0001 .. SPDX-License-Identifier: GPL-2.0
0002 .. include:: <isonum.txt>
0003
0004 ==========================
0005 The MSI Driver Guide HOWTO
0006 ==========================
0007
0008 :Authors: Tom L Nguyen; Martine Silbermann; Matthew Wilcox
0009
0010 :Copyright: 2003, 2008 Intel Corporation
0011
0012 About this guide
0013 ================
0014
0015 This guide describes the basics of Message Signaled Interrupts (MSIs),
0016 the advantages of using MSI over traditional interrupt mechanisms, how
0017 to change your driver to use MSI or MSI-X and some basic diagnostics to
0018 try if a device doesn't support MSIs.
0019
0020
0021 What are MSIs?
0022 ==============
0023
0024 A Message Signaled Interrupt is a write from the device to a special
0025 address which causes an interrupt to be received by the CPU.
0026
0027 The MSI capability was first specified in PCI 2.2 and was later enhanced
0028 in PCI 3.0 to allow each interrupt to be masked individually. The MSI-X
0029 capability was also introduced with PCI 3.0. It supports more interrupts
0030 per device than MSI and allows interrupts to be independently configured.
0031
0032 Devices may support both MSI and MSI-X, but only one can be enabled at
0033 a time.
0034
0035
0036 Why use MSIs?
0037 =============
0038
0039 There are three reasons why using MSIs can give an advantage over
0040 traditional pin-based interrupts.
0041
0042 Pin-based PCI interrupts are often shared amongst several devices.
0043 To support this, the kernel must call each interrupt handler associated
0044 with an interrupt, which leads to reduced performance for the system as
0045 a whole. MSIs are never shared, so this problem cannot arise.
0046
0047 When a device writes data to memory, then raises a pin-based interrupt,
0048 it is possible that the interrupt may arrive before all the data has
0049 arrived in memory (this becomes more likely with devices behind PCI-PCI
0050 bridges). In order to ensure that all the data has arrived in memory,
0051 the interrupt handler must read a register on the device which raised
0052 the interrupt. PCI transaction ordering rules require that all the data
0053 arrive in memory before the value may be returned from the register.
0054 Using MSIs avoids this problem as the interrupt-generating write cannot
0055 pass the data writes, so by the time the interrupt is raised, the driver
0056 knows that all the data has arrived in memory.
0057
0058 PCI devices can only support a single pin-based interrupt per function.
0059 Often drivers have to query the device to find out what event has
0060 occurred, slowing down interrupt handling for the common case. With
0061 MSIs, a device can support more interrupts, allowing each interrupt
0062 to be specialised to a different purpose. One possible design gives
0063 infrequent conditions (such as errors) their own interrupt which allows
0064 the driver to handle the normal interrupt handling path more efficiently.
0065 Other possible designs include giving one interrupt to each packet queue
0066 in a network card or each port in a storage controller.
0067
0068
0069 How to use MSIs
0070 ===============
0071
0072 PCI devices are initialised to use pin-based interrupts. The device
0073 driver has to set up the device to use MSI or MSI-X. Not all machines
0074 support MSIs correctly, and for those machines, the APIs described below
0075 will simply fail and the device will continue to use pin-based interrupts.
0076
0077 Include kernel support for MSIs
0078 -------------------------------
0079
0080 To support MSI or MSI-X, the kernel must be built with the CONFIG_PCI_MSI
0081 option enabled. This option is only available on some architectures,
0082 and it may depend on some other options also being set. For example,
0083 on x86, you must also enable X86_UP_APIC or SMP in order to see the
0084 CONFIG_PCI_MSI option.
0085
0086 Using MSI
0087 ---------
0088
0089 Most of the hard work is done for the driver in the PCI layer. The driver
0090 simply has to request that the PCI layer set up the MSI capability for this
0091 device.
0092
0093 To automatically use MSI or MSI-X interrupt vectors, use the following
0094 function::
0095
0096 int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
0097 unsigned int max_vecs, unsigned int flags);
0098
0099 which allocates up to max_vecs interrupt vectors for a PCI device. It
0100 returns the number of vectors allocated or a negative error. If the device
0101 has a requirements for a minimum number of vectors the driver can pass a
0102 min_vecs argument set to this limit, and the PCI core will return -ENOSPC
0103 if it can't meet the minimum number of vectors.
0104
0105 The flags argument is used to specify which type of interrupt can be used
0106 by the device and the driver (PCI_IRQ_LEGACY, PCI_IRQ_MSI, PCI_IRQ_MSIX).
0107 A convenient short-hand (PCI_IRQ_ALL_TYPES) is also available to ask for
0108 any possible kind of interrupt. If the PCI_IRQ_AFFINITY flag is set,
0109 pci_alloc_irq_vectors() will spread the interrupts around the available CPUs.
0110
0111 To get the Linux IRQ numbers passed to request_irq() and free_irq() and the
0112 vectors, use the following function::
0113
0114 int pci_irq_vector(struct pci_dev *dev, unsigned int nr);
0115
0116 Any allocated resources should be freed before removing the device using
0117 the following function::
0118
0119 void pci_free_irq_vectors(struct pci_dev *dev);
0120
0121 If a device supports both MSI-X and MSI capabilities, this API will use the
0122 MSI-X facilities in preference to the MSI facilities. MSI-X supports any
0123 number of interrupts between 1 and 2048. In contrast, MSI is restricted to
0124 a maximum of 32 interrupts (and must be a power of two). In addition, the
0125 MSI interrupt vectors must be allocated consecutively, so the system might
0126 not be able to allocate as many vectors for MSI as it could for MSI-X. On
0127 some platforms, MSI interrupts must all be targeted at the same set of CPUs
0128 whereas MSI-X interrupts can all be targeted at different CPUs.
0129
0130 If a device supports neither MSI-X or MSI it will fall back to a single
0131 legacy IRQ vector.
0132
0133 The typical usage of MSI or MSI-X interrupts is to allocate as many vectors
0134 as possible, likely up to the limit supported by the device. If nvec is
0135 larger than the number supported by the device it will automatically be
0136 capped to the supported limit, so there is no need to query the number of
0137 vectors supported beforehand::
0138
0139 nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_ALL_TYPES)
0140 if (nvec < 0)
0141 goto out_err;
0142
0143 If a driver is unable or unwilling to deal with a variable number of MSI
0144 interrupts it can request a particular number of interrupts by passing that
0145 number to pci_alloc_irq_vectors() function as both 'min_vecs' and
0146 'max_vecs' parameters::
0147
0148 ret = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_ALL_TYPES);
0149 if (ret < 0)
0150 goto out_err;
0151
0152 The most notorious example of the request type described above is enabling
0153 the single MSI mode for a device. It could be done by passing two 1s as
0154 'min_vecs' and 'max_vecs'::
0155
0156 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
0157 if (ret < 0)
0158 goto out_err;
0159
0160 Some devices might not support using legacy line interrupts, in which case
0161 the driver can specify that only MSI or MSI-X is acceptable::
0162
0163 nvec = pci_alloc_irq_vectors(pdev, 1, nvec, PCI_IRQ_MSI | PCI_IRQ_MSIX);
0164 if (nvec < 0)
0165 goto out_err;
0166
0167 Legacy APIs
0168 -----------
0169
0170 The following old APIs to enable and disable MSI or MSI-X interrupts should
0171 not be used in new code::
0172
0173 pci_enable_msi() /* deprecated */
0174 pci_disable_msi() /* deprecated */
0175 pci_enable_msix_range() /* deprecated */
0176 pci_enable_msix_exact() /* deprecated */
0177 pci_disable_msix() /* deprecated */
0178
0179 Additionally there are APIs to provide the number of supported MSI or MSI-X
0180 vectors: pci_msi_vec_count() and pci_msix_vec_count(). In general these
0181 should be avoided in favor of letting pci_alloc_irq_vectors() cap the
0182 number of vectors. If you have a legitimate special use case for the count
0183 of vectors we might have to revisit that decision and add a
0184 pci_nr_irq_vectors() helper that handles MSI and MSI-X transparently.
0185
0186 Considerations when using MSIs
0187 ------------------------------
0188
0189 Spinlocks
0190 ~~~~~~~~~
0191
0192 Most device drivers have a per-device spinlock which is taken in the
0193 interrupt handler. With pin-based interrupts or a single MSI, it is not
0194 necessary to disable interrupts (Linux guarantees the same interrupt will
0195 not be re-entered). If a device uses multiple interrupts, the driver
0196 must disable interrupts while the lock is held. If the device sends
0197 a different interrupt, the driver will deadlock trying to recursively
0198 acquire the spinlock. Such deadlocks can be avoided by using
0199 spin_lock_irqsave() or spin_lock_irq() which disable local interrupts
0200 and acquire the lock (see Documentation/kernel-hacking/locking.rst).
0201
0202 How to tell whether MSI/MSI-X is enabled on a device
0203 ----------------------------------------------------
0204
0205 Using 'lspci -v' (as root) may show some devices with "MSI", "Message
0206 Signalled Interrupts" or "MSI-X" capabilities. Each of these capabilities
0207 has an 'Enable' flag which is followed with either "+" (enabled)
0208 or "-" (disabled).
0209
0210
0211 MSI quirks
0212 ==========
0213
0214 Several PCI chipsets or devices are known not to support MSIs.
0215 The PCI stack provides three ways to disable MSIs:
0216
0217 1. globally
0218 2. on all devices behind a specific bridge
0219 3. on a single device
0220
0221 Disabling MSIs globally
0222 -----------------------
0223
0224 Some host chipsets simply don't support MSIs properly. If we're
0225 lucky, the manufacturer knows this and has indicated it in the ACPI
0226 FADT table. In this case, Linux automatically disables MSIs.
0227 Some boards don't include this information in the table and so we have
0228 to detect them ourselves. The complete list of these is found near the
0229 quirk_disable_all_msi() function in drivers/pci/quirks.c.
0230
0231 If you have a board which has problems with MSIs, you can pass pci=nomsi
0232 on the kernel command line to disable MSIs on all devices. It would be
0233 in your best interests to report the problem to linux-pci@vger.kernel.org
0234 including a full 'lspci -v' so we can add the quirks to the kernel.
0235
0236 Disabling MSIs below a bridge
0237 -----------------------------
0238
0239 Some PCI bridges are not able to route MSIs between busses properly.
0240 In this case, MSIs must be disabled on all devices behind the bridge.
0241
0242 Some bridges allow you to enable MSIs by changing some bits in their
0243 PCI configuration space (especially the Hypertransport chipsets such
0244 as the nVidia nForce and Serverworks HT2000). As with host chipsets,
0245 Linux mostly knows about them and automatically enables MSIs if it can.
0246 If you have a bridge unknown to Linux, you can enable
0247 MSIs in configuration space using whatever method you know works, then
0248 enable MSIs on that bridge by doing::
0249
0250 echo 1 > /sys/bus/pci/devices/$bridge/msi_bus
0251
0252 where $bridge is the PCI address of the bridge you've enabled (eg
0253 0000:00:0e.0).
0254
0255 To disable MSIs, echo 0 instead of 1. Changing this value should be
0256 done with caution as it could break interrupt handling for all devices
0257 below this bridge.
0258
0259 Again, please notify linux-pci@vger.kernel.org of any bridges that need
0260 special handling.
0261
0262 Disabling MSIs on a single device
0263 ---------------------------------
0264
0265 Some devices are known to have faulty MSI implementations. Usually this
0266 is handled in the individual device driver, but occasionally it's necessary
0267 to handle this with a quirk. Some drivers have an option to disable use
0268 of MSI. While this is a convenient workaround for the driver author,
0269 it is not good practice, and should not be emulated.
0270
0271 Finding why MSIs are disabled on a device
0272 -----------------------------------------
0273
0274 From the above three sections, you can see that there are many reasons
0275 why MSIs may not be enabled for a given device. Your first step should
0276 be to examine your dmesg carefully to determine whether MSIs are enabled
0277 for your machine. You should also check your .config to be sure you
0278 have enabled CONFIG_PCI_MSI.
0279
0280 Then, 'lspci -t' gives the list of bridges above a device. Reading
0281 `/sys/bus/pci/devices/*/msi_bus` will tell you whether MSIs are enabled (1)
0282 or disabled (0). If 0 is found in any of the msi_bus files belonging
0283 to bridges between the PCI root and the device, MSIs are disabled.
0284
0285 It is also worth checking the device driver to see whether it supports MSIs.
0286 For example, it may contain calls to pci_alloc_irq_vectors() with the
0287 PCI_IRQ_MSI or PCI_IRQ_MSIX flags.