xref: /linux/Documentation/PCI/msi-howto.rst (revision 53597deca0e38c30e6cd4ba2114fa42d2bcd85bb)
1.. SPDX-License-Identifier: GPL-2.0
2.. include:: <isonum.txt>
3
4==========================
5The MSI Driver Guide HOWTO
6==========================
7
8:Authors: Tom L Nguyen; Martine Silbermann; Matthew Wilcox
9
10:Copyright: 2003, 2008 Intel Corporation
11
12About this guide
13================
14
15This guide describes the basics of Message Signaled Interrupts (MSIs),
16the advantages of using MSI over traditional interrupt mechanisms, how
17to change your driver to use MSI or MSI-X and some basic diagnostics to
18try if a device doesn't support MSIs.
19
20
21What are MSIs?
22==============
23
24A Message Signaled Interrupt is a write from the device to a special
25address which causes an interrupt to be received by the CPU.
26
27The MSI capability was first specified in PCI 2.2 and was later enhanced
28in PCI 3.0 to allow each interrupt to be masked individually.  The MSI-X
29capability was also introduced with PCI 3.0.  It supports more interrupts
30per device than MSI and allows interrupts to be independently configured.
31
32Devices may support both MSI and MSI-X, but only one can be enabled at
33a time.
34
35
36Why use MSIs?
37=============
38
39There are three reasons why using MSIs can give an advantage over
40traditional pin-based interrupts.
41
42Pin-based PCI interrupts are often shared amongst several devices.
43To support this, the kernel must call each interrupt handler associated
44with an interrupt, which leads to reduced performance for the system as
45a whole.  MSIs are never shared, so this problem cannot arise.
46
47When a device writes data to memory, then raises a pin-based interrupt,
48it is possible that the interrupt may arrive before all the data has
49arrived in memory (this becomes more likely with devices behind PCI-PCI
50bridges).  In order to ensure that all the data has arrived in memory,
51the interrupt handler must read a register on the device which raised
52the interrupt.  PCI transaction ordering rules require that all the data
53arrive in memory before the value may be returned from the register.
54Using MSIs avoids this problem as the interrupt-generating write cannot
55pass the data writes, so by the time the interrupt is raised, the driver
56knows that all the data has arrived in memory.
57
58PCI devices can only support a single pin-based interrupt per function.
59Often drivers have to query the device to find out what event has
60occurred, slowing down interrupt handling for the common case.  With
61MSIs, a device can support more interrupts, allowing each interrupt
62to be specialised to a different purpose.  One possible design gives
63infrequent conditions (such as errors) their own interrupt which allows
64the driver to handle the normal interrupt handling path more efficiently.
65Other possible designs include giving one interrupt to each packet queue
66in a network card or each port in a storage controller.
67
68
69How to use MSIs
70===============
71
72PCI devices are initialised to use pin-based interrupts.  The device
73driver has to set up the device to use MSI or MSI-X.  Not all machines
74support MSIs correctly, and for those machines, the APIs described below
75will simply fail and the device will continue to use pin-based interrupts.
76
77Include kernel support for MSIs
78-------------------------------
79
80To support MSI or MSI-X, the kernel must be built with the CONFIG_PCI_MSI
81option enabled.  This option is only available on some architectures,
82and it may depend on some other options also being set.  For example,
83on x86, you must also enable X86_UP_APIC or SMP in order to see the
84CONFIG_PCI_MSI option.
85
86Using MSI
87---------
88
89Most of the hard work is done for the driver in the PCI layer.  The driver
90simply has to request that the PCI layer set up the MSI capability for this
91device.
92
93To automatically use MSI or MSI-X interrupt vectors, use the following
94function::
95
96  int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
97		unsigned int max_vecs, unsigned int flags);
98
99which allocates up to max_vecs interrupt vectors for a PCI device.  It
100returns the number of vectors allocated or a negative error.  If the device
101has a requirement for a minimum number of vectors the driver can pass a
102min_vecs argument set to this limit, and the PCI core will return -ENOSPC
103if it can't meet the minimum number of vectors.
104
105The flags argument is used to specify which type of interrupt can be used
106by the device and the driver (PCI_IRQ_INTX, PCI_IRQ_MSI, PCI_IRQ_MSIX).
107A convenient short-hand (PCI_IRQ_ALL_TYPES) is also available to ask for
108any possible kind of interrupt.  If the PCI_IRQ_AFFINITY flag is set,
109pci_alloc_irq_vectors() will spread the interrupts around the available CPUs.
110
111To get the Linux IRQ numbers passed to request_irq() and free_irq() and the
112vectors, use the following function::
113
114  int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
115
116If the driver enables the device using pcim_enable_device(), the driver
117shouldn't call pci_free_irq_vectors() because pcim_enable_device()
118activates automatic management for IRQ vectors. Otherwise, the driver should
119free any allocated IRQ vectors before removing the device using the following
120function::
121
122  void pci_free_irq_vectors(struct pci_dev *dev);
123
124If a device supports both MSI-X and MSI capabilities, this API will use the
125MSI-X facilities in preference to the MSI facilities.  MSI-X supports any
126number of interrupts between 1 and 2048.  In contrast, MSI is restricted to
127a maximum of 32 interrupts (and must be a power of two).  In addition, the
128MSI interrupt vectors must be allocated consecutively, so the system might
129not be able to allocate as many vectors for MSI as it could for MSI-X.  On
130some platforms, MSI interrupts must all be targeted at the same set of CPUs
131whereas MSI-X interrupts can all be targeted at different CPUs.
132
133If a device supports neither MSI-X nor MSI it will fall back to a single
134legacy IRQ vector.
135
136The typical usage of MSI or MSI-X interrupts is to allocate as many vectors
137as possible, likely up to the limit supported by the device.  If nvec is
138larger than the number supported by the device it will automatically be
139capped to the supported limit, so there is no need to query the number of
140vectors supported beforehand::
141
142	nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_ALL_TYPES)
143	if (nvec < 0)
144		goto out_err;
145
146If a driver is unable or unwilling to deal with a variable number of MSI
147interrupts it can request a particular number of interrupts by passing that
148number to pci_alloc_irq_vectors() function as both 'min_vecs' and
149'max_vecs' parameters::
150
151	ret = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_ALL_TYPES);
152	if (ret < 0)
153		goto out_err;
154
155The most notorious example of the request type described above is enabling
156the single MSI mode for a device.  It could be done by passing two 1s as
157'min_vecs' and 'max_vecs'::
158
159	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
160	if (ret < 0)
161		goto out_err;
162
163Some devices might not support using legacy line interrupts, in which case
164the driver can specify that only MSI or MSI-X is acceptable::
165
166	nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_MSI | PCI_IRQ_MSIX);
167	if (nvec < 0)
168		goto out_err;
169
170Legacy APIs
171-----------
172
173The following old APIs to enable and disable MSI or MSI-X interrupts should
174not be used in new code::
175
176  pci_enable_msi()		/* deprecated */
177  pci_disable_msi()		/* deprecated */
178  pci_enable_msix_range()	/* deprecated */
179  pci_enable_msix_exact()	/* deprecated */
180  pci_disable_msix()		/* deprecated */
181
182Additionally there are APIs to provide the number of supported MSI or MSI-X
183vectors: pci_msi_vec_count() and pci_msix_vec_count().  In general these
184should be avoided in favor of letting pci_alloc_irq_vectors() cap the
185number of vectors.  If you have a legitimate special use case for the count
186of vectors we might have to revisit that decision and add a
187pci_nr_irq_vectors() helper that handles MSI and MSI-X transparently.
188
189Considerations when using MSIs
190------------------------------
191
192Spinlocks
193~~~~~~~~~
194
195Most device drivers have a per-device spinlock which is taken in the
196interrupt handler.  With pin-based interrupts or a single MSI, it is not
197necessary to disable interrupts (Linux guarantees the same interrupt will
198not be re-entered).  If a device uses multiple interrupts, the driver
199must disable interrupts while the lock is held.  If the device sends
200a different interrupt, the driver will deadlock trying to recursively
201acquire the spinlock.  Such deadlocks can be avoided by using
202spin_lock_irqsave() or spin_lock_irq() which disable local interrupts
203and acquire the lock (see Documentation/kernel-hacking/locking.rst).
204
205How to tell whether MSI/MSI-X is enabled on a device
206----------------------------------------------------
207
208Using 'lspci -v' (as root) may show some devices with "MSI", "Message
209Signaled Interrupts" or "MSI-X" capabilities.  Each of these capabilities
210has an 'Enable' flag which is followed with either "+" (enabled)
211or "-" (disabled).
212
213
214MSI quirks
215==========
216
217Several PCI chipsets or devices are known not to support MSIs.
218The PCI stack provides three ways to disable MSIs:
219
2201. globally
2212. on all devices behind a specific bridge
2223. on a single device
223
224Disabling MSIs globally
225-----------------------
226
227Some host chipsets simply don't support MSIs properly.  If we're
228lucky, the manufacturer knows this and has indicated it in the ACPI
229FADT table.  In this case, Linux automatically disables MSIs.
230Some boards don't include this information in the table and so we have
231to detect them ourselves.  The complete list of these is found near the
232quirk_disable_all_msi() function in drivers/pci/quirks.c.
233
234If you have a board which has problems with MSIs, you can pass pci=nomsi
235on the kernel command line to disable MSIs on all devices.  It would be
236in your best interests to report the problem to linux-pci@vger.kernel.org
237including a full 'lspci -v' so we can add the quirks to the kernel.
238
239Disabling MSIs below a bridge
240-----------------------------
241
242Some PCI bridges are not able to route MSIs between buses properly.
243In this case, MSIs must be disabled on all devices behind the bridge.
244
245Some bridges allow you to enable MSIs by changing some bits in their
246PCI configuration space (especially the Hypertransport chipsets such
247as the nVidia nForce and Serverworks HT2000).  As with host chipsets,
248Linux mostly knows about them and automatically enables MSIs if it can.
249If you have a bridge unknown to Linux, you can enable
250MSIs in configuration space using whatever method you know works, then
251enable MSIs on that bridge by doing::
252
253       echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
254
255where $bridge is the PCI address of the bridge you've enabled (eg
2560000:00:0e.0).
257
258To disable MSIs, echo 0 instead of 1.  Changing this value should be
259done with caution as it could break interrupt handling for all devices
260below this bridge.
261
262Again, please notify linux-pci@vger.kernel.org of any bridges that need
263special handling.
264
265Disabling MSIs on a single device
266---------------------------------
267
268Some devices are known to have faulty MSI implementations.  Usually this
269is handled in the individual device driver, but occasionally it's necessary
270to handle this with a quirk.  Some drivers have an option to disable use
271of MSI.  While this is a convenient workaround for the driver author,
272it is not good practice, and should not be emulated.
273
274Finding why MSIs are disabled on a device
275-----------------------------------------
276
277From the above three sections, you can see that there are many reasons
278why MSIs may not be enabled for a given device.  Your first step should
279be to examine your dmesg carefully to determine whether MSIs are enabled
280for your machine.  You should also check your .config to be sure you
281have enabled CONFIG_PCI_MSI.
282
283Then, 'lspci -t' gives the list of bridges above a device. Reading
284`/sys/bus/pci/devices/*/msi_bus` will tell you whether MSIs are enabled (1)
285or disabled (0).  If 0 is found in any of the msi_bus files belonging
286to bridges between the PCI root and the device, MSIs are disabled.
287
288It is also worth checking the device driver to see whether it supports MSIs.
289For example, it may contain calls to pci_alloc_irq_vectors() with the
290PCI_IRQ_MSI or PCI_IRQ_MSIX flags.
291
292
293List of device drivers MSI(-X) APIs
294===================================
295
296The PCI/MSI subsystem has a dedicated C file for its exported device driver
297APIs — `drivers/pci/msi/api.c`. The following functions are exported:
298
299.. kernel-doc:: drivers/pci/msi/api.c
300   :export:
301