Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6] / arch / s390 / kernel / early.c
1 /*
2  *  arch/s390/kernel/early.c
3  *
4  *    Copyright IBM Corp. 2007
5  *    Author(s): Hongjie Yang <hongjie@us.ibm.com>,
6  *               Heiko Carstens <heiko.carstens@de.ibm.com>
7  */
8
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>
17 #include <asm/ipl.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>
23 #include <asm/sclp.h>
24 #include "entry.h"
25
26 /*
27  * Create a Kernel NSS if the SAVESYS= parameter is defined
28  */
29 #define DEFSYS_CMD_SIZE         96
30 #define SAVESYS_CMD_SIZE        32
31
32 char kernel_nss_name[NSS_NAME_SIZE + 1];
33
34 #ifdef CONFIG_SHARED_KERNEL
35 static noinline __init void create_kernel_nss(void)
36 {
37         unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size;
38 #ifdef CONFIG_BLK_DEV_INITRD
39         unsigned int sinitrd_pfn, einitrd_pfn;
40 #endif
41         int response;
42         char *savesys_ptr;
43         char upper_command_line[COMMAND_LINE_SIZE];
44         char defsys_cmd[DEFSYS_CMD_SIZE];
45         char savesys_cmd[SAVESYS_CMD_SIZE];
46
47         /* Do nothing if we are not running under VM */
48         if (!MACHINE_IS_VM)
49                 return;
50
51         /* Convert COMMAND_LINE to upper case */
52         for (i = 0; i < strlen(COMMAND_LINE); i++)
53                 upper_command_line[i] = toupper(COMMAND_LINE[i]);
54
55         savesys_ptr = strstr(upper_command_line, "SAVESYS=");
56
57         if (!savesys_ptr)
58                 return;
59
60         savesys_ptr += 8;    /* Point to the beginning of the NSS name */
61         for (i = 0; i < NSS_NAME_SIZE; i++) {
62                 if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0')
63                         break;
64                 kernel_nss_name[i] = savesys_ptr[i];
65         }
66
67         stext_pfn = PFN_DOWN(__pa(&_stext));
68         eshared_pfn = PFN_DOWN(__pa(&_eshared));
69         end_pfn = PFN_UP(__pa(&_end));
70         min_size = end_pfn << 2;
71
72         sprintf(defsys_cmd, "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
73                 kernel_nss_name, stext_pfn - 1, stext_pfn, eshared_pfn - 1,
74                 eshared_pfn, end_pfn);
75
76 #ifdef CONFIG_BLK_DEV_INITRD
77         if (INITRD_START && INITRD_SIZE) {
78                 sinitrd_pfn = PFN_DOWN(__pa(INITRD_START));
79                 einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE));
80                 min_size = einitrd_pfn << 2;
81                 sprintf(defsys_cmd, "%s EW %.5X-%.5X", defsys_cmd,
82                 sinitrd_pfn, einitrd_pfn);
83         }
84 #endif
85
86         sprintf(defsys_cmd, "%s EW MINSIZE=%.7iK", defsys_cmd, min_size);
87         sprintf(savesys_cmd, "SAVESYS %s \n IPL %s",
88                 kernel_nss_name, kernel_nss_name);
89
90         __cpcmd(defsys_cmd, NULL, 0, &response);
91
92         if (response != 0) {
93                 kernel_nss_name[0] = '\0';
94                 return;
95         }
96
97         __cpcmd(savesys_cmd, NULL, 0, &response);
98
99         if (response != strlen(savesys_cmd)) {
100                 kernel_nss_name[0] = '\0';
101                 return;
102         }
103
104         ipl_flags = IPL_NSS_VALID;
105 }
106
107 #else /* CONFIG_SHARED_KERNEL */
108
109 static inline void create_kernel_nss(void) { }
110
111 #endif /* CONFIG_SHARED_KERNEL */
112
113 /*
114  * Clear bss memory
115  */
116 static noinline __init void clear_bss_section(void)
117 {
118         memset(__bss_start, 0, __bss_stop - __bss_start);
119 }
120
121 /*
122  * Initialize storage key for kernel pages
123  */
124 static noinline __init void init_kernel_storage_key(void)
125 {
126         unsigned long end_pfn, init_pfn;
127
128         end_pfn = PFN_UP(__pa(&_end));
129
130         for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
131                 page_set_storage_key(init_pfn << PAGE_SHIFT, PAGE_DEFAULT_KEY);
132 }
133
134 static noinline __init void detect_machine_type(void)
135 {
136         struct cpuinfo_S390 *cpuinfo = &S390_lowcore.cpu_data;
137
138         get_cpu_id(&S390_lowcore.cpu_data.cpu_id);
139
140         /* Running under z/VM ? */
141         if (cpuinfo->cpu_id.version == 0xff)
142                 machine_flags |= MACHINE_FLAG_VM;
143
144         /* Running on a P/390 ? */
145         if (cpuinfo->cpu_id.machine == 0x7490)
146                 machine_flags |= MACHINE_FLAG_P390;
147
148         /* Running under KVM ? */
149         if (cpuinfo->cpu_id.version == 0xfe)
150                 machine_flags |= MACHINE_FLAG_KVM;
151 }
152
153 #ifdef CONFIG_64BIT
154 static noinline __init int memory_fast_detect(void)
155 {
156         unsigned long val0 = 0;
157         unsigned long val1 = 0xc;
158         int ret = -ENOSYS;
159
160         if (ipl_flags & IPL_NSS_VALID)
161                 return -ENOSYS;
162
163         asm volatile(
164                 "       diag    %1,%2,0x260\n"
165                 "0:     lhi     %0,0\n"
166                 "1:\n"
167                 EX_TABLE(0b,1b)
168                 : "+d" (ret), "+d" (val0), "+d" (val1) : : "cc");
169
170         if (ret || val0 != val1)
171                 return -ENOSYS;
172
173         memory_chunk[0].size = val0 + 1;
174         return 0;
175 }
176 #else
177 static inline int memory_fast_detect(void)
178 {
179         return -ENOSYS;
180 }
181 #endif
182
183 static inline __init unsigned long __tprot(unsigned long addr)
184 {
185         int cc = -1;
186
187         asm volatile(
188                 "       tprot   0(%1),0\n"
189                 "0:     ipm     %0\n"
190                 "       srl     %0,28\n"
191                 "1:\n"
192                 EX_TABLE(0b,1b)
193                 : "+d" (cc) : "a" (addr) : "cc");
194         return (unsigned long)cc;
195 }
196
197 /* Checking memory in 128KB increments. */
198 #define CHUNK_INCR      (1UL << 17)
199 #define ADDR2G          (1UL << 31)
200
201 static noinline __init void find_memory_chunks(unsigned long memsize)
202 {
203         unsigned long addr = 0, old_addr = 0;
204         unsigned long old_cc = CHUNK_READ_WRITE;
205         unsigned long cc;
206         int chunk = 0;
207
208         while (chunk < MEMORY_CHUNKS) {
209                 cc = __tprot(addr);
210                 while (cc == old_cc) {
211                         addr += CHUNK_INCR;
212                         if (memsize && addr >= memsize)
213                                 break;
214 #ifndef CONFIG_64BIT
215                         if (addr == ADDR2G)
216                                 break;
217 #endif
218                         cc = __tprot(addr);
219                 }
220
221                 if (old_addr != addr &&
222                     (old_cc == CHUNK_READ_WRITE || old_cc == CHUNK_READ_ONLY)) {
223                         memory_chunk[chunk].addr = old_addr;
224                         memory_chunk[chunk].size = addr - old_addr;
225                         memory_chunk[chunk].type = old_cc;
226                         chunk++;
227                 }
228
229                 old_addr = addr;
230                 old_cc = cc;
231
232 #ifndef CONFIG_64BIT
233                 if (addr == ADDR2G)
234                         break;
235 #endif
236                 /*
237                  * Finish memory detection at the first hole
238                  * if storage size is unknown.
239                  */
240                 if (cc == -1UL && !memsize)
241                         break;
242                 if (memsize && addr >= memsize)
243                         break;
244         }
245 }
246
247 static __init void early_pgm_check_handler(void)
248 {
249         unsigned long addr;
250         const struct exception_table_entry *fixup;
251
252         addr = S390_lowcore.program_old_psw.addr;
253         fixup = search_exception_tables(addr & PSW_ADDR_INSN);
254         if (!fixup)
255                 disabled_wait(0);
256         S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE;
257 }
258
259 static noinline __init void setup_lowcore_early(void)
260 {
261         psw_t psw;
262
263         psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
264         psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler;
265         S390_lowcore.external_new_psw = psw;
266         psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler;
267         S390_lowcore.program_new_psw = psw;
268         s390_base_pgm_handler_fn = early_pgm_check_handler;
269 }
270
271 static noinline __init void setup_hpage(void)
272 {
273 #ifndef CONFIG_DEBUG_PAGEALLOC
274         unsigned int facilities;
275
276         facilities = stfl();
277         if (!(facilities & (1UL << 23)) || !(facilities & (1UL << 29)))
278                 return;
279         machine_flags |= MACHINE_FLAG_HPAGE;
280         __ctl_set_bit(0, 23);
281 #endif
282 }
283
284 static __init void detect_mvpg(void)
285 {
286 #ifndef CONFIG_64BIT
287         int rc;
288
289         asm volatile(
290                 "       la      0,0\n"
291                 "       mvpg    %2,%2\n"
292                 "0:     la      %0,0\n"
293                 "1:\n"
294                 EX_TABLE(0b,1b)
295                 : "=d" (rc) : "0" (-EOPNOTSUPP), "a" (0) : "memory", "cc", "0");
296         if (!rc)
297                 machine_flags |= MACHINE_FLAG_MVPG;
298 #endif
299 }
300
301 static __init void detect_ieee(void)
302 {
303 #ifndef CONFIG_64BIT
304         int rc, tmp;
305
306         asm volatile(
307                 "       efpc    %1,0\n"
308                 "0:     la      %0,0\n"
309                 "1:\n"
310                 EX_TABLE(0b,1b)
311                 : "=d" (rc), "=d" (tmp): "0" (-EOPNOTSUPP) : "cc");
312         if (!rc)
313                 machine_flags |= MACHINE_FLAG_IEEE;
314 #endif
315 }
316
317 static __init void detect_csp(void)
318 {
319 #ifndef CONFIG_64BIT
320         int rc;
321
322         asm volatile(
323                 "       la      0,0\n"
324                 "       la      1,0\n"
325                 "       la      2,4\n"
326                 "       csp     0,2\n"
327                 "0:     la      %0,0\n"
328                 "1:\n"
329                 EX_TABLE(0b,1b)
330                 : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc", "0", "1", "2");
331         if (!rc)
332                 machine_flags |= MACHINE_FLAG_CSP;
333 #endif
334 }
335
336 static __init void detect_diag9c(void)
337 {
338         unsigned int cpu_address;
339         int rc;
340
341         cpu_address = stap();
342         asm volatile(
343                 "       diag    %2,0,0x9c\n"
344                 "0:     la      %0,0\n"
345                 "1:\n"
346                 EX_TABLE(0b,1b)
347                 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
348         if (!rc)
349                 machine_flags |= MACHINE_FLAG_DIAG9C;
350 }
351
352 static __init void detect_diag44(void)
353 {
354 #ifdef CONFIG_64BIT
355         int rc;
356
357         asm volatile(
358                 "       diag    0,0,0x44\n"
359                 "0:     la      %0,0\n"
360                 "1:\n"
361                 EX_TABLE(0b,1b)
362                 : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
363         if (!rc)
364                 machine_flags |= MACHINE_FLAG_DIAG44;
365 #endif
366 }
367
368 static __init void detect_machine_facilities(void)
369 {
370 #ifdef CONFIG_64BIT
371         unsigned int facilities;
372
373         facilities = stfl();
374         if (facilities & (1 << 28))
375                 machine_flags |= MACHINE_FLAG_IDTE;
376         if (facilities & (1 << 23))
377                 machine_flags |= MACHINE_FLAG_PFMF;
378         if (facilities & (1 << 4))
379                 machine_flags |= MACHINE_FLAG_MVCOS;
380 #endif
381 }
382
383 /*
384  * Save ipl parameters, clear bss memory, initialize storage keys
385  * and create a kernel NSS at startup if the SAVESYS= parm is defined
386  */
387 void __init startup_init(void)
388 {
389         unsigned long long memsize;
390
391         ipl_save_parameters();
392         clear_bss_section();
393         init_kernel_storage_key();
394         lockdep_init();
395         lockdep_off();
396         detect_machine_type();
397         create_kernel_nss();
398         sort_main_extable();
399         setup_lowcore_early();
400         detect_mvpg();
401         detect_ieee();
402         detect_csp();
403         detect_diag9c();
404         detect_diag44();
405         detect_machine_facilities();
406         setup_hpage();
407         sclp_read_info_early();
408         sclp_facilities_detect();
409         memsize = sclp_memory_detect();
410 #ifndef CONFIG_64BIT
411         /*
412          * Can't deal with more than 2G in 31 bit addressing mode, so
413          * limit the value in order to avoid strange side effects.
414          */
415         if (memsize > ADDR2G)
416                 memsize = ADDR2G;
417 #endif
418         if (memory_fast_detect() < 0)
419                 find_memory_chunks((unsigned long) memsize);
420         lockdep_on();
421 }