init.c 10.0 KB

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