]> git.proxmox.com Git - mirror_qemu.git/blob - hw/vfio/common.c
Merge remote-tracking branch 'remotes/awilliam/tags/vfio-updates-20170710.0' into...
[mirror_qemu.git] / hw / vfio / common.c
1 /*
2 * generic functions used by VFIO devices
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Authors:
7 * Alex Williamson <alex.williamson@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
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)
19 */
20
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>
27
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/kvm.h"
36 #include "trace.h"
37 #include "qapi/error.h"
38
39 struct vfio_group_head vfio_group_list =
40 QLIST_HEAD_INITIALIZER(vfio_group_list);
41 struct vfio_as_head vfio_address_spaces =
42 QLIST_HEAD_INITIALIZER(vfio_address_spaces);
43
44 #ifdef CONFIG_KVM
45 /*
46 * We have a single VFIO pseudo device per KVM VM. Once created it lives
47 * for the life of the VM. Closing the file descriptor only drops our
48 * reference to it and the device's reference to kvm. Therefore once
49 * initialized, this file descriptor is only released on QEMU exit and
50 * we'll re-use it should another vfio device be attached before then.
51 */
52 static int vfio_kvm_device_fd = -1;
53 #endif
54
55 /*
56 * Common VFIO interrupt disable
57 */
58 void vfio_disable_irqindex(VFIODevice *vbasedev, int index)
59 {
60 struct vfio_irq_set irq_set = {
61 .argsz = sizeof(irq_set),
62 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
63 .index = index,
64 .start = 0,
65 .count = 0,
66 };
67
68 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
69 }
70
71 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index)
72 {
73 struct vfio_irq_set irq_set = {
74 .argsz = sizeof(irq_set),
75 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
76 .index = index,
77 .start = 0,
78 .count = 1,
79 };
80
81 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
82 }
83
84 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
85 {
86 struct vfio_irq_set irq_set = {
87 .argsz = sizeof(irq_set),
88 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
89 .index = index,
90 .start = 0,
91 .count = 1,
92 };
93
94 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
95 }
96
97 /*
98 * IO Port/MMIO - Beware of the endians, VFIO is always little endian
99 */
100 void vfio_region_write(void *opaque, hwaddr addr,
101 uint64_t data, unsigned size)
102 {
103 VFIORegion *region = opaque;
104 VFIODevice *vbasedev = region->vbasedev;
105 union {
106 uint8_t byte;
107 uint16_t word;
108 uint32_t dword;
109 uint64_t qword;
110 } buf;
111
112 switch (size) {
113 case 1:
114 buf.byte = data;
115 break;
116 case 2:
117 buf.word = cpu_to_le16(data);
118 break;
119 case 4:
120 buf.dword = cpu_to_le32(data);
121 break;
122 case 8:
123 buf.qword = cpu_to_le64(data);
124 break;
125 default:
126 hw_error("vfio: unsupported write size, %d bytes", size);
127 break;
128 }
129
130 if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
131 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
132 ",%d) failed: %m",
133 __func__, vbasedev->name, region->nr,
134 addr, data, size);
135 }
136
137 trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
138
139 /*
140 * A read or write to a BAR always signals an INTx EOI. This will
141 * do nothing if not pending (including not in INTx mode). We assume
142 * that a BAR access is in response to an interrupt and that BAR
143 * accesses will service the interrupt. Unfortunately, we don't know
144 * which access will service the interrupt, so we're potentially
145 * getting quite a few host interrupts per guest interrupt.
146 */
147 vbasedev->ops->vfio_eoi(vbasedev);
148 }
149
150 uint64_t vfio_region_read(void *opaque,
151 hwaddr addr, unsigned size)
152 {
153 VFIORegion *region = opaque;
154 VFIODevice *vbasedev = region->vbasedev;
155 union {
156 uint8_t byte;
157 uint16_t word;
158 uint32_t dword;
159 uint64_t qword;
160 } buf;
161 uint64_t data = 0;
162
163 if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
164 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
165 __func__, vbasedev->name, region->nr,
166 addr, size);
167 return (uint64_t)-1;
168 }
169 switch (size) {
170 case 1:
171 data = buf.byte;
172 break;
173 case 2:
174 data = le16_to_cpu(buf.word);
175 break;
176 case 4:
177 data = le32_to_cpu(buf.dword);
178 break;
179 case 8:
180 data = le64_to_cpu(buf.qword);
181 break;
182 default:
183 hw_error("vfio: unsupported read size, %d bytes", size);
184 break;
185 }
186
187 trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data);
188
189 /* Same as write above */
190 vbasedev->ops->vfio_eoi(vbasedev);
191
192 return data;
193 }
194
195 const MemoryRegionOps vfio_region_ops = {
196 .read = vfio_region_read,
197 .write = vfio_region_write,
198 .endianness = DEVICE_LITTLE_ENDIAN,
199 .valid = {
200 .min_access_size = 1,
201 .max_access_size = 8,
202 },
203 .impl = {
204 .min_access_size = 1,
205 .max_access_size = 8,
206 },
207 };
208
209 /*
210 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
211 */
212 static int vfio_dma_unmap(VFIOContainer *container,
213 hwaddr iova, ram_addr_t size)
214 {
215 struct vfio_iommu_type1_dma_unmap unmap = {
216 .argsz = sizeof(unmap),
217 .flags = 0,
218 .iova = iova,
219 .size = size,
220 };
221
222 if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
223 error_report("VFIO_UNMAP_DMA: %d", -errno);
224 return -errno;
225 }
226
227 return 0;
228 }
229
230 static int vfio_dma_map(VFIOContainer *container, hwaddr iova,
231 ram_addr_t size, void *vaddr, bool readonly)
232 {
233 struct vfio_iommu_type1_dma_map map = {
234 .argsz = sizeof(map),
235 .flags = VFIO_DMA_MAP_FLAG_READ,
236 .vaddr = (__u64)(uintptr_t)vaddr,
237 .iova = iova,
238 .size = size,
239 };
240
241 if (!readonly) {
242 map.flags |= VFIO_DMA_MAP_FLAG_WRITE;
243 }
244
245 /*
246 * Try the mapping, if it fails with EBUSY, unmap the region and try
247 * again. This shouldn't be necessary, but we sometimes see it in
248 * the VGA ROM space.
249 */
250 if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 ||
251 (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 &&
252 ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) {
253 return 0;
254 }
255
256 error_report("VFIO_MAP_DMA: %d", -errno);
257 return -errno;
258 }
259
260 static void vfio_host_win_add(VFIOContainer *container,
261 hwaddr min_iova, hwaddr max_iova,
262 uint64_t iova_pgsizes)
263 {
264 VFIOHostDMAWindow *hostwin;
265
266 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
267 if (ranges_overlap(hostwin->min_iova,
268 hostwin->max_iova - hostwin->min_iova + 1,
269 min_iova,
270 max_iova - min_iova + 1)) {
271 hw_error("%s: Overlapped IOMMU are not enabled", __func__);
272 }
273 }
274
275 hostwin = g_malloc0(sizeof(*hostwin));
276
277 hostwin->min_iova = min_iova;
278 hostwin->max_iova = max_iova;
279 hostwin->iova_pgsizes = iova_pgsizes;
280 QLIST_INSERT_HEAD(&container->hostwin_list, hostwin, hostwin_next);
281 }
282
283 static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova,
284 hwaddr max_iova)
285 {
286 VFIOHostDMAWindow *hostwin;
287
288 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
289 if (hostwin->min_iova == min_iova && hostwin->max_iova == max_iova) {
290 QLIST_REMOVE(hostwin, hostwin_next);
291 return 0;
292 }
293 }
294
295 return -1;
296 }
297
298 static bool vfio_listener_skipped_section(MemoryRegionSection *section)
299 {
300 return (!memory_region_is_ram(section->mr) &&
301 !memory_region_is_iommu(section->mr)) ||
302 /*
303 * Sizing an enabled 64-bit BAR can cause spurious mappings to
304 * addresses in the upper part of the 64-bit address space. These
305 * are never accessed by the CPU and beyond the address width of
306 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
307 */
308 section->offset_within_address_space & (1ULL << 63);
309 }
310
311 /* Called with rcu_read_lock held. */
312 static bool vfio_get_vaddr(IOMMUTLBEntry *iotlb, void **vaddr,
313 bool *read_only)
314 {
315 MemoryRegion *mr;
316 hwaddr xlat;
317 hwaddr len = iotlb->addr_mask + 1;
318 bool writable = iotlb->perm & IOMMU_WO;
319
320 /*
321 * The IOMMU TLB entry we have just covers translation through
322 * this IOMMU to its immediate target. We need to translate
323 * it the rest of the way through to memory.
324 */
325 mr = address_space_translate(&address_space_memory,
326 iotlb->translated_addr,
327 &xlat, &len, writable);
328 if (!memory_region_is_ram(mr)) {
329 error_report("iommu map to non memory area %"HWADDR_PRIx"",
330 xlat);
331 return false;
332 }
333
334 /*
335 * Translation truncates length to the IOMMU page size,
336 * check that it did not truncate too much.
337 */
338 if (len & iotlb->addr_mask) {
339 error_report("iommu has granularity incompatible with target AS");
340 return false;
341 }
342
343 *vaddr = memory_region_get_ram_ptr(mr) + xlat;
344 *read_only = !writable || mr->readonly;
345
346 return true;
347 }
348
349 static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
350 {
351 VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
352 VFIOContainer *container = giommu->container;
353 hwaddr iova = iotlb->iova + giommu->iommu_offset;
354 bool read_only;
355 void *vaddr;
356 int ret;
357
358 trace_vfio_iommu_map_notify(iotlb->perm == IOMMU_NONE ? "UNMAP" : "MAP",
359 iova, iova + iotlb->addr_mask);
360
361 if (iotlb->target_as != &address_space_memory) {
362 error_report("Wrong target AS \"%s\", only system memory is allowed",
363 iotlb->target_as->name ? iotlb->target_as->name : "none");
364 return;
365 }
366
367 rcu_read_lock();
368
369 if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
370 if (!vfio_get_vaddr(iotlb, &vaddr, &read_only)) {
371 goto out;
372 }
373 /*
374 * vaddr is only valid until rcu_read_unlock(). But after
375 * vfio_dma_map has set up the mapping the pages will be
376 * pinned by the kernel. This makes sure that the RAM backend
377 * of vaddr will always be there, even if the memory object is
378 * destroyed and its backing memory munmap-ed.
379 */
380 ret = vfio_dma_map(container, iova,
381 iotlb->addr_mask + 1, vaddr,
382 read_only);
383 if (ret) {
384 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
385 "0x%"HWADDR_PRIx", %p) = %d (%m)",
386 container, iova,
387 iotlb->addr_mask + 1, vaddr, ret);
388 }
389 } else {
390 ret = vfio_dma_unmap(container, iova, iotlb->addr_mask + 1);
391 if (ret) {
392 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
393 "0x%"HWADDR_PRIx") = %d (%m)",
394 container, iova,
395 iotlb->addr_mask + 1, ret);
396 }
397 }
398 out:
399 rcu_read_unlock();
400 }
401
402 static void vfio_listener_region_add(MemoryListener *listener,
403 MemoryRegionSection *section)
404 {
405 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
406 hwaddr iova, end;
407 Int128 llend, llsize;
408 void *vaddr;
409 int ret;
410 VFIOHostDMAWindow *hostwin;
411 bool hostwin_found;
412
413 if (vfio_listener_skipped_section(section)) {
414 trace_vfio_listener_region_add_skip(
415 section->offset_within_address_space,
416 section->offset_within_address_space +
417 int128_get64(int128_sub(section->size, int128_one())));
418 return;
419 }
420
421 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
422 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
423 error_report("%s received unaligned region", __func__);
424 return;
425 }
426
427 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
428 llend = int128_make64(section->offset_within_address_space);
429 llend = int128_add(llend, section->size);
430 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
431
432 if (int128_ge(int128_make64(iova), llend)) {
433 return;
434 }
435 end = int128_get64(int128_sub(llend, int128_one()));
436
437 if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
438 VFIOHostDMAWindow *hostwin;
439 hwaddr pgsize = 0;
440
441 /* For now intersections are not allowed, we may relax this later */
442 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
443 if (ranges_overlap(hostwin->min_iova,
444 hostwin->max_iova - hostwin->min_iova + 1,
445 section->offset_within_address_space,
446 int128_get64(section->size))) {
447 ret = -1;
448 goto fail;
449 }
450 }
451
452 ret = vfio_spapr_create_window(container, section, &pgsize);
453 if (ret) {
454 goto fail;
455 }
456
457 vfio_host_win_add(container, section->offset_within_address_space,
458 section->offset_within_address_space +
459 int128_get64(section->size) - 1, pgsize);
460 }
461
462 hostwin_found = false;
463 QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
464 if (hostwin->min_iova <= iova && end <= hostwin->max_iova) {
465 hostwin_found = true;
466 break;
467 }
468 }
469
470 if (!hostwin_found) {
471 error_report("vfio: IOMMU container %p can't map guest IOVA region"
472 " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx,
473 container, iova, end);
474 ret = -EFAULT;
475 goto fail;
476 }
477
478 memory_region_ref(section->mr);
479
480 if (memory_region_is_iommu(section->mr)) {
481 VFIOGuestIOMMU *giommu;
482
483 trace_vfio_listener_region_add_iommu(iova, end);
484 /*
485 * FIXME: For VFIO iommu types which have KVM acceleration to
486 * avoid bouncing all map/unmaps through qemu this way, this
487 * would be the right place to wire that up (tell the KVM
488 * device emulation the VFIO iommu handles to use).
489 */
490 giommu = g_malloc0(sizeof(*giommu));
491 giommu->iommu = section->mr;
492 giommu->iommu_offset = section->offset_within_address_space -
493 section->offset_within_region;
494 giommu->container = container;
495 llend = int128_add(int128_make64(section->offset_within_region),
496 section->size);
497 llend = int128_sub(llend, int128_one());
498 iommu_notifier_init(&giommu->n, vfio_iommu_map_notify,
499 IOMMU_NOTIFIER_ALL,
500 section->offset_within_region,
501 int128_get64(llend));
502 QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next);
503
504 memory_region_register_iommu_notifier(giommu->iommu, &giommu->n);
505 memory_region_iommu_replay(giommu->iommu, &giommu->n);
506
507 return;
508 }
509
510 /* Here we assume that memory_region_is_ram(section->mr)==true */
511
512 vaddr = memory_region_get_ram_ptr(section->mr) +
513 section->offset_within_region +
514 (iova - section->offset_within_address_space);
515
516 trace_vfio_listener_region_add_ram(iova, end, vaddr);
517
518 llsize = int128_sub(llend, int128_make64(iova));
519
520 ret = vfio_dma_map(container, iova, int128_get64(llsize),
521 vaddr, section->readonly);
522 if (ret) {
523 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "
524 "0x%"HWADDR_PRIx", %p) = %d (%m)",
525 container, iova, int128_get64(llsize), vaddr, ret);
526 goto fail;
527 }
528
529 return;
530
531 fail:
532 /*
533 * On the initfn path, store the first error in the container so we
534 * can gracefully fail. Runtime, there's not much we can do other
535 * than throw a hardware error.
536 */
537 if (!container->initialized) {
538 if (!container->error) {
539 container->error = ret;
540 }
541 } else {
542 hw_error("vfio: DMA mapping failed, unable to continue");
543 }
544 }
545
546 static void vfio_listener_region_del(MemoryListener *listener,
547 MemoryRegionSection *section)
548 {
549 VFIOContainer *container = container_of(listener, VFIOContainer, listener);
550 hwaddr iova, end;
551 Int128 llend, llsize;
552 int ret;
553
554 if (vfio_listener_skipped_section(section)) {
555 trace_vfio_listener_region_del_skip(
556 section->offset_within_address_space,
557 section->offset_within_address_space +
558 int128_get64(int128_sub(section->size, int128_one())));
559 return;
560 }
561
562 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
563 (section->offset_within_region & ~TARGET_PAGE_MASK))) {
564 error_report("%s received unaligned region", __func__);
565 return;
566 }
567
568 if (memory_region_is_iommu(section->mr)) {
569 VFIOGuestIOMMU *giommu;
570
571 QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
572 if (giommu->iommu == section->mr &&
573 giommu->n.start == section->offset_within_region) {
574 memory_region_unregister_iommu_notifier(giommu->iommu,
575 &giommu->n);
576 QLIST_REMOVE(giommu, giommu_next);
577 g_free(giommu);
578 break;
579 }
580 }
581
582 /*
583 * FIXME: We assume the one big unmap below is adequate to
584 * remove any individual page mappings in the IOMMU which
585 * might have been copied into VFIO. This works for a page table
586 * based IOMMU where a big unmap flattens a large range of IO-PTEs.
587 * That may not be true for all IOMMU types.
588 */
589 }
590
591 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
592 llend = int128_make64(section->offset_within_address_space);
593 llend = int128_add(llend, section->size);
594 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
595
596 if (int128_ge(int128_make64(iova), llend)) {
597 return;
598 }
599 end = int128_get64(int128_sub(llend, int128_one()));
600
601 llsize = int128_sub(llend, int128_make64(iova));
602
603 trace_vfio_listener_region_del(iova, end);
604
605 ret = vfio_dma_unmap(container, iova, int128_get64(llsize));
606 memory_region_unref(section->mr);
607 if (ret) {
608 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", "
609 "0x%"HWADDR_PRIx") = %d (%m)",
610 container, iova, int128_get64(llsize), ret);
611 }
612
613 if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
614 vfio_spapr_remove_window(container,
615 section->offset_within_address_space);
616 if (vfio_host_win_del(container,
617 section->offset_within_address_space,
618 section->offset_within_address_space +
619 int128_get64(section->size) - 1) < 0) {
620 hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx,
621 __func__, section->offset_within_address_space);
622 }
623 }
624 }
625
626 static const MemoryListener vfio_memory_listener = {
627 .region_add = vfio_listener_region_add,
628 .region_del = vfio_listener_region_del,
629 };
630
631 static void vfio_listener_release(VFIOContainer *container)
632 {
633 memory_listener_unregister(&container->listener);
634 if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
635 memory_listener_unregister(&container->prereg_listener);
636 }
637 }
638
639 static struct vfio_info_cap_header *
640 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id)
641 {
642 struct vfio_info_cap_header *hdr;
643 void *ptr = info;
644
645 if (!(info->flags & VFIO_REGION_INFO_FLAG_CAPS)) {
646 return NULL;
647 }
648
649 for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) {
650 if (hdr->id == id) {
651 return hdr;
652 }
653 }
654
655 return NULL;
656 }
657
658 static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
659 struct vfio_region_info *info)
660 {
661 struct vfio_info_cap_header *hdr;
662 struct vfio_region_info_cap_sparse_mmap *sparse;
663 int i, j;
664
665 hdr = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP);
666 if (!hdr) {
667 return -ENODEV;
668 }
669
670 sparse = container_of(hdr, struct vfio_region_info_cap_sparse_mmap, header);
671
672 trace_vfio_region_sparse_mmap_header(region->vbasedev->name,
673 region->nr, sparse->nr_areas);
674
675 region->mmaps = g_new0(VFIOMmap, sparse->nr_areas);
676
677 for (i = 0, j = 0; i < sparse->nr_areas; i++) {
678 trace_vfio_region_sparse_mmap_entry(i, sparse->areas[i].offset,
679 sparse->areas[i].offset +
680 sparse->areas[i].size);
681
682 if (sparse->areas[i].size) {
683 region->mmaps[j].offset = sparse->areas[i].offset;
684 region->mmaps[j].size = sparse->areas[i].size;
685 j++;
686 }
687 }
688
689 region->nr_mmaps = j;
690 region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap));
691
692 return 0;
693 }
694
695 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
696 int index, const char *name)
697 {
698 struct vfio_region_info *info;
699 int ret;
700
701 ret = vfio_get_region_info(vbasedev, index, &info);
702 if (ret) {
703 return ret;
704 }
705
706 region->vbasedev = vbasedev;
707 region->flags = info->flags;
708 region->size = info->size;
709 region->fd_offset = info->offset;
710 region->nr = index;
711
712 if (region->size) {
713 region->mem = g_new0(MemoryRegion, 1);
714 memory_region_init_io(region->mem, obj, &vfio_region_ops,
715 region, name, region->size);
716
717 if (!vbasedev->no_mmap &&
718 region->flags & VFIO_REGION_INFO_FLAG_MMAP) {
719
720 ret = vfio_setup_region_sparse_mmaps(region, info);
721
722 if (ret) {
723 region->nr_mmaps = 1;
724 region->mmaps = g_new0(VFIOMmap, region->nr_mmaps);
725 region->mmaps[0].offset = 0;
726 region->mmaps[0].size = region->size;
727 }
728 }
729 }
730
731 g_free(info);
732
733 trace_vfio_region_setup(vbasedev->name, index, name,
734 region->flags, region->fd_offset, region->size);
735 return 0;
736 }
737
738 int vfio_region_mmap(VFIORegion *region)
739 {
740 int i, prot = 0;
741 char *name;
742
743 if (!region->mem) {
744 return 0;
745 }
746
747 prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0;
748 prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0;
749
750 for (i = 0; i < region->nr_mmaps; i++) {
751 region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot,
752 MAP_SHARED, region->vbasedev->fd,
753 region->fd_offset +
754 region->mmaps[i].offset);
755 if (region->mmaps[i].mmap == MAP_FAILED) {
756 int ret = -errno;
757
758 trace_vfio_region_mmap_fault(memory_region_name(region->mem), i,
759 region->fd_offset +
760 region->mmaps[i].offset,
761 region->fd_offset +
762 region->mmaps[i].offset +
763 region->mmaps[i].size - 1, ret);
764
765 region->mmaps[i].mmap = NULL;
766
767 for (i--; i >= 0; i--) {
768 memory_region_del_subregion(region->mem, &region->mmaps[i].mem);
769 munmap(region->mmaps[i].mmap, region->mmaps[i].size);
770 object_unparent(OBJECT(&region->mmaps[i].mem));
771 region->mmaps[i].mmap = NULL;
772 }
773
774 return ret;
775 }
776
777 name = g_strdup_printf("%s mmaps[%d]",
778 memory_region_name(region->mem), i);
779 memory_region_init_ram_device_ptr(&region->mmaps[i].mem,
780 memory_region_owner(region->mem),
781 name, region->mmaps[i].size,
782 region->mmaps[i].mmap);
783 g_free(name);
784 memory_region_add_subregion(region->mem, region->mmaps[i].offset,
785 &region->mmaps[i].mem);
786
787 trace_vfio_region_mmap(memory_region_name(&region->mmaps[i].mem),
788 region->mmaps[i].offset,
789 region->mmaps[i].offset +
790 region->mmaps[i].size - 1);
791 }
792
793 return 0;
794 }
795
796 void vfio_region_exit(VFIORegion *region)
797 {
798 int i;
799
800 if (!region->mem) {
801 return;
802 }
803
804 for (i = 0; i < region->nr_mmaps; i++) {
805 if (region->mmaps[i].mmap) {
806 memory_region_del_subregion(region->mem, &region->mmaps[i].mem);
807 }
808 }
809
810 trace_vfio_region_exit(region->vbasedev->name, region->nr);
811 }
812
813 void vfio_region_finalize(VFIORegion *region)
814 {
815 int i;
816
817 if (!region->mem) {
818 return;
819 }
820
821 for (i = 0; i < region->nr_mmaps; i++) {
822 if (region->mmaps[i].mmap) {
823 munmap(region->mmaps[i].mmap, region->mmaps[i].size);
824 object_unparent(OBJECT(&region->mmaps[i].mem));
825 }
826 }
827
828 object_unparent(OBJECT(region->mem));
829
830 g_free(region->mem);
831 g_free(region->mmaps);
832
833 trace_vfio_region_finalize(region->vbasedev->name, region->nr);
834 }
835
836 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
837 {
838 int i;
839
840 if (!region->mem) {
841 return;
842 }
843
844 for (i = 0; i < region->nr_mmaps; i++) {
845 if (region->mmaps[i].mmap) {
846 memory_region_set_enabled(&region->mmaps[i].mem, enabled);
847 }
848 }
849
850 trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
851 enabled);
852 }
853
854 void vfio_reset_handler(void *opaque)
855 {
856 VFIOGroup *group;
857 VFIODevice *vbasedev;
858
859 QLIST_FOREACH(group, &vfio_group_list, next) {
860 QLIST_FOREACH(vbasedev, &group->device_list, next) {
861 if (vbasedev->dev->realized) {
862 vbasedev->ops->vfio_compute_needs_reset(vbasedev);
863 }
864 }
865 }
866
867 QLIST_FOREACH(group, &vfio_group_list, next) {
868 QLIST_FOREACH(vbasedev, &group->device_list, next) {
869 if (vbasedev->dev->realized && vbasedev->needs_reset) {
870 vbasedev->ops->vfio_hot_reset_multi(vbasedev);
871 }
872 }
873 }
874 }
875
876 static void vfio_kvm_device_add_group(VFIOGroup *group)
877 {
878 #ifdef CONFIG_KVM
879 struct kvm_device_attr attr = {
880 .group = KVM_DEV_VFIO_GROUP,
881 .attr = KVM_DEV_VFIO_GROUP_ADD,
882 .addr = (uint64_t)(unsigned long)&group->fd,
883 };
884
885 if (!kvm_enabled()) {
886 return;
887 }
888
889 if (vfio_kvm_device_fd < 0) {
890 struct kvm_create_device cd = {
891 .type = KVM_DEV_TYPE_VFIO,
892 };
893
894 if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {
895 error_report("Failed to create KVM VFIO device: %m");
896 return;
897 }
898
899 vfio_kvm_device_fd = cd.fd;
900 }
901
902 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
903 error_report("Failed to add group %d to KVM VFIO device: %m",
904 group->groupid);
905 }
906 #endif
907 }
908
909 static void vfio_kvm_device_del_group(VFIOGroup *group)
910 {
911 #ifdef CONFIG_KVM
912 struct kvm_device_attr attr = {
913 .group = KVM_DEV_VFIO_GROUP,
914 .attr = KVM_DEV_VFIO_GROUP_DEL,
915 .addr = (uint64_t)(unsigned long)&group->fd,
916 };
917
918 if (vfio_kvm_device_fd < 0) {
919 return;
920 }
921
922 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
923 error_report("Failed to remove group %d from KVM VFIO device: %m",
924 group->groupid);
925 }
926 #endif
927 }
928
929 static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as)
930 {
931 VFIOAddressSpace *space;
932
933 QLIST_FOREACH(space, &vfio_address_spaces, list) {
934 if (space->as == as) {
935 return space;
936 }
937 }
938
939 /* No suitable VFIOAddressSpace, create a new one */
940 space = g_malloc0(sizeof(*space));
941 space->as = as;
942 QLIST_INIT(&space->containers);
943
944 QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
945
946 return space;
947 }
948
949 static void vfio_put_address_space(VFIOAddressSpace *space)
950 {
951 if (QLIST_EMPTY(&space->containers)) {
952 QLIST_REMOVE(space, list);
953 g_free(space);
954 }
955 }
956
957 static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
958 Error **errp)
959 {
960 VFIOContainer *container;
961 int ret, fd;
962 VFIOAddressSpace *space;
963
964 space = vfio_get_address_space(as);
965
966 QLIST_FOREACH(container, &space->containers, next) {
967 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
968 group->container = container;
969 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
970 return 0;
971 }
972 }
973
974 fd = qemu_open("/dev/vfio/vfio", O_RDWR);
975 if (fd < 0) {
976 error_setg_errno(errp, errno, "failed to open /dev/vfio/vfio");
977 ret = -errno;
978 goto put_space_exit;
979 }
980
981 ret = ioctl(fd, VFIO_GET_API_VERSION);
982 if (ret != VFIO_API_VERSION) {
983 error_setg(errp, "supported vfio version: %d, "
984 "reported version: %d", VFIO_API_VERSION, ret);
985 ret = -EINVAL;
986 goto close_fd_exit;
987 }
988
989 container = g_malloc0(sizeof(*container));
990 container->space = space;
991 container->fd = fd;
992 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) ||
993 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) {
994 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU);
995 struct vfio_iommu_type1_info info;
996
997 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
998 if (ret) {
999 error_setg_errno(errp, errno, "failed to set group container");
1000 ret = -errno;
1001 goto free_container_exit;
1002 }
1003
1004 container->iommu_type = v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU;
1005 ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type);
1006 if (ret) {
1007 error_setg_errno(errp, errno, "failed to set iommu for container");
1008 ret = -errno;
1009 goto free_container_exit;
1010 }
1011
1012 /*
1013 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit
1014 * IOVA whatsoever. That's not actually true, but the current
1015 * kernel interface doesn't tell us what it can map, and the
1016 * existing Type1 IOMMUs generally support any IOVA we're
1017 * going to actually try in practice.
1018 */
1019 info.argsz = sizeof(info);
1020 ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info);
1021 /* Ignore errors */
1022 if (ret || !(info.flags & VFIO_IOMMU_INFO_PGSIZES)) {
1023 /* Assume 4k IOVA page size */
1024 info.iova_pgsizes = 4096;
1025 }
1026 vfio_host_win_add(container, 0, (hwaddr)-1, info.iova_pgsizes);
1027 } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU) ||
1028 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU)) {
1029 struct vfio_iommu_spapr_tce_info info;
1030 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU);
1031
1032 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
1033 if (ret) {
1034 error_setg_errno(errp, errno, "failed to set group container");
1035 ret = -errno;
1036 goto free_container_exit;
1037 }
1038 container->iommu_type =
1039 v2 ? VFIO_SPAPR_TCE_v2_IOMMU : VFIO_SPAPR_TCE_IOMMU;
1040 ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type);
1041 if (ret) {
1042 error_setg_errno(errp, errno, "failed to set iommu for container");
1043 ret = -errno;
1044 goto free_container_exit;
1045 }
1046
1047 /*
1048 * The host kernel code implementing VFIO_IOMMU_DISABLE is called
1049 * when container fd is closed so we do not call it explicitly
1050 * in this file.
1051 */
1052 if (!v2) {
1053 ret = ioctl(fd, VFIO_IOMMU_ENABLE);
1054 if (ret) {
1055 error_setg_errno(errp, errno, "failed to enable container");
1056 ret = -errno;
1057 goto free_container_exit;
1058 }
1059 } else {
1060 container->prereg_listener = vfio_prereg_listener;
1061
1062 memory_listener_register(&container->prereg_listener,
1063 &address_space_memory);
1064 if (container->error) {
1065 memory_listener_unregister(&container->prereg_listener);
1066 ret = container->error;
1067 error_setg(errp,
1068 "RAM memory listener initialization failed for container");
1069 goto free_container_exit;
1070 }
1071 }
1072
1073 info.argsz = sizeof(info);
1074 ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
1075 if (ret) {
1076 error_setg_errno(errp, errno,
1077 "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed");
1078 ret = -errno;
1079 if (v2) {
1080 memory_listener_unregister(&container->prereg_listener);
1081 }
1082 goto free_container_exit;
1083 }
1084
1085 if (v2) {
1086 /*
1087 * There is a default window in just created container.
1088 * To make region_add/del simpler, we better remove this
1089 * window now and let those iommu_listener callbacks
1090 * create/remove them when needed.
1091 */
1092 ret = vfio_spapr_remove_window(container, info.dma32_window_start);
1093 if (ret) {
1094 error_setg_errno(errp, -ret,
1095 "failed to remove existing window");
1096 goto free_container_exit;
1097 }
1098 } else {
1099 /* The default table uses 4K pages */
1100 vfio_host_win_add(container, info.dma32_window_start,
1101 info.dma32_window_start +
1102 info.dma32_window_size - 1,
1103 0x1000);
1104 }
1105 } else {
1106 error_setg(errp, "No available IOMMU models");
1107 ret = -EINVAL;
1108 goto free_container_exit;
1109 }
1110
1111 container->listener = vfio_memory_listener;
1112
1113 memory_listener_register(&container->listener, container->space->as);
1114
1115 if (container->error) {
1116 ret = container->error;
1117 error_setg_errno(errp, -ret,
1118 "memory listener initialization failed for container");
1119 goto listener_release_exit;
1120 }
1121
1122 container->initialized = true;
1123
1124 QLIST_INIT(&container->group_list);
1125 QLIST_INSERT_HEAD(&space->containers, container, next);
1126
1127 group->container = container;
1128 QLIST_INSERT_HEAD(&container->group_list, group, container_next);
1129
1130 return 0;
1131 listener_release_exit:
1132 vfio_listener_release(container);
1133
1134 free_container_exit:
1135 g_free(container);
1136
1137 close_fd_exit:
1138 close(fd);
1139
1140 put_space_exit:
1141 vfio_put_address_space(space);
1142
1143 return ret;
1144 }
1145
1146 static void vfio_disconnect_container(VFIOGroup *group)
1147 {
1148 VFIOContainer *container = group->container;
1149
1150 if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
1151 error_report("vfio: error disconnecting group %d from container",
1152 group->groupid);
1153 }
1154
1155 QLIST_REMOVE(group, container_next);
1156 group->container = NULL;
1157
1158 if (QLIST_EMPTY(&container->group_list)) {
1159 VFIOAddressSpace *space = container->space;
1160 VFIOGuestIOMMU *giommu, *tmp;
1161
1162 vfio_listener_release(container);
1163 QLIST_REMOVE(container, next);
1164
1165 QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) {
1166 memory_region_unregister_iommu_notifier(giommu->iommu, &giommu->n);
1167 QLIST_REMOVE(giommu, giommu_next);
1168 g_free(giommu);
1169 }
1170
1171 trace_vfio_disconnect_container(container->fd);
1172 close(container->fd);
1173 g_free(container);
1174
1175 vfio_put_address_space(space);
1176 }
1177 }
1178
1179 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
1180 {
1181 VFIOGroup *group;
1182 char path[32];
1183 struct vfio_group_status status = { .argsz = sizeof(status) };
1184
1185 QLIST_FOREACH(group, &vfio_group_list, next) {
1186 if (group->groupid == groupid) {
1187 /* Found it. Now is it already in the right context? */
1188 if (group->container->space->as == as) {
1189 return group;
1190 } else {
1191 error_setg(errp, "group %d used in multiple address spaces",
1192 group->groupid);
1193 return NULL;
1194 }
1195 }
1196 }
1197
1198 group = g_malloc0(sizeof(*group));
1199
1200 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
1201 group->fd = qemu_open(path, O_RDWR);
1202 if (group->fd < 0) {
1203 error_setg_errno(errp, errno, "failed to open %s", path);
1204 goto free_group_exit;
1205 }
1206
1207 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) {
1208 error_setg_errno(errp, errno, "failed to get group %d status", groupid);
1209 goto close_fd_exit;
1210 }
1211
1212 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
1213 error_setg(errp, "group %d is not viable", groupid);
1214 error_append_hint(errp,
1215 "Please ensure all devices within the iommu_group "
1216 "are bound to their vfio bus driver.\n");
1217 goto close_fd_exit;
1218 }
1219
1220 group->groupid = groupid;
1221 QLIST_INIT(&group->device_list);
1222
1223 if (vfio_connect_container(group, as, errp)) {
1224 error_prepend(errp, "failed to setup container for group %d: ",
1225 groupid);
1226 goto close_fd_exit;
1227 }
1228
1229 if (QLIST_EMPTY(&vfio_group_list)) {
1230 qemu_register_reset(vfio_reset_handler, NULL);
1231 }
1232
1233 QLIST_INSERT_HEAD(&vfio_group_list, group, next);
1234
1235 vfio_kvm_device_add_group(group);
1236
1237 return group;
1238
1239 close_fd_exit:
1240 close(group->fd);
1241
1242 free_group_exit:
1243 g_free(group);
1244
1245 return NULL;
1246 }
1247
1248 void vfio_put_group(VFIOGroup *group)
1249 {
1250 if (!group || !QLIST_EMPTY(&group->device_list)) {
1251 return;
1252 }
1253
1254 vfio_kvm_device_del_group(group);
1255 vfio_disconnect_container(group);
1256 QLIST_REMOVE(group, next);
1257 trace_vfio_put_group(group->fd);
1258 close(group->fd);
1259 g_free(group);
1260
1261 if (QLIST_EMPTY(&vfio_group_list)) {
1262 qemu_unregister_reset(vfio_reset_handler, NULL);
1263 }
1264 }
1265
1266 int vfio_get_device(VFIOGroup *group, const char *name,
1267 VFIODevice *vbasedev, Error **errp)
1268 {
1269 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
1270 int ret, fd;
1271
1272 fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
1273 if (fd < 0) {
1274 error_setg_errno(errp, errno, "error getting device from group %d",
1275 group->groupid);
1276 error_append_hint(errp,
1277 "Verify all devices in group %d are bound to vfio-<bus> "
1278 "or pci-stub and not already in use\n", group->groupid);
1279 return fd;
1280 }
1281
1282 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
1283 if (ret) {
1284 error_setg_errno(errp, errno, "error getting device info");
1285 close(fd);
1286 return ret;
1287 }
1288
1289 vbasedev->fd = fd;
1290 vbasedev->group = group;
1291 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
1292
1293 vbasedev->num_irqs = dev_info.num_irqs;
1294 vbasedev->num_regions = dev_info.num_regions;
1295 vbasedev->flags = dev_info.flags;
1296
1297 trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
1298 dev_info.num_irqs);
1299
1300 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
1301 return 0;
1302 }
1303
1304 void vfio_put_base_device(VFIODevice *vbasedev)
1305 {
1306 if (!vbasedev->group) {
1307 return;
1308 }
1309 QLIST_REMOVE(vbasedev, next);
1310 vbasedev->group = NULL;
1311 trace_vfio_put_base_device(vbasedev->fd);
1312 close(vbasedev->fd);
1313 }
1314
1315 int vfio_get_region_info(VFIODevice *vbasedev, int index,
1316 struct vfio_region_info **info)
1317 {
1318 size_t argsz = sizeof(struct vfio_region_info);
1319
1320 *info = g_malloc0(argsz);
1321
1322 (*info)->index = index;
1323 retry:
1324 (*info)->argsz = argsz;
1325
1326 if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, *info)) {
1327 g_free(*info);
1328 *info = NULL;
1329 return -errno;
1330 }
1331
1332 if ((*info)->argsz > argsz) {
1333 argsz = (*info)->argsz;
1334 *info = g_realloc(*info, argsz);
1335
1336 goto retry;
1337 }
1338
1339 return 0;
1340 }
1341
1342 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
1343 uint32_t subtype, struct vfio_region_info **info)
1344 {
1345 int i;
1346
1347 for (i = 0; i < vbasedev->num_regions; i++) {
1348 struct vfio_info_cap_header *hdr;
1349 struct vfio_region_info_cap_type *cap_type;
1350
1351 if (vfio_get_region_info(vbasedev, i, info)) {
1352 continue;
1353 }
1354
1355 hdr = vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE);
1356 if (!hdr) {
1357 g_free(*info);
1358 continue;
1359 }
1360
1361 cap_type = container_of(hdr, struct vfio_region_info_cap_type, header);
1362
1363 trace_vfio_get_dev_region(vbasedev->name, i,
1364 cap_type->type, cap_type->subtype);
1365
1366 if (cap_type->type == type && cap_type->subtype == subtype) {
1367 return 0;
1368 }
1369
1370 g_free(*info);
1371 }
1372
1373 *info = NULL;
1374 return -ENODEV;
1375 }
1376
1377 /*
1378 * Interfaces for IBM EEH (Enhanced Error Handling)
1379 */
1380 static bool vfio_eeh_container_ok(VFIOContainer *container)
1381 {
1382 /*
1383 * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO
1384 * implementation is broken if there are multiple groups in a
1385 * container. The hardware works in units of Partitionable
1386 * Endpoints (== IOMMU groups) and the EEH operations naively
1387 * iterate across all groups in the container, without any logic
1388 * to make sure the groups have their state synchronized. For
1389 * certain operations (ENABLE) that might be ok, until an error
1390 * occurs, but for others (GET_STATE) it's clearly broken.
1391 */
1392
1393 /*
1394 * XXX Once fixed kernels exist, test for them here
1395 */
1396
1397 if (QLIST_EMPTY(&container->group_list)) {
1398 return false;
1399 }
1400
1401 if (QLIST_NEXT(QLIST_FIRST(&container->group_list), container_next)) {
1402 return false;
1403 }
1404
1405 return true;
1406 }
1407
1408 static int vfio_eeh_container_op(VFIOContainer *container, uint32_t op)
1409 {
1410 struct vfio_eeh_pe_op pe_op = {
1411 .argsz = sizeof(pe_op),
1412 .op = op,
1413 };
1414 int ret;
1415
1416 if (!vfio_eeh_container_ok(container)) {
1417 error_report("vfio/eeh: EEH_PE_OP 0x%x: "
1418 "kernel requires a container with exactly one group", op);
1419 return -EPERM;
1420 }
1421
1422 ret = ioctl(container->fd, VFIO_EEH_PE_OP, &pe_op);
1423 if (ret < 0) {
1424 error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op);
1425 return -errno;
1426 }
1427
1428 return ret;
1429 }
1430
1431 static VFIOContainer *vfio_eeh_as_container(AddressSpace *as)
1432 {
1433 VFIOAddressSpace *space = vfio_get_address_space(as);
1434 VFIOContainer *container = NULL;
1435
1436 if (QLIST_EMPTY(&space->containers)) {
1437 /* No containers to act on */
1438 goto out;
1439 }
1440
1441 container = QLIST_FIRST(&space->containers);
1442
1443 if (QLIST_NEXT(container, next)) {
1444 /* We don't yet have logic to synchronize EEH state across
1445 * multiple containers */
1446 container = NULL;
1447 goto out;
1448 }
1449
1450 out:
1451 vfio_put_address_space(space);
1452 return container;
1453 }
1454
1455 bool vfio_eeh_as_ok(AddressSpace *as)
1456 {
1457 VFIOContainer *container = vfio_eeh_as_container(as);
1458
1459 return (container != NULL) && vfio_eeh_container_ok(container);
1460 }
1461
1462 int vfio_eeh_as_op(AddressSpace *as, uint32_t op)
1463 {
1464 VFIOContainer *container = vfio_eeh_as_container(as);
1465
1466 if (!container) {
1467 return -ENODEV;
1468 }
1469 return vfio_eeh_container_op(container, op);
1470 }