vfio.h 16 KB

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