vfio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * VFIO API definition
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #ifndef _UAPIVFIO_H
  12. #define _UAPIVFIO_H
  13. #include <linux/types.h>
  14. #include <linux/ioctl.h>
  15. #define VFIO_API_VERSION 0
  16. /* Kernel & User level defines for VFIO IOCTLs. */
  17. /* Extensions */
  18. #define VFIO_TYPE1_IOMMU 1
  19. /*
  20. * The IOCTL interface is designed for extensibility by embedding the
  21. * structure length (argsz) and flags into structures passed between
  22. * kernel and userspace. We therefore use the _IO() macro for these
  23. * defines to avoid implicitly embedding a size into the ioctl request.
  24. * As structure fields are added, argsz will increase to match and flag
  25. * bits will be defined to indicate additional fields with valid data.
  26. * It's *always* the caller's responsibility to indicate the size of
  27. * the structure passed by setting argsz appropriately.
  28. */
  29. #define VFIO_TYPE (';')
  30. #define VFIO_BASE 100
  31. /* -------- IOCTLs for VFIO file descriptor (/dev/vfio/vfio) -------- */
  32. /**
  33. * VFIO_GET_API_VERSION - _IO(VFIO_TYPE, VFIO_BASE + 0)
  34. *
  35. * Report the version of the VFIO API. This allows us to bump the entire
  36. * API version should we later need to add or change features in incompatible
  37. * ways.
  38. * Return: VFIO_API_VERSION
  39. * Availability: Always
  40. */
  41. #define VFIO_GET_API_VERSION _IO(VFIO_TYPE, VFIO_BASE + 0)
  42. /**
  43. * VFIO_CHECK_EXTENSION - _IOW(VFIO_TYPE, VFIO_BASE + 1, __u32)
  44. *
  45. * Check whether an extension is supported.
  46. * Return: 0 if not supported, 1 (or some other positive integer) if supported.
  47. * Availability: Always
  48. */
  49. #define VFIO_CHECK_EXTENSION _IO(VFIO_TYPE, VFIO_BASE + 1)
  50. /**
  51. * VFIO_SET_IOMMU - _IOW(VFIO_TYPE, VFIO_BASE + 2, __s32)
  52. *
  53. * Set the iommu to the given type. The type must be supported by an
  54. * iommu driver as verified by calling CHECK_EXTENSION using the same
  55. * type. A group must be set to this file descriptor before this
  56. * ioctl is available. The IOMMU interfaces enabled by this call are
  57. * specific to the value set.
  58. * Return: 0 on success, -errno on failure
  59. * Availability: When VFIO group attached
  60. */
  61. #define VFIO_SET_IOMMU _IO(VFIO_TYPE, VFIO_BASE + 2)
  62. /* -------- IOCTLs for GROUP file descriptors (/dev/vfio/$GROUP) -------- */
  63. /**
  64. * VFIO_GROUP_GET_STATUS - _IOR(VFIO_TYPE, VFIO_BASE + 3,
  65. * struct vfio_group_status)
  66. *
  67. * Retrieve information about the group. Fills in provided
  68. * struct vfio_group_info. Caller sets argsz.
  69. * Return: 0 on succes, -errno on failure.
  70. * Availability: Always
  71. */
  72. struct vfio_group_status {
  73. __u32 argsz;
  74. __u32 flags;
  75. #define VFIO_GROUP_FLAGS_VIABLE (1 << 0)
  76. #define VFIO_GROUP_FLAGS_CONTAINER_SET (1 << 1)
  77. };
  78. #define VFIO_GROUP_GET_STATUS _IO(VFIO_TYPE, VFIO_BASE + 3)
  79. /**
  80. * VFIO_GROUP_SET_CONTAINER - _IOW(VFIO_TYPE, VFIO_BASE + 4, __s32)
  81. *
  82. * Set the container for the VFIO group to the open VFIO file
  83. * descriptor provided. Groups may only belong to a single
  84. * container. Containers may, at their discretion, support multiple
  85. * groups. Only when a container is set are all of the interfaces
  86. * of the VFIO file descriptor and the VFIO group file descriptor
  87. * available to the user.
  88. * Return: 0 on success, -errno on failure.
  89. * Availability: Always
  90. */
  91. #define VFIO_GROUP_SET_CONTAINER _IO(VFIO_TYPE, VFIO_BASE + 4)
  92. /**
  93. * VFIO_GROUP_UNSET_CONTAINER - _IO(VFIO_TYPE, VFIO_BASE + 5)
  94. *
  95. * Remove the group from the attached container. This is the
  96. * opposite of the SET_CONTAINER call and returns the group to
  97. * an initial state. All device file descriptors must be released
  98. * prior to calling this interface. When removing the last group
  99. * from a container, the IOMMU will be disabled and all state lost,
  100. * effectively also returning the VFIO file descriptor to an initial
  101. * state.
  102. * Return: 0 on success, -errno on failure.
  103. * Availability: When attached to container
  104. */
  105. #define VFIO_GROUP_UNSET_CONTAINER _IO(VFIO_TYPE, VFIO_BASE + 5)
  106. /**
  107. * VFIO_GROUP_GET_DEVICE_FD - _IOW(VFIO_TYPE, VFIO_BASE + 6, char)
  108. *
  109. * Return a new file descriptor for the device object described by
  110. * the provided string. The string should match a device listed in
  111. * the devices subdirectory of the IOMMU group sysfs entry. The
  112. * group containing the device must already be added to this context.
  113. * Return: new file descriptor on success, -errno on failure.
  114. * Availability: When attached to container
  115. */
  116. #define VFIO_GROUP_GET_DEVICE_FD _IO(VFIO_TYPE, VFIO_BASE + 6)
  117. /* --------------- IOCTLs for DEVICE file descriptors --------------- */
  118. /**
  119. * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7,
  120. * struct vfio_device_info)
  121. *
  122. * Retrieve information about the device. Fills in provided
  123. * struct vfio_device_info. Caller sets argsz.
  124. * Return: 0 on success, -errno on failure.
  125. */
  126. struct vfio_device_info {
  127. __u32 argsz;
  128. __u32 flags;
  129. #define VFIO_DEVICE_FLAGS_RESET (1 << 0) /* Device supports reset */
  130. #define VFIO_DEVICE_FLAGS_PCI (1 << 1) /* vfio-pci device */
  131. __u32 num_regions; /* Max region index + 1 */
  132. __u32 num_irqs; /* Max IRQ index + 1 */
  133. };
  134. #define VFIO_DEVICE_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 7)
  135. /**
  136. * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8,
  137. * struct vfio_region_info)
  138. *
  139. * Retrieve information about a device region. Caller provides
  140. * struct vfio_region_info with index value set. Caller sets argsz.
  141. * Implementation of region mapping is bus driver specific. This is
  142. * intended to describe MMIO, I/O port, as well as bus specific
  143. * regions (ex. PCI config space). Zero sized regions may be used
  144. * to describe unimplemented regions (ex. unimplemented PCI BARs).
  145. * Return: 0 on success, -errno on failure.
  146. */
  147. struct vfio_region_info {
  148. __u32 argsz;
  149. __u32 flags;
  150. #define VFIO_REGION_INFO_FLAG_READ (1 << 0) /* Region supports read */
  151. #define VFIO_REGION_INFO_FLAG_WRITE (1 << 1) /* Region supports write */
  152. #define VFIO_REGION_INFO_FLAG_MMAP (1 << 2) /* Region supports mmap */
  153. __u32 index; /* Region index */
  154. __u32 resv; /* Reserved for alignment */
  155. __u64 size; /* Region size (bytes) */
  156. __u64 offset; /* Region offset from start of device fd */
  157. };
  158. #define VFIO_DEVICE_GET_REGION_INFO _IO(VFIO_TYPE, VFIO_BASE + 8)
  159. /**
  160. * VFIO_DEVICE_GET_IRQ_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 9,
  161. * struct vfio_irq_info)
  162. *
  163. * Retrieve information about a device IRQ. Caller provides
  164. * struct vfio_irq_info with index value set. Caller sets argsz.
  165. * Implementation of IRQ mapping is bus driver specific. Indexes
  166. * using multiple IRQs are primarily intended to support MSI-like
  167. * interrupt blocks. Zero count irq blocks may be used to describe
  168. * unimplemented interrupt types.
  169. *
  170. * The EVENTFD flag indicates the interrupt index supports eventfd based
  171. * signaling.
  172. *
  173. * The MASKABLE flags indicates the index supports MASK and UNMASK
  174. * actions described below.
  175. *
  176. * AUTOMASKED indicates that after signaling, the interrupt line is
  177. * automatically masked by VFIO and the user needs to unmask the line
  178. * to receive new interrupts. This is primarily intended to distinguish
  179. * level triggered interrupts.
  180. *
  181. * The NORESIZE flag indicates that the interrupt lines within the index
  182. * are setup as a set and new subindexes cannot be enabled without first
  183. * disabling the entire index. This is used for interrupts like PCI MSI
  184. * and MSI-X where the driver may only use a subset of the available
  185. * indexes, but VFIO needs to enable a specific number of vectors
  186. * upfront. In the case of MSI-X, where the user can enable MSI-X and
  187. * then add and unmask vectors, it's up to userspace to make the decision
  188. * whether to allocate the maximum supported number of vectors or tear
  189. * down setup and incrementally increase the vectors as each is enabled.
  190. */
  191. struct vfio_irq_info {
  192. __u32 argsz;
  193. __u32 flags;
  194. #define VFIO_IRQ_INFO_EVENTFD (1 << 0)
  195. #define VFIO_IRQ_INFO_MASKABLE (1 << 1)
  196. #define VFIO_IRQ_INFO_AUTOMASKED (1 << 2)
  197. #define VFIO_IRQ_INFO_NORESIZE (1 << 3)
  198. __u32 index; /* IRQ index */
  199. __u32 count; /* Number of IRQs within this index */
  200. };
  201. #define VFIO_DEVICE_GET_IRQ_INFO _IO(VFIO_TYPE, VFIO_BASE + 9)
  202. /**
  203. * VFIO_DEVICE_SET_IRQS - _IOW(VFIO_TYPE, VFIO_BASE + 10, struct vfio_irq_set)
  204. *
  205. * Set signaling, masking, and unmasking of interrupts. Caller provides
  206. * struct vfio_irq_set with all fields set. 'start' and 'count' indicate
  207. * the range of subindexes being specified.
  208. *
  209. * The DATA flags specify the type of data provided. If DATA_NONE, the
  210. * operation performs the specified action immediately on the specified
  211. * interrupt(s). For example, to unmask AUTOMASKED interrupt [0,0]:
  212. * flags = (DATA_NONE|ACTION_UNMASK), index = 0, start = 0, count = 1.
  213. *
  214. * DATA_BOOL allows sparse support for the same on arrays of interrupts.
  215. * For example, to mask interrupts [0,1] and [0,3] (but not [0,2]):
  216. * flags = (DATA_BOOL|ACTION_MASK), index = 0, start = 1, count = 3,
  217. * data = {1,0,1}
  218. *
  219. * DATA_EVENTFD binds the specified ACTION to the provided __s32 eventfd.
  220. * A value of -1 can be used to either de-assign interrupts if already
  221. * assigned or skip un-assigned interrupts. For example, to set an eventfd
  222. * to be trigger for interrupts [0,0] and [0,2]:
  223. * flags = (DATA_EVENTFD|ACTION_TRIGGER), index = 0, start = 0, count = 3,
  224. * data = {fd1, -1, fd2}
  225. * If index [0,1] is previously set, two count = 1 ioctls calls would be
  226. * required to set [0,0] and [0,2] without changing [0,1].
  227. *
  228. * Once a signaling mechanism is set, DATA_BOOL or DATA_NONE can be used
  229. * with ACTION_TRIGGER to perform kernel level interrupt loopback testing
  230. * from userspace (ie. simulate hardware triggering).
  231. *
  232. * Setting of an event triggering mechanism to userspace for ACTION_TRIGGER
  233. * enables the interrupt index for the device. Individual subindex interrupts
  234. * can be disabled using the -1 value for DATA_EVENTFD or the index can be
  235. * disabled as a whole with: flags = (DATA_NONE|ACTION_TRIGGER), count = 0.
  236. *
  237. * Note that ACTION_[UN]MASK specify user->kernel signaling (irqfds) while
  238. * ACTION_TRIGGER specifies kernel->user signaling.
  239. */
  240. struct vfio_irq_set {
  241. __u32 argsz;
  242. __u32 flags;
  243. #define VFIO_IRQ_SET_DATA_NONE (1 << 0) /* Data not present */
  244. #define VFIO_IRQ_SET_DATA_BOOL (1 << 1) /* Data is bool (u8) */
  245. #define VFIO_IRQ_SET_DATA_EVENTFD (1 << 2) /* Data is eventfd (s32) */
  246. #define VFIO_IRQ_SET_ACTION_MASK (1 << 3) /* Mask interrupt */
  247. #define VFIO_IRQ_SET_ACTION_UNMASK (1 << 4) /* Unmask interrupt */
  248. #define VFIO_IRQ_SET_ACTION_TRIGGER (1 << 5) /* Trigger interrupt */
  249. __u32 index;
  250. __u32 start;
  251. __u32 count;
  252. __u8 data[];
  253. };
  254. #define VFIO_DEVICE_SET_IRQS _IO(VFIO_TYPE, VFIO_BASE + 10)
  255. #define VFIO_IRQ_SET_DATA_TYPE_MASK (VFIO_IRQ_SET_DATA_NONE | \
  256. VFIO_IRQ_SET_DATA_BOOL | \
  257. VFIO_IRQ_SET_DATA_EVENTFD)
  258. #define VFIO_IRQ_SET_ACTION_TYPE_MASK (VFIO_IRQ_SET_ACTION_MASK | \
  259. VFIO_IRQ_SET_ACTION_UNMASK | \
  260. VFIO_IRQ_SET_ACTION_TRIGGER)
  261. /**
  262. * VFIO_DEVICE_RESET - _IO(VFIO_TYPE, VFIO_BASE + 11)
  263. *
  264. * Reset a device.
  265. */
  266. #define VFIO_DEVICE_RESET _IO(VFIO_TYPE, VFIO_BASE + 11)
  267. /*
  268. * The VFIO-PCI bus driver makes use of the following fixed region and
  269. * IRQ index mapping. Unimplemented regions return a size of zero.
  270. * Unimplemented IRQ types return a count of zero.
  271. */
  272. enum {
  273. VFIO_PCI_BAR0_REGION_INDEX,
  274. VFIO_PCI_BAR1_REGION_INDEX,
  275. VFIO_PCI_BAR2_REGION_INDEX,
  276. VFIO_PCI_BAR3_REGION_INDEX,
  277. VFIO_PCI_BAR4_REGION_INDEX,
  278. VFIO_PCI_BAR5_REGION_INDEX,
  279. VFIO_PCI_ROM_REGION_INDEX,
  280. VFIO_PCI_CONFIG_REGION_INDEX,
  281. VFIO_PCI_NUM_REGIONS
  282. };
  283. enum {
  284. VFIO_PCI_INTX_IRQ_INDEX,
  285. VFIO_PCI_MSI_IRQ_INDEX,
  286. VFIO_PCI_MSIX_IRQ_INDEX,
  287. VFIO_PCI_NUM_IRQS
  288. };
  289. /* -------- API for Type1 VFIO IOMMU -------- */
  290. /**
  291. * VFIO_IOMMU_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 12, struct vfio_iommu_info)
  292. *
  293. * Retrieve information about the IOMMU object. Fills in provided
  294. * struct vfio_iommu_info. Caller sets argsz.
  295. *
  296. * XXX Should we do these by CHECK_EXTENSION too?
  297. */
  298. struct vfio_iommu_type1_info {
  299. __u32 argsz;
  300. __u32 flags;
  301. #define VFIO_IOMMU_INFO_PGSIZES (1 << 0) /* supported page sizes info */
  302. __u64 iova_pgsizes; /* Bitmap of supported page sizes */
  303. };
  304. #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
  305. /**
  306. * VFIO_IOMMU_MAP_DMA - _IOW(VFIO_TYPE, VFIO_BASE + 13, struct vfio_dma_map)
  307. *
  308. * Map process virtual addresses to IO virtual addresses using the
  309. * provided struct vfio_dma_map. Caller sets argsz. READ &/ WRITE required.
  310. */
  311. struct vfio_iommu_type1_dma_map {
  312. __u32 argsz;
  313. __u32 flags;
  314. #define VFIO_DMA_MAP_FLAG_READ (1 << 0) /* readable from device */
  315. #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1) /* writable from device */
  316. __u64 vaddr; /* Process virtual address */
  317. __u64 iova; /* IO virtual address */
  318. __u64 size; /* Size of mapping (bytes) */
  319. };
  320. #define VFIO_IOMMU_MAP_DMA _IO(VFIO_TYPE, VFIO_BASE + 13)
  321. /**
  322. * VFIO_IOMMU_UNMAP_DMA - _IOW(VFIO_TYPE, VFIO_BASE + 14, struct vfio_dma_unmap)
  323. *
  324. * Unmap IO virtual addresses using the provided struct vfio_dma_unmap.
  325. * Caller sets argsz.
  326. */
  327. struct vfio_iommu_type1_dma_unmap {
  328. __u32 argsz;
  329. __u32 flags;
  330. __u64 iova; /* IO virtual address */
  331. __u64 size; /* Size of mapping (bytes) */
  332. };
  333. #define VFIO_IOMMU_UNMAP_DMA _IO(VFIO_TYPE, VFIO_BASE + 14)
  334. #endif /* _UAPIVFIO_H */