From nobody Tue Apr 23 15:47:34 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1675198744; cv=none; d=zohomail.com; s=zohoarc; b=K0Tzb4rwBZk3GSzYfEJfklcck8hrXoLqd/KnTNfPlS4xziQJMP1/2DmhoZYESWt+IGzCx0q+FA6I0ikclcA5Swqde0XL0VhsGjWLSIIEzzpvnFE/huCM9Jb0YynxmzY70E6X0+IBV1tUSwAVkbbp/dc62oiUaEEBNpkHttnKcHE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675198744; h=Content-Transfer-Encoding:Cc: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=bwIpAqcX+KnGrnnAkFDnrt9OFoUGPx1pO+F4THaPIKA=; b=StJ8/GTPQpcEz78q0mpzIyZZ/6M2PJ3iIJ+98jDDIyTrzyUdvJ3xOB3lHWzrezf9RQQfhVQzD8N+MEwzJWt/JAt570aBg4qp0ypTkbOsxXgPsQypsbP+NSrh+IDL2dT6wkAIX3VrfcFrKgItLEAbk/aiE43xq79hXDNhiWxKgoM= 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+importer2=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 1675198744908745.8884231019147; Tue, 31 Jan 2023 12:59:04 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pMxeP-0004RW-Fx; Tue, 31 Jan 2023 15:54:45 -0500 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 1pMxeM-0004F7-UQ for qemu-devel@nongnu.org; Tue, 31 Jan 2023 15:54:43 -0500 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 1pMxeL-0004hA-As for qemu-devel@nongnu.org; Tue, 31 Jan 2023 15:54:42 -0500 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-9-KCVD7BVlOhWNQgDhwdbZIw-1; Tue, 31 Jan 2023 15:54:36 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EF619811E6E; Tue, 31 Jan 2023 20:54:34 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.193.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2184040C2064; Tue, 31 Jan 2023 20:54:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675198480; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bwIpAqcX+KnGrnnAkFDnrt9OFoUGPx1pO+F4THaPIKA=; b=Ivul+8Oh9C4Ml2YoJC7UxlTpRxsNBuW1wMwKbRMqIWQmUU3ZNsJW5mSzdpQgqwNbojwoY9 H4nI5pRK53TXBJFv2l7/dVs5rd9hQbm/ZnjqczaDo62tbd45KeJEakdQhFjabPBDqDUPBC sUA/CKyAgMgG89O0k/9jrnfYr5AEzHU= X-MC-Unique: KCVD7BVlOhWNQgDhwdbZIw-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, yi.l.liu@intel.com, yi.y.sun@intel.com, alex.williamson@redhat.com, clg@redhat.com, qemu-devel@nongnu.org Cc: david@gibson.dropbear.id.au, thuth@redhat.com, farman@linux.ibm.com, mjrosato@linux.ibm.com, akrowiak@linux.ibm.com, pasic@linux.ibm.com, jjherne@linux.ibm.com, jasowang@redhat.com, kvm@vger.kernel.org, jgg@nvidia.com, nicolinc@nvidia.com, kevin.tian@intel.com, chao.p.peng@intel.com, peterx@redhat.com, shameerali.kolothum.thodi@huawei.com, zhangfei.gao@linaro.org, berrange@redhat.com, apopple@nvidia.com, suravee.suthikulpanit@amd.com Subject: [RFC v3 12/18] vfio/container-base: Introduce [attach/detach]_device container callbacks Date: Tue, 31 Jan 2023 21:52:59 +0100 Message-Id: <20230131205305.2726330-13-eric.auger@redhat.com> In-Reply-To: <20230131205305.2726330-1-eric.auger@redhat.com> References: <20230131205305.2726330-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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 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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1675198746768100001 Content-Type: text/plain; charset="utf-8" Let's turn attach/detach_device as container callbacks. That way, their implementation can be easily customized for a given backend. For the time being, only the legacy container is supported. Signed-off-by: Eric Auger Signed-off-by: Yi Liu --- include/hw/vfio/vfio-common.h | 1 + include/hw/vfio/vfio-container-base.h | 2 ++ hw/vfio/as.c | 21 +++++++++++++++++++++ hw/vfio/container.c | 9 +++++++-- hw/vfio/pci.c | 2 +- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 1580f9617c..4f89657ac1 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -86,6 +86,7 @@ typedef struct VFIODeviceOps VFIODeviceOps; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next; struct VFIOGroup *group; + VFIOContainer *container; char *sysfsdev; char *name; DeviceState *dev; diff --git a/include/hw/vfio/vfio-container-base.h b/include/hw/vfio/vfio-c= ontainer-base.h index 6080a588bf..5e47faf293 100644 --- a/include/hw/vfio/vfio-container-base.h +++ b/include/hw/vfio/vfio-container-base.h @@ -133,6 +133,8 @@ struct VFIOIOMMUBackendOpsClass { int (*dma_unmap)(VFIOContainer *container, hwaddr iova, ram_addr_t size, IOMMUTLBEntry *iotlb); + int (*attach_device)(VFIODevice *vbasedev, AddressSpace *as, Error **e= rrp); + void (*detach_device)(VFIODevice *vbasedev); /* migration feature */ bool (*devices_all_dirty_tracking)(VFIOContainer *container); void (*set_dirty_page_tracking)(VFIOContainer *container, bool start); diff --git a/hw/vfio/as.c b/hw/vfio/as.c index 6a5f3f80b5..d212974b9b 100644 --- a/hw/vfio/as.c +++ b/hw/vfio/as.c @@ -851,6 +851,27 @@ void vfio_put_address_space(VFIOAddressSpace *space) } } =20 +int vfio_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **err= p) +{ + const VFIOIOMMUBackendOpsClass *ops; + + ops =3D VFIO_IOMMU_BACKEND_OPS_CLASS( + object_class_by_name(TYPE_VFIO_IOMMU_BACKEND_LEGACY_OPS)= ); + if (!ops) { + error_setg(errp, "VFIO IOMMU Backend not found!"); + return -ENODEV; + } + return ops->attach_device(vbasedev, as, errp); +} + +void vfio_detach_device(VFIODevice *vbasedev) +{ + if (!vbasedev->container) { + return; + } + vbasedev->container->ops->detach_device(vbasedev); +} + static const TypeInfo vfio_iommu_backend_ops_type_info =3D { .name =3D TYPE_VFIO_IOMMU_BACKEND_OPS, .parent =3D TYPE_OBJECT, diff --git a/hw/vfio/container.c b/hw/vfio/container.c index b9ee56067c..e99d12f4d8 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -1278,7 +1278,8 @@ static int vfio_device_groupid(VFIODevice *vbasedev, = Error **errp) return groupid; } =20 -int vfio_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **err= p) +static int +vfio_legacy_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **= errp) { int groupid =3D vfio_device_groupid(vbasedev, errp); VFIODevice *vbasedev_iter; @@ -1307,14 +1308,16 @@ int vfio_attach_device(VFIODevice *vbasedev, Addres= sSpace *as, Error **errp) vfio_put_group(group); return -1; } + vbasedev->container =3D &group->container->bcontainer; =20 return 0; } =20 -void vfio_detach_device(VFIODevice *vbasedev) +static void vfio_legacy_detach_device(VFIODevice *vbasedev) { vfio_put_base_device(vbasedev); vfio_put_group(vbasedev->group); + vbasedev->container =3D NULL; } =20 static void vfio_iommu_backend_legacy_ops_class_init(ObjectClass *oc, @@ -1329,6 +1332,8 @@ static void vfio_iommu_backend_legacy_ops_class_init(= ObjectClass *oc, ops->add_window =3D vfio_legacy_container_add_section_window; ops->del_window =3D vfio_legacy_container_del_section_window; ops->check_extension =3D vfio_legacy_container_check_extension; + ops->attach_device =3D vfio_legacy_attach_device; + ops->detach_device =3D vfio_legacy_detach_device; } =20 static const TypeInfo vfio_iommu_backend_legacy_ops_type =3D { diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 9856d81819..07361a6fcd 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -3106,7 +3106,7 @@ static void vfio_realize(PCIDevice *pdev, Error **err= p) } =20 if (!pdev->failover_pair_id && - vfio_container_check_extension(&vbasedev->group->container->bconta= iner, + vfio_container_check_extension(vbasedev->container, VFIO_FEAT_LIVE_MIGRATION)) { ret =3D vfio_migration_probe(vbasedev, errp); if (ret) { --=20 2.37.3