• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 u64 __hyp_vmemmap;
24 #define hyp_vmemmap ((struct hyp_page *)__hyp_vmemmap)
25 
26 #define __hyp_va(phys)	((void *)((phys_addr_t)(phys) - hyp_physvirt_offset))
27 
hyp_phys_to_virt(phys_addr_t phys)28 static inline void *hyp_phys_to_virt(phys_addr_t phys)
29 {
30 	return __hyp_va(phys);
31 }
32 
hyp_virt_to_phys(void * addr)33 static inline phys_addr_t hyp_virt_to_phys(void *addr)
34 {
35 	return __hyp_pa(addr);
36 }
37 
38 #define hyp_phys_to_pfn(phys)	((phys) >> PAGE_SHIFT)
39 #define hyp_pfn_to_phys(pfn)	((phys_addr_t)((pfn) << PAGE_SHIFT))
40 #define hyp_phys_to_page(phys)	(&hyp_vmemmap[hyp_phys_to_pfn(phys)])
41 #define hyp_virt_to_page(virt)	hyp_phys_to_page(__hyp_pa(virt))
42 #define hyp_virt_to_pfn(virt)	hyp_phys_to_pfn(__hyp_pa(virt))
43 
44 #define hyp_page_to_pfn(page)	((struct hyp_page *)(page) - hyp_vmemmap)
45 #define hyp_page_to_phys(page)  hyp_pfn_to_phys((hyp_page_to_pfn(page)))
46 #define hyp_page_to_virt(page)	__hyp_va(hyp_page_to_phys(page))
47 #define hyp_page_to_pool(page)	(((struct hyp_page *)page)->pool)
48 
hyp_page_count(void * addr)49 static inline int hyp_page_count(void *addr)
50 {
51 	struct hyp_page *p = hyp_virt_to_page(addr);
52 
53 	return p->refcount;
54 }
55 
hyp_page_ref_inc(struct hyp_page * p)56 static inline void hyp_page_ref_inc(struct hyp_page *p)
57 {
58 	BUG_ON(p->refcount == USHRT_MAX);
59 	p->refcount++;
60 }
61 
hyp_page_ref_dec(struct hyp_page * p)62 static inline void hyp_page_ref_dec(struct hyp_page *p)
63 {
64 	BUG_ON(!p->refcount);
65 	p->refcount--;
66 }
67 
hyp_page_ref_dec_and_test(struct hyp_page * p)68 static inline int hyp_page_ref_dec_and_test(struct hyp_page *p)
69 {
70 	hyp_page_ref_dec(p);
71 	return (p->refcount == 0);
72 }
73 
hyp_set_page_refcounted(struct hyp_page * p)74 static inline void hyp_set_page_refcounted(struct hyp_page *p)
75 {
76 	BUG_ON(p->refcount);
77 	p->refcount = 1;
78 }
79 #endif /* __KVM_HYP_MEMORY_H */
80