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 struct hyp_pool;
11 struct hyp_page {
12 	unsigned int refcount;
13 	unsigned int order;
14 	struct hyp_pool *pool;
15 	struct list_head node;
16 };
17 
18 extern u64 __hyp_vmemmap;
19 #define hyp_vmemmap ((struct hyp_page *)__hyp_vmemmap)
20 
21 #define __hyp_va(phys)	((void *)((phys_addr_t)(phys) - hyp_physvirt_offset))
22 
23 static inline void *hyp_phys_to_virt(phys_addr_t phys)
24 {
25 	return __hyp_va(phys);
26 }
27 
28 static inline phys_addr_t hyp_virt_to_phys(void *addr)
29 {
30 	return __hyp_pa(addr);
31 }
32 
33 #define hyp_phys_to_pfn(phys)	((phys) >> PAGE_SHIFT)
34 #define hyp_pfn_to_phys(pfn)	((phys_addr_t)((pfn) << PAGE_SHIFT))
35 #define hyp_phys_to_page(phys)	(&hyp_vmemmap[hyp_phys_to_pfn(phys)])
36 #define hyp_virt_to_page(virt)	hyp_phys_to_page(__hyp_pa(virt))
37 #define hyp_virt_to_pfn(virt)	hyp_phys_to_pfn(__hyp_pa(virt))
38 
39 #define hyp_page_to_pfn(page)	((struct hyp_page *)(page) - hyp_vmemmap)
40 #define hyp_page_to_phys(page)  hyp_pfn_to_phys((hyp_page_to_pfn(page)))
41 #define hyp_page_to_virt(page)	__hyp_va(hyp_page_to_phys(page))
42 #define hyp_page_to_pool(page)	(((struct hyp_page *)page)->pool)
43 
44 static inline int hyp_page_count(void *addr)
45 {
46 	struct hyp_page *p = hyp_virt_to_page(addr);
47 
48 	return p->refcount;
49 }
50 
51 #endif /* __KVM_HYP_MEMORY_H */
52