From patchwork Thu Oct 24 22:34:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13849839 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 474B6D10395 for ; Thu, 24 Oct 2024 23:46:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Cc:To: In-Reply-To:References:Message-Id:Content-Transfer-Encoding:Content-Type: Subject:Date:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=gGMO0kSVYnR6lRqgwfKP0k2e/QOTtqGn3+WzHCXK8bg=; b=XemCDfyd0oUbqALvRRYOYHhisT B+Awu4gefL3bHms80qjhnVvUIwjXo7DZmZK5wUqly9y5yqvlZ7uNq3gI2FKkCHA5WoUSUrhD2B/A5 Ys5o53ekMhw47vfWccJ3hTMcCOlE+jVrb41RoMYuoTB9CIGAU2bxtT+Lmw04wWpW/AdfCoKwBx61I 6iqwEC2B7MIBV5dwsqQetyneHlSFr9Dxu6p4DfAKjBmpFL7PwumUiBTgKRhtxuDU0rWMG1OAfdi9U V7u1UQCEwU0TZLAOWN6BXeRTwfdKwy18UDppwTMFNWLigroCiVIK6SkqiLsjh8ZsWcs4bQygJCt9i xCe7709w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t47Wt-0000000230E-0UN3; Thu, 24 Oct 2024 23:46:11 +0000 Received: from mail-am6eur05on20630.outbound.protection.outlook.com ([2a01:111:f403:2612::630] helo=EUR05-AM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t46QA-00000001y4n-2PKz for linux-arm-kernel@lists.infradead.org; Thu, 24 Oct 2024 22:35:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=syK1BQnSiwgKWHZj7DLuHOeNQe33pjjxvQe8w3Z+Lt1W178Lhg34JaJe93aERbgz0RCMYNcMyNyhGMCvMMQaxkd9Qn680WWAzvuff4eQ7OntDAaD8V3tdxLyhrebopfW1Btef5UuXCnz5W+a14TI7dEPxZ5YMeAU23aO47xgtIOao0nbaoW/zrpIGxo1MrqgnhxR6+t5WX8mQGxLx22TEjg7nMz9+DebswD1GZMU9/FnifuwQ5Q76N+t0F7v/co22BjR37Uy8LRJDR8Z+UgkzT3J6+5BBZ4+nwpabtEjRC9yTDAa7QAuwgl3aXLmId7Wop3fZsld6EpEQgsZPqv5NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=gGMO0kSVYnR6lRqgwfKP0k2e/QOTtqGn3+WzHCXK8bg=; b=l2PmKmFaQ8B7RCsG6ckRMvQhLIgEGKwwfCZw4fTooPA0FaC19zItDwK5z9Iy6f5iF485ca6a4ZND64xlbrpuVg2+KkaW81ydwxFe1lHOrMy5WWvcwZOt9ugAWR5eHCVbInQ8ETo+JdAPhntiJc8ogmYskg92fVQX1AnLwP30HlMzIV6gqQCwKC0IuK0sLRSLW5K8d4c8t0LzLuxQ1dPAc7AIi1gM5xixNo1FUu6QgxJd+ITwWgp3/E/gJTQG90bMZATK097ibCrBTcdxsBSt7uhfCqwgt+fMZ2uC9vBzs19nrxXuPRwIK1/XISk4nXMZWF4T2VJ2n5DH3qk46cXCxQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gGMO0kSVYnR6lRqgwfKP0k2e/QOTtqGn3+WzHCXK8bg=; b=C6Jl82q6QHeuLTpFdImfbJRPxoZWIsdxyJVuwuuQC342UYDTDuFj8kwsc1dchGWzjoZpVeGs9sEsboM5UTm8O72HMz8iSKm2JliklU6b8CeOBv4gMoh5FlLATHLglLhzTJURO0BN4naSpMxLnOVt/ATIPLOZ6uBy6X02ES/3IL8GgyuXW+w0PwRcHCCovswX2DoiWBo4eHc2zigZcDwwdHe2NZK3DGNZLm2tqsnIBRHUYw5CO7iFeppb37JGTiqCFqm8QHMzenmyOTlNuZv9Ahw0Io4B0/S2i0U3juaDIEJFjz1cwpB0pNygjZXGfi4OMzZkZUbZhE7EX+Je+L5MOQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by AM7PR04MB7189.eurprd04.prod.outlook.com (2603:10a6:20b:116::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.20; Thu, 24 Oct 2024 22:35:05 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%4]) with mapi id 15.20.8069.024; Thu, 24 Oct 2024 22:35:05 +0000 From: Frank Li Date: Thu, 24 Oct 2024 18:34:44 -0400 Subject: [PATCH v3 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241024-imx95_lut-v3-1-7509c9bbab86@nxp.com> References: <20241024-imx95_lut-v3-0-7509c9bbab86@nxp.com> In-Reply-To: <20241024-imx95_lut-v3-0-7509c9bbab86@nxp.com> To: Bjorn Helgaas , Richard Zhu , Lucas Stach , Lorenzo Pieralisi , =?utf-8?q?Krzysztof_Wilczy?= =?utf-8?q?=C5=84ski?= , Manivannan Sadhasivam , Rob Herring , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, imx@lists.linux.dev, Frank.li@nxp.com, alyssa@rosenzweig.io, bpf@vger.kernel.org, broonie@kernel.org, jgg@ziepe.ca, joro@8bytes.org, l.stach@pengutronix.de, lgirdwood@gmail.com, maz@kernel.org, p.zabel@pengutronix.de, robin.murphy@arm.com, will@kernel.org, Robin Murphy , Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1729809293; l=3684; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=N+O2/eXaO6AkhaAeZYU5FQuPIX9O4F9QoICBgRu6MQM=; b=VesD61ANJVOLjtQA0ptlj+ydmMtuGCK6E3XEPit1tm5yjGFYLk1JG9SUmyNEXlvm+mv7TFfi3 tBpSJnUFBGeAH4L5S6QmS08eutaB4dZf78BZyrkag2KF3bxgEhWatiL X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR05CA0001.namprd05.prod.outlook.com (2603:10b6:a03:c0::14) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|AM7PR04MB7189:EE_ X-MS-Office365-Filtering-Correlation-Id: ac1fec68-e554-4d17-5bdb-08dcf47c1bab X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|52116014|7416014|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?0VRUBnG0/LpTrXOf8q9gbaaxMxJE0rU?= =?utf-8?q?emSdhMBMZj9XkoW6z1w5eeLsFkMTNH+xlg7ASUeKvnzHcgMoa1CI33pv5tNMqOLxE?= =?utf-8?q?43NshcagK+bQf8Ofw1hUf22JLpcLr1wBBLEVk3ZVHypr/kYlPxc5dADwiWpZBD9C7?= =?utf-8?q?vfgqRVfzwEvtf/NhD2rMgVAeJ2otLSr6ncOA3eRlQBzepH14F0f+FJmZkn7K4ZGhQ?= =?utf-8?q?Vz6Ev3GkCd098OSchUsAotjPEVT/2GP3sBcEFQwGDPBtQaj7x/XnhyC+D6mA6aDam?= =?utf-8?q?q52iq7Nbej5wFSL1+4f0WRuwwtUcbMNQbt1UXZQEHT3KcnQnrQnFMmXl7HvMIhPyE?= =?utf-8?q?0ln4luB1jN9QwVSIv80oEdxgH4NfOtjWi/wfYSuw5JZosSB3RqjuWKntsLdt6d1BS?= =?utf-8?q?xOPIDDSqZo4SoEVOaVM5J038WBhgwJmIawwnJFYG2SCvFR3bfI9brWtSg7H9d9+4y?= =?utf-8?q?uQj/FKcr/+if5uqZHGvWAcRYIMudf/AJpsexgT5yUdFH0vF1gpepmmyDCh3RSB8KM?= =?utf-8?q?i2CwRKR/fT0VLJwqc4E7X69uS/1at3XHEyUlTG4t5GZyXI2LoJ5gnPRwy7Kugr/G0?= =?utf-8?q?N6qNiJR8WMvRDrTYLlVk4wFT33vxe0NlBZ4vgKOfmhCWMNuBAfhqLWe311M0h5BH6?= =?utf-8?q?2hBDCskDjSP1jQ4ESzUAJd5ozbBP0maBmBF4JCmNe4wcYfXFKbMqP95zKl7Eny9sd?= =?utf-8?q?E/jqLSHzWQM9iJiMDyIOeWd5HguPfxAOLJ8QkuNfvah3wwyvKyta9NaZYpy9jkzEu?= =?utf-8?q?DUrWCAu/I+p2JaOU/a+LhkEM/JN/xLw/BIgCLD7UqjwoWjgXjT7gYmfV7fKIbFF7l?= =?utf-8?q?Uc5DsZ2lYJaBvdPJrkaMDv9CDcVM5bvNebp/a9/keaKAazSb/ypoUvXnbgMHDNvPR?= =?utf-8?q?ZIS7jzZf/6e6zJpp0+yINhtXDYQPQuaJrjQH0FH8fjzZutJ9OuM6YywSMsCv4FkWN?= =?utf-8?q?Jm99vsgU79ecd/1fRruOpmyARStQocRnWuvufhR6zXpWqU48NUAZL91hz+b+OJPeX?= =?utf-8?q?VOY2RluUClCfuOkNjqgggXND6Qv25BvkmWrBMYBPsU7chdvdM1i4udUTxtzfAlA6N?= =?utf-8?q?0DF2dLJi+qDvcqv5hGcw5E0DN2f0wmqXvDn/BrGUYqSCWt0t1vCjXSBYGnAKayBXM?= =?utf-8?q?XUCf26VidSAYtdmxi02/aNoKlUKjyJHQYlEoSI9X5hjs5PfOkvfNiIlI9R9oEjJkI?= =?utf-8?q?PEQOSlaJCj6W6ko2CaHMY0aC0BpGI9BhzxK7/jl/KjydwsrxeuWhz2HrNdfbOUaLj?= =?utf-8?q?MnFW/QUuNN8GUZUTPXIlhssLhjvTyjFjXufOzndiMu/vdoy3YM1ua9cEamsUYdG94?= =?utf-8?q?37s7ZMYDMPrc?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(52116014)(7416014)(376014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?ugqKctoGoh9ka0dGfjHNQb6z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L/?= =?utf-8?q?3ddUJ6yrfYfLosdMWwKuEy19Zefl3IWTg2ErgoGBMtZT247WR0Sfe2Q6tk7YzbI7D?= =?utf-8?q?5hEJSwJ1iODJVblXRWDj3fpzwCmexG6H1cpUS/S2mrD/xORUaUGlIaOlB1Zr58ysq?= =?utf-8?q?/dBjoLwGV2aEp9kZM1XlhQntBcqoQ4JoZlhS9S+r+2K/mSaT+M/+KTqaUvRDeDrSV?= =?utf-8?q?a5He2d5CZTlrdlBnrv2FH/ms3g7UqykcsSLEO+8gRArOp5o+vgvIEYlh200qHpIXx?= =?utf-8?q?BL7WGpQXOtjGgjKhpShlNIeRfsGlo0Ea5VMGord7uCLhH8QBStm1gYdQIFxNouDHe?= =?utf-8?q?sFaSz9SEygkyiD6QG2vNj9Yx730q3BV91uw0Y1978sPL8s+T3XeHfNW6onHV4+S41?= =?utf-8?q?RTWrLCgFSQkCka3YQyBXSHWgymZW9FG8FxIXzfvwCYGwButqXqucwQwB14ZAFOHln?= =?utf-8?q?IczpK7yFGahWFJx0qAfCQYKucqbVKJ4puPfEcmJ0BKI/1dotXURfSbv/DXULxQVbz?= =?utf-8?q?eSa/U1EWwWFp/Kd/g2pwzdEfRjUxdczyhsM2JtbwD8IIiwgycKylvNWMspTSd+efa?= =?utf-8?q?b+yHJveC22PzjKCBGsp+/0kSjDkajH4amR5oRe0IC5waNp3bQF+CHwzLHRMs1R6bY?= =?utf-8?q?XbnFlYbZSC+x?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ac1fec68-e554-4d17-5bdb-08dcf47c1bab X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Oct 2024 22:35:05.7773 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1xd9i01Xo/MkismE9znLBTH9SE5jbDnsORochsx7+/6Ep9juSUx74w1qxqZcae1ipQ/0XUxoRLdKDQ51CtHeFA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR04MB7189 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241024_153510_659957_3D873053 X-CRM114-Status: GOOD ( 21.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Some PCIe host bridges require special handling when enabling or disabling PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map Requester IDs to StreamIDs, which are used by the SMMU and MSI controller to identify the source of DMA accesses. Without this mapping, DMA accesses may target unintended memory, which would corrupt memory or read the wrong data. Add a host bridge .enable_device() hook the imx6 driver can use to configure the Requester ID to StreamID mapping. The hardware table isn't big enough to map all possible Requester IDs, so this hook may fail if no table space is available. In that case, return failure from pci_enable_device(). It might make more sense to make pci_set_master() decline to enable bus mastering and return failure, but it currently doesn't have a way to return failure. Signed-off-by: Frank Li Acked-by: Bjorn Helgaas --- Change from v2 to v3 - use Bjorn suggest's commit message. - call disable_device() when error happen. Change from v1 to v2 - move enable(disable)device ops to pci_host_bridge --- drivers/pci/pci.c | 23 ++++++++++++++++++++++- include/linux/pci.h | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 7d85c04fbba2a..5e0cb9b6f4d4f 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars) static int do_pci_enable_device(struct pci_dev *dev, int bars) { int err; + struct pci_host_bridge *host_bridge; struct pci_dev *bridge; u16 cmd; u8 pin; @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) if (bridge) pcie_aspm_powersave_config_link(bridge); + host_bridge = pci_find_host_bridge(dev->bus); + if (host_bridge && host_bridge->enable_device) { + err = host_bridge->enable_device(host_bridge, dev); + if (err) + return err; + } + err = pcibios_enable_device(dev, bars); if (err < 0) - return err; + goto err_enable; pci_fixup_device(pci_fixup_enable, dev); if (dev->msi_enabled || dev->msix_enabled) @@ -2085,6 +2093,13 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) } return 0; + +err_enable: + if (host_bridge && host_bridge->disable_device) + host_bridge->disable_device(host_bridge, dev); + + return err; + } /** @@ -2262,12 +2277,18 @@ void pci_disable_enabled_device(struct pci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { + struct pci_host_bridge *host_bridge; + dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0, "disabling already-disabled device"); if (atomic_dec_return(&dev->enable_cnt) != 0) return; + host_bridge = pci_find_host_bridge(dev->bus); + if (host_bridge && host_bridge->disable_device) + host_bridge->disable_device(host_bridge, dev); + do_pci_disable_device(dev); dev->is_busmaster = 0; diff --git a/include/linux/pci.h b/include/linux/pci.h index 573b4c4c2be61..ac15b02e14ddd 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -578,6 +578,8 @@ struct pci_host_bridge { u8 (*swizzle_irq)(struct pci_dev *, u8 *); /* Platform IRQ swizzler */ int (*map_irq)(const struct pci_dev *, u8, u8); void (*release_fn)(struct pci_host_bridge *); + int (*enable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev); + void (*disable_device)(struct pci_host_bridge *bridge, struct pci_dev *dev); void *release_data; unsigned int ignore_reset_delay:1; /* For entire hierarchy */ unsigned int no_ext_tags:1; /* No Extended Tags */