init.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/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. #endif /* CONFIG_HIGHMEM */
  49. void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
  50. {
  51. #ifdef CONFIG_HIGHMEM
  52. max_zone_pfns[ZONE_DMA] = max_low_pfn;
  53. max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
  54. #else
  55. max_zone_pfns[ZONE_DMA] = max_pfn;
  56. #endif
  57. }
  58. /*
  59. * paging_init() sets up the page tables - in fact we've already done this.
  60. */
  61. static void __init paging_init(void)
  62. {
  63. int idx;
  64. /* Setup fixmaps */
  65. for (idx = 0; idx < __end_of_fixed_addresses; idx++)
  66. clear_fixmap(idx);
  67. #ifdef CONFIG_HIGHMEM
  68. highmem_init();
  69. #endif
  70. }
  71. void __init setup_memory(void)
  72. {
  73. /*
  74. * Kernel:
  75. * start: base phys address of kernel - page align
  76. * end: base phys address of kernel - page align
  77. *
  78. * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
  79. * max_low_pfn
  80. */
  81. /* memory start is from the kernel end (aligned) to higher addr */
  82. min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
  83. max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
  84. max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
  85. pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
  86. pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
  87. pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn);
  88. paging_init();
  89. }
  90. void __init mem_init(void)
  91. {
  92. mem_init_done = 1;
  93. }
  94. int page_is_ram(unsigned long pfn)
  95. {
  96. return pfn < max_low_pfn;
  97. }
  98. /*
  99. * Check for command-line options that affect what MMU_init will do.
  100. */
  101. static void __init mm_cmdline_setup(void)
  102. {
  103. unsigned long maxmem = 0;
  104. char *p = cmd_line;
  105. /* Look for mem= option on command line */
  106. p = strstr(cmd_line, "mem=");
  107. if (p) {
  108. p += 4;
  109. maxmem = memparse(p, &p);
  110. if (maxmem && memory_size > maxmem) {
  111. memory_size = maxmem;
  112. memblock.memory.regions[0].size = memory_size;
  113. }
  114. }
  115. }
  116. /*
  117. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  118. */
  119. static void __init mmu_init_hw(void)
  120. {
  121. /*
  122. * The Zone Protection Register (ZPR) defines how protection will
  123. * be applied to every page which is a member of a given zone. At
  124. * present, we utilize only two of the zones.
  125. * The zone index bits (of ZSEL) in the PTE are used for software
  126. * indicators, except the LSB. For user access, zone 1 is used,
  127. * for kernel access, zone 0 is used. We set all but zone 1
  128. * to zero, allowing only kernel access as indicated in the PTE.
  129. * For zone 1, we set a 01 binary (a value of 10 will not work)
  130. * to allow user access as indicated in the PTE. This also allows
  131. * kernel access as indicated in the PTE.
  132. */
  133. __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
  134. "mts rzpr, r11;"
  135. : : : "r11");
  136. }
  137. /*
  138. * MMU_init sets up the basic memory mappings for the kernel,
  139. * including both RAM and possibly some I/O regions,
  140. * and sets up the page tables and the MMU hardware ready to go.
  141. */
  142. /* called from head.S */
  143. asmlinkage void __init mmu_init(void)
  144. {
  145. unsigned int kstart, ksize;
  146. if ((u32) memblock.memory.regions[0].size < 0x400000) {
  147. pr_emerg("Memory must be greater than 4MB\n");
  148. machine_restart(NULL);
  149. }
  150. if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
  151. pr_emerg("Kernel size is greater than memory node\n");
  152. machine_restart(NULL);
  153. }
  154. /* Find main memory where the kernel is */
  155. memory_start = (u32) memblock.memory.regions[0].base;
  156. lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
  157. if (lowmem_size > CONFIG_LOWMEM_SIZE) {
  158. lowmem_size = CONFIG_LOWMEM_SIZE;
  159. #ifndef CONFIG_HIGHMEM
  160. memory_size = lowmem_size;
  161. #endif
  162. }
  163. mm_cmdline_setup(); /* FIXME parse args from command line - not used */
  164. /*
  165. * Map out the kernel text/data/bss from the available physical
  166. * memory.
  167. */
  168. kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
  169. /* kernel size */
  170. ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
  171. memblock_reserve(kstart, ksize);
  172. #if defined(CONFIG_BLK_DEV_INITRD)
  173. /* Remove the init RAM disk from the available memory. */
  174. if (initrd_start) {
  175. unsigned long size;
  176. size = initrd_end - initrd_start;
  177. memblock_reserve(__virt_to_phys(initrd_start), size);
  178. }
  179. #endif /* CONFIG_BLK_DEV_INITRD */
  180. /* Initialize the MMU hardware */
  181. mmu_init_hw();
  182. /* Map in all of RAM starting at CONFIG_KERNEL_START */
  183. mapin_ram();
  184. /* Extend vmalloc and ioremap area as big as possible */
  185. #ifdef CONFIG_HIGHMEM
  186. ioremap_base = ioremap_bot = PKMAP_BASE;
  187. #else
  188. ioremap_base = ioremap_bot = FIXADDR_START;
  189. #endif
  190. /* Initialize the context management stuff */
  191. mmu_context_init();
  192. /* Shortly after that, the entire linear mapping will be available */
  193. /* This will also cause that unflatten device tree will be allocated
  194. * inside 768MB limit */
  195. memblock_set_current_limit(memory_start + lowmem_size - 1);
  196. parse_early_param();
  197. early_init_fdt_scan_reserved_mem();
  198. /* CMA initialization */
  199. dma_contiguous_reserve(memory_start + lowmem_size - 1);
  200. memblock_dump_all();
  201. }
  202. static const pgprot_t protection_map[16] = {
  203. [VM_NONE] = PAGE_NONE,
  204. [VM_READ] = PAGE_READONLY_X,
  205. [VM_WRITE] = PAGE_COPY,
  206. [VM_WRITE | VM_READ] = PAGE_COPY_X,
  207. [VM_EXEC] = PAGE_READONLY,
  208. [VM_EXEC | VM_READ] = PAGE_READONLY_X,
  209. [VM_EXEC | VM_WRITE] = PAGE_COPY,
  210. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_X,
  211. [VM_SHARED] = PAGE_NONE,
  212. [VM_SHARED | VM_READ] = PAGE_READONLY_X,
  213. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  214. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_X,
  215. [VM_SHARED | VM_EXEC] = PAGE_READONLY,
  216. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_X,
  217. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
  218. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_X
  219. };
  220. DECLARE_VM_GET_PAGE_PROT