When vIOMMU is configured x-flts=on in scalable mode, stage-1 page table
is passed to host to construct nested page table. We need to check
compatibility of some critical IOMMU capabilities between vIOMMU and
host IOMMU to ensure guest stage-1 page table could be used by host.
For instance, vIOMMU supports stage-1 1GB huge page mapping, but host
does not, then this IOMMUFD backed device should be failed.
Declare an enum type host_iommu_device_iommu_hw_info_type aliased to
iommu_hw_info_type which comes from iommufd header file. This can avoid
build failure on windows which doesn't support iommufd.
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
include/system/host_iommu_device.h | 13 +++++++++++
hw/i386/intel_iommu.c | 36 ++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/include/system/host_iommu_device.h b/include/system/host_iommu_device.h
index 30da88789d..38070aff09 100644
--- a/include/system/host_iommu_device.h
+++ b/include/system/host_iommu_device.h
@@ -125,5 +125,18 @@ struct HostIOMMUDeviceClass {
#define HOST_IOMMU_DEVICE_CAP_FS1GP 3
#define HOST_IOMMU_DEVICE_CAP_ERRATA 4
+/**
+ * enum host_iommu_device_iommu_hw_info_type - IOMMU Hardware Info Types
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that do not
+ * report hardware info
+ * @HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type
+ *
+ * This is alias to enum iommu_hw_info_type but for general purpose.
+ */
+enum host_iommu_device_iommu_hw_info_type {
+ HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_NONE,
+ HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD,
+};
+
#define HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX 64
#endif
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index a2f3250724..dc839037cf 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -39,6 +39,7 @@
#include "kvm/kvm_i386.h"
#include "migration/vmstate.h"
#include "trace.h"
+#include "system/iommufd.h"
/* context entry operations */
#define VTD_CE_GET_RID2PASID(ce) \
@@ -4361,6 +4362,41 @@ static bool vtd_check_hiod(IntelIOMMUState *s, HostIOMMUDevice *hiod,
return true;
}
+ /* Remaining checks are all stage-1 translation specific */
+ if (!object_dynamic_cast(OBJECT(hiod), TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+ error_setg(errp, "Need IOMMUFD backend when x-flts=on");
+ return false;
+ }
+
+ /*
+ * HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE should be supported by different
+ * backend devices, either VFIO or VDPA.
+ */
+ ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE, errp);
+ assert(ret >= 0);
+ if (ret != HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+ error_setg(errp, "Incompatible host platform IOMMU type %d", ret);
+ return false;
+ }
+
+ /*
+ * HOST_IOMMU_DEVICE_CAP_NESTING/FS1GP are VTD vendor specific
+ * capabilities, so get_cap() should never fail on them now that
+ * HOST_IOMMU_DEVICE_IOMMU_HW_INFO_TYPE_INTEL_VTD type check passed
+ * above.
+ */
+ ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_NESTING, errp);
+ if (ret != 1) {
+ error_setg(errp, "Host IOMMU doesn't support nested translation");
+ return false;
+ }
+
+ ret = hiodc->get_cap(hiod, HOST_IOMMU_DEVICE_CAP_FS1GP, errp);
+ if (s->fs1gp && ret != 1) {
+ error_setg(errp, "Stage-1 1GB huge page is unsupported by host IOMMU");
+ return false;
+ }
+
error_setg(errp, "host device is uncompatible with stage-1 translation");
return false;
}
--
2.34.1