blob: d84cf30bb279086cc5b23ac8692c0b8987eba8c6 [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 Gleixner9c8e9c92021-11-04 00:27:29 +0100151 if (!desc->msi_attrib.can_mask)
152 return;
153
Thomas Gleixner77e89af2021-07-29 23:51:47 +0200154 raw_spin_lock_irqsave(lock, flags);
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200155 desc->msi_mask &= ~clear;
156 desc->msi_mask |= set;
Jiang Liue39758e2015-07-09 16:00:43 +0800157 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200158 desc->msi_mask);
Thomas Gleixner77e89af2021-07-29 23:51:47 +0200159 raw_spin_unlock_irqrestore(lock, flags);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900160}
161
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200162static inline void pci_msi_mask(struct msi_desc *desc, u32 mask)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900163{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200164 pci_msi_update_mask(desc, 0, mask);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400165}
166
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200167static inline void pci_msi_unmask(struct msi_desc *desc, u32 mask)
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900168{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200169 pci_msi_update_mask(desc, mask, 0);
170}
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600171
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200172static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
173{
174 return desc->mask_base + desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900175}
176
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400177/*
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200178 * This internal function does not flush PCI writes to the device. All
179 * users must ensure that they read from the device before either assuming
180 * that the device state is up to date, or returning out of this file.
181 * It does not affect the msi_desc::msix_ctrl cache either. Use with care!
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400182 */
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200183static void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400184{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200185 void __iomem *desc_addr = pci_msix_desc_addr(desc);
Yijing Wang38737d82014-10-27 10:44:36 +0800186
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100187 if (desc->msi_attrib.can_mask)
188 writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900189}
190
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200191static inline void pci_msix_mask(struct msi_desc *desc)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900192{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200193 desc->msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
194 pci_msix_write_vector_ctrl(desc, desc->msix_ctrl);
195 /* Flush write to device */
196 readl(desc->mask_base);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400197}
198
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200199static inline void pci_msix_unmask(struct msi_desc *desc)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400200{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200201 desc->msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
202 pci_msix_write_vector_ctrl(desc, desc->msix_ctrl);
203}
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400204
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200205static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask)
206{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200207 if (desc->msi_attrib.is_msix)
208 pci_msix_mask(desc);
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100209 else
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200210 pci_msi_mask(desc, mask);
211}
212
213static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask)
214{
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200215 if (desc->msi_attrib.is_msix)
216 pci_msix_unmask(desc);
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100217 else
Thomas Gleixnerfcacdfb2021-08-09 21:08:56 +0200218 pci_msi_unmask(desc, mask);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400219}
220
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100221/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500222 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100223 * @data: pointer to irqdata associated to that interrupt
224 */
225void pci_msi_mask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400226{
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200227 struct msi_desc *desc = irq_data_get_msi_desc(data);
228
229 __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400230}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000231EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400232
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100233/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500234 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100235 * @data: pointer to irqdata associated to that interrupt
236 */
237void pci_msi_unmask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400238{
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200239 struct msi_desc *desc = irq_data_get_msi_desc(data);
240
241 __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000243EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800245void default_restore_msi_irqs(struct pci_dev *dev)
246{
247 struct msi_desc *entry;
248
Jiang Liu5004e982015-07-09 16:00:41 +0800249 for_each_pci_msi_entry(entry, dev)
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800250 default_restore_msi_irq(dev, entry->irq);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800251}
252
Jiang Liu891d4a42014-11-09 23:10:33 +0800253void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700254{
Jiang Liue39758e2015-07-09 16:00:43 +0800255 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
256
257 BUG_ON(dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700258
Ben Hutchings30da5522010-07-23 14:56:28 +0100259 if (entry->msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900260 void __iomem *base = pci_msix_desc_addr(entry);
Ben Hutchings30da5522010-07-23 14:56:28 +0100261
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200262 if (WARN_ON_ONCE(entry->msi_attrib.is_virtual))
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600263 return;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600264
Ben Hutchings30da5522010-07-23 14:56:28 +0100265 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
266 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
267 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
268 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600269 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100270 u16 data;
271
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600272 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
273 &msg->address_lo);
Ben Hutchings30da5522010-07-23 14:56:28 +0100274 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600275 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
276 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600277 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100278 } else {
279 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600280 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100281 }
282 msg->data = data;
283 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700284}
285
Jiang Liu83a18912014-11-09 23:10:34 +0800286void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800287{
Jiang Liue39758e2015-07-09 16:00:43 +0800288 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
289
Keith Busch01705912017-03-29 22:49:11 -0500290 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100291 /* Don't touch the hardware now */
292 } else if (entry->msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900293 void __iomem *base = pci_msix_desc_addr(entry);
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200294 u32 ctrl = entry->msix_ctrl;
295 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400296
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200297 if (entry->msi_attrib.is_virtual)
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600298 goto skip;
299
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200300 /*
301 * The specification mandates that the entry is masked
302 * when the message is modified:
303 *
304 * "If software changes the Address or Data value of an
305 * entry while the entry is unmasked, the result is
306 * undefined."
307 */
308 if (unmasked)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200309 pci_msix_write_vector_ctrl(entry, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200310
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900311 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
312 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
313 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Thomas Gleixnerda181dc2021-07-29 23:51:42 +0200314
315 if (unmasked)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200316 pci_msix_write_vector_ctrl(entry, ctrl);
Thomas Gleixnerb9255a72021-07-29 23:51:43 +0200317
318 /* Ensure that the writes are visible in the device */
319 readl(base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400320 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600321 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400322 u16 msgctl;
323
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600324 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400325 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
326 msgctl |= entry->msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600327 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700328
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600329 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
330 msg->address_lo);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700331 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600332 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
333 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600334 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
335 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700336 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600337 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
338 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700339 }
Thomas Gleixnerb9255a72021-07-29 23:51:43 +0200340 /* Ensure that the writes are visible in the device */
341 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700342 }
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600343
344skip:
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700345 entry->msg = *msg;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600346
347 if (entry->write_msi_msg)
348 entry->write_msi_msg(entry, entry->write_msi_msg_data);
349
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700350}
351
Jiang Liu83a18912014-11-09 23:10:34 +0800352void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800353{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200354 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800355
Jiang Liu83a18912014-11-09 23:10:34 +0800356 __pci_write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800357}
Jiang Liu83a18912014-11-09 23:10:34 +0800358EXPORT_SYMBOL_GPL(pci_write_msi_msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800359
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900360static void free_msi_irqs(struct pci_dev *dev)
361{
Jiang Liu5004e982015-07-09 16:00:41 +0800362 struct list_head *msi_list = dev_to_msi_list(&dev->dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900363 struct msi_desc *entry, *tmp;
Barry Song2f170812021-08-13 15:56:27 +1200364 int i;
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900365
Jiang Liu5004e982015-07-09 16:00:41 +0800366 for_each_pci_msi_entry(entry, dev)
Jiang Liu63a7b172014-11-06 22:20:32 +0800367 if (entry->irq)
368 for (i = 0; i < entry->nvec_used; i++)
369 BUG_ON(irq_has_action(entry->irq + i));
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900370
Thomas Gleixner37354592021-11-09 14:53:57 +0100371 if (dev->msi_irq_groups) {
372 msi_destroy_sysfs(&dev->dev, dev->msi_irq_groups);
373 dev->msi_irq_groups = NULL;
374 }
375
Jiang Liu8e047ad2014-11-15 22:24:07 +0800376 pci_msi_teardown_msi_irqs(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900377
Jiang Liu5004e982015-07-09 16:00:41 +0800378 list_for_each_entry_safe(entry, tmp, msi_list, list) {
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900379 if (entry->msi_attrib.is_msix) {
Jiang Liu5004e982015-07-09 16:00:41 +0800380 if (list_is_last(&entry->list, msi_list))
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900381 iounmap(entry->mask_base);
382 }
Neil Horman424eb392012-01-03 10:29:54 -0500383
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900384 list_del(&entry->list);
Prarit Bhargava81efbad2017-02-15 11:53:08 -0500385 free_msi_entry(entry);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900386 }
387}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900388
David Millerba698ad2007-10-25 01:16:30 -0700389static void pci_intx_for_msi(struct pci_dev *dev, int enable)
390{
391 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
392 pci_intx(dev, enable);
393}
394
Bjorn Helgaas830dfe82020-12-03 12:51:09 -0600395static void pci_msi_set_enable(struct pci_dev *dev, int enable)
396{
397 u16 control;
398
399 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
400 control &= ~PCI_MSI_FLAGS_ENABLE;
401 if (enable)
402 control |= PCI_MSI_FLAGS_ENABLE;
403 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
404}
405
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100406static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800407{
Shaohua Li41017f02006-02-08 17:11:38 +0800408 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700409 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800410
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800411 if (!dev->msi_enabled)
412 return;
413
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200414 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800415
David Millerba698ad2007-10-25 01:16:30 -0700416 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500417 pci_msi_set_enable(dev, 0);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800418 arch_restore_msi_irqs(dev);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700419
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600420 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200421 pci_msi_update_mask(entry, 0, 0);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700422 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400423 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600424 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100425}
426
Bjorn Helgaas830dfe82020-12-03 12:51:09 -0600427static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
428{
429 u16 ctrl;
430
431 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
432 ctrl &= ~clear;
433 ctrl |= set;
434 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
435}
436
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100437static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800438{
Shaohua Li41017f02006-02-08 17:11:38 +0800439 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800440
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700441 if (!dev->msix_enabled)
442 return;
Jiang Liu5004e982015-07-09 16:00:41 +0800443 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700444
Shaohua Li41017f02006-02-08 17:11:38 +0800445 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700446 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500447 pci_msix_clear_and_set_ctrl(dev, 0,
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800448 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
Shaohua Li41017f02006-02-08 17:11:38 +0800449
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800450 arch_restore_msi_irqs(dev);
Jiang Liu5004e982015-07-09 16:00:41 +0800451 for_each_pci_msi_entry(entry, dev)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200452 pci_msix_write_vector_ctrl(entry, entry->msix_ctrl);
Shaohua Li41017f02006-02-08 17:11:38 +0800453
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500454 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800455}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100456
457void pci_restore_msi_state(struct pci_dev *dev)
458{
459 __pci_restore_msi_state(dev);
460 __pci_restore_msix_state(dev);
461}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600462EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800463
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200464static struct msi_desc *
Ming Leic66d4bd2019-02-16 18:13:09 +0100465msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
Yijing Wangd873b4d2014-07-08 10:07:23 +0800466{
Dou Liyangbec04032018-12-04 23:51:20 +0800467 struct irq_affinity_desc *masks = NULL;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800468 struct msi_desc *entry;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200469 u16 control;
470
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500471 if (affd)
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800472 masks = irq_create_affinity_masks(nvec, affd);
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500473
Yijing Wangd873b4d2014-07-08 10:07:23 +0800474 /* MSI Entry Initialization */
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200475 entry = alloc_msi_entry(&dev->dev, nvec, masks);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800476 if (!entry)
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200477 goto out;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800478
479 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Marc Zyngier22266672021-11-04 18:01:29 +0000480 /* Lies, damned lies, and MSIs */
481 if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING)
482 control |= PCI_MSI_FLAGS_MASKBIT;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800483
484 entry->msi_attrib.is_msix = 0;
485 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600486 entry->msi_attrib.is_virtual = 0;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800487 entry->msi_attrib.entry_nr = 0;
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100488 entry->msi_attrib.can_mask = !pci_msi_ignore_mask &&
489 !!(control & PCI_MSI_FLAGS_MASKBIT);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800490 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Yijing Wangd873b4d2014-07-08 10:07:23 +0800491 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
Jiang Liu63a7b172014-11-06 22:20:32 +0800492 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
Yijing Wangd873b4d2014-07-08 10:07:23 +0800493
494 if (control & PCI_MSI_FLAGS_64BIT)
495 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
496 else
497 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
498
499 /* Save the initial mask status */
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100500 if (entry->msi_attrib.can_mask)
Thomas Gleixner67961e72021-07-29 23:51:53 +0200501 pci_read_config_dword(dev, entry->mask_pos, &entry->msi_mask);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800502
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200503out:
504 kfree(masks);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800505 return entry;
506}
507
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000508static int msi_verify_entries(struct pci_dev *dev)
509{
510 struct msi_desc *entry;
511
Thomas Gleixnera6e8b942021-07-29 23:51:52 +0200512 if (!dev->no_64bit_msi)
513 return 0;
514
Jiang Liu5004e982015-07-09 16:00:41 +0800515 for_each_pci_msi_entry(entry, dev) {
Thomas Gleixnera6e8b942021-07-29 23:51:52 +0200516 if (entry->msg.address_hi) {
Vidya Sagar20532302020-12-03 12:51:10 -0600517 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
518 entry->msg.address_hi, entry->msg.address_lo);
519 return -EIO;
520 }
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000521 }
522 return 0;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/**
526 * msi_capability_init - configure device's MSI capability structure
527 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400528 * @nvec: number of interrupts to allocate
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500529 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400531 * Setup the MSI capability structure of the device with the requested
532 * number of interrupts. A return value of zero indicates the successful
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500533 * setup of an entry with the new MSI IRQ. A negative return value indicates
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400534 * an error, and a positive return value indicates the number of interrupts
535 * which could have been allocated.
536 */
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800537static int msi_capability_init(struct pci_dev *dev, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100538 struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Wang Hai2b94b6b2021-10-12 15:15:56 +0800540 const struct attribute_group **groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000542 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500544 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600545
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800546 entry = msi_setup_entry(dev, nvec, affd);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700547 if (!entry)
548 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700549
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500550 /* All MSIs are unmasked by default; mask them all */
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200551 pci_msi_mask(entry, msi_multi_mask(entry));
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400552
Jiang Liu5004e982015-07-09 16:00:41 +0800553 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
Michael Ellerman9c831332007-04-18 19:39:21 +1000554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Configure MSI capability structure */
Jiang Liu8e047ad2014-11-15 22:24:07 +0800556 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200557 if (ret)
558 goto err;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700559
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000560 ret = msi_verify_entries(dev);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200561 if (ret)
562 goto err;
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000563
Wang Hai2b94b6b2021-10-12 15:15:56 +0800564 groups = msi_populate_sysfs(&dev->dev);
565 if (IS_ERR(groups)) {
566 ret = PTR_ERR(groups);
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200567 goto err;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400568 }
569
Wang Hai2b94b6b2021-10-12 15:15:56 +0800570 dev->msi_irq_groups = groups;
571
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500572 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700573 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500574 pci_msi_set_enable(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800575 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jiang Liu5f226992015-07-30 14:00:08 -0500577 pcibios_free_irq(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000578 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200580
581err:
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200582 pci_msi_unmask(entry, msi_multi_mask(entry));
Thomas Gleixner8eb5ce32021-07-29 23:51:54 +0200583 free_msi_irqs(dev);
584 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Krzysztof Wilczyńskifd1ae232021-10-13 01:41:36 +0000587static void __iomem *msix_map_region(struct pci_dev *dev,
588 unsigned int nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900589{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900590 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900591 u32 table_offset;
Yijing Wang6a878e52015-01-28 09:52:17 +0800592 unsigned long flags;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900593 u8 bir;
594
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600595 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
596 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600597 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
Yijing Wang6a878e52015-01-28 09:52:17 +0800598 flags = pci_resource_flags(dev, bir);
599 if (!flags || (flags & IORESOURCE_UNSET))
600 return NULL;
601
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600602 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900603 phys_addr = pci_resource_start(dev, bir) + table_offset;
604
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100605 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900606}
607
Gavin Shan520fe9d2013-04-04 16:54:33 +0000608static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200609 struct msix_entry *entries, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100610 struct irq_affinity *affd)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900611{
Dou Liyangbec04032018-12-04 23:51:20 +0800612 struct irq_affinity_desc *curmsk, *masks = NULL;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900613 struct msi_desc *entry;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200614 void __iomem *addr;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200615 int ret, i;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600616 int vec_count = pci_msix_vec_count(dev);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900617
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500618 if (affd)
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800619 masks = irq_create_affinity_masks(nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900620
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200621 for (i = 0, curmsk = masks; i < nvec; i++) {
622 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900623 if (!entry) {
624 if (!i)
625 iounmap(base);
626 else
627 free_msi_irqs(dev);
628 /* No enough memory. Don't try again */
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200629 ret = -ENOMEM;
630 goto out;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900631 }
632
633 entry->msi_attrib.is_msix = 1;
634 entry->msi_attrib.is_64 = 1;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200635
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900636 if (entries)
637 entry->msi_attrib.entry_nr = entries[i].entry;
638 else
639 entry->msi_attrib.entry_nr = i;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600640
641 entry->msi_attrib.is_virtual =
642 entry->msi_attrib.entry_nr >= vec_count;
643
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100644 entry->msi_attrib.can_mask = !pci_msi_ignore_mask &&
645 !entry->msi_attrib.is_virtual;
646
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900647 entry->msi_attrib.default_irq = dev->irq;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900648 entry->mask_base = base;
649
Thomas Gleixner9c8e9c92021-11-04 00:27:29 +0100650 if (entry->msi_attrib.can_mask) {
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200651 addr = pci_msix_desc_addr(entry);
Thomas Gleixner67961e72021-07-29 23:51:53 +0200652 entry->msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Thomas Gleixnerb296aba2021-07-29 23:51:55 +0200653 }
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200654
Jiang Liu5004e982015-07-09 16:00:41 +0800655 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200656 if (masks)
657 curmsk++;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900658 }
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200659 ret = 0;
660out:
661 kfree(masks);
Christophe JAILLET3adfb572017-01-27 16:14:53 +0100662 return ret;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900663}
664
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200665static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900666{
667 struct msi_desc *entry;
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900668
Jiang Liu5004e982015-07-09 16:00:41 +0800669 for_each_pci_msi_entry(entry, dev) {
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200670 if (entries) {
671 entries->vector = entry->irq;
672 entries++;
673 }
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900674 }
675}
676
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200677static void msix_mask_all(void __iomem *base, int tsize)
678{
679 u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
680 int i;
681
Marek Marczykowski-Górecki1a519dc2021-08-26 19:03:42 +0200682 if (pci_msi_ignore_mask)
683 return;
684
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200685 for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
686 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/**
690 * msix_capability_init - configure device's MSI-X capability
691 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700692 * @entries: pointer to an array of struct msix_entry entries
693 * @nvec: number of @entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500694 * @affd: Optional pointer to enable automatic affinity assignment
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600696 * Setup the MSI-X capability structure of device function with a
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500697 * single MSI-X IRQ. A return of zero indicates the successful setup of
698 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 **/
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200700static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
Ming Leic66d4bd2019-02-16 18:13:09 +0100701 int nvec, struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Wang Hai2b94b6b2021-10-12 15:15:56 +0800703 const struct attribute_group **groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 void __iomem *base;
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200705 int ret, tsize;
706 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Thomas Gleixner43855392021-07-29 23:51:40 +0200708 /*
709 * Some devices require MSI-X to be enabled before the MSI-X
710 * registers can be accessed. Mask all the vectors to prevent
711 * interrupts coming in before they're fully set up.
712 */
713 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
714 PCI_MSIX_FLAGS_ENABLE);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700715
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800716 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Request & Map MSI-X table region */
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200718 tsize = msix_table_size(control);
719 base = msix_map_region(dev, tsize);
Thomas Gleixner43855392021-07-29 23:51:40 +0200720 if (!base) {
721 ret = -ENOMEM;
722 goto out_disable;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800725 ret = msix_setup_entries(dev, base, entries, nvec, affd);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900726 if (ret)
Thomas Gleixner43855392021-07-29 23:51:40 +0200727 goto out_disable;
Michael Ellerman9c831332007-04-18 19:39:21 +1000728
Jiang Liu8e047ad2014-11-15 22:24:07 +0800729 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900730 if (ret)
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100731 goto out_avail;
Michael Ellerman9c831332007-04-18 19:39:21 +1000732
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000733 /* Check if all MSI entries honor device restrictions */
734 ret = msi_verify_entries(dev);
735 if (ret)
736 goto out_free;
737
Thomas Gleixner7d5ec3d2021-07-29 23:51:41 +0200738 msix_update_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700739
Wang Hai2b94b6b2021-10-12 15:15:56 +0800740 groups = msi_populate_sysfs(&dev->dev);
741 if (IS_ERR(groups)) {
742 ret = PTR_ERR(groups);
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100743 goto out_free;
Barry Song2f170812021-08-13 15:56:27 +1200744 }
Neil Hormanda8d1c82011-10-06 14:08:18 -0400745
Wang Hai2b94b6b2021-10-12 15:15:56 +0800746 dev->msi_irq_groups = groups;
747
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700748 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700749 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800750 dev->msix_enabled = 1;
Stefan Roese83dbf892021-12-14 12:49:32 +0100751
752 /*
753 * Ensure that all table entries are masked to prevent
754 * stale entries from firing in a crash kernel.
755 *
756 * Done late to deal with a broken Marvell NVME device
757 * which takes the MSI-X mask bits into account even
758 * when MSI-X is disabled, which prevents MSI delivery.
759 */
760 msix_mask_all(base, tsize);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500761 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600762
Jiang Liu5f226992015-07-30 14:00:08 -0500763 pcibios_free_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900765
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100766out_avail:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900767 if (ret < 0) {
768 /*
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500769 * If we had some success, report the number of IRQs
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900770 * we succeeded in setting up.
771 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900772 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900773 int avail = 0;
774
Jiang Liu5004e982015-07-09 16:00:41 +0800775 for_each_pci_msi_entry(entry, dev) {
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900776 if (entry->irq != 0)
777 avail++;
778 }
779 if (avail != 0)
780 ret = avail;
781 }
782
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100783out_free:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900784 free_msi_irqs(dev);
785
Thomas Gleixner43855392021-07-29 23:51:40 +0200786out_disable:
Thomas Gleixner94185ad2021-12-14 12:42:14 +0100787 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
Thomas Gleixner43855392021-07-29 23:51:40 +0200788
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900789 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/**
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600793 * pci_msi_supported - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400794 * @dev: pointer to the pci_dev data structure of MSI device function
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500795 * @nvec: how many MSIs have been requested?
Brice Goglin24334a12006-08-31 01:55:07 -0400796 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700797 * Look at global flags, the device itself, and its parent buses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000798 * to determine if MSI/-X are supported for the device. If MSI/-X is
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600799 * supported return 1, else return 0.
Brice Goglin24334a12006-08-31 01:55:07 -0400800 **/
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600801static int pci_msi_supported(struct pci_dev *dev, int nvec)
Brice Goglin24334a12006-08-31 01:55:07 -0400802{
803 struct pci_bus *bus;
804
Brice Goglin0306ebf2006-10-05 10:24:31 +0200805 /* MSI must be globally enabled and supported by the device */
Alexander Gordeev27e20602014-09-23 14:25:11 -0600806 if (!pci_msi_enable)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600807 return 0;
Alexander Gordeev27e20602014-09-23 14:25:11 -0600808
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500809 if (!dev || dev->no_msi)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600810 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400811
Michael Ellerman314e77b2007-04-05 17:19:12 +1000812 /*
813 * You can't ask to have 0 or less MSIs configured.
814 * a) it's stupid ..
815 * b) the list manipulation code assumes nvec >= 1.
816 */
817 if (nvec < 1)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600818 return 0;
Michael Ellerman314e77b2007-04-05 17:19:12 +1000819
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900820 /*
821 * Any bridge which does NOT route MSI transactions from its
822 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200823 * the secondary pci_bus.
Marc Zyngier61af6922021-03-30 16:11:44 +0100824 *
825 * The NO_MSI flag can either be set directly by:
826 * - arch-specific PCI host bus controller drivers (deprecated)
827 * - quirks for specific PCI bridges
828 *
829 * or indirectly by platform-specific PCI host bridge drivers by
830 * advertising the 'msi_domain' property, which results in
831 * the NO_MSI flag when no MSI domain is found for this bridge
832 * at probe time.
Brice Goglin0306ebf2006-10-05 10:24:31 +0200833 */
Brice Goglin24334a12006-08-31 01:55:07 -0400834 for (bus = dev->bus; bus; bus = bus->parent)
835 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600836 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400837
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600838 return 1;
Brice Goglin24334a12006-08-31 01:55:07 -0400839}
840
841/**
Alexander Gordeevd1ac1d22013-12-30 08:28:13 +0100842 * pci_msi_vec_count - Return the number of MSI vectors a device can send
843 * @dev: device to report about
844 *
845 * This function returns the number of MSI vectors a device requested via
846 * Multiple Message Capable register. It returns a negative errno if the
847 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
848 * and returns a power of two, up to a maximum of 2^5 (32), according to the
849 * MSI specification.
850 **/
851int pci_msi_vec_count(struct pci_dev *dev)
852{
853 int ret;
854 u16 msgctl;
855
856 if (!dev->msi_cap)
857 return -EINVAL;
858
859 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
860 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
861
862 return ret;
863}
864EXPORT_SYMBOL(pci_msi_vec_count);
865
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600866static void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400868 struct msi_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100870 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700871 return;
872
Jiang Liu5004e982015-07-09 16:00:41 +0800873 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
Jiang Liu4a7cc832015-07-09 16:00:44 +0800874 desc = first_pci_msi_entry(dev);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600875
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500876 pci_msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700877 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800878 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700879
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900880 /* Return the device with MSI unmasked as initial states */
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200881 pci_msi_unmask(desc, msi_multi_mask(desc));
Michael Ellermane387b9e2007-03-22 21:51:27 +1100882
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500883 /* Restore dev->irq to its default pin-assertion IRQ */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400884 dev->irq = desc->msi_attrib.default_irq;
Jiang Liu5f226992015-07-30 14:00:08 -0500885 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700886}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400887
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900888void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700889{
Yinghai Lud52877c2008-04-23 14:58:09 -0700890 if (!pci_msi_enable || !dev || !dev->msi_enabled)
891 return;
892
893 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900894 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100896EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/**
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100899 * pci_msix_vec_count - return the number of device's MSI-X table entries
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100900 * @dev: pointer to the pci_dev data structure of MSI-X device function
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100901 * This function returns the number of device's MSI-X table entries and
902 * therefore the number of MSI-X vectors device is capable of sending.
903 * It returns a negative errno if the device is not capable of sending MSI-X
904 * interrupts.
905 **/
906int pci_msix_vec_count(struct pci_dev *dev)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100907{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100908 u16 control;
909
Gavin Shan520fe9d2013-04-04 16:54:33 +0000910 if (!dev->msix_cap)
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100911 return -EINVAL;
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100912
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600913 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600914 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100915}
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100916EXPORT_SYMBOL(pci_msix_vec_count);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100917
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200918static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600919 int nvec, struct irq_affinity *affd, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Bjorn Helgaas5ec09402014-09-23 14:38:28 -0600921 int nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700922 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500924 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600925 return -EINVAL;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000926
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100927 nr_entries = pci_msix_vec_count(dev);
928 if (nr_entries < 0)
929 return nr_entries;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600930 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300931 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900933 if (entries) {
934 /* Check for any invalid entries */
935 for (i = 0; i < nvec; i++) {
936 if (entries[i].entry >= nr_entries)
937 return -EINVAL; /* invalid entry */
938 for (j = i + 1; j < nvec; j++) {
939 if (entries[i].entry == entries[j].entry)
940 return -EINVAL; /* duplicate entry */
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700944
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500945 /* Check whether driver already requested for MSI IRQ */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900946 if (dev->msi_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600947 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800950 return msix_capability_init(dev, entries, nvec, affd);
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200951}
952
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600953static void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100954{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900955 struct msi_desc *entry;
956
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100957 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700958 return;
959
Keith Busch01705912017-03-29 22:49:11 -0500960 if (pci_dev_is_disconnected(dev)) {
961 dev->msix_enabled = 0;
962 return;
963 }
964
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900965 /* Return the device with MSI-X masked as initial states */
Thomas Gleixner689e6b52021-07-29 23:51:45 +0200966 for_each_pci_msi_entry(entry, dev)
Thomas Gleixner446a98b2021-07-29 23:51:58 +0200967 pci_msix_mask(entry);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900968
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500969 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
David Millerba698ad2007-10-25 01:16:30 -0700970 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800971 dev->msix_enabled = 0;
Jiang Liu5f226992015-07-30 14:00:08 -0500972 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700973}
Hidetoshi Setoc9018512009-08-06 11:31:27 +0900974
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900975void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700976{
977 if (!pci_msi_enable || !dev || !dev->msix_enabled)
978 return;
979
980 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900981 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100983EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700985void pci_no_msi(void)
986{
987 pci_msi_enable = 0;
988}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000989
Andrew Patterson07ae95f2008-11-10 15:31:05 -0700990/**
991 * pci_msi_enabled - is MSI enabled?
992 *
993 * Returns true if MSI has not been disabled by the command-line option
994 * pci=nomsi.
995 **/
996int pci_msi_enabled(void)
997{
998 return pci_msi_enable;
999}
1000EXPORT_SYMBOL(pci_msi_enabled);
1001
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001002static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
Ming Leic66d4bd2019-02-16 18:13:09 +01001003 struct irq_affinity *affd)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001004{
Alexander Gordeev034cd972014-04-14 15:28:35 +02001005 int nvec;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001006 int rc;
1007
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -05001008 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -06001009 return -EINVAL;
Alexander Gordeev034cd972014-04-14 15:28:35 +02001010
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001011 /* Check whether driver already requested MSI-X IRQs */
Alexander Gordeev034cd972014-04-14 15:28:35 +02001012 if (dev->msix_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001013 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
Alexander Gordeev034cd972014-04-14 15:28:35 +02001014 return -EINVAL;
1015 }
1016
Alexander Gordeev302a2522013-12-30 08:28:16 +01001017 if (maxvec < minvec)
1018 return -ERANGE;
1019
Tonghao Zhang4c1ef722018-09-24 07:00:41 -07001020 if (WARN_ON_ONCE(dev->msi_enabled))
1021 return -EINVAL;
1022
Alexander Gordeev034cd972014-04-14 15:28:35 +02001023 nvec = pci_msi_vec_count(dev);
1024 if (nvec < 0)
1025 return nvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001026 if (nvec < minvec)
Dennis Chen948b7622016-12-01 10:15:04 +08001027 return -ENOSPC;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001028
1029 if (nvec > maxvec)
Alexander Gordeev034cd972014-04-14 15:28:35 +02001030 nvec = maxvec;
1031
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001032 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001033 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -07001034 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001035 if (nvec < minvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001036 return -ENOSPC;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001037 }
Alexander Gordeev302a2522013-12-30 08:28:16 +01001038
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001039 rc = msi_capability_init(dev, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001040 if (rc == 0)
1041 return nvec;
1042
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001043 if (rc < 0)
1044 return rc;
1045 if (rc < minvec)
1046 return -ENOSPC;
1047
1048 nvec = rc;
1049 }
1050}
1051
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001052/* deprecated, don't use */
1053int pci_enable_msi(struct pci_dev *dev)
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001054{
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001055 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1056 if (rc < 0)
1057 return rc;
1058 return 0;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001059}
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001060EXPORT_SYMBOL(pci_enable_msi);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001061
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001062static int __pci_enable_msix_range(struct pci_dev *dev,
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001063 struct msix_entry *entries, int minvec,
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001064 int maxvec, struct irq_affinity *affd,
1065 int flags)
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001066{
Thomas Gleixnere75eafb2016-09-14 16:18:49 +02001067 int rc, nvec = maxvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001068
1069 if (maxvec < minvec)
1070 return -ERANGE;
1071
Tonghao Zhang4c1ef722018-09-24 07:00:41 -07001072 if (WARN_ON_ONCE(dev->msix_enabled))
1073 return -EINVAL;
1074
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001075 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001076 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -07001077 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001078 if (nvec < minvec)
1079 return -ENOSPC;
1080 }
1081
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001082 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001083 if (rc == 0)
1084 return nvec;
1085
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001086 if (rc < 0)
1087 return rc;
1088 if (rc < minvec)
1089 return -ENOSPC;
1090
1091 nvec = rc;
1092 }
1093}
1094
Alexander Gordeev302a2522013-12-30 08:28:16 +01001095/**
1096 * pci_enable_msix_range - configure device's MSI-X capability structure
1097 * @dev: pointer to the pci_dev data structure of MSI-X device function
1098 * @entries: pointer to an array of MSI-X entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001099 * @minvec: minimum number of MSI-X IRQs requested
1100 * @maxvec: maximum number of MSI-X IRQs requested
Alexander Gordeev302a2522013-12-30 08:28:16 +01001101 *
1102 * Setup the MSI-X capability structure of device function with a maximum
1103 * possible number of interrupts in the range between @minvec and @maxvec
1104 * upon its software driver call to request for MSI-X mode enabled on its
1105 * hardware device function. It returns a negative errno if an error occurs.
1106 * If it succeeds, it returns the actual number of interrupts allocated and
1107 * indicates the successful configuration of MSI-X capability structure
1108 * with new allocated MSI-X interrupts.
1109 **/
1110int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001111 int minvec, int maxvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001112{
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001113 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001114}
1115EXPORT_SYMBOL(pci_enable_msix_range);
Jiang Liu3878eae2014-11-11 21:02:18 +08001116
Christoph Hellwigaff17162016-07-12 18:20:17 +09001117/**
Christoph Hellwig402723a2016-11-08 17:15:05 -08001118 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
Christoph Hellwigaff17162016-07-12 18:20:17 +09001119 * @dev: PCI device to operate on
1120 * @min_vecs: minimum number of vectors required (must be >= 1)
1121 * @max_vecs: maximum (desired) number of vectors
1122 * @flags: flags or quirks for the allocation
Christoph Hellwig402723a2016-11-08 17:15:05 -08001123 * @affd: optional description of the affinity requirements
Christoph Hellwigaff17162016-07-12 18:20:17 +09001124 *
1125 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1126 * vectors if available, and fall back to a single legacy vector
1127 * if neither is available. Return the number of vectors allocated,
1128 * (which might be smaller than @max_vecs) if successful, or a negative
1129 * error code on error. If less than @min_vecs interrupt vectors are
1130 * available for @dev the function will fail with -ENOSPC.
1131 *
1132 * To get the Linux IRQ number used for a vector that can be passed to
1133 * request_irq() use the pci_irq_vector() helper.
1134 */
Christoph Hellwig402723a2016-11-08 17:15:05 -08001135int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1136 unsigned int max_vecs, unsigned int flags,
Ming Leic66d4bd2019-02-16 18:13:09 +01001137 struct irq_affinity *affd)
Christoph Hellwigaff17162016-07-12 18:20:17 +09001138{
Ming Leic66d4bd2019-02-16 18:13:09 +01001139 struct irq_affinity msi_default_affd = {0};
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001140 int nvecs = -ENOSPC;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001141
Christoph Hellwig402723a2016-11-08 17:15:05 -08001142 if (flags & PCI_IRQ_AFFINITY) {
1143 if (!affd)
1144 affd = &msi_default_affd;
1145 } else {
1146 if (WARN_ON(affd))
1147 affd = NULL;
1148 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001149
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001150 if (flags & PCI_IRQ_MSIX) {
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001151 nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1152 affd, flags);
1153 if (nvecs > 0)
1154 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001155 }
1156
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001157 if (flags & PCI_IRQ_MSI) {
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001158 nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1159 if (nvecs > 0)
1160 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001161 }
1162
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001163 /* use legacy IRQ if allowed */
Christoph Hellwig862290f2017-02-01 14:41:42 +01001164 if (flags & PCI_IRQ_LEGACY) {
1165 if (min_vecs == 1 && dev->irq) {
Ming Leic66d4bd2019-02-16 18:13:09 +01001166 /*
1167 * Invoke the affinity spreading logic to ensure that
1168 * the device driver can adjust queue configuration
1169 * for the single interrupt case.
1170 */
1171 if (affd)
1172 irq_create_affinity_masks(1, affd);
Christoph Hellwig862290f2017-02-01 14:41:42 +01001173 pci_intx(dev, 1);
1174 return 1;
1175 }
Christoph Hellwig5d0bdf22016-08-11 07:11:05 -07001176 }
1177
Piotr Stankiewicz30ff3e82020-06-16 09:33:16 +02001178 return nvecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001179}
Christoph Hellwig402723a2016-11-08 17:15:05 -08001180EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
Christoph Hellwigaff17162016-07-12 18:20:17 +09001181
1182/**
1183 * pci_free_irq_vectors - free previously allocated IRQs for a device
1184 * @dev: PCI device to operate on
1185 *
1186 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1187 */
1188void pci_free_irq_vectors(struct pci_dev *dev)
1189{
1190 pci_disable_msix(dev);
1191 pci_disable_msi(dev);
1192}
1193EXPORT_SYMBOL(pci_free_irq_vectors);
1194
1195/**
1196 * pci_irq_vector - return Linux IRQ number of a device vector
1197 * @dev: PCI device to operate on
1198 * @nr: device-relative interrupt vector index (0-based).
1199 */
1200int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1201{
1202 if (dev->msix_enabled) {
1203 struct msi_desc *entry;
1204 int i = 0;
1205
1206 for_each_pci_msi_entry(entry, dev) {
1207 if (i == nr)
1208 return entry->irq;
1209 i++;
1210 }
1211 WARN_ON_ONCE(1);
1212 return -EINVAL;
1213 }
1214
1215 if (dev->msi_enabled) {
1216 struct msi_desc *entry = first_pci_msi_entry(dev);
1217
1218 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1219 return -EINVAL;
1220 } else {
1221 if (WARN_ON_ONCE(nr > 0))
1222 return -EINVAL;
1223 }
1224
1225 return dev->irq + nr;
1226}
1227EXPORT_SYMBOL(pci_irq_vector);
1228
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001229/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001230 * pci_irq_get_affinity - return the affinity of a particular MSI vector
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001231 * @dev: PCI device to operate on
1232 * @nr: device-relative interrupt vector index (0-based).
1233 */
1234const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1235{
1236 if (dev->msix_enabled) {
1237 struct msi_desc *entry;
1238 int i = 0;
1239
1240 for_each_pci_msi_entry(entry, dev) {
1241 if (i == nr)
Dou Liyangbec04032018-12-04 23:51:20 +08001242 return &entry->affinity->mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001243 i++;
1244 }
1245 WARN_ON_ONCE(1);
1246 return NULL;
1247 } else if (dev->msi_enabled) {
1248 struct msi_desc *entry = first_pci_msi_entry(dev);
1249
Jan Beulichd1d111e2016-11-08 00:43:54 -07001250 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1251 nr >= entry->nvec_used))
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001252 return NULL;
1253
Dou Liyangbec04032018-12-04 23:51:20 +08001254 return &entry->affinity[nr].mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001255 } else {
1256 return cpu_possible_mask;
1257 }
1258}
1259EXPORT_SYMBOL(pci_irq_get_affinity);
1260
Jiang Liu25a98bd2015-07-09 16:00:45 +08001261struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1262{
1263 return to_pci_dev(desc->dev);
1264}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001265EXPORT_SYMBOL(msi_desc_to_pci_dev);
Jiang Liu25a98bd2015-07-09 16:00:45 +08001266
Jiang Liuc179c9b2015-07-09 16:00:36 +08001267void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1268{
1269 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1270
1271 return dev->bus->sysdata;
1272}
1273EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1274
Jiang Liu3878eae2014-11-11 21:02:18 +08001275#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1276/**
1277 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1278 * @irq_data: Pointer to interrupt data of the MSI interrupt
1279 * @msg: Pointer to the message
1280 */
1281void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1282{
Jiang Liu507a8832015-06-01 16:05:42 +08001283 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
Jiang Liu3878eae2014-11-11 21:02:18 +08001284
1285 /*
1286 * For MSI-X desc->irq is always equal to irq_data->irq. For
1287 * MSI only the first interrupt of MULTI MSI passes the test.
1288 */
1289 if (desc->irq == irq_data->irq)
1290 __pci_write_msi_msg(desc, msg);
1291}
1292
1293/**
1294 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001295 * @desc: Pointer to the MSI descriptor
Jiang Liu3878eae2014-11-11 21:02:18 +08001296 *
1297 * The ID number is only used within the irqdomain.
1298 */
Thomas Gleixner9006c132020-08-26 13:16:47 +02001299static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
Jiang Liu3878eae2014-11-11 21:02:18 +08001300{
Thomas Gleixnerdfb9eb72020-08-26 13:16:45 +02001301 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1302
Jiang Liu3878eae2014-11-11 21:02:18 +08001303 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001304 pci_dev_id(dev) << 11 |
Jiang Liu3878eae2014-11-11 21:02:18 +08001305 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1306}
1307
1308static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1309{
1310 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1311}
1312
1313/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001314 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
1315 * for @dev
Jiang Liu3878eae2014-11-11 21:02:18 +08001316 * @domain: The interrupt domain to check
1317 * @info: The domain info for verification
1318 * @dev: The device to check
1319 *
1320 * Returns:
1321 * 0 if the functionality is supported
1322 * 1 if Multi MSI is requested, but the domain does not support it
1323 * -ENOTSUPP otherwise
1324 */
1325int pci_msi_domain_check_cap(struct irq_domain *domain,
1326 struct msi_domain_info *info, struct device *dev)
1327{
1328 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1329
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001330 /* Special handling to support __pci_enable_msi_range() */
Jiang Liu3878eae2014-11-11 21:02:18 +08001331 if (pci_msi_desc_is_multi_msi(desc) &&
1332 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1333 return 1;
1334 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1335 return -ENOTSUPP;
1336
1337 return 0;
1338}
1339
1340static int pci_msi_domain_handle_error(struct irq_domain *domain,
1341 struct msi_desc *desc, int error)
1342{
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001343 /* Special handling to support __pci_enable_msi_range() */
Jiang Liu3878eae2014-11-11 21:02:18 +08001344 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1345 return 1;
1346
1347 return error;
1348}
1349
Jiang Liu3878eae2014-11-11 21:02:18 +08001350static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1351 struct msi_desc *desc)
1352{
1353 arg->desc = desc;
Thomas Gleixnerdfb9eb72020-08-26 13:16:45 +02001354 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
Jiang Liu3878eae2014-11-11 21:02:18 +08001355}
Jiang Liu3878eae2014-11-11 21:02:18 +08001356
1357static struct msi_domain_ops pci_msi_domain_ops_default = {
1358 .set_desc = pci_msi_domain_set_desc,
1359 .msi_check = pci_msi_domain_check_cap,
1360 .handle_error = pci_msi_domain_handle_error,
1361};
1362
1363static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1364{
1365 struct msi_domain_ops *ops = info->ops;
1366
1367 if (ops == NULL) {
1368 info->ops = &pci_msi_domain_ops_default;
1369 } else {
1370 if (ops->set_desc == NULL)
1371 ops->set_desc = pci_msi_domain_set_desc;
1372 if (ops->msi_check == NULL)
1373 ops->msi_check = pci_msi_domain_check_cap;
1374 if (ops->handle_error == NULL)
1375 ops->handle_error = pci_msi_domain_handle_error;
1376 }
1377}
1378
1379static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1380{
1381 struct irq_chip *chip = info->chip;
1382
1383 BUG_ON(!chip);
1384 if (!chip->irq_write_msi_msg)
1385 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
Marc Zyngier0701c532015-10-13 19:14:45 +01001386 if (!chip->irq_mask)
1387 chip->irq_mask = pci_msi_mask_irq;
1388 if (!chip->irq_unmask)
1389 chip->irq_unmask = pci_msi_unmask_irq;
Jiang Liu3878eae2014-11-11 21:02:18 +08001390}
1391
1392/**
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001393 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1394 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liu3878eae2014-11-11 21:02:18 +08001395 * @info: MSI domain info
1396 * @parent: Parent irq domain
1397 *
1398 * Updates the domain and chip ops and creates a MSI interrupt domain.
1399 *
1400 * Returns:
1401 * A domain pointer or NULL in case of failure.
1402 */
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001403struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
Jiang Liu3878eae2014-11-11 21:02:18 +08001404 struct msi_domain_info *info,
1405 struct irq_domain *parent)
1406{
Marc Zyngier03808392015-07-28 14:46:09 +01001407 struct irq_domain *domain;
1408
Marc Zyngier6988e0e2018-05-08 13:14:31 +01001409 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1410 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1411
Jiang Liu3878eae2014-11-11 21:02:18 +08001412 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1413 pci_msi_domain_update_dom_ops(info);
1414 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1415 pci_msi_domain_update_chip_ops(info);
1416
Marc Zyngierf3b09462016-07-13 17:18:33 +01001417 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
Thomas Gleixner25e960e2017-10-17 09:54:58 +02001418 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1419 info->flags |= MSI_FLAG_MUST_REACTIVATE;
Marc Zyngierf3b09462016-07-13 17:18:33 +01001420
Heiner Kallweit923aa4c2018-08-05 22:31:03 +02001421 /* PCI-MSI is oneshot-safe */
1422 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1423
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001424 domain = msi_create_irq_domain(fwnode, info, parent);
Marc Zyngier03808392015-07-28 14:46:09 +01001425 if (!domain)
1426 return NULL;
1427
Marc Zyngier96f0d932017-06-22 11:42:50 +01001428 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
Marc Zyngier03808392015-07-28 14:46:09 +01001429 return domain;
Jiang Liu3878eae2014-11-11 21:02:18 +08001430}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001431EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
Jiang Liu3878eae2014-11-11 21:02:18 +08001432
Robin Murphy235b2c72017-08-01 18:59:08 +01001433/*
1434 * Users of the generic MSI infrastructure expect a device to have a single ID,
1435 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1436 * DMA phantom functions tend to still emit MSIs from the real function number,
1437 * so we ignore those and only consider topological aliases where either the
1438 * alias device or RID appears on a different bus number. We also make the
1439 * reasonable assumption that bridges are walked in an upstream direction (so
1440 * the last one seen wins), and the much braver assumption that the most likely
1441 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1442 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1443 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1444 * for taking ownership all we can really do is close our eyes and hope...
1445 */
David Daneyb6eec9b2015-10-08 15:10:49 -07001446static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1447{
1448 u32 *pa = data;
Robin Murphy235b2c72017-08-01 18:59:08 +01001449 u8 bus = PCI_BUS_NUM(*pa);
David Daneyb6eec9b2015-10-08 15:10:49 -07001450
Robin Murphy235b2c72017-08-01 18:59:08 +01001451 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1452 *pa = alias;
1453
David Daneyb6eec9b2015-10-08 15:10:49 -07001454 return 0;
1455}
Robin Murphy235b2c72017-08-01 18:59:08 +01001456
David Daneyb6eec9b2015-10-08 15:10:49 -07001457/**
1458 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1459 * @domain: The interrupt domain
1460 * @pdev: The PCI device.
1461 *
1462 * The RID for a device is formed from the alias, with a firmware
1463 * supplied mapping applied
1464 *
1465 * Returns: The RID.
1466 */
1467u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1468{
1469 struct device_node *of_node;
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001470 u32 rid = pci_dev_id(pdev);
David Daneyb6eec9b2015-10-08 15:10:49 -07001471
1472 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1473
1474 of_node = irq_domain_get_of_node(domain);
Lorenzo Pieralisi2bcdd8f2020-06-19 09:20:11 +01001475 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +01001476 iort_msi_map_id(&pdev->dev, rid);
David Daneyb6eec9b2015-10-08 15:10:49 -07001477
1478 return rid;
1479}
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001480
1481/**
1482 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1483 * @pdev: The PCI device
1484 *
1485 * Use the firmware data to find a device-specific MSI domain
Robin Murphy235b2c72017-08-01 18:59:08 +01001486 * (i.e. not one that is set as a default).
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001487 *
Robin Murphy235b2c72017-08-01 18:59:08 +01001488 * Returns: The corresponding MSI domain or NULL if none has been found.
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001489 */
1490struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1491{
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001492 struct irq_domain *dom;
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001493 u32 rid = pci_dev_id(pdev);
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001494
1495 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
Diana Craciun6f881ab2020-06-19 09:20:10 +01001496 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001497 if (!dom)
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +01001498 dom = iort_get_device_domain(&pdev->dev, rid,
1499 DOMAIN_BUS_PCI_MSI);
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001500 return dom;
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001501}
Thomas Gleixner2fd60262020-08-26 13:16:53 +02001502
1503/**
1504 * pci_dev_has_special_msi_domain - Check whether the device is handled by
1505 * a non-standard PCI-MSI domain
1506 * @pdev: The PCI device to check.
1507 *
1508 * Returns: True if the device irqdomain or the bus irqdomain is
1509 * non-standard PCI/MSI.
1510 */
1511bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
1512{
1513 struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
1514
1515 if (!dom)
1516 dom = dev_get_msi_domain(&pdev->bus->dev);
1517
1518 if (!dom)
1519 return true;
1520
1521 return dom->bus_token != DOMAIN_BUS_PCI_MSI;
1522}
1523
Jiang Liu3878eae2014-11-11 21:02:18 +08001524#endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */
Bjorn Helgaascbc40d52020-12-03 12:51:08 -06001525#endif /* CONFIG_PCI_MSI */
1526
1527void pci_msi_init(struct pci_dev *dev)
1528{
1529 u16 ctrl;
1530
1531 /*
1532 * Disable the MSI hardware to avoid screaming interrupts
1533 * during boot. This is the power on reset default so
1534 * usually this should be a noop.
1535 */
1536 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1537 if (!dev->msi_cap)
1538 return;
1539
1540 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
1541 if (ctrl & PCI_MSI_FLAGS_ENABLE)
1542 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS,
1543 ctrl & ~PCI_MSI_FLAGS_ENABLE);
Vidya Sagar20532302020-12-03 12:51:10 -06001544
1545 if (!(ctrl & PCI_MSI_FLAGS_64BIT))
1546 dev->no_64bit_msi = 1;
Bjorn Helgaascbc40d52020-12-03 12:51:08 -06001547}
1548
1549void pci_msix_init(struct pci_dev *dev)
1550{
1551 u16 ctrl;
1552
1553 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1554 if (!dev->msix_cap)
1555 return;
1556
1557 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
1558 if (ctrl & PCI_MSIX_FLAGS_ENABLE)
1559 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS,
1560 ctrl & ~PCI_MSIX_FLAGS_ENABLE);
1561}