init.c 14 KB

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