From nobody Sun Feb 8 15:53:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1591880358; cv=none; d=zohomail.com; s=zohoarc; b=KnmFiyT9sI1KRw3tEWcqaaDxSSTvGmZg5b3/KblQySpiX4J4E3jRbLADQRJzMJ7e8Z9+HioYoginJSIRmOWCprMZrr9EackeCQb2YryuUAxl4ljUdLGXgQyQ0sC25iIVdBy2OSp027UKhdCPD2a1v25ahY6lmliwTMPGodopVHg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591880358; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=S0/R+JyiYv7uKivS4KghhAU1ZpQPAru5g+LdgAqG1pQ=; b=XJqpiOcBj9ki65gsd80cvica/IIIy8/bmC4+LEpuT+tQqXJ76xGRXWSOa+UVHjqpNEhRdtkdnJn/PmXQgYSSHdFyWDYjgo5p0lbOhsmhAo9KBVUKZAn1VfHLiAaDjL7j9JrXTokrmaibWZNtZ8at6kv40rbsavBxybxdLpiYMlo= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1591880358411668.601739851696; Thu, 11 Jun 2020 05:59:18 -0700 (PDT) Received: from localhost ([::1]:55312 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jjMnd-0005RO-4W for importer@patchew.org; Thu, 11 Jun 2020 08:59:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46450) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjMd1-0002lw-No for qemu-devel@nongnu.org; Thu, 11 Jun 2020 08:48:19 -0400 Received: from mga14.intel.com ([192.55.52.115]:37617) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjMcz-0007zT-23 for qemu-devel@nongnu.org; Thu, 11 Jun 2020 08:48:19 -0400 Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jun 2020 05:48:01 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga005.jf.intel.com with ESMTP; 11 Jun 2020 05:48:00 -0700 IronPort-SDR: ajgfvNMNGuRG/ALKUPHz7FWUnnwvNascjD0kFoG43ZlvZgQP3tqPIrtQZ2/nGjFD5TnbVAT0H2 vjEKSDH8VsxQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: nFXldJEi9zDDjZQcjBsaDPltia1OQZmotCQPr1W3ie2Z4dgf4QMgq03Ka73z1XXYn4l2l1LDTM n0E6rSKrPApw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,499,1583222400"; d="scan'208";a="447911263" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v6 16/25] vfio: add bind stage-1 page table support Date: Thu, 11 Jun 2020 05:54:15 -0700 Message-Id: <1591880064-30638-17-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1591880064-30638-1-git-send-email-yi.l.liu@intel.com> References: <1591880064-30638-1-git-send-email-yi.l.liu@intel.com> Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=192.55.52.115; envelope-from=yi.l.liu@intel.com; helo=mga14.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/11 08:48:00 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jean-philippe@linaro.org, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds bind_stage1_pgtbl() definition in HostIOMMUContextClass, also adds corresponding implementation in VFIO. This is to expose a way for vIOMMU to setup dual stage DMA translation for passthru devices on hardware. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Signed-off-by: Liu Yi L --- hw/iommu/host_iommu_context.c | 57 +++++++++++++++++++++++++++++++= ++- hw/vfio/common.c | 58 +++++++++++++++++++++++++++++++= +++- include/hw/iommu/host_iommu_context.h | 19 +++++++++++- 3 files changed, 131 insertions(+), 3 deletions(-) diff --git a/hw/iommu/host_iommu_context.c b/hw/iommu/host_iommu_context.c index 5fb2223..5c5d5cf 100644 --- a/hw/iommu/host_iommu_context.c +++ b/hw/iommu/host_iommu_context.c @@ -69,23 +69,78 @@ int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_c= tx, uint32_t pasid) return hicxc->pasid_free(iommu_ctx, pasid); } =20 +int host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_bind_data *bind) +{ + HostIOMMUContextClass *hicxc; + + if (!iommu_ctx) { + return -EINVAL; + } + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_NESTING) || + !hicxc->bind_stage1_pgtbl) { + return -EINVAL; + } + + return hicxc->bind_stage1_pgtbl(iommu_ctx, bind); +} + +int host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_unbind_data *unbind) +{ + HostIOMMUContextClass *hicxc; + + if (!iommu_ctx) { + return -EINVAL; + } + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_NESTING) || + !hicxc->unbind_stage1_pgtbl) { + return -EINVAL; + } + + return hicxc->unbind_stage1_pgtbl(iommu_ctx, unbind); +} + void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, const char *mrtypename, - uint64_t flags) + uint64_t flags, + struct iommu_nesting_info *info) { HostIOMMUContext *iommu_ctx; =20 object_initialize(_iommu_ctx, instance_size, mrtypename); iommu_ctx =3D HOST_IOMMU_CONTEXT(_iommu_ctx); iommu_ctx->flags =3D flags; + iommu_ctx->info =3D g_malloc0(info->size); + memcpy(iommu_ctx->info, info, info->size); iommu_ctx->initialized =3D true; } =20 +static void host_iommu_ctx_finalize_fn(Object *obj) +{ + HostIOMMUContext *iommu_ctx =3D HOST_IOMMU_CONTEXT(obj); + + g_free(iommu_ctx->info); +} + static const TypeInfo host_iommu_context_info =3D { .parent =3D TYPE_OBJECT, .name =3D TYPE_HOST_IOMMU_CONTEXT, .class_size =3D sizeof(HostIOMMUContextClass), .instance_size =3D sizeof(HostIOMMUContext), + .instance_finalize =3D host_iommu_ctx_finalize_fn, .abstract =3D true, }; =20 diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 6f94d19..3e500f5 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1228,6 +1228,54 @@ static int vfio_host_iommu_ctx_pasid_free(HostIOMMUC= ontext *iommu_ctx, return ret; } =20 +static int vfio_host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_c= tx, + struct iommu_gpasid_bind_data *bi= nd) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_nesting_op *op; + unsigned long argsz; + int ret =3D 0; + + argsz =3D sizeof(*op) + sizeof(*bind); + op =3D g_malloc0(argsz); + op->argsz =3D argsz; + op->flags =3D VFIO_IOMMU_NESTING_OP_BIND_PGTBL; + memcpy(&op->data, bind, sizeof(*bind)); + + if (ioctl(container->fd, VFIO_IOMMU_NESTING_OP, op)) { + ret =3D -errno; + error_report("%s: pasid (%llu) bind failed: %m", + __func__, bind->hpasid); + } + g_free(op); + return ret; +} + +static int vfio_host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu= _ctx, + struct iommu_gpasid_unbind_data *un= bind) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_nesting_op *op; + unsigned long argsz; + int ret =3D 0; + + argsz =3D sizeof(*op) + sizeof(*unbind); + op =3D g_malloc0(argsz); + op->argsz =3D argsz; + op->flags =3D VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL; + memcpy(&op->data, unbind, sizeof(*unbind)); + + if (ioctl(container->fd, VFIO_IOMMU_NESTING_OP, op)) { + ret =3D -errno; + error_report("%s: pasid (%llu) unbind failed: %m", + __func__, unbind->pasid); + } + g_free(op); + return ret; +} + /** * Get iommu info from host. Caller of this funcion should free * the memory pointed by the returned pointer stored in @info @@ -1363,10 +1411,16 @@ static int vfio_init_container(VFIOContainer *conta= iner, int group_fd, nest_info =3D (struct iommu_nesting_info *) &nesting->info; flags |=3D (nest_info->features & IOMMU_NESTING_FEAT_SYSWIDE_PASID= ) ? HOST_IOMMU_PASID_REQUEST : 0; + if ((nest_info->features & IOMMU_NESTING_FEAT_BIND_PGTBL) && + (nest_info->features & IOMMU_NESTING_FEAT_CACHE_INVLD)) { + flags |=3D HOST_IOMMU_NESTING; + } + host_iommu_ctx_init(&container->iommu_ctx, sizeof(container->iommu_ctx), TYPE_VFIO_HOST_IOMMU_CONTEXT, - flags); + flags, + nest_info); g_free(nesting); } =20 @@ -1960,6 +2014,8 @@ static void vfio_host_iommu_context_class_init(Object= Class *klass, =20 hicxc->pasid_alloc =3D vfio_host_iommu_ctx_pasid_alloc; hicxc->pasid_free =3D vfio_host_iommu_ctx_pasid_free; + hicxc->bind_stage1_pgtbl =3D vfio_host_iommu_ctx_bind_stage1_pgtbl; + hicxc->unbind_stage1_pgtbl =3D vfio_host_iommu_ctx_unbind_stage1_pgtbl; } =20 static const TypeInfo vfio_host_iommu_context_info =3D { diff --git a/include/hw/iommu/host_iommu_context.h b/include/hw/iommu/host_= iommu_context.h index 227c433..7fb8356 100644 --- a/include/hw/iommu/host_iommu_context.h +++ b/include/hw/iommu/host_iommu_context.h @@ -54,6 +54,16 @@ typedef struct HostIOMMUContextClass { /* Reclaim pasid from HostIOMMUContext (a.k.a. host software) */ int (*pasid_free)(HostIOMMUContext *iommu_ctx, uint32_t pasid); + /* + * Bind stage-1 page table to a hostIOMMU w/ dual stage + * DMA translation capability. + * @bind specifies the bind configurations. + */ + int (*bind_stage1_pgtbl)(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_bind_data *bind); + /* Undo a previous bind. @unbind specifies the unbind info. */ + int (*unbind_stage1_pgtbl)(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_unbind_data *unbind); } HostIOMMUContextClass; =20 /* @@ -62,17 +72,24 @@ typedef struct HostIOMMUContextClass { struct HostIOMMUContext { Object parent_obj; #define HOST_IOMMU_PASID_REQUEST (1ULL << 0) +#define HOST_IOMMU_NESTING (1ULL << 1) uint64_t flags; + struct iommu_nesting_info *info; bool initialized; }; =20 int host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, uint32_t min, uint32_t max, uint32_t *pasid); int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, uint32_t pasid); +int host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_bind_data *bind); +int host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_unbind_data *unbind); =20 void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, const char *mrtypename, - uint64_t flags); + uint64_t flags, + struct iommu_nesting_info *info); void host_iommu_ctx_destroy(HostIOMMUContext *iommu_ctx); =20 #endif --=20 2.7.4