blob: 10929cd43d5dcee5dc0410610752e83070005c3f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
James Bottomley388c8c12008-08-03 13:02:12 -05002/*
Christoph Hellwig704e8952017-04-13 09:06:42 +02003 * PCI IRQ handling code
James Bottomley388c8c12008-08-03 13:02:12 -05004 *
5 * Copyright (c) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
Christoph Hellwig704e8952017-04-13 09:06:42 +02006 * Copyright (C) 2017 Christoph Hellwig.
James Bottomley388c8c12008-08-03 13:02:12 -05007 */
8
9#include <linux/acpi.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040012#include <linux/export.h>
James Bottomley388c8c12008-08-03 13:02:12 -050013#include <linux/pci.h>
14
15static void pci_note_irq_problem(struct pci_dev *pdev, const char *reason)
16{
17 struct pci_dev *parent = to_pci_dev(pdev->dev.parent);
18
Joe Perches438be3c2012-10-28 01:05:49 -070019 dev_err(&pdev->dev,
20 "Potentially misrouted IRQ (Bridge %s %04x:%04x)\n",
21 dev_name(&parent->dev), parent->vendor, parent->device);
22 dev_err(&pdev->dev, "%s\n", reason);
23 dev_err(&pdev->dev, "Please report to linux-kernel@vger.kernel.org\n");
James Bottomley388c8c12008-08-03 13:02:12 -050024 WARN_ON(1);
25}
26
27/**
28 * pci_lost_interrupt - reports a lost PCI interrupt
29 * @pdev: device whose interrupt is lost
Bjorn Helgaasf7625982013-11-14 11:28:18 -070030 *
James Bottomley388c8c12008-08-03 13:02:12 -050031 * The primary function of this routine is to report a lost interrupt
32 * in a standard way which users can recognise (instead of blaming the
33 * driver).
34 *
35 * Returns:
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -030036 * a suggestion for fixing it (although the driver is not required to
James Bottomley388c8c12008-08-03 13:02:12 -050037 * act on this).
38 */
39enum pci_lost_interrupt_reason pci_lost_interrupt(struct pci_dev *pdev)
40{
41 if (pdev->msi_enabled || pdev->msix_enabled) {
42 enum pci_lost_interrupt_reason ret;
43
44 if (pdev->msix_enabled) {
45 pci_note_irq_problem(pdev, "MSIX routing failure");
46 ret = PCI_LOST_IRQ_DISABLE_MSIX;
47 } else {
48 pci_note_irq_problem(pdev, "MSI routing failure");
49 ret = PCI_LOST_IRQ_DISABLE_MSI;
50 }
51 return ret;
52 }
53#ifdef CONFIG_ACPI
54 if (!(acpi_disabled || acpi_noirq)) {
55 pci_note_irq_problem(pdev, "Potential ACPI misrouting please reboot with acpi=noirq");
56 /* currently no way to fix acpi on the fly */
57 return PCI_LOST_IRQ_DISABLE_ACPI;
58 }
59#endif
60 pci_note_irq_problem(pdev, "unknown cause (not MSI or ACPI)");
61 return PCI_LOST_IRQ_NO_INFORMATION;
62}
63EXPORT_SYMBOL(pci_lost_interrupt);
Christoph Hellwig704e8952017-04-13 09:06:42 +020064
65/**
66 * pci_request_irq - allocate an interrupt line for a PCI device
67 * @dev: PCI device to operate on
68 * @nr: device-relative interrupt vector index (0-based).
69 * @handler: Function to be called when the IRQ occurs.
70 * Primary handler for threaded interrupts.
71 * If NULL and thread_fn != NULL the default primary handler is
72 * installed.
73 * @thread_fn: Function called from the IRQ handler thread
74 * If NULL, no IRQ thread is created
75 * @dev_id: Cookie passed back to the handler function
76 * @fmt: Printf-like format string naming the handler
77 *
78 * This call allocates interrupt resources and enables the interrupt line and
79 * IRQ handling. From the point this call is made @handler and @thread_fn may
80 * be invoked. All interrupts requested using this function might be shared.
81 *
82 * @dev_id must not be NULL and must be globally unique.
83 */
84int pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler,
85 irq_handler_t thread_fn, void *dev_id, const char *fmt, ...)
86{
87 va_list ap;
88 int ret;
89 char *devname;
90
91 va_start(ap, fmt);
92 devname = kvasprintf(GFP_KERNEL, fmt, ap);
93 va_end(ap);
94
95 ret = request_threaded_irq(pci_irq_vector(dev, nr), handler, thread_fn,
96 IRQF_SHARED, devname, dev_id);
97 if (ret)
98 kfree(devname);
99 return ret;
100}
101EXPORT_SYMBOL(pci_request_irq);
102
103/**
104 * pci_free_irq - free an interrupt allocated with pci_request_irq
105 * @dev: PCI device to operate on
106 * @nr: device-relative interrupt vector index (0-based).
107 * @dev_id: Device identity to free
108 *
109 * Remove an interrupt handler. The handler is removed and if the interrupt
110 * line is no longer in use by any driver it is disabled. The caller must
111 * ensure the interrupt is disabled on the device before calling this function.
112 * The function does not return until any executing interrupts for this IRQ
113 * have completed.
114 *
115 * This function must not be called from interrupt context.
116 */
117void pci_free_irq(struct pci_dev *dev, unsigned int nr, void *dev_id)
118{
119 kfree(free_irq(pci_irq_vector(dev, nr), dev_id));
120}
121EXPORT_SYMBOL(pci_free_irq);