setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 <asm/addrspace.h>
  26. #include <asm/bootinfo.h>
  27. #include <asm/bugs.h>
  28. #include <asm/cache.h>
  29. #include <asm/cpu.h>
  30. #include <asm/sections.h>
  31. #include <asm/setup.h>
  32. #include <asm/smp-ops.h>
  33. #include <asm/prom.h>
  34. struct cpuinfo_mips cpu_data[NR_CPUS] __read_mostly;
  35. EXPORT_SYMBOL(cpu_data);
  36. #ifdef CONFIG_VT
  37. struct screen_info screen_info;
  38. #endif
  39. /*
  40. * Despite it's name this variable is even if we don't have PCI
  41. */
  42. unsigned int PCI_DMA_BUS_IS_PHYS;
  43. EXPORT_SYMBOL(PCI_DMA_BUS_IS_PHYS);
  44. /*
  45. * Setup information
  46. *
  47. * These are initialized so they are in the .data section
  48. */
  49. unsigned long mips_machtype __read_mostly = MACH_UNKNOWN;
  50. EXPORT_SYMBOL(mips_machtype);
  51. struct boot_mem_map boot_mem_map;
  52. static char __initdata command_line[COMMAND_LINE_SIZE];
  53. char __initdata arcs_cmdline[COMMAND_LINE_SIZE];
  54. #ifdef CONFIG_CMDLINE_BOOL
  55. static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
  56. #endif
  57. /*
  58. * mips_io_port_base is the begin of the address space to which x86 style
  59. * I/O ports are mapped.
  60. */
  61. const unsigned long mips_io_port_base = -1;
  62. EXPORT_SYMBOL(mips_io_port_base);
  63. static struct resource code_resource = { .name = "Kernel code", };
  64. static struct resource data_resource = { .name = "Kernel data", };
  65. void __init add_memory_region(phys_t start, phys_t size, long type)
  66. {
  67. int x = boot_mem_map.nr_map;
  68. int i;
  69. /* Sanity check */
  70. if (start + size < start) {
  71. pr_warning("Trying to add an invalid memory region, skipped\n");
  72. return;
  73. }
  74. /*
  75. * Try to merge with existing entry, if any.
  76. */
  77. for (i = 0; i < boot_mem_map.nr_map; i++) {
  78. struct boot_mem_map_entry *entry = boot_mem_map.map + i;
  79. unsigned long top;
  80. if (entry->type != type)
  81. continue;
  82. if (start + size < entry->addr)
  83. continue; /* no overlap */
  84. if (entry->addr + entry->size < start)
  85. continue; /* no overlap */
  86. top = max(entry->addr + entry->size, start + size);
  87. entry->addr = min(entry->addr, start);
  88. entry->size = top - entry->addr;
  89. return;
  90. }
  91. if (boot_mem_map.nr_map == BOOT_MEM_MAP_MAX) {
  92. pr_err("Ooops! Too many entries in the memory map!\n");
  93. return;
  94. }
  95. boot_mem_map.map[x].addr = start;
  96. boot_mem_map.map[x].size = size;
  97. boot_mem_map.map[x].type = type;
  98. boot_mem_map.nr_map++;
  99. }
  100. static void __init print_memory_map(void)
  101. {
  102. int i;
  103. const int field = 2 * sizeof(unsigned long);
  104. for (i = 0; i < boot_mem_map.nr_map; i++) {
  105. printk(KERN_INFO " memory: %0*Lx @ %0*Lx ",
  106. field, (unsigned long long) boot_mem_map.map[i].size,
  107. field, (unsigned long long) boot_mem_map.map[i].addr);
  108. switch (boot_mem_map.map[i].type) {
  109. case BOOT_MEM_RAM:
  110. printk(KERN_CONT "(usable)\n");
  111. break;
  112. case BOOT_MEM_INIT_RAM:
  113. printk(KERN_CONT "(usable after init)\n");
  114. break;
  115. case BOOT_MEM_ROM_DATA:
  116. printk(KERN_CONT "(ROM data)\n");
  117. break;
  118. case BOOT_MEM_RESERVED:
  119. printk(KERN_CONT "(reserved)\n");
  120. break;
  121. default:
  122. printk(KERN_CONT "type %lu\n", boot_mem_map.map[i].type);
  123. break;
  124. }
  125. }
  126. }
  127. /*
  128. * Manage initrd
  129. */
  130. #ifdef CONFIG_BLK_DEV_INITRD
  131. static int __init rd_start_early(char *p)
  132. {
  133. unsigned long start = memparse(p, &p);
  134. #ifdef CONFIG_64BIT
  135. /* Guess if the sign extension was forgotten by bootloader */
  136. if (start < XKPHYS)
  137. start = (int)start;
  138. #endif
  139. initrd_start = start;
  140. initrd_end += start;
  141. return 0;
  142. }
  143. early_param("rd_start", rd_start_early);
  144. static int __init rd_size_early(char *p)
  145. {
  146. initrd_end += memparse(p, &p);
  147. return 0;
  148. }
  149. early_param("rd_size", rd_size_early);
  150. /* it returns the next free pfn after initrd */
  151. static unsigned long __init init_initrd(void)
  152. {
  153. unsigned long end;
  154. /*
  155. * Board specific code or command line parser should have
  156. * already set up initrd_start and initrd_end. In these cases
  157. * perfom sanity checks and use them if all looks good.
  158. */
  159. if (!initrd_start || initrd_end <= initrd_start)
  160. goto disable;
  161. if (initrd_start & ~PAGE_MASK) {
  162. pr_err("initrd start must be page aligned\n");
  163. goto disable;
  164. }
  165. if (initrd_start < PAGE_OFFSET) {
  166. pr_err("initrd start < PAGE_OFFSET\n");
  167. goto disable;
  168. }
  169. /*
  170. * Sanitize initrd addresses. For example firmware
  171. * can't guess if they need to pass them through
  172. * 64-bits values if the kernel has been built in pure
  173. * 32-bit. We need also to switch from KSEG0 to XKPHYS
  174. * addresses now, so the code can now safely use __pa().
  175. */
  176. end = __pa(initrd_end);
  177. initrd_end = (unsigned long)__va(end);
  178. initrd_start = (unsigned long)__va(__pa(initrd_start));
  179. ROOT_DEV = Root_RAM0;
  180. return PFN_UP(end);
  181. disable:
  182. initrd_start = 0;
  183. initrd_end = 0;
  184. return 0;
  185. }
  186. static void __init finalize_initrd(void)
  187. {
  188. unsigned long size = initrd_end - initrd_start;
  189. if (size == 0) {
  190. printk(KERN_INFO "Initrd not found or empty");
  191. goto disable;
  192. }
  193. if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
  194. printk(KERN_ERR "Initrd extends beyond end of memory");
  195. goto disable;
  196. }
  197. reserve_bootmem(__pa(initrd_start), size, BOOTMEM_DEFAULT);
  198. initrd_below_start_ok = 1;
  199. pr_info("Initial ramdisk at: 0x%lx (%lu bytes)\n",
  200. initrd_start, size);
  201. return;
  202. disable:
  203. printk(KERN_CONT " - disabling initrd\n");
  204. initrd_start = 0;
  205. initrd_end = 0;
  206. }
  207. #else /* !CONFIG_BLK_DEV_INITRD */
  208. static unsigned long __init init_initrd(void)
  209. {
  210. return 0;
  211. }
  212. #define finalize_initrd() do {} while (0)
  213. #endif
  214. /*
  215. * Initialize the bootmem allocator. It also setup initrd related data
  216. * if needed.
  217. */
  218. #ifdef CONFIG_SGI_IP27
  219. static void __init bootmem_init(void)
  220. {
  221. init_initrd();
  222. finalize_initrd();
  223. }
  224. #else /* !CONFIG_SGI_IP27 */
  225. static void __init bootmem_init(void)
  226. {
  227. unsigned long reserved_end;
  228. unsigned long mapstart = ~0UL;
  229. unsigned long bootmap_size;
  230. int i;
  231. /*
  232. * Init any data related to initrd. It's a nop if INITRD is
  233. * not selected. Once that done we can determine the low bound
  234. * of usable memory.
  235. */
  236. reserved_end = max(init_initrd(),
  237. (unsigned long) PFN_UP(__pa_symbol(&_end)));
  238. /*
  239. * max_low_pfn is not a number of pages. The number of pages
  240. * of the system is given by 'max_low_pfn - min_low_pfn'.
  241. */
  242. min_low_pfn = ~0UL;
  243. max_low_pfn = 0;
  244. /*
  245. * Find the highest page frame number we have available.
  246. */
  247. for (i = 0; i < boot_mem_map.nr_map; i++) {
  248. unsigned long start, end;
  249. if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
  250. continue;
  251. start = PFN_UP(boot_mem_map.map[i].addr);
  252. end = PFN_DOWN(boot_mem_map.map[i].addr
  253. + boot_mem_map.map[i].size);
  254. if (end > max_low_pfn)
  255. max_low_pfn = end;
  256. if (start < min_low_pfn)
  257. min_low_pfn = start;
  258. if (end <= reserved_end)
  259. continue;
  260. if (start >= mapstart)
  261. continue;
  262. mapstart = max(reserved_end, start);
  263. }
  264. if (min_low_pfn >= max_low_pfn)
  265. panic("Incorrect memory mapping !!!");
  266. if (min_low_pfn > ARCH_PFN_OFFSET) {
  267. pr_info("Wasting %lu bytes for tracking %lu unused pages\n",
  268. (min_low_pfn - ARCH_PFN_OFFSET) * sizeof(struct page),
  269. min_low_pfn - ARCH_PFN_OFFSET);
  270. } else if (min_low_pfn < ARCH_PFN_OFFSET) {
  271. pr_info("%lu free pages won't be used\n",
  272. ARCH_PFN_OFFSET - min_low_pfn);
  273. }
  274. min_low_pfn = ARCH_PFN_OFFSET;
  275. /*
  276. * Determine low and high memory ranges
  277. */
  278. max_pfn = max_low_pfn;
  279. if (max_low_pfn > PFN_DOWN(HIGHMEM_START)) {
  280. #ifdef CONFIG_HIGHMEM
  281. highstart_pfn = PFN_DOWN(HIGHMEM_START);
  282. highend_pfn = max_low_pfn;
  283. #endif
  284. max_low_pfn = PFN_DOWN(HIGHMEM_START);
  285. }
  286. /*
  287. * Initialize the boot-time allocator with low memory only.
  288. */
  289. bootmap_size = init_bootmem_node(NODE_DATA(0), mapstart,
  290. min_low_pfn, max_low_pfn);
  291. for (i = 0; i < boot_mem_map.nr_map; i++) {
  292. unsigned long start, end;
  293. start = PFN_UP(boot_mem_map.map[i].addr);
  294. end = PFN_DOWN(boot_mem_map.map[i].addr
  295. + boot_mem_map.map[i].size);
  296. if (start <= min_low_pfn)
  297. start = min_low_pfn;
  298. if (start >= end)
  299. continue;
  300. #ifndef CONFIG_HIGHMEM
  301. if (end > max_low_pfn)
  302. end = max_low_pfn;
  303. /*
  304. * ... finally, is the area going away?
  305. */
  306. if (end <= start)
  307. continue;
  308. #endif
  309. memblock_add_node(PFN_PHYS(start), PFN_PHYS(end - start), 0);
  310. }
  311. /*
  312. * Register fully available low RAM pages with the bootmem allocator.
  313. */
  314. for (i = 0; i < boot_mem_map.nr_map; i++) {
  315. unsigned long start, end, size;
  316. start = PFN_UP(boot_mem_map.map[i].addr);
  317. end = PFN_DOWN(boot_mem_map.map[i].addr
  318. + boot_mem_map.map[i].size);
  319. /*
  320. * Reserve usable memory.
  321. */
  322. switch (boot_mem_map.map[i].type) {
  323. case BOOT_MEM_RAM:
  324. break;
  325. case BOOT_MEM_INIT_RAM:
  326. memory_present(0, start, end);
  327. continue;
  328. default:
  329. /* Not usable memory */
  330. continue;
  331. }
  332. /*
  333. * We are rounding up the start address of usable memory
  334. * and at the end of the usable range downwards.
  335. */
  336. if (start >= max_low_pfn)
  337. continue;
  338. if (start < reserved_end)
  339. start = reserved_end;
  340. if (end > max_low_pfn)
  341. end = max_low_pfn;
  342. /*
  343. * ... finally, is the area going away?
  344. */
  345. if (end <= start)
  346. continue;
  347. size = end - start;
  348. /* Register lowmem ranges */
  349. free_bootmem(PFN_PHYS(start), size << PAGE_SHIFT);
  350. memory_present(0, start, end);
  351. }
  352. /*
  353. * Reserve the bootmap memory.
  354. */
  355. reserve_bootmem(PFN_PHYS(mapstart), bootmap_size, BOOTMEM_DEFAULT);
  356. /*
  357. * Reserve initrd memory if needed.
  358. */
  359. finalize_initrd();
  360. }
  361. #endif /* CONFIG_SGI_IP27 */
  362. /*
  363. * arch_mem_init - initialize memory management subsystem
  364. *
  365. * o plat_mem_setup() detects the memory configuration and will record detected
  366. * memory areas using add_memory_region.
  367. *
  368. * At this stage the memory configuration of the system is known to the
  369. * kernel but generic memory management system is still entirely uninitialized.
  370. *
  371. * o bootmem_init()
  372. * o sparse_init()
  373. * o paging_init()
  374. *
  375. * At this stage the bootmem allocator is ready to use.
  376. *
  377. * NOTE: historically plat_mem_setup did the entire platform initialization.
  378. * This was rather impractical because it meant plat_mem_setup had to
  379. * get away without any kind of memory allocator. To keep old code from
  380. * breaking plat_setup was just renamed to plat_setup and a second platform
  381. * initialization hook for anything else was introduced.
  382. */
  383. static int usermem __initdata;
  384. static int __init early_parse_mem(char *p)
  385. {
  386. unsigned long start, size;
  387. /*
  388. * If a user specifies memory size, we
  389. * blow away any automatically generated
  390. * size.
  391. */
  392. if (usermem == 0) {
  393. boot_mem_map.nr_map = 0;
  394. usermem = 1;
  395. }
  396. start = 0;
  397. size = memparse(p, &p);
  398. if (*p == '@')
  399. start = memparse(p + 1, &p);
  400. add_memory_region(start, size, BOOT_MEM_RAM);
  401. return 0;
  402. }
  403. early_param("mem", early_parse_mem);
  404. static void __init arch_mem_init(char **cmdline_p)
  405. {
  406. phys_t init_mem, init_end, init_size;
  407. extern void plat_mem_setup(void);
  408. /* call board setup routine */
  409. plat_mem_setup();
  410. init_mem = PFN_UP(__pa_symbol(&__init_begin)) << PAGE_SHIFT;
  411. init_end = PFN_DOWN(__pa_symbol(&__init_end)) << PAGE_SHIFT;
  412. init_size = init_end - init_mem;
  413. if (init_size) {
  414. /* Make sure it is in the boot_mem_map */
  415. int i, found;
  416. found = 0;
  417. for (i = 0; i < boot_mem_map.nr_map; i++) {
  418. if (init_mem >= boot_mem_map.map[i].addr &&
  419. init_mem < (boot_mem_map.map[i].addr +
  420. boot_mem_map.map[i].size)) {
  421. found = 1;
  422. break;
  423. }
  424. }
  425. if (!found)
  426. add_memory_region(init_mem, init_size,
  427. BOOT_MEM_INIT_RAM);
  428. }
  429. pr_info("Determined physical RAM map:\n");
  430. print_memory_map();
  431. #ifdef CONFIG_CMDLINE_BOOL
  432. #ifdef CONFIG_CMDLINE_OVERRIDE
  433. strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  434. #else
  435. if (builtin_cmdline[0]) {
  436. strlcat(arcs_cmdline, " ", COMMAND_LINE_SIZE);
  437. strlcat(arcs_cmdline, builtin_cmdline, COMMAND_LINE_SIZE);
  438. }
  439. strlcpy(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
  440. #endif
  441. #else
  442. strlcpy(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
  443. #endif
  444. strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  445. *cmdline_p = command_line;
  446. parse_early_param();
  447. if (usermem) {
  448. pr_info("User-defined physical RAM map:\n");
  449. print_memory_map();
  450. }
  451. bootmem_init();
  452. device_tree_init();
  453. sparse_init();
  454. plat_swiotlb_setup();
  455. paging_init();
  456. }
  457. static void __init resource_init(void)
  458. {
  459. int i;
  460. if (UNCAC_BASE != IO_BASE)
  461. return;
  462. code_resource.start = __pa_symbol(&_text);
  463. code_resource.end = __pa_symbol(&_etext) - 1;
  464. data_resource.start = __pa_symbol(&_etext);
  465. data_resource.end = __pa_symbol(&_edata) - 1;
  466. /*
  467. * Request address space for all standard RAM.
  468. */
  469. for (i = 0; i < boot_mem_map.nr_map; i++) {
  470. struct resource *res;
  471. unsigned long start, end;
  472. start = boot_mem_map.map[i].addr;
  473. end = boot_mem_map.map[i].addr + boot_mem_map.map[i].size - 1;
  474. if (start >= HIGHMEM_START)
  475. continue;
  476. if (end >= HIGHMEM_START)
  477. end = HIGHMEM_START - 1;
  478. res = alloc_bootmem(sizeof(struct resource));
  479. switch (boot_mem_map.map[i].type) {
  480. case BOOT_MEM_RAM:
  481. case BOOT_MEM_INIT_RAM:
  482. case BOOT_MEM_ROM_DATA:
  483. res->name = "System RAM";
  484. break;
  485. case BOOT_MEM_RESERVED:
  486. default:
  487. res->name = "reserved";
  488. }
  489. res->start = start;
  490. res->end = end;
  491. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  492. request_resource(&iomem_resource, res);
  493. /*
  494. * We don't know which RAM region contains kernel data,
  495. * so we try it repeatedly and let the resource manager
  496. * test it.
  497. */
  498. request_resource(res, &code_resource);
  499. request_resource(res, &data_resource);
  500. }
  501. }
  502. void __init setup_arch(char **cmdline_p)
  503. {
  504. cpu_probe();
  505. prom_init();
  506. #ifdef CONFIG_EARLY_PRINTK
  507. setup_early_printk();
  508. #endif
  509. cpu_report();
  510. check_bugs_early();
  511. #if defined(CONFIG_VT)
  512. #if defined(CONFIG_VGA_CONSOLE)
  513. conswitchp = &vga_con;
  514. #elif defined(CONFIG_DUMMY_CONSOLE)
  515. conswitchp = &dummy_con;
  516. #endif
  517. #endif
  518. arch_mem_init(cmdline_p);
  519. resource_init();
  520. plat_smp_setup();
  521. cpu_cache_init();
  522. }
  523. unsigned long kernelsp[NR_CPUS];
  524. unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
  525. #ifdef CONFIG_DEBUG_FS
  526. struct dentry *mips_debugfs_dir;
  527. static int __init debugfs_mips(void)
  528. {
  529. struct dentry *d;
  530. d = debugfs_create_dir("mips", NULL);
  531. if (!d)
  532. return -ENOMEM;
  533. mips_debugfs_dir = d;
  534. return 0;
  535. }
  536. arch_initcall(debugfs_mips);
  537. #endif