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