diff mbox series

[v1,09/22] vfio/common: check PASID alloc/free availability

Message ID 1584880579-12178-10-git-send-email-yi.l.liu@intel.com (mailing list archive)
State New, archived
Headers show
Series intel_iommu: expose Shared Virtual Addressing to VMs | expand

Commit Message

Yi Liu March 22, 2020, 12:36 p.m. UTC
VFIO exposes host IOMMU dual-stage DMA translation programming capability
to userspace by VFIO_TYPE1_NESTING_IOMMU type. However, userspace needs
more info on the nesting type. e.g. the supported stage 1 format and PASID
alloc/free request availability.

This patch gets the iommu nesting cap info from kernel by using IOCTL
VFIO_IOMMU_GET_INFO. And checks the HOST_IOMMU_PASID_REQUEST bit in the
nesting capabilities.

This patch referred some code from Shameer Kolothum.
https://lists.gnu.org/archive/html/qemu-devel/2018-05/msg03759.html

Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Jacob Pan <jacob.jun.pan@linux.intel.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Eric Auger <eric.auger@redhat.com>
Cc: Yi Sun <yi.y.sun@linux.intel.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Cc: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Signed-off-by: Liu Yi L <yi.l.liu@intel.com>
---
 hw/vfio/common.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 91 insertions(+), 5 deletions(-)

Comments

Peter Xu March 23, 2020, 10:06 p.m. UTC | #1
On Sun, Mar 22, 2020 at 05:36:06AM -0700, Liu Yi L wrote:

[...]

