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