init.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2007-2008 Michal Simek <[email protected]>
  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/dma-map-ops.h>
  10. #include <linux/memblock.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h> /* mem_init */
  14. #include <linux/initrd.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/pfn.h>
  18. #include <linux/slab.h>
  19. #include <linux/swap.h>
  20. #include <linux/export.h>
  21. #include <asm/page.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/sections.h>
  25. #include <asm/tlb.h>
  26. #include <asm/fixmap.h>
  27. /* Use for MMU and noMMU because of PCI generic code */
  28. int mem_init_done;
  29. char *klimit = _end;
  30. /*
  31. * Initialize the bootmem system and give it all the memory we
  32. * have available.
  33. */
  34. unsigned long memory_start;
  35. EXPORT_SYMBOL(memory_start);
  36. unsigned long memory_size;
  37. EXPORT_SYMBOL(memory_size);
  38. unsigned long lowmem_size;
  39. EXPORT_SYMBOL(min_low_pfn);
  40. EXPORT_SYMBOL(max_low_pfn);
  41. #ifdef CONFIG_HIGHMEM
  42. static void __init highmem_init(void)
  43. {
  44. pr_debug("%x\n", (u32)PKMAP_BASE);
  45. map_page(PKMAP_BASE, 0, 0); /* XXX gross */
  46. pkmap_page_table = virt_to_kpte(PKMAP_BASE);
  47. }
  48. static void __meminit highmem_setup(void)
  49. {
  50. unsigned long pfn;
  51. for (pfn = max_low_pfn; pfn < max_pfn; ++pfn) {
  52. struct page *page = pfn_to_page(pfn);
  53. /* FIXME not sure about */
  54. if (!memblock_is_reserved(pfn << PAGE_SHIFT))
  55. free_highmem_page(page);
  56. }
  57. }
  58. #endif /* CONFIG_HIGHMEM */
  59. /*
  60. * paging_init() sets up the page tables - in fact we've already done this.
  61. */
  62. static void __init paging_init(void)
  63. {
  64. unsigned long zones_size[MAX_NR_ZONES];
  65. int idx;
  66. /* Setup fixmaps */
  67. for (idx = 0; idx < __end_of_fixed_addresses; idx++)
  68. clear_fixmap(idx);
  69. /* Clean every zones */
  70. memset(zones_size, 0, sizeof(zones_size));
  71. #ifdef CONFIG_HIGHMEM
  72. highmem_init();
  73. zones_size[ZONE_DMA] = max_low_pfn;
  74. zones_size[ZONE_HIGHMEM] = max_pfn;
  75. #else
  76. zones_size[ZONE_DMA] = max_pfn;
  77. #endif
  78. /* We don't have holes in memory map */
  79. free_area_init(zones_size);
  80. }
  81. void __init setup_memory(void)
  82. {
  83. /*
  84. * Kernel:
  85. * start: base phys address of kernel - page align
  86. * end: base phys address of kernel - page align
  87. *
  88. * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
  89. * max_low_pfn
  90. * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
  91. */
  92. /* memory start is from the kernel end (aligned) to higher addr */
  93. min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
  94. /* RAM is assumed contiguous */
  95. max_mapnr = memory_size >> PAGE_SHIFT;
  96. max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
  97. max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
  98. pr_info("%s: max_mapnr: %#lx\n", __func__, max_mapnr);
  99. pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
  100. pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
  101. pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn);
  102. paging_init();
  103. }
  104. void __init mem_init(void)
  105. {
  106. high_memory = (void *)__va(memory_start + lowmem_size - 1);
  107. /* this will put all memory onto the freelists */
  108. memblock_free_all();
  109. #ifdef CONFIG_HIGHMEM
  110. highmem_setup();
  111. #endif
  112. mem_init_done = 1;
  113. }
  114. int page_is_ram(unsigned long pfn)
  115. {
  116. return pfn < max_low_pfn;
  117. }
  118. /*
  119. * Check for command-line options that affect what MMU_init will do.
  120. */
  121. static void mm_cmdline_setup(void)
  122. {
  123. unsigned long maxmem = 0;
  124. char *p = cmd_line;
  125. /* Look for mem= option on command line */
  126. p = strstr(cmd_line, "mem=");
  127. if (p) {
  128. p += 4;
  129. maxmem = memparse(p, &p);
  130. if (maxmem && memory_size > maxmem) {
  131. memory_size = maxmem;
  132. memblock.memory.regions[0].size = memory_size;
  133. }
  134. }
  135. }
  136. /*
  137. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  138. */
  139. static void __init mmu_init_hw(void)
  140. {
  141. /*
  142. * The Zone Protection Register (ZPR) defines how protection will
  143. * be applied to every page which is a member of a given zone. At
  144. * present, we utilize only two of the zones.
  145. * The zone index bits (of ZSEL) in the PTE are used for software
  146. * indicators, except the LSB. For user access, zone 1 is used,
  147. * for kernel access, zone 0 is used. We set all but zone 1
  148. * to zero, allowing only kernel access as indicated in the PTE.
  149. * For zone 1, we set a 01 binary (a value of 10 will not work)
  150. * to allow user access as indicated in the PTE. This also allows
  151. * kernel access as indicated in the PTE.
  152. */
  153. __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
  154. "mts rzpr, r11;"
  155. : : : "r11");
  156. }
  157. /*
  158. * MMU_init sets up the basic memory mappings for the kernel,
  159. * including both RAM and possibly some I/O regions,
  160. * and sets up the page tables and the MMU hardware ready to go.
  161. */
  162. /* called from head.S */
  163. asmlinkage void __init mmu_init(void)
  164. {
  165. unsigned int kstart, ksize;
  166. if (!memblock.reserved.cnt) {
  167. pr_emerg("Error memory count\n");
  168. machine_restart(NULL);
  169. }
  170. if ((u32) memblock.memory.regions[0].size < 0x400000) {
  171. pr_emerg("Memory must be greater than 4MB\n");
  172. machine_restart(NULL);
  173. }
  174. if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
  175. pr_emerg("Kernel size is greater than memory node\n");
  176. machine_restart(NULL);
  177. }
  178. /* Find main memory where the kernel is */
  179. memory_start = (u32) memblock.memory.regions[0].base;
  180. lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
  181. if (lowmem_size > CONFIG_LOWMEM_SIZE) {
  182. lowmem_size = CONFIG_LOWMEM_SIZE;
  183. #ifndef CONFIG_HIGHMEM
  184. memory_size = lowmem_size;
  185. #endif
  186. }
  187. mm_cmdline_setup(); /* FIXME parse args from command line - not used */
  188. /*
  189. * Map out the kernel text/data/bss from the available physical
  190. * memory.
  191. */
  192. kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
  193. /* kernel size */
  194. ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
  195. memblock_reserve(kstart, ksize);
  196. #if defined(CONFIG_BLK_DEV_INITRD)
  197. /* Remove the init RAM disk from the available memory. */
  198. if (initrd_start) {
  199. unsigned long size;
  200. size = initrd_end - initrd_start;
  201. memblock_reserve(__virt_to_phys(initrd_start), size);
  202. }
  203. #endif /* CONFIG_BLK_DEV_INITRD */
  204. /* Initialize the MMU hardware */
  205. mmu_init_hw();
  206. /* Map in all of RAM starting at CONFIG_KERNEL_START */
  207. mapin_ram();
  208. /* Extend vmalloc and ioremap area as big as possible */
  209. #ifdef CONFIG_HIGHMEM
  210. ioremap_base = ioremap_bot = PKMAP_BASE;
  211. #else
  212. ioremap_base = ioremap_bot = FIXADDR_START;
  213. #endif
  214. /* Initialize the context management stuff */
  215. mmu_context_init();
  216. /* Shortly after that, the entire linear mapping will be available */
  217. /* This will also cause that unflatten device tree will be allocated
  218. * inside 768MB limit */
  219. memblock_set_current_limit(memory_start + lowmem_size - 1);
  220. parse_early_param();
  221. early_init_fdt_scan_reserved_mem();
  222. /* CMA initialization */
  223. dma_contiguous_reserve(memory_start + lowmem_size - 1);
  224. memblock_dump_all();
  225. }
  226. void * __ref zalloc_maybe_bootmem(size_t size, gfp_t mask)
  227. {
  228. void *p;
  229. if (mem_init_done) {
  230. p = kzalloc(size, mask);
  231. } else {
  232. p = memblock_alloc(size, SMP_CACHE_BYTES);
  233. if (!p)
  234. panic("%s: Failed to allocate %zu bytes\n",
  235. __func__, size);
  236. }
  237. return p;
  238. }
  239. static const pgprot_t protection_map[16] = {
  240. [VM_NONE] = PAGE_NONE,
  241. [VM_READ] = PAGE_READONLY_X,
  242. [VM_WRITE] = PAGE_COPY,
  243. [VM_WRITE | VM_READ] = PAGE_COPY_X,
  244. [VM_EXEC] = PAGE_READONLY,
  245. [VM_EXEC | VM_READ] = PAGE_READONLY_X,
  246. [VM_EXEC | VM_WRITE] = PAGE_COPY,
  247. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_X,
  248. [VM_SHARED] = PAGE_NONE,
  249. [VM_SHARED | VM_READ] = PAGE_READONLY_X,
  250. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  251. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_X,
  252. [VM_SHARED | VM_EXEC] = PAGE_READONLY,
  253. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_X,
  254. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
  255. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_X
  256. };
  257. DECLARE_VM_GET_PAGE_PROT