From patchwork Tue Dec 3 23:27:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13893076 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2065.outbound.protection.outlook.com [40.107.249.65]) (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 214D620ADD3; Tue, 3 Dec 2024 23:27:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.249.65 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733268462; cv=fail; b=QcOe/SlHLGup2711M+t/I2uBggCnqAhM3cJej5GkYk59qx5lp5Jw9Wlb3NfT0UCQER39euW8Bt3H/Mgg5CwTvMxDgDXlMckHEYdvMm1lR0cgsUJJwFBP9WE63jHirkYl3bX7OHAfslvZY+5/ZPE9XGMZgBdcl9clNaFeEj0Wgzs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733268462; c=relaxed/simple; bh=7cB7g1Uh8JOotIqznQjVLlaCi6bPwnExy88Hl3IJhNU=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=d78KZMfwXfVlGy0XWf6e/jh+w3m/7dK5x7SUgNoPzLgzh4PD8D+HT9T5ePaMhtQEjpC7cidn3QI2X6oms6S0UzUqD84U8MwPe6+2TlV0KU7JWU5lfmD68qd+Axtec1v98fCMQATxZzshFaeq3NhF4Tb7C+4LDPg1Peg82v+Mo64= 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=nxPFKg4y; arc=fail smtp.client-ip=40.107.249.65 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="nxPFKg4y" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ia4NsE56WuWkjr8uh8lxepfvpRh1S21Zo8M0cQsXCc9oGodJIZ3MD+g4EEBzT7zxtkaGDhZu+HohadHpsqGismREKmeO70lVMDybuw7wB8ZZP3bHaYi7uAK5i5sjHYm/fisnoQvJbumfjo7m/G88K6WuQPvj8tgDKFUhSUD9k7w5BOuHWIoHFBcdoYtHEQ3UkrGpfj+6l5X7LIj5kwHpq7G7jWjWhBZDgZpQCaF8VkfMiTAFxWiMaTgnd3UH5SqULFa/wwvil2+mNYFLUMzPKUrba4yzfDkkr9OygWPY4Tej5x1WEGElAz31jOsVNbkOvBsAhQ4OEWGiDUjr3spvZA== 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=sw/R9FZZp1VJOqnI4EGnpxzsdk8zWFQNSU+rOqngUXE=; b=YIwHjjvsg/lCMouGR7/vNoGajYFk/7WFjzR92+J+BCRmPKR1F6w6Zp0QOwhJcsdymyVRbc+NXruy5C+f6m5nHSF0yBhOoORz2hVqNnRYmS84govh4UhLCs3NeWxg8X2EnP/08Wy+qtsWi9N5EL2anjz5w93Nc0z8+XUig+PY3oKOPGMSApUiHGr3aVeXdum9dhF5Vb2dK6YiWbVWFjvaiAMjidV6j9JUhB1i2PvntCzRU19qe/iFy63kPRTbyBgDOT+6LzJQxqSndiYcWXiFoB43myh0I9MtJAOHfxFTMUJ17gnrEGiKxw3PSzqmQYldH3enVJFoaWeuQS0XVZ1BzA== 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=sw/R9FZZp1VJOqnI4EGnpxzsdk8zWFQNSU+rOqngUXE=; b=nxPFKg4yEyWDQxtZ++kZTw4YYxhvYvZrU/egw69hh2+mqxUbS1RbGqzcOTfGXvgoz6sgs6zutcO1mwiiaryIYR7q50KLAzah1661ygg/5QIUYKVYpcCyVRVynW9S8ruXoCVQIMEk1DG8tWt3fBMbyQyJBDbVDVtmN4hXNeElpMSi8hzfZJmtvPY5WsOloit1qI09QBzE4YNqsRtBdaBGydVrJOTBHyMU6I8nHweJ2EVj/aVj+bj4DT6fMmHwpffTo9C3JRp1D/y0E/9I3VasLJNEZMssC7+uBlGNQvM+K5F+EhmbzRncCd6pHoDaJft3yKUnBhAAnQOiLurGs41PYQ== 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 PAXPR04MB8846.eurprd04.prod.outlook.com (2603:10a6:102:20d::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.19; Tue, 3 Dec 2024 23:27:37 +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.8207.017; Tue, 3 Dec 2024 23:27:37 +0000 From: Frank Li Date: Tue, 03 Dec 2024 18:27:15 -0500 Subject: [PATCH v7 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241203-imx95_lut-v7-1-d0cd6293225e@nxp.com> References: <20241203-imx95_lut-v7-0-d0cd6293225e@nxp.com> In-Reply-To: <20241203-imx95_lut-v7-0-d0cd6293225e@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=1733268445; l=4315; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=7cB7g1Uh8JOotIqznQjVLlaCi6bPwnExy88Hl3IJhNU=; b=HYEDrP3HycCh9VaQ+5xea4ku8p7VoaXNt8ymbqLM1S22i/IOHU1vw/3j1k4twoMYU4xIex9bw KJQHtfrXORlCcK0M8LWPV25Tlyh0nJ4xXMM/521OoXSlDOythVDflv9 X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR13CA0128.namprd13.prod.outlook.com (2603:10b6:a03:2c6::13) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|PAXPR04MB8846:EE_ X-MS-Office365-Filtering-Correlation-Id: d97c3db2-27f8-4664-9d79-08dd13f212cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?q?XG5EDDV8+0FMbpwR8WkJZs6yJwRSLkR?= =?utf-8?q?pMzQ6mGaoiVXtiAATjh4G8g63C6eDAMp8mk7HMwaScD6nAzfyL7KgMDItuZvpCTX+?= =?utf-8?q?5tfAgz41wdDYorgYsle4kqg0TMoH7Cl7nEIBuo0B2VDeg0WC2zRMQDeomrvS+M/V/?= =?utf-8?q?PqdIpf0Ikl+1V3h60ysIHmrbWtlUL4trYX4+uyHR0czfytqZlrIUBKvEbx7UCUaod?= =?utf-8?q?PF8FSq8hXsIefcPX0Ht9pHFeUbDX01LNWJd2ZPdBhUr9hm3tLi6ZjUgan9YqOhahD?= =?utf-8?q?fVF41mWeAtGnL3nKW96rhK3JaJm26tCAQicyI5o2k2xuFP8l2y7gEaXi1eJ6ejnY9?= =?utf-8?q?CBl0fXUHQEytwFyjSgvGeZYZlsF3Nz4keCnJh5qDRFjXIeXFS5+oMIB4NAUK1CGSI?= =?utf-8?q?jIfBctVOsKsikXkik+JAKhkNhFrjOhuy60ChFJ9KFpwo6bUZBhiMZQtwecc1bx3g0?= =?utf-8?q?+ZQb8Axh3eAq0fDYUsioaeE6OWm0EfsupaGjBbEuUbjuQ9180oaWNIL9KPQD3+mr/?= =?utf-8?q?C1lzkERnrALpXEv/UI+mvfIMMGfO7l0T1KmkOInbr3gTJ7yyUe0ALJ+9zaOQALTS5?= =?utf-8?q?UBRid7SeTLzXtYJBqsl/CrsYYZbHR+wimphFANsbgVGDLZTy9DP7nYy7rmPH2gpUv?= =?utf-8?q?LHNvDwISoE3wcAk3JeLU5E91f4/bVtbIMlna0SkKzcD2BhwQDgdOr2F+yxLG7f6/A?= =?utf-8?q?bHITf0rShJ4lPo7HjcLEqs+aj3u+HCcpg05g4EfrkTKSGXRxcScKs6gw7lSY1lgle?= =?utf-8?q?N9GAYlXHkXrDQrMmg39Rlcm9/4EyIOMZhqp426Qcr9FbQ1uOsW/43SEpmDyupDMtX?= =?utf-8?q?ykYXqaG3Qjjn0JzGHMz1Jqd2ld2+QNKR6AkqhvFkiTs8QCAYsX4kW98mx8xcDr8dI?= =?utf-8?q?q0BNwL+hXb8ldXz6LEqN8vstGS2trDTj3J9tld3Wq0fd2ki8f3mmSgdihFwwa1AVV?= =?utf-8?q?ACMRINGALV90feB4wfoFIzRZuXyXIq/mdP8yK2mZvjDq2aE5RET9+rOR1laYIWC26?= =?utf-8?q?BbPXg+A528oYDg79PfBA7bY6f/t2BL1+/mTy9B5iAly71VBogGP/X2hGOEsmOOyA+?= =?utf-8?q?o1dTxymnZsjvWnWh/9RsucMcfM8H+vENHezsvkwjo1Qy2eRBtY5BMs4XDgoOJe8/x?= =?utf-8?q?Y1F+twhU3vyZheN9GayrI4PaYQ/7czs78aAVyj9Z73/XBFAys6zlCrVpDKghwDZ0U?= =?utf-8?q?4gue813vS5uPr4uPahK227haIybEM4+/pw83hjulDr04R/kjbK8oYUc3PyS8G/sA+?= =?utf-8?q?rX6pxoJ6KK/IgxW2SjWf1yxaSqzDw3Wm4Zu1iyVOComiFkbr3y2uMgCfWiilD96Jp?= =?utf-8?q?wBzIBP2JoOjPwgjwgT6xDv9xlzJZIdG6gokP3JpEJL/9ad0Er1hoZB8=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)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?CCVQ7JJY1W9AE8OTPfVTm/vxrVnt?= =?utf-8?q?JLBdC24zexkj0RbPFkY1zi3XJBsrK08ZSYMnmBw1QLVp5sZ+DPCasAKLCAK5Vs4O8?= =?utf-8?q?1dd5iQ18CLWxUiEuFpXfis+f9Fr4Qvl4rkYnUnj7vuu/EKILedYwN5AiV9WqkNAdc?= =?utf-8?q?7Vh1ytw51BRodHsekuyZzO2rdh5oe8bmvr4had9sRYf//2w3FymOzKhY4XVIIjNmp?= =?utf-8?q?C3pGRyIT8LVpfK33fsW9WX0ZM+UmIKh7bFHZ9HDI5hZbNciuIoTiUOLhTv/kDxnvb?= =?utf-8?q?cEepVg2Kw593V0E57/QX/Sj/8gSPtfaxIHi/wwZGEqBRMjtI+v2GQfZrQ8DQP/cHf?= =?utf-8?q?guwXspQ0wuH8KrByXv0/zKRVGMpaxeQA86SOWeGbfC3Wwp5krTMZYI1uzDqks8kwr?= =?utf-8?q?3SRCXXn8bi2fVU9Gz8uUlTG1pYr6cQ+/eK7vSf1WfaQwBrxR9lLvK7SJg25qv765A?= =?utf-8?q?1Wlmny338gFNSny6ngP/uIAa+3yHadT10K6SzwbXXC0ocBVs4kLWsLlHSbYjaqaK4?= =?utf-8?q?anMqs+Jgh+SyqJPFPbEwGu2aCj7BGXARQU3im8Qpz43WK+ZpRBt8t2aipdN1kNm8q?= =?utf-8?q?UmlmzvNQpzwZ0lVyUWAVq18dKZYmNyjFSjExJ8ZurETmGNz71hPTnC9rPDJfe+YMw?= =?utf-8?q?c6+AOeVeJyJT68IaKEWK48du+pixEKASpdIDxEE3MFbjafSHy2flGGKVKCIq90F+0?= =?utf-8?q?wmSZBo++79taq+h5JH4DLL1F2b2bnRrxtKKu9LWstEo+ocmFh49Hvj99kucN7TYV0?= =?utf-8?q?A1V53xnp88Pe+ECyFybIvRz7i99dW6+GZpiwwU+t4TvbsAhhSzxQ1VI9OGNUNLtHy?= =?utf-8?q?YSs3KMKKUskXPVvSlv52wzimRpJNtboZZk7GHqlpvSgX4mjKYTubQahcH9WD6XnJr?= =?utf-8?q?Xrsh+N/IGEG8Uspz8yB71lnl2BpP4KB+TZFhuhosU1RvVVqE3ZfrDgW8eXqcbuuCI?= =?utf-8?q?kbH71Vbs7ojTPpdfhBFY6T7ItQlvYqkKs+6IUb0pkvglcIFDfnhtEO5TvgFw2FYHL?= =?utf-8?q?HRzqYdS8b7o52dqqlSS8ZYCxdXfrH0fS+3JuXOgDvUcgpwdNgWmZHPd1LH9Dosrm+?= =?utf-8?q?qX2xn75TZlsPcAifoUAqLbV+tb5FCSPApEiYS6PCLNPX7P0LOKrmjxZ175M0T1+AV?= =?utf-8?q?OyqYoY1Ie5/Z5VaOxmxQsH6wV0ZstfnRZBT0dWEF5JWG5puaZmOyEU2Douajb5iWL?= =?utf-8?q?KOxwpTfOyvh0W1YpnZtLLS2a8FSBBGXevTYsNBGqYZZB/22GJM2E+0uKxfA76zRg5?= =?utf-8?q?lGRlcBx8SSDksKc1Esf9T8dvInUpEZATTz3tBoyJysDb2qXRemUonv4xMNyXZ5qfI?= =?utf-8?q?pUP3aTBob+hgiI1VDLIBQFJvmqF4wNQKJZ4grfbMKuB37DZugNru6/89U46z55Hic?= =?utf-8?q?usXBWbBCEcTiptKpgqgD0Ssiewz7YKLEcvk3IhBEh30tUblC5h6b711l402kv+vdD?= =?utf-8?q?/7IOSgiBb7HlpddaqL53cZYBMQAm/CHLqnoiadx2hcIxDAR8HMPGozRHBQfq2rWwz?= =?utf-8?q?mQNe69yDe/om?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: d97c3db2-27f8-4664-9d79-08dd13f212cd X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 23:27:37.6116 (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: iPvjD5g0D6NQxdqi3fme37RGDPMwuL9u2yUtj+aVySPAeKYVnjOpAwA83StO6uufIkinRs7f32+D9n08mSrOPA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8846 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 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 3 23:27:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13893077 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2054.outbound.protection.outlook.com [40.107.104.54]) (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 66A6220A5EB; Tue, 3 Dec 2024 23:27:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.104.54 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733268468; cv=fail; b=YgbLB23eJxZrU3iP3gVlmidKCrP/uS7zzWl1oeJ/c4h6lAnNlmQx53mnRB+WsULbU7rGykhvUQUTxjJCF2G6Q+4ZYEvkdzmxym7HWN/quJqPjILqS2pabA7o/RoIrB9PTw1Hd6fJIufc61ZdqSCH4KfAsLQgZmYDimAyvV6KfGo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733268468; c=relaxed/simple; bh=rXhNqwdgirRPqijx7i4RJfbC7AudsiBL7xFaxLL5LAQ=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=oa/tRX0GquerPNR/NZqQg3x379hqbp6A2JclvcZ9wB4qFoD5lTLNcbFcCdwQ6r/8IAxN6s6m6qx9LlsFt0gYT48jIGP50J4CIPk1I7weKMak69ld3GRWiT4oIhCNpk1I7gzUEJwW+RqcOh4Tq611PMxZtakKZUUdKzR2StNNGHw= 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=b+4iR4lS; arc=fail smtp.client-ip=40.107.104.54 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="b+4iR4lS" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gtoM2NtgZM4G1Z3G38yOE9LHI1Ou1swzvX0Lb7/GHovEpefVyDBbpB3Ww4yhCSuD5fO2cv9F/oUWsZ2lR8kc1u6PLC56N8YhqaxAdOgVfsDx8L8b+AkV5NqavUsOL4tkRCRo8Rik5vKsi3RuevggzZbG7Kd3hhH6wgqpkoc6zFXVqMX+EhxvqYxqLavyRAyfrV7W2NJrEONauzzNyUM3kIJOa8h+Itt8/SUkFO7u0EXeM4ChridPBBsyQV9Up0ELoYt7Xoe9ObJOHXOorQHsrqquOy/j04Knh3T0BEGjMYLfbXQBqTlQDqLzpw/PBHyzrLEU7BbjTs98ufxI/ZU//Q== 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=3mpFJRyM8rsbkTuLLftvVbBxxmv+3Sp+Y18ElK0+EmI=; b=OVanvwZUau1ltpMy2APnX1x+3Kb+SeJMxWENXjTrpIJFw116kJH03CuiGlW9w8cuA5mZ1JZ33WhHtHKDlZtAbW8q0sc1GPGf5crss/tUwqVfBKlDTqEl2GI/XG40aM316mKmF1ug0AK6Iq2aBV/zBJFK075+KDX1e2j4DdTNQIep2tM/qjInqvEkC4VtNtJk/rDl+5hrtjtDPO96EFcNsL08QqjHNO4CWIkr8IdH33J+f5p+3ci+IRuY5XUgqcc8lhMKwkNPD6VzB/FEd9b/WlgWc959zFL3xqxoT4dD/SXnUzxya5Pks7dvT3A/WYwquc/Wae7OvAsVZWVN5X7Y1Q== 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=3mpFJRyM8rsbkTuLLftvVbBxxmv+3Sp+Y18ElK0+EmI=; b=b+4iR4lStLa4E2RqUVluTbxZKEFMbazh+j+KKogtaXwJ9PmPBwop1BnRJv4hDJVEPMuawVUQKtsN47iWpvwbh46gEEqVYdUbqwqr02KQBTy7o+RDM9obU1k8NkcAu5mD8cUt8LzmSxPqWL5L+Vhgtn7gUzwtZFLH2wpbZd2DBFpzx+fju08x0LPk4K6hpJ4iozqeczkFxgeE0arkQ6or4swlDIK41nh520Cmv870wJjaWFg5sU5BMt0AnPVDz7g7puA0evyOAOTHEPbUeWuIXC1AuU5E7Zshh+QKWOjTIVbjHQkWzj3CGvvs4jyNxun3P4GFw7FqkCa5c8cfdAeSaw== 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 PAXPR04MB8846.eurprd04.prod.outlook.com (2603:10a6:102:20d::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.19; Tue, 3 Dec 2024 23:27:43 +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.8207.017; Tue, 3 Dec 2024 23:27:43 +0000 From: Frank Li Date: Tue, 03 Dec 2024 18:27:16 -0500 Subject: [PATCH v7 2/2] PCI: imx6: Add IOMMU and ITS MSI support for i.MX95 Message-Id: <20241203-imx95_lut-v7-2-d0cd6293225e@nxp.com> References: <20241203-imx95_lut-v7-0-d0cd6293225e@nxp.com> In-Reply-To: <20241203-imx95_lut-v7-0-d0cd6293225e@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=1733268445; l=8868; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=rXhNqwdgirRPqijx7i4RJfbC7AudsiBL7xFaxLL5LAQ=; b=JfU1yYrnBR0XrgK2ehuoXBbhcSAxNnSo7Ft6lKLFdaQNK4p2BIKke2wYBSV0pYPs8euZxITF0 RUzC3EBiUsgBAv+rGhp5EBV+8rsD3zZczxB/JIRxm2Rb9vPxIhuDhFj X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR13CA0128.namprd13.prod.outlook.com (2603:10b6:a03:2c6::13) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|PAXPR04MB8846:EE_ X-MS-Office365-Filtering-Correlation-Id: 10ae0996-8a30-4c76-68e8-08dd13f2167d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?q?5HwSTavs/7yhPfmzbYFlMVLkJ2+74j8?= =?utf-8?q?nxHJi0xcQDcA8w/k6eTXzpX3nflizL0dt8u6ugk+aBzfbTWcI9NfG6t7Vu/fMs0bz?= =?utf-8?q?PEf/nBCPlCfVVIKTKH+ymdfSbdLvK0yCsy86Exj3a6yX4o//0ZTxmnr3uGL1t2iCJ?= =?utf-8?q?iX9EyaeumDnmZdYb69uNeNWRpVo85F9cmj7MhFn9Oh/IECtw0jLr0LcqkUEeCi84R?= =?utf-8?q?Z7Xj5JHqvX/6VP1/daobswxwD4KCjsl2XWBiFS1/cR8b+ADGaz5OtzIdDScLsEU+C?= =?utf-8?q?3ZEnz8ofohs6jbpERScRz0Og62NCyrxXCuFWC9Z7CBH4Vu/kspx/82L+T0B53ahaA?= =?utf-8?q?a32Rc580r1L/QyPyhDn+ELI6/KYqDwqF1yllgNiPPQKwfgfUk030VlfGAfVRe3yvM?= =?utf-8?q?wkpmUntSYBw3dOl/BIs0ZkwRHcPZecp2YeGDhNQ/bIb+NFz+6j1JeT2cCFpYMVtYP?= =?utf-8?q?J8U07BS9Q9KhW1UWLY9Fy+PGoSNl4HrqLCMeTO/MMlrxXBGit61sB884kCof27W8F?= =?utf-8?q?lfbdOyKRUV5165yMWf6TuHF3zgetob6kEjDKaR2TIc2uNPQf7vhQz6h9gQuLtRERz?= =?utf-8?q?obdkzDLZO5IE38gQQpLiDX8xurCkGZSlA8hO2vtVngelBgFQCY4ayTXvH3c3qIeaA?= =?utf-8?q?9M/LE4xU18YG2pHpSUMxCKUqrZYDpgD0l6sJ614UZ3u1deDjeQjg3ABXkzSdZH+Nf?= =?utf-8?q?gcXaE92VW32DhPqLb84wM7xIU42cIiMEGdr2yyNtvIZT4R+nDaLtWYd9QVESjJF66?= =?utf-8?q?UTXswdO78V1A1BZSmM07yKz25LApR4wP01DjkhBncPEj80C4BvcAB8BgYNvBRFbyT?= =?utf-8?q?UcxlIgQq8M9D1FQ5qvILIFYj/wcxofQYxLydaTSOZiO8LH50kkqFyKmGlfGlTByjP?= =?utf-8?q?jLlHWizqD0vbbaeR3rjdwZvBSL17KxDbaM3hQLRE843imIqvVA38p3qUaVqOkMGL9?= =?utf-8?q?on3UQjo5tN1XXmGtetBVG4tUjOvL6R2CHFzAapBie87Zh+Sxdk3vOQVLYsm+Yu1+f?= =?utf-8?q?oU4J3k4R9Cmz2IwORVmWUOgQH8Kv6fKi1QRHNLxiGr89hLWLulTbLOl1ExP7ToM04?= =?utf-8?q?7LSEk+aTsYu5R6NRxYTv1wbfTCv2OSkd/K0JupRcnAmjyDx6CABwrXD2g/Qcr2oel?= =?utf-8?q?ox6OhwOWMbAzhQ8Dc+UdnDNZL4Xi5sqGpJpP6C7F/8r3nyb/gKNDpUIDGnDW8FJmf?= =?utf-8?q?rM2WMTXX4T4AGCSBvdaCixufSxmrYwwDTiaf3FnGVdxEvOB9GcsWodd9k3HBO+hBv?= =?utf-8?q?zgU2OYjep1YxKcMGr7gQas5y+6Kqi5huYtJKndIkr7vusHI0zFjlvFwDCoDxKINuA?= =?utf-8?q?X6OjrNuF6ZLTHzVeG5nItryzFXYFv4/wEnugIDoD0k5nNw9tnQToPd0=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)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?I/owmzp0t0NQD71TNWNqGpEG7h1K?= =?utf-8?q?r5wOkFnqOpa2XzNcgpTFanfKW4E4iMgGwHZ1JBrOJTvfoj081I8/Np5LUPWWW5GA/?= =?utf-8?q?aeXBje1lyaSYQ45ro3Jf19u3viuDVzaVv93kk+05fuO/00z/PzuJWSyYg2TsSbQjZ?= =?utf-8?q?P5Bkh27AXAYE88kJnnJAL8CDztvdDddd7VGZJOBlDShLpjofbSGGTxgZ96sA9/5DA?= =?utf-8?q?qTqKWSeJBeo98FeDSIB/G0RSkIA6wzaEyI3NOLpqmliw/uZAM6HSzUcALUGBUDdK6?= =?utf-8?q?lr0dbyfu/pPtCWUDDty1pfxjMtZIVAjP9rk+DxVq4mvksEqkyJKzAmhM+2hV3MkEy?= =?utf-8?q?yh9XyXog5TH/PUx5u9+Gb33auVfrSZbuqSXoGq9k+gH98XqiDTyWHc2WEF4tmbPrA?= =?utf-8?q?qJhHLwkmO4R0QhsN2AR0jDBr5fQI0xvbPveKGTh+rQh7pu35lMw3ebK2b2BnCYpWr?= =?utf-8?q?VXGlR1vlURl3nCni7zl6OJSK6Pty2O/rtB/TRsaX2E59uNjzc1akgX/2IQ7TP9aWU?= =?utf-8?q?sKP8yZuXrtAdwJfU2DN0ygtUrFC4t/UUU1x5RB0NKUu8/VmZiVdw4LoX/+n7fWmIV?= =?utf-8?q?cTBUnhw13LMiPxE/SVXPU5DQjtUTEAllDWccB+4Vf4rWMPhA02RTRfm/suAvbCTTN?= =?utf-8?q?86XbWrig/tu2mov/xPMFpHQZFtY6oDNuT4+/mwERw6vppIvekaFRsvMfMOgoF6ygE?= =?utf-8?q?qGkf9wlSuGqBzlCjp2/qm/3sQcQgulB/fSamiAM1rGJEkjGc+zQXiZtyBfd476cUd?= =?utf-8?q?jyL1J7TGZ/T/0MEOoext10vftaD3U2Hb411jKEcehXKH3ZAm/TOn5yW1n5LZoNSdV?= =?utf-8?q?e2NHnvyGBDPFNnDfG6gv24fWKA9ssavQEPfZbIjpypoxTP7INboXTqwtnREb4+Kt6?= =?utf-8?q?hd3AL4DNYdVqJoBS7+xli+srIwsXKn1sRtFRKe9ix0mw/EjYmZQblUpYH9oRSB0IG?= =?utf-8?q?7PfGZRqOKpJioGcpLCR3wpBwkh5WXtO7DdFfW6NuvqFQ9RauEsfloNfNQNI+Junqy?= =?utf-8?q?JtY+JyVB49am1HXrUB5jxO1/3875k92GB4KWXALEj/lgurH9JzPCFOOXc0u2+iNfi?= =?utf-8?q?QPH9awz+YFGlrM9Vecl8/xOmcgakpfwVYA9kHS0/4+QWnchkHKqmEXrl7j9FCdB87?= =?utf-8?q?2x81hqLr02NoAPzTHrLb9rEdjJjzbrwa0yrDjUrUzvd7hgJuRNnEPGYRQivZ41WBK?= =?utf-8?q?Ue8H2FLbOBD/vMUvlgBPbC1rmGovJ6nt+tSJBmlc+B5RC7BG1UUwOdsnHPL8EMtzS?= =?utf-8?q?QhyluqZagt63r3nqoy89j+o7ydtibHY8SUIGsrjRXHQKzBfr6NNpnbvQKDtBK1d6m?= =?utf-8?q?881M7/EP+VJmkS+Be6PznVQuJbQnICTn8iy/DsnwvhW1Wnv+oSr3VcGD1T7ddMHaB?= =?utf-8?q?uhcmPs8eUU/ElIHbbty0wutSg4MobUHI83J9dW4N4bP9di2Y9MnqZL3dFANSwZOEq?= =?utf-8?q?Tpy7Oo8Ytf0eJHxqtJsG9cIJYd1CjVuxdwftMEUN4HL/8GW9QvmY7auFfSdnfo2qa?= =?utf-8?q?K2pQiUWJG8ej?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 10ae0996-8a30-4c76-68e8-08dd13f2167d X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 23:27:43.7667 (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: 7s+Ytw8Gk095fG6GnRk6ClkZYOdxVQ+33ubRhmyYQ2nTljY/o+ckVTXl3kVGOVRXD6ok7Npa4sZDDg+dN+0f1Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8846 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 v5 to v6 - 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 | 183 +++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c index c8d5c90aa4d45..ac5caa7b05075 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,159 @@ 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; + + 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 bits controller id ahead of stream ID, + * so mask this 2bits to get 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); @@ -946,6 +1119,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 +1508,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 +1807,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,