vfio.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * VFIO API definition
  4. *
  5. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  6. * Author: Alex Williamson <alex.williamson@redhat.com>
  7. */
  8. #ifndef VFIO_H
  9. #define VFIO_H
  10. #include <linux/iommu.h>
  11. #include <linux/mm.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/poll.h>
  14. #include <uapi/linux/vfio.h>
  15. /**
  16. * struct vfio_device_ops - VFIO bus driver device callbacks
  17. *
  18. * @open: Called when userspace creates new file descriptor for device
  19. * @release: Called when userspace releases file descriptor for device
  20. * @read: Perform read(2) on device file descriptor
  21. * @write: Perform write(2) on device file descriptor
  22. * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
  23. * operations documented below
  24. * @mmap: Perform mmap(2) on a region of the device file descriptor
  25. * @request: Request for the bus driver to release the device
  26. * @match: Optional device name match callback (return: 0 for no-match, >0 for
  27. * match, -errno for abort (ex. match with insufficient or incorrect
  28. * additional args)
  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. void (*request)(void *device_data, unsigned int count);
  42. int (*match)(void *device_data, char *buf);
  43. };
  44. extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
  45. extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
  46. extern int vfio_add_group_dev(struct device *dev,
  47. const struct vfio_device_ops *ops,
  48. void *device_data);
  49. extern void *vfio_del_group_dev(struct device *dev);
  50. extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
  51. extern void vfio_device_put(struct vfio_device *device);
  52. extern void *vfio_device_data(struct vfio_device *device);
  53. /**
  54. * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
  55. */
  56. struct vfio_iommu_driver_ops {
  57. char *name;
  58. struct module *owner;
  59. void *(*open)(unsigned long arg);
  60. void (*release)(void *iommu_data);
  61. ssize_t (*read)(void *iommu_data, char __user *buf,
  62. size_t count, loff_t *ppos);
  63. ssize_t (*write)(void *iommu_data, const char __user *buf,
  64. size_t count, loff_t *size);
  65. long (*ioctl)(void *iommu_data, unsigned int cmd,
  66. unsigned long arg);
  67. int (*mmap)(void *iommu_data, struct vm_area_struct *vma);
  68. int (*attach_group)(void *iommu_data,
  69. struct iommu_group *group);
  70. void (*detach_group)(void *iommu_data,
  71. struct iommu_group *group);
  72. int (*pin_pages)(void *iommu_data,
  73. struct iommu_group *group,
  74. unsigned long *user_pfn,
  75. int npage, int prot,
  76. unsigned long *phys_pfn);
  77. int (*unpin_pages)(void *iommu_data,
  78. unsigned long *user_pfn, int npage);
  79. int (*register_notifier)(void *iommu_data,
  80. unsigned long *events,
  81. struct notifier_block *nb);
  82. int (*unregister_notifier)(void *iommu_data,
  83. struct notifier_block *nb);
  84. int (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
  85. void *data, size_t count, bool write);
  86. };
  87. extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
  88. extern void vfio_unregister_iommu_driver(
  89. const struct vfio_iommu_driver_ops *ops);
  90. /*
  91. * External user API
  92. */
  93. extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
  94. extern void vfio_group_put_external_user(struct vfio_group *group);
  95. extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
  96. *dev);
  97. extern bool vfio_external_group_match_file(struct vfio_group *group,
  98. struct file *filep);
  99. extern int vfio_external_user_iommu_id(struct vfio_group *group);
  100. extern long vfio_external_check_extension(struct vfio_group *group,
  101. unsigned long arg);
  102. #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
  103. extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
  104. int npage, int prot, unsigned long *phys_pfn);
  105. extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
  106. int npage);
  107. extern int vfio_group_pin_pages(struct vfio_group *group,
  108. unsigned long *user_iova_pfn, int npage,
  109. int prot, unsigned long *phys_pfn);
  110. extern int vfio_group_unpin_pages(struct vfio_group *group,
  111. unsigned long *user_iova_pfn, int npage);
  112. extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
  113. void *data, size_t len, bool write);
  114. /* each type has independent events */
  115. enum vfio_notify_type {
  116. VFIO_IOMMU_NOTIFY = 0,
  117. VFIO_GROUP_NOTIFY = 1,
  118. };
  119. /* events for VFIO_IOMMU_NOTIFY */
  120. #define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0)
  121. /* events for VFIO_GROUP_NOTIFY */
  122. #define VFIO_GROUP_NOTIFY_SET_KVM BIT(0)
  123. extern int vfio_register_notifier(struct device *dev,
  124. enum vfio_notify_type type,
  125. unsigned long *required_events,
  126. struct notifier_block *nb);
  127. extern int vfio_unregister_notifier(struct device *dev,
  128. enum vfio_notify_type type,
  129. struct notifier_block *nb);
  130. struct kvm;
  131. extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
  132. /*
  133. * Sub-module helpers
  134. */
  135. struct vfio_info_cap {
  136. struct vfio_info_cap_header *buf;
  137. size_t size;
  138. };
  139. extern struct vfio_info_cap_header *vfio_info_cap_add(
  140. struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
  141. extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
  142. extern int vfio_info_add_capability(struct vfio_info_cap *caps,
  143. struct vfio_info_cap_header *cap,
  144. size_t size);
  145. extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
  146. int num_irqs, int max_irq_type,
  147. size_t *data_size);
  148. struct pci_dev;
  149. #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
  150. extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
  151. extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
  152. extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
  153. unsigned int cmd,
  154. unsigned long arg);
  155. #else
  156. static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
  157. {
  158. }
  159. static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
  160. {
  161. }
  162. static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
  163. unsigned int cmd,
  164. unsigned long arg)
  165. {
  166. return -ENOTTY;
  167. }
  168. #endif /* CONFIG_VFIO_SPAPR_EEH */
  169. /*
  170. * IRQfd - generic
  171. */
  172. struct virqfd {
  173. void *opaque;
  174. struct eventfd_ctx *eventfd;
  175. int (*handler)(void *, void *);
  176. void (*thread)(void *, void *);
  177. void *data;
  178. struct work_struct inject;
  179. wait_queue_entry_t wait;
  180. poll_table pt;
  181. struct work_struct shutdown;
  182. struct virqfd **pvirqfd;
  183. };
  184. extern int vfio_virqfd_enable(void *opaque,
  185. int (*handler)(void *, void *),
  186. void (*thread)(void *, void *),
  187. void *data, struct virqfd **pvirqfd, int fd);
  188. extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
  189. #endif /* VFIO_H */