From patchwork Mon Nov 18 20:24:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13879020 Received: from OSPPR02CU001.outbound.protection.outlook.com (mail-norwayeastazon11013000.outbound.protection.outlook.com [40.107.159.0]) (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 56FAB1E7658; Mon, 18 Nov 2024 20:24:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.159.0 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731961499; cv=fail; b=bP2lulnSOz+XU/Ked7NbTqHVSMCEDTRG4suZIUoryZeyoQUPXWXR9Omi6WiWr2KqEi1tCTSqGnzOvsduGjQqqxDwepYQ4+1O/iQ3OUR3COWX6l5kdCJLtOBUVi3Fyzqvzv1eSKeFIA/P0fc1NcJwZzk2Da6XdMpAlU18Vsykf1Y= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731961499; c=relaxed/simple; bh=LM4nM4pq5iLunWRIpH0m0Z9TCSc7rsvjExDSEjrn0cU=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=fRoKzwgSA2U9Kc797hILOnYVMo+dn165FDXY79+RLQoOdeGZ/wlrUeSCtUTepWoLDfApGiTldL/gvJgBb3ekQnEWc14mcnqTNJI74wv4+P6lCkpM0amhAljBsLU7ZHjOKqYhVmE88cbEvhVXjnAZWrGVtjZ2UMFEwV3dZj1m0iI= 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=eYYu0Vyb; arc=fail smtp.client-ip=40.107.159.0 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="eYYu0Vyb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=MvunnkEcL7GTCkp35VoxoIqSSaZrxz7TLHneTpFGe+YNTUDe1xGefN3AvvnbcBaMiUvPJNHQcLrv2RHfGiuej23g/P6f2KI50w3BsPgvZr8S6AhdtbgKz0O64SUcl22bue9ZzyPFlp5NrQQncun7hpFyBowzVAhJud76yeeaJX5NYFA2pwyyN8EoDfB3YkqeysuHU11JKG6T3FXNsuwlwbLM8vC174zIndVVvWiO5aFUoT3IZOz/9aq+mDDE1mrS8g9p0Jf+4KQIj8xzfrBb6N4C4Z65IExrvA6lmsYGZEbf1Q16KZJwiNWlsPFQscUtXbZIfswFPerazHsRl4mESw== 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=PjltTYAiP3JJUYeKeACfTOThUjyhe58YE6Cz4xazx5Q=; b=TCLRtnI3wekOXp0KceJ5Z4LBrTGQFCD47f86DzqCfOgyulVZQAvlX3hWjbHmcvY2ITvtP1qy0eMbX1fkcGyCiPCM8jPJ43AEU4PVeDnR5szD/83ZF/uVAEaBqDMm34vrbGYVk0/vHOVvwB7KlY2Jtt+CM7IUMMUmC6O0D1b/04o8hUT3XOPrX4xVNPQKcN5MvY06L4Gx+pblMrl3ZnU4XOAOfU9TObZIvXE4qzvkjYL147QOtyE4+Kon8bkCc9loYK/fX8GYWGkHNWOOHggzQMHFEjP88p4XRI6JUiLJblLjdhGLg5zvup5zVP+YC0tjJnvoHB1ZnfnXW4Me6KhaeA== 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=PjltTYAiP3JJUYeKeACfTOThUjyhe58YE6Cz4xazx5Q=; b=eYYu0VybZ8ZegWPl6BbHHg2bJL5XsReDycX1ze2cOGtOx468zV89xGN1UiEBWZzwb4IsqZSHBQJ9LbTErvBp9YZY4o/a3xNAfmNaRe1FDGIlt0fH0zlqJT/lP9i1h+KBWb2Fzp8HeHZzUa3q4fniNoRzwuZPDO9NuvMCcaXcys5nrxfGHwM/WeL31nrGlPv3R6/cBA1bVj7qJIrO9QmhiXvBcC2tkw2kudA6kQEjpnzAk5HebIyZgcVEk/vKwxoWlz2Pzb1eZOOJ4Wi5g5MQR2sKxtGhIL1nAM6X3NGl7aUwMzqRBHIoCVE8Oc3NYAlKDHSEPAVA8exYvW/w7nRpYg== 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 DB8PR04MB6858.eurprd04.prod.outlook.com (2603:10a6:10:113::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.21; Mon, 18 Nov 2024 20:24:54 +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.8158.021; Mon, 18 Nov 2024 20:24:54 +0000 From: Frank Li Date: Mon, 18 Nov 2024 15:24:27 -0500 Subject: [PATCH v6 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241118-imx95_lut-v6-1-a2951ba13347@nxp.com> References: <20241118-imx95_lut-v6-0-a2951ba13347@nxp.com> In-Reply-To: <20241118-imx95_lut-v6-0-a2951ba13347@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=1731961482; l=4315; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=LM4nM4pq5iLunWRIpH0m0Z9TCSc7rsvjExDSEjrn0cU=; b=J+WakrLToP+kpt9NmKzlo1QeqlCklM/+nIXuqTtTPTDbLN5wMYV/iPqKiox43tVtV5xs0XKni tyubh5j/4wuAzoA5BKMgMrGkSknQAM166M719sfGQdRfvBHAqVzHrWQ X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR02CA0048.namprd02.prod.outlook.com (2603:10b6:a03:54::25) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|DB8PR04MB6858:EE_ X-MS-Office365-Filtering-Correlation-Id: 8e5e3b0a-817f-450e-d076-08dd080f0ffb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|7416014|52116014|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?1oBW9C3gPCHUmPGbnnI7hfpGZTY69wB?= =?utf-8?q?IcALA2mACVFEgUH4v+Oaa+AC8CMrApBvRBAlYk2A/o9s1h4l3E7WH0qo+W7Ma0Vwx?= =?utf-8?q?IClFDHRAm2uQLuweHzQ58O8ypLTzE+HFaV9msczaRQXZSlx+HFPH5TaMu6v+mrjLe?= =?utf-8?q?RXFRP5rHVj+XcXahxaUb6HfGi8npx/AnXXvBqhgQQ7V5mTvdtyA2v81m/9107Ij5H?= =?utf-8?q?FRWHFw7qIgrfWtCA9lrZg1uUGHdH0Jnrj/Yi66VgxnF1ZzWqSHZ2sDBAsoYrZok7F?= =?utf-8?q?bwm66Nv57XzqvDUz5aZNp9v27gL49WgS6tncrQRldOq21ejkm0YPTjtLKdjIg9GI2?= =?utf-8?q?Bd8mm/rxjjN4MQnPQWDAwEOyUBP37AF/MU/XphDD0XlmnhGfJluXaZSrAchHIkyKS?= =?utf-8?q?P34OGsUU88OEyJrgWrncFPHW/ueQRxZ75UuzrVruCThKO1MpIDjGOJgu74zOtNGQq?= =?utf-8?q?gir3B8eIaQlug2a6sPSTm31Jr+wwd81NRZn4sYUXKGLjc49MurrYjXVUqbHrLVo1i?= =?utf-8?q?IctuQLTZtWXwLa4wAyxiAmYPOVrpuJDLCAaTwAsT8b/hrbJx/qb+1AmJNt8a/nZC+?= =?utf-8?q?sxlsMC+JcfEVG5luT2rFzlVFCOoSdUklRw15W79tVuN+ZoGb5Odp1vJ+UfgiUYms8?= =?utf-8?q?I9aPoD4sAOljrkTOvJKuGEQvgePWW0HIqV0RYNx/YXEXKZZw7wWaJbcNNgIbtIvpo?= =?utf-8?q?1UUCHnRjdCQTvqmoBKJwlgysqXe3kVSXF8Hd+85Jl63T5lDwgcXG75JXb300BfNce?= =?utf-8?q?Nuff52oxdLj4fpylSU8P6akRAv9NQpvC9Gjr3wyvqVfCl2QHT+oGuFsMhrmpDNQPz?= =?utf-8?q?JPzEXY4DAVDSRomGtBB646OQ/r+/rs8kXLU0MvTVsqrWSl6rG7CcjPThTLaOfQfEO?= =?utf-8?q?uFFF6/+uP3jHnnyMHlPeHWZkYWckMz8LbznXz2NowrXaz0DNG+8KJnq4lZWxUFn+q?= =?utf-8?q?Kg5mbSTXpbq+8Px9yPyuZ8P4DyyEtsw/8M0LS6MXKz/C4yM9yRPPKG/iAmgkjEHUI?= =?utf-8?q?KEuPkwB2A+4A29IxUZfTcP2qwPodO1u/fjxY0n+KRO04snGCJoJEjdSHWS2+pytVJ?= =?utf-8?q?H4IjMrnjKucAbkErPEqVFZEYkpZG8OgOCLhm5VHOf/4wTWSm5FeK72wvHHCC7wAEw?= =?utf-8?q?Xrl+u3Fl+dUZl0gKildnxc2WaFnNUh4QKV8klx33noHlfNOIGzj71rF/zIPYjdkxB?= =?utf-8?q?qCd79yJdBrl4uolhwVbEIF0JLUchMVA7TYatx2R1T3o1nfUaC8k8wONuw95AoR491?= =?utf-8?q?ezqsvW9xg1sdoXZJeyCqWke3cCG6ouhq78tM/QtBpCGrSTxU1QB6wM0yXkoEIRsn4?= =?utf-8?q?EoNXHV7MqdYc?= 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)(7416014)(52116014)(376014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?laHObGojsmKz5GAuLWLe1ao/d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9EmhwG0oc905hQw9Iz1UFychAZLbMlJhECQ53jXaqKTEfn7s4YIoyHT0iwPJBo/D?= =?utf-8?q?KO+MSDifTX6fGm6JD0ajOSGE0BTQqTkjp4Kajhb7nwHhmbmcxzd/yYUH5U8+LMveD?= =?utf-8?q?RKjzxNZsi+7wHu+JpK+9SRqofvhlNvkYFP1VgtZMxHuABDG5saSbmykvn/K6Owys8?= =?utf-8?q?EI9QJUXONlNqACh9geAXQ12dS+4/sl9d2nTsJuq7T7JwLMLPirkUNhoFzSojhZsp9?= =?utf-8?q?XdD18uzwEWjaO3zJVV8zC4ajQX1xhlSJ4hXbncUz4CrKjmC9FWzMD+k9yV9P4ClXs?= =?utf-8?q?R7MirnVvlT8ClzGO96b+9qqeER1jq/FM9GJ+iaoCrLXfFU11IFSnXXGmGkQZr9zp6?= =?utf-8?q?DuYZtnhtK1Ny6N30a6/iBv+SyDxNpKWt5rLWVNL82y6n0nO4ybl+Cfn6kse+KiO5P?= =?utf-8?q?jWrjy5mqxhljiaTHNui36/bdU+JgOpdM9TKK1XgIvKsUH8GpM1jsmcWrVx5PX8eWC?= =?utf-8?q?ovI5DrFfK4BSiueJopFFVg4R+sYF5bWlhE40KDY4V8+y1J5KZyvMTh/44NpVipIAA?= =?utf-8?q?/L2f9QDAWmzIYJYnQjRh+Fe3+229+RIXy0lQt+elLayTvMIpcZWKSEQw=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8e5e3b0a-817f-450e-d076-08dd080f0ffb X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2024 20:24:54.3190 (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: aOE5drYL0M4BeXtmD3x0MJun35oJ/rPQxbFVrEkpO4TR51Ze2N7jMoNqEyQtilVTrcExVqk4BhIL+0P7UAUgDw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR04MB6858 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 67013df89a694..4735bc665ab3b 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2055,6 +2055,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; @@ -2070,9 +2092,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) @@ -2087,6 +2113,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; + } /** @@ -2270,6 +2302,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 a17edc6c28fda..5f75c30f263be 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -596,6 +596,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 */