init.c 14 KB

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