Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp867097ybb; Fri, 20 Mar 2020 09:21:02 -0700 (PDT) X-Google-Smtp-Source: ADFU+vspcEue0JN9BAmM+E91J9YjpJ/SizaFD+wYFpnKvszl9FNETWrcwGh0TZbRTN8iD0ylxvBm X-Received: by 2002:a9d:7497:: with SMTP id t23mr7236520otk.291.1584721261912; Fri, 20 Mar 2020 09:21:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584721261; cv=none; d=google.com; s=arc-20160816; b=VMb/Mdt+hqIsuXlebpC6AYhanXi2LbFerBSKUIxZ/j5BnsY5Op103XRAlnvWMMBugZ dnnFxKNuf1KMPsU13+66g6R+NgxAR12FsMGtftvjYlS05B3FQtXO2BQOZDcJAywm8la+ 6KtE254JIoDc7Dc66CCNS0yp4Zez/q5fcfiOyRgxV0eXLe7WyMHdxua5cUbX/OlcfZGL Squ66qgS2IA97ib2wPVlOytgeyFzetjBRKBx70FaXDMl/7Y9hn1AzstQrzbbpRLUALeM +3RqtP8zjpesKAPP/WGWDU8uPTLTPdpaX7+EBQM710WOYeZw+Rrgsb8rrEVj/3wvOMJu S51w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IqrZ5PAivz8D0DNnQlULxvOl6hsl7punh73vFXG+XJQ=; b=wxotREufv051DF3k29WjiLbrARMeOW9xTjXpkhOTps0JTv26orqTcoy16eD3H05CWT mPYceznfoS6hpc9sKxgCpJZ+nDj12xFxYeFL5mLUm+4hpaIMtDk28p7uWYJYhxAITBqn CmYcGRjqnuMwyjXp74dRRWMeImmnt5IlmVhOmJ+1ZCj18xKldjzGmAqaxGzRyuhmwDbS Iv6MjfovN/a8bG6j4Y8O4eLOfKG9jhevSct8cu5IMpJ9GgBrPGpfTx9Y7q4SM9i3IYS9 SKERfCXznJ2kWMVJ2v8/O11C84SCTOw3S4bjMKuKW26izJTOiFbgb26oh3sz9o4c2dZI j/6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JMoML1Ha; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r184si2740448oie.79.2020.03.20.09.20.44; Fri, 20 Mar 2020 09:21:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JMoML1Ha; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727617AbgCTQUD (ORCPT + 99 others); Fri, 20 Mar 2020 12:20:03 -0400 Received: from us-smtp-delivery-74.mimecast.com ([63.128.21.74]:42086 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727542AbgCTQUC (ORCPT ); Fri, 20 Mar 2020 12:20:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584721200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IqrZ5PAivz8D0DNnQlULxvOl6hsl7punh73vFXG+XJQ=; b=JMoML1HaNCK58wpYB9CPxtsyQsU/NOcxCe0rYWLcaX3bfjJlYbWK3XzfBvGCKAFji+ZADS 9uWVucDGanORmFhCl0I0bYbO/fyk4ymSGE0icXxY9wirS2tudTKb6uCsi2dOy4iEeQhu8W L+9lB+BxmYvViu99/y93ZE0FTrQxB98= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-168-HCiK2-sHOu2SSnIYOI-GIg-1; Fri, 20 Mar 2020 12:19:56 -0400 X-MC-Unique: HCiK2-sHOu2SSnIYOI-GIg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 42559149C1; Fri, 20 Mar 2020 16:19:32 +0000 (UTC) Received: from laptop.redhat.com (ovpn-113-142.ams2.redhat.com [10.36.113.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 91F1760BFB; Fri, 20 Mar 2020 16:19:25 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, joro@8bytes.org, alex.williamson@redhat.com, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, jean-philippe.brucker@arm.com, will.deacon@arm.com, robin.murphy@arm.com Cc: marc.zyngier@arm.com, peter.maydell@linaro.org, zhangfei.gao@gmail.com Subject: [PATCH v10 01/11] vfio: VFIO_IOMMU_SET_PASID_TABLE Date: Fri, 20 Mar 2020 17:19:01 +0100 Message-Id: <20200320161911.27494-2-eric.auger@redhat.com> In-Reply-To: <20200320161911.27494-1-eric.auger@redhat.com> References: <20200320161911.27494-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Liu, Yi L" This patch adds an VFIO_IOMMU_SET_PASID_TABLE ioctl which aims to pass the virtual iommu guest configuration to the host. This latter takes the form of the so-called PASID table. Signed-off-by: Jacob Pan Signed-off-by: Liu, Yi L Signed-off-by: Eric Auger --- v8 -> v9: - Merge VFIO_IOMMU_ATTACH/DETACH_PASID_TABLE into a single VFIO_IOMMU_SET_PASID_TABLE ioctl. v6 -> v7: - add a comment related to VFIO_IOMMU_DETACH_PASID_TABLE v3 -> v4: - restore ATTACH/DETACH - add unwind on failure v2 -> v3: - s/BIND_PASID_TABLE/SET_PASID_TABLE v1 -> v2: - s/BIND_GUEST_STAGE/BIND_PASID_TABLE - remove the struct device arg --- drivers/vfio/vfio_iommu_type1.c | 56 +++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 19 +++++++++++ 2 files changed, 75 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_ty= pe1.c index a177bf2c6683..bfacbd876ee1 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2172,6 +2172,43 @@ static int vfio_iommu_iova_build_caps(struct vfio_= iommu *iommu, return ret; } =20 +static void +vfio_detach_pasid_table(struct vfio_iommu *iommu) +{ + struct vfio_domain *d; + + mutex_lock(&iommu->lock); + + list_for_each_entry(d, &iommu->domain_list, next) { + iommu_detach_pasid_table(d->domain); + } + mutex_unlock(&iommu->lock); +} + +static int +vfio_attach_pasid_table(struct vfio_iommu *iommu, + struct vfio_iommu_type1_set_pasid_table *ustruct) +{ + struct vfio_domain *d; + int ret =3D 0; + + mutex_lock(&iommu->lock); + + list_for_each_entry(d, &iommu->domain_list, next) { + ret =3D iommu_attach_pasid_table(d->domain, &ustruct->config); + if (ret) + goto unwind; + } + goto unlock; +unwind: + list_for_each_entry_continue_reverse(d, &iommu->domain_list, next) { + iommu_detach_pasid_table(d->domain); + } +unlock: + mutex_unlock(&iommu->lock); + return ret; +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { @@ -2276,6 +2313,25 @@ static long vfio_iommu_type1_ioctl(void *iommu_dat= a, =20 return copy_to_user((void __user *)arg, &unmap, minsz) ? -EFAULT : 0; + } else if (cmd =3D=3D VFIO_IOMMU_SET_PASID_TABLE) { + struct vfio_iommu_type1_set_pasid_table ustruct; + + minsz =3D offsetofend(struct vfio_iommu_type1_set_pasid_table, + config); + + if (copy_from_user(&ustruct, (void __user *)arg, minsz)) + return -EFAULT; + + if (ustruct.argsz < minsz) + return -EINVAL; + + if (ustruct.flags & VFIO_PASID_TABLE_FLAG_SET) + return vfio_attach_pasid_table(iommu, &ustruct); + else if (ustruct.flags & VFIO_PASID_TABLE_FLAG_UNSET) { + vfio_detach_pasid_table(iommu); + return 0; + } else + return -EINVAL; } =20 return -ENOTTY; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 9e843a147ead..e032a1fe6ed9 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -14,6 +14,7 @@ =20 #include #include +#include =20 #define VFIO_API_VERSION 0 =20 @@ -794,6 +795,24 @@ struct vfio_iommu_type1_dma_unmap { #define VFIO_IOMMU_ENABLE _IO(VFIO_TYPE, VFIO_BASE + 15) #define VFIO_IOMMU_DISABLE _IO(VFIO_TYPE, VFIO_BASE + 16) =20 +/** + * VFIO_IOMMU_SET_PASID_TABLE - _IOWR(VFIO_TYPE, VFIO_BASE + 22, + * struct vfio_iommu_type1_set_pasid_table) + * + * The SET operation passes a PASID table to the host while the + * UNSET operation detaches the one currently programmed. Setting + * a table while another is already programmed replaces the old table. + */ +struct vfio_iommu_type1_set_pasid_table { + __u32 argsz; + __u32 flags; +#define VFIO_PASID_TABLE_FLAG_SET (1 << 0) +#define VFIO_PASID_TABLE_FLAG_UNSET (1 << 1) + struct iommu_pasid_table_config config; /* used on SET */ +}; + +#define VFIO_IOMMU_SET_PASID_TABLE _IO(VFIO_TYPE, VFIO_BASE + 22) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU --------= */ =20 /* --=20 2.20.1