Since this function needs kvm specific types, we need to extract in another file and link it only for KVM builds.
Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Pierrick Bouvier <[email protected]> --- hw/vfio/kvm-spapr.h | 12 ++++++++++++ hw/vfio/kvm-spapr.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/kvm-stubs.c | 8 ++++++++ hw/vfio/spapr.c | 30 ++++------------------------- hw/vfio/meson.build | 1 + 5 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 hw/vfio/kvm-spapr.h create mode 100644 hw/vfio/kvm-spapr.c diff --git a/hw/vfio/kvm-spapr.h b/hw/vfio/kvm-spapr.h new file mode 100644 index 00000000000..b1f68c686a7 --- /dev/null +++ b/hw/vfio/kvm-spapr.h @@ -0,0 +1,12 @@ +/* + * VFIO sPAPR KVM specific functions + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "hw/vfio/vfio-container.h" +#include "qapi/error.h" + +bool vfio_spapr_kvm_attach_tce(VFIOContainer *bcontainer, + MemoryRegionSection *section, + Error **errp); diff --git a/hw/vfio/kvm-spapr.c b/hw/vfio/kvm-spapr.c new file mode 100644 index 00000000000..ad71c5a85e2 --- /dev/null +++ b/hw/vfio/kvm-spapr.c @@ -0,0 +1,47 @@ +/* + * VFIO sPAPR KVM specific functions + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include <sys/ioctl.h> +#include <linux/vfio.h> +#include <linux/kvm.h> + +#include "hw/vfio/vfio-container-legacy.h" +#include "hw/vfio/kvm-spapr.h" +#include "qapi/error.h" +#include "trace.h" +#include "vfio-helpers.h" + +bool vfio_spapr_kvm_attach_tce(VFIOContainer *bcontainer, + MemoryRegionSection *section, + Error **errp) +{ + VFIOLegacyContainer *container = VFIO_IOMMU_LEGACY(bcontainer); + VFIOGroup *group; + IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); + struct kvm_vfio_spapr_tce param; + struct kvm_device_attr attr = { + .group = KVM_DEV_VFIO_GROUP, + .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE, + .addr = (uint64_t)(unsigned long)¶m, + }; + + if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD, + ¶m.tablefd)) { + QLIST_FOREACH(group, &container->group_list, container_next) { + param.groupfd = group->fd; + if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { + error_setg_errno(errp, errno, + "vfio: failed GROUP_SET_SPAPR_TCE for " + "KVM VFIO device %d and group fd %d", + param.tablefd, param.groupfd); + return false; + } + trace_vfio_spapr_group_attach(param.groupfd, param.tablefd); + } + } + return true; +} diff --git a/hw/vfio/kvm-stubs.c b/hw/vfio/kvm-stubs.c index 5a489d1b711..78c51b99155 100644 --- a/hw/vfio/kvm-stubs.c +++ b/hw/vfio/kvm-stubs.c @@ -6,6 +6,7 @@ #include "qemu/osdep.h" +#include "hw/vfio/kvm-spapr.h" #include "hw/vfio/vfio-device.h" #include "qapi/error.h" #include "vfio-helpers.h" @@ -24,3 +25,10 @@ int vfio_kvm_device_del_fd(int fd, Error **errp) { return 0; } + +bool vfio_spapr_kvm_attach_tce(VFIOContainer *bcontainer, + MemoryRegionSection *section, + Error **errp) +{ + g_assert_not_reached(); +} diff --git a/hw/vfio/spapr.c b/hw/vfio/spapr.c index a9f093c3570..42690e4323d 100644 --- a/hw/vfio/spapr.c +++ b/hw/vfio/spapr.c @@ -16,6 +16,7 @@ #include "system/address-spaces.h" #include "hw/vfio/vfio-container-legacy.h" +#include "hw/vfio/kvm-spapr.h" #include "hw/core/hw-error.h" #include "qemu/error-report.h" #include "qapi/error.h" @@ -406,33 +407,10 @@ vfio_spapr_container_add_section_window(VFIOContainer *bcontainer, vfio_host_win_add(scontainer, section->offset_within_address_space, section->offset_within_address_space + int128_get64(section->size) - 1, pgsize); -#ifdef CONFIG_KVM - if (kvm_enabled()) { - VFIOGroup *group; - IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - struct kvm_vfio_spapr_tce param; - struct kvm_device_attr attr = { - .group = KVM_DEV_VFIO_GROUP, - .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE, - .addr = (uint64_t)(unsigned long)¶m, - }; - - if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD, - ¶m.tablefd)) { - QLIST_FOREACH(group, &container->group_list, container_next) { - param.groupfd = group->fd; - if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { - error_setg_errno(errp, errno, - "vfio: failed GROUP_SET_SPAPR_TCE for " - "KVM VFIO device %d and group fd %d", - param.tablefd, param.groupfd); - return false; - } - trace_vfio_spapr_group_attach(param.groupfd, param.tablefd); - } - } + if (kvm_enabled() && !vfio_spapr_kvm_attach_tce(bcontainer, section, errp)) { + return false; } -#endif + return true; } diff --git a/hw/vfio/meson.build b/hw/vfio/meson.build index 6c00a7f51bb..bab5f2b7f15 100644 --- a/hw/vfio/meson.build +++ b/hw/vfio/meson.build @@ -10,6 +10,7 @@ vfio_ss.add(files( vfio_ss.add(when: 'CONFIG_KVM', if_true: files('kvm-helpers.c')) stub_ss.add(files('kvm-stubs.c')) vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c')) +vfio_ss.add(when: ['CONFIG_KVM', 'CONFIG_PSERIES'], if_true: files('kvm-spapr.c')) vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( 'pci-quirks.c', 'pci.c', -- 2.47.3
