master
c 47 lines 1.51 KB
Raw
1 /*
2 * VFIO sPAPR KVM specific functions
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "qemu/osdep.h"
8 #include <sys/ioctl.h>
9 #include <linux/vfio.h>
10 #include <linux/kvm.h>
11
12 #include "hw/vfio/vfio-container-legacy.h"
13 #include "hw/vfio/kvm-spapr.h"
14 #include "qapi/error.h"
15 #include "trace.h"
16 #include "vfio-helpers.h"
17
18 bool vfio_spapr_kvm_attach_tce(VFIOContainer *bcontainer,
19 MemoryRegionSection *section,
20 Error **errp)
21 {
22 VFIOLegacyContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
23 VFIOGroup *group;
24 IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
25 struct kvm_vfio_spapr_tce param;
26 struct kvm_device_attr attr = {
27 .group = KVM_DEV_VFIO_GROUP,
28 .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
29 .addr = (uint64_t)(unsigned long)&param,
30 };
31
32 if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD,
33 &param.tablefd)) {
34 QLIST_FOREACH(group, &container->group_list, container_next) {
35 param.groupfd = group->fd;
36 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
37 error_setg_errno(errp, errno,
38 "vfio: failed GROUP_SET_SPAPR_TCE for "
39 "KVM VFIO device %d and group fd %d",
40 param.tablefd, param.groupfd);
41 return false;
42 }
43 trace_vfio_spapr_group_attach(param.groupfd, param.tablefd);
44 }
45 }
46 return true;
47 }