mmu.c 11 KB

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