1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES.
4 #ifndef _UAPI_IOMMUFD_H
5 #define _UAPI_IOMMUFD_H
7 #include <linux/types.h>
8 #include <linux/ioctl.h>
10 #define IOMMUFD_TYPE (';')
13 * DOC: General ioctl format
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.
21 * ioctls use a standard meaning for common errnos:
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
31 * - ENOENT: An ID or IOVA provided does not exist.
32 * - ENOMEM: Out of memory.
33 * - EOVERFLOW: Mathematics overflowed.
35 * As well as additional errnos, within specific ioctls.
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,
45 IOMMUFD_CMD_IOAS_UNMAP,
47 IOMMUFD_CMD_VFIO_IOAS,
51 * struct iommu_destroy - ioctl(IOMMU_DESTROY)
52 * @size: sizeof(struct iommu_destroy)
53 * @id: iommufd object ID to destroy. Can be any destroyable object type.
55 * Destroy any object held within iommufd.
57 struct iommu_destroy {
61 #define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY)
64 * struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC)
65 * @size: sizeof(struct iommu_ioas_alloc)
67 * @out_ioas_id: Output IOAS ID for the allocated object
69 * Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA)
72 struct iommu_ioas_alloc {
77 #define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC)
80 * struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE)
82 * @last: Inclusive last IOVA
84 * An interval in IOVA space.
86 struct iommu_iova_range {
92 * struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES)
93 * @size: sizeof(struct iommu_ioas_iova_ranges)
94 * @ioas_id: IOAS ID to read ranges from
95 * @num_iovas: Input/Output total number of ranges in the IOAS
96 * @__reserved: Must be 0
97 * @allowed_iovas: Pointer to the output array of struct iommu_iova_range
98 * @out_iova_alignment: Minimum alignment required for mapping IOVA
100 * Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges
101 * is not allowed. num_iovas will be set to the total number of iovas and
102 * the allowed_iovas[] will be filled in as space permits.
104 * The allowed ranges are dependent on the HW path the DMA operation takes, and
105 * can change during the lifetime of the IOAS. A fresh empty IOAS will have a
106 * full range, and each attached device will narrow the ranges based on that
107 * device's HW restrictions. Detaching a device can widen the ranges. Userspace
108 * should query ranges after every attach/detach to know what IOVAs are valid
111 * On input num_iovas is the length of the allowed_iovas array. On output it is
112 * the total number of iovas filled in. The ioctl will return -EMSGSIZE and set
113 * num_iovas to the required value if num_iovas is too small. In this case the
114 * caller should allocate a larger output array and re-issue the ioctl.
116 * out_iova_alignment returns the minimum IOVA alignment that can be given
117 * to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy::
119 * starting_iova % out_iova_alignment == 0
120 * (starting_iova + length) % out_iova_alignment == 0
122 * out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot
123 * be higher than the system PAGE_SIZE.
125 struct iommu_ioas_iova_ranges {
130 __aligned_u64 allowed_iovas;
131 __aligned_u64 out_iova_alignment;
133 #define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES)
136 * struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS)
137 * @size: sizeof(struct iommu_ioas_allow_iovas)
138 * @ioas_id: IOAS ID to allow IOVAs from
139 * @num_iovas: Input/Output total number of ranges in the IOAS
140 * @__reserved: Must be 0
141 * @allowed_iovas: Pointer to array of struct iommu_iova_range
143 * Ensure a range of IOVAs are always available for allocation. If this call
144 * succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges
145 * that are narrower than the ranges provided here. This call will fail if
146 * IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges.
148 * When an IOAS is first created the IOVA_RANGES will be maximally sized, and as
149 * devices are attached the IOVA will narrow based on the device restrictions.
150 * When an allowed range is specified any narrowing will be refused, ie device
151 * attachment can fail if the device requires limiting within the allowed range.
153 * Automatic IOVA allocation is also impacted by this call. MAP will only
154 * allocate within the allowed IOVAs if they are present.
156 * This call replaces the entire allowed list with the given list.
158 struct iommu_ioas_allow_iovas {
163 __aligned_u64 allowed_iovas;
165 #define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS)
168 * enum iommufd_ioas_map_flags - Flags for map and copy
169 * @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate
170 * IOVA to place the mapping at
171 * @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping
172 * @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping
174 enum iommufd_ioas_map_flags {
175 IOMMU_IOAS_MAP_FIXED_IOVA = 1 << 0,
176 IOMMU_IOAS_MAP_WRITEABLE = 1 << 1,
177 IOMMU_IOAS_MAP_READABLE = 1 << 2,
181 * struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP)
182 * @size: sizeof(struct iommu_ioas_map)
183 * @flags: Combination of enum iommufd_ioas_map_flags
184 * @ioas_id: IOAS ID to change the mapping of
185 * @__reserved: Must be 0
186 * @user_va: Userspace pointer to start mapping from
187 * @length: Number of bytes to map
188 * @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set
189 * then this must be provided as input.
191 * Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the
192 * mapping will be established at iova, otherwise a suitable location based on
193 * the reserved and allowed lists will be automatically selected and returned in
196 * If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently
197 * be unused, existing IOVA cannot be replaced.
199 struct iommu_ioas_map {
204 __aligned_u64 user_va;
205 __aligned_u64 length;
208 #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP)
211 * struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY)
212 * @size: sizeof(struct iommu_ioas_copy)
213 * @flags: Combination of enum iommufd_ioas_map_flags
214 * @dst_ioas_id: IOAS ID to change the mapping of
215 * @src_ioas_id: IOAS ID to copy from
216 * @length: Number of bytes to copy and map
217 * @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is
218 * set then this must be provided as input.
219 * @src_iova: IOVA to start the copy
221 * Copy an already existing mapping from src_ioas_id and establish it in
222 * dst_ioas_id. The src iova/length must exactly match a range used with
225 * This may be used to efficiently clone a subset of an IOAS to another, or as a
226 * kind of 'cache' to speed up mapping. Copy has an efficiency advantage over
227 * establishing equivalent new mappings, as internal resources are shared, and
228 * the kernel will pin the user memory only once.
230 struct iommu_ioas_copy {
235 __aligned_u64 length;
236 __aligned_u64 dst_iova;
237 __aligned_u64 src_iova;
239 #define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY)
242 * struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP)
243 * @size: sizeof(struct iommu_ioas_unmap)
244 * @ioas_id: IOAS ID to change the mapping of
245 * @iova: IOVA to start the unmapping at
246 * @length: Number of bytes to unmap, and return back the bytes unmapped
248 * Unmap an IOVA range. The iova/length must be a superset of a previously
249 * mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or
250 * truncating ranges is not allowed. The values 0 to U64_MAX will unmap
253 struct iommu_ioas_unmap {
257 __aligned_u64 length;
259 #define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP)
262 * enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and
263 * ioctl(IOMMU_OPTION_HUGE_PAGES)
264 * @IOMMU_OPTION_RLIMIT_MODE:
265 * Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege
266 * to invoke this. Value 0 (default) is user based accouting, 1 uses process
267 * based accounting. Global option, object_id must be 0
268 * @IOMMU_OPTION_HUGE_PAGES:
269 * Value 1 (default) allows contiguous pages to be combined when generating
270 * iommu mappings. Value 0 disables combining, everything is mapped to
271 * PAGE_SIZE. This can be useful for benchmarking. This is a per-IOAS
272 * option, the object_id must be the IOAS ID.
274 enum iommufd_option {
275 IOMMU_OPTION_RLIMIT_MODE = 0,
276 IOMMU_OPTION_HUGE_PAGES = 1,
280 * enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and
281 * ioctl(IOMMU_OPTION_OP_GET)
282 * @IOMMU_OPTION_OP_SET: Set the option's value
283 * @IOMMU_OPTION_OP_GET: Get the option's value
285 enum iommufd_option_ops {
286 IOMMU_OPTION_OP_SET = 0,
287 IOMMU_OPTION_OP_GET = 1,
291 * struct iommu_option - iommu option multiplexer
292 * @size: sizeof(struct iommu_option)
293 * @option_id: One of enum iommufd_option
294 * @op: One of enum iommufd_option_ops
295 * @__reserved: Must be 0
296 * @object_id: ID of the object if required
297 * @val64: Option value to set or value returned on get
299 * Change a simple option value. This multiplexor allows controlling options
300 * on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET
301 * will return the current value.
303 struct iommu_option {
311 #define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION)
314 * enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls
315 * @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS
316 * @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS
317 * @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility
319 enum iommufd_vfio_ioas_op {
320 IOMMU_VFIO_IOAS_GET = 0,
321 IOMMU_VFIO_IOAS_SET = 1,
322 IOMMU_VFIO_IOAS_CLEAR = 2,
326 * struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS)
327 * @size: sizeof(struct iommu_vfio_ioas)
328 * @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set
329 * For IOMMU_VFIO_IOAS_GET will output the IOAS ID
330 * @op: One of enum iommufd_vfio_ioas_op
331 * @__reserved: Must be 0
333 * The VFIO compatibility support uses a single ioas because VFIO APIs do not
334 * support the ID field. Set or Get the IOAS that VFIO compatibility will use.
335 * When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the
336 * compatibility ioas, either by taking what is already set, or auto creating
337 * one. From then on VFIO will continue to use that ioas and is not effected by
338 * this ioctl. SET or CLEAR does not destroy any auto-created IOAS.
340 struct iommu_vfio_ioas {
346 #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS)