hw/vfio/spapr.c: extract vfio_spapr_kvm_attach_tce to hw/vfio/kvm-spapr.c
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é <philmd@linaro.org> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org> Reviewed-by: Cédric Le Goater <clg@redhat.com> Tested-by: Cédric Le Goater <clg@redhat.com> Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org> Link: https://lore.kernel.org/qemu-devel/20260318174733.1717643-8-pierrick.bouvier@linaro.org Signed-off-by: Cédric Le Goater <clg@redhat.com>
Pierrick Bouvier committed
Mar 18, 2026 at 10:47 UTC
5bf4c7c834ed9aad0a1d450eb7e28364d5c2dab6
5 files changed
+72
-26
hw/vfio/kvm-spapr.c
new
+47
@@ -0,0 +1,47 @@
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)¶m,
30
+ };
31
+
32
+ if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD,
33
+ ¶m.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
+}
hw/vfio/kvm-spapr.h
new
+12
@@ -0,0 +1,12 @@
1
+/*
2
+ * VFIO sPAPR KVM specific functions
3
+ *
4
+ * SPDX-License-Identifier: GPL-2.0-or-later
5
+ */
6
+
7
+#include "hw/vfio/vfio-container.h"
8
+#include "qapi/error.h"
9
+
10
+bool vfio_spapr_kvm_attach_tce(VFIOContainer *bcontainer,
11
+ MemoryRegionSection *section,
12
+ Error **errp);
hw/vfio/kvm-stubs.c
+8
@@ -6,6 +6,7 @@
6
7
#include "qemu/osdep.h"
8
9
+#include "hw/vfio/kvm-spapr.h"
10
#include "hw/vfio/vfio-device.h"
11
#include "qapi/error.h"
12
#include "vfio-helpers.h"
@@ -24,3 +25,10 @@ int vfio_kvm_device_del_fd(int fd, Error **errp)
25
{
26
return 0;
27
}
28
+
29
+bool vfio_spapr_kvm_attach_tce(VFIOContainer *bcontainer,
30
+ MemoryRegionSection *section,
31
+ Error **errp)
32
+{
33
+ g_assert_not_reached();
34
+}
hw/vfio/meson.build
+1
@@ -10,6 +10,7 @@ vfio_ss.add(files(
10
vfio_ss.add(when: 'CONFIG_KVM', if_true: files('kvm-helpers.c'))
11
stub_ss.add(files('kvm-stubs.c'))
12
vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))
13
+vfio_ss.add(when: ['CONFIG_KVM', 'CONFIG_PSERIES'], if_true: files('kvm-spapr.c'))
14
vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
15
'pci-quirks.c',
16
'pci.c',
hw/vfio/spapr.c
+4
-26
@@ -16,6 +16,7 @@
16
#include "system/address-spaces.h"
17
18
#include "hw/vfio/vfio-container-legacy.h"
19
+#include "hw/vfio/kvm-spapr.h"
20
#include "hw/core/hw-error.h"
21
#include "qemu/error-report.h"
22
#include "qapi/error.h"
@@ -406,33 +407,10 @@ vfio_spapr_container_add_section_window(VFIOContainer *bcontainer,
407
vfio_host_win_add(scontainer, section->offset_within_address_space,
408
section->offset_within_address_space +
409
int128_get64(section->size) - 1, pgsize);
409
-#ifdef CONFIG_KVM
410
- if (kvm_enabled()) {
411
- VFIOGroup *group;
412
- IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
413
- struct kvm_vfio_spapr_tce param;
414
- struct kvm_device_attr attr = {
415
- .group = KVM_DEV_VFIO_GROUP,
416
- .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
417
- .addr = (uint64_t)(unsigned long)¶m,
418
- };
419
-
420
- if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD,
421
- ¶m.tablefd)) {
422
- QLIST_FOREACH(group, &container->group_list, container_next) {
423
- param.groupfd = group->fd;
424
- if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
425
- error_setg_errno(errp, errno,
426
- "vfio: failed GROUP_SET_SPAPR_TCE for "
427
- "KVM VFIO device %d and group fd %d",
428
- param.tablefd, param.groupfd);
429
- return false;
430
- }
431
- trace_vfio_spapr_group_attach(param.groupfd, param.tablefd);
432
- }
433
- }
410
+ if (kvm_enabled() && !vfio_spapr_kvm_attach_tce(bcontainer, section, errp)) {
411
+ return false;
412
}
435
-#endif
413
+
414
return true;
415
}
416