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