> @@ -1256,11 +1334,19 @@ static int vfio_init_container(VFIOContainer *container, int group_fd,
>      }
>  
>      if (iommu_type == VFIO_TYPE1_NESTING_IOMMU) {
> -        /*
> -         * TODO: config flags per host IOMMU nesting capability
> -         * e.g. check if VFIO_TYPE1_NESTING_IOMMU supports PASID
> -         * alloc/free
> -         */
> +        struct vfio_iommu_type1_info_cap_nesting nesting = {
> +                                         .nesting_capabilities = 0x0,
> +                                         .stage1_formats = 0, };
> +
> +        ret = vfio_get_nesting_iommu_cap(container, &nesting);
> +        if (ret) {
> +            error_setg_errno(errp, -ret,
> +                             "Failed to get nesting iommu cap");
> +            return ret;
> +        }
> +
> +        flags |= (nesting.nesting_capabilities & VFIO_IOMMU_PASID_REQS) ?
> +                 HOST_IOMMU_PASID_REQUEST : 0;

I replied in the previous patch but I forgot to use reply-all...

Anyway I'll comment again here - I think it'll be slightly better we
use the previous patch to only offer the vfio specific hooks, and this
patch to do all the rest including host_iommu_ctx_init() below, which
will avoid creating the host_iommu_ctx_init().

Thanks,

>          host_iommu_ctx_init(&container->host_icx,
>                              sizeof(container->host_icx),
>                              TYPE_VFIO_HOST_IOMMU_CONTEXT,
> -- 
> 2.7.4
>
Yi Liu March 24, 2020, 11:18 a.m. UTC | #2
> From: Peter Xu <peterx@redhat.com>
> Sent: Tuesday, March 24, 2020 6:07 AM
> To: Liu, Yi L <yi.l.liu@intel.com>
> Subject: Re: [PATCH v1 09/22] vfio/common: check PASID alloc/free availability
> 
> On Sun, Mar 22, 2020 at 05:36:06AM -0700, Liu Yi L wrote:
> 
> [...]
> 
> > @@ -1256,11 +1334,19 @@ static int vfio_init_container(VFIOContainer
> *container, int group_fd,
> >      }
> >
> >      if (iommu_type == VFIO_TYPE1_NESTING_IOMMU) {
> > -        /*
> > -         * TODO: config flags per host IOMMU nesting capability
> > -         * e.g. check if VFIO_TYPE1_NESTING_IOMMU supports PASID
> > -         * alloc/free
> > -         */
> > +        struct vfio_iommu_type1_info_cap_nesting nesting = {
> > +                                         .nesting_capabilities = 0x0,
> > +                                         .stage1_formats = 0, };
> > +
> > +        ret = vfio_get_nesting_iommu_cap(container, &nesting);
> > +        if (ret) {
> > +            error_setg_errno(errp, -ret,
> > +                             "Failed to get nesting iommu cap");
> > +            return ret;
> > +        }
> > +
> > +        flags |= (nesting.nesting_capabilities & VFIO_IOMMU_PASID_REQS) ?
> > +                 HOST_IOMMU_PASID_REQUEST : 0;
> 
> I replied in the previous patch but I forgot to use reply-all...
> 
> Anyway I'll comment again here - I think it'll be slightly better we
> use the previous patch to only offer the vfio specific hooks, and this
> patch to do all the rest including host_iommu_ctx_init() below, which
> will avoid creating the host_iommu_ctx_init().

Got it. Let me do it in next version.

Regards,
Yi Liu
diff mbox series

Patch

diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index e4f5f10..e0f2828 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -1223,6 +1223,84 @@  static int vfio_host_icx_pasid_free(HostIOMMUContext *host_icx,
     return 0;
 }
 
+/**
+ * Get iommu info from host. Caller of this funcion should free
+ * the memory pointed by the returned pointer stored in @info
+ * after a successful calling when finished its usage.
+ */
+static int vfio_get_iommu_info(VFIOContainer *container,
+                         struct vfio_iommu_type1_info **info)
+{
+
+    size_t argsz = sizeof(struct vfio_iommu_type1_info);
+
+    *info = g_malloc0(argsz);
+
+retry:
+    (*info)->argsz = argsz;
+
+    if (ioctl(container->fd, VFIO_IOMMU_GET_INFO, *info)) {
+        g_free(*info);
+        *info = NULL;
+        return -errno;
+    }
+
+    if (((*info)->argsz > argsz)) {
+        argsz = (*info)->argsz;
+        *info = g_realloc(*info, argsz);
+        goto retry;
+    }
+
+    return 0;
+}
+
+static struct vfio_info_cap_header *
+vfio_get_iommu_info_cap(struct vfio_iommu_type1_info *info, uint16_t id)
+{
+    struct vfio_info_cap_header *hdr;
+    void *ptr = info;
+
+    if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) {
+        return NULL;
+    }
+
+    for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) {
+        if (hdr->id == id) {
+            return hdr;
+        }
+    }
+
+    return NULL;
+}
+
+static int vfio_get_nesting_iommu_cap(VFIOContainer *container,
+                   struct vfio_iommu_type1_info_cap_nesting *cap_nesting)
+{
+    struct vfio_iommu_type1_info *info;
+    struct vfio_info_cap_header *hdr;
+    struct vfio_iommu_type1_info_cap_nesting *cap;
+    int ret;
+
+    ret = vfio_get_iommu_info(container, &info);
+    if (ret) {
+        return ret;
+    }
+
+    hdr = vfio_get_iommu_info_cap(info,
+                        VFIO_IOMMU_TYPE1_INFO_CAP_NESTING);
+    if (!hdr) {
+        g_free(info);
+        return -errno;
+    }
+
+    cap = container_of(hdr,
+                struct vfio_iommu_type1_info_cap_nesting, header);
+    *cap_nesting = *cap;
+
+    g_free(info);
+    return 0;
+}
+
 static int vfio_init_container(VFIOContainer *container, int group_fd,
                                Error **errp)
 {
@@ -1256,11 +1334,19 @@  static int vfio_init_container(VFIOContainer *container, int group_fd,
     }
 
     if (iommu_type == VFIO_TYPE1_NESTING_IOMMU) {
-        /*
-         * TODO: config flags per host IOMMU nesting capability
-         * e.g. check if VFIO_TYPE1_NESTING_IOMMU supports PASID
-         * alloc/free
-         */
+        struct vfio_iommu_type1_info_cap_nesting nesting = {
+                                         .nesting_capabilities = 0x0,
+                                         .stage1_formats = 0, };
+
+        ret = vfio_get_nesting_iommu_cap(container, &nesting);
+        if (ret) {
+            error_setg_errno(errp, -ret,
+                             "Failed to get nesting iommu cap");
+            return ret;
+        }
+
+        flags |= (nesting.nesting_capabilities & VFIO_IOMMU_PASID_REQS) ?
+                 HOST_IOMMU_PASID_REQUEST : 0;
         host_iommu_ctx_init(&container->host_icx,
                             sizeof(container->host_icx),
                             TYPE_VFIO_HOST_IOMMU_CONTEXT,