xref: /OK3568_Linux_fs/kernel/drivers/pci/irq.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * PCI IRQ handling code
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6*4882a593Smuzhiyun  * Copyright (C) 2017 Christoph Hellwig.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/export.h>
12*4882a593Smuzhiyun #include <linux/pci.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /**
15*4882a593Smuzhiyun  * pci_request_irq - allocate an interrupt line for a PCI device
16*4882a593Smuzhiyun  * @dev:	PCI device to operate on
17*4882a593Smuzhiyun  * @nr:		device-relative interrupt vector index (0-based).
18*4882a593Smuzhiyun  * @handler:	Function to be called when the IRQ occurs.
19*4882a593Smuzhiyun  *		Primary handler for threaded interrupts.
20*4882a593Smuzhiyun  *		If NULL and thread_fn != NULL the default primary handler is
21*4882a593Smuzhiyun  *		installed.
22*4882a593Smuzhiyun  * @thread_fn:	Function called from the IRQ handler thread
23*4882a593Smuzhiyun  *		If NULL, no IRQ thread is created
24*4882a593Smuzhiyun  * @dev_id:	Cookie passed back to the handler function
25*4882a593Smuzhiyun  * @fmt:	Printf-like format string naming the handler
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * This call allocates interrupt resources and enables the interrupt line and
28*4882a593Smuzhiyun  * IRQ handling. From the point this call is made @handler and @thread_fn may
29*4882a593Smuzhiyun  * be invoked.  All interrupts requested using this function might be shared.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * @dev_id must not be NULL and must be globally unique.
32*4882a593Smuzhiyun  */
pci_request_irq(struct pci_dev * dev,unsigned int nr,irq_handler_t handler,irq_handler_t thread_fn,void * dev_id,const char * fmt,...)33*4882a593Smuzhiyun int pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler,
34*4882a593Smuzhiyun 		irq_handler_t thread_fn, void *dev_id, const char *fmt, ...)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	va_list ap;
37*4882a593Smuzhiyun 	int ret;
38*4882a593Smuzhiyun 	char *devname;
39*4882a593Smuzhiyun 	unsigned long irqflags = IRQF_SHARED;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	if (!handler)
42*4882a593Smuzhiyun 		irqflags |= IRQF_ONESHOT;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	va_start(ap, fmt);
45*4882a593Smuzhiyun 	devname = kvasprintf(GFP_KERNEL, fmt, ap);
46*4882a593Smuzhiyun 	va_end(ap);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	ret = request_threaded_irq(pci_irq_vector(dev, nr), handler, thread_fn,
49*4882a593Smuzhiyun 				   irqflags, devname, dev_id);
50*4882a593Smuzhiyun 	if (ret)
51*4882a593Smuzhiyun 		kfree(devname);
52*4882a593Smuzhiyun 	return ret;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun EXPORT_SYMBOL(pci_request_irq);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun  * pci_free_irq - free an interrupt allocated with pci_request_irq
58*4882a593Smuzhiyun  * @dev:	PCI device to operate on
59*4882a593Smuzhiyun  * @nr:		device-relative interrupt vector index (0-based).
60*4882a593Smuzhiyun  * @dev_id:	Device identity to free
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Remove an interrupt handler. The handler is removed and if the interrupt
63*4882a593Smuzhiyun  * line is no longer in use by any driver it is disabled.  The caller must
64*4882a593Smuzhiyun  * ensure the interrupt is disabled on the device before calling this function.
65*4882a593Smuzhiyun  * The function does not return until any executing interrupts for this IRQ
66*4882a593Smuzhiyun  * have completed.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * This function must not be called from interrupt context.
69*4882a593Smuzhiyun  */
pci_free_irq(struct pci_dev * dev,unsigned int nr,void * dev_id)70*4882a593Smuzhiyun void pci_free_irq(struct pci_dev *dev, unsigned int nr, void *dev_id)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	kfree(free_irq(pci_irq_vector(dev, nr), dev_id));
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun EXPORT_SYMBOL(pci_free_irq);
75