From patchwork Tue Dec 10 22:48:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13902417 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2085.outbound.protection.outlook.com [40.107.20.85]) (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 EC7D51F1917 for ; Tue, 10 Dec 2024 22:49:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.20.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733870963; cv=fail; b=QOWu/y6wH58QBk4x5aFyZsT8fx6On3QoHK46ROjpuPknf3MqkXc2bgHUW53s6CDMWQ6lldT9LRObqqaEZ9Qbz+FrKeA3RTHrrXOPca10MKssvG3TZGxRJY9zrd8YZL5v83EKYilNzrT04Mu9aiEl9q8gn6uHpE9fdJZokN5mvW8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733870963; c=relaxed/simple; bh=HRvECFQA7StWB7pWk/IGaoQ4Bvx9a2u8BEJbLRBEmYc=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=HG6A3qyWhJua1SuTnSCLKmAdDImnZgQRe1aj4mDNjKjrkJBfhZh4b+Bo8O9ONJ/z/zoA+NoyERqeyIk/57y67rq9rgRb73X90zfpLoBg9fE55kx+uVTavhR7kaBugaWoWSQwsWbTsItB/ITDpIuFuzJjCdM2WAOEY3DvINjxI2k= 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=bVZGUtQB; arc=fail smtp.client-ip=40.107.20.85 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="bVZGUtQB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AcJyiUXoh2kymg2rHe6b5Y7lX5gbiIZiqh4CsG9lhpCEx4ow3x/uPafBzuZ/vi/93BM6AIZL4ngtxYAG9jaCQ7fqHFKvoZSqZRLLUQ98ePv8aJICbhxtR0tmtv7rK1VRVM9ryGxF2+21ecll4qneArGTztls+YESRZsTCJUSNyZ6PnI/46JSW05A1Y7a3eyC/lUsnnambCtuZq1Slzu3E01+YhSKmHvf0QYzA1xz4xdouOwV5mHskrb1fVEz83jce4RU+fsqAt17bMtPDC2KvB38uRyAVI2t3ZubHVTmFu762JW3ZIZvkgSxnPsvJcmhXEbsO20VLmrEfeyrQ0CdDw== 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=E7IKGdrjwj1RdTdGQ56GK0ia4qm/q9umJDVy+Eznp6A=; b=A/YuIIYjjnXX9NKbt3FjFt46jN1AGU3hmjPm64JihzP0XsxWqenimhJGgmCyfk9woJWune6bm2sZ83LuwGvU4tm8tmtyIesnsa2iTzAESJWaMfljv73SoHMXc54qSJVY/iBdYIFQ3EKPJQJAxJhLS+sumCicAMyQB65cUkGbXmosXCRyXBizYiM5xBQiB3UsUmvgmq9m7itkHNoLTS5ENWLij/JdvMiF6yKSZ4do/a61UqLH82K44ZBaMrup/ysmjv4uA+OTUIcLFXhEhEniOcjM6IYcZD0pau2bw2jv697pTSVmuPZp4bZ4ePLBtpXSnGnQjjcnWsa8TswrRR1vSA== 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=E7IKGdrjwj1RdTdGQ56GK0ia4qm/q9umJDVy+Eznp6A=; b=bVZGUtQBg/NIvr7/uWR0hc68DSRjMQA+RKZ9oyc3UG4RnfXeX1GU5pHkBCU/DAeGEtX3INsOYc57hN//z+2i8G1vSRgKJKAD8A3fFuRE5dl1nwBJsf3ooO+czUNItrjG8heqiNR8Vd+0KiLYse2YjcIcNlJYXe3vLxmInaS5lic3ivJI8+H7rf8H5gdxN4xDEsoQZv8NPyLTtMeg7pHsg7yFuFSHwV/dToI8jOaHnDbW6mTkEwYwGLm3QPuaJamh+tAeML4/tkXpAwXiJe9mtQOHG5DVvL1B32Nts+poxXcx3WO9Ehjd3Q4yerYILrgjRW3gvTrKjzJbTKrF9mY05A== 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 VI1PR04MB6784.eurprd04.prod.outlook.com (2603:10a6:803:13e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 22:49:19 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%5]) with mapi id 15.20.8230.010; Tue, 10 Dec 2024 22:49:19 +0000 From: Frank Li Date: Tue, 10 Dec 2024 17:48:58 -0500 Subject: [PATCH v8 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241210-imx95_lut-v8-1-2e730b2e5fde@nxp.com> References: <20241210-imx95_lut-v8-0-2e730b2e5fde@nxp.com> In-Reply-To: <20241210-imx95_lut-v8-0-2e730b2e5fde@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=1733870948; l=4347; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=HRvECFQA7StWB7pWk/IGaoQ4Bvx9a2u8BEJbLRBEmYc=; b=2dHOhD6AYxIFhpI9rhrCSVylul64jHTTKVtwVlC1m1h8Riy5dVxCbEzVSJo6NEKzplISAyhxh crS4db/POyBCV2ybZLILuQwkKEiGHayzmVm1Do9ZfXLpjLj6M5INhdD X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR05CA0020.namprd05.prod.outlook.com (2603:10b6:a03:c0::33) 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_|VI1PR04MB6784:EE_ X-MS-Office365-Filtering-Correlation-Id: 014e3707-9ed1-4644-0f7e-08dd196ce22a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024|7416014|52116014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?X1u0wMl1QopHKBewb2ykdjF9ofnKaP9?= =?utf-8?q?mfJeD59ilAJ7tKrmc6pJfMf5zB21qwlcx8GcfYhhtMmHCkW4+Zf/9HLRNLVyMPtMl?= =?utf-8?q?vWzhJR2Jznfh6C2001tA5xv2ebK4+FiQhVMPovT73dQqOJObKxlaVjI8ALF47nTRE?= =?utf-8?q?VAlJebyiLdLUPfVsRalstCPRBKLXj1Dvr1SlfaY8GG+TCVFbvc+7hXsuCM1j95Gc2?= =?utf-8?q?Q/1mklx3jv3nTJUyhwwOABfY33YzR8dN7gQasHEHAGPkW3Uy8QkJPnfxadc/eju1l?= =?utf-8?q?15ql6TxCPBqbIfMWLD8ny+Vhm+Tjnkh5aaGQOmJGi9/IvqLoDnaA9gKa+p9zFIbd5?= =?utf-8?q?QBdpoeDbmOgOhw0aapW0SX/0qqz/Sw+QxF3Fu8RuuA0BxvSB2KvRyrp9mMyqhDiHA?= =?utf-8?q?xiX+XB66yqtcGCqbVxkWElCKGzfN5YIe+wpSWp/kRAy2pHQCP5/4mtYC+1DRweD2j?= =?utf-8?q?BDHEBpKldKPtxDHoV9N7fBQWlJoOp7UWjv9KbB0uyZU36xPa3zYo7fmegJ5sb98mX?= =?utf-8?q?B+wNZS96ma/hFmtIEbeQ3Y9K7hU4LhrUZpUEbUWY6Xkch9FsaS0Tr01Cjd4Arrqfe?= =?utf-8?q?zuBau8Mq0Z9819ATDifCUWxY4/34q8zfDSNYG7JyqqepkS+4spRQ8v+ur3X7c7Gwv?= =?utf-8?q?hJXoyy9tapxiKai8Kx9wvfVfSJRBouy+gn2ef/fH4mTYOs4/tN86CoMz7aF5xynMI?= =?utf-8?q?r9rYihet7gy64aP+mVzpOTNefMufvE/1YNh/i4RyARFQqr2ONyXEyEQSa98/qRfc0?= =?utf-8?q?ABHns7QaNO3w4hsVTZ5sVpS53vD8QyWMMipl1QrL3ZH9COtv2sh/yHAvCguZIMhF4?= =?utf-8?q?1sYE7jEjNLYNn0nDlu0DISJWBr1hk7rq8H4bJeXpaftcwKvZB/liLQC2hQg4f0nZN?= =?utf-8?q?JYyEjgt22NxPU1cABBZGKvuLnALgpe+hk+7bjbAWpVuHRwwByTANZV8nIYUA9QqTh?= =?utf-8?q?CCnsMWUM3JZbNJOF8cTvyLIOiMa387L7ZlvY2cyyXtClv66VENUNdNIGpRtMEqXRU?= =?utf-8?q?TwaFfaRjbod5w8YoP/ZIscANRvrIG3N0xkq890A3k5Q4PSBcwUIPK7xHGEhCC2eB+?= =?utf-8?q?PtlmGKuE1anmtIlwbzmbs7HLnlsib67nD5dDcqTdsj8mOIy3x3w7eL0jjSDrX0EO5?= =?utf-8?q?zjzN9SiKOTZjAILP4vQqS4KQDAq+T1yvhTA4eqoK/Nzy8XgoqYr7nJPl/HinltfcG?= =?utf-8?q?X7CRTQCcUJJUlN234tg1LEc6Qaff3oIuIWQhnbwtVnbsPTh8+dZXulwDmqfyK7gG+?= =?utf-8?q?q7Gj1wJUEPOoM2nAuom9ndV5bus8Da45DCFl6LSdrB+NY0BDFseu9RzPyiTpnQufJ?= =?utf-8?q?KU3ekv7JJ07j/5d0T8g3IinnFmAn6loJwKGHCwxkLrpqLUQyTB6b8i0=3D?= 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)(366016)(376014)(1800799024)(7416014)(52116014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?xQI7X+UUHiBWb3aJP1rqNKgg6hCo?= =?utf-8?q?8HAH4VJjJ3bpny4ftHBmH4dBsZCzRdcpZyhOecJ02/CnRBTqhhqwXXVzwuSUN1UuP?= =?utf-8?q?U/c9K1c4eAVminm4jmO+fmvwfD8cypjgpC8QKPCyEhxR9qHpbCSh5YnAzzjuGc8yH?= =?utf-8?q?t8k1KxEz2IiqG3kd+Um4TiVhQOHEdrUCs6fRO7xhqZ4FcQp5OsQS8N+HGArPOm4+9?= =?utf-8?q?J+fEk760blVYyqfy+I1qaOGvKAD3+9ZSmhZ8YZAmQInDqMYSaIpV39C+U8usenvQq?= =?utf-8?q?blrz6gpc9CsKePvnAT/rh6AvrufVCTqthFHviwNwGKfriQZpoNLsBG5nJVIvjxzC9?= =?utf-8?q?oSiyV3mw68wVtaEXgrORUCISlV0NAFyGoAY2VuIxRy301rX9CGdMvvM97mt/vZrJZ?= =?utf-8?q?8cbP8KZP2124ZxeUGa50GLWo23OlhtN/Zo0Zd4f0XXN9k8lIInpECf2WeDACPCjHo?= =?utf-8?q?/1tk3EMDTAvO0b7TdSk5LVktFV0LbWDaQjf1suJ1Y7DvYgnSY/OWmhYP+I/mYIDRf?= =?utf-8?q?TMa5DI+lsa3z8DyTdd/nSCI1GQ8hdtfEsRvqSkx481H5oCWUSBO62L5DEGmgyrKdc?= =?utf-8?q?5C6VKkw+Vm89g7c8jWc6mXxG8dJoExx1nN1I8ET3mJf1qyfZwkzNIJH5GgWqlx95p?= =?utf-8?q?vBmqzaik5sDmSK5Agms8Gf0toO7/g5Ac3HGa9k73jpSFoTLoxf9BlQVIpG3LsXgmw?= =?utf-8?q?cqz7AMlyM0kHkFxLCVP2fHCUMaV1lH7zpdLtnHW62grgLF/my3D4zHQd30jbx1g2N?= =?utf-8?q?bFFdwmpppwIeGIhe81OPPbPN7Tpxz+v38jWrb2QE4eUMuelk+hPQj2YXEy3PsZDcf?= =?utf-8?q?pmVv0JHp9aLfQHy2DEquOWyhtJveERybZWPiI50yGYlROLHuQTRKAztOYdkF7A2ZK?= =?utf-8?q?3slvCLlRh/cR9qdI47KrSCLekuO2fQX9XSEK+zW/blWRftNZrPHElaXU2uAERjXQw?= =?utf-8?q?0ahNpuMite8nGXba+4yc7srJyri7C9pRfBNU5UhKjaw6XlpGHIhkNhhiXlAY/favc?= =?utf-8?q?a4GDLSYbxUld1Dhevgn1NUD2+H7Ed8ekFd0/tFTl9J9HoLF5N9Sv4jHmrE6j2occB?= =?utf-8?q?oVvUk4bH0S0gRIibgLnsbj7vKqBMIfmL1qwDE/+oo6wG6PytYKlxm0sIePHj3tAWI?= =?utf-8?q?++iM0LOEe41gbWyDhWjCpoREr5J1pxMKzfqzAZJaBPRi9TQQAkA6SOa5uDMGlC3b0?= =?utf-8?q?BF0w7f1BOCUY20mxzNr4DvIWJIvugC7XoyaEOQDQYrCcIH9JW0O/MJGHHlx41WQ5V?= =?utf-8?q?koMN8iH7lXDXEdzwUtsPUFF8RgOYwfeLvTwFdmwka/fjfHuJuWYE24p3oMGyF+XH0?= =?utf-8?q?Bbsh1eB7wtnuif3sIguqleMv8NLMhQ0ilCyLkPhDHpppkXiLl6KYuHq56zq4p6424?= =?utf-8?q?1E17Ztu3oxT1W27W+BQwK3gTh+ZA3G8rt2YmG4XCROP6PPTeoOR8bNyaMINK1eKR/?= =?utf-8?q?9euzVZXSzxXQVYcNbLgdVmueW8Z+3Ts/LrUQJsiHUBYeo3BQP074lxpFfUJo33hA5?= =?utf-8?q?yPtiK6S3TG88?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 014e3707-9ed1-4644-0f7e-08dd196ce22a X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 22:49:19.8889 (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: AerULrSAY7tx0TBvFSli0gPM7aisS5C5s9+rwZ9ylpIh+eQxo76oDJtck5CrosmBbcnS1eT+Z/Updfa+EsapSQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB6784 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. Reviewed-by: Marc Zyngier Tested-by: Marc Zyngier Acked-by: Manivannan Sadhasivam Signed-off-by: Frank Li --- Change from v6 to v8 - none Change from v5 to v6 - Add Marc testedby and Reviewed-by tag - Add Mani's acked tag 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 0b29ec6e8e5e2..773ca3cbd3221 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2059,6 +2059,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; @@ -2074,9 +2096,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) @@ -2091,6 +2117,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; + } /** @@ -2274,6 +2306,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 db9b47ce3eefd..bcbef004dd561 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -595,6 +595,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 Tue Dec 10 22:48: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: 13902418 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2077.outbound.protection.outlook.com [40.107.20.77]) (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 BC2BE211289 for ; Tue, 10 Dec 2024 22:49:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.20.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733870971; cv=fail; b=CqpoTN56XKdG1/gOTcQZ3M/HvKOEu/34sB/lFpWyCD+QMlH9AN+tfkXDwRQ7oqyGfM+OImFPpT8P8L3iVUEP1aAq1Q3Y1dla5acTY1tgQUk3PHf/BZkF3bkIFQLpupaP8F+ZAkdtbqEznGaGaw4L9hl2/1Z1g4uJPGUTbZpmHVY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733870971; c=relaxed/simple; bh=oP5DdB3yQAs0Hi/+MK6Xu7LeBmLWMIOx7WxcBiJ3W20=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=JT4EeHjrgBkV72fb8Zxr/DeYEgzlWteMPJhRbcFx2XAJ9L0qdPPPP/9P3Z0LgwkSjTWKwiCV1EtHKkgqVt0CkqLZvlCSg2lV/U1JmN9FWsDvITnJDdABLTOz7YthTgEVmHn3GH4cy9IQjy+WvP2njWLurHt2kQMlitl69ebtLPQ= 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=ijzIVV4N; arc=fail smtp.client-ip=40.107.20.77 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="ijzIVV4N" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wkFXqKuQS9XMQo0AF04lCXRveEcyLX+YGafKZ2O56MkGyBwk0vXK6WiFuI0v8J+jApW+S+JErRLWSJYnf2PrM4YBCioN9UV0aFo+2UMhPOgJqRl/yFzxswK6vYcTaEXq17ib7vtv85weJYZMOAbJewaduScVMKnweCquWdlPk4hci6nm8R54iE22zad6QZ19CxUjct1DmlS5QFrif+0yq3JvK/rVXUxrrnMmflWzk8Y8hwh7kxkqVZmZzsfANWiIhqX3Ce1p03krjmdLPtmpgURQiFKfndMoDUym+nrxl8BSY0TgSqLnTVX12G/SWQnKcx8AbDBbPfnBz0h5doPu7g== 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=XREZuEFyeTssP2HW1zZetavLYUnAQmEZryNOkuxfpb8=; b=OtccVUFHkEwIIrtrRcCNddFCzvUy6zoWap8sqrdi1ncNoWRum1HeYnE68pz+VAIYw9HQ5+I4g8YLrkBt1L6ztzOgzWesgfDvs7W+MbcXxZEDxtDnwocV4hBd1psusrU89Qr+TS1D3wNr9X5RT1KdLqURSanWmk8AdWZUlEFhhJTW3SjVuqSIFSe6TdWp8qVlTTuSSuAh52DuuDXKxPIlnse8/61xRhu9n+C10yJHiQrqaKkJ8te/9ALJqI0KeM66kzz4lk59qPQtUjlwlR21UjADtgNcFJobQqrs/M8nWi/zoSlostU70uENx/goUn4yaaRbiuKxjwv7DZ+LY2zxwQ== 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=XREZuEFyeTssP2HW1zZetavLYUnAQmEZryNOkuxfpb8=; b=ijzIVV4NgxvKacNwxCVKi/9uqX6hZD3l53AW5hVBa/vw9MUfIhgtHVLRIJe9p8QRSVkghRCt9p6I/hin7U4nTN9HUwgUAVclVQTpUIH5lkIuwmMzX1f+3tnIvJ6lB1t7MwpEYSchCeMS88fZVOppqAfJbKc/ED//PbfSfEuhsK7QL0iWiG7IrfCgu+TfrakxVEAHHSbedH1fvm3cD7UxzV31Bn3ilywuPfVP22aZeTfCaNdxtqHvI7VzgGhgTl4qz6ojzFT6+p6l5/yl5Y7wOhVIS3xPcnYhO4JUKwsDHwBifOhZJzUbI0D6pEEBhz4sI8+O7yZK9PNWBjbiPk+CDQ== 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 VI1PR04MB6784.eurprd04.prod.outlook.com (2603:10a6:803:13e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 22:49: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%5]) with mapi id 15.20.8230.010; Tue, 10 Dec 2024 22:49:25 +0000 From: Frank Li Date: Tue, 10 Dec 2024 17:48:59 -0500 Subject: [PATCH v8 2/2] PCI: imx6: Add IOMMU and ITS MSI support for i.MX95 Message-Id: <20241210-imx95_lut-v8-2-2e730b2e5fde@nxp.com> References: <20241210-imx95_lut-v8-0-2e730b2e5fde@nxp.com> In-Reply-To: <20241210-imx95_lut-v8-0-2e730b2e5fde@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=1733870948; l=9334; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=oP5DdB3yQAs0Hi/+MK6Xu7LeBmLWMIOx7WxcBiJ3W20=; b=BaD46AC8ONfzMIhJjsmXkrTYQA+WUdUPz65eXylBMxPZuMSUygZXzCz+oIiiydFuv8tlr219/ F2hESt2VqivC7CQX247XT04G8Jf4/P89maRqUZQ4Mc0rnxkXbmdVkRz X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR05CA0020.namprd05.prod.outlook.com (2603:10b6:a03:c0::33) 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_|VI1PR04MB6784:EE_ X-MS-Office365-Filtering-Correlation-Id: b6f20d62-c18a-46f0-9cbd-08dd196ce5b8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024|7416014|52116014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?PRaRFb9nrnE6NgxIPvMMWkBZgVrNU2X?= =?utf-8?q?dwflf2CxymtKvqzAD4BL0oay0Z1M29m8iDdwYuqpn9fo/QLDzWTgKQbW70DWb+hoO?= =?utf-8?q?JigRlX4pFiHod/7RgXELQBJoMsLPBwz1BwsJr5PHLsVEieytN1AThQDT+7nWPCkRj?= =?utf-8?q?302R3nMmaIx17pWPa7KS4sstwhPpEBW46J0krZNPpkST+k8jFhZG55vAXbpmkEmg8?= =?utf-8?q?KzuMzh2OhVcBVvKP/KA+0cJDvxDevlwwtkMjhSC5AgMLr5TR40HOIDd7D1HrgexSz?= =?utf-8?q?HZoK8nk4U46C7p5EmZvq7lTNehelG+TuRSadgVE/amB7iJJU1Lll39WDz/kUjvHGV?= =?utf-8?q?wwQEy7fKrmvHAFM+T/JK4KD0UUtHQeWgq0aD0xbyHMaRzv5I2K3uIcxQbAfCUeZ6A?= =?utf-8?q?ndXXw7twOXClrafT5tgnnVt8VzL62qiac51YhpUxEP16Ta88cVgq8z2KdhhQCJAYk?= =?utf-8?q?ajKj33aCAd4YCBFgEroxF4d1AJXuebHVyVTPD70JcVnWSAhWKvjVpJuj7zjKErg7d?= =?utf-8?q?NlKHsLyuqM1qMRs0TvMhCGefCCTlM1/e4a30EZEfCtUOGnkbc735yR+B5m4PaBo4y?= =?utf-8?q?ogS6lRquYCtqQR2MuOwzjpLPiB8Ml87VxPbR1x9FqGYpVSux4NlAkgWRSmBDeX8dN?= =?utf-8?q?vQEf9xg5aeC6wsxNSDd9l0WTbWu6unvwa67r5mzkmJLdPFiy1CVTkOzGLzh36WWRJ?= =?utf-8?q?4HHHtFRD+TgBzQ56vurGg5l22xemMnvtHMOEdBaXiVHUWDIXQrKlvG/xQ36fmI/FI?= =?utf-8?q?wOtCZcmukX+/Qd6XeYoWr9g0K0c6UQRg6IqKt76YrudhB+jru6Zx4lZmMnZxRS6wb?= =?utf-8?q?pAiiKrbrzEPcYaXzGAA/nEQ5BztcJoyvp2mgfPyEPf6svjTKVnLeSrejCxeSdkHEO?= =?utf-8?q?LJvTxzilFg7Xxi26PFg10ZrGkrI3brhrc5nHr5YryA2okadjXWnQUMuXl/YQq5VaU?= =?utf-8?q?WRVGUtHGGCSzqOHTvUuIq0J0Uv55KlzBMGqHw7BAyAHeyN8peJrJ5tGPFDZSFqs5B?= =?utf-8?q?bLutsKTrr7gBMjwmSpuLGl8+yd/bb9xGKzqwNFabACN/aPEz5ZywoApZgGSN21dLX?= =?utf-8?q?4USBjMfStFYk8G8qKWT22iRznwkc2DX5QFXtvyXGELRkb5o/v66RUUC2Pmgu/7gy3?= =?utf-8?q?7Lcd9LyIihKypYCCzGXP/YNpxaXk2fKUYRUAFX5dhQkRqH/ko2YVR2HvukkJz3T04?= =?utf-8?q?YFpITJJ+xyBL8ex3fYjJ53rDW/0qQgyaomlshDoX4nN5f76X0w1vu1KpkKpw3sM1B?= =?utf-8?q?UeRvsUzomqqHndb5bvea8Kodo/TB5zwB8o+CdDueVI4LnsnXE10BtQR6mQC6IChhx?= =?utf-8?q?rQBrrH7OLkGzNcSw89E573e5q6EofwHk9ASX6hqPSuoOrEFQOYkQnRg=3D?= 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)(366016)(376014)(1800799024)(7416014)(52116014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?cO1d7/O0eEFyBb2t7id0zhh9VV4U?= =?utf-8?q?2rmcnma2swwwh6WJfgS1GJFaa+/TSesLP7ukDkyu8P2oN7ErX4JM0QxAbUWgHfntC?= =?utf-8?q?zY5L+/rk1bqaQBJgMuUTLwbR/OngO/TMzm+M3y43ecI2zECVGJmWLRdHWF/2oXSCY?= =?utf-8?q?lEh1imElxIBdxbz5BE26yP7vxti80/+1UwwR8yDu7VmmBPz3ipR9qe54QG7z2ofa8?= =?utf-8?q?E+0Ya0BkTv7oSGz25KYeUQPa4eq9qWvjczGZPmyJmUSnbM3/8nVTGqfXCY2pyluGd?= =?utf-8?q?R9R7Jrsd21TwVC6QbyJ1iypKlOS/n8bBcb2+uANlSll06OeiiiSN91Kt7VFUOsHKs?= =?utf-8?q?3MWivqD7GcKEo50f7wv2oh0gETbqEEU308zwABivjMI1tLBOheKni99BcNpv6efEN?= =?utf-8?q?g50MZZ15a+rh/Qbd0rLboMwp5Q+J6+cntg1zlt4TzmurLA2nw7iXRle2ZraXq8P7d?= =?utf-8?q?UyErR0nUKS5gRddqBPi6I+BDhoCJ9/jEhlg4MlK+iemv8BFNNveTdDSM2KP4JEgDX?= =?utf-8?q?UGrYBvxj53VFBxoTjIcZSuHiywL83+efRRMQsoEWrMMzuFgTRROOkgbF+BvuunrTG?= =?utf-8?q?vGK1BHhTm91xbdOLs82z4fr6OAKuqGI6eYF59ZaL6gNa/WRCINuL4W12dxQETp/iu?= =?utf-8?q?SE2Gf/FD5d8EwTkaEeSwm3pNJ6X/rEPj9rsNqmRJzKoka7vSplbQdj6vzG+0SxDzv?= =?utf-8?q?TeiBVgwOaGCyKlfwlDHKAWsniAKkZR5/XDVdqmCmBM14Fdgs7Vk5xZqoe2Y9kISR6?= =?utf-8?q?PhOV0neqaj+6DAd4Sr0+376KALZ1dfsKKyNhNqhQ/rpAo8WGJ0638C1/RlxPqnTZC?= =?utf-8?q?bp5Shrdw2I1W1/twvYxUpFXmWwHimfdvlZ6y9oNkqaFtSosmr3hpwZPXYFY0FuD5y?= =?utf-8?q?NvsMGLypRgB2iIAItH7vQDt6+Qf62I3pZRUkA48s7JB/dG/YE1wmx0CkGUgeUEUrA?= =?utf-8?q?QR88LR22TI5lqV4PDqhXJWfm11ouJERzT/Q/fnCA06tkoKJ5w2zaXwa8qZwSF8yFN?= =?utf-8?q?dBc6JldfgGsk0t6kmSq2PGuU4DD2OQkuR3hcSIG9EvuNorimnNXTA96IBD6pZLcPT?= =?utf-8?q?f4LBcf6XQcozu6J7oE/9avAomHjb6aXUPBxGuivxH62u9ZbDfnl14TVPa2voGkwo8?= =?utf-8?q?zOZxfx4ZS5lZFFq3WSQDn7RBfuXXJgFKyE6tWWKgmOmOcffhNQnw85QrlPqdY9U47?= =?utf-8?q?p/PUqetF4zDnI2J6YtScbtU6ixo0Q5t97NcpUtxC5pCGnxCcrvDr/7Ln+ikMTMsA4?= =?utf-8?q?NrY/0TsPHEjin5Rtoy+xw0Bze2jBSdIxttNmaY9fLi84pJ2tRHWYt2F9E9dnjGHak?= =?utf-8?q?wlAg9SlfvMYTtHUbfIBtKsTSP1+21dwpQknAFRhN54oRMAtZOuJoUD4oo8rZynEDP?= =?utf-8?q?VVVuK7/oePHcsqoSS65NAOCtw8wsgrNzaCJCAfQaRP2oXkYl3IKKsHtKr6FtKq5+b?= =?utf-8?q?c3niSpdVmQjIskNq+eGY5IgLg2SCJ0uyfSvr/GvRDFWxbyRf1QVH0AyjzQkon6j3S?= =?utf-8?q?BzxIMAxtmkfl?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: b6f20d62-c18a-46f0-9cbd-08dd196ce5b8 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 22:49:25.8744 (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: XhVq3J7XOX2R9pYQY0JqmGUyuo09f/No3o+gVXhNF1LnaFFYXOkedLeSRq+QpgQbOfDJwuOKFwbQJIVFuheTKA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB6784 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 checking msi-map and iommu-map device tree properties 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 v7 to v8 - update comment message according to Lorenzo Pieralisi's suggestion. - rework err target table - improve err==0 && target ==NULL description, use 1:1 map RID to stream ID. - invalidate case -> unexisted case, never happen - sid_i will not do mask, add comments said only MSI glue layer add controller id. - rework iommu map and msi map return value check logic according to Lorenzo Pieralisi's suggestion Change from v5 to v7 - change comment rid to RID - some mini change according to mani's feedback 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 | 186 +++++++++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c index c8d5c90aa4d45..d325f4fb279c8 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 { @@ -87,6 +103,7 @@ enum imx_pcie_variants { * workaround suspend resume on some devices which are affected by this errata. */ #define IMX_PCIE_FLAG_BROKEN_SUSPEND BIT(9) +#define IMX_PCIE_FLAG_HAS_LUT BIT(10) #define imx_check_flag(pci, val) (pci->drvdata->flags & val) @@ -139,6 +156,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 */ @@ -930,6 +950,162 @@ 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); + + /* + * Iterate through all LUT entries to check for duplicate RID and + * identify the first available entry. Configure this available entry + * immediately after verification to avoid rescanning it. + */ + 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); + + /* Do not add duplicate RID */ + if (rid == FIELD_GET(IMX95_PE0_LUT_REQID, data2)) { + dev_warn(dev, "Existing LUT entry available for RID (%d)", rid); + return 0; + } + } + + if (free < 0) { + dev_err(dev, "LUT entry is not available\n"); + return -ENOSPC; + } + + 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, sid_m, rid = pci_dev_id(pdev); + struct device_node *target; + struct device *dev; + int err_i, err_m; + u32 sid; + + 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 { + /* + * "target == NULL && err_i == 0" means use 1:1 map RID to + * stream ID. Hardware can't support this because stream ID + * only 5bits + */ + err_i = -EINVAL; + } + + target = NULL; + err_m = of_map_id(dev->of_node, rid, "msi-map", "msi-map-mask", &target, &sid_m); + + /* + * err_m target + * 0 NULL Use 1:1 map RID to stream ID, + * Current hardware can't support it, + * So return -EINVAL. + * != 0 NULL msi-map not exist, use built-in MSI. + * 0 != NULL Get correct streamID from RID. + * != 0 != NULL Unexisted case, never happen. + */ + if (!err_m && !target) + return -EINVAL; + else if (target) + of_node_put(target); /* Find stream ID map entry for RID in msi-map */ + + /* + * msi-map iommu-map + * N N DWC MSI Ctrl + * Y Y ITS + SMMU, require the same sid + * Y N ITS + * N Y DWC MSI Ctrl + SMMU + */ + if (err_i && err_m) + return 0; + + if (!err_i && !err_m) { + /* + * MSI glue layer auto add 2 bits controller ID ahead of stream + * ID, so mask this 2bits to get stream ID. + * But IOMMU glue layer doesn't do that. + */ + if (sid_i != (sid_m & IMX95_SID_MASK)) { + dev_err(dev, "iommu-map and msi-map entries mismatch!\n"); + return -EINVAL; + } + } + + sid = sid_i; + if (!err_m) + sid = sid_m & IMX95_SID_MASK; + + return imx_pcie_add_lut(imx_pcie, rid, sid); +} + +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); @@ -946,6 +1122,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) @@ -1330,6 +1511,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) { @@ -1627,7 +1810,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,