xref: /linux/arch/s390/kernel/setup.c (revision c5951e7c8ee5cb04b8b41c32bf567b90117a2124)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  S390 version
4  *    Copyright IBM Corp. 1999, 2012
5  *    Author(s): Hartmut Penner (hp@de.ibm.com),
6  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
7  *
8  *  Derived from "arch/i386/kernel/setup.c"
9  *    Copyright (C) 1995, Linus Torvalds
10  */
11 
12 /*
13  * This file handles the architecture-dependent parts of initialization
14  */
15 
16 #define KMSG_COMPONENT "setup"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 
19 #include <linux/errno.h>
20 #include <linux/export.h>
21 #include <linux/sched.h>
22 #include <linux/sched/task.h>
23 #include <linux/cpu.h>
24 #include <linux/kernel.h>
25 #include <linux/memblock.h>
26 #include <linux/mm.h>
27 #include <linux/stddef.h>
28 #include <linux/unistd.h>
29 #include <linux/ptrace.h>
30 #include <linux/random.h>
31 #include <linux/user.h>
32 #include <linux/tty.h>
33 #include <linux/ioport.h>
34 #include <linux/delay.h>
35 #include <linux/init.h>
36 #include <linux/initrd.h>
37 #include <linux/root_dev.h>
38 #include <linux/console.h>
39 #include <linux/kernel_stat.h>
40 #include <linux/dma-contiguous.h>
41 #include <linux/device.h>
42 #include <linux/notifier.h>
43 #include <linux/pfn.h>
44 #include <linux/ctype.h>
45 #include <linux/reboot.h>
46 #include <linux/topology.h>
47 #include <linux/kexec.h>
48 #include <linux/crash_dump.h>
49 #include <linux/memory.h>
50 #include <linux/compat.h>
51 #include <linux/start_kernel.h>
52 
53 #include <asm/boot_data.h>
54 #include <asm/ipl.h>
55 #include <asm/facility.h>
56 #include <asm/smp.h>
57 #include <asm/mmu_context.h>
58 #include <asm/cpcmd.h>
59 #include <asm/lowcore.h>
60 #include <asm/nmi.h>
61 #include <asm/irq.h>
62 #include <asm/page.h>
63 #include <asm/ptrace.h>
64 #include <asm/sections.h>
65 #include <asm/ebcdic.h>
66 #include <asm/diag.h>
67 #include <asm/os_info.h>
68 #include <asm/sclp.h>
69 #include <asm/stacktrace.h>
70 #include <asm/sysinfo.h>
71 #include <asm/numa.h>
72 #include <asm/alternative.h>
73 #include <asm/nospec-branch.h>
74 #include <asm/mem_detect.h>
75 #include <asm/uv.h>
76 #include "entry.h"
77 
78 /*
79  * Machine setup..
80  */
81 unsigned int console_mode = 0;
82 EXPORT_SYMBOL(console_mode);
83 
84 unsigned int console_devno = -1;
85 EXPORT_SYMBOL(console_devno);
86 
87 unsigned int console_irq = -1;
88 EXPORT_SYMBOL(console_irq);
89 
90 unsigned long elf_hwcap __read_mostly = 0;
91 char elf_platform[ELF_PLATFORM_SIZE];
92 
93 unsigned long int_hwcap = 0;
94 
95 #ifdef CONFIG_PROTECTED_VIRTUALIZATION_GUEST
96 int __bootdata_preserved(prot_virt_guest);
97 #endif
98 
99 int __bootdata(noexec_disabled);
100 int __bootdata(memory_end_set);
101 unsigned long __bootdata(memory_end);
102 unsigned long __bootdata(vmalloc_size);
103 unsigned long __bootdata(max_physmem_end);
104 struct mem_detect_info __bootdata(mem_detect);
105 
106 struct exception_table_entry *__bootdata_preserved(__start_dma_ex_table);
107 struct exception_table_entry *__bootdata_preserved(__stop_dma_ex_table);
108 unsigned long __bootdata_preserved(__swsusp_reset_dma);
109 unsigned long __bootdata_preserved(__stext_dma);
110 unsigned long __bootdata_preserved(__etext_dma);
111 unsigned long __bootdata_preserved(__sdma);
112 unsigned long __bootdata_preserved(__edma);
113 unsigned long __bootdata_preserved(__kaslr_offset);
114 
115 unsigned long VMALLOC_START;
116 EXPORT_SYMBOL(VMALLOC_START);
117 
118 unsigned long VMALLOC_END;
119 EXPORT_SYMBOL(VMALLOC_END);
120 
121 struct page *vmemmap;
122 EXPORT_SYMBOL(vmemmap);
123 
124 unsigned long MODULES_VADDR;
125 unsigned long MODULES_END;
126 
127 /* An array with a pointer to the lowcore of every CPU. */
128 struct lowcore *lowcore_ptr[NR_CPUS];
129 EXPORT_SYMBOL(lowcore_ptr);
130 
131 /*
132  * This is set up by the setup-routine at boot-time
133  * for S390 need to find out, what we have to setup
134  * using address 0x10400 ...
135  */
136 
137 #include <asm/setup.h>
138 
139 /*
140  * condev= and conmode= setup parameter.
141  */
142 
143 static int __init condev_setup(char *str)
144 {
145 	int vdev;
146 
147 	vdev = simple_strtoul(str, &str, 0);
148 	if (vdev >= 0 && vdev < 65536) {
149 		console_devno = vdev;
150 		console_irq = -1;
151 	}
152 	return 1;
153 }
154 
155 __setup("condev=", condev_setup);
156 
157 static void __init set_preferred_console(void)
158 {
159 	if (CONSOLE_IS_3215 || CONSOLE_IS_SCLP)
160 		add_preferred_console("ttyS", 0, NULL);
161 	else if (CONSOLE_IS_3270)
162 		add_preferred_console("tty3270", 0, NULL);
163 	else if (CONSOLE_IS_VT220)
164 		add_preferred_console("ttyS", 1, NULL);
165 	else if (CONSOLE_IS_HVC)
166 		add_preferred_console("hvc", 0, NULL);
167 }
168 
169 static int __init conmode_setup(char *str)
170 {
171 #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
172 	if (!strcmp(str, "hwc") || !strcmp(str, "sclp"))
173                 SET_CONSOLE_SCLP;
174 #endif
175 #if defined(CONFIG_TN3215_CONSOLE)
176 	if (!strcmp(str, "3215"))
177 		SET_CONSOLE_3215;
178 #endif
179 #if defined(CONFIG_TN3270_CONSOLE)
180 	if (!strcmp(str, "3270"))
181 		SET_CONSOLE_3270;
182 #endif
183 	set_preferred_console();
184         return 1;
185 }
186 
187 __setup("conmode=", conmode_setup);
188 
189 static void __init conmode_default(void)
190 {
191 	char query_buffer[1024];
192 	char *ptr;
193 
194         if (MACHINE_IS_VM) {
195 		cpcmd("QUERY CONSOLE", query_buffer, 1024, NULL);
196 		console_devno = simple_strtoul(query_buffer + 5, NULL, 16);
197 		ptr = strstr(query_buffer, "SUBCHANNEL =");
198 		console_irq = simple_strtoul(ptr + 13, NULL, 16);
199 		cpcmd("QUERY TERM", query_buffer, 1024, NULL);
200 		ptr = strstr(query_buffer, "CONMODE");
201 		/*
202 		 * Set the conmode to 3215 so that the device recognition
203 		 * will set the cu_type of the console to 3215. If the
204 		 * conmode is 3270 and we don't set it back then both
205 		 * 3215 and the 3270 driver will try to access the console
206 		 * device (3215 as console and 3270 as normal tty).
207 		 */
208 		cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
209 		if (ptr == NULL) {
210 #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
211 			SET_CONSOLE_SCLP;
212 #endif
213 			return;
214 		}
215 		if (str_has_prefix(ptr + 8, "3270")) {
216 #if defined(CONFIG_TN3270_CONSOLE)
217 			SET_CONSOLE_3270;
218 #elif defined(CONFIG_TN3215_CONSOLE)
219 			SET_CONSOLE_3215;
220 #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
221 			SET_CONSOLE_SCLP;
222 #endif
223 		} else if (str_has_prefix(ptr + 8, "3215")) {
224 #if defined(CONFIG_TN3215_CONSOLE)
225 			SET_CONSOLE_3215;
226 #elif defined(CONFIG_TN3270_CONSOLE)
227 			SET_CONSOLE_3270;
228 #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
229 			SET_CONSOLE_SCLP;
230 #endif
231 		}
232 	} else if (MACHINE_IS_KVM) {
233 		if (sclp.has_vt220 && IS_ENABLED(CONFIG_SCLP_VT220_CONSOLE))
234 			SET_CONSOLE_VT220;
235 		else if (sclp.has_linemode && IS_ENABLED(CONFIG_SCLP_CONSOLE))
236 			SET_CONSOLE_SCLP;
237 		else
238 			SET_CONSOLE_HVC;
239 	} else {
240 #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
241 		SET_CONSOLE_SCLP;
242 #endif
243 	}
244 }
245 
246 #ifdef CONFIG_CRASH_DUMP
247 static void __init setup_zfcpdump(void)
248 {
249 	if (ipl_info.type != IPL_TYPE_FCP_DUMP)
250 		return;
251 	if (OLDMEM_BASE)
252 		return;
253 	strcat(boot_command_line, " cio_ignore=all,!ipldev,!condev");
254 	console_loglevel = 2;
255 }
256 #else
257 static inline void setup_zfcpdump(void) {}
258 #endif /* CONFIG_CRASH_DUMP */
259 
260  /*
261  * Reboot, halt and power_off stubs. They just call _machine_restart,
262  * _machine_halt or _machine_power_off.
263  */
264 
265 void machine_restart(char *command)
266 {
267 	if ((!in_interrupt() && !in_atomic()) || oops_in_progress)
268 		/*
269 		 * Only unblank the console if we are called in enabled
270 		 * context or a bust_spinlocks cleared the way for us.
271 		 */
272 		console_unblank();
273 	_machine_restart(command);
274 }
275 
276 void machine_halt(void)
277 {
278 	if (!in_interrupt() || oops_in_progress)
279 		/*
280 		 * Only unblank the console if we are called in enabled
281 		 * context or a bust_spinlocks cleared the way for us.
282 		 */
283 		console_unblank();
284 	_machine_halt();
285 }
286 
287 void machine_power_off(void)
288 {
289 	if (!in_interrupt() || oops_in_progress)
290 		/*
291 		 * Only unblank the console if we are called in enabled
292 		 * context or a bust_spinlocks cleared the way for us.
293 		 */
294 		console_unblank();
295 	_machine_power_off();
296 }
297 
298 /*
299  * Dummy power off function.
300  */
301 void (*pm_power_off)(void) = machine_power_off;
302 EXPORT_SYMBOL_GPL(pm_power_off);
303 
304 void *restart_stack __section(.data);
305 
306 unsigned long stack_alloc(void)
307 {
308 #ifdef CONFIG_VMAP_STACK
309 	return (unsigned long)
310 		__vmalloc_node_range(THREAD_SIZE, THREAD_SIZE,
311 				     VMALLOC_START, VMALLOC_END,
312 				     THREADINFO_GFP,
313 				     PAGE_KERNEL, 0, NUMA_NO_NODE,
314 				     __builtin_return_address(0));
315 #else
316 	return __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
317 #endif
318 }
319 
320 void stack_free(unsigned long stack)
321 {
322 #ifdef CONFIG_VMAP_STACK
323 	vfree((void *) stack);
324 #else
325 	free_pages(stack, THREAD_SIZE_ORDER);
326 #endif
327 }
328 
329 int __init arch_early_irq_init(void)
330 {
331 	unsigned long stack;
332 
333 	stack = __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
334 	if (!stack)
335 		panic("Couldn't allocate async stack");
336 	S390_lowcore.async_stack = stack + STACK_INIT_OFFSET;
337 	return 0;
338 }
339 
340 static int __init async_stack_realloc(void)
341 {
342 	unsigned long old, new;
343 
344 	old = S390_lowcore.async_stack - STACK_INIT_OFFSET;
345 	new = stack_alloc();
346 	if (!new)
347 		panic("Couldn't allocate async stack");
348 	S390_lowcore.async_stack = new + STACK_INIT_OFFSET;
349 	free_pages(old, THREAD_SIZE_ORDER);
350 	return 0;
351 }
352 early_initcall(async_stack_realloc);
353 
354 void __init arch_call_rest_init(void)
355 {
356 	unsigned long stack;
357 
358 	stack = stack_alloc();
359 	if (!stack)
360 		panic("Couldn't allocate kernel stack");
361 	current->stack = (void *) stack;
362 #ifdef CONFIG_VMAP_STACK
363 	current->stack_vm_area = (void *) stack;
364 #endif
365 	set_task_stack_end_magic(current);
366 	stack += STACK_INIT_OFFSET;
367 	S390_lowcore.kernel_stack = stack;
368 	CALL_ON_STACK_NORETURN(rest_init, stack);
369 }
370 
371 static void __init setup_lowcore_dat_off(void)
372 {
373 	struct lowcore *lc;
374 
375 	/*
376 	 * Setup lowcore for boot cpu
377 	 */
378 	BUILD_BUG_ON(sizeof(struct lowcore) != LC_PAGES * PAGE_SIZE);
379 	lc = memblock_alloc_low(sizeof(*lc), sizeof(*lc));
380 	if (!lc)
381 		panic("%s: Failed to allocate %zu bytes align=%zx\n",
382 		      __func__, sizeof(*lc), sizeof(*lc));
383 
384 	lc->restart_psw.mask = PSW_KERNEL_BITS;
385 	lc->restart_psw.addr = (unsigned long) restart_int_handler;
386 	lc->external_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK;
387 	lc->external_new_psw.addr = (unsigned long) ext_int_handler;
388 	lc->svc_new_psw.mask = PSW_KERNEL_BITS |
389 		PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
390 	lc->svc_new_psw.addr = (unsigned long) system_call;
391 	lc->program_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK;
392 	lc->program_new_psw.addr = (unsigned long) pgm_check_handler;
393 	lc->mcck_new_psw.mask = PSW_KERNEL_BITS;
394 	lc->mcck_new_psw.addr = (unsigned long) mcck_int_handler;
395 	lc->io_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK;
396 	lc->io_new_psw.addr = (unsigned long) io_int_handler;
397 	lc->clock_comparator = clock_comparator_max;
398 	lc->nodat_stack = ((unsigned long) &init_thread_union)
399 		+ THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
400 	lc->current_task = (unsigned long)&init_task;
401 	lc->lpp = LPP_MAGIC;
402 	lc->machine_flags = S390_lowcore.machine_flags;
403 	lc->preempt_count = S390_lowcore.preempt_count;
404 	lc->stfl_fac_list = S390_lowcore.stfl_fac_list;
405 	memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
406 	       sizeof(lc->stfle_fac_list));
407 	memcpy(lc->alt_stfle_fac_list, S390_lowcore.alt_stfle_fac_list,
408 	       sizeof(lc->alt_stfle_fac_list));
409 	nmi_alloc_boot_cpu(lc);
410 	vdso_alloc_boot_cpu(lc);
411 	lc->sync_enter_timer = S390_lowcore.sync_enter_timer;
412 	lc->async_enter_timer = S390_lowcore.async_enter_timer;
413 	lc->exit_timer = S390_lowcore.exit_timer;
414 	lc->user_timer = S390_lowcore.user_timer;
415 	lc->system_timer = S390_lowcore.system_timer;
416 	lc->steal_timer = S390_lowcore.steal_timer;
417 	lc->last_update_timer = S390_lowcore.last_update_timer;
418 	lc->last_update_clock = S390_lowcore.last_update_clock;
419 
420 	/*
421 	 * Allocate the global restart stack which is the same for
422 	 * all CPUs in cast *one* of them does a PSW restart.
423 	 */
424 	restart_stack = memblock_alloc(THREAD_SIZE, THREAD_SIZE);
425 	if (!restart_stack)
426 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
427 		      __func__, THREAD_SIZE, THREAD_SIZE);
428 	restart_stack += STACK_INIT_OFFSET;
429 
430 	/*
431 	 * Set up PSW restart to call ipl.c:do_restart(). Copy the relevant
432 	 * restart data to the absolute zero lowcore. This is necessary if
433 	 * PSW restart is done on an offline CPU that has lowcore zero.
434 	 */
435 	lc->restart_stack = (unsigned long) restart_stack;
436 	lc->restart_fn = (unsigned long) do_restart;
437 	lc->restart_data = 0;
438 	lc->restart_source = -1UL;
439 
440 	/* Setup absolute zero lowcore */
441 	mem_assign_absolute(S390_lowcore.restart_stack, lc->restart_stack);
442 	mem_assign_absolute(S390_lowcore.restart_fn, lc->restart_fn);
443 	mem_assign_absolute(S390_lowcore.restart_data, lc->restart_data);
444 	mem_assign_absolute(S390_lowcore.restart_source, lc->restart_source);
445 	mem_assign_absolute(S390_lowcore.restart_psw, lc->restart_psw);
446 
447 	lc->spinlock_lockval = arch_spin_lockval(0);
448 	lc->spinlock_index = 0;
449 	arch_spin_lock_setup(0);
450 	lc->br_r1_trampoline = 0x07f1;	/* br %r1 */
451 
452 	set_prefix((u32)(unsigned long) lc);
453 	lowcore_ptr[0] = lc;
454 }
455 
456 static void __init setup_lowcore_dat_on(void)
457 {
458 	__ctl_clear_bit(0, 28);
459 	S390_lowcore.external_new_psw.mask |= PSW_MASK_DAT;
460 	S390_lowcore.svc_new_psw.mask |= PSW_MASK_DAT;
461 	S390_lowcore.program_new_psw.mask |= PSW_MASK_DAT;
462 	S390_lowcore.io_new_psw.mask |= PSW_MASK_DAT;
463 	__ctl_set_bit(0, 28);
464 }
465 
466 static struct resource code_resource = {
467 	.name  = "Kernel code",
468 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
469 };
470 
471 static struct resource data_resource = {
472 	.name = "Kernel data",
473 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
474 };
475 
476 static struct resource bss_resource = {
477 	.name = "Kernel bss",
478 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
479 };
480 
481 static struct resource __initdata *standard_resources[] = {
482 	&code_resource,
483 	&data_resource,
484 	&bss_resource,
485 };
486 
487 static void __init setup_resources(void)
488 {
489 	struct resource *res, *std_res, *sub_res;
490 	struct memblock_region *reg;
491 	int j;
492 
493 	code_resource.start = (unsigned long) _text;
494 	code_resource.end = (unsigned long) _etext - 1;
495 	data_resource.start = (unsigned long) _etext;
496 	data_resource.end = (unsigned long) _edata - 1;
497 	bss_resource.start = (unsigned long) __bss_start;
498 	bss_resource.end = (unsigned long) __bss_stop - 1;
499 
500 	for_each_memblock(memory, reg) {
501 		res = memblock_alloc(sizeof(*res), 8);
502 		if (!res)
503 			panic("%s: Failed to allocate %zu bytes align=0x%x\n",
504 			      __func__, sizeof(*res), 8);
505 		res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
506 
507 		res->name = "System RAM";
508 		res->start = reg->base;
509 		res->end = reg->base + reg->size - 1;
510 		request_resource(&iomem_resource, res);
511 
512 		for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
513 			std_res = standard_resources[j];
514 			if (std_res->start < res->start ||
515 			    std_res->start > res->end)
516 				continue;
517 			if (std_res->end > res->end) {
518 				sub_res = memblock_alloc(sizeof(*sub_res), 8);
519 				if (!sub_res)
520 					panic("%s: Failed to allocate %zu bytes align=0x%x\n",
521 					      __func__, sizeof(*sub_res), 8);
522 				*sub_res = *std_res;
523 				sub_res->end = res->end;
524 				std_res->start = res->end + 1;
525 				request_resource(res, sub_res);
526 			} else {
527 				request_resource(res, std_res);
528 			}
529 		}
530 	}
531 #ifdef CONFIG_CRASH_DUMP
532 	/*
533 	 * Re-add removed crash kernel memory as reserved memory. This makes
534 	 * sure it will be mapped with the identity mapping and struct pages
535 	 * will be created, so it can be resized later on.
536 	 * However add it later since the crash kernel resource should not be
537 	 * part of the System RAM resource.
538 	 */
539 	if (crashk_res.end) {
540 		memblock_add_node(crashk_res.start, resource_size(&crashk_res), 0);
541 		memblock_reserve(crashk_res.start, resource_size(&crashk_res));
542 		insert_resource(&iomem_resource, &crashk_res);
543 	}
544 #endif
545 }
546 
547 static void __init setup_memory_end(void)
548 {
549 	unsigned long vmax, tmp;
550 
551 	/* Choose kernel address space layout: 3 or 4 levels. */
552 	if (IS_ENABLED(CONFIG_KASAN)) {
553 		vmax = IS_ENABLED(CONFIG_KASAN_S390_4_LEVEL_PAGING)
554 			   ? _REGION1_SIZE
555 			   : _REGION2_SIZE;
556 	} else {
557 		tmp = (memory_end ?: max_physmem_end) / PAGE_SIZE;
558 		tmp = tmp * (sizeof(struct page) + PAGE_SIZE);
559 		if (tmp + vmalloc_size + MODULES_LEN <= _REGION2_SIZE)
560 			vmax = _REGION2_SIZE; /* 3-level kernel page table */
561 		else
562 			vmax = _REGION1_SIZE; /* 4-level kernel page table */
563 	}
564 
565 	/* module area is at the end of the kernel address space. */
566 	MODULES_END = vmax;
567 	MODULES_VADDR = MODULES_END - MODULES_LEN;
568 	VMALLOC_END = MODULES_VADDR;
569 	VMALLOC_START = VMALLOC_END - vmalloc_size;
570 
571 	/* Split remaining virtual space between 1:1 mapping & vmemmap array */
572 	tmp = VMALLOC_START / (PAGE_SIZE + sizeof(struct page));
573 	/* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
574 	tmp = SECTION_ALIGN_UP(tmp);
575 	tmp = VMALLOC_START - tmp * sizeof(struct page);
576 	tmp &= ~((vmax >> 11) - 1);	/* align to page table level */
577 	tmp = min(tmp, 1UL << MAX_PHYSMEM_BITS);
578 	vmemmap = (struct page *) tmp;
579 
580 	/* Take care that memory_end is set and <= vmemmap */
581 	memory_end = min(memory_end ?: max_physmem_end, (unsigned long)vmemmap);
582 #ifdef CONFIG_KASAN
583 	/* fit in kasan shadow memory region between 1:1 and vmemmap */
584 	memory_end = min(memory_end, KASAN_SHADOW_START);
585 	vmemmap = max(vmemmap, (struct page *)KASAN_SHADOW_END);
586 #endif
587 	max_pfn = max_low_pfn = PFN_DOWN(memory_end);
588 	memblock_remove(memory_end, ULONG_MAX);
589 
590 	pr_notice("The maximum memory size is %luMB\n", memory_end >> 20);
591 }
592 
593 #ifdef CONFIG_CRASH_DUMP
594 
595 /*
596  * When kdump is enabled, we have to ensure that no memory from
597  * the area [0 - crashkernel memory size] and
598  * [crashk_res.start - crashk_res.end] is set offline.
599  */
600 static int kdump_mem_notifier(struct notifier_block *nb,
601 			      unsigned long action, void *data)
602 {
603 	struct memory_notify *arg = data;
604 
605 	if (action != MEM_GOING_OFFLINE)
606 		return NOTIFY_OK;
607 	if (arg->start_pfn < PFN_DOWN(resource_size(&crashk_res)))
608 		return NOTIFY_BAD;
609 	if (arg->start_pfn > PFN_DOWN(crashk_res.end))
610 		return NOTIFY_OK;
611 	if (arg->start_pfn + arg->nr_pages - 1 < PFN_DOWN(crashk_res.start))
612 		return NOTIFY_OK;
613 	return NOTIFY_BAD;
614 }
615 
616 static struct notifier_block kdump_mem_nb = {
617 	.notifier_call = kdump_mem_notifier,
618 };
619 
620 #endif
621 
622 /*
623  * Make sure that the area behind memory_end is protected
624  */
625 static void reserve_memory_end(void)
626 {
627 	if (memory_end_set)
628 		memblock_reserve(memory_end, ULONG_MAX);
629 }
630 
631 /*
632  * Make sure that oldmem, where the dump is stored, is protected
633  */
634 static void reserve_oldmem(void)
635 {
636 #ifdef CONFIG_CRASH_DUMP
637 	if (OLDMEM_BASE)
638 		/* Forget all memory above the running kdump system */
639 		memblock_reserve(OLDMEM_SIZE, (phys_addr_t)ULONG_MAX);
640 #endif
641 }
642 
643 /*
644  * Make sure that oldmem, where the dump is stored, is protected
645  */
646 static void remove_oldmem(void)
647 {
648 #ifdef CONFIG_CRASH_DUMP
649 	if (OLDMEM_BASE)
650 		/* Forget all memory above the running kdump system */
651 		memblock_remove(OLDMEM_SIZE, (phys_addr_t)ULONG_MAX);
652 #endif
653 }
654 
655 /*
656  * Reserve memory for kdump kernel to be loaded with kexec
657  */
658 static void __init reserve_crashkernel(void)
659 {
660 #ifdef CONFIG_CRASH_DUMP
661 	unsigned long long crash_base, crash_size;
662 	phys_addr_t low, high;
663 	int rc;
664 
665 	rc = parse_crashkernel(boot_command_line, memory_end, &crash_size,
666 			       &crash_base);
667 
668 	crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
669 	crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
670 	if (rc || crash_size == 0)
671 		return;
672 
673 	if (memblock.memory.regions[0].size < crash_size) {
674 		pr_info("crashkernel reservation failed: %s\n",
675 			"first memory chunk must be at least crashkernel size");
676 		return;
677 	}
678 
679 	low = crash_base ?: OLDMEM_BASE;
680 	high = low + crash_size;
681 	if (low >= OLDMEM_BASE && high <= OLDMEM_BASE + OLDMEM_SIZE) {
682 		/* The crashkernel fits into OLDMEM, reuse OLDMEM */
683 		crash_base = low;
684 	} else {
685 		/* Find suitable area in free memory */
686 		low = max_t(unsigned long, crash_size, sclp.hsa_size);
687 		high = crash_base ? crash_base + crash_size : ULONG_MAX;
688 
689 		if (crash_base && crash_base < low) {
690 			pr_info("crashkernel reservation failed: %s\n",
691 				"crash_base too low");
692 			return;
693 		}
694 		low = crash_base ?: low;
695 		crash_base = memblock_find_in_range(low, high, crash_size,
696 						    KEXEC_CRASH_MEM_ALIGN);
697 	}
698 
699 	if (!crash_base) {
700 		pr_info("crashkernel reservation failed: %s\n",
701 			"no suitable area found");
702 		return;
703 	}
704 
705 	if (register_memory_notifier(&kdump_mem_nb))
706 		return;
707 
708 	if (!OLDMEM_BASE && MACHINE_IS_VM)
709 		diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));
710 	crashk_res.start = crash_base;
711 	crashk_res.end = crash_base + crash_size - 1;
712 	memblock_remove(crash_base, crash_size);
713 	pr_info("Reserving %lluMB of memory at %lluMB "
714 		"for crashkernel (System RAM: %luMB)\n",
715 		crash_size >> 20, crash_base >> 20,
716 		(unsigned long)memblock.memory.total_size >> 20);
717 	os_info_crashkernel_add(crash_base, crash_size);
718 #endif
719 }
720 
721 /*
722  * Reserve the initrd from being used by memblock
723  */
724 static void __init reserve_initrd(void)
725 {
726 #ifdef CONFIG_BLK_DEV_INITRD
727 	if (!INITRD_START || !INITRD_SIZE)
728 		return;
729 	initrd_start = INITRD_START;
730 	initrd_end = initrd_start + INITRD_SIZE;
731 	memblock_reserve(INITRD_START, INITRD_SIZE);
732 #endif
733 }
734 
735 /*
736  * Reserve the memory area used to pass the certificate lists
737  */
738 static void __init reserve_certificate_list(void)
739 {
740 	if (ipl_cert_list_addr)
741 		memblock_reserve(ipl_cert_list_addr, ipl_cert_list_size);
742 }
743 
744 static void __init reserve_mem_detect_info(void)
745 {
746 	unsigned long start, size;
747 
748 	get_mem_detect_reserved(&start, &size);
749 	if (size)
750 		memblock_reserve(start, size);
751 }
752 
753 static void __init free_mem_detect_info(void)
754 {
755 	unsigned long start, size;
756 
757 	get_mem_detect_reserved(&start, &size);
758 	if (size)
759 		memblock_free(start, size);
760 }
761 
762 static void __init memblock_physmem_add(phys_addr_t start, phys_addr_t size)
763 {
764 	memblock_dbg("memblock_physmem_add: [%#016llx-%#016llx]\n",
765 		     start, start + size - 1);
766 	memblock_add_range(&memblock.memory, start, size, 0, 0);
767 	memblock_add_range(&memblock.physmem, start, size, 0, 0);
768 }
769 
770 static const char * __init get_mem_info_source(void)
771 {
772 	switch (mem_detect.info_source) {
773 	case MEM_DETECT_SCLP_STOR_INFO:
774 		return "sclp storage info";
775 	case MEM_DETECT_DIAG260:
776 		return "diag260";
777 	case MEM_DETECT_SCLP_READ_INFO:
778 		return "sclp read info";
779 	case MEM_DETECT_BIN_SEARCH:
780 		return "binary search";
781 	}
782 	return "none";
783 }
784 
785 static void __init memblock_add_mem_detect_info(void)
786 {
787 	unsigned long start, end;
788 	int i;
789 
790 	memblock_dbg("physmem info source: %s (%hhd)\n",
791 		     get_mem_info_source(), mem_detect.info_source);
792 	/* keep memblock lists close to the kernel */
793 	memblock_set_bottom_up(true);
794 	for_each_mem_detect_block(i, &start, &end)
795 		memblock_physmem_add(start, end - start);
796 	memblock_set_bottom_up(false);
797 	memblock_dump_all();
798 }
799 
800 /*
801  * Check for initrd being in usable memory
802  */
803 static void __init check_initrd(void)
804 {
805 #ifdef CONFIG_BLK_DEV_INITRD
806 	if (INITRD_START && INITRD_SIZE &&
807 	    !memblock_is_region_memory(INITRD_START, INITRD_SIZE)) {
808 		pr_err("The initial RAM disk does not fit into the memory\n");
809 		memblock_free(INITRD_START, INITRD_SIZE);
810 		initrd_start = initrd_end = 0;
811 	}
812 #endif
813 }
814 
815 /*
816  * Reserve memory used for lowcore/command line/kernel image.
817  */
818 static void __init reserve_kernel(void)
819 {
820 	unsigned long start_pfn = PFN_UP(__pa(_end));
821 
822 	memblock_reserve(0, HEAD_END);
823 	memblock_reserve((unsigned long)_stext, PFN_PHYS(start_pfn)
824 			 - (unsigned long)_stext);
825 	memblock_reserve(__sdma, __edma - __sdma);
826 }
827 
828 static void __init setup_memory(void)
829 {
830 	struct memblock_region *reg;
831 
832 	/*
833 	 * Init storage key for present memory
834 	 */
835 	for_each_memblock(memory, reg) {
836 		storage_key_init_range(reg->base, reg->base + reg->size);
837 	}
838 	psw_set_key(PAGE_DEFAULT_KEY);
839 
840 	/* Only cosmetics */
841 	memblock_enforce_memory_limit(memblock_end_of_DRAM());
842 }
843 
844 /*
845  * Setup hardware capabilities.
846  */
847 static int __init setup_hwcaps(void)
848 {
849 	static const int stfl_bits[6] = { 0, 2, 7, 17, 19, 21 };
850 	struct cpuid cpu_id;
851 	int i;
852 
853 	/*
854 	 * The store facility list bits numbers as found in the principles
855 	 * of operation are numbered with bit 1UL<<31 as number 0 to
856 	 * bit 1UL<<0 as number 31.
857 	 *   Bit 0: instructions named N3, "backported" to esa-mode
858 	 *   Bit 2: z/Architecture mode is active
859 	 *   Bit 7: the store-facility-list-extended facility is installed
860 	 *   Bit 17: the message-security assist is installed
861 	 *   Bit 19: the long-displacement facility is installed
862 	 *   Bit 21: the extended-immediate facility is installed
863 	 *   Bit 22: extended-translation facility 3 is installed
864 	 *   Bit 30: extended-translation facility 3 enhancement facility
865 	 * These get translated to:
866 	 *   HWCAP_S390_ESAN3 bit 0, HWCAP_S390_ZARCH bit 1,
867 	 *   HWCAP_S390_STFLE bit 2, HWCAP_S390_MSA bit 3,
868 	 *   HWCAP_S390_LDISP bit 4, HWCAP_S390_EIMM bit 5 and
869 	 *   HWCAP_S390_ETF3EH bit 8 (22 && 30).
870 	 */
871 	for (i = 0; i < 6; i++)
872 		if (test_facility(stfl_bits[i]))
873 			elf_hwcap |= 1UL << i;
874 
875 	if (test_facility(22) && test_facility(30))
876 		elf_hwcap |= HWCAP_S390_ETF3EH;
877 
878 	/*
879 	 * Check for additional facilities with store-facility-list-extended.
880 	 * stfle stores doublewords (8 byte) with bit 1ULL<<63 as bit 0
881 	 * and 1ULL<<0 as bit 63. Bits 0-31 contain the same information
882 	 * as stored by stfl, bits 32-xxx contain additional facilities.
883 	 * How many facility words are stored depends on the number of
884 	 * doublewords passed to the instruction. The additional facilities
885 	 * are:
886 	 *   Bit 42: decimal floating point facility is installed
887 	 *   Bit 44: perform floating point operation facility is installed
888 	 * translated to:
889 	 *   HWCAP_S390_DFP bit 6 (42 && 44).
890 	 */
891 	if ((elf_hwcap & (1UL << 2)) && test_facility(42) && test_facility(44))
892 		elf_hwcap |= HWCAP_S390_DFP;
893 
894 	/*
895 	 * Huge page support HWCAP_S390_HPAGE is bit 7.
896 	 */
897 	if (MACHINE_HAS_EDAT1)
898 		elf_hwcap |= HWCAP_S390_HPAGE;
899 
900 	/*
901 	 * 64-bit register support for 31-bit processes
902 	 * HWCAP_S390_HIGH_GPRS is bit 9.
903 	 */
904 	elf_hwcap |= HWCAP_S390_HIGH_GPRS;
905 
906 	/*
907 	 * Transactional execution support HWCAP_S390_TE is bit 10.
908 	 */
909 	if (MACHINE_HAS_TE)
910 		elf_hwcap |= HWCAP_S390_TE;
911 
912 	/*
913 	 * Vector extension HWCAP_S390_VXRS is bit 11. The Vector extension
914 	 * can be disabled with the "novx" parameter. Use MACHINE_HAS_VX
915 	 * instead of facility bit 129.
916 	 */
917 	if (MACHINE_HAS_VX) {
918 		elf_hwcap |= HWCAP_S390_VXRS;
919 		if (test_facility(134))
920 			elf_hwcap |= HWCAP_S390_VXRS_EXT;
921 		if (test_facility(135))
922 			elf_hwcap |= HWCAP_S390_VXRS_BCD;
923 		if (test_facility(148))
924 			elf_hwcap |= HWCAP_S390_VXRS_EXT2;
925 		if (test_facility(152))
926 			elf_hwcap |= HWCAP_S390_VXRS_PDE;
927 	}
928 	if (test_facility(150))
929 		elf_hwcap |= HWCAP_S390_SORT;
930 	if (test_facility(151))
931 		elf_hwcap |= HWCAP_S390_DFLT;
932 
933 	/*
934 	 * Guarded storage support HWCAP_S390_GS is bit 12.
935 	 */
936 	if (MACHINE_HAS_GS)
937 		elf_hwcap |= HWCAP_S390_GS;
938 
939 	get_cpu_id(&cpu_id);
940 	add_device_randomness(&cpu_id, sizeof(cpu_id));
941 	switch (cpu_id.machine) {
942 	case 0x2064:
943 	case 0x2066:
944 	default:	/* Use "z900" as default for 64 bit kernels. */
945 		strcpy(elf_platform, "z900");
946 		break;
947 	case 0x2084:
948 	case 0x2086:
949 		strcpy(elf_platform, "z990");
950 		break;
951 	case 0x2094:
952 	case 0x2096:
953 		strcpy(elf_platform, "z9-109");
954 		break;
955 	case 0x2097:
956 	case 0x2098:
957 		strcpy(elf_platform, "z10");
958 		break;
959 	case 0x2817:
960 	case 0x2818:
961 		strcpy(elf_platform, "z196");
962 		break;
963 	case 0x2827:
964 	case 0x2828:
965 		strcpy(elf_platform, "zEC12");
966 		break;
967 	case 0x2964:
968 	case 0x2965:
969 		strcpy(elf_platform, "z13");
970 		break;
971 	case 0x3906:
972 	case 0x3907:
973 		strcpy(elf_platform, "z14");
974 		break;
975 	case 0x8561:
976 	case 0x8562:
977 		strcpy(elf_platform, "z15");
978 		break;
979 	}
980 
981 	/*
982 	 * Virtualization support HWCAP_INT_SIE is bit 0.
983 	 */
984 	if (sclp.has_sief2)
985 		int_hwcap |= HWCAP_INT_SIE;
986 
987 	return 0;
988 }
989 arch_initcall(setup_hwcaps);
990 
991 /*
992  * Add system information as device randomness
993  */
994 static void __init setup_randomness(void)
995 {
996 	struct sysinfo_3_2_2 *vmms;
997 
998 	vmms = (struct sysinfo_3_2_2 *) memblock_phys_alloc(PAGE_SIZE,
999 							    PAGE_SIZE);
1000 	if (!vmms)
1001 		panic("Failed to allocate memory for sysinfo structure\n");
1002 
1003 	if (stsi(vmms, 3, 2, 2) == 0 && vmms->count)
1004 		add_device_randomness(&vmms->vm, sizeof(vmms->vm[0]) * vmms->count);
1005 	memblock_free((unsigned long) vmms, PAGE_SIZE);
1006 }
1007 
1008 /*
1009  * Find the correct size for the task_struct. This depends on
1010  * the size of the struct fpu at the end of the thread_struct
1011  * which is embedded in the task_struct.
1012  */
1013 static void __init setup_task_size(void)
1014 {
1015 	int task_size = sizeof(struct task_struct);
1016 
1017 	if (!MACHINE_HAS_VX) {
1018 		task_size -= sizeof(__vector128) * __NUM_VXRS;
1019 		task_size += sizeof(freg_t) * __NUM_FPRS;
1020 	}
1021 	arch_task_struct_size = task_size;
1022 }
1023 
1024 /*
1025  * Issue diagnose 318 to set the control program name and
1026  * version codes.
1027  */
1028 static void __init setup_control_program_code(void)
1029 {
1030 	union diag318_info diag318_info = {
1031 		.cpnc = CPNC_LINUX,
1032 		.cpvc_linux = 0,
1033 		.cpvc_distro = {0},
1034 	};
1035 
1036 	if (!sclp.has_diag318)
1037 		return;
1038 
1039 	diag_stat_inc(DIAG_STAT_X318);
1040 	asm volatile("diag %0,0,0x318\n" : : "d" (diag318_info.val));
1041 }
1042 
1043 /*
1044  * Print the component list from the IPL report
1045  */
1046 static void __init log_component_list(void)
1047 {
1048 	struct ipl_rb_component_entry *ptr, *end;
1049 	char *str;
1050 
1051 	if (!early_ipl_comp_list_addr)
1052 		return;
1053 	if (ipl_block.hdr.flags & IPL_PL_FLAG_SIPL)
1054 		pr_info("Linux is running with Secure-IPL enabled\n");
1055 	else
1056 		pr_info("Linux is running with Secure-IPL disabled\n");
1057 	ptr = (void *) early_ipl_comp_list_addr;
1058 	end = (void *) ptr + early_ipl_comp_list_size;
1059 	pr_info("The IPL report contains the following components:\n");
1060 	while (ptr < end) {
1061 		if (ptr->flags & IPL_RB_COMPONENT_FLAG_SIGNED) {
1062 			if (ptr->flags & IPL_RB_COMPONENT_FLAG_VERIFIED)
1063 				str = "signed, verified";
1064 			else
1065 				str = "signed, verification failed";
1066 		} else {
1067 			str = "not signed";
1068 		}
1069 		pr_info("%016llx - %016llx (%s)\n",
1070 			ptr->addr, ptr->addr + ptr->len, str);
1071 		ptr++;
1072 	}
1073 }
1074 
1075 /*
1076  * Setup function called from init/main.c just after the banner
1077  * was printed.
1078  */
1079 
1080 void __init setup_arch(char **cmdline_p)
1081 {
1082         /*
1083          * print what head.S has found out about the machine
1084          */
1085 	if (MACHINE_IS_VM)
1086 		pr_info("Linux is running as a z/VM "
1087 			"guest operating system in 64-bit mode\n");
1088 	else if (MACHINE_IS_KVM)
1089 		pr_info("Linux is running under KVM in 64-bit mode\n");
1090 	else if (MACHINE_IS_LPAR)
1091 		pr_info("Linux is running natively in 64-bit mode\n");
1092 	else
1093 		pr_info("Linux is running as a guest in 64-bit mode\n");
1094 
1095 	log_component_list();
1096 
1097 	/* Have one command line that is parsed and saved in /proc/cmdline */
1098 	/* boot_command_line has been already set up in early.c */
1099 	*cmdline_p = boot_command_line;
1100 
1101         ROOT_DEV = Root_RAM0;
1102 
1103 	init_mm.start_code = (unsigned long) _text;
1104 	init_mm.end_code = (unsigned long) _etext;
1105 	init_mm.end_data = (unsigned long) _edata;
1106 	init_mm.brk = (unsigned long) _end;
1107 
1108 	if (IS_ENABLED(CONFIG_EXPOLINE_AUTO))
1109 		nospec_auto_detect();
1110 
1111 	parse_early_param();
1112 #ifdef CONFIG_CRASH_DUMP
1113 	/* Deactivate elfcorehdr= kernel parameter */
1114 	elfcorehdr_addr = ELFCORE_ADDR_MAX;
1115 #endif
1116 
1117 	os_info_init();
1118 	setup_ipl();
1119 	setup_task_size();
1120 	setup_control_program_code();
1121 
1122 	/* Do some memory reservations *before* memory is added to memblock */
1123 	reserve_memory_end();
1124 	reserve_oldmem();
1125 	reserve_kernel();
1126 	reserve_initrd();
1127 	reserve_certificate_list();
1128 	reserve_mem_detect_info();
1129 	memblock_allow_resize();
1130 
1131 	/* Get information about *all* installed memory */
1132 	memblock_add_mem_detect_info();
1133 
1134 	free_mem_detect_info();
1135 	remove_oldmem();
1136 
1137 	/*
1138 	 * Make sure all chunks are MAX_ORDER aligned so we don't need the
1139 	 * extra checks that HOLES_IN_ZONE would require.
1140 	 *
1141 	 * Is this still required?
1142 	 */
1143 	memblock_trim_memory(1UL << (MAX_ORDER - 1 + PAGE_SHIFT));
1144 
1145 	setup_memory_end();
1146 	setup_memory();
1147 	dma_contiguous_reserve(memory_end);
1148 	vmcp_cma_reserve();
1149 
1150 	check_initrd();
1151 	reserve_crashkernel();
1152 #ifdef CONFIG_CRASH_DUMP
1153 	/*
1154 	 * Be aware that smp_save_dump_cpus() triggers a system reset.
1155 	 * Therefore CPU and device initialization should be done afterwards.
1156 	 */
1157 	smp_save_dump_cpus();
1158 #endif
1159 
1160 	setup_resources();
1161 	setup_lowcore_dat_off();
1162 	smp_fill_possible_mask();
1163 	cpu_detect_mhz_feature();
1164         cpu_init();
1165 	numa_setup();
1166 	smp_detect_cpus();
1167 	topology_init_early();
1168 
1169 	/*
1170 	 * Create kernel page tables and switch to virtual addressing.
1171 	 */
1172         paging_init();
1173 
1174 	/*
1175 	 * After paging_init created the kernel page table, the new PSWs
1176 	 * in lowcore can now run with DAT enabled.
1177 	 */
1178 	setup_lowcore_dat_on();
1179 
1180         /* Setup default console */
1181 	conmode_default();
1182 	set_preferred_console();
1183 
1184 	apply_alternative_instructions();
1185 	if (IS_ENABLED(CONFIG_EXPOLINE))
1186 		nospec_init_branches();
1187 
1188 	/* Setup zfcpdump support */
1189 	setup_zfcpdump();
1190 
1191 	/* Add system specific data to the random pool */
1192 	setup_randomness();
1193 }
1194