Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp866914ybb; Fri, 20 Mar 2020 09:20:50 -0700 (PDT) X-Google-Smtp-Source: ADFU+vucnmXk3rEhR1tgL81gMK3dhHR6LrFfZIl8YdKof0S+mfjrYCJPqJdzcKrFI1S9cyYio5Cs X-Received: by 2002:a54:4388:: with SMTP id u8mr7151742oiv.67.1584721250775; Fri, 20 Mar 2020 09:20:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584721250; cv=none; d=google.com; s=arc-20160816; b=ViMWhZv8ByLXOkz8ngKELxzM8sD+NIkzc2N5yifCuHWT9Z2MCdzMMonSTRQGLIhaFE dszSv0bR9SWJtQAAaqZ14gMpehj9p/pLvd5vSGqb+J5qXQEzUNSIbycQ7CHKAIheAQ1D Nsfk6P3/ZoBr5UQqSUlRFkA4WGE0B55K4McZHq7lLuN8sesII5HM8ByM1jy78Ks8dCyX KbgvmjzqQNBNujvVzW2nzfNsXmI3wnzgUanFCaj5EgkeZT3cu2KLkDMDoOKzCv3utg4l UDOtz04nZbNjTBM09iEYQfaOC7nB3O/3Q+LVtTyYu900ces/G1fxna2XyjyNZPMldRqz YDAA== 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=G4KcaPvP2+ZJTQdICwGAAmPfH7tz1yQt/U/Khyn28OQ=; b=Naoc8fWv1/CZeMY66kxp6Tnv3NYRXDTeqkJSxK+h81rs7G+athy0kGq9lUEwH30DCK OEJo4F4dhYMX5QqKOfX9xkd8ZACtlYMC5LEhIC8nTxOMckJ2h0joBb2daUT1kFIobMF1 Y/YLEBG584UoZgQFrzlloJMHPrRlMCEFSgwLr0FyFItdYClhle2Gv2PM0HCKtK2YfVo9 NWg5EEFGVLJ0VzVzSelIeYEEUrAQbRplBODVFa/vYY8G3KF9OIH7RF+HGOuhaP4/U7qf 2olkYytARLnJwJHXK8BK//FucuaUcZoVeTl0cfkE9dpIjQmgiu8koEHs2c/iH8I2M5wn 1vIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iGNt+0y7; 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 w90si3325653otb.305.2020.03.20.09.20.35; Fri, 20 Mar 2020 09:20:50 -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=iGNt+0y7; 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 S1727671AbgCTQUH (ORCPT + 99 others); Fri, 20 Mar 2020 12:20:07 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:60329 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727613AbgCTQUD (ORCPT ); Fri, 20 Mar 2020 12:20:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584721203; 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=G4KcaPvP2+ZJTQdICwGAAmPfH7tz1yQt/U/Khyn28OQ=; b=iGNt+0y7StNFJ9c9x/8lhyA1PLQ3tDd5nq7Fn5oEDUNEOPIscwRtQV/Q58IL6ho13bOM1k rSQVjWDxpdi0qjQULSPv+1YznEFUswkVlxXXnj9DLMg1LvPJPFPXBvLzSRqiCxEQN/B+04 oB+IoWuDtvsUdxzvgK3cdkIaIVVvRlI= 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-201-tUUKnrpqMMK2P6J70y8zqg-1; Fri, 20 Mar 2020 12:19:59 -0400 X-MC-Unique: tUUKnrpqMMK2P6J70y8zqg-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 0D063107AD65; Fri, 20 Mar 2020 16:19:51 +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 0D68660C63; Fri, 20 Mar 2020 16:19:36 +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 03/11] vfio: VFIO_IOMMU_SET_MSI_BINDING Date: Fri, 20 Mar 2020 17:19:03 +0100 Message-Id: <20200320161911.27494-4-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 This patch adds the VFIO_IOMMU_SET_MSI_BINDING ioctl which aim to (un)register the guest MSI binding to the host. This latter then can use those stage 1 bindings to build a nested stage binding targeting the physical MSIs. Signed-off-by: Eric Auger --- v8 -> v9: - merge VFIO_IOMMU_BIND_MSI/VFIO_IOMMU_UNBIND_MSI into a single VFIO_IOMMU_SET_MSI_BINDING ioctl - ioctl id changed v6 -> v7: - removed the dev arg v3 -> v4: - add UNBIND - unwind on BIND error v2 -> v3: - adapt to new proto of bind_guest_msi - directly use vfio_iommu_for_each_dev v1 -> v2: - s/vfio_iommu_type1_guest_msi_binding/vfio_iommu_type1_bind_guest_msi --- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 20 ++++++++++++ 2 files changed, 75 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_ty= pe1.c index 04c6625098bb..17ba63de1494 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2246,6 +2246,42 @@ static int vfio_cache_inv_fn(struct device *dev, v= oid *data) return iommu_cache_invalidate(dc->domain, dev, &ustruct->info); } =20 +static int +vfio_bind_msi(struct vfio_iommu *iommu, + dma_addr_t giova, phys_addr_t gpa, size_t size) +{ + struct vfio_domain *d; + int ret =3D 0; + + mutex_lock(&iommu->lock); + + list_for_each_entry(d, &iommu->domain_list, next) { + ret =3D iommu_bind_guest_msi(d->domain, giova, gpa, size); + if (ret) + goto unwind; + } + goto unlock; +unwind: + list_for_each_entry_continue_reverse(d, &iommu->domain_list, next) { + iommu_unbind_guest_msi(d->domain, giova); + } +unlock: + mutex_unlock(&iommu->lock); + return ret; +} + +static void +vfio_unbind_msi(struct vfio_iommu *iommu, dma_addr_t giova) +{ + struct vfio_domain *d; + + mutex_lock(&iommu->lock); + list_for_each_entry(d, &iommu->domain_list, next) { + iommu_unbind_guest_msi(d->domain, giova); + } + mutex_unlock(&iommu->lock); +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { @@ -2387,6 +2423,25 @@ static long vfio_iommu_type1_ioctl(void *iommu_dat= a, &ustruct); mutex_unlock(&iommu->lock); return ret; + } else if (cmd =3D=3D VFIO_IOMMU_SET_MSI_BINDING) { + struct vfio_iommu_type1_set_msi_binding ustruct; + + minsz =3D offsetofend(struct vfio_iommu_type1_set_msi_binding, + size); + + if (copy_from_user(&ustruct, (void __user *)arg, minsz)) + return -EFAULT; + + if (ustruct.argsz < minsz) + return -EINVAL; + + if (ustruct.flags =3D=3D VFIO_IOMMU_UNBIND_MSI) + vfio_unbind_msi(iommu, ustruct.iova); + else if (ustruct.flags =3D=3D VFIO_IOMMU_BIND_MSI) + return vfio_bind_msi(iommu, ustruct.iova, ustruct.gpa, + ustruct.size); + else + return -EINVAL; } =20 return -ENOTTY; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 98212c1711e7..9f2429eb1958 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -826,6 +826,26 @@ struct vfio_iommu_type1_cache_invalidate { }; #define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 23) =20 +/** + * VFIO_IOMMU_SET_MSI_BINDING - _IOWR(VFIO_TYPE, VFIO_BASE + 24, + * struct vfio_iommu_type1_set_msi_binding) + * + * Pass a stage 1 MSI doorbell mapping to the host so that this + * latter can build a nested stage2 mapping. Or conversely tear + * down a previously bound stage 1 MSI binding. + */ +struct vfio_iommu_type1_set_msi_binding { + __u32 argsz; + __u32 flags; +#define VFIO_IOMMU_BIND_MSI (1 << 0) +#define VFIO_IOMMU_UNBIND_MSI (1 << 1) + __u64 iova; /* MSI guest IOVA */ + /* Fields below are used on BIND */ + __u64 gpa; /* MSI guest physical address */ + __u64 size; /* size of stage1 mapping (bytes) */ +}; +#define VFIO_IOMMU_SET_MSI_BINDING _IO(VFIO_TYPE, VFIO_BASE + 24) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU --------= */ =20 /* --=20 2.20.1