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?g1iB6LXB9Ydp5lrNM2FW6oMNHof37SB?= =?utf-8?q?fLSKNHZvw7uzIsyq9dTQh5ZSEr/0Wkrhl4asx1M7zdnhyWcZhqsyrWiRsb9xUqTeS?= =?utf-8?q?qrQKdjvGuiU+eSX9YmjLlB3TPYgZ1c44KzemziZLYAcAJcce4afSkfgJJTGl5ZXfA?= =?utf-8?q?g7Mdg0tFk8w/RI0kdxY+Rsktu5ZC+dSnOCdduzr5u7TfgMO4Jacp5moNY7GHznmQ7?= =?utf-8?q?xNAU6mCDlCw9Dn2Rx4ghofGtoDi8ctU9DYVy338pCN6TnsvdK/8EzTJcQo9jdxSRA?= =?utf-8?q?3328cBMu7htJZqM9SJm0qaTw7fEqH/iCOt4+ElT+NtNcwrNw5CeaBYBeQi5bZEUq2?= =?utf-8?q?L15Z9vcAzW5sBkYVy8sl8UqliJ05RIPluegz+ZK1Ka9OntsdKPDzUhwWW5y4Jyv8v?= =?utf-8?q?H5UhXLKFcOVwnFtnUXBQ8ucL473ur2DbjUUFs8bbRM4l3fHR8Ax/wWw2QzwJTXybr?= =?utf-8?q?VNpa90IEU7PFMRWdS5d70QdDNOCWW9X8XfJQcvtzk6fAzqWx/XcxcSglVLBGDFTQJ?= =?utf-8?q?DEMzSwQW1qrNHVi0JKm0/WFwaZ+4FdLlNwrZ8uSh5dJDaLKIdWgBRfxaCtlZMHunt?= =?utf-8?q?4sCfr/TsPnazN9RpfYICltyBUZiblk07YIs8Oou7su9GEsunVrvurWL8QvC83oieZ?= =?utf-8?q?J2OrLg34c4Nmmhn8tejFZ/weLwZuenHGRlAOKNMW2qhYY3WRBxaz/UV/VkK/aGYWX?= =?utf-8?q?rMftMn4x7qJaSIE/KmOILqleDZfPQXMVGCjJzw5N6PFSKzvS4A+cUnSnfCKgkIVWt?= =?utf-8?q?44jVNUiHngJi4UFEQmBdWdkVnmFxlrT/sUfhqfr4RIUaBpTPMmqLrpRw/TDnMnYZg?= =?utf-8?q?9+6R49L2Yxi2AcmMN8HGQlfNZnvaxEQfvJ9j6pMtWXRub9pEAHatgDwEXUv7Lni4y?= =?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/JaOH3ha30EN3OntT1IwIYEeCFKTf+p8qUt67YTaaO?= =?utf-8?q?mr9IzuAIQNam0dE/AW4yJnDjFc6uAOsOph+gRRcpCfjiLQq46vOXAkIWytFsH883f?= =?utf-8?q?Qyqc67wFokWf9BZpFpJ5S1RSPIN1XCSBe2zLNjHq3r2Q4OntnPX5ZOPl6Wj8qiQD1?= =?utf-8?q?d3dawRbVXu7D/h/NEKA/EqQLIo61ZDkmC9z2khIQr/NdxNxjC853lzstMCltLxTT3?= =?utf-8?q?iB56mDB4TT1f1Hld/sz4hKIneZxcIPtWhtY+xU+sV9RRWv3p14bHl9VxlImkiKSJf?= =?utf-8?q?BXeb+VdnXoTnl4AiiQZA9iIQa2bLswy8gVafY0DkcOxCZePzwhyzuENT859CqUu0W?= =?utf-8?q?2BvVAp1blH2/15P13C3St/AGU62hNc4kXspWWnnmvrhGdBCSao5+6uI+yzbaLOi40?= =?utf-8?q?pVhMPKa0BYfNwgNszw3LqxhYPI4D2nCZPT9EsHHgXP33ZiZ1wsc+EgmtZxVdVL1aj?= =?utf-8?q?r/im7/ueCB1P/EpKbYWXRM/LiVuAz5i2bogFkWH7YG78xecLkZWpoxi5v/QCLj2J+?= =?utf-8?q?aEPzBEa1EwKIciIVuIBh1FcP7xef/WZ9fW+W6s9JsLrzDC1R3BkCsHV9AsK+qZIPe?= =?utf-8?q?Mwn4GSezuuwK3ME0QyrksfKU6RGW2Qqyn/fVAWlg6/evcT70WdlLXk/GWIkXazEWe?= =?utf-8?q?ehl7D88cFjj2je3KhCOiDL+jZzdX8Odeis0yhOrBRQsggdTdoJ4tQXa2uqSgbso7Z?= =?utf-8?q?l5tgIdO/rha0y320nR2k4VYmyQ+Y4nAFMo9jRML/w3P2d2ZrU8Nz3jJy57b5xJmC6?= =?utf-8?q?ogsLuQo5mOcXhtMUdipy33HC8WluaL9e3RxbiUmbZsOBEkbPVcq4IrhUZcAde2Wim?= =?utf-8?q?2QayufqR8UiVqRNAZgSIREqKjRSqz4xEcJVdKtaKKtzh4h08QL84aXqjUqgE+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,