qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Alex Williamson" <alex.williamson@redhat.com>,
	"Zhenzhong Duan" <zhenzhong.duan@intel.com>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: [PULL 10/31] vfio: Introduce helper vfio_pci_from_vfio_device()
Date: Mon,  8 Sep 2025 18:53:33 +0200	[thread overview]
Message-ID: <20250908165354.1731444-11-clg@redhat.com> (raw)
In-Reply-To: <20250908165354.1731444-1-clg@redhat.com>

From: Zhenzhong Duan <zhenzhong.duan@intel.com>

Introduce helper vfio_pci_from_vfio_device() to transform from VFIODevice
to VFIOPCIDevice, also to hide low level VFIO_DEVICE_TYPE_PCI type check.

Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Link: https://lore.kernel.org/qemu-devel/20250822064101.123526-5-zhenzhong.duan@intel.com
[ clg: Added documentation ]
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 hw/vfio/pci.h       | 12 ++++++++++++
 hw/vfio/container.c |  4 ++--
 hw/vfio/device.c    |  2 +-
 hw/vfio/iommufd.c   |  4 ++--
 hw/vfio/listener.c  |  4 ++--
 hw/vfio/pci.c       |  9 +++++++++
 6 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
index 923cf9c2f79d078546239bf67195d6ba8157265a..96144b6fdeb94dccf6c0beecf039a12b9845c59b 100644
--- a/hw/vfio/pci.h
+++ b/hw/vfio/pci.h
@@ -226,6 +226,18 @@ void vfio_pci_write_config(PCIDevice *pdev,
 uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size);
 void vfio_vga_write(void *opaque, hwaddr addr, uint64_t data, unsigned size);
 
+/**
+ * vfio_pci_from_vfio_device: Transform from VFIODevice to
+ * VFIOPCIDevice
+ *
+ * This function checks if the given @vbasedev is a VFIO PCI device.
+ * If it is, it returns the containing VFIOPCIDevice.
+ *
+ * @vbasedev: The VFIODevice to transform
+ *
+ * Return: The VFIOPCIDevice on success, NULL on failure.
+ */
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev);
 void vfio_sub_page_bar_update_mappings(VFIOPCIDevice *vdev);
 bool vfio_opt_rom_in_denylist(VFIOPCIDevice *vdev);
 bool vfio_config_quirk_setup(VFIOPCIDevice *vdev, Error **errp);
