blob: a8d01005f480ad6be4c86f187665e9ef3d75d08e [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
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
Alexander Gordeev302a2522013-12-30 08:28:16 +0100854.2.1 pci_enable_msi_range
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Alexander Gordeev302a2522013-12-30 08:28:16 +010087int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Alexander Gordeev302a2522013-12-30 08:28:16 +010089This function allows a device driver to request any number of MSI
90interrupts within specified range from 'minvec' to 'maxvec'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Alexander Gordeev302a2522013-12-30 08:28:16 +010092If this function returns a positive number it indicates the number of
93MSI interrupts that have been successfully allocated. In this case
94the device is switched from pin-based interrupt mode to MSI mode and
95updates dev->irq to be the lowest of the new interrupts assigned to it.
96The other interrupts assigned to the device are in the range dev->irq
97to dev->irq + returned value - 1. Device driver can use the returned
98number of successfully allocated MSI interrupts to further allocate
99and initialize device resources.
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400100
101If this function returns a negative number, it indicates an error and
102the driver should not attempt to request any more MSI interrupts for
Alexander Gordeev302a2522013-12-30 08:28:16 +0100103this device.
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400104
Alexander Gordeev302a2522013-12-30 08:28:16 +0100105This function should be called before the driver calls request_irq(),
106because MSI interrupts are delivered via vectors that are different
107from the vector of a pin-based interrupt.
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400108
Alexander Gordeev302a2522013-12-30 08:28:16 +0100109It is ideal if drivers can cope with a variable number of MSI interrupts;
110there are many reasons why the platform may not be able to provide the
111exact number that a driver asks for.
112
113There could be devices that can not operate with just any number of MSI
114interrupts within a range. See chapter 4.3.1.3 to get the idea how to
115handle such devices for MSI-X - the same logic applies to MSI.
116
1174.2.1.1 Maximum possible number of MSI interrupts
118
119The typical usage of MSI interrupts is to allocate as many vectors as
120possible, likely up to the limit returned by pci_msi_vec_count() function:
121
122static int foo_driver_enable_msi(struct pci_dev *pdev, int nvec)
123{
124 return pci_enable_msi_range(pdev, 1, nvec);
125}
126
127Note the value of 'minvec' parameter is 1. As 'minvec' is inclusive,
128the value of 0 would be meaningless and could result in error.
129
130Some devices have a minimal limit on number of MSI interrupts.
131In this case the function could look like this:
132
133static 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
1384.2.1.2 Exact number of MSI interrupts
139
140If a driver is unable or unwilling to deal with a variable number of MSI
141interrupts it could request a particular number of interrupts by passing
142that number to pci_enable_msi_range() function as both 'minvec' and 'maxvec'
143parameters:
144
145static int foo_driver_enable_msi(struct pci_dev *pdev, int nvec)
146{
147 return pci_enable_msi_range(pdev, nvec, nvec);
148}
149
1504.2.1.3 Single MSI mode
151
152The most notorious example of the request type described above is
153enabling the single MSI mode for a device. It could be done by passing
154two 1s as 'minvec' and 'maxvec':
155
156static int foo_driver_enable_single_msi(struct pci_dev *pdev)
157{
158 return pci_enable_msi_range(pdev, 1, 1);
159}
160
1614.2.2 pci_disable_msi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163void pci_disable_msi(struct pci_dev *dev)
164
Alexander Gordeev302a2522013-12-30 08:28:16 +0100165This function should be used to undo the effect of pci_enable_msi_range().
166Calling it restores dev->irq to the pin-based interrupt number and frees
167the previously allocated MSIs. The interrupts may subsequently be assigned
168to another device, so drivers should not cache the value of dev->irq.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Michael Witten263d8d52011-07-14 21:28:00 +0000170Before calling this function, a device driver must always call free_irq()
171on any interrupt for which it previously called request_irq().
Michael Witten4979de62011-07-14 19:52:56 +0000172Failure to do so results in a BUG_ON(), leaving the device with
173MSI enabled and thus leaking its vector.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Alexander Gordeev302a2522013-12-30 08:28:16 +01001754.2.3 pci_msi_vec_count
Alexander Gordeevd1ac1d22013-12-30 08:28:13 +0100176
177int pci_msi_vec_count(struct pci_dev *dev)
178
179This function could be used to retrieve the number of MSI vectors the
180device requested (via the Multiple Message Capable register). The MSI
181specification only allows the returned value to be a power of two,
182up to a maximum of 2^5 (32).
183
184If this function returns a negative number, it indicates the device is
185not capable of sending MSIs.
186
187If this function returns a positive number, it indicates the maximum
188number of MSI interrupt vectors that could be allocated.
189
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04001904.3 Using MSI-X
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400192The MSI-X capability is much more flexible than the MSI capability.
193It supports up to 2048 interrupts, each of which can be controlled
194independently. To support this flexibility, drivers must use an array of
195`struct msix_entry':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197struct msix_entry {
198 u16 vector; /* kernel uses to write alloc vector */
199 u16 entry; /* driver uses to specify entry */
200};
201
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400202This allows for the device to use these interrupts in a sparse fashion;
Michael Wittene4439232011-07-14 21:30:18 +0000203for example, it could use interrupts 3 and 1027 and yet allocate only a
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400204two-element array. The driver is expected to fill in the 'entry' value
Michael Wittene4439232011-07-14 21:30:18 +0000205in each element of the array to indicate for which entries the kernel
206should assign interrupts; it is invalid to fill in two entries with the
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400207same number.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Alexander Gordeev302a2522013-12-30 08:28:16 +01002094.3.1 pci_enable_msix_range
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Alexander Gordeev302a2522013-12-30 08:28:16 +0100211int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
212 int minvec, int maxvec)
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400213
Alexander Gordeev302a2522013-12-30 08:28:16 +0100214Calling this function asks the PCI subsystem to allocate any number of
215MSI-X interrupts within specified range from 'minvec' to 'maxvec'.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400216The 'entries' argument is a pointer to an array of msix_entry structs
Alexander Gordeev302a2522013-12-30 08:28:16 +0100217which should be at least 'maxvec' entries in size.
218
219On success, the device is switched into MSI-X mode and the function
220returns the number of MSI-X interrupts that have been successfully
221allocated. In this case the 'vector' member in entries numbered from
2220 to the returned value - 1 is populated with the interrupt number;
Michael Witten4979de62011-07-14 19:52:56 +0000223the driver should then call request_irq() for each 'vector' that it
Michael Witten6457d9b2011-07-14 21:54:18 +0000224decides to use. The device driver is responsible for keeping track of the
225interrupts assigned to the MSI-X vectors so it can free them again later.
Alexander Gordeev302a2522013-12-30 08:28:16 +0100226Device driver can use the returned number of successfully allocated MSI-X
227interrupts to further allocate and initialize device resources.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400228
229If this function returns a negative number, it indicates an error and
230the driver should not attempt to allocate any more MSI-X interrupts for
Alexander Gordeev302a2522013-12-30 08:28:16 +0100231this device.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400232
Alexander Gordeev302a2522013-12-30 08:28:16 +0100233This function, in contrast with pci_enable_msi_range(), does not adjust
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400234dev->irq. The device will not generate interrupts for this interrupt
Michael Witten6457d9b2011-07-14 21:54:18 +0000235number once MSI-X is enabled.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400236
237Device drivers should normally call this function once per device
238during the initialization phase.
239
Michael Witten5a84fc32011-07-14 21:55:05 +0000240It is ideal if drivers can cope with a variable number of MSI-X interrupts;
Michael Ellermanfafad5b2009-03-20 15:22:12 +1100241there are many reasons why the platform may not be able to provide the
Michael Wittened737c12011-07-18 16:15:00 +0000242exact number that a driver asks for.
Michael Ellermanfafad5b2009-03-20 15:22:12 +1100243
Alexander Gordeev302a2522013-12-30 08:28:16 +0100244There could be devices that can not operate with just any number of MSI-X
245interrupts within a range. E.g., an network adapter might need let's say
246four vectors per each queue it provides. Therefore, a number of MSI-X
247interrupts allocated should be a multiple of four. In this case interface
248pci_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
250the multiple of four) and the device driver should master a custom logic
251to request the required number of MSI-X interrupts.
252
2534.3.1.1 Maximum possible number of MSI-X interrupts
254
255The typical usage of MSI-X interrupts is to allocate as many vectors as
256possible, likely up to the limit returned by pci_msix_vec_count() function:
Michael Ellermanfafad5b2009-03-20 15:22:12 +1100257
258static int foo_driver_enable_msix(struct foo_adapter *adapter, int nvec)
259{
Alexander Gordeev302a2522013-12-30 08:28:16 +0100260 return pci_enable_msi_range(adapter->pdev, adapter->msix_entries,
261 1, nvec);
262}
263
264Note the value of 'minvec' parameter is 1. As 'minvec' is inclusive,
265the value of 0 would be meaningless and could result in error.
266
267Some devices have a minimal limit on number of MSI-X interrupts.
268In this case the function could look like this:
269
270static 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
2764.3.1.2 Exact number of MSI-X interrupts
277
278If a driver is unable or unwilling to deal with a variable number of MSI-X
279interrupts it could request a particular number of interrupts by passing
280that number to pci_enable_msix_range() function as both 'minvec' and 'maxvec'
281parameters:
282
283static 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
2894.3.1.3 Specific requirements to the number of MSI-X interrupts
290
291As noted above, there could be devices that can not operate with just any
292number of MSI-X interrupts within a range. E.g., let's assume a device that
293is only capable sending the number of MSI-X interrupts which is a power of
294two. 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 */
299static 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
310retry:
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;
Michael Ellermanfafad5b2009-03-20 15:22:12 +1100326 }
327
Alexander Gordeev302a2522013-12-30 08:28:16 +0100328 return rc;
Michael Ellermanfafad5b2009-03-20 15:22:12 +1100329}
330
Alexander Gordeev302a2522013-12-30 08:28:16 +0100331Note how pci_enable_msix_range() return value is analized for a fallback -
332any error code other than -ENOSPC indicates a fatal error and should not
333be retried.
334
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04003354.3.2 pci_disable_msix
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337void pci_disable_msix(struct pci_dev *dev)
338
Alexander Gordeev302a2522013-12-30 08:28:16 +0100339This function should be used to undo the effect of pci_enable_msix_range().
340It frees the previously allocated MSI-X interrupts. The interrupts may
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400341subsequently be assigned to another device, so drivers should not cache
342the value of the 'vector' elements over a call to pci_disable_msix().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Michael Witten263d8d52011-07-14 21:28:00 +0000344Before calling this function, a device driver must always call free_irq()
345on any interrupt for which it previously called request_irq().
Michael Witten4979de62011-07-14 19:52:56 +0000346Failure to do so results in a BUG_ON(), leaving the device with
347MSI-X enabled and thus leaking its vector.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04003494.3.3 The MSI-X Table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400351The MSI-X capability specifies a BAR and offset within that BAR for the
352MSI-X Table. This address is mapped by the PCI subsystem, and should not
353be accessed directly by the device driver. If the driver wishes to
354mask or unmask an interrupt, it should call disable_irq() / enable_irq().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Alexander Gordeevff1aa432013-12-30 08:28:15 +01003564.3.4 pci_msix_vec_count
357
358int pci_msix_vec_count(struct pci_dev *dev)
359
360This function could be used to retrieve number of entries in the device
361MSI-X table.
362
363If this function returns a negative number, it indicates the device is
364not capable of sending MSI-Xs.
365
366If this function returns a positive number, it indicates the maximum
367number of MSI-X interrupt vectors that could be allocated.
368
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04003694.4 Handling devices implementing both MSI and MSI-X capabilities
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400371If a device implements both MSI and MSI-X capabilities, it can
Michael Wittene14bd7e2011-07-15 03:12:13 +0000372run in either MSI mode or MSI-X mode, but not both simultaneously.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400373This is a requirement of the PCI spec, and it is enforced by the
Alexander Gordeev302a2522013-12-30 08:28:16 +0100374PCI layer. Calling pci_enable_msi_range() when MSI-X is already
375enabled or pci_enable_msix_range() when MSI is already enabled
376results in an error. If a device driver wishes to switch between MSI
377and MSI-X at runtime, it must first quiesce the device, then switch
378it back to pin-interrupt mode, before calling pci_enable_msi_range()
379or pci_enable_msix_range() and resuming operation. This is not expected
380to be a common operation but may be useful for debugging or testing
381during development.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04003834.5 Considerations when using MSIs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04003854.5.1 Choosing between MSI-X and MSI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400387If your device supports both MSI-X and MSI capabilities, you should use
388the MSI-X facilities in preference to the MSI facilities. As mentioned
389above, MSI-X supports any number of interrupts between 1 and 2048.
390In constrast, MSI is restricted to a maximum of 32 interrupts (and
391must be a power of two). In addition, the MSI interrupt vectors must
Michael Witten952df552011-07-15 03:15:10 +0000392be allocated consecutively, so the system might not be able to allocate
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400393as many vectors for MSI as it could for MSI-X. On some platforms, MSI
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300394interrupts must all be targeted at the same set of CPUs whereas MSI-X
395interrupts can all be targeted at different CPUs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04003974.5.2 Spinlocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400399Most device drivers have a per-device spinlock which is taken in the
400interrupt handler. With pin-based interrupts or a single MSI, it is not
401necessary to disable interrupts (Linux guarantees the same interrupt will
402not be re-entered). If a device uses multiple interrupts, the driver
403must disable interrupts while the lock is held. If the device sends
404a different interrupt, the driver will deadlock trying to recursively
405acquire the spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400407There are two solutions. The first is to take the lock with
408spin_lock_irqsave() or spin_lock_irq() (see
409Documentation/DocBook/kernel-locking). The second is to specify
410IRQF_DISABLED to request_irq() so that the kernel runs the entire
411interrupt routine with interrupts disabled.
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800412
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400413If your MSI interrupt routine does not hold the lock for the whole time
414it is running, the first solution may be best. The second solution is
415normally preferred as it avoids making two transitions from interrupt
416disabled to enabled and back again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004184.6 How to tell whether MSI/MSI-X is enabled on a device
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800419
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400420Using 'lspci -v' (as root) may show some devices with "MSI", "Message
421Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
Michael Witten4979de62011-07-14 19:52:56 +0000422has an 'Enable' flag which is followed with either "+" (enabled)
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400423or "-" (disabled).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004265. MSI quirks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400428Several PCI chipsets or devices are known not to support MSIs.
429The PCI stack provides three ways to disable MSIs:
Randy Dunlap2500e7a2005-11-07 01:01:03 -0800430
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004311. globally
4322. on all devices behind a specific bridge
4333. on a single device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004355.1. Disabling MSIs globally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400437Some host chipsets simply don't support MSIs properly. If we're
438lucky, the manufacturer knows this and has indicated it in the ACPI
Michael Witten4979de62011-07-14 19:52:56 +0000439FADT table. In this case, Linux automatically disables MSIs.
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400440Some boards don't include this information in the table and so we have
441to detect them ourselves. The complete list of these is found near the
442quirk_disable_all_msi() function in drivers/pci/quirks.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400444If you have a board which has problems with MSIs, you can pass pci=nomsi
445on the kernel command line to disable MSIs on all devices. It would be
446in your best interests to report the problem to linux-pci@vger.kernel.org
447including a full 'lspci -v' so we can add the quirks to the kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004495.2. Disabling MSIs below a bridge
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400451Some PCI bridges are not able to route MSIs between busses properly.
452In this case, MSIs must be disabled on all devices behind the bridge.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200453
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400454Some bridges allow you to enable MSIs by changing some bits in their
455PCI configuration space (especially the Hypertransport chipsets such
456as the nVidia nForce and Serverworks HT2000). As with host chipsets,
457Linux mostly knows about them and automatically enables MSIs if it can.
Michael Wittene6b85a12011-07-15 03:25:44 +0000458If you have a bridge unknown to Linux, you can enable
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400459MSIs in configuration space using whatever method you know works, then
460enable MSIs on that bridge by doing:
Brice Goglin0cc2b372006-10-05 10:24:42 +0200461
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400462 echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
Brice Goglin0cc2b372006-10-05 10:24:42 +0200463
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400464where $bridge is the PCI address of the bridge you've enabled (eg
4650000:00:0e.0).
Brice Goglin0cc2b372006-10-05 10:24:42 +0200466
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400467To disable MSIs, echo 0 instead of 1. Changing this value should be
Michael Witten1b8386f2011-07-15 03:26:37 +0000468done with caution as it could break interrupt handling for all devices
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400469below this bridge.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200470
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400471Again, please notify linux-pci@vger.kernel.org of any bridges that need
472special handling.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200473
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004745.3. Disabling MSIs on a single device
Brice Goglin0cc2b372006-10-05 10:24:42 +0200475
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400476Some devices are known to have faulty MSI implementations. Usually this
Michael Wittenc2b65e12011-07-15 03:27:22 +0000477is handled in the individual device driver, but occasionally it's necessary
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400478to handle this with a quirk. Some drivers have an option to disable use
479of MSI. While this is a convenient workaround for the driver author,
480it is not good practise, and should not be emulated.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200481
Matthew Wilcoxc41ade22009-03-17 08:54:05 -04004825.4. Finding why MSIs are disabled on a device
Brice Goglin0cc2b372006-10-05 10:24:42 +0200483
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400484From the above three sections, you can see that there are many reasons
485why MSIs may not be enabled for a given device. Your first step should
486be to examine your dmesg carefully to determine whether MSIs are enabled
487for your machine. You should also check your .config to be sure you
488have enabled CONFIG_PCI_MSI.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200489
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400490Then, 'lspci -t' gives the list of bridges above a device. Reading
Michael Witten798c7942011-07-15 03:29:04 +0000491/sys/bus/pci/devices/*/msi_bus will tell you whether MSIs are enabled (1)
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400492or disabled (0). If 0 is found in any of the msi_bus files belonging
493to bridges between the PCI root and the device, MSIs are disabled.
Brice Goglin0cc2b372006-10-05 10:24:42 +0200494
Matthew Wilcoxc41ade22009-03-17 08:54:05 -0400495It is also worth checking the device driver to see whether it supports MSIs.
Alexander Gordeev302a2522013-12-30 08:28:16 +0100496For example, it may contain calls to pci_enable_msi_range() or
497pci_enable_msix_range().