init.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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/sort.h>
  19. #include <linux/highmem.h>
  20. #include <asm/mach-types.h>
  21. #include <asm/sections.h>
  22. #include <asm/setup.h>
  23. #include <asm/sizes.h>
  24. #include <asm/tlb.h>
  25. #include <asm/mach/arch.h>
  26. #include <asm/mach/map.h>
  27. #include "mm.h"
  28. static unsigned long phys_initrd_start __initdata = 0;
  29. static unsigned long phys_initrd_size __initdata = 0;
  30. static void __init early_initrd(char **p)
  31. {
  32. unsigned long start, size;
  33. start = memparse(*p, p);
  34. if (**p == ',') {
  35. size = memparse((*p) + 1, p);
  36. phys_initrd_start = start;
  37. phys_initrd_size = size;
  38. }
  39. }
  40. __early_param("initrd=", early_initrd);
  41. static int __init parse_tag_initrd(const struct tag *tag)
  42. {
  43. printk(KERN_WARNING "ATAG_INITRD is deprecated; "
  44. "please update your bootloader.\n");
  45. phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
  46. phys_initrd_size = tag->u.initrd.size;
  47. return 0;
  48. }
  49. __tagtable(ATAG_INITRD, parse_tag_initrd);
  50. static int __init parse_tag_initrd2(const struct tag *tag)
  51. {
  52. phys_initrd_start = tag->u.initrd.start;
  53. phys_initrd_size = tag->u.initrd.size;
  54. return 0;
  55. }
  56. __tagtable(ATAG_INITRD2, parse_tag_initrd2);
  57. /*
  58. * This keeps memory configuration data used by a couple memory
  59. * initialization functions, as well as show_mem() for the skipping
  60. * of holes in the memory map. It is populated by arm_add_memory().
  61. */
  62. struct meminfo meminfo;
  63. void show_mem(void)
  64. {
  65. int free = 0, total = 0, reserved = 0;
  66. int shared = 0, cached = 0, slab = 0, node, i;
  67. struct meminfo * mi = &meminfo;
  68. printk("Mem-info:\n");
  69. show_free_areas();
  70. for_each_online_node(node) {
  71. pg_data_t *n = NODE_DATA(node);
  72. struct page *map = pgdat_page_nr(n, 0) - n->node_start_pfn;
  73. for_each_nodebank (i,mi,node) {
  74. struct membank *bank = &mi->bank[i];
  75. unsigned int pfn1, pfn2;
  76. struct page *page, *end;
  77. pfn1 = bank_pfn_start(bank);
  78. pfn2 = bank_pfn_end(bank);
  79. page = map + pfn1;
  80. end = map + pfn2;
  81. do {
  82. total++;
  83. if (PageReserved(page))
  84. reserved++;
  85. else if (PageSwapCache(page))
  86. cached++;
  87. else if (PageSlab(page))
  88. slab++;
  89. else if (!page_count(page))
  90. free++;
  91. else
  92. shared += page_count(page) - 1;
  93. page++;
  94. } while (page < end);
  95. }
  96. }
  97. printk("%d pages of RAM\n", total);
  98. printk("%d free pages\n", free);
  99. printk("%d reserved pages\n", reserved);
  100. printk("%d slab pages\n", slab);
  101. printk("%d pages shared\n", shared);
  102. printk("%d pages swap cached\n", cached);
  103. }
  104. static void __init find_node_limits(int node, struct meminfo *mi,
  105. unsigned long *min, unsigned long *max_low, unsigned long *max_high)
  106. {
  107. int i;
  108. *min = -1UL;
  109. *max_low = *max_high = 0;
  110. for_each_nodebank(i, mi, node) {
  111. struct membank *bank = &mi->bank[i];
  112. unsigned long start, end;
  113. start = bank_pfn_start(bank);
  114. end = bank_pfn_end(bank);
  115. if (*min > start)
  116. *min = start;
  117. if (*max_high < end)
  118. *max_high = end;
  119. if (bank->highmem)
  120. continue;
  121. if (*max_low < end)
  122. *max_low = end;
  123. }
  124. }
  125. /*
  126. * FIXME: We really want to avoid allocating the bootmap bitmap
  127. * over the top of the initrd. Hopefully, this is located towards
  128. * the start of a bank, so if we allocate the bootmap bitmap at
  129. * the end, we won't clash.
  130. */
  131. static unsigned int __init
  132. find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
  133. {
  134. unsigned int start_pfn, i, bootmap_pfn;
  135. start_pfn = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
  136. bootmap_pfn = 0;
  137. for_each_nodebank(i, mi, node) {
  138. struct membank *bank = &mi->bank[i];
  139. unsigned int start, end;
  140. start = bank_pfn_start(bank);
  141. end = bank_pfn_end(bank);
  142. if (end < start_pfn)
  143. continue;
  144. if (start < start_pfn)
  145. start = start_pfn;
  146. if (end <= start)
  147. continue;
  148. if (end - start >= bootmap_pages) {
  149. bootmap_pfn = start;
  150. break;
  151. }
  152. }
  153. if (bootmap_pfn == 0)
  154. BUG();
  155. return bootmap_pfn;
  156. }
  157. static int __init check_initrd(struct meminfo *mi)
  158. {
  159. int initrd_node = -2;
  160. #ifdef CONFIG_BLK_DEV_INITRD
  161. unsigned long end = phys_initrd_start + phys_initrd_size;
  162. /*
  163. * Make sure that the initrd is within a valid area of
  164. * memory.
  165. */
  166. if (phys_initrd_size) {
  167. unsigned int i;
  168. initrd_node = -1;
  169. for (i = 0; i < mi->nr_banks; i++) {
  170. struct membank *bank = &mi->bank[i];
  171. if (bank_phys_start(bank) <= phys_initrd_start &&
  172. end <= bank_phys_end(bank))
  173. initrd_node = bank->node;
  174. }
  175. }
  176. if (initrd_node == -1) {
  177. printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
  178. "physical memory - disabling initrd\n",
  179. phys_initrd_start, phys_initrd_size);
  180. phys_initrd_start = phys_initrd_size = 0;
  181. }
  182. #endif
  183. return initrd_node;
  184. }
  185. static inline void map_memory_bank(struct membank *bank)
  186. {
  187. #ifdef CONFIG_MMU
  188. struct map_desc map;
  189. map.pfn = bank_pfn_start(bank);
  190. map.virtual = __phys_to_virt(bank_phys_start(bank));
  191. map.length = bank_phys_size(bank);
  192. map.type = MT_MEMORY;
  193. create_mapping(&map);
  194. #endif
  195. }
  196. static void __init bootmem_init_node(int node, struct meminfo *mi,
  197. unsigned long start_pfn, unsigned long end_pfn)
  198. {
  199. unsigned long boot_pfn;
  200. unsigned int boot_pages;
  201. pg_data_t *pgdat;
  202. int i;
  203. /*
  204. * Map the memory banks for this node.
  205. */
  206. for_each_nodebank(i, mi, node) {
  207. struct membank *bank = &mi->bank[i];
  208. if (!bank->highmem)
  209. map_memory_bank(bank);
  210. }
  211. /*
  212. * Allocate the bootmem bitmap page.
  213. */
  214. boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  215. boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
  216. /*
  217. * Initialise the bootmem allocator for this node, handing the
  218. * memory banks over to bootmem.
  219. */
  220. node_set_online(node);
  221. pgdat = NODE_DATA(node);
  222. init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
  223. for_each_nodebank(i, mi, node) {
  224. struct membank *bank = &mi->bank[i];
  225. if (!bank->highmem)
  226. free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(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. #ifndef CONFIG_SPARSEMEM
  292. int pfn_valid(unsigned long pfn)
  293. {
  294. struct meminfo *mi = &meminfo;
  295. unsigned int left = 0, right = mi->nr_banks;
  296. do {
  297. unsigned int mid = (right + left) / 2;
  298. struct membank *bank = &mi->bank[mid];
  299. if (pfn < bank_pfn_start(bank))
  300. right = mid;
  301. else if (pfn >= bank_pfn_end(bank))
  302. left = mid + 1;
  303. else
  304. return 1;
  305. } while (left < right);
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(pfn_valid);
  309. static void arm_memory_present(struct meminfo *mi, int node)
  310. {
  311. }
  312. #else
  313. static void arm_memory_present(struct meminfo *mi, int node)
  314. {
  315. int i;
  316. for_each_nodebank(i, mi, node) {
  317. struct membank *bank = &mi->bank[i];
  318. memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
  319. }
  320. }
  321. #endif
  322. static int __init meminfo_cmp(const void *_a, const void *_b)
  323. {
  324. const struct membank *a = _a, *b = _b;
  325. long cmp = bank_pfn_start(a) - bank_pfn_start(b);
  326. return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
  327. }
  328. void __init bootmem_init(void)
  329. {
  330. struct meminfo *mi = &meminfo;
  331. unsigned long min, max_low, max_high;
  332. int node, initrd_node;
  333. sort(&mi->bank, mi->nr_banks, sizeof(mi->bank[0]), meminfo_cmp, NULL);
  334. /*
  335. * Locate which node contains the ramdisk image, if any.
  336. */
  337. initrd_node = check_initrd(mi);
  338. max_low = max_high = 0;
  339. /*
  340. * Run through each node initialising the bootmem allocator.
  341. */
  342. for_each_node(node) {
  343. unsigned long node_low, node_high;
  344. find_node_limits(node, mi, &min, &node_low, &node_high);
  345. if (node_low > max_low)
  346. max_low = node_low;
  347. if (node_high > max_high)
  348. max_high = node_high;
  349. /*
  350. * If there is no memory in this node, ignore it.
  351. * (We can't have nodes which have no lowmem)
  352. */
  353. if (node_low == 0)
  354. continue;
  355. bootmem_init_node(node, mi, min, node_low);
  356. /*
  357. * Reserve any special node zero regions.
  358. */
  359. if (node == 0)
  360. reserve_node_zero(NODE_DATA(node));
  361. /*
  362. * If the initrd is in this node, reserve its memory.
  363. */
  364. if (node == initrd_node)
  365. bootmem_reserve_initrd(node);
  366. /*
  367. * Sparsemem tries to allocate bootmem in memory_present(),
  368. * so must be done after the fixed reservations
  369. */
  370. arm_memory_present(mi, node);
  371. }
  372. /*
  373. * sparse_init() needs the bootmem allocator up and running.
  374. */
  375. sparse_init();
  376. /*
  377. * Now free memory in each node - free_area_init_node needs
  378. * the sparse mem_map arrays initialized by sparse_init()
  379. * for memmap_init_zone(), otherwise all PFNs are invalid.
  380. */
  381. for_each_node(node)
  382. bootmem_free_node(node, mi);
  383. high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
  384. /*
  385. * This doesn't seem to be used by the Linux memory manager any
  386. * more, but is used by ll_rw_block. If we can get rid of it, we
  387. * also get rid of some of the stuff above as well.
  388. *
  389. * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  390. * the system, not the maximum PFN.
  391. */
  392. max_low_pfn = max_low - PHYS_PFN_OFFSET;
  393. max_pfn = max_high - PHYS_PFN_OFFSET;
  394. }
  395. static inline int free_area(unsigned long pfn, unsigned long end, char *s)
  396. {
  397. unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
  398. for (; pfn < end; pfn++) {
  399. struct page *page = pfn_to_page(pfn);
  400. ClearPageReserved(page);
  401. init_page_count(page);
  402. __free_page(page);
  403. pages++;
  404. }
  405. if (size && s)
  406. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  407. return pages;
  408. }
  409. static inline void
  410. free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
  411. {
  412. struct page *start_pg, *end_pg;
  413. unsigned long pg, pgend;
  414. /*
  415. * Convert start_pfn/end_pfn to a struct page pointer.
  416. */
  417. start_pg = pfn_to_page(start_pfn - 1) + 1;
  418. end_pg = pfn_to_page(end_pfn);
  419. /*
  420. * Convert to physical addresses, and
  421. * round start upwards and end downwards.
  422. */
  423. pg = PAGE_ALIGN(__pa(start_pg));
  424. pgend = __pa(end_pg) & PAGE_MASK;
  425. /*
  426. * If there are free pages between these,
  427. * free the section of the memmap array.
  428. */
  429. if (pg < pgend)
  430. free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
  431. }
  432. /*
  433. * The mem_map array can get very big. Free the unused area of the memory map.
  434. */
  435. static void __init free_unused_memmap_node(int node, struct meminfo *mi)
  436. {
  437. unsigned long bank_start, prev_bank_end = 0;
  438. unsigned int i;
  439. /*
  440. * [FIXME] This relies on each bank being in address order. This
  441. * may not be the case, especially if the user has provided the
  442. * information on the command line.
  443. */
  444. for_each_nodebank(i, mi, node) {
  445. struct membank *bank = &mi->bank[i];
  446. bank_start = bank_pfn_start(bank);
  447. if (bank_start < prev_bank_end) {
  448. printk(KERN_ERR "MEM: unordered memory banks. "
  449. "Not freeing memmap.\n");
  450. break;
  451. }
  452. /*
  453. * If we had a previous bank, and there is a space
  454. * between the current bank and the previous, free it.
  455. */
  456. if (prev_bank_end && prev_bank_end != bank_start)
  457. free_memmap(node, prev_bank_end, bank_start);
  458. prev_bank_end = bank_pfn_end(bank);
  459. }
  460. }
  461. /*
  462. * mem_init() marks the free areas in the mem_map and tells us how much
  463. * memory is free. This is done after various parts of the system have
  464. * claimed their memory after the kernel image.
  465. */
  466. void __init mem_init(void)
  467. {
  468. unsigned int codesize, datasize, initsize;
  469. int i, node;
  470. #ifndef CONFIG_DISCONTIGMEM
  471. max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
  472. #endif
  473. /* this will put all unused low memory onto the freelists */
  474. for_each_online_node(node) {
  475. pg_data_t *pgdat = NODE_DATA(node);
  476. free_unused_memmap_node(node, &meminfo);
  477. if (pgdat->node_spanned_pages != 0)
  478. totalram_pages += free_all_bootmem_node(pgdat);
  479. }
  480. #ifdef CONFIG_SA1111
  481. /* now that our DMA memory is actually so designated, we can free it */
  482. totalram_pages += free_area(PHYS_PFN_OFFSET,
  483. __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
  484. #endif
  485. #ifdef CONFIG_HIGHMEM
  486. /* set highmem page free */
  487. for_each_online_node(node) {
  488. for_each_nodebank (i, &meminfo, node) {
  489. unsigned long start = bank_pfn_start(&meminfo.bank[i]);
  490. unsigned long end = bank_pfn_end(&meminfo.bank[i]);
  491. if (start >= max_low_pfn + PHYS_PFN_OFFSET)
  492. totalhigh_pages += free_area(start, end, NULL);
  493. }
  494. }
  495. totalram_pages += totalhigh_pages;
  496. #endif
  497. /*
  498. * Since our memory may not be contiguous, calculate the
  499. * real number of pages we have in this system
  500. */
  501. printk(KERN_INFO "Memory:");
  502. num_physpages = 0;
  503. for (i = 0; i < meminfo.nr_banks; i++) {
  504. num_physpages += bank_pfn_size(&meminfo.bank[i]);
  505. printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
  506. }
  507. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  508. codesize = _etext - _text;
  509. datasize = _end - _data;
  510. initsize = __init_end - __init_begin;
  511. printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
  512. "%dK data, %dK init, %luK highmem)\n",
  513. nr_free_pages() << (PAGE_SHIFT-10), codesize >> 10,
  514. datasize >> 10, initsize >> 10,
  515. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
  516. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  517. extern int sysctl_overcommit_memory;
  518. /*
  519. * On a machine this small we won't get
  520. * anywhere without overcommit, so turn
  521. * it on by default.
  522. */
  523. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  524. }
  525. }
  526. void free_initmem(void)
  527. {
  528. #ifdef CONFIG_HAVE_TCM
  529. extern char *__tcm_start, *__tcm_end;
  530. totalram_pages += free_area(__phys_to_pfn(__pa(__tcm_start)),
  531. __phys_to_pfn(__pa(__tcm_end)),
  532. "TCM link");
  533. #endif
  534. if (!machine_is_integrator() && !machine_is_cintegrator())
  535. totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
  536. __phys_to_pfn(__pa(__init_end)),
  537. "init");
  538. }
  539. #ifdef CONFIG_BLK_DEV_INITRD
  540. static int keep_initrd;
  541. void free_initrd_mem(unsigned long start, unsigned long end)
  542. {
  543. if (!keep_initrd)
  544. totalram_pages += free_area(__phys_to_pfn(__pa(start)),
  545. __phys_to_pfn(__pa(end)),
  546. "initrd");
  547. }
  548. static int __init keepinitrd_setup(char *__unused)
  549. {
  550. keep_initrd = 1;
  551. return 1;
  552. }
  553. __setup("keepinitrd", keepinitrd_setup);
  554. #endif