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