xref: /openbmc/linux/drivers/pci/setup-irq.c (revision 01a6e126)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	drivers/pci/setup-irq.c
4  *
5  * Extruded from code written by
6  *      Dave Rusling (david.rusling@reo.mts.dec.com)
7  *      David Mosberger (davidm@cs.arizona.edu)
8  *	David Miller (davem@redhat.com)
9  *
10  * Support routines for initializing a PCI subsystem.
11  */
12 
13 
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/cache.h>
19 #include "pci.h"
20 
21 void pci_assign_irq(struct pci_dev *dev)
22 {
23 	u8 pin;
24 	u8 slot = -1;
25 	int irq = 0;
26 	struct pci_host_bridge *hbrg = pci_find_host_bridge(dev->bus);
27 
28 	if (!(hbrg->map_irq)) {
29 		pci_dbg(dev, "runtime IRQ mapping not provided by arch\n");
30 		return;
31 	}
32 
33 	/* If this device is not on the primary bus, we need to figure out
34 	   which interrupt pin it will come in on.   We know which slot it
35 	   will come in on 'cos that slot is where the bridge is.   Each
36 	   time the interrupt line passes through a PCI-PCI bridge we must
37 	   apply the swizzle function.  */
38 
39 	pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
40 	/* Cope with illegal. */
41 	if (pin > 4)
42 		pin = 1;
43 
44 	if (pin) {
45 		/* Follow the chain of bridges, swizzling as we go.  */
46 		if (hbrg->swizzle_irq)
47 			slot = (*(hbrg->swizzle_irq))(dev, &pin);
48 
49 		/*
50 		 * If a swizzling function is not used map_irq must
51 		 * ignore slot
52 		 */
53 		irq = (*(hbrg->map_irq))(dev, slot, pin);
54 		if (irq == -1)
55 			irq = 0;
56 	}
57 	dev->irq = irq;
58 
59 	pci_dbg(dev, "assign IRQ: got %d\n", dev->irq);
60 
61 	/* Always tell the device, so the driver knows what is
62 	   the real IRQ to use; the device does not use it. */
63 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
64 }
65