From patchwork Wed Dec 11 20:57:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13904174 Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03on2045.outbound.protection.outlook.com [40.107.103.45]) (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 5F6CE23FD3C for ; Wed, 11 Dec 2024 20:58:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.103.45 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733950698; cv=fail; b=C7+XrRkz/HrmykarY/oAvoAGkk87Z8jdPWqSENlNstQgdHq/745YSfYy4vIYwIcP/c0q4Ig2d9KHOg3y37s6fYDJyawNrki5jTt6luZ15kzNtcQMZMKE6j7AxD3PlmO5v3tyMZIx6HyPhnJUG2/gHD9FT0p95k3XVyG5WKuo1CI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733950698; c=relaxed/simple; bh=ATnxL3MtlZvwsNg/rnjJY7VKdXtCjc5uSP/3WxbH5ls=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=l36GhFkdGkMv9Qewq5m0+PRKvcJ2BzdxTmkF+Yv8iTFMnV/2fjQ7uh4+8exjLFUpe2JCEgBhmRZzsJA7BLDemLm8glJd2GNATP9BPpxC31+pi0rz4EokJfWAtPpr9e0Bt5Vqx4S8KJE4lY4BYNFTSAkH/QmMMLbLtevJ/f1VsUg= 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=KrEXKY/O; arc=fail smtp.client-ip=40.107.103.45 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="KrEXKY/O" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=sBwhkYFtls6et2fxQGCKgJ6tE6dBACoousVa9353oneihsatBNMggzN4Yu55IJE4+r+7C44sQwZHmwcmivN1ntRB5S+3xBcEZmA930yQRMejyICMHJfSzxGqrShTeS19/rYW4DvlwRhodvxsK9Z+A8Zu5ivu0RHt8y8JS9ETPCaUgPht4NKUd2biIQZW8ENFpDtQb/wRQ8e9RIG5BOEQjGJYir4EQ0cEhFBTlFKyDgYd5e0nzNbgiH1wltoxIhlPu/tOxQuIwSZNwdLlJ2mOCPA3o23MclHPrBTw9IiOzZ1SgYvKH0m7qjr3CXQMlMGGHMLVZtZkYdFrvhyLQEf3uA== 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=e8pqhqg1rJbKFFLKPcwz4vZVPUqET3tTJLjryUl1peg=; b=fhifroqeAuRpCU8mL8ls0AQHSVMxuHtbVYDBFra4nIoEzowh6VOkslVGKuZDrIt6AUhJyMZW6wsMKGcFpTsD6RjhSp2cFezupepf4GRCu3lg4RfxT5H0N1zw+6Odzo2rq0o7cDfcsyr09F1JH7b7Wf5dzQGSvftqHsGn2hWf/MZ+9x7Bwdc/cG0GLXDpcw9sQwfdC417L/3tF3RFLaEF4SkY8QsyQMsA4VJZxmSwywDOqq4qHOKvIFgEx9JSLRumRydjhPzgJscH4cgNYTctlhoIliSNkCDDgNsSrhMCvIf/V0GxJ5beE7yQ5MG+SlKn927TZItmzRx60T/a4sKXHQ== 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=e8pqhqg1rJbKFFLKPcwz4vZVPUqET3tTJLjryUl1peg=; b=KrEXKY/OpaeShP/6JcshukbMIzyF0H3Hra6ZbvNNj9hEyNDBMCxCMsjj1RFsSYWMtikdKAG6xsKgKZ4HEac7NJLIfPFMtIrcmMcv7ypPcq/bJCZ4liL7TyXic4LiB9xQJsC5qHbS0pT2ZlWgkDrdfjGeEuq4KyIPlfDB6mZG3FiDa3WGdd1fqgROB5XGMUmh9h3cysT4l7+OtBITrnrlvnnpCiPBsoN7eW04iExWx8lbOTY6t9SpKIgsxMwnlmjig5MvQVPqeKua+1JZOZKLos1LBMBNcp/AQC0Uk0z3QER2Y2JHmcptMvL9VlNSBW656MPrapnj2mWeLqvU+0DPAw== 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 AM0PR04MB7139.eurprd04.prod.outlook.com (2603:10a6:208:19d::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.16; Wed, 11 Dec 2024 20:58:13 +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, 11 Dec 2024 20:58:13 +0000 From: Frank Li Date: Wed, 11 Dec 2024 15:57:34 -0500 Subject: [PATCH v12 6/9] PCI: endpoint: Add pci_epf_align_inbound_addr() helper for address alignment Message-Id: <20241211-ep-msi-v12-6-33d4532fa520@nxp.com> References: <20241211-ep-msi-v12-0-33d4532fa520@nxp.com> In-Reply-To: <20241211-ep-msi-v12-0-33d4532fa520@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=1733950663; l=3395; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=ATnxL3MtlZvwsNg/rnjJY7VKdXtCjc5uSP/3WxbH5ls=; b=HXiGHHTwwx7E5UQgOBkgSKnHF98cvOYYzQEFoScg0AbvTUrMm41/UCTDdaHC0QMJITRoJTUlU Jcjjz7xPgelCGAH0nzFH2nlAC1JVQzGmrf2G5Kivg5JSE5W+gkqaGCH X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR01CA0037.prod.exchangelabs.com (2603:10b6:a03:94::14) 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_|AM0PR04MB7139:EE_ X-MS-Office365-Filtering-Correlation-Id: dc2c9820-8dee-4915-0731-08dd1a268735 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|1800799024|7416014|366016|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?B8d1gNJFR2Ebs7VTt1DZVNVP8CP379H?= =?utf-8?q?0XTAlJUIu1LEHZHbSoQEeSn6VWoK4TSMbjqXwBUyHFpHR7zIoIUYhg1M5qubOnex8?= =?utf-8?q?oui0w1OY0kar32u2myYvnQGzY8pXsFmpOSTunPLNUGF9OYiEUzpTqTaIQKJ1Uu+b8?= =?utf-8?q?mPq7L3wNXVJFppQQezaAJtQahLZLs7sYRCxT3SVi9L5ttGo6oV+NY33CADuR3iC2H?= =?utf-8?q?MshckJMKEiLrnSy6Zp1thx71RklLreBBGPAqZGJo81mh5RtYyMwmkeANVR7W0np5O?= =?utf-8?q?90Z6CvoW0EJ60m3VCM3fMBgD7V4D9NOlMPV+yQm84441d27wlJNHVY+XSCshc4wQ8?= =?utf-8?q?n4Ay12B9Bc9rDXx7TRYATCe4VxDbpQ2pHlOAN5QXIFptXflwM7boSm+YAAIYcHz+9?= =?utf-8?q?YUFvvvVBZs3X0P+HI4jPZKFQugAs6IdHn1q9d0mNkMhog+w1l8lDkJwPvecBo8Q9n?= =?utf-8?q?oCzOD+qSewco5o66t23otMUr3E22yMdp0FkOoVUbbT+eVffVTjEqNWJP84PGLyo2N?= =?utf-8?q?1hJ1rgUhRtdCXAPY1fa+NzKYGvCC+Yjff91jxh//NYPsuuQRHj3StNJ2T/BcHcb02?= =?utf-8?q?G4W6j1TDFFAGxbaPx3y8T9UGj0HFh+Rg4DZvyZuFTVZzvo4cCigo5U9HhHIa0DXc5?= =?utf-8?q?VKiEjqQ7DTca666p54K1PC+1+C2Op6fzCxw/kWqKhq0OJdI3xjIkYVZVNYHYXYH9H?= =?utf-8?q?87wayOR/dEOxKmHCZVvJ0bkDUUauyx3+eGvPdRGXZ1VSJ54PRFcRiSVavwe2NA42J?= =?utf-8?q?9b7n27IA8883OAovU4P19Lvek66G0vRCqtrZQOOAUjAjP9v+lpsDfLXKlE7ygGs/d?= =?utf-8?q?M0h5BMEroMCiqHfJUATGRLo5j4Amm9L5TYf7iTNOO2i1HdjA0b9zgzUUb4q9UXXy2?= =?utf-8?q?ERsD2wwPTANh6BiJIp8mxi3fOKoWrdzUmkd3IMqjSquElClNUjegz1BqA7LhErgQk?= =?utf-8?q?K34spGJ67Lwd1nnlwxyFzuCkOtQtq6fBZlXQrjtni+ZF6DoE64Df0QNHIHBackrLh?= =?utf-8?q?pl+zoCiaNGfihxl1c+YkMhep1wuEUl/1QW0V8lvkwguOgIMAJf5DmmAeIl7XbtTNl?= =?utf-8?q?Etf0Lc5T7sfe4FAkafYU/nU44f8Drksvs9ZIRrXfz6yfbuaXTKhoKRPLQ3Es+8+r+?= =?utf-8?q?KAXSm1LtTkENazDJtMhei6Bi4CMuIbHb+W9h4qtN6u7Un1Zmyr6E4yPgNfYPPl8VP?= =?utf-8?q?mXjQsTp3WLiSkRYghH4SN3qE8bn4yXpuzxZXzAwsBFR/LXk9iCudnG+E2ZhdwAxev?= =?utf-8?q?x1QdycGaGaCNUzGoBHNTZ/ruuUPKkbdH+3qw+4nB+MC68AAtncPZbmDee4movUnMt?= =?utf-8?q?R336rKYd2ZT9vfn1j3Yj1qmVDS2nsdsohbZbs5GmTMU2g/VJmt8rbTM=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)(52116014)(376014)(1800799024)(7416014)(366016)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?g7KE0Uiztm9KSL3xz0kwymkD+Fpk?= =?utf-8?q?akVOf6jhWe+mqqSRzK45STzHRN9tsESozZzQJ1l2mcZCYRGYJFQG+bmlutVCSrh7c?= =?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cdZXUO3wl52BtSFv+sXZQi7Px4Ai4jmhDEv6hnzFouiJMBl54jUS2UqKX?= =?utf-8?q?NP+HBGLut4OUORTMPN1r3sS5TbAYNhwCFuj5kj6NHX4Sj3NYnGK7tyXCeYDwNJMwG?= =?utf-8?q?76vo5Mg34dW5oXedOs93HCkMFcaWiq+QPntqfhDBMgfAI9pC6TAPdIokj06CZWsid?= =?utf-8?q?V3D3P8fCBaNi0i9YICNpIaz5YAcTEeSShj5wpPk0s9qyMZD7oEi2UoFF/BfOVFQyP?= =?utf-8?q?NQqWrdDPnk9eQQLdp7A38vi2VCABN9oNs/KqDrVSQHsneoB3g1y6lgQV9istEqSrr?= =?utf-8?q?0L7ajDFamRHBMosojVT9eMzi09PrO7wjQ8tOE+HZmmorUYvw6fnGiLDx+IF0fHwRB?= =?utf-8?q?1n2yo7Yk2iBMvJ1oiZBwC0QXXASZ9JfL9N7RspZU+g/a0BagEWEFJhDoq1ehIiws9?= =?utf-8?q?29S81jwhfrraIu17ZWrZhHeW3osNPIK/gxUX+EGA7RYXZiiESntSsSZgwOMSTWFyB?= =?utf-8?q?9fDaZUqVaqxdq73IiQK505DJ0D8h6ZM29UhegjWnmTLuSw9hmbke6nPPFoQLPj8ws?= =?utf-8?q?nGa1ncprWdgE?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: dc2c9820-8dee-4915-0731-08dd1a268735 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Dec 2024 20:58:13.7067 (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: AZeV8ebavj4SakSMGU0C416Fyl03txeHhFu8lB3d58d48Ex6qtRTBWmwEz5DbI574GNdaW8lD9QlzW2Q5qstxA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB7139 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 v12 - 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);