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 144*257c6e1cSHeiko 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 164*257c6e1cSHeiko Carstens memory_chunk[0].size = val0 + 1; 165ab14de6cSHeiko Carstens return 0; 166ab14de6cSHeiko Carstens } 167*257c6e1cSHeiko Carstens #else 168*257c6e1cSHeiko Carstens static inline int memory_fast_detect(void) 169*257c6e1cSHeiko Carstens { 170*257c6e1cSHeiko Carstens return -ENOSYS; 171*257c6e1cSHeiko Carstens } 172*257c6e1cSHeiko Carstens #endif 173ab14de6cSHeiko Carstens 174ab14de6cSHeiko Carstens #define ADDR2G (1UL << 31) 175ab14de6cSHeiko Carstens 176ab14de6cSHeiko Carstens static noinline __init unsigned long sclp_memory_detect(void) 177ab14de6cSHeiko Carstens { 178ab14de6cSHeiko Carstens struct sclp_readinfo_sccb *sccb; 179ab14de6cSHeiko Carstens unsigned long long memsize; 180ab14de6cSHeiko Carstens 181ab14de6cSHeiko Carstens sccb = &s390_readinfo_sccb; 182ab14de6cSHeiko Carstens 183ab14de6cSHeiko Carstens if (sccb->header.response_code != 0x10) 184ab14de6cSHeiko Carstens return 0; 185ab14de6cSHeiko Carstens 186ab14de6cSHeiko Carstens if (sccb->rnsize) 187ab14de6cSHeiko Carstens memsize = sccb->rnsize << 20; 188ab14de6cSHeiko Carstens else 189ab14de6cSHeiko Carstens memsize = sccb->rnsize2 << 20; 190ab14de6cSHeiko Carstens if (sccb->rnmax) 191ab14de6cSHeiko Carstens memsize *= sccb->rnmax; 192ab14de6cSHeiko Carstens else 193ab14de6cSHeiko Carstens memsize *= sccb->rnmax2; 194ab14de6cSHeiko Carstens #ifndef CONFIG_64BIT 195ab14de6cSHeiko Carstens /* 196ab14de6cSHeiko Carstens * Can't deal with more than 2G in 31 bit addressing mode, so 197ab14de6cSHeiko Carstens * limit the value in order to avoid strange side effects. 198ab14de6cSHeiko Carstens */ 199ab14de6cSHeiko Carstens if (memsize > ADDR2G) 200ab14de6cSHeiko Carstens memsize = ADDR2G; 201ab14de6cSHeiko Carstens #endif 202ab14de6cSHeiko Carstens return (unsigned long) memsize; 203ab14de6cSHeiko Carstens } 204ab14de6cSHeiko Carstens 205ab14de6cSHeiko Carstens static inline __init unsigned long __tprot(unsigned long addr) 206ab14de6cSHeiko Carstens { 207ab14de6cSHeiko Carstens int cc = -1; 208ab14de6cSHeiko Carstens 209ab14de6cSHeiko Carstens asm volatile( 210ab14de6cSHeiko Carstens " tprot 0(%1),0\n" 211ab14de6cSHeiko Carstens "0: ipm %0\n" 212ab14de6cSHeiko Carstens " srl %0,28\n" 213ab14de6cSHeiko Carstens "1:\n" 214ab14de6cSHeiko Carstens EX_TABLE(0b,1b) 215ab14de6cSHeiko Carstens : "+d" (cc) : "a" (addr) : "cc"); 216ab14de6cSHeiko Carstens return (unsigned long)cc; 217ab14de6cSHeiko Carstens } 218ab14de6cSHeiko Carstens 219ab14de6cSHeiko Carstens /* Checking memory in 128KB increments. */ 220ab14de6cSHeiko Carstens #define CHUNK_INCR (1UL << 17) 221ab14de6cSHeiko Carstens 222ab14de6cSHeiko Carstens static noinline __init void find_memory_chunks(unsigned long memsize) 223ab14de6cSHeiko Carstens { 224ab14de6cSHeiko Carstens unsigned long addr = 0, old_addr = 0; 225ab14de6cSHeiko Carstens unsigned long old_cc = CHUNK_READ_WRITE; 226ab14de6cSHeiko Carstens unsigned long cc; 227ab14de6cSHeiko Carstens int chunk = 0; 228ab14de6cSHeiko Carstens 229ab14de6cSHeiko Carstens while (chunk < MEMORY_CHUNKS) { 230ab14de6cSHeiko Carstens cc = __tprot(addr); 231ab14de6cSHeiko Carstens while (cc == old_cc) { 232ab14de6cSHeiko Carstens addr += CHUNK_INCR; 233ab14de6cSHeiko Carstens cc = __tprot(addr); 234ab14de6cSHeiko Carstens #ifndef CONFIG_64BIT 235ab14de6cSHeiko Carstens if (addr == ADDR2G) 236ab14de6cSHeiko Carstens break; 237ab14de6cSHeiko Carstens #endif 238ab14de6cSHeiko Carstens } 239ab14de6cSHeiko Carstens 240ab14de6cSHeiko Carstens if (old_addr != addr && 241ab14de6cSHeiko Carstens (old_cc == CHUNK_READ_WRITE || old_cc == CHUNK_READ_ONLY)) { 242ab14de6cSHeiko Carstens memory_chunk[chunk].addr = old_addr; 243ab14de6cSHeiko Carstens memory_chunk[chunk].size = addr - old_addr; 244ab14de6cSHeiko Carstens memory_chunk[chunk].type = old_cc; 245ab14de6cSHeiko Carstens chunk++; 246ab14de6cSHeiko Carstens } 247ab14de6cSHeiko Carstens 248ab14de6cSHeiko Carstens old_addr = addr; 249ab14de6cSHeiko Carstens old_cc = cc; 250ab14de6cSHeiko Carstens 251ab14de6cSHeiko Carstens #ifndef CONFIG_64BIT 252ab14de6cSHeiko Carstens if (addr == ADDR2G) 253ab14de6cSHeiko Carstens break; 254ab14de6cSHeiko Carstens #endif 255ab14de6cSHeiko Carstens /* 256ab14de6cSHeiko Carstens * Finish memory detection at the first hole, unless 257ab14de6cSHeiko Carstens * - we reached the hsa -> skip it. 258ab14de6cSHeiko Carstens * - we know there must be more. 259ab14de6cSHeiko Carstens */ 260ab14de6cSHeiko Carstens if (cc == -1UL && !memsize && old_addr != ADDR2G) 261ab14de6cSHeiko Carstens break; 262ab14de6cSHeiko Carstens if (memsize && addr >= memsize) 263ab14de6cSHeiko Carstens break; 264ab14de6cSHeiko Carstens } 265ab14de6cSHeiko Carstens } 266ab14de6cSHeiko Carstens 267ab14de6cSHeiko Carstens static __init void early_pgm_check_handler(void) 268ab14de6cSHeiko Carstens { 269ab14de6cSHeiko Carstens unsigned long addr; 270ab14de6cSHeiko Carstens const struct exception_table_entry *fixup; 271ab14de6cSHeiko Carstens 272ab14de6cSHeiko Carstens addr = S390_lowcore.program_old_psw.addr; 273ab14de6cSHeiko Carstens fixup = search_exception_tables(addr & PSW_ADDR_INSN); 274ab14de6cSHeiko Carstens if (!fixup) 275ab14de6cSHeiko Carstens disabled_wait(0); 276ab14de6cSHeiko Carstens S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE; 277ab14de6cSHeiko Carstens } 278ab14de6cSHeiko Carstens 279ab14de6cSHeiko Carstens static noinline __init void setup_lowcore_early(void) 280ab14de6cSHeiko Carstens { 281ab14de6cSHeiko Carstens psw_t psw; 282ab14de6cSHeiko Carstens 283ab14de6cSHeiko Carstens psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY; 284ab14de6cSHeiko Carstens psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler; 285ab14de6cSHeiko Carstens S390_lowcore.external_new_psw = psw; 286ab14de6cSHeiko Carstens psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler; 287ab14de6cSHeiko Carstens S390_lowcore.program_new_psw = psw; 288ab14de6cSHeiko Carstens s390_base_pgm_handler_fn = early_pgm_check_handler; 289ab14de6cSHeiko Carstens } 290ab14de6cSHeiko Carstens 291ab14de6cSHeiko Carstens /* 292ab14de6cSHeiko Carstens * Save ipl parameters, clear bss memory, initialize storage keys 293ab14de6cSHeiko Carstens * and create a kernel NSS at startup if the SAVESYS= parm is defined 294ab14de6cSHeiko Carstens */ 295ab14de6cSHeiko Carstens void __init startup_init(void) 296ab14de6cSHeiko Carstens { 297ab14de6cSHeiko Carstens unsigned long memsize; 298ab14de6cSHeiko Carstens 299ab14de6cSHeiko Carstens ipl_save_parameters(); 300ab14de6cSHeiko Carstens clear_bss_section(); 301ab14de6cSHeiko Carstens init_kernel_storage_key(); 302ab14de6cSHeiko Carstens lockdep_init(); 303ab14de6cSHeiko Carstens lockdep_off(); 304ab14de6cSHeiko Carstens detect_machine_type(); 305ab14de6cSHeiko Carstens create_kernel_nss(); 306ab14de6cSHeiko Carstens sort_main_extable(); 307ab14de6cSHeiko Carstens setup_lowcore_early(); 308ab14de6cSHeiko Carstens sclp_readinfo_early(); 309ab14de6cSHeiko Carstens memsize = sclp_memory_detect(); 310ab14de6cSHeiko Carstens if (memory_fast_detect() < 0) 311ab14de6cSHeiko Carstens find_memory_chunks(memsize); 312ab14de6cSHeiko Carstens lockdep_on(); 313ab14de6cSHeiko Carstens } 314