init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2006 Atmark Techno, Inc.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. */
  9. #include <linux/bootmem.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/memblock.h>
  13. #include <linux/mm.h> /* mem_init */
  14. #include <linux/initrd.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/pfn.h>
  17. #include <linux/slab.h>
  18. #include <linux/swap.h>
  19. #include <linux/export.h>
  20. #include <asm/page.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/sections.h>
  24. #include <asm/tlb.h>
  25. #include <asm/fixmap.h>
  26. /* Use for MMU and noMMU because of PCI generic code */
  27. int mem_init_done;
  28. #ifndef CONFIG_MMU
  29. unsigned int __page_offset;
  30. EXPORT_SYMBOL(__page_offset);
  31. #else
  32. static int init_bootmem_done;
  33. #endif /* CONFIG_MMU */
  34. char *klimit = _end;
  35. /*
  36. * Initialize the bootmem system and give it all the memory we
  37. * have available.
  38. */
  39. unsigned long memory_start;
  40. EXPORT_SYMBOL(memory_start);
  41. unsigned long memory_size;
  42. EXPORT_SYMBOL(memory_size);
  43. unsigned long lowmem_size;
  44. #ifdef CONFIG_HIGHMEM
  45. pte_t *kmap_pte;
  46. EXPORT_SYMBOL(kmap_pte);
  47. pgprot_t kmap_prot;
  48. EXPORT_SYMBOL(kmap_prot);
  49. static inline pte_t *virt_to_kpte(unsigned long vaddr)
  50. {
  51. return pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr),
  52. vaddr), vaddr);
  53. }
  54. static void __init highmem_init(void)
  55. {
  56. pr_debug("%x\n", (u32)PKMAP_BASE);
  57. map_page(PKMAP_BASE, 0, 0); /* XXX gross */
  58. pkmap_page_table = virt_to_kpte(PKMAP_BASE);
  59. kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
  60. kmap_prot = PAGE_KERNEL;
  61. }
  62. static void highmem_setup(void)
  63. {
  64. unsigned long pfn;
  65. for (pfn = max_low_pfn; pfn < max_pfn; ++pfn) {
  66. struct page *page = pfn_to_page(pfn);
  67. /* FIXME not sure about */
  68. if (!memblock_is_reserved(pfn << PAGE_SHIFT))
  69. free_highmem_page(page);
  70. }
  71. }
  72. #endif /* CONFIG_HIGHMEM */
  73. /*
  74. * paging_init() sets up the page tables - in fact we've already done this.
  75. */
  76. static void __init paging_init(void)
  77. {
  78. unsigned long zones_size[MAX_NR_ZONES];
  79. #ifdef CONFIG_MMU
  80. int idx;
  81. /* Setup fixmaps */
  82. for (idx = 0; idx < __end_of_fixed_addresses; idx++)
  83. clear_fixmap(idx);
  84. #endif
  85. /* Clean every zones */
  86. memset(zones_size, 0, sizeof(zones_size));
  87. #ifdef CONFIG_HIGHMEM
  88. highmem_init();
  89. zones_size[ZONE_DMA] = max_low_pfn;
  90. zones_size[ZONE_HIGHMEM] = max_pfn;
  91. #else
  92. zones_size[ZONE_DMA] = max_pfn;
  93. #endif
  94. /* We don't have holes in memory map */
  95. free_area_init_nodes(zones_size);
  96. }
  97. void __init setup_memory(void)
  98. {
  99. unsigned long map_size;
  100. struct memblock_region *reg;
  101. #ifndef CONFIG_MMU
  102. u32 kernel_align_start, kernel_align_size;
  103. /* Find main memory where is the kernel */
  104. for_each_memblock(memory, reg) {
  105. memory_start = (u32)reg->base;
  106. lowmem_size = reg->size;
  107. if ((memory_start <= (u32)_text) &&
  108. ((u32)_text <= (memory_start + lowmem_size - 1))) {
  109. memory_size = lowmem_size;
  110. PAGE_OFFSET = memory_start;
  111. pr_info("%s: Main mem: 0x%x, size 0x%08x\n",
  112. __func__, (u32) memory_start,
  113. (u32) memory_size);
  114. break;
  115. }
  116. }
  117. if (!memory_start || !memory_size) {
  118. panic("%s: Missing memory setting 0x%08x, size=0x%08x\n",
  119. __func__, (u32) memory_start, (u32) memory_size);
  120. }
  121. /* reservation of region where is the kernel */
  122. kernel_align_start = PAGE_DOWN((u32)_text);
  123. /* ALIGN can be remove because _end in vmlinux.lds.S is align */
  124. kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start;
  125. pr_info("%s: kernel addr:0x%08x-0x%08x size=0x%08x\n",
  126. __func__, kernel_align_start, kernel_align_start
  127. + kernel_align_size, kernel_align_size);
  128. memblock_reserve(kernel_align_start, kernel_align_size);
  129. #endif
  130. /*
  131. * Kernel:
  132. * start: base phys address of kernel - page align
  133. * end: base phys address of kernel - page align
  134. *
  135. * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
  136. * max_low_pfn
  137. * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
  138. */
  139. /* memory start is from the kernel end (aligned) to higher addr */
  140. min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
  141. /* RAM is assumed contiguous */
  142. max_mapnr = memory_size >> PAGE_SHIFT;
  143. max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
  144. max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
  145. pr_info("%s: max_mapnr: %#lx\n", __func__, max_mapnr);
  146. pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
  147. pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
  148. pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn);
  149. /*
  150. * Find an area to use for the bootmem bitmap.
  151. * We look for the first area which is at least
  152. * 128kB in length (128kB is enough for a bitmap
  153. * for 4GB of memory, using 4kB pages), plus 1 page
  154. * (in case the address isn't page-aligned).
  155. */
  156. map_size = init_bootmem_node(NODE_DATA(0),
  157. PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn);
  158. memblock_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size);
  159. /* Add active regions with valid PFNs */
  160. for_each_memblock(memory, reg) {
  161. unsigned long start_pfn, end_pfn;
  162. start_pfn = memblock_region_memory_base_pfn(reg);
  163. end_pfn = memblock_region_memory_end_pfn(reg);
  164. memblock_set_node(start_pfn << PAGE_SHIFT,
  165. (end_pfn - start_pfn) << PAGE_SHIFT,
  166. &memblock.memory, 0);
  167. }
  168. /* free bootmem is whole main memory */
  169. free_bootmem_with_active_regions(0, max_low_pfn);
  170. /* reserve allocate blocks */
  171. for_each_memblock(reserved, reg) {
  172. unsigned long top = reg->base + reg->size - 1;
  173. pr_debug("reserved - 0x%08x-0x%08x, %lx, %lx\n",
  174. (u32) reg->base, (u32) reg->size, top,
  175. memory_start + lowmem_size - 1);
  176. if (top <= (memory_start + lowmem_size - 1)) {
  177. reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
  178. } else if (reg->base < (memory_start + lowmem_size - 1)) {
  179. unsigned long trunc_size = memory_start + lowmem_size -
  180. reg->base;
  181. reserve_bootmem(reg->base, trunc_size, BOOTMEM_DEFAULT);
  182. }
  183. }
  184. /* XXX need to clip this if using highmem? */
  185. sparse_memory_present_with_active_regions(0);
  186. #ifdef CONFIG_MMU
  187. init_bootmem_done = 1;
  188. #endif
  189. paging_init();
  190. }
  191. #ifdef CONFIG_BLK_DEV_INITRD
  192. void free_initrd_mem(unsigned long start, unsigned long end)
  193. {
  194. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  195. }
  196. #endif
  197. void free_initmem(void)
  198. {
  199. free_initmem_default(-1);
  200. }
  201. void __init mem_init(void)
  202. {
  203. high_memory = (void *)__va(memory_start + lowmem_size - 1);
  204. /* this will put all memory onto the freelists */
  205. free_all_bootmem();
  206. #ifdef CONFIG_HIGHMEM
  207. highmem_setup();
  208. #endif
  209. mem_init_print_info(NULL);
  210. #ifdef CONFIG_MMU
  211. pr_info("Kernel virtual memory layout:\n");
  212. pr_info(" * 0x%08lx..0x%08lx : fixmap\n", FIXADDR_START, FIXADDR_TOP);
  213. #ifdef CONFIG_HIGHMEM
  214. pr_info(" * 0x%08lx..0x%08lx : highmem PTEs\n",
  215. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
  216. #endif /* CONFIG_HIGHMEM */
  217. pr_info(" * 0x%08lx..0x%08lx : early ioremap\n",
  218. ioremap_bot, ioremap_base);
  219. pr_info(" * 0x%08lx..0x%08lx : vmalloc & ioremap\n",
  220. (unsigned long)VMALLOC_START, VMALLOC_END);
  221. #endif
  222. mem_init_done = 1;
  223. }
  224. #ifndef CONFIG_MMU
  225. int page_is_ram(unsigned long pfn)
  226. {
  227. return __range_ok(pfn, 0);
  228. }
  229. #else
  230. int page_is_ram(unsigned long pfn)
  231. {
  232. return pfn < max_low_pfn;
  233. }
  234. /*
  235. * Check for command-line options that affect what MMU_init will do.
  236. */
  237. static void mm_cmdline_setup(void)
  238. {
  239. unsigned long maxmem = 0;
  240. char *p = cmd_line;
  241. /* Look for mem= option on command line */
  242. p = strstr(cmd_line, "mem=");
  243. if (p) {
  244. p += 4;
  245. maxmem = memparse(p, &p);
  246. if (maxmem && memory_size > maxmem) {
  247. memory_size = maxmem;
  248. memblock.memory.regions[0].size = memory_size;
  249. }
  250. }
  251. }
  252. /*
  253. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  254. */
  255. static void __init mmu_init_hw(void)
  256. {
  257. /*
  258. * The Zone Protection Register (ZPR) defines how protection will
  259. * be applied to every page which is a member of a given zone. At
  260. * present, we utilize only two of the zones.
  261. * The zone index bits (of ZSEL) in the PTE are used for software
  262. * indicators, except the LSB. For user access, zone 1 is used,
  263. * for kernel access, zone 0 is used. We set all but zone 1
  264. * to zero, allowing only kernel access as indicated in the PTE.
  265. * For zone 1, we set a 01 binary (a value of 10 will not work)
  266. * to allow user access as indicated in the PTE. This also allows
  267. * kernel access as indicated in the PTE.
  268. */
  269. __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
  270. "mts rzpr, r11;"
  271. : : : "r11");
  272. }
  273. /*
  274. * MMU_init sets up the basic memory mappings for the kernel,
  275. * including both RAM and possibly some I/O regions,
  276. * and sets up the page tables and the MMU hardware ready to go.
  277. */
  278. /* called from head.S */
  279. asmlinkage void __init mmu_init(void)
  280. {
  281. unsigned int kstart, ksize;
  282. if (!memblock.reserved.cnt) {
  283. pr_emerg("Error memory count\n");
  284. machine_restart(NULL);
  285. }
  286. if ((u32) memblock.memory.regions[0].size < 0x400000) {
  287. pr_emerg("Memory must be greater than 4MB\n");
  288. machine_restart(NULL);
  289. }
  290. if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
  291. pr_emerg("Kernel size is greater than memory node\n");
  292. machine_restart(NULL);
  293. }
  294. /* Find main memory where the kernel is */
  295. memory_start = (u32) memblock.memory.regions[0].base;
  296. lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
  297. if (lowmem_size > CONFIG_LOWMEM_SIZE) {
  298. lowmem_size = CONFIG_LOWMEM_SIZE;
  299. #ifndef CONFIG_HIGHMEM
  300. memory_size = lowmem_size;
  301. #endif
  302. }
  303. mm_cmdline_setup(); /* FIXME parse args from command line - not used */
  304. /*
  305. * Map out the kernel text/data/bss from the available physical
  306. * memory.
  307. */
  308. kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
  309. /* kernel size */
  310. ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
  311. memblock_reserve(kstart, ksize);
  312. #if defined(CONFIG_BLK_DEV_INITRD)
  313. /* Remove the init RAM disk from the available memory. */
  314. if (initrd_start) {
  315. unsigned long size;
  316. size = initrd_end - initrd_start;
  317. memblock_reserve(__virt_to_phys(initrd_start), size);
  318. }
  319. #endif /* CONFIG_BLK_DEV_INITRD */
  320. /* Initialize the MMU hardware */
  321. mmu_init_hw();
  322. /* Map in all of RAM starting at CONFIG_KERNEL_START */
  323. mapin_ram();
  324. /* Extend vmalloc and ioremap area as big as possible */
  325. #ifdef CONFIG_HIGHMEM
  326. ioremap_base = ioremap_bot = PKMAP_BASE;
  327. #else
  328. ioremap_base = ioremap_bot = FIXADDR_START;
  329. #endif
  330. /* Initialize the context management stuff */
  331. mmu_context_init();
  332. /* Shortly after that, the entire linear mapping will be available */
  333. /* This will also cause that unflatten device tree will be allocated
  334. * inside 768MB limit */
  335. memblock_set_current_limit(memory_start + lowmem_size - 1);
  336. }
  337. /* This is only called until mem_init is done. */
  338. void __init *early_get_page(void)
  339. {
  340. void *p;
  341. if (init_bootmem_done) {
  342. p = alloc_bootmem_pages(PAGE_SIZE);
  343. } else {
  344. /*
  345. * Mem start + kernel_tlb -> here is limit
  346. * because of mem mapping from head.S
  347. */
  348. p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE,
  349. memory_start + kernel_tlb));
  350. }
  351. return p;
  352. }
  353. #endif /* CONFIG_MMU */
  354. void * __ref alloc_maybe_bootmem(size_t size, gfp_t mask)
  355. {
  356. if (mem_init_done)
  357. return kmalloc(size, mask);
  358. else
  359. return alloc_bootmem(size);
  360. }
  361. void * __ref zalloc_maybe_bootmem(size_t size, gfp_t mask)
  362. {
  363. void *p;
  364. if (mem_init_done)
  365. p = kzalloc(size, mask);
  366. else {
  367. p = alloc_bootmem(size);
  368. if (p)
  369. memset(p, 0, size);
  370. }
  371. return p;
  372. }