vfio: Record host IOMMU's available IO page sizes
[qemu/ar7.git] / hw / vfio / common.c
blobf666de2c960b0f63159cefc63287e4eff9879af8
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 <sys/ioctl.h>
22 #include <sys/mman.h>
23 #include <linux/vfio.h>
25 #include "hw/vfio/vfio-common.h"
26 #include "hw/vfio/vfio.h"
27 #include "exec/address-spaces.h"
28 #include "exec/memory.h"
29 #include "hw/hw.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/kvm.h"
32 #include "trace.h"
34 struct vfio_group_head vfio_group_list =
35 QLIST_HEAD_INITIALIZER(vfio_group_list);
36 struct vfio_as_head vfio_address_spaces =
37 QLIST_HEAD_INITIALIZER(vfio_address_spaces);
39 #ifdef CONFIG_KVM
41 * We have a single VFIO pseudo device per KVM VM. Once created it lives
42 * for the life of the VM. Closing the file descriptor only drops our
43 * reference to it and the device's reference to kvm. Therefore once
44 * initialized, this file descriptor is only released on QEMU exit and
45 * we'll re-use it should another vfio device be attached before then.
47 static int vfio_kvm_device_fd = -1;
48 #endif
51 * Common VFIO interrupt disable
53 void vfio_disable_irqindex(VFIODevice *vbasedev, int index)
55 struct vfio_irq_set irq_set = {
56 .argsz = sizeof(irq_set),
57 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
58 .index = index,
59 .start = 0,
60 .count = 0,
63 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
66 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index)
68 struct vfio_irq_set irq_set = {
69 .argsz = sizeof(irq_set),
70 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
71 .index = index,
72 .start = 0,
73 .count = 1,
76 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
79 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
81 struct vfio_irq_set irq_set = {
82 .argsz = sizeof(irq_set),
83 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
84 .index = index,
85 .start = 0,
86 .count = 1,
89 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
93 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
95 void vfio_region_write(void *opaque, hwaddr addr,
96 uint64_t data, unsigned size)
98 VFIORegion *region = opaque;
99 VFIODevice *vbasedev = region->vbasedev;
100 union {
101 uint8_t byte;
102 uint16_t word;
103 uint32_t dword;
104 uint64_t qword;
105 } buf;
107 switch (size) {
108 case 1:
109 buf.byte = data;
110 break;
111 case 2:
112 buf.word = cpu_to_le16(data);
113 break;
114 case 4:
115 buf.dword = cpu_to_le32(data);
116 break;
117 default:
118 hw_error("vfio: unsupported write size, %d bytes", size);
119 break;
122 if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
123 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
124 ",%d) failed: %m",
125 __func__, vbasedev->name, region->nr,
126 addr, data, size);
129 trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
132 * A read or write to a BAR always signals an INTx EOI. This will
133 * do nothing if not pending (including not in INTx mode). We assume
134 * that a BAR access is in response to an interrupt and that BAR
135 * accesses will service the interrupt. Unfortunately, we don't know
136 * which access will service the interrupt, so we're potentially
137 * getting quite a few host interrupts per guest interrupt.
139 vbasedev->ops->vfio_eoi(vbasedev);
142 uint64_t vfio_region_read(void *opaque,
143 hwaddr addr, unsigned size)
145 VFIORegion *region = opaque;
146 VFIODevice *vbasedev = region->vbasedev;
147 union {
148 uint8_t byte;
149 uint16_t word;
150 uint32_t dword;
151 uint64_t qword;
152 } buf;
153 uint64_t data = 0;
155 if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
156 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
157 __func__, vbasedev->name, region->nr,
158 addr, size);
159 return (uint64_t)-1;
161 switch (size) {
162 case 1:
163 data = buf.byte;
164 break;
165 case 2:
166 data = le16_to_cpu(buf.word);
167 break;
168 case 4:
169 data = le32_to_cpu(buf.dword);
170 break;
171 default:
172 hw_error("vfio: unsupported read size, %d bytes", size);
173 break;
176 trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data);
178 /* Same as write above */
179 vbasedev->ops->vfio_eoi(vbasedev);
181 return data;
184 const MemoryRegionOps vfio_region_ops = {
185 .read = vfio_region_read,
186 .write = vfio_region_write,
187 .endianness = DEVICE_LITTLE_ENDIAN,
191 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
193 static int vfio_dma_unmap(VFIOContainer *container,
194 hwaddr iova, ram_addr_t size)
196 struct vfio_iommu_type1_dma_unmap unmap = {
197 .argsz = sizeof(unmap),
198 .flags = 0,
199 .iova = iova,
200 .size = size,
203 if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
204 error_report("VFIO_UNMAP_DMA: %d", -errno);
205 return -errno;
208 return 0;
211 static int vfio_dma_map(VFIOContainer *container, hwaddr iova,
212 ram_addr_t size, void *vaddr, bool readonly)
214 struct vfio_iommu_type1_dma_map map = {
215 .argsz = sizeof(map),
216 .flags = VFIO_DMA_MAP_FLAG_READ,
217 .vaddr = (__u64)(uintptr_t)vaddr,
218 .iova = iova,
219 .size = size,
222 if (!readonly) {
223 map.flags |= VFIO_DMA_MAP_FLAG_WRITE;
227 * Try the mapping, if it fails with EBUSY, unmap the region and try
228 * again. This shouldn't be necessary, but we sometimes see it in
229 * the VGA ROM space.
231 if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 ||
232 (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 &&
233 ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) {
234 return 0;
237 error_report("VFIO_MAP_DMA: %d", -errno);
238 return -errno;
241 static bool vfio_listener_skipped_section(MemoryRegionSection *section)
243 return (!memory_region_is_ram(section->mr) &&
244 !memory_region_is_iommu(section->mr)) ||
246 * Sizing an enabled 64-bit BAR can cause spurious mappings to
247 * addresses in the upper part of the 64-bit address space. These
248 * are never accessed by the CPU and beyond the address width of
249 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
251 section->offset_within_address_space & (1ULL << 63);
254 static void vfio_iommu_map_notify(Notifier *n, void *data)
256 VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
257 VFIOContainer *container = giommu->container;
258 IOMMUTLBEntry *iotlb = data;
259 MemoryRegion *mr;
260 hwaddr xlat;
261 hwaddr len = iotlb->addr_mask + 1;
262 void *vaddr;
263 int ret;
265 trace_vfio_iommu_map_notify(iotlb->iova,
266 iotlb->iova + iotlb->addr_mask);
269 * The IOMMU TLB entry we have just covers translation through
270 * this IOMMU to its immediate target. We need to translate
271 * it the rest of the way through to memory.
273 rcu_read_lock();
274 mr = address_space_translate(&address_space_memory,
275 iotlb->translated_addr,
276 &xlat, &len, iotlb->perm & IOMMU_WO);
277 if (!memory_region_is_ram(mr)) {
278 error_report("iommu map to non memory area %"HWADDR_PRIx"",
279 xlat);
280 goto out;
283 * Translation truncates length to the IOMMU page size,
284 * check that it did not truncate too much.
286 if (len & iotlb->addr_mask) {
287 error_report("iommu has granularity incompatible with target AS");
288 goto out;
291 if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
292 vaddr = memory_region_get_ram_ptr(mr) + xlat;
293 ret = vfio_dma_map(container, iotlb->iova,
294 iotlb->addr_mask + 1, vaddr,
295 !(iotlb->perm & IOMMU_WO) || mr->readonly);
296 if (ret) {
297 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
298 "0x%"HWADDR_PRIx", %p) = %d (%m)",
299 container, iotlb->iova,
300 iotlb->addr_mask + 1, vaddr, ret);
302 } else {
303 ret = vfio_dma_unmap(container, iotlb->iova, iotlb->addr_mask + 1);
304 if (ret) {
305 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
306 "0x%"HWADDR_PRIx") = %d (%m)",
307 container, iotlb->iova,
308 iotlb->addr_mask + 1, ret);
311 out:
312 rcu_read_unlock();
315 static void vfio_listener_region_add(MemoryListener *listener,
316 MemoryRegionSection *section)
318 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
319 hwaddr iova, end;
320 Int128 llend;
321 void *vaddr;
322 int ret;
324 if (vfio_listener_skipped_section(section)) {
325 trace_vfio_listener_region_add_skip(
326 section->offset_within_address_space,
327 section->offset_within_address_space +
328 int128_get64(int128_sub(section->size, int128_one())));
329 return;
332 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
333 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
334 error_report("%s received unaligned region", __func__);
335 return;
338 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
339 llend = int128_make64(section->offset_within_address_space);
340 llend = int128_add(llend, section->size);
341 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
343 if (int128_ge(int128_make64(iova), llend)) {
344 return;
346 end = int128_get64(llend);
348 if ((iova < container->min_iova) || ((end - 1) > container->max_iova)) {
349 error_report("vfio: IOMMU container %p can't map guest IOVA region"
350 " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx,
351 container, iova, end - 1);
352 ret = -EFAULT;
353 goto fail;
356 memory_region_ref(section->mr);
358 if (memory_region_is_iommu(section->mr)) {
359 VFIOGuestIOMMU *giommu;
361 trace_vfio_listener_region_add_iommu(iova, end - 1);
363 * FIXME: We should do some checking to see if the
364 * capabilities of the host VFIO IOMMU are adequate to model
365 * the guest IOMMU
367 * FIXME: For VFIO iommu types which have KVM acceleration to
368 * avoid bouncing all map/unmaps through qemu this way, this
369 * would be the right place to wire that up (tell the KVM
370 * device emulation the VFIO iommu handles to use).
373 * This assumes that the guest IOMMU is empty of
374 * mappings at this point.
376 * One way of doing this is:
377 * 1. Avoid sharing IOMMUs between emulated devices or different
378 * IOMMU groups.
379 * 2. Implement VFIO_IOMMU_ENABLE in the host kernel to fail if
380 * there are some mappings in IOMMU.
382 * VFIO on SPAPR does that. Other IOMMU models may do that different,
383 * they must make sure there are no existing mappings or
384 * loop through existing mappings to map them into VFIO.
386 giommu = g_malloc0(sizeof(*giommu));
387 giommu->iommu = section->mr;
388 giommu->container = container;
389 giommu->n.notify = vfio_iommu_map_notify;
390 QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next);
391 memory_region_register_iommu_notifier(giommu->iommu, &giommu->n);
393 return;
396 /* Here we assume that memory_region_is_ram(section->mr)==true */
398 vaddr = memory_region_get_ram_ptr(section->mr) +
399 section->offset_within_region +
400 (iova - section->offset_within_address_space);
402 trace_vfio_listener_region_add_ram(iova, end - 1, vaddr);
404 ret = vfio_dma_map(container, iova, end - iova, vaddr, section->readonly);
405 if (ret) {
406 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
407 "0x%"HWADDR_PRIx", %p) = %d (%m)",
408 container, iova, end - iova, vaddr, ret);
409 goto fail;
412 return;
414 fail:
416 * On the initfn path, store the first error in the container so we
417 * can gracefully fail. Runtime, there's not much we can do other
418 * than throw a hardware error.
420 if (!container->initialized) {
421 if (!container->error) {
422 container->error = ret;
424 } else {
425 hw_error("vfio: DMA mapping failed, unable to continue");
429 static void vfio_listener_region_del(MemoryListener *listener,
430 MemoryRegionSection *section)
432 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
433 hwaddr iova, end;
434 int ret;
436 if (vfio_listener_skipped_section(section)) {
437 trace_vfio_listener_region_del_skip(
438 section->offset_within_address_space,
439 section->offset_within_address_space +
440 int128_get64(int128_sub(section->size, int128_one())));
441 return;
444 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
445 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
446 error_report("%s received unaligned region", __func__);
447 return;
450 if (memory_region_is_iommu(section->mr)) {
451 VFIOGuestIOMMU *giommu;
453 QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
454 if (giommu->iommu == section->mr) {
455 memory_region_unregister_iommu_notifier(&giommu->n);
456 QLIST_REMOVE(giommu, giommu_next);
457 g_free(giommu);
458 break;
463 * FIXME: We assume the one big unmap below is adequate to
464 * remove any individual page mappings in the IOMMU which
465 * might have been copied into VFIO. This works for a page table
466 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
467 * That may not be true for all IOMMU types.
471 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
472 end = (section->offset_within_address_space + int128_get64(section->size)) &
473 TARGET_PAGE_MASK;
475 if (iova >= end) {
476 return;
479 trace_vfio_listener_region_del(iova, end - 1);
481 ret = vfio_dma_unmap(container, iova, end - iova);
482 memory_region_unref(section->mr);
483 if (ret) {
484 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
485 "0x%"HWADDR_PRIx") = %d (%m)",
486 container, iova, end - iova, ret);
490 static const MemoryListener vfio_memory_listener = {
491 .region_add = vfio_listener_region_add,
492 .region_del = vfio_listener_region_del,
495 static void vfio_listener_release(VFIOContainer *container)
497 memory_listener_unregister(&container->listener);
500 int vfio_mmap_region(Object *obj, VFIORegion *region,
501 MemoryRegion *mem, MemoryRegion *submem,
502 void **map, size_t size, off_t offset,
503 const char *name)
505 int ret = 0;
506 VFIODevice *vbasedev = region->vbasedev;
508 if (!vbasedev->no_mmap && size && region->flags &
509 VFIO_REGION_INFO_FLAG_MMAP) {
510 int prot = 0;
512 if (region->flags & VFIO_REGION_INFO_FLAG_READ) {
513 prot |= PROT_READ;
516 if (region->flags & VFIO_REGION_INFO_FLAG_WRITE) {
517 prot |= PROT_WRITE;
520 *map = mmap(NULL, size, prot, MAP_SHARED,
521 vbasedev->fd,
522 region->fd_offset + offset);
523 if (*map == MAP_FAILED) {
524 *map = NULL;
525 ret = -errno;
526 goto empty_region;
529 memory_region_init_ram_ptr(submem, obj, name, size, *map);
530 memory_region_set_skip_dump(submem);
531 } else {
532 empty_region:
533 /* Create a zero sized sub-region to make cleanup easy. */
534 memory_region_init(submem, obj, name, 0);
537 memory_region_add_subregion(mem, offset, submem);
539 return ret;
542 void vfio_reset_handler(void *opaque)
544 VFIOGroup *group;
545 VFIODevice *vbasedev;
547 QLIST_FOREACH(group, &vfio_group_list, next) {
548 QLIST_FOREACH(vbasedev, &group->device_list, next) {
549 vbasedev->ops->vfio_compute_needs_reset(vbasedev);
553 QLIST_FOREACH(group, &vfio_group_list, next) {
554 QLIST_FOREACH(vbasedev, &group->device_list, next) {
555 if (vbasedev->needs_reset) {
556 vbasedev->ops->vfio_hot_reset_multi(vbasedev);
562 static void vfio_kvm_device_add_group(VFIOGroup *group)
564 #ifdef CONFIG_KVM
565 struct kvm_device_attr attr = {
566 .group = KVM_DEV_VFIO_GROUP,
567 .attr = KVM_DEV_VFIO_GROUP_ADD,
568 .addr = (uint64_t)(unsigned long)&group->fd,
571 if (!kvm_enabled()) {
572 return;
575 if (vfio_kvm_device_fd < 0) {
576 struct kvm_create_device cd = {
577 .type = KVM_DEV_TYPE_VFIO,
580 if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {
581 error_report("Failed to create KVM VFIO device: %m");
582 return;
585 vfio_kvm_device_fd = cd.fd;
588 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
589 error_report("Failed to add group %d to KVM VFIO device: %m",
590 group->groupid);
592 #endif
595 static void vfio_kvm_device_del_group(VFIOGroup *group)
597 #ifdef CONFIG_KVM
598 struct kvm_device_attr attr = {
599 .group = KVM_DEV_VFIO_GROUP,
600 .attr = KVM_DEV_VFIO_GROUP_DEL,
601 .addr = (uint64_t)(unsigned long)&group->fd,
604 if (vfio_kvm_device_fd < 0) {
605 return;
608 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
609 error_report("Failed to remove group %d from KVM VFIO device: %m",
610 group->groupid);
612 #endif
615 static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as)
617 VFIOAddressSpace *space;
619 QLIST_FOREACH(space, &vfio_address_spaces, list) {
620 if (space->as == as) {
621 return space;
625 /* No suitable VFIOAddressSpace, create a new one */
626 space = g_malloc0(sizeof(*space));
627 space->as = as;
628 QLIST_INIT(&space->containers);
630 QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
632 return space;
635 static void vfio_put_address_space(VFIOAddressSpace *space)
637 if (QLIST_EMPTY(&space->containers)) {
638 QLIST_REMOVE(space, list);
639 g_free(space);
643 static int vfio_connect_container(VFIOGroup *group, AddressSpace *as)
645 VFIOContainer *container;
646 int ret, fd;
647 VFIOAddressSpace *space;
649 space = vfio_get_address_space(as);
651 QLIST_FOREACH(container, &space->containers, next) {
652 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
653 group->container = container;
654 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
655 return 0;
659 fd = qemu_open("/dev/vfio/vfio", O_RDWR);
660 if (fd < 0) {
661 error_report("vfio: failed to open /dev/vfio/vfio: %m");
662 ret = -errno;
663 goto put_space_exit;
666 ret = ioctl(fd, VFIO_GET_API_VERSION);
667 if (ret != VFIO_API_VERSION) {
668 error_report("vfio: supported vfio version: %d, "
669 "reported version: %d", VFIO_API_VERSION, ret);
670 ret = -EINVAL;
671 goto close_fd_exit;
674 container = g_malloc0(sizeof(*container));
675 container->space = space;
676 container->fd = fd;
677 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) ||
678 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) {
679 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU);
680 struct vfio_iommu_type1_info info;
682 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
683 if (ret) {
684 error_report("vfio: failed to set group container: %m");
685 ret = -errno;
686 goto free_container_exit;
689 ret = ioctl(fd, VFIO_SET_IOMMU,
690 v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU);
691 if (ret) {
692 error_report("vfio: failed to set iommu for container: %m");
693 ret = -errno;
694 goto free_container_exit;
698 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
699 * IOVA whatsoever. That's not actually true, but the current
700 * kernel interface doesn't tell us what it can map, and the
701 * existing Type1 IOMMUs generally support any IOVA we're
702 * going to actually try in practice.
704 container->min_iova = 0;
705 container->max_iova = (hwaddr)-1;
707 /* Assume just 4K IOVA page size */
708 container->iova_pgsizes = 0x1000;
709 info.argsz = sizeof(info);
710 ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info);
711 /* Ignore errors */
712 if ((ret == 0) && (info.flags & VFIO_IOMMU_INFO_PGSIZES)) {
713 container->iova_pgsizes = info.iova_pgsizes;
715 } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
716 struct vfio_iommu_spapr_tce_info info;
718 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
719 if (ret) {
720 error_report("vfio: failed to set group container: %m");
721 ret = -errno;
722 goto free_container_exit;
724 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU);
725 if (ret) {
726 error_report("vfio: failed to set iommu for container: %m");
727 ret = -errno;
728 goto free_container_exit;
732 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
733 * when container fd is closed so we do not call it explicitly
734 * in this file.
736 ret = ioctl(fd, VFIO_IOMMU_ENABLE);
737 if (ret) {
738 error_report("vfio: failed to enable container: %m");
739 ret = -errno;
740 goto free_container_exit;
744 * This only considers the host IOMMU's 32-bit window. At
745 * some point we need to add support for the optional 64-bit
746 * window and dynamic windows
748 info.argsz = sizeof(info);
749 ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
750 if (ret) {
751 error_report("vfio: VFIO_IOMMU_SPAPR_TCE_GET_INFO failed: %m");
752 ret = -errno;
753 goto free_container_exit;
755 container->min_iova = info.dma32_window_start;
756 container->max_iova = container->min_iova + info.dma32_window_size - 1;
758 /* Assume just 4K IOVA pages for now */
759 container->iova_pgsizes = 0x1000;
760 } else {
761 error_report("vfio: No available IOMMU models");
762 ret = -EINVAL;
763 goto free_container_exit;
766 container->listener = vfio_memory_listener;
768 memory_listener_register(&container->listener, container->space->as);
770 if (container->error) {
771 ret = container->error;
772 error_report("vfio: memory listener initialization failed for container");
773 goto listener_release_exit;
776 container->initialized = true;
778 QLIST_INIT(&container->group_list);
779 QLIST_INSERT_HEAD(&space->containers, container, next);
781 group->container = container;
782 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
784 return 0;
785 listener_release_exit:
786 vfio_listener_release(container);
788 free_container_exit:
789 g_free(container);
791 close_fd_exit:
792 close(fd);
794 put_space_exit:
795 vfio_put_address_space(space);
797 return ret;
800 static void vfio_disconnect_container(VFIOGroup *group)
802 VFIOContainer *container = group->container;
804 if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
805 error_report("vfio: error disconnecting group %d from container",
806 group->groupid);
809 QLIST_REMOVE(group, container_next);
810 group->container = NULL;
812 if (QLIST_EMPTY(&container->group_list)) {
813 VFIOAddressSpace *space = container->space;
814 VFIOGuestIOMMU *giommu, *tmp;
816 vfio_listener_release(container);
817 QLIST_REMOVE(container, next);
819 QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) {
820 memory_region_unregister_iommu_notifier(&giommu->n);
821 QLIST_REMOVE(giommu, giommu_next);
822 g_free(giommu);
825 trace_vfio_disconnect_container(container->fd);
826 close(container->fd);
827 g_free(container);
829 vfio_put_address_space(space);
833 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as)
835 VFIOGroup *group;
836 char path[32];
837 struct vfio_group_status status = { .argsz = sizeof(status) };
839 QLIST_FOREACH(group, &vfio_group_list, next) {
840 if (group->groupid == groupid) {
841 /* Found it. Now is it already in the right context? */
842 if (group->container->space->as == as) {
843 return group;
844 } else {
845 error_report("vfio: group %d used in multiple address spaces",
846 group->groupid);
847 return NULL;
852 group = g_malloc0(sizeof(*group));
854 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
855 group->fd = qemu_open(path, O_RDWR);
856 if (group->fd < 0) {
857 error_report("vfio: error opening %s: %m", path);
858 goto free_group_exit;
861 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) {
862 error_report("vfio: error getting group status: %m");
863 goto close_fd_exit;
866 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
867 error_report("vfio: error, group %d is not viable, please ensure "
868 "all devices within the iommu_group are bound to their "
869 "vfio bus driver.", groupid);
870 goto close_fd_exit;
873 group->groupid = groupid;
874 QLIST_INIT(&group->device_list);
876 if (vfio_connect_container(group, as)) {
877 error_report("vfio: failed to setup container for group %d", groupid);
878 goto close_fd_exit;
881 if (QLIST_EMPTY(&vfio_group_list)) {
882 qemu_register_reset(vfio_reset_handler, NULL);
885 QLIST_INSERT_HEAD(&vfio_group_list, group, next);
887 vfio_kvm_device_add_group(group);
889 return group;
891 close_fd_exit:
892 close(group->fd);
894 free_group_exit:
895 g_free(group);
897 return NULL;
900 void vfio_put_group(VFIOGroup *group)
902 if (!group || !QLIST_EMPTY(&group->device_list)) {
903 return;
906 vfio_kvm_device_del_group(group);
907 vfio_disconnect_container(group);
908 QLIST_REMOVE(group, next);
909 trace_vfio_put_group(group->fd);
910 close(group->fd);
911 g_free(group);
913 if (QLIST_EMPTY(&vfio_group_list)) {
914 qemu_unregister_reset(vfio_reset_handler, NULL);
918 int vfio_get_device(VFIOGroup *group, const char *name,
919 VFIODevice *vbasedev)
921 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
922 int ret, fd;
924 fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
925 if (fd < 0) {
926 error_report("vfio: error getting device %s from group %d: %m",
927 name, group->groupid);
928 error_printf("Verify all devices in group %d are bound to vfio-<bus> "
929 "or pci-stub and not already in use\n", group->groupid);
930 return fd;
933 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
934 if (ret) {
935 error_report("vfio: error getting device info: %m");
936 close(fd);
937 return ret;
940 vbasedev->fd = fd;
941 vbasedev->group = group;
942 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
944 vbasedev->num_irqs = dev_info.num_irqs;
945 vbasedev->num_regions = dev_info.num_regions;
946 vbasedev->flags = dev_info.flags;
948 trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
949 dev_info.num_irqs);
951 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
952 return 0;
955 void vfio_put_base_device(VFIODevice *vbasedev)
957 if (!vbasedev->group) {
958 return;
960 QLIST_REMOVE(vbasedev, next);
961 vbasedev->group = NULL;
962 trace_vfio_put_base_device(vbasedev->fd);
963 close(vbasedev->fd);
966 static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
967 int req, void *param)
969 VFIOGroup *group;
970 VFIOContainer *container;
971 int ret = -1;
973 group = vfio_get_group(groupid, as);
974 if (!group) {
975 error_report("vfio: group %d not registered", groupid);
976 return ret;
979 container = group->container;
980 if (group->container) {
981 ret = ioctl(container->fd, req, param);
982 if (ret < 0) {
983 error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
984 _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
988 vfio_put_group(group);
990 return ret;
993 int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
994 int req, void *param)
996 /* We allow only certain ioctls to the container */
997 switch (req) {
998 case VFIO_CHECK_EXTENSION:
999 case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
1000 case VFIO_EEH_PE_OP:
1001 break;
1002 default:
1003 /* Return an error on unknown requests */
1004 error_report("vfio: unsupported ioctl %X", req);
1005 return -1;
1008 return vfio_container_do_ioctl(as, groupid, req, param);