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/types.h>
8f7cbfa71SZhenzhong Duan #include <linux/ioctl.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,
40f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_IOAS_ALLOC,
41f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_IOAS_ALLOW_IOVAS,
42f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_IOAS_COPY,
43f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_IOAS_IOVA_RANGES,
44f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_IOAS_MAP,
45f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_IOAS_UNMAP,
46f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_OPTION,
47f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_VFIO_IOAS,
48f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_HWPT_ALLOC,
49f7cbfa71SZhenzhong Duan 	IOMMUFD_CMD_GET_HW_INFO,
50*efb91426SDaniel Henrique Barboza 	IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING,
51*efb91426SDaniel Henrique Barboza 	IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP,
52f7cbfa71SZhenzhong Duan };
53f7cbfa71SZhenzhong Duan 
54f7cbfa71SZhenzhong Duan /**
55f7cbfa71SZhenzhong Duan  * struct iommu_destroy - ioctl(IOMMU_DESTROY)
56f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_destroy)
57f7cbfa71SZhenzhong Duan  * @id: iommufd object ID to destroy. Can be any destroyable object type.
58f7cbfa71SZhenzhong Duan  *
59f7cbfa71SZhenzhong Duan  * Destroy any object held within iommufd.
60f7cbfa71SZhenzhong Duan  */
61f7cbfa71SZhenzhong Duan struct iommu_destroy {
62f7cbfa71SZhenzhong Duan 	__u32 size;
63f7cbfa71SZhenzhong Duan 	__u32 id;
64f7cbfa71SZhenzhong Duan };
65f7cbfa71SZhenzhong Duan #define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY)
66f7cbfa71SZhenzhong Duan 
67f7cbfa71SZhenzhong Duan /**
68f7cbfa71SZhenzhong Duan  * struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC)
69f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_alloc)
70f7cbfa71SZhenzhong Duan  * @flags: Must be 0
71f7cbfa71SZhenzhong Duan  * @out_ioas_id: Output IOAS ID for the allocated object
72f7cbfa71SZhenzhong Duan  *
73f7cbfa71SZhenzhong Duan  * Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA)
74f7cbfa71SZhenzhong Duan  * to memory mapping.
75f7cbfa71SZhenzhong Duan  */
76f7cbfa71SZhenzhong Duan struct iommu_ioas_alloc {
77f7cbfa71SZhenzhong Duan 	__u32 size;
78f7cbfa71SZhenzhong Duan 	__u32 flags;
79f7cbfa71SZhenzhong Duan 	__u32 out_ioas_id;
80f7cbfa71SZhenzhong Duan };
81f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC)
82f7cbfa71SZhenzhong Duan 
83f7cbfa71SZhenzhong Duan /**
84f7cbfa71SZhenzhong Duan  * struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE)
85f7cbfa71SZhenzhong Duan  * @start: First IOVA
86f7cbfa71SZhenzhong Duan  * @last: Inclusive last IOVA
87f7cbfa71SZhenzhong Duan  *
88f7cbfa71SZhenzhong Duan  * An interval in IOVA space.
89f7cbfa71SZhenzhong Duan  */
90f7cbfa71SZhenzhong Duan struct iommu_iova_range {
91f7cbfa71SZhenzhong Duan 	__aligned_u64 start;
92f7cbfa71SZhenzhong Duan 	__aligned_u64 last;
93f7cbfa71SZhenzhong Duan };
94f7cbfa71SZhenzhong Duan 
95f7cbfa71SZhenzhong Duan /**
96f7cbfa71SZhenzhong Duan  * struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES)
97f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_iova_ranges)
98f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to read ranges from
99f7cbfa71SZhenzhong Duan  * @num_iovas: Input/Output total number of ranges in the IOAS
100f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
101f7cbfa71SZhenzhong Duan  * @allowed_iovas: Pointer to the output array of struct iommu_iova_range
102f7cbfa71SZhenzhong Duan  * @out_iova_alignment: Minimum alignment required for mapping IOVA
103f7cbfa71SZhenzhong Duan  *
104f7cbfa71SZhenzhong Duan  * Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges
105f7cbfa71SZhenzhong Duan  * is not allowed. num_iovas will be set to the total number of iovas and
106f7cbfa71SZhenzhong Duan  * the allowed_iovas[] will be filled in as space permits.
107f7cbfa71SZhenzhong Duan  *
108f7cbfa71SZhenzhong Duan  * The allowed ranges are dependent on the HW path the DMA operation takes, and
109f7cbfa71SZhenzhong Duan  * can change during the lifetime of the IOAS. A fresh empty IOAS will have a
110f7cbfa71SZhenzhong Duan  * full range, and each attached device will narrow the ranges based on that
111f7cbfa71SZhenzhong Duan  * device's HW restrictions. Detaching a device can widen the ranges. Userspace
112f7cbfa71SZhenzhong Duan  * should query ranges after every attach/detach to know what IOVAs are valid
113f7cbfa71SZhenzhong Duan  * for mapping.
114f7cbfa71SZhenzhong Duan  *
115f7cbfa71SZhenzhong Duan  * On input num_iovas is the length of the allowed_iovas array. On output it is
116f7cbfa71SZhenzhong Duan  * the total number of iovas filled in. The ioctl will return -EMSGSIZE and set
117f7cbfa71SZhenzhong Duan  * num_iovas to the required value if num_iovas is too small. In this case the
118f7cbfa71SZhenzhong Duan  * caller should allocate a larger output array and re-issue the ioctl.
119f7cbfa71SZhenzhong Duan  *
120f7cbfa71SZhenzhong Duan  * out_iova_alignment returns the minimum IOVA alignment that can be given
121f7cbfa71SZhenzhong Duan  * to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy::
122f7cbfa71SZhenzhong Duan  *
123f7cbfa71SZhenzhong Duan  *   starting_iova % out_iova_alignment == 0
124f7cbfa71SZhenzhong Duan  *   (starting_iova + length) % out_iova_alignment == 0
125f7cbfa71SZhenzhong Duan  *
126f7cbfa71SZhenzhong Duan  * out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot
127f7cbfa71SZhenzhong Duan  * be higher than the system PAGE_SIZE.
128f7cbfa71SZhenzhong Duan  */
129f7cbfa71SZhenzhong Duan struct iommu_ioas_iova_ranges {
130f7cbfa71SZhenzhong Duan 	__u32 size;
131f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
132f7cbfa71SZhenzhong Duan 	__u32 num_iovas;
133f7cbfa71SZhenzhong Duan 	__u32 __reserved;
134f7cbfa71SZhenzhong Duan 	__aligned_u64 allowed_iovas;
135f7cbfa71SZhenzhong Duan 	__aligned_u64 out_iova_alignment;
136f7cbfa71SZhenzhong Duan };
137f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES)
138f7cbfa71SZhenzhong Duan 
139f7cbfa71SZhenzhong Duan /**
140f7cbfa71SZhenzhong Duan  * struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS)
141f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_allow_iovas)
142f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to allow IOVAs from
143f7cbfa71SZhenzhong Duan  * @num_iovas: Input/Output total number of ranges in the IOAS
144f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
145f7cbfa71SZhenzhong Duan  * @allowed_iovas: Pointer to array of struct iommu_iova_range
146f7cbfa71SZhenzhong Duan  *
147f7cbfa71SZhenzhong Duan  * Ensure a range of IOVAs are always available for allocation. If this call
148f7cbfa71SZhenzhong Duan  * succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges
149f7cbfa71SZhenzhong Duan  * that are narrower than the ranges provided here. This call will fail if
150f7cbfa71SZhenzhong Duan  * IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges.
151f7cbfa71SZhenzhong Duan  *
152f7cbfa71SZhenzhong Duan  * When an IOAS is first created the IOVA_RANGES will be maximally sized, and as
153f7cbfa71SZhenzhong Duan  * devices are attached the IOVA will narrow based on the device restrictions.
154f7cbfa71SZhenzhong Duan  * When an allowed range is specified any narrowing will be refused, ie device
155f7cbfa71SZhenzhong Duan  * attachment can fail if the device requires limiting within the allowed range.
156f7cbfa71SZhenzhong Duan  *
157f7cbfa71SZhenzhong Duan  * Automatic IOVA allocation is also impacted by this call. MAP will only
158f7cbfa71SZhenzhong Duan  * allocate within the allowed IOVAs if they are present.
159f7cbfa71SZhenzhong Duan  *
160f7cbfa71SZhenzhong Duan  * This call replaces the entire allowed list with the given list.
161f7cbfa71SZhenzhong Duan  */
162f7cbfa71SZhenzhong Duan struct iommu_ioas_allow_iovas {
163f7cbfa71SZhenzhong Duan 	__u32 size;
164f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
165f7cbfa71SZhenzhong Duan 	__u32 num_iovas;
166f7cbfa71SZhenzhong Duan 	__u32 __reserved;
167f7cbfa71SZhenzhong Duan 	__aligned_u64 allowed_iovas;
168f7cbfa71SZhenzhong Duan };
169f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS)
170f7cbfa71SZhenzhong Duan 
171f7cbfa71SZhenzhong Duan /**
172f7cbfa71SZhenzhong Duan  * enum iommufd_ioas_map_flags - Flags for map and copy
173f7cbfa71SZhenzhong Duan  * @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate
174f7cbfa71SZhenzhong Duan  *                             IOVA to place the mapping at
175f7cbfa71SZhenzhong Duan  * @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping
176f7cbfa71SZhenzhong Duan  * @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping
177f7cbfa71SZhenzhong Duan  */
178f7cbfa71SZhenzhong Duan enum iommufd_ioas_map_flags {
179f7cbfa71SZhenzhong Duan 	IOMMU_IOAS_MAP_FIXED_IOVA = 1 << 0,
180f7cbfa71SZhenzhong Duan 	IOMMU_IOAS_MAP_WRITEABLE = 1 << 1,
181f7cbfa71SZhenzhong Duan 	IOMMU_IOAS_MAP_READABLE = 1 << 2,
182f7cbfa71SZhenzhong Duan };
183f7cbfa71SZhenzhong Duan 
184f7cbfa71SZhenzhong Duan /**
185f7cbfa71SZhenzhong Duan  * struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP)
186f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_map)
187f7cbfa71SZhenzhong Duan  * @flags: Combination of enum iommufd_ioas_map_flags
188f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to change the mapping of
189f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
190f7cbfa71SZhenzhong Duan  * @user_va: Userspace pointer to start mapping from
191f7cbfa71SZhenzhong Duan  * @length: Number of bytes to map
192f7cbfa71SZhenzhong Duan  * @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set
193f7cbfa71SZhenzhong Duan  *        then this must be provided as input.
194f7cbfa71SZhenzhong Duan  *
195f7cbfa71SZhenzhong Duan  * Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the
196f7cbfa71SZhenzhong Duan  * mapping will be established at iova, otherwise a suitable location based on
197f7cbfa71SZhenzhong Duan  * the reserved and allowed lists will be automatically selected and returned in
198f7cbfa71SZhenzhong Duan  * iova.
199f7cbfa71SZhenzhong Duan  *
200f7cbfa71SZhenzhong Duan  * If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently
201f7cbfa71SZhenzhong Duan  * be unused, existing IOVA cannot be replaced.
202f7cbfa71SZhenzhong Duan  */
203f7cbfa71SZhenzhong Duan struct iommu_ioas_map {
204f7cbfa71SZhenzhong Duan 	__u32 size;
205f7cbfa71SZhenzhong Duan 	__u32 flags;
206f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
207f7cbfa71SZhenzhong Duan 	__u32 __reserved;
208f7cbfa71SZhenzhong Duan 	__aligned_u64 user_va;
209f7cbfa71SZhenzhong Duan 	__aligned_u64 length;
210f7cbfa71SZhenzhong Duan 	__aligned_u64 iova;
211f7cbfa71SZhenzhong Duan };
212f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP)
213f7cbfa71SZhenzhong Duan 
214f7cbfa71SZhenzhong Duan /**
215f7cbfa71SZhenzhong Duan  * struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY)
216f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_copy)
217f7cbfa71SZhenzhong Duan  * @flags: Combination of enum iommufd_ioas_map_flags
218f7cbfa71SZhenzhong Duan  * @dst_ioas_id: IOAS ID to change the mapping of
219f7cbfa71SZhenzhong Duan  * @src_ioas_id: IOAS ID to copy from
220f7cbfa71SZhenzhong Duan  * @length: Number of bytes to copy and map
221f7cbfa71SZhenzhong Duan  * @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is
222f7cbfa71SZhenzhong Duan  *            set then this must be provided as input.
223f7cbfa71SZhenzhong Duan  * @src_iova: IOVA to start the copy
224f7cbfa71SZhenzhong Duan  *
225f7cbfa71SZhenzhong Duan  * Copy an already existing mapping from src_ioas_id and establish it in
226f7cbfa71SZhenzhong Duan  * dst_ioas_id. The src iova/length must exactly match a range used with
227f7cbfa71SZhenzhong Duan  * IOMMU_IOAS_MAP.
228f7cbfa71SZhenzhong Duan  *
229f7cbfa71SZhenzhong Duan  * This may be used to efficiently clone a subset of an IOAS to another, or as a
230f7cbfa71SZhenzhong Duan  * kind of 'cache' to speed up mapping. Copy has an efficiency advantage over
231f7cbfa71SZhenzhong Duan  * establishing equivalent new mappings, as internal resources are shared, and
232f7cbfa71SZhenzhong Duan  * the kernel will pin the user memory only once.
233f7cbfa71SZhenzhong Duan  */
234f7cbfa71SZhenzhong Duan struct iommu_ioas_copy {
235f7cbfa71SZhenzhong Duan 	__u32 size;
236f7cbfa71SZhenzhong Duan 	__u32 flags;
237f7cbfa71SZhenzhong Duan 	__u32 dst_ioas_id;
238f7cbfa71SZhenzhong Duan 	__u32 src_ioas_id;
239f7cbfa71SZhenzhong Duan 	__aligned_u64 length;
240f7cbfa71SZhenzhong Duan 	__aligned_u64 dst_iova;
241f7cbfa71SZhenzhong Duan 	__aligned_u64 src_iova;
242f7cbfa71SZhenzhong Duan };
243f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY)
244f7cbfa71SZhenzhong Duan 
245f7cbfa71SZhenzhong Duan /**
246f7cbfa71SZhenzhong Duan  * struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP)
247f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_ioas_unmap)
248f7cbfa71SZhenzhong Duan  * @ioas_id: IOAS ID to change the mapping of
249f7cbfa71SZhenzhong Duan  * @iova: IOVA to start the unmapping at
250f7cbfa71SZhenzhong Duan  * @length: Number of bytes to unmap, and return back the bytes unmapped
251f7cbfa71SZhenzhong Duan  *
252f7cbfa71SZhenzhong Duan  * Unmap an IOVA range. The iova/length must be a superset of a previously
253f7cbfa71SZhenzhong Duan  * mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or
254f7cbfa71SZhenzhong Duan  * truncating ranges is not allowed. The values 0 to U64_MAX will unmap
255f7cbfa71SZhenzhong Duan  * everything.
256f7cbfa71SZhenzhong Duan  */
257f7cbfa71SZhenzhong Duan struct iommu_ioas_unmap {
258f7cbfa71SZhenzhong Duan 	__u32 size;
259f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
260f7cbfa71SZhenzhong Duan 	__aligned_u64 iova;
261f7cbfa71SZhenzhong Duan 	__aligned_u64 length;
262f7cbfa71SZhenzhong Duan };
263f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP)
264f7cbfa71SZhenzhong Duan 
265f7cbfa71SZhenzhong Duan /**
266f7cbfa71SZhenzhong Duan  * enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and
267f7cbfa71SZhenzhong Duan  *                       ioctl(IOMMU_OPTION_HUGE_PAGES)
268f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_RLIMIT_MODE:
269f7cbfa71SZhenzhong Duan  *    Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege
270f7cbfa71SZhenzhong Duan  *    to invoke this. Value 0 (default) is user based accouting, 1 uses process
271f7cbfa71SZhenzhong Duan  *    based accounting. Global option, object_id must be 0
272f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_HUGE_PAGES:
273f7cbfa71SZhenzhong Duan  *    Value 1 (default) allows contiguous pages to be combined when generating
274f7cbfa71SZhenzhong Duan  *    iommu mappings. Value 0 disables combining, everything is mapped to
275f7cbfa71SZhenzhong Duan  *    PAGE_SIZE. This can be useful for benchmarking.  This is a per-IOAS
276f7cbfa71SZhenzhong Duan  *    option, the object_id must be the IOAS ID.
277f7cbfa71SZhenzhong Duan  */
278f7cbfa71SZhenzhong Duan enum iommufd_option {
279f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_RLIMIT_MODE = 0,
280f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_HUGE_PAGES = 1,
281f7cbfa71SZhenzhong Duan };
282f7cbfa71SZhenzhong Duan 
283f7cbfa71SZhenzhong Duan /**
284f7cbfa71SZhenzhong Duan  * enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and
285f7cbfa71SZhenzhong Duan  *                           ioctl(IOMMU_OPTION_OP_GET)
286f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_OP_SET: Set the option's value
287f7cbfa71SZhenzhong Duan  * @IOMMU_OPTION_OP_GET: Get the option's value
288f7cbfa71SZhenzhong Duan  */
289f7cbfa71SZhenzhong Duan enum iommufd_option_ops {
290f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_OP_SET = 0,
291f7cbfa71SZhenzhong Duan 	IOMMU_OPTION_OP_GET = 1,
292f7cbfa71SZhenzhong Duan };
293f7cbfa71SZhenzhong Duan 
294f7cbfa71SZhenzhong Duan /**
295f7cbfa71SZhenzhong Duan  * struct iommu_option - iommu option multiplexer
296f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_option)
297f7cbfa71SZhenzhong Duan  * @option_id: One of enum iommufd_option
298f7cbfa71SZhenzhong Duan  * @op: One of enum iommufd_option_ops
299f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
300f7cbfa71SZhenzhong Duan  * @object_id: ID of the object if required
301f7cbfa71SZhenzhong Duan  * @val64: Option value to set or value returned on get
302f7cbfa71SZhenzhong Duan  *
303f7cbfa71SZhenzhong Duan  * Change a simple option value. This multiplexor allows controlling options
304f7cbfa71SZhenzhong Duan  * on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET
305f7cbfa71SZhenzhong Duan  * will return the current value.
306f7cbfa71SZhenzhong Duan  */
307f7cbfa71SZhenzhong Duan struct iommu_option {
308f7cbfa71SZhenzhong Duan 	__u32 size;
309f7cbfa71SZhenzhong Duan 	__u32 option_id;
310f7cbfa71SZhenzhong Duan 	__u16 op;
311f7cbfa71SZhenzhong Duan 	__u16 __reserved;
312f7cbfa71SZhenzhong Duan 	__u32 object_id;
313f7cbfa71SZhenzhong Duan 	__aligned_u64 val64;
314f7cbfa71SZhenzhong Duan };
315f7cbfa71SZhenzhong Duan #define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION)
316f7cbfa71SZhenzhong Duan 
317f7cbfa71SZhenzhong Duan /**
318f7cbfa71SZhenzhong Duan  * enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls
319f7cbfa71SZhenzhong Duan  * @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS
320f7cbfa71SZhenzhong Duan  * @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS
321f7cbfa71SZhenzhong Duan  * @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility
322f7cbfa71SZhenzhong Duan  */
323f7cbfa71SZhenzhong Duan enum iommufd_vfio_ioas_op {
324f7cbfa71SZhenzhong Duan 	IOMMU_VFIO_IOAS_GET = 0,
325f7cbfa71SZhenzhong Duan 	IOMMU_VFIO_IOAS_SET = 1,
326f7cbfa71SZhenzhong Duan 	IOMMU_VFIO_IOAS_CLEAR = 2,
327f7cbfa71SZhenzhong Duan };
328f7cbfa71SZhenzhong Duan 
329f7cbfa71SZhenzhong Duan /**
330f7cbfa71SZhenzhong Duan  * struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS)
331f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_vfio_ioas)
332f7cbfa71SZhenzhong Duan  * @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set
333f7cbfa71SZhenzhong Duan  *           For IOMMU_VFIO_IOAS_GET will output the IOAS ID
334f7cbfa71SZhenzhong Duan  * @op: One of enum iommufd_vfio_ioas_op
335f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
336f7cbfa71SZhenzhong Duan  *
337f7cbfa71SZhenzhong Duan  * The VFIO compatibility support uses a single ioas because VFIO APIs do not
338f7cbfa71SZhenzhong Duan  * support the ID field. Set or Get the IOAS that VFIO compatibility will use.
339f7cbfa71SZhenzhong Duan  * When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the
340f7cbfa71SZhenzhong Duan  * compatibility ioas, either by taking what is already set, or auto creating
341f7cbfa71SZhenzhong Duan  * one. From then on VFIO will continue to use that ioas and is not effected by
342f7cbfa71SZhenzhong Duan  * this ioctl. SET or CLEAR does not destroy any auto-created IOAS.
343f7cbfa71SZhenzhong Duan  */
344f7cbfa71SZhenzhong Duan struct iommu_vfio_ioas {
345f7cbfa71SZhenzhong Duan 	__u32 size;
346f7cbfa71SZhenzhong Duan 	__u32 ioas_id;
347f7cbfa71SZhenzhong Duan 	__u16 op;
348f7cbfa71SZhenzhong Duan 	__u16 __reserved;
349f7cbfa71SZhenzhong Duan };
350f7cbfa71SZhenzhong Duan #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS)
351f7cbfa71SZhenzhong Duan 
352f7cbfa71SZhenzhong Duan /**
353*efb91426SDaniel Henrique Barboza  * enum iommufd_hwpt_alloc_flags - Flags for HWPT allocation
354*efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_ALLOC_NEST_PARENT: If set, allocate a HWPT that can serve as
355*efb91426SDaniel Henrique Barboza  *                                the parent HWPT in a nesting configuration.
356*efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_ALLOC_DIRTY_TRACKING: Dirty tracking support for device IOMMU is
357*efb91426SDaniel Henrique Barboza  *                                   enforced on device attachment
358*efb91426SDaniel Henrique Barboza  */
359*efb91426SDaniel Henrique Barboza enum iommufd_hwpt_alloc_flags {
360*efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_ALLOC_NEST_PARENT = 1 << 0,
361*efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1,
362*efb91426SDaniel Henrique Barboza };
363*efb91426SDaniel Henrique Barboza 
364*efb91426SDaniel Henrique Barboza /**
365*efb91426SDaniel Henrique Barboza  * enum iommu_hwpt_vtd_s1_flags - Intel VT-d stage-1 page table
366*efb91426SDaniel Henrique Barboza  *                                entry attributes
367*efb91426SDaniel Henrique Barboza  * @IOMMU_VTD_S1_SRE: Supervisor request
368*efb91426SDaniel Henrique Barboza  * @IOMMU_VTD_S1_EAFE: Extended access enable
369*efb91426SDaniel Henrique Barboza  * @IOMMU_VTD_S1_WPE: Write protect enable
370*efb91426SDaniel Henrique Barboza  */
371*efb91426SDaniel Henrique Barboza enum iommu_hwpt_vtd_s1_flags {
372*efb91426SDaniel Henrique Barboza 	IOMMU_VTD_S1_SRE = 1 << 0,
373*efb91426SDaniel Henrique Barboza 	IOMMU_VTD_S1_EAFE = 1 << 1,
374*efb91426SDaniel Henrique Barboza 	IOMMU_VTD_S1_WPE = 1 << 2,
375*efb91426SDaniel Henrique Barboza };
376*efb91426SDaniel Henrique Barboza 
377*efb91426SDaniel Henrique Barboza /**
378*efb91426SDaniel Henrique Barboza  * struct iommu_hwpt_vtd_s1 - Intel VT-d stage-1 page table
379*efb91426SDaniel Henrique Barboza  *                            info (IOMMU_HWPT_DATA_VTD_S1)
380*efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommu_hwpt_vtd_s1_flags
381*efb91426SDaniel Henrique Barboza  * @pgtbl_addr: The base address of the stage-1 page table.
382*efb91426SDaniel Henrique Barboza  * @addr_width: The address width of the stage-1 page table
383*efb91426SDaniel Henrique Barboza  * @__reserved: Must be 0
384*efb91426SDaniel Henrique Barboza  */
385*efb91426SDaniel Henrique Barboza struct iommu_hwpt_vtd_s1 {
386*efb91426SDaniel Henrique Barboza 	__aligned_u64 flags;
387*efb91426SDaniel Henrique Barboza 	__aligned_u64 pgtbl_addr;
388*efb91426SDaniel Henrique Barboza 	__u32 addr_width;
389*efb91426SDaniel Henrique Barboza 	__u32 __reserved;
390*efb91426SDaniel Henrique Barboza };
391*efb91426SDaniel Henrique Barboza 
392*efb91426SDaniel Henrique Barboza /**
393*efb91426SDaniel Henrique Barboza  * enum iommu_hwpt_data_type - IOMMU HWPT Data Type
394*efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_DATA_NONE: no data
395*efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table
396*efb91426SDaniel Henrique Barboza  */
397*efb91426SDaniel Henrique Barboza enum iommu_hwpt_data_type {
398*efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_DATA_NONE,
399*efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_DATA_VTD_S1,
400*efb91426SDaniel Henrique Barboza };
401*efb91426SDaniel Henrique Barboza 
402*efb91426SDaniel Henrique Barboza /**
403f7cbfa71SZhenzhong Duan  * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC)
404f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_hwpt_alloc)
405*efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommufd_hwpt_alloc_flags
406f7cbfa71SZhenzhong Duan  * @dev_id: The device to allocate this HWPT for
407*efb91426SDaniel Henrique Barboza  * @pt_id: The IOAS or HWPT to connect this HWPT to
408f7cbfa71SZhenzhong Duan  * @out_hwpt_id: The ID of the new HWPT
409f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
410*efb91426SDaniel Henrique Barboza  * @data_type: One of enum iommu_hwpt_data_type
411*efb91426SDaniel Henrique Barboza  * @data_len: Length of the type specific data
412*efb91426SDaniel Henrique Barboza  * @data_uptr: User pointer to the type specific data
413f7cbfa71SZhenzhong Duan  *
414f7cbfa71SZhenzhong Duan  * Explicitly allocate a hardware page table object. This is the same object
415f7cbfa71SZhenzhong Duan  * type that is returned by iommufd_device_attach() and represents the
416f7cbfa71SZhenzhong Duan  * underlying iommu driver's iommu_domain kernel object.
417f7cbfa71SZhenzhong Duan  *
418*efb91426SDaniel Henrique Barboza  * A kernel-managed HWPT will be created with the mappings from the given
419*efb91426SDaniel Henrique Barboza  * IOAS via the @pt_id. The @data_type for this allocation must be set to
420*efb91426SDaniel Henrique Barboza  * IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a
421*efb91426SDaniel Henrique Barboza  * nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags.
422*efb91426SDaniel Henrique Barboza  *
423*efb91426SDaniel Henrique Barboza  * A user-managed nested HWPT will be created from a given parent HWPT via
424*efb91426SDaniel Henrique Barboza  * @pt_id, in which the parent HWPT must be allocated previously via the
425*efb91426SDaniel Henrique Barboza  * same ioctl from a given IOAS (@pt_id). In this case, the @data_type
426*efb91426SDaniel Henrique Barboza  * must be set to a pre-defined type corresponding to an I/O page table
427*efb91426SDaniel Henrique Barboza  * type supported by the underlying IOMMU hardware.
428*efb91426SDaniel Henrique Barboza  *
429*efb91426SDaniel Henrique Barboza  * If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and
430*efb91426SDaniel Henrique Barboza  * @data_uptr should be zero. Otherwise, both @data_len and @data_uptr
431*efb91426SDaniel Henrique Barboza  * must be given.
432f7cbfa71SZhenzhong Duan  */
433f7cbfa71SZhenzhong Duan struct iommu_hwpt_alloc {
434f7cbfa71SZhenzhong Duan 	__u32 size;
435f7cbfa71SZhenzhong Duan 	__u32 flags;
436f7cbfa71SZhenzhong Duan 	__u32 dev_id;
437f7cbfa71SZhenzhong Duan 	__u32 pt_id;
438f7cbfa71SZhenzhong Duan 	__u32 out_hwpt_id;
439f7cbfa71SZhenzhong Duan 	__u32 __reserved;
440*efb91426SDaniel Henrique Barboza 	__u32 data_type;
441*efb91426SDaniel Henrique Barboza 	__u32 data_len;
442*efb91426SDaniel Henrique Barboza 	__aligned_u64 data_uptr;
443f7cbfa71SZhenzhong Duan };
444f7cbfa71SZhenzhong Duan #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC)
445f7cbfa71SZhenzhong Duan 
446f7cbfa71SZhenzhong Duan /**
447*efb91426SDaniel Henrique Barboza  * enum iommu_hw_info_vtd_flags - Flags for VT-d hw_info
448*efb91426SDaniel Henrique Barboza  * @IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17: If set, disallow read-only mappings
449*efb91426SDaniel Henrique Barboza  *                                         on a nested_parent domain.
450*efb91426SDaniel Henrique Barboza  *                                         https://www.intel.com/content/www/us/en/content-details/772415/content-details.html
451*efb91426SDaniel Henrique Barboza  */
452*efb91426SDaniel Henrique Barboza enum iommu_hw_info_vtd_flags {
453*efb91426SDaniel Henrique Barboza 	IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 = 1 << 0,
454*efb91426SDaniel Henrique Barboza };
455*efb91426SDaniel Henrique Barboza 
456*efb91426SDaniel Henrique Barboza /**
457f7cbfa71SZhenzhong Duan  * struct iommu_hw_info_vtd - Intel VT-d hardware information
458f7cbfa71SZhenzhong Duan  *
459*efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommu_hw_info_vtd_flags
460f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
461f7cbfa71SZhenzhong Duan  *
462f7cbfa71SZhenzhong Duan  * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec
463f7cbfa71SZhenzhong Duan  *           section 11.4.2 Capability Register.
464f7cbfa71SZhenzhong Duan  * @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec
465f7cbfa71SZhenzhong Duan  *            section 11.4.3 Extended Capability Register.
466f7cbfa71SZhenzhong Duan  *
467f7cbfa71SZhenzhong Duan  * User needs to understand the Intel VT-d specification to decode the
468f7cbfa71SZhenzhong Duan  * register value.
469f7cbfa71SZhenzhong Duan  */
470f7cbfa71SZhenzhong Duan struct iommu_hw_info_vtd {
471f7cbfa71SZhenzhong Duan 	__u32 flags;
472f7cbfa71SZhenzhong Duan 	__u32 __reserved;
473f7cbfa71SZhenzhong Duan 	__aligned_u64 cap_reg;
474f7cbfa71SZhenzhong Duan 	__aligned_u64 ecap_reg;
475f7cbfa71SZhenzhong Duan };
476f7cbfa71SZhenzhong Duan 
477f7cbfa71SZhenzhong Duan /**
478f7cbfa71SZhenzhong Duan  * enum iommu_hw_info_type - IOMMU Hardware Info Types
479f7cbfa71SZhenzhong Duan  * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not report hardware
480f7cbfa71SZhenzhong Duan  *                           info
481f7cbfa71SZhenzhong Duan  * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
482f7cbfa71SZhenzhong Duan  */
483f7cbfa71SZhenzhong Duan enum iommu_hw_info_type {
484f7cbfa71SZhenzhong Duan 	IOMMU_HW_INFO_TYPE_NONE,
485f7cbfa71SZhenzhong Duan 	IOMMU_HW_INFO_TYPE_INTEL_VTD,
486f7cbfa71SZhenzhong Duan };
487f7cbfa71SZhenzhong Duan 
488f7cbfa71SZhenzhong Duan /**
489*efb91426SDaniel Henrique Barboza  * enum iommufd_hw_capabilities
490*efb91426SDaniel Henrique Barboza  * @IOMMU_HW_CAP_DIRTY_TRACKING: IOMMU hardware support for dirty tracking
491*efb91426SDaniel Henrique Barboza  *                               If available, it means the following APIs
492*efb91426SDaniel Henrique Barboza  *                               are supported:
493*efb91426SDaniel Henrique Barboza  *
494*efb91426SDaniel Henrique Barboza  *                                   IOMMU_HWPT_GET_DIRTY_BITMAP
495*efb91426SDaniel Henrique Barboza  *                                   IOMMU_HWPT_SET_DIRTY_TRACKING
496*efb91426SDaniel Henrique Barboza  *
497*efb91426SDaniel Henrique Barboza  */
498*efb91426SDaniel Henrique Barboza enum iommufd_hw_capabilities {
499*efb91426SDaniel Henrique Barboza 	IOMMU_HW_CAP_DIRTY_TRACKING = 1 << 0,
500*efb91426SDaniel Henrique Barboza };
501*efb91426SDaniel Henrique Barboza 
502*efb91426SDaniel Henrique Barboza /**
503f7cbfa71SZhenzhong Duan  * struct iommu_hw_info - ioctl(IOMMU_GET_HW_INFO)
504f7cbfa71SZhenzhong Duan  * @size: sizeof(struct iommu_hw_info)
505f7cbfa71SZhenzhong Duan  * @flags: Must be 0
506f7cbfa71SZhenzhong Duan  * @dev_id: The device bound to the iommufd
507f7cbfa71SZhenzhong Duan  * @data_len: Input the length of a user buffer in bytes. Output the length of
508f7cbfa71SZhenzhong Duan  *            data that kernel supports
509f7cbfa71SZhenzhong Duan  * @data_uptr: User pointer to a user-space buffer used by the kernel to fill
510f7cbfa71SZhenzhong Duan  *             the iommu type specific hardware information data
511f7cbfa71SZhenzhong Duan  * @out_data_type: Output the iommu hardware info type as defined in the enum
512f7cbfa71SZhenzhong Duan  *                 iommu_hw_info_type.
513*efb91426SDaniel Henrique Barboza  * @out_capabilities: Output the generic iommu capability info type as defined
514*efb91426SDaniel Henrique Barboza  *                    in the enum iommu_hw_capabilities.
515f7cbfa71SZhenzhong Duan  * @__reserved: Must be 0
516f7cbfa71SZhenzhong Duan  *
517f7cbfa71SZhenzhong Duan  * Query an iommu type specific hardware information data from an iommu behind
518f7cbfa71SZhenzhong Duan  * a given device that has been bound to iommufd. This hardware info data will
519f7cbfa71SZhenzhong Duan  * be used to sync capabilities between the virtual iommu and the physical
520f7cbfa71SZhenzhong Duan  * iommu, e.g. a nested translation setup needs to check the hardware info, so
521f7cbfa71SZhenzhong Duan  * a guest stage-1 page table can be compatible with the physical iommu.
522f7cbfa71SZhenzhong Duan  *
523f7cbfa71SZhenzhong Duan  * To capture an iommu type specific hardware information data, @data_uptr and
524f7cbfa71SZhenzhong Duan  * its length @data_len must be provided. Trailing bytes will be zeroed if the
525f7cbfa71SZhenzhong Duan  * user buffer is larger than the data that kernel has. Otherwise, kernel only
526f7cbfa71SZhenzhong Duan  * fills the buffer using the given length in @data_len. If the ioctl succeeds,
527f7cbfa71SZhenzhong Duan  * @data_len will be updated to the length that kernel actually supports,
528f7cbfa71SZhenzhong Duan  * @out_data_type will be filled to decode the data filled in the buffer
529f7cbfa71SZhenzhong Duan  * pointed by @data_uptr. Input @data_len == zero is allowed.
530f7cbfa71SZhenzhong Duan  */
531f7cbfa71SZhenzhong Duan struct iommu_hw_info {
532f7cbfa71SZhenzhong Duan 	__u32 size;
533f7cbfa71SZhenzhong Duan 	__u32 flags;
534f7cbfa71SZhenzhong Duan 	__u32 dev_id;
535f7cbfa71SZhenzhong Duan 	__u32 data_len;
536f7cbfa71SZhenzhong Duan 	__aligned_u64 data_uptr;
537f7cbfa71SZhenzhong Duan 	__u32 out_data_type;
538f7cbfa71SZhenzhong Duan 	__u32 __reserved;
539*efb91426SDaniel Henrique Barboza 	__aligned_u64 out_capabilities;
540f7cbfa71SZhenzhong Duan };
541f7cbfa71SZhenzhong Duan #define IOMMU_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_GET_HW_INFO)
542*efb91426SDaniel Henrique Barboza 
543*efb91426SDaniel Henrique Barboza /*
544*efb91426SDaniel Henrique Barboza  * enum iommufd_hwpt_set_dirty_tracking_flags - Flags for steering dirty
545*efb91426SDaniel Henrique Barboza  *                                              tracking
546*efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_DIRTY_TRACKING_ENABLE: Enable dirty tracking
547*efb91426SDaniel Henrique Barboza  */
548*efb91426SDaniel Henrique Barboza enum iommufd_hwpt_set_dirty_tracking_flags {
549*efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_DIRTY_TRACKING_ENABLE = 1,
550*efb91426SDaniel Henrique Barboza };
551*efb91426SDaniel Henrique Barboza 
552*efb91426SDaniel Henrique Barboza /**
553*efb91426SDaniel Henrique Barboza  * struct iommu_hwpt_set_dirty_tracking - ioctl(IOMMU_HWPT_SET_DIRTY_TRACKING)
554*efb91426SDaniel Henrique Barboza  * @size: sizeof(struct iommu_hwpt_set_dirty_tracking)
555*efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommufd_hwpt_set_dirty_tracking_flags
556*efb91426SDaniel Henrique Barboza  * @hwpt_id: HW pagetable ID that represents the IOMMU domain
557*efb91426SDaniel Henrique Barboza  * @__reserved: Must be 0
558*efb91426SDaniel Henrique Barboza  *
559*efb91426SDaniel Henrique Barboza  * Toggle dirty tracking on an HW pagetable.
560*efb91426SDaniel Henrique Barboza  */
561*efb91426SDaniel Henrique Barboza struct iommu_hwpt_set_dirty_tracking {
562*efb91426SDaniel Henrique Barboza 	__u32 size;
563*efb91426SDaniel Henrique Barboza 	__u32 flags;
564*efb91426SDaniel Henrique Barboza 	__u32 hwpt_id;
565*efb91426SDaniel Henrique Barboza 	__u32 __reserved;
566*efb91426SDaniel Henrique Barboza };
567*efb91426SDaniel Henrique Barboza #define IOMMU_HWPT_SET_DIRTY_TRACKING _IO(IOMMUFD_TYPE, \
568*efb91426SDaniel Henrique Barboza 					  IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING)
569*efb91426SDaniel Henrique Barboza 
570*efb91426SDaniel Henrique Barboza /**
571*efb91426SDaniel Henrique Barboza  * enum iommufd_hwpt_get_dirty_bitmap_flags - Flags for getting dirty bits
572*efb91426SDaniel Henrique Barboza  * @IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR: Just read the PTEs without clearing
573*efb91426SDaniel Henrique Barboza  *                                        any dirty bits metadata. This flag
574*efb91426SDaniel Henrique Barboza  *                                        can be passed in the expectation
575*efb91426SDaniel Henrique Barboza  *                                        where the next operation is an unmap
576*efb91426SDaniel Henrique Barboza  *                                        of the same IOVA range.
577*efb91426SDaniel Henrique Barboza  *
578*efb91426SDaniel Henrique Barboza  */
579*efb91426SDaniel Henrique Barboza enum iommufd_hwpt_get_dirty_bitmap_flags {
580*efb91426SDaniel Henrique Barboza 	IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR = 1,
581*efb91426SDaniel Henrique Barboza };
582*efb91426SDaniel Henrique Barboza 
583*efb91426SDaniel Henrique Barboza /**
584*efb91426SDaniel Henrique Barboza  * struct iommu_hwpt_get_dirty_bitmap - ioctl(IOMMU_HWPT_GET_DIRTY_BITMAP)
585*efb91426SDaniel Henrique Barboza  * @size: sizeof(struct iommu_hwpt_get_dirty_bitmap)
586*efb91426SDaniel Henrique Barboza  * @hwpt_id: HW pagetable ID that represents the IOMMU domain
587*efb91426SDaniel Henrique Barboza  * @flags: Combination of enum iommufd_hwpt_get_dirty_bitmap_flags
588*efb91426SDaniel Henrique Barboza  * @__reserved: Must be 0
589*efb91426SDaniel Henrique Barboza  * @iova: base IOVA of the bitmap first bit
590*efb91426SDaniel Henrique Barboza  * @length: IOVA range size
591*efb91426SDaniel Henrique Barboza  * @page_size: page size granularity of each bit in the bitmap
592*efb91426SDaniel Henrique Barboza  * @data: bitmap where to set the dirty bits. The bitmap bits each
593*efb91426SDaniel Henrique Barboza  *        represent a page_size which you deviate from an arbitrary iova.
594*efb91426SDaniel Henrique Barboza  *
595*efb91426SDaniel Henrique Barboza  * Checking a given IOVA is dirty:
596*efb91426SDaniel Henrique Barboza  *
597*efb91426SDaniel Henrique Barboza  *  data[(iova / page_size) / 64] & (1ULL << ((iova / page_size) % 64))
598*efb91426SDaniel Henrique Barboza  *
599*efb91426SDaniel Henrique Barboza  * Walk the IOMMU pagetables for a given IOVA range to return a bitmap
600*efb91426SDaniel Henrique Barboza  * with the dirty IOVAs. In doing so it will also by default clear any
601*efb91426SDaniel Henrique Barboza  * dirty bit metadata set in the IOPTE.
602*efb91426SDaniel Henrique Barboza  */
603*efb91426SDaniel Henrique Barboza struct iommu_hwpt_get_dirty_bitmap {
604*efb91426SDaniel Henrique Barboza 	__u32 size;
605*efb91426SDaniel Henrique Barboza 	__u32 hwpt_id;
606*efb91426SDaniel Henrique Barboza 	__u32 flags;
607*efb91426SDaniel Henrique Barboza 	__u32 __reserved;
608*efb91426SDaniel Henrique Barboza 	__aligned_u64 iova;
609*efb91426SDaniel Henrique Barboza 	__aligned_u64 length;
610*efb91426SDaniel Henrique Barboza 	__aligned_u64 page_size;
611*efb91426SDaniel Henrique Barboza 	__aligned_u64 data;
612*efb91426SDaniel Henrique Barboza };
613*efb91426SDaniel Henrique Barboza #define IOMMU_HWPT_GET_DIRTY_BITMAP _IO(IOMMUFD_TYPE, \
614*efb91426SDaniel Henrique Barboza 					IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP)
615*efb91426SDaniel Henrique Barboza 
616f7cbfa71SZhenzhong Duan #endif
617