init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 <asm/mach-types.h>
  19. #include <asm/setup.h>
  20. #include <asm/sizes.h>
  21. #include <asm/tlb.h>
  22. #include <asm/mach/arch.h>
  23. #include <asm/mach/map.h>
  24. #include "mm.h"
  25. static unsigned long phys_initrd_start __initdata = 0;
  26. static unsigned long phys_initrd_size __initdata = 0;
  27. static void __init early_initrd(char **p)
  28. {
  29. unsigned long start, size;
  30. start = memparse(*p, p);
  31. if (**p == ',') {
  32. size = memparse((*p) + 1, p);
  33. phys_initrd_start = start;
  34. phys_initrd_size = size;
  35. }
  36. }
  37. __early_param("initrd=", early_initrd);
  38. static int __init parse_tag_initrd(const struct tag *tag)
  39. {
  40. printk(KERN_WARNING "ATAG_INITRD is deprecated; "
  41. "please update your bootloader.\n");
  42. phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
  43. phys_initrd_size = tag->u.initrd.size;
  44. return 0;
  45. }
  46. __tagtable(ATAG_INITRD, parse_tag_initrd);
  47. static int __init parse_tag_initrd2(const struct tag *tag)
  48. {
  49. phys_initrd_start = tag->u.initrd.start;
  50. phys_initrd_size = tag->u.initrd.size;
  51. return 0;
  52. }
  53. __tagtable(ATAG_INITRD2, parse_tag_initrd2);
  54. /*
  55. * This is used to pass memory configuration data from paging_init
  56. * to mem_init, and by show_mem() to skip holes in the memory map.
  57. */
  58. static struct meminfo meminfo = { 0, };
  59. void show_mem(void)
  60. {
  61. int free = 0, total = 0, reserved = 0;
  62. int shared = 0, cached = 0, slab = 0, node, i;
  63. struct meminfo * mi = &meminfo;
  64. printk("Mem-info:\n");
  65. show_free_areas();
  66. for_each_online_node(node) {
  67. pg_data_t *n = NODE_DATA(node);
  68. struct page *map = n->node_mem_map - n->node_start_pfn;
  69. for_each_nodebank (i,mi,node) {
  70. struct membank *bank = &mi->bank[i];
  71. unsigned int pfn1, pfn2;
  72. struct page *page, *end;
  73. pfn1 = bank_pfn_start(bank);
  74. pfn2 = bank_pfn_end(bank);
  75. page = map + pfn1;
  76. end = map + pfn2;
  77. do {
  78. total++;
  79. if (PageReserved(page))
  80. reserved++;
  81. else if (PageSwapCache(page))
  82. cached++;
  83. else if (PageSlab(page))
  84. slab++;
  85. else if (!page_count(page))
  86. free++;
  87. else
  88. shared += page_count(page) - 1;
  89. page++;
  90. } while (page < end);
  91. }
  92. }
  93. printk("%d pages of RAM\n", total);
  94. printk("%d free pages\n", free);
  95. printk("%d reserved pages\n", reserved);
  96. printk("%d slab pages\n", slab);
  97. printk("%d pages shared\n", shared);
  98. printk("%d pages swap cached\n", cached);
  99. }
  100. /*
  101. * FIXME: We really want to avoid allocating the bootmap bitmap
  102. * over the top of the initrd. Hopefully, this is located towards
  103. * the start of a bank, so if we allocate the bootmap bitmap at
  104. * the end, we won't clash.
  105. */
  106. static unsigned int __init
  107. find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
  108. {
  109. unsigned int start_pfn, i, bootmap_pfn;
  110. start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
  111. bootmap_pfn = 0;
  112. for_each_nodebank(i, mi, node) {
  113. struct membank *bank = &mi->bank[i];
  114. unsigned int start, end;
  115. start = bank_pfn_start(bank);
  116. end = bank_pfn_end(bank);
  117. if (end < start_pfn)
  118. continue;
  119. if (start < start_pfn)
  120. start = start_pfn;
  121. if (end <= start)
  122. continue;
  123. if (end - start >= bootmap_pages) {
  124. bootmap_pfn = start;
  125. break;
  126. }
  127. }
  128. if (bootmap_pfn == 0)
  129. BUG();
  130. return bootmap_pfn;
  131. }
  132. static int __init check_initrd(struct meminfo *mi)
  133. {
  134. int initrd_node = -2;
  135. #ifdef CONFIG_BLK_DEV_INITRD
  136. unsigned long end = phys_initrd_start + phys_initrd_size;
  137. /*
  138. * Make sure that the initrd is within a valid area of
  139. * memory.
  140. */
  141. if (phys_initrd_size) {
  142. unsigned int i;
  143. initrd_node = -1;
  144. for (i = 0; i < mi->nr_banks; i++) {
  145. struct membank *bank = &mi->bank[i];
  146. if (bank_phys_start(bank) <= phys_initrd_start &&
  147. end <= bank_phys_end(bank))
  148. initrd_node = bank->node;
  149. }
  150. }
  151. if (initrd_node == -1) {
  152. printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
  153. "physical memory - disabling initrd\n",
  154. phys_initrd_start, phys_initrd_size);
  155. phys_initrd_start = phys_initrd_size = 0;
  156. }
  157. #endif
  158. return initrd_node;
  159. }
  160. static inline void map_memory_bank(struct membank *bank)
  161. {
  162. #ifdef CONFIG_MMU
  163. struct map_desc map;
  164. map.pfn = bank_pfn_start(bank);
  165. map.virtual = __phys_to_virt(bank_phys_start(bank));
  166. map.length = bank_phys_size(bank);
  167. map.type = MT_MEMORY;
  168. create_mapping(&map);
  169. #endif
  170. }
  171. static unsigned long __init
  172. bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
  173. {
  174. unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
  175. unsigned long start_pfn, end_pfn, boot_pfn;
  176. unsigned int boot_pages;
  177. pg_data_t *pgdat;
  178. int i;
  179. start_pfn = -1UL;
  180. end_pfn = 0;
  181. /*
  182. * Calculate the pfn range, and map the memory banks for this node.
  183. */
  184. for_each_nodebank(i, mi, node) {
  185. struct membank *bank = &mi->bank[i];
  186. unsigned long start, end;
  187. start = bank_pfn_start(bank);
  188. end = bank_pfn_end(bank);
  189. if (start_pfn > start)
  190. start_pfn = start;
  191. if (end_pfn < end)
  192. end_pfn = end;
  193. map_memory_bank(bank);
  194. }
  195. /*
  196. * If there is no memory in this node, ignore it.
  197. */
  198. if (end_pfn == 0)
  199. return end_pfn;
  200. /*
  201. * Allocate the bootmem bitmap page.
  202. */
  203. boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  204. boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
  205. /*
  206. * Initialise the bootmem allocator for this node, handing the
  207. * memory banks over to bootmem.
  208. */
  209. node_set_online(node);
  210. pgdat = NODE_DATA(node);
  211. init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
  212. for_each_nodebank(i, mi, node) {
  213. struct membank *bank = &mi->bank[i];
  214. free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
  215. }
  216. /*
  217. * Reserve the bootmem bitmap for this node.
  218. */
  219. reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
  220. boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
  221. /*
  222. * Reserve any special node zero regions.
  223. */
  224. if (node == 0)
  225. reserve_node_zero(pgdat);
  226. #ifdef CONFIG_BLK_DEV_INITRD
  227. /*
  228. * If the initrd is in this node, reserve its memory.
  229. */
  230. if (node == initrd_node) {
  231. int res = reserve_bootmem_node(pgdat, phys_initrd_start,
  232. phys_initrd_size, BOOTMEM_EXCLUSIVE);
  233. if (res == 0) {
  234. initrd_start = __phys_to_virt(phys_initrd_start);
  235. initrd_end = initrd_start + phys_initrd_size;
  236. } else {
  237. printk(KERN_ERR
  238. "INITRD: 0x%08lx+0x%08lx overlaps in-use "
  239. "memory region - disabling initrd\n",
  240. phys_initrd_start, phys_initrd_size);
  241. }
  242. }
  243. #endif
  244. /*
  245. * initialise the zones within this node.
  246. */
  247. memset(zone_size, 0, sizeof(zone_size));
  248. memset(zhole_size, 0, sizeof(zhole_size));
  249. /*
  250. * The size of this node has already been determined. If we need
  251. * to do anything fancy with the allocation of this memory to the
  252. * zones, now is the time to do it.
  253. */
  254. zone_size[0] = end_pfn - start_pfn;
  255. /*
  256. * For each bank in this node, calculate the size of the holes.
  257. * holes = node_size - sum(bank_sizes_in_node)
  258. */
  259. zhole_size[0] = zone_size[0];
  260. for_each_nodebank(i, mi, node)
  261. zhole_size[0] -= bank_pfn_size(&mi->bank[i]);
  262. /*
  263. * Adjust the sizes according to any special requirements for
  264. * this machine type.
  265. */
  266. arch_adjust_zones(node, zone_size, zhole_size);
  267. free_area_init_node(node, zone_size, start_pfn, zhole_size);
  268. return end_pfn;
  269. }
  270. void __init bootmem_init(struct meminfo *mi)
  271. {
  272. unsigned long memend_pfn = 0;
  273. int node, initrd_node;
  274. memcpy(&meminfo, mi, sizeof(meminfo));
  275. /*
  276. * Locate which node contains the ramdisk image, if any.
  277. */
  278. initrd_node = check_initrd(mi);
  279. /*
  280. * Run through each node initialising the bootmem allocator.
  281. */
  282. for_each_node(node) {
  283. unsigned long end_pfn;
  284. end_pfn = bootmem_init_node(node, initrd_node, mi);
  285. /*
  286. * Remember the highest memory PFN.
  287. */
  288. if (end_pfn > memend_pfn)
  289. memend_pfn = end_pfn;
  290. }
  291. high_memory = __va(memend_pfn << PAGE_SHIFT);
  292. /*
  293. * This doesn't seem to be used by the Linux memory manager any
  294. * more, but is used by ll_rw_block. If we can get rid of it, we
  295. * also get rid of some of the stuff above as well.
  296. *
  297. * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  298. * the system, not the maximum PFN.
  299. */
  300. max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
  301. }
  302. static inline void free_area(unsigned long addr, unsigned long end, char *s)
  303. {
  304. unsigned int size = (end - addr) >> 10;
  305. for (; addr < end; addr += PAGE_SIZE) {
  306. struct page *page = virt_to_page(addr);
  307. ClearPageReserved(page);
  308. init_page_count(page);
  309. free_page(addr);
  310. totalram_pages++;
  311. }
  312. if (size && s)
  313. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  314. }
  315. static inline void
  316. free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
  317. {
  318. struct page *start_pg, *end_pg;
  319. unsigned long pg, pgend;
  320. /*
  321. * Convert start_pfn/end_pfn to a struct page pointer.
  322. */
  323. start_pg = pfn_to_page(start_pfn);
  324. end_pg = pfn_to_page(end_pfn);
  325. /*
  326. * Convert to physical addresses, and
  327. * round start upwards and end downwards.
  328. */
  329. pg = PAGE_ALIGN(__pa(start_pg));
  330. pgend = __pa(end_pg) & PAGE_MASK;
  331. /*
  332. * If there are free pages between these,
  333. * free the section of the memmap array.
  334. */
  335. if (pg < pgend)
  336. free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
  337. }
  338. /*
  339. * The mem_map array can get very big. Free the unused area of the memory map.
  340. */
  341. static void __init free_unused_memmap_node(int node, struct meminfo *mi)
  342. {
  343. unsigned long bank_start, prev_bank_end = 0;
  344. unsigned int i;
  345. /*
  346. * [FIXME] This relies on each bank being in address order. This
  347. * may not be the case, especially if the user has provided the
  348. * information on the command line.
  349. */
  350. for_each_nodebank(i, mi, node) {
  351. struct membank *bank = &mi->bank[i];
  352. bank_start = bank_pfn_start(bank);
  353. if (bank_start < prev_bank_end) {
  354. printk(KERN_ERR "MEM: unordered memory banks. "
  355. "Not freeing memmap.\n");
  356. break;
  357. }
  358. /*
  359. * If we had a previous bank, and there is a space
  360. * between the current bank and the previous, free it.
  361. */
  362. if (prev_bank_end && prev_bank_end != bank_start)
  363. free_memmap(node, prev_bank_end, bank_start);
  364. prev_bank_end = bank_pfn_end(bank);
  365. }
  366. }
  367. /*
  368. * mem_init() marks the free areas in the mem_map and tells us how much
  369. * memory is free. This is done after various parts of the system have
  370. * claimed their memory after the kernel image.
  371. */
  372. void __init mem_init(void)
  373. {
  374. unsigned int codepages, datapages, initpages;
  375. int i, node;
  376. codepages = &_etext - &_text;
  377. datapages = &_end - &__data_start;
  378. initpages = &__init_end - &__init_begin;
  379. #ifndef CONFIG_DISCONTIGMEM
  380. max_mapnr = virt_to_page(high_memory) - mem_map;
  381. #endif
  382. /* this will put all unused low memory onto the freelists */
  383. for_each_online_node(node) {
  384. pg_data_t *pgdat = NODE_DATA(node);
  385. free_unused_memmap_node(node, &meminfo);
  386. if (pgdat->node_spanned_pages != 0)
  387. totalram_pages += free_all_bootmem_node(pgdat);
  388. }
  389. #ifdef CONFIG_SA1111
  390. /* now that our DMA memory is actually so designated, we can free it */
  391. free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
  392. #endif
  393. /*
  394. * Since our memory may not be contiguous, calculate the
  395. * real number of pages we have in this system
  396. */
  397. printk(KERN_INFO "Memory:");
  398. num_physpages = 0;
  399. for (i = 0; i < meminfo.nr_banks; i++) {
  400. num_physpages += bank_pfn_size(&meminfo.bank[i]);
  401. printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
  402. }
  403. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  404. printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
  405. "%dK data, %dK init)\n",
  406. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  407. codepages >> 10, datapages >> 10, initpages >> 10);
  408. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  409. extern int sysctl_overcommit_memory;
  410. /*
  411. * On a machine this small we won't get
  412. * anywhere without overcommit, so turn
  413. * it on by default.
  414. */
  415. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  416. }
  417. }
  418. void free_initmem(void)
  419. {
  420. if (!machine_is_integrator() && !machine_is_cintegrator()) {
  421. free_area((unsigned long)(&__init_begin),
  422. (unsigned long)(&__init_end),
  423. "init");
  424. }
  425. }
  426. #ifdef CONFIG_BLK_DEV_INITRD
  427. static int keep_initrd;
  428. void free_initrd_mem(unsigned long start, unsigned long end)
  429. {
  430. if (!keep_initrd)
  431. free_area(start, end, "initrd");
  432. }
  433. static int __init keepinitrd_setup(char *__unused)
  434. {
  435. keep_initrd = 1;
  436. return 1;
  437. }
  438. __setup("keepinitrd", keepinitrd_setup);
  439. #endif