MSI-HOWTO.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. The MSI Driver Guide HOWTO
  2. Tom L Nguyen tom.l.nguyen@intel.com
  3. 10/03/2003
  4. Revised Feb 12, 2004 by Martine Silbermann
  5. email: Martine.Silbermann@hp.com
  6. Revised Jun 25, 2004 by Tom L Nguyen
  7. Revised Jul 9, 2008 by Matthew Wilcox <willy@linux.intel.com>
  8. Copyright 2003, 2008 Intel Corporation
  9. 1. About this guide
  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. 2. What are MSIs?
  15. A Message Signaled Interrupt is a write from the device to a special
  16. address which causes an interrupt to be received by the CPU.
  17. The MSI capability was first specified in PCI 2.2 and was later enhanced
  18. in PCI 3.0 to allow each interrupt to be masked individually. The MSI-X
  19. capability was also introduced with PCI 3.0. It supports more interrupts
  20. per device than MSI and allows interrupts to be independently configured.
  21. Devices may support both MSI and MSI-X, but only one can be enabled at
  22. a time.
  23. 3. Why use MSIs?
  24. There are three reasons why using MSIs can give an advantage over
  25. traditional pin-based interrupts.
  26. Pin-based PCI interrupts are often shared amongst several devices.
  27. To support this, the kernel must call each interrupt handler associated
  28. with an interrupt, which leads to reduced performance for the system as
  29. a whole. MSIs are never shared, so this problem cannot arise.
  30. When a device writes data to memory, then raises a pin-based interrupt,
  31. it is possible that the interrupt may arrive before all the data has
  32. arrived in memory (this becomes more likely with devices behind PCI-PCI
  33. bridges). In order to ensure that all the data has arrived in memory,
  34. the interrupt handler must read a register on the device which raised
  35. the interrupt. PCI transaction ordering rules require that all the data
  36. arrive in memory before the value may be returned from the register.
  37. Using MSIs avoids this problem as the interrupt-generating write cannot
  38. pass the data writes, so by the time the interrupt is raised, the driver
  39. knows that all the data has arrived in memory.
  40. PCI devices can only support a single pin-based interrupt per function.
  41. Often drivers have to query the device to find out what event has
  42. occurred, slowing down interrupt handling for the common case. With
  43. MSIs, a device can support more interrupts, allowing each interrupt
  44. to be specialised to a different purpose. One possible design gives
  45. infrequent conditions (such as errors) their own interrupt which allows
  46. the driver to handle the normal interrupt handling path more efficiently.
  47. Other possible designs include giving one interrupt to each packet queue
  48. in a network card or each port in a storage controller.
  49. 4. How to use MSIs
  50. PCI devices are initialised to use pin-based interrupts. The device
  51. driver has to set up the device to use MSI or MSI-X. Not all machines
  52. support MSIs correctly, and for those machines, the APIs described below
  53. will simply fail and the device will continue to use pin-based interrupts.
  54. 4.1 Include kernel support for MSIs
  55. To support MSI or MSI-X, the kernel must be built with the CONFIG_PCI_MSI
  56. option enabled. This option is only available on some architectures,
  57. and it may depend on some other options also being set. For example,
  58. on x86, you must also enable X86_UP_APIC or SMP in order to see the
  59. CONFIG_PCI_MSI option.
  60. 4.2 Using MSI
  61. Most of the hard work is done for the driver in the PCI layer. It simply
  62. has to request that the PCI layer set up the MSI capability for this
  63. device.
  64. 4.2.1 pci_enable_msi
  65. int pci_enable_msi(struct pci_dev *dev)
  66. A successful call allocates ONE interrupt to the device, regardless
  67. of how many MSIs the device supports. The device is switched from
  68. pin-based interrupt mode to MSI mode. The dev->irq number is changed
  69. to a new number which represents the message signaled interrupt;
  70. consequently, this function should be called before the driver calls
  71. request_irq(), because an MSI is delivered via a vector that is
  72. different from the vector of a pin-based interrupt.
  73. 4.2.2 pci_enable_msi_block
  74. int pci_enable_msi_block(struct pci_dev *dev, int count)
  75. This variation on the above call allows a device driver to request multiple
  76. MSIs. The MSI specification only allows interrupts to be allocated in
  77. powers of two, up to a maximum of 2^5 (32).
  78. If this function returns 0, it has succeeded in allocating at least as many
  79. interrupts as the driver requested (it may have allocated more in order
  80. to satisfy the power-of-two requirement). In this case, the function
  81. enables MSI on this device and updates dev->irq to be the lowest of
  82. the new interrupts assigned to it. The other interrupts assigned to
  83. the device are in the range dev->irq to dev->irq + count - 1.
  84. If this function returns a negative number, it indicates an error and
  85. the driver should not attempt to request any more MSI interrupts for
  86. this device. If this function returns a positive number, it is
  87. less than 'count' and indicates the number of interrupts that could have
  88. been allocated. In neither case is the irq value updated or the device
  89. switched into MSI mode.
  90. The device driver must decide what action to take if
  91. pci_enable_msi_block() returns a value less than the number requested.
  92. For instance, the driver could still make use of fewer interrupts;
  93. in this case the driver should call pci_enable_msi_block()
  94. again. Note that it is not guaranteed to succeed, even when the
  95. 'count' has been reduced to the value returned from a previous call to
  96. pci_enable_msi_block(). This is because there are multiple constraints
  97. on the number of vectors that can be allocated; pci_enable_msi_block()
  98. returns as soon as it finds any constraint that doesn't allow the
  99. call to succeed.
  100. 4.2.3 pci_enable_msi_block_auto
  101. int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *count)
  102. This variation on pci_enable_msi() call allows a device driver to request
  103. the maximum possible number of MSIs. The MSI specification only allows
  104. interrupts to be allocated in powers of two, up to a maximum of 2^5 (32).
  105. If this function returns a positive number, it indicates that it has
  106. succeeded and the returned value is the number of allocated interrupts. In
  107. this case, the function enables MSI on this device and updates dev->irq to
  108. be the lowest of the new interrupts assigned to it. The other interrupts
  109. assigned to the device are in the range dev->irq to dev->irq + returned
  110. value - 1.
  111. If this function returns a negative number, it indicates an error and
  112. the driver should not attempt to request any more MSI interrupts for
  113. this device.
  114. If the device driver needs to know the number of interrupts the device
  115. supports it can pass the pointer count where that number is stored. The
  116. device driver must decide what action to take if pci_enable_msi_block_auto()
  117. succeeds, but returns a value less than the number of interrupts supported.
  118. If the device driver does not need to know the number of interrupts
  119. supported, it can set the pointer count to NULL.
  120. 4.2.4 pci_disable_msi
  121. void pci_disable_msi(struct pci_dev *dev)
  122. This function should be used to undo the effect of pci_enable_msi() or
  123. pci_enable_msi_block() or pci_enable_msi_block_auto(). Calling it restores
  124. dev->irq to the pin-based interrupt number and frees the previously
  125. allocated message signaled interrupt(s). The interrupt may subsequently be
  126. assigned to another device, so drivers should not cache the value of
  127. dev->irq.
  128. Before calling this function, a device driver must always call free_irq()
  129. on any interrupt for which it previously called request_irq().
  130. Failure to do so results in a BUG_ON(), leaving the device with
  131. MSI enabled and thus leaking its vector.
  132. 4.3 Using MSI-X
  133. The MSI-X capability is much more flexible than the MSI capability.
  134. It supports up to 2048 interrupts, each of which can be controlled
  135. independently. To support this flexibility, drivers must use an array of
  136. `struct msix_entry':
  137. struct msix_entry {
  138. u16 vector; /* kernel uses to write alloc vector */
  139. u16 entry; /* driver uses to specify entry */
  140. };
  141. This allows for the device to use these interrupts in a sparse fashion;
  142. for example, it could use interrupts 3 and 1027 and yet allocate only a
  143. two-element array. The driver is expected to fill in the 'entry' value
  144. in each element of the array to indicate for which entries the kernel
  145. should assign interrupts; it is invalid to fill in two entries with the
  146. same number.
  147. 4.3.1 pci_enable_msix
  148. int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
  149. Calling this function asks the PCI subsystem to allocate 'nvec' MSIs.
  150. The 'entries' argument is a pointer to an array of msix_entry structs
  151. which should be at least 'nvec' entries in size. On success, the
  152. device is switched into MSI-X mode and the function returns 0.
  153. The 'vector' member in each entry is populated with the interrupt number;
  154. the driver should then call request_irq() for each 'vector' that it
  155. decides to use. The device driver is responsible for keeping track of the
  156. interrupts assigned to the MSI-X vectors so it can free them again later.
  157. If this function returns a negative number, it indicates an error and
  158. the driver should not attempt to allocate any more MSI-X interrupts for
  159. this device. If it returns a positive number, it indicates the maximum
  160. number of interrupt vectors that could have been allocated. See example
  161. below.
  162. This function, in contrast with pci_enable_msi(), does not adjust
  163. dev->irq. The device will not generate interrupts for this interrupt
  164. number once MSI-X is enabled.
  165. Device drivers should normally call this function once per device
  166. during the initialization phase.
  167. It is ideal if drivers can cope with a variable number of MSI-X interrupts;
  168. there are many reasons why the platform may not be able to provide the
  169. exact number that a driver asks for.
  170. A request loop to achieve that might look like:
  171. static int foo_driver_enable_msix(struct foo_adapter *adapter, int nvec)
  172. {
  173. while (nvec >= FOO_DRIVER_MINIMUM_NVEC) {
  174. rc = pci_enable_msix(adapter->pdev,
  175. adapter->msix_entries, nvec);
  176. if (rc > 0)
  177. nvec = rc;
  178. else
  179. return rc;
  180. }
  181. return -ENOSPC;
  182. }
  183. 4.3.2 pci_disable_msix
  184. void pci_disable_msix(struct pci_dev *dev)
  185. This function should be used to undo the effect of pci_enable_msix(). It frees
  186. the previously allocated message signaled interrupts. The interrupts may
  187. subsequently be assigned to another device, so drivers should not cache
  188. the value of the 'vector' elements over a call to pci_disable_msix().
  189. Before calling this function, a device driver must always call free_irq()
  190. on any interrupt for which it previously called request_irq().
  191. Failure to do so results in a BUG_ON(), leaving the device with
  192. MSI-X enabled and thus leaking its vector.
  193. 4.3.3 The MSI-X Table
  194. The MSI-X capability specifies a BAR and offset within that BAR for the
  195. MSI-X Table. This address is mapped by the PCI subsystem, and should not
  196. be accessed directly by the device driver. If the driver wishes to
  197. mask or unmask an interrupt, it should call disable_irq() / enable_irq().
  198. 4.4 Handling devices implementing both MSI and MSI-X capabilities
  199. If a device implements both MSI and MSI-X capabilities, it can
  200. run in either MSI mode or MSI-X mode, but not both simultaneously.
  201. This is a requirement of the PCI spec, and it is enforced by the
  202. PCI layer. Calling pci_enable_msi() when MSI-X is already enabled or
  203. pci_enable_msix() when MSI is already enabled results in an error.
  204. If a device driver wishes to switch between MSI and MSI-X at runtime,
  205. it must first quiesce the device, then switch it back to pin-interrupt
  206. mode, before calling pci_enable_msi() or pci_enable_msix() and resuming
  207. operation. This is not expected to be a common operation but may be
  208. useful for debugging or testing during development.
  209. 4.5 Considerations when using MSIs
  210. 4.5.1 Choosing between MSI-X and MSI
  211. If your device supports both MSI-X and MSI capabilities, you should use
  212. the MSI-X facilities in preference to the MSI facilities. As mentioned
  213. above, MSI-X supports any number of interrupts between 1 and 2048.
  214. In constrast, MSI is restricted to a maximum of 32 interrupts (and
  215. must be a power of two). In addition, the MSI interrupt vectors must
  216. be allocated consecutively, so the system might not be able to allocate
  217. as many vectors for MSI as it could for MSI-X. On some platforms, MSI
  218. interrupts must all be targeted at the same set of CPUs whereas MSI-X
  219. interrupts can all be targeted at different CPUs.
  220. 4.5.2 Spinlocks
  221. Most device drivers have a per-device spinlock which is taken in the
  222. interrupt handler. With pin-based interrupts or a single MSI, it is not
  223. necessary to disable interrupts (Linux guarantees the same interrupt will
  224. not be re-entered). If a device uses multiple interrupts, the driver
  225. must disable interrupts while the lock is held. If the device sends
  226. a different interrupt, the driver will deadlock trying to recursively
  227. acquire the spinlock.
  228. There are two solutions. The first is to take the lock with
  229. spin_lock_irqsave() or spin_lock_irq() (see
  230. Documentation/DocBook/kernel-locking). The second is to specify
  231. IRQF_DISABLED to request_irq() so that the kernel runs the entire
  232. interrupt routine with interrupts disabled.
  233. If your MSI interrupt routine does not hold the lock for the whole time
  234. it is running, the first solution may be best. The second solution is
  235. normally preferred as it avoids making two transitions from interrupt
  236. disabled to enabled and back again.
  237. 4.6 How to tell whether MSI/MSI-X is enabled on a device
  238. Using 'lspci -v' (as root) may show some devices with "MSI", "Message
  239. Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
  240. has an 'Enable' flag which is followed with either "+" (enabled)
  241. or "-" (disabled).
  242. 5. MSI quirks
  243. Several PCI chipsets or devices are known not to support MSIs.
  244. The PCI stack provides three ways to disable MSIs:
  245. 1. globally
  246. 2. on all devices behind a specific bridge
  247. 3. on a single device
  248. 5.1. Disabling MSIs globally
  249. Some host chipsets simply don't support MSIs properly. If we're
  250. lucky, the manufacturer knows this and has indicated it in the ACPI
  251. FADT table. In this case, Linux automatically disables MSIs.
  252. Some boards don't include this information in the table and so we have
  253. to detect them ourselves. The complete list of these is found near the
  254. quirk_disable_all_msi() function in drivers/pci/quirks.c.
  255. If you have a board which has problems with MSIs, you can pass pci=nomsi
  256. on the kernel command line to disable MSIs on all devices. It would be
  257. in your best interests to report the problem to linux-pci@vger.kernel.org
  258. including a full 'lspci -v' so we can add the quirks to the kernel.
  259. 5.2. Disabling MSIs below a bridge
  260. Some PCI bridges are not able to route MSIs between busses properly.
  261. In this case, MSIs must be disabled on all devices behind the bridge.
  262. Some bridges allow you to enable MSIs by changing some bits in their
  263. PCI configuration space (especially the Hypertransport chipsets such
  264. as the nVidia nForce and Serverworks HT2000). As with host chipsets,
  265. Linux mostly knows about them and automatically enables MSIs if it can.
  266. If you have a bridge unknown to Linux, you can enable
  267. MSIs in configuration space using whatever method you know works, then
  268. enable MSIs on that bridge by doing:
  269. echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
  270. where $bridge is the PCI address of the bridge you've enabled (eg
  271. 0000:00:0e.0).
  272. To disable MSIs, echo 0 instead of 1. Changing this value should be
  273. done with caution as it could break interrupt handling for all devices
  274. below this bridge.
  275. Again, please notify linux-pci@vger.kernel.org of any bridges that need
  276. special handling.
  277. 5.3. Disabling MSIs on a single device
  278. Some devices are known to have faulty MSI implementations. Usually this
  279. is handled in the individual device driver, but occasionally it's necessary
  280. to handle this with a quirk. Some drivers have an option to disable use
  281. of MSI. While this is a convenient workaround for the driver author,
  282. it is not good practise, and should not be emulated.
  283. 5.4. Finding why MSIs are disabled on a device
  284. From the above three sections, you can see that there are many reasons
  285. why MSIs may not be enabled for a given device. Your first step should
  286. be to examine your dmesg carefully to determine whether MSIs are enabled
  287. for your machine. You should also check your .config to be sure you
  288. have enabled CONFIG_PCI_MSI.
  289. Then, 'lspci -t' gives the list of bridges above a device. Reading
  290. /sys/bus/pci/devices/*/msi_bus will tell you whether MSIs are enabled (1)
  291. or disabled (0). If 0 is found in any of the msi_bus files belonging
  292. to bridges between the PCI root and the device, MSIs are disabled.
  293. It is also worth checking the device driver to see whether it supports MSIs.
  294. For example, it may contain calls to pci_enable_msi(), pci_enable_msix() or
  295. pci_enable_msi_block().