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: 13861883 Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03on2070.outbound.protection.outlook.com [40.107.105.70]) (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 A034D1CB318 for ; Mon, 4 Nov 2024 19:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.105.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730748210; cv=fail; b=CMA8DCMbvbqXZUrOAcdHBgUm6sGPK1dbWImKizLVc4IWCIfZNLxrbglDMA+sRm9cz6HZtoo+smhnW1K69ABelKQexeAD/jO3Bgufm7qJ34RnW9EWm1g4eTTVlcOnBfYF4AIlDU4TPz3w5M/DtaWiBOpcJvooEVM5O4V4PcE4cG8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730748210; c=relaxed/simple; bh=8CKgz0CXHgolk8bfnP5P9n7wD4iGkd6sl+5AuBcqB38=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=dy7OHo9gCBmHviUYBCibZX7Ug+IJgVmDc2+A7UVfIBDQ7VpkaR7wDaoILUPpQPydjfz0ld6cLcrKqSnmeLLv2NgyUrwos5BqNzf0HTdcMO1YwjQq71+g3BdWpB2EplhmctuSoMoK8anhMBBGMHgrBNlAuSApKFkW6dJEShRBKuM= 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.105.70 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: imx@lists.linux.dev 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+/Q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eOFeadEV2NsllC4n36Zyg4sgGSiwkruj1pAveCIiFyJ8+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 */ From patchwork Mon Nov 4 19:23:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13861884 Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03on2067.outbound.protection.outlook.com [40.107.105.67]) (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 A05211C4A18 for ; Mon, 4 Nov 2024 19:23:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.105.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730748216; cv=fail; b=jrlf6JkFEVxlr4qQxtirDv+Zs3dQoKL1YtGE4GIdwJRHbx6i1+u7XzPRONlbahzPUL4x4STzAaD/iafRAVY61sEytxZ/qXyNFXxnQVaMkrBAmoVPyjPjT9qaLREJyk2WTqM706wnbgTahX3Hi/aqiYdMn/6IuwACORr1mlPFy00= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730748216; c=relaxed/simple; bh=+aIiIuuWGwNys2L38Y01Wb94GgVrnLzwlwttSKeybKc=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=esYTb7phFpMxVyjM/GMZHT8zntMng3b49S1Dz7oxfOsjv0UVVRtsK0qrBBTwYWkBwQHoPxxz+iPujJZ535I/kmsDy8U0Rof/+85Xzgq3XTSPHFcc+jkHLRHMLludLWPNlfk4g5CLAeOm+/CO9rqKlfs1Scfi1G9cLMRP3Hz9D5U= 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=juJcFmgG; arc=fail smtp.client-ip=40.107.105.67 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="juJcFmgG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pnbi08pdQR5Sjv0tzl90CFj1pduCEPM2/WDkpU3EwfObk4uThHn9EwoPTfLE20xmHAYSe9vCr8/enkwb4f8o/B5Rh0a2a9go0sH8Y51zJqXDGGJnbzzFhlE8B86HexHX4RE32M1FtKOFVSH2AgmhJpHkd3y+P54wKCIuTPt9GVQRWXXStU/1msCOweBql/CQtwJqn6ntVkkdQYLK1JsLoXY57rkesC94V0BT0pq7mugpBEvEPqt37eEkGRmPi/Ule0eWR3hsCBksEyOq3Lbj+xiyiwVv9v41ICsDCzfRtmJ4jN5CEqUah7b3JU5McTCVFe+11LWhf0V7rf/hxr1sYw== 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=tQjjFi3dBR7ct2082uGxiptRJvIFAG5bMc0X6N5857c=; b=ywtpIUbQtKc1NY+VSWVg8TRLEe05TXI8RUPDUoix+hd+YyirDD05NjMeNMK9kawModW+9KQSrzQhjt+mpa7G4uMdm+tiGRcNY63NH7lSUPSAbzVVGnkVG0ezYoYh5NvSeR7k/rWMCXzrwAUUrWIG/kaKeyxLKRe7CUbp+UCViSWPuhj2XgzynfXlx1O+qDFijrdJX3hdz3B+cSWGZb3mzmOh9CZADqLzHGgVZPw10IE5KyIpiBh6VtXUTx4EZIOZcWYEimagGC7tcrHamyaXm0xN7PXv+ohfNvMBDNqoYEMo5LFuJTpH2p7w3ql6RtX/94nsreencyiMrNxz/gwaOg== 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=tQjjFi3dBR7ct2082uGxiptRJvIFAG5bMc0X6N5857c=; b=juJcFmgG7BxPH3Uhv+zCvYVL7VKML83JoSWmE3wfFO6dXJ1NhaR3kVJLXFbJJKxlkxD/bqKCX94hHUp+XfJjVVKrabg+mmDg1edud0OUyuDP90dQBqiLRpkddrlx2SVrzrdw/70qGwFVIug+3rA1nf4DAp/4ZmkjRkCu1DjmPNZN1GZYe7o4aOpcxYJXEG2G0BqPzVAl2csanrYA1eXLOUAscd9F74g0+tN/kFzN+F6zqHhEe9OuMjqyFhP+NtHb/oUd6PSySjz89bGmQbyJF9ockmANbnZfONkTFhIRa0sel4/r6OGjhZOBk4Iv9NeSN2cyEYBHc9w5IJMq2v35Qw== 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:31 +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:31 +0000 From: Frank Li Date: Mon, 04 Nov 2024 14:23:00 -0500 Subject: [PATCH v5 2/2] PCI: imx6: Add IOMMU and ITS MSI support for i.MX95 Message-Id: <20241104-imx95_lut-v5-2-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=8517; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=+aIiIuuWGwNys2L38Y01Wb94GgVrnLzwlwttSKeybKc=; b=bH+v7y/ABezCJFM7lwh4MoCDxsYBRmAGhqGn+M70SXZ2LTSsZcO5wimcUJwldWy2gmJmHmw2V khKjjjEW8T7CVNV1Q5e43DJgBuHEYj6X1kPbUoT8mViWcsImisp7LBu 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: imx@lists.linux.dev 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: 272d4d66-a657-4464-3fb3-08dcfd062b1e 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?g1iB6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nvaxEQfvJ9j6pMtWXRub9pEAHatgDwEXUv7Lni4y?= =?utf-8?q?CvgCTLKCj3gS4Qrb8KNHl0k4MaikFZAdciw7Xm6mFV602hgIyQ0zEruM1aSMPX9sz?= =?utf-8?q?iNE8b0NvA6bNoSq1Qd3A0suDc5MAZPXcZncUYk7dcbPpi5yeke6THB6RgBAoyz4GS?= =?utf-8?q?Ahiah7JsR/DA4XLYDw001qujukXGfc5+eK+j0ubiqenbRXhlgSEH7Mdzud8eTH/Vh?= =?utf-8?q?znF6SefyeLe0GS2lXnUhbZuRPLOvjYfA4loBidMogPzv+jG57+4vNIY5czRGuZ9Y3?= =?utf-8?q?TVi0Seb5p8A0XWZuHa/+bfr3nSqy80fDqf0PV1yow/DVE1G3Ez+1qZ202J4wB050b?= =?utf-8?q?Ok6OX3Us5ylgT83yqE3eNxUDYBt08SrSaZc9CmaRJZxt4I1rGAWuwZ3fUZWNGdNI/?= =?utf-8?q?TeW+2WxFW6dXa7qNV2Rs6gbQbgU93B3XHwfPhHy8OcCsiveYgEv2mnWQQs7cj/xA+?= =?utf-8?q?1YVN4K1pEQNG?= 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?egVicMfh4U+TpxI7q/qmnkFQvm+8?= =?utf-8?q?u0/j+DcaUSK9TzrtYyDSmbyiBdXdKEKI5iR5fDK6fzhT32t6ZsBM2sJsUdK2ocgmn?= =?utf-8?q?asyavkLG+N/ZntFoqMksLfmZLA+wT9S5x+BkVkQlYeWMe7a6HsCVlGftkJcK9gljX?= =?utf-8?q?zdVChpOtuhsdAVcaxDBAhXq/JaOH3ha30E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h4h08QL84aXqjUqgE+jqNo?= =?utf-8?q?MB09cpSw7Qw7bSmpSuNEItxIcKGcGyJ+ImdNILzR0fPN6PkKT2gbW+MRuGSi3Mu1b?= =?utf-8?q?Zo/bicQcOjylsYxxxxrojduLwPGkKl7bixJi33y7G9LfSjPOZ/AKZQ/juIyJ6j2t2?= =?utf-8?q?1hwRmXhtJigeLVYi3U4NKuvyUYFtBXkB+jZQQr1hlW80EPVrf1+x4Q0tyLwwMhMyv?= =?utf-8?q?SKojoU0kW0g6EyWYP7k0Dyw1kUCk9czfReew/lc2XbolhLSkZBT5ODzG02VrC35Uv?= =?utf-8?q?lCoB9bQ/JCuCgp3hRgiW7MOzBjBn3s9BvEw5MAOJ7bEjg7YqD0o91rGyogNwY1pFk?= =?utf-8?q?qLW3QVVdgJgCxC5dQ+wCJLOHJ70TcdbSbK3Bdv8tFCnt0NdRiYfy1wfAt8epPu3kP?= =?utf-8?q?xnQXRSS/Nt4efCXBuyin9c8DKYaSJT0TH6urv2QofEY/JZqSaXDs0d5g=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 272d4d66-a657-4464-3fb3-08dcfd062b1e 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:31.5908 (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: WgnyzpVLsObcU/OSN0wNt/9IaBWWsoFKv2V2zL801v3wVmOK4G4QzQRgqGfDJMv/zrLZNvXOTZAKL+qhNs57fw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR04MB6886 For the i.MX95, configuration of a LUT is necessary to convert Bus Device Function (BDF) to stream IDs, which are utilized by both IOMMU and ITS. This involves examining the msi-map and smmu-map to ensure consistent mapping of PCI BDF to the same stream IDs. Subsequently, LUT-related registers are configured. In the absence of an msi-map, the built-in MSI controller is utilized as a fallback. Register a PCI bus callback function to handle enable_device() and disable_device() operations, setting up the LUT whenever a new PCI device is enabled. Acked-by: Richard Zhu Signed-off-by: Frank Li --- Change from v4 to v5 - rework commt message - add comment for mutex - s/reqid/rid/ - keep only one loop when enable lut - add warning when try to add duplicate rid - Replace hardcode 0xffff with IMX95_PE0_LUT_MASK - Fix some error message Change from v3 to v4 - Check target value at of_map_id(). - of_node_put() for target. - add case for msi-map exist, but rid entry is not exist. Change from v2 to v3 - Use the "target" argument of of_map_id() - Check if rid already in lut table when enable device change from v1 to v2 - set callback to pci_host_bridge instead pci->ops. --- drivers/pci/controller/dwc/pci-imx6.c | 176 +++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c index 94f3411352bf0..e75dc361e284e 100644 --- a/drivers/pci/controller/dwc/pci-imx6.c +++ b/drivers/pci/controller/dwc/pci-imx6.c @@ -55,6 +55,22 @@ #define IMX95_PE0_GEN_CTRL_3 0x1058 #define IMX95_PCIE_LTSSM_EN BIT(0) +#define IMX95_PE0_LUT_ACSCTRL 0x1008 +#define IMX95_PEO_LUT_RWA BIT(16) +#define IMX95_PE0_LUT_ENLOC GENMASK(4, 0) + +#define IMX95_PE0_LUT_DATA1 0x100c +#define IMX95_PE0_LUT_VLD BIT(31) +#define IMX95_PE0_LUT_DAC_ID GENMASK(10, 8) +#define IMX95_PE0_LUT_STREAM_ID GENMASK(5, 0) + +#define IMX95_PE0_LUT_DATA2 0x1010 +#define IMX95_PE0_LUT_REQID GENMASK(31, 16) +#define IMX95_PE0_LUT_MASK GENMASK(15, 0) + +#define IMX95_SID_MASK GENMASK(5, 0) +#define IMX95_MAX_LUT 32 + #define to_imx_pcie(x) dev_get_drvdata((x)->dev) enum imx_pcie_variants { @@ -82,6 +98,7 @@ enum imx_pcie_variants { #define IMX_PCIE_FLAG_HAS_PHY_RESET BIT(5) #define IMX_PCIE_FLAG_HAS_SERDES BIT(6) #define IMX_PCIE_FLAG_SUPPORT_64BIT BIT(7) +#define IMX_PCIE_FLAG_HAS_LUT BIT(8) #define imx_check_flag(pci, val) (pci->drvdata->flags & val) @@ -134,6 +151,9 @@ struct imx_pcie { struct device *pd_pcie_phy; struct phy *phy; const struct imx_pcie_drvdata *drvdata; + + /* Ensure that only one device's LUT is configured at any given time */ + struct mutex lock; }; /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */ @@ -925,6 +945,152 @@ static void imx_pcie_stop_link(struct dw_pcie *pci) imx_pcie_ltssm_disable(dev); } +static int imx_pcie_add_lut(struct imx_pcie *imx_pcie, u16 rid, u8 sid) +{ + struct dw_pcie *pci = imx_pcie->pci; + struct device *dev = pci->dev; + u32 data1, data2; + int free = -1; + int i; + + if (sid >= 64) { + dev_err(dev, "Invalid SID for index %d\n", sid); + return -EINVAL; + } + + guard(mutex)(&imx_pcie->lock); + + for (i = 0; i < IMX95_MAX_LUT; i++) { + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_ACSCTRL, IMX95_PEO_LUT_RWA | i); + regmap_read(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA1, &data1); + + if (!(data1 & IMX95_PE0_LUT_VLD)) { + if (free < 0) + free = i; + continue; + } + + regmap_read(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA2, &data2); + + /* Needn't add duplicated Request ID */ + if (rid == FIELD_GET(IMX95_PE0_LUT_REQID, data2)) { + dev_warn(dev, "Try to enable rid(%d) twice without disable it\n", rid); + return 0; + } + } + + if (free < 0) { + dev_err(dev, "LUT entry is not available\n"); + return -EINVAL; + } + + data1 = FIELD_PREP(IMX95_PE0_LUT_DAC_ID, 0); + data1 |= FIELD_PREP(IMX95_PE0_LUT_STREAM_ID, sid); + data1 |= IMX95_PE0_LUT_VLD; + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA1, data1); + + data2 = IMX95_PE0_LUT_MASK; /* Match all bits of rid */ + data2 |= FIELD_PREP(IMX95_PE0_LUT_REQID, rid); + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA2, data2); + + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_ACSCTRL, free); + + return 0; +} + +static void imx_pcie_remove_lut(struct imx_pcie *imx_pcie, u16 rid) +{ + u32 data2; + int i; + + guard(mutex)(&imx_pcie->lock); + + for (i = 0; i < IMX95_MAX_LUT; i++) { + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_ACSCTRL, IMX95_PEO_LUT_RWA | i); + + regmap_read(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA2, &data2); + if (FIELD_GET(IMX95_PE0_LUT_REQID, data2) == rid) { + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA1, 0); + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA2, 0); + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_ACSCTRL, i); + + break; + } + } +} + +static int imx_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_dev *pdev) +{ + struct imx_pcie *imx_pcie = to_imx_pcie(to_dw_pcie_from_pp(bridge->sysdata)); + u32 sid_i = 0, sid_m = 0, rid = pci_dev_id(pdev); + struct device_node *target; + struct device *dev; + int err_i, err_m; + + dev = imx_pcie->pci->dev; + + target = NULL; + err_i = of_map_id(dev->of_node, rid, "iommu-map", "iommu-map-mask", &target, &sid_i); + if (target) + of_node_put(target); + else + err_i = -EINVAL; + + target = NULL; + err_m = of_map_id(dev->of_node, rid, "msi-map", "msi-map-mask", &target, &sid_m); + + /* + * Return failure if msi-map exist and no entry for rid because dwc common + * driver will skip setting up built-in MSI controller if msi-map existed. + * + * err_m target + * 0 NULL Return failure, function not work. + * !0 NULL msi-map not exist, use built-in MSI. + * 0 !NULL Find one entry. + * !0 !NULL Invalidate case. + */ + if (!err_m && !target) + return -EINVAL; + else if (target) + of_node_put(target); /* Find entry for rid in msi-map */ + + /* + * msi-map iommu-map + * Y Y ITS + SMMU, require the same sid + * Y N ITS + * N Y DWC MSI Ctrl + SMMU + * N N DWC MSI Ctrl + */ + if (!err_i && !err_m) + if ((sid_i & IMX95_SID_MASK) != (sid_m & IMX95_SID_MASK)) { + dev_err(dev, "iommu-map and msi-map entries mismatch!\n"); + return -EINVAL; + } + + /* + * Both iommu-map and msi-map not exist, use dwc built-in MSI + * controller, do nothing here. + */ + if (err_i && err_m) + return 0; + + if (!err_i) + return imx_pcie_add_lut(imx_pcie, rid, sid_i); + else if (!err_m) + /* Hardware auto add 2 bit controller id ahead of stream ID */ + return imx_pcie_add_lut(imx_pcie, rid, sid_m & IMX95_SID_MASK); + + return 0; +} + +static void imx_pcie_disable_device(struct pci_host_bridge *bridge, struct pci_dev *pdev) +{ + struct imx_pcie *imx_pcie; + + imx_pcie = to_imx_pcie(to_dw_pcie_from_pp(bridge->sysdata)); + imx_pcie_remove_lut(imx_pcie, pci_dev_id(pdev)); +} + static int imx_pcie_host_init(struct dw_pcie_rp *pp) { struct dw_pcie *pci = to_dw_pcie_from_pp(pp); @@ -941,6 +1107,11 @@ static int imx_pcie_host_init(struct dw_pcie_rp *pp) } } + if (pp->bridge && imx_check_flag(imx_pcie, IMX_PCIE_FLAG_HAS_LUT)) { + pp->bridge->enable_device = imx_pcie_enable_device; + pp->bridge->disable_device = imx_pcie_disable_device; + } + imx_pcie_assert_core_reset(imx_pcie); if (imx_pcie->drvdata->init_phy) @@ -1292,6 +1463,8 @@ static int imx_pcie_probe(struct platform_device *pdev) imx_pcie->pci = pci; imx_pcie->drvdata = of_device_get_match_data(dev); + mutex_init(&imx_pcie->lock); + /* Find the PHY if one is defined, only imx7d uses it */ np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0); if (np) { @@ -1587,7 +1760,8 @@ static const struct imx_pcie_drvdata drvdata[] = { }, [IMX95] = { .variant = IMX95, - .flags = IMX_PCIE_FLAG_HAS_SERDES, + .flags = IMX_PCIE_FLAG_HAS_SERDES | + IMX_PCIE_FLAG_HAS_LUT, .clk_names = imx8mq_clks, .clks_cnt = ARRAY_SIZE(imx8mq_clks), .ltssm_off = IMX95_PE0_GEN_CTRL_3,