PCI/MSI: Make pci_enable_msi/msix() 'nvec' argument type as int
[linux-2.6-block.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
854.2.1 pci_enable_msi
1da177e4
LT
86
87int pci_enable_msi(struct pci_dev *dev)
88
4979de6e
MW
89A successful call allocates ONE interrupt to the device, regardless
90of how many MSIs the device supports. The device is switched from
c41ade2e 91pin-based interrupt mode to MSI mode. The dev->irq number is changed
4979de6e
MW
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.
1da177e4 96
1c8d7b0a
MW
974.2.2 pci_enable_msi_block
98
99int pci_enable_msi_block(struct pci_dev *dev, int count)
100
101This variation on the above call allows a device driver to request multiple
102MSIs. The MSI specification only allows interrupts to be allocated in
103powers of two, up to a maximum of 2^5 (32).
104
105If this function returns 0, it has succeeded in allocating at least as many
106interrupts as the driver requested (it may have allocated more in order
107to satisfy the power-of-two requirement). In this case, the function
108enables MSI on this device and updates dev->irq to be the lowest of
109the new interrupts assigned to it. The other interrupts assigned to
110the device are in the range dev->irq to dev->irq + count - 1.
111
112If this function returns a negative number, it indicates an error and
113the driver should not attempt to request any more MSI interrupts for
4979de6e
MW
114this device. If this function returns a positive number, it is
115less than 'count' and indicates the number of interrupts that could have
116been allocated. In neither case is the irq value updated or the device
117switched into MSI mode.
1c8d7b0a
MW
118
119The device driver must decide what action to take if
a2d4d501 120pci_enable_msi_block() returns a value less than the number requested.
1d15afcc
MW
121For instance, the driver could still make use of fewer interrupts;
122in this case the driver should call pci_enable_msi_block()
1c8d7b0a
MW
123again. Note that it is not guaranteed to succeed, even when the
124'count' has been reduced to the value returned from a previous call to
125pci_enable_msi_block(). This is because there are multiple constraints
126on the number of vectors that can be allocated; pci_enable_msi_block()
4979de6e 127returns as soon as it finds any constraint that doesn't allow the
1c8d7b0a
MW
128call to succeed.
129
08261d87
AG
1304.2.3 pci_enable_msi_block_auto
131
52179dc9 132int pci_enable_msi_block_auto(struct pci_dev *dev, int *count)
08261d87
AG
133
134This variation on pci_enable_msi() call allows a device driver to request
135the maximum possible number of MSIs. The MSI specification only allows
136interrupts to be allocated in powers of two, up to a maximum of 2^5 (32).
137
138If this function returns a positive number, it indicates that it has
139succeeded and the returned value is the number of allocated interrupts. In
140this case, the function enables MSI on this device and updates dev->irq to
141be the lowest of the new interrupts assigned to it. The other interrupts
142assigned to the device are in the range dev->irq to dev->irq + returned
143value - 1.
144
145If this function returns a negative number, it indicates an error and
146the driver should not attempt to request any more MSI interrupts for
147this device.
148
149If the device driver needs to know the number of interrupts the device
150supports it can pass the pointer count where that number is stored. The
151device driver must decide what action to take if pci_enable_msi_block_auto()
152succeeds, but returns a value less than the number of interrupts supported.
153If the device driver does not need to know the number of interrupts
154supported, it can set the pointer count to NULL.
155
1564.2.4 pci_disable_msi
1da177e4
LT
157
158void pci_disable_msi(struct pci_dev *dev)
159
1c8d7b0a 160This function should be used to undo the effect of pci_enable_msi() or
08261d87
AG
161pci_enable_msi_block() or pci_enable_msi_block_auto(). Calling it restores
162dev->irq to the pin-based interrupt number and frees the previously
163allocated message signaled interrupt(s). The interrupt may subsequently be
164assigned to another device, so drivers should not cache the value of
165dev->irq.
1da177e4 166
263d8d57
MW
167Before calling this function, a device driver must always call free_irq()
168on any interrupt for which it previously called request_irq().
4979de6e
MW
169Failure to do so results in a BUG_ON(), leaving the device with
170MSI enabled and thus leaking its vector.
1da177e4 171
c41ade2e 1724.3 Using MSI-X
1da177e4 173
c41ade2e
MW
174The MSI-X capability is much more flexible than the MSI capability.
175It supports up to 2048 interrupts, each of which can be controlled
176independently. To support this flexibility, drivers must use an array of
177`struct msix_entry':
1da177e4
LT
178
179struct msix_entry {
180 u16 vector; /* kernel uses to write alloc vector */
181 u16 entry; /* driver uses to specify entry */
182};
183
c41ade2e 184This allows for the device to use these interrupts in a sparse fashion;
e4439236 185for example, it could use interrupts 3 and 1027 and yet allocate only a
c41ade2e 186two-element array. The driver is expected to fill in the 'entry' value
e4439236
MW
187in each element of the array to indicate for which entries the kernel
188should assign interrupts; it is invalid to fill in two entries with the
c41ade2e
MW
189same number.
190
1914.3.1 pci_enable_msix
192
193int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
194
195Calling this function asks the PCI subsystem to allocate 'nvec' MSIs.
196The 'entries' argument is a pointer to an array of msix_entry structs
197which should be at least 'nvec' entries in size. On success, the
4979de6e
MW
198device is switched into MSI-X mode and the function returns 0.
199The 'vector' member in each entry is populated with the interrupt number;
200the driver should then call request_irq() for each 'vector' that it
6457d9b3
MW
201decides to use. The device driver is responsible for keeping track of the
202interrupts assigned to the MSI-X vectors so it can free them again later.
c41ade2e
MW
203
204If this function returns a negative number, it indicates an error and
205the driver should not attempt to allocate any more MSI-X interrupts for
206this device. If it returns a positive number, it indicates the maximum
fafad5bf
ME
207number of interrupt vectors that could have been allocated. See example
208below.
c41ade2e
MW
209
210This function, in contrast with pci_enable_msi(), does not adjust
211dev->irq. The device will not generate interrupts for this interrupt
6457d9b3 212number once MSI-X is enabled.
c41ade2e
MW
213
214Device drivers should normally call this function once per device
215during the initialization phase.
216
5a84fc31 217It is ideal if drivers can cope with a variable number of MSI-X interrupts;
fafad5bf 218there are many reasons why the platform may not be able to provide the
ed737c18 219exact number that a driver asks for.
fafad5bf
ME
220
221A request loop to achieve that might look like:
222
223static int foo_driver_enable_msix(struct foo_adapter *adapter, int nvec)
224{
225 while (nvec >= FOO_DRIVER_MINIMUM_NVEC) {
226 rc = pci_enable_msix(adapter->pdev,
227 adapter->msix_entries, nvec);
228 if (rc > 0)
229 nvec = rc;
230 else
231 return rc;
232 }
233
234 return -ENOSPC;
235}
236
c41ade2e 2374.3.2 pci_disable_msix
1da177e4
LT
238
239void pci_disable_msix(struct pci_dev *dev)
240
e6ffceb0 241This function should be used to undo the effect of pci_enable_msix(). It frees
c41ade2e
MW
242the previously allocated message signaled interrupts. The interrupts may
243subsequently be assigned to another device, so drivers should not cache
244the value of the 'vector' elements over a call to pci_disable_msix().
245
263d8d57
MW
246Before calling this function, a device driver must always call free_irq()
247on any interrupt for which it previously called request_irq().
4979de6e
MW
248Failure to do so results in a BUG_ON(), leaving the device with
249MSI-X enabled and thus leaking its vector.
c41ade2e
MW
250
2514.3.3 The MSI-X Table
252
253The MSI-X capability specifies a BAR and offset within that BAR for the
254MSI-X Table. This address is mapped by the PCI subsystem, and should not
255be accessed directly by the device driver. If the driver wishes to
256mask or unmask an interrupt, it should call disable_irq() / enable_irq().
257
2584.4 Handling devices implementing both MSI and MSI-X capabilities
259
260If a device implements both MSI and MSI-X capabilities, it can
e14bd7e6 261run in either MSI mode or MSI-X mode, but not both simultaneously.
c41ade2e
MW
262This is a requirement of the PCI spec, and it is enforced by the
263PCI layer. Calling pci_enable_msi() when MSI-X is already enabled or
4979de6e 264pci_enable_msix() when MSI is already enabled results in an error.
c41ade2e
MW
265If a device driver wishes to switch between MSI and MSI-X at runtime,
266it must first quiesce the device, then switch it back to pin-interrupt
267mode, before calling pci_enable_msi() or pci_enable_msix() and resuming
268operation. This is not expected to be a common operation but may be
269useful for debugging or testing during development.
270
2714.5 Considerations when using MSIs
272
2734.5.1 Choosing between MSI-X and MSI
274
275If your device supports both MSI-X and MSI capabilities, you should use
276the MSI-X facilities in preference to the MSI facilities. As mentioned
277above, MSI-X supports any number of interrupts between 1 and 2048.
278In constrast, MSI is restricted to a maximum of 32 interrupts (and
279must be a power of two). In addition, the MSI interrupt vectors must
952df55b 280be allocated consecutively, so the system might not be able to allocate
c41ade2e 281as many vectors for MSI as it could for MSI-X. On some platforms, MSI
25985edc
LDM
282interrupts must all be targeted at the same set of CPUs whereas MSI-X
283interrupts can all be targeted at different CPUs.
c41ade2e
MW
284
2854.5.2 Spinlocks
286
287Most device drivers have a per-device spinlock which is taken in the
288interrupt handler. With pin-based interrupts or a single MSI, it is not
289necessary to disable interrupts (Linux guarantees the same interrupt will
290not be re-entered). If a device uses multiple interrupts, the driver
291must disable interrupts while the lock is held. If the device sends
292a different interrupt, the driver will deadlock trying to recursively
293acquire the spinlock.
294
295There are two solutions. The first is to take the lock with
296spin_lock_irqsave() or spin_lock_irq() (see
297Documentation/DocBook/kernel-locking). The second is to specify
298IRQF_DISABLED to request_irq() so that the kernel runs the entire
299interrupt routine with interrupts disabled.
300
301If your MSI interrupt routine does not hold the lock for the whole time
302it is running, the first solution may be best. The second solution is
303normally preferred as it avoids making two transitions from interrupt
304disabled to enabled and back again.
305
3064.6 How to tell whether MSI/MSI-X is enabled on a device
307
308Using 'lspci -v' (as root) may show some devices with "MSI", "Message
309Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
4979de6e 310has an 'Enable' flag which is followed with either "+" (enabled)
c41ade2e
MW
311or "-" (disabled).
312
313
3145. MSI quirks
315
316Several PCI chipsets or devices are known not to support MSIs.
317The PCI stack provides three ways to disable MSIs:
318
3191. globally
3202. on all devices behind a specific bridge
3213. on a single device
322
3235.1. Disabling MSIs globally
324
325Some host chipsets simply don't support MSIs properly. If we're
326lucky, the manufacturer knows this and has indicated it in the ACPI
4979de6e 327FADT table. In this case, Linux automatically disables MSIs.
c41ade2e
MW
328Some boards don't include this information in the table and so we have
329to detect them ourselves. The complete list of these is found near the
330quirk_disable_all_msi() function in drivers/pci/quirks.c.
331
332If you have a board which has problems with MSIs, you can pass pci=nomsi
333on the kernel command line to disable MSIs on all devices. It would be
334in your best interests to report the problem to linux-pci@vger.kernel.org
335including a full 'lspci -v' so we can add the quirks to the kernel.
336
3375.2. Disabling MSIs below a bridge
338
339Some PCI bridges are not able to route MSIs between busses properly.
340In this case, MSIs must be disabled on all devices behind the bridge.
341
342Some bridges allow you to enable MSIs by changing some bits in their
343PCI configuration space (especially the Hypertransport chipsets such
344as the nVidia nForce and Serverworks HT2000). As with host chipsets,
345Linux mostly knows about them and automatically enables MSIs if it can.
e6b85a1f 346If you have a bridge unknown to Linux, you can enable
c41ade2e
MW
347MSIs in configuration space using whatever method you know works, then
348enable MSIs on that bridge by doing:
349
350 echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
351
352where $bridge is the PCI address of the bridge you've enabled (eg
3530000:00:0e.0).
354
355To disable MSIs, echo 0 instead of 1. Changing this value should be
1b8386f6 356done with caution as it could break interrupt handling for all devices
c41ade2e
MW
357below this bridge.
358
359Again, please notify linux-pci@vger.kernel.org of any bridges that need
360special handling.
361
3625.3. Disabling MSIs on a single device
363
364Some devices are known to have faulty MSI implementations. Usually this
c2b65e18 365is handled in the individual device driver, but occasionally it's necessary
c41ade2e
MW
366to handle this with a quirk. Some drivers have an option to disable use
367of MSI. While this is a convenient workaround for the driver author,
368it is not good practise, and should not be emulated.
369
3705.4. Finding why MSIs are disabled on a device
371
372From the above three sections, you can see that there are many reasons
373why MSIs may not be enabled for a given device. Your first step should
374be to examine your dmesg carefully to determine whether MSIs are enabled
375for your machine. You should also check your .config to be sure you
376have enabled CONFIG_PCI_MSI.
377
378Then, 'lspci -t' gives the list of bridges above a device. Reading
798c794d 379/sys/bus/pci/devices/*/msi_bus will tell you whether MSIs are enabled (1)
c41ade2e
MW
380or disabled (0). If 0 is found in any of the msi_bus files belonging
381to bridges between the PCI root and the device, MSIs are disabled.
382
383It is also worth checking the device driver to see whether it supports MSIs.
384For example, it may contain calls to pci_enable_msi(), pci_enable_msix() or
385pci_enable_msi_block().