2 * vfio based device assignment support
4 * Copyright Red Hat, Inc. 2012
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.
12 * Based on qemu-kvm device-assignment:
13 * Adapted for KVM by Qumranet.
14 * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
15 * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
16 * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
17 * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
18 * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
22 #include <linux/vfio.h>
23 #include <sys/ioctl.h>
26 #include <sys/types.h>
30 #include "exec/address-spaces.h"
31 #include "exec/memory.h"
32 #include "hw/pci/msi.h"
33 #include "hw/pci/msix.h"
34 #include "hw/pci/pci.h"
35 #include "qemu-common.h"
36 #include "qemu/error-report.h"
37 #include "qemu/event_notifier.h"
38 #include "qemu/queue.h"
39 #include "qemu/range.h"
40 #include "sysemu/kvm.h"
41 #include "sysemu/sysemu.h"
43 /* #define DEBUG_VFIO */
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
48 #define DPRINTF(fmt, ...) \
52 /* Extra debugging, trap acceleration paths for more logging */
53 #define VFIO_ALLOW_MMAP 1
54 #define VFIO_ALLOW_KVM_INTX 1
55 #define VFIO_ALLOW_KVM_MSI 1
56 #define VFIO_ALLOW_KVM_MSIX 1
60 typedef struct VFIOQuirk
{
62 struct VFIODevice
*vdev
;
63 QLIST_ENTRY(VFIOQuirk
) next
;
65 uint32_t base_offset
:TARGET_PAGE_BITS
;
66 uint32_t address_offset
:TARGET_PAGE_BITS
;
67 uint32_t address_size
:3;
70 uint32_t address_match
;
71 uint32_t address_mask
;
73 uint32_t address_val
:TARGET_PAGE_BITS
;
74 uint32_t data_offset
:TARGET_PAGE_BITS
;
83 typedef struct VFIOBAR
{
84 off_t fd_offset
; /* offset of BAR within device fd */
85 int fd
; /* device fd, allows us to pass VFIOBAR as opaque data */
86 MemoryRegion mem
; /* slow, read/write access */
87 MemoryRegion mmap_mem
; /* direct mapped access */
90 uint32_t flags
; /* VFIO region flags (rd/wr/mmap) */
91 uint8_t nr
; /* cache the BAR number for debug */
94 QLIST_HEAD(, VFIOQuirk
) quirks
;
97 typedef struct VFIOVGARegion
{
101 QLIST_HEAD(, VFIOQuirk
) quirks
;
104 typedef struct VFIOVGA
{
107 VFIOVGARegion region
[QEMU_PCI_VGA_NUM_REGIONS
];
110 typedef struct VFIOINTx
{
111 bool pending
; /* interrupt pending */
112 bool kvm_accel
; /* set when QEMU bypass through KVM enabled */
113 uint8_t pin
; /* which pin to pull for qemu_set_irq */
114 EventNotifier interrupt
; /* eventfd triggered on interrupt */
115 EventNotifier unmask
; /* eventfd for unmask on QEMU bypass */
116 PCIINTxRoute route
; /* routing info for QEMU bypass */
117 uint32_t mmap_timeout
; /* delay to re-enable mmaps after interrupt */
118 QEMUTimer
*mmap_timer
; /* enable mmaps after periods w/o interrupts */
121 typedef struct VFIOMSIVector
{
122 EventNotifier interrupt
; /* eventfd triggered on interrupt */
123 struct VFIODevice
*vdev
; /* back pointer to device */
124 MSIMessage msg
; /* cache the MSI message so we know when it changes */
125 int virq
; /* KVM irqchip route for QEMU bypass */
138 typedef struct VFIOType1
{
139 MemoryListener listener
;
144 typedef struct VFIOContainer
{
145 int fd
; /* /dev/vfio/vfio, empowered by the attached groups */
147 /* enable abstraction to support various iommu backends */
151 void (*release
)(struct VFIOContainer
*);
153 QLIST_HEAD(, VFIOGroup
) group_list
;
154 QLIST_ENTRY(VFIOContainer
) next
;
157 /* Cache of MSI-X setup plus extra mmap and memory region for split BAR map */
158 typedef struct VFIOMSIXInfo
{
162 uint32_t table_offset
;
164 MemoryRegion mmap_mem
;
168 typedef struct VFIODevice
{
172 unsigned int config_size
;
173 uint8_t *emulated_config_bits
; /* QEMU emulated bits, little-endian */
174 off_t config_offset
; /* Offset of config space region within device fd */
175 unsigned int rom_size
;
176 off_t rom_offset
; /* Offset of ROM region within device fd */
179 VFIOMSIVector
*msi_vectors
;
181 int nr_vectors
; /* Number of MSI/MSIX vectors currently in use */
182 int interrupt
; /* Current interrupt type */
183 VFIOBAR bars
[PCI_NUM_REGIONS
- 1]; /* No ROM */
184 VFIOVGA vga
; /* 0xa0000, 0x3b0, 0x3c0 */
185 PCIHostDeviceAddress host
;
186 QLIST_ENTRY(VFIODevice
) next
;
187 struct VFIOGroup
*group
;
188 EventNotifier err_notifier
;
190 #define VFIO_FEATURE_ENABLE_VGA_BIT 0
191 #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)
200 bool rom_read_failed
;
203 typedef struct VFIOGroup
{
206 VFIOContainer
*container
;
207 QLIST_HEAD(, VFIODevice
) device_list
;
208 QLIST_ENTRY(VFIOGroup
) next
;
209 QLIST_ENTRY(VFIOGroup
) container_next
;
212 #define MSIX_CAP_LENGTH 12
214 static QLIST_HEAD(, VFIOContainer
)
215 container_list
= QLIST_HEAD_INITIALIZER(container_list
);
217 static QLIST_HEAD(, VFIOGroup
)
218 group_list
= QLIST_HEAD_INITIALIZER(group_list
);
222 * We have a single VFIO pseudo device per KVM VM. Once created it lives
223 * for the life of the VM. Closing the file descriptor only drops our
224 * reference to it and the device's reference to kvm. Therefore once
225 * initialized, this file descriptor is only released on QEMU exit and
226 * we'll re-use it should another vfio device be attached before then.
228 static int vfio_kvm_device_fd
= -1;
231 static void vfio_disable_interrupts(VFIODevice
*vdev
);
232 static uint32_t vfio_pci_read_config(PCIDevice
*pdev
, uint32_t addr
, int len
);
233 static void vfio_pci_write_config(PCIDevice
*pdev
, uint32_t addr
,
234 uint32_t val
, int len
);
235 static void vfio_mmap_set_enabled(VFIODevice
*vdev
, bool enabled
);
238 * Common VFIO interrupt disable
240 static void vfio_disable_irqindex(VFIODevice
*vdev
, int index
)
242 struct vfio_irq_set irq_set
= {
243 .argsz
= sizeof(irq_set
),
244 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_TRIGGER
,
250 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
256 static void vfio_unmask_intx(VFIODevice
*vdev
)
258 struct vfio_irq_set irq_set
= {
259 .argsz
= sizeof(irq_set
),
260 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_UNMASK
,
261 .index
= VFIO_PCI_INTX_IRQ_INDEX
,
266 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
269 #ifdef CONFIG_KVM /* Unused outside of CONFIG_KVM code */
270 static void vfio_mask_intx(VFIODevice
*vdev
)
272 struct vfio_irq_set irq_set
= {
273 .argsz
= sizeof(irq_set
),
274 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_MASK
,
275 .index
= VFIO_PCI_INTX_IRQ_INDEX
,
280 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
285 * Disabling BAR mmaping can be slow, but toggling it around INTx can
286 * also be a huge overhead. We try to get the best of both worlds by
287 * waiting until an interrupt to disable mmaps (subsequent transitions
288 * to the same state are effectively no overhead). If the interrupt has
289 * been serviced and the time gap is long enough, we re-enable mmaps for
290 * performance. This works well for things like graphics cards, which
291 * may not use their interrupt at all and are penalized to an unusable
292 * level by read/write BAR traps. Other devices, like NICs, have more
293 * regular interrupts and see much better latency by staying in non-mmap
294 * mode. We therefore set the default mmap_timeout such that a ping
295 * is just enough to keep the mmap disabled. Users can experiment with
296 * other options with the x-intx-mmap-timeout-ms parameter (a value of
297 * zero disables the timer).
299 static void vfio_intx_mmap_enable(void *opaque
)
301 VFIODevice
*vdev
= opaque
;
303 if (vdev
->intx
.pending
) {
304 timer_mod(vdev
->intx
.mmap_timer
,
305 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vdev
->intx
.mmap_timeout
);
309 vfio_mmap_set_enabled(vdev
, true);
312 static void vfio_intx_interrupt(void *opaque
)
314 VFIODevice
*vdev
= opaque
;
316 if (!event_notifier_test_and_clear(&vdev
->intx
.interrupt
)) {
320 DPRINTF("%s(%04x:%02x:%02x.%x) Pin %c\n", __func__
, vdev
->host
.domain
,
321 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
322 'A' + vdev
->intx
.pin
);
324 vdev
->intx
.pending
= true;
325 pci_irq_assert(&vdev
->pdev
);
326 vfio_mmap_set_enabled(vdev
, false);
327 if (vdev
->intx
.mmap_timeout
) {
328 timer_mod(vdev
->intx
.mmap_timer
,
329 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vdev
->intx
.mmap_timeout
);
333 static void vfio_eoi(VFIODevice
*vdev
)
335 if (!vdev
->intx
.pending
) {
339 DPRINTF("%s(%04x:%02x:%02x.%x) EOI\n", __func__
, vdev
->host
.domain
,
340 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
342 vdev
->intx
.pending
= false;
343 pci_irq_deassert(&vdev
->pdev
);
344 vfio_unmask_intx(vdev
);
347 static void vfio_enable_intx_kvm(VFIODevice
*vdev
)
350 struct kvm_irqfd irqfd
= {
351 .fd
= event_notifier_get_fd(&vdev
->intx
.interrupt
),
352 .gsi
= vdev
->intx
.route
.irq
,
353 .flags
= KVM_IRQFD_FLAG_RESAMPLE
,
355 struct vfio_irq_set
*irq_set
;
359 if (!VFIO_ALLOW_KVM_INTX
|| !kvm_irqfds_enabled() ||
360 vdev
->intx
.route
.mode
!= PCI_INTX_ENABLED
||
361 !kvm_check_extension(kvm_state
, KVM_CAP_IRQFD_RESAMPLE
)) {
365 /* Get to a known interrupt state */
366 qemu_set_fd_handler(irqfd
.fd
, NULL
, NULL
, vdev
);
367 vfio_mask_intx(vdev
);
368 vdev
->intx
.pending
= false;
369 pci_irq_deassert(&vdev
->pdev
);
371 /* Get an eventfd for resample/unmask */
372 if (event_notifier_init(&vdev
->intx
.unmask
, 0)) {
373 error_report("vfio: Error: event_notifier_init failed eoi");
377 /* KVM triggers it, VFIO listens for it */
378 irqfd
.resamplefd
= event_notifier_get_fd(&vdev
->intx
.unmask
);
380 if (kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
)) {
381 error_report("vfio: Error: Failed to setup resample irqfd: %m");
385 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
387 irq_set
= g_malloc0(argsz
);
388 irq_set
->argsz
= argsz
;
389 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_UNMASK
;
390 irq_set
->index
= VFIO_PCI_INTX_IRQ_INDEX
;
393 pfd
= (int32_t *)&irq_set
->data
;
395 *pfd
= irqfd
.resamplefd
;
397 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
400 error_report("vfio: Error: Failed to setup INTx unmask fd: %m");
405 vfio_unmask_intx(vdev
);
407 vdev
->intx
.kvm_accel
= true;
409 DPRINTF("%s(%04x:%02x:%02x.%x) KVM INTx accel enabled\n",
410 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
411 vdev
->host
.slot
, vdev
->host
.function
);
416 irqfd
.flags
= KVM_IRQFD_FLAG_DEASSIGN
;
417 kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
);
419 event_notifier_cleanup(&vdev
->intx
.unmask
);
421 qemu_set_fd_handler(irqfd
.fd
, vfio_intx_interrupt
, NULL
, vdev
);
422 vfio_unmask_intx(vdev
);
426 static void vfio_disable_intx_kvm(VFIODevice
*vdev
)
429 struct kvm_irqfd irqfd
= {
430 .fd
= event_notifier_get_fd(&vdev
->intx
.interrupt
),
431 .gsi
= vdev
->intx
.route
.irq
,
432 .flags
= KVM_IRQFD_FLAG_DEASSIGN
,
435 if (!vdev
->intx
.kvm_accel
) {
440 * Get to a known state, hardware masked, QEMU ready to accept new
441 * interrupts, QEMU IRQ de-asserted.
443 vfio_mask_intx(vdev
);
444 vdev
->intx
.pending
= false;
445 pci_irq_deassert(&vdev
->pdev
);
447 /* Tell KVM to stop listening for an INTx irqfd */
448 if (kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
)) {
449 error_report("vfio: Error: Failed to disable INTx irqfd: %m");
452 /* We only need to close the eventfd for VFIO to cleanup the kernel side */
453 event_notifier_cleanup(&vdev
->intx
.unmask
);
455 /* QEMU starts listening for interrupt events. */
456 qemu_set_fd_handler(irqfd
.fd
, vfio_intx_interrupt
, NULL
, vdev
);
458 vdev
->intx
.kvm_accel
= false;
460 /* If we've missed an event, let it re-fire through QEMU */
461 vfio_unmask_intx(vdev
);
463 DPRINTF("%s(%04x:%02x:%02x.%x) KVM INTx accel disabled\n",
464 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
465 vdev
->host
.slot
, vdev
->host
.function
);
469 static void vfio_update_irq(PCIDevice
*pdev
)
471 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
474 if (vdev
->interrupt
!= VFIO_INT_INTx
) {
478 route
= pci_device_route_intx_to_irq(&vdev
->pdev
, vdev
->intx
.pin
);
480 if (!pci_intx_route_changed(&vdev
->intx
.route
, &route
)) {
481 return; /* Nothing changed */
484 DPRINTF("%s(%04x:%02x:%02x.%x) IRQ moved %d -> %d\n", __func__
,
485 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
486 vdev
->host
.function
, vdev
->intx
.route
.irq
, route
.irq
);
488 vfio_disable_intx_kvm(vdev
);
490 vdev
->intx
.route
= route
;
492 if (route
.mode
!= PCI_INTX_ENABLED
) {
496 vfio_enable_intx_kvm(vdev
);
498 /* Re-enable the interrupt in cased we missed an EOI */
502 static int vfio_enable_intx(VFIODevice
*vdev
)
504 uint8_t pin
= vfio_pci_read_config(&vdev
->pdev
, PCI_INTERRUPT_PIN
, 1);
506 struct vfio_irq_set
*irq_set
;
513 vfio_disable_interrupts(vdev
);
515 vdev
->intx
.pin
= pin
- 1; /* Pin A (1) -> irq[0] */
516 pci_config_set_interrupt_pin(vdev
->pdev
.config
, pin
);
520 * Only conditional to avoid generating error messages on platforms
521 * where we won't actually use the result anyway.
523 if (kvm_irqfds_enabled() &&
524 kvm_check_extension(kvm_state
, KVM_CAP_IRQFD_RESAMPLE
)) {
525 vdev
->intx
.route
= pci_device_route_intx_to_irq(&vdev
->pdev
,
530 ret
= event_notifier_init(&vdev
->intx
.interrupt
, 0);
532 error_report("vfio: Error: event_notifier_init failed");
536 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
538 irq_set
= g_malloc0(argsz
);
539 irq_set
->argsz
= argsz
;
540 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_TRIGGER
;
541 irq_set
->index
= VFIO_PCI_INTX_IRQ_INDEX
;
544 pfd
= (int32_t *)&irq_set
->data
;
546 *pfd
= event_notifier_get_fd(&vdev
->intx
.interrupt
);
547 qemu_set_fd_handler(*pfd
, vfio_intx_interrupt
, NULL
, vdev
);
549 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
552 error_report("vfio: Error: Failed to setup INTx fd: %m");
553 qemu_set_fd_handler(*pfd
, NULL
, NULL
, vdev
);
554 event_notifier_cleanup(&vdev
->intx
.interrupt
);
558 vfio_enable_intx_kvm(vdev
);
560 vdev
->interrupt
= VFIO_INT_INTx
;
562 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
563 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
568 static void vfio_disable_intx(VFIODevice
*vdev
)
572 timer_del(vdev
->intx
.mmap_timer
);
573 vfio_disable_intx_kvm(vdev
);
574 vfio_disable_irqindex(vdev
, VFIO_PCI_INTX_IRQ_INDEX
);
575 vdev
->intx
.pending
= false;
576 pci_irq_deassert(&vdev
->pdev
);
577 vfio_mmap_set_enabled(vdev
, true);
579 fd
= event_notifier_get_fd(&vdev
->intx
.interrupt
);
580 qemu_set_fd_handler(fd
, NULL
, NULL
, vdev
);
581 event_notifier_cleanup(&vdev
->intx
.interrupt
);
583 vdev
->interrupt
= VFIO_INT_NONE
;
585 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
586 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
592 static void vfio_msi_interrupt(void *opaque
)
594 VFIOMSIVector
*vector
= opaque
;
595 VFIODevice
*vdev
= vector
->vdev
;
596 int nr
= vector
- vdev
->msi_vectors
;
598 if (!event_notifier_test_and_clear(&vector
->interrupt
)) {
605 if (vdev
->interrupt
== VFIO_INT_MSIX
) {
606 msg
= msi_get_message(&vdev
->pdev
, nr
);
607 } else if (vdev
->interrupt
== VFIO_INT_MSI
) {
608 msg
= msix_get_message(&vdev
->pdev
, nr
);
613 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d 0x%"PRIx64
"/0x%x\n", __func__
,
614 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
615 vdev
->host
.function
, nr
, msg
.address
, msg
.data
);
618 if (vdev
->interrupt
== VFIO_INT_MSIX
) {
619 msix_notify(&vdev
->pdev
, nr
);
620 } else if (vdev
->interrupt
== VFIO_INT_MSI
) {
621 msi_notify(&vdev
->pdev
, nr
);
623 error_report("vfio: MSI interrupt receieved, but not enabled?");
627 static int vfio_enable_vectors(VFIODevice
*vdev
, bool msix
)
629 struct vfio_irq_set
*irq_set
;
630 int ret
= 0, i
, argsz
;
633 argsz
= sizeof(*irq_set
) + (vdev
->nr_vectors
* sizeof(*fds
));
635 irq_set
= g_malloc0(argsz
);
636 irq_set
->argsz
= argsz
;
637 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_TRIGGER
;
638 irq_set
->index
= msix
? VFIO_PCI_MSIX_IRQ_INDEX
: VFIO_PCI_MSI_IRQ_INDEX
;
640 irq_set
->count
= vdev
->nr_vectors
;
641 fds
= (int32_t *)&irq_set
->data
;
643 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
644 if (!vdev
->msi_vectors
[i
].use
) {
649 fds
[i
] = event_notifier_get_fd(&vdev
->msi_vectors
[i
].interrupt
);
652 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
659 static int vfio_msix_vector_do_use(PCIDevice
*pdev
, unsigned int nr
,
660 MSIMessage
*msg
, IOHandler
*handler
)
662 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
663 VFIOMSIVector
*vector
;
666 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d used\n", __func__
,
667 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
668 vdev
->host
.function
, nr
);
670 vector
= &vdev
->msi_vectors
[nr
];
674 msix_vector_use(pdev
, nr
);
676 if (event_notifier_init(&vector
->interrupt
, 0)) {
677 error_report("vfio: Error: event_notifier_init failed");
681 * Attempt to enable route through KVM irqchip,
682 * default to userspace handling if unavailable.
684 vector
->virq
= msg
&& VFIO_ALLOW_KVM_MSIX
?
685 kvm_irqchip_add_msi_route(kvm_state
, *msg
) : -1;
686 if (vector
->virq
< 0 ||
687 kvm_irqchip_add_irqfd_notifier(kvm_state
, &vector
->interrupt
,
688 NULL
, vector
->virq
) < 0) {
689 if (vector
->virq
>= 0) {
690 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
693 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
694 handler
, NULL
, vector
);
698 * We don't want to have the host allocate all possible MSI vectors
699 * for a device if they're not in use, so we shutdown and incrementally
700 * increase them as needed.
702 if (vdev
->nr_vectors
< nr
+ 1) {
703 vfio_disable_irqindex(vdev
, VFIO_PCI_MSIX_IRQ_INDEX
);
704 vdev
->nr_vectors
= nr
+ 1;
705 ret
= vfio_enable_vectors(vdev
, true);
707 error_report("vfio: failed to enable vectors, %d", ret
);
711 struct vfio_irq_set
*irq_set
;
714 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
716 irq_set
= g_malloc0(argsz
);
717 irq_set
->argsz
= argsz
;
718 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
719 VFIO_IRQ_SET_ACTION_TRIGGER
;
720 irq_set
->index
= VFIO_PCI_MSIX_IRQ_INDEX
;
723 pfd
= (int32_t *)&irq_set
->data
;
725 *pfd
= event_notifier_get_fd(&vector
->interrupt
);
727 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
730 error_report("vfio: failed to modify vector, %d", ret
);
737 static int vfio_msix_vector_use(PCIDevice
*pdev
,
738 unsigned int nr
, MSIMessage msg
)
740 return vfio_msix_vector_do_use(pdev
, nr
, &msg
, vfio_msi_interrupt
);
743 static void vfio_msix_vector_release(PCIDevice
*pdev
, unsigned int nr
)
745 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
746 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[nr
];
748 struct vfio_irq_set
*irq_set
;
751 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d released\n", __func__
,
752 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
753 vdev
->host
.function
, nr
);
756 * XXX What's the right thing to do here? This turns off the interrupt
757 * completely, but do we really just want to switch the interrupt to
758 * bouncing through userspace and let msix.c drop it? Not sure.
760 msix_vector_unuse(pdev
, nr
);
762 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
764 irq_set
= g_malloc0(argsz
);
765 irq_set
->argsz
= argsz
;
766 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
767 VFIO_IRQ_SET_ACTION_TRIGGER
;
768 irq_set
->index
= VFIO_PCI_MSIX_IRQ_INDEX
;
771 pfd
= (int32_t *)&irq_set
->data
;
775 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
779 if (vector
->virq
< 0) {
780 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
783 kvm_irqchip_remove_irqfd_notifier(kvm_state
, &vector
->interrupt
,
785 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
789 event_notifier_cleanup(&vector
->interrupt
);
793 static void vfio_enable_msix(VFIODevice
*vdev
)
795 vfio_disable_interrupts(vdev
);
797 vdev
->msi_vectors
= g_malloc0(vdev
->msix
->entries
* sizeof(VFIOMSIVector
));
799 vdev
->interrupt
= VFIO_INT_MSIX
;
802 * Some communication channels between VF & PF or PF & fw rely on the
803 * physical state of the device and expect that enabling MSI-X from the
804 * guest enables the same on the host. When our guest is Linux, the
805 * guest driver call to pci_enable_msix() sets the enabling bit in the
806 * MSI-X capability, but leaves the vector table masked. We therefore
807 * can't rely on a vector_use callback (from request_irq() in the guest)
808 * to switch the physical device into MSI-X mode because that may come a
809 * long time after pci_enable_msix(). This code enables vector 0 with
810 * triggering to userspace, then immediately release the vector, leaving
811 * the physical device with no vectors enabled, but MSI-X enabled, just
812 * like the guest view.
814 vfio_msix_vector_do_use(&vdev
->pdev
, 0, NULL
, NULL
);
815 vfio_msix_vector_release(&vdev
->pdev
, 0);
817 if (msix_set_vector_notifiers(&vdev
->pdev
, vfio_msix_vector_use
,
818 vfio_msix_vector_release
, NULL
)) {
819 error_report("vfio: msix_set_vector_notifiers failed");
822 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
823 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
826 static void vfio_enable_msi(VFIODevice
*vdev
)
830 vfio_disable_interrupts(vdev
);
832 vdev
->nr_vectors
= msi_nr_vectors_allocated(&vdev
->pdev
);
834 vdev
->msi_vectors
= g_malloc0(vdev
->nr_vectors
* sizeof(VFIOMSIVector
));
836 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
837 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
842 if (event_notifier_init(&vector
->interrupt
, 0)) {
843 error_report("vfio: Error: event_notifier_init failed");
846 vector
->msg
= msi_get_message(&vdev
->pdev
, i
);
849 * Attempt to enable route through KVM irqchip,
850 * default to userspace handling if unavailable.
852 vector
->virq
= VFIO_ALLOW_KVM_MSI
?
853 kvm_irqchip_add_msi_route(kvm_state
, vector
->msg
) : -1;
854 if (vector
->virq
< 0 ||
855 kvm_irqchip_add_irqfd_notifier(kvm_state
, &vector
->interrupt
,
856 NULL
, vector
->virq
) < 0) {
857 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
858 vfio_msi_interrupt
, NULL
, vector
);
862 ret
= vfio_enable_vectors(vdev
, false);
865 error_report("vfio: Error: Failed to setup MSI fds: %m");
866 } else if (ret
!= vdev
->nr_vectors
) {
867 error_report("vfio: Error: Failed to enable %d "
868 "MSI vectors, retry with %d", vdev
->nr_vectors
, ret
);
871 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
872 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
873 if (vector
->virq
>= 0) {
874 kvm_irqchip_remove_irqfd_notifier(kvm_state
, &vector
->interrupt
,
876 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
879 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
882 event_notifier_cleanup(&vector
->interrupt
);
885 g_free(vdev
->msi_vectors
);
887 if (ret
> 0 && ret
!= vdev
->nr_vectors
) {
888 vdev
->nr_vectors
= ret
;
891 vdev
->nr_vectors
= 0;
896 vdev
->interrupt
= VFIO_INT_MSI
;
898 DPRINTF("%s(%04x:%02x:%02x.%x) Enabled %d MSI vectors\n", __func__
,
899 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
900 vdev
->host
.function
, vdev
->nr_vectors
);
903 static void vfio_disable_msi_common(VFIODevice
*vdev
)
905 g_free(vdev
->msi_vectors
);
906 vdev
->msi_vectors
= NULL
;
907 vdev
->nr_vectors
= 0;
908 vdev
->interrupt
= VFIO_INT_NONE
;
910 vfio_enable_intx(vdev
);
913 static void vfio_disable_msix(VFIODevice
*vdev
)
917 msix_unset_vector_notifiers(&vdev
->pdev
);
920 * MSI-X will only release vectors if MSI-X is still enabled on the
921 * device, check through the rest and release it ourselves if necessary.
923 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
924 if (vdev
->msi_vectors
[i
].use
) {
925 vfio_msix_vector_release(&vdev
->pdev
, i
);
929 if (vdev
->nr_vectors
) {
930 vfio_disable_irqindex(vdev
, VFIO_PCI_MSIX_IRQ_INDEX
);
933 vfio_disable_msi_common(vdev
);
935 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
936 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
939 static void vfio_disable_msi(VFIODevice
*vdev
)
943 vfio_disable_irqindex(vdev
, VFIO_PCI_MSI_IRQ_INDEX
);
945 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
946 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
952 if (vector
->virq
>= 0) {
953 kvm_irqchip_remove_irqfd_notifier(kvm_state
,
954 &vector
->interrupt
, vector
->virq
);
955 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
958 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
962 event_notifier_cleanup(&vector
->interrupt
);
965 vfio_disable_msi_common(vdev
);
967 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
968 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
971 static void vfio_update_msi(VFIODevice
*vdev
)
975 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
976 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
979 if (!vector
->use
|| vector
->virq
< 0) {
983 msg
= msi_get_message(&vdev
->pdev
, i
);
985 if (msg
.address
!= vector
->msg
.address
||
986 msg
.data
!= vector
->msg
.data
) {
988 DPRINTF("%s(%04x:%02x:%02x.%x) MSI vector %d changed\n",
989 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
990 vdev
->host
.slot
, vdev
->host
.function
, i
);
992 kvm_irqchip_update_msi_route(kvm_state
, vector
->virq
, msg
);
999 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
1001 static void vfio_bar_write(void *opaque
, hwaddr addr
,
1002 uint64_t data
, unsigned size
)
1004 VFIOBAR
*bar
= opaque
;
1017 buf
.word
= cpu_to_le16(data
);
1020 buf
.dword
= cpu_to_le32(data
);
1023 hw_error("vfio: unsupported write size, %d bytes\n", size
);
1027 if (pwrite(bar
->fd
, &buf
, size
, bar
->fd_offset
+ addr
) != size
) {
1028 error_report("%s(,0x%"HWADDR_PRIx
", 0x%"PRIx64
", %d) failed: %m",
1029 __func__
, addr
, data
, size
);
1034 VFIODevice
*vdev
= container_of(bar
, VFIODevice
, bars
[bar
->nr
]);
1036 DPRINTF("%s(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", 0x%"PRIx64
1037 ", %d)\n", __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
1038 vdev
->host
.slot
, vdev
->host
.function
, bar
->nr
, addr
,
1044 * A read or write to a BAR always signals an INTx EOI. This will
1045 * do nothing if not pending (including not in INTx mode). We assume
1046 * that a BAR access is in response to an interrupt and that BAR
1047 * accesses will service the interrupt. Unfortunately, we don't know
1048 * which access will service the interrupt, so we're potentially
1049 * getting quite a few host interrupts per guest interrupt.
1051 vfio_eoi(container_of(bar
, VFIODevice
, bars
[bar
->nr
]));
1054 static uint64_t vfio_bar_read(void *opaque
,
1055 hwaddr addr
, unsigned size
)
1057 VFIOBAR
*bar
= opaque
;
1066 if (pread(bar
->fd
, &buf
, size
, bar
->fd_offset
+ addr
) != size
) {
1067 error_report("%s(,0x%"HWADDR_PRIx
", %d) failed: %m",
1068 __func__
, addr
, size
);
1069 return (uint64_t)-1;
1077 data
= le16_to_cpu(buf
.word
);
1080 data
= le32_to_cpu(buf
.dword
);
1083 hw_error("vfio: unsupported read size, %d bytes\n", size
);
1089 VFIODevice
*vdev
= container_of(bar
, VFIODevice
, bars
[bar
->nr
]);
1091 DPRINTF("%s(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
1092 ", %d) = 0x%"PRIx64
"\n", __func__
, vdev
->host
.domain
,
1093 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
1094 bar
->nr
, addr
, size
, data
);
1098 /* Same as write above */
1099 vfio_eoi(container_of(bar
, VFIODevice
, bars
[bar
->nr
]));
1104 static const MemoryRegionOps vfio_bar_ops
= {
1105 .read
= vfio_bar_read
,
1106 .write
= vfio_bar_write
,
1107 .endianness
= DEVICE_LITTLE_ENDIAN
,
1110 static void vfio_pci_load_rom(VFIODevice
*vdev
)
1112 struct vfio_region_info reg_info
= {
1113 .argsz
= sizeof(reg_info
),
1114 .index
= VFIO_PCI_ROM_REGION_INDEX
1120 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
)) {
1121 error_report("vfio: Error getting ROM info: %m");
1125 DPRINTF("Device %04x:%02x:%02x.%x ROM:\n", vdev
->host
.domain
,
1126 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
1127 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
1128 (unsigned long)reg_info
.size
, (unsigned long)reg_info
.offset
,
1129 (unsigned long)reg_info
.flags
);
1131 vdev
->rom_size
= size
= reg_info
.size
;
1132 vdev
->rom_offset
= reg_info
.offset
;
1134 if (!vdev
->rom_size
) {
1135 vdev
->rom_read_failed
= true;
1136 error_report("vfio-pci: Cannot read device rom at "
1137 "%04x:%02x:%02x.%x\n",
1138 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1139 vdev
->host
.function
);
1140 error_printf("Device option ROM contents are probably invalid "
1141 "(check dmesg).\nSkip option ROM probe with rombar=0, "
1142 "or load from file with romfile=\n");
1146 vdev
->rom
= g_malloc(size
);
1147 memset(vdev
->rom
, 0xff, size
);
1150 bytes
= pread(vdev
->fd
, vdev
->rom
+ off
, size
, vdev
->rom_offset
+ off
);
1153 } else if (bytes
> 0) {
1157 if (errno
== EINTR
|| errno
== EAGAIN
) {
1160 error_report("vfio: Error reading device ROM: %m");
1166 static uint64_t vfio_rom_read(void *opaque
, hwaddr addr
, unsigned size
)
1168 VFIODevice
*vdev
= opaque
;
1169 uint64_t val
= ((uint64_t)1 << (size
* 8)) - 1;
1171 /* Load the ROM lazily when the guest tries to read it */
1172 if (unlikely(!vdev
->rom
)) {
1173 vfio_pci_load_rom(vdev
);
1174 if (unlikely(!vdev
->rom
&& !vdev
->rom_read_failed
)) {
1175 vfio_pci_load_rom(vdev
);
1179 memcpy(&val
, vdev
->rom
+ addr
,
1180 (addr
< vdev
->rom_size
) ? MIN(size
, vdev
->rom_size
- addr
) : 0);
1182 DPRINTF("%s(%04x:%02x:%02x.%x, 0x%"HWADDR_PRIx
", 0x%x) = 0x%"PRIx64
"\n",
1183 __func__
, vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1184 vdev
->host
.function
, addr
, size
, val
);
1189 static void vfio_rom_write(void *opaque
, hwaddr addr
,
1190 uint64_t data
, unsigned size
)
1194 static const MemoryRegionOps vfio_rom_ops
= {
1195 .read
= vfio_rom_read
,
1196 .write
= vfio_rom_write
,
1197 .endianness
= DEVICE_LITTLE_ENDIAN
,
1200 static void vfio_pci_size_rom(VFIODevice
*vdev
)
1202 uint32_t orig
, size
= cpu_to_le32((uint32_t)PCI_ROM_ADDRESS_MASK
);
1203 off_t offset
= vdev
->config_offset
+ PCI_ROM_ADDRESS
;
1206 if (vdev
->pdev
.romfile
|| !vdev
->pdev
.rom_bar
) {
1211 * Use the same size ROM BAR as the physical device. The contents
1212 * will get filled in later when the guest tries to read it.
1214 if (pread(vdev
->fd
, &orig
, 4, offset
) != 4 ||
1215 pwrite(vdev
->fd
, &size
, 4, offset
) != 4 ||
1216 pread(vdev
->fd
, &size
, 4, offset
) != 4 ||
1217 pwrite(vdev
->fd
, &orig
, 4, offset
) != 4) {
1218 error_report("%s(%04x:%02x:%02x.%x) failed: %m",
1219 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
1220 vdev
->host
.slot
, vdev
->host
.function
);
1224 size
= ~(le32_to_cpu(size
) & PCI_ROM_ADDRESS_MASK
) + 1;
1230 DPRINTF("%04x:%02x:%02x.%x ROM size 0x%x\n", vdev
->host
.domain
,
1231 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
, size
);
1233 snprintf(name
, sizeof(name
), "vfio[%04x:%02x:%02x.%x].rom",
1234 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1235 vdev
->host
.function
);
1237 memory_region_init_io(&vdev
->pdev
.rom
, OBJECT(vdev
),
1238 &vfio_rom_ops
, vdev
, name
, size
);
1240 pci_register_bar(&vdev
->pdev
, PCI_ROM_SLOT
,
1241 PCI_BASE_ADDRESS_SPACE_MEMORY
, &vdev
->pdev
.rom
);
1243 vdev
->pdev
.has_rom
= true;
1244 vdev
->rom_read_failed
= false;
1247 static void vfio_vga_write(void *opaque
, hwaddr addr
,
1248 uint64_t data
, unsigned size
)
1250 VFIOVGARegion
*region
= opaque
;
1251 VFIOVGA
*vga
= container_of(region
, VFIOVGA
, region
[region
->nr
]);
1258 off_t offset
= vga
->fd_offset
+ region
->offset
+ addr
;
1265 buf
.word
= cpu_to_le16(data
);
1268 buf
.dword
= cpu_to_le32(data
);
1271 hw_error("vfio: unsupported write size, %d bytes\n", size
);
1275 if (pwrite(vga
->fd
, &buf
, size
, offset
) != size
) {
1276 error_report("%s(,0x%"HWADDR_PRIx
", 0x%"PRIx64
", %d) failed: %m",
1277 __func__
, region
->offset
+ addr
, data
, size
);
1280 DPRINTF("%s(0x%"HWADDR_PRIx
", 0x%"PRIx64
", %d)\n",
1281 __func__
, region
->offset
+ addr
, data
, size
);
1284 static uint64_t vfio_vga_read(void *opaque
, hwaddr addr
, unsigned size
)
1286 VFIOVGARegion
*region
= opaque
;
1287 VFIOVGA
*vga
= container_of(region
, VFIOVGA
, region
[region
->nr
]);
1295 off_t offset
= vga
->fd_offset
+ region
->offset
+ addr
;
1297 if (pread(vga
->fd
, &buf
, size
, offset
) != size
) {
1298 error_report("%s(,0x%"HWADDR_PRIx
", %d) failed: %m",
1299 __func__
, region
->offset
+ addr
, size
);
1300 return (uint64_t)-1;
1308 data
= le16_to_cpu(buf
.word
);
1311 data
= le32_to_cpu(buf
.dword
);
1314 hw_error("vfio: unsupported read size, %d bytes\n", size
);
1318 DPRINTF("%s(0x%"HWADDR_PRIx
", %d) = 0x%"PRIx64
"\n",
1319 __func__
, region
->offset
+ addr
, size
, data
);
1324 static const MemoryRegionOps vfio_vga_ops
= {
1325 .read
= vfio_vga_read
,
1326 .write
= vfio_vga_write
,
1327 .endianness
= DEVICE_LITTLE_ENDIAN
,
1331 * Device specific quirks
1334 /* Is range1 fully contained within range2? */
1335 static bool vfio_range_contained(uint64_t first1
, uint64_t len1
,
1336 uint64_t first2
, uint64_t len2
) {
1337 return (first1
>= first2
&& first1
+ len1
<= first2
+ len2
);
1340 static bool vfio_flags_enabled(uint8_t flags
, uint8_t mask
)
1342 return (mask
&& (flags
& mask
) == mask
);
1345 static uint64_t vfio_generic_window_quirk_read(void *opaque
,
1346 hwaddr addr
, unsigned size
)
1348 VFIOQuirk
*quirk
= opaque
;
1349 VFIODevice
*vdev
= quirk
->vdev
;
1352 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.read_flags
) &&
1353 ranges_overlap(addr
, size
,
1354 quirk
->data
.data_offset
, quirk
->data
.data_size
)) {
1355 hwaddr offset
= addr
- quirk
->data
.data_offset
;
1357 if (!vfio_range_contained(addr
, size
, quirk
->data
.data_offset
,
1358 quirk
->data
.data_size
)) {
1359 hw_error("%s: window data read not fully contained: %s\n",
1360 __func__
, memory_region_name(&quirk
->mem
));
1363 data
= vfio_pci_read_config(&vdev
->pdev
,
1364 quirk
->data
.address_val
+ offset
, size
);
1366 DPRINTF("%s read(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", %d) = 0x%"
1367 PRIx64
"\n", memory_region_name(&quirk
->mem
), vdev
->host
.domain
,
1368 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
1369 quirk
->data
.bar
, addr
, size
, data
);
1371 data
= vfio_bar_read(&vdev
->bars
[quirk
->data
.bar
],
1372 addr
+ quirk
->data
.base_offset
, size
);
1378 static void vfio_generic_window_quirk_write(void *opaque
, hwaddr addr
,
1379 uint64_t data
, unsigned size
)
1381 VFIOQuirk
*quirk
= opaque
;
1382 VFIODevice
*vdev
= quirk
->vdev
;
1384 if (ranges_overlap(addr
, size
,
1385 quirk
->data
.address_offset
, quirk
->data
.address_size
)) {
1387 if (addr
!= quirk
->data
.address_offset
) {
1388 hw_error("%s: offset write into address window: %s\n",
1389 __func__
, memory_region_name(&quirk
->mem
));
1392 if ((data
& ~quirk
->data
.address_mask
) == quirk
->data
.address_match
) {
1393 quirk
->data
.flags
|= quirk
->data
.write_flags
|
1394 quirk
->data
.read_flags
;
1395 quirk
->data
.address_val
= data
& quirk
->data
.address_mask
;
1397 quirk
->data
.flags
&= ~(quirk
->data
.write_flags
|
1398 quirk
->data
.read_flags
);
1402 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.write_flags
) &&
1403 ranges_overlap(addr
, size
,
1404 quirk
->data
.data_offset
, quirk
->data
.data_size
)) {
1405 hwaddr offset
= addr
- quirk
->data
.data_offset
;
1407 if (!vfio_range_contained(addr
, size
, quirk
->data
.data_offset
,
1408 quirk
->data
.data_size
)) {
1409 hw_error("%s: window data write not fully contained: %s\n",
1410 __func__
, memory_region_name(&quirk
->mem
));
1413 vfio_pci_write_config(&vdev
->pdev
,
1414 quirk
->data
.address_val
+ offset
, data
, size
);
1415 DPRINTF("%s write(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", 0x%"
1416 PRIx64
", %d)\n", memory_region_name(&quirk
->mem
),
1417 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1418 vdev
->host
.function
, quirk
->data
.bar
, addr
, data
, size
);
1422 vfio_bar_write(&vdev
->bars
[quirk
->data
.bar
],
1423 addr
+ quirk
->data
.base_offset
, data
, size
);
1426 static const MemoryRegionOps vfio_generic_window_quirk
= {
1427 .read
= vfio_generic_window_quirk_read
,
1428 .write
= vfio_generic_window_quirk_write
,
1429 .endianness
= DEVICE_LITTLE_ENDIAN
,
1432 static uint64_t vfio_generic_quirk_read(void *opaque
,
1433 hwaddr addr
, unsigned size
)
1435 VFIOQuirk
*quirk
= opaque
;
1436 VFIODevice
*vdev
= quirk
->vdev
;
1437 hwaddr base
= quirk
->data
.address_match
& TARGET_PAGE_MASK
;
1438 hwaddr offset
= quirk
->data
.address_match
& ~TARGET_PAGE_MASK
;
1441 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.read_flags
) &&
1442 ranges_overlap(addr
, size
, offset
, quirk
->data
.address_mask
+ 1)) {
1443 if (!vfio_range_contained(addr
, size
, offset
,
1444 quirk
->data
.address_mask
+ 1)) {
1445 hw_error("%s: read not fully contained: %s\n",
1446 __func__
, memory_region_name(&quirk
->mem
));
1449 data
= vfio_pci_read_config(&vdev
->pdev
, addr
- offset
, size
);
1451 DPRINTF("%s read(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", %d) = 0x%"
1452 PRIx64
"\n", memory_region_name(&quirk
->mem
), vdev
->host
.domain
,
1453 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
1454 quirk
->data
.bar
, addr
+ base
, size
, data
);
1456 data
= vfio_bar_read(&vdev
->bars
[quirk
->data
.bar
], addr
+ base
, size
);
1462 static void vfio_generic_quirk_write(void *opaque
, hwaddr addr
,
1463 uint64_t data
, unsigned size
)
1465 VFIOQuirk
*quirk
= opaque
;
1466 VFIODevice
*vdev
= quirk
->vdev
;
1467 hwaddr base
= quirk
->data
.address_match
& TARGET_PAGE_MASK
;
1468 hwaddr offset
= quirk
->data
.address_match
& ~TARGET_PAGE_MASK
;
1470 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.write_flags
) &&
1471 ranges_overlap(addr
, size
, offset
, quirk
->data
.address_mask
+ 1)) {
1472 if (!vfio_range_contained(addr
, size
, offset
,
1473 quirk
->data
.address_mask
+ 1)) {
1474 hw_error("%s: write not fully contained: %s\n",
1475 __func__
, memory_region_name(&quirk
->mem
));
1478 vfio_pci_write_config(&vdev
->pdev
, addr
- offset
, data
, size
);
1480 DPRINTF("%s write(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", 0x%"
1481 PRIx64
", %d)\n", memory_region_name(&quirk
->mem
),
1482 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1483 vdev
->host
.function
, quirk
->data
.bar
, addr
+ base
, data
, size
);
1485 vfio_bar_write(&vdev
->bars
[quirk
->data
.bar
], addr
+ base
, data
, size
);
1489 static const MemoryRegionOps vfio_generic_quirk
= {
1490 .read
= vfio_generic_quirk_read
,
1491 .write
= vfio_generic_quirk_write
,
1492 .endianness
= DEVICE_LITTLE_ENDIAN
,
1495 #define PCI_VENDOR_ID_ATI 0x1002
1498 * Radeon HD cards (HD5450 & HD7850) report the upper byte of the I/O port BAR
1499 * through VGA register 0x3c3. On newer cards, the I/O port BAR is always
1500 * BAR4 (older cards like the X550 used BAR1, but we don't care to support
1501 * those). Note that on bare metal, a read of 0x3c3 doesn't always return the
1502 * I/O port BAR address. Originally this was coded to return the virtual BAR
1503 * address only if the physical register read returns the actual BAR address,
1504 * but users have reported greater success if we return the virtual address
1507 static uint64_t vfio_ati_3c3_quirk_read(void *opaque
,
1508 hwaddr addr
, unsigned size
)
1510 VFIOQuirk
*quirk
= opaque
;
1511 VFIODevice
*vdev
= quirk
->vdev
;
1512 uint64_t data
= vfio_pci_read_config(&vdev
->pdev
,
1513 PCI_BASE_ADDRESS_0
+ (4 * 4) + 1,
1515 DPRINTF("%s(0x3c3, 1) = 0x%"PRIx64
"\n", __func__
, data
);
1520 static const MemoryRegionOps vfio_ati_3c3_quirk
= {
1521 .read
= vfio_ati_3c3_quirk_read
,
1522 .endianness
= DEVICE_LITTLE_ENDIAN
,
1525 static void vfio_vga_probe_ati_3c3_quirk(VFIODevice
*vdev
)
1527 PCIDevice
*pdev
= &vdev
->pdev
;
1530 if (pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_ATI
) {
1535 * As long as the BAR is >= 256 bytes it will be aligned such that the
1536 * lower byte is always zero. Filter out anything else, if it exists.
1538 if (!vdev
->bars
[4].ioport
|| vdev
->bars
[4].size
< 256) {
1542 quirk
= g_malloc0(sizeof(*quirk
));
1545 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_ati_3c3_quirk
, quirk
,
1546 "vfio-ati-3c3-quirk", 1);
1547 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
1548 3 /* offset 3 bytes from 0x3c0 */, &quirk
->mem
);
1550 QLIST_INSERT_HEAD(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
,
1553 DPRINTF("Enabled ATI/AMD quirk 0x3c3 BAR4for device %04x:%02x:%02x.%x\n",
1554 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1555 vdev
->host
.function
);
1559 * Newer ATI/AMD devices, including HD5450 and HD7850, have a window to PCI
1560 * config space through MMIO BAR2 at offset 0x4000. Nothing seems to access
1561 * the MMIO space directly, but a window to this space is provided through
1562 * I/O port BAR4. Offset 0x0 is the address register and offset 0x4 is the
1563 * data register. When the address is programmed to a range of 0x4000-0x4fff
1564 * PCI configuration space is available. Experimentation seems to indicate
1565 * that only read-only access is provided, but we drop writes when the window
1566 * is enabled to config space nonetheless.
1568 static void vfio_probe_ati_bar4_window_quirk(VFIODevice
*vdev
, int nr
)
1570 PCIDevice
*pdev
= &vdev
->pdev
;
1573 if (!vdev
->has_vga
|| nr
!= 4 ||
1574 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_ATI
) {
1578 quirk
= g_malloc0(sizeof(*quirk
));
1580 quirk
->data
.address_size
= 4;
1581 quirk
->data
.data_offset
= 4;
1582 quirk
->data
.data_size
= 4;
1583 quirk
->data
.address_match
= 0x4000;
1584 quirk
->data
.address_mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
1585 quirk
->data
.bar
= nr
;
1586 quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1588 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
),
1589 &vfio_generic_window_quirk
, quirk
,
1590 "vfio-ati-bar4-window-quirk", 8);
1591 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1592 quirk
->data
.base_offset
, &quirk
->mem
, 1);
1594 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1596 DPRINTF("Enabled ATI/AMD BAR4 window quirk for device %04x:%02x:%02x.%x\n",
1597 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1598 vdev
->host
.function
);
1602 * Trap the BAR2 MMIO window to config space as well.
1604 static void vfio_probe_ati_bar2_4000_quirk(VFIODevice
*vdev
, int nr
)
1606 PCIDevice
*pdev
= &vdev
->pdev
;
1609 /* Only enable on newer devices where BAR2 is 64bit */
1610 if (!vdev
->has_vga
|| nr
!= 2 || !vdev
->bars
[2].mem64
||
1611 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_ATI
) {
1615 quirk
= g_malloc0(sizeof(*quirk
));
1617 quirk
->data
.flags
= quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1618 quirk
->data
.address_match
= 0x4000;
1619 quirk
->data
.address_mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
1620 quirk
->data
.bar
= nr
;
1622 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_generic_quirk
, quirk
,
1623 "vfio-ati-bar2-4000-quirk",
1624 TARGET_PAGE_ALIGN(quirk
->data
.address_mask
+ 1));
1625 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1626 quirk
->data
.address_match
& TARGET_PAGE_MASK
,
1629 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1631 DPRINTF("Enabled ATI/AMD BAR2 0x4000 quirk for device %04x:%02x:%02x.%x\n",
1632 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1633 vdev
->host
.function
);
1637 * Older ATI/AMD cards like the X550 have a similar window to that above.
1638 * I/O port BAR1 provides a window to a mirror of PCI config space located
1639 * in BAR2 at offset 0xf00. We don't care to support such older cards, but
1640 * note it for future reference.
1643 #define PCI_VENDOR_ID_NVIDIA 0x10de
1646 * Nvidia has several different methods to get to config space, the
1647 * nouveu project has several of these documented here:
1648 * https://github.com/pathscale/envytools/tree/master/hwdocs
1650 * The first quirk is actually not documented in envytools and is found
1651 * on 10de:01d1 (NVIDIA Corporation G72 [GeForce 7300 LE]). This is an
1652 * NV46 chipset. The backdoor uses the legacy VGA I/O ports to access
1653 * the mirror of PCI config space found at BAR0 offset 0x1800. The access
1654 * sequence first writes 0x338 to I/O port 0x3d4. The target offset is
1655 * then written to 0x3d0. Finally 0x538 is written for a read and 0x738
1656 * is written for a write to 0x3d4. The BAR0 offset is then accessible
1657 * through 0x3d0. This quirk doesn't seem to be necessary on newer cards
1658 * that use the I/O port BAR5 window but it doesn't hurt to leave it.
1668 static uint64_t vfio_nvidia_3d0_quirk_read(void *opaque
,
1669 hwaddr addr
, unsigned size
)
1671 VFIOQuirk
*quirk
= opaque
;
1672 VFIODevice
*vdev
= quirk
->vdev
;
1673 PCIDevice
*pdev
= &vdev
->pdev
;
1674 uint64_t data
= vfio_vga_read(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
1675 addr
+ quirk
->data
.base_offset
, size
);
1677 if (quirk
->data
.flags
== NV_3D0_READ
&& addr
== quirk
->data
.data_offset
) {
1678 data
= vfio_pci_read_config(pdev
, quirk
->data
.address_val
, size
);
1679 DPRINTF("%s(0x3d0, %d) = 0x%"PRIx64
"\n", __func__
, size
, data
);
1682 quirk
->data
.flags
= NV_3D0_NONE
;
1687 static void vfio_nvidia_3d0_quirk_write(void *opaque
, hwaddr addr
,
1688 uint64_t data
, unsigned size
)
1690 VFIOQuirk
*quirk
= opaque
;
1691 VFIODevice
*vdev
= quirk
->vdev
;
1692 PCIDevice
*pdev
= &vdev
->pdev
;
1694 switch (quirk
->data
.flags
) {
1696 if (addr
== quirk
->data
.address_offset
&& data
== 0x338) {
1697 quirk
->data
.flags
= NV_3D0_SELECT
;
1701 quirk
->data
.flags
= NV_3D0_NONE
;
1702 if (addr
== quirk
->data
.data_offset
&&
1703 (data
& ~quirk
->data
.address_mask
) == quirk
->data
.address_match
) {
1704 quirk
->data
.flags
= NV_3D0_WINDOW
;
1705 quirk
->data
.address_val
= data
& quirk
->data
.address_mask
;
1709 quirk
->data
.flags
= NV_3D0_NONE
;
1710 if (addr
== quirk
->data
.address_offset
) {
1711 if (data
== 0x538) {
1712 quirk
->data
.flags
= NV_3D0_READ
;
1713 } else if (data
== 0x738) {
1714 quirk
->data
.flags
= NV_3D0_WRITE
;
1719 quirk
->data
.flags
= NV_3D0_NONE
;
1720 if (addr
== quirk
->data
.data_offset
) {
1721 vfio_pci_write_config(pdev
, quirk
->data
.address_val
, data
, size
);
1722 DPRINTF("%s(0x3d0, 0x%"PRIx64
", %d)\n", __func__
, data
, size
);
1728 vfio_vga_write(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
1729 addr
+ quirk
->data
.base_offset
, data
, size
);
1732 static const MemoryRegionOps vfio_nvidia_3d0_quirk
= {
1733 .read
= vfio_nvidia_3d0_quirk_read
,
1734 .write
= vfio_nvidia_3d0_quirk_write
,
1735 .endianness
= DEVICE_LITTLE_ENDIAN
,
1738 static void vfio_vga_probe_nvidia_3d0_quirk(VFIODevice
*vdev
)
1740 PCIDevice
*pdev
= &vdev
->pdev
;
1743 if (pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
||
1744 !vdev
->bars
[1].size
) {
1748 quirk
= g_malloc0(sizeof(*quirk
));
1750 quirk
->data
.base_offset
= 0x10;
1751 quirk
->data
.address_offset
= 4;
1752 quirk
->data
.address_size
= 2;
1753 quirk
->data
.address_match
= 0x1800;
1754 quirk
->data
.address_mask
= PCI_CONFIG_SPACE_SIZE
- 1;
1755 quirk
->data
.data_offset
= 0;
1756 quirk
->data
.data_size
= 4;
1758 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_nvidia_3d0_quirk
,
1759 quirk
, "vfio-nvidia-3d0-quirk", 6);
1760 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
1761 quirk
->data
.base_offset
, &quirk
->mem
);
1763 QLIST_INSERT_HEAD(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
,
1766 DPRINTF("Enabled NVIDIA VGA 0x3d0 quirk for device %04x:%02x:%02x.%x\n",
1767 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1768 vdev
->host
.function
);
1772 * The second quirk is documented in envytools. The I/O port BAR5 is just
1773 * a set of address/data ports to the MMIO BARs. The BAR we care about is
1774 * again BAR0. This backdoor is apparently a bit newer than the one above
1775 * so we need to not only trap 256 bytes @0x1800, but all of PCI config
1776 * space, including extended space is available at the 4k @0x88000.
1779 NV_BAR5_ADDRESS
= 0x1,
1780 NV_BAR5_ENABLE
= 0x2,
1781 NV_BAR5_MASTER
= 0x4,
1782 NV_BAR5_VALID
= 0x7,
1785 static void vfio_nvidia_bar5_window_quirk_write(void *opaque
, hwaddr addr
,
1786 uint64_t data
, unsigned size
)
1788 VFIOQuirk
*quirk
= opaque
;
1793 quirk
->data
.flags
|= NV_BAR5_MASTER
;
1795 quirk
->data
.flags
&= ~NV_BAR5_MASTER
;
1800 quirk
->data
.flags
|= NV_BAR5_ENABLE
;
1802 quirk
->data
.flags
&= ~NV_BAR5_ENABLE
;
1806 if (quirk
->data
.flags
& NV_BAR5_MASTER
) {
1807 if ((data
& ~0xfff) == 0x88000) {
1808 quirk
->data
.flags
|= NV_BAR5_ADDRESS
;
1809 quirk
->data
.address_val
= data
& 0xfff;
1810 } else if ((data
& ~0xff) == 0x1800) {
1811 quirk
->data
.flags
|= NV_BAR5_ADDRESS
;
1812 quirk
->data
.address_val
= data
& 0xff;
1814 quirk
->data
.flags
&= ~NV_BAR5_ADDRESS
;
1820 vfio_generic_window_quirk_write(opaque
, addr
, data
, size
);
1823 static const MemoryRegionOps vfio_nvidia_bar5_window_quirk
= {
1824 .read
= vfio_generic_window_quirk_read
,
1825 .write
= vfio_nvidia_bar5_window_quirk_write
,
1826 .valid
.min_access_size
= 4,
1827 .endianness
= DEVICE_LITTLE_ENDIAN
,
1830 static void vfio_probe_nvidia_bar5_window_quirk(VFIODevice
*vdev
, int nr
)
1832 PCIDevice
*pdev
= &vdev
->pdev
;
1835 if (!vdev
->has_vga
|| nr
!= 5 ||
1836 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
) {
1840 quirk
= g_malloc0(sizeof(*quirk
));
1842 quirk
->data
.read_flags
= quirk
->data
.write_flags
= NV_BAR5_VALID
;
1843 quirk
->data
.address_offset
= 0x8;
1844 quirk
->data
.address_size
= 0; /* actually 4, but avoids generic code */
1845 quirk
->data
.data_offset
= 0xc;
1846 quirk
->data
.data_size
= 4;
1847 quirk
->data
.bar
= nr
;
1849 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
),
1850 &vfio_nvidia_bar5_window_quirk
, quirk
,
1851 "vfio-nvidia-bar5-window-quirk", 16);
1852 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
, 0, &quirk
->mem
, 1);
1854 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1856 DPRINTF("Enabled NVIDIA BAR5 window quirk for device %04x:%02x:%02x.%x\n",
1857 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1858 vdev
->host
.function
);
1861 static void vfio_nvidia_88000_quirk_write(void *opaque
, hwaddr addr
,
1862 uint64_t data
, unsigned size
)
1864 VFIOQuirk
*quirk
= opaque
;
1865 VFIODevice
*vdev
= quirk
->vdev
;
1866 PCIDevice
*pdev
= &vdev
->pdev
;
1867 hwaddr base
= quirk
->data
.address_match
& TARGET_PAGE_MASK
;
1869 vfio_generic_quirk_write(opaque
, addr
, data
, size
);
1872 * Nvidia seems to acknowledge MSI interrupts by writing 0xff to the
1873 * MSI capability ID register. Both the ID and next register are
1874 * read-only, so we allow writes covering either of those to real hw.
1875 * NB - only fixed for the 0x88000 MMIO window.
1877 if ((pdev
->cap_present
& QEMU_PCI_CAP_MSI
) &&
1878 vfio_range_contained(addr
, size
, pdev
->msi_cap
, PCI_MSI_FLAGS
)) {
1879 vfio_bar_write(&vdev
->bars
[quirk
->data
.bar
], addr
+ base
, data
, size
);
1883 static const MemoryRegionOps vfio_nvidia_88000_quirk
= {
1884 .read
= vfio_generic_quirk_read
,
1885 .write
= vfio_nvidia_88000_quirk_write
,
1886 .endianness
= DEVICE_LITTLE_ENDIAN
,
1890 * Finally, BAR0 itself. We want to redirect any accesses to either
1891 * 0x1800 or 0x88000 through the PCI config space access functions.
1893 * NB - quirk at a page granularity or else they don't seem to work when
1896 * Here's offset 0x88000...
1898 static void vfio_probe_nvidia_bar0_88000_quirk(VFIODevice
*vdev
, int nr
)
1900 PCIDevice
*pdev
= &vdev
->pdev
;
1903 if (!vdev
->has_vga
|| nr
!= 0 ||
1904 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
) {
1908 quirk
= g_malloc0(sizeof(*quirk
));
1910 quirk
->data
.flags
= quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1911 quirk
->data
.address_match
= 0x88000;
1912 quirk
->data
.address_mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
1913 quirk
->data
.bar
= nr
;
1915 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_nvidia_88000_quirk
,
1916 quirk
, "vfio-nvidia-bar0-88000-quirk",
1917 TARGET_PAGE_ALIGN(quirk
->data
.address_mask
+ 1));
1918 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1919 quirk
->data
.address_match
& TARGET_PAGE_MASK
,
1922 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1924 DPRINTF("Enabled NVIDIA BAR0 0x88000 quirk for device %04x:%02x:%02x.%x\n",
1925 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1926 vdev
->host
.function
);
1930 * And here's the same for BAR0 offset 0x1800...
1932 static void vfio_probe_nvidia_bar0_1800_quirk(VFIODevice
*vdev
, int nr
)
1934 PCIDevice
*pdev
= &vdev
->pdev
;
1937 if (!vdev
->has_vga
|| nr
!= 0 ||
1938 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
) {
1942 /* Log the chipset ID */
1943 DPRINTF("Nvidia NV%02x\n",
1944 (unsigned int)(vfio_bar_read(&vdev
->bars
[0], 0, 4) >> 20) & 0xff);
1946 quirk
= g_malloc0(sizeof(*quirk
));
1948 quirk
->data
.flags
= quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1949 quirk
->data
.address_match
= 0x1800;
1950 quirk
->data
.address_mask
= PCI_CONFIG_SPACE_SIZE
- 1;
1951 quirk
->data
.bar
= nr
;
1953 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_generic_quirk
, quirk
,
1954 "vfio-nvidia-bar0-1800-quirk",
1955 TARGET_PAGE_ALIGN(quirk
->data
.address_mask
+ 1));
1956 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1957 quirk
->data
.address_match
& TARGET_PAGE_MASK
,
1960 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1962 DPRINTF("Enabled NVIDIA BAR0 0x1800 quirk for device %04x:%02x:%02x.%x\n",
1963 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1964 vdev
->host
.function
);
1968 * TODO - Some Nvidia devices provide config access to their companion HDA
1969 * device and even to their parent bridge via these config space mirrors.
1970 * Add quirks for those regions.
1974 * Common quirk probe entry points.
1976 static void vfio_vga_quirk_setup(VFIODevice
*vdev
)
1978 vfio_vga_probe_ati_3c3_quirk(vdev
);
1979 vfio_vga_probe_nvidia_3d0_quirk(vdev
);
1982 static void vfio_vga_quirk_teardown(VFIODevice
*vdev
)
1986 for (i
= 0; i
< ARRAY_SIZE(vdev
->vga
.region
); i
++) {
1987 while (!QLIST_EMPTY(&vdev
->vga
.region
[i
].quirks
)) {
1988 VFIOQuirk
*quirk
= QLIST_FIRST(&vdev
->vga
.region
[i
].quirks
);
1989 memory_region_del_subregion(&vdev
->vga
.region
[i
].mem
, &quirk
->mem
);
1990 memory_region_destroy(&quirk
->mem
);
1991 QLIST_REMOVE(quirk
, next
);
1997 static void vfio_bar_quirk_setup(VFIODevice
*vdev
, int nr
)
1999 vfio_probe_ati_bar4_window_quirk(vdev
, nr
);
2000 vfio_probe_ati_bar2_4000_quirk(vdev
, nr
);
2001 vfio_probe_nvidia_bar5_window_quirk(vdev
, nr
);
2002 vfio_probe_nvidia_bar0_88000_quirk(vdev
, nr
);
2003 vfio_probe_nvidia_bar0_1800_quirk(vdev
, nr
);
2006 static void vfio_bar_quirk_teardown(VFIODevice
*vdev
, int nr
)
2008 VFIOBAR
*bar
= &vdev
->bars
[nr
];
2010 while (!QLIST_EMPTY(&bar
->quirks
)) {
2011 VFIOQuirk
*quirk
= QLIST_FIRST(&bar
->quirks
);
2012 memory_region_del_subregion(&bar
->mem
, &quirk
->mem
);
2013 memory_region_destroy(&quirk
->mem
);
2014 QLIST_REMOVE(quirk
, next
);
2022 static uint32_t vfio_pci_read_config(PCIDevice
*pdev
, uint32_t addr
, int len
)
2024 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
2025 uint32_t emu_bits
= 0, emu_val
= 0, phys_val
= 0, val
;
2027 memcpy(&emu_bits
, vdev
->emulated_config_bits
+ addr
, len
);
2028 emu_bits
= le32_to_cpu(emu_bits
);
2031 emu_val
= pci_default_read_config(pdev
, addr
, len
);
2034 if (~emu_bits
& (0xffffffffU
>> (32 - len
* 8))) {
2037 ret
= pread(vdev
->fd
, &phys_val
, len
, vdev
->config_offset
+ addr
);
2039 error_report("%s(%04x:%02x:%02x.%x, 0x%x, 0x%x) failed: %m",
2040 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
2041 vdev
->host
.slot
, vdev
->host
.function
, addr
, len
);
2044 phys_val
= le32_to_cpu(phys_val
);
2047 val
= (emu_val
& emu_bits
) | (phys_val
& ~emu_bits
);
2049 DPRINTF("%s(%04x:%02x:%02x.%x, @0x%x, len=0x%x) %x\n", __func__
,
2050 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2051 vdev
->host
.function
, addr
, len
, val
);
2056 static void vfio_pci_write_config(PCIDevice
*pdev
, uint32_t addr
,
2057 uint32_t val
, int len
)
2059 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
2060 uint32_t val_le
= cpu_to_le32(val
);
2062 DPRINTF("%s(%04x:%02x:%02x.%x, @0x%x, 0x%x, len=0x%x)\n", __func__
,
2063 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2064 vdev
->host
.function
, addr
, val
, len
);
2066 /* Write everything to VFIO, let it filter out what we can't write */
2067 if (pwrite(vdev
->fd
, &val_le
, len
, vdev
->config_offset
+ addr
) != len
) {
2068 error_report("%s(%04x:%02x:%02x.%x, 0x%x, 0x%x, 0x%x) failed: %m",
2069 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
2070 vdev
->host
.slot
, vdev
->host
.function
, addr
, val
, len
);
2073 /* MSI/MSI-X Enabling/Disabling */
2074 if (pdev
->cap_present
& QEMU_PCI_CAP_MSI
&&
2075 ranges_overlap(addr
, len
, pdev
->msi_cap
, vdev
->msi_cap_size
)) {
2076 int is_enabled
, was_enabled
= msi_enabled(pdev
);
2078 pci_default_write_config(pdev
, addr
, val
, len
);
2080 is_enabled
= msi_enabled(pdev
);
2084 vfio_enable_msi(vdev
);
2088 vfio_disable_msi(vdev
);
2090 vfio_update_msi(vdev
);
2093 } else if (pdev
->cap_present
& QEMU_PCI_CAP_MSIX
&&
2094 ranges_overlap(addr
, len
, pdev
->msix_cap
, MSIX_CAP_LENGTH
)) {
2095 int is_enabled
, was_enabled
= msix_enabled(pdev
);
2097 pci_default_write_config(pdev
, addr
, val
, len
);
2099 is_enabled
= msix_enabled(pdev
);
2101 if (!was_enabled
&& is_enabled
) {
2102 vfio_enable_msix(vdev
);
2103 } else if (was_enabled
&& !is_enabled
) {
2104 vfio_disable_msix(vdev
);
2107 /* Write everything to QEMU to keep emulated bits correct */
2108 pci_default_write_config(pdev
, addr
, val
, len
);
2113 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
2115 static int vfio_dma_unmap(VFIOContainer
*container
,
2116 hwaddr iova
, ram_addr_t size
)
2118 struct vfio_iommu_type1_dma_unmap unmap
= {
2119 .argsz
= sizeof(unmap
),
2125 if (ioctl(container
->fd
, VFIO_IOMMU_UNMAP_DMA
, &unmap
)) {
2126 DPRINTF("VFIO_UNMAP_DMA: %d\n", -errno
);
2133 static int vfio_dma_map(VFIOContainer
*container
, hwaddr iova
,
2134 ram_addr_t size
, void *vaddr
, bool readonly
)
2136 struct vfio_iommu_type1_dma_map map
= {
2137 .argsz
= sizeof(map
),
2138 .flags
= VFIO_DMA_MAP_FLAG_READ
,
2139 .vaddr
= (__u64
)(uintptr_t)vaddr
,
2145 map
.flags
|= VFIO_DMA_MAP_FLAG_WRITE
;
2149 * Try the mapping, if it fails with EBUSY, unmap the region and try
2150 * again. This shouldn't be necessary, but we sometimes see it in
2151 * the the VGA ROM space.
2153 if (ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0 ||
2154 (errno
== EBUSY
&& vfio_dma_unmap(container
, iova
, size
) == 0 &&
2155 ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0)) {
2159 DPRINTF("VFIO_MAP_DMA: %d\n", -errno
);
2163 static bool vfio_listener_skipped_section(MemoryRegionSection
*section
)
2165 return !memory_region_is_ram(section
->mr
) ||
2167 * Sizing an enabled 64-bit BAR can cause spurious mappings to
2168 * addresses in the upper part of the 64-bit address space. These
2169 * are never accessed by the CPU and beyond the address width of
2170 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
2172 section
->offset_within_address_space
& (1ULL << 63);
2175 static void vfio_listener_region_add(MemoryListener
*listener
,
2176 MemoryRegionSection
*section
)
2178 VFIOContainer
*container
= container_of(listener
, VFIOContainer
,
2179 iommu_data
.type1
.listener
);
2184 assert(!memory_region_is_iommu(section
->mr
));
2186 if (vfio_listener_skipped_section(section
)) {
2187 DPRINTF("SKIPPING region_add %"HWADDR_PRIx
" - %"PRIx64
"\n",
2188 section
->offset_within_address_space
,
2189 section
->offset_within_address_space
+
2190 int128_get64(int128_sub(section
->size
, int128_one())));
2194 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
2195 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
2196 error_report("%s received unaligned region", __func__
);
2200 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
2201 end
= (section
->offset_within_address_space
+ int128_get64(section
->size
)) &
2208 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
2209 section
->offset_within_region
+
2210 (iova
- section
->offset_within_address_space
);
2212 DPRINTF("region_add %"HWADDR_PRIx
" - %"HWADDR_PRIx
" [%p]\n",
2213 iova
, end
- 1, vaddr
);
2215 memory_region_ref(section
->mr
);
2216 ret
= vfio_dma_map(container
, iova
, end
- iova
, vaddr
, section
->readonly
);
2218 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
2219 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
2220 container
, iova
, end
- iova
, vaddr
, ret
);
2223 * On the initfn path, store the first error in the container so we
2224 * can gracefully fail. Runtime, there's not much we can do other
2225 * than throw a hardware error.
2227 if (!container
->iommu_data
.type1
.initialized
) {
2228 if (!container
->iommu_data
.type1
.error
) {
2229 container
->iommu_data
.type1
.error
= ret
;
2232 hw_error("vfio: DMA mapping failed, unable to continue\n");
2237 static void vfio_listener_region_del(MemoryListener
*listener
,
2238 MemoryRegionSection
*section
)
2240 VFIOContainer
*container
= container_of(listener
, VFIOContainer
,
2241 iommu_data
.type1
.listener
);
2245 if (vfio_listener_skipped_section(section
)) {
2246 DPRINTF("SKIPPING region_del %"HWADDR_PRIx
" - %"PRIx64
"\n",
2247 section
->offset_within_address_space
,
2248 section
->offset_within_address_space
+
2249 int128_get64(int128_sub(section
->size
, int128_one())));
2253 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
2254 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
2255 error_report("%s received unaligned region", __func__
);
2259 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
2260 end
= (section
->offset_within_address_space
+ int128_get64(section
->size
)) &
2267 DPRINTF("region_del %"HWADDR_PRIx
" - %"HWADDR_PRIx
"\n",
2270 ret
= vfio_dma_unmap(container
, iova
, end
- iova
);
2271 memory_region_unref(section
->mr
);
2273 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
2274 "0x%"HWADDR_PRIx
") = %d (%m)",
2275 container
, iova
, end
- iova
, ret
);
2279 static MemoryListener vfio_memory_listener
= {
2280 .region_add
= vfio_listener_region_add
,
2281 .region_del
= vfio_listener_region_del
,
2284 static void vfio_listener_release(VFIOContainer
*container
)
2286 memory_listener_unregister(&container
->iommu_data
.type1
.listener
);
2292 static void vfio_disable_interrupts(VFIODevice
*vdev
)
2294 switch (vdev
->interrupt
) {
2296 vfio_disable_intx(vdev
);
2299 vfio_disable_msi(vdev
);
2302 vfio_disable_msix(vdev
);
2307 static int vfio_setup_msi(VFIODevice
*vdev
, int pos
)
2310 bool msi_64bit
, msi_maskbit
;
2313 if (pread(vdev
->fd
, &ctrl
, sizeof(ctrl
),
2314 vdev
->config_offset
+ pos
+ PCI_CAP_FLAGS
) != sizeof(ctrl
)) {
2317 ctrl
= le16_to_cpu(ctrl
);
2319 msi_64bit
= !!(ctrl
& PCI_MSI_FLAGS_64BIT
);
2320 msi_maskbit
= !!(ctrl
& PCI_MSI_FLAGS_MASKBIT
);
2321 entries
= 1 << ((ctrl
& PCI_MSI_FLAGS_QMASK
) >> 1);
2323 DPRINTF("%04x:%02x:%02x.%x PCI MSI CAP @0x%x\n", vdev
->host
.domain
,
2324 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
, pos
);
2326 ret
= msi_init(&vdev
->pdev
, pos
, entries
, msi_64bit
, msi_maskbit
);
2328 if (ret
== -ENOTSUP
) {
2331 error_report("vfio: msi_init failed");
2334 vdev
->msi_cap_size
= 0xa + (msi_maskbit
? 0xa : 0) + (msi_64bit
? 0x4 : 0);
2340 * We don't have any control over how pci_add_capability() inserts
2341 * capabilities into the chain. In order to setup MSI-X we need a
2342 * MemoryRegion for the BAR. In order to setup the BAR and not
2343 * attempt to mmap the MSI-X table area, which VFIO won't allow, we
2344 * need to first look for where the MSI-X table lives. So we
2345 * unfortunately split MSI-X setup across two functions.
2347 static int vfio_early_setup_msix(VFIODevice
*vdev
)
2351 uint32_t table
, pba
;
2353 pos
= pci_find_capability(&vdev
->pdev
, PCI_CAP_ID_MSIX
);
2358 if (pread(vdev
->fd
, &ctrl
, sizeof(ctrl
),
2359 vdev
->config_offset
+ pos
+ PCI_CAP_FLAGS
) != sizeof(ctrl
)) {
2363 if (pread(vdev
->fd
, &table
, sizeof(table
),
2364 vdev
->config_offset
+ pos
+ PCI_MSIX_TABLE
) != sizeof(table
)) {
2368 if (pread(vdev
->fd
, &pba
, sizeof(pba
),
2369 vdev
->config_offset
+ pos
+ PCI_MSIX_PBA
) != sizeof(pba
)) {
2373 ctrl
= le16_to_cpu(ctrl
);
2374 table
= le32_to_cpu(table
);
2375 pba
= le32_to_cpu(pba
);
2377 vdev
->msix
= g_malloc0(sizeof(*(vdev
->msix
)));
2378 vdev
->msix
->table_bar
= table
& PCI_MSIX_FLAGS_BIRMASK
;
2379 vdev
->msix
->table_offset
= table
& ~PCI_MSIX_FLAGS_BIRMASK
;
2380 vdev
->msix
->pba_bar
= pba
& PCI_MSIX_FLAGS_BIRMASK
;
2381 vdev
->msix
->pba_offset
= pba
& ~PCI_MSIX_FLAGS_BIRMASK
;
2382 vdev
->msix
->entries
= (ctrl
& PCI_MSIX_FLAGS_QSIZE
) + 1;
2384 DPRINTF("%04x:%02x:%02x.%x "
2385 "PCI MSI-X CAP @0x%x, BAR %d, offset 0x%x, entries %d\n",
2386 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2387 vdev
->host
.function
, pos
, vdev
->msix
->table_bar
,
2388 vdev
->msix
->table_offset
, vdev
->msix
->entries
);
2393 static int vfio_setup_msix(VFIODevice
*vdev
, int pos
)
2397 ret
= msix_init(&vdev
->pdev
, vdev
->msix
->entries
,
2398 &vdev
->bars
[vdev
->msix
->table_bar
].mem
,
2399 vdev
->msix
->table_bar
, vdev
->msix
->table_offset
,
2400 &vdev
->bars
[vdev
->msix
->pba_bar
].mem
,
2401 vdev
->msix
->pba_bar
, vdev
->msix
->pba_offset
, pos
);
2403 if (ret
== -ENOTSUP
) {
2406 error_report("vfio: msix_init failed");
2413 static void vfio_teardown_msi(VFIODevice
*vdev
)
2415 msi_uninit(&vdev
->pdev
);
2418 msix_uninit(&vdev
->pdev
, &vdev
->bars
[vdev
->msix
->table_bar
].mem
,
2419 &vdev
->bars
[vdev
->msix
->pba_bar
].mem
);
2426 static void vfio_mmap_set_enabled(VFIODevice
*vdev
, bool enabled
)
2430 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
2431 VFIOBAR
*bar
= &vdev
->bars
[i
];
2437 memory_region_set_enabled(&bar
->mmap_mem
, enabled
);
2438 if (vdev
->msix
&& vdev
->msix
->table_bar
== i
) {
2439 memory_region_set_enabled(&vdev
->msix
->mmap_mem
, enabled
);
2444 static void vfio_unmap_bar(VFIODevice
*vdev
, int nr
)
2446 VFIOBAR
*bar
= &vdev
->bars
[nr
];
2452 vfio_bar_quirk_teardown(vdev
, nr
);
2454 memory_region_del_subregion(&bar
->mem
, &bar
->mmap_mem
);
2455 munmap(bar
->mmap
, memory_region_size(&bar
->mmap_mem
));
2456 memory_region_destroy(&bar
->mmap_mem
);
2458 if (vdev
->msix
&& vdev
->msix
->table_bar
== nr
) {
2459 memory_region_del_subregion(&bar
->mem
, &vdev
->msix
->mmap_mem
);
2460 munmap(vdev
->msix
->mmap
, memory_region_size(&vdev
->msix
->mmap_mem
));
2461 memory_region_destroy(&vdev
->msix
->mmap_mem
);
2464 memory_region_destroy(&bar
->mem
);
2467 static int vfio_mmap_bar(VFIODevice
*vdev
, VFIOBAR
*bar
,
2468 MemoryRegion
*mem
, MemoryRegion
*submem
,
2469 void **map
, size_t size
, off_t offset
,
2474 if (VFIO_ALLOW_MMAP
&& size
&& bar
->flags
& VFIO_REGION_INFO_FLAG_MMAP
) {
2477 if (bar
->flags
& VFIO_REGION_INFO_FLAG_READ
) {
2481 if (bar
->flags
& VFIO_REGION_INFO_FLAG_WRITE
) {
2485 *map
= mmap(NULL
, size
, prot
, MAP_SHARED
,
2486 bar
->fd
, bar
->fd_offset
+ offset
);
2487 if (*map
== MAP_FAILED
) {
2493 memory_region_init_ram_ptr(submem
, OBJECT(vdev
), name
, size
, *map
);
2496 /* Create a zero sized sub-region to make cleanup easy. */
2497 memory_region_init(submem
, OBJECT(vdev
), name
, 0);
2500 memory_region_add_subregion(mem
, offset
, submem
);
2505 static void vfio_map_bar(VFIODevice
*vdev
, int nr
)
2507 VFIOBAR
*bar
= &vdev
->bars
[nr
];
2508 unsigned size
= bar
->size
;
2514 /* Skip both unimplemented BARs and the upper half of 64bit BARS. */
2519 snprintf(name
, sizeof(name
), "VFIO %04x:%02x:%02x.%x BAR %d",
2520 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2521 vdev
->host
.function
, nr
);
2523 /* Determine what type of BAR this is for registration */
2524 ret
= pread(vdev
->fd
, &pci_bar
, sizeof(pci_bar
),
2525 vdev
->config_offset
+ PCI_BASE_ADDRESS_0
+ (4 * nr
));
2526 if (ret
!= sizeof(pci_bar
)) {
2527 error_report("vfio: Failed to read BAR %d (%m)", nr
);
2531 pci_bar
= le32_to_cpu(pci_bar
);
2532 bar
->ioport
= (pci_bar
& PCI_BASE_ADDRESS_SPACE_IO
);
2533 bar
->mem64
= bar
->ioport
? 0 : (pci_bar
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
2534 type
= pci_bar
& (bar
->ioport
? ~PCI_BASE_ADDRESS_IO_MASK
:
2535 ~PCI_BASE_ADDRESS_MEM_MASK
);
2537 /* A "slow" read/write mapping underlies all BARs */
2538 memory_region_init_io(&bar
->mem
, OBJECT(vdev
), &vfio_bar_ops
,
2540 pci_register_bar(&vdev
->pdev
, nr
, type
, &bar
->mem
);
2543 * We can't mmap areas overlapping the MSIX vector table, so we
2544 * potentially insert a direct-mapped subregion before and after it.
2546 if (vdev
->msix
&& vdev
->msix
->table_bar
== nr
) {
2547 size
= vdev
->msix
->table_offset
& qemu_host_page_mask
;
2550 strncat(name
, " mmap", sizeof(name
) - strlen(name
) - 1);
2551 if (vfio_mmap_bar(vdev
, bar
, &bar
->mem
,
2552 &bar
->mmap_mem
, &bar
->mmap
, size
, 0, name
)) {
2553 error_report("%s unsupported. Performance may be slow", name
);
2556 if (vdev
->msix
&& vdev
->msix
->table_bar
== nr
) {
2559 start
= HOST_PAGE_ALIGN(vdev
->msix
->table_offset
+
2560 (vdev
->msix
->entries
* PCI_MSIX_ENTRY_SIZE
));
2562 size
= start
< bar
->size
? bar
->size
- start
: 0;
2563 strncat(name
, " msix-hi", sizeof(name
) - strlen(name
) - 1);
2564 /* VFIOMSIXInfo contains another MemoryRegion for this mapping */
2565 if (vfio_mmap_bar(vdev
, bar
, &bar
->mem
, &vdev
->msix
->mmap_mem
,
2566 &vdev
->msix
->mmap
, size
, start
, name
)) {
2567 error_report("%s unsupported. Performance may be slow", name
);
2571 vfio_bar_quirk_setup(vdev
, nr
);
2574 static void vfio_map_bars(VFIODevice
*vdev
)
2578 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
2579 vfio_map_bar(vdev
, i
);
2582 if (vdev
->has_vga
) {
2583 memory_region_init_io(&vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].mem
,
2584 OBJECT(vdev
), &vfio_vga_ops
,
2585 &vdev
->vga
.region
[QEMU_PCI_VGA_MEM
],
2586 "vfio-vga-mmio@0xa0000",
2587 QEMU_PCI_VGA_MEM_SIZE
);
2588 memory_region_init_io(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].mem
,
2589 OBJECT(vdev
), &vfio_vga_ops
,
2590 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
],
2591 "vfio-vga-io@0x3b0",
2592 QEMU_PCI_VGA_IO_LO_SIZE
);
2593 memory_region_init_io(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
2594 OBJECT(vdev
), &vfio_vga_ops
,
2595 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
2596 "vfio-vga-io@0x3c0",
2597 QEMU_PCI_VGA_IO_HI_SIZE
);
2599 pci_register_vga(&vdev
->pdev
, &vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].mem
,
2600 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].mem
,
2601 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
);
2602 vfio_vga_quirk_setup(vdev
);
2606 static void vfio_unmap_bars(VFIODevice
*vdev
)
2610 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
2611 vfio_unmap_bar(vdev
, i
);
2614 if (vdev
->has_vga
) {
2615 vfio_vga_quirk_teardown(vdev
);
2616 pci_unregister_vga(&vdev
->pdev
);
2617 memory_region_destroy(&vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].mem
);
2618 memory_region_destroy(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].mem
);
2619 memory_region_destroy(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
);
2626 static uint8_t vfio_std_cap_max_size(PCIDevice
*pdev
, uint8_t pos
)
2628 uint8_t tmp
, next
= 0xff;
2630 for (tmp
= pdev
->config
[PCI_CAPABILITY_LIST
]; tmp
;
2631 tmp
= pdev
->config
[tmp
+ 1]) {
2632 if (tmp
> pos
&& tmp
< next
) {
2640 static void vfio_set_word_bits(uint8_t *buf
, uint16_t val
, uint16_t mask
)
2642 pci_set_word(buf
, (pci_get_word(buf
) & ~mask
) | val
);
2645 static void vfio_add_emulated_word(VFIODevice
*vdev
, int pos
,
2646 uint16_t val
, uint16_t mask
)
2648 vfio_set_word_bits(vdev
->pdev
.config
+ pos
, val
, mask
);
2649 vfio_set_word_bits(vdev
->pdev
.wmask
+ pos
, ~mask
, mask
);
2650 vfio_set_word_bits(vdev
->emulated_config_bits
+ pos
, mask
, mask
);
2653 static void vfio_set_long_bits(uint8_t *buf
, uint32_t val
, uint32_t mask
)
2655 pci_set_long(buf
, (pci_get_long(buf
) & ~mask
) | val
);
2658 static void vfio_add_emulated_long(VFIODevice
*vdev
, int pos
,
2659 uint32_t val
, uint32_t mask
)
2661 vfio_set_long_bits(vdev
->pdev
.config
+ pos
, val
, mask
);
2662 vfio_set_long_bits(vdev
->pdev
.wmask
+ pos
, ~mask
, mask
);
2663 vfio_set_long_bits(vdev
->emulated_config_bits
+ pos
, mask
, mask
);
2666 static int vfio_setup_pcie_cap(VFIODevice
*vdev
, int pos
, uint8_t size
)
2671 flags
= pci_get_word(vdev
->pdev
.config
+ pos
+ PCI_CAP_FLAGS
);
2672 type
= (flags
& PCI_EXP_FLAGS_TYPE
) >> 4;
2674 if (type
!= PCI_EXP_TYPE_ENDPOINT
&&
2675 type
!= PCI_EXP_TYPE_LEG_END
&&
2676 type
!= PCI_EXP_TYPE_RC_END
) {
2678 error_report("vfio: Assignment of PCIe type 0x%x "
2679 "devices is not currently supported", type
);
2683 if (!pci_bus_is_express(vdev
->pdev
.bus
)) {
2685 * Use express capability as-is on PCI bus. It doesn't make much
2686 * sense to even expose, but some drivers (ex. tg3) depend on it
2687 * and guests don't seem to be particular about it. We'll need
2688 * to revist this or force express devices to express buses if we
2689 * ever expose an IOMMU to the guest.
2691 } else if (pci_bus_is_root(vdev
->pdev
.bus
)) {
2693 * On a Root Complex bus Endpoints become Root Complex Integrated
2694 * Endpoints, which changes the type and clears the LNK & LNK2 fields.
2696 if (type
== PCI_EXP_TYPE_ENDPOINT
) {
2697 vfio_add_emulated_word(vdev
, pos
+ PCI_CAP_FLAGS
,
2698 PCI_EXP_TYPE_RC_END
<< 4,
2699 PCI_EXP_FLAGS_TYPE
);
2701 /* Link Capabilities, Status, and Control goes away */
2702 if (size
> PCI_EXP_LNKCTL
) {
2703 vfio_add_emulated_long(vdev
, pos
+ PCI_EXP_LNKCAP
, 0, ~0);
2704 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKCTL
, 0, ~0);
2705 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKSTA
, 0, ~0);
2707 #ifndef PCI_EXP_LNKCAP2
2708 #define PCI_EXP_LNKCAP2 44
2710 #ifndef PCI_EXP_LNKSTA2
2711 #define PCI_EXP_LNKSTA2 50
2713 /* Link 2 Capabilities, Status, and Control goes away */
2714 if (size
> PCI_EXP_LNKCAP2
) {
2715 vfio_add_emulated_long(vdev
, pos
+ PCI_EXP_LNKCAP2
, 0, ~0);
2716 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKCTL2
, 0, ~0);
2717 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKSTA2
, 0, ~0);
2721 } else if (type
== PCI_EXP_TYPE_LEG_END
) {
2723 * Legacy endpoints don't belong on the root complex. Windows
2724 * seems to be happier with devices if we skip the capability.
2731 * Convert Root Complex Integrated Endpoints to regular endpoints.
2732 * These devices don't support LNK/LNK2 capabilities, so make them up.
2734 if (type
== PCI_EXP_TYPE_RC_END
) {
2735 vfio_add_emulated_word(vdev
, pos
+ PCI_CAP_FLAGS
,
2736 PCI_EXP_TYPE_ENDPOINT
<< 4,
2737 PCI_EXP_FLAGS_TYPE
);
2738 vfio_add_emulated_long(vdev
, pos
+ PCI_EXP_LNKCAP
,
2739 PCI_EXP_LNK_MLW_1
| PCI_EXP_LNK_LS_25
, ~0);
2740 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKCTL
, 0, ~0);
2743 /* Mark the Link Status bits as emulated to allow virtual negotiation */
2744 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKSTA
,
2745 pci_get_word(vdev
->pdev
.config
+ pos
+
2747 PCI_EXP_LNKCAP_MLW
| PCI_EXP_LNKCAP_SLS
);
2750 pos
= pci_add_capability(&vdev
->pdev
, PCI_CAP_ID_EXP
, pos
, size
);
2752 vdev
->pdev
.exp
.exp_cap
= pos
;
2758 static void vfio_check_pcie_flr(VFIODevice
*vdev
, uint8_t pos
)
2760 uint32_t cap
= pci_get_long(vdev
->pdev
.config
+ pos
+ PCI_EXP_DEVCAP
);
2762 if (cap
& PCI_EXP_DEVCAP_FLR
) {
2763 DPRINTF("%04x:%02x:%02x.%x Supports FLR via PCIe cap\n",
2764 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2765 vdev
->host
.function
);
2766 vdev
->has_flr
= true;
2770 static void vfio_check_pm_reset(VFIODevice
*vdev
, uint8_t pos
)
2772 uint16_t csr
= pci_get_word(vdev
->pdev
.config
+ pos
+ PCI_PM_CTRL
);
2774 if (!(csr
& PCI_PM_CTRL_NO_SOFT_RESET
)) {
2775 DPRINTF("%04x:%02x:%02x.%x Supports PM reset\n",
2776 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2777 vdev
->host
.function
);
2778 vdev
->has_pm_reset
= true;
2782 static void vfio_check_af_flr(VFIODevice
*vdev
, uint8_t pos
)
2784 uint8_t cap
= pci_get_byte(vdev
->pdev
.config
+ pos
+ PCI_AF_CAP
);
2786 if ((cap
& PCI_AF_CAP_TP
) && (cap
& PCI_AF_CAP_FLR
)) {
2787 DPRINTF("%04x:%02x:%02x.%x Supports FLR via AF cap\n",
2788 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2789 vdev
->host
.function
);
2790 vdev
->has_flr
= true;
2794 static int vfio_add_std_cap(VFIODevice
*vdev
, uint8_t pos
)
2796 PCIDevice
*pdev
= &vdev
->pdev
;
2797 uint8_t cap_id
, next
, size
;
2800 cap_id
= pdev
->config
[pos
];
2801 next
= pdev
->config
[pos
+ 1];
2804 * If it becomes important to configure capabilities to their actual
2805 * size, use this as the default when it's something we don't recognize.
2806 * Since QEMU doesn't actually handle many of the config accesses,
2807 * exact size doesn't seem worthwhile.
2809 size
= vfio_std_cap_max_size(pdev
, pos
);
2812 * pci_add_capability always inserts the new capability at the head
2813 * of the chain. Therefore to end up with a chain that matches the
2814 * physical device, we insert from the end by making this recursive.
2815 * This is also why we pre-caclulate size above as cached config space
2816 * will be changed as we unwind the stack.
2819 ret
= vfio_add_std_cap(vdev
, next
);
2824 /* Begin the rebuild, use QEMU emulated list bits */
2825 pdev
->config
[PCI_CAPABILITY_LIST
] = 0;
2826 vdev
->emulated_config_bits
[PCI_CAPABILITY_LIST
] = 0xff;
2827 vdev
->emulated_config_bits
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
2830 /* Use emulated next pointer to allow dropping caps */
2831 pci_set_byte(vdev
->emulated_config_bits
+ pos
+ 1, 0xff);
2834 case PCI_CAP_ID_MSI
:
2835 ret
= vfio_setup_msi(vdev
, pos
);
2837 case PCI_CAP_ID_EXP
:
2838 vfio_check_pcie_flr(vdev
, pos
);
2839 ret
= vfio_setup_pcie_cap(vdev
, pos
, size
);
2841 case PCI_CAP_ID_MSIX
:
2842 ret
= vfio_setup_msix(vdev
, pos
);
2845 vfio_check_pm_reset(vdev
, pos
);
2847 ret
= pci_add_capability(pdev
, cap_id
, pos
, size
);
2850 vfio_check_af_flr(vdev
, pos
);
2851 ret
= pci_add_capability(pdev
, cap_id
, pos
, size
);
2854 ret
= pci_add_capability(pdev
, cap_id
, pos
, size
);
2859 error_report("vfio: %04x:%02x:%02x.%x Error adding PCI capability "
2860 "0x%x[0x%x]@0x%x: %d", vdev
->host
.domain
,
2861 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
2862 cap_id
, size
, pos
, ret
);
2869 static int vfio_add_capabilities(VFIODevice
*vdev
)
2871 PCIDevice
*pdev
= &vdev
->pdev
;
2873 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
) ||
2874 !pdev
->config
[PCI_CAPABILITY_LIST
]) {
2875 return 0; /* Nothing to add */
2878 return vfio_add_std_cap(vdev
, pdev
->config
[PCI_CAPABILITY_LIST
]);
2881 static void vfio_pci_pre_reset(VFIODevice
*vdev
)
2883 PCIDevice
*pdev
= &vdev
->pdev
;
2886 vfio_disable_interrupts(vdev
);
2888 /* Make sure the device is in D0 */
2893 pmcsr
= vfio_pci_read_config(pdev
, vdev
->pm_cap
+ PCI_PM_CTRL
, 2);
2894 state
= pmcsr
& PCI_PM_CTRL_STATE_MASK
;
2896 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
2897 vfio_pci_write_config(pdev
, vdev
->pm_cap
+ PCI_PM_CTRL
, pmcsr
, 2);
2898 /* vfio handles the necessary delay here */
2899 pmcsr
= vfio_pci_read_config(pdev
, vdev
->pm_cap
+ PCI_PM_CTRL
, 2);
2900 state
= pmcsr
& PCI_PM_CTRL_STATE_MASK
;
2902 error_report("vfio: Unable to power on device, stuck in D%d\n",
2909 * Stop any ongoing DMA by disconecting I/O, MMIO, and bus master.
2910 * Also put INTx Disable in known state.
2912 cmd
= vfio_pci_read_config(pdev
, PCI_COMMAND
, 2);
2913 cmd
&= ~(PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
|
2914 PCI_COMMAND_INTX_DISABLE
);
2915 vfio_pci_write_config(pdev
, PCI_COMMAND
, cmd
, 2);
2918 static void vfio_pci_post_reset(VFIODevice
*vdev
)
2920 vfio_enable_intx(vdev
);
2923 static bool vfio_pci_host_match(PCIHostDeviceAddress
*host1
,
2924 PCIHostDeviceAddress
*host2
)
2926 return (host1
->domain
== host2
->domain
&& host1
->bus
== host2
->bus
&&
2927 host1
->slot
== host2
->slot
&& host1
->function
== host2
->function
);
2930 static int vfio_pci_hot_reset(VFIODevice
*vdev
, bool single
)
2933 struct vfio_pci_hot_reset_info
*info
;
2934 struct vfio_pci_dependent_device
*devices
;
2935 struct vfio_pci_hot_reset
*reset
;
2940 DPRINTF("%s(%04x:%02x:%02x.%x) %s\n", __func__
, vdev
->host
.domain
,
2941 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
2942 single
? "one" : "multi");
2944 vfio_pci_pre_reset(vdev
);
2945 vdev
->needs_reset
= false;
2947 info
= g_malloc0(sizeof(*info
));
2948 info
->argsz
= sizeof(*info
);
2950 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
, info
);
2951 if (ret
&& errno
!= ENOSPC
) {
2953 if (!vdev
->has_pm_reset
) {
2954 error_report("vfio: Cannot reset device %04x:%02x:%02x.%x, "
2955 "no available reset mechanism.", vdev
->host
.domain
,
2956 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
2961 count
= info
->count
;
2962 info
= g_realloc(info
, sizeof(*info
) + (count
* sizeof(*devices
)));
2963 info
->argsz
= sizeof(*info
) + (count
* sizeof(*devices
));
2964 devices
= &info
->devices
[0];
2966 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
, info
);
2969 error_report("vfio: hot reset info failed: %m");
2973 DPRINTF("%04x:%02x:%02x.%x: hot reset dependent devices:\n",
2974 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2975 vdev
->host
.function
);
2977 /* Verify that we have all the groups required */
2978 for (i
= 0; i
< info
->count
; i
++) {
2979 PCIHostDeviceAddress host
;
2982 host
.domain
= devices
[i
].segment
;
2983 host
.bus
= devices
[i
].bus
;
2984 host
.slot
= PCI_SLOT(devices
[i
].devfn
);
2985 host
.function
= PCI_FUNC(devices
[i
].devfn
);
2987 DPRINTF("\t%04x:%02x:%02x.%x group %d\n", host
.domain
,
2988 host
.bus
, host
.slot
, host
.function
, devices
[i
].group_id
);
2990 if (vfio_pci_host_match(&host
, &vdev
->host
)) {
2994 QLIST_FOREACH(group
, &group_list
, next
) {
2995 if (group
->groupid
== devices
[i
].group_id
) {
3001 if (!vdev
->has_pm_reset
) {
3002 error_report("vfio: Cannot reset device %04x:%02x:%02x.%x, "
3003 "depends on group %d which is not owned.",
3004 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3005 vdev
->host
.function
, devices
[i
].group_id
);
3011 /* Prep dependent devices for reset and clear our marker. */
3012 QLIST_FOREACH(tmp
, &group
->device_list
, next
) {
3013 if (vfio_pci_host_match(&host
, &tmp
->host
)) {
3015 DPRINTF("vfio: found another in-use device "
3016 "%04x:%02x:%02x.%x\n", host
.domain
, host
.bus
,
3017 host
.slot
, host
.function
);
3021 vfio_pci_pre_reset(tmp
);
3022 tmp
->needs_reset
= false;
3029 if (!single
&& !multi
) {
3030 DPRINTF("vfio: No other in-use devices for multi hot reset\n");
3035 /* Determine how many group fds need to be passed */
3037 QLIST_FOREACH(group
, &group_list
, next
) {
3038 for (i
= 0; i
< info
->count
; i
++) {
3039 if (group
->groupid
== devices
[i
].group_id
) {
3046 reset
= g_malloc0(sizeof(*reset
) + (count
* sizeof(*fds
)));
3047 reset
->argsz
= sizeof(*reset
) + (count
* sizeof(*fds
));
3048 fds
= &reset
->group_fds
[0];
3050 /* Fill in group fds */
3051 QLIST_FOREACH(group
, &group_list
, next
) {
3052 for (i
= 0; i
< info
->count
; i
++) {
3053 if (group
->groupid
== devices
[i
].group_id
) {
3054 fds
[reset
->count
++] = group
->fd
;
3061 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_PCI_HOT_RESET
, reset
);
3064 DPRINTF("%04x:%02x:%02x.%x hot reset: %s\n", vdev
->host
.domain
,
3065 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
3066 ret
? "%m" : "Success");
3069 /* Re-enable INTx on affected devices */
3070 for (i
= 0; i
< info
->count
; i
++) {
3071 PCIHostDeviceAddress host
;
3074 host
.domain
= devices
[i
].segment
;
3075 host
.bus
= devices
[i
].bus
;
3076 host
.slot
= PCI_SLOT(devices
[i
].devfn
);
3077 host
.function
= PCI_FUNC(devices
[i
].devfn
);
3079 if (vfio_pci_host_match(&host
, &vdev
->host
)) {
3083 QLIST_FOREACH(group
, &group_list
, next
) {
3084 if (group
->groupid
== devices
[i
].group_id
) {
3093 QLIST_FOREACH(tmp
, &group
->device_list
, next
) {
3094 if (vfio_pci_host_match(&host
, &tmp
->host
)) {
3095 vfio_pci_post_reset(tmp
);
3101 vfio_pci_post_reset(vdev
);
3108 * We want to differentiate hot reset of mulitple in-use devices vs hot reset
3109 * of a single in-use device. VFIO_DEVICE_RESET will already handle the case
3110 * of doing hot resets when there is only a single device per bus. The in-use
3111 * here refers to how many VFIODevices are affected. A hot reset that affects
3112 * multiple devices, but only a single in-use device, means that we can call
3113 * it from our bus ->reset() callback since the extent is effectively a single
3114 * device. This allows us to make use of it in the hotplug path. When there
3115 * are multiple in-use devices, we can only trigger the hot reset during a
3116 * system reset and thus from our reset handler. We separate _one vs _multi
3117 * here so that we don't overlap and do a double reset on the system reset
3118 * path where both our reset handler and ->reset() callback are used. Calling
3119 * _one() will only do a hot reset for the one in-use devices case, calling
3120 * _multi() will do nothing if a _one() would have been sufficient.
3122 static int vfio_pci_hot_reset_one(VFIODevice
*vdev
)
3124 return vfio_pci_hot_reset(vdev
, true);
3127 static int vfio_pci_hot_reset_multi(VFIODevice
*vdev
)
3129 return vfio_pci_hot_reset(vdev
, false);
3132 static void vfio_pci_reset_handler(void *opaque
)
3137 QLIST_FOREACH(group
, &group_list
, next
) {
3138 QLIST_FOREACH(vdev
, &group
->device_list
, next
) {
3139 if (!vdev
->reset_works
|| (!vdev
->has_flr
&& vdev
->has_pm_reset
)) {
3140 vdev
->needs_reset
= true;
3145 QLIST_FOREACH(group
, &group_list
, next
) {
3146 QLIST_FOREACH(vdev
, &group
->device_list
, next
) {
3147 if (vdev
->needs_reset
) {
3148 vfio_pci_hot_reset_multi(vdev
);
3154 static void vfio_kvm_device_add_group(VFIOGroup
*group
)
3157 struct kvm_device_attr attr
= {
3158 .group
= KVM_DEV_VFIO_GROUP
,
3159 .attr
= KVM_DEV_VFIO_GROUP_ADD
,
3160 .addr
= (uint64_t)(unsigned long)&group
->fd
,
3163 if (!kvm_enabled()) {
3167 if (vfio_kvm_device_fd
< 0) {
3168 struct kvm_create_device cd
= {
3169 .type
= KVM_DEV_TYPE_VFIO
,
3172 if (kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
)) {
3173 DPRINTF("KVM_CREATE_DEVICE: %m\n");
3177 vfio_kvm_device_fd
= cd
.fd
;
3180 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
3181 error_report("Failed to add group %d to KVM VFIO device: %m",
3187 static void vfio_kvm_device_del_group(VFIOGroup
*group
)
3190 struct kvm_device_attr attr
= {
3191 .group
= KVM_DEV_VFIO_GROUP
,
3192 .attr
= KVM_DEV_VFIO_GROUP_DEL
,
3193 .addr
= (uint64_t)(unsigned long)&group
->fd
,
3196 if (vfio_kvm_device_fd
< 0) {
3200 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
3201 error_report("Failed to remove group %d to KVM VFIO device: %m",
3207 static int vfio_connect_container(VFIOGroup
*group
)
3209 VFIOContainer
*container
;
3212 if (group
->container
) {
3216 QLIST_FOREACH(container
, &container_list
, next
) {
3217 if (!ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &container
->fd
)) {
3218 group
->container
= container
;
3219 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
3224 fd
= qemu_open("/dev/vfio/vfio", O_RDWR
);
3226 error_report("vfio: failed to open /dev/vfio/vfio: %m");
3230 ret
= ioctl(fd
, VFIO_GET_API_VERSION
);
3231 if (ret
!= VFIO_API_VERSION
) {
3232 error_report("vfio: supported vfio version: %d, "
3233 "reported version: %d", VFIO_API_VERSION
, ret
);
3238 container
= g_malloc0(sizeof(*container
));
3241 if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1_IOMMU
)) {
3242 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
3244 error_report("vfio: failed to set group container: %m");
3250 ret
= ioctl(fd
, VFIO_SET_IOMMU
, VFIO_TYPE1_IOMMU
);
3252 error_report("vfio: failed to set iommu for container: %m");
3258 container
->iommu_data
.type1
.listener
= vfio_memory_listener
;
3259 container
->iommu_data
.release
= vfio_listener_release
;
3261 memory_listener_register(&container
->iommu_data
.type1
.listener
,
3262 &address_space_memory
);
3264 if (container
->iommu_data
.type1
.error
) {
3265 ret
= container
->iommu_data
.type1
.error
;
3266 vfio_listener_release(container
);
3269 error_report("vfio: memory listener initialization failed for container\n");
3273 container
->iommu_data
.type1
.initialized
= true;
3276 error_report("vfio: No available IOMMU models");
3282 QLIST_INIT(&container
->group_list
);
3283 QLIST_INSERT_HEAD(&container_list
, container
, next
);
3285 group
->container
= container
;
3286 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
3291 static void vfio_disconnect_container(VFIOGroup
*group
)
3293 VFIOContainer
*container
= group
->container
;
3295 if (ioctl(group
->fd
, VFIO_GROUP_UNSET_CONTAINER
, &container
->fd
)) {
3296 error_report("vfio: error disconnecting group %d from container",
3300 QLIST_REMOVE(group
, container_next
);
3301 group
->container
= NULL
;
3303 if (QLIST_EMPTY(&container
->group_list
)) {
3304 if (container
->iommu_data
.release
) {
3305 container
->iommu_data
.release(container
);
3307 QLIST_REMOVE(container
, next
);
3308 DPRINTF("vfio_disconnect_container: close container->fd\n");
3309 close(container
->fd
);
3314 static VFIOGroup
*vfio_get_group(int groupid
)
3318 struct vfio_group_status status
= { .argsz
= sizeof(status
) };
3320 QLIST_FOREACH(group
, &group_list
, next
) {
3321 if (group
->groupid
== groupid
) {
3326 group
= g_malloc0(sizeof(*group
));
3328 snprintf(path
, sizeof(path
), "/dev/vfio/%d", groupid
);
3329 group
->fd
= qemu_open(path
, O_RDWR
);
3330 if (group
->fd
< 0) {
3331 error_report("vfio: error opening %s: %m", path
);
3336 if (ioctl(group
->fd
, VFIO_GROUP_GET_STATUS
, &status
)) {
3337 error_report("vfio: error getting group status: %m");
3343 if (!(status
.flags
& VFIO_GROUP_FLAGS_VIABLE
)) {
3344 error_report("vfio: error, group %d is not viable, please ensure "
3345 "all devices within the iommu_group are bound to their "
3346 "vfio bus driver.", groupid
);
3352 group
->groupid
= groupid
;
3353 QLIST_INIT(&group
->device_list
);
3355 if (vfio_connect_container(group
)) {
3356 error_report("vfio: failed to setup container for group %d", groupid
);
3362 if (QLIST_EMPTY(&group_list
)) {
3363 qemu_register_reset(vfio_pci_reset_handler
, NULL
);
3366 QLIST_INSERT_HEAD(&group_list
, group
, next
);
3368 vfio_kvm_device_add_group(group
);
3373 static void vfio_put_group(VFIOGroup
*group
)
3375 if (!QLIST_EMPTY(&group
->device_list
)) {
3379 vfio_kvm_device_del_group(group
);
3380 vfio_disconnect_container(group
);
3381 QLIST_REMOVE(group
, next
);
3382 DPRINTF("vfio_put_group: close group->fd\n");
3386 if (QLIST_EMPTY(&group_list
)) {
3387 qemu_unregister_reset(vfio_pci_reset_handler
, NULL
);
3391 static int vfio_get_device(VFIOGroup
*group
, const char *name
, VFIODevice
*vdev
)
3393 struct vfio_device_info dev_info
= { .argsz
= sizeof(dev_info
) };
3394 struct vfio_region_info reg_info
= { .argsz
= sizeof(reg_info
) };
3395 struct vfio_irq_info irq_info
= { .argsz
= sizeof(irq_info
) };
3398 ret
= ioctl(group
->fd
, VFIO_GROUP_GET_DEVICE_FD
, name
);
3400 error_report("vfio: error getting device %s from group %d: %m",
3401 name
, group
->groupid
);
3402 error_printf("Verify all devices in group %d are bound to vfio-pci "
3403 "or pci-stub and not already in use\n", group
->groupid
);
3408 vdev
->group
= group
;
3409 QLIST_INSERT_HEAD(&group
->device_list
, vdev
, next
);
3411 /* Sanity check device */
3412 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_INFO
, &dev_info
);
3414 error_report("vfio: error getting device info: %m");
3418 DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name
,
3419 dev_info
.flags
, dev_info
.num_regions
, dev_info
.num_irqs
);
3421 if (!(dev_info
.flags
& VFIO_DEVICE_FLAGS_PCI
)) {
3422 error_report("vfio: Um, this isn't a PCI device");
3426 vdev
->reset_works
= !!(dev_info
.flags
& VFIO_DEVICE_FLAGS_RESET
);
3428 if (dev_info
.num_regions
< VFIO_PCI_CONFIG_REGION_INDEX
+ 1) {
3429 error_report("vfio: unexpected number of io regions %u",
3430 dev_info
.num_regions
);
3434 if (dev_info
.num_irqs
< VFIO_PCI_MSIX_IRQ_INDEX
+ 1) {
3435 error_report("vfio: unexpected number of irqs %u", dev_info
.num_irqs
);
3439 for (i
= VFIO_PCI_BAR0_REGION_INDEX
; i
< VFIO_PCI_ROM_REGION_INDEX
; i
++) {
3442 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
);
3444 error_report("vfio: Error getting region %d info: %m", i
);
3448 DPRINTF("Device %s region %d:\n", name
, i
);
3449 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
3450 (unsigned long)reg_info
.size
, (unsigned long)reg_info
.offset
,
3451 (unsigned long)reg_info
.flags
);
3453 vdev
->bars
[i
].flags
= reg_info
.flags
;
3454 vdev
->bars
[i
].size
= reg_info
.size
;
3455 vdev
->bars
[i
].fd_offset
= reg_info
.offset
;
3456 vdev
->bars
[i
].fd
= vdev
->fd
;
3457 vdev
->bars
[i
].nr
= i
;
3458 QLIST_INIT(&vdev
->bars
[i
].quirks
);
3461 reg_info
.index
= VFIO_PCI_CONFIG_REGION_INDEX
;
3463 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
);
3465 error_report("vfio: Error getting config info: %m");
3469 DPRINTF("Device %s config:\n", name
);
3470 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
3471 (unsigned long)reg_info
.size
, (unsigned long)reg_info
.offset
,
3472 (unsigned long)reg_info
.flags
);
3474 vdev
->config_size
= reg_info
.size
;
3475 if (vdev
->config_size
== PCI_CONFIG_SPACE_SIZE
) {
3476 vdev
->pdev
.cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
3478 vdev
->config_offset
= reg_info
.offset
;
3480 if ((vdev
->features
& VFIO_FEATURE_ENABLE_VGA
) &&
3481 dev_info
.num_regions
> VFIO_PCI_VGA_REGION_INDEX
) {
3482 struct vfio_region_info vga_info
= {
3483 .argsz
= sizeof(vga_info
),
3484 .index
= VFIO_PCI_VGA_REGION_INDEX
,
3487 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, &vga_info
);
3490 "vfio: Device does not support requested feature x-vga");
3494 if (!(vga_info
.flags
& VFIO_REGION_INFO_FLAG_READ
) ||
3495 !(vga_info
.flags
& VFIO_REGION_INFO_FLAG_WRITE
) ||
3496 vga_info
.size
< 0xbffff + 1) {
3497 error_report("vfio: Unexpected VGA info, flags 0x%lx, size 0x%lx",
3498 (unsigned long)vga_info
.flags
,
3499 (unsigned long)vga_info
.size
);
3503 vdev
->vga
.fd_offset
= vga_info
.offset
;
3504 vdev
->vga
.fd
= vdev
->fd
;
3506 vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].offset
= QEMU_PCI_VGA_MEM_BASE
;
3507 vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].nr
= QEMU_PCI_VGA_MEM
;
3508 QLIST_INIT(&vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].quirks
);
3510 vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].offset
= QEMU_PCI_VGA_IO_LO_BASE
;
3511 vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].nr
= QEMU_PCI_VGA_IO_LO
;
3512 QLIST_INIT(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].quirks
);
3514 vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].offset
= QEMU_PCI_VGA_IO_HI_BASE
;
3515 vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].nr
= QEMU_PCI_VGA_IO_HI
;
3516 QLIST_INIT(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
);
3518 vdev
->has_vga
= true;
3520 irq_info
.index
= VFIO_PCI_ERR_IRQ_INDEX
;
3522 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
, &irq_info
);
3524 /* This can fail for an old kernel or legacy PCI dev */
3525 DPRINTF("VFIO_DEVICE_GET_IRQ_INFO failure: %m\n");
3527 } else if (irq_info
.count
== 1) {
3528 vdev
->pci_aer
= true;
3530 error_report("vfio: %04x:%02x:%02x.%x "
3531 "Could not enable error recovery for the device",
3532 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3533 vdev
->host
.function
);
3538 QLIST_REMOVE(vdev
, next
);
3545 static void vfio_put_device(VFIODevice
*vdev
)
3547 QLIST_REMOVE(vdev
, next
);
3549 DPRINTF("vfio_put_device: close vdev->fd\n");
3557 static void vfio_err_notifier_handler(void *opaque
)
3559 VFIODevice
*vdev
= opaque
;
3561 if (!event_notifier_test_and_clear(&vdev
->err_notifier
)) {
3566 * TBD. Retrieve the error details and decide what action
3567 * needs to be taken. One of the actions could be to pass
3568 * the error to the guest and have the guest driver recover
3569 * from the error. This requires that PCIe capabilities be
3570 * exposed to the guest. For now, we just terminate the
3571 * guest to contain the error.
3574 error_report("%s(%04x:%02x:%02x.%x) Unrecoverable error detected. "
3575 "Please collect any data possible and then kill the guest",
3576 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
3577 vdev
->host
.slot
, vdev
->host
.function
);
3579 vm_stop(RUN_STATE_IO_ERROR
);
3583 * Registers error notifier for devices supporting error recovery.
3584 * If we encounter a failure in this function, we report an error
3585 * and continue after disabling error recovery support for the
3588 static void vfio_register_err_notifier(VFIODevice
*vdev
)
3592 struct vfio_irq_set
*irq_set
;
3595 if (!vdev
->pci_aer
) {
3599 if (event_notifier_init(&vdev
->err_notifier
, 0)) {
3600 error_report("vfio: Unable to init event notifier for error detection");
3601 vdev
->pci_aer
= false;
3605 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
3607 irq_set
= g_malloc0(argsz
);
3608 irq_set
->argsz
= argsz
;
3609 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
3610 VFIO_IRQ_SET_ACTION_TRIGGER
;
3611 irq_set
->index
= VFIO_PCI_ERR_IRQ_INDEX
;
3614 pfd
= (int32_t *)&irq_set
->data
;
3616 *pfd
= event_notifier_get_fd(&vdev
->err_notifier
);
3617 qemu_set_fd_handler(*pfd
, vfio_err_notifier_handler
, NULL
, vdev
);
3619 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
3621 error_report("vfio: Failed to set up error notification");
3622 qemu_set_fd_handler(*pfd
, NULL
, NULL
, vdev
);
3623 event_notifier_cleanup(&vdev
->err_notifier
);
3624 vdev
->pci_aer
= false;
3629 static void vfio_unregister_err_notifier(VFIODevice
*vdev
)
3632 struct vfio_irq_set
*irq_set
;
3636 if (!vdev
->pci_aer
) {
3640 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
3642 irq_set
= g_malloc0(argsz
);
3643 irq_set
->argsz
= argsz
;
3644 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
3645 VFIO_IRQ_SET_ACTION_TRIGGER
;
3646 irq_set
->index
= VFIO_PCI_ERR_IRQ_INDEX
;
3649 pfd
= (int32_t *)&irq_set
->data
;
3652 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
3654 error_report("vfio: Failed to de-assign error fd: %m");
3657 qemu_set_fd_handler(event_notifier_get_fd(&vdev
->err_notifier
),
3659 event_notifier_cleanup(&vdev
->err_notifier
);
3662 static int vfio_initfn(PCIDevice
*pdev
)
3664 VFIODevice
*pvdev
, *vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
3666 char path
[PATH_MAX
], iommu_group_path
[PATH_MAX
], *group_name
;
3672 /* Check that the host device exists */
3673 snprintf(path
, sizeof(path
),
3674 "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/",
3675 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3676 vdev
->host
.function
);
3677 if (stat(path
, &st
) < 0) {
3678 error_report("vfio: error: no such host device: %s", path
);
3682 strncat(path
, "iommu_group", sizeof(path
) - strlen(path
) - 1);
3684 len
= readlink(path
, iommu_group_path
, PATH_MAX
);
3686 error_report("vfio: error no iommu_group for device");
3690 iommu_group_path
[len
] = 0;
3691 group_name
= basename(iommu_group_path
);
3693 if (sscanf(group_name
, "%d", &groupid
) != 1) {
3694 error_report("vfio: error reading %s: %m", path
);
3698 DPRINTF("%s(%04x:%02x:%02x.%x) group %d\n", __func__
, vdev
->host
.domain
,
3699 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
, groupid
);
3701 group
= vfio_get_group(groupid
);
3703 error_report("vfio: failed to get group %d", groupid
);
3707 snprintf(path
, sizeof(path
), "%04x:%02x:%02x.%01x",
3708 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3709 vdev
->host
.function
);
3711 QLIST_FOREACH(pvdev
, &group
->device_list
, next
) {
3712 if (pvdev
->host
.domain
== vdev
->host
.domain
&&
3713 pvdev
->host
.bus
== vdev
->host
.bus
&&
3714 pvdev
->host
.slot
== vdev
->host
.slot
&&
3715 pvdev
->host
.function
== vdev
->host
.function
) {
3717 error_report("vfio: error: device %s is already attached", path
);
3718 vfio_put_group(group
);
3723 ret
= vfio_get_device(group
, path
, vdev
);
3725 error_report("vfio: failed to get device %s", path
);
3726 vfio_put_group(group
);
3730 /* Get a copy of config space */
3731 ret
= pread(vdev
->fd
, vdev
->pdev
.config
,
3732 MIN(pci_config_size(&vdev
->pdev
), vdev
->config_size
),
3733 vdev
->config_offset
);
3734 if (ret
< (int)MIN(pci_config_size(&vdev
->pdev
), vdev
->config_size
)) {
3735 ret
= ret
< 0 ? -errno
: -EFAULT
;
3736 error_report("vfio: Failed to read device config space");
3740 /* vfio emulates a lot for us, but some bits need extra love */
3741 vdev
->emulated_config_bits
= g_malloc0(vdev
->config_size
);
3743 /* QEMU can choose to expose the ROM or not */
3744 memset(vdev
->emulated_config_bits
+ PCI_ROM_ADDRESS
, 0xff, 4);
3746 /* QEMU can change multi-function devices to single function, or reverse */
3747 vdev
->emulated_config_bits
[PCI_HEADER_TYPE
] =
3748 PCI_HEADER_TYPE_MULTI_FUNCTION
;
3750 /* Restore or clear multifunction, this is always controlled by QEMU */
3751 if (vdev
->pdev
.cap_present
& QEMU_PCI_CAP_MULTIFUNCTION
) {
3752 vdev
->pdev
.config
[PCI_HEADER_TYPE
] |= PCI_HEADER_TYPE_MULTI_FUNCTION
;
3754 vdev
->pdev
.config
[PCI_HEADER_TYPE
] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
3758 * Clear host resource mapping info. If we choose not to register a
3759 * BAR, such as might be the case with the option ROM, we can get
3760 * confusing, unwritable, residual addresses from the host here.
3762 memset(&vdev
->pdev
.config
[PCI_BASE_ADDRESS_0
], 0, 24);
3763 memset(&vdev
->pdev
.config
[PCI_ROM_ADDRESS
], 0, 4);
3765 vfio_pci_size_rom(vdev
);
3767 ret
= vfio_early_setup_msix(vdev
);
3772 vfio_map_bars(vdev
);
3774 ret
= vfio_add_capabilities(vdev
);
3779 /* QEMU emulates all of MSI & MSIX */
3780 if (pdev
->cap_present
& QEMU_PCI_CAP_MSIX
) {
3781 memset(vdev
->emulated_config_bits
+ pdev
->msix_cap
, 0xff,
3785 if (pdev
->cap_present
& QEMU_PCI_CAP_MSI
) {
3786 memset(vdev
->emulated_config_bits
+ pdev
->msi_cap
, 0xff,
3787 vdev
->msi_cap_size
);
3790 if (vfio_pci_read_config(&vdev
->pdev
, PCI_INTERRUPT_PIN
, 1)) {
3791 vdev
->intx
.mmap_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
3792 vfio_intx_mmap_enable
, vdev
);
3793 pci_device_set_intx_routing_notifier(&vdev
->pdev
, vfio_update_irq
);
3794 ret
= vfio_enable_intx(vdev
);
3800 add_boot_device_path(vdev
->bootindex
, &pdev
->qdev
, NULL
);
3801 vfio_register_err_notifier(vdev
);
3806 pci_device_set_intx_routing_notifier(&vdev
->pdev
, NULL
);
3807 vfio_teardown_msi(vdev
);
3808 vfio_unmap_bars(vdev
);
3810 g_free(vdev
->emulated_config_bits
);
3811 vfio_put_device(vdev
);
3812 vfio_put_group(group
);
3816 static void vfio_exitfn(PCIDevice
*pdev
)
3818 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
3819 VFIOGroup
*group
= vdev
->group
;
3821 vfio_unregister_err_notifier(vdev
);
3822 pci_device_set_intx_routing_notifier(&vdev
->pdev
, NULL
);
3823 vfio_disable_interrupts(vdev
);
3824 if (vdev
->intx
.mmap_timer
) {
3825 timer_free(vdev
->intx
.mmap_timer
);
3827 vfio_teardown_msi(vdev
);
3828 vfio_unmap_bars(vdev
);
3829 g_free(vdev
->emulated_config_bits
);
3831 vfio_put_device(vdev
);
3832 vfio_put_group(group
);
3835 static void vfio_pci_reset(DeviceState
*dev
)
3837 PCIDevice
*pdev
= DO_UPCAST(PCIDevice
, qdev
, dev
);
3838 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
3840 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
3841 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
3843 vfio_pci_pre_reset(vdev
);
3845 if (vdev
->reset_works
&& (vdev
->has_flr
|| !vdev
->has_pm_reset
) &&
3846 !ioctl(vdev
->fd
, VFIO_DEVICE_RESET
)) {
3847 DPRINTF("%04x:%02x:%02x.%x FLR/VFIO_DEVICE_RESET\n", vdev
->host
.domain
,
3848 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
3852 /* See if we can do our own bus reset */
3853 if (!vfio_pci_hot_reset_one(vdev
)) {
3857 /* If nothing else works and the device supports PM reset, use it */
3858 if (vdev
->reset_works
&& vdev
->has_pm_reset
&&
3859 !ioctl(vdev
->fd
, VFIO_DEVICE_RESET
)) {
3860 DPRINTF("%04x:%02x:%02x.%x PCI PM Reset\n", vdev
->host
.domain
,
3861 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
3866 vfio_pci_post_reset(vdev
);
3869 static Property vfio_pci_dev_properties
[] = {
3870 DEFINE_PROP_PCI_HOST_DEVADDR("host", VFIODevice
, host
),
3871 DEFINE_PROP_UINT32("x-intx-mmap-timeout-ms", VFIODevice
,
3872 intx
.mmap_timeout
, 1100),
3873 DEFINE_PROP_BIT("x-vga", VFIODevice
, features
,
3874 VFIO_FEATURE_ENABLE_VGA_BIT
, false),
3875 DEFINE_PROP_INT32("bootindex", VFIODevice
, bootindex
, -1),
3877 * TODO - support passed fds... is this necessary?
3878 * DEFINE_PROP_STRING("vfiofd", VFIODevice, vfiofd_name),
3879 * DEFINE_PROP_STRING("vfiogroupfd, VFIODevice, vfiogroupfd_name),
3881 DEFINE_PROP_END_OF_LIST(),
3884 static const VMStateDescription vfio_pci_vmstate
= {
3889 static void vfio_pci_dev_class_init(ObjectClass
*klass
, void *data
)
3891 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3892 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(klass
);
3894 dc
->reset
= vfio_pci_reset
;
3895 dc
->props
= vfio_pci_dev_properties
;
3896 dc
->vmsd
= &vfio_pci_vmstate
;
3897 dc
->desc
= "VFIO-based PCI device assignment";
3898 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
3899 pdc
->init
= vfio_initfn
;
3900 pdc
->exit
= vfio_exitfn
;
3901 pdc
->config_read
= vfio_pci_read_config
;
3902 pdc
->config_write
= vfio_pci_write_config
;
3903 pdc
->is_express
= 1; /* We might be */
3906 static const TypeInfo vfio_pci_dev_info
= {
3908 .parent
= TYPE_PCI_DEVICE
,
3909 .instance_size
= sizeof(VFIODevice
),
3910 .class_init
= vfio_pci_dev_class_init
,
3913 static void register_vfio_pci_dev_type(void)
3915 type_register_static(&vfio_pci_dev_info
);
3918 type_init(register_vfio_pci_dev_type
)