xref: /qemu/linux-headers/linux/iommufd.h (revision 44fe383c274174405da79f5fcb028e39fe688036)
1f7cbfa71SZhenzhong Duan /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2f7cbfa71SZhenzhong Duan /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES.
3f7cbfa71SZhenzhong Duan  */
4f7cbfa71SZhenzhong Duan #ifndef _IOMMUFD_H
5f7cbfa71SZhenzhong Duan #define _IOMMUFD_H
6f7cbfa71SZhenzhong Duan 
7f7cbfa71SZhenzhong Duan #include <linux/ioctl.h>
80d2eeef7SBibo Mao #include <linux/types.h>
9f7cbfa71SZhenzhong Duan 
10f7cbfa71SZhenzhong Duan #define IOMMUFD_TYPE (';')
11f7cbfa71SZhenzhong Duan 
12f7cbfa71SZhenzhong Duan /**
13f7cbfa71SZhenzhong Duan  * DOC: General ioctl format
14f7cbfa71SZhenzhong Duan  *
15f7cbfa71SZhenzhong Duan  * The ioctl interface follows a general format to allow for extensibility. Each
16f7cbfa71SZhenzhong Duan  * ioctl is passed in a structure pointer as the argument providing the size of
17f7cbfa71SZhenzhong Duan  * the structure in the first u32. The kernel checks that any structure space
18f7cbfa71SZhenzhong Duan  * beyond what it understands is 0. This allows userspace to use the backward
19f7cbfa71SZhenzhong Duan  * compatible portion while consistently using the newer, larger, structures.
20f7cbfa71SZhenzhong Duan  *
21f7cbfa71SZhenzhong Duan  * ioctls use a standard meaning for common errnos:
22f7cbfa71SZhenzhong Duan  *
23f7cbfa71SZhenzhong Duan  *  - ENOTTY: The IOCTL number itself is not supported at all
24f7cbfa71SZhenzhong Duan  *  - E2BIG: The IOCTL number is supported, but the provided structure has
25f7cbfa71SZhenzhong Duan  *    non-zero in a part the kernel does not understand.
26f7cbfa71SZhenzhong Duan  *  - EOPNOTSUPP: The IOCTL number is supported, and the structure is
27f7cbfa71SZhenzhong Duan  *    understood, however a known field has a value the kernel does not
28f7cbfa71SZhenzhong Duan  *    understand or support.
29f7cbfa71SZhenzhong Duan  *  - EINVAL: Everything about the IOCTL was understood, but a field is not
30f7cbfa71SZhenzhong Duan  *    correct.
31f7cbfa71SZhenzhong Duan  *  - ENOENT: An ID or IOVA provided does not exist.
32f7cbfa71SZhenzhong Duan  *  - ENOMEM: Out of memory.
33f7cbfa71SZhenzhong Duan  *  - EOVERFLOW: Mathematics overflowed.
34f7cbfa71SZhenzhong Duan  *
35f7cbfa71SZhenzhong Duan  * As well as additional errnos, within specific ioctls.
36f7cbfa71SZhenzhong Duan  */
37f7cbfa71SZhenzhong Duan enum {
38f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_BASE = 0x80,
39f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_DESTROY = IOMMUFD_CMD_BASE,
400d2eeef7SBibo Mao 	IOMMUFD_CMD_IOAS_ALLOC = 0x81,
410d2eeef7SBibo Mao 	IOMMUFD_CMD_IOAS_ALLOW_IOVAS = 0x82,
420d2eeef7SBibo Mao 	IOMMUFD_CMD_IOAS_COPY = 0x83,
430d2eeef7SBibo Mao 	IOMMUFD_CMD_IOAS_IOVA_RANGES = 0x84,
440d2eeef7SBibo Mao 	IOMMUFD_CMD_IOAS_MAP = 0x85,
450d2eeef7SBibo Mao 	IOMMUFD_CMD_IOAS_UNMAP = 0x86,
460d2eeef7SBibo Mao 	IOMMUFD_CMD_OPTION = 0x87,
470d2eeef7SBibo Mao 	IOMMUFD_CMD_VFIO_IOAS = 0x88,
480d2eeef7SBibo Mao 	IOMMUFD_CMD_HWPT_ALLOC = 0x89,
490d2eeef7SBibo Mao 	IOMMUFD_CMD_GET_HW_INFO = 0x8a,
500d2eeef7SBibo Mao 	IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING = 0x8b,
510d2eeef7SBibo Mao 	IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP = 0x8c,
520d2eeef7SBibo Mao 	IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d,
530d2eeef7SBibo Mao 	IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e,
54*44fe383cSHendrik Brueckner 	IOMMUFD_CMD_IOAS_MAP_FILE = 0x8f,
55*44fe383cSHendrik Brueckner 	IOMMUFD_CMD_VIOMMU_ALLOC = 0x90,
56*44fe383cSHendrik Brueckner 	IOMMUFD_CMD_VDEVICE_ALLOC = 0x91,
57*44fe383cSHendrik Brueckner 	IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
58f7cbfa71SZhenzhong Duan };
59f7cbfa71SZhenzhong Duan 
60f7cbfa71SZhenzhong Duan /**
61f7cbfa71SZhenzhong Duan  * struct iommu_destroy - ioctl(IOMMU_DESTROY)
62f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_destroy)
63f7cbfa71SZhenzhong Duan  * @id: iommufd object ID to destroy. Can be any destroyable object type.
64f7cbfa71SZhenzhong Duan  *
65f7cbfa71SZhenzhong Duan  * Destroy any object held within iommufd.
66f7cbfa71SZhenzhong Duan  */
67f7cbfa71SZhenzhong Duan struct iommu_destroy {
68f7cbfa71SZhenzhong Duan 	__u32 size;
69f7cbfa71SZhenzhong Duan 	__u32 id;
70f7cbfa71SZhenzhong Duan };
71f7cbfa71SZhenzhong Duan #define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY)
72f7cbfa71SZhenzhong Duan 
73f7cbfa71SZhenzhong Duan /**
74f7cbfa71SZhenzhong Duan  * struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC)
75f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_alloc)
76f7cbfa71SZhenzhong Duan  * @flags: Must be 0
77f7cbfa71SZhenzhong Duan  * @out_ioas_id: Output IOAS ID for the allocated object
78f7cbfa71SZhenzhong Duan  *
79f7cbfa71SZhenzhong Duan  * Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA)
80f7cbfa71SZhenzhong Duan  * to memory mapping.
81f7cbfa71SZhenzhong Duan  */
82f7cbfa71SZhenzhong Duan struct iommu_ioas_alloc {
83f7cbfa71SZhenzhong Duan 	__u32 size;
84f7cbfa71SZhenzhong Duan 	__u32 flags;
85f7cbfa71SZhenzhong Duan 	__u32 out_ioas_id;
86f7cbfa71SZhenzhong Duan };
87f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC)
88f7cbfa71SZhenzhong Duan 
89f7cbfa71SZhenzhong Duan /**
90f7cbfa71SZhenzhong Duan  * struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE)
91f7cbfa71SZhenzhong Duan  * @start: First IOVA
92f7cbfa71SZhenzhong Duan  * @last: Inclusive last IOVA
93f7cbfa71SZhenzhong Duan  *
94f7cbfa71SZhenzhong Duan  * An interval in IOVA space.
95f7cbfa71SZhenzhong Duan  */
96f7cbfa71SZhenzhong Duan struct iommu_iova_range {
97f7cbfa71SZhenzhong Duan 	__aligned_u64 start;
98f7cbfa71SZhenzhong Duan 	__aligned_u64 last;
99f7cbfa71SZhenzhong Duan };
100f7cbfa71SZhenzhong Duan 
101f7cbfa71SZhenzhong Duan /**
102f7cbfa71SZhenzhong Duan  * struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES)
103f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_iova_ranges)
104f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to read ranges from
105f7cbfa71SZhenzhong Duan  * @num_iovas: Input/Output total number of ranges in the IOAS
106f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
107f7cbfa71SZhenzhong Duan  * @allowed_iovas: Pointer to the output array of struct iommu_iova_range
108f7cbfa71SZhenzhong Duan  * @out_iova_alignment: Minimum alignment required for mapping IOVA
109f7cbfa71SZhenzhong Duan  *
110f7cbfa71SZhenzhong Duan  * Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges
111f7cbfa71SZhenzhong Duan  * is not allowed. num_iovas will be set to the total number of iovas and
112f7cbfa71SZhenzhong Duan  * the allowed_iovas[] will be filled in as space permits.
113f7cbfa71SZhenzhong Duan  *
114f7cbfa71SZhenzhong Duan  * The allowed ranges are dependent on the HW path the DMA operation takes, and
115f7cbfa71SZhenzhong Duan  * can change during the lifetime of the IOAS. A fresh empty IOAS will have a
116f7cbfa71SZhenzhong Duan  * full range, and each attached device will narrow the ranges based on that
117f7cbfa71SZhenzhong Duan  * device's HW restrictions. Detaching a device can widen the ranges. Userspace
118f7cbfa71SZhenzhong Duan  * should query ranges after every attach/detach to know what IOVAs are valid
119f7cbfa71SZhenzhong Duan  * for mapping.
120f7cbfa71SZhenzhong Duan  *
121f7cbfa71SZhenzhong Duan  * On input num_iovas is the length of the allowed_iovas array. On output it is
122f7cbfa71SZhenzhong Duan  * the total number of iovas filled in. The ioctl will return -EMSGSIZE and set
123f7cbfa71SZhenzhong Duan  * num_iovas to the required value if num_iovas is too small. In this case the
124f7cbfa71SZhenzhong Duan  * caller should allocate a larger output array and re-issue the ioctl.
125f7cbfa71SZhenzhong Duan  *
126f7cbfa71SZhenzhong Duan  * out_iova_alignment returns the minimum IOVA alignment that can be given
127f7cbfa71SZhenzhong Duan  * to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy::
128f7cbfa71SZhenzhong Duan  *
129f7cbfa71SZhenzhong Duan  *   starting_iova % out_iova_alignment == 0
130f7cbfa71SZhenzhong Duan  *   (starting_iova + length) % out_iova_alignment == 0
131f7cbfa71SZhenzhong Duan  *
132f7cbfa71SZhenzhong Duan  * out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot
133f7cbfa71SZhenzhong Duan  * be higher than the system PAGE_SIZE.
134f7cbfa71SZhenzhong Duan  */
135f7cbfa71SZhenzhong Duan struct iommu_ioas_iova_ranges {
136f7cbfa71SZhenzhong Duan 	__u32 size;
137f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
138f7cbfa71SZhenzhong Duan 	__u32 num_iovas;
139f7cbfa71SZhenzhong Duan 	__u32 __reserved;
140f7cbfa71SZhenzhong Duan 	__aligned_u64 allowed_iovas;
141f7cbfa71SZhenzhong Duan 	__aligned_u64 out_iova_alignment;
142f7cbfa71SZhenzhong Duan };
143f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES)
144f7cbfa71SZhenzhong Duan 
145f7cbfa71SZhenzhong Duan /**
146f7cbfa71SZhenzhong Duan  * struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS)
147f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_allow_iovas)
148f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to allow IOVAs from
149f7cbfa71SZhenzhong Duan  * @num_iovas: Input/Output total number of ranges in the IOAS
150f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
151f7cbfa71SZhenzhong Duan  * @allowed_iovas: Pointer to array of struct iommu_iova_range
152f7cbfa71SZhenzhong Duan  *
153f7cbfa71SZhenzhong Duan  * Ensure a range of IOVAs are always available for allocation. If this call
154f7cbfa71SZhenzhong Duan  * succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges
155f7cbfa71SZhenzhong Duan  * that are narrower than the ranges provided here. This call will fail if
156f7cbfa71SZhenzhong Duan  * IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges.
157f7cbfa71SZhenzhong Duan  *
158f7cbfa71SZhenzhong Duan  * When an IOAS is first created the IOVA_RANGES will be maximally sized, and as
159f7cbfa71SZhenzhong Duan  * devices are attached the IOVA will narrow based on the device restrictions.
160f7cbfa71SZhenzhong Duan  * When an allowed range is specified any narrowing will be refused, ie device
161f7cbfa71SZhenzhong Duan  * attachment can fail if the device requires limiting within the allowed range.
162f7cbfa71SZhenzhong Duan  *
163f7cbfa71SZhenzhong Duan  * Automatic IOVA allocation is also impacted by this call. MAP will only
164f7cbfa71SZhenzhong Duan  * allocate within the allowed IOVAs if they are present.
165f7cbfa71SZhenzhong Duan  *
166f7cbfa71SZhenzhong Duan  * This call replaces the entire allowed list with the given list.
167f7cbfa71SZhenzhong Duan  */
168f7cbfa71SZhenzhong Duan struct iommu_ioas_allow_iovas {
169f7cbfa71SZhenzhong Duan 	__u32 size;
170f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
171f7cbfa71SZhenzhong Duan 	__u32 num_iovas;
172f7cbfa71SZhenzhong Duan 	__u32 __reserved;
173f7cbfa71SZhenzhong Duan 	__aligned_u64 allowed_iovas;
174f7cbfa71SZhenzhong Duan };
175f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS)
176f7cbfa71SZhenzhong Duan 
177f7cbfa71SZhenzhong Duan /**
178f7cbfa71SZhenzhong Duan  * enum iommufd_ioas_map_flags - Flags for map and copy
179f7cbfa71SZhenzhong Duan  * @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate
180f7cbfa71SZhenzhong Duan  *                             IOVA to place the mapping at
181f7cbfa71SZhenzhong Duan  * @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping
182f7cbfa71SZhenzhong Duan  * @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping
183f7cbfa71SZhenzhong Duan  */
184f7cbfa71SZhenzhong Duan enum iommufd_ioas_map_flags {
185f7cbfa71SZhenzhong Duan 	IOMMU_IOAS_MAP_FIXED_IOVA = 1 << 0,
186f7cbfa71SZhenzhong Duan 	IOMMU_IOAS_MAP_WRITEABLE = 1 << 1,
187f7cbfa71SZhenzhong Duan 	IOMMU_IOAS_MAP_READABLE = 1 << 2,
188f7cbfa71SZhenzhong Duan };
189f7cbfa71SZhenzhong Duan 
190f7cbfa71SZhenzhong Duan /**
191f7cbfa71SZhenzhong Duan  * struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP)
192f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_map)
193f7cbfa71SZhenzhong Duan  * @flags: Combination of enum iommufd_ioas_map_flags
194f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to change the mapping of
195f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
196f7cbfa71SZhenzhong Duan  * @user_va: Userspace pointer to start mapping from
197f7cbfa71SZhenzhong Duan  * @length: Number of bytes to map
198f7cbfa71SZhenzhong Duan  * @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set
199f7cbfa71SZhenzhong Duan  *        then this must be provided as input.
200f7cbfa71SZhenzhong Duan  *
201f7cbfa71SZhenzhong Duan  * Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the
202f7cbfa71SZhenzhong Duan  * mapping will be established at iova, otherwise a suitable location based on
203f7cbfa71SZhenzhong Duan  * the reserved and allowed lists will be automatically selected and returned in
204f7cbfa71SZhenzhong Duan  * iova.
205f7cbfa71SZhenzhong Duan  *
206f7cbfa71SZhenzhong Duan  * If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently
207f7cbfa71SZhenzhong Duan  * be unused, existing IOVA cannot be replaced.
208f7cbfa71SZhenzhong Duan  */
209f7cbfa71SZhenzhong Duan struct iommu_ioas_map {
210f7cbfa71SZhenzhong Duan 	__u32 size;
211f7cbfa71SZhenzhong Duan 	__u32 flags;
212f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
213f7cbfa71SZhenzhong Duan 	__u32 __reserved;
214f7cbfa71SZhenzhong Duan 	__aligned_u64 user_va;
215f7cbfa71SZhenzhong Duan 	__aligned_u64 length;
216f7cbfa71SZhenzhong Duan 	__aligned_u64 iova;
217f7cbfa71SZhenzhong Duan };
218f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP)
219f7cbfa71SZhenzhong Duan 
220f7cbfa71SZhenzhong Duan /**
221*44fe383cSHendrik Brueckner  * struct iommu_ioas_map_file - ioctl(IOMMU_IOAS_MAP_FILE)
222*44fe383cSHendrik Brueckner  * @size: sizeof(struct iommu_ioas_map_file)
223*44fe383cSHendrik Brueckner  * @flags: same as for iommu_ioas_map
224*44fe383cSHendrik Brueckner  * @ioas_id: same as for iommu_ioas_map
225*44fe383cSHendrik Brueckner  * @fd: the memfd to map
226*44fe383cSHendrik Brueckner  * @start: byte offset from start of file to map from
227*44fe383cSHendrik Brueckner  * @length: same as for iommu_ioas_map
228*44fe383cSHendrik Brueckner  * @iova: same as for iommu_ioas_map
229*44fe383cSHendrik Brueckner  *
230*44fe383cSHendrik Brueckner  * Set an IOVA mapping from a memfd file.  All other arguments and semantics
231*44fe383cSHendrik Brueckner  * match those of IOMMU_IOAS_MAP.
232*44fe383cSHendrik Brueckner  */
233*44fe383cSHendrik Brueckner struct iommu_ioas_map_file {
234*44fe383cSHendrik Brueckner 	__u32 size;
235*44fe383cSHendrik Brueckner 	__u32 flags;
236*44fe383cSHendrik Brueckner 	__u32 ioas_id;
237*44fe383cSHendrik Brueckner 	__s32 fd;
238*44fe383cSHendrik Brueckner 	__aligned_u64 start;
239*44fe383cSHendrik Brueckner 	__aligned_u64 length;
240*44fe383cSHendrik Brueckner 	__aligned_u64 iova;
241*44fe383cSHendrik Brueckner };
242*44fe383cSHendrik Brueckner #define IOMMU_IOAS_MAP_FILE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP_FILE)
243*44fe383cSHendrik Brueckner 
244*44fe383cSHendrik Brueckner /**
245f7cbfa71SZhenzhong Duan  * struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY)
246f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_copy)
247f7cbfa71SZhenzhong Duan  * @flags: Combination of enum iommufd_ioas_map_flags
248f7cbfa71SZhenzhong Duan  * @dst_ioas_id: IOAS ID to change the mapping of
249f7cbfa71SZhenzhong Duan  * @src_ioas_id: IOAS ID to copy from
250f7cbfa71SZhenzhong Duan  * @length: Number of bytes to copy and map
251f7cbfa71SZhenzhong Duan  * @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is
252f7cbfa71SZhenzhong Duan  *            set then this must be provided as input.
253f7cbfa71SZhenzhong Duan  * @src_iova: IOVA to start the copy
254f7cbfa71SZhenzhong Duan  *
255f7cbfa71SZhenzhong Duan  * Copy an already existing mapping from src_ioas_id and establish it in
256f7cbfa71SZhenzhong Duan  * dst_ioas_id. The src iova/length must exactly match a range used with
257f7cbfa71SZhenzhong Duan  * IOMMU_IOAS_MAP.
258f7cbfa71SZhenzhong Duan  *
259f7cbfa71SZhenzhong Duan  * This may be used to efficiently clone a subset of an IOAS to another, or as a
260f7cbfa71SZhenzhong Duan  * kind of 'cache' to speed up mapping. Copy has an efficiency advantage over
261f7cbfa71SZhenzhong Duan  * establishing equivalent new mappings, as internal resources are shared, and
262f7cbfa71SZhenzhong Duan  * the kernel will pin the user memory only once.
263f7cbfa71SZhenzhong Duan  */
264f7cbfa71SZhenzhong Duan struct iommu_ioas_copy {
265f7cbfa71SZhenzhong Duan 	__u32 size;
266f7cbfa71SZhenzhong Duan 	__u32 flags;
267f7cbfa71SZhenzhong Duan 	__u32 dst_ioas_id;
268f7cbfa71SZhenzhong Duan 	__u32 src_ioas_id;
269f7cbfa71SZhenzhong Duan 	__aligned_u64 length;
270f7cbfa71SZhenzhong Duan 	__aligned_u64 dst_iova;
271f7cbfa71SZhenzhong Duan 	__aligned_u64 src_iova;
272f7cbfa71SZhenzhong Duan };
273f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY)
274f7cbfa71SZhenzhong Duan 
275f7cbfa71SZhenzhong Duan /**
276f7cbfa71SZhenzhong Duan  * struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP)
277f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_unmap)
278f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to change the mapping of
279f7cbfa71SZhenzhong Duan  * @iova: IOVA to start the unmapping at
280f7cbfa71SZhenzhong Duan  * @length: Number of bytes to unmap, and return back the bytes unmapped
281f7cbfa71SZhenzhong Duan  *
282f7cbfa71SZhenzhong Duan  * Unmap an IOVA range. The iova/length must be a superset of a previously
283f7cbfa71SZhenzhong Duan  * mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or
284f7cbfa71SZhenzhong Duan  * truncating ranges is not allowed. The values 0 to U64_MAX will unmap
285f7cbfa71SZhenzhong Duan  * everything.
286f7cbfa71SZhenzhong Duan  */
287f7cbfa71SZhenzhong Duan struct iommu_ioas_unmap {
288f7cbfa71SZhenzhong Duan 	__u32 size;
289f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
290f7cbfa71SZhenzhong Duan 	__aligned_u64 iova;
291f7cbfa71SZhenzhong Duan 	__aligned_u64 length;
292f7cbfa71SZhenzhong Duan };
293f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP)
294f7cbfa71SZhenzhong Duan 
295f7cbfa71SZhenzhong Duan /**
296f7cbfa71SZhenzhong Duan  * enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and
297f7cbfa71SZhenzhong Duan  *                       ioctl(IOMMU_OPTION_HUGE_PAGES)
298f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_RLIMIT_MODE:
299f7cbfa71SZhenzhong Duan  *    Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege
300f7cbfa71SZhenzhong Duan  *    to invoke this. Value 0 (default) is user based accouting, 1 uses process
301f7cbfa71SZhenzhong Duan  *    based accounting. Global option, object_id must be 0
302f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_HUGE_PAGES:
303f7cbfa71SZhenzhong Duan  *    Value 1 (default) allows contiguous pages to be combined when generating
304f7cbfa71SZhenzhong Duan  *    iommu mappings. Value 0 disables combining, everything is mapped to
305f7cbfa71SZhenzhong Duan  *    PAGE_SIZE. This can be useful for benchmarking.  This is a per-IOAS
306f7cbfa71SZhenzhong Duan  *    option, the object_id must be the IOAS ID.
307f7cbfa71SZhenzhong Duan  */
308f7cbfa71SZhenzhong Duan enum iommufd_option {
309f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_RLIMIT_MODE = 0,
310f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_HUGE_PAGES = 1,
311f7cbfa71SZhenzhong Duan };
312f7cbfa71SZhenzhong Duan 
313f7cbfa71SZhenzhong Duan /**
314f7cbfa71SZhenzhong Duan  * enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and
315f7cbfa71SZhenzhong Duan  *                           ioctl(IOMMU_OPTION_OP_GET)
316f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_OP_SET: Set the option's value
317f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_OP_GET: Get the option's value
318f7cbfa71SZhenzhong Duan  */
319f7cbfa71SZhenzhong Duan enum iommufd_option_ops {
320f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_OP_SET = 0,
321f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_OP_GET = 1,
322f7cbfa71SZhenzhong Duan };
323f7cbfa71SZhenzhong Duan 
324f7cbfa71SZhenzhong Duan /**
325f7cbfa71SZhenzhong Duan  * struct iommu_option - iommu option multiplexer
326f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_option)
327f7cbfa71SZhenzhong Duan  * @option_id: One of enum iommufd_option
328f7cbfa71SZhenzhong Duan  * @op: One of enum iommufd_option_ops
329f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
330f7cbfa71SZhenzhong Duan  * @object_id: ID of the object if required
331f7cbfa71SZhenzhong Duan  * @val64: Option value to set or value returned on get
332f7cbfa71SZhenzhong Duan  *
333f7cbfa71SZhenzhong Duan  * Change a simple option value. This multiplexor allows controlling options
334f7cbfa71SZhenzhong Duan  * on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET
335f7cbfa71SZhenzhong Duan  * will return the current value.
336f7cbfa71SZhenzhong Duan  */
337f7cbfa71SZhenzhong Duan struct iommu_option {
338f7cbfa71SZhenzhong Duan 	__u32 size;
339f7cbfa71SZhenzhong Duan 	__u32 option_id;
340f7cbfa71SZhenzhong Duan 	__u16 op;
341f7cbfa71SZhenzhong Duan 	__u16 __reserved;
342f7cbfa71SZhenzhong Duan 	__u32 object_id;
343f7cbfa71SZhenzhong Duan 	__aligned_u64 val64;
344f7cbfa71SZhenzhong Duan };
345f7cbfa71SZhenzhong Duan #define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION)
346f7cbfa71SZhenzhong Duan 
347f7cbfa71SZhenzhong Duan /**
348f7cbfa71SZhenzhong Duan  * enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls
349f7cbfa71SZhenzhong Duan  * @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS
350f7cbfa71SZhenzhong Duan  * @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS
351f7cbfa71SZhenzhong Duan  * @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility
352f7cbfa71SZhenzhong Duan  */
353f7cbfa71SZhenzhong Duan enum iommufd_vfio_ioas_op {
354f7cbfa71SZhenzhong Duan 	IOMMU_VFIO_IOAS_GET = 0,
355f7cbfa71SZhenzhong Duan 	IOMMU_VFIO_IOAS_SET = 1,
356f7cbfa71SZhenzhong Duan 	IOMMU_VFIO_IOAS_CLEAR = 2,
357f7cbfa71SZhenzhong Duan };
358f7cbfa71SZhenzhong Duan 
359f7cbfa71SZhenzhong Duan /**
360f7cbfa71SZhenzhong Duan  * struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS)
361f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_vfio_ioas)
362f7cbfa71SZhenzhong Duan  * @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set
363f7cbfa71SZhenzhong Duan  *           For IOMMU_VFIO_IOAS_GET will output the IOAS ID
364f7cbfa71SZhenzhong Duan  * @op: One of enum iommufd_vfio_ioas_op
365f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
366f7cbfa71SZhenzhong Duan  *
367f7cbfa71SZhenzhong Duan  * The VFIO compatibility support uses a single ioas because VFIO APIs do not
368f7cbfa71SZhenzhong Duan  * support the ID field. Set or Get the IOAS that VFIO compatibility will use.
369f7cbfa71SZhenzhong Duan  * When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the
370f7cbfa71SZhenzhong Duan  * compatibility ioas, either by taking what is already set, or auto creating
371f7cbfa71SZhenzhong Duan  * one. From then on VFIO will continue to use that ioas and is not effected by
372f7cbfa71SZhenzhong Duan  * this ioctl. SET or CLEAR does not destroy any auto-created IOAS.
373f7cbfa71SZhenzhong Duan  */
374f7cbfa71SZhenzhong Duan struct iommu_vfio_ioas {
375f7cbfa71SZhenzhong Duan 	__u32 size;
376f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
377f7cbfa71SZhenzhong Duan 	__u16 op;
378f7cbfa71SZhenzhong Duan 	__u16 __reserved;
379f7cbfa71SZhenzhong Duan };
380f7cbfa71SZhenzhong Duan #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS)
381f7cbfa71SZhenzhong Duan 
382f7cbfa71SZhenzhong Duan /**
383efb91426SDaniel Henrique Barboza  * enum iommufd_hwpt_alloc_flags - Flags for HWPT allocation
384efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_ALLOC_NEST_PARENT: If set, allocate a HWPT that can serve as
385efb91426SDaniel Henrique Barboza  *                                the parent HWPT in a nesting configuration.
386efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_ALLOC_DIRTY_TRACKING: Dirty tracking support for device IOMMU is
387efb91426SDaniel Henrique Barboza  *                                   enforced on device attachment
3880d2eeef7SBibo Mao  * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation data is
3890d2eeef7SBibo Mao  *                             valid.
390*44fe383cSHendrik Brueckner  * @IOMMU_HWPT_ALLOC_PASID: Requests a domain that can be used with PASID. The
391*44fe383cSHendrik Brueckner  *                          domain can be attached to any PASID on the device.
392*44fe383cSHendrik Brueckner  *                          Any domain attached to the non-PASID part of the
393*44fe383cSHendrik Brueckner  *                          device must also be flaged, otherwise attaching a
394*44fe383cSHendrik Brueckner  *                          PASID will blocked.
395*44fe383cSHendrik Brueckner  *                          If IOMMU does not support PASID it will return
396*44fe383cSHendrik Brueckner  *                          error (-EOPNOTSUPP).
397efb91426SDaniel Henrique Barboza  */
398efb91426SDaniel Henrique Barboza enum iommufd_hwpt_alloc_flags {
399efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_ALLOC_NEST_PARENT = 1 << 0,
400efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1,
4010d2eeef7SBibo Mao 	IOMMU_HWPT_FAULT_ID_VALID = 1 << 2,
402*44fe383cSHendrik Brueckner 	IOMMU_HWPT_ALLOC_PASID = 1 << 3,
403efb91426SDaniel Henrique Barboza };
404efb91426SDaniel Henrique Barboza 
405efb91426SDaniel Henrique Barboza /**
406efb91426SDaniel Henrique Barboza  * enum iommu_hwpt_vtd_s1_flags - Intel VT-d stage-1 page table
407efb91426SDaniel Henrique Barboza  *                                entry attributes
408efb91426SDaniel Henrique Barboza  * @IOMMU_VTD_S1_SRE: Supervisor request
409efb91426SDaniel Henrique Barboza  * @IOMMU_VTD_S1_EAFE: Extended access enable
410efb91426SDaniel Henrique Barboza  * @IOMMU_VTD_S1_WPE: Write protect enable
411efb91426SDaniel Henrique Barboza  */
412efb91426SDaniel Henrique Barboza enum iommu_hwpt_vtd_s1_flags {
413efb91426SDaniel Henrique Barboza 	IOMMU_VTD_S1_SRE = 1 << 0,
414efb91426SDaniel Henrique Barboza 	IOMMU_VTD_S1_EAFE = 1 << 1,
415efb91426SDaniel Henrique Barboza 	IOMMU_VTD_S1_WPE = 1 << 2,
416efb91426SDaniel Henrique Barboza };
417efb91426SDaniel Henrique Barboza 
418efb91426SDaniel Henrique Barboza /**
419efb91426SDaniel Henrique Barboza  * struct iommu_hwpt_vtd_s1 - Intel VT-d stage-1 page table
420efb91426SDaniel Henrique Barboza  *                            info (IOMMU_HWPT_DATA_VTD_S1)
421efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommu_hwpt_vtd_s1_flags
422efb91426SDaniel Henrique Barboza  * @pgtbl_addr: The base address of the stage-1 page table.
423efb91426SDaniel Henrique Barboza  * @addr_width: The address width of the stage-1 page table
424efb91426SDaniel Henrique Barboza  * @__reserved: Must be 0
425efb91426SDaniel Henrique Barboza  */
426efb91426SDaniel Henrique Barboza struct iommu_hwpt_vtd_s1 {
427efb91426SDaniel Henrique Barboza 	__aligned_u64 flags;
428efb91426SDaniel Henrique Barboza 	__aligned_u64 pgtbl_addr;
429efb91426SDaniel Henrique Barboza 	__u32 addr_width;
430efb91426SDaniel Henrique Barboza 	__u32 __reserved;
431efb91426SDaniel Henrique Barboza };
432efb91426SDaniel Henrique Barboza 
433efb91426SDaniel Henrique Barboza /**
434*44fe383cSHendrik Brueckner  * struct iommu_hwpt_arm_smmuv3 - ARM SMMUv3 nested STE
435*44fe383cSHendrik Brueckner  *                                (IOMMU_HWPT_DATA_ARM_SMMUV3)
436*44fe383cSHendrik Brueckner  *
437*44fe383cSHendrik Brueckner  * @ste: The first two double words of the user space Stream Table Entry for
438*44fe383cSHendrik Brueckner  *       the translation. Must be little-endian.
439*44fe383cSHendrik Brueckner  *       Allowed fields: (Refer to "5.2 Stream Table Entry" in SMMUv3 HW Spec)
440*44fe383cSHendrik Brueckner  *       - word-0: V, Cfg, S1Fmt, S1ContextPtr, S1CDMax
441*44fe383cSHendrik Brueckner  *       - word-1: EATS, S1DSS, S1CIR, S1COR, S1CSH, S1STALLD
442*44fe383cSHendrik Brueckner  *
443*44fe383cSHendrik Brueckner  * -EIO will be returned if @ste is not legal or contains any non-allowed field.
444*44fe383cSHendrik Brueckner  * Cfg can be used to select a S1, Bypass or Abort configuration. A Bypass
445*44fe383cSHendrik Brueckner  * nested domain will translate the same as the nesting parent. The S1 will
446*44fe383cSHendrik Brueckner  * install a Context Descriptor Table pointing at userspace memory translated
447*44fe383cSHendrik Brueckner  * by the nesting parent.
448*44fe383cSHendrik Brueckner  */
449*44fe383cSHendrik Brueckner struct iommu_hwpt_arm_smmuv3 {
450*44fe383cSHendrik Brueckner 	__aligned_le64 ste[2];
451*44fe383cSHendrik Brueckner };
452*44fe383cSHendrik Brueckner 
453*44fe383cSHendrik Brueckner /**
454efb91426SDaniel Henrique Barboza  * enum iommu_hwpt_data_type - IOMMU HWPT Data Type
455efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_DATA_NONE: no data
456efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table
457*44fe383cSHendrik Brueckner  * @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table
458efb91426SDaniel Henrique Barboza  */
459efb91426SDaniel Henrique Barboza enum iommu_hwpt_data_type {
4600d2eeef7SBibo Mao 	IOMMU_HWPT_DATA_NONE = 0,
4610d2eeef7SBibo Mao 	IOMMU_HWPT_DATA_VTD_S1 = 1,
462*44fe383cSHendrik Brueckner 	IOMMU_HWPT_DATA_ARM_SMMUV3 = 2,
463efb91426SDaniel Henrique Barboza };
464efb91426SDaniel Henrique Barboza 
465efb91426SDaniel Henrique Barboza /**
466f7cbfa71SZhenzhong Duan  * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC)
467f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_hwpt_alloc)
468efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommufd_hwpt_alloc_flags
469f7cbfa71SZhenzhong Duan  * @dev_id: The device to allocate this HWPT for
470*44fe383cSHendrik Brueckner  * @pt_id: The IOAS or HWPT or vIOMMU to connect this HWPT to
471f7cbfa71SZhenzhong Duan  * @out_hwpt_id: The ID of the new HWPT
472f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
473efb91426SDaniel Henrique Barboza  * @data_type: One of enum iommu_hwpt_data_type
474efb91426SDaniel Henrique Barboza  * @data_len: Length of the type specific data
475efb91426SDaniel Henrique Barboza  * @data_uptr: User pointer to the type specific data
4760d2eeef7SBibo Mao  * @fault_id: The ID of IOMMUFD_FAULT object. Valid only if flags field of
4770d2eeef7SBibo Mao  *            IOMMU_HWPT_FAULT_ID_VALID is set.
4780d2eeef7SBibo Mao  * @__reserved2: Padding to 64-bit alignment. Must be 0.
479f7cbfa71SZhenzhong Duan  *
480f7cbfa71SZhenzhong Duan  * Explicitly allocate a hardware page table object. This is the same object
481f7cbfa71SZhenzhong Duan  * type that is returned by iommufd_device_attach() and represents the
482f7cbfa71SZhenzhong Duan  * underlying iommu driver's iommu_domain kernel object.
483f7cbfa71SZhenzhong Duan  *
484efb91426SDaniel Henrique Barboza  * A kernel-managed HWPT will be created with the mappings from the given
485efb91426SDaniel Henrique Barboza  * IOAS via the @pt_id. The @data_type for this allocation must be set to
486efb91426SDaniel Henrique Barboza  * IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a
487efb91426SDaniel Henrique Barboza  * nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags.
488efb91426SDaniel Henrique Barboza  *
489*44fe383cSHendrik Brueckner  * A user-managed nested HWPT will be created from a given vIOMMU (wrapping a
490*44fe383cSHendrik Brueckner  * parent HWPT) or a parent HWPT via @pt_id, in which the parent HWPT must be
491*44fe383cSHendrik Brueckner  * allocated previously via the same ioctl from a given IOAS (@pt_id). In this
492*44fe383cSHendrik Brueckner  * case, the @data_type must be set to a pre-defined type corresponding to an
493*44fe383cSHendrik Brueckner  * I/O page table type supported by the underlying IOMMU hardware. The device
494*44fe383cSHendrik Brueckner  * via @dev_id and the vIOMMU via @pt_id must be associated to the same IOMMU
495*44fe383cSHendrik Brueckner  * instance.
496efb91426SDaniel Henrique Barboza  *
497efb91426SDaniel Henrique Barboza  * If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and
498efb91426SDaniel Henrique Barboza  * @data_uptr should be zero. Otherwise, both @data_len and @data_uptr
499efb91426SDaniel Henrique Barboza  * must be given.
500f7cbfa71SZhenzhong Duan  */
501f7cbfa71SZhenzhong Duan struct iommu_hwpt_alloc {
502f7cbfa71SZhenzhong Duan 	__u32 size;
503f7cbfa71SZhenzhong Duan 	__u32 flags;
504f7cbfa71SZhenzhong Duan 	__u32 dev_id;
505f7cbfa71SZhenzhong Duan 	__u32 pt_id;
506f7cbfa71SZhenzhong Duan 	__u32 out_hwpt_id;
507f7cbfa71SZhenzhong Duan 	__u32 __reserved;
508efb91426SDaniel Henrique Barboza 	__u32 data_type;
509efb91426SDaniel Henrique Barboza 	__u32 data_len;
510efb91426SDaniel Henrique Barboza 	__aligned_u64 data_uptr;
5110d2eeef7SBibo Mao 	__u32 fault_id;
5120d2eeef7SBibo Mao 	__u32 __reserved2;
513f7cbfa71SZhenzhong Duan };
514f7cbfa71SZhenzhong Duan #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC)
515f7cbfa71SZhenzhong Duan 
516f7cbfa71SZhenzhong Duan /**
517efb91426SDaniel Henrique Barboza  * enum iommu_hw_info_vtd_flags - Flags for VT-d hw_info
518efb91426SDaniel Henrique Barboza  * @IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17: If set, disallow read-only mappings
519efb91426SDaniel Henrique Barboza  *                                         on a nested_parent domain.
520efb91426SDaniel Henrique Barboza  *                                         https://www.intel.com/content/www/us/en/content-details/772415/content-details.html
521efb91426SDaniel Henrique Barboza  */
522efb91426SDaniel Henrique Barboza enum iommu_hw_info_vtd_flags {
523efb91426SDaniel Henrique Barboza 	IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 = 1 << 0,
524efb91426SDaniel Henrique Barboza };
525efb91426SDaniel Henrique Barboza 
526efb91426SDaniel Henrique Barboza /**
527f7cbfa71SZhenzhong Duan  * struct iommu_hw_info_vtd - Intel VT-d hardware information
528f7cbfa71SZhenzhong Duan  *
529efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommu_hw_info_vtd_flags
530f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
531f7cbfa71SZhenzhong Duan  *
532f7cbfa71SZhenzhong Duan  * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec
533f7cbfa71SZhenzhong Duan  *           section 11.4.2 Capability Register.
534f7cbfa71SZhenzhong Duan  * @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec
535f7cbfa71SZhenzhong Duan  *            section 11.4.3 Extended Capability Register.
536f7cbfa71SZhenzhong Duan  *
537f7cbfa71SZhenzhong Duan  * User needs to understand the Intel VT-d specification to decode the
538f7cbfa71SZhenzhong Duan  * register value.
539f7cbfa71SZhenzhong Duan  */
540f7cbfa71SZhenzhong Duan struct iommu_hw_info_vtd {
541f7cbfa71SZhenzhong Duan 	__u32 flags;
542f7cbfa71SZhenzhong Duan 	__u32 __reserved;
543f7cbfa71SZhenzhong Duan 	__aligned_u64 cap_reg;
544f7cbfa71SZhenzhong Duan 	__aligned_u64 ecap_reg;
545f7cbfa71SZhenzhong Duan };
546f7cbfa71SZhenzhong Duan 
547f7cbfa71SZhenzhong Duan /**
548*44fe383cSHendrik Brueckner  * struct iommu_hw_info_arm_smmuv3 - ARM SMMUv3 hardware information
549*44fe383cSHendrik Brueckner  *                                   (IOMMU_HW_INFO_TYPE_ARM_SMMUV3)
550*44fe383cSHendrik Brueckner  *
551*44fe383cSHendrik Brueckner  * @flags: Must be set to 0
552*44fe383cSHendrik Brueckner  * @__reserved: Must be 0
553*44fe383cSHendrik Brueckner  * @idr: Implemented features for ARM SMMU Non-secure programming interface
554*44fe383cSHendrik Brueckner  * @iidr: Information about the implementation and implementer of ARM SMMU,
555*44fe383cSHendrik Brueckner  *        and architecture version supported
556*44fe383cSHendrik Brueckner  * @aidr: ARM SMMU architecture version
557*44fe383cSHendrik Brueckner  *
558*44fe383cSHendrik Brueckner  * For the details of @idr, @iidr and @aidr, please refer to the chapters
559*44fe383cSHendrik Brueckner  * from 6.3.1 to 6.3.6 in the SMMUv3 Spec.
560*44fe383cSHendrik Brueckner  *
561*44fe383cSHendrik Brueckner  * User space should read the underlying ARM SMMUv3 hardware information for
562*44fe383cSHendrik Brueckner  * the list of supported features.
563*44fe383cSHendrik Brueckner  *
564*44fe383cSHendrik Brueckner  * Note that these values reflect the raw HW capability, without any insight if
565*44fe383cSHendrik Brueckner  * any required kernel driver support is present. Bits may be set indicating the
566*44fe383cSHendrik Brueckner  * HW has functionality that is lacking kernel software support, such as BTM. If
567*44fe383cSHendrik Brueckner  * a VMM is using this information to construct emulated copies of these
568*44fe383cSHendrik Brueckner  * registers it should only forward bits that it knows it can support.
569*44fe383cSHendrik Brueckner  *
570*44fe383cSHendrik Brueckner  * In future, presence of required kernel support will be indicated in flags.
571*44fe383cSHendrik Brueckner  */
572*44fe383cSHendrik Brueckner struct iommu_hw_info_arm_smmuv3 {
573*44fe383cSHendrik Brueckner 	__u32 flags;
574*44fe383cSHendrik Brueckner 	__u32 __reserved;
575*44fe383cSHendrik Brueckner 	__u32 idr[6];
576*44fe383cSHendrik Brueckner 	__u32 iidr;
577*44fe383cSHendrik Brueckner 	__u32 aidr;
578*44fe383cSHendrik Brueckner };
579*44fe383cSHendrik Brueckner 
580*44fe383cSHendrik Brueckner /**
581f7cbfa71SZhenzhong Duan  * enum iommu_hw_info_type - IOMMU Hardware Info Types
582f7cbfa71SZhenzhong Duan  * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not report hardware
583f7cbfa71SZhenzhong Duan  *                           info
584f7cbfa71SZhenzhong Duan  * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
585*44fe383cSHendrik Brueckner  * @IOMMU_HW_INFO_TYPE_ARM_SMMUV3: ARM SMMUv3 iommu info type
586f7cbfa71SZhenzhong Duan  */
587f7cbfa71SZhenzhong Duan enum iommu_hw_info_type {
5880d2eeef7SBibo Mao 	IOMMU_HW_INFO_TYPE_NONE = 0,
5890d2eeef7SBibo Mao 	IOMMU_HW_INFO_TYPE_INTEL_VTD = 1,
590*44fe383cSHendrik Brueckner 	IOMMU_HW_INFO_TYPE_ARM_SMMUV3 = 2,
591f7cbfa71SZhenzhong Duan };
592f7cbfa71SZhenzhong Duan 
593f7cbfa71SZhenzhong Duan /**
594efb91426SDaniel Henrique Barboza  * enum iommufd_hw_capabilities
595efb91426SDaniel Henrique Barboza  * @IOMMU_HW_CAP_DIRTY_TRACKING: IOMMU hardware support for dirty tracking
596efb91426SDaniel Henrique Barboza  *                               If available, it means the following APIs
597efb91426SDaniel Henrique Barboza  *                               are supported:
598efb91426SDaniel Henrique Barboza  *
599efb91426SDaniel Henrique Barboza  *                                   IOMMU_HWPT_GET_DIRTY_BITMAP
600efb91426SDaniel Henrique Barboza  *                                   IOMMU_HWPT_SET_DIRTY_TRACKING
601efb91426SDaniel Henrique Barboza  *
602efb91426SDaniel Henrique Barboza  */
603efb91426SDaniel Henrique Barboza enum iommufd_hw_capabilities {
604efb91426SDaniel Henrique Barboza 	IOMMU_HW_CAP_DIRTY_TRACKING = 1 << 0,
605efb91426SDaniel Henrique Barboza };
606efb91426SDaniel Henrique Barboza 
607efb91426SDaniel Henrique Barboza /**
608f7cbfa71SZhenzhong Duan  * struct iommu_hw_info - ioctl(IOMMU_GET_HW_INFO)
609f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_hw_info)
610f7cbfa71SZhenzhong Duan  * @flags: Must be 0
611f7cbfa71SZhenzhong Duan  * @dev_id: The device bound to the iommufd
612f7cbfa71SZhenzhong Duan  * @data_len: Input the length of a user buffer in bytes. Output the length of
613f7cbfa71SZhenzhong Duan  *            data that kernel supports
614f7cbfa71SZhenzhong Duan  * @data_uptr: User pointer to a user-space buffer used by the kernel to fill
615f7cbfa71SZhenzhong Duan  *             the iommu type specific hardware information data
616f7cbfa71SZhenzhong Duan  * @out_data_type: Output the iommu hardware info type as defined in the enum
617f7cbfa71SZhenzhong Duan  *                 iommu_hw_info_type.
618efb91426SDaniel Henrique Barboza  * @out_capabilities: Output the generic iommu capability info type as defined
619efb91426SDaniel Henrique Barboza  *                    in the enum iommu_hw_capabilities.
620f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
621f7cbfa71SZhenzhong Duan  *
622f7cbfa71SZhenzhong Duan  * Query an iommu type specific hardware information data from an iommu behind
623f7cbfa71SZhenzhong Duan  * a given device that has been bound to iommufd. This hardware info data will
624f7cbfa71SZhenzhong Duan  * be used to sync capabilities between the virtual iommu and the physical
625f7cbfa71SZhenzhong Duan  * iommu, e.g. a nested translation setup needs to check the hardware info, so
626f7cbfa71SZhenzhong Duan  * a guest stage-1 page table can be compatible with the physical iommu.
627f7cbfa71SZhenzhong Duan  *
628f7cbfa71SZhenzhong Duan  * To capture an iommu type specific hardware information data, @data_uptr and
629f7cbfa71SZhenzhong Duan  * its length @data_len must be provided. Trailing bytes will be zeroed if the
630f7cbfa71SZhenzhong Duan  * user buffer is larger than the data that kernel has. Otherwise, kernel only
631f7cbfa71SZhenzhong Duan  * fills the buffer using the given length in @data_len. If the ioctl succeeds,
632f7cbfa71SZhenzhong Duan  * @data_len will be updated to the length that kernel actually supports,
633f7cbfa71SZhenzhong Duan  * @out_data_type will be filled to decode the data filled in the buffer
634f7cbfa71SZhenzhong Duan  * pointed by @data_uptr. Input @data_len == zero is allowed.
635f7cbfa71SZhenzhong Duan  */
636f7cbfa71SZhenzhong Duan struct iommu_hw_info {
637f7cbfa71SZhenzhong Duan 	__u32 size;
638f7cbfa71SZhenzhong Duan 	__u32 flags;
639f7cbfa71SZhenzhong Duan 	__u32 dev_id;
640f7cbfa71SZhenzhong Duan 	__u32 data_len;
641f7cbfa71SZhenzhong Duan 	__aligned_u64 data_uptr;
642f7cbfa71SZhenzhong Duan 	__u32 out_data_type;
643f7cbfa71SZhenzhong Duan 	__u32 __reserved;
644efb91426SDaniel Henrique Barboza 	__aligned_u64 out_capabilities;
645f7cbfa71SZhenzhong Duan };
646f7cbfa71SZhenzhong Duan #define IOMMU_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_GET_HW_INFO)
647efb91426SDaniel Henrique Barboza 
648efb91426SDaniel Henrique Barboza /*
649efb91426SDaniel Henrique Barboza  * enum iommufd_hwpt_set_dirty_tracking_flags - Flags for steering dirty
650efb91426SDaniel Henrique Barboza  *                                              tracking
651efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_DIRTY_TRACKING_ENABLE: Enable dirty tracking
652efb91426SDaniel Henrique Barboza  */
653efb91426SDaniel Henrique Barboza enum iommufd_hwpt_set_dirty_tracking_flags {
654efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_DIRTY_TRACKING_ENABLE = 1,
655efb91426SDaniel Henrique Barboza };
656efb91426SDaniel Henrique Barboza 
657efb91426SDaniel Henrique Barboza /**
658efb91426SDaniel Henrique Barboza  * struct iommu_hwpt_set_dirty_tracking - ioctl(IOMMU_HWPT_SET_DIRTY_TRACKING)
659efb91426SDaniel Henrique Barboza  * @size: sizeof(struct iommu_hwpt_set_dirty_tracking)
660efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommufd_hwpt_set_dirty_tracking_flags
661efb91426SDaniel Henrique Barboza  * @hwpt_id: HW pagetable ID that represents the IOMMU domain
662efb91426SDaniel Henrique Barboza  * @__reserved: Must be 0
663efb91426SDaniel Henrique Barboza  *
664efb91426SDaniel Henrique Barboza  * Toggle dirty tracking on an HW pagetable.
665efb91426SDaniel Henrique Barboza  */
666efb91426SDaniel Henrique Barboza struct iommu_hwpt_set_dirty_tracking {
667efb91426SDaniel Henrique Barboza 	__u32 size;
668efb91426SDaniel Henrique Barboza 	__u32 flags;
669efb91426SDaniel Henrique Barboza 	__u32 hwpt_id;
670efb91426SDaniel Henrique Barboza 	__u32 __reserved;
671efb91426SDaniel Henrique Barboza };
672efb91426SDaniel Henrique Barboza #define IOMMU_HWPT_SET_DIRTY_TRACKING _IO(IOMMUFD_TYPE, \
673efb91426SDaniel Henrique Barboza 					  IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING)
674efb91426SDaniel Henrique Barboza 
675efb91426SDaniel Henrique Barboza /**
676efb91426SDaniel Henrique Barboza  * enum iommufd_hwpt_get_dirty_bitmap_flags - Flags for getting dirty bits
677efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR: Just read the PTEs without clearing
678efb91426SDaniel Henrique Barboza  *                                        any dirty bits metadata. This flag
679efb91426SDaniel Henrique Barboza  *                                        can be passed in the expectation
680efb91426SDaniel Henrique Barboza  *                                        where the next operation is an unmap
681efb91426SDaniel Henrique Barboza  *                                        of the same IOVA range.
682efb91426SDaniel Henrique Barboza  *
683efb91426SDaniel Henrique Barboza  */
684efb91426SDaniel Henrique Barboza enum iommufd_hwpt_get_dirty_bitmap_flags {
685efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR = 1,
686efb91426SDaniel Henrique Barboza };
687efb91426SDaniel Henrique Barboza 
688efb91426SDaniel Henrique Barboza /**
689efb91426SDaniel Henrique Barboza  * struct iommu_hwpt_get_dirty_bitmap - ioctl(IOMMU_HWPT_GET_DIRTY_BITMAP)
690efb91426SDaniel Henrique Barboza  * @size: sizeof(struct iommu_hwpt_get_dirty_bitmap)
691efb91426SDaniel Henrique Barboza  * @hwpt_id: HW pagetable ID that represents the IOMMU domain
692efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommufd_hwpt_get_dirty_bitmap_flags
693efb91426SDaniel Henrique Barboza  * @__reserved: Must be 0
694efb91426SDaniel Henrique Barboza  * @iova: base IOVA of the bitmap first bit
695efb91426SDaniel Henrique Barboza  * @length: IOVA range size
696efb91426SDaniel Henrique Barboza  * @page_size: page size granularity of each bit in the bitmap
697efb91426SDaniel Henrique Barboza  * @data: bitmap where to set the dirty bits. The bitmap bits each
698efb91426SDaniel Henrique Barboza  *        represent a page_size which you deviate from an arbitrary iova.
699efb91426SDaniel Henrique Barboza  *
700efb91426SDaniel Henrique Barboza  * Checking a given IOVA is dirty:
701efb91426SDaniel Henrique Barboza  *
702efb91426SDaniel Henrique Barboza  *  data[(iova / page_size) / 64] & (1ULL << ((iova / page_size) % 64))
703efb91426SDaniel Henrique Barboza  *
704efb91426SDaniel Henrique Barboza  * Walk the IOMMU pagetables for a given IOVA range to return a bitmap
705efb91426SDaniel Henrique Barboza  * with the dirty IOVAs. In doing so it will also by default clear any
706efb91426SDaniel Henrique Barboza  * dirty bit metadata set in the IOPTE.
707efb91426SDaniel Henrique Barboza  */
708efb91426SDaniel Henrique Barboza struct iommu_hwpt_get_dirty_bitmap {
709efb91426SDaniel Henrique Barboza 	__u32 size;
710efb91426SDaniel Henrique Barboza 	__u32 hwpt_id;
711efb91426SDaniel Henrique Barboza 	__u32 flags;
712efb91426SDaniel Henrique Barboza 	__u32 __reserved;
713efb91426SDaniel Henrique Barboza 	__aligned_u64 iova;
714efb91426SDaniel Henrique Barboza 	__aligned_u64 length;
715efb91426SDaniel Henrique Barboza 	__aligned_u64 page_size;
716efb91426SDaniel Henrique Barboza 	__aligned_u64 data;
717efb91426SDaniel Henrique Barboza };
718efb91426SDaniel Henrique Barboza #define IOMMU_HWPT_GET_DIRTY_BITMAP _IO(IOMMUFD_TYPE, \
719efb91426SDaniel Henrique Barboza 					IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP)
720efb91426SDaniel Henrique Barboza 
7216a02465fSDaniel Henrique Barboza /**
7226a02465fSDaniel Henrique Barboza  * enum iommu_hwpt_invalidate_data_type - IOMMU HWPT Cache Invalidation
7236a02465fSDaniel Henrique Barboza  *                                        Data Type
7246a02465fSDaniel Henrique Barboza  * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1
725*44fe383cSHendrik Brueckner  * @IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3: Invalidation data for ARM SMMUv3
7266a02465fSDaniel Henrique Barboza  */
7276a02465fSDaniel Henrique Barboza enum iommu_hwpt_invalidate_data_type {
7280d2eeef7SBibo Mao 	IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 = 0,
729*44fe383cSHendrik Brueckner 	IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3 = 1,
7306a02465fSDaniel Henrique Barboza };
7316a02465fSDaniel Henrique Barboza 
7326a02465fSDaniel Henrique Barboza /**
7336a02465fSDaniel Henrique Barboza  * enum iommu_hwpt_vtd_s1_invalidate_flags - Flags for Intel VT-d
7346a02465fSDaniel Henrique Barboza  *                                           stage-1 cache invalidation
7356a02465fSDaniel Henrique Barboza  * @IOMMU_VTD_INV_FLAGS_LEAF: Indicates whether the invalidation applies
7366a02465fSDaniel Henrique Barboza  *                            to all-levels page structure cache or just
7376a02465fSDaniel Henrique Barboza  *                            the leaf PTE cache.
7386a02465fSDaniel Henrique Barboza  */
7396a02465fSDaniel Henrique Barboza enum iommu_hwpt_vtd_s1_invalidate_flags {
7406a02465fSDaniel Henrique Barboza 	IOMMU_VTD_INV_FLAGS_LEAF = 1 << 0,
7416a02465fSDaniel Henrique Barboza };
7426a02465fSDaniel Henrique Barboza 
7436a02465fSDaniel Henrique Barboza /**
7446a02465fSDaniel Henrique Barboza  * struct iommu_hwpt_vtd_s1_invalidate - Intel VT-d cache invalidation
7456a02465fSDaniel Henrique Barboza  *                                       (IOMMU_HWPT_INVALIDATE_DATA_VTD_S1)
7466a02465fSDaniel Henrique Barboza  * @addr: The start address of the range to be invalidated. It needs to
7476a02465fSDaniel Henrique Barboza  *        be 4KB aligned.
7486a02465fSDaniel Henrique Barboza  * @npages: Number of contiguous 4K pages to be invalidated.
7496a02465fSDaniel Henrique Barboza  * @flags: Combination of enum iommu_hwpt_vtd_s1_invalidate_flags
7506a02465fSDaniel Henrique Barboza  * @__reserved: Must be 0
7516a02465fSDaniel Henrique Barboza  *
7526a02465fSDaniel Henrique Barboza  * The Intel VT-d specific invalidation data for user-managed stage-1 cache
7536a02465fSDaniel Henrique Barboza  * invalidation in nested translation. Userspace uses this structure to
7546a02465fSDaniel Henrique Barboza  * tell the impacted cache scope after modifying the stage-1 page table.
7556a02465fSDaniel Henrique Barboza  *
7566a02465fSDaniel Henrique Barboza  * Invalidating all the caches related to the page table by setting @addr
7576a02465fSDaniel Henrique Barboza  * to be 0 and @npages to be U64_MAX.
7586a02465fSDaniel Henrique Barboza  *
7596a02465fSDaniel Henrique Barboza  * The device TLB will be invalidated automatically if ATS is enabled.
7606a02465fSDaniel Henrique Barboza  */
7616a02465fSDaniel Henrique Barboza struct iommu_hwpt_vtd_s1_invalidate {
7626a02465fSDaniel Henrique Barboza 	__aligned_u64 addr;
7636a02465fSDaniel Henrique Barboza 	__aligned_u64 npages;
7646a02465fSDaniel Henrique Barboza 	__u32 flags;
7656a02465fSDaniel Henrique Barboza 	__u32 __reserved;
7666a02465fSDaniel Henrique Barboza };
7676a02465fSDaniel Henrique Barboza 
7686a02465fSDaniel Henrique Barboza /**
769*44fe383cSHendrik Brueckner  * struct iommu_viommu_arm_smmuv3_invalidate - ARM SMMUv3 cahce invalidation
770*44fe383cSHendrik Brueckner  *         (IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3)
771*44fe383cSHendrik Brueckner  * @cmd: 128-bit cache invalidation command that runs in SMMU CMDQ.
772*44fe383cSHendrik Brueckner  *       Must be little-endian.
773*44fe383cSHendrik Brueckner  *
774*44fe383cSHendrik Brueckner  * Supported command list only when passing in a vIOMMU via @hwpt_id:
775*44fe383cSHendrik Brueckner  *     CMDQ_OP_TLBI_NSNH_ALL
776*44fe383cSHendrik Brueckner  *     CMDQ_OP_TLBI_NH_VA
777*44fe383cSHendrik Brueckner  *     CMDQ_OP_TLBI_NH_VAA
778*44fe383cSHendrik Brueckner  *     CMDQ_OP_TLBI_NH_ALL
779*44fe383cSHendrik Brueckner  *     CMDQ_OP_TLBI_NH_ASID
780*44fe383cSHendrik Brueckner  *     CMDQ_OP_ATC_INV
781*44fe383cSHendrik Brueckner  *     CMDQ_OP_CFGI_CD
782*44fe383cSHendrik Brueckner  *     CMDQ_OP_CFGI_CD_ALL
783*44fe383cSHendrik Brueckner  *
784*44fe383cSHendrik Brueckner  * -EIO will be returned if the command is not supported.
785*44fe383cSHendrik Brueckner  */
786*44fe383cSHendrik Brueckner struct iommu_viommu_arm_smmuv3_invalidate {
787*44fe383cSHendrik Brueckner 	__aligned_le64 cmd[2];
788*44fe383cSHendrik Brueckner };
789*44fe383cSHendrik Brueckner 
790*44fe383cSHendrik Brueckner /**
7916a02465fSDaniel Henrique Barboza  * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE)
7926a02465fSDaniel Henrique Barboza  * @size: sizeof(struct iommu_hwpt_invalidate)
793*44fe383cSHendrik Brueckner  * @hwpt_id: ID of a nested HWPT or a vIOMMU, for cache invalidation
7946a02465fSDaniel Henrique Barboza  * @data_uptr: User pointer to an array of driver-specific cache invalidation
7956a02465fSDaniel Henrique Barboza  *             data.
7966a02465fSDaniel Henrique Barboza  * @data_type: One of enum iommu_hwpt_invalidate_data_type, defining the data
7976a02465fSDaniel Henrique Barboza  *             type of all the entries in the invalidation request array. It
7986a02465fSDaniel Henrique Barboza  *             should be a type supported by the hwpt pointed by @hwpt_id.
7996a02465fSDaniel Henrique Barboza  * @entry_len: Length (in bytes) of a request entry in the request array
8006a02465fSDaniel Henrique Barboza  * @entry_num: Input the number of cache invalidation requests in the array.
8016a02465fSDaniel Henrique Barboza  *             Output the number of requests successfully handled by kernel.
8026a02465fSDaniel Henrique Barboza  * @__reserved: Must be 0.
8036a02465fSDaniel Henrique Barboza  *
804*44fe383cSHendrik Brueckner  * Invalidate iommu cache for user-managed page table or vIOMMU. Modifications
805*44fe383cSHendrik Brueckner  * on a user-managed page table should be followed by this operation, if a HWPT
806*44fe383cSHendrik Brueckner  * is passed in via @hwpt_id. Other caches, such as device cache or descriptor
807*44fe383cSHendrik Brueckner  * cache can be flushed if a vIOMMU is passed in via the @hwpt_id field.
808*44fe383cSHendrik Brueckner  *
8096a02465fSDaniel Henrique Barboza  * Each ioctl can support one or more cache invalidation requests in the array
8106a02465fSDaniel Henrique Barboza  * that has a total size of @entry_len * @entry_num.
8116a02465fSDaniel Henrique Barboza  *
8126a02465fSDaniel Henrique Barboza  * An empty invalidation request array by setting @entry_num==0 is allowed, and
8136a02465fSDaniel Henrique Barboza  * @entry_len and @data_uptr would be ignored in this case. This can be used to
8146a02465fSDaniel Henrique Barboza  * check if the given @data_type is supported or not by kernel.
8156a02465fSDaniel Henrique Barboza  */
8166a02465fSDaniel Henrique Barboza struct iommu_hwpt_invalidate {
8176a02465fSDaniel Henrique Barboza 	__u32 size;
8186a02465fSDaniel Henrique Barboza 	__u32 hwpt_id;
8196a02465fSDaniel Henrique Barboza 	__aligned_u64 data_uptr;
8206a02465fSDaniel Henrique Barboza 	__u32 data_type;
8216a02465fSDaniel Henrique Barboza 	__u32 entry_len;
8226a02465fSDaniel Henrique Barboza 	__u32 entry_num;
8236a02465fSDaniel Henrique Barboza 	__u32 __reserved;
8246a02465fSDaniel Henrique Barboza };
8256a02465fSDaniel Henrique Barboza #define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE)
8260d2eeef7SBibo Mao 
8270d2eeef7SBibo Mao /**
8280d2eeef7SBibo Mao  * enum iommu_hwpt_pgfault_flags - flags for struct iommu_hwpt_pgfault
8290d2eeef7SBibo Mao  * @IOMMU_PGFAULT_FLAGS_PASID_VALID: The pasid field of the fault data is
8300d2eeef7SBibo Mao  *                                   valid.
8310d2eeef7SBibo Mao  * @IOMMU_PGFAULT_FLAGS_LAST_PAGE: It's the last fault of a fault group.
8320d2eeef7SBibo Mao  */
8330d2eeef7SBibo Mao enum iommu_hwpt_pgfault_flags {
8340d2eeef7SBibo Mao 	IOMMU_PGFAULT_FLAGS_PASID_VALID		= (1 << 0),
8350d2eeef7SBibo Mao 	IOMMU_PGFAULT_FLAGS_LAST_PAGE		= (1 << 1),
8360d2eeef7SBibo Mao };
8370d2eeef7SBibo Mao 
8380d2eeef7SBibo Mao /**
8390d2eeef7SBibo Mao  * enum iommu_hwpt_pgfault_perm - perm bits for struct iommu_hwpt_pgfault
8400d2eeef7SBibo Mao  * @IOMMU_PGFAULT_PERM_READ: request for read permission
8410d2eeef7SBibo Mao  * @IOMMU_PGFAULT_PERM_WRITE: request for write permission
8420d2eeef7SBibo Mao  * @IOMMU_PGFAULT_PERM_EXEC: (PCIE 10.4.1) request with a PASID that has the
8430d2eeef7SBibo Mao  *                           Execute Requested bit set in PASID TLP Prefix.
8440d2eeef7SBibo Mao  * @IOMMU_PGFAULT_PERM_PRIV: (PCIE 10.4.1) request with a PASID that has the
8450d2eeef7SBibo Mao  *                           Privileged Mode Requested bit set in PASID TLP
8460d2eeef7SBibo Mao  *                           Prefix.
8470d2eeef7SBibo Mao  */
8480d2eeef7SBibo Mao enum iommu_hwpt_pgfault_perm {
8490d2eeef7SBibo Mao 	IOMMU_PGFAULT_PERM_READ			= (1 << 0),
8500d2eeef7SBibo Mao 	IOMMU_PGFAULT_PERM_WRITE		= (1 << 1),
8510d2eeef7SBibo Mao 	IOMMU_PGFAULT_PERM_EXEC			= (1 << 2),
8520d2eeef7SBibo Mao 	IOMMU_PGFAULT_PERM_PRIV			= (1 << 3),
8530d2eeef7SBibo Mao };
8540d2eeef7SBibo Mao 
8550d2eeef7SBibo Mao /**
8560d2eeef7SBibo Mao  * struct iommu_hwpt_pgfault - iommu page fault data
8570d2eeef7SBibo Mao  * @flags: Combination of enum iommu_hwpt_pgfault_flags
8580d2eeef7SBibo Mao  * @dev_id: id of the originated device
8590d2eeef7SBibo Mao  * @pasid: Process Address Space ID
8600d2eeef7SBibo Mao  * @grpid: Page Request Group Index
8610d2eeef7SBibo Mao  * @perm: Combination of enum iommu_hwpt_pgfault_perm
8620d2eeef7SBibo Mao  * @addr: Fault address
8630d2eeef7SBibo Mao  * @length: a hint of how much data the requestor is expecting to fetch. For
8640d2eeef7SBibo Mao  *          example, if the PRI initiator knows it is going to do a 10MB
8650d2eeef7SBibo Mao  *          transfer, it could fill in 10MB and the OS could pre-fault in
8660d2eeef7SBibo Mao  *          10MB of IOVA. It's default to 0 if there's no such hint.
8670d2eeef7SBibo Mao  * @cookie: kernel-managed cookie identifying a group of fault messages. The
8680d2eeef7SBibo Mao  *          cookie number encoded in the last page fault of the group should
8690d2eeef7SBibo Mao  *          be echoed back in the response message.
8700d2eeef7SBibo Mao  */
8710d2eeef7SBibo Mao struct iommu_hwpt_pgfault {
8720d2eeef7SBibo Mao 	__u32 flags;
8730d2eeef7SBibo Mao 	__u32 dev_id;
8740d2eeef7SBibo Mao 	__u32 pasid;
8750d2eeef7SBibo Mao 	__u32 grpid;
8760d2eeef7SBibo Mao 	__u32 perm;
8770d2eeef7SBibo Mao 	__u64 addr;
8780d2eeef7SBibo Mao 	__u32 length;
8790d2eeef7SBibo Mao 	__u32 cookie;
8800d2eeef7SBibo Mao };
8810d2eeef7SBibo Mao 
8820d2eeef7SBibo Mao /**
8830d2eeef7SBibo Mao  * enum iommufd_page_response_code - Return status of fault handlers
8840d2eeef7SBibo Mao  * @IOMMUFD_PAGE_RESP_SUCCESS: Fault has been handled and the page tables
8850d2eeef7SBibo Mao  *                             populated, retry the access. This is the
8860d2eeef7SBibo Mao  *                             "Success" defined in PCI 10.4.2.1.
8870d2eeef7SBibo Mao  * @IOMMUFD_PAGE_RESP_INVALID: Could not handle this fault, don't retry the
8880d2eeef7SBibo Mao  *                             access. This is the "Invalid Request" in PCI
8890d2eeef7SBibo Mao  *                             10.4.2.1.
8900d2eeef7SBibo Mao  */
8910d2eeef7SBibo Mao enum iommufd_page_response_code {
8920d2eeef7SBibo Mao 	IOMMUFD_PAGE_RESP_SUCCESS = 0,
8930d2eeef7SBibo Mao 	IOMMUFD_PAGE_RESP_INVALID = 1,
8940d2eeef7SBibo Mao };
8950d2eeef7SBibo Mao 
8960d2eeef7SBibo Mao /**
8970d2eeef7SBibo Mao  * struct iommu_hwpt_page_response - IOMMU page fault response
8980d2eeef7SBibo Mao  * @cookie: The kernel-managed cookie reported in the fault message.
8990d2eeef7SBibo Mao  * @code: One of response code in enum iommufd_page_response_code.
9000d2eeef7SBibo Mao  */
9010d2eeef7SBibo Mao struct iommu_hwpt_page_response {
9020d2eeef7SBibo Mao 	__u32 cookie;
9030d2eeef7SBibo Mao 	__u32 code;
9040d2eeef7SBibo Mao };
9050d2eeef7SBibo Mao 
9060d2eeef7SBibo Mao /**
9070d2eeef7SBibo Mao  * struct iommu_fault_alloc - ioctl(IOMMU_FAULT_QUEUE_ALLOC)
9080d2eeef7SBibo Mao  * @size: sizeof(struct iommu_fault_alloc)
9090d2eeef7SBibo Mao  * @flags: Must be 0
9100d2eeef7SBibo Mao  * @out_fault_id: The ID of the new FAULT
9110d2eeef7SBibo Mao  * @out_fault_fd: The fd of the new FAULT
9120d2eeef7SBibo Mao  *
9130d2eeef7SBibo Mao  * Explicitly allocate a fault handling object.
9140d2eeef7SBibo Mao  */
9150d2eeef7SBibo Mao struct iommu_fault_alloc {
9160d2eeef7SBibo Mao 	__u32 size;
9170d2eeef7SBibo Mao 	__u32 flags;
9180d2eeef7SBibo Mao 	__u32 out_fault_id;
9190d2eeef7SBibo Mao 	__u32 out_fault_fd;
9200d2eeef7SBibo Mao };
9210d2eeef7SBibo Mao #define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC)
922*44fe383cSHendrik Brueckner 
923*44fe383cSHendrik Brueckner /**
924*44fe383cSHendrik Brueckner  * enum iommu_viommu_type - Virtual IOMMU Type
925*44fe383cSHendrik Brueckner  * @IOMMU_VIOMMU_TYPE_DEFAULT: Reserved for future use
926*44fe383cSHendrik Brueckner  * @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type
927*44fe383cSHendrik Brueckner  */
928*44fe383cSHendrik Brueckner enum iommu_viommu_type {
929*44fe383cSHendrik Brueckner 	IOMMU_VIOMMU_TYPE_DEFAULT = 0,
930*44fe383cSHendrik Brueckner 	IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1,
931*44fe383cSHendrik Brueckner };
932*44fe383cSHendrik Brueckner 
933*44fe383cSHendrik Brueckner /**
934*44fe383cSHendrik Brueckner  * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC)
935*44fe383cSHendrik Brueckner  * @size: sizeof(struct iommu_viommu_alloc)
936*44fe383cSHendrik Brueckner  * @flags: Must be 0
937*44fe383cSHendrik Brueckner  * @type: Type of the virtual IOMMU. Must be defined in enum iommu_viommu_type
938*44fe383cSHendrik Brueckner  * @dev_id: The device's physical IOMMU will be used to back the virtual IOMMU
939*44fe383cSHendrik Brueckner  * @hwpt_id: ID of a nesting parent HWPT to associate to
940*44fe383cSHendrik Brueckner  * @out_viommu_id: Output virtual IOMMU ID for the allocated object
941*44fe383cSHendrik Brueckner  *
942*44fe383cSHendrik Brueckner  * Allocate a virtual IOMMU object, representing the underlying physical IOMMU's
943*44fe383cSHendrik Brueckner  * virtualization support that is a security-isolated slice of the real IOMMU HW
944*44fe383cSHendrik Brueckner  * that is unique to a specific VM. Operations global to the IOMMU are connected
945*44fe383cSHendrik Brueckner  * to the vIOMMU, such as:
946*44fe383cSHendrik Brueckner  * - Security namespace for guest owned ID, e.g. guest-controlled cache tags
947*44fe383cSHendrik Brueckner  * - Non-device-affiliated event reporting, e.g. invalidation queue errors
948*44fe383cSHendrik Brueckner  * - Access to a sharable nesting parent pagetable across physical IOMMUs
949*44fe383cSHendrik Brueckner  * - Virtualization of various platforms IDs, e.g. RIDs and others
950*44fe383cSHendrik Brueckner  * - Delivery of paravirtualized invalidation
951*44fe383cSHendrik Brueckner  * - Direct assigned invalidation queues
952*44fe383cSHendrik Brueckner  * - Direct assigned interrupts
953*44fe383cSHendrik Brueckner  */
954*44fe383cSHendrik Brueckner struct iommu_viommu_alloc {
955*44fe383cSHendrik Brueckner 	__u32 size;
956*44fe383cSHendrik Brueckner 	__u32 flags;
957*44fe383cSHendrik Brueckner 	__u32 type;
958*44fe383cSHendrik Brueckner 	__u32 dev_id;
959*44fe383cSHendrik Brueckner 	__u32 hwpt_id;
960*44fe383cSHendrik Brueckner 	__u32 out_viommu_id;
961*44fe383cSHendrik Brueckner };
962*44fe383cSHendrik Brueckner #define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)
963*44fe383cSHendrik Brueckner 
964*44fe383cSHendrik Brueckner /**
965*44fe383cSHendrik Brueckner  * struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC)
966*44fe383cSHendrik Brueckner  * @size: sizeof(struct iommu_vdevice_alloc)
967*44fe383cSHendrik Brueckner  * @viommu_id: vIOMMU ID to associate with the virtual device
968*44fe383cSHendrik Brueckner  * @dev_id: The physical device to allocate a virtual instance on the vIOMMU
969*44fe383cSHendrik Brueckner  * @out_vdevice_id: Object handle for the vDevice. Pass to IOMMU_DESTORY
970*44fe383cSHendrik Brueckner  * @virt_id: Virtual device ID per vIOMMU, e.g. vSID of ARM SMMUv3, vDeviceID
971*44fe383cSHendrik Brueckner  *           of AMD IOMMU, and vRID of a nested Intel VT-d to a Context Table
972*44fe383cSHendrik Brueckner  *
973*44fe383cSHendrik Brueckner  * Allocate a virtual device instance (for a physical device) against a vIOMMU.
974*44fe383cSHendrik Brueckner  * This instance holds the device's information (related to its vIOMMU) in a VM.
975*44fe383cSHendrik Brueckner  */
976*44fe383cSHendrik Brueckner struct iommu_vdevice_alloc {
977*44fe383cSHendrik Brueckner 	__u32 size;
978*44fe383cSHendrik Brueckner 	__u32 viommu_id;
979*44fe383cSHendrik Brueckner 	__u32 dev_id;
980*44fe383cSHendrik Brueckner 	__u32 out_vdevice_id;
981*44fe383cSHendrik Brueckner 	__aligned_u64 virt_id;
982*44fe383cSHendrik Brueckner };
983*44fe383cSHendrik Brueckner #define IOMMU_VDEVICE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_ALLOC)
984*44fe383cSHendrik Brueckner 
985*44fe383cSHendrik Brueckner /**
986*44fe383cSHendrik Brueckner  * struct iommu_ioas_change_process - ioctl(VFIO_IOAS_CHANGE_PROCESS)
987*44fe383cSHendrik Brueckner  * @size: sizeof(struct iommu_ioas_change_process)
988*44fe383cSHendrik Brueckner  * @__reserved: Must be 0
989*44fe383cSHendrik Brueckner  *
990*44fe383cSHendrik Brueckner  * This transfers pinned memory counts for every memory map in every IOAS
991*44fe383cSHendrik Brueckner  * in the context to the current process.  This only supports maps created
992*44fe383cSHendrik Brueckner  * with IOMMU_IOAS_MAP_FILE, and returns EINVAL if other maps are present.
993*44fe383cSHendrik Brueckner  * If the ioctl returns a failure status, then nothing is changed.
994*44fe383cSHendrik Brueckner  *
995*44fe383cSHendrik Brueckner  * This API is useful for transferring operation of a device from one process
996*44fe383cSHendrik Brueckner  * to another, such as during userland live update.
997*44fe383cSHendrik Brueckner  */
998*44fe383cSHendrik Brueckner struct iommu_ioas_change_process {
999*44fe383cSHendrik Brueckner 	__u32 size;
1000*44fe383cSHendrik Brueckner 	__u32 __reserved;
1001*44fe383cSHendrik Brueckner };
1002*44fe383cSHendrik Brueckner 
1003*44fe383cSHendrik Brueckner #define IOMMU_IOAS_CHANGE_PROCESS \
1004*44fe383cSHendrik Brueckner 	_IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_CHANGE_PROCESS)
1005*44fe383cSHendrik Brueckner 
1006f7cbfa71SZhenzhong Duan #endif
1007