From patchwork Fri Jun 16 19:02:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13283121 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 47E96EB64D8 for ; Fri, 16 Jun 2023 19:04:12 +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=4n49qvk++PmqlP05YDaqqnO1vU/mGFTBdA4WYvWDsIA=; b=QRZX6Tq4rVZXqZ CF+GN5dp3dDjgHyprwWnhWYamVrr25sabn7xOKJkUbB47kOxJ2evDNNYnrZOxDjKcxQiJKgLOv0/c fz926Gs+KA3HGa4pT1pgKOIviH3Qv36JJwPxrCvIrWR5ZrhGOKoU86RnH1yDQUfJxnx/I7F+wlOyC N6TKBdb5dGdvPxn3zVXJ1Wd6GSLzegmU5hq/6DkL7KpMIlqNMlRFJ2mJMEpsUGODZam55QsVZN5uP GlOIQC4dtu6IirVSvu4Yd/tTman+QNfGteIR1ft4JkYfp4b1EsYgl7RvPCFvnXExSZHx4VU87TnVU D3HaivcWb8NBJygny/gw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qAEjs-001Y0P-2g; Fri, 16 Jun 2023 19:04:04 +0000 Received: from mail-bn7nam10on2062f.outbound.protection.outlook.com ([2a01:111:f400:7e8a::62f] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qAEiy-001X1S-0P; Fri, 16 Jun 2023 19:03:09 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f2KTCxvjDQUqsm7Ae0nxl4ecGQVbgN8bwsTy0tb5jKP7Now6WQLp8MsvRsxHXaDqKsYpg6EWHB7wk8SZMw2pVv0dJ553XtxNch+hi3p6hGJD0L3UPxvOKyxePNbZdeIc6w75pH4zbhr2aXn+v9zQUGhrfFvDaRQABTxaeMunfEtxd4eGS6TtVkvUK5ix4aP2Hbf4xK2XFTyH41NOhY4WGlLSF/vEQCX0y9JAci0EQFk4GzA14Kwltix2AJ947gR6SEJkvDnLuANJKFWECzvdC3PqGA3djgPRgyusgjeGtpdKXe/XXbEST25y9TVA2cNrbomlUgDW7kOW3OM1k2Krug== 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=wtwsG7Y4uCGU7mH6MbKK3qYDI3hc9WHokBPbjewgzx4=; b=Mqr4vsNL1LtvYm2/wGqQsH7JKsuez9tLYIzdYOkpgW/I2dycFZFsuSSQWGVVxk9eA6qTTPZ07rjPgRigY3PfE0Cdy+X94lTJlx9c78FybH3bO/7nv0c89SO6l43M+AkmWLlz/bb57UukwKIb8cM8hRq8d/1wOCaAiUR7OJPN4SF8sM/U7w6EZyb2v+Y/SNl4w08agHo6qg7Lg5xCwqZ2JhFFq46QnvzT6JxqoeTUZbVWe2mMDU3Afsqg845WZLjqnT5G3mLEI0S5FUdHxdJsl2z5p6d3uxSAKHqHEywlSbFMxVZDaG4CcSvcgIyYot+f7akZo9wFYyLoPBsEmDcTpg== 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=wtwsG7Y4uCGU7mH6MbKK3qYDI3hc9WHokBPbjewgzx4=; b=HKWqmbmKtUQ/nazqB68n0Rbqe0obIN53UNZwTV6pSLTkyGFK6LU7Ly+c9OYrwayVVU2MFXGZNH8zKSmtBIJR2Aw5wFqaT/hyzLPUOOHxdFpDvKdFOEZq8srme+SBL8yWFqtM0wEq+VQA0ode0EjkidYYGKZ8pDgiIsSoHAr1TBXeWVslUQ4hhqUrwr0LQrDMmTQVyHfl3zLxKf2imiEPzmJca9AhyzlsZutsblR68IMLLxyl2MU90ly+D7fWtPa0U5rRhpWZwEpqR5+cVTIhybGQDGouNjZwis9Xi+SB0k64/h1r0G2cwb4Y6031SjccMl4vZ9/5v5F6kHuWD9daGg== 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 SJ1PR12MB6051.namprd12.prod.outlook.com (2603:10b6:a03:48a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6477.37; Fri, 16 Jun 2023 19:03:01 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%7]) with mapi id 15.20.6477.037; Fri, 16 Jun 2023 19:03:01 +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 v4 23/25] iommu: Add ops->domain_alloc_paging() Date: Fri, 16 Jun 2023 16:02:52 -0300 Message-Id: <23-v4-874277bde66e+1a9f6-iommu_all_defdom_jgg@nvidia.com> In-Reply-To: <0-v4-874277bde66e+1a9f6-iommu_all_defdom_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0072.namprd03.prod.outlook.com (2603:10b6:208:329::17) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ1PR12MB6051:EE_ X-MS-Office365-Filtering-Correlation-Id: ecd79999-76aa-4c52-24b2-08db6e9c4b39 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Pgk9mMpsY2urgIhqXmAGoXzL3yXXvM10FGUJ4oNnZp+Z3wDoXMPsHU+xljCvzWp5LyTs21M7uBi0tjWRQiVX9EeTVWZFqGd8Nzvsh7A39vV+qwJb0PBsgll3njZww0Tx1KLnAt4hs4JNxze10ZA/UKko1nzf4Ij2QCxcTJ/pOwLXIbOmaRYztSmjrY6zsLAOoqr8qCtJKReHE4WVP9YYImgquw0v4xEmV0A1vyOrMvt09cat6i+Ec7mqLPgAa4XTztXBlAG7Xy540NZoQCoxqLLG+8Chcn3T9rNiTQslJCcURu1lTqPY9JafpLC8vu0d6jf63EdQ37oM82CEZQ/2eA9kRHEhMrUbkdS7qfKeOgBNHazsLK795TLOqKLPD5BnQhUiNixbQ8n53EbV1M4qHwvSKIDk049Ef75pxv7uzzWfHm7THxaOuKCf94+6by/xrztLPdvyOqICvFQyLiqFxguhfCQ3Da5r7MmFv1y07ETz0OOR7kqKtJynstYBve53SupH5OgFKB3yqxJ4BPTBT+gZhWf4D0CTrgcVUvMEeS3t/PFsXVwMzwwxmeq4dsATi5jb4g2hqL7342ZOzViehh514sfscwXyM0T/QBf+cZY= 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)(396003)(366004)(346002)(39860400002)(136003)(376002)(451199021)(2906002)(83380400001)(2616005)(36756003)(86362001)(921005)(38100700002)(8676002)(8936002)(5660300002)(107886003)(66556008)(316002)(478600001)(66476007)(66946007)(4326008)(6666004)(6486002)(186003)(7416002)(6506007)(54906003)(7406005)(110136005)(41300700001)(26005)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MW+Sm9XVB23zTEh36hHAByAmtaZli774ZADhXkDD4hjWuQPxYmFMg7AlJ/HHBIut/aRa8mbjzvYRa9sqSXci6Hz4WWSF6N4knZIFCqje+lfuQPNih5Sy8ldynzb99uJ3xesr/IakceNx6uANBE8Xq4SST1Sw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J7GDfgNPtgzlnRVbYVFnh5ftHHn1kqihiLM4rCLmtEf4rA2qoOYPjrkjrx1THyeWpHDVJWXiSBSbtDaE5zvEglrWsNB0FVGlIAvr2NzBhGgcBJ57Nai+HFr8jSS/3yq56pBq11fXLKvoILfehAI6PNMxqC+/OF0211s9GJgRNa2sJgU2AofS/gKDNE6JBkf1TlYI1rvDIfdfO3CcNIKYDoHA4Na+wIBVT3PUJiymHP6q17f5rouu7WsTkCz+AQ0x0NemwIjShap/Nxt7/FuQYapYqJwYFAeHy3vhZdMGsGFy01ouE6UBXalWvbcgsjOxUppdEr3q0mtOwRp9eHZXzNEZWn X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ecd79999-76aa-4c52-24b2-08db6e9c4b39 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2023 19:02:56.0466 (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: o2yZOfniEbWY4EkqeVWCiPu9RCjRVQ9tD8quy89d3NhVE1c86QryOHDqmb9+UJ44 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6051 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230616_120308_180710_0627DB9B X-CRM114-Status: GOOD ( 19.46 ) 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 callback requests the driver to create only a __IOMMU_DOMAIN_PAGING domain, so it saves a few lines in a lot of drivers needlessly checking the type. More critically, this allows us to sweep out all the IOMMU_DOMAIN_UNMANAGED and IOMMU_DOMAIN_DMA checks from a lot of the drivers, simplifying what is going on in the code and ultimately removing the now-unused special cases in drivers where they did not support IOMMU_DOMAIN_DMA. domain_alloc_paging() should return a struct iommu_domain that is functionally compatible with ARM_DMA_USE_IOMMU, dma-iommu.c and iommufd. Be forwards looking and pass in a 'struct device *' argument. We can provide this when allocating the default_domain. No drivers will look at this. Tested-by: Steven Price Tested-by: Marek Szyprowski Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 13 ++++++++++--- include/linux/iommu.h | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 0346c05e108438..8f3464ba204498 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1985,6 +1985,7 @@ 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 iommu_ops *ops, + struct device *dev, unsigned int type) { struct iommu_domain *domain; @@ -1992,8 +1993,13 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops, if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain) return ops->identity_domain; + else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging) { + domain = ops->domain_alloc_paging(dev); + } else if (ops->domain_alloc) + domain = ops->domain_alloc(alloc_type); + else + return NULL; - domain = ops->domain_alloc(alloc_type); if (!domain) return NULL; @@ -2024,14 +2030,15 @@ __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type) lockdep_assert_held(&group->mutex); - return __iommu_domain_alloc(dev_iommu_ops(dev), type); + return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type); } struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) { if (bus == NULL || bus->iommu_ops == NULL) return NULL; - return __iommu_domain_alloc(bus->iommu_ops, IOMMU_DOMAIN_UNMANAGED); + return __iommu_domain_alloc(bus->iommu_ops, NULL, + IOMMU_DOMAIN_UNMANAGED); } EXPORT_SYMBOL_GPL(iommu_domain_alloc); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 49331573f1d1f5..8e4d178c49c417 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -233,6 +233,8 @@ struct iommu_iotlb_gather { * struct iommu_ops - iommu ops and capabilities * @capable: check capability * @domain_alloc: allocate iommu domain + * @domain_alloc_paging: Allocate an iommu_domain that can be used for + * UNMANAGED, DMA, and DMA_FQ domain types. * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling * @probe_finalize: Do final setup work after the device is added to an IOMMU @@ -264,6 +266,7 @@ struct iommu_ops { /* Domain allocation and freeing by the iommu driver */ struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); + struct iommu_domain *(*domain_alloc_paging)(struct device *dev); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev);