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
58 typedef struct VFIOQuirk
{
60 struct VFIODevice
*vdev
;
61 QLIST_ENTRY(VFIOQuirk
) next
;
63 uint32_t base_offset
:TARGET_PAGE_BITS
;
64 uint32_t address_offset
:TARGET_PAGE_BITS
;
65 uint32_t address_size
:3;
68 uint32_t address_match
;
69 uint32_t address_mask
;
71 uint32_t address_val
:TARGET_PAGE_BITS
;
72 uint32_t data_offset
:TARGET_PAGE_BITS
;
81 typedef struct VFIOBAR
{
82 off_t fd_offset
; /* offset of BAR within device fd */
83 int fd
; /* device fd, allows us to pass VFIOBAR as opaque data */
84 MemoryRegion mem
; /* slow, read/write access */
85 MemoryRegion mmap_mem
; /* direct mapped access */
88 uint32_t flags
; /* VFIO region flags (rd/wr/mmap) */
89 uint8_t nr
; /* cache the BAR number for debug */
92 QLIST_HEAD(, VFIOQuirk
) quirks
;
95 typedef struct VFIOVGARegion
{
99 QLIST_HEAD(, VFIOQuirk
) quirks
;
102 typedef struct VFIOVGA
{
105 VFIOVGARegion region
[QEMU_PCI_VGA_NUM_REGIONS
];
108 typedef struct VFIOINTx
{
109 bool pending
; /* interrupt pending */
110 bool kvm_accel
; /* set when QEMU bypass through KVM enabled */
111 uint8_t pin
; /* which pin to pull for qemu_set_irq */
112 EventNotifier interrupt
; /* eventfd triggered on interrupt */
113 EventNotifier unmask
; /* eventfd for unmask on QEMU bypass */
114 PCIINTxRoute route
; /* routing info for QEMU bypass */
115 uint32_t mmap_timeout
; /* delay to re-enable mmaps after interrupt */
116 QEMUTimer
*mmap_timer
; /* enable mmaps after periods w/o interrupts */
119 typedef struct VFIOMSIVector
{
120 EventNotifier interrupt
; /* eventfd triggered on interrupt */
121 struct VFIODevice
*vdev
; /* back pointer to device */
122 MSIMessage msg
; /* cache the MSI message so we know when it changes */
123 int virq
; /* KVM irqchip route for QEMU bypass */
136 typedef struct VFIOContainer
{
137 int fd
; /* /dev/vfio/vfio, empowered by the attached groups */
139 /* enable abstraction to support various iommu backends */
141 MemoryListener listener
; /* Used by type1 iommu */
143 void (*release
)(struct VFIOContainer
*);
145 QLIST_HEAD(, VFIOGroup
) group_list
;
146 QLIST_ENTRY(VFIOContainer
) next
;
149 /* Cache of MSI-X setup plus extra mmap and memory region for split BAR map */
150 typedef struct VFIOMSIXInfo
{
154 uint32_t table_offset
;
156 MemoryRegion mmap_mem
;
160 typedef struct VFIODevice
{
164 unsigned int config_size
;
165 uint8_t *emulated_config_bits
; /* QEMU emulated bits, little-endian */
166 off_t config_offset
; /* Offset of config space region within device fd */
167 unsigned int rom_size
;
168 off_t rom_offset
; /* Offset of ROM region within device fd */
171 VFIOMSIVector
*msi_vectors
;
173 int nr_vectors
; /* Number of MSI/MSIX vectors currently in use */
174 int interrupt
; /* Current interrupt type */
175 VFIOBAR bars
[PCI_NUM_REGIONS
- 1]; /* No ROM */
176 VFIOVGA vga
; /* 0xa0000, 0x3b0, 0x3c0 */
177 PCIHostDeviceAddress host
;
178 QLIST_ENTRY(VFIODevice
) next
;
179 struct VFIOGroup
*group
;
180 EventNotifier err_notifier
;
182 #define VFIO_FEATURE_ENABLE_VGA_BIT 0
183 #define VFIO_FEATURE_ENABLE_VGA (1 << VFIO_FEATURE_ENABLE_VGA_BIT)
194 typedef struct VFIOGroup
{
197 VFIOContainer
*container
;
198 QLIST_HEAD(, VFIODevice
) device_list
;
199 QLIST_ENTRY(VFIOGroup
) next
;
200 QLIST_ENTRY(VFIOGroup
) container_next
;
203 #define MSIX_CAP_LENGTH 12
205 static QLIST_HEAD(, VFIOContainer
)
206 container_list
= QLIST_HEAD_INITIALIZER(container_list
);
208 static QLIST_HEAD(, VFIOGroup
)
209 group_list
= QLIST_HEAD_INITIALIZER(group_list
);
211 static void vfio_disable_interrupts(VFIODevice
*vdev
);
212 static uint32_t vfio_pci_read_config(PCIDevice
*pdev
, uint32_t addr
, int len
);
213 static void vfio_pci_write_config(PCIDevice
*pdev
, uint32_t addr
,
214 uint32_t val
, int len
);
215 static void vfio_mmap_set_enabled(VFIODevice
*vdev
, bool enabled
);
218 * Common VFIO interrupt disable
220 static void vfio_disable_irqindex(VFIODevice
*vdev
, int index
)
222 struct vfio_irq_set irq_set
= {
223 .argsz
= sizeof(irq_set
),
224 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_TRIGGER
,
230 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
236 static void vfio_unmask_intx(VFIODevice
*vdev
)
238 struct vfio_irq_set irq_set
= {
239 .argsz
= sizeof(irq_set
),
240 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_UNMASK
,
241 .index
= VFIO_PCI_INTX_IRQ_INDEX
,
246 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
249 #ifdef CONFIG_KVM /* Unused outside of CONFIG_KVM code */
250 static void vfio_mask_intx(VFIODevice
*vdev
)
252 struct vfio_irq_set irq_set
= {
253 .argsz
= sizeof(irq_set
),
254 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_MASK
,
255 .index
= VFIO_PCI_INTX_IRQ_INDEX
,
260 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
265 * Disabling BAR mmaping can be slow, but toggling it around INTx can
266 * also be a huge overhead. We try to get the best of both worlds by
267 * waiting until an interrupt to disable mmaps (subsequent transitions
268 * to the same state are effectively no overhead). If the interrupt has
269 * been serviced and the time gap is long enough, we re-enable mmaps for
270 * performance. This works well for things like graphics cards, which
271 * may not use their interrupt at all and are penalized to an unusable
272 * level by read/write BAR traps. Other devices, like NICs, have more
273 * regular interrupts and see much better latency by staying in non-mmap
274 * mode. We therefore set the default mmap_timeout such that a ping
275 * is just enough to keep the mmap disabled. Users can experiment with
276 * other options with the x-intx-mmap-timeout-ms parameter (a value of
277 * zero disables the timer).
279 static void vfio_intx_mmap_enable(void *opaque
)
281 VFIODevice
*vdev
= opaque
;
283 if (vdev
->intx
.pending
) {
284 timer_mod(vdev
->intx
.mmap_timer
,
285 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vdev
->intx
.mmap_timeout
);
289 vfio_mmap_set_enabled(vdev
, true);
292 static void vfio_intx_interrupt(void *opaque
)
294 VFIODevice
*vdev
= opaque
;
296 if (!event_notifier_test_and_clear(&vdev
->intx
.interrupt
)) {
300 DPRINTF("%s(%04x:%02x:%02x.%x) Pin %c\n", __func__
, vdev
->host
.domain
,
301 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
302 'A' + vdev
->intx
.pin
);
304 vdev
->intx
.pending
= true;
305 pci_irq_assert(&vdev
->pdev
);
306 vfio_mmap_set_enabled(vdev
, false);
307 if (vdev
->intx
.mmap_timeout
) {
308 timer_mod(vdev
->intx
.mmap_timer
,
309 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vdev
->intx
.mmap_timeout
);
313 static void vfio_eoi(VFIODevice
*vdev
)
315 if (!vdev
->intx
.pending
) {
319 DPRINTF("%s(%04x:%02x:%02x.%x) EOI\n", __func__
, vdev
->host
.domain
,
320 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
322 vdev
->intx
.pending
= false;
323 pci_irq_deassert(&vdev
->pdev
);
324 vfio_unmask_intx(vdev
);
327 static void vfio_enable_intx_kvm(VFIODevice
*vdev
)
330 struct kvm_irqfd irqfd
= {
331 .fd
= event_notifier_get_fd(&vdev
->intx
.interrupt
),
332 .gsi
= vdev
->intx
.route
.irq
,
333 .flags
= KVM_IRQFD_FLAG_RESAMPLE
,
335 struct vfio_irq_set
*irq_set
;
339 if (!VFIO_ALLOW_KVM_INTX
|| !kvm_irqfds_enabled() ||
340 vdev
->intx
.route
.mode
!= PCI_INTX_ENABLED
||
341 !kvm_check_extension(kvm_state
, KVM_CAP_IRQFD_RESAMPLE
)) {
345 /* Get to a known interrupt state */
346 qemu_set_fd_handler(irqfd
.fd
, NULL
, NULL
, vdev
);
347 vfio_mask_intx(vdev
);
348 vdev
->intx
.pending
= false;
349 pci_irq_deassert(&vdev
->pdev
);
351 /* Get an eventfd for resample/unmask */
352 if (event_notifier_init(&vdev
->intx
.unmask
, 0)) {
353 error_report("vfio: Error: event_notifier_init failed eoi");
357 /* KVM triggers it, VFIO listens for it */
358 irqfd
.resamplefd
= event_notifier_get_fd(&vdev
->intx
.unmask
);
360 if (kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
)) {
361 error_report("vfio: Error: Failed to setup resample irqfd: %m");
365 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
367 irq_set
= g_malloc0(argsz
);
368 irq_set
->argsz
= argsz
;
369 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_UNMASK
;
370 irq_set
->index
= VFIO_PCI_INTX_IRQ_INDEX
;
373 pfd
= (int32_t *)&irq_set
->data
;
375 *pfd
= irqfd
.resamplefd
;
377 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
380 error_report("vfio: Error: Failed to setup INTx unmask fd: %m");
385 vfio_unmask_intx(vdev
);
387 vdev
->intx
.kvm_accel
= true;
389 DPRINTF("%s(%04x:%02x:%02x.%x) KVM INTx accel enabled\n",
390 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
391 vdev
->host
.slot
, vdev
->host
.function
);
396 irqfd
.flags
= KVM_IRQFD_FLAG_DEASSIGN
;
397 kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
);
399 event_notifier_cleanup(&vdev
->intx
.unmask
);
401 qemu_set_fd_handler(irqfd
.fd
, vfio_intx_interrupt
, NULL
, vdev
);
402 vfio_unmask_intx(vdev
);
406 static void vfio_disable_intx_kvm(VFIODevice
*vdev
)
409 struct kvm_irqfd irqfd
= {
410 .fd
= event_notifier_get_fd(&vdev
->intx
.interrupt
),
411 .gsi
= vdev
->intx
.route
.irq
,
412 .flags
= KVM_IRQFD_FLAG_DEASSIGN
,
415 if (!vdev
->intx
.kvm_accel
) {
420 * Get to a known state, hardware masked, QEMU ready to accept new
421 * interrupts, QEMU IRQ de-asserted.
423 vfio_mask_intx(vdev
);
424 vdev
->intx
.pending
= false;
425 pci_irq_deassert(&vdev
->pdev
);
427 /* Tell KVM to stop listening for an INTx irqfd */
428 if (kvm_vm_ioctl(kvm_state
, KVM_IRQFD
, &irqfd
)) {
429 error_report("vfio: Error: Failed to disable INTx irqfd: %m");
432 /* We only need to close the eventfd for VFIO to cleanup the kernel side */
433 event_notifier_cleanup(&vdev
->intx
.unmask
);
435 /* QEMU starts listening for interrupt events. */
436 qemu_set_fd_handler(irqfd
.fd
, vfio_intx_interrupt
, NULL
, vdev
);
438 vdev
->intx
.kvm_accel
= false;
440 /* If we've missed an event, let it re-fire through QEMU */
441 vfio_unmask_intx(vdev
);
443 DPRINTF("%s(%04x:%02x:%02x.%x) KVM INTx accel disabled\n",
444 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
445 vdev
->host
.slot
, vdev
->host
.function
);
449 static void vfio_update_irq(PCIDevice
*pdev
)
451 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
454 if (vdev
->interrupt
!= VFIO_INT_INTx
) {
458 route
= pci_device_route_intx_to_irq(&vdev
->pdev
, vdev
->intx
.pin
);
460 if (!pci_intx_route_changed(&vdev
->intx
.route
, &route
)) {
461 return; /* Nothing changed */
464 DPRINTF("%s(%04x:%02x:%02x.%x) IRQ moved %d -> %d\n", __func__
,
465 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
466 vdev
->host
.function
, vdev
->intx
.route
.irq
, route
.irq
);
468 vfio_disable_intx_kvm(vdev
);
470 vdev
->intx
.route
= route
;
472 if (route
.mode
!= PCI_INTX_ENABLED
) {
476 vfio_enable_intx_kvm(vdev
);
478 /* Re-enable the interrupt in cased we missed an EOI */
482 static int vfio_enable_intx(VFIODevice
*vdev
)
484 uint8_t pin
= vfio_pci_read_config(&vdev
->pdev
, PCI_INTERRUPT_PIN
, 1);
486 struct vfio_irq_set
*irq_set
;
493 vfio_disable_interrupts(vdev
);
495 vdev
->intx
.pin
= pin
- 1; /* Pin A (1) -> irq[0] */
496 pci_config_set_interrupt_pin(vdev
->pdev
.config
, pin
);
500 * Only conditional to avoid generating error messages on platforms
501 * where we won't actually use the result anyway.
503 if (kvm_irqfds_enabled() &&
504 kvm_check_extension(kvm_state
, KVM_CAP_IRQFD_RESAMPLE
)) {
505 vdev
->intx
.route
= pci_device_route_intx_to_irq(&vdev
->pdev
,
510 ret
= event_notifier_init(&vdev
->intx
.interrupt
, 0);
512 error_report("vfio: Error: event_notifier_init failed");
516 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
518 irq_set
= g_malloc0(argsz
);
519 irq_set
->argsz
= argsz
;
520 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_TRIGGER
;
521 irq_set
->index
= VFIO_PCI_INTX_IRQ_INDEX
;
524 pfd
= (int32_t *)&irq_set
->data
;
526 *pfd
= event_notifier_get_fd(&vdev
->intx
.interrupt
);
527 qemu_set_fd_handler(*pfd
, vfio_intx_interrupt
, NULL
, vdev
);
529 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
532 error_report("vfio: Error: Failed to setup INTx fd: %m");
533 qemu_set_fd_handler(*pfd
, NULL
, NULL
, vdev
);
534 event_notifier_cleanup(&vdev
->intx
.interrupt
);
538 vfio_enable_intx_kvm(vdev
);
540 vdev
->interrupt
= VFIO_INT_INTx
;
542 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
543 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
548 static void vfio_disable_intx(VFIODevice
*vdev
)
552 timer_del(vdev
->intx
.mmap_timer
);
553 vfio_disable_intx_kvm(vdev
);
554 vfio_disable_irqindex(vdev
, VFIO_PCI_INTX_IRQ_INDEX
);
555 vdev
->intx
.pending
= false;
556 pci_irq_deassert(&vdev
->pdev
);
557 vfio_mmap_set_enabled(vdev
, true);
559 fd
= event_notifier_get_fd(&vdev
->intx
.interrupt
);
560 qemu_set_fd_handler(fd
, NULL
, NULL
, vdev
);
561 event_notifier_cleanup(&vdev
->intx
.interrupt
);
563 vdev
->interrupt
= VFIO_INT_NONE
;
565 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
566 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
572 static void vfio_msi_interrupt(void *opaque
)
574 VFIOMSIVector
*vector
= opaque
;
575 VFIODevice
*vdev
= vector
->vdev
;
576 int nr
= vector
- vdev
->msi_vectors
;
578 if (!event_notifier_test_and_clear(&vector
->interrupt
)) {
582 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d\n", __func__
,
583 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
584 vdev
->host
.function
, nr
);
586 if (vdev
->interrupt
== VFIO_INT_MSIX
) {
587 msix_notify(&vdev
->pdev
, nr
);
588 } else if (vdev
->interrupt
== VFIO_INT_MSI
) {
589 msi_notify(&vdev
->pdev
, nr
);
591 error_report("vfio: MSI interrupt receieved, but not enabled?");
595 static int vfio_enable_vectors(VFIODevice
*vdev
, bool msix
)
597 struct vfio_irq_set
*irq_set
;
598 int ret
= 0, i
, argsz
;
601 argsz
= sizeof(*irq_set
) + (vdev
->nr_vectors
* sizeof(*fds
));
603 irq_set
= g_malloc0(argsz
);
604 irq_set
->argsz
= argsz
;
605 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
| VFIO_IRQ_SET_ACTION_TRIGGER
;
606 irq_set
->index
= msix
? VFIO_PCI_MSIX_IRQ_INDEX
: VFIO_PCI_MSI_IRQ_INDEX
;
608 irq_set
->count
= vdev
->nr_vectors
;
609 fds
= (int32_t *)&irq_set
->data
;
611 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
612 if (!vdev
->msi_vectors
[i
].use
) {
617 fds
[i
] = event_notifier_get_fd(&vdev
->msi_vectors
[i
].interrupt
);
620 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
627 static int vfio_msix_vector_do_use(PCIDevice
*pdev
, unsigned int nr
,
628 MSIMessage
*msg
, IOHandler
*handler
)
630 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
631 VFIOMSIVector
*vector
;
634 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d used\n", __func__
,
635 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
636 vdev
->host
.function
, nr
);
638 vector
= &vdev
->msi_vectors
[nr
];
642 msix_vector_use(pdev
, nr
);
644 if (event_notifier_init(&vector
->interrupt
, 0)) {
645 error_report("vfio: Error: event_notifier_init failed");
649 * Attempt to enable route through KVM irqchip,
650 * default to userspace handling if unavailable.
652 vector
->virq
= msg
? kvm_irqchip_add_msi_route(kvm_state
, *msg
) : -1;
653 if (vector
->virq
< 0 ||
654 kvm_irqchip_add_irqfd_notifier(kvm_state
, &vector
->interrupt
,
655 NULL
, vector
->virq
) < 0) {
656 if (vector
->virq
>= 0) {
657 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
660 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
661 handler
, NULL
, vector
);
665 * We don't want to have the host allocate all possible MSI vectors
666 * for a device if they're not in use, so we shutdown and incrementally
667 * increase them as needed.
669 if (vdev
->nr_vectors
< nr
+ 1) {
670 vfio_disable_irqindex(vdev
, VFIO_PCI_MSIX_IRQ_INDEX
);
671 vdev
->nr_vectors
= nr
+ 1;
672 ret
= vfio_enable_vectors(vdev
, true);
674 error_report("vfio: failed to enable vectors, %d", ret
);
678 struct vfio_irq_set
*irq_set
;
681 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
683 irq_set
= g_malloc0(argsz
);
684 irq_set
->argsz
= argsz
;
685 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
686 VFIO_IRQ_SET_ACTION_TRIGGER
;
687 irq_set
->index
= VFIO_PCI_MSIX_IRQ_INDEX
;
690 pfd
= (int32_t *)&irq_set
->data
;
692 *pfd
= event_notifier_get_fd(&vector
->interrupt
);
694 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
697 error_report("vfio: failed to modify vector, %d", ret
);
704 static int vfio_msix_vector_use(PCIDevice
*pdev
,
705 unsigned int nr
, MSIMessage msg
)
707 return vfio_msix_vector_do_use(pdev
, nr
, &msg
, vfio_msi_interrupt
);
710 static void vfio_msix_vector_release(PCIDevice
*pdev
, unsigned int nr
)
712 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
713 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[nr
];
715 struct vfio_irq_set
*irq_set
;
718 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d released\n", __func__
,
719 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
720 vdev
->host
.function
, nr
);
723 * XXX What's the right thing to do here? This turns off the interrupt
724 * completely, but do we really just want to switch the interrupt to
725 * bouncing through userspace and let msix.c drop it? Not sure.
727 msix_vector_unuse(pdev
, nr
);
729 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
731 irq_set
= g_malloc0(argsz
);
732 irq_set
->argsz
= argsz
;
733 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
734 VFIO_IRQ_SET_ACTION_TRIGGER
;
735 irq_set
->index
= VFIO_PCI_MSIX_IRQ_INDEX
;
738 pfd
= (int32_t *)&irq_set
->data
;
742 ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
746 if (vector
->virq
< 0) {
747 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
750 kvm_irqchip_remove_irqfd_notifier(kvm_state
, &vector
->interrupt
,
752 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
756 event_notifier_cleanup(&vector
->interrupt
);
760 static void vfio_enable_msix(VFIODevice
*vdev
)
762 vfio_disable_interrupts(vdev
);
764 vdev
->msi_vectors
= g_malloc0(vdev
->msix
->entries
* sizeof(VFIOMSIVector
));
766 vdev
->interrupt
= VFIO_INT_MSIX
;
769 * Some communication channels between VF & PF or PF & fw rely on the
770 * physical state of the device and expect that enabling MSI-X from the
771 * guest enables the same on the host. When our guest is Linux, the
772 * guest driver call to pci_enable_msix() sets the enabling bit in the
773 * MSI-X capability, but leaves the vector table masked. We therefore
774 * can't rely on a vector_use callback (from request_irq() in the guest)
775 * to switch the physical device into MSI-X mode because that may come a
776 * long time after pci_enable_msix(). This code enables vector 0 with
777 * triggering to userspace, then immediately release the vector, leaving
778 * the physical device with no vectors enabled, but MSI-X enabled, just
779 * like the guest view.
781 vfio_msix_vector_do_use(&vdev
->pdev
, 0, NULL
, NULL
);
782 vfio_msix_vector_release(&vdev
->pdev
, 0);
784 if (msix_set_vector_notifiers(&vdev
->pdev
, vfio_msix_vector_use
,
785 vfio_msix_vector_release
, NULL
)) {
786 error_report("vfio: msix_set_vector_notifiers failed");
789 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
790 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
793 static void vfio_enable_msi(VFIODevice
*vdev
)
797 vfio_disable_interrupts(vdev
);
799 vdev
->nr_vectors
= msi_nr_vectors_allocated(&vdev
->pdev
);
801 vdev
->msi_vectors
= g_malloc0(vdev
->nr_vectors
* sizeof(VFIOMSIVector
));
803 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
804 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
809 if (event_notifier_init(&vector
->interrupt
, 0)) {
810 error_report("vfio: Error: event_notifier_init failed");
813 vector
->msg
= msi_get_message(&vdev
->pdev
, i
);
816 * Attempt to enable route through KVM irqchip,
817 * default to userspace handling if unavailable.
819 vector
->virq
= kvm_irqchip_add_msi_route(kvm_state
, vector
->msg
);
820 if (vector
->virq
< 0 ||
821 kvm_irqchip_add_irqfd_notifier(kvm_state
, &vector
->interrupt
,
822 NULL
, vector
->virq
) < 0) {
823 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
824 vfio_msi_interrupt
, NULL
, vector
);
828 ret
= vfio_enable_vectors(vdev
, false);
831 error_report("vfio: Error: Failed to setup MSI fds: %m");
832 } else if (ret
!= vdev
->nr_vectors
) {
833 error_report("vfio: Error: Failed to enable %d "
834 "MSI vectors, retry with %d", vdev
->nr_vectors
, ret
);
837 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
838 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
839 if (vector
->virq
>= 0) {
840 kvm_irqchip_remove_irqfd_notifier(kvm_state
, &vector
->interrupt
,
842 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
845 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
848 event_notifier_cleanup(&vector
->interrupt
);
851 g_free(vdev
->msi_vectors
);
853 if (ret
> 0 && ret
!= vdev
->nr_vectors
) {
854 vdev
->nr_vectors
= ret
;
857 vdev
->nr_vectors
= 0;
862 vdev
->interrupt
= VFIO_INT_MSI
;
864 DPRINTF("%s(%04x:%02x:%02x.%x) Enabled %d MSI vectors\n", __func__
,
865 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
866 vdev
->host
.function
, vdev
->nr_vectors
);
869 static void vfio_disable_msi_common(VFIODevice
*vdev
)
871 g_free(vdev
->msi_vectors
);
872 vdev
->msi_vectors
= NULL
;
873 vdev
->nr_vectors
= 0;
874 vdev
->interrupt
= VFIO_INT_NONE
;
876 vfio_enable_intx(vdev
);
879 static void vfio_disable_msix(VFIODevice
*vdev
)
881 msix_unset_vector_notifiers(&vdev
->pdev
);
883 if (vdev
->nr_vectors
) {
884 vfio_disable_irqindex(vdev
, VFIO_PCI_MSIX_IRQ_INDEX
);
887 vfio_disable_msi_common(vdev
);
889 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
890 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
893 static void vfio_disable_msi(VFIODevice
*vdev
)
897 vfio_disable_irqindex(vdev
, VFIO_PCI_MSI_IRQ_INDEX
);
899 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
900 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
906 if (vector
->virq
>= 0) {
907 kvm_irqchip_remove_irqfd_notifier(kvm_state
,
908 &vector
->interrupt
, vector
->virq
);
909 kvm_irqchip_release_virq(kvm_state
, vector
->virq
);
912 qemu_set_fd_handler(event_notifier_get_fd(&vector
->interrupt
),
916 event_notifier_cleanup(&vector
->interrupt
);
919 vfio_disable_msi_common(vdev
);
921 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
922 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
925 static void vfio_update_msi(VFIODevice
*vdev
)
929 for (i
= 0; i
< vdev
->nr_vectors
; i
++) {
930 VFIOMSIVector
*vector
= &vdev
->msi_vectors
[i
];
933 if (!vector
->use
|| vector
->virq
< 0) {
937 msg
= msi_get_message(&vdev
->pdev
, i
);
939 if (msg
.address
!= vector
->msg
.address
||
940 msg
.data
!= vector
->msg
.data
) {
942 DPRINTF("%s(%04x:%02x:%02x.%x) MSI vector %d changed\n",
943 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
944 vdev
->host
.slot
, vdev
->host
.function
, i
);
946 kvm_irqchip_update_msi_route(kvm_state
, vector
->virq
, msg
);
953 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
955 static void vfio_bar_write(void *opaque
, hwaddr addr
,
956 uint64_t data
, unsigned size
)
958 VFIOBAR
*bar
= opaque
;
971 buf
.word
= cpu_to_le16(data
);
974 buf
.dword
= cpu_to_le32(data
);
977 hw_error("vfio: unsupported write size, %d bytes\n", size
);
981 if (pwrite(bar
->fd
, &buf
, size
, bar
->fd_offset
+ addr
) != size
) {
982 error_report("%s(,0x%"HWADDR_PRIx
", 0x%"PRIx64
", %d) failed: %m",
983 __func__
, addr
, data
, size
);
988 VFIODevice
*vdev
= container_of(bar
, VFIODevice
, bars
[bar
->nr
]);
990 DPRINTF("%s(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", 0x%"PRIx64
991 ", %d)\n", __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
992 vdev
->host
.slot
, vdev
->host
.function
, bar
->nr
, addr
,
998 * A read or write to a BAR always signals an INTx EOI. This will
999 * do nothing if not pending (including not in INTx mode). We assume
1000 * that a BAR access is in response to an interrupt and that BAR
1001 * accesses will service the interrupt. Unfortunately, we don't know
1002 * which access will service the interrupt, so we're potentially
1003 * getting quite a few host interrupts per guest interrupt.
1005 vfio_eoi(container_of(bar
, VFIODevice
, bars
[bar
->nr
]));
1008 static uint64_t vfio_bar_read(void *opaque
,
1009 hwaddr addr
, unsigned size
)
1011 VFIOBAR
*bar
= opaque
;
1020 if (pread(bar
->fd
, &buf
, size
, bar
->fd_offset
+ addr
) != size
) {
1021 error_report("%s(,0x%"HWADDR_PRIx
", %d) failed: %m",
1022 __func__
, addr
, size
);
1023 return (uint64_t)-1;
1031 data
= le16_to_cpu(buf
.word
);
1034 data
= le32_to_cpu(buf
.dword
);
1037 hw_error("vfio: unsupported read size, %d bytes\n", size
);
1043 VFIODevice
*vdev
= container_of(bar
, VFIODevice
, bars
[bar
->nr
]);
1045 DPRINTF("%s(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
1046 ", %d) = 0x%"PRIx64
"\n", __func__
, vdev
->host
.domain
,
1047 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
1048 bar
->nr
, addr
, size
, data
);
1052 /* Same as write above */
1053 vfio_eoi(container_of(bar
, VFIODevice
, bars
[bar
->nr
]));
1058 static const MemoryRegionOps vfio_bar_ops
= {
1059 .read
= vfio_bar_read
,
1060 .write
= vfio_bar_write
,
1061 .endianness
= DEVICE_LITTLE_ENDIAN
,
1064 static void vfio_pci_load_rom(VFIODevice
*vdev
)
1066 struct vfio_region_info reg_info
= {
1067 .argsz
= sizeof(reg_info
),
1068 .index
= VFIO_PCI_ROM_REGION_INDEX
1074 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
)) {
1075 error_report("vfio: Error getting ROM info: %m");
1079 DPRINTF("Device %04x:%02x:%02x.%x ROM:\n", vdev
->host
.domain
,
1080 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
1081 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
1082 (unsigned long)reg_info
.size
, (unsigned long)reg_info
.offset
,
1083 (unsigned long)reg_info
.flags
);
1085 vdev
->rom_size
= size
= reg_info
.size
;
1086 vdev
->rom_offset
= reg_info
.offset
;
1088 if (!vdev
->rom_size
) {
1092 vdev
->rom
= g_malloc(size
);
1093 memset(vdev
->rom
, 0xff, size
);
1096 bytes
= pread(vdev
->fd
, vdev
->rom
+ off
, size
, vdev
->rom_offset
+ off
);
1099 } else if (bytes
> 0) {
1103 if (errno
== EINTR
|| errno
== EAGAIN
) {
1106 error_report("vfio: Error reading device ROM: %m");
1112 static uint64_t vfio_rom_read(void *opaque
, hwaddr addr
, unsigned size
)
1114 VFIODevice
*vdev
= opaque
;
1115 uint64_t val
= ((uint64_t)1 << (size
* 8)) - 1;
1117 /* Load the ROM lazily when the guest tries to read it */
1118 if (unlikely(!vdev
->rom
)) {
1119 vfio_pci_load_rom(vdev
);
1122 memcpy(&val
, vdev
->rom
+ addr
,
1123 (addr
< vdev
->rom_size
) ? MIN(size
, vdev
->rom_size
- addr
) : 0);
1125 DPRINTF("%s(%04x:%02x:%02x.%x, 0x%"HWADDR_PRIx
", 0x%x) = 0x%"PRIx64
"\n",
1126 __func__
, vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1127 vdev
->host
.function
, addr
, size
, val
);
1132 static void vfio_rom_write(void *opaque
, hwaddr addr
,
1133 uint64_t data
, unsigned size
)
1137 static const MemoryRegionOps vfio_rom_ops
= {
1138 .read
= vfio_rom_read
,
1139 .write
= vfio_rom_write
,
1140 .endianness
= DEVICE_LITTLE_ENDIAN
,
1143 static void vfio_pci_size_rom(VFIODevice
*vdev
)
1145 uint32_t orig
, size
= cpu_to_le32((uint32_t)PCI_ROM_ADDRESS_MASK
);
1146 off_t offset
= vdev
->config_offset
+ PCI_ROM_ADDRESS
;
1149 if (vdev
->pdev
.romfile
|| !vdev
->pdev
.rom_bar
) {
1154 * Use the same size ROM BAR as the physical device. The contents
1155 * will get filled in later when the guest tries to read it.
1157 if (pread(vdev
->fd
, &orig
, 4, offset
) != 4 ||
1158 pwrite(vdev
->fd
, &size
, 4, offset
) != 4 ||
1159 pread(vdev
->fd
, &size
, 4, offset
) != 4 ||
1160 pwrite(vdev
->fd
, &orig
, 4, offset
) != 4) {
1161 error_report("%s(%04x:%02x:%02x.%x) failed: %m",
1162 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
1163 vdev
->host
.slot
, vdev
->host
.function
);
1167 size
= ~(le32_to_cpu(size
) & PCI_ROM_ADDRESS_MASK
) + 1;
1173 DPRINTF("%04x:%02x:%02x.%x ROM size 0x%x\n", vdev
->host
.domain
,
1174 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
, size
);
1176 snprintf(name
, sizeof(name
), "vfio[%04x:%02x:%02x.%x].rom",
1177 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1178 vdev
->host
.function
);
1180 memory_region_init_io(&vdev
->pdev
.rom
, OBJECT(vdev
),
1181 &vfio_rom_ops
, vdev
, name
, size
);
1183 pci_register_bar(&vdev
->pdev
, PCI_ROM_SLOT
,
1184 PCI_BASE_ADDRESS_SPACE_MEMORY
, &vdev
->pdev
.rom
);
1186 vdev
->pdev
.has_rom
= true;
1189 static void vfio_vga_write(void *opaque
, hwaddr addr
,
1190 uint64_t data
, unsigned size
)
1192 VFIOVGARegion
*region
= opaque
;
1193 VFIOVGA
*vga
= container_of(region
, VFIOVGA
, region
[region
->nr
]);
1200 off_t offset
= vga
->fd_offset
+ region
->offset
+ addr
;
1207 buf
.word
= cpu_to_le16(data
);
1210 buf
.dword
= cpu_to_le32(data
);
1213 hw_error("vfio: unsupported write size, %d bytes\n", size
);
1217 if (pwrite(vga
->fd
, &buf
, size
, offset
) != size
) {
1218 error_report("%s(,0x%"HWADDR_PRIx
", 0x%"PRIx64
", %d) failed: %m",
1219 __func__
, region
->offset
+ addr
, data
, size
);
1222 DPRINTF("%s(0x%"HWADDR_PRIx
", 0x%"PRIx64
", %d)\n",
1223 __func__
, region
->offset
+ addr
, data
, size
);
1226 static uint64_t vfio_vga_read(void *opaque
, hwaddr addr
, unsigned size
)
1228 VFIOVGARegion
*region
= opaque
;
1229 VFIOVGA
*vga
= container_of(region
, VFIOVGA
, region
[region
->nr
]);
1237 off_t offset
= vga
->fd_offset
+ region
->offset
+ addr
;
1239 if (pread(vga
->fd
, &buf
, size
, offset
) != size
) {
1240 error_report("%s(,0x%"HWADDR_PRIx
", %d) failed: %m",
1241 __func__
, region
->offset
+ addr
, size
);
1242 return (uint64_t)-1;
1250 data
= le16_to_cpu(buf
.word
);
1253 data
= le32_to_cpu(buf
.dword
);
1256 hw_error("vfio: unsupported read size, %d bytes\n", size
);
1260 DPRINTF("%s(0x%"HWADDR_PRIx
", %d) = 0x%"PRIx64
"\n",
1261 __func__
, region
->offset
+ addr
, size
, data
);
1266 static const MemoryRegionOps vfio_vga_ops
= {
1267 .read
= vfio_vga_read
,
1268 .write
= vfio_vga_write
,
1269 .endianness
= DEVICE_LITTLE_ENDIAN
,
1273 * Device specific quirks
1276 /* Is range1 fully contained within range2? */
1277 static bool vfio_range_contained(uint64_t first1
, uint64_t len1
,
1278 uint64_t first2
, uint64_t len2
) {
1279 return (first1
>= first2
&& first1
+ len1
<= first2
+ len2
);
1282 static bool vfio_flags_enabled(uint8_t flags
, uint8_t mask
)
1284 return (mask
&& (flags
& mask
) == mask
);
1287 static uint64_t vfio_generic_window_quirk_read(void *opaque
,
1288 hwaddr addr
, unsigned size
)
1290 VFIOQuirk
*quirk
= opaque
;
1291 VFIODevice
*vdev
= quirk
->vdev
;
1294 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.read_flags
) &&
1295 ranges_overlap(addr
, size
,
1296 quirk
->data
.data_offset
, quirk
->data
.data_size
)) {
1297 hwaddr offset
= addr
- quirk
->data
.data_offset
;
1299 if (!vfio_range_contained(addr
, size
, quirk
->data
.data_offset
,
1300 quirk
->data
.data_size
)) {
1301 hw_error("%s: window data read not fully contained: %s\n",
1302 __func__
, memory_region_name(&quirk
->mem
));
1305 data
= vfio_pci_read_config(&vdev
->pdev
,
1306 quirk
->data
.address_val
+ offset
, size
);
1308 DPRINTF("%s read(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", %d) = 0x%"
1309 PRIx64
"\n", memory_region_name(&quirk
->mem
), vdev
->host
.domain
,
1310 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
1311 quirk
->data
.bar
, addr
, size
, data
);
1313 data
= vfio_bar_read(&vdev
->bars
[quirk
->data
.bar
],
1314 addr
+ quirk
->data
.base_offset
, size
);
1320 static void vfio_generic_window_quirk_write(void *opaque
, hwaddr addr
,
1321 uint64_t data
, unsigned size
)
1323 VFIOQuirk
*quirk
= opaque
;
1324 VFIODevice
*vdev
= quirk
->vdev
;
1326 if (ranges_overlap(addr
, size
,
1327 quirk
->data
.address_offset
, quirk
->data
.address_size
)) {
1329 if (addr
!= quirk
->data
.address_offset
) {
1330 hw_error("%s: offset write into address window: %s\n",
1331 __func__
, memory_region_name(&quirk
->mem
));
1334 if ((data
& ~quirk
->data
.address_mask
) == quirk
->data
.address_match
) {
1335 quirk
->data
.flags
|= quirk
->data
.write_flags
|
1336 quirk
->data
.read_flags
;
1337 quirk
->data
.address_val
= data
& quirk
->data
.address_mask
;
1339 quirk
->data
.flags
&= ~(quirk
->data
.write_flags
|
1340 quirk
->data
.read_flags
);
1344 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.write_flags
) &&
1345 ranges_overlap(addr
, size
,
1346 quirk
->data
.data_offset
, quirk
->data
.data_size
)) {
1347 hwaddr offset
= addr
- quirk
->data
.data_offset
;
1349 if (!vfio_range_contained(addr
, size
, quirk
->data
.data_offset
,
1350 quirk
->data
.data_size
)) {
1351 hw_error("%s: window data write not fully contained: %s\n",
1352 __func__
, memory_region_name(&quirk
->mem
));
1355 vfio_pci_write_config(&vdev
->pdev
,
1356 quirk
->data
.address_val
+ offset
, data
, size
);
1357 DPRINTF("%s write(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", 0x%"
1358 PRIx64
", %d)\n", memory_region_name(&quirk
->mem
),
1359 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1360 vdev
->host
.function
, quirk
->data
.bar
, addr
, data
, size
);
1364 vfio_bar_write(&vdev
->bars
[quirk
->data
.bar
],
1365 addr
+ quirk
->data
.base_offset
, data
, size
);
1368 static const MemoryRegionOps vfio_generic_window_quirk
= {
1369 .read
= vfio_generic_window_quirk_read
,
1370 .write
= vfio_generic_window_quirk_write
,
1371 .endianness
= DEVICE_LITTLE_ENDIAN
,
1374 static uint64_t vfio_generic_quirk_read(void *opaque
,
1375 hwaddr addr
, unsigned size
)
1377 VFIOQuirk
*quirk
= opaque
;
1378 VFIODevice
*vdev
= quirk
->vdev
;
1379 hwaddr base
= quirk
->data
.address_match
& TARGET_PAGE_MASK
;
1380 hwaddr offset
= quirk
->data
.address_match
& ~TARGET_PAGE_MASK
;
1383 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.read_flags
) &&
1384 ranges_overlap(addr
, size
, offset
, quirk
->data
.address_mask
+ 1)) {
1385 if (!vfio_range_contained(addr
, size
, offset
,
1386 quirk
->data
.address_mask
+ 1)) {
1387 hw_error("%s: read not fully contained: %s\n",
1388 __func__
, memory_region_name(&quirk
->mem
));
1391 data
= vfio_pci_read_config(&vdev
->pdev
, addr
- offset
, size
);
1393 DPRINTF("%s read(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", %d) = 0x%"
1394 PRIx64
"\n", memory_region_name(&quirk
->mem
), vdev
->host
.domain
,
1395 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
1396 quirk
->data
.bar
, addr
+ base
, size
, data
);
1398 data
= vfio_bar_read(&vdev
->bars
[quirk
->data
.bar
], addr
+ base
, size
);
1404 static void vfio_generic_quirk_write(void *opaque
, hwaddr addr
,
1405 uint64_t data
, unsigned size
)
1407 VFIOQuirk
*quirk
= opaque
;
1408 VFIODevice
*vdev
= quirk
->vdev
;
1409 hwaddr base
= quirk
->data
.address_match
& TARGET_PAGE_MASK
;
1410 hwaddr offset
= quirk
->data
.address_match
& ~TARGET_PAGE_MASK
;
1412 if (vfio_flags_enabled(quirk
->data
.flags
, quirk
->data
.write_flags
) &&
1413 ranges_overlap(addr
, size
, offset
, quirk
->data
.address_mask
+ 1)) {
1414 if (!vfio_range_contained(addr
, size
, offset
,
1415 quirk
->data
.address_mask
+ 1)) {
1416 hw_error("%s: write not fully contained: %s\n",
1417 __func__
, memory_region_name(&quirk
->mem
));
1420 vfio_pci_write_config(&vdev
->pdev
, addr
- offset
, data
, size
);
1422 DPRINTF("%s write(%04x:%02x:%02x.%x:BAR%d+0x%"HWADDR_PRIx
", 0x%"
1423 PRIx64
", %d)\n", memory_region_name(&quirk
->mem
),
1424 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1425 vdev
->host
.function
, quirk
->data
.bar
, addr
+ base
, data
, size
);
1427 vfio_bar_write(&vdev
->bars
[quirk
->data
.bar
], addr
+ base
, data
, size
);
1431 static const MemoryRegionOps vfio_generic_quirk
= {
1432 .read
= vfio_generic_quirk_read
,
1433 .write
= vfio_generic_quirk_write
,
1434 .endianness
= DEVICE_LITTLE_ENDIAN
,
1437 #define PCI_VENDOR_ID_ATI 0x1002
1440 * Radeon HD cards (HD5450 & HD7850) report the upper byte of the I/O port BAR
1441 * through VGA register 0x3c3. On newer cards, the I/O port BAR is always
1442 * BAR4 (older cards like the X550 used BAR1, but we don't care to support
1443 * those). Note that on bare metal, a read of 0x3c3 doesn't always return the
1444 * I/O port BAR address. Originally this was coded to return the virtual BAR
1445 * address only if the physical register read returns the actual BAR address,
1446 * but users have reported greater success if we return the virtual address
1449 static uint64_t vfio_ati_3c3_quirk_read(void *opaque
,
1450 hwaddr addr
, unsigned size
)
1452 VFIOQuirk
*quirk
= opaque
;
1453 VFIODevice
*vdev
= quirk
->vdev
;
1454 uint64_t data
= vfio_pci_read_config(&vdev
->pdev
,
1455 PCI_BASE_ADDRESS_0
+ (4 * 4) + 1,
1457 DPRINTF("%s(0x3c3, 1) = 0x%"PRIx64
"\n", __func__
, data
);
1462 static const MemoryRegionOps vfio_ati_3c3_quirk
= {
1463 .read
= vfio_ati_3c3_quirk_read
,
1464 .endianness
= DEVICE_LITTLE_ENDIAN
,
1467 static void vfio_vga_probe_ati_3c3_quirk(VFIODevice
*vdev
)
1469 PCIDevice
*pdev
= &vdev
->pdev
;
1472 if (pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_ATI
) {
1477 * As long as the BAR is >= 256 bytes it will be aligned such that the
1478 * lower byte is always zero. Filter out anything else, if it exists.
1480 if (!vdev
->bars
[4].ioport
|| vdev
->bars
[4].size
< 256) {
1484 quirk
= g_malloc0(sizeof(*quirk
));
1487 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_ati_3c3_quirk
, quirk
,
1488 "vfio-ati-3c3-quirk", 1);
1489 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
1490 3 /* offset 3 bytes from 0x3c0 */, &quirk
->mem
);
1492 QLIST_INSERT_HEAD(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
,
1495 DPRINTF("Enabled ATI/AMD quirk 0x3c3 BAR4for device %04x:%02x:%02x.%x\n",
1496 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1497 vdev
->host
.function
);
1501 * Newer ATI/AMD devices, including HD5450 and HD7850, have a window to PCI
1502 * config space through MMIO BAR2 at offset 0x4000. Nothing seems to access
1503 * the MMIO space directly, but a window to this space is provided through
1504 * I/O port BAR4. Offset 0x0 is the address register and offset 0x4 is the
1505 * data register. When the address is programmed to a range of 0x4000-0x4fff
1506 * PCI configuration space is available. Experimentation seems to indicate
1507 * that only read-only access is provided, but we drop writes when the window
1508 * is enabled to config space nonetheless.
1510 static void vfio_probe_ati_bar4_window_quirk(VFIODevice
*vdev
, int nr
)
1512 PCIDevice
*pdev
= &vdev
->pdev
;
1515 if (!vdev
->has_vga
|| nr
!= 4 ||
1516 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_ATI
) {
1520 quirk
= g_malloc0(sizeof(*quirk
));
1522 quirk
->data
.address_size
= 4;
1523 quirk
->data
.data_offset
= 4;
1524 quirk
->data
.data_size
= 4;
1525 quirk
->data
.address_match
= 0x4000;
1526 quirk
->data
.address_mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
1527 quirk
->data
.bar
= nr
;
1528 quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1530 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
),
1531 &vfio_generic_window_quirk
, quirk
,
1532 "vfio-ati-bar4-window-quirk", 8);
1533 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1534 quirk
->data
.base_offset
, &quirk
->mem
, 1);
1536 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1538 DPRINTF("Enabled ATI/AMD BAR4 window quirk for device %04x:%02x:%02x.%x\n",
1539 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1540 vdev
->host
.function
);
1544 * Trap the BAR2 MMIO window to config space as well.
1546 static void vfio_probe_ati_bar2_4000_quirk(VFIODevice
*vdev
, int nr
)
1548 PCIDevice
*pdev
= &vdev
->pdev
;
1551 /* Only enable on newer devices where BAR2 is 64bit */
1552 if (!vdev
->has_vga
|| nr
!= 2 || !vdev
->bars
[2].mem64
||
1553 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_ATI
) {
1557 quirk
= g_malloc0(sizeof(*quirk
));
1559 quirk
->data
.flags
= quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1560 quirk
->data
.address_match
= 0x4000;
1561 quirk
->data
.address_mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
1562 quirk
->data
.bar
= nr
;
1564 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_generic_quirk
, quirk
,
1565 "vfio-ati-bar2-4000-quirk",
1566 TARGET_PAGE_ALIGN(quirk
->data
.address_mask
+ 1));
1567 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1568 quirk
->data
.address_match
& TARGET_PAGE_MASK
,
1571 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1573 DPRINTF("Enabled ATI/AMD BAR2 0x4000 quirk for device %04x:%02x:%02x.%x\n",
1574 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1575 vdev
->host
.function
);
1579 * Older ATI/AMD cards like the X550 have a similar window to that above.
1580 * I/O port BAR1 provides a window to a mirror of PCI config space located
1581 * in BAR2 at offset 0xf00. We don't care to support such older cards, but
1582 * note it for future reference.
1585 #define PCI_VENDOR_ID_NVIDIA 0x10de
1588 * Nvidia has several different methods to get to config space, the
1589 * nouveu project has several of these documented here:
1590 * https://github.com/pathscale/envytools/tree/master/hwdocs
1592 * The first quirk is actually not documented in envytools and is found
1593 * on 10de:01d1 (NVIDIA Corporation G72 [GeForce 7300 LE]). This is an
1594 * NV46 chipset. The backdoor uses the legacy VGA I/O ports to access
1595 * the mirror of PCI config space found at BAR0 offset 0x1800. The access
1596 * sequence first writes 0x338 to I/O port 0x3d4. The target offset is
1597 * then written to 0x3d0. Finally 0x538 is written for a read and 0x738
1598 * is written for a write to 0x3d4. The BAR0 offset is then accessible
1599 * through 0x3d0. This quirk doesn't seem to be necessary on newer cards
1600 * that use the I/O port BAR5 window but it doesn't hurt to leave it.
1610 static uint64_t vfio_nvidia_3d0_quirk_read(void *opaque
,
1611 hwaddr addr
, unsigned size
)
1613 VFIOQuirk
*quirk
= opaque
;
1614 VFIODevice
*vdev
= quirk
->vdev
;
1615 PCIDevice
*pdev
= &vdev
->pdev
;
1616 uint64_t data
= vfio_vga_read(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
1617 addr
+ quirk
->data
.base_offset
, size
);
1619 if (quirk
->data
.flags
== NV_3D0_READ
&& addr
== quirk
->data
.data_offset
) {
1620 data
= vfio_pci_read_config(pdev
, quirk
->data
.address_val
, size
);
1621 DPRINTF("%s(0x3d0, %d) = 0x%"PRIx64
"\n", __func__
, size
, data
);
1624 quirk
->data
.flags
= NV_3D0_NONE
;
1629 static void vfio_nvidia_3d0_quirk_write(void *opaque
, hwaddr addr
,
1630 uint64_t data
, unsigned size
)
1632 VFIOQuirk
*quirk
= opaque
;
1633 VFIODevice
*vdev
= quirk
->vdev
;
1634 PCIDevice
*pdev
= &vdev
->pdev
;
1636 switch (quirk
->data
.flags
) {
1638 if (addr
== quirk
->data
.address_offset
&& data
== 0x338) {
1639 quirk
->data
.flags
= NV_3D0_SELECT
;
1643 quirk
->data
.flags
= NV_3D0_NONE
;
1644 if (addr
== quirk
->data
.data_offset
&&
1645 (data
& ~quirk
->data
.address_mask
) == quirk
->data
.address_match
) {
1646 quirk
->data
.flags
= NV_3D0_WINDOW
;
1647 quirk
->data
.address_val
= data
& quirk
->data
.address_mask
;
1651 quirk
->data
.flags
= NV_3D0_NONE
;
1652 if (addr
== quirk
->data
.address_offset
) {
1653 if (data
== 0x538) {
1654 quirk
->data
.flags
= NV_3D0_READ
;
1655 } else if (data
== 0x738) {
1656 quirk
->data
.flags
= NV_3D0_WRITE
;
1661 quirk
->data
.flags
= NV_3D0_NONE
;
1662 if (addr
== quirk
->data
.data_offset
) {
1663 vfio_pci_write_config(pdev
, quirk
->data
.address_val
, data
, size
);
1664 DPRINTF("%s(0x3d0, 0x%"PRIx64
", %d)\n", __func__
, data
, size
);
1670 vfio_vga_write(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
1671 addr
+ quirk
->data
.base_offset
, data
, size
);
1674 static const MemoryRegionOps vfio_nvidia_3d0_quirk
= {
1675 .read
= vfio_nvidia_3d0_quirk_read
,
1676 .write
= vfio_nvidia_3d0_quirk_write
,
1677 .endianness
= DEVICE_LITTLE_ENDIAN
,
1680 static void vfio_vga_probe_nvidia_3d0_quirk(VFIODevice
*vdev
)
1682 PCIDevice
*pdev
= &vdev
->pdev
;
1685 if (pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
||
1686 !vdev
->bars
[1].size
) {
1690 quirk
= g_malloc0(sizeof(*quirk
));
1692 quirk
->data
.base_offset
= 0x10;
1693 quirk
->data
.address_offset
= 4;
1694 quirk
->data
.address_size
= 2;
1695 quirk
->data
.address_match
= 0x1800;
1696 quirk
->data
.address_mask
= PCI_CONFIG_SPACE_SIZE
- 1;
1697 quirk
->data
.data_offset
= 0;
1698 quirk
->data
.data_size
= 4;
1700 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_nvidia_3d0_quirk
,
1701 quirk
, "vfio-nvidia-3d0-quirk", 6);
1702 memory_region_add_subregion(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
1703 quirk
->data
.base_offset
, &quirk
->mem
);
1705 QLIST_INSERT_HEAD(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
,
1708 DPRINTF("Enabled NVIDIA VGA 0x3d0 quirk for device %04x:%02x:%02x.%x\n",
1709 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1710 vdev
->host
.function
);
1714 * The second quirk is documented in envytools. The I/O port BAR5 is just
1715 * a set of address/data ports to the MMIO BARs. The BAR we care about is
1716 * again BAR0. This backdoor is apparently a bit newer than the one above
1717 * so we need to not only trap 256 bytes @0x1800, but all of PCI config
1718 * space, including extended space is available at the 4k @0x88000.
1721 NV_BAR5_ADDRESS
= 0x1,
1722 NV_BAR5_ENABLE
= 0x2,
1723 NV_BAR5_MASTER
= 0x4,
1724 NV_BAR5_VALID
= 0x7,
1727 static void vfio_nvidia_bar5_window_quirk_write(void *opaque
, hwaddr addr
,
1728 uint64_t data
, unsigned size
)
1730 VFIOQuirk
*quirk
= opaque
;
1735 quirk
->data
.flags
|= NV_BAR5_MASTER
;
1737 quirk
->data
.flags
&= ~NV_BAR5_MASTER
;
1742 quirk
->data
.flags
|= NV_BAR5_ENABLE
;
1744 quirk
->data
.flags
&= ~NV_BAR5_ENABLE
;
1748 if (quirk
->data
.flags
& NV_BAR5_MASTER
) {
1749 if ((data
& ~0xfff) == 0x88000) {
1750 quirk
->data
.flags
|= NV_BAR5_ADDRESS
;
1751 quirk
->data
.address_val
= data
& 0xfff;
1752 } else if ((data
& ~0xff) == 0x1800) {
1753 quirk
->data
.flags
|= NV_BAR5_ADDRESS
;
1754 quirk
->data
.address_val
= data
& 0xff;
1756 quirk
->data
.flags
&= ~NV_BAR5_ADDRESS
;
1762 vfio_generic_window_quirk_write(opaque
, addr
, data
, size
);
1765 static const MemoryRegionOps vfio_nvidia_bar5_window_quirk
= {
1766 .read
= vfio_generic_window_quirk_read
,
1767 .write
= vfio_nvidia_bar5_window_quirk_write
,
1768 .valid
.min_access_size
= 4,
1769 .endianness
= DEVICE_LITTLE_ENDIAN
,
1772 static void vfio_probe_nvidia_bar5_window_quirk(VFIODevice
*vdev
, int nr
)
1774 PCIDevice
*pdev
= &vdev
->pdev
;
1777 if (!vdev
->has_vga
|| nr
!= 5 ||
1778 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
) {
1782 quirk
= g_malloc0(sizeof(*quirk
));
1784 quirk
->data
.read_flags
= quirk
->data
.write_flags
= NV_BAR5_VALID
;
1785 quirk
->data
.address_offset
= 0x8;
1786 quirk
->data
.address_size
= 0; /* actually 4, but avoids generic code */
1787 quirk
->data
.data_offset
= 0xc;
1788 quirk
->data
.data_size
= 4;
1789 quirk
->data
.bar
= nr
;
1791 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
),
1792 &vfio_nvidia_bar5_window_quirk
, quirk
,
1793 "vfio-nvidia-bar5-window-quirk", 16);
1794 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
, 0, &quirk
->mem
, 1);
1796 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1798 DPRINTF("Enabled NVIDIA BAR5 window quirk for device %04x:%02x:%02x.%x\n",
1799 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1800 vdev
->host
.function
);
1804 * Finally, BAR0 itself. We want to redirect any accesses to either
1805 * 0x1800 or 0x88000 through the PCI config space access functions.
1807 * NB - quirk at a page granularity or else they don't seem to work when
1810 * Here's offset 0x88000...
1812 static void vfio_probe_nvidia_bar0_88000_quirk(VFIODevice
*vdev
, int nr
)
1814 PCIDevice
*pdev
= &vdev
->pdev
;
1817 if (!vdev
->has_vga
|| nr
!= 0 ||
1818 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
) {
1822 quirk
= g_malloc0(sizeof(*quirk
));
1824 quirk
->data
.flags
= quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1825 quirk
->data
.address_match
= 0x88000;
1826 quirk
->data
.address_mask
= PCIE_CONFIG_SPACE_SIZE
- 1;
1827 quirk
->data
.bar
= nr
;
1829 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_generic_quirk
,
1830 quirk
, "vfio-nvidia-bar0-88000-quirk",
1831 TARGET_PAGE_ALIGN(quirk
->data
.address_mask
+ 1));
1832 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1833 quirk
->data
.address_match
& TARGET_PAGE_MASK
,
1836 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1838 DPRINTF("Enabled NVIDIA BAR0 0x88000 quirk for device %04x:%02x:%02x.%x\n",
1839 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1840 vdev
->host
.function
);
1844 * And here's the same for BAR0 offset 0x1800...
1846 static void vfio_probe_nvidia_bar0_1800_quirk(VFIODevice
*vdev
, int nr
)
1848 PCIDevice
*pdev
= &vdev
->pdev
;
1851 if (!vdev
->has_vga
|| nr
!= 0 ||
1852 pci_get_word(pdev
->config
+ PCI_VENDOR_ID
) != PCI_VENDOR_ID_NVIDIA
) {
1856 /* Log the chipset ID */
1857 DPRINTF("Nvidia NV%02x\n",
1858 (unsigned int)(vfio_bar_read(&vdev
->bars
[0], 0, 4) >> 20) & 0xff);
1860 quirk
= g_malloc0(sizeof(*quirk
));
1862 quirk
->data
.flags
= quirk
->data
.read_flags
= quirk
->data
.write_flags
= 1;
1863 quirk
->data
.address_match
= 0x1800;
1864 quirk
->data
.address_mask
= PCI_CONFIG_SPACE_SIZE
- 1;
1865 quirk
->data
.bar
= nr
;
1867 memory_region_init_io(&quirk
->mem
, OBJECT(vdev
), &vfio_generic_quirk
, quirk
,
1868 "vfio-nvidia-bar0-1800-quirk",
1869 TARGET_PAGE_ALIGN(quirk
->data
.address_mask
+ 1));
1870 memory_region_add_subregion_overlap(&vdev
->bars
[nr
].mem
,
1871 quirk
->data
.address_match
& TARGET_PAGE_MASK
,
1874 QLIST_INSERT_HEAD(&vdev
->bars
[nr
].quirks
, quirk
, next
);
1876 DPRINTF("Enabled NVIDIA BAR0 0x1800 quirk for device %04x:%02x:%02x.%x\n",
1877 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1878 vdev
->host
.function
);
1882 * TODO - Some Nvidia devices provide config access to their companion HDA
1883 * device and even to their parent bridge via these config space mirrors.
1884 * Add quirks for those regions.
1888 * Common quirk probe entry points.
1890 static void vfio_vga_quirk_setup(VFIODevice
*vdev
)
1892 vfio_vga_probe_ati_3c3_quirk(vdev
);
1893 vfio_vga_probe_nvidia_3d0_quirk(vdev
);
1896 static void vfio_vga_quirk_teardown(VFIODevice
*vdev
)
1900 for (i
= 0; i
< ARRAY_SIZE(vdev
->vga
.region
); i
++) {
1901 while (!QLIST_EMPTY(&vdev
->vga
.region
[i
].quirks
)) {
1902 VFIOQuirk
*quirk
= QLIST_FIRST(&vdev
->vga
.region
[i
].quirks
);
1903 memory_region_del_subregion(&vdev
->vga
.region
[i
].mem
, &quirk
->mem
);
1904 QLIST_REMOVE(quirk
, next
);
1910 static void vfio_bar_quirk_setup(VFIODevice
*vdev
, int nr
)
1912 vfio_probe_ati_bar4_window_quirk(vdev
, nr
);
1913 vfio_probe_ati_bar2_4000_quirk(vdev
, nr
);
1914 vfio_probe_nvidia_bar5_window_quirk(vdev
, nr
);
1915 vfio_probe_nvidia_bar0_88000_quirk(vdev
, nr
);
1916 vfio_probe_nvidia_bar0_1800_quirk(vdev
, nr
);
1919 static void vfio_bar_quirk_teardown(VFIODevice
*vdev
, int nr
)
1921 VFIOBAR
*bar
= &vdev
->bars
[nr
];
1923 while (!QLIST_EMPTY(&bar
->quirks
)) {
1924 VFIOQuirk
*quirk
= QLIST_FIRST(&bar
->quirks
);
1925 memory_region_del_subregion(&bar
->mem
, &quirk
->mem
);
1926 QLIST_REMOVE(quirk
, next
);
1934 static uint32_t vfio_pci_read_config(PCIDevice
*pdev
, uint32_t addr
, int len
)
1936 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
1937 uint32_t emu_bits
= 0, emu_val
= 0, phys_val
= 0, val
;
1939 memcpy(&emu_bits
, vdev
->emulated_config_bits
+ addr
, len
);
1940 emu_bits
= le32_to_cpu(emu_bits
);
1943 emu_val
= pci_default_read_config(pdev
, addr
, len
);
1946 if (~emu_bits
& (0xffffffffU
>> (32 - len
* 8))) {
1949 ret
= pread(vdev
->fd
, &phys_val
, len
, vdev
->config_offset
+ addr
);
1951 error_report("%s(%04x:%02x:%02x.%x, 0x%x, 0x%x) failed: %m",
1952 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
1953 vdev
->host
.slot
, vdev
->host
.function
, addr
, len
);
1956 phys_val
= le32_to_cpu(phys_val
);
1959 val
= (emu_val
& emu_bits
) | (phys_val
& ~emu_bits
);
1961 DPRINTF("%s(%04x:%02x:%02x.%x, @0x%x, len=0x%x) %x\n", __func__
,
1962 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1963 vdev
->host
.function
, addr
, len
, val
);
1968 static void vfio_pci_write_config(PCIDevice
*pdev
, uint32_t addr
,
1969 uint32_t val
, int len
)
1971 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
1972 uint32_t val_le
= cpu_to_le32(val
);
1974 DPRINTF("%s(%04x:%02x:%02x.%x, @0x%x, 0x%x, len=0x%x)\n", __func__
,
1975 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
1976 vdev
->host
.function
, addr
, val
, len
);
1978 /* Write everything to VFIO, let it filter out what we can't write */
1979 if (pwrite(vdev
->fd
, &val_le
, len
, vdev
->config_offset
+ addr
) != len
) {
1980 error_report("%s(%04x:%02x:%02x.%x, 0x%x, 0x%x, 0x%x) failed: %m",
1981 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
1982 vdev
->host
.slot
, vdev
->host
.function
, addr
, val
, len
);
1985 /* MSI/MSI-X Enabling/Disabling */
1986 if (pdev
->cap_present
& QEMU_PCI_CAP_MSI
&&
1987 ranges_overlap(addr
, len
, pdev
->msi_cap
, vdev
->msi_cap_size
)) {
1988 int is_enabled
, was_enabled
= msi_enabled(pdev
);
1990 pci_default_write_config(pdev
, addr
, val
, len
);
1992 is_enabled
= msi_enabled(pdev
);
1996 vfio_enable_msi(vdev
);
2000 vfio_disable_msi(vdev
);
2002 vfio_update_msi(vdev
);
2005 } else if (pdev
->cap_present
& QEMU_PCI_CAP_MSIX
&&
2006 ranges_overlap(addr
, len
, pdev
->msix_cap
, MSIX_CAP_LENGTH
)) {
2007 int is_enabled
, was_enabled
= msix_enabled(pdev
);
2009 pci_default_write_config(pdev
, addr
, val
, len
);
2011 is_enabled
= msix_enabled(pdev
);
2013 if (!was_enabled
&& is_enabled
) {
2014 vfio_enable_msix(vdev
);
2015 } else if (was_enabled
&& !is_enabled
) {
2016 vfio_disable_msix(vdev
);
2019 /* Write everything to QEMU to keep emulated bits correct */
2020 pci_default_write_config(pdev
, addr
, val
, len
);
2025 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
2027 static int vfio_dma_unmap(VFIOContainer
*container
,
2028 hwaddr iova
, ram_addr_t size
)
2030 struct vfio_iommu_type1_dma_unmap unmap
= {
2031 .argsz
= sizeof(unmap
),
2037 if (ioctl(container
->fd
, VFIO_IOMMU_UNMAP_DMA
, &unmap
)) {
2038 DPRINTF("VFIO_UNMAP_DMA: %d\n", -errno
);
2045 static int vfio_dma_map(VFIOContainer
*container
, hwaddr iova
,
2046 ram_addr_t size
, void *vaddr
, bool readonly
)
2048 struct vfio_iommu_type1_dma_map map
= {
2049 .argsz
= sizeof(map
),
2050 .flags
= VFIO_DMA_MAP_FLAG_READ
,
2051 .vaddr
= (__u64
)(uintptr_t)vaddr
,
2057 map
.flags
|= VFIO_DMA_MAP_FLAG_WRITE
;
2061 * Try the mapping, if it fails with EBUSY, unmap the region and try
2062 * again. This shouldn't be necessary, but we sometimes see it in
2063 * the the VGA ROM space.
2065 if (ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0 ||
2066 (errno
== EBUSY
&& vfio_dma_unmap(container
, iova
, size
) == 0 &&
2067 ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0)) {
2071 DPRINTF("VFIO_MAP_DMA: %d\n", -errno
);
2075 static bool vfio_listener_skipped_section(MemoryRegionSection
*section
)
2077 return !memory_region_is_ram(section
->mr
);
2080 static void vfio_listener_region_add(MemoryListener
*listener
,
2081 MemoryRegionSection
*section
)
2083 VFIOContainer
*container
= container_of(listener
, VFIOContainer
,
2084 iommu_data
.listener
);
2089 assert(!memory_region_is_iommu(section
->mr
));
2091 if (vfio_listener_skipped_section(section
)) {
2092 DPRINTF("SKIPPING region_add %"HWADDR_PRIx
" - %"PRIx64
"\n",
2093 section
->offset_within_address_space
,
2094 section
->offset_within_address_space
+
2095 int128_get64(int128_sub(section
->size
, int128_one())));
2099 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
2100 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
2101 error_report("%s received unaligned region", __func__
);
2105 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
2106 end
= (section
->offset_within_address_space
+ int128_get64(section
->size
)) &
2113 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
2114 section
->offset_within_region
+
2115 (iova
- section
->offset_within_address_space
);
2117 DPRINTF("region_add %"HWADDR_PRIx
" - %"HWADDR_PRIx
" [%p]\n",
2118 iova
, end
- 1, vaddr
);
2120 memory_region_ref(section
->mr
);
2121 ret
= vfio_dma_map(container
, iova
, end
- iova
, vaddr
, section
->readonly
);
2123 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
2124 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
2125 container
, iova
, end
- iova
, vaddr
, ret
);
2129 static void vfio_listener_region_del(MemoryListener
*listener
,
2130 MemoryRegionSection
*section
)
2132 VFIOContainer
*container
= container_of(listener
, VFIOContainer
,
2133 iommu_data
.listener
);
2137 if (vfio_listener_skipped_section(section
)) {
2138 DPRINTF("SKIPPING region_del %"HWADDR_PRIx
" - %"PRIx64
"\n",
2139 section
->offset_within_address_space
,
2140 section
->offset_within_address_space
+
2141 int128_get64(int128_sub(section
->size
, int128_one())));
2145 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
2146 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
2147 error_report("%s received unaligned region", __func__
);
2151 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
2152 end
= (section
->offset_within_address_space
+ int128_get64(section
->size
)) &
2159 DPRINTF("region_del %"HWADDR_PRIx
" - %"HWADDR_PRIx
"\n",
2162 ret
= vfio_dma_unmap(container
, iova
, end
- iova
);
2163 memory_region_unref(section
->mr
);
2165 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
2166 "0x%"HWADDR_PRIx
") = %d (%m)",
2167 container
, iova
, end
- iova
, ret
);
2171 static MemoryListener vfio_memory_listener
= {
2172 .region_add
= vfio_listener_region_add
,
2173 .region_del
= vfio_listener_region_del
,
2176 static void vfio_listener_release(VFIOContainer
*container
)
2178 memory_listener_unregister(&container
->iommu_data
.listener
);
2184 static void vfio_disable_interrupts(VFIODevice
*vdev
)
2186 switch (vdev
->interrupt
) {
2188 vfio_disable_intx(vdev
);
2191 vfio_disable_msi(vdev
);
2194 vfio_disable_msix(vdev
);
2199 static int vfio_setup_msi(VFIODevice
*vdev
, int pos
)
2202 bool msi_64bit
, msi_maskbit
;
2205 if (pread(vdev
->fd
, &ctrl
, sizeof(ctrl
),
2206 vdev
->config_offset
+ pos
+ PCI_CAP_FLAGS
) != sizeof(ctrl
)) {
2209 ctrl
= le16_to_cpu(ctrl
);
2211 msi_64bit
= !!(ctrl
& PCI_MSI_FLAGS_64BIT
);
2212 msi_maskbit
= !!(ctrl
& PCI_MSI_FLAGS_MASKBIT
);
2213 entries
= 1 << ((ctrl
& PCI_MSI_FLAGS_QMASK
) >> 1);
2215 DPRINTF("%04x:%02x:%02x.%x PCI MSI CAP @0x%x\n", vdev
->host
.domain
,
2216 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
, pos
);
2218 ret
= msi_init(&vdev
->pdev
, pos
, entries
, msi_64bit
, msi_maskbit
);
2220 if (ret
== -ENOTSUP
) {
2223 error_report("vfio: msi_init failed");
2226 vdev
->msi_cap_size
= 0xa + (msi_maskbit
? 0xa : 0) + (msi_64bit
? 0x4 : 0);
2232 * We don't have any control over how pci_add_capability() inserts
2233 * capabilities into the chain. In order to setup MSI-X we need a
2234 * MemoryRegion for the BAR. In order to setup the BAR and not
2235 * attempt to mmap the MSI-X table area, which VFIO won't allow, we
2236 * need to first look for where the MSI-X table lives. So we
2237 * unfortunately split MSI-X setup across two functions.
2239 static int vfio_early_setup_msix(VFIODevice
*vdev
)
2243 uint32_t table
, pba
;
2245 pos
= pci_find_capability(&vdev
->pdev
, PCI_CAP_ID_MSIX
);
2250 if (pread(vdev
->fd
, &ctrl
, sizeof(ctrl
),
2251 vdev
->config_offset
+ pos
+ PCI_CAP_FLAGS
) != sizeof(ctrl
)) {
2255 if (pread(vdev
->fd
, &table
, sizeof(table
),
2256 vdev
->config_offset
+ pos
+ PCI_MSIX_TABLE
) != sizeof(table
)) {
2260 if (pread(vdev
->fd
, &pba
, sizeof(pba
),
2261 vdev
->config_offset
+ pos
+ PCI_MSIX_PBA
) != sizeof(pba
)) {
2265 ctrl
= le16_to_cpu(ctrl
);
2266 table
= le32_to_cpu(table
);
2267 pba
= le32_to_cpu(pba
);
2269 vdev
->msix
= g_malloc0(sizeof(*(vdev
->msix
)));
2270 vdev
->msix
->table_bar
= table
& PCI_MSIX_FLAGS_BIRMASK
;
2271 vdev
->msix
->table_offset
= table
& ~PCI_MSIX_FLAGS_BIRMASK
;
2272 vdev
->msix
->pba_bar
= pba
& PCI_MSIX_FLAGS_BIRMASK
;
2273 vdev
->msix
->pba_offset
= pba
& ~PCI_MSIX_FLAGS_BIRMASK
;
2274 vdev
->msix
->entries
= (ctrl
& PCI_MSIX_FLAGS_QSIZE
) + 1;
2276 DPRINTF("%04x:%02x:%02x.%x "
2277 "PCI MSI-X CAP @0x%x, BAR %d, offset 0x%x, entries %d\n",
2278 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2279 vdev
->host
.function
, pos
, vdev
->msix
->table_bar
,
2280 vdev
->msix
->table_offset
, vdev
->msix
->entries
);
2285 static int vfio_setup_msix(VFIODevice
*vdev
, int pos
)
2289 ret
= msix_init(&vdev
->pdev
, vdev
->msix
->entries
,
2290 &vdev
->bars
[vdev
->msix
->table_bar
].mem
,
2291 vdev
->msix
->table_bar
, vdev
->msix
->table_offset
,
2292 &vdev
->bars
[vdev
->msix
->pba_bar
].mem
,
2293 vdev
->msix
->pba_bar
, vdev
->msix
->pba_offset
, pos
);
2295 if (ret
== -ENOTSUP
) {
2298 error_report("vfio: msix_init failed");
2305 static void vfio_teardown_msi(VFIODevice
*vdev
)
2307 msi_uninit(&vdev
->pdev
);
2310 msix_uninit(&vdev
->pdev
, &vdev
->bars
[vdev
->msix
->table_bar
].mem
,
2311 &vdev
->bars
[vdev
->msix
->pba_bar
].mem
);
2318 static void vfio_mmap_set_enabled(VFIODevice
*vdev
, bool enabled
)
2322 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
2323 VFIOBAR
*bar
= &vdev
->bars
[i
];
2329 memory_region_set_enabled(&bar
->mmap_mem
, enabled
);
2330 if (vdev
->msix
&& vdev
->msix
->table_bar
== i
) {
2331 memory_region_set_enabled(&vdev
->msix
->mmap_mem
, enabled
);
2336 static void vfio_unmap_bar(VFIODevice
*vdev
, int nr
)
2338 VFIOBAR
*bar
= &vdev
->bars
[nr
];
2344 vfio_bar_quirk_teardown(vdev
, nr
);
2346 memory_region_del_subregion(&bar
->mem
, &bar
->mmap_mem
);
2347 munmap(bar
->mmap
, memory_region_size(&bar
->mmap_mem
));
2349 if (vdev
->msix
&& vdev
->msix
->table_bar
== nr
) {
2350 memory_region_del_subregion(&bar
->mem
, &vdev
->msix
->mmap_mem
);
2351 munmap(vdev
->msix
->mmap
, memory_region_size(&vdev
->msix
->mmap_mem
));
2354 memory_region_destroy(&bar
->mem
);
2357 static int vfio_mmap_bar(VFIODevice
*vdev
, VFIOBAR
*bar
,
2358 MemoryRegion
*mem
, MemoryRegion
*submem
,
2359 void **map
, size_t size
, off_t offset
,
2364 if (VFIO_ALLOW_MMAP
&& size
&& bar
->flags
& VFIO_REGION_INFO_FLAG_MMAP
) {
2367 if (bar
->flags
& VFIO_REGION_INFO_FLAG_READ
) {
2371 if (bar
->flags
& VFIO_REGION_INFO_FLAG_WRITE
) {
2375 *map
= mmap(NULL
, size
, prot
, MAP_SHARED
,
2376 bar
->fd
, bar
->fd_offset
+ offset
);
2377 if (*map
== MAP_FAILED
) {
2383 memory_region_init_ram_ptr(submem
, OBJECT(vdev
), name
, size
, *map
);
2386 /* Create a zero sized sub-region to make cleanup easy. */
2387 memory_region_init(submem
, OBJECT(vdev
), name
, 0);
2390 memory_region_add_subregion(mem
, offset
, submem
);
2395 static void vfio_map_bar(VFIODevice
*vdev
, int nr
)
2397 VFIOBAR
*bar
= &vdev
->bars
[nr
];
2398 unsigned size
= bar
->size
;
2404 /* Skip both unimplemented BARs and the upper half of 64bit BARS. */
2409 snprintf(name
, sizeof(name
), "VFIO %04x:%02x:%02x.%x BAR %d",
2410 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2411 vdev
->host
.function
, nr
);
2413 /* Determine what type of BAR this is for registration */
2414 ret
= pread(vdev
->fd
, &pci_bar
, sizeof(pci_bar
),
2415 vdev
->config_offset
+ PCI_BASE_ADDRESS_0
+ (4 * nr
));
2416 if (ret
!= sizeof(pci_bar
)) {
2417 error_report("vfio: Failed to read BAR %d (%m)", nr
);
2421 pci_bar
= le32_to_cpu(pci_bar
);
2422 bar
->ioport
= (pci_bar
& PCI_BASE_ADDRESS_SPACE_IO
);
2423 bar
->mem64
= bar
->ioport
? 0 : (pci_bar
& PCI_BASE_ADDRESS_MEM_TYPE_64
);
2424 type
= pci_bar
& (bar
->ioport
? ~PCI_BASE_ADDRESS_IO_MASK
:
2425 ~PCI_BASE_ADDRESS_MEM_MASK
);
2427 /* A "slow" read/write mapping underlies all BARs */
2428 memory_region_init_io(&bar
->mem
, OBJECT(vdev
), &vfio_bar_ops
,
2430 pci_register_bar(&vdev
->pdev
, nr
, type
, &bar
->mem
);
2433 * We can't mmap areas overlapping the MSIX vector table, so we
2434 * potentially insert a direct-mapped subregion before and after it.
2436 if (vdev
->msix
&& vdev
->msix
->table_bar
== nr
) {
2437 size
= vdev
->msix
->table_offset
& TARGET_PAGE_MASK
;
2440 strncat(name
, " mmap", sizeof(name
) - strlen(name
) - 1);
2441 if (vfio_mmap_bar(vdev
, bar
, &bar
->mem
,
2442 &bar
->mmap_mem
, &bar
->mmap
, size
, 0, name
)) {
2443 error_report("%s unsupported. Performance may be slow", name
);
2446 if (vdev
->msix
&& vdev
->msix
->table_bar
== nr
) {
2449 start
= TARGET_PAGE_ALIGN(vdev
->msix
->table_offset
+
2450 (vdev
->msix
->entries
* PCI_MSIX_ENTRY_SIZE
));
2452 size
= start
< bar
->size
? bar
->size
- start
: 0;
2453 strncat(name
, " msix-hi", sizeof(name
) - strlen(name
) - 1);
2454 /* VFIOMSIXInfo contains another MemoryRegion for this mapping */
2455 if (vfio_mmap_bar(vdev
, bar
, &bar
->mem
, &vdev
->msix
->mmap_mem
,
2456 &vdev
->msix
->mmap
, size
, start
, name
)) {
2457 error_report("%s unsupported. Performance may be slow", name
);
2461 vfio_bar_quirk_setup(vdev
, nr
);
2464 static void vfio_map_bars(VFIODevice
*vdev
)
2468 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
2469 vfio_map_bar(vdev
, i
);
2472 if (vdev
->has_vga
) {
2473 memory_region_init_io(&vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].mem
,
2474 OBJECT(vdev
), &vfio_vga_ops
,
2475 &vdev
->vga
.region
[QEMU_PCI_VGA_MEM
],
2476 "vfio-vga-mmio@0xa0000",
2477 QEMU_PCI_VGA_MEM_SIZE
);
2478 memory_region_init_io(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].mem
,
2479 OBJECT(vdev
), &vfio_vga_ops
,
2480 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
],
2481 "vfio-vga-io@0x3b0",
2482 QEMU_PCI_VGA_IO_LO_SIZE
);
2483 memory_region_init_io(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
,
2484 OBJECT(vdev
), &vfio_vga_ops
,
2485 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
],
2486 "vfio-vga-io@0x3c0",
2487 QEMU_PCI_VGA_IO_HI_SIZE
);
2489 pci_register_vga(&vdev
->pdev
, &vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].mem
,
2490 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].mem
,
2491 &vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
);
2492 vfio_vga_quirk_setup(vdev
);
2496 static void vfio_unmap_bars(VFIODevice
*vdev
)
2500 for (i
= 0; i
< PCI_ROM_SLOT
; i
++) {
2501 vfio_unmap_bar(vdev
, i
);
2504 if (vdev
->has_vga
) {
2505 vfio_vga_quirk_teardown(vdev
);
2506 pci_unregister_vga(&vdev
->pdev
);
2507 memory_region_destroy(&vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].mem
);
2508 memory_region_destroy(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].mem
);
2509 memory_region_destroy(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].mem
);
2516 static uint8_t vfio_std_cap_max_size(PCIDevice
*pdev
, uint8_t pos
)
2518 uint8_t tmp
, next
= 0xff;
2520 for (tmp
= pdev
->config
[PCI_CAPABILITY_LIST
]; tmp
;
2521 tmp
= pdev
->config
[tmp
+ 1]) {
2522 if (tmp
> pos
&& tmp
< next
) {
2530 static void vfio_set_word_bits(uint8_t *buf
, uint16_t val
, uint16_t mask
)
2532 pci_set_word(buf
, (pci_get_word(buf
) & ~mask
) | val
);
2535 static void vfio_add_emulated_word(VFIODevice
*vdev
, int pos
,
2536 uint16_t val
, uint16_t mask
)
2538 vfio_set_word_bits(vdev
->pdev
.config
+ pos
, val
, mask
);
2539 vfio_set_word_bits(vdev
->pdev
.wmask
+ pos
, ~mask
, mask
);
2540 vfio_set_word_bits(vdev
->emulated_config_bits
+ pos
, mask
, mask
);
2543 static void vfio_set_long_bits(uint8_t *buf
, uint32_t val
, uint32_t mask
)
2545 pci_set_long(buf
, (pci_get_long(buf
) & ~mask
) | val
);
2548 static void vfio_add_emulated_long(VFIODevice
*vdev
, int pos
,
2549 uint32_t val
, uint32_t mask
)
2551 vfio_set_long_bits(vdev
->pdev
.config
+ pos
, val
, mask
);
2552 vfio_set_long_bits(vdev
->pdev
.wmask
+ pos
, ~mask
, mask
);
2553 vfio_set_long_bits(vdev
->emulated_config_bits
+ pos
, mask
, mask
);
2556 static int vfio_setup_pcie_cap(VFIODevice
*vdev
, int pos
, uint8_t size
)
2561 flags
= pci_get_word(vdev
->pdev
.config
+ pos
+ PCI_CAP_FLAGS
);
2562 type
= (flags
& PCI_EXP_FLAGS_TYPE
) >> 4;
2564 if (type
!= PCI_EXP_TYPE_ENDPOINT
&&
2565 type
!= PCI_EXP_TYPE_LEG_END
&&
2566 type
!= PCI_EXP_TYPE_RC_END
) {
2568 error_report("vfio: Assignment of PCIe type 0x%x "
2569 "devices is not currently supported", type
);
2573 if (!pci_bus_is_express(vdev
->pdev
.bus
)) {
2575 * Use express capability as-is on PCI bus. It doesn't make much
2576 * sense to even expose, but some drivers (ex. tg3) depend on it
2577 * and guests don't seem to be particular about it. We'll need
2578 * to revist this or force express devices to express buses if we
2579 * ever expose an IOMMU to the guest.
2581 } else if (pci_bus_is_root(vdev
->pdev
.bus
)) {
2583 * On a Root Complex bus Endpoints become Root Complex Integrated
2584 * Endpoints, which changes the type and clears the LNK & LNK2 fields.
2586 if (type
== PCI_EXP_TYPE_ENDPOINT
) {
2587 vfio_add_emulated_word(vdev
, pos
+ PCI_CAP_FLAGS
,
2588 PCI_EXP_TYPE_RC_END
<< 4,
2589 PCI_EXP_FLAGS_TYPE
);
2591 /* Link Capabilities, Status, and Control goes away */
2592 if (size
> PCI_EXP_LNKCTL
) {
2593 vfio_add_emulated_long(vdev
, pos
+ PCI_EXP_LNKCAP
, 0, ~0);
2594 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKCTL
, 0, ~0);
2595 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKSTA
, 0, ~0);
2597 #ifndef PCI_EXP_LNKCAP2
2598 #define PCI_EXP_LNKCAP2 44
2600 #ifndef PCI_EXP_LNKSTA2
2601 #define PCI_EXP_LNKSTA2 50
2603 /* Link 2 Capabilities, Status, and Control goes away */
2604 if (size
> PCI_EXP_LNKCAP2
) {
2605 vfio_add_emulated_long(vdev
, pos
+ PCI_EXP_LNKCAP2
, 0, ~0);
2606 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKCTL2
, 0, ~0);
2607 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKSTA2
, 0, ~0);
2611 } else if (type
== PCI_EXP_TYPE_LEG_END
) {
2613 * Legacy endpoints don't belong on the root complex. Windows
2614 * seems to be happier with devices if we skip the capability.
2621 * Convert Root Complex Integrated Endpoints to regular endpoints.
2622 * These devices don't support LNK/LNK2 capabilities, so make them up.
2624 if (type
== PCI_EXP_TYPE_RC_END
) {
2625 vfio_add_emulated_word(vdev
, pos
+ PCI_CAP_FLAGS
,
2626 PCI_EXP_TYPE_ENDPOINT
<< 4,
2627 PCI_EXP_FLAGS_TYPE
);
2628 vfio_add_emulated_long(vdev
, pos
+ PCI_EXP_LNKCAP
,
2629 PCI_EXP_LNK_MLW_1
| PCI_EXP_LNK_LS_25
, ~0);
2630 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKCTL
, 0, ~0);
2633 /* Mark the Link Status bits as emulated to allow virtual negotiation */
2634 vfio_add_emulated_word(vdev
, pos
+ PCI_EXP_LNKSTA
,
2635 pci_get_word(vdev
->pdev
.config
+ pos
+
2637 PCI_EXP_LNKCAP_MLW
| PCI_EXP_LNKCAP_SLS
);
2640 pos
= pci_add_capability(&vdev
->pdev
, PCI_CAP_ID_EXP
, pos
, size
);
2642 vdev
->pdev
.exp
.exp_cap
= pos
;
2648 static void vfio_check_pcie_flr(VFIODevice
*vdev
, uint8_t pos
)
2650 uint32_t cap
= pci_get_long(vdev
->pdev
.config
+ pos
+ PCI_EXP_DEVCAP
);
2652 if (cap
& PCI_EXP_DEVCAP_FLR
) {
2653 DPRINTF("%04x:%02x:%02x.%x Supports FLR via PCIe cap\n",
2654 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2655 vdev
->host
.function
);
2656 vdev
->has_flr
= true;
2660 static void vfio_check_pm_reset(VFIODevice
*vdev
, uint8_t pos
)
2662 uint16_t csr
= pci_get_word(vdev
->pdev
.config
+ pos
+ PCI_PM_CTRL
);
2664 if (!(csr
& PCI_PM_CTRL_NO_SOFT_RESET
)) {
2665 DPRINTF("%04x:%02x:%02x.%x Supports PM reset\n",
2666 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2667 vdev
->host
.function
);
2668 vdev
->has_pm_reset
= true;
2672 static void vfio_check_af_flr(VFIODevice
*vdev
, uint8_t pos
)
2674 uint8_t cap
= pci_get_byte(vdev
->pdev
.config
+ pos
+ PCI_AF_CAP
);
2676 if ((cap
& PCI_AF_CAP_TP
) && (cap
& PCI_AF_CAP_FLR
)) {
2677 DPRINTF("%04x:%02x:%02x.%x Supports FLR via AF cap\n",
2678 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2679 vdev
->host
.function
);
2680 vdev
->has_flr
= true;
2684 static int vfio_add_std_cap(VFIODevice
*vdev
, uint8_t pos
)
2686 PCIDevice
*pdev
= &vdev
->pdev
;
2687 uint8_t cap_id
, next
, size
;
2690 cap_id
= pdev
->config
[pos
];
2691 next
= pdev
->config
[pos
+ 1];
2694 * If it becomes important to configure capabilities to their actual
2695 * size, use this as the default when it's something we don't recognize.
2696 * Since QEMU doesn't actually handle many of the config accesses,
2697 * exact size doesn't seem worthwhile.
2699 size
= vfio_std_cap_max_size(pdev
, pos
);
2702 * pci_add_capability always inserts the new capability at the head
2703 * of the chain. Therefore to end up with a chain that matches the
2704 * physical device, we insert from the end by making this recursive.
2705 * This is also why we pre-caclulate size above as cached config space
2706 * will be changed as we unwind the stack.
2709 ret
= vfio_add_std_cap(vdev
, next
);
2714 /* Begin the rebuild, use QEMU emulated list bits */
2715 pdev
->config
[PCI_CAPABILITY_LIST
] = 0;
2716 vdev
->emulated_config_bits
[PCI_CAPABILITY_LIST
] = 0xff;
2717 vdev
->emulated_config_bits
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
2720 /* Use emulated next pointer to allow dropping caps */
2721 pci_set_byte(vdev
->emulated_config_bits
+ pos
+ 1, 0xff);
2724 case PCI_CAP_ID_MSI
:
2725 ret
= vfio_setup_msi(vdev
, pos
);
2727 case PCI_CAP_ID_EXP
:
2728 vfio_check_pcie_flr(vdev
, pos
);
2729 ret
= vfio_setup_pcie_cap(vdev
, pos
, size
);
2731 case PCI_CAP_ID_MSIX
:
2732 ret
= vfio_setup_msix(vdev
, pos
);
2735 vfio_check_pm_reset(vdev
, pos
);
2737 ret
= pci_add_capability(pdev
, cap_id
, pos
, size
);
2740 vfio_check_af_flr(vdev
, pos
);
2741 ret
= pci_add_capability(pdev
, cap_id
, pos
, size
);
2744 ret
= pci_add_capability(pdev
, cap_id
, pos
, size
);
2749 error_report("vfio: %04x:%02x:%02x.%x Error adding PCI capability "
2750 "0x%x[0x%x]@0x%x: %d", vdev
->host
.domain
,
2751 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
2752 cap_id
, size
, pos
, ret
);
2759 static int vfio_add_capabilities(VFIODevice
*vdev
)
2761 PCIDevice
*pdev
= &vdev
->pdev
;
2763 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
) ||
2764 !pdev
->config
[PCI_CAPABILITY_LIST
]) {
2765 return 0; /* Nothing to add */
2768 return vfio_add_std_cap(vdev
, pdev
->config
[PCI_CAPABILITY_LIST
]);
2771 static void vfio_pci_pre_reset(VFIODevice
*vdev
)
2773 PCIDevice
*pdev
= &vdev
->pdev
;
2776 vfio_disable_interrupts(vdev
);
2778 /* Make sure the device is in D0 */
2783 pmcsr
= vfio_pci_read_config(pdev
, vdev
->pm_cap
+ PCI_PM_CTRL
, 2);
2784 state
= pmcsr
& PCI_PM_CTRL_STATE_MASK
;
2786 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
2787 vfio_pci_write_config(pdev
, vdev
->pm_cap
+ PCI_PM_CTRL
, pmcsr
, 2);
2788 /* vfio handles the necessary delay here */
2789 pmcsr
= vfio_pci_read_config(pdev
, vdev
->pm_cap
+ PCI_PM_CTRL
, 2);
2790 state
= pmcsr
& PCI_PM_CTRL_STATE_MASK
;
2792 error_report("vfio: Unable to power on device, stuck in D%d\n",
2799 * Stop any ongoing DMA by disconecting I/O, MMIO, and bus master.
2800 * Also put INTx Disable in known state.
2802 cmd
= vfio_pci_read_config(pdev
, PCI_COMMAND
, 2);
2803 cmd
&= ~(PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
|
2804 PCI_COMMAND_INTX_DISABLE
);
2805 vfio_pci_write_config(pdev
, PCI_COMMAND
, cmd
, 2);
2808 static void vfio_pci_post_reset(VFIODevice
*vdev
)
2810 vfio_enable_intx(vdev
);
2813 static bool vfio_pci_host_match(PCIHostDeviceAddress
*host1
,
2814 PCIHostDeviceAddress
*host2
)
2816 return (host1
->domain
== host2
->domain
&& host1
->bus
== host2
->bus
&&
2817 host1
->slot
== host2
->slot
&& host1
->function
== host2
->function
);
2820 static int vfio_pci_hot_reset(VFIODevice
*vdev
, bool single
)
2823 struct vfio_pci_hot_reset_info
*info
;
2824 struct vfio_pci_dependent_device
*devices
;
2825 struct vfio_pci_hot_reset
*reset
;
2830 DPRINTF("%s(%04x:%02x:%02x.%x) %s\n", __func__
, vdev
->host
.domain
,
2831 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
2832 single
? "one" : "multi");
2834 vfio_pci_pre_reset(vdev
);
2835 vdev
->needs_reset
= false;
2837 info
= g_malloc0(sizeof(*info
));
2838 info
->argsz
= sizeof(*info
);
2840 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
, info
);
2841 if (ret
&& errno
!= ENOSPC
) {
2843 if (!vdev
->has_pm_reset
) {
2844 error_report("vfio: Cannot reset device %04x:%02x:%02x.%x, "
2845 "no available reset mechanism.", vdev
->host
.domain
,
2846 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
2851 count
= info
->count
;
2852 info
= g_realloc(info
, sizeof(*info
) + (count
* sizeof(*devices
)));
2853 info
->argsz
= sizeof(*info
) + (count
* sizeof(*devices
));
2854 devices
= &info
->devices
[0];
2856 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO
, info
);
2859 error_report("vfio: hot reset info failed: %m");
2863 DPRINTF("%04x:%02x:%02x.%x: hot reset dependent devices:\n",
2864 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2865 vdev
->host
.function
);
2867 /* Verify that we have all the groups required */
2868 for (i
= 0; i
< info
->count
; i
++) {
2869 PCIHostDeviceAddress host
;
2872 host
.domain
= devices
[i
].segment
;
2873 host
.bus
= devices
[i
].bus
;
2874 host
.slot
= PCI_SLOT(devices
[i
].devfn
);
2875 host
.function
= PCI_FUNC(devices
[i
].devfn
);
2877 DPRINTF("\t%04x:%02x:%02x.%x group %d\n", host
.domain
,
2878 host
.bus
, host
.slot
, host
.function
, devices
[i
].group_id
);
2880 if (vfio_pci_host_match(&host
, &vdev
->host
)) {
2884 QLIST_FOREACH(group
, &group_list
, next
) {
2885 if (group
->groupid
== devices
[i
].group_id
) {
2891 if (!vdev
->has_pm_reset
) {
2892 error_report("vfio: Cannot reset device %04x:%02x:%02x.%x, "
2893 "depends on group %d which is not owned.",
2894 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
2895 vdev
->host
.function
, devices
[i
].group_id
);
2901 /* Prep dependent devices for reset and clear our marker. */
2902 QLIST_FOREACH(tmp
, &group
->device_list
, next
) {
2903 if (vfio_pci_host_match(&host
, &tmp
->host
)) {
2905 DPRINTF("vfio: found another in-use device "
2906 "%04x:%02x:%02x.%x\n", host
.domain
, host
.bus
,
2907 host
.slot
, host
.function
);
2911 vfio_pci_pre_reset(tmp
);
2912 tmp
->needs_reset
= false;
2919 if (!single
&& !multi
) {
2920 DPRINTF("vfio: No other in-use devices for multi hot reset\n");
2925 /* Determine how many group fds need to be passed */
2927 QLIST_FOREACH(group
, &group_list
, next
) {
2928 for (i
= 0; i
< info
->count
; i
++) {
2929 if (group
->groupid
== devices
[i
].group_id
) {
2936 reset
= g_malloc0(sizeof(*reset
) + (count
* sizeof(*fds
)));
2937 reset
->argsz
= sizeof(*reset
) + (count
* sizeof(*fds
));
2938 fds
= &reset
->group_fds
[0];
2940 /* Fill in group fds */
2941 QLIST_FOREACH(group
, &group_list
, next
) {
2942 for (i
= 0; i
< info
->count
; i
++) {
2943 if (group
->groupid
== devices
[i
].group_id
) {
2944 fds
[reset
->count
++] = group
->fd
;
2951 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_PCI_HOT_RESET
, reset
);
2954 DPRINTF("%04x:%02x:%02x.%x hot reset: %s\n", vdev
->host
.domain
,
2955 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
,
2956 ret
? "%m" : "Success");
2959 /* Re-enable INTx on affected devices */
2960 for (i
= 0; i
< info
->count
; i
++) {
2961 PCIHostDeviceAddress host
;
2964 host
.domain
= devices
[i
].segment
;
2965 host
.bus
= devices
[i
].bus
;
2966 host
.slot
= PCI_SLOT(devices
[i
].devfn
);
2967 host
.function
= PCI_FUNC(devices
[i
].devfn
);
2969 if (vfio_pci_host_match(&host
, &vdev
->host
)) {
2973 QLIST_FOREACH(group
, &group_list
, next
) {
2974 if (group
->groupid
== devices
[i
].group_id
) {
2983 QLIST_FOREACH(tmp
, &group
->device_list
, next
) {
2984 if (vfio_pci_host_match(&host
, &tmp
->host
)) {
2985 vfio_pci_post_reset(tmp
);
2991 vfio_pci_post_reset(vdev
);
2998 * We want to differentiate hot reset of mulitple in-use devices vs hot reset
2999 * of a single in-use device. VFIO_DEVICE_RESET will already handle the case
3000 * of doing hot resets when there is only a single device per bus. The in-use
3001 * here refers to how many VFIODevices are affected. A hot reset that affects
3002 * multiple devices, but only a single in-use device, means that we can call
3003 * it from our bus ->reset() callback since the extent is effectively a single
3004 * device. This allows us to make use of it in the hotplug path. When there
3005 * are multiple in-use devices, we can only trigger the hot reset during a
3006 * system reset and thus from our reset handler. We separate _one vs _multi
3007 * here so that we don't overlap and do a double reset on the system reset
3008 * path where both our reset handler and ->reset() callback are used. Calling
3009 * _one() will only do a hot reset for the one in-use devices case, calling
3010 * _multi() will do nothing if a _one() would have been sufficient.
3012 static int vfio_pci_hot_reset_one(VFIODevice
*vdev
)
3014 return vfio_pci_hot_reset(vdev
, true);
3017 static int vfio_pci_hot_reset_multi(VFIODevice
*vdev
)
3019 return vfio_pci_hot_reset(vdev
, false);
3022 static void vfio_pci_reset_handler(void *opaque
)
3027 QLIST_FOREACH(group
, &group_list
, next
) {
3028 QLIST_FOREACH(vdev
, &group
->device_list
, next
) {
3029 if (!vdev
->reset_works
|| (!vdev
->has_flr
&& vdev
->has_pm_reset
)) {
3030 vdev
->needs_reset
= true;
3035 QLIST_FOREACH(group
, &group_list
, next
) {
3036 QLIST_FOREACH(vdev
, &group
->device_list
, next
) {
3037 if (vdev
->needs_reset
) {
3038 vfio_pci_hot_reset_multi(vdev
);
3044 static int vfio_connect_container(VFIOGroup
*group
)
3046 VFIOContainer
*container
;
3049 if (group
->container
) {
3053 QLIST_FOREACH(container
, &container_list
, next
) {
3054 if (!ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &container
->fd
)) {
3055 group
->container
= container
;
3056 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
3061 fd
= qemu_open("/dev/vfio/vfio", O_RDWR
);
3063 error_report("vfio: failed to open /dev/vfio/vfio: %m");
3067 ret
= ioctl(fd
, VFIO_GET_API_VERSION
);
3068 if (ret
!= VFIO_API_VERSION
) {
3069 error_report("vfio: supported vfio version: %d, "
3070 "reported version: %d", VFIO_API_VERSION
, ret
);
3075 container
= g_malloc0(sizeof(*container
));
3078 if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1_IOMMU
)) {
3079 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
3081 error_report("vfio: failed to set group container: %m");
3087 ret
= ioctl(fd
, VFIO_SET_IOMMU
, VFIO_TYPE1_IOMMU
);
3089 error_report("vfio: failed to set iommu for container: %m");
3095 container
->iommu_data
.listener
= vfio_memory_listener
;
3096 container
->iommu_data
.release
= vfio_listener_release
;
3098 memory_listener_register(&container
->iommu_data
.listener
, &address_space_memory
);
3100 error_report("vfio: No available IOMMU models");
3106 QLIST_INIT(&container
->group_list
);
3107 QLIST_INSERT_HEAD(&container_list
, container
, next
);
3109 group
->container
= container
;
3110 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
3115 static void vfio_disconnect_container(VFIOGroup
*group
)
3117 VFIOContainer
*container
= group
->container
;
3119 if (ioctl(group
->fd
, VFIO_GROUP_UNSET_CONTAINER
, &container
->fd
)) {
3120 error_report("vfio: error disconnecting group %d from container",
3124 QLIST_REMOVE(group
, container_next
);
3125 group
->container
= NULL
;
3127 if (QLIST_EMPTY(&container
->group_list
)) {
3128 if (container
->iommu_data
.release
) {
3129 container
->iommu_data
.release(container
);
3131 QLIST_REMOVE(container
, next
);
3132 DPRINTF("vfio_disconnect_container: close container->fd\n");
3133 close(container
->fd
);
3138 static VFIOGroup
*vfio_get_group(int groupid
)
3142 struct vfio_group_status status
= { .argsz
= sizeof(status
) };
3144 QLIST_FOREACH(group
, &group_list
, next
) {
3145 if (group
->groupid
== groupid
) {
3150 group
= g_malloc0(sizeof(*group
));
3152 snprintf(path
, sizeof(path
), "/dev/vfio/%d", groupid
);
3153 group
->fd
= qemu_open(path
, O_RDWR
);
3154 if (group
->fd
< 0) {
3155 error_report("vfio: error opening %s: %m", path
);
3160 if (ioctl(group
->fd
, VFIO_GROUP_GET_STATUS
, &status
)) {
3161 error_report("vfio: error getting group status: %m");
3167 if (!(status
.flags
& VFIO_GROUP_FLAGS_VIABLE
)) {
3168 error_report("vfio: error, group %d is not viable, please ensure "
3169 "all devices within the iommu_group are bound to their "
3170 "vfio bus driver.", groupid
);
3176 group
->groupid
= groupid
;
3177 QLIST_INIT(&group
->device_list
);
3179 if (vfio_connect_container(group
)) {
3180 error_report("vfio: failed to setup container for group %d", groupid
);
3186 if (QLIST_EMPTY(&group_list
)) {
3187 qemu_register_reset(vfio_pci_reset_handler
, NULL
);
3190 QLIST_INSERT_HEAD(&group_list
, group
, next
);
3195 static void vfio_put_group(VFIOGroup
*group
)
3197 if (!QLIST_EMPTY(&group
->device_list
)) {
3201 vfio_disconnect_container(group
);
3202 QLIST_REMOVE(group
, next
);
3203 DPRINTF("vfio_put_group: close group->fd\n");
3207 if (QLIST_EMPTY(&group_list
)) {
3208 qemu_unregister_reset(vfio_pci_reset_handler
, NULL
);
3212 static int vfio_get_device(VFIOGroup
*group
, const char *name
, VFIODevice
*vdev
)
3214 struct vfio_device_info dev_info
= { .argsz
= sizeof(dev_info
) };
3215 struct vfio_region_info reg_info
= { .argsz
= sizeof(reg_info
) };
3216 struct vfio_irq_info irq_info
= { .argsz
= sizeof(irq_info
) };
3219 ret
= ioctl(group
->fd
, VFIO_GROUP_GET_DEVICE_FD
, name
);
3221 error_report("vfio: error getting device %s from group %d: %m",
3222 name
, group
->groupid
);
3223 error_printf("Verify all devices in group %d are bound to vfio-pci "
3224 "or pci-stub and not already in use\n", group
->groupid
);
3229 vdev
->group
= group
;
3230 QLIST_INSERT_HEAD(&group
->device_list
, vdev
, next
);
3232 /* Sanity check device */
3233 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_INFO
, &dev_info
);
3235 error_report("vfio: error getting device info: %m");
3239 DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name
,
3240 dev_info
.flags
, dev_info
.num_regions
, dev_info
.num_irqs
);
3242 if (!(dev_info
.flags
& VFIO_DEVICE_FLAGS_PCI
)) {
3243 error_report("vfio: Um, this isn't a PCI device");
3247 vdev
->reset_works
= !!(dev_info
.flags
& VFIO_DEVICE_FLAGS_RESET
);
3249 if (dev_info
.num_regions
< VFIO_PCI_CONFIG_REGION_INDEX
+ 1) {
3250 error_report("vfio: unexpected number of io regions %u",
3251 dev_info
.num_regions
);
3255 if (dev_info
.num_irqs
< VFIO_PCI_MSIX_IRQ_INDEX
+ 1) {
3256 error_report("vfio: unexpected number of irqs %u", dev_info
.num_irqs
);
3260 for (i
= VFIO_PCI_BAR0_REGION_INDEX
; i
< VFIO_PCI_ROM_REGION_INDEX
; i
++) {
3263 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
);
3265 error_report("vfio: Error getting region %d info: %m", i
);
3269 DPRINTF("Device %s region %d:\n", name
, i
);
3270 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
3271 (unsigned long)reg_info
.size
, (unsigned long)reg_info
.offset
,
3272 (unsigned long)reg_info
.flags
);
3274 vdev
->bars
[i
].flags
= reg_info
.flags
;
3275 vdev
->bars
[i
].size
= reg_info
.size
;
3276 vdev
->bars
[i
].fd_offset
= reg_info
.offset
;
3277 vdev
->bars
[i
].fd
= vdev
->fd
;
3278 vdev
->bars
[i
].nr
= i
;
3279 QLIST_INIT(&vdev
->bars
[i
].quirks
);
3282 reg_info
.index
= VFIO_PCI_CONFIG_REGION_INDEX
;
3284 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, ®_info
);
3286 error_report("vfio: Error getting config info: %m");
3290 DPRINTF("Device %s config:\n", name
);
3291 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
3292 (unsigned long)reg_info
.size
, (unsigned long)reg_info
.offset
,
3293 (unsigned long)reg_info
.flags
);
3295 vdev
->config_size
= reg_info
.size
;
3296 if (vdev
->config_size
== PCI_CONFIG_SPACE_SIZE
) {
3297 vdev
->pdev
.cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
3299 vdev
->config_offset
= reg_info
.offset
;
3301 if ((vdev
->features
& VFIO_FEATURE_ENABLE_VGA
) &&
3302 dev_info
.num_regions
> VFIO_PCI_VGA_REGION_INDEX
) {
3303 struct vfio_region_info vga_info
= {
3304 .argsz
= sizeof(vga_info
),
3305 .index
= VFIO_PCI_VGA_REGION_INDEX
,
3308 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, &vga_info
);
3311 "vfio: Device does not support requested feature x-vga");
3315 if (!(vga_info
.flags
& VFIO_REGION_INFO_FLAG_READ
) ||
3316 !(vga_info
.flags
& VFIO_REGION_INFO_FLAG_WRITE
) ||
3317 vga_info
.size
< 0xbffff + 1) {
3318 error_report("vfio: Unexpected VGA info, flags 0x%lx, size 0x%lx",
3319 (unsigned long)vga_info
.flags
,
3320 (unsigned long)vga_info
.size
);
3324 vdev
->vga
.fd_offset
= vga_info
.offset
;
3325 vdev
->vga
.fd
= vdev
->fd
;
3327 vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].offset
= QEMU_PCI_VGA_MEM_BASE
;
3328 vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].nr
= QEMU_PCI_VGA_MEM
;
3329 QLIST_INIT(&vdev
->vga
.region
[QEMU_PCI_VGA_MEM
].quirks
);
3331 vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].offset
= QEMU_PCI_VGA_IO_LO_BASE
;
3332 vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].nr
= QEMU_PCI_VGA_IO_LO
;
3333 QLIST_INIT(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_LO
].quirks
);
3335 vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].offset
= QEMU_PCI_VGA_IO_HI_BASE
;
3336 vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].nr
= QEMU_PCI_VGA_IO_HI
;
3337 QLIST_INIT(&vdev
->vga
.region
[QEMU_PCI_VGA_IO_HI
].quirks
);
3339 vdev
->has_vga
= true;
3341 irq_info
.index
= VFIO_PCI_ERR_IRQ_INDEX
;
3343 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
, &irq_info
);
3345 /* This can fail for an old kernel or legacy PCI dev */
3346 DPRINTF("VFIO_DEVICE_GET_IRQ_INFO failure: %m\n");
3348 } else if (irq_info
.count
== 1) {
3349 vdev
->pci_aer
= true;
3351 error_report("vfio: %04x:%02x:%02x.%x "
3352 "Could not enable error recovery for the device",
3353 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3354 vdev
->host
.function
);
3359 QLIST_REMOVE(vdev
, next
);
3366 static void vfio_put_device(VFIODevice
*vdev
)
3368 QLIST_REMOVE(vdev
, next
);
3370 DPRINTF("vfio_put_device: close vdev->fd\n");
3378 static void vfio_err_notifier_handler(void *opaque
)
3380 VFIODevice
*vdev
= opaque
;
3382 if (!event_notifier_test_and_clear(&vdev
->err_notifier
)) {
3387 * TBD. Retrieve the error details and decide what action
3388 * needs to be taken. One of the actions could be to pass
3389 * the error to the guest and have the guest driver recover
3390 * from the error. This requires that PCIe capabilities be
3391 * exposed to the guest. For now, we just terminate the
3392 * guest to contain the error.
3395 error_report("%s(%04x:%02x:%02x.%x) Unrecoverable error detected. "
3396 "Please collect any data possible and then kill the guest",
3397 __func__
, vdev
->host
.domain
, vdev
->host
.bus
,
3398 vdev
->host
.slot
, vdev
->host
.function
);
3400 vm_stop(RUN_STATE_IO_ERROR
);
3404 * Registers error notifier for devices supporting error recovery.
3405 * If we encounter a failure in this function, we report an error
3406 * and continue after disabling error recovery support for the
3409 static void vfio_register_err_notifier(VFIODevice
*vdev
)
3413 struct vfio_irq_set
*irq_set
;
3416 if (!vdev
->pci_aer
) {
3420 if (event_notifier_init(&vdev
->err_notifier
, 0)) {
3421 error_report("vfio: Unable to init event notifier for error detection");
3422 vdev
->pci_aer
= false;
3426 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
3428 irq_set
= g_malloc0(argsz
);
3429 irq_set
->argsz
= argsz
;
3430 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
3431 VFIO_IRQ_SET_ACTION_TRIGGER
;
3432 irq_set
->index
= VFIO_PCI_ERR_IRQ_INDEX
;
3435 pfd
= (int32_t *)&irq_set
->data
;
3437 *pfd
= event_notifier_get_fd(&vdev
->err_notifier
);
3438 qemu_set_fd_handler(*pfd
, vfio_err_notifier_handler
, NULL
, vdev
);
3440 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
3442 error_report("vfio: Failed to set up error notification");
3443 qemu_set_fd_handler(*pfd
, NULL
, NULL
, vdev
);
3444 event_notifier_cleanup(&vdev
->err_notifier
);
3445 vdev
->pci_aer
= false;
3450 static void vfio_unregister_err_notifier(VFIODevice
*vdev
)
3453 struct vfio_irq_set
*irq_set
;
3457 if (!vdev
->pci_aer
) {
3461 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
3463 irq_set
= g_malloc0(argsz
);
3464 irq_set
->argsz
= argsz
;
3465 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
3466 VFIO_IRQ_SET_ACTION_TRIGGER
;
3467 irq_set
->index
= VFIO_PCI_ERR_IRQ_INDEX
;
3470 pfd
= (int32_t *)&irq_set
->data
;
3473 ret
= ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
);
3475 error_report("vfio: Failed to de-assign error fd: %m");
3478 qemu_set_fd_handler(event_notifier_get_fd(&vdev
->err_notifier
),
3480 event_notifier_cleanup(&vdev
->err_notifier
);
3483 static int vfio_initfn(PCIDevice
*pdev
)
3485 VFIODevice
*pvdev
, *vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
3487 char path
[PATH_MAX
], iommu_group_path
[PATH_MAX
], *group_name
;
3493 /* Check that the host device exists */
3494 snprintf(path
, sizeof(path
),
3495 "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/",
3496 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3497 vdev
->host
.function
);
3498 if (stat(path
, &st
) < 0) {
3499 error_report("vfio: error: no such host device: %s", path
);
3503 strncat(path
, "iommu_group", sizeof(path
) - strlen(path
) - 1);
3505 len
= readlink(path
, iommu_group_path
, PATH_MAX
);
3507 error_report("vfio: error no iommu_group for device");
3511 iommu_group_path
[len
] = 0;
3512 group_name
= basename(iommu_group_path
);
3514 if (sscanf(group_name
, "%d", &groupid
) != 1) {
3515 error_report("vfio: error reading %s: %m", path
);
3519 DPRINTF("%s(%04x:%02x:%02x.%x) group %d\n", __func__
, vdev
->host
.domain
,
3520 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
, groupid
);
3522 group
= vfio_get_group(groupid
);
3524 error_report("vfio: failed to get group %d", groupid
);
3528 snprintf(path
, sizeof(path
), "%04x:%02x:%02x.%01x",
3529 vdev
->host
.domain
, vdev
->host
.bus
, vdev
->host
.slot
,
3530 vdev
->host
.function
);
3532 QLIST_FOREACH(pvdev
, &group
->device_list
, next
) {
3533 if (pvdev
->host
.domain
== vdev
->host
.domain
&&
3534 pvdev
->host
.bus
== vdev
->host
.bus
&&
3535 pvdev
->host
.slot
== vdev
->host
.slot
&&
3536 pvdev
->host
.function
== vdev
->host
.function
) {
3538 error_report("vfio: error: device %s is already attached", path
);
3539 vfio_put_group(group
);
3544 ret
= vfio_get_device(group
, path
, vdev
);
3546 error_report("vfio: failed to get device %s", path
);
3547 vfio_put_group(group
);
3551 /* Get a copy of config space */
3552 ret
= pread(vdev
->fd
, vdev
->pdev
.config
,
3553 MIN(pci_config_size(&vdev
->pdev
), vdev
->config_size
),
3554 vdev
->config_offset
);
3555 if (ret
< (int)MIN(pci_config_size(&vdev
->pdev
), vdev
->config_size
)) {
3556 ret
= ret
< 0 ? -errno
: -EFAULT
;
3557 error_report("vfio: Failed to read device config space");
3561 /* vfio emulates a lot for us, but some bits need extra love */
3562 vdev
->emulated_config_bits
= g_malloc0(vdev
->config_size
);
3564 /* QEMU can choose to expose the ROM or not */
3565 memset(vdev
->emulated_config_bits
+ PCI_ROM_ADDRESS
, 0xff, 4);
3567 /* QEMU can change multi-function devices to single function, or reverse */
3568 vdev
->emulated_config_bits
[PCI_HEADER_TYPE
] =
3569 PCI_HEADER_TYPE_MULTI_FUNCTION
;
3571 /* Restore or clear multifunction, this is always controlled by QEMU */
3572 if (vdev
->pdev
.cap_present
& QEMU_PCI_CAP_MULTIFUNCTION
) {
3573 vdev
->pdev
.config
[PCI_HEADER_TYPE
] |= PCI_HEADER_TYPE_MULTI_FUNCTION
;
3575 vdev
->pdev
.config
[PCI_HEADER_TYPE
] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
3579 * Clear host resource mapping info. If we choose not to register a
3580 * BAR, such as might be the case with the option ROM, we can get
3581 * confusing, unwritable, residual addresses from the host here.
3583 memset(&vdev
->pdev
.config
[PCI_BASE_ADDRESS_0
], 0, 24);
3584 memset(&vdev
->pdev
.config
[PCI_ROM_ADDRESS
], 0, 4);
3586 vfio_pci_size_rom(vdev
);
3588 ret
= vfio_early_setup_msix(vdev
);
3593 vfio_map_bars(vdev
);
3595 ret
= vfio_add_capabilities(vdev
);
3600 /* QEMU emulates all of MSI & MSIX */
3601 if (pdev
->cap_present
& QEMU_PCI_CAP_MSIX
) {
3602 memset(vdev
->emulated_config_bits
+ pdev
->msix_cap
, 0xff,
3606 if (pdev
->cap_present
& QEMU_PCI_CAP_MSI
) {
3607 memset(vdev
->emulated_config_bits
+ pdev
->msi_cap
, 0xff,
3608 vdev
->msi_cap_size
);
3611 if (vfio_pci_read_config(&vdev
->pdev
, PCI_INTERRUPT_PIN
, 1)) {
3612 vdev
->intx
.mmap_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
3613 vfio_intx_mmap_enable
, vdev
);
3614 pci_device_set_intx_routing_notifier(&vdev
->pdev
, vfio_update_irq
);
3615 ret
= vfio_enable_intx(vdev
);
3621 add_boot_device_path(vdev
->bootindex
, &pdev
->qdev
, NULL
);
3622 vfio_register_err_notifier(vdev
);
3627 pci_device_set_intx_routing_notifier(&vdev
->pdev
, NULL
);
3628 vfio_teardown_msi(vdev
);
3629 vfio_unmap_bars(vdev
);
3631 g_free(vdev
->emulated_config_bits
);
3632 vfio_put_device(vdev
);
3633 vfio_put_group(group
);
3637 static void vfio_exitfn(PCIDevice
*pdev
)
3639 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
3640 VFIOGroup
*group
= vdev
->group
;
3642 vfio_unregister_err_notifier(vdev
);
3643 pci_device_set_intx_routing_notifier(&vdev
->pdev
, NULL
);
3644 vfio_disable_interrupts(vdev
);
3645 if (vdev
->intx
.mmap_timer
) {
3646 timer_free(vdev
->intx
.mmap_timer
);
3648 vfio_teardown_msi(vdev
);
3649 vfio_unmap_bars(vdev
);
3650 g_free(vdev
->emulated_config_bits
);
3652 vfio_put_device(vdev
);
3653 vfio_put_group(group
);
3656 static void vfio_pci_reset(DeviceState
*dev
)
3658 PCIDevice
*pdev
= DO_UPCAST(PCIDevice
, qdev
, dev
);
3659 VFIODevice
*vdev
= DO_UPCAST(VFIODevice
, pdev
, pdev
);
3661 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__
, vdev
->host
.domain
,
3662 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
3664 vfio_pci_pre_reset(vdev
);
3666 if (vdev
->reset_works
&& (vdev
->has_flr
|| !vdev
->has_pm_reset
) &&
3667 !ioctl(vdev
->fd
, VFIO_DEVICE_RESET
)) {
3668 DPRINTF("%04x:%02x:%02x.%x FLR/VFIO_DEVICE_RESET\n", vdev
->host
.domain
,
3669 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
3673 /* See if we can do our own bus reset */
3674 if (!vfio_pci_hot_reset_one(vdev
)) {
3678 /* If nothing else works and the device supports PM reset, use it */
3679 if (vdev
->reset_works
&& vdev
->has_pm_reset
&&
3680 !ioctl(vdev
->fd
, VFIO_DEVICE_RESET
)) {
3681 DPRINTF("%04x:%02x:%02x.%x PCI PM Reset\n", vdev
->host
.domain
,
3682 vdev
->host
.bus
, vdev
->host
.slot
, vdev
->host
.function
);
3687 vfio_pci_post_reset(vdev
);
3690 static Property vfio_pci_dev_properties
[] = {
3691 DEFINE_PROP_PCI_HOST_DEVADDR("host", VFIODevice
, host
),
3692 DEFINE_PROP_UINT32("x-intx-mmap-timeout-ms", VFIODevice
,
3693 intx
.mmap_timeout
, 1100),
3694 DEFINE_PROP_BIT("x-vga", VFIODevice
, features
,
3695 VFIO_FEATURE_ENABLE_VGA_BIT
, false),
3696 DEFINE_PROP_INT32("bootindex", VFIODevice
, bootindex
, -1),
3698 * TODO - support passed fds... is this necessary?
3699 * DEFINE_PROP_STRING("vfiofd", VFIODevice, vfiofd_name),
3700 * DEFINE_PROP_STRING("vfiogroupfd, VFIODevice, vfiogroupfd_name),
3702 DEFINE_PROP_END_OF_LIST(),
3705 static const VMStateDescription vfio_pci_vmstate
= {
3710 static void vfio_pci_dev_class_init(ObjectClass
*klass
, void *data
)
3712 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3713 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(klass
);
3715 dc
->reset
= vfio_pci_reset
;
3716 dc
->props
= vfio_pci_dev_properties
;
3717 dc
->vmsd
= &vfio_pci_vmstate
;
3718 dc
->desc
= "VFIO-based PCI device assignment";
3719 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
3720 pdc
->init
= vfio_initfn
;
3721 pdc
->exit
= vfio_exitfn
;
3722 pdc
->config_read
= vfio_pci_read_config
;
3723 pdc
->config_write
= vfio_pci_write_config
;
3724 pdc
->is_express
= 1; /* We might be */
3727 static const TypeInfo vfio_pci_dev_info
= {
3729 .parent
= TYPE_PCI_DEVICE
,
3730 .instance_size
= sizeof(VFIODevice
),
3731 .class_init
= vfio_pci_dev_class_init
,
3734 static void register_vfio_pci_dev_type(void)
3736 type_register_static(&vfio_pci_dev_info
);
3739 type_init(register_vfio_pci_dev_type
)