From patchwork Tue Jul 28 06:34:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 11688403 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 342BE913 for ; Tue, 28 Jul 2020 06:31:26 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 12D2C20792 for ; Tue, 28 Jul 2020 06:31:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 12D2C20792 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:40580 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k0J90-0006KL-MK for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Jul 2020 02:31:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50642) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0J5b-00011f-Mj for qemu-devel@nongnu.org; Tue, 28 Jul 2020 02:27:51 -0400 Received: from mga12.intel.com ([192.55.52.136]:57952) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0J5X-0002Ah-Rj for qemu-devel@nongnu.org; Tue, 28 Jul 2020 02:27:51 -0400 IronPort-SDR: tbf9DNCrMHgwNc2jeMLTFMO06Cvmtm56orKtjGyfR9wIEvO/+7RBM6RRMdXAkBAn5xPXCRNBBw yws3/V6iRoew== X-IronPort-AV: E=McAfee;i="6000,8403,9695"; a="130720417" X-IronPort-AV: E=Sophos;i="5.75,405,1589266800"; d="scan'208";a="130720417" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2020 23:27:41 -0700 IronPort-SDR: ZZBh2i2ZiLLbFq2GI8n1scT1MUTr2ELDtZKZ6a62v2422P609PZmiTGok7fQ4Y3rhkjetKls9u qv8FmENxgEzw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,405,1589266800"; d="scan'208";a="394232901" Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by fmsmga001.fm.intel.com with ESMTP; 27 Jul 2020 23:27:40 -0700 From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com, jasowang@redhat.com Subject: [RFC v9 11/25] vfio/common: provide PASID alloc/free hooks Date: Mon, 27 Jul 2020 23:34:04 -0700 Message-Id: <1595918058-33392-12-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1595918058-33392-1-git-send-email-yi.l.liu@intel.com> References: <1595918058-33392-1-git-send-email-yi.l.liu@intel.com> Received-SPF: pass client-ip=192.55.52.136; envelope-from=yi.l.liu@intel.com; helo=mga12.intel.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/28 02:27:42 X-ACL-Warn: Detected OS = FreeBSD 9.x or newer [fuzzy] X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" This patch defines vfio_host_iommu_context_info, implements the PASID alloc/free hooks defined in HostIOMMUContextClass. 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 | 66 +++++++++++++++++++++++++++++++++++ include/hw/iommu/host_iommu_context.h | 3 ++ include/hw/vfio/vfio-common.h | 4 +++ 3 files changed, 73 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index af91eca..41aaf41 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1183,6 +1183,50 @@ static int vfio_get_iommu_type(VFIOContainer *container, return ret; } +static int vfio_host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, + uint32_t min, uint32_t max, + uint32_t *pasid) +{ + VFIOContainer *container = container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_pasid_request req; + int ret = 0; + + req.argsz = sizeof(req); + req.flags = VFIO_IOMMU_FLAG_ALLOC_PASID; + req.range.min = min; + req.range.max = max; + + ret = ioctl(container->fd, VFIO_IOMMU_PASID_REQUEST, &req); + if (ret < 0) { + error_report("%s: alloc failed (%m)", __func__); + return ret; + } + *pasid = ret; + return 0; +} + +static int vfio_host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, + uint32_t pasid) +{ + VFIOContainer *container = container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_pasid_request req; + + int ret = 0; + + req.argsz = sizeof(req); + req.flags = VFIO_IOMMU_FLAG_FREE_PASID; + req.range.min = pasid; + req.range.max = pasid + 1; + + ret = ioctl(container->fd, VFIO_IOMMU_PASID_REQUEST, &req); + if (ret) { + error_report("%s: free failed (%m)", __func__); + } + return ret; +} + static int vfio_init_container(VFIOContainer *container, int group_fd, bool want_nested, Error **errp) { @@ -1802,3 +1846,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->pasid_alloc = vfio_host_iommu_ctx_pasid_alloc; + hicxc->pasid_free = vfio_host_iommu_ctx_pasid_free; +} + +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 35c4861..227c433 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 bdb09f4..a5eaf35 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -26,12 +26,15 @@ #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 #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, @@ -71,6 +74,7 @@ typedef struct VFIOContainer { MemoryListener listener; MemoryListener prereg_listener; unsigned iommu_type; + HostIOMMUContext iommu_ctx; Error *error; bool initialized; unsigned long pgsizes;