qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zhenzhong Duan <zhenzhong.duan@intel.com>
To: qemu-devel@nongnu.org
Cc: alex.williamson@redhat.com, clg@redhat.com,
	eric.auger@redhat.com, nicolinc@nvidia.com,
	joao.m.martins@oracle.com, chao.p.peng@intel.com,
	Zhenzhong Duan <zhenzhong.duan@intel.com>,
	Yi Liu <yi.l.liu@intel.com>
Subject: [PATCH 3/5] vfio/iommufd: Implement .get_cap() in TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO sub-class
Date: Fri, 11 Apr 2025 18:17:05 +0800	[thread overview]
Message-ID: <20250411101707.3460429-4-zhenzhong.duan@intel.com> (raw)
In-Reply-To: <20250411101707.3460429-1-zhenzhong.duan@intel.com>

Now we have saved a copy of host iommu capabilities in VFIODevice, implemented
hiod_iommufd_vfio_get_cap() by querying the caps copy in sub-class. This
overrides .get_cap() implementation hiod_iommufd_vfio_get_cap() in
TYPE_HOST_IOMMU_DEVICE_IOMMUFD parent class.

Vendor caps are checked for a specific capability, e.g., for vtd, checking
code will be in hiod_iommufd_get_vtd_cap().

This also fixes an issue that calling vfio_device_get_aw_bits() in
TYPE_HOST_IOMMU_DEVICE_IOMMUFD parent class .get_cap().

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 include/system/iommufd.h |  4 ++++
 backends/iommufd.c       | 40 ++++++++++++++++++++++++++++++++++++++++
 hw/vfio/iommufd.c        | 16 ++++++++++++++++
 3 files changed, 60 insertions(+)

diff --git a/include/system/iommufd.h b/include/system/iommufd.h
index 0f337585c9..baba5ec1d8 100644
--- a/include/system/iommufd.h
+++ b/include/system/iommufd.h
@@ -85,4 +85,8 @@ typedef struct HostIOMMUDeviceIOMMUFDCaps {
     uint64_t hw_caps;
     VendorCaps vendor_caps;
 } HostIOMMUDeviceIOMMUFDCaps;
+
+int hiod_iommufd_get_common_cap(HostIOMMUDevice *hiod,
+                                HostIOMMUDeviceIOMMUFDCaps *caps,
+                                int cap, Error **errp);
 #endif
diff --git a/backends/iommufd.c b/backends/iommufd.c
index 9587e4d99b..54fa3174d0 100644
--- a/backends/iommufd.c
+++ b/backends/iommufd.c
@@ -355,3 +355,43 @@ static const TypeInfo types[] = {
 };
 
 DEFINE_TYPES(types)
+
+static int hiod_iommufd_get_vtd_cap(HostIOMMUDevice *hiod,
+                                    struct iommu_hw_info_vtd *vtd,
+                                    int cap, Error **errp)
+{
+    /* TODO: Check vtd->cap_reg/ecap_reg for capability */
+    error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
+    return -EINVAL;
+}
+
+static int hiod_iommufd_get_vendor_cap(HostIOMMUDevice *hiod,
+                                       HostIOMMUDeviceIOMMUFDCaps *caps,
+                                       int cap, Error **errp)
+{
+    enum iommu_hw_info_type type = caps->type;
+
+    switch (type) {
+    case IOMMU_HW_INFO_TYPE_INTEL_VTD:
+        return hiod_iommufd_get_vtd_cap(hiod, &caps->vendor_caps.vtd,
+                                        cap, errp);
+    case IOMMU_HW_INFO_TYPE_ARM_SMMUV3:
+    case IOMMU_HW_INFO_TYPE_NONE:
+        break;
+    }
+
+    error_setg(errp, "%s: unsupported capability type %x", hiod->name, type);
+    return -EINVAL;
+}
+
+int hiod_iommufd_get_common_cap(HostIOMMUDevice *hiod,
+                                HostIOMMUDeviceIOMMUFDCaps *caps,
+                                int cap, Error **errp)
+{
+    switch (cap) {
+    case HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE:
+        return caps->type;
+    default:
+        return hiod_iommufd_get_vendor_cap(hiod, caps, cap, errp);
+    }
+}
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index e05b472e35..e7ca92f81f 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -833,6 +833,21 @@ static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
     return true;
 }
 
