setup.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999, 2012
  4. * Author(s): Hartmut Penner (hp@de.ibm.com),
  5. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  6. *
  7. * Derived from "arch/i386/kernel/setup.c"
  8. * Copyright (C) 1995, Linus Torvalds
  9. */
  10. /*
  11. * This file handles the architecture-dependent parts of initialization
  12. */
  13. #define KMSG_COMPONENT "setup"
  14. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  15. #include <linux/errno.h>
  16. #include <linux/export.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/memblock.h>
  20. #include <linux/mm.h>
  21. #include <linux/stddef.h>
  22. #include <linux/unistd.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/user.h>
  25. #include <linux/tty.h>
  26. #include <linux/ioport.h>
  27. #include <linux/delay.h>
  28. #include <linux/init.h>
  29. #include <linux/initrd.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/root_dev.h>
  32. #include <linux/console.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/device.h>
  35. #include <linux/notifier.h>
  36. #include <linux/pfn.h>
  37. #include <linux/ctype.h>
  38. #include <linux/reboot.h>
  39. #include <linux/topology.h>
  40. #include <linux/ftrace.h>
  41. #include <linux/kexec.h>
  42. #include <linux/crash_dump.h>
  43. #include <linux/memory.h>
  44. #include <linux/compat.h>
  45. #include <asm/ipl.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/facility.h>
  48. #include <asm/smp.h>
  49. #include <asm/mmu_context.h>
  50. #include <asm/cpcmd.h>
  51. #include <asm/lowcore.h>
  52. #include <asm/irq.h>
  53. #include <asm/page.h>
  54. #include <asm/ptrace.h>
  55. #include <asm/sections.h>
  56. #include <asm/ebcdic.h>
  57. #include <asm/kvm_virtio.h>
  58. #include <asm/diag.h>
  59. #include <asm/os_info.h>
  60. #include <asm/sclp.h>
  61. #include "entry.h"
  62. long psw_kernel_bits = PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_ASC_PRIMARY |
  63. PSW_MASK_EA | PSW_MASK_BA;
  64. long psw_user_bits = PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT |
  65. PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_MCHECK |
  66. PSW_MASK_PSTATE | PSW_ASC_HOME;
  67. /*
  68. * User copy operations.
  69. */
  70. struct uaccess_ops uaccess;
  71. EXPORT_SYMBOL(uaccess);
  72. /*
  73. * Machine setup..
  74. */
  75. unsigned int console_mode = 0;
  76. EXPORT_SYMBOL(console_mode);
  77. unsigned int console_devno = -1;
  78. EXPORT_SYMBOL(console_devno);
  79. unsigned int console_irq = -1;
  80. EXPORT_SYMBOL(console_irq);
  81. unsigned long elf_hwcap = 0;
  82. char elf_platform[ELF_PLATFORM_SIZE];
  83. struct mem_chunk __initdata memory_chunk[MEMORY_CHUNKS];
  84. int __initdata memory_end_set;
  85. unsigned long __initdata memory_end;
  86. unsigned long VMALLOC_START;
  87. EXPORT_SYMBOL(VMALLOC_START);
  88. unsigned long VMALLOC_END;
  89. EXPORT_SYMBOL(VMALLOC_END);
  90. struct page *vmemmap;
  91. EXPORT_SYMBOL(vmemmap);
  92. #ifdef CONFIG_64BIT
  93. unsigned long MODULES_VADDR;
  94. unsigned long MODULES_END;
  95. #endif
  96. /* An array with a pointer to the lowcore of every CPU. */
  97. struct _lowcore *lowcore_ptr[NR_CPUS];
  98. EXPORT_SYMBOL(lowcore_ptr);
  99. /*
  100. * This is set up by the setup-routine at boot-time
  101. * for S390 need to find out, what we have to setup
  102. * using address 0x10400 ...
  103. */
  104. #include <asm/setup.h>
  105. /*
  106. * condev= and conmode= setup parameter.
  107. */
  108. static int __init condev_setup(char *str)
  109. {
  110. int vdev;
  111. vdev = simple_strtoul(str, &str, 0);
  112. if (vdev >= 0 && vdev < 65536) {
  113. console_devno = vdev;
  114. console_irq = -1;
  115. }
  116. return 1;
  117. }
  118. __setup("condev=", condev_setup);
  119. static void __init set_preferred_console(void)
  120. {
  121. if (MACHINE_IS_KVM) {
  122. if (sclp_has_vt220())
  123. add_preferred_console("ttyS", 1, NULL);
  124. else if (sclp_has_linemode())
  125. add_preferred_console("ttyS", 0, NULL);
  126. else
  127. add_preferred_console("hvc", 0, NULL);
  128. } else if (CONSOLE_IS_3215 || CONSOLE_IS_SCLP)
  129. add_preferred_console("ttyS", 0, NULL);
  130. else if (CONSOLE_IS_3270)
  131. add_preferred_console("tty3270", 0, NULL);
  132. }
  133. static int __init conmode_setup(char *str)
  134. {
  135. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  136. if (strncmp(str, "hwc", 4) == 0 || strncmp(str, "sclp", 5) == 0)
  137. SET_CONSOLE_SCLP;
  138. #endif
  139. #if defined(CONFIG_TN3215_CONSOLE)
  140. if (strncmp(str, "3215", 5) == 0)
  141. SET_CONSOLE_3215;
  142. #endif
  143. #if defined(CONFIG_TN3270_CONSOLE)
  144. if (strncmp(str, "3270", 5) == 0)
  145. SET_CONSOLE_3270;
  146. #endif
  147. set_preferred_console();
  148. return 1;
  149. }
  150. __setup("conmode=", conmode_setup);
  151. static void __init conmode_default(void)
  152. {
  153. char query_buffer[1024];
  154. char *ptr;
  155. if (MACHINE_IS_VM) {
  156. cpcmd("QUERY CONSOLE", query_buffer, 1024, NULL);
  157. console_devno = simple_strtoul(query_buffer + 5, NULL, 16);
  158. ptr = strstr(query_buffer, "SUBCHANNEL =");
  159. console_irq = simple_strtoul(ptr + 13, NULL, 16);
  160. cpcmd("QUERY TERM", query_buffer, 1024, NULL);
  161. ptr = strstr(query_buffer, "CONMODE");
  162. /*
  163. * Set the conmode to 3215 so that the device recognition
  164. * will set the cu_type of the console to 3215. If the
  165. * conmode is 3270 and we don't set it back then both
  166. * 3215 and the 3270 driver will try to access the console
  167. * device (3215 as console and 3270 as normal tty).
  168. */
  169. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  170. if (ptr == NULL) {
  171. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  172. SET_CONSOLE_SCLP;
  173. #endif
  174. return;
  175. }
  176. if (strncmp(ptr + 8, "3270", 4) == 0) {
  177. #if defined(CONFIG_TN3270_CONSOLE)
  178. SET_CONSOLE_3270;
  179. #elif defined(CONFIG_TN3215_CONSOLE)
  180. SET_CONSOLE_3215;
  181. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  182. SET_CONSOLE_SCLP;
  183. #endif
  184. } else if (strncmp(ptr + 8, "3215", 4) == 0) {
  185. #if defined(CONFIG_TN3215_CONSOLE)
  186. SET_CONSOLE_3215;
  187. #elif defined(CONFIG_TN3270_CONSOLE)
  188. SET_CONSOLE_3270;
  189. #elif defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  190. SET_CONSOLE_SCLP;
  191. #endif
  192. }
  193. } else {
  194. #if defined(CONFIG_SCLP_CONSOLE) || defined(CONFIG_SCLP_VT220_CONSOLE)
  195. SET_CONSOLE_SCLP;
  196. #endif
  197. }
  198. }
  199. #ifdef CONFIG_ZFCPDUMP
  200. static void __init setup_zfcpdump(unsigned int console_devno)
  201. {
  202. static char str[41];
  203. if (ipl_info.type != IPL_TYPE_FCP_DUMP)
  204. return;
  205. if (OLDMEM_BASE)
  206. return;
  207. if (console_devno != -1)
  208. sprintf(str, " cio_ignore=all,!0.0.%04x,!0.0.%04x",
  209. ipl_info.data.fcp.dev_id.devno, console_devno);
  210. else
  211. sprintf(str, " cio_ignore=all,!0.0.%04x",
  212. ipl_info.data.fcp.dev_id.devno);
  213. strcat(boot_command_line, str);
  214. console_loglevel = 2;
  215. }
  216. #else
  217. static inline void setup_zfcpdump(unsigned int console_devno) {}
  218. #endif /* CONFIG_ZFCPDUMP */
  219. /*
  220. * Reboot, halt and power_off stubs. They just call _machine_restart,
  221. * _machine_halt or _machine_power_off.
  222. */
  223. void machine_restart(char *command)
  224. {
  225. if ((!in_interrupt() && !in_atomic()) || oops_in_progress)
  226. /*
  227. * Only unblank the console if we are called in enabled
  228. * context or a bust_spinlocks cleared the way for us.
  229. */
  230. console_unblank();
  231. _machine_restart(command);
  232. }
  233. void machine_halt(void)
  234. {
  235. if (!in_interrupt() || oops_in_progress)
  236. /*
  237. * Only unblank the console if we are called in enabled
  238. * context or a bust_spinlocks cleared the way for us.
  239. */
  240. console_unblank();
  241. _machine_halt();
  242. }
  243. void machine_power_off(void)
  244. {
  245. if (!in_interrupt() || oops_in_progress)
  246. /*
  247. * Only unblank the console if we are called in enabled
  248. * context or a bust_spinlocks cleared the way for us.
  249. */
  250. console_unblank();
  251. _machine_power_off();
  252. }
  253. /*
  254. * Dummy power off function.
  255. */
  256. void (*pm_power_off)(void) = machine_power_off;
  257. EXPORT_SYMBOL_GPL(pm_power_off);
  258. static int __init early_parse_mem(char *p)
  259. {
  260. memory_end = memparse(p, &p);
  261. memory_end_set = 1;
  262. return 0;
  263. }
  264. early_param("mem", early_parse_mem);
  265. static int __init parse_vmalloc(char *arg)
  266. {
  267. if (!arg)
  268. return -EINVAL;
  269. VMALLOC_END = (memparse(arg, &arg) + PAGE_SIZE - 1) & PAGE_MASK;
  270. return 0;
  271. }
  272. early_param("vmalloc", parse_vmalloc);
  273. unsigned int s390_user_mode = PRIMARY_SPACE_MODE;
  274. EXPORT_SYMBOL_GPL(s390_user_mode);
  275. static void __init set_user_mode_primary(void)
  276. {
  277. psw_kernel_bits = (psw_kernel_bits & ~PSW_MASK_ASC) | PSW_ASC_HOME;
  278. psw_user_bits = (psw_user_bits & ~PSW_MASK_ASC) | PSW_ASC_PRIMARY;
  279. #ifdef CONFIG_COMPAT
  280. psw32_user_bits =
  281. (psw32_user_bits & ~PSW32_MASK_ASC) | PSW32_ASC_PRIMARY;
  282. #endif
  283. uaccess = MACHINE_HAS_MVCOS ? uaccess_mvcos_switch : uaccess_pt;
  284. }
  285. static int __init early_parse_user_mode(char *p)
  286. {
  287. if (p && strcmp(p, "primary") == 0)
  288. s390_user_mode = PRIMARY_SPACE_MODE;
  289. else if (!p || strcmp(p, "home") == 0)
  290. s390_user_mode = HOME_SPACE_MODE;
  291. else
  292. return 1;
  293. return 0;
  294. }
  295. early_param("user_mode", early_parse_user_mode);
  296. static void __init setup_addressing_mode(void)
  297. {
  298. if (s390_user_mode != PRIMARY_SPACE_MODE)
  299. return;
  300. set_user_mode_primary();
  301. if (MACHINE_HAS_MVCOS)
  302. pr_info("Address spaces switched, mvcos available\n");
  303. else
  304. pr_info("Address spaces switched, mvcos not available\n");
  305. }
  306. void *restart_stack __attribute__((__section__(".data")));
  307. static void __init setup_lowcore(void)
  308. {
  309. struct _lowcore *lc;
  310. /*
  311. * Setup lowcore for boot cpu
  312. */
  313. BUILD_BUG_ON(sizeof(struct _lowcore) != LC_PAGES * 4096);
  314. lc = __alloc_bootmem_low(LC_PAGES * PAGE_SIZE, LC_PAGES * PAGE_SIZE, 0);
  315. lc->restart_psw.mask = psw_kernel_bits;
  316. lc->restart_psw.addr =
  317. PSW_ADDR_AMODE | (unsigned long) restart_int_handler;
  318. lc->external_new_psw.mask = psw_kernel_bits |
  319. PSW_MASK_DAT | PSW_MASK_MCHECK;
  320. lc->external_new_psw.addr =
  321. PSW_ADDR_AMODE | (unsigned long) ext_int_handler;
  322. lc->svc_new_psw.mask = psw_kernel_bits |
  323. PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  324. lc->svc_new_psw.addr = PSW_ADDR_AMODE | (unsigned long) system_call;
  325. lc->program_new_psw.mask = psw_kernel_bits |
  326. PSW_MASK_DAT | PSW_MASK_MCHECK;
  327. lc->program_new_psw.addr =
  328. PSW_ADDR_AMODE | (unsigned long) pgm_check_handler;
  329. lc->mcck_new_psw.mask = psw_kernel_bits;
  330. lc->mcck_new_psw.addr =
  331. PSW_ADDR_AMODE | (unsigned long) mcck_int_handler;
  332. lc->io_new_psw.mask = psw_kernel_bits |
  333. PSW_MASK_DAT | PSW_MASK_MCHECK;
  334. lc->io_new_psw.addr = PSW_ADDR_AMODE | (unsigned long) io_int_handler;
  335. lc->clock_comparator = -1ULL;
  336. lc->kernel_stack = ((unsigned long) &init_thread_union)
  337. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  338. lc->async_stack = (unsigned long)
  339. __alloc_bootmem(ASYNC_SIZE, ASYNC_SIZE, 0)
  340. + ASYNC_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  341. lc->panic_stack = (unsigned long)
  342. __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, 0)
  343. + PAGE_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  344. lc->current_task = (unsigned long) init_thread_union.thread_info.task;
  345. lc->thread_info = (unsigned long) &init_thread_union;
  346. lc->machine_flags = S390_lowcore.machine_flags;
  347. lc->stfl_fac_list = S390_lowcore.stfl_fac_list;
  348. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  349. MAX_FACILITY_BIT/8);
  350. #ifndef CONFIG_64BIT
  351. if (MACHINE_HAS_IEEE) {
  352. lc->extended_save_area_addr = (__u32)
  353. __alloc_bootmem_low(PAGE_SIZE, PAGE_SIZE, 0);
  354. /* enable extended save area */
  355. __ctl_set_bit(14, 29);
  356. }
  357. #else
  358. lc->vdso_per_cpu_data = (unsigned long) &lc->paste[0];
  359. #endif
  360. lc->sync_enter_timer = S390_lowcore.sync_enter_timer;
  361. lc->async_enter_timer = S390_lowcore.async_enter_timer;
  362. lc->exit_timer = S390_lowcore.exit_timer;
  363. lc->user_timer = S390_lowcore.user_timer;
  364. lc->system_timer = S390_lowcore.system_timer;
  365. lc->steal_timer = S390_lowcore.steal_timer;
  366. lc->last_update_timer = S390_lowcore.last_update_timer;
  367. lc->last_update_clock = S390_lowcore.last_update_clock;
  368. lc->ftrace_func = S390_lowcore.ftrace_func;
  369. restart_stack = __alloc_bootmem(ASYNC_SIZE, ASYNC_SIZE, 0);
  370. restart_stack += ASYNC_SIZE;
  371. /*
  372. * Set up PSW restart to call ipl.c:do_restart(). Copy the relevant
  373. * restart data to the absolute zero lowcore. This is necesary if
  374. * PSW restart is done on an offline CPU that has lowcore zero.
  375. */
  376. lc->restart_stack = (unsigned long) restart_stack;
  377. lc->restart_fn = (unsigned long) do_restart;
  378. lc->restart_data = 0;
  379. lc->restart_source = -1UL;
  380. /* Setup absolute zero lowcore */
  381. mem_assign_absolute(S390_lowcore.restart_stack, lc->restart_stack);
  382. mem_assign_absolute(S390_lowcore.restart_fn, lc->restart_fn);
  383. mem_assign_absolute(S390_lowcore.restart_data, lc->restart_data);
  384. mem_assign_absolute(S390_lowcore.restart_source, lc->restart_source);
  385. mem_assign_absolute(S390_lowcore.restart_psw, lc->restart_psw);
  386. set_prefix((u32)(unsigned long) lc);
  387. lowcore_ptr[0] = lc;
  388. }
  389. static struct resource code_resource = {
  390. .name = "Kernel code",
  391. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  392. };
  393. static struct resource data_resource = {
  394. .name = "Kernel data",
  395. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  396. };
  397. static struct resource bss_resource = {
  398. .name = "Kernel bss",
  399. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  400. };
  401. static struct resource __initdata *standard_resources[] = {
  402. &code_resource,
  403. &data_resource,
  404. &bss_resource,
  405. };
  406. static void __init setup_resources(void)
  407. {
  408. struct resource *res, *std_res, *sub_res;
  409. int i, j;
  410. code_resource.start = (unsigned long) &_text;
  411. code_resource.end = (unsigned long) &_etext - 1;
  412. data_resource.start = (unsigned long) &_etext;
  413. data_resource.end = (unsigned long) &_edata - 1;
  414. bss_resource.start = (unsigned long) &__bss_start;
  415. bss_resource.end = (unsigned long) &__bss_stop - 1;
  416. for (i = 0; i < MEMORY_CHUNKS; i++) {
  417. if (!memory_chunk[i].size)
  418. continue;
  419. if (memory_chunk[i].type == CHUNK_OLDMEM ||
  420. memory_chunk[i].type == CHUNK_CRASHK)
  421. continue;
  422. res = alloc_bootmem_low(sizeof(*res));
  423. res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  424. switch (memory_chunk[i].type) {
  425. case CHUNK_READ_WRITE:
  426. case CHUNK_CRASHK:
  427. res->name = "System RAM";
  428. break;
  429. case CHUNK_READ_ONLY:
  430. res->name = "System ROM";
  431. res->flags |= IORESOURCE_READONLY;
  432. break;
  433. default:
  434. res->name = "reserved";
  435. }
  436. res->start = memory_chunk[i].addr;
  437. res->end = res->start + memory_chunk[i].size - 1;
  438. request_resource(&iomem_resource, res);
  439. for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
  440. std_res = standard_resources[j];
  441. if (std_res->start < res->start ||
  442. std_res->start > res->end)
  443. continue;
  444. if (std_res->end > res->end) {
  445. sub_res = alloc_bootmem_low(sizeof(*sub_res));
  446. *sub_res = *std_res;
  447. sub_res->end = res->end;
  448. std_res->start = res->end + 1;
  449. request_resource(res, sub_res);
  450. } else {
  451. request_resource(res, std_res);
  452. }
  453. }
  454. }
  455. }
  456. unsigned long real_memory_size;
  457. EXPORT_SYMBOL_GPL(real_memory_size);
  458. static void __init setup_memory_end(void)
  459. {
  460. unsigned long vmax, vmalloc_size, tmp;
  461. int i;
  462. #ifdef CONFIG_ZFCPDUMP
  463. if (ipl_info.type == IPL_TYPE_FCP_DUMP && !OLDMEM_BASE) {
  464. memory_end = ZFCPDUMP_HSA_SIZE;
  465. memory_end_set = 1;
  466. }
  467. #endif
  468. real_memory_size = 0;
  469. memory_end &= PAGE_MASK;
  470. /*
  471. * Make sure all chunks are MAX_ORDER aligned so we don't need the
  472. * extra checks that HOLES_IN_ZONE would require.
  473. */
  474. for (i = 0; i < MEMORY_CHUNKS; i++) {
  475. unsigned long start, end;
  476. struct mem_chunk *chunk;
  477. unsigned long align;
  478. chunk = &memory_chunk[i];
  479. align = 1UL << (MAX_ORDER + PAGE_SHIFT - 1);
  480. start = (chunk->addr + align - 1) & ~(align - 1);
  481. end = (chunk->addr + chunk->size) & ~(align - 1);
  482. if (start >= end)
  483. memset(chunk, 0, sizeof(*chunk));
  484. else {
  485. chunk->addr = start;
  486. chunk->size = end - start;
  487. }
  488. real_memory_size = max(real_memory_size,
  489. chunk->addr + chunk->size);
  490. }
  491. /* Choose kernel address space layout: 2, 3, or 4 levels. */
  492. #ifdef CONFIG_64BIT
  493. vmalloc_size = VMALLOC_END ?: (128UL << 30) - MODULES_LEN;
  494. tmp = (memory_end ?: real_memory_size) / PAGE_SIZE;
  495. tmp = tmp * (sizeof(struct page) + PAGE_SIZE) + vmalloc_size;
  496. if (tmp <= (1UL << 42))
  497. vmax = 1UL << 42; /* 3-level kernel page table */
  498. else
  499. vmax = 1UL << 53; /* 4-level kernel page table */
  500. /* module area is at the end of the kernel address space. */
  501. MODULES_END = vmax;
  502. MODULES_VADDR = MODULES_END - MODULES_LEN;
  503. VMALLOC_END = MODULES_VADDR;
  504. #else
  505. vmalloc_size = VMALLOC_END ?: 96UL << 20;
  506. vmax = 1UL << 31; /* 2-level kernel page table */
  507. /* vmalloc area is at the end of the kernel address space. */
  508. VMALLOC_END = vmax;
  509. #endif
  510. VMALLOC_START = vmax - vmalloc_size;
  511. /* Split remaining virtual space between 1:1 mapping & vmemmap array */
  512. tmp = VMALLOC_START / (PAGE_SIZE + sizeof(struct page));
  513. /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
  514. tmp = SECTION_ALIGN_UP(tmp);
  515. tmp = VMALLOC_START - tmp * sizeof(struct page);
  516. tmp &= ~((vmax >> 11) - 1); /* align to page table level */
  517. tmp = min(tmp, 1UL << MAX_PHYSMEM_BITS);
  518. vmemmap = (struct page *) tmp;
  519. /* Take care that memory_end is set and <= vmemmap */
  520. memory_end = min(memory_end ?: real_memory_size, tmp);
  521. /* Fixup memory chunk array to fit into 0..memory_end */
  522. for (i = 0; i < MEMORY_CHUNKS; i++) {
  523. struct mem_chunk *chunk = &memory_chunk[i];
  524. if (chunk->addr >= memory_end) {
  525. memset(chunk, 0, sizeof(*chunk));
  526. continue;
  527. }
  528. if (chunk->addr + chunk->size > memory_end)
  529. chunk->size = memory_end - chunk->addr;
  530. }
  531. }
  532. static void __init setup_vmcoreinfo(void)
  533. {
  534. mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
  535. }
  536. #ifdef CONFIG_CRASH_DUMP
  537. /*
  538. * Find suitable location for crashkernel memory
  539. */
  540. static unsigned long __init find_crash_base(unsigned long crash_size,
  541. char **msg)
  542. {
  543. unsigned long crash_base;
  544. struct mem_chunk *chunk;
  545. int i;
  546. if (memory_chunk[0].size < crash_size) {
  547. *msg = "first memory chunk must be at least crashkernel size";
  548. return 0;
  549. }
  550. if (OLDMEM_BASE && crash_size == OLDMEM_SIZE)
  551. return OLDMEM_BASE;
  552. for (i = MEMORY_CHUNKS - 1; i >= 0; i--) {
  553. chunk = &memory_chunk[i];
  554. if (chunk->size == 0)
  555. continue;
  556. if (chunk->type != CHUNK_READ_WRITE)
  557. continue;
  558. if (chunk->size < crash_size)
  559. continue;
  560. crash_base = (chunk->addr + chunk->size) - crash_size;
  561. if (crash_base < crash_size)
  562. continue;
  563. if (crash_base < ZFCPDUMP_HSA_SIZE_MAX)
  564. continue;
  565. if (crash_base < (unsigned long) INITRD_START + INITRD_SIZE)
  566. continue;
  567. return crash_base;
  568. }
  569. *msg = "no suitable area found";
  570. return 0;
  571. }
  572. /*
  573. * Check if crash_base and crash_size is valid
  574. */
  575. static int __init verify_crash_base(unsigned long crash_base,
  576. unsigned long crash_size,
  577. char **msg)
  578. {
  579. struct mem_chunk *chunk;
  580. int i;
  581. /*
  582. * Because we do the swap to zero, we must have at least 'crash_size'
  583. * bytes free space before crash_base
  584. */
  585. if (crash_size > crash_base) {
  586. *msg = "crashkernel offset must be greater than size";
  587. return -EINVAL;
  588. }
  589. /* First memory chunk must be at least crash_size */
  590. if (memory_chunk[0].size < crash_size) {
  591. *msg = "first memory chunk must be at least crashkernel size";
  592. return -EINVAL;
  593. }
  594. /* Check if we fit into the respective memory chunk */
  595. for (i = 0; i < MEMORY_CHUNKS; i++) {
  596. chunk = &memory_chunk[i];
  597. if (chunk->size == 0)
  598. continue;
  599. if (crash_base < chunk->addr)
  600. continue;
  601. if (crash_base >= chunk->addr + chunk->size)
  602. continue;
  603. /* we have found the memory chunk */
  604. if (crash_base + crash_size > chunk->addr + chunk->size) {
  605. *msg = "selected memory chunk is too small for "
  606. "crashkernel memory";
  607. return -EINVAL;
  608. }
  609. return 0;
  610. }
  611. *msg = "invalid memory range specified";
  612. return -EINVAL;
  613. }
  614. /*
  615. * Reserve kdump memory by creating a memory hole in the mem_chunk array
  616. */
  617. static void __init reserve_kdump_bootmem(unsigned long addr, unsigned long size,
  618. int type)
  619. {
  620. create_mem_hole(memory_chunk, addr, size, type);
  621. }
  622. /*
  623. * When kdump is enabled, we have to ensure that no memory from
  624. * the area [0 - crashkernel memory size] and
  625. * [crashk_res.start - crashk_res.end] is set offline.
  626. */
  627. static int kdump_mem_notifier(struct notifier_block *nb,
  628. unsigned long action, void *data)
  629. {
  630. struct memory_notify *arg = data;
  631. if (arg->start_pfn < PFN_DOWN(resource_size(&crashk_res)))
  632. return NOTIFY_BAD;
  633. if (arg->start_pfn > PFN_DOWN(crashk_res.end))
  634. return NOTIFY_OK;
  635. if (arg->start_pfn + arg->nr_pages - 1 < PFN_DOWN(crashk_res.start))
  636. return NOTIFY_OK;
  637. return NOTIFY_BAD;
  638. }
  639. static struct notifier_block kdump_mem_nb = {
  640. .notifier_call = kdump_mem_notifier,
  641. };
  642. #endif
  643. /*
  644. * Make sure that oldmem, where the dump is stored, is protected
  645. */
  646. static void reserve_oldmem(void)
  647. {
  648. #ifdef CONFIG_CRASH_DUMP
  649. if (!OLDMEM_BASE)
  650. return;
  651. reserve_kdump_bootmem(OLDMEM_BASE, OLDMEM_SIZE, CHUNK_OLDMEM);
  652. reserve_kdump_bootmem(OLDMEM_SIZE, memory_end - OLDMEM_SIZE,
  653. CHUNK_OLDMEM);
  654. if (OLDMEM_BASE + OLDMEM_SIZE == real_memory_size)
  655. saved_max_pfn = PFN_DOWN(OLDMEM_BASE) - 1;
  656. else
  657. saved_max_pfn = PFN_DOWN(real_memory_size) - 1;
  658. #endif
  659. }
  660. /*
  661. * Reserve memory for kdump kernel to be loaded with kexec
  662. */
  663. static void __init reserve_crashkernel(void)
  664. {
  665. #ifdef CONFIG_CRASH_DUMP
  666. unsigned long long crash_base, crash_size;
  667. char *msg = NULL;
  668. int rc;
  669. rc = parse_crashkernel(boot_command_line, memory_end, &crash_size,
  670. &crash_base);
  671. if (rc || crash_size == 0)
  672. return;
  673. crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
  674. crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
  675. if (register_memory_notifier(&kdump_mem_nb))
  676. return;
  677. if (!crash_base)
  678. crash_base = find_crash_base(crash_size, &msg);
  679. if (!crash_base) {
  680. pr_info("crashkernel reservation failed: %s\n", msg);
  681. unregister_memory_notifier(&kdump_mem_nb);
  682. return;
  683. }
  684. if (verify_crash_base(crash_base, crash_size, &msg)) {
  685. pr_info("crashkernel reservation failed: %s\n", msg);
  686. unregister_memory_notifier(&kdump_mem_nb);
  687. return;
  688. }
  689. if (!OLDMEM_BASE && MACHINE_IS_VM)
  690. diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));
  691. crashk_res.start = crash_base;
  692. crashk_res.end = crash_base + crash_size - 1;
  693. insert_resource(&iomem_resource, &crashk_res);
  694. reserve_kdump_bootmem(crash_base, crash_size, CHUNK_CRASHK);
  695. pr_info("Reserving %lluMB of memory at %lluMB "
  696. "for crashkernel (System RAM: %luMB)\n",
  697. crash_size >> 20, crash_base >> 20, memory_end >> 20);
  698. os_info_crashkernel_add(crash_base, crash_size);
  699. #endif
  700. }
  701. static void __init setup_memory(void)
  702. {
  703. unsigned long bootmap_size;
  704. unsigned long start_pfn, end_pfn;
  705. int i;
  706. /*
  707. * partially used pages are not usable - thus
  708. * we are rounding upwards:
  709. */
  710. start_pfn = PFN_UP(__pa(&_end));
  711. end_pfn = max_pfn = PFN_DOWN(memory_end);
  712. #ifdef CONFIG_BLK_DEV_INITRD
  713. /*
  714. * Move the initrd in case the bitmap of the bootmem allocater
  715. * would overwrite it.
  716. */
  717. if (INITRD_START && INITRD_SIZE) {
  718. unsigned long bmap_size;
  719. unsigned long start;
  720. bmap_size = bootmem_bootmap_pages(end_pfn - start_pfn + 1);
  721. bmap_size = PFN_PHYS(bmap_size);
  722. if (PFN_PHYS(start_pfn) + bmap_size > INITRD_START) {
  723. start = PFN_PHYS(start_pfn) + bmap_size + PAGE_SIZE;
  724. #ifdef CONFIG_CRASH_DUMP
  725. if (OLDMEM_BASE) {
  726. /* Move initrd behind kdump oldmem */
  727. if (start + INITRD_SIZE > OLDMEM_BASE &&
  728. start < OLDMEM_BASE + OLDMEM_SIZE)
  729. start = OLDMEM_BASE + OLDMEM_SIZE;
  730. }
  731. #endif
  732. if (start + INITRD_SIZE > memory_end) {
  733. pr_err("initrd extends beyond end of "
  734. "memory (0x%08lx > 0x%08lx) "
  735. "disabling initrd\n",
  736. start + INITRD_SIZE, memory_end);
  737. INITRD_START = INITRD_SIZE = 0;
  738. } else {
  739. pr_info("Moving initrd (0x%08lx -> "
  740. "0x%08lx, size: %ld)\n",
  741. INITRD_START, start, INITRD_SIZE);
  742. memmove((void *) start, (void *) INITRD_START,
  743. INITRD_SIZE);
  744. INITRD_START = start;
  745. }
  746. }
  747. }
  748. #endif
  749. /*
  750. * Initialize the boot-time allocator
  751. */
  752. bootmap_size = init_bootmem(start_pfn, end_pfn);
  753. /*
  754. * Register RAM areas with the bootmem allocator.
  755. */
  756. for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
  757. unsigned long start_chunk, end_chunk, pfn;
  758. if (memory_chunk[i].type != CHUNK_READ_WRITE &&
  759. memory_chunk[i].type != CHUNK_CRASHK)
  760. continue;
  761. start_chunk = PFN_DOWN(memory_chunk[i].addr);
  762. end_chunk = start_chunk + PFN_DOWN(memory_chunk[i].size);
  763. end_chunk = min(end_chunk, end_pfn);
  764. if (start_chunk >= end_chunk)
  765. continue;
  766. memblock_add_node(PFN_PHYS(start_chunk),
  767. PFN_PHYS(end_chunk - start_chunk), 0);
  768. pfn = max(start_chunk, start_pfn);
  769. storage_key_init_range(PFN_PHYS(pfn), PFN_PHYS(end_chunk));
  770. }
  771. psw_set_key(PAGE_DEFAULT_KEY);
  772. free_bootmem_with_active_regions(0, max_pfn);
  773. /*
  774. * Reserve memory used for lowcore/command line/kernel image.
  775. */
  776. reserve_bootmem(0, (unsigned long)_ehead, BOOTMEM_DEFAULT);
  777. reserve_bootmem((unsigned long)_stext,
  778. PFN_PHYS(start_pfn) - (unsigned long)_stext,
  779. BOOTMEM_DEFAULT);
  780. /*
  781. * Reserve the bootmem bitmap itself as well. We do this in two
  782. * steps (first step was init_bootmem()) because this catches
  783. * the (very unlikely) case of us accidentally initializing the
  784. * bootmem allocator with an invalid RAM area.
  785. */
  786. reserve_bootmem(start_pfn << PAGE_SHIFT, bootmap_size,
  787. BOOTMEM_DEFAULT);
  788. #ifdef CONFIG_CRASH_DUMP
  789. if (crashk_res.start)
  790. reserve_bootmem(crashk_res.start,
  791. crashk_res.end - crashk_res.start + 1,
  792. BOOTMEM_DEFAULT);
  793. if (is_kdump_kernel())
  794. reserve_bootmem(elfcorehdr_addr - OLDMEM_BASE,
  795. PAGE_ALIGN(elfcorehdr_size), BOOTMEM_DEFAULT);
  796. #endif
  797. #ifdef CONFIG_BLK_DEV_INITRD
  798. if (INITRD_START && INITRD_SIZE) {
  799. if (INITRD_START + INITRD_SIZE <= memory_end) {
  800. reserve_bootmem(INITRD_START, INITRD_SIZE,
  801. BOOTMEM_DEFAULT);
  802. initrd_start = INITRD_START;
  803. initrd_end = initrd_start + INITRD_SIZE;
  804. } else {
  805. pr_err("initrd extends beyond end of "
  806. "memory (0x%08lx > 0x%08lx) "
  807. "disabling initrd\n",
  808. initrd_start + INITRD_SIZE, memory_end);
  809. initrd_start = initrd_end = 0;
  810. }
  811. }
  812. #endif
  813. }
  814. /*
  815. * Setup hardware capabilities.
  816. */
  817. static void __init setup_hwcaps(void)
  818. {
  819. static const int stfl_bits[6] = { 0, 2, 7, 17, 19, 21 };
  820. struct cpuid cpu_id;
  821. int i;
  822. /*
  823. * The store facility list bits numbers as found in the principles
  824. * of operation are numbered with bit 1UL<<31 as number 0 to
  825. * bit 1UL<<0 as number 31.
  826. * Bit 0: instructions named N3, "backported" to esa-mode
  827. * Bit 2: z/Architecture mode is active
  828. * Bit 7: the store-facility-list-extended facility is installed
  829. * Bit 17: the message-security assist is installed
  830. * Bit 19: the long-displacement facility is installed
  831. * Bit 21: the extended-immediate facility is installed
  832. * Bit 22: extended-translation facility 3 is installed
  833. * Bit 30: extended-translation facility 3 enhancement facility
  834. * These get translated to:
  835. * HWCAP_S390_ESAN3 bit 0, HWCAP_S390_ZARCH bit 1,
  836. * HWCAP_S390_STFLE bit 2, HWCAP_S390_MSA bit 3,
  837. * HWCAP_S390_LDISP bit 4, HWCAP_S390_EIMM bit 5 and
  838. * HWCAP_S390_ETF3EH bit 8 (22 && 30).
  839. */
  840. for (i = 0; i < 6; i++)
  841. if (test_facility(stfl_bits[i]))
  842. elf_hwcap |= 1UL << i;
  843. if (test_facility(22) && test_facility(30))
  844. elf_hwcap |= HWCAP_S390_ETF3EH;
  845. /*
  846. * Check for additional facilities with store-facility-list-extended.
  847. * stfle stores doublewords (8 byte) with bit 1ULL<<63 as bit 0
  848. * and 1ULL<<0 as bit 63. Bits 0-31 contain the same information
  849. * as stored by stfl, bits 32-xxx contain additional facilities.
  850. * How many facility words are stored depends on the number of
  851. * doublewords passed to the instruction. The additional facilities
  852. * are:
  853. * Bit 42: decimal floating point facility is installed
  854. * Bit 44: perform floating point operation facility is installed
  855. * translated to:
  856. * HWCAP_S390_DFP bit 6 (42 && 44).
  857. */
  858. if ((elf_hwcap & (1UL << 2)) && test_facility(42) && test_facility(44))
  859. elf_hwcap |= HWCAP_S390_DFP;
  860. /*
  861. * Huge page support HWCAP_S390_HPAGE is bit 7.
  862. */
  863. if (MACHINE_HAS_HPAGE)
  864. elf_hwcap |= HWCAP_S390_HPAGE;
  865. #if defined(CONFIG_64BIT)
  866. /*
  867. * 64-bit register support for 31-bit processes
  868. * HWCAP_S390_HIGH_GPRS is bit 9.
  869. */
  870. elf_hwcap |= HWCAP_S390_HIGH_GPRS;
  871. /*
  872. * Transactional execution support HWCAP_S390_TE is bit 10.
  873. */
  874. if (test_facility(50) && test_facility(73))
  875. elf_hwcap |= HWCAP_S390_TE;
  876. #endif
  877. get_cpu_id(&cpu_id);
  878. switch (cpu_id.machine) {
  879. case 0x9672:
  880. #if !defined(CONFIG_64BIT)
  881. default: /* Use "g5" as default for 31 bit kernels. */
  882. #endif
  883. strcpy(elf_platform, "g5");
  884. break;
  885. case 0x2064:
  886. case 0x2066:
  887. #if defined(CONFIG_64BIT)
  888. default: /* Use "z900" as default for 64 bit kernels. */
  889. #endif
  890. strcpy(elf_platform, "z900");
  891. break;
  892. case 0x2084:
  893. case 0x2086:
  894. strcpy(elf_platform, "z990");
  895. break;
  896. case 0x2094:
  897. case 0x2096:
  898. strcpy(elf_platform, "z9-109");
  899. break;
  900. case 0x2097:
  901. case 0x2098:
  902. strcpy(elf_platform, "z10");
  903. break;
  904. case 0x2817:
  905. case 0x2818:
  906. strcpy(elf_platform, "z196");
  907. break;
  908. case 0x2827:
  909. strcpy(elf_platform, "zEC12");
  910. break;
  911. }
  912. }
  913. /*
  914. * Setup function called from init/main.c just after the banner
  915. * was printed.
  916. */
  917. void __init setup_arch(char **cmdline_p)
  918. {
  919. /*
  920. * print what head.S has found out about the machine
  921. */
  922. #ifndef CONFIG_64BIT
  923. if (MACHINE_IS_VM)
  924. pr_info("Linux is running as a z/VM "
  925. "guest operating system in 31-bit mode\n");
  926. else if (MACHINE_IS_LPAR)
  927. pr_info("Linux is running natively in 31-bit mode\n");
  928. if (MACHINE_HAS_IEEE)
  929. pr_info("The hardware system has IEEE compatible "
  930. "floating point units\n");
  931. else
  932. pr_info("The hardware system has no IEEE compatible "
  933. "floating point units\n");
  934. #else /* CONFIG_64BIT */
  935. if (MACHINE_IS_VM)
  936. pr_info("Linux is running as a z/VM "
  937. "guest operating system in 64-bit mode\n");
  938. else if (MACHINE_IS_KVM)
  939. pr_info("Linux is running under KVM in 64-bit mode\n");
  940. else if (MACHINE_IS_LPAR)
  941. pr_info("Linux is running natively in 64-bit mode\n");
  942. #endif /* CONFIG_64BIT */
  943. /* Have one command line that is parsed and saved in /proc/cmdline */
  944. /* boot_command_line has been already set up in early.c */
  945. *cmdline_p = boot_command_line;
  946. ROOT_DEV = Root_RAM0;
  947. init_mm.start_code = PAGE_OFFSET;
  948. init_mm.end_code = (unsigned long) &_etext;
  949. init_mm.end_data = (unsigned long) &_edata;
  950. init_mm.brk = (unsigned long) &_end;
  951. if (MACHINE_HAS_MVCOS)
  952. memcpy(&uaccess, &uaccess_mvcos, sizeof(uaccess));
  953. else
  954. memcpy(&uaccess, &uaccess_std, sizeof(uaccess));
  955. parse_early_param();
  956. os_info_init();
  957. setup_ipl();
  958. setup_memory_end();
  959. setup_addressing_mode();
  960. reserve_oldmem();
  961. reserve_crashkernel();
  962. setup_memory();
  963. setup_resources();
  964. setup_vmcoreinfo();
  965. setup_lowcore();
  966. cpu_init();
  967. s390_init_cpu_topology();
  968. /*
  969. * Setup capabilities (ELF_HWCAP & ELF_PLATFORM).
  970. */
  971. setup_hwcaps();
  972. /*
  973. * Create kernel page tables and switch to virtual addressing.
  974. */
  975. paging_init();
  976. /* Setup default console */
  977. conmode_default();
  978. set_preferred_console();
  979. /* Setup zfcpdump support */
  980. setup_zfcpdump(console_devno);
  981. }