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: "Eric Auger" <eric.auger@redhat.com>,
	"Zhenzhong Duan" <zhenzhong.duan@intel.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: [PATCH v1 10/16] vfio/container: Discover IOMMU type before creating the container
Date: Wed, 12 Jun 2024 15:01:16 +0200	[thread overview]
Message-ID: <20240612130122.813935-11-clg@redhat.com> (raw)
In-Reply-To: <20240612130122.813935-1-clg@redhat.com>

Since the QEMU struct type representing the VFIO container is deduced
from the IOMMU type exposed by the host, this type should be well
defined *before* creating the container struct. This will be necessary
to instantiate a QOM object of the correct type in future changes.

Rework vfio_set_iommu() to extract the part doing the container
initialization and move it under vfio_create_container().

Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 hw/vfio/container.c | 47 ++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 24 deletions(-)

diff --git a/hw/vfio/container.c b/hw/vfio/container.c
index 8b47d1a13dca793dee1e1b7e06f36e8ea58a81e9..983726f9514ec1106d521c9711a46a4780688ee1 100644
--- a/hw/vfio/container.c
+++ b/hw/vfio/container.c
@@ -389,54 +389,56 @@ static const char *vfio_get_iommu_class_name(int iommu_type)
     };
 }
 
-static bool vfio_set_iommu(VFIOContainer *container, int group_fd,
-                           Error **errp)
+static bool vfio_set_iommu(int container_fd, int group_fd,
+                           int *iommu_type, Error **errp)
 {
-    int iommu_type;
-    const VFIOIOMMUClass *vioc;
-    const char *vioc_name;
-
-    iommu_type = vfio_get_iommu_type(container->fd, errp);
-    if (iommu_type < 0) {
-        return false;
-    }
-
-    if (ioctl(group_fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
+    if (ioctl(group_fd, VFIO_GROUP_SET_CONTAINER, &container_fd)) {
         error_setg_errno(errp, errno, "Failed to set group container");
         return false;
     }
 
-    while (ioctl(container->fd, VFIO_SET_IOMMU, iommu_type)) {
-        if (iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
+    while (ioctl(container_fd, VFIO_SET_IOMMU, *iommu_type)) {
+        if (*iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
             /*
              * On sPAPR, despite the IOMMU subdriver always advertises v1 and
              * v2, the running platform may not support v2 and there is no
              * way to guess it until an IOMMU group gets added to the container.
              * So in case it fails with v2, try v1 as a fallback.
              */
-            iommu_type = VFIO_SPAPR_TCE_IOMMU;
+            *iommu_type = VFIO_SPAPR_TCE_IOMMU;
             continue;
         }
         error_setg_errno(errp, errno, "Failed to set iommu for container");
         return false;
     }
 
-    container->iommu_type = iommu_type;
-
-    vioc_name = vfio_get_iommu_class_name(iommu_type);
-    vioc = VFIO_IOMMU_CLASS(object_class_by_name(vioc_name));
-
-    vfio_container_init(&container->bcontainer, vioc);
     return true;
 }
 
 static VFIOContainer *vfio_create_container(int fd, VFIOGroup *group,
                                             Error **errp)
 {
+    int iommu_type;
+    const VFIOIOMMUClass *vioc;
+    const char *vioc_name;
     VFIOContainer *container;
 
+    iommu_type = vfio_get_iommu_type(fd, errp);
+    if (iommu_type < 0) {
+        return NULL;
+    }
+
+    if (!vfio_set_iommu(fd, group->fd, &iommu_type, errp)) {
+        return NULL;
+    }
+
+    vioc_name = vfio_get_iommu_class_name(iommu_type);
+    vioc = VFIO_IOMMU_CLASS(object_class_by_name(vioc_name));
+
     container = g_malloc0(sizeof(*container));
     container->fd = fd;
+    container->iommu_type = iommu_type;
+    vfio_container_init(&container->bcontainer, vioc);
     return container;
 }
 
@@ -618,9 +620,6 @@ static bool vfio_connect_container(VFIOGroup *group, AddressSpace *as,
     if (!container) {
         goto close_fd_exit;
     }
-    if (!vfio_set_iommu(container, group->fd, errp)) {
-        goto free_container_exit;
-    }
     bcontainer = &container->bcontainer;
 
     if (!vfio_cpr_register_container(bcontainer, errp)) {
-- 
2.45.2



  parent reply	other threads:[~2024-06-12 13:08 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-12 13:01 [PATCH v1 00/16] vfio: QOMify VFIOContainer Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 01/16] vfio: Make vfio_devices_dma_logging_start() return bool Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 02/16] vfio: Remove unused declarations from vfio-common.h Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 03/16] vfio/common: Move dirty tracking ranges update to helper Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 04/16] vfio/common: Extract vIOMMU code from vfio_sync_dirty_bitmap() Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 05/16] vfio/container: Introduce vfio_address_space_insert() Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 06/16] vfio/container: Simplify vfio_container_init() Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 07/16] vfio/container: Modify vfio_get_iommu_type() to use a container fd Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 08/16] vfio/container: Introduce vfio_get_iommu_class_name() Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 09/16] vfio/container: Introduce vfio_create_container() Cédric Le Goater
2024-06-12 13:01 ` Cédric Le Goater [this message]
2024-06-12 13:01 ` [PATCH v1 11/16] vfio/container: Change VFIOContainerBase to use QOM Cédric Le Goater
2024-06-13  9:17   ` Duan, Zhenzhong
2024-06-13 12:21     ` Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 12/16] vfio/container: Switch to QOM Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 13/16] vfio/container: Introduce an instance_init() handler Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 14/16] vfio/container: Remove VFIOContainerBase::ops Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 15/16] vfio/container: Remove vfio_container_init() Cédric Le Goater
2024-06-13  9:20   ` Duan, Zhenzhong
2024-06-13 13:38     ` Cédric Le Goater
2024-06-12 13:01 ` [PATCH v1 16/16] vfio/container: Introduce vfio_iommu_legacy_instance_init() Cédric Le Goater
2024-06-17  1:30 ` [PATCH v1 00/16] vfio: QOMify VFIOContainer Duan, Zhenzhong
2024-06-17  5:04   ` Cédric Le Goater

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=20240612130122.813935-11-clg@redhat.com \
    --to=clg@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=eric.auger@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).