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