logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

iommufd.h (29017B)


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