pci.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * vfio based device assignment support - PCI devices
  3. *
  4. * Copyright Red Hat, Inc. 2012-2015
  5. *
  6. * Authors:
  7. * Alex Williamson <alex.williamson@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. */
  12. #ifndef HW_VFIO_VFIO_PCI_H
  13. #define HW_VFIO_VFIO_PCI_H
  14. #include "exec/memory.h"
  15. #include "hw/pci/pci_device.h"
  16. #include "hw/vfio/vfio-common.h"
  17. #include "qemu/event_notifier.h"
  18. #include "qemu/queue.h"
  19. #include "qemu/timer.h"
  20. #include "qom/object.h"
  21. #include "system/kvm.h"
  22. #define PCI_ANY_ID (~0)
  23. struct VFIOPCIDevice;
  24. typedef struct VFIOIOEventFD {
  25. QLIST_ENTRY(VFIOIOEventFD) next;
  26. MemoryRegion *mr;
  27. hwaddr addr;
  28. unsigned size;
  29. uint64_t data;
  30. EventNotifier e;
  31. VFIORegion *region;
  32. hwaddr region_addr;
  33. bool dynamic; /* Added runtime, removed on device reset */
  34. bool vfio;
  35. } VFIOIOEventFD;
  36. typedef struct VFIOQuirk {
  37. QLIST_ENTRY(VFIOQuirk) next;
  38. void *data;
  39. QLIST_HEAD(, VFIOIOEventFD) ioeventfds;
  40. int nr_mem;
  41. MemoryRegion *mem;
  42. void (*reset)(struct VFIOPCIDevice *vdev, struct VFIOQuirk *quirk);
  43. } VFIOQuirk;
  44. typedef struct VFIOBAR {
  45. VFIORegion region;
  46. MemoryRegion *mr;
  47. size_t size;
  48. uint8_t type;
  49. bool ioport;
  50. bool mem64;
  51. QLIST_HEAD(, VFIOQuirk) quirks;
  52. } VFIOBAR;
  53. typedef struct VFIOVGARegion {
  54. MemoryRegion mem;
  55. off_t offset;
  56. int nr;
  57. QLIST_HEAD(, VFIOQuirk) quirks;
  58. } VFIOVGARegion;
  59. typedef struct VFIOVGA {
  60. off_t fd_offset;
  61. int fd;
  62. VFIOVGARegion region[QEMU_PCI_VGA_NUM_REGIONS];
  63. } VFIOVGA;
  64. typedef struct VFIOINTx {
  65. bool pending; /* interrupt pending */
  66. bool kvm_accel; /* set when QEMU bypass through KVM enabled */
  67. uint8_t pin; /* which pin to pull for qemu_set_irq */
  68. EventNotifier interrupt; /* eventfd triggered on interrupt */
  69. EventNotifier unmask; /* eventfd for unmask on QEMU bypass */
  70. PCIINTxRoute route; /* routing info for QEMU bypass */
  71. uint32_t mmap_timeout; /* delay to re-enable mmaps after interrupt */
  72. QEMUTimer *mmap_timer; /* enable mmaps after periods w/o interrupts */
  73. } VFIOINTx;
  74. typedef struct VFIOMSIVector {
  75. /*
  76. * Two interrupt paths are configured per vector. The first, is only used
  77. * for interrupts injected via QEMU. This is typically the non-accel path,
  78. * but may also be used when we want QEMU to handle masking and pending
  79. * bits. The KVM path bypasses QEMU and is therefore higher performance,
  80. * but requires masking at the device. virq is used to track the MSI route
  81. * through KVM, thus kvm_interrupt is only available when virq is set to a
  82. * valid (>= 0) value.
  83. */
  84. EventNotifier interrupt;
  85. EventNotifier kvm_interrupt;
  86. struct VFIOPCIDevice *vdev; /* back pointer to device */
  87. int virq;
  88. bool use;
  89. } VFIOMSIVector;
  90. enum {
  91. VFIO_INT_NONE = 0,
  92. VFIO_INT_INTx = 1,
  93. VFIO_INT_MSI = 2,
  94. VFIO_INT_MSIX = 3,
  95. };
  96. /* Cache of MSI-X setup */
  97. typedef struct VFIOMSIXInfo {
  98. uint8_t table_bar;
  99. uint8_t pba_bar;
  100. uint16_t entries;
  101. uint32_t table_offset;
  102. uint32_t pba_offset;
  103. unsigned long *pending;
  104. bool noresize;
  105. } VFIOMSIXInfo;
  106. #define TYPE_VFIO_PCI "vfio-pci"
  107. OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI)
  108. struct VFIOPCIDevice {
  109. PCIDevice pdev;
  110. VFIODevice vbasedev;
  111. VFIOINTx intx;
  112. unsigned int config_size;
  113. uint8_t *emulated_config_bits; /* QEMU emulated bits, little-endian */
  114. off_t config_offset; /* Offset of config space region within device fd */
  115. unsigned int rom_size;
  116. off_t rom_offset; /* Offset of ROM region within device fd */
  117. void *rom;
  118. int msi_cap_size;
  119. VFIOMSIVector *msi_vectors;
  120. VFIOMSIXInfo *msix;
  121. int nr_vectors; /* Number of MSI/MSIX vectors currently in use */
  122. int interrupt; /* Current interrupt type */
  123. VFIOBAR bars[PCI_NUM_REGIONS - 1]; /* No ROM */
  124. VFIOVGA *vga; /* 0xa0000, 0x3b0, 0x3c0 */
  125. void *igd_opregion;
  126. PCIHostDeviceAddress host;
  127. QemuUUID vf_token;
  128. EventNotifier err_notifier;
  129. EventNotifier req_notifier;
  130. int (*resetfn)(struct VFIOPCIDevice *);
  131. uint32_t vendor_id;
  132. uint32_t device_id;
  133. uint32_t sub_vendor_id;
  134. uint32_t sub_device_id;
  135. uint32_t features;
  136. #define VFIO_FEATURE_ENABLE_VGA_BIT 0
  137. #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)
  138. #define VFIO_FEATURE_ENABLE_REQ_BIT 1
  139. #define VFIO_FEATURE_ENABLE_REQ (1 << VFIO_FEATURE_ENABLE_REQ_BIT)
  140. #define VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT 2
  141. #define VFIO_FEATURE_ENABLE_IGD_OPREGION \
  142. (1 << VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT)
  143. #define VFIO_FEATURE_ENABLE_IGD_LPC_BIT 3
  144. #define VFIO_FEATURE_ENABLE_IGD_LPC \
  145. (1 << VFIO_FEATURE_ENABLE_IGD_LPC_BIT)
  146. OnOffAuto display;
  147. uint32_t display_xres;
  148. uint32_t display_yres;
  149. int32_t bootindex;
  150. OnOffAuto igd_legacy_mode;
  151. uint32_t igd_gms;
  152. OffAutoPCIBAR msix_relo;
  153. uint8_t nv_gpudirect_clique;
  154. bool pci_aer;
  155. bool req_enabled;
  156. bool has_flr;
  157. bool has_pm_reset;
  158. bool rom_read_failed;
  159. bool no_kvm_intx;
  160. bool no_kvm_msi;
  161. bool no_kvm_msix;
  162. bool no_geforce_quirks;
  163. bool no_kvm_ioeventfd;
  164. bool no_vfio_ioeventfd;
  165. bool enable_ramfb;
  166. OnOffAuto ramfb_migrate;
  167. bool defer_kvm_irq_routing;
  168. bool clear_parent_atomics_on_exit;
  169. bool skip_vsc_check;
  170. VFIODisplay *dpy;
  171. Notifier irqchip_change_notifier;
  172. };
  173. /* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */
  174. static inline bool vfio_pci_is(VFIOPCIDevice *vdev, uint32_t vendor, uint32_t device)
  175. {
  176. return (vendor == PCI_ANY_ID || vendor == vdev->vendor_id) &&
  177. (device == PCI_ANY_ID || device == vdev->device_id);
  178. }
  179. static inline bool vfio_is_vga(VFIOPCIDevice *vdev)
  180. {
  181. PCIDevice *pdev = &vdev->pdev;
  182. uint16_t class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
  183. return class == PCI_CLASS_DISPLAY_VGA;
  184. }
  185. uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
  186. void vfio_pci_write_config(PCIDevice *pdev,
  187. uint32_t addr, uint32_t val, int len);
  188. uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
  189. void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
  190. bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev);
  191. bool vfio_config_quirk_setup(VFIOPCIDevice *vdev, Error **errp);
  192. void vfio_vga_quirk_setup(VFIOPCIDevice *vdev);
  193. void vfio_vga_quirk_exit(VFIOPCIDevice *vdev);
  194. void vfio_vga_quirk_finalize(VFIOPCIDevice *vdev);
  195. void vfio_bar_quirk_setup(VFIOPCIDevice *vdev, int nr);
  196. void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr);
  197. void vfio_bar_quirk_finalize(VFIOPCIDevice *vdev, int nr);
  198. void vfio_setup_resetfn_quirk(VFIOPCIDevice *vdev);
  199. bool vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp);
  200. void vfio_quirk_reset(VFIOPCIDevice *vdev);
  201. VFIOQuirk *vfio_quirk_alloc(int nr_mem);
  202. void vfio_probe_igd_bar0_quirk(VFIOPCIDevice *vdev, int nr);
  203. bool vfio_probe_igd_config_quirk(VFIOPCIDevice *vdev, Error **errp);
  204. extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
  205. void vfio_pci_pre_reset(VFIOPCIDevice *vdev);
  206. void vfio_pci_post_reset(VFIOPCIDevice *vdev);
  207. bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name);
  208. int vfio_pci_get_pci_hot_reset_info(VFIOPCIDevice *vdev,
  209. struct vfio_pci_hot_reset_info **info_p);
  210. bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp);
  211. void vfio_display_reset(VFIOPCIDevice *vdev);
  212. bool vfio_display_probe(VFIOPCIDevice *vdev, Error **errp);
  213. void vfio_display_finalize(VFIOPCIDevice *vdev);
  214. extern const VMStateDescription vfio_display_vmstate;
  215. #endif /* HW_VFIO_VFIO_PCI_H */