blob: bdc6ba7f39f06a2f17226447d8ee5736629576d6 [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 */
9
Eric W. Biederman1ce03372006-10-04 02:16:41 -070010#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/irq.h>
13#include <linux/interrupt.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pci.h>
17#include <linux/proc_fs.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070018#include <linux/msi.h>
Dan Williams4fdadeb2007-04-26 18:21:38 -070019#include <linux/smp.h>
Hidetoshi Seto500559a2009-08-10 10:14:15 +090020#include <linux/errno.h>
21#include <linux/io.h>
Tomasz Nowickibe2021b2016-09-12 20:32:22 +020022#include <linux/acpi_iort.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Jiang Liu3878eae2014-11-11 21:02:18 +080024#include <linux/irqdomain.h>
David Daneyb6eec9b2015-10-08 15:10:49 -070025#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Bjorn Helgaascbc40d52020-12-03 12:51:08 -060029#ifdef CONFIG_PCI_MSI
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int pci_msi_enable = 1;
Yijing Wang38737d82014-10-27 10:44:36 +080032int pci_msi_ignore_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Bjorn Helgaas527eee22013-04-17 17:44:48 -060034#define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
35
Jiang Liu8e047ad2014-11-15 22:24:07 +080036#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
Jiang Liu8e047ad2014-11-15 22:24:07 +080037static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
38{
39 struct irq_domain *domain;
40
Christoph Hellwig47feb412017-02-08 18:17:43 +010041 domain = dev_get_msi_domain(&dev->dev);
Marc Zyngier3845d292015-12-04 10:28:14 -060042 if (domain && irq_domain_is_hierarchy(domain))
Christoph Hellwig699c4ce2017-02-08 18:17:44 +010043 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
Jiang Liu8e047ad2014-11-15 22:24:07 +080044
45 return arch_setup_msi_irqs(dev, nvec, type);
46}
47
48static void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
49{
50 struct irq_domain *domain;
51
Christoph Hellwig47feb412017-02-08 18:17:43 +010052 domain = dev_get_msi_domain(&dev->dev);
Marc Zyngier3845d292015-12-04 10:28:14 -060053 if (domain && irq_domain_is_hierarchy(domain))
Christoph Hellwig699c4ce2017-02-08 18:17:44 +010054 msi_domain_free_irqs(domain, &dev->dev);
Jiang Liu8e047ad2014-11-15 22:24:07 +080055 else
56 arch_teardown_msi_irqs(dev);
57}
58#else
59#define pci_msi_setup_msi_irqs arch_setup_msi_irqs
60#define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
61#endif
Bjorn Helgaas527eee22013-04-17 17:44:48 -060062
Thomas Gleixner077ee782020-08-26 13:17:02 +020063#ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010064/* Arch hooks */
Thomas Petazzoni4287d822013-08-09 22:27:06 +020065int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
66{
Marc Zyngier3a05d082021-03-30 16:11:38 +010067 return -EINVAL;
Thomas Petazzoni4287d822013-08-09 22:27:06 +020068}
69
70void __weak arch_teardown_msi_irq(unsigned int irq)
71{
72}
73
Thomas Petazzoni4287d822013-08-09 22:27:06 +020074int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010075{
76 struct msi_desc *entry;
77 int ret;
78
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040079 /*
80 * If an architecture wants to support multiple MSI, it needs to
81 * override arch_setup_msi_irqs()
82 */
83 if (type == PCI_CAP_ID_MSI && nvec > 1)
84 return 1;
85
Jiang Liu5004e982015-07-09 16:00:41 +080086 for_each_pci_msi_entry(entry, dev) {
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010087 ret = arch_setup_msi_irq(dev, entry);
Michael Ellermanb5fbf532009-02-11 22:27:02 +110088 if (ret < 0)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010089 return ret;
Michael Ellermanb5fbf532009-02-11 22:27:02 +110090 if (ret > 0)
91 return -ENOSPC;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010092 }
93
94 return 0;
95}
96
Marc Zyngierf8bcf242021-03-30 16:11:40 +010097void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010098{
Jiang Liu63a7b172014-11-06 22:20:32 +080099 int i;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100100 struct msi_desc *entry;
101
Jiang Liu5004e982015-07-09 16:00:41 +0800102 for_each_pci_msi_entry(entry, dev)
Jiang Liu63a7b172014-11-06 22:20:32 +0800103 if (entry->irq)
104 for (i = 0; i < entry->nvec_used; i++)
105 arch_teardown_msi_irq(entry->irq + i);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100106}
Thomas Gleixner077ee782020-08-26 13:17:02 +0200107#endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500108
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800109static void default_restore_msi_irq(struct pci_dev *dev, int irq)
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500110{
111 struct msi_desc *entry;
112
113 entry = NULL;
114 if (dev->msix_enabled) {
Jiang Liu5004e982015-07-09 16:00:41 +0800115 for_each_pci_msi_entry(entry, dev) {
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500116 if (irq == entry->irq)
117 break;
118 }
119 } else if (dev->msi_enabled) {
120 entry = irq_get_msi_desc(irq);
121 }
122
123 if (entry)
Jiang Liu83a18912014-11-09 23:10:34 +0800124 __pci_write_msi_msg(entry, &entry->msg);
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500125}
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200126
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800127void __weak arch_restore_msi_irqs(struct pci_dev *dev)
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200128{
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800129 return default_restore_msi_irqs(dev);
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200130}
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500131
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600132/*
133 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
134 * mask all MSI interrupts by clearing the MSI enable bit does not work
135 * reliably as devices without an INTx disable bit will then generate a
136 * level IRQ which will never be cleared.
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600137 */
Thomas Gleixner7327cef2021-07-29 23:51:56 +0200138static inline __attribute_const__ u32 msi_multi_mask(struct msi_desc *desc)
139{
140 /* Don't shift by >= width of type */
141 if (desc->msi_attrib.multi_cap >= 5)
142 return 0xffffffff;
143 return (1 << (1 << desc->msi_attrib.multi_cap)) - 1;
144}
145
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200146static noinline void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Thomas Gleixner77e89af2021-07-29 23:51:47 +0200148 raw_spinlock_t *lock = &desc->dev->msi_lock;
149 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Thomas Gleixner77e89af2021-07-29 23:51:47 +0200151 raw_spin_lock_irqsave(lock, flags);
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200152 desc->msi_mask &= ~clear;
153 desc->msi_mask |= set;
Jiang Liue39758e2015-07-09 16:00:43 +0800154 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200155 desc->msi_mask);
Thomas Gleixner77e89af2021-07-29 23:51:47 +0200156 raw_spin_unlock_irqrestore(lock, flags);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900157}
158
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200159static inline void pci_msi_mask(struct msi_desc *desc, u32 mask)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900160{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200161 pci_msi_update_mask(desc, 0, mask);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400162}
163
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200164static inline void pci_msi_unmask(struct msi_desc *desc, u32 mask)
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900165{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200166 pci_msi_update_mask(desc, mask, 0);
167}
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600168
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200169static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
170{
171 return desc->mask_base + desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900172}
173
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400174/*
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200175 * This internal function does not flush PCI writes to the device. All
176 * users must ensure that they read from the device before either assuming
177 * that the device state is up to date, or returning out of this file.
178 * It does not affect the msi_desc::msix_ctrl cache either. Use with care!
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400179 */
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200180static void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400181{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200182 void __iomem *desc_addr = pci_msix_desc_addr(desc);
Yijing Wang38737d82014-10-27 10:44:36 +0800183
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200184 writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900185}
186
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200187static inline void pci_msix_mask(struct msi_desc *desc)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900188{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200189 desc->msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
190 pci_msix_write_vector_ctrl(desc, desc->msix_ctrl);
191 /* Flush write to device */
192 readl(desc->mask_base);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400193}
194
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200195static inline void pci_msix_unmask(struct msi_desc *desc)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400196{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200197 desc->msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
198 pci_msix_write_vector_ctrl(desc, desc->msix_ctrl);
199}
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400200
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200201static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask)
202{
203 if (pci_msi_ignore_mask || desc->msi_attrib.is_virtual)
204 return;
205
206 if (desc->msi_attrib.is_msix)
207 pci_msix_mask(desc);
208 else if (desc->msi_attrib.maskbit)
209 pci_msi_mask(desc, mask);
210}
211
212static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask)
213{
214 if (pci_msi_ignore_mask || desc->msi_attrib.is_virtual)
215 return;
216
217 if (desc->msi_attrib.is_msix)
218 pci_msix_unmask(desc);
219 else if (desc->msi_attrib.maskbit)
220 pci_msi_unmask(desc, mask);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400221}
222
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100223/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500224 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100225 * @data: pointer to irqdata associated to that interrupt
226 */
227void pci_msi_mask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400228{
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200229 struct msi_desc *desc = irq_data_get_msi_desc(data);
230
231 __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400232}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000233EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400234
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100235/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500236 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100237 * @data: pointer to irqdata associated to that interrupt
238 */
239void pci_msi_unmask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400240{
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200241 struct msi_desc *desc = irq_data_get_msi_desc(data);
242
243 __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000245EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800247void default_restore_msi_irqs(struct pci_dev *dev)
248{
249 struct msi_desc *entry;
250
Jiang Liu5004e982015-07-09 16:00:41 +0800251 for_each_pci_msi_entry(entry, dev)
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800252 default_restore_msi_irq(dev, entry->irq);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800253}
254
Jiang Liu891d4a42014-11-09 23:10:33 +0800255void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700256{
Jiang Liue39758e2015-07-09 16:00:43 +0800257 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
258
259 BUG_ON(dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700260
Ben Hutchings30da5522010-07-23 14:56:28 +0100261 if (entry->msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900262 void __iomem *base = pci_msix_desc_addr(entry);
Ben Hutchings30da5522010-07-23 14:56:28 +0100263
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200264 if (WARN_ON_ONCE(entry->msi_attrib.is_virtual))
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600265 return;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600266
Ben Hutchings30da5522010-07-23 14:56:28 +0100267 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
268 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
269 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
270 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600271 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100272 u16 data;
273
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600274 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
275 &msg->address_lo);
Ben Hutchings30da5522010-07-23 14:56:28 +0100276 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600277 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
278 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600279 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100280 } else {
281 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600282 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100283 }
284 msg->data = data;
285 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700286}
287
Jiang Liu83a18912014-11-09 23:10:34 +0800288void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800289{
Jiang Liue39758e2015-07-09 16:00:43 +0800290 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
291
Keith Busch01705912017-03-29 22:49:11 -0500292 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100293 /* Don't touch the hardware now */
294 } else if (entry->msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900295 void __iomem *base = pci_msix_desc_addr(entry);
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200296 u32 ctrl = entry->msix_ctrl;
297 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400298
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200299 if (entry->msi_attrib.is_virtual)
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600300 goto skip;
301
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200302 /*
303 * The specification mandates that the entry is masked
304 * when the message is modified:
305 *
306 * "If software changes the Address or Data value of an
307 * entry while the entry is unmasked, the result is
308 * undefined."
309 */
310 if (unmasked)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200311 pci_msix_write_vector_ctrl(entry, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200312
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900313 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
314 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
315 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200316
317 if (unmasked)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200318 pci_msix_write_vector_ctrl(entry, ctrl);
Thomas Gleixnerb9255a72021-07-29 23:51:43 +0200319
320 /* Ensure that the writes are visible in the device */
321 readl(base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400322 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600323 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400324 u16 msgctl;
325
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600326 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400327 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
328 msgctl |= entry->msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600329 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700330
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600331 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
332 msg->address_lo);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700333 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600334 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
335 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600336 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
337 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700338 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600339 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
340 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700341 }
Thomas Gleixnerb9255a72021-07-29 23:51:43 +0200342 /* Ensure that the writes are visible in the device */
343 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700344 }
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600345
346skip:
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700347 entry->msg = *msg;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600348
349 if (entry->write_msi_msg)
350 entry->write_msi_msg(entry, entry->write_msi_msg_data);
351
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700352}
353
Jiang Liu83a18912014-11-09 23:10:34 +0800354void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800355{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200356 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800357
Jiang Liu83a18912014-11-09 23:10:34 +0800358 __pci_write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800359}
Jiang Liu83a18912014-11-09 23:10:34 +0800360EXPORT_SYMBOL_GPL(pci_write_msi_msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800361
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900362static void free_msi_irqs(struct pci_dev *dev)
363{
Jiang Liu5004e982015-07-09 16:00:41 +0800364 struct list_head *msi_list = dev_to_msi_list(&dev->dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900365 struct msi_desc *entry, *tmp;
Barry Song2f170812021-08-13 15:56:27 +1200366 int i;
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900367
Jiang Liu5004e982015-07-09 16:00:41 +0800368 for_each_pci_msi_entry(entry, dev)
Jiang Liu63a7b172014-11-06 22:20:32 +0800369 if (entry->irq)
370 for (i = 0; i < entry->nvec_used; i++)
371 BUG_ON(irq_has_action(entry->irq + i));
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900372
Jiang Liu8e047ad2014-11-15 22:24:07 +0800373 pci_msi_teardown_msi_irqs(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900374
Jiang Liu5004e982015-07-09 16:00:41 +0800375 list_for_each_entry_safe(entry, tmp, msi_list, list) {
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900376 if (entry->msi_attrib.is_msix) {
Jiang Liu5004e982015-07-09 16:00:41 +0800377 if (list_is_last(&entry->list, msi_list))
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900378 iounmap(entry->mask_base);
379 }
Neil Horman424eb392012-01-03 10:29:54 -0500380
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900381 list_del(&entry->list);
Prarit Bhargava81efbad2017-02-15 11:53:08 -0500382 free_msi_entry(entry);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900383 }
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800384
385 if (dev->msi_irq_groups) {
Barry Song2f170812021-08-13 15:56:27 +1200386 msi_destroy_sysfs(&dev->dev, dev->msi_irq_groups);
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800387 dev->msi_irq_groups = NULL;
388 }
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900389}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900390
David Millerba698ad2007-10-25 01:16:30 -0700391static void pci_intx_for_msi(struct pci_dev *dev, int enable)
392{
393 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
394 pci_intx(dev, enable);
395}
396
Bjorn Helgaas830dfe82020-12-03 12:51:09 -0600397static void pci_msi_set_enable(struct pci_dev *dev, int enable)
398{
399 u16 control;
400
401 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
402 control &= ~PCI_MSI_FLAGS_ENABLE;
403 if (enable)
404 control |= PCI_MSI_FLAGS_ENABLE;
405 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
406}
407
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100408static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800409{
Shaohua Li41017f02006-02-08 17:11:38 +0800410 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700411 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800412
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800413 if (!dev->msi_enabled)
414 return;
415
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200416 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800417
David Millerba698ad2007-10-25 01:16:30 -0700418 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500419 pci_msi_set_enable(dev, 0);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800420 arch_restore_msi_irqs(dev);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700421
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600422 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200423 pci_msi_update_mask(entry, 0, 0);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700424 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400425 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600426 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100427}
428
Bjorn Helgaas830dfe82020-12-03 12:51:09 -0600429static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
430{
431 u16 ctrl;
432
433 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
434 ctrl &= ~clear;
435 ctrl |= set;
436 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
437}
438
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100439static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800440{
Shaohua Li41017f02006-02-08 17:11:38 +0800441 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800442
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700443 if (!dev->msix_enabled)
444 return;
Jiang Liu5004e982015-07-09 16:00:41 +0800445 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700446
Shaohua Li41017f02006-02-08 17:11:38 +0800447 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700448 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500449 pci_msix_clear_and_set_ctrl(dev, 0,
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800450 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
Shaohua Li41017f02006-02-08 17:11:38 +0800451
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800452 arch_restore_msi_irqs(dev);
Jiang Liu5004e982015-07-09 16:00:41 +0800453 for_each_pci_msi_entry(entry, dev)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200454 pci_msix_write_vector_ctrl(entry, entry->msix_ctrl);
Shaohua Li41017f02006-02-08 17:11:38 +0800455
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500456 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800457}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100458
459void pci_restore_msi_state(struct pci_dev *dev)
460{
461 __pci_restore_msi_state(dev);
462 __pci_restore_msix_state(dev);
463}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600464EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800465
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200466static struct msi_desc *
Ming Leic66d4bd2019-02-16 18:13:09 +0100467msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
Yijing Wangd873b4d2014-07-08 10:07:23 +0800468{
Dou Liyangbec04032018-12-04 23:51:20 +0800469 struct irq_affinity_desc *masks = NULL;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800470 struct msi_desc *entry;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200471 u16 control;
472
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500473 if (affd)
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800474 masks = irq_create_affinity_masks(nvec, affd);
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500475
Yijing Wangd873b4d2014-07-08 10:07:23 +0800476 /* MSI Entry Initialization */
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200477 entry = alloc_msi_entry(&dev->dev, nvec, masks);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800478 if (!entry)
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200479 goto out;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800480
481 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
482
483 entry->msi_attrib.is_msix = 0;
484 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600485 entry->msi_attrib.is_virtual = 0;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800486 entry->msi_attrib.entry_nr = 0;
487 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
488 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Yijing Wangd873b4d2014-07-08 10:07:23 +0800489 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
Jiang Liu63a7b172014-11-06 22:20:32 +0800490 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
Yijing Wangd873b4d2014-07-08 10:07:23 +0800491
492 if (control & PCI_MSI_FLAGS_64BIT)
493 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
494 else
495 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
496
497 /* Save the initial mask status */
498 if (entry->msi_attrib.maskbit)
Thomas Gleixner67961e72021-07-29 23:51:53 +0200499 pci_read_config_dword(dev, entry->mask_pos, &entry->msi_mask);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800500
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200501out:
502 kfree(masks);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800503 return entry;
504}
505
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000506static int msi_verify_entries(struct pci_dev *dev)
507{
508 struct msi_desc *entry;
509
Thomas Gleixnera6e8b942021-07-29 23:51:52 +0200510 if (!dev->no_64bit_msi)
511 return 0;
512
Jiang Liu5004e982015-07-09 16:00:41 +0800513 for_each_pci_msi_entry(entry, dev) {
Thomas Gleixnera6e8b942021-07-29 23:51:52 +0200514 if (entry->msg.address_hi) {
Vidya Sagar20532302020-12-03 12:51:10 -0600515 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
516 entry->msg.address_hi, entry->msg.address_lo);
517 return -EIO;
518 }
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000519 }
520 return 0;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/**
524 * msi_capability_init - configure device's MSI capability structure
525 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400526 * @nvec: number of interrupts to allocate
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500527 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400529 * Setup the MSI capability structure of the device with the requested
530 * number of interrupts. A return value of zero indicates the successful
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500531 * setup of an entry with the new MSI IRQ. A negative return value indicates
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400532 * an error, and a positive return value indicates the number of interrupts
533 * which could have been allocated.
534 */
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800535static int msi_capability_init(struct pci_dev *dev, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100536 struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500541 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600542
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800543 entry = msi_setup_entry(dev, nvec, affd);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700544 if (!entry)
545 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700546
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500547 /* All MSIs are unmasked by default; mask them all */
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200548 pci_msi_mask(entry, msi_multi_mask(entry));
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400549
Jiang Liu5004e982015-07-09 16:00:41 +0800550 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
Michael Ellerman9c831332007-04-18 19:39:21 +1000551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* Configure MSI capability structure */
Jiang Liu8e047ad2014-11-15 22:24:07 +0800553 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200554 if (ret)
555 goto err;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700556
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000557 ret = msi_verify_entries(dev);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200558 if (ret)
559 goto err;
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000560
Barry Song2f170812021-08-13 15:56:27 +1200561 dev->msi_irq_groups = msi_populate_sysfs(&dev->dev);
562 if (IS_ERR(dev->msi_irq_groups)) {
563 ret = PTR_ERR(dev->msi_irq_groups);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200564 goto err;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400565 }
566
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500567 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700568 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500569 pci_msi_set_enable(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800570 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jiang Liu5f226992015-07-30 14:00:08 -0500572 pcibios_free_irq(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000573 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200575
576err:
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200577 pci_msi_unmask(entry, msi_multi_mask(entry));
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200578 free_msi_irqs(dev);
579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Krzysztof Wilczyńskifd1ae232021-10-13 01:41:36 +0000582static void __iomem *msix_map_region(struct pci_dev *dev,
583 unsigned int nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900584{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900585 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900586 u32 table_offset;
Yijing Wang6a878e52015-01-28 09:52:17 +0800587 unsigned long flags;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900588 u8 bir;
589
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600590 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
591 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600592 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
Yijing Wang6a878e52015-01-28 09:52:17 +0800593 flags = pci_resource_flags(dev, bir);
594 if (!flags || (flags & IORESOURCE_UNSET))
595 return NULL;
596
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600597 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900598 phys_addr = pci_resource_start(dev, bir) + table_offset;
599
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100600 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900601}
602
Gavin Shan520fe9d2013-04-04 16:54:33 +0000603static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200604 struct msix_entry *entries, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100605 struct irq_affinity *affd)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900606{
Dou Liyangbec04032018-12-04 23:51:20 +0800607 struct irq_affinity_desc *curmsk, *masks = NULL;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900608 struct msi_desc *entry;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200609 void __iomem *addr;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200610 int ret, i;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600611 int vec_count = pci_msix_vec_count(dev);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900612
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500613 if (affd)
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800614 masks = irq_create_affinity_masks(nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900615
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200616 for (i = 0, curmsk = masks; i < nvec; i++) {
617 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900618 if (!entry) {
619 if (!i)
620 iounmap(base);
621 else
622 free_msi_irqs(dev);
623 /* No enough memory. Don't try again */
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200624 ret = -ENOMEM;
625 goto out;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900626 }
627
628 entry->msi_attrib.is_msix = 1;
629 entry->msi_attrib.is_64 = 1;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200630
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900631 if (entries)
632 entry->msi_attrib.entry_nr = entries[i].entry;
633 else
634 entry->msi_attrib.entry_nr = i;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600635
636 entry->msi_attrib.is_virtual =
637 entry->msi_attrib.entry_nr >= vec_count;
638
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900639 entry->msi_attrib.default_irq = dev->irq;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900640 entry->mask_base = base;
641
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200642 if (!entry->msi_attrib.is_virtual) {
643 addr = pci_msix_desc_addr(entry);
Thomas Gleixner67961e72021-07-29 23:51:53 +0200644 entry->msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200645 }
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200646
Jiang Liu5004e982015-07-09 16:00:41 +0800647 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200648 if (masks)
649 curmsk++;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900650 }
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200651 ret = 0;
652out:
653 kfree(masks);
Christophe JAILLET3adfb572017-01-27 16:14:53 +0100654 return ret;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900655}
656
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200657static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900658{
659 struct msi_desc *entry;
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900660
Jiang Liu5004e982015-07-09 16:00:41 +0800661 for_each_pci_msi_entry(entry, dev) {
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200662 if (entries) {
663 entries->vector = entry->irq;
664 entries++;
665 }
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900666 }
667}
668
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200669static void msix_mask_all(void __iomem *base, int tsize)
670{
671 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
672 int i;
673
Marek Marczykowski-Górecki1a519dc2021-08-26 19:03:42 +0200674 if (pci_msi_ignore_mask)
675 return;
676
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200677 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
678 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
682 * msix_capability_init - configure device's MSI-X capability
683 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700684 * @entries: pointer to an array of struct msix_entry entries
685 * @nvec: number of @entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500686 * @affd: Optional pointer to enable automatic affinity assignment
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600688 * Setup the MSI-X capability structure of device function with a
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500689 * single MSI-X IRQ. A return of zero indicates the successful setup of
690 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 **/
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200692static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
Ming Leic66d4bd2019-02-16 18:13:09 +0100693 int nvec, struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 void __iomem *base;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200696 int ret, tsize;
697 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Thomas Gleixner43855392021-07-29 23:51:40 +0200699 /*
700 * Some devices require MSI-X to be enabled before the MSI-X
701 * registers can be accessed. Mask all the vectors to prevent
702 * interrupts coming in before they're fully set up.
703 */
704 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
705 PCI_MSIX_FLAGS_ENABLE);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700706
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800707 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* Request & Map MSI-X table region */
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200709 tsize = msix_table_size(control);
710 base = msix_map_region(dev, tsize);
Thomas Gleixner43855392021-07-29 23:51:40 +0200711 if (!base) {
712 ret = -ENOMEM;
713 goto out_disable;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200716 /* Ensure that all table entries are masked. */
717 msix_mask_all(base, tsize);
718
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800719 ret = msix_setup_entries(dev, base, entries, nvec, affd);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900720 if (ret)
Thomas Gleixner43855392021-07-29 23:51:40 +0200721 goto out_disable;
Michael Ellerman9c831332007-04-18 19:39:21 +1000722
Jiang Liu8e047ad2014-11-15 22:24:07 +0800723 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900724 if (ret)
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100725 goto out_avail;
Michael Ellerman9c831332007-04-18 19:39:21 +1000726
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000727 /* Check if all MSI entries honor device restrictions */
728 ret = msi_verify_entries(dev);
729 if (ret)
730 goto out_free;
731
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200732 msix_update_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700733
Barry Song2f170812021-08-13 15:56:27 +1200734 dev->msi_irq_groups = msi_populate_sysfs(&dev->dev);
735 if (IS_ERR(dev->msi_irq_groups)) {
736 ret = PTR_ERR(dev->msi_irq_groups);
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100737 goto out_free;
Barry Song2f170812021-08-13 15:56:27 +1200738 }
Neil Hormanda8d1c82011-10-06 14:08:18 -0400739
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700740 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700741 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800742 dev->msix_enabled = 1;
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500743 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600744
Jiang Liu5f226992015-07-30 14:00:08 -0500745 pcibios_free_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900747
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100748out_avail:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900749 if (ret < 0) {
750 /*
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500751 * If we had some success, report the number of IRQs
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900752 * we succeeded in setting up.
753 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900754 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900755 int avail = 0;
756
Jiang Liu5004e982015-07-09 16:00:41 +0800757 for_each_pci_msi_entry(entry, dev) {
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900758 if (entry->irq != 0)
759 avail++;
760 }
761 if (avail != 0)
762 ret = avail;
763 }
764
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100765out_free:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900766 free_msi_irqs(dev);
767
Thomas Gleixner43855392021-07-29 23:51:40 +0200768out_disable:
769 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
770
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900771 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/**
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600775 * pci_msi_supported - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400776 * @dev: pointer to the pci_dev data structure of MSI device function
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500777 * @nvec: how many MSIs have been requested?
Brice Goglin24334a12006-08-31 01:55:07 -0400778 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700779 * Look at global flags, the device itself, and its parent buses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000780 * to determine if MSI/-X are supported for the device. If MSI/-X is
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600781 * supported return 1, else return 0.
Brice Goglin24334a12006-08-31 01:55:07 -0400782 **/
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600783static int pci_msi_supported(struct pci_dev *dev, int nvec)
Brice Goglin24334a12006-08-31 01:55:07 -0400784{
785 struct pci_bus *bus;
786
Brice Goglin0306ebf2006-10-05 10:24:31 +0200787 /* MSI must be globally enabled and supported by the device */
Alexander Gordeev27e20602014-09-23 14:25:11 -0600788 if (!pci_msi_enable)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600789 return 0;
Alexander Gordeev27e20602014-09-23 14:25:11 -0600790
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500791 if (!dev || dev->no_msi)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600792 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400793
Michael Ellerman314e77b2007-04-05 17:19:12 +1000794 /*
795 * You can't ask to have 0 or less MSIs configured.
796 * a) it's stupid ..
797 * b) the list manipulation code assumes nvec >= 1.
798 */
799 if (nvec < 1)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600800 return 0;
Michael Ellerman314e77b2007-04-05 17:19:12 +1000801
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900802 /*
803 * Any bridge which does NOT route MSI transactions from its
804 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200805 * the secondary pci_bus.
Marc Zyngier61af6922021-03-30 16:11:44 +0100806 *
807 * The NO_MSI flag can either be set directly by:
808 * - arch-specific PCI host bus controller drivers (deprecated)
809 * - quirks for specific PCI bridges
810 *
811 * or indirectly by platform-specific PCI host bridge drivers by
812 * advertising the 'msi_domain' property, which results in
813 * the NO_MSI flag when no MSI domain is found for this bridge
814 * at probe time.
Brice Goglin0306ebf2006-10-05 10:24:31 +0200815 */
Brice Goglin24334a12006-08-31 01:55:07 -0400816 for (bus = dev->bus; bus; bus = bus->parent)
817 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600818 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400819
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600820 return 1;
Brice Goglin24334a12006-08-31 01:55:07 -0400821}
822
823/**
Alexander Gordeevd1ac1d22013-12-30 08:28:13 +0100824 * pci_msi_vec_count - Return the number of MSI vectors a device can send
825 * @dev: device to report about
826 *
827 * This function returns the number of MSI vectors a device requested via
828 * Multiple Message Capable register. It returns a negative errno if the
829 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
830 * and returns a power of two, up to a maximum of 2^5 (32), according to the
831 * MSI specification.
832 **/
833int pci_msi_vec_count(struct pci_dev *dev)
834{
835 int ret;
836 u16 msgctl;
837
838 if (!dev->msi_cap)
839 return -EINVAL;
840
841 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
842 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
843
844 return ret;
845}
846EXPORT_SYMBOL(pci_msi_vec_count);
847
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600848static void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400850 struct msi_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100852 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700853 return;
854
Jiang Liu5004e982015-07-09 16:00:41 +0800855 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
Jiang Liu4a7cc832015-07-09 16:00:44 +0800856 desc = first_pci_msi_entry(dev);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600857
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500858 pci_msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700859 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800860 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700861
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900862 /* Return the device with MSI unmasked as initial states */
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200863 pci_msi_unmask(desc, msi_multi_mask(desc));
Michael Ellermane387b9e2007-03-22 21:51:27 +1100864
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500865 /* Restore dev->irq to its default pin-assertion IRQ */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400866 dev->irq = desc->msi_attrib.default_irq;
Jiang Liu5f226992015-07-30 14:00:08 -0500867 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700868}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400869
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900870void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700871{
Yinghai Lud52877c2008-04-23 14:58:09 -0700872 if (!pci_msi_enable || !dev || !dev->msi_enabled)
873 return;
874
875 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900876 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100878EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/**
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100881 * pci_msix_vec_count - return the number of device's MSI-X table entries
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100882 * @dev: pointer to the pci_dev data structure of MSI-X device function
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100883 * This function returns the number of device's MSI-X table entries and
884 * therefore the number of MSI-X vectors device is capable of sending.
885 * It returns a negative errno if the device is not capable of sending MSI-X
886 * interrupts.
887 **/
888int pci_msix_vec_count(struct pci_dev *dev)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100889{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100890 u16 control;
891
Gavin Shan520fe9d2013-04-04 16:54:33 +0000892 if (!dev->msix_cap)
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100893 return -EINVAL;
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100894
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600895 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600896 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100897}
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100898EXPORT_SYMBOL(pci_msix_vec_count);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100899
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200900static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600901 int nvec, struct irq_affinity *affd, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Bjorn Helgaas5ec09402014-09-23 14:38:28 -0600903 int nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700904 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500906 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600907 return -EINVAL;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000908
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100909 nr_entries = pci_msix_vec_count(dev);
910 if (nr_entries < 0)
911 return nr_entries;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600912 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300913 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900915 if (entries) {
916 /* Check for any invalid entries */
917 for (i = 0; i < nvec; i++) {
918 if (entries[i].entry >= nr_entries)
919 return -EINVAL; /* invalid entry */
920 for (j = i + 1; j < nvec; j++) {
921 if (entries[i].entry == entries[j].entry)
922 return -EINVAL; /* duplicate entry */
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700926
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500927 /* Check whether driver already requested for MSI IRQ */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900928 if (dev->msi_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600929 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EINVAL;
931 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800932 return msix_capability_init(dev, entries, nvec, affd);
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200933}
934
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600935static void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100936{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900937 struct msi_desc *entry;
938
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100939 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700940 return;
941
Keith Busch01705912017-03-29 22:49:11 -0500942 if (pci_dev_is_disconnected(dev)) {
943 dev->msix_enabled = 0;
944 return;
945 }
946
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900947 /* Return the device with MSI-X masked as initial states */
Thomas Gleixner689e6b52021-07-29 23:51:45 +0200948 for_each_pci_msi_entry(entry, dev)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200949 pci_msix_mask(entry);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900950
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500951 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
David Millerba698ad2007-10-25 01:16:30 -0700952 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800953 dev->msix_enabled = 0;
Jiang Liu5f226992015-07-30 14:00:08 -0500954 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700955}
Hidetoshi Setoc9018512009-08-06 11:31:27 +0900956
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900957void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700958{
959 if (!pci_msi_enable || !dev || !dev->msix_enabled)
960 return;
961
962 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900963 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100965EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700967void pci_no_msi(void)
968{
969 pci_msi_enable = 0;
970}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000971
Andrew Patterson07ae95f2008-11-10 15:31:05 -0700972/**
973 * pci_msi_enabled - is MSI enabled?
974 *
975 * Returns true if MSI has not been disabled by the command-line option
976 * pci=nomsi.
977 **/
978int pci_msi_enabled(void)
979{
980 return pci_msi_enable;
981}
982EXPORT_SYMBOL(pci_msi_enabled);
983
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900984static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100985 struct irq_affinity *affd)
Alexander Gordeev302a2522013-12-30 08:28:16 +0100986{
Alexander Gordeev034cd972014-04-14 15:28:35 +0200987 int nvec;
Alexander Gordeev302a2522013-12-30 08:28:16 +0100988 int rc;
989
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500990 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600991 return -EINVAL;
Alexander Gordeev034cd972014-04-14 15:28:35 +0200992
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500993 /* Check whether driver already requested MSI-X IRQs */
Alexander Gordeev034cd972014-04-14 15:28:35 +0200994 if (dev->msix_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600995 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
Alexander Gordeev034cd972014-04-14 15:28:35 +0200996 return -EINVAL;
997 }
998
Alexander Gordeev302a2522013-12-30 08:28:16 +0100999 if (maxvec < minvec)
1000 return -ERANGE;
1001
Tonghao Zhang4c1ef722018-09-24 07:00:41 -07001002 if (WARN_ON_ONCE(dev->msi_enabled))
1003 return -EINVAL;
1004
Alexander Gordeev034cd972014-04-14 15:28:35 +02001005 nvec = pci_msi_vec_count(dev);
1006 if (nvec < 0)
1007 return nvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001008 if (nvec < minvec)
Dennis Chen948b7622016-12-01 10:15:04 +08001009 return -ENOSPC;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001010
1011 if (nvec > maxvec)
Alexander Gordeev034cd972014-04-14 15:28:35 +02001012 nvec = maxvec;
1013
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001014 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001015 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -07001016 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001017 if (nvec < minvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001018 return -ENOSPC;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001019 }
Alexander Gordeev302a2522013-12-30 08:28:16 +01001020
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001021 rc = msi_capability_init(dev, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001022 if (rc == 0)
1023 return nvec;
1024
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001025 if (rc < 0)
1026 return rc;
1027 if (rc < minvec)
1028 return -ENOSPC;
1029
1030 nvec = rc;
1031 }
1032}
1033
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001034/* deprecated, don't use */
1035int pci_enable_msi(struct pci_dev *dev)
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001036{
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001037 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1038 if (rc < 0)
1039 return rc;
1040 return 0;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001041}
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001042EXPORT_SYMBOL(pci_enable_msi);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001043
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001044static int __pci_enable_msix_range(struct pci_dev *dev,
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001045 struct msix_entry *entries, int minvec,
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001046 int maxvec, struct irq_affinity *affd,
1047 int flags)
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001048{
Thomas Gleixnere75eafb2016-09-14 16:18:49 +02001049 int rc, nvec = maxvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001050
1051 if (maxvec < minvec)
1052 return -ERANGE;
1053
Tonghao Zhang4c1ef722018-09-24 07:00:41 -07001054 if (WARN_ON_ONCE(dev->msix_enabled))
1055 return -EINVAL;
1056
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001057 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001058 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -07001059 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001060 if (nvec < minvec)
1061 return -ENOSPC;
1062 }
1063
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001064 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001065 if (rc == 0)
1066 return nvec;
1067
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001068 if (rc < 0)
1069 return rc;
1070 if (rc < minvec)
1071 return -ENOSPC;
1072
1073 nvec = rc;
1074 }
1075}
1076
Alexander Gordeev302a2522013-12-30 08:28:16 +01001077/**
1078 * pci_enable_msix_range - configure device's MSI-X capability structure
1079 * @dev: pointer to the pci_dev data structure of MSI-X device function
1080 * @entries: pointer to an array of MSI-X entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001081 * @minvec: minimum number of MSI-X IRQs requested
1082 * @maxvec: maximum number of MSI-X IRQs requested
Alexander Gordeev302a2522013-12-30 08:28:16 +01001083 *
1084 * Setup the MSI-X capability structure of device function with a maximum
1085 * possible number of interrupts in the range between @minvec and @maxvec
1086 * upon its software driver call to request for MSI-X mode enabled on its
1087 * hardware device function. It returns a negative errno if an error occurs.
1088 * If it succeeds, it returns the actual number of interrupts allocated and
1089 * indicates the successful configuration of MSI-X capability structure
1090 * with new allocated MSI-X interrupts.
1091 **/
1092int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001093 int minvec, int maxvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001094{
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001095 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001096}
1097EXPORT_SYMBOL(pci_enable_msix_range);
Jiang Liu3878eae2014-11-11 21:02:18 +08001098
Christoph Hellwigaff17162016-07-12 18:20:17 +09001099/**
Christoph Hellwig402723a2016-11-08 17:15:05 -08001100 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
Christoph Hellwigaff17162016-07-12 18:20:17 +09001101 * @dev: PCI device to operate on
1102 * @min_vecs: minimum number of vectors required (must be >= 1)
1103 * @max_vecs: maximum (desired) number of vectors
1104 * @flags: flags or quirks for the allocation
Christoph Hellwig402723a2016-11-08 17:15:05 -08001105 * @affd: optional description of the affinity requirements
Christoph Hellwigaff17162016-07-12 18:20:17 +09001106 *
1107 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1108 * vectors if available, and fall back to a single legacy vector
1109 * if neither is available. Return the number of vectors allocated,
1110 * (which might be smaller than @max_vecs) if successful, or a negative
1111 * error code on error. If less than @min_vecs interrupt vectors are
1112 * available for @dev the function will fail with -ENOSPC.
1113 *
1114 * To get the Linux IRQ number used for a vector that can be passed to
1115 * request_irq() use the pci_irq_vector() helper.
1116 */
Christoph Hellwig402723a2016-11-08 17:15:05 -08001117int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1118 unsigned int max_vecs, unsigned int flags,
Ming Leic66d4bd2019-02-16 18:13:09 +01001119 struct irq_affinity *affd)
Christoph Hellwigaff17162016-07-12 18:20:17 +09001120{
Ming Leic66d4bd2019-02-16 18:13:09 +01001121 struct irq_affinity msi_default_affd = {0};
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001122 int nvecs = -ENOSPC;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001123
Christoph Hellwig402723a2016-11-08 17:15:05 -08001124 if (flags & PCI_IRQ_AFFINITY) {
1125 if (!affd)
1126 affd = &msi_default_affd;
1127 } else {
1128 if (WARN_ON(affd))
1129 affd = NULL;
1130 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001131
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001132 if (flags & PCI_IRQ_MSIX) {
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001133 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1134 affd, flags);
1135 if (nvecs > 0)
1136 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001137 }
1138
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001139 if (flags & PCI_IRQ_MSI) {
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001140 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1141 if (nvecs > 0)
1142 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001143 }
1144
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001145 /* use legacy IRQ if allowed */
Christoph Hellwig862290f2017-02-01 14:41:42 +01001146 if (flags & PCI_IRQ_LEGACY) {
1147 if (min_vecs == 1 && dev->irq) {
Ming Leic66d4bd2019-02-16 18:13:09 +01001148 /*
1149 * Invoke the affinity spreading logic to ensure that
1150 * the device driver can adjust queue configuration
1151 * for the single interrupt case.
1152 */
1153 if (affd)
1154 irq_create_affinity_masks(1, affd);
Christoph Hellwig862290f2017-02-01 14:41:42 +01001155 pci_intx(dev, 1);
1156 return 1;
1157 }
Christoph Hellwig5d0bdf22016-08-11 07:11:05 -07001158 }
1159
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001160 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001161}
Christoph Hellwig402723a2016-11-08 17:15:05 -08001162EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
Christoph Hellwigaff17162016-07-12 18:20:17 +09001163
1164/**
1165 * pci_free_irq_vectors - free previously allocated IRQs for a device
1166 * @dev: PCI device to operate on
1167 *
1168 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1169 */
1170void pci_free_irq_vectors(struct pci_dev *dev)
1171{
1172 pci_disable_msix(dev);
1173 pci_disable_msi(dev);
1174}
1175EXPORT_SYMBOL(pci_free_irq_vectors);
1176
1177/**
1178 * pci_irq_vector - return Linux IRQ number of a device vector
1179 * @dev: PCI device to operate on
1180 * @nr: device-relative interrupt vector index (0-based).
1181 */
1182int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1183{
1184 if (dev->msix_enabled) {
1185 struct msi_desc *entry;
1186 int i = 0;
1187
1188 for_each_pci_msi_entry(entry, dev) {
1189 if (i == nr)
1190 return entry->irq;
1191 i++;
1192 }
1193 WARN_ON_ONCE(1);
1194 return -EINVAL;
1195 }
1196
1197 if (dev->msi_enabled) {
1198 struct msi_desc *entry = first_pci_msi_entry(dev);
1199
1200 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1201 return -EINVAL;
1202 } else {
1203 if (WARN_ON_ONCE(nr > 0))
1204 return -EINVAL;
1205 }
1206
1207 return dev->irq + nr;
1208}
1209EXPORT_SYMBOL(pci_irq_vector);
1210
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001211/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001212 * pci_irq_get_affinity - return the affinity of a particular MSI vector
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001213 * @dev: PCI device to operate on
1214 * @nr: device-relative interrupt vector index (0-based).
1215 */
1216const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1217{
1218 if (dev->msix_enabled) {
1219 struct msi_desc *entry;
1220 int i = 0;
1221
1222 for_each_pci_msi_entry(entry, dev) {
1223 if (i == nr)
Dou Liyangbec04032018-12-04 23:51:20 +08001224 return &entry->affinity->mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001225 i++;
1226 }
1227 WARN_ON_ONCE(1);
1228 return NULL;
1229 } else if (dev->msi_enabled) {
1230 struct msi_desc *entry = first_pci_msi_entry(dev);
1231
Jan Beulichd1d111e2016-11-08 00:43:54 -07001232 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1233 nr >= entry->nvec_used))
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001234 return NULL;
1235
Dou Liyangbec04032018-12-04 23:51:20 +08001236 return &entry->affinity[nr].mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001237 } else {
1238 return cpu_possible_mask;
1239 }
1240}
1241EXPORT_SYMBOL(pci_irq_get_affinity);
1242
Jiang Liu25a98bd2015-07-09 16:00:45 +08001243struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1244{
1245 return to_pci_dev(desc->dev);
1246}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001247EXPORT_SYMBOL(msi_desc_to_pci_dev);
Jiang Liu25a98bd2015-07-09 16:00:45 +08001248
Jiang Liuc179c9b2015-07-09 16:00:36 +08001249void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1250{
1251 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1252
1253 return dev->bus->sysdata;
1254}
1255EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1256
Jiang Liu3878eae2014-11-11 21:02:18 +08001257#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1258/**
1259 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1260 * @irq_data: Pointer to interrupt data of the MSI interrupt
1261 * @msg: Pointer to the message
1262 */
1263void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1264{
Jiang Liu507a8832015-06-01 16:05:42 +08001265 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
Jiang Liu3878eae2014-11-11 21:02:18 +08001266
1267 /*
1268 * For MSI-X desc->irq is always equal to irq_data->irq. For
1269 * MSI only the first interrupt of MULTI MSI passes the test.
1270 */
1271 if (desc->irq == irq_data->irq)
1272 __pci_write_msi_msg(desc, msg);
1273}
1274
1275/**
1276 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001277 * @desc: Pointer to the MSI descriptor
Jiang Liu3878eae2014-11-11 21:02:18 +08001278 *
1279 * The ID number is only used within the irqdomain.
1280 */
Thomas Gleixner9006c132020-08-26 13:16:47 +02001281static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
Jiang Liu3878eae2014-11-11 21:02:18 +08001282{
Thomas Gleixnerdfb9eb72020-08-26 13:16:45 +02001283 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1284
Jiang Liu3878eae2014-11-11 21:02:18 +08001285 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001286 pci_dev_id(dev) << 11 |
Jiang Liu3878eae2014-11-11 21:02:18 +08001287 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1288}
1289
1290static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1291{
1292 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1293}
1294
1295/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001296 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
1297 * for @dev
Jiang Liu3878eae2014-11-11 21:02:18 +08001298 * @domain: The interrupt domain to check
1299 * @info: The domain info for verification
1300 * @dev: The device to check
1301 *
1302 * Returns:
1303 * 0 if the functionality is supported
1304 * 1 if Multi MSI is requested, but the domain does not support it
1305 * -ENOTSUPP otherwise
1306 */
1307int pci_msi_domain_check_cap(struct irq_domain *domain,
1308 struct msi_domain_info *info, struct device *dev)
1309{
1310 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1311
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001312 /* Special handling to support __pci_enable_msi_range() */
Jiang Liu3878eae2014-11-11 21:02:18 +08001313 if (pci_msi_desc_is_multi_msi(desc) &&
1314 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1315 return 1;
1316 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1317 return -ENOTSUPP;
1318
1319 return 0;
1320}
1321
1322static int pci_msi_domain_handle_error(struct irq_domain *domain,
1323 struct msi_desc *desc, int error)
1324{
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001325 /* Special handling to support __pci_enable_msi_range() */
Jiang Liu3878eae2014-11-11 21:02:18 +08001326 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1327 return 1;
1328
1329 return error;
1330}
1331
Jiang Liu3878eae2014-11-11 21:02:18 +08001332static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1333 struct msi_desc *desc)
1334{
1335 arg->desc = desc;
Thomas Gleixnerdfb9eb72020-08-26 13:16:45 +02001336 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
Jiang Liu3878eae2014-11-11 21:02:18 +08001337}
Jiang Liu3878eae2014-11-11 21:02:18 +08001338
1339static struct msi_domain_ops pci_msi_domain_ops_default = {
1340 .set_desc = pci_msi_domain_set_desc,
1341 .msi_check = pci_msi_domain_check_cap,
1342 .handle_error = pci_msi_domain_handle_error,
1343};
1344
1345static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1346{
1347 struct msi_domain_ops *ops = info->ops;
1348
1349 if (ops == NULL) {
1350 info->ops = &pci_msi_domain_ops_default;
1351 } else {
1352 if (ops->set_desc == NULL)
1353 ops->set_desc = pci_msi_domain_set_desc;
1354 if (ops->msi_check == NULL)
1355 ops->msi_check = pci_msi_domain_check_cap;
1356 if (ops->handle_error == NULL)
1357 ops->handle_error = pci_msi_domain_handle_error;
1358 }
1359}
1360
1361static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1362{
1363 struct irq_chip *chip = info->chip;
1364
1365 BUG_ON(!chip);
1366 if (!chip->irq_write_msi_msg)
1367 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
Marc Zyngier0701c532015-10-13 19:14:45 +01001368 if (!chip->irq_mask)
1369 chip->irq_mask = pci_msi_mask_irq;
1370 if (!chip->irq_unmask)
1371 chip->irq_unmask = pci_msi_unmask_irq;
Jiang Liu3878eae2014-11-11 21:02:18 +08001372}
1373
1374/**
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001375 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1376 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liu3878eae2014-11-11 21:02:18 +08001377 * @info: MSI domain info
1378 * @parent: Parent irq domain
1379 *
1380 * Updates the domain and chip ops and creates a MSI interrupt domain.
1381 *
1382 * Returns:
1383 * A domain pointer or NULL in case of failure.
1384 */
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001385struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
Jiang Liu3878eae2014-11-11 21:02:18 +08001386 struct msi_domain_info *info,
1387 struct irq_domain *parent)
1388{
Marc Zyngier03808392015-07-28 14:46:09 +01001389 struct irq_domain *domain;
1390
Marc Zyngier6988e0e2018-05-08 13:14:31 +01001391 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1392 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1393
Jiang Liu3878eae2014-11-11 21:02:18 +08001394 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1395 pci_msi_domain_update_dom_ops(info);
1396 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1397 pci_msi_domain_update_chip_ops(info);
1398
Marc Zyngierf3b09462016-07-13 17:18:33 +01001399 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
Thomas Gleixner25e960e2017-10-17 09:54:58 +02001400 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1401 info->flags |= MSI_FLAG_MUST_REACTIVATE;
Marc Zyngierf3b09462016-07-13 17:18:33 +01001402
Heiner Kallweit923aa4c2018-08-05 22:31:03 +02001403 /* PCI-MSI is oneshot-safe */
1404 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1405
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001406 domain = msi_create_irq_domain(fwnode, info, parent);
Marc Zyngier03808392015-07-28 14:46:09 +01001407 if (!domain)
1408 return NULL;
1409
Marc Zyngier96f0d932017-06-22 11:42:50 +01001410 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
Marc Zyngier03808392015-07-28 14:46:09 +01001411 return domain;
Jiang Liu3878eae2014-11-11 21:02:18 +08001412}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001413EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
Jiang Liu3878eae2014-11-11 21:02:18 +08001414
Robin Murphy235b2c72017-08-01 18:59:08 +01001415/*
1416 * Users of the generic MSI infrastructure expect a device to have a single ID,
1417 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1418 * DMA phantom functions tend to still emit MSIs from the real function number,
1419 * so we ignore those and only consider topological aliases where either the
1420 * alias device or RID appears on a different bus number. We also make the
1421 * reasonable assumption that bridges are walked in an upstream direction (so
1422 * the last one seen wins), and the much braver assumption that the most likely
1423 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1424 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1425 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1426 * for taking ownership all we can really do is close our eyes and hope...
1427 */
David Daneyb6eec9b2015-10-08 15:10:49 -07001428static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1429{
1430 u32 *pa = data;
Robin Murphy235b2c72017-08-01 18:59:08 +01001431 u8 bus = PCI_BUS_NUM(*pa);
David Daneyb6eec9b2015-10-08 15:10:49 -07001432
Robin Murphy235b2c72017-08-01 18:59:08 +01001433 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1434 *pa = alias;
1435
David Daneyb6eec9b2015-10-08 15:10:49 -07001436 return 0;
1437}
Robin Murphy235b2c72017-08-01 18:59:08 +01001438
David Daneyb6eec9b2015-10-08 15:10:49 -07001439/**
1440 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1441 * @domain: The interrupt domain
1442 * @pdev: The PCI device.
1443 *
1444 * The RID for a device is formed from the alias, with a firmware
1445 * supplied mapping applied
1446 *
1447 * Returns: The RID.
1448 */
1449u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1450{
1451 struct device_node *of_node;
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001452 u32 rid = pci_dev_id(pdev);
David Daneyb6eec9b2015-10-08 15:10:49 -07001453
1454 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1455
1456 of_node = irq_domain_get_of_node(domain);
Lorenzo Pieralisi2bcdd8f2020-06-19 09:20:11 +01001457 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +01001458 iort_msi_map_id(&pdev->dev, rid);
David Daneyb6eec9b2015-10-08 15:10:49 -07001459
1460 return rid;
1461}
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001462
1463/**
1464 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1465 * @pdev: The PCI device
1466 *
1467 * Use the firmware data to find a device-specific MSI domain
Robin Murphy235b2c72017-08-01 18:59:08 +01001468 * (i.e. not one that is set as a default).
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001469 *
Robin Murphy235b2c72017-08-01 18:59:08 +01001470 * Returns: The corresponding MSI domain or NULL if none has been found.
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001471 */
1472struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1473{
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001474 struct irq_domain *dom;
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001475 u32 rid = pci_dev_id(pdev);
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001476
1477 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
Diana Craciun6f881ab2020-06-19 09:20:10 +01001478 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001479 if (!dom)
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +01001480 dom = iort_get_device_domain(&pdev->dev, rid,
1481 DOMAIN_BUS_PCI_MSI);
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001482 return dom;
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001483}
Thomas Gleixner2fd60262020-08-26 13:16:53 +02001484
1485/**
1486 * pci_dev_has_special_msi_domain - Check whether the device is handled by
1487 * a non-standard PCI-MSI domain
1488 * @pdev: The PCI device to check.
1489 *
1490 * Returns: True if the device irqdomain or the bus irqdomain is
1491 * non-standard PCI/MSI.
1492 */
1493bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
1494{
1495 struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
1496
1497 if (!dom)
1498 dom = dev_get_msi_domain(&pdev->bus->dev);
1499
1500 if (!dom)
1501 return true;
1502
1503 return dom->bus_token != DOMAIN_BUS_PCI_MSI;
1504}
1505
Jiang Liu3878eae2014-11-11 21:02:18 +08001506#endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
Bjorn Helgaascbc40d52020-12-03 12:51:08 -06001507#endif /* CONFIG_PCI_MSI */
1508
1509void pci_msi_init(struct pci_dev *dev)
1510{
1511 u16 ctrl;
1512
1513 /*
1514 * Disable the MSI hardware to avoid screaming interrupts
1515 * during boot. This is the power on reset default so
1516 * usually this should be a noop.
1517 */
1518 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1519 if (!dev->msi_cap)
1520 return;
1521
1522 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
1523 if (ctrl & PCI_MSI_FLAGS_ENABLE)
1524 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS,
1525 ctrl & ~PCI_MSI_FLAGS_ENABLE);
Vidya Sagar20532302020-12-03 12:51:10 -06001526
1527 if (!(ctrl & PCI_MSI_FLAGS_64BIT))
1528 dev->no_64bit_msi = 1;
Bjorn Helgaascbc40d52020-12-03 12:51:08 -06001529}
1530
1531void pci_msix_init(struct pci_dev *dev)
1532{
1533 u16 ctrl;
1534
1535 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1536 if (!dev->msix_cap)
1537 return;
1538
1539 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
1540 if (ctrl & PCI_MSIX_FLAGS_ENABLE)
1541 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS,
1542 ctrl & ~PCI_MSIX_FLAGS_ENABLE);
1543}