diff --git a/hw/vfio/container.c b/hw/vfio/container.c
index 3e13feaa74c30e8d7f5a0978e15824cecbf6d674..134ddccc52449cd7d4ac90d17075e9fca5439674 100644
--- a/hw/vfio/container.c
+++ b/hw/vfio/container.c
@@ -1087,7 +1087,7 @@ static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single)
         /* Prep dependent devices for reset and clear our marker. */
         QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
             if (!vbasedev_iter->dev->realized ||
-                vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
+                !vfio_pci_from_vfio_device(vbasedev_iter)) {
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
@@ -1172,7 +1172,7 @@ out:
 
         QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
             if (!vbasedev_iter->dev->realized ||
-                vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
+                !vfio_pci_from_vfio_device(vbasedev_iter)) {
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 52a1996dc4e122499a978cc8a5ce7e01620a9639..08f12ac31f44fdebc9fc1f23f716c52f391ed53e 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -129,7 +129,7 @@ static inline const char *action_to_str(int action)
 
 static const char *index_to_str(VFIODevice *vbasedev, int index)
 {
-    if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
+    if (!vfio_pci_from_vfio_device(vbasedev)) {
         return NULL;
     }
 
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index 48c590b6a9467c8139131bef8b92864893a8f2e6..8c27222f754a22f47fda6b6a1a86005341ec0d7f 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -737,8 +737,8 @@ iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
     }
 
     vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
-    if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
-        vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
+    if (!vfio_pci_from_vfio_device(vbasedev_tmp) ||
+        !vbasedev_tmp->dev->realized) {
         return NULL;
     }
 
diff --git a/hw/vfio/listener.c b/hw/vfio/listener.c
index c244be5e218ae60e2b9c8a29d2d4b9dad86fc77a..e0938331659833bdca98e700ac42651a90fb1571 100644
--- a/hw/vfio/listener.c
+++ b/hw/vfio/listener.c
@@ -453,7 +453,7 @@ static void vfio_device_error_append(VFIODevice *vbasedev, Error **errp)
      * MMIO region mapping failures are not fatal but in this case PCI
      * peer-to-peer transactions are broken.
      */
-    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+    if (vfio_pci_from_vfio_device(vbasedev)) {
         error_append_hint(errp, "%s: PCI peer-to-peer transactions "
                           "on BARs are not supported.\n", vbasedev->name);
     }
@@ -759,7 +759,7 @@ static bool vfio_section_is_vfio_pci(MemoryRegionSection *section,
     owner = memory_region_owner(section->mr);
 
     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
-        if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
+        if (!vfio_pci_from_vfio_device(vbasedev)) {
             continue;
         }
         pcidev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 07257d0fa049b09fc296ac2279a6fafbdf93d277..3fe5b03eb1160bb66fc4ae3942d14a2d71b4431a 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2833,6 +2833,15 @@ static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
     return ret;
 }
 
+/* Transform from VFIODevice to VFIOPCIDevice. Return NULL if fails. */
+VFIOPCIDevice *vfio_pci_from_vfio_device(VFIODevice *vbasedev)
+{
+    if (vbasedev && vbasedev->type == VFIO_DEVICE_TYPE_PCI) {
+        return container_of(vbasedev, VFIOPCIDevice, vbasedev);
+    }
+    return NULL;
+}
+
 void vfio_sub_page_bar_update_mappings(VFIOPCIDevice *vdev)
 {
     PCIDevice *pdev = &vdev->pdev;
-- 
2.51.0



  parent reply	other threads:[~2025-09-08 17:04 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-08 16:53 [PULL 00/31] vfio queue Cédric Le Goater
2025-09-08 16:53 ` [PULL 01/31] MAINTAINERS: Add myself as reviewer for PowerNV and XIVE Cédric Le Goater
2025-09-08 16:53 ` [PULL 02/31] vfio/igd: Enable quirks when IGD is not the primary display Cédric Le Goater
2025-09-08 16:53 ` [PULL 03/31] vfio: Remove 'vfio-amd-xgbe' device Cédric Le Goater
2025-09-08 16:53 ` [PULL 04/31] vfio: Remove 'vfio-calxeda-xgmac' device Cédric Le Goater
2025-09-08 16:53 ` [PULL 05/31] vfio: Remove 'vfio-platform' Cédric Le Goater
2025-09-08 16:53 ` [PULL 06/31] vfio: Move vfio-region.h under hw/vfio/ Cédric Le Goater
2025-09-08 16:53 ` [PULL 07/31] vfio/container: set error on cpr failure Cédric Le Goater
2025-09-08 16:53 ` [PULL 08/31] vfio: Report an error when the 'dma_max_mappings' limit is reached Cédric Le Goater
2025-09-08 16:53 ` [PULL 09/31] hw/vfio-user: add x-pci-class-code Cédric Le Goater
2025-09-08 16:53 ` Cédric Le Goater [this message]
2025-09-08 16:53 ` [PULL 11/31] vfio/vfio-container-base.h: update VFIOContainerBase declaration Cédric Le Goater
2025-09-08 16:53 ` [PULL 12/31] vfio/vfio-container.h: update VFIOContainer declaration Cédric Le Goater
2025-09-08 16:53 ` [PULL 13/31] hw/vfio/cpr-legacy.c: use QOM casts where appropriate Cédric Le Goater
2025-09-08 16:53 ` [PULL 14/31] hw/vfio/container.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 15/31] ppc/spapr_pci_vfio.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 16/31] vfio/spapr.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 17/31] vfio/vfio-container.h: rename VFIOContainer bcontainer field to parent_obj Cédric Le Goater
2025-09-08 16:53 ` [PULL 18/31] vfio-user/container.h: update VFIOUserContainer declaration Cédric Le Goater
2025-09-08 16:53 ` [PULL 19/31] vfio/container.c: use QOM casts where appropriate Cédric Le Goater
2025-09-08 16:53 ` [PULL 20/31] vfio-user/container.h: rename VFIOUserContainer bcontainer field to parent_obj Cédric Le Goater
2025-09-08 16:53 ` [PULL 21/31] vfio-user/pci.c: update VFIOUserPCIDevice declaration Cédric Le Goater
2025-09-08 16:53 ` [PULL 22/31] vfio-user/pci.c: use QOM casts where appropriate Cédric Le Goater
2025-09-08 16:53 ` [PULL 23/31] vfio-user/pci.c: rename VFIOUserPCIDevice device field to parent_obj Cédric Le Goater
2025-09-08 16:53 ` [PULL 24/31] vfio/pci.h: update VFIOPCIDevice declaration Cédric Le Goater
2025-09-08 16:53 ` [PULL 25/31] vfio/pci.c: use QOM casts where appropriate Cédric Le Goater
2025-09-08 16:53 ` [PULL 26/31] vfio/pci-quirks.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 27/31] vfio/cpr.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 28/31] vfio/igd.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 29/31] vfio-user/pci.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 30/31] s390x/s390-pci-vfio.c: " Cédric Le Goater
2025-09-08 16:53 ` [PULL 31/31] vfio/pci.h: rename VFIOPCIDevice pdev field to parent_obj Cédric Le Goater
2025-09-11 13:11 ` [PULL 00/31] vfio queue Peter Maydell

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=20250908165354.1731444-11-clg@redhat.com \
    --to=clg@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=zhenzhong.duan@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).