mmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. struct cachepolicy {
  42. const char policy[16];
  43. u64 mair;
  44. u64 tcr;
  45. };
  46. static struct cachepolicy cache_policies[] __initdata = {
  47. {
  48. .policy = "uncached",
  49. .mair = 0x44, /* inner, outer non-cacheable */
  50. .tcr = TCR_IRGN_NC | TCR_ORGN_NC,
  51. }, {
  52. .policy = "writethrough",
  53. .mair = 0xaa, /* inner, outer write-through, read-allocate */
  54. .tcr = TCR_IRGN_WT | TCR_ORGN_WT,
  55. }, {
  56. .policy = "writeback",
  57. .mair = 0xee, /* inner, outer write-back, read-allocate */
  58. .tcr = TCR_IRGN_WBnWA | TCR_ORGN_WBnWA,
  59. }
  60. };
  61. /*
  62. * These are useful for identifying cache coherency problems by allowing the
  63. * cache or the cache and writebuffer to be turned off. It changes the Normal
  64. * memory caching attributes in the MAIR_EL1 register.
  65. */
  66. static int __init early_cachepolicy(char *p)
  67. {
  68. int i;
  69. u64 tmp;
  70. for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
  71. int len = strlen(cache_policies[i].policy);
  72. if (memcmp(p, cache_policies[i].policy, len) == 0)
  73. break;
  74. }
  75. if (i == ARRAY_SIZE(cache_policies)) {
  76. pr_err("ERROR: unknown or unsupported cache policy: %s\n", p);
  77. return 0;
  78. }
  79. flush_cache_all();
  80. /*
  81. * Modify MT_NORMAL attributes in MAIR_EL1.
  82. */
  83. asm volatile(
  84. " mrs %0, mair_el1\n"
  85. " bfi %0, %1, #%2, #8\n"
  86. " msr mair_el1, %0\n"
  87. " isb\n"
  88. : "=&r" (tmp)
  89. : "r" (cache_policies[i].mair), "i" (MT_NORMAL * 8));
  90. /*
  91. * Modify TCR PTW cacheability attributes.
  92. */
  93. asm volatile(
  94. " mrs %0, tcr_el1\n"
  95. " bic %0, %0, %2\n"
  96. " orr %0, %0, %1\n"
  97. " msr tcr_el1, %0\n"
  98. " isb\n"
  99. : "=&r" (tmp)
  100. : "r" (cache_policies[i].tcr), "r" (TCR_IRGN_MASK | TCR_ORGN_MASK));
  101. flush_cache_all();
  102. return 0;
  103. }
  104. early_param("cachepolicy", early_cachepolicy);
  105. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  106. unsigned long size, pgprot_t vma_prot)
  107. {
  108. if (!pfn_valid(pfn))
  109. return pgprot_noncached(vma_prot);
  110. else if (file->f_flags & O_SYNC)
  111. return pgprot_writecombine(vma_prot);
  112. return vma_prot;
  113. }
  114. EXPORT_SYMBOL(phys_mem_access_prot);
  115. static void __init *early_alloc(unsigned long sz)
  116. {
  117. void *ptr = __va(memblock_alloc(sz, sz));
  118. memset(ptr, 0, sz);
  119. return ptr;
  120. }
  121. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  122. unsigned long end, unsigned long pfn,
  123. pgprot_t prot)
  124. {
  125. pte_t *pte;
  126. if (pmd_none(*pmd)) {
  127. pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
  128. __pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
  129. }
  130. BUG_ON(pmd_bad(*pmd));
  131. pte = pte_offset_kernel(pmd, addr);
  132. do {
  133. set_pte(pte, pfn_pte(pfn, prot));
  134. pfn++;
  135. } while (pte++, addr += PAGE_SIZE, addr != end);
  136. }
  137. static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
  138. unsigned long end, phys_addr_t phys,
  139. int map_io)
  140. {
  141. pmd_t *pmd;
  142. unsigned long next;
  143. pmdval_t prot_sect;
  144. pgprot_t prot_pte;
  145. if (map_io) {
  146. prot_sect = PROT_SECT_DEVICE_nGnRE;
  147. prot_pte = __pgprot(PROT_DEVICE_nGnRE);
  148. } else {
  149. prot_sect = PROT_SECT_NORMAL_EXEC;
  150. prot_pte = PAGE_KERNEL_EXEC;
  151. }
  152. /*
  153. * Check for initial section mappings in the pgd/pud and remove them.
  154. */
  155. if (pud_none(*pud) || pud_bad(*pud)) {
  156. pmd = early_alloc(PTRS_PER_PMD * sizeof(pmd_t));
  157. pud_populate(&init_mm, pud, pmd);
  158. }
  159. pmd = pmd_offset(pud, addr);
  160. do {
  161. next = pmd_addr_end(addr, end);
  162. /* try section mapping first */
  163. if (((addr | next | phys) & ~SECTION_MASK) == 0) {
  164. pmd_t old_pmd =*pmd;
  165. set_pmd(pmd, __pmd(phys | prot_sect));
  166. /*
  167. * Check for previous table entries created during
  168. * boot (__create_page_tables) and flush them.
  169. */
  170. if (!pmd_none(old_pmd))
  171. flush_tlb_all();
  172. } else {
  173. alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
  174. prot_pte);
  175. }
  176. phys += next - addr;
  177. } while (pmd++, addr = next, addr != end);
  178. }
  179. static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
  180. unsigned long end, unsigned long phys,
  181. int map_io)
  182. {
  183. pud_t *pud = pud_offset(pgd, addr);
  184. unsigned long next;
  185. do {
  186. next = pud_addr_end(addr, end);
  187. /*
  188. * For 4K granule only, attempt to put down a 1GB block
  189. */
  190. if (!map_io && (PAGE_SHIFT == 12) &&
  191. ((addr | next | phys) & ~PUD_MASK) == 0) {
  192. pud_t old_pud = *pud;
  193. set_pud(pud, __pud(phys | PROT_SECT_NORMAL_EXEC));
  194. /*
  195. * If we have an old value for a pud, it will
  196. * be pointing to a pmd table that we no longer
  197. * need (from swapper_pg_dir).
  198. *
  199. * Look up the old pmd table and free it.
  200. */
  201. if (!pud_none(old_pud)) {
  202. phys_addr_t table = __pa(pmd_offset(&old_pud, 0));
  203. memblock_free(table, PAGE_SIZE);
  204. flush_tlb_all();
  205. }
  206. } else {
  207. alloc_init_pmd(pud, addr, next, phys, map_io);
  208. }
  209. phys += next - addr;
  210. } while (pud++, addr = next, addr != end);
  211. }
  212. /*
  213. * Create the page directory entries and any necessary page tables for the
  214. * mapping specified by 'md'.
  215. */
  216. static void __init __create_mapping(pgd_t *pgd, phys_addr_t phys,
  217. unsigned long virt, phys_addr_t size,
  218. int map_io)
  219. {
  220. unsigned long addr, length, end, next;
  221. addr = virt & PAGE_MASK;
  222. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  223. end = addr + length;
  224. do {
  225. next = pgd_addr_end(addr, end);
  226. alloc_init_pud(pgd, addr, next, phys, map_io);
  227. phys += next - addr;
  228. } while (pgd++, addr = next, addr != end);
  229. }
  230. static void __init create_mapping(phys_addr_t phys, unsigned long virt,
  231. phys_addr_t size)
  232. {
  233. if (virt < VMALLOC_START) {
  234. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  235. &phys, virt);
  236. return;
  237. }
  238. __create_mapping(pgd_offset_k(virt & PAGE_MASK), phys, virt, size, 0);
  239. }
  240. void __init create_id_mapping(phys_addr_t addr, phys_addr_t size, int map_io)
  241. {
  242. if ((addr >> PGDIR_SHIFT) >= ARRAY_SIZE(idmap_pg_dir)) {
  243. pr_warn("BUG: not creating id mapping for %pa\n", &addr);
  244. return;
  245. }
  246. __create_mapping(&idmap_pg_dir[pgd_index(addr)],
  247. addr, addr, size, map_io);
  248. }
  249. static void __init map_mem(void)
  250. {
  251. struct memblock_region *reg;
  252. phys_addr_t limit;
  253. /*
  254. * Temporarily limit the memblock range. We need to do this as
  255. * create_mapping requires puds, pmds and ptes to be allocated from
  256. * memory addressable from the initial direct kernel mapping.
  257. *
  258. * The initial direct kernel mapping, located at swapper_pg_dir,
  259. * gives us PGDIR_SIZE memory starting from PHYS_OFFSET (which must be
  260. * aligned to 2MB as per Documentation/arm64/booting.txt).
  261. */
  262. limit = PHYS_OFFSET + PGDIR_SIZE;
  263. memblock_set_current_limit(limit);
  264. /* map all the memory banks */
  265. for_each_memblock(memory, reg) {
  266. phys_addr_t start = reg->base;
  267. phys_addr_t end = start + reg->size;
  268. if (start >= end)
  269. break;
  270. #ifndef CONFIG_ARM64_64K_PAGES
  271. /*
  272. * For the first memory bank align the start address and
  273. * current memblock limit to prevent create_mapping() from
  274. * allocating pte page tables from unmapped memory.
  275. * When 64K pages are enabled, the pte page table for the
  276. * first PGDIR_SIZE is already present in swapper_pg_dir.
  277. */
  278. if (start < limit)
  279. start = ALIGN(start, PMD_SIZE);
  280. if (end < limit) {
  281. limit = end & PMD_MASK;
  282. memblock_set_current_limit(limit);
  283. }
  284. #endif
  285. create_mapping(start, __phys_to_virt(start), end - start);
  286. }
  287. /* Limit no longer required. */
  288. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  289. }
  290. /*
  291. * paging_init() sets up the page tables, initialises the zone memory
  292. * maps and sets up the zero page.
  293. */
  294. void __init paging_init(void)
  295. {
  296. void *zero_page;
  297. map_mem();
  298. /*
  299. * Finally flush the caches and tlb to ensure that we're in a
  300. * consistent state.
  301. */
  302. flush_cache_all();
  303. flush_tlb_all();
  304. /* allocate the zero page. */
  305. zero_page = early_alloc(PAGE_SIZE);
  306. bootmem_init();
  307. empty_zero_page = virt_to_page(zero_page);
  308. /*
  309. * TTBR0 is only used for the identity mapping at this stage. Make it
  310. * point to zero page to avoid speculatively fetching new entries.
  311. */
  312. cpu_set_reserved_ttbr0();
  313. flush_tlb_all();
  314. }
  315. /*
  316. * Enable the identity mapping to allow the MMU disabling.
  317. */
  318. void setup_mm_for_reboot(void)
  319. {
  320. cpu_switch_mm(idmap_pg_dir, &init_mm);
  321. flush_tlb_all();
  322. }
  323. /*
  324. * Check whether a kernel address is valid (derived from arch/x86/).
  325. */
  326. int kern_addr_valid(unsigned long addr)
  327. {
  328. pgd_t *pgd;
  329. pud_t *pud;
  330. pmd_t *pmd;
  331. pte_t *pte;
  332. if ((((long)addr) >> VA_BITS) != -1UL)
  333. return 0;
  334. pgd = pgd_offset_k(addr);
  335. if (pgd_none(*pgd))
  336. return 0;
  337. pud = pud_offset(pgd, addr);
  338. if (pud_none(*pud))
  339. return 0;
  340. if (pud_sect(*pud))
  341. return pfn_valid(pud_pfn(*pud));
  342. pmd = pmd_offset(pud, addr);
  343. if (pmd_none(*pmd))
  344. return 0;
  345. if (pmd_sect(*pmd))
  346. return pfn_valid(pmd_pfn(*pmd));
  347. pte = pte_offset_kernel(pmd, addr);
  348. if (pte_none(*pte))
  349. return 0;
  350. return pfn_valid(pte_pfn(*pte));
  351. }
  352. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  353. #ifdef CONFIG_ARM64_64K_PAGES
  354. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  355. {
  356. return vmemmap_populate_basepages(start, end, node);
  357. }
  358. #else /* !CONFIG_ARM64_64K_PAGES */
  359. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  360. {
  361. unsigned long addr = start;
  362. unsigned long next;
  363. pgd_t *pgd;
  364. pud_t *pud;
  365. pmd_t *pmd;
  366. do {
  367. next = pmd_addr_end(addr, end);
  368. pgd = vmemmap_pgd_populate(addr, node);
  369. if (!pgd)
  370. return -ENOMEM;
  371. pud = vmemmap_pud_populate(pgd, addr, node);
  372. if (!pud)
  373. return -ENOMEM;
  374. pmd = pmd_offset(pud, addr);
  375. if (pmd_none(*pmd)) {
  376. void *p = NULL;
  377. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  378. if (!p)
  379. return -ENOMEM;
  380. set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
  381. } else
  382. vmemmap_verify((pte_t *)pmd, node, addr, next);
  383. } while (addr = next, addr != end);
  384. return 0;
  385. }
  386. #endif /* CONFIG_ARM64_64K_PAGES */
  387. void vmemmap_free(unsigned long start, unsigned long end)
  388. {
  389. }
  390. #endif /* CONFIG_SPARSEMEM_VMEMMAP */