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>
17 #include <asm/lowcore.h>
18 #include <asm/processor.h>
19 #include <asm/sections.h>
20 #include <asm/setup.h>
21 #include <asm/cpcmd.h>
25 * Create a Kernel NSS if the SAVESYS= parameter is defined
27 #define DEFSYS_CMD_SIZE 96
28 #define SAVESYS_CMD_SIZE 32
30 char kernel_nss_name
[NSS_NAME_SIZE
+ 1];
32 #ifdef CONFIG_SHARED_KERNEL
33 static noinline __init
void create_kernel_nss(void)
35 unsigned int i
, stext_pfn
, eshared_pfn
, end_pfn
, min_size
;
36 #ifdef CONFIG_BLK_DEV_INITRD
37 unsigned int sinitrd_pfn
, einitrd_pfn
;
41 char upper_command_line
[COMMAND_LINE_SIZE
];
42 char defsys_cmd
[DEFSYS_CMD_SIZE
];
43 char savesys_cmd
[SAVESYS_CMD_SIZE
];
45 /* Do nothing if we are not running under VM */
49 /* Convert COMMAND_LINE to upper case */
50 for (i
= 0; i
< strlen(COMMAND_LINE
); i
++)
51 upper_command_line
[i
] = toupper(COMMAND_LINE
[i
]);
53 savesys_ptr
= strstr(upper_command_line
, "SAVESYS=");
58 savesys_ptr
+= 8; /* Point to the beginning of the NSS name */
59 for (i
= 0; i
< NSS_NAME_SIZE
; i
++) {
60 if (savesys_ptr
[i
] == ' ' || savesys_ptr
[i
] == '\0')
62 kernel_nss_name
[i
] = savesys_ptr
[i
];
65 stext_pfn
= PFN_DOWN(__pa(&_stext
));
66 eshared_pfn
= PFN_DOWN(__pa(&_eshared
));
67 end_pfn
= PFN_UP(__pa(&_end
));
68 min_size
= end_pfn
<< 2;
70 sprintf(defsys_cmd
, "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
71 kernel_nss_name
, stext_pfn
- 1, stext_pfn
, eshared_pfn
- 1,
72 eshared_pfn
, end_pfn
);
74 #ifdef CONFIG_BLK_DEV_INITRD
75 if (INITRD_START
&& INITRD_SIZE
) {
76 sinitrd_pfn
= PFN_DOWN(__pa(INITRD_START
));
77 einitrd_pfn
= PFN_UP(__pa(INITRD_START
+ INITRD_SIZE
));
78 min_size
= einitrd_pfn
<< 2;
79 sprintf(defsys_cmd
, "%s EW %.5X-%.5X", defsys_cmd
,
80 sinitrd_pfn
, einitrd_pfn
);
84 sprintf(defsys_cmd
, "%s EW MINSIZE=%.7iK", defsys_cmd
, min_size
);
85 sprintf(savesys_cmd
, "SAVESYS %s \n IPL %s",
86 kernel_nss_name
, kernel_nss_name
);
88 __cpcmd(defsys_cmd
, NULL
, 0, &response
);
93 __cpcmd(savesys_cmd
, NULL
, 0, &response
);
95 if (response
!= strlen(savesys_cmd
))
98 ipl_flags
= IPL_NSS_VALID
;
101 #else /* CONFIG_SHARED_KERNEL */
103 static inline void create_kernel_nss(void) { }
105 #endif /* CONFIG_SHARED_KERNEL */
110 static noinline __init
void clear_bss_section(void)
112 memset(__bss_start
, 0, _end
- __bss_start
);
116 * Initialize storage key for kernel pages
118 static noinline __init
void init_kernel_storage_key(void)
120 unsigned long end_pfn
, init_pfn
;
122 end_pfn
= PFN_UP(__pa(&_end
));
124 for (init_pfn
= 0 ; init_pfn
< end_pfn
; init_pfn
++)
125 page_set_storage_key(init_pfn
<< PAGE_SHIFT
, PAGE_DEFAULT_KEY
);
128 static noinline __init
void detect_machine_type(void)
130 struct cpuinfo_S390
*cpuinfo
= &S390_lowcore
.cpu_data
;
132 asm volatile("stidp %0" : "=m" (S390_lowcore
.cpu_data
.cpu_id
));
134 /* Running under z/VM ? */
135 if (cpuinfo
->cpu_id
.version
== 0xff)
138 /* Running on a P/390 ? */
139 if (cpuinfo
->cpu_id
.machine
== 0x7490)
143 static noinline __init
int memory_fast_detect(void)
146 unsigned long val0
= 0;
147 unsigned long val1
= 0xc;
150 if (ipl_flags
& IPL_NSS_VALID
)
154 " diag %1,%2,0x260\n"
158 : "+d" (ret
), "+d" (val0
), "+d" (val1
) : : "cc");
160 if (ret
|| val0
!= val1
)
163 memory_chunk
[0].size
= val0
;
167 #define ADDR2G (1UL << 31)
169 static noinline __init
unsigned long sclp_memory_detect(void)
171 struct sclp_readinfo_sccb
*sccb
;
172 unsigned long long memsize
;
174 sccb
= &s390_readinfo_sccb
;
176 if (sccb
->header
.response_code
!= 0x10)
180 memsize
= sccb
->rnsize
<< 20;
182 memsize
= sccb
->rnsize2
<< 20;
184 memsize
*= sccb
->rnmax
;
186 memsize
*= sccb
->rnmax2
;
189 * Can't deal with more than 2G in 31 bit addressing mode, so
190 * limit the value in order to avoid strange side effects.
192 if (memsize
> ADDR2G
)
195 return (unsigned long) memsize
;
198 static inline __init
unsigned long __tprot(unsigned long addr
)
208 : "+d" (cc
) : "a" (addr
) : "cc");
209 return (unsigned long)cc
;
212 /* Checking memory in 128KB increments. */
213 #define CHUNK_INCR (1UL << 17)
215 static noinline __init
void find_memory_chunks(unsigned long memsize
)
217 unsigned long addr
= 0, old_addr
= 0;
218 unsigned long old_cc
= CHUNK_READ_WRITE
;
222 while (chunk
< MEMORY_CHUNKS
) {
224 while (cc
== old_cc
) {
233 if (old_addr
!= addr
&&
234 (old_cc
== CHUNK_READ_WRITE
|| old_cc
== CHUNK_READ_ONLY
)) {
235 memory_chunk
[chunk
].addr
= old_addr
;
236 memory_chunk
[chunk
].size
= addr
- old_addr
;
237 memory_chunk
[chunk
].type
= old_cc
;
249 * Finish memory detection at the first hole, unless
250 * - we reached the hsa -> skip it.
251 * - we know there must be more.
253 if (cc
== -1UL && !memsize
&& old_addr
!= ADDR2G
)
255 if (memsize
&& addr
>= memsize
)
260 static __init
void early_pgm_check_handler(void)
263 const struct exception_table_entry
*fixup
;
265 addr
= S390_lowcore
.program_old_psw
.addr
;
266 fixup
= search_exception_tables(addr
& PSW_ADDR_INSN
);
269 S390_lowcore
.program_old_psw
.addr
= fixup
->fixup
| PSW_ADDR_AMODE
;
272 static noinline __init
void setup_lowcore_early(void)
276 psw
.mask
= PSW_BASE_BITS
| PSW_DEFAULT_KEY
;
277 psw
.addr
= PSW_ADDR_AMODE
| (unsigned long) s390_base_ext_handler
;
278 S390_lowcore
.external_new_psw
= psw
;
279 psw
.addr
= PSW_ADDR_AMODE
| (unsigned long) s390_base_pgm_handler
;
280 S390_lowcore
.program_new_psw
= psw
;
281 s390_base_pgm_handler_fn
= early_pgm_check_handler
;
285 * Save ipl parameters, clear bss memory, initialize storage keys
286 * and create a kernel NSS at startup if the SAVESYS= parm is defined
288 void __init
startup_init(void)
290 unsigned long memsize
;
292 ipl_save_parameters();
294 init_kernel_storage_key();
297 detect_machine_type();
300 setup_lowcore_early();
301 sclp_readinfo_early();
302 memsize
= sclp_memory_detect();
303 if (memory_fast_detect() < 0)
304 find_memory_chunks(memsize
);