1f7cbfa71SZhenzhong Duan /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2f7cbfa71SZhenzhong Duan /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. 3f7cbfa71SZhenzhong Duan */ 4f7cbfa71SZhenzhong Duan #ifndef _IOMMUFD_H 5f7cbfa71SZhenzhong Duan #define _IOMMUFD_H 6f7cbfa71SZhenzhong Duan 7f7cbfa71SZhenzhong Duan #include <linux/ioctl.h> 8*0d2eeef7SBibo Mao #include <linux/types.h> 9f7cbfa71SZhenzhong Duan 10f7cbfa71SZhenzhong Duan #define IOMMUFD_TYPE (';') 11f7cbfa71SZhenzhong Duan 12f7cbfa71SZhenzhong Duan /** 13f7cbfa71SZhenzhong Duan * DOC: General ioctl format 14f7cbfa71SZhenzhong Duan * 15f7cbfa71SZhenzhong Duan * The ioctl interface follows a general format to allow for extensibility. Each 16f7cbfa71SZhenzhong Duan * ioctl is passed in a structure pointer as the argument providing the size of 17f7cbfa71SZhenzhong Duan * the structure in the first u32. The kernel checks that any structure space 18f7cbfa71SZhenzhong Duan * beyond what it understands is 0. This allows userspace to use the backward 19f7cbfa71SZhenzhong Duan * compatible portion while consistently using the newer, larger, structures. 20f7cbfa71SZhenzhong Duan * 21f7cbfa71SZhenzhong Duan * ioctls use a standard meaning for common errnos: 22f7cbfa71SZhenzhong Duan * 23f7cbfa71SZhenzhong Duan * - ENOTTY: The IOCTL number itself is not supported at all 24f7cbfa71SZhenzhong Duan * - E2BIG: The IOCTL number is supported, but the provided structure has 25f7cbfa71SZhenzhong Duan * non-zero in a part the kernel does not understand. 26f7cbfa71SZhenzhong Duan * - EOPNOTSUPP: The IOCTL number is supported, and the structure is 27f7cbfa71SZhenzhong Duan * understood, however a known field has a value the kernel does not 28f7cbfa71SZhenzhong Duan * understand or support. 29f7cbfa71SZhenzhong Duan * - EINVAL: Everything about the IOCTL was understood, but a field is not 30f7cbfa71SZhenzhong Duan * correct. 31f7cbfa71SZhenzhong Duan * - ENOENT: An ID or IOVA provided does not exist. 32f7cbfa71SZhenzhong Duan * - ENOMEM: Out of memory. 33f7cbfa71SZhenzhong Duan * - EOVERFLOW: Mathematics overflowed. 34f7cbfa71SZhenzhong Duan * 35f7cbfa71SZhenzhong Duan * As well as additional errnos, within specific ioctls. 36f7cbfa71SZhenzhong Duan */ 37f7cbfa71SZhenzhong Duan enum { 38f7cbfa71SZhenzhong Duan IOMMUFD_CMD_BASE = 0x80, 39f7cbfa71SZhenzhong Duan IOMMUFD_CMD_DESTROY = IOMMUFD_CMD_BASE, 40*0d2eeef7SBibo Mao IOMMUFD_CMD_IOAS_ALLOC = 0x81, 41*0d2eeef7SBibo Mao IOMMUFD_CMD_IOAS_ALLOW_IOVAS = 0x82, 42*0d2eeef7SBibo Mao IOMMUFD_CMD_IOAS_COPY = 0x83, 43*0d2eeef7SBibo Mao IOMMUFD_CMD_IOAS_IOVA_RANGES = 0x84, 44*0d2eeef7SBibo Mao IOMMUFD_CMD_IOAS_MAP = 0x85, 45*0d2eeef7SBibo Mao IOMMUFD_CMD_IOAS_UNMAP = 0x86, 46*0d2eeef7SBibo Mao IOMMUFD_CMD_OPTION = 0x87, 47*0d2eeef7SBibo Mao IOMMUFD_CMD_VFIO_IOAS = 0x88, 48*0d2eeef7SBibo Mao IOMMUFD_CMD_HWPT_ALLOC = 0x89, 49*0d2eeef7SBibo Mao IOMMUFD_CMD_GET_HW_INFO = 0x8a, 50*0d2eeef7SBibo Mao IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING = 0x8b, 51*0d2eeef7SBibo Mao IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP = 0x8c, 52*0d2eeef7SBibo Mao IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d, 53*0d2eeef7SBibo Mao IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e, 54f7cbfa71SZhenzhong Duan }; 55f7cbfa71SZhenzhong Duan 56f7cbfa71SZhenzhong Duan /** 57f7cbfa71SZhenzhong Duan * struct iommu_destroy - ioctl(IOMMU_DESTROY) 58f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_destroy) 59f7cbfa71SZhenzhong Duan * @id: iommufd object ID to destroy. Can be any destroyable object type. 60f7cbfa71SZhenzhong Duan * 61f7cbfa71SZhenzhong Duan * Destroy any object held within iommufd. 62f7cbfa71SZhenzhong Duan */ 63f7cbfa71SZhenzhong Duan struct iommu_destroy { 64f7cbfa71SZhenzhong Duan __u32 size; 65f7cbfa71SZhenzhong Duan __u32 id; 66f7cbfa71SZhenzhong Duan }; 67f7cbfa71SZhenzhong Duan #define IOMMU_DESTROY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DESTROY) 68f7cbfa71SZhenzhong Duan 69f7cbfa71SZhenzhong Duan /** 70f7cbfa71SZhenzhong Duan * struct iommu_ioas_alloc - ioctl(IOMMU_IOAS_ALLOC) 71f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_ioas_alloc) 72f7cbfa71SZhenzhong Duan * @flags: Must be 0 73f7cbfa71SZhenzhong Duan * @out_ioas_id: Output IOAS ID for the allocated object 74f7cbfa71SZhenzhong Duan * 75f7cbfa71SZhenzhong Duan * Allocate an IO Address Space (IOAS) which holds an IO Virtual Address (IOVA) 76f7cbfa71SZhenzhong Duan * to memory mapping. 77f7cbfa71SZhenzhong Duan */ 78f7cbfa71SZhenzhong Duan struct iommu_ioas_alloc { 79f7cbfa71SZhenzhong Duan __u32 size; 80f7cbfa71SZhenzhong Duan __u32 flags; 81f7cbfa71SZhenzhong Duan __u32 out_ioas_id; 82f7cbfa71SZhenzhong Duan }; 83f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOC) 84f7cbfa71SZhenzhong Duan 85f7cbfa71SZhenzhong Duan /** 86f7cbfa71SZhenzhong Duan * struct iommu_iova_range - ioctl(IOMMU_IOVA_RANGE) 87f7cbfa71SZhenzhong Duan * @start: First IOVA 88f7cbfa71SZhenzhong Duan * @last: Inclusive last IOVA 89f7cbfa71SZhenzhong Duan * 90f7cbfa71SZhenzhong Duan * An interval in IOVA space. 91f7cbfa71SZhenzhong Duan */ 92f7cbfa71SZhenzhong Duan struct iommu_iova_range { 93f7cbfa71SZhenzhong Duan __aligned_u64 start; 94f7cbfa71SZhenzhong Duan __aligned_u64 last; 95f7cbfa71SZhenzhong Duan }; 96f7cbfa71SZhenzhong Duan 97f7cbfa71SZhenzhong Duan /** 98f7cbfa71SZhenzhong Duan * struct iommu_ioas_iova_ranges - ioctl(IOMMU_IOAS_IOVA_RANGES) 99f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_ioas_iova_ranges) 100f7cbfa71SZhenzhong Duan * @ioas_id: IOAS ID to read ranges from 101f7cbfa71SZhenzhong Duan * @num_iovas: Input/Output total number of ranges in the IOAS 102f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 103f7cbfa71SZhenzhong Duan * @allowed_iovas: Pointer to the output array of struct iommu_iova_range 104f7cbfa71SZhenzhong Duan * @out_iova_alignment: Minimum alignment required for mapping IOVA 105f7cbfa71SZhenzhong Duan * 106f7cbfa71SZhenzhong Duan * Query an IOAS for ranges of allowed IOVAs. Mapping IOVA outside these ranges 107f7cbfa71SZhenzhong Duan * is not allowed. num_iovas will be set to the total number of iovas and 108f7cbfa71SZhenzhong Duan * the allowed_iovas[] will be filled in as space permits. 109f7cbfa71SZhenzhong Duan * 110f7cbfa71SZhenzhong Duan * The allowed ranges are dependent on the HW path the DMA operation takes, and 111f7cbfa71SZhenzhong Duan * can change during the lifetime of the IOAS. A fresh empty IOAS will have a 112f7cbfa71SZhenzhong Duan * full range, and each attached device will narrow the ranges based on that 113f7cbfa71SZhenzhong Duan * device's HW restrictions. Detaching a device can widen the ranges. Userspace 114f7cbfa71SZhenzhong Duan * should query ranges after every attach/detach to know what IOVAs are valid 115f7cbfa71SZhenzhong Duan * for mapping. 116f7cbfa71SZhenzhong Duan * 117f7cbfa71SZhenzhong Duan * On input num_iovas is the length of the allowed_iovas array. On output it is 118f7cbfa71SZhenzhong Duan * the total number of iovas filled in. The ioctl will return -EMSGSIZE and set 119f7cbfa71SZhenzhong Duan * num_iovas to the required value if num_iovas is too small. In this case the 120f7cbfa71SZhenzhong Duan * caller should allocate a larger output array and re-issue the ioctl. 121f7cbfa71SZhenzhong Duan * 122f7cbfa71SZhenzhong Duan * out_iova_alignment returns the minimum IOVA alignment that can be given 123f7cbfa71SZhenzhong Duan * to IOMMU_IOAS_MAP/COPY. IOVA's must satisfy:: 124f7cbfa71SZhenzhong Duan * 125f7cbfa71SZhenzhong Duan * starting_iova % out_iova_alignment == 0 126f7cbfa71SZhenzhong Duan * (starting_iova + length) % out_iova_alignment == 0 127f7cbfa71SZhenzhong Duan * 128f7cbfa71SZhenzhong Duan * out_iova_alignment can be 1 indicating any IOVA is allowed. It cannot 129f7cbfa71SZhenzhong Duan * be higher than the system PAGE_SIZE. 130f7cbfa71SZhenzhong Duan */ 131f7cbfa71SZhenzhong Duan struct iommu_ioas_iova_ranges { 132f7cbfa71SZhenzhong Duan __u32 size; 133f7cbfa71SZhenzhong Duan __u32 ioas_id; 134f7cbfa71SZhenzhong Duan __u32 num_iovas; 135f7cbfa71SZhenzhong Duan __u32 __reserved; 136f7cbfa71SZhenzhong Duan __aligned_u64 allowed_iovas; 137f7cbfa71SZhenzhong Duan __aligned_u64 out_iova_alignment; 138f7cbfa71SZhenzhong Duan }; 139f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_IOVA_RANGES _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_IOVA_RANGES) 140f7cbfa71SZhenzhong Duan 141f7cbfa71SZhenzhong Duan /** 142f7cbfa71SZhenzhong Duan * struct iommu_ioas_allow_iovas - ioctl(IOMMU_IOAS_ALLOW_IOVAS) 143f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_ioas_allow_iovas) 144f7cbfa71SZhenzhong Duan * @ioas_id: IOAS ID to allow IOVAs from 145f7cbfa71SZhenzhong Duan * @num_iovas: Input/Output total number of ranges in the IOAS 146f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 147f7cbfa71SZhenzhong Duan * @allowed_iovas: Pointer to array of struct iommu_iova_range 148f7cbfa71SZhenzhong Duan * 149f7cbfa71SZhenzhong Duan * Ensure a range of IOVAs are always available for allocation. If this call 150f7cbfa71SZhenzhong Duan * succeeds then IOMMU_IOAS_IOVA_RANGES will never return a list of IOVA ranges 151f7cbfa71SZhenzhong Duan * that are narrower than the ranges provided here. This call will fail if 152f7cbfa71SZhenzhong Duan * IOMMU_IOAS_IOVA_RANGES is currently narrower than the given ranges. 153f7cbfa71SZhenzhong Duan * 154f7cbfa71SZhenzhong Duan * When an IOAS is first created the IOVA_RANGES will be maximally sized, and as 155f7cbfa71SZhenzhong Duan * devices are attached the IOVA will narrow based on the device restrictions. 156f7cbfa71SZhenzhong Duan * When an allowed range is specified any narrowing will be refused, ie device 157f7cbfa71SZhenzhong Duan * attachment can fail if the device requires limiting within the allowed range. 158f7cbfa71SZhenzhong Duan * 159f7cbfa71SZhenzhong Duan * Automatic IOVA allocation is also impacted by this call. MAP will only 160f7cbfa71SZhenzhong Duan * allocate within the allowed IOVAs if they are present. 161f7cbfa71SZhenzhong Duan * 162f7cbfa71SZhenzhong Duan * This call replaces the entire allowed list with the given list. 163f7cbfa71SZhenzhong Duan */ 164f7cbfa71SZhenzhong Duan struct iommu_ioas_allow_iovas { 165f7cbfa71SZhenzhong Duan __u32 size; 166f7cbfa71SZhenzhong Duan __u32 ioas_id; 167f7cbfa71SZhenzhong Duan __u32 num_iovas; 168f7cbfa71SZhenzhong Duan __u32 __reserved; 169f7cbfa71SZhenzhong Duan __aligned_u64 allowed_iovas; 170f7cbfa71SZhenzhong Duan }; 171f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_ALLOW_IOVAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_ALLOW_IOVAS) 172f7cbfa71SZhenzhong Duan 173f7cbfa71SZhenzhong Duan /** 174f7cbfa71SZhenzhong Duan * enum iommufd_ioas_map_flags - Flags for map and copy 175f7cbfa71SZhenzhong Duan * @IOMMU_IOAS_MAP_FIXED_IOVA: If clear the kernel will compute an appropriate 176f7cbfa71SZhenzhong Duan * IOVA to place the mapping at 177f7cbfa71SZhenzhong Duan * @IOMMU_IOAS_MAP_WRITEABLE: DMA is allowed to write to this mapping 178f7cbfa71SZhenzhong Duan * @IOMMU_IOAS_MAP_READABLE: DMA is allowed to read from this mapping 179f7cbfa71SZhenzhong Duan */ 180f7cbfa71SZhenzhong Duan enum iommufd_ioas_map_flags { 181f7cbfa71SZhenzhong Duan IOMMU_IOAS_MAP_FIXED_IOVA = 1 << 0, 182f7cbfa71SZhenzhong Duan IOMMU_IOAS_MAP_WRITEABLE = 1 << 1, 183f7cbfa71SZhenzhong Duan IOMMU_IOAS_MAP_READABLE = 1 << 2, 184f7cbfa71SZhenzhong Duan }; 185f7cbfa71SZhenzhong Duan 186f7cbfa71SZhenzhong Duan /** 187f7cbfa71SZhenzhong Duan * struct iommu_ioas_map - ioctl(IOMMU_IOAS_MAP) 188f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_ioas_map) 189f7cbfa71SZhenzhong Duan * @flags: Combination of enum iommufd_ioas_map_flags 190f7cbfa71SZhenzhong Duan * @ioas_id: IOAS ID to change the mapping of 191f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 192f7cbfa71SZhenzhong Duan * @user_va: Userspace pointer to start mapping from 193f7cbfa71SZhenzhong Duan * @length: Number of bytes to map 194f7cbfa71SZhenzhong Duan * @iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is set 195f7cbfa71SZhenzhong Duan * then this must be provided as input. 196f7cbfa71SZhenzhong Duan * 197f7cbfa71SZhenzhong Duan * Set an IOVA mapping from a user pointer. If FIXED_IOVA is specified then the 198f7cbfa71SZhenzhong Duan * mapping will be established at iova, otherwise a suitable location based on 199f7cbfa71SZhenzhong Duan * the reserved and allowed lists will be automatically selected and returned in 200f7cbfa71SZhenzhong Duan * iova. 201f7cbfa71SZhenzhong Duan * 202f7cbfa71SZhenzhong Duan * If IOMMU_IOAS_MAP_FIXED_IOVA is specified then the iova range must currently 203f7cbfa71SZhenzhong Duan * be unused, existing IOVA cannot be replaced. 204f7cbfa71SZhenzhong Duan */ 205f7cbfa71SZhenzhong Duan struct iommu_ioas_map { 206f7cbfa71SZhenzhong Duan __u32 size; 207f7cbfa71SZhenzhong Duan __u32 flags; 208f7cbfa71SZhenzhong Duan __u32 ioas_id; 209f7cbfa71SZhenzhong Duan __u32 __reserved; 210f7cbfa71SZhenzhong Duan __aligned_u64 user_va; 211f7cbfa71SZhenzhong Duan __aligned_u64 length; 212f7cbfa71SZhenzhong Duan __aligned_u64 iova; 213f7cbfa71SZhenzhong Duan }; 214f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP) 215f7cbfa71SZhenzhong Duan 216f7cbfa71SZhenzhong Duan /** 217f7cbfa71SZhenzhong Duan * struct iommu_ioas_copy - ioctl(IOMMU_IOAS_COPY) 218f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_ioas_copy) 219f7cbfa71SZhenzhong Duan * @flags: Combination of enum iommufd_ioas_map_flags 220f7cbfa71SZhenzhong Duan * @dst_ioas_id: IOAS ID to change the mapping of 221f7cbfa71SZhenzhong Duan * @src_ioas_id: IOAS ID to copy from 222f7cbfa71SZhenzhong Duan * @length: Number of bytes to copy and map 223f7cbfa71SZhenzhong Duan * @dst_iova: IOVA the mapping was placed at. If IOMMU_IOAS_MAP_FIXED_IOVA is 224f7cbfa71SZhenzhong Duan * set then this must be provided as input. 225f7cbfa71SZhenzhong Duan * @src_iova: IOVA to start the copy 226f7cbfa71SZhenzhong Duan * 227f7cbfa71SZhenzhong Duan * Copy an already existing mapping from src_ioas_id and establish it in 228f7cbfa71SZhenzhong Duan * dst_ioas_id. The src iova/length must exactly match a range used with 229f7cbfa71SZhenzhong Duan * IOMMU_IOAS_MAP. 230f7cbfa71SZhenzhong Duan * 231f7cbfa71SZhenzhong Duan * This may be used to efficiently clone a subset of an IOAS to another, or as a 232f7cbfa71SZhenzhong Duan * kind of 'cache' to speed up mapping. Copy has an efficiency advantage over 233f7cbfa71SZhenzhong Duan * establishing equivalent new mappings, as internal resources are shared, and 234f7cbfa71SZhenzhong Duan * the kernel will pin the user memory only once. 235f7cbfa71SZhenzhong Duan */ 236f7cbfa71SZhenzhong Duan struct iommu_ioas_copy { 237f7cbfa71SZhenzhong Duan __u32 size; 238f7cbfa71SZhenzhong Duan __u32 flags; 239f7cbfa71SZhenzhong Duan __u32 dst_ioas_id; 240f7cbfa71SZhenzhong Duan __u32 src_ioas_id; 241f7cbfa71SZhenzhong Duan __aligned_u64 length; 242f7cbfa71SZhenzhong Duan __aligned_u64 dst_iova; 243f7cbfa71SZhenzhong Duan __aligned_u64 src_iova; 244f7cbfa71SZhenzhong Duan }; 245f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_COPY _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_COPY) 246f7cbfa71SZhenzhong Duan 247f7cbfa71SZhenzhong Duan /** 248f7cbfa71SZhenzhong Duan * struct iommu_ioas_unmap - ioctl(IOMMU_IOAS_UNMAP) 249f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_ioas_unmap) 250f7cbfa71SZhenzhong Duan * @ioas_id: IOAS ID to change the mapping of 251f7cbfa71SZhenzhong Duan * @iova: IOVA to start the unmapping at 252f7cbfa71SZhenzhong Duan * @length: Number of bytes to unmap, and return back the bytes unmapped 253f7cbfa71SZhenzhong Duan * 254f7cbfa71SZhenzhong Duan * Unmap an IOVA range. The iova/length must be a superset of a previously 255f7cbfa71SZhenzhong Duan * mapped range used with IOMMU_IOAS_MAP or IOMMU_IOAS_COPY. Splitting or 256f7cbfa71SZhenzhong Duan * truncating ranges is not allowed. The values 0 to U64_MAX will unmap 257f7cbfa71SZhenzhong Duan * everything. 258f7cbfa71SZhenzhong Duan */ 259f7cbfa71SZhenzhong Duan struct iommu_ioas_unmap { 260f7cbfa71SZhenzhong Duan __u32 size; 261f7cbfa71SZhenzhong Duan __u32 ioas_id; 262f7cbfa71SZhenzhong Duan __aligned_u64 iova; 263f7cbfa71SZhenzhong Duan __aligned_u64 length; 264f7cbfa71SZhenzhong Duan }; 265f7cbfa71SZhenzhong Duan #define IOMMU_IOAS_UNMAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_UNMAP) 266f7cbfa71SZhenzhong Duan 267f7cbfa71SZhenzhong Duan /** 268f7cbfa71SZhenzhong Duan * enum iommufd_option - ioctl(IOMMU_OPTION_RLIMIT_MODE) and 269f7cbfa71SZhenzhong Duan * ioctl(IOMMU_OPTION_HUGE_PAGES) 270f7cbfa71SZhenzhong Duan * @IOMMU_OPTION_RLIMIT_MODE: 271f7cbfa71SZhenzhong Duan * Change how RLIMIT_MEMLOCK accounting works. The caller must have privilege 272f7cbfa71SZhenzhong Duan * to invoke this. Value 0 (default) is user based accouting, 1 uses process 273f7cbfa71SZhenzhong Duan * based accounting. Global option, object_id must be 0 274f7cbfa71SZhenzhong Duan * @IOMMU_OPTION_HUGE_PAGES: 275f7cbfa71SZhenzhong Duan * Value 1 (default) allows contiguous pages to be combined when generating 276f7cbfa71SZhenzhong Duan * iommu mappings. Value 0 disables combining, everything is mapped to 277f7cbfa71SZhenzhong Duan * PAGE_SIZE. This can be useful for benchmarking. This is a per-IOAS 278f7cbfa71SZhenzhong Duan * option, the object_id must be the IOAS ID. 279f7cbfa71SZhenzhong Duan */ 280f7cbfa71SZhenzhong Duan enum iommufd_option { 281f7cbfa71SZhenzhong Duan IOMMU_OPTION_RLIMIT_MODE = 0, 282f7cbfa71SZhenzhong Duan IOMMU_OPTION_HUGE_PAGES = 1, 283f7cbfa71SZhenzhong Duan }; 284f7cbfa71SZhenzhong Duan 285f7cbfa71SZhenzhong Duan /** 286f7cbfa71SZhenzhong Duan * enum iommufd_option_ops - ioctl(IOMMU_OPTION_OP_SET) and 287f7cbfa71SZhenzhong Duan * ioctl(IOMMU_OPTION_OP_GET) 288f7cbfa71SZhenzhong Duan * @IOMMU_OPTION_OP_SET: Set the option's value 289f7cbfa71SZhenzhong Duan * @IOMMU_OPTION_OP_GET: Get the option's value 290f7cbfa71SZhenzhong Duan */ 291f7cbfa71SZhenzhong Duan enum iommufd_option_ops { 292f7cbfa71SZhenzhong Duan IOMMU_OPTION_OP_SET = 0, 293f7cbfa71SZhenzhong Duan IOMMU_OPTION_OP_GET = 1, 294f7cbfa71SZhenzhong Duan }; 295f7cbfa71SZhenzhong Duan 296f7cbfa71SZhenzhong Duan /** 297f7cbfa71SZhenzhong Duan * struct iommu_option - iommu option multiplexer 298f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_option) 299f7cbfa71SZhenzhong Duan * @option_id: One of enum iommufd_option 300f7cbfa71SZhenzhong Duan * @op: One of enum iommufd_option_ops 301f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 302f7cbfa71SZhenzhong Duan * @object_id: ID of the object if required 303f7cbfa71SZhenzhong Duan * @val64: Option value to set or value returned on get 304f7cbfa71SZhenzhong Duan * 305f7cbfa71SZhenzhong Duan * Change a simple option value. This multiplexor allows controlling options 306f7cbfa71SZhenzhong Duan * on objects. IOMMU_OPTION_OP_SET will load an option and IOMMU_OPTION_OP_GET 307f7cbfa71SZhenzhong Duan * will return the current value. 308f7cbfa71SZhenzhong Duan */ 309f7cbfa71SZhenzhong Duan struct iommu_option { 310f7cbfa71SZhenzhong Duan __u32 size; 311f7cbfa71SZhenzhong Duan __u32 option_id; 312f7cbfa71SZhenzhong Duan __u16 op; 313f7cbfa71SZhenzhong Duan __u16 __reserved; 314f7cbfa71SZhenzhong Duan __u32 object_id; 315f7cbfa71SZhenzhong Duan __aligned_u64 val64; 316f7cbfa71SZhenzhong Duan }; 317f7cbfa71SZhenzhong Duan #define IOMMU_OPTION _IO(IOMMUFD_TYPE, IOMMUFD_CMD_OPTION) 318f7cbfa71SZhenzhong Duan 319f7cbfa71SZhenzhong Duan /** 320f7cbfa71SZhenzhong Duan * enum iommufd_vfio_ioas_op - IOMMU_VFIO_IOAS_* ioctls 321f7cbfa71SZhenzhong Duan * @IOMMU_VFIO_IOAS_GET: Get the current compatibility IOAS 322f7cbfa71SZhenzhong Duan * @IOMMU_VFIO_IOAS_SET: Change the current compatibility IOAS 323f7cbfa71SZhenzhong Duan * @IOMMU_VFIO_IOAS_CLEAR: Disable VFIO compatibility 324f7cbfa71SZhenzhong Duan */ 325f7cbfa71SZhenzhong Duan enum iommufd_vfio_ioas_op { 326f7cbfa71SZhenzhong Duan IOMMU_VFIO_IOAS_GET = 0, 327f7cbfa71SZhenzhong Duan IOMMU_VFIO_IOAS_SET = 1, 328f7cbfa71SZhenzhong Duan IOMMU_VFIO_IOAS_CLEAR = 2, 329f7cbfa71SZhenzhong Duan }; 330f7cbfa71SZhenzhong Duan 331f7cbfa71SZhenzhong Duan /** 332f7cbfa71SZhenzhong Duan * struct iommu_vfio_ioas - ioctl(IOMMU_VFIO_IOAS) 333f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_vfio_ioas) 334f7cbfa71SZhenzhong Duan * @ioas_id: For IOMMU_VFIO_IOAS_SET the input IOAS ID to set 335f7cbfa71SZhenzhong Duan * For IOMMU_VFIO_IOAS_GET will output the IOAS ID 336f7cbfa71SZhenzhong Duan * @op: One of enum iommufd_vfio_ioas_op 337f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 338f7cbfa71SZhenzhong Duan * 339f7cbfa71SZhenzhong Duan * The VFIO compatibility support uses a single ioas because VFIO APIs do not 340f7cbfa71SZhenzhong Duan * support the ID field. Set or Get the IOAS that VFIO compatibility will use. 341f7cbfa71SZhenzhong Duan * When VFIO_GROUP_SET_CONTAINER is used on an iommufd it will get the 342f7cbfa71SZhenzhong Duan * compatibility ioas, either by taking what is already set, or auto creating 343f7cbfa71SZhenzhong Duan * one. From then on VFIO will continue to use that ioas and is not effected by 344f7cbfa71SZhenzhong Duan * this ioctl. SET or CLEAR does not destroy any auto-created IOAS. 345f7cbfa71SZhenzhong Duan */ 346f7cbfa71SZhenzhong Duan struct iommu_vfio_ioas { 347f7cbfa71SZhenzhong Duan __u32 size; 348f7cbfa71SZhenzhong Duan __u32 ioas_id; 349f7cbfa71SZhenzhong Duan __u16 op; 350f7cbfa71SZhenzhong Duan __u16 __reserved; 351f7cbfa71SZhenzhong Duan }; 352f7cbfa71SZhenzhong Duan #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) 353f7cbfa71SZhenzhong Duan 354f7cbfa71SZhenzhong Duan /** 355efb91426SDaniel Henrique Barboza * enum iommufd_hwpt_alloc_flags - Flags for HWPT allocation 356efb91426SDaniel Henrique Barboza * @IOMMU_HWPT_ALLOC_NEST_PARENT: If set, allocate a HWPT that can serve as 357efb91426SDaniel Henrique Barboza * the parent HWPT in a nesting configuration. 358efb91426SDaniel Henrique Barboza * @IOMMU_HWPT_ALLOC_DIRTY_TRACKING: Dirty tracking support for device IOMMU is 359efb91426SDaniel Henrique Barboza * enforced on device attachment 360*0d2eeef7SBibo Mao * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation data is 361*0d2eeef7SBibo Mao * valid. 362efb91426SDaniel Henrique Barboza */ 363efb91426SDaniel Henrique Barboza enum iommufd_hwpt_alloc_flags { 364efb91426SDaniel Henrique Barboza IOMMU_HWPT_ALLOC_NEST_PARENT = 1 << 0, 365efb91426SDaniel Henrique Barboza IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1, 366*0d2eeef7SBibo Mao IOMMU_HWPT_FAULT_ID_VALID = 1 << 2, 367efb91426SDaniel Henrique Barboza }; 368efb91426SDaniel Henrique Barboza 369efb91426SDaniel Henrique Barboza /** 370efb91426SDaniel Henrique Barboza * enum iommu_hwpt_vtd_s1_flags - Intel VT-d stage-1 page table 371efb91426SDaniel Henrique Barboza * entry attributes 372efb91426SDaniel Henrique Barboza * @IOMMU_VTD_S1_SRE: Supervisor request 373efb91426SDaniel Henrique Barboza * @IOMMU_VTD_S1_EAFE: Extended access enable 374efb91426SDaniel Henrique Barboza * @IOMMU_VTD_S1_WPE: Write protect enable 375efb91426SDaniel Henrique Barboza */ 376efb91426SDaniel Henrique Barboza enum iommu_hwpt_vtd_s1_flags { 377efb91426SDaniel Henrique Barboza IOMMU_VTD_S1_SRE = 1 << 0, 378efb91426SDaniel Henrique Barboza IOMMU_VTD_S1_EAFE = 1 << 1, 379efb91426SDaniel Henrique Barboza IOMMU_VTD_S1_WPE = 1 << 2, 380efb91426SDaniel Henrique Barboza }; 381efb91426SDaniel Henrique Barboza 382efb91426SDaniel Henrique Barboza /** 383efb91426SDaniel Henrique Barboza * struct iommu_hwpt_vtd_s1 - Intel VT-d stage-1 page table 384efb91426SDaniel Henrique Barboza * info (IOMMU_HWPT_DATA_VTD_S1) 385efb91426SDaniel Henrique Barboza * @flags: Combination of enum iommu_hwpt_vtd_s1_flags 386efb91426SDaniel Henrique Barboza * @pgtbl_addr: The base address of the stage-1 page table. 387efb91426SDaniel Henrique Barboza * @addr_width: The address width of the stage-1 page table 388efb91426SDaniel Henrique Barboza * @__reserved: Must be 0 389efb91426SDaniel Henrique Barboza */ 390efb91426SDaniel Henrique Barboza struct iommu_hwpt_vtd_s1 { 391efb91426SDaniel Henrique Barboza __aligned_u64 flags; 392efb91426SDaniel Henrique Barboza __aligned_u64 pgtbl_addr; 393efb91426SDaniel Henrique Barboza __u32 addr_width; 394efb91426SDaniel Henrique Barboza __u32 __reserved; 395efb91426SDaniel Henrique Barboza }; 396efb91426SDaniel Henrique Barboza 397efb91426SDaniel Henrique Barboza /** 398efb91426SDaniel Henrique Barboza * enum iommu_hwpt_data_type - IOMMU HWPT Data Type 399efb91426SDaniel Henrique Barboza * @IOMMU_HWPT_DATA_NONE: no data 400efb91426SDaniel Henrique Barboza * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table 401efb91426SDaniel Henrique Barboza */ 402efb91426SDaniel Henrique Barboza enum iommu_hwpt_data_type { 403*0d2eeef7SBibo Mao IOMMU_HWPT_DATA_NONE = 0, 404*0d2eeef7SBibo Mao IOMMU_HWPT_DATA_VTD_S1 = 1, 405efb91426SDaniel Henrique Barboza }; 406efb91426SDaniel Henrique Barboza 407efb91426SDaniel Henrique Barboza /** 408f7cbfa71SZhenzhong Duan * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) 409f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_hwpt_alloc) 410efb91426SDaniel Henrique Barboza * @flags: Combination of enum iommufd_hwpt_alloc_flags 411f7cbfa71SZhenzhong Duan * @dev_id: The device to allocate this HWPT for 412efb91426SDaniel Henrique Barboza * @pt_id: The IOAS or HWPT to connect this HWPT to 413f7cbfa71SZhenzhong Duan * @out_hwpt_id: The ID of the new HWPT 414f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 415efb91426SDaniel Henrique Barboza * @data_type: One of enum iommu_hwpt_data_type 416efb91426SDaniel Henrique Barboza * @data_len: Length of the type specific data 417efb91426SDaniel Henrique Barboza * @data_uptr: User pointer to the type specific data 418*0d2eeef7SBibo Mao * @fault_id: The ID of IOMMUFD_FAULT object. Valid only if flags field of 419*0d2eeef7SBibo Mao * IOMMU_HWPT_FAULT_ID_VALID is set. 420*0d2eeef7SBibo Mao * @__reserved2: Padding to 64-bit alignment. Must be 0. 421f7cbfa71SZhenzhong Duan * 422f7cbfa71SZhenzhong Duan * Explicitly allocate a hardware page table object. This is the same object 423f7cbfa71SZhenzhong Duan * type that is returned by iommufd_device_attach() and represents the 424f7cbfa71SZhenzhong Duan * underlying iommu driver's iommu_domain kernel object. 425f7cbfa71SZhenzhong Duan * 426efb91426SDaniel Henrique Barboza * A kernel-managed HWPT will be created with the mappings from the given 427efb91426SDaniel Henrique Barboza * IOAS via the @pt_id. The @data_type for this allocation must be set to 428efb91426SDaniel Henrique Barboza * IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a 429efb91426SDaniel Henrique Barboza * nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags. 430efb91426SDaniel Henrique Barboza * 431efb91426SDaniel Henrique Barboza * A user-managed nested HWPT will be created from a given parent HWPT via 432efb91426SDaniel Henrique Barboza * @pt_id, in which the parent HWPT must be allocated previously via the 433efb91426SDaniel Henrique Barboza * same ioctl from a given IOAS (@pt_id). In this case, the @data_type 434efb91426SDaniel Henrique Barboza * must be set to a pre-defined type corresponding to an I/O page table 435efb91426SDaniel Henrique Barboza * type supported by the underlying IOMMU hardware. 436efb91426SDaniel Henrique Barboza * 437efb91426SDaniel Henrique Barboza * If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and 438efb91426SDaniel Henrique Barboza * @data_uptr should be zero. Otherwise, both @data_len and @data_uptr 439efb91426SDaniel Henrique Barboza * must be given. 440f7cbfa71SZhenzhong Duan */ 441f7cbfa71SZhenzhong Duan struct iommu_hwpt_alloc { 442f7cbfa71SZhenzhong Duan __u32 size; 443f7cbfa71SZhenzhong Duan __u32 flags; 444f7cbfa71SZhenzhong Duan __u32 dev_id; 445f7cbfa71SZhenzhong Duan __u32 pt_id; 446f7cbfa71SZhenzhong Duan __u32 out_hwpt_id; 447f7cbfa71SZhenzhong Duan __u32 __reserved; 448efb91426SDaniel Henrique Barboza __u32 data_type; 449efb91426SDaniel Henrique Barboza __u32 data_len; 450efb91426SDaniel Henrique Barboza __aligned_u64 data_uptr; 451*0d2eeef7SBibo Mao __u32 fault_id; 452*0d2eeef7SBibo Mao __u32 __reserved2; 453f7cbfa71SZhenzhong Duan }; 454f7cbfa71SZhenzhong Duan #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) 455f7cbfa71SZhenzhong Duan 456f7cbfa71SZhenzhong Duan /** 457efb91426SDaniel Henrique Barboza * enum iommu_hw_info_vtd_flags - Flags for VT-d hw_info 458efb91426SDaniel Henrique Barboza * @IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17: If set, disallow read-only mappings 459efb91426SDaniel Henrique Barboza * on a nested_parent domain. 460efb91426SDaniel Henrique Barboza * https://www.intel.com/content/www/us/en/content-details/772415/content-details.html 461efb91426SDaniel Henrique Barboza */ 462efb91426SDaniel Henrique Barboza enum iommu_hw_info_vtd_flags { 463efb91426SDaniel Henrique Barboza IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 = 1 << 0, 464efb91426SDaniel Henrique Barboza }; 465efb91426SDaniel Henrique Barboza 466efb91426SDaniel Henrique Barboza /** 467f7cbfa71SZhenzhong Duan * struct iommu_hw_info_vtd - Intel VT-d hardware information 468f7cbfa71SZhenzhong Duan * 469efb91426SDaniel Henrique Barboza * @flags: Combination of enum iommu_hw_info_vtd_flags 470f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 471f7cbfa71SZhenzhong Duan * 472f7cbfa71SZhenzhong Duan * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec 473f7cbfa71SZhenzhong Duan * section 11.4.2 Capability Register. 474f7cbfa71SZhenzhong Duan * @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec 475f7cbfa71SZhenzhong Duan * section 11.4.3 Extended Capability Register. 476f7cbfa71SZhenzhong Duan * 477f7cbfa71SZhenzhong Duan * User needs to understand the Intel VT-d specification to decode the 478f7cbfa71SZhenzhong Duan * register value. 479f7cbfa71SZhenzhong Duan */ 480f7cbfa71SZhenzhong Duan struct iommu_hw_info_vtd { 481f7cbfa71SZhenzhong Duan __u32 flags; 482f7cbfa71SZhenzhong Duan __u32 __reserved; 483f7cbfa71SZhenzhong Duan __aligned_u64 cap_reg; 484f7cbfa71SZhenzhong Duan __aligned_u64 ecap_reg; 485f7cbfa71SZhenzhong Duan }; 486f7cbfa71SZhenzhong Duan 487f7cbfa71SZhenzhong Duan /** 488f7cbfa71SZhenzhong Duan * enum iommu_hw_info_type - IOMMU Hardware Info Types 489f7cbfa71SZhenzhong Duan * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not report hardware 490f7cbfa71SZhenzhong Duan * info 491f7cbfa71SZhenzhong Duan * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type 492f7cbfa71SZhenzhong Duan */ 493f7cbfa71SZhenzhong Duan enum iommu_hw_info_type { 494*0d2eeef7SBibo Mao IOMMU_HW_INFO_TYPE_NONE = 0, 495*0d2eeef7SBibo Mao IOMMU_HW_INFO_TYPE_INTEL_VTD = 1, 496f7cbfa71SZhenzhong Duan }; 497f7cbfa71SZhenzhong Duan 498f7cbfa71SZhenzhong Duan /** 499efb91426SDaniel Henrique Barboza * enum iommufd_hw_capabilities 500efb91426SDaniel Henrique Barboza * @IOMMU_HW_CAP_DIRTY_TRACKING: IOMMU hardware support for dirty tracking 501efb91426SDaniel Henrique Barboza * If available, it means the following APIs 502efb91426SDaniel Henrique Barboza * are supported: 503efb91426SDaniel Henrique Barboza * 504efb91426SDaniel Henrique Barboza * IOMMU_HWPT_GET_DIRTY_BITMAP 505efb91426SDaniel Henrique Barboza * IOMMU_HWPT_SET_DIRTY_TRACKING 506efb91426SDaniel Henrique Barboza * 507efb91426SDaniel Henrique Barboza */ 508efb91426SDaniel Henrique Barboza enum iommufd_hw_capabilities { 509efb91426SDaniel Henrique Barboza IOMMU_HW_CAP_DIRTY_TRACKING = 1 << 0, 510efb91426SDaniel Henrique Barboza }; 511efb91426SDaniel Henrique Barboza 512efb91426SDaniel Henrique Barboza /** 513f7cbfa71SZhenzhong Duan * struct iommu_hw_info - ioctl(IOMMU_GET_HW_INFO) 514f7cbfa71SZhenzhong Duan * @size: sizeof(struct iommu_hw_info) 515f7cbfa71SZhenzhong Duan * @flags: Must be 0 516f7cbfa71SZhenzhong Duan * @dev_id: The device bound to the iommufd 517f7cbfa71SZhenzhong Duan * @data_len: Input the length of a user buffer in bytes. Output the length of 518f7cbfa71SZhenzhong Duan * data that kernel supports 519f7cbfa71SZhenzhong Duan * @data_uptr: User pointer to a user-space buffer used by the kernel to fill 520f7cbfa71SZhenzhong Duan * the iommu type specific hardware information data 521f7cbfa71SZhenzhong Duan * @out_data_type: Output the iommu hardware info type as defined in the enum 522f7cbfa71SZhenzhong Duan * iommu_hw_info_type. 523efb91426SDaniel Henrique Barboza * @out_capabilities: Output the generic iommu capability info type as defined 524efb91426SDaniel Henrique Barboza * in the enum iommu_hw_capabilities. 525f7cbfa71SZhenzhong Duan * @__reserved: Must be 0 526f7cbfa71SZhenzhong Duan * 527f7cbfa71SZhenzhong Duan * Query an iommu type specific hardware information data from an iommu behind 528f7cbfa71SZhenzhong Duan * a given device that has been bound to iommufd. This hardware info data will 529f7cbfa71SZhenzhong Duan * be used to sync capabilities between the virtual iommu and the physical 530f7cbfa71SZhenzhong Duan * iommu, e.g. a nested translation setup needs to check the hardware info, so 531f7cbfa71SZhenzhong Duan * a guest stage-1 page table can be compatible with the physical iommu. 532f7cbfa71SZhenzhong Duan * 533f7cbfa71SZhenzhong Duan * To capture an iommu type specific hardware information data, @data_uptr and 534f7cbfa71SZhenzhong Duan * its length @data_len must be provided. Trailing bytes will be zeroed if the 535f7cbfa71SZhenzhong Duan * user buffer is larger than the data that kernel has. Otherwise, kernel only 536f7cbfa71SZhenzhong Duan * fills the buffer using the given length in @data_len. If the ioctl succeeds, 537f7cbfa71SZhenzhong Duan * @data_len will be updated to the length that kernel actually supports, 538f7cbfa71SZhenzhong Duan * @out_data_type will be filled to decode the data filled in the buffer 539f7cbfa71SZhenzhong Duan * pointed by @data_uptr. Input @data_len == zero is allowed. 540f7cbfa71SZhenzhong Duan */ 541f7cbfa71SZhenzhong Duan struct iommu_hw_info { 542f7cbfa71SZhenzhong Duan __u32 size; 543f7cbfa71SZhenzhong Duan __u32 flags; 544f7cbfa71SZhenzhong Duan __u32 dev_id; 545f7cbfa71SZhenzhong Duan __u32 data_len; 546f7cbfa71SZhenzhong Duan __aligned_u64 data_uptr; 547f7cbfa71SZhenzhong Duan __u32 out_data_type; 548f7cbfa71SZhenzhong Duan __u32 __reserved; 549efb91426SDaniel Henrique Barboza __aligned_u64 out_capabilities; 550f7cbfa71SZhenzhong Duan }; 551f7cbfa71SZhenzhong Duan #define IOMMU_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_GET_HW_INFO) 552efb91426SDaniel Henrique Barboza 553efb91426SDaniel Henrique Barboza /* 554efb91426SDaniel Henrique Barboza * enum iommufd_hwpt_set_dirty_tracking_flags - Flags for steering dirty 555efb91426SDaniel Henrique Barboza * tracking 556efb91426SDaniel Henrique Barboza * @IOMMU_HWPT_DIRTY_TRACKING_ENABLE: Enable dirty tracking 557efb91426SDaniel Henrique Barboza */ 558efb91426SDaniel Henrique Barboza enum iommufd_hwpt_set_dirty_tracking_flags { 559efb91426SDaniel Henrique Barboza IOMMU_HWPT_DIRTY_TRACKING_ENABLE = 1, 560efb91426SDaniel Henrique Barboza }; 561efb91426SDaniel Henrique Barboza 562efb91426SDaniel Henrique Barboza /** 563efb91426SDaniel Henrique Barboza * struct iommu_hwpt_set_dirty_tracking - ioctl(IOMMU_HWPT_SET_DIRTY_TRACKING) 564efb91426SDaniel Henrique Barboza * @size: sizeof(struct iommu_hwpt_set_dirty_tracking) 565efb91426SDaniel Henrique Barboza * @flags: Combination of enum iommufd_hwpt_set_dirty_tracking_flags 566efb91426SDaniel Henrique Barboza * @hwpt_id: HW pagetable ID that represents the IOMMU domain 567efb91426SDaniel Henrique Barboza * @__reserved: Must be 0 568efb91426SDaniel Henrique Barboza * 569efb91426SDaniel Henrique Barboza * Toggle dirty tracking on an HW pagetable. 570efb91426SDaniel Henrique Barboza */ 571efb91426SDaniel Henrique Barboza struct iommu_hwpt_set_dirty_tracking { 572efb91426SDaniel Henrique Barboza __u32 size; 573efb91426SDaniel Henrique Barboza __u32 flags; 574efb91426SDaniel Henrique Barboza __u32 hwpt_id; 575efb91426SDaniel Henrique Barboza __u32 __reserved; 576efb91426SDaniel Henrique Barboza }; 577efb91426SDaniel Henrique Barboza #define IOMMU_HWPT_SET_DIRTY_TRACKING _IO(IOMMUFD_TYPE, \ 578efb91426SDaniel Henrique Barboza IOMMUFD_CMD_HWPT_SET_DIRTY_TRACKING) 579efb91426SDaniel Henrique Barboza 580efb91426SDaniel Henrique Barboza /** 581efb91426SDaniel Henrique Barboza * enum iommufd_hwpt_get_dirty_bitmap_flags - Flags for getting dirty bits 582efb91426SDaniel Henrique Barboza * @IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR: Just read the PTEs without clearing 583efb91426SDaniel Henrique Barboza * any dirty bits metadata. This flag 584efb91426SDaniel Henrique Barboza * can be passed in the expectation 585efb91426SDaniel Henrique Barboza * where the next operation is an unmap 586efb91426SDaniel Henrique Barboza * of the same IOVA range. 587efb91426SDaniel Henrique Barboza * 588efb91426SDaniel Henrique Barboza */ 589efb91426SDaniel Henrique Barboza enum iommufd_hwpt_get_dirty_bitmap_flags { 590efb91426SDaniel Henrique Barboza IOMMU_HWPT_GET_DIRTY_BITMAP_NO_CLEAR = 1, 591efb91426SDaniel Henrique Barboza }; 592efb91426SDaniel Henrique Barboza 593efb91426SDaniel Henrique Barboza /** 594efb91426SDaniel Henrique Barboza * struct iommu_hwpt_get_dirty_bitmap - ioctl(IOMMU_HWPT_GET_DIRTY_BITMAP) 595efb91426SDaniel Henrique Barboza * @size: sizeof(struct iommu_hwpt_get_dirty_bitmap) 596efb91426SDaniel Henrique Barboza * @hwpt_id: HW pagetable ID that represents the IOMMU domain 597efb91426SDaniel Henrique Barboza * @flags: Combination of enum iommufd_hwpt_get_dirty_bitmap_flags 598efb91426SDaniel Henrique Barboza * @__reserved: Must be 0 599efb91426SDaniel Henrique Barboza * @iova: base IOVA of the bitmap first bit 600efb91426SDaniel Henrique Barboza * @length: IOVA range size 601efb91426SDaniel Henrique Barboza * @page_size: page size granularity of each bit in the bitmap 602efb91426SDaniel Henrique Barboza * @data: bitmap where to set the dirty bits. The bitmap bits each 603efb91426SDaniel Henrique Barboza * represent a page_size which you deviate from an arbitrary iova. 604efb91426SDaniel Henrique Barboza * 605efb91426SDaniel Henrique Barboza * Checking a given IOVA is dirty: 606efb91426SDaniel Henrique Barboza * 607efb91426SDaniel Henrique Barboza * data[(iova / page_size) / 64] & (1ULL << ((iova / page_size) % 64)) 608efb91426SDaniel Henrique Barboza * 609efb91426SDaniel Henrique Barboza * Walk the IOMMU pagetables for a given IOVA range to return a bitmap 610efb91426SDaniel Henrique Barboza * with the dirty IOVAs. In doing so it will also by default clear any 611efb91426SDaniel Henrique Barboza * dirty bit metadata set in the IOPTE. 612efb91426SDaniel Henrique Barboza */ 613efb91426SDaniel Henrique Barboza struct iommu_hwpt_get_dirty_bitmap { 614efb91426SDaniel Henrique Barboza __u32 size; 615efb91426SDaniel Henrique Barboza __u32 hwpt_id; 616efb91426SDaniel Henrique Barboza __u32 flags; 617efb91426SDaniel Henrique Barboza __u32 __reserved; 618efb91426SDaniel Henrique Barboza __aligned_u64 iova; 619efb91426SDaniel Henrique Barboza __aligned_u64 length; 620efb91426SDaniel Henrique Barboza __aligned_u64 page_size; 621efb91426SDaniel Henrique Barboza __aligned_u64 data; 622efb91426SDaniel Henrique Barboza }; 623efb91426SDaniel Henrique Barboza #define IOMMU_HWPT_GET_DIRTY_BITMAP _IO(IOMMUFD_TYPE, \ 624efb91426SDaniel Henrique Barboza IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP) 625efb91426SDaniel Henrique Barboza 6266a02465fSDaniel Henrique Barboza /** 6276a02465fSDaniel Henrique Barboza * enum iommu_hwpt_invalidate_data_type - IOMMU HWPT Cache Invalidation 6286a02465fSDaniel Henrique Barboza * Data Type 6296a02465fSDaniel Henrique Barboza * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1 6306a02465fSDaniel Henrique Barboza */ 6316a02465fSDaniel Henrique Barboza enum iommu_hwpt_invalidate_data_type { 632*0d2eeef7SBibo Mao IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 = 0, 6336a02465fSDaniel Henrique Barboza }; 6346a02465fSDaniel Henrique Barboza 6356a02465fSDaniel Henrique Barboza /** 6366a02465fSDaniel Henrique Barboza * enum iommu_hwpt_vtd_s1_invalidate_flags - Flags for Intel VT-d 6376a02465fSDaniel Henrique Barboza * stage-1 cache invalidation 6386a02465fSDaniel Henrique Barboza * @IOMMU_VTD_INV_FLAGS_LEAF: Indicates whether the invalidation applies 6396a02465fSDaniel Henrique Barboza * to all-levels page structure cache or just 6406a02465fSDaniel Henrique Barboza * the leaf PTE cache. 6416a02465fSDaniel Henrique Barboza */ 6426a02465fSDaniel Henrique Barboza enum iommu_hwpt_vtd_s1_invalidate_flags { 6436a02465fSDaniel Henrique Barboza IOMMU_VTD_INV_FLAGS_LEAF = 1 << 0, 6446a02465fSDaniel Henrique Barboza }; 6456a02465fSDaniel Henrique Barboza 6466a02465fSDaniel Henrique Barboza /** 6476a02465fSDaniel Henrique Barboza * struct iommu_hwpt_vtd_s1_invalidate - Intel VT-d cache invalidation 6486a02465fSDaniel Henrique Barboza * (IOMMU_HWPT_INVALIDATE_DATA_VTD_S1) 6496a02465fSDaniel Henrique Barboza * @addr: The start address of the range to be invalidated. It needs to 6506a02465fSDaniel Henrique Barboza * be 4KB aligned. 6516a02465fSDaniel Henrique Barboza * @npages: Number of contiguous 4K pages to be invalidated. 6526a02465fSDaniel Henrique Barboza * @flags: Combination of enum iommu_hwpt_vtd_s1_invalidate_flags 6536a02465fSDaniel Henrique Barboza * @__reserved: Must be 0 6546a02465fSDaniel Henrique Barboza * 6556a02465fSDaniel Henrique Barboza * The Intel VT-d specific invalidation data for user-managed stage-1 cache 6566a02465fSDaniel Henrique Barboza * invalidation in nested translation. Userspace uses this structure to 6576a02465fSDaniel Henrique Barboza * tell the impacted cache scope after modifying the stage-1 page table. 6586a02465fSDaniel Henrique Barboza * 6596a02465fSDaniel Henrique Barboza * Invalidating all the caches related to the page table by setting @addr 6606a02465fSDaniel Henrique Barboza * to be 0 and @npages to be U64_MAX. 6616a02465fSDaniel Henrique Barboza * 6626a02465fSDaniel Henrique Barboza * The device TLB will be invalidated automatically if ATS is enabled. 6636a02465fSDaniel Henrique Barboza */ 6646a02465fSDaniel Henrique Barboza struct iommu_hwpt_vtd_s1_invalidate { 6656a02465fSDaniel Henrique Barboza __aligned_u64 addr; 6666a02465fSDaniel Henrique Barboza __aligned_u64 npages; 6676a02465fSDaniel Henrique Barboza __u32 flags; 6686a02465fSDaniel Henrique Barboza __u32 __reserved; 6696a02465fSDaniel Henrique Barboza }; 6706a02465fSDaniel Henrique Barboza 6716a02465fSDaniel Henrique Barboza /** 6726a02465fSDaniel Henrique Barboza * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) 6736a02465fSDaniel Henrique Barboza * @size: sizeof(struct iommu_hwpt_invalidate) 6746a02465fSDaniel Henrique Barboza * @hwpt_id: ID of a nested HWPT for cache invalidation 6756a02465fSDaniel Henrique Barboza * @data_uptr: User pointer to an array of driver-specific cache invalidation 6766a02465fSDaniel Henrique Barboza * data. 6776a02465fSDaniel Henrique Barboza * @data_type: One of enum iommu_hwpt_invalidate_data_type, defining the data 6786a02465fSDaniel Henrique Barboza * type of all the entries in the invalidation request array. It 6796a02465fSDaniel Henrique Barboza * should be a type supported by the hwpt pointed by @hwpt_id. 6806a02465fSDaniel Henrique Barboza * @entry_len: Length (in bytes) of a request entry in the request array 6816a02465fSDaniel Henrique Barboza * @entry_num: Input the number of cache invalidation requests in the array. 6826a02465fSDaniel Henrique Barboza * Output the number of requests successfully handled by kernel. 6836a02465fSDaniel Henrique Barboza * @__reserved: Must be 0. 6846a02465fSDaniel Henrique Barboza * 6856a02465fSDaniel Henrique Barboza * Invalidate the iommu cache for user-managed page table. Modifications on a 6866a02465fSDaniel Henrique Barboza * user-managed page table should be followed by this operation to sync cache. 6876a02465fSDaniel Henrique Barboza * Each ioctl can support one or more cache invalidation requests in the array 6886a02465fSDaniel Henrique Barboza * that has a total size of @entry_len * @entry_num. 6896a02465fSDaniel Henrique Barboza * 6906a02465fSDaniel Henrique Barboza * An empty invalidation request array by setting @entry_num==0 is allowed, and 6916a02465fSDaniel Henrique Barboza * @entry_len and @data_uptr would be ignored in this case. This can be used to 6926a02465fSDaniel Henrique Barboza * check if the given @data_type is supported or not by kernel. 6936a02465fSDaniel Henrique Barboza */ 6946a02465fSDaniel Henrique Barboza struct iommu_hwpt_invalidate { 6956a02465fSDaniel Henrique Barboza __u32 size; 6966a02465fSDaniel Henrique Barboza __u32 hwpt_id; 6976a02465fSDaniel Henrique Barboza __aligned_u64 data_uptr; 6986a02465fSDaniel Henrique Barboza __u32 data_type; 6996a02465fSDaniel Henrique Barboza __u32 entry_len; 7006a02465fSDaniel Henrique Barboza __u32 entry_num; 7016a02465fSDaniel Henrique Barboza __u32 __reserved; 7026a02465fSDaniel Henrique Barboza }; 7036a02465fSDaniel Henrique Barboza #define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE) 704*0d2eeef7SBibo Mao 705*0d2eeef7SBibo Mao /** 706*0d2eeef7SBibo Mao * enum iommu_hwpt_pgfault_flags - flags for struct iommu_hwpt_pgfault 707*0d2eeef7SBibo Mao * @IOMMU_PGFAULT_FLAGS_PASID_VALID: The pasid field of the fault data is 708*0d2eeef7SBibo Mao * valid. 709*0d2eeef7SBibo Mao * @IOMMU_PGFAULT_FLAGS_LAST_PAGE: It's the last fault of a fault group. 710*0d2eeef7SBibo Mao */ 711*0d2eeef7SBibo Mao enum iommu_hwpt_pgfault_flags { 712*0d2eeef7SBibo Mao IOMMU_PGFAULT_FLAGS_PASID_VALID = (1 << 0), 713*0d2eeef7SBibo Mao IOMMU_PGFAULT_FLAGS_LAST_PAGE = (1 << 1), 714*0d2eeef7SBibo Mao }; 715*0d2eeef7SBibo Mao 716*0d2eeef7SBibo Mao /** 717*0d2eeef7SBibo Mao * enum iommu_hwpt_pgfault_perm - perm bits for struct iommu_hwpt_pgfault 718*0d2eeef7SBibo Mao * @IOMMU_PGFAULT_PERM_READ: request for read permission 719*0d2eeef7SBibo Mao * @IOMMU_PGFAULT_PERM_WRITE: request for write permission 720*0d2eeef7SBibo Mao * @IOMMU_PGFAULT_PERM_EXEC: (PCIE 10.4.1) request with a PASID that has the 721*0d2eeef7SBibo Mao * Execute Requested bit set in PASID TLP Prefix. 722*0d2eeef7SBibo Mao * @IOMMU_PGFAULT_PERM_PRIV: (PCIE 10.4.1) request with a PASID that has the 723*0d2eeef7SBibo Mao * Privileged Mode Requested bit set in PASID TLP 724*0d2eeef7SBibo Mao * Prefix. 725*0d2eeef7SBibo Mao */ 726*0d2eeef7SBibo Mao enum iommu_hwpt_pgfault_perm { 727*0d2eeef7SBibo Mao IOMMU_PGFAULT_PERM_READ = (1 << 0), 728*0d2eeef7SBibo Mao IOMMU_PGFAULT_PERM_WRITE = (1 << 1), 729*0d2eeef7SBibo Mao IOMMU_PGFAULT_PERM_EXEC = (1 << 2), 730*0d2eeef7SBibo Mao IOMMU_PGFAULT_PERM_PRIV = (1 << 3), 731*0d2eeef7SBibo Mao }; 732*0d2eeef7SBibo Mao 733*0d2eeef7SBibo Mao /** 734*0d2eeef7SBibo Mao * struct iommu_hwpt_pgfault - iommu page fault data 735*0d2eeef7SBibo Mao * @flags: Combination of enum iommu_hwpt_pgfault_flags 736*0d2eeef7SBibo Mao * @dev_id: id of the originated device 737*0d2eeef7SBibo Mao * @pasid: Process Address Space ID 738*0d2eeef7SBibo Mao * @grpid: Page Request Group Index 739*0d2eeef7SBibo Mao * @perm: Combination of enum iommu_hwpt_pgfault_perm 740*0d2eeef7SBibo Mao * @addr: Fault address 741*0d2eeef7SBibo Mao * @length: a hint of how much data the requestor is expecting to fetch. For 742*0d2eeef7SBibo Mao * example, if the PRI initiator knows it is going to do a 10MB 743*0d2eeef7SBibo Mao * transfer, it could fill in 10MB and the OS could pre-fault in 744*0d2eeef7SBibo Mao * 10MB of IOVA. It's default to 0 if there's no such hint. 745*0d2eeef7SBibo Mao * @cookie: kernel-managed cookie identifying a group of fault messages. The 746*0d2eeef7SBibo Mao * cookie number encoded in the last page fault of the group should 747*0d2eeef7SBibo Mao * be echoed back in the response message. 748*0d2eeef7SBibo Mao */ 749*0d2eeef7SBibo Mao struct iommu_hwpt_pgfault { 750*0d2eeef7SBibo Mao __u32 flags; 751*0d2eeef7SBibo Mao __u32 dev_id; 752*0d2eeef7SBibo Mao __u32 pasid; 753*0d2eeef7SBibo Mao __u32 grpid; 754*0d2eeef7SBibo Mao __u32 perm; 755*0d2eeef7SBibo Mao __u64 addr; 756*0d2eeef7SBibo Mao __u32 length; 757*0d2eeef7SBibo Mao __u32 cookie; 758*0d2eeef7SBibo Mao }; 759*0d2eeef7SBibo Mao 760*0d2eeef7SBibo Mao /** 761*0d2eeef7SBibo Mao * enum iommufd_page_response_code - Return status of fault handlers 762*0d2eeef7SBibo Mao * @IOMMUFD_PAGE_RESP_SUCCESS: Fault has been handled and the page tables 763*0d2eeef7SBibo Mao * populated, retry the access. This is the 764*0d2eeef7SBibo Mao * "Success" defined in PCI 10.4.2.1. 765*0d2eeef7SBibo Mao * @IOMMUFD_PAGE_RESP_INVALID: Could not handle this fault, don't retry the 766*0d2eeef7SBibo Mao * access. This is the "Invalid Request" in PCI 767*0d2eeef7SBibo Mao * 10.4.2.1. 768*0d2eeef7SBibo Mao */ 769*0d2eeef7SBibo Mao enum iommufd_page_response_code { 770*0d2eeef7SBibo Mao IOMMUFD_PAGE_RESP_SUCCESS = 0, 771*0d2eeef7SBibo Mao IOMMUFD_PAGE_RESP_INVALID = 1, 772*0d2eeef7SBibo Mao }; 773*0d2eeef7SBibo Mao 774*0d2eeef7SBibo Mao /** 775*0d2eeef7SBibo Mao * struct iommu_hwpt_page_response - IOMMU page fault response 776*0d2eeef7SBibo Mao * @cookie: The kernel-managed cookie reported in the fault message. 777*0d2eeef7SBibo Mao * @code: One of response code in enum iommufd_page_response_code. 778*0d2eeef7SBibo Mao */ 779*0d2eeef7SBibo Mao struct iommu_hwpt_page_response { 780*0d2eeef7SBibo Mao __u32 cookie; 781*0d2eeef7SBibo Mao __u32 code; 782*0d2eeef7SBibo Mao }; 783*0d2eeef7SBibo Mao 784*0d2eeef7SBibo Mao /** 785*0d2eeef7SBibo Mao * struct iommu_fault_alloc - ioctl(IOMMU_FAULT_QUEUE_ALLOC) 786*0d2eeef7SBibo Mao * @size: sizeof(struct iommu_fault_alloc) 787*0d2eeef7SBibo Mao * @flags: Must be 0 788*0d2eeef7SBibo Mao * @out_fault_id: The ID of the new FAULT 789*0d2eeef7SBibo Mao * @out_fault_fd: The fd of the new FAULT 790*0d2eeef7SBibo Mao * 791*0d2eeef7SBibo Mao * Explicitly allocate a fault handling object. 792*0d2eeef7SBibo Mao */ 793*0d2eeef7SBibo Mao struct iommu_fault_alloc { 794*0d2eeef7SBibo Mao __u32 size; 795*0d2eeef7SBibo Mao __u32 flags; 796*0d2eeef7SBibo Mao __u32 out_fault_id; 797*0d2eeef7SBibo Mao __u32 out_fault_fd; 798*0d2eeef7SBibo Mao }; 799*0d2eeef7SBibo Mao #define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC) 800f7cbfa71SZhenzhong Duan #endif 801