irq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI IRQ handling code
  4. *
  5. * Copyright (c) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
  6. * Copyright (C) 2017 Christoph Hellwig.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/export.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pci.h>
  14. #include "pci.h"
  15. /**
  16. * pci_request_irq - allocate an interrupt line for a PCI device
  17. * @dev: PCI device to operate on
  18. * @nr: device-relative interrupt vector index (0-based).
  19. * @handler: Function to be called when the IRQ occurs.
  20. * Primary handler for threaded interrupts.
  21. * If NULL and thread_fn != NULL the default primary handler is
  22. * installed.
  23. * @thread_fn: Function called from the IRQ handler thread
  24. * If NULL, no IRQ thread is created
  25. * @dev_id: Cookie passed back to the handler function
  26. * @fmt: Printf-like format string naming the handler
  27. *
  28. * This call allocates interrupt resources and enables the interrupt line and
  29. * IRQ handling. From the point this call is made @handler and @thread_fn may
  30. * be invoked. All interrupts requested using this function might be shared.
  31. *
  32. * @dev_id must not be NULL and must be globally unique.
  33. */
  34. int pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler,
  35. irq_handler_t thread_fn, void *dev_id, const char *fmt, ...)
  36. {
  37. va_list ap;
  38. int ret;
  39. char *devname;
  40. unsigned long irqflags = IRQF_SHARED;
  41. if (!handler)
  42. irqflags |= IRQF_ONESHOT;
  43. va_start(ap, fmt);
  44. devname = kvasprintf(GFP_KERNEL, fmt, ap);
  45. va_end(ap);
  46. if (!devname)
  47. return -ENOMEM;
  48. ret = request_threaded_irq(pci_irq_vector(dev, nr), handler, thread_fn,
  49. irqflags, devname, dev_id);
  50. if (ret)
  51. kfree(devname);
  52. return ret;
  53. }
  54. EXPORT_SYMBOL(pci_request_irq);
  55. /**
  56. * pci_free_irq - free an interrupt allocated with pci_request_irq
  57. * @dev: PCI device to operate on
  58. * @nr: device-relative interrupt vector index (0-based).
  59. * @dev_id: Device identity to free
  60. *
  61. * Remove an interrupt handler. The handler is removed and if the interrupt
  62. * line is no longer in use by any driver it is disabled. The caller must
  63. * ensure the interrupt is disabled on the device before calling this function.
  64. * The function does not return until any executing interrupts for this IRQ
  65. * have completed.
  66. *
  67. * This function must not be called from interrupt context.
  68. */
  69. void pci_free_irq(struct pci_dev *dev, unsigned int nr, void *dev_id)
  70. {
  71. kfree(free_irq(pci_irq_vector(dev, nr), dev_id));
  72. }
  73. EXPORT_SYMBOL(pci_free_irq);
  74. /**
  75. * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
  76. * @dev: the PCI device
  77. * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
  78. *
  79. * Perform INTx swizzling for a device behind one level of bridge. This is
  80. * required by section 9.1 of the PCI-to-PCI bridge specification for devices
  81. * behind bridges on add-in cards. For devices with ARI enabled, the slot
  82. * number is always 0 (see the Implementation Note in section 2.2.8.1 of
  83. * the PCI Express Base Specification, Revision 2.1)
  84. */
  85. u8 pci_swizzle_interrupt_pin(const struct pci_dev *dev, u8 pin)
  86. {
  87. int slot;
  88. if (pci_ari_enabled(dev->bus))
  89. slot = 0;
  90. else
  91. slot = PCI_SLOT(dev->devfn);
  92. return (((pin - 1) + slot) % 4) + 1;
  93. }
  94. int pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
  95. {
  96. u8 pin;
  97. pin = dev->pin;
  98. if (!pin)
  99. return -1;
  100. while (!pci_is_root_bus(dev->bus)) {
  101. pin = pci_swizzle_interrupt_pin(dev, pin);
  102. dev = dev->bus->self;
  103. }
  104. *bridge = dev;
  105. return pin;
  106. }
  107. /**
  108. * pci_common_swizzle - swizzle INTx all the way to root bridge
  109. * @dev: the PCI device
  110. * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
  111. *
  112. * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
  113. * bridges all the way up to a PCI root bus.
  114. */
  115. u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
  116. {
  117. u8 pin = *pinp;
  118. while (!pci_is_root_bus(dev->bus)) {
  119. pin = pci_swizzle_interrupt_pin(dev, pin);
  120. dev = dev->bus->self;
  121. }
  122. *pinp = pin;
  123. return PCI_SLOT(dev->devfn);
  124. }
  125. EXPORT_SYMBOL_GPL(pci_common_swizzle);
  126. void pci_assign_irq(struct pci_dev *dev)
  127. {
  128. u8 pin;
  129. u8 slot = -1;
  130. int irq = 0;
  131. struct pci_host_bridge *hbrg = pci_find_host_bridge(dev->bus);
  132. if (!(hbrg->map_irq)) {
  133. pci_dbg(dev, "runtime IRQ mapping not provided by arch\n");
  134. return;
  135. }
  136. /*
  137. * If this device is not on the primary bus, we need to figure out
  138. * which interrupt pin it will come in on. We know which slot it
  139. * will come in on because that slot is where the bridge is. Each
  140. * time the interrupt line passes through a PCI-PCI bridge we must
  141. * apply the swizzle function.
  142. */
  143. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  144. /* Cope with illegal. */
  145. if (pin > 4)
  146. pin = 1;
  147. if (pin) {
  148. /* Follow the chain of bridges, swizzling as we go. */
  149. if (hbrg->swizzle_irq)
  150. slot = (*(hbrg->swizzle_irq))(dev, &pin);
  151. /*
  152. * If a swizzling function is not used, map_irq() must
  153. * ignore slot.
  154. */
  155. irq = (*(hbrg->map_irq))(dev, slot, pin);
  156. if (irq == -1)
  157. irq = 0;
  158. }
  159. dev->irq = irq;
  160. pci_dbg(dev, "assign IRQ: got %d\n", dev->irq);
  161. /*
  162. * Always tell the device, so the driver knows what is the real IRQ
  163. * to use; the device does not use it.
  164. */
  165. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  166. }
  167. static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask)
  168. {
  169. struct pci_bus *bus = dev->bus;
  170. bool mask_updated = true;
  171. u32 cmd_status_dword;
  172. u16 origcmd, newcmd;
  173. unsigned long flags;
  174. bool irq_pending;
  175. /*
  176. * We do a single dword read to retrieve both command and status.
  177. * Document assumptions that make this possible.
  178. */
  179. BUILD_BUG_ON(PCI_COMMAND % 4);
  180. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  181. raw_spin_lock_irqsave(&pci_lock, flags);
  182. bus->ops->read(bus, dev->devfn, PCI_COMMAND, 4, &cmd_status_dword);
  183. irq_pending = (cmd_status_dword >> 16) & PCI_STATUS_INTERRUPT;
  184. /*
  185. * Check interrupt status register to see whether our device
  186. * triggered the interrupt (when masking) or the next IRQ is
  187. * already pending (when unmasking).
  188. */
  189. if (mask != irq_pending) {
  190. mask_updated = false;
  191. goto done;
  192. }
  193. origcmd = cmd_status_dword;
  194. newcmd = origcmd & ~PCI_COMMAND_INTX_DISABLE;
  195. if (mask)
  196. newcmd |= PCI_COMMAND_INTX_DISABLE;
  197. if (newcmd != origcmd)
  198. bus->ops->write(bus, dev->devfn, PCI_COMMAND, 2, newcmd);
  199. done:
  200. raw_spin_unlock_irqrestore(&pci_lock, flags);
  201. return mask_updated;
  202. }
  203. /**
  204. * pci_check_and_mask_intx - mask INTx on pending interrupt
  205. * @dev: the PCI device to operate on
  206. *
  207. * Check if the device dev has its INTx line asserted, mask it and return
  208. * true in that case. False is returned if no interrupt was pending.
  209. */
  210. bool pci_check_and_mask_intx(struct pci_dev *dev)
  211. {
  212. return pci_check_and_set_intx_mask(dev, true);
  213. }
  214. EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
  215. /**
  216. * pci_check_and_unmask_intx - unmask INTx if no interrupt is pending
  217. * @dev: the PCI device to operate on
  218. *
  219. * Check if the device dev has its INTx line asserted, unmask it if not and
  220. * return true. False is returned and the mask remains active if there was
  221. * still an interrupt pending.
  222. */
  223. bool pci_check_and_unmask_intx(struct pci_dev *dev)
  224. {
  225. return pci_check_and_set_intx_mask(dev, false);
  226. }
  227. EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx);
  228. /**
  229. * pcibios_penalize_isa_irq - penalize an ISA IRQ
  230. * @irq: ISA IRQ to penalize
  231. * @active: IRQ active or not
  232. *
  233. * Permits the platform to provide architecture-specific functionality when
  234. * penalizing ISA IRQs. This is the default implementation. Architecture
  235. * implementations can override this.
  236. */
  237. void __weak pcibios_penalize_isa_irq(int irq, int active) {}
  238. int __weak pcibios_alloc_irq(struct pci_dev *dev)
  239. {
  240. return 0;
  241. }
  242. void __weak pcibios_free_irq(struct pci_dev *dev)
  243. {
  244. }