From patchwork Wed Dec 11 20:57:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13904204 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7C4EBE7717D for ; Wed, 11 Dec 2024 21:05:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Cc:To: In-Reply-To:References:Message-Id:Content-Transfer-Encoding:Content-Type: Subject:Date:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oSjI2OUg9zpkx5v2mVQeZQbwxanG9aTvPl/3UjNHnHk=; b=Rp7T98yrUrnR96ny/pVAfMv9th FJtFj89R43zRr2t3dGo1IU75E6Vwbxg+2rJ3RnMZPx3L10rnxSEH1jwhZYSGdH52vLGNQBVvexDwS QVVdEUgGSq6FbAbAoHmSIVeKJY3eUMwZnlkxfskK0jJ8aWIXgKyBEGkk5OIOieaN/IXmCuMi5D2zP B/laaHVtVUHv89CL4EDQekAlAUY7Ns7LRmoP58GBcbM2EDZR18xSrQPMf7e3TsNSLECcqCiWTJJrz 9dn/OFlTW4382O2KW1rxyRs+UAUBYcJQY8yhqD072kCPMLmKV0ZioL9dGCrooBL8ViCc7vIItixVv VfIoTDrQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tLTtG-0000000G7Ve-3iDJ; Wed, 11 Dec 2024 21:05:02 +0000 Received: from mail-am7eur03on2061c.outbound.protection.outlook.com ([2a01:111:f403:260e::61c] helo=EUR03-AM7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tLTmQ-0000000G5Y8-1Cjr for linux-arm-kernel@lists.infradead.org; Wed, 11 Dec 2024 20:57:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wOO1sI0CuvMXkg9lwYysUU7z9xjxEVgAxgUV1bgcd5xmvgI2A83M48NdLqf2BaCjL6ovtD43+b7VZ1rnMhzlKsQYNh/LmJTDO1Seg5mIaKaTocB6XoI4/GBff2ZSeyM9+GKUQIdIQdaqkmxNZinWg92YW/aV/awqa8nEkPElRBnI8lJ2EKLbHB2LNx202kmL87m64+SRFEDKXkZF1QLQhY7C/eBuUw2S/gVpJO55Th+4NqgJiCLNs3Mo45pFqoNtSyHKvB0pyXdKv8zCz1AWs4TD1fH1P/lRkR3NJZ9DXgDuJitBY/i9lxHhqVaR4v+Jfajh8vYZYiteSXoqZMZH+g== 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=oSjI2OUg9zpkx5v2mVQeZQbwxanG9aTvPl/3UjNHnHk=; b=W8WtlFClJ4eEfmlVbvcTB2UNzHBc1FqrpfXZZ8/p1UrD8bjrgZ1iDX6T4Lozhv15tKsSeaxC5izRwsQul9eX1gM6wW0I5rCC3bhKskvr79eEgNVOj3X+qMI77iQxrzn4YU/JQo56NDfuxGFDJGKUmSsaj7CgxA1OlzFQGvZRINw57ZBqLml4nbRxpPfc6cOEI4UZV+36BIbp78Yku0YLr4xQ/Q+XliRiLalmF9CEdg1jfxj8077QkoKY4iHMRszk3jD7uPYqbHgEhMmur9tpqtcN29SYSZ2kjELSsFM+wAaApZ1Uc1DHtAJuBmpMAvVGa5z3xNoNyrTmUaIkodKoVA== 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=oSjI2OUg9zpkx5v2mVQeZQbwxanG9aTvPl/3UjNHnHk=; b=iTc+YW1tQHH+GXA18yVgyxlODIRzWvmcQtxKxdQO0wyEdBwNwO0FqJI+8wmS1P/Z5R864OC5j/DDgbHFYayYNo2BKabHEHx865zmZ0y8H4Lzp6O4NFgCCRW6AL6+mvLh3P5ijRdVS8U4gIvOR7PykkKtMxPKmf1O+wSFeibUYDoQnsbNHa1bYZBpRXZgHgnqZyTUW9CXYiGRI57zcRup6hp7tcGG1qMPkY8iiZwrN2kRlSTq9O92S9S+icHV6yMHQLGzTIkOdbKJ9zhDn7P5a+NDPWVFTM94dOkGJqhhgVLLtY1An7TJw4ayuIgihce5xUxzsuthCFUjHwP9Jt4XvA== 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:57:56 +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:57:56 +0000 From: Frank Li Date: Wed, 11 Dec 2024 15:57:30 -0500 Subject: [PATCH v12 2/9] PCI: endpoint: Add RC-to-EP doorbell support using platform MSI controller Message-Id: <20241211-ep-msi-v12-2-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=8536; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=Udf092p8YjxrnQN11gltHeyiotoRRFrXWEdyz1BjlZI=; b=uBHyPAj0CEvZaks+WEu1KQpI8LXbt4CPR/bd0UABXhGiJTkjOsHSaRUtZ+10g3No16A7PEmLc t9TrdVoy4NgA2ZM5ThbbpU5795ZXeXzSPzAKJnnd5ayMHmwT2R6zDqb 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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|AM0PR04MB7139:EE_ X-MS-Office365-Filtering-Correlation-Id: caedd752-9a95-4b9b-10e1-08dd1a267cf4 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?djrcfthH0fS14JQwTpW5hzR1bfmCCzk?= =?utf-8?q?TCNpigttIeclyy1VcHVIA1+icgZPyeCrX0zZJ09BrkJm0bNJ5HR5zAtEWPSLyY1Yt?= =?utf-8?q?WEanxHaFOyQEaX77fRTen19L9hESrjjLAzJ82CIiUFUeg1PjOcerba/EgMyHlfDY7?= =?utf-8?q?A0VNAvLp6FMEe5zY7HY/XCP0OhRerb9X9rOb1dM5wz3unkQXw2zKx0a67M0ngOTAo?= =?utf-8?q?QhsRNYFmMIHZW+CVtxGzWAcKpzDylonaBYwbMe4TZv1tWuz6k/SVm1/MZnXHuBYEz?= =?utf-8?q?2LpYilRTsey9J6d55mDHy1ewobKGaqkJhr3jKz3PqplL1inWOwxVK9ipfetD5k75I?= =?utf-8?q?65GGMPRhS1BG0ubv/G8+v/J6vk7kD50stfKVf1cnXG/GRdQm3M/F9GuLVwu/2oEc0?= =?utf-8?q?vp/F7jHERcPH9GBCXf+am2jHVJkMC2z2zoqTXS92yFPfvvMt0Xu6aeo+PBLp4Jizu?= =?utf-8?q?kIxgEoj7/Q73tY2qB/8igDB7iR9coF1tQV7lPYFK+h37F/IpLpze0f3fGHN1CfXlH?= =?utf-8?q?8UpwDY21ztWznEilEsiwkHm5oTshc8ZEoiPlJR6JViXeRz8mEc5MX5ywLOrRxlLRb?= =?utf-8?q?7uBE0P16ShebWewm+DJVPatefAwQUOHWTjncdld0jB/PONl2MIRoTfaf/ylYnh3t1?= =?utf-8?q?q3EzmzMqjM/o9Um1FahI0Jis1qEWCvH+VwgnTM7+Ne9/sovbT7dznhFAlAxIP8D52?= =?utf-8?q?PpNYpKErM5w+aOgsN1XqPedaRy+enrwodH49r4t8NVrG5KBfXJBF3xmYZOc8L8YIr?= =?utf-8?q?5vQEcLdJDmJiGIdnJmsmBbD/HMnCNSyHKscL0UefmtKEP7JTooktwDYHPgxn9XE/x?= =?utf-8?q?zbLEzdSFjHcjUymP4Gcp0VlBiFkFRBYZDDI8FjDiVbRKxdFPFtETSnKT/hzYYmk3/?= =?utf-8?q?UbLMFYzlGCBnhIF+tGm3w3iWEe/rGd4/nnE4W1vr2RRCFSReihPnm8TusjGWLEqjn?= =?utf-8?q?q/nhLdWpPgrxwcpKw6n1U0wGaHbi9beoMlaacrJjXFAJ87S+mRiXGgPByb+ft4Aqa?= =?utf-8?q?K5rXaAxiey5RXuXnbov9r+ad2gGXNw+dugxjOiaMIIloFW9euLOGLRRtT+VB7r/cr?= =?utf-8?q?oREyWu+WeGHbx8z9xXg1CsM95KR0QDIfNI4J6oUWoRjyneJvGNhzZXIYbRjklDNU4?= =?utf-8?q?AAAT8S1WVGdqiQBleANTM+lwTdvT7qBMX34NRVUMsi8pVvOghHy2pcgQGHGVusf2/?= =?utf-8?q?4uGi4ctoWQ+RL8PfPae1n87ZwQY6qA5hZBYbwg1+6dCopUnnoX5+CP/ohTztQ6Ico?= =?utf-8?q?pTZ4XpCBUTSIH4MY39vYYHbgNP8GnKrcFZAdRmDr1q69kAhtyLRK1Z3oxEasOqWw3?= =?utf-8?q?ZuDug81hRSBgcjGXbQgtD2c6amrfFN88KtwHAOPVzl8zaYJDjeoMQ1g=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?Zhd1rSxAJQTKxwMEvm1xLIgnAESS?= =?utf-8?q?GmWstWTrpeau1CovGxHeVfZLHAD+K0nIyw/jQnjGXEGugCQ5z7EPwnaZxhmtqAv/1?= =?utf-8?q?za2gHyu5xdduG13LyTAus4pOarFSJ643AOltNkUmm6GWiq+B9i9+oDnm14J9L/uG5?= =?utf-8?q?gNa8h9wC8nCbMoDQHf8AI9pwhsWBU0VvqAHW52W4hQyHeGipJKA4h+f2loC9M6G5t?= =?utf-8?q?lGMoyHp+WzANkPPvZFoumprCt8y4UfA9Fhz9bp2pvIAGvRyOTmiv5tKWXbdvRQhdF?= =?utf-8?q?MH8Bkai0oCCN9ywgNmxse8EX/AUFV/nBkjYVMn8AYYl21AHk3jUb9j+KQQCvZPtwE?= =?utf-8?q?uelpMCeEPB+e8NxXN0L02eIEEf5o0CGNqJuxK4Wa89lyWo+LuUZxo2dauWTOmCl50?= =?utf-8?q?qDhw47iPsH7lgf44Mz13ZTuAISp+tbJYvDHSUQ+0Q7O7iGeDKTgNiN+Z6FK1lX3zz?= =?utf-8?q?hecQbGvb2xccTCyIuifwRz/lkX22YMIyC7pN4PMNkxDwL+NLT2UsmWhuFynrWqoBY?= =?utf-8?q?fhncZYZQP1J7yIpbFU58IOQGByiy9KFcLtnQJgldK9q6/ZwIvSy5g8Ys0uR2gGKjD?= =?utf-8?q?I4411gLogULNiL1SSoGqei9RWtpmr+WQrfTNuYNTVMkBBnZ8aX5qGs9uiNLIEG2u8?= =?utf-8?q?pON/oOZjTtCrbcRqYCWV3KUXdXgrlUDCd2ySg8SMfSq4WaUno6FK4PCQtBg4ZNw7I?= =?utf-8?q?YNcnvVLa2+ZsbUwCdTNL98Yx4fkMsbb0Yrqwf91GUrrHdueETdgi/SFHZLLHY4ZxA?= =?utf-8?q?aDfgndE24M/xgY7N5FhNf49OtuVbm9Lwg+DgKgc+8W07lwriZqXbxdVzVmGQZ5Oso?= =?utf-8?q?dOwNOEcJ+fmo3UHuBEfsVMc6FiuTOlc5qIjvMlIXoxoDRv30uOyKCXH/2OVUQ6pIL?= =?utf-8?q?W8AdExpns4MfequseHtJcAKv09o/GGThKWmvGbf+k9G1GI6ISPoyue3ebdJjOfqZE?= =?utf-8?q?jBc0BFan9Rqk1WTgMImPstZlZeueioGYqksZXca5md10zGTfOdcn/XdbOFNCziUWR?= =?utf-8?q?lUGcJwzW04gTST8tsve18sw/NAeVUJsFyFHdd0bwjQOcZt/ofYBaLYihakLVgzkY/?= =?utf-8?q?yqHvytwDAtwb6E2MD0KN2yrgCCeZ3lFPkgdkup9nZlDSesTmM7XnK8hp8AW8iAaJq?= =?utf-8?q?lo1abir6zQ62XytJSqKnhXWQMrvYQh1T245R7v13TPS50aDoEDM5HGujwhLI5oK5F?= =?utf-8?q?FCUkrNbNCVE6MF3wCIa5g+ZwQFxd3k1eTW1C7CEocOcnSgqylcTKN0i4t+Clvv76/?= =?utf-8?q?fd9Uajt5DSiDeoZHG52pskub3Ka/esv/Pc9fWEZ4LjmtKP9viXzpfp09ACnECHBB8?= =?utf-8?q?EGXXhbAp9wz292AtMh3E4xaVv3J06v3UUwv9DZxX3Kx+Bu+Xld+X61tykmO8Va3vR?= =?utf-8?q?lHQGW3pGgmLh34OR1UbxuPpjkeL2tnM2P0qpcwjbgPY2CPXPRnNh44ZheNRHZecRK?= =?utf-8?q?qJh14gJyot95Bq0EVOqj8vO5LD36tXxRAG4bTN3gdIoUYBfPMAaoPq/9kfRGiEKCj?= =?utf-8?q?TO36b/8cHk0q?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: caedd752-9a95-4b9b-10e1-08dd1a267cf4 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:57:56.5230 (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: ym669mySxWsqdPziA77iKhfxbUrt+JOGu65kxPpZCEdSBeK9tWbusEkX9QuD8KcMBGfe+pJ/0CFl2cq6bIIKQA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB7139 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241211_125758_338651_319DBDF2 X-CRM114-Status: GOOD ( 24.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Doorbell feature is implemented by mapping the EP's MSI interrupt controller message address to a dedicated BAR in the EPC core. It is the responsibility of the EPF driver to pass the actual message data to be written by the host to the doorbell BAR region through its own logic. Tested-by: Niklas Cassel Signed-off-by: Frank Li --- Change from v10 to v12 - none Change from v9 to v10 - Create msi domain for each function device. - Remove only function support limiation. My hardware only support one function, so not test more than one case. - use "msi-map" descript msi information msi-map = ; Chagne from v8 to v9 - sort header file - use pci_epc_get(dev_name(msi_desc_to_dev(desc))); - check epf number at pci_epf_alloc_doorbell - Add comments for miss msi-parent case change from v5 to v8 -none Change from v4 to v5 - Remove request_irq() in pci_epc_alloc_doorbell() and leave to EP function driver, so ep function driver can register differece call back function for difference doorbell events and set irq affinity to differece CPU core. - Improve error message when MSI allocate failure. Change from v3 to v4 - msi change to use msi_get_virq() avoid use msi_for_each_desc(). - add new struct for pci_epf_doorbell_msg to msi msg,virq and irq name. - move mutex lock to epc function - initialize variable at declear place. - passdown epf to epc*() function to simplify code. --- drivers/pci/endpoint/Makefile | 2 +- drivers/pci/endpoint/pci-ep-msi.c | 148 ++++++++++++++++++++++++++++++++++++++ include/linux/pci-ep-msi.h | 15 ++++ include/linux/pci-epf.h | 16 +++++ 4 files changed, 180 insertions(+), 1 deletion(-) diff --git a/drivers/pci/endpoint/Makefile b/drivers/pci/endpoint/Makefile index 95b2fe47e3b06..a1ccce440c2c5 100644 --- a/drivers/pci/endpoint/Makefile +++ b/drivers/pci/endpoint/Makefile @@ -5,4 +5,4 @@ obj-$(CONFIG_PCI_ENDPOINT_CONFIGFS) += pci-ep-cfs.o obj-$(CONFIG_PCI_ENDPOINT) += pci-epc-core.o pci-epf-core.o\ - pci-epc-mem.o functions/ + pci-epc-mem.o pci-ep-msi.o functions/ diff --git a/drivers/pci/endpoint/pci-ep-msi.c b/drivers/pci/endpoint/pci-ep-msi.c new file mode 100644 index 0000000000000..b0a91fde202f3 --- /dev/null +++ b/drivers/pci/endpoint/pci-ep-msi.c @@ -0,0 +1,148 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * PCI Endpoint *Controller* (EPC) MSI library + * + * Copyright (C) 2024 NXP + * Author: Frank Li + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void pci_epf_write_msi_msg(struct irq_data *d, struct msi_msg *msg) +{ + struct msi_desc *desc = irq_data_get_msi_desc(d); + struct pci_epf *epf = to_pci_epf(desc->dev); + + if (epf && epf->db_msg && desc->msi_index < epf->num_db) + memcpy(&epf->db_msg[desc->msi_index].msg, msg, sizeof(*msg)); +} + +static void pci_epf_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc) +{ + arg->desc = desc; + arg->hwirq = desc->msi_index; +} + +static int pci_epf_msi_prepare(struct irq_domain *domain, struct device *dev, + int nvec, msi_alloc_info_t *arg) +{ + struct pci_epf *epf = to_pci_epf(dev); + struct msi_domain_info *msi_info; + struct pci_epc *epc = epf->epc; + + memset(arg, 0, sizeof(*arg)); + arg->scratchpad[0].ul = of_msi_map_id(epc->dev.parent, NULL, + (epf->func_no << 8) | epf->vfunc_no); + + /* + * @domain->msi_domain_info->hwsize contains the size of the device + * domain, but vector allocation happens one by one. + */ + msi_info = msi_get_domain_info(domain); + if (msi_info->hwsize > nvec) + nvec = msi_info->hwsize; + + /* Allocate at least 32 MSIs, and always as a power of 2 */ + nvec = max_t(int, 32, roundup_pow_of_two(nvec)); + + msi_info = msi_get_domain_info(domain->parent); + return msi_info->ops->msi_prepare(domain->parent, dev, nvec, arg); +} + +static const struct msi_domain_template pci_epf_msi_template = { + .chip = { + .name = "EP-MSI", + .irq_mask = irq_chip_mask_parent, + .irq_unmask = irq_chip_unmask_parent, + .irq_write_msi_msg = pci_epf_write_msi_msg, + /* The rest is filled in by the MSI parent */ + }, + + .ops = { + .msi_prepare = pci_epf_msi_prepare, + .set_desc = pci_epf_msi_set_desc, + }, + + .info = { + .bus_token = DOMAIN_BUS_DEVICE_MSI, + }, +}; + +static int pci_epf_device_msi_init_and_alloc_irqs(struct device *dev, unsigned int nvec) +{ + struct irq_domain *domain = dev->msi.domain; + + if (!domain) + return -EINVAL; + + if (!msi_create_device_irq_domain(dev, MSI_DEFAULT_DOMAIN, + &pci_epf_msi_template, nvec, NULL, NULL)) + return -ENODEV; + + return msi_domain_alloc_irqs_range(dev, MSI_DEFAULT_DOMAIN, 0, nvec - 1); +} + +int pci_epf_alloc_doorbell(struct pci_epf *epf, u16 num_db) +{ + struct pci_epc *epc = epf->epc; + struct device *dev = &epf->dev; + struct irq_domain *dom; + void *msg; + u32 rid; + int ret; + int i; + + rid = (epf->func_no << 8) | epf->vfunc_no; + dom = of_msi_map_get_device_domain(epc->dev.parent, rid, DOMAIN_BUS_PLATFORM_MSI); + if (!dom) { + dev_err(dev, "Can't find msi domain\n"); + return -EINVAL; + } + + dev_set_msi_domain(dev, dom); + + msg = kcalloc(num_db, sizeof(struct pci_epf_doorbell_msg), GFP_KERNEL); + if (!msg) + return -ENOMEM; + + epf->num_db = num_db; + epf->db_msg = msg; + + ret = pci_epf_device_msi_init_and_alloc_irqs(dev, num_db); + if (ret) { + /* + * The pcie_ep DT node has to specify 'msi-parent' for EP + * doorbell support to work. Right now only GIC ITS is + * supported. If you have GIC ITS and reached this print, + * perhaps you are missing 'msi-map' in DT. + */ + dev_err(dev, "Failed to allocate MSI\n"); + kfree(msg); + return -ENOMEM; + } + + for (i = 0; i < num_db; i++) + epf->db_msg[i].virq = msi_get_virq(dev, i); + + return ret; +} +EXPORT_SYMBOL_GPL(pci_epf_alloc_doorbell); + +void pci_epf_free_doorbell(struct pci_epf *epf) +{ + msi_domain_free_irqs_all(&epf->dev, MSI_DEFAULT_DOMAIN); + msi_remove_device_irq_domain(&epf->dev, MSI_DEFAULT_DOMAIN); + + kfree(epf->db_msg); + epf->db_msg = NULL; + epf->num_db = 0; +} +EXPORT_SYMBOL_GPL(pci_epf_free_doorbell); diff --git a/include/linux/pci-ep-msi.h b/include/linux/pci-ep-msi.h new file mode 100644 index 0000000000000..f0cfecf491199 --- /dev/null +++ b/include/linux/pci-ep-msi.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * PCI Endpoint *Function* side MSI header file + * + * Copyright (C) 2024 NXP + * Author: Frank Li + */ + +#ifndef __PCI_EP_MSI__ +#define __PCI_EP_MSI__ + +int pci_epf_alloc_doorbell(struct pci_epf *epf, u16 nums); +void pci_epf_free_doorbell(struct pci_epf *epf); + +#endif /* __PCI_EP_MSI__ */ diff --git a/include/linux/pci-epf.h b/include/linux/pci-epf.h index 18a3aeb62ae4e..5374e6515ffa0 100644 --- a/include/linux/pci-epf.h +++ b/include/linux/pci-epf.h @@ -12,6 +12,7 @@ #include #include #include +#include #include struct pci_epf; @@ -125,6 +126,17 @@ struct pci_epf_bar { int flags; }; +/** + * struct pci_epf_doorbell_msg - represents doorbell message + * @msi_msg: MSI message + * @virq: irq number of this doorbell MSI message + * @name: irq name for doorbell interrupt + */ +struct pci_epf_doorbell_msg { + struct msi_msg msg; + int virq; +}; + /** * struct pci_epf - represents the PCI EPF device * @dev: the PCI EPF device @@ -152,6 +164,8 @@ struct pci_epf_bar { * @vfunction_num_map: bitmap to manage virtual function number * @pci_vepf: list of virtual endpoint functions associated with this function * @event_ops: Callbacks for capturing the EPC events + * @db_msg: data for MSI from RC side + * @num_db: number of doorbells */ struct pci_epf { struct device dev; @@ -182,6 +196,8 @@ struct pci_epf { unsigned long vfunction_num_map; struct list_head pci_vepf; const struct pci_epc_event_ops *event_ops; + struct pci_epf_doorbell_msg *db_msg; + u16 num_db; }; /**