From patchwork Fri Nov 1 21:04:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13859763 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2057.outbound.protection.outlook.com [40.107.22.57]) (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 13C2B1E2846; Fri, 1 Nov 2024 21:05:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.22.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730495109; cv=fail; b=fONJ73RFmje/jatCTKX7fTouEVL8XQMFKqqF9qjmB2JtB1pEVqCB0Gr8bb7KeatNxYlu9kLpFwMIi8LPc5ogZPSNTXrxFXHOsRDPFk7SoA6bDJjwWqtkSYrfRjYUP0McGLIbCAedj7h62mfOUQWjfM0+cCiBkrt4s/FOIrSZ5f8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730495109; c=relaxed/simple; bh=rJIqxvluRC/yMOR5R9EHOlh7W/U5VF9REJtvm0zbl1o=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=E00D28qCFAZKf9GEed9feL/RnefoE19fHZT6sCNaynMvQXtHHw3qO5pZLRfijNUPyoviGAKOx8ShonR6JRAKjmDau/mWE3aSSwDKqtk+eoWYpD9DifGVHlAQlMUwZSmCKL2Z0lSF5onTvMOCTR28+6k6QDCCTJ5eS9hAEufY3ck= 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=AzWYrHVP; arc=fail smtp.client-ip=40.107.22.57 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="AzWYrHVP" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RpsN5UwMQVpd2auImlR5HpYcdkS89MdoEuFLjK8E/2RF24ozhf4deZM5uYRPsvWdFIVgUD0493qokCcEW8Q4Mv2mgi4yFENp9HHsXFhgHOr2T/aBunJLPSqZ2We6+afXYsno7oxbLbmmkdQ6M0NVnOXxPL+0hzaq9S4O5q6chk6ALmI3+UtjIJhJNfVM5Nmx2s05tjpe3CtMm9IZyAZWEr8voXOOQ5qhE4efTEUrRp+yhUY1hy/gF87McxRFbGPmcQa1El9Wd1sC5NNW+kq0oIEOktT1dqSLOlfur9yj0dLUGgPQYRktUGghS1AUkXGoeCKfYW6u4nGSj2ugm/SISg== 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=rgZfvPdpXTDMFhwi/+fS6tJCSsYlh0cHbpKlPxzHmSU=; b=Z/PRm1Zt7f4VwjbH62PcQJuoOXOFKvvT/guMOJkgAZaP9LNXzebiVqPxvWh2T1aC+OdTwX8sheOqrdMiCehiExm+xv5vSffykhQ0EaamdxQYa4yXDA5Fh4rxV01vGm9XoLN5o5lnXlXz0Gytwk2xes35w3mP1D2T9v9yLUnT3rlSIWYceSN+VyruaTqensOq17DDF+xQvCIAF4pJSnSOWe1sNBsY5+eaH8/x8GgfcO3MtayzKVDS1eUhkPxXaBoI54OyTRq6gdf5C/l7qExAllmNez/zKgt8fNMp4nOx58fdC0DMlpeDj/kSMk8ygCGnFJh8htOF4KSEgNjOGObHlg== 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=rgZfvPdpXTDMFhwi/+fS6tJCSsYlh0cHbpKlPxzHmSU=; b=AzWYrHVPO9jwo+DEMwHsJT6JGOgtWgoGtM5Wx+T/12Ccpp9Pxvqty3Gtyd1IVlFRpVUPTbdjMwT2FQkgWI9SbmqMKfDJIFPWX5K7/4oIWkvUl9nT8iRVn09JQ7ibs2KjHLSr8zd5nj1pT57C37j4LfCib/dcdGkiWoMavHn4Egy+i5lUdSIPy8NP2Xfqz7H860kcXayxrzXCSj8stkASKZkecegNtxzWGLBHyzRnCwMhWwcnI1w5e4X8PLaWZTNe8ClfS02COEiz2DhkxJVJ1mxndZB8CvU9/t1jemnDOW54bzTPYOn686e2ZoAAjcAWA2LOrkqD6FGjb4swHPivtQ== 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 PA1PR04MB10502.eurprd04.prod.outlook.com (2603:10a6:102:44f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.24; Fri, 1 Nov 2024 21:05:02 +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.8093.027; Fri, 1 Nov 2024 21:05:02 +0000 From: Frank Li Date: Fri, 01 Nov 2024 17:04:39 -0400 Subject: [PATCH v4 1/2] PCI: Add enable_device() and disable_device() callbacks for bridges Message-Id: <20241101-imx95_lut-v4-1-0fdf9a2fe754@nxp.com> References: <20241101-imx95_lut-v4-0-0fdf9a2fe754@nxp.com> In-Reply-To: <20241101-imx95_lut-v4-0-0fdf9a2fe754@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=1730495090; l=3777; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=rJIqxvluRC/yMOR5R9EHOlh7W/U5VF9REJtvm0zbl1o=; b=pmY+M7aJ3wCP5C5dh8LDONwupDZUwfyckIugCkGGv6I/UrbabCgqsTxrlxV//qW3N7sqPIpOa pSbqeXRj7cfCm0AKXcyRewJeUFbp1iHuhqZTB52oA1r4CaoezDIBVP7 X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR13CA0092.namprd13.prod.outlook.com (2603:10b6:a03:2c5::7) 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_|PA1PR04MB10502:EE_ X-MS-Office365-Filtering-Correlation-Id: b2ca22a4-4fa8-41bb-c1de-08dcfab8da5e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|366016|376014|1800799024|7416014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?28jfwpibjBnoqWjyFpRbY5ID4TkFgj6?= =?utf-8?q?IVIaPXUN5LOvtMr9bz9vUr2efb3/sfyIlXMBzpBbdHkrcIYPb70zFWjXzOThoDagb?= =?utf-8?q?fERvbvj9xkxBiWgIQJMcyjdysk3SbpxMirlGj1BB2aopGrtjQ5ACaUjOkdZv+xCtw?= =?utf-8?q?7R5eXLKzYz1yp2ddDxZJM6eEk/URn7qvXfevLfL4ZlhBumS6MRcIBiw34SrIaJgPL?= =?utf-8?q?Zbc6c2688qvpUqP5oKNxKdob2S8AeuQt319xBt3iIurN+9LAl76KdUMKEDONe0e2m?= =?utf-8?q?cRB0KPWm7Io4aH3zZLolyNRXoOHtbei8H2bkWk0MniETL03OcDy5soG3Ki3aMsHPd?= =?utf-8?q?2WLGSo1vAuh4XwDOe27G9kZdndsFgF88DgiKd9lr7e3u2M01V56i61ozdWxsHHKAh?= =?utf-8?q?vVtpqRJiO0VejZ0mb87dnpuPsyOdgFGhpn4GxhohWHnsI0rLYwdKG3TK7gPmYCpMK?= =?utf-8?q?j9v9KXD3uAnYlXijobui8V0uHld/L4XrbbUWU4pFwDwGjbdf+ZlfvF/AwZ1fyZeyS?= =?utf-8?q?e+PBIcFSS4civjSCWxO/X9S3+GW6XVfXWUOVK0km2PlASBPB179I7cQJ+jqepc9dB?= =?utf-8?q?Bn4x1C5FgGrbWVZdM4jzvSps/cyu37aM2DfgtuD6AmTj3naQAbJVVEDBA0FhJeCxQ?= =?utf-8?q?E7eGnF+sRouEZU1T9NZ6XMuTinOJjfXiWnsozkphaDiBFMPPUVxkHP0a+/Px+KrFG?= =?utf-8?q?RcQpJjv9+/lDh5AFiisooPdnC3uiOo29O7EGWUyQhlCxpOOgfMi4K3azaKwXRc82V?= =?utf-8?q?ttFMciApLiRIrByTpHAOTV0a1XhOQB6fm69ZanWg3aqGahHhpNxDwn0kzK4/qh3lS?= =?utf-8?q?7r5iwXWiIcDkTxgFRwy0rI2VVbYqeKM6arCowWx8bCwDQ7MDxascDSpeHmL4aGFV+?= =?utf-8?q?J4ArMLWEBd2I3a/zvJP2sm536D6BIl76JMLhxz21OmQymfJvDPfy+iGKhRUV2Es77?= =?utf-8?q?dwTKMcyv48vq4dLgvy+nCd2k1kicsm8RXdmeqiM0CQLKL/dtjoFLRCsuECOFK5fjv?= =?utf-8?q?C3+1NknKf/AmCyNsxpBLx2VQ0pSn4S0cUXzjNoUlP4XNu1QemNrdfwAnwfUD8YqNw?= =?utf-8?q?R+Kckq1lUEGcdHX70PMtIuenTqkCooNUHLXYBCg8II5Yob5ovC5kgpY3x2aBK+EL7?= =?utf-8?q?67CCsLEM2SUmtGbuTqsk1zs198/ueiL0sF3jSi/72CFYueptz/RVPgWRc03IHZab1?= =?utf-8?q?hsdjgznhO7FYTbM2d/lkZPuzDwUBYyB9JPG9iDBbQSAUgAcsuw5kF3TWNfFW0STC5?= =?utf-8?q?j4vjL9BZC0T/9we+Y+ks++DoPay6BIq3KSkdcOfQ0DqB8MiTRbeMezlb5OQZgWLt1?= =?utf-8?q?edIWIsijt3bn?= 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)(52116014)(366016)(376014)(1800799024)(7416014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?Cb7owNJYtP4wpTVCUhbvBBidkkh2?= =?utf-8?q?9kbn6/eQR9Uz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TwcxzSII7v76s76IUKvDScnAZcxbCwcSPzspJFO4kKyTj2Ht3?= =?utf-8?q?QoHnbL6qq3d9M7cIzJnRddnTfv17ckLzToKjRfPPSBigi23WpoTZax7uaMSc7lnZs?= =?utf-8?q?ta814Pii7LoCpO+parYmYBavZxqg67m5QdQwTOX9MGxqJgIWVt0xaLznHNYgUV2YN?= =?utf-8?q?8Om1AKnu92USw6/NuAq30L3sdC24piSxrmUHwMNf0L30iH0o2uyjVDVeG9xPK0kyQ?= =?utf-8?q?O7GFp4l0ZIxqPkWgbVFPoYN07GkC+/PNiJ05iih70MToDthI3dim2Zu8UEri/yFbq?= =?utf-8?q?RebLFWD2yEpU64R3wn2DivLs/jsRISJtQm5XQ4xl/nBGxZEYcBrQITeQKR2cNnDVI?= =?utf-8?q?pgMusMYtsAeajneE5p4ft1+aFgPYzuehJH2jQxastaS0T4lMOnDwKzKXGA7Hcy7oE?= =?utf-8?q?I2N78OeD/8ehX6v3FjytHp9PQ967oPmvMNdyzw3tvSKEXWGiepZHzh1dOPpBYAX8p?= =?utf-8?q?Js1vYRcNnzKooQqN9FHXukjReut6X86BEHWQuLiKV/BGCxTqzgFd+SXPh+CJQPFZD?= =?utf-8?q?XxHvYdU+KTyrUD68uVnWwBCEvbWgI1G7Nn4LWIpDliw+vHeKrlgOrzK8iUwiz9B0L?= =?utf-8?q?5AcgZGYfaZgH?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: b2ca22a4-4fa8-41bb-c1de-08dcfab8da5e X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2024 21:05:02.5177 (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: kufqtSdoXTfMtXgghIeQW/SO/Gv5XUUXn1hzxveBWU7C8n4XuuSIoX5vsEAK+tej9rdXGoQr1TZqe9RaAXckdg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA1PR04MB10502 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. Acked-by: Bjorn Helgaas Signed-off-by: Frank Li 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 | 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..4699b1208d621 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 */