0001
0002
0003
0004
0005
0006
0007
0008 #ifndef VFIO_H
0009 #define VFIO_H
0010
0011
0012 #include <linux/iommu.h>
0013 #include <linux/mm.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/poll.h>
0016 #include <uapi/linux/vfio.h>
0017
0018 struct kvm;
0019
0020
0021
0022
0023
0024
0025 struct vfio_device_set {
0026 void *set_id;
0027 struct mutex lock;
0028 struct list_head device_list;
0029 unsigned int device_count;
0030 };
0031
0032 struct vfio_device {
0033 struct device *dev;
0034 const struct vfio_device_ops *ops;
0035
0036
0037
0038
0039 const struct vfio_migration_ops *mig_ops;
0040 struct vfio_group *group;
0041 struct vfio_device_set *dev_set;
0042 struct list_head dev_set_list;
0043 unsigned int migration_flags;
0044
0045 struct kvm *kvm;
0046
0047
0048 refcount_t refcount;
0049 unsigned int open_count;
0050 struct completion comp;
0051 struct list_head group_next;
0052 struct list_head iommu_entry;
0053 };
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 struct vfio_device_ops {
0074 char *name;
0075 int (*open_device)(struct vfio_device *vdev);
0076 void (*close_device)(struct vfio_device *vdev);
0077 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
0078 size_t count, loff_t *ppos);
0079 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
0080 size_t count, loff_t *size);
0081 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
0082 unsigned long arg);
0083 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
0084 void (*request)(struct vfio_device *vdev, unsigned int count);
0085 int (*match)(struct vfio_device *vdev, char *buf);
0086 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
0087 int (*device_feature)(struct vfio_device *device, u32 flags,
0088 void __user *arg, size_t argsz);
0089 };
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 struct vfio_migration_ops {
0104 struct file *(*migration_set_state)(
0105 struct vfio_device *device,
0106 enum vfio_device_mig_state new_state);
0107 int (*migration_get_state)(struct vfio_device *device,
0108 enum vfio_device_mig_state *curr_state);
0109 };
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
0125 size_t minsz)
0126 {
0127 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
0128 ~supported_ops)
0129 return -EINVAL;
0130 if (flags & VFIO_DEVICE_FEATURE_PROBE)
0131 return 0;
0132
0133 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
0134 return -EINVAL;
0135 if (argsz < minsz)
0136 return -EINVAL;
0137 return 1;
0138 }
0139
0140 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
0141 const struct vfio_device_ops *ops);
0142 void vfio_uninit_group_dev(struct vfio_device *device);
0143 int vfio_register_group_dev(struct vfio_device *device);
0144 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
0145 void vfio_unregister_group_dev(struct vfio_device *device);
0146
0147 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
0148
0149 int vfio_mig_get_next_state(struct vfio_device *device,
0150 enum vfio_device_mig_state cur_fsm,
0151 enum vfio_device_mig_state new_fsm,
0152 enum vfio_device_mig_state *next_fsm);
0153
0154
0155
0156
0157 struct iommu_group *vfio_file_iommu_group(struct file *file);
0158 bool vfio_file_enforced_coherent(struct file *file);
0159 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
0160 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
0161
0162 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
0163
0164 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
0165 int npage, int prot, struct page **pages);
0166 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
0167 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
0168 void *data, size_t len, bool write);
0169
0170
0171
0172
0173 struct vfio_info_cap {
0174 struct vfio_info_cap_header *buf;
0175 size_t size;
0176 };
0177 struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
0178 size_t size, u16 id,
0179 u16 version);
0180 void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
0181
0182 int vfio_info_add_capability(struct vfio_info_cap *caps,
0183 struct vfio_info_cap_header *cap, size_t size);
0184
0185 int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
0186 int num_irqs, int max_irq_type,
0187 size_t *data_size);
0188
0189 struct pci_dev;
0190 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
0191 void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
0192 void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
0193 long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, unsigned int cmd,
0194 unsigned long arg);
0195 #else
0196 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
0197 {
0198 }
0199
0200 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
0201 {
0202 }
0203
0204 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
0205 unsigned int cmd,
0206 unsigned long arg)
0207 {
0208 return -ENOTTY;
0209 }
0210 #endif
0211
0212
0213
0214
0215 struct virqfd {
0216 void *opaque;
0217 struct eventfd_ctx *eventfd;
0218 int (*handler)(void *, void *);
0219 void (*thread)(void *, void *);
0220 void *data;
0221 struct work_struct inject;
0222 wait_queue_entry_t wait;
0223 poll_table pt;
0224 struct work_struct shutdown;
0225 struct virqfd **pvirqfd;
0226 };
0227
0228 int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
0229 void (*thread)(void *, void *), void *data,
0230 struct virqfd **pvirqfd, int fd);
0231 void vfio_virqfd_disable(struct virqfd **pvirqfd);
0232
0233 #endif