setup.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1995 Linus Torvalds
  7. * Copyright (C) 1995 Waldorf Electronics
  8. * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01, 02, 03 Ralf Baechle
  9. * Copyright (C) 1996 Stoned Elipot
  10. * Copyright (C) 1999 Silicon Graphics, Inc.
  11. * Copyright (C) 2000, 2001, 2002, 2007 Maciej W. Rozycki
  12. */
  13. #include <linux/init.h>
  14. #include <linux/cpu.h>
  15. #include <linux/delay.h>
  16. #include <linux/ioport.h>
  17. #include <linux/export.h>
  18. #include <linux/screen_info.h>
  19. #include <linux/memblock.h>
  20. #include <linux/initrd.h>
  21. #include <linux/root_dev.h>
  22. #include <linux/highmem.h>
  23. #include <linux/console.h>
  24. #include <linux/pfn.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/kexec.h>
  27. #include <linux/sizes.h>
  28. #include <linux/device.h>
  29. #include <linux/dma-map-ops.h>
  30. #include <linux/decompress/generic.h>
  31. #include <linux/of_fdt.h>
  32. #include <linux/dmi.h>
  33. #include <linux/crash_dump.h>
  34. #include <asm/addrspace.h>
  35. #include <asm/bootinfo.h>
  36. #include <asm/bugs.h>
  37. #include <asm/cache.h>
  38. #include <asm/cdmm.h>
  39. #include <asm/cpu.h>
  40. #include <asm/debug.h>
  41. #include <asm/mmzone.h>
  42. #include <asm/sections.h>
  43. #include <asm/setup.h>
  44. #include <asm/smp-ops.h>
  45. #include <asm/prom.h>
  46. #include <asm/fw/fw.h>
  47. #ifdef CONFIG_MIPS_ELF_APPENDED_DTB
  48. char __section(".appended_dtb") __appended_dtb[0x100000];
  49. #endif /* CONFIG_MIPS_ELF_APPENDED_DTB */
  50. struct cpuinfo_mips cpu_data[NR_CPUS] __read_mostly;
  51. EXPORT_SYMBOL(cpu_data);
  52. #ifdef CONFIG_VT
  53. struct screen_info screen_info;
  54. #endif
  55. /*
  56. * Setup information
  57. *
  58. * These are initialized so they are in the .data section
  59. */
  60. unsigned long mips_machtype __read_mostly = MACH_UNKNOWN;
  61. EXPORT_SYMBOL(mips_machtype);
  62. static char __initdata command_line[COMMAND_LINE_SIZE];
  63. char __initdata arcs_cmdline[COMMAND_LINE_SIZE];
  64. #ifdef CONFIG_CMDLINE_BOOL
  65. static const char builtin_cmdline[] __initconst = CONFIG_CMDLINE;
  66. #else
  67. static const char builtin_cmdline[] __initconst = "";
  68. #endif
  69. /*
  70. * mips_io_port_base is the begin of the address space to which x86 style
  71. * I/O ports are mapped.
  72. */
  73. unsigned long mips_io_port_base = -1;
  74. EXPORT_SYMBOL(mips_io_port_base);
  75. static struct resource code_resource = { .name = "Kernel code", };
  76. static struct resource data_resource = { .name = "Kernel data", };
  77. static struct resource bss_resource = { .name = "Kernel bss", };
  78. unsigned long __kaslr_offset __ro_after_init;
  79. EXPORT_SYMBOL(__kaslr_offset);
  80. static void *detect_magic __initdata = detect_memory_region;
  81. #ifdef CONFIG_MIPS_AUTO_PFN_OFFSET
  82. unsigned long ARCH_PFN_OFFSET;
  83. EXPORT_SYMBOL(ARCH_PFN_OFFSET);
  84. #endif
  85. void __init detect_memory_region(phys_addr_t start, phys_addr_t sz_min, phys_addr_t sz_max)
  86. {
  87. void *dm = &detect_magic;
  88. phys_addr_t size;
  89. for (size = sz_min; size < sz_max; size <<= 1) {
  90. if (!memcmp(dm, dm + size, sizeof(detect_magic)))
  91. break;
  92. }
  93. pr_debug("Memory: %lluMB of RAM detected at 0x%llx (min: %lluMB, max: %lluMB)\n",
  94. ((unsigned long long) size) / SZ_1M,
  95. (unsigned long long) start,
  96. ((unsigned long long) sz_min) / SZ_1M,
  97. ((unsigned long long) sz_max) / SZ_1M);
  98. memblock_add(start, size);
  99. }
  100. /*
  101. * Manage initrd
  102. */
  103. #ifdef CONFIG_BLK_DEV_INITRD
  104. static int __init rd_start_early(char *p)
  105. {
  106. unsigned long start = memparse(p, &p);
  107. #ifdef CONFIG_64BIT
  108. /* Guess if the sign extension was forgotten by bootloader */
  109. if (start < XKPHYS)
  110. start = (int)start;
  111. #endif
  112. initrd_start = start;
  113. initrd_end += start;
  114. return 0;
  115. }
  116. early_param("rd_start", rd_start_early);
  117. static int __init rd_size_early(char *p)
  118. {
  119. initrd_end += memparse(p, &p);
  120. return 0;
  121. }
  122. early_param("rd_size", rd_size_early);
  123. /* it returns the next free pfn after initrd */
  124. static unsigned long __init init_initrd(void)
  125. {
  126. unsigned long end;
  127. /*
  128. * Board specific code or command line parser should have
  129. * already set up initrd_start and initrd_end. In these cases
  130. * perfom sanity checks and use them if all looks good.
  131. */
  132. if (!initrd_start || initrd_end <= initrd_start)
  133. goto disable;
  134. if (initrd_start & ~PAGE_MASK) {
  135. pr_err("initrd start must be page aligned\n");
  136. goto disable;
  137. }
  138. /*
  139. * Sanitize initrd addresses. For example firmware
  140. * can't guess if they need to pass them through
  141. * 64-bits values if the kernel has been built in pure
  142. * 32-bit. We need also to switch from KSEG0 to XKPHYS
  143. * addresses now, so the code can now safely use __pa().
  144. */
  145. end = __pa(initrd_end);
  146. initrd_end = (unsigned long)__va(end);
  147. initrd_start = (unsigned long)__va(__pa(initrd_start));
  148. if (initrd_start < PAGE_OFFSET) {
  149. pr_err("initrd start < PAGE_OFFSET\n");
  150. goto disable;
  151. }
  152. ROOT_DEV = Root_RAM0;
  153. return PFN_UP(end);
  154. disable:
  155. initrd_start = 0;
  156. initrd_end = 0;
  157. return 0;
  158. }
  159. /* In some conditions (e.g. big endian bootloader with a little endian
  160. kernel), the initrd might appear byte swapped. Try to detect this and
  161. byte swap it if needed. */
  162. static void __init maybe_bswap_initrd(void)
  163. {
  164. #if defined(CONFIG_CPU_CAVIUM_OCTEON)
  165. u64 buf;
  166. /* Check for CPIO signature */
  167. if (!memcmp((void *)initrd_start, "070701", 6))
  168. return;
  169. /* Check for compressed initrd */
  170. if (decompress_method((unsigned char *)initrd_start, 8, NULL))
  171. return;
  172. /* Try again with a byte swapped header */
  173. buf = swab64p((u64 *)initrd_start);
  174. if (!memcmp(&buf, "070701", 6) ||
  175. decompress_method((unsigned char *)(&buf), 8, NULL)) {
  176. unsigned long i;
  177. pr_info("Byteswapped initrd detected\n");
  178. for (i = initrd_start; i < ALIGN(initrd_end, 8); i += 8)
  179. swab64s((u64 *)i);
  180. }
  181. #endif
  182. }
  183. static void __init finalize_initrd(void)
  184. {
  185. unsigned long size = initrd_end - initrd_start;
  186. if (size == 0) {
  187. printk(KERN_INFO "Initrd not found or empty");
  188. goto disable;
  189. }
  190. if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
  191. printk(KERN_ERR "Initrd extends beyond end of memory");
  192. goto disable;
  193. }
  194. maybe_bswap_initrd();
  195. memblock_reserve(__pa(initrd_start), size);
  196. initrd_below_start_ok = 1;
  197. pr_info("Initial ramdisk at: 0x%lx (%lu bytes)\n",
  198. initrd_start, size);
  199. return;
  200. disable:
  201. printk(KERN_CONT " - disabling initrd\n");
  202. initrd_start = 0;
  203. initrd_end = 0;
  204. }
  205. #else /* !CONFIG_BLK_DEV_INITRD */
  206. static unsigned long __init init_initrd(void)
  207. {
  208. return 0;
  209. }
  210. #define finalize_initrd() do {} while (0)
  211. #endif
  212. /*
  213. * Initialize the bootmem allocator. It also setup initrd related data
  214. * if needed.
  215. */
  216. #if defined(CONFIG_SGI_IP27) || (defined(CONFIG_CPU_LOONGSON64) && defined(CONFIG_NUMA))
  217. static void __init bootmem_init(void)
  218. {
  219. init_initrd();
  220. finalize_initrd();
  221. }
  222. #else /* !CONFIG_SGI_IP27 */
  223. static void __init bootmem_init(void)
  224. {
  225. phys_addr_t ramstart, ramend;
  226. unsigned long start, end;
  227. int i;
  228. ramstart = memblock_start_of_DRAM();
  229. ramend = memblock_end_of_DRAM();
  230. /*
  231. * Sanity check any INITRD first. We don't take it into account
  232. * for bootmem setup initially, rely on the end-of-kernel-code
  233. * as our memory range starting point. Once bootmem is inited we
  234. * will reserve the area used for the initrd.
  235. */
  236. init_initrd();
  237. /* Reserve memory occupied by kernel. */
  238. memblock_reserve(__pa_symbol(&_text),
  239. __pa_symbol(&_end) - __pa_symbol(&_text));
  240. /* max_low_pfn is not a number of pages but the end pfn of low mem */
  241. #ifdef CONFIG_MIPS_AUTO_PFN_OFFSET
  242. ARCH_PFN_OFFSET = PFN_UP(ramstart);
  243. #else
  244. /*
  245. * Reserve any memory between the start of RAM and PHYS_OFFSET
  246. */
  247. if (ramstart > PHYS_OFFSET)
  248. memblock_reserve(PHYS_OFFSET, ramstart - PHYS_OFFSET);
  249. if (PFN_UP(ramstart) > ARCH_PFN_OFFSET) {
  250. pr_info("Wasting %lu bytes for tracking %lu unused pages\n",
  251. (unsigned long)((PFN_UP(ramstart) - ARCH_PFN_OFFSET) * sizeof(struct page)),
  252. (unsigned long)(PFN_UP(ramstart) - ARCH_PFN_OFFSET));
  253. }
  254. #endif
  255. min_low_pfn = ARCH_PFN_OFFSET;
  256. max_pfn = PFN_DOWN(ramend);
  257. for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
  258. /*
  259. * Skip highmem here so we get an accurate max_low_pfn if low
  260. * memory stops short of high memory.
  261. * If the region overlaps HIGHMEM_START, end is clipped so
  262. * max_pfn excludes the highmem portion.
  263. */
  264. if (start >= PFN_DOWN(HIGHMEM_START))
  265. continue;
  266. if (end > PFN_DOWN(HIGHMEM_START))
  267. end = PFN_DOWN(HIGHMEM_START);
  268. if (end > max_low_pfn)
  269. max_low_pfn = end;
  270. }
  271. if (min_low_pfn >= max_low_pfn)
  272. panic("Incorrect memory mapping !!!");
  273. if (max_pfn > PFN_DOWN(HIGHMEM_START)) {
  274. #ifdef CONFIG_HIGHMEM
  275. highstart_pfn = PFN_DOWN(HIGHMEM_START);
  276. highend_pfn = max_pfn;
  277. #else
  278. max_low_pfn = PFN_DOWN(HIGHMEM_START);
  279. max_pfn = max_low_pfn;
  280. #endif
  281. }
  282. /*
  283. * Reserve initrd memory if needed.
  284. */
  285. finalize_initrd();
  286. }
  287. #endif /* CONFIG_SGI_IP27 */
  288. static int usermem __initdata;
  289. static int __init early_parse_mem(char *p)
  290. {
  291. phys_addr_t start, size;
  292. if (!p) {
  293. pr_err("mem parameter is empty, do nothing\n");
  294. return -EINVAL;
  295. }
  296. /*
  297. * If a user specifies memory size, we
  298. * blow away any automatically generated
  299. * size.
  300. */
  301. if (usermem == 0) {
  302. usermem = 1;
  303. memblock_remove(memblock_start_of_DRAM(),
  304. memblock_end_of_DRAM() - memblock_start_of_DRAM());
  305. }
  306. start = 0;
  307. size = memparse(p, &p);
  308. if (*p == '@')
  309. start = memparse(p + 1, &p);
  310. if (IS_ENABLED(CONFIG_NUMA))
  311. memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
  312. else
  313. memblock_add(start, size);
  314. return 0;
  315. }
  316. early_param("mem", early_parse_mem);
  317. static int __init early_parse_memmap(char *p)
  318. {
  319. char *oldp;
  320. u64 start_at, mem_size;
  321. if (!p)
  322. return -EINVAL;
  323. if (!strncmp(p, "exactmap", 8)) {
  324. pr_err("\"memmap=exactmap\" invalid on MIPS\n");
  325. return 0;
  326. }
  327. oldp = p;
  328. mem_size = memparse(p, &p);
  329. if (p == oldp)
  330. return -EINVAL;
  331. if (*p == '@') {
  332. start_at = memparse(p+1, &p);
  333. memblock_add(start_at, mem_size);
  334. } else if (*p == '#') {
  335. pr_err("\"memmap=nn#ss\" (force ACPI data) invalid on MIPS\n");
  336. return -EINVAL;
  337. } else if (*p == '$') {
  338. start_at = memparse(p+1, &p);
  339. memblock_add(start_at, mem_size);
  340. memblock_reserve(start_at, mem_size);
  341. } else {
  342. pr_err("\"memmap\" invalid format!\n");
  343. return -EINVAL;
  344. }
  345. if (*p == '\0') {
  346. usermem = 1;
  347. return 0;
  348. } else
  349. return -EINVAL;
  350. }
  351. early_param("memmap", early_parse_memmap);
  352. static void __init mips_reserve_vmcore(void)
  353. {
  354. #ifdef CONFIG_PROC_VMCORE
  355. phys_addr_t start, end;
  356. u64 i;
  357. if (!elfcorehdr_size) {
  358. for_each_mem_range(i, &start, &end) {
  359. if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
  360. /*
  361. * Reserve from the elf core header to the end of
  362. * the memory segment, that should all be kdump
  363. * reserved memory.
  364. */
  365. elfcorehdr_size = end - elfcorehdr_addr;
  366. break;
  367. }
  368. }
  369. }
  370. pr_info("Reserving %ldKB of memory at %ldKB for kdump\n",
  371. (unsigned long)elfcorehdr_size >> 10, (unsigned long)elfcorehdr_addr >> 10);
  372. memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
  373. #endif
  374. }
  375. #ifdef CONFIG_KEXEC
  376. /* 64M alignment for crash kernel regions */
  377. #define CRASH_ALIGN SZ_64M
  378. #define CRASH_ADDR_MAX SZ_512M
  379. static void __init mips_parse_crashkernel(void)
  380. {
  381. unsigned long long total_mem;
  382. unsigned long long crash_size, crash_base;
  383. int ret;
  384. total_mem = memblock_phys_mem_size();
  385. ret = parse_crashkernel(boot_command_line, total_mem,
  386. &crash_size, &crash_base);
  387. if (ret != 0 || crash_size <= 0)
  388. return;
  389. if (crash_base <= 0) {
  390. crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
  391. CRASH_ALIGN,
  392. CRASH_ADDR_MAX);
  393. if (!crash_base) {
  394. pr_warn("crashkernel reservation failed - No suitable area found.\n");
  395. return;
  396. }
  397. } else {
  398. unsigned long long start;
  399. start = memblock_phys_alloc_range(crash_size, 1,
  400. crash_base,
  401. crash_base + crash_size);
  402. if (start != crash_base) {
  403. pr_warn("Invalid memory region reserved for crash kernel\n");
  404. return;
  405. }
  406. }
  407. crashk_res.start = crash_base;
  408. crashk_res.end = crash_base + crash_size - 1;
  409. }
  410. static void __init request_crashkernel(struct resource *res)
  411. {
  412. int ret;
  413. if (crashk_res.start == crashk_res.end)
  414. return;
  415. ret = request_resource(res, &crashk_res);
  416. if (!ret)
  417. pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
  418. (unsigned long)(resource_size(&crashk_res) >> 20),
  419. (unsigned long)(crashk_res.start >> 20));
  420. }
  421. #else /* !defined(CONFIG_KEXEC) */
  422. static void __init mips_parse_crashkernel(void)
  423. {
  424. }
  425. static void __init request_crashkernel(struct resource *res)
  426. {
  427. }
  428. #endif /* !defined(CONFIG_KEXEC) */
  429. static void __init check_kernel_sections_mem(void)
  430. {
  431. phys_addr_t start = __pa_symbol(&_text);
  432. phys_addr_t size = __pa_symbol(&_end) - start;
  433. if (!memblock_is_region_memory(start, size)) {
  434. pr_info("Kernel sections are not in the memory maps\n");
  435. memblock_add(start, size);
  436. }
  437. }
  438. static void __init bootcmdline_append(const char *s, size_t max)
  439. {
  440. if (!s[0] || !max)
  441. return;
  442. if (boot_command_line[0])
  443. strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
  444. strlcat(boot_command_line, s, max);
  445. }
  446. #ifdef CONFIG_OF_EARLY_FLATTREE
  447. static int __init bootcmdline_scan_chosen(unsigned long node, const char *uname,
  448. int depth, void *data)
  449. {
  450. bool *dt_bootargs = data;
  451. const char *p;
  452. int l;
  453. if (depth != 1 || !data ||
  454. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  455. return 0;
  456. p = of_get_flat_dt_prop(node, "bootargs", &l);
  457. if (p != NULL && l > 0) {
  458. bootcmdline_append(p, min(l, COMMAND_LINE_SIZE));
  459. *dt_bootargs = true;
  460. }
  461. return 1;
  462. }
  463. #endif /* CONFIG_OF_EARLY_FLATTREE */
  464. static void __init bootcmdline_init(void)
  465. {
  466. bool dt_bootargs = false;
  467. /*
  468. * If CMDLINE_OVERRIDE is enabled then initializing the command line is
  469. * trivial - we simply use the built-in command line unconditionally &
  470. * unmodified.
  471. */
  472. if (IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
  473. strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  474. return;
  475. }
  476. /*
  477. * If the user specified a built-in command line &
  478. * MIPS_CMDLINE_BUILTIN_EXTEND, then the built-in command line is
  479. * prepended to arguments from the bootloader or DT so we'll copy them
  480. * to the start of boot_command_line here. Otherwise, empty
  481. * boot_command_line to undo anything early_init_dt_scan_chosen() did.
  482. */
  483. if (IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND))
  484. strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  485. else
  486. boot_command_line[0] = 0;
  487. #ifdef CONFIG_OF_EARLY_FLATTREE
  488. /*
  489. * If we're configured to take boot arguments from DT, look for those
  490. * now.
  491. */
  492. if (IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_DTB) ||
  493. IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND))
  494. of_scan_flat_dt(bootcmdline_scan_chosen, &dt_bootargs);
  495. #endif
  496. /*
  497. * If we didn't get any arguments from DT (regardless of whether that's
  498. * because we weren't configured to look for them, or because we looked
  499. * & found none) then we'll take arguments from the bootloader.
  500. * plat_mem_setup() should have filled arcs_cmdline with arguments from
  501. * the bootloader.
  502. */
  503. if (IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND) || !dt_bootargs)
  504. bootcmdline_append(arcs_cmdline, COMMAND_LINE_SIZE);
  505. /*
  506. * If the user specified a built-in command line & we didn't already
  507. * prepend it, we append it to boot_command_line here.
  508. */
  509. if (IS_ENABLED(CONFIG_CMDLINE_BOOL) &&
  510. !IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND))
  511. bootcmdline_append(builtin_cmdline, COMMAND_LINE_SIZE);
  512. }
  513. /*
  514. * arch_mem_init - initialize memory management subsystem
  515. *
  516. * o plat_mem_setup() detects the memory configuration and will record detected
  517. * memory areas using memblock_add.
  518. *
  519. * At this stage the memory configuration of the system is known to the
  520. * kernel but generic memory management system is still entirely uninitialized.
  521. *
  522. * o bootmem_init()
  523. * o sparse_init()
  524. * o paging_init()
  525. * o dma_contiguous_reserve()
  526. *
  527. * At this stage the bootmem allocator is ready to use.
  528. *
  529. * NOTE: historically plat_mem_setup did the entire platform initialization.
  530. * This was rather impractical because it meant plat_mem_setup had to
  531. * get away without any kind of memory allocator. To keep old code from
  532. * breaking plat_setup was just renamed to plat_mem_setup and a second platform
  533. * initialization hook for anything else was introduced.
  534. */
  535. static void __init arch_mem_init(char **cmdline_p)
  536. {
  537. /* call board setup routine */
  538. plat_mem_setup();
  539. memblock_set_bottom_up(true);
  540. bootcmdline_init();
  541. strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  542. *cmdline_p = command_line;
  543. parse_early_param();
  544. if (usermem)
  545. pr_info("User-defined physical RAM map overwrite\n");
  546. check_kernel_sections_mem();
  547. early_init_fdt_reserve_self();
  548. early_init_fdt_scan_reserved_mem();
  549. #ifndef CONFIG_NUMA
  550. memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
  551. #endif
  552. bootmem_init();
  553. /*
  554. * Prevent memblock from allocating high memory.
  555. * This cannot be done before max_low_pfn is detected, so up
  556. * to this point is possible to only reserve physical memory
  557. * with memblock_reserve; memblock_alloc* can be used
  558. * only after this point
  559. */
  560. memblock_set_current_limit(PFN_PHYS(max_low_pfn));
  561. mips_reserve_vmcore();
  562. mips_parse_crashkernel();
  563. device_tree_init();
  564. /*
  565. * In order to reduce the possibility of kernel panic when failed to
  566. * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
  567. * low memory as small as possible before plat_swiotlb_setup(), so
  568. * make sparse_init() using top-down allocation.
  569. */
  570. memblock_set_bottom_up(false);
  571. sparse_init();
  572. memblock_set_bottom_up(true);
  573. plat_swiotlb_setup();
  574. dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
  575. /* Reserve for hibernation. */
  576. memblock_reserve(__pa_symbol(&__nosave_begin),
  577. __pa_symbol(&__nosave_end) - __pa_symbol(&__nosave_begin));
  578. early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
  579. }
  580. static void __init resource_init(void)
  581. {
  582. phys_addr_t start, end;
  583. u64 i;
  584. if (UNCAC_BASE != IO_BASE)
  585. return;
  586. code_resource.start = __pa_symbol(&_text);
  587. code_resource.end = __pa_symbol(&_etext) - 1;
  588. data_resource.start = __pa_symbol(&_etext);
  589. data_resource.end = __pa_symbol(&_edata) - 1;
  590. bss_resource.start = __pa_symbol(&__bss_start);
  591. bss_resource.end = __pa_symbol(&__bss_stop) - 1;
  592. for_each_mem_range(i, &start, &end) {
  593. struct resource *res;
  594. res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
  595. if (!res)
  596. panic("%s: Failed to allocate %zu bytes\n", __func__,
  597. sizeof(struct resource));
  598. res->start = start;
  599. /*
  600. * In memblock, end points to the first byte after the
  601. * range while in resourses, end points to the last byte in
  602. * the range.
  603. */
  604. res->end = end - 1;
  605. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  606. res->name = "System RAM";
  607. request_resource(&iomem_resource, res);
  608. /*
  609. * We don't know which RAM region contains kernel data,
  610. * so we try it repeatedly and let the resource manager
  611. * test it.
  612. */
  613. request_resource(res, &code_resource);
  614. request_resource(res, &data_resource);
  615. request_resource(res, &bss_resource);
  616. request_crashkernel(res);
  617. }
  618. }
  619. #ifdef CONFIG_SMP
  620. static void __init prefill_possible_map(void)
  621. {
  622. int i, possible = num_possible_cpus();
  623. if (possible > nr_cpu_ids)
  624. possible = nr_cpu_ids;
  625. for (i = 0; i < possible; i++)
  626. set_cpu_possible(i, true);
  627. for (; i < NR_CPUS; i++)
  628. set_cpu_possible(i, false);
  629. set_nr_cpu_ids(possible);
  630. }
  631. #else
  632. static inline void prefill_possible_map(void) {}
  633. #endif
  634. static void __init setup_rng_seed(void)
  635. {
  636. char *rng_seed_hex = fw_getenv("rngseed");
  637. u8 rng_seed[512];
  638. size_t len;
  639. if (!rng_seed_hex)
  640. return;
  641. len = min(sizeof(rng_seed), strlen(rng_seed_hex) / 2);
  642. if (hex2bin(rng_seed, rng_seed_hex, len))
  643. return;
  644. add_bootloader_randomness(rng_seed, len);
  645. memzero_explicit(rng_seed, len);
  646. memzero_explicit(rng_seed_hex, len * 2);
  647. }
  648. void __init setup_arch(char **cmdline_p)
  649. {
  650. cpu_probe();
  651. mips_cm_probe();
  652. prom_init();
  653. setup_early_fdc_console();
  654. #ifdef CONFIG_EARLY_PRINTK
  655. setup_early_printk();
  656. #endif
  657. cpu_report();
  658. check_bugs_early();
  659. #if defined(CONFIG_VT)
  660. #if defined(CONFIG_VGA_CONSOLE)
  661. conswitchp = &vga_con;
  662. #endif
  663. #endif
  664. arch_mem_init(cmdline_p);
  665. dmi_setup();
  666. resource_init();
  667. plat_smp_setup();
  668. prefill_possible_map();
  669. cpu_cache_init();
  670. paging_init();
  671. memblock_dump_all();
  672. setup_rng_seed();
  673. }
  674. unsigned long kernelsp[NR_CPUS];
  675. unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
  676. #ifdef CONFIG_DEBUG_FS
  677. struct dentry *mips_debugfs_dir;
  678. static int __init debugfs_mips(void)
  679. {
  680. mips_debugfs_dir = debugfs_create_dir("mips", NULL);
  681. return 0;
  682. }
  683. arch_initcall(debugfs_mips);
  684. #endif
  685. #ifdef CONFIG_DMA_NONCOHERENT
  686. static int __init setcoherentio(char *str)
  687. {
  688. dma_default_coherent = true;
  689. pr_info("Hardware DMA cache coherency (command line)\n");
  690. return 0;
  691. }
  692. early_param("coherentio", setcoherentio);
  693. static int __init setnocoherentio(char *str)
  694. {
  695. dma_default_coherent = false;
  696. pr_info("Software DMA cache coherency (command line)\n");
  697. return 0;
  698. }
  699. early_param("nocoherentio", setnocoherentio);
  700. #endif
  701. void __init arch_cpu_finalize_init(void)
  702. {
  703. unsigned int cpu = smp_processor_id();
  704. cpu_data[cpu].udelay_val = loops_per_jiffy;
  705. check_bugs32();
  706. if (IS_ENABLED(CONFIG_CPU_R4X00_BUGS64))
  707. check_bugs64();
  708. }