1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef __KVM_HYP_MEMORY_H 3 #define __KVM_HYP_MEMORY_H 4 5 #include <asm/kvm_mmu.h> 6 #include <asm/page.h> 7 8 #include <linux/types.h> 9 10 /* 11 * Accesses to struct hyp_page flags must be serialized by the host stage-2 12 * page-table lock due to the lack of atomics at EL2. 13 */ 14 #define HOST_PAGE_NEED_POISONING BIT(0) 15 #define HOST_PAGE_PENDING_RECLAIM BIT(1) 16 17 struct hyp_page { 18 unsigned short refcount; 19 u8 order; 20 u8 flags; 21 }; 22 23 extern s64 hyp_physvirt_offset; 24 extern u64 __hyp_vmemmap; 25 #define hyp_vmemmap ((struct hyp_page *)__hyp_vmemmap) 26 27 #define __hyp_va(phys) ((void *)((phys_addr_t)(phys) - hyp_physvirt_offset)) 28 hyp_phys_to_virt(phys_addr_t phys)29static inline void *hyp_phys_to_virt(phys_addr_t phys) 30 { 31 return __hyp_va(phys); 32 } 33 hyp_virt_to_phys(void * addr)34static inline phys_addr_t hyp_virt_to_phys(void *addr) 35 { 36 return __hyp_pa(addr); 37 } 38 39 #define hyp_phys_to_pfn(phys) ((phys) >> PAGE_SHIFT) 40 #define hyp_pfn_to_phys(pfn) ((phys_addr_t)((pfn) << PAGE_SHIFT)) 41 #define hyp_phys_to_page(phys) (&hyp_vmemmap[hyp_phys_to_pfn(phys)]) 42 #define hyp_virt_to_page(virt) hyp_phys_to_page(__hyp_pa(virt)) 43 #define hyp_virt_to_pfn(virt) hyp_phys_to_pfn(__hyp_pa(virt)) 44 45 #define hyp_page_to_pfn(page) ((struct hyp_page *)(page) - hyp_vmemmap) 46 #define hyp_page_to_phys(page) hyp_pfn_to_phys((hyp_page_to_pfn(page))) 47 #define hyp_page_to_virt(page) __hyp_va(hyp_page_to_phys(page)) 48 #define hyp_page_to_pool(page) (((struct hyp_page *)page)->pool) 49 hyp_page_count(void * addr)50static inline int hyp_page_count(void *addr) 51 { 52 struct hyp_page *p = hyp_virt_to_page(addr); 53 54 return p->refcount; 55 } 56 hyp_page_ref_inc(struct hyp_page * p)57static inline void hyp_page_ref_inc(struct hyp_page *p) 58 { 59 BUG_ON(p->refcount == USHRT_MAX); 60 p->refcount++; 61 } 62 hyp_page_ref_dec(struct hyp_page * p)63static inline void hyp_page_ref_dec(struct hyp_page *p) 64 { 65 BUG_ON(!p->refcount); 66 p->refcount--; 67 } 68 hyp_page_ref_dec_and_test(struct hyp_page * p)69static inline int hyp_page_ref_dec_and_test(struct hyp_page *p) 70 { 71 hyp_page_ref_dec(p); 72 return (p->refcount == 0); 73 } 74 hyp_set_page_refcounted(struct hyp_page * p)75static inline void hyp_set_page_refcounted(struct hyp_page *p) 76 { 77 BUG_ON(p->refcount); 78 p->refcount = 1; 79 } 80 #endif /* __KVM_HYP_MEMORY_H */ 81