Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6572808rwb; Mon, 5 Dec 2022 14:30:39 -0800 (PST) X-Google-Smtp-Source: AA0mqf5z8JxNrxFpqULAPlvfB2eDy2hRJdXlw+R7kVPmrD4r1/CSkCLe+RcPZIXs3q9ax0eNRRws X-Received: by 2002:aa7:8d5a:0:b0:562:7f16:7407 with SMTP id s26-20020aa78d5a000000b005627f167407mr89326526pfe.15.1670279438999; Mon, 05 Dec 2022 14:30:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670279438; cv=none; d=google.com; s=arc-20160816; b=Q+kFHIX6K0ii6fZPRPgiP8ouP2W55IstTAC+AKdtCvjLoJMxZO9cOiXFEE8SGcK2SC LVmAZa9yjuB9EhBNN7A8NJkkxUaP0BQlyUF87GsJnIEnXSuUvLsEPDQEaJHSx9WE5C7L hq7tr7NtlHls3w3AJaXid2NFR9DJbsdDHkyJSC6M6/5cQNBXQiTkUi1wHpq6qPUWLvfl 0i6sUdHUorpAIzz4G7+Zp4UnY3F1pzEyLHtoW9a5gqpQeKERj8m8zwnM5175Gk/xaF/d IJg8QUBfEfNY3hVUy9EHLCkcHlnR1Wor1cQ/KnEyLwIxgre10GAFs4qXfdysAa8aTiel jGOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=O0qgrS9EuulO5jnl+4Zw1LlIhN5IGgt9g2X5sKFKWZM=; b=0/wu3SvmkOzeMryW8vqPR235DbncdokQZJHxIuC9Viz/Sh0CE6wb4s1e9YtvWPmUk3 HJJ2nWgTip5E2HSaNUSs3ikcpUW+3oFA1CXeM+G1OIfwnGFKoxp4jCi61SjazU1xr3ky hgKt/4Hs6WslTrtWs3TKcrr1uo9FdaWUGgzM6s8ksgmj71nbLqSi+qBQw8drluqrbVho qTPPrrwLc9lHkUNOFyD2BFr09Hthp2QrSRq2YXDgamusZzDTo7ZymztqsgLnXaOiL/rx aeL9EJpTBHv1EQxLW5ezTA20iWAm0GvaYd4ICsVdMjZy2WM7wnDknV1pRLhuLSeSTdCK IetA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=DKY4HWh9; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 21-20020a630c55000000b0046004f18c6csi15261243pgm.456.2022.12.05.14.30.26; Mon, 05 Dec 2022 14:30:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=DKY4HWh9; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232064AbiLEVma (ORCPT + 80 others); Mon, 5 Dec 2022 16:42:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233804AbiLEVlx (ORCPT ); Mon, 5 Dec 2022 16:41:53 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 262AC2B619; Mon, 5 Dec 2022 13:41:52 -0800 (PST) Date: Mon, 05 Dec 2022 21:41:47 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1670276507; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O0qgrS9EuulO5jnl+4Zw1LlIhN5IGgt9g2X5sKFKWZM=; b=DKY4HWh9oBZiwHE6OlTu5outU5/kL+kZYhcubsTwwOCEGcdwwAPZgNFp7FR4lUqtbhGzC/ k8eLZ/hMdp4BmrkJIa/eEGdmexant14NwjYBI55KUgCUItOMjTBADcAQYQ1rveMaJm8wd5 jEYhmgfac6Vah7Jxw7WNY/QW0hw2BTgcuAiuIKoaszcLhyWwedzvxN0vdSQIBevmtW3qzP ZkQvIv5pqD/WVj9AGsBp2EiBtm2qjlvoeM4pT/6Qtwjkeox+rtwwd5NahnuEr6NT467u2z 4AYaVG0UflWZZmyqTEz5cdiZ/+jJ9TQxrYgsvTLN1t+bnvhpq5rzLxbbTd0YCQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1670276507; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O0qgrS9EuulO5jnl+4Zw1LlIhN5IGgt9g2X5sKFKWZM=; b=apJkme7oZT2cX6PYJrI/0bXW0zJqCnuvE8QL5d9S8vCLUCFThjPlVuVARsXQivPItYve0p QXIBYqyT88qsAMCA== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: irq/core] PCI/MSI: Provide post-enable dynamic allocation interfaces for MSI-X Cc: Thomas Gleixner , Kevin Tian , Bjorn Helgaas , Marc Zyngier , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20211126232735.547996838@linutronix.de> References: <20211126232735.547996838@linutronix.de> MIME-Version: 1.0 Message-ID: <167027650746.4906.2615383258348029502.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the irq/core branch of tip: Commit-ID: 34026364df8eca05ee32e706a2c014511a19af02 Gitweb: https://git.kernel.org/tip/34026364df8eca05ee32e706a2c014511a19af02 Author: Thomas Gleixner AuthorDate: Fri, 25 Nov 2022 00:26:24 +01:00 Committer: Thomas Gleixner CommitterDate: Mon, 05 Dec 2022 22:22:34 +01:00 PCI/MSI: Provide post-enable dynamic allocation interfaces for MSI-X MSI-X vectors can be allocated after the initial MSI-X enablement, but this needs explicit support of the underlying interrupt domains. Provide a function to query the ability and functions to allocate/free individual vectors post-enable. The allocation can either request a specific index in the MSI-X table or with the index argument MSI_ANY_INDEX it allocates the next free vector. The return value is a struct msi_map which on success contains both index and the Linux interrupt number. In case of failure index is negative and the Linux interrupt number is 0. The allocation function is for a single MSI-X index at a time as that's sufficient for the most urgent use case VFIO to get rid of the 'disable MSI-X, reallocate, enable-MSI-X' cycle which is prone to lost interrupts and redirections to the legacy and obviously unhandled INTx. As single index allocation is also sufficient for the use cases Jason Gunthorpe pointed out: Allocation of a MSI-X or IMS vector for a network queue. See Link below. Signed-off-by: Thomas Gleixner Reviewed-by: Kevin Tian Acked-by: Bjorn Helgaas Acked-by: Marc Zyngier Link: https://lore.kernel.org/all/20211126232735.547996838@linutronix.de Link: https://lore.kernel.org/r/20221124232326.731233614@linutronix.de --- drivers/pci/msi/api.c | 67 ++++++++++++++++++++++++++++++++++++- drivers/pci/msi/irqdomain.c | 3 +- include/linux/pci.h | 6 +++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c index 2d46a0c..c8816db 100644 --- a/drivers/pci/msi/api.c +++ b/drivers/pci/msi/api.c @@ -113,6 +113,73 @@ int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, EXPORT_SYMBOL(pci_enable_msix_range); /** + * pci_msix_can_alloc_dyn - Query whether dynamic allocation after enabling + * MSI-X is supported + * + * @dev: PCI device to operate on + * + * Return: True if supported, false otherwise + */ +bool pci_msix_can_alloc_dyn(struct pci_dev *dev) +{ + if (!dev->msix_cap) + return false; + + return pci_msi_domain_supports(dev, MSI_FLAG_PCI_MSIX_ALLOC_DYN, DENY_LEGACY); +} +EXPORT_SYMBOL_GPL(pci_msix_can_alloc_dyn); + +/** + * pci_msix_alloc_irq_at - Allocate an MSI-X interrupt after enabling MSI-X + * at a given MSI-X vector index or any free vector index + * + * @dev: PCI device to operate on + * @index: Index to allocate. If @index == MSI_ANY_INDEX this allocates + * the next free index in the MSI-X table + * @affdesc: Optional pointer to an affinity descriptor structure. NULL otherwise + * + * Return: A struct msi_map + * + * On success msi_map::index contains the allocated index (>= 0) and + * msi_map::virq contains the allocated Linux interrupt number (> 0). + * + * On fail msi_map::index contains the error code and msi_map::virq + * is set to 0. + */ +struct msi_map pci_msix_alloc_irq_at(struct pci_dev *dev, unsigned int index, + const struct irq_affinity_desc *affdesc) +{ + struct msi_map map = { .index = -ENOTSUPP }; + + if (!dev->msix_enabled) + return map; + + if (!pci_msix_can_alloc_dyn(dev)) + return map; + + return msi_domain_alloc_irq_at(&dev->dev, MSI_DEFAULT_DOMAIN, index, affdesc, NULL); +} +EXPORT_SYMBOL_GPL(pci_msix_alloc_irq_at); + +/** + * pci_msix_free_irq - Free an interrupt on a PCI/MSIX interrupt domain + * which was allocated via pci_msix_alloc_irq_at() + * + * @dev: The PCI device to operate on + * @map: A struct msi_map describing the interrupt to free + * as returned from the allocation function. + */ +void pci_msix_free_irq(struct pci_dev *dev, struct msi_map map) +{ + if (WARN_ON_ONCE(map.index < 0 || map.virq <= 0)) + return; + if (WARN_ON_ONCE(!pci_msix_can_alloc_dyn(dev))) + return; + msi_domain_free_irqs_range(&dev->dev, MSI_DEFAULT_DOMAIN, map.index, map.index); +} +EXPORT_SYMBOL_GPL(pci_msix_free_irq); + +/** * pci_disable_msix() - Disable MSI-X interrupt mode on device * @dev: the PCI device to operate on * diff --git a/drivers/pci/msi/irqdomain.c b/drivers/pci/msi/irqdomain.c index 8afaef1..deb1930 100644 --- a/drivers/pci/msi/irqdomain.c +++ b/drivers/pci/msi/irqdomain.c @@ -225,7 +225,8 @@ static const struct msi_domain_template pci_msix_template = { }, .info = { - .flags = MSI_COMMON_FLAGS | MSI_FLAG_PCI_MSIX, + .flags = MSI_COMMON_FLAGS | MSI_FLAG_PCI_MSIX | + MSI_FLAG_PCI_MSIX_ALLOC_DYN, .bus_token = DOMAIN_BUS_PCI_DEVICE_MSIX, }, }; diff --git a/include/linux/pci.h b/include/linux/pci.h index 243e48f..68b14ba 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -1559,6 +1560,11 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, unsigned int max_vecs, unsigned int flags, struct irq_affinity *affd); +bool pci_msix_can_alloc_dyn(struct pci_dev *dev); +struct msi_map pci_msix_alloc_irq_at(struct pci_dev *dev, unsigned int index, + const struct irq_affinity_desc *affdesc); +void pci_msix_free_irq(struct pci_dev *pdev, struct msi_map map); + void pci_free_irq_vectors(struct pci_dev *dev); int pci_irq_vector(struct pci_dev *dev, unsigned int nr); const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev, int vec);