pci.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "sysemu/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. } VFIOMSIXInfo;
  105. #define TYPE_VFIO_PCI "vfio-pci"
  106. OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI)
  107. struct VFIOPCIDevice {
  108. PCIDevice pdev;
  109. VFIODevice vbasedev;
  110. VFIOINTx intx;
  111. unsigned int config_size;
  112. uint8_t *emulated_config_bits; /* QEMU emulated bits, little-endian */
  113. off_t config_offset; /* Offset of config space region within device fd */
  114. unsigned int rom_size;
  115. off_t rom_offset; /* Offset of ROM region within device fd */
  116. void *rom;
  117. int msi_cap_size;
  118. VFIOMSIVector *msi_vectors;
  119. VFIOMSIXInfo *msix;
  120. int nr_vectors; /* Number of MSI/MSIX vectors currently in use */
  121. int interrupt; /* Current interrupt type */
  122. VFIOBAR bars[PCI_NUM_REGIONS - 1]; /* No ROM */
  123. VFIOVGA *vga; /* 0xa0000, 0x3b0, 0x3c0 */
  124. void *igd_opregion;
  125. PCIHostDeviceAddress host;
  126. EventNotifier err_notifier;
  127. EventNotifier req_notifier;
  128. int (*resetfn)(struct VFIOPCIDevice *);
  129. uint32_t vendor_id;
  130. uint32_t device_id;
  131. uint32_t sub_vendor_id;
  132. uint32_t sub_device_id;
  133. uint32_t features;
  134. #define VFIO_FEATURE_ENABLE_VGA_BIT 0
  135. #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)
  136. #define VFIO_FEATURE_ENABLE_REQ_BIT 1
  137. #define VFIO_FEATURE_ENABLE_REQ (1 << VFIO_FEATURE_ENABLE_REQ_BIT)
  138. #define VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT 2
  139. #define VFIO_FEATURE_ENABLE_IGD_OPREGION \
  140. (1 << VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT)
  141. OnOffAuto display;
  142. uint32_t display_xres;
  143. uint32_t display_yres;
  144. int32_t bootindex;
  145. uint32_t igd_gms;
  146. OffAutoPCIBAR msix_relo;
  147. uint8_t pm_cap;
  148. uint8_t nv_gpudirect_clique;
  149. bool pci_aer;
  150. bool req_enabled;
  151. bool has_flr;
  152. bool has_pm_reset;
  153. bool rom_read_failed;
  154. bool no_kvm_intx;
  155. bool no_kvm_msi;
  156. bool no_kvm_msix;
  157. bool no_geforce_quirks;
  158. bool no_kvm_ioeventfd;
  159. bool no_vfio_ioeventfd;
  160. bool enable_ramfb;
  161. bool defer_kvm_irq_routing;
  162. VFIODisplay *dpy;
  163. Notifier irqchip_change_notifier;
  164. };
  165. /* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */
  166. static inline bool vfio_pci_is(VFIOPCIDevice *vdev, uint32_t vendor, uint32_t device)
  167. {
  168. return (vendor == PCI_ANY_ID || vendor == vdev->vendor_id) &&
  169. (device == PCI_ANY_ID || device == vdev->device_id);
  170. }
  171. static inline bool vfio_is_vga(VFIOPCIDevice *vdev)
  172. {
  173. PCIDevice *pdev = &vdev->pdev;
  174. uint16_t class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
  175. return class == PCI_CLASS_DISPLAY_VGA;
  176. }
  177. uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
  178. void vfio_pci_write_config(PCIDevice *pdev,
  179. uint32_t addr, uint32_t val, int len);
  180. uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
  181. void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
  182. bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev);
  183. void vfio_vga_quirk_setup(VFIOPCIDevice *vdev);
  184. void vfio_vga_quirk_exit(VFIOPCIDevice *vdev);
  185. void vfio_vga_quirk_finalize(VFIOPCIDevice *vdev);
  186. void vfio_bar_quirk_setup(VFIOPCIDevice *vdev, int nr);
  187. void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr);
  188. void vfio_bar_quirk_finalize(VFIOPCIDevice *vdev, int nr);
  189. void vfio_setup_resetfn_quirk(VFIOPCIDevice *vdev);
  190. int vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp);
  191. void vfio_quirk_reset(VFIOPCIDevice *vdev);
  192. VFIOQuirk *vfio_quirk_alloc(int nr_mem);
  193. void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr);
  194. extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
  195. int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp);
  196. int vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev,
  197. struct vfio_region_info *info,
  198. Error **errp);
  199. int vfio_pci_nvidia_v100_ram_init(VFIOPCIDevice *vdev, Error **errp);
  200. int vfio_pci_nvlink2_init(VFIOPCIDevice *vdev, Error **errp);
  201. void vfio_display_reset(VFIOPCIDevice *vdev);
  202. int vfio_display_probe(VFIOPCIDevice *vdev, Error **errp);
  203. void vfio_display_finalize(VFIOPCIDevice *vdev);
  204. #endif /* HW_VFIO_VFIO_PCI_H */