[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 09/19] vfio/iommufd: Implement HostIOMMUDeviceClass::realize()
From: |
Zhenzhong Duan |
Subject: |
[PATCH v3 09/19] vfio/iommufd: Implement HostIOMMUDeviceClass::realize() handler |
Date: |
Mon, 29 Apr 2024 14:50:36 +0800 |
It calls iommufd_backend_get_device_info() to get host IOMMU
related information and translate it into HostIOMMUDeviceCaps
for query with .check_cap().
Introduce macro VTD_MGAW_FROM_CAP to get MGAW which equals to
(aw_bits - 1).
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
include/hw/i386/intel_iommu.h | 1 +
hw/vfio/iommufd.c | 44 +++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h
index 7fa0a695c8..7d694b0813 100644
--- a/include/hw/i386/intel_iommu.h
+++ b/include/hw/i386/intel_iommu.h
@@ -47,6 +47,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState,
INTEL_IOMMU_DEVICE)
#define VTD_HOST_AW_48BIT 48
#define VTD_HOST_ADDRESS_WIDTH VTD_HOST_AW_39BIT
#define VTD_HAW_MASK(aw) ((1ULL << (aw)) - 1)
+#define VTD_MGAW_FROM_CAP(cap) ((cap >> 16) & 0x3fULL)
#define DMAR_REPORT_F_INTR (1)
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index 997f4ac43e..6bc2dc68f6 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -25,6 +25,7 @@
#include "qemu/cutils.h"
#include "qemu/chardev_open.h"
#include "pci.h"
+#include "hw/i386/intel_iommu_internal.h"
static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
ram_addr_t size, void *vaddr, bool readonly)
@@ -634,6 +635,48 @@ static void vfio_iommu_iommufd_class_init(ObjectClass
*klass, void *data)
vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
};
+static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
+ Error **errp)
+{
+ VFIODevice *vdev = opaque;
+ HostIOMMUDeviceIOMMUFD *idev = HOST_IOMMU_DEVICE_IOMMUFD(hiod);
+ HostIOMMUDeviceCaps *caps = &hiod->caps;
+ enum iommu_hw_info_type type;
+ union {
+ struct iommu_hw_info_vtd vtd;
+ } data;
+ int ret;
+
+ HOST_IOMMU_DEVICE_IOMMUFD_VFIO(hiod)->vdev = vdev;
+ idev->iommufd = vdev->iommufd;
+ idev->devid = vdev->devid;
+
+ ret = iommufd_backend_get_device_info(idev->iommufd, idev->devid,
+ &type, &data, sizeof(data), errp);
+ if (ret) {
+ return false;
+ }
+
+ caps->type = type;
+
+ switch (type) {
+ case IOMMU_HW_INFO_TYPE_INTEL_VTD:
+ caps->aw_bits = VTD_MGAW_FROM_CAP(data.vtd.cap_reg) + 1;
+ break;
+ case IOMMU_HW_INFO_TYPE_NONE:
+ break;
+ }
+
+ return true;
+}
+
+static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
+{
+ HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+ hiodc->realize = hiod_iommufd_vfio_realize;
+};
+
static const TypeInfo types[] = {
{
.name = TYPE_VFIO_IOMMU_IOMMUFD,
@@ -643,6 +686,7 @@ static const TypeInfo types[] = {
.name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO,
.parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
.instance_size = sizeof(HostIOMMUDeviceIOMMUFDVFIO),
+ .class_init = hiod_iommufd_vfio_class_init,
}
};
--
2.34.1
- Re: [PATCH v3 05/19] backends/host_iommu_device: Introduce HostIOMMUDeviceCaps, (continued)
- [PATCH v3 06/19] range: Introduce range_get_last_bit(), Zhenzhong Duan, 2024/04/29
- [PATCH v3 07/19] vfio/container: Implement HostIOMMUDeviceClass::realize() handler, Zhenzhong Duan, 2024/04/29
- [PATCH v3 08/19] backends/iommufd: Introduce helper function iommufd_backend_get_device_info(), Zhenzhong Duan, 2024/04/29
- [PATCH v3 09/19] vfio/iommufd: Implement HostIOMMUDeviceClass::realize() handler,
Zhenzhong Duan <=
- [PATCH v3 12/19] vfio: Introduce VFIOIOMMUClass::hiod_typename attribute, Zhenzhong Duan, 2024/04/29
- [PATCH v3 11/19] backends/iommufd: Implement HostIOMMUDeviceClass::check_cap() handler, Zhenzhong Duan, 2024/04/29
- [PATCH v3 10/19] vfio/container: Implement HostIOMMUDeviceClass::check_cap() handler, Zhenzhong Duan, 2024/04/29
- [PATCH v3 13/19] vfio: Create host IOMMU device instance, Zhenzhong Duan, 2024/04/29