init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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)
  164. {
  165. unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
  166. unsigned long min, max_low, max_high;
  167. int i;
  168. find_limits(mi, &min, &max_low, &max_high);
  169. /*
  170. * initialise the zones.
  171. */
  172. memset(zone_size, 0, sizeof(zone_size));
  173. /*
  174. * The memory size has already been determined. If we need
  175. * to do anything fancy with the allocation of this memory
  176. * to the zones, now is the time to do it.
  177. */
  178. zone_size[0] = max_low - min;
  179. #ifdef CONFIG_HIGHMEM
  180. zone_size[ZONE_HIGHMEM] = max_high - max_low;
  181. #endif
  182. /*
  183. * Calculate the size of the holes.
  184. * holes = node_size - sum(bank_sizes)
  185. */
  186. memcpy(zhole_size, zone_size, sizeof(zhole_size));
  187. for_each_bank(i, mi) {
  188. int idx = 0;
  189. #ifdef CONFIG_HIGHMEM
  190. if (mi->bank[i].highmem)
  191. idx = ZONE_HIGHMEM;
  192. #endif
  193. zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
  194. }
  195. /*
  196. * Adjust the sizes according to any special requirements for
  197. * this machine type.
  198. */
  199. arch_adjust_zones(zone_size, zhole_size);
  200. free_area_init_node(0, zone_size, min, zhole_size);
  201. }
  202. #ifndef CONFIG_SPARSEMEM
  203. int pfn_valid(unsigned long pfn)
  204. {
  205. struct meminfo *mi = &meminfo;
  206. unsigned int left = 0, right = mi->nr_banks;
  207. do {
  208. unsigned int mid = (right + left) / 2;
  209. struct membank *bank = &mi->bank[mid];
  210. if (pfn < bank_pfn_start(bank))
  211. right = mid;
  212. else if (pfn >= bank_pfn_end(bank))
  213. left = mid + 1;
  214. else
  215. return 1;
  216. } while (left < right);
  217. return 0;
  218. }
  219. EXPORT_SYMBOL(pfn_valid);
  220. static void arm_memory_present(void)
  221. {
  222. }
  223. #else
  224. static void arm_memory_present(void)
  225. {
  226. int i;
  227. for (i = 0; i < memblock.memory.cnt; i++)
  228. memory_present(0, memblock_start_pfn(&memblock.memory, i),
  229. memblock_end_pfn(&memblock.memory, i));
  230. }
  231. #endif
  232. void __init arm_memblock_init(struct meminfo *mi, struct machine_desc *mdesc)
  233. {
  234. int i;
  235. memblock_init();
  236. for (i = 0; i < mi->nr_banks; i++)
  237. memblock_add(mi->bank[i].start, mi->bank[i].size);
  238. /* Register the kernel text, kernel data and initrd with memblock. */
  239. #ifdef CONFIG_XIP_KERNEL
  240. memblock_reserve(__pa(_data), _end - _data);
  241. #else
  242. memblock_reserve(__pa(_stext), _end - _stext);
  243. #endif
  244. #ifdef CONFIG_BLK_DEV_INITRD
  245. if (phys_initrd_size) {
  246. memblock_reserve(phys_initrd_start, phys_initrd_size);
  247. /* Now convert initrd to virtual addresses */
  248. initrd_start = __phys_to_virt(phys_initrd_start);
  249. initrd_end = initrd_start + phys_initrd_size;
  250. }
  251. #endif
  252. arm_mm_memblock_reserve();
  253. /* reserve any platform specific memblock areas */
  254. if (mdesc->reserve)
  255. mdesc->reserve();
  256. memblock_analyze();
  257. memblock_dump_all();
  258. }
  259. void __init bootmem_init(void)
  260. {
  261. struct meminfo *mi = &meminfo;
  262. unsigned long min, max_low, max_high;
  263. max_low = max_high = 0;
  264. find_limits(mi, &min, &max_low, &max_high);
  265. arm_bootmem_init(mi, min, max_low);
  266. /*
  267. * Sparsemem tries to allocate bootmem in memory_present(),
  268. * so must be done after the fixed reservations
  269. */
  270. arm_memory_present();
  271. /*
  272. * sparse_init() needs the bootmem allocator up and running.
  273. */
  274. sparse_init();
  275. /*
  276. * Now free the memory - free_area_init_node needs
  277. * the sparse mem_map arrays initialized by sparse_init()
  278. * for memmap_init_zone(), otherwise all PFNs are invalid.
  279. */
  280. arm_bootmem_free(mi);
  281. high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
  282. /*
  283. * This doesn't seem to be used by the Linux memory manager any
  284. * more, but is used by ll_rw_block. If we can get rid of it, we
  285. * also get rid of some of the stuff above as well.
  286. *
  287. * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  288. * the system, not the maximum PFN.
  289. */
  290. max_low_pfn = max_low - PHYS_PFN_OFFSET;
  291. max_pfn = max_high - PHYS_PFN_OFFSET;
  292. }
  293. static inline int free_area(unsigned long pfn, unsigned long end, char *s)
  294. {
  295. unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
  296. for (; pfn < end; pfn++) {
  297. struct page *page = pfn_to_page(pfn);
  298. ClearPageReserved(page);
  299. init_page_count(page);
  300. __free_page(page);
  301. pages++;
  302. }
  303. if (size && s)
  304. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  305. return pages;
  306. }
  307. static inline void
  308. free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  309. {
  310. struct page *start_pg, *end_pg;
  311. unsigned long pg, pgend;
  312. /*
  313. * Convert start_pfn/end_pfn to a struct page pointer.
  314. */
  315. start_pg = pfn_to_page(start_pfn - 1) + 1;
  316. end_pg = pfn_to_page(end_pfn);
  317. /*
  318. * Convert to physical addresses, and
  319. * round start upwards and end downwards.
  320. */
  321. pg = PAGE_ALIGN(__pa(start_pg));
  322. pgend = __pa(end_pg) & PAGE_MASK;
  323. /*
  324. * If there are free pages between these,
  325. * free the section of the memmap array.
  326. */
  327. if (pg < pgend)
  328. free_bootmem(pg, pgend - pg);
  329. }
  330. /*
  331. * The mem_map array can get very big. Free the unused area of the memory map.
  332. */
  333. static void __init free_unused_memmap(struct meminfo *mi)
  334. {
  335. unsigned long bank_start, prev_bank_end = 0;
  336. unsigned int i;
  337. /*
  338. * [FIXME] This relies on each bank being in address order. This
  339. * may not be the case, especially if the user has provided the
  340. * information on the command line.
  341. */
  342. for_each_bank(i, mi) {
  343. struct membank *bank = &mi->bank[i];
  344. bank_start = bank_pfn_start(bank);
  345. if (bank_start < prev_bank_end) {
  346. printk(KERN_ERR "MEM: unordered memory banks. "
  347. "Not freeing memmap.\n");
  348. break;
  349. }
  350. /*
  351. * If we had a previous bank, and there is a space
  352. * between the current bank and the previous, free it.
  353. */
  354. if (prev_bank_end && prev_bank_end != bank_start)
  355. free_memmap(prev_bank_end, bank_start);
  356. prev_bank_end = bank_pfn_end(bank);
  357. }
  358. }
  359. /*
  360. * mem_init() marks the free areas in the mem_map and tells us how much
  361. * memory is free. This is done after various parts of the system have
  362. * claimed their memory after the kernel image.
  363. */
  364. void __init mem_init(void)
  365. {
  366. unsigned long reserved_pages, free_pages;
  367. int i;
  368. max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
  369. /* this will put all unused low memory onto the freelists */
  370. free_unused_memmap(&meminfo);
  371. totalram_pages += free_all_bootmem();
  372. #ifdef CONFIG_SA1111
  373. /* now that our DMA memory is actually so designated, we can free it */
  374. totalram_pages += free_area(PHYS_PFN_OFFSET,
  375. __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
  376. #endif
  377. #ifdef CONFIG_HIGHMEM
  378. /* set highmem page free */
  379. for_each_bank (i, &meminfo) {
  380. unsigned long start = bank_pfn_start(&meminfo.bank[i]);
  381. unsigned long end = bank_pfn_end(&meminfo.bank[i]);
  382. if (start >= max_low_pfn + PHYS_PFN_OFFSET)
  383. totalhigh_pages += free_area(start, end, NULL);
  384. }
  385. totalram_pages += totalhigh_pages;
  386. #endif
  387. reserved_pages = free_pages = 0;
  388. for_each_bank(i, &meminfo) {
  389. struct membank *bank = &meminfo.bank[i];
  390. unsigned int pfn1, pfn2;
  391. struct page *page, *end;
  392. pfn1 = bank_pfn_start(bank);
  393. pfn2 = bank_pfn_end(bank);
  394. page = pfn_to_page(pfn1);
  395. end = pfn_to_page(pfn2 - 1) + 1;
  396. do {
  397. if (PageReserved(page))
  398. reserved_pages++;
  399. else if (!page_count(page))
  400. free_pages++;
  401. page++;
  402. } while (page < end);
  403. }
  404. /*
  405. * Since our memory may not be contiguous, calculate the
  406. * real number of pages we have in this system
  407. */
  408. printk(KERN_INFO "Memory:");
  409. num_physpages = 0;
  410. for (i = 0; i < meminfo.nr_banks; i++) {
  411. num_physpages += bank_pfn_size(&meminfo.bank[i]);
  412. printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
  413. }
  414. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  415. printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
  416. nr_free_pages() << (PAGE_SHIFT-10),
  417. free_pages << (PAGE_SHIFT-10),
  418. reserved_pages << (PAGE_SHIFT-10),
  419. totalhigh_pages << (PAGE_SHIFT-10));
  420. #define MLK(b, t) b, t, ((t) - (b)) >> 10
  421. #define MLM(b, t) b, t, ((t) - (b)) >> 20
  422. #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
  423. printk(KERN_NOTICE "Virtual kernel memory layout:\n"
  424. " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
  425. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  426. #ifdef CONFIG_MMU
  427. " DMA : 0x%08lx - 0x%08lx (%4ld MB)\n"
  428. #endif
  429. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  430. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  431. #ifdef CONFIG_HIGHMEM
  432. " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n"
  433. #endif
  434. " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
  435. " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
  436. " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
  437. " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
  438. MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
  439. (PAGE_SIZE)),
  440. MLK(FIXADDR_START, FIXADDR_TOP),
  441. #ifdef CONFIG_MMU
  442. MLM(CONSISTENT_BASE, CONSISTENT_END),
  443. #endif
  444. MLM(VMALLOC_START, VMALLOC_END),
  445. MLM(PAGE_OFFSET, (unsigned long)high_memory),
  446. #ifdef CONFIG_HIGHMEM
  447. MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
  448. (PAGE_SIZE)),
  449. #endif
  450. MLM(MODULES_VADDR, MODULES_END),
  451. MLK_ROUNDUP(__init_begin, __init_end),
  452. MLK_ROUNDUP(_text, _etext),
  453. MLK_ROUNDUP(_data, _edata));
  454. #undef MLK
  455. #undef MLM
  456. #undef MLK_ROUNDUP
  457. /*
  458. * Check boundaries twice: Some fundamental inconsistencies can
  459. * be detected at build time already.
  460. */
  461. #ifdef CONFIG_MMU
  462. BUILD_BUG_ON(VMALLOC_END > CONSISTENT_BASE);
  463. BUG_ON(VMALLOC_END > CONSISTENT_BASE);
  464. BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
  465. BUG_ON(TASK_SIZE > MODULES_VADDR);
  466. #endif
  467. #ifdef CONFIG_HIGHMEM
  468. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  469. BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  470. #endif
  471. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  472. extern int sysctl_overcommit_memory;
  473. /*
  474. * On a machine this small we won't get
  475. * anywhere without overcommit, so turn
  476. * it on by default.
  477. */
  478. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  479. }
  480. }
  481. void free_initmem(void)
  482. {
  483. #ifdef CONFIG_HAVE_TCM
  484. extern char __tcm_start, __tcm_end;
  485. totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)),
  486. __phys_to_pfn(__pa(&__tcm_end)),
  487. "TCM link");
  488. #endif
  489. if (!machine_is_integrator() && !machine_is_cintegrator())
  490. totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
  491. __phys_to_pfn(__pa(__init_end)),
  492. "init");
  493. }
  494. #ifdef CONFIG_BLK_DEV_INITRD
  495. static int keep_initrd;
  496. void free_initrd_mem(unsigned long start, unsigned long end)
  497. {
  498. if (!keep_initrd)
  499. totalram_pages += free_area(__phys_to_pfn(__pa(start)),
  500. __phys_to_pfn(__pa(end)),
  501. "initrd");
  502. }
  503. static int __init keepinitrd_setup(char *__unused)
  504. {
  505. keep_initrd = 1;
  506. return 1;
  507. }
  508. __setup("keepinitrd", keepinitrd_setup);
  509. #endif