mmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <mt-plat/mtk_memcfg.h>
  36. #include "mm.h"
  37. /*
  38. * Empty_zero_page is a special page that is used for zero-initialized data
  39. * and COW.
  40. */
  41. struct page *empty_zero_page;
  42. EXPORT_SYMBOL(empty_zero_page);
  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. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  108. unsigned long size, pgprot_t vma_prot)
  109. {
  110. if (!pfn_valid(pfn))
  111. return pgprot_noncached(vma_prot);
  112. else if (file->f_flags & O_SYNC)
  113. return pgprot_writecombine(vma_prot);
  114. return vma_prot;
  115. }
  116. EXPORT_SYMBOL(phys_mem_access_prot);
  117. static void __init *early_alloc(unsigned long sz)
  118. {
  119. void *ptr = __va(memblock_alloc(sz, sz));
  120. memset(ptr, 0, sz);
  121. return ptr;
  122. }
  123. static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
  124. unsigned long end, unsigned long pfn,
  125. pgprot_t prot)
  126. {
  127. pte_t *pte;
  128. if (pmd_none(*pmd)) {
  129. pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
  130. __pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
  131. }
  132. BUG_ON(pmd_bad(*pmd));
  133. pte = pte_offset_kernel(pmd, addr);
  134. do {
  135. set_pte(pte, pfn_pte(pfn, prot));
  136. pfn++;
  137. } while (pte++, addr += PAGE_SIZE, addr != end);
  138. }
  139. static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
  140. unsigned long end, phys_addr_t phys,
  141. int map_io)
  142. {
  143. pmd_t *pmd;
  144. unsigned long next;
  145. pmdval_t prot_sect;
  146. pgprot_t prot_pte;
  147. if (map_io) {
  148. prot_sect = PROT_SECT_DEVICE_nGnRE;
  149. prot_pte = __pgprot(PROT_DEVICE_nGnRE);
  150. } else {
  151. prot_sect = PROT_SECT_NORMAL_EXEC;
  152. prot_pte = PAGE_KERNEL_EXEC;
  153. }
  154. /*
  155. * Check for initial section mappings in the pgd/pud and remove them.
  156. */
  157. if (pud_none(*pud) || pud_bad(*pud)) {
  158. pmd = early_alloc(PTRS_PER_PMD * sizeof(pmd_t));
  159. pud_populate(&init_mm, pud, pmd);
  160. }
  161. pmd = pmd_offset(pud, addr);
  162. do {
  163. next = pmd_addr_end(addr, end);
  164. /* try section mapping first */
  165. if (((addr | next | phys) & ~SECTION_MASK) == 0) {
  166. pmd_t old_pmd =*pmd;
  167. set_pmd(pmd, __pmd(phys | prot_sect));
  168. /*
  169. * Check for previous table entries created during
  170. * boot (__create_page_tables) and flush them.
  171. */
  172. if (!pmd_none(old_pmd))
  173. flush_tlb_all();
  174. } else {
  175. alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
  176. prot_pte);
  177. }
  178. phys += next - addr;
  179. } while (pmd++, addr = next, addr != end);
  180. }
  181. static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
  182. unsigned long end, phys_addr_t phys,
  183. int map_io)
  184. {
  185. pud_t *pud;
  186. unsigned long next;
  187. if (pgd_none(*pgd)) {
  188. pud = early_alloc(PTRS_PER_PUD * sizeof(pud_t));
  189. pgd_populate(&init_mm, pgd, pud);
  190. }
  191. BUG_ON(pgd_bad(*pgd));
  192. pud = pud_offset(pgd, addr);
  193. do {
  194. next = pud_addr_end(addr, end);
  195. /*
  196. * For 4K granule only, attempt to put down a 1GB block
  197. */
  198. if (!map_io && (PAGE_SHIFT == 12) &&
  199. ((addr | next | phys) & ~PUD_MASK) == 0) {
  200. pud_t old_pud = *pud;
  201. set_pud(pud, __pud(phys | PROT_SECT_NORMAL_EXEC));
  202. /*
  203. * If we have an old value for a pud, it will
  204. * be pointing to a pmd table that we no longer
  205. * need (from swapper_pg_dir).
  206. *
  207. * Look up the old pmd table and free it.
  208. */
  209. if (!pud_none(old_pud)) {
  210. phys_addr_t table = __pa(pmd_offset(&old_pud, 0));
  211. memblock_free(table, PAGE_SIZE);
  212. flush_tlb_all();
  213. }
  214. } else {
  215. alloc_init_pmd(pud, addr, next, phys, map_io);
  216. }
  217. phys += next - addr;
  218. } while (pud++, addr = next, addr != end);
  219. }
  220. /*
  221. * Create the page directory entries and any necessary page tables for the
  222. * mapping specified by 'md'.
  223. */
  224. static void __init __create_mapping(pgd_t *pgd, phys_addr_t phys,
  225. unsigned long virt, phys_addr_t size,
  226. int map_io)
  227. {
  228. unsigned long addr, length, end, next;
  229. addr = virt & PAGE_MASK;
  230. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  231. end = addr + length;
  232. do {
  233. next = pgd_addr_end(addr, end);
  234. alloc_init_pud(pgd, addr, next, phys, map_io);
  235. phys += next - addr;
  236. } while (pgd++, addr = next, addr != end);
  237. }
  238. static void __init create_mapping(phys_addr_t phys, unsigned long virt,
  239. phys_addr_t size)
  240. {
  241. if (virt < VMALLOC_START) {
  242. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  243. &phys, virt);
  244. return;
  245. }
  246. __create_mapping(pgd_offset_k(virt & PAGE_MASK), phys, virt, size, 0);
  247. }
  248. void __init create_id_mapping(phys_addr_t addr, phys_addr_t size, int map_io)
  249. {
  250. if ((addr >> PGDIR_SHIFT) >= ARRAY_SIZE(idmap_pg_dir)) {
  251. pr_warn("BUG: not creating id mapping for %pa\n", &addr);
  252. return;
  253. }
  254. __create_mapping(&idmap_pg_dir[pgd_index(addr)],
  255. addr, addr, size, map_io);
  256. }
  257. static void __init map_mem(void)
  258. {
  259. struct memblock_region *reg;
  260. phys_addr_t limit;
  261. /*
  262. * Temporarily limit the memblock range. We need to do this as
  263. * create_mapping requires puds, pmds and ptes to be allocated from
  264. * memory addressable from the initial direct kernel mapping.
  265. *
  266. * The initial direct kernel mapping, located at swapper_pg_dir, gives
  267. * us PUD_SIZE (4K pages) or PMD_SIZE (64K pages) memory starting from
  268. * PHYS_OFFSET (which must be aligned to 2MB as per
  269. * Documentation/arm64/booting.txt).
  270. */
  271. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
  272. limit = PHYS_OFFSET + PMD_SIZE;
  273. else
  274. limit = PHYS_OFFSET + PUD_SIZE;
  275. memblock_set_current_limit(limit);
  276. /* map all the memory banks */
  277. for_each_memblock(memory, reg) {
  278. phys_addr_t start = reg->base;
  279. phys_addr_t end = start + reg->size;
  280. MTK_MEMCFG_LOG_AND_PRINTK(
  281. "[PHY layout]kernel : 0x%08llx - 0x%08llx (0x%llx)\n",
  282. (unsigned long long)start,
  283. (unsigned long long)start + reg->size - 1,
  284. (unsigned long long)reg->size);
  285. if (start >= end)
  286. break;
  287. #ifndef CONFIG_ARM64_64K_PAGES
  288. /*
  289. * For the first memory bank align the start address and
  290. * current memblock limit to prevent create_mapping() from
  291. * allocating pte page tables from unmapped memory.
  292. * When 64K pages are enabled, the pte page table for the
  293. * first PGDIR_SIZE is already present in swapper_pg_dir.
  294. */
  295. if (start < limit)
  296. start = ALIGN(start, PMD_SIZE);
  297. if (end < limit) {
  298. limit = end & PMD_MASK;
  299. memblock_set_current_limit(limit);
  300. }
  301. #endif
  302. create_mapping(start, __phys_to_virt(start), end - start);
  303. }
  304. /* Limit no longer required. */
  305. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  306. }
  307. /*
  308. * paging_init() sets up the page tables, initialises the zone memory
  309. * maps and sets up the zero page.
  310. */
  311. void __init paging_init(void)
  312. {
  313. void *zero_page;
  314. map_mem();
  315. /*
  316. * Finally flush the caches and tlb to ensure that we're in a
  317. * consistent state.
  318. */
  319. flush_cache_all();
  320. flush_tlb_all();
  321. /* allocate the zero page. */
  322. zero_page = early_alloc(PAGE_SIZE);
  323. bootmem_init();
  324. empty_zero_page = virt_to_page(zero_page);
  325. /*
  326. * TTBR0 is only used for the identity mapping at this stage. Make it
  327. * point to zero page to avoid speculatively fetching new entries.
  328. */
  329. cpu_set_reserved_ttbr0();
  330. flush_tlb_all();
  331. }
  332. /*
  333. * Enable the identity mapping to allow the MMU disabling.
  334. */
  335. void setup_mm_for_reboot(void)
  336. {
  337. cpu_switch_mm(idmap_pg_dir, &init_mm);
  338. flush_tlb_all();
  339. }
  340. /*
  341. * Check whether a kernel address is valid (derived from arch/x86/).
  342. */
  343. int kern_addr_valid(unsigned long addr)
  344. {
  345. pgd_t *pgd;
  346. pud_t *pud;
  347. pmd_t *pmd;
  348. pte_t *pte;
  349. if ((((long)addr) >> VA_BITS) != -1UL)
  350. return 0;
  351. pgd = pgd_offset_k(addr);
  352. if (pgd_none(*pgd))
  353. return 0;
  354. pud = pud_offset(pgd, addr);
  355. if (pud_none(*pud))
  356. return 0;
  357. if (pud_sect(*pud))
  358. return pfn_valid(pud_pfn(*pud));
  359. pmd = pmd_offset(pud, addr);
  360. if (pmd_none(*pmd))
  361. return 0;
  362. if (pmd_sect(*pmd))
  363. return pfn_valid(pmd_pfn(*pmd));
  364. pte = pte_offset_kernel(pmd, addr);
  365. if (pte_none(*pte))
  366. return 0;
  367. return pfn_valid(pte_pfn(*pte));
  368. }
  369. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  370. #ifdef CONFIG_ARM64_64K_PAGES
  371. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  372. {
  373. return vmemmap_populate_basepages(start, end, node);
  374. }
  375. #else /* !CONFIG_ARM64_64K_PAGES */
  376. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  377. {
  378. unsigned long addr = start;
  379. unsigned long next;
  380. pgd_t *pgd;
  381. pud_t *pud;
  382. pmd_t *pmd;
  383. do {
  384. next = pmd_addr_end(addr, end);
  385. pgd = vmemmap_pgd_populate(addr, node);
  386. if (!pgd)
  387. return -ENOMEM;
  388. pud = vmemmap_pud_populate(pgd, addr, node);
  389. if (!pud)
  390. return -ENOMEM;
  391. pmd = pmd_offset(pud, addr);
  392. if (pmd_none(*pmd)) {
  393. void *p = NULL;
  394. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  395. if (!p)
  396. return -ENOMEM;
  397. set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
  398. } else
  399. vmemmap_verify((pte_t *)pmd, node, addr, next);
  400. } while (addr = next, addr != end);
  401. return 0;
  402. }
  403. #endif /* CONFIG_ARM64_64K_PAGES */
  404. void vmemmap_free(unsigned long start, unsigned long end)
  405. {
  406. }
  407. #endif /* CONFIG_SPARSEMEM_VMEMMAP */