spi: efm32: use $vendor,$device scheme for compatible string
[linux.git] / 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 arrive in memory before the value may 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_range
86
87 int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
88
89 This function allows a device driver to request any number of MSI
90 interrupts within specified range from 'minvec' to 'maxvec'.
91
92 If this function returns a positive number it indicates the number of
93 MSI interrupts that have been successfully allocated.  In this case
94 the device is switched from pin-based interrupt mode to MSI mode and
95 updates dev->irq to be the lowest of the new interrupts assigned to it.
96 The other interrupts assigned to the device are in the range dev->irq
97 to dev->irq + returned value - 1.  Device driver can use the returned
98 number of successfully allocated MSI interrupts to further allocate
99 and initialize device resources.
100
101 If this function returns a negative number, it indicates an error and
102 the driver should not attempt to request any more MSI interrupts for
103 this device.
104
105 This function should be called before the driver calls request_irq(),
106 because MSI interrupts are delivered via vectors that are different
107 from the vector of a pin-based interrupt.
108
109 It is ideal if drivers can cope with a variable number of MSI interrupts;
110 there are many reasons why the platform may not be able to provide the
111 exact number that a driver asks for.
112
113 There could be devices that can not operate with just any number of MSI
114 interrupts within a range.  See chapter 4.3.1.3 to get the idea how to
115 handle such devices for MSI-X - the same logic applies to MSI.
116
117 4.2.1.1 Maximum possible number of MSI interrupts
118
119 The typical usage of MSI interrupts is to allocate as many vectors as
120 possible, likely up to the limit returned by pci_msi_vec_count() function:
121
122 static int foo_driver_enable_msi(struct pci_dev *pdev, int nvec)
123 {
124         return pci_enable_msi_range(pdev, 1, nvec);
125 }
126
127 Note the value of 'minvec' parameter is 1.  As 'minvec' is inclusive,
128 the value of 0 would be meaningless and could result in error.
129
130 Some devices have a minimal limit on number of MSI interrupts.
131 In this case the function could look like this:
132
133 static int foo_driver_enable_msi(struct pci_dev *pdev, int nvec)
134 {
135         return pci_enable_msi_range(pdev, FOO_DRIVER_MINIMUM_NVEC, nvec);
136 }
137
138 4.2.1.2 Exact number of MSI interrupts
139
140 If a driver is unable or unwilling to deal with a variable number of MSI
141 interrupts it could request a particular number of interrupts by passing
142 that number to pci_enable_msi_range() function as both 'minvec' and 'maxvec'
143 parameters:
144
145 static int foo_driver_enable_msi(struct pci_dev *pdev, int nvec)
146 {
147         return pci_enable_msi_range(pdev, nvec, nvec);
148 }
149
150 4.2.1.3 Single MSI mode
151
152 The most notorious example of the request type described above is
153 enabling the single MSI mode for a device.  It could be done by passing
154 two 1s as 'minvec' and 'maxvec':
155
156 static int foo_driver_enable_single_msi(struct pci_dev *pdev)
157 {
158         return pci_enable_msi_range(pdev, 1, 1);
159 }
160
161 4.2.2 pci_disable_msi
162
163 void pci_disable_msi(struct pci_dev *dev)
164
165 This function should be used to undo the effect of pci_enable_msi_range().
166 Calling it restores dev->irq to the pin-based interrupt number and frees
167 the previously allocated MSIs.  The interrupts may subsequently be assigned
168 to another device, so drivers should not cache the value of dev->irq.
169
170 Before calling this function, a device driver must always call free_irq()
171 on any interrupt for which it previously called request_irq().
172 Failure to do so results in a BUG_ON(), leaving the device with
173 MSI enabled and thus leaking its vector.
174
175 4.2.3 pci_msi_vec_count
176
177 int pci_msi_vec_count(struct pci_dev *dev)
178
179 This function could be used to retrieve the number of MSI vectors the
180 device requested (via the Multiple Message Capable register). The MSI
181 specification only allows the returned value to be a power of two,
182 up to a maximum of 2^5 (32).
183
184 If this function returns a negative number, it indicates the device is
185 not capable of sending MSIs.
186
187 If this function returns a positive number, it indicates the maximum
188 number of MSI interrupt vectors that could be allocated.
189
190 4.3 Using MSI-X
191
192 The MSI-X capability is much more flexible than the MSI capability.
193 It supports up to 2048 interrupts, each of which can be controlled
194 independently.  To support this flexibility, drivers must use an array of
195 `struct msix_entry':
196
197 struct msix_entry {
198         u16     vector; /* kernel uses to write alloc vector */
199         u16     entry; /* driver uses to specify entry */
200 };
201
202 This allows for the device to use these interrupts in a sparse fashion;
203 for example, it could use interrupts 3 and 1027 and yet allocate only a
204 two-element array.  The driver is expected to fill in the 'entry' value
205 in each element of the array to indicate for which entries the kernel
206 should assign interrupts; it is invalid to fill in two entries with the
207 same number.
208
209 4.3.1 pci_enable_msix_range
210
211 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
212                           int minvec, int maxvec)
213
214 Calling this function asks the PCI subsystem to allocate any number of
215 MSI-X interrupts within specified range from 'minvec' to 'maxvec'.
216 The 'entries' argument is a pointer to an array of msix_entry structs
217 which should be at least 'maxvec' entries in size.
218
219 On success, the device is switched into MSI-X mode and the function
220 returns the number of MSI-X interrupts that have been successfully
221 allocated.  In this case the 'vector' member in entries numbered from
222 0 to the returned value - 1 is populated with the interrupt number;
223 the driver should then call request_irq() for each 'vector' that it
224 decides to use.  The device driver is responsible for keeping track of the
225 interrupts assigned to the MSI-X vectors so it can free them again later.
226 Device driver can use the returned number of successfully allocated MSI-X
227 interrupts to further allocate and initialize device resources.
228
229 If this function returns a negative number, it indicates an error and
230 the driver should not attempt to allocate any more MSI-X interrupts for
231 this device.
232
233 This function, in contrast with pci_enable_msi_range(), does not adjust
234 dev->irq.  The device will not generate interrupts for this interrupt
235 number once MSI-X is enabled.
236
237 Device drivers should normally call this function once per device
238 during the initialization phase.
239
240 It is ideal if drivers can cope with a variable number of MSI-X interrupts;
241 there are many reasons why the platform may not be able to provide the
242 exact number that a driver asks for.
243
244 There could be devices that can not operate with just any number of MSI-X
245 interrupts within a range.  E.g., an network adapter might need let's say
246 four vectors per each queue it provides.  Therefore, a number of MSI-X
247 interrupts allocated should be a multiple of four.  In this case interface
248 pci_enable_msix_range() can not be used alone to request MSI-X interrupts
249 (since it can allocate any number within the range, without any notion of
250 the multiple of four) and the device driver should master a custom logic
251 to request the required number of MSI-X interrupts.
252
253 4.3.1.1 Maximum possible number of MSI-X interrupts
254
255 The typical usage of MSI-X interrupts is to allocate as many vectors as
256 possible, likely up to the limit returned by pci_msix_vec_count() function:
257
258 static int foo_driver_enable_msix(struct foo_adapter *adapter, int nvec)
259 {
260         return pci_enable_msi_range(adapter->pdev, adapter->msix_entries,
261                                     1, nvec);
262 }
263
264 Note the value of 'minvec' parameter is 1.  As 'minvec' is inclusive,
265 the value of 0 would be meaningless and could result in error.
266
267 Some devices have a minimal limit on number of MSI-X interrupts.
268 In this case the function could look like this:
269
270 static int foo_driver_enable_msix(struct foo_adapter *adapter, int nvec)
271 {
272         return pci_enable_msi_range(adapter->pdev, adapter->msix_entries,
273                                     FOO_DRIVER_MINIMUM_NVEC, nvec);
274 }
275
276 4.3.1.2 Exact number of MSI-X interrupts
277
278 If a driver is unable or unwilling to deal with a variable number of MSI-X
279 interrupts it could request a particular number of interrupts by passing
280 that number to pci_enable_msix_range() function as both 'minvec' and 'maxvec'
281 parameters:
282
283 static int foo_driver_enable_msix(struct foo_adapter *adapter, int nvec)
284 {
285         return pci_enable_msi_range(adapter->pdev, adapter->msix_entries,
286                                     nvec, nvec);
287 }
288
289 4.3.1.3 Specific requirements to the number of MSI-X interrupts
290
291 As noted above, there could be devices that can not operate with just any
292 number of MSI-X interrupts within a range.  E.g., let's assume a device that
293 is only capable sending the number of MSI-X interrupts which is a power of
294 two.  A routine that enables MSI-X mode for such device might look like this:
295
296 /*
297  * Assume 'minvec' and 'maxvec' are non-zero
298  */
299 static int foo_driver_enable_msix(struct foo_adapter *adapter,
300                                   int minvec, int maxvec)
301 {
302         int rc;
303
304         minvec = roundup_pow_of_two(minvec);
305         maxvec = rounddown_pow_of_two(maxvec);
306
307         if (minvec > maxvec)
308                 return -ERANGE;
309
310 retry:
311         rc = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
312                                    maxvec, maxvec);
313         /*
314          * -ENOSPC is the only error code allowed to be analized
315          */
316         if (rc == -ENOSPC) {
317                 if (maxvec == 1)
318                         return -ENOSPC;
319
320                 maxvec /= 2;
321
322                 if (minvec > maxvec)
323                         return -ENOSPC;
324
325                 goto retry;
326         }
327
328         return rc;
329 }
330
331 Note how pci_enable_msix_range() return value is analized for a fallback -
332 any error code other than -ENOSPC indicates a fatal error and should not
333 be retried.
334
335 4.3.2 pci_disable_msix
336
337 void pci_disable_msix(struct pci_dev *dev)
338
339 This function should be used to undo the effect of pci_enable_msix_range().
340 It frees the previously allocated MSI-X interrupts. The interrupts may
341 subsequently be assigned to another device, so drivers should not cache
342 the value of the 'vector' elements over a call to pci_disable_msix().
343
344 Before calling this function, a device driver must always call free_irq()
345 on any interrupt for which it previously called request_irq().
346 Failure to do so results in a BUG_ON(), leaving the device with
347 MSI-X enabled and thus leaking its vector.
348
349 4.3.3 The MSI-X Table
350
351 The MSI-X capability specifies a BAR and offset within that BAR for the
352 MSI-X Table.  This address is mapped by the PCI subsystem, and should not
353 be accessed directly by the device driver.  If the driver wishes to
354 mask or unmask an interrupt, it should call disable_irq() / enable_irq().
355
356 4.3.4 pci_msix_vec_count
357
358 int pci_msix_vec_count(struct pci_dev *dev)
359
360 This function could be used to retrieve number of entries in the device
361 MSI-X table.
362
363 If this function returns a negative number, it indicates the device is
364 not capable of sending MSI-Xs.
365
366 If this function returns a positive number, it indicates the maximum
367 number of MSI-X interrupt vectors that could be allocated.
368
369 4.4 Handling devices implementing both MSI and MSI-X capabilities
370
371 If a device implements both MSI and MSI-X capabilities, it can
372 run in either MSI mode or MSI-X mode, but not both simultaneously.
373 This is a requirement of the PCI spec, and it is enforced by the
374 PCI layer.  Calling pci_enable_msi_range() when MSI-X is already
375 enabled or pci_enable_msix_range() when MSI is already enabled
376 results in an error.  If a device driver wishes to switch between MSI
377 and MSI-X at runtime, it must first quiesce the device, then switch
378 it back to pin-interrupt mode, before calling pci_enable_msi_range()
379 or pci_enable_msix_range() and resuming operation.  This is not expected
380 to be a common operation but may be useful for debugging or testing
381 during development.
382
383 4.5 Considerations when using MSIs
384
385 4.5.1 Choosing between MSI-X and MSI
386
387 If your device supports both MSI-X and MSI capabilities, you should use
388 the MSI-X facilities in preference to the MSI facilities.  As mentioned
389 above, MSI-X supports any number of interrupts between 1 and 2048.
390 In constrast, MSI is restricted to a maximum of 32 interrupts (and
391 must be a power of two).  In addition, the MSI interrupt vectors must
392 be allocated consecutively, so the system might not be able to allocate
393 as many vectors for MSI as it could for MSI-X.  On some platforms, MSI
394 interrupts must all be targeted at the same set of CPUs whereas MSI-X
395 interrupts can all be targeted at different CPUs.
396
397 4.5.2 Spinlocks
398
399 Most device drivers have a per-device spinlock which is taken in the
400 interrupt handler.  With pin-based interrupts or a single MSI, it is not
401 necessary to disable interrupts (Linux guarantees the same interrupt will
402 not be re-entered).  If a device uses multiple interrupts, the driver
403 must disable interrupts while the lock is held.  If the device sends
404 a different interrupt, the driver will deadlock trying to recursively
405 acquire the spinlock.
406
407 There are two solutions.  The first is to take the lock with
408 spin_lock_irqsave() or spin_lock_irq() (see
409 Documentation/DocBook/kernel-locking).  The second is to specify
410 IRQF_DISABLED to request_irq() so that the kernel runs the entire
411 interrupt routine with interrupts disabled.
412
413 If your MSI interrupt routine does not hold the lock for the whole time
414 it is running, the first solution may be best.  The second solution is
415 normally preferred as it avoids making two transitions from interrupt
416 disabled to enabled and back again.
417
418 4.6 How to tell whether MSI/MSI-X is enabled on a device
419
420 Using 'lspci -v' (as root) may show some devices with "MSI", "Message
421 Signalled Interrupts" or "MSI-X" capabilities.  Each of these capabilities
422 has an 'Enable' flag which is followed with either "+" (enabled)
423 or "-" (disabled).
424
425
426 5. MSI quirks
427
428 Several PCI chipsets or devices are known not to support MSIs.
429 The PCI stack provides three ways to disable MSIs:
430
431 1. globally
432 2. on all devices behind a specific bridge
433 3. on a single device
434
435 5.1. Disabling MSIs globally
436
437 Some host chipsets simply don't support MSIs properly.  If we're
438 lucky, the manufacturer knows this and has indicated it in the ACPI
439 FADT table.  In this case, Linux automatically disables MSIs.
440 Some boards don't include this information in the table and so we have
441 to detect them ourselves.  The complete list of these is found near the
442 quirk_disable_all_msi() function in drivers/pci/quirks.c.
443
444 If you have a board which has problems with MSIs, you can pass pci=nomsi
445 on the kernel command line to disable MSIs on all devices.  It would be
446 in your best interests to report the problem to linux-pci@vger.kernel.org
447 including a full 'lspci -v' so we can add the quirks to the kernel.
448
449 5.2. Disabling MSIs below a bridge
450
451 Some PCI bridges are not able to route MSIs between busses properly.
452 In this case, MSIs must be disabled on all devices behind the bridge.
453
454 Some bridges allow you to enable MSIs by changing some bits in their
455 PCI configuration space (especially the Hypertransport chipsets such
456 as the nVidia nForce and Serverworks HT2000).  As with host chipsets,
457 Linux mostly knows about them and automatically enables MSIs if it can.
458 If you have a bridge unknown to Linux, you can enable
459 MSIs in configuration space using whatever method you know works, then
460 enable MSIs on that bridge by doing:
461
462        echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
463
464 where $bridge is the PCI address of the bridge you've enabled (eg
465 0000:00:0e.0).
466
467 To disable MSIs, echo 0 instead of 1.  Changing this value should be
468 done with caution as it could break interrupt handling for all devices
469 below this bridge.
470
471 Again, please notify linux-pci@vger.kernel.org of any bridges that need
472 special handling.
473
474 5.3. Disabling MSIs on a single device
475
476 Some devices are known to have faulty MSI implementations.  Usually this
477 is handled in the individual device driver, but occasionally it's necessary
478 to handle this with a quirk.  Some drivers have an option to disable use
479 of MSI.  While this is a convenient workaround for the driver author,
480 it is not good practise, and should not be emulated.
481
482 5.4. Finding why MSIs are disabled on a device
483
484 From the above three sections, you can see that there are many reasons
485 why MSIs may not be enabled for a given device.  Your first step should
486 be to examine your dmesg carefully to determine whether MSIs are enabled
487 for your machine.  You should also check your .config to be sure you
488 have enabled CONFIG_PCI_MSI.
489
490 Then, 'lspci -t' gives the list of bridges above a device.  Reading
491 /sys/bus/pci/devices/*/msi_bus will tell you whether MSIs are enabled (1)
492 or disabled (0).  If 0 is found in any of the msi_bus files belonging
493 to bridges between the PCI root and the device, MSIs are disabled.
494
495 It is also worth checking the device driver to see whether it supports MSIs.
496 For example, it may contain calls to pci_enable_msi_range() or
497 pci_enable_msix_range().