blob: 36b0c2e55929c551a23d80a25dbfa8ee861d53a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File: msi.c
3 * Purpose: PCI Message Signaled Interrupt (MSI)
4 *
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7 */
8
Eric W. Biederman1ce03372006-10-04 02:16:41 -07009#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
11#include <linux/irq.h>
12#include <linux/interrupt.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pci.h>
16#include <linux/proc_fs.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070017#include <linux/msi.h>
Dan Williams4fdadeb2007-04-26 18:21:38 -070018#include <linux/smp.h>
Hidetoshi Seto500559a2009-08-10 10:14:15 +090019#include <linux/errno.h>
20#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Jiang Liu3878eae2014-11-11 21:02:18 +080022#include <linux/irqdomain.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int pci_msi_enable = 1;
Yijing Wang38737d82014-10-27 10:44:36 +080027int pci_msi_ignore_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Bjorn Helgaas527eee22013-04-17 17:44:48 -060029#define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
30
31
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010032/* Arch hooks */
33
Yijing Wang262a2ba2014-11-11 15:22:45 -070034struct msi_controller * __weak pcibios_msi_controller(struct pci_dev *dev)
35{
36 return NULL;
37}
38
39static struct msi_controller *pci_msi_controller(struct pci_dev *dev)
40{
41 struct msi_controller *msi_ctrl = dev->bus->msi;
42
43 if (msi_ctrl)
44 return msi_ctrl;
45
46 return pcibios_msi_controller(dev);
47}
48
Thomas Petazzoni4287d822013-08-09 22:27:06 +020049int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
50{
Yijing Wang262a2ba2014-11-11 15:22:45 -070051 struct msi_controller *chip = pci_msi_controller(dev);
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020052 int err;
53
54 if (!chip || !chip->setup_irq)
55 return -EINVAL;
56
57 err = chip->setup_irq(chip, dev, desc);
58 if (err < 0)
59 return err;
60
61 irq_set_chip_data(desc->irq, chip);
62
63 return 0;
Thomas Petazzoni4287d822013-08-09 22:27:06 +020064}
65
66void __weak arch_teardown_msi_irq(unsigned int irq)
67{
Yijing Wangc2791b82014-11-11 17:45:45 -070068 struct msi_controller *chip = irq_get_chip_data(irq);
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020069
70 if (!chip || !chip->teardown_irq)
71 return;
72
73 chip->teardown_irq(chip, irq);
Thomas Petazzoni4287d822013-08-09 22:27:06 +020074}
75
Thomas Petazzoni4287d822013-08-09 22:27:06 +020076int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010077{
78 struct msi_desc *entry;
79 int ret;
80
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040081 /*
82 * If an architecture wants to support multiple MSI, it needs to
83 * override arch_setup_msi_irqs()
84 */
85 if (type == PCI_CAP_ID_MSI && nvec > 1)
86 return 1;
87
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010088 list_for_each_entry(entry, &dev->msi_list, list) {
89 ret = arch_setup_msi_irq(dev, entry);
Michael Ellermanb5fbf532009-02-11 22:27:02 +110090 if (ret < 0)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010091 return ret;
Michael Ellermanb5fbf532009-02-11 22:27:02 +110092 if (ret > 0)
93 return -ENOSPC;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010094 }
95
96 return 0;
97}
98
Thomas Petazzoni4287d822013-08-09 22:27:06 +020099/*
100 * We have a default implementation available as a separate non-weak
101 * function, as it is used by the Xen x86 PCI code
102 */
Thomas Gleixner1525bf02010-10-06 16:05:35 -0400103void default_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100104{
Jiang Liu63a7b172014-11-06 22:20:32 +0800105 int i;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100106 struct msi_desc *entry;
107
Jiang Liu63a7b172014-11-06 22:20:32 +0800108 list_for_each_entry(entry, &dev->msi_list, list)
109 if (entry->irq)
110 for (i = 0; i < entry->nvec_used; i++)
111 arch_teardown_msi_irq(entry->irq + i);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100112}
113
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200114void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
115{
116 return default_teardown_msi_irqs(dev);
117}
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500118
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800119static void default_restore_msi_irq(struct pci_dev *dev, int irq)
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500120{
121 struct msi_desc *entry;
122
123 entry = NULL;
124 if (dev->msix_enabled) {
125 list_for_each_entry(entry, &dev->msi_list, list) {
126 if (irq == entry->irq)
127 break;
128 }
129 } else if (dev->msi_enabled) {
130 entry = irq_get_msi_desc(irq);
131 }
132
133 if (entry)
Jiang Liu83a18912014-11-09 23:10:34 +0800134 __pci_write_msi_msg(entry, &entry->msg);
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500135}
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200136
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800137void __weak arch_restore_msi_irqs(struct pci_dev *dev)
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200138{
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800139 return default_restore_msi_irqs(dev);
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200140}
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500141
Gavin Shane375b562013-04-04 16:54:30 +0000142static void msi_set_enable(struct pci_dev *dev, int enable)
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800143{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800144 u16 control;
145
Gavin Shane375b562013-04-04 16:54:30 +0000146 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600147 control &= ~PCI_MSI_FLAGS_ENABLE;
148 if (enable)
149 control |= PCI_MSI_FLAGS_ENABLE;
Gavin Shane375b562013-04-04 16:54:30 +0000150 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +0900151}
152
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800153static void msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800154{
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800155 u16 ctrl;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800156
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800157 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
158 ctrl &= ~clear;
159 ctrl |= set;
160 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800161}
162
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500163static inline __attribute_const__ u32 msi_mask(unsigned x)
164{
Matthew Wilcox0b49ec32009-02-08 20:27:47 -0700165 /* Don't shift by >= width of type */
166 if (x >= 5)
167 return 0xffffffff;
168 return (1 << (1 << x)) - 1;
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500169}
170
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600171/*
172 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
173 * mask all MSI interrupts by clearing the MSI enable bit does not work
174 * reliably as devices without an INTx disable bit will then generate a
175 * level IRQ which will never be cleared.
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600176 */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100177u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400179 u32 mask_bits = desc->masked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Yijing Wang38737d82014-10-27 10:44:36 +0800181 if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900182 return 0;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400183
184 mask_bits &= ~mask;
185 mask_bits |= flag;
186 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900187
188 return mask_bits;
189}
190
191static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
192{
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100193 desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400194}
195
196/*
197 * This internal function does not flush PCI writes to the device.
198 * All users must ensure that they read from the device before either
199 * assuming that the device state is up to date, or returning out of this
200 * file. This saves a few milliseconds when initialising devices with lots
201 * of MSI-X interrupts.
202 */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100203u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400204{
205 u32 mask_bits = desc->masked;
206 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900207 PCI_MSIX_ENTRY_VECTOR_CTRL;
Yijing Wang38737d82014-10-27 10:44:36 +0800208
209 if (pci_msi_ignore_mask)
210 return 0;
211
Sheng Yang8d805282010-11-11 15:46:55 +0800212 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
213 if (flag)
214 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400215 writel(mask_bits, desc->mask_base + offset);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900216
217 return mask_bits;
218}
219
220static void msix_mask_irq(struct msi_desc *desc, u32 flag)
221{
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100222 desc->masked = __pci_msix_desc_mask_irq(desc, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400223}
224
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200225static void msi_set_mask_bit(struct irq_data *data, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400226{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200227 struct msi_desc *desc = irq_data_get_msi(data);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400228
229 if (desc->msi_attrib.is_msix) {
230 msix_mask_irq(desc, flag);
231 readl(desc->mask_base); /* Flush write to device */
Matthew Wilcox24d27552009-03-17 08:54:06 -0400232 } else {
Yijing Wanga281b782014-07-08 10:08:55 +0800233 unsigned offset = data->irq - desc->irq;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400234 msi_mask_irq(desc, 1 << offset, flag << offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400236}
237
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100238/**
239 * pci_msi_mask_irq - Generic irq chip callback to mask PCI/MSI interrupts
240 * @data: pointer to irqdata associated to that interrupt
241 */
242void pci_msi_mask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400243{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200244 msi_set_mask_bit(data, 1);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400245}
246
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100247/**
248 * pci_msi_unmask_irq - Generic irq chip callback to unmask PCI/MSI interrupts
249 * @data: pointer to irqdata associated to that interrupt
250 */
251void pci_msi_unmask_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400252{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200253 msi_set_mask_bit(data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800256void default_restore_msi_irqs(struct pci_dev *dev)
257{
258 struct msi_desc *entry;
259
Jiang Liu3f3ceca2014-11-06 22:20:31 +0800260 list_for_each_entry(entry, &dev->msi_list, list)
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800261 default_restore_msi_irq(dev, entry->irq);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800262}
263
Jiang Liu891d4a42014-11-09 23:10:33 +0800264void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700265{
Ben Hutchings30da5522010-07-23 14:56:28 +0100266 BUG_ON(entry->dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700267
Ben Hutchings30da5522010-07-23 14:56:28 +0100268 if (entry->msi_attrib.is_msix) {
269 void __iomem *base = entry->mask_base +
270 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
271
272 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
273 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
274 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
275 } else {
276 struct pci_dev *dev = entry->dev;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600277 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100278 u16 data;
279
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600280 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
281 &msg->address_lo);
Ben Hutchings30da5522010-07-23 14:56:28 +0100282 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600283 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
284 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600285 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100286 } else {
287 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600288 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100289 }
290 msg->data = data;
291 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700292}
293
Jiang Liu83a18912014-11-09 23:10:34 +0800294void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800295{
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100296 if (entry->dev->current_state != PCI_D0) {
297 /* Don't touch the hardware now */
298 } else if (entry->msi_attrib.is_msix) {
Matthew Wilcox24d27552009-03-17 08:54:06 -0400299 void __iomem *base;
300 base = entry->mask_base +
301 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
302
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900303 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
304 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
305 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400306 } else {
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700307 struct pci_dev *dev = entry->dev;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600308 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400309 u16 msgctl;
310
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600311 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400312 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
313 msgctl |= entry->msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600314 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700315
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600316 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
317 msg->address_lo);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700318 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600319 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
320 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600321 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
322 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700323 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600324 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
325 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700326 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700327 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700328 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700329}
330
Jiang Liu83a18912014-11-09 23:10:34 +0800331void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800332{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200333 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800334
Jiang Liu83a18912014-11-09 23:10:34 +0800335 __pci_write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800336}
Jiang Liu83a18912014-11-09 23:10:34 +0800337EXPORT_SYMBOL_GPL(pci_write_msi_msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800338
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900339static void free_msi_irqs(struct pci_dev *dev)
340{
341 struct msi_desc *entry, *tmp;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800342 struct attribute **msi_attrs;
343 struct device_attribute *dev_attr;
Jiang Liu63a7b172014-11-06 22:20:32 +0800344 int i, count = 0;
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900345
Jiang Liu63a7b172014-11-06 22:20:32 +0800346 list_for_each_entry(entry, &dev->msi_list, list)
347 if (entry->irq)
348 for (i = 0; i < entry->nvec_used; i++)
349 BUG_ON(irq_has_action(entry->irq + i));
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900350
351 arch_teardown_msi_irqs(dev);
352
353 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
354 if (entry->msi_attrib.is_msix) {
355 if (list_is_last(&entry->list, &dev->msi_list))
356 iounmap(entry->mask_base);
357 }
Neil Horman424eb392012-01-03 10:29:54 -0500358
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900359 list_del(&entry->list);
360 kfree(entry);
361 }
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800362
363 if (dev->msi_irq_groups) {
364 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
365 msi_attrs = dev->msi_irq_groups[0]->attrs;
Alexei Starovoitovb701c0b2014-06-04 15:49:50 -0700366 while (msi_attrs[count]) {
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800367 dev_attr = container_of(msi_attrs[count],
368 struct device_attribute, attr);
369 kfree(dev_attr->attr.name);
370 kfree(dev_attr);
371 ++count;
372 }
373 kfree(msi_attrs);
374 kfree(dev->msi_irq_groups[0]);
375 kfree(dev->msi_irq_groups);
376 dev->msi_irq_groups = NULL;
377 }
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900378}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900379
Matthew Wilcox379f5322009-03-17 08:54:07 -0400380static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Matthew Wilcox379f5322009-03-17 08:54:07 -0400382 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
383 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return NULL;
385
Matthew Wilcox379f5322009-03-17 08:54:07 -0400386 INIT_LIST_HEAD(&desc->list);
387 desc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Matthew Wilcox379f5322009-03-17 08:54:07 -0400389 return desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
David Millerba698ad2007-10-25 01:16:30 -0700392static void pci_intx_for_msi(struct pci_dev *dev, int enable)
393{
394 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
395 pci_intx(dev, enable);
396}
397
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100398static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800399{
Shaohua Li41017f02006-02-08 17:11:38 +0800400 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700401 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800402
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800403 if (!dev->msi_enabled)
404 return;
405
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200406 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800407
David Millerba698ad2007-10-25 01:16:30 -0700408 pci_intx_for_msi(dev, 0);
Gavin Shane375b562013-04-04 16:54:30 +0000409 msi_set_enable(dev, 0);
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800410 arch_restore_msi_irqs(dev);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700411
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600412 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Yijing Wang31ea5d42014-06-19 16:30:30 +0800413 msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
414 entry->masked);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700415 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400416 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600417 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100418}
419
420static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800421{
Shaohua Li41017f02006-02-08 17:11:38 +0800422 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800423
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700424 if (!dev->msix_enabled)
425 return;
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700426 BUG_ON(list_empty(&dev->msi_list));
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700427
Shaohua Li41017f02006-02-08 17:11:38 +0800428 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700429 pci_intx_for_msi(dev, 0);
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800430 msix_clear_and_set_ctrl(dev, 0,
431 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
Shaohua Li41017f02006-02-08 17:11:38 +0800432
DuanZhenzhongac8344c2013-12-04 13:09:16 +0800433 arch_restore_msi_irqs(dev);
Jiang Liu3f3ceca2014-11-06 22:20:31 +0800434 list_for_each_entry(entry, &dev->msi_list, list)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400435 msix_mask_irq(entry, entry->masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800436
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800437 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800438}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100439
440void pci_restore_msi_state(struct pci_dev *dev)
441{
442 __pci_restore_msi_state(dev);
443 __pci_restore_msix_state(dev);
444}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600445EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800446
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800447static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
Neil Hormanda8d1c82011-10-06 14:08:18 -0400448 char *buf)
449{
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800450 struct msi_desc *entry;
451 unsigned long irq;
452 int retval;
453
454 retval = kstrtoul(attr->attr.name, 10, &irq);
455 if (retval)
456 return retval;
457
Yijing Wange11ece52014-07-08 10:09:19 +0800458 entry = irq_get_msi_desc(irq);
459 if (entry)
460 return sprintf(buf, "%s\n",
461 entry->msi_attrib.is_msix ? "msix" : "msi");
462
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800463 return -ENODEV;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400464}
465
Neil Hormanda8d1c82011-10-06 14:08:18 -0400466static int populate_msi_sysfs(struct pci_dev *pdev)
467{
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800468 struct attribute **msi_attrs;
469 struct attribute *msi_attr;
470 struct device_attribute *msi_dev_attr;
471 struct attribute_group *msi_irq_group;
472 const struct attribute_group **msi_irq_groups;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400473 struct msi_desc *entry;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800474 int ret = -ENOMEM;
475 int num_msi = 0;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400476 int count = 0;
477
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800478 /* Determine how many msi entries we have */
Jiang Liu3f3ceca2014-11-06 22:20:31 +0800479 list_for_each_entry(entry, &pdev->msi_list, list)
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800480 ++num_msi;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800481 if (!num_msi)
482 return 0;
483
484 /* Dynamically create the MSI attributes for the PCI device */
485 msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
486 if (!msi_attrs)
487 return -ENOMEM;
488 list_for_each_entry(entry, &pdev->msi_list, list) {
Greg Kroah-Hartman86bb4f62014-02-13 10:47:20 -0700489 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
Jan Beulich14062762014-04-14 14:59:50 -0600490 if (!msi_dev_attr)
Greg Kroah-Hartman86bb4f62014-02-13 10:47:20 -0700491 goto error_attrs;
Jan Beulich14062762014-04-14 14:59:50 -0600492 msi_attrs[count] = &msi_dev_attr->attr;
Greg Kroah-Hartman86bb4f62014-02-13 10:47:20 -0700493
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800494 sysfs_attr_init(&msi_dev_attr->attr);
Jan Beulich14062762014-04-14 14:59:50 -0600495 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
496 entry->irq);
497 if (!msi_dev_attr->attr.name)
498 goto error_attrs;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800499 msi_dev_attr->attr.mode = S_IRUGO;
500 msi_dev_attr->show = msi_mode_show;
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800501 ++count;
502 }
503
504 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
505 if (!msi_irq_group)
506 goto error_attrs;
507 msi_irq_group->name = "msi_irqs";
508 msi_irq_group->attrs = msi_attrs;
509
510 msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
511 if (!msi_irq_groups)
512 goto error_irq_group;
513 msi_irq_groups[0] = msi_irq_group;
514
515 ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
516 if (ret)
517 goto error_irq_groups;
518 pdev->msi_irq_groups = msi_irq_groups;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400519
520 return 0;
521
Greg Kroah-Hartman1c51b502013-12-19 12:30:17 -0800522error_irq_groups:
523 kfree(msi_irq_groups);
524error_irq_group:
525 kfree(msi_irq_group);
526error_attrs:
527 count = 0;
528 msi_attr = msi_attrs[count];
529 while (msi_attr) {
530 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
531 kfree(msi_attr->name);
532 kfree(msi_dev_attr);
533 ++count;
534 msi_attr = msi_attrs[count];
Neil Hormanda8d1c82011-10-06 14:08:18 -0400535 }
Greg Kroah-Hartman29237752014-02-13 10:47:35 -0700536 kfree(msi_attrs);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400537 return ret;
538}
539
Jiang Liu63a7b172014-11-06 22:20:32 +0800540static struct msi_desc *msi_setup_entry(struct pci_dev *dev, int nvec)
Yijing Wangd873b4d2014-07-08 10:07:23 +0800541{
542 u16 control;
543 struct msi_desc *entry;
544
545 /* MSI Entry Initialization */
546 entry = alloc_msi_entry(dev);
547 if (!entry)
548 return NULL;
549
550 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
551
552 entry->msi_attrib.is_msix = 0;
553 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
554 entry->msi_attrib.entry_nr = 0;
555 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
556 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Yijing Wangd873b4d2014-07-08 10:07:23 +0800557 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
Jiang Liu63a7b172014-11-06 22:20:32 +0800558 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
559 entry->nvec_used = nvec;
Yijing Wangd873b4d2014-07-08 10:07:23 +0800560
561 if (control & PCI_MSI_FLAGS_64BIT)
562 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
563 else
564 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
565
566 /* Save the initial mask status */
567 if (entry->msi_attrib.maskbit)
568 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
569
570 return entry;
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/**
574 * msi_capability_init - configure device's MSI capability structure
575 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400576 * @nvec: number of interrupts to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400578 * Setup the MSI capability structure of the device with the requested
579 * number of interrupts. A return value of zero indicates the successful
580 * setup of an entry with the new MSI irq. A negative return value indicates
581 * an error, and a positive return value indicates the number of interrupts
582 * which could have been allocated.
583 */
584static int msi_capability_init(struct pci_dev *dev, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000587 int ret;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400588 unsigned mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Gavin Shane375b562013-04-04 16:54:30 +0000590 msi_set_enable(dev, 0); /* Disable MSI during set up */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600591
Jiang Liu63a7b172014-11-06 22:20:32 +0800592 entry = msi_setup_entry(dev, nvec);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700593 if (!entry)
594 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700595
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400596 /* All MSIs are unmasked by default, Mask them all */
Yijing Wang31ea5d42014-06-19 16:30:30 +0800597 mask = msi_mask(entry->msi_attrib.multi_cap);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400598 msi_mask_irq(entry, mask, mask);
599
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700600 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* Configure MSI capability structure */
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400603 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000604 if (ret) {
Hidetoshi Seto7ba19302009-06-23 17:39:27 +0900605 msi_mask_irq(entry, mask, ~mask);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900606 free_msi_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000607 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500608 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700609
Neil Hormanda8d1c82011-10-06 14:08:18 -0400610 ret = populate_msi_sysfs(dev);
611 if (ret) {
612 msi_mask_irq(entry, mask, ~mask);
613 free_msi_irqs(dev);
614 return ret;
615 }
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700618 pci_intx_for_msi(dev, 0);
Gavin Shane375b562013-04-04 16:54:30 +0000619 msi_set_enable(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800620 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Michael Ellerman7fe37302007-04-18 19:39:21 +1000622 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624}
625
Gavin Shan520fe9d2013-04-04 16:54:33 +0000626static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900627{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900628 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900629 u32 table_offset;
630 u8 bir;
631
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600632 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
633 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600634 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
635 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900636 phys_addr = pci_resource_start(dev, bir) + table_offset;
637
638 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
639}
640
Gavin Shan520fe9d2013-04-04 16:54:33 +0000641static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
642 struct msix_entry *entries, int nvec)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900643{
644 struct msi_desc *entry;
645 int i;
646
647 for (i = 0; i < nvec; i++) {
648 entry = alloc_msi_entry(dev);
649 if (!entry) {
650 if (!i)
651 iounmap(base);
652 else
653 free_msi_irqs(dev);
654 /* No enough memory. Don't try again */
655 return -ENOMEM;
656 }
657
658 entry->msi_attrib.is_msix = 1;
659 entry->msi_attrib.is_64 = 1;
660 entry->msi_attrib.entry_nr = entries[i].entry;
661 entry->msi_attrib.default_irq = dev->irq;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900662 entry->mask_base = base;
Jiang Liu63a7b172014-11-06 22:20:32 +0800663 entry->nvec_used = 1;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900664
665 list_add_tail(&entry->list, &dev->msi_list);
666 }
667
668 return 0;
669}
670
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900671static void msix_program_entries(struct pci_dev *dev,
Gavin Shan520fe9d2013-04-04 16:54:33 +0000672 struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900673{
674 struct msi_desc *entry;
675 int i = 0;
676
677 list_for_each_entry(entry, &dev->msi_list, list) {
678 int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
679 PCI_MSIX_ENTRY_VECTOR_CTRL;
680
681 entries[i].vector = entry->irq;
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900682 entry->masked = readl(entry->mask_base + offset);
683 msix_mask_irq(entry, 1);
684 i++;
685 }
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/**
689 * msix_capability_init - configure device's MSI-X capability
690 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700691 * @entries: pointer to an array of struct msix_entry entries
692 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600694 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700695 * single MSI-X irq. A return of zero indicates the successful setup of
696 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 **/
698static int msix_capability_init(struct pci_dev *dev,
699 struct msix_entry *entries, int nvec)
700{
Gavin Shan520fe9d2013-04-04 16:54:33 +0000701 int ret;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900702 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 void __iomem *base;
704
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700705 /* Ensure MSI-X is disabled while it is set up */
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800706 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700707
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800708 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* Request & Map MSI-X table region */
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600710 base = msix_map_region(dev, msix_table_size(control));
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900711 if (!base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -ENOMEM;
713
Gavin Shan520fe9d2013-04-04 16:54:33 +0000714 ret = msix_setup_entries(dev, base, entries, nvec);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900715 if (ret)
716 return ret;
Michael Ellerman9c831332007-04-18 19:39:21 +1000717
718 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900719 if (ret)
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100720 goto out_avail;
Michael Ellerman9c831332007-04-18 19:39:21 +1000721
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700722 /*
723 * Some devices require MSI-X to be enabled before we can touch the
724 * MSI-X registers. We need to mask all the vectors to prevent
725 * interrupts coming in before they're fully set up.
726 */
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800727 msix_clear_and_set_ctrl(dev, 0,
728 PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700729
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900730 msix_program_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700731
Neil Hormanda8d1c82011-10-06 14:08:18 -0400732 ret = populate_msi_sysfs(dev);
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100733 if (ret)
734 goto out_free;
Neil Hormanda8d1c82011-10-06 14:08:18 -0400735
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700736 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700737 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800738 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800740 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900743
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100744out_avail:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900745 if (ret < 0) {
746 /*
747 * If we had some success, report the number of irqs
748 * we succeeded in setting up.
749 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900750 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900751 int avail = 0;
752
753 list_for_each_entry(entry, &dev->msi_list, list) {
754 if (entry->irq != 0)
755 avail++;
756 }
757 if (avail != 0)
758 ret = avail;
759 }
760
Alexander Gordeev2adc7902013-12-16 09:34:56 +0100761out_free:
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900762 free_msi_irqs(dev);
763
764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/**
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600768 * pci_msi_supported - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400769 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000770 * @nvec: how many MSIs have been requested ?
Brice Goglin24334a12006-08-31 01:55:07 -0400771 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700772 * Look at global flags, the device itself, and its parent buses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000773 * to determine if MSI/-X are supported for the device. If MSI/-X is
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600774 * supported return 1, else return 0.
Brice Goglin24334a12006-08-31 01:55:07 -0400775 **/
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600776static int pci_msi_supported(struct pci_dev *dev, int nvec)
Brice Goglin24334a12006-08-31 01:55:07 -0400777{
778 struct pci_bus *bus;
779
Brice Goglin0306ebf2006-10-05 10:24:31 +0200780 /* MSI must be globally enabled and supported by the device */
Alexander Gordeev27e20602014-09-23 14:25:11 -0600781 if (!pci_msi_enable)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600782 return 0;
Alexander Gordeev27e20602014-09-23 14:25:11 -0600783
784 if (!dev || dev->no_msi || dev->current_state != PCI_D0)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600785 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400786
Michael Ellerman314e77b2007-04-05 17:19:12 +1000787 /*
788 * You can't ask to have 0 or less MSIs configured.
789 * a) it's stupid ..
790 * b) the list manipulation code assumes nvec >= 1.
791 */
792 if (nvec < 1)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600793 return 0;
Michael Ellerman314e77b2007-04-05 17:19:12 +1000794
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900795 /*
796 * Any bridge which does NOT route MSI transactions from its
797 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200798 * the secondary pci_bus.
799 * We expect only arch-specific PCI host bus controller driver
800 * or quirks for specific PCI bridges to be setting NO_MSI.
801 */
Brice Goglin24334a12006-08-31 01:55:07 -0400802 for (bus = dev->bus; bus; bus = bus->parent)
803 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600804 return 0;
Brice Goglin24334a12006-08-31 01:55:07 -0400805
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600806 return 1;
Brice Goglin24334a12006-08-31 01:55:07 -0400807}
808
809/**
Alexander Gordeevd1ac1d22013-12-30 08:28:13 +0100810 * pci_msi_vec_count - Return the number of MSI vectors a device can send
811 * @dev: device to report about
812 *
813 * This function returns the number of MSI vectors a device requested via
814 * Multiple Message Capable register. It returns a negative errno if the
815 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
816 * and returns a power of two, up to a maximum of 2^5 (32), according to the
817 * MSI specification.
818 **/
819int pci_msi_vec_count(struct pci_dev *dev)
820{
821 int ret;
822 u16 msgctl;
823
824 if (!dev->msi_cap)
825 return -EINVAL;
826
827 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
828 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
829
830 return ret;
831}
832EXPORT_SYMBOL(pci_msi_vec_count);
833
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400834void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400836 struct msi_desc *desc;
837 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100839 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700840 return;
841
Matthew Wilcox110828c2009-06-16 06:31:45 -0600842 BUG_ON(list_empty(&dev->msi_list));
843 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600844
Gavin Shane375b562013-04-04 16:54:30 +0000845 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700846 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800847 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700848
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900849 /* Return the device with MSI unmasked as initial states */
Yijing Wang31ea5d42014-06-19 16:30:30 +0800850 mask = msi_mask(desc->msi_attrib.multi_cap);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900851 /* Keep cached state to be restored */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100852 __pci_msi_desc_mask_irq(desc, mask, ~mask);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100853
854 /* Restore dev->irq to its default pin-assertion irq */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400855 dev->irq = desc->msi_attrib.default_irq;
Yinghai Lud52877c2008-04-23 14:58:09 -0700856}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400857
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900858void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700859{
Yinghai Lud52877c2008-04-23 14:58:09 -0700860 if (!pci_msi_enable || !dev || !dev->msi_enabled)
861 return;
862
863 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900864 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100866EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100869 * pci_msix_vec_count - return the number of device's MSI-X table entries
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100870 * @dev: pointer to the pci_dev data structure of MSI-X device function
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100871 * This function returns the number of device's MSI-X table entries and
872 * therefore the number of MSI-X vectors device is capable of sending.
873 * It returns a negative errno if the device is not capable of sending MSI-X
874 * interrupts.
875 **/
876int pci_msix_vec_count(struct pci_dev *dev)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100877{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100878 u16 control;
879
Gavin Shan520fe9d2013-04-04 16:54:33 +0000880 if (!dev->msix_cap)
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100881 return -EINVAL;
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100882
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600883 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600884 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100885}
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100886EXPORT_SYMBOL(pci_msix_vec_count);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100887
888/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * pci_enable_msix - configure device's MSI-X capability structure
890 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700891 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700892 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 *
894 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700895 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * MSI-X mode enabled on its hardware device function. A return of zero
897 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700898 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * Or a return of > 0 indicates that driver request is exceeding the number
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300900 * of irqs or MSI-X vectors available. Driver should use the returned value to
901 * re-send its request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900903int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Bjorn Helgaas5ec09402014-09-23 14:38:28 -0600905 int nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700906 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Alexander Gordeeva06cd742014-09-23 12:45:58 -0600908 if (!pci_msi_supported(dev, nvec))
909 return -EINVAL;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000910
Alexander Gordeev27e20602014-09-23 14:25:11 -0600911 if (!entries)
912 return -EINVAL;
913
Alexander Gordeevff1aa432013-12-30 08:28:15 +0100914 nr_entries = pci_msix_vec_count(dev);
915 if (nr_entries < 0)
916 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (nvec > nr_entries)
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300918 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* Check for any invalid entries */
921 for (i = 0; i < nvec; i++) {
922 if (entries[i].entry >= nr_entries)
923 return -EINVAL; /* invalid entry */
924 for (j = i + 1; j < nvec; j++) {
925 if (entries[i].entry == entries[j].entry)
926 return -EINVAL; /* duplicate entry */
927 }
928 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700929 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700930
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700931 /* Check whether driver already requested for MSI irq */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900932 if (dev->msi_enabled) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400933 dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -EINVAL;
935 }
Bjorn Helgaas5ec09402014-09-23 14:38:28 -0600936 return msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100938EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900940void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100941{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900942 struct msi_desc *entry;
943
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100944 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700945 return;
946
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900947 /* Return the device with MSI-X masked as initial states */
948 list_for_each_entry(entry, &dev->msi_list, list) {
949 /* Keep cached states to be restored */
Thomas Gleixner23ed8d52014-11-23 11:55:58 +0100950 __pci_msix_desc_mask_irq(entry, 1);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900951 }
952
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800953 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
David Millerba698ad2007-10-25 01:16:30 -0700954 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800955 dev->msix_enabled = 0;
Yinghai Lud52877c2008-04-23 14:58:09 -0700956}
Hidetoshi Setoc9018512009-08-06 11:31:27 +0900957
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900958void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700959{
960 if (!pci_msi_enable || !dev || !dev->msix_enabled)
961 return;
962
963 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900964 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100966EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700968void pci_no_msi(void)
969{
970 pci_msi_enable = 0;
971}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000972
Andrew Patterson07ae95f2008-11-10 15:31:05 -0700973/**
974 * pci_msi_enabled - is MSI enabled?
975 *
976 * Returns true if MSI has not been disabled by the command-line option
977 * pci=nomsi.
978 **/
979int pci_msi_enabled(void)
980{
981 return pci_msi_enable;
982}
983EXPORT_SYMBOL(pci_msi_enabled);
984
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000985void pci_msi_init_pci_dev(struct pci_dev *dev)
986{
987 INIT_LIST_HEAD(&dev->msi_list);
Eric W. Biedermand5dea7d2011-10-17 11:46:06 -0700988
989 /* Disable the msi hardware to avoid screaming interrupts
990 * during boot. This is the power on reset default so
991 * usually this should be a noop.
992 */
Gavin Shane375b562013-04-04 16:54:30 +0000993 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
994 if (dev->msi_cap)
995 msi_set_enable(dev, 0);
996
997 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
998 if (dev->msix_cap)
Yijing Wang66f0d0c2014-06-19 16:29:53 +0800999 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +10001000}
Alexander Gordeev302a2522013-12-30 08:28:16 +01001001
1002/**
1003 * pci_enable_msi_range - configure device's MSI capability structure
1004 * @dev: device to configure
1005 * @minvec: minimal number of interrupts to configure
1006 * @maxvec: maximum number of interrupts to configure
1007 *
1008 * This function tries to allocate a maximum possible number of interrupts in a
1009 * range between @minvec and @maxvec. It returns a negative errno if an error
1010 * occurs. If it succeeds, it returns the actual number of interrupts allocated
1011 * and updates the @dev's irq member to the lowest new interrupt number;
1012 * the other interrupt numbers allocated to this device are consecutive.
1013 **/
1014int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
1015{
Alexander Gordeev034cd972014-04-14 15:28:35 +02001016 int nvec;
Alexander Gordeev302a2522013-12-30 08:28:16 +01001017 int rc;
1018
Alexander Gordeeva06cd742014-09-23 12:45:58 -06001019 if (!pci_msi_supported(dev, minvec))
1020 return -EINVAL;
Alexander Gordeev034cd972014-04-14 15:28:35 +02001021
1022 WARN_ON(!!dev->msi_enabled);
1023
1024 /* Check whether driver already requested MSI-X irqs */
1025 if (dev->msix_enabled) {
1026 dev_info(&dev->dev,
1027 "can't enable MSI (MSI-X already enabled)\n");
1028 return -EINVAL;
1029 }
1030
Alexander Gordeev302a2522013-12-30 08:28:16 +01001031 if (maxvec < minvec)
1032 return -ERANGE;
1033
Alexander Gordeev034cd972014-04-14 15:28:35 +02001034 nvec = pci_msi_vec_count(dev);
1035 if (nvec < 0)
1036 return nvec;
1037 else if (nvec < minvec)
1038 return -EINVAL;
1039 else if (nvec > maxvec)
1040 nvec = maxvec;
1041
Alexander Gordeev302a2522013-12-30 08:28:16 +01001042 do {
Alexander Gordeev034cd972014-04-14 15:28:35 +02001043 rc = msi_capability_init(dev, nvec);
Alexander Gordeev302a2522013-12-30 08:28:16 +01001044 if (rc < 0) {
1045 return rc;
1046 } else if (rc > 0) {
1047 if (rc < minvec)
1048 return -ENOSPC;
1049 nvec = rc;
1050 }
1051 } while (rc);
1052
1053 return nvec;
1054}
1055EXPORT_SYMBOL(pci_enable_msi_range);
1056
1057/**
1058 * pci_enable_msix_range - configure device's MSI-X capability structure
1059 * @dev: pointer to the pci_dev data structure of MSI-X device function
1060 * @entries: pointer to an array of MSI-X entries
1061 * @minvec: minimum number of MSI-X irqs requested
1062 * @maxvec: maximum number of MSI-X irqs requested
1063 *
1064 * Setup the MSI-X capability structure of device function with a maximum
1065 * possible number of interrupts in the range between @minvec and @maxvec
1066 * upon its software driver call to request for MSI-X mode enabled on its
1067 * hardware device function. It returns a negative errno if an error occurs.
1068 * If it succeeds, it returns the actual number of interrupts allocated and
1069 * indicates the successful configuration of MSI-X capability structure
1070 * with new allocated MSI-X interrupts.
1071 **/
1072int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1073 int minvec, int maxvec)
1074{
1075 int nvec = maxvec;
1076 int rc;
1077
1078 if (maxvec < minvec)
1079 return -ERANGE;
1080
1081 do {
1082 rc = pci_enable_msix(dev, entries, nvec);
1083 if (rc < 0) {
1084 return rc;
1085 } else if (rc > 0) {
1086 if (rc < minvec)
1087 return -ENOSPC;
1088 nvec = rc;
1089 }
1090 } while (rc);
1091
1092 return nvec;
1093}
1094EXPORT_SYMBOL(pci_enable_msix_range);
Jiang Liu3878eae2014-11-11 21:02:18 +08001095
1096#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1097/**
1098 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1099 * @irq_data: Pointer to interrupt data of the MSI interrupt
1100 * @msg: Pointer to the message
1101 */
1102void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1103{
1104 struct msi_desc *desc = irq_data->msi_desc;
1105
1106 /*
1107 * For MSI-X desc->irq is always equal to irq_data->irq. For
1108 * MSI only the first interrupt of MULTI MSI passes the test.
1109 */
1110 if (desc->irq == irq_data->irq)
1111 __pci_write_msi_msg(desc, msg);
1112}
1113
1114/**
1115 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1116 * @dev: Pointer to the PCI device
1117 * @desc: Pointer to the msi descriptor
1118 *
1119 * The ID number is only used within the irqdomain.
1120 */
1121irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
1122 struct msi_desc *desc)
1123{
1124 return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1125 PCI_DEVID(dev->bus->number, dev->devfn) << 11 |
1126 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1127}
1128
1129static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1130{
1131 return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1132}
1133
1134/**
1135 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
1136 * @domain: The interrupt domain to check
1137 * @info: The domain info for verification
1138 * @dev: The device to check
1139 *
1140 * Returns:
1141 * 0 if the functionality is supported
1142 * 1 if Multi MSI is requested, but the domain does not support it
1143 * -ENOTSUPP otherwise
1144 */
1145int pci_msi_domain_check_cap(struct irq_domain *domain,
1146 struct msi_domain_info *info, struct device *dev)
1147{
1148 struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1149
1150 /* Special handling to support pci_enable_msi_range() */
1151 if (pci_msi_desc_is_multi_msi(desc) &&
1152 !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1153 return 1;
1154 else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1155 return -ENOTSUPP;
1156
1157 return 0;
1158}
1159
1160static int pci_msi_domain_handle_error(struct irq_domain *domain,
1161 struct msi_desc *desc, int error)
1162{
1163 /* Special handling to support pci_enable_msi_range() */
1164 if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1165 return 1;
1166
1167 return error;
1168}
1169
1170#ifdef GENERIC_MSI_DOMAIN_OPS
1171static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1172 struct msi_desc *desc)
1173{
1174 arg->desc = desc;
1175 arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
1176 desc);
1177}
1178#else
1179#define pci_msi_domain_set_desc NULL
1180#endif
1181
1182static struct msi_domain_ops pci_msi_domain_ops_default = {
1183 .set_desc = pci_msi_domain_set_desc,
1184 .msi_check = pci_msi_domain_check_cap,
1185 .handle_error = pci_msi_domain_handle_error,
1186};
1187
1188static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1189{
1190 struct msi_domain_ops *ops = info->ops;
1191
1192 if (ops == NULL) {
1193 info->ops = &pci_msi_domain_ops_default;
1194 } else {
1195 if (ops->set_desc == NULL)
1196 ops->set_desc = pci_msi_domain_set_desc;
1197 if (ops->msi_check == NULL)
1198 ops->msi_check = pci_msi_domain_check_cap;
1199 if (ops->handle_error == NULL)
1200 ops->handle_error = pci_msi_domain_handle_error;
1201 }
1202}
1203
1204static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1205{
1206 struct irq_chip *chip = info->chip;
1207
1208 BUG_ON(!chip);
1209 if (!chip->irq_write_msi_msg)
1210 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1211}
1212
1213/**
1214 * pci_msi_create_irq_domain - Creat a MSI interrupt domain
1215 * @node: Optional device-tree node of the interrupt controller
1216 * @info: MSI domain info
1217 * @parent: Parent irq domain
1218 *
1219 * Updates the domain and chip ops and creates a MSI interrupt domain.
1220 *
1221 * Returns:
1222 * A domain pointer or NULL in case of failure.
1223 */
1224struct irq_domain *pci_msi_create_irq_domain(struct device_node *node,
1225 struct msi_domain_info *info,
1226 struct irq_domain *parent)
1227{
1228 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1229 pci_msi_domain_update_dom_ops(info);
1230 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1231 pci_msi_domain_update_chip_ops(info);
1232
1233 return msi_create_irq_domain(node, info, parent);
1234}
1235
1236/**
1237 * pci_msi_domain_alloc_irqs - Allocate interrupts for @dev in @domain
1238 * @domain: The interrupt domain to allocate from
1239 * @dev: The device for which to allocate
1240 * @nvec: The number of interrupts to allocate
1241 * @type: Unused to allow simpler migration from the arch_XXX interfaces
1242 *
1243 * Returns:
1244 * A virtual interrupt number or an error code in case of failure
1245 */
1246int pci_msi_domain_alloc_irqs(struct irq_domain *domain, struct pci_dev *dev,
1247 int nvec, int type)
1248{
1249 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
1250}
1251
1252/**
1253 * pci_msi_domain_free_irqs - Free interrupts for @dev in @domain
1254 * @domain: The interrupt domain
1255 * @dev: The device for which to free interrupts
1256 */
1257void pci_msi_domain_free_irqs(struct irq_domain *domain, struct pci_dev *dev)
1258{
1259 msi_domain_free_irqs(domain, &dev->dev);
1260}
1261#endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */