vfio: Inhibit ballooning based on group attachment to a container
[qemu/ar7.git] / hw / vfio / common.c
bloba3d758af8d8fe80e0b4e5964f1d3dd32bccd964d
1 /*
2 * generic functions used by VFIO devices
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
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>
23 #ifdef CONFIG_KVM
24 #include <linux/kvm.h>
25 #endif
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"
32 #include "hw/hw.h"
33 #include "qemu/error-report.h"
34 #include "qemu/range.h"
35 #include "sysemu/balloon.h"
36 #include "sysemu/kvm.h"
37 #include "trace.h"
38 #include "qapi/error.h"
40 struct vfio_group_head vfio_group_list =
41 QLIST_HEAD_INITIALIZER(vfio_group_list);
42 struct vfio_as_head vfio_address_spaces =
43 QLIST_HEAD_INITIALIZER(vfio_address_spaces);
45 #ifdef CONFIG_KVM
47 * We have a single VFIO pseudo device per KVM VM. Once created it lives
48 * for the life of the VM. Closing the file descriptor only drops our
49 * reference to it and the device's reference to kvm. Therefore once
50 * initialized, this file descriptor is only released on QEMU exit and
51 * we'll re-use it should another vfio device be attached before then.
53 static int vfio_kvm_device_fd = -1;
54 #endif
57 * Common VFIO interrupt disable
59 void vfio_disable_irqindex(VFIODevice *vbasedev, int index)
61 struct vfio_irq_set irq_set = {
62 .argsz = sizeof(irq_set),
63 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
64 .index = index,
65 .start = 0,
66 .count = 0,
69 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
72 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index)
74 struct vfio_irq_set irq_set = {
75 .argsz = sizeof(irq_set),
76 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
77 .index = index,
78 .start = 0,
79 .count = 1,
82 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
85 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
87 struct vfio_irq_set irq_set = {
88 .argsz = sizeof(irq_set),
89 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
90 .index = index,
91 .start = 0,
92 .count = 1,
95 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
99 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
101 void vfio_region_write(void *opaque, hwaddr addr,
102 uint64_t data, unsigned size)
104 VFIORegion *region = opaque;
105 VFIODevice *vbasedev = region->vbasedev;
106 union {
107 uint8_t byte;
108 uint16_t word;
109 uint32_t dword;
110 uint64_t qword;
111 } buf;
113 switch (size) {
114 case 1:
115 buf.byte = data;
116 break;
117 case 2:
118 buf.word = cpu_to_le16(data);
119 break;
120 case 4:
121 buf.dword = cpu_to_le32(data);
122 break;
123 case 8:
124 buf.qword = cpu_to_le64(data);
125 break;
126 default:
127 hw_error("vfio: unsupported write size, %d bytes", size);
128 break;
131 if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
132 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
133 ",%d) failed: %m",
134 __func__, vbasedev->name, region->nr,
135 addr, data, size);
138 trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
141 * A read or write to a BAR always signals an INTx EOI. This will
142 * do nothing if not pending (including not in INTx mode). We assume
143 * that a BAR access is in response to an interrupt and that BAR
144 * accesses will service the interrupt. Unfortunately, we don't know
145 * which access will service the interrupt, so we're potentially
146 * getting quite a few host interrupts per guest interrupt.
148 vbasedev->ops->vfio_eoi(vbasedev);
151 uint64_t vfio_region_read(void *opaque,
152 hwaddr addr, unsigned size)
154 VFIORegion *region = opaque;
155 VFIODevice *vbasedev = region->vbasedev;
156 union {
157 uint8_t byte;
158 uint16_t word;
159 uint32_t dword;
160 uint64_t qword;
161 } buf;
162 uint64_t data = 0;
164 if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
165 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
166 __func__, vbasedev->name, region->nr,
167 addr, size);
168 return (uint64_t)-1;
170 switch (size) {
171 case 1:
172 data = buf.byte;
173 break;
174 case 2:
175 data = le16_to_cpu(buf.word);
176 break;
177 case 4:
178 data = le32_to_cpu(buf.dword);
179 break;
180 case 8:
181 data = le64_to_cpu(buf.qword);
182 break;
183 default:
184 hw_error("vfio: unsupported read size, %d bytes", size);
185 break;
188 trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data);
190 /* Same as write above */
191 vbasedev->ops->vfio_eoi(vbasedev);
193 return data;
196 const MemoryRegionOps vfio_region_ops = {
197 .read = vfio_region_read,
198 .write = vfio_region_write,
199 .endianness = DEVICE_LITTLE_ENDIAN,
200 .valid = {
201 .min_access_size = 1,
202 .max_access_size = 8,
204 .impl = {
205 .min_access_size = 1,
206 .max_access_size = 8,
211 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
213 static int vfio_dma_unmap(VFIOContainer *container,
214 hwaddr iova, ram_addr_t size)
216 struct vfio_iommu_type1_dma_unmap unmap = {
217 .argsz = sizeof(unmap),
218 .flags = 0,
219 .iova = iova,
220 .size = size,
223 if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
224 error_report("VFIO_UNMAP_DMA: %d", -errno);
225 return -errno;
228 return 0;
231 static int vfio_dma_map(VFIOContainer *container, hwaddr iova,
232 ram_addr_t size, void *vaddr, bool readonly)
234 struct vfio_iommu_type1_dma_map map = {
235 .argsz = sizeof(map),
236 .flags = VFIO_DMA_MAP_FLAG_READ,
237 .vaddr = (__u64)(uintptr_t)vaddr,
238 .iova = iova,
239 .size = size,
242 if (!readonly) {
243 map.flags |= VFIO_DMA_MAP_FLAG_WRITE;
247 * Try the mapping, if it fails with EBUSY, unmap the region and try
248 * again. This shouldn't be necessary, but we sometimes see it in
249 * the VGA ROM space.
251 if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 ||
252 (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 &&
253 ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) {
254 return 0;
257 error_report("VFIO_MAP_DMA: %d", -errno);
258 return -errno;
261 static void vfio_host_win_add(VFIOContainer *container,
262 hwaddr min_iova, hwaddr max_iova,
263 uint64_t iova_pgsizes)
265 VFIOHostDMAWindow *hostwin;
267 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
268 if (ranges_overlap(hostwin->min_iova,
269 hostwin->max_iova - hostwin->min_iova + 1,
270 min_iova,
271 max_iova - min_iova + 1)) {
272 hw_error("%s: Overlapped IOMMU are not enabled", __func__);
276 hostwin = g_malloc0(sizeof(*hostwin));
278 hostwin->min_iova = min_iova;
279 hostwin->max_iova = max_iova;
280 hostwin->iova_pgsizes = iova_pgsizes;
281 QLIST_INSERT_HEAD(&container->hostwin_list, hostwin, hostwin_next);
284 static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova,
285 hwaddr max_iova)
287 VFIOHostDMAWindow *hostwin;
289 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
290 if (hostwin->min_iova == min_iova && hostwin->max_iova == max_iova) {
291 QLIST_REMOVE(hostwin, hostwin_next);
292 return 0;
296 return -1;
299 static bool vfio_listener_skipped_section(MemoryRegionSection *section)
301 return (!memory_region_is_ram(section->mr) &&
302 !memory_region_is_iommu(section->mr)) ||
304 * Sizing an enabled 64-bit BAR can cause spurious mappings to
305 * addresses in the upper part of the 64-bit address space. These
306 * are never accessed by the CPU and beyond the address width of
307 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
309 section->offset_within_address_space & (1ULL << 63);
312 /* Called with rcu_read_lock held. */
313 static bool vfio_get_vaddr(IOMMUTLBEntry *iotlb, void **vaddr,
314 bool *read_only)
316 MemoryRegion *mr;
317 hwaddr xlat;
318 hwaddr len = iotlb->addr_mask + 1;
319 bool writable = iotlb->perm & IOMMU_WO;
322 * The IOMMU TLB entry we have just covers translation through
323 * this IOMMU to its immediate target. We need to translate
324 * it the rest of the way through to memory.
326 mr = address_space_translate(&address_space_memory,
327 iotlb->translated_addr,
328 &xlat, &len, writable,
329 MEMTXATTRS_UNSPECIFIED);
330 if (!memory_region_is_ram(mr)) {
331 error_report("iommu map to non memory area %"HWADDR_PRIx"",
332 xlat);
333 return false;
337 * Translation truncates length to the IOMMU page size,
338 * check that it did not truncate too much.
340 if (len & iotlb->addr_mask) {
341 error_report("iommu has granularity incompatible with target AS");
342 return false;
345 *vaddr = memory_region_get_ram_ptr(mr) + xlat;
346 *read_only = !writable || mr->readonly;
348 return true;
351 static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
353 VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
354 VFIOContainer *container = giommu->container;
355 hwaddr iova = iotlb->iova + giommu->iommu_offset;
356 bool read_only;
357 void *vaddr;
358 int ret;
360 trace_vfio_iommu_map_notify(iotlb->perm == IOMMU_NONE ? "UNMAP" : "MAP",
361 iova, iova + iotlb->addr_mask);
363 if (iotlb->target_as != &address_space_memory) {
364 error_report("Wrong target AS \"%s\", only system memory is allowed",
365 iotlb->target_as->name ? iotlb->target_as->name : "none");
366 return;
369 rcu_read_lock();
371 if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
372 if (!vfio_get_vaddr(iotlb, &vaddr, &read_only)) {
373 goto out;
376 * vaddr is only valid until rcu_read_unlock(). But after
377 * vfio_dma_map has set up the mapping the pages will be
378 * pinned by the kernel. This makes sure that the RAM backend
379 * of vaddr will always be there, even if the memory object is
380 * destroyed and its backing memory munmap-ed.
382 ret = vfio_dma_map(container, iova,
383 iotlb->addr_mask + 1, vaddr,
384 read_only);
385 if (ret) {
386 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
387 "0x%"HWADDR_PRIx", %p) = %d (%m)",
388 container, iova,
389 iotlb->addr_mask + 1, vaddr, ret);
391 } else {
392 ret = vfio_dma_unmap(container, iova, iotlb->addr_mask + 1);
393 if (ret) {
394 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
395 "0x%"HWADDR_PRIx") = %d (%m)",
396 container, iova,
397 iotlb->addr_mask + 1, ret);
400 out:
401 rcu_read_unlock();
404 static void vfio_listener_region_add(MemoryListener *listener,
405 MemoryRegionSection *section)
407 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
408 hwaddr iova, end;
409 Int128 llend, llsize;
410 void *vaddr;
411 int ret;
412 VFIOHostDMAWindow *hostwin;
413 bool hostwin_found;
415 if (vfio_listener_skipped_section(section)) {
416 trace_vfio_listener_region_add_skip(
417 section->offset_within_address_space,
418 section->offset_within_address_space +
419 int128_get64(int128_sub(section->size, int128_one())));
420 return;
423 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
424 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
425 error_report("%s received unaligned region", __func__);
426 return;
429 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
430 llend = int128_make64(section->offset_within_address_space);
431 llend = int128_add(llend, section->size);
432 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
434 if (int128_ge(int128_make64(iova), llend)) {
435 return;
437 end = int128_get64(int128_sub(llend, int128_one()));
439 if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
440 hwaddr pgsize = 0;
442 /* For now intersections are not allowed, we may relax this later */
443 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
444 if (ranges_overlap(hostwin->min_iova,
445 hostwin->max_iova - hostwin->min_iova + 1,
446 section->offset_within_address_space,
447 int128_get64(section->size))) {
448 ret = -1;
449 goto fail;
453 ret = vfio_spapr_create_window(container, section, &pgsize);
454 if (ret) {
455 goto fail;
458 vfio_host_win_add(container, section->offset_within_address_space,
459 section->offset_within_address_space +
460 int128_get64(section->size) - 1, pgsize);
461 #ifdef CONFIG_KVM
462 if (kvm_enabled()) {
463 VFIOGroup *group;
464 IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
465 struct kvm_vfio_spapr_tce param;
466 struct kvm_device_attr attr = {
467 .group = KVM_DEV_VFIO_GROUP,
468 .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
469 .addr = (uint64_t)(unsigned long)&param,
472 if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD,
473 &param.tablefd)) {
474 QLIST_FOREACH(group, &container->group_list, container_next) {
475 param.groupfd = group->fd;
476 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
477 error_report("vfio: failed to setup fd %d "
478 "for a group with fd %d: %s",
479 param.tablefd, param.groupfd,
480 strerror(errno));
481 return;
483 trace_vfio_spapr_group_attach(param.groupfd, param.tablefd);
487 #endif
490 hostwin_found = false;
491 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
492 if (hostwin->min_iova <= iova && end <= hostwin->max_iova) {
493 hostwin_found = true;
494 break;
498 if (!hostwin_found) {
499 error_report("vfio: IOMMU container %p can't map guest IOVA region"
500 " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx,
501 container, iova, end);
502 ret = -EFAULT;
503 goto fail;
506 memory_region_ref(section->mr);
508 if (memory_region_is_iommu(section->mr)) {
509 VFIOGuestIOMMU *giommu;
510 IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
511 int iommu_idx;
513 trace_vfio_listener_region_add_iommu(iova, end);
515 * FIXME: For VFIO iommu types which have KVM acceleration to
516 * avoid bouncing all map/unmaps through qemu this way, this
517 * would be the right place to wire that up (tell the KVM
518 * device emulation the VFIO iommu handles to use).
520 giommu = g_malloc0(sizeof(*giommu));
521 giommu->iommu = iommu_mr;
522 giommu->iommu_offset = section->offset_within_address_space -
523 section->offset_within_region;
524 giommu->container = container;
525 llend = int128_add(int128_make64(section->offset_within_region),
526 section->size);
527 llend = int128_sub(llend, int128_one());
528 iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
529 MEMTXATTRS_UNSPECIFIED);
530 iommu_notifier_init(&giommu->n, vfio_iommu_map_notify,
531 IOMMU_NOTIFIER_ALL,
532 section->offset_within_region,
533 int128_get64(llend),
534 iommu_idx);
535 QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next);
537 memory_region_register_iommu_notifier(section->mr, &giommu->n);
538 memory_region_iommu_replay(giommu->iommu, &giommu->n);
540 return;
543 /* Here we assume that memory_region_is_ram(section->mr)==true */
545 vaddr = memory_region_get_ram_ptr(section->mr) +
546 section->offset_within_region +
547 (iova - section->offset_within_address_space);
549 trace_vfio_listener_region_add_ram(iova, end, vaddr);
551 llsize = int128_sub(llend, int128_make64(iova));
553 if (memory_region_is_ram_device(section->mr)) {
554 hwaddr pgmask = (1ULL << ctz64(hostwin->iova_pgsizes)) - 1;
556 if ((iova & pgmask) || (int128_get64(llsize) & pgmask)) {
557 trace_vfio_listener_region_add_no_dma_map(
558 memory_region_name(section->mr),
559 section->offset_within_address_space,
560 int128_getlo(section->size),
561 pgmask + 1);
562 return;
566 ret = vfio_dma_map(container, iova, int128_get64(llsize),
567 vaddr, section->readonly);
568 if (ret) {
569 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
570 "0x%"HWADDR_PRIx", %p) = %d (%m)",
571 container, iova, int128_get64(llsize), vaddr, ret);
572 if (memory_region_is_ram_device(section->mr)) {
573 /* Allow unexpected mappings not to be fatal for RAM devices */
574 return;
576 goto fail;
579 return;
581 fail:
582 if (memory_region_is_ram_device(section->mr)) {
583 error_report("failed to vfio_dma_map. pci p2p may not work");
584 return;
587 * On the initfn path, store the first error in the container so we
588 * can gracefully fail. Runtime, there's not much we can do other
589 * than throw a hardware error.
591 if (!container->initialized) {
592 if (!container->error) {
593 container->error = ret;
595 } else {
596 hw_error("vfio: DMA mapping failed, unable to continue");
600 static void vfio_listener_region_del(MemoryListener *listener,
601 MemoryRegionSection *section)
603 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
604 hwaddr iova, end;
605 Int128 llend, llsize;
606 int ret;
607 bool try_unmap = true;
609 if (vfio_listener_skipped_section(section)) {
610 trace_vfio_listener_region_del_skip(
611 section->offset_within_address_space,
612 section->offset_within_address_space +
613 int128_get64(int128_sub(section->size, int128_one())));
614 return;
617 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
618 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
619 error_report("%s received unaligned region", __func__);
620 return;
623 if (memory_region_is_iommu(section->mr)) {
624 VFIOGuestIOMMU *giommu;
626 QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
627 if (MEMORY_REGION(giommu->iommu) == section->mr &&
628 giommu->n.start == section->offset_within_region) {
629 memory_region_unregister_iommu_notifier(section->mr,
630 &giommu->n);
631 QLIST_REMOVE(giommu, giommu_next);
632 g_free(giommu);
633 break;
638 * FIXME: We assume the one big unmap below is adequate to
639 * remove any individual page mappings in the IOMMU which
640 * might have been copied into VFIO. This works for a page table
641 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
642 * That may not be true for all IOMMU types.
646 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
647 llend = int128_make64(section->offset_within_address_space);
648 llend = int128_add(llend, section->size);
649 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
651 if (int128_ge(int128_make64(iova), llend)) {
652 return;
654 end = int128_get64(int128_sub(llend, int128_one()));
656 llsize = int128_sub(llend, int128_make64(iova));
658 trace_vfio_listener_region_del(iova, end);
660 if (memory_region_is_ram_device(section->mr)) {
661 hwaddr pgmask;
662 VFIOHostDMAWindow *hostwin;
663 bool hostwin_found = false;
665 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
666 if (hostwin->min_iova <= iova && end <= hostwin->max_iova) {
667 hostwin_found = true;
668 break;
671 assert(hostwin_found); /* or region_add() would have failed */
673 pgmask = (1ULL << ctz64(hostwin->iova_pgsizes)) - 1;
674 try_unmap = !((iova & pgmask) || (int128_get64(llsize) & pgmask));
677 if (try_unmap) {
678 ret = vfio_dma_unmap(container, iova, int128_get64(llsize));
679 if (ret) {
680 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
681 "0x%"HWADDR_PRIx") = %d (%m)",
682 container, iova, int128_get64(llsize), ret);
686 memory_region_unref(section->mr);
688 if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
689 vfio_spapr_remove_window(container,
690 section->offset_within_address_space);
691 if (vfio_host_win_del(container,
692 section->offset_within_address_space,
693 section->offset_within_address_space +
694 int128_get64(section->size) - 1) < 0) {
695 hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx,
696 __func__, section->offset_within_address_space);
701 static const MemoryListener vfio_memory_listener = {
702 .region_add = vfio_listener_region_add,
703 .region_del = vfio_listener_region_del,
706 static void vfio_listener_release(VFIOContainer *container)
708 memory_listener_unregister(&container->listener);
709 if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
710 memory_listener_unregister(&container->prereg_listener);
714 static struct vfio_info_cap_header *
715 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id)
717 struct vfio_info_cap_header *hdr;
718 void *ptr = info;
720 if (!(info->flags & VFIO_REGION_INFO_FLAG_CAPS)) {
721 return NULL;
724 for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) {
725 if (hdr->id == id) {
726 return hdr;
730 return NULL;
733 static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
734 struct vfio_region_info *info)
736 struct vfio_info_cap_header *hdr;
737 struct vfio_region_info_cap_sparse_mmap *sparse;
738 int i, j;
740 hdr = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP);
741 if (!hdr) {
742 return -ENODEV;
745 sparse = container_of(hdr, struct vfio_region_info_cap_sparse_mmap, header);
747 trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
748 region->nr, sparse->nr_areas);
750 region->mmaps = g_new0(VFIOMmap, sparse->nr_areas);
752 for (i = 0, j = 0; i < sparse->nr_areas; i++) {
753 trace_vfio_region_sparse_mmap_entry(i, sparse->areas[i].offset,
754 sparse->areas[i].offset +
755 sparse->areas[i].size);
757 if (sparse->areas[i].size) {
758 region->mmaps[j].offset = sparse->areas[i].offset;
759 region->mmaps[j].size = sparse->areas[i].size;
760 j++;
764 region->nr_mmaps = j;
765 region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
767 return 0;
770 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
771 int index, const char *name)
773 struct vfio_region_info *info;
774 int ret;
776 ret = vfio_get_region_info(vbasedev, index, &info);
777 if (ret) {
778 return ret;
781 region->vbasedev = vbasedev;
782 region->flags = info->flags;
783 region->size = info->size;
784 region->fd_offset = info->offset;
785 region->nr = index;
787 if (region->size) {
788 region->mem = g_new0(MemoryRegion, 1);
789 memory_region_init_io(region->mem, obj, &vfio_region_ops,
790 region, name, region->size);
792 if (!vbasedev->no_mmap &&
793 region->flags & VFIO_REGION_INFO_FLAG_MMAP) {
795 ret = vfio_setup_region_sparse_mmaps(region, info);
797 if (ret) {
798 region->nr_mmaps = 1;
799 region->mmaps = g_new0(VFIOMmap, region->nr_mmaps);
800 region->mmaps[0].offset = 0;
801 region->mmaps[0].size = region->size;
806 g_free(info);
808 trace_vfio_region_setup(vbasedev->name, index, name,
809 region->flags, region->fd_offset, region->size);
810 return 0;
813 int vfio_region_mmap(VFIORegion *region)
815 int i, prot = 0;
816 char *name;
818 if (!region->mem) {
819 return 0;
822 prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;
823 prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;
825 for (i = 0; i < region->nr_mmaps; i++) {
826 region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,
827 MAP_SHARED, region->vbasedev->fd,
828 region->fd_offset +
829 region->mmaps[i].offset);
830 if (region->mmaps[i].mmap == MAP_FAILED) {
831 int ret = -errno;
833 trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,
834 region->fd_offset +
835 region->mmaps[i].offset,
836 region->fd_offset +
837 region->mmaps[i].offset +
838 region->mmaps[i].size - 1, ret);
840 region->mmaps[i].mmap = NULL;
842 for (i--; i >= 0; i--) {
843 memory_region_del_subregion(region->mem, &region->mmaps[i].mem);
844 munmap(region->mmaps[i].mmap, region->mmaps[i].size);
845 object_unparent(OBJECT(&region->mmaps[i].mem));
846 region->mmaps[i].mmap = NULL;
849 return ret;
852 name = g_strdup_printf("%s mmaps[%d]",
853 memory_region_name(region->mem), i);
854 memory_region_init_ram_device_ptr(&region->mmaps[i].mem,
855 memory_region_owner(region->mem),
856 name, region->mmaps[i].size,
857 region->mmaps[i].mmap);
858 g_free(name);
859 memory_region_add_subregion(region->mem, region->mmaps[i].offset,
860 &region->mmaps[i].mem);
862 trace_vfio_region_mmap(memory_region_name(&region->mmaps[i].mem),
863 region->mmaps[i].offset,
864 region->mmaps[i].offset +
865 region->mmaps[i].size - 1);
868 return 0;
871 void vfio_region_exit(VFIORegion *region)
873 int i;
875 if (!region->mem) {
876 return;
879 for (i = 0; i < region->nr_mmaps; i++) {
880 if (region->mmaps[i].mmap) {
881 memory_region_del_subregion(region->mem, &region->mmaps[i].mem);
885 trace_vfio_region_exit(region->vbasedev->name, region->nr);
888 void vfio_region_finalize(VFIORegion *region)
890 int i;
892 if (!region->mem) {
893 return;
896 for (i = 0; i < region->nr_mmaps; i++) {
897 if (region->mmaps[i].mmap) {
898 munmap(region->mmaps[i].mmap, region->mmaps[i].size);
899 object_unparent(OBJECT(&region->mmaps[i].mem));
903 object_unparent(OBJECT(region->mem));
905 g_free(region->mem);
906 g_free(region->mmaps);
908 trace_vfio_region_finalize(region->vbasedev->name, region->nr);
910 region->mem = NULL;
911 region->mmaps = NULL;
912 region->nr_mmaps = 0;
913 region->size = 0;
914 region->flags = 0;
915 region->nr = 0;
918 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
920 int i;
922 if (!region->mem) {
923 return;
926 for (i = 0; i < region->nr_mmaps; i++) {
927 if (region->mmaps[i].mmap) {
928 memory_region_set_enabled(&region->mmaps[i].mem, enabled);
932 trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
933 enabled);
936 void vfio_reset_handler(void *opaque)
938 VFIOGroup *group;
939 VFIODevice *vbasedev;
941 QLIST_FOREACH(group, &vfio_group_list, next) {
942 QLIST_FOREACH(vbasedev, &group->device_list, next) {
943 if (vbasedev->dev->realized) {
944 vbasedev->ops->vfio_compute_needs_reset(vbasedev);
949 QLIST_FOREACH(group, &vfio_group_list, next) {
950 QLIST_FOREACH(vbasedev, &group->device_list, next) {
951 if (vbasedev->dev->realized && vbasedev->needs_reset) {
952 vbasedev->ops->vfio_hot_reset_multi(vbasedev);
958 static void vfio_kvm_device_add_group(VFIOGroup *group)
960 #ifdef CONFIG_KVM
961 struct kvm_device_attr attr = {
962 .group = KVM_DEV_VFIO_GROUP,
963 .attr = KVM_DEV_VFIO_GROUP_ADD,
964 .addr = (uint64_t)(unsigned long)&group->fd,
967 if (!kvm_enabled()) {
968 return;
971 if (vfio_kvm_device_fd < 0) {
972 struct kvm_create_device cd = {
973 .type = KVM_DEV_TYPE_VFIO,
976 if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {
977 error_report("Failed to create KVM VFIO device: %m");
978 return;
981 vfio_kvm_device_fd = cd.fd;
984 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
985 error_report("Failed to add group %d to KVM VFIO device: %m",
986 group->groupid);
988 #endif
991 static void vfio_kvm_device_del_group(VFIOGroup *group)
993 #ifdef CONFIG_KVM
994 struct kvm_device_attr attr = {
995 .group = KVM_DEV_VFIO_GROUP,
996 .attr = KVM_DEV_VFIO_GROUP_DEL,
997 .addr = (uint64_t)(unsigned long)&group->fd,
1000 if (vfio_kvm_device_fd < 0) {
1001 return;
1004 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
1005 error_report("Failed to remove group %d from KVM VFIO device: %m",
1006 group->groupid);
1008 #endif
1011 static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as)
1013 VFIOAddressSpace *space;
1015 QLIST_FOREACH(space, &vfio_address_spaces, list) {
1016 if (space->as == as) {
1017 return space;
1021 /* No suitable VFIOAddressSpace, create a new one */
1022 space = g_malloc0(sizeof(*space));
1023 space->as = as;
1024 QLIST_INIT(&space->containers);
1026 QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
1028 return space;
1031 static void vfio_put_address_space(VFIOAddressSpace *space)
1033 if (QLIST_EMPTY(&space->containers)) {
1034 QLIST_REMOVE(space, list);
1035 g_free(space);
1039 static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
1040 Error **errp)
1042 VFIOContainer *container;
1043 int ret, fd;
1044 VFIOAddressSpace *space;
1046 space = vfio_get_address_space(as);
1049 * VFIO is currently incompatible with memory ballooning insofar as the
1050 * madvise to purge (zap) the page from QEMU's address space does not
1051 * interact with the memory API and therefore leaves stale virtual to
1052 * physical mappings in the IOMMU if the page was previously pinned. We
1053 * therefore add a balloon inhibit for each group added to a container,
1054 * whether the container is used individually or shared. This provides
1055 * us with options to allow devices within a group to opt-in and allow
1056 * ballooning, so long as it is done consistently for a group (for instance
1057 * if the device is an mdev device where it is known that the host vendor
1058 * driver will never pin pages outside of the working set of the guest
1059 * driver, which would thus not be ballooning candidates).
1061 * The first opportunity to induce pinning occurs here where we attempt to
1062 * attach the group to existing containers within the AddressSpace. If any
1063 * pages are already zapped from the virtual address space, such as from a
1064 * previous ballooning opt-in, new pinning will cause valid mappings to be
1065 * re-established. Likewise, when the overall MemoryListener for a new
1066 * container is registered, a replay of mappings within the AddressSpace
1067 * will occur, re-establishing any previously zapped pages as well.
1069 * NB. Balloon inhibiting does not currently block operation of the
1070 * balloon driver or revoke previously pinned pages, it only prevents
1071 * calling madvise to modify the virtual mapping of ballooned pages.
1073 qemu_balloon_inhibit(true);
1075 QLIST_FOREACH(container, &space->containers, next) {
1076 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
1077 group->container = container;
1078 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1079 vfio_kvm_device_add_group(group);
1080 return 0;
1084 fd = qemu_open("/dev/vfio/vfio", O_RDWR);
1085 if (fd < 0) {
1086 error_setg_errno(errp, errno, "failed to open /dev/vfio/vfio");
1087 ret = -errno;
1088 goto put_space_exit;
1091 ret = ioctl(fd, VFIO_GET_API_VERSION);
1092 if (ret != VFIO_API_VERSION) {
1093 error_setg(errp, "supported vfio version: %d, "
1094 "reported version: %d", VFIO_API_VERSION, ret);
1095 ret = -EINVAL;
1096 goto close_fd_exit;
1099 container = g_malloc0(sizeof(*container));
1100 container->space = space;
1101 container->fd = fd;
1102 QLIST_INIT(&container->giommu_list);
1103 QLIST_INIT(&container->hostwin_list);
1104 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) ||
1105 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) {
1106 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU);
1107 struct vfio_iommu_type1_info info;
1109 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
1110 if (ret) {
1111 error_setg_errno(errp, errno, "failed to set group container");
1112 ret = -errno;
1113 goto free_container_exit;
1116 container->iommu_type = v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU;
1117 ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type);
1118 if (ret) {
1119 error_setg_errno(errp, errno, "failed to set iommu for container");
1120 ret = -errno;
1121 goto free_container_exit;
1125 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
1126 * IOVA whatsoever. That's not actually true, but the current
1127 * kernel interface doesn't tell us what it can map, and the
1128 * existing Type1 IOMMUs generally support any IOVA we're
1129 * going to actually try in practice.
1131 info.argsz = sizeof(info);
1132 ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info);
1133 /* Ignore errors */
1134 if (ret || !(info.flags & VFIO_IOMMU_INFO_PGSIZES)) {
1135 /* Assume 4k IOVA page size */
1136 info.iova_pgsizes = 4096;
1138 vfio_host_win_add(container, 0, (hwaddr)-1, info.iova_pgsizes);
1139 } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU) ||
1140 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU)) {
1141 struct vfio_iommu_spapr_tce_info info;
1142 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU);
1144 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
1145 if (ret) {
1146 error_setg_errno(errp, errno, "failed to set group container");
1147 ret = -errno;
1148 goto free_container_exit;
1150 container->iommu_type =
1151 v2 ? VFIO_SPAPR_TCE_v2_IOMMU : VFIO_SPAPR_TCE_IOMMU;
1152 ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type);
1153 if (ret) {
1154 container->iommu_type = VFIO_SPAPR_TCE_IOMMU;
1155 v2 = false;
1156 ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type);
1158 if (ret) {
1159 error_setg_errno(errp, errno, "failed to set iommu for container");
1160 ret = -errno;
1161 goto free_container_exit;
1165 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
1166 * when container fd is closed so we do not call it explicitly
1167 * in this file.
1169 if (!v2) {
1170 ret = ioctl(fd, VFIO_IOMMU_ENABLE);
1171 if (ret) {
1172 error_setg_errno(errp, errno, "failed to enable container");
1173 ret = -errno;
1174 goto free_container_exit;
1176 } else {
1177 container->prereg_listener = vfio_prereg_listener;
1179 memory_listener_register(&container->prereg_listener,
1180 &address_space_memory);
1181 if (container->error) {
1182 memory_listener_unregister(&container->prereg_listener);
1183 ret = container->error;
1184 error_setg(errp,
1185 "RAM memory listener initialization failed for container");
1186 goto free_container_exit;
1190 info.argsz = sizeof(info);
1191 ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1192 if (ret) {
1193 error_setg_errno(errp, errno,
1194 "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed");
1195 ret = -errno;
1196 if (v2) {
1197 memory_listener_unregister(&container->prereg_listener);
1199 goto free_container_exit;
1202 if (v2) {
1204 * There is a default window in just created container.
1205 * To make region_add/del simpler, we better remove this
1206 * window now and let those iommu_listener callbacks
1207 * create/remove them when needed.
1209 ret = vfio_spapr_remove_window(container, info.dma32_window_start);
1210 if (ret) {
1211 error_setg_errno(errp, -ret,
1212 "failed to remove existing window");
1213 goto free_container_exit;
1215 } else {
1216 /* The default table uses 4K pages */
1217 vfio_host_win_add(container, info.dma32_window_start,
1218 info.dma32_window_start +
1219 info.dma32_window_size - 1,
1220 0x1000);
1222 } else {
1223 error_setg(errp, "No available IOMMU models");
1224 ret = -EINVAL;
1225 goto free_container_exit;
1228 vfio_kvm_device_add_group(group);
1230 QLIST_INIT(&container->group_list);
1231 QLIST_INSERT_HEAD(&space->containers, container, next);
1233 group->container = container;
1234 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1236 container->listener = vfio_memory_listener;
1238 memory_listener_register(&container->listener, container->space->as);
1240 if (container->error) {
1241 ret = container->error;
1242 error_setg_errno(errp, -ret,
1243 "memory listener initialization failed for container");
1244 goto listener_release_exit;
1247 container->initialized = true;
1249 return 0;
1250 listener_release_exit:
1251 QLIST_REMOVE(group, container_next);
1252 QLIST_REMOVE(container, next);
1253 vfio_kvm_device_del_group(group);
1254 vfio_listener_release(container);
1256 free_container_exit:
1257 g_free(container);
1259 close_fd_exit:
1260 close(fd);
1262 put_space_exit:
1263 qemu_balloon_inhibit(false);
1264 vfio_put_address_space(space);
1266 return ret;
1269 static void vfio_disconnect_container(VFIOGroup *group)
1271 VFIOContainer *container = group->container;
1273 QLIST_REMOVE(group, container_next);
1274 group->container = NULL;
1277 * Explicitly release the listener first before unset container,
1278 * since unset may destroy the backend container if it's the last
1279 * group.
1281 if (QLIST_EMPTY(&container->group_list)) {
1282 vfio_listener_release(container);
1285 if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
1286 error_report("vfio: error disconnecting group %d from container",
1287 group->groupid);
1290 if (QLIST_EMPTY(&container->group_list)) {
1291 VFIOAddressSpace *space = container->space;
1292 VFIOGuestIOMMU *giommu, *tmp;
1294 QLIST_REMOVE(container, next);
1296 QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) {
1297 memory_region_unregister_iommu_notifier(
1298 MEMORY_REGION(giommu->iommu), &giommu->n);
1299 QLIST_REMOVE(giommu, giommu_next);
1300 g_free(giommu);
1303 trace_vfio_disconnect_container(container->fd);
1304 close(container->fd);
1305 g_free(container);
1307 vfio_put_address_space(space);
1311 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
1313 VFIOGroup *group;
1314 char path[32];
1315 struct vfio_group_status status = { .argsz = sizeof(status) };
1317 QLIST_FOREACH(group, &vfio_group_list, next) {
1318 if (group->groupid == groupid) {
1319 /* Found it. Now is it already in the right context? */
1320 if (group->container->space->as == as) {
1321 return group;
1322 } else {
1323 error_setg(errp, "group %d used in multiple address spaces",
1324 group->groupid);
1325 return NULL;
1330 group = g_malloc0(sizeof(*group));
1332 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
1333 group->fd = qemu_open(path, O_RDWR);
1334 if (group->fd < 0) {
1335 error_setg_errno(errp, errno, "failed to open %s", path);
1336 goto free_group_exit;
1339 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) {
1340 error_setg_errno(errp, errno, "failed to get group %d status", groupid);
1341 goto close_fd_exit;
1344 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
1345 error_setg(errp, "group %d is not viable", groupid);
1346 error_append_hint(errp,
1347 "Please ensure all devices within the iommu_group "
1348 "are bound to their vfio bus driver.\n");
1349 goto close_fd_exit;
1352 group->groupid = groupid;
1353 QLIST_INIT(&group->device_list);
1355 if (vfio_connect_container(group, as, errp)) {
1356 error_prepend(errp, "failed to setup container for group %d: ",
1357 groupid);
1358 goto close_fd_exit;
1361 if (QLIST_EMPTY(&vfio_group_list)) {
1362 qemu_register_reset(vfio_reset_handler, NULL);
1365 QLIST_INSERT_HEAD(&vfio_group_list, group, next);
1367 return group;
1369 close_fd_exit:
1370 close(group->fd);
1372 free_group_exit:
1373 g_free(group);
1375 return NULL;
1378 void vfio_put_group(VFIOGroup *group)
1380 if (!group || !QLIST_EMPTY(&group->device_list)) {
1381 return;
1384 qemu_balloon_inhibit(false);
1385 vfio_kvm_device_del_group(group);
1386 vfio_disconnect_container(group);
1387 QLIST_REMOVE(group, next);
1388 trace_vfio_put_group(group->fd);
1389 close(group->fd);
1390 g_free(group);
1392 if (QLIST_EMPTY(&vfio_group_list)) {
1393 qemu_unregister_reset(vfio_reset_handler, NULL);
1397 int vfio_get_device(VFIOGroup *group, const char *name,
1398 VFIODevice *vbasedev, Error **errp)
1400 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
1401 int ret, fd;
1403 fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
1404 if (fd < 0) {
1405 error_setg_errno(errp, errno, "error getting device from group %d",
1406 group->groupid);
1407 error_append_hint(errp,
1408 "Verify all devices in group %d are bound to vfio-<bus> "
1409 "or pci-stub and not already in use\n", group->groupid);
1410 return fd;
1413 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
1414 if (ret) {
1415 error_setg_errno(errp, errno, "error getting device info");
1416 close(fd);
1417 return ret;
1420 vbasedev->fd = fd;
1421 vbasedev->group = group;
1422 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
1424 vbasedev->num_irqs = dev_info.num_irqs;
1425 vbasedev->num_regions = dev_info.num_regions;
1426 vbasedev->flags = dev_info.flags;
1428 trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
1429 dev_info.num_irqs);
1431 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
1432 return 0;
1435 void vfio_put_base_device(VFIODevice *vbasedev)
1437 if (!vbasedev->group) {
1438 return;
1440 QLIST_REMOVE(vbasedev, next);
1441 vbasedev->group = NULL;
1442 trace_vfio_put_base_device(vbasedev->fd);
1443 close(vbasedev->fd);
1446 int vfio_get_region_info(VFIODevice *vbasedev, int index,
1447 struct vfio_region_info **info)
1449 size_t argsz = sizeof(struct vfio_region_info);
1451 *info = g_malloc0(argsz);
1453 (*info)->index = index;
1454 retry:
1455 (*info)->argsz = argsz;
1457 if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, *info)) {
1458 g_free(*info);
1459 *info = NULL;
1460 return -errno;
1463 if ((*info)->argsz > argsz) {
1464 argsz = (*info)->argsz;
1465 *info = g_realloc(*info, argsz);
1467 goto retry;
1470 return 0;
1473 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
1474 uint32_t subtype, struct vfio_region_info **info)
1476 int i;
1478 for (i = 0; i < vbasedev->num_regions; i++) {
1479 struct vfio_info_cap_header *hdr;
1480 struct vfio_region_info_cap_type *cap_type;
1482 if (vfio_get_region_info(vbasedev, i, info)) {
1483 continue;
1486 hdr = vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE);
1487 if (!hdr) {
1488 g_free(*info);
1489 continue;
1492 cap_type = container_of(hdr, struct vfio_region_info_cap_type, header);
1494 trace_vfio_get_dev_region(vbasedev->name, i,
1495 cap_type->type, cap_type->subtype);
1497 if (cap_type->type == type && cap_type->subtype == subtype) {
1498 return 0;
1501 g_free(*info);
1504 *info = NULL;
1505 return -ENODEV;
1508 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type)
1510 struct vfio_region_info *info = NULL;
1511 bool ret = false;
1513 if (!vfio_get_region_info(vbasedev, region, &info)) {
1514 if (vfio_get_region_info_cap(info, cap_type)) {
1515 ret = true;
1517 g_free(info);
1520 return ret;
1524 * Interfaces for IBM EEH (Enhanced Error Handling)
1526 static bool vfio_eeh_container_ok(VFIOContainer *container)
1529 * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO
1530 * implementation is broken if there are multiple groups in a
1531 * container. The hardware works in units of Partitionable
1532 * Endpoints (== IOMMU groups) and the EEH operations naively
1533 * iterate across all groups in the container, without any logic
1534 * to make sure the groups have their state synchronized. For
1535 * certain operations (ENABLE) that might be ok, until an error
1536 * occurs, but for others (GET_STATE) it's clearly broken.
1540 * XXX Once fixed kernels exist, test for them here
1543 if (QLIST_EMPTY(&container->group_list)) {
1544 return false;
1547 if (QLIST_NEXT(QLIST_FIRST(&container->group_list), container_next)) {
1548 return false;
1551 return true;
1554 static int vfio_eeh_container_op(VFIOContainer *container, uint32_t op)
1556 struct vfio_eeh_pe_op pe_op = {
1557 .argsz = sizeof(pe_op),
1558 .op = op,
1560 int ret;
1562 if (!vfio_eeh_container_ok(container)) {
1563 error_report("vfio/eeh: EEH_PE_OP 0x%x: "
1564 "kernel requires a container with exactly one group", op);
1565 return -EPERM;
1568 ret = ioctl(container->fd, VFIO_EEH_PE_OP, &pe_op);
1569 if (ret < 0) {
1570 error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op);
1571 return -errno;
1574 return ret;
1577 static VFIOContainer *vfio_eeh_as_container(AddressSpace *as)
1579 VFIOAddressSpace *space = vfio_get_address_space(as);
1580 VFIOContainer *container = NULL;
1582 if (QLIST_EMPTY(&space->containers)) {
1583 /* No containers to act on */
1584 goto out;
1587 container = QLIST_FIRST(&space->containers);
1589 if (QLIST_NEXT(container, next)) {
1590 /* We don't yet have logic to synchronize EEH state across
1591 * multiple containers */
1592 container = NULL;
1593 goto out;
1596 out:
1597 vfio_put_address_space(space);
1598 return container;
1601 bool vfio_eeh_as_ok(AddressSpace *as)
1603 VFIOContainer *container = vfio_eeh_as_container(as);
1605 return (container != NULL) && vfio_eeh_container_ok(container);
1608 int vfio_eeh_as_op(AddressSpace *as, uint32_t op)
1610 VFIOContainer *container = vfio_eeh_as_container(as);
1612 if (!container) {
1613 return -ENODEV;
1615 return vfio_eeh_container_op(container, op);