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/kvm.h>
26 #include <linux/vfio.h>
28 #include "hw/vfio/vfio-common.h"
29 #include "hw/vfio/vfio.h"
30 #include "exec/address-spaces.h"
31 #include "exec/memory.h"
33 #include "qemu/error-report.h"
34 #include "qemu/range.h"
35 #include "sysemu/kvm.h"
37 #include "qapi/error.h"
39 struct vfio_group_head vfio_group_list
=
40 QLIST_HEAD_INITIALIZER(vfio_group_list
);
41 struct vfio_as_head vfio_address_spaces
=
42 QLIST_HEAD_INITIALIZER(vfio_address_spaces
);
46 * We have a single VFIO pseudo device per KVM VM. Once created it lives
47 * for the life of the VM. Closing the file descriptor only drops our
48 * reference to it and the device's reference to kvm. Therefore once
49 * initialized, this file descriptor is only released on QEMU exit and
50 * we'll re-use it should another vfio device be attached before then.
52 static int vfio_kvm_device_fd
= -1;
56 * Common VFIO interrupt disable
58 void vfio_disable_irqindex(VFIODevice
*vbasedev
, int index
)
60 struct vfio_irq_set irq_set
= {
61 .argsz
= sizeof(irq_set
),
62 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_TRIGGER
,
68 ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
71 void vfio_unmask_single_irqindex(VFIODevice
*vbasedev
, int index
)
73 struct vfio_irq_set irq_set
= {
74 .argsz
= sizeof(irq_set
),
75 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_UNMASK
,
81 ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
84 void vfio_mask_single_irqindex(VFIODevice
*vbasedev
, int index
)
86 struct vfio_irq_set irq_set
= {
87 .argsz
= sizeof(irq_set
),
88 .flags
= VFIO_IRQ_SET_DATA_NONE
| VFIO_IRQ_SET_ACTION_MASK
,
94 ioctl(vbasedev
->fd
, VFIO_DEVICE_SET_IRQS
, &irq_set
);
98 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
100 void vfio_region_write(void *opaque
, hwaddr addr
,
101 uint64_t data
, unsigned size
)
103 VFIORegion
*region
= opaque
;
104 VFIODevice
*vbasedev
= region
->vbasedev
;
117 buf
.word
= cpu_to_le16(data
);
120 buf
.dword
= cpu_to_le32(data
);
123 hw_error("vfio: unsupported write size, %d bytes", size
);
127 if (pwrite(vbasedev
->fd
, &buf
, size
, region
->fd_offset
+ addr
) != size
) {
128 error_report("%s(%s:region%d+0x%"HWADDR_PRIx
", 0x%"PRIx64
130 __func__
, vbasedev
->name
, region
->nr
,
134 trace_vfio_region_write(vbasedev
->name
, region
->nr
, addr
, data
, size
);
137 * A read or write to a BAR always signals an INTx EOI. This will
138 * do nothing if not pending (including not in INTx mode). We assume
139 * that a BAR access is in response to an interrupt and that BAR
140 * accesses will service the interrupt. Unfortunately, we don't know
141 * which access will service the interrupt, so we're potentially
142 * getting quite a few host interrupts per guest interrupt.
144 vbasedev
->ops
->vfio_eoi(vbasedev
);
147 uint64_t vfio_region_read(void *opaque
,
148 hwaddr addr
, unsigned size
)
150 VFIORegion
*region
= opaque
;
151 VFIODevice
*vbasedev
= region
->vbasedev
;
160 if (pread(vbasedev
->fd
, &buf
, size
, region
->fd_offset
+ addr
) != size
) {
161 error_report("%s(%s:region%d+0x%"HWADDR_PRIx
", %d) failed: %m",
162 __func__
, vbasedev
->name
, region
->nr
,
171 data
= le16_to_cpu(buf
.word
);
174 data
= le32_to_cpu(buf
.dword
);
177 hw_error("vfio: unsupported read size, %d bytes", size
);
181 trace_vfio_region_read(vbasedev
->name
, region
->nr
, addr
, size
, data
);
183 /* Same as write above */
184 vbasedev
->ops
->vfio_eoi(vbasedev
);
189 const MemoryRegionOps vfio_region_ops
= {
190 .read
= vfio_region_read
,
191 .write
= vfio_region_write
,
192 .endianness
= DEVICE_LITTLE_ENDIAN
,
196 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
198 static int vfio_dma_unmap(VFIOContainer
*container
,
199 hwaddr iova
, ram_addr_t size
)
201 struct vfio_iommu_type1_dma_unmap unmap
= {
202 .argsz
= sizeof(unmap
),
208 if (ioctl(container
->fd
, VFIO_IOMMU_UNMAP_DMA
, &unmap
)) {
209 error_report("VFIO_UNMAP_DMA: %d", -errno
);
216 static int vfio_dma_map(VFIOContainer
*container
, hwaddr iova
,
217 ram_addr_t size
, void *vaddr
, bool readonly
)
219 struct vfio_iommu_type1_dma_map map
= {
220 .argsz
= sizeof(map
),
221 .flags
= VFIO_DMA_MAP_FLAG_READ
,
222 .vaddr
= (__u64
)(uintptr_t)vaddr
,
228 map
.flags
|= VFIO_DMA_MAP_FLAG_WRITE
;
232 * Try the mapping, if it fails with EBUSY, unmap the region and try
233 * again. This shouldn't be necessary, but we sometimes see it in
236 if (ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0 ||
237 (errno
== EBUSY
&& vfio_dma_unmap(container
, iova
, size
) == 0 &&
238 ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0)) {
242 error_report("VFIO_MAP_DMA: %d", -errno
);
246 static void vfio_host_win_add(VFIOContainer
*container
,
247 hwaddr min_iova
, hwaddr max_iova
,
248 uint64_t iova_pgsizes
)
250 VFIOHostDMAWindow
*hostwin
;
252 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
253 if (ranges_overlap(hostwin
->min_iova
,
254 hostwin
->max_iova
- hostwin
->min_iova
+ 1,
256 max_iova
- min_iova
+ 1)) {
257 hw_error("%s: Overlapped IOMMU are not enabled", __func__
);
261 hostwin
= g_malloc0(sizeof(*hostwin
));
263 hostwin
->min_iova
= min_iova
;
264 hostwin
->max_iova
= max_iova
;
265 hostwin
->iova_pgsizes
= iova_pgsizes
;
266 QLIST_INSERT_HEAD(&container
->hostwin_list
, hostwin
, hostwin_next
);
269 static int vfio_host_win_del(VFIOContainer
*container
, hwaddr min_iova
,
272 VFIOHostDMAWindow
*hostwin
;
274 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
275 if (hostwin
->min_iova
== min_iova
&& hostwin
->max_iova
== max_iova
) {
276 QLIST_REMOVE(hostwin
, hostwin_next
);
284 static bool vfio_listener_skipped_section(MemoryRegionSection
*section
)
286 return (!memory_region_is_ram(section
->mr
) &&
287 !memory_region_is_iommu(section
->mr
)) ||
289 * Sizing an enabled 64-bit BAR can cause spurious mappings to
290 * addresses in the upper part of the 64-bit address space. These
291 * are never accessed by the CPU and beyond the address width of
292 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
294 section
->offset_within_address_space
& (1ULL << 63);
297 /* Called with rcu_read_lock held. */
298 static bool vfio_get_vaddr(IOMMUTLBEntry
*iotlb
, void **vaddr
,
303 hwaddr len
= iotlb
->addr_mask
+ 1;
304 bool writable
= iotlb
->perm
& IOMMU_WO
;
307 * The IOMMU TLB entry we have just covers translation through
308 * this IOMMU to its immediate target. We need to translate
309 * it the rest of the way through to memory.
311 mr
= address_space_translate(&address_space_memory
,
312 iotlb
->translated_addr
,
313 &xlat
, &len
, writable
);
314 if (!memory_region_is_ram(mr
)) {
315 error_report("iommu map to non memory area %"HWADDR_PRIx
"",
321 * Translation truncates length to the IOMMU page size,
322 * check that it did not truncate too much.
324 if (len
& iotlb
->addr_mask
) {
325 error_report("iommu has granularity incompatible with target AS");
329 *vaddr
= memory_region_get_ram_ptr(mr
) + xlat
;
330 *read_only
= !writable
|| mr
->readonly
;
335 static void vfio_iommu_map_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
337 VFIOGuestIOMMU
*giommu
= container_of(n
, VFIOGuestIOMMU
, n
);
338 VFIOContainer
*container
= giommu
->container
;
339 hwaddr iova
= iotlb
->iova
+ giommu
->iommu_offset
;
344 trace_vfio_iommu_map_notify(iotlb
->perm
== IOMMU_NONE
? "UNMAP" : "MAP",
345 iova
, iova
+ iotlb
->addr_mask
);
347 if (iotlb
->target_as
!= &address_space_memory
) {
348 error_report("Wrong target AS \"%s\", only system memory is allowed",
349 iotlb
->target_as
->name
? iotlb
->target_as
->name
: "none");
355 if ((iotlb
->perm
& IOMMU_RW
) != IOMMU_NONE
) {
356 if (!vfio_get_vaddr(iotlb
, &vaddr
, &read_only
)) {
360 * vaddr is only valid until rcu_read_unlock(). But after
361 * vfio_dma_map has set up the mapping the pages will be
362 * pinned by the kernel. This makes sure that the RAM backend
363 * of vaddr will always be there, even if the memory object is
364 * destroyed and its backing memory munmap-ed.
366 ret
= vfio_dma_map(container
, iova
,
367 iotlb
->addr_mask
+ 1, vaddr
,
370 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
371 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
373 iotlb
->addr_mask
+ 1, vaddr
, ret
);
376 ret
= vfio_dma_unmap(container
, iova
, iotlb
->addr_mask
+ 1);
378 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
379 "0x%"HWADDR_PRIx
") = %d (%m)",
381 iotlb
->addr_mask
+ 1, ret
);
388 static void vfio_listener_region_add(MemoryListener
*listener
,
389 MemoryRegionSection
*section
)
391 VFIOContainer
*container
= container_of(listener
, VFIOContainer
, listener
);
393 Int128 llend
, llsize
;
396 VFIOHostDMAWindow
*hostwin
;
399 if (vfio_listener_skipped_section(section
)) {
400 trace_vfio_listener_region_add_skip(
401 section
->offset_within_address_space
,
402 section
->offset_within_address_space
+
403 int128_get64(int128_sub(section
->size
, int128_one())));
407 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
408 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
409 error_report("%s received unaligned region", __func__
);
413 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
414 llend
= int128_make64(section
->offset_within_address_space
);
415 llend
= int128_add(llend
, section
->size
);
416 llend
= int128_and(llend
, int128_exts64(TARGET_PAGE_MASK
));
418 if (int128_ge(int128_make64(iova
), llend
)) {
421 end
= int128_get64(int128_sub(llend
, int128_one()));
423 if (container
->iommu_type
== VFIO_SPAPR_TCE_v2_IOMMU
) {
424 VFIOHostDMAWindow
*hostwin
;
427 /* For now intersections are not allowed, we may relax this later */
428 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
429 if (ranges_overlap(hostwin
->min_iova
,
430 hostwin
->max_iova
- hostwin
->min_iova
+ 1,
431 section
->offset_within_address_space
,
432 int128_get64(section
->size
))) {
438 ret
= vfio_spapr_create_window(container
, section
, &pgsize
);
443 vfio_host_win_add(container
, section
->offset_within_address_space
,
444 section
->offset_within_address_space
+
445 int128_get64(section
->size
) - 1, pgsize
);
448 hostwin_found
= false;
449 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
450 if (hostwin
->min_iova
<= iova
&& end
<= hostwin
->max_iova
) {
451 hostwin_found
= true;
456 if (!hostwin_found
) {
457 error_report("vfio: IOMMU container %p can't map guest IOVA region"
458 " 0x%"HWADDR_PRIx
"..0x%"HWADDR_PRIx
,
459 container
, iova
, end
);
464 memory_region_ref(section
->mr
);
466 if (memory_region_is_iommu(section
->mr
)) {
467 VFIOGuestIOMMU
*giommu
;
469 trace_vfio_listener_region_add_iommu(iova
, end
);
471 * FIXME: For VFIO iommu types which have KVM acceleration to
472 * avoid bouncing all map/unmaps through qemu this way, this
473 * would be the right place to wire that up (tell the KVM
474 * device emulation the VFIO iommu handles to use).
476 giommu
= g_malloc0(sizeof(*giommu
));
477 giommu
->iommu
= section
->mr
;
478 giommu
->iommu_offset
= section
->offset_within_address_space
-
479 section
->offset_within_region
;
480 giommu
->container
= container
;
481 giommu
->n
.notify
= vfio_iommu_map_notify
;
482 giommu
->n
.notifier_flags
= IOMMU_NOTIFIER_ALL
;
483 QLIST_INSERT_HEAD(&container
->giommu_list
, giommu
, giommu_next
);
485 memory_region_register_iommu_notifier(giommu
->iommu
, &giommu
->n
);
486 memory_region_iommu_replay(giommu
->iommu
, &giommu
->n
, false);
491 /* Here we assume that memory_region_is_ram(section->mr)==true */
493 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
494 section
->offset_within_region
+
495 (iova
- section
->offset_within_address_space
);
497 trace_vfio_listener_region_add_ram(iova
, end
, vaddr
);
499 llsize
= int128_sub(llend
, int128_make64(iova
));
501 ret
= vfio_dma_map(container
, iova
, int128_get64(llsize
),
502 vaddr
, section
->readonly
);
504 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
505 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
506 container
, iova
, int128_get64(llsize
), vaddr
, ret
);
514 * On the initfn path, store the first error in the container so we
515 * can gracefully fail. Runtime, there's not much we can do other
516 * than throw a hardware error.
518 if (!container
->initialized
) {
519 if (!container
->error
) {
520 container
->error
= ret
;
523 hw_error("vfio: DMA mapping failed, unable to continue");
527 static void vfio_listener_region_del(MemoryListener
*listener
,
528 MemoryRegionSection
*section
)
530 VFIOContainer
*container
= container_of(listener
, VFIOContainer
, listener
);
532 Int128 llend
, llsize
;
535 if (vfio_listener_skipped_section(section
)) {
536 trace_vfio_listener_region_del_skip(
537 section
->offset_within_address_space
,
538 section
->offset_within_address_space
+
539 int128_get64(int128_sub(section
->size
, int128_one())));
543 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
544 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
545 error_report("%s received unaligned region", __func__
);
549 if (memory_region_is_iommu(section
->mr
)) {
550 VFIOGuestIOMMU
*giommu
;
552 QLIST_FOREACH(giommu
, &container
->giommu_list
, giommu_next
) {
553 if (giommu
->iommu
== section
->mr
) {
554 memory_region_unregister_iommu_notifier(giommu
->iommu
,
556 QLIST_REMOVE(giommu
, giommu_next
);
563 * FIXME: We assume the one big unmap below is adequate to
564 * remove any individual page mappings in the IOMMU which
565 * might have been copied into VFIO. This works for a page table
566 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
567 * That may not be true for all IOMMU types.
571 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
572 llend
= int128_make64(section
->offset_within_address_space
);
573 llend
= int128_add(llend
, section
->size
);
574 llend
= int128_and(llend
, int128_exts64(TARGET_PAGE_MASK
));
576 if (int128_ge(int128_make64(iova
), llend
)) {
579 end
= int128_get64(int128_sub(llend
, int128_one()));
581 llsize
= int128_sub(llend
, int128_make64(iova
));
583 trace_vfio_listener_region_del(iova
, end
);
585 ret
= vfio_dma_unmap(container
, iova
, int128_get64(llsize
));
586 memory_region_unref(section
->mr
);
588 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
589 "0x%"HWADDR_PRIx
") = %d (%m)",
590 container
, iova
, int128_get64(llsize
), ret
);
593 if (container
->iommu_type
== VFIO_SPAPR_TCE_v2_IOMMU
) {
594 vfio_spapr_remove_window(container
,
595 section
->offset_within_address_space
);
596 if (vfio_host_win_del(container
,
597 section
->offset_within_address_space
,
598 section
->offset_within_address_space
+
599 int128_get64(section
->size
) - 1) < 0) {
600 hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx
,
601 __func__
, section
->offset_within_address_space
);
606 static const MemoryListener vfio_memory_listener
= {
607 .region_add
= vfio_listener_region_add
,
608 .region_del
= vfio_listener_region_del
,
611 static void vfio_listener_release(VFIOContainer
*container
)
613 memory_listener_unregister(&container
->listener
);
614 if (container
->iommu_type
== VFIO_SPAPR_TCE_v2_IOMMU
) {
615 memory_listener_unregister(&container
->prereg_listener
);
619 static struct vfio_info_cap_header
*
620 vfio_get_region_info_cap(struct vfio_region_info
*info
, uint16_t id
)
622 struct vfio_info_cap_header
*hdr
;
625 if (!(info
->flags
& VFIO_REGION_INFO_FLAG_CAPS
)) {
629 for (hdr
= ptr
+ info
->cap_offset
; hdr
!= ptr
; hdr
= ptr
+ hdr
->next
) {
638 static int vfio_setup_region_sparse_mmaps(VFIORegion
*region
,
639 struct vfio_region_info
*info
)
641 struct vfio_info_cap_header
*hdr
;
642 struct vfio_region_info_cap_sparse_mmap
*sparse
;
645 hdr
= vfio_get_region_info_cap(info
, VFIO_REGION_INFO_CAP_SPARSE_MMAP
);
650 sparse
= container_of(hdr
, struct vfio_region_info_cap_sparse_mmap
, header
);
652 trace_vfio_region_sparse_mmap_header(region
->vbasedev
->name
,
653 region
->nr
, sparse
->nr_areas
);
655 region
->mmaps
= g_new0(VFIOMmap
, sparse
->nr_areas
);
657 for (i
= 0, j
= 0; i
< sparse
->nr_areas
; i
++) {
658 trace_vfio_region_sparse_mmap_entry(i
, sparse
->areas
[i
].offset
,
659 sparse
->areas
[i
].offset
+
660 sparse
->areas
[i
].size
);
662 if (sparse
->areas
[i
].size
) {
663 region
->mmaps
[j
].offset
= sparse
->areas
[i
].offset
;
664 region
->mmaps
[j
].size
= sparse
->areas
[i
].size
;
669 region
->nr_mmaps
= j
;
670 region
->mmaps
= g_realloc(region
->mmaps
, j
* sizeof(VFIOMmap
));
675 int vfio_region_setup(Object
*obj
, VFIODevice
*vbasedev
, VFIORegion
*region
,
676 int index
, const char *name
)
678 struct vfio_region_info
*info
;
681 ret
= vfio_get_region_info(vbasedev
, index
, &info
);
686 region
->vbasedev
= vbasedev
;
687 region
->flags
= info
->flags
;
688 region
->size
= info
->size
;
689 region
->fd_offset
= info
->offset
;
693 region
->mem
= g_new0(MemoryRegion
, 1);
694 memory_region_init_io(region
->mem
, obj
, &vfio_region_ops
,
695 region
, name
, region
->size
);
697 if (!vbasedev
->no_mmap
&&
698 region
->flags
& VFIO_REGION_INFO_FLAG_MMAP
) {
700 ret
= vfio_setup_region_sparse_mmaps(region
, info
);
703 region
->nr_mmaps
= 1;
704 region
->mmaps
= g_new0(VFIOMmap
, region
->nr_mmaps
);
705 region
->mmaps
[0].offset
= 0;
706 region
->mmaps
[0].size
= region
->size
;
713 trace_vfio_region_setup(vbasedev
->name
, index
, name
,
714 region
->flags
, region
->fd_offset
, region
->size
);
718 int vfio_region_mmap(VFIORegion
*region
)
727 prot
|= region
->flags
& VFIO_REGION_INFO_FLAG_READ
? PROT_READ
: 0;
728 prot
|= region
->flags
& VFIO_REGION_INFO_FLAG_WRITE
? PROT_WRITE
: 0;
730 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
731 region
->mmaps
[i
].mmap
= mmap(NULL
, region
->mmaps
[i
].size
, prot
,
732 MAP_SHARED
, region
->vbasedev
->fd
,
734 region
->mmaps
[i
].offset
);
735 if (region
->mmaps
[i
].mmap
== MAP_FAILED
) {
738 trace_vfio_region_mmap_fault(memory_region_name(region
->mem
), i
,
740 region
->mmaps
[i
].offset
,
742 region
->mmaps
[i
].offset
+
743 region
->mmaps
[i
].size
- 1, ret
);
745 region
->mmaps
[i
].mmap
= NULL
;
747 for (i
--; i
>= 0; i
--) {
748 memory_region_del_subregion(region
->mem
, ®ion
->mmaps
[i
].mem
);
749 munmap(region
->mmaps
[i
].mmap
, region
->mmaps
[i
].size
);
750 object_unparent(OBJECT(®ion
->mmaps
[i
].mem
));
751 region
->mmaps
[i
].mmap
= NULL
;
757 name
= g_strdup_printf("%s mmaps[%d]",
758 memory_region_name(region
->mem
), i
);
759 memory_region_init_ram_device_ptr(®ion
->mmaps
[i
].mem
,
760 memory_region_owner(region
->mem
),
761 name
, region
->mmaps
[i
].size
,
762 region
->mmaps
[i
].mmap
);
764 memory_region_add_subregion(region
->mem
, region
->mmaps
[i
].offset
,
765 ®ion
->mmaps
[i
].mem
);
767 trace_vfio_region_mmap(memory_region_name(®ion
->mmaps
[i
].mem
),
768 region
->mmaps
[i
].offset
,
769 region
->mmaps
[i
].offset
+
770 region
->mmaps
[i
].size
- 1);
776 void vfio_region_exit(VFIORegion
*region
)
784 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
785 if (region
->mmaps
[i
].mmap
) {
786 memory_region_del_subregion(region
->mem
, ®ion
->mmaps
[i
].mem
);
790 trace_vfio_region_exit(region
->vbasedev
->name
, region
->nr
);
793 void vfio_region_finalize(VFIORegion
*region
)
801 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
802 if (region
->mmaps
[i
].mmap
) {
803 munmap(region
->mmaps
[i
].mmap
, region
->mmaps
[i
].size
);
804 object_unparent(OBJECT(®ion
->mmaps
[i
].mem
));
808 object_unparent(OBJECT(region
->mem
));
811 g_free(region
->mmaps
);
813 trace_vfio_region_finalize(region
->vbasedev
->name
, region
->nr
);
816 void vfio_region_mmaps_set_enabled(VFIORegion
*region
, bool enabled
)
824 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
825 if (region
->mmaps
[i
].mmap
) {
826 memory_region_set_enabled(®ion
->mmaps
[i
].mem
, enabled
);
830 trace_vfio_region_mmaps_set_enabled(memory_region_name(region
->mem
),
834 void vfio_reset_handler(void *opaque
)
837 VFIODevice
*vbasedev
;
839 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
840 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
841 vbasedev
->ops
->vfio_compute_needs_reset(vbasedev
);
845 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
846 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
847 if (vbasedev
->needs_reset
) {
848 vbasedev
->ops
->vfio_hot_reset_multi(vbasedev
);
854 static void vfio_kvm_device_add_group(VFIOGroup
*group
)
857 struct kvm_device_attr attr
= {
858 .group
= KVM_DEV_VFIO_GROUP
,
859 .attr
= KVM_DEV_VFIO_GROUP_ADD
,
860 .addr
= (uint64_t)(unsigned long)&group
->fd
,
863 if (!kvm_enabled()) {
867 if (vfio_kvm_device_fd
< 0) {
868 struct kvm_create_device cd
= {
869 .type
= KVM_DEV_TYPE_VFIO
,
872 if (kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
)) {
873 error_report("Failed to create KVM VFIO device: %m");
877 vfio_kvm_device_fd
= cd
.fd
;
880 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
881 error_report("Failed to add group %d to KVM VFIO device: %m",
887 static void vfio_kvm_device_del_group(VFIOGroup
*group
)
890 struct kvm_device_attr attr
= {
891 .group
= KVM_DEV_VFIO_GROUP
,
892 .attr
= KVM_DEV_VFIO_GROUP_DEL
,
893 .addr
= (uint64_t)(unsigned long)&group
->fd
,
896 if (vfio_kvm_device_fd
< 0) {
900 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
901 error_report("Failed to remove group %d from KVM VFIO device: %m",
907 static VFIOAddressSpace
*vfio_get_address_space(AddressSpace
*as
)
909 VFIOAddressSpace
*space
;
911 QLIST_FOREACH(space
, &vfio_address_spaces
, list
) {
912 if (space
->as
== as
) {
917 /* No suitable VFIOAddressSpace, create a new one */
918 space
= g_malloc0(sizeof(*space
));
920 QLIST_INIT(&space
->containers
);
922 QLIST_INSERT_HEAD(&vfio_address_spaces
, space
, list
);
927 static void vfio_put_address_space(VFIOAddressSpace
*space
)
929 if (QLIST_EMPTY(&space
->containers
)) {
930 QLIST_REMOVE(space
, list
);
935 static int vfio_connect_container(VFIOGroup
*group
, AddressSpace
*as
,
938 VFIOContainer
*container
;
940 VFIOAddressSpace
*space
;
942 space
= vfio_get_address_space(as
);
944 QLIST_FOREACH(container
, &space
->containers
, next
) {
945 if (!ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &container
->fd
)) {
946 group
->container
= container
;
947 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
952 fd
= qemu_open("/dev/vfio/vfio", O_RDWR
);
954 error_setg_errno(errp
, errno
, "failed to open /dev/vfio/vfio");
959 ret
= ioctl(fd
, VFIO_GET_API_VERSION
);
960 if (ret
!= VFIO_API_VERSION
) {
961 error_setg(errp
, "supported vfio version: %d, "
962 "reported version: %d", VFIO_API_VERSION
, ret
);
967 container
= g_malloc0(sizeof(*container
));
968 container
->space
= space
;
970 if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1_IOMMU
) ||
971 ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1v2_IOMMU
)) {
972 bool v2
= !!ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1v2_IOMMU
);
973 struct vfio_iommu_type1_info info
;
975 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
977 error_setg_errno(errp
, errno
, "failed to set group container");
979 goto free_container_exit
;
982 container
->iommu_type
= v2
? VFIO_TYPE1v2_IOMMU
: VFIO_TYPE1_IOMMU
;
983 ret
= ioctl(fd
, VFIO_SET_IOMMU
, container
->iommu_type
);
985 error_setg_errno(errp
, errno
, "failed to set iommu for container");
987 goto free_container_exit
;
991 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
992 * IOVA whatsoever. That's not actually true, but the current
993 * kernel interface doesn't tell us what it can map, and the
994 * existing Type1 IOMMUs generally support any IOVA we're
995 * going to actually try in practice.
997 info
.argsz
= sizeof(info
);
998 ret
= ioctl(fd
, VFIO_IOMMU_GET_INFO
, &info
);
1000 if (ret
|| !(info
.flags
& VFIO_IOMMU_INFO_PGSIZES
)) {
1001 /* Assume 4k IOVA page size */
1002 info
.iova_pgsizes
= 4096;
1004 vfio_host_win_add(container
, 0, (hwaddr
)-1, info
.iova_pgsizes
);
1005 } else if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_IOMMU
) ||
1006 ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_v2_IOMMU
)) {
1007 struct vfio_iommu_spapr_tce_info info
;
1008 bool v2
= !!ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_v2_IOMMU
);
1010 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
1012 error_setg_errno(errp
, errno
, "failed to set group container");
1014 goto free_container_exit
;
1016 container
->iommu_type
=
1017 v2
? VFIO_SPAPR_TCE_v2_IOMMU
: VFIO_SPAPR_TCE_IOMMU
;
1018 ret
= ioctl(fd
, VFIO_SET_IOMMU
, container
->iommu_type
);
1020 error_setg_errno(errp
, errno
, "failed to set iommu for container");
1022 goto free_container_exit
;
1026 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
1027 * when container fd is closed so we do not call it explicitly
1031 ret
= ioctl(fd
, VFIO_IOMMU_ENABLE
);
1033 error_setg_errno(errp
, errno
, "failed to enable container");
1035 goto free_container_exit
;
1038 container
->prereg_listener
= vfio_prereg_listener
;
1040 memory_listener_register(&container
->prereg_listener
,
1041 &address_space_memory
);
1042 if (container
->error
) {
1043 memory_listener_unregister(&container
->prereg_listener
);
1044 ret
= container
->error
;
1046 "RAM memory listener initialization failed for container");
1047 goto free_container_exit
;
1051 info
.argsz
= sizeof(info
);
1052 ret
= ioctl(fd
, VFIO_IOMMU_SPAPR_TCE_GET_INFO
, &info
);
1054 error_setg_errno(errp
, errno
,
1055 "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed");
1058 memory_listener_unregister(&container
->prereg_listener
);
1060 goto free_container_exit
;
1065 * There is a default window in just created container.
1066 * To make region_add/del simpler, we better remove this
1067 * window now and let those iommu_listener callbacks
1068 * create/remove them when needed.
1070 ret
= vfio_spapr_remove_window(container
, info
.dma32_window_start
);
1072 error_setg_errno(errp
, -ret
,
1073 "failed to remove existing window");
1074 goto free_container_exit
;
1077 /* The default table uses 4K pages */
1078 vfio_host_win_add(container
, info
.dma32_window_start
,
1079 info
.dma32_window_start
+
1080 info
.dma32_window_size
- 1,
1084 error_setg(errp
, "No available IOMMU models");
1086 goto free_container_exit
;
1089 container
->listener
= vfio_memory_listener
;
1091 memory_listener_register(&container
->listener
, container
->space
->as
);
1093 if (container
->error
) {
1094 ret
= container
->error
;
1095 error_setg_errno(errp
, -ret
,
1096 "memory listener initialization failed for container");
1097 goto listener_release_exit
;
1100 container
->initialized
= true;
1102 QLIST_INIT(&container
->group_list
);
1103 QLIST_INSERT_HEAD(&space
->containers
, container
, next
);
1105 group
->container
= container
;
1106 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
1109 listener_release_exit
:
1110 vfio_listener_release(container
);
1112 free_container_exit
:
1119 vfio_put_address_space(space
);
1124 static void vfio_disconnect_container(VFIOGroup
*group
)
1126 VFIOContainer
*container
= group
->container
;
1128 if (ioctl(group
->fd
, VFIO_GROUP_UNSET_CONTAINER
, &container
->fd
)) {
1129 error_report("vfio: error disconnecting group %d from container",
1133 QLIST_REMOVE(group
, container_next
);
1134 group
->container
= NULL
;
1136 if (QLIST_EMPTY(&container
->group_list
)) {
1137 VFIOAddressSpace
*space
= container
->space
;
1138 VFIOGuestIOMMU
*giommu
, *tmp
;
1140 vfio_listener_release(container
);
1141 QLIST_REMOVE(container
, next
);
1143 QLIST_FOREACH_SAFE(giommu
, &container
->giommu_list
, giommu_next
, tmp
) {
1144 memory_region_unregister_iommu_notifier(giommu
->iommu
, &giommu
->n
);
1145 QLIST_REMOVE(giommu
, giommu_next
);
1149 trace_vfio_disconnect_container(container
->fd
);
1150 close(container
->fd
);
1153 vfio_put_address_space(space
);
1157 VFIOGroup
*vfio_get_group(int groupid
, AddressSpace
*as
, Error
**errp
)
1161 struct vfio_group_status status
= { .argsz
= sizeof(status
) };
1163 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
1164 if (group
->groupid
== groupid
) {
1165 /* Found it. Now is it already in the right context? */
1166 if (group
->container
->space
->as
== as
) {
1169 error_setg(errp
, "group %d used in multiple address spaces",
1176 group
= g_malloc0(sizeof(*group
));
1178 snprintf(path
, sizeof(path
), "/dev/vfio/%d", groupid
);
1179 group
->fd
= qemu_open(path
, O_RDWR
);
1180 if (group
->fd
< 0) {
1181 error_setg_errno(errp
, errno
, "failed to open %s", path
);
1182 goto free_group_exit
;
1185 if (ioctl(group
->fd
, VFIO_GROUP_GET_STATUS
, &status
)) {
1186 error_setg_errno(errp
, errno
, "failed to get group %d status", groupid
);
1190 if (!(status
.flags
& VFIO_GROUP_FLAGS_VIABLE
)) {
1191 error_setg(errp
, "group %d is not viable", groupid
);
1192 error_append_hint(errp
,
1193 "Please ensure all devices within the iommu_group "
1194 "are bound to their vfio bus driver.\n");
1198 group
->groupid
= groupid
;
1199 QLIST_INIT(&group
->device_list
);
1201 if (vfio_connect_container(group
, as
, errp
)) {
1202 error_prepend(errp
, "failed to setup container for group %d: ",
1207 if (QLIST_EMPTY(&vfio_group_list
)) {
1208 qemu_register_reset(vfio_reset_handler
, NULL
);
1211 QLIST_INSERT_HEAD(&vfio_group_list
, group
, next
);
1213 vfio_kvm_device_add_group(group
);
1226 void vfio_put_group(VFIOGroup
*group
)
1228 if (!group
|| !QLIST_EMPTY(&group
->device_list
)) {
1232 vfio_kvm_device_del_group(group
);
1233 vfio_disconnect_container(group
);
1234 QLIST_REMOVE(group
, next
);
1235 trace_vfio_put_group(group
->fd
);
1239 if (QLIST_EMPTY(&vfio_group_list
)) {
1240 qemu_unregister_reset(vfio_reset_handler
, NULL
);
1244 int vfio_get_device(VFIOGroup
*group
, const char *name
,
1245 VFIODevice
*vbasedev
, Error
**errp
)
1247 struct vfio_device_info dev_info
= { .argsz
= sizeof(dev_info
) };
1250 fd
= ioctl(group
->fd
, VFIO_GROUP_GET_DEVICE_FD
, name
);
1252 error_setg_errno(errp
, errno
, "error getting device from group %d",
1254 error_append_hint(errp
,
1255 "Verify all devices in group %d are bound to vfio-<bus> "
1256 "or pci-stub and not already in use\n", group
->groupid
);
1260 ret
= ioctl(fd
, VFIO_DEVICE_GET_INFO
, &dev_info
);
1262 error_setg_errno(errp
, errno
, "error getting device info");
1268 vbasedev
->group
= group
;
1269 QLIST_INSERT_HEAD(&group
->device_list
, vbasedev
, next
);
1271 vbasedev
->num_irqs
= dev_info
.num_irqs
;
1272 vbasedev
->num_regions
= dev_info
.num_regions
;
1273 vbasedev
->flags
= dev_info
.flags
;
1275 trace_vfio_get_device(name
, dev_info
.flags
, dev_info
.num_regions
,
1278 vbasedev
->reset_works
= !!(dev_info
.flags
& VFIO_DEVICE_FLAGS_RESET
);
1282 void vfio_put_base_device(VFIODevice
*vbasedev
)
1284 if (!vbasedev
->group
) {
1287 QLIST_REMOVE(vbasedev
, next
);
1288 vbasedev
->group
= NULL
;
1289 trace_vfio_put_base_device(vbasedev
->fd
);
1290 close(vbasedev
->fd
);
1293 int vfio_get_region_info(VFIODevice
*vbasedev
, int index
,
1294 struct vfio_region_info
**info
)
1296 size_t argsz
= sizeof(struct vfio_region_info
);
1298 *info
= g_malloc0(argsz
);
1300 (*info
)->index
= index
;
1302 (*info
)->argsz
= argsz
;
1304 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, *info
)) {
1310 if ((*info
)->argsz
> argsz
) {
1311 argsz
= (*info
)->argsz
;
1312 *info
= g_realloc(*info
, argsz
);
1320 int vfio_get_dev_region_info(VFIODevice
*vbasedev
, uint32_t type
,
1321 uint32_t subtype
, struct vfio_region_info
**info
)
1325 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
1326 struct vfio_info_cap_header
*hdr
;
1327 struct vfio_region_info_cap_type
*cap_type
;
1329 if (vfio_get_region_info(vbasedev
, i
, info
)) {
1333 hdr
= vfio_get_region_info_cap(*info
, VFIO_REGION_INFO_CAP_TYPE
);
1339 cap_type
= container_of(hdr
, struct vfio_region_info_cap_type
, header
);
1341 trace_vfio_get_dev_region(vbasedev
->name
, i
,
1342 cap_type
->type
, cap_type
->subtype
);
1344 if (cap_type
->type
== type
&& cap_type
->subtype
== subtype
) {
1356 * Interfaces for IBM EEH (Enhanced Error Handling)
1358 static bool vfio_eeh_container_ok(VFIOContainer
*container
)
1361 * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO
1362 * implementation is broken if there are multiple groups in a
1363 * container. The hardware works in units of Partitionable
1364 * Endpoints (== IOMMU groups) and the EEH operations naively
1365 * iterate across all groups in the container, without any logic
1366 * to make sure the groups have their state synchronized. For
1367 * certain operations (ENABLE) that might be ok, until an error
1368 * occurs, but for others (GET_STATE) it's clearly broken.
1372 * XXX Once fixed kernels exist, test for them here
1375 if (QLIST_EMPTY(&container
->group_list
)) {
1379 if (QLIST_NEXT(QLIST_FIRST(&container
->group_list
), container_next
)) {
1386 static int vfio_eeh_container_op(VFIOContainer
*container
, uint32_t op
)
1388 struct vfio_eeh_pe_op pe_op
= {
1389 .argsz
= sizeof(pe_op
),
1394 if (!vfio_eeh_container_ok(container
)) {
1395 error_report("vfio/eeh: EEH_PE_OP 0x%x: "
1396 "kernel requires a container with exactly one group", op
);
1400 ret
= ioctl(container
->fd
, VFIO_EEH_PE_OP
, &pe_op
);
1402 error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op
);
1409 static VFIOContainer
*vfio_eeh_as_container(AddressSpace
*as
)
1411 VFIOAddressSpace
*space
= vfio_get_address_space(as
);
1412 VFIOContainer
*container
= NULL
;
1414 if (QLIST_EMPTY(&space
->containers
)) {
1415 /* No containers to act on */
1419 container
= QLIST_FIRST(&space
->containers
);
1421 if (QLIST_NEXT(container
, next
)) {
1422 /* We don't yet have logic to synchronize EEH state across
1423 * multiple containers */
1429 vfio_put_address_space(space
);
1433 bool vfio_eeh_as_ok(AddressSpace
*as
)
1435 VFIOContainer
*container
= vfio_eeh_as_container(as
);
1437 return (container
!= NULL
) && vfio_eeh_container_ok(container
);
1440 int vfio_eeh_as_op(AddressSpace
*as
, uint32_t op
)
1442 VFIOContainer
*container
= vfio_eeh_as_container(as
);
1447 return vfio_eeh_container_op(container
, op
);