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,