xref: /openbmc/qemu/linux-headers/linux/iommufd.h (revision 6c7937ec)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES.
3  */
4 #ifndef _IOMMUFD_H
5 #define _IOMMUFD_H
6 
7 #include <linux/types.h>
8 #include <linux/ioctl.h>
9 
10 #define IOMMUFD_TYPE (';')
11 
12 /**
13  * DOC: General ioctl format
14  *
15  * The ioctl interface follows a general format to allow for extensibility. Each
16  * ioctl is passed in a structure pointer as the argument providing the size of
17  * the structure in the first u32. The kernel checks that any structure space
18  * beyond what it understands is 0. This allows userspace to use the backward
19  * compatible portion while consistently using the newer, larger, structures.
20  *
21  * ioctls use a standard meaning for common errnos:
22  *
23  *  - ENOTTY: The IOCTL number itself is not supported at all
24  *  - E2BIG: The IOCTL number is supported, but the provided structure has
25  *    non-zero in a part the kernel does not understand.
26  *  - EOPNOTSUPP: The IOCTL number is supported, and the structure is
27  *    understood, however a known field has a value the kernel does not
28  *    understand or support.
29  *  - EINVAL: Everything about the IOCTL was understood, but a field is not
30  *    correct.
31  *  - ENOENT: An ID or IOVA provided does not exist.
32  *  - ENOMEM: Out of memory.
33  *  - EOVERFLOW: Mathematics overflowed.
34  *
35  * As well as additional errnos, within specific ioctls.
36  */
37 enum {
38 	IOMMUFD_CMD_BASE = 0x80,
39 	IOMMUFD_CMD_DESTROY = IOMMUFD_CMD_BASE,
40 	IOMMUFD_CMD_IOAS_ALLOC,
41 	IOMMUFD_CMD_IOAS_ALLOW_IOVAS,
42 	IOMMUFD_CMD_IOAS_COPY,
43 	IOMMUFD_CMD_IOAS_IOVA_RANGES,
44 	IOMMUFD_CMD_IOAS_MAP,
45 	IOMMUFD_CMD_IOAS_UNMAP,
46 	IOMMUFD_CMD_OPTION,
47 	IOMMUFD_CMD_VFIO_IOAS,
48 	IOMMUFD_CMD_HWPT_ALLOC,
49 	IOMMUFD_CMD_GET_HW_INFO,
50 };
51 
52 /**
53  * struct iommu_destroy - ioctl(IOMMU_DESTROY)
54  * @size: sizeof(struct iommu_destroy)
55  * @id: iommufd object ID to destroy. Can be any destroyable object type.
56  *
57  * Destroy any object held within iommufd.
58  */
59 struct iommu_destroy {
60 	__u32 size;
61 	__u32 id;
62 };
63 #define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY)
64 
65 /**
66  * struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC)
67  * @size: sizeof(struct iommu_ioas_alloc)
68  * @flags: Must be 0
69  * @out_ioas_id: Output IOAS ID for the allocated object
70  *
71  * Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA)
72  * to memory mapping.
73  */
74 struct iommu_ioas_alloc {
75 	__u32 size;
76 	__u32 flags;
77 	__u32 out_ioas_id;
78 };
79 #define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC)
80 
81 /**
82  * struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE)
83  * @start: First IOVA
84  * @last: Inclusive last IOVA
85  *
86  * An interval in IOVA space.
87  */
88 struct iommu_iova_range {
89 	__aligned_u64 start;
90 	__aligned_u64 last;
91 };
92 
93 /**
94  * struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES)
95  * @size: sizeof(struct iommu_ioas_iova_ranges)
96  * @ioas_id: IOAS ID to read ranges from
97  * @num_iovas: Input/Output total number of ranges in the IOAS
98  * @__reserved: Must be 0
99  * @allowed_iovas: Pointer to the output array of struct iommu_iova_range
100  * @out_iova_alignment: Minimum alignment required for mapping IOVA
101  *
102  * Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges
103  * is not allowed. num_iovas will be set to the total number of iovas and
104  * the allowed_iovas[] will be filled in as space permits.
105  *
106  * The allowed ranges are dependent on the HW path the DMA operation takes, and
107  * can change during the lifetime of the IOAS. A fresh empty IOAS will have a
108  * full range, and each attached device will narrow the ranges based on that
109  * device's HW restrictions. Detaching a device can widen the ranges. Userspace
110  * should query ranges after every attach/detach to know what IOVAs are valid
111  * for mapping.
112  *
113  * On input num_iovas is the length of the allowed_iovas array. On output it is
114  * the total number of iovas filled in. The ioctl will return -EMSGSIZE and set
115  * num_iovas to the required value if num_iovas is too small. In this case the
116  * caller should allocate a larger output array and re-issue the ioctl.
117  *
118  * out_iova_alignment returns the minimum IOVA alignment that can be given
119  * to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy::
120  *
121  *   starting_iova % out_iova_alignment == 0
122  *   (starting_iova + length) % out_iova_alignment == 0
123  *
124  * out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot
125  * be higher than the system PAGE_SIZE.
126  */
127 struct iommu_ioas_iova_ranges {
128 	__u32 size;
129 	__u32 ioas_id;
130 	__u32 num_iovas;
131 	__u32 __reserved;
132 	__aligned_u64 allowed_iovas;
133 	__aligned_u64 out_iova_alignment;
134 };
135 #define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES)
136 
137 /**
138  * struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS)
139  * @size: sizeof(struct iommu_ioas_allow_iovas)
140  * @ioas_id: IOAS ID to allow IOVAs from
141  * @num_iovas: Input/Output total number of ranges in the IOAS
142  * @__reserved: Must be 0
143  * @allowed_iovas: Pointer to array of struct iommu_iova_range
144  *
145  * Ensure a range of IOVAs are always available for allocation. If this call
146  * succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges
147  * that are narrower than the ranges provided here. This call will fail if
148  * IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges.
149  *
150  * When an IOAS is first created the IOVA_RANGES will be maximally sized, and as
151  * devices are attached the IOVA will narrow based on the device restrictions.
152  * When an allowed range is specified any narrowing will be refused, ie device
153  * attachment can fail if the device requires limiting within the allowed range.
154  *
155  * Automatic IOVA allocation is also impacted by this call. MAP will only
156  * allocate within the allowed IOVAs if they are present.
157  *
158  * This call replaces the entire allowed list with the given list.
159  */
160 struct iommu_ioas_allow_iovas {
161 	__u32 size;
162 	__u32 ioas_id;
163 	__u32 num_iovas;
164 	__u32 __reserved;
165 	__aligned_u64 allowed_iovas;
166 };
167 #define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS)
168 
169 /**
170  * enum iommufd_ioas_map_flags - Flags for map and copy
171  * @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate
172  *                             IOVA to place the mapping at
173  * @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping
174  * @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping
175  */
176 enum iommufd_ioas_map_flags {
177 	IOMMU_IOAS_MAP_FIXED_IOVA = 1 << 0,
178 	IOMMU_IOAS_MAP_WRITEABLE = 1 << 1,
179 	IOMMU_IOAS_MAP_READABLE = 1 << 2,
180 };
181 
182 /**
183  * struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP)
184  * @size: sizeof(struct iommu_ioas_map)
185  * @flags: Combination of enum iommufd_ioas_map_flags
186  * @ioas_id: IOAS ID to change the mapping of
187  * @__reserved: Must be 0
188  * @user_va: Userspace pointer to start mapping from
189  * @length: Number of bytes to map
190  * @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set
191  *        then this must be provided as input.
192  *
193  * Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the
194  * mapping will be established at iova, otherwise a suitable location based on
195  * the reserved and allowed lists will be automatically selected and returned in
196  * iova.
197  *
198  * If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently
199  * be unused, existing IOVA cannot be replaced.
200  */
201 struct iommu_ioas_map {
202 	__u32 size;
203 	__u32 flags;
204 	__u32 ioas_id;
205 	__u32 __reserved;
206 	__aligned_u64 user_va;
207 	__aligned_u64 length;
208 	__aligned_u64 iova;
209 };
210 #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP)
211 
212 /**
213  * struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY)
214  * @size: sizeof(struct iommu_ioas_copy)
215  * @flags: Combination of enum iommufd_ioas_map_flags
216  * @dst_ioas_id: IOAS ID to change the mapping of
217  * @src_ioas_id: IOAS ID to copy from
218  * @length: Number of bytes to copy and map
219  * @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is
220  *            set then this must be provided as input.
221  * @src_iova: IOVA to start the copy
222  *
223  * Copy an already existing mapping from src_ioas_id and establish it in
224  * dst_ioas_id. The src iova/length must exactly match a range used with
225  * IOMMU_IOAS_MAP.
226  *
227  * This may be used to efficiently clone a subset of an IOAS to another, or as a
228  * kind of 'cache' to speed up mapping. Copy has an efficiency advantage over
229  * establishing equivalent new mappings, as internal resources are shared, and
230  * the kernel will pin the user memory only once.
231  */
232 struct iommu_ioas_copy {
233 	__u32 size;
234 	__u32 flags;
235 	__u32 dst_ioas_id;
236 	__u32 src_ioas_id;
237 	__aligned_u64 length;
238 	__aligned_u64 dst_iova;
239 	__aligned_u64 src_iova;
240 };
241 #define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY)
242 
243 /**
244  * struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP)
245  * @size: sizeof(struct iommu_ioas_unmap)
246  * @ioas_id: IOAS ID to change the mapping of
247  * @iova: IOVA to start the unmapping at
248  * @length: Number of bytes to unmap, and return back the bytes unmapped
249  *
250  * Unmap an IOVA range. The iova/length must be a superset of a previously
251  * mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or
252  * truncating ranges is not allowed. The values 0 to U64_MAX will unmap
253  * everything.
254  */
255 struct iommu_ioas_unmap {
256 	__u32 size;
257 	__u32 ioas_id;
258 	__aligned_u64 iova;
259 	__aligned_u64 length;
260 };
261 #define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP)
262 
263 /**
264  * enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and
265  *                       ioctl(IOMMU_OPTION_HUGE_PAGES)
266  * @IOMMU_OPTION_RLIMIT_MODE:
267  *    Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege
268  *    to invoke this. Value 0 (default) is user based accouting, 1 uses process
269  *    based accounting. Global option, object_id must be 0
270  * @IOMMU_OPTION_HUGE_PAGES:
271  *    Value 1 (default) allows contiguous pages to be combined when generating
272  *    iommu mappings. Value 0 disables combining, everything is mapped to
273  *    PAGE_SIZE. This can be useful for benchmarking.  This is a per-IOAS
274  *    option, the object_id must be the IOAS ID.
275  */
276 enum iommufd_option {
277 	IOMMU_OPTION_RLIMIT_MODE = 0,
278 	IOMMU_OPTION_HUGE_PAGES = 1,
279 };
280 
281 /**
282  * enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and
283  *                           ioctl(IOMMU_OPTION_OP_GET)
284  * @IOMMU_OPTION_OP_SET: Set the option's value
285  * @IOMMU_OPTION_OP_GET: Get the option's value
286  */
287 enum iommufd_option_ops {
288 	IOMMU_OPTION_OP_SET = 0,
289 	IOMMU_OPTION_OP_GET = 1,
290 };
291 
292 /**
293  * struct iommu_option - iommu option multiplexer
294  * @size: sizeof(struct iommu_option)
295  * @option_id: One of enum iommufd_option
296  * @op: One of enum iommufd_option_ops
297  * @__reserved: Must be 0
298  * @object_id: ID of the object if required
299  * @val64: Option value to set or value returned on get
300  *
301  * Change a simple option value. This multiplexor allows controlling options
302  * on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET
303  * will return the current value.
304  */
305 struct iommu_option {
306 	__u32 size;
307 	__u32 option_id;
308 	__u16 op;
309 	__u16 __reserved;
310 	__u32 object_id;
311 	__aligned_u64 val64;
312 };
313 #define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION)
314 
315 /**
316  * enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls
317  * @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS
318  * @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS
319  * @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility
320  */
321 enum iommufd_vfio_ioas_op {
322 	IOMMU_VFIO_IOAS_GET = 0,
323 	IOMMU_VFIO_IOAS_SET = 1,
324 	IOMMU_VFIO_IOAS_CLEAR = 2,
325 };
326 
327 /**
328  * struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS)
329  * @size: sizeof(struct iommu_vfio_ioas)
330  * @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set
331  *           For IOMMU_VFIO_IOAS_GET will output the IOAS ID
332  * @op: One of enum iommufd_vfio_ioas_op
333  * @__reserved: Must be 0
334  *
335  * The VFIO compatibility support uses a single ioas because VFIO APIs do not
336  * support the ID field. Set or Get the IOAS that VFIO compatibility will use.
337  * When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the
338  * compatibility ioas, either by taking what is already set, or auto creating
339  * one. From then on VFIO will continue to use that ioas and is not effected by
340  * this ioctl. SET or CLEAR does not destroy any auto-created IOAS.
341  */
342 struct iommu_vfio_ioas {
343 	__u32 size;
344 	__u32 ioas_id;
345 	__u16 op;
346 	__u16 __reserved;
347 };
348 #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS)
349 
350 /**
351  * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC)
352  * @size: sizeof(struct iommu_hwpt_alloc)
353  * @flags: Must be 0
354  * @dev_id: The device to allocate this HWPT for
355  * @pt_id: The IOAS to connect this HWPT to
356  * @out_hwpt_id: The ID of the new HWPT
357  * @__reserved: Must be 0
358  *
359  * Explicitly allocate a hardware page table object. This is the same object
360  * type that is returned by iommufd_device_attach() and represents the
361  * underlying iommu driver's iommu_domain kernel object.
362  *
363  * A HWPT will be created with the IOVA mappings from the given IOAS.
364  */
365 struct iommu_hwpt_alloc {
366 	__u32 size;
367 	__u32 flags;
368 	__u32 dev_id;
369 	__u32 pt_id;
370 	__u32 out_hwpt_id;
371 	__u32 __reserved;
372 };
373 #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC)
374 
375 /**
376  * struct iommu_hw_info_vtd - Intel VT-d hardware information
377  *
378  * @flags: Must be 0
379  * @__reserved: Must be 0
380  *
381  * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec
382  *           section 11.4.2 Capability Register.
383  * @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec
384  *            section 11.4.3 Extended Capability Register.
385  *
386  * User needs to understand the Intel VT-d specification to decode the
387  * register value.
388  */
389 struct iommu_hw_info_vtd {
390 	__u32 flags;
391 	__u32 __reserved;
392 	__aligned_u64 cap_reg;
393 	__aligned_u64 ecap_reg;
394 };
395 
396 /**
397  * enum iommu_hw_info_type - IOMMU Hardware Info Types
398  * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not report hardware
399  *                           info
400  * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
401  */
402 enum iommu_hw_info_type {
403 	IOMMU_HW_INFO_TYPE_NONE,
404 	IOMMU_HW_INFO_TYPE_INTEL_VTD,
405 };
406 
407 /**
408  * struct iommu_hw_info - ioctl(IOMMU_GET_HW_INFO)
409  * @size: sizeof(struct iommu_hw_info)
410  * @flags: Must be 0
411  * @dev_id: The device bound to the iommufd
412  * @data_len: Input the length of a user buffer in bytes. Output the length of
413  *            data that kernel supports
414  * @data_uptr: User pointer to a user-space buffer used by the kernel to fill
415  *             the iommu type specific hardware information data
416  * @out_data_type: Output the iommu hardware info type as defined in the enum
417  *                 iommu_hw_info_type.
418  * @__reserved: Must be 0
419  *
420  * Query an iommu type specific hardware information data from an iommu behind
421  * a given device that has been bound to iommufd. This hardware info data will
422  * be used to sync capabilities between the virtual iommu and the physical
423  * iommu, e.g. a nested translation setup needs to check the hardware info, so
424  * a guest stage-1 page table can be compatible with the physical iommu.
425  *
426  * To capture an iommu type specific hardware information data, @data_uptr and
427  * its length @data_len must be provided. Trailing bytes will be zeroed if the
428  * user buffer is larger than the data that kernel has. Otherwise, kernel only
429  * fills the buffer using the given length in @data_len. If the ioctl succeeds,
430  * @data_len will be updated to the length that kernel actually supports,
431  * @out_data_type will be filled to decode the data filled in the buffer
432  * pointed by @data_uptr. Input @data_len == zero is allowed.
433  */
434 struct iommu_hw_info {
435 	__u32 size;
436 	__u32 flags;
437 	__u32 dev_id;
438 	__u32 data_len;
439 	__aligned_u64 data_uptr;
440 	__u32 out_data_type;
441 	__u32 __reserved;
442 };
443 #define IOMMU_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_GET_HW_INFO)
444 #endif
445