init.c 12 KB

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