Bjorn Helgaas | 7328c8f | 2018-01-26 11:45:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 3 | * Support routines for initializing a PCI subsystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/pci.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/ioport.h> |
| 15 | #include <linux/cache.h> |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 16 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 18 | void pci_assign_irq(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 20 | u8 pin; |
| 21 | u8 slot = -1; |
Andreas Block | 691cd0c | 2007-02-05 16:36:07 -0800 | [diff] [blame] | 22 | int irq = 0; |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 23 | struct pci_host_bridge *hbrg = pci_find_host_bridge(dev->bus); |
| 24 | |
| 25 | if (!(hbrg->map_irq)) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 26 | pci_dbg(dev, "runtime IRQ mapping not provided by arch\n"); |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 27 | return; |
| 28 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Pranay Sanghai | 9a0a141 | 2021-09-18 13:18:02 -0700 | [diff] [blame] | 30 | /* |
| 31 | * If this device is not on the primary bus, we need to figure out |
| 32 | * which interrupt pin it will come in on. We know which slot it |
| 33 | * will come in on because that slot is where the bridge is. Each |
| 34 | * time the interrupt line passes through a PCI-PCI bridge we must |
| 35 | * apply the swizzle function. |
| 36 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin); |
Andreas Block | 691cd0c | 2007-02-05 16:36:07 -0800 | [diff] [blame] | 38 | /* Cope with illegal. */ |
| 39 | if (pin > 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | pin = 1; |
| 41 | |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 42 | if (pin) { |
Pranay Sanghai | 9a0a141 | 2021-09-18 13:18:02 -0700 | [diff] [blame] | 43 | /* Follow the chain of bridges, swizzling as we go. */ |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 44 | if (hbrg->swizzle_irq) |
| 45 | slot = (*(hbrg->swizzle_irq))(dev, &pin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 47 | /* |
Pranay Sanghai | 9a0a141 | 2021-09-18 13:18:02 -0700 | [diff] [blame] | 48 | * If a swizzling function is not used, map_irq() must |
| 49 | * ignore slot. |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 50 | */ |
| 51 | irq = (*(hbrg->map_irq))(dev, slot, pin); |
Andreas Block | 691cd0c | 2007-02-05 16:36:07 -0800 | [diff] [blame] | 52 | if (irq == -1) |
| 53 | irq = 0; |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | dev->irq = irq; |
| 56 | |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 57 | pci_dbg(dev, "assign IRQ: got %d\n", dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Pranay Sanghai | 9a0a141 | 2021-09-18 13:18:02 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Always tell the device, so the driver knows what is the real IRQ |
| 61 | * to use; the device does not use it. |
| 62 | */ |
Bjorn Helgaas | 606799c | 2017-08-10 12:49:57 -0500 | [diff] [blame] | 63 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |