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