init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 = pgdat_page_nr(n, 0) - 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 bootmem_init_node(int node, struct meminfo *mi)
  172. {
  173. unsigned long start_pfn, end_pfn, boot_pfn;
  174. unsigned int boot_pages;
  175. pg_data_t *pgdat;
  176. int i;
  177. start_pfn = -1UL;
  178. end_pfn = 0;
  179. /*
  180. * Calculate the pfn range, and map the memory banks for this node.
  181. */
  182. for_each_nodebank(i, mi, node) {
  183. struct membank *bank = &mi->bank[i];
  184. unsigned long start, end;
  185. start = bank_pfn_start(bank);
  186. end = bank_pfn_end(bank);
  187. if (start_pfn > start)
  188. start_pfn = start;
  189. if (end_pfn < end)
  190. end_pfn = end;
  191. map_memory_bank(bank);
  192. }
  193. /*
  194. * If there is no memory in this node, ignore it.
  195. */
  196. if (end_pfn == 0)
  197. return end_pfn;
  198. /*
  199. * Allocate the bootmem bitmap page.
  200. */
  201. boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  202. boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
  203. /*
  204. * Initialise the bootmem allocator for this node, handing the
  205. * memory banks over to bootmem.
  206. */
  207. node_set_online(node);
  208. pgdat = NODE_DATA(node);
  209. init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
  210. for_each_nodebank(i, mi, node) {
  211. struct membank *bank = &mi->bank[i];
  212. free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
  213. memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
  214. }
  215. /*
  216. * Reserve the bootmem bitmap for this node.
  217. */
  218. reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
  219. boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
  220. return end_pfn;
  221. }
  222. static void __init bootmem_reserve_initrd(int node)
  223. {
  224. #ifdef CONFIG_BLK_DEV_INITRD
  225. pg_data_t *pgdat = NODE_DATA(node);
  226. int res;
  227. res = reserve_bootmem_node(pgdat, phys_initrd_start,
  228. phys_initrd_size, BOOTMEM_EXCLUSIVE);
  229. if (res == 0) {
  230. initrd_start = __phys_to_virt(phys_initrd_start);
  231. initrd_end = initrd_start + phys_initrd_size;
  232. } else {
  233. printk(KERN_ERR
  234. "INITRD: 0x%08lx+0x%08lx overlaps in-use "
  235. "memory region - disabling initrd\n",
  236. phys_initrd_start, phys_initrd_size);
  237. }
  238. #endif
  239. }
  240. static void __init bootmem_free_node(int node, struct meminfo *mi)
  241. {
  242. unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
  243. unsigned long start_pfn, end_pfn;
  244. pg_data_t *pgdat = NODE_DATA(node);
  245. int i;
  246. start_pfn = pgdat->bdata->node_min_pfn;
  247. end_pfn = pgdat->bdata->node_low_pfn;
  248. /*
  249. * initialise the zones within this node.
  250. */
  251. memset(zone_size, 0, sizeof(zone_size));
  252. memset(zhole_size, 0, sizeof(zhole_size));
  253. /*
  254. * The size of this node has already been determined. If we need
  255. * to do anything fancy with the allocation of this memory to the
  256. * zones, now is the time to do it.
  257. */
  258. zone_size[0] = end_pfn - start_pfn;
  259. /*
  260. * For each bank in this node, calculate the size of the holes.
  261. * holes = node_size - sum(bank_sizes_in_node)
  262. */
  263. zhole_size[0] = zone_size[0];
  264. for_each_nodebank(i, mi, node)
  265. zhole_size[0] -= bank_pfn_size(&mi->bank[i]);
  266. /*
  267. * Adjust the sizes according to any special requirements for
  268. * this machine type.
  269. */
  270. arch_adjust_zones(node, zone_size, zhole_size);
  271. free_area_init_node(node, zone_size, start_pfn, zhole_size);
  272. }
  273. void __init bootmem_init(struct meminfo *mi)
  274. {
  275. unsigned long memend_pfn = 0;
  276. int node, initrd_node;
  277. memcpy(&meminfo, mi, sizeof(meminfo));
  278. /*
  279. * Locate which node contains the ramdisk image, if any.
  280. */
  281. initrd_node = check_initrd(mi);
  282. /*
  283. * Run through each node initialising the bootmem allocator.
  284. */
  285. for_each_node(node) {
  286. unsigned long end_pfn = bootmem_init_node(node, mi);
  287. /*
  288. * Reserve any special node zero regions.
  289. */
  290. if (node == 0)
  291. reserve_node_zero(NODE_DATA(node));
  292. /*
  293. * If the initrd is in this node, reserve its memory.
  294. */
  295. if (node == initrd_node)
  296. bootmem_reserve_initrd(node);
  297. /*
  298. * Remember the highest memory PFN.
  299. */
  300. if (end_pfn > memend_pfn)
  301. memend_pfn = end_pfn;
  302. }
  303. /*
  304. * sparse_init() needs the bootmem allocator up and running.
  305. */
  306. sparse_init();
  307. /*
  308. * Now free memory in each node - free_area_init_node needs
  309. * the sparse mem_map arrays initialized by sparse_init()
  310. * for memmap_init_zone(), otherwise all PFNs are invalid.
  311. */
  312. for_each_node(node)
  313. bootmem_free_node(node, mi);
  314. high_memory = __va(memend_pfn << PAGE_SHIFT);
  315. /*
  316. * This doesn't seem to be used by the Linux memory manager any
  317. * more, but is used by ll_rw_block. If we can get rid of it, we
  318. * also get rid of some of the stuff above as well.
  319. *
  320. * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  321. * the system, not the maximum PFN.
  322. */
  323. max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
  324. }
  325. static inline void free_area(unsigned long addr, unsigned long end, char *s)
  326. {
  327. unsigned int size = (end - addr) >> 10;
  328. for (; addr < end; addr += PAGE_SIZE) {
  329. struct page *page = virt_to_page(addr);
  330. ClearPageReserved(page);
  331. init_page_count(page);
  332. free_page(addr);
  333. totalram_pages++;
  334. }
  335. if (size && s)
  336. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  337. }
  338. static inline void
  339. free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
  340. {
  341. struct page *start_pg, *end_pg;
  342. unsigned long pg, pgend;
  343. /*
  344. * Convert start_pfn/end_pfn to a struct page pointer.
  345. */
  346. start_pg = pfn_to_page(start_pfn);
  347. end_pg = pfn_to_page(end_pfn);
  348. /*
  349. * Convert to physical addresses, and
  350. * round start upwards and end downwards.
  351. */
  352. pg = PAGE_ALIGN(__pa(start_pg));
  353. pgend = __pa(end_pg) & PAGE_MASK;
  354. /*
  355. * If there are free pages between these,
  356. * free the section of the memmap array.
  357. */
  358. if (pg < pgend)
  359. free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
  360. }
  361. /*
  362. * The mem_map array can get very big. Free the unused area of the memory map.
  363. */
  364. static void __init free_unused_memmap_node(int node, struct meminfo *mi)
  365. {
  366. unsigned long bank_start, prev_bank_end = 0;
  367. unsigned int i;
  368. /*
  369. * [FIXME] This relies on each bank being in address order. This
  370. * may not be the case, especially if the user has provided the
  371. * information on the command line.
  372. */
  373. for_each_nodebank(i, mi, node) {
  374. struct membank *bank = &mi->bank[i];
  375. bank_start = bank_pfn_start(bank);
  376. if (bank_start < prev_bank_end) {
  377. printk(KERN_ERR "MEM: unordered memory banks. "
  378. "Not freeing memmap.\n");
  379. break;
  380. }
  381. /*
  382. * If we had a previous bank, and there is a space
  383. * between the current bank and the previous, free it.
  384. */
  385. if (prev_bank_end && prev_bank_end != bank_start)
  386. free_memmap(node, prev_bank_end, bank_start);
  387. prev_bank_end = bank_pfn_end(bank);
  388. }
  389. }
  390. /*
  391. * mem_init() marks the free areas in the mem_map and tells us how much
  392. * memory is free. This is done after various parts of the system have
  393. * claimed their memory after the kernel image.
  394. */
  395. void __init mem_init(void)
  396. {
  397. unsigned int codepages, datapages, initpages;
  398. int i, node;
  399. codepages = &_etext - &_text;
  400. datapages = &_end - &__data_start;
  401. initpages = &__init_end - &__init_begin;
  402. #ifndef CONFIG_DISCONTIGMEM
  403. max_mapnr = virt_to_page(high_memory) - mem_map;
  404. #endif
  405. /* this will put all unused low memory onto the freelists */
  406. for_each_online_node(node) {
  407. pg_data_t *pgdat = NODE_DATA(node);
  408. free_unused_memmap_node(node, &meminfo);
  409. if (pgdat->node_spanned_pages != 0)
  410. totalram_pages += free_all_bootmem_node(pgdat);
  411. }
  412. #ifdef CONFIG_SA1111
  413. /* now that our DMA memory is actually so designated, we can free it */
  414. free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
  415. #endif
  416. /*
  417. * Since our memory may not be contiguous, calculate the
  418. * real number of pages we have in this system
  419. */
  420. printk(KERN_INFO "Memory:");
  421. num_physpages = 0;
  422. for (i = 0; i < meminfo.nr_banks; i++) {
  423. num_physpages += bank_pfn_size(&meminfo.bank[i]);
  424. printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
  425. }
  426. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  427. printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
  428. "%dK data, %dK init)\n",
  429. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  430. codepages >> 10, datapages >> 10, initpages >> 10);
  431. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  432. extern int sysctl_overcommit_memory;
  433. /*
  434. * On a machine this small we won't get
  435. * anywhere without overcommit, so turn
  436. * it on by default.
  437. */
  438. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  439. }
  440. }
  441. void free_initmem(void)
  442. {
  443. if (!machine_is_integrator() && !machine_is_cintegrator()) {
  444. free_area((unsigned long)(&__init_begin),
  445. (unsigned long)(&__init_end),
  446. "init");
  447. }
  448. }
  449. #ifdef CONFIG_BLK_DEV_INITRD
  450. static int keep_initrd;
  451. void free_initrd_mem(unsigned long start, unsigned long end)
  452. {
  453. if (!keep_initrd)
  454. free_area(start, end, "initrd");
  455. }
  456. static int __init keepinitrd_setup(char *__unused)
  457. {
  458. keep_initrd = 1;
  459. return 1;
  460. }
  461. __setup("keepinitrd", keepinitrd_setup);
  462. #endif