init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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, 0);
  166. }
  167. /* free bootmem is whole main memory */
  168. free_bootmem_with_active_regions(0, max_low_pfn);
  169. /* reserve allocate blocks */
  170. for_each_memblock(reserved, reg) {
  171. unsigned long top = reg->base + reg->size - 1;
  172. pr_debug("reserved - 0x%08x-0x%08x, %lx, %lx\n",
  173. (u32) reg->base, (u32) reg->size, top,
  174. memory_start + lowmem_size - 1);
  175. if (top <= (memory_start + lowmem_size - 1)) {
  176. reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
  177. } else if (reg->base < (memory_start + lowmem_size - 1)) {
  178. unsigned long trunc_size = memory_start + lowmem_size -
  179. reg->base;
  180. reserve_bootmem(reg->base, trunc_size, BOOTMEM_DEFAULT);
  181. }
  182. }
  183. /* XXX need to clip this if using highmem? */
  184. sparse_memory_present_with_active_regions(0);
  185. #ifdef CONFIG_MMU
  186. init_bootmem_done = 1;
  187. #endif
  188. paging_init();
  189. }
  190. #ifdef CONFIG_BLK_DEV_INITRD
  191. void free_initrd_mem(unsigned long start, unsigned long end)
  192. {
  193. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  194. }
  195. #endif
  196. void free_initmem(void)
  197. {
  198. free_initmem_default(-1);
  199. }
  200. void __init mem_init(void)
  201. {
  202. high_memory = (void *)__va(memory_start + lowmem_size - 1);
  203. /* this will put all memory onto the freelists */
  204. free_all_bootmem();
  205. #ifdef CONFIG_HIGHMEM
  206. highmem_setup();
  207. #endif
  208. mem_init_print_info(NULL);
  209. #ifdef CONFIG_MMU
  210. pr_info("Kernel virtual memory layout:\n");
  211. pr_info(" * 0x%08lx..0x%08lx : fixmap\n", FIXADDR_START, FIXADDR_TOP);
  212. #ifdef CONFIG_HIGHMEM
  213. pr_info(" * 0x%08lx..0x%08lx : highmem PTEs\n",
  214. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
  215. #endif /* CONFIG_HIGHMEM */
  216. pr_info(" * 0x%08lx..0x%08lx : early ioremap\n",
  217. ioremap_bot, ioremap_base);
  218. pr_info(" * 0x%08lx..0x%08lx : vmalloc & ioremap\n",
  219. (unsigned long)VMALLOC_START, VMALLOC_END);
  220. #endif
  221. mem_init_done = 1;
  222. }
  223. #ifndef CONFIG_MMU
  224. int page_is_ram(unsigned long pfn)
  225. {
  226. return __range_ok(pfn, 0);
  227. }
  228. #else
  229. int page_is_ram(unsigned long pfn)
  230. {
  231. return pfn < max_low_pfn;
  232. }
  233. /*
  234. * Check for command-line options that affect what MMU_init will do.
  235. */
  236. static void mm_cmdline_setup(void)
  237. {
  238. unsigned long maxmem = 0;
  239. char *p = cmd_line;
  240. /* Look for mem= option on command line */
  241. p = strstr(cmd_line, "mem=");
  242. if (p) {
  243. p += 4;
  244. maxmem = memparse(p, &p);
  245. if (maxmem && memory_size > maxmem) {
  246. memory_size = maxmem;
  247. memblock.memory.regions[0].size = memory_size;
  248. }
  249. }
  250. }
  251. /*
  252. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  253. */
  254. static void __init mmu_init_hw(void)
  255. {
  256. /*
  257. * The Zone Protection Register (ZPR) defines how protection will
  258. * be applied to every page which is a member of a given zone. At
  259. * present, we utilize only two of the zones.
  260. * The zone index bits (of ZSEL) in the PTE are used for software
  261. * indicators, except the LSB. For user access, zone 1 is used,
  262. * for kernel access, zone 0 is used. We set all but zone 1
  263. * to zero, allowing only kernel access as indicated in the PTE.
  264. * For zone 1, we set a 01 binary (a value of 10 will not work)
  265. * to allow user access as indicated in the PTE. This also allows
  266. * kernel access as indicated in the PTE.
  267. */
  268. __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
  269. "mts rzpr, r11;"
  270. : : : "r11");
  271. }
  272. /*
  273. * MMU_init sets up the basic memory mappings for the kernel,
  274. * including both RAM and possibly some I/O regions,
  275. * and sets up the page tables and the MMU hardware ready to go.
  276. */
  277. /* called from head.S */
  278. asmlinkage void __init mmu_init(void)
  279. {
  280. unsigned int kstart, ksize;
  281. if (!memblock.reserved.cnt) {
  282. pr_emerg("Error memory count\n");
  283. machine_restart(NULL);
  284. }
  285. if ((u32) memblock.memory.regions[0].size < 0x400000) {
  286. pr_emerg("Memory must be greater than 4MB\n");
  287. machine_restart(NULL);
  288. }
  289. if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
  290. pr_emerg("Kernel size is greater than memory node\n");
  291. machine_restart(NULL);
  292. }
  293. /* Find main memory where the kernel is */
  294. memory_start = (u32) memblock.memory.regions[0].base;
  295. lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
  296. if (lowmem_size > CONFIG_LOWMEM_SIZE) {
  297. lowmem_size = CONFIG_LOWMEM_SIZE;
  298. #ifndef CONFIG_HIGHMEM
  299. memory_size = lowmem_size;
  300. #endif
  301. }
  302. mm_cmdline_setup(); /* FIXME parse args from command line - not used */
  303. /*
  304. * Map out the kernel text/data/bss from the available physical
  305. * memory.
  306. */
  307. kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
  308. /* kernel size */
  309. ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
  310. memblock_reserve(kstart, ksize);
  311. #if defined(CONFIG_BLK_DEV_INITRD)
  312. /* Remove the init RAM disk from the available memory. */
  313. if (initrd_start) {
  314. unsigned long size;
  315. size = initrd_end - initrd_start;
  316. memblock_reserve(virt_to_phys(initrd_start), size);
  317. }
  318. #endif /* CONFIG_BLK_DEV_INITRD */
  319. /* Initialize the MMU hardware */
  320. mmu_init_hw();
  321. /* Map in all of RAM starting at CONFIG_KERNEL_START */
  322. mapin_ram();
  323. /* Extend vmalloc and ioremap area as big as possible */
  324. #ifdef CONFIG_HIGHMEM
  325. ioremap_base = ioremap_bot = PKMAP_BASE;
  326. #else
  327. ioremap_base = ioremap_bot = FIXADDR_START;
  328. #endif
  329. /* Initialize the context management stuff */
  330. mmu_context_init();
  331. /* Shortly after that, the entire linear mapping will be available */
  332. /* This will also cause that unflatten device tree will be allocated
  333. * inside 768MB limit */
  334. memblock_set_current_limit(memory_start + lowmem_size - 1);
  335. }
  336. /* This is only called until mem_init is done. */
  337. void __init *early_get_page(void)
  338. {
  339. void *p;
  340. if (init_bootmem_done) {
  341. p = alloc_bootmem_pages(PAGE_SIZE);
  342. } else {
  343. /*
  344. * Mem start + kernel_tlb -> here is limit
  345. * because of mem mapping from head.S
  346. */
  347. p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE,
  348. memory_start + kernel_tlb));
  349. }
  350. return p;
  351. }
  352. #endif /* CONFIG_MMU */
  353. void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask)
  354. {
  355. if (mem_init_done)
  356. return kmalloc(size, mask);
  357. else
  358. return alloc_bootmem(size);
  359. }
  360. void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask)
  361. {
  362. void *p;
  363. if (mem_init_done)
  364. p = kzalloc(size, mask);
  365. else {
  366. p = alloc_bootmem(size);
  367. if (p)
  368. memset(p, 0, size);
  369. }
  370. return p;
  371. }