xref: /linux/drivers/iommu/iommufd/iommufd_test.h (revision 8c4dc1a5025f5c35beef43fbf8ce50bb7e93b762)
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 	IOMMU_TEST_OP_DMABUF_GET,
33 	IOMMU_TEST_OP_DMABUF_REVOKE,
34 };
35 
36 enum {
37 	MOCK_IOMMUPT_DEFAULT = 0,
38 	MOCK_IOMMUPT_HUGE,
39 };
40 
41 /* These values are true for MOCK_IOMMUPT_DEFAULT */
42 enum {
43 	MOCK_APERTURE_START = 1UL << 24,
44 	MOCK_APERTURE_LAST = (1UL << 31) - 1,
45 	MOCK_PAGE_SIZE = 2048,
46 	MOCK_HUGE_PAGE_SIZE = 512 * MOCK_PAGE_SIZE,
47 };
48 
49 enum {
50 	MOCK_FLAGS_ACCESS_WRITE = 1 << 0,
51 	MOCK_FLAGS_ACCESS_SYZ = 1 << 16,
52 };
53 
54 enum {
55 	MOCK_ACCESS_RW_WRITE = 1 << 0,
56 	MOCK_ACCESS_RW_SLOW_PATH = 1 << 2,
57 };
58 
59 enum {
60 	MOCK_FLAGS_ACCESS_CREATE_NEEDS_PIN_PAGES = 1 << 0,
61 };
62 
63 enum {
64 	MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0,
65 	MOCK_FLAGS_DEVICE_PASID = 1 << 2,
66 };
67 
68 enum {
69 	MOCK_NESTED_DOMAIN_IOTLB_ID_MAX = 3,
70 	MOCK_NESTED_DOMAIN_IOTLB_NUM = 4,
71 };
72 
73 enum {
74 	MOCK_DEV_CACHE_ID_MAX = 3,
75 	MOCK_DEV_CACHE_NUM = 4,
76 };
77 
78 /* Reserved for special pasid replace test */
79 #define IOMMU_TEST_PASID_RESERVED 1024
80 
81 struct iommu_test_cmd {
82 	__u32 size;
83 	__u32 op;
84 	__u32 id;
85 	__u32 __reserved;
86 	union {
87 		struct {
88 			__aligned_u64 start;
89 			__aligned_u64 length;
90 		} add_reserved;
91 		struct {
92 			__u32 out_stdev_id;
93 			__u32 out_hwpt_id;
94 			/* out_idev_id is the standard iommufd_bind object */
95 			__u32 out_idev_id;
96 		} mock_domain;
97 		struct {
98 			__u32 out_stdev_id;
99 			__u32 out_hwpt_id;
100 			__u32 out_idev_id;
101 			/* Expand mock_domain to set mock device flags */
102 			__u32 dev_flags;
103 		} mock_domain_flags;
104 		struct {
105 			__u32 pt_id;
106 		} mock_domain_replace;
107 		struct {
108 			__aligned_u64 iova;
109 			__aligned_u64 length;
110 			__aligned_u64 uptr;
111 		} check_map;
112 		struct {
113 			__aligned_u64 length;
114 			__aligned_u64 uptr;
115 			__u32 refs;
116 		} check_refs;
117 		struct {
118 			__u32 out_access_fd;
119 			__u32 flags;
120 		} create_access;
121 		struct {
122 			__u32 access_pages_id;
123 		} destroy_access_pages;
124 		struct {
125 			__u32 flags;
126 			__u32 out_access_pages_id;
127 			__aligned_u64 iova;
128 			__aligned_u64 length;
129 			__aligned_u64 uptr;
130 		} access_pages;
131 		struct {
132 			__aligned_u64 iova;
133 			__aligned_u64 length;
134 			__aligned_u64 uptr;
135 			__u32 flags;
136 		} access_rw;
137 		struct {
138 			__u32 limit;
139 		} memory_limit;
140 		struct {
141 			__u32 ioas_id;
142 		} access_replace_ioas;
143 		struct {
144 			__u32 flags;
145 			__aligned_u64 iova;
146 			__aligned_u64 length;
147 			__aligned_u64 page_size;
148 			__aligned_u64 uptr;
149 			__aligned_u64 out_nr_dirty;
150 		} dirty;
151 		struct {
152 			__u32 id;
153 			__u32 iotlb;
154 		} check_iotlb;
155 		struct {
156 			__u32 dev_id;
157 			__u32 pasid;
158 			__u32 grpid;
159 			__u32 perm;
160 			__u64 addr;
161 		} trigger_iopf;
162 		struct {
163 			__u32 id;
164 			__u32 cache;
165 		} check_dev_cache;
166 		struct {
167 			__u32 dev_id;
168 		} trigger_vevent;
169 		struct {
170 			__u32 pasid;
171 			__u32 pt_id;
172 			/* @id is stdev_id */
173 		} pasid_attach;
174 		struct {
175 			__u32 pasid;
176 			__u32 pt_id;
177 			/* @id is stdev_id */
178 		} pasid_replace;
179 		struct {
180 			__u32 pasid;
181 			/* @id is stdev_id */
182 		} pasid_detach;
183 		struct {
184 			__u32 pasid;
185 			__u32 hwpt_id;
186 			/* @id is stdev_id */
187 		} pasid_check;
188 		struct {
189 			__u32 length;
190 			__u32 open_flags;
191 		} dmabuf_get;
192 		struct {
193 			__s32 dmabuf_fd;
194 			__u32 revoked;
195 		} dmabuf_revoke;
196 	};
197 	__u32 last;
198 };
199 #define IOMMU_TEST_CMD _IO(IOMMUFD_TYPE, IOMMUFD_CMD_BASE + 32)
200 
201 /* Mock device/iommu PASID width */
202 #define MOCK_PASID_WIDTH 20
203 
204 /* Mock structs for IOMMU_DEVICE_GET_HW_INFO ioctl */
205 #define IOMMU_HW_INFO_TYPE_SELFTEST	0xfeedbeef
206 #define IOMMU_HW_INFO_SELFTEST_REGVAL	0xdeadbeef
207 
208 struct iommu_test_hw_info {
209 	__u32 flags;
210 	__u32 test_reg;
211 };
212 
213 /* Should not be equal to any defined value in enum iommu_hwpt_data_type */
214 #define IOMMU_HWPT_DATA_SELFTEST 0xdead
215 #define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef
216 #define IOMMU_TEST_DEV_CACHE_DEFAULT 0xbaddad
217 
218 /**
219  * struct iommu_hwpt_selftest
220  *
221  * @iotlb: default mock iotlb value, IOMMU_TEST_IOTLB_DEFAULT
222  */
223 struct iommu_hwpt_selftest {
224 	__u32 iotlb;
225 	__u32 pagetable_type;
226 };
227 
228 /* Should not be equal to any defined value in enum iommu_hwpt_invalidate_data_type */
229 #define IOMMU_HWPT_INVALIDATE_DATA_SELFTEST 0xdeadbeef
230 #define IOMMU_HWPT_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef
231 
232 /**
233  * struct iommu_hwpt_invalidate_selftest - Invalidation data for Mock driver
234  *                                         (IOMMU_HWPT_INVALIDATE_DATA_SELFTEST)
235  * @flags: Invalidate flags
236  * @iotlb_id: Invalidate iotlb entry index
237  *
238  * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @iotlb_id will be ignored
239  */
240 struct iommu_hwpt_invalidate_selftest {
241 #define IOMMU_TEST_INVALIDATE_FLAG_ALL	(1 << 0)
242 	__u32 flags;
243 	__u32 iotlb_id;
244 };
245 
246 #define IOMMU_VIOMMU_TYPE_SELFTEST 0xdeadbeef
247 
248 /**
249  * struct iommu_viommu_selftest - vIOMMU data for Mock driver
250  *                                (IOMMU_VIOMMU_TYPE_SELFTEST)
251  * @in_data: Input random data from user space
252  * @out_data: Output data (matching @in_data) to user space
253  * @out_mmap_offset: The offset argument for mmap syscall
254  * @out_mmap_length: The length argument for mmap syscall
255  *
256  * Simply set @out_data=@in_data for a loopback test
257  */
258 struct iommu_viommu_selftest {
259 	__u32 in_data;
260 	__u32 out_data;
261 	__aligned_u64 out_mmap_offset;
262 	__aligned_u64 out_mmap_length;
263 };
264 
265 /* Should not be equal to any defined value in enum iommu_viommu_invalidate_data_type */
266 #define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST 0xdeadbeef
267 #define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef
268 
269 /**
270  * struct iommu_viommu_invalidate_selftest - Invalidation data for Mock VIOMMU
271  *                                        (IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST)
272  * @flags: Invalidate flags
273  * @cache_id: Invalidate cache entry index
274  *
275  * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @cache_id will be ignored
276  */
277 struct iommu_viommu_invalidate_selftest {
278 #define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0)
279 	__u32 flags;
280 	__u32 vdev_id;
281 	__u32 cache_id;
282 };
283 
284 #define IOMMU_VEVENTQ_TYPE_SELFTEST 0xbeefbeef
285 
286 struct iommu_viommu_event_selftest {
287 	__u32 virt_id;
288 };
289 
290 #define IOMMU_HW_QUEUE_TYPE_SELFTEST 0xdeadbeef
291 #define IOMMU_TEST_HW_QUEUE_MAX 2
292 
293 #endif
294