From nobody Tue Dec 16 11:45:40 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 29EA4243951; Wed, 9 Jul 2025 21:02:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752094971; cv=none; b=YWNfcfxXdnsYzFiBreqgUjETeyULtUFlWH0xXMqsvTbrJO9cKPRBooYPjIMV5p1+LBhuLmoCjTC5GnwOr6o/q0yf5GGHKJNeV4CWW0eYI3mLx/WBd5ur62ZJGqR9d9xeuq/su5FpRGVjmf86Ezo7ft4FDbh1K2GAYzKseSYnXtk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752094971; c=relaxed/simple; bh=2jVuJKlJwuE7wDUqRkDUnMsC8vHhgvzVYDcezCuIN/o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XjrzqBM58N2rnfGpfpJFvAgqmS3GEz1/w5ds2voK2dRHpO2P2dxjokRRkcaqXlexmxZL0rffB5QCo3sdountXLebJFlLsTRHtxmYKNEPIyUmMjyZf4NRavLH00y4KH08LA9A+QMTJdpEa9IfmCF3hn5oYniYS17R0c4vHTVaXms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hpeu3RT1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hpeu3RT1" Received: by smtp.kernel.org (Postfix) with ESMTPS id 896EBC4AF4D; Wed, 9 Jul 2025 21:02:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752094970; bh=2jVuJKlJwuE7wDUqRkDUnMsC8vHhgvzVYDcezCuIN/o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=hpeu3RT1fQr+ZGknkPUJAb0CIvU6TL4PoAAoWOoRcgSZZWt2WKAP/jVhpmmCyQqiJ o6/JANe6fNr2xs0YAs/WcM8UucCIUwqlUc5eWyeTHgtjmVTiPvs2k+tUkwJqpZOFp3 /69HJanBGIotCsSKb0ZwCdmOlSChHFGbLXjKIfPuWVGwKfpnT8is09imAHfhjeKNTo vn+GSrHMkmQ+NEkwh/ZBbUAgX3ZTMkp1eJhSev9+IVt0vf8JflJR6GCrTEiI+tOYUh e38UqRTciRoaNUK/oXU++UzaCBi73ZS5ymOPCf6DB5hmyDa1Vn1PcI6zLvxEfOpt5G 9aYV+1eGWbVQg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7ABAEC83F10; Wed, 9 Jul 2025 21:02:50 +0000 (UTC) From: Frank Li via B4 Relay Date: Wed, 09 Jul 2025 17:02:17 -0400 Subject: [PATCH v20 5/9] PCI: endpoint: Add pci_epf_align_inbound_addr() helper for address alignment Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250709-ep-msi-v20-5-43d56f9bd54a@nxp.com> References: <20250709-ep-msi-v20-0-43d56f9bd54a@nxp.com> In-Reply-To: <20250709-ep-msi-v20-0-43d56f9bd54a@nxp.com> To: Kishon Vijay Abraham I , "Rafael J. Wysocki" , Thomas Gleixner , Anup Patel , Kishon Vijay Abraham I , Marc Zyngier , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Kishon Vijay Abraham I , Bjorn Helgaas , Arnd Bergmann , Shuah Khan , Richard Zhu , Lucas Stach , Lorenzo Pieralisi , Rob Herring , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Krzysztof Kozlowski , Conor Dooley , Manivannan Sadhasivam , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= Cc: Niklas Cassel , dlemoal@kernel.org, jdmason@kudzu.us, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-kselftest@vger.kernel.org, imx@lists.linux.dev, devicetree@vger.kernel.org, Niklas Cassel , Frank Li X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1752094968; l=4622; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=PebSbCDfzLlvbjVW+lpBMsdu88x18IX5dRYDwgZD7aQ=; b=R3IHlef1Bbi/hWLCcUiLgZzHZUGpyb01O4MgGiwq/qDGUGyzZ1Ap0GvZtRyUX3OdhSfq69uIF yrunbnVfHTCB+oemotsLwfWWQ4Ve4X8EBQyWw5/sSpsaITlmfLVJUxp X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-Endpoint-Received: by B4 Relay for Frank.Li@nxp.com/20240130 with auth_id=121 X-Original-From: Frank Li Reply-To: Frank.Li@nxp.com From: Frank Li 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 --- Changes in V20 - update function kdoc comments. - 128 come from pci_epf_alloc_space() drivers/pci/endpoint/pci-epf-core.c Change from v15 to v16 - none Change from v14 to v15 - change out address type to dma_addr_t to fix below build issue | Reported-by: kernel test robot | Closes: https://lore.kernel.org/oe-kbuild-all/202502082311.G1hWGggF-lkp@i= ntel.com/ All errors (new ones prefixed by >>): drivers/pci/endpoint/functions/pci-epf-test.c: In function 'pci_epf_test= _enable_doorbell': >> drivers/pci/endpoint/functions/pci-epf-test.c:726:42: error: passing arg= ument 4 of 'pci_epf_align_inbound_addr' from incompatible pointer type [-We= rror=3Dincompatible-pointer-types] 726 | &epf_test->db_bar.phys_= addr, &offset); | ^~~~~~~~~~~~~~~~~~~~~~~= ~~~~ | | | dma_addr_t * {aka unsig= ned int *} In file included from include/linux/pci-epc.h:12, Change form v9 to v14 - 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 577a9e490115c9dd5d7fb624c4a3001f39b97e78..6d6911559780a003b8e81c629d7= 654d6959c7dcf 100644 --- a/drivers/pci/endpoint/pci-epf-core.c +++ b/drivers/pci/endpoint/pci-epf-core.c @@ -477,6 +477,50 @@ struct pci_epf *pci_epf_create(const char *name) } EXPORT_SYMBOL_GPL(pci_epf_create); =20 +/** + * pci_epf_align_inbound_addr() - Align the given address based on the BAR + * alignment requirement + * @epf: the EPF device + * @addr: inbound address to be aligned + * @bar: the BAR number corresponding to the given addr + * @base: base address matching the @bar alignment requirement. + * @off: offset to be added to the @base address. + * + * Helper function to align 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, dma_addr_t *base, size_t *off) +{ + const struct pci_epc_features *epc_features; + u64 align; + + if (!base || !off) + return -EINVAL; + + epc_features =3D 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 =3D epc_features->align; + align =3D align ? align : 128; + if (epc_features->bar[bar].type =3D=3D BAR_FIXED) + align =3D max(epc_features->bar[bar].fixed_size, align); + + *base =3D round_down(addr, align); + *off =3D 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 =3D to_pci_epf(dev); diff --git a/include/linux/pci-epf.h b/include/linux/pci-epf.h index a3b1a1d90d18356e8cda966c7004c3be93962a0d..0ca08f0d05d7439ef4043a77c20= 4062989a27bd9 100644 --- a/include/linux/pci-epf.h +++ b/include/linux/pci-epf.h @@ -241,6 +241,9 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t s= ize, enum pci_barno bar, enum pci_epc_interface_type type); void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno ba= r, enum pci_epc_interface_type type); + +int pci_epf_align_inbound_addr(struct pci_epf *epf, enum pci_barno bar, + u64 addr, dma_addr_t *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); --=20 2.34.1