pci.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.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. #define PCI_ANY_ID (~0)
  21. struct VFIOPCIDevice;
  22. typedef struct VFIOIOEventFD {
  23. QLIST_ENTRY(VFIOIOEventFD) next;
  24. MemoryRegion *mr;
  25. hwaddr addr;
  26. unsigned size;
  27. uint64_t data;
  28. EventNotifier e;
  29. VFIORegion *region;
  30. hwaddr region_addr;
  31. bool dynamic; /* Added runtime, removed on device reset */
  32. bool vfio;
  33. } VFIOIOEventFD;
  34. typedef struct VFIOQuirk {
  35. QLIST_ENTRY(VFIOQuirk) next;
  36. void *data;
  37. QLIST_HEAD(, VFIOIOEventFD) ioeventfds;
  38. int nr_mem;
  39. MemoryRegion *mem;
  40. void (*reset)(struct VFIOPCIDevice *vdev, struct VFIOQuirk *quirk);
  41. } VFIOQuirk;
  42. typedef struct VFIOBAR {
  43. VFIORegion region;
  44. MemoryRegion *mr;
  45. size_t size;
  46. uint8_t type;
  47. bool ioport;
  48. bool mem64;
  49. QLIST_HEAD(, VFIOQuirk) quirks;
  50. } VFIOBAR;
  51. typedef struct VFIOVGARegion {
  52. MemoryRegion mem;
  53. off_t offset;
  54. int nr;
  55. QLIST_HEAD(, VFIOQuirk) quirks;
  56. } VFIOVGARegion;
  57. typedef struct VFIOVGA {
  58. off_t fd_offset;
  59. int fd;
  60. VFIOVGARegion region[QEMU_PCI_VGA_NUM_REGIONS];
  61. } VFIOVGA;
  62. typedef struct VFIOINTx {
  63. bool pending; /* interrupt pending */
  64. bool kvm_accel; /* set when QEMU bypass through KVM enabled */
  65. uint8_t pin; /* which pin to pull for qemu_set_irq */
  66. EventNotifier interrupt; /* eventfd triggered on interrupt */
  67. EventNotifier unmask; /* eventfd for unmask on QEMU bypass */
  68. PCIINTxRoute route; /* routing info for QEMU bypass */
  69. uint32_t mmap_timeout; /* delay to re-enable mmaps after interrupt */
  70. QEMUTimer *mmap_timer; /* enable mmaps after periods w/o interrupts */
  71. } VFIOINTx;
  72. typedef struct VFIOMSIVector {
  73. /*
  74. * Two interrupt paths are configured per vector. The first, is only used
  75. * for interrupts injected via QEMU. This is typically the non-accel path,
  76. * but may also be used when we want QEMU to handle masking and pending
  77. * bits. The KVM path bypasses QEMU and is therefore higher performance,
  78. * but requires masking at the device. virq is used to track the MSI route
  79. * through KVM, thus kvm_interrupt is only available when virq is set to a
  80. * valid (>= 0) value.
  81. */
  82. EventNotifier interrupt;
  83. EventNotifier kvm_interrupt;
  84. struct VFIOPCIDevice *vdev; /* back pointer to device */
  85. int virq;
  86. bool use;
  87. } VFIOMSIVector;
  88. enum {
  89. VFIO_INT_NONE = 0,
  90. VFIO_INT_INTx = 1,
  91. VFIO_INT_MSI = 2,
  92. VFIO_INT_MSIX = 3,
  93. };
  94. /* Cache of MSI-X setup */
  95. typedef struct VFIOMSIXInfo {
  96. uint8_t table_bar;
  97. uint8_t pba_bar;
  98. uint16_t entries;
  99. uint32_t table_offset;
  100. uint32_t pba_offset;
  101. unsigned long *pending;
  102. } VFIOMSIXInfo;
  103. typedef struct VFIOPCIDevice {
  104. PCIDevice pdev;
  105. VFIODevice vbasedev;
  106. VFIOINTx intx;
  107. unsigned int config_size;
  108. uint8_t *emulated_config_bits; /* QEMU emulated bits, little-endian */
  109. off_t config_offset; /* Offset of config space region within device fd */
  110. unsigned int rom_size;
  111. off_t rom_offset; /* Offset of ROM region within device fd */
  112. void *rom;
  113. int msi_cap_size;
  114. VFIOMSIVector *msi_vectors;
  115. VFIOMSIXInfo *msix;
  116. int nr_vectors; /* Number of MSI/MSIX vectors currently in use */
  117. int interrupt; /* Current interrupt type */
  118. VFIOBAR bars[PCI_NUM_REGIONS - 1]; /* No ROM */
  119. VFIOVGA *vga; /* 0xa0000, 0x3b0, 0x3c0 */
  120. void *igd_opregion;
  121. PCIHostDeviceAddress host;
  122. EventNotifier err_notifier;
  123. EventNotifier req_notifier;
  124. int (*resetfn)(struct VFIOPCIDevice *);
  125. uint32_t vendor_id;
  126. uint32_t device_id;
  127. uint32_t sub_vendor_id;
  128. uint32_t sub_device_id;
  129. uint32_t features;
  130. #define VFIO_FEATURE_ENABLE_VGA_BIT 0
  131. #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)
  132. #define VFIO_FEATURE_ENABLE_REQ_BIT 1
  133. #define VFIO_FEATURE_ENABLE_REQ (1 << VFIO_FEATURE_ENABLE_REQ_BIT)
  134. #define VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT 2
  135. #define VFIO_FEATURE_ENABLE_IGD_OPREGION \
  136. (1 << VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT)
  137. OnOffAuto display;
  138. uint32_t display_xres;
  139. uint32_t display_yres;
  140. int32_t bootindex;
  141. uint32_t igd_gms;
  142. OffAutoPCIBAR msix_relo;
  143. uint8_t pm_cap;
  144. uint8_t nv_gpudirect_clique;
  145. bool pci_aer;
  146. bool req_enabled;
  147. bool has_flr;
  148. bool has_pm_reset;
  149. bool rom_read_failed;
  150. bool no_kvm_intx;
  151. bool no_kvm_msi;
  152. bool no_kvm_msix;
  153. bool no_geforce_quirks;
  154. bool no_kvm_ioeventfd;
  155. bool no_vfio_ioeventfd;
  156. bool enable_ramfb;
  157. VFIODisplay *dpy;
  158. Error *migration_blocker;
  159. Notifier irqchip_change_notifier;
  160. } VFIOPCIDevice;
  161. uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
  162. void vfio_pci_write_config(PCIDevice *pdev,
  163. uint32_t addr, uint32_t val, int len);
  164. uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
  165. void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
  166. bool vfio_blacklist_opt_rom(VFIOPCIDevice *vdev);
  167. void vfio_vga_quirk_setup(VFIOPCIDevice *vdev);
  168. void vfio_vga_quirk_exit(VFIOPCIDevice *vdev);
  169. void vfio_vga_quirk_finalize(VFIOPCIDevice *vdev);
  170. void vfio_bar_quirk_setup(VFIOPCIDevice *vdev, int nr);
  171. void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr);
  172. void vfio_bar_quirk_finalize(VFIOPCIDevice *vdev, int nr);
  173. void vfio_setup_resetfn_quirk(VFIOPCIDevice *vdev);
  174. int vfio_add_virt_caps(VFIOPCIDevice *vdev, Error **errp);
  175. void vfio_quirk_reset(VFIOPCIDevice *vdev);
  176. extern const PropertyInfo qdev_prop_nv_gpudirect_clique;
  177. int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp);
  178. int vfio_pci_igd_opregion_init(VFIOPCIDevice *vdev,
  179. struct vfio_region_info *info,
  180. Error **errp);
  181. int vfio_pci_nvidia_v100_ram_init(VFIOPCIDevice *vdev, Error **errp);
  182. int vfio_pci_nvlink2_init(VFIOPCIDevice *vdev, Error **errp);
  183. void vfio_display_reset(VFIOPCIDevice *vdev);
  184. int vfio_display_probe(VFIOPCIDevice *vdev, Error **errp);
  185. void vfio_display_finalize(VFIOPCIDevice *vdev);
  186. #endif /* HW_VFIO_VFIO_PCI_H */