init.c 9.8 KB

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