mmu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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/cache.h>
  20. #include <linux/export.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/libfdt.h>
  25. #include <linux/mman.h>
  26. #include <linux/nodemask.h>
  27. #include <linux/memblock.h>
  28. #include <linux/fs.h>
  29. #include <linux/io.h>
  30. #include <asm/barrier.h>
  31. #include <asm/cputype.h>
  32. #include <asm/fixmap.h>
  33. #include <asm/kasan.h>
  34. #include <asm/kernel-pgtable.h>
  35. #include <asm/sections.h>
  36. #include <asm/setup.h>
  37. #include <asm/sizes.h>
  38. #include <asm/tlb.h>
  39. #include <asm/memblock.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/ptdump.h>
  42. u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
  43. u64 kimage_voffset __ro_after_init;
  44. EXPORT_SYMBOL(kimage_voffset);
  45. /*
  46. * Empty_zero_page is a special page that is used for zero-initialized data
  47. * and COW.
  48. */
  49. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  50. EXPORT_SYMBOL(empty_zero_page);
  51. static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
  52. static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
  53. static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
  54. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  55. unsigned long size, pgprot_t vma_prot)
  56. {
  57. if (!pfn_valid(pfn))
  58. return pgprot_noncached(vma_prot);
  59. else if (file->f_flags & O_SYNC)
  60. return pgprot_writecombine(vma_prot);
  61. return vma_prot;
  62. }
  63. EXPORT_SYMBOL(phys_mem_access_prot);
  64. static phys_addr_t __init early_pgtable_alloc(void)
  65. {
  66. phys_addr_t phys;
  67. void *ptr;
  68. phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  69. /*
  70. * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
  71. * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
  72. * any level of table.
  73. */
  74. ptr = pte_set_fixmap(phys);
  75. memset(ptr, 0, PAGE_SIZE);
  76. /*
  77. * Implicit barriers also ensure the zeroed page is visible to the page
  78. * table walker
  79. */
  80. pte_clear_fixmap();
  81. return phys;
  82. }
  83. static bool pgattr_change_is_safe(u64 old, u64 new)
  84. {
  85. /*
  86. * The following mapping attributes may be updated in live
  87. * kernel mappings without the need for break-before-make.
  88. */
  89. static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE;
  90. return old == 0 || new == 0 || ((old ^ new) & ~mask) == 0;
  91. }
  92. static void alloc_init_pte(pmd_t *pmd, unsigned long addr,
  93. unsigned long end, unsigned long pfn,
  94. pgprot_t prot,
  95. phys_addr_t (*pgtable_alloc)(void),
  96. bool page_mappings_only)
  97. {
  98. pgprot_t __prot = prot;
  99. pte_t *pte;
  100. BUG_ON(pmd_sect(*pmd));
  101. if (pmd_none(*pmd)) {
  102. phys_addr_t pte_phys;
  103. BUG_ON(!pgtable_alloc);
  104. pte_phys = pgtable_alloc();
  105. pte = pte_set_fixmap(pte_phys);
  106. __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
  107. pte_clear_fixmap();
  108. }
  109. BUG_ON(pmd_bad(*pmd));
  110. pte = pte_set_fixmap_offset(pmd, addr);
  111. do {
  112. pte_t old_pte = *pte;
  113. /*
  114. * Set the contiguous bit for the subsequent group of PTEs if
  115. * its size and alignment are appropriate.
  116. */
  117. if (((addr | PFN_PHYS(pfn)) & ~CONT_PTE_MASK) == 0) {
  118. if (end - addr >= CONT_PTE_SIZE && !page_mappings_only)
  119. __prot = __pgprot(pgprot_val(prot) | PTE_CONT);
  120. else
  121. __prot = prot;
  122. }
  123. set_pte(pte, pfn_pte(pfn, __prot));
  124. pfn++;
  125. /*
  126. * After the PTE entry has been populated once, we
  127. * only allow updates to the permission attributes.
  128. */
  129. BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), pte_val(*pte)));
  130. } while (pte++, addr += PAGE_SIZE, addr != end);
  131. pte_clear_fixmap();
  132. }
  133. static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
  134. phys_addr_t phys, pgprot_t prot,
  135. phys_addr_t (*pgtable_alloc)(void),
  136. bool page_mappings_only)
  137. {
  138. pgprot_t __prot = prot;
  139. pmd_t *pmd;
  140. unsigned long next;
  141. /*
  142. * Check for initial section mappings in the pgd/pud and remove them.
  143. */
  144. BUG_ON(pud_sect(*pud));
  145. if (pud_none(*pud)) {
  146. phys_addr_t pmd_phys;
  147. BUG_ON(!pgtable_alloc);
  148. pmd_phys = pgtable_alloc();
  149. pmd = pmd_set_fixmap(pmd_phys);
  150. __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
  151. pmd_clear_fixmap();
  152. }
  153. BUG_ON(pud_bad(*pud));
  154. pmd = pmd_set_fixmap_offset(pud, addr);
  155. do {
  156. pmd_t old_pmd = *pmd;
  157. next = pmd_addr_end(addr, end);
  158. /* try section mapping first */
  159. if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
  160. !page_mappings_only) {
  161. /*
  162. * Set the contiguous bit for the subsequent group of
  163. * PMDs if its size and alignment are appropriate.
  164. */
  165. if (((addr | phys) & ~CONT_PMD_MASK) == 0) {
  166. if (end - addr >= CONT_PMD_SIZE)
  167. __prot = __pgprot(pgprot_val(prot) |
  168. PTE_CONT);
  169. else
  170. __prot = prot;
  171. }
  172. pmd_set_huge(pmd, phys, __prot);
  173. /*
  174. * After the PMD entry has been populated once, we
  175. * only allow updates to the permission attributes.
  176. */
  177. BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
  178. pmd_val(*pmd)));
  179. } else {
  180. alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
  181. prot, pgtable_alloc,
  182. page_mappings_only);
  183. BUG_ON(pmd_val(old_pmd) != 0 &&
  184. pmd_val(old_pmd) != pmd_val(*pmd));
  185. }
  186. phys += next - addr;
  187. } while (pmd++, addr = next, addr != end);
  188. pmd_clear_fixmap();
  189. }
  190. static inline bool use_1G_block(unsigned long addr, unsigned long next,
  191. unsigned long phys)
  192. {
  193. if (PAGE_SHIFT != 12)
  194. return false;
  195. if (((addr | next | phys) & ~PUD_MASK) != 0)
  196. return false;
  197. return true;
  198. }
  199. static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
  200. phys_addr_t phys, pgprot_t prot,
  201. phys_addr_t (*pgtable_alloc)(void),
  202. bool page_mappings_only)
  203. {
  204. pud_t *pud;
  205. unsigned long next;
  206. if (pgd_none(*pgd)) {
  207. phys_addr_t pud_phys;
  208. BUG_ON(!pgtable_alloc);
  209. pud_phys = pgtable_alloc();
  210. __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
  211. }
  212. BUG_ON(pgd_bad(*pgd));
  213. pud = pud_set_fixmap_offset(pgd, addr);
  214. do {
  215. pud_t old_pud = *pud;
  216. next = pud_addr_end(addr, end);
  217. /*
  218. * For 4K granule only, attempt to put down a 1GB block
  219. */
  220. if (use_1G_block(addr, next, phys) && !page_mappings_only) {
  221. pud_set_huge(pud, phys, prot);
  222. /*
  223. * After the PUD entry has been populated once, we
  224. * only allow updates to the permission attributes.
  225. */
  226. BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
  227. pud_val(*pud)));
  228. } else {
  229. alloc_init_pmd(pud, addr, next, phys, prot,
  230. pgtable_alloc, page_mappings_only);
  231. BUG_ON(pud_val(old_pud) != 0 &&
  232. pud_val(old_pud) != pud_val(*pud));
  233. }
  234. phys += next - addr;
  235. } while (pud++, addr = next, addr != end);
  236. pud_clear_fixmap();
  237. }
  238. static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
  239. unsigned long virt, phys_addr_t size,
  240. pgprot_t prot,
  241. phys_addr_t (*pgtable_alloc)(void),
  242. bool page_mappings_only)
  243. {
  244. unsigned long addr, length, end, next;
  245. pgd_t *pgd = pgd_offset_raw(pgdir, virt);
  246. /*
  247. * If the virtual and physical address don't have the same offset
  248. * within a page, we cannot map the region as the caller expects.
  249. */
  250. if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
  251. return;
  252. phys &= PAGE_MASK;
  253. addr = virt & PAGE_MASK;
  254. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  255. end = addr + length;
  256. do {
  257. next = pgd_addr_end(addr, end);
  258. alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc,
  259. page_mappings_only);
  260. phys += next - addr;
  261. } while (pgd++, addr = next, addr != end);
  262. }
  263. static phys_addr_t pgd_pgtable_alloc(void)
  264. {
  265. void *ptr = (void *)__get_free_page(PGALLOC_GFP);
  266. if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
  267. BUG();
  268. /* Ensure the zeroed page is visible to the page table walker */
  269. dsb(ishst);
  270. return __pa(ptr);
  271. }
  272. /*
  273. * This function can only be used to modify existing table entries,
  274. * without allocating new levels of table. Note that this permits the
  275. * creation of new section or page entries.
  276. */
  277. static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
  278. phys_addr_t size, pgprot_t prot)
  279. {
  280. if (virt < VMALLOC_START) {
  281. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  282. &phys, virt);
  283. return;
  284. }
  285. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, false);
  286. }
  287. void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
  288. unsigned long virt, phys_addr_t size,
  289. pgprot_t prot, bool page_mappings_only)
  290. {
  291. BUG_ON(mm == &init_mm);
  292. __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
  293. pgd_pgtable_alloc, page_mappings_only);
  294. }
  295. static void create_mapping_late(phys_addr_t phys, unsigned long virt,
  296. phys_addr_t size, pgprot_t prot)
  297. {
  298. if (virt < VMALLOC_START) {
  299. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  300. &phys, virt);
  301. return;
  302. }
  303. __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
  304. NULL, debug_pagealloc_enabled());
  305. }
  306. static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
  307. {
  308. unsigned long kernel_start = __pa(_text);
  309. unsigned long kernel_end = __pa(__init_begin);
  310. /*
  311. * Take care not to create a writable alias for the
  312. * read-only text and rodata sections of the kernel image.
  313. */
  314. /* No overlap with the kernel text/rodata */
  315. if (end < kernel_start || start >= kernel_end) {
  316. __create_pgd_mapping(pgd, start, __phys_to_virt(start),
  317. end - start, PAGE_KERNEL,
  318. early_pgtable_alloc,
  319. debug_pagealloc_enabled());
  320. return;
  321. }
  322. /*
  323. * This block overlaps the kernel text/rodata mappings.
  324. * Map the portion(s) which don't overlap.
  325. */
  326. if (start < kernel_start)
  327. __create_pgd_mapping(pgd, start,
  328. __phys_to_virt(start),
  329. kernel_start - start, PAGE_KERNEL,
  330. early_pgtable_alloc,
  331. debug_pagealloc_enabled());
  332. if (kernel_end < end)
  333. __create_pgd_mapping(pgd, kernel_end,
  334. __phys_to_virt(kernel_end),
  335. end - kernel_end, PAGE_KERNEL,
  336. early_pgtable_alloc,
  337. debug_pagealloc_enabled());
  338. /*
  339. * Map the linear alias of the [_text, __init_begin) interval as
  340. * read-only/non-executable. This makes the contents of the
  341. * region accessible to subsystems such as hibernate, but
  342. * protects it from inadvertent modification or execution.
  343. */
  344. __create_pgd_mapping(pgd, kernel_start, __phys_to_virt(kernel_start),
  345. kernel_end - kernel_start, PAGE_KERNEL_RO,
  346. early_pgtable_alloc, debug_pagealloc_enabled());
  347. }
  348. static void __init map_mem(pgd_t *pgd)
  349. {
  350. struct memblock_region *reg;
  351. /* map all the memory banks */
  352. for_each_memblock(memory, reg) {
  353. phys_addr_t start = reg->base;
  354. phys_addr_t end = start + reg->size;
  355. if (start >= end)
  356. break;
  357. if (memblock_is_nomap(reg))
  358. continue;
  359. __map_memblock(pgd, start, end);
  360. }
  361. }
  362. void mark_rodata_ro(void)
  363. {
  364. unsigned long section_size;
  365. section_size = (unsigned long)_etext - (unsigned long)_text;
  366. create_mapping_late(__pa(_text), (unsigned long)_text,
  367. section_size, PAGE_KERNEL_ROX);
  368. /*
  369. * mark .rodata as read only. Use __init_begin rather than __end_rodata
  370. * to cover NOTES and EXCEPTION_TABLE.
  371. */
  372. section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
  373. create_mapping_late(__pa(__start_rodata), (unsigned long)__start_rodata,
  374. section_size, PAGE_KERNEL_RO);
  375. /* flush the TLBs after updating live kernel mappings */
  376. flush_tlb_all();
  377. debug_checkwx();
  378. }
  379. static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end,
  380. pgprot_t prot, struct vm_struct *vma)
  381. {
  382. phys_addr_t pa_start = __pa(va_start);
  383. unsigned long size = va_end - va_start;
  384. BUG_ON(!PAGE_ALIGNED(pa_start));
  385. BUG_ON(!PAGE_ALIGNED(size));
  386. __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
  387. early_pgtable_alloc, debug_pagealloc_enabled());
  388. vma->addr = va_start;
  389. vma->phys_addr = pa_start;
  390. vma->size = size;
  391. vma->flags = VM_MAP;
  392. vma->caller = __builtin_return_address(0);
  393. vm_area_add_early(vma);
  394. }
  395. /*
  396. * Create fine-grained mappings for the kernel.
  397. */
  398. static void __init map_kernel(pgd_t *pgd)
  399. {
  400. static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_init, vmlinux_data;
  401. map_kernel_segment(pgd, _text, _etext, PAGE_KERNEL_EXEC, &vmlinux_text);
  402. map_kernel_segment(pgd, __start_rodata, __init_begin, PAGE_KERNEL, &vmlinux_rodata);
  403. map_kernel_segment(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC,
  404. &vmlinux_init);
  405. map_kernel_segment(pgd, _data, _end, PAGE_KERNEL, &vmlinux_data);
  406. if (!pgd_val(*pgd_offset_raw(pgd, FIXADDR_START))) {
  407. /*
  408. * The fixmap falls in a separate pgd to the kernel, and doesn't
  409. * live in the carveout for the swapper_pg_dir. We can simply
  410. * re-use the existing dir for the fixmap.
  411. */
  412. set_pgd(pgd_offset_raw(pgd, FIXADDR_START),
  413. *pgd_offset_k(FIXADDR_START));
  414. } else if (CONFIG_PGTABLE_LEVELS > 3) {
  415. /*
  416. * The fixmap shares its top level pgd entry with the kernel
  417. * mapping. This can really only occur when we are running
  418. * with 16k/4 levels, so we can simply reuse the pud level
  419. * entry instead.
  420. */
  421. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  422. set_pud(pud_set_fixmap_offset(pgd, FIXADDR_START),
  423. __pud(__pa(bm_pmd) | PUD_TYPE_TABLE));
  424. pud_clear_fixmap();
  425. } else {
  426. BUG();
  427. }
  428. kasan_copy_shadow(pgd);
  429. }
  430. /*
  431. * paging_init() sets up the page tables, initialises the zone memory
  432. * maps and sets up the zero page.
  433. */
  434. void __init paging_init(void)
  435. {
  436. phys_addr_t pgd_phys = early_pgtable_alloc();
  437. pgd_t *pgd = pgd_set_fixmap(pgd_phys);
  438. map_kernel(pgd);
  439. map_mem(pgd);
  440. /*
  441. * We want to reuse the original swapper_pg_dir so we don't have to
  442. * communicate the new address to non-coherent secondaries in
  443. * secondary_entry, and so cpu_switch_mm can generate the address with
  444. * adrp+add rather than a load from some global variable.
  445. *
  446. * To do this we need to go via a temporary pgd.
  447. */
  448. cpu_replace_ttbr1(__va(pgd_phys));
  449. memcpy(swapper_pg_dir, pgd, PAGE_SIZE);
  450. cpu_replace_ttbr1(swapper_pg_dir);
  451. pgd_clear_fixmap();
  452. memblock_free(pgd_phys, PAGE_SIZE);
  453. /*
  454. * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
  455. * allocated with it.
  456. */
  457. memblock_free(__pa(swapper_pg_dir) + PAGE_SIZE,
  458. SWAPPER_DIR_SIZE - PAGE_SIZE);
  459. }
  460. /*
  461. * Check whether a kernel address is valid (derived from arch/x86/).
  462. */
  463. int kern_addr_valid(unsigned long addr)
  464. {
  465. pgd_t *pgd;
  466. pud_t *pud;
  467. pmd_t *pmd;
  468. pte_t *pte;
  469. if ((((long)addr) >> VA_BITS) != -1UL)
  470. return 0;
  471. pgd = pgd_offset_k(addr);
  472. if (pgd_none(*pgd))
  473. return 0;
  474. pud = pud_offset(pgd, addr);
  475. if (pud_none(*pud))
  476. return 0;
  477. if (pud_sect(*pud))
  478. return pfn_valid(pud_pfn(*pud));
  479. pmd = pmd_offset(pud, addr);
  480. if (pmd_none(*pmd))
  481. return 0;
  482. if (pmd_sect(*pmd))
  483. return pfn_valid(pmd_pfn(*pmd));
  484. pte = pte_offset_kernel(pmd, addr);
  485. if (pte_none(*pte))
  486. return 0;
  487. return pfn_valid(pte_pfn(*pte));
  488. }
  489. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  490. #if !ARM64_SWAPPER_USES_SECTION_MAPS
  491. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  492. {
  493. return vmemmap_populate_basepages(start, end, node);
  494. }
  495. #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */
  496. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  497. {
  498. unsigned long addr = start;
  499. unsigned long next;
  500. pgd_t *pgd;
  501. pud_t *pud;
  502. pmd_t *pmd;
  503. do {
  504. next = pmd_addr_end(addr, end);
  505. pgd = vmemmap_pgd_populate(addr, node);
  506. if (!pgd)
  507. return -ENOMEM;
  508. pud = vmemmap_pud_populate(pgd, addr, node);
  509. if (!pud)
  510. return -ENOMEM;
  511. pmd = pmd_offset(pud, addr);
  512. if (pmd_none(*pmd)) {
  513. void *p = NULL;
  514. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  515. if (!p)
  516. return -ENOMEM;
  517. set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
  518. } else
  519. vmemmap_verify((pte_t *)pmd, node, addr, next);
  520. } while (addr = next, addr != end);
  521. return 0;
  522. }
  523. #endif /* CONFIG_ARM64_64K_PAGES */
  524. void vmemmap_free(unsigned long start, unsigned long end)
  525. {
  526. }
  527. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  528. static inline pud_t * fixmap_pud(unsigned long addr)
  529. {
  530. pgd_t *pgd = pgd_offset_k(addr);
  531. BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
  532. return pud_offset_kimg(pgd, addr);
  533. }
  534. static inline pmd_t * fixmap_pmd(unsigned long addr)
  535. {
  536. pud_t *pud = fixmap_pud(addr);
  537. BUG_ON(pud_none(*pud) || pud_bad(*pud));
  538. return pmd_offset_kimg(pud, addr);
  539. }
  540. static inline pte_t * fixmap_pte(unsigned long addr)
  541. {
  542. return &bm_pte[pte_index(addr)];
  543. }
  544. void __init early_fixmap_init(void)
  545. {
  546. pgd_t *pgd;
  547. pud_t *pud;
  548. pmd_t *pmd;
  549. unsigned long addr = FIXADDR_START;
  550. pgd = pgd_offset_k(addr);
  551. if (CONFIG_PGTABLE_LEVELS > 3 &&
  552. !(pgd_none(*pgd) || pgd_page_paddr(*pgd) == __pa(bm_pud))) {
  553. /*
  554. * We only end up here if the kernel mapping and the fixmap
  555. * share the top level pgd entry, which should only happen on
  556. * 16k/4 levels configurations.
  557. */
  558. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  559. pud = pud_offset_kimg(pgd, addr);
  560. } else {
  561. pgd_populate(&init_mm, pgd, bm_pud);
  562. pud = fixmap_pud(addr);
  563. }
  564. pud_populate(&init_mm, pud, bm_pmd);
  565. pmd = fixmap_pmd(addr);
  566. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  567. /*
  568. * The boot-ioremap range spans multiple pmds, for which
  569. * we are not prepared:
  570. */
  571. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  572. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  573. if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
  574. || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  575. WARN_ON(1);
  576. pr_warn("pmd %p != %p, %p\n",
  577. pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
  578. fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
  579. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  580. fix_to_virt(FIX_BTMAP_BEGIN));
  581. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  582. fix_to_virt(FIX_BTMAP_END));
  583. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  584. pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
  585. }
  586. }
  587. void __set_fixmap(enum fixed_addresses idx,
  588. phys_addr_t phys, pgprot_t flags)
  589. {
  590. unsigned long addr = __fix_to_virt(idx);
  591. pte_t *pte;
  592. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  593. pte = fixmap_pte(addr);
  594. if (pgprot_val(flags)) {
  595. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  596. } else {
  597. pte_clear(&init_mm, addr, pte);
  598. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  599. }
  600. }
  601. void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
  602. {
  603. const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
  604. int offset;
  605. void *dt_virt;
  606. /*
  607. * Check whether the physical FDT address is set and meets the minimum
  608. * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
  609. * at least 8 bytes so that we can always access the magic and size
  610. * fields of the FDT header after mapping the first chunk, double check
  611. * here if that is indeed the case.
  612. */
  613. BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
  614. if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
  615. return NULL;
  616. /*
  617. * Make sure that the FDT region can be mapped without the need to
  618. * allocate additional translation table pages, so that it is safe
  619. * to call create_mapping_noalloc() this early.
  620. *
  621. * On 64k pages, the FDT will be mapped using PTEs, so we need to
  622. * be in the same PMD as the rest of the fixmap.
  623. * On 4k pages, we'll use section mappings for the FDT so we only
  624. * have to be in the same PUD.
  625. */
  626. BUILD_BUG_ON(dt_virt_base % SZ_2M);
  627. BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
  628. __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
  629. offset = dt_phys % SWAPPER_BLOCK_SIZE;
  630. dt_virt = (void *)dt_virt_base + offset;
  631. /* map the first chunk so we can read the size from the header */
  632. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
  633. dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
  634. if (fdt_magic(dt_virt) != FDT_MAGIC)
  635. return NULL;
  636. *size = fdt_totalsize(dt_virt);
  637. if (*size > MAX_FDT_SIZE)
  638. return NULL;
  639. if (offset + *size > SWAPPER_BLOCK_SIZE)
  640. create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
  641. round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
  642. return dt_virt;
  643. }
  644. void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
  645. {
  646. void *dt_virt;
  647. int size;
  648. dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
  649. if (!dt_virt)
  650. return NULL;
  651. memblock_reserve(dt_phys, size);
  652. return dt_virt;
  653. }
  654. int __init arch_ioremap_pud_supported(void)
  655. {
  656. /* only 4k granule supports level 1 block mappings */
  657. return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
  658. }
  659. int __init arch_ioremap_pmd_supported(void)
  660. {
  661. return 1;
  662. }
  663. int pud_set_huge(pud_t *pud, phys_addr_t phys, pgprot_t prot)
  664. {
  665. BUG_ON(phys & ~PUD_MASK);
  666. set_pud(pud, __pud(phys | PUD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
  667. return 1;
  668. }
  669. int pmd_set_huge(pmd_t *pmd, phys_addr_t phys, pgprot_t prot)
  670. {
  671. BUG_ON(phys & ~PMD_MASK);
  672. set_pmd(pmd, __pmd(phys | PMD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
  673. return 1;
  674. }
  675. int pud_clear_huge(pud_t *pud)
  676. {
  677. if (!pud_sect(*pud))
  678. return 0;
  679. pud_clear(pud);
  680. return 1;
  681. }
  682. int pmd_clear_huge(pmd_t *pmd)
  683. {
  684. if (!pmd_sect(*pmd))
  685. return 0;
  686. pmd_clear(pmd);
  687. return 1;
  688. }