init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * linux/arch/arm/mm/init.c
  3. *
  4. * Copyright (C) 1995-2005 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/swap.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/mman.h>
  16. #include <linux/nodemask.h>
  17. #include <linux/initrd.h>
  18. #include <linux/highmem.h>
  19. #include <linux/gfp.h>
  20. #include <linux/memblock.h>
  21. #include <linux/sort.h>
  22. #include <asm/mach-types.h>
  23. #include <asm/sections.h>
  24. #include <asm/setup.h>
  25. #include <asm/sizes.h>
  26. #include <asm/tlb.h>
  27. #include <asm/fixmap.h>
  28. #include <asm/mach/arch.h>
  29. #include <asm/mach/map.h>
  30. #include "mm.h"
  31. static unsigned long phys_initrd_start __initdata = 0;
  32. static unsigned long phys_initrd_size __initdata = 0;
  33. static int __init early_initrd(char *p)
  34. {
  35. unsigned long start, size;
  36. char *endp;
  37. start = memparse(p, &endp);
  38. if (*endp == ',') {
  39. size = memparse(endp + 1, NULL);
  40. phys_initrd_start = start;
  41. phys_initrd_size = size;
  42. }
  43. return 0;
  44. }
  45. early_param("initrd", early_initrd);
  46. static int __init parse_tag_initrd(const struct tag *tag)
  47. {
  48. printk(KERN_WARNING "ATAG_INITRD is deprecated; "
  49. "please update your bootloader.\n");
  50. phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
  51. phys_initrd_size = tag->u.initrd.size;
  52. return 0;
  53. }
  54. __tagtable(ATAG_INITRD, parse_tag_initrd);
  55. static int __init parse_tag_initrd2(const struct tag *tag)
  56. {
  57. phys_initrd_start = tag->u.initrd.start;
  58. phys_initrd_size = tag->u.initrd.size;
  59. return 0;
  60. }
  61. __tagtable(ATAG_INITRD2, parse_tag_initrd2);
  62. /*
  63. * This keeps memory configuration data used by a couple memory
  64. * initialization functions, as well as show_mem() for the skipping
  65. * of holes in the memory map. It is populated by arm_add_memory().
  66. */
  67. struct meminfo meminfo;
  68. void show_mem(void)
  69. {
  70. int free = 0, total = 0, reserved = 0;
  71. int shared = 0, cached = 0, slab = 0, i;
  72. struct meminfo * mi = &meminfo;
  73. printk("Mem-info:\n");
  74. show_free_areas();
  75. for_each_bank (i, mi) {
  76. struct membank *bank = &mi->bank[i];
  77. unsigned int pfn1, pfn2;
  78. struct page *page, *end;
  79. pfn1 = bank_pfn_start(bank);
  80. pfn2 = bank_pfn_end(bank);
  81. page = pfn_to_page(pfn1);
  82. end = pfn_to_page(pfn2 - 1) + 1;
  83. do {
  84. total++;
  85. if (PageReserved(page))
  86. reserved++;
  87. else if (PageSwapCache(page))
  88. cached++;
  89. else if (PageSlab(page))
  90. slab++;
  91. else if (!page_count(page))
  92. free++;
  93. else
  94. shared += page_count(page) - 1;
  95. page++;
  96. } while (page < end);
  97. }
  98. printk("%d pages of RAM\n", total);
  99. printk("%d free pages\n", free);
  100. printk("%d reserved pages\n", reserved);
  101. printk("%d slab pages\n", slab);
  102. printk("%d pages shared\n", shared);
  103. printk("%d pages swap cached\n", cached);
  104. }
  105. static void __init find_limits(struct meminfo *mi,
  106. unsigned long *min, unsigned long *max_low, unsigned long *max_high)
  107. {
  108. int i;
  109. *min = -1UL;
  110. *max_low = *max_high = 0;
  111. for_each_bank (i, mi) {
  112. struct membank *bank = &mi->bank[i];
  113. unsigned long start, end;
  114. start = bank_pfn_start(bank);
  115. end = bank_pfn_end(bank);
  116. if (*min > start)
  117. *min = start;
  118. if (*max_high < end)
  119. *max_high = end;
  120. if (bank->highmem)
  121. continue;
  122. if (*max_low < end)
  123. *max_low = end;
  124. }
  125. }
  126. static void __init arm_bootmem_init(struct meminfo *mi,
  127. unsigned long start_pfn, unsigned long end_pfn)
  128. {
  129. struct memblock_region *reg;
  130. unsigned int boot_pages;
  131. phys_addr_t bitmap;
  132. pg_data_t *pgdat;
  133. int i;
  134. /*
  135. * Allocate the bootmem bitmap page. This must be in a region
  136. * of memory which has already been mapped.
  137. */
  138. boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  139. bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
  140. __pfn_to_phys(end_pfn));
  141. /*
  142. * Initialise the bootmem allocator, handing the
  143. * memory banks over to bootmem.
  144. */
  145. node_set_online(0);
  146. pgdat = NODE_DATA(0);
  147. init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
  148. for_each_bank(i, mi) {
  149. struct membank *bank = &mi->bank[i];
  150. if (!bank->highmem)
  151. free_bootmem(bank_phys_start(bank), bank_phys_size(bank));
  152. }
  153. /*
  154. * Reserve the memblock reserved regions in bootmem.
  155. */
  156. for_each_memblock(reserved, reg) {
  157. phys_addr_t start = memblock_region_reserved_base_pfn(reg);
  158. phys_addr_t end = memblock_region_reserved_end_pfn(reg);
  159. if (start >= start_pfn && end <= end_pfn)
  160. reserve_bootmem_node(pgdat, __pfn_to_phys(start),
  161. (end - start) << PAGE_SHIFT,
  162. BOOTMEM_DEFAULT);
  163. }
  164. }
  165. static void __init arm_bootmem_free(unsigned long min, unsigned long max_low,
  166. unsigned long max_high)
  167. {
  168. unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
  169. struct memblock_region *reg;
  170. /*
  171. * initialise the zones.
  172. */
  173. memset(zone_size, 0, sizeof(zone_size));
  174. /*
  175. * The memory size has already been determined. If we need
  176. * to do anything fancy with the allocation of this memory
  177. * to the zones, now is the time to do it.
  178. */
  179. zone_size[0] = max_low - min;
  180. #ifdef CONFIG_HIGHMEM
  181. zone_size[ZONE_HIGHMEM] = max_high - max_low;
  182. #endif
  183. /*
  184. * Calculate the size of the holes.
  185. * holes = node_size - sum(bank_sizes)
  186. */
  187. memcpy(zhole_size, zone_size, sizeof(zhole_size));
  188. for_each_memblock(memory, reg) {
  189. unsigned long start = memblock_region_memory_base_pfn(reg);
  190. unsigned long end = memblock_region_memory_end_pfn(reg);
  191. if (start < max_low) {
  192. unsigned long low_end = min(end, max_low);
  193. zhole_size[0] -= low_end - start;
  194. }
  195. #ifdef CONFIG_HIGHMEM
  196. if (end > max_low) {
  197. unsigned long high_start = max(start, max_low);
  198. zhole_size[ZONE_HIGHMEM] -= end - high_start;
  199. }
  200. #endif
  201. }
  202. /*
  203. * Adjust the sizes according to any special requirements for
  204. * this machine type.
  205. */
  206. arch_adjust_zones(zone_size, zhole_size);
  207. free_area_init_node(0, zone_size, min, zhole_size);
  208. }
  209. #ifndef CONFIG_SPARSEMEM
  210. int pfn_valid(unsigned long pfn)
  211. {
  212. return memblock_is_memory(pfn << PAGE_SHIFT);
  213. }
  214. EXPORT_SYMBOL(pfn_valid);
  215. static void arm_memory_present(void)
  216. {
  217. }
  218. #else
  219. static void arm_memory_present(void)
  220. {
  221. struct memblock_region *reg;
  222. for_each_memblock(memory, reg)
  223. memory_present(0, memblock_region_memory_base_pfn(reg),
  224. memblock_region_memory_end_pfn(reg));
  225. }
  226. #endif
  227. static int __init meminfo_cmp(const void *_a, const void *_b)
  228. {
  229. const struct membank *a = _a, *b = _b;
  230. long cmp = bank_pfn_start(a) - bank_pfn_start(b);
  231. return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
  232. }
  233. void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc)
  234. {
  235. int i;
  236. sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]), meminfo_cmp, NULL);
  237. memblock_init();
  238. for (i = 0; i < mi->nr_banks; i++)
  239. memblock_add(mi->bank[i].start, mi->bank[i].size);
  240. /* Register the kernel text, kernel data and initrd with memblock. */
  241. #ifdef CONFIG_XIP_KERNEL
  242. memblock_reserve(__pa(_sdata), _end - _sdata);
  243. #else
  244. memblock_reserve(__pa(_stext), _end - _stext);
  245. #endif
  246. #ifdef CONFIG_BLK_DEV_INITRD
  247. if (phys_initrd_size) {
  248. memblock_reserve(phys_initrd_start, phys_initrd_size);
  249. /* Now convert initrd to virtual addresses */
  250. initrd_start = __phys_to_virt(phys_initrd_start);
  251. initrd_end = initrd_start + phys_initrd_size;
  252. }
  253. #endif
  254. arm_mm_memblock_reserve();
  255. /* reserve any platform specific memblock areas */
  256. if (mdesc->reserve)
  257. mdesc->reserve();
  258. memblock_analyze();
  259. memblock_dump_all();
  260. }
  261. void __init bootmem_init(void)
  262. {
  263. struct meminfo *mi = &meminfo;
  264. unsigned long min, max_low, max_high;
  265. max_low = max_high = 0;
  266. find_limits(mi, &min, &max_low, &max_high);
  267. arm_bootmem_init(mi, min, max_low);
  268. /*
  269. * Sparsemem tries to allocate bootmem in memory_present(),
  270. * so must be done after the fixed reservations
  271. */
  272. arm_memory_present();
  273. /*
  274. * sparse_init() needs the bootmem allocator up and running.
  275. */
  276. sparse_init();
  277. /*
  278. * Now free the memory - free_area_init_node needs
  279. * the sparse mem_map arrays initialized by sparse_init()
  280. * for memmap_init_zone(), otherwise all PFNs are invalid.
  281. */
  282. arm_bootmem_free(min, max_low, max_high);
  283. high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
  284. /*
  285. * This doesn't seem to be used by the Linux memory manager any
  286. * more, but is used by ll_rw_block. If we can get rid of it, we
  287. * also get rid of some of the stuff above as well.
  288. *
  289. * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  290. * the system, not the maximum PFN.
  291. */
  292. max_low_pfn = max_low - PHYS_PFN_OFFSET;
  293. max_pfn = max_high - PHYS_PFN_OFFSET;
  294. }
  295. static inline int free_area(unsigned long pfn, unsigned long end, char *s)
  296. {
  297. unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
  298. for (; pfn < end; pfn++) {
  299. struct page *page = pfn_to_page(pfn);
  300. ClearPageReserved(page);
  301. init_page_count(page);
  302. __free_page(page);
  303. pages++;
  304. }
  305. if (size && s)
  306. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  307. return pages;
  308. }
  309. static inline void
  310. free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  311. {
  312. struct page *start_pg, *end_pg;
  313. unsigned long pg, pgend;
  314. /*
  315. * Convert start_pfn/end_pfn to a struct page pointer.
  316. */
  317. start_pg = pfn_to_page(start_pfn - 1) + 1;
  318. end_pg = pfn_to_page(end_pfn);
  319. /*
  320. * Convert to physical addresses, and
  321. * round start upwards and end downwards.
  322. */
  323. pg = PAGE_ALIGN(__pa(start_pg));
  324. pgend = __pa(end_pg) & PAGE_MASK;
  325. /*
  326. * If there are free pages between these,
  327. * free the section of the memmap array.
  328. */
  329. if (pg < pgend)
  330. free_bootmem(pg, pgend - pg);
  331. }
  332. /*
  333. * The mem_map array can get very big. Free the unused area of the memory map.
  334. */
  335. static void __init free_unused_memmap(struct meminfo *mi)
  336. {
  337. unsigned long bank_start, prev_bank_end = 0;
  338. unsigned int i;
  339. /*
  340. * This relies on each bank being in address order.
  341. * The banks are sorted previously in bootmem_init().
  342. */
  343. for_each_bank(i, mi) {
  344. struct membank *bank = &mi->bank[i];
  345. bank_start = bank_pfn_start(bank);
  346. /*
  347. * If we had a previous bank, and there is a space
  348. * between the current bank and the previous, free it.
  349. */
  350. if (prev_bank_end && prev_bank_end < bank_start)
  351. free_memmap(prev_bank_end, bank_start);
  352. /*
  353. * Align up here since the VM subsystem insists that the
  354. * memmap entries are valid from the bank end aligned to
  355. * MAX_ORDER_NR_PAGES.
  356. */
  357. prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
  358. }
  359. }
  360. /*
  361. * mem_init() marks the free areas in the mem_map and tells us how much
  362. * memory is free. This is done after various parts of the system have
  363. * claimed their memory after the kernel image.
  364. */
  365. void __init mem_init(void)
  366. {
  367. unsigned long reserved_pages, free_pages;
  368. int i;
  369. #ifdef CONFIG_HAVE_TCM
  370. /* These pointers are filled in on TCM detection */
  371. extern u32 dtcm_end;
  372. extern u32 itcm_end;
  373. #endif
  374. max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
  375. /* this will put all unused low memory onto the freelists */
  376. free_unused_memmap(&meminfo);
  377. totalram_pages += free_all_bootmem();
  378. #ifdef CONFIG_SA1111
  379. /* now that our DMA memory is actually so designated, we can free it */
  380. totalram_pages += free_area(PHYS_PFN_OFFSET,
  381. __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
  382. #endif
  383. #ifdef CONFIG_HIGHMEM
  384. /* set highmem page free */
  385. for_each_bank (i, &meminfo) {
  386. unsigned long start = bank_pfn_start(&meminfo.bank[i]);
  387. unsigned long end = bank_pfn_end(&meminfo.bank[i]);
  388. if (start >= max_low_pfn + PHYS_PFN_OFFSET)
  389. totalhigh_pages += free_area(start, end, NULL);
  390. }
  391. totalram_pages += totalhigh_pages;
  392. #endif
  393. reserved_pages = free_pages = 0;
  394. for_each_bank(i, &meminfo) {
  395. struct membank *bank = &meminfo.bank[i];
  396. unsigned int pfn1, pfn2;
  397. struct page *page, *end;
  398. pfn1 = bank_pfn_start(bank);
  399. pfn2 = bank_pfn_end(bank);
  400. page = pfn_to_page(pfn1);
  401. end = pfn_to_page(pfn2 - 1) + 1;
  402. do {
  403. if (PageReserved(page))
  404. reserved_pages++;
  405. else if (!page_count(page))
  406. free_pages++;
  407. page++;
  408. } while (page < end);
  409. }
  410. /*
  411. * Since our memory may not be contiguous, calculate the
  412. * real number of pages we have in this system
  413. */
  414. printk(KERN_INFO "Memory:");
  415. num_physpages = 0;
  416. for (i = 0; i < meminfo.nr_banks; i++) {
  417. num_physpages += bank_pfn_size(&meminfo.bank[i]);
  418. printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
  419. }
  420. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  421. printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
  422. nr_free_pages() << (PAGE_SHIFT-10),
  423. free_pages << (PAGE_SHIFT-10),
  424. reserved_pages << (PAGE_SHIFT-10),
  425. totalhigh_pages << (PAGE_SHIFT-10));
  426. #define MLK(b, t) b, t, ((t) - (b)) >> 10
  427. #define MLM(b, t) b, t, ((t) - (b)) >> 20
  428. #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
  429. printk(KERN_NOTICE "Virtual kernel memory layout:\n"
  430. " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
  431. #ifdef CONFIG_HAVE_TCM
  432. " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n"
  433. " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n"
  434. #endif
  435. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  436. #ifdef CONFIG_MMU
  437. " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
  438. #endif
  439. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  440. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  441. #ifdef CONFIG_HIGHMEM
  442. " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
  443. #endif
  444. " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
  445. " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
  446. " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
  447. " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
  448. MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
  449. (PAGE_SIZE)),
  450. #ifdef CONFIG_HAVE_TCM
  451. MLK(DTCM_OFFSET, (unsigned long) dtcm_end),
  452. MLK(ITCM_OFFSET, (unsigned long) itcm_end),
  453. #endif
  454. MLK(FIXADDR_START, FIXADDR_TOP),
  455. #ifdef CONFIG_MMU
  456. MLM(CONSISTENT_BASE, CONSISTENT_END),
  457. #endif
  458. MLM(VMALLOC_START, VMALLOC_END),
  459. MLM(PAGE_OFFSET, (unsigned long)high_memory),
  460. #ifdef CONFIG_HIGHMEM
  461. MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
  462. (PAGE_SIZE)),
  463. #endif
  464. MLM(MODULES_VADDR, MODULES_END),
  465. MLK_ROUNDUP(__init_begin, __init_end),
  466. MLK_ROUNDUP(_text, _etext),
  467. MLK_ROUNDUP(_sdata, _edata));
  468. #undef MLK
  469. #undef MLM
  470. #undef MLK_ROUNDUP
  471. /*
  472. * Check boundaries twice: Some fundamental inconsistencies can
  473. * be detected at build time already.
  474. */
  475. #ifdef CONFIG_MMU
  476. BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
  477. BUG_ON(VMALLOC_END > CONSISTENT_BASE);
  478. BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
  479. BUG_ON(TASK_SIZE > MODULES_VADDR);
  480. #endif
  481. #ifdef CONFIG_HIGHMEM
  482. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  483. BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  484. #endif
  485. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  486. extern int sysctl_overcommit_memory;
  487. /*
  488. * On a machine this small we won't get
  489. * anywhere without overcommit, so turn
  490. * it on by default.
  491. */
  492. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  493. }
  494. }
  495. void free_initmem(void)
  496. {
  497. #ifdef CONFIG_HAVE_TCM
  498. extern char __tcm_start, __tcm_end;
  499. totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)),
  500. __phys_to_pfn(__pa(&__tcm_end)),
  501. "TCM link");
  502. #endif
  503. if (!machine_is_integrator() && !machine_is_cintegrator())
  504. totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
  505. __phys_to_pfn(__pa(__init_end)),
  506. "init");
  507. }
  508. #ifdef CONFIG_BLK_DEV_INITRD
  509. static int keep_initrd;
  510. void free_initrd_mem(unsigned long start, unsigned long end)
  511. {
  512. if (!keep_initrd)
  513. totalram_pages += free_area(__phys_to_pfn(__pa(start)),
  514. __phys_to_pfn(__pa(end)),
  515. "initrd");
  516. }
  517. static int __init keepinitrd_setup(char *__unused)
  518. {
  519. keep_initrd = 1;
  520. return 1;
  521. }
  522. __setup("keepinitrd", keepinitrd_setup);
  523. #endif