xref: /linux-6.15/include/uapi/linux/iommufd.h (revision 858c9c10)
12ff4bed7SJason Gunthorpe /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
22ff4bed7SJason Gunthorpe /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES.
32ff4bed7SJason Gunthorpe  */
42ff4bed7SJason Gunthorpe #ifndef _UAPI_IOMMUFD_H
52ff4bed7SJason Gunthorpe #define _UAPI_IOMMUFD_H
62ff4bed7SJason Gunthorpe 
72ff4bed7SJason Gunthorpe #include <linux/ioctl.h>
81d4684fbSNicolin Chen #include <linux/types.h>
92ff4bed7SJason Gunthorpe 
102ff4bed7SJason Gunthorpe #define IOMMUFD_TYPE (';')
112ff4bed7SJason Gunthorpe 
122ff4bed7SJason Gunthorpe /**
132ff4bed7SJason Gunthorpe  * DOC: General ioctl format
142ff4bed7SJason Gunthorpe  *
152ff4bed7SJason Gunthorpe  * The ioctl interface follows a general format to allow for extensibility. Each
162ff4bed7SJason Gunthorpe  * ioctl is passed in a structure pointer as the argument providing the size of
172ff4bed7SJason Gunthorpe  * the structure in the first u32. The kernel checks that any structure space
182ff4bed7SJason Gunthorpe  * beyond what it understands is 0. This allows userspace to use the backward
192ff4bed7SJason Gunthorpe  * compatible portion while consistently using the newer, larger, structures.
202ff4bed7SJason Gunthorpe  *
212ff4bed7SJason Gunthorpe  * ioctls use a standard meaning for common errnos:
222ff4bed7SJason Gunthorpe  *
232ff4bed7SJason Gunthorpe  *  - ENOTTY: The IOCTL number itself is not supported at all
242ff4bed7SJason Gunthorpe  *  - E2BIG: The IOCTL number is supported, but the provided structure has
252ff4bed7SJason Gunthorpe  *    non-zero in a part the kernel does not understand.
262ff4bed7SJason Gunthorpe  *  - EOPNOTSUPP: The IOCTL number is supported, and the structure is
272ff4bed7SJason Gunthorpe  *    understood, however a known field has a value the kernel does not
282ff4bed7SJason Gunthorpe  *    understand or support.
292ff4bed7SJason Gunthorpe  *  - EINVAL: Everything about the IOCTL was understood, but a field is not
302ff4bed7SJason Gunthorpe  *    correct.
312ff4bed7SJason Gunthorpe  *  - ENOENT: An ID or IOVA provided does not exist.
322ff4bed7SJason Gunthorpe  *  - ENOMEM: Out of memory.
332ff4bed7SJason Gunthorpe  *  - EOVERFLOW: Mathematics overflowed.
342ff4bed7SJason Gunthorpe  *
352ff4bed7SJason Gunthorpe  * As well as additional errnos, within specific ioctls.
362ff4bed7SJason Gunthorpe  */
372ff4bed7SJason Gunthorpe enum {
382ff4bed7SJason Gunthorpe 	IOMMUFD_CMD_BASE = 0x80,
392ff4bed7SJason Gunthorpe 	IOMMUFD_CMD_DESTROY = IOMMUFD_CMD_BASE,
40136a8066SJason Gunthorpe 	IOMMUFD_CMD_IOAS_ALLOC = 0x81,
41136a8066SJason Gunthorpe 	IOMMUFD_CMD_IOAS_ALLOW_IOVAS = 0x82,
42136a8066SJason Gunthorpe 	IOMMUFD_CMD_IOAS_COPY = 0x83,
43136a8066SJason Gunthorpe 	IOMMUFD_CMD_IOAS_IOVA_RANGES = 0x84,
44136a8066SJason Gunthorpe 	IOMMUFD_CMD_IOAS_MAP = 0x85,
45136a8066SJason Gunthorpe 	IOMMUFD_CMD_IOAS_UNMAP = 0x86,
46136a8066SJason Gunthorpe 	IOMMUFD_CMD_OPTION = 0x87,
47136a8066SJason Gunthorpe 	IOMMUFD_CMD_VFIO_IOAS = 0x88,
48136a8066SJason Gunthorpe 	IOMMUFD_CMD_HWPT_ALLOC = 0x89,
49136a8066SJason Gunthorpe 	IOMMUFD_CMD_GET_HW_INFO = 0x8a,
50136a8066SJason Gunthorpe 	IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING = 0x8b,
51136a8066SJason Gunthorpe 	IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP = 0x8c,
52136a8066SJason Gunthorpe 	IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d,
53136a8066SJason Gunthorpe 	IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e,
54f4986a72SSteve Sistare 	IOMMUFD_CMD_IOAS_MAP_FILE = 0x8f,
554db97c21SNicolin Chen 	IOMMUFD_CMD_VIOMMU_ALLOC = 0x90,
560ce5c247SNicolin Chen 	IOMMUFD_CMD_VDEVICE_ALLOC = 0x91,
57829ed626SSteve Sistare 	IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
58e36ba5abSNicolin Chen 	IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93,
592ff4bed7SJason Gunthorpe };
602ff4bed7SJason Gunthorpe 
612ff4bed7SJason Gunthorpe /**
622ff4bed7SJason Gunthorpe  * struct iommu_destroy - ioctl(IOMMU_DESTROY)
632ff4bed7SJason Gunthorpe  * @size: sizeof(struct iommu_destroy)
64aad37e71SJason Gunthorpe  * @id: iommufd object ID to destroy. Can be any destroyable object type.
652ff4bed7SJason Gunthorpe  *
662ff4bed7SJason Gunthorpe  * Destroy any object held within iommufd.
672ff4bed7SJason Gunthorpe  */
682ff4bed7SJason Gunthorpe struct iommu_destroy {
692ff4bed7SJason Gunthorpe 	__u32 size;
702ff4bed7SJason Gunthorpe 	__u32 id;
712ff4bed7SJason Gunthorpe };
722ff4bed7SJason Gunthorpe #define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY)
732ff4bed7SJason Gunthorpe 
74aad37e71SJason Gunthorpe /**
75aad37e71SJason Gunthorpe  * struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC)
76aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_ioas_alloc)
77aad37e71SJason Gunthorpe  * @flags: Must be 0
78aad37e71SJason Gunthorpe  * @out_ioas_id: Output IOAS ID for the allocated object
79aad37e71SJason Gunthorpe  *
80aad37e71SJason Gunthorpe  * Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA)
81aad37e71SJason Gunthorpe  * to memory mapping.
82aad37e71SJason Gunthorpe  */
83aad37e71SJason Gunthorpe struct iommu_ioas_alloc {
84aad37e71SJason Gunthorpe 	__u32 size;
85aad37e71SJason Gunthorpe 	__u32 flags;
86aad37e71SJason Gunthorpe 	__u32 out_ioas_id;
87aad37e71SJason Gunthorpe };
88aad37e71SJason Gunthorpe #define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC)
89aad37e71SJason Gunthorpe 
90aad37e71SJason Gunthorpe /**
91aad37e71SJason Gunthorpe  * struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE)
92aad37e71SJason Gunthorpe  * @start: First IOVA
93aad37e71SJason Gunthorpe  * @last: Inclusive last IOVA
94aad37e71SJason Gunthorpe  *
95aad37e71SJason Gunthorpe  * An interval in IOVA space.
96aad37e71SJason Gunthorpe  */
97aad37e71SJason Gunthorpe struct iommu_iova_range {
98aad37e71SJason Gunthorpe 	__aligned_u64 start;
99aad37e71SJason Gunthorpe 	__aligned_u64 last;
100aad37e71SJason Gunthorpe };
101aad37e71SJason Gunthorpe 
102aad37e71SJason Gunthorpe /**
103aad37e71SJason Gunthorpe  * struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES)
104aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_ioas_iova_ranges)
105aad37e71SJason Gunthorpe  * @ioas_id: IOAS ID to read ranges from
106aad37e71SJason Gunthorpe  * @num_iovas: Input/Output total number of ranges in the IOAS
107aad37e71SJason Gunthorpe  * @__reserved: Must be 0
108aad37e71SJason Gunthorpe  * @allowed_iovas: Pointer to the output array of struct iommu_iova_range
109aad37e71SJason Gunthorpe  * @out_iova_alignment: Minimum alignment required for mapping IOVA
110aad37e71SJason Gunthorpe  *
111aad37e71SJason Gunthorpe  * Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges
112aad37e71SJason Gunthorpe  * is not allowed. num_iovas will be set to the total number of iovas and
113aad37e71SJason Gunthorpe  * the allowed_iovas[] will be filled in as space permits.
114aad37e71SJason Gunthorpe  *
115aad37e71SJason Gunthorpe  * The allowed ranges are dependent on the HW path the DMA operation takes, and
116aad37e71SJason Gunthorpe  * can change during the lifetime of the IOAS. A fresh empty IOAS will have a
117aad37e71SJason Gunthorpe  * full range, and each attached device will narrow the ranges based on that
118aad37e71SJason Gunthorpe  * device's HW restrictions. Detaching a device can widen the ranges. Userspace
119aad37e71SJason Gunthorpe  * should query ranges after every attach/detach to know what IOVAs are valid
120aad37e71SJason Gunthorpe  * for mapping.
121aad37e71SJason Gunthorpe  *
122aad37e71SJason Gunthorpe  * On input num_iovas is the length of the allowed_iovas array. On output it is
123aad37e71SJason Gunthorpe  * the total number of iovas filled in. The ioctl will return -EMSGSIZE and set
124aad37e71SJason Gunthorpe  * num_iovas to the required value if num_iovas is too small. In this case the
125aad37e71SJason Gunthorpe  * caller should allocate a larger output array and re-issue the ioctl.
126aad37e71SJason Gunthorpe  *
127aad37e71SJason Gunthorpe  * out_iova_alignment returns the minimum IOVA alignment that can be given
128aad37e71SJason Gunthorpe  * to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy::
129aad37e71SJason Gunthorpe  *
130aad37e71SJason Gunthorpe  *   starting_iova % out_iova_alignment == 0
131aad37e71SJason Gunthorpe  *   (starting_iova + length) % out_iova_alignment == 0
132aad37e71SJason Gunthorpe  *
133aad37e71SJason Gunthorpe  * out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot
134aad37e71SJason Gunthorpe  * be higher than the system PAGE_SIZE.
135aad37e71SJason Gunthorpe  */
136aad37e71SJason Gunthorpe struct iommu_ioas_iova_ranges {
137aad37e71SJason Gunthorpe 	__u32 size;
138aad37e71SJason Gunthorpe 	__u32 ioas_id;
139aad37e71SJason Gunthorpe 	__u32 num_iovas;
140aad37e71SJason Gunthorpe 	__u32 __reserved;
141aad37e71SJason Gunthorpe 	__aligned_u64 allowed_iovas;
142aad37e71SJason Gunthorpe 	__aligned_u64 out_iova_alignment;
143aad37e71SJason Gunthorpe };
144aad37e71SJason Gunthorpe #define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES)
145aad37e71SJason Gunthorpe 
146aad37e71SJason Gunthorpe /**
147aad37e71SJason Gunthorpe  * struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS)
148aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_ioas_allow_iovas)
149aad37e71SJason Gunthorpe  * @ioas_id: IOAS ID to allow IOVAs from
150aad37e71SJason Gunthorpe  * @num_iovas: Input/Output total number of ranges in the IOAS
151aad37e71SJason Gunthorpe  * @__reserved: Must be 0
152aad37e71SJason Gunthorpe  * @allowed_iovas: Pointer to array of struct iommu_iova_range
153aad37e71SJason Gunthorpe  *
154aad37e71SJason Gunthorpe  * Ensure a range of IOVAs are always available for allocation. If this call
155aad37e71SJason Gunthorpe  * succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges
156aad37e71SJason Gunthorpe  * that are narrower than the ranges provided here. This call will fail if
157aad37e71SJason Gunthorpe  * IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges.
158aad37e71SJason Gunthorpe  *
159aad37e71SJason Gunthorpe  * When an IOAS is first created the IOVA_RANGES will be maximally sized, and as
160aad37e71SJason Gunthorpe  * devices are attached the IOVA will narrow based on the device restrictions.
161aad37e71SJason Gunthorpe  * When an allowed range is specified any narrowing will be refused, ie device
162aad37e71SJason Gunthorpe  * attachment can fail if the device requires limiting within the allowed range.
163aad37e71SJason Gunthorpe  *
164aad37e71SJason Gunthorpe  * Automatic IOVA allocation is also impacted by this call. MAP will only
165aad37e71SJason Gunthorpe  * allocate within the allowed IOVAs if they are present.
166aad37e71SJason Gunthorpe  *
167aad37e71SJason Gunthorpe  * This call replaces the entire allowed list with the given list.
168aad37e71SJason Gunthorpe  */
169aad37e71SJason Gunthorpe struct iommu_ioas_allow_iovas {
170aad37e71SJason Gunthorpe 	__u32 size;
171aad37e71SJason Gunthorpe 	__u32 ioas_id;
172aad37e71SJason Gunthorpe 	__u32 num_iovas;
173aad37e71SJason Gunthorpe 	__u32 __reserved;
174aad37e71SJason Gunthorpe 	__aligned_u64 allowed_iovas;
175aad37e71SJason Gunthorpe };
176aad37e71SJason Gunthorpe #define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS)
177aad37e71SJason Gunthorpe 
178aad37e71SJason Gunthorpe /**
179aad37e71SJason Gunthorpe  * enum iommufd_ioas_map_flags - Flags for map and copy
180aad37e71SJason Gunthorpe  * @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate
181aad37e71SJason Gunthorpe  *                             IOVA to place the mapping at
182aad37e71SJason Gunthorpe  * @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping
183aad37e71SJason Gunthorpe  * @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping
184aad37e71SJason Gunthorpe  */
185aad37e71SJason Gunthorpe enum iommufd_ioas_map_flags {
186aad37e71SJason Gunthorpe 	IOMMU_IOAS_MAP_FIXED_IOVA = 1 << 0,
187aad37e71SJason Gunthorpe 	IOMMU_IOAS_MAP_WRITEABLE = 1 << 1,
188aad37e71SJason Gunthorpe 	IOMMU_IOAS_MAP_READABLE = 1 << 2,
189aad37e71SJason Gunthorpe };
190aad37e71SJason Gunthorpe 
191aad37e71SJason Gunthorpe /**
192aad37e71SJason Gunthorpe  * struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP)
193aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_ioas_map)
194aad37e71SJason Gunthorpe  * @flags: Combination of enum iommufd_ioas_map_flags
195aad37e71SJason Gunthorpe  * @ioas_id: IOAS ID to change the mapping of
196aad37e71SJason Gunthorpe  * @__reserved: Must be 0
197aad37e71SJason Gunthorpe  * @user_va: Userspace pointer to start mapping from
198aad37e71SJason Gunthorpe  * @length: Number of bytes to map
199aad37e71SJason Gunthorpe  * @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set
200aad37e71SJason Gunthorpe  *        then this must be provided as input.
201aad37e71SJason Gunthorpe  *
202aad37e71SJason Gunthorpe  * Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the
203aad37e71SJason Gunthorpe  * mapping will be established at iova, otherwise a suitable location based on
204aad37e71SJason Gunthorpe  * the reserved and allowed lists will be automatically selected and returned in
205aad37e71SJason Gunthorpe  * iova.
206aad37e71SJason Gunthorpe  *
207aad37e71SJason Gunthorpe  * If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently
208aad37e71SJason Gunthorpe  * be unused, existing IOVA cannot be replaced.
209aad37e71SJason Gunthorpe  */
210aad37e71SJason Gunthorpe struct iommu_ioas_map {
211aad37e71SJason Gunthorpe 	__u32 size;
212aad37e71SJason Gunthorpe 	__u32 flags;
213aad37e71SJason Gunthorpe 	__u32 ioas_id;
214aad37e71SJason Gunthorpe 	__u32 __reserved;
215aad37e71SJason Gunthorpe 	__aligned_u64 user_va;
216aad37e71SJason Gunthorpe 	__aligned_u64 length;
217aad37e71SJason Gunthorpe 	__aligned_u64 iova;
218aad37e71SJason Gunthorpe };
219aad37e71SJason Gunthorpe #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP)
220aad37e71SJason Gunthorpe 
221aad37e71SJason Gunthorpe /**
222f4986a72SSteve Sistare  * struct iommu_ioas_map_file - ioctl(IOMMU_IOAS_MAP_FILE)
223f4986a72SSteve Sistare  * @size: sizeof(struct iommu_ioas_map_file)
224f4986a72SSteve Sistare  * @flags: same as for iommu_ioas_map
225f4986a72SSteve Sistare  * @ioas_id: same as for iommu_ioas_map
226f4986a72SSteve Sistare  * @fd: the memfd to map
227f4986a72SSteve Sistare  * @start: byte offset from start of file to map from
228f4986a72SSteve Sistare  * @length: same as for iommu_ioas_map
229f4986a72SSteve Sistare  * @iova: same as for iommu_ioas_map
230f4986a72SSteve Sistare  *
231f4986a72SSteve Sistare  * Set an IOVA mapping from a memfd file.  All other arguments and semantics
232f4986a72SSteve Sistare  * match those of IOMMU_IOAS_MAP.
233f4986a72SSteve Sistare  */
234f4986a72SSteve Sistare struct iommu_ioas_map_file {
235f4986a72SSteve Sistare 	__u32 size;
236f4986a72SSteve Sistare 	__u32 flags;
237f4986a72SSteve Sistare 	__u32 ioas_id;
238f4986a72SSteve Sistare 	__s32 fd;
239f4986a72SSteve Sistare 	__aligned_u64 start;
240f4986a72SSteve Sistare 	__aligned_u64 length;
241f4986a72SSteve Sistare 	__aligned_u64 iova;
242f4986a72SSteve Sistare };
243f4986a72SSteve Sistare #define IOMMU_IOAS_MAP_FILE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP_FILE)
244f4986a72SSteve Sistare 
245f4986a72SSteve Sistare /**
246aad37e71SJason Gunthorpe  * struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY)
247aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_ioas_copy)
248aad37e71SJason Gunthorpe  * @flags: Combination of enum iommufd_ioas_map_flags
249aad37e71SJason Gunthorpe  * @dst_ioas_id: IOAS ID to change the mapping of
250aad37e71SJason Gunthorpe  * @src_ioas_id: IOAS ID to copy from
251aad37e71SJason Gunthorpe  * @length: Number of bytes to copy and map
252aad37e71SJason Gunthorpe  * @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is
253aad37e71SJason Gunthorpe  *            set then this must be provided as input.
254aad37e71SJason Gunthorpe  * @src_iova: IOVA to start the copy
255aad37e71SJason Gunthorpe  *
256aad37e71SJason Gunthorpe  * Copy an already existing mapping from src_ioas_id and establish it in
257aad37e71SJason Gunthorpe  * dst_ioas_id. The src iova/length must exactly match a range used with
258aad37e71SJason Gunthorpe  * IOMMU_IOAS_MAP.
259aad37e71SJason Gunthorpe  *
260aad37e71SJason Gunthorpe  * This may be used to efficiently clone a subset of an IOAS to another, or as a
261aad37e71SJason Gunthorpe  * kind of 'cache' to speed up mapping. Copy has an efficiency advantage over
262aad37e71SJason Gunthorpe  * establishing equivalent new mappings, as internal resources are shared, and
263aad37e71SJason Gunthorpe  * the kernel will pin the user memory only once.
264aad37e71SJason Gunthorpe  */
265aad37e71SJason Gunthorpe struct iommu_ioas_copy {
266aad37e71SJason Gunthorpe 	__u32 size;
267aad37e71SJason Gunthorpe 	__u32 flags;
268aad37e71SJason Gunthorpe 	__u32 dst_ioas_id;
269aad37e71SJason Gunthorpe 	__u32 src_ioas_id;
270aad37e71SJason Gunthorpe 	__aligned_u64 length;
271aad37e71SJason Gunthorpe 	__aligned_u64 dst_iova;
272aad37e71SJason Gunthorpe 	__aligned_u64 src_iova;
273aad37e71SJason Gunthorpe };
274aad37e71SJason Gunthorpe #define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY)
275aad37e71SJason Gunthorpe 
276aad37e71SJason Gunthorpe /**
277aad37e71SJason Gunthorpe  * struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP)
278aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_ioas_unmap)
279aad37e71SJason Gunthorpe  * @ioas_id: IOAS ID to change the mapping of
280aad37e71SJason Gunthorpe  * @iova: IOVA to start the unmapping at
281aad37e71SJason Gunthorpe  * @length: Number of bytes to unmap, and return back the bytes unmapped
282aad37e71SJason Gunthorpe  *
283aad37e71SJason Gunthorpe  * Unmap an IOVA range. The iova/length must be a superset of a previously
284aad37e71SJason Gunthorpe  * mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or
285aad37e71SJason Gunthorpe  * truncating ranges is not allowed. The values 0 to U64_MAX will unmap
286aad37e71SJason Gunthorpe  * everything.
287aad37e71SJason Gunthorpe  */
288aad37e71SJason Gunthorpe struct iommu_ioas_unmap {
289aad37e71SJason Gunthorpe 	__u32 size;
290aad37e71SJason Gunthorpe 	__u32 ioas_id;
291aad37e71SJason Gunthorpe 	__aligned_u64 iova;
292aad37e71SJason Gunthorpe 	__aligned_u64 length;
293aad37e71SJason Gunthorpe };
294aad37e71SJason Gunthorpe #define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP)
295aad37e71SJason Gunthorpe 
296aad37e71SJason Gunthorpe /**
297aad37e71SJason Gunthorpe  * enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and
298aad37e71SJason Gunthorpe  *                       ioctl(IOMMU_OPTION_HUGE_PAGES)
299aad37e71SJason Gunthorpe  * @IOMMU_OPTION_RLIMIT_MODE:
300aad37e71SJason Gunthorpe  *    Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege
3017937a1bfSRandy Dunlap  *    to invoke this. Value 0 (default) is user based accounting, 1 uses process
302aad37e71SJason Gunthorpe  *    based accounting. Global option, object_id must be 0
303aad37e71SJason Gunthorpe  * @IOMMU_OPTION_HUGE_PAGES:
304aad37e71SJason Gunthorpe  *    Value 1 (default) allows contiguous pages to be combined when generating
305aad37e71SJason Gunthorpe  *    iommu mappings. Value 0 disables combining, everything is mapped to
306aad37e71SJason Gunthorpe  *    PAGE_SIZE. This can be useful for benchmarking.  This is a per-IOAS
307aad37e71SJason Gunthorpe  *    option, the object_id must be the IOAS ID.
308aad37e71SJason Gunthorpe  */
309aad37e71SJason Gunthorpe enum iommufd_option {
310aad37e71SJason Gunthorpe 	IOMMU_OPTION_RLIMIT_MODE = 0,
311aad37e71SJason Gunthorpe 	IOMMU_OPTION_HUGE_PAGES = 1,
312aad37e71SJason Gunthorpe };
313aad37e71SJason Gunthorpe 
314aad37e71SJason Gunthorpe /**
315aad37e71SJason Gunthorpe  * enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and
316aad37e71SJason Gunthorpe  *                           ioctl(IOMMU_OPTION_OP_GET)
317aad37e71SJason Gunthorpe  * @IOMMU_OPTION_OP_SET: Set the option's value
318aad37e71SJason Gunthorpe  * @IOMMU_OPTION_OP_GET: Get the option's value
319aad37e71SJason Gunthorpe  */
320aad37e71SJason Gunthorpe enum iommufd_option_ops {
321aad37e71SJason Gunthorpe 	IOMMU_OPTION_OP_SET = 0,
322aad37e71SJason Gunthorpe 	IOMMU_OPTION_OP_GET = 1,
323aad37e71SJason Gunthorpe };
324aad37e71SJason Gunthorpe 
325aad37e71SJason Gunthorpe /**
326aad37e71SJason Gunthorpe  * struct iommu_option - iommu option multiplexer
327aad37e71SJason Gunthorpe  * @size: sizeof(struct iommu_option)
328aad37e71SJason Gunthorpe  * @option_id: One of enum iommufd_option
329aad37e71SJason Gunthorpe  * @op: One of enum iommufd_option_ops
330aad37e71SJason Gunthorpe  * @__reserved: Must be 0
331aad37e71SJason Gunthorpe  * @object_id: ID of the object if required
332aad37e71SJason Gunthorpe  * @val64: Option value to set or value returned on get
333aad37e71SJason Gunthorpe  *
334aad37e71SJason Gunthorpe  * Change a simple option value. This multiplexor allows controlling options
335aad37e71SJason Gunthorpe  * on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET
336aad37e71SJason Gunthorpe  * will return the current value.
337aad37e71SJason Gunthorpe  */
338aad37e71SJason Gunthorpe struct iommu_option {
339aad37e71SJason Gunthorpe 	__u32 size;
340aad37e71SJason Gunthorpe 	__u32 option_id;
341aad37e71SJason Gunthorpe 	__u16 op;
342aad37e71SJason Gunthorpe 	__u16 __reserved;
343aad37e71SJason Gunthorpe 	__u32 object_id;
344aad37e71SJason Gunthorpe 	__aligned_u64 val64;
345aad37e71SJason Gunthorpe };
346aad37e71SJason Gunthorpe #define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION)
347d624d665SJason Gunthorpe 
348d624d665SJason Gunthorpe /**
349d624d665SJason Gunthorpe  * enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls
350d624d665SJason Gunthorpe  * @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS
351d624d665SJason Gunthorpe  * @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS
352d624d665SJason Gunthorpe  * @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility
353d624d665SJason Gunthorpe  */
354d624d665SJason Gunthorpe enum iommufd_vfio_ioas_op {
355d624d665SJason Gunthorpe 	IOMMU_VFIO_IOAS_GET = 0,
356d624d665SJason Gunthorpe 	IOMMU_VFIO_IOAS_SET = 1,
357d624d665SJason Gunthorpe 	IOMMU_VFIO_IOAS_CLEAR = 2,
358d624d665SJason Gunthorpe };
359d624d665SJason Gunthorpe 
360d624d665SJason Gunthorpe /**
361d624d665SJason Gunthorpe  * struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS)
362d624d665SJason Gunthorpe  * @size: sizeof(struct iommu_vfio_ioas)
363d624d665SJason Gunthorpe  * @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set
364d624d665SJason Gunthorpe  *           For IOMMU_VFIO_IOAS_GET will output the IOAS ID
365d624d665SJason Gunthorpe  * @op: One of enum iommufd_vfio_ioas_op
366d624d665SJason Gunthorpe  * @__reserved: Must be 0
367d624d665SJason Gunthorpe  *
368d624d665SJason Gunthorpe  * The VFIO compatibility support uses a single ioas because VFIO APIs do not
369d624d665SJason Gunthorpe  * support the ID field. Set or Get the IOAS that VFIO compatibility will use.
370d624d665SJason Gunthorpe  * When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the
371d624d665SJason Gunthorpe  * compatibility ioas, either by taking what is already set, or auto creating
372d624d665SJason Gunthorpe  * one. From then on VFIO will continue to use that ioas and is not effected by
373d624d665SJason Gunthorpe  * this ioctl. SET or CLEAR does not destroy any auto-created IOAS.
374d624d665SJason Gunthorpe  */
375d624d665SJason Gunthorpe struct iommu_vfio_ioas {
376d624d665SJason Gunthorpe 	__u32 size;
377d624d665SJason Gunthorpe 	__u32 ioas_id;
378d624d665SJason Gunthorpe 	__u16 op;
379d624d665SJason Gunthorpe 	__u16 __reserved;
380d624d665SJason Gunthorpe };
381d624d665SJason Gunthorpe #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS)
3827074d7bdSJason Gunthorpe 
3837074d7bdSJason Gunthorpe /**
3844ff54216SYi Liu  * enum iommufd_hwpt_alloc_flags - Flags for HWPT allocation
385b5f9e632SNicolin Chen  * @IOMMU_HWPT_ALLOC_NEST_PARENT: If set, allocate a HWPT that can serve as
386b5f9e632SNicolin Chen  *                                the parent HWPT in a nesting configuration.
3875f9bdbf4SJoao Martins  * @IOMMU_HWPT_ALLOC_DIRTY_TRACKING: Dirty tracking support for device IOMMU is
3885f9bdbf4SJoao Martins  *                                   enforced on device attachment
38934765cbcSLu Baolu  * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation data is
39034765cbcSLu Baolu  *                             valid.
391b7a0855eSJason Gunthorpe  * @IOMMU_HWPT_ALLOC_PASID: Requests a domain that can be used with PASID. The
392b7a0855eSJason Gunthorpe  *                          domain can be attached to any PASID on the device.
393b7a0855eSJason Gunthorpe  *                          Any domain attached to the non-PASID part of the
3947937a1bfSRandy Dunlap  *                          device must also be flagged, otherwise attaching a
395b7a0855eSJason Gunthorpe  *                          PASID will blocked.
396dbc5f37bSYi Liu  *                          For the user that wants to attach PASID, ioas is
397dbc5f37bSYi Liu  *                          not recommended for both the non-PASID part
398dbc5f37bSYi Liu  *                          and PASID part of the device.
399b7a0855eSJason Gunthorpe  *                          If IOMMU does not support PASID it will return
400b7a0855eSJason Gunthorpe  *                          error (-EOPNOTSUPP).
4014ff54216SYi Liu  */
4024ff54216SYi Liu enum iommufd_hwpt_alloc_flags {
4034ff54216SYi Liu 	IOMMU_HWPT_ALLOC_NEST_PARENT = 1 << 0,
4045f9bdbf4SJoao Martins 	IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1,
40534765cbcSLu Baolu 	IOMMU_HWPT_FAULT_ID_VALID = 1 << 2,
406b7a0855eSJason Gunthorpe 	IOMMU_HWPT_ALLOC_PASID = 1 << 3,
4074ff54216SYi Liu };
4084ff54216SYi Liu 
4094ff54216SYi Liu /**
41082b6661cSYi Liu  * enum iommu_hwpt_vtd_s1_flags - Intel VT-d stage-1 page table
41182b6661cSYi Liu  *                                entry attributes
41282b6661cSYi Liu  * @IOMMU_VTD_S1_SRE: Supervisor request
41382b6661cSYi Liu  * @IOMMU_VTD_S1_EAFE: Extended access enable
41482b6661cSYi Liu  * @IOMMU_VTD_S1_WPE: Write protect enable
41582b6661cSYi Liu  */
41682b6661cSYi Liu enum iommu_hwpt_vtd_s1_flags {
41782b6661cSYi Liu 	IOMMU_VTD_S1_SRE = 1 << 0,
41882b6661cSYi Liu 	IOMMU_VTD_S1_EAFE = 1 << 1,
41982b6661cSYi Liu 	IOMMU_VTD_S1_WPE = 1 << 2,
42082b6661cSYi Liu };
42182b6661cSYi Liu 
42282b6661cSYi Liu /**
42382b6661cSYi Liu  * struct iommu_hwpt_vtd_s1 - Intel VT-d stage-1 page table
42482b6661cSYi Liu  *                            info (IOMMU_HWPT_DATA_VTD_S1)
42582b6661cSYi Liu  * @flags: Combination of enum iommu_hwpt_vtd_s1_flags
42682b6661cSYi Liu  * @pgtbl_addr: The base address of the stage-1 page table.
42782b6661cSYi Liu  * @addr_width: The address width of the stage-1 page table
42882b6661cSYi Liu  * @__reserved: Must be 0
42982b6661cSYi Liu  */
43082b6661cSYi Liu struct iommu_hwpt_vtd_s1 {
43182b6661cSYi Liu 	__aligned_u64 flags;
43282b6661cSYi Liu 	__aligned_u64 pgtbl_addr;
43382b6661cSYi Liu 	__u32 addr_width;
43482b6661cSYi Liu 	__u32 __reserved;
43582b6661cSYi Liu };
43682b6661cSYi Liu 
43782b6661cSYi Liu /**
4381e8be08dSJason Gunthorpe  * struct iommu_hwpt_arm_smmuv3 - ARM SMMUv3 nested STE
4391e8be08dSJason Gunthorpe  *                                (IOMMU_HWPT_DATA_ARM_SMMUV3)
4401e8be08dSJason Gunthorpe  *
4411e8be08dSJason Gunthorpe  * @ste: The first two double words of the user space Stream Table Entry for
4421e8be08dSJason Gunthorpe  *       the translation. Must be little-endian.
4431e8be08dSJason Gunthorpe  *       Allowed fields: (Refer to "5.2 Stream Table Entry" in SMMUv3 HW Spec)
4441e8be08dSJason Gunthorpe  *       - word-0: V, Cfg, S1Fmt, S1ContextPtr, S1CDMax
445f27298a8SJason Gunthorpe  *       - word-1: EATS, S1DSS, S1CIR, S1COR, S1CSH, S1STALLD
4461e8be08dSJason Gunthorpe  *
4471e8be08dSJason Gunthorpe  * -EIO will be returned if @ste is not legal or contains any non-allowed field.
4481e8be08dSJason Gunthorpe  * Cfg can be used to select a S1, Bypass or Abort configuration. A Bypass
4491e8be08dSJason Gunthorpe  * nested domain will translate the same as the nesting parent. The S1 will
4501e8be08dSJason Gunthorpe  * install a Context Descriptor Table pointing at userspace memory translated
4511e8be08dSJason Gunthorpe  * by the nesting parent.
4521e8be08dSJason Gunthorpe  */
4531e8be08dSJason Gunthorpe struct iommu_hwpt_arm_smmuv3 {
4541e8be08dSJason Gunthorpe 	__aligned_le64 ste[2];
4551e8be08dSJason Gunthorpe };
4561e8be08dSJason Gunthorpe 
4571e8be08dSJason Gunthorpe /**
458bd529dbbSNicolin Chen  * enum iommu_hwpt_data_type - IOMMU HWPT Data Type
459bd529dbbSNicolin Chen  * @IOMMU_HWPT_DATA_NONE: no data
46082b6661cSYi Liu  * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table
46169d9b312SNicolin Chen  * @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table
462bd529dbbSNicolin Chen  */
463bd529dbbSNicolin Chen enum iommu_hwpt_data_type {
464136a8066SJason Gunthorpe 	IOMMU_HWPT_DATA_NONE = 0,
465136a8066SJason Gunthorpe 	IOMMU_HWPT_DATA_VTD_S1 = 1,
46669d9b312SNicolin Chen 	IOMMU_HWPT_DATA_ARM_SMMUV3 = 2,
467bd529dbbSNicolin Chen };
468bd529dbbSNicolin Chen 
469bd529dbbSNicolin Chen /**
4707074d7bdSJason Gunthorpe  * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC)
4717074d7bdSJason Gunthorpe  * @size: sizeof(struct iommu_hwpt_alloc)
4724ff54216SYi Liu  * @flags: Combination of enum iommufd_hwpt_alloc_flags
4737074d7bdSJason Gunthorpe  * @dev_id: The device to allocate this HWPT for
47413a75018SNicolin Chen  * @pt_id: The IOAS or HWPT or vIOMMU to connect this HWPT to
4757074d7bdSJason Gunthorpe  * @out_hwpt_id: The ID of the new HWPT
4767074d7bdSJason Gunthorpe  * @__reserved: Must be 0
477bd529dbbSNicolin Chen  * @data_type: One of enum iommu_hwpt_data_type
478bd529dbbSNicolin Chen  * @data_len: Length of the type specific data
479bd529dbbSNicolin Chen  * @data_uptr: User pointer to the type specific data
48034765cbcSLu Baolu  * @fault_id: The ID of IOMMUFD_FAULT object. Valid only if flags field of
48134765cbcSLu Baolu  *            IOMMU_HWPT_FAULT_ID_VALID is set.
48234765cbcSLu Baolu  * @__reserved2: Padding to 64-bit alignment. Must be 0.
4837074d7bdSJason Gunthorpe  *
4847074d7bdSJason Gunthorpe  * Explicitly allocate a hardware page table object. This is the same object
4857074d7bdSJason Gunthorpe  * type that is returned by iommufd_device_attach() and represents the
4867074d7bdSJason Gunthorpe  * underlying iommu driver's iommu_domain kernel object.
4877074d7bdSJason Gunthorpe  *
488bd529dbbSNicolin Chen  * A kernel-managed HWPT will be created with the mappings from the given
489bd529dbbSNicolin Chen  * IOAS via the @pt_id. The @data_type for this allocation must be set to
490bd529dbbSNicolin Chen  * IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a
491bd529dbbSNicolin Chen  * nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags.
492bd529dbbSNicolin Chen  *
49313a75018SNicolin Chen  * A user-managed nested HWPT will be created from a given vIOMMU (wrapping a
49413a75018SNicolin Chen  * parent HWPT) or a parent HWPT via @pt_id, in which the parent HWPT must be
49513a75018SNicolin Chen  * allocated previously via the same ioctl from a given IOAS (@pt_id). In this
49613a75018SNicolin Chen  * case, the @data_type must be set to a pre-defined type corresponding to an
49713a75018SNicolin Chen  * I/O page table type supported by the underlying IOMMU hardware. The device
49813a75018SNicolin Chen  * via @dev_id and the vIOMMU via @pt_id must be associated to the same IOMMU
49913a75018SNicolin Chen  * instance.
500bd529dbbSNicolin Chen  *
501bd529dbbSNicolin Chen  * If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and
502bd529dbbSNicolin Chen  * @data_uptr should be zero. Otherwise, both @data_len and @data_uptr
503bd529dbbSNicolin Chen  * must be given.
5047074d7bdSJason Gunthorpe  */
5057074d7bdSJason Gunthorpe struct iommu_hwpt_alloc {
5067074d7bdSJason Gunthorpe 	__u32 size;
5077074d7bdSJason Gunthorpe 	__u32 flags;
5087074d7bdSJason Gunthorpe 	__u32 dev_id;
5097074d7bdSJason Gunthorpe 	__u32 pt_id;
5107074d7bdSJason Gunthorpe 	__u32 out_hwpt_id;
5117074d7bdSJason Gunthorpe 	__u32 __reserved;
512bd529dbbSNicolin Chen 	__u32 data_type;
513bd529dbbSNicolin Chen 	__u32 data_len;
514bd529dbbSNicolin Chen 	__aligned_u64 data_uptr;
51534765cbcSLu Baolu 	__u32 fault_id;
51634765cbcSLu Baolu 	__u32 __reserved2;
5177074d7bdSJason Gunthorpe };
5187074d7bdSJason Gunthorpe #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC)
51960fedb26SLu Baolu 
52060fedb26SLu Baolu /**
52103476e68SLu Baolu  * enum iommu_hw_info_vtd_flags - Flags for VT-d hw_info
52203476e68SLu Baolu  * @IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17: If set, disallow read-only mappings
52303476e68SLu Baolu  *                                         on a nested_parent domain.
52403476e68SLu Baolu  *                                         https://www.intel.com/content/www/us/en/content-details/772415/content-details.html
52503476e68SLu Baolu  */
52603476e68SLu Baolu enum iommu_hw_info_vtd_flags {
52703476e68SLu Baolu 	IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 = 1 << 0,
52803476e68SLu Baolu };
52903476e68SLu Baolu 
53003476e68SLu Baolu /**
53155243393SYi Liu  * struct iommu_hw_info_vtd - Intel VT-d hardware information
53255243393SYi Liu  *
53303476e68SLu Baolu  * @flags: Combination of enum iommu_hw_info_vtd_flags
53455243393SYi Liu  * @__reserved: Must be 0
53555243393SYi Liu  *
53655243393SYi Liu  * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec
53755243393SYi Liu  *           section 11.4.2 Capability Register.
53855243393SYi Liu  * @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec
53955243393SYi Liu  *            section 11.4.3 Extended Capability Register.
54055243393SYi Liu  *
54155243393SYi Liu  * User needs to understand the Intel VT-d specification to decode the
54255243393SYi Liu  * register value.
54355243393SYi Liu  */
54455243393SYi Liu struct iommu_hw_info_vtd {
54555243393SYi Liu 	__u32 flags;
54655243393SYi Liu 	__u32 __reserved;
54755243393SYi Liu 	__aligned_u64 cap_reg;
54855243393SYi Liu 	__aligned_u64 ecap_reg;
54955243393SYi Liu };
55055243393SYi Liu 
55155243393SYi Liu /**
5526912ec91SNicolin Chen  * struct iommu_hw_info_arm_smmuv3 - ARM SMMUv3 hardware information
5536912ec91SNicolin Chen  *                                   (IOMMU_HW_INFO_TYPE_ARM_SMMUV3)
5546912ec91SNicolin Chen  *
5556912ec91SNicolin Chen  * @flags: Must be set to 0
5566912ec91SNicolin Chen  * @__reserved: Must be 0
5576912ec91SNicolin Chen  * @idr: Implemented features for ARM SMMU Non-secure programming interface
5586912ec91SNicolin Chen  * @iidr: Information about the implementation and implementer of ARM SMMU,
5596912ec91SNicolin Chen  *        and architecture version supported
5606912ec91SNicolin Chen  * @aidr: ARM SMMU architecture version
5616912ec91SNicolin Chen  *
5626912ec91SNicolin Chen  * For the details of @idr, @iidr and @aidr, please refer to the chapters
5636912ec91SNicolin Chen  * from 6.3.1 to 6.3.6 in the SMMUv3 Spec.
5646912ec91SNicolin Chen  *
5652ca704f5SJason Gunthorpe  * This reports the raw HW capability, and not all bits are meaningful to be
5662ca704f5SJason Gunthorpe  * read by userspace. Only the following fields should be used:
5676912ec91SNicolin Chen  *
5682ca704f5SJason Gunthorpe  * idr[0]: ST_LEVEL, TERM_MODEL, STALL_MODEL, TTENDIAN , CD2L, ASID16, TTF
5692ca704f5SJason Gunthorpe  * idr[1]: SIDSIZE, SSIDSIZE
5702ca704f5SJason Gunthorpe  * idr[3]: BBML, RIL
5712ca704f5SJason Gunthorpe  * idr[5]: VAX, GRAN64K, GRAN16K, GRAN4K
5726912ec91SNicolin Chen  *
5732ca704f5SJason Gunthorpe  * - S1P should be assumed to be true if a NESTED HWPT can be created
5742ca704f5SJason Gunthorpe  * - VFIO/iommufd only support platforms with COHACC, it should be assumed to be
5752ca704f5SJason Gunthorpe  *   true.
5762ca704f5SJason Gunthorpe  * - ATS is a per-device property. If the VMM describes any devices as ATS
5772ca704f5SJason Gunthorpe  *   capable in ACPI/DT it should set the corresponding idr.
5782ca704f5SJason Gunthorpe  *
5792ca704f5SJason Gunthorpe  * This list may expand in future (eg E0PD, AIE, PBHA, D128, DS etc). It is
5802ca704f5SJason Gunthorpe  * important that VMMs do not read bits outside the list to allow for
5812ca704f5SJason Gunthorpe  * compatibility with future kernels. Several features in the SMMUv3
5822ca704f5SJason Gunthorpe  * architecture are not currently supported by the kernel for nesting: HTTU,
5832ca704f5SJason Gunthorpe  * BTM, MPAM and others.
5846912ec91SNicolin Chen  */
5856912ec91SNicolin Chen struct iommu_hw_info_arm_smmuv3 {
5866912ec91SNicolin Chen 	__u32 flags;
5876912ec91SNicolin Chen 	__u32 __reserved;
5886912ec91SNicolin Chen 	__u32 idr[6];
5896912ec91SNicolin Chen 	__u32 iidr;
5906912ec91SNicolin Chen 	__u32 aidr;
5916912ec91SNicolin Chen };
5926912ec91SNicolin Chen 
5936912ec91SNicolin Chen /**
59460fedb26SLu Baolu  * enum iommu_hw_info_type - IOMMU Hardware Info Types
59560fedb26SLu Baolu  * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not report hardware
59660fedb26SLu Baolu  *                           info
59755243393SYi Liu  * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
5986912ec91SNicolin Chen  * @IOMMU_HW_INFO_TYPE_ARM_SMMUV3: ARM SMMUv3 iommu info type
59960fedb26SLu Baolu  */
60060fedb26SLu Baolu enum iommu_hw_info_type {
601136a8066SJason Gunthorpe 	IOMMU_HW_INFO_TYPE_NONE = 0,
602136a8066SJason Gunthorpe 	IOMMU_HW_INFO_TYPE_INTEL_VTD = 1,
6036912ec91SNicolin Chen 	IOMMU_HW_INFO_TYPE_ARM_SMMUV3 = 2,
60460fedb26SLu Baolu };
60555dd4023SYi Liu 
60655dd4023SYi Liu /**
60776236838SJoao Martins  * enum iommufd_hw_capabilities
60876236838SJoao Martins  * @IOMMU_HW_CAP_DIRTY_TRACKING: IOMMU hardware support for dirty tracking
60976236838SJoao Martins  *                               If available, it means the following APIs
61076236838SJoao Martins  *                               are supported:
61176236838SJoao Martins  *
61276236838SJoao Martins  *                                   IOMMU_HWPT_GET_DIRTY_BITMAP
61376236838SJoao Martins  *                                   IOMMU_HWPT_SET_DIRTY_TRACKING
61476236838SJoao Martins  *
615803f9729SYi Liu  * @IOMMU_HW_CAP_PCI_PASID_EXEC: Execute Permission Supported, user ignores it
616803f9729SYi Liu  *                               when the struct
617803f9729SYi Liu  *                               iommu_hw_info::out_max_pasid_log2 is zero.
618803f9729SYi Liu  * @IOMMU_HW_CAP_PCI_PASID_PRIV: Privileged Mode Supported, user ignores it
619803f9729SYi Liu  *                               when the struct
620803f9729SYi Liu  *                               iommu_hw_info::out_max_pasid_log2 is zero.
62176236838SJoao Martins  */
62276236838SJoao Martins enum iommufd_hw_capabilities {
62376236838SJoao Martins 	IOMMU_HW_CAP_DIRTY_TRACKING = 1 << 0,
624803f9729SYi Liu 	IOMMU_HW_CAP_PCI_PASID_EXEC = 1 << 1,
625803f9729SYi Liu 	IOMMU_HW_CAP_PCI_PASID_PRIV = 1 << 2,
62676236838SJoao Martins };
62776236838SJoao Martins 
62876236838SJoao Martins /**
62955dd4023SYi Liu  * struct iommu_hw_info - ioctl(IOMMU_GET_HW_INFO)
63055dd4023SYi Liu  * @size: sizeof(struct iommu_hw_info)
63155dd4023SYi Liu  * @flags: Must be 0
63255dd4023SYi Liu  * @dev_id: The device bound to the iommufd
63355dd4023SYi Liu  * @data_len: Input the length of a user buffer in bytes. Output the length of
63455dd4023SYi Liu  *            data that kernel supports
63555dd4023SYi Liu  * @data_uptr: User pointer to a user-space buffer used by the kernel to fill
63655dd4023SYi Liu  *             the iommu type specific hardware information data
63755dd4023SYi Liu  * @out_data_type: Output the iommu hardware info type as defined in the enum
63855dd4023SYi Liu  *                 iommu_hw_info_type.
63976236838SJoao Martins  * @out_capabilities: Output the generic iommu capability info type as defined
64076236838SJoao Martins  *                    in the enum iommu_hw_capabilities.
641803f9729SYi Liu  * @out_max_pasid_log2: Output the width of PASIDs. 0 means no PASID support.
642803f9729SYi Liu  *                      PCI devices turn to out_capabilities to check if the
643803f9729SYi Liu  *                      specific capabilities is supported or not.
64455dd4023SYi Liu  * @__reserved: Must be 0
64555dd4023SYi Liu  *
64655dd4023SYi Liu  * Query an iommu type specific hardware information data from an iommu behind
64755dd4023SYi Liu  * a given device that has been bound to iommufd. This hardware info data will
64855dd4023SYi Liu  * be used to sync capabilities between the virtual iommu and the physical
64955dd4023SYi Liu  * iommu, e.g. a nested translation setup needs to check the hardware info, so
65055dd4023SYi Liu  * a guest stage-1 page table can be compatible with the physical iommu.
65155dd4023SYi Liu  *
65255dd4023SYi Liu  * To capture an iommu type specific hardware information data, @data_uptr and
65355dd4023SYi Liu  * its length @data_len must be provided. Trailing bytes will be zeroed if the
65455dd4023SYi Liu  * user buffer is larger than the data that kernel has. Otherwise, kernel only
65555dd4023SYi Liu  * fills the buffer using the given length in @data_len. If the ioctl succeeds,
65655dd4023SYi Liu  * @data_len will be updated to the length that kernel actually supports,
65755dd4023SYi Liu  * @out_data_type will be filled to decode the data filled in the buffer
65855dd4023SYi Liu  * pointed by @data_uptr. Input @data_len == zero is allowed.
65955dd4023SYi Liu  */
66055dd4023SYi Liu struct iommu_hw_info {
66155dd4023SYi Liu 	__u32 size;
66255dd4023SYi Liu 	__u32 flags;
66355dd4023SYi Liu 	__u32 dev_id;
66455dd4023SYi Liu 	__u32 data_len;
66555dd4023SYi Liu 	__aligned_u64 data_uptr;
66655dd4023SYi Liu 	__u32 out_data_type;
667803f9729SYi Liu 	__u8 out_max_pasid_log2;
668803f9729SYi Liu 	__u8 __reserved[3];
66976236838SJoao Martins 	__aligned_u64 out_capabilities;
67055dd4023SYi Liu };
67155dd4023SYi Liu #define IOMMU_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_GET_HW_INFO)
672e2a4b294SJoao Martins 
673e2a4b294SJoao Martins /*
674e2a4b294SJoao Martins  * enum iommufd_hwpt_set_dirty_tracking_flags - Flags for steering dirty
675e2a4b294SJoao Martins  *                                              tracking
676e2a4b294SJoao Martins  * @IOMMU_HWPT_DIRTY_TRACKING_ENABLE: Enable dirty tracking
677e2a4b294SJoao Martins  */
678e2a4b294SJoao Martins enum iommufd_hwpt_set_dirty_tracking_flags {
679e2a4b294SJoao Martins 	IOMMU_HWPT_DIRTY_TRACKING_ENABLE = 1,
680e2a4b294SJoao Martins };
681e2a4b294SJoao Martins 
682e2a4b294SJoao Martins /**
683e2a4b294SJoao Martins  * struct iommu_hwpt_set_dirty_tracking - ioctl(IOMMU_HWPT_SET_DIRTY_TRACKING)
684e2a4b294SJoao Martins  * @size: sizeof(struct iommu_hwpt_set_dirty_tracking)
685e2a4b294SJoao Martins  * @flags: Combination of enum iommufd_hwpt_set_dirty_tracking_flags
686e2a4b294SJoao Martins  * @hwpt_id: HW pagetable ID that represents the IOMMU domain
687e2a4b294SJoao Martins  * @__reserved: Must be 0
688e2a4b294SJoao Martins  *
689e2a4b294SJoao Martins  * Toggle dirty tracking on an HW pagetable.
690e2a4b294SJoao Martins  */
691e2a4b294SJoao Martins struct iommu_hwpt_set_dirty_tracking {
692e2a4b294SJoao Martins 	__u32 size;
693e2a4b294SJoao Martins 	__u32 flags;
694e2a4b294SJoao Martins 	__u32 hwpt_id;
695e2a4b294SJoao Martins 	__u32 __reserved;
696e2a4b294SJoao Martins };
697e2a4b294SJoao Martins #define IOMMU_HWPT_SET_DIRTY_TRACKING _IO(IOMMUFD_TYPE, \
698e2a4b294SJoao Martins 					  IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING)
699b9a60d6fSJoao Martins 
700b9a60d6fSJoao Martins /**
70160984813SJoao Martins  * enum iommufd_hwpt_get_dirty_bitmap_flags - Flags for getting dirty bits
70260984813SJoao Martins  * @IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR: Just read the PTEs without clearing
70360984813SJoao Martins  *                                        any dirty bits metadata. This flag
70460984813SJoao Martins  *                                        can be passed in the expectation
70560984813SJoao Martins  *                                        where the next operation is an unmap
70660984813SJoao Martins  *                                        of the same IOVA range.
70760984813SJoao Martins  *
70860984813SJoao Martins  */
70960984813SJoao Martins enum iommufd_hwpt_get_dirty_bitmap_flags {
71060984813SJoao Martins 	IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR = 1,
71160984813SJoao Martins };
71260984813SJoao Martins 
71360984813SJoao Martins /**
714b9a60d6fSJoao Martins  * struct iommu_hwpt_get_dirty_bitmap - ioctl(IOMMU_HWPT_GET_DIRTY_BITMAP)
715b9a60d6fSJoao Martins  * @size: sizeof(struct iommu_hwpt_get_dirty_bitmap)
716b9a60d6fSJoao Martins  * @hwpt_id: HW pagetable ID that represents the IOMMU domain
71760984813SJoao Martins  * @flags: Combination of enum iommufd_hwpt_get_dirty_bitmap_flags
718b9a60d6fSJoao Martins  * @__reserved: Must be 0
719b9a60d6fSJoao Martins  * @iova: base IOVA of the bitmap first bit
720b9a60d6fSJoao Martins  * @length: IOVA range size
721b9a60d6fSJoao Martins  * @page_size: page size granularity of each bit in the bitmap
722b9a60d6fSJoao Martins  * @data: bitmap where to set the dirty bits. The bitmap bits each
723b9a60d6fSJoao Martins  *        represent a page_size which you deviate from an arbitrary iova.
724b9a60d6fSJoao Martins  *
725b9a60d6fSJoao Martins  * Checking a given IOVA is dirty:
726b9a60d6fSJoao Martins  *
727b9a60d6fSJoao Martins  *  data[(iova / page_size) / 64] & (1ULL << ((iova / page_size) % 64))
728b9a60d6fSJoao Martins  *
729b9a60d6fSJoao Martins  * Walk the IOMMU pagetables for a given IOVA range to return a bitmap
730b9a60d6fSJoao Martins  * with the dirty IOVAs. In doing so it will also by default clear any
731b9a60d6fSJoao Martins  * dirty bit metadata set in the IOPTE.
732b9a60d6fSJoao Martins  */
733b9a60d6fSJoao Martins struct iommu_hwpt_get_dirty_bitmap {
734b9a60d6fSJoao Martins 	__u32 size;
735b9a60d6fSJoao Martins 	__u32 hwpt_id;
736b9a60d6fSJoao Martins 	__u32 flags;
737b9a60d6fSJoao Martins 	__u32 __reserved;
738b9a60d6fSJoao Martins 	__aligned_u64 iova;
739b9a60d6fSJoao Martins 	__aligned_u64 length;
740b9a60d6fSJoao Martins 	__aligned_u64 page_size;
741b9a60d6fSJoao Martins 	__aligned_u64 data;
742b9a60d6fSJoao Martins };
743b9a60d6fSJoao Martins #define IOMMU_HWPT_GET_DIRTY_BITMAP _IO(IOMMUFD_TYPE, \
744b9a60d6fSJoao Martins 					IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP)
745b9a60d6fSJoao Martins 
7468c6eabaeSYi Liu /**
7478c6eabaeSYi Liu  * enum iommu_hwpt_invalidate_data_type - IOMMU HWPT Cache Invalidation
7488c6eabaeSYi Liu  *                                        Data Type
7498c6eabaeSYi Liu  * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1
750d68beb27SNicolin Chen  * @IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3: Invalidation data for ARM SMMUv3
7518c6eabaeSYi Liu  */
7528c6eabaeSYi Liu enum iommu_hwpt_invalidate_data_type {
753136a8066SJason Gunthorpe 	IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 = 0,
754d68beb27SNicolin Chen 	IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3 = 1,
7558c6eabaeSYi Liu };
7568c6eabaeSYi Liu 
7578c6eabaeSYi Liu /**
758393a5778SYi Liu  * enum iommu_hwpt_vtd_s1_invalidate_flags - Flags for Intel VT-d
759393a5778SYi Liu  *                                           stage-1 cache invalidation
760393a5778SYi Liu  * @IOMMU_VTD_INV_FLAGS_LEAF: Indicates whether the invalidation applies
761393a5778SYi Liu  *                            to all-levels page structure cache or just
762393a5778SYi Liu  *                            the leaf PTE cache.
763393a5778SYi Liu  */
764393a5778SYi Liu enum iommu_hwpt_vtd_s1_invalidate_flags {
765393a5778SYi Liu 	IOMMU_VTD_INV_FLAGS_LEAF = 1 << 0,
766393a5778SYi Liu };
767393a5778SYi Liu 
768393a5778SYi Liu /**
769393a5778SYi Liu  * struct iommu_hwpt_vtd_s1_invalidate - Intel VT-d cache invalidation
770393a5778SYi Liu  *                                       (IOMMU_HWPT_INVALIDATE_DATA_VTD_S1)
771393a5778SYi Liu  * @addr: The start address of the range to be invalidated. It needs to
772393a5778SYi Liu  *        be 4KB aligned.
773393a5778SYi Liu  * @npages: Number of contiguous 4K pages to be invalidated.
774393a5778SYi Liu  * @flags: Combination of enum iommu_hwpt_vtd_s1_invalidate_flags
775393a5778SYi Liu  * @__reserved: Must be 0
776393a5778SYi Liu  *
777393a5778SYi Liu  * The Intel VT-d specific invalidation data for user-managed stage-1 cache
778393a5778SYi Liu  * invalidation in nested translation. Userspace uses this structure to
779393a5778SYi Liu  * tell the impacted cache scope after modifying the stage-1 page table.
780393a5778SYi Liu  *
781393a5778SYi Liu  * Invalidating all the caches related to the page table by setting @addr
782393a5778SYi Liu  * to be 0 and @npages to be U64_MAX.
783393a5778SYi Liu  *
784393a5778SYi Liu  * The device TLB will be invalidated automatically if ATS is enabled.
785393a5778SYi Liu  */
786393a5778SYi Liu struct iommu_hwpt_vtd_s1_invalidate {
787393a5778SYi Liu 	__aligned_u64 addr;
788393a5778SYi Liu 	__aligned_u64 npages;
789393a5778SYi Liu 	__u32 flags;
790393a5778SYi Liu 	__u32 __reserved;
791393a5778SYi Liu };
792393a5778SYi Liu 
793393a5778SYi Liu /**
7947937a1bfSRandy Dunlap  * struct iommu_viommu_arm_smmuv3_invalidate - ARM SMMUv3 cache invalidation
795d68beb27SNicolin Chen  *         (IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3)
796d68beb27SNicolin Chen  * @cmd: 128-bit cache invalidation command that runs in SMMU CMDQ.
797d68beb27SNicolin Chen  *       Must be little-endian.
798d68beb27SNicolin Chen  *
799d68beb27SNicolin Chen  * Supported command list only when passing in a vIOMMU via @hwpt_id:
800d68beb27SNicolin Chen  *     CMDQ_OP_TLBI_NSNH_ALL
801d68beb27SNicolin Chen  *     CMDQ_OP_TLBI_NH_VA
802d68beb27SNicolin Chen  *     CMDQ_OP_TLBI_NH_VAA
803d68beb27SNicolin Chen  *     CMDQ_OP_TLBI_NH_ALL
804d68beb27SNicolin Chen  *     CMDQ_OP_TLBI_NH_ASID
805d68beb27SNicolin Chen  *     CMDQ_OP_ATC_INV
806d68beb27SNicolin Chen  *     CMDQ_OP_CFGI_CD
807d68beb27SNicolin Chen  *     CMDQ_OP_CFGI_CD_ALL
808d68beb27SNicolin Chen  *
809d68beb27SNicolin Chen  * -EIO will be returned if the command is not supported.
810d68beb27SNicolin Chen  */
811d68beb27SNicolin Chen struct iommu_viommu_arm_smmuv3_invalidate {
812d68beb27SNicolin Chen 	__aligned_le64 cmd[2];
813d68beb27SNicolin Chen };
814d68beb27SNicolin Chen 
815d68beb27SNicolin Chen /**
8168c6eabaeSYi Liu  * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE)
8178c6eabaeSYi Liu  * @size: sizeof(struct iommu_hwpt_invalidate)
81854ce69e3SNicolin Chen  * @hwpt_id: ID of a nested HWPT or a vIOMMU, for cache invalidation
8198c6eabaeSYi Liu  * @data_uptr: User pointer to an array of driver-specific cache invalidation
8208c6eabaeSYi Liu  *             data.
8218c6eabaeSYi Liu  * @data_type: One of enum iommu_hwpt_invalidate_data_type, defining the data
8228c6eabaeSYi Liu  *             type of all the entries in the invalidation request array. It
8238c6eabaeSYi Liu  *             should be a type supported by the hwpt pointed by @hwpt_id.
8248c6eabaeSYi Liu  * @entry_len: Length (in bytes) of a request entry in the request array
8258c6eabaeSYi Liu  * @entry_num: Input the number of cache invalidation requests in the array.
8268c6eabaeSYi Liu  *             Output the number of requests successfully handled by kernel.
8278c6eabaeSYi Liu  * @__reserved: Must be 0.
8288c6eabaeSYi Liu  *
82954ce69e3SNicolin Chen  * Invalidate iommu cache for user-managed page table or vIOMMU. Modifications
83054ce69e3SNicolin Chen  * on a user-managed page table should be followed by this operation, if a HWPT
83154ce69e3SNicolin Chen  * is passed in via @hwpt_id. Other caches, such as device cache or descriptor
83254ce69e3SNicolin Chen  * cache can be flushed if a vIOMMU is passed in via the @hwpt_id field.
83354ce69e3SNicolin Chen  *
8348c6eabaeSYi Liu  * Each ioctl can support one or more cache invalidation requests in the array
8358c6eabaeSYi Liu  * that has a total size of @entry_len * @entry_num.
8368c6eabaeSYi Liu  *
8378c6eabaeSYi Liu  * An empty invalidation request array by setting @entry_num==0 is allowed, and
8388c6eabaeSYi Liu  * @entry_len and @data_uptr would be ignored in this case. This can be used to
8398c6eabaeSYi Liu  * check if the given @data_type is supported or not by kernel.
8408c6eabaeSYi Liu  */
8418c6eabaeSYi Liu struct iommu_hwpt_invalidate {
8428c6eabaeSYi Liu 	__u32 size;
8438c6eabaeSYi Liu 	__u32 hwpt_id;
8448c6eabaeSYi Liu 	__aligned_u64 data_uptr;
8458c6eabaeSYi Liu 	__u32 data_type;
8468c6eabaeSYi Liu 	__u32 entry_len;
8478c6eabaeSYi Liu 	__u32 entry_num;
8488c6eabaeSYi Liu 	__u32 __reserved;
8498c6eabaeSYi Liu };
8508c6eabaeSYi Liu #define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE)
851c714f158SLu Baolu 
852c714f158SLu Baolu /**
853c714f158SLu Baolu  * enum iommu_hwpt_pgfault_flags - flags for struct iommu_hwpt_pgfault
854c714f158SLu Baolu  * @IOMMU_PGFAULT_FLAGS_PASID_VALID: The pasid field of the fault data is
855c714f158SLu Baolu  *                                   valid.
856c714f158SLu Baolu  * @IOMMU_PGFAULT_FLAGS_LAST_PAGE: It's the last fault of a fault group.
857c714f158SLu Baolu  */
858c714f158SLu Baolu enum iommu_hwpt_pgfault_flags {
859c714f158SLu Baolu 	IOMMU_PGFAULT_FLAGS_PASID_VALID		= (1 << 0),
860c714f158SLu Baolu 	IOMMU_PGFAULT_FLAGS_LAST_PAGE		= (1 << 1),
861c714f158SLu Baolu };
862c714f158SLu Baolu 
863c714f158SLu Baolu /**
864c714f158SLu Baolu  * enum iommu_hwpt_pgfault_perm - perm bits for struct iommu_hwpt_pgfault
865c714f158SLu Baolu  * @IOMMU_PGFAULT_PERM_READ: request for read permission
866c714f158SLu Baolu  * @IOMMU_PGFAULT_PERM_WRITE: request for write permission
867c714f158SLu Baolu  * @IOMMU_PGFAULT_PERM_EXEC: (PCIE 10.4.1) request with a PASID that has the
868c714f158SLu Baolu  *                           Execute Requested bit set in PASID TLP Prefix.
869c714f158SLu Baolu  * @IOMMU_PGFAULT_PERM_PRIV: (PCIE 10.4.1) request with a PASID that has the
870c714f158SLu Baolu  *                           Privileged Mode Requested bit set in PASID TLP
871c714f158SLu Baolu  *                           Prefix.
872c714f158SLu Baolu  */
873c714f158SLu Baolu enum iommu_hwpt_pgfault_perm {
874c714f158SLu Baolu 	IOMMU_PGFAULT_PERM_READ			= (1 << 0),
875c714f158SLu Baolu 	IOMMU_PGFAULT_PERM_WRITE		= (1 << 1),
876c714f158SLu Baolu 	IOMMU_PGFAULT_PERM_EXEC			= (1 << 2),
877c714f158SLu Baolu 	IOMMU_PGFAULT_PERM_PRIV			= (1 << 3),
878c714f158SLu Baolu };
879c714f158SLu Baolu 
880c714f158SLu Baolu /**
881c714f158SLu Baolu  * struct iommu_hwpt_pgfault - iommu page fault data
882c714f158SLu Baolu  * @flags: Combination of enum iommu_hwpt_pgfault_flags
883c714f158SLu Baolu  * @dev_id: id of the originated device
884c714f158SLu Baolu  * @pasid: Process Address Space ID
885c714f158SLu Baolu  * @grpid: Page Request Group Index
886c714f158SLu Baolu  * @perm: Combination of enum iommu_hwpt_pgfault_perm
887e721f619SNicolin Chen  * @__reserved: Must be 0.
888c714f158SLu Baolu  * @addr: Fault address
889c714f158SLu Baolu  * @length: a hint of how much data the requestor is expecting to fetch. For
890c714f158SLu Baolu  *          example, if the PRI initiator knows it is going to do a 10MB
891c714f158SLu Baolu  *          transfer, it could fill in 10MB and the OS could pre-fault in
892c714f158SLu Baolu  *          10MB of IOVA. It's default to 0 if there's no such hint.
893c714f158SLu Baolu  * @cookie: kernel-managed cookie identifying a group of fault messages. The
894c714f158SLu Baolu  *          cookie number encoded in the last page fault of the group should
895c714f158SLu Baolu  *          be echoed back in the response message.
896c714f158SLu Baolu  */
897c714f158SLu Baolu struct iommu_hwpt_pgfault {
898c714f158SLu Baolu 	__u32 flags;
899c714f158SLu Baolu 	__u32 dev_id;
900c714f158SLu Baolu 	__u32 pasid;
901c714f158SLu Baolu 	__u32 grpid;
902c714f158SLu Baolu 	__u32 perm;
903e721f619SNicolin Chen 	__u32 __reserved;
904e721f619SNicolin Chen 	__aligned_u64 addr;
905c714f158SLu Baolu 	__u32 length;
906c714f158SLu Baolu 	__u32 cookie;
907c714f158SLu Baolu };
908c714f158SLu Baolu 
909c714f158SLu Baolu /**
910c714f158SLu Baolu  * enum iommufd_page_response_code - Return status of fault handlers
911c714f158SLu Baolu  * @IOMMUFD_PAGE_RESP_SUCCESS: Fault has been handled and the page tables
912c714f158SLu Baolu  *                             populated, retry the access. This is the
913c714f158SLu Baolu  *                             "Success" defined in PCI 10.4.2.1.
914c714f158SLu Baolu  * @IOMMUFD_PAGE_RESP_INVALID: Could not handle this fault, don't retry the
915c714f158SLu Baolu  *                             access. This is the "Invalid Request" in PCI
916c714f158SLu Baolu  *                             10.4.2.1.
917c714f158SLu Baolu  */
918c714f158SLu Baolu enum iommufd_page_response_code {
919c714f158SLu Baolu 	IOMMUFD_PAGE_RESP_SUCCESS = 0,
920136a8066SJason Gunthorpe 	IOMMUFD_PAGE_RESP_INVALID = 1,
921c714f158SLu Baolu };
922c714f158SLu Baolu 
923c714f158SLu Baolu /**
924c714f158SLu Baolu  * struct iommu_hwpt_page_response - IOMMU page fault response
925c714f158SLu Baolu  * @cookie: The kernel-managed cookie reported in the fault message.
926c714f158SLu Baolu  * @code: One of response code in enum iommufd_page_response_code.
927c714f158SLu Baolu  */
928c714f158SLu Baolu struct iommu_hwpt_page_response {
929c714f158SLu Baolu 	__u32 cookie;
930c714f158SLu Baolu 	__u32 code;
931c714f158SLu Baolu };
93207838f7fSLu Baolu 
93307838f7fSLu Baolu /**
93407838f7fSLu Baolu  * struct iommu_fault_alloc - ioctl(IOMMU_FAULT_QUEUE_ALLOC)
93507838f7fSLu Baolu  * @size: sizeof(struct iommu_fault_alloc)
93607838f7fSLu Baolu  * @flags: Must be 0
93707838f7fSLu Baolu  * @out_fault_id: The ID of the new FAULT
93807838f7fSLu Baolu  * @out_fault_fd: The fd of the new FAULT
93907838f7fSLu Baolu  *
94007838f7fSLu Baolu  * Explicitly allocate a fault handling object.
94107838f7fSLu Baolu  */
94207838f7fSLu Baolu struct iommu_fault_alloc {
94307838f7fSLu Baolu 	__u32 size;
94407838f7fSLu Baolu 	__u32 flags;
94507838f7fSLu Baolu 	__u32 out_fault_id;
94607838f7fSLu Baolu 	__u32 out_fault_fd;
94707838f7fSLu Baolu };
94807838f7fSLu Baolu #define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC)
9494db97c21SNicolin Chen 
9504db97c21SNicolin Chen /**
9514db97c21SNicolin Chen  * enum iommu_viommu_type - Virtual IOMMU Type
9524db97c21SNicolin Chen  * @IOMMU_VIOMMU_TYPE_DEFAULT: Reserved for future use
95369d9b312SNicolin Chen  * @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type
9544db97c21SNicolin Chen  */
9554db97c21SNicolin Chen enum iommu_viommu_type {
9564db97c21SNicolin Chen 	IOMMU_VIOMMU_TYPE_DEFAULT = 0,
95769d9b312SNicolin Chen 	IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1,
9584db97c21SNicolin Chen };
9594db97c21SNicolin Chen 
9604db97c21SNicolin Chen /**
9614db97c21SNicolin Chen  * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC)
9624db97c21SNicolin Chen  * @size: sizeof(struct iommu_viommu_alloc)
9634db97c21SNicolin Chen  * @flags: Must be 0
9644db97c21SNicolin Chen  * @type: Type of the virtual IOMMU. Must be defined in enum iommu_viommu_type
9654db97c21SNicolin Chen  * @dev_id: The device's physical IOMMU will be used to back the virtual IOMMU
9664db97c21SNicolin Chen  * @hwpt_id: ID of a nesting parent HWPT to associate to
9674db97c21SNicolin Chen  * @out_viommu_id: Output virtual IOMMU ID for the allocated object
9684db97c21SNicolin Chen  *
9694db97c21SNicolin Chen  * Allocate a virtual IOMMU object, representing the underlying physical IOMMU's
9704db97c21SNicolin Chen  * virtualization support that is a security-isolated slice of the real IOMMU HW
9714db97c21SNicolin Chen  * that is unique to a specific VM. Operations global to the IOMMU are connected
9724db97c21SNicolin Chen  * to the vIOMMU, such as:
9734db97c21SNicolin Chen  * - Security namespace for guest owned ID, e.g. guest-controlled cache tags
9744db97c21SNicolin Chen  * - Non-device-affiliated event reporting, e.g. invalidation queue errors
9754db97c21SNicolin Chen  * - Access to a sharable nesting parent pagetable across physical IOMMUs
9764db97c21SNicolin Chen  * - Virtualization of various platforms IDs, e.g. RIDs and others
9774db97c21SNicolin Chen  * - Delivery of paravirtualized invalidation
9784db97c21SNicolin Chen  * - Direct assigned invalidation queues
9794db97c21SNicolin Chen  * - Direct assigned interrupts
9804db97c21SNicolin Chen  */
9814db97c21SNicolin Chen struct iommu_viommu_alloc {
9824db97c21SNicolin Chen 	__u32 size;
9834db97c21SNicolin Chen 	__u32 flags;
9844db97c21SNicolin Chen 	__u32 type;
9854db97c21SNicolin Chen 	__u32 dev_id;
9864db97c21SNicolin Chen 	__u32 hwpt_id;
9874db97c21SNicolin Chen 	__u32 out_viommu_id;
9884db97c21SNicolin Chen };
9894db97c21SNicolin Chen #define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)
9900ce5c247SNicolin Chen 
9910ce5c247SNicolin Chen /**
9920ce5c247SNicolin Chen  * struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC)
9930ce5c247SNicolin Chen  * @size: sizeof(struct iommu_vdevice_alloc)
9940ce5c247SNicolin Chen  * @viommu_id: vIOMMU ID to associate with the virtual device
9950ce5c247SNicolin Chen  * @dev_id: The physical device to allocate a virtual instance on the vIOMMU
9960ce5c247SNicolin Chen  * @out_vdevice_id: Object handle for the vDevice. Pass to IOMMU_DESTORY
9970ce5c247SNicolin Chen  * @virt_id: Virtual device ID per vIOMMU, e.g. vSID of ARM SMMUv3, vDeviceID
9980ce5c247SNicolin Chen  *           of AMD IOMMU, and vRID of a nested Intel VT-d to a Context Table
9990ce5c247SNicolin Chen  *
10000ce5c247SNicolin Chen  * Allocate a virtual device instance (for a physical device) against a vIOMMU.
10010ce5c247SNicolin Chen  * This instance holds the device's information (related to its vIOMMU) in a VM.
10020ce5c247SNicolin Chen  */
10030ce5c247SNicolin Chen struct iommu_vdevice_alloc {
10040ce5c247SNicolin Chen 	__u32 size;
10050ce5c247SNicolin Chen 	__u32 viommu_id;
10060ce5c247SNicolin Chen 	__u32 dev_id;
10070ce5c247SNicolin Chen 	__u32 out_vdevice_id;
10080ce5c247SNicolin Chen 	__aligned_u64 virt_id;
10090ce5c247SNicolin Chen };
10100ce5c247SNicolin Chen #define IOMMU_VDEVICE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_ALLOC)
1011829ed626SSteve Sistare 
1012829ed626SSteve Sistare /**
1013829ed626SSteve Sistare  * struct iommu_ioas_change_process - ioctl(VFIO_IOAS_CHANGE_PROCESS)
1014829ed626SSteve Sistare  * @size: sizeof(struct iommu_ioas_change_process)
1015829ed626SSteve Sistare  * @__reserved: Must be 0
1016829ed626SSteve Sistare  *
1017829ed626SSteve Sistare  * This transfers pinned memory counts for every memory map in every IOAS
1018829ed626SSteve Sistare  * in the context to the current process.  This only supports maps created
1019829ed626SSteve Sistare  * with IOMMU_IOAS_MAP_FILE, and returns EINVAL if other maps are present.
1020829ed626SSteve Sistare  * If the ioctl returns a failure status, then nothing is changed.
1021829ed626SSteve Sistare  *
1022829ed626SSteve Sistare  * This API is useful for transferring operation of a device from one process
1023829ed626SSteve Sistare  * to another, such as during userland live update.
1024829ed626SSteve Sistare  */
1025829ed626SSteve Sistare struct iommu_ioas_change_process {
1026829ed626SSteve Sistare 	__u32 size;
1027829ed626SSteve Sistare 	__u32 __reserved;
1028829ed626SSteve Sistare };
1029829ed626SSteve Sistare 
1030829ed626SSteve Sistare #define IOMMU_IOAS_CHANGE_PROCESS \
1031829ed626SSteve Sistare 	_IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_CHANGE_PROCESS)
1032829ed626SSteve Sistare 
1033e36ba5abSNicolin Chen /**
1034e36ba5abSNicolin Chen  * enum iommu_veventq_flag - flag for struct iommufd_vevent_header
1035e36ba5abSNicolin Chen  * @IOMMU_VEVENTQ_FLAG_LOST_EVENTS: vEVENTQ has lost vEVENTs
1036e36ba5abSNicolin Chen  */
1037e36ba5abSNicolin Chen enum iommu_veventq_flag {
1038e36ba5abSNicolin Chen 	IOMMU_VEVENTQ_FLAG_LOST_EVENTS = (1U << 0),
1039e36ba5abSNicolin Chen };
1040e36ba5abSNicolin Chen 
1041e36ba5abSNicolin Chen /**
1042e36ba5abSNicolin Chen  * struct iommufd_vevent_header - Virtual Event Header for a vEVENTQ Status
1043e36ba5abSNicolin Chen  * @flags: Combination of enum iommu_veventq_flag
1044e36ba5abSNicolin Chen  * @sequence: The sequence index of a vEVENT in the vEVENTQ, with a range of
1045e36ba5abSNicolin Chen  *            [0, INT_MAX] where the following index of INT_MAX is 0
1046e36ba5abSNicolin Chen  *
1047e36ba5abSNicolin Chen  * Each iommufd_vevent_header reports a sequence index of the following vEVENT:
1048*858c9c10SBagas Sanjaya  *
1049*858c9c10SBagas Sanjaya  * +----------------------+-------+----------------------+-------+---+-------+
1050e36ba5abSNicolin Chen  * | header0 {sequence=0} | data0 | header1 {sequence=1} | data1 |...| dataN |
1051*858c9c10SBagas Sanjaya  * +----------------------+-------+----------------------+-------+---+-------+
1052*858c9c10SBagas Sanjaya  *
1053e36ba5abSNicolin Chen  * And this sequence index is expected to be monotonic to the sequence index of
1054e36ba5abSNicolin Chen  * the previous vEVENT. If two adjacent sequence indexes has a delta larger than
1055e36ba5abSNicolin Chen  * 1, it means that delta - 1 number of vEVENTs has lost, e.g. two lost vEVENTs:
1056*858c9c10SBagas Sanjaya  *
1057*858c9c10SBagas Sanjaya  * +-----+----------------------+-------+----------------------+-------+-----+
1058e36ba5abSNicolin Chen  * | ... | header3 {sequence=3} | data3 | header6 {sequence=6} | data6 | ... |
1059*858c9c10SBagas Sanjaya  * +-----+----------------------+-------+----------------------+-------+-----+
1060*858c9c10SBagas Sanjaya  *
1061e36ba5abSNicolin Chen  * If a vEVENT lost at the tail of the vEVENTQ and there is no following vEVENT
1062e36ba5abSNicolin Chen  * providing the next sequence index, an IOMMU_VEVENTQ_FLAG_LOST_EVENTS header
1063e36ba5abSNicolin Chen  * would be added to the tail, and no data would follow this header:
1064*858c9c10SBagas Sanjaya  *
1065*858c9c10SBagas Sanjaya  * +--+----------------------+-------+-----------------------------------------+
1066e36ba5abSNicolin Chen  * |..| header3 {sequence=3} | data3 | header4 {flags=LOST_EVENTS, sequence=4} |
1067*858c9c10SBagas Sanjaya  * +--+----------------------+-------+-----------------------------------------+
1068e36ba5abSNicolin Chen  */
1069e36ba5abSNicolin Chen struct iommufd_vevent_header {
1070e36ba5abSNicolin Chen 	__u32 flags;
1071e36ba5abSNicolin Chen 	__u32 sequence;
1072e36ba5abSNicolin Chen };
1073e36ba5abSNicolin Chen 
1074e36ba5abSNicolin Chen /**
1075e36ba5abSNicolin Chen  * enum iommu_veventq_type - Virtual Event Queue Type
1076e36ba5abSNicolin Chen  * @IOMMU_VEVENTQ_TYPE_DEFAULT: Reserved for future use
1077e7d3fa3dSNicolin Chen  * @IOMMU_VEVENTQ_TYPE_ARM_SMMUV3: ARM SMMUv3 Virtual Event Queue
1078e36ba5abSNicolin Chen  */
1079e36ba5abSNicolin Chen enum iommu_veventq_type {
1080e36ba5abSNicolin Chen 	IOMMU_VEVENTQ_TYPE_DEFAULT = 0,
1081e7d3fa3dSNicolin Chen 	IOMMU_VEVENTQ_TYPE_ARM_SMMUV3 = 1,
1082e7d3fa3dSNicolin Chen };
1083e7d3fa3dSNicolin Chen 
1084e7d3fa3dSNicolin Chen /**
1085e7d3fa3dSNicolin Chen  * struct iommu_vevent_arm_smmuv3 - ARM SMMUv3 Virtual Event
1086e7d3fa3dSNicolin Chen  *                                  (IOMMU_VEVENTQ_TYPE_ARM_SMMUV3)
1087e7d3fa3dSNicolin Chen  * @evt: 256-bit ARM SMMUv3 Event record, little-endian.
1088e7d3fa3dSNicolin Chen  *       Reported event records: (Refer to "7.3 Event records" in SMMUv3 HW Spec)
1089e7d3fa3dSNicolin Chen  *       - 0x04 C_BAD_STE
1090e7d3fa3dSNicolin Chen  *       - 0x06 F_STREAM_DISABLED
1091e7d3fa3dSNicolin Chen  *       - 0x08 C_BAD_SUBSTREAMID
1092e7d3fa3dSNicolin Chen  *       - 0x0a C_BAD_CD
1093e7d3fa3dSNicolin Chen  *       - 0x10 F_TRANSLATION
1094e7d3fa3dSNicolin Chen  *       - 0x11 F_ADDR_SIZE
1095e7d3fa3dSNicolin Chen  *       - 0x12 F_ACCESS
1096e7d3fa3dSNicolin Chen  *       - 0x13 F_PERMISSION
1097e7d3fa3dSNicolin Chen  *
1098e7d3fa3dSNicolin Chen  * StreamID field reports a virtual device ID. To receive a virtual event for a
1099e7d3fa3dSNicolin Chen  * device, a vDEVICE must be allocated via IOMMU_VDEVICE_ALLOC.
1100e7d3fa3dSNicolin Chen  */
1101e7d3fa3dSNicolin Chen struct iommu_vevent_arm_smmuv3 {
1102e7d3fa3dSNicolin Chen 	__aligned_le64 evt[4];
1103e36ba5abSNicolin Chen };
1104e36ba5abSNicolin Chen 
1105e36ba5abSNicolin Chen /**
1106e36ba5abSNicolin Chen  * struct iommu_veventq_alloc - ioctl(IOMMU_VEVENTQ_ALLOC)
1107e36ba5abSNicolin Chen  * @size: sizeof(struct iommu_veventq_alloc)
1108e36ba5abSNicolin Chen  * @flags: Must be 0
1109e36ba5abSNicolin Chen  * @viommu_id: virtual IOMMU ID to associate the vEVENTQ with
1110e36ba5abSNicolin Chen  * @type: Type of the vEVENTQ. Must be defined in enum iommu_veventq_type
1111e36ba5abSNicolin Chen  * @veventq_depth: Maximum number of events in the vEVENTQ
1112e36ba5abSNicolin Chen  * @out_veventq_id: The ID of the new vEVENTQ
1113e36ba5abSNicolin Chen  * @out_veventq_fd: The fd of the new vEVENTQ. User space must close the
1114e36ba5abSNicolin Chen  *                  successfully returned fd after using it
1115e36ba5abSNicolin Chen  * @__reserved: Must be 0
1116e36ba5abSNicolin Chen  *
1117e36ba5abSNicolin Chen  * Explicitly allocate a virtual event queue interface for a vIOMMU. A vIOMMU
1118e36ba5abSNicolin Chen  * can have multiple FDs for different types, but is confined to one per @type.
1119e36ba5abSNicolin Chen  * User space should open the @out_veventq_fd to read vEVENTs out of a vEVENTQ,
1120e36ba5abSNicolin Chen  * if there are vEVENTs available. A vEVENTQ will lose events due to overflow,
1121e36ba5abSNicolin Chen  * if the number of the vEVENTs hits @veventq_depth.
1122e36ba5abSNicolin Chen  *
1123e36ba5abSNicolin Chen  * Each vEVENT in a vEVENTQ encloses a struct iommufd_vevent_header followed by
1124e36ba5abSNicolin Chen  * a type-specific data structure, in a normal case:
1125*858c9c10SBagas Sanjaya  *
1126*858c9c10SBagas Sanjaya  * +-+---------+-------+---------+-------+-----+---------+-------+-+
1127e36ba5abSNicolin Chen  * | | header0 | data0 | header1 | data1 | ... | headerN | dataN | |
1128*858c9c10SBagas Sanjaya  * +-+---------+-------+---------+-------+-----+---------+-------+-+
1129*858c9c10SBagas Sanjaya  *
1130e36ba5abSNicolin Chen  * unless a tailing IOMMU_VEVENTQ_FLAG_LOST_EVENTS header is logged (refer to
1131e36ba5abSNicolin Chen  * struct iommufd_vevent_header).
1132e36ba5abSNicolin Chen  */
1133e36ba5abSNicolin Chen struct iommu_veventq_alloc {
1134e36ba5abSNicolin Chen 	__u32 size;
1135e36ba5abSNicolin Chen 	__u32 flags;
1136e36ba5abSNicolin Chen 	__u32 viommu_id;
1137e36ba5abSNicolin Chen 	__u32 type;
1138e36ba5abSNicolin Chen 	__u32 veventq_depth;
1139e36ba5abSNicolin Chen 	__u32 out_veventq_id;
1140e36ba5abSNicolin Chen 	__u32 out_veventq_fd;
1141e36ba5abSNicolin Chen 	__u32 __reserved;
1142e36ba5abSNicolin Chen };
1143e36ba5abSNicolin Chen #define IOMMU_VEVENTQ_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VEVENTQ_ALLOC)
11442ff4bed7SJason Gunthorpe #endif
1145