From patchwork Tue Sep 6 19:40:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 12968051 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 E7E23C38145 for ; Tue, 6 Sep 2022 19:44:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pd+60IhDr3RIWAFMeDO56JTOF02eXwndBa0EyxdD+MA=; b=sjOQDnDDPNkyWr utg/t2r5ocZrg9PfX2REaSFpEXf36r5wcG37l7txUG8SLKcDkly6sTKNIRuMgoMO1hwV4lVbS5K9j 1EQ4IfQpK5JnWHkLYDT/+DmJk9pOknE8HULIJmcEWgGSLywoL/GSOLE2+9lAzdd56V8Hqb2z4GMNk 2pbTYvLjzJZnGRNPoM5JRKy+nUVY/diSVOWUanKKvlcg89MeR5iDoNMvfKF4VB46obP6NhFnP2/Lg pEly2SKEthzhgTkx2XpzucXIWqCiASf42rAL1nL/zcR4TYh5AyPPPxWh64wjQmv/xMsChZ4qzcuIt sOLrPTQqF6fybMFg34cw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oVeTB-00GnfH-Ql; Tue, 06 Sep 2022 19:42:51 +0000 Received: from mail-eopbgr60070.outbound.protection.outlook.com ([40.107.6.70] helo=EUR04-DB3-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oVeS5-00Gn6H-TB for linux-arm-kernel@lists.infradead.org; Tue, 06 Sep 2022 19:41:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jL/EGuLS/HViwsRcRPOMIkPQNs/CIFoCSY3OCVwBMvrMrOQfivr4J35Mu2RZvRYTaetFJg30Gwk50ETbO7bXNg+pX+MtJzX3koiAL5gz7U+QRfq9v9GtjIGm8IcSlW+dyIkmGa7zgE+1AuGvB0qtrO2yrSD1eggxwbNF14duHV8btuCqHYeEG+DO2VYEU1FaTGnMzAZJPJmxiEXRS3ZxaurOz1DlaLtUiWslxC6KtXnNfiph/k5/mu9RgMZ2LhgN6DEpXDKN2Cpifk3zXBHt1GxZ1tdJLcaHFwNIzfwSz+H1jVryBl1w2TM4/rIsX17kp+0Jodop15j8GCSpDxv1Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=luNuA87ZdAV700jTSvPtoctsb3rd8ltqs7Sjx4ZnBbs=; b=Y+T6Deyv09Y0rblMxeLsHKdi+By8kFGM9aR+QCDYr91bMJtM/SLBAFb1rZ8Q2rzxqDHY9R/yGLaiB+rGqrH32t0QirsWRQu36iryRhxzwjkaQCOQK9EtzgFhGuU9XVp4xAAsbLWAFnSMgykYLqcVDFCWe19a58s7rmoAeXnNVJS8uMtF/Dgt4TvlwD6kgnsQKA4kTE65vbL8MN3Q6PO3JDEnPJsQftBOMZz86q5A4QjGdTPZVAOSM6VtPu2NK0qeCDuvdFqXeodql3j9m0qs6xW062gfGH0AgbWLRe2ILDbrG0r4RL3rhjDAe0Byk3v2Ml/VM/rP+PItzttqwKLogQ== 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=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=luNuA87ZdAV700jTSvPtoctsb3rd8ltqs7Sjx4ZnBbs=; b=N0PsTT+XRSgZM6m4dsen/KFVN74P2USpwgj3MucB57FI/jn+kaMIJx+Y9v6LtHIzGygqPdT6xWc1gxshaW2HU2eP01yuuwZtMe5dlb7LO45Zc4a9ci2zQCxQhW2BO1a/atzOMBBgYqlWVf9YqvBczJMli/Zd7Kv6oRuB88Pl2zQ= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from AM9PR04MB8793.eurprd04.prod.outlook.com (2603:10a6:20b:408::22) by AM0PR04MB5059.eurprd04.prod.outlook.com (2603:10a6:208:bf::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.17; Tue, 6 Sep 2022 19:41:35 +0000 Received: from AM9PR04MB8793.eurprd04.prod.outlook.com ([fe80::1c3e:36a0:1adc:beb]) by AM9PR04MB8793.eurprd04.prod.outlook.com ([fe80::1c3e:36a0:1adc:beb%9]) with mapi id 15.20.5612.012; Tue, 6 Sep 2022 19:41:35 +0000 From: Frank Li To: maz@kernel.org, tglx@linutronix.de, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, shawnguo@kernel.org, s.hauer@pengutronix.de, kw@linux.com, bhelgaas@google.com Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, peng.fan@nxp.com, aisheng.dong@nxp.com, jdmason@kudzu.us, kernel@pengutronix.de, festevam@gmail.com, linux-imx@nxp.com, kishon@ti.com, lorenzo.pieralisi@arm.com, ntb@lists.linux.dev, lznuaa@gmail.com, imx@lists.linux.dev Subject: [PATCH v8 4/4] PCI: endpoint: Add vNTB MSI support Date: Tue, 6 Sep 2022 14:40:52 -0500 Message-Id: <20220906194052.3079599-5-Frank.Li@nxp.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220906194052.3079599-1-Frank.Li@nxp.com> References: <20220906194052.3079599-1-Frank.Li@nxp.com> X-ClientProxiedBy: BYAPR11CA0042.namprd11.prod.outlook.com (2603:10b6:a03:80::19) To AM9PR04MB8793.eurprd04.prod.outlook.com (2603:10a6:20b:408::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM9PR04MB8793:EE_|AM0PR04MB5059:EE_ X-MS-Office365-Filtering-Correlation-Id: 44bdd7ce-165e-4d75-06dc-08da903fced5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CPsYo9Ose6v/2IBYr6sc5tkO24lNRAHc11k1HIGiB8BxftBeqwRWfg21vPJsBsJFO6oYCo0pBf8+m/Pwng2OZ2aJGUGmWoJUfgHiwUZ8GOVr54ed5cXVjImyvf+hJt3bWyzBt1EwnDWUSOdTnbWx28wa0526y4OBUb1KT8J9JzC+0Hg+Mzw+yN08fseYc2bifZc+jLOLPVhLTuE9cXjBtSWlamTvCkGe9YzsAmOF9QgRvC00w0rDBFy7a+KeH4xse27Hd2ks0t+59+meN9FnmAFSwQsMZDM/iPvgZNOl/omgHe+Ghh+Lv367tTB/fABtl7ZhZ/FVOC1DxNfQqlGV6e0FHg+BEwNjh7NdJKfzD5E48+PV/HWY28WeoYX3UmVsK1VDjAQy5YLppZRdegEqSvHpfnHMVUkz42HPOobspPWKa3ESRqZ6aR3ldBPhpRROl7fYI7QCNB4IrUhfeWtEzyc7WF1eirmEGd6RhAqoJyJsJPNfjvycmEONdU13X+5ahgUPjL2XGVnLfzCO5jFJ6fHY5fPTC0Wo2vOXfVklQE3EuXqtBZNKCbza8YrkTpNxmWVjHPpPIIFC2bGq6njYf/j6hd9+JtT3NZIqI73Ua0ytB0F438M3IVJcipU3Q06P2yKE5eMa+T+pCVxTtoddm3OmebAq5FUtW7yaeCYhfZVw5vOzlC1D/ix1+RIOSWmD3dwNxc6dYMzpDagQwvpK9LH781dBpa6UZd4BjLlIh7uK77v+/FNsnjs53hGSS/BzvwD63GStOl6JvZDPcxx8bQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM9PR04MB8793.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(366004)(396003)(136003)(39860400002)(346002)(376002)(6512007)(2906002)(478600001)(26005)(6666004)(41300700001)(83380400001)(2616005)(186003)(1076003)(8936002)(5660300002)(6506007)(6486002)(316002)(52116002)(4326008)(66556008)(8676002)(66946007)(7416002)(38100700002)(86362001)(66476007)(36756003)(38350700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?4o9BNozVjW5AM1R//fd5VfgRsMtu?= =?utf-8?q?efqoQNoX7h+wV+FVmqMEpzw1xOqVQC1BP6eg0OT+RWCYmBbikmVsUy75xeSkQDfBV?= =?utf-8?q?9/Hj9WxnmlHYNzsauyfsBstEE8SP/C/mjTOUv1kgxOFyUz9PyOwJYbnsBIfZMS3i0?= =?utf-8?q?j0JF5JFfkvBJQELZekodTX6PKOErclGsYMaUkaagUiKRirF64YKfiW12XnIAOETlV?= =?utf-8?q?ouIEPb+hAoa2Uiia/kbjyhh08HzKa+rzGFTFhSU0tM4UO7HjAcC++N/PCBOraIRPT?= =?utf-8?q?qOEZ5r/xgWD4GyeDYPUF452SYeLrgFkdgVFnc0Lar74iW1U7SVOX3p8bVlfIjllBb?= =?utf-8?q?Dn/VBEoZIY0R5u6nK3+tlEi76RFmdHsqm8xoRJHcayqEn5DopmzsqH1Ct7UYHsHUL?= =?utf-8?q?WHsK6+RiKDJwEJceuW41WqY6T5pA0zIfGtyU5HcDh96Cbw1pkdw7cv1Ot7CjE6aI8?= =?utf-8?q?wUNCbxOHMEYXnILp98Bl6BB78xs/UYxvMk3QtAocqPXpsx7RL4phnHrZj+Df4XuMd?= =?utf-8?q?GL/BYilPFgw7M354ZIdCDa+7FANK0kTPkjRe7ZSmid+ujXr04dTQvp7x8eZQAKdC6?= =?utf-8?q?CI3qckqoaEqTh+D4LVcSz7GTgz9ZBbtysGwH5dTv473QJutt2NtsYGdhZOPFIK6e6?= =?utf-8?q?Wy4jb02rwYiRUug/91M1sfk3I2S25sV3VLcZH2cGMlFCs+fMAX3xwmc6HDkt3tNP1?= =?utf-8?q?9MpOv7ttvLZllLQDcvkWs5mdm35NUZosXOB0CziN5ydp0MwQXUhUhUoXhpGLIfSPr?= =?utf-8?q?Bzprboi7YJWMyHzFkXkMsSvVvekPLN9hFM0CxDDT/wb87mBs4Utx8a5yol6PC0DJ4?= =?utf-8?q?OBBCo7ewNEuXKNC3MSDNz33RhtgQxEP//l/NnJ058B0db0oPTClcBZXKARtpaNOpO?= =?utf-8?q?SznnnI9cO+v6dgXq2Zv73IVSyf1xVF3TaEt97Cabf7pwbCp/1KIx0QrdWpkXaI0wr?= =?utf-8?q?zy4/UBCpgp/qWP4nz1H+TRPsF1cmhd+roVpxOOUnHxflyolxnAM5qdegVFvjOLCUq?= =?utf-8?q?jezxZohZndg5r3/RjMWBMGPwE/nZ05uw3Vj34gb/l28sN7kZC//nWHv7tQDt+0sHR?= =?utf-8?q?oIkSac9ReDaaUzTAgSNn5Uy9sHT+woVNjuE4IXcEsgP9guCgVmz2Atj3gwuTK7zLv?= =?utf-8?q?gJyDqK5UoPAXQCU9F/K452x9fPF9/1TPKV8l9bYHzSLnIhR/RhrkscJUZ8jvOG8sp?= =?utf-8?q?6URYibce2ZiGlDnzB/Kcwm8my8MJuKqs2/9ScDpz0Vib0pwz1TS399fCkry520xkK?= =?utf-8?q?0bTRTKiLZSgOBtDnq4enYzjHNO7gIDhZWvXsx22Cf9BR3Rx8Ciip5DVHD76sWU/5e?= =?utf-8?q?HVsZqmbUWCrLRb78Lp+CQ5Dj/NeVF1cJ1p2+CisuDHHCVpxjMd2J/7XwWYDerthvF?= =?utf-8?q?jvzlZ6oCBUodRCidLw9qmbr8DcARmDcAXgHMNVj2lrv1m70R8to+ru+rLhxgp+g3n?= =?utf-8?q?mTBHQ70c9qP55iWlfUA8KXu9VnnbUbgje2Ay3weUqStOzSn6V03hu+opl4w/neyHT?= =?utf-8?q?oUeJzb1L4rSU?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 44bdd7ce-165e-4d75-06dc-08da903fced5 X-MS-Exchange-CrossTenant-AuthSource: AM9PR04MB8793.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Sep 2022 19:41:35.5169 (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: R5UDaGVdToQz/7FT0fRcEHqasyVPUpFlKjHGlEjMVhdgr+m5Bj0/Dixp+q36zQGIRcazJ2sVZuIRkJ92pTONTA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB5059 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220906_124142_120111_BCF17141 X-CRM114-Status: GOOD ( 22.06 ) 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 ┌───────┐ ┌──────────┐ │ │ │ │ ┌─────────────┐ │ PCI │ │ PCI Host │ │ MSI │◄┐ │ EP │ │ │ │ Controller │ │ │ │ 3.MSI Write │ │ └────────┬────┘ └─┼───────┼───────────────────┤ │ ▲ │ │ │ ├─BAR_n │ │ └────────┼───────┼──────────────────►│ │ │ │ │ 2.Call Back │ │ │ │ │ write_msi_msg() │ │ │ │ │ │ │ │ └───┬───┘ └──────────┘ │ │ └───────────────────┘ 1.platform_msi_domain_alloc_irqs() There is no defined way of raising IRQs by PCI host to the PCI endpoint. Only define MSI/MSI-X to let EP notified RC status change. The memory assigned for BAR region by the PCI host is mapped to the message address of platform msi interrupt controller in PCI Endpoint. Such that, whenever the PCI host writes to the BAR region, it will trigger an IRQ in the Endpoint. Basic working follow as 1. EP function driver call platform_msi_domain_alloc_irqs() alloc a MSI irq from MSI controller with call back function write_msi_msg(); 2. write_msg_msg will config BAR and map to address defined in msi_msg; 3. Host side trigger an IRQ in Endpoint by write to BAR region. Add MSI support for pci-epf-vntb. Query if system has an MSI controller. Set up doorbell address according to struct msi_msg. So PCI RC can write this doorbell address to trigger EP side's IRQ. If no MSI controller exists, fall back to software polling. Signed-off-by: Frank Li --- drivers/pci/endpoint/functions/pci-epf-vntb.c | 155 +++++++++++++++--- 1 file changed, 128 insertions(+), 27 deletions(-) diff --git a/drivers/pci/endpoint/functions/pci-epf-vntb.c b/drivers/pci/endpoint/functions/pci-epf-vntb.c index 1466dd1904175..426205b980a09 100644 --- a/drivers/pci/endpoint/functions/pci-epf-vntb.c +++ b/drivers/pci/endpoint/functions/pci-epf-vntb.c @@ -44,6 +44,7 @@ #include #include #include +#include static struct workqueue_struct *kpcintb_workqueue; @@ -136,13 +137,15 @@ struct epf_ntb { struct epf_ntb_ctrl *reg; - phys_addr_t epf_db_phy; + phys_addr_t epf_db_phys; void __iomem *epf_db; phys_addr_t vpci_mw_phy[MAX_MW]; void __iomem *vpci_mw_addr[MAX_MW]; struct delayed_work cmd_handler; + + int msi_virqbase; }; #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group) @@ -253,13 +256,15 @@ static void epf_ntb_cmd_handler(struct work_struct *work) ntb = container_of(work, struct epf_ntb, cmd_handler.work); - for (i = 1; i < ntb->db_count; i++) { - if (readl(ntb->epf_db + i * 4)) { - if (readl(ntb->epf_db + i * 4)) - ntb->db |= 1 << (i - 1); + if (!ntb->epf_db_phys) { + for (i = 1; i < ntb->db_count; i++) { + if (readl(ntb->epf_db + i * 4)) { + if (readl(ntb->epf_db + i * 4)) + ntb->db |= 1 << (i - 1); - ntb_db_event(&ntb->ntb, i); - writel(0, ntb->epf_db + i * 4); + ntb_db_event(&ntb->ntb, i); + writel(0, ntb->epf_db + i * 4); + } } } @@ -454,11 +459,9 @@ static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb) ctrl->num_mws = ntb->num_mws; ntb->spad_size = spad_size; - ctrl->db_entry_size = 4; - for (i = 0; i < ntb->db_count; i++) { ntb->reg->db_data[i] = 1 + i; - ntb->reg->db_offset[i] = 0; + ntb->reg->db_offset[i] = 4 * i; } return 0; @@ -509,6 +512,28 @@ static int epf_ntb_configure_interrupt(struct epf_ntb *ntb) return 0; } +static int epf_ntb_db_size(struct epf_ntb *ntb) +{ + const struct pci_epc_features *epc_features; + size_t size = 4 * ntb->db_count; + u32 align; + + epc_features = pci_epc_get_features(ntb->epf->epc, + ntb->epf->func_no, + ntb->epf->vfunc_no); + align = epc_features->align; + + if (size < 128) + size = 128; + + if (align) + size = ALIGN(size, align); + else + size = roundup_pow_of_two(size); + + return size; +} + /** * epf_ntb_db_bar_init() - Configure Doorbell window BARs * @ntb: NTB device that facilitates communication between HOST and vHOST @@ -522,33 +547,32 @@ static int epf_ntb_db_bar_init(struct epf_ntb *ntb) struct pci_epf_bar *epf_bar; void __iomem *mw_addr; enum pci_barno barno; - size_t size = 4 * ntb->db_count; + size_t size; epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no); align = epc_features->align; - - if (size < 128) - size = 128; - - if (align) - size = ALIGN(size, align); - else - size = roundup_pow_of_two(size); + size = epf_ntb_db_size(ntb); barno = ntb->epf_ntb_bar[BAR_DB]; + epf_bar = &ntb->epf->bar[barno]; - mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, align, 0); - if (!mw_addr) { - dev_err(dev, "Failed to allocate OB address\n"); - return -ENOMEM; + if (ntb->epf_db_phys) { + mw_addr = NULL; + epf_bar->phys_addr = ntb->epf_db_phys; + epf_bar->barno = barno; + epf_bar->size = size; + } else { + mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, align, 0); + if (!mw_addr) { + dev_err(dev, "Failed to allocate door bell address\n"); + return -ENOMEM; + } } ntb->epf_db = mw_addr; - epf_bar = &ntb->epf->bar[barno]; - ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar); if (ret) { dev_err(dev, "Doorbell BAR set failed\n"); @@ -704,6 +728,82 @@ static int epf_ntb_init_epc_bar(struct epf_ntb *ntb) return 0; } +#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN +static void epf_ntb_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) +{ + struct epf_ntb *ntb = dev_get_drvdata(desc->dev); + struct epf_ntb_ctrl *reg = ntb->reg; + int size = epf_ntb_db_size(ntb); + u64 addr; + + addr = msg->address_hi; + addr <<= 32; + addr |= msg->address_lo; + + reg->db_data[desc->msi_index] = msg->data; + + if (desc->msi_index == 0) + ntb->epf_db_phys = round_down(addr, size); + + reg->db_offset[desc->msi_index] = addr - ntb->epf_db_phys; +} +#endif + +static irqreturn_t epf_ntb_interrupt_handler(int irq, void *data) +{ + struct epf_ntb *ntb = data; + int index; + + index = irq - ntb->msi_virqbase; + ntb->db |= 1 << (index - 1); + ntb_db_event(&ntb->ntb, index); + + return IRQ_HANDLED; +} + +static void epf_ntb_epc_msi_init(struct epf_ntb *ntb) +{ + struct device *dev = &ntb->epf->dev; + struct irq_domain *domain; + int virq; + int ret; + int i; + + domain = dev_get_msi_domain(ntb->epf->epc->dev.parent); + if (!domain) + return; + + dev_set_msi_domain(dev, domain); + +#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN + if (platform_msi_domain_alloc_irqs(&ntb->epf->dev, + ntb->db_count, + epf_ntb_write_msi_msg)) { + dev_info(dev, "Can't allocate MSI, fall back to poll mode\n"); + return; + } +#else + return; +#endif + dev_info(dev, "vntb use MSI as doorbell\n"); + + for (i = 0; i < ntb->db_count; i++) { + virq = msi_get_virq(dev, i); + ret = devm_request_irq(dev, virq, + epf_ntb_interrupt_handler, 0, + "vntb", ntb); + + if (ret) { + dev_err(dev, "devm_request_irq() failure, fall back to poll mode\n"); + ntb->epf_db_phys = 0; + break; + } + + if (!i) + ntb->msi_virqbase = virq; + } +} + /** * epf_ntb_epc_init() - Initialize NTB interface * @ntb: NTB device that facilitates communication between HOST and vHOST2 @@ -1299,14 +1399,15 @@ static int epf_ntb_bind(struct pci_epf *epf) goto err_bar_alloc; } + epf_set_drvdata(epf, ntb); + epf_ntb_epc_msi_init(ntb); + ret = epf_ntb_epc_init(ntb); if (ret) { dev_err(dev, "Failed to initialize EPC\n"); goto err_bar_alloc; } - epf_set_drvdata(epf, ntb); - pci_space[0] = (ntb->vntb_pid << 16) | ntb->vntb_vid; pci_vntb_table[0].vendor = ntb->vntb_vid; pci_vntb_table[0].device = ntb->vntb_pid;