init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * linux/arch/unicore32/mm/init.c
  3. *
  4. * Copyright (C) 2010 GUAN Xue-tao
  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 <linux/gfp.h>
  20. #include <linux/memblock.h>
  21. #include <linux/sort.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/export.h>
  24. #include <asm/sections.h>
  25. #include <asm/setup.h>
  26. #include <asm/sizes.h>
  27. #include <asm/tlb.h>
  28. #include <asm/memblock.h>
  29. #include <mach/map.h>
  30. #include "mm.h"
  31. static unsigned long phys_initrd_start __initdata = 0x01000000;
  32. static unsigned long phys_initrd_size __initdata = SZ_8M;
  33. static int __init early_initrd(char *p)
  34. {
  35. unsigned long start, size;
  36. char *endp;
  37. start = memparse(p, &endp);
  38. if (*endp == ',') {
  39. size = memparse(endp + 1, NULL);
  40. phys_initrd_start = start;
  41. phys_initrd_size = size;
  42. }
  43. return 0;
  44. }
  45. early_param("initrd", early_initrd);
  46. /*
  47. * This keeps memory configuration data used by a couple memory
  48. * initialization functions, as well as show_mem() for the skipping
  49. * of holes in the memory map. It is populated by uc32_add_memory().
  50. */
  51. struct meminfo meminfo;
  52. void show_mem(unsigned int filter)
  53. {
  54. int free = 0, total = 0, reserved = 0;
  55. int shared = 0, cached = 0, slab = 0, i;
  56. struct meminfo *mi = &meminfo;
  57. printk(KERN_DEFAULT "Mem-info:\n");
  58. show_free_areas(filter);
  59. if (filter & SHOW_MEM_FILTER_PAGE_COUNT)
  60. return;
  61. for_each_bank(i, mi) {
  62. struct membank *bank = &mi->bank[i];
  63. unsigned int pfn1, pfn2;
  64. struct page *page, *end;
  65. pfn1 = bank_pfn_start(bank);
  66. pfn2 = bank_pfn_end(bank);
  67. page = pfn_to_page(pfn1);
  68. end = pfn_to_page(pfn2 - 1) + 1;
  69. do {
  70. total++;
  71. if (PageReserved(page))
  72. reserved++;
  73. else if (PageSwapCache(page))
  74. cached++;
  75. else if (PageSlab(page))
  76. slab++;
  77. else if (!page_count(page))
  78. free++;
  79. else
  80. shared += page_count(page) - 1;
  81. page++;
  82. } while (page < end);
  83. }
  84. printk(KERN_DEFAULT "%d pages of RAM\n", total);
  85. printk(KERN_DEFAULT "%d free pages\n", free);
  86. printk(KERN_DEFAULT "%d reserved pages\n", reserved);
  87. printk(KERN_DEFAULT "%d slab pages\n", slab);
  88. printk(KERN_DEFAULT "%d pages shared\n", shared);
  89. printk(KERN_DEFAULT "%d pages swap cached\n", cached);
  90. }
  91. static void __init find_limits(unsigned long *min, unsigned long *max_low,
  92. unsigned long *max_high)
  93. {
  94. struct meminfo *mi = &meminfo;
  95. int i;
  96. *min = -1UL;
  97. *max_low = *max_high = 0;
  98. for_each_bank(i, mi) {
  99. struct membank *bank = &mi->bank[i];
  100. unsigned long start, end;
  101. start = bank_pfn_start(bank);
  102. end = bank_pfn_end(bank);
  103. if (*min > start)
  104. *min = start;
  105. if (*max_high < end)
  106. *max_high = end;
  107. if (bank->highmem)
  108. continue;
  109. if (*max_low < end)
  110. *max_low = end;
  111. }
  112. }
  113. static void __init uc32_bootmem_init(unsigned long start_pfn,
  114. unsigned long end_pfn)
  115. {
  116. struct memblock_region *reg;
  117. unsigned int boot_pages;
  118. phys_addr_t bitmap;
  119. pg_data_t *pgdat;
  120. /*
  121. * Allocate the bootmem bitmap page. This must be in a region
  122. * of memory which has already been mapped.
  123. */
  124. boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  125. bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
  126. __pfn_to_phys(end_pfn));
  127. /*
  128. * Initialise the bootmem allocator, handing the
  129. * memory banks over to bootmem.
  130. */
  131. node_set_online(0);
  132. pgdat = NODE_DATA(0);
  133. init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
  134. /* Free the lowmem regions from memblock into bootmem. */
  135. for_each_memblock(memory, reg) {
  136. unsigned long start = memblock_region_memory_base_pfn(reg);
  137. unsigned long end = memblock_region_memory_end_pfn(reg);
  138. if (end >= end_pfn)
  139. end = end_pfn;
  140. if (start >= end)
  141. break;
  142. free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT);
  143. }
  144. /* Reserve the lowmem memblock reserved regions in bootmem. */
  145. for_each_memblock(reserved, reg) {
  146. unsigned long start = memblock_region_reserved_base_pfn(reg);
  147. unsigned long end = memblock_region_reserved_end_pfn(reg);
  148. if (end >= end_pfn)
  149. end = end_pfn;
  150. if (start >= end)
  151. break;
  152. reserve_bootmem(__pfn_to_phys(start),
  153. (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT);
  154. }
  155. }
  156. static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
  157. unsigned long max_high)
  158. {
  159. unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
  160. struct memblock_region *reg;
  161. /*
  162. * initialise the zones.
  163. */
  164. memset(zone_size, 0, sizeof(zone_size));
  165. /*
  166. * The memory size has already been determined. If we need
  167. * to do anything fancy with the allocation of this memory
  168. * to the zones, now is the time to do it.
  169. */
  170. zone_size[0] = max_low - min;
  171. /*
  172. * Calculate the size of the holes.
  173. * holes = node_size - sum(bank_sizes)
  174. */
  175. memcpy(zhole_size, zone_size, sizeof(zhole_size));
  176. for_each_memblock(memory, reg) {
  177. unsigned long start = memblock_region_memory_base_pfn(reg);
  178. unsigned long end = memblock_region_memory_end_pfn(reg);
  179. if (start < max_low) {
  180. unsigned long low_end = min(end, max_low);
  181. zhole_size[0] -= low_end - start;
  182. }
  183. }
  184. /*
  185. * Adjust the sizes according to any special requirements for
  186. * this machine type.
  187. */
  188. arch_adjust_zones(zone_size, zhole_size);
  189. free_area_init_node(0, zone_size, min, zhole_size);
  190. }
  191. int pfn_valid(unsigned long pfn)
  192. {
  193. return memblock_is_memory(pfn << PAGE_SHIFT);
  194. }
  195. EXPORT_SYMBOL(pfn_valid);
  196. static void uc32_memory_present(void)
  197. {
  198. }
  199. static int __init meminfo_cmp(const void *_a, const void *_b)
  200. {
  201. const struct membank *a = _a, *b = _b;
  202. long cmp = bank_pfn_start(a) - bank_pfn_start(b);
  203. return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
  204. }
  205. void __init uc32_memblock_init(struct meminfo *mi)
  206. {
  207. int i;
  208. sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
  209. meminfo_cmp, NULL);
  210. for (i = 0; i < mi->nr_banks; i++)
  211. memblock_add(mi->bank[i].start, mi->bank[i].size);
  212. /* Register the kernel text, kernel data and initrd with memblock. */
  213. memblock_reserve(__pa(_text), _end - _text);
  214. #ifdef CONFIG_BLK_DEV_INITRD
  215. if (phys_initrd_size) {
  216. memblock_reserve(phys_initrd_start, phys_initrd_size);
  217. /* Now convert initrd to virtual addresses */
  218. initrd_start = __phys_to_virt(phys_initrd_start);
  219. initrd_end = initrd_start + phys_initrd_size;
  220. }
  221. #endif
  222. uc32_mm_memblock_reserve();
  223. memblock_allow_resize();
  224. memblock_dump_all();
  225. }
  226. void __init bootmem_init(void)
  227. {
  228. unsigned long min, max_low, max_high;
  229. max_low = max_high = 0;
  230. find_limits(&min, &max_low, &max_high);
  231. uc32_bootmem_init(min, max_low);
  232. #ifdef CONFIG_SWIOTLB
  233. swiotlb_init(1);
  234. #endif
  235. /*
  236. * Sparsemem tries to allocate bootmem in memory_present(),
  237. * so must be done after the fixed reservations
  238. */
  239. uc32_memory_present();
  240. /*
  241. * sparse_init() needs the bootmem allocator up and running.
  242. */
  243. sparse_init();
  244. /*
  245. * Now free the memory - free_area_init_node needs
  246. * the sparse mem_map arrays initialized by sparse_init()
  247. * for memmap_init_zone(), otherwise all PFNs are invalid.
  248. */
  249. uc32_bootmem_free(min, max_low, max_high);
  250. high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
  251. /*
  252. * This doesn't seem to be used by the Linux memory manager any
  253. * more, but is used by ll_rw_block. If we can get rid of it, we
  254. * also get rid of some of the stuff above as well.
  255. *
  256. * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
  257. * the system, not the maximum PFN.
  258. */
  259. max_low_pfn = max_low - PHYS_PFN_OFFSET;
  260. max_pfn = max_high - PHYS_PFN_OFFSET;
  261. }
  262. static inline void
  263. free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  264. {
  265. struct page *start_pg, *end_pg;
  266. unsigned long pg, pgend;
  267. /*
  268. * Convert start_pfn/end_pfn to a struct page pointer.
  269. */
  270. start_pg = pfn_to_page(start_pfn - 1) + 1;
  271. end_pg = pfn_to_page(end_pfn);
  272. /*
  273. * Convert to physical addresses, and
  274. * round start upwards and end downwards.
  275. */
  276. pg = PAGE_ALIGN(__pa(start_pg));
  277. pgend = __pa(end_pg) & PAGE_MASK;
  278. /*
  279. * If there are free pages between these,
  280. * free the section of the memmap array.
  281. */
  282. if (pg < pgend)
  283. free_bootmem(pg, pgend - pg);
  284. }
  285. /*
  286. * The mem_map array can get very big. Free the unused area of the memory map.
  287. */
  288. static void __init free_unused_memmap(struct meminfo *mi)
  289. {
  290. unsigned long bank_start, prev_bank_end = 0;
  291. unsigned int i;
  292. /*
  293. * This relies on each bank being in address order.
  294. * The banks are sorted previously in bootmem_init().
  295. */
  296. for_each_bank(i, mi) {
  297. struct membank *bank = &mi->bank[i];
  298. bank_start = bank_pfn_start(bank);
  299. /*
  300. * If we had a previous bank, and there is a space
  301. * between the current bank and the previous, free it.
  302. */
  303. if (prev_bank_end && prev_bank_end < bank_start)
  304. free_memmap(prev_bank_end, bank_start);
  305. /*
  306. * Align up here since the VM subsystem insists that the
  307. * memmap entries are valid from the bank end aligned to
  308. * MAX_ORDER_NR_PAGES.
  309. */
  310. prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
  311. }
  312. }
  313. /*
  314. * mem_init() marks the free areas in the mem_map and tells us how much
  315. * memory is free. This is done after various parts of the system have
  316. * claimed their memory after the kernel image.
  317. */
  318. void __init mem_init(void)
  319. {
  320. unsigned long reserved_pages, free_pages;
  321. struct memblock_region *reg;
  322. int i;
  323. max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
  324. free_unused_memmap(&meminfo);
  325. /* this will put all unused low memory onto the freelists */
  326. totalram_pages += free_all_bootmem();
  327. reserved_pages = free_pages = 0;
  328. for_each_bank(i, &meminfo) {
  329. struct membank *bank = &meminfo.bank[i];
  330. unsigned int pfn1, pfn2;
  331. struct page *page, *end;
  332. pfn1 = bank_pfn_start(bank);
  333. pfn2 = bank_pfn_end(bank);
  334. page = pfn_to_page(pfn1);
  335. end = pfn_to_page(pfn2 - 1) + 1;
  336. do {
  337. if (PageReserved(page))
  338. reserved_pages++;
  339. else if (!page_count(page))
  340. free_pages++;
  341. page++;
  342. } while (page < end);
  343. }
  344. /*
  345. * Since our memory may not be contiguous, calculate the
  346. * real number of pages we have in this system
  347. */
  348. printk(KERN_INFO "Memory:");
  349. num_physpages = 0;
  350. for_each_memblock(memory, reg) {
  351. unsigned long pages = memblock_region_memory_end_pfn(reg) -
  352. memblock_region_memory_base_pfn(reg);
  353. num_physpages += pages;
  354. printk(" %ldMB", pages >> (20 - PAGE_SHIFT));
  355. }
  356. printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  357. printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
  358. nr_free_pages() << (PAGE_SHIFT-10),
  359. free_pages << (PAGE_SHIFT-10),
  360. reserved_pages << (PAGE_SHIFT-10),
  361. totalhigh_pages << (PAGE_SHIFT-10));
  362. printk(KERN_NOTICE "Virtual kernel memory layout:\n"
  363. " vector : 0x%08lx - 0x%08lx (%4ld kB)\n"
  364. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  365. " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n"
  366. " modules : 0x%08lx - 0x%08lx (%4ld MB)\n"
  367. " .init : 0x%p" " - 0x%p" " (%4d kB)\n"
  368. " .text : 0x%p" " - 0x%p" " (%4d kB)\n"
  369. " .data : 0x%p" " - 0x%p" " (%4d kB)\n",
  370. VECTORS_BASE, VECTORS_BASE + PAGE_SIZE,
  371. DIV_ROUND_UP(PAGE_SIZE, SZ_1K),
  372. VMALLOC_START, VMALLOC_END,
  373. DIV_ROUND_UP((VMALLOC_END - VMALLOC_START), SZ_1M),
  374. PAGE_OFFSET, (unsigned long)high_memory,
  375. DIV_ROUND_UP(((unsigned long)high_memory - PAGE_OFFSET), SZ_1M),
  376. MODULES_VADDR, MODULES_END,
  377. DIV_ROUND_UP((MODULES_END - MODULES_VADDR), SZ_1M),
  378. __init_begin, __init_end,
  379. DIV_ROUND_UP((__init_end - __init_begin), SZ_1K),
  380. _stext, _etext,
  381. DIV_ROUND_UP((_etext - _stext), SZ_1K),
  382. _sdata, _edata,
  383. DIV_ROUND_UP((_edata - _sdata), SZ_1K));
  384. BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
  385. BUG_ON(TASK_SIZE > MODULES_VADDR);
  386. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  387. /*
  388. * On a machine this small we won't get
  389. * anywhere without overcommit, so turn
  390. * it on by default.
  391. */
  392. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  393. }
  394. }
  395. void free_initmem(void)
  396. {
  397. free_initmem_default(0);
  398. }
  399. #ifdef CONFIG_BLK_DEV_INITRD
  400. static int keep_initrd;
  401. void free_initrd_mem(unsigned long start, unsigned long end)
  402. {
  403. if (!keep_initrd)
  404. free_reserved_area(start, end, 0, "initrd");
  405. }
  406. static int __init keepinitrd_setup(char *__unused)
  407. {
  408. keep_initrd = 1;
  409. return 1;
  410. }
  411. __setup("keepinitrd", keepinitrd_setup);
  412. #endif