From e9e9cffee93a62cc4c185e257483307a2aed25a5 Mon Sep 17 00:00:00 2001 From: Quentin Perret Date: Thu, 21 Oct 2021 10:19:05 +0100 Subject: [PATCH] ANDROID: KVM: arm64: Add hyp_memcache helpers for the kernel Introduce helper functions easing the manipulation of a hyp_memcache from the kernel. Signed-off-by: Quentin Perret Bug: 209580772 Change-Id: I42f2718e059f0cb32b7007e390ba758ba68ddd0c Signed-off-by: Will Deacon --- arch/arm64/kvm/mmu.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c92ca3affb2b..4bee458fd198 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -776,6 +776,33 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) } } +static void hyp_mc_free_fn(void *addr, void *unused) +{ + free_page((unsigned long)addr); +} + +static void *hyp_mc_alloc_fn(void *unused) +{ + return (void *)__get_free_page(GFP_KERNEL_ACCOUNT); +} + +void free_hyp_memcache(struct kvm_hyp_memcache *mc) +{ + if (is_protected_kvm_enabled()) + __free_hyp_memcache(mc, hyp_mc_free_fn, + kvm_host_va, NULL); +} + +static int topup_hyp_memcache(struct kvm_hyp_memcache *mc, + unsigned long min_pages) +{ + if (!is_protected_kvm_enabled()) + return 0; + + return __topup_hyp_memcache(mc, min_pages, hyp_mc_alloc_fn, + kvm_host_pa, NULL); +} + /** * kvm_phys_addr_ioremap - map a device range to guest IPA *