]> git.proxmox.com Git - mirror_qemu.git/blame - hw/vfio_pci.c
vfio-pci: Extend reset
[mirror_qemu.git] / hw / vfio_pci.c
CommitLineData
65501a74
AW
1/*
2 * vfio based device assignment support
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Alex Williamson <alex.williamson@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 * 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)
19 */
20
21#include <dirent.h>
22#include <unistd.h>
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <linux/vfio.h>
28
29#include "config.h"
30#include "event_notifier.h"
31#include "exec-memory.h"
32#include "kvm.h"
33#include "memory.h"
34#include "msi.h"
35#include "msix.h"
5c97e5eb
AW
36#include "pci.h"
37#include "qemu-common.h"
65501a74 38#include "qemu-error.h"
5c97e5eb 39#include "qemu-queue.h"
65501a74 40#include "range.h"
65501a74
AW
41
42/* #define DEBUG_VFIO */
43#ifdef DEBUG_VFIO
44#define DPRINTF(fmt, ...) \
45 do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
46#else
47#define DPRINTF(fmt, ...) \
48 do { } while (0)
49#endif
50
5c97e5eb
AW
51typedef struct VFIOBAR {
52 off_t fd_offset; /* offset of BAR within device fd */
53 int fd; /* device fd, allows us to pass VFIOBAR as opaque data */
54 MemoryRegion mem; /* slow, read/write access */
55 MemoryRegion mmap_mem; /* direct mapped access */
56 void *mmap;
57 size_t size;
58 uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
59 uint8_t nr; /* cache the BAR number for debug */
60} VFIOBAR;
61
62typedef struct VFIOINTx {
63 bool pending; /* interrupt pending */
64 bool kvm_accel; /* set when QEMU bypass through KVM enabled */
65 uint8_t pin; /* which pin to pull for qemu_set_irq */
66 EventNotifier interrupt; /* eventfd triggered on interrupt */
67 EventNotifier unmask; /* eventfd for unmask on QEMU bypass */
68 PCIINTxRoute route; /* routing info for QEMU bypass */
69 uint32_t mmap_timeout; /* delay to re-enable mmaps after interrupt */
70 QEMUTimer *mmap_timer; /* enable mmaps after periods w/o interrupts */
71} VFIOINTx;
72
73struct VFIODevice;
74
75typedef struct VFIOMSIVector {
76 EventNotifier interrupt; /* eventfd triggered on interrupt */
77 struct VFIODevice *vdev; /* back pointer to device */
78 int virq; /* KVM irqchip route for QEMU bypass */
79 bool use;
80} VFIOMSIVector;
81
82enum {
83 VFIO_INT_NONE = 0,
84 VFIO_INT_INTx = 1,
85 VFIO_INT_MSI = 2,
86 VFIO_INT_MSIX = 3,
87};
88
89struct VFIOGroup;
90
91typedef struct VFIOContainer {
92 int fd; /* /dev/vfio/vfio, empowered by the attached groups */
93 struct {
94 /* enable abstraction to support various iommu backends */
95 union {
96 MemoryListener listener; /* Used by type1 iommu */
97 };
98 void (*release)(struct VFIOContainer *);
99 } iommu_data;
100 QLIST_HEAD(, VFIOGroup) group_list;
101 QLIST_ENTRY(VFIOContainer) next;
102} VFIOContainer;
103
104/* Cache of MSI-X setup plus extra mmap and memory region for split BAR map */
105typedef struct VFIOMSIXInfo {
106 uint8_t table_bar;
107 uint8_t pba_bar;
108 uint16_t entries;
109 uint32_t table_offset;
110 uint32_t pba_offset;
111 MemoryRegion mmap_mem;
112 void *mmap;
113} VFIOMSIXInfo;
114
115typedef struct VFIODevice {
116 PCIDevice pdev;
117 int fd;
118 VFIOINTx intx;
119 unsigned int config_size;
120 off_t config_offset; /* Offset of config space region within device fd */
121 unsigned int rom_size;
122 off_t rom_offset; /* Offset of ROM region within device fd */
123 int msi_cap_size;
124 VFIOMSIVector *msi_vectors;
125 VFIOMSIXInfo *msix;
126 int nr_vectors; /* Number of MSI/MSIX vectors currently in use */
127 int interrupt; /* Current interrupt type */
128 VFIOBAR bars[PCI_NUM_REGIONS - 1]; /* No ROM */
129 PCIHostDeviceAddress host;
130 QLIST_ENTRY(VFIODevice) next;
131 struct VFIOGroup *group;
132 bool reset_works;
133} VFIODevice;
134
135typedef struct VFIOGroup {
136 int fd;
137 int groupid;
138 VFIOContainer *container;
139 QLIST_HEAD(, VFIODevice) device_list;
140 QLIST_ENTRY(VFIOGroup) next;
141 QLIST_ENTRY(VFIOGroup) container_next;
142} VFIOGroup;
143
65501a74
AW
144#define MSIX_CAP_LENGTH 12
145
146static QLIST_HEAD(, VFIOContainer)
147 container_list = QLIST_HEAD_INITIALIZER(container_list);
148
149static QLIST_HEAD(, VFIOGroup)
150 group_list = QLIST_HEAD_INITIALIZER(group_list);
151
152static void vfio_disable_interrupts(VFIODevice *vdev);
153static uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
154static void vfio_mmap_set_enabled(VFIODevice *vdev, bool enabled);
155
156/*
157 * Common VFIO interrupt disable
158 */
159static void vfio_disable_irqindex(VFIODevice *vdev, int index)
160{
161 struct vfio_irq_set irq_set = {
162 .argsz = sizeof(irq_set),
163 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
164 .index = index,
165 .start = 0,
166 .count = 0,
167 };
168
169 ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
65501a74
AW
170}
171
172/*
173 * INTx
174 */
175static void vfio_unmask_intx(VFIODevice *vdev)
176{
177 struct vfio_irq_set irq_set = {
178 .argsz = sizeof(irq_set),
179 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
180 .index = VFIO_PCI_INTX_IRQ_INDEX,
181 .start = 0,
182 .count = 1,
183 };
184
185 ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
186}
187
ea486926
AW
188/*
189 * Disabling BAR mmaping can be slow, but toggling it around INTx can
190 * also be a huge overhead. We try to get the best of both worlds by
191 * waiting until an interrupt to disable mmaps (subsequent transitions
192 * to the same state are effectively no overhead). If the interrupt has
193 * been serviced and the time gap is long enough, we re-enable mmaps for
194 * performance. This works well for things like graphics cards, which
195 * may not use their interrupt at all and are penalized to an unusable
196 * level by read/write BAR traps. Other devices, like NICs, have more
197 * regular interrupts and see much better latency by staying in non-mmap
198 * mode. We therefore set the default mmap_timeout such that a ping
199 * is just enough to keep the mmap disabled. Users can experiment with
200 * other options with the x-intx-mmap-timeout-ms parameter (a value of
201 * zero disables the timer).
202 */
203static void vfio_intx_mmap_enable(void *opaque)
204{
205 VFIODevice *vdev = opaque;
206
207 if (vdev->intx.pending) {
208 qemu_mod_timer(vdev->intx.mmap_timer,
209 qemu_get_clock_ms(vm_clock) + vdev->intx.mmap_timeout);
210 return;
211 }
212
213 vfio_mmap_set_enabled(vdev, true);
214}
215
65501a74
AW
216static void vfio_intx_interrupt(void *opaque)
217{
218 VFIODevice *vdev = opaque;
219
220 if (!event_notifier_test_and_clear(&vdev->intx.interrupt)) {
221 return;
222 }
223
224 DPRINTF("%s(%04x:%02x:%02x.%x) Pin %c\n", __func__, vdev->host.domain,
225 vdev->host.bus, vdev->host.slot, vdev->host.function,
226 'A' + vdev->intx.pin);
227
228 vdev->intx.pending = true;
229 qemu_set_irq(vdev->pdev.irq[vdev->intx.pin], 1);
ea486926
AW
230 vfio_mmap_set_enabled(vdev, false);
231 if (vdev->intx.mmap_timeout) {
232 qemu_mod_timer(vdev->intx.mmap_timer,
233 qemu_get_clock_ms(vm_clock) + vdev->intx.mmap_timeout);
234 }
65501a74
AW
235}
236
237static void vfio_eoi(VFIODevice *vdev)
238{
239 if (!vdev->intx.pending) {
240 return;
241 }
242
243 DPRINTF("%s(%04x:%02x:%02x.%x) EOI\n", __func__, vdev->host.domain,
244 vdev->host.bus, vdev->host.slot, vdev->host.function);
245
246 vdev->intx.pending = false;
247 qemu_set_irq(vdev->pdev.irq[vdev->intx.pin], 0);
248 vfio_unmask_intx(vdev);
249}
250
251typedef struct QEMU_PACKED VFIOIRQSetFD {
252 struct vfio_irq_set irq_set;
253 int32_t fd;
254} VFIOIRQSetFD;
255
256static int vfio_enable_intx(VFIODevice *vdev)
257{
258 VFIOIRQSetFD irq_set_fd = {
259 .irq_set = {
260 .argsz = sizeof(irq_set_fd),
261 .flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER,
262 .index = VFIO_PCI_INTX_IRQ_INDEX,
263 .start = 0,
264 .count = 1,
265 },
266 };
267 uint8_t pin = vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1);
268 int ret;
269
ea486926 270 if (!pin) {
65501a74
AW
271 return 0;
272 }
273
274 vfio_disable_interrupts(vdev);
275
276 vdev->intx.pin = pin - 1; /* Pin A (1) -> irq[0] */
277 ret = event_notifier_init(&vdev->intx.interrupt, 0);
278 if (ret) {
279 error_report("vfio: Error: event_notifier_init failed\n");
280 return ret;
281 }
282
283 irq_set_fd.fd = event_notifier_get_fd(&vdev->intx.interrupt);
284 qemu_set_fd_handler(irq_set_fd.fd, vfio_intx_interrupt, NULL, vdev);
285
286 if (ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, &irq_set_fd)) {
287 error_report("vfio: Error: Failed to setup INTx fd: %m\n");
288 return -errno;
289 }
290
65501a74
AW
291 vdev->interrupt = VFIO_INT_INTx;
292
293 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__, vdev->host.domain,
294 vdev->host.bus, vdev->host.slot, vdev->host.function);
295
296 return 0;
297}
298
299static void vfio_disable_intx(VFIODevice *vdev)
300{
301 int fd;
302
ea486926 303 qemu_del_timer(vdev->intx.mmap_timer);
65501a74
AW
304 vfio_disable_irqindex(vdev, VFIO_PCI_INTX_IRQ_INDEX);
305 vdev->intx.pending = false;
306 qemu_set_irq(vdev->pdev.irq[vdev->intx.pin], 0);
307 vfio_mmap_set_enabled(vdev, true);
308
309 fd = event_notifier_get_fd(&vdev->intx.interrupt);
310 qemu_set_fd_handler(fd, NULL, NULL, vdev);
311 event_notifier_cleanup(&vdev->intx.interrupt);
312
313 vdev->interrupt = VFIO_INT_NONE;
314
315 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__, vdev->host.domain,
316 vdev->host.bus, vdev->host.slot, vdev->host.function);
317}
318
319/*
320 * MSI/X
321 */
322static void vfio_msi_interrupt(void *opaque)
323{
324 VFIOMSIVector *vector = opaque;
325 VFIODevice *vdev = vector->vdev;
326 int nr = vector - vdev->msi_vectors;
327
328 if (!event_notifier_test_and_clear(&vector->interrupt)) {
329 return;
330 }
331
332 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d\n", __func__,
333 vdev->host.domain, vdev->host.bus, vdev->host.slot,
334 vdev->host.function, nr);
335
336 if (vdev->interrupt == VFIO_INT_MSIX) {
337 msix_notify(&vdev->pdev, nr);
338 } else if (vdev->interrupt == VFIO_INT_MSI) {
339 msi_notify(&vdev->pdev, nr);
340 } else {
341 error_report("vfio: MSI interrupt receieved, but not enabled?\n");
342 }
343}
344
345static int vfio_enable_vectors(VFIODevice *vdev, bool msix)
346{
347 struct vfio_irq_set *irq_set;
348 int ret = 0, i, argsz;
349 int32_t *fds;
350
351 argsz = sizeof(*irq_set) + (vdev->nr_vectors * sizeof(*fds));
352
353 irq_set = g_malloc0(argsz);
354 irq_set->argsz = argsz;
355 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
356 irq_set->index = msix ? VFIO_PCI_MSIX_IRQ_INDEX : VFIO_PCI_MSI_IRQ_INDEX;
357 irq_set->start = 0;
358 irq_set->count = vdev->nr_vectors;
359 fds = (int32_t *)&irq_set->data;
360
361 for (i = 0; i < vdev->nr_vectors; i++) {
362 if (!vdev->msi_vectors[i].use) {
363 fds[i] = -1;
364 continue;
365 }
366
367 fds[i] = event_notifier_get_fd(&vdev->msi_vectors[i].interrupt);
368 }
369
370 ret = ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
371
372 g_free(irq_set);
373
65501a74
AW
374 return ret;
375}
376
377static int vfio_msix_vector_use(PCIDevice *pdev,
378 unsigned int nr, MSIMessage msg)
379{
380 VFIODevice *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
381 VFIOMSIVector *vector;
382 int ret;
383
384 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d used\n", __func__,
385 vdev->host.domain, vdev->host.bus, vdev->host.slot,
386 vdev->host.function, nr);
387
65501a74
AW
388 vector = &vdev->msi_vectors[nr];
389 vector->vdev = vdev;
390 vector->use = true;
391
392 msix_vector_use(pdev, nr);
393
394 if (event_notifier_init(&vector->interrupt, 0)) {
395 error_report("vfio: Error: event_notifier_init failed\n");
396 }
397
398 /*
399 * Attempt to enable route through KVM irqchip,
400 * default to userspace handling if unavailable.
401 */
402 vector->virq = kvm_irqchip_add_msi_route(kvm_state, msg);
403 if (vector->virq < 0 ||
404 kvm_irqchip_add_irqfd_notifier(kvm_state, &vector->interrupt,
405 vector->virq) < 0) {
406 if (vector->virq >= 0) {
407 kvm_irqchip_release_virq(kvm_state, vector->virq);
408 vector->virq = -1;
409 }
410 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
411 vfio_msi_interrupt, NULL, vector);
412 }
413
414 /*
415 * We don't want to have the host allocate all possible MSI vectors
416 * for a device if they're not in use, so we shutdown and incrementally
417 * increase them as needed.
418 */
419 if (vdev->nr_vectors < nr + 1) {
65501a74
AW
420 vfio_disable_irqindex(vdev, VFIO_PCI_MSIX_IRQ_INDEX);
421 vdev->nr_vectors = nr + 1;
422 ret = vfio_enable_vectors(vdev, true);
423 if (ret) {
424 error_report("vfio: failed to enable vectors, %d\n", ret);
425 }
65501a74
AW
426 } else {
427 VFIOIRQSetFD irq_set_fd = {
428 .irq_set = {
429 .argsz = sizeof(irq_set_fd),
430 .flags = VFIO_IRQ_SET_DATA_EVENTFD |
431 VFIO_IRQ_SET_ACTION_TRIGGER,
432 .index = VFIO_PCI_MSIX_IRQ_INDEX,
433 .start = nr,
434 .count = 1,
435 },
436 .fd = event_notifier_get_fd(&vector->interrupt),
437 };
438 ret = ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, &irq_set_fd);
439 if (ret) {
440 error_report("vfio: failed to modify vector, %d\n", ret);
441 }
65501a74
AW
442 }
443
444 return 0;
445}
446
447static void vfio_msix_vector_release(PCIDevice *pdev, unsigned int nr)
448{
449 VFIODevice *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
450 VFIOMSIVector *vector = &vdev->msi_vectors[nr];
451 VFIOIRQSetFD irq_set_fd = {
452 .irq_set = {
453 .argsz = sizeof(irq_set_fd),
454 .flags = VFIO_IRQ_SET_DATA_EVENTFD |
455 VFIO_IRQ_SET_ACTION_TRIGGER,
456 .index = VFIO_PCI_MSIX_IRQ_INDEX,
457 .start = nr,
458 .count = 1,
459 },
460 .fd = -1,
461 };
462
463 DPRINTF("%s(%04x:%02x:%02x.%x) vector %d released\n", __func__,
464 vdev->host.domain, vdev->host.bus, vdev->host.slot,
465 vdev->host.function, nr);
466
467 /*
468 * XXX What's the right thing to do here? This turns off the interrupt
469 * completely, but do we really just want to switch the interrupt to
470 * bouncing through userspace and let msix.c drop it? Not sure.
471 */
472 msix_vector_unuse(pdev, nr);
473 ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, &irq_set_fd);
474
475 if (vector->virq < 0) {
476 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
477 NULL, NULL, NULL);
478 } else {
479 kvm_irqchip_remove_irqfd_notifier(kvm_state, &vector->interrupt,
480 vector->virq);
481 kvm_irqchip_release_virq(kvm_state, vector->virq);
482 vector->virq = -1;
483 }
484
485 event_notifier_cleanup(&vector->interrupt);
486 vector->use = false;
487}
488
489/* TODO This should move to msi.c */
490static MSIMessage msi_get_msg(PCIDevice *pdev, unsigned int vector)
491{
492 uint16_t flags = pci_get_word(pdev->config + pdev->msi_cap + PCI_MSI_FLAGS);
493 bool msi64bit = flags & PCI_MSI_FLAGS_64BIT;
494 MSIMessage msg;
495
496 if (msi64bit) {
497 msg.address = pci_get_quad(pdev->config +
498 pdev->msi_cap + PCI_MSI_ADDRESS_LO);
499 } else {
500 msg.address = pci_get_long(pdev->config +
501 pdev->msi_cap + PCI_MSI_ADDRESS_LO);
502 }
503
504 msg.data = pci_get_word(pdev->config + pdev->msi_cap +
505 (msi64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32));
506 msg.data += vector;
507
508 return msg;
509}
510
fd704adc
AW
511static void vfio_enable_msix(VFIODevice *vdev)
512{
513 vfio_disable_interrupts(vdev);
514
515 vdev->msi_vectors = g_malloc0(vdev->msix->entries * sizeof(VFIOMSIVector));
516
517 vdev->interrupt = VFIO_INT_MSIX;
518
519 if (msix_set_vector_notifiers(&vdev->pdev, vfio_msix_vector_use,
520 vfio_msix_vector_release)) {
521 error_report("vfio: msix_set_vector_notifiers failed\n");
522 }
523
524 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__, vdev->host.domain,
525 vdev->host.bus, vdev->host.slot, vdev->host.function);
526}
527
65501a74
AW
528static void vfio_enable_msi(VFIODevice *vdev)
529{
530 int ret, i;
531
532 vfio_disable_interrupts(vdev);
533
534 vdev->nr_vectors = msi_nr_vectors_allocated(&vdev->pdev);
535retry:
536 vdev->msi_vectors = g_malloc0(vdev->nr_vectors * sizeof(VFIOMSIVector));
537
538 for (i = 0; i < vdev->nr_vectors; i++) {
539 MSIMessage msg;
540 VFIOMSIVector *vector = &vdev->msi_vectors[i];
541
542 vector->vdev = vdev;
543 vector->use = true;
544
545 if (event_notifier_init(&vector->interrupt, 0)) {
546 error_report("vfio: Error: event_notifier_init failed\n");
547 }
548
549 msg = msi_get_msg(&vdev->pdev, i);
550
551 /*
552 * Attempt to enable route through KVM irqchip,
553 * default to userspace handling if unavailable.
554 */
555 vector->virq = kvm_irqchip_add_msi_route(kvm_state, msg);
556 if (vector->virq < 0 ||
557 kvm_irqchip_add_irqfd_notifier(kvm_state, &vector->interrupt,
558 vector->virq) < 0) {
559 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
560 vfio_msi_interrupt, NULL, vector);
561 }
562 }
563
564 ret = vfio_enable_vectors(vdev, false);
565 if (ret) {
566 if (ret < 0) {
567 error_report("vfio: Error: Failed to setup MSI fds: %m\n");
568 } else if (ret != vdev->nr_vectors) {
569 error_report("vfio: Error: Failed to enable %d "
570 "MSI vectors, retry with %d\n", vdev->nr_vectors, ret);
571 }
572
573 for (i = 0; i < vdev->nr_vectors; i++) {
574 VFIOMSIVector *vector = &vdev->msi_vectors[i];
575 if (vector->virq >= 0) {
576 kvm_irqchip_remove_irqfd_notifier(kvm_state, &vector->interrupt,
577 vector->virq);
578 kvm_irqchip_release_virq(kvm_state, vector->virq);
579 vector->virq = -1;
580 } else {
581 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
582 NULL, NULL, NULL);
583 }
584 event_notifier_cleanup(&vector->interrupt);
585 }
586
587 g_free(vdev->msi_vectors);
588
589 if (ret > 0 && ret != vdev->nr_vectors) {
590 vdev->nr_vectors = ret;
591 goto retry;
592 }
593 vdev->nr_vectors = 0;
594
595 return;
596 }
597
fd704adc
AW
598 vdev->interrupt = VFIO_INT_MSI;
599
65501a74
AW
600 DPRINTF("%s(%04x:%02x:%02x.%x) Enabled %d MSI vectors\n", __func__,
601 vdev->host.domain, vdev->host.bus, vdev->host.slot,
602 vdev->host.function, vdev->nr_vectors);
603}
604
fd704adc
AW
605static void vfio_disable_msi_common(VFIODevice *vdev)
606{
607 g_free(vdev->msi_vectors);
608 vdev->msi_vectors = NULL;
609 vdev->nr_vectors = 0;
610 vdev->interrupt = VFIO_INT_NONE;
611
612 vfio_enable_intx(vdev);
613}
614
615static void vfio_disable_msix(VFIODevice *vdev)
616{
617 msix_unset_vector_notifiers(&vdev->pdev);
618
619 if (vdev->nr_vectors) {
620 vfio_disable_irqindex(vdev, VFIO_PCI_MSIX_IRQ_INDEX);
621 }
622
623 vfio_disable_msi_common(vdev);
624
625 DPRINTF("%s(%04x:%02x:%02x.%x, msi%s)\n", __func__,
626 vdev->host.domain, vdev->host.bus, vdev->host.slot,
627 vdev->host.function, msix ? "x" : "");
628}
629
630static void vfio_disable_msi(VFIODevice *vdev)
65501a74
AW
631{
632 int i;
633
fd704adc 634 vfio_disable_irqindex(vdev, VFIO_PCI_MSI_IRQ_INDEX);
65501a74
AW
635
636 for (i = 0; i < vdev->nr_vectors; i++) {
637 VFIOMSIVector *vector = &vdev->msi_vectors[i];
638
639 if (!vector->use) {
640 continue;
641 }
642
643 if (vector->virq >= 0) {
644 kvm_irqchip_remove_irqfd_notifier(kvm_state,
645 &vector->interrupt, vector->virq);
646 kvm_irqchip_release_virq(kvm_state, vector->virq);
647 vector->virq = -1;
648 } else {
649 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
650 NULL, NULL, NULL);
651 }
652
65501a74
AW
653 event_notifier_cleanup(&vector->interrupt);
654 }
655
fd704adc 656 vfio_disable_msi_common(vdev);
65501a74 657
fd704adc
AW
658 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__, vdev->host.domain,
659 vdev->host.bus, vdev->host.slot, vdev->host.function);
65501a74
AW
660}
661
662/*
663 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
664 */
665static void vfio_bar_write(void *opaque, target_phys_addr_t addr,
666 uint64_t data, unsigned size)
667{
668 VFIOBAR *bar = opaque;
669 union {
670 uint8_t byte;
671 uint16_t word;
672 uint32_t dword;
673 uint64_t qword;
674 } buf;
675
676 switch (size) {
677 case 1:
678 buf.byte = data;
679 break;
680 case 2:
681 buf.word = cpu_to_le16(data);
682 break;
683 case 4:
684 buf.dword = cpu_to_le32(data);
685 break;
686 default:
687 hw_error("vfio: unsupported write size, %d bytes\n", size);
688 break;
689 }
690
691 if (pwrite(bar->fd, &buf, size, bar->fd_offset + addr) != size) {
692 error_report("%s(,0x%"TARGET_PRIxPHYS", 0x%"PRIx64", %d) failed: %m\n",
693 __func__, addr, data, size);
694 }
695
696 DPRINTF("%s(BAR%d+0x%"TARGET_PRIxPHYS", 0x%"PRIx64", %d)\n",
697 __func__, bar->nr, addr, data, size);
698
699 /*
700 * A read or write to a BAR always signals an INTx EOI. This will
701 * do nothing if not pending (including not in INTx mode). We assume
702 * that a BAR access is in response to an interrupt and that BAR
703 * accesses will service the interrupt. Unfortunately, we don't know
704 * which access will service the interrupt, so we're potentially
705 * getting quite a few host interrupts per guest interrupt.
706 */
707 vfio_eoi(DO_UPCAST(VFIODevice, bars[bar->nr], bar));
708}
709
710static uint64_t vfio_bar_read(void *opaque,
711 target_phys_addr_t addr, unsigned size)
712{
713 VFIOBAR *bar = opaque;
714 union {
715 uint8_t byte;
716 uint16_t word;
717 uint32_t dword;
718 uint64_t qword;
719 } buf;
720 uint64_t data = 0;
721
722 if (pread(bar->fd, &buf, size, bar->fd_offset + addr) != size) {
723 error_report("%s(,0x%"TARGET_PRIxPHYS", %d) failed: %m\n",
724 __func__, addr, size);
725 return (uint64_t)-1;
726 }
727
728 switch (size) {
729 case 1:
730 data = buf.byte;
731 break;
732 case 2:
733 data = le16_to_cpu(buf.word);
734 break;
735 case 4:
736 data = le32_to_cpu(buf.dword);
737 break;
738 default:
739 hw_error("vfio: unsupported read size, %d bytes\n", size);
740 break;
741 }
742
743 DPRINTF("%s(BAR%d+0x%"TARGET_PRIxPHYS", %d) = 0x%"PRIx64"\n",
744 __func__, bar->nr, addr, size, data);
745
746 /* Same as write above */
747 vfio_eoi(DO_UPCAST(VFIODevice, bars[bar->nr], bar));
748
749 return data;
750}
751
752static const MemoryRegionOps vfio_bar_ops = {
753 .read = vfio_bar_read,
754 .write = vfio_bar_write,
755 .endianness = DEVICE_LITTLE_ENDIAN,
756};
757
758/*
759 * PCI config space
760 */
761static uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len)
762{
763 VFIODevice *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
764 uint32_t val = 0;
765
766 /*
767 * We only need QEMU PCI config support for the ROM BAR, the MSI and MSIX
768 * capabilities, and the multifunction bit below. We let VFIO handle
769 * virtualizing everything else. Performance is not a concern here.
770 */
771 if (ranges_overlap(addr, len, PCI_ROM_ADDRESS, 4) ||
772 (pdev->cap_present & QEMU_PCI_CAP_MSIX &&
773 ranges_overlap(addr, len, pdev->msix_cap, MSIX_CAP_LENGTH)) ||
774 (pdev->cap_present & QEMU_PCI_CAP_MSI &&
775 ranges_overlap(addr, len, pdev->msi_cap, vdev->msi_cap_size))) {
776
777 val = pci_default_read_config(pdev, addr, len);
778 } else {
779 if (pread(vdev->fd, &val, len, vdev->config_offset + addr) != len) {
780 error_report("%s(%04x:%02x:%02x.%x, 0x%x, 0x%x) failed: %m\n",
781 __func__, vdev->host.domain, vdev->host.bus,
782 vdev->host.slot, vdev->host.function, addr, len);
783 return -errno;
784 }
785 val = le32_to_cpu(val);
786 }
787
788 /* Multifunction bit is virualized in QEMU */
789 if (unlikely(ranges_overlap(addr, len, PCI_HEADER_TYPE, 1))) {
790 uint32_t mask = PCI_HEADER_TYPE_MULTI_FUNCTION;
791
792 if (len == 4) {
793 mask <<= 16;
794 }
795
796 if (pdev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
797 val |= mask;
798 } else {
799 val &= ~mask;
800 }
801 }
802
803 DPRINTF("%s(%04x:%02x:%02x.%x, @0x%x, len=0x%x) %x\n", __func__,
804 vdev->host.domain, vdev->host.bus, vdev->host.slot,
805 vdev->host.function, addr, len, val);
806
807 return val;
808}
809
810static void vfio_pci_write_config(PCIDevice *pdev, uint32_t addr,
811 uint32_t val, int len)
812{
813 VFIODevice *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
814 uint32_t val_le = cpu_to_le32(val);
815
816 DPRINTF("%s(%04x:%02x:%02x.%x, @0x%x, 0x%x, len=0x%x)\n", __func__,
817 vdev->host.domain, vdev->host.bus, vdev->host.slot,
818 vdev->host.function, addr, val, len);
819
820 /* Write everything to VFIO, let it filter out what we can't write */
821 if (pwrite(vdev->fd, &val_le, len, vdev->config_offset + addr) != len) {
822 error_report("%s(%04x:%02x:%02x.%x, 0x%x, 0x%x, 0x%x) failed: %m\n",
823 __func__, vdev->host.domain, vdev->host.bus,
824 vdev->host.slot, vdev->host.function, addr, val, len);
825 }
826
827 /* Write standard header bits to emulation */
828 if (addr < PCI_CONFIG_HEADER_SIZE) {
829 pci_default_write_config(pdev, addr, val, len);
830 return;
831 }
832
833 /* MSI/MSI-X Enabling/Disabling */
834 if (pdev->cap_present & QEMU_PCI_CAP_MSI &&
835 ranges_overlap(addr, len, pdev->msi_cap, vdev->msi_cap_size)) {
836 int is_enabled, was_enabled = msi_enabled(pdev);
837
838 pci_default_write_config(pdev, addr, val, len);
839
840 is_enabled = msi_enabled(pdev);
841
842 if (!was_enabled && is_enabled) {
843 vfio_enable_msi(vdev);
844 } else if (was_enabled && !is_enabled) {
fd704adc 845 vfio_disable_msi(vdev);
65501a74
AW
846 }
847 }
848
849 if (pdev->cap_present & QEMU_PCI_CAP_MSIX &&
850 ranges_overlap(addr, len, pdev->msix_cap, MSIX_CAP_LENGTH)) {
851 int is_enabled, was_enabled = msix_enabled(pdev);
852
853 pci_default_write_config(pdev, addr, val, len);
854
855 is_enabled = msix_enabled(pdev);
856
857 if (!was_enabled && is_enabled) {
fd704adc 858 vfio_enable_msix(vdev);
65501a74 859 } else if (was_enabled && !is_enabled) {
fd704adc 860 vfio_disable_msix(vdev);
65501a74
AW
861 }
862 }
863}
864
865/*
866 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
867 */
af6bc27e
AW
868static int vfio_dma_unmap(VFIOContainer *container,
869 target_phys_addr_t iova, ram_addr_t size)
870{
871 struct vfio_iommu_type1_dma_unmap unmap = {
872 .argsz = sizeof(unmap),
873 .flags = 0,
874 .iova = iova,
875 .size = size,
876 };
877
878 if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
879 DPRINTF("VFIO_UNMAP_DMA: %d\n", -errno);
880 return -errno;
881 }
882
883 return 0;
884}
885
65501a74
AW
886static int vfio_dma_map(VFIOContainer *container, target_phys_addr_t iova,
887 ram_addr_t size, void *vaddr, bool readonly)
888{
889 struct vfio_iommu_type1_dma_map map = {
890 .argsz = sizeof(map),
891 .flags = VFIO_DMA_MAP_FLAG_READ,
5976cdd5 892 .vaddr = (__u64)(uintptr_t)vaddr,
65501a74
AW
893 .iova = iova,
894 .size = size,
895 };
896
897 if (!readonly) {
898 map.flags |= VFIO_DMA_MAP_FLAG_WRITE;
899 }
900
12af1344
AW
901 /*
902 * Try the mapping, if it fails with EBUSY, unmap the region and try
903 * again. This shouldn't be necessary, but we sometimes see it in
904 * the the VGA ROM space.
905 */
906 if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 ||
907 (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 &&
908 ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) {
909 return 0;
65501a74
AW
910 }
911
12af1344
AW
912 DPRINTF("VFIO_MAP_DMA: %d\n", -errno);
913 return -errno;
65501a74
AW
914}
915
65501a74
AW
916static void vfio_listener_dummy1(MemoryListener *listener)
917{
918 /* We don't do batching (begin/commit) or care about logging */
919}
920
921static void vfio_listener_dummy2(MemoryListener *listener,
922 MemoryRegionSection *section)
923{
924 /* We don't do logging or care about nops */
925}
926
927static void vfio_listener_dummy3(MemoryListener *listener,
928 MemoryRegionSection *section,
929 bool match_data, uint64_t data,
930 EventNotifier *e)
931{
932 /* We don't care about eventfds */
933}
934
935static bool vfio_listener_skipped_section(MemoryRegionSection *section)
936{
937 return !memory_region_is_ram(section->mr);
938}
939
940static void vfio_listener_region_add(MemoryListener *listener,
941 MemoryRegionSection *section)
942{
943 VFIOContainer *container = container_of(listener, VFIOContainer,
944 iommu_data.listener);
945 target_phys_addr_t iova, end;
946 void *vaddr;
947 int ret;
948
949 if (vfio_listener_skipped_section(section)) {
950 DPRINTF("vfio: SKIPPING region_add %"TARGET_PRIxPHYS" - %"PRIx64"\n",
951 section->offset_within_address_space,
952 section->offset_within_address_space + section->size - 1);
953 return;
954 }
955
956 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
957 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
958 error_report("%s received unaligned region\n", __func__);
959 return;
960 }
961
962 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
963 end = (section->offset_within_address_space + section->size) &
964 TARGET_PAGE_MASK;
965
966 if (iova >= end) {
967 return;
968 }
969
970 vaddr = memory_region_get_ram_ptr(section->mr) +
971 section->offset_within_region +
972 (iova - section->offset_within_address_space);
973
974 DPRINTF("vfio: region_add %"TARGET_PRIxPHYS" - %"TARGET_PRIxPHYS" [%p]\n",
975 iova, end - 1, vaddr);
976
977 ret = vfio_dma_map(container, iova, end - iova, vaddr, section->readonly);
978 if (ret) {
979 error_report("vfio_dma_map(%p, 0x%"TARGET_PRIxPHYS", "
980 "0x%"TARGET_PRIxPHYS", %p) = %d (%m)\n",
981 container, iova, end - iova, vaddr, ret);
982 }
983}
984
985static void vfio_listener_region_del(MemoryListener *listener,
986 MemoryRegionSection *section)
987{
988 VFIOContainer *container = container_of(listener, VFIOContainer,
989 iommu_data.listener);
990 target_phys_addr_t iova, end;
991 int ret;
992
993 if (vfio_listener_skipped_section(section)) {
994 DPRINTF("vfio: SKIPPING region_del %"TARGET_PRIxPHYS" - %"PRIx64"\n",
995 section->offset_within_address_space,
996 section->offset_within_address_space + section->size - 1);
997 return;
998 }
999
1000 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
1001 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
1002 error_report("%s received unaligned region\n", __func__);
1003 return;
1004 }
1005
1006 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
1007 end = (section->offset_within_address_space + section->size) &
1008 TARGET_PAGE_MASK;
1009
1010 if (iova >= end) {
1011 return;
1012 }
1013
1014 DPRINTF("vfio: region_del %"TARGET_PRIxPHYS" - %"TARGET_PRIxPHYS"\n",
1015 iova, end - 1);
1016
1017 ret = vfio_dma_unmap(container, iova, end - iova);
1018 if (ret) {
1019 error_report("vfio_dma_unmap(%p, 0x%"TARGET_PRIxPHYS", "
1020 "0x%"TARGET_PRIxPHYS") = %d (%m)\n",
1021 container, iova, end - iova, ret);
1022 }
1023}
1024
1025static MemoryListener vfio_memory_listener = {
1026 .begin = vfio_listener_dummy1,
1027 .commit = vfio_listener_dummy1,
1028 .region_add = vfio_listener_region_add,
1029 .region_del = vfio_listener_region_del,
1030 .region_nop = vfio_listener_dummy2,
1031 .log_start = vfio_listener_dummy2,
1032 .log_stop = vfio_listener_dummy2,
1033 .log_sync = vfio_listener_dummy2,
1034 .log_global_start = vfio_listener_dummy1,
1035 .log_global_stop = vfio_listener_dummy1,
1036 .eventfd_add = vfio_listener_dummy3,
1037 .eventfd_del = vfio_listener_dummy3,
1038};
1039
1040static void vfio_listener_release(VFIOContainer *container)
1041{
1042 memory_listener_unregister(&container->iommu_data.listener);
1043}
1044
1045/*
1046 * Interrupt setup
1047 */
1048static void vfio_disable_interrupts(VFIODevice *vdev)
1049{
1050 switch (vdev->interrupt) {
1051 case VFIO_INT_INTx:
1052 vfio_disable_intx(vdev);
1053 break;
1054 case VFIO_INT_MSI:
fd704adc 1055 vfio_disable_msi(vdev);
65501a74
AW
1056 break;
1057 case VFIO_INT_MSIX:
fd704adc 1058 vfio_disable_msix(vdev);
65501a74
AW
1059 break;
1060 }
1061}
1062
1063static int vfio_setup_msi(VFIODevice *vdev, int pos)
1064{
1065 uint16_t ctrl;
1066 bool msi_64bit, msi_maskbit;
1067 int ret, entries;
1068
65501a74
AW
1069 if (pread(vdev->fd, &ctrl, sizeof(ctrl),
1070 vdev->config_offset + pos + PCI_CAP_FLAGS) != sizeof(ctrl)) {
1071 return -errno;
1072 }
1073 ctrl = le16_to_cpu(ctrl);
1074
1075 msi_64bit = !!(ctrl & PCI_MSI_FLAGS_64BIT);
1076 msi_maskbit = !!(ctrl & PCI_MSI_FLAGS_MASKBIT);
1077 entries = 1 << ((ctrl & PCI_MSI_FLAGS_QMASK) >> 1);
1078
1079 DPRINTF("%04x:%02x:%02x.%x PCI MSI CAP @0x%x\n", vdev->host.domain,
1080 vdev->host.bus, vdev->host.slot, vdev->host.function, pos);
1081
1082 ret = msi_init(&vdev->pdev, pos, entries, msi_64bit, msi_maskbit);
1083 if (ret < 0) {
e43b9a5a
AW
1084 if (ret == -ENOTSUP) {
1085 return 0;
1086 }
65501a74
AW
1087 error_report("vfio: msi_init failed\n");
1088 return ret;
1089 }
1090 vdev->msi_cap_size = 0xa + (msi_maskbit ? 0xa : 0) + (msi_64bit ? 0x4 : 0);
1091
1092 return 0;
1093}
1094
1095/*
1096 * We don't have any control over how pci_add_capability() inserts
1097 * capabilities into the chain. In order to setup MSI-X we need a
1098 * MemoryRegion for the BAR. In order to setup the BAR and not
1099 * attempt to mmap the MSI-X table area, which VFIO won't allow, we
1100 * need to first look for where the MSI-X table lives. So we
1101 * unfortunately split MSI-X setup across two functions.
1102 */
1103static int vfio_early_setup_msix(VFIODevice *vdev)
1104{
1105 uint8_t pos;
1106 uint16_t ctrl;
1107 uint32_t table, pba;
1108
1109 pos = pci_find_capability(&vdev->pdev, PCI_CAP_ID_MSIX);
1110 if (!pos) {
1111 return 0;
1112 }
1113
1114 if (pread(vdev->fd, &ctrl, sizeof(ctrl),
1115 vdev->config_offset + pos + PCI_CAP_FLAGS) != sizeof(ctrl)) {
1116 return -errno;
1117 }
1118
1119 if (pread(vdev->fd, &table, sizeof(table),
1120 vdev->config_offset + pos + PCI_MSIX_TABLE) != sizeof(table)) {
1121 return -errno;
1122 }
1123
1124 if (pread(vdev->fd, &pba, sizeof(pba),
1125 vdev->config_offset + pos + PCI_MSIX_PBA) != sizeof(pba)) {
1126 return -errno;
1127 }
1128
1129 ctrl = le16_to_cpu(ctrl);
1130 table = le32_to_cpu(table);
1131 pba = le32_to_cpu(pba);
1132
1133 vdev->msix = g_malloc0(sizeof(*(vdev->msix)));
1134 vdev->msix->table_bar = table & PCI_MSIX_FLAGS_BIRMASK;
1135 vdev->msix->table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
1136 vdev->msix->pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK;
1137 vdev->msix->pba_offset = pba & ~PCI_MSIX_FLAGS_BIRMASK;
1138 vdev->msix->entries = (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
1139
1140 DPRINTF("%04x:%02x:%02x.%x "
1141 "PCI MSI-X CAP @0x%x, BAR %d, offset 0x%x, entries %d\n",
1142 vdev->host.domain, vdev->host.bus, vdev->host.slot,
1143 vdev->host.function, pos, vdev->msix->table_bar,
1144 vdev->msix->table_offset, vdev->msix->entries);
1145
1146 return 0;
1147}
1148
1149static int vfio_setup_msix(VFIODevice *vdev, int pos)
1150{
1151 int ret;
1152
65501a74
AW
1153 ret = msix_init(&vdev->pdev, vdev->msix->entries,
1154 &vdev->bars[vdev->msix->table_bar].mem,
1155 vdev->msix->table_bar, vdev->msix->table_offset,
1156 &vdev->bars[vdev->msix->pba_bar].mem,
1157 vdev->msix->pba_bar, vdev->msix->pba_offset, pos);
1158 if (ret < 0) {
e43b9a5a
AW
1159 if (ret == -ENOTSUP) {
1160 return 0;
1161 }
65501a74
AW
1162 error_report("vfio: msix_init failed\n");
1163 return ret;
1164 }
1165
65501a74
AW
1166 return 0;
1167}
1168
1169static void vfio_teardown_msi(VFIODevice *vdev)
1170{
1171 msi_uninit(&vdev->pdev);
1172
1173 if (vdev->msix) {
65501a74
AW
1174 msix_uninit(&vdev->pdev, &vdev->bars[vdev->msix->table_bar].mem,
1175 &vdev->bars[vdev->msix->pba_bar].mem);
1176 }
1177}
1178
1179/*
1180 * Resource setup
1181 */
1182static void vfio_mmap_set_enabled(VFIODevice *vdev, bool enabled)
1183{
1184 int i;
1185
1186 for (i = 0; i < PCI_ROM_SLOT; i++) {
1187 VFIOBAR *bar = &vdev->bars[i];
1188
1189 if (!bar->size) {
1190 continue;
1191 }
1192
1193 memory_region_set_enabled(&bar->mmap_mem, enabled);
1194 if (vdev->msix && vdev->msix->table_bar == i) {
1195 memory_region_set_enabled(&vdev->msix->mmap_mem, enabled);
1196 }
1197 }
1198}
1199
1200static void vfio_unmap_bar(VFIODevice *vdev, int nr)
1201{
1202 VFIOBAR *bar = &vdev->bars[nr];
1203
1204 if (!bar->size) {
1205 return;
1206 }
1207
1208 memory_region_del_subregion(&bar->mem, &bar->mmap_mem);
1209 munmap(bar->mmap, memory_region_size(&bar->mmap_mem));
1210
1211 if (vdev->msix && vdev->msix->table_bar == nr) {
1212 memory_region_del_subregion(&bar->mem, &vdev->msix->mmap_mem);
1213 munmap(vdev->msix->mmap, memory_region_size(&vdev->msix->mmap_mem));
1214 }
1215
1216 memory_region_destroy(&bar->mem);
1217}
1218
1219static int vfio_mmap_bar(VFIOBAR *bar, MemoryRegion *mem, MemoryRegion *submem,
1220 void **map, size_t size, off_t offset,
1221 const char *name)
1222{
1223 int ret = 0;
1224
1225 if (size && bar->flags & VFIO_REGION_INFO_FLAG_MMAP) {
1226 int prot = 0;
1227
1228 if (bar->flags & VFIO_REGION_INFO_FLAG_READ) {
1229 prot |= PROT_READ;
1230 }
1231
1232 if (bar->flags & VFIO_REGION_INFO_FLAG_WRITE) {
1233 prot |= PROT_WRITE;
1234 }
1235
1236 *map = mmap(NULL, size, prot, MAP_SHARED,
1237 bar->fd, bar->fd_offset + offset);
1238 if (*map == MAP_FAILED) {
1239 *map = NULL;
1240 ret = -errno;
1241 goto empty_region;
1242 }
1243
1244 memory_region_init_ram_ptr(submem, name, size, *map);
1245 } else {
1246empty_region:
1247 /* Create a zero sized sub-region to make cleanup easy. */
1248 memory_region_init(submem, name, 0);
1249 }
1250
1251 memory_region_add_subregion(mem, offset, submem);
1252
1253 return ret;
1254}
1255
1256static void vfio_map_bar(VFIODevice *vdev, int nr)
1257{
1258 VFIOBAR *bar = &vdev->bars[nr];
1259 unsigned size = bar->size;
1260 char name[64];
1261 uint32_t pci_bar;
1262 uint8_t type;
1263 int ret;
1264
1265 /* Skip both unimplemented BARs and the upper half of 64bit BARS. */
1266 if (!size) {
1267 return;
1268 }
1269
1270 snprintf(name, sizeof(name), "VFIO %04x:%02x:%02x.%x BAR %d",
1271 vdev->host.domain, vdev->host.bus, vdev->host.slot,
1272 vdev->host.function, nr);
1273
1274 /* Determine what type of BAR this is for registration */
1275 ret = pread(vdev->fd, &pci_bar, sizeof(pci_bar),
1276 vdev->config_offset + PCI_BASE_ADDRESS_0 + (4 * nr));
1277 if (ret != sizeof(pci_bar)) {
1278 error_report("vfio: Failed to read BAR %d (%m)\n", nr);
1279 return;
1280 }
1281
1282 pci_bar = le32_to_cpu(pci_bar);
1283 type = pci_bar & (pci_bar & PCI_BASE_ADDRESS_SPACE_IO ?
1284 ~PCI_BASE_ADDRESS_IO_MASK : ~PCI_BASE_ADDRESS_MEM_MASK);
1285
1286 /* A "slow" read/write mapping underlies all BARs */
1287 memory_region_init_io(&bar->mem, &vfio_bar_ops, bar, name, size);
1288 pci_register_bar(&vdev->pdev, nr, type, &bar->mem);
1289
1290 /*
1291 * We can't mmap areas overlapping the MSIX vector table, so we
1292 * potentially insert a direct-mapped subregion before and after it.
1293 */
1294 if (vdev->msix && vdev->msix->table_bar == nr) {
1295 size = vdev->msix->table_offset & TARGET_PAGE_MASK;
1296 }
1297
1298 strncat(name, " mmap", sizeof(name) - strlen(name) - 1);
1299 if (vfio_mmap_bar(bar, &bar->mem,
1300 &bar->mmap_mem, &bar->mmap, size, 0, name)) {
1301 error_report("%s unsupported. Performance may be slow\n", name);
1302 }
1303
1304 if (vdev->msix && vdev->msix->table_bar == nr) {
1305 unsigned start;
1306
1307 start = TARGET_PAGE_ALIGN(vdev->msix->table_offset +
1308 (vdev->msix->entries * PCI_MSIX_ENTRY_SIZE));
1309
1310 size = start < bar->size ? bar->size - start : 0;
1311 strncat(name, " msix-hi", sizeof(name) - strlen(name) - 1);
1312 /* VFIOMSIXInfo contains another MemoryRegion for this mapping */
1313 if (vfio_mmap_bar(bar, &bar->mem, &vdev->msix->mmap_mem,
1314 &vdev->msix->mmap, size, start, name)) {
1315 error_report("%s unsupported. Performance may be slow\n", name);
1316 }
1317 }
1318}
1319
1320static void vfio_map_bars(VFIODevice *vdev)
1321{
1322 int i;
1323
1324 for (i = 0; i < PCI_ROM_SLOT; i++) {
1325 vfio_map_bar(vdev, i);
1326 }
1327}
1328
1329static void vfio_unmap_bars(VFIODevice *vdev)
1330{
1331 int i;
1332
1333 for (i = 0; i < PCI_ROM_SLOT; i++) {
1334 vfio_unmap_bar(vdev, i);
1335 }
1336}
1337
1338/*
1339 * General setup
1340 */
1341static uint8_t vfio_std_cap_max_size(PCIDevice *pdev, uint8_t pos)
1342{
1343 uint8_t tmp, next = 0xff;
1344
1345 for (tmp = pdev->config[PCI_CAPABILITY_LIST]; tmp;
1346 tmp = pdev->config[tmp + 1]) {
1347 if (tmp > pos && tmp < next) {
1348 next = tmp;
1349 }
1350 }
1351
1352 return next - pos;
1353}
1354
1355static int vfio_add_std_cap(VFIODevice *vdev, uint8_t pos)
1356{
1357 PCIDevice *pdev = &vdev->pdev;
1358 uint8_t cap_id, next, size;
1359 int ret;
1360
1361 cap_id = pdev->config[pos];
1362 next = pdev->config[pos + 1];
1363
1364 /*
1365 * If it becomes important to configure capabilities to their actual
1366 * size, use this as the default when it's something we don't recognize.
1367 * Since QEMU doesn't actually handle many of the config accesses,
1368 * exact size doesn't seem worthwhile.
1369 */
1370 size = vfio_std_cap_max_size(pdev, pos);
1371
1372 /*
1373 * pci_add_capability always inserts the new capability at the head
1374 * of the chain. Therefore to end up with a chain that matches the
1375 * physical device, we insert from the end by making this recursive.
1376 * This is also why we pre-caclulate size above as cached config space
1377 * will be changed as we unwind the stack.
1378 */
1379 if (next) {
1380 ret = vfio_add_std_cap(vdev, next);
1381 if (ret) {
1382 return ret;
1383 }
1384 } else {
1385 pdev->config[PCI_CAPABILITY_LIST] = 0; /* Begin the rebuild */
1386 }
1387
1388 switch (cap_id) {
1389 case PCI_CAP_ID_MSI:
1390 ret = vfio_setup_msi(vdev, pos);
1391 break;
1392 case PCI_CAP_ID_MSIX:
1393 ret = vfio_setup_msix(vdev, pos);
1394 break;
1395 default:
1396 ret = pci_add_capability(pdev, cap_id, pos, size);
1397 break;
1398 }
1399
1400 if (ret < 0) {
1401 error_report("vfio: %04x:%02x:%02x.%x Error adding PCI capability "
1402 "0x%x[0x%x]@0x%x: %d\n", vdev->host.domain,
1403 vdev->host.bus, vdev->host.slot, vdev->host.function,
1404 cap_id, size, pos, ret);
1405 return ret;
1406 }
1407
1408 return 0;
1409}
1410
1411static int vfio_add_capabilities(VFIODevice *vdev)
1412{
1413 PCIDevice *pdev = &vdev->pdev;
1414
1415 if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST) ||
1416 !pdev->config[PCI_CAPABILITY_LIST]) {
1417 return 0; /* Nothing to add */
1418 }
1419
1420 return vfio_add_std_cap(vdev, pdev->config[PCI_CAPABILITY_LIST]);
1421}
1422
1423static int vfio_load_rom(VFIODevice *vdev)
1424{
1425 uint64_t size = vdev->rom_size;
1426 char name[32];
1427 off_t off = 0, voff = vdev->rom_offset;
1428 ssize_t bytes;
1429 void *ptr;
1430
1431 /* If loading ROM from file, pci handles it */
1432 if (vdev->pdev.romfile || !vdev->pdev.rom_bar || !size) {
1433 return 0;
1434 }
1435
1436 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__, vdev->host.domain,
1437 vdev->host.bus, vdev->host.slot, vdev->host.function);
1438
1439 snprintf(name, sizeof(name), "vfio[%04x:%02x:%02x.%x].rom",
1440 vdev->host.domain, vdev->host.bus, vdev->host.slot,
1441 vdev->host.function);
1442 memory_region_init_ram(&vdev->pdev.rom, name, size);
1443 ptr = memory_region_get_ram_ptr(&vdev->pdev.rom);
1444 memset(ptr, 0xff, size);
1445
1446 while (size) {
1447 bytes = pread(vdev->fd, ptr + off, size, voff + off);
1448 if (bytes == 0) {
1449 break; /* expect that we could get back less than the ROM BAR */
1450 } else if (bytes > 0) {
1451 off += bytes;
1452 size -= bytes;
1453 } else {
1454 if (errno == EINTR || errno == EAGAIN) {
1455 continue;
1456 }
1457 error_report("vfio: Error reading device ROM: %m\n");
1458 memory_region_destroy(&vdev->pdev.rom);
1459 return -errno;
1460 }
1461 }
1462
1463 pci_register_bar(&vdev->pdev, PCI_ROM_SLOT, 0, &vdev->pdev.rom);
1464 vdev->pdev.has_rom = true;
1465 return 0;
1466}
1467
1468static int vfio_connect_container(VFIOGroup *group)
1469{
1470 VFIOContainer *container;
1471 int ret, fd;
1472
1473 if (group->container) {
1474 return 0;
1475 }
1476
1477 QLIST_FOREACH(container, &container_list, next) {
1478 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
1479 group->container = container;
1480 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1481 return 0;
1482 }
1483 }
1484
1485 fd = qemu_open("/dev/vfio/vfio", O_RDWR);
1486 if (fd < 0) {
1487 error_report("vfio: failed to open /dev/vfio/vfio: %m\n");
1488 return -errno;
1489 }
1490
1491 ret = ioctl(fd, VFIO_GET_API_VERSION);
1492 if (ret != VFIO_API_VERSION) {
1493 error_report("vfio: supported vfio version: %d, "
1494 "reported version: %d\n", VFIO_API_VERSION, ret);
1495 close(fd);
1496 return -EINVAL;
1497 }
1498
1499 container = g_malloc0(sizeof(*container));
1500 container->fd = fd;
1501
1502 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
1503 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
1504 if (ret) {
1505 error_report("vfio: failed to set group container: %m\n");
1506 g_free(container);
1507 close(fd);
1508 return -errno;
1509 }
1510
1511 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
1512 if (ret) {
1513 error_report("vfio: failed to set iommu for container: %m\n");
1514 g_free(container);
1515 close(fd);
1516 return -errno;
1517 }
1518
1519 container->iommu_data.listener = vfio_memory_listener;
1520 container->iommu_data.release = vfio_listener_release;
1521
1522 memory_listener_register(&container->iommu_data.listener,
1523 get_system_memory());
1524 } else {
1525 error_report("vfio: No available IOMMU models\n");
1526 g_free(container);
1527 close(fd);
1528 return -EINVAL;
1529 }
1530
1531 QLIST_INIT(&container->group_list);
1532 QLIST_INSERT_HEAD(&container_list, container, next);
1533
1534 group->container = container;
1535 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1536
1537 return 0;
1538}
1539
1540static void vfio_disconnect_container(VFIOGroup *group)
1541{
1542 VFIOContainer *container = group->container;
1543
1544 if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
1545 error_report("vfio: error disconnecting group %d from container\n",
1546 group->groupid);
1547 }
1548
1549 QLIST_REMOVE(group, container_next);
1550 group->container = NULL;
1551
1552 if (QLIST_EMPTY(&container->group_list)) {
1553 if (container->iommu_data.release) {
1554 container->iommu_data.release(container);
1555 }
1556 QLIST_REMOVE(container, next);
1557 DPRINTF("vfio_disconnect_container: close container->fd\n");
1558 close(container->fd);
1559 g_free(container);
1560 }
1561}
1562
1563static VFIOGroup *vfio_get_group(int groupid)
1564{
1565 VFIOGroup *group;
1566 char path[32];
1567 struct vfio_group_status status = { .argsz = sizeof(status) };
1568
1569 QLIST_FOREACH(group, &group_list, next) {
1570 if (group->groupid == groupid) {
1571 return group;
1572 }
1573 }
1574
1575 group = g_malloc0(sizeof(*group));
1576
1577 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
1578 group->fd = qemu_open(path, O_RDWR);
1579 if (group->fd < 0) {
1580 error_report("vfio: error opening %s: %m\n", path);
1581 g_free(group);
1582 return NULL;
1583 }
1584
1585 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) {
1586 error_report("vfio: error getting group status: %m\n");
1587 close(group->fd);
1588 g_free(group);
1589 return NULL;
1590 }
1591
1592 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
1593 error_report("vfio: error, group %d is not viable, please ensure "
1594 "all devices within the iommu_group are bound to their "
1595 "vfio bus driver.\n", groupid);
1596 close(group->fd);
1597 g_free(group);
1598 return NULL;
1599 }
1600
1601 group->groupid = groupid;
1602 QLIST_INIT(&group->device_list);
1603
1604 if (vfio_connect_container(group)) {
1605 error_report("vfio: failed to setup container for group %d\n", groupid);
1606 close(group->fd);
1607 g_free(group);
1608 return NULL;
1609 }
1610
1611 QLIST_INSERT_HEAD(&group_list, group, next);
1612
1613 return group;
1614}
1615
1616static void vfio_put_group(VFIOGroup *group)
1617{
1618 if (!QLIST_EMPTY(&group->device_list)) {
1619 return;
1620 }
1621
1622 vfio_disconnect_container(group);
1623 QLIST_REMOVE(group, next);
1624 DPRINTF("vfio_put_group: close group->fd\n");
1625 close(group->fd);
1626 g_free(group);
1627}
1628
1629static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)
1630{
1631 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
1632 struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
1633 int ret, i;
1634
1635 ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
1636 if (ret < 0) {
1637 error_report("vfio: error getting device %s from group %d: %m\n",
1638 name, group->groupid);
1639 error_report("Verify all devices in group %d are bound to vfio-pci "
1640 "or pci-stub and not already in use\n", group->groupid);
1641 return ret;
1642 }
1643
1644 vdev->fd = ret;
1645 vdev->group = group;
1646 QLIST_INSERT_HEAD(&group->device_list, vdev, next);
1647
1648 /* Sanity check device */
1649 ret = ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &dev_info);
1650 if (ret) {
1651 error_report("vfio: error getting device info: %m\n");
1652 goto error;
1653 }
1654
1655 DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name,
1656 dev_info.flags, dev_info.num_regions, dev_info.num_irqs);
1657
1658 if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PCI)) {
1659 error_report("vfio: Um, this isn't a PCI device\n");
1660 goto error;
1661 }
1662
1663 vdev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
1664 if (!vdev->reset_works) {
1665 error_report("Warning, device %s does not support reset\n", name);
1666 }
1667
1668 if (dev_info.num_regions != VFIO_PCI_NUM_REGIONS) {
1669 error_report("vfio: unexpected number of io regions %u\n",
1670 dev_info.num_regions);
1671 goto error;
1672 }
1673
1674 if (dev_info.num_irqs != VFIO_PCI_NUM_IRQS) {
1675 error_report("vfio: unexpected number of irqs %u\n", dev_info.num_irqs);
1676 goto error;
1677 }
1678
1679 for (i = VFIO_PCI_BAR0_REGION_INDEX; i < VFIO_PCI_ROM_REGION_INDEX; i++) {
1680 reg_info.index = i;
1681
1682 ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
1683 if (ret) {
1684 error_report("vfio: Error getting region %d info: %m\n", i);
1685 goto error;
1686 }
1687
1688 DPRINTF("Device %s region %d:\n", name, i);
1689 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
1690 (unsigned long)reg_info.size, (unsigned long)reg_info.offset,
1691 (unsigned long)reg_info.flags);
1692
1693 vdev->bars[i].flags = reg_info.flags;
1694 vdev->bars[i].size = reg_info.size;
1695 vdev->bars[i].fd_offset = reg_info.offset;
1696 vdev->bars[i].fd = vdev->fd;
1697 vdev->bars[i].nr = i;
1698 }
1699
1700 reg_info.index = VFIO_PCI_ROM_REGION_INDEX;
1701
1702 ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
1703 if (ret) {
1704 error_report("vfio: Error getting ROM info: %m\n");
1705 goto error;
1706 }
1707
1708 DPRINTF("Device %s ROM:\n", name);
1709 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
1710 (unsigned long)reg_info.size, (unsigned long)reg_info.offset,
1711 (unsigned long)reg_info.flags);
1712
1713 vdev->rom_size = reg_info.size;
1714 vdev->rom_offset = reg_info.offset;
1715
1716 reg_info.index = VFIO_PCI_CONFIG_REGION_INDEX;
1717
1718 ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
1719 if (ret) {
1720 error_report("vfio: Error getting config info: %m\n");
1721 goto error;
1722 }
1723
1724 DPRINTF("Device %s config:\n", name);
1725 DPRINTF(" size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
1726 (unsigned long)reg_info.size, (unsigned long)reg_info.offset,
1727 (unsigned long)reg_info.flags);
1728
1729 vdev->config_size = reg_info.size;
1730 vdev->config_offset = reg_info.offset;
1731
1732error:
1733 if (ret) {
1734 QLIST_REMOVE(vdev, next);
1735 vdev->group = NULL;
1736 close(vdev->fd);
1737 }
1738 return ret;
1739}
1740
1741static void vfio_put_device(VFIODevice *vdev)
1742{
1743 QLIST_REMOVE(vdev, next);
1744 vdev->group = NULL;
1745 DPRINTF("vfio_put_device: close vdev->fd\n");
1746 close(vdev->fd);
1747 if (vdev->msix) {
1748 g_free(vdev->msix);
1749 vdev->msix = NULL;
1750 }
1751}
1752
1753static int vfio_initfn(PCIDevice *pdev)
1754{
1755 VFIODevice *pvdev, *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
1756 VFIOGroup *group;
1757 char path[PATH_MAX], iommu_group_path[PATH_MAX], *group_name;
1758 ssize_t len;
1759 struct stat st;
1760 int groupid;
1761 int ret;
1762
1763 /* Check that the host device exists */
1764 snprintf(path, sizeof(path),
1765 "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/",
1766 vdev->host.domain, vdev->host.bus, vdev->host.slot,
1767 vdev->host.function);
1768 if (stat(path, &st) < 0) {
1769 error_report("vfio: error: no such host device: %s\n", path);
1770 return -errno;
1771 }
1772
1773 strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1);
1774
1775 len = readlink(path, iommu_group_path, PATH_MAX);
1776 if (len <= 0) {
1777 error_report("vfio: error no iommu_group for device\n");
1778 return -errno;
1779 }
1780
1781 iommu_group_path[len] = 0;
1782 group_name = basename(iommu_group_path);
1783
1784 if (sscanf(group_name, "%d", &groupid) != 1) {
1785 error_report("vfio: error reading %s: %m\n", path);
1786 return -errno;
1787 }
1788
1789 DPRINTF("%s(%04x:%02x:%02x.%x) group %d\n", __func__, vdev->host.domain,
1790 vdev->host.bus, vdev->host.slot, vdev->host.function, groupid);
1791
1792 group = vfio_get_group(groupid);
1793 if (!group) {
1794 error_report("vfio: failed to get group %d\n", groupid);
1795 return -ENOENT;
1796 }
1797
1798 snprintf(path, sizeof(path), "%04x:%02x:%02x.%01x",
1799 vdev->host.domain, vdev->host.bus, vdev->host.slot,
1800 vdev->host.function);
1801
1802 QLIST_FOREACH(pvdev, &group->device_list, next) {
1803 if (pvdev->host.domain == vdev->host.domain &&
1804 pvdev->host.bus == vdev->host.bus &&
1805 pvdev->host.slot == vdev->host.slot &&
1806 pvdev->host.function == vdev->host.function) {
1807
1808 error_report("vfio: error: device %s is already attached\n", path);
1809 vfio_put_group(group);
1810 return -EBUSY;
1811 }
1812 }
1813
1814 ret = vfio_get_device(group, path, vdev);
1815 if (ret) {
1816 error_report("vfio: failed to get device %s\n", path);
1817 vfio_put_group(group);
1818 return ret;
1819 }
1820
1821 /* Get a copy of config space */
1822 ret = pread(vdev->fd, vdev->pdev.config,
1823 MIN(pci_config_size(&vdev->pdev), vdev->config_size),
1824 vdev->config_offset);
1825 if (ret < (int)MIN(pci_config_size(&vdev->pdev), vdev->config_size)) {
1826 ret = ret < 0 ? -errno : -EFAULT;
1827 error_report("vfio: Failed to read device config space\n");
1828 goto out_put;
1829 }
1830
1831 /*
1832 * Clear host resource mapping info. If we choose not to register a
1833 * BAR, such as might be the case with the option ROM, we can get
1834 * confusing, unwritable, residual addresses from the host here.
1835 */
1836 memset(&vdev->pdev.config[PCI_BASE_ADDRESS_0], 0, 24);
1837 memset(&vdev->pdev.config[PCI_ROM_ADDRESS], 0, 4);
1838
1839 vfio_load_rom(vdev);
1840
1841 ret = vfio_early_setup_msix(vdev);
1842 if (ret) {
1843 goto out_put;
1844 }
1845
1846 vfio_map_bars(vdev);
1847
1848 ret = vfio_add_capabilities(vdev);
1849 if (ret) {
1850 goto out_teardown;
1851 }
1852
1853 if (vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1)) {
ea486926
AW
1854 vdev->intx.mmap_timer = qemu_new_timer_ms(vm_clock,
1855 vfio_intx_mmap_enable, vdev);
65501a74
AW
1856 ret = vfio_enable_intx(vdev);
1857 if (ret) {
1858 goto out_teardown;
1859 }
1860 }
1861
1862 return 0;
1863
1864out_teardown:
1865 pci_device_set_intx_routing_notifier(&vdev->pdev, NULL);
1866 vfio_teardown_msi(vdev);
1867 vfio_unmap_bars(vdev);
1868out_put:
1869 vfio_put_device(vdev);
1870 vfio_put_group(group);
1871 return ret;
1872}
1873
1874static void vfio_exitfn(PCIDevice *pdev)
1875{
1876 VFIODevice *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
1877 VFIOGroup *group = vdev->group;
1878
1879 pci_device_set_intx_routing_notifier(&vdev->pdev, NULL);
1880 vfio_disable_interrupts(vdev);
ea486926
AW
1881 if (vdev->intx.mmap_timer) {
1882 qemu_free_timer(vdev->intx.mmap_timer);
1883 }
65501a74
AW
1884 vfio_teardown_msi(vdev);
1885 vfio_unmap_bars(vdev);
1886 vfio_put_device(vdev);
1887 vfio_put_group(group);
1888}
1889
1890static void vfio_pci_reset(DeviceState *dev)
1891{
1892 PCIDevice *pdev = DO_UPCAST(PCIDevice, qdev, dev);
1893 VFIODevice *vdev = DO_UPCAST(VFIODevice, pdev, pdev);
5834a83f 1894 uint16_t cmd;
65501a74 1895
5834a83f
AW
1896 DPRINTF("%s(%04x:%02x:%02x.%x)\n", __func__, vdev->host.domain,
1897 vdev->host.bus, vdev->host.slot, vdev->host.function);
1898
1899 vfio_disable_interrupts(vdev);
65501a74 1900
5834a83f
AW
1901 /*
1902 * Stop any ongoing DMA by disconecting I/O, MMIO, and bus master.
1903 * Also put INTx Disable in known state.
1904 */
1905 cmd = vfio_pci_read_config(pdev, PCI_COMMAND, 2);
1906 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
1907 PCI_COMMAND_INTX_DISABLE);
1908 vfio_pci_write_config(pdev, PCI_COMMAND, cmd, 2);
1909
1910 if (vdev->reset_works) {
1911 if (ioctl(vdev->fd, VFIO_DEVICE_RESET)) {
1912 error_report("vfio: Error unable to reset physical device "
1913 "(%04x:%02x:%02x.%x): %m\n", vdev->host.domain,
1914 vdev->host.bus, vdev->host.slot, vdev->host.function);
1915 }
65501a74 1916 }
5834a83f
AW
1917
1918 vfio_enable_intx(vdev);
65501a74
AW
1919}
1920
1921static Property vfio_pci_dev_properties[] = {
1922 DEFINE_PROP_PCI_HOST_DEVADDR("host", VFIODevice, host),
ea486926
AW
1923 DEFINE_PROP_UINT32("x-intx-mmap-timeout-ms", VFIODevice,
1924 intx.mmap_timeout, 1100),
65501a74
AW
1925 /*
1926 * TODO - support passed fds... is this necessary?
1927 * DEFINE_PROP_STRING("vfiofd", VFIODevice, vfiofd_name),
1928 * DEFINE_PROP_STRING("vfiogroupfd, VFIODevice, vfiogroupfd_name),
1929 */
1930 DEFINE_PROP_END_OF_LIST(),
1931};
1932
1933
1934static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
1935{
1936 DeviceClass *dc = DEVICE_CLASS(klass);
1937 PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
1938
1939 dc->reset = vfio_pci_reset;
1940 dc->props = vfio_pci_dev_properties;
1941 pdc->init = vfio_initfn;
1942 pdc->exit = vfio_exitfn;
1943 pdc->config_read = vfio_pci_read_config;
1944 pdc->config_write = vfio_pci_write_config;
1945}
1946
1947static const TypeInfo vfio_pci_dev_info = {
1948 .name = "vfio-pci",
1949 .parent = TYPE_PCI_DEVICE,
1950 .instance_size = sizeof(VFIODevice),
1951 .class_init = vfio_pci_dev_class_init,
1952};
1953
1954static void register_vfio_pci_dev_type(void)
1955{
1956 type_register_static(&vfio_pci_dev_info);
1957}
1958
1959type_init(register_vfio_pci_dev_type)