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 | |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/pci.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/cache.h> |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 17 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 19 | void pci_assign_irq(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 21 | u8 pin; |
| 22 | u8 slot = -1; |
Andreas Block | 691cd0c | 2007-02-05 16:36:07 -0800 | [diff] [blame] | 23 | int irq = 0; |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 24 | struct pci_host_bridge *hbrg = pci_find_host_bridge(dev->bus); |
| 25 | |
| 26 | if (!(hbrg->map_irq)) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 27 | pci_dbg(dev, "runtime IRQ mapping not provided by arch\n"); |
Matthew Minter | 47a650f | 2017-06-28 15:14:02 -0500 | [diff] [blame] | 28 | return; |
| 29 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -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 'cos 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 | |
| 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) { |
Andreas Block | 691cd0c | 2007-02-05 16:36:07 -0800 | [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 | /* |
| 48 | * If a swizzling function is not used map_irq must |
| 49 | * ignore slot |
| 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 | |
| 59 | /* Always tell the device, so the driver knows what is |
| 60 | the real IRQ to use; the device does not use it. */ |
Bjorn Helgaas | 606799c | 2017-08-10 12:49:57 -0500 | [diff] [blame] | 61 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |