blob: c19c7ca581868a366289932163621ad8ae794c34 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * PCI Message Signaled Interrupt (MSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
Christoph Hellwigaff17162016-07-12 18:20:17 +09007 * Copyright (C) 2016 Christoph Hellwig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Thomas Gleixner29a03ad2021-12-06 23:27:44 +01009#include <linux/err.h>
10#include <linux/export.h>
11#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Thomas Gleixner288c81c2021-12-06 23:27:47 +010013#include "../pci.h"
Thomas Gleixneraa423ac2021-12-06 23:27:52 +010014#include "msi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016static int pci_msi_enable = 1;
Yijing Wang38737d82014-10-27 10:44:36 +080017int pci_msi_ignore_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020019static noinline void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Thomas Gleixnercd119b02021-12-06 23:27:56 +010021 raw_spinlock_t *lock = &to_pci_dev(desc->dev)->msi_lock;
Thomas Gleixner77e89af2021-07-29 23:51:47 +020022 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Thomas Gleixnere58f2252021-12-06 23:27:39 +010024 if (!desc->pci.msi_attrib.can_mask)
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +010025 return;
26
Thomas Gleixner77e89af2021-07-29 23:51:47 +020027 raw_spin_lock_irqsave(lock, flags);
Thomas Gleixnere58f2252021-12-06 23:27:39 +010028 desc->pci.msi_mask &= ~clear;
29 desc->pci.msi_mask |= set;
30 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->pci.mask_pos,
31 desc->pci.msi_mask);
Thomas Gleixner77e89af2021-07-29 23:51:47 +020032 raw_spin_unlock_irqrestore(lock, flags);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +090033}
34
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020035static inline void pci_msi_mask(struct msi_desc *desc, u32 mask)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +090036{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020037 pci_msi_update_mask(desc, 0, mask);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040038}
39
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020040static inline void pci_msi_unmask(struct msi_desc *desc, u32 mask)
Christoph Hellwig5eb6d662016-07-12 18:20:14 +090041{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020042 pci_msi_update_mask(desc, mask, 0);
43}
Logan Gunthorped7cc6092019-05-23 16:30:51 -060044
Thomas Gleixner446a98b2021-07-29 23:51:58 +020045static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
46{
Thomas Gleixner173ffad2021-12-10 23:19:18 +010047 return desc->pci.mask_base + desc->msi_index * PCI_MSIX_ENTRY_SIZE;
Christoph Hellwig5eb6d662016-07-12 18:20:14 +090048}
49
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040050/*
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020051 * This internal function does not flush PCI writes to the device. All
52 * users must ensure that they read from the device before either assuming
53 * that the device state is up to date, or returning out of this file.
54 * It does not affect the msi_desc::msix_ctrl cache either. Use with care!
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040055 */
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020056static void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040057{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020058 void __iomem *desc_addr = pci_msix_desc_addr(desc);
Yijing Wang38737d82014-10-27 10:44:36 +080059
Thomas Gleixnere58f2252021-12-06 23:27:39 +010060 if (desc->pci.msi_attrib.can_mask)
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +010061 writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +090062}
63
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020064static inline void pci_msix_mask(struct msi_desc *desc)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +090065{
Thomas Gleixnere58f2252021-12-06 23:27:39 +010066 desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
67 pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020068 /* Flush write to device */
Thomas Gleixnere58f2252021-12-06 23:27:39 +010069 readl(desc->pci.mask_base);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040070}
71
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020072static inline void pci_msix_unmask(struct msi_desc *desc)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040073{
Thomas Gleixnere58f2252021-12-06 23:27:39 +010074 desc->pci.msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
75 pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl);
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020076}
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040077
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020078static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask)
79{
Thomas Gleixnere58f2252021-12-06 23:27:39 +010080 if (desc->pci.msi_attrib.is_msix)
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020081 pci_msix_mask(desc);
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +010082 else
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020083 pci_msi_mask(desc, mask);
84}
85
86static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask)
87{
Thomas Gleixnere58f2252021-12-06 23:27:39 +010088 if (desc->pci.msi_attrib.is_msix)
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020089 pci_msix_unmask(desc);
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +010090 else
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +020091 pci_msi_unmask(desc, mask);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040092}
93
Thomas Gleixner23ed8d52014-11-23 11:55:58 +010094/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -050095 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +010096 * @data: pointer to irqdata associated to that interrupt
97 */
98void pci_msi_mask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -040099{
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200100 struct msi_desc *desc = irq_data_get_msi_desc(data);
101
102 __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400103}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000104EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400105
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100106/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500107 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100108 * @data: pointer to irqdata associated to that interrupt
109 */
110void pci_msi_unmask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400111{
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200112 struct msi_desc *desc = irq_data_get_msi_desc(data);
113
114 __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000116EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jiang Liu891d4a42014-11-09 23:10:33 +0800118void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700119{
Jiang Liue39758e2015-07-09 16:00:43 +0800120 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
121
122 BUG_ON(dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700123
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100124 if (entry->pci.msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900125 void __iomem *base = pci_msix_desc_addr(entry);
Ben Hutchings30da5522010-07-23 14:56:28 +0100126
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100127 if (WARN_ON_ONCE(entry->pci.msi_attrib.is_virtual))
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600128 return;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600129
Ben Hutchings30da5522010-07-23 14:56:28 +0100130 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
131 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
132 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
133 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600134 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100135 u16 data;
136
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600137 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
138 &msg->address_lo);
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100139 if (entry->pci.msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600140 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
141 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600142 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100143 } else {
144 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600145 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100146 }
147 msg->data = data;
148 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700149}
150
Jiang Liu83a18912014-11-09 23:10:34 +0800151void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800152{
Jiang Liue39758e2015-07-09 16:00:43 +0800153 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
154
Keith Busch01705912017-03-29 22:49:11 -0500155 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100156 /* Don't touch the hardware now */
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100157 } else if (entry->pci.msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900158 void __iomem *base = pci_msix_desc_addr(entry);
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100159 u32 ctrl = entry->pci.msix_ctrl;
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200160 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400161
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100162 if (entry->pci.msi_attrib.is_virtual)
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600163 goto skip;
164
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200165 /*
166 * The specification mandates that the entry is masked
167 * when the message is modified:
168 *
169 * "If software changes the Address or Data value of an
170 * entry while the entry is unmasked, the result is
171 * undefined."
172 */
173 if (unmasked)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200174 pci_msix_write_vector_ctrl(entry, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200175
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900176 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
177 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
178 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200179
180 if (unmasked)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200181 pci_msix_write_vector_ctrl(entry, ctrl);
Thomas Gleixnerb9255a72021-07-29 23:51:43 +0200182
183 /* Ensure that the writes are visible in the device */
184 readl(base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400185 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600186 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400187 u16 msgctl;
188
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600189 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400190 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100191 msgctl |= entry->pci.msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600192 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700193
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600194 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
195 msg->address_lo);
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100196 if (entry->pci.msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600197 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
198 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600199 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
200 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700201 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600202 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
203 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700204 }
Thomas Gleixnerb9255a72021-07-29 23:51:43 +0200205 /* Ensure that the writes are visible in the device */
206 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700207 }
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600208
209skip:
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700210 entry->msg = *msg;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600211
212 if (entry->write_msi_msg)
213 entry->write_msi_msg(entry, entry->write_msi_msg_data);
214
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700215}
216
Jiang Liu83a18912014-11-09 23:10:34 +0800217void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800218{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200219 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800220
Jiang Liu83a18912014-11-09 23:10:34 +0800221 __pci_write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800222}
Jiang Liu83a18912014-11-09 23:10:34 +0800223EXPORT_SYMBOL_GPL(pci_write_msi_msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800224
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900225static void free_msi_irqs(struct pci_dev *dev)
226{
Jiang Liu8e047ad2014-11-15 22:24:07 +0800227 pci_msi_teardown_msi_irqs(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900228
Thomas Gleixner85aa6072021-12-06 23:27:54 +0100229 if (dev->msix_base) {
230 iounmap(dev->msix_base);
231 dev->msix_base = NULL;
232 }
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900233}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900234
David Millerba698ad2007-10-25 01:16:30 -0700235static void pci_intx_for_msi(struct pci_dev *dev, int enable)
236{
237 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
238 pci_intx(dev, enable);
239}
240
Bjorn Helgaas830dfe82020-12-03 12:51:09 -0600241static void pci_msi_set_enable(struct pci_dev *dev, int enable)
242{
243 u16 control;
244
245 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
246 control &= ~PCI_MSI_FLAGS_ENABLE;
247 if (enable)
248 control |= PCI_MSI_FLAGS_ENABLE;
249 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
250}
251
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100252/*
253 * Architecture override returns true when the PCI MSI message should be
254 * written by the generic restore function.
255 */
256bool __weak arch_restore_msi_irqs(struct pci_dev *dev)
257{
258 return true;
259}
260
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100261static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800262{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700263 struct msi_desc *entry;
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100264 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800265
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800266 if (!dev->msi_enabled)
267 return;
268
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200269 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800270
David Millerba698ad2007-10-25 01:16:30 -0700271 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500272 pci_msi_set_enable(dev, 0);
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100273 if (arch_restore_msi_irqs(dev))
274 __pci_write_msi_msg(entry, &entry->msg);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700275
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600276 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200277 pci_msi_update_mask(entry, 0, 0);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700278 control &= ~PCI_MSI_FLAGS_QSIZE;
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100279 control |= (entry->pci.msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600280 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100281}
282
Bjorn Helgaas830dfe82020-12-03 12:51:09 -0600283static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
284{
285 u16 ctrl;
286
287 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
288 ctrl &= ~clear;
289 ctrl |= set;
290 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
291}
292
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100293static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800294{
Shaohua Li41017f02006-02-08 17:11:38 +0800295 struct msi_desc *entry;
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100296 bool write_msg;
Shaohua Li41017f02006-02-08 17:11:38 +0800297
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700298 if (!dev->msix_enabled)
299 return;
300
Shaohua Li41017f02006-02-08 17:11:38 +0800301 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700302 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500303 pci_msix_clear_and_set_ctrl(dev, 0,
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800304 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
Shaohua Li41017f02006-02-08 17:11:38 +0800305
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100306 write_msg = arch_restore_msi_irqs(dev);
307
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100308 msi_lock_descs(&dev->dev);
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100309 msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100310 if (write_msg)
311 __pci_write_msi_msg(entry, &entry->msg);
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100312 pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl);
Thomas Gleixnerae72f312021-12-06 23:27:42 +0100313 }
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100314 msi_unlock_descs(&dev->dev);
Shaohua Li41017f02006-02-08 17:11:38 +0800315
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500316 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800317}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100318
319void pci_restore_msi_state(struct pci_dev *dev)
320{
321 __pci_restore_msi_state(dev);
322 __pci_restore_msix_state(dev);
323}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600324EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800325
Thomas Gleixner3f35d2c2021-12-15 18:16:44 +0100326static void pcim_msi_release(void *pcidev)
327{
328 struct pci_dev *dev = pcidev;
329
330 dev->is_msi_managed = false;
331 pci_free_irq_vectors(dev);
332}
333
334/*
335 * Needs to be separate from pcim_release to prevent an ordering problem
336 * vs. msi_device_data_release() in the MSI core code.
337 */
338static int pcim_setup_msi_release(struct pci_dev *dev)
339{
340 int ret;
341
342 if (!pci_is_managed(dev) || dev->is_msi_managed)
343 return 0;
344
345 ret = devm_add_action(&dev->dev, pcim_msi_release, dev);
346 if (!ret)
347 dev->is_msi_managed = true;
348 return ret;
349}
350
Thomas Gleixner93296cd2021-12-15 18:19:49 +0100351/*
352 * Ordering vs. devres: msi device data has to be installed first so that
353 * pcim_msi_release() is invoked before it on device release.
354 */
355static int pci_setup_msi_context(struct pci_dev *dev)
356{
357 int ret = msi_setup_device_data(&dev->dev);
358
359 if (!ret)
360 ret = pcim_setup_msi_release(dev);
361 return ret;
362}
363
Thomas Gleixner71020a32021-12-06 23:51:15 +0100364static int msi_setup_msi_desc(struct pci_dev *dev, int nvec,
365 struct irq_affinity_desc *masks)
Yijing Wangd873b4d2014-07-08 10:07:23 +0800366{
Thomas Gleixner71020a32021-12-06 23:51:15 +0100367 struct msi_desc desc;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200368 u16 control;
369
Yijing Wangd873b4d2014-07-08 10:07:23 +0800370 /* MSI Entry Initialization */
Thomas Gleixner71020a32021-12-06 23:51:15 +0100371 memset(&desc, 0, sizeof(desc));
Yijing Wangd873b4d2014-07-08 10:07:23 +0800372
373 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Marc Zyngier22266672021-11-04 18:01:29 +0000374 /* Lies, damned lies, and MSIs */
375 if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING)
376 control |= PCI_MSI_FLAGS_MASKBIT;
Thomas Gleixner71020a32021-12-06 23:51:15 +0100377 /* Respect XEN's mask disabling */
378 if (pci_msi_ignore_mask)
379 control &= ~PCI_MSI_FLAGS_MASKBIT;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800380
Thomas Gleixner71020a32021-12-06 23:51:15 +0100381 desc.nvec_used = nvec;
382 desc.pci.msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
383 desc.pci.msi_attrib.can_mask = !!(control & PCI_MSI_FLAGS_MASKBIT);
384 desc.pci.msi_attrib.default_irq = dev->irq;
385 desc.pci.msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
386 desc.pci.msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
387 desc.affinity = masks;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800388
389 if (control & PCI_MSI_FLAGS_64BIT)
Thomas Gleixner71020a32021-12-06 23:51:15 +0100390 desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800391 else
Thomas Gleixner71020a32021-12-06 23:51:15 +0100392 desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800393
394 /* Save the initial mask status */
Thomas Gleixner71020a32021-12-06 23:51:15 +0100395 if (desc.pci.msi_attrib.can_mask)
396 pci_read_config_dword(dev, desc.pci.mask_pos, &desc.pci.msi_mask);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800397
Thomas Gleixner71020a32021-12-06 23:51:15 +0100398 return msi_add_msi_desc(&dev->dev, &desc);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800399}
400
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000401static int msi_verify_entries(struct pci_dev *dev)
402{
403 struct msi_desc *entry;
404
Thomas Gleixnera6e8b942021-07-29 23:51:52 +0200405 if (!dev->no_64bit_msi)
406 return 0;
407
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100408 msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
Thomas Gleixnera6e8b942021-07-29 23:51:52 +0200409 if (entry->msg.address_hi) {
Vidya Sagar20532302020-12-03 12:51:10 -0600410 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
411 entry->msg.address_hi, entry->msg.address_lo);
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100412 break;
Vidya Sagar20532302020-12-03 12:51:10 -0600413 }
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000414 }
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100415 return !entry ? 0 : -EIO;
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * msi_capability_init - configure device's MSI capability structure
420 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400421 * @nvec: number of interrupts to allocate
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500422 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400424 * Setup the MSI capability structure of the device with the requested
425 * number of interrupts. A return value of zero indicates the successful
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500426 * setup of an entry with the new MSI IRQ. A negative return value indicates
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400427 * an error, and a positive return value indicates the number of interrupts
428 * which could have been allocated.
429 */
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800430static int msi_capability_init(struct pci_dev *dev, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100431 struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100433 struct irq_affinity_desc *masks = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000435 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Thomas Gleixnerc7ecb952021-12-10 23:18:44 +0100437 /*
438 * Disable MSI during setup in the hardware, but mark it enabled
439 * so that setup code can evaluate it.
440 */
441 pci_msi_set_enable(dev, 0);
442 dev->msi_enabled = 1;
Matthew Wilcox110828c2009-06-16 06:31:45 -0600443
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100444 if (affd)
445 masks = irq_create_affinity_masks(nvec, affd);
446
447 msi_lock_descs(&dev->dev);
Thomas Gleixner71020a32021-12-06 23:51:15 +0100448 ret = msi_setup_msi_desc(dev, nvec, masks);
449 if (ret)
Thomas Gleixnerc7ecb952021-12-10 23:18:44 +0100450 goto fail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700451
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500452 /* All MSIs are unmasked by default; mask them all */
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100453 entry = msi_first_desc(&dev->dev, MSI_DESC_ALL);
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200454 pci_msi_mask(entry, msi_multi_mask(entry));
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Configure MSI capability structure */
Jiang Liu8e047ad2014-11-15 22:24:07 +0800457 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200458 if (ret)
459 goto err;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700460
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000461 ret = msi_verify_entries(dev);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200462 if (ret)
463 goto err;
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000464
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500465 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700466 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500467 pci_msi_set_enable(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jiang Liu5f226992015-07-30 14:00:08 -0500469 pcibios_free_irq(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000470 dev->irq = entry->irq;
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100471 goto unlock;
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200472
473err:
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200474 pci_msi_unmask(entry, msi_multi_mask(entry));
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200475 free_msi_irqs(dev);
Thomas Gleixnerc7ecb952021-12-10 23:18:44 +0100476fail:
477 dev->msi_enabled = 0;
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100478unlock:
479 msi_unlock_descs(&dev->dev);
480 kfree(masks);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Krzysztof Wilczyńskifd1ae232021-10-13 01:41:36 +0000484static void __iomem *msix_map_region(struct pci_dev *dev,
485 unsigned int nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900486{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900487 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900488 u32 table_offset;
Yijing Wang6a878e52015-01-28 09:52:17 +0800489 unsigned long flags;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900490 u8 bir;
491
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600492 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
493 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600494 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
Yijing Wang6a878e52015-01-28 09:52:17 +0800495 flags = pci_resource_flags(dev, bir);
496 if (!flags || (flags & IORESOURCE_UNSET))
497 return NULL;
498
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600499 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900500 phys_addr = pci_resource_start(dev, bir) + table_offset;
501
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100502 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900503}
504
Thomas Gleixner71020a32021-12-06 23:51:15 +0100505static int msix_setup_msi_descs(struct pci_dev *dev, void __iomem *base,
506 struct msix_entry *entries, int nvec,
507 struct irq_affinity_desc *masks)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900508{
Thomas Gleixner71020a32021-12-06 23:51:15 +0100509 int ret = 0, i, vec_count = pci_msix_vec_count(dev);
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100510 struct irq_affinity_desc *curmsk;
Thomas Gleixner71020a32021-12-06 23:51:15 +0100511 struct msi_desc desc;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200512 void __iomem *addr;
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900513
Thomas Gleixner71020a32021-12-06 23:51:15 +0100514 memset(&desc, 0, sizeof(desc));
515
516 desc.nvec_used = 1;
517 desc.pci.msi_attrib.is_msix = 1;
518 desc.pci.msi_attrib.is_64 = 1;
519 desc.pci.msi_attrib.default_irq = dev->irq;
520 desc.pci.mask_base = base;
521
522 for (i = 0, curmsk = masks; i < nvec; i++, curmsk++) {
523 desc.msi_index = entries ? entries[i].entry : i;
524 desc.affinity = masks ? curmsk : NULL;
525 desc.pci.msi_attrib.is_virtual = desc.msi_index >= vec_count;
526 desc.pci.msi_attrib.can_mask = !pci_msi_ignore_mask &&
527 !desc.pci.msi_attrib.is_virtual;
528
529 if (!desc.pci.msi_attrib.can_mask) {
530 addr = pci_msix_desc_addr(&desc);
531 desc.pci.msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900532 }
533
Thomas Gleixner71020a32021-12-06 23:51:15 +0100534 ret = msi_add_msi_desc(&dev->dev, &desc);
535 if (ret)
536 break;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900537 }
Thomas Gleixner71020a32021-12-06 23:51:15 +0100538 return ret;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900539}
540
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200541static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900542{
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100543 struct msi_desc *desc;
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900544
Thomas Gleixner71121582021-12-06 23:27:46 +0100545 if (entries) {
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100546 msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL) {
547 entries->vector = desc->irq;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200548 entries++;
549 }
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900550 }
551}
552
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200553static void msix_mask_all(void __iomem *base, int tsize)
554{
555 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
556 int i;
557
Marek Marczykowski-Górecki1a519dc2021-08-26 19:03:42 +0200558 if (pci_msi_ignore_mask)
559 return;
560
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200561 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
562 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
563}
564
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100565static int msix_setup_interrupts(struct pci_dev *dev, void __iomem *base,
566 struct msix_entry *entries, int nvec,
567 struct irq_affinity *affd)
568{
569 struct irq_affinity_desc *masks = NULL;
570 int ret;
571
572 if (affd)
573 masks = irq_create_affinity_masks(nvec, affd);
574
575 msi_lock_descs(&dev->dev);
Thomas Gleixner71020a32021-12-06 23:51:15 +0100576 ret = msix_setup_msi_descs(dev, base, entries, nvec, masks);
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100577 if (ret)
578 goto out_free;
579
580 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
581 if (ret)
582 goto out_free;
583
584 /* Check if all MSI entries honor device restrictions */
585 ret = msi_verify_entries(dev);
586 if (ret)
587 goto out_free;
588
589 msix_update_entries(dev, entries);
590 goto out_unlock;
591
592out_free:
593 free_msi_irqs(dev);
594out_unlock:
595 msi_unlock_descs(&dev->dev);
596 kfree(masks);
597 return ret;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
601 * msix_capability_init - configure device's MSI-X capability
602 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700603 * @entries: pointer to an array of struct msix_entry entries
604 * @nvec: number of @entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500605 * @affd: Optional pointer to enable automatic affinity assignment
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600607 * Setup the MSI-X capability structure of device function with a
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500608 * single MSI-X IRQ. A return of zero indicates the successful setup of
609 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 **/
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200611static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
Ming Leic66d4bd2019-02-16 18:13:09 +0100612 int nvec, struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 void __iomem *base;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200615 int ret, tsize;
616 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Thomas Gleixner43855392021-07-29 23:51:40 +0200618 /*
619 * Some devices require MSI-X to be enabled before the MSI-X
620 * registers can be accessed. Mask all the vectors to prevent
621 * interrupts coming in before they're fully set up.
622 */
623 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
624 PCI_MSIX_FLAGS_ENABLE);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700625
Thomas Gleixnerc7ecb952021-12-10 23:18:44 +0100626 /* Mark it enabled so setup functions can query it */
627 dev->msix_enabled = 1;
628
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800629 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Request & Map MSI-X table region */
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200631 tsize = msix_table_size(control);
632 base = msix_map_region(dev, tsize);
Thomas Gleixner43855392021-07-29 23:51:40 +0200633 if (!base) {
634 ret = -ENOMEM;
635 goto out_disable;
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Thomas Gleixner85aa6072021-12-06 23:27:54 +0100638 dev->msix_base = base;
639
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100640 ret = msix_setup_interrupts(dev, base, entries, nvec, affd);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900641 if (ret)
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100642 goto out_disable;
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700643
Thomas Gleixnerc7ecb952021-12-10 23:18:44 +0100644 /* Disable INTX */
David Millerba698ad2007-10-25 01:16:30 -0700645 pci_intx_for_msi(dev, 0);
Stefan Roese83dbf892021-12-14 12:49:32 +0100646
647 /*
648 * Ensure that all table entries are masked to prevent
649 * stale entries from firing in a crash kernel.
650 *
651 * Done late to deal with a broken Marvell NVME device
652 * which takes the MSI-X mask bits into account even
653 * when MSI-X is disabled, which prevents MSI delivery.
654 */
655 msix_mask_all(base, tsize);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500656 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600657
Jiang Liu5f226992015-07-30 14:00:08 -0500658 pcibios_free_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900660
Thomas Gleixner43855392021-07-29 23:51:40 +0200661out_disable:
Thomas Gleixnerc7ecb952021-12-10 23:18:44 +0100662 dev->msix_enabled = 0;
Thomas Gleixner94185ad2021-12-14 12:42:14 +0100663 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
Thomas Gleixner43855392021-07-29 23:51:40 +0200664
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900665 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/**
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600669 * pci_msi_supported - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400670 * @dev: pointer to the pci_dev data structure of MSI device function
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500671 * @nvec: how many MSIs have been requested?
Brice Goglin24334a12006-08-31 01:55:07 -0400672 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700673 * Look at global flags, the device itself, and its parent buses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000674 * to determine if MSI/-X are supported for the device. If MSI/-X is
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600675 * supported return 1, else return 0.
Brice Goglin24334a12006-08-31 01:55:07 -0400676 **/
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600677static int pci_msi_supported(struct pci_dev *dev, int nvec)
Brice Goglin24334a12006-08-31 01:55:07 -0400678{
679 struct pci_bus *bus;
680
Brice Goglin0306ebf2006-10-05 10:24:31 +0200681 /* MSI must be globally enabled and supported by the device */
Alexander Gordeev27e20602014-09-23 14:25:11 -0600682 if (!pci_msi_enable)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600683 return 0;
Alexander Gordeev27e20602014-09-23 14:25:11 -0600684
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500685 if (!dev || dev->no_msi)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600686 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400687
Michael Ellerman314e77b2007-04-05 17:19:12 +1000688 /*
689 * You can't ask to have 0 or less MSIs configured.
690 * a) it's stupid ..
691 * b) the list manipulation code assumes nvec >= 1.
692 */
693 if (nvec < 1)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600694 return 0;
Michael Ellerman314e77b2007-04-05 17:19:12 +1000695
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900696 /*
697 * Any bridge which does NOT route MSI transactions from its
698 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200699 * the secondary pci_bus.
Marc Zyngier61af6922021-03-30 16:11:44 +0100700 *
701 * The NO_MSI flag can either be set directly by:
702 * - arch-specific PCI host bus controller drivers (deprecated)
703 * - quirks for specific PCI bridges
704 *
705 * or indirectly by platform-specific PCI host bridge drivers by
706 * advertising the 'msi_domain' property, which results in
707 * the NO_MSI flag when no MSI domain is found for this bridge
708 * at probe time.
Brice Goglin0306ebf2006-10-05 10:24:31 +0200709 */
Brice Goglin24334a12006-08-31 01:55:07 -0400710 for (bus = dev->bus; bus; bus = bus->parent)
711 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600712 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400713
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600714 return 1;
Brice Goglin24334a12006-08-31 01:55:07 -0400715}
716
717/**
Alexander Gordeevd1ac1d22013-12-30 08:28:13 +0100718 * pci_msi_vec_count - Return the number of MSI vectors a device can send
719 * @dev: device to report about
720 *
721 * This function returns the number of MSI vectors a device requested via
722 * Multiple Message Capable register. It returns a negative errno if the
723 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
724 * and returns a power of two, up to a maximum of 2^5 (32), according to the
725 * MSI specification.
726 **/
727int pci_msi_vec_count(struct pci_dev *dev)
728{
729 int ret;
730 u16 msgctl;
731
732 if (!dev->msi_cap)
733 return -EINVAL;
734
735 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
736 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
737
738 return ret;
739}
740EXPORT_SYMBOL(pci_msi_vec_count);
741
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600742static void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400744 struct msi_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100746 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700747 return;
748
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500749 pci_msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700750 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800751 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700752
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900753 /* Return the device with MSI unmasked as initial states */
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100754 desc = msi_first_desc(&dev->dev, MSI_DESC_ALL);
755 if (!WARN_ON_ONCE(!desc))
756 pci_msi_unmask(desc, msi_multi_mask(desc));
Michael Ellermane387b9e2007-03-22 21:51:27 +1100757
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500758 /* Restore dev->irq to its default pin-assertion IRQ */
Thomas Gleixnere58f2252021-12-06 23:27:39 +0100759 dev->irq = desc->pci.msi_attrib.default_irq;
Jiang Liu5f226992015-07-30 14:00:08 -0500760 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700761}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400762
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900763void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700764{
Yinghai Lud52877c2008-04-23 14:58:09 -0700765 if (!pci_msi_enable || !dev || !dev->msi_enabled)
766 return;
767
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100768 msi_lock_descs(&dev->dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700769 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900770 free_msi_irqs(dev);
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100771 msi_unlock_descs(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100773EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/**
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100776 * pci_msix_vec_count - return the number of device's MSI-X table entries
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100777 * @dev: pointer to the pci_dev data structure of MSI-X device function
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100778 * This function returns the number of device's MSI-X table entries and
779 * therefore the number of MSI-X vectors device is capable of sending.
780 * It returns a negative errno if the device is not capable of sending MSI-X
781 * interrupts.
782 **/
783int pci_msix_vec_count(struct pci_dev *dev)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100784{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100785 u16 control;
786
Gavin Shan520fe9d2013-04-04 16:54:33 +0000787 if (!dev->msix_cap)
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100788 return -EINVAL;
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100789
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600790 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600791 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100792}
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100793EXPORT_SYMBOL(pci_msix_vec_count);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100794
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200795static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600796 int nvec, struct irq_affinity *affd, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Bjorn Helgaas5ec09402014-09-23 14:38:28 -0600798 int nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700799 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500801 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600802 return -EINVAL;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000803
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100804 nr_entries = pci_msix_vec_count(dev);
805 if (nr_entries < 0)
806 return nr_entries;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600807 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300808 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900810 if (entries) {
811 /* Check for any invalid entries */
812 for (i = 0; i < nvec; i++) {
813 if (entries[i].entry >= nr_entries)
814 return -EINVAL; /* invalid entry */
815 for (j = i + 1; j < nvec; j++) {
816 if (entries[i].entry == entries[j].entry)
817 return -EINVAL; /* duplicate entry */
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700821
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500822 /* Check whether driver already requested for MSI IRQ */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900823 if (dev->msi_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600824 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
826 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800827 return msix_capability_init(dev, entries, nvec, affd);
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200828}
829
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600830static void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100831{
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100832 struct msi_desc *desc;
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900833
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100834 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700835 return;
836
Keith Busch01705912017-03-29 22:49:11 -0500837 if (pci_dev_is_disconnected(dev)) {
838 dev->msix_enabled = 0;
839 return;
840 }
841
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900842 /* Return the device with MSI-X masked as initial states */
Thomas Gleixnerae24e282021-12-06 23:51:18 +0100843 msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL)
844 pci_msix_mask(desc);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900845
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500846 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
David Millerba698ad2007-10-25 01:16:30 -0700847 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800848 dev->msix_enabled = 0;
Jiang Liu5f226992015-07-30 14:00:08 -0500849 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700850}
Hidetoshi Setoc9018512009-08-06 11:31:27 +0900851
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900852void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700853{
854 if (!pci_msi_enable || !dev || !dev->msix_enabled)
855 return;
856
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100857 msi_lock_descs(&dev->dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700858 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900859 free_msi_irqs(dev);
Thomas Gleixner5512c5e2021-12-06 23:51:13 +0100860 msi_unlock_descs(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100862EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900864static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100865 struct irq_affinity *affd)
Alexander Gordeev302a2522013-12-30 08:28:16 +0100866{
Alexander Gordeev034cd972014-04-14 15:28:35 +0200867 int nvec;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100868 int rc;
869
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500870 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600871 return -EINVAL;
Alexander Gordeev034cd972014-04-14 15:28:35 +0200872
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500873 /* Check whether driver already requested MSI-X IRQs */
Alexander Gordeev034cd972014-04-14 15:28:35 +0200874 if (dev->msix_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600875 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
Alexander Gordeev034cd972014-04-14 15:28:35 +0200876 return -EINVAL;
877 }
878
Alexander Gordeev302a2522013-12-30 08:28:16 +0100879 if (maxvec < minvec)
880 return -ERANGE;
881
Tonghao Zhang4c1ef722018-09-24 07:00:41 -0700882 if (WARN_ON_ONCE(dev->msi_enabled))
883 return -EINVAL;
884
Alexander Gordeev034cd972014-04-14 15:28:35 +0200885 nvec = pci_msi_vec_count(dev);
886 if (nvec < 0)
887 return nvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900888 if (nvec < minvec)
Dennis Chen948b7622016-12-01 10:15:04 +0800889 return -ENOSPC;
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900890
891 if (nvec > maxvec)
Alexander Gordeev034cd972014-04-14 15:28:35 +0200892 nvec = maxvec;
893
Thomas Gleixner93296cd2021-12-15 18:19:49 +0100894 rc = pci_setup_msi_context(dev);
Thomas Gleixner3f35d2c2021-12-15 18:16:44 +0100895 if (rc)
896 return rc;
897
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900898 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800899 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700900 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900901 if (nvec < minvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +0100902 return -ENOSPC;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100903 }
Alexander Gordeev302a2522013-12-30 08:28:16 +0100904
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800905 rc = msi_capability_init(dev, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900906 if (rc == 0)
907 return nvec;
908
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900909 if (rc < 0)
910 return rc;
911 if (rc < minvec)
912 return -ENOSPC;
913
914 nvec = rc;
915 }
916}
917
Christoph Hellwig4fe03952017-01-09 21:37:40 +0100918/* deprecated, don't use */
919int pci_enable_msi(struct pci_dev *dev)
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900920{
Christoph Hellwig4fe03952017-01-09 21:37:40 +0100921 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
922 if (rc < 0)
923 return rc;
924 return 0;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100925}
Christoph Hellwig4fe03952017-01-09 21:37:40 +0100926EXPORT_SYMBOL(pci_enable_msi);
Alexander Gordeev302a2522013-12-30 08:28:16 +0100927
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900928static int __pci_enable_msix_range(struct pci_dev *dev,
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800929 struct msix_entry *entries, int minvec,
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600930 int maxvec, struct irq_affinity *affd,
931 int flags)
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900932{
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200933 int rc, nvec = maxvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900934
935 if (maxvec < minvec)
936 return -ERANGE;
937
Tonghao Zhang4c1ef722018-09-24 07:00:41 -0700938 if (WARN_ON_ONCE(dev->msix_enabled))
939 return -EINVAL;
940
Thomas Gleixner93296cd2021-12-15 18:19:49 +0100941 rc = pci_setup_msi_context(dev);
Thomas Gleixner3f35d2c2021-12-15 18:16:44 +0100942 if (rc)
943 return rc;
944
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900945 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800946 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -0700947 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900948 if (nvec < minvec)
949 return -ENOSPC;
950 }
951
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600952 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900953 if (rc == 0)
954 return nvec;
955
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900956 if (rc < 0)
957 return rc;
958 if (rc < minvec)
959 return -ENOSPC;
960
961 nvec = rc;
962 }
963}
964
Alexander Gordeev302a2522013-12-30 08:28:16 +0100965/**
966 * pci_enable_msix_range - configure device's MSI-X capability structure
967 * @dev: pointer to the pci_dev data structure of MSI-X device function
968 * @entries: pointer to an array of MSI-X entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500969 * @minvec: minimum number of MSI-X IRQs requested
970 * @maxvec: maximum number of MSI-X IRQs requested
Alexander Gordeev302a2522013-12-30 08:28:16 +0100971 *
972 * Setup the MSI-X capability structure of device function with a maximum
973 * possible number of interrupts in the range between @minvec and @maxvec
974 * upon its software driver call to request for MSI-X mode enabled on its
975 * hardware device function. It returns a negative errno if an error occurs.
976 * If it succeeds, it returns the actual number of interrupts allocated and
977 * indicates the successful configuration of MSI-X capability structure
978 * with new allocated MSI-X interrupts.
979 **/
980int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900981 int minvec, int maxvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +0100982{
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600983 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
Alexander Gordeev302a2522013-12-30 08:28:16 +0100984}
985EXPORT_SYMBOL(pci_enable_msix_range);
Jiang Liu3878eae2014-11-11 21:02:18 +0800986
Christoph Hellwigaff17162016-07-12 18:20:17 +0900987/**
Christoph Hellwig402723a2016-11-08 17:15:05 -0800988 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
Christoph Hellwigaff17162016-07-12 18:20:17 +0900989 * @dev: PCI device to operate on
990 * @min_vecs: minimum number of vectors required (must be >= 1)
991 * @max_vecs: maximum (desired) number of vectors
992 * @flags: flags or quirks for the allocation
Christoph Hellwig402723a2016-11-08 17:15:05 -0800993 * @affd: optional description of the affinity requirements
Christoph Hellwigaff17162016-07-12 18:20:17 +0900994 *
995 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
996 * vectors if available, and fall back to a single legacy vector
997 * if neither is available. Return the number of vectors allocated,
998 * (which might be smaller than @max_vecs) if successful, or a negative
999 * error code on error. If less than @min_vecs interrupt vectors are
1000 * available for @dev the function will fail with -ENOSPC.
1001 *
1002 * To get the Linux IRQ number used for a vector that can be passed to
1003 * request_irq() use the pci_irq_vector() helper.
1004 */
Christoph Hellwig402723a2016-11-08 17:15:05 -08001005int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1006 unsigned int max_vecs, unsigned int flags,
Ming Leic66d4bd2019-02-16 18:13:09 +01001007 struct irq_affinity *affd)
Christoph Hellwigaff17162016-07-12 18:20:17 +09001008{
Ming Leic66d4bd2019-02-16 18:13:09 +01001009 struct irq_affinity msi_default_affd = {0};
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001010 int nvecs = -ENOSPC;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001011
Christoph Hellwig402723a2016-11-08 17:15:05 -08001012 if (flags & PCI_IRQ_AFFINITY) {
1013 if (!affd)
1014 affd = &msi_default_affd;
1015 } else {
1016 if (WARN_ON(affd))
1017 affd = NULL;
1018 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001019
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001020 if (flags & PCI_IRQ_MSIX) {
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001021 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1022 affd, flags);
1023 if (nvecs > 0)
1024 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001025 }
1026
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001027 if (flags & PCI_IRQ_MSI) {
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001028 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1029 if (nvecs > 0)
1030 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001031 }
1032
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001033 /* use legacy IRQ if allowed */
Christoph Hellwig862290f2017-02-01 14:41:42 +01001034 if (flags & PCI_IRQ_LEGACY) {
1035 if (min_vecs == 1 && dev->irq) {
Ming Leic66d4bd2019-02-16 18:13:09 +01001036 /*
1037 * Invoke the affinity spreading logic to ensure that
1038 * the device driver can adjust queue configuration
1039 * for the single interrupt case.
1040 */
1041 if (affd)
1042 irq_create_affinity_masks(1, affd);
Christoph Hellwig862290f2017-02-01 14:41:42 +01001043 pci_intx(dev, 1);
1044 return 1;
1045 }
Christoph Hellwig5d0bdf22016-08-11 07:11:05 -07001046 }
1047
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001048 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001049}
Christoph Hellwig402723a2016-11-08 17:15:05 -08001050EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
Christoph Hellwigaff17162016-07-12 18:20:17 +09001051
1052/**
1053 * pci_free_irq_vectors - free previously allocated IRQs for a device
1054 * @dev: PCI device to operate on
1055 *
1056 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1057 */
1058void pci_free_irq_vectors(struct pci_dev *dev)
1059{
1060 pci_disable_msix(dev);
1061 pci_disable_msi(dev);
1062}
1063EXPORT_SYMBOL(pci_free_irq_vectors);
1064
1065/**
1066 * pci_irq_vector - return Linux IRQ number of a device vector
Thomas Gleixner29bbc352021-12-06 23:27:26 +01001067 * @dev: PCI device to operate on
1068 * @nr: Interrupt vector index (0-based)
1069 *
1070 * @nr has the following meanings depending on the interrupt mode:
1071 * MSI-X: The index in the MSI-X vector table
1072 * MSI: The index of the enabled MSI vectors
1073 * INTx: Must be 0
1074 *
1075 * Return: The Linux interrupt number or -EINVAl if @nr is out of range.
Christoph Hellwigaff17162016-07-12 18:20:17 +09001076 */
1077int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1078{
Thomas Gleixner82ff8e62021-12-10 23:19:25 +01001079 unsigned int irq;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001080
Thomas Gleixner82ff8e62021-12-10 23:19:25 +01001081 if (!dev->msi_enabled && !dev->msix_enabled)
1082 return !nr ? dev->irq : -EINVAL;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001083
Thomas Gleixner82ff8e62021-12-10 23:19:25 +01001084 irq = msi_get_virq(&dev->dev, nr);
1085 return irq ? irq : -EINVAL;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001086}
1087EXPORT_SYMBOL(pci_irq_vector);
1088
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001089/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001090 * pci_irq_get_affinity - return the affinity of a particular MSI vector
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001091 * @dev: PCI device to operate on
1092 * @nr: device-relative interrupt vector index (0-based).
Thomas Gleixner29bbc352021-12-06 23:27:26 +01001093 *
1094 * @nr has the following meanings depending on the interrupt mode:
1095 * MSI-X: The index in the MSI-X vector table
1096 * MSI: The index of the enabled MSI vectors
1097 * INTx: Must be 0
1098 *
1099 * Return: A cpumask pointer or NULL if @nr is out of range
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001100 */
1101const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1102{
Thomas Gleixnerd5582852021-12-18 11:25:14 +01001103 int idx, irq = pci_irq_vector(dev, nr);
Thomas Gleixnerf4823592021-12-10 23:19:26 +01001104 struct msi_desc *desc;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001105
Thomas Gleixnerf4823592021-12-10 23:19:26 +01001106 if (WARN_ON_ONCE(irq <= 0))
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001107 return NULL;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001108
Thomas Gleixnerf4823592021-12-10 23:19:26 +01001109 desc = irq_get_msi_desc(irq);
1110 /* Non-MSI does not have the information handy */
1111 if (!desc)
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001112 return cpu_possible_mask;
Thomas Gleixnerf4823592021-12-10 23:19:26 +01001113
1114 if (WARN_ON_ONCE(!desc->affinity))
1115 return NULL;
Thomas Gleixnerd5582852021-12-18 11:25:14 +01001116
1117 /*
1118 * MSI has a mask array in the descriptor.
1119 * MSI-X has a single mask.
1120 */
1121 idx = dev->msi_enabled ? nr : 0;
1122 return &desc->affinity[idx].mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001123}
1124EXPORT_SYMBOL(pci_irq_get_affinity);
1125
Jiang Liu25a98bd2015-07-09 16:00:45 +08001126struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1127{
1128 return to_pci_dev(desc->dev);
1129}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001130EXPORT_SYMBOL(msi_desc_to_pci_dev);
Jiang Liu25a98bd2015-07-09 16:00:45 +08001131
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01001132void pci_no_msi(void)
Jiang Liu3878eae2014-11-11 21:02:18 +08001133{
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01001134 pci_msi_enable = 0;
Jiang Liu3878eae2014-11-11 21:02:18 +08001135}
1136
1137/**
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01001138 * pci_msi_enabled - is MSI enabled?
Jiang Liu3878eae2014-11-11 21:02:18 +08001139 *
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01001140 * Returns true if MSI has not been disabled by the command-line option
1141 * pci=nomsi.
1142 **/
1143int pci_msi_enabled(void)
Jiang Liu3878eae2014-11-11 21:02:18 +08001144{
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01001145 return pci_msi_enable;
Jiang Liu3878eae2014-11-11 21:02:18 +08001146}
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01001147EXPORT_SYMBOL(pci_msi_enabled);