mm/memblock: define memblock_physmem_add()
[linux-2.6-microblaze.git] / arch / s390 / kernel / setup.c
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 const char * __init get_mem_info_source(void)
763 {
764         switch (mem_detect.info_source) {
765         case MEM_DETECT_SCLP_STOR_INFO:
766                 return "sclp storage info";
767         case MEM_DETECT_DIAG260:
768                 return "diag260";
769         case MEM_DETECT_SCLP_READ_INFO:
770                 return "sclp read info";
771         case MEM_DETECT_BIN_SEARCH:
772                 return "binary search";
773         }
774         return "none";
775 }
776
777 static void __init memblock_add_mem_detect_info(void)
778 {
779         unsigned long start, end;
780         int i;
781
782         memblock_dbg("physmem info source: %s (%hhd)\n",
783                      get_mem_info_source(), mem_detect.info_source);
784         /* keep memblock lists close to the kernel */
785         memblock_set_bottom_up(true);
786         for_each_mem_detect_block(i, &start, &end) {
787                 memblock_add(start, end - start);
788                 memblock_physmem_add(start, end - start);
789         }
790         memblock_set_bottom_up(false);
791         memblock_dump_all();
792 }
793
794 /*
795  * Check for initrd being in usable memory
796  */
797 static void __init check_initrd(void)
798 {
799 #ifdef CONFIG_BLK_DEV_INITRD
800         if (INITRD_START && INITRD_SIZE &&
801             !memblock_is_region_memory(INITRD_START, INITRD_SIZE)) {
802                 pr_err("The initial RAM disk does not fit into the memory\n");
803                 memblock_free(INITRD_START, INITRD_SIZE);
804                 initrd_start = initrd_end = 0;
805         }
806 #endif
807 }
808
809 /*
810  * Reserve memory used for lowcore/command line/kernel image.
811  */
812 static void __init reserve_kernel(void)
813 {
814         unsigned long start_pfn = PFN_UP(__pa(_end));
815
816         memblock_reserve(0, HEAD_END);
817         memblock_reserve((unsigned long)_stext, PFN_PHYS(start_pfn)
818                          - (unsigned long)_stext);
819         memblock_reserve(__sdma, __edma - __sdma);
820 }
821
822 static void __init setup_memory(void)
823 {
824         struct memblock_region *reg;
825
826         /*
827          * Init storage key for present memory
828          */
829         for_each_memblock(memory, reg) {
830                 storage_key_init_range(reg->base, reg->base + reg->size);
831         }
832         psw_set_key(PAGE_DEFAULT_KEY);
833
834         /* Only cosmetics */
835         memblock_enforce_memory_limit(memblock_end_of_DRAM());
836 }
837
838 /*
839  * Setup hardware capabilities.
840  */
841 static int __init setup_hwcaps(void)
842 {
843         static const int stfl_bits[6] = { 0, 2, 7, 17, 19, 21 };
844         struct cpuid cpu_id;
845         int i;
846
847         /*
848          * The store facility list bits numbers as found in the principles
849          * of operation are numbered with bit 1UL<<31 as number 0 to
850          * bit 1UL<<0 as number 31.
851          *   Bit 0: instructions named N3, "backported" to esa-mode
852          *   Bit 2: z/Architecture mode is active
853          *   Bit 7: the store-facility-list-extended facility is installed
854          *   Bit 17: the message-security assist is installed
855          *   Bit 19: the long-displacement facility is installed
856          *   Bit 21: the extended-immediate facility is installed
857          *   Bit 22: extended-translation facility 3 is installed
858          *   Bit 30: extended-translation facility 3 enhancement facility
859          * These get translated to:
860          *   HWCAP_S390_ESAN3 bit 0, HWCAP_S390_ZARCH bit 1,
861          *   HWCAP_S390_STFLE bit 2, HWCAP_S390_MSA bit 3,
862          *   HWCAP_S390_LDISP bit 4, HWCAP_S390_EIMM bit 5 and
863          *   HWCAP_S390_ETF3EH bit 8 (22 && 30).
864          */
865         for (i = 0; i < 6; i++)
866                 if (test_facility(stfl_bits[i]))
867                         elf_hwcap |= 1UL << i;
868
869         if (test_facility(22) && test_facility(30))
870                 elf_hwcap |= HWCAP_S390_ETF3EH;
871
872         /*
873          * Check for additional facilities with store-facility-list-extended.
874          * stfle stores doublewords (8 byte) with bit 1ULL<<63 as bit 0
875          * and 1ULL<<0 as bit 63. Bits 0-31 contain the same information
876          * as stored by stfl, bits 32-xxx contain additional facilities.
877          * How many facility words are stored depends on the number of
878          * doublewords passed to the instruction. The additional facilities
879          * are:
880          *   Bit 42: decimal floating point facility is installed
881          *   Bit 44: perform floating point operation facility is installed
882          * translated to:
883          *   HWCAP_S390_DFP bit 6 (42 && 44).
884          */
885         if ((elf_hwcap & (1UL << 2)) && test_facility(42) && test_facility(44))
886                 elf_hwcap |= HWCAP_S390_DFP;
887
888         /*
889          * Huge page support HWCAP_S390_HPAGE is bit 7.
890          */
891         if (MACHINE_HAS_EDAT1)
892                 elf_hwcap |= HWCAP_S390_HPAGE;
893
894         /*
895          * 64-bit register support for 31-bit processes
896          * HWCAP_S390_HIGH_GPRS is bit 9.
897          */
898         elf_hwcap |= HWCAP_S390_HIGH_GPRS;
899
900         /*
901          * Transactional execution support HWCAP_S390_TE is bit 10.
902          */
903         if (MACHINE_HAS_TE)
904                 elf_hwcap |= HWCAP_S390_TE;
905
906         /*
907          * Vector extension HWCAP_S390_VXRS is bit 11. The Vector extension
908          * can be disabled with the "novx" parameter. Use MACHINE_HAS_VX
909          * instead of facility bit 129.
910          */
911         if (MACHINE_HAS_VX) {
912                 elf_hwcap |= HWCAP_S390_VXRS;
913                 if (test_facility(134))
914                         elf_hwcap |= HWCAP_S390_VXRS_EXT;
915                 if (test_facility(135))
916                         elf_hwcap |= HWCAP_S390_VXRS_BCD;
917                 if (test_facility(148))
918                         elf_hwcap |= HWCAP_S390_VXRS_EXT2;
919                 if (test_facility(152))
920                         elf_hwcap |= HWCAP_S390_VXRS_PDE;
921         }
922         if (test_facility(150))
923                 elf_hwcap |= HWCAP_S390_SORT;
924         if (test_facility(151))
925                 elf_hwcap |= HWCAP_S390_DFLT;
926
927         /*
928          * Guarded storage support HWCAP_S390_GS is bit 12.
929          */
930         if (MACHINE_HAS_GS)
931                 elf_hwcap |= HWCAP_S390_GS;
932
933         get_cpu_id(&cpu_id);
934         add_device_randomness(&cpu_id, sizeof(cpu_id));
935         switch (cpu_id.machine) {
936         case 0x2064:
937         case 0x2066:
938         default:        /* Use "z900" as default for 64 bit kernels. */
939                 strcpy(elf_platform, "z900");
940                 break;
941         case 0x2084:
942         case 0x2086:
943                 strcpy(elf_platform, "z990");
944                 break;
945         case 0x2094:
946         case 0x2096:
947                 strcpy(elf_platform, "z9-109");
948                 break;
949         case 0x2097:
950         case 0x2098:
951                 strcpy(elf_platform, "z10");
952                 break;
953         case 0x2817:
954         case 0x2818:
955                 strcpy(elf_platform, "z196");
956                 break;
957         case 0x2827:
958         case 0x2828:
959                 strcpy(elf_platform, "zEC12");
960                 break;
961         case 0x2964:
962         case 0x2965:
963                 strcpy(elf_platform, "z13");
964                 break;
965         case 0x3906:
966         case 0x3907:
967                 strcpy(elf_platform, "z14");
968                 break;
969         case 0x8561:
970         case 0x8562:
971                 strcpy(elf_platform, "z15");
972                 break;
973         }
974
975         /*
976          * Virtualization support HWCAP_INT_SIE is bit 0.
977          */
978         if (sclp.has_sief2)
979                 int_hwcap |= HWCAP_INT_SIE;
980
981         return 0;
982 }
983 arch_initcall(setup_hwcaps);
984
985 /*
986  * Add system information as device randomness
987  */
988 static void __init setup_randomness(void)
989 {
990         struct sysinfo_3_2_2 *vmms;
991
992         vmms = (struct sysinfo_3_2_2 *) memblock_phys_alloc(PAGE_SIZE,
993                                                             PAGE_SIZE);
994         if (!vmms)
995                 panic("Failed to allocate memory for sysinfo structure\n");
996
997         if (stsi(vmms, 3, 2, 2) == 0 && vmms->count)
998                 add_device_randomness(&vmms->vm, sizeof(vmms->vm[0]) * vmms->count);
999         memblock_free((unsigned long) vmms, PAGE_SIZE);
1000 }
1001
1002 /*
1003  * Find the correct size for the task_struct. This depends on
1004  * the size of the struct fpu at the end of the thread_struct
1005  * which is embedded in the task_struct.
1006  */
1007 static void __init setup_task_size(void)
1008 {
1009         int task_size = sizeof(struct task_struct);
1010
1011         if (!MACHINE_HAS_VX) {
1012                 task_size -= sizeof(__vector128) * __NUM_VXRS;
1013                 task_size += sizeof(freg_t) * __NUM_FPRS;
1014         }
1015         arch_task_struct_size = task_size;
1016 }
1017
1018 /*
1019  * Issue diagnose 318 to set the control program name and
1020  * version codes.
1021  */
1022 static void __init setup_control_program_code(void)
1023 {
1024         union diag318_info diag318_info = {
1025                 .cpnc = CPNC_LINUX,
1026                 .cpvc_linux = 0,
1027                 .cpvc_distro = {0},
1028         };
1029
1030         if (!sclp.has_diag318)
1031                 return;
1032
1033         diag_stat_inc(DIAG_STAT_X318);
1034         asm volatile("diag %0,0,0x318\n" : : "d" (diag318_info.val));
1035 }
1036
1037 /*
1038  * Print the component list from the IPL report
1039  */
1040 static void __init log_component_list(void)
1041 {
1042         struct ipl_rb_component_entry *ptr, *end;
1043         char *str;
1044
1045         if (!early_ipl_comp_list_addr)
1046                 return;
1047         if (ipl_block.hdr.flags & IPL_PL_FLAG_SIPL)
1048                 pr_info("Linux is running with Secure-IPL enabled\n");
1049         else
1050                 pr_info("Linux is running with Secure-IPL disabled\n");
1051         ptr = (void *) early_ipl_comp_list_addr;
1052         end = (void *) ptr + early_ipl_comp_list_size;
1053         pr_info("The IPL report contains the following components:\n");
1054         while (ptr < end) {
1055                 if (ptr->flags & IPL_RB_COMPONENT_FLAG_SIGNED) {
1056                         if (ptr->flags & IPL_RB_COMPONENT_FLAG_VERIFIED)
1057                                 str = "signed, verified";
1058                         else
1059                                 str = "signed, verification failed";
1060                 } else {
1061                         str = "not signed";
1062                 }
1063                 pr_info("%016llx - %016llx (%s)\n",
1064                         ptr->addr, ptr->addr + ptr->len, str);
1065                 ptr++;
1066         }
1067 }
1068
1069 /*
1070  * Setup function called from init/main.c just after the banner
1071  * was printed.
1072  */
1073
1074 void __init setup_arch(char **cmdline_p)
1075 {
1076         /*
1077          * print what head.S has found out about the machine
1078          */
1079         if (MACHINE_IS_VM)
1080                 pr_info("Linux is running as a z/VM "
1081                         "guest operating system in 64-bit mode\n");
1082         else if (MACHINE_IS_KVM)
1083                 pr_info("Linux is running under KVM in 64-bit mode\n");
1084         else if (MACHINE_IS_LPAR)
1085                 pr_info("Linux is running natively in 64-bit mode\n");
1086         else
1087                 pr_info("Linux is running as a guest in 64-bit mode\n");
1088
1089         log_component_list();
1090
1091         /* Have one command line that is parsed and saved in /proc/cmdline */
1092         /* boot_command_line has been already set up in early.c */
1093         *cmdline_p = boot_command_line;
1094
1095         ROOT_DEV = Root_RAM0;
1096
1097         init_mm.start_code = (unsigned long) _text;
1098         init_mm.end_code = (unsigned long) _etext;
1099         init_mm.end_data = (unsigned long) _edata;
1100         init_mm.brk = (unsigned long) _end;
1101
1102         if (IS_ENABLED(CONFIG_EXPOLINE_AUTO))
1103                 nospec_auto_detect();
1104
1105         parse_early_param();
1106 #ifdef CONFIG_CRASH_DUMP
1107         /* Deactivate elfcorehdr= kernel parameter */
1108         elfcorehdr_addr = ELFCORE_ADDR_MAX;
1109 #endif
1110
1111         os_info_init();
1112         setup_ipl();
1113         setup_task_size();
1114         setup_control_program_code();
1115
1116         /* Do some memory reservations *before* memory is added to memblock */
1117         reserve_memory_end();
1118         reserve_oldmem();
1119         reserve_kernel();
1120         reserve_initrd();
1121         reserve_certificate_list();
1122         reserve_mem_detect_info();
1123         memblock_allow_resize();
1124
1125         /* Get information about *all* installed memory */
1126         memblock_add_mem_detect_info();
1127
1128         free_mem_detect_info();
1129         remove_oldmem();
1130
1131         /*
1132          * Make sure all chunks are MAX_ORDER aligned so we don't need the
1133          * extra checks that HOLES_IN_ZONE would require.
1134          *
1135          * Is this still required?
1136          */
1137         memblock_trim_memory(1UL << (MAX_ORDER - 1 + PAGE_SHIFT));
1138
1139         setup_memory_end();
1140         setup_memory();
1141         dma_contiguous_reserve(memory_end);
1142         vmcp_cma_reserve();
1143
1144         check_initrd();
1145         reserve_crashkernel();
1146 #ifdef CONFIG_CRASH_DUMP
1147         /*
1148          * Be aware that smp_save_dump_cpus() triggers a system reset.
1149          * Therefore CPU and device initialization should be done afterwards.
1150          */
1151         smp_save_dump_cpus();
1152 #endif
1153
1154         setup_resources();
1155         setup_lowcore_dat_off();
1156         smp_fill_possible_mask();
1157         cpu_detect_mhz_feature();
1158         cpu_init();
1159         numa_setup();
1160         smp_detect_cpus();
1161         topology_init_early();
1162
1163         /*
1164          * Create kernel page tables and switch to virtual addressing.
1165          */
1166         paging_init();
1167
1168         /*
1169          * After paging_init created the kernel page table, the new PSWs
1170          * in lowcore can now run with DAT enabled.
1171          */
1172         setup_lowcore_dat_on();
1173
1174         /* Setup default console */
1175         conmode_default();
1176         set_preferred_console();
1177
1178         apply_alternative_instructions();
1179         if (IS_ENABLED(CONFIG_EXPOLINE))
1180                 nospec_init_branches();
1181
1182         /* Setup zfcpdump support */
1183         setup_zfcpdump();
1184
1185         /* Add system specific data to the random pool */
1186         setup_randomness();
1187 }