1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES.
3  */
4 #ifndef _UAPI_IOMMUFD_TEST_H
5 #define _UAPI_IOMMUFD_TEST_H
6 
7 #include <linux/iommufd.h>
8 #include <linux/types.h>
9 
10 enum {
11 	IOMMU_TEST_OP_ADD_RESERVED = 1,
12 	IOMMU_TEST_OP_MOCK_DOMAIN,
13 	IOMMU_TEST_OP_MD_CHECK_MAP,
14 	IOMMU_TEST_OP_MD_CHECK_REFS,
15 	IOMMU_TEST_OP_CREATE_ACCESS,
16 	IOMMU_TEST_OP_DESTROY_ACCESS_PAGES,
17 	IOMMU_TEST_OP_ACCESS_PAGES,
18 	IOMMU_TEST_OP_ACCESS_RW,
19 	IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT,
20 	IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE,
21 	IOMMU_TEST_OP_ACCESS_REPLACE_IOAS,
22 	IOMMU_TEST_OP_MOCK_DOMAIN_FLAGS,
23 	IOMMU_TEST_OP_DIRTY,
24 	IOMMU_TEST_OP_MD_CHECK_IOTLB,
25 	IOMMU_TEST_OP_TRIGGER_IOPF,
26 	IOMMU_TEST_OP_DEV_CHECK_CACHE,
27 	IOMMU_TEST_OP_TRIGGER_VEVENT,
28 	IOMMU_TEST_OP_PASID_ATTACH,
29 	IOMMU_TEST_OP_PASID_REPLACE,
30 	IOMMU_TEST_OP_PASID_DETACH,
31 	IOMMU_TEST_OP_PASID_CHECK_HWPT,
32 };
33 
34 enum {
35 	MOCK_APERTURE_START = 1UL << 24,
36 	MOCK_APERTURE_LAST = (1UL << 31) - 1,
37 };
38 
39 enum {
40 	MOCK_FLAGS_ACCESS_WRITE = 1 << 0,
41 	MOCK_FLAGS_ACCESS_SYZ = 1 << 16,
42 };
43 
44 enum {
45 	MOCK_ACCESS_RW_WRITE = 1 << 0,
46 	MOCK_ACCESS_RW_SLOW_PATH = 1 << 2,
47 };
48 
49 enum {
50 	MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES = 1 << 0,
51 };
52 
53 enum {
54 	MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0,
55 	MOCK_FLAGS_DEVICE_HUGE_IOVA = 1 << 1,
56 	MOCK_FLAGS_DEVICE_PASID = 1 << 2,
57 };
58 
59 enum {
60 	MOCK_NESTED_DOMAIN_IOTLB_ID_MAX = 3,
61 	MOCK_NESTED_DOMAIN_IOTLB_NUM = 4,
62 };
63 
64 enum {
65 	MOCK_DEV_CACHE_ID_MAX = 3,
66 	MOCK_DEV_CACHE_NUM = 4,
67 };
68 
69 /* Reserved for special pasid replace test */
70 #define IOMMU_TEST_PASID_RESERVED 1024
71 
72 struct iommu_test_cmd {
73 	__u32 size;
74 	__u32 op;
75 	__u32 id;
76 	__u32 __reserved;
77 	union {
78 		struct {
79 			__aligned_u64 start;
80 			__aligned_u64 length;
81 		} add_reserved;
82 		struct {
83 			__u32 out_stdev_id;
84 			__u32 out_hwpt_id;
85 			/* out_idev_id is the standard iommufd_bind object */
86 			__u32 out_idev_id;
87 		} mock_domain;
88 		struct {
89 			__u32 out_stdev_id;
90 			__u32 out_hwpt_id;
91 			__u32 out_idev_id;
92 			/* Expand mock_domain to set mock device flags */
93 			__u32 dev_flags;
94 		} mock_domain_flags;
95 		struct {
96 			__u32 pt_id;
97 		} mock_domain_replace;
98 		struct {
99 			__aligned_u64 iova;
100 			__aligned_u64 length;
101 			__aligned_u64 uptr;
102 		} check_map;
103 		struct {
104 			__aligned_u64 length;
105 			__aligned_u64 uptr;
106 			__u32 refs;
107 		} check_refs;
108 		struct {
109 			__u32 out_access_fd;
110 			__u32 flags;
111 		} create_access;
112 		struct {
113 			__u32 access_pages_id;
114 		} destroy_access_pages;
115 		struct {
116 			__u32 flags;
117 			__u32 out_access_pages_id;
118 			__aligned_u64 iova;
119 			__aligned_u64 length;
120 			__aligned_u64 uptr;
121 		} access_pages;
122 		struct {
123 			__aligned_u64 iova;
124 			__aligned_u64 length;
125 			__aligned_u64 uptr;
126 			__u32 flags;
127 		} access_rw;
128 		struct {
129 			__u32 limit;
130 		} memory_limit;
131 		struct {
132 			__u32 ioas_id;
133 		} access_replace_ioas;
134 		struct {
135 			__u32 flags;
136 			__aligned_u64 iova;
137 			__aligned_u64 length;
138 			__aligned_u64 page_size;
139 			__aligned_u64 uptr;
140 			__aligned_u64 out_nr_dirty;
141 		} dirty;
142 		struct {
143 			__u32 id;
144 			__u32 iotlb;
145 		} check_iotlb;
146 		struct {
147 			__u32 dev_id;
148 			__u32 pasid;
149 			__u32 grpid;
150 			__u32 perm;
151 			__u64 addr;
152 		} trigger_iopf;
153 		struct {
154 			__u32 id;
155 			__u32 cache;
156 		} check_dev_cache;
157 		struct {
158 			__u32 dev_id;
159 		} trigger_vevent;
160 		struct {
161 			__u32 pasid;
162 			__u32 pt_id;
163 			/* @id is stdev_id */
164 		} pasid_attach;
165 		struct {
166 			__u32 pasid;
167 			__u32 pt_id;
168 			/* @id is stdev_id */
169 		} pasid_replace;
170 		struct {
171 			__u32 pasid;
172 			/* @id is stdev_id */
173 		} pasid_detach;
174 		struct {
175 			__u32 pasid;
176 			__u32 hwpt_id;
177 			/* @id is stdev_id */
178 		} pasid_check;
179 	};
180 	__u32 last;
181 };
182 #define IOMMU_TEST_CMD _IO(IOMMUFD_TYPE, IOMMUFD_CMD_BASE + 32)
183 
184 /* Mock device/iommu PASID width */
185 #define MOCK_PASID_WIDTH 20
186 
187 /* Mock structs for IOMMU_DEVICE_GET_HW_INFO ioctl */
188 #define IOMMU_HW_INFO_TYPE_SELFTEST	0xfeedbeef
189 #define IOMMU_HW_INFO_SELFTEST_REGVAL	0xdeadbeef
190 
191 struct iommu_test_hw_info {
192 	__u32 flags;
193 	__u32 test_reg;
194 };
195 
196 /* Should not be equal to any defined value in enum iommu_hwpt_data_type */
197 #define IOMMU_HWPT_DATA_SELFTEST 0xdead
198 #define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef
199 #define IOMMU_TEST_DEV_CACHE_DEFAULT 0xbaddad
200 
201 /**
202  * struct iommu_hwpt_selftest
203  *
204  * @iotlb: default mock iotlb value, IOMMU_TEST_IOTLB_DEFAULT
205  */
206 struct iommu_hwpt_selftest {
207 	__u32 iotlb;
208 };
209 
210 /* Should not be equal to any defined value in enum iommu_hwpt_invalidate_data_type */
211 #define IOMMU_HWPT_INVALIDATE_DATA_SELFTEST 0xdeadbeef
212 #define IOMMU_HWPT_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef
213 
214 /**
215  * struct iommu_hwpt_invalidate_selftest - Invalidation data for Mock driver
216  *                                         (IOMMU_HWPT_INVALIDATE_DATA_SELFTEST)
217  * @flags: Invalidate flags
218  * @iotlb_id: Invalidate iotlb entry index
219  *
220  * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @iotlb_id will be ignored
221  */
222 struct iommu_hwpt_invalidate_selftest {
223 #define IOMMU_TEST_INVALIDATE_FLAG_ALL	(1 << 0)
224 	__u32 flags;
225 	__u32 iotlb_id;
226 };
227 
228 #define IOMMU_VIOMMU_TYPE_SELFTEST 0xdeadbeef
229 
230 /* Should not be equal to any defined value in enum iommu_viommu_invalidate_data_type */
231 #define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST 0xdeadbeef
232 #define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef
233 
234 /**
235  * struct iommu_viommu_invalidate_selftest - Invalidation data for Mock VIOMMU
236  *                                        (IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST)
237  * @flags: Invalidate flags
238  * @cache_id: Invalidate cache entry index
239  *
240  * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @cache_id will be ignored
241  */
242 struct iommu_viommu_invalidate_selftest {
243 #define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0)
244 	__u32 flags;
245 	__u32 vdev_id;
246 	__u32 cache_id;
247 };
248 
249 #define IOMMU_VEVENTQ_TYPE_SELFTEST 0xbeefbeef
250 
251 struct iommu_viommu_event_selftest {
252 	__u32 virt_id;
253 };
254 
255 #endif
256