mmu.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Based on arch/arm/mm/mmu.c
  3. *
  4. * Copyright (C) 1995-2005 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/export.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/mman.h>
  24. #include <linux/nodemask.h>
  25. #include <linux/memblock.h>
  26. #include <linux/fs.h>
  27. #include <asm/cputype.h>
  28. #include <asm/sections.h>
  29. #include <asm/setup.h>
  30. #include <asm/sizes.h>
  31. #include <asm/tlb.h>
  32. #include <asm/mmu_context.h>
  33. #include "mm.h"
  34. /*
  35. * Empty_zero_page is a special page that is used for zero-initialized data
  36. * and COW.
  37. */
  38. struct page *empty_zero_page;
  39. EXPORT_SYMBOL(empty_zero_page);
  40. pgprot_t pgprot_default;
  41. EXPORT_SYMBOL(pgprot_default);
  42. static pmdval_t prot_sect_kernel;
  43. struct cachepolicy {
  44. const char policy[16];
  45. u64 mair;
  46. u64 tcr;
  47. };
  48. static struct cachepolicy cache_policies[] __initdata = {
  49. {
  50. .policy = "uncached",
  51. .mair = 0x44, /* inner, outer non-cacheable */
  52. .tcr = TCR_IRGN_NC | TCR_ORGN_NC,
  53. }, {
  54. .policy = "writethrough",
  55. .mair = 0xaa, /* inner, outer write-through, read-allocate */
  56. .tcr = TCR_IRGN_WT | TCR_ORGN_WT,
  57. }, {
  58. .policy = "writeback",
  59. .mair = 0xee, /* inner, outer write-back, read-allocate */
  60. .tcr = TCR_IRGN_WBnWA | TCR_ORGN_WBnWA,
  61. }
  62. };
  63. /*
  64. * These are useful for identifying cache coherency problems by allowing the
  65. * cache or the cache and writebuffer to be turned off. It changes the Normal
  66. * memory caching attributes in the MAIR_EL1 register.
  67. */
  68. static int __init early_cachepolicy(char *p)
  69. {
  70. int i;
  71. u64 tmp;
  72. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  73. int len = strlen(cache_policies[i].policy);
  74. if (memcmp(p, cache_policies[i].policy, len) == 0)
  75. break;
  76. }
  77. if (i == ARRAY_SIZE(cache_policies)) {
  78. pr_err("ERROR: unknown or unsupported cache policy: %s\n", p);
  79. return 0;
  80. }
  81. flush_cache_all();
  82. /*
  83. * Modify MT_NORMAL attributes in MAIR_EL1.
  84. */
  85. asm volatile(
  86. " mrs %0, mair_el1\n"
  87. " bfi %0, %1, #%2, #8\n"
  88. " msr mair_el1, %0\n"
  89. " isb\n"
  90. : "=&r" (tmp)
  91. : "r" (cache_policies[i].mair), "i" (MT_NORMAL * 8));
  92. /*
  93. * Modify TCR PTW cacheability attributes.
  94. */
  95. asm volatile(
  96. " mrs %0, tcr_el1\n"
  97. " bic %0, %0, %2\n"
  98. " orr %0, %0, %1\n"
  99. " msr tcr_el1, %0\n"
  100. " isb\n"
  101. : "=&r" (tmp)
  102. : "r" (cache_policies[i].tcr), "r" (TCR_IRGN_MASK | TCR_ORGN_MASK));
  103. flush_cache_all();
  104. return 0;
  105. }
  106. early_param("cachepolicy", early_cachepolicy);
  107. /*
  108. * Adjust the PMD section entries according to the CPU in use.
  109. */
  110. static void __init init_mem_pgprot(void)
  111. {
  112. pteval_t default_pgprot;
  113. int i;
  114. default_pgprot = PTE_ATTRINDX(MT_NORMAL);
  115. prot_sect_kernel = PMD_TYPE_SECT | PMD_SECT_AF | PMD_ATTRINDX(MT_NORMAL);
  116. #ifdef CONFIG_SMP
  117. /*
  118. * Mark memory with the "shared" attribute for SMP systems
  119. */
  120. default_pgprot |= PTE_SHARED;
  121. prot_sect_kernel |= PMD_SECT_S;
  122. #endif
  123. for (i = 0; i < 16; i++) {
  124. unsigned long v = pgprot_val(protection_map[i]);
  125. protection_map[i] = __pgprot(v | default_pgprot);
  126. }
  127. pgprot_default = __pgprot(PTE_TYPE_PAGE | PTE_AF | default_pgprot);
  128. }
  129. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  130. unsigned long size, pgprot_t vma_prot)
  131. {
  132. if (!pfn_valid(pfn))
  133. return pgprot_noncached(vma_prot);
  134. else if (file->f_flags & O_SYNC)
  135. return pgprot_writecombine(vma_prot);
  136. return vma_prot;
  137. }
  138. EXPORT_SYMBOL(phys_mem_access_prot);
  139. static void __init *early_alloc(unsigned long sz)
  140. {
  141. void *ptr = __va(memblock_alloc(sz, sz));
  142. memset(ptr, 0, sz);
  143. return ptr;
  144. }
  145. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  146. unsigned long end, unsigned long pfn)
  147. {
  148. pte_t *pte;
  149. if (pmd_none(*pmd)) {
  150. pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
  151. __pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
  152. }
  153. BUG_ON(pmd_bad(*pmd));
  154. pte = pte_offset_kernel(pmd, addr);
  155. do {
  156. set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
  157. pfn++;
  158. } while (pte++, addr += PAGE_SIZE, addr != end);
  159. }
  160. static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
  161. unsigned long end, phys_addr_t phys)
  162. {
  163. pmd_t *pmd;
  164. unsigned long next;
  165. /*
  166. * Check for initial section mappings in the pgd/pud and remove them.
  167. */
  168. if (pud_none(*pud) || pud_bad(*pud)) {
  169. pmd = early_alloc(PTRS_PER_PMD * sizeof(pmd_t));
  170. pud_populate(&init_mm, pud, pmd);
  171. }
  172. pmd = pmd_offset(pud, addr);
  173. do {
  174. next = pmd_addr_end(addr, end);
  175. /* try section mapping first */
  176. if (((addr | next | phys) & ~SECTION_MASK) == 0)
  177. set_pmd(pmd, __pmd(phys | prot_sect_kernel));
  178. else
  179. alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys));
  180. phys += next - addr;
  181. } while (pmd++, addr = next, addr != end);
  182. }
  183. static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
  184. unsigned long end, unsigned long phys)
  185. {
  186. pud_t *pud = pud_offset(pgd, addr);
  187. unsigned long next;
  188. do {
  189. next = pud_addr_end(addr, end);
  190. alloc_init_pmd(pud, addr, next, phys);
  191. phys += next - addr;
  192. } while (pud++, addr = next, addr != end);
  193. }
  194. /*
  195. * Create the page directory entries and any necessary page tables for the
  196. * mapping specified by 'md'.
  197. */
  198. static void __init create_mapping(phys_addr_t phys, unsigned long virt,
  199. phys_addr_t size)
  200. {
  201. unsigned long addr, length, end, next;
  202. pgd_t *pgd;
  203. if (virt < VMALLOC_START) {
  204. pr_warning("BUG: not creating mapping for 0x%016llx at 0x%016lx - outside kernel range\n",
  205. phys, virt);
  206. return;
  207. }
  208. addr = virt & PAGE_MASK;
  209. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  210. pgd = pgd_offset_k(addr);
  211. end = addr + length;
  212. do {
  213. next = pgd_addr_end(addr, end);
  214. alloc_init_pud(pgd, addr, next, phys);
  215. phys += next - addr;
  216. } while (pgd++, addr = next, addr != end);
  217. }
  218. static void __init map_mem(void)
  219. {
  220. struct memblock_region *reg;
  221. /* map all the memory banks */
  222. for_each_memblock(memory, reg) {
  223. phys_addr_t start = reg->base;
  224. phys_addr_t end = start + reg->size;
  225. if (start >= end)
  226. break;
  227. create_mapping(start, __phys_to_virt(start), end - start);
  228. }
  229. }
  230. /*
  231. * paging_init() sets up the page tables, initialises the zone memory
  232. * maps and sets up the zero page.
  233. */
  234. void __init paging_init(void)
  235. {
  236. void *zero_page;
  237. /*
  238. * Maximum PGDIR_SIZE addressable via the initial direct kernel
  239. * mapping in swapper_pg_dir.
  240. */
  241. memblock_set_current_limit((PHYS_OFFSET & PGDIR_MASK) + PGDIR_SIZE);
  242. init_mem_pgprot();
  243. map_mem();
  244. /*
  245. * Finally flush the caches and tlb to ensure that we're in a
  246. * consistent state.
  247. */
  248. flush_cache_all();
  249. flush_tlb_all();
  250. /* allocate the zero page. */
  251. zero_page = early_alloc(PAGE_SIZE);
  252. bootmem_init();
  253. empty_zero_page = virt_to_page(zero_page);
  254. __flush_dcache_page(empty_zero_page);
  255. /*
  256. * TTBR0 is only used for the identity mapping at this stage. Make it
  257. * point to zero page to avoid speculatively fetching new entries.
  258. */
  259. cpu_set_reserved_ttbr0();
  260. flush_tlb_all();
  261. }
  262. /*
  263. * Enable the identity mapping to allow the MMU disabling.
  264. */
  265. void setup_mm_for_reboot(void)
  266. {
  267. cpu_switch_mm(idmap_pg_dir, &init_mm);
  268. flush_tlb_all();
  269. }
  270. /*
  271. * Check whether a kernel address is valid (derived from arch/x86/).
  272. */
  273. int kern_addr_valid(unsigned long addr)
  274. {
  275. pgd_t *pgd;
  276. pud_t *pud;
  277. pmd_t *pmd;
  278. pte_t *pte;
  279. if ((((long)addr) >> VA_BITS) != -1UL)
  280. return 0;
  281. pgd = pgd_offset_k(addr);
  282. if (pgd_none(*pgd))
  283. return 0;
  284. pud = pud_offset(pgd, addr);
  285. if (pud_none(*pud))
  286. return 0;
  287. pmd = pmd_offset(pud, addr);
  288. if (pmd_none(*pmd))
  289. return 0;
  290. pte = pte_offset_kernel(pmd, addr);
  291. if (pte_none(*pte))
  292. return 0;
  293. return pfn_valid(pte_pfn(*pte));
  294. }
  295. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  296. #ifdef CONFIG_ARM64_64K_PAGES
  297. int __meminit vmemmap_populate(struct page *start_page,
  298. unsigned long size, int node)
  299. {
  300. return vmemmap_populate_basepages(start_page, size, node);
  301. }
  302. #else /* !CONFIG_ARM64_64K_PAGES */
  303. int __meminit vmemmap_populate(struct page *start_page,
  304. unsigned long size, int node)
  305. {
  306. unsigned long addr = (unsigned long)start_page;
  307. unsigned long end = (unsigned long)(start_page + size);
  308. unsigned long next;
  309. pgd_t *pgd;
  310. pud_t *pud;
  311. pmd_t *pmd;
  312. do {
  313. next = pmd_addr_end(addr, end);
  314. pgd = vmemmap_pgd_populate(addr, node);
  315. if (!pgd)
  316. return -ENOMEM;
  317. pud = vmemmap_pud_populate(pgd, addr, node);
  318. if (!pud)
  319. return -ENOMEM;
  320. pmd = pmd_offset(pud, addr);
  321. if (pmd_none(*pmd)) {
  322. void *p = NULL;
  323. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  324. if (!p)
  325. return -ENOMEM;
  326. set_pmd(pmd, __pmd(__pa(p) | prot_sect_kernel));
  327. } else
  328. vmemmap_verify((pte_t *)pmd, node, addr, next);
  329. } while (addr = next, addr != end);
  330. return 0;
  331. }
  332. #endif /* CONFIG_ARM64_64K_PAGES */
  333. #endif /* CONFIG_SPARSEMEM_VMEMMAP */