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: =?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Zk7GHqlpvSgX4mjKYTubQahcH9WD6XnJr?= =?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 */