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 buf
.qword
= cpu_to_le64(data
);
126 hw_error("vfio: unsupported write size, %d bytes", size
);
130 if (pwrite(vbasedev
->fd
, &buf
, size
, region
->fd_offset
+ addr
) != size
) {
131 error_report("%s(%s:region%d+0x%"HWADDR_PRIx
", 0x%"PRIx64
133 __func__
, vbasedev
->name
, region
->nr
,
137 trace_vfio_region_write(vbasedev
->name
, region
->nr
, addr
, data
, size
);
140 * A read or write to a BAR always signals an INTx EOI. This will
141 * do nothing if not pending (including not in INTx mode). We assume
142 * that a BAR access is in response to an interrupt and that BAR
143 * accesses will service the interrupt. Unfortunately, we don't know
144 * which access will service the interrupt, so we're potentially
145 * getting quite a few host interrupts per guest interrupt.
147 vbasedev
->ops
->vfio_eoi(vbasedev
);
150 uint64_t vfio_region_read(void *opaque
,
151 hwaddr addr
, unsigned size
)
153 VFIORegion
*region
= opaque
;
154 VFIODevice
*vbasedev
= region
->vbasedev
;
163 if (pread(vbasedev
->fd
, &buf
, size
, region
->fd_offset
+ addr
) != size
) {
164 error_report("%s(%s:region%d+0x%"HWADDR_PRIx
", %d) failed: %m",
165 __func__
, vbasedev
->name
, region
->nr
,
174 data
= le16_to_cpu(buf
.word
);
177 data
= le32_to_cpu(buf
.dword
);
180 data
= le64_to_cpu(buf
.qword
);
183 hw_error("vfio: unsupported read size, %d bytes", size
);
187 trace_vfio_region_read(vbasedev
->name
, region
->nr
, addr
, size
, data
);
189 /* Same as write above */
190 vbasedev
->ops
->vfio_eoi(vbasedev
);
195 const MemoryRegionOps vfio_region_ops
= {
196 .read
= vfio_region_read
,
197 .write
= vfio_region_write
,
198 .endianness
= DEVICE_LITTLE_ENDIAN
,
200 .min_access_size
= 1,
201 .max_access_size
= 8,
204 .min_access_size
= 1,
205 .max_access_size
= 8,
210 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
212 static int vfio_dma_unmap(VFIOContainer
*container
,
213 hwaddr iova
, ram_addr_t size
)
215 struct vfio_iommu_type1_dma_unmap unmap
= {
216 .argsz
= sizeof(unmap
),
222 if (ioctl(container
->fd
, VFIO_IOMMU_UNMAP_DMA
, &unmap
)) {
223 error_report("VFIO_UNMAP_DMA: %d", -errno
);
230 static int vfio_dma_map(VFIOContainer
*container
, hwaddr iova
,
231 ram_addr_t size
, void *vaddr
, bool readonly
)
233 struct vfio_iommu_type1_dma_map map
= {
234 .argsz
= sizeof(map
),
235 .flags
= VFIO_DMA_MAP_FLAG_READ
,
236 .vaddr
= (__u64
)(uintptr_t)vaddr
,
242 map
.flags
|= VFIO_DMA_MAP_FLAG_WRITE
;
246 * Try the mapping, if it fails with EBUSY, unmap the region and try
247 * again. This shouldn't be necessary, but we sometimes see it in
250 if (ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0 ||
251 (errno
== EBUSY
&& vfio_dma_unmap(container
, iova
, size
) == 0 &&
252 ioctl(container
->fd
, VFIO_IOMMU_MAP_DMA
, &map
) == 0)) {
256 error_report("VFIO_MAP_DMA: %d", -errno
);
260 static void vfio_host_win_add(VFIOContainer
*container
,
261 hwaddr min_iova
, hwaddr max_iova
,
262 uint64_t iova_pgsizes
)
264 VFIOHostDMAWindow
*hostwin
;
266 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
267 if (ranges_overlap(hostwin
->min_iova
,
268 hostwin
->max_iova
- hostwin
->min_iova
+ 1,
270 max_iova
- min_iova
+ 1)) {
271 hw_error("%s: Overlapped IOMMU are not enabled", __func__
);
275 hostwin
= g_malloc0(sizeof(*hostwin
));
277 hostwin
->min_iova
= min_iova
;
278 hostwin
->max_iova
= max_iova
;
279 hostwin
->iova_pgsizes
= iova_pgsizes
;
280 QLIST_INSERT_HEAD(&container
->hostwin_list
, hostwin
, hostwin_next
);
283 static int vfio_host_win_del(VFIOContainer
*container
, hwaddr min_iova
,
286 VFIOHostDMAWindow
*hostwin
;
288 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
289 if (hostwin
->min_iova
== min_iova
&& hostwin
->max_iova
== max_iova
) {
290 QLIST_REMOVE(hostwin
, hostwin_next
);
298 static bool vfio_listener_skipped_section(MemoryRegionSection
*section
)
300 return (!memory_region_is_ram(section
->mr
) &&
301 !memory_region_is_iommu(section
->mr
)) ||
303 * Sizing an enabled 64-bit BAR can cause spurious mappings to
304 * addresses in the upper part of the 64-bit address space. These
305 * are never accessed by the CPU and beyond the address width of
306 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
308 section
->offset_within_address_space
& (1ULL << 63);
311 /* Called with rcu_read_lock held. */
312 static bool vfio_get_vaddr(IOMMUTLBEntry
*iotlb
, void **vaddr
,
317 hwaddr len
= iotlb
->addr_mask
+ 1;
318 bool writable
= iotlb
->perm
& IOMMU_WO
;
321 * The IOMMU TLB entry we have just covers translation through
322 * this IOMMU to its immediate target. We need to translate
323 * it the rest of the way through to memory.
325 mr
= address_space_translate(&address_space_memory
,
326 iotlb
->translated_addr
,
327 &xlat
, &len
, writable
);
328 if (!memory_region_is_ram(mr
)) {
329 error_report("iommu map to non memory area %"HWADDR_PRIx
"",
335 * Translation truncates length to the IOMMU page size,
336 * check that it did not truncate too much.
338 if (len
& iotlb
->addr_mask
) {
339 error_report("iommu has granularity incompatible with target AS");
343 *vaddr
= memory_region_get_ram_ptr(mr
) + xlat
;
344 *read_only
= !writable
|| mr
->readonly
;
349 static void vfio_iommu_map_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
351 VFIOGuestIOMMU
*giommu
= container_of(n
, VFIOGuestIOMMU
, n
);
352 VFIOContainer
*container
= giommu
->container
;
353 hwaddr iova
= iotlb
->iova
+ giommu
->iommu_offset
;
358 trace_vfio_iommu_map_notify(iotlb
->perm
== IOMMU_NONE
? "UNMAP" : "MAP",
359 iova
, iova
+ iotlb
->addr_mask
);
361 if (iotlb
->target_as
!= &address_space_memory
) {
362 error_report("Wrong target AS \"%s\", only system memory is allowed",
363 iotlb
->target_as
->name
? iotlb
->target_as
->name
: "none");
369 if ((iotlb
->perm
& IOMMU_RW
) != IOMMU_NONE
) {
370 if (!vfio_get_vaddr(iotlb
, &vaddr
, &read_only
)) {
374 * vaddr is only valid until rcu_read_unlock(). But after
375 * vfio_dma_map has set up the mapping the pages will be
376 * pinned by the kernel. This makes sure that the RAM backend
377 * of vaddr will always be there, even if the memory object is
378 * destroyed and its backing memory munmap-ed.
380 ret
= vfio_dma_map(container
, iova
,
381 iotlb
->addr_mask
+ 1, vaddr
,
384 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
385 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
387 iotlb
->addr_mask
+ 1, vaddr
, ret
);
390 ret
= vfio_dma_unmap(container
, iova
, iotlb
->addr_mask
+ 1);
392 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
393 "0x%"HWADDR_PRIx
") = %d (%m)",
395 iotlb
->addr_mask
+ 1, ret
);
402 static void vfio_listener_region_add(MemoryListener
*listener
,
403 MemoryRegionSection
*section
)
405 VFIOContainer
*container
= container_of(listener
, VFIOContainer
, listener
);
407 Int128 llend
, llsize
;
410 VFIOHostDMAWindow
*hostwin
;
413 if (vfio_listener_skipped_section(section
)) {
414 trace_vfio_listener_region_add_skip(
415 section
->offset_within_address_space
,
416 section
->offset_within_address_space
+
417 int128_get64(int128_sub(section
->size
, int128_one())));
421 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
422 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
423 error_report("%s received unaligned region", __func__
);
427 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
428 llend
= int128_make64(section
->offset_within_address_space
);
429 llend
= int128_add(llend
, section
->size
);
430 llend
= int128_and(llend
, int128_exts64(TARGET_PAGE_MASK
));
432 if (int128_ge(int128_make64(iova
), llend
)) {
435 end
= int128_get64(int128_sub(llend
, int128_one()));
437 if (container
->iommu_type
== VFIO_SPAPR_TCE_v2_IOMMU
) {
438 VFIOHostDMAWindow
*hostwin
;
441 /* For now intersections are not allowed, we may relax this later */
442 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
443 if (ranges_overlap(hostwin
->min_iova
,
444 hostwin
->max_iova
- hostwin
->min_iova
+ 1,
445 section
->offset_within_address_space
,
446 int128_get64(section
->size
))) {
452 ret
= vfio_spapr_create_window(container
, section
, &pgsize
);
457 vfio_host_win_add(container
, section
->offset_within_address_space
,
458 section
->offset_within_address_space
+
459 int128_get64(section
->size
) - 1, pgsize
);
462 hostwin_found
= false;
463 QLIST_FOREACH(hostwin
, &container
->hostwin_list
, hostwin_next
) {
464 if (hostwin
->min_iova
<= iova
&& end
<= hostwin
->max_iova
) {
465 hostwin_found
= true;
470 if (!hostwin_found
) {
471 error_report("vfio: IOMMU container %p can't map guest IOVA region"
472 " 0x%"HWADDR_PRIx
"..0x%"HWADDR_PRIx
,
473 container
, iova
, end
);
478 memory_region_ref(section
->mr
);
480 if (memory_region_is_iommu(section
->mr
)) {
481 VFIOGuestIOMMU
*giommu
;
482 IOMMUMemoryRegion
*iommu_mr
= IOMMU_MEMORY_REGION(section
->mr
);
484 trace_vfio_listener_region_add_iommu(iova
, end
);
486 * FIXME: For VFIO iommu types which have KVM acceleration to
487 * avoid bouncing all map/unmaps through qemu this way, this
488 * would be the right place to wire that up (tell the KVM
489 * device emulation the VFIO iommu handles to use).
491 giommu
= g_malloc0(sizeof(*giommu
));
492 giommu
->iommu
= iommu_mr
;
493 giommu
->iommu_offset
= section
->offset_within_address_space
-
494 section
->offset_within_region
;
495 giommu
->container
= container
;
496 llend
= int128_add(int128_make64(section
->offset_within_region
),
498 llend
= int128_sub(llend
, int128_one());
499 iommu_notifier_init(&giommu
->n
, vfio_iommu_map_notify
,
501 section
->offset_within_region
,
502 int128_get64(llend
));
503 QLIST_INSERT_HEAD(&container
->giommu_list
, giommu
, giommu_next
);
505 memory_region_register_iommu_notifier(section
->mr
, &giommu
->n
);
506 memory_region_iommu_replay(giommu
->iommu
, &giommu
->n
);
511 /* Here we assume that memory_region_is_ram(section->mr)==true */
513 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
514 section
->offset_within_region
+
515 (iova
- section
->offset_within_address_space
);
517 trace_vfio_listener_region_add_ram(iova
, end
, vaddr
);
519 llsize
= int128_sub(llend
, int128_make64(iova
));
521 ret
= vfio_dma_map(container
, iova
, int128_get64(llsize
),
522 vaddr
, section
->readonly
);
524 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx
", "
525 "0x%"HWADDR_PRIx
", %p) = %d (%m)",
526 container
, iova
, int128_get64(llsize
), vaddr
, ret
);
534 * On the initfn path, store the first error in the container so we
535 * can gracefully fail. Runtime, there's not much we can do other
536 * than throw a hardware error.
538 if (!container
->initialized
) {
539 if (!container
->error
) {
540 container
->error
= ret
;
543 hw_error("vfio: DMA mapping failed, unable to continue");
547 static void vfio_listener_region_del(MemoryListener
*listener
,
548 MemoryRegionSection
*section
)
550 VFIOContainer
*container
= container_of(listener
, VFIOContainer
, listener
);
552 Int128 llend
, llsize
;
555 if (vfio_listener_skipped_section(section
)) {
556 trace_vfio_listener_region_del_skip(
557 section
->offset_within_address_space
,
558 section
->offset_within_address_space
+
559 int128_get64(int128_sub(section
->size
, int128_one())));
563 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
564 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
565 error_report("%s received unaligned region", __func__
);
569 if (memory_region_is_iommu(section
->mr
)) {
570 VFIOGuestIOMMU
*giommu
;
572 QLIST_FOREACH(giommu
, &container
->giommu_list
, giommu_next
) {
573 if (MEMORY_REGION(giommu
->iommu
) == section
->mr
&&
574 giommu
->n
.start
== section
->offset_within_region
) {
575 memory_region_unregister_iommu_notifier(section
->mr
,
577 QLIST_REMOVE(giommu
, giommu_next
);
584 * FIXME: We assume the one big unmap below is adequate to
585 * remove any individual page mappings in the IOMMU which
586 * might have been copied into VFIO. This works for a page table
587 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
588 * That may not be true for all IOMMU types.
592 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
593 llend
= int128_make64(section
->offset_within_address_space
);
594 llend
= int128_add(llend
, section
->size
);
595 llend
= int128_and(llend
, int128_exts64(TARGET_PAGE_MASK
));
597 if (int128_ge(int128_make64(iova
), llend
)) {
600 end
= int128_get64(int128_sub(llend
, int128_one()));
602 llsize
= int128_sub(llend
, int128_make64(iova
));
604 trace_vfio_listener_region_del(iova
, end
);
606 ret
= vfio_dma_unmap(container
, iova
, int128_get64(llsize
));
607 memory_region_unref(section
->mr
);
609 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx
", "
610 "0x%"HWADDR_PRIx
") = %d (%m)",
611 container
, iova
, int128_get64(llsize
), ret
);
614 if (container
->iommu_type
== VFIO_SPAPR_TCE_v2_IOMMU
) {
615 vfio_spapr_remove_window(container
,
616 section
->offset_within_address_space
);
617 if (vfio_host_win_del(container
,
618 section
->offset_within_address_space
,
619 section
->offset_within_address_space
+
620 int128_get64(section
->size
) - 1) < 0) {
621 hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx
,
622 __func__
, section
->offset_within_address_space
);
627 static const MemoryListener vfio_memory_listener
= {
628 .region_add
= vfio_listener_region_add
,
629 .region_del
= vfio_listener_region_del
,
632 static void vfio_listener_release(VFIOContainer
*container
)
634 memory_listener_unregister(&container
->listener
);
635 if (container
->iommu_type
== VFIO_SPAPR_TCE_v2_IOMMU
) {
636 memory_listener_unregister(&container
->prereg_listener
);
640 static struct vfio_info_cap_header
*
641 vfio_get_region_info_cap(struct vfio_region_info
*info
, uint16_t id
)
643 struct vfio_info_cap_header
*hdr
;
646 if (!(info
->flags
& VFIO_REGION_INFO_FLAG_CAPS
)) {
650 for (hdr
= ptr
+ info
->cap_offset
; hdr
!= ptr
; hdr
= ptr
+ hdr
->next
) {
659 static int vfio_setup_region_sparse_mmaps(VFIORegion
*region
,
660 struct vfio_region_info
*info
)
662 struct vfio_info_cap_header
*hdr
;
663 struct vfio_region_info_cap_sparse_mmap
*sparse
;
666 hdr
= vfio_get_region_info_cap(info
, VFIO_REGION_INFO_CAP_SPARSE_MMAP
);
671 sparse
= container_of(hdr
, struct vfio_region_info_cap_sparse_mmap
, header
);
673 trace_vfio_region_sparse_mmap_header(region
->vbasedev
->name
,
674 region
->nr
, sparse
->nr_areas
);
676 region
->mmaps
= g_new0(VFIOMmap
, sparse
->nr_areas
);
678 for (i
= 0, j
= 0; i
< sparse
->nr_areas
; i
++) {
679 trace_vfio_region_sparse_mmap_entry(i
, sparse
->areas
[i
].offset
,
680 sparse
->areas
[i
].offset
+
681 sparse
->areas
[i
].size
);
683 if (sparse
->areas
[i
].size
) {
684 region
->mmaps
[j
].offset
= sparse
->areas
[i
].offset
;
685 region
->mmaps
[j
].size
= sparse
->areas
[i
].size
;
690 region
->nr_mmaps
= j
;
691 region
->mmaps
= g_realloc(region
->mmaps
, j
* sizeof(VFIOMmap
));
696 int vfio_region_setup(Object
*obj
, VFIODevice
*vbasedev
, VFIORegion
*region
,
697 int index
, const char *name
)
699 struct vfio_region_info
*info
;
702 ret
= vfio_get_region_info(vbasedev
, index
, &info
);
707 region
->vbasedev
= vbasedev
;
708 region
->flags
= info
->flags
;
709 region
->size
= info
->size
;
710 region
->fd_offset
= info
->offset
;
714 region
->mem
= g_new0(MemoryRegion
, 1);
715 memory_region_init_io(region
->mem
, obj
, &vfio_region_ops
,
716 region
, name
, region
->size
);
718 if (!vbasedev
->no_mmap
&&
719 region
->flags
& VFIO_REGION_INFO_FLAG_MMAP
) {
721 ret
= vfio_setup_region_sparse_mmaps(region
, info
);
724 region
->nr_mmaps
= 1;
725 region
->mmaps
= g_new0(VFIOMmap
, region
->nr_mmaps
);
726 region
->mmaps
[0].offset
= 0;
727 region
->mmaps
[0].size
= region
->size
;
734 trace_vfio_region_setup(vbasedev
->name
, index
, name
,
735 region
->flags
, region
->fd_offset
, region
->size
);
739 int vfio_region_mmap(VFIORegion
*region
)
748 prot
|= region
->flags
& VFIO_REGION_INFO_FLAG_READ
? PROT_READ
: 0;
749 prot
|= region
->flags
& VFIO_REGION_INFO_FLAG_WRITE
? PROT_WRITE
: 0;
751 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
752 region
->mmaps
[i
].mmap
= mmap(NULL
, region
->mmaps
[i
].size
, prot
,
753 MAP_SHARED
, region
->vbasedev
->fd
,
755 region
->mmaps
[i
].offset
);
756 if (region
->mmaps
[i
].mmap
== MAP_FAILED
) {
759 trace_vfio_region_mmap_fault(memory_region_name(region
->mem
), i
,
761 region
->mmaps
[i
].offset
,
763 region
->mmaps
[i
].offset
+
764 region
->mmaps
[i
].size
- 1, ret
);
766 region
->mmaps
[i
].mmap
= NULL
;
768 for (i
--; i
>= 0; i
--) {
769 memory_region_del_subregion(region
->mem
, ®ion
->mmaps
[i
].mem
);
770 munmap(region
->mmaps
[i
].mmap
, region
->mmaps
[i
].size
);
771 object_unparent(OBJECT(®ion
->mmaps
[i
].mem
));
772 region
->mmaps
[i
].mmap
= NULL
;
778 name
= g_strdup_printf("%s mmaps[%d]",
779 memory_region_name(region
->mem
), i
);
780 memory_region_init_ram_device_ptr(®ion
->mmaps
[i
].mem
,
781 memory_region_owner(region
->mem
),
782 name
, region
->mmaps
[i
].size
,
783 region
->mmaps
[i
].mmap
);
785 memory_region_add_subregion(region
->mem
, region
->mmaps
[i
].offset
,
786 ®ion
->mmaps
[i
].mem
);
788 trace_vfio_region_mmap(memory_region_name(®ion
->mmaps
[i
].mem
),
789 region
->mmaps
[i
].offset
,
790 region
->mmaps
[i
].offset
+
791 region
->mmaps
[i
].size
- 1);
797 void vfio_region_exit(VFIORegion
*region
)
805 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
806 if (region
->mmaps
[i
].mmap
) {
807 memory_region_del_subregion(region
->mem
, ®ion
->mmaps
[i
].mem
);
811 trace_vfio_region_exit(region
->vbasedev
->name
, region
->nr
);
814 void vfio_region_finalize(VFIORegion
*region
)
822 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
823 if (region
->mmaps
[i
].mmap
) {
824 munmap(region
->mmaps
[i
].mmap
, region
->mmaps
[i
].size
);
825 object_unparent(OBJECT(®ion
->mmaps
[i
].mem
));
829 object_unparent(OBJECT(region
->mem
));
832 g_free(region
->mmaps
);
834 trace_vfio_region_finalize(region
->vbasedev
->name
, region
->nr
);
837 void vfio_region_mmaps_set_enabled(VFIORegion
*region
, bool enabled
)
845 for (i
= 0; i
< region
->nr_mmaps
; i
++) {
846 if (region
->mmaps
[i
].mmap
) {
847 memory_region_set_enabled(®ion
->mmaps
[i
].mem
, enabled
);
851 trace_vfio_region_mmaps_set_enabled(memory_region_name(region
->mem
),
855 void vfio_reset_handler(void *opaque
)
858 VFIODevice
*vbasedev
;
860 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
861 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
862 if (vbasedev
->dev
->realized
) {
863 vbasedev
->ops
->vfio_compute_needs_reset(vbasedev
);
868 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
869 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
870 if (vbasedev
->dev
->realized
&& vbasedev
->needs_reset
) {
871 vbasedev
->ops
->vfio_hot_reset_multi(vbasedev
);
877 static void vfio_kvm_device_add_group(VFIOGroup
*group
)
880 struct kvm_device_attr attr
= {
881 .group
= KVM_DEV_VFIO_GROUP
,
882 .attr
= KVM_DEV_VFIO_GROUP_ADD
,
883 .addr
= (uint64_t)(unsigned long)&group
->fd
,
886 if (!kvm_enabled()) {
890 if (vfio_kvm_device_fd
< 0) {
891 struct kvm_create_device cd
= {
892 .type
= KVM_DEV_TYPE_VFIO
,
895 if (kvm_vm_ioctl(kvm_state
, KVM_CREATE_DEVICE
, &cd
)) {
896 error_report("Failed to create KVM VFIO device: %m");
900 vfio_kvm_device_fd
= cd
.fd
;
903 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
904 error_report("Failed to add group %d to KVM VFIO device: %m",
910 static void vfio_kvm_device_del_group(VFIOGroup
*group
)
913 struct kvm_device_attr attr
= {
914 .group
= KVM_DEV_VFIO_GROUP
,
915 .attr
= KVM_DEV_VFIO_GROUP_DEL
,
916 .addr
= (uint64_t)(unsigned long)&group
->fd
,
919 if (vfio_kvm_device_fd
< 0) {
923 if (ioctl(vfio_kvm_device_fd
, KVM_SET_DEVICE_ATTR
, &attr
)) {
924 error_report("Failed to remove group %d from KVM VFIO device: %m",
930 static VFIOAddressSpace
*vfio_get_address_space(AddressSpace
*as
)
932 VFIOAddressSpace
*space
;
934 QLIST_FOREACH(space
, &vfio_address_spaces
, list
) {
935 if (space
->as
== as
) {
940 /* No suitable VFIOAddressSpace, create a new one */
941 space
= g_malloc0(sizeof(*space
));
943 QLIST_INIT(&space
->containers
);
945 QLIST_INSERT_HEAD(&vfio_address_spaces
, space
, list
);
950 static void vfio_put_address_space(VFIOAddressSpace
*space
)
952 if (QLIST_EMPTY(&space
->containers
)) {
953 QLIST_REMOVE(space
, list
);
958 static int vfio_connect_container(VFIOGroup
*group
, AddressSpace
*as
,
961 VFIOContainer
*container
;
963 VFIOAddressSpace
*space
;
965 space
= vfio_get_address_space(as
);
967 QLIST_FOREACH(container
, &space
->containers
, next
) {
968 if (!ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &container
->fd
)) {
969 group
->container
= container
;
970 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
971 vfio_kvm_device_add_group(group
);
976 fd
= qemu_open("/dev/vfio/vfio", O_RDWR
);
978 error_setg_errno(errp
, errno
, "failed to open /dev/vfio/vfio");
983 ret
= ioctl(fd
, VFIO_GET_API_VERSION
);
984 if (ret
!= VFIO_API_VERSION
) {
985 error_setg(errp
, "supported vfio version: %d, "
986 "reported version: %d", VFIO_API_VERSION
, ret
);
991 container
= g_malloc0(sizeof(*container
));
992 container
->space
= space
;
994 QLIST_INIT(&container
->giommu_list
);
995 QLIST_INIT(&container
->hostwin_list
);
996 if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1_IOMMU
) ||
997 ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1v2_IOMMU
)) {
998 bool v2
= !!ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_TYPE1v2_IOMMU
);
999 struct vfio_iommu_type1_info info
;
1001 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
1003 error_setg_errno(errp
, errno
, "failed to set group container");
1005 goto free_container_exit
;
1008 container
->iommu_type
= v2
? VFIO_TYPE1v2_IOMMU
: VFIO_TYPE1_IOMMU
;
1009 ret
= ioctl(fd
, VFIO_SET_IOMMU
, container
->iommu_type
);
1011 error_setg_errno(errp
, errno
, "failed to set iommu for container");
1013 goto free_container_exit
;
1017 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
1018 * IOVA whatsoever. That's not actually true, but the current
1019 * kernel interface doesn't tell us what it can map, and the
1020 * existing Type1 IOMMUs generally support any IOVA we're
1021 * going to actually try in practice.
1023 info
.argsz
= sizeof(info
);
1024 ret
= ioctl(fd
, VFIO_IOMMU_GET_INFO
, &info
);
1026 if (ret
|| !(info
.flags
& VFIO_IOMMU_INFO_PGSIZES
)) {
1027 /* Assume 4k IOVA page size */
1028 info
.iova_pgsizes
= 4096;
1030 vfio_host_win_add(container
, 0, (hwaddr
)-1, info
.iova_pgsizes
);
1031 } else if (ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_IOMMU
) ||
1032 ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_v2_IOMMU
)) {
1033 struct vfio_iommu_spapr_tce_info info
;
1034 bool v2
= !!ioctl(fd
, VFIO_CHECK_EXTENSION
, VFIO_SPAPR_TCE_v2_IOMMU
);
1036 ret
= ioctl(group
->fd
, VFIO_GROUP_SET_CONTAINER
, &fd
);
1038 error_setg_errno(errp
, errno
, "failed to set group container");
1040 goto free_container_exit
;
1042 container
->iommu_type
=
1043 v2
? VFIO_SPAPR_TCE_v2_IOMMU
: VFIO_SPAPR_TCE_IOMMU
;
1044 ret
= ioctl(fd
, VFIO_SET_IOMMU
, container
->iommu_type
);
1046 container
->iommu_type
= VFIO_SPAPR_TCE_IOMMU
;
1048 ret
= ioctl(fd
, VFIO_SET_IOMMU
, container
->iommu_type
);
1051 error_setg_errno(errp
, errno
, "failed to set iommu for container");
1053 goto free_container_exit
;
1057 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
1058 * when container fd is closed so we do not call it explicitly
1062 ret
= ioctl(fd
, VFIO_IOMMU_ENABLE
);
1064 error_setg_errno(errp
, errno
, "failed to enable container");
1066 goto free_container_exit
;
1069 container
->prereg_listener
= vfio_prereg_listener
;
1071 memory_listener_register(&container
->prereg_listener
,
1072 &address_space_memory
);
1073 if (container
->error
) {
1074 memory_listener_unregister(&container
->prereg_listener
);
1075 ret
= container
->error
;
1077 "RAM memory listener initialization failed for container");
1078 goto free_container_exit
;
1082 info
.argsz
= sizeof(info
);
1083 ret
= ioctl(fd
, VFIO_IOMMU_SPAPR_TCE_GET_INFO
, &info
);
1085 error_setg_errno(errp
, errno
,
1086 "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed");
1089 memory_listener_unregister(&container
->prereg_listener
);
1091 goto free_container_exit
;
1096 * There is a default window in just created container.
1097 * To make region_add/del simpler, we better remove this
1098 * window now and let those iommu_listener callbacks
1099 * create/remove them when needed.
1101 ret
= vfio_spapr_remove_window(container
, info
.dma32_window_start
);
1103 error_setg_errno(errp
, -ret
,
1104 "failed to remove existing window");
1105 goto free_container_exit
;
1108 /* The default table uses 4K pages */
1109 vfio_host_win_add(container
, info
.dma32_window_start
,
1110 info
.dma32_window_start
+
1111 info
.dma32_window_size
- 1,
1115 error_setg(errp
, "No available IOMMU models");
1117 goto free_container_exit
;
1120 vfio_kvm_device_add_group(group
);
1122 QLIST_INIT(&container
->group_list
);
1123 QLIST_INSERT_HEAD(&space
->containers
, container
, next
);
1125 group
->container
= container
;
1126 QLIST_INSERT_HEAD(&container
->group_list
, group
, container_next
);
1128 container
->listener
= vfio_memory_listener
;
1130 memory_listener_register(&container
->listener
, container
->space
->as
);
1132 if (container
->error
) {
1133 ret
= container
->error
;
1134 error_setg_errno(errp
, -ret
,
1135 "memory listener initialization failed for container");
1136 goto listener_release_exit
;
1139 container
->initialized
= true;
1142 listener_release_exit
:
1143 QLIST_REMOVE(group
, container_next
);
1144 QLIST_REMOVE(container
, next
);
1145 vfio_kvm_device_del_group(group
);
1146 vfio_listener_release(container
);
1148 free_container_exit
:
1155 vfio_put_address_space(space
);
1160 static void vfio_disconnect_container(VFIOGroup
*group
)
1162 VFIOContainer
*container
= group
->container
;
1164 QLIST_REMOVE(group
, container_next
);
1165 group
->container
= NULL
;
1168 * Explicitly release the listener first before unset container,
1169 * since unset may destroy the backend container if it's the last
1172 if (QLIST_EMPTY(&container
->group_list
)) {
1173 vfio_listener_release(container
);
1176 if (ioctl(group
->fd
, VFIO_GROUP_UNSET_CONTAINER
, &container
->fd
)) {
1177 error_report("vfio: error disconnecting group %d from container",
1181 if (QLIST_EMPTY(&container
->group_list
)) {
1182 VFIOAddressSpace
*space
= container
->space
;
1183 VFIOGuestIOMMU
*giommu
, *tmp
;
1185 QLIST_REMOVE(container
, next
);
1187 QLIST_FOREACH_SAFE(giommu
, &container
->giommu_list
, giommu_next
, tmp
) {
1188 memory_region_unregister_iommu_notifier(
1189 MEMORY_REGION(giommu
->iommu
), &giommu
->n
);
1190 QLIST_REMOVE(giommu
, giommu_next
);
1194 trace_vfio_disconnect_container(container
->fd
);
1195 close(container
->fd
);
1198 vfio_put_address_space(space
);
1202 VFIOGroup
*vfio_get_group(int groupid
, AddressSpace
*as
, Error
**errp
)
1206 struct vfio_group_status status
= { .argsz
= sizeof(status
) };
1208 QLIST_FOREACH(group
, &vfio_group_list
, next
) {
1209 if (group
->groupid
== groupid
) {
1210 /* Found it. Now is it already in the right context? */
1211 if (group
->container
->space
->as
== as
) {
1214 error_setg(errp
, "group %d used in multiple address spaces",
1221 group
= g_malloc0(sizeof(*group
));
1223 snprintf(path
, sizeof(path
), "/dev/vfio/%d", groupid
);
1224 group
->fd
= qemu_open(path
, O_RDWR
);
1225 if (group
->fd
< 0) {
1226 error_setg_errno(errp
, errno
, "failed to open %s", path
);
1227 goto free_group_exit
;
1230 if (ioctl(group
->fd
, VFIO_GROUP_GET_STATUS
, &status
)) {
1231 error_setg_errno(errp
, errno
, "failed to get group %d status", groupid
);
1235 if (!(status
.flags
& VFIO_GROUP_FLAGS_VIABLE
)) {
1236 error_setg(errp
, "group %d is not viable", groupid
);
1237 error_append_hint(errp
,
1238 "Please ensure all devices within the iommu_group "
1239 "are bound to their vfio bus driver.\n");
1243 group
->groupid
= groupid
;
1244 QLIST_INIT(&group
->device_list
);
1246 if (vfio_connect_container(group
, as
, errp
)) {
1247 error_prepend(errp
, "failed to setup container for group %d: ",
1252 if (QLIST_EMPTY(&vfio_group_list
)) {
1253 qemu_register_reset(vfio_reset_handler
, NULL
);
1256 QLIST_INSERT_HEAD(&vfio_group_list
, group
, next
);
1269 void vfio_put_group(VFIOGroup
*group
)
1271 if (!group
|| !QLIST_EMPTY(&group
->device_list
)) {
1275 vfio_kvm_device_del_group(group
);
1276 vfio_disconnect_container(group
);
1277 QLIST_REMOVE(group
, next
);
1278 trace_vfio_put_group(group
->fd
);
1282 if (QLIST_EMPTY(&vfio_group_list
)) {
1283 qemu_unregister_reset(vfio_reset_handler
, NULL
);
1287 int vfio_get_device(VFIOGroup
*group
, const char *name
,
1288 VFIODevice
*vbasedev
, Error
**errp
)
1290 struct vfio_device_info dev_info
= { .argsz
= sizeof(dev_info
) };
1293 fd
= ioctl(group
->fd
, VFIO_GROUP_GET_DEVICE_FD
, name
);
1295 error_setg_errno(errp
, errno
, "error getting device from group %d",
1297 error_append_hint(errp
,
1298 "Verify all devices in group %d are bound to vfio-<bus> "
1299 "or pci-stub and not already in use\n", group
->groupid
);
1303 ret
= ioctl(fd
, VFIO_DEVICE_GET_INFO
, &dev_info
);
1305 error_setg_errno(errp
, errno
, "error getting device info");
1311 vbasedev
->group
= group
;
1312 QLIST_INSERT_HEAD(&group
->device_list
, vbasedev
, next
);
1314 vbasedev
->num_irqs
= dev_info
.num_irqs
;
1315 vbasedev
->num_regions
= dev_info
.num_regions
;
1316 vbasedev
->flags
= dev_info
.flags
;
1318 trace_vfio_get_device(name
, dev_info
.flags
, dev_info
.num_regions
,
1321 vbasedev
->reset_works
= !!(dev_info
.flags
& VFIO_DEVICE_FLAGS_RESET
);
1325 void vfio_put_base_device(VFIODevice
*vbasedev
)
1327 if (!vbasedev
->group
) {
1330 QLIST_REMOVE(vbasedev
, next
);
1331 vbasedev
->group
= NULL
;
1332 trace_vfio_put_base_device(vbasedev
->fd
);
1333 close(vbasedev
->fd
);
1336 int vfio_get_region_info(VFIODevice
*vbasedev
, int index
,
1337 struct vfio_region_info
**info
)
1339 size_t argsz
= sizeof(struct vfio_region_info
);
1341 *info
= g_malloc0(argsz
);
1343 (*info
)->index
= index
;
1345 (*info
)->argsz
= argsz
;
1347 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_GET_REGION_INFO
, *info
)) {
1353 if ((*info
)->argsz
> argsz
) {
1354 argsz
= (*info
)->argsz
;
1355 *info
= g_realloc(*info
, argsz
);
1363 int vfio_get_dev_region_info(VFIODevice
*vbasedev
, uint32_t type
,
1364 uint32_t subtype
, struct vfio_region_info
**info
)
1368 for (i
= 0; i
< vbasedev
->num_regions
; i
++) {
1369 struct vfio_info_cap_header
*hdr
;
1370 struct vfio_region_info_cap_type
*cap_type
;
1372 if (vfio_get_region_info(vbasedev
, i
, info
)) {
1376 hdr
= vfio_get_region_info_cap(*info
, VFIO_REGION_INFO_CAP_TYPE
);
1382 cap_type
= container_of(hdr
, struct vfio_region_info_cap_type
, header
);
1384 trace_vfio_get_dev_region(vbasedev
->name
, i
,
1385 cap_type
->type
, cap_type
->subtype
);
1387 if (cap_type
->type
== type
&& cap_type
->subtype
== subtype
) {
1399 * Interfaces for IBM EEH (Enhanced Error Handling)
1401 static bool vfio_eeh_container_ok(VFIOContainer
*container
)
1404 * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO
1405 * implementation is broken if there are multiple groups in a
1406 * container. The hardware works in units of Partitionable
1407 * Endpoints (== IOMMU groups) and the EEH operations naively
1408 * iterate across all groups in the container, without any logic
1409 * to make sure the groups have their state synchronized. For
1410 * certain operations (ENABLE) that might be ok, until an error
1411 * occurs, but for others (GET_STATE) it's clearly broken.
1415 * XXX Once fixed kernels exist, test for them here
1418 if (QLIST_EMPTY(&container
->group_list
)) {
1422 if (QLIST_NEXT(QLIST_FIRST(&container
->group_list
), container_next
)) {
1429 static int vfio_eeh_container_op(VFIOContainer
*container
, uint32_t op
)
1431 struct vfio_eeh_pe_op pe_op
= {
1432 .argsz
= sizeof(pe_op
),
1437 if (!vfio_eeh_container_ok(container
)) {
1438 error_report("vfio/eeh: EEH_PE_OP 0x%x: "
1439 "kernel requires a container with exactly one group", op
);
1443 ret
= ioctl(container
->fd
, VFIO_EEH_PE_OP
, &pe_op
);
1445 error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op
);
1452 static VFIOContainer
*vfio_eeh_as_container(AddressSpace
*as
)
1454 VFIOAddressSpace
*space
= vfio_get_address_space(as
);
1455 VFIOContainer
*container
= NULL
;
1457 if (QLIST_EMPTY(&space
->containers
)) {
1458 /* No containers to act on */
1462 container
= QLIST_FIRST(&space
->containers
);
1464 if (QLIST_NEXT(container
, next
)) {
1465 /* We don't yet have logic to synchronize EEH state across
1466 * multiple containers */
1472 vfio_put_address_space(space
);
1476 bool vfio_eeh_as_ok(AddressSpace
*as
)
1478 VFIOContainer
*container
= vfio_eeh_as_container(as
);
1480 return (container
!= NULL
) && vfio_eeh_container_ok(container
);
1483 int vfio_eeh_as_op(AddressSpace
*as
, uint32_t op
)
1485 VFIOContainer
*container
= vfio_eeh_as_container(as
);
1490 return vfio_eeh_container_op(container
, op
);