2 * generic functions used by VFIO devices
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)
21 #include "qemu/osdep.h"
22 #include <sys/ioctl.h>
24 #include <linux/vfio.h>
26 #include "hw/vfio/vfio-common.h"
27 #include "hw/vfio/vfio.h"
28 #include "exec/address-spaces.h"
29 #include "exec/memory.h"
31 #include "qemu/error-report.h"
32 #include "sysemu/kvm.h"
34 #include "linux/kvm.h"
38 struct vfio_group_head vfio_group_list
=
39 QLIST_HEAD_INITIALIZER(vfio_group_list
);
40 struct vfio_as_head vfio_address_spaces
=
41 QLIST_HEAD_INITIALIZER(vfio_address_spaces
);
45 * We have a single VFIO pseudo device per KVM VM. Once created it lives
46 * for the life of the VM. Closing the file descriptor only drops our
47 * reference to it and the device's reference to kvm. Therefore once
48 * initialized, this file descriptor is only released on QEMU exit and
49 * we'll re-use it should another vfio device be attached before then.
51 static int vfio_kvm_device_fd
= -1;
55 * Common VFIO interrupt disable
57 void vfio_disable_irqindex(VFIODevice
*vbasedev
, int index
)
59 struct vfio_irq_set irq_set
= {
60 .argsz
= sizeof(irq_set
),
61 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_TRIGGER
,
67 ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
70 void vfio_unmask_single_irqindex(VFIODevice
*vbasedev
, int index
)
72 struct vfio_irq_set irq_set
= {
73 .argsz
= sizeof(irq_set
),
74 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_UNMASK
,
80 ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
83 void vfio_mask_single_irqindex(VFIODevice
*vbasedev
, int index
)
85 struct vfio_irq_set irq_set
= {
86 .argsz
= sizeof(irq_set
),
87 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_MASK
,
93 ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
97 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
99 void vfio_region_write(void *opaque
, hwaddr addr
,
100 uint64_t data
, unsigned size
)
102 VFIORegion
*region
= opaque
;
103 VFIODevice
*vbasedev
= region
->vbasedev
;
116 buf
.word
= cpu_to_le16(data
);
119 buf
.dword
= cpu_to_le32(data
);
122 hw_error("vfio: unsupported write size, %d bytes", size
);
126 if (pwrite(vbasedev
->fd
, &buf
, size
, region
->fd_offset
+ addr
) != size
) {
127 error_report("%s(%s:region%d+0x%"HWADDR_PRIx
", 0x%"PRIx64
129 __func__
, vbasedev
->name
, region
->nr
,
133 trace_vfio_region_write(vbasedev
->name
, region
->nr
, addr
, data
, size
);
136 * A read or write to a BAR always signals an INTx EOI. This will
137 * do nothing if not pending (including not in INTx mode). We assume
138 * that a BAR access is in response to an interrupt and that BAR
139 * accesses will service the interrupt. Unfortunately, we don't know
140 * which access will service the interrupt, so we're potentially
141 * getting quite a few host interrupts per guest interrupt.
143 vbasedev
->ops
->vfio_eoi(vbasedev
);
146 uint64_t vfio_region_read(void *opaque
,
147 hwaddr addr
, unsigned size
)
149 VFIORegion
*region
= opaque
;
150 VFIODevice
*vbasedev
= region
->vbasedev
;
159 if (pread(vbasedev
->fd
, &buf
, size
, region
->fd_offset
+ addr
) != size
) {
160 error_report("%s(%s:region%d+0x%"HWADDR_PRIx
", %d) failed: %m",
161 __func__
, vbasedev
->name
, region
->nr
,
170 data
= le16_to_cpu(buf
.word
);
173 data
= le32_to_cpu(buf
.dword
);
176 hw_error("vfio: unsupported read size, %d bytes", size
);
180 trace_vfio_region_read(vbasedev
->name
, region
->nr
, addr
, size
, data
);
182 /* Same as write above */
183 vbasedev
->ops
->vfio_eoi(vbasedev
);
188 const MemoryRegionOps vfio_region_ops
= {
189 .read
= vfio_region_read
,
190 .write
= vfio_region_write
,
191 .endianness
= DEVICE_LITTLE_ENDIAN
,
195 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
197 static int vfio_dma_unmap(VFIOContainer
*container
,
198 hwaddr iova
, ram_addr_t size
)
200 struct vfio_iommu_type1_dma_unmap unmap
= {
201 .argsz
= sizeof(unmap
),
207 if (ioctl(container
->fd
, VFIO_IOMMU_UNMAP_DMA
, &unmap
)) {
208 error_report("VFIO_UNMAP_DMA: %d", -errno
);
215 static int vfio_dma_map(VFIOContainer
*container
, hwaddr iova
,
216 ram_addr_t size
, void *vaddr
, bool readonly
)
218 struct vfio_iommu_type1_dma_map map
= {
219 .argsz
= sizeof(map
),
220 .flags
= VFIO_DMA_MAP_FLAG_READ
,
221 .vaddr
= (__u64
)(uintptr_t)vaddr
,
227 map
.flags
|= VFIO_DMA_MAP_FLAG_WRITE
;
231 * Try the mapping, if it fails with EBUSY, unmap the region and try
232 * again. This shouldn't be necessary, but we sometimes see it in
235 if (ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0 ||
236 (errno
== EBUSY
&& vfio_dma_unmap(container
, iova
, size
) == 0 &&
237 ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0)) {
241 error_report("VFIO_MAP_DMA: %d", -errno
);
245 static bool vfio_listener_skipped_section(MemoryRegionSection
*section
)
247 return (!memory_region_is_ram(section
->mr
) &&
248 !memory_region_is_iommu(section
->mr
)) ||
250 * Sizing an enabled 64-bit BAR can cause spurious mappings to
251 * addresses in the upper part of the 64-bit address space. These
252 * are never accessed by the CPU and beyond the address width of
253 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
255 section
->offset_within_address_space
& (1ULL << 63);
258 static void vfio_iommu_map_notify(Notifier
*n
, void *data
)
260 VFIOGuestIOMMU
*giommu
= container_of(n
, VFIOGuestIOMMU
, n
);
261 VFIOContainer
*container
= giommu
->container
;
262 IOMMUTLBEntry
*iotlb
= data
;
265 hwaddr len
= iotlb
->addr_mask
+ 1;
269 trace_vfio_iommu_map_notify(iotlb
->iova
,
270 iotlb
->iova
+ iotlb
->addr_mask
);
273 * The IOMMU TLB entry we have just covers translation through
274 * this IOMMU to its immediate target. We need to translate
275 * it the rest of the way through to memory.
278 mr
= address_space_translate(&address_space_memory
,
279 iotlb
->translated_addr
,
280 &xlat
, &len
, iotlb
->perm
& IOMMU_WO
);
281 if (!memory_region_is_ram(mr
)) {
282 error_report("iommu map to non memory area %"HWADDR_PRIx
"",
287 * Translation truncates length to the IOMMU page size,
288 * check that it did not truncate too much.
290 if (len
& iotlb
->addr_mask
) {
291 error_report("iommu has granularity incompatible with target AS");
295 if ((iotlb
->perm
& IOMMU_RW
) != IOMMU_NONE
) {
296 vaddr
= memory_region_get_ram_ptr(mr
) + xlat
;
297 ret
= vfio_dma_map(container
, iotlb
->iova
,
298 iotlb
->addr_mask
+ 1, vaddr
,
299 !(iotlb
->perm
& IOMMU_WO
) || mr
->readonly
);
301 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
302 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
303 container
, iotlb
->iova
,
304 iotlb
->addr_mask
+ 1, vaddr
, ret
);
307 ret
= vfio_dma_unmap(container
, iotlb
->iova
, iotlb
->addr_mask
+ 1);
309 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
310 "0x%"HWADDR_PRIx
") = %d (%m)",
311 container
, iotlb
->iova
,
312 iotlb
->addr_mask
+ 1, ret
);
319 static hwaddr
vfio_container_granularity(VFIOContainer
*container
)
321 return (hwaddr
)1 << ctz64(container
->iova_pgsizes
);
324 static void vfio_listener_region_add(MemoryListener
*listener
,
325 MemoryRegionSection
*section
)
327 VFIOContainer
*container
= container_of(listener
, VFIOContainer
, listener
);
329 Int128 llend
, llsize
;
333 if (vfio_listener_skipped_section(section
)) {
334 trace_vfio_listener_region_add_skip(
335 section
->offset_within_address_space
,
336 section
->offset_within_address_space
+
337 int128_get64(int128_sub(section
->size
, int128_one())));
341 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
342 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
343 error_report("%s received unaligned region", __func__
);
347 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
348 llend
= int128_make64(section
->offset_within_address_space
);
349 llend
= int128_add(llend
, section
->size
);
350 llend
= int128_and(llend
, int128_exts64(TARGET_PAGE_MASK
));
352 if (int128_ge(int128_make64(iova
), llend
)) {
355 end
= int128_get64(int128_sub(llend
, int128_one()));
357 if ((iova
< container
->min_iova
) || (end
> container
->max_iova
)) {
358 error_report("vfio: IOMMU container %p can't map guest IOVA region"
359 " 0x%"HWADDR_PRIx
"..0x%"HWADDR_PRIx
,
360 container
, iova
, end
);
365 memory_region_ref(section
->mr
);
367 if (memory_region_is_iommu(section
->mr
)) {
368 VFIOGuestIOMMU
*giommu
;
370 trace_vfio_listener_region_add_iommu(iova
, end
);
372 * FIXME: We should do some checking to see if the
373 * capabilities of the host VFIO IOMMU are adequate to model
376 * FIXME: For VFIO iommu types which have KVM acceleration to
377 * avoid bouncing all map/unmaps through qemu this way, this
378 * would be the right place to wire that up (tell the KVM
379 * device emulation the VFIO iommu handles to use).
381 giommu
= g_malloc0(sizeof(*giommu
));
382 giommu
->iommu
= section
->mr
;
383 giommu
->container
= container
;
384 giommu
->n
.notify
= vfio_iommu_map_notify
;
385 QLIST_INSERT_HEAD(&container
->giommu_list
, giommu
, giommu_next
);
387 memory_region_register_iommu_notifier(giommu
->iommu
, &giommu
->n
);
388 memory_region_iommu_replay(giommu
->iommu
, &giommu
->n
,
389 vfio_container_granularity(container
),
395 /* Here we assume that memory_region_is_ram(section->mr)==true */
397 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
398 section
->offset_within_region
+
399 (iova
- section
->offset_within_address_space
);
401 trace_vfio_listener_region_add_ram(iova
, end
, vaddr
);
403 llsize
= int128_sub(llend
, int128_make64(iova
));
405 ret
= vfio_dma_map(container
, iova
, int128_get64(llsize
),
406 vaddr
, section
->readonly
);
408 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
409 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
410 container
, iova
, int128_get64(llsize
), vaddr
, ret
);
418 * On the initfn path, store the first error in the container so we
419 * can gracefully fail. Runtime, there's not much we can do other
420 * than throw a hardware error.
422 if (!container
->initialized
) {
423 if (!container
->error
) {
424 container
->error
= ret
;
427 hw_error("vfio: DMA mapping failed, unable to continue");
431 static void vfio_listener_region_del(MemoryListener
*listener
,
432 MemoryRegionSection
*section
)
434 VFIOContainer
*container
= container_of(listener
, VFIOContainer
, listener
);
438 if (vfio_listener_skipped_section(section
)) {
439 trace_vfio_listener_region_del_skip(
440 section
->offset_within_address_space
,
441 section
->offset_within_address_space
+
442 int128_get64(int128_sub(section
->size
, int128_one())));
446 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
447 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
448 error_report("%s received unaligned region", __func__
);
452 if (memory_region_is_iommu(section
->mr
)) {
453 VFIOGuestIOMMU
*giommu
;
455 QLIST_FOREACH(giommu
, &container
->giommu_list
, giommu_next
) {
456 if (giommu
->iommu
== section
->mr
) {
457 memory_region_unregister_iommu_notifier(&giommu
->n
);
458 QLIST_REMOVE(giommu
, giommu_next
);
465 * FIXME: We assume the one big unmap below is adequate to
466 * remove any individual page mappings in the IOMMU which
467 * might have been copied into VFIO. This works for a page table
468 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
469 * That may not be true for all IOMMU types.
473 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
474 end
= (section
->offset_within_address_space
+ int128_get64(section
->size
)) &
481 trace_vfio_listener_region_del(iova
, end
- 1);
483 ret
= vfio_dma_unmap(container
, iova
, end
- iova
);
484 memory_region_unref(section
->mr
);
486 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
487 "0x%"HWADDR_PRIx
") = %d (%m)",
488 container
, iova
, end
- iova
, ret
);
492 static const MemoryListener vfio_memory_listener
= {
493 .region_add
= vfio_listener_region_add
,
494 .region_del
= vfio_listener_region_del
,
497 static void vfio_listener_release(VFIOContainer
*container
)
499 memory_listener_unregister(&container
->listener
);
502 int vfio_region_setup(Object
*obj
, VFIODevice
*vbasedev
, VFIORegion
*region
,
503 int index
, const char *name
)
505 struct vfio_region_info
*info
;
508 ret
= vfio_get_region_info(vbasedev
, index
, &info
);
513 region
->vbasedev
= vbasedev
;
514 region
->flags
= info
->flags
;
515 region
->size
= info
->size
;
516 region
->fd_offset
= info
->offset
;
520 region
->mem
= g_new0(MemoryRegion
, 1);
521 memory_region_init_io(region
->mem
, obj
, &vfio_region_ops
,
522 region
, name
, region
->size
);
524 if (!vbasedev
->no_mmap
&&
525 region
->flags
& VFIO_REGION_INFO_FLAG_MMAP
&&
526 !(region
->size
& ~qemu_real_host_page_mask
)) {
528 region
->nr_mmaps
= 1;
529 region
->mmaps
= g_new0(VFIOMmap
, region
->nr_mmaps
);
531 region
->mmaps
[0].offset
= 0;
532 region
->mmaps
[0].size
= region
->size
;
538 trace_vfio_region_setup(vbasedev
->name
, index
, name
,
539 region
->flags
, region
->fd_offset
, region
->size
);
543 int vfio_region_mmap(VFIORegion
*region
)
552 prot
|= region
->flags
& VFIO_REGION_INFO_FLAG_READ
? PROT_READ
: 0;
553 prot
|= region
->flags
& VFIO_REGION_INFO_FLAG_WRITE
? PROT_WRITE
: 0;
555 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
556 region
->mmaps
[i
].mmap
= mmap(NULL
, region
->mmaps
[i
].size
, prot
,
557 MAP_SHARED
, region
->vbasedev
->fd
,
559 region
->mmaps
[i
].offset
);
560 if (region
->mmaps
[i
].mmap
== MAP_FAILED
) {
563 trace_vfio_region_mmap_fault(memory_region_name(region
->mem
), i
,
565 region
->mmaps
[i
].offset
,
567 region
->mmaps
[i
].offset
+
568 region
->mmaps
[i
].size
- 1, ret
);
570 region
->mmaps
[i
].mmap
= NULL
;
572 for (i
--; i
>= 0; i
--) {
573 memory_region_del_subregion(region
->mem
, ®ion
->mmaps
[i
].mem
);
574 munmap(region
->mmaps
[i
].mmap
, region
->mmaps
[i
].size
);
575 object_unparent(OBJECT(®ion
->mmaps
[i
].mem
));
576 region
->mmaps
[i
].mmap
= NULL
;
582 name
= g_strdup_printf("%s mmaps[%d]",
583 memory_region_name(region
->mem
), i
);
584 memory_region_init_ram_ptr(®ion
->mmaps
[i
].mem
,
585 memory_region_owner(region
->mem
),
586 name
, region
->mmaps
[i
].size
,
587 region
->mmaps
[i
].mmap
);
589 memory_region_set_skip_dump(®ion
->mmaps
[i
].mem
);
590 memory_region_add_subregion(region
->mem
, region
->mmaps
[i
].offset
,
591 ®ion
->mmaps
[i
].mem
);
593 trace_vfio_region_mmap(memory_region_name(®ion
->mmaps
[i
].mem
),
594 region
->mmaps
[i
].offset
,
595 region
->mmaps
[i
].offset
+
596 region
->mmaps
[i
].size
- 1);
602 void vfio_region_exit(VFIORegion
*region
)
610 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
611 if (region
->mmaps
[i
].mmap
) {
612 memory_region_del_subregion(region
->mem
, ®ion
->mmaps
[i
].mem
);
616 trace_vfio_region_exit(region
->vbasedev
->name
, region
->nr
);
619 void vfio_region_finalize(VFIORegion
*region
)
627 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
628 if (region
->mmaps
[i
].mmap
) {
629 munmap(region
->mmaps
[i
].mmap
, region
->mmaps
[i
].size
);
630 object_unparent(OBJECT(®ion
->mmaps
[i
].mem
));
634 object_unparent(OBJECT(region
->mem
));
637 g_free(region
->mmaps
);
639 trace_vfio_region_finalize(region
->vbasedev
->name
, region
->nr
);
642 void vfio_region_mmaps_set_enabled(VFIORegion
*region
, bool enabled
)
650 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
651 if (region
->mmaps
[i
].mmap
) {
652 memory_region_set_enabled(®ion
->mmaps
[i
].mem
, enabled
);
656 trace_vfio_region_mmaps_set_enabled(memory_region_name(region
->mem
),
660 void vfio_reset_handler(void *opaque
)
663 VFIODevice
*vbasedev
;
665 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
666 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
667 vbasedev
->ops
->vfio_compute_needs_reset(vbasedev
);
671 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
672 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
673 if (vbasedev
->needs_reset
) {
674 vbasedev
->ops
->vfio_hot_reset_multi(vbasedev
);
680 static void vfio_kvm_device_add_group(VFIOGroup
*group
)
683 struct kvm_device_attr attr
= {
684 .group
= KVM_DEV_VFIO_GROUP
,
685 .attr
= KVM_DEV_VFIO_GROUP_ADD
,
686 .addr
= (uint64_t)(unsigned long)&group
->fd
,
689 if (!kvm_enabled()) {
693 if (vfio_kvm_device_fd
< 0) {
694 struct kvm_create_device cd
= {
695 .type
= KVM_DEV_TYPE_VFIO
,
698 if (kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
)) {
699 error_report("Failed to create KVM VFIO device: %m");
703 vfio_kvm_device_fd
= cd
.fd
;
706 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
707 error_report("Failed to add group %d to KVM VFIO device: %m",
713 static void vfio_kvm_device_del_group(VFIOGroup
*group
)
716 struct kvm_device_attr attr
= {
717 .group
= KVM_DEV_VFIO_GROUP
,
718 .attr
= KVM_DEV_VFIO_GROUP_DEL
,
719 .addr
= (uint64_t)(unsigned long)&group
->fd
,
722 if (vfio_kvm_device_fd
< 0) {
726 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
727 error_report("Failed to remove group %d from KVM VFIO device: %m",
733 static VFIOAddressSpace
*vfio_get_address_space(AddressSpace
*as
)
735 VFIOAddressSpace
*space
;
737 QLIST_FOREACH(space
, &vfio_address_spaces
, list
) {
738 if (space
->as
== as
) {
743 /* No suitable VFIOAddressSpace, create a new one */
744 space
= g_malloc0(sizeof(*space
));
746 QLIST_INIT(&space
->containers
);
748 QLIST_INSERT_HEAD(&vfio_address_spaces
, space
, list
);
753 static void vfio_put_address_space(VFIOAddressSpace
*space
)
755 if (QLIST_EMPTY(&space
->containers
)) {
756 QLIST_REMOVE(space
, list
);
761 static int vfio_connect_container(VFIOGroup
*group
, AddressSpace
*as
)
763 VFIOContainer
*container
;
765 VFIOAddressSpace
*space
;
767 space
= vfio_get_address_space(as
);
769 QLIST_FOREACH(container
, &space
->containers
, next
) {
770 if (!ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &container
->fd
)) {
771 group
->container
= container
;
772 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
777 fd
= qemu_open("/dev/vfio/vfio", O_RDWR
);
779 error_report("vfio: failed to open /dev/vfio/vfio: %m");
784 ret
= ioctl(fd
, VFIO_GET_API_VERSION
);
785 if (ret
!= VFIO_API_VERSION
) {
786 error_report("vfio: supported vfio version: %d, "
787 "reported version: %d", VFIO_API_VERSION
, ret
);
792 container
= g_malloc0(sizeof(*container
));
793 container
->space
= space
;
795 if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1_IOMMU
) ||
796 ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1v2_IOMMU
)) {
797 bool v2
= !!ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1v2_IOMMU
);
798 struct vfio_iommu_type1_info info
;
800 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
802 error_report("vfio: failed to set group container: %m");
804 goto free_container_exit
;
807 ret
= ioctl(fd
, VFIO_SET_IOMMU
,
808 v2
? VFIO_TYPE1v2_IOMMU
: VFIO_TYPE1_IOMMU
);
810 error_report("vfio: failed to set iommu for container: %m");
812 goto free_container_exit
;
816 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
817 * IOVA whatsoever. That's not actually true, but the current
818 * kernel interface doesn't tell us what it can map, and the
819 * existing Type1 IOMMUs generally support any IOVA we're
820 * going to actually try in practice.
822 container
->min_iova
= 0;
823 container
->max_iova
= (hwaddr
)-1;
825 /* Assume just 4K IOVA page size */
826 container
->iova_pgsizes
= 0x1000;
827 info
.argsz
= sizeof(info
);
828 ret
= ioctl(fd
, VFIO_IOMMU_GET_INFO
, &info
);
830 if ((ret
== 0) && (info
.flags
& VFIO_IOMMU_INFO_PGSIZES
)) {
831 container
->iova_pgsizes
= info
.iova_pgsizes
;
833 } else if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_IOMMU
)) {
834 struct vfio_iommu_spapr_tce_info info
;
836 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
838 error_report("vfio: failed to set group container: %m");
840 goto free_container_exit
;
842 ret
= ioctl(fd
, VFIO_SET_IOMMU
, VFIO_SPAPR_TCE_IOMMU
);
844 error_report("vfio: failed to set iommu for container: %m");
846 goto free_container_exit
;
850 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
851 * when container fd is closed so we do not call it explicitly
854 ret
= ioctl(fd
, VFIO_IOMMU_ENABLE
);
856 error_report("vfio: failed to enable container: %m");
858 goto free_container_exit
;
862 * This only considers the host IOMMU's 32-bit window. At
863 * some point we need to add support for the optional 64-bit
864 * window and dynamic windows
866 info
.argsz
= sizeof(info
);
867 ret
= ioctl(fd
, VFIO_IOMMU_SPAPR_TCE_GET_INFO
, &info
);
869 error_report("vfio: VFIO_IOMMU_SPAPR_TCE_GET_INFO failed: %m");
871 goto free_container_exit
;
873 container
->min_iova
= info
.dma32_window_start
;
874 container
->max_iova
= container
->min_iova
+ info
.dma32_window_size
- 1;
876 /* Assume just 4K IOVA pages for now */
877 container
->iova_pgsizes
= 0x1000;
879 error_report("vfio: No available IOMMU models");
881 goto free_container_exit
;
884 container
->listener
= vfio_memory_listener
;
886 memory_listener_register(&container
->listener
, container
->space
->as
);
888 if (container
->error
) {
889 ret
= container
->error
;
890 error_report("vfio: memory listener initialization failed for container");
891 goto listener_release_exit
;
894 container
->initialized
= true;
896 QLIST_INIT(&container
->group_list
);
897 QLIST_INSERT_HEAD(&space
->containers
, container
, next
);
899 group
->container
= container
;
900 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
903 listener_release_exit
:
904 vfio_listener_release(container
);
913 vfio_put_address_space(space
);
918 static void vfio_disconnect_container(VFIOGroup
*group
)
920 VFIOContainer
*container
= group
->container
;
922 if (ioctl(group
->fd
, VFIO_GROUP_UNSET_CONTAINER
, &container
->fd
)) {
923 error_report("vfio: error disconnecting group %d from container",
927 QLIST_REMOVE(group
, container_next
);
928 group
->container
= NULL
;
930 if (QLIST_EMPTY(&container
->group_list
)) {
931 VFIOAddressSpace
*space
= container
->space
;
932 VFIOGuestIOMMU
*giommu
, *tmp
;
934 vfio_listener_release(container
);
935 QLIST_REMOVE(container
, next
);
937 QLIST_FOREACH_SAFE(giommu
, &container
->giommu_list
, giommu_next
, tmp
) {
938 memory_region_unregister_iommu_notifier(&giommu
->n
);
939 QLIST_REMOVE(giommu
, giommu_next
);
943 trace_vfio_disconnect_container(container
->fd
);
944 close(container
->fd
);
947 vfio_put_address_space(space
);
951 VFIOGroup
*vfio_get_group(int groupid
, AddressSpace
*as
)
955 struct vfio_group_status status
= { .argsz
= sizeof(status
) };
957 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
958 if (group
->groupid
== groupid
) {
959 /* Found it. Now is it already in the right context? */
960 if (group
->container
->space
->as
== as
) {
963 error_report("vfio: group %d used in multiple address spaces",
970 group
= g_malloc0(sizeof(*group
));
972 snprintf(path
, sizeof(path
), "/dev/vfio/%d", groupid
);
973 group
->fd
= qemu_open(path
, O_RDWR
);
975 error_report("vfio: error opening %s: %m", path
);
976 goto free_group_exit
;
979 if (ioctl(group
->fd
, VFIO_GROUP_GET_STATUS
, &status
)) {
980 error_report("vfio: error getting group status: %m");
984 if (!(status
.flags
& VFIO_GROUP_FLAGS_VIABLE
)) {
985 error_report("vfio: error, group %d is not viable, please ensure "
986 "all devices within the iommu_group are bound to their "
987 "vfio bus driver.", groupid
);
991 group
->groupid
= groupid
;
992 QLIST_INIT(&group
->device_list
);
994 if (vfio_connect_container(group
, as
)) {
995 error_report("vfio: failed to setup container for group %d", groupid
);
999 if (QLIST_EMPTY(&vfio_group_list
)) {
1000 qemu_register_reset(vfio_reset_handler
, NULL
);
1003 QLIST_INSERT_HEAD(&vfio_group_list
, group
, next
);
1005 vfio_kvm_device_add_group(group
);
1018 void vfio_put_group(VFIOGroup
*group
)
1020 if (!group
|| !QLIST_EMPTY(&group
->device_list
)) {
1024 vfio_kvm_device_del_group(group
);
1025 vfio_disconnect_container(group
);
1026 QLIST_REMOVE(group
, next
);
1027 trace_vfio_put_group(group
->fd
);
1031 if (QLIST_EMPTY(&vfio_group_list
)) {
1032 qemu_unregister_reset(vfio_reset_handler
, NULL
);
1036 int vfio_get_device(VFIOGroup
*group
, const char *name
,
1037 VFIODevice
*vbasedev
)
1039 struct vfio_device_info dev_info
= { .argsz
= sizeof(dev_info
) };
1042 fd
= ioctl(group
->fd
, VFIO_GROUP_GET_DEVICE_FD
, name
);
1044 error_report("vfio: error getting device %s from group %d: %m",
1045 name
, group
->groupid
);
1046 error_printf("Verify all devices in group %d are bound to vfio-<bus> "
1047 "or pci-stub and not already in use\n", group
->groupid
);
1051 ret
= ioctl(fd
, VFIO_DEVICE_GET_INFO
, &dev_info
);
1053 error_report("vfio: error getting device info: %m");
1059 vbasedev
->group
= group
;
1060 QLIST_INSERT_HEAD(&group
->device_list
, vbasedev
, next
);
1062 vbasedev
->num_irqs
= dev_info
.num_irqs
;
1063 vbasedev
->num_regions
= dev_info
.num_regions
;
1064 vbasedev
->flags
= dev_info
.flags
;
1066 trace_vfio_get_device(name
, dev_info
.flags
, dev_info
.num_regions
,
1069 vbasedev
->reset_works
= !!(dev_info
.flags
& VFIO_DEVICE_FLAGS_RESET
);
1073 void vfio_put_base_device(VFIODevice
*vbasedev
)
1075 if (!vbasedev
->group
) {
1078 QLIST_REMOVE(vbasedev
, next
);
1079 vbasedev
->group
= NULL
;
1080 trace_vfio_put_base_device(vbasedev
->fd
);
1081 close(vbasedev
->fd
);
1084 int vfio_get_region_info(VFIODevice
*vbasedev
, int index
,
1085 struct vfio_region_info
**info
)
1087 size_t argsz
= sizeof(struct vfio_region_info
);
1089 *info
= g_malloc0(argsz
);
1091 (*info
)->index
= index
;
1092 (*info
)->argsz
= argsz
;
1094 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, *info
)) {
1103 * Interfaces for IBM EEH (Enhanced Error Handling)
1105 static bool vfio_eeh_container_ok(VFIOContainer
*container
)
1108 * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO
1109 * implementation is broken if there are multiple groups in a
1110 * container. The hardware works in units of Partitionable
1111 * Endpoints (== IOMMU groups) and the EEH operations naively
1112 * iterate across all groups in the container, without any logic
1113 * to make sure the groups have their state synchronized. For
1114 * certain operations (ENABLE) that might be ok, until an error
1115 * occurs, but for others (GET_STATE) it's clearly broken.
1119 * XXX Once fixed kernels exist, test for them here
1122 if (QLIST_EMPTY(&container
->group_list
)) {
1126 if (QLIST_NEXT(QLIST_FIRST(&container
->group_list
), container_next
)) {
1133 static int vfio_eeh_container_op(VFIOContainer
*container
, uint32_t op
)
1135 struct vfio_eeh_pe_op pe_op
= {
1136 .argsz
= sizeof(pe_op
),
1141 if (!vfio_eeh_container_ok(container
)) {
1142 error_report("vfio/eeh: EEH_PE_OP 0x%x: "
1143 "kernel requires a container with exactly one group", op
);
1147 ret
= ioctl(container
->fd
, VFIO_EEH_PE_OP
, &pe_op
);
1149 error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op
);
1156 static VFIOContainer
*vfio_eeh_as_container(AddressSpace
*as
)
1158 VFIOAddressSpace
*space
= vfio_get_address_space(as
);
1159 VFIOContainer
*container
= NULL
;
1161 if (QLIST_EMPTY(&space
->containers
)) {
1162 /* No containers to act on */
1166 container
= QLIST_FIRST(&space
->containers
);
1168 if (QLIST_NEXT(container
, next
)) {
1169 /* We don't yet have logic to synchronize EEH state across
1170 * multiple containers */
1176 vfio_put_address_space(space
);
1180 bool vfio_eeh_as_ok(AddressSpace
*as
)
1182 VFIOContainer
*container
= vfio_eeh_as_container(as
);
1184 return (container
!= NULL
) && vfio_eeh_container_ok(container
);
1187 int vfio_eeh_as_op(AddressSpace
*as
, uint32_t op
)
1189 VFIOContainer
*container
= vfio_eeh_as_container(as
);
1194 return vfio_eeh_container_op(container
, op
);