init.c 11 KB

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