From patchwork Wed May 3 09:13:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229924 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 6B958C77B78 for ; Wed, 3 May 2023 09:16:59 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8Zo-000369-FL; Wed, 03 May 2023 05:15:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8Zl-00030G-C1 for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8Zi-00073o-GY for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105301; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ryitY9tRg7IelP3yy4lFrjMVzUomwKT1eJ9sxT8lPmU=; b=VkTjXaLZvJdzz42za6NBOlRgY0zPRqxM7nbjXZ1F2AvVqRGlJKkuqEsZ93J8TI4otGxViE anOF9Z0iSHXGoUWnfqmFDWYG/SBe7aLLqMLWYjHhv8jZ7gRvt3tpjhAqi2ao6LAZz4P5+U pMoQ7YtDk6o1MgxXO02I47ghG1UcUYs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-152-CeYoq9pQNRGh6EaGJYaxeQ-1; Wed, 03 May 2023 05:13:45 -0400 X-MC-Unique: CeYoq9pQNRGh6EaGJYaxeQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C8069884EC3 for ; Wed, 3 May 2023 09:13:44 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id D6205492C13; Wed, 3 May 2023 09:13:42 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 1/7] vhost: introduce new UAPI to support IOMMUFD Date: Wed, 3 May 2023 17:13:31 +0800 Message-Id: <20230503091337.2130631-2-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass client-ip=170.10.133.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add 3 new UAPI VHOST_VDPA_SET_IOMMU_FD: this to bind the vdpa device to iommufd VDPA_DEVICE_ATTACH_IOMMUFD_AS: attach new ioas to iommufd VDPA_DEVICE_DTTACH_IOMMUFD_AS: detach all the ioas from iommufd Signed-off-by: Cindy Lu --- linux-headers/linux/vhost.h | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/linux-headers/linux/vhost.h b/linux-headers/linux/vhost.h index f9f115a7c7..bf426177f3 100644 --- a/linux-headers/linux/vhost.h +++ b/linux-headers/linux/vhost.h @@ -180,4 +180,76 @@ */ #define VHOST_VDPA_SUSPEND _IO(VHOST_VIRTIO, 0x7D) +/* vhost vdpa set iommufd + * Input parameters: + * @iommufd: file descriptor from /dev/iommu; pass -1 to unset + * @group_id: identifier of the group that a virtqueue belongs to + * @ioas_id: IOAS identifier returned from ioctl(IOMMU_IOAS_ALLOC) + * Output parameters: + * @out_dev_id: device identifier + * @out_hwpt_id: hardware IO pagetable identifier + */ +struct vhost_vdpa_set_iommufd { + __s32 iommufd; + __u32 group_id; + __u32 ioas_id; + __u32 out_devid; + __u32 out_hwptid; +}; + +#define VHOST_VDPA_SET_IOMMU_FD \ + _IOW(VHOST_VIRTIO, 0x7e, struct vhost_vdpa_set_iommufd) + +/* + * VDPA_DEVICE_ATTACH_IOMMUFD_AS - + * _IOW(VHOST_VIRTIO, 0x7f, struct vdpa_device_attach_iommufd_as) + * + * Attach a vdpa device to an iommufd address space specified by IOAS + * id. + * + * Available only after a device has been bound to iommufd via + * VHOST_VDPA_SET_IOMMU_FD + * + * Undo by VDPA_DEVICE_DETACH_IOMMUFD_AS or device fd close. + * + * @argsz: user filled size of this data. + * @flags: must be 0. + * @ioas_id: Input the target id which can represent an ioas + * allocated via iommufd subsystem. + * + * Return: 0 on success, -errno on failure. + */ +struct vdpa_device_attach_iommufd_as { + __u32 argsz; + __u32 flags; + __u32 ioas_id; +}; + +#define VDPA_DEVICE_ATTACH_IOMMUFD_AS \ + _IOW(VHOST_VIRTIO, 0x7f, struct vdpa_device_attach_iommufd_as) + + +/* + * VDPA_DEVICE_DETACH_IOMMUFD_AS + * + * Detach a vdpa device from the iommufd address space it has been + * attached to. After it, device should be in a blocking DMA state. + * + * Available only after a device has been bound to iommufd via + * VHOST_VDPA_SET_IOMMU_FD + * + * @argsz: user filled size of this data. + * @flags: must be 0. + * + * Return: 0 on success, -errno on failure. + */ +struct vdpa_device_detach_iommufd_as { + __u32 argsz; + __u32 flags; +}; + +#define VDPA_DEVICE_DETACH_IOMMUFD_AS \ + _IOW(VHOST_VIRTIO, 0x83, struct vdpa_device_detach_iommufd_as) + + #endif From patchwork Wed May 3 09:13:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229926 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id A5033C7EE26 for ; Wed, 3 May 2023 09:17:01 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8Zq-00039A-S4; Wed, 03 May 2023 05:15:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8Zl-00030i-I9 for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8Zj-00073V-9s for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105301; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7qh9Z7SDMxmfTTyiqO7zQepPLJ7V+DQzGPZswLsafXg=; b=Y8CTusgC8C2pHwBEMmY+y44e9GiG/ijpZyQtOZAdZ0LHCJvgAEeROMO2iJ+1dsei6NeRNL MuagzdFM3oKihokxyWQRaJzA03Sy1UkH8K/anUGE9l9o2ajYCA4D2pFmbAq2yawd0qqho1 78pMGJfsfk337Gyg1OlzDbSTEEdqYDg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-330-yiMj2dJAMMCFTESWMiJUPQ-1; Wed, 03 May 2023 05:13:47 -0400 X-MC-Unique: yiMj2dJAMMCFTESWMiJUPQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5EC4285530C for ; Wed, 3 May 2023 09:13:47 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6D6FD492C13; Wed, 3 May 2023 09:13:45 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 2/7] qapi: support iommufd in vdpa Date: Wed, 3 May 2023 17:13:32 +0800 Message-Id: <20230503091337.2130631-3-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass client-ip=170.10.129.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add a new option for iommufd, The usage is .... -object iommufd,id=iommufd0 \ -device virtio-net-pci,netdev=vhost-vdpa1,disable-legacy=on,disable-modern=off\ -netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa1,iommufd=iommufd0\ ... Signed-off-by: Cindy Lu --- qapi/net.json | 1 + 1 file changed, 1 insertion(+) diff --git a/qapi/net.json b/qapi/net.json index 522ac582ed..fffaf9bb5e 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -461,6 +461,7 @@ '*vhostdev': 'str', '*vhostfd': 'str', '*queues': 'int', + '*iommufd': 'str', '*x-svq': {'type': 'bool', 'features' : [ 'unstable'] } } } ## From patchwork Wed May 3 09:13:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229935 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 6B176C77B78 for ; Wed, 3 May 2023 09:17:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8Zn-00034r-MS; Wed, 03 May 2023 05:15:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8Zk-0002yV-HV for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8Zi-00073X-4r for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105301; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HmYRbCYzitazcterE+EAaoFJhU5tvGMhxgkBemRn5q0=; b=gj2xsK7/g1JaInTYPhqQm/cN00MTTzx/Ngom+w4APvgMg3HZWMB4L1c1m5J2kxiEF+q4tA mP10Qz28QBmAkhot9lg+Q/l9bxyTuS2hu987k1Dt17pedfpeI5VYbuYPUic+WX7Y+6Vrxx RrUTU8B2BbtKUIJ8Zlmn1CVRB/ODuew= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-558-ymbJDr8bPV2kQ58NdhHP1g-1; Wed, 03 May 2023 05:13:50 -0400 X-MC-Unique: ymbJDr8bPV2kQ58NdhHP1g-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E8B4E857FB7 for ; Wed, 3 May 2023 09:13:49 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id 039F5492C13; Wed, 3 May 2023 09:13:47 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 3/7] virtio : add a ptr for vdpa_iommufd in VirtIODevice Date: Wed, 3 May 2023 17:13:33 +0800 Message-Id: <20230503091337.2130631-4-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass client-ip=170.10.129.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org To support iommufd, vdpa needs to save the ioas_id and the ASID, which need to be shared between all vhost_vdpa devices. So Add a pointer in VirtIODevice. vdpa device need to init it when the dev start, Add all the vdpa device will read/write this same ptr. TODO: need to add a lock for read and write Signed-off-by: Cindy Lu --- include/hw/virtio/vhost-vdpa.h | 23 +++++++++++++++++++++++ include/hw/virtio/virtio.h | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h index 7997f09a8d..309d4ffc70 100644 --- a/include/hw/virtio/vhost-vdpa.h +++ b/include/hw/virtio/vhost-vdpa.h @@ -18,6 +18,10 @@ #include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" +//#include "sysemu/iommufd.h" +#include "qemu/osdep.h" +#include "sysemu/sysemu.h" + /* * ASID dedicated to map guest's addresses. If SVQ is disabled it maps GPA to @@ -30,6 +34,8 @@ typedef struct VhostVDPAHostNotifier { void *addr; } VhostVDPAHostNotifier; +typedef struct IOMMUFDBackend IOMMUFDBackend; + typedef struct vhost_vdpa { int device_fd; int index; @@ -51,6 +57,23 @@ typedef struct vhost_vdpa { VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX]; } VhostVDPA; + +typedef struct vdpa_iommufd { + IOMMUFDBackend *iommufd; + struct vhost_dev *dev; + /*ioas_id get from IOMMUFD, iommufd need to use this id to map,unmap*/ + uint32_t ioas_id; + /*ASID used for vq*/ + uint32_t asid; + __u32 devid; /*not use */ + __u32 hwptid; /*not use*/ + AddressSpace *as; + struct vdpa_iommufd *next; + // QLIST_ENTRY(vdpa_iommufd) iommufd_next; + +} VDPAIOMMUFDState; + + int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range); int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 77c6c55929..36b4783466 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -152,6 +152,11 @@ struct VirtIODevice uint8_t device_endian; bool use_guest_notifier_mask; AddressSpace *dma_as; + /*this is an ptr point to struct vdpa_iommufd, will change to QLIST if + * needed*/ + /*in this struct saved the ioas_id/ASID that we need to use in iommufd + map/unmap, this ioas_id/ASID will share between vqs,so we add the ptr here*/ + void *iommufd_ptr; QLIST_HEAD(, VirtQueue) *vector_queues; QTAILQ_ENTRY(VirtIODevice) next; EventNotifier config_notifier; From patchwork Wed May 3 09:13:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229929 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id C6F72C77B78 for ; Wed, 3 May 2023 09:17:17 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8aD-0004dY-Ii; Wed, 03 May 2023 05:15:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8aA-0004EP-MM for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8a8-0007Qs-KH for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105328; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+WLQCQHr3+R7LOMp/vZIq2FAN7t0z8GbCrXcfznFhD4=; b=W8M4NaEqOPeTmOt1VCn3s81zf+YoKV7/AIHnVd0LvBDg9AW6zllU1nmcyqfSgx11KL0kEU ngmQhaH/4nwNoW34y76H1AWgSnS9zkQXf/IasU6Ir1jwwOJp4BylqLIzWGx59fzHdVNIuU QFIMgUBWUwVvP3we8LdMM5VQD9zwrYE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-600-nGqaynikONGzdm8_qdfNUQ-1; Wed, 03 May 2023 05:14:14 -0400 X-MC-Unique: nGqaynikONGzdm8_qdfNUQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 19A5B811E7B for ; Wed, 3 May 2023 09:14:14 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id 283B51121331; Wed, 3 May 2023 09:14:11 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 4/7] net/vhost-vdpa: Add the check for iommufd Date: Wed, 3 May 2023 17:13:34 +0800 Message-Id: <20230503091337.2130631-5-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass client-ip=170.10.133.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add the check for object iommufd, if the iommfd enabled. pass the information to vhost_vdpa. vhost_vdpa dev start will check this bool and connect to the iommufd Signed-off-by: Cindy Lu --- net/vhost-vdpa.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index 1a13a34d35..d4819c28e1 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -659,16 +659,12 @@ static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = { .avail_handler = vhost_vdpa_net_handle_ctrl_avail, }; -static NetClientState *net_vhost_vdpa_init(NetClientState *peer, - const char *device, - const char *name, - int vdpa_device_fd, - int queue_pair_index, - int nvqs, - bool is_datapath, - bool svq, - struct vhost_vdpa_iova_range iova_range, - VhostIOVATree *iova_tree) +static NetClientState * +net_vhost_vdpa_init(NetClientState *peer, const char *device, const char *name, + int vdpa_device_fd, int queue_pair_index, int nvqs, + bool is_datapath, bool svq, bool enable_iommufd, + struct vhost_vdpa_iova_range iova_range, + VhostIOVATree *iova_tree) { NetClientState *nc = NULL; VhostVDPAState *s; @@ -691,6 +687,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.iova_range = iova_range; s->vhost_vdpa.shadow_data = svq; s->vhost_vdpa.iova_tree = iova_tree; + s->vhost_vdpa.enable_iommufd = enable_iommufd; if (!is_datapath) { s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(), vhost_vdpa_net_cvq_cmd_page_len()); @@ -793,6 +790,12 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, } } + bool enable_iommufd = false; + if (opts->iommufd) { + enable_iommufd = true; + printf("[%s] %d called\n", __func__, __LINE__); + } + r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp); if (unlikely(r < 0)) { goto err; @@ -825,15 +828,15 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, for (i = 0; i < queue_pairs; i++) { ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, vdpa_device_fd, i, 2, true, opts->x_svq, - iova_range, iova_tree); + enable_iommufd, iova_range, iova_tree); if (!ncs[i]) goto err; } if (has_cvq) { - nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, - vdpa_device_fd, i, 1, false, - opts->x_svq, iova_range, iova_tree); + nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, vdpa_device_fd, i, + 1, false, opts->x_svq, enable_iommufd, + iova_range, iova_tree); if (!nc) goto err; } From patchwork Wed May 3 09:13:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229927 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id E008CC77B78 for ; Wed, 3 May 2023 09:17:07 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8al-0007Ic-O4; Wed, 03 May 2023 05:16:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8ah-0007FW-RD for qemu-devel@nongnu.org; Wed, 03 May 2023 05:16:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8af-00084N-Rm for qemu-devel@nongnu.org; Wed, 03 May 2023 05:16:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105361; 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=tv9zdnzFPYflddWP3Ob5IxNNR8Pzq02z+BOdGTsuewc=; b=atIn5OuG8G1dz5Uyshh+Xllqxi1nA4NhVEDHs9cdWXw2BR0lTiB7Mg5XxPCQTM61wp7Fre UtBcDjNMTZYNv9bPUNYVgXYFpDbFlxMoLV+KLbzNpd0WuqQFhAlNeqvNtfmUvJLoSaAXLR PaYVGViBvGsNsvB4FOcSDJoVB98Hxec= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-299-vlTftgrgOLWKHvgTux1ZCg-1; Wed, 03 May 2023 05:14:16 -0400 X-MC-Unique: vlTftgrgOLWKHvgTux1ZCg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A36EF1C0A580 for ; Wed, 3 May 2023 09:14:16 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id B2DE91121331; Wed, 3 May 2023 09:14:14 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 5/7] vhost-vdpa: Add the iommufd support in the map/unmap function Date: Wed, 3 May 2023 17:13:35 +0800 Message-Id: <20230503091337.2130631-6-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass client-ip=170.10.133.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 1.Change the map/umap function to legacy_map/unmap 2. Add the check for iommufd support,    a>If support iommufd, call the iommufd-related function   b>In order to use kernel's iotlb process. Still need to call     the legacy mode iotlb message, Kernel will check and      skip the legacy iotlb message if iommufd enable Signed-off-by: Cindy Lu --- hw/virtio/vhost-vdpa.c | 56 ++++++++++++++++++++++++++++++---- include/hw/virtio/vhost-vdpa.h | 24 +++++++++++++++ 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 542e003101..85240926b2 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -26,6 +26,7 @@ #include "cpu.h" #include "trace.h" #include "qapi/error.h" +#include "sysemu/iommufd.h" /* * Return one past the end of the end of section. Be careful with uint64_t @@ -76,8 +77,9 @@ static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection *section, * The caller must set asid = 0 if the device does not support asid. * This is not an ABI break since it is set to 0 by the initializer anyway. */ -int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, - hwaddr size, void *vaddr, bool readonly) + +int vhost_vdpa_leagcy_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, + hwaddr size, void *vaddr, bool readonly) { struct vhost_msg_v2 msg = {}; int fd = v->device_fd; @@ -103,13 +105,32 @@ int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, return ret; } +int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, + hwaddr size, void *vaddr, bool readonly) +{ + struct vhost_dev *dev = v->dev; + + if ((v->enable_iommufd) && (v->ops == NULL)) { + vdpa_backend_iommufd_ops_class_init(v); + } + + struct vdpa_iommu_backend_ops *ops = v->ops; + /* inoder to reuse the iotlb prcess to in kernel, still need to call leagcy + mode mapping but in kernel , the leagcy mode mapping was replace by + iommufd*/ + if (v->enable_iommufd) { + ops->dma_map(dev, asid, iova, size, vaddr, readonly); + } + return vhost_vdpa_leagcy_dma_map(v, asid, iova, size, vaddr, readonly); +} /* * The caller must set asid = 0 if the device does not support asid. * This is not an ABI break since it is set to 0 by the initializer anyway. */ -int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, - hwaddr size) + +int vhost_vdpa_leagcy_dma_unmap(struct vhost_vdpa *v, uint32_t asid, + hwaddr iova, hwaddr size) { struct vhost_msg_v2 msg = {}; int fd = v->device_fd; @@ -132,6 +153,26 @@ int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, return ret; } +int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, + hwaddr size) +{ + struct vhost_dev *dev = v->dev; + + if ((v->enable_iommufd) && (v->ops == NULL)) { + vdpa_backend_iommufd_ops_class_init(v); + } + + + /* inoder to reuse the iotlb prcess to in kernel, still need to call leagcy + mode mapping but in kernel , the leagcy mode mapping was replace by + iommufd*/ + if (v->enable_iommufd) { + struct vdpa_iommu_backend_ops *ops = v->ops; + + ops->dma_unmap(dev, asid, iova, size); + } + return vhost_vdpa_leagcy_dma_unmap(v, asid, iova, size); +} static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v) { @@ -423,13 +464,14 @@ static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v) v->shadow_vqs = g_steal_pointer(&shadow_vqs); } - +int g_iommufd; static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) { struct vhost_vdpa *v; assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); trace_vhost_vdpa_init(dev, opaque); int ret; + printf("[%s] %d called\n", __func__, __LINE__); /* * Similar to VFIO, we end up pinning all guest memory and have to @@ -580,7 +622,9 @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev) vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); memory_listener_unregister(&v->listener); vhost_vdpa_svq_cleanup(dev); - + if (vhost_vdpa_first_dev(dev)) { + v->ops->detach_device(v); + } dev->opaque = NULL; ram_block_discard_disable(false); diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h index 309d4ffc70..aa0e3ed65b 100644 --- a/include/hw/virtio/vhost-vdpa.h +++ b/include/hw/virtio/vhost-vdpa.h @@ -55,6 +55,10 @@ typedef struct vhost_vdpa { void *shadow_vq_ops_opaque; struct vhost_dev *dev; VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX]; + /*iommufd related*/ + struct vdpa_iommu_backend_ops *ops; + bool enable_iommufd; + } VhostVDPA; @@ -76,9 +80,29 @@ typedef struct vdpa_iommufd { int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range); +int vhost_vdpa_leagcy_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, + hwaddr size, void *vaddr, bool readonly); +int vhost_vdpa_leagcy_dma_unmap(struct vhost_vdpa *v, uint32_t asid, + hwaddr iova, hwaddr size); + int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, hwaddr size, void *vaddr, bool readonly); int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, hwaddr size); +struct vdpa_iommu_backend_ops { + /*< private >*/ + ObjectClass parent_class; + int (*dma_map)(struct vhost_dev *dev, uint32_t asid, hwaddr iova, + hwaddr size, void *vaddr, bool readonly); + int (*dma_unmap)(struct vhost_dev *dev, uint32_t asid, hwaddr iova, + hwaddr size); + int (*attach_device)(struct vhost_vdpa *dev, AddressSpace *as, + Error **errp); + void (*detach_device)(struct vhost_vdpa *dev); + int (*reset)(VDPAIOMMUFDState *vdpa_iommufd); +}; + +void vdpa_backend_iommufd_ops_class_init(struct vhost_vdpa *v); + #endif From patchwork Wed May 3 09:13:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229928 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 4EB1AC77B7F for ; Wed, 3 May 2023 09:17:09 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8aE-0004sF-J1; Wed, 03 May 2023 05:15:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8a9-0003z8-Dh for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8a6-0007QI-Fi for qemu-devel@nongnu.org; Wed, 03 May 2023 05:15:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105325; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iebPCkg5eFFs1qB8vOt/Pycrcm/HX0yYXVK1rX33ArI=; b=dJBhlp7V2xcFrq1TNxpdsUrL0o7DFxmM4tBQuqyPHuMb9fhIcZrOa5yrjCzzfLRQX4uRGz 97NT1PEn7AS/GWgbHJTdSby8z/wQ0oFjQR09kOnib1gj1sS7EyVSW3+nR+Rz9tIXKnGKT9 IR21o9Rl7CmDt2+/Dlh1NtAzQzqqdm4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-664-6Wc4FjAOMcOhlBcl6x9lCQ-1; Wed, 03 May 2023 05:14:19 -0400 X-MC-Unique: 6Wc4FjAOMcOhlBcl6x9lCQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39B8385A5B1 for ; Wed, 3 May 2023 09:14:19 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4872B1121331; Wed, 3 May 2023 09:14:16 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 6/7] vhost-vdpa: init iommufd function in vhost_vdpa start Date: Wed, 3 May 2023 17:13:36 +0800 Message-Id: <20230503091337.2130631-7-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass client-ip=170.10.129.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add support for iommufd, init the vdpa_iommufd in vdpa_start in this step, driver will bind to the iommufd device and attach the default ASID(asid 0) to iommufd Signed-off-by: Cindy Lu --- hw/virtio/vhost-vdpa.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 85240926b2..6c01e3b44f 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1158,6 +1158,24 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) trace_vhost_vdpa_dev_start(dev, started); if (started) { + if ((v->enable_iommufd) && (vhost_vdpa_first_dev(dev))) { + struct vdpa_iommufd *vdpa_iommufd; + + vdpa_backend_iommufd_ops_class_init(v); + + if (dev->vdev->iommufd_ptr == NULL) { + vdpa_iommufd = g_malloc(sizeof(VDPAIOMMUFDState)); + + vdpa_iommufd->iommufd = g_malloc(sizeof(IOMMUFDBackend)); + dev->vdev->iommufd_ptr = vdpa_iommufd; + + qemu_mutex_init(&vdpa_iommufd->iommufd->lock); + iommufd_backend_connect(vdpa_iommufd->iommufd, NULL); + + v->ops->attach_device(v, dev->vdev->dma_as, NULL); + } + } + vhost_vdpa_host_notifiers_init(dev); ok = vhost_vdpa_svqs_start(dev); if (unlikely(!ok)) { From patchwork Wed May 3 09:13:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 13229930 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 14364C77B7F for ; Wed, 3 May 2023 09:17:19 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pu8au-0007VT-8t; Wed, 03 May 2023 05:16:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8as-0007Tm-Vc for qemu-devel@nongnu.org; Wed, 03 May 2023 05:16:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pu8aq-0008Hs-SH for qemu-devel@nongnu.org; Wed, 03 May 2023 05:16:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683105372; 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=8tXpMpIWR7EehLxorBcS/d7hy6TsGogWnQ3nyCwcUeM=; b=SbvR9wnH5WT2BytxjmljfR14pR9pUO7hW0Ft6BvC1wOZ4Ml5yO70mXttm7uVdEBO5Zuaz9 FNXUYnZfR2yRRaPY5KMVMx0/C8RfzgNnRWLP4tUG9hpQkeRWIar/aEpZ6uvSJdTKbGJ0BE u8TP5Btsa0k70FVeMpdvbvRHtoWQZE8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-48-pANSXla8M0i9qP61dGFRnA-1; Wed, 03 May 2023 05:14:22 -0400 X-MC-Unique: pANSXla8M0i9qP61dGFRnA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C38F7A0F385 for ; Wed, 3 May 2023 09:14:21 +0000 (UTC) Received: from server.redhat.com (ovpn-12-29.pek2.redhat.com [10.72.12.29]) by smtp.corp.redhat.com (Postfix) with ESMTP id D35171121331; Wed, 3 May 2023 09:14:19 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Subject: [RFC 7/7] vhost-vdpa-iommufd: Add iommufd support for vdpa Date: Wed, 3 May 2023 17:13:37 +0800 Message-Id: <20230503091337.2130631-8-lulu@redhat.com> In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com> References: <20230503091337.2130631-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass client-ip=170.10.129.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.171, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This file is support iommufd for vdpa, including the function: 1> iommufd bind/unbind the iommufd device  bind the vdpa device to iommufd and attach the ASID 0 to iommufd 2> iommufd map/unmap function.The map function working process is    a. Check if the asid was used before.    b. If this is the new asid,  get the new ioas_id and attach it to iommufd.        save this information in vdpa_iommufd.    c. Use the ioas_id for mapping    The unmap logic is the same Signed-off-by: Cindy Lu --- hw/virtio/meson.build | 2 +- hw/virtio/vhost-vdpa-iommufd.c | 240 +++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-vdpa-iommufd.c diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index f93be2e137..848fdb18eb 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -13,7 +13,7 @@ if have_vhost specific_virtio_ss.add(files('vhost-user.c')) endif if have_vhost_vdpa - specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c')) + specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c','vhost-vdpa-iommufd.c')) endif else softmmu_virtio_ss.add(files('vhost-stub.c')) diff --git a/hw/virtio/vhost-vdpa-iommufd.c b/hw/virtio/vhost-vdpa-iommufd.c new file mode 100644 index 0000000000..6a0875c0a4 --- /dev/null +++ b/hw/virtio/vhost-vdpa-iommufd.c @@ -0,0 +1,240 @@ + +#include "qemu/osdep.h" +#include +#include +#include +#include +#include "sysemu/iommufd.h" +#include "hw/virtio/vhost.h" + +#include "hw/virtio/vhost-vdpa.h" + +static int vdpa_device_attach_ioas(struct vhost_vdpa *dev, + VDPAIOMMUFDState *vdpa_iommufd) +{ + int ret; + + struct vdpa_device_attach_iommufd_as attach_data = { + .argsz = sizeof(attach_data), + .flags = 0, + .ioas_id = vdpa_iommufd->ioas_id, + }; + /* Attach device to an ioas within iommufd */ + ret = ioctl(dev->device_fd, VDPA_DEVICE_ATTACH_IOMMUFD_AS, &attach_data); + if (ret) { + error_report("fail to bind device fd=%d to ioas_id=%d", dev->device_fd, + vdpa_iommufd->ioas_id); + return ret; + } + + return 0; +} +static VDPAIOMMUFDState *vdpa_get_ioas_by_asid(struct vhost_dev *hdev, + uint32_t asid) +{ + VDPAIOMMUFDState *vdpa_iommufd_ptr = hdev->vdev->iommufd_ptr; + while (vdpa_iommufd_ptr != NULL) { + if (asid == vdpa_iommufd_ptr->asid) { + return vdpa_iommufd_ptr; + } + + vdpa_iommufd_ptr = vdpa_iommufd_ptr->next; + } + + return NULL; +} +static VDPAIOMMUFDState *vdpa_add_new_ioas_id(struct vhost_dev *hdev, + uint32_t asid) +{ + int ret; + uint32_t ioas_id; + + struct vhost_vdpa *v = hdev->opaque; + VDPAIOMMUFDState *vdpa_iommufd_ptr = hdev->vdev->iommufd_ptr; + VDPAIOMMUFDState *vdpa_iommufd_new = g_malloc(sizeof(VDPAIOMMUFDState)); + + vdpa_iommufd_new->dev = hdev; + vdpa_iommufd_new->asid = asid; + vdpa_iommufd_new->iommufd = vdpa_iommufd_ptr->iommufd; + + ret = iommufd_backend_get_ioas(vdpa_iommufd_new->iommufd, &ioas_id); + if (ret < 0) { + error_report("Failed to alloc ioas (%s)", strerror(errno)); + return NULL; + } + + vdpa_iommufd_new->ioas_id = ioas_id; + /* this is new asid, attch to iommufd*/ + ret = vdpa_device_attach_ioas(v, vdpa_iommufd_new); + if (ret < 0) { + error_report("Failed to attach ioas (%s)", strerror(errno)); + return NULL; + } + while (vdpa_iommufd_ptr->next != NULL) { + vdpa_iommufd_ptr = vdpa_iommufd_ptr->next; + } + /*save this vdpa_iommufd in list */ + vdpa_iommufd_ptr->next = vdpa_iommufd_new; + vdpa_iommufd_new->next = NULL; + return vdpa_iommufd_new; +} +static int vdpa_iommufd_map(struct vhost_dev *hdev, uint32_t asid, hwaddr iova, + hwaddr size, void *vaddr, bool readonly) +{ + VDPAIOMMUFDState *vdpa_iommufd; + + if (hdev->vdev == NULL) { + error_report("Failed to get vdev (%s)", strerror(errno)); + return 0; + } + /*search if this asid have attach to iommufd before*/ + vdpa_iommufd = vdpa_get_ioas_by_asid(hdev, asid); + if (vdpa_iommufd == NULL) { + /*this asid is first use, need to alloc and add to iommufd*/ + vdpa_iommufd = vdpa_add_new_ioas_id(hdev, asid); + } + return iommufd_backend_map_dma(vdpa_iommufd->iommufd, vdpa_iommufd->ioas_id, + iova, size, vaddr, readonly); +} + + +static int vdpa_iommufd_unmap(struct vhost_dev *hdev, uint32_t asid, + hwaddr iova, hwaddr size) +{ + VDPAIOMMUFDState *vdpa_iommufd; + if (hdev->vdev == NULL) { + error_report("Failed to get vdev (%s)", strerror(errno)); + return 0; + } + /*search if this asid have attach to iommufd before*/ + + vdpa_iommufd = vdpa_get_ioas_by_asid(hdev, asid); + if (vdpa_iommufd == NULL) { + error_report("Failed to get ioas (%s)", strerror(errno)); + return 0; + } + return iommufd_backend_unmap_dma(vdpa_iommufd->iommufd, + vdpa_iommufd->ioas_id, iova, size); +} + + +static void vdpa_device_detach_iommufd(struct vhost_vdpa *v, + VDPAIOMMUFDState *vdpa_iommufd, + Error **errp) +{ + struct vdpa_device_detach_iommufd_as detach_data = { + .argsz = sizeof(detach_data), + .flags = 0, + }; + + if (ioctl(v->device_fd, VDPA_DEVICE_DETACH_IOMMUFD_AS, &detach_data)) { + error_report("error bind device fd=%d ", v->device_fd); + return; + } +} + + +static int vdpa_device_bind_iommufd(struct vhost_vdpa *dev, + VDPAIOMMUFDState *vdpa_iommufd, + Error **errp) +{ + struct vhost_vdpa_set_iommufd bind = { + .iommufd = vdpa_iommufd->iommufd->fd, + .ioas_id = vdpa_iommufd->ioas_id, + }; + + int ret; + /* Bind device to iommufd */ + ret = ioctl(dev->device_fd, VHOST_VDPA_SET_IOMMU_FD, &bind); + if (ret) { + error_report("error bind device fd=%d to iommufd=%d", dev->device_fd, + bind.iommufd); + return ret; + } + + vdpa_iommufd->devid = bind.out_devid; + vdpa_iommufd->hwptid = bind.out_hwptid; + + return vdpa_device_attach_ioas(dev, vdpa_iommufd); +} + +static void vdpa_iommufd_destroy(VDPAIOMMUFDState *vdpa_iommufd) +{ + g_free(vdpa_iommufd); +} + +/*attach the device to iommufd */ +static int vdpa_iommufd_attach_device(struct vhost_vdpa *v, AddressSpace *as, + Error **errp) +{ + VDPAIOMMUFDState *vdpa_iommufd; + int ret; + uint32_t ioas_id; + Error *err = NULL; + struct vhost_dev *dev = v->dev; + vdpa_iommufd = dev->vdev->iommufd_ptr; + + /*allocate a new IOAS */ + ret = iommufd_backend_get_ioas(vdpa_iommufd->iommufd, &ioas_id); + if (ret < 0) { + close(v->device_fd); + error_report("Failed to alloc ioas (%s)", strerror(errno)); + return ret; + } + + vdpa_iommufd->ioas_id = ioas_id; + vdpa_iommufd->dev = dev; + /* use the default ASID*/ + vdpa_iommufd->asid = VHOST_VDPA_GUEST_PA_ASID; + vdpa_iommufd->next = NULL; + + vdpa_iommufd->as = as; + /*bind the default ASID to iommufd*/ + ret = vdpa_device_bind_iommufd(v, vdpa_iommufd, &err); + if (ret) { + /* todo check if fail */ + error_report("Failed to vdpa_device_bind_iommufd (%s)", + strerror(errno)); + iommufd_backend_put_ioas(vdpa_iommufd->iommufd, ioas_id); + + vdpa_iommufd_destroy(vdpa_iommufd); + return ret; + } + + return ret; +} + +static void vdpa_iommufd_detach_device(struct vhost_vdpa *v) +{ + VDPAIOMMUFDState *vdpa_iommufd; + + VDPAIOMMUFDState *vdpa_iommufd_tmp; + Error *err = NULL; + + struct vhost_dev *dev = v->dev; + if (!dev->vdev) { + return; + } + vdpa_iommufd = dev->vdev->iommufd_ptr; + vdpa_device_detach_iommufd(v, vdpa_iommufd, &err); + + while (vdpa_iommufd != NULL) { + iommufd_backend_put_ioas(vdpa_iommufd->iommufd, vdpa_iommufd->ioas_id); + vdpa_iommufd_tmp = vdpa_iommufd; + vdpa_iommufd = vdpa_iommufd->next; + + vdpa_iommufd_destroy(vdpa_iommufd_tmp); + } +} + +struct vdpa_iommu_backend_ops iommufd_ops = { + .dma_map = vdpa_iommufd_map, + .dma_unmap = vdpa_iommufd_unmap, + .attach_device = vdpa_iommufd_attach_device, + .detach_device = vdpa_iommufd_detach_device, +}; + +void vdpa_backend_iommufd_ops_class_init(struct vhost_vdpa *v) +{ + v->ops = &iommufd_ops; +}