• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_IOMMU_H
3 #define __KVM_IOMMU_H
4 
5 #include <asm/kvm_host.h>
6 #include <kvm/power_domain.h>
7 #include <linux/io-pgtable.h>
8 
9 /*
10  * Domain ID for identity mapped domain that the host can attach
11  * to get the same mapping available to the CPU page table.
12  */
13 #define KVM_IOMMU_DOMAIN_IDMAP_ID		0
14 
15 /* Used in alloc_domain type argument. */
16 #define KVM_IOMMU_DOMAIN_IDMAP_TYPE		0
17 /* Typically used for guests, as they don't know IOMMU topology. */
18 #define KVM_IOMMU_DOMAIN_ANY_TYPE		1
19 
20 #define KVM_IOMMU_DOMAIN_NR_START		(KVM_IOMMU_DOMAIN_IDMAP_ID + 1)
21 
22 struct kvm_hyp_iommu_domain {
23 	atomic_t		refs;
24 	pkvm_handle_t		domain_id;
25 	void			*priv;
26 	void			*vm;
27 	ANDROID_KABI_RESERVE(1);
28 	ANDROID_KABI_RESERVE(2);
29 };
30 
31 extern void **kvm_nvhe_sym(kvm_hyp_iommu_domains);
32 #define kvm_hyp_iommu_domains kvm_nvhe_sym(kvm_hyp_iommu_domains)
33 
34 /*
35  * At the moment the number of domains is limited to 2^16
36  * In practice we're rarely going to need a lot of domains. To avoid allocating
37  * a large domain table, we use a two-level table, indexed by domain ID. With
38  * 4kB pages and 16-bytes domains, the leaf table contains 256 domains, and the
39  * root table 256 pointers. With 64kB pages, the leaf table contains 4096
40  * domains and the root table 16 pointers. In this case, or when using 8-bit
41  * VMIDs, it may be more advantageous to use a single level. But using two
42  * levels allows to easily extend the domain size.
43  */
44 #define KVM_IOMMU_MAX_DOMAINS	(1 << 16)
45 
46 /* Number of entries in the level-2 domain table */
47 #define KVM_IOMMU_DOMAINS_PER_PAGE \
48 	(PAGE_SIZE / sizeof(struct kvm_hyp_iommu_domain))
49 
50 /* Number of entries in the root domain table */
51 #define KVM_IOMMU_DOMAINS_ROOT_ENTRIES \
52 	(KVM_IOMMU_MAX_DOMAINS / KVM_IOMMU_DOMAINS_PER_PAGE)
53 
54 #define KVM_IOMMU_DOMAINS_ROOT_SIZE \
55 	(KVM_IOMMU_DOMAINS_ROOT_ENTRIES * sizeof(void *))
56 
57 #define KVM_IOMMU_DOMAINS_ROOT_ORDER_NR	\
58 	(1 << get_order(KVM_IOMMU_DOMAINS_ROOT_SIZE))
59 
60 struct kvm_hyp_iommu {
61 	u32				lock;   /* lock size verified in kvm_iommu_get_lock.  */
62 	struct kvm_power_domain		power_domain;
63 	bool				power_is_off;
64 	ANDROID_KABI_RESERVE(1);
65 	ANDROID_KABI_RESERVE(2);
66 	ANDROID_KABI_RESERVE(3);
67 	ANDROID_KABI_RESERVE(4);
68 };
69 
70 #endif /* __KVM_IOMMU_H */
71