init.c 15 KB

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