setup.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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/root_dev.h>
  35. #include <linux/console.h>
  36. #include <linux/kernel_stat.h>
  37. #include <linux/dma-map-ops.h>
  38. #include <linux/device.h>
  39. #include <linux/notifier.h>
  40. #include <linux/pfn.h>
  41. #include <linux/ctype.h>
  42. #include <linux/reboot.h>
  43. #include <linux/topology.h>
  44. #include <linux/kexec.h>
  45. #include <linux/crash_dump.h>
  46. #include <linux/memory.h>
  47. #include <linux/compat.h>
  48. #include <linux/start_kernel.h>
  49. #include <linux/hugetlb.h>
  50. #include <linux/kmemleak.h>
  51. #include <asm/archrandom.h>
  52. #include <asm/boot_data.h>
  53. #include <asm/ipl.h>
  54. #include <asm/facility.h>
  55. #include <asm/smp.h>
  56. #include <asm/mmu_context.h>
  57. #include <asm/cpcmd.h>
  58. #include <asm/abs_lowcore.h>
  59. #include <asm/nmi.h>
  60. #include <asm/irq.h>
  61. #include <asm/page.h>
  62. #include <asm/ptrace.h>
  63. #include <asm/sections.h>
  64. #include <asm/ebcdic.h>
  65. #include <asm/diag.h>
  66. #include <asm/os_info.h>
  67. #include <asm/sclp.h>
  68. #include <asm/stacktrace.h>
  69. #include <asm/sysinfo.h>
  70. #include <asm/numa.h>
  71. #include <asm/alternative.h>
  72. #include <asm/nospec-branch.h>
  73. #include <asm/physmem_info.h>
  74. #include <asm/maccess.h>
  75. #include <asm/uv.h>
  76. #include <asm/asm-offsets.h>
  77. #include "entry.h"
  78. /*
  79. * Machine setup..
  80. */
  81. unsigned int console_mode = 0;
  82. EXPORT_SYMBOL(console_mode);
  83. unsigned int console_devno = -1;
  84. EXPORT_SYMBOL(console_devno);
  85. unsigned int console_irq = -1;
  86. EXPORT_SYMBOL(console_irq);
  87. /*
  88. * Some code and data needs to stay below 2 GB, even when the kernel would be
  89. * relocated above 2 GB, because it has to use 31 bit addresses.
  90. * Such code and data is part of the .amode31 section.
  91. */
  92. char __amode31_ref *__samode31 = _samode31;
  93. char __amode31_ref *__eamode31 = _eamode31;
  94. char __amode31_ref *__stext_amode31 = _stext_amode31;
  95. char __amode31_ref *__etext_amode31 = _etext_amode31;
  96. struct exception_table_entry __amode31_ref *__start_amode31_ex_table = _start_amode31_ex_table;
  97. struct exception_table_entry __amode31_ref *__stop_amode31_ex_table = _stop_amode31_ex_table;
  98. /*
  99. * Control registers CR2, CR5 and CR15 are initialized with addresses
  100. * of tables that must be placed below 2G which is handled by the AMODE31
  101. * sections.
  102. * Because the AMODE31 sections are relocated below 2G at startup,
  103. * the content of control registers CR2, CR5 and CR15 must be updated
  104. * with new addresses after the relocation. The initial initialization of
  105. * control registers occurs in head64.S and then gets updated again after AMODE31
  106. * relocation. We must access the relevant AMODE31 tables indirectly via
  107. * pointers placed in the .amode31.refs linker section. Those pointers get
  108. * updated automatically during AMODE31 relocation and always contain a valid
  109. * address within AMODE31 sections.
  110. */
  111. static __amode31_data u32 __ctl_duct_amode31[16] __aligned(64);
  112. static __amode31_data u64 __ctl_aste_amode31[8] __aligned(64) = {
  113. [1] = 0xffffffffffffffff
  114. };
  115. static __amode31_data u32 __ctl_duald_amode31[32] __aligned(128) = {
  116. 0x80000000, 0, 0, 0,
  117. 0x80000000, 0, 0, 0,
  118. 0x80000000, 0, 0, 0,
  119. 0x80000000, 0, 0, 0,
  120. 0x80000000, 0, 0, 0,
  121. 0x80000000, 0, 0, 0,
  122. 0x80000000, 0, 0, 0,
  123. 0x80000000, 0, 0, 0
  124. };
  125. static __amode31_data u32 __ctl_linkage_stack_amode31[8] __aligned(64) = {
  126. 0, 0, 0x89000000, 0,
  127. 0, 0, 0x8a000000, 0
  128. };
  129. static u64 __amode31_ref *__ctl_aste = __ctl_aste_amode31;
  130. static u32 __amode31_ref *__ctl_duald = __ctl_duald_amode31;
  131. static u32 __amode31_ref *__ctl_linkage_stack = __ctl_linkage_stack_amode31;
  132. static u32 __amode31_ref *__ctl_duct = __ctl_duct_amode31;
  133. unsigned long __bootdata_preserved(max_mappable);
  134. struct physmem_info __bootdata(physmem_info);
  135. struct vm_layout __bootdata_preserved(vm_layout);
  136. EXPORT_SYMBOL(vm_layout);
  137. int __bootdata_preserved(__kaslr_enabled);
  138. unsigned int __bootdata_preserved(zlib_dfltcc_support);
  139. EXPORT_SYMBOL(zlib_dfltcc_support);
  140. u64 __bootdata_preserved(stfle_fac_list[16]);
  141. EXPORT_SYMBOL(stfle_fac_list);
  142. struct oldmem_data __bootdata_preserved(oldmem_data);
  143. unsigned long VMALLOC_START;
  144. EXPORT_SYMBOL(VMALLOC_START);
  145. unsigned long VMALLOC_END;
  146. EXPORT_SYMBOL(VMALLOC_END);
  147. struct page *vmemmap;
  148. EXPORT_SYMBOL(vmemmap);
  149. unsigned long vmemmap_size;
  150. unsigned long MODULES_VADDR;
  151. unsigned long MODULES_END;
  152. /* An array with a pointer to the lowcore of every CPU. */
  153. struct lowcore *lowcore_ptr[NR_CPUS];
  154. EXPORT_SYMBOL(lowcore_ptr);
  155. DEFINE_STATIC_KEY_FALSE(cpu_has_bear);
  156. /*
  157. * The Write Back bit position in the physaddr is given by the SLPC PCI.
  158. * Leaving the mask zero always uses write through which is safe
  159. */
  160. unsigned long mio_wb_bit_mask __ro_after_init;
  161. /*
  162. * This is set up by the setup-routine at boot-time
  163. * for S390 need to find out, what we have to setup
  164. * using address 0x10400 ...
  165. */
  166. #include <asm/setup.h>
  167. /*
  168. * condev= and conmode= setup parameter.
  169. */
  170. static int __init condev_setup(char *str)
  171. {
  172. int vdev;
  173. vdev = simple_strtoul(str, &str, 0);
  174. if (vdev >= 0 && vdev < 65536) {
  175. console_devno = vdev;
  176. console_irq = -1;
  177. }
  178. return 1;
  179. }
  180. __setup("condev=", condev_setup);
  181. static void __init set_preferred_console(void)
  182. {
  183. if (CONSOLE_IS_3215 || CONSOLE_IS_SCLP)
  184. add_preferred_console("ttyS", 0, NULL);
  185. else if (CONSOLE_IS_3270)
  186. add_preferred_console("tty3270", 0, NULL);
  187. else if (CONSOLE_IS_VT220)
  188. add_preferred_console("ttysclp", 0, NULL);
  189. else if (CONSOLE_IS_HVC)
  190. add_preferred_console("hvc", 0, NULL);
  191. }
  192. static int __init conmode_setup(char *str)
  193. {
  194. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  195. if (!strcmp(str, "hwc") || !strcmp(str, "sclp"))
  196. SET_CONSOLE_SCLP;
  197. #endif
  198. #if defined(CONFIG_TN3215_CONSOLE)
  199. if (!strcmp(str, "3215"))
  200. SET_CONSOLE_3215;
  201. #endif
  202. #if defined(CONFIG_TN3270_CONSOLE)
  203. if (!strcmp(str, "3270"))
  204. SET_CONSOLE_3270;
  205. #endif
  206. set_preferred_console();
  207. return 1;
  208. }
  209. __setup("conmode=", conmode_setup);
  210. static void __init conmode_default(void)
  211. {
  212. char query_buffer[1024];
  213. char *ptr;
  214. if (MACHINE_IS_VM) {
  215. cpcmd("QUERY CONSOLE", query_buffer, 1024, NULL);
  216. console_devno = simple_strtoul(query_buffer + 5, NULL, 16);
  217. ptr = strstr(query_buffer, "SUBCHANNEL =");
  218. console_irq = simple_strtoul(ptr + 13, NULL, 16);
  219. cpcmd("QUERY TERM", query_buffer, 1024, NULL);
  220. ptr = strstr(query_buffer, "CONMODE");
  221. /*
  222. * Set the conmode to 3215 so that the device recognition
  223. * will set the cu_type of the console to 3215. If the
  224. * conmode is 3270 and we don't set it back then both
  225. * 3215 and the 3270 driver will try to access the console
  226. * device (3215 as console and 3270 as normal tty).
  227. */
  228. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  229. if (ptr == NULL) {
  230. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  231. SET_CONSOLE_SCLP;
  232. #endif
  233. return;
  234. }
  235. if (str_has_prefix(ptr + 8, "3270")) {
  236. #if defined(CONFIG_TN3270_CONSOLE)
  237. SET_CONSOLE_3270;
  238. #elif defined(CONFIG_TN3215_CONSOLE)
  239. SET_CONSOLE_3215;
  240. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  241. SET_CONSOLE_SCLP;
  242. #endif
  243. } else if (str_has_prefix(ptr + 8, "3215")) {
  244. #if defined(CONFIG_TN3215_CONSOLE)
  245. SET_CONSOLE_3215;
  246. #elif defined(CONFIG_TN3270_CONSOLE)
  247. SET_CONSOLE_3270;
  248. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  249. SET_CONSOLE_SCLP;
  250. #endif
  251. }
  252. } else if (MACHINE_IS_KVM) {
  253. if (sclp.has_vt220 && IS_ENABLED(CONFIG_SCLP_VT220_CONSOLE))
  254. SET_CONSOLE_VT220;
  255. else if (sclp.has_linemode && IS_ENABLED(CONFIG_SCLP_CONSOLE))
  256. SET_CONSOLE_SCLP;
  257. else
  258. SET_CONSOLE_HVC;
  259. } else {
  260. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  261. SET_CONSOLE_SCLP;
  262. #endif
  263. }
  264. }
  265. #ifdef CONFIG_CRASH_DUMP
  266. static void __init setup_zfcpdump(void)
  267. {
  268. if (!is_ipl_type_dump())
  269. return;
  270. if (oldmem_data.start)
  271. return;
  272. strlcat(boot_command_line, " cio_ignore=all,!ipldev,!condev", COMMAND_LINE_SIZE);
  273. console_loglevel = 2;
  274. }
  275. #else
  276. static inline void setup_zfcpdump(void) {}
  277. #endif /* CONFIG_CRASH_DUMP */
  278. /*
  279. * Reboot, halt and power_off stubs. They just call _machine_restart,
  280. * _machine_halt or _machine_power_off.
  281. */
  282. void machine_restart(char *command)
  283. {
  284. if ((!in_interrupt() && !in_atomic()) || oops_in_progress)
  285. /*
  286. * Only unblank the console if we are called in enabled
  287. * context or a bust_spinlocks cleared the way for us.
  288. */
  289. console_unblank();
  290. _machine_restart(command);
  291. }
  292. void machine_halt(void)
  293. {
  294. if (!in_interrupt() || oops_in_progress)
  295. /*
  296. * Only unblank the console if we are called in enabled
  297. * context or a bust_spinlocks cleared the way for us.
  298. */
  299. console_unblank();
  300. _machine_halt();
  301. }
  302. void machine_power_off(void)
  303. {
  304. if (!in_interrupt() || oops_in_progress)
  305. /*
  306. * Only unblank the console if we are called in enabled
  307. * context or a bust_spinlocks cleared the way for us.
  308. */
  309. console_unblank();
  310. _machine_power_off();
  311. }
  312. /*
  313. * Dummy power off function.
  314. */
  315. void (*pm_power_off)(void) = machine_power_off;
  316. EXPORT_SYMBOL_GPL(pm_power_off);
  317. void *restart_stack;
  318. unsigned long stack_alloc(void)
  319. {
  320. #ifdef CONFIG_VMAP_STACK
  321. void *ret;
  322. ret = __vmalloc_node(THREAD_SIZE, THREAD_SIZE, THREADINFO_GFP,
  323. NUMA_NO_NODE, __builtin_return_address(0));
  324. kmemleak_not_leak(ret);
  325. return (unsigned long)ret;
  326. #else
  327. return __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
  328. #endif
  329. }
  330. void stack_free(unsigned long stack)
  331. {
  332. #ifdef CONFIG_VMAP_STACK
  333. vfree((void *) stack);
  334. #else
  335. free_pages(stack, THREAD_SIZE_ORDER);
  336. #endif
  337. }
  338. static unsigned long __init stack_alloc_early(void)
  339. {
  340. unsigned long stack;
  341. stack = (unsigned long)memblock_alloc(THREAD_SIZE, THREAD_SIZE);
  342. if (!stack) {
  343. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  344. __func__, THREAD_SIZE, THREAD_SIZE);
  345. }
  346. return stack;
  347. }
  348. static void __init setup_lowcore(void)
  349. {
  350. struct lowcore *lc, *abs_lc;
  351. /*
  352. * Setup lowcore for boot cpu
  353. */
  354. BUILD_BUG_ON(sizeof(struct lowcore) != LC_PAGES * PAGE_SIZE);
  355. lc = memblock_alloc_low(sizeof(*lc), sizeof(*lc));
  356. if (!lc)
  357. panic("%s: Failed to allocate %zu bytes align=%zx\n",
  358. __func__, sizeof(*lc), sizeof(*lc));
  359. lc->pcpu = (unsigned long)per_cpu_ptr(&pcpu_devices, 0);
  360. lc->restart_psw.mask = PSW_KERNEL_BITS & ~PSW_MASK_DAT;
  361. lc->restart_psw.addr = __pa(restart_int_handler);
  362. lc->external_new_psw.mask = PSW_KERNEL_BITS;
  363. lc->external_new_psw.addr = (unsigned long) ext_int_handler;
  364. lc->svc_new_psw.mask = PSW_KERNEL_BITS;
  365. lc->svc_new_psw.addr = (unsigned long) system_call;
  366. lc->program_new_psw.mask = PSW_KERNEL_BITS;
  367. lc->program_new_psw.addr = (unsigned long) pgm_check_handler;
  368. lc->mcck_new_psw.mask = PSW_KERNEL_BITS;
  369. lc->mcck_new_psw.addr = (unsigned long) mcck_int_handler;
  370. lc->io_new_psw.mask = PSW_KERNEL_BITS;
  371. lc->io_new_psw.addr = (unsigned long) io_int_handler;
  372. lc->clock_comparator = clock_comparator_max;
  373. lc->current_task = (unsigned long)&init_task;
  374. lc->lpp = LPP_MAGIC;
  375. lc->machine_flags = get_lowcore()->machine_flags;
  376. lc->preempt_count = get_lowcore()->preempt_count;
  377. nmi_alloc_mcesa_early(&lc->mcesad);
  378. lc->sys_enter_timer = get_lowcore()->sys_enter_timer;
  379. lc->exit_timer = get_lowcore()->exit_timer;
  380. lc->user_timer = get_lowcore()->user_timer;
  381. lc->system_timer = get_lowcore()->system_timer;
  382. lc->steal_timer = get_lowcore()->steal_timer;
  383. lc->last_update_timer = get_lowcore()->last_update_timer;
  384. lc->last_update_clock = get_lowcore()->last_update_clock;
  385. /*
  386. * Allocate the global restart stack which is the same for
  387. * all CPUs in case *one* of them does a PSW restart.
  388. */
  389. restart_stack = (void *)(stack_alloc_early() + STACK_INIT_OFFSET);
  390. lc->mcck_stack = stack_alloc_early() + STACK_INIT_OFFSET;
  391. lc->async_stack = stack_alloc_early() + STACK_INIT_OFFSET;
  392. lc->nodat_stack = stack_alloc_early() + STACK_INIT_OFFSET;
  393. lc->kernel_stack = get_lowcore()->kernel_stack;
  394. /*
  395. * Set up PSW restart to call ipl.c:do_restart(). Copy the relevant
  396. * restart data to the absolute zero lowcore. This is necessary if
  397. * PSW restart is done on an offline CPU that has lowcore zero.
  398. */
  399. lc->restart_stack = (unsigned long) restart_stack;
  400. lc->restart_fn = (unsigned long) do_restart;
  401. lc->restart_data = 0;
  402. lc->restart_source = -1U;
  403. lc->spinlock_lockval = arch_spin_lockval(0);
  404. lc->spinlock_index = 0;
  405. arch_spin_lock_setup(0);
  406. lc->return_lpswe = gen_lpswe(__LC_RETURN_PSW);
  407. lc->return_mcck_lpswe = gen_lpswe(__LC_RETURN_MCCK_PSW);
  408. lc->preempt_count = PREEMPT_DISABLED;
  409. lc->kernel_asce = get_lowcore()->kernel_asce;
  410. lc->user_asce = get_lowcore()->user_asce;
  411. system_ctlreg_init_save_area(lc);
  412. abs_lc = get_abs_lowcore();
  413. abs_lc->restart_stack = lc->restart_stack;
  414. abs_lc->restart_fn = lc->restart_fn;
  415. abs_lc->restart_data = lc->restart_data;
  416. abs_lc->restart_source = lc->restart_source;
  417. abs_lc->restart_psw = lc->restart_psw;
  418. abs_lc->restart_flags = RESTART_FLAG_CTLREGS;
  419. abs_lc->program_new_psw = lc->program_new_psw;
  420. abs_lc->mcesad = lc->mcesad;
  421. put_abs_lowcore(abs_lc);
  422. set_prefix(__pa(lc));
  423. lowcore_ptr[0] = lc;
  424. if (abs_lowcore_map(0, lowcore_ptr[0], false))
  425. panic("Couldn't setup absolute lowcore");
  426. }
  427. static struct resource code_resource = {
  428. .name = "Kernel code",
  429. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  430. };
  431. static struct resource data_resource = {
  432. .name = "Kernel data",
  433. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  434. };
  435. static struct resource bss_resource = {
  436. .name = "Kernel bss",
  437. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  438. };
  439. static struct resource __initdata *standard_resources[] = {
  440. &code_resource,
  441. &data_resource,
  442. &bss_resource,
  443. };
  444. static void __init setup_resources(void)
  445. {
  446. struct resource *res, *std_res, *sub_res;
  447. phys_addr_t start, end;
  448. int j;
  449. u64 i;
  450. code_resource.start = __pa_symbol(_text);
  451. code_resource.end = __pa_symbol(_etext) - 1;
  452. data_resource.start = __pa_symbol(_etext);
  453. data_resource.end = __pa_symbol(_edata) - 1;
  454. bss_resource.start = __pa_symbol(__bss_start);
  455. bss_resource.end = __pa_symbol(__bss_stop) - 1;
  456. for_each_mem_range(i, &start, &end) {
  457. res = memblock_alloc(sizeof(*res), 8);
  458. if (!res)
  459. panic("%s: Failed to allocate %zu bytes align=0x%x\n",
  460. __func__, sizeof(*res), 8);
  461. res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  462. res->name = "System RAM";
  463. res->start = start;
  464. /*
  465. * In memblock, end points to the first byte after the
  466. * range while in resources, end points to the last byte in
  467. * the range.
  468. */
  469. res->end = end - 1;
  470. request_resource(&iomem_resource, res);
  471. for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
  472. std_res = standard_resources[j];
  473. if (std_res->start < res->start ||
  474. std_res->start > res->end)
  475. continue;
  476. if (std_res->end > res->end) {
  477. sub_res = memblock_alloc(sizeof(*sub_res), 8);
  478. if (!sub_res)
  479. panic("%s: Failed to allocate %zu bytes align=0x%x\n",
  480. __func__, sizeof(*sub_res), 8);
  481. *sub_res = *std_res;
  482. sub_res->end = res->end;
  483. std_res->start = res->end + 1;
  484. request_resource(res, sub_res);
  485. } else {
  486. request_resource(res, std_res);
  487. }
  488. }
  489. }
  490. #ifdef CONFIG_CRASH_DUMP
  491. /*
  492. * Re-add removed crash kernel memory as reserved memory. This makes
  493. * sure it will be mapped with the identity mapping and struct pages
  494. * will be created, so it can be resized later on.
  495. * However add it later since the crash kernel resource should not be
  496. * part of the System RAM resource.
  497. */
  498. if (crashk_res.end) {
  499. memblock_add_node(crashk_res.start, resource_size(&crashk_res),
  500. 0, MEMBLOCK_NONE);
  501. memblock_reserve(crashk_res.start, resource_size(&crashk_res));
  502. insert_resource(&iomem_resource, &crashk_res);
  503. }
  504. #endif
  505. }
  506. static void __init setup_memory_end(void)
  507. {
  508. max_pfn = max_low_pfn = PFN_DOWN(ident_map_size);
  509. pr_notice("The maximum memory size is %luMB\n", ident_map_size >> 20);
  510. }
  511. #ifdef CONFIG_CRASH_DUMP
  512. /*
  513. * When kdump is enabled, we have to ensure that no memory from the area
  514. * [0 - crashkernel memory size] is set offline - it will be exchanged with
  515. * the crashkernel memory region when kdump is triggered. The crashkernel
  516. * memory region can never get offlined (pages are unmovable).
  517. */
  518. static int kdump_mem_notifier(struct notifier_block *nb,
  519. unsigned long action, void *data)
  520. {
  521. struct memory_notify *arg = data;
  522. if (action != MEM_GOING_OFFLINE)
  523. return NOTIFY_OK;
  524. if (arg->start_pfn < PFN_DOWN(resource_size(&crashk_res)))
  525. return NOTIFY_BAD;
  526. return NOTIFY_OK;
  527. }
  528. static struct notifier_block kdump_mem_nb = {
  529. .notifier_call = kdump_mem_notifier,
  530. };
  531. #endif
  532. /*
  533. * Reserve page tables created by decompressor
  534. */
  535. static void __init reserve_pgtables(void)
  536. {
  537. unsigned long start, end;
  538. struct reserved_range *range;
  539. for_each_physmem_reserved_type_range(RR_VMEM, range, &start, &end)
  540. memblock_reserve(start, end - start);
  541. }
  542. /*
  543. * Reserve memory for kdump kernel to be loaded with kexec
  544. */
  545. static void __init reserve_crashkernel(void)
  546. {
  547. #ifdef CONFIG_CRASH_DUMP
  548. unsigned long long crash_base, crash_size;
  549. phys_addr_t low, high;
  550. int rc;
  551. rc = parse_crashkernel(boot_command_line, ident_map_size,
  552. &crash_size, &crash_base, NULL, NULL);
  553. crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
  554. crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
  555. if (rc || crash_size == 0)
  556. return;
  557. if (memblock.memory.regions[0].size < crash_size) {
  558. pr_info("crashkernel reservation failed: %s\n",
  559. "first memory chunk must be at least crashkernel size");
  560. return;
  561. }
  562. low = crash_base ?: oldmem_data.start;
  563. high = low + crash_size;
  564. if (low >= oldmem_data.start && high <= oldmem_data.start + oldmem_data.size) {
  565. /* The crashkernel fits into OLDMEM, reuse OLDMEM */
  566. crash_base = low;
  567. } else {
  568. /* Find suitable area in free memory */
  569. low = max_t(unsigned long, crash_size, sclp.hsa_size);
  570. high = crash_base ? crash_base + crash_size : ULONG_MAX;
  571. if (crash_base && crash_base < low) {
  572. pr_info("crashkernel reservation failed: %s\n",
  573. "crash_base too low");
  574. return;
  575. }
  576. low = crash_base ?: low;
  577. crash_base = memblock_phys_alloc_range(crash_size,
  578. KEXEC_CRASH_MEM_ALIGN,
  579. low, high);
  580. }
  581. if (!crash_base) {
  582. pr_info("crashkernel reservation failed: %s\n",
  583. "no suitable area found");
  584. return;
  585. }
  586. if (register_memory_notifier(&kdump_mem_nb)) {
  587. memblock_phys_free(crash_base, crash_size);
  588. return;
  589. }
  590. if (!oldmem_data.start && MACHINE_IS_VM)
  591. diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));
  592. crashk_res.start = crash_base;
  593. crashk_res.end = crash_base + crash_size - 1;
  594. memblock_remove(crash_base, crash_size);
  595. pr_info("Reserving %lluMB of memory at %lluMB "
  596. "for crashkernel (System RAM: %luMB)\n",
  597. crash_size >> 20, crash_base >> 20,
  598. (unsigned long)memblock.memory.total_size >> 20);
  599. os_info_crashkernel_add(crash_base, crash_size);
  600. #endif
  601. }
  602. /*
  603. * Reserve the initrd from being used by memblock
  604. */
  605. static void __init reserve_initrd(void)
  606. {
  607. unsigned long addr, size;
  608. if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || !get_physmem_reserved(RR_INITRD, &addr, &size))
  609. return;
  610. initrd_start = (unsigned long)__va(addr);
  611. initrd_end = initrd_start + size;
  612. memblock_reserve(addr, size);
  613. }
  614. /*
  615. * Reserve the memory area used to pass the certificate lists
  616. */
  617. static void __init reserve_certificate_list(void)
  618. {
  619. if (ipl_cert_list_addr)
  620. memblock_reserve(ipl_cert_list_addr, ipl_cert_list_size);
  621. }
  622. static void __init reserve_physmem_info(void)
  623. {
  624. unsigned long addr, size;
  625. if (get_physmem_reserved(RR_MEM_DETECT_EXTENDED, &addr, &size))
  626. memblock_reserve(addr, size);
  627. }
  628. static void __init free_physmem_info(void)
  629. {
  630. unsigned long addr, size;
  631. if (get_physmem_reserved(RR_MEM_DETECT_EXTENDED, &addr, &size))
  632. memblock_phys_free(addr, size);
  633. }
  634. static void __init memblock_add_physmem_info(void)
  635. {
  636. unsigned long start, end;
  637. int i;
  638. pr_debug("physmem info source: %s (%hhd)\n",
  639. get_physmem_info_source(), physmem_info.info_source);
  640. /* keep memblock lists close to the kernel */
  641. memblock_set_bottom_up(true);
  642. for_each_physmem_usable_range(i, &start, &end)
  643. memblock_add(start, end - start);
  644. for_each_physmem_online_range(i, &start, &end)
  645. memblock_physmem_add(start, end - start);
  646. memblock_set_bottom_up(false);
  647. memblock_set_node(0, ULONG_MAX, &memblock.memory, 0);
  648. }
  649. /*
  650. * Reserve memory used for lowcore.
  651. */
  652. static void __init reserve_lowcore(void)
  653. {
  654. void *lowcore_start = get_lowcore();
  655. void *lowcore_end = lowcore_start + sizeof(struct lowcore);
  656. void *start, *end;
  657. if ((void *)__identity_base < lowcore_end) {
  658. start = max(lowcore_start, (void *)__identity_base);
  659. end = min(lowcore_end, (void *)(__identity_base + ident_map_size));
  660. memblock_reserve(__pa(start), __pa(end));
  661. }
  662. }
  663. /*
  664. * Reserve memory used for absolute lowcore/command line/kernel image.
  665. */
  666. static void __init reserve_kernel(void)
  667. {
  668. memblock_reserve(0, STARTUP_NORMAL_OFFSET);
  669. memblock_reserve(OLDMEM_BASE, sizeof(unsigned long));
  670. memblock_reserve(OLDMEM_SIZE, sizeof(unsigned long));
  671. memblock_reserve(physmem_info.reserved[RR_AMODE31].start, __eamode31 - __samode31);
  672. memblock_reserve(__pa(sclp_early_sccb), EXT_SCCB_READ_SCP);
  673. memblock_reserve(__pa(_stext), _end - _stext);
  674. }
  675. static void __init setup_memory(void)
  676. {
  677. phys_addr_t start, end;
  678. u64 i;
  679. /*
  680. * Init storage key for present memory
  681. */
  682. for_each_mem_range(i, &start, &end)
  683. storage_key_init_range(start, end);
  684. psw_set_key(PAGE_DEFAULT_KEY);
  685. }
  686. static void __init relocate_amode31_section(void)
  687. {
  688. unsigned long amode31_size = __eamode31 - __samode31;
  689. long amode31_offset, *ptr;
  690. amode31_offset = AMODE31_START - (unsigned long)__samode31;
  691. pr_info("Relocating AMODE31 section of size 0x%08lx\n", amode31_size);
  692. /* Move original AMODE31 section to the new one */
  693. memmove((void *)physmem_info.reserved[RR_AMODE31].start, __samode31, amode31_size);
  694. /* Zero out the old AMODE31 section to catch invalid accesses within it */
  695. memset(__samode31, 0, amode31_size);
  696. /* Update all AMODE31 region references */
  697. for (ptr = _start_amode31_refs; ptr != _end_amode31_refs; ptr++)
  698. *ptr += amode31_offset;
  699. }
  700. /* This must be called after AMODE31 relocation */
  701. static void __init setup_cr(void)
  702. {
  703. union ctlreg2 cr2;
  704. union ctlreg5 cr5;
  705. union ctlreg15 cr15;
  706. __ctl_duct[1] = (unsigned long)__ctl_aste;
  707. __ctl_duct[2] = (unsigned long)__ctl_aste;
  708. __ctl_duct[4] = (unsigned long)__ctl_duald;
  709. /* Update control registers CR2, CR5 and CR15 */
  710. local_ctl_store(2, &cr2.reg);
  711. local_ctl_store(5, &cr5.reg);
  712. local_ctl_store(15, &cr15.reg);
  713. cr2.ducto = (unsigned long)__ctl_duct >> 6;
  714. cr5.pasteo = (unsigned long)__ctl_duct >> 6;
  715. cr15.lsea = (unsigned long)__ctl_linkage_stack >> 3;
  716. system_ctl_load(2, &cr2.reg);
  717. system_ctl_load(5, &cr5.reg);
  718. system_ctl_load(15, &cr15.reg);
  719. }
  720. /*
  721. * Add system information as device randomness
  722. */
  723. static void __init setup_randomness(void)
  724. {
  725. struct sysinfo_3_2_2 *vmms;
  726. vmms = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  727. if (!vmms)
  728. panic("Failed to allocate memory for sysinfo structure\n");
  729. if (stsi(vmms, 3, 2, 2) == 0 && vmms->count)
  730. add_device_randomness(&vmms->vm, sizeof(vmms->vm[0]) * vmms->count);
  731. memblock_free(vmms, PAGE_SIZE);
  732. if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
  733. static_branch_enable(&s390_arch_random_available);
  734. }
  735. /*
  736. * Issue diagnose 318 to set the control program name and
  737. * version codes.
  738. */
  739. static void __init setup_control_program_code(void)
  740. {
  741. union diag318_info diag318_info = {
  742. .cpnc = CPNC_LINUX,
  743. .cpvc = 0,
  744. };
  745. if (!sclp.has_diag318)
  746. return;
  747. diag_stat_inc(DIAG_STAT_X318);
  748. asm volatile("diag %0,0,0x318\n" : : "d" (diag318_info.val));
  749. }
  750. /*
  751. * Print the component list from the IPL report
  752. */
  753. static void __init log_component_list(void)
  754. {
  755. struct ipl_rb_component_entry *ptr, *end;
  756. char *str;
  757. if (!early_ipl_comp_list_addr)
  758. return;
  759. if (ipl_block.hdr.flags & IPL_PL_FLAG_SIPL)
  760. pr_info("Linux is running with Secure-IPL enabled\n");
  761. else
  762. pr_info("Linux is running with Secure-IPL disabled\n");
  763. ptr = __va(early_ipl_comp_list_addr);
  764. end = (void *) ptr + early_ipl_comp_list_size;
  765. pr_info("The IPL report contains the following components:\n");
  766. while (ptr < end) {
  767. if (ptr->flags & IPL_RB_COMPONENT_FLAG_SIGNED) {
  768. if (ptr->flags & IPL_RB_COMPONENT_FLAG_VERIFIED)
  769. str = "signed, verified";
  770. else
  771. str = "signed, verification failed";
  772. } else {
  773. str = "not signed";
  774. }
  775. pr_info("%016llx - %016llx (%s)\n",
  776. ptr->addr, ptr->addr + ptr->len, str);
  777. ptr++;
  778. }
  779. }
  780. /*
  781. * Setup function called from init/main.c just after the banner
  782. * was printed.
  783. */
  784. void __init setup_arch(char **cmdline_p)
  785. {
  786. /*
  787. * print what head.S has found out about the machine
  788. */
  789. if (MACHINE_IS_VM)
  790. pr_info("Linux is running as a z/VM "
  791. "guest operating system in 64-bit mode\n");
  792. else if (MACHINE_IS_KVM)
  793. pr_info("Linux is running under KVM in 64-bit mode\n");
  794. else if (MACHINE_IS_LPAR)
  795. pr_info("Linux is running natively in 64-bit mode\n");
  796. else
  797. pr_info("Linux is running as a guest in 64-bit mode\n");
  798. if (have_relocated_lowcore())
  799. pr_info("Lowcore relocated to 0x%px\n", get_lowcore());
  800. log_component_list();
  801. /* Have one command line that is parsed and saved in /proc/cmdline */
  802. /* boot_command_line has been already set up in early.c */
  803. *cmdline_p = boot_command_line;
  804. ROOT_DEV = Root_RAM0;
  805. setup_initial_init_mm(_text, _etext, _edata, _end);
  806. if (IS_ENABLED(CONFIG_EXPOLINE_AUTO))
  807. nospec_auto_detect();
  808. jump_label_init();
  809. parse_early_param();
  810. #ifdef CONFIG_CRASH_DUMP
  811. /* Deactivate elfcorehdr= kernel parameter */
  812. elfcorehdr_addr = ELFCORE_ADDR_MAX;
  813. #endif
  814. os_info_init();
  815. setup_ipl();
  816. setup_control_program_code();
  817. /* Do some memory reservations *before* memory is added to memblock */
  818. reserve_pgtables();
  819. reserve_lowcore();
  820. reserve_kernel();
  821. reserve_initrd();
  822. reserve_certificate_list();
  823. reserve_physmem_info();
  824. memblock_set_current_limit(ident_map_size);
  825. memblock_allow_resize();
  826. /* Get information about *all* installed memory */
  827. memblock_add_physmem_info();
  828. free_physmem_info();
  829. setup_memory_end();
  830. memblock_dump_all();
  831. setup_memory();
  832. relocate_amode31_section();
  833. setup_cr();
  834. setup_uv();
  835. dma_contiguous_reserve(ident_map_size);
  836. vmcp_cma_reserve();
  837. if (MACHINE_HAS_EDAT2)
  838. hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
  839. reserve_crashkernel();
  840. #ifdef CONFIG_CRASH_DUMP
  841. /*
  842. * Be aware that smp_save_dump_secondary_cpus() triggers a system reset.
  843. * Therefore CPU and device initialization should be done afterwards.
  844. */
  845. smp_save_dump_secondary_cpus();
  846. #endif
  847. setup_resources();
  848. setup_lowcore();
  849. smp_fill_possible_mask();
  850. cpu_detect_mhz_feature();
  851. cpu_init();
  852. numa_setup();
  853. smp_detect_cpus();
  854. topology_init_early();
  855. if (test_facility(193))
  856. static_branch_enable(&cpu_has_bear);
  857. /*
  858. * Create kernel page tables.
  859. */
  860. paging_init();
  861. /*
  862. * After paging_init created the kernel page table, the new PSWs
  863. * in lowcore can now run with DAT enabled.
  864. */
  865. #ifdef CONFIG_CRASH_DUMP
  866. smp_save_dump_ipl_cpu();
  867. #endif
  868. /* Setup default console */
  869. conmode_default();
  870. set_preferred_console();
  871. apply_alternative_instructions();
  872. if (IS_ENABLED(CONFIG_EXPOLINE))
  873. nospec_init_branches();
  874. /* Setup zfcp/nvme dump support */
  875. setup_zfcpdump();
  876. /* Add system specific data to the random pool */
  877. setup_randomness();
  878. }
  879. void __init arch_cpu_finalize_init(void)
  880. {
  881. sclp_init();
  882. }