init.c 13 KB

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