From patchwork Tue Mar 2 20:38:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 12111779 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DATE_IN_FUTURE_03_06,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBFD6C433DB for ; Tue, 2 Mar 2021 16:08:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 858D564F14 for ; Tue, 2 Mar 2021 16:08:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1839046AbhCBQFP (ORCPT ); Tue, 2 Mar 2021 11:05:15 -0500 Received: from mga07.intel.com ([134.134.136.100]:51835 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446935AbhCBMle (ORCPT ); Tue, 2 Mar 2021 07:41:34 -0500 IronPort-SDR: NS7VCG0FYC4syDhUZnY+g6rYbjMmIubQtX9gFmeeggR//84hfGiPKYUSDidRQ5hfxj3b7t58PZ iq4okt3REvmw== X-IronPort-AV: E=McAfee;i="6000,8403,9910"; a="250841290" X-IronPort-AV: E=Sophos;i="5.81,216,1610438400"; d="scan'208";a="250841290" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2021 04:40:25 -0800 IronPort-SDR: 3m2tH7iTtFMEWJ1fNEtLOwW5jsZf0m2Tm6vYxUwFZllkkL72c4tFJWJLN3nHDOWSdjpTDEyAty eT6FiT3EfZxA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,216,1610438400"; d="scan'208";a="427472924" Received: from yiliu-dev.bj.intel.com (HELO dual-ub.bj.intel.com) ([10.238.156.135]) by fmsmga004.fm.intel.com with ESMTP; 02 Mar 2021 04:40:20 -0800 From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com, jasowang@redhat.com Cc: mst@redhat.com, pbonzini@redhat.com, eric.auger@redhat.com, david@gibson.dropbear.id.au, jean-philippe@linaro.org, kevin.tian@intel.com, yi.l.liu@intel.com, jun.j.tian@intel.com, yi.y.sun@intel.com, hao.wu@intel.com, kvm@vger.kernel.org, Lingshan.Zhu@intel.com, Jacob Pan , Yi Sun Subject: [RFC v11 12/25] vfio: add HostIOMMUContext support Date: Wed, 3 Mar 2021 04:38:14 +0800 Message-Id: <20210302203827.437645-13-yi.l.liu@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210302203827.437645-1-yi.l.liu@intel.com> References: <20210302203827.437645-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This patch adds support for HostIOMMUContext, implements bind_stage1_pgtbl() and unbind_stage1_pgtbl() 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/vfio/common.c | 70 +++++++++++++++++++++++++++ include/hw/iommu/host_iommu_context.h | 3 ++ include/hw/vfio/vfio-common.h | 3 ++ 3 files changed, 76 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 883815d5b0..433938c245 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1575,6 +1575,54 @@ static int vfio_get_iommu_type(VFIOContainer *container, return ret; } +static int vfio_host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + struct iommu_gpasid_bind_data *bind) +{ + VFIOContainer *container = container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_nesting_op *op; + unsigned long argsz; + int ret = 0; + + argsz = sizeof(*op) + sizeof(*bind); + op = g_malloc0(argsz); + op->argsz = argsz; + op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL; + memcpy(&op->data, bind, sizeof(*bind)); + + if (ioctl(container->fd, VFIO_IOMMU_NESTING_OP, op)) { + ret = -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_bind_data *unbind) +{ + VFIOContainer *container = container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_nesting_op *op; + unsigned long argsz; + int ret = 0; + + argsz = sizeof(*op) + sizeof(*unbind); + op = g_malloc0(argsz); + op->argsz = argsz; + op->flags = VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL; + memcpy(&op->data, unbind, sizeof(*unbind)); + + if (ioctl(container->fd, VFIO_IOMMU_NESTING_OP, op)) { + ret = -errno; + error_report("%s: pasid (%llu) unbind failed: %m", + __func__, unbind->hpasid); + } + g_free(op); + return ret; +} + static int vfio_init_container(VFIOContainer *container, int group_fd, bool want_nested, Error **errp) { @@ -2268,3 +2316,25 @@ int vfio_eeh_as_op(AddressSpace *as, uint32_t op) } return vfio_eeh_container_op(container, op); } + +static void vfio_host_iommu_context_class_init(ObjectClass *klass, + void *data) +{ + HostIOMMUContextClass *hicxc = HOST_IOMMU_CONTEXT_CLASS(klass); + + hicxc->bind_stage1_pgtbl = vfio_host_iommu_ctx_bind_stage1_pgtbl; + hicxc->unbind_stage1_pgtbl = vfio_host_iommu_ctx_unbind_stage1_pgtbl; +} + +static const TypeInfo vfio_host_iommu_context_info = { + .parent = TYPE_HOST_IOMMU_CONTEXT, + .name = TYPE_VFIO_HOST_IOMMU_CONTEXT, + .class_init = vfio_host_iommu_context_class_init, +}; + +static void vfio_register_types(void) +{ + type_register_static(&vfio_host_iommu_context_info); +} + +type_init(vfio_register_types) diff --git a/include/hw/iommu/host_iommu_context.h b/include/hw/iommu/host_iommu_context.h index 41c4176c15..3498a3e25d 100644 --- a/include/hw/iommu/host_iommu_context.h +++ b/include/hw/iommu/host_iommu_context.h @@ -33,6 +33,9 @@ #define TYPE_HOST_IOMMU_CONTEXT "qemu:host-iommu-context" #define HOST_IOMMU_CONTEXT(obj) \ OBJECT_CHECK(HostIOMMUContext, (obj), TYPE_HOST_IOMMU_CONTEXT) +#define HOST_IOMMU_CONTEXT_CLASS(klass) \ + OBJECT_CLASS_CHECK(HostIOMMUContextClass, (klass), \ + TYPE_HOST_IOMMU_CONTEXT) #define HOST_IOMMU_CONTEXT_GET_CLASS(obj) \ OBJECT_GET_CLASS(HostIOMMUContextClass, (obj), \ TYPE_HOST_IOMMU_CONTEXT) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 293d3785f3..55241ee270 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -26,6 +26,7 @@ #include "qemu/notify.h" #include "ui/console.h" #include "hw/display/ramfb.h" +#include "hw/iommu/host_iommu_context.h" #ifdef CONFIG_LINUX #include #endif @@ -33,6 +34,8 @@ #define VFIO_MSG_PREFIX "vfio %s: " +#define TYPE_VFIO_HOST_IOMMU_CONTEXT "qemu:vfio-host-iommu-context" + enum { VFIO_DEVICE_TYPE_PCI = 0, VFIO_DEVICE_TYPE_PLATFORM = 1,