msi-howto.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. include:: <isonum.txt>
  3. ==========================
  4. The MSI Driver Guide HOWTO
  5. ==========================
  6. :Authors: Tom L Nguyen; Martine Silbermann; Matthew Wilcox
  7. :Copyright: 2003, 2008 Intel Corporation
  8. About this guide
  9. ================
  10. This guide describes the basics of Message Signaled Interrupts (MSIs),
  11. the advantages of using MSI over traditional interrupt mechanisms, how
  12. to change your driver to use MSI or MSI-X and some basic diagnostics to
  13. try if a device doesn't support MSIs.
  14. What are MSIs?
  15. ==============
  16. A Message Signaled Interrupt is a write from the device to a special
  17. address which causes an interrupt to be received by the CPU.
  18. The MSI capability was first specified in PCI 2.2 and was later enhanced
  19. in PCI 3.0 to allow each interrupt to be masked individually. The MSI-X
  20. capability was also introduced with PCI 3.0. It supports more interrupts
  21. per device than MSI and allows interrupts to be independently configured.
  22. Devices may support both MSI and MSI-X, but only one can be enabled at
  23. a time.
  24. Why use MSIs?
  25. =============
  26. There are three reasons why using MSIs can give an advantage over
  27. traditional pin-based interrupts.
  28. Pin-based PCI interrupts are often shared amongst several devices.
  29. To support this, the kernel must call each interrupt handler associated
  30. with an interrupt, which leads to reduced performance for the system as
  31. a whole. MSIs are never shared, so this problem cannot arise.
  32. When a device writes data to memory, then raises a pin-based interrupt,
  33. it is possible that the interrupt may arrive before all the data has
  34. arrived in memory (this becomes more likely with devices behind PCI-PCI
  35. bridges). In order to ensure that all the data has arrived in memory,
  36. the interrupt handler must read a register on the device which raised
  37. the interrupt. PCI transaction ordering rules require that all the data
  38. arrive in memory before the value may be returned from the register.
  39. Using MSIs avoids this problem as the interrupt-generating write cannot
  40. pass the data writes, so by the time the interrupt is raised, the driver
  41. knows that all the data has arrived in memory.
  42. PCI devices can only support a single pin-based interrupt per function.
  43. Often drivers have to query the device to find out what event has
  44. occurred, slowing down interrupt handling for the common case. With
  45. MSIs, a device can support more interrupts, allowing each interrupt
  46. to be specialised to a different purpose. One possible design gives
  47. infrequent conditions (such as errors) their own interrupt which allows
  48. the driver to handle the normal interrupt handling path more efficiently.
  49. Other possible designs include giving one interrupt to each packet queue
  50. in a network card or each port in a storage controller.
  51. How to use MSIs
  52. ===============
  53. PCI devices are initialised to use pin-based interrupts. The device
  54. driver has to set up the device to use MSI or MSI-X. Not all machines
  55. support MSIs correctly, and for those machines, the APIs described below
  56. will simply fail and the device will continue to use pin-based interrupts.
  57. Include kernel support for MSIs
  58. -------------------------------
  59. To support MSI or MSI-X, the kernel must be built with the CONFIG_PCI_MSI
  60. option enabled. This option is only available on some architectures,
  61. and it may depend on some other options also being set. For example,
  62. on x86, you must also enable X86_UP_APIC or SMP in order to see the
  63. CONFIG_PCI_MSI option.
  64. Using MSI
  65. ---------
  66. Most of the hard work is done for the driver in the PCI layer. The driver
  67. simply has to request that the PCI layer set up the MSI capability for this
  68. device.
  69. To automatically use MSI or MSI-X interrupt vectors, use the following
  70. function::
  71. int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
  72. unsigned int max_vecs, unsigned int flags);
  73. which allocates up to max_vecs interrupt vectors for a PCI device. It
  74. returns the number of vectors allocated or a negative error. If the device
  75. has a requirements for a minimum number of vectors the driver can pass a
  76. min_vecs argument set to this limit, and the PCI core will return -ENOSPC
  77. if it can't meet the minimum number of vectors.
  78. The flags argument is used to specify which type of interrupt can be used
  79. by the device and the driver (PCI_IRQ_LEGACY, PCI_IRQ_MSI, PCI_IRQ_MSIX).
  80. A convenient short-hand (PCI_IRQ_ALL_TYPES) is also available to ask for
  81. any possible kind of interrupt. If the PCI_IRQ_AFFINITY flag is set,
  82. pci_alloc_irq_vectors() will spread the interrupts around the available CPUs.
  83. To get the Linux IRQ numbers passed to request_irq() and free_irq() and the
  84. vectors, use the following function::
  85. int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
  86. Any allocated resources should be freed before removing the device using
  87. the following function::
  88. void pci_free_irq_vectors(struct pci_dev *dev);
  89. If a device supports both MSI-X and MSI capabilities, this API will use the
  90. MSI-X facilities in preference to the MSI facilities. MSI-X supports any
  91. number of interrupts between 1 and 2048. In contrast, MSI is restricted to
  92. a maximum of 32 interrupts (and must be a power of two). In addition, the
  93. MSI interrupt vectors must be allocated consecutively, so the system might
  94. not be able to allocate as many vectors for MSI as it could for MSI-X. On
  95. some platforms, MSI interrupts must all be targeted at the same set of CPUs
  96. whereas MSI-X interrupts can all be targeted at different CPUs.
  97. If a device supports neither MSI-X or MSI it will fall back to a single
  98. legacy IRQ vector.
  99. The typical usage of MSI or MSI-X interrupts is to allocate as many vectors
  100. as possible, likely up to the limit supported by the device. If nvec is
  101. larger than the number supported by the device it will automatically be
  102. capped to the supported limit, so there is no need to query the number of
  103. vectors supported beforehand::
  104. nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_ALL_TYPES)
  105. if (nvec < 0)
  106. goto out_err;
  107. If a driver is unable or unwilling to deal with a variable number of MSI
  108. interrupts it can request a particular number of interrupts by passing that
  109. number to pci_alloc_irq_vectors() function as both 'min_vecs' and
  110. 'max_vecs' parameters::
  111. ret = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_ALL_TYPES);
  112. if (ret < 0)
  113. goto out_err;
  114. The most notorious example of the request type described above is enabling
  115. the single MSI mode for a device. It could be done by passing two 1s as
  116. 'min_vecs' and 'max_vecs'::
  117. ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  118. if (ret < 0)
  119. goto out_err;
  120. Some devices might not support using legacy line interrupts, in which case
  121. the driver can specify that only MSI or MSI-X is acceptable::
  122. nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_MSI | PCI_IRQ_MSIX);
  123. if (nvec < 0)
  124. goto out_err;
  125. Legacy APIs
  126. -----------
  127. The following old APIs to enable and disable MSI or MSI-X interrupts should
  128. not be used in new code::
  129. pci_enable_msi() /* deprecated */
  130. pci_disable_msi() /* deprecated */
  131. pci_enable_msix_range() /* deprecated */
  132. pci_enable_msix_exact() /* deprecated */
  133. pci_disable_msix() /* deprecated */
  134. Additionally there are APIs to provide the number of supported MSI or MSI-X
  135. vectors: pci_msi_vec_count() and pci_msix_vec_count(). In general these
  136. should be avoided in favor of letting pci_alloc_irq_vectors() cap the
  137. number of vectors. If you have a legitimate special use case for the count
  138. of vectors we might have to revisit that decision and add a
  139. pci_nr_irq_vectors() helper that handles MSI and MSI-X transparently.
  140. Considerations when using MSIs
  141. ------------------------------
  142. Spinlocks
  143. ~~~~~~~~~
  144. Most device drivers have a per-device spinlock which is taken in the
  145. interrupt handler. With pin-based interrupts or a single MSI, it is not
  146. necessary to disable interrupts (Linux guarantees the same interrupt will
  147. not be re-entered). If a device uses multiple interrupts, the driver
  148. must disable interrupts while the lock is held. If the device sends
  149. a different interrupt, the driver will deadlock trying to recursively
  150. acquire the spinlock. Such deadlocks can be avoided by using
  151. spin_lock_irqsave() or spin_lock_irq() which disable local interrupts
  152. and acquire the lock (see Documentation/kernel-hacking/locking.rst).
  153. How to tell whether MSI/MSI-X is enabled on a device
  154. ----------------------------------------------------
  155. Using 'lspci -v' (as root) may show some devices with "MSI", "Message
  156. Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
  157. has an 'Enable' flag which is followed with either "+" (enabled)
  158. or "-" (disabled).
  159. MSI quirks
  160. ==========
  161. Several PCI chipsets or devices are known not to support MSIs.
  162. The PCI stack provides three ways to disable MSIs:
  163. 1. globally
  164. 2. on all devices behind a specific bridge
  165. 3. on a single device
  166. Disabling MSIs globally
  167. -----------------------
  168. Some host chipsets simply don't support MSIs properly. If we're
  169. lucky, the manufacturer knows this and has indicated it in the ACPI
  170. FADT table. In this case, Linux automatically disables MSIs.
  171. Some boards don't include this information in the table and so we have
  172. to detect them ourselves. The complete list of these is found near the
  173. quirk_disable_all_msi() function in drivers/pci/quirks.c.
  174. If you have a board which has problems with MSIs, you can pass pci=nomsi
  175. on the kernel command line to disable MSIs on all devices. It would be
  176. in your best interests to report the problem to [email protected]
  177. including a full 'lspci -v' so we can add the quirks to the kernel.
  178. Disabling MSIs below a bridge
  179. -----------------------------
  180. Some PCI bridges are not able to route MSIs between busses properly.
  181. In this case, MSIs must be disabled on all devices behind the bridge.
  182. Some bridges allow you to enable MSIs by changing some bits in their
  183. PCI configuration space (especially the Hypertransport chipsets such
  184. as the nVidia nForce and Serverworks HT2000). As with host chipsets,
  185. Linux mostly knows about them and automatically enables MSIs if it can.
  186. If you have a bridge unknown to Linux, you can enable
  187. MSIs in configuration space using whatever method you know works, then
  188. enable MSIs on that bridge by doing::
  189. echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
  190. where $bridge is the PCI address of the bridge you've enabled (eg
  191. 0000:00:0e.0).
  192. To disable MSIs, echo 0 instead of 1. Changing this value should be
  193. done with caution as it could break interrupt handling for all devices
  194. below this bridge.
  195. Again, please notify [email protected] of any bridges that need
  196. special handling.
  197. Disabling MSIs on a single device
  198. ---------------------------------
  199. Some devices are known to have faulty MSI implementations. Usually this
  200. is handled in the individual device driver, but occasionally it's necessary
  201. to handle this with a quirk. Some drivers have an option to disable use
  202. of MSI. While this is a convenient workaround for the driver author,
  203. it is not good practice, and should not be emulated.
  204. Finding why MSIs are disabled on a device
  205. -----------------------------------------
  206. From the above three sections, you can see that there are many reasons
  207. why MSIs may not be enabled for a given device. Your first step should
  208. be to examine your dmesg carefully to determine whether MSIs are enabled
  209. for your machine. You should also check your .config to be sure you
  210. have enabled CONFIG_PCI_MSI.
  211. Then, 'lspci -t' gives the list of bridges above a device. Reading
  212. `/sys/bus/pci/devices/*/msi_bus` will tell you whether MSIs are enabled (1)
  213. or disabled (0). If 0 is found in any of the msi_bus files belonging
  214. to bridges between the PCI root and the device, MSIs are disabled.
  215. It is also worth checking the device driver to see whether it supports MSIs.
  216. For example, it may contain calls to pci_alloc_irq_vectors() with the
  217. PCI_IRQ_MSI or PCI_IRQ_MSIX flags.