1ab14de6cSHeiko Carstens /* 2ab14de6cSHeiko Carstens * arch/s390/kernel/early.c 3ab14de6cSHeiko Carstens * 4ab14de6cSHeiko Carstens * Copyright IBM Corp. 2007 5ab14de6cSHeiko Carstens * Author(s): Hongjie Yang <hongjie@us.ibm.com>, 6ab14de6cSHeiko Carstens * Heiko Carstens <heiko.carstens@de.ibm.com> 7ab14de6cSHeiko Carstens */ 8ab14de6cSHeiko Carstens 9ab14de6cSHeiko Carstens #include <linux/init.h> 10ab14de6cSHeiko Carstens #include <linux/errno.h> 11ab14de6cSHeiko Carstens #include <linux/string.h> 12ab14de6cSHeiko Carstens #include <linux/ctype.h> 13ab14de6cSHeiko Carstens #include <linux/lockdep.h> 14ab14de6cSHeiko Carstens #include <linux/module.h> 15ab14de6cSHeiko Carstens #include <linux/pfn.h> 16ab14de6cSHeiko Carstens #include <linux/uaccess.h> 1746b05d26SMichael Holzheu #include <asm/ipl.h> 18ab14de6cSHeiko Carstens #include <asm/lowcore.h> 19ab14de6cSHeiko Carstens #include <asm/processor.h> 20ab14de6cSHeiko Carstens #include <asm/sections.h> 21ab14de6cSHeiko Carstens #include <asm/setup.h> 22ab14de6cSHeiko Carstens #include <asm/cpcmd.h> 23ab14de6cSHeiko Carstens #include <asm/sclp.h> 24ab14de6cSHeiko Carstens 25ab14de6cSHeiko Carstens /* 26ab14de6cSHeiko Carstens * Create a Kernel NSS if the SAVESYS= parameter is defined 27ab14de6cSHeiko Carstens */ 28ab14de6cSHeiko Carstens #define DEFSYS_CMD_SIZE 96 29ab14de6cSHeiko Carstens #define SAVESYS_CMD_SIZE 32 30ab14de6cSHeiko Carstens 31ab14de6cSHeiko Carstens char kernel_nss_name[NSS_NAME_SIZE + 1]; 32ab14de6cSHeiko Carstens 33ab14de6cSHeiko Carstens #ifdef CONFIG_SHARED_KERNEL 34ab14de6cSHeiko Carstens static noinline __init void create_kernel_nss(void) 35ab14de6cSHeiko Carstens { 36ab14de6cSHeiko Carstens unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size; 37ab14de6cSHeiko Carstens #ifdef CONFIG_BLK_DEV_INITRD 38ab14de6cSHeiko Carstens unsigned int sinitrd_pfn, einitrd_pfn; 39ab14de6cSHeiko Carstens #endif 40ab14de6cSHeiko Carstens int response; 41ab14de6cSHeiko Carstens char *savesys_ptr; 42ab14de6cSHeiko Carstens char upper_command_line[COMMAND_LINE_SIZE]; 43ab14de6cSHeiko Carstens char defsys_cmd[DEFSYS_CMD_SIZE]; 44ab14de6cSHeiko Carstens char savesys_cmd[SAVESYS_CMD_SIZE]; 45ab14de6cSHeiko Carstens 46ab14de6cSHeiko Carstens /* Do nothing if we are not running under VM */ 47ab14de6cSHeiko Carstens if (!MACHINE_IS_VM) 48ab14de6cSHeiko Carstens return; 49ab14de6cSHeiko Carstens 50ab14de6cSHeiko Carstens /* Convert COMMAND_LINE to upper case */ 51ab14de6cSHeiko Carstens for (i = 0; i < strlen(COMMAND_LINE); i++) 52ab14de6cSHeiko Carstens upper_command_line[i] = toupper(COMMAND_LINE[i]); 53ab14de6cSHeiko Carstens 54ab14de6cSHeiko Carstens savesys_ptr = strstr(upper_command_line, "SAVESYS="); 55ab14de6cSHeiko Carstens 56ab14de6cSHeiko Carstens if (!savesys_ptr) 57ab14de6cSHeiko Carstens return; 58ab14de6cSHeiko Carstens 59ab14de6cSHeiko Carstens savesys_ptr += 8; /* Point to the beginning of the NSS name */ 60ab14de6cSHeiko Carstens for (i = 0; i < NSS_NAME_SIZE; i++) { 61ab14de6cSHeiko Carstens if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0') 62ab14de6cSHeiko Carstens break; 63ab14de6cSHeiko Carstens kernel_nss_name[i] = savesys_ptr[i]; 64ab14de6cSHeiko Carstens } 65ab14de6cSHeiko Carstens 66ab14de6cSHeiko Carstens stext_pfn = PFN_DOWN(__pa(&_stext)); 67ab14de6cSHeiko Carstens eshared_pfn = PFN_DOWN(__pa(&_eshared)); 68ab14de6cSHeiko Carstens end_pfn = PFN_UP(__pa(&_end)); 69ab14de6cSHeiko Carstens min_size = end_pfn << 2; 70ab14de6cSHeiko Carstens 71ab14de6cSHeiko Carstens sprintf(defsys_cmd, "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X", 72ab14de6cSHeiko Carstens kernel_nss_name, stext_pfn - 1, stext_pfn, eshared_pfn - 1, 73ab14de6cSHeiko Carstens eshared_pfn, end_pfn); 74ab14de6cSHeiko Carstens 75ab14de6cSHeiko Carstens #ifdef CONFIG_BLK_DEV_INITRD 76ab14de6cSHeiko Carstens if (INITRD_START && INITRD_SIZE) { 77ab14de6cSHeiko Carstens sinitrd_pfn = PFN_DOWN(__pa(INITRD_START)); 78ab14de6cSHeiko Carstens einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE)); 79ab14de6cSHeiko Carstens min_size = einitrd_pfn << 2; 80ab14de6cSHeiko Carstens sprintf(defsys_cmd, "%s EW %.5X-%.5X", defsys_cmd, 81ab14de6cSHeiko Carstens sinitrd_pfn, einitrd_pfn); 82ab14de6cSHeiko Carstens } 83ab14de6cSHeiko Carstens #endif 84ab14de6cSHeiko Carstens 85ab14de6cSHeiko Carstens sprintf(defsys_cmd, "%s EW MINSIZE=%.7iK", defsys_cmd, min_size); 86ab14de6cSHeiko Carstens sprintf(savesys_cmd, "SAVESYS %s \n IPL %s", 87ab14de6cSHeiko Carstens kernel_nss_name, kernel_nss_name); 88ab14de6cSHeiko Carstens 89ab14de6cSHeiko Carstens __cpcmd(defsys_cmd, NULL, 0, &response); 90ab14de6cSHeiko Carstens 91ab14de6cSHeiko Carstens if (response != 0) 92ab14de6cSHeiko Carstens return; 93ab14de6cSHeiko Carstens 94ab14de6cSHeiko Carstens __cpcmd(savesys_cmd, NULL, 0, &response); 95ab14de6cSHeiko Carstens 96ab14de6cSHeiko Carstens if (response != strlen(savesys_cmd)) 97ab14de6cSHeiko Carstens return; 98ab14de6cSHeiko Carstens 99ab14de6cSHeiko Carstens ipl_flags = IPL_NSS_VALID; 100ab14de6cSHeiko Carstens } 101ab14de6cSHeiko Carstens 102ab14de6cSHeiko Carstens #else /* CONFIG_SHARED_KERNEL */ 103ab14de6cSHeiko Carstens 104ab14de6cSHeiko Carstens static inline void create_kernel_nss(void) { } 105ab14de6cSHeiko Carstens 106ab14de6cSHeiko Carstens #endif /* CONFIG_SHARED_KERNEL */ 107ab14de6cSHeiko Carstens 108ab14de6cSHeiko Carstens /* 109ab14de6cSHeiko Carstens * Clear bss memory 110ab14de6cSHeiko Carstens */ 111ab14de6cSHeiko Carstens static noinline __init void clear_bss_section(void) 112ab14de6cSHeiko Carstens { 113229d9c6dSHeiko Carstens memset(__bss_start, 0, __bss_stop - __bss_start); 114ab14de6cSHeiko Carstens } 115ab14de6cSHeiko Carstens 116ab14de6cSHeiko Carstens /* 117ab14de6cSHeiko Carstens * Initialize storage key for kernel pages 118ab14de6cSHeiko Carstens */ 119ab14de6cSHeiko Carstens static noinline __init void init_kernel_storage_key(void) 120ab14de6cSHeiko Carstens { 121ab14de6cSHeiko Carstens unsigned long end_pfn, init_pfn; 122ab14de6cSHeiko Carstens 123ab14de6cSHeiko Carstens end_pfn = PFN_UP(__pa(&_end)); 124ab14de6cSHeiko Carstens 125ab14de6cSHeiko Carstens for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++) 126ab14de6cSHeiko Carstens page_set_storage_key(init_pfn << PAGE_SHIFT, PAGE_DEFAULT_KEY); 127ab14de6cSHeiko Carstens } 128ab14de6cSHeiko Carstens 129ab14de6cSHeiko Carstens static noinline __init void detect_machine_type(void) 130ab14de6cSHeiko Carstens { 131ab14de6cSHeiko Carstens struct cpuinfo_S390 *cpuinfo = &S390_lowcore.cpu_data; 132ab14de6cSHeiko Carstens 13372960a02SMichael Holzheu get_cpu_id(&S390_lowcore.cpu_data.cpu_id); 134ab14de6cSHeiko Carstens 135ab14de6cSHeiko Carstens /* Running under z/VM ? */ 136ab14de6cSHeiko Carstens if (cpuinfo->cpu_id.version == 0xff) 137ab14de6cSHeiko Carstens machine_flags |= 1; 138ab14de6cSHeiko Carstens 139ab14de6cSHeiko Carstens /* Running on a P/390 ? */ 140ab14de6cSHeiko Carstens if (cpuinfo->cpu_id.machine == 0x7490) 141ab14de6cSHeiko Carstens machine_flags |= 4; 142ab14de6cSHeiko Carstens } 143ab14de6cSHeiko Carstens 144257c6e1cSHeiko Carstens #ifdef CONFIG_64BIT 145ab14de6cSHeiko Carstens static noinline __init int memory_fast_detect(void) 146ab14de6cSHeiko Carstens { 147ab14de6cSHeiko Carstens unsigned long val0 = 0; 148ab14de6cSHeiko Carstens unsigned long val1 = 0xc; 149ab14de6cSHeiko Carstens int ret = -ENOSYS; 150ab14de6cSHeiko Carstens 151ab14de6cSHeiko Carstens if (ipl_flags & IPL_NSS_VALID) 152ab14de6cSHeiko Carstens return -ENOSYS; 153ab14de6cSHeiko Carstens 154ab14de6cSHeiko Carstens asm volatile( 155ab14de6cSHeiko Carstens " diag %1,%2,0x260\n" 156ab14de6cSHeiko Carstens "0: lhi %0,0\n" 157ab14de6cSHeiko Carstens "1:\n" 158ab14de6cSHeiko Carstens EX_TABLE(0b,1b) 159ab14de6cSHeiko Carstens : "+d" (ret), "+d" (val0), "+d" (val1) : : "cc"); 160ab14de6cSHeiko Carstens 161ab14de6cSHeiko Carstens if (ret || val0 != val1) 162ab14de6cSHeiko Carstens return -ENOSYS; 163ab14de6cSHeiko Carstens 164257c6e1cSHeiko Carstens memory_chunk[0].size = val0 + 1; 165ab14de6cSHeiko Carstens return 0; 166ab14de6cSHeiko Carstens } 167257c6e1cSHeiko Carstens #else 168257c6e1cSHeiko Carstens static inline int memory_fast_detect(void) 169257c6e1cSHeiko Carstens { 170257c6e1cSHeiko Carstens return -ENOSYS; 171257c6e1cSHeiko Carstens } 172257c6e1cSHeiko Carstens #endif 173ab14de6cSHeiko Carstens 174ab14de6cSHeiko Carstens static inline __init unsigned long __tprot(unsigned long addr) 175ab14de6cSHeiko Carstens { 176ab14de6cSHeiko Carstens int cc = -1; 177ab14de6cSHeiko Carstens 178ab14de6cSHeiko Carstens asm volatile( 179ab14de6cSHeiko Carstens " tprot 0(%1),0\n" 180ab14de6cSHeiko Carstens "0: ipm %0\n" 181ab14de6cSHeiko Carstens " srl %0,28\n" 182ab14de6cSHeiko Carstens "1:\n" 183ab14de6cSHeiko Carstens EX_TABLE(0b,1b) 184ab14de6cSHeiko Carstens : "+d" (cc) : "a" (addr) : "cc"); 185ab14de6cSHeiko Carstens return (unsigned long)cc; 186ab14de6cSHeiko Carstens } 187ab14de6cSHeiko Carstens 188ab14de6cSHeiko Carstens /* Checking memory in 128KB increments. */ 189ab14de6cSHeiko Carstens #define CHUNK_INCR (1UL << 17) 19005dd2530SHeiko Carstens #define ADDR2G (1UL << 31) 191ab14de6cSHeiko Carstens 192ab14de6cSHeiko Carstens static noinline __init void find_memory_chunks(unsigned long memsize) 193ab14de6cSHeiko Carstens { 194ab14de6cSHeiko Carstens unsigned long addr = 0, old_addr = 0; 195ab14de6cSHeiko Carstens unsigned long old_cc = CHUNK_READ_WRITE; 196ab14de6cSHeiko Carstens unsigned long cc; 197ab14de6cSHeiko Carstens int chunk = 0; 198ab14de6cSHeiko Carstens 199ab14de6cSHeiko Carstens while (chunk < MEMORY_CHUNKS) { 200ab14de6cSHeiko Carstens cc = __tprot(addr); 201ab14de6cSHeiko Carstens while (cc == old_cc) { 202ab14de6cSHeiko Carstens addr += CHUNK_INCR; 203*a2cb0737SHeiko Carstens if (memsize && addr >= memsize) 2040b8da654SHeiko Carstens break; 205ab14de6cSHeiko Carstens #ifndef CONFIG_64BIT 206ab14de6cSHeiko Carstens if (addr == ADDR2G) 207ab14de6cSHeiko Carstens break; 208ab14de6cSHeiko Carstens #endif 2090b8da654SHeiko Carstens cc = __tprot(addr); 210ab14de6cSHeiko Carstens } 211ab14de6cSHeiko Carstens 212ab14de6cSHeiko Carstens if (old_addr != addr && 213ab14de6cSHeiko Carstens (old_cc == CHUNK_READ_WRITE || old_cc == CHUNK_READ_ONLY)) { 214ab14de6cSHeiko Carstens memory_chunk[chunk].addr = old_addr; 215ab14de6cSHeiko Carstens memory_chunk[chunk].size = addr - old_addr; 216ab14de6cSHeiko Carstens memory_chunk[chunk].type = old_cc; 217ab14de6cSHeiko Carstens chunk++; 218ab14de6cSHeiko Carstens } 219ab14de6cSHeiko Carstens 220ab14de6cSHeiko Carstens old_addr = addr; 221ab14de6cSHeiko Carstens old_cc = cc; 222ab14de6cSHeiko Carstens 223ab14de6cSHeiko Carstens #ifndef CONFIG_64BIT 224ab14de6cSHeiko Carstens if (addr == ADDR2G) 225ab14de6cSHeiko Carstens break; 226ab14de6cSHeiko Carstens #endif 227ab14de6cSHeiko Carstens /* 22829c380f5SHeiko Carstens * Finish memory detection at the first hole 22929c380f5SHeiko Carstens * if storage size is unknown. 230ab14de6cSHeiko Carstens */ 23129c380f5SHeiko Carstens if (cc == -1UL && !memsize) 232ab14de6cSHeiko Carstens break; 233ab14de6cSHeiko Carstens if (memsize && addr >= memsize) 234ab14de6cSHeiko Carstens break; 235ab14de6cSHeiko Carstens } 236ab14de6cSHeiko Carstens } 237ab14de6cSHeiko Carstens 238ab14de6cSHeiko Carstens static __init void early_pgm_check_handler(void) 239ab14de6cSHeiko Carstens { 240ab14de6cSHeiko Carstens unsigned long addr; 241ab14de6cSHeiko Carstens const struct exception_table_entry *fixup; 242ab14de6cSHeiko Carstens 243ab14de6cSHeiko Carstens addr = S390_lowcore.program_old_psw.addr; 244ab14de6cSHeiko Carstens fixup = search_exception_tables(addr & PSW_ADDR_INSN); 245ab14de6cSHeiko Carstens if (!fixup) 246ab14de6cSHeiko Carstens disabled_wait(0); 247ab14de6cSHeiko Carstens S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE; 248ab14de6cSHeiko Carstens } 249ab14de6cSHeiko Carstens 250ab14de6cSHeiko Carstens static noinline __init void setup_lowcore_early(void) 251ab14de6cSHeiko Carstens { 252ab14de6cSHeiko Carstens psw_t psw; 253ab14de6cSHeiko Carstens 254ab14de6cSHeiko Carstens psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY; 255ab14de6cSHeiko Carstens psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler; 256ab14de6cSHeiko Carstens S390_lowcore.external_new_psw = psw; 257ab14de6cSHeiko Carstens psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler; 258ab14de6cSHeiko Carstens S390_lowcore.program_new_psw = psw; 259ab14de6cSHeiko Carstens s390_base_pgm_handler_fn = early_pgm_check_handler; 260ab14de6cSHeiko Carstens } 261ab14de6cSHeiko Carstens 262ab14de6cSHeiko Carstens /* 263ab14de6cSHeiko Carstens * Save ipl parameters, clear bss memory, initialize storage keys 264ab14de6cSHeiko Carstens * and create a kernel NSS at startup if the SAVESYS= parm is defined 265ab14de6cSHeiko Carstens */ 266ab14de6cSHeiko Carstens void __init startup_init(void) 267ab14de6cSHeiko Carstens { 26805dd2530SHeiko Carstens unsigned long long memsize; 269ab14de6cSHeiko Carstens 270ab14de6cSHeiko Carstens ipl_save_parameters(); 271ab14de6cSHeiko Carstens clear_bss_section(); 272ab14de6cSHeiko Carstens init_kernel_storage_key(); 273ab14de6cSHeiko Carstens lockdep_init(); 274ab14de6cSHeiko Carstens lockdep_off(); 275ab14de6cSHeiko Carstens detect_machine_type(); 276ab14de6cSHeiko Carstens create_kernel_nss(); 277ab14de6cSHeiko Carstens sort_main_extable(); 278ab14de6cSHeiko Carstens setup_lowcore_early(); 279ab14de6cSHeiko Carstens sclp_readinfo_early(); 28083119ad4SHeiko Carstens sclp_facilities_detect(); 281ab14de6cSHeiko Carstens memsize = sclp_memory_detect(); 28205dd2530SHeiko Carstens #ifndef CONFIG_64BIT 28305dd2530SHeiko Carstens /* 28405dd2530SHeiko Carstens * Can't deal with more than 2G in 31 bit addressing mode, so 28505dd2530SHeiko Carstens * limit the value in order to avoid strange side effects. 28605dd2530SHeiko Carstens */ 28705dd2530SHeiko Carstens if (memsize > ADDR2G) 28805dd2530SHeiko Carstens memsize = ADDR2G; 28905dd2530SHeiko Carstens #endif 290ab14de6cSHeiko Carstens if (memory_fast_detect() < 0) 29105dd2530SHeiko Carstens find_memory_chunks((unsigned long) memsize); 292ab14de6cSHeiko Carstens lockdep_on(); 293ab14de6cSHeiko Carstens } 294