blob: c55df2911136c90d37944616cfeebaeca4954a96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001 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
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04007 Revised Jul 9, 2008 by Matthew Wilcox <willy@linux.intel.com>
8 Copyright 2003, 2008 Intel Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
101. About this guide
11
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040012This guide describes the basics of Message Signaled Interrupts (MSIs),
13the advantages of using MSI over traditional interrupt mechanisms, how
14to change your driver to use MSI or MSI-X and some basic diagnostics to
15try if a device doesn't support MSIs.
Randy Dunlap2500e7a2005-11-07 01:01:03 -080016
Randy Dunlap2500e7a2005-11-07 01:01:03 -080017
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400182. What are MSIs?
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040020A Message Signaled Interrupt is a write from the device to a special
21address which causes an interrupt to be received by the CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040023The MSI capability was first specified in PCI 2.2 and was later enhanced
24in PCI 3.0 to allow each interrupt to be masked individually. The MSI-X
25capability was also introduced with PCI 3.0. It supports more interrupts
26per device than MSI and allows interrupts to be independently configured.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040028Devices may support both MSI and MSI-X, but only one can be enabled at
29a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400323. Why use MSIs?
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040034There are three reasons why using MSIs can give an advantage over
35traditional pin-based interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040037Pin-based PCI interrupts are often shared amongst several devices.
38To support this, the kernel must call each interrupt handler associated
39with an interrupt, which leads to reduced performance for the system as
40a whole. MSIs are never shared, so this problem cannot arise.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040042When a device writes data to memory, then raises a pin-based interrupt,
43it is possible that the interrupt may arrive before all the data has
44arrived in memory (this becomes more likely with devices behind PCI-PCI
45bridges). In order to ensure that all the data has arrived in memory,
46the interrupt handler must read a register on the device which raised
47the interrupt. PCI transaction ordering rules require that all the data
Michael Witten891f6922011-07-14 17:53:54 +000048arrive in memory before the value may be returned from the register.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040049Using MSIs avoids this problem as the interrupt-generating write cannot
50pass the data writes, so by the time the interrupt is raised, the driver
51knows that all the data has arrived in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040053PCI devices can only support a single pin-based interrupt per function.
54Often drivers have to query the device to find out what event has
55occurred, slowing down interrupt handling for the common case. With
56MSIs, a device can support more interrupts, allowing each interrupt
57to be specialised to a different purpose. One possible design gives
58infrequent conditions (such as errors) their own interrupt which allows
59the driver to handle the normal interrupt handling path more efficiently.
60Other possible designs include giving one interrupt to each packet queue
61in a network card or each port in a storage controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400644. How to use MSIs
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040066PCI devices are initialised to use pin-based interrupts. The device
67driver has to set up the device to use MSI or MSI-X. Not all machines
68support MSIs correctly, and for those machines, the APIs described below
69will simply fail and the device will continue to use pin-based interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400714.1 Include kernel support for MSIs
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040073To support MSI or MSI-X, the kernel must be built with the CONFIG_PCI_MSI
74option enabled. This option is only available on some architectures,
75and it may depend on some other options also being set. For example,
76on x86, you must also enable X86_UP_APIC or SMP in order to see the
77CONFIG_PCI_MSI option.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400794.2 Using MSI
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Hellwigaff17162016-07-12 18:20:17 +090081Most of the hard work is done for the driver in the PCI layer. The driver
82simply has to request that the PCI layer set up the MSI capability for this
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040083device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Christoph Hellwigaff17162016-07-12 18:20:17 +090085To automatically use MSI or MSI-X interrupt vectors, use the following
86function:
Alexander Gordeev7918b2d2014-02-13 10:47:51 -070087
Christoph Hellwigaff17162016-07-12 18:20:17 +090088 int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
89 unsigned int max_vecs, unsigned int flags);
Alexander Gordeev7918b2d2014-02-13 10:47:51 -070090
Christoph Hellwigaff17162016-07-12 18:20:17 +090091which allocates up to max_vecs interrupt vectors for a PCI device. It
92returns the number of vectors allocated or a negative error. If the device
93has a requirements for a minimum number of vectors the driver can pass a
94min_vecs argument set to this limit, and the PCI core will return -ENOSPC
95if it can't meet the minimum number of vectors.
Alexander Gordeev7918b2d2014-02-13 10:47:51 -070096
Christoph Hellwigaff17162016-07-12 18:20:17 +090097The flags argument should normally be set to 0, but can be used to pass the
98PCI_IRQ_NOMSI and PCI_IRQ_NOMSIX flag in case a device claims to support
99MSI or MSI-X, but the support is broken, or to pass PCI_IRQ_NOLEGACY in
100case the device does not support legacy interrupt lines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900102By default this function will spread the interrupts around the available
103CPUs, but this feature can be disabled by passing the PCI_IRQ_NOAFFINITY
104flag.
105
Christoph Hellwigaff17162016-07-12 18:20:17 +0900106To get the Linux IRQ numbers passed to request_irq() and free_irq() and the
107vectors, use the following function:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Christoph Hellwigaff17162016-07-12 18:20:17 +0900109 int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Christoph Hellwigaff17162016-07-12 18:20:17 +0900111Any allocated resources should be freed before removing the device using
112the following function:
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400113
Christoph Hellwigaff17162016-07-12 18:20:17 +0900114 void pci_free_irq_vectors(struct pci_dev *dev);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400115
Christoph Hellwigaff17162016-07-12 18:20:17 +0900116If a device supports both MSI-X and MSI capabilities, this API will use the
117MSI-X facilities in preference to the MSI facilities. MSI-X supports any
118number of interrupts between 1 and 2048. In contrast, MSI is restricted to
119a maximum of 32 interrupts (and must be a power of two). In addition, the
120MSI interrupt vectors must be allocated consecutively, so the system might
121not be able to allocate as many vectors for MSI as it could for MSI-X. On
122some platforms, MSI interrupts must all be targeted at the same set of CPUs
123whereas MSI-X interrupts can all be targeted at different CPUs.
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400124
Christoph Hellwigaff17162016-07-12 18:20:17 +0900125If a device supports neither MSI-X or MSI it will fall back to a single
126legacy IRQ vector.
Alexander Gordeev302a2522013-12-30 08:28:16 +0100127
Christoph Hellwigaff17162016-07-12 18:20:17 +0900128The typical usage of MSI or MSI-X interrupts is to allocate as many vectors
129as possible, likely up to the limit supported by the device. If nvec is
130larger than the number supported by the device it will automatically be
131capped to the supported limit, so there is no need to query the number of
132vectors supported beforehand:
Alexander Gordeev302a2522013-12-30 08:28:16 +0100133
Christoph Hellwigaff17162016-07-12 18:20:17 +0900134 nvec = pci_alloc_irq_vectors(pdev, 1, nvec, 0);
135 if (nvec < 0)
136 goto out_err;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100137
138If a driver is unable or unwilling to deal with a variable number of MSI
Christoph Hellwigaff17162016-07-12 18:20:17 +0900139interrupts it can request a particular number of interrupts by passing that
140number to pci_alloc_irq_vectors() function as both 'min_vecs' and
141'max_vecs' parameters:
Alexander Gordeev302a2522013-12-30 08:28:16 +0100142
Christoph Hellwigaff17162016-07-12 18:20:17 +0900143 ret = pci_alloc_irq_vectors(pdev, nvec, nvec, 0);
144 if (ret < 0)
145 goto out_err;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100146
Christoph Hellwigaff17162016-07-12 18:20:17 +0900147The most notorious example of the request type described above is enabling
148the single MSI mode for a device. It could be done by passing two 1s as
149'min_vecs' and 'max_vecs':
Alexander Gordeev3ce4e862014-02-13 10:48:02 -0700150
Christoph Hellwigaff17162016-07-12 18:20:17 +0900151 ret = pci_alloc_irq_vectors(pdev, 1, 1, 0);
152 if (ret < 0)
153 goto out_err;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100154
Christoph Hellwigaff17162016-07-12 18:20:17 +0900155Some devices might not support using legacy line interrupts, in which case
156the PCI_IRQ_NOLEGACY flag can be used to fail the request if the platform
157can't provide MSI or MSI-X interrupts:
Alexander Gordeev302a2522013-12-30 08:28:16 +0100158
Christoph Hellwigaff17162016-07-12 18:20:17 +0900159 nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_NOLEGACY);
160 if (nvec < 0)
161 goto out_err;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100162
Christoph Hellwigaff17162016-07-12 18:20:17 +09001634.3 Legacy APIs
Alexander Gordeev7918b2d2014-02-13 10:47:51 -0700164
Christoph Hellwigaff17162016-07-12 18:20:17 +0900165The following old APIs to enable and disable MSI or MSI-X interrupts should
166not be used in new code:
Alexander Gordeev3ce4e862014-02-13 10:48:02 -0700167
Christoph Hellwigaff17162016-07-12 18:20:17 +0900168 pci_enable_msi() /* deprecated */
169 pci_enable_msi_range() /* deprecated */
170 pci_enable_msi_exact() /* deprecated */
171 pci_disable_msi() /* deprecated */
172 pci_enable_msix_range() /* deprecated */
173 pci_enable_msix_exact() /* deprecated */
174 pci_disable_msix() /* deprecated */
Alexander Gordeev3ce4e862014-02-13 10:48:02 -0700175
Christoph Hellwigaff17162016-07-12 18:20:17 +0900176Additionally there are APIs to provide the number of supported MSI or MSI-X
177vectors: pci_msi_vec_count() and pci_msix_vec_count(). In general these
178should be avoided in favor of letting pci_alloc_irq_vectors() cap the
179number of vectors. If you have a legitimate special use case for the count
180of vectors we might have to revisit that decision and add a
181pci_nr_irq_vectors() helper that handles MSI and MSI-X transparently.
Alexander Gordeev3ce4e862014-02-13 10:48:02 -0700182
Christoph Hellwigaff17162016-07-12 18:20:17 +09001834.4 Considerations when using MSIs
Alexander Gordeev3ce4e862014-02-13 10:48:02 -0700184
Christoph Hellwigaff17162016-07-12 18:20:17 +09001854.4.1 Spinlocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400187Most device drivers have a per-device spinlock which is taken in the
188interrupt handler. With pin-based interrupts or a single MSI, it is not
189necessary to disable interrupts (Linux guarantees the same interrupt will
190not be re-entered). If a device uses multiple interrupts, the driver
191must disable interrupts while the lock is held. If the device sends
192a different interrupt, the driver will deadlock trying to recursively
Valentin Rothberg2f9d7382015-02-27 12:55:16 +0100193acquire the spinlock. Such deadlocks can be avoided by using
194spin_lock_irqsave() or spin_lock_irq() which disable local interrupts
195and acquire the lock (see Documentation/DocBook/kernel-locking).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Christoph Hellwigaff17162016-07-12 18:20:17 +09001974.5 How to tell whether MSI/MSI-X is enabled on a device
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800198
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400199Using 'lspci -v' (as root) may show some devices with "MSI", "Message
200Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
Michael Witten4979de62011-07-14 19:52:56 +0000201has an 'Enable' flag which is followed with either "+" (enabled)
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400202or "-" (disabled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002055. MSI quirks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400207Several PCI chipsets or devices are known not to support MSIs.
208The PCI stack provides three ways to disable MSIs:
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800209
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002101. globally
2112. on all devices behind a specific bridge
2123. on a single device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002145.1. Disabling MSIs globally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400216Some host chipsets simply don't support MSIs properly. If we're
217lucky, the manufacturer knows this and has indicated it in the ACPI
Michael Witten4979de62011-07-14 19:52:56 +0000218FADT table. In this case, Linux automatically disables MSIs.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400219Some boards don't include this information in the table and so we have
220to detect them ourselves. The complete list of these is found near the
221quirk_disable_all_msi() function in drivers/pci/quirks.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400223If you have a board which has problems with MSIs, you can pass pci=nomsi
224on the kernel command line to disable MSIs on all devices. It would be
225in your best interests to report the problem to linux-pci@vger.kernel.org
226including a full 'lspci -v' so we can add the quirks to the kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002285.2. Disabling MSIs below a bridge
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400230Some PCI bridges are not able to route MSIs between busses properly.
231In this case, MSIs must be disabled on all devices behind the bridge.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200232
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400233Some bridges allow you to enable MSIs by changing some bits in their
234PCI configuration space (especially the Hypertransport chipsets such
235as the nVidia nForce and Serverworks HT2000). As with host chipsets,
236Linux mostly knows about them and automatically enables MSIs if it can.
Michael Wittene6b85a12011-07-15 03:25:44 +0000237If you have a bridge unknown to Linux, you can enable
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400238MSIs in configuration space using whatever method you know works, then
239enable MSIs on that bridge by doing:
Brice Goglin0cc2b372006-10-05 10:24:42 +0200240
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400241 echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
Brice Goglin0cc2b372006-10-05 10:24:42 +0200242
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400243where $bridge is the PCI address of the bridge you've enabled (eg
2440000:00:0e.0).
Brice Goglin0cc2b372006-10-05 10:24:42 +0200245
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400246To disable MSIs, echo 0 instead of 1. Changing this value should be
Michael Witten1b8386f2011-07-15 03:26:37 +0000247done with caution as it could break interrupt handling for all devices
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400248below this bridge.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200249
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400250Again, please notify linux-pci@vger.kernel.org of any bridges that need
251special handling.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200252
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002535.3. Disabling MSIs on a single device
Brice Goglin0cc2b372006-10-05 10:24:42 +0200254
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400255Some devices are known to have faulty MSI implementations. Usually this
Michael Wittenc2b65e12011-07-15 03:27:22 +0000256is handled in the individual device driver, but occasionally it's necessary
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400257to handle this with a quirk. Some drivers have an option to disable use
258of MSI. While this is a convenient workaround for the driver author,
Jeremiah Mahler305af082014-05-22 00:04:26 -0700259it is not good practice, and should not be emulated.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200260
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002615.4. Finding why MSIs are disabled on a device
Brice Goglin0cc2b372006-10-05 10:24:42 +0200262
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400263From the above three sections, you can see that there are many reasons
264why MSIs may not be enabled for a given device. Your first step should
265be to examine your dmesg carefully to determine whether MSIs are enabled
266for your machine. You should also check your .config to be sure you
267have enabled CONFIG_PCI_MSI.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200268
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400269Then, 'lspci -t' gives the list of bridges above a device. Reading
Michael Witten798c7942011-07-15 03:29:04 +0000270/sys/bus/pci/devices/*/msi_bus will tell you whether MSIs are enabled (1)
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400271or disabled (0). If 0 is found in any of the msi_bus files belonging
272to bridges between the PCI root and the device, MSIs are disabled.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200273
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400274It is also worth checking the device driver to see whether it supports MSIs.
Alexander Gordeev302a2522013-12-30 08:28:16 +0100275For example, it may contain calls to pci_enable_msi_range() or
276pci_enable_msix_range().