xref: /openbmc/linux/arch/arm64/include/asm/kvm_mmu.h (revision e4c5a6851058386c9e109ad529717a23173918bc)
137c43753SMarc Zyngier /*
237c43753SMarc Zyngier  * Copyright (C) 2012,2013 - ARM Ltd
337c43753SMarc Zyngier  * Author: Marc Zyngier <marc.zyngier@arm.com>
437c43753SMarc Zyngier  *
537c43753SMarc Zyngier  * This program is free software; you can redistribute it and/or modify
637c43753SMarc Zyngier  * it under the terms of the GNU General Public License version 2 as
737c43753SMarc Zyngier  * published by the Free Software Foundation.
837c43753SMarc Zyngier  *
937c43753SMarc Zyngier  * This program is distributed in the hope that it will be useful,
1037c43753SMarc Zyngier  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1137c43753SMarc Zyngier  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1237c43753SMarc Zyngier  * GNU General Public License for more details.
1337c43753SMarc Zyngier  *
1437c43753SMarc Zyngier  * You should have received a copy of the GNU General Public License
1537c43753SMarc Zyngier  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
1637c43753SMarc Zyngier  */
1737c43753SMarc Zyngier 
1837c43753SMarc Zyngier #ifndef __ARM64_KVM_MMU_H__
1937c43753SMarc Zyngier #define __ARM64_KVM_MMU_H__
2037c43753SMarc Zyngier 
2137c43753SMarc Zyngier #include <asm/page.h>
2237c43753SMarc Zyngier #include <asm/memory.h>
2337c43753SMarc Zyngier 
2437c43753SMarc Zyngier /*
2537c43753SMarc Zyngier  * As we only have the TTBR0_EL2 register, we cannot express
2637c43753SMarc Zyngier  * "negative" addresses. This makes it impossible to directly share
2737c43753SMarc Zyngier  * mappings with the kernel.
2837c43753SMarc Zyngier  *
2937c43753SMarc Zyngier  * Instead, give the HYP mode its own VA region at a fixed offset from
3037c43753SMarc Zyngier  * the kernel by just masking the top bits (which are all ones for a
3137c43753SMarc Zyngier  * kernel address).
3237c43753SMarc Zyngier  */
3337c43753SMarc Zyngier #define HYP_PAGE_OFFSET_SHIFT	VA_BITS
3437c43753SMarc Zyngier #define HYP_PAGE_OFFSET_MASK	((UL(1) << HYP_PAGE_OFFSET_SHIFT) - 1)
3537c43753SMarc Zyngier #define HYP_PAGE_OFFSET		(PAGE_OFFSET & HYP_PAGE_OFFSET_MASK)
3637c43753SMarc Zyngier 
3737c43753SMarc Zyngier /*
3837c43753SMarc Zyngier  * Our virtual mapping for the idmap-ed MMU-enable code. Must be
3937c43753SMarc Zyngier  * shared across all the page-tables. Conveniently, we use the last
4037c43753SMarc Zyngier  * possible page, where no kernel mapping will ever exist.
4137c43753SMarc Zyngier  */
4237c43753SMarc Zyngier #define TRAMPOLINE_VA		(HYP_PAGE_OFFSET_MASK & PAGE_MASK)
4337c43753SMarc Zyngier 
4438f791a4SChristoffer Dall /*
4538f791a4SChristoffer Dall  * KVM_MMU_CACHE_MIN_PAGES is the number of stage2 page table translation
4638f791a4SChristoffer Dall  * levels in addition to the PGD and potentially the PUD which are
4738f791a4SChristoffer Dall  * pre-allocated (we pre-allocate the fake PGD and the PUD when the Stage-2
4838f791a4SChristoffer Dall  * tables use one level of tables less than the kernel.
4938f791a4SChristoffer Dall  */
5038f791a4SChristoffer Dall #ifdef CONFIG_ARM64_64K_PAGES
5138f791a4SChristoffer Dall #define KVM_MMU_CACHE_MIN_PAGES	1
5238f791a4SChristoffer Dall #else
5338f791a4SChristoffer Dall #define KVM_MMU_CACHE_MIN_PAGES	2
5438f791a4SChristoffer Dall #endif
5538f791a4SChristoffer Dall 
5637c43753SMarc Zyngier #ifdef __ASSEMBLY__
5737c43753SMarc Zyngier 
5837c43753SMarc Zyngier /*
5937c43753SMarc Zyngier  * Convert a kernel VA into a HYP VA.
6037c43753SMarc Zyngier  * reg: VA to be converted.
6137c43753SMarc Zyngier  */
6237c43753SMarc Zyngier .macro kern_hyp_va	reg
6337c43753SMarc Zyngier 	and	\reg, \reg, #HYP_PAGE_OFFSET_MASK
6437c43753SMarc Zyngier .endm
6537c43753SMarc Zyngier 
6637c43753SMarc Zyngier #else
6737c43753SMarc Zyngier 
6838f791a4SChristoffer Dall #include <asm/pgalloc.h>
6937c43753SMarc Zyngier #include <asm/cachetype.h>
7037c43753SMarc Zyngier #include <asm/cacheflush.h>
71*e4c5a685SArd Biesheuvel #include <asm/mmu_context.h>
72*e4c5a685SArd Biesheuvel #include <asm/pgtable.h>
7337c43753SMarc Zyngier 
7437c43753SMarc Zyngier #define KERN_TO_HYP(kva)	((unsigned long)kva - PAGE_OFFSET + HYP_PAGE_OFFSET)
7537c43753SMarc Zyngier 
7637c43753SMarc Zyngier /*
77dbff124eSJoel Schopp  * We currently only support a 40bit IPA.
7837c43753SMarc Zyngier  */
79dbff124eSJoel Schopp #define KVM_PHYS_SHIFT	(40)
8037c43753SMarc Zyngier #define KVM_PHYS_SIZE	(1UL << KVM_PHYS_SHIFT)
8137c43753SMarc Zyngier #define KVM_PHYS_MASK	(KVM_PHYS_SIZE - 1UL)
8237c43753SMarc Zyngier 
8337c43753SMarc Zyngier int create_hyp_mappings(void *from, void *to);
8437c43753SMarc Zyngier int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
8537c43753SMarc Zyngier void free_boot_hyp_pgd(void);
8637c43753SMarc Zyngier void free_hyp_pgds(void);
8737c43753SMarc Zyngier 
88957db105SChristoffer Dall void stage2_unmap_vm(struct kvm *kvm);
8937c43753SMarc Zyngier int kvm_alloc_stage2_pgd(struct kvm *kvm);
9037c43753SMarc Zyngier void kvm_free_stage2_pgd(struct kvm *kvm);
9137c43753SMarc Zyngier int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
92c40f2f8fSArd Biesheuvel 			  phys_addr_t pa, unsigned long size, bool writable);
9337c43753SMarc Zyngier 
9437c43753SMarc Zyngier int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
9537c43753SMarc Zyngier 
9637c43753SMarc Zyngier void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
9737c43753SMarc Zyngier 
9837c43753SMarc Zyngier phys_addr_t kvm_mmu_get_httbr(void);
9937c43753SMarc Zyngier phys_addr_t kvm_mmu_get_boot_httbr(void);
10037c43753SMarc Zyngier phys_addr_t kvm_get_idmap_vector(void);
10137c43753SMarc Zyngier int kvm_mmu_init(void);
10237c43753SMarc Zyngier void kvm_clear_hyp_idmap(void);
10337c43753SMarc Zyngier 
10437c43753SMarc Zyngier #define	kvm_set_pte(ptep, pte)		set_pte(ptep, pte)
105ad361f09SChristoffer Dall #define	kvm_set_pmd(pmdp, pmd)		set_pmd(pmdp, pmd)
10637c43753SMarc Zyngier 
10737c43753SMarc Zyngier static inline void kvm_clean_pgd(pgd_t *pgd) {}
10838f791a4SChristoffer Dall static inline void kvm_clean_pmd(pmd_t *pmd) {}
10937c43753SMarc Zyngier static inline void kvm_clean_pmd_entry(pmd_t *pmd) {}
11037c43753SMarc Zyngier static inline void kvm_clean_pte(pte_t *pte) {}
11137c43753SMarc Zyngier static inline void kvm_clean_pte_entry(pte_t *pte) {}
11237c43753SMarc Zyngier 
11337c43753SMarc Zyngier static inline void kvm_set_s2pte_writable(pte_t *pte)
11437c43753SMarc Zyngier {
11537c43753SMarc Zyngier 	pte_val(*pte) |= PTE_S2_RDWR;
11637c43753SMarc Zyngier }
11737c43753SMarc Zyngier 
118ad361f09SChristoffer Dall static inline void kvm_set_s2pmd_writable(pmd_t *pmd)
119ad361f09SChristoffer Dall {
120ad361f09SChristoffer Dall 	pmd_val(*pmd) |= PMD_S2_RDWR;
121ad361f09SChristoffer Dall }
122ad361f09SChristoffer Dall 
1238199ed0eSMario Smarduch static inline void kvm_set_s2pte_readonly(pte_t *pte)
1248199ed0eSMario Smarduch {
1258199ed0eSMario Smarduch 	pte_val(*pte) = (pte_val(*pte) & ~PTE_S2_RDWR) | PTE_S2_RDONLY;
1268199ed0eSMario Smarduch }
1278199ed0eSMario Smarduch 
1288199ed0eSMario Smarduch static inline bool kvm_s2pte_readonly(pte_t *pte)
1298199ed0eSMario Smarduch {
1308199ed0eSMario Smarduch 	return (pte_val(*pte) & PTE_S2_RDWR) == PTE_S2_RDONLY;
1318199ed0eSMario Smarduch }
1328199ed0eSMario Smarduch 
1338199ed0eSMario Smarduch static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
1348199ed0eSMario Smarduch {
1358199ed0eSMario Smarduch 	pmd_val(*pmd) = (pmd_val(*pmd) & ~PMD_S2_RDWR) | PMD_S2_RDONLY;
1368199ed0eSMario Smarduch }
1378199ed0eSMario Smarduch 
1388199ed0eSMario Smarduch static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
1398199ed0eSMario Smarduch {
1408199ed0eSMario Smarduch 	return (pmd_val(*pmd) & PMD_S2_RDWR) == PMD_S2_RDONLY;
1418199ed0eSMario Smarduch }
1428199ed0eSMario Smarduch 
1438199ed0eSMario Smarduch 
144a3c8bd31SMarc Zyngier #define kvm_pgd_addr_end(addr, end)	pgd_addr_end(addr, end)
145a3c8bd31SMarc Zyngier #define kvm_pud_addr_end(addr, end)	pud_addr_end(addr, end)
146a3c8bd31SMarc Zyngier #define kvm_pmd_addr_end(addr, end)	pmd_addr_end(addr, end)
147a3c8bd31SMarc Zyngier 
14838f791a4SChristoffer Dall /*
14938f791a4SChristoffer Dall  * In the case where PGDIR_SHIFT is larger than KVM_PHYS_SHIFT, we can address
15038f791a4SChristoffer Dall  * the entire IPA input range with a single pgd entry, and we would only need
15138f791a4SChristoffer Dall  * one pgd entry.  Note that in this case, the pgd is actually not used by
15238f791a4SChristoffer Dall  * the MMU for Stage-2 translations, but is merely a fake pgd used as a data
15338f791a4SChristoffer Dall  * structure for the kernel pgtable macros to work.
15438f791a4SChristoffer Dall  */
15538f791a4SChristoffer Dall #if PGDIR_SHIFT > KVM_PHYS_SHIFT
15638f791a4SChristoffer Dall #define PTRS_PER_S2_PGD_SHIFT	0
15738f791a4SChristoffer Dall #else
15838f791a4SChristoffer Dall #define PTRS_PER_S2_PGD_SHIFT	(KVM_PHYS_SHIFT - PGDIR_SHIFT)
15938f791a4SChristoffer Dall #endif
16038f791a4SChristoffer Dall #define PTRS_PER_S2_PGD		(1 << PTRS_PER_S2_PGD_SHIFT)
16138f791a4SChristoffer Dall #define S2_PGD_ORDER		get_order(PTRS_PER_S2_PGD * sizeof(pgd_t))
16238f791a4SChristoffer Dall 
16338f791a4SChristoffer Dall /*
16438f791a4SChristoffer Dall  * If we are concatenating first level stage-2 page tables, we would have less
16538f791a4SChristoffer Dall  * than or equal to 16 pointers in the fake PGD, because that's what the
16638f791a4SChristoffer Dall  * architecture allows.  In this case, (4 - CONFIG_ARM64_PGTABLE_LEVELS)
16738f791a4SChristoffer Dall  * represents the first level for the host, and we add 1 to go to the next
16838f791a4SChristoffer Dall  * level (which uses contatenation) for the stage-2 tables.
16938f791a4SChristoffer Dall  */
17038f791a4SChristoffer Dall #if PTRS_PER_S2_PGD <= 16
17138f791a4SChristoffer Dall #define KVM_PREALLOC_LEVEL	(4 - CONFIG_ARM64_PGTABLE_LEVELS + 1)
17238f791a4SChristoffer Dall #else
17338f791a4SChristoffer Dall #define KVM_PREALLOC_LEVEL	(0)
17438f791a4SChristoffer Dall #endif
17538f791a4SChristoffer Dall 
17638f791a4SChristoffer Dall /**
17738f791a4SChristoffer Dall  * kvm_prealloc_hwpgd - allocate inital table for VTTBR
17838f791a4SChristoffer Dall  * @kvm:	The KVM struct pointer for the VM.
17938f791a4SChristoffer Dall  * @pgd:	The kernel pseudo pgd
18038f791a4SChristoffer Dall  *
18138f791a4SChristoffer Dall  * When the kernel uses more levels of page tables than the guest, we allocate
18238f791a4SChristoffer Dall  * a fake PGD and pre-populate it to point to the next-level page table, which
18338f791a4SChristoffer Dall  * will be the real initial page table pointed to by the VTTBR.
18438f791a4SChristoffer Dall  *
18538f791a4SChristoffer Dall  * When KVM_PREALLOC_LEVEL==2, we allocate a single page for the PMD and
18638f791a4SChristoffer Dall  * the kernel will use folded pud.  When KVM_PREALLOC_LEVEL==1, we
18738f791a4SChristoffer Dall  * allocate 2 consecutive PUD pages.
18838f791a4SChristoffer Dall  */
18938f791a4SChristoffer Dall static inline int kvm_prealloc_hwpgd(struct kvm *kvm, pgd_t *pgd)
19038f791a4SChristoffer Dall {
19138f791a4SChristoffer Dall 	unsigned int i;
19238f791a4SChristoffer Dall 	unsigned long hwpgd;
19338f791a4SChristoffer Dall 
19438f791a4SChristoffer Dall 	if (KVM_PREALLOC_LEVEL == 0)
19538f791a4SChristoffer Dall 		return 0;
19638f791a4SChristoffer Dall 
19738f791a4SChristoffer Dall 	hwpgd = __get_free_pages(GFP_KERNEL | __GFP_ZERO, PTRS_PER_S2_PGD_SHIFT);
19838f791a4SChristoffer Dall 	if (!hwpgd)
19938f791a4SChristoffer Dall 		return -ENOMEM;
20038f791a4SChristoffer Dall 
20138f791a4SChristoffer Dall 	for (i = 0; i < PTRS_PER_S2_PGD; i++) {
20238f791a4SChristoffer Dall 		if (KVM_PREALLOC_LEVEL == 1)
20338f791a4SChristoffer Dall 			pgd_populate(NULL, pgd + i,
20438f791a4SChristoffer Dall 				     (pud_t *)hwpgd + i * PTRS_PER_PUD);
20538f791a4SChristoffer Dall 		else if (KVM_PREALLOC_LEVEL == 2)
20638f791a4SChristoffer Dall 			pud_populate(NULL, pud_offset(pgd, 0) + i,
20738f791a4SChristoffer Dall 				     (pmd_t *)hwpgd + i * PTRS_PER_PMD);
20838f791a4SChristoffer Dall 	}
20938f791a4SChristoffer Dall 
21038f791a4SChristoffer Dall 	return 0;
21138f791a4SChristoffer Dall }
21238f791a4SChristoffer Dall 
21338f791a4SChristoffer Dall static inline void *kvm_get_hwpgd(struct kvm *kvm)
21438f791a4SChristoffer Dall {
21538f791a4SChristoffer Dall 	pgd_t *pgd = kvm->arch.pgd;
21638f791a4SChristoffer Dall 	pud_t *pud;
21738f791a4SChristoffer Dall 
21838f791a4SChristoffer Dall 	if (KVM_PREALLOC_LEVEL == 0)
21938f791a4SChristoffer Dall 		return pgd;
22038f791a4SChristoffer Dall 
22138f791a4SChristoffer Dall 	pud = pud_offset(pgd, 0);
22238f791a4SChristoffer Dall 	if (KVM_PREALLOC_LEVEL == 1)
22338f791a4SChristoffer Dall 		return pud;
22438f791a4SChristoffer Dall 
22538f791a4SChristoffer Dall 	BUG_ON(KVM_PREALLOC_LEVEL != 2);
22638f791a4SChristoffer Dall 	return pmd_offset(pud, 0);
22738f791a4SChristoffer Dall }
22838f791a4SChristoffer Dall 
22938f791a4SChristoffer Dall static inline void kvm_free_hwpgd(struct kvm *kvm)
23038f791a4SChristoffer Dall {
23138f791a4SChristoffer Dall 	if (KVM_PREALLOC_LEVEL > 0) {
23238f791a4SChristoffer Dall 		unsigned long hwpgd = (unsigned long)kvm_get_hwpgd(kvm);
23338f791a4SChristoffer Dall 		free_pages(hwpgd, PTRS_PER_S2_PGD_SHIFT);
23438f791a4SChristoffer Dall 	}
23538f791a4SChristoffer Dall }
23638f791a4SChristoffer Dall 
2374f853a71SChristoffer Dall static inline bool kvm_page_empty(void *ptr)
2384f853a71SChristoffer Dall {
2394f853a71SChristoffer Dall 	struct page *ptr_page = virt_to_page(ptr);
2404f853a71SChristoffer Dall 	return page_count(ptr_page) == 1;
2414f853a71SChristoffer Dall }
2424f853a71SChristoffer Dall 
24338f791a4SChristoffer Dall #define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
24438f791a4SChristoffer Dall 
24538f791a4SChristoffer Dall #ifdef __PAGETABLE_PMD_FOLDED
24638f791a4SChristoffer Dall #define kvm_pmd_table_empty(kvm, pmdp) (0)
2474f853a71SChristoffer Dall #else
24838f791a4SChristoffer Dall #define kvm_pmd_table_empty(kvm, pmdp) \
24938f791a4SChristoffer Dall 	(kvm_page_empty(pmdp) && (!(kvm) || KVM_PREALLOC_LEVEL < 2))
2504f853a71SChristoffer Dall #endif
25138f791a4SChristoffer Dall 
25238f791a4SChristoffer Dall #ifdef __PAGETABLE_PUD_FOLDED
25338f791a4SChristoffer Dall #define kvm_pud_table_empty(kvm, pudp) (0)
25438f791a4SChristoffer Dall #else
25538f791a4SChristoffer Dall #define kvm_pud_table_empty(kvm, pudp) \
25638f791a4SChristoffer Dall 	(kvm_page_empty(pudp) && (!(kvm) || KVM_PREALLOC_LEVEL < 1))
25738f791a4SChristoffer Dall #endif
2584f853a71SChristoffer Dall 
2594f853a71SChristoffer Dall 
26037c43753SMarc Zyngier struct kvm;
26137c43753SMarc Zyngier 
2622d58b733SMarc Zyngier #define kvm_flush_dcache_to_poc(a,l)	__flush_dcache_area((a), (l))
2632d58b733SMarc Zyngier 
2642d58b733SMarc Zyngier static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
2652d58b733SMarc Zyngier {
2662d58b733SMarc Zyngier 	return (vcpu_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
2672d58b733SMarc Zyngier }
2682d58b733SMarc Zyngier 
2690d3e4d4fSMarc Zyngier static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu, pfn_t pfn,
270840f4bfbSLaszlo Ersek 					       unsigned long size,
271840f4bfbSLaszlo Ersek 					       bool ipa_uncached)
27237c43753SMarc Zyngier {
2730d3e4d4fSMarc Zyngier 	void *va = page_address(pfn_to_page(pfn));
2740d3e4d4fSMarc Zyngier 
275840f4bfbSLaszlo Ersek 	if (!vcpu_has_cache_enabled(vcpu) || ipa_uncached)
2760d3e4d4fSMarc Zyngier 		kvm_flush_dcache_to_poc(va, size);
2772d58b733SMarc Zyngier 
27837c43753SMarc Zyngier 	if (!icache_is_aliasing()) {		/* PIPT */
2790d3e4d4fSMarc Zyngier 		flush_icache_range((unsigned long)va,
2800d3e4d4fSMarc Zyngier 				   (unsigned long)va + size);
28137c43753SMarc Zyngier 	} else if (!icache_is_aivivt()) {	/* non ASID-tagged VIVT */
28237c43753SMarc Zyngier 		/* any kind of VIPT cache */
28337c43753SMarc Zyngier 		__flush_icache_all();
28437c43753SMarc Zyngier 	}
28537c43753SMarc Zyngier }
28637c43753SMarc Zyngier 
287363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pte(pte_t pte)
288363ef89fSMarc Zyngier {
289363ef89fSMarc Zyngier 	struct page *page = pte_page(pte);
290363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
291363ef89fSMarc Zyngier }
292363ef89fSMarc Zyngier 
293363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
294363ef89fSMarc Zyngier {
295363ef89fSMarc Zyngier 	struct page *page = pmd_page(pmd);
296363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
297363ef89fSMarc Zyngier }
298363ef89fSMarc Zyngier 
299363ef89fSMarc Zyngier static inline void __kvm_flush_dcache_pud(pud_t pud)
300363ef89fSMarc Zyngier {
301363ef89fSMarc Zyngier 	struct page *page = pud_page(pud);
302363ef89fSMarc Zyngier 	kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
303363ef89fSMarc Zyngier }
304363ef89fSMarc Zyngier 
3054fda342cSSantosh Shilimkar #define kvm_virt_to_phys(x)		__virt_to_phys((unsigned long)(x))
30637c43753SMarc Zyngier 
3073c1e7165SMarc Zyngier void kvm_set_way_flush(struct kvm_vcpu *vcpu);
3083c1e7165SMarc Zyngier void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
3099d218a1fSMarc Zyngier 
310*e4c5a685SArd Biesheuvel static inline bool __kvm_cpu_uses_extended_idmap(void)
311*e4c5a685SArd Biesheuvel {
312*e4c5a685SArd Biesheuvel 	return __cpu_uses_extended_idmap();
313*e4c5a685SArd Biesheuvel }
314*e4c5a685SArd Biesheuvel 
315*e4c5a685SArd Biesheuvel static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
316*e4c5a685SArd Biesheuvel 				       pgd_t *hyp_pgd,
317*e4c5a685SArd Biesheuvel 				       pgd_t *merged_hyp_pgd,
318*e4c5a685SArd Biesheuvel 				       unsigned long hyp_idmap_start)
319*e4c5a685SArd Biesheuvel {
320*e4c5a685SArd Biesheuvel 	int idmap_idx;
321*e4c5a685SArd Biesheuvel 
322*e4c5a685SArd Biesheuvel 	/*
323*e4c5a685SArd Biesheuvel 	 * Use the first entry to access the HYP mappings. It is
324*e4c5a685SArd Biesheuvel 	 * guaranteed to be free, otherwise we wouldn't use an
325*e4c5a685SArd Biesheuvel 	 * extended idmap.
326*e4c5a685SArd Biesheuvel 	 */
327*e4c5a685SArd Biesheuvel 	VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
328*e4c5a685SArd Biesheuvel 	merged_hyp_pgd[0] = __pgd(__pa(hyp_pgd) | PMD_TYPE_TABLE);
329*e4c5a685SArd Biesheuvel 
330*e4c5a685SArd Biesheuvel 	/*
331*e4c5a685SArd Biesheuvel 	 * Create another extended level entry that points to the boot HYP map,
332*e4c5a685SArd Biesheuvel 	 * which contains an ID mapping of the HYP init code. We essentially
333*e4c5a685SArd Biesheuvel 	 * merge the boot and runtime HYP maps by doing so, but they don't
334*e4c5a685SArd Biesheuvel 	 * overlap anyway, so this is fine.
335*e4c5a685SArd Biesheuvel 	 */
336*e4c5a685SArd Biesheuvel 	idmap_idx = hyp_idmap_start >> VA_BITS;
337*e4c5a685SArd Biesheuvel 	VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
338*e4c5a685SArd Biesheuvel 	merged_hyp_pgd[idmap_idx] = __pgd(__pa(boot_hyp_pgd) | PMD_TYPE_TABLE);
339*e4c5a685SArd Biesheuvel }
340*e4c5a685SArd Biesheuvel 
34137c43753SMarc Zyngier #endif /* __ASSEMBLY__ */
34237c43753SMarc Zyngier #endif /* __ARM64_KVM_MMU_H__ */
343