setup.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. * This file handles the architecture-dependent parts of initialization
  13. */
  14. #define KMSG_COMPONENT "setup"
  15. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  16. #include <linux/errno.h>
  17. #include <linux/export.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/cpu.h>
  21. #include <linux/kernel.h>
  22. #include <linux/memblock.h>
  23. #include <linux/mm.h>
  24. #include <linux/stddef.h>
  25. #include <linux/unistd.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/random.h>
  28. #include <linux/user.h>
  29. #include <linux/tty.h>
  30. #include <linux/ioport.h>
  31. #include <linux/delay.h>
  32. #include <linux/init.h>
  33. #include <linux/initrd.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/root_dev.h>
  36. #include <linux/console.h>
  37. #include <linux/kernel_stat.h>
  38. #include <linux/dma-contiguous.h>
  39. #include <linux/device.h>
  40. #include <linux/notifier.h>
  41. #include <linux/pfn.h>
  42. #include <linux/ctype.h>
  43. #include <linux/reboot.h>
  44. #include <linux/topology.h>
  45. #include <linux/kexec.h>
  46. #include <linux/crash_dump.h>
  47. #include <linux/memory.h>
  48. #include <linux/compat.h>
  49. #include <asm/ipl.h>
  50. #include <asm/facility.h>
  51. #include <asm/smp.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/cpcmd.h>
  54. #include <asm/lowcore.h>
  55. #include <asm/nmi.h>
  56. #include <asm/irq.h>
  57. #include <asm/page.h>
  58. #include <asm/ptrace.h>
  59. #include <asm/sections.h>
  60. #include <asm/ebcdic.h>
  61. #include <asm/diag.h>
  62. #include <asm/os_info.h>
  63. #include <asm/sclp.h>
  64. #include <asm/sysinfo.h>
  65. #include <asm/numa.h>
  66. #include <asm/alternative.h>
  67. #include <asm/nospec-branch.h>
  68. #include "entry.h"
  69. /*
  70. * Machine setup..
  71. */
  72. unsigned int console_mode = 0;
  73. EXPORT_SYMBOL(console_mode);
  74. unsigned int console_devno = -1;
  75. EXPORT_SYMBOL(console_devno);
  76. unsigned int console_irq = -1;
  77. EXPORT_SYMBOL(console_irq);
  78. unsigned long elf_hwcap __read_mostly = 0;
  79. char elf_platform[ELF_PLATFORM_SIZE];
  80. unsigned long int_hwcap = 0;
  81. int __initdata memory_end_set;
  82. unsigned long __initdata memory_end;
  83. unsigned long __initdata max_physmem_end;
  84. unsigned long VMALLOC_START;
  85. EXPORT_SYMBOL(VMALLOC_START);
  86. unsigned long VMALLOC_END;
  87. EXPORT_SYMBOL(VMALLOC_END);
  88. struct page *vmemmap;
  89. EXPORT_SYMBOL(vmemmap);
  90. unsigned long MODULES_VADDR;
  91. unsigned long MODULES_END;
  92. /* An array with a pointer to the lowcore of every CPU. */
  93. struct lowcore *lowcore_ptr[NR_CPUS];
  94. EXPORT_SYMBOL(lowcore_ptr);
  95. /*
  96. * This is set up by the setup-routine at boot-time
  97. * for S390 need to find out, what we have to setup
  98. * using address 0x10400 ...
  99. */
  100. #include <asm/setup.h>
  101. /*
  102. * condev= and conmode= setup parameter.
  103. */
  104. static int __init condev_setup(char *str)
  105. {
  106. int vdev;
  107. vdev = simple_strtoul(str, &str, 0);
  108. if (vdev >= 0 && vdev < 65536) {
  109. console_devno = vdev;
  110. console_irq = -1;
  111. }
  112. return 1;
  113. }
  114. __setup("condev=", condev_setup);
  115. static void __init set_preferred_console(void)
  116. {
  117. if (CONSOLE_IS_3215 || CONSOLE_IS_SCLP)
  118. add_preferred_console("ttyS", 0, NULL);
  119. else if (CONSOLE_IS_3270)
  120. add_preferred_console("tty3270", 0, NULL);
  121. else if (CONSOLE_IS_VT220)
  122. add_preferred_console("ttyS", 1, NULL);
  123. else if (CONSOLE_IS_HVC)
  124. add_preferred_console("hvc", 0, NULL);
  125. }
  126. static int __init conmode_setup(char *str)
  127. {
  128. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  129. if (strncmp(str, "hwc", 4) == 0 || strncmp(str, "sclp", 5) == 0)
  130. SET_CONSOLE_SCLP;
  131. #endif
  132. #if defined(CONFIG_TN3215_CONSOLE)
  133. if (strncmp(str, "3215", 5) == 0)
  134. SET_CONSOLE_3215;
  135. #endif
  136. #if defined(CONFIG_TN3270_CONSOLE)
  137. if (strncmp(str, "3270", 5) == 0)
  138. SET_CONSOLE_3270;
  139. #endif
  140. set_preferred_console();
  141. return 1;
  142. }
  143. __setup("conmode=", conmode_setup);
  144. static void __init conmode_default(void)
  145. {
  146. char query_buffer[1024];
  147. char *ptr;
  148. if (MACHINE_IS_VM) {
  149. cpcmd("QUERY CONSOLE", query_buffer, 1024, NULL);
  150. console_devno = simple_strtoul(query_buffer + 5, NULL, 16);
  151. ptr = strstr(query_buffer, "SUBCHANNEL =");
  152. console_irq = simple_strtoul(ptr + 13, NULL, 16);
  153. cpcmd("QUERY TERM", query_buffer, 1024, NULL);
  154. ptr = strstr(query_buffer, "CONMODE");
  155. /*
  156. * Set the conmode to 3215 so that the device recognition
  157. * will set the cu_type of the console to 3215. If the
  158. * conmode is 3270 and we don't set it back then both
  159. * 3215 and the 3270 driver will try to access the console
  160. * device (3215 as console and 3270 as normal tty).
  161. */
  162. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  163. if (ptr == NULL) {
  164. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  165. SET_CONSOLE_SCLP;
  166. #endif
  167. return;
  168. }
  169. if (strncmp(ptr + 8, "3270", 4) == 0) {
  170. #if defined(CONFIG_TN3270_CONSOLE)
  171. SET_CONSOLE_3270;
  172. #elif defined(CONFIG_TN3215_CONSOLE)
  173. SET_CONSOLE_3215;
  174. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  175. SET_CONSOLE_SCLP;
  176. #endif
  177. } else if (strncmp(ptr + 8, "3215", 4) == 0) {
  178. #if defined(CONFIG_TN3215_CONSOLE)
  179. SET_CONSOLE_3215;
  180. #elif defined(CONFIG_TN3270_CONSOLE)
  181. SET_CONSOLE_3270;
  182. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  183. SET_CONSOLE_SCLP;
  184. #endif
  185. }
  186. } else if (MACHINE_IS_KVM) {
  187. if (sclp.has_vt220 && IS_ENABLED(CONFIG_SCLP_VT220_CONSOLE))
  188. SET_CONSOLE_VT220;
  189. else if (sclp.has_linemode && IS_ENABLED(CONFIG_SCLP_CONSOLE))
  190. SET_CONSOLE_SCLP;
  191. else
  192. SET_CONSOLE_HVC;
  193. } else {
  194. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  195. SET_CONSOLE_SCLP;
  196. #endif
  197. }
  198. if (IS_ENABLED(CONFIG_VT) && IS_ENABLED(CONFIG_DUMMY_CONSOLE))
  199. conswitchp = &dummy_con;
  200. }
  201. #ifdef CONFIG_CRASH_DUMP
  202. static void __init setup_zfcpdump(void)
  203. {
  204. if (ipl_info.type != IPL_TYPE_FCP_DUMP)
  205. return;
  206. if (OLDMEM_BASE)
  207. return;
  208. strcat(boot_command_line, " cio_ignore=all,!ipldev,!condev");
  209. console_loglevel = 2;
  210. }
  211. #else
  212. static inline void setup_zfcpdump(void) {}
  213. #endif /* CONFIG_CRASH_DUMP */
  214. /*
  215. * Reboot, halt and power_off stubs. They just call _machine_restart,
  216. * _machine_halt or _machine_power_off.
  217. */
  218. void machine_restart(char *command)
  219. {
  220. if ((!in_interrupt() && !in_atomic()) || oops_in_progress)
  221. /*
  222. * Only unblank the console if we are called in enabled
  223. * context or a bust_spinlocks cleared the way for us.
  224. */
  225. console_unblank();
  226. _machine_restart(command);
  227. }
  228. void machine_halt(void)
  229. {
  230. if (!in_interrupt() || oops_in_progress)
  231. /*
  232. * Only unblank the console if we are called in enabled
  233. * context or a bust_spinlocks cleared the way for us.
  234. */
  235. console_unblank();
  236. _machine_halt();
  237. }
  238. void machine_power_off(void)
  239. {
  240. if (!in_interrupt() || oops_in_progress)
  241. /*
  242. * Only unblank the console if we are called in enabled
  243. * context or a bust_spinlocks cleared the way for us.
  244. */
  245. console_unblank();
  246. _machine_power_off();
  247. }
  248. /*
  249. * Dummy power off function.
  250. */
  251. void (*pm_power_off)(void) = machine_power_off;
  252. EXPORT_SYMBOL_GPL(pm_power_off);
  253. static int __init early_parse_mem(char *p)
  254. {
  255. memory_end = memparse(p, &p);
  256. memory_end &= PAGE_MASK;
  257. memory_end_set = 1;
  258. return 0;
  259. }
  260. early_param("mem", early_parse_mem);
  261. static int __init parse_vmalloc(char *arg)
  262. {
  263. if (!arg)
  264. return -EINVAL;
  265. VMALLOC_END = (memparse(arg, &arg) + PAGE_SIZE - 1) & PAGE_MASK;
  266. return 0;
  267. }
  268. early_param("vmalloc", parse_vmalloc);
  269. void *restart_stack __section(.data);
  270. static void __init setup_lowcore_dat_off(void)
  271. {
  272. struct lowcore *lc;
  273. /*
  274. * Setup lowcore for boot cpu
  275. */
  276. BUILD_BUG_ON(sizeof(struct lowcore) != LC_PAGES * PAGE_SIZE);
  277. lc = memblock_virt_alloc_low(sizeof(*lc), sizeof(*lc));
  278. lc->restart_psw.mask = PSW_KERNEL_BITS;
  279. lc->restart_psw.addr = (unsigned long) restart_int_handler;
  280. lc->external_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK;
  281. lc->external_new_psw.addr = (unsigned long) ext_int_handler;
  282. lc->svc_new_psw.mask = PSW_KERNEL_BITS |
  283. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  284. lc->svc_new_psw.addr = (unsigned long) system_call;
  285. lc->program_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK;
  286. lc->program_new_psw.addr = (unsigned long) pgm_check_handler;
  287. lc->mcck_new_psw.mask = PSW_KERNEL_BITS;
  288. lc->mcck_new_psw.addr = (unsigned long) mcck_int_handler;
  289. lc->io_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_MCHECK;
  290. lc->io_new_psw.addr = (unsigned long) io_int_handler;
  291. lc->clock_comparator = clock_comparator_max;
  292. lc->kernel_stack = ((unsigned long) &init_thread_union)
  293. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  294. lc->async_stack = (unsigned long)
  295. memblock_virt_alloc(ASYNC_SIZE, ASYNC_SIZE)
  296. + ASYNC_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  297. lc->panic_stack = (unsigned long)
  298. memblock_virt_alloc(PAGE_SIZE, PAGE_SIZE)
  299. + PAGE_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  300. lc->current_task = (unsigned long)&init_task;
  301. lc->lpp = LPP_MAGIC;
  302. lc->machine_flags = S390_lowcore.machine_flags;
  303. lc->preempt_count = S390_lowcore.preempt_count;
  304. lc->stfl_fac_list = S390_lowcore.stfl_fac_list;
  305. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  306. sizeof(lc->stfle_fac_list));
  307. memcpy(lc->alt_stfle_fac_list, S390_lowcore.alt_stfle_fac_list,
  308. sizeof(lc->alt_stfle_fac_list));
  309. nmi_alloc_boot_cpu(lc);
  310. vdso_alloc_boot_cpu(lc);
  311. lc->sync_enter_timer = S390_lowcore.sync_enter_timer;
  312. lc->async_enter_timer = S390_lowcore.async_enter_timer;
  313. lc->exit_timer = S390_lowcore.exit_timer;
  314. lc->user_timer = S390_lowcore.user_timer;
  315. lc->system_timer = S390_lowcore.system_timer;
  316. lc->steal_timer = S390_lowcore.steal_timer;
  317. lc->last_update_timer = S390_lowcore.last_update_timer;
  318. lc->last_update_clock = S390_lowcore.last_update_clock;
  319. restart_stack = memblock_virt_alloc(ASYNC_SIZE, ASYNC_SIZE);
  320. restart_stack += ASYNC_SIZE;
  321. /*
  322. * Set up PSW restart to call ipl.c:do_restart(). Copy the relevant
  323. * restart data to the absolute zero lowcore. This is necessary if
  324. * PSW restart is done on an offline CPU that has lowcore zero.
  325. */
  326. lc->restart_stack = (unsigned long) restart_stack;
  327. lc->restart_fn = (unsigned long) do_restart;
  328. lc->restart_data = 0;
  329. lc->restart_source = -1UL;
  330. /* Setup absolute zero lowcore */
  331. mem_assign_absolute(S390_lowcore.restart_stack, lc->restart_stack);
  332. mem_assign_absolute(S390_lowcore.restart_fn, lc->restart_fn);
  333. mem_assign_absolute(S390_lowcore.restart_data, lc->restart_data);
  334. mem_assign_absolute(S390_lowcore.restart_source, lc->restart_source);
  335. mem_assign_absolute(S390_lowcore.restart_psw, lc->restart_psw);
  336. #ifdef CONFIG_SMP
  337. lc->spinlock_lockval = arch_spin_lockval(0);
  338. lc->spinlock_index = 0;
  339. arch_spin_lock_setup(0);
  340. #endif
  341. lc->br_r1_trampoline = 0x07f1; /* br %r1 */
  342. set_prefix((u32)(unsigned long) lc);
  343. lowcore_ptr[0] = lc;
  344. }
  345. static void __init setup_lowcore_dat_on(void)
  346. {
  347. __ctl_clear_bit(0, 28);
  348. S390_lowcore.external_new_psw.mask |= PSW_MASK_DAT;
  349. S390_lowcore.svc_new_psw.mask |= PSW_MASK_DAT;
  350. S390_lowcore.program_new_psw.mask |= PSW_MASK_DAT;
  351. S390_lowcore.io_new_psw.mask |= PSW_MASK_DAT;
  352. __ctl_set_bit(0, 28);
  353. }
  354. static struct resource code_resource = {
  355. .name = "Kernel code",
  356. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  357. };
  358. static struct resource data_resource = {
  359. .name = "Kernel data",
  360. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  361. };
  362. static struct resource bss_resource = {
  363. .name = "Kernel bss",
  364. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  365. };
  366. static struct resource __initdata *standard_resources[] = {
  367. &code_resource,
  368. &data_resource,
  369. &bss_resource,
  370. };
  371. static void __init setup_resources(void)
  372. {
  373. struct resource *res, *std_res, *sub_res;
  374. struct memblock_region *reg;
  375. int j;
  376. code_resource.start = (unsigned long) _text;
  377. code_resource.end = (unsigned long) _etext - 1;
  378. data_resource.start = (unsigned long) _etext;
  379. data_resource.end = (unsigned long) _edata - 1;
  380. bss_resource.start = (unsigned long) __bss_start;
  381. bss_resource.end = (unsigned long) __bss_stop - 1;
  382. for_each_memblock(memory, reg) {
  383. res = memblock_virt_alloc(sizeof(*res), 8);
  384. res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  385. res->name = "System RAM";
  386. res->start = reg->base;
  387. res->end = reg->base + reg->size - 1;
  388. request_resource(&iomem_resource, res);
  389. for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
  390. std_res = standard_resources[j];
  391. if (std_res->start < res->start ||
  392. std_res->start > res->end)
  393. continue;
  394. if (std_res->end > res->end) {
  395. sub_res = memblock_virt_alloc(sizeof(*sub_res), 8);
  396. *sub_res = *std_res;
  397. sub_res->end = res->end;
  398. std_res->start = res->end + 1;
  399. request_resource(res, sub_res);
  400. } else {
  401. request_resource(res, std_res);
  402. }
  403. }
  404. }
  405. #ifdef CONFIG_CRASH_DUMP
  406. /*
  407. * Re-add removed crash kernel memory as reserved memory. This makes
  408. * sure it will be mapped with the identity mapping and struct pages
  409. * will be created, so it can be resized later on.
  410. * However add it later since the crash kernel resource should not be
  411. * part of the System RAM resource.
  412. */
  413. if (crashk_res.end) {
  414. memblock_add_node(crashk_res.start, resource_size(&crashk_res), 0);
  415. memblock_reserve(crashk_res.start, resource_size(&crashk_res));
  416. insert_resource(&iomem_resource, &crashk_res);
  417. }
  418. #endif
  419. }
  420. static void __init setup_memory_end(void)
  421. {
  422. unsigned long vmax, vmalloc_size, tmp;
  423. /* Choose kernel address space layout: 2, 3, or 4 levels. */
  424. vmalloc_size = VMALLOC_END ?: (128UL << 30) - MODULES_LEN;
  425. tmp = (memory_end ?: max_physmem_end) / PAGE_SIZE;
  426. tmp = tmp * (sizeof(struct page) + PAGE_SIZE);
  427. if (tmp + vmalloc_size + MODULES_LEN <= _REGION2_SIZE)
  428. vmax = _REGION2_SIZE; /* 3-level kernel page table */
  429. else
  430. vmax = _REGION1_SIZE; /* 4-level kernel page table */
  431. /* module area is at the end of the kernel address space. */
  432. MODULES_END = vmax;
  433. MODULES_VADDR = MODULES_END - MODULES_LEN;
  434. VMALLOC_END = MODULES_VADDR;
  435. VMALLOC_START = vmax - vmalloc_size;
  436. /* Split remaining virtual space between 1:1 mapping & vmemmap array */
  437. tmp = VMALLOC_START / (PAGE_SIZE + sizeof(struct page));
  438. /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
  439. tmp = SECTION_ALIGN_UP(tmp);
  440. tmp = VMALLOC_START - tmp * sizeof(struct page);
  441. tmp &= ~((vmax >> 11) - 1); /* align to page table level */
  442. tmp = min(tmp, 1UL << MAX_PHYSMEM_BITS);
  443. vmemmap = (struct page *) tmp;
  444. /* Take care that memory_end is set and <= vmemmap */
  445. memory_end = min(memory_end ?: max_physmem_end, tmp);
  446. max_pfn = max_low_pfn = PFN_DOWN(memory_end);
  447. memblock_remove(memory_end, ULONG_MAX);
  448. pr_notice("The maximum memory size is %luMB\n", memory_end >> 20);
  449. }
  450. #ifdef CONFIG_CRASH_DUMP
  451. /*
  452. * When kdump is enabled, we have to ensure that no memory from
  453. * the area [0 - crashkernel memory size] and
  454. * [crashk_res.start - crashk_res.end] is set offline.
  455. */
  456. static int kdump_mem_notifier(struct notifier_block *nb,
  457. unsigned long action, void *data)
  458. {
  459. struct memory_notify *arg = data;
  460. if (action != MEM_GOING_OFFLINE)
  461. return NOTIFY_OK;
  462. if (arg->start_pfn < PFN_DOWN(resource_size(&crashk_res)))
  463. return NOTIFY_BAD;
  464. if (arg->start_pfn > PFN_DOWN(crashk_res.end))
  465. return NOTIFY_OK;
  466. if (arg->start_pfn + arg->nr_pages - 1 < PFN_DOWN(crashk_res.start))
  467. return NOTIFY_OK;
  468. return NOTIFY_BAD;
  469. }
  470. static struct notifier_block kdump_mem_nb = {
  471. .notifier_call = kdump_mem_notifier,
  472. };
  473. #endif
  474. /*
  475. * Make sure that the area behind memory_end is protected
  476. */
  477. static void reserve_memory_end(void)
  478. {
  479. #ifdef CONFIG_CRASH_DUMP
  480. if (ipl_info.type == IPL_TYPE_FCP_DUMP &&
  481. !OLDMEM_BASE && sclp.hsa_size) {
  482. memory_end = sclp.hsa_size;
  483. memory_end &= PAGE_MASK;
  484. memory_end_set = 1;
  485. }
  486. #endif
  487. if (!memory_end_set)
  488. return;
  489. memblock_reserve(memory_end, ULONG_MAX);
  490. }
  491. /*
  492. * Make sure that oldmem, where the dump is stored, is protected
  493. */
  494. static void reserve_oldmem(void)
  495. {
  496. #ifdef CONFIG_CRASH_DUMP
  497. if (OLDMEM_BASE)
  498. /* Forget all memory above the running kdump system */
  499. memblock_reserve(OLDMEM_SIZE, (phys_addr_t)ULONG_MAX);
  500. #endif
  501. }
  502. /*
  503. * Make sure that oldmem, where the dump is stored, is protected
  504. */
  505. static void remove_oldmem(void)
  506. {
  507. #ifdef CONFIG_CRASH_DUMP
  508. if (OLDMEM_BASE)
  509. /* Forget all memory above the running kdump system */
  510. memblock_remove(OLDMEM_SIZE, (phys_addr_t)ULONG_MAX);
  511. #endif
  512. }
  513. /*
  514. * Reserve memory for kdump kernel to be loaded with kexec
  515. */
  516. static void __init reserve_crashkernel(void)
  517. {
  518. #ifdef CONFIG_CRASH_DUMP
  519. unsigned long long crash_base, crash_size;
  520. phys_addr_t low, high;
  521. int rc;
  522. rc = parse_crashkernel(boot_command_line, memory_end, &crash_size,
  523. &crash_base);
  524. crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
  525. crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
  526. if (rc || crash_size == 0)
  527. return;
  528. if (memblock.memory.regions[0].size < crash_size) {
  529. pr_info("crashkernel reservation failed: %s\n",
  530. "first memory chunk must be at least crashkernel size");
  531. return;
  532. }
  533. low = crash_base ?: OLDMEM_BASE;
  534. high = low + crash_size;
  535. if (low >= OLDMEM_BASE && high <= OLDMEM_BASE + OLDMEM_SIZE) {
  536. /* The crashkernel fits into OLDMEM, reuse OLDMEM */
  537. crash_base = low;
  538. } else {
  539. /* Find suitable area in free memory */
  540. low = max_t(unsigned long, crash_size, sclp.hsa_size);
  541. high = crash_base ? crash_base + crash_size : ULONG_MAX;
  542. if (crash_base && crash_base < low) {
  543. pr_info("crashkernel reservation failed: %s\n",
  544. "crash_base too low");
  545. return;
  546. }
  547. low = crash_base ?: low;
  548. crash_base = memblock_find_in_range(low, high, crash_size,
  549. KEXEC_CRASH_MEM_ALIGN);
  550. }
  551. if (!crash_base) {
  552. pr_info("crashkernel reservation failed: %s\n",
  553. "no suitable area found");
  554. return;
  555. }
  556. if (register_memory_notifier(&kdump_mem_nb))
  557. return;
  558. if (!OLDMEM_BASE && MACHINE_IS_VM)
  559. diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));
  560. crashk_res.start = crash_base;
  561. crashk_res.end = crash_base + crash_size - 1;
  562. memblock_remove(crash_base, crash_size);
  563. pr_info("Reserving %lluMB of memory at %lluMB "
  564. "for crashkernel (System RAM: %luMB)\n",
  565. crash_size >> 20, crash_base >> 20,
  566. (unsigned long)memblock.memory.total_size >> 20);
  567. os_info_crashkernel_add(crash_base, crash_size);
  568. #endif
  569. }
  570. /*
  571. * Reserve the initrd from being used by memblock
  572. */
  573. static void __init reserve_initrd(void)
  574. {
  575. #ifdef CONFIG_BLK_DEV_INITRD
  576. if (!INITRD_START || !INITRD_SIZE)
  577. return;
  578. initrd_start = INITRD_START;
  579. initrd_end = initrd_start + INITRD_SIZE;
  580. memblock_reserve(INITRD_START, INITRD_SIZE);
  581. #endif
  582. }
  583. /*
  584. * Check for initrd being in usable memory
  585. */
  586. static void __init check_initrd(void)
  587. {
  588. #ifdef CONFIG_BLK_DEV_INITRD
  589. if (INITRD_START && INITRD_SIZE &&
  590. !memblock_is_region_memory(INITRD_START, INITRD_SIZE)) {
  591. pr_err("The initial RAM disk does not fit into the memory\n");
  592. memblock_free(INITRD_START, INITRD_SIZE);
  593. initrd_start = initrd_end = 0;
  594. }
  595. #endif
  596. }
  597. /*
  598. * Reserve memory used for lowcore/command line/kernel image.
  599. */
  600. static void __init reserve_kernel(void)
  601. {
  602. unsigned long start_pfn = PFN_UP(__pa(_end));
  603. #ifdef CONFIG_DMA_API_DEBUG
  604. /*
  605. * DMA_API_DEBUG code stumbles over addresses from the
  606. * range [PARMAREA_END, _stext]. Mark the memory as reserved
  607. * so it is not used for CONFIG_DMA_API_DEBUG=y.
  608. */
  609. memblock_reserve(0, PFN_PHYS(start_pfn));
  610. #else
  611. memblock_reserve(0, PARMAREA_END);
  612. memblock_reserve((unsigned long)_stext, PFN_PHYS(start_pfn)
  613. - (unsigned long)_stext);
  614. #endif
  615. }
  616. static void __init setup_memory(void)
  617. {
  618. struct memblock_region *reg;
  619. /*
  620. * Init storage key for present memory
  621. */
  622. for_each_memblock(memory, reg) {
  623. storage_key_init_range(reg->base, reg->base + reg->size);
  624. }
  625. psw_set_key(PAGE_DEFAULT_KEY);
  626. /* Only cosmetics */
  627. memblock_enforce_memory_limit(memblock_end_of_DRAM());
  628. }
  629. /*
  630. * Setup hardware capabilities.
  631. */
  632. static int __init setup_hwcaps(void)
  633. {
  634. static const int stfl_bits[6] = { 0, 2, 7, 17, 19, 21 };
  635. struct cpuid cpu_id;
  636. int i;
  637. /*
  638. * The store facility list bits numbers as found in the principles
  639. * of operation are numbered with bit 1UL<<31 as number 0 to
  640. * bit 1UL<<0 as number 31.
  641. * Bit 0: instructions named N3, "backported" to esa-mode
  642. * Bit 2: z/Architecture mode is active
  643. * Bit 7: the store-facility-list-extended facility is installed
  644. * Bit 17: the message-security assist is installed
  645. * Bit 19: the long-displacement facility is installed
  646. * Bit 21: the extended-immediate facility is installed
  647. * Bit 22: extended-translation facility 3 is installed
  648. * Bit 30: extended-translation facility 3 enhancement facility
  649. * These get translated to:
  650. * HWCAP_S390_ESAN3 bit 0, HWCAP_S390_ZARCH bit 1,
  651. * HWCAP_S390_STFLE bit 2, HWCAP_S390_MSA bit 3,
  652. * HWCAP_S390_LDISP bit 4, HWCAP_S390_EIMM bit 5 and
  653. * HWCAP_S390_ETF3EH bit 8 (22 && 30).
  654. */
  655. for (i = 0; i < 6; i++)
  656. if (test_facility(stfl_bits[i]))
  657. elf_hwcap |= 1UL << i;
  658. if (test_facility(22) && test_facility(30))
  659. elf_hwcap |= HWCAP_S390_ETF3EH;
  660. /*
  661. * Check for additional facilities with store-facility-list-extended.
  662. * stfle stores doublewords (8 byte) with bit 1ULL<<63 as bit 0
  663. * and 1ULL<<0 as bit 63. Bits 0-31 contain the same information
  664. * as stored by stfl, bits 32-xxx contain additional facilities.
  665. * How many facility words are stored depends on the number of
  666. * doublewords passed to the instruction. The additional facilities
  667. * are:
  668. * Bit 42: decimal floating point facility is installed
  669. * Bit 44: perform floating point operation facility is installed
  670. * translated to:
  671. * HWCAP_S390_DFP bit 6 (42 && 44).
  672. */
  673. if ((elf_hwcap & (1UL << 2)) && test_facility(42) && test_facility(44))
  674. elf_hwcap |= HWCAP_S390_DFP;
  675. /*
  676. * Huge page support HWCAP_S390_HPAGE is bit 7.
  677. */
  678. if (MACHINE_HAS_EDAT1)
  679. elf_hwcap |= HWCAP_S390_HPAGE;
  680. /*
  681. * 64-bit register support for 31-bit processes
  682. * HWCAP_S390_HIGH_GPRS is bit 9.
  683. */
  684. elf_hwcap |= HWCAP_S390_HIGH_GPRS;
  685. /*
  686. * Transactional execution support HWCAP_S390_TE is bit 10.
  687. */
  688. if (MACHINE_HAS_TE)
  689. elf_hwcap |= HWCAP_S390_TE;
  690. /*
  691. * Vector extension HWCAP_S390_VXRS is bit 11. The Vector extension
  692. * can be disabled with the "novx" parameter. Use MACHINE_HAS_VX
  693. * instead of facility bit 129.
  694. */
  695. if (MACHINE_HAS_VX) {
  696. elf_hwcap |= HWCAP_S390_VXRS;
  697. if (test_facility(134))
  698. elf_hwcap |= HWCAP_S390_VXRS_EXT;
  699. if (test_facility(135))
  700. elf_hwcap |= HWCAP_S390_VXRS_BCD;
  701. }
  702. /*
  703. * Guarded storage support HWCAP_S390_GS is bit 12.
  704. */
  705. if (MACHINE_HAS_GS)
  706. elf_hwcap |= HWCAP_S390_GS;
  707. get_cpu_id(&cpu_id);
  708. add_device_randomness(&cpu_id, sizeof(cpu_id));
  709. switch (cpu_id.machine) {
  710. case 0x2064:
  711. case 0x2066:
  712. default: /* Use "z900" as default for 64 bit kernels. */
  713. strcpy(elf_platform, "z900");
  714. break;
  715. case 0x2084:
  716. case 0x2086:
  717. strcpy(elf_platform, "z990");
  718. break;
  719. case 0x2094:
  720. case 0x2096:
  721. strcpy(elf_platform, "z9-109");
  722. break;
  723. case 0x2097:
  724. case 0x2098:
  725. strcpy(elf_platform, "z10");
  726. break;
  727. case 0x2817:
  728. case 0x2818:
  729. strcpy(elf_platform, "z196");
  730. break;
  731. case 0x2827:
  732. case 0x2828:
  733. strcpy(elf_platform, "zEC12");
  734. break;
  735. case 0x2964:
  736. case 0x2965:
  737. strcpy(elf_platform, "z13");
  738. break;
  739. case 0x3906:
  740. case 0x3907:
  741. strcpy(elf_platform, "z14");
  742. break;
  743. }
  744. /*
  745. * Virtualization support HWCAP_INT_SIE is bit 0.
  746. */
  747. if (sclp.has_sief2)
  748. int_hwcap |= HWCAP_INT_SIE;
  749. return 0;
  750. }
  751. arch_initcall(setup_hwcaps);
  752. /*
  753. * Add system information as device randomness
  754. */
  755. static void __init setup_randomness(void)
  756. {
  757. struct sysinfo_3_2_2 *vmms;
  758. vmms = (struct sysinfo_3_2_2 *) memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  759. if (stsi(vmms, 3, 2, 2) == 0 && vmms->count)
  760. add_device_randomness(&vmms->vm, sizeof(vmms->vm[0]) * vmms->count);
  761. memblock_free((unsigned long) vmms, PAGE_SIZE);
  762. }
  763. /*
  764. * Find the correct size for the task_struct. This depends on
  765. * the size of the struct fpu at the end of the thread_struct
  766. * which is embedded in the task_struct.
  767. */
  768. static void __init setup_task_size(void)
  769. {
  770. int task_size = sizeof(struct task_struct);
  771. if (!MACHINE_HAS_VX) {
  772. task_size -= sizeof(__vector128) * __NUM_VXRS;
  773. task_size += sizeof(freg_t) * __NUM_FPRS;
  774. }
  775. arch_task_struct_size = task_size;
  776. }
  777. /*
  778. * Setup function called from init/main.c just after the banner
  779. * was printed.
  780. */
  781. void __init setup_arch(char **cmdline_p)
  782. {
  783. /*
  784. * print what head.S has found out about the machine
  785. */
  786. if (MACHINE_IS_VM)
  787. pr_info("Linux is running as a z/VM "
  788. "guest operating system in 64-bit mode\n");
  789. else if (MACHINE_IS_KVM)
  790. pr_info("Linux is running under KVM in 64-bit mode\n");
  791. else if (MACHINE_IS_LPAR)
  792. pr_info("Linux is running natively in 64-bit mode\n");
  793. else
  794. pr_info("Linux is running as a guest in 64-bit mode\n");
  795. /* Have one command line that is parsed and saved in /proc/cmdline */
  796. /* boot_command_line has been already set up in early.c */
  797. *cmdline_p = boot_command_line;
  798. ROOT_DEV = Root_RAM0;
  799. /* Is init_mm really needed? */
  800. init_mm.start_code = PAGE_OFFSET;
  801. init_mm.end_code = (unsigned long) _etext;
  802. init_mm.end_data = (unsigned long) _edata;
  803. init_mm.brk = (unsigned long) _end;
  804. if (IS_ENABLED(CONFIG_EXPOLINE_AUTO))
  805. nospec_auto_detect();
  806. parse_early_param();
  807. #ifdef CONFIG_CRASH_DUMP
  808. /* Deactivate elfcorehdr= kernel parameter */
  809. elfcorehdr_addr = ELFCORE_ADDR_MAX;
  810. #endif
  811. os_info_init();
  812. setup_ipl();
  813. setup_task_size();
  814. /* Do some memory reservations *before* memory is added to memblock */
  815. reserve_memory_end();
  816. reserve_oldmem();
  817. reserve_kernel();
  818. reserve_initrd();
  819. memblock_allow_resize();
  820. /* Get information about *all* installed memory */
  821. detect_memory_memblock();
  822. remove_oldmem();
  823. /*
  824. * Make sure all chunks are MAX_ORDER aligned so we don't need the
  825. * extra checks that HOLES_IN_ZONE would require.
  826. *
  827. * Is this still required?
  828. */
  829. memblock_trim_memory(1UL << (MAX_ORDER - 1 + PAGE_SHIFT));
  830. setup_memory_end();
  831. setup_memory();
  832. dma_contiguous_reserve(memory_end);
  833. vmcp_cma_reserve();
  834. check_initrd();
  835. reserve_crashkernel();
  836. #ifdef CONFIG_CRASH_DUMP
  837. /*
  838. * Be aware that smp_save_dump_cpus() triggers a system reset.
  839. * Therefore CPU and device initialization should be done afterwards.
  840. */
  841. smp_save_dump_cpus();
  842. #endif
  843. setup_resources();
  844. setup_lowcore_dat_off();
  845. smp_fill_possible_mask();
  846. cpu_detect_mhz_feature();
  847. cpu_init();
  848. numa_setup();
  849. smp_detect_cpus();
  850. topology_init_early();
  851. /*
  852. * Create kernel page tables and switch to virtual addressing.
  853. */
  854. paging_init();
  855. /*
  856. * After paging_init created the kernel page table, the new PSWs
  857. * in lowcore can now run with DAT enabled.
  858. */
  859. setup_lowcore_dat_on();
  860. /* Setup default console */
  861. conmode_default();
  862. set_preferred_console();
  863. apply_alternative_instructions();
  864. if (IS_ENABLED(CONFIG_EXPOLINE))
  865. nospec_init_branches();
  866. /* Setup zfcpdump support */
  867. setup_zfcpdump();
  868. /* Add system specific data to the random pool */
  869. setup_randomness();
  870. }