From patchwork Tue Aug 22 16:15:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13361129 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 70AE0EE49AE for ; Tue, 22 Aug 2023 16:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=dQFrGnV5Z53zLj2MFKLkzPYhBpRTGkH8B/rn+Lc44JE=; b=wVrzLY4IK5soOg Iccb+0DpPDwvONH8ikWEF/OIavbhzt9TsvKs+HV5bbRsuHEdac7z6MAF0Xd3vK8YaPpK5fvPP2pjj 8YwgZS/wra95FoEHdmeTLgncrd1miCB4fPiY1qOUTaIypXKypBLgicuGIbT6CQAnu3Jz/WE3cp7DG cyzasNDMITrNCtZXILoBVgAaHuaJf7ypAZPc8mchgfrjsIPDQ79qgtI5Hbn5IhLl6t2IwYlYghZm3 CZKvS5Dtm2ibRvnVI9WtbMJAqCsiIpbyQHT4U8ckgTavduTevZvb7Go2fyL6FAYf4aKhkk4KEKP6Y 3FZx4fjahYoj/FxNX8Lg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qYU3S-00GSCW-0P; Tue, 22 Aug 2023 16:16:30 +0000 Received: from mail-mw2nam12on20631.outbound.protection.outlook.com ([2a01:111:f400:fe5a::631] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qYU3I-00GS2C-0M; Tue, 22 Aug 2023 16:16:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZLUSAvEdYsI1aahuk+uiByU0HihX03I9XKlzLCWLJknKEpiHDM+nvbEIVt+PpJbxxPEqirFW1PpztstqqntcFaFYhOTqHB0igycFGxDuupCPCcBlgckBFfBJ4GVWhpTke5vypBbF38CC2bBe91SCv4IB+seV6zDN+QKU2jz4J5yjJ5M4XCsgMKdE59OcUAoYlJzmY9x9po0IHH03CxA2iAmNTCuO3Rb6ZeE8b4CQOcU6Hp020CZ0o2R9x2cybD7DoeROzj4GR1iKDocuLZ7WdvRPbRlCfpK+C8IlLwoEi0Tuvs8R7/MHWxSQTUP7gXR04JOvJXVi659FQgDxccg+xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=r/EiohERC4PV6mQAz7QZADKo/0C4MxnhQ3V5u2NLOno=; b=CHpb3zzBqJrOynDu3gsWUaDOASGHuVdb+PeE22IQsUW7uyLr7KZ89LLcPa/Ch+V0p4NLtg1X7vvqaKFC7DvpEl5I1QCjiZqtt1Cr5Y+dZKfUn3KxlLmgGGSWV39rhiCagDoOUM0IMXtiPBdoRNGVFA7DvXndQZaOtpq4giuh1bML02OSb6U73aRT5FL35LYhMd8lzXsBOU67w9I0U5gVTFLPXGb+nU1GgwaFSZ0cEKoqi7WNSklP3EU6/XBRsErDj3f1PVJMQ3o5uLymYpgc3WZvejerc1byVI9Pty6SMQCS3dTcnNyCpBexckifnVhDcnGa9qW85zJQx40UO3o0tw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=r/EiohERC4PV6mQAz7QZADKo/0C4MxnhQ3V5u2NLOno=; b=I9L4ZHoUk3OTwM0YWpyniB7DEJZUE2s2MTQu+Hypz05oMmbg0rjyFpu+kUuls8DfoDAuZXjUuGootbWa+98GgUKPZNOgK2yi8KDgt/fGYZYYE0qFdERqzh9OtScpZzHNM8v+00pCnZ4Gd5zkCENgeyT11UB98rm/ywvEiXUbzpOw9jAkipKq5ZjYG7+yh360JvUkIHdP2GIJsAWk7o6875jEKL7lJvALvaCmxwMkFJDx3piozxGQEEAO8S1b+T+9gyZYWKSjGipcDMyq85kjsoq+oUxCHqgD3o3JLvPyEdf9ms4QyiShZ9JBjNzzq/CJEOx/Jq2B2/4LmyxJmV1JtA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by PH7PR12MB5831.namprd12.prod.outlook.com (2603:10b6:510:1d6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.20; Tue, 22 Aug 2023 16:16:05 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6699.020; Tue, 22 Aug 2023 16:16:05 +0000 From: Jason Gunthorpe To: Baolin Wang , Heiko Stuebner , iommu@lists.linux.dev, Jernej Skrabec , Joerg Roedel , linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev, Orson Zhai , Robin Murphy , Samuel Holland , Chen-Yu Tsai , Will Deacon , Chunyan Zhang Cc: Lu Baolu , Kevin Tian , Marek Szyprowski Subject: [PATCH 2/7] iommu: Add generic_single_device_group() Date: Tue, 22 Aug 2023 13:15:57 -0300 Message-ID: <2-v1-c869a95191f2+5e8-iommu_single_grp_jgg@nvidia.com> In-Reply-To: <0-v1-c869a95191f2+5e8-iommu_single_grp_jgg@nvidia.com> References: X-ClientProxiedBy: CH2PR17CA0021.namprd17.prod.outlook.com (2603:10b6:610:53::31) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5831:EE_ X-MS-Office365-Filtering-Correlation-Id: a4f04f2d-77a0-4c09-ac85-08dba32b155e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Gda+1Ljnor2ziEY154vx3O3vKxR7hj7MGA+lWCwWxH+XWRn59R8WzDG1JoLjWU8qGJSUsn/1nQ26HRRHwmTtQzMrnHMgG0PoGIhBDxGauFCvvwBk2Y4N3ApXo6ktgWZ8N2KOjgh6SQmWvljfTgBNwpbHp7H55gtYRIVYXvuDPLlnMSidFFgWdxLBFvFig+E3nR16/He+/Eqc6uAF80XX0OAC1dA+tg3+HgIt6kZhUyaUP9oMTiDhG6QXdnWmXrmJkKMr8enIdX8Ax7t5Y5vs9H98UdzVtAdRxmScoqydmLpYNqpKAINLCc7CfgTjLcOOkZAtlT4jaTvBdcUb199BIbwiDdZ5lt4EbtrdjD83eQlK+wL3G9KxL6kRTtjXEnTWRTvYYIiHNcfCVdrQ/EQgFu6/XWV/100FZKBo5pXKbLhKcsintD2QScCbQbZ18Zpra0Y3hqX/8kzUas3Qxul/vBhkV4De/frhwl5I5j7BSesTVq3lifmaqSORZTaCRTqzccqhGSK24Rt4yL2g0DqvSQ7k+cj7zK2m5vbqGi6HfLDJjTBzg+Wan8F33HEQB7X0YYeP9QJhCKFS5Ul0ytZOJXNq/1LtytffBS34DVEGPkOK3GJu1FIvmrfPNs3nt+5+MsoSiho545soD6HP1re6Rg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(451199024)(1800799009)(186009)(2906002)(7416002)(38100700002)(6506007)(6486002)(83380400001)(5660300002)(26005)(86362001)(8676002)(2616005)(8936002)(4326008)(66946007)(6512007)(54906003)(66556008)(66476007)(110136005)(498600001)(921005)(6666004)(36756003)(4216001)(41533002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Sukzy1eSpfHy5j5DmZrQnHX0c9R/Qs/pycuvZmGQZ/u+s+djd5zXRTxuSaVf0fF13nwxSBcBXLLCfhrAIjjJT1dI7GrRAhuofEMzKNrQ71G5kVCx6a5oZLm/eTbKm4kBZ8p6npiIm6dVdlvmdGQYlaqmPioONITvFkBLuyVfDxgFv6tmQffUWQeh8lFyp3EfCcSmZbzZTYiz3ENxET3QcJDKzfn+WCxIhYKqoWbflI+Bye4kgkbznibQtjGneJxdLWkdThGb1pmwVU/+ek8oA/ffrUuKp6HfW+w/oPLzKH4FiwR3zoYElffA94dJjDc2uNh033Ox029pKBmrdfLUc7A/8NU9AhG+yMSlK+GmH47FCHLv5EcVhIim8bykCD01byfTVjwBcCWsg8eZvGjHgvRABO7H0FhmG0rd9YhDJ/wWgaY1GzCWuwEdLz5ZBuMFjM06CfJaHBbkKwk+RQRupymbwjVgzKJ8o6LEZi2V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kZWB7KXjP/Sp1nq2SLIjWzFfp/6x/h9uzPeTXpUE/GmccWWqG X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a4f04f2d-77a0-4c09-ac85-08dba32b155e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2023 16:16:04.1898 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bW4Q4gQdCCL5stCpSZitijV2v3SKhIRhN8jKXSEbgpql/uqzJ9fXkqElHcdHApQ1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5831 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230822_091620_173776_1FF89AD8 X-CRM114-Status: GOOD ( 16.72 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+linux-rockchip=archiver.kernel.org@lists.infradead.org This implements the common pattern seen in drivers of a single iommu_group for the entire iommu driver instance. Implement this in core code so the drivers that want this can select it from their ops. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 28 +++++++++++++++++++++++++++- include/linux/iommu.h | 3 +++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 50afc55eaca8e6..3d6bb2537e8d85 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -288,6 +288,10 @@ void iommu_device_unregister(struct iommu_device *iommu) spin_lock(&iommu_device_lock); list_del(&iommu->list); spin_unlock(&iommu_device_lock); + + /* Pairs with the alloc in generic_single_device_group() */ + iommu_group_put(iommu->singleton_group); + iommu->singleton_group = NULL; } EXPORT_SYMBOL_GPL(iommu_device_unregister); @@ -360,6 +364,7 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) ret = PTR_ERR(iommu_dev); goto err_module_put; } + dev->iommu->iommu_dev = iommu_dev; ret = iommu_device_link(iommu_dev, dev); if (ret) @@ -374,7 +379,6 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) } dev->iommu_group = group; - dev->iommu->iommu_dev = iommu_dev; dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev); if (ops->is_attach_deferred) dev->iommu->attach_deferred = ops->is_attach_deferred(dev); @@ -388,6 +392,7 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) err_module_put: module_put(ops->owner); err_free: + dev->iommu->iommu_dev = NULL; dev_iommu_free(dev); return ret; } @@ -1591,6 +1596,27 @@ struct iommu_group *generic_device_group(struct device *dev) } EXPORT_SYMBOL_GPL(generic_device_group); +/* + * Generic device_group call-back function. It just allocates one + * iommu-group per iommu driver instance shared by every device + * probed by that iommu driver. + */ +struct iommu_group *generic_single_device_group(struct device *dev) +{ + struct iommu_device *iommu = dev->iommu->iommu_dev; + + if (!iommu->singleton_group) { + struct iommu_group *group; + + group = iommu_group_alloc(); + if (IS_ERR(group)) + return group; + iommu->singleton_group = group; + } + return iommu_group_ref_get(iommu->singleton_group); +} +EXPORT_SYMBOL_GPL(generic_single_device_group); + /* * Use standard PCI bus topology, isolation features, and DMA alias quirks * to find or create an IOMMU group for a device. diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 389fffc0b3a2df..9ed139bf111f6d 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -363,6 +363,7 @@ struct iommu_domain_ops { * @list: Used by the iommu-core to keep a list of registered iommus * @ops: iommu-ops for talking to this iommu * @dev: struct device for sysfs handling + * @singleton_group: Used internally for drivers that have only one group * @max_pasids: number of supported PASIDs */ struct iommu_device { @@ -370,6 +371,7 @@ struct iommu_device { const struct iommu_ops *ops; struct fwnode_handle *fwnode; struct device *dev; + struct iommu_group *singleton_group; u32 max_pasids; }; @@ -644,6 +646,7 @@ extern struct iommu_group *pci_device_group(struct device *dev); extern struct iommu_group *generic_device_group(struct device *dev); /* FSL-MC device grouping function */ struct iommu_group *fsl_mc_device_group(struct device *dev); +extern struct iommu_group *generic_single_device_group(struct device *dev); /** * struct iommu_fwspec - per-device IOMMU instance data