blob: 1c02431f1d1a10ab4409906a3fae7cc1e0cd7687 [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
48arrives in memory before the value can be returned from the register.
49Using 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
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040081Most of the hard work is done for the driver in the PCI layer. It simply
82has to request that the PCI layer set up the MSI capability for this
83device.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400854.2.1 pci_enable_msi
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87int pci_enable_msi(struct pci_dev *dev)
88
Matthew Wilcoxc41ade22009-03-17 08:54:05 -040089A successful call will allocate ONE interrupt to the device, regardless
90of how many MSIs the device supports. The device will be switched from
91pin-based interrupt mode to MSI mode. The dev->irq number is changed
92to a new number which represents the message signaled interrupt.
93This function should be called before the driver calls request_irq()
94since enabling MSIs disables the pin-based IRQ and the driver will not
95receive interrupts on the old interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400974.2.2 pci_disable_msi
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99void pci_disable_msi(struct pci_dev *dev)
100
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400101This function should be used to undo the effect of pci_enable_msi().
102Calling it restores dev->irq to the pin-based interrupt number and frees
103the previously allocated message signaled interrupt(s). The interrupt
104may subsequently be assigned to another device, so drivers should not
105cache the value of dev->irq.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400107A device driver must always call free_irq() on the interrupt(s)
108for which it has called request_irq() before calling this function.
109Failure to do so will result in a BUG_ON(), the device will be left with
110MSI enabled and will leak its vector.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001124.3 Using MSI-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400114The MSI-X capability is much more flexible than the MSI capability.
115It supports up to 2048 interrupts, each of which can be controlled
116independently. To support this flexibility, drivers must use an array of
117`struct msix_entry':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119struct msix_entry {
120 u16 vector; /* kernel uses to write alloc vector */
121 u16 entry; /* driver uses to specify entry */
122};
123
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400124This allows for the device to use these interrupts in a sparse fashion;
125for example it could use interrupts 3 and 1027 and allocate only a
126two-element array. The driver is expected to fill in the 'entry' value
127in each element of the array to indicate which entries it wants the kernel
128to assign interrupts for. It is invalid to fill in two entries with the
129same number.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001314.3.1 pci_enable_msix
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400133int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
134
135Calling this function asks the PCI subsystem to allocate 'nvec' MSIs.
136The 'entries' argument is a pointer to an array of msix_entry structs
137which should be at least 'nvec' entries in size. On success, the
138function will return 0 and the device will have been switched into
139MSI-X interrupt mode. The 'vector' elements in each entry will have
140been filled in with the interrupt number. The driver should then call
141request_irq() for each 'vector' that it decides to use.
142
143If this function returns a negative number, it indicates an error and
144the driver should not attempt to allocate any more MSI-X interrupts for
145this device. If it returns a positive number, it indicates the maximum
146number of interrupt vectors that could have been allocated.
147
148This function, in contrast with pci_enable_msi(), does not adjust
149dev->irq. The device will not generate interrupts for this interrupt
150number once MSI-X is enabled. The device driver is responsible for
151keeping track of the interrupts assigned to the MSI-X vectors so it can
152free them again later.
153
154Device drivers should normally call this function once per device
155during the initialization phase.
156
1574.3.2 pci_disable_msix
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159void pci_disable_msix(struct pci_dev *dev)
160
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400161This API should be used to undo the effect of pci_enable_msix(). It frees
162the previously allocated message signaled interrupts. The interrupts may
163subsequently be assigned to another device, so drivers should not cache
164the value of the 'vector' elements over a call to pci_disable_msix().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400166A device driver must always call free_irq() on the interrupt(s)
167for which it has called request_irq() before calling this function.
168Failure to do so will result in a BUG_ON(), the device will be left with
169MSI enabled and will leak its vector.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001714.3.3 The MSI-X Table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400173The MSI-X capability specifies a BAR and offset within that BAR for the
174MSI-X Table. This address is mapped by the PCI subsystem, and should not
175be accessed directly by the device driver. If the driver wishes to
176mask or unmask an interrupt, it should call disable_irq() / enable_irq().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001784.4 Handling devices implementing both MSI and MSI-X capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400180If a device implements both MSI and MSI-X capabilities, it can
181run in either MSI mode or MSI-X mode but not both simultaneously.
182This is a requirement of the PCI spec, and it is enforced by the
183PCI layer. Calling pci_enable_msi() when MSI-X is already enabled or
184pci_enable_msix() when MSI is already enabled will result in an error.
185If a device driver wishes to switch between MSI and MSI-X at runtime,
186it must first quiesce the device, then switch it back to pin-interrupt
187mode, before calling pci_enable_msi() or pci_enable_msix() and resuming
188operation. This is not expected to be a common operation but may be
189useful for debugging or testing during development.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001914.5 Considerations when using MSIs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001934.5.1 Choosing between MSI-X and MSI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400195If your device supports both MSI-X and MSI capabilities, you should use
196the MSI-X facilities in preference to the MSI facilities. As mentioned
197above, MSI-X supports any number of interrupts between 1 and 2048.
198In constrast, MSI is restricted to a maximum of 32 interrupts (and
199must be a power of two). In addition, the MSI interrupt vectors must
200be allocated consecutively, so the system may not be able to allocate
201as many vectors for MSI as it could for MSI-X. On some platforms, MSI
202interrupts must all be targetted at the same set of CPUs whereas MSI-X
203interrupts can all be targetted at different CPUs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002054.5.2 Spinlocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400207Most device drivers have a per-device spinlock which is taken in the
208interrupt handler. With pin-based interrupts or a single MSI, it is not
209necessary to disable interrupts (Linux guarantees the same interrupt will
210not be re-entered). If a device uses multiple interrupts, the driver
211must disable interrupts while the lock is held. If the device sends
212a different interrupt, the driver will deadlock trying to recursively
213acquire the spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400215There are two solutions. The first is to take the lock with
216spin_lock_irqsave() or spin_lock_irq() (see
217Documentation/DocBook/kernel-locking). The second is to specify
218IRQF_DISABLED to request_irq() so that the kernel runs the entire
219interrupt routine with interrupts disabled.
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800220
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400221If your MSI interrupt routine does not hold the lock for the whole time
222it is running, the first solution may be best. The second solution is
223normally preferred as it avoids making two transitions from interrupt
224disabled to enabled and back again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002264.6 How to tell whether MSI/MSI-X is enabled on a device
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800227
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400228Using 'lspci -v' (as root) may show some devices with "MSI", "Message
229Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
230has an 'Enable' flag which will be followed with either "+" (enabled)
231or "-" (disabled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002345. MSI quirks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400236Several PCI chipsets or devices are known not to support MSIs.
237The PCI stack provides three ways to disable MSIs:
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800238
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002391. globally
2402. on all devices behind a specific bridge
2413. on a single device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002435.1. Disabling MSIs globally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400245Some host chipsets simply don't support MSIs properly. If we're
246lucky, the manufacturer knows this and has indicated it in the ACPI
247FADT table. In this case, Linux will automatically disable MSIs.
248Some boards don't include this information in the table and so we have
249to detect them ourselves. The complete list of these is found near the
250quirk_disable_all_msi() function in drivers/pci/quirks.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400252If you have a board which has problems with MSIs, you can pass pci=nomsi
253on the kernel command line to disable MSIs on all devices. It would be
254in your best interests to report the problem to linux-pci@vger.kernel.org
255including a full 'lspci -v' so we can add the quirks to the kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002575.2. Disabling MSIs below a bridge
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400259Some PCI bridges are not able to route MSIs between busses properly.
260In this case, MSIs must be disabled on all devices behind the bridge.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200261
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400262Some bridges allow you to enable MSIs by changing some bits in their
263PCI configuration space (especially the Hypertransport chipsets such
264as the nVidia nForce and Serverworks HT2000). As with host chipsets,
265Linux mostly knows about them and automatically enables MSIs if it can.
266If you have a bridge which Linux doesn't yet know about, you can enable
267MSIs in configuration space using whatever method you know works, then
268enable MSIs on that bridge by doing:
Brice Goglin0cc2b372006-10-05 10:24:42 +0200269
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400270 echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
Brice Goglin0cc2b372006-10-05 10:24:42 +0200271
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400272where $bridge is the PCI address of the bridge you've enabled (eg
2730000:00:0e.0).
Brice Goglin0cc2b372006-10-05 10:24:42 +0200274
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400275To disable MSIs, echo 0 instead of 1. Changing this value should be
276done with caution as it can break interrupt handling for all devices
277below this bridge.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200278
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400279Again, please notify linux-pci@vger.kernel.org of any bridges that need
280special handling.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200281
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002825.3. Disabling MSIs on a single device
Brice Goglin0cc2b372006-10-05 10:24:42 +0200283
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400284Some devices are known to have faulty MSI implementations. Usually this
285is handled in the individual device driver but occasionally it's necessary
286to handle this with a quirk. Some drivers have an option to disable use
287of MSI. While this is a convenient workaround for the driver author,
288it is not good practise, and should not be emulated.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200289
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04002905.4. Finding why MSIs are disabled on a device
Brice Goglin0cc2b372006-10-05 10:24:42 +0200291
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400292From the above three sections, you can see that there are many reasons
293why MSIs may not be enabled for a given device. Your first step should
294be to examine your dmesg carefully to determine whether MSIs are enabled
295for your machine. You should also check your .config to be sure you
296have enabled CONFIG_PCI_MSI.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200297
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400298Then, 'lspci -t' gives the list of bridges above a device. Reading
299/sys/bus/pci/devices/*/msi_bus will tell you whether MSI are enabled (1)
300or disabled (0). If 0 is found in any of the msi_bus files belonging
301to bridges between the PCI root and the device, MSIs are disabled.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200302
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400303It is also worth checking the device driver to see whether it supports MSIs.
304For example, it may contain calls to pci_enable_msi(), pci_enable_msix() or
305pci_enable_msi_block().