blob: d5f90d6383bc38660dcaba3786adcbf1e78a936b [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>
13#include <linux/init.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Bjorn Helgaas527eee22013-04-17 17:44:48 -060028#define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
29
30
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010031/* Arch hooks */
32
Thomas Petazzoni4287d822013-08-09 22:27:06 +020033int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
34{
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020035 struct msi_chip *chip = dev->bus->msi;
36 int err;
37
38 if (!chip || !chip->setup_irq)
39 return -EINVAL;
40
41 err = chip->setup_irq(chip, dev, desc);
42 if (err < 0)
43 return err;
44
45 irq_set_chip_data(desc->irq, chip);
46
47 return 0;
Thomas Petazzoni4287d822013-08-09 22:27:06 +020048}
49
50void __weak arch_teardown_msi_irq(unsigned int irq)
51{
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020052 struct msi_chip *chip = irq_get_chip_data(irq);
53
54 if (!chip || !chip->teardown_irq)
55 return;
56
57 chip->teardown_irq(chip, irq);
Thomas Petazzoni4287d822013-08-09 22:27:06 +020058}
59
60int __weak arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010061{
Thierry Reding0cbdcfc2013-08-09 22:27:08 +020062 struct msi_chip *chip = dev->bus->msi;
63
64 if (!chip || !chip->check_device)
65 return 0;
66
67 return chip->check_device(chip, dev, nvec, type);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010068}
69
Thomas Petazzoni4287d822013-08-09 22:27:06 +020070int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010071{
72 struct msi_desc *entry;
73 int ret;
74
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040075 /*
76 * If an architecture wants to support multiple MSI, it needs to
77 * override arch_setup_msi_irqs()
78 */
79 if (type == PCI_CAP_ID_MSI && nvec > 1)
80 return 1;
81
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010082 list_for_each_entry(entry, &dev->msi_list, list) {
83 ret = arch_setup_msi_irq(dev, entry);
Michael Ellermanb5fbf532009-02-11 22:27:02 +110084 if (ret < 0)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010085 return ret;
Michael Ellermanb5fbf532009-02-11 22:27:02 +110086 if (ret > 0)
87 return -ENOSPC;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010088 }
89
90 return 0;
91}
92
Thomas Petazzoni4287d822013-08-09 22:27:06 +020093/*
94 * We have a default implementation available as a separate non-weak
95 * function, as it is used by the Xen x86 PCI code
96 */
Thomas Gleixner1525bf02010-10-06 16:05:35 -040097void default_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010098{
99 struct msi_desc *entry;
100
101 list_for_each_entry(entry, &dev->msi_list, list) {
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400102 int i, nvec;
103 if (entry->irq == 0)
104 continue;
Alexander Gordeev65f6ae62013-05-13 11:05:48 +0200105 if (entry->nvec_used)
106 nvec = entry->nvec_used;
107 else
108 nvec = 1 << entry->msi_attrib.multiple;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400109 for (i = 0; i < nvec; i++)
110 arch_teardown_msi_irq(entry->irq + i);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +0100111 }
112}
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
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500119void default_restore_msi_irqs(struct pci_dev *dev, int irq)
120{
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)
134 write_msi_msg(irq, &entry->msg);
135}
Thomas Petazzoni4287d822013-08-09 22:27:06 +0200136
137void __weak arch_restore_msi_irqs(struct pci_dev *dev, int irq)
138{
139 return default_restore_msi_irqs(dev, irq);
140}
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
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800153static void msix_set_enable(struct pci_dev *dev, int enable)
154{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800155 u16 control;
156
Gavin Shane375b562013-04-04 16:54:30 +0000157 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
158 control &= ~PCI_MSIX_FLAGS_ENABLE;
159 if (enable)
160 control |= PCI_MSIX_FLAGS_ENABLE;
161 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800162}
163
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500164static inline __attribute_const__ u32 msi_mask(unsigned x)
165{
Matthew Wilcox0b49ec32009-02-08 20:27:47 -0700166 /* Don't shift by >= width of type */
167 if (x >= 5)
168 return 0xffffffff;
169 return (1 << (1 << x)) - 1;
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500170}
171
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400172static inline __attribute_const__ u32 msi_capable_mask(u16 control)
Mitch Williams988cbb12007-03-30 11:54:08 -0700173{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400174 return msi_mask((control >> 1) & 7);
175}
Mitch Williams988cbb12007-03-30 11:54:08 -0700176
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400177static inline __attribute_const__ u32 msi_enabled_mask(u16 control)
178{
179 return msi_mask((control >> 4) & 7);
Mitch Williams988cbb12007-03-30 11:54:08 -0700180}
181
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600182/*
183 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
184 * mask all MSI interrupts by clearing the MSI enable bit does not work
185 * reliably as devices without an INTx disable bit will then generate a
186 * level IRQ which will never be cleared.
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600187 */
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900188static u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400190 u32 mask_bits = desc->masked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400192 if (!desc->msi_attrib.maskbit)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900193 return 0;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400194
195 mask_bits &= ~mask;
196 mask_bits |= flag;
197 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900198
199 return mask_bits;
200}
201
202static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
203{
204 desc->masked = __msi_mask_irq(desc, mask, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400205}
206
207/*
208 * This internal function does not flush PCI writes to the device.
209 * All users must ensure that they read from the device before either
210 * assuming that the device state is up to date, or returning out of this
211 * file. This saves a few milliseconds when initialising devices with lots
212 * of MSI-X interrupts.
213 */
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900214static u32 __msix_mask_irq(struct msi_desc *desc, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400215{
216 u32 mask_bits = desc->masked;
217 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900218 PCI_MSIX_ENTRY_VECTOR_CTRL;
Sheng Yang8d805282010-11-11 15:46:55 +0800219 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
220 if (flag)
221 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400222 writel(mask_bits, desc->mask_base + offset);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900223
224 return mask_bits;
225}
226
227static void msix_mask_irq(struct msi_desc *desc, u32 flag)
228{
229 desc->masked = __msix_mask_irq(desc, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400230}
231
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200232static void msi_set_mask_bit(struct irq_data *data, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400233{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200234 struct msi_desc *desc = irq_data_get_msi(data);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400235
236 if (desc->msi_attrib.is_msix) {
237 msix_mask_irq(desc, flag);
238 readl(desc->mask_base); /* Flush write to device */
Matthew Wilcox24d27552009-03-17 08:54:06 -0400239 } else {
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200240 unsigned offset = data->irq - desc->dev->irq;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400241 msi_mask_irq(desc, 1 << offset, flag << offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400243}
244
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200245void mask_msi_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400246{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200247 msi_set_mask_bit(data, 1);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400248}
249
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200250void unmask_msi_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400251{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200252 msi_set_mask_bit(data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200255void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700256{
Ben Hutchings30da5522010-07-23 14:56:28 +0100257 BUG_ON(entry->dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700258
Ben Hutchings30da5522010-07-23 14:56:28 +0100259 if (entry->msi_attrib.is_msix) {
260 void __iomem *base = entry->mask_base +
261 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
262
263 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
264 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
265 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
266 } else {
267 struct pci_dev *dev = entry->dev;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600268 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100269 u16 data;
270
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600271 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
272 &msg->address_lo);
Ben Hutchings30da5522010-07-23 14:56:28 +0100273 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600274 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
275 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600276 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100277 } else {
278 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600279 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100280 }
281 msg->data = data;
282 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700283}
284
Yinghai Lu3145e942008-12-05 18:58:34 -0800285void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700286{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200287 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800288
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200289 __read_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800290}
291
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200292void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Ben Hutchings30da5522010-07-23 14:56:28 +0100293{
Ben Hutchings30da5522010-07-23 14:56:28 +0100294 /* Assert that the cache is valid, assuming that
295 * valid messages are not all-zeroes. */
296 BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
297 entry->msg.data));
298
299 *msg = entry->msg;
300}
301
302void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
303{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200304 struct msi_desc *entry = irq_get_msi_desc(irq);
Ben Hutchings30da5522010-07-23 14:56:28 +0100305
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200306 __get_cached_msi_msg(entry, msg);
Ben Hutchings30da5522010-07-23 14:56:28 +0100307}
308
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200309void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800310{
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100311 if (entry->dev->current_state != PCI_D0) {
312 /* Don't touch the hardware now */
313 } else if (entry->msi_attrib.is_msix) {
Matthew Wilcox24d27552009-03-17 08:54:06 -0400314 void __iomem *base;
315 base = entry->mask_base +
316 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
317
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900318 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
319 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
320 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400321 } else {
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700322 struct pci_dev *dev = entry->dev;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600323 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400324 u16 msgctl;
325
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600326 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400327 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
328 msgctl |= entry->msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600329 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700330
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600331 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
332 msg->address_lo);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700333 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600334 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
335 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600336 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
337 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700338 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600339 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
340 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700341 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700342 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700343 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700344}
345
Yinghai Lu3145e942008-12-05 18:58:34 -0800346void write_msi_msg(unsigned int irq, struct msi_msg *msg)
347{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200348 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800349
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200350 __write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800351}
352
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900353static void free_msi_irqs(struct pci_dev *dev)
354{
355 struct msi_desc *entry, *tmp;
356
357 list_for_each_entry(entry, &dev->msi_list, list) {
358 int i, nvec;
359 if (!entry->irq)
360 continue;
Alexander Gordeev65f6ae62013-05-13 11:05:48 +0200361 if (entry->nvec_used)
362 nvec = entry->nvec_used;
363 else
364 nvec = 1 << entry->msi_attrib.multiple;
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900365 for (i = 0; i < nvec; i++)
366 BUG_ON(irq_has_action(entry->irq + i));
367 }
368
369 arch_teardown_msi_irqs(dev);
370
371 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
372 if (entry->msi_attrib.is_msix) {
373 if (list_is_last(&entry->list, &dev->msi_list))
374 iounmap(entry->mask_base);
375 }
Neil Horman424eb392012-01-03 10:29:54 -0500376
377 /*
378 * Its possible that we get into this path
379 * When populate_msi_sysfs fails, which means the entries
380 * were not registered with sysfs. In that case don't
381 * unregister them.
382 */
383 if (entry->kobj.parent) {
384 kobject_del(&entry->kobj);
385 kobject_put(&entry->kobj);
386 }
387
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900388 list_del(&entry->list);
389 kfree(entry);
390 }
391}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900392
Matthew Wilcox379f5322009-03-17 08:54:07 -0400393static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Matthew Wilcox379f5322009-03-17 08:54:07 -0400395 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
396 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return NULL;
398
Matthew Wilcox379f5322009-03-17 08:54:07 -0400399 INIT_LIST_HEAD(&desc->list);
400 desc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Matthew Wilcox379f5322009-03-17 08:54:07 -0400402 return desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
David Millerba698ad2007-10-25 01:16:30 -0700405static void pci_intx_for_msi(struct pci_dev *dev, int enable)
406{
407 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
408 pci_intx(dev, enable);
409}
410
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100411static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800412{
Shaohua Li41017f02006-02-08 17:11:38 +0800413 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700414 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800415
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800416 if (!dev->msi_enabled)
417 return;
418
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200419 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800420
David Millerba698ad2007-10-25 01:16:30 -0700421 pci_intx_for_msi(dev, 0);
Gavin Shane375b562013-04-04 16:54:30 +0000422 msi_set_enable(dev, 0);
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500423 arch_restore_msi_irqs(dev, dev->irq);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700424
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600425 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400426 msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700427 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400428 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600429 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100430}
431
432static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800433{
Shaohua Li41017f02006-02-08 17:11:38 +0800434 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700435 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800436
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700437 if (!dev->msix_enabled)
438 return;
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700439 BUG_ON(list_empty(&dev->msi_list));
Hidetoshi Seto9cc8d542009-08-06 11:32:04 +0900440 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600441 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700442
Shaohua Li41017f02006-02-08 17:11:38 +0800443 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700444 pci_intx_for_msi(dev, 0);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700445 control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600446 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800447
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000448 list_for_each_entry(entry, &dev->msi_list, list) {
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500449 arch_restore_msi_irqs(dev, entry->irq);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400450 msix_mask_irq(entry, entry->masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800451 }
Shaohua Li41017f02006-02-08 17:11:38 +0800452
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700453 control &= ~PCI_MSIX_FLAGS_MASKALL;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600454 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800455}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100456
457void pci_restore_msi_state(struct pci_dev *dev)
458{
459 __pci_restore_msi_state(dev);
460 __pci_restore_msix_state(dev);
461}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600462EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800463
Neil Hormanda8d1c82011-10-06 14:08:18 -0400464
465#define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
466#define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
467
468struct msi_attribute {
469 struct attribute attr;
470 ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
471 char *buf);
472 ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
473 const char *buf, size_t count);
474};
475
476static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
477 char *buf)
478{
479 return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
480}
481
482static ssize_t msi_irq_attr_show(struct kobject *kobj,
483 struct attribute *attr, char *buf)
484{
485 struct msi_attribute *attribute = to_msi_attr(attr);
486 struct msi_desc *entry = to_msi_desc(kobj);
487
488 if (!attribute->show)
489 return -EIO;
490
491 return attribute->show(entry, attribute, buf);
492}
493
494static const struct sysfs_ops msi_irq_sysfs_ops = {
495 .show = msi_irq_attr_show,
496};
497
498static struct msi_attribute mode_attribute =
499 __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
500
501
Bjorn Helgaas9738abe2013-04-12 11:20:03 -0600502static struct attribute *msi_irq_default_attrs[] = {
Neil Hormanda8d1c82011-10-06 14:08:18 -0400503 &mode_attribute.attr,
504 NULL
505};
506
Bjorn Helgaas9738abe2013-04-12 11:20:03 -0600507static void msi_kobj_release(struct kobject *kobj)
Neil Hormanda8d1c82011-10-06 14:08:18 -0400508{
509 struct msi_desc *entry = to_msi_desc(kobj);
510
511 pci_dev_put(entry->dev);
512}
513
514static struct kobj_type msi_irq_ktype = {
515 .release = msi_kobj_release,
516 .sysfs_ops = &msi_irq_sysfs_ops,
517 .default_attrs = msi_irq_default_attrs,
518};
519
520static int populate_msi_sysfs(struct pci_dev *pdev)
521{
522 struct msi_desc *entry;
523 struct kobject *kobj;
524 int ret;
525 int count = 0;
526
527 pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
528 if (!pdev->msi_kset)
529 return -ENOMEM;
530
531 list_for_each_entry(entry, &pdev->msi_list, list) {
532 kobj = &entry->kobj;
533 kobj->kset = pdev->msi_kset;
534 pci_dev_get(pdev);
535 ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
536 "%u", entry->irq);
537 if (ret)
538 goto out_unroll;
539
540 count++;
541 }
542
543 return 0;
544
545out_unroll:
546 list_for_each_entry(entry, &pdev->msi_list, list) {
547 if (!count)
548 break;
549 kobject_del(&entry->kobj);
550 kobject_put(&entry->kobj);
551 count--;
552 }
553 return ret;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/**
557 * msi_capability_init - configure device's MSI capability structure
558 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400559 * @nvec: number of interrupts to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400561 * Setup the MSI capability structure of the device with the requested
562 * number of interrupts. A return value of zero indicates the successful
563 * setup of an entry with the new MSI irq. A negative return value indicates
564 * an error, and a positive return value indicates the number of interrupts
565 * which could have been allocated.
566 */
567static int msi_capability_init(struct pci_dev *dev, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000570 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 u16 control;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400572 unsigned mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Gavin Shane375b562013-04-04 16:54:30 +0000574 msi_set_enable(dev, 0); /* Disable MSI during set up */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600575
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600576 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* MSI Entry Initialization */
Matthew Wilcox379f5322009-03-17 08:54:07 -0400578 entry = alloc_msi_entry(dev);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700579 if (!entry)
580 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700581
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900582 entry->msi_attrib.is_msix = 0;
Bjorn Helgaas4987ce82013-04-17 17:42:30 -0600583 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900584 entry->msi_attrib.entry_nr = 0;
Bjorn Helgaas4987ce82013-04-17 17:42:30 -0600585 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900586 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Gavin Shanf4651362013-04-04 16:54:32 +0000587 entry->msi_attrib.pos = dev->msi_cap;
Hidetoshi Seto0db29af2008-12-24 17:27:04 +0900588
Dan Carpentere5f66ea2013-04-30 10:44:54 +0300589 if (control & PCI_MSI_FLAGS_64BIT)
590 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
591 else
592 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400593 /* All MSIs are unmasked by default, Mask them all */
594 if (entry->msi_attrib.maskbit)
595 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
596 mask = msi_capable_mask(control);
597 msi_mask_irq(entry, mask, mask);
598
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700599 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Configure MSI capability structure */
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400602 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000603 if (ret) {
Hidetoshi Seto7ba19302009-06-23 17:39:27 +0900604 msi_mask_irq(entry, mask, ~mask);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900605 free_msi_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000606 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500607 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700608
Neil Hormanda8d1c82011-10-06 14:08:18 -0400609 ret = populate_msi_sysfs(dev);
610 if (ret) {
611 msi_mask_irq(entry, mask, ~mask);
612 free_msi_irqs(dev);
613 return ret;
614 }
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700617 pci_intx_for_msi(dev, 0);
Gavin Shane375b562013-04-04 16:54:30 +0000618 msi_set_enable(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800619 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Michael Ellerman7fe37302007-04-18 19:39:21 +1000621 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624
Gavin Shan520fe9d2013-04-04 16:54:33 +0000625static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900626{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900627 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900628 u32 table_offset;
629 u8 bir;
630
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600631 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
632 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600633 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
634 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900635 phys_addr = pci_resource_start(dev, bir) + table_offset;
636
637 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
638}
639
Gavin Shan520fe9d2013-04-04 16:54:33 +0000640static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
641 struct msix_entry *entries, int nvec)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900642{
643 struct msi_desc *entry;
644 int i;
645
646 for (i = 0; i < nvec; i++) {
647 entry = alloc_msi_entry(dev);
648 if (!entry) {
649 if (!i)
650 iounmap(base);
651 else
652 free_msi_irqs(dev);
653 /* No enough memory. Don't try again */
654 return -ENOMEM;
655 }
656
657 entry->msi_attrib.is_msix = 1;
658 entry->msi_attrib.is_64 = 1;
659 entry->msi_attrib.entry_nr = entries[i].entry;
660 entry->msi_attrib.default_irq = dev->irq;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000661 entry->msi_attrib.pos = dev->msix_cap;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900662 entry->mask_base = base;
663
664 list_add_tail(&entry->list, &dev->msi_list);
665 }
666
667 return 0;
668}
669
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900670static void msix_program_entries(struct pci_dev *dev,
Gavin Shan520fe9d2013-04-04 16:54:33 +0000671 struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900672{
673 struct msi_desc *entry;
674 int i = 0;
675
676 list_for_each_entry(entry, &dev->msi_list, list) {
677 int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
678 PCI_MSIX_ENTRY_VECTOR_CTRL;
679
680 entries[i].vector = entry->irq;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200681 irq_set_msi_desc(entry->irq, entry);
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
Gavin Shan520fe9d2013-04-04 16:54:33 +0000705 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700706
707 /* Ensure MSI-X is disabled while it is set up */
708 control &= ~PCI_MSIX_FLAGS_ENABLE;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000709 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Request & Map MSI-X table region */
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600712 base = msix_map_region(dev, msix_table_size(control));
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900713 if (!base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -ENOMEM;
715
Gavin Shan520fe9d2013-04-04 16:54:33 +0000716 ret = msix_setup_entries(dev, base, entries, nvec);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900717 if (ret)
718 return ret;
Michael Ellerman9c831332007-04-18 19:39:21 +1000719
720 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900721 if (ret)
722 goto error;
Michael Ellerman9c831332007-04-18 19:39:21 +1000723
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700724 /*
725 * Some devices require MSI-X to be enabled before we can touch the
726 * MSI-X registers. We need to mask all the vectors to prevent
727 * interrupts coming in before they're fully set up.
728 */
729 control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000730 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700731
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900732 msix_program_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700733
Neil Hormanda8d1c82011-10-06 14:08:18 -0400734 ret = populate_msi_sysfs(dev);
735 if (ret) {
736 ret = 0;
737 goto error;
738 }
739
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700740 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700741 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800742 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700744 control &= ~PCI_MSIX_FLAGS_MASKALL;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000745 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900748
749error:
750 if (ret < 0) {
751 /*
752 * If we had some success, report the number of irqs
753 * we succeeded in setting up.
754 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900755 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900756 int avail = 0;
757
758 list_for_each_entry(entry, &dev->msi_list, list) {
759 if (entry->irq != 0)
760 avail++;
761 }
762 if (avail != 0)
763 ret = avail;
764 }
765
766 free_msi_irqs(dev);
767
768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000772 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400773 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000774 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100775 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400776 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200777 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000778 * to determine if MSI/-X are supported for the device. If MSI/-X is
779 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400780 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900781static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400782{
783 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000784 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400785
Brice Goglin0306ebf2006-10-05 10:24:31 +0200786 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400787 if (!pci_msi_enable || !dev || dev->no_msi)
788 return -EINVAL;
789
Michael Ellerman314e77b2007-04-05 17:19:12 +1000790 /*
791 * You can't ask to have 0 or less MSIs configured.
792 * a) it's stupid ..
793 * b) the list manipulation code assumes nvec >= 1.
794 */
795 if (nvec < 1)
796 return -ERANGE;
797
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900798 /*
799 * Any bridge which does NOT route MSI transactions from its
800 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200801 * the secondary pci_bus.
802 * We expect only arch-specific PCI host bus controller driver
803 * or quirks for specific PCI bridges to be setting NO_MSI.
804 */
Brice Goglin24334a12006-08-31 01:55:07 -0400805 for (bus = dev->bus; bus; bus = bus->parent)
806 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
807 return -EINVAL;
808
Michael Ellermanc9953a72007-04-05 17:19:08 +1000809 ret = arch_msi_check_device(dev, nvec, type);
810 if (ret)
811 return ret;
812
Brice Goglin24334a12006-08-31 01:55:07 -0400813 return 0;
814}
815
816/**
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400817 * pci_enable_msi_block - configure device's MSI capability structure
818 * @dev: device to configure
819 * @nvec: number of interrupts to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400821 * Allocate IRQs for a device with the MSI capability.
822 * This function returns a negative errno if an error occurs. If it
823 * is unable to allocate the number of interrupts requested, it returns
824 * the number of interrupts it might be able to allocate. If it successfully
825 * allocates at least the number of interrupts requested, it returns 0 and
826 * updates the @dev's irq member to the lowest new interrupt number; the
827 * other interrupt numbers allocated to this device are consecutive.
828 */
829int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Gavin Shanf4651362013-04-04 16:54:32 +0000831 int status, maxvec;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400832 u16 msgctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Gavin Shanf4651362013-04-04 16:54:32 +0000834 if (!dev->msi_cap)
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400835 return -EINVAL;
Gavin Shanf4651362013-04-04 16:54:32 +0000836
837 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400838 maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
839 if (nvec > maxvec)
840 return maxvec;
841
842 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellermanc9953a72007-04-05 17:19:08 +1000843 if (status)
844 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700846 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400848 /* Check whether driver already requested MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800849 if (dev->msix_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600850 dev_info(&dev->dev, "can't enable MSI "
851 "(MSI-X already enabled)\n");
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800852 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400854
855 status = msi_capability_init(dev, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return status;
857}
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400858EXPORT_SYMBOL(pci_enable_msi_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Alexander Gordeev08261d82012-11-19 16:02:10 +0100860int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec)
861{
Gavin Shanf4651362013-04-04 16:54:32 +0000862 int ret, nvec;
Alexander Gordeev08261d82012-11-19 16:02:10 +0100863 u16 msgctl;
864
Gavin Shanf4651362013-04-04 16:54:32 +0000865 if (!dev->msi_cap)
Alexander Gordeev08261d82012-11-19 16:02:10 +0100866 return -EINVAL;
867
Gavin Shanf4651362013-04-04 16:54:32 +0000868 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
Alexander Gordeev08261d82012-11-19 16:02:10 +0100869 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
870
871 if (maxvec)
872 *maxvec = ret;
873
874 do {
875 nvec = ret;
876 ret = pci_enable_msi_block(dev, nvec);
877 } while (ret > 0);
878
879 if (ret < 0)
880 return ret;
881 return nvec;
882}
883EXPORT_SYMBOL(pci_enable_msi_block_auto);
884
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400885void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400887 struct msi_desc *desc;
888 u32 mask;
889 u16 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100891 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700892 return;
893
Matthew Wilcox110828c2009-06-16 06:31:45 -0600894 BUG_ON(list_empty(&dev->msi_list));
895 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600896
Gavin Shane375b562013-04-04 16:54:30 +0000897 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700898 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800899 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700900
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900901 /* Return the device with MSI unmasked as initial states */
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600902 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400903 mask = msi_capable_mask(ctrl);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900904 /* Keep cached state to be restored */
905 __msi_mask_irq(desc, mask, ~mask);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100906
907 /* Restore dev->irq to its default pin-assertion irq */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400908 dev->irq = desc->msi_attrib.default_irq;
Yinghai Lud52877c2008-04-23 14:58:09 -0700909}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400910
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900911void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700912{
Yinghai Lud52877c2008-04-23 14:58:09 -0700913 if (!pci_msi_enable || !dev || !dev->msi_enabled)
914 return;
915
916 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900917 free_msi_irqs(dev);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400918 kset_unregister(dev->msi_kset);
919 dev->msi_kset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100921EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923/**
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100924 * pci_msix_table_size - return the number of device's MSI-X table entries
925 * @dev: pointer to the pci_dev data structure of MSI-X device function
926 */
927int pci_msix_table_size(struct pci_dev *dev)
928{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100929 u16 control;
930
Gavin Shan520fe9d2013-04-04 16:54:33 +0000931 if (!dev->msix_cap)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100932 return 0;
933
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600934 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600935 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100936}
937
938/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * pci_enable_msix - configure device's MSI-X capability structure
940 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700941 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700942 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
944 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700945 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * MSI-X mode enabled on its hardware device function. A return of zero
947 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700948 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * Or a return of > 0 indicates that driver request is exceeding the number
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300950 * of irqs or MSI-X vectors available. Driver should use the returned value to
951 * re-send its request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900953int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100955 int status, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700956 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Gavin Shancdf1fd42013-04-04 16:54:31 +0000958 if (!entries || !dev->msix_cap)
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900959 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Michael Ellermanc9953a72007-04-05 17:19:08 +1000961 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
962 if (status)
963 return status;
964
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100965 nr_entries = pci_msix_table_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (nvec > nr_entries)
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300967 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* Check for any invalid entries */
970 for (i = 0; i < nvec; i++) {
971 if (entries[i].entry >= nr_entries)
972 return -EINVAL; /* invalid entry */
973 for (j = i + 1; j < nvec; j++) {
974 if (entries[i].entry == entries[j].entry)
975 return -EINVAL; /* duplicate entry */
976 }
977 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700978 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700979
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700980 /* Check whether driver already requested for MSI irq */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900981 if (dev->msi_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600982 dev_info(&dev->dev, "can't enable MSI-X "
983 "(MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EINVAL;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return status;
988}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100989EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900991void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100992{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900993 struct msi_desc *entry;
994
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100995 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700996 return;
997
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900998 /* Return the device with MSI-X masked as initial states */
999 list_for_each_entry(entry, &dev->msi_list, list) {
1000 /* Keep cached states to be restored */
1001 __msix_mask_irq(entry, 1);
1002 }
1003
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -08001004 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -07001005 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -08001006 dev->msix_enabled = 0;
Yinghai Lud52877c2008-04-23 14:58:09 -07001007}
Hidetoshi Setoc9018512009-08-06 11:31:27 +09001008
Hidetoshi Seto500559a2009-08-10 10:14:15 +09001009void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -07001010{
1011 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1012 return;
1013
1014 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +09001015 free_msi_irqs(dev);
Neil Hormanda8d1c82011-10-06 14:08:18 -04001016 kset_unregister(dev->msi_kset);
1017 dev->msi_kset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Michael Ellerman4cc086f2007-03-22 21:51:34 +11001019EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001022 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1024 *
Steven Coleeaae4b32005-05-03 18:38:30 -06001025 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001026 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * allocated for this device function, are reclaimed to unused state,
1028 * which may be used later on.
1029 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +09001030void msi_remove_pci_irq_vectors(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!pci_msi_enable || !dev)
Hidetoshi Seto500559a2009-08-10 10:14:15 +09001033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Hidetoshi Setof56e4482009-08-06 11:32:51 +09001035 if (dev->msi_enabled || dev->msix_enabled)
1036 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
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
Michael Ellerman4aa9bc92007-04-05 17:19:10 +10001056void pci_msi_init_pci_dev(struct pci_dev *dev)
1057{
1058 INIT_LIST_HEAD(&dev->msi_list);
Eric W. Biedermand5dea7d2011-10-17 11:46:06 -07001059
1060 /* Disable the msi hardware to avoid screaming interrupts
1061 * during boot. This is the power on reset default so
1062 * usually this should be a noop.
1063 */
Gavin Shane375b562013-04-04 16:54:30 +00001064 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1065 if (dev->msi_cap)
1066 msi_set_enable(dev, 0);
1067
1068 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1069 if (dev->msix_cap)
1070 msix_set_enable(dev, 0);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +10001071}