init.c 9.8 KB

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