From patchwork Tue Dec 3 20:36:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13892932 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 2B89D1CEEA8; Tue, 3 Dec 2024 20:37:24 +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=1733258247; cv=fail; b=eqhbCeTcU+0irtizxxDvJZH76PRoCAFeMspJwrOcDazbyrLCfalLtwPPtJikRsCja+mWb28vs7rhw2H5gbZNZPQx8/cav/C+WfyPXJNI9ILhzx9WqSqgm0m1kALxwaLgxYEIdPN+p4uDQHBdHD9PjzpAbjXqtdlEaY9XoCXMnAk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733258247; c=relaxed/simple; bh=BqHEt8m1nlQE+CROgZ5Z9jf9BfRy50ieLQvI2rOkvkU=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=RzlAM8XjbZ/6ax9rAKwARm9Fi9ScH9xH/xRotgvsdUdfEfO7u0OwoiwQEn/s3KmHZWDTklmWsJinx2MjB02GlS0McIhsFjcxJ+KsGCTcFtF80c6YTon+dhUCWrXmLI2VaqiV4wikL8s1NpAkYIecRBnbeaRPkX1hsv8yoS95UzE= 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=Rbl3AUyT; 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="Rbl3AUyT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hsXuTdMnLzx4LveSjw5SPLaABPOHTcGAS2rNFdrgsyGf10T+eZUyjUsbOQ55bvEsrS9A/txaIzJRu9hniaTn8c1ccy4LOfhaXAVY4+Y5YO7S8gL/1WlliFIhNH5Fa6KyZNFQs7yXRM7Hr6a0L8c8WGeH+G6SVWgeJEP69SPdjMz8lu+7o0zcRNeDZKtmlwpIjX8VbjGrNT7n0SD0tDdviR5KcM08PLYR6sUrHFNzzTbKU1Ol+9SdV3kzbXLU17c3yI/yLaYzXrosgYJQ6eTWlaF6kCBI9MN46rQ4IC0YtEnjzY5tGwgfXjvlEqJs0wpNLp0p1TeTgOCpvv8YNkxdzQ== 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=1EtZBap3w8ZIg+WP8zXzWpJT9lQzplQNKYcUxAiAUeY=; b=iduKCd7kX5ZzXwVeEV91hLuzc2grdmNL4bhil9mAvnH9OQLE9NmP6KSMpbnS94dAmkV7JOOEP8MnoyTQlxAG7EyLKaudVa+2Klo/7tByDQWrLPorSUX1n+H+1ZDSG8+lLt0U5ZDnwz0i7Szd+HP5M+cK266BJMW+M5hK9K8Q5/ED+H8agayZdOjP5D1+d7MLVvO0xbuxz3oW1nuJ92K5jvtn3w9uJg7aicH4LBeHKECNHEWTn4j4Auj/OdND0WhMiFvy6+LdoMYbIuhN7+GRJgZBVwzu8X0tZYmOnIiP1+uCN78dw1TA6un3/CkpKBbD5AcGlgSdS6DRPNBZbFUPVA== 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=1EtZBap3w8ZIg+WP8zXzWpJT9lQzplQNKYcUxAiAUeY=; b=Rbl3AUyTXFlNQqPzo1OwiHfNwx9+u9DgF2FFT8+SjutU76j5ExbOZo+FlSI5voe5K4K7MkM/5dsjvv9OVU+Kr9xPK2jrkjqErkPf58GriJnCdzVNN1PJHPSqe1uvxuq+4Ig9FL5/EFXQDiTLEvGhIHYVlugocpk4YVHOeWJXQ/BJXPEqbD0wi/ZEHz3FfMnVKxeTavcv1YRi9BrNI78EUMNpyeAa96c+B19awoHe1L+grbKqfxCXlpuKGZ8LtLYFVICEcROFxSAgKaLlDtqxCCBUcWwXsmSc2WsTjjJ4LoDKxLx2QL6t04u+uf+qt6I4HUPTvSQAuUNyAcyYj+hOhg== 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 AM0PR04MB7108.eurprd04.prod.outlook.com (2603:10a6:208:19e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.17; Tue, 3 Dec 2024 20:37:22 +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.8207.017; Tue, 3 Dec 2024 20:37:22 +0000 From: Frank Li Date: Tue, 03 Dec 2024 15:36:52 -0500 Subject: [PATCH v9 3/6] PCI: endpoint: Add pci_epf_align_inbound_addr() helper for address alignment Message-Id: <20241203-ep-msi-v9-3-a60dbc3f15dd@nxp.com> References: <20241203-ep-msi-v9-0-a60dbc3f15dd@nxp.com> In-Reply-To: <20241203-ep-msi-v9-0-a60dbc3f15dd@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 Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, imx@lists.linux.dev, Niklas Cassel , dlemoal@kernel.org, maz@kernel.org, jdmason@kudzu.us, Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1733258225; l=3362; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=BqHEt8m1nlQE+CROgZ5Z9jf9BfRy50ieLQvI2rOkvkU=; b=QQN5g0eQRW0rJT6T5urTDAdXuYXzc5JS03FnK/FaP3Sz8oXcUuwJ5uVfJRcVOJm5kFTecJksZ ignF7Y+w9OBBBsuHwFcRA1yWCWjv+bq+HtfFq/Mj/jzSNBJd3IFolH5 X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BYAPR05CA0013.namprd05.prod.outlook.com (2603:10b6:a03:c0::26) 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_|AM0PR04MB7108:EE_ X-MS-Office365-Filtering-Correlation-Id: 12ab7f54-4cb1-4b13-d987-08dd13da49e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|52116014|7416014|376014|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?zJlSxu7LuPzhnsl6uf6F/15waykEPSK?= =?utf-8?q?t0v5UPLboGSAvYgdJUjXtAyli9xlKhdK9c6BBMWe+82gN0J2Ywts6CozAUV9aJ1PB?= =?utf-8?q?JYwd7I8YpR2z6DZeKNrwY6noSC5NyBvRJB7zZscnc9dOxVUX6q5obK4l284v4iOVq?= =?utf-8?q?GINWhMbk0RSY+BEVpAnaf5cJ7WrJzHIgY34EvLIKH8jhGidnJqx+3aUhsGL+KrrQP?= =?utf-8?q?04zoirFvX97Ijgm08eOjr8eiPXfChwbqXMi9q8AwmTUvq9V5nS0QPZONrYGJX1Bkz?= =?utf-8?q?QvS24S3MvT/Ko9DmuvvO4aWxmA98DARyULg2xEqrc7RadIq0VnULQeH0pacST5vVA?= =?utf-8?q?+WlP5Jy0GdpfM8ws1jefPrXg6TPTv1wK2S/ZHwBTEHS5QG48eRuZIcA+F1fA41AKU?= =?utf-8?q?hfUKQpXir0aDlMCLpH05WiUVoE7Frwx7q3csMwSfNl0FVmbjuAX4IiFjW5zcvdfDF?= =?utf-8?q?x+tzjEGu/YrBDP2wRwLEB9wBcsnHoaOYBb4lS9PD5Kmv93rjN5bD5uAnJ9Q0uiw03?= =?utf-8?q?gN7bJjPameY8rBwz975E4NVkPlka4DlYiTPEUQOv0ryzJ2xP3t4Tgq0PmLv3S7C6q?= =?utf-8?q?5+lrw57z7lLhtQJVaQ43+FMgQMhqXZiC6eYr1Ppt9QJR5jw3o3SGxF+2PwqrhgwAw?= =?utf-8?q?2yxUqTHN1+iHKSXz4mQ6o8IQ84oCLXUd0a3ZhJXt+ruo3C4HqYyuf50f6j5rEJF2h?= =?utf-8?q?iHZ6ojEIVAJf/hvRQxoiD3AOOmmcLvg8nPs7F9p6LkkpioT6CNGlkq8RY/zYIqv1A?= =?utf-8?q?aias3UpjskmoVXgELGs/H9BFb2CGkLwxGKDBisnwYYN5yRGL1LUBWcnglfZtIstMt?= =?utf-8?q?BDlBoPMCV7aknilGN/FFSyF3oGrwV6Hznx/IV5XUj34H+QCf4cuPdBhxbLcRdSruI?= =?utf-8?q?KlcC8ltldN4dehgkQ3uI4cR3dFTIxDKFLnX+VuvsJXYoAue4aJ/i4G8mGL3DoJPHo?= =?utf-8?q?ZEYSwY6PlHLDMtDtAEp3sRhK1otOHujoLCv3i3WafYmhJPAcc4YRjrFaZKt514SH7?= =?utf-8?q?qB+qWkHBbSyvjt8gmjdzk6i7qmV+okBFvB3onj3dHPzc6egWRJbXyrSiWNZD5mm1Y?= =?utf-8?q?13HVCDBrcnQFbIA3lOuN+JN+3vDBgfUfmPaamnjbdGuf2QAcv/rq629zVh2SGJaws?= =?utf-8?q?KdjrHdFmEroerRXLR6r6DNt4aZtTmhUqJnBkJ8Rf2X8Og8PesUH3X/bl6l8nTtyn1?= =?utf-8?q?upDdlbJCnEVapueiNkzHLK65RoIeVudnwK1rOsHCpbu2fNyzCKgwWKNx2u8/zCB1e?= =?utf-8?q?ACxk0QEgmevNdM0ULwtKHm7QBGPFm3Ljg/aG5NjqFL4WrO1RvRGpMYvxRJwt6TQQy?= =?utf-8?q?Psilu02TaYb86qdpi42VumpGGmd4yLy3LA=3D=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)(366016)(52116014)(7416014)(376014)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?gRyeQsOP8sm8ju9LGjoDLzl+fP8f?= =?utf-8?q?GDl3yajuAzO3xDPdrz6xb+VW1SuOi0TpajvGL/17NStQdrPq8W+xmWCQGy3bx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=?utf-8?q?jznHlMBlhoR0A+93cIzDpqhSRhWZCePCyeOz5pyE01CVCR+Tf/DCVn5icNOtJQYR3?= =?utf-8?q?sdQWX1yC2fdn4egi0ag7y27zrPrJSqFazwx1Gvo/HN9o73qujqwvqZQgLeOuUgeb2?= =?utf-8?q?PmwAJtNF/pGWhYxO94wUyTmQ3b7J00WJvjlJx/CDUjf8Uxbr0ksbHkcQuvHiElkDw?= =?utf-8?q?IFVIcv9/+AEJ4unMVnpYtk0PDw0WostTGgDIKhWzwuR4Xui6BeAvPlWne7oat13b/?= =?utf-8?q?4AhWPxieua+ZJE/giB1uQjc16RIm4qdrCcR/2zSKs2jrEWHQlsbhIqucSX8UgaUs9?= =?utf-8?q?yyBewfp2ruAjnC6LyzQrzrvCWYdi+g+pAdDhVnv8TxGuNeF1DaRulkYtB3CZG0bdj?= =?utf-8?q?/e7a8zffFpvX8AWpHxCzS0FAVQbyDrWS4tQORu6pSTKxoyi87tGq9SmddYZwp+C16?= =?utf-8?q?TI4ZJb/ndGpVp4kdAN2sZivEV2jc6GSlJlgMvLNKLLyo2iEZGA2URLrsLXwlIwEIk?= =?utf-8?q?27dY67IUvvg1grqpdbhYp1eGAymDObEg2SNlRSNU6KNOcF9FhTnqoY9Y=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 12ab7f54-4cb1-4b13-d987-08dd13da49e3 X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 20:37:22.1031 (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: reAW98dWL4AB9PC8b9g1xf6w1T6X1P4Hr2h1LadXb2MCbPLzXbFV006RJw63AJrOlZEC7d/UATzbGPNMJYGLIQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB7108 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 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);