From patchwork Wed Aug 23 16:47:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13362834 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 ECBE3C27C40 for ; Wed, 23 Aug 2023 16:49:59 +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=8QJpntZprCzAwz+BQUG5SaOscADajtmQUtP/g2Jsrzg=; b=ABMb0WqbMLOOEB 0hhvkai8fr/iQ+s9oY+H4I4ou9QI/u3qTECd708QmC9UEQoslgejkml6ksa3awg0eRzLlnzIUDIJy RvWDOPtbRTKATa5bqGNDCFW3Waa1Tc4/Uz3KOltrSmZHgwTKT32bX2UihIUYGRDLrEmVDsO/tfDL+ BEgc8M893cYjK81xcXJYm5lwPe4YSZpPZwyzoVWmF0MCYF2WGxWVA07dQsAuGbxC4yDs/ed50nzFW oEiJjVRsP2nsNFTR6rAUbDLZEHgKSSNNDxZ6QPmKgzTLdlG7vj6jvvUmLuOMX42mwkUfMgrJkZ+lH OpfMp/HcEf9KJKuM0WUg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qYr3K-0017z9-26; Wed, 23 Aug 2023 16:49:54 +0000 Received: from mail-bn8nam12on20629.outbound.protection.outlook.com ([2a01:111:f400:fe5b::629] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qYr1X-0016EL-2c; Wed, 23 Aug 2023 16:48:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dEV55TpPvv1XWpYARFYe6gks7H9zQTGrsoEl9uPlCION+FOlr/pgcN7vP9JpQt8sO428Laf1LwH3PJsBLu4QOcHEhLyWDMJ4ETXI2gTxY2Uq4w/R04OPFbJQFqyl3S1t5X/MMUBvnhMl/CdliUQYlfy0Ho3Bx7tNcgq4byADV/Gp077dOeMgu3XriyNFpJgjdKpb0+xlr06D/3sF9+ZACaQ66MVKiVY1W9UTKzx7KnJwHPPxKAQtZHScoQX1g3yNUAfVDx5W4ugKYX1lW/jHA7Mcn2UKrMT29AeFaQPPJIea1yVCnz7pXRl5NJBXyRrSTwbdH8Ppk32359zHzOSpDQ== 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=4Vh/jrZar+VF6tSbgxNByQJmljVZ29qOBiorGlPOVR4=; b=CBaih213gLAcebKSpZW/gjXPaO/jYUs1OXCnT/DVyARYzMtcCPJE9NL6ZVbyFiGTZ4tsCiDb62m7YpwwPn6tj209cf+KLV3WPHf+A26W1QqvuNoTCShjR+xInU4QA/uqvBTIGyBUIv5IFpcg2YUW8HE/jV3y7hcqmRDS33rkEFhsGOo5wzuRAIJxD5PJLS/q/bjEiDOJVZWlCQ8vWxP1Ym+K885upgrtmzjgcvzP2dPNfDldmIWS+4uZtg0VRcKeLPwXPJy0IoEjzQjz2otkJWEOAx+O6tvCm79DgZovYclgbAj4o12UTLKs/ducUFVuajcRt4WNMUWtcZ37983kag== 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=4Vh/jrZar+VF6tSbgxNByQJmljVZ29qOBiorGlPOVR4=; b=ha5Aph5gPGc6tZPEY1idGawKYiqTa49LEW77vJ3FmFtKFXdTGtXTkjdP1r507LnybBP3ReNJhG7rtE0lvQUpkMfMylp55UipEF3J5ChHK/6wyQOfMXs7BiK3pSLP/tlKa8wzpluFiWRON7KmosJMcaApaBypdhSP1sWpB5XnCVdM18YuvOEzZhkCia+acq029Nxi2RUdyOT9l9cDFa244Pf2bCBvRnPxei6o4yIqlT4XcnJvK6pGwRS8eNQr5ycIaLwFGOIbR7SQ6uTdn0qTdFbW9Nnw6lqNW38NMzZtqrDQ0pc0PRqTC01GmO3w7JkUC8hqSzuJHyImeCjDE3cJ/Q== 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 CY5PR12MB6346.namprd12.prod.outlook.com (2603:10b6:930:21::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.24; Wed, 23 Aug 2023 16:47:51 +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; Wed, 23 Aug 2023 16:47:51 +0000 From: Jason Gunthorpe To: Andy Gross , Alim Akhtar , Bjorn Andersson , AngeloGioacchino Del Regno , Baolin Wang , Christophe Leroy , Gerald Schaefer , Heiko Stuebner , iommu@lists.linux.dev, Jernej Skrabec , Jonathan Hunter , Joerg Roedel , Kevin Tian , Konrad Dybcio , Krzysztof Kozlowski , linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-s390@vger.kernel.org, linux-samsung-soc@vger.kernel.org, linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org, Russell King , linuxppc-dev@lists.ozlabs.org, Matthias Brugger , Matthew Rosato , Michael Ellerman , Nicholas Piggin , Orson Zhai , Rob Clark , Robin Murphy , Samuel Holland , Thierry Reding , Krishna Reddy , Chen-Yu Tsai , Will Deacon , Yong Wu , Chunyan Zhang Cc: Lu Baolu , Dmitry Osipenko , Marek Szyprowski , Nicolin Chen , Niklas Schnelle , Steven Price , Thierry Reding Subject: [PATCH v7 21/24] iommu: Add __iommu_group_domain_alloc() Date: Wed, 23 Aug 2023 13:47:35 -0300 Message-ID: <21-v7-de04a3217c48+15055-iommu_all_defdom_jgg@nvidia.com> In-Reply-To: <0-v7-de04a3217c48+15055-iommu_all_defdom_jgg@nvidia.com> References: X-ClientProxiedBy: CH2PR20CA0008.namprd20.prod.outlook.com (2603:10b6:610:58::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|CY5PR12MB6346:EE_ X-MS-Office365-Filtering-Correlation-Id: 3fa7d4e8-1c33-4c80-7e47-08dba3f8ac3a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hZ3j5lZFzcqVEtk5AZSndJmX8RQNq6XKOFSbU7Nu2iRisiKty/qfsONeZqY30YYtdXvpH228zbxjRMug3dOFMOQ3/u6Y0ibWH9zZIsSoBGSsEaZanPPyyWOv0AIMyfZGZovQT/UWa5TssXsiBnsipCUF5zRaMrLfnwpAZliGSx2nRBiS/5CRXvZXgUnV+DCZMFqoYRVA6CyJ8cwl7oWcbKUGjnTnWD1sLQmq73z53BTC8523mWA69JCWTR1IbFtw98iM/BcgM48q5zZrL99jcJJHZ/oud1K1MC2WPTvzaK3hII7G3kB77otZHfradBVWHFelpLZfn0ZRjjmqxwGDDfHT5igJctaP8WsE0qwQGcN6Nt3ntjFDfvmdxGBHEklA6Un+QZB64tw/Ptjc4WpxVCxgV6Qsb7gIbniKcwUgch7xSxXxerDNr+sXzLMZw3/YAIEI2K79H+bpMfoXvOpsvi79nnUgT4kDpecHGxod8XoV+DhfjBjb+Lz+IUiXk6H7c90UpFdpEGYZp4WOUOL58BnNpqC6P59hYb0l0eJCRroGNk7wEx+VEuCIOlNANUem3bdjjZqNuKIPI1Gp7Qgl4jDgu+dqoHMzo0dQ6ivuZCo= 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)(376002)(396003)(136003)(346002)(366004)(39860400002)(186009)(1800799009)(451199024)(54906003)(66476007)(66946007)(6512007)(316002)(66556008)(110136005)(8676002)(8936002)(107886003)(2616005)(4326008)(36756003)(41300700001)(921005)(478600001)(6666004)(6506007)(38100700002)(6486002)(83380400001)(7416002)(2906002)(7406005)(86362001)(5660300002)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 36sMzJRQmGwHSUoz9jJ5ixTEVjrR2dhZ+7wecttstgoRsNa6lDKlI0exuqalSsU8xWTyCNKm5Tv6Hgw5fgcbPp8UXUHoGFcSil2Wy5g6nsqPJRK/tstj3i73U3ZRpaoDoDXVnoENRmy/TLfP5RwBYiNz4W4ZZP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uJAqOv+MwpPEEbWal5ferKZXPfihwoHkCaOZ0nWZREXFXV3eLrU8DUfBNPWmqhIm/ajCvsF+kpcXkYBH80MsmpPWv0VTRlk76EpgOXf30R85J1FauPkLlu9dsb8kzwijPGuez/xjfWPBFyqGSAj/pLhZ3CUlN+vr2miZpNqyN1LwiS5fWg8TM6dzcSW21G12S97wPpEGaTrHH7pSGfMidGhhgxKYNB7RLRbzdJYLr9+k6WhCgymAdRGpSquvPoGdbaH3pjRZKQaD8e1TGnlywtxWfo+tuy9jCznLUcZ1/V/vyR45Wa7xm3kdQAMEhbmQRot2OMArDYvbYfexUsk8lMauD7h1S1EZeWLq8PKqan X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3fa7d4e8-1c33-4c80-7e47-08dba3f8ac3a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Aug 2023 16:47:44.3673 (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: J40PjWnDM7KG8j2X8Zvd8xQXE261dl7s4YKVONaTybRYQ++3Huc1tCk5NRtoEz9p X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6346 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230823_094803_879545_B25E842E X-CRM114-Status: GOOD ( 20.18 ) 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 Allocate a domain from a group. Automatically obtains the iommu_ops to use from the device list of the group. Convert the internal callers to use it. Tested-by: Steven Price Tested-by: Marek Szyprowski Tested-by: Nicolin Chen Reviewed-by: Lu Baolu Signed-off-by: Jason Gunthorpe Reviewed-by: Jerry Snitselaar --- drivers/iommu/iommu.c | 59 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3003c249ab9b8e..568dec45a06954 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -96,8 +96,8 @@ static const char * const iommu_group_resv_type_string[] = { static int iommu_bus_notifier(struct notifier_block *nb, unsigned long action, void *data); static void iommu_release_device(struct device *dev); -static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, - unsigned type); +static struct iommu_domain * +__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type); static int __iommu_attach_device(struct iommu_domain *domain, struct device *dev); static int __iommu_attach_group(struct iommu_domain *domain, @@ -1719,12 +1719,11 @@ struct iommu_group *fsl_mc_device_group(struct device *dev) EXPORT_SYMBOL_GPL(fsl_mc_device_group); static struct iommu_domain * -__iommu_group_alloc_default_domain(const struct bus_type *bus, - struct iommu_group *group, int req_type) +__iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) { if (group->default_domain && group->default_domain->type == req_type) return group->default_domain; - return __iommu_domain_alloc(bus, req_type); + return __iommu_group_domain_alloc(group, req_type); } /* @@ -1751,9 +1750,7 @@ static const struct iommu_ops *group_iommu_ops(struct iommu_group *group) static struct iommu_domain * iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) { - const struct bus_type *bus = - list_first_entry(&group->devices, struct group_device, list) - ->dev->bus; + const struct iommu_ops *ops = group_iommu_ops(group); struct iommu_domain *dom; lockdep_assert_held(&group->mutex); @@ -1763,24 +1760,24 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) * domain. This should always be either an IDENTITY or PLATFORM domain. * Do not use in new drivers. */ - if (bus->iommu_ops->default_domain) { + if (ops->default_domain) { if (req_type) return ERR_PTR(-EINVAL); - return bus->iommu_ops->default_domain; + return ops->default_domain; } if (req_type) - return __iommu_group_alloc_default_domain(bus, group, req_type); + return __iommu_group_alloc_default_domain(group, req_type); /* The driver gave no guidance on what type to use, try the default */ - dom = __iommu_group_alloc_default_domain(bus, group, iommu_def_domain_type); + dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type); if (dom) return dom; /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */ if (iommu_def_domain_type == IOMMU_DOMAIN_DMA) return NULL; - dom = __iommu_group_alloc_default_domain(bus, group, IOMMU_DOMAIN_DMA); + dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA); if (!dom) return NULL; @@ -2043,19 +2040,16 @@ void iommu_set_fault_handler(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_fault_handler); -static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, - unsigned type) +static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops, + unsigned int type) { struct iommu_domain *domain; unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS; - if (bus == NULL || bus->iommu_ops == NULL) - return NULL; + if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain) + return ops->identity_domain; - if (alloc_type == IOMMU_DOMAIN_IDENTITY && bus->iommu_ops->identity_domain) - return bus->iommu_ops->identity_domain; - - domain = bus->iommu_ops->domain_alloc(alloc_type); + domain = ops->domain_alloc(alloc_type); if (!domain) return NULL; @@ -2065,10 +2059,10 @@ static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, * may override this later */ if (!domain->pgsize_bitmap) - domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; + domain->pgsize_bitmap = ops->pgsize_bitmap; if (!domain->ops) - domain->ops = bus->iommu_ops->default_domain_ops; + domain->ops = ops->default_domain_ops; if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) { iommu_domain_free(domain); @@ -2077,9 +2071,17 @@ static struct iommu_domain *__iommu_domain_alloc(const struct bus_type *bus, return domain; } +static struct iommu_domain * +__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type) +{ + return __iommu_domain_alloc(group_iommu_ops(group), type); +} + struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) { - return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); + if (bus == NULL || bus->iommu_ops == NULL) + return NULL; + return __iommu_domain_alloc(bus->iommu_ops, IOMMU_DOMAIN_UNMANAGED); } EXPORT_SYMBOL_GPL(iommu_domain_alloc); @@ -3239,21 +3241,18 @@ void iommu_device_unuse_default_domain(struct device *dev) static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) { - struct group_device *dev = - list_first_entry(&group->devices, struct group_device, list); - if (group->blocking_domain) return 0; group->blocking_domain = - __iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED); + __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED); if (!group->blocking_domain) { /* * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED * create an empty domain instead. */ - group->blocking_domain = __iommu_domain_alloc( - dev->dev->bus, IOMMU_DOMAIN_UNMANAGED); + group->blocking_domain = __iommu_group_domain_alloc( + group, IOMMU_DOMAIN_UNMANAGED); if (!group->blocking_domain) return -EINVAL; }