1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 4 * Author: Alex Williamson <alex.williamson@redhat.com> 5 * 6 * Derived from original vfio: 7 * Copyright 2010 Cisco Systems, Inc. All rights reserved. 8 * Author: Tom Lyon, pugs@cisco.com 9 */ 10 11 #include <linux/device.h> 12 #include <linux/mutex.h> 13 #include <linux/pci.h> 14 #include <linux/vfio.h> 15 #include <linux/irqbypass.h> 16 #include <linux/rcupdate.h> 17 #include <linux/types.h> 18 #include <linux/uuid.h> 19 #include <linux/notifier.h> 20 #include <linux/pm.h> 21 22 #ifndef VFIO_PCI_CORE_H 23 #define VFIO_PCI_CORE_H 24 25 #define VFIO_PCI_OFFSET_SHIFT 40 26 #define VFIO_PCI_OFFSET_TO_INDEX(off) ((u64)(off) >> VFIO_PCI_OFFSET_SHIFT) 27 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT) 28 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1) 29 30 struct vfio_pci_core_device; 31 struct vfio_pci_region; 32 struct p2pdma_provider; 33 struct dma_buf_attachment; 34 35 struct vfio_pci_eventfd { 36 struct eventfd_ctx *ctx; 37 struct rcu_head rcu; 38 }; 39 40 struct vfio_pci_regops { 41 ssize_t (*rw)(struct vfio_pci_core_device *vdev, char __user *buf, 42 size_t count, loff_t *ppos, bool iswrite); 43 void (*release)(struct vfio_pci_core_device *vdev, 44 struct vfio_pci_region *region); 45 int (*mmap)(struct vfio_pci_core_device *vdev, 46 struct vfio_pci_region *region, 47 struct vm_area_struct *vma); 48 int (*add_capability)(struct vfio_pci_core_device *vdev, 49 struct vfio_pci_region *region, 50 struct vfio_info_cap *caps); 51 }; 52 53 struct vfio_pci_region { 54 u32 type; 55 u32 subtype; 56 const struct vfio_pci_regops *ops; 57 void *data; 58 size_t size; 59 u32 flags; 60 }; 61 62 struct vfio_pci_device_ops { 63 int (*get_dmabuf_phys)(struct vfio_pci_core_device *vdev, 64 struct p2pdma_provider **provider, 65 unsigned int region_index, 66 struct phys_vec *phys_vec, 67 struct vfio_region_dma_range *dma_ranges, 68 size_t nr_ranges); 69 }; 70 71 #if IS_ENABLED(CONFIG_VFIO_PCI_DMABUF) 72 int vfio_pci_core_fill_phys_vec(struct phys_vec *phys_vec, 73 struct vfio_region_dma_range *dma_ranges, 74 size_t nr_ranges, phys_addr_t start, 75 phys_addr_t len); 76 int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev, 77 struct p2pdma_provider **provider, 78 unsigned int region_index, 79 struct phys_vec *phys_vec, 80 struct vfio_region_dma_range *dma_ranges, 81 size_t nr_ranges); 82 #else 83 static inline int 84 vfio_pci_core_fill_phys_vec(struct phys_vec *phys_vec, 85 struct vfio_region_dma_range *dma_ranges, 86 size_t nr_ranges, phys_addr_t start, 87 phys_addr_t len) 88 { 89 return -EINVAL; 90 } 91 static inline int vfio_pci_core_get_dmabuf_phys( 92 struct vfio_pci_core_device *vdev, struct p2pdma_provider **provider, 93 unsigned int region_index, struct phys_vec *phys_vec, 94 struct vfio_region_dma_range *dma_ranges, size_t nr_ranges) 95 { 96 return -EOPNOTSUPP; 97 } 98 #endif 99 100 struct vfio_pci_core_device { 101 struct vfio_device vdev; 102 struct pci_dev *pdev; 103 const struct vfio_pci_device_ops *pci_ops; 104 void __iomem *barmap[PCI_STD_NUM_BARS]; 105 bool bar_mmap_supported[PCI_STD_NUM_BARS]; 106 /* Flags modified at runtime - dedicated storage unit */ 107 bool virq_disabled; 108 bool bardirty; 109 u8 *pci_config_map; 110 u8 *vconfig; 111 struct perm_bits *msi_perm; 112 spinlock_t irqlock; 113 struct mutex igate; 114 struct xarray ctx; 115 int irq_type; 116 int num_regions; 117 struct vfio_pci_region *region; 118 u8 msi_qmax; 119 u8 msix_bar; 120 u16 msix_size; 121 u32 msix_offset; 122 u32 rbar[7]; 123 /* Flags only modified on setup/release - bitfield ok */ 124 bool has_dyn_msix:1; 125 bool pci_2_3:1; 126 bool reset_works:1; 127 bool extended_caps:1; 128 bool has_vga:1; 129 bool nointx:1; 130 bool needs_pm_restore:1; 131 bool disable_idle_d3:1; 132 bool nointxmask:1; 133 bool disable_vga:1; 134 /* Flags modified at runtime - dedicated storage unit */ 135 bool needs_reset; 136 bool pm_intx_masked; 137 bool pm_runtime_engaged; 138 bool sriov_active; 139 struct pci_saved_state *pci_saved_state; 140 struct pci_saved_state *pm_save; 141 int ioeventfds_nr; 142 struct vfio_pci_eventfd __rcu *err_trigger; 143 struct vfio_pci_eventfd __rcu *req_trigger; 144 struct eventfd_ctx *pm_wake_eventfd_ctx; 145 struct list_head dummy_resources_list; 146 struct mutex ioeventfds_lock; 147 struct list_head ioeventfds_list; 148 struct vfio_pci_vf_token *vf_token; 149 struct list_head sriov_pfs_item; 150 struct vfio_pci_core_device *sriov_pf_core_dev; 151 struct notifier_block nb; 152 struct rw_semaphore memory_lock; 153 struct list_head dmabufs; 154 }; 155 156 enum vfio_pci_io_width { 157 VFIO_PCI_IO_WIDTH_1 = 1, 158 VFIO_PCI_IO_WIDTH_2 = 2, 159 VFIO_PCI_IO_WIDTH_4 = 4, 160 VFIO_PCI_IO_WIDTH_8 = 8, 161 }; 162 163 /* Will be exported for vfio pci drivers usage */ 164 int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, 165 unsigned int type, unsigned int subtype, 166 const struct vfio_pci_regops *ops, 167 size_t size, u32 flags, void *data); 168 void vfio_pci_core_close_device(struct vfio_device *core_vdev); 169 int vfio_pci_core_init_dev(struct vfio_device *core_vdev); 170 void vfio_pci_core_release_dev(struct vfio_device *core_vdev); 171 int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev); 172 void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev); 173 int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, 174 int nr_virtfn); 175 long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, 176 unsigned long arg); 177 int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, 178 void __user *arg, size_t argsz); 179 int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev, 180 struct vfio_region_info *info, 181 struct vfio_info_cap *caps); 182 ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf, 183 size_t count, loff_t *ppos); 184 ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf, 185 size_t count, loff_t *ppos); 186 vm_fault_t vfio_pci_vmf_insert_pfn(struct vfio_pci_core_device *vdev, 187 struct vm_fault *vmf, unsigned long pfn, 188 unsigned int order); 189 int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma); 190 void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count); 191 int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); 192 int vfio_pci_core_match_token_uuid(struct vfio_device *core_vdev, 193 const uuid_t *uuid); 194 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev); 195 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev); 196 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev); 197 ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, 198 void __iomem *io, char __user *buf, 199 loff_t off, size_t count, size_t x_start, 200 size_t x_end, bool iswrite, 201 enum vfio_pci_io_width max_width); 202 bool __vfio_pci_memory_enabled(struct vfio_pci_core_device *vdev); 203 bool vfio_pci_core_range_intersect_range(loff_t buf_start, size_t buf_cnt, 204 loff_t reg_start, size_t reg_cnt, 205 loff_t *buf_offset, 206 size_t *intersect_count, 207 size_t *register_offset); 208 #define VFIO_IOWRITE_DECLARATION(size) \ 209 int vfio_pci_core_iowrite##size(struct vfio_pci_core_device *vdev, \ 210 bool test_mem, u##size val, void __iomem *io); 211 212 VFIO_IOWRITE_DECLARATION(8) 213 VFIO_IOWRITE_DECLARATION(16) 214 VFIO_IOWRITE_DECLARATION(32) 215 #ifdef iowrite64 216 VFIO_IOWRITE_DECLARATION(64) 217 #endif 218 219 #define VFIO_IOREAD_DECLARATION(size) \ 220 int vfio_pci_core_ioread##size(struct vfio_pci_core_device *vdev, \ 221 bool test_mem, u##size *val, void __iomem *io); 222 223 VFIO_IOREAD_DECLARATION(8) 224 VFIO_IOREAD_DECLARATION(16) 225 VFIO_IOREAD_DECLARATION(32) 226 #ifdef ioread64 227 VFIO_IOREAD_DECLARATION(64) 228 #endif 229 230 static inline bool is_aligned_for_order(struct vm_area_struct *vma, 231 unsigned long addr, 232 unsigned long pfn, 233 unsigned int order) 234 { 235 return !(order && (addr < vma->vm_start || 236 addr + (PAGE_SIZE << order) > vma->vm_end || 237 !IS_ALIGNED(pfn, 1 << order))); 238 } 239 240 /* 241 * Returns a BAR's iomap base or an ERR_PTR() if, for example, the 242 * BAR isn't valid, its resource wasn't acquired, or its iomap 243 * failed. This shall only be used after vfio_pci_core_enable() 244 * has set up the BAR maps and before vfio_pci_core_disable() 245 * tears them down. 246 */ 247 static inline void __iomem __must_check * 248 vfio_pci_core_get_iomap(struct vfio_pci_core_device *vdev, unsigned int bar) 249 { 250 if (WARN_ON_ONCE(bar >= PCI_STD_NUM_BARS)) 251 return IOMEM_ERR_PTR(-EINVAL); 252 253 if (WARN_ON_ONCE(!vdev->barmap[bar])) 254 return IOMEM_ERR_PTR(-ENODEV); 255 256 return vdev->barmap[bar]; 257 } 258 259 int vfio_pci_dma_buf_iommufd_map(struct dma_buf_attachment *attachment, 260 struct phys_vec *phys); 261 262 int vfio_pci_core_runtime_suspend(struct vfio_pci_core_device *vdev); 263 int vfio_pci_core_runtime_resume(struct vfio_pci_core_device *vdev); 264 pci_ers_result_t vfio_pci_core_aer_err_detected(struct vfio_pci_core_device *vdev); 265 266 /* 267 * Per-driver PM/AER trampolines. driver_data points to the driver's private 268 * structure, which embeds a struct vfio_pci_core_device. 269 * 270 * @name: prefix for <name>_pm_ops and <name>_aer_err_detected 271 * @drv_type: type of the driver's private structure, including struct 272 * @member: member containing the struct vfio_pci_core_device 273 */ 274 #define VFIO_PCI_CORE_DEFINE_CALLBACKS(name, drv_type, member) \ 275 static int __maybe_unused name##_pm_suspend(struct device *dev) \ 276 { \ 277 drv_type *drvdata = dev_get_drvdata(dev); \ 278 \ 279 return vfio_pci_core_runtime_suspend(&drvdata->member); \ 280 } \ 281 static int __maybe_unused name##_pm_resume(struct device *dev) \ 282 { \ 283 drv_type *drvdata = dev_get_drvdata(dev); \ 284 \ 285 return vfio_pci_core_runtime_resume(&drvdata->member); \ 286 } \ 287 static const struct dev_pm_ops name##_pm_ops = { \ 288 SET_RUNTIME_PM_OPS(name##_pm_suspend, name##_pm_resume, NULL) \ 289 }; \ 290 static pci_ers_result_t name##_aer_err_detected(struct pci_dev *pdev, \ 291 pci_channel_state_t state) \ 292 { \ 293 drv_type *drvdata = pci_get_drvdata(pdev); \ 294 \ 295 return vfio_pci_core_aer_err_detected(&drvdata->member); \ 296 } 297 298 #endif /* VFIO_PCI_CORE_H */ 299