2 * device quirks for PCI devices
4 * Copyright Red Hat, Inc. 2012-2015
7 * Alex Williamson <alex.williamson@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 #include "qemu/range.h"
17 /* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */
18 static bool vfio_pci_is(VFIOPCIDevice
*vdev
, uint32_t vendor
, uint32_t device
)
20 return (vendor
== PCI_ANY_ID
|| vendor
== vdev
->vendor_id
) &&
21 (device
== PCI_ANY_ID
|| device
== vdev
->device_id
);
24 static bool vfio_is_vga(VFIOPCIDevice
*vdev
)
26 PCIDevice
*pdev
= &vdev
->pdev
;
27 uint16_t class = pci_get_word(pdev
->config
+ PCI_CLASS_DEVICE
);
29 return class == PCI_CLASS_DISPLAY_VGA
;
33 * List of device ids/vendor ids for which to disable
34 * option rom loading. This avoids the guest hangs during rom
35 * execution as noticed with the BCM 57810 card for lack of a
36 * more better way to handle such issues.
37 * The user can still override by specifying a romfile or
39 * Please see https://bugs.launchpad.net/qemu/+bug/1284874
40 * for an analysis of the 57810 card hang. When adding
41 * a new vendor id/device id combination below, please also add
42 * your card/environment details and information that could
43 * help in debugging to the bug tracking this issue
49 { 0x14e4, 0x168e }, /* Broadcom BCM 57810 */
52 bool vfio_blacklist_opt_rom(VFIOPCIDevice
*vdev
)
56 for (i
= 0 ; i
< ARRAY_SIZE(romblacklist
); i
++) {
57 if (vfio_pci_is(vdev
, romblacklist
[i
].vendor
, romblacklist
[i
].device
)) {
58 trace_vfio_quirk_rom_blacklisted(vdev
->vbasedev
.name
,
59 romblacklist
[i
].vendor
,
60 romblacklist
[i
].device
);
68 * Device specific region quirks (mostly backdoors to PCI config space)
72 * The generic window quirks operate on an address and data register,
73 * vfio_generic_window_address_quirk handles the address register and
74 * vfio_generic_window_data_quirk handles the data register. These ops
75 * pass reads and writes through to hardware until a value matching the
76 * stored address match/mask is written. When this occurs, the data
77 * register access emulated PCI config space for the device rather than
78 * passing through accesses. This enables devices where PCI config space
79 * is accessible behind a window register to maintain the virtualization
80 * provided through vfio.
82 typedef struct VFIOConfigWindowMatch
{
85 } VFIOConfigWindowMatch
;
87 typedef struct VFIOConfigWindowQuirk
{
88 struct VFIOPCIDevice
*vdev
;
92 uint32_t address_offset
;
98 MemoryRegion
*addr_mem
;
99 MemoryRegion
*data_mem
;
102 VFIOConfigWindowMatch matches
[];
103 } VFIOConfigWindowQuirk
;
105 static uint64_t vfio_generic_window_quirk_address_read(void *opaque
,
109 VFIOConfigWindowQuirk
*window
= opaque
;
110 VFIOPCIDevice
*vdev
= window
->vdev
;
112 return vfio_region_read(&vdev
->bars
[window
->bar
].region
,
113 addr
+ window
->address_offset
, size
);
116 static void vfio_generic_window_quirk_address_write(void *opaque
, hwaddr addr
,
120 VFIOConfigWindowQuirk
*window
= opaque
;
121 VFIOPCIDevice
*vdev
= window
->vdev
;
124 window
->window_enabled
= false;
126 vfio_region_write(&vdev
->bars
[window
->bar
].region
,
127 addr
+ window
->address_offset
, data
, size
);
129 for (i
= 0; i
< window
->nr_matches
; i
++) {
130 if ((data
& ~window
->matches
[i
].mask
) == window
->matches
[i
].match
) {
131 window
->window_enabled
= true;
132 window
->address_val
= data
& window
->matches
[i
].mask
;
133 trace_vfio_quirk_generic_window_address_write(vdev
->vbasedev
.name
,
134 memory_region_name(window
->addr_mem
), data
);
140 static const MemoryRegionOps vfio_generic_window_address_quirk
= {
141 .read
= vfio_generic_window_quirk_address_read
,
142 .write
= vfio_generic_window_quirk_address_write
,
143 .endianness
= DEVICE_LITTLE_ENDIAN
,
146 static uint64_t vfio_generic_window_quirk_data_read(void *opaque
,
147 hwaddr addr
, unsigned size
)
149 VFIOConfigWindowQuirk
*window
= opaque
;
150 VFIOPCIDevice
*vdev
= window
->vdev
;
153 /* Always read data reg, discard if window enabled */
154 data
= vfio_region_read(&vdev
->bars
[window
->bar
].region
,
155 addr
+ window
->data_offset
, size
);
157 if (window
->window_enabled
) {
158 data
= vfio_pci_read_config(&vdev
->pdev
, window
->address_val
, size
);
159 trace_vfio_quirk_generic_window_data_read(vdev
->vbasedev
.name
,
160 memory_region_name(window
->data_mem
), data
);
166 static void vfio_generic_window_quirk_data_write(void *opaque
, hwaddr addr
,
167 uint64_t data
, unsigned size
)
169 VFIOConfigWindowQuirk
*window
= opaque
;
170 VFIOPCIDevice
*vdev
= window
->vdev
;
172 if (window
->window_enabled
) {
173 vfio_pci_write_config(&vdev
->pdev
, window
->address_val
, data
, size
);
174 trace_vfio_quirk_generic_window_data_write(vdev
->vbasedev
.name
,
175 memory_region_name(window
->data_mem
), data
);
179 vfio_region_write(&vdev
->bars
[window
->bar
].region
,
180 addr
+ window
->data_offset
, data
, size
);
183 static const MemoryRegionOps vfio_generic_window_data_quirk
= {
184 .read
= vfio_generic_window_quirk_data_read
,
185 .write
= vfio_generic_window_quirk_data_write
,
186 .endianness
= DEVICE_LITTLE_ENDIAN
,
190 * The generic mirror quirk handles devices which expose PCI config space
191 * through a region within a BAR. When enabled, reads and writes are
192 * redirected through to emulated PCI config space. XXX if PCI config space
193 * used memory regions, this could just be an alias.
195 typedef struct VFIOConfigMirrorQuirk
{
196 struct VFIOPCIDevice
*vdev
;
200 } VFIOConfigMirrorQuirk
;
202 static uint64_t vfio_generic_quirk_mirror_read(void *opaque
,
203 hwaddr addr
, unsigned size
)
205 VFIOConfigMirrorQuirk
*mirror
= opaque
;
206 VFIOPCIDevice
*vdev
= mirror
->vdev
;
209 /* Read and discard in case the hardware cares */
210 (void)vfio_region_read(&vdev
->bars
[mirror
->bar
].region
,
211 addr
+ mirror
->offset
, size
);
213 data
= vfio_pci_read_config(&vdev
->pdev
, addr
, size
);
214 trace_vfio_quirk_generic_mirror_read(vdev
->vbasedev
.name
,
215 memory_region_name(mirror
->mem
),
220 static void vfio_generic_quirk_mirror_write(void *opaque
, hwaddr addr
,
221 uint64_t data
, unsigned size
)
223 VFIOConfigMirrorQuirk
*mirror
= opaque
;
224 VFIOPCIDevice
*vdev
= mirror
->vdev
;
226 vfio_pci_write_config(&vdev
->pdev
, addr
, data
, size
);
227 trace_vfio_quirk_generic_mirror_write(vdev
->vbasedev
.name
,
228 memory_region_name(mirror
->mem
),
232 static const MemoryRegionOps vfio_generic_mirror_quirk
= {
233 .read
= vfio_generic_quirk_mirror_read
,
234 .write
= vfio_generic_quirk_mirror_write
,
235 .endianness
= DEVICE_LITTLE_ENDIAN
,
238 /* Is range1 fully contained within range2? */
239 static bool vfio_range_contained(uint64_t first1
, uint64_t len1
,
240 uint64_t first2
, uint64_t len2
) {
241 return (first1
>= first2
&& first1
+ len1
<= first2
+ len2
);
244 #define PCI_VENDOR_ID_ATI 0x1002
247 * Radeon HD cards (HD5450 & HD7850) report the upper byte of the I/O port BAR
248 * through VGA register 0x3c3. On newer cards, the I/O port BAR is always
249 * BAR4 (older cards like the X550 used BAR1, but we don't care to support
250 * those). Note that on bare metal, a read of 0x3c3 doesn't always return the
251 * I/O port BAR address. Originally this was coded to return the virtual BAR
252 * address only if the physical register read returns the actual BAR address,
253 * but users have reported greater success if we return the virtual address
256 static uint64_t vfio_ati_3c3_quirk_read(void *opaque
,
257 hwaddr addr
, unsigned size
)
259 VFIOPCIDevice
*vdev
= opaque
;
260 uint64_t data
= vfio_pci_read_config(&vdev
->pdev
,
261 PCI_BASE_ADDRESS_4
+ 1, size
);
263 trace_vfio_quirk_ati_3c3_read(vdev
->vbasedev
.name
, data
);
268 static const MemoryRegionOps vfio_ati_3c3_quirk
= {
269 .read
= vfio_ati_3c3_quirk_read
,
270 .endianness
= DEVICE_LITTLE_ENDIAN
,
273 static void vfio_vga_probe_ati_3c3_quirk(VFIOPCIDevice
*vdev
)
278 * As long as the BAR is >= 256 bytes it will be aligned such that the
279 * lower byte is always zero. Filter out anything else, if it exists.
281 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_ATI
, PCI_ANY_ID
) ||
282 !vdev
->bars
[4].ioport
|| vdev
->bars
[4].region
.size
< 256) {
286 quirk
= g_malloc0(sizeof(*quirk
));
287 quirk
->mem
= g_new0(MemoryRegion
, 1);
290 memory_region_init_io(quirk
->mem
, OBJECT(vdev
), &vfio_ati_3c3_quirk
, vdev
,
291 "vfio-ati-3c3-quirk", 1);
292 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
293 3 /* offset 3 bytes from 0x3c0 */, quirk
->mem
);
295 QLIST_INSERT_HEAD(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
,
298 trace_vfio_quirk_ati_3c3_probe(vdev
->vbasedev
.name
);
302 * Newer ATI/AMD devices, including HD5450 and HD7850, have a mirror to PCI
303 * config space through MMIO BAR2 at offset 0x4000. Nothing seems to access
304 * the MMIO space directly, but a window to this space is provided through
305 * I/O port BAR4. Offset 0x0 is the address register and offset 0x4 is the
306 * data register. When the address is programmed to a range of 0x4000-0x4fff
307 * PCI configuration space is available. Experimentation seems to indicate
308 * that read-only may be provided by hardware.
310 static void vfio_probe_ati_bar4_quirk(VFIOPCIDevice
*vdev
, int nr
)
313 VFIOConfigWindowQuirk
*window
;
315 /* This windows doesn't seem to be used except by legacy VGA code */
316 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_ATI
, PCI_ANY_ID
) ||
317 !vdev
->has_vga
|| nr
!= 4) {
321 quirk
= g_malloc0(sizeof(*quirk
));
322 quirk
->mem
= g_new0(MemoryRegion
, 2);
324 window
= quirk
->data
= g_malloc0(sizeof(*window
) +
325 sizeof(VFIOConfigWindowMatch
));
327 window
->address_offset
= 0;
328 window
->data_offset
= 4;
329 window
->nr_matches
= 1;
330 window
->matches
[0].match
= 0x4000;
331 window
->matches
[0].mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
333 window
->addr_mem
= &quirk
->mem
[0];
334 window
->data_mem
= &quirk
->mem
[1];
336 memory_region_init_io(window
->addr_mem
, OBJECT(vdev
),
337 &vfio_generic_window_address_quirk
, window
,
338 "vfio-ati-bar4-window-address-quirk", 4);
339 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
340 window
->address_offset
,
341 window
->addr_mem
, 1);
343 memory_region_init_io(window
->data_mem
, OBJECT(vdev
),
344 &vfio_generic_window_data_quirk
, window
,
345 "vfio-ati-bar4-window-data-quirk", 4);
346 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
348 window
->data_mem
, 1);
350 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
352 trace_vfio_quirk_ati_bar4_probe(vdev
->vbasedev
.name
);
356 * Trap the BAR2 MMIO mirror to config space as well.
358 static void vfio_probe_ati_bar2_quirk(VFIOPCIDevice
*vdev
, int nr
)
361 VFIOConfigMirrorQuirk
*mirror
;
363 /* Only enable on newer devices where BAR2 is 64bit */
364 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_ATI
, PCI_ANY_ID
) ||
365 !vdev
->has_vga
|| nr
!= 2 || !vdev
->bars
[2].mem64
) {
369 quirk
= g_malloc0(sizeof(*quirk
));
370 mirror
= quirk
->data
= g_malloc0(sizeof(*mirror
));
371 mirror
->mem
= quirk
->mem
= g_new0(MemoryRegion
, 1);
374 mirror
->offset
= 0x4000;
377 memory_region_init_io(mirror
->mem
, OBJECT(vdev
),
378 &vfio_generic_mirror_quirk
, mirror
,
379 "vfio-ati-bar2-4000-quirk", PCI_CONFIG_SPACE_SIZE
);
380 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
381 mirror
->offset
, mirror
->mem
, 1);
383 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
385 trace_vfio_quirk_ati_bar2_probe(vdev
->vbasedev
.name
);
389 * Older ATI/AMD cards like the X550 have a similar window to that above.
390 * I/O port BAR1 provides a window to a mirror of PCI config space located
391 * in BAR2 at offset 0xf00. We don't care to support such older cards, but
392 * note it for future reference.
395 #define PCI_VENDOR_ID_NVIDIA 0x10de
398 * Nvidia has several different methods to get to config space, the
399 * nouveu project has several of these documented here:
400 * https://github.com/pathscale/envytools/tree/master/hwdocs
402 * The first quirk is actually not documented in envytools and is found
403 * on 10de:01d1 (NVIDIA Corporation G72 [GeForce 7300 LE]). This is an
404 * NV46 chipset. The backdoor uses the legacy VGA I/O ports to access
405 * the mirror of PCI config space found at BAR0 offset 0x1800. The access
406 * sequence first writes 0x338 to I/O port 0x3d4. The target offset is
407 * then written to 0x3d0. Finally 0x538 is written for a read and 0x738
408 * is written for a write to 0x3d4. The BAR0 offset is then accessible
409 * through 0x3d0. This quirk doesn't seem to be necessary on newer cards
410 * that use the I/O port BAR5 window but it doesn't hurt to leave it.
412 typedef enum {NONE
= 0, SELECT
, WINDOW
, READ
, WRITE
} VFIONvidia3d0State
;
413 static const char *nv3d0_states
[] = { "NONE", "SELECT",
414 "WINDOW", "READ", "WRITE" };
416 typedef struct VFIONvidia3d0Quirk
{
418 VFIONvidia3d0State state
;
420 } VFIONvidia3d0Quirk
;
422 static uint64_t vfio_nvidia_3d4_quirk_read(void *opaque
,
423 hwaddr addr
, unsigned size
)
425 VFIONvidia3d0Quirk
*quirk
= opaque
;
426 VFIOPCIDevice
*vdev
= quirk
->vdev
;
430 return vfio_vga_read(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
434 static void vfio_nvidia_3d4_quirk_write(void *opaque
, hwaddr addr
,
435 uint64_t data
, unsigned size
)
437 VFIONvidia3d0Quirk
*quirk
= opaque
;
438 VFIOPCIDevice
*vdev
= quirk
->vdev
;
439 VFIONvidia3d0State old_state
= quirk
->state
;
445 if (old_state
== NONE
) {
446 quirk
->state
= SELECT
;
447 trace_vfio_quirk_nvidia_3d0_state(vdev
->vbasedev
.name
,
448 nv3d0_states
[quirk
->state
]);
452 if (old_state
== WINDOW
) {
454 trace_vfio_quirk_nvidia_3d0_state(vdev
->vbasedev
.name
,
455 nv3d0_states
[quirk
->state
]);
459 if (old_state
== WINDOW
) {
460 quirk
->state
= WRITE
;
461 trace_vfio_quirk_nvidia_3d0_state(vdev
->vbasedev
.name
,
462 nv3d0_states
[quirk
->state
]);
467 vfio_vga_write(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
468 addr
+ 0x14, data
, size
);
471 static const MemoryRegionOps vfio_nvidia_3d4_quirk
= {
472 .read
= vfio_nvidia_3d4_quirk_read
,
473 .write
= vfio_nvidia_3d4_quirk_write
,
474 .endianness
= DEVICE_LITTLE_ENDIAN
,
477 static uint64_t vfio_nvidia_3d0_quirk_read(void *opaque
,
478 hwaddr addr
, unsigned size
)
480 VFIONvidia3d0Quirk
*quirk
= opaque
;
481 VFIOPCIDevice
*vdev
= quirk
->vdev
;
482 VFIONvidia3d0State old_state
= quirk
->state
;
483 uint64_t data
= vfio_vga_read(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
488 if (old_state
== READ
&&
489 (quirk
->offset
& ~(PCI_CONFIG_SPACE_SIZE
- 1)) == 0x1800) {
490 uint8_t offset
= quirk
->offset
& (PCI_CONFIG_SPACE_SIZE
- 1);
492 data
= vfio_pci_read_config(&vdev
->pdev
, offset
, size
);
493 trace_vfio_quirk_nvidia_3d0_read(vdev
->vbasedev
.name
,
500 static void vfio_nvidia_3d0_quirk_write(void *opaque
, hwaddr addr
,
501 uint64_t data
, unsigned size
)
503 VFIONvidia3d0Quirk
*quirk
= opaque
;
504 VFIOPCIDevice
*vdev
= quirk
->vdev
;
505 VFIONvidia3d0State old_state
= quirk
->state
;
509 if (old_state
== SELECT
) {
510 quirk
->offset
= (uint32_t)data
;
511 quirk
->state
= WINDOW
;
512 trace_vfio_quirk_nvidia_3d0_state(vdev
->vbasedev
.name
,
513 nv3d0_states
[quirk
->state
]);
514 } else if (old_state
== WRITE
) {
515 if ((quirk
->offset
& ~(PCI_CONFIG_SPACE_SIZE
- 1)) == 0x1800) {
516 uint8_t offset
= quirk
->offset
& (PCI_CONFIG_SPACE_SIZE
- 1);
518 vfio_pci_write_config(&vdev
->pdev
, offset
, data
, size
);
519 trace_vfio_quirk_nvidia_3d0_write(vdev
->vbasedev
.name
,
525 vfio_vga_write(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
526 addr
+ 0x10, data
, size
);
529 static const MemoryRegionOps vfio_nvidia_3d0_quirk
= {
530 .read
= vfio_nvidia_3d0_quirk_read
,
531 .write
= vfio_nvidia_3d0_quirk_write
,
532 .endianness
= DEVICE_LITTLE_ENDIAN
,
535 static void vfio_vga_probe_nvidia_3d0_quirk(VFIOPCIDevice
*vdev
)
538 VFIONvidia3d0Quirk
*data
;
540 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_NVIDIA
, PCI_ANY_ID
) ||
541 !vdev
->bars
[1].region
.size
) {
545 quirk
= g_malloc0(sizeof(*quirk
));
546 quirk
->data
= data
= g_malloc0(sizeof(*data
));
547 quirk
->mem
= g_new0(MemoryRegion
, 2);
551 memory_region_init_io(&quirk
->mem
[0], OBJECT(vdev
), &vfio_nvidia_3d4_quirk
,
552 data
, "vfio-nvidia-3d4-quirk", 2);
553 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
554 0x14 /* 0x3c0 + 0x14 */, &quirk
->mem
[0]);
556 memory_region_init_io(&quirk
->mem
[1], OBJECT(vdev
), &vfio_nvidia_3d0_quirk
,
557 data
, "vfio-nvidia-3d0-quirk", 2);
558 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
559 0x10 /* 0x3c0 + 0x10 */, &quirk
->mem
[1]);
561 QLIST_INSERT_HEAD(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
,
564 trace_vfio_quirk_nvidia_3d0_probe(vdev
->vbasedev
.name
);
568 * The second quirk is documented in envytools. The I/O port BAR5 is just
569 * a set of address/data ports to the MMIO BARs. The BAR we care about is
570 * again BAR0. This backdoor is apparently a bit newer than the one above
571 * so we need to not only trap 256 bytes @0x1800, but all of PCI config
572 * space, including extended space is available at the 4k @0x88000.
574 typedef struct VFIONvidiaBAR5Quirk
{
577 MemoryRegion
*addr_mem
;
578 MemoryRegion
*data_mem
;
580 VFIOConfigWindowQuirk window
; /* last for match data */
581 } VFIONvidiaBAR5Quirk
;
583 static void vfio_nvidia_bar5_enable(VFIONvidiaBAR5Quirk
*bar5
)
585 VFIOPCIDevice
*vdev
= bar5
->window
.vdev
;
587 if (((bar5
->master
& bar5
->enable
) & 0x1) == bar5
->enabled
) {
591 bar5
->enabled
= !bar5
->enabled
;
592 trace_vfio_quirk_nvidia_bar5_state(vdev
->vbasedev
.name
,
593 bar5
->enabled
? "Enable" : "Disable");
594 memory_region_set_enabled(bar5
->addr_mem
, bar5
->enabled
);
595 memory_region_set_enabled(bar5
->data_mem
, bar5
->enabled
);
598 static uint64_t vfio_nvidia_bar5_quirk_master_read(void *opaque
,
599 hwaddr addr
, unsigned size
)
601 VFIONvidiaBAR5Quirk
*bar5
= opaque
;
602 VFIOPCIDevice
*vdev
= bar5
->window
.vdev
;
604 return vfio_region_read(&vdev
->bars
[5].region
, addr
, size
);
607 static void vfio_nvidia_bar5_quirk_master_write(void *opaque
, hwaddr addr
,
608 uint64_t data
, unsigned size
)
610 VFIONvidiaBAR5Quirk
*bar5
= opaque
;
611 VFIOPCIDevice
*vdev
= bar5
->window
.vdev
;
613 vfio_region_write(&vdev
->bars
[5].region
, addr
, data
, size
);
616 vfio_nvidia_bar5_enable(bar5
);
619 static const MemoryRegionOps vfio_nvidia_bar5_quirk_master
= {
620 .read
= vfio_nvidia_bar5_quirk_master_read
,
621 .write
= vfio_nvidia_bar5_quirk_master_write
,
622 .endianness
= DEVICE_LITTLE_ENDIAN
,
625 static uint64_t vfio_nvidia_bar5_quirk_enable_read(void *opaque
,
626 hwaddr addr
, unsigned size
)
628 VFIONvidiaBAR5Quirk
*bar5
= opaque
;
629 VFIOPCIDevice
*vdev
= bar5
->window
.vdev
;
631 return vfio_region_read(&vdev
->bars
[5].region
, addr
+ 4, size
);
634 static void vfio_nvidia_bar5_quirk_enable_write(void *opaque
, hwaddr addr
,
635 uint64_t data
, unsigned size
)
637 VFIONvidiaBAR5Quirk
*bar5
= opaque
;
638 VFIOPCIDevice
*vdev
= bar5
->window
.vdev
;
640 vfio_region_write(&vdev
->bars
[5].region
, addr
+ 4, data
, size
);
643 vfio_nvidia_bar5_enable(bar5
);
646 static const MemoryRegionOps vfio_nvidia_bar5_quirk_enable
= {
647 .read
= vfio_nvidia_bar5_quirk_enable_read
,
648 .write
= vfio_nvidia_bar5_quirk_enable_write
,
649 .endianness
= DEVICE_LITTLE_ENDIAN
,
652 static void vfio_probe_nvidia_bar5_quirk(VFIOPCIDevice
*vdev
, int nr
)
655 VFIONvidiaBAR5Quirk
*bar5
;
656 VFIOConfigWindowQuirk
*window
;
658 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_NVIDIA
, PCI_ANY_ID
) ||
659 !vdev
->has_vga
|| nr
!= 5) {
663 quirk
= g_malloc0(sizeof(*quirk
));
664 quirk
->mem
= g_new0(MemoryRegion
, 4);
666 bar5
= quirk
->data
= g_malloc0(sizeof(*bar5
) +
667 (sizeof(VFIOConfigWindowMatch
) * 2));
668 window
= &bar5
->window
;
671 window
->address_offset
= 0x8;
672 window
->data_offset
= 0xc;
673 window
->nr_matches
= 2;
674 window
->matches
[0].match
= 0x1800;
675 window
->matches
[0].mask
= PCI_CONFIG_SPACE_SIZE
- 1;
676 window
->matches
[1].match
= 0x88000;
677 window
->matches
[1].mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
679 window
->addr_mem
= bar5
->addr_mem
= &quirk
->mem
[0];
680 window
->data_mem
= bar5
->data_mem
= &quirk
->mem
[1];
682 memory_region_init_io(window
->addr_mem
, OBJECT(vdev
),
683 &vfio_generic_window_address_quirk
, window
,
684 "vfio-nvidia-bar5-window-address-quirk", 4);
685 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
686 window
->address_offset
,
687 window
->addr_mem
, 1);
688 memory_region_set_enabled(window
->addr_mem
, false);
690 memory_region_init_io(window
->data_mem
, OBJECT(vdev
),
691 &vfio_generic_window_data_quirk
, window
,
692 "vfio-nvidia-bar5-window-data-quirk", 4);
693 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
695 window
->data_mem
, 1);
696 memory_region_set_enabled(window
->data_mem
, false);
698 memory_region_init_io(&quirk
->mem
[2], OBJECT(vdev
),
699 &vfio_nvidia_bar5_quirk_master
, bar5
,
700 "vfio-nvidia-bar5-master-quirk", 4);
701 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
702 0, &quirk
->mem
[2], 1);
704 memory_region_init_io(&quirk
->mem
[3], OBJECT(vdev
),
705 &vfio_nvidia_bar5_quirk_enable
, bar5
,
706 "vfio-nvidia-bar5-enable-quirk", 4);
707 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
708 4, &quirk
->mem
[3], 1);
710 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
712 trace_vfio_quirk_nvidia_bar5_probe(vdev
->vbasedev
.name
);
716 * Finally, BAR0 itself. We want to redirect any accesses to either
717 * 0x1800 or 0x88000 through the PCI config space access functions.
719 static void vfio_nvidia_quirk_mirror_write(void *opaque
, hwaddr addr
,
720 uint64_t data
, unsigned size
)
722 VFIOConfigMirrorQuirk
*mirror
= opaque
;
723 VFIOPCIDevice
*vdev
= mirror
->vdev
;
724 PCIDevice
*pdev
= &vdev
->pdev
;
726 vfio_generic_quirk_mirror_write(opaque
, addr
, data
, size
);
729 * Nvidia seems to acknowledge MSI interrupts by writing 0xff to the
730 * MSI capability ID register. Both the ID and next register are
731 * read-only, so we allow writes covering either of those to real hw.
733 if ((pdev
->cap_present
& QEMU_PCI_CAP_MSI
) &&
734 vfio_range_contained(addr
, size
, pdev
->msi_cap
, PCI_MSI_FLAGS
)) {
735 vfio_region_write(&vdev
->bars
[mirror
->bar
].region
,
736 addr
+ mirror
->offset
, data
, size
);
737 trace_vfio_quirk_nvidia_bar0_msi_ack(vdev
->vbasedev
.name
);
741 static const MemoryRegionOps vfio_nvidia_mirror_quirk
= {
742 .read
= vfio_generic_quirk_mirror_read
,
743 .write
= vfio_nvidia_quirk_mirror_write
,
744 .endianness
= DEVICE_LITTLE_ENDIAN
,
747 static void vfio_probe_nvidia_bar0_quirk(VFIOPCIDevice
*vdev
, int nr
)
750 VFIOConfigMirrorQuirk
*mirror
;
752 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_NVIDIA
, PCI_ANY_ID
) ||
753 !vfio_is_vga(vdev
) || nr
!= 0) {
757 quirk
= g_malloc0(sizeof(*quirk
));
758 mirror
= quirk
->data
= g_malloc0(sizeof(*mirror
));
759 mirror
->mem
= quirk
->mem
= g_new0(MemoryRegion
, 1);
762 mirror
->offset
= 0x88000;
765 memory_region_init_io(mirror
->mem
, OBJECT(vdev
),
766 &vfio_nvidia_mirror_quirk
, mirror
,
767 "vfio-nvidia-bar0-88000-mirror-quirk",
768 PCIE_CONFIG_SPACE_SIZE
);
769 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
770 mirror
->offset
, mirror
->mem
, 1);
772 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
774 /* The 0x1800 offset mirror only seems to get used by legacy VGA */
776 quirk
= g_malloc0(sizeof(*quirk
));
777 mirror
= quirk
->data
= g_malloc0(sizeof(*mirror
));
778 mirror
->mem
= quirk
->mem
= g_new0(MemoryRegion
, 1);
781 mirror
->offset
= 0x1800;
784 memory_region_init_io(mirror
->mem
, OBJECT(vdev
),
785 &vfio_nvidia_mirror_quirk
, mirror
,
786 "vfio-nvidia-bar0-1800-mirror-quirk",
787 PCI_CONFIG_SPACE_SIZE
);
788 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
789 mirror
->offset
, mirror
->mem
, 1);
791 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
794 trace_vfio_quirk_nvidia_bar0_probe(vdev
->vbasedev
.name
);
798 * TODO - Some Nvidia devices provide config access to their companion HDA
799 * device and even to their parent bridge via these config space mirrors.
800 * Add quirks for those regions.
803 #define PCI_VENDOR_ID_REALTEK 0x10ec
806 * RTL8168 devices have a backdoor that can access the MSI-X table. At BAR2
807 * offset 0x70 there is a dword data register, offset 0x74 is a dword address
808 * register. According to the Linux r8169 driver, the MSI-X table is addressed
809 * when the "type" portion of the address register is set to 0x1. This appears
810 * to be bits 16:30. Bit 31 is both a write indicator and some sort of
811 * "address latched" indicator. Bits 12:15 are a mask field, which we can
812 * ignore because the MSI-X table should always be accessed as a dword (full
813 * mask). Bits 0:11 is offset within the type.
817 * Read from MSI-X table offset 0
818 * vfio: vfio_bar_write(0000:05:00.0:BAR2+0x74, 0x1f000, 4) // store read addr
819 * vfio: vfio_bar_read(0000:05:00.0:BAR2+0x74, 4) = 0x8001f000 // latch
820 * vfio: vfio_bar_read(0000:05:00.0:BAR2+0x70, 4) = 0xfee00398 // read data
822 * Write 0xfee00000 to MSI-X table offset 0
823 * vfio: vfio_bar_write(0000:05:00.0:BAR2+0x70, 0xfee00000, 4) // write data
824 * vfio: vfio_bar_write(0000:05:00.0:BAR2+0x74, 0x8001f000, 4) // do write
825 * vfio: vfio_bar_read(0000:05:00.0:BAR2+0x74, 4) = 0x1f000 // complete
827 typedef struct VFIOrtl8168Quirk
{
834 static uint64_t vfio_rtl8168_quirk_address_read(void *opaque
,
835 hwaddr addr
, unsigned size
)
837 VFIOrtl8168Quirk
*rtl
= opaque
;
838 VFIOPCIDevice
*vdev
= rtl
->vdev
;
839 uint64_t data
= vfio_region_read(&vdev
->bars
[2].region
, addr
+ 0x74, size
);
842 data
= rtl
->addr
^ 0x80000000U
; /* latch/complete */
843 trace_vfio_quirk_rtl8168_fake_latch(vdev
->vbasedev
.name
, data
);
849 static void vfio_rtl8168_quirk_address_write(void *opaque
, hwaddr addr
,
850 uint64_t data
, unsigned size
)
852 VFIOrtl8168Quirk
*rtl
= opaque
;
853 VFIOPCIDevice
*vdev
= rtl
->vdev
;
855 rtl
->enabled
= false;
857 if ((data
& 0x7fff0000) == 0x10000) { /* MSI-X table */
859 rtl
->addr
= (uint32_t)data
;
861 if (data
& 0x80000000U
) { /* Do write */
862 if (vdev
->pdev
.cap_present
& QEMU_PCI_CAP_MSIX
) {
863 hwaddr offset
= data
& 0xfff;
864 uint64_t val
= rtl
->data
;
866 trace_vfio_quirk_rtl8168_msix_write(vdev
->vbasedev
.name
,
867 (uint16_t)offset
, val
);
869 /* Write to the proper guest MSI-X table instead */
870 memory_region_dispatch_write(&vdev
->pdev
.msix_table_mmio
,
872 MEMTXATTRS_UNSPECIFIED
);
874 return; /* Do not write guest MSI-X data to hardware */
878 vfio_region_write(&vdev
->bars
[2].region
, addr
+ 0x74, data
, size
);
881 static const MemoryRegionOps vfio_rtl_address_quirk
= {
882 .read
= vfio_rtl8168_quirk_address_read
,
883 .write
= vfio_rtl8168_quirk_address_write
,
885 .min_access_size
= 4,
886 .max_access_size
= 4,
889 .endianness
= DEVICE_LITTLE_ENDIAN
,
892 static uint64_t vfio_rtl8168_quirk_data_read(void *opaque
,
893 hwaddr addr
, unsigned size
)
895 VFIOrtl8168Quirk
*rtl
= opaque
;
896 VFIOPCIDevice
*vdev
= rtl
->vdev
;
897 uint64_t data
= vfio_region_read(&vdev
->bars
[2].region
, addr
+ 0x74, size
);
899 if (rtl
->enabled
&& (vdev
->pdev
.cap_present
& QEMU_PCI_CAP_MSIX
)) {
900 hwaddr offset
= rtl
->addr
& 0xfff;
901 memory_region_dispatch_read(&vdev
->pdev
.msix_table_mmio
, offset
,
902 &data
, size
, MEMTXATTRS_UNSPECIFIED
);
903 trace_vfio_quirk_rtl8168_msix_read(vdev
->vbasedev
.name
, offset
, data
);
909 static void vfio_rtl8168_quirk_data_write(void *opaque
, hwaddr addr
,
910 uint64_t data
, unsigned size
)
912 VFIOrtl8168Quirk
*rtl
= opaque
;
913 VFIOPCIDevice
*vdev
= rtl
->vdev
;
915 rtl
->data
= (uint32_t)data
;
917 vfio_region_write(&vdev
->bars
[2].region
, addr
+ 0x70, data
, size
);
920 static const MemoryRegionOps vfio_rtl_data_quirk
= {
921 .read
= vfio_rtl8168_quirk_data_read
,
922 .write
= vfio_rtl8168_quirk_data_write
,
924 .min_access_size
= 4,
925 .max_access_size
= 4,
928 .endianness
= DEVICE_LITTLE_ENDIAN
,
931 static void vfio_probe_rtl8168_bar2_quirk(VFIOPCIDevice
*vdev
, int nr
)
934 VFIOrtl8168Quirk
*rtl
;
936 if (!vfio_pci_is(vdev
, PCI_VENDOR_ID_REALTEK
, 0x8168) || nr
!= 2) {
940 quirk
= g_malloc0(sizeof(*quirk
));
941 quirk
->mem
= g_new0(MemoryRegion
, 2);
943 quirk
->data
= rtl
= g_malloc0(sizeof(*rtl
));
946 memory_region_init_io(&quirk
->mem
[0], OBJECT(vdev
),
947 &vfio_rtl_address_quirk
, rtl
,
948 "vfio-rtl8168-window-address-quirk", 4);
949 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
950 0x74, &quirk
->mem
[0], 1);
952 memory_region_init_io(&quirk
->mem
[1], OBJECT(vdev
),
953 &vfio_rtl_data_quirk
, rtl
,
954 "vfio-rtl8168-window-data-quirk", 4);
955 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].region
.mem
,
956 0x70, &quirk
->mem
[1], 1);
958 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
960 trace_vfio_quirk_rtl8168_probe(vdev
->vbasedev
.name
);
964 * Common quirk probe entry points.
966 void vfio_vga_quirk_setup(VFIOPCIDevice
*vdev
)
968 vfio_vga_probe_ati_3c3_quirk(vdev
);
969 vfio_vga_probe_nvidia_3d0_quirk(vdev
);
972 void vfio_vga_quirk_teardown(VFIOPCIDevice
*vdev
)
977 for (i
= 0; i
< ARRAY_SIZE(vdev
->vga
.region
); i
++) {
978 QLIST_FOREACH(quirk
, &vdev
->vga
.region
[i
].quirks
, next
) {
979 for (j
= 0; j
< quirk
->nr_mem
; j
++) {
980 memory_region_del_subregion(&vdev
->vga
.region
[i
].mem
,
987 void vfio_vga_quirk_free(VFIOPCIDevice
*vdev
)
991 for (i
= 0; i
< ARRAY_SIZE(vdev
->vga
.region
); i
++) {
992 while (!QLIST_EMPTY(&vdev
->vga
.region
[i
].quirks
)) {
993 VFIOQuirk
*quirk
= QLIST_FIRST(&vdev
->vga
.region
[i
].quirks
);
994 QLIST_REMOVE(quirk
, next
);
995 for (j
= 0; j
< quirk
->nr_mem
; j
++) {
996 object_unparent(OBJECT(&quirk
->mem
[j
]));
1005 void vfio_bar_quirk_setup(VFIOPCIDevice
*vdev
, int nr
)
1007 vfio_probe_ati_bar4_quirk(vdev
, nr
);
1008 vfio_probe_ati_bar2_quirk(vdev
, nr
);
1009 vfio_probe_nvidia_bar5_quirk(vdev
, nr
);
1010 vfio_probe_nvidia_bar0_quirk(vdev
, nr
);
1011 vfio_probe_rtl8168_bar2_quirk(vdev
, nr
);
1014 void vfio_bar_quirk_teardown(VFIOPCIDevice
*vdev
, int nr
)
1016 VFIOBAR
*bar
= &vdev
->bars
[nr
];
1020 QLIST_FOREACH(quirk
, &bar
->quirks
, next
) {
1021 for (i
= 0; i
< quirk
->nr_mem
; i
++) {
1022 memory_region_del_subregion(&bar
->region
.mem
, &quirk
->mem
[i
]);
1027 void vfio_bar_quirk_free(VFIOPCIDevice
*vdev
, int nr
)
1029 VFIOBAR
*bar
= &vdev
->bars
[nr
];
1032 while (!QLIST_EMPTY(&bar
->quirks
)) {
1033 VFIOQuirk
*quirk
= QLIST_FIRST(&bar
->quirks
);
1034 QLIST_REMOVE(quirk
, next
);
1035 for (i
= 0; i
< quirk
->nr_mem
; i
++) {
1036 object_unparent(OBJECT(&quirk
->mem
[i
]));
1039 g_free(quirk
->data
);
1049 * AMD Radeon PCI config reset, based on Linux:
1050 * drivers/gpu/drm/radeon/ci_smc.c:ci_is_smc_running()
1051 * drivers/gpu/drm/radeon/radeon_device.c:radeon_pci_config_reset
1052 * drivers/gpu/drm/radeon/ci_smc.c:ci_reset_smc()
1053 * drivers/gpu/drm/radeon/ci_smc.c:ci_stop_smc_clock()
1054 * IDs: include/drm/drm_pciids.h
1055 * Registers: http://cgit.freedesktop.org/~agd5f/linux/commit/?id=4e2aa447f6f0
1057 * Bonaire and Hawaii GPUs do not respond to a bus reset. This is a bug in the
1058 * hardware that should be fixed on future ASICs. The symptom of this is that
1059 * once the accerlated driver loads, Windows guests will bsod on subsequent
1060 * attmpts to load the driver, such as after VM reset or shutdown/restart. To
1061 * work around this, we do an AMD specific PCI config reset, followed by an SMC
1062 * reset. The PCI config reset only works if SMC firmware is running, so we
1063 * have a dependency on the state of the device as to whether this reset will
1064 * be effective. There are still cases where we won't be able to kick the
1065 * device into working, but this greatly improves the usability overall. The
1066 * config reset magic is relatively common on AMD GPUs, but the setup and SMC
1067 * poking is largely ASIC specific.
1069 static bool vfio_radeon_smc_is_running(VFIOPCIDevice
*vdev
)
1074 * Registers 200h and 204h are index and data registers for accessing
1075 * indirect configuration registers within the device.
1077 vfio_region_write(&vdev
->bars
[5].region
, 0x200, 0x80000004, 4);
1078 clk
= vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4);
1079 vfio_region_write(&vdev
->bars
[5].region
, 0x200, 0x80000370, 4);
1080 pc_c
= vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4);
1082 return (!(clk
& 1) && (0x20100 <= pc_c
));
1086 * The scope of a config reset is controlled by a mode bit in the misc register
1087 * and a fuse, exposed as a bit in another register. The fuse is the default
1088 * (0 = GFX, 1 = whole GPU), the misc bit is a toggle, with the forumula
1089 * scope = !(misc ^ fuse), where the resulting scope is defined the same as
1090 * the fuse. A truth table therefore tells us that if misc == fuse, we need
1091 * to flip the value of the bit in the misc register.
1093 static void vfio_radeon_set_gfx_only_reset(VFIOPCIDevice
*vdev
)
1095 uint32_t misc
, fuse
;
1098 vfio_region_write(&vdev
->bars
[5].region
, 0x200, 0xc00c0000, 4);
1099 fuse
= vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4);
1102 vfio_region_write(&vdev
->bars
[5].region
, 0x200, 0xc0000010, 4);
1103 misc
= vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4);
1107 vfio_region_write(&vdev
->bars
[5].region
, 0x204, misc
^ 2, 4);
1108 vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4); /* flush */
1112 static int vfio_radeon_reset(VFIOPCIDevice
*vdev
)
1114 PCIDevice
*pdev
= &vdev
->pdev
;
1118 /* Defer to a kernel implemented reset */
1119 if (vdev
->vbasedev
.reset_works
) {
1120 trace_vfio_quirk_ati_bonaire_reset_skipped(vdev
->vbasedev
.name
);
1124 /* Enable only memory BAR access */
1125 vfio_pci_write_config(pdev
, PCI_COMMAND
, PCI_COMMAND_MEMORY
, 2);
1127 /* Reset only works if SMC firmware is loaded and running */
1128 if (!vfio_radeon_smc_is_running(vdev
)) {
1130 trace_vfio_quirk_ati_bonaire_reset_no_smc(vdev
->vbasedev
.name
);
1134 /* Make sure only the GFX function is reset */
1135 vfio_radeon_set_gfx_only_reset(vdev
);
1137 /* AMD PCI config reset */
1138 vfio_pci_write_config(pdev
, 0x7c, 0x39d5e86b, 4);
1141 /* Read back the memory size to make sure we're out of reset */
1142 for (i
= 0; i
< 100000; i
++) {
1143 if (vfio_region_read(&vdev
->bars
[5].region
, 0x5428, 4) != 0xffffffff) {
1149 trace_vfio_quirk_ati_bonaire_reset_timeout(vdev
->vbasedev
.name
);
1153 vfio_region_write(&vdev
->bars
[5].region
, 0x200, 0x80000000, 4);
1154 data
= vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4);
1156 vfio_region_write(&vdev
->bars
[5].region
, 0x204, data
, 4);
1158 /* Disable SMC clock */
1159 vfio_region_write(&vdev
->bars
[5].region
, 0x200, 0x80000004, 4);
1160 data
= vfio_region_read(&vdev
->bars
[5].region
, 0x204, 4);
1162 vfio_region_write(&vdev
->bars
[5].region
, 0x204, data
, 4);
1164 trace_vfio_quirk_ati_bonaire_reset_done(vdev
->vbasedev
.name
);
1167 /* Restore PCI command register */
1168 vfio_pci_write_config(pdev
, PCI_COMMAND
, 0, 2);
1173 void vfio_setup_resetfn_quirk(VFIOPCIDevice
*vdev
)
1175 switch (vdev
->vendor_id
) {
1177 switch (vdev
->device_id
) {
1179 case 0x6649: /* Bonaire [FirePro W5100] */
1182 case 0x6658: /* Bonaire XTX [Radeon R7 260X] */
1183 case 0x665c: /* Bonaire XT [Radeon HD 7790/8770 / R9 260 OEM] */
1184 case 0x665d: /* Bonaire [Radeon R7 200 Series] */
1186 case 0x67A0: /* Hawaii XT GL [FirePro W9100] */
1187 case 0x67A1: /* Hawaii PRO GL [FirePro W8100] */
1192 case 0x67B0: /* Hawaii XT [Radeon R9 290X] */
1193 case 0x67B1: /* Hawaii PRO [Radeon R9 290] */
1198 vdev
->resetfn
= vfio_radeon_reset
;
1199 trace_vfio_quirk_ati_bonaire_reset(vdev
->vbasedev
.name
);