error: Improve documentation some more
[qemu/ar7.git] / hw / vfio / common.c
blob607ec70be390e401a611624931f0b4548adc838f
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 #include <sys/mman.h>
24 #include <linux/vfio.h>
26 #include "hw/vfio/vfio-common.h"
27 #include "hw/vfio/vfio.h"
28 #include "exec/address-spaces.h"
29 #include "exec/memory.h"
30 #include "hw/hw.h"
31 #include "qemu/error-report.h"
32 #include "sysemu/kvm.h"
33 #include "trace.h"
35 struct vfio_group_head vfio_group_list =
36 QLIST_HEAD_INITIALIZER(vfio_group_list);
37 struct vfio_as_head vfio_address_spaces =
38 QLIST_HEAD_INITIALIZER(vfio_address_spaces);
40 #ifdef CONFIG_KVM
42 * We have a single VFIO pseudo device per KVM VM. Once created it lives
43 * for the life of the VM. Closing the file descriptor only drops our
44 * reference to it and the device's reference to kvm. Therefore once
45 * initialized, this file descriptor is only released on QEMU exit and
46 * we'll re-use it should another vfio device be attached before then.
48 static int vfio_kvm_device_fd = -1;
49 #endif
52 * Common VFIO interrupt disable
54 void vfio_disable_irqindex(VFIODevice *vbasedev, int index)
56 struct vfio_irq_set irq_set = {
57 .argsz = sizeof(irq_set),
58 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
59 .index = index,
60 .start = 0,
61 .count = 0,
64 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
67 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index)
69 struct vfio_irq_set irq_set = {
70 .argsz = sizeof(irq_set),
71 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
72 .index = index,
73 .start = 0,
74 .count = 1,
77 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
80 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
82 struct vfio_irq_set irq_set = {
83 .argsz = sizeof(irq_set),
84 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
85 .index = index,
86 .start = 0,
87 .count = 1,
90 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
94 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
96 void vfio_region_write(void *opaque, hwaddr addr,
97 uint64_t data, unsigned size)
99 VFIORegion *region = opaque;
100 VFIODevice *vbasedev = region->vbasedev;
101 union {
102 uint8_t byte;
103 uint16_t word;
104 uint32_t dword;
105 uint64_t qword;
106 } buf;
108 switch (size) {
109 case 1:
110 buf.byte = data;
111 break;
112 case 2:
113 buf.word = cpu_to_le16(data);
114 break;
115 case 4:
116 buf.dword = cpu_to_le32(data);
117 break;
118 default:
119 hw_error("vfio: unsupported write size, %d bytes", size);
120 break;
123 if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
124 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
125 ",%d) failed: %m",
126 __func__, vbasedev->name, region->nr,
127 addr, data, size);
130 trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
133 * A read or write to a BAR always signals an INTx EOI. This will
134 * do nothing if not pending (including not in INTx mode). We assume
135 * that a BAR access is in response to an interrupt and that BAR
136 * accesses will service the interrupt. Unfortunately, we don't know
137 * which access will service the interrupt, so we're potentially
138 * getting quite a few host interrupts per guest interrupt.
140 vbasedev->ops->vfio_eoi(vbasedev);
143 uint64_t vfio_region_read(void *opaque,
144 hwaddr addr, unsigned size)
146 VFIORegion *region = opaque;
147 VFIODevice *vbasedev = region->vbasedev;
148 union {
149 uint8_t byte;
150 uint16_t word;
151 uint32_t dword;
152 uint64_t qword;
153 } buf;
154 uint64_t data = 0;
156 if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
157 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
158 __func__, vbasedev->name, region->nr,
159 addr, size);
160 return (uint64_t)-1;
162 switch (size) {
163 case 1:
164 data = buf.byte;
165 break;
166 case 2:
167 data = le16_to_cpu(buf.word);
168 break;
169 case 4:
170 data = le32_to_cpu(buf.dword);
171 break;
172 default:
173 hw_error("vfio: unsupported read size, %d bytes", size);
174 break;
177 trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data);
179 /* Same as write above */
180 vbasedev->ops->vfio_eoi(vbasedev);
182 return data;
185 const MemoryRegionOps vfio_region_ops = {
186 .read = vfio_region_read,
187 .write = vfio_region_write,
188 .endianness = DEVICE_LITTLE_ENDIAN,
192 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
194 static int vfio_dma_unmap(VFIOContainer *container,
195 hwaddr iova, ram_addr_t size)
197 struct vfio_iommu_type1_dma_unmap unmap = {
198 .argsz = sizeof(unmap),
199 .flags = 0,
200 .iova = iova,
201 .size = size,
204 if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
205 error_report("VFIO_UNMAP_DMA: %d", -errno);
206 return -errno;
209 return 0;
212 static int vfio_dma_map(VFIOContainer *container, hwaddr iova,
213 ram_addr_t size, void *vaddr, bool readonly)
215 struct vfio_iommu_type1_dma_map map = {
216 .argsz = sizeof(map),
217 .flags = VFIO_DMA_MAP_FLAG_READ,
218 .vaddr = (__u64)(uintptr_t)vaddr,
219 .iova = iova,
220 .size = size,
223 if (!readonly) {
224 map.flags |= VFIO_DMA_MAP_FLAG_WRITE;
228 * Try the mapping, if it fails with EBUSY, unmap the region and try
229 * again. This shouldn't be necessary, but we sometimes see it in
230 * the VGA ROM space.
232 if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 ||
233 (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 &&
234 ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) {
235 return 0;
238 error_report("VFIO_MAP_DMA: %d", -errno);
239 return -errno;
242 static bool vfio_listener_skipped_section(MemoryRegionSection *section)
244 return (!memory_region_is_ram(section->mr) &&
245 !memory_region_is_iommu(section->mr)) ||
247 * Sizing an enabled 64-bit BAR can cause spurious mappings to
248 * addresses in the upper part of the 64-bit address space. These
249 * are never accessed by the CPU and beyond the address width of
250 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
252 section->offset_within_address_space & (1ULL << 63);
255 static void vfio_iommu_map_notify(Notifier *n, void *data)
257 VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
258 VFIOContainer *container = giommu->container;
259 IOMMUTLBEntry *iotlb = data;
260 MemoryRegion *mr;
261 hwaddr xlat;
262 hwaddr len = iotlb->addr_mask + 1;
263 void *vaddr;
264 int ret;
266 trace_vfio_iommu_map_notify(iotlb->iova,
267 iotlb->iova + iotlb->addr_mask);
270 * The IOMMU TLB entry we have just covers translation through
271 * this IOMMU to its immediate target. We need to translate
272 * it the rest of the way through to memory.
274 rcu_read_lock();
275 mr = address_space_translate(&address_space_memory,
276 iotlb->translated_addr,
277 &xlat, &len, iotlb->perm & IOMMU_WO);
278 if (!memory_region_is_ram(mr)) {
279 error_report("iommu map to non memory area %"HWADDR_PRIx"",
280 xlat);
281 goto out;
284 * Translation truncates length to the IOMMU page size,
285 * check that it did not truncate too much.
287 if (len & iotlb->addr_mask) {
288 error_report("iommu has granularity incompatible with target AS");
289 goto out;
292 if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
293 vaddr = memory_region_get_ram_ptr(mr) + xlat;
294 ret = vfio_dma_map(container, iotlb->iova,
295 iotlb->addr_mask + 1, vaddr,
296 !(iotlb->perm & IOMMU_WO) || mr->readonly);
297 if (ret) {
298 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
299 "0x%"HWADDR_PRIx", %p) = %d (%m)",
300 container, iotlb->iova,
301 iotlb->addr_mask + 1, vaddr, ret);
303 } else {
304 ret = vfio_dma_unmap(container, iotlb->iova, iotlb->addr_mask + 1);
305 if (ret) {
306 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
307 "0x%"HWADDR_PRIx") = %d (%m)",
308 container, iotlb->iova,
309 iotlb->addr_mask + 1, ret);
312 out:
313 rcu_read_unlock();
316 static hwaddr vfio_container_granularity(VFIOContainer *container)
318 return (hwaddr)1 << ctz64(container->iova_pgsizes);
321 static void vfio_listener_region_add(MemoryListener *listener,
322 MemoryRegionSection *section)
324 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
325 hwaddr iova, end;
326 Int128 llend;
327 void *vaddr;
328 int ret;
330 if (vfio_listener_skipped_section(section)) {
331 trace_vfio_listener_region_add_skip(
332 section->offset_within_address_space,
333 section->offset_within_address_space +
334 int128_get64(int128_sub(section->size, int128_one())));
335 return;
338 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
339 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
340 error_report("%s received unaligned region", __func__);
341 return;
344 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
345 llend = int128_make64(section->offset_within_address_space);
346 llend = int128_add(llend, section->size);
347 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
349 if (int128_ge(int128_make64(iova), llend)) {
350 return;
352 end = int128_get64(llend);
354 if ((iova < container->min_iova) || ((end - 1) > container->max_iova)) {
355 error_report("vfio: IOMMU container %p can't map guest IOVA region"
356 " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx,
357 container, iova, end - 1);
358 ret = -EFAULT;
359 goto fail;
362 memory_region_ref(section->mr);
364 if (memory_region_is_iommu(section->mr)) {
365 VFIOGuestIOMMU *giommu;
367 trace_vfio_listener_region_add_iommu(iova, end - 1);
369 * FIXME: We should do some checking to see if the
370 * capabilities of the host VFIO IOMMU are adequate to model
371 * the guest IOMMU
373 * FIXME: For VFIO iommu types which have KVM acceleration to
374 * avoid bouncing all map/unmaps through qemu this way, this
375 * would be the right place to wire that up (tell the KVM
376 * device emulation the VFIO iommu handles to use).
378 giommu = g_malloc0(sizeof(*giommu));
379 giommu->iommu = section->mr;
380 giommu->container = container;
381 giommu->n.notify = vfio_iommu_map_notify;
382 QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next);
384 memory_region_register_iommu_notifier(giommu->iommu, &giommu->n);
385 memory_region_iommu_replay(giommu->iommu, &giommu->n,
386 vfio_container_granularity(container),
387 false);
389 return;
392 /* Here we assume that memory_region_is_ram(section->mr)==true */
394 vaddr = memory_region_get_ram_ptr(section->mr) +
395 section->offset_within_region +
396 (iova - section->offset_within_address_space);
398 trace_vfio_listener_region_add_ram(iova, end - 1, vaddr);
400 ret = vfio_dma_map(container, iova, end - iova, vaddr, section->readonly);
401 if (ret) {
402 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
403 "0x%"HWADDR_PRIx", %p) = %d (%m)",
404 container, iova, end - iova, vaddr, ret);
405 goto fail;
408 return;
410 fail:
412 * On the initfn path, store the first error in the container so we
413 * can gracefully fail. Runtime, there's not much we can do other
414 * than throw a hardware error.
416 if (!container->initialized) {
417 if (!container->error) {
418 container->error = ret;
420 } else {
421 hw_error("vfio: DMA mapping failed, unable to continue");
425 static void vfio_listener_region_del(MemoryListener *listener,
426 MemoryRegionSection *section)
428 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
429 hwaddr iova, end;
430 int ret;
432 if (vfio_listener_skipped_section(section)) {
433 trace_vfio_listener_region_del_skip(
434 section->offset_within_address_space,
435 section->offset_within_address_space +
436 int128_get64(int128_sub(section->size, int128_one())));
437 return;
440 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
441 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
442 error_report("%s received unaligned region", __func__);
443 return;
446 if (memory_region_is_iommu(section->mr)) {
447 VFIOGuestIOMMU *giommu;
449 QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
450 if (giommu->iommu == section->mr) {
451 memory_region_unregister_iommu_notifier(&giommu->n);
452 QLIST_REMOVE(giommu, giommu_next);
453 g_free(giommu);
454 break;
459 * FIXME: We assume the one big unmap below is adequate to
460 * remove any individual page mappings in the IOMMU which
461 * might have been copied into VFIO. This works for a page table
462 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
463 * That may not be true for all IOMMU types.
467 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
468 end = (section->offset_within_address_space + int128_get64(section->size)) &
469 TARGET_PAGE_MASK;
471 if (iova >= end) {
472 return;
475 trace_vfio_listener_region_del(iova, end - 1);
477 ret = vfio_dma_unmap(container, iova, end - iova);
478 memory_region_unref(section->mr);
479 if (ret) {
480 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
481 "0x%"HWADDR_PRIx") = %d (%m)",
482 container, iova, end - iova, ret);
486 static const MemoryListener vfio_memory_listener = {
487 .region_add = vfio_listener_region_add,
488 .region_del = vfio_listener_region_del,
491 static void vfio_listener_release(VFIOContainer *container)
493 memory_listener_unregister(&container->listener);
496 int vfio_mmap_region(Object *obj, VFIORegion *region,
497 MemoryRegion *mem, MemoryRegion *submem,
498 void **map, size_t size, off_t offset,
499 const char *name)
501 int ret = 0;
502 VFIODevice *vbasedev = region->vbasedev;
504 if (!vbasedev->no_mmap && size && region->flags &
505 VFIO_REGION_INFO_FLAG_MMAP) {
506 int prot = 0;
508 if (region->flags & VFIO_REGION_INFO_FLAG_READ) {
509 prot |= PROT_READ;
512 if (region->flags & VFIO_REGION_INFO_FLAG_WRITE) {
513 prot |= PROT_WRITE;
516 *map = mmap(NULL, size, prot, MAP_SHARED,
517 vbasedev->fd,
518 region->fd_offset + offset);
519 if (*map == MAP_FAILED) {
520 *map = NULL;
521 ret = -errno;
522 goto empty_region;
525 memory_region_init_ram_ptr(submem, obj, name, size, *map);
526 memory_region_set_skip_dump(submem);
527 } else {
528 empty_region:
529 /* Create a zero sized sub-region to make cleanup easy. */
530 memory_region_init(submem, obj, name, 0);
533 memory_region_add_subregion(mem, offset, submem);
535 return ret;
538 void vfio_reset_handler(void *opaque)
540 VFIOGroup *group;
541 VFIODevice *vbasedev;
543 QLIST_FOREACH(group, &vfio_group_list, next) {
544 QLIST_FOREACH(vbasedev, &group->device_list, next) {
545 vbasedev->ops->vfio_compute_needs_reset(vbasedev);
549 QLIST_FOREACH(group, &vfio_group_list, next) {
550 QLIST_FOREACH(vbasedev, &group->device_list, next) {
551 if (vbasedev->needs_reset) {
552 vbasedev->ops->vfio_hot_reset_multi(vbasedev);
558 static void vfio_kvm_device_add_group(VFIOGroup *group)
560 #ifdef CONFIG_KVM
561 struct kvm_device_attr attr = {
562 .group = KVM_DEV_VFIO_GROUP,
563 .attr = KVM_DEV_VFIO_GROUP_ADD,
564 .addr = (uint64_t)(unsigned long)&group->fd,
567 if (!kvm_enabled()) {
568 return;
571 if (vfio_kvm_device_fd < 0) {
572 struct kvm_create_device cd = {
573 .type = KVM_DEV_TYPE_VFIO,
576 if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {
577 error_report("Failed to create KVM VFIO device: %m");
578 return;
581 vfio_kvm_device_fd = cd.fd;
584 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
585 error_report("Failed to add group %d to KVM VFIO device: %m",
586 group->groupid);
588 #endif
591 static void vfio_kvm_device_del_group(VFIOGroup *group)
593 #ifdef CONFIG_KVM
594 struct kvm_device_attr attr = {
595 .group = KVM_DEV_VFIO_GROUP,
596 .attr = KVM_DEV_VFIO_GROUP_DEL,
597 .addr = (uint64_t)(unsigned long)&group->fd,
600 if (vfio_kvm_device_fd < 0) {
601 return;
604 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
605 error_report("Failed to remove group %d from KVM VFIO device: %m",
606 group->groupid);
608 #endif
611 static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as)
613 VFIOAddressSpace *space;
615 QLIST_FOREACH(space, &vfio_address_spaces, list) {
616 if (space->as == as) {
617 return space;
621 /* No suitable VFIOAddressSpace, create a new one */
622 space = g_malloc0(sizeof(*space));
623 space->as = as;
624 QLIST_INIT(&space->containers);
626 QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
628 return space;
631 static void vfio_put_address_space(VFIOAddressSpace *space)
633 if (QLIST_EMPTY(&space->containers)) {
634 QLIST_REMOVE(space, list);
635 g_free(space);
639 static int vfio_connect_container(VFIOGroup *group, AddressSpace *as)
641 VFIOContainer *container;
642 int ret, fd;
643 VFIOAddressSpace *space;
645 space = vfio_get_address_space(as);
647 QLIST_FOREACH(container, &space->containers, next) {
648 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
649 group->container = container;
650 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
651 return 0;
655 fd = qemu_open("/dev/vfio/vfio", O_RDWR);
656 if (fd < 0) {
657 error_report("vfio: failed to open /dev/vfio/vfio: %m");
658 ret = -errno;
659 goto put_space_exit;
662 ret = ioctl(fd, VFIO_GET_API_VERSION);
663 if (ret != VFIO_API_VERSION) {
664 error_report("vfio: supported vfio version: %d, "
665 "reported version: %d", VFIO_API_VERSION, ret);
666 ret = -EINVAL;
667 goto close_fd_exit;
670 container = g_malloc0(sizeof(*container));
671 container->space = space;
672 container->fd = fd;
673 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) ||
674 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) {
675 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU);
676 struct vfio_iommu_type1_info info;
678 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
679 if (ret) {
680 error_report("vfio: failed to set group container: %m");
681 ret = -errno;
682 goto free_container_exit;
685 ret = ioctl(fd, VFIO_SET_IOMMU,
686 v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU);
687 if (ret) {
688 error_report("vfio: failed to set iommu for container: %m");
689 ret = -errno;
690 goto free_container_exit;
694 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
695 * IOVA whatsoever. That's not actually true, but the current
696 * kernel interface doesn't tell us what it can map, and the
697 * existing Type1 IOMMUs generally support any IOVA we're
698 * going to actually try in practice.
700 container->min_iova = 0;
701 container->max_iova = (hwaddr)-1;
703 /* Assume just 4K IOVA page size */
704 container->iova_pgsizes = 0x1000;
705 info.argsz = sizeof(info);
706 ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info);
707 /* Ignore errors */
708 if ((ret == 0) && (info.flags & VFIO_IOMMU_INFO_PGSIZES)) {
709 container->iova_pgsizes = info.iova_pgsizes;
711 } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) {
712 struct vfio_iommu_spapr_tce_info info;
714 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
715 if (ret) {
716 error_report("vfio: failed to set group container: %m");
717 ret = -errno;
718 goto free_container_exit;
720 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU);
721 if (ret) {
722 error_report("vfio: failed to set iommu for container: %m");
723 ret = -errno;
724 goto free_container_exit;
728 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
729 * when container fd is closed so we do not call it explicitly
730 * in this file.
732 ret = ioctl(fd, VFIO_IOMMU_ENABLE);
733 if (ret) {
734 error_report("vfio: failed to enable container: %m");
735 ret = -errno;
736 goto free_container_exit;
740 * This only considers the host IOMMU's 32-bit window. At
741 * some point we need to add support for the optional 64-bit
742 * window and dynamic windows
744 info.argsz = sizeof(info);
745 ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
746 if (ret) {
747 error_report("vfio: VFIO_IOMMU_SPAPR_TCE_GET_INFO failed: %m");
748 ret = -errno;
749 goto free_container_exit;
751 container->min_iova = info.dma32_window_start;
752 container->max_iova = container->min_iova + info.dma32_window_size - 1;
754 /* Assume just 4K IOVA pages for now */
755 container->iova_pgsizes = 0x1000;
756 } else {
757 error_report("vfio: No available IOMMU models");
758 ret = -EINVAL;
759 goto free_container_exit;
762 container->listener = vfio_memory_listener;
764 memory_listener_register(&container->listener, container->space->as);
766 if (container->error) {
767 ret = container->error;
768 error_report("vfio: memory listener initialization failed for container");
769 goto listener_release_exit;
772 container->initialized = true;
774 QLIST_INIT(&container->group_list);
775 QLIST_INSERT_HEAD(&space->containers, container, next);
777 group->container = container;
778 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
780 return 0;
781 listener_release_exit:
782 vfio_listener_release(container);
784 free_container_exit:
785 g_free(container);
787 close_fd_exit:
788 close(fd);
790 put_space_exit:
791 vfio_put_address_space(space);
793 return ret;
796 static void vfio_disconnect_container(VFIOGroup *group)
798 VFIOContainer *container = group->container;
800 if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
801 error_report("vfio: error disconnecting group %d from container",
802 group->groupid);
805 QLIST_REMOVE(group, container_next);
806 group->container = NULL;
808 if (QLIST_EMPTY(&container->group_list)) {
809 VFIOAddressSpace *space = container->space;
810 VFIOGuestIOMMU *giommu, *tmp;
812 vfio_listener_release(container);
813 QLIST_REMOVE(container, next);
815 QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) {
816 memory_region_unregister_iommu_notifier(&giommu->n);
817 QLIST_REMOVE(giommu, giommu_next);
818 g_free(giommu);
821 trace_vfio_disconnect_container(container->fd);
822 close(container->fd);
823 g_free(container);
825 vfio_put_address_space(space);
829 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as)
831 VFIOGroup *group;
832 char path[32];
833 struct vfio_group_status status = { .argsz = sizeof(status) };
835 QLIST_FOREACH(group, &vfio_group_list, next) {
836 if (group->groupid == groupid) {
837 /* Found it. Now is it already in the right context? */
838 if (group->container->space->as == as) {
839 return group;
840 } else {
841 error_report("vfio: group %d used in multiple address spaces",
842 group->groupid);
843 return NULL;
848 group = g_malloc0(sizeof(*group));
850 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
851 group->fd = qemu_open(path, O_RDWR);
852 if (group->fd < 0) {
853 error_report("vfio: error opening %s: %m", path);
854 goto free_group_exit;
857 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) {
858 error_report("vfio: error getting group status: %m");
859 goto close_fd_exit;
862 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
863 error_report("vfio: error, group %d is not viable, please ensure "
864 "all devices within the iommu_group are bound to their "
865 "vfio bus driver.", groupid);
866 goto close_fd_exit;
869 group->groupid = groupid;
870 QLIST_INIT(&group->device_list);
872 if (vfio_connect_container(group, as)) {
873 error_report("vfio: failed to setup container for group %d", groupid);
874 goto close_fd_exit;
877 if (QLIST_EMPTY(&vfio_group_list)) {
878 qemu_register_reset(vfio_reset_handler, NULL);
881 QLIST_INSERT_HEAD(&vfio_group_list, group, next);
883 vfio_kvm_device_add_group(group);
885 return group;
887 close_fd_exit:
888 close(group->fd);
890 free_group_exit:
891 g_free(group);
893 return NULL;
896 void vfio_put_group(VFIOGroup *group)
898 if (!group || !QLIST_EMPTY(&group->device_list)) {
899 return;
902 vfio_kvm_device_del_group(group);
903 vfio_disconnect_container(group);
904 QLIST_REMOVE(group, next);
905 trace_vfio_put_group(group->fd);
906 close(group->fd);
907 g_free(group);
909 if (QLIST_EMPTY(&vfio_group_list)) {
910 qemu_unregister_reset(vfio_reset_handler, NULL);
914 int vfio_get_device(VFIOGroup *group, const char *name,
915 VFIODevice *vbasedev)
917 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
918 int ret, fd;
920 fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
921 if (fd < 0) {
922 error_report("vfio: error getting device %s from group %d: %m",
923 name, group->groupid);
924 error_printf("Verify all devices in group %d are bound to vfio-<bus> "
925 "or pci-stub and not already in use\n", group->groupid);
926 return fd;
929 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
930 if (ret) {
931 error_report("vfio: error getting device info: %m");
932 close(fd);
933 return ret;
936 vbasedev->fd = fd;
937 vbasedev->group = group;
938 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
940 vbasedev->num_irqs = dev_info.num_irqs;
941 vbasedev->num_regions = dev_info.num_regions;
942 vbasedev->flags = dev_info.flags;
944 trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
945 dev_info.num_irqs);
947 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
948 return 0;
951 void vfio_put_base_device(VFIODevice *vbasedev)
953 if (!vbasedev->group) {
954 return;
956 QLIST_REMOVE(vbasedev, next);
957 vbasedev->group = NULL;
958 trace_vfio_put_base_device(vbasedev->fd);
959 close(vbasedev->fd);
962 static int vfio_container_do_ioctl(AddressSpace *as, int32_t groupid,
963 int req, void *param)
965 VFIOGroup *group;
966 VFIOContainer *container;
967 int ret = -1;
969 group = vfio_get_group(groupid, as);
970 if (!group) {
971 error_report("vfio: group %d not registered", groupid);
972 return ret;
975 container = group->container;
976 if (group->container) {
977 ret = ioctl(container->fd, req, param);
978 if (ret < 0) {
979 error_report("vfio: failed to ioctl %d to container: ret=%d, %s",
980 _IOC_NR(req) - VFIO_BASE, ret, strerror(errno));
984 vfio_put_group(group);
986 return ret;
989 int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
990 int req, void *param)
992 /* We allow only certain ioctls to the container */
993 switch (req) {
994 case VFIO_CHECK_EXTENSION:
995 case VFIO_IOMMU_SPAPR_TCE_GET_INFO:
996 case VFIO_EEH_PE_OP:
997 break;
998 default:
999 /* Return an error on unknown requests */
1000 error_report("vfio: unsupported ioctl %X", req);
1001 return -1;
1004 return vfio_container_do_ioctl(as, groupid, req, param);