From patchwork Thu Oct 24 22:34:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13849820 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2062.outbound.protection.outlook.com [40.107.22.62]) (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 36CE921859E; Thu, 24 Oct 2024 22:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.22.62 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729809309; cv=fail; b=XmsIk3+k4huW4ozFrN0DAnzzC4OMZhP2hA6lR2dr5XctOz1jgea1hgGlGAAK4NDSugFCo0vdfzgptLbIBBkU0BFLn2sLqaFh3XUwC6yVYFVTbIiXLhMhe3gvZeiy7MMeAs0dAqOROf8ro4vyygUiGJj+SeB8U0suC9wl5/JykD0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729809309; c=relaxed/simple; bh=N+O2/eXaO6AkhaAeZYU5FQuPIX9O4F9QoICBgRu6MQM=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=Z4bF76TqENdD3xfh4SJ+OaPfZZFbaKBX/tQLNY0NlnlO7CeLsjRyq8/dcNx0eglfbHK91JiKSbiRUC0pGDqXwYrEjqq5nABCBU4kNa9lWzcC67/BuW8BsWlFs+yQRCmmav30gnY/vhNHT6PKrzK8/4v79oZrJmQWUpmdO/jcIpU= 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=C6Jl82q6; arc=fail smtp.client-ip=40.107.22.62 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="C6Jl82q6" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=syK1BQnSiwgKWHZj7DLuHOeNQe33pjjxvQe8w3Z+Lt1W178Lhg34JaJe93aERbgz0RCMYNcMyNyhGMCvMMQaxkd9Qn680WWAzvuff4eQ7OntDAaD8V3tdxLyhrebopfW1Btef5UuXCnz5W+a14TI7dEPxZ5YMeAU23aO47xgtIOao0nbaoW/zrpIGxo1MrqgnhxR6+t5WX8mQGxLx22TEjg7nMz9+DebswD1GZMU9/FnifuwQ5Q76N+t0F7v/co22BjR37Uy8LRJDR8Z+UgkzT3J6+5BBZ4+nwpabtEjRC9yTDAa7QAuwgl3aXLmId7Wop3fZsld6EpEQgsZPqv5NQ== 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=gGMO0kSVYnR6lRqgwfKP0k2e/QOTtqGn3+WzHCXK8bg=; b=l2PmKmFaQ8B7RCsG6ckRMvQhLIgEGKwwfCZw4fTooPA0FaC19zItDwK5z9Iy6f5iF485ca6a4ZND64xlbrpuVg2+KkaW81ydwxFe1lHOrMy5WWvcwZOt9ugAWR5eHCVbInQ8ETo+JdAPhntiJc8ogmYskg92fVQX1AnLwP30HlMzIV6gqQCwKC0IuK0sLRSLW5K8d4c8t0LzLuxQ1dPAc7AIi1gM5xixNo1FUu6QgxJd+ITwWgp3/E/gJTQG90bMZATK097ibCrBTcdxsBSt7uhfCqwgt+fMZ2uC9vBzs19nrxXuPRwIK1/XISk4nXMZWF4T2VJ2n5DH3qk46cXCxQ== 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=gGMO0kSVYnR6lRqgwfKP0k2e/QOTtqGn3+WzHCXK8bg=; b=C6Jl82q6QHeuLTpFdImfbJRPxoZWIsdxyJVuwuuQC342UYDTDuFj8kwsc1dchGWzjoZpVeGs9sEsboM5UTm8O72HMz8iSKm2JliklU6b8CeOBv4gMoh5FlLATHLglLhzTJURO0BN4naSpMxLnOVt/ATIPLOZ6uBy6X02ES/3IL8GgyuXW+w0PwRcHCCovswX2DoiWBo4eHc2zigZcDwwdHe2NZK3DGNZLm2tqsnIBRHUYw5CO7iFeppb37JGTiqCFqm8QHMzenmyOTlNuZv9Ahw0Io4B0/S2i0U3juaDIEJFjz1cwpB0pNygjZXGfi4OMzZkZUbZhE7EX+Je+L5MOQ== 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 AM7PR04MB7189.eurprd04.prod.outlook.com (2603:10a6:20b:116::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.20; Thu, 24 Oct 2024 22:35:05 +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.8069.024; Thu, 24 Oct 2024 22:35:05 +0000 From: Frank Li Date: Thu, 24 Oct 2024 18:34:44 -0400 Subject: [PATCH v3 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241024-imx95_lut-v3-1-7509c9bbab86@nxp.com> References: <20241024-imx95_lut-v3-0-7509c9bbab86@nxp.com> In-Reply-To: <20241024-imx95_lut-v3-0-7509c9bbab86@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 , Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1729809293; l=3684; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=N+O2/eXaO6AkhaAeZYU5FQuPIX9O4F9QoICBgRu6MQM=; b=VesD61ANJVOLjtQA0ptlj+ydmMtuGCK6E3XEPit1tm5yjGFYLk1JG9SUmyNEXlvm+mv7TFfi3 tBpSJnUFBGeAH4L5S6QmS08eutaB4dZf78BZyrkag2KF3bxgEhWatiL X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR05CA0001.namprd05.prod.outlook.com (2603:10b6:a03:c0::14) 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_|AM7PR04MB7189:EE_ X-MS-Office365-Filtering-Correlation-Id: ac1fec68-e554-4d17-5bdb-08dcf47c1bab X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|52116014|7416014|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?0VRUBnG0/LpTrXOf8q9gbaaxMxJE0rU?= =?utf-8?q?emSdhMBMZj9XkoW6z1w5eeLsFkMTNH+xlg7ASUeKvnzHcgMoa1CI33pv5tNMqOLxE?= =?utf-8?q?43NshcagK+bQf8Ofw1hUf22JLpcLr1wBBLEVk3ZVHypr/kYlPxc5dADwiWpZBD9C7?= =?utf-8?q?vfgqRVfzwEvtf/NhD2rMgVAeJ2otLSr6ncOA3eRlQBzepH14F0f+FJmZkn7K4ZGhQ?= =?utf-8?q?Vz6Ev3GkCd098OSchUsAotjPEVT/2GP3sBcEFQwGDPBtQaj7x/XnhyC+D6mA6aDam?= =?utf-8?q?q52iq7Nbej5wFSL1+4f0WRuwwtUcbMNQbt1UXZQEHT3KcnQnrQnFMmXl7HvMIhPyE?= =?utf-8?q?0ln4luB1jN9QwVSIv80oEdxgH4NfOtjWi/wfYSuw5JZosSB3RqjuWKntsLdt6d1BS?= =?utf-8?q?xOPIDDSqZo4SoEVOaVM5J038WBhgwJmIawwnJFYG2SCvFR3bfI9brWtSg7H9d9+4y?= =?utf-8?q?uQj/FKcr/+if5uqZHGvWAcRYIMudf/AJpsexgT5yUdFH0vF1gpepmmyDCh3RSB8KM?= =?utf-8?q?i2CwRKR/fT0VLJwqc4E7X69uS/1at3XHEyUlTG4t5GZyXI2LoJ5gnPRwy7Kugr/G0?= =?utf-8?q?N6qNiJR8WMvRDrTYLlVk4wFT33vxe0NlBZ4vgKOfmhCWMNuBAfhqLWe311M0h5BH6?= =?utf-8?q?2hBDCskDjSP1jQ4ESzUAJd5ozbBP0maBmBF4JCmNe4wcYfXFKbMqP95zKl7Eny9sd?= =?utf-8?q?E/jqLSHzWQM9iJiMDyIOeWd5HguPfxAOLJ8QkuNfvah3wwyvKyta9NaZYpy9jkzEu?= =?utf-8?q?DUrWCAu/I+p2JaOU/a+LhkEM/JN/xLw/BIgCLD7UqjwoWjgXjT7gYmfV7fKIbFF7l?= =?utf-8?q?Uc5DsZ2lYJaBvdPJrkaMDv9CDcVM5bvNebp/a9/keaKAazSb/ypoUvXnbgMHDNvPR?= =?utf-8?q?ZIS7jzZf/6e6zJpp0+yINhtXDYQPQuaJrjQH0FH8fjzZutJ9OuM6YywSMsCv4FkWN?= =?utf-8?q?Jm99vsgU79ecd/1fRruOpmyARStQocRnWuvufhR6zXpWqU48NUAZL91hz+b+OJPeX?= =?utf-8?q?VOY2RluUClCfuOkNjqgggXND6Qv25BvkmWrBMYBPsU7chdvdM1i4udUTxtzfAlA6N?= =?utf-8?q?0DF2dLJi+qDvcqv5hGcw5E0DN2f0wmqXvDn/BrGUYqSCWt0t1vCjXSBYGnAKayBXM?= =?utf-8?q?XUCf26VidSAYtdmxi02/aNoKlUKjyJHQYlEoSI9X5hjs5PfOkvfNiIlI9R9oEjJkI?= =?utf-8?q?PEQOSlaJCj6W6ko2CaHMY0aC0BpGI9BhzxK7/jl/KjydwsrxeuWhz2HrNdfbOUaLj?= =?utf-8?q?MnFW/QUuNN8GUZUTPXIlhssLhjvTyjFjXufOzndiMu/vdoy3YM1ua9cEamsUYdG94?= =?utf-8?q?37s7ZMYDMPrc?= 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)(366016)(52116014)(7416014)(376014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?ugqKctoGoh9ka0dGfjHNQb6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tL/?= =?utf-8?q?3ddUJ6yrfYfLosdMWwKuEy19Zefl3IWTg2ErgoGBMtZT247WR0Sfe2Q6tk7YzbI7D?= =?utf-8?q?5hEJSwJ1iODJVblXRWDj3fpzwCmexG6H1cpUS/S2mrD/xORUaUGlIaOlB1Zr58ysq?= =?utf-8?q?/dBjoLwGV2aEp9kZM1XlhQntBcqoQ4JoZlhS9S+r+2K/mSaT+M/+KTqaUvRDeDrSV?= =?utf-8?q?a5He2d5CZTlrdlBnrv2FH/ms3g7UqykcsSLEO+8gRArOp5o+vgvIEYlh200qHpIXx?= =?utf-8?q?BL7WGpQXOtjGgjKhpShlNIeRfsGlo0Ea5VMGord7uCLhH8QBStm1gYdQIFxNouDHe?= =?utf-8?q?sFaSz9SEygkyiD6QG2vNj9Yx730q3BV91uw0Y1978sPL8s+T3XeHfNW6onHV4+S41?= =?utf-8?q?RTWrLCgFSQkCka3YQyBXSHWgymZW9FG8FxIXzfvwCYGwButqXqucwQwB14ZAFOHln?= =?utf-8?q?IczpK7yFGahWFJx0qAfCQYKucqbVKJ4puPfEcmJ0BKI/1dotXURfSbv/DXULxQVbz?= =?utf-8?q?eSa/U1EWwWFp/Kd/g2pwzdEfRjUxdczyhsM2JtbwD8IIiwgycKylvNWMspTSd+efa?= =?utf-8?q?b+yHJveC22PzjKCBGsp+/0kSjDkajH4amR5oRe0IC5waNp3bQF+CHwzLHRMs1R6bY?= =?utf-8?q?XbnFlYbZSC+x?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ac1fec68-e554-4d17-5bdb-08dcf47c1bab X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Oct 2024 22:35:05.7773 (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: 1xd9i01Xo/MkismE9znLBTH9SE5jbDnsORochsx7+/6Ep9juSUx74w1qxqZcae1ipQ/0XUxoRLdKDQ51CtHeFA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR04MB7189 Some PCIe host bridges require special handling when enabling or disabling PCIe Endpoints. For example, the i.MX95 platform has a lookup table to map Requester IDs to StreamIDs, which are used by the SMMU and MSI controller to identify the source of DMA accesses. Without this mapping, DMA accesses may target unintended memory, which would corrupt memory or read the wrong data. Add a host bridge .enable_device() hook the imx6 driver can use to configure the Requester ID to StreamID mapping. The hardware table isn't big enough to map all possible Requester IDs, so this hook may fail if no table space is available. In that case, return failure from pci_enable_device(). It might make more sense to make pci_set_master() decline to enable bus mastering and return failure, but it currently doesn't have a way to return failure. Signed-off-by: Frank Li Acked-by: Bjorn Helgaas --- 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 | 23 ++++++++++++++++++++++- include/linux/pci.h | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 7d85c04fbba2a..5e0cb9b6f4d4f 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2056,6 +2056,7 @@ int __weak pcibios_enable_device(struct pci_dev *dev, int bars) static int do_pci_enable_device(struct pci_dev *dev, int bars) { int err; + struct pci_host_bridge *host_bridge; struct pci_dev *bridge; u16 cmd; u8 pin; @@ -2068,9 +2069,16 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) if (bridge) pcie_aspm_powersave_config_link(bridge); + host_bridge = pci_find_host_bridge(dev->bus); + if (host_bridge && host_bridge->enable_device) { + err = host_bridge->enable_device(host_bridge, 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) @@ -2085,6 +2093,13 @@ static int do_pci_enable_device(struct pci_dev *dev, int bars) } return 0; + +err_enable: + if (host_bridge && host_bridge->disable_device) + host_bridge->disable_device(host_bridge, dev); + + return err; + } /** @@ -2262,12 +2277,18 @@ void pci_disable_enabled_device(struct pci_dev *dev) */ void pci_disable_device(struct pci_dev *dev) { + struct pci_host_bridge *host_bridge; + dev_WARN_ONCE(&dev->dev, atomic_read(&dev->enable_cnt) <= 0, "disabling already-disabled device"); if (atomic_dec_return(&dev->enable_cnt) != 0) return; + host_bridge = pci_find_host_bridge(dev->bus); + if (host_bridge && host_bridge->disable_device) + host_bridge->disable_device(host_bridge, dev); + do_pci_disable_device(dev); dev->is_busmaster = 0; diff --git a/include/linux/pci.h b/include/linux/pci.h index 573b4c4c2be61..ac15b02e14ddd 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -578,6 +578,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 Thu Oct 24 22:34:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13849821 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2087.outbound.protection.outlook.com [40.107.20.87]) (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 24AF7219492; Thu, 24 Oct 2024 22:35:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.20.87 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729809318; cv=fail; b=j9YA2vZqgldhxaGXEUNeZtc+GHkP5ZICX/VXmvPKgyU6TJfWLhwq+LjCrHtTNURLIHdaLVjh9+8UXy5HQRXHLvs+TbnBPkaE8mGXaaRudUbEtjRIe21J5OGA2IJ9KevpDc5NtLG8S58/UMpLFenb1QPxebsEBg7hwKPX/yHk/AY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729809318; c=relaxed/simple; bh=++Ra5UUPQByKYlOdODjlqMpR2reTqaPcz/EXBHMKqmo=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=TGItrHVedDyuMhw6dO+ZentrkcA2oHyEwNEmtLVvuNJsdftCExQtt9V968OENF9seKXxy5n/QZ6A/6j6Tvym1+crdMa1ZVCaHBdnBMEu+A965oXaqCNr9klCGu6l+p7nbU6L3+oV6gOLuDWAjmdUD5dubeY30lrHKjMpQf/erug= 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=NhKjr2Vb; arc=fail smtp.client-ip=40.107.20.87 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="NhKjr2Vb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=VD6hsDcwD+8N1eNnQGc0xuT5JeiIkOCK5unUsTWaLYTBJPn9Lix2nrcbE1+Bt8v/aSENZl94JrUGFpQPJ57UB465MAp4mtkajPauxbZQ5xpGdq/9bc+wU3F3iSrm/8nYtgrUlnGj2PtOlkum9he64azqvvoIkKtFZFfxZP8Ev3mpZNPDIlM2KOTroFIbAy4wHXI73ZzgQMUVC8lgFfKkFdtu8SWHkxai2JBGp/0ZLTBROtfKau43wjqFP4SK0YOIhCho4SwR18FjbUbF0PC5bDXL3kAmL0McORKSXFF5/PcljOU5vilmQxZfU+ieZP0Evx+CcUJAyTBaSu9/L06H3Q== 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=Sw1r65JSYmrG2SKNClARjtA2eh894aOhfoVKCDGVrxg=; b=UkG8FfIDiB6afFbdjoHcJgn/fUH/pC7ei/WCsFcGAH+hx0Oh0E4iZMwyn7Kz+aePwEEPnkJtja4tffg2zXdG+jUrWD3UWbHCJ884weP/Jc31YhS0JwweZEUm7tlT+2RlETNtjS51F66fWlYFJ03qIRVfKIBgWjs2WCzRGUPmdGkCD7hXTyr9V2siSLpUX/3IgTFeGvCn5/gzTKmdgZKn3+F7WDvrGQfOEDF4wf4uOOa5ueu7UWmsnMIM4EyANgnjXV9XLeFX6VRbq3UTLI+xtoN4P95nSTgLxagvbONOuNert4/zpAEIDW8PWJFUHiDBhd4gYXUTeZaReH5q2x9h1g== 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=Sw1r65JSYmrG2SKNClARjtA2eh894aOhfoVKCDGVrxg=; b=NhKjr2Vb1Py968TsDzn3cjpJADr/URphKDe01L9NEgjuEHNPq9h1nawV3CUYz56ien/ScFYVVJMALpvnN56WzbyhRadcHgHGOOV1NqxJCIWQrCkUtHLPig0pab7WPrSECSZmAcD4mQR5rXB9VrKnW1TGTOmnvPrNtW08s4N3Od/kAH0Jd1Kszb+vAnX+yWSKqSkjrsnsaLnsntREFyfAkjmbcfUJPs3g4YuTt7wwy7hsSNloFMFWnh6AZEEkx30PAqAjVkPlYbiHLxIwVjI+HrHWIOKCm2tQp5+Y1ybwwApUdn7GRkMnvn6pm2ESzMeMBhcx7t2nAcrGeAh8wa3N4A== 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 AM7PR04MB7189.eurprd04.prod.outlook.com (2603:10a6:20b:116::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.20; Thu, 24 Oct 2024 22:35:12 +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.8069.024; Thu, 24 Oct 2024 22:35:12 +0000 From: Frank Li Date: Thu, 24 Oct 2024 18:34:45 -0400 Subject: [PATCH v3 2/2] PCI: imx6: Add IOMMU and ITS MSI support for i.MX95 Message-Id: <20241024-imx95_lut-v3-2-7509c9bbab86@nxp.com> References: <20241024-imx95_lut-v3-0-7509c9bbab86@nxp.com> In-Reply-To: <20241024-imx95_lut-v3-0-7509c9bbab86@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 , Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1729809293; l=7490; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=++Ra5UUPQByKYlOdODjlqMpR2reTqaPcz/EXBHMKqmo=; b=Xnj1jOlE/HzeXRsQUoFRSJNlKXL//4xmQphSrwpQd7/t83Iv82dDXfA3EJ+n3bgH9AZkSGDGs ix+4FTCLsaTBF5t6H41A/uqaNdROkSdnZAW++8S9SExrUt5gy6ba13P X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR05CA0001.namprd05.prod.outlook.com (2603:10b6:a03:c0::14) 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_|AM7PR04MB7189:EE_ X-MS-Office365-Filtering-Correlation-Id: 43cb1f34-51cd-48a5-ea34-08dcf47c1f94 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|52116014|7416014|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?N5pw1TkXGabZnP1wFz2upqKyBxb09zW?= =?utf-8?q?6GNZA5W+gKdoCG9Vl9Y7YG5XgJ/oWrCe/Aq91UiMCLvtE8ZkBOyrARl2/+MEzhBXt?=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E9Z99R8fr?= =?utf-8?q?Kx7kpdmUUuQeMhIBgfAuTpHesBnIbaPYA7R/fYq3OCOTXIZ/VuZrA0b4IAGG9GmUE?= =?utf-8?q?2B9IijYpEhi93rJwMh5Xl0xGzsIfVbpPKX6A4tgSZY+pkBSKhkLLHzP5vXc9ny3tf?= =?utf-8?q?78uLIilwWWbwGQ8hZ2oRG3Bd4vTvrENxQ5PJJkXjXemBPQ4RnoQdJ3PBlpuN8upwT?= =?utf-8?q?Vq3aGwyIDdYtBYc6lDtCFF5KN7O/+WTdy03xAkL/RvPoXfl/bVnNDYDs6VOkaFvrz?= =?utf-8?q?KuNDPhyIIf1mvSWtvsQrZAemXgdUaVBCG+mhCGPyoEZyKikZxEByAs2wYU21YrgCq?= =?utf-8?q?DueehnLQPkoacQhqse4lK/FaKmx4zR3W9XuXKEhsIv1cXPWqtMg91aWnklRp+9e5m?= =?utf-8?q?Abw/Z0pRmTVM?= 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)(366016)(52116014)(7416014)(376014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?8b5qIDxzXSKZlekD9P7AwkS7de26?= =?utf-8?q?VUWvs3GWNMRlYcmmy9XB9BHpghROXFSq/PAzBifqfmqaJvD0eUr7iwOJ7TZXHFZL2?= =?utf-8?q?1x4pwhrIHfUXDp7skmFB7RG1t8KV/Ag1fUKctaYHpNUAiSuJM9Ajni7Nw6Qb+/s12?= =?utf-8?q?A0S8h16/KvvH82qyAm2Yul39WD0Ot0wn0zySftNgVMeUJkBMC/U/doWxHaqPNaa8w?= =?utf-8?q?Z+ahAXwJ4JTNnnsNJ+EBnOyulyC3/MfrDfZ/8FKke6YGKvsnxhz0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GJPlfiqnxmhKw?= =?utf-8?q?ilDwJ6jEf02mSY6vGdA66X9edNgwxdQudbpybBA6wjNu6XGI8mwxaub4NN2jBFMoV?= =?utf-8?q?ao8A/kk6aUu08CXrbzFPxU1S+QQ+XHsHs+vaf97AvKe0OtUuFmVSUiwHwqDE9gSH2?= =?utf-8?q?EDuZiOgeP8pPAM7jhrBujKXymGH2uUIWobcfA99Zjw0UvZ3pgcYnvDZkrZnpSdIx8?= =?utf-8?q?715AG2ccwFjQMvCOHVuczi7oybH6UkF/fsExnH2zgi5tUo0ZsOb0abPQDNYW3SQRS?= =?utf-8?q?4bmDsm4BB2T7huS/hZ2AEURcNCzrW2+TdKsbDOsjvfm0+lXn89a5Ir1Xj/VPs9w3c?= =?utf-8?q?lG6Q8IXUiibT+Krrg3lnwsGj1/ri9CY+K7mID1CZ929iZKLlrdcxkNBFvrGfbcyWm?= =?utf-8?q?01dPhs1dr29R?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 43cb1f34-51cd-48a5-ea34-08dcf47c1f94 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Oct 2024 22:35:12.3398 (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: vFj2j/i2g/JxjIZRqe8E2tZ+HSJWyXTL7U6vFY+8ud2XV/iWyyqQmxvoqwhfEgG2NHnADkLtmDJXl8Svgeinvw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR04MB7189 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. Additionally, register a PCI bus callback function enable_device() and disable_device() to config LUT when enable a new PCI device. Signed-off-by: Frank Li --- 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 | 159 +++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-) diff --git a/drivers/pci/controller/dwc/pci-imx6.c b/drivers/pci/controller/dwc/pci-imx6.c index 94f3411352bf0..95f06bfb9fc5e 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,7 @@ struct imx_pcie { struct device *pd_pcie_phy; struct phy *phy; const struct imx_pcie_drvdata *drvdata; + struct mutex lock; }; /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */ @@ -925,6 +943,137 @@ 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 reqid, u8 sid) +{ + struct dw_pcie *pci = imx_pcie->pci; + struct device *dev = pci->dev; + u32 data1, data2; + 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)) + continue; + + regmap_read(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA2, &data2); + + /* Needn't add duplicated Request ID */ + if (reqid == FIELD_GET(IMX95_PE0_LUT_REQID, data2)) + return 0; + } + + 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) + continue; + + 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 = 0xffff; + data2 |= FIELD_PREP(IMX95_PE0_LUT_REQID, reqid); + + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_DATA2, data2); + + regmap_write(imx_pcie->iomuxc_gpr, IMX95_PE0_LUT_ACSCTRL, i); + + return 0; + } + + dev_err(dev, "All lut already used\n"); + return -EINVAL; +} + +static void imx_pcie_remove_lut(struct imx_pcie *imx_pcie, u16 reqid) +{ + u32 data2 = 0; + 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) == reqid) { + 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) +{ + u32 sid_i = 0, sid_m = 0, rid = pci_dev_id(pdev); + struct device_node *target; + struct imx_pcie *imx_pcie; + struct device *dev; + int err_i, err_m; + + imx_pcie = to_imx_pcie(to_dw_pcie_from_pp(bridge->sysdata)); + dev = imx_pcie->pci->dev; + + target = NULL; + err_i = of_map_id(dev->of_node, rid, "iommu-map", "iommu-map-mask", &target, &sid_i); + target = NULL; + err_m = of_map_id(dev->of_node, rid, "msi-map", "msi-map-mask", &target, &sid_m); + + + /* + * 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, "its and iommu stream id miss match, please check dts file\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); + + 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 +1090,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 +1446,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 +1743,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,