2 * arch/s390/kernel/early.c
4 * Copyright IBM Corp. 2007
5 * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
6 * Heiko Carstens <heiko.carstens@de.ibm.com>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/ctype.h>
13 #include <linux/lockdep.h>
14 #include <linux/module.h>
15 #include <linux/pfn.h>
16 #include <linux/uaccess.h>
18 #include <asm/lowcore.h>
19 #include <asm/processor.h>
20 #include <asm/sections.h>
21 #include <asm/setup.h>
22 #include <asm/cpcmd.h>
26 * Create a Kernel NSS if the SAVESYS= parameter is defined
28 #define DEFSYS_CMD_SIZE 96
29 #define SAVESYS_CMD_SIZE 32
31 char kernel_nss_name[NSS_NAME_SIZE + 1];
33 #ifdef CONFIG_SHARED_KERNEL
34 static noinline __init void create_kernel_nss(void)
36 unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size;
37 #ifdef CONFIG_BLK_DEV_INITRD
38 unsigned int sinitrd_pfn, einitrd_pfn;
42 char upper_command_line[COMMAND_LINE_SIZE];
43 char defsys_cmd[DEFSYS_CMD_SIZE];
44 char savesys_cmd[SAVESYS_CMD_SIZE];
46 /* Do nothing if we are not running under VM */
50 /* Convert COMMAND_LINE to upper case */
51 for (i = 0; i < strlen(COMMAND_LINE); i++)
52 upper_command_line[i] = toupper(COMMAND_LINE[i]);
54 savesys_ptr = strstr(upper_command_line, "SAVESYS=");
59 savesys_ptr += 8; /* Point to the beginning of the NSS name */
60 for (i = 0; i < NSS_NAME_SIZE; i++) {
61 if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0')
63 kernel_nss_name[i] = savesys_ptr[i];
66 stext_pfn = PFN_DOWN(__pa(&_stext));
67 eshared_pfn = PFN_DOWN(__pa(&_eshared));
68 end_pfn = PFN_UP(__pa(&_end));
69 min_size = end_pfn << 2;
71 sprintf(defsys_cmd, "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
72 kernel_nss_name, stext_pfn - 1, stext_pfn, eshared_pfn - 1,
73 eshared_pfn, end_pfn);
75 #ifdef CONFIG_BLK_DEV_INITRD
76 if (INITRD_START && INITRD_SIZE) {
77 sinitrd_pfn = PFN_DOWN(__pa(INITRD_START));
78 einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE));
79 min_size = einitrd_pfn << 2;
80 sprintf(defsys_cmd, "%s EW %.5X-%.5X", defsys_cmd,
81 sinitrd_pfn, einitrd_pfn);
85 sprintf(defsys_cmd, "%s EW MINSIZE=%.7iK", defsys_cmd, min_size);
86 sprintf(savesys_cmd, "SAVESYS %s \n IPL %s",
87 kernel_nss_name, kernel_nss_name);
89 __cpcmd(defsys_cmd, NULL, 0, &response);
92 kernel_nss_name[0] = '\0';
96 __cpcmd(savesys_cmd, NULL, 0, &response);
98 if (response != strlen(savesys_cmd)) {
99 kernel_nss_name[0] = '\0';
103 ipl_flags = IPL_NSS_VALID;
106 #else /* CONFIG_SHARED_KERNEL */
108 static inline void create_kernel_nss(void) { }
110 #endif /* CONFIG_SHARED_KERNEL */
115 static noinline __init void clear_bss_section(void)
117 memset(__bss_start, 0, __bss_stop - __bss_start);
121 * Initialize storage key for kernel pages
123 static noinline __init void init_kernel_storage_key(void)
125 unsigned long end_pfn, init_pfn;
127 end_pfn = PFN_UP(__pa(&_end));
129 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
130 page_set_storage_key(init_pfn << PAGE_SHIFT, PAGE_DEFAULT_KEY);
133 static noinline __init void detect_machine_type(void)
135 struct cpuinfo_S390 *cpuinfo = &S390_lowcore.cpu_data;
137 get_cpu_id(&S390_lowcore.cpu_data.cpu_id);
139 /* Running under z/VM ? */
140 if (cpuinfo->cpu_id.version == 0xff)
143 /* Running on a P/390 ? */
144 if (cpuinfo->cpu_id.machine == 0x7490)
149 static noinline __init int memory_fast_detect(void)
151 unsigned long val0 = 0;
152 unsigned long val1 = 0xc;
155 if (ipl_flags & IPL_NSS_VALID)
159 " diag %1,%2,0x260\n"
163 : "+d" (ret), "+d" (val0), "+d" (val1) : : "cc");
165 if (ret || val0 != val1)
168 memory_chunk[0].size = val0 + 1;
172 static inline int memory_fast_detect(void)
178 static inline __init unsigned long __tprot(unsigned long addr)
188 : "+d" (cc) : "a" (addr) : "cc");
189 return (unsigned long)cc;
192 /* Checking memory in 128KB increments. */
193 #define CHUNK_INCR (1UL << 17)
194 #define ADDR2G (1UL << 31)
196 static noinline __init void find_memory_chunks(unsigned long memsize)
198 unsigned long addr = 0, old_addr = 0;
199 unsigned long old_cc = CHUNK_READ_WRITE;
203 while (chunk < MEMORY_CHUNKS) {
205 while (cc == old_cc) {
207 if (memsize && addr >= memsize)
216 if (old_addr != addr &&
217 (old_cc == CHUNK_READ_WRITE || old_cc == CHUNK_READ_ONLY)) {
218 memory_chunk[chunk].addr = old_addr;
219 memory_chunk[chunk].size = addr - old_addr;
220 memory_chunk[chunk].type = old_cc;
232 * Finish memory detection at the first hole
233 * if storage size is unknown.
235 if (cc == -1UL && !memsize)
237 if (memsize && addr >= memsize)
242 static __init void early_pgm_check_handler(void)
245 const struct exception_table_entry *fixup;
247 addr = S390_lowcore.program_old_psw.addr;
248 fixup = search_exception_tables(addr & PSW_ADDR_INSN);
251 S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE;
254 static noinline __init void setup_lowcore_early(void)
258 psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
259 psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler;
260 S390_lowcore.external_new_psw = psw;
261 psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler;
262 S390_lowcore.program_new_psw = psw;
263 s390_base_pgm_handler_fn = early_pgm_check_handler;
267 * Save ipl parameters, clear bss memory, initialize storage keys
268 * and create a kernel NSS at startup if the SAVESYS= parm is defined
270 void __init startup_init(void)
272 unsigned long long memsize;
274 ipl_save_parameters();
276 init_kernel_storage_key();
279 detect_machine_type();
282 setup_lowcore_early();
283 sclp_read_info_early();
284 sclp_facilities_detect();
285 memsize = sclp_memory_detect();
288 * Can't deal with more than 2G in 31 bit addressing mode, so
289 * limit the value in order to avoid strange side effects.
291 if (memsize > ADDR2G)
294 if (memory_fast_detect() < 0)
295 find_memory_chunks((unsigned long) memsize);