From patchwork Sat Nov 16 14:40:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13877599 Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazon11013052.outbound.protection.outlook.com [52.101.67.52]) (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 C418985270; Sat, 16 Nov 2024 14:41:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.67.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731768075; cv=fail; b=JgLT0m4SANv6kJWtMbF0wEo0DVrS3jOfO8sliy1mDxCjuTRgbb8bs0f2oVapP4aEBXH2rEhpGOAyNPB5oMC8COxF+19uGhaLfY0zGDDaRWdzsqbVHUzb6AfN2HxP9NuMXN2em+965v8q9fUC1XiNqdc+kq/NN1N2S6/WAh0Tz1U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731768075; c=relaxed/simple; bh=ALBAmWL9QxkgiZp6flstYazhrERZ/ebCU5ovpzJ3ESM=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=dktGos92A77wETDufjEy6XskzYQehJgo6zFnb2/N9LJJb9lKk4bt/lthnao+htbRSQ4ZA5iHA901zsYcbUIaLBWA+MZwy8ehs9+nYQZXf53Xc1bISqxyvUEo8YT5CVcAom4vGLhA/Bxyes1YWgp/+RGnGkzlcu/fHpuAhzJSZGk= 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=GT4aJ7Zj; arc=fail smtp.client-ip=52.101.67.52 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="GT4aJ7Zj" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Lt6Kd3TCWAxZ7fa6SKW3oP2rXFY42zpkGwnlXRIredkQwyZSFep+IAd5cjgS+TKToKE06XVxvNqY4mg+bqyneMEkHqh+ysuy67yjs86Vh8JAohpolGNtvRJRPWTyXOAkwAYzeIf+0uSm0Sf76ZQZWJAGzVl+ONUUWM/1DB9hoP6xLJz3+jAosmGk31N0+vtey7Jg1qQ2y5cTpqbptc6uJU931r7i9KE53gNwjHKqubCk7Q90PPzvK7ZeTYB8KRSn8wznLcjO5iosEe8eotPCu45cq/ieGmIfV8hk3Z4cxBC6N5kamLyVyFvttSzjnF5FlTw+7vw5uxhju/EQgs/q0g== 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=MY5/Xe0JHtvYAV4/rZwMg3zPMgkyu1iopPbYp02fQO4=; b=tTL44kJ4POM5JNokvJfO7jRYotlEKAoFxAvAqjzj36SyLzr/9ULqfZAu1hfIA9TqGz2MLYYmcynuFfxk2iRbx/WC1crRYelvtXHt1JdoxFItYsGpPKzuC/4dhhGpy+Ud+cyP2DfjmfbqCQuZSHbp7nWlHm7Ow6hlB9lC+kpzyGt8PCKhluFC4Y7VrRWUAaLu75XRZSmj58ngK+LzfXKv/RTtYYdAFFm3tH4PLdNzFGKvFYTzbUht8ZgbrfXaxjKv3sQBzSNRV/AQ4Nd7wKF+BNKka9i/iyjIf/aXaOvcx+Ig13OeUu4cysduSRGZ1bYl9OXo2UjWWXOqHncJMulhaQ== 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=MY5/Xe0JHtvYAV4/rZwMg3zPMgkyu1iopPbYp02fQO4=; b=GT4aJ7ZjVt7VuNYzLZ1Q2yuOBd477oXfQY1XItrIoPO7K9jW9ggWwRa1n/k0QMjZVieX2VMfXRJnD3qTQDNmxXz7mv3KvZ2sKsMUhoAPTgeqB2YKOJ7wxfFryXTVQ93oLqiF6ueVfy4mmt4MqqihjE0CLlTnBPJJJsuPUTAR5mxUAf0GF0OKUoIBvTNpkFPmJEtccRMGMNymg+RSLGi8vuIEQTLuYLxI3oZPEjtZy116FNwEPkZ743TFr7rcpjWRatk35EK+JnuDdC7C1pWXQNLIs1ttSVUskWFNXSQ6ud5cc2utBoY5OEex3U1hCX2G5pnkutwxHtHE88kLWqSe2A== 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 AM8PR04MB7265.eurprd04.prod.outlook.com (2603:10a6:20b:1d8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.21; Sat, 16 Nov 2024 14:41: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.8158.017; Sat, 16 Nov 2024 14:41:12 +0000 From: Frank Li Date: Sat, 16 Nov 2024 09:40:43 -0500 Subject: [PATCH v8 3/6] PCI: endpoint: Add pci_epf_align_addr() helper for address alignment Message-Id: <20241116-ep-msi-v8-3-6f1f68ffd1bb@nxp.com> References: <20241116-ep-msi-v8-0-6f1f68ffd1bb@nxp.com> In-Reply-To: <20241116-ep-msi-v8-0-6f1f68ffd1bb@nxp.com> To: Manivannan Sadhasivam , =?utf-8?q?Krzy?= =?utf-8?q?sztof_Wilczy=C5=84ski?= , Kishon Vijay Abraham I , Bjorn Helgaas , Arnd Bergmann , Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, imx@lists.linux.dev, Niklas Cassel , dlemoal@kernel.org, maz@kernel.org, tglx@linutronix.de, jdmason@kudzu.us, Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1731768057; l=3556; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=ALBAmWL9QxkgiZp6flstYazhrERZ/ebCU5ovpzJ3ESM=; b=3llnIb+dDZT9nhTeO3V4fobFNL3Innm/Y9a784osQBC92UaQLwFT+sfqe/wq9QXW9m6CdK/yZ P9w6p3MbsNdBi3w4NL4lPEtLdmjpwEa3OzaDBship1VdShJ6XCXIBte X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR13CA0210.namprd13.prod.outlook.com (2603:10b6:a03:2c3::35) 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_|AM8PR04MB7265:EE_ X-MS-Office365-Filtering-Correlation-Id: 01f8ab64-7d1b-4892-4844-08dd064cb7b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|7416014|376014|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?wFlGqhcvbXh8B0tOCd4fbOPNBnwUvel?= =?utf-8?q?lmlZ0DEsR2kuZJ/CEk944fUn2LWV0Jew83EVessem3V9q7He14DrWIqK0EC0L1a6U?= =?utf-8?q?weYnGu1Sfj/nXpw8W4PP0owABBXaqFuy2xKomOFytwsYFgj851mCW/WwD+T5bTFNn?= =?utf-8?q?4MKouM4AAhZWbIEq7dISSc92tEnGsK92DbjOsTNxbT2ci+V7aJ98AJf2ywwnXO4TY?= =?utf-8?q?SQPYQOdN7D37PqtWipEwKJotjriTdWDpifKzEcZqTbzaB38O7rlzy4EhO4S79oM32?= =?utf-8?q?3tK6RrKevuTg/TAUSBOIj//wR6ZIpfr4e2ugG9Jv3tmygTV+NjOhd1+zXKvAzMUl6?= =?utf-8?q?74FwSjZUdBvZrq9TMhdDvGidPoYyoHvkAR+iUl+vchmtzCer7U1EZZrd7brMMPXoX?= =?utf-8?q?PbX6+3KSSXOL9hZn8OPq/PO0PV4xQMSmtWCJwQ7lMMyHxz8FXKDHNyqJpOuVBKJal?= =?utf-8?q?cJ4YzPu/+TacHnJVamg/OB3l95TmjGmOJXrM1E/WUMKj+Nt2L31F0+LwsNJysPE/s?= =?utf-8?q?zH2pB+iAOQOrrVyoo6asgsu+N6hZI7MlWnk6zmlDneW14NnWJ+NUR6bWB7VstLU04?= =?utf-8?q?P/LvK661FnqsGZcZDR1EBLZcahwZdDEms05b50obCxFqIG8bKlX4mjwn4eu2SYgis?= =?utf-8?q?SjPFF/L+FHjXHkXbIJaYGZpYteYOjEQBxLyzpGTf2sRk/AQaD75+iCMKWyx310XIv?= =?utf-8?q?C8BNTs8fPcQZ3wbhnHe8907DtVPXWcM3aP5pKV7bM3p3RzccupVT1N/jciYaLQJIw?= =?utf-8?q?3mZkUGZ/rOm8OXzr+ghhX1Y8d9275rGlo0Gv8YbZh1+Y1hYI0Xx3T9fcH/wOSa9IS?= =?utf-8?q?qNpfhUTF29rp0wcJgGgMiBQ4ULUSOToAIwpMY6jQBEhyfflg0aizOHzXkiZ8dU2zv?= =?utf-8?q?wDQRblTasYEGoTsvkprswC2NdjR9UFo6B3ip+RAhvypFupmBUXYRTJm8A1byS2YtZ?= =?utf-8?q?Foi5hVc3lueMgIfVYDf/IKx0ajEM4wNLkXdX0VJxU9rwJUbdJUa2coirKJ4TIz5Bb?= =?utf-8?q?EWw+ne4DGw9M1Sl/PryIs+1J7nOL1gfO0VPD7PSNvjmdmdcODuFE/WfabMfYCav+N?= =?utf-8?q?41Yi2TpCh+17tPnegSVKC3JMTL6ynDXA4WNrSThmgurdFroHLD5wqpeYzUdJENi6C?= =?utf-8?q?z6Cr995KNqEMoJ3ytDm/hUCeqN+GUGr5KkCzeuaI3/nOuOUJHPbyTcI9Ptc8X4PAo?= =?utf-8?q?Ms40Ri6MagIiVDJg8NViL+r8/2LPtz/RlCpfGHsS66AjLk+nwCJCEb385hLmRslxq?= =?utf-8?q?hi6hpbN3CsiTRmTXS0A4/aSmnddmikCPsxk0oqBPQqoGI3lGXRFxkZ6c=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)(7416014)(376014)(366016)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?brJ+FA8THI1xTig0I0c/ixQaIbyv?= =?utf-8?q?sATetzoaYNHRDDlr7HyTnt7kAXdRS52dfg+yo4ZL8kJzLvQPm7GtdviGrvAaMsn1N?= =?utf-8?q?sk6jkebcr8JdaAYK+yiA899oux99nPsENLcauZR9gQgOu7Zw0uu8PyK8qu+NuybXe?= =?utf-8?q?NHKVw2LBCZvhEYLUbItXb1nZ+op18qT8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deyc9Zzr8o9bFx87COh9MSYdntARjhX8Vzv?= =?utf-8?q?tMpCLo/EmadvxhGGPFRn0GZhC8wIhpEKcYwEo6jsNEEfWOZRgCm57NSqTOEEJ5eIr?= =?utf-8?q?Oir3TxRKq04t0ZgPuAbzZdh5voMo1dXqOmeETSyiJ2IBf0FR+5CE+cyetOF3OtrRU?= =?utf-8?q?OXXDY4Y0TBMXaMI5t/BorVYmKNcqCNzvy/YIkojbhgr6AOs/kc2AIR5HkMiW55VuI?= =?utf-8?q?NKMfyokzao7pefT9GbIa58tbgcTSgaaltcMluZbrBsMPNDyR9BfG1GExsjntLeVgt?= =?utf-8?q?XA4IA6L0bUyaxmA48VJeFmHRdvD/6Av57AFQwSSg/k6FJH40/g5+MZ3Od/7mC9pqz?= =?utf-8?q?aCOezeLpqbNNOWumGv8aiExI7wqn/BA5xfcl4/SL2REomeiVvy9IJCiJihQstT621?= =?utf-8?q?dkDifOycBpGH060yeoqLSWTBgfYpiFItpO1FXFUnx/ctOYuh88oSFy5I=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 01f8ab64-7d1b-4892-4844-08dd064cb7b2 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Nov 2024 14:41:12.6467 (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: jJhWBaPWvJiS1vDuCnL8fuwBUjpBXXys6jI3e9zDknpHKNhHq6vaZs+byhAmT2L69YIoMLlx2WkBkLhCZ/oQ3Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR04MB7265 Introduce the helper function pci_epf_align_addr() to adjust addresses according to PCI BAR alignment requirements, converting addresses into base and offset values. Signed-off-by: Frank Li --- change from v7 to v8 - change name to pci_epf_align_inbound_addr() - update comment said only need for memory, which not allocated by pci_epf_alloc_space(). change from v6 to v7 - new patch --- drivers/pci/endpoint/pci-epf-core.c | 45 +++++++++++++++++++++++++++++++++++++ include/linux/pci-epf.h | 14 ++++++++++++ 2 files changed, 59 insertions(+) diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c index 8fa2797d4169a..4dfc218ebe20b 100644 --- a/drivers/pci/endpoint/pci-epf-core.c +++ b/drivers/pci/endpoint/pci-epf-core.c @@ -464,6 +464,51 @@ struct pci_epf *pci_epf_create(const char *name) } EXPORT_SYMBOL_GPL(pci_epf_create); +/** + * pci_epf_align_inbound_addr() - Get base address and offset that match bar's + * alignment requirement + * @epf: the EPF device + * @addr: the address of the memory + * @bar: the BAR number corresponding to map addr + * @base: return base address, which match BAR's alignment requirement, nothing + * return if NULL + * @off: return offset, nothing return if NULL + * + * Helper function to convert input 'addr' to base and offset, which match + * BAR's alignment requirement. + * + * The pci_epf_alloc_space() function already accounts for alignment. This is + * primarily intended for use with other memory regions not allocated by + * pci_epf_alloc_space(), such as peripheral register spaces or the trigger + * address for a platform MSI controller. + */ +int pci_epf_align_inbound_addr(struct pci_epf *epf, enum pci_barno bar, + u64 addr, u64 *base, size_t *off) +{ + const struct pci_epc_features *epc_features; + u64 align; + + epc_features = pci_epc_get_features(epf->epc, epf->func_no, epf->vfunc_no); + if (!epc_features) { + dev_err(&epf->dev, "epc_features not implemented\n"); + return -EOPNOTSUPP; + } + + align = epc_features->align; + align = align ? align : 128; + if (epc_features->bar[bar].type == BAR_FIXED) + align = max(epc_features->bar[bar].fixed_size, align); + + if (base) + *base = round_down(addr, align); + + if (off) + *off = addr & (align - 1); + + return 0; +} +EXPORT_SYMBOL_GPL(pci_epf_align_inbound_addr); + static void pci_epf_dev_release(struct device *dev) { struct pci_epf *epf = to_pci_epf(dev); diff --git a/include/linux/pci-epf.h b/include/linux/pci-epf.h index 5374e6515ffa0..eff73ccb5e702 100644 --- a/include/linux/pci-epf.h +++ b/include/linux/pci-epf.h @@ -238,6 +238,20 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar, enum pci_epc_interface_type type); void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar, enum pci_epc_interface_type type); + +int pci_epf_align_inbound_addr(struct pci_epf *epf, enum pci_barno bar, + u64 addr, u64 *base, size_t *off); +static inline int pci_epf_align_inbound_addr_lo_hi(struct pci_epf *epf, enum pci_barno bar, + u32 low, u32 high, u64 *base, size_t *off) +{ + u64 addr = high; + + addr <<= 32; + addr |= low; + + return pci_epf_align_inbound_addr(epf, bar, addr, base, off); +} + int pci_epf_bind(struct pci_epf *epf); void pci_epf_unbind(struct pci_epf *epf); int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf);