From patchwork Mon Dec 9 17:48:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13900152 Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2085.outbound.protection.outlook.com [40.107.247.85]) (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 53D151C5CD6 for ; Mon, 9 Dec 2024 17:48:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.247.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733766535; cv=fail; b=jy3AaE2/jXnsyEpFN0er0CUIMGHrt/fERvSlKPiu6b1NxjXbJueSP45AXGe3714TxI2xXDmfLTklcscKMXt2hnSTwsrJkP4j0EcSZCNfUJfAKF7+f5+HygXi6AtegsF4y921T+GHZCjmhQEaBpytJN8iOUUduHpgpNL7NIwieJE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733766535; c=relaxed/simple; bh=3ddx5i2vZgkRmKAKHbO1+MH+deLur5mXWHjfPbO8T5c=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=awXExIY2zb8pOZMGXrAAMHqnoKMOjrpERHOZIPat/iJ27ls4eRQxc9fe/Ia+d0MqpWteFaBAu0i3BMWjmM2iYTJ6IDGx8848WgrgBRDR126PM2gpSW/c5U5fhmI1X3UZ28gpfDrA9YM0xJ+mPULmi7cDICiKrcSthfd+VDj31yo= 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=ihcBrur0; arc=fail smtp.client-ip=40.107.247.85 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="ihcBrur0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=bGMdOGQh3QVAzHZyob+Pd8sv99go5CeZJd97QtEwtX/ByZuHJOvgam44OHmKcATl/O+ep2OuSlevdkrxXLQPfrULFl2NJHMOaSS0Cr+HSjVLYtdnIeJKVQH4TVXkl/Gne3nlgx/2CUCV3C+H26sVC5mz+y0k7bJixIP3tw2CiPofvQ5AYE+E32fPwRzAcIOTQ7Lq7VrlOaixxP7XJVUNj0+MS6jNXyOh1Wv7V1jkPdH3wslJLPjxJQkP+AJQSz/adBDWVbyhp6pTQpnHBy+caLJKwgGSKBydNoMhA6n5LdPuhrm1zU8V2eMe4q1V/BSqehv2ZI9ZsRF+UrtGx68yzg== 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=oszU2qojRF6w8uj4Xk3VbRlVoS3ouEiYM+63L8FZlXI=; b=cIzrAoJZ9/Y4h8E7qXWQDtLxHUXU5r0FjvngYtRpddwjwn3zkN4VKPiyg1J7pWEZL5UgfvRkZhmFgYEd+scoChgz2pc29mpYtkRBohOXD08KUaFG1S3BVGBebhHn25NjXE9gAd4yObrBH5GH6hOElZ6plQ1U2cosORmzrEwWUVAXsPFhX0+KS3M3d2EtAlYT7qhpCdIVfaaI64Z45oj6iQe8mcnVTSxpogs7Y1Gsv9CwKOsDSNzpmHoJOfeQPgpALjNEShq0IIAvgYZlpCRgStr6Nhll1kqgY/NqOyP/IJcT7kEdxBxK/epPe4HWpaLTDk6Nob+9zMTHrR0deFJejg== 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=oszU2qojRF6w8uj4Xk3VbRlVoS3ouEiYM+63L8FZlXI=; b=ihcBrur0Elz0h9YVqXoYt9DBdJ+r7Qv4TwkeVw4R4MUg/CvaA2QPiL8JqXVO1v6OMWzGTKF2WJV7J3VFDpY6/EJNyJOCJ+O3AT/YvZJQXMHfLOLhd7reRLVddokrK+8oHIwkx81gEMaoDAGsS2L4D5bwlmLlaIkwRGW/1Li90mGE8H9RqhpsMj2Maim0e/u8kowEKc4aSYHCif6/T/T8SuATyaC6vcoOT7tCS1M144jLYFbCRYlUxsErO+8WE6lv2AuXVchvUZnJqSZilVoJrAHzKH3qyrzjRCiAJgQ9KK1HDP22tmipetGYeJoC6QzGqzYXwIhAvRtOf7Jnl0qUZw== 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 GVXPR04MB11017.eurprd04.prod.outlook.com (2603:10a6:150:21c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Mon, 9 Dec 2024 17:48:52 +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; Mon, 9 Dec 2024 17:48:52 +0000 From: Frank Li Date: Mon, 09 Dec 2024 12:48:17 -0500 Subject: [PATCH v11 4/7] PCI: endpoint: Add pci_epf_align_inbound_addr() helper for address alignment Message-Id: <20241209-ep-msi-v11-4-7434fa8397bd@nxp.com> References: <20241209-ep-msi-v11-0-7434fa8397bd@nxp.com> In-Reply-To: <20241209-ep-msi-v11-0-7434fa8397bd@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=1733766511; l=3395; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=3ddx5i2vZgkRmKAKHbO1+MH+deLur5mXWHjfPbO8T5c=; b=tPnkfLpbDfNGLxvDO7/ZFgSOGa8rVpKvB23zBdMz+eSYMSp7n7eemg5kEhPD+dtgDdCiAhIET MWuKIl778elCnLROlEm7vDF1f87MX9kDeORbql3et85CqncE7vLePsR X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BY5PR17CA0070.namprd17.prod.outlook.com (2603:10b6:a03:167::47) 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_|GVXPR04MB11017:EE_ X-MS-Office365-Filtering-Correlation-Id: d2097a4b-1a1c-416d-80b2-08dd1879be67 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|366016|1800799024|7416014|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?q?NoRGPU0WFxQihs3rjE4jCQcgr3O26vU?= =?utf-8?q?NS44KJdG5o/vzShZ3S8D4njSgjmMxfcPs5BiPQWZ00acgOjsF7RZSryVvoFF5GUl2?= =?utf-8?q?6L8axe2BD7pxy0NaUgONmy9m2gezz8D0iFvCgWap6gXGb/bC25IyZ9j/1Qisivnh5?= =?utf-8?q?fCqsmZ9yPF6CqLn2ZQIRCjY/dFkrFeZB3zjxkDflGFCU+gDvUPJApSoHs7/qcKcAh?= =?utf-8?q?odKA08/mfP/tI+cJ28SrBJz8jEBl25VIGtbuoqRdXtqjRpAUurpBdwB8kSQbBKP1z?= =?utf-8?q?i6oXvIW+AlbUfDZsqWd21lCXy5ywHJ8lAjv+bFY+6GuradJO9CH3BGcPWP8Tfuc0R?= =?utf-8?q?heWtabfFjWE1KyR31muJOzKqYLCKeofIWsyBxXOp0PnsVqvPkMqmYP4D7Fpwd8K1C?= =?utf-8?q?5H17Np9Ligau5lmKDe6sPDZE8lhGw7MZoFDXf7EIokrbJg2mOqW8azRAag7vagLK1?= =?utf-8?q?4RxDvwWELfU5FlFTFpiQhBL/2camoj9iTOKrb+Dsz3VxFvtIRyWGW/Nylga7QIxPS?= =?utf-8?q?GqKh2jDvETzApPqsZRc+9KapSXwUPTnbTS0+pp/tuKW1qb/hQBciL2NqQZkV3wU4b?= =?utf-8?q?YNjbzQALw4S/jK2/g7fzafOVk0Y0Me5fapqlT/k4iTxzw/UZ0U8Kdj2znEZPOhdog?= =?utf-8?q?3biURfo/P2CRIKhuTpDZMUvckUHkVigtcPDcBiL6OOAjl/93CJeETYJfsFdTks0ns?= =?utf-8?q?ZU6YeFKqfGI9LAmBlWkazRtM1Q8Kmojyuste1tRwPHT2CT4vN6+ZjH+iy9HHIXbKv?= =?utf-8?q?MsCSWUjDzANwXsutBGp9RCF9hKNfysxEPrJw7bdIPVmSZNgF32BQPV/+H6YGvOQmP?= =?utf-8?q?KvBwEFk7fkAbtM6J1C3NNuPT4fKt15cTbs+nNATOTEC2Qy5A+M+zAwFISKtoYx4nq?= =?utf-8?q?STNnS+IvxtYzSIATk3IajFUFyBxR6ezpvg88jyF5hbJCSzfNHNb25w+PrfpVCZER/?= =?utf-8?q?XQ+oFfIAYq5fbDp9meXzPzWaHRnnj9CMLMX/aH9Fm+A9iYpSN154MwCe9TL//4OcQ?= =?utf-8?q?KcMtD0SacChv92Ep6Ak2kS/VbcqjHzFzSj/Yxyh8Ni/hc6Sopx3hf4geqsw5QHdWi?= =?utf-8?q?rH+vYGqEpN3XZ24RW2qL1Bv6gZj7/NHqBxk318K3LofN+L9yxpbrNQ0vIO683rgJ0?= =?utf-8?q?+78E1bO3CLbNPacxjhnz9EML87EnQGsqXu601Ambk+7H5OKh7ODiN1fI19IDkMxr7?= =?utf-8?q?kt+G08ovMgUnxqpHiyydoWLnDb7/xZeoqotWdt3Fbv9EH5VrNEod6wCn5X2c8qS5N?= =?utf-8?q?cqF0dsbV6lrg9IPlHJPtbDcmhAmtI/YOoCp91diCC6J7J7HvlDM0VaVmq5Y7zdFK2?= =?utf-8?q?LbOWx+PcmSFXgt4GOQ9dC3QqGy83y0p9TusMR33HLHgX618trhOOxEw=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)(366016)(1800799024)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?gSgjtQtwUGBNlA3Fzhs3jvsFN+3W?= =?utf-8?q?vqDsp/X67UfNQKTq34Nl/qi6DYEOoyFRw6YPLz9LgOaHcErpvNARsLKNgeh681ntv?= =?utf-8?q?IbJV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vyed3yrFxYBKzVX+WI5LMWVKPKLcytSQVimB+Vzvcug4CoBL6jHwHfSLWKcYo?= =?utf-8?q?jU5MEMOm85SVrhBiOopyrI316Gx0aNIdIbQTHImWVnUGxnyB+trf4HC3lWyiELNu0?= =?utf-8?q?VIGzuEeE0DIx8KH2K61dbPhj+G63Qiyn0LFMgRAv1xF7EPH3KFx0+kEF0h55YdSS8?= =?utf-8?q?DWDhxVDLwDMQoWlC0O/CFFyqJMdA+Rv1SnPC6BFwuQ42Qxp0aSDChKhFv4hyjMJuN?= =?utf-8?q?DLcUulUdm2T48bKbzOiVG/m0c/jvratiX6iC9PUhrUgkg4Ioy6t5EQFbQazhessZA?= =?utf-8?q?HhHiG/Lqlsq0/IZVbFQBRGnA4/ijGQX48R1WIRsmgBAQl+3aHBgQDdj2fjH9kAKts?= =?utf-8?q?lHEf0YJ2PEKW6UtRjpDgS+chGIv4oUMgfqV2+H4yyq2jgaopyPvdY6E8hMpXv9YQX?= =?utf-8?q?bJUI3BM1yxUqn1n7q8MoYPDg7wcA6A18+WUbJVXR16l634oTXuSkk7cRK2k0P4ICT?= =?utf-8?q?mIc923hyf3p53J8qVeYGXF4gX6Hb7PG6RFT6UTDGhwe2ZmF93VoWqI6w=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2097a4b-1a1c-416d-80b2-08dd1879be67 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2024 17:48:52.2069 (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: Aq/WU8doqzrAWUXlIeg6dX4D8EhUDXlStyl70wrauB0XI+KxtDRUVQJ2tExINOPkSN18pqzjUvYfC5f6RYJMfg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXPR04MB11017 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 v11 - 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);