blob: 82de95ec2ea09504211025351c37a8d17852a510 [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"
25#include "msi.h"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010029/* Arch hooks */
30
Michael Ellerman11df1f02009-01-19 11:31:00 +110031#ifndef arch_msi_check_device
32int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010033{
34 return 0;
35}
Michael Ellerman11df1f02009-01-19 11:31:00 +110036#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010037
Michael Ellerman11df1f02009-01-19 11:31:00 +110038#ifndef arch_setup_msi_irqs
Thomas Gleixner1525bf02010-10-06 16:05:35 -040039# define arch_setup_msi_irqs default_setup_msi_irqs
40# define HAVE_DEFAULT_MSI_SETUP_IRQS
41#endif
42
43#ifdef HAVE_DEFAULT_MSI_SETUP_IRQS
44int default_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010045{
46 struct msi_desc *entry;
47 int ret;
48
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040049 /*
50 * If an architecture wants to support multiple MSI, it needs to
51 * override arch_setup_msi_irqs()
52 */
53 if (type == PCI_CAP_ID_MSI && nvec > 1)
54 return 1;
55
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010056 list_for_each_entry(entry, &dev->msi_list, list) {
57 ret = arch_setup_msi_irq(dev, entry);
Michael Ellermanb5fbf532009-02-11 22:27:02 +110058 if (ret < 0)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010059 return ret;
Michael Ellermanb5fbf532009-02-11 22:27:02 +110060 if (ret > 0)
61 return -ENOSPC;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010062 }
63
64 return 0;
65}
Michael Ellerman11df1f02009-01-19 11:31:00 +110066#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010067
Michael Ellerman11df1f02009-01-19 11:31:00 +110068#ifndef arch_teardown_msi_irqs
Thomas Gleixner1525bf02010-10-06 16:05:35 -040069# define arch_teardown_msi_irqs default_teardown_msi_irqs
70# define HAVE_DEFAULT_MSI_TEARDOWN_IRQS
71#endif
72
73#ifdef HAVE_DEFAULT_MSI_TEARDOWN_IRQS
74void default_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010075{
76 struct msi_desc *entry;
77
78 list_for_each_entry(entry, &dev->msi_list, list) {
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040079 int i, nvec;
80 if (entry->irq == 0)
81 continue;
82 nvec = 1 << entry->msi_attrib.multiple;
83 for (i = 0; i < nvec; i++)
84 arch_teardown_msi_irq(entry->irq + i);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010085 }
86}
Michael Ellerman11df1f02009-01-19 11:31:00 +110087#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010088
Matthew Wilcox110828c2009-06-16 06:31:45 -060089static void msi_set_enable(struct pci_dev *dev, int pos, int enable)
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080090{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080091 u16 control;
92
Matthew Wilcox110828c2009-06-16 06:31:45 -060093 BUG_ON(!pos);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080094
Matthew Wilcox110828c2009-06-16 06:31:45 -060095 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
96 control &= ~PCI_MSI_FLAGS_ENABLE;
97 if (enable)
98 control |= PCI_MSI_FLAGS_ENABLE;
99 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +0900100}
101
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800102static void msix_set_enable(struct pci_dev *dev, int enable)
103{
104 int pos;
105 u16 control;
106
107 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
108 if (pos) {
109 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
110 control &= ~PCI_MSIX_FLAGS_ENABLE;
111 if (enable)
112 control |= PCI_MSIX_FLAGS_ENABLE;
113 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
114 }
115}
116
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500117static inline __attribute_const__ u32 msi_mask(unsigned x)
118{
Matthew Wilcox0b49ec32009-02-08 20:27:47 -0700119 /* Don't shift by >= width of type */
120 if (x >= 5)
121 return 0xffffffff;
122 return (1 << (1 << x)) - 1;
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500123}
124
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400125static inline __attribute_const__ u32 msi_capable_mask(u16 control)
Mitch Williams988cbb12007-03-30 11:54:08 -0700126{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400127 return msi_mask((control >> 1) & 7);
128}
Mitch Williams988cbb12007-03-30 11:54:08 -0700129
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400130static inline __attribute_const__ u32 msi_enabled_mask(u16 control)
131{
132 return msi_mask((control >> 4) & 7);
Mitch Williams988cbb12007-03-30 11:54:08 -0700133}
134
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600135/*
136 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
137 * mask all MSI interrupts by clearing the MSI enable bit does not work
138 * reliably as devices without an INTx disable bit will then generate a
139 * level IRQ which will never be cleared.
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600140 */
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900141static u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400143 u32 mask_bits = desc->masked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400145 if (!desc->msi_attrib.maskbit)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900146 return 0;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400147
148 mask_bits &= ~mask;
149 mask_bits |= flag;
150 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900151
152 return mask_bits;
153}
154
155static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
156{
157 desc->masked = __msi_mask_irq(desc, mask, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400158}
159
160/*
161 * This internal function does not flush PCI writes to the device.
162 * All users must ensure that they read from the device before either
163 * assuming that the device state is up to date, or returning out of this
164 * file. This saves a few milliseconds when initialising devices with lots
165 * of MSI-X interrupts.
166 */
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900167static u32 __msix_mask_irq(struct msi_desc *desc, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400168{
169 u32 mask_bits = desc->masked;
170 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900171 PCI_MSIX_ENTRY_VECTOR_CTRL;
Sheng Yang8d805282010-11-11 15:46:55 +0800172 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
173 if (flag)
174 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400175 writel(mask_bits, desc->mask_base + offset);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900176
177 return mask_bits;
178}
179
180static void msix_mask_irq(struct msi_desc *desc, u32 flag)
181{
182 desc->masked = __msix_mask_irq(desc, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400183}
184
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200185static void msi_set_mask_bit(struct irq_data *data, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400186{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200187 struct msi_desc *desc = irq_data_get_msi(data);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400188
189 if (desc->msi_attrib.is_msix) {
190 msix_mask_irq(desc, flag);
191 readl(desc->mask_base); /* Flush write to device */
Matthew Wilcox24d27552009-03-17 08:54:06 -0400192 } else {
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200193 unsigned offset = data->irq - desc->dev->irq;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400194 msi_mask_irq(desc, 1 << offset, flag << offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400196}
197
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200198void mask_msi_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400199{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200200 msi_set_mask_bit(data, 1);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400201}
202
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200203void unmask_msi_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400204{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200205 msi_set_mask_bit(data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200208void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700209{
Ben Hutchings30da5522010-07-23 14:56:28 +0100210 BUG_ON(entry->dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700211
Ben Hutchings30da5522010-07-23 14:56:28 +0100212 if (entry->msi_attrib.is_msix) {
213 void __iomem *base = entry->mask_base +
214 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
215
216 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
217 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
218 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
219 } else {
220 struct pci_dev *dev = entry->dev;
221 int pos = entry->msi_attrib.pos;
222 u16 data;
223
224 pci_read_config_dword(dev, msi_lower_address_reg(pos),
225 &msg->address_lo);
226 if (entry->msi_attrib.is_64) {
227 pci_read_config_dword(dev, msi_upper_address_reg(pos),
228 &msg->address_hi);
229 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
230 } else {
231 msg->address_hi = 0;
232 pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
233 }
234 msg->data = data;
235 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700236}
237
Yinghai Lu3145e942008-12-05 18:58:34 -0800238void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700239{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200240 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800241
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200242 __read_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800243}
244
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200245void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Ben Hutchings30da5522010-07-23 14:56:28 +0100246{
Ben Hutchings30da5522010-07-23 14:56:28 +0100247 /* Assert that the cache is valid, assuming that
248 * valid messages are not all-zeroes. */
249 BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
250 entry->msg.data));
251
252 *msg = entry->msg;
253}
254
255void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
256{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200257 struct msi_desc *entry = irq_get_msi_desc(irq);
Ben Hutchings30da5522010-07-23 14:56:28 +0100258
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200259 __get_cached_msi_msg(entry, msg);
Ben Hutchings30da5522010-07-23 14:56:28 +0100260}
261
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200262void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800263{
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100264 if (entry->dev->current_state != PCI_D0) {
265 /* Don't touch the hardware now */
266 } else if (entry->msi_attrib.is_msix) {
Matthew Wilcox24d27552009-03-17 08:54:06 -0400267 void __iomem *base;
268 base = entry->mask_base +
269 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
270
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900271 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
272 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
273 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400274 } else {
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700275 struct pci_dev *dev = entry->dev;
276 int pos = entry->msi_attrib.pos;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400277 u16 msgctl;
278
279 pci_read_config_word(dev, msi_control_reg(pos), &msgctl);
280 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
281 msgctl |= entry->msi_attrib.multiple << 4;
282 pci_write_config_word(dev, msi_control_reg(pos), msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700283
284 pci_write_config_dword(dev, msi_lower_address_reg(pos),
285 msg->address_lo);
286 if (entry->msi_attrib.is_64) {
287 pci_write_config_dword(dev, msi_upper_address_reg(pos),
288 msg->address_hi);
289 pci_write_config_word(dev, msi_data_reg(pos, 1),
290 msg->data);
291 } else {
292 pci_write_config_word(dev, msi_data_reg(pos, 0),
293 msg->data);
294 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700295 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700296 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700297}
298
Yinghai Lu3145e942008-12-05 18:58:34 -0800299void write_msi_msg(unsigned int irq, struct msi_msg *msg)
300{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200301 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800302
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200303 __write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800304}
305
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900306static void free_msi_irqs(struct pci_dev *dev)
307{
308 struct msi_desc *entry, *tmp;
309
310 list_for_each_entry(entry, &dev->msi_list, list) {
311 int i, nvec;
312 if (!entry->irq)
313 continue;
314 nvec = 1 << entry->msi_attrib.multiple;
315 for (i = 0; i < nvec; i++)
316 BUG_ON(irq_has_action(entry->irq + i));
317 }
318
319 arch_teardown_msi_irqs(dev);
320
321 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
322 if (entry->msi_attrib.is_msix) {
323 if (list_is_last(&entry->list, &dev->msi_list))
324 iounmap(entry->mask_base);
325 }
Neil Horman424eb392012-01-03 10:29:54 -0500326
327 /*
328 * Its possible that we get into this path
329 * When populate_msi_sysfs fails, which means the entries
330 * were not registered with sysfs. In that case don't
331 * unregister them.
332 */
333 if (entry->kobj.parent) {
334 kobject_del(&entry->kobj);
335 kobject_put(&entry->kobj);
336 }
337
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900338 list_del(&entry->list);
339 kfree(entry);
340 }
341}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900342
Matthew Wilcox379f5322009-03-17 08:54:07 -0400343static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Matthew Wilcox379f5322009-03-17 08:54:07 -0400345 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
346 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return NULL;
348
Matthew Wilcox379f5322009-03-17 08:54:07 -0400349 INIT_LIST_HEAD(&desc->list);
350 desc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Matthew Wilcox379f5322009-03-17 08:54:07 -0400352 return desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
David Millerba698ad2007-10-25 01:16:30 -0700355static void pci_intx_for_msi(struct pci_dev *dev, int enable)
356{
357 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
358 pci_intx(dev, enable);
359}
360
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100361static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800362{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700363 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800364 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700365 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800366
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800367 if (!dev->msi_enabled)
368 return;
369
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200370 entry = irq_get_msi_desc(dev->irq);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700371 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800372
David Millerba698ad2007-10-25 01:16:30 -0700373 pci_intx_for_msi(dev, 0);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600374 msi_set_enable(dev, pos, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700375 write_msi_msg(dev->irq, &entry->msg);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700376
377 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400378 msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700379 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400380 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800381 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100382}
383
384static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800385{
Shaohua Li41017f02006-02-08 17:11:38 +0800386 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800387 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700388 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800389
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700390 if (!dev->msix_enabled)
391 return;
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700392 BUG_ON(list_empty(&dev->msi_list));
Hidetoshi Seto9cc8d542009-08-06 11:32:04 +0900393 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700394 pos = entry->msi_attrib.pos;
395 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700396
Shaohua Li41017f02006-02-08 17:11:38 +0800397 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700398 pci_intx_for_msi(dev, 0);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700399 control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
400 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800401
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000402 list_for_each_entry(entry, &dev->msi_list, list) {
403 write_msi_msg(entry->irq, &entry->msg);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400404 msix_mask_irq(entry, entry->masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800405 }
Shaohua Li41017f02006-02-08 17:11:38 +0800406
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700407 control &= ~PCI_MSIX_FLAGS_MASKALL;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700408 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800409}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100410
411void pci_restore_msi_state(struct pci_dev *dev)
412{
413 __pci_restore_msi_state(dev);
414 __pci_restore_msix_state(dev);
415}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600416EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800417
Neil Hormanda8d1c82011-10-06 14:08:18 -0400418
419#define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
420#define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
421
422struct msi_attribute {
423 struct attribute attr;
424 ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
425 char *buf);
426 ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
427 const char *buf, size_t count);
428};
429
430static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
431 char *buf)
432{
433 return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
434}
435
436static ssize_t msi_irq_attr_show(struct kobject *kobj,
437 struct attribute *attr, char *buf)
438{
439 struct msi_attribute *attribute = to_msi_attr(attr);
440 struct msi_desc *entry = to_msi_desc(kobj);
441
442 if (!attribute->show)
443 return -EIO;
444
445 return attribute->show(entry, attribute, buf);
446}
447
448static const struct sysfs_ops msi_irq_sysfs_ops = {
449 .show = msi_irq_attr_show,
450};
451
452static struct msi_attribute mode_attribute =
453 __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
454
455
456struct attribute *msi_irq_default_attrs[] = {
457 &mode_attribute.attr,
458 NULL
459};
460
461void msi_kobj_release(struct kobject *kobj)
462{
463 struct msi_desc *entry = to_msi_desc(kobj);
464
465 pci_dev_put(entry->dev);
466}
467
468static struct kobj_type msi_irq_ktype = {
469 .release = msi_kobj_release,
470 .sysfs_ops = &msi_irq_sysfs_ops,
471 .default_attrs = msi_irq_default_attrs,
472};
473
474static int populate_msi_sysfs(struct pci_dev *pdev)
475{
476 struct msi_desc *entry;
477 struct kobject *kobj;
478 int ret;
479 int count = 0;
480
481 pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
482 if (!pdev->msi_kset)
483 return -ENOMEM;
484
485 list_for_each_entry(entry, &pdev->msi_list, list) {
486 kobj = &entry->kobj;
487 kobj->kset = pdev->msi_kset;
488 pci_dev_get(pdev);
489 ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
490 "%u", entry->irq);
491 if (ret)
492 goto out_unroll;
493
494 count++;
495 }
496
497 return 0;
498
499out_unroll:
500 list_for_each_entry(entry, &pdev->msi_list, list) {
501 if (!count)
502 break;
503 kobject_del(&entry->kobj);
504 kobject_put(&entry->kobj);
505 count--;
506 }
507 return ret;
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/**
511 * msi_capability_init - configure device's MSI capability structure
512 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400513 * @nvec: number of interrupts to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400515 * Setup the MSI capability structure of the device with the requested
516 * number of interrupts. A return value of zero indicates the successful
517 * setup of an entry with the new MSI irq. A negative return value indicates
518 * an error, and a positive return value indicates the number of interrupts
519 * which could have been allocated.
520 */
521static int msi_capability_init(struct pci_dev *dev, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000524 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 u16 control;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400526 unsigned mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900528 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600529 msi_set_enable(dev, pos, 0); /* Disable MSI during set up */
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pci_read_config_word(dev, msi_control_reg(pos), &control);
532 /* MSI Entry Initialization */
Matthew Wilcox379f5322009-03-17 08:54:07 -0400533 entry = alloc_msi_entry(dev);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700534 if (!entry)
535 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700536
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900537 entry->msi_attrib.is_msix = 0;
538 entry->msi_attrib.is_64 = is_64bit_address(control);
539 entry->msi_attrib.entry_nr = 0;
540 entry->msi_attrib.maskbit = is_mask_bit_support(control);
541 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
542 entry->msi_attrib.pos = pos;
Hidetoshi Seto0db29af2008-12-24 17:27:04 +0900543
Hidetoshi Seto67b5db62009-04-20 10:54:59 +0900544 entry->mask_pos = msi_mask_reg(pos, entry->msi_attrib.is_64);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400545 /* All MSIs are unmasked by default, Mask them all */
546 if (entry->msi_attrib.maskbit)
547 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
548 mask = msi_capable_mask(control);
549 msi_mask_irq(entry, mask, mask);
550
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700551 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Configure MSI capability structure */
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400554 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000555 if (ret) {
Hidetoshi Seto7ba19302009-06-23 17:39:27 +0900556 msi_mask_irq(entry, mask, ~mask);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900557 free_msi_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000558 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500559 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700560
Neil Hormanda8d1c82011-10-06 14:08:18 -0400561 ret = populate_msi_sysfs(dev);
562 if (ret) {
563 msi_mask_irq(entry, mask, ~mask);
564 free_msi_irqs(dev);
565 return ret;
566 }
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700569 pci_intx_for_msi(dev, 0);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600570 msi_set_enable(dev, pos, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800571 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Michael Ellerman7fe37302007-04-18 19:39:21 +1000573 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
575}
576
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900577static void __iomem *msix_map_region(struct pci_dev *dev, unsigned pos,
578 unsigned nr_entries)
579{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900580 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900581 u32 table_offset;
582 u8 bir;
583
584 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
585 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
586 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
587 phys_addr = pci_resource_start(dev, bir) + table_offset;
588
589 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
590}
591
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900592static int msix_setup_entries(struct pci_dev *dev, unsigned pos,
593 void __iomem *base, struct msix_entry *entries,
594 int nvec)
595{
596 struct msi_desc *entry;
597 int i;
598
599 for (i = 0; i < nvec; i++) {
600 entry = alloc_msi_entry(dev);
601 if (!entry) {
602 if (!i)
603 iounmap(base);
604 else
605 free_msi_irqs(dev);
606 /* No enough memory. Don't try again */
607 return -ENOMEM;
608 }
609
610 entry->msi_attrib.is_msix = 1;
611 entry->msi_attrib.is_64 = 1;
612 entry->msi_attrib.entry_nr = entries[i].entry;
613 entry->msi_attrib.default_irq = dev->irq;
614 entry->msi_attrib.pos = pos;
615 entry->mask_base = base;
616
617 list_add_tail(&entry->list, &dev->msi_list);
618 }
619
620 return 0;
621}
622
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900623static void msix_program_entries(struct pci_dev *dev,
624 struct msix_entry *entries)
625{
626 struct msi_desc *entry;
627 int i = 0;
628
629 list_for_each_entry(entry, &dev->msi_list, list) {
630 int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
631 PCI_MSIX_ENTRY_VECTOR_CTRL;
632
633 entries[i].vector = entry->irq;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200634 irq_set_msi_desc(entry->irq, entry);
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900635 entry->masked = readl(entry->mask_base + offset);
636 msix_mask_irq(entry, 1);
637 i++;
638 }
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/**
642 * msix_capability_init - configure device's MSI-X capability
643 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700644 * @entries: pointer to an array of struct msix_entry entries
645 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600647 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700648 * single MSI-X irq. A return of zero indicates the successful setup of
649 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 **/
651static int msix_capability_init(struct pci_dev *dev,
652 struct msix_entry *entries, int nvec)
653{
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900654 int pos, ret;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900655 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 void __iomem *base;
657
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900658 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700659 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
660
661 /* Ensure MSI-X is disabled while it is set up */
662 control &= ~PCI_MSIX_FLAGS_ENABLE;
663 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Request & Map MSI-X table region */
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900666 base = msix_map_region(dev, pos, multi_msix_capable(control));
667 if (!base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -ENOMEM;
669
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900670 ret = msix_setup_entries(dev, pos, base, entries, nvec);
671 if (ret)
672 return ret;
Michael Ellerman9c831332007-04-18 19:39:21 +1000673
674 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900675 if (ret)
676 goto error;
Michael Ellerman9c831332007-04-18 19:39:21 +1000677
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700678 /*
679 * Some devices require MSI-X to be enabled before we can touch the
680 * MSI-X registers. We need to mask all the vectors to prevent
681 * interrupts coming in before they're fully set up.
682 */
683 control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
684 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
685
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900686 msix_program_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700687
Neil Hormanda8d1c82011-10-06 14:08:18 -0400688 ret = populate_msi_sysfs(dev);
689 if (ret) {
690 ret = 0;
691 goto error;
692 }
693
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700694 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700695 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800696 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700698 control &= ~PCI_MSIX_FLAGS_MASKALL;
699 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900702
703error:
704 if (ret < 0) {
705 /*
706 * If we had some success, report the number of irqs
707 * we succeeded in setting up.
708 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900709 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900710 int avail = 0;
711
712 list_for_each_entry(entry, &dev->msi_list, list) {
713 if (entry->irq != 0)
714 avail++;
715 }
716 if (avail != 0)
717 ret = avail;
718 }
719
720 free_msi_irqs(dev);
721
722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000726 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400727 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000728 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100729 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400730 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200731 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000732 * to determine if MSI/-X are supported for the device. If MSI/-X is
733 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400734 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900735static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400736{
737 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000738 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400739
Brice Goglin0306ebf2006-10-05 10:24:31 +0200740 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400741 if (!pci_msi_enable || !dev || dev->no_msi)
742 return -EINVAL;
743
Michael Ellerman314e77b2007-04-05 17:19:12 +1000744 /*
745 * You can't ask to have 0 or less MSIs configured.
746 * a) it's stupid ..
747 * b) the list manipulation code assumes nvec >= 1.
748 */
749 if (nvec < 1)
750 return -ERANGE;
751
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900752 /*
753 * Any bridge which does NOT route MSI transactions from its
754 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200755 * the secondary pci_bus.
756 * We expect only arch-specific PCI host bus controller driver
757 * or quirks for specific PCI bridges to be setting NO_MSI.
758 */
Brice Goglin24334a12006-08-31 01:55:07 -0400759 for (bus = dev->bus; bus; bus = bus->parent)
760 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
761 return -EINVAL;
762
Michael Ellermanc9953a72007-04-05 17:19:08 +1000763 ret = arch_msi_check_device(dev, nvec, type);
764 if (ret)
765 return ret;
766
Michael Ellermanb1e23032007-03-22 21:51:39 +1100767 if (!pci_find_capability(dev, type))
768 return -EINVAL;
769
Brice Goglin24334a12006-08-31 01:55:07 -0400770 return 0;
771}
772
773/**
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400774 * pci_enable_msi_block - configure device's MSI capability structure
775 * @dev: device to configure
776 * @nvec: number of interrupts to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400778 * Allocate IRQs for a device with the MSI capability.
779 * This function returns a negative errno if an error occurs. If it
780 * is unable to allocate the number of interrupts requested, it returns
781 * the number of interrupts it might be able to allocate. If it successfully
782 * allocates at least the number of interrupts requested, it returns 0 and
783 * updates the @dev's irq member to the lowest new interrupt number; the
784 * other interrupt numbers allocated to this device are consecutive.
785 */
786int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400788 int status, pos, maxvec;
789 u16 msgctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400791 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
792 if (!pos)
793 return -EINVAL;
794 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
795 maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
796 if (nvec > maxvec)
797 return maxvec;
798
799 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellermanc9953a72007-04-05 17:19:08 +1000800 if (status)
801 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700803 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400805 /* Check whether driver already requested MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800806 if (dev->msix_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600807 dev_info(&dev->dev, "can't enable MSI "
808 "(MSI-X already enabled)\n");
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400811
812 status = msi_capability_init(dev, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return status;
814}
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400815EXPORT_SYMBOL(pci_enable_msi_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400817void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400819 struct msi_desc *desc;
820 u32 mask;
821 u16 ctrl;
Matthew Wilcox110828c2009-06-16 06:31:45 -0600822 unsigned pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100824 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700825 return;
826
Matthew Wilcox110828c2009-06-16 06:31:45 -0600827 BUG_ON(list_empty(&dev->msi_list));
828 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
829 pos = desc->msi_attrib.pos;
830
831 msi_set_enable(dev, pos, 0);
David Millerba698ad2007-10-25 01:16:30 -0700832 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800833 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700834
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900835 /* Return the device with MSI unmasked as initial states */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600836 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &ctrl);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400837 mask = msi_capable_mask(ctrl);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900838 /* Keep cached state to be restored */
839 __msi_mask_irq(desc, mask, ~mask);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100840
841 /* Restore dev->irq to its default pin-assertion irq */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400842 dev->irq = desc->msi_attrib.default_irq;
Yinghai Lud52877c2008-04-23 14:58:09 -0700843}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400844
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900845void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700846{
Yinghai Lud52877c2008-04-23 14:58:09 -0700847 if (!pci_msi_enable || !dev || !dev->msi_enabled)
848 return;
849
850 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900851 free_msi_irqs(dev);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400852 kset_unregister(dev->msi_kset);
853 dev->msi_kset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100855EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/**
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100858 * pci_msix_table_size - return the number of device's MSI-X table entries
859 * @dev: pointer to the pci_dev data structure of MSI-X device function
860 */
861int pci_msix_table_size(struct pci_dev *dev)
862{
863 int pos;
864 u16 control;
865
866 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
867 if (!pos)
868 return 0;
869
870 pci_read_config_word(dev, msi_control_reg(pos), &control);
871 return multi_msix_capable(control);
872}
873
874/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * pci_enable_msix - configure device's MSI-X capability structure
876 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700877 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700878 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *
880 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700881 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * MSI-X mode enabled on its hardware device function. A return of zero
883 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700884 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * Or a return of > 0 indicates that driver request is exceeding the number
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300886 * of irqs or MSI-X vectors available. Driver should use the returned value to
887 * re-send its request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900889int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100891 int status, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700892 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Michael Ellermanc9953a72007-04-05 17:19:08 +1000894 if (!entries)
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900895 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Michael Ellermanc9953a72007-04-05 17:19:08 +1000897 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
898 if (status)
899 return status;
900
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100901 nr_entries = pci_msix_table_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (nvec > nr_entries)
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300903 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* Check for any invalid entries */
906 for (i = 0; i < nvec; i++) {
907 if (entries[i].entry >= nr_entries)
908 return -EINVAL; /* invalid entry */
909 for (j = i + 1; j < nvec; j++) {
910 if (entries[i].entry == entries[j].entry)
911 return -EINVAL; /* duplicate entry */
912 }
913 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700914 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700915
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700916 /* Check whether driver already requested for MSI irq */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900917 if (dev->msi_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600918 dev_info(&dev->dev, "can't enable MSI-X "
919 "(MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EINVAL;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return status;
924}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100925EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900927void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100928{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900929 struct msi_desc *entry;
930
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100931 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700932 return;
933
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900934 /* Return the device with MSI-X masked as initial states */
935 list_for_each_entry(entry, &dev->msi_list, list) {
936 /* Keep cached states to be restored */
937 __msix_mask_irq(entry, 1);
938 }
939
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800940 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700941 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800942 dev->msix_enabled = 0;
Yinghai Lud52877c2008-04-23 14:58:09 -0700943}
Hidetoshi Setoc9018512009-08-06 11:31:27 +0900944
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900945void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700946{
947 if (!pci_msi_enable || !dev || !dev->msix_enabled)
948 return;
949
950 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900951 free_msi_irqs(dev);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400952 kset_unregister(dev->msi_kset);
953 dev->msi_kset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100955EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700958 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * @dev: pointer to the pci_dev data structure of MSI(X) device function
960 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600961 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700962 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * allocated for this device function, are reclaimed to unused state,
964 * which may be used later on.
965 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900966void msi_remove_pci_irq_vectors(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!pci_msi_enable || !dev)
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900969 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900971 if (dev->msi_enabled || dev->msix_enabled)
972 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700975void pci_no_msi(void)
976{
977 pci_msi_enable = 0;
978}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000979
Andrew Patterson07ae95f2008-11-10 15:31:05 -0700980/**
981 * pci_msi_enabled - is MSI enabled?
982 *
983 * Returns true if MSI has not been disabled by the command-line option
984 * pci=nomsi.
985 **/
986int pci_msi_enabled(void)
987{
988 return pci_msi_enable;
989}
990EXPORT_SYMBOL(pci_msi_enabled);
991
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000992void pci_msi_init_pci_dev(struct pci_dev *dev)
993{
Eric W. Biedermand5dea7d2011-10-17 11:46:06 -0700994 int pos;
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000995 INIT_LIST_HEAD(&dev->msi_list);
Eric W. Biedermand5dea7d2011-10-17 11:46:06 -0700996
997 /* Disable the msi hardware to avoid screaming interrupts
998 * during boot. This is the power on reset default so
999 * usually this should be a noop.
1000 */
1001 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
1002 if (pos)
1003 msi_set_enable(dev, pos, 0);
1004 msix_set_enable(dev, 0);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +10001005}