setup.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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/ioport.h>
  15. #include <linux/export.h>
  16. #include <linux/screen_info.h>
  17. #include <linux/memblock.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/initrd.h>
  20. #include <linux/root_dev.h>
  21. #include <linux/highmem.h>
  22. #include <linux/console.h>
  23. #include <linux/pfn.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/kexec.h>
  26. #include <linux/sizes.h>
  27. #include <linux/device.h>
  28. #include <linux/dma-contiguous.h>
  29. #include <linux/decompress/generic.h>
  30. #include <linux/of_fdt.h>
  31. #include <asm/addrspace.h>
  32. #include <asm/bootinfo.h>
  33. #include <asm/bugs.h>
  34. #include <asm/cache.h>
  35. #include <asm/cdmm.h>
  36. #include <asm/cpu.h>
  37. #include <asm/debug.h>
  38. #include <asm/sections.h>
  39. #include <asm/setup.h>
  40. #include <asm/smp-ops.h>
  41. #include <asm/prom.h>
  42. #ifdef CONFIG_MIPS_ELF_APPENDED_DTB
  43. const char __section(.appended_dtb) __appended_dtb[0x100000];
  44. #endif /* CONFIG_MIPS_ELF_APPENDED_DTB */
  45. struct cpuinfo_mips cpu_data[NR_CPUS] __read_mostly;
  46. EXPORT_SYMBOL(cpu_data);
  47. #ifdef CONFIG_VT
  48. struct screen_info screen_info;
  49. #endif
  50. /*
  51. * Setup information
  52. *
  53. * These are initialized so they are in the .data section
  54. */
  55. unsigned long mips_machtype __read_mostly = MACH_UNKNOWN;
  56. EXPORT_SYMBOL(mips_machtype);
  57. struct boot_mem_map boot_mem_map;
  58. static char __initdata command_line[COMMAND_LINE_SIZE];
  59. char __initdata arcs_cmdline[COMMAND_LINE_SIZE];
  60. #ifdef CONFIG_CMDLINE_BOOL
  61. static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
  62. #endif
  63. /*
  64. * mips_io_port_base is the begin of the address space to which x86 style
  65. * I/O ports are mapped.
  66. */
  67. const unsigned long mips_io_port_base = -1;
  68. EXPORT_SYMBOL(mips_io_port_base);
  69. static struct resource code_resource = { .name = "Kernel code", };
  70. static struct resource data_resource = { .name = "Kernel data", };
  71. static struct resource bss_resource = { .name = "Kernel bss", };
  72. static void *detect_magic __initdata = detect_memory_region;
  73. void __init add_memory_region(phys_addr_t start, phys_addr_t size, long type)
  74. {
  75. int x = boot_mem_map.nr_map;
  76. int i;
  77. /*
  78. * If the region reaches the top of the physical address space, adjust
  79. * the size slightly so that (start + size) doesn't overflow
  80. */
  81. if (start + size - 1 == (phys_addr_t)ULLONG_MAX)
  82. --size;
  83. /* Sanity check */
  84. if (start + size < start) {
  85. pr_warn("Trying to add an invalid memory region, skipped\n");
  86. return;
  87. }
  88. /*
  89. * Try to merge with existing entry, if any.
  90. */
  91. for (i = 0; i < boot_mem_map.nr_map; i++) {
  92. struct boot_mem_map_entry *entry = boot_mem_map.map + i;
  93. unsigned long top;
  94. if (entry->type != type)
  95. continue;
  96. if (start + size < entry->addr)
  97. continue; /* no overlap */
  98. if (entry->addr + entry->size < start)
  99. continue; /* no overlap */
  100. top = max(entry->addr + entry->size, start + size);
  101. entry->addr = min(entry->addr, start);
  102. entry->size = top - entry->addr;
  103. return;
  104. }
  105. if (boot_mem_map.nr_map == BOOT_MEM_MAP_MAX) {
  106. pr_err("Ooops! Too many entries in the memory map!\n");
  107. return;
  108. }
  109. boot_mem_map.map[x].addr = start;
  110. boot_mem_map.map[x].size = size;
  111. boot_mem_map.map[x].type = type;
  112. boot_mem_map.nr_map++;
  113. }
  114. void __init detect_memory_region(phys_addr_t start, phys_addr_t sz_min, phys_addr_t sz_max)
  115. {
  116. void *dm = &detect_magic;
  117. phys_addr_t size;
  118. for (size = sz_min; size < sz_max; size <<= 1) {
  119. if (!memcmp(dm, dm + size, sizeof(detect_magic)))
  120. break;
  121. }
  122. pr_debug("Memory: %lluMB of RAM detected at 0x%llx (min: %lluMB, max: %lluMB)\n",
  123. ((unsigned long long) size) / SZ_1M,
  124. (unsigned long long) start,
  125. ((unsigned long long) sz_min) / SZ_1M,
  126. ((unsigned long long) sz_max) / SZ_1M);
  127. add_memory_region(start, size, BOOT_MEM_RAM);
  128. }
  129. bool __init memory_region_available(phys_addr_t start, phys_addr_t size)
  130. {
  131. int i;
  132. bool in_ram = false, free = true;
  133. for (i = 0; i < boot_mem_map.nr_map; i++) {
  134. phys_addr_t start_, end_;
  135. start_ = boot_mem_map.map[i].addr;
  136. end_ = boot_mem_map.map[i].addr + boot_mem_map.map[i].size;
  137. switch (boot_mem_map.map[i].type) {
  138. case BOOT_MEM_RAM:
  139. if (start >= start_ && start + size <= end_)
  140. in_ram = true;
  141. break;
  142. case BOOT_MEM_RESERVED:
  143. if ((start >= start_ && start < end_) ||
  144. (start < start_ && start + size >= start_))
  145. free = false;
  146. break;
  147. default:
  148. continue;
  149. }
  150. }
  151. return in_ram && free;
  152. }
  153. static void __init print_memory_map(void)
  154. {
  155. int i;
  156. const int field = 2 * sizeof(unsigned long);
  157. for (i = 0; i < boot_mem_map.nr_map; i++) {
  158. printk(KERN_INFO " memory: %0*Lx @ %0*Lx ",
  159. field, (unsigned long long) boot_mem_map.map[i].size,
  160. field, (unsigned long long) boot_mem_map.map[i].addr);
  161. switch (boot_mem_map.map[i].type) {
  162. case BOOT_MEM_RAM:
  163. printk(KERN_CONT "(usable)\n");
  164. break;
  165. case BOOT_MEM_INIT_RAM:
  166. printk(KERN_CONT "(usable after init)\n");
  167. break;
  168. case BOOT_MEM_ROM_DATA:
  169. printk(KERN_CONT "(ROM data)\n");
  170. break;
  171. case BOOT_MEM_RESERVED:
  172. printk(KERN_CONT "(reserved)\n");
  173. break;
  174. default:
  175. printk(KERN_CONT "type %lu\n", boot_mem_map.map[i].type);
  176. break;
  177. }
  178. }
  179. }
  180. /*
  181. * Manage initrd
  182. */
  183. #ifdef CONFIG_BLK_DEV_INITRD
  184. static int __init rd_start_early(char *p)
  185. {
  186. unsigned long start = memparse(p, &p);
  187. #ifdef CONFIG_64BIT
  188. /* Guess if the sign extension was forgotten by bootloader */
  189. if (start < XKPHYS)
  190. start = (int)start;
  191. #endif
  192. initrd_start = start;
  193. initrd_end += start;
  194. return 0;
  195. }
  196. early_param("rd_start", rd_start_early);
  197. static int __init rd_size_early(char *p)
  198. {
  199. initrd_end += memparse(p, &p);
  200. return 0;
  201. }
  202. early_param("rd_size", rd_size_early);
  203. /* it returns the next free pfn after initrd */
  204. static unsigned long __init init_initrd(void)
  205. {
  206. unsigned long end;
  207. /*
  208. * Board specific code or command line parser should have
  209. * already set up initrd_start and initrd_end. In these cases
  210. * perfom sanity checks and use them if all looks good.
  211. */
  212. if (!initrd_start || initrd_end <= initrd_start)
  213. goto disable;
  214. if (initrd_start & ~PAGE_MASK) {
  215. pr_err("initrd start must be page aligned\n");
  216. goto disable;
  217. }
  218. if (initrd_start < PAGE_OFFSET) {
  219. pr_err("initrd start < PAGE_OFFSET\n");
  220. goto disable;
  221. }
  222. /*
  223. * Sanitize initrd addresses. For example firmware
  224. * can't guess if they need to pass them through
  225. * 64-bits values if the kernel has been built in pure
  226. * 32-bit. We need also to switch from KSEG0 to XKPHYS
  227. * addresses now, so the code can now safely use __pa().
  228. */
  229. end = __pa(initrd_end);
  230. initrd_end = (unsigned long)__va(end);
  231. initrd_start = (unsigned long)__va(__pa(initrd_start));
  232. ROOT_DEV = Root_RAM0;
  233. return PFN_UP(end);
  234. disable:
  235. initrd_start = 0;
  236. initrd_end = 0;
  237. return 0;
  238. }
  239. /* In some conditions (e.g. big endian bootloader with a little endian
  240. kernel), the initrd might appear byte swapped. Try to detect this and
  241. byte swap it if needed. */
  242. static void __init maybe_bswap_initrd(void)
  243. {
  244. #if defined(CONFIG_CPU_CAVIUM_OCTEON)
  245. u64 buf;
  246. /* Check for CPIO signature */
  247. if (!memcmp((void *)initrd_start, "070701", 6))
  248. return;
  249. /* Check for compressed initrd */
  250. if (decompress_method((unsigned char *)initrd_start, 8, NULL))
  251. return;
  252. /* Try again with a byte swapped header */
  253. buf = swab64p((u64 *)initrd_start);
  254. if (!memcmp(&buf, "070701", 6) ||
  255. decompress_method((unsigned char *)(&buf), 8, NULL)) {
  256. unsigned long i;
  257. pr_info("Byteswapped initrd detected\n");
  258. for (i = initrd_start; i < ALIGN(initrd_end, 8); i += 8)
  259. swab64s((u64 *)i);
  260. }
  261. #endif
  262. }
  263. static void __init finalize_initrd(void)
  264. {
  265. unsigned long size = initrd_end - initrd_start;
  266. if (size == 0) {
  267. printk(KERN_INFO "Initrd not found or empty");
  268. goto disable;
  269. }
  270. if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
  271. printk(KERN_ERR "Initrd extends beyond end of memory");
  272. goto disable;
  273. }
  274. maybe_bswap_initrd();
  275. reserve_bootmem(__pa(initrd_start), size, BOOTMEM_DEFAULT);
  276. initrd_below_start_ok = 1;
  277. pr_info("Initial ramdisk at: 0x%lx (%lu bytes)\n",
  278. initrd_start, size);
  279. return;
  280. disable:
  281. printk(KERN_CONT " - disabling initrd\n");
  282. initrd_start = 0;
  283. initrd_end = 0;
  284. }
  285. #else /* !CONFIG_BLK_DEV_INITRD */
  286. static unsigned long __init init_initrd(void)
  287. {
  288. return 0;
  289. }
  290. #define finalize_initrd() do {} while (0)
  291. #endif
  292. /*
  293. * Initialize the bootmem allocator. It also setup initrd related data
  294. * if needed.
  295. */
  296. #if defined(CONFIG_SGI_IP27) || (defined(CONFIG_CPU_LOONGSON3) && defined(CONFIG_NUMA))
  297. static void __init bootmem_init(void)
  298. {
  299. init_initrd();
  300. finalize_initrd();
  301. }
  302. #else /* !CONFIG_SGI_IP27 */
  303. static unsigned long __init bootmap_bytes(unsigned long pages)
  304. {
  305. unsigned long bytes = DIV_ROUND_UP(pages, 8);
  306. return ALIGN(bytes, sizeof(long));
  307. }
  308. static void __init bootmem_init(void)
  309. {
  310. unsigned long reserved_end;
  311. unsigned long mapstart = ~0UL;
  312. unsigned long bootmap_size;
  313. phys_addr_t ramstart = (phys_addr_t)ULLONG_MAX;
  314. bool bootmap_valid = false;
  315. int i;
  316. /*
  317. * Sanity check any INITRD first. We don't take it into account
  318. * for bootmem setup initially, rely on the end-of-kernel-code
  319. * as our memory range starting point. Once bootmem is inited we
  320. * will reserve the area used for the initrd.
  321. */
  322. init_initrd();
  323. reserved_end = (unsigned long) PFN_UP(__pa_symbol(&_end));
  324. /*
  325. * max_low_pfn is not a number of pages. The number of pages
  326. * of the system is given by 'max_low_pfn - min_low_pfn'.
  327. */
  328. min_low_pfn = ~0UL;
  329. max_low_pfn = 0;
  330. /*
  331. * Find the highest page frame number we have available
  332. * and the lowest used RAM address
  333. */
  334. for (i = 0; i < boot_mem_map.nr_map; i++) {
  335. unsigned long start, end;
  336. if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
  337. continue;
  338. start = PFN_UP(boot_mem_map.map[i].addr);
  339. end = PFN_DOWN(boot_mem_map.map[i].addr
  340. + boot_mem_map.map[i].size);
  341. ramstart = min(ramstart, boot_mem_map.map[i].addr);
  342. #ifndef CONFIG_HIGHMEM
  343. /*
  344. * Skip highmem here so we get an accurate max_low_pfn if low
  345. * memory stops short of high memory.
  346. * If the region overlaps HIGHMEM_START, end is clipped so
  347. * max_pfn excludes the highmem portion.
  348. */
  349. if (start >= PFN_DOWN(HIGHMEM_START))
  350. continue;
  351. if (end > PFN_DOWN(HIGHMEM_START))
  352. end = PFN_DOWN(HIGHMEM_START);
  353. #endif
  354. if (end > max_low_pfn)
  355. max_low_pfn = end;
  356. if (start < min_low_pfn)
  357. min_low_pfn = start;
  358. if (end <= reserved_end)
  359. continue;
  360. #ifdef CONFIG_BLK_DEV_INITRD
  361. /* Skip zones before initrd and initrd itself */
  362. if (initrd_end && end <= (unsigned long)PFN_UP(__pa(initrd_end)))
  363. continue;
  364. #endif
  365. if (start >= mapstart)
  366. continue;
  367. mapstart = max(reserved_end, start);
  368. }
  369. /*
  370. * Reserve any memory between the start of RAM and PHYS_OFFSET
  371. */
  372. if (ramstart > PHYS_OFFSET)
  373. add_memory_region(PHYS_OFFSET, ramstart - PHYS_OFFSET,
  374. BOOT_MEM_RESERVED);
  375. if (min_low_pfn >= max_low_pfn)
  376. panic("Incorrect memory mapping !!!");
  377. if (min_low_pfn > ARCH_PFN_OFFSET) {
  378. pr_info("Wasting %lu bytes for tracking %lu unused pages\n",
  379. (min_low_pfn - ARCH_PFN_OFFSET) * sizeof(struct page),
  380. min_low_pfn - ARCH_PFN_OFFSET);
  381. } else if (min_low_pfn < ARCH_PFN_OFFSET) {
  382. pr_info("%lu free pages won't be used\n",
  383. ARCH_PFN_OFFSET - min_low_pfn);
  384. }
  385. min_low_pfn = ARCH_PFN_OFFSET;
  386. /*
  387. * Determine low and high memory ranges
  388. */
  389. max_pfn = max_low_pfn;
  390. if (max_low_pfn > PFN_DOWN(HIGHMEM_START)) {
  391. #ifdef CONFIG_HIGHMEM
  392. highstart_pfn = PFN_DOWN(HIGHMEM_START);
  393. highend_pfn = max_low_pfn;
  394. #endif
  395. max_low_pfn = PFN_DOWN(HIGHMEM_START);
  396. }
  397. #ifdef CONFIG_BLK_DEV_INITRD
  398. /*
  399. * mapstart should be after initrd_end
  400. */
  401. if (initrd_end)
  402. mapstart = max(mapstart, (unsigned long)PFN_UP(__pa(initrd_end)));
  403. #endif
  404. /*
  405. * check that mapstart doesn't overlap with any of
  406. * memory regions that have been reserved through eg. DTB
  407. */
  408. bootmap_size = bootmap_bytes(max_low_pfn - min_low_pfn);
  409. bootmap_valid = memory_region_available(PFN_PHYS(mapstart),
  410. bootmap_size);
  411. for (i = 0; i < boot_mem_map.nr_map && !bootmap_valid; i++) {
  412. unsigned long mapstart_addr;
  413. switch (boot_mem_map.map[i].type) {
  414. case BOOT_MEM_RESERVED:
  415. mapstart_addr = PFN_ALIGN(boot_mem_map.map[i].addr +
  416. boot_mem_map.map[i].size);
  417. if (PHYS_PFN(mapstart_addr) < mapstart)
  418. break;
  419. bootmap_valid = memory_region_available(mapstart_addr,
  420. bootmap_size);
  421. if (bootmap_valid)
  422. mapstart = PHYS_PFN(mapstart_addr);
  423. break;
  424. default:
  425. break;
  426. }
  427. }
  428. if (!bootmap_valid)
  429. panic("No memory area to place a bootmap bitmap");
  430. /*
  431. * Initialize the boot-time allocator with low memory only.
  432. */
  433. if (bootmap_size != init_bootmem_node(NODE_DATA(0), mapstart,
  434. min_low_pfn, max_low_pfn))
  435. panic("Unexpected memory size required for bootmap");
  436. for (i = 0; i < boot_mem_map.nr_map; i++) {
  437. unsigned long start, end;
  438. start = PFN_UP(boot_mem_map.map[i].addr);
  439. end = PFN_DOWN(boot_mem_map.map[i].addr
  440. + boot_mem_map.map[i].size);
  441. if (start <= min_low_pfn)
  442. start = min_low_pfn;
  443. if (start >= end)
  444. continue;
  445. #ifndef CONFIG_HIGHMEM
  446. if (end > max_low_pfn)
  447. end = max_low_pfn;
  448. /*
  449. * ... finally, is the area going away?
  450. */
  451. if (end <= start)
  452. continue;
  453. #endif
  454. memblock_add_node(PFN_PHYS(start), PFN_PHYS(end - start), 0);
  455. }
  456. /*
  457. * Register fully available low RAM pages with the bootmem allocator.
  458. */
  459. for (i = 0; i < boot_mem_map.nr_map; i++) {
  460. unsigned long start, end, size;
  461. start = PFN_UP(boot_mem_map.map[i].addr);
  462. end = PFN_DOWN(boot_mem_map.map[i].addr
  463. + boot_mem_map.map[i].size);
  464. /*
  465. * Reserve usable memory.
  466. */
  467. switch (boot_mem_map.map[i].type) {
  468. case BOOT_MEM_RAM:
  469. break;
  470. case BOOT_MEM_INIT_RAM:
  471. memory_present(0, start, end);
  472. continue;
  473. default:
  474. /* Not usable memory */
  475. if (start > min_low_pfn && end < max_low_pfn)
  476. reserve_bootmem(boot_mem_map.map[i].addr,
  477. boot_mem_map.map[i].size,
  478. BOOTMEM_DEFAULT);
  479. continue;
  480. }
  481. /*
  482. * We are rounding up the start address of usable memory
  483. * and at the end of the usable range downwards.
  484. */
  485. if (start >= max_low_pfn)
  486. continue;
  487. if (start < reserved_end)
  488. start = reserved_end;
  489. if (end > max_low_pfn)
  490. end = max_low_pfn;
  491. /*
  492. * ... finally, is the area going away?
  493. */
  494. if (end <= start)
  495. continue;
  496. size = end - start;
  497. /* Register lowmem ranges */
  498. free_bootmem(PFN_PHYS(start), size << PAGE_SHIFT);
  499. memory_present(0, start, end);
  500. }
  501. /*
  502. * Reserve the bootmap memory.
  503. */
  504. reserve_bootmem(PFN_PHYS(mapstart), bootmap_size, BOOTMEM_DEFAULT);
  505. #ifdef CONFIG_RELOCATABLE
  506. /*
  507. * The kernel reserves all memory below its _end symbol as bootmem,
  508. * but the kernel may now be at a much higher address. The memory
  509. * between the original and new locations may be returned to the system.
  510. */
  511. if (__pa_symbol(_text) > __pa_symbol(VMLINUX_LOAD_ADDRESS)) {
  512. unsigned long offset;
  513. extern void show_kernel_relocation(const char *level);
  514. offset = __pa_symbol(_text) - __pa_symbol(VMLINUX_LOAD_ADDRESS);
  515. free_bootmem(__pa_symbol(VMLINUX_LOAD_ADDRESS), offset);
  516. #if defined(CONFIG_DEBUG_KERNEL) && defined(CONFIG_DEBUG_INFO)
  517. /*
  518. * This information is necessary when debugging the kernel
  519. * But is a security vulnerability otherwise!
  520. */
  521. show_kernel_relocation(KERN_INFO);
  522. #endif
  523. }
  524. #endif
  525. /*
  526. * Reserve initrd memory if needed.
  527. */
  528. finalize_initrd();
  529. }
  530. #endif /* CONFIG_SGI_IP27 */
  531. /*
  532. * arch_mem_init - initialize memory management subsystem
  533. *
  534. * o plat_mem_setup() detects the memory configuration and will record detected
  535. * memory areas using add_memory_region.
  536. *
  537. * At this stage the memory configuration of the system is known to the
  538. * kernel but generic memory management system is still entirely uninitialized.
  539. *
  540. * o bootmem_init()
  541. * o sparse_init()
  542. * o paging_init()
  543. * o dma_contiguous_reserve()
  544. *
  545. * At this stage the bootmem allocator is ready to use.
  546. *
  547. * NOTE: historically plat_mem_setup did the entire platform initialization.
  548. * This was rather impractical because it meant plat_mem_setup had to
  549. * get away without any kind of memory allocator. To keep old code from
  550. * breaking plat_setup was just renamed to plat_mem_setup and a second platform
  551. * initialization hook for anything else was introduced.
  552. */
  553. static int usermem __initdata;
  554. static int __init early_parse_mem(char *p)
  555. {
  556. phys_addr_t start, size;
  557. /*
  558. * If a user specifies memory size, we
  559. * blow away any automatically generated
  560. * size.
  561. */
  562. if (usermem == 0) {
  563. boot_mem_map.nr_map = 0;
  564. usermem = 1;
  565. }
  566. start = 0;
  567. size = memparse(p, &p);
  568. if (*p == '@')
  569. start = memparse(p + 1, &p);
  570. add_memory_region(start, size, BOOT_MEM_RAM);
  571. return 0;
  572. }
  573. early_param("mem", early_parse_mem);
  574. static int __init early_parse_memmap(char *p)
  575. {
  576. char *oldp;
  577. u64 start_at, mem_size;
  578. if (!p)
  579. return -EINVAL;
  580. if (!strncmp(p, "exactmap", 8)) {
  581. pr_err("\"memmap=exactmap\" invalid on MIPS\n");
  582. return 0;
  583. }
  584. oldp = p;
  585. mem_size = memparse(p, &p);
  586. if (p == oldp)
  587. return -EINVAL;
  588. if (*p == '@') {
  589. start_at = memparse(p+1, &p);
  590. add_memory_region(start_at, mem_size, BOOT_MEM_RAM);
  591. } else if (*p == '#') {
  592. pr_err("\"memmap=nn#ss\" (force ACPI data) invalid on MIPS\n");
  593. return -EINVAL;
  594. } else if (*p == '$') {
  595. start_at = memparse(p+1, &p);
  596. add_memory_region(start_at, mem_size, BOOT_MEM_RESERVED);
  597. } else {
  598. pr_err("\"memmap\" invalid format!\n");
  599. return -EINVAL;
  600. }
  601. if (*p == '\0') {
  602. usermem = 1;
  603. return 0;
  604. } else
  605. return -EINVAL;
  606. }
  607. early_param("memmap", early_parse_memmap);
  608. #ifdef CONFIG_PROC_VMCORE
  609. unsigned long setup_elfcorehdr, setup_elfcorehdr_size;
  610. static int __init early_parse_elfcorehdr(char *p)
  611. {
  612. int i;
  613. setup_elfcorehdr = memparse(p, &p);
  614. for (i = 0; i < boot_mem_map.nr_map; i++) {
  615. unsigned long start = boot_mem_map.map[i].addr;
  616. unsigned long end = (boot_mem_map.map[i].addr +
  617. boot_mem_map.map[i].size);
  618. if (setup_elfcorehdr >= start && setup_elfcorehdr < end) {
  619. /*
  620. * Reserve from the elf core header to the end of
  621. * the memory segment, that should all be kdump
  622. * reserved memory.
  623. */
  624. setup_elfcorehdr_size = end - setup_elfcorehdr;
  625. break;
  626. }
  627. }
  628. /*
  629. * If we don't find it in the memory map, then we shouldn't
  630. * have to worry about it, as the new kernel won't use it.
  631. */
  632. return 0;
  633. }
  634. early_param("elfcorehdr", early_parse_elfcorehdr);
  635. #endif
  636. static void __init arch_mem_addpart(phys_addr_t mem, phys_addr_t end, int type)
  637. {
  638. phys_addr_t size;
  639. int i;
  640. size = end - mem;
  641. if (!size)
  642. return;
  643. /* Make sure it is in the boot_mem_map */
  644. for (i = 0; i < boot_mem_map.nr_map; i++) {
  645. if (mem >= boot_mem_map.map[i].addr &&
  646. mem < (boot_mem_map.map[i].addr +
  647. boot_mem_map.map[i].size))
  648. return;
  649. }
  650. add_memory_region(mem, size, type);
  651. }
  652. #ifdef CONFIG_KEXEC
  653. static inline unsigned long long get_total_mem(void)
  654. {
  655. unsigned long long total;
  656. total = max_pfn - min_low_pfn;
  657. return total << PAGE_SHIFT;
  658. }
  659. static void __init mips_parse_crashkernel(void)
  660. {
  661. unsigned long long total_mem;
  662. unsigned long long crash_size, crash_base;
  663. int ret;
  664. total_mem = get_total_mem();
  665. ret = parse_crashkernel(boot_command_line, total_mem,
  666. &crash_size, &crash_base);
  667. if (ret != 0 || crash_size <= 0)
  668. return;
  669. if (!memory_region_available(crash_base, crash_size)) {
  670. pr_warn("Invalid memory region reserved for crash kernel\n");
  671. return;
  672. }
  673. crashk_res.start = crash_base;
  674. crashk_res.end = crash_base + crash_size - 1;
  675. }
  676. static void __init request_crashkernel(struct resource *res)
  677. {
  678. int ret;
  679. if (crashk_res.start == crashk_res.end)
  680. return;
  681. ret = request_resource(res, &crashk_res);
  682. if (!ret)
  683. pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
  684. (unsigned long)((crashk_res.end -
  685. crashk_res.start + 1) >> 20),
  686. (unsigned long)(crashk_res.start >> 20));
  687. }
  688. #else /* !defined(CONFIG_KEXEC) */
  689. static void __init mips_parse_crashkernel(void)
  690. {
  691. }
  692. static void __init request_crashkernel(struct resource *res)
  693. {
  694. }
  695. #endif /* !defined(CONFIG_KEXEC) */
  696. #define USE_PROM_CMDLINE IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_BOOTLOADER)
  697. #define USE_DTB_CMDLINE IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_DTB)
  698. #define EXTEND_WITH_PROM IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND)
  699. #define BUILTIN_EXTEND_WITH_PROM \
  700. IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND)
  701. static void __init arch_mem_init(char **cmdline_p)
  702. {
  703. struct memblock_region *reg;
  704. extern void plat_mem_setup(void);
  705. #if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
  706. strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  707. #else
  708. if ((USE_PROM_CMDLINE && arcs_cmdline[0]) ||
  709. (USE_DTB_CMDLINE && !boot_command_line[0]))
  710. strlcpy(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
  711. if (EXTEND_WITH_PROM && arcs_cmdline[0]) {
  712. if (boot_command_line[0])
  713. strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
  714. strlcat(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
  715. }
  716. #if defined(CONFIG_CMDLINE_BOOL)
  717. if (builtin_cmdline[0]) {
  718. if (boot_command_line[0])
  719. strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
  720. strlcat(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  721. }
  722. if (BUILTIN_EXTEND_WITH_PROM && arcs_cmdline[0]) {
  723. if (boot_command_line[0])
  724. strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
  725. strlcat(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
  726. }
  727. #endif
  728. #endif
  729. /* call board setup routine */
  730. plat_mem_setup();
  731. /*
  732. * Make sure all kernel memory is in the maps. The "UP" and
  733. * "DOWN" are opposite for initdata since if it crosses over
  734. * into another memory section you don't want that to be
  735. * freed when the initdata is freed.
  736. */
  737. arch_mem_addpart(PFN_DOWN(__pa_symbol(&_text)) << PAGE_SHIFT,
  738. PFN_UP(__pa_symbol(&_edata)) << PAGE_SHIFT,
  739. BOOT_MEM_RAM);
  740. arch_mem_addpart(PFN_UP(__pa_symbol(&__init_begin)) << PAGE_SHIFT,
  741. PFN_DOWN(__pa_symbol(&__init_end)) << PAGE_SHIFT,
  742. BOOT_MEM_INIT_RAM);
  743. pr_info("Determined physical RAM map:\n");
  744. print_memory_map();
  745. strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  746. *cmdline_p = command_line;
  747. parse_early_param();
  748. if (usermem) {
  749. pr_info("User-defined physical RAM map:\n");
  750. print_memory_map();
  751. }
  752. early_init_fdt_reserve_self();
  753. early_init_fdt_scan_reserved_mem();
  754. bootmem_init();
  755. #ifdef CONFIG_PROC_VMCORE
  756. if (setup_elfcorehdr && setup_elfcorehdr_size) {
  757. printk(KERN_INFO "kdump reserved memory at %lx-%lx\n",
  758. setup_elfcorehdr, setup_elfcorehdr_size);
  759. reserve_bootmem(setup_elfcorehdr, setup_elfcorehdr_size,
  760. BOOTMEM_DEFAULT);
  761. }
  762. #endif
  763. mips_parse_crashkernel();
  764. #ifdef CONFIG_KEXEC
  765. if (crashk_res.start != crashk_res.end)
  766. reserve_bootmem(crashk_res.start,
  767. crashk_res.end - crashk_res.start + 1,
  768. BOOTMEM_DEFAULT);
  769. #endif
  770. device_tree_init();
  771. sparse_init();
  772. plat_swiotlb_setup();
  773. dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
  774. /* Tell bootmem about cma reserved memblock section */
  775. for_each_memblock(reserved, reg)
  776. if (reg->size != 0)
  777. reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
  778. reserve_bootmem_region(__pa_symbol(&__nosave_begin),
  779. __pa_symbol(&__nosave_end)); /* Reserve for hibernation */
  780. }
  781. static void __init resource_init(void)
  782. {
  783. int i;
  784. if (UNCAC_BASE != IO_BASE)
  785. return;
  786. code_resource.start = __pa_symbol(&_text);
  787. code_resource.end = __pa_symbol(&_etext) - 1;
  788. data_resource.start = __pa_symbol(&_etext);
  789. data_resource.end = __pa_symbol(&_edata) - 1;
  790. bss_resource.start = __pa_symbol(&__bss_start);
  791. bss_resource.end = __pa_symbol(&__bss_stop) - 1;
  792. for (i = 0; i < boot_mem_map.nr_map; i++) {
  793. struct resource *res;
  794. unsigned long start, end;
  795. start = boot_mem_map.map[i].addr;
  796. end = boot_mem_map.map[i].addr + boot_mem_map.map[i].size - 1;
  797. if (start >= HIGHMEM_START)
  798. continue;
  799. if (end >= HIGHMEM_START)
  800. end = HIGHMEM_START - 1;
  801. res = alloc_bootmem(sizeof(struct resource));
  802. res->start = start;
  803. res->end = end;
  804. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  805. switch (boot_mem_map.map[i].type) {
  806. case BOOT_MEM_RAM:
  807. case BOOT_MEM_INIT_RAM:
  808. case BOOT_MEM_ROM_DATA:
  809. res->name = "System RAM";
  810. res->flags |= IORESOURCE_SYSRAM;
  811. break;
  812. case BOOT_MEM_RESERVED:
  813. default:
  814. res->name = "reserved";
  815. }
  816. request_resource(&iomem_resource, res);
  817. /*
  818. * We don't know which RAM region contains kernel data,
  819. * so we try it repeatedly and let the resource manager
  820. * test it.
  821. */
  822. request_resource(res, &code_resource);
  823. request_resource(res, &data_resource);
  824. request_resource(res, &bss_resource);
  825. request_crashkernel(res);
  826. }
  827. }
  828. #ifdef CONFIG_SMP
  829. static void __init prefill_possible_map(void)
  830. {
  831. int i, possible = num_possible_cpus();
  832. if (possible > nr_cpu_ids)
  833. possible = nr_cpu_ids;
  834. for (i = 0; i < possible; i++)
  835. set_cpu_possible(i, true);
  836. for (; i < NR_CPUS; i++)
  837. set_cpu_possible(i, false);
  838. nr_cpu_ids = possible;
  839. }
  840. #else
  841. static inline void prefill_possible_map(void) {}
  842. #endif
  843. void __init setup_arch(char **cmdline_p)
  844. {
  845. cpu_probe();
  846. mips_cm_probe();
  847. prom_init();
  848. setup_early_fdc_console();
  849. #ifdef CONFIG_EARLY_PRINTK
  850. setup_early_printk();
  851. #endif
  852. cpu_report();
  853. check_bugs_early();
  854. #if defined(CONFIG_VT)
  855. #if defined(CONFIG_VGA_CONSOLE)
  856. conswitchp = &vga_con;
  857. #elif defined(CONFIG_DUMMY_CONSOLE)
  858. conswitchp = &dummy_con;
  859. #endif
  860. #endif
  861. arch_mem_init(cmdline_p);
  862. resource_init();
  863. plat_smp_setup();
  864. prefill_possible_map();
  865. cpu_cache_init();
  866. paging_init();
  867. }
  868. unsigned long kernelsp[NR_CPUS];
  869. unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
  870. #ifdef CONFIG_USE_OF
  871. unsigned long fw_passed_dtb;
  872. #endif
  873. #ifdef CONFIG_DEBUG_FS
  874. struct dentry *mips_debugfs_dir;
  875. static int __init debugfs_mips(void)
  876. {
  877. struct dentry *d;
  878. d = debugfs_create_dir("mips", NULL);
  879. if (!d)
  880. return -ENOMEM;
  881. mips_debugfs_dir = d;
  882. return 0;
  883. }
  884. arch_initcall(debugfs_mips);
  885. #endif