mmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 <linux/io.h>
  28. #include <asm/cputype.h>
  29. #include <asm/sections.h>
  30. #include <asm/setup.h>
  31. #include <asm/sizes.h>
  32. #include <asm/tlb.h>
  33. #include <asm/mmu_context.h>
  34. #include "mm.h"
  35. /*
  36. * Empty_zero_page is a special page that is used for zero-initialized data
  37. * and COW.
  38. */
  39. struct page *empty_zero_page;
  40. EXPORT_SYMBOL(empty_zero_page);
  41. pgprot_t pgprot_default;
  42. EXPORT_SYMBOL(pgprot_default);
  43. static pmdval_t prot_sect_kernel;
  44. struct cachepolicy {
  45. const char policy[16];
  46. u64 mair;
  47. u64 tcr;
  48. };
  49. static struct cachepolicy cache_policies[] __initdata = {
  50. {
  51. .policy = "uncached",
  52. .mair = 0x44, /* inner, outer non-cacheable */
  53. .tcr = TCR_IRGN_NC | TCR_ORGN_NC,
  54. }, {
  55. .policy = "writethrough",
  56. .mair = 0xaa, /* inner, outer write-through, read-allocate */
  57. .tcr = TCR_IRGN_WT | TCR_ORGN_WT,
  58. }, {
  59. .policy = "writeback",
  60. .mair = 0xee, /* inner, outer write-back, read-allocate */
  61. .tcr = TCR_IRGN_WBnWA | TCR_ORGN_WBnWA,
  62. }
  63. };
  64. /*
  65. * These are useful for identifying cache coherency problems by allowing the
  66. * cache or the cache and writebuffer to be turned off. It changes the Normal
  67. * memory caching attributes in the MAIR_EL1 register.
  68. */
  69. static int __init early_cachepolicy(char *p)
  70. {
  71. int i;
  72. u64 tmp;
  73. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  74. int len = strlen(cache_policies[i].policy);
  75. if (memcmp(p, cache_policies[i].policy, len) == 0)
  76. break;
  77. }
  78. if (i == ARRAY_SIZE(cache_policies)) {
  79. pr_err("ERROR: unknown or unsupported cache policy: %s\n", p);
  80. return 0;
  81. }
  82. flush_cache_all();
  83. /*
  84. * Modify MT_NORMAL attributes in MAIR_EL1.
  85. */
  86. asm volatile(
  87. " mrs %0, mair_el1\n"
  88. " bfi %0, %1, #%2, #8\n"
  89. " msr mair_el1, %0\n"
  90. " isb\n"
  91. : "=&r" (tmp)
  92. : "r" (cache_policies[i].mair), "i" (MT_NORMAL * 8));
  93. /*
  94. * Modify TCR PTW cacheability attributes.
  95. */
  96. asm volatile(
  97. " mrs %0, tcr_el1\n"
  98. " bic %0, %0, %2\n"
  99. " orr %0, %0, %1\n"
  100. " msr tcr_el1, %0\n"
  101. " isb\n"
  102. : "=&r" (tmp)
  103. : "r" (cache_policies[i].tcr), "r" (TCR_IRGN_MASK | TCR_ORGN_MASK));
  104. flush_cache_all();
  105. return 0;
  106. }
  107. early_param("cachepolicy", early_cachepolicy);
  108. /*
  109. * Adjust the PMD section entries according to the CPU in use.
  110. */
  111. static void __init init_mem_pgprot(void)
  112. {
  113. pteval_t default_pgprot;
  114. int i;
  115. default_pgprot = PTE_ATTRINDX(MT_NORMAL);
  116. prot_sect_kernel = PMD_TYPE_SECT | PMD_SECT_AF | PMD_ATTRINDX(MT_NORMAL);
  117. #ifdef CONFIG_SMP
  118. /*
  119. * Mark memory with the "shared" attribute for SMP systems
  120. */
  121. default_pgprot |= PTE_SHARED;
  122. prot_sect_kernel |= PMD_SECT_S;
  123. #endif
  124. for (i = 0; i < 16; i++) {
  125. unsigned long v = pgprot_val(protection_map[i]);
  126. protection_map[i] = __pgprot(v | default_pgprot);
  127. }
  128. pgprot_default = __pgprot(PTE_TYPE_PAGE | PTE_AF | default_pgprot);
  129. }
  130. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  131. unsigned long size, pgprot_t vma_prot)
  132. {
  133. if (!pfn_valid(pfn))
  134. return pgprot_noncached(vma_prot);
  135. else if (file->f_flags & O_SYNC)
  136. return pgprot_writecombine(vma_prot);
  137. return vma_prot;
  138. }
  139. EXPORT_SYMBOL(phys_mem_access_prot);
  140. static void __init *early_alloc(unsigned long sz)
  141. {
  142. void *ptr = __va(memblock_alloc(sz, sz));
  143. memset(ptr, 0, sz);
  144. return ptr;
  145. }
  146. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  147. unsigned long end, unsigned long pfn)
  148. {
  149. pte_t *pte;
  150. if (pmd_none(*pmd)) {
  151. pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
  152. __pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
  153. }
  154. BUG_ON(pmd_bad(*pmd));
  155. pte = pte_offset_kernel(pmd, addr);
  156. do {
  157. set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
  158. pfn++;
  159. } while (pte++, addr += PAGE_SIZE, addr != end);
  160. }
  161. static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
  162. unsigned long end, phys_addr_t phys)
  163. {
  164. pmd_t *pmd;
  165. unsigned long next;
  166. /*
  167. * Check for initial section mappings in the pgd/pud and remove them.
  168. */
  169. if (pud_none(*pud) || pud_bad(*pud)) {
  170. pmd = early_alloc(PTRS_PER_PMD * sizeof(pmd_t));
  171. pud_populate(&init_mm, pud, pmd);
  172. }
  173. pmd = pmd_offset(pud, addr);
  174. do {
  175. next = pmd_addr_end(addr, end);
  176. /* try section mapping first */
  177. if (((addr | next | phys) & ~SECTION_MASK) == 0)
  178. set_pmd(pmd, __pmd(phys | prot_sect_kernel));
  179. else
  180. alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys));
  181. phys += next - addr;
  182. } while (pmd++, addr = next, addr != end);
  183. }
  184. static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
  185. unsigned long end, unsigned long phys)
  186. {
  187. pud_t *pud = pud_offset(pgd, addr);
  188. unsigned long next;
  189. do {
  190. next = pud_addr_end(addr, end);
  191. alloc_init_pmd(pud, addr, next, phys);
  192. phys += next - addr;
  193. } while (pud++, addr = next, addr != end);
  194. }
  195. /*
  196. * Create the page directory entries and any necessary page tables for the
  197. * mapping specified by 'md'.
  198. */
  199. static void __init create_mapping(phys_addr_t phys, unsigned long virt,
  200. phys_addr_t size)
  201. {
  202. unsigned long addr, length, end, next;
  203. pgd_t *pgd;
  204. if (virt < VMALLOC_START) {
  205. pr_warning("BUG: not creating mapping for 0x%016llx at 0x%016lx - outside kernel range\n",
  206. phys, virt);
  207. return;
  208. }
  209. addr = virt & PAGE_MASK;
  210. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  211. pgd = pgd_offset_k(addr);
  212. end = addr + length;
  213. do {
  214. next = pgd_addr_end(addr, end);
  215. alloc_init_pud(pgd, addr, next, phys);
  216. phys += next - addr;
  217. } while (pgd++, addr = next, addr != end);
  218. }
  219. #ifdef CONFIG_EARLY_PRINTK
  220. /*
  221. * Create an early I/O mapping using the pgd/pmd entries already populated
  222. * in head.S as this function is called too early to allocated any memory. The
  223. * mapping size is 2MB with 4KB pages or 64KB or 64KB pages.
  224. */
  225. void __iomem * __init early_io_map(phys_addr_t phys, unsigned long virt)
  226. {
  227. unsigned long size, mask;
  228. bool page64k = IS_ENABLED(CONFIG_ARM64_64K_PAGES);
  229. pgd_t *pgd;
  230. pud_t *pud;
  231. pmd_t *pmd;
  232. pte_t *pte;
  233. /*
  234. * No early pte entries with !ARM64_64K_PAGES configuration, so using
  235. * sections (pmd).
  236. */
  237. size = page64k ? PAGE_SIZE : SECTION_SIZE;
  238. mask = ~(size - 1);
  239. pgd = pgd_offset_k(virt);
  240. pud = pud_offset(pgd, virt);
  241. if (pud_none(*pud))
  242. return NULL;
  243. pmd = pmd_offset(pud, virt);
  244. if (page64k) {
  245. if (pmd_none(*pmd))
  246. return NULL;
  247. pte = pte_offset_kernel(pmd, virt);
  248. set_pte(pte, __pte((phys & mask) | PROT_DEVICE_nGnRE));
  249. } else {
  250. set_pmd(pmd, __pmd((phys & mask) | PROT_SECT_DEVICE_nGnRE));
  251. }
  252. return (void __iomem *)((virt & mask) + (phys & ~mask));
  253. }
  254. #endif
  255. static void __init map_mem(void)
  256. {
  257. struct memblock_region *reg;
  258. /* map all the memory banks */
  259. for_each_memblock(memory, reg) {
  260. phys_addr_t start = reg->base;
  261. phys_addr_t end = start + reg->size;
  262. if (start >= end)
  263. break;
  264. create_mapping(start, __phys_to_virt(start), end - start);
  265. }
  266. }
  267. /*
  268. * paging_init() sets up the page tables, initialises the zone memory
  269. * maps and sets up the zero page.
  270. */
  271. void __init paging_init(void)
  272. {
  273. void *zero_page;
  274. /*
  275. * Maximum PGDIR_SIZE addressable via the initial direct kernel
  276. * mapping in swapper_pg_dir.
  277. */
  278. memblock_set_current_limit((PHYS_OFFSET & PGDIR_MASK) + PGDIR_SIZE);
  279. init_mem_pgprot();
  280. map_mem();
  281. /*
  282. * Finally flush the caches and tlb to ensure that we're in a
  283. * consistent state.
  284. */
  285. flush_cache_all();
  286. flush_tlb_all();
  287. /* allocate the zero page. */
  288. zero_page = early_alloc(PAGE_SIZE);
  289. bootmem_init();
  290. empty_zero_page = virt_to_page(zero_page);
  291. __flush_dcache_page(empty_zero_page);
  292. /*
  293. * TTBR0 is only used for the identity mapping at this stage. Make it
  294. * point to zero page to avoid speculatively fetching new entries.
  295. */
  296. cpu_set_reserved_ttbr0();
  297. flush_tlb_all();
  298. }
  299. /*
  300. * Enable the identity mapping to allow the MMU disabling.
  301. */
  302. void setup_mm_for_reboot(void)
  303. {
  304. cpu_switch_mm(idmap_pg_dir, &init_mm);
  305. flush_tlb_all();
  306. }
  307. /*
  308. * Check whether a kernel address is valid (derived from arch/x86/).
  309. */
  310. int kern_addr_valid(unsigned long addr)
  311. {
  312. pgd_t *pgd;
  313. pud_t *pud;
  314. pmd_t *pmd;
  315. pte_t *pte;
  316. if ((((long)addr) >> VA_BITS) != -1UL)
  317. return 0;
  318. pgd = pgd_offset_k(addr);
  319. if (pgd_none(*pgd))
  320. return 0;
  321. pud = pud_offset(pgd, addr);
  322. if (pud_none(*pud))
  323. return 0;
  324. pmd = pmd_offset(pud, addr);
  325. if (pmd_none(*pmd))
  326. return 0;
  327. pte = pte_offset_kernel(pmd, addr);
  328. if (pte_none(*pte))
  329. return 0;
  330. return pfn_valid(pte_pfn(*pte));
  331. }
  332. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  333. #ifdef CONFIG_ARM64_64K_PAGES
  334. int __meminit vmemmap_populate(struct page *start_page,
  335. unsigned long size, int node)
  336. {
  337. return vmemmap_populate_basepages(start_page, size, node);
  338. }
  339. #else /* !CONFIG_ARM64_64K_PAGES */
  340. int __meminit vmemmap_populate(struct page *start_page,
  341. unsigned long size, int node)
  342. {
  343. unsigned long addr = (unsigned long)start_page;
  344. unsigned long end = (unsigned long)(start_page + size);
  345. unsigned long next;
  346. pgd_t *pgd;
  347. pud_t *pud;
  348. pmd_t *pmd;
  349. do {
  350. next = pmd_addr_end(addr, end);
  351. pgd = vmemmap_pgd_populate(addr, node);
  352. if (!pgd)
  353. return -ENOMEM;
  354. pud = vmemmap_pud_populate(pgd, addr, node);
  355. if (!pud)
  356. return -ENOMEM;
  357. pmd = pmd_offset(pud, addr);
  358. if (pmd_none(*pmd)) {
  359. void *p = NULL;
  360. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  361. if (!p)
  362. return -ENOMEM;
  363. set_pmd(pmd, __pmd(__pa(p) | prot_sect_kernel));
  364. } else
  365. vmemmap_verify((pte_t *)pmd, node, addr, next);
  366. } while (addr = next, addr != end);
  367. return 0;
  368. }
  369. #endif /* CONFIG_ARM64_64K_PAGES */
  370. void vmemmap_free(struct page *memmap, unsigned long nr_pages)
  371. {
  372. }
  373. #endif /* CONFIG_SPARSEMEM_VMEMMAP */