init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * linux/arch/arm/mm/init.c
  3. *
  4. * Copyright (C) 1995-2002 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/config.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/swap.h>
  15. #include <linux/init.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/mman.h>
  18. #include <linux/nodemask.h>
  19. #include <linux/initrd.h>
  20. #include <asm/mach-types.h>
  21. #include <asm/hardware.h>
  22. #include <asm/setup.h>
  23. #include <asm/tlb.h>
  24. #include <asm/mach/arch.h>
  25. #include <asm/mach/map.h>
  26. #define TABLE_SIZE (2 * PTRS_PER_PTE * sizeof(pte_t))
  27. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  28. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  29. extern void _stext, _text, _etext, __data_start, _end, __init_begin, __init_end;
  30. extern unsigned long phys_initrd_start;
  31. extern unsigned long phys_initrd_size;
  32. /*
  33. * The sole use of this is to pass memory configuration
  34. * data from paging_init to mem_init.
  35. */
  36. static struct meminfo meminfo __initdata = { 0, };
  37. /*
  38. * empty_zero_page is a special page that is used for
  39. * zero-initialized data and COW.
  40. */
  41. struct page *empty_zero_page;
  42. void show_mem(void)
  43. {
  44. int free = 0, total = 0, reserved = 0;
  45. int shared = 0, cached = 0, slab = 0, node;
  46. printk("Mem-info:\n");
  47. show_free_areas();
  48. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  49. for_each_online_node(node) {
  50. struct page *page, *end;
  51. page = NODE_MEM_MAP(node);
  52. end = page + NODE_DATA(node)->node_spanned_pages;
  53. do {
  54. total++;
  55. if (PageReserved(page))
  56. reserved++;
  57. else if (PageSwapCache(page))
  58. cached++;
  59. else if (PageSlab(page))
  60. slab++;
  61. else if (!page_count(page))
  62. free++;
  63. else
  64. shared += page_count(page) - 1;
  65. page++;
  66. } while (page < end);
  67. }
  68. printk("%d pages of RAM\n", total);
  69. printk("%d free pages\n", free);
  70. printk("%d reserved pages\n", reserved);
  71. printk("%d slab pages\n", slab);
  72. printk("%d pages shared\n", shared);
  73. printk("%d pages swap cached\n", cached);
  74. }
  75. struct node_info {
  76. unsigned int start;
  77. unsigned int end;
  78. int bootmap_pages;
  79. };
  80. #define O_PFN_DOWN(x) ((x) >> PAGE_SHIFT)
  81. #define V_PFN_DOWN(x) O_PFN_DOWN(__pa(x))
  82. #define O_PFN_UP(x) (PAGE_ALIGN(x) >> PAGE_SHIFT)
  83. #define V_PFN_UP(x) O_PFN_UP(__pa(x))
  84. #define PFN_SIZE(x) ((x) >> PAGE_SHIFT)
  85. #define PFN_RANGE(s,e) PFN_SIZE(PAGE_ALIGN((unsigned long)(e)) - \
  86. (((unsigned long)(s)) & PAGE_MASK))
  87. /*
  88. * FIXME: We really want to avoid allocating the bootmap bitmap
  89. * over the top of the initrd. Hopefully, this is located towards
  90. * the start of a bank, so if we allocate the bootmap bitmap at
  91. * the end, we won't clash.
  92. */
  93. static unsigned int __init
  94. find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
  95. {
  96. unsigned int start_pfn, bank, bootmap_pfn;
  97. start_pfn = V_PFN_UP(&_end);
  98. bootmap_pfn = 0;
  99. for (bank = 0; bank < mi->nr_banks; bank ++) {
  100. unsigned int start, end;
  101. if (mi->bank[bank].node != node)
  102. continue;
  103. start = O_PFN_UP(mi->bank[bank].start);
  104. end = O_PFN_DOWN(mi->bank[bank].size +
  105. mi->bank[bank].start);
  106. if (end < start_pfn)
  107. continue;
  108. if (start < start_pfn)
  109. start = start_pfn;
  110. if (end <= start)
  111. continue;
  112. if (end - start >= bootmap_pages) {
  113. bootmap_pfn = start;
  114. break;
  115. }
  116. }
  117. if (bootmap_pfn == 0)
  118. BUG();
  119. return bootmap_pfn;
  120. }
  121. /*
  122. * Scan the memory info structure and pull out:
  123. * - the end of memory
  124. * - the number of nodes
  125. * - the pfn range of each node
  126. * - the number of bootmem bitmap pages
  127. */
  128. static unsigned int __init
  129. find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
  130. {
  131. unsigned int i, bootmem_pages = 0, memend_pfn = 0;
  132. for (i = 0; i < MAX_NUMNODES; i++) {
  133. np[i].start = -1U;
  134. np[i].end = 0;
  135. np[i].bootmap_pages = 0;
  136. }
  137. for (i = 0; i < mi->nr_banks; i++) {
  138. unsigned long start, end;
  139. int node;
  140. if (mi->bank[i].size == 0) {
  141. /*
  142. * Mark this bank with an invalid node number
  143. */
  144. mi->bank[i].node = -1;
  145. continue;
  146. }
  147. node = mi->bank[i].node;
  148. /*
  149. * Make sure we haven't exceeded the maximum number of nodes
  150. * that we have in this configuration. If we have, we're in
  151. * trouble. (maybe we ought to limit, instead of bugging?)
  152. */
  153. if (node >= MAX_NUMNODES)
  154. BUG();
  155. node_set_online(node);
  156. /*
  157. * Get the start and end pfns for this bank
  158. */
  159. start = O_PFN_UP(mi->bank[i].start);
  160. end = O_PFN_DOWN(mi->bank[i].start + mi->bank[i].size);
  161. if (np[node].start > start)
  162. np[node].start = start;
  163. if (np[node].end < end)
  164. np[node].end = end;
  165. if (memend_pfn < end)
  166. memend_pfn = end;
  167. }
  168. /*
  169. * Calculate the number of pages we require to
  170. * store the bootmem bitmaps.
  171. */
  172. for_each_online_node(i) {
  173. if (np[i].end == 0)
  174. continue;
  175. np[i].bootmap_pages = bootmem_bootmap_pages(np[i].end -
  176. np[i].start);
  177. bootmem_pages += np[i].bootmap_pages;
  178. }
  179. high_memory = __va(memend_pfn << PAGE_SHIFT);
  180. /*
  181. * This doesn't seem to be used by the Linux memory
  182. * manager any more. If we can get rid of it, we
  183. * also get rid of some of the stuff above as well.
  184. *
  185. * Note: max_low_pfn and max_pfn reflect the number
  186. * of _pages_ in the system, not the maximum PFN.
  187. */
  188. max_low_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
  189. max_pfn = memend_pfn - O_PFN_DOWN(PHYS_OFFSET);
  190. return bootmem_pages;
  191. }
  192. static int __init check_initrd(struct meminfo *mi)
  193. {
  194. int initrd_node = -2;
  195. #ifdef CONFIG_BLK_DEV_INITRD
  196. unsigned long end = phys_initrd_start + phys_initrd_size;
  197. /*
  198. * Make sure that the initrd is within a valid area of
  199. * memory.
  200. */
  201. if (phys_initrd_size) {
  202. unsigned int i;
  203. initrd_node = -1;
  204. for (i = 0; i < mi->nr_banks; i++) {
  205. unsigned long bank_end;
  206. bank_end = mi->bank[i].start + mi->bank[i].size;
  207. if (mi->bank[i].start <= phys_initrd_start &&
  208. end <= bank_end)
  209. initrd_node = mi->bank[i].node;
  210. }
  211. }
  212. if (initrd_node == -1) {
  213. printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
  214. "physical memory - disabling initrd\n",
  215. phys_initrd_start, end);
  216. phys_initrd_start = phys_initrd_size = 0;
  217. }
  218. #endif
  219. return initrd_node;
  220. }
  221. /*
  222. * Reserve the various regions of node 0
  223. */
  224. static __init void reserve_node_zero(unsigned int bootmap_pfn, unsigned int bootmap_pages)
  225. {
  226. pg_data_t *pgdat = NODE_DATA(0);
  227. unsigned long res_size = 0;
  228. /*
  229. * Register the kernel text and data with bootmem.
  230. * Note that this can only be in node 0.
  231. */
  232. #ifdef CONFIG_XIP_KERNEL
  233. reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start);
  234. #else
  235. reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
  236. #endif
  237. /*
  238. * Reserve the page tables. These are already in use,
  239. * and can only be in node 0.
  240. */
  241. reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
  242. PTRS_PER_PGD * sizeof(pgd_t));
  243. /*
  244. * And don't forget to reserve the allocator bitmap,
  245. * which will be freed later.
  246. */
  247. reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
  248. bootmap_pages << PAGE_SHIFT);
  249. /*
  250. * Hmm... This should go elsewhere, but we really really need to
  251. * stop things allocating the low memory; ideally we need a better
  252. * implementation of GFP_DMA which does not assume that DMA-able
  253. * memory starts at zero.
  254. */
  255. if (machine_is_integrator() || machine_is_cintegrator())
  256. res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
  257. /*
  258. * These should likewise go elsewhere. They pre-reserve the
  259. * screen memory region at the start of main system memory.
  260. */
  261. if (machine_is_edb7211())
  262. res_size = 0x00020000;
  263. if (machine_is_p720t())
  264. res_size = 0x00014000;
  265. #ifdef CONFIG_SA1111
  266. /*
  267. * Because of the SA1111 DMA bug, we want to preserve our
  268. * precious DMA-able memory...
  269. */
  270. res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
  271. #endif
  272. if (res_size)
  273. reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
  274. }
  275. /*
  276. * Register all available RAM in this node with the bootmem allocator.
  277. */
  278. static inline void free_bootmem_node_bank(int node, struct meminfo *mi)
  279. {
  280. pg_data_t *pgdat = NODE_DATA(node);
  281. int bank;
  282. for (bank = 0; bank < mi->nr_banks; bank++)
  283. if (mi->bank[bank].node == node)
  284. free_bootmem_node(pgdat, mi->bank[bank].start,
  285. mi->bank[bank].size);
  286. }
  287. /*
  288. * Initialise the bootmem allocator for all nodes. This is called
  289. * early during the architecture specific initialisation.
  290. */
  291. static void __init bootmem_init(struct meminfo *mi)
  292. {
  293. struct node_info node_info[MAX_NUMNODES], *np = node_info;
  294. unsigned int bootmap_pages, bootmap_pfn, map_pg;
  295. int node, initrd_node;
  296. bootmap_pages = find_memend_and_nodes(mi, np);
  297. bootmap_pfn = find_bootmap_pfn(0, mi, bootmap_pages);
  298. initrd_node = check_initrd(mi);
  299. map_pg = bootmap_pfn;
  300. /*
  301. * Initialise the bootmem nodes.
  302. *
  303. * What we really want to do is:
  304. *
  305. * unmap_all_regions_except_kernel();
  306. * for_each_node_in_reverse_order(node) {
  307. * map_node(node);
  308. * allocate_bootmem_map(node);
  309. * init_bootmem_node(node);
  310. * free_bootmem_node(node);
  311. * }
  312. *
  313. * but this is a 2.5-type change. For now, we just set
  314. * the nodes up in reverse order.
  315. *
  316. * (we could also do with rolling bootmem_init and paging_init
  317. * into one generic "memory_init" type function).
  318. */
  319. np += num_online_nodes() - 1;
  320. for (node = num_online_nodes() - 1; node >= 0; node--, np--) {
  321. /*
  322. * If there are no pages in this node, ignore it.
  323. * Note that node 0 must always have some pages.
  324. */
  325. if (np->end == 0 || !node_online(node)) {
  326. if (node == 0)
  327. BUG();
  328. continue;
  329. }
  330. /*
  331. * Initialise the bootmem allocator.
  332. */
  333. init_bootmem_node(NODE_DATA(node), map_pg, np->start, np->end);
  334. free_bootmem_node_bank(node, mi);
  335. map_pg += np->bootmap_pages;
  336. /*
  337. * If this is node 0, we need to reserve some areas ASAP -
  338. * we may use bootmem on node 0 to setup the other nodes.
  339. */
  340. if (node == 0)
  341. reserve_node_zero(bootmap_pfn, bootmap_pages);
  342. }
  343. #ifdef CONFIG_BLK_DEV_INITRD
  344. if (phys_initrd_size && initrd_node >= 0) {
  345. reserve_bootmem_node(NODE_DATA(initrd_node), phys_initrd_start,
  346. phys_initrd_size);
  347. initrd_start = __phys_to_virt(phys_initrd_start);
  348. initrd_end = initrd_start + phys_initrd_size;
  349. }
  350. #endif
  351. BUG_ON(map_pg != bootmap_pfn + bootmap_pages);
  352. }
  353. /*
  354. * paging_init() sets up the page tables, initialises the zone memory
  355. * maps, and sets up the zero page, bad page and bad page tables.
  356. */
  357. void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
  358. {
  359. void *zero_page;
  360. int node;
  361. bootmem_init(mi);
  362. memcpy(&meminfo, mi, sizeof(meminfo));
  363. /*
  364. * allocate the zero page. Note that we count on this going ok.
  365. */
  366. zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
  367. /*
  368. * initialise the page tables.
  369. */
  370. memtable_init(mi);
  371. if (mdesc->map_io)
  372. mdesc->map_io();
  373. flush_tlb_all();
  374. /*
  375. * initialise the zones within each node
  376. */
  377. for_each_online_node(node) {
  378. unsigned long zone_size[MAX_NR_ZONES];
  379. unsigned long zhole_size[MAX_NR_ZONES];
  380. struct bootmem_data *bdata;
  381. pg_data_t *pgdat;
  382. int i;
  383. /*
  384. * Initialise the zone size information.
  385. */
  386. for (i = 0; i < MAX_NR_ZONES; i++) {
  387. zone_size[i] = 0;
  388. zhole_size[i] = 0;
  389. }
  390. pgdat = NODE_DATA(node);
  391. bdata = pgdat->bdata;
  392. /*
  393. * The size of this node has already been determined.
  394. * If we need to do anything fancy with the allocation
  395. * of this memory to the zones, now is the time to do
  396. * it.
  397. */
  398. zone_size[0] = bdata->node_low_pfn -
  399. (bdata->node_boot_start >> PAGE_SHIFT);
  400. /*
  401. * If this zone has zero size, skip it.
  402. */
  403. if (!zone_size[0])
  404. continue;
  405. /*
  406. * For each bank in this node, calculate the size of the
  407. * holes. holes = node_size - sum(bank_sizes_in_node)
  408. */
  409. zhole_size[0] = zone_size[0];
  410. for (i = 0; i < mi->nr_banks; i++) {
  411. if (mi->bank[i].node != node)
  412. continue;
  413. zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
  414. }
  415. /*
  416. * Adjust the sizes according to any special
  417. * requirements for this machine type.
  418. */
  419. arch_adjust_zones(node, zone_size, zhole_size);
  420. free_area_init_node(node, pgdat, zone_size,
  421. bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
  422. }
  423. /*
  424. * finish off the bad pages once
  425. * the mem_map is initialised
  426. */
  427. memzero(zero_page, PAGE_SIZE);
  428. empty_zero_page = virt_to_page(zero_page);
  429. flush_dcache_page(empty_zero_page);
  430. }
  431. static inline void free_area(unsigned long addr, unsigned long end, char *s)
  432. {
  433. unsigned int size = (end - addr) >> 10;
  434. for (; addr < end; addr += PAGE_SIZE) {
  435. struct page *page = virt_to_page(addr);
  436. ClearPageReserved(page);
  437. set_page_count(page, 1);
  438. free_page(addr);
  439. totalram_pages++;
  440. }
  441. if (size && s)
  442. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  443. }
  444. /*
  445. * mem_init() marks the free areas in the mem_map and tells us how much
  446. * memory is free. This is done after various parts of the system have
  447. * claimed their memory after the kernel image.
  448. */
  449. void __init mem_init(void)
  450. {
  451. unsigned int codepages, datapages, initpages;
  452. int i, node;
  453. codepages = &_etext - &_text;
  454. datapages = &_end - &__data_start;
  455. initpages = &__init_end - &__init_begin;
  456. #ifndef CONFIG_DISCONTIGMEM
  457. max_mapnr = virt_to_page(high_memory) - mem_map;
  458. #endif
  459. /*
  460. * We may have non-contiguous memory.
  461. */
  462. if (meminfo.nr_banks != 1)
  463. create_memmap_holes(&meminfo);
  464. /* this will put all unused low memory onto the freelists */
  465. for_each_online_node(node) {
  466. pg_data_t *pgdat = NODE_DATA(node);
  467. if (pgdat->node_spanned_pages != 0)
  468. totalram_pages += free_all_bootmem_node(pgdat);
  469. }
  470. #ifdef CONFIG_SA1111
  471. /* now that our DMA memory is actually so designated, we can free it */
  472. free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
  473. #endif
  474. /*
  475. * Since our memory may not be contiguous, calculate the
  476. * real number of pages we have in this system
  477. */
  478. printk(KERN_INFO "Memory:");
  479. num_physpages = 0;
  480. for (i = 0; i < meminfo.nr_banks; i++) {
  481. num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
  482. printk(" %ldMB", meminfo.bank[i].size >> 20);
  483. }
  484. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  485. printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
  486. "%dK data, %dK init)\n",
  487. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  488. codepages >> 10, datapages >> 10, initpages >> 10);
  489. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  490. extern int sysctl_overcommit_memory;
  491. /*
  492. * On a machine this small we won't get
  493. * anywhere without overcommit, so turn
  494. * it on by default.
  495. */
  496. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  497. }
  498. }
  499. void free_initmem(void)
  500. {
  501. if (!machine_is_integrator() && !machine_is_cintegrator()) {
  502. free_area((unsigned long)(&__init_begin),
  503. (unsigned long)(&__init_end),
  504. "init");
  505. }
  506. }
  507. #ifdef CONFIG_BLK_DEV_INITRD
  508. static int keep_initrd;
  509. void free_initrd_mem(unsigned long start, unsigned long end)
  510. {
  511. if (!keep_initrd)
  512. free_area(start, end, "initrd");
  513. }
  514. static int __init keepinitrd_setup(char *__unused)
  515. {
  516. keep_initrd = 1;
  517. return 1;
  518. }
  519. __setup("keepinitrd", keepinitrd_setup);
  520. #endif