+static int hiod_iommufd_vfio_get_cap(HostIOMMUDevice *hiod, int cap,
+                                     Error **errp)
+{
+    VFIODevice *vdev = hiod->agent;
+    HostIOMMUDeviceIOMMUFDCaps *caps = &vdev->caps;
+
+    /* VFIO has its own way to get aw_bits which may be different from VDPA */
+    switch (cap) {
+    case HOST_IOMMU_DEVICE_CAP_AW_BITS:
+        return vfio_device_get_aw_bits(hiod->agent);
+    default:
+        return hiod_iommufd_get_common_cap(hiod, caps, cap, errp);
+    }
+}
+
 static GList *
 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
 {
@@ -857,6 +872,7 @@ static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
 
     hiodc->realize = hiod_iommufd_vfio_realize;
+    hiodc->get_cap = hiod_iommufd_vfio_get_cap;
     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
 };
-- 
2.34.1



  parent reply	other threads:[~2025-04-11 10:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-11 10:17 [PATCH 0/5] cleanup interfaces Zhenzhong Duan
2025-04-11 10:17 ` [PATCH 1/5] vfio/iommufd: Save host iommu capabilities in VFIODevice.caps Zhenzhong Duan
2025-04-11 10:49   ` Joao Martins
2025-04-14  9:11     ` Duan, Zhenzhong
2025-04-11 11:28   ` Cédric Le Goater
2025-04-14  9:30     ` Duan, Zhenzhong
     [not found]       ` <Z/7z2RZyqhy43S/O@Asurada-Nvidia>
2025-04-16  5:49         ` Duan, Zhenzhong
     [not found]           ` <Z//4pYO/Xs/7U+dW@Asurada-Nvidia>
2025-04-17  4:08             ` Duan, Zhenzhong
2025-05-05 16:14     ` Eric Auger
2025-05-06  9:25       ` Duan, Zhenzhong
2025-05-05 16:22   ` Eric Auger
2025-05-05 16:38   ` Eric Auger
2025-05-06  6:22     ` Nicolin Chen
2025-04-11 10:17 ` [PATCH 2/5] vfio: Move realize() after attach_device() Zhenzhong Duan
2025-04-11 10:54   ` Philippe Mathieu-Daudé
2025-04-14  9:12     ` Duan, Zhenzhong
2025-04-11 11:33   ` Cédric Le Goater
2025-04-14  9:37     ` Duan, Zhenzhong
2025-04-18 20:56   ` Donald Dutile
2025-04-11 10:17 ` Zhenzhong Duan [this message]
     [not found]   ` <Z/702atFa6kyCI/D@Asurada-Nvidia>
2025-04-16  5:54     ` [PATCH 3/5] vfio/iommufd: Implement .get_cap() in TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO sub-class Duan, Zhenzhong
2025-04-11 10:17 ` [PATCH 4/5] backends/iommufd: Drop hiod_iommufd_get_cap() Zhenzhong Duan
2025-04-11 10:17 ` [PATCH 5/5] vfio/iommufd: Drop HostIOMMUDeviceCaps from HostIOMMUDevice Zhenzhong Duan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250411101707.3460429-4-zhenzhong.duan@intel.com \
    --to=zhenzhong.duan@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=chao.p.peng@intel.com \
    --cc=clg@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=joao.m.martins@oracle.com \
    --cc=nicolinc@nvidia.com \
    --cc=qemu-devel@nongnu.org \
    --cc=yi.l.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).