vfio.h 6.3 KB

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