init.c 12 KB

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