From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105419; cv=none; d=zohomail.com; s=zohoarc; b=HRtI3BjB1uqD6UhWGrCHwDA6GMEixpPQKhf4TaZyUmo+VpX+UjY85bBwOSWUdNDLQ9u4dBjiArwOCYmKxLQm5s+n4rti8E69R5Iou+5Z33ZjQo3tMv71K8ud9bROgWqbtCoQKbzPNrHjEBIaxtd0SKX9Glvcp2NIG6VSBdrmd8Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105419; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ryitY9tRg7IelP3yy4lFrjMVzUomwKT1eJ9sxT8lPmU=; b=YzR5Uodx24rqupdTFiNwH/TlpITn/OMsnRDIsAsQCvGe3vPlLvqJDAnsiVIm/J4hpAkFjmVcp7vchDxSDKDGEcY6lSnJsuFxhevTqKIQdbnWWggt96X2RadyIEsZY2qlF5biFX4ZZVx8MN4/Yoy6G4AhjF5YGQFm2vQhIv+7EKE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105419676301.11832095200657; Wed, 3 May 2023 02:16:59 -0700 (PDT) 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 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 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) 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= X-MC-Unique: CeYoq9pQNRGh6EaGJYaxeQ-1 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105421173100005 Content-Type: text/plain; charset="utf-8" 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) =20 +/* 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 --=20 2.34.3 From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105421; cv=none; d=zohomail.com; s=zohoarc; b=d6uk2i0YfP6i9rwnU3LSAArtqCX9KdIBQabKQD8ovqklVLXRMj0E9VsOBn/bXxH+zz9VcFkyzy2NY+lUQn231tsDotS8v89TB50scMfk1cXjVaaUHbQFys3u3fYD57qVmori/AHfAyo+3W8WwBNt7VjLGa5bQHlswlGGP/CMf9I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105421; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7qh9Z7SDMxmfTTyiqO7zQepPLJ7V+DQzGPZswLsafXg=; b=U+qtbeH7gr1+ZNKTdULo5/EIsqaxKz+7kvQFqVJsPhQeH1HahVoO5ltWkbi/bWdF57rkHxvJw78/E7+MvsoKexCXEh7rjtBa717auIZv6SPNuoWj3WRHzCOIj7CDb26mV9wx+M6XJ44I3XDIonamezx7uB674BeiUJyumVSaZxQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105421808982.3362828893767; Wed, 3 May 2023 02:17:01 -0700 (PDT) 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 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 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) 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= X-MC-Unique: yiMj2dJAMMCFTESWMiJUPQ-1 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105422379100009 Content-Type: text/plain; charset="utf-8" Add a new option for iommufd, The usage is .... -object iommufd,id=3Diommufd0 \ -device virtio-net-pci,netdev=3Dvhost-vdpa1,disable-legacy=3Don,disable-mod= ern=3Doff\ -netdev type=3Dvhost-vdpa,vhostdev=3D/dev/vhost-vdpa-0,id=3Dvhost-vdpa1,iom= mufd=3Diommufd0\ ... 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'] } } } =20 ## --=20 2.34.3 From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105459; cv=none; d=zohomail.com; s=zohoarc; b=Qi2r1lpYTTWFN1fmVYWoJSKcnxsF46cS0sVEQarW4hSWBXvPprwECRjBNYNtySzsxY73Qi0VrB2leDC/utFdjwElPJA6NrvUP4gmk/CtWnt35AuJtquCNLoyKB+N3CFncw8w8xyhWGynS+qbfPPisdjCyXkOeiI6GQpDd0ij5WQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105459; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HmYRbCYzitazcterE+EAaoFJhU5tvGMhxgkBemRn5q0=; b=DHPWACdecMCYXdkd81eCmB2f+qAvPqFlsgG7XnEC9MnwE8/S8PfR9Z5cV08cXTCvwmtuneDir50JDZl0G+C366hM0F7B3Z6XhKnwHMZU55w9elt7A6SJ1XDsBYttT1Lp6K7kM5gsf+8FfKYzmtmxAY8VFIBpgrep6jyXCCveniM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105459514371.3259140938244; Wed, 3 May 2023 02:17:39 -0700 (PDT) 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 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 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) 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= X-MC-Unique: ymbJDr8bPV2kQ58NdhHP1g-1 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105461388100002 Content-Type: text/plain; charset="utf-8" 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" + =20 /* * ASID dedicated to map guest's addresses. If SVQ is disabled it maps GP= A to @@ -30,6 +34,8 @@ typedef struct VhostVDPAHostNotifier { void *addr; } VhostVDPAHostNotifier; =20 +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; =20 + +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_r= ange); =20 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; --=20 2.34.3 From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105437; cv=none; d=zohomail.com; s=zohoarc; b=cBf/pReUS3llbJr7OdtxdPcFXum3xhOa3v538I7bNgvZ6wOrn5MtzerWu6SGCbCzR1nJUhNj4TRodSalEXLbd4Lgql/TyrX3Bvb3dMLJ6Llb0xssmjNkACLG6WgfUXbdnT/me8KII/OFL9c74gBZEQBsUA5OnykfOn9xmAQv6Q8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105437; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+WLQCQHr3+R7LOMp/vZIq2FAN7t0z8GbCrXcfznFhD4=; b=bhmZHJ3GBIFyNgg6GylyUrKQjbRZdv6sU8pHpKJiefMbYahR9+ZNk/Pc8FMKcGPHFn9K6sCEBU3o22RkgJocelXZP5ymML1DVgnzgQ+NxvkDyF+/ZMWyBk9tPI3YVnD8bepd6G7X4IM6rMzMyzNIN60yZP5o03OV9GJBqQWbTPc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105437961653.9190029812027; Wed, 3 May 2023 02:17:17 -0700 (PDT) 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 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 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) 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= X-MC-Unique: nGqaynikONGzdm8_qdfNUQ-1 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105439273100002 Content-Type: text/plain; charset="utf-8" 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_s= vq_ops =3D { .avail_handler =3D vhost_vdpa_net_handle_ctrl_avail, }; =20 -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_r= ange, - 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 =3D NULL; VhostVDPAState *s; @@ -691,6 +687,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientSta= te *peer, s->vhost_vdpa.iova_range =3D iova_range; s->vhost_vdpa.shadow_data =3D svq; s->vhost_vdpa.iova_tree =3D iova_tree; + s->vhost_vdpa.enable_iommufd =3D enable_iommufd; if (!is_datapath) { s->cvq_cmd_out_buffer =3D qemu_memalign(qemu_real_host_page_size(), vhost_vdpa_net_cvq_cmd_page_le= n()); @@ -793,6 +790,12 @@ int net_init_vhost_vdpa(const Netdev *netdev, const ch= ar *name, } } =20 + bool enable_iommufd =3D false; + if (opts->iommufd) { + enable_iommufd =3D true; + printf("[%s] %d called\n", __func__, __LINE__); + } + r =3D 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 c= har *name, for (i =3D 0; i < queue_pairs; i++) { ncs[i] =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, vdpa_device_fd, i, 2, true, opts->x_s= vq, - iova_range, iova_tree); + enable_iommufd, iova_range, iova_tree= ); if (!ncs[i]) goto err; } =20 if (has_cvq) { - nc =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, - vdpa_device_fd, i, 1, false, - opts->x_svq, iova_range, iova_tree); + nc =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, vdpa_devic= e_fd, i, + 1, false, opts->x_svq, enable_iommufd, + iova_range, iova_tree); if (!nc) goto err; } --=20 2.34.3 From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105427; cv=none; d=zohomail.com; s=zohoarc; b=gKUy7uFE7QUsfnOwqUvxS2Nx2uTXHXIXZk8xzVWn29+1PTIC623kJchrWEk0ldUBv0oHwzuEsCPqFziUMFgjMLTWJ8aOnP0GHtqBcMy418v0OqcrEG+0AvhcMTw/V0p4pfj17jClmBputtV6igoc7t1XnToETRJ7OTzB+OcP0y8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105427; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tv9zdnzFPYflddWP3Ob5IxNNR8Pzq02z+BOdGTsuewc=; b=C9wo3UPOmG2YkclzFEL+jsVdOOnKErcYeuUVC5NCuy0qwM4EMK1GJbV/TZWXnSYCTpc15H9AdFHYdKdh8D1GOY5K5vQ60HB5CUmjM0XkXq1c6WTJbiqyvNWhFi70kHZkE6+2Bs9DyhaQ8Ib0Uzo7w8fok8xjC7OLhPAGV05tyQ0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105427804609.9795131846637; Wed, 3 May 2023 02:17:07 -0700 (PDT) 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 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 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) 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= X-MC-Unique: vlTftgrgOLWKHvgTux1ZCg-1 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105428911100005 1.Change the map/umap function to legacy_map/unmap 2. Add the check for iommufd support, =C2=A0 =C2=A0a>If support iommufd, call the iommufd-related function =C2=A0 b>In order to use kernel's iotlb process. Still need to call =C2=A0 =C2=A0 the legacy mode iotlb message, Kernel will check and =C2=A0 =C2=A0 =C2=A0skip 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" =20 /* * 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(MemoryReg= ionSection *section, * The caller must set asid =3D 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 =3D {}; int fd =3D v->device_fd; @@ -103,13 +105,32 @@ int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t= asid, hwaddr iova, =20 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 =3D v->dev; + + if ((v->enable_iommufd) && (v->ops =3D=3D NULL)) { + vdpa_backend_iommufd_ops_class_init(v); + } + + struct vdpa_iommu_backend_ops *ops =3D v->ops; + /* inoder to reuse the iotlb prcess to in kernel, still need to call l= eagcy + 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); +} =20 /* * The caller must set asid =3D 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 =3D {}; int fd =3D v->device_fd; @@ -132,6 +153,26 @@ int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_= t asid, hwaddr iova, =20 return ret; } +int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, + hwaddr size) +{ + struct vhost_dev *dev =3D v->dev; + + if ((v->enable_iommufd) && (v->ops =3D=3D NULL)) { + vdpa_backend_iommufd_ops_class_init(v); + } + + + /* inoder to reuse the iotlb prcess to in kernel, still need to call l= eagcy + mode mapping but in kernel , the leagcy mode mapping was replace by + iommufd*/ + if (v->enable_iommufd) { + struct vdpa_iommu_backend_ops *ops =3D v->ops; + + ops->dma_unmap(dev, asid, iova, size); + } + return vhost_vdpa_leagcy_dma_unmap(v, asid, iova, size); +} =20 static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v) { @@ -423,13 +464,14 @@ static void vhost_vdpa_init_svq(struct vhost_dev *hde= v, struct vhost_vdpa *v) =20 v->shadow_vqs =3D g_steal_pointer(&shadow_vqs); } - +int g_iommufd; static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **er= rp) { struct vhost_vdpa *v; assert(dev->vhost_ops->backend_type =3D=3D VHOST_BACKEND_TYPE_VDPA); trace_vhost_vdpa_init(dev, opaque); int ret; + printf("[%s] %d called\n", __func__, __LINE__); =20 /* * 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 =3D NULL; ram_block_discard_disable(false); =20 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; =20 =20 @@ -76,9 +80,29 @@ typedef struct vdpa_iommufd { =20 int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_r= ange); =20 +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); =20 +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 --=20 2.34.3 From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105429; cv=none; d=zohomail.com; s=zohoarc; b=EkB/ktScZzDOb1hCWrP6IOlKLB+tMKQoXMpGoBylC3+pjJZBIS+9mWSeXrPSne4wPsAc4Ia72CDX/lrou1WzxisLKPcuzbma7yKbwMLJ4J2/S8rgLlWBN3f9YsnzoZdumi+wHJRVoLqcXElGrFLbLZCQs/PijqF5ws+zFJbgDGg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105429; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iebPCkg5eFFs1qB8vOt/Pycrcm/HX0yYXVK1rX33ArI=; b=j8gCz6HSbt+WRGerlgSCKTRa/OjyuYedTFeIzJOmz5vLbmkxyfGXV7wWpo5qbivAuL3P7IqsARHs4zYj0r+xw9giWg+aQwpKJn6NgoGjtMwYuzHphLGG4sjY0o0LU7iqDUrGL6eGLk7RnhABBgzvE0NrLdQr5RXDbMWGTmIrjZE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105429554876.3403567810415; Wed, 3 May 2023 02:17:09 -0700 (PDT) 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 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 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) 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= X-MC-Unique: 6Wc4FjAOMcOhlBcl6x9lCQ-1 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105431210100011 Content-Type: text/plain; charset="utf-8" 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 *de= v, bool started) trace_vhost_vdpa_dev_start(dev, started); =20 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 =3D=3D NULL) { + vdpa_iommufd =3D g_malloc(sizeof(VDPAIOMMUFDState)); + + vdpa_iommufd->iommufd =3D g_malloc(sizeof(IOMMUFDBackend)); + dev->vdev->iommufd_ptr =3D 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 =3D vhost_vdpa_svqs_start(dev); if (unlikely(!ok)) { --=20 2.34.3 From nobody Tue May 14 11:21:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683105439; cv=none; d=zohomail.com; s=zohoarc; b=PleQKZLrwd7O+4k4vXbMQa8JF+Ph/mJuLjkBfIizhu3XmrWmV1L1425amSIcSRjebUSDUZWLTVzneQIwbiySHXXnq0IUeCsHX4dFSvIaRMrUzo4or6ZnQi7Alx9o+ucGiih9zv6LeJd8DY6O3L6BZmTZesrVsYXSYRRgvp1icKA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683105439; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8tXpMpIWR7EehLxorBcS/d7hy6TsGogWnQ3nyCwcUeM=; b=MsbPGxXcBNqCW3RSykB9v2TqS5K0PdrZ55nfaqqYGYXPiA2Gf5h/xvEUsxoRLbD8rYIxu/jUwW8tWYsDIfNwcCjd7LAl8QKFHahwUExz32LkqHPm9VSqU1zOlcfK/eB0c1C3FW+CLbsT1X4uZaYji5wrEGYE0mi7IgBf14anRhc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1683105439252390.84977629620846; Wed, 3 May 2023 02:17:19 -0700 (PDT) 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 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 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) 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= X-MC-Unique: pANSXla8M0i9qP61dGFRnA-1 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683105440816100008 This file is support iommufd for vdpa, including the function: 1> iommufd bind/unbind the iommufd device=C2=A0 bind the vdpa device to iommufd and attach the ASID 0 to iommufd 2> iommufd map/unmap function.The map function working process is =C2=A0 =C2=A0a. Check if the asid=C2=A0was used before. =C2=A0 =C2=A0b. If this is the new asid,=C2=A0 get the new ioas_id and atta= ch it to iommufd. =C2=A0 =C2=A0 =C2=A0 =C2=A0save this information in vdpa_iommufd. =C2=A0 =C2=A0c. Use the ioas_id for mapping =C2=A0 =C2=A0The 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 =3D { + .argsz =3D sizeof(attach_data), + .flags =3D 0, + .ioas_id =3D vdpa_iommufd->ioas_id, + }; + /* Attach device to an ioas within iommufd */ + ret =3D ioctl(dev->device_fd, VDPA_DEVICE_ATTACH_IOMMUFD_AS, &attach_d= ata); + if (ret) { + error_report("fail to bind device fd=3D%d to ioas_id=3D%d", dev->d= evice_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 =3D hdev->vdev->iommufd_ptr; + while (vdpa_iommufd_ptr !=3D NULL) { + if (asid =3D=3D vdpa_iommufd_ptr->asid) { + return vdpa_iommufd_ptr; + } + + vdpa_iommufd_ptr =3D 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 =3D hdev->opaque; + VDPAIOMMUFDState *vdpa_iommufd_ptr =3D hdev->vdev->iommufd_ptr; + VDPAIOMMUFDState *vdpa_iommufd_new =3D g_malloc(sizeof(VDPAIOMMUFDStat= e)); + + vdpa_iommufd_new->dev =3D hdev; + vdpa_iommufd_new->asid =3D asid; + vdpa_iommufd_new->iommufd =3D vdpa_iommufd_ptr->iommufd; + + ret =3D 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 =3D ioas_id; + /* this is new asid, attch to iommufd*/ + ret =3D 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 !=3D NULL) { + vdpa_iommufd_ptr =3D vdpa_iommufd_ptr->next; + } + /*save this vdpa_iommufd in list */ + vdpa_iommufd_ptr->next =3D vdpa_iommufd_new; + vdpa_iommufd_new->next =3D 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 =3D=3D NULL) { + error_report("Failed to get vdev (%s)", strerror(errno)); + return 0; + } + /*search if this asid have attach to iommufd before*/ + vdpa_iommufd =3D vdpa_get_ioas_by_asid(hdev, asid); + if (vdpa_iommufd =3D=3D NULL) { + /*this asid is first use, need to alloc and add to iommufd*/ + vdpa_iommufd =3D vdpa_add_new_ioas_id(hdev, asid); + } + return iommufd_backend_map_dma(vdpa_iommufd->iommufd, vdpa_iommufd->io= as_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 =3D=3D NULL) { + error_report("Failed to get vdev (%s)", strerror(errno)); + return 0; + } + /*search if this asid have attach to iommufd before*/ + + vdpa_iommufd =3D vdpa_get_ioas_by_asid(hdev, asid); + if (vdpa_iommufd =3D=3D 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 =3D { + .argsz =3D sizeof(detach_data), + .flags =3D 0, + }; + + if (ioctl(v->device_fd, VDPA_DEVICE_DETACH_IOMMUFD_AS, &detach_data)) { + error_report("error bind device fd=3D%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 =3D { + .iommufd =3D vdpa_iommufd->iommufd->fd, + .ioas_id =3D vdpa_iommufd->ioas_id, + }; + + int ret; + /* Bind device to iommufd */ + ret =3D ioctl(dev->device_fd, VHOST_VDPA_SET_IOMMU_FD, &bind); + if (ret) { + error_report("error bind device fd=3D%d to iommufd=3D%d", dev->dev= ice_fd, + bind.iommufd); + return ret; + } + + vdpa_iommufd->devid =3D bind.out_devid; + vdpa_iommufd->hwptid =3D 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 =3D NULL; + struct vhost_dev *dev =3D v->dev; + vdpa_iommufd =3D dev->vdev->iommufd_ptr; + + /*allocate a new IOAS */ + ret =3D 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 =3D ioas_id; + vdpa_iommufd->dev =3D dev; + /* use the default ASID*/ + vdpa_iommufd->asid =3D VHOST_VDPA_GUEST_PA_ASID; + vdpa_iommufd->next =3D NULL; + + vdpa_iommufd->as =3D as; + /*bind the default ASID to iommufd*/ + ret =3D 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 =3D NULL; + + struct vhost_dev *dev =3D v->dev; + if (!dev->vdev) { + return; + } + vdpa_iommufd =3D dev->vdev->iommufd_ptr; + vdpa_device_detach_iommufd(v, vdpa_iommufd, &err); + + while (vdpa_iommufd !=3D NULL) { + iommufd_backend_put_ioas(vdpa_iommufd->iommufd, vdpa_iommufd->ioas= _id); + vdpa_iommufd_tmp =3D vdpa_iommufd; + vdpa_iommufd =3D vdpa_iommufd->next; + + vdpa_iommufd_destroy(vdpa_iommufd_tmp); + } +} + +struct vdpa_iommu_backend_ops iommufd_ops =3D { + .dma_map =3D vdpa_iommufd_map, + .dma_unmap =3D vdpa_iommufd_unmap, + .attach_device =3D vdpa_iommufd_attach_device, + .detach_device =3D vdpa_iommufd_detach_device, +}; + +void vdpa_backend_iommufd_ops_class_init(struct vhost_vdpa *v) +{ + v->ops =3D &iommufd_ops; +} --=20 2.34.3