blob: c7709e49f0e497065d528b2fbf753e30f919e3ba [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int pci_msi_enable = 1;
Yijing Wang38737d82014-10-27 10:44:36 +080030int pci_msi_ignore_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Bjorn Helgaas527eee22013-04-17 17:44:48 -060032#define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
33
Jiang Liu8e047ad2014-11-15 22:24:07 +080034#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
Jiang Liu8e047ad2014-11-15 22:24:07 +080035static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
36{
37 struct irq_domain *domain;
38
Christoph Hellwig47feb412017-02-08 18:17:43 +010039 domain = dev_get_msi_domain(&dev->dev);
Marc Zyngier3845d292015-12-04 10:28:14 -060040 if (domain && irq_domain_is_hierarchy(domain))
Christoph Hellwig699c4ce2017-02-08 18:17:44 +010041 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
Jiang Liu8e047ad2014-11-15 22:24:07 +080042
43 return arch_setup_msi_irqs(dev, nvec, type);
44}
45
46static void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
47{
48 struct irq_domain *domain;
49
Christoph Hellwig47feb412017-02-08 18:17:43 +010050 domain = dev_get_msi_domain(&dev->dev);
Marc Zyngier3845d292015-12-04 10:28:14 -060051 if (domain && irq_domain_is_hierarchy(domain))
Christoph Hellwig699c4ce2017-02-08 18:17:44 +010052 msi_domain_free_irqs(domain, &dev->dev);
Jiang Liu8e047ad2014-11-15 22:24:07 +080053 else
54 arch_teardown_msi_irqs(dev);
55}
56#else
57#define pci_msi_setup_msi_irqs arch_setup_msi_irqs
58#define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
59#endif
Bjorn Helgaas527eee22013-04-17 17:44:48 -060060
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010061/* Arch hooks */
62
Thomas Petazzoni4287d822013-08-09 22:27:06 +020063int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
64{
Lorenzo Pieralisi2291ec02015-08-03 22:04:06 -050065 struct msi_controller *chip = dev->bus->msi;
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020066 int err;
67
68 if (!chip || !chip->setup_irq)
69 return -EINVAL;
70
71 err = chip->setup_irq(chip, dev, desc);
72 if (err < 0)
73 return err;
74
75 irq_set_chip_data(desc->irq, chip);
76
77 return 0;
Thomas Petazzoni4287d822013-08-09 22:27:06 +020078}
79
80void __weak arch_teardown_msi_irq(unsigned int irq)
81{
Yijing Wangc2791b82014-11-11 17:45:45 -070082 struct msi_controller *chip = irq_get_chip_data(irq);
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020083
84 if (!chip || !chip->teardown_irq)
85 return;
86
87 chip->teardown_irq(chip, irq);
Thomas Petazzoni4287d822013-08-09 22:27:06 +020088}
89
Thomas Petazzoni4287d822013-08-09 22:27:06 +020090int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010091{
Lucas Stach339e5b42015-09-18 13:58:34 -050092 struct msi_controller *chip = dev->bus->msi;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010093 struct msi_desc *entry;
94 int ret;
95
Lucas Stach339e5b42015-09-18 13:58:34 -050096 if (chip && chip->setup_irqs)
97 return chip->setup_irqs(chip, dev, nvec, type);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040098 /*
99 * If an architecture wants to support multiple MSI, it needs to
100 * override arch_setup_msi_irqs()
101 */
102 if (type == PCI_CAP_ID_MSI && nvec > 1)
103 return 1;
104
Jiang Liu5004e982015-07-09 16:00:41 +0800105 for_each_pci_msi_entry(entry, dev) {
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100106 ret = arch_setup_msi_irq(dev, entry);
Michael Ellermanb5fbf532009-02-11 22:27:02 +1100107 if (ret < 0)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100108 return ret;
Michael Ellermanb5fbf532009-02-11 22:27:02 +1100109 if (ret > 0)
110 return -ENOSPC;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100111 }
112
113 return 0;
114}
115
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200116/*
117 * We have a default implementation available as a separate non-weak
118 * function, as it is used by the Xen x86 PCI code
119 */
Thomas Gleixner1525bf02010-10-06 16:05:35 -0400120void default_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100121{
Jiang Liu63a7b172014-11-06 22:20:32 +0800122 int i;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100123 struct msi_desc *entry;
124
Jiang Liu5004e982015-07-09 16:00:41 +0800125 for_each_pci_msi_entry(entry, dev)
Jiang Liu63a7b172014-11-06 22:20:32 +0800126 if (entry->irq)
127 for (i = 0; i < entry->nvec_used; i++)
128 arch_teardown_msi_irq(entry->irq + i);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100129}
130
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200131void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
132{
133 return default_teardown_msi_irqs(dev);
134}
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500135
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800136static void default_restore_msi_irq(struct pci_dev *dev, int irq)
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500137{
138 struct msi_desc *entry;
139
140 entry = NULL;
141 if (dev->msix_enabled) {
Jiang Liu5004e982015-07-09 16:00:41 +0800142 for_each_pci_msi_entry(entry, dev) {
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500143 if (irq == entry->irq)
144 break;
145 }
146 } else if (dev->msi_enabled) {
147 entry = irq_get_msi_desc(irq);
148 }
149
150 if (entry)
Jiang Liu83a18912014-11-09 23:10:34 +0800151 __pci_write_msi_msg(entry, &entry->msg);
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500152}
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200153
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800154void __weak arch_restore_msi_irqs(struct pci_dev *dev)
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200155{
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800156 return default_restore_msi_irqs(dev);
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200157}
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500158
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500159static inline __attribute_const__ u32 msi_mask(unsigned x)
160{
Matthew Wilcox0b49ec32009-02-08 20:27:47 -0700161 /* Don't shift by >= width of type */
162 if (x >= 5)
163 return 0xffffffff;
164 return (1 << (1 << x)) - 1;
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500165}
166
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600167/*
168 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
169 * mask all MSI interrupts by clearing the MSI enable bit does not work
170 * reliably as devices without an INTx disable bit will then generate a
171 * level IRQ which will never be cleared.
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600172 */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100173u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400175 u32 mask_bits = desc->masked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Yijing Wang38737d82014-10-27 10:44:36 +0800177 if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900178 return 0;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400179
180 mask_bits &= ~mask;
181 mask_bits |= flag;
Jiang Liue39758e2015-07-09 16:00:43 +0800182 pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
183 mask_bits);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900184
185 return mask_bits;
186}
187
188static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
189{
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100190 desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400191}
192
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900193static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
194{
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600195 if (desc->msi_attrib.is_virtual)
196 return NULL;
197
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900198 return desc->mask_base +
199 desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
200}
201
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400202/*
203 * This internal function does not flush PCI writes to the device.
204 * All users must ensure that they read from the device before either
205 * assuming that the device state is up to date, or returning out of this
206 * file. This saves a few milliseconds when initialising devices with lots
207 * of MSI-X interrupts.
208 */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100209u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400210{
211 u32 mask_bits = desc->masked;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600212 void __iomem *desc_addr;
Yijing Wang38737d82014-10-27 10:44:36 +0800213
214 if (pci_msi_ignore_mask)
215 return 0;
Jian-Hong Pane045fa22019-10-08 11:42:39 +0800216
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600217 desc_addr = pci_msix_desc_addr(desc);
218 if (!desc_addr)
219 return 0;
Yijing Wang38737d82014-10-27 10:44:36 +0800220
Sheng Yang8d805282010-11-11 15:46:55 +0800221 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
Jian-Hong Pane045fa22019-10-08 11:42:39 +0800222 if (flag & PCI_MSIX_ENTRY_CTRL_MASKBIT)
Sheng Yang8d805282010-11-11 15:46:55 +0800223 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600224
225 writel(mask_bits, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900226
227 return mask_bits;
228}
229
230static void msix_mask_irq(struct msi_desc *desc, u32 flag)
231{
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100232 desc->masked = __pci_msix_desc_mask_irq(desc, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400233}
234
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200235static void msi_set_mask_bit(struct irq_data *data, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400236{
Jiang Liuc391f262015-06-01 16:05:41 +0800237 struct msi_desc *desc = irq_data_get_msi_desc(data);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400238
239 if (desc->msi_attrib.is_msix) {
240 msix_mask_irq(desc, flag);
241 readl(desc->mask_base); /* Flush write to device */
Matthew Wilcox24d27552009-03-17 08:54:06 -0400242 } else {
Yijing Wanga281b782014-07-08 10:08:55 +0800243 unsigned offset = data->irq - desc->irq;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400244 msi_mask_irq(desc, 1 << offset, flag << offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400246}
247
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100248/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500249 * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100250 * @data: pointer to irqdata associated to that interrupt
251 */
252void pci_msi_mask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400253{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200254 msi_set_mask_bit(data, 1);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400255}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000256EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400257
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100258/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500259 * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100260 * @data: pointer to irqdata associated to that interrupt
261 */
262void pci_msi_unmask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400263{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200264 msi_set_mask_bit(data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000266EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800268void default_restore_msi_irqs(struct pci_dev *dev)
269{
270 struct msi_desc *entry;
271
Jiang Liu5004e982015-07-09 16:00:41 +0800272 for_each_pci_msi_entry(entry, dev)
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800273 default_restore_msi_irq(dev, entry->irq);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800274}
275
Jiang Liu891d4a42014-11-09 23:10:33 +0800276void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700277{
Jiang Liue39758e2015-07-09 16:00:43 +0800278 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
279
280 BUG_ON(dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700281
Ben Hutchings30da5522010-07-23 14:56:28 +0100282 if (entry->msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900283 void __iomem *base = pci_msix_desc_addr(entry);
Ben Hutchings30da5522010-07-23 14:56:28 +0100284
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600285 if (!base) {
286 WARN_ON(1);
287 return;
288 }
289
Ben Hutchings30da5522010-07-23 14:56:28 +0100290 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
291 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
292 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
293 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600294 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100295 u16 data;
296
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600297 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
298 &msg->address_lo);
Ben Hutchings30da5522010-07-23 14:56:28 +0100299 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600300 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
301 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600302 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100303 } else {
304 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600305 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100306 }
307 msg->data = data;
308 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700309}
310
Jiang Liu83a18912014-11-09 23:10:34 +0800311void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800312{
Jiang Liue39758e2015-07-09 16:00:43 +0800313 struct pci_dev *dev = msi_desc_to_pci_dev(entry);
314
Keith Busch01705912017-03-29 22:49:11 -0500315 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100316 /* Don't touch the hardware now */
317 } else if (entry->msi_attrib.is_msix) {
Christoph Hellwig5eb6d662016-07-12 18:20:14 +0900318 void __iomem *base = pci_msix_desc_addr(entry);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400319
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600320 if (!base)
321 goto skip;
322
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900323 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
324 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
325 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400326 } else {
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600327 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400328 u16 msgctl;
329
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600330 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400331 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
332 msgctl |= entry->msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600333 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700334
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600335 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
336 msg->address_lo);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700337 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600338 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
339 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600340 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
341 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700342 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600343 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
344 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700345 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700346 }
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600347
348skip:
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700349 entry->msg = *msg;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600350
351 if (entry->write_msi_msg)
352 entry->write_msi_msg(entry, entry->write_msi_msg_data);
353
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700354}
355
Jiang Liu83a18912014-11-09 23:10:34 +0800356void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800357{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200358 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800359
Jiang Liu83a18912014-11-09 23:10:34 +0800360 __pci_write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800361}
Jiang Liu83a18912014-11-09 23:10:34 +0800362EXPORT_SYMBOL_GPL(pci_write_msi_msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800363
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900364static void free_msi_irqs(struct pci_dev *dev)
365{
Jiang Liu5004e982015-07-09 16:00:41 +0800366 struct list_head *msi_list = dev_to_msi_list(&dev->dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900367 struct msi_desc *entry, *tmp;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800368 struct attribute **msi_attrs;
369 struct device_attribute *dev_attr;
Jiang Liu63a7b172014-11-06 22:20:32 +0800370 int i, count = 0;
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900371
Jiang Liu5004e982015-07-09 16:00:41 +0800372 for_each_pci_msi_entry(entry, dev)
Jiang Liu63a7b172014-11-06 22:20:32 +0800373 if (entry->irq)
374 for (i = 0; i < entry->nvec_used; i++)
375 BUG_ON(irq_has_action(entry->irq + i));
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900376
Jiang Liu8e047ad2014-11-15 22:24:07 +0800377 pci_msi_teardown_msi_irqs(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900378
Jiang Liu5004e982015-07-09 16:00:41 +0800379 list_for_each_entry_safe(entry, tmp, msi_list, list) {
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900380 if (entry->msi_attrib.is_msix) {
Jiang Liu5004e982015-07-09 16:00:41 +0800381 if (list_is_last(&entry->list, msi_list))
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900382 iounmap(entry->mask_base);
383 }
Neil Horman424eb392012-01-03 10:29:54 -0500384
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900385 list_del(&entry->list);
Prarit Bhargava81efbad2017-02-15 11:53:08 -0500386 free_msi_entry(entry);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900387 }
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800388
389 if (dev->msi_irq_groups) {
390 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
391 msi_attrs = dev->msi_irq_groups[0]->attrs;
Alexei Starovoitovb701c0b2014-06-04 15:49:50 -0700392 while (msi_attrs[count]) {
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800393 dev_attr = container_of(msi_attrs[count],
394 struct device_attribute, attr);
395 kfree(dev_attr->attr.name);
396 kfree(dev_attr);
397 ++count;
398 }
399 kfree(msi_attrs);
400 kfree(dev->msi_irq_groups[0]);
401 kfree(dev->msi_irq_groups);
402 dev->msi_irq_groups = NULL;
403 }
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900404}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900405
David Millerba698ad2007-10-25 01:16:30 -0700406static void pci_intx_for_msi(struct pci_dev *dev, int enable)
407{
408 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
409 pci_intx(dev, enable);
410}
411
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100412static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800413{
Shaohua Li41017f02006-02-08 17:11:38 +0800414 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700415 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800416
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800417 if (!dev->msi_enabled)
418 return;
419
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200420 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800421
David Millerba698ad2007-10-25 01:16:30 -0700422 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500423 pci_msi_set_enable(dev, 0);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800424 arch_restore_msi_irqs(dev);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700425
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600426 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Yijing Wang31ea5d42014-06-19 16:30:30 +0800427 msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
428 entry->masked);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700429 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400430 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600431 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100432}
433
434static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800435{
Shaohua Li41017f02006-02-08 17:11:38 +0800436 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800437
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700438 if (!dev->msix_enabled)
439 return;
Jiang Liu5004e982015-07-09 16:00:41 +0800440 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700441
Shaohua Li41017f02006-02-08 17:11:38 +0800442 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700443 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500444 pci_msix_clear_and_set_ctrl(dev, 0,
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800445 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
Shaohua Li41017f02006-02-08 17:11:38 +0800446
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800447 arch_restore_msi_irqs(dev);
Jiang Liu5004e982015-07-09 16:00:41 +0800448 for_each_pci_msi_entry(entry, dev)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400449 msix_mask_irq(entry, entry->masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800450
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500451 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800452}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100453
454void pci_restore_msi_state(struct pci_dev *dev)
455{
456 __pci_restore_msi_state(dev);
457 __pci_restore_msix_state(dev);
458}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600459EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800460
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800461static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
Neil Hormanda8d1c82011-10-06 14:08:18 -0400462 char *buf)
463{
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800464 struct msi_desc *entry;
465 unsigned long irq;
466 int retval;
467
468 retval = kstrtoul(attr->attr.name, 10, &irq);
469 if (retval)
470 return retval;
471
Yijing Wange11ece52014-07-08 10:09:19 +0800472 entry = irq_get_msi_desc(irq);
473 if (entry)
474 return sprintf(buf, "%s\n",
475 entry->msi_attrib.is_msix ? "msix" : "msi");
476
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800477 return -ENODEV;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400478}
479
Neil Hormanda8d1c82011-10-06 14:08:18 -0400480static int populate_msi_sysfs(struct pci_dev *pdev)
481{
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800482 struct attribute **msi_attrs;
483 struct attribute *msi_attr;
484 struct device_attribute *msi_dev_attr;
485 struct attribute_group *msi_irq_group;
486 const struct attribute_group **msi_irq_groups;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400487 struct msi_desc *entry;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800488 int ret = -ENOMEM;
489 int num_msi = 0;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400490 int count = 0;
Romain Bezuta8676062015-09-24 01:31:16 +0200491 int i;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400492
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800493 /* Determine how many msi entries we have */
Jiang Liu5004e982015-07-09 16:00:41 +0800494 for_each_pci_msi_entry(entry, pdev)
Romain Bezuta8676062015-09-24 01:31:16 +0200495 num_msi += entry->nvec_used;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800496 if (!num_msi)
497 return 0;
498
499 /* Dynamically create the MSI attributes for the PCI device */
Kees Cook6396bb22018-06-12 14:03:40 -0700500 msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL);
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800501 if (!msi_attrs)
502 return -ENOMEM;
Jiang Liu5004e982015-07-09 16:00:41 +0800503 for_each_pci_msi_entry(entry, pdev) {
Romain Bezuta8676062015-09-24 01:31:16 +0200504 for (i = 0; i < entry->nvec_used; i++) {
505 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
506 if (!msi_dev_attr)
507 goto error_attrs;
508 msi_attrs[count] = &msi_dev_attr->attr;
Greg Kroah-Hartman86bb4f62014-02-13 10:47:20 -0700509
Romain Bezuta8676062015-09-24 01:31:16 +0200510 sysfs_attr_init(&msi_dev_attr->attr);
511 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
512 entry->irq + i);
513 if (!msi_dev_attr->attr.name)
514 goto error_attrs;
515 msi_dev_attr->attr.mode = S_IRUGO;
516 msi_dev_attr->show = msi_mode_show;
517 ++count;
518 }
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800519 }
520
521 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
522 if (!msi_irq_group)
523 goto error_attrs;
524 msi_irq_group->name = "msi_irqs";
525 msi_irq_group->attrs = msi_attrs;
526
Kees Cook6396bb22018-06-12 14:03:40 -0700527 msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL);
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800528 if (!msi_irq_groups)
529 goto error_irq_group;
530 msi_irq_groups[0] = msi_irq_group;
531
532 ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
533 if (ret)
534 goto error_irq_groups;
535 pdev->msi_irq_groups = msi_irq_groups;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400536
537 return 0;
538
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800539error_irq_groups:
540 kfree(msi_irq_groups);
541error_irq_group:
542 kfree(msi_irq_group);
543error_attrs:
544 count = 0;
545 msi_attr = msi_attrs[count];
546 while (msi_attr) {
547 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
548 kfree(msi_attr->name);
549 kfree(msi_dev_attr);
550 ++count;
551 msi_attr = msi_attrs[count];
Neil Hormanda8d1c82011-10-06 14:08:18 -0400552 }
Greg Kroah-Hartman29237752014-02-13 10:47:35 -0700553 kfree(msi_attrs);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400554 return ret;
555}
556
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200557static struct msi_desc *
Ming Leic66d4bd2019-02-16 18:13:09 +0100558msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd)
Yijing Wangd873b4d2014-07-08 10:07:23 +0800559{
Dou Liyangbec04032018-12-04 23:51:20 +0800560 struct irq_affinity_desc *masks = NULL;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800561 struct msi_desc *entry;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200562 u16 control;
563
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500564 if (affd)
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800565 masks = irq_create_affinity_masks(nvec, affd);
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500566
Yijing Wangd873b4d2014-07-08 10:07:23 +0800567 /* MSI Entry Initialization */
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200568 entry = alloc_msi_entry(&dev->dev, nvec, masks);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800569 if (!entry)
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200570 goto out;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800571
572 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
573
574 entry->msi_attrib.is_msix = 0;
575 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600576 entry->msi_attrib.is_virtual = 0;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800577 entry->msi_attrib.entry_nr = 0;
578 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
579 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Yijing Wangd873b4d2014-07-08 10:07:23 +0800580 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
Jiang Liu63a7b172014-11-06 22:20:32 +0800581 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
Yijing Wangd873b4d2014-07-08 10:07:23 +0800582
583 if (control & PCI_MSI_FLAGS_64BIT)
584 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
585 else
586 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
587
588 /* Save the initial mask status */
589 if (entry->msi_attrib.maskbit)
590 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
591
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200592out:
593 kfree(masks);
Yijing Wangd873b4d2014-07-08 10:07:23 +0800594 return entry;
595}
596
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000597static int msi_verify_entries(struct pci_dev *dev)
598{
599 struct msi_desc *entry;
600
Jiang Liu5004e982015-07-09 16:00:41 +0800601 for_each_pci_msi_entry(entry, dev) {
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000602 if (!dev->no_64bit_msi || !entry->msg.address_hi)
603 continue;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600604 pci_err(dev, "Device has broken 64-bit MSI but arch"
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000605 " tried to assign one above 4G\n");
606 return -EIO;
607 }
608 return 0;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
612 * msi_capability_init - configure device's MSI capability structure
613 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400614 * @nvec: number of interrupts to allocate
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500615 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400617 * Setup the MSI capability structure of the device with the requested
618 * number of interrupts. A return value of zero indicates the successful
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500619 * setup of an entry with the new MSI IRQ. A negative return value indicates
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400620 * an error, and a positive return value indicates the number of interrupts
621 * which could have been allocated.
622 */
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800623static int msi_capability_init(struct pci_dev *dev, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100624 struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000627 int ret;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400628 unsigned mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500630 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600631
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800632 entry = msi_setup_entry(dev, nvec, affd);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700633 if (!entry)
634 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700635
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500636 /* All MSIs are unmasked by default; mask them all */
Yijing Wang31ea5d42014-06-19 16:30:30 +0800637 mask = msi_mask(entry->msi_attrib.multi_cap);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400638 msi_mask_irq(entry, mask, mask);
639
Jiang Liu5004e982015-07-09 16:00:41 +0800640 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
Michael Ellerman9c831332007-04-18 19:39:21 +1000641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Configure MSI capability structure */
Jiang Liu8e047ad2014-11-15 22:24:07 +0800643 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000644 if (ret) {
Hidetoshi Seto7ba19302009-06-23 17:39:27 +0900645 msi_mask_irq(entry, mask, ~mask);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900646 free_msi_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000647 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500648 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700649
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000650 ret = msi_verify_entries(dev);
651 if (ret) {
652 msi_mask_irq(entry, mask, ~mask);
653 free_msi_irqs(dev);
654 return ret;
655 }
656
Neil Hormanda8d1c82011-10-06 14:08:18 -0400657 ret = populate_msi_sysfs(dev);
658 if (ret) {
659 msi_mask_irq(entry, mask, ~mask);
660 free_msi_irqs(dev);
661 return ret;
662 }
663
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500664 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700665 pci_intx_for_msi(dev, 0);
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500666 pci_msi_set_enable(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800667 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Jiang Liu5f226992015-07-30 14:00:08 -0500669 pcibios_free_irq(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000670 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 0;
672}
673
Gavin Shan520fe9d2013-04-04 16:54:33 +0000674static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900675{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900676 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900677 u32 table_offset;
Yijing Wang6a878e52015-01-28 09:52:17 +0800678 unsigned long flags;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900679 u8 bir;
680
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600681 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
682 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600683 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
Yijing Wang6a878e52015-01-28 09:52:17 +0800684 flags = pci_resource_flags(dev, bir);
685 if (!flags || (flags & IORESOURCE_UNSET))
686 return NULL;
687
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600688 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900689 phys_addr = pci_resource_start(dev, bir) + table_offset;
690
691 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
692}
693
Gavin Shan520fe9d2013-04-04 16:54:33 +0000694static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200695 struct msix_entry *entries, int nvec,
Ming Leic66d4bd2019-02-16 18:13:09 +0100696 struct irq_affinity *affd)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900697{
Dou Liyangbec04032018-12-04 23:51:20 +0800698 struct irq_affinity_desc *curmsk, *masks = NULL;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900699 struct msi_desc *entry;
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200700 int ret, i;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600701 int vec_count = pci_msix_vec_count(dev);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900702
Christoph Hellwig8e1101d2017-08-25 18:58:42 -0500703 if (affd)
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800704 masks = irq_create_affinity_masks(nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +0900705
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200706 for (i = 0, curmsk = masks; i < nvec; i++) {
707 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900708 if (!entry) {
709 if (!i)
710 iounmap(base);
711 else
712 free_msi_irqs(dev);
713 /* No enough memory. Don't try again */
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200714 ret = -ENOMEM;
715 goto out;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900716 }
717
718 entry->msi_attrib.is_msix = 1;
719 entry->msi_attrib.is_64 = 1;
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900720 if (entries)
721 entry->msi_attrib.entry_nr = entries[i].entry;
722 else
723 entry->msi_attrib.entry_nr = i;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600724
725 entry->msi_attrib.is_virtual =
726 entry->msi_attrib.entry_nr >= vec_count;
727
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900728 entry->msi_attrib.default_irq = dev->irq;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900729 entry->mask_base = base;
730
Jiang Liu5004e982015-07-09 16:00:41 +0800731 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200732 if (masks)
733 curmsk++;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900734 }
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200735 ret = 0;
736out:
737 kfree(masks);
Christophe JAILLET3adfb572017-01-27 16:14:53 +0100738 return ret;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900739}
740
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900741static void msix_program_entries(struct pci_dev *dev,
Gavin Shan520fe9d2013-04-04 16:54:33 +0000742 struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900743{
744 struct msi_desc *entry;
745 int i = 0;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600746 void __iomem *desc_addr;
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900747
Jiang Liu5004e982015-07-09 16:00:41 +0800748 for_each_pci_msi_entry(entry, dev) {
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900749 if (entries)
750 entries[i++].vector = entry->irq;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600751
752 desc_addr = pci_msix_desc_addr(entry);
753 if (desc_addr)
754 entry->masked = readl(desc_addr +
755 PCI_MSIX_ENTRY_VECTOR_CTRL);
756 else
757 entry->masked = 0;
758
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900759 msix_mask_irq(entry, 1);
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900760 }
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/**
764 * msix_capability_init - configure device's MSI-X capability
765 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700766 * @entries: pointer to an array of struct msix_entry entries
767 * @nvec: number of @entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500768 * @affd: Optional pointer to enable automatic affinity assignment
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600770 * Setup the MSI-X capability structure of device function with a
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500771 * single MSI-X IRQ. A return of zero indicates the successful setup of
772 * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 **/
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200774static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
Ming Leic66d4bd2019-02-16 18:13:09 +0100775 int nvec, struct irq_affinity *affd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Gavin Shan520fe9d2013-04-04 16:54:33 +0000777 int ret;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900778 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 void __iomem *base;
780
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700781 /* Ensure MSI-X is disabled while it is set up */
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500782 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700783
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800784 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* Request & Map MSI-X table region */
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600786 base = msix_map_region(dev, msix_table_size(control));
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900787 if (!base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -ENOMEM;
789
Christoph Hellwig61e1c592016-11-08 17:15:04 -0800790 ret = msix_setup_entries(dev, base, entries, nvec, affd);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900791 if (ret)
792 return ret;
Michael Ellerman9c831332007-04-18 19:39:21 +1000793
Jiang Liu8e047ad2014-11-15 22:24:07 +0800794 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900795 if (ret)
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100796 goto out_avail;
Michael Ellerman9c831332007-04-18 19:39:21 +1000797
Benjamin Herrenschmidtf144d142014-10-03 15:13:24 +1000798 /* Check if all MSI entries honor device restrictions */
799 ret = msi_verify_entries(dev);
800 if (ret)
801 goto out_free;
802
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700803 /*
804 * Some devices require MSI-X to be enabled before we can touch the
805 * MSI-X registers. We need to mask all the vectors to prevent
806 * interrupts coming in before they're fully set up.
807 */
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500808 pci_msix_clear_and_set_ctrl(dev, 0,
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800809 PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700810
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900811 msix_program_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700812
Neil Hormanda8d1c82011-10-06 14:08:18 -0400813 ret = populate_msi_sysfs(dev);
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100814 if (ret)
815 goto out_free;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400816
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700817 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700818 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800819 dev->msix_enabled = 1;
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500820 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600821
Jiang Liu5f226992015-07-30 14:00:08 -0500822 pcibios_free_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900824
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100825out_avail:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900826 if (ret < 0) {
827 /*
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500828 * If we had some success, report the number of IRQs
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900829 * we succeeded in setting up.
830 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900831 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900832 int avail = 0;
833
Jiang Liu5004e982015-07-09 16:00:41 +0800834 for_each_pci_msi_entry(entry, dev) {
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900835 if (entry->irq != 0)
836 avail++;
837 }
838 if (avail != 0)
839 ret = avail;
840 }
841
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100842out_free:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900843 free_msi_irqs(dev);
844
845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848/**
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600849 * pci_msi_supported - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400850 * @dev: pointer to the pci_dev data structure of MSI device function
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500851 * @nvec: how many MSIs have been requested?
Brice Goglin24334a12006-08-31 01:55:07 -0400852 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700853 * Look at global flags, the device itself, and its parent buses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000854 * to determine if MSI/-X are supported for the device. If MSI/-X is
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600855 * supported return 1, else return 0.
Brice Goglin24334a12006-08-31 01:55:07 -0400856 **/
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600857static int pci_msi_supported(struct pci_dev *dev, int nvec)
Brice Goglin24334a12006-08-31 01:55:07 -0400858{
859 struct pci_bus *bus;
860
Brice Goglin0306ebf2006-10-05 10:24:31 +0200861 /* MSI must be globally enabled and supported by the device */
Alexander Gordeev27e20602014-09-23 14:25:11 -0600862 if (!pci_msi_enable)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600863 return 0;
Alexander Gordeev27e20602014-09-23 14:25:11 -0600864
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500865 if (!dev || dev->no_msi)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600866 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400867
Michael Ellerman314e77b2007-04-05 17:19:12 +1000868 /*
869 * You can't ask to have 0 or less MSIs configured.
870 * a) it's stupid ..
871 * b) the list manipulation code assumes nvec >= 1.
872 */
873 if (nvec < 1)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600874 return 0;
Michael Ellerman314e77b2007-04-05 17:19:12 +1000875
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900876 /*
877 * Any bridge which does NOT route MSI transactions from its
878 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200879 * the secondary pci_bus.
880 * We expect only arch-specific PCI host bus controller driver
881 * or quirks for specific PCI bridges to be setting NO_MSI.
882 */
Brice Goglin24334a12006-08-31 01:55:07 -0400883 for (bus = dev->bus; bus; bus = bus->parent)
884 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600885 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400886
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600887 return 1;
Brice Goglin24334a12006-08-31 01:55:07 -0400888}
889
890/**
Alexander Gordeevd1ac1d22013-12-30 08:28:13 +0100891 * pci_msi_vec_count - Return the number of MSI vectors a device can send
892 * @dev: device to report about
893 *
894 * This function returns the number of MSI vectors a device requested via
895 * Multiple Message Capable register. It returns a negative errno if the
896 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
897 * and returns a power of two, up to a maximum of 2^5 (32), according to the
898 * MSI specification.
899 **/
900int pci_msi_vec_count(struct pci_dev *dev)
901{
902 int ret;
903 u16 msgctl;
904
905 if (!dev->msi_cap)
906 return -EINVAL;
907
908 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
909 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
910
911 return ret;
912}
913EXPORT_SYMBOL(pci_msi_vec_count);
914
Bjorn Helgaas688769f2017-03-09 15:45:14 -0600915static void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400917 struct msi_desc *desc;
918 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100920 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700921 return;
922
Jiang Liu5004e982015-07-09 16:00:41 +0800923 BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
Jiang Liu4a7cc832015-07-09 16:00:44 +0800924 desc = first_pci_msi_entry(dev);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600925
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -0500926 pci_msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700927 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800928 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700929
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900930 /* Return the device with MSI unmasked as initial states */
Yijing Wang31ea5d42014-06-19 16:30:30 +0800931 mask = msi_mask(desc->msi_attrib.multi_cap);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900932 /* Keep cached state to be restored */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100933 __pci_msi_desc_mask_irq(desc, mask, ~mask);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100934
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500935 /* Restore dev->irq to its default pin-assertion IRQ */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400936 dev->irq = desc->msi_attrib.default_irq;
Jiang Liu5f226992015-07-30 14:00:08 -0500937 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -0700938}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400939
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900940void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700941{
Yinghai Lud52877c2008-04-23 14:58:09 -0700942 if (!pci_msi_enable || !dev || !dev->msi_enabled)
943 return;
944
945 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900946 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100948EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/**
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100951 * pci_msix_vec_count - return the number of device's MSI-X table entries
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100952 * @dev: pointer to the pci_dev data structure of MSI-X device function
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100953 * This function returns the number of device's MSI-X table entries and
954 * therefore the number of MSI-X vectors device is capable of sending.
955 * It returns a negative errno if the device is not capable of sending MSI-X
956 * interrupts.
957 **/
958int pci_msix_vec_count(struct pci_dev *dev)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100959{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100960 u16 control;
961
Gavin Shan520fe9d2013-04-04 16:54:33 +0000962 if (!dev->msix_cap)
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100963 return -EINVAL;
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100964
Bjorn Helgaasf84ecd282013-04-17 17:38:32 -0600965 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600966 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100967}
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100968EXPORT_SYMBOL(pci_msix_vec_count);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100969
Thomas Gleixnere75eafb2016-09-14 16:18:49 +0200970static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600971 int nvec, struct irq_affinity *affd, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Bjorn Helgaas5ec09402014-09-23 14:38:28 -0600973 int nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700974 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -0500976 if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600977 return -EINVAL;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000978
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100979 nr_entries = pci_msix_vec_count(dev);
980 if (nr_entries < 0)
981 return nr_entries;
Logan Gunthorped7cc6092019-05-23 16:30:51 -0600982 if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL))
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300983 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Christoph Hellwig3ac020e2016-07-12 18:20:16 +0900985 if (entries) {
986 /* Check for any invalid entries */
987 for (i = 0; i < nvec; i++) {
988 if (entries[i].entry >= nr_entries)
989 return -EINVAL; /* invalid entry */
990 for (j = i + 1; j < nvec; j++) {
991 if (entries[i].entry == entries[j].entry)
992 return -EINVAL; /* duplicate entry */
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700996
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500997 /* Check whether driver already requested for MSI IRQ */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900998 if (dev->msi_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600999 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -EINVAL;
1001 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001002 return msix_capability_init(dev, entries, nvec, affd);
Thomas Gleixnere75eafb2016-09-14 16:18:49 +02001003}
1004
Bjorn Helgaas688769f2017-03-09 15:45:14 -06001005static void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +11001006{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +09001007 struct msi_desc *entry;
1008
Michael Ellerman128bc5f2007-03-22 21:51:39 +11001009 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -07001010 return;
1011
Keith Busch01705912017-03-29 22:49:11 -05001012 if (pci_dev_is_disconnected(dev)) {
1013 dev->msix_enabled = 0;
1014 return;
1015 }
1016
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +09001017 /* Return the device with MSI-X masked as initial states */
Jiang Liu5004e982015-07-09 16:00:41 +08001018 for_each_pci_msi_entry(entry, dev) {
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +09001019 /* Keep cached states to be restored */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +01001020 __pci_msix_desc_mask_irq(entry, 1);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +09001021 }
1022
Michael S. Tsirkin61b64ab2015-05-07 09:52:21 -05001023 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
David Millerba698ad2007-10-25 01:16:30 -07001024 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -08001025 dev->msix_enabled = 0;
Jiang Liu5f226992015-07-30 14:00:08 -05001026 pcibios_alloc_irq(dev);
Yinghai Lud52877c2008-04-23 14:58:09 -07001027}
Hidetoshi Setoc9018512009-08-06 11:31:27 +09001028
Hidetoshi Seto500559a2009-08-10 10:14:15 +09001029void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -07001030{
1031 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1032 return;
1033
1034 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +09001035 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
Michael Ellerman4cc086f2007-03-22 21:51:34 +11001037EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001039void pci_no_msi(void)
1040{
1041 pci_msi_enable = 0;
1042}
Michael Ellermanc9953a72007-04-05 17:19:08 +10001043
Andrew Patterson07ae95f2008-11-10 15:31:05 -07001044/**
1045 * pci_msi_enabled - is MSI enabled?
1046 *
1047 * Returns true if MSI has not been disabled by the command-line option
1048 * pci=nomsi.
1049 **/
1050int pci_msi_enabled(void)
1051{
1052 return pci_msi_enable;
1053}
1054EXPORT_SYMBOL(pci_msi_enabled);
1055
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001056static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
Ming Leic66d4bd2019-02-16 18:13:09 +01001057 struct irq_affinity *affd)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001058{
Alexander Gordeev034cd972014-04-14 15:28:35 +02001059 int nvec;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001060 int rc;
1061
Bjorn Helgaas901c4dd2019-10-14 16:17:05 -05001062 if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -06001063 return -EINVAL;
Alexander Gordeev034cd972014-04-14 15:28:35 +02001064
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001065 /* Check whether driver already requested MSI-X IRQs */
Alexander Gordeev034cd972014-04-14 15:28:35 +02001066 if (dev->msix_enabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001067 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
Alexander Gordeev034cd972014-04-14 15:28:35 +02001068 return -EINVAL;
1069 }
1070
Alexander Gordeev302a2522013-12-30 08:28:16 +01001071 if (maxvec < minvec)
1072 return -ERANGE;
1073
Tonghao Zhang4c1ef722018-09-24 07:00:41 -07001074 if (WARN_ON_ONCE(dev->msi_enabled))
1075 return -EINVAL;
1076
Alexander Gordeev034cd972014-04-14 15:28:35 +02001077 nvec = pci_msi_vec_count(dev);
1078 if (nvec < 0)
1079 return nvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001080 if (nvec < minvec)
Dennis Chen948b7622016-12-01 10:15:04 +08001081 return -ENOSPC;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001082
1083 if (nvec > maxvec)
Alexander Gordeev034cd972014-04-14 15:28:35 +02001084 nvec = maxvec;
1085
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001086 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001087 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -07001088 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001089 if (nvec < minvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001090 return -ENOSPC;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001091 }
Alexander Gordeev302a2522013-12-30 08:28:16 +01001092
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001093 rc = msi_capability_init(dev, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001094 if (rc == 0)
1095 return nvec;
1096
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001097 if (rc < 0)
1098 return rc;
1099 if (rc < minvec)
1100 return -ENOSPC;
1101
1102 nvec = rc;
1103 }
1104}
1105
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001106/* deprecated, don't use */
1107int pci_enable_msi(struct pci_dev *dev)
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001108{
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001109 int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1110 if (rc < 0)
1111 return rc;
1112 return 0;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001113}
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001114EXPORT_SYMBOL(pci_enable_msi);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001115
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001116static int __pci_enable_msix_range(struct pci_dev *dev,
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001117 struct msix_entry *entries, int minvec,
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001118 int maxvec, struct irq_affinity *affd,
1119 int flags)
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001120{
Thomas Gleixnere75eafb2016-09-14 16:18:49 +02001121 int rc, nvec = maxvec;
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001122
1123 if (maxvec < minvec)
1124 return -ERANGE;
1125
Tonghao Zhang4c1ef722018-09-24 07:00:41 -07001126 if (WARN_ON_ONCE(dev->msix_enabled))
1127 return -EINVAL;
1128
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001129 for (;;) {
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001130 if (affd) {
Michael Hernandez6f9a22b2017-05-18 10:47:47 -07001131 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001132 if (nvec < minvec)
1133 return -ENOSPC;
1134 }
1135
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001136 rc = __pci_enable_msix(dev, entries, nvec, affd, flags);
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001137 if (rc == 0)
1138 return nvec;
1139
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001140 if (rc < 0)
1141 return rc;
1142 if (rc < minvec)
1143 return -ENOSPC;
1144
1145 nvec = rc;
1146 }
1147}
1148
Alexander Gordeev302a2522013-12-30 08:28:16 +01001149/**
1150 * pci_enable_msix_range - configure device's MSI-X capability structure
1151 * @dev: pointer to the pci_dev data structure of MSI-X device function
1152 * @entries: pointer to an array of MSI-X entries
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001153 * @minvec: minimum number of MSI-X IRQs requested
1154 * @maxvec: maximum number of MSI-X IRQs requested
Alexander Gordeev302a2522013-12-30 08:28:16 +01001155 *
1156 * Setup the MSI-X capability structure of device function with a maximum
1157 * possible number of interrupts in the range between @minvec and @maxvec
1158 * upon its software driver call to request for MSI-X mode enabled on its
1159 * hardware device function. It returns a negative errno if an error occurs.
1160 * If it succeeds, it returns the actual number of interrupts allocated and
1161 * indicates the successful configuration of MSI-X capability structure
1162 * with new allocated MSI-X interrupts.
1163 **/
1164int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
Christoph Hellwig4ef33682016-07-12 18:20:18 +09001165 int minvec, int maxvec)
Alexander Gordeev302a2522013-12-30 08:28:16 +01001166{
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001167 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001168}
1169EXPORT_SYMBOL(pci_enable_msix_range);
Jiang Liu3878eae2014-11-11 21:02:18 +08001170
Christoph Hellwigaff17162016-07-12 18:20:17 +09001171/**
Christoph Hellwig402723a2016-11-08 17:15:05 -08001172 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
Christoph Hellwigaff17162016-07-12 18:20:17 +09001173 * @dev: PCI device to operate on
1174 * @min_vecs: minimum number of vectors required (must be >= 1)
1175 * @max_vecs: maximum (desired) number of vectors
1176 * @flags: flags or quirks for the allocation
Christoph Hellwig402723a2016-11-08 17:15:05 -08001177 * @affd: optional description of the affinity requirements
Christoph Hellwigaff17162016-07-12 18:20:17 +09001178 *
1179 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1180 * vectors if available, and fall back to a single legacy vector
1181 * if neither is available. Return the number of vectors allocated,
1182 * (which might be smaller than @max_vecs) if successful, or a negative
1183 * error code on error. If less than @min_vecs interrupt vectors are
1184 * available for @dev the function will fail with -ENOSPC.
1185 *
1186 * To get the Linux IRQ number used for a vector that can be passed to
1187 * request_irq() use the pci_irq_vector() helper.
1188 */
Christoph Hellwig402723a2016-11-08 17:15:05 -08001189int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1190 unsigned int max_vecs, unsigned int flags,
Ming Leic66d4bd2019-02-16 18:13:09 +01001191 struct irq_affinity *affd)
Christoph Hellwigaff17162016-07-12 18:20:17 +09001192{
Ming Leic66d4bd2019-02-16 18:13:09 +01001193 struct irq_affinity msi_default_affd = {0};
Ming Lei77f88ab2019-01-15 17:31:29 -06001194 int msix_vecs = -ENOSPC;
1195 int msi_vecs = -ENOSPC;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001196
Christoph Hellwig402723a2016-11-08 17:15:05 -08001197 if (flags & PCI_IRQ_AFFINITY) {
1198 if (!affd)
1199 affd = &msi_default_affd;
1200 } else {
1201 if (WARN_ON(affd))
1202 affd = NULL;
1203 }
Christoph Hellwig61e1c592016-11-08 17:15:04 -08001204
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001205 if (flags & PCI_IRQ_MSIX) {
Ming Lei77f88ab2019-01-15 17:31:29 -06001206 msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs,
Logan Gunthorped7cc6092019-05-23 16:30:51 -06001207 max_vecs, affd, flags);
Ming Lei77f88ab2019-01-15 17:31:29 -06001208 if (msix_vecs > 0)
1209 return msix_vecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001210 }
1211
Christoph Hellwig4fe0d152016-08-11 07:11:04 -07001212 if (flags & PCI_IRQ_MSI) {
Ming Lei77f88ab2019-01-15 17:31:29 -06001213 msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs,
1214 affd);
1215 if (msi_vecs > 0)
1216 return msi_vecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001217 }
1218
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001219 /* use legacy IRQ if allowed */
Christoph Hellwig862290f2017-02-01 14:41:42 +01001220 if (flags & PCI_IRQ_LEGACY) {
1221 if (min_vecs == 1 && dev->irq) {
Ming Leic66d4bd2019-02-16 18:13:09 +01001222 /*
1223 * Invoke the affinity spreading logic to ensure that
1224 * the device driver can adjust queue configuration
1225 * for the single interrupt case.
1226 */
1227 if (affd)
1228 irq_create_affinity_masks(1, affd);
Christoph Hellwig862290f2017-02-01 14:41:42 +01001229 pci_intx(dev, 1);
1230 return 1;
1231 }
Christoph Hellwig5d0bdf22016-08-11 07:11:05 -07001232 }
1233
Ming Lei77f88ab2019-01-15 17:31:29 -06001234 if (msix_vecs == -ENOSPC)
1235 return -ENOSPC;
1236 return msi_vecs;
Christoph Hellwigaff17162016-07-12 18:20:17 +09001237}
Christoph Hellwig402723a2016-11-08 17:15:05 -08001238EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
Christoph Hellwigaff17162016-07-12 18:20:17 +09001239
1240/**
1241 * pci_free_irq_vectors - free previously allocated IRQs for a device
1242 * @dev: PCI device to operate on
1243 *
1244 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1245 */
1246void pci_free_irq_vectors(struct pci_dev *dev)
1247{
1248 pci_disable_msix(dev);
1249 pci_disable_msi(dev);
1250}
1251EXPORT_SYMBOL(pci_free_irq_vectors);
1252
1253/**
1254 * pci_irq_vector - return Linux IRQ number of a device vector
1255 * @dev: PCI device to operate on
1256 * @nr: device-relative interrupt vector index (0-based).
1257 */
1258int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1259{
1260 if (dev->msix_enabled) {
1261 struct msi_desc *entry;
1262 int i = 0;
1263
1264 for_each_pci_msi_entry(entry, dev) {
1265 if (i == nr)
1266 return entry->irq;
1267 i++;
1268 }
1269 WARN_ON_ONCE(1);
1270 return -EINVAL;
1271 }
1272
1273 if (dev->msi_enabled) {
1274 struct msi_desc *entry = first_pci_msi_entry(dev);
1275
1276 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1277 return -EINVAL;
1278 } else {
1279 if (WARN_ON_ONCE(nr > 0))
1280 return -EINVAL;
1281 }
1282
1283 return dev->irq + nr;
1284}
1285EXPORT_SYMBOL(pci_irq_vector);
1286
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001287/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001288 * pci_irq_get_affinity - return the affinity of a particular MSI vector
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001289 * @dev: PCI device to operate on
1290 * @nr: device-relative interrupt vector index (0-based).
1291 */
1292const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1293{
1294 if (dev->msix_enabled) {
1295 struct msi_desc *entry;
1296 int i = 0;
1297
1298 for_each_pci_msi_entry(entry, dev) {
1299 if (i == nr)
Dou Liyangbec04032018-12-04 23:51:20 +08001300 return &entry->affinity->mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001301 i++;
1302 }
1303 WARN_ON_ONCE(1);
1304 return NULL;
1305 } else if (dev->msi_enabled) {
1306 struct msi_desc *entry = first_pci_msi_entry(dev);
1307
Jan Beulichd1d111e2016-11-08 00:43:54 -07001308 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1309 nr >= entry->nvec_used))
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001310 return NULL;
1311
Dou Liyangbec04032018-12-04 23:51:20 +08001312 return &entry->affinity[nr].mask;
Thomas Gleixneree8d41e2016-09-14 16:18:51 +02001313 } else {
1314 return cpu_possible_mask;
1315 }
1316}
1317EXPORT_SYMBOL(pci_irq_get_affinity);
1318
Jiang Liu25a98bd2015-07-09 16:00:45 +08001319struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1320{
1321 return to_pci_dev(desc->dev);
1322}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001323EXPORT_SYMBOL(msi_desc_to_pci_dev);
Jiang Liu25a98bd2015-07-09 16:00:45 +08001324
Jiang Liuc179c9b2015-07-09 16:00:36 +08001325void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1326{
1327 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1328
1329 return dev->bus->sysdata;
1330}
1331EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1332
Jiang Liu3878eae2014-11-11 21:02:18 +08001333#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1334/**
1335 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1336 * @irq_data: Pointer to interrupt data of the MSI interrupt
1337 * @msg: Pointer to the message
1338 */
1339void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1340{
Jiang Liu507a8832015-06-01 16:05:42 +08001341 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
Jiang Liu3878eae2014-11-11 21:02:18 +08001342
1343 /*
1344 * For MSI-X desc->irq is always equal to irq_data->irq. For
1345 * MSI only the first interrupt of MULTI MSI passes the test.
1346 */
1347 if (desc->irq == irq_data->irq)
1348 __pci_write_msi_msg(desc, msg);
1349}
1350
1351/**
1352 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1353 * @dev: Pointer to the PCI device
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001354 * @desc: Pointer to the MSI descriptor
Jiang Liu3878eae2014-11-11 21:02:18 +08001355 *
1356 * The ID number is only used within the irqdomain.
1357 */
1358irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
1359 struct msi_desc *desc)
1360{
1361 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001362 pci_dev_id(dev) << 11 |
Jiang Liu3878eae2014-11-11 21:02:18 +08001363 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1364}
1365
1366static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1367{
1368 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1369}
1370
1371/**
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -05001372 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
1373 * for @dev
Jiang Liu3878eae2014-11-11 21:02:18 +08001374 * @domain: The interrupt domain to check
1375 * @info: The domain info for verification
1376 * @dev: The device to check
1377 *
1378 * Returns:
1379 * 0 if the functionality is supported
1380 * 1 if Multi MSI is requested, but the domain does not support it
1381 * -ENOTSUPP otherwise
1382 */
1383int pci_msi_domain_check_cap(struct irq_domain *domain,
1384 struct msi_domain_info *info, struct device *dev)
1385{
1386 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1387
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001388 /* Special handling to support __pci_enable_msi_range() */
Jiang Liu3878eae2014-11-11 21:02:18 +08001389 if (pci_msi_desc_is_multi_msi(desc) &&
1390 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1391 return 1;
1392 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1393 return -ENOTSUPP;
1394
1395 return 0;
1396}
1397
1398static int pci_msi_domain_handle_error(struct irq_domain *domain,
1399 struct msi_desc *desc, int error)
1400{
Christoph Hellwig4fe03952017-01-09 21:37:40 +01001401 /* Special handling to support __pci_enable_msi_range() */
Jiang Liu3878eae2014-11-11 21:02:18 +08001402 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1403 return 1;
1404
1405 return error;
1406}
1407
1408#ifdef GENERIC_MSI_DOMAIN_OPS
1409static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1410 struct msi_desc *desc)
1411{
1412 arg->desc = desc;
1413 arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
1414 desc);
1415}
1416#else
1417#define pci_msi_domain_set_desc NULL
1418#endif
1419
1420static struct msi_domain_ops pci_msi_domain_ops_default = {
1421 .set_desc = pci_msi_domain_set_desc,
1422 .msi_check = pci_msi_domain_check_cap,
1423 .handle_error = pci_msi_domain_handle_error,
1424};
1425
1426static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1427{
1428 struct msi_domain_ops *ops = info->ops;
1429
1430 if (ops == NULL) {
1431 info->ops = &pci_msi_domain_ops_default;
1432 } else {
1433 if (ops->set_desc == NULL)
1434 ops->set_desc = pci_msi_domain_set_desc;
1435 if (ops->msi_check == NULL)
1436 ops->msi_check = pci_msi_domain_check_cap;
1437 if (ops->handle_error == NULL)
1438 ops->handle_error = pci_msi_domain_handle_error;
1439 }
1440}
1441
1442static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1443{
1444 struct irq_chip *chip = info->chip;
1445
1446 BUG_ON(!chip);
1447 if (!chip->irq_write_msi_msg)
1448 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
Marc Zyngier0701c532015-10-13 19:14:45 +01001449 if (!chip->irq_mask)
1450 chip->irq_mask = pci_msi_mask_irq;
1451 if (!chip->irq_unmask)
1452 chip->irq_unmask = pci_msi_unmask_irq;
Jiang Liu3878eae2014-11-11 21:02:18 +08001453}
1454
1455/**
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001456 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1457 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liu3878eae2014-11-11 21:02:18 +08001458 * @info: MSI domain info
1459 * @parent: Parent irq domain
1460 *
1461 * Updates the domain and chip ops and creates a MSI interrupt domain.
1462 *
1463 * Returns:
1464 * A domain pointer or NULL in case of failure.
1465 */
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001466struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
Jiang Liu3878eae2014-11-11 21:02:18 +08001467 struct msi_domain_info *info,
1468 struct irq_domain *parent)
1469{
Marc Zyngier03808392015-07-28 14:46:09 +01001470 struct irq_domain *domain;
1471
Marc Zyngier6988e0e2018-05-08 13:14:31 +01001472 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1473 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1474
Jiang Liu3878eae2014-11-11 21:02:18 +08001475 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1476 pci_msi_domain_update_dom_ops(info);
1477 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1478 pci_msi_domain_update_chip_ops(info);
1479
Marc Zyngierf3b09462016-07-13 17:18:33 +01001480 info->flags |= MSI_FLAG_ACTIVATE_EARLY;
Thomas Gleixner25e960e2017-10-17 09:54:58 +02001481 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1482 info->flags |= MSI_FLAG_MUST_REACTIVATE;
Marc Zyngierf3b09462016-07-13 17:18:33 +01001483
Heiner Kallweit923aa4c2018-08-05 22:31:03 +02001484 /* PCI-MSI is oneshot-safe */
1485 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1486
Marc Zyngierbe5436c2015-10-13 12:51:44 +01001487 domain = msi_create_irq_domain(fwnode, info, parent);
Marc Zyngier03808392015-07-28 14:46:09 +01001488 if (!domain)
1489 return NULL;
1490
Marc Zyngier96f0d932017-06-22 11:42:50 +01001491 irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
Marc Zyngier03808392015-07-28 14:46:09 +01001492 return domain;
Jiang Liu3878eae2014-11-11 21:02:18 +08001493}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001494EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
Jiang Liu3878eae2014-11-11 21:02:18 +08001495
Robin Murphy235b2c72017-08-01 18:59:08 +01001496/*
1497 * Users of the generic MSI infrastructure expect a device to have a single ID,
1498 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1499 * DMA phantom functions tend to still emit MSIs from the real function number,
1500 * so we ignore those and only consider topological aliases where either the
1501 * alias device or RID appears on a different bus number. We also make the
1502 * reasonable assumption that bridges are walked in an upstream direction (so
1503 * the last one seen wins), and the much braver assumption that the most likely
1504 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1505 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1506 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1507 * for taking ownership all we can really do is close our eyes and hope...
1508 */
David Daneyb6eec9b2015-10-08 15:10:49 -07001509static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1510{
1511 u32 *pa = data;
Robin Murphy235b2c72017-08-01 18:59:08 +01001512 u8 bus = PCI_BUS_NUM(*pa);
David Daneyb6eec9b2015-10-08 15:10:49 -07001513
Robin Murphy235b2c72017-08-01 18:59:08 +01001514 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1515 *pa = alias;
1516
David Daneyb6eec9b2015-10-08 15:10:49 -07001517 return 0;
1518}
Robin Murphy235b2c72017-08-01 18:59:08 +01001519
David Daneyb6eec9b2015-10-08 15:10:49 -07001520/**
1521 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1522 * @domain: The interrupt domain
1523 * @pdev: The PCI device.
1524 *
1525 * The RID for a device is formed from the alias, with a firmware
1526 * supplied mapping applied
1527 *
1528 * Returns: The RID.
1529 */
1530u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1531{
1532 struct device_node *of_node;
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001533 u32 rid = pci_dev_id(pdev);
David Daneyb6eec9b2015-10-08 15:10:49 -07001534
1535 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1536
1537 of_node = irq_domain_get_of_node(domain);
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001538 rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
1539 iort_msi_map_rid(&pdev->dev, rid);
David Daneyb6eec9b2015-10-08 15:10:49 -07001540
1541 return rid;
1542}
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001543
1544/**
1545 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1546 * @pdev: The PCI device
1547 *
1548 * Use the firmware data to find a device-specific MSI domain
Robin Murphy235b2c72017-08-01 18:59:08 +01001549 * (i.e. not one that is set as a default).
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001550 *
Robin Murphy235b2c72017-08-01 18:59:08 +01001551 * Returns: The corresponding MSI domain or NULL if none has been found.
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001552 */
1553struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1554{
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001555 struct irq_domain *dom;
Heiner Kallweit4e544ba2019-04-24 21:11:58 +02001556 u32 rid = pci_dev_id(pdev);
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001557
1558 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
Tomasz Nowickibe2021b2016-09-12 20:32:22 +02001559 dom = of_msi_map_get_device_domain(&pdev->dev, rid);
1560 if (!dom)
1561 dom = iort_get_device_domain(&pdev->dev, rid);
1562 return dom;
Marc Zyngier54fa97e2015-10-02 14:43:06 +01001563}
Jiang Liu3878eae2014-11-11 21:02:18 +08001564#endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */