1 // SPDX-License-Identifier: GPL-2.0
3 * PCI IRQ handling code
5 * Copyright (c) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6 * Copyright (C) 2017 Christoph Hellwig.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/export.h>
13 #include <linux/interrupt.h>
14 #include <linux/pci.h>
19 * pci_request_irq - allocate an interrupt line for a PCI device
20 * @dev: PCI device to operate on
21 * @nr: device-relative interrupt vector index (0-based).
22 * @handler: Function to be called when the IRQ occurs.
23 * Primary handler for threaded interrupts.
24 * If NULL and thread_fn != NULL the default primary handler is
26 * @thread_fn: Function called from the IRQ handler thread
27 * If NULL, no IRQ thread is created
28 * @dev_id: Cookie passed back to the handler function
29 * @fmt: Printf-like format string naming the handler
31 * This call allocates interrupt resources and enables the interrupt line and
32 * IRQ handling. From the point this call is made @handler and @thread_fn may
33 * be invoked. All interrupts requested using this function might be shared.
35 * @dev_id must not be NULL and must be globally unique.
37 int pci_request_irq(struct pci_dev
*dev
, unsigned int nr
, irq_handler_t handler
,
38 irq_handler_t thread_fn
, void *dev_id
, const char *fmt
, ...)
43 unsigned long irqflags
= IRQF_SHARED
;
46 irqflags
|= IRQF_ONESHOT
;
49 devname
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
54 ret
= request_threaded_irq(pci_irq_vector(dev
, nr
), handler
, thread_fn
,
55 irqflags
, devname
, dev_id
);
60 EXPORT_SYMBOL(pci_request_irq
);
63 * pci_free_irq - free an interrupt allocated with pci_request_irq
64 * @dev: PCI device to operate on
65 * @nr: device-relative interrupt vector index (0-based).
66 * @dev_id: Device identity to free
68 * Remove an interrupt handler. The handler is removed and if the interrupt
69 * line is no longer in use by any driver it is disabled. The caller must
70 * ensure the interrupt is disabled on the device before calling this function.
71 * The function does not return until any executing interrupts for this IRQ
74 * This function must not be called from interrupt context.
76 void pci_free_irq(struct pci_dev
*dev
, unsigned int nr
, void *dev_id
)
78 kfree(free_irq(pci_irq_vector(dev
, nr
), dev_id
));
80 EXPORT_SYMBOL(pci_free_irq
);
83 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
84 * @dev: the PCI device
85 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
87 * Perform INTx swizzling for a device behind one level of bridge. This is
88 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
89 * behind bridges on add-in cards. For devices with ARI enabled, the slot
90 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
91 * the PCI Express Base Specification, Revision 2.1)
93 u8
pci_swizzle_interrupt_pin(const struct pci_dev
*dev
, u8 pin
)
97 if (pci_ari_enabled(dev
->bus
))
100 slot
= PCI_SLOT(dev
->devfn
);
102 return (((pin
- 1) + slot
) % 4) + 1;
105 int pci_get_interrupt_pin(struct pci_dev
*dev
, struct pci_dev
**bridge
)
113 while (!pci_is_root_bus(dev
->bus
)) {
114 pin
= pci_swizzle_interrupt_pin(dev
, pin
);
115 dev
= dev
->bus
->self
;
122 * pci_common_swizzle - swizzle INTx all the way to root bridge
123 * @dev: the PCI device
124 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
126 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
127 * bridges all the way up to a PCI root bus.
129 u8
pci_common_swizzle(struct pci_dev
*dev
, u8
*pinp
)
133 while (!pci_is_root_bus(dev
->bus
)) {
134 pin
= pci_swizzle_interrupt_pin(dev
, pin
);
135 dev
= dev
->bus
->self
;
138 return PCI_SLOT(dev
->devfn
);
140 EXPORT_SYMBOL_GPL(pci_common_swizzle
);
142 void pci_assign_irq(struct pci_dev
*dev
)
147 struct pci_host_bridge
*hbrg
= pci_find_host_bridge(dev
->bus
);
149 if (!(hbrg
->map_irq
)) {
150 pci_dbg(dev
, "runtime IRQ mapping not provided by arch\n");
155 * If this device is not on the primary bus, we need to figure out
156 * which interrupt pin it will come in on. We know which slot it
157 * will come in on because that slot is where the bridge is. Each
158 * time the interrupt line passes through a PCI-PCI bridge we must
159 * apply the swizzle function.
161 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
162 /* Cope with illegal. */
167 /* Follow the chain of bridges, swizzling as we go. */
168 if (hbrg
->swizzle_irq
)
169 slot
= (*(hbrg
->swizzle_irq
))(dev
, &pin
);
172 * If a swizzling function is not used, map_irq() must
175 irq
= (*(hbrg
->map_irq
))(dev
, slot
, pin
);
181 pci_dbg(dev
, "assign IRQ: got %d\n", dev
->irq
);
184 * Always tell the device, so the driver knows what is the real IRQ
185 * to use; the device does not use it.
187 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, irq
);
190 static bool pci_check_and_set_intx_mask(struct pci_dev
*dev
, bool mask
)
192 struct pci_bus
*bus
= dev
->bus
;
193 bool mask_updated
= true;
194 u32 cmd_status_dword
;
200 * We do a single dword read to retrieve both command and status.
201 * Document assumptions that make this possible.
203 BUILD_BUG_ON(PCI_COMMAND
% 4);
204 BUILD_BUG_ON(PCI_COMMAND
+ 2 != PCI_STATUS
);
206 raw_spin_lock_irqsave(&pci_lock
, flags
);
208 bus
->ops
->read(bus
, dev
->devfn
, PCI_COMMAND
, 4, &cmd_status_dword
);
210 irq_pending
= (cmd_status_dword
>> 16) & PCI_STATUS_INTERRUPT
;
213 * Check interrupt status register to see whether our device
214 * triggered the interrupt (when masking) or the next IRQ is
215 * already pending (when unmasking).
217 if (mask
!= irq_pending
) {
218 mask_updated
= false;
222 origcmd
= cmd_status_dword
;
223 newcmd
= origcmd
& ~PCI_COMMAND_INTX_DISABLE
;
225 newcmd
|= PCI_COMMAND_INTX_DISABLE
;
226 if (newcmd
!= origcmd
)
227 bus
->ops
->write(bus
, dev
->devfn
, PCI_COMMAND
, 2, newcmd
);
230 raw_spin_unlock_irqrestore(&pci_lock
, flags
);
236 * pci_check_and_mask_intx - mask INTx on pending interrupt
237 * @dev: the PCI device to operate on
239 * Check if the device dev has its INTx line asserted, mask it and return
240 * true in that case. False is returned if no interrupt was pending.
242 bool pci_check_and_mask_intx(struct pci_dev
*dev
)
244 return pci_check_and_set_intx_mask(dev
, true);
246 EXPORT_SYMBOL_GPL(pci_check_and_mask_intx
);
249 * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
250 * @dev: the PCI device to operate on
252 * Check if the device dev has its INTx line asserted, unmask it if not and
253 * return true. False is returned and the mask remains active if there was
254 * still an interrupt pending.
256 bool pci_check_and_unmask_intx(struct pci_dev
*dev
)
258 return pci_check_and_set_intx_mask(dev
, false);
260 EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx
);
263 * pcibios_penalize_isa_irq - penalize an ISA IRQ
264 * @irq: ISA IRQ to penalize
265 * @active: IRQ active or not
267 * Permits the platform to provide architecture-specific functionality when
268 * penalizing ISA IRQs. This is the default implementation. Architecture
269 * implementations can override this.
271 void __weak
pcibios_penalize_isa_irq(int irq
, int active
) {}
273 int __weak
pcibios_alloc_irq(struct pci_dev
*dev
)
278 void __weak
pcibios_free_irq(struct pci_dev
*dev
)