From patchwork Mon Nov 4 19:22:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13861877 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2047.outbound.protection.outlook.com [40.107.104.47]) (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 AD7391BC9FE; Mon, 4 Nov 2024 19:23:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.104.47 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730748208; cv=fail; b=Zsd2qq5rRiston81RKJ/afkf6lIoWx0x0C4laL7pdDxRyK1ppwCKfywbzW9KkRLeYSgWgDbKlJ0Q+tfwnCgmnJzadhsal2sC0gpulwVRYHrXd7giw5Svs4MY7XIyv+W5bFceUF6xRDPnjF046PWRxNRUkxL8MPEGsZUMg9fHQks= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730748208; c=relaxed/simple; bh=8CKgz0CXHgolk8bfnP5P9n7wD4iGkd6sl+5AuBcqB38=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=pgfDPaUcX3tivTAR5dZzqQj8jabC+ei2cWj8UYgexLC6TeGGq7ihpVRB1qOd/x6mzno62Lk39xpiu2Jo4uGn6puutOs5TWBVEkSa5hdnmFk6AxxndrrnhAct+uOfkMktzwcwpVEA4wLQxoljcXZLdfWZPsRzCPYAxHhrA6hO9ls= 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=H5n+TbXf; arc=fail smtp.client-ip=40.107.104.47 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="H5n+TbXf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Zuj4S/8NepRymJCwsAiOQOCLVLsiZ9KDEe5Ge/EinzaxNlAxKN8cXkae46x3Qn9QUmQ+zulnIv/hZDRAogWyWhxViGPL7Wq+k97RBK8k8+zlYknOOt1/Lx2c6DphWjhlp4CjPyfYTKQ9HX6aS6IpuBG+TvqgNZoDLpWaHzsfAZvRvm4Vgi7okhXr/8TSQDQwm8CAgUdNoUPDiAPUw91CgoNZx8pn/oIPdkV3Al+jmbkmL0VAyyYllCl/AxVnFRz85/0VwySrmmcYihAzncv0RbAiZxqsU52lftiGO5RHSAu+J3RD09MFXmrkGqFOrl2UuaRD4+TajGZCT3eM7ehu7w== 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=3+Z6+TCJdDoy8UwE3Y9/Bs3kkPwtKnXI87qBGTTmAy8=; b=WuuFIkMTkMFDtbC6AaWKOKVoVXHL1wj+7d5L262b6Svpo///PokxbKDd4Li9bxDBVjh931rC6/25nIoOoWmml2Rm04eb8VZRP9oyfM2O1p7ZbYbNxyZpb2m6sYAufHZpsUxk+VkZQFwgTBqvXbG50E/zsdjFPuSdEtOSt9YJu2X3mssTvAlGAF9YAAaKT/XXy0kqdgcFZ87nOvRyIDEBX1VsN/r15TZyK3aUN7dYZ1KR2bzXUDw0e3pkYrfmO3qx0PzEonLXaWJlgZJ723N1AmG6CbOcVO+eqkFSR90JIt2+O08binJzFkN341pvoM8U2YjknnyIL4KUNbIbikM3YA== 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=3+Z6+TCJdDoy8UwE3Y9/Bs3kkPwtKnXI87qBGTTmAy8=; b=H5n+TbXfPYzDEDcA/oebki1wKmfIbcDLvKzJqOYq4p94nu+QxpBVafwJIqOHBZxA0Lt3BT3ZDleP1o921ghEK3/lbSFf+mGGdVOoNKmagxLqWWoXzfyAgsZ7VE1hpOF8SK+ojdqUiSw+/SwwhlHbpgfxIQS5+NHdp0TNLYvNS02B821a+V449DPl8/mE+R0ePkXwQZu/TMZjB2YJIg9P4FkScs4BTcE3T4XuHcPfgSO65MwMDVGnQGkS74o/UlScrnEljJ4yAiDD2XVEjS2Jd8SCH8Q+Dj8k/L76z19mnVs90ldJIaR4KWW4HKZNTMEbwnWy16H5m+TF/sc7q7Bvjw== 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 AM7PR04MB6886.eurprd04.prod.outlook.com (2603:10a6:20b:106::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.30; Mon, 4 Nov 2024 19:23:25 +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.8114.028; Mon, 4 Nov 2024 19:23:25 +0000 From: Frank Li Date: Mon, 04 Nov 2024 14:22:59 -0500 Subject: [PATCH v5 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241104-imx95_lut-v5-1-feb972f3f13b@nxp.com> References: <20241104-imx95_lut-v5-0-feb972f3f13b@nxp.com> In-Reply-To: <20241104-imx95_lut-v5-0-feb972f3f13b@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 , Marc Zyngier , Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1730748193; l=4072; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=8CKgz0CXHgolk8bfnP5P9n7wD4iGkd6sl+5AuBcqB38=; b=dcSFC3MwwFcbr4holoLbUXJvikUCgME+sFBu83RzwZi1VDYdbP9VGI2nFYMUFYBbkkwMK/DSF eer7aKWDJBlBGACBF76LbyFgji0htAtHM/DV9UfFTF1vrQ1YJByB5Gc X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BY5PR03CA0025.namprd03.prod.outlook.com (2603:10b6:a03:1e0::35) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|AM7PR04MB6886:EE_ X-MS-Office365-Filtering-Correlation-Id: 8d239559-d862-4aff-cca8-08dcfd06276a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|52116014|1800799024|376014|366016|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?0zEnWK8vX6d1AGSh875Hsb5eekHMfSb?= =?utf-8?q?zKj+JGnRhPzeb9Gqq2fBnUi7AsbYjWniOHlXROIbT4xw8SUxK7SSMVyccOo/y/rvr?= =?utf-8?q?jMw2qUmR2m1H6pJ7MxTJXhzBVanppsQIbxz9xIYgMBlMlYvf6NlraB00bDs8BIyR2?= =?utf-8?q?AwwZq1A3FTL8uT9hoepgzN3eAVxC+pvD0pmESxQ8yyFHBvMCjlxaFyii1IxdPzHj6?= =?utf-8?q?bpIlOwXIJs6KvPWtG2IC6C+0zyzKXVVJ3ETgVZc+8awc6M0Y580bJAMnn+F2B2lB+?= =?utf-8?q?1rbq8kG0PqI+9W153S18g8/DcABxYImiA0maO5j6pjOmVWmoKHSFCU5xL6Gr1ZA96?= =?utf-8?q?DubKM9gK8Ki12DrsPSB5mmR10EXvhQ+/YuHy2s15R+ph0h7y7tZyDThPCIPaaWCu1?= =?utf-8?q?7G/jsfJbdNOqDQMcDYbyDMLIGGA643mnMEK6vyKfWFW94bTW0SE6l9N8uBInGaTFY?= =?utf-8?q?t7D2jVnhpvU1g2e/GNAOMNbIGVMf3cNdAEcAmo7IGRSyEIuis8Ew6lqzXfSsBhJK3?= =?utf-8?q?PHSkxVZW3Y6P3zuh8duhO2u4fHq6i9YYQsgT6l6wmHleFHAtmSdXWuj4YE5PGpr3d?= =?utf-8?q?gIa39ylx6wc+k35nZfUV/q6W5hoo+/IX+mS5Sc7S5z06ZxS1PgOZp7qrD/js1ZzCl?= =?utf-8?q?dK089eHs2OKwlVDKnBkTyUnZj0gCn4L4Cf7dDEsVx/eqvcvOVdr4Hd4WmNnvSfTqJ?= =?utf-8?q?mYJFms0yZthClPQ3O6lOJ9UFo0Er5gpXOe/2ft5QbfqifaWh4AQqtbNfM0oi8oFUk?= =?utf-8?q?/TKg84y4l8trF9YWPlwUOGZ0F85MS5sLm/wrQ9aT3DjB6rxMVw8rIHrb06ZBSHmz5?= =?utf-8?q?7TcvEn8EAXmU35OKBfl9h8jWL+cgrqqebHe6FIJFLuV9HFcNZWQ0HKFkBzQm02ZeL?= =?utf-8?q?gw2PEIsZxJ4sptfFgj5o5/L9x/Z3Qwl8Lmwqa2PDghhdQTVkHNTzt5TjOXJKZTKUM?= =?utf-8?q?cI++Mq52kO7j2/eQaHQyB7Bf28TosVkRogMCjtG8m5cvqLv5eQZ5lvfvjZgl8wX+p?= =?utf-8?q?zqEpQC9Y1x4nsgbOTZSbuBJknrx5cTgB/nIianYwI6cfl3mGtalkNrMoH3PYkVkfe?= =?utf-8?q?M+tyCJ5p2LzKgymgCT3iraFC8DRuvhtAYorjn03xGqutx58klb9KWBCJTemHw50nN?= =?utf-8?q?lyZDjDspyycVA6zA3O3vml4ykY5RVADz2W647iAgLSjs8ccu08YnhUhohacBM9jpi?= =?utf-8?q?evaUnjTuLDiff8n/mNy3NyamE05WLJcXpI7btg22Y/akxQtTAa8mjR3yacmYRfSEj?= =?utf-8?q?/fKPDBy8FYephYaMorkORHDeg/Dlc5bkefE6glh/jH7PzybtNK4t9RAXNItClW11r?= =?utf-8?q?p9w0RYLaoHhs?= 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)(7416014)(52116014)(1800799024)(376014)(366016)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?nUOP8p61J/9um44lX+WYQ+/QJ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OFeadEV2NsllC4n36Zyg4sgGSiwkruj1pAveCIiFyJ8+bISw/8Lpytm8zFEIBKMi?= =?utf-8?q?6d3ZkkSH9E8Zlr2ST+1SojZ4BNB4TokS58tFOFMTw2lJ6s2dpW3+zIjlPDdKtTHtK?= =?utf-8?q?AKE6OqvR65gTuf1d8Q+n0QTm6VeBxIW2n3NuBjIW47E9Qe1/kwvGPGDOby09q2Wdg?= =?utf-8?q?FOKdsiJPdQ+/7jxvtm6IqaFxzxG+pIFikyUV7OEBRLxhRTe6xRZT8qoD+cK1OuhA/?= =?utf-8?q?yx/oLM5fzjwR32eVkKtsg4NodH44HSrh0QIwF/MCAJwxmhllZeOzIT21Ip9kjZ5lF?= =?utf-8?q?Q9gj+/CujzHPRqB9YlYh9YxO3R1nGg8cJi7jTdR8Ioy64At7UTufS6kfwf3NwQGpm?= =?utf-8?q?5GHUCSoLMf88yLC/UAaQ9V5cNTwFaRbLzsfhnueXpD/BPLF/ichf5pSUfb29Sek6Y?= =?utf-8?q?z9fg4OEj9MaSIQr1FQjFL+6L2iY6LYLSZ6gcLAFgLUys95hqR6rJ2jlooqGMluoZJ?= =?utf-8?q?c/DkNjul5L74vLmWEooaUOyAN+xq9LyFnEI5JtfLSWZLBjzgkKmv3LEW39g6JP1i8?= =?utf-8?q?SHwoeP76dxZ8KPbYiSQCw9kP4NRTmZ3jYoQieJaQmtXsdO17M25HiJqs=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d239559-d862-4aff-cca8-08dcfd06276a X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Nov 2024 19:23:25.3793 (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: 4wA3lh6PyzaTVjOD2B7Th2iaOFuWMwfht0iRo6RjBHvxGzEiuPzSBNZb/0JfLdK9uou+krexcaJ58JqE5EBsnw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR04MB6886 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: Manivannan Sadhasivam --- Change from v4 to v5 - Add two static help functions int pci_host_bridge_enable_device(dev); void pci_host_bridge_disable_device(dev); - remove tags because big change Acked-by: Bjorn Helgaas Reviewed-by: Marc Zyngier Tested-by: Marc Zyngier Change from v3 to v4 - Add Bjorn's ack tag 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 | 36 +++++++++++++++++++++++++++++++++++- include/linux/pci.h | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 67013df89a694..4735bc665ab3b 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2055,6 +2055,28 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars) return pci_enable_resources(dev, bars); } +static int pci_host_bridge_enable_device(struct pci_dev *dev) +{ + struct pci_host_bridge *host_bridge = pci_find_host_bridge(dev->bus); + int err; + + if (host_bridge && host_bridge->enable_device) { + err = host_bridge->enable_device(host_bridge, dev); + if (err) + return err; + } + + return 0; +} + +static void pci_host_bridge_disable_device(struct pci_dev *dev) +{ + struct pci_host_bridge *host_bridge = pci_find_host_bridge(dev->bus); + + if (host_bridge && host_bridge->disable_device) + host_bridge->disable_device(host_bridge, dev); +} + static int do_pci_enable_device(struct pci_dev *dev, int bars) { int err; @@ -2070,9 +2092,13 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) if (bridge) pcie_aspm_powersave_config_link(bridge); + err = pci_host_bridge_enable_device(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) @@ -2087,6 +2113,12 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) } return 0; + +err_enable: + pci_host_bridge_disable_device(dev); + + return err; + } /** @@ -2270,6 +2302,8 @@ void pci_disable_device(struct pci_dev *dev) if (atomic_dec_return(&dev->enable_cnt) != 0) return; + pci_host_bridge_disable_device(dev); + do_pci_disable_device(dev); dev->is_busmaster = 0; diff --git a/include/linux/pci.h b/include/linux/pci.h index a17edc6c28fda..5f75c30f263be 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -596,6 +596,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 */