init.c 12 KB

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