From patchwork Wed Dec 4 23:25:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13894449 Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02on2041.outbound.protection.outlook.com [40.107.241.41]) (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 A3B2E206F26 for ; Wed, 4 Dec 2024 23:26:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.241.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733354794; cv=fail; b=SsHr1BVsK80DD99lpf+oXjsaSpVa47Wwdd4OPQ7uWIVSoCOYL8YAsMAdoQmTv85941VULaHY+YB+vYgrlHTUuZnN+8114V/DEYUtUo4ym0kWgKmLrLSk0RjCCJORAqQV7I5/PsWjBjwNNSgeNuX/IUgSb4f0gzfECm2OyscGd90= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733354794; c=relaxed/simple; bh=qzdj+nRdFv5Cm2vKc30lduKze81AuWoXfq4Swx2tuik=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=cPcQAwEzelKBYzXEj2SL9Zr3hTbkhkyOu55RK0e6Np/5w065pkXtrIXEmkz2OT9wjf3xa3CbI3kBWIy4zVpRUFLi2fDLd7qcv0oLaR7UDI+0ANYw/LonzvS030y0l94ppBSbqFIHUftczEf91tIESNNZ8fKp1DcOJOV65zG0vE0= 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=f62oKsyT; arc=fail smtp.client-ip=40.107.241.41 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="f62oKsyT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DZgVPnYiRS6TX+s41qr/sUOcnRwpCo2yRRYQSwqXGsuLBaX5UsMcSuNp7XK6i+IslaNENg4D6HRZQtI/RiPDRJa7dqElnxuXcXPIob1M1UBGE2iRNKV41CuiYazcVucMcJXuV1wyiSsX7E62b3mLSl3RYZ/3HNrYlHS97ND1Yap20uBTUJYYdnv2Kt7lMPLWlcETHmQhzFBDS7hem5vXZIEET7HX3xroFTgPBZF6aQiJLXBFOMkU1olayyE6N67zRCDkdw9cFBcCzO2MCRguRz6V36gVUT67GVJzR9S+ICMVOTVjdY4OnZRKT09XC9Nn836KO/AA/Yv4JlCiPpK7fA== 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=6a/cK/pYlQ0BGc5w9UFnxXw6jPGYAo0/YzxPKdtiIWw=; b=cHz7jZoD917t01rXgMwF/0wBVaGJH8Wedknh/zq9NxUW/vvQOe3YKK9FWK+1UPsh36Jym4ZZ4VandsbXUOmHyX704rExzn1X3YgEzNcNF8568VLp+e8a3cWr9WYdwEqxdIAk8k45DVUHwXA9JduX993Sfzo3OzMEvoZQIWaig7MeSmLy8Ml2wiVLIFlvTX3QaX9GIW6kxBpdFMuu4zErMmQ+Yzn4MzsAEINwJI4/8+sNSvHVBpodcCf5n7+AkU7ytexY0EnGVg0wQLQWFE9iyGyE0ETJIZY9uEgrcH/jScqA2wAA4BW3dd5lzPTZtI2AU0q+HEU5s41lmo0BaHJ1Sw== 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=6a/cK/pYlQ0BGc5w9UFnxXw6jPGYAo0/YzxPKdtiIWw=; b=f62oKsyTC1thk6ahi1q2cf2VchblHJzf30q3ros7cRWU7+EV7CS9+u0tc/wwz0n6CZrLNmWfJOItMrz4JugVMSFc0/rpgL9AIvvO6RZ9DPtTx6TrPF5e0FSh8NXnMBNfzgwvClugzDHYeuNxqL1hhOaNRoKVNWPgY2JQeUmz6YDhv3dSQd/gkRTZMUg360juLhbkxhk/XGpismtL8kMZdkzq5vKLO84d0sj8e/+EoqqAr59QIbqmmbfrAqzKOS8tLVyEYEMNwMcFn8XB2wxPnowqRRhzP87/a9AIa33s0jcRFptyu1OOuDqx8QoadMFWyM5hKJ6nTjra6wxOhOQEnQ== 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 DU4PR04MB11006.eurprd04.prod.outlook.com (2603:10a6:10:58a::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.11; Wed, 4 Dec 2024 23:26:31 +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.8230.010; Wed, 4 Dec 2024 23:26:31 +0000 From: Frank Li Date: Wed, 04 Dec 2024 18:25:54 -0500 Subject: [PATCH v10 4/7] PCI: endpoint: Add pci_epf_align_inbound_addr() helper for address alignment Message-Id: <20241204-ep-msi-v10-4-87c378dbcd6d@nxp.com> References: <20241204-ep-msi-v10-0-87c378dbcd6d@nxp.com> In-Reply-To: <20241204-ep-msi-v10-0-87c378dbcd6d@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 , "Rafael J. Wysocki" , Thomas Gleixner , Anup Patel , Marc Zyngier Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, imx@lists.linux.dev, Niklas Cassel , dlemoal@kernel.org, jdmason@kudzu.us, linux-arm-kernel@lists.infradead.org, Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1733354769; l=3395; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=qzdj+nRdFv5Cm2vKc30lduKze81AuWoXfq4Swx2tuik=; b=wmMgIa+byA4MKLIBaG/jM1vYCoDGfvaZW4tFZTjxycmT36AxTmpPrHFX7UNoGgyhEq1+UowaH LIWeZ4vsIHaDprF0TxJT9qWZu2IrRm8/GLT50gV8D36ReijDXJ3qlg0 X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR05CA0031.namprd05.prod.outlook.com (2603:10b6:a03:33f::6) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: imx@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|DU4PR04MB11006:EE_ X-MS-Office365-Filtering-Correlation-Id: ce7e50ed-3ae9-44f1-b032-08dd14bb15e8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|52116014|376014|7416014|1800799024|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?q?Rr00N2mL9mILI60LDPKXdODGQKlEuH8?= =?utf-8?q?lF1UBxZGZkI8tks7lF78KvxAQSFn8aokCPye4PD2LRFgVapelPyPrRP8hzBrYAVGY?= =?utf-8?q?Twh3f0HRvo8ggaGY7TAfh/GIysT1YTkfZm7l1C9wjCB8TGXEn3I5ULwrM3OK8YHNL?= =?utf-8?q?MPTSU2GrOp9oQF+A284aiYsOnIqF3Ngi/Y+NhHvO5d/GqwsSrT6GzWxUFuhmSCKGj?= =?utf-8?q?wQbycLBv/DqTvfv9r0C+9hzJ1t4x9wMgavPp4Q4SFFQfna9w+EEmEhvQvh0U3OZH7?= =?utf-8?q?45A7P34uJDHBjAGLMEbll3zfyMONO52/tpycxWdFGpc6YwaRR0yL0K/CONhFBVWfv?= =?utf-8?q?CLqCL+fdJto3imbEHr6liUSo597NiwMwAUnjxBm9ycI97fGieyYMd3m3RBsxbAjMy?= =?utf-8?q?0xzCcyF2uikgyhrdeQP2s1wf3D3htAJZozeCpFFDeSghmC35P79+wk8SU5r1M42iz?= =?utf-8?q?wAYMBmbPGS/IH1Rt/nesx65r3WOjoDVU1d1B8u9Wy6o0T5te5grQI2XK1AP7shO8L?= =?utf-8?q?d6uElxH1H/cRP1nJF4dSG2kZ/T0Yx6UiJd7Irf7xUPDhn4eLwKufrP1OOJgOT3co1?= =?utf-8?q?2+oTarnP4NrLIbW9csq0I4Se3xkGsUe2rJkNLIPIVHq+PkjYO9P8HiPF8cviRXtLO?= =?utf-8?q?oviTACoRlAvJoOoLD2U7lcQoICbr1WnkqKprQVcaYEUODzIfNKrNJrTzrQDGYy6G5?= =?utf-8?q?gIUBMaJC7qsmghdUmiJhrZkX2tgdeNOr7/3qeFhnvGUbo159h/J6HtALG6cbHFuOB?= =?utf-8?q?NLYKRf2j7yG5HTDKMx2anPvWtbrG35nDV4DNbZdEGMwruRBoJO6/+cHaTeG87Cqvd?= =?utf-8?q?XTwv3fXCKfFeUpw2hz4P7NvfDIuRMvchM+GxU84Szy6MBX5nJd2JNjU4IKYTRyndk?= =?utf-8?q?mTgQwVskENU7FfG86OA3AOuaZ44wBZl0guFicw7ueJDhxF5+QK/rJNGxD6x3aWE6E?= =?utf-8?q?S7IWdSZyHwYTT39ORoLZfzPNjLRIoHnCdbe9txh90Vp8aIg1CcAYctw/5suSMjtQO?= =?utf-8?q?zCdJo6H+djiW3gSoaMfPomCGBWnVUfaDyQC7AjjRv8m1fNFZyF+3553MzlMsRP7ND?= =?utf-8?q?obVhdwzEwFhZR/kMJ88gtao/hYgQ23uqNE328hXnpSt7mPgLAkTFSw+66Zn5Fokfj?= =?utf-8?q?hoKBJLgfDp7nd1F4NrM2/6gGqtDcsC5pHn8WFWtwTU4J0oVSR7fwsj6w7/ObXw5dI?= =?utf-8?q?0v7TaRMJEmo9X7kIQNwrL76JdRVCkcuAS8bB0Swa7iaVgbofG1p0qjC3SBioR4O4s?= =?utf-8?q?pvd6OIcwrCq+Dx8uruuhkyQFUhSZTm8B03bS3BFyIOGoJj6nHL85H3Yjv3sCG0BIz?= =?utf-8?q?Ou30x96S5aGjyPPtQLs28679FcmpvS76LbswbEgNqe9kCBj+k3ebzvM=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)(366016)(52116014)(376014)(7416014)(1800799024)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?4h9G2exD49sBB6F6B/q0rKodDfBF?= =?utf-8?q?4P8uBY0CocNp+ka9UyBgZHXVemb94tTOMdFjBUemir1uPmUnL/A9zUUCwPZK86WtQ?= =?utf-8?q?yt3DvYKQO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cLTq9NMlwO8H7NB4xM+SSSM/KwCgc7hgMpJGXjJpXL?= =?utf-8?q?trWc0H2dHzXLwriU5nsBecj2srbJkvW4MSAcJC8EAdWKroLTjf1RWwIm6rZScksDY?= =?utf-8?q?FXNcM4MjhKK1bHWTje1vsPOhC2JUX5ZuYr+pB/dqfZEDM2TaQaS4QVklLiPqu0W9r?= =?utf-8?q?0GUKAeYTcu8NKLEEliAuJ1Svih3b9Q0BXe2gg+CNucO4a7+UF2jK9logOQznI4YiR?= =?utf-8?q?l0vDaM1iA41ledVlQF/U9Rnvkrr7DmPoY1iq6FX6rQBMo/AfmZ7j9OcSmNY1jjQV5?= =?utf-8?q?/CK3bpSioPiT4kjQoCJiyumOOuqZMibS0wCmRS6dsN4xP2DNqtt7Dk4LpwwVL1luU?= =?utf-8?q?49g9RzbpD/FdceOwOKMo2V8MMCqk62n4xpm8onxQrdqy1w4exVeCsk8DVbdGmMrOB?= =?utf-8?q?78TY0aFRclQrQCG39Jk3CgK1ryrx4dG5w5XtLCrmLfHA+6C64+2kQHTmyN3pF0Ea1?= =?utf-8?q?j6vT3KC+jcGqutpGV/kuSDo+LvcRpOdGE5MTtASWCRnLNvcPategCpFx9TcPDtJV8?= =?utf-8?q?BOfPNv14qB94?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce7e50ed-3ae9-44f1-b032-08dd14bb15e8 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2024 23:26:31.6309 (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: mOb1dcFfzBpxo1jkUeY5hveA/RxReeDTJbqmfPUJI/oxcTkNwu+RC/oS+ijZX/1fy601NbTSf0kQltn+eYKhFQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU4PR04MB11006 Introduce the helper function pci_epf_align_inbound_addr() to adjust addresses according to PCI BAR alignment requirements, converting addresses into base and offset values. Signed-off-by: Frank Li --- Change form v9 to v10 - none change from v8 to v9 - pci_epf_align_inbound_addr(), base and off must be not NULL - rm pci_epf_align_inbound_addr_lo_hi() 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 | 44 +++++++++++++++++++++++++++++++++++++ include/linux/pci-epf.h | 3 +++ 2 files changed, 47 insertions(+) diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c index 8fa2797d4169a..d7a80f9c1e661 100644 --- a/drivers/pci/endpoint/pci-epf-core.c +++ b/drivers/pci/endpoint/pci-epf-core.c @@ -464,6 +464,50 @@ 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. + * @off: return offset. + * + * 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; + + if (!base || !off) + return -EINVAL; + + 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); + + *base = round_down(addr, align); + *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..2847d195433bf 100644 --- a/include/linux/pci-epf.h +++ b/include/linux/pci-epf.h @@ -238,6 +238,9 @@ 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); 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);