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: 13849823 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2053.outbound.protection.outlook.com [40.107.22.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1CA318784C for ; Thu, 24 Oct 2024 22:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.22.53 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729809310; cv=fail; b=Si7iJaAOQEbuHrTFXg/l3QntR56m7KQkoEiE8ugtYpKBbvox58Aq6r1mmccE37kOfFbGRlgjQEgUUqzh2aXXsS+EIEJ1ap4hQY6YdQkPOnxJEpOQjXOt9FxeYtkJqci2hn2g6lauQ3cp/7dObDQ+nVdkStVNmxf5HSgh0C2Oqcc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729809310; c=relaxed/simple; bh=N+O2/eXaO6AkhaAeZYU5FQuPIX9O4F9QoICBgRu6MQM=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=KIbfNoJDxau/L8Q42ZN6/LG1uw42OAkdUlpHRTVewD7m8oLHy2I1KDW8VBt0q7+nj0bCZ9ffSmc6cL+/u3pJ5iesxo3TW62LtCO3aao7S0Jyut2ZebpxFiTGMSk9qF7MkchnJf2SjQ+vSUjimWhtF0DYrj7qoGkxuZvKiqMVPyE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b=C6Jl82q6; arc=fail smtp.client-ip=40.107.22.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b="C6Jl82q6" 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) Precedence: bulk X-Mailing-List: imx@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: 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?ugqKctoGoh9ka0d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v5HotXQmNtL/?= =?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 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 */