From patchwork Mon Jul 31 17:50:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13335345 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 C6FFFC00528 for ; Mon, 31 Jul 2023 17:51:08 +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=bX4N017l77NX01iTEKomqxRxLSY+05vDfKxq3FVp+z0=; b=B8+yBxV0xrVw0o CpV4LzwPIVg9uaVKGkcU4bN5WMgLUD+orxLgGlqkzQr2YYwXQbODbfbC8sD59WY5+FZt4ZIyYP/2E RctgDHIghLahRCQslu/MPRmMJRF4DFPompMsChIj1gUQ4DTV7eooXrqrstKCCPqcGKPt/iK1LThb5 9/tTvSw+jzocXqX7tN2Z/J7dBpU8fYBN5Ar2e8qSY4aBghO+jMdGOzvOUtuFshOBo6h2eJhLzZyie jv581U8lQLSME43WFoXYPtgK7tGjv7MtIegDbt4hibIOkQR2ZzdYthyn3i9QcMX8k81zjACSWlIY/ fy8/RGCtbOcSqaJfSYGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQX2m-00Gtlk-0p; Mon, 31 Jul 2023 17:50:56 +0000 Received: from mail-bn8nam12on20618.outbound.protection.outlook.com ([2a01:111:f400:fe5b::618] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQX2Z-00GtYt-1B; Mon, 31 Jul 2023 17:50:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l5BYgaN6E6oMSJ7pc0pK8shan4qemJJfkrwBUtCIHySEHdhz1uT3aquELrWDUo4yoSJXmCZLGLC2lnaSqpkBgs/1r7ptBuK9rPuJdRC43XSNYLIcqvyWLN5fJ636ixbMk23UOWXBk5d9Tmi6BJmPhkqPdAudgVFPVbQVqJEg7YKeZJ6yXPkRFqPWXa6K1czSV5Xa3JxklXNReMz8DPTorElO304jhE7wdZz7b/j+N6iL6NrC+DrKT6ylOlBs8+LUDKFPmpi89U/l3SdpRCKnNh74+cv1V9RKWAIogEyaxiUzIxAdGjj1F7HfBvI5rA+ATedLdOSvX8cQf7BHwTUT4g== 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=2GIBQCaLptdeGVGlR2btZ3TztjtvQ4WCLvPN7KeQI5U=; b=YOoWQ3uH8iF9mSCxXerMV5q1MAFsxKw7uahnCjZlOr+boZSMu3fFyfBWOA3DGLquYFTlsMKsA0x1AYUU/wcXkQi4oo1DY3GQFrNy79v4l283qd+CkI40VvCcrq2nod33AJnh9SxRijuEGtsMCIB4B4hypIdG6zjHsMyv9klVELDpzsd6S5dajWK3DGolyQDTpovF8qnfvNkOuPbuGvhFuuJHSRaJv5VkKV83U5GNapM3EeL0HqCf4y2sEHgIM0lV65unzGsGAVxg2wsw8YHajRqEnCSbyFPKLZbieS5paxMieCWyV0Pnw+9FwkD84198COXVCEgPaS7efuUNg2KNQg== 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=2GIBQCaLptdeGVGlR2btZ3TztjtvQ4WCLvPN7KeQI5U=; b=sW4V8gksyAIaeNius6p03qIEMP1chd+cI6KnSm62o006lqdlxaOWE9u/J2gW70VfznHonvx8H1VKFOVyIDvncbMU+le7wJ7M2nX82DF1RvDnXPmxgr+MhI7G1HGcWl+HnxXDqe5WyiqgDmR7fMsf1tCbiZM+yN7CuDDU4Vh6GwzOsawjEdu4UbQngWxAFKJAqK1eNeemey2p474S4O63M39iQ5DsN+/kWOQRwIgzw/HU9ccNd76qjW0uXQas/8wn4rY1HJ57crdxAW0ZtGPlwzCyEMhxE3sV1lyNSiNbiOwWDBp8ciFOfaptjA87O9OJUGf2S45WX9KBeCWcdE04AA== 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 SJ0PR12MB6992.namprd12.prod.outlook.com (2603:10b6:a03:483::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.43; Mon, 31 Jul 2023 17:50:36 +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.6631.043; Mon, 31 Jul 2023 17:50:36 +0000 From: Jason Gunthorpe To: Baolin Wang , David Woodhouse , 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: Alex Williamson , Lu Baolu Subject: [PATCH v2 03/10] iommu: Add generic_single_device_group() Date: Mon, 31 Jul 2023 14:50:26 -0300 Message-ID: <3-v2-b0417f84403e+11f-iommu_group_locking_jgg@nvidia.com> In-Reply-To: <0-v2-b0417f84403e+11f-iommu_group_locking_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0038.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d3::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6992:EE_ X-MS-Office365-Filtering-Correlation-Id: 3547cdc4-839e-489d-4787-08db91eea462 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3uqDZSzsumXXTCKovB2Hd2MLgoq92IPMLYQBkYNjSUESieFL5yKfHzm407dUqCmBfYYeCIKJg8ZfDyF5mcpWKrhNEAKfL13a651KiUlt3ZJV/rcbfEm841qK8jYfpV3XDB86iWexshUhM3tHbWrXurJNqTMkaVT5mZQwsbvf85E4CclSIesdBdXXczwiXRjwTD12Ee21mweIJY4G0Kfzf+bv02rYwDrs1d6RtVp4/att4Z4OEhdj7YRvlnXG3oJ7dDhfarlmZDcp80B1TG6J9oH/B2kn7goW8bn906F+tvZmf6ZnvBxTXKTWAJkvnA90r5bAi+NkHalQxK4z3xqwnSlfsurk6Cp4fR5nQFv5ntfwpCQ7NaWAZ5KFDpMIsXPtaJk5MMl7kWGCWpVBFF+Xc/7tEyyxw373lDkTTjv2YlvdAGR/+v17Zelh3FtLr2R2YZb799Ds6eKSjr+sQoQ+3U7BFJaIrHozwNvyjN6P9WvDDJmxmHqbTIOYd/S8Sn+uDchvj8AHAGXfBYlvBrMVy0Xf5goBg26mLrrRJikxtEDzgsO0SXPU5KGZt/V6gZ4TeOL9CHVbcqYgn5t9lX/ckXs58UjgKQWuDpoq+7LjbOZk8+yTyTZWZIPQ3SMKgqwPbq8GYCy9mwNN5Lr/d6rMKg== 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:(13230028)(4636009)(39860400002)(346002)(366004)(376002)(136003)(396003)(451199021)(6512007)(6486002)(36756003)(2616005)(6506007)(26005)(83380400001)(186003)(66946007)(66556008)(7416002)(54906003)(41300700001)(921005)(110136005)(38100700002)(66476007)(86362001)(316002)(5660300002)(4326008)(8676002)(8936002)(2906002)(6666004)(478600001)(4216001)(41533002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HfrHOAZmXt6cbiHAEwMuyLnfydBwryNyLTVJn8sv0+SoPJba78b8nA1ehNgT0l1VITbOhKplAS5yRO3pazzAa7SjKMqn522qvxcngfGEs8M68sxtuvFxFyOAZTvTd92Lwtqxa/lGWe9xEU1Cbs57iXM67AcGN9fKz9Z2Q0YUmL7DEd9k3Op5ZqL6K2bMvOJQ7zYGHb7dx+94DQ8wuyQp8eUcHuJTKget6Is3J1fZam96bX1AxZVsLOvmFtiwPnQ+Icvd8pj3BHvXPl69eCIhHfNDZFvCdIbPGxGlksq4lhPt9QiVQR0g98EKsanY8ZuyLJdbQsEufm+9eXStmAg2Jxgp4cBrU5Ykn5AsDXOeAqwU6PNhWGd9e8HiW7xDyhaYh8Oe0Am9CvzTsHYQkSAlJUBR59RatBU9e1iDeyQEkxc+KIDJWmNVTpRiTcXZBvdXwNMLdRvQluv1vBnw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r2znuQjYxV2VmIPJc90QjMDiEEvs6Wfm/fOT4AwxPC7gZHS7 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3547cdc4-839e-489d-4787-08db91eea462 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jul 2023 17:50:35.0704 (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: rCaMASu3STTYMzi4fWciT/VFWfFk6X4PgP8oIpzPWTdc1MtET7SFS1nTbp1c1j3C X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6992 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230731_105043_411467_29607A5A X-CRM114-Status: GOOD ( 15.69 ) 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 Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 27 +++++++++++++++++++++++++++ include/linux/iommu.h | 3 +++ 2 files changed, 30 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f1c8a333553e3b..c673d021d367e4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -289,6 +289,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); @@ -1595,6 +1599,29 @@ 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; + + lockdep_assert_held(&dev_iommu_group_lock); + + 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 b1dcb1b9b17040..f1e18e81fca78b 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -361,6 +361,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 { @@ -368,6 +369,7 @@ struct iommu_device { const struct iommu_ops *ops; struct fwnode_handle *fwnode; struct device *dev; + struct iommu_group *singleton_group; u32 max_pasids; }; @@ -640,6 +642,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