Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp635955rdf; Fri, 3 Nov 2023 10:18:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFpSiIBwwFKgP0MSxdRR0qeeqQCux31jgV6n+9vN+eyoGwTw1lgh68SZMPKTHxtxGIcxch2 X-Received: by 2002:a05:6808:f8e:b0:3ae:2850:b481 with SMTP id o14-20020a0568080f8e00b003ae2850b481mr24236033oiw.17.1699031882287; Fri, 03 Nov 2023 10:18:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699031882; cv=none; d=google.com; s=arc-20160816; b=N1qHdx5LlPyiylh0gJEChOsWtpFftGwctnudMZ0dXB9GoNAoq4hQz55vdsR34oSYjH RxduvTewrAVxnQ8aPis+a+aKFNsSviZ1jGnt6MyVpMWCljdFfN8otEMWaZY4IZRN84fL BFgBVT2sK2zAbKCnk4V9RQCv/2PJC4Mc5+GB0pOVu2P9GKdgyN4DCW2hjsDJ4hiRYgtI NRJWB17haaBWrEG5Poz6eRmPmcqyYE++atRU3nCPmmXrgPCH6dZerxpL7HFikxsFA98l lBsNgaX9tjPQBlO4yDHReFF7l836h+mx/7NFlNRpIRkWRnU+mnkE3yDl/pp8SjwhiLXo Chdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=OZOkp7RIIlEcdzCc+o8G685sXVkqf9owoeJPzWLLPLs=; fh=xiBZnYthZkU72K3VlivZH8ewgfrUkWRaBxXhAGtxMAw=; b=eN0B6EESOmZp0iC3o7HyINx0UTkT7yGEiD3VgkgBkexruYHC4dAQxPmpwxAvlO/nRB zmTZQBHFxG35UuF82hFSz84z/mYA1oWDp7QvZ8Q53V7pTzLBuic4H1kv6jhcgfhKZYXY uykfgSviQWXvGiliTe0LDzXHKBSVHEmPavcmUaQNC5xPu+vtQe3UvoA7MuYRL2BuUPdL DM0eLc4U5ZpzzqOgjjN5/4gAPCNU0DPe4HiAdypR9lsURsSpuSiZr/cG/3cE/Oz/oAAj urgPg3EQNSGOgQCV710XfU751wQQH5bTfsuZdP1DS2PuBclQ0kS6gcbFF1sfmJvwu48m NUeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DXAglskq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id ck24-20020a056a02091800b005b881cc9498si2059299pgb.339.2023.11.03.10.18.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:18:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DXAglskq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 946A2832FD90; Fri, 3 Nov 2023 10:17:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230202AbjKCRRj (ORCPT + 99 others); Fri, 3 Nov 2023 13:17:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjKCRRj (ORCPT ); Fri, 3 Nov 2023 13:17:39 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7F1CD48 for ; Fri, 3 Nov 2023 10:16:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031815; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OZOkp7RIIlEcdzCc+o8G685sXVkqf9owoeJPzWLLPLs=; b=DXAglskqoBjX1zAit+HiPrFFFIgik4spl9dgitQ0t1GFh/BYuLCJYEmlbtN3XOkAyFhcOv Z0KHobG2TLP4uJn1XStEOZCduueCjCfcejL1tMvNg/eavO6LA06tl5l1b6xJ1Q3NX26myw 52l83kD5zM8xotg0oiCfj4DHxaXdsCI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-589-s4ZotfSOOLO-hga7WjSckw-1; Fri, 03 Nov 2023 13:16:52 -0400 X-MC-Unique: s4ZotfSOOLO-hga7WjSckw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 21CC1811E88; Fri, 3 Nov 2023 17:16:52 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id F145740C6ECE; Fri, 3 Nov 2023 17:16:48 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, yi.l.liu@intel.com, jgg@nvidia.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC v1 1/8] vhost/iommufd: Add the functions support iommufd Date: Sat, 4 Nov 2023 01:16:34 +0800 Message-Id: <20231103171641.1703146-2-lulu@redhat.com> In-Reply-To: <20231103171641.1703146-1-lulu@redhat.com> References: <20231103171641.1703146-1-lulu@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 03 Nov 2023 10:17:55 -0700 (PDT) Add a new file vhost/iommufd.c to support the function of iommufd, This file contains iommufd function of emulated device and the physical device.  Signed-off-by: Cindy Lu --- drivers/vhost/iommufd.c | 178 ++++++++++++++++++++++++++++++++++++++++ drivers/vhost/vhost.h | 21 +++++ 2 files changed, 199 insertions(+) create mode 100644 drivers/vhost/iommufd.c diff --git a/drivers/vhost/iommufd.c b/drivers/vhost/iommufd.c new file mode 100644 index 000000000000..113dda50a9b6 --- /dev/null +++ b/drivers/vhost/iommufd.c @@ -0,0 +1,178 @@ +#include +#include + +#include "vhost.h" + +MODULE_IMPORT_NS(IOMMUFD); + +int vdpa_iommufd_bind(struct vdpa_device *vdpa, struct iommufd_ctx *ictx, + u32 *ioas_id, u32 *device_id) +{ + int ret; + + vhost_vdpa_lockdep_assert_held(vdpa); + + /* + * If the driver doesn't provide this op then it means the device does + * not do DMA at all. So nothing to do. + */ + if (!vdpa->config->bind_iommufd) + return 0; + ret = vdpa->config->bind_iommufd(vdpa, ictx, device_id); + if (ret) + return ret; + + return 0; +} + +void vdpa_iommufd_unbind(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->config->unbind_iommufd) + vdpa->config->unbind_iommufd(vdpa); +} + +int vdpa_iommufd_physical_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *out_device_id) +{ + struct device *dma_dev = vdpa_get_dma_dev(vdpa); + struct iommufd_device *idev; + + idev = iommufd_device_bind(ictx, dma_dev, out_device_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + vdpa->iommufd_device = idev; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_bind); + +void vdpa_iommufd_physical_unbind(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->iommufd_attached) { + iommufd_device_detach(vdpa->iommufd_device); + vdpa->iommufd_attached = false; + } + iommufd_device_unbind(vdpa->iommufd_device); + vdpa->iommufd_device = NULL; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_unbind); + +int vdpa_iommufd_physical_attach_ioas(struct vdpa_device *vdpa, + u32 *iommufd_ioasid) +{ + int rc; + + vhost_vdpa_lockdep_assert_held(vdpa); + + if (WARN_ON(!vdpa->iommufd_device)) + return -EINVAL; + + if (vdpa->iommufd_attached) + rc = iommufd_device_replace(vdpa->iommufd_device, + iommufd_ioasid); + else + rc = iommufd_device_attach(vdpa->iommufd_device, + iommufd_ioasid); + if (rc) + return rc; + vdpa->iommufd_attached = true; + + return 0; +} + +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_attach_ioas); +int vdpa_iommufd_physical_detach_ioas(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (WARN_ON(!vdpa->iommufd_device) || !vdpa->iommufd_attached) + return -1; + + iommufd_device_detach(vdpa->iommufd_device); + vdpa->iommufd_attached = false; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_physical_detach_ioas); + +static void vdpa_emulated_unmap(void *data, unsigned long iova, + unsigned long length) +{ + struct vdpa_device *vdpa = data; + /* todo: need to unmap the iova-lenth in all ASID*/ + + // vdpa->config->dma_unmap(vdpa, 0, iova, length); +} + +static const struct iommufd_access_ops vdpa_user_ops = { + .needs_pin_pages = 1, + .unmap = vdpa_emulated_unmap, +}; + +int vdpa_iommufd_emulated_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *out_device_id) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + struct iommufd_access *user; + + user = iommufd_access_create(ictx, &vdpa_user_ops, vdpa, out_device_id); + if (IS_ERR(user)) + return PTR_ERR(user); + vdpa->iommufd_access = user; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_bind); + +void vdpa_iommufd_emulated_unbind(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->iommufd_access) { + iommufd_access_destroy(vdpa->iommufd_access); + vdpa->iommufd_attached = false; + vdpa->iommufd_access = NULL; + } +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_unbind); + +int vdpa_iommufd_emulated_attach_ioas(struct vdpa_device *vdpa, + u32 *iommufd_ioasid) +{ + int rc; + + struct iommufd_access *user; + + vhost_vdpa_lockdep_assert_held(vdpa); + + if (vdpa->iommufd_attached) { + rc = iommufd_access_replace(vdpa->iommufd_access, + *iommufd_ioasid); + } else { + rc = iommufd_access_attach(vdpa->iommufd_access, + *iommufd_ioasid); + } + user = vdpa->iommufd_access; + + if (rc) + return rc; + vdpa->iommufd_attached = true; + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_attach_ioas); + +int vdpa_iommufd_emulated_detach_ioas(struct vdpa_device *vdpa) +{ + vhost_vdpa_lockdep_assert_held(vdpa); + + if (WARN_ON(!vdpa->iommufd_access) || !vdpa->iommufd_attached) + return -1; + + iommufd_access_detach(vdpa->iommufd_access); + vdpa->iommufd_attached = false; + + return 0; +} +EXPORT_SYMBOL_GPL(vdpa_iommufd_emulated_detach_ioas); diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index f60d5f7bef94..179012e350f9 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -310,6 +310,27 @@ static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) } #endif +struct iommufd_ctx; +struct vdpa_device; +void vhost_vdpa_lockdep_assert_held(struct vdpa_device *vdpa); + +#if IS_ENABLED(CONFIG_IOMMUFD) +int vdpa_iommufd_bind(struct vdpa_device *vdpa, struct iommufd_ctx *ictx, + u32 *ioas_id, u32 *device_id); +void vdpa_iommufd_unbind(struct vdpa_device *vdpa); +#else +static inline int vdpa_iommufd_bind(struct vdpa_device *vdpa, + struct iommufd_ctx *ictx, u32 *ioas_id, + u32 *device_id) +{ + return -EOPNOTSUPP; +} + +static inline void vdpa_iommufd_unbind(struct vdpa_device *vdpa) +{ +} +#endif + /* Memory accessors */ static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) { -- 2.34.3