@@ -4450,3 +4450,44 @@ int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
return vfio_container_do_ioctl(as, groupid, req, param);
}
+
+int vfio_container_spapr_set_liobn(AddressSpace *as,
+ int32_t groupid,
+ uint64_t liobn,
+ uint64_t start_addr)
+{
+#ifdef CONFIG_KVM
+ VFIOGroup *group;
+ int ret;
+ struct kvm_vfio_spapr_tce_liobn param = {
+ .argsz = sizeof(param),
+ .liobn = liobn,
+ .start_addr = start_addr
+ };
+ struct kvm_device_attr attr = {
+ .group = KVM_DEV_VFIO_GROUP,
+ .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE_LIOBN,
+ .addr = (uint64_t)(unsigned long)¶m,
+ };
+
+ if (vfio_kvm_device_fd < 0) {
+ return 0;
+ }
+
+ group = vfio_get_group(groupid, as);
+ if (!group) {
+ return -1;
+ }
+
+ param.fd = group->fd;
+ ret = ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr);
+ if (ret) {
+ error_report("vfio: failed to setup liobn for a group: %s",
+ strerror(errno));
+ }
+
+ return ret;
+#else
+ return 0;
+#endif
+}
@@ -126,6 +126,7 @@ static int spapr_tce_table_realize(DeviceState *dev)
if (!tcet->table) {
size_t table_size = tcet->nb_table * sizeof(uint64_t);
tcet->table = g_malloc0(table_size);
+ tcet->vfio_accel = false;
}
trace_spapr_iommu_new_table(tcet->liobn, tcet, tcet->table, tcet->fd);
@@ -21,6 +21,7 @@
#include "hw/pci-host/spapr.h"
#include "linux/vfio.h"
#include "hw/misc/vfio.h"
+#include "qemu/error-report.h"
static Property spapr_phb_vfio_properties[] = {
DEFINE_PROP_INT32("iommu", sPAPRPHBVFIOState, iommugroupid, -1),
@@ -69,6 +70,17 @@ static void spapr_phb_vfio_finish_realize(sPAPRPHBState *sphb, Error **errp)
/* Register default 32bit DMA window */
memory_region_add_subregion(&sphb->iommu_root, tcet->bus_offset,
spapr_tce_get_iommu(tcet));
+
+ if (!tcet->vfio_accel) {
+ return;
+ }
+ ret = vfio_container_spapr_set_liobn(&svphb->phb.iommu_as,
+ svphb->iommugroupid,
+ tcet->liobn,
+ tcet->bus_offset);
+ if (ret) {
+ error_report("spapr-vfio: failed to create link to IOMMU");
+ }
}
static int spapr_pci_vfio_ddw_query(sPAPRPHBState *sphb,
@@ -116,6 +128,16 @@ static int spapr_pci_vfio_ddw_create(sPAPRPHBState *sphb, uint32_t page_shift,
memory_region_add_subregion(&sphb->iommu_root, (*ptcet)->bus_offset,
spapr_tce_get_iommu(*ptcet));
+ if (!(*ptcet)->vfio_accel) {
+ return 0;
+ }
+ ret = vfio_container_spapr_set_liobn(&sphb->iommu_as, svphb->iommugroupid,
+ liobn, (*ptcet)->bus_offset);
+ if (ret) {
+ error_report("spapr-vfio: failed to create link to IOMMU");
+ ret = 0;
+ }
+
return ret;
}
@@ -6,4 +6,9 @@
extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
int req, void *param);
+extern int vfio_container_spapr_set_liobn(AddressSpace *as,
+ int32_t groupid,
+ uint64_t liobn,
+ uint64_t start_addr);
+
#endif
TCE hypercalls (H_PUT_TCE, H_PUT_TCE_INDIRECT, H_STUFF_TCE) use a logical bus number (LIOBN) to identify which TCE table the request is addressed to. However VFIO kernel driver operates with IOMMU group IDs and has no idea about which LIOBN corresponds to which group. If the host kernel supports in-kernel acceleration for TCE calls, we have to provide the LIOBN to IOMMU mapping information. This makes use of a VFIO KVM device's KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE_LIOBN attribute to set the link between LIOBN and IOMMU group. The vfio_container_spapr_set_liobn() helper is implemented completely in vfio.c because kvm_vfio_spapr_tce_liobn needs a group fd and we do not want to share resources likes that outside vfio.c. Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru> --- hw/misc/vfio.c | 41 +++++++++++++++++++++++++++++++++++++++++ hw/ppc/spapr_iommu.c | 1 + hw/ppc/spapr_pci_vfio.c | 22 ++++++++++++++++++++++ include/hw/misc/vfio.h | 5 +++++ 4 files changed, 69 insertions(+)