0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/mutex.h>
0012 #include <linux/pci.h>
0013 #include <linux/vfio.h>
0014 #include <linux/irqbypass.h>
0015 #include <linux/types.h>
0016 #include <linux/uuid.h>
0017 #include <linux/notifier.h>
0018
0019 #ifndef VFIO_PCI_CORE_H
0020 #define VFIO_PCI_CORE_H
0021
0022 #define VFIO_PCI_OFFSET_SHIFT 40
0023
0024 #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
0025 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
0026 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
0027
0028
0029 #define PCI_CAP_ID_INVALID 0xFF
0030 #define PCI_CAP_ID_INVALID_VIRT 0xFE
0031
0032
0033 #define VFIO_PCI_IOEVENTFD_MAX 1000
0034
0035 struct vfio_pci_ioeventfd {
0036 struct list_head next;
0037 struct vfio_pci_core_device *vdev;
0038 struct virqfd *virqfd;
0039 void __iomem *addr;
0040 uint64_t data;
0041 loff_t pos;
0042 int bar;
0043 int count;
0044 bool test_mem;
0045 };
0046
0047 struct vfio_pci_irq_ctx {
0048 struct eventfd_ctx *trigger;
0049 struct virqfd *unmask;
0050 struct virqfd *mask;
0051 char *name;
0052 bool masked;
0053 struct irq_bypass_producer producer;
0054 };
0055
0056 struct vfio_pci_core_device;
0057 struct vfio_pci_region;
0058
0059 struct vfio_pci_regops {
0060 ssize_t (*rw)(struct vfio_pci_core_device *vdev, char __user *buf,
0061 size_t count, loff_t *ppos, bool iswrite);
0062 void (*release)(struct vfio_pci_core_device *vdev,
0063 struct vfio_pci_region *region);
0064 int (*mmap)(struct vfio_pci_core_device *vdev,
0065 struct vfio_pci_region *region,
0066 struct vm_area_struct *vma);
0067 int (*add_capability)(struct vfio_pci_core_device *vdev,
0068 struct vfio_pci_region *region,
0069 struct vfio_info_cap *caps);
0070 };
0071
0072 struct vfio_pci_region {
0073 u32 type;
0074 u32 subtype;
0075 const struct vfio_pci_regops *ops;
0076 void *data;
0077 size_t size;
0078 u32 flags;
0079 };
0080
0081 struct vfio_pci_dummy_resource {
0082 struct resource resource;
0083 int index;
0084 struct list_head res_next;
0085 };
0086
0087 struct vfio_pci_vf_token {
0088 struct mutex lock;
0089 uuid_t uuid;
0090 int users;
0091 };
0092
0093 struct vfio_pci_mmap_vma {
0094 struct vm_area_struct *vma;
0095 struct list_head vma_next;
0096 };
0097
0098 struct vfio_pci_core_device {
0099 struct vfio_device vdev;
0100 struct pci_dev *pdev;
0101 void __iomem *barmap[PCI_STD_NUM_BARS];
0102 bool bar_mmap_supported[PCI_STD_NUM_BARS];
0103 u8 *pci_config_map;
0104 u8 *vconfig;
0105 struct perm_bits *msi_perm;
0106 spinlock_t irqlock;
0107 struct mutex igate;
0108 struct vfio_pci_irq_ctx *ctx;
0109 int num_ctx;
0110 int irq_type;
0111 int num_regions;
0112 struct vfio_pci_region *region;
0113 u8 msi_qmax;
0114 u8 msix_bar;
0115 u16 msix_size;
0116 u32 msix_offset;
0117 u32 rbar[7];
0118 bool pci_2_3;
0119 bool virq_disabled;
0120 bool reset_works;
0121 bool extended_caps;
0122 bool bardirty;
0123 bool has_vga;
0124 bool needs_reset;
0125 bool nointx;
0126 bool needs_pm_restore;
0127 struct pci_saved_state *pci_saved_state;
0128 struct pci_saved_state *pm_save;
0129 int ioeventfds_nr;
0130 struct eventfd_ctx *err_trigger;
0131 struct eventfd_ctx *req_trigger;
0132 struct list_head dummy_resources_list;
0133 struct mutex ioeventfds_lock;
0134 struct list_head ioeventfds_list;
0135 struct vfio_pci_vf_token *vf_token;
0136 struct list_head sriov_pfs_item;
0137 struct vfio_pci_core_device *sriov_pf_core_dev;
0138 struct notifier_block nb;
0139 struct mutex vma_lock;
0140 struct list_head vma_list;
0141 struct rw_semaphore memory_lock;
0142 };
0143
0144 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
0145 #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
0146 #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
0147 #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
0148 #define irq_is(vdev, type) (vdev->irq_type == type)
0149
0150 void vfio_pci_intx_mask(struct vfio_pci_core_device *vdev);
0151 void vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev);
0152
0153 int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev,
0154 uint32_t flags, unsigned index,
0155 unsigned start, unsigned count, void *data);
0156
0157 ssize_t vfio_pci_config_rw(struct vfio_pci_core_device *vdev,
0158 char __user *buf, size_t count,
0159 loff_t *ppos, bool iswrite);
0160
0161 ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf,
0162 size_t count, loff_t *ppos, bool iswrite);
0163
0164 #ifdef CONFIG_VFIO_PCI_VGA
0165 ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
0166 size_t count, loff_t *ppos, bool iswrite);
0167 #else
0168 static inline ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev,
0169 char __user *buf, size_t count,
0170 loff_t *ppos, bool iswrite)
0171 {
0172 return -EINVAL;
0173 }
0174 #endif
0175
0176 long vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
0177 uint64_t data, int count, int fd);
0178
0179 int vfio_pci_init_perm_bits(void);
0180 void vfio_pci_uninit_perm_bits(void);
0181
0182 int vfio_config_init(struct vfio_pci_core_device *vdev);
0183 void vfio_config_free(struct vfio_pci_core_device *vdev);
0184
0185 int vfio_pci_register_dev_region(struct vfio_pci_core_device *vdev,
0186 unsigned int type, unsigned int subtype,
0187 const struct vfio_pci_regops *ops,
0188 size_t size, u32 flags, void *data);
0189
0190 int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev,
0191 pci_power_t state);
0192
0193 bool __vfio_pci_memory_enabled(struct vfio_pci_core_device *vdev);
0194 void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *vdev);
0195 u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_core_device *vdev);
0196 void vfio_pci_memory_unlock_and_restore(struct vfio_pci_core_device *vdev,
0197 u16 cmd);
0198
0199 #ifdef CONFIG_VFIO_PCI_IGD
0200 int vfio_pci_igd_init(struct vfio_pci_core_device *vdev);
0201 #else
0202 static inline int vfio_pci_igd_init(struct vfio_pci_core_device *vdev)
0203 {
0204 return -ENODEV;
0205 }
0206 #endif
0207
0208 #ifdef CONFIG_VFIO_PCI_ZDEV_KVM
0209 int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
0210 struct vfio_info_cap *caps);
0211 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev);
0212 void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev);
0213 #else
0214 static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
0215 struct vfio_info_cap *caps)
0216 {
0217 return -ENODEV;
0218 }
0219
0220 static inline int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
0221 {
0222 return 0;
0223 }
0224
0225 static inline void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
0226 {}
0227 #endif
0228
0229
0230 void vfio_pci_core_set_params(bool nointxmask, bool is_disable_vga,
0231 bool is_disable_idle_d3);
0232 void vfio_pci_core_close_device(struct vfio_device *core_vdev);
0233 void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev,
0234 struct pci_dev *pdev,
0235 const struct vfio_device_ops *vfio_pci_ops);
0236 int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev);
0237 void vfio_pci_core_uninit_device(struct vfio_pci_core_device *vdev);
0238 void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev);
0239 extern const struct pci_error_handlers vfio_pci_core_err_handlers;
0240 int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev,
0241 int nr_virtfn);
0242 long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
0243 unsigned long arg);
0244 int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
0245 void __user *arg, size_t argsz);
0246 ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
0247 size_t count, loff_t *ppos);
0248 ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
0249 size_t count, loff_t *ppos);
0250 int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
0251 void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
0252 int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
0253 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev);
0254 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
0255 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
0256 pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
0257 pci_channel_state_t state);
0258
0259 static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
0260 {
0261 return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
0262 }
0263
0264 #endif