From patchwork Thu Nov 14 22:52:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13875707 Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazon11013009.outbound.protection.outlook.com [52.101.67.9]) (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 DDFC71B21BD; Thu, 14 Nov 2024 22:53:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.67.9 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731624787; cv=fail; b=G9dR7EoqQGghyII6G2tIBHXm/IRBjW4clhYmMUYBulciHwXFKNstrUNgMFRBQJKl6qjfdNcHdIA2s+GMssZuYZoMo6QseO8QpJt9UBom7IuhSvyzj2MA1XDIxURyo8Ekakwtgz72JxsbBCxNbS51T3bYTbB7lQi6YEv3q20qvC8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731624787; c=relaxed/simple; bh=W0/3suDS2X+PR81+KxkOxEg/dzb/QOb1xgTjwq/FHuI=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=fnnw3RErGN6hnD7S1/PjHQui43dMZZyoBGliRH/fJ3/ZwP/pei45vAbhafapqpTeZ0VV3WwfsX/nv1RgiBfMvYP+crf/QqCUiu9CquWYzjOQUQFqyTAbibjzCPsuFtCjXh8K+wzR34LvG4iUr6HshEGY+7hqbjjzvJPap/CpZ0Y= 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=ZOX/8pNl; arc=fail smtp.client-ip=52.101.67.9 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="ZOX/8pNl" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=I8HBaMTw/OzKG1jvGCi2ac8yGMVPuRCuTzRXvFJskwF260MEnb1j4JxXSzFdWy/nCVpeTG9azfW7c0FSjRhOr3fErRMbd2qUPue6enHam+5FQCh/Lfz5N/OpfVJVRmcNmKlHria9qdoOXrUmCc9kpb+3PqKBIzOuXQFvFMGy8aaB90+IJfeHCPAlonG/PM7o4pVE0t6bF+JzDrlZxLh0j0GMG590Jsd6XxYd6kGwsQXrwzO4OWKzo2J2ic9UO71kw8enMSSD1I2r5jmUdw2Y8shreOJTdzlBEMPPKbz3FBxnY/Vmig20tvbhqW8rgIf77ZJTjVEihbcAp5XBKPB0fA== 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=jlXLZ5x5NHDqWvGgBVnFd6Us3IzJiiZwh2cGtn9n1VI=; b=IABAhDDYt4lnlQWiokDVNV/WrtxQLxx4X58R5Qn2iQ/U+n5b1lLzkksCB5DqKJVTEX4zbLiAZLZ8W3HqL70WAp29yJx6Kv0OF9w3/zhCSjEShorktPAykTLR3FZU5tnE2JGBClLv+WpQMTYtGqSEa6jYtEmbbjctImdSBKglzEkA93zA6lmGpj0+U5+CyU0nE73UeWzodRFs1G9HWMQAG39ZY/NVERiy/2ZsK5YQgsrRoV+SrA7ylgOvvbObABdAXps6iVCmF9fDgVnOT0nL2rxH0r4GHbhrr1IBiwgmOztqohXuIctt5KxZgeiefCIbNnLlDPq/VzQEY1yogY+48A== 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=jlXLZ5x5NHDqWvGgBVnFd6Us3IzJiiZwh2cGtn9n1VI=; b=ZOX/8pNlL25L0j65I8JmSeyP1oChN/1ji5owb5iwKIUXUeNJGCFzP0TB79pQ1aO4TjtuVpUfgifNn2p2c+qof3WgrZfrn5qIxCJfQBaeQ8VoO/FCOs1QDSYZRsT1pt0cQEj4eJVObEPpj9eUz7Pv302CfvldtEjbM01OJgAnMFPFAqUJK25kg0znqASEH7xnrP6Juj9y6gaBhHhMo51awTkPhqUoOa0DGjEQL3MdgeRI9A9jFV5QAQr0Ep2PgXK6lcIPDY4EK4iuXSzbgCiViHFt/4ZLSp2yqAegpXfVwkVmZ2kjDwlV0dYD1Huxl6m2QxoRnt9i8cQp5ME0QMU1gg== 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 DUZPR04MB9967.eurprd04.prod.outlook.com (2603:10a6:10:4dd::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.17; Thu, 14 Nov 2024 22:53:03 +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.8137.027; Thu, 14 Nov 2024 22:53:03 +0000 From: Frank Li Date: Thu, 14 Nov 2024 17:52:39 -0500 Subject: [PATCH v7 3/6] PCI: endpoint: Add pci_epf_align_addr() helper for address alignment Message-Id: <20241114-ep-msi-v7-3-d4ac7aafbd2c@nxp.com> References: <20241114-ep-msi-v7-0-d4ac7aafbd2c@nxp.com> In-Reply-To: <20241114-ep-msi-v7-0-d4ac7aafbd2c@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=1731624768; l=3039; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=W0/3suDS2X+PR81+KxkOxEg/dzb/QOb1xgTjwq/FHuI=; b=CmGk9/KoFwJjo4n4AYZ5ddK4TCAISmSrzK3mH0rPCsmbJ7Vw9rX50/YwCUG/5kAYwaiugj2as fdXFX0Nr5PnDggkHNsGFbI6vtmt+fbnqzAyIahih/VWC8vSQTmkbUFz X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0PR03CA0166.namprd03.prod.outlook.com (2603:10b6:a03:338::21) 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_|DUZPR04MB9967:EE_ X-MS-Office365-Filtering-Correlation-Id: ac137464-518c-4b74-da2c-08dd04ff18a6 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?XBbZ6YvcDEsCQGXYKtTPmd2k+mKNMFn?= =?utf-8?q?9bnHLtHoS3OMwSMH2P0EziLQQgowo6MdTi8BAaNLUQ4T4R2kwgqjmBnRE5eCEdZv7?= =?utf-8?q?UX1cFOfKFOtZXP1XPKf9tw3SDxwB9OAPpxW8+41SZ4mU0STbZyi1jXVlvlkp7GTzF?= =?utf-8?q?9jPWAl7nxsyxfmVVW08/pukeWhzXmr5qyZRQ3RGlIZTD0pyFAG30wff7nQgsldKoD?= =?utf-8?q?kMi0mryD3uNDz3A+1kLNSCh5kgVuuRoOpU4ubA+Qjh+6TAkEVxa9Ntz9EgMX+Dpkd?= =?utf-8?q?6lyHLrxfvfFbGUqY02KmxvYG3k3aBvD9xQp5tTmHx9gsS3/gf4nqkI/5Y+nqCKND3?= =?utf-8?q?AAq9JSGB6hbZZBsJTX95mPN4Mv6RUayvBmPQkei87S1YSIwYjpPnKPV2t9sDLk6Ia?= =?utf-8?q?ClPI0Mv6wIx1Ew+ZgjoPd2KsDL6HoHQ6zvGall1fyrfeGNgubebC4TGYX4jjMLsWF?= =?utf-8?q?F/uUiJXjdTlPI4tx19cMk/bnia9Uzu7mkiPbTnh32PgwlMQUqT50/2ATqF4YYdfYM?= =?utf-8?q?BwHPpkCWxtzO6rXoMLcdAkcRLXRf2LKtR1bWTQp4lFKrZXuwRpxWk88iWpK10iyrz?= =?utf-8?q?QcOgjTTcOmg79T1HauJZbU48/REv9jJVv0B/aeNh4YzONyHSizlME9FmdwslKc6Qw?= =?utf-8?q?K0hQWevaO42vLmUSQVbY97kwGnZLBGwAzb8S/DeZNzVYKipWim+0dMBLM8+4MLLgs?= =?utf-8?q?HsdiqYcOvJoOLTWrCeKQJvXdiTnd2+asEwvLIzI7uVE9l5vbLuXFf7eJVaX1+Wwd7?= =?utf-8?q?pKpanq4rMazpcn5JJtw4i2PfOrroJGyelNfLO0jQHctvxuGSRxyfBT4NjANmiZt8T?= =?utf-8?q?qE0JlbepaChk0PyFC1C8obfKDr2X/qkpU5EYycfpmvYjW3vfVRjJm2iEGP1Fiw8e+?= =?utf-8?q?bp5BSrjNWCiL1uifJsHZp2YTQD9n/Is8IHO4VmFY4+m/hDdI+BOdj4VVLnPCJwYAC?= =?utf-8?q?+XWtOBodn3BblApYsJcQd7QXYIhYrPuBDIRv3qohhrTKa1ZppgIyh4Ur4Mr91A9nU?= =?utf-8?q?ls6ORXDbmiyqEb1mKP/+TUc7v2fA4VSS9wupmtPq1QAHPqD0ZnQnjAjldNfZqJdWn?= =?utf-8?q?FUPWxKGAJ99Vzwp3a1LmngXTxX7oLexuhw7fOmmN/d5N1W+1g/fiHqYOzS1fRJ2Os?= =?utf-8?q?JlbRpvB3T3bzRv1BSjG+YxC/tmOTXqi7yHiIy3PTIMTB1frYPRVdnkVzHLM2ETGHs?= =?utf-8?q?t2n0Jruwc5qfhGD+PZj8QBZiTZYq45x5TEKEeZ+eHeAYX4XZ4O5/gc/kdkAY3rc3k?= =?utf-8?q?hHSMcP8SQSbBb5dVkK2xUka5+RLHA19QCQoXxxY0Wo8CpaRX1cldrCe4=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?noF2B37YZgvVaffHV8xF1OXtdvsR?= =?utf-8?q?HR24Q6EqGZmtsjQN4ucG/XtstionF2yT3Gf7ot/0zyYTDu0lU4QVSFrNhC9EAdrQW?= =?utf-8?q?sEBQ+Q7GxCv/2nh9mJnB+T7PmvVlTpzlKU4NTX332uG3pdevbZmMO8CcHennKM9JV?= =?utf-8?q?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iO23isYRqxCH9oS+P4ZzqIOZF5hLOLTSMWYEC1TJCIkpdgALohcp5eaLO0?= =?utf-8?q?9IMiV1cNVK3rF0fLYFtDNU0QPrM26mlAVHX1m++uIcfvFVW89AIkFGxIHmwyGqrWf?= =?utf-8?q?3hUJyhsiWOjK1iApmUdj7y8jz9EkmzUwes3uMswB8NiFOoa1gB9GEMCK0g55ZoA9O?= =?utf-8?q?a3x1H4re0FYpbOKHlm7rHLprb9GirJD/TJaQaw37ddyCi0zyrwnMuttMZwYc6Unbo?= =?utf-8?q?yH+Kc70E3l21FFZHLeDUVPvPTAliNJhK5drqiPRBjWnrxNJXuaXtv1esudGTB89n/?= =?utf-8?q?S63+E3dCjrHuQ3rBEAmU4PgT5DXbjgm2TsnAE3oQsFrYxsp2vf8bHW6k1sBDjbLyb?= =?utf-8?q?Mb1LUoJKYVtVHyf2Nx17KXq+cH9dtD5ZwfMxJd2/wzSSRAW5x7aPHOI8wkIqt0ERi?= =?utf-8?q?kgFbageBCZ9PU0U5hYnA/KyNCpuiWvm8ySazRQpUArxQp3cuvOJ4zp2I=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ac137464-518c-4b74-da2c-08dd04ff18a6 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Nov 2024 22:53:03.4053 (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: eTGBBSplcryIC1HMs5mNKXcuLd5/dPYTzyA8lUDtilA3NVu2Olygw0363iYd+HcJ0O7PYMmqGUFmvbvQmT3Dhw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DUZPR04MB9967 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 v6 to v7 - new patch --- drivers/pci/endpoint/pci-epf-core.c | 39 +++++++++++++++++++++++++++++++++++++ include/linux/pci-epf.h | 13 +++++++++++++ 2 files changed, 52 insertions(+) diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c index 8fa2797d4169a..a3f172cc786e9 100644 --- a/drivers/pci/endpoint/pci-epf-core.c +++ b/drivers/pci/endpoint/pci-epf-core.c @@ -464,6 +464,45 @@ struct pci_epf *pci_epf_create(const char *name) } EXPORT_SYMBOL_GPL(pci_epf_create); +/** + * pci_epf_align_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. + */ +int pci_epf_align_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_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..20f4f31ba9b36 100644 --- a/include/linux/pci-epf.h +++ b/include/linux/pci-epf.h @@ -238,6 +238,19 @@ 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_addr(struct pci_epf *epf, enum pci_barno bar, u64 addr, u64 *base, size_t *off); +static inline int pci_epf_align_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_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);