blob: aca7578b05e56205d854c6f76a5aac88c77d326e [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
Michael Ellerman11df1f02009-01-19 11:31:00 +110033#ifndef arch_msi_check_device
34int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010035{
36 return 0;
37}
Michael Ellerman11df1f02009-01-19 11:31:00 +110038#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010039
Michael Ellerman11df1f02009-01-19 11:31:00 +110040#ifndef arch_setup_msi_irqs
Thomas Gleixner1525bf02010-10-06 16:05:35 -040041# define arch_setup_msi_irqs default_setup_msi_irqs
42# define HAVE_DEFAULT_MSI_SETUP_IRQS
43#endif
44
45#ifdef HAVE_DEFAULT_MSI_SETUP_IRQS
46int default_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010047{
48 struct msi_desc *entry;
49 int ret;
50
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040051 /*
52 * If an architecture wants to support multiple MSI, it needs to
53 * override arch_setup_msi_irqs()
54 */
55 if (type == PCI_CAP_ID_MSI && nvec > 1)
56 return 1;
57
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010058 list_for_each_entry(entry, &dev->msi_list, list) {
59 ret = arch_setup_msi_irq(dev, entry);
Michael Ellermanb5fbf532009-02-11 22:27:02 +110060 if (ret < 0)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010061 return ret;
Michael Ellermanb5fbf532009-02-11 22:27:02 +110062 if (ret > 0)
63 return -ENOSPC;
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010064 }
65
66 return 0;
67}
Michael Ellerman11df1f02009-01-19 11:31:00 +110068#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010069
Michael Ellerman11df1f02009-01-19 11:31:00 +110070#ifndef arch_teardown_msi_irqs
Thomas Gleixner1525bf02010-10-06 16:05:35 -040071# define arch_teardown_msi_irqs default_teardown_msi_irqs
72# define HAVE_DEFAULT_MSI_TEARDOWN_IRQS
73#endif
74
75#ifdef HAVE_DEFAULT_MSI_TEARDOWN_IRQS
76void default_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010077{
78 struct msi_desc *entry;
79
80 list_for_each_entry(entry, &dev->msi_list, list) {
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040081 int i, nvec;
82 if (entry->irq == 0)
83 continue;
Alexander Gordeev65f6ae62013-05-13 11:05:48 +020084 if (entry->nvec_used)
85 nvec = entry->nvec_used;
86 else
87 nvec = 1 << entry->msi_attrib.multiple;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -040088 for (i = 0; i < nvec; i++)
89 arch_teardown_msi_irq(entry->irq + i);
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010090 }
91}
Michael Ellerman11df1f02009-01-19 11:31:00 +110092#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010093
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -050094#ifndef arch_restore_msi_irqs
95# define arch_restore_msi_irqs default_restore_msi_irqs
96# define HAVE_DEFAULT_MSI_RESTORE_IRQS
97#endif
98
99#ifdef HAVE_DEFAULT_MSI_RESTORE_IRQS
100void default_restore_msi_irqs(struct pci_dev *dev, int irq)
101{
102 struct msi_desc *entry;
103
104 entry = NULL;
105 if (dev->msix_enabled) {
106 list_for_each_entry(entry, &dev->msi_list, list) {
107 if (irq == entry->irq)
108 break;
109 }
110 } else if (dev->msi_enabled) {
111 entry = irq_get_msi_desc(irq);
112 }
113
114 if (entry)
115 write_msi_msg(irq, &entry->msg);
116}
117#endif
118
Gavin Shane375b562013-04-04 16:54:30 +0000119static void msi_set_enable(struct pci_dev *dev, int enable)
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800120{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800121 u16 control;
122
Gavin Shane375b562013-04-04 16:54:30 +0000123 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600124 control &= ~PCI_MSI_FLAGS_ENABLE;
125 if (enable)
126 control |= PCI_MSI_FLAGS_ENABLE;
Gavin Shane375b562013-04-04 16:54:30 +0000127 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +0900128}
129
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800130static void msix_set_enable(struct pci_dev *dev, int enable)
131{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800132 u16 control;
133
Gavin Shane375b562013-04-04 16:54:30 +0000134 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
135 control &= ~PCI_MSIX_FLAGS_ENABLE;
136 if (enable)
137 control |= PCI_MSIX_FLAGS_ENABLE;
138 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800139}
140
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500141static inline __attribute_const__ u32 msi_mask(unsigned x)
142{
Matthew Wilcox0b49ec32009-02-08 20:27:47 -0700143 /* Don't shift by >= width of type */
144 if (x >= 5)
145 return 0xffffffff;
146 return (1 << (1 << x)) - 1;
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500147}
148
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400149static inline __attribute_const__ u32 msi_capable_mask(u16 control)
Mitch Williams988cbb12007-03-30 11:54:08 -0700150{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400151 return msi_mask((control >> 1) & 7);
152}
Mitch Williams988cbb12007-03-30 11:54:08 -0700153
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400154static inline __attribute_const__ u32 msi_enabled_mask(u16 control)
155{
156 return msi_mask((control >> 4) & 7);
Mitch Williams988cbb12007-03-30 11:54:08 -0700157}
158
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600159/*
160 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
161 * mask all MSI interrupts by clearing the MSI enable bit does not work
162 * reliably as devices without an INTx disable bit will then generate a
163 * level IRQ which will never be cleared.
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600164 */
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900165static u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400167 u32 mask_bits = desc->masked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400169 if (!desc->msi_attrib.maskbit)
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900170 return 0;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400171
172 mask_bits &= ~mask;
173 mask_bits |= flag;
174 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900175
176 return mask_bits;
177}
178
179static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
180{
181 desc->masked = __msi_mask_irq(desc, mask, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400182}
183
184/*
185 * This internal function does not flush PCI writes to the device.
186 * All users must ensure that they read from the device before either
187 * assuming that the device state is up to date, or returning out of this
188 * file. This saves a few milliseconds when initialising devices with lots
189 * of MSI-X interrupts.
190 */
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900191static u32 __msix_mask_irq(struct msi_desc *desc, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400192{
193 u32 mask_bits = desc->masked;
194 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900195 PCI_MSIX_ENTRY_VECTOR_CTRL;
Sheng Yang8d805282010-11-11 15:46:55 +0800196 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
197 if (flag)
198 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400199 writel(mask_bits, desc->mask_base + offset);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900200
201 return mask_bits;
202}
203
204static void msix_mask_irq(struct msi_desc *desc, u32 flag)
205{
206 desc->masked = __msix_mask_irq(desc, flag);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400207}
208
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100209#ifdef CONFIG_GENERIC_HARDIRQS
210
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200211static void msi_set_mask_bit(struct irq_data *data, u32 flag)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400212{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200213 struct msi_desc *desc = irq_data_get_msi(data);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400214
215 if (desc->msi_attrib.is_msix) {
216 msix_mask_irq(desc, flag);
217 readl(desc->mask_base); /* Flush write to device */
Matthew Wilcox24d27552009-03-17 08:54:06 -0400218 } else {
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200219 unsigned offset = data->irq - desc->dev->irq;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400220 msi_mask_irq(desc, 1 << offset, flag << offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400222}
223
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200224void mask_msi_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400225{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200226 msi_set_mask_bit(data, 1);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400227}
228
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200229void unmask_msi_irq(struct irq_data *data)
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400230{
Thomas Gleixner1c9db522010-09-28 16:46:51 +0200231 msi_set_mask_bit(data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100234#endif /* CONFIG_GENERIC_HARDIRQS */
235
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200236void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700237{
Ben Hutchings30da5522010-07-23 14:56:28 +0100238 BUG_ON(entry->dev->current_state != PCI_D0);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700239
Ben Hutchings30da5522010-07-23 14:56:28 +0100240 if (entry->msi_attrib.is_msix) {
241 void __iomem *base = entry->mask_base +
242 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
243
244 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
245 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
246 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
247 } else {
248 struct pci_dev *dev = entry->dev;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600249 int pos = dev->msi_cap;
Ben Hutchings30da5522010-07-23 14:56:28 +0100250 u16 data;
251
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600252 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
253 &msg->address_lo);
Ben Hutchings30da5522010-07-23 14:56:28 +0100254 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600255 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
256 &msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600257 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100258 } else {
259 msg->address_hi = 0;
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600260 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
Ben Hutchings30da5522010-07-23 14:56:28 +0100261 }
262 msg->data = data;
263 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700264}
265
Yinghai Lu3145e942008-12-05 18:58:34 -0800266void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700267{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200268 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800269
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200270 __read_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800271}
272
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200273void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Ben Hutchings30da5522010-07-23 14:56:28 +0100274{
Ben Hutchings30da5522010-07-23 14:56:28 +0100275 /* Assert that the cache is valid, assuming that
276 * valid messages are not all-zeroes. */
277 BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
278 entry->msg.data));
279
280 *msg = entry->msg;
281}
282
283void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
284{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200285 struct msi_desc *entry = irq_get_msi_desc(irq);
Ben Hutchings30da5522010-07-23 14:56:28 +0100286
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200287 __get_cached_msi_msg(entry, msg);
Ben Hutchings30da5522010-07-23 14:56:28 +0100288}
289
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200290void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
Yinghai Lu3145e942008-12-05 18:58:34 -0800291{
Ben Hutchingsfcd097f2010-06-17 20:16:36 +0100292 if (entry->dev->current_state != PCI_D0) {
293 /* Don't touch the hardware now */
294 } else if (entry->msi_attrib.is_msix) {
Matthew Wilcox24d27552009-03-17 08:54:06 -0400295 void __iomem *base;
296 base = entry->mask_base +
297 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
298
Hidetoshi Seto2c21fd42009-06-23 17:40:04 +0900299 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
300 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
301 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
Matthew Wilcox24d27552009-03-17 08:54:06 -0400302 } else {
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700303 struct pci_dev *dev = entry->dev;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600304 int pos = dev->msi_cap;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400305 u16 msgctl;
306
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600307 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400308 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
309 msgctl |= entry->msi_attrib.multiple << 4;
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600310 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700311
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600312 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
313 msg->address_lo);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700314 if (entry->msi_attrib.is_64) {
Bjorn Helgaas9925ad02013-04-17 17:39:57 -0600315 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
316 msg->address_hi);
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600317 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
318 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700319 } else {
Bjorn Helgaas2f221342013-04-17 17:41:13 -0600320 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
321 msg->data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700322 }
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700323 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700324 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700325}
326
Yinghai Lu3145e942008-12-05 18:58:34 -0800327void write_msi_msg(unsigned int irq, struct msi_msg *msg)
328{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200329 struct msi_desc *entry = irq_get_msi_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800330
Thomas Gleixner39431ac2010-09-28 19:09:51 +0200331 __write_msi_msg(entry, msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800332}
333
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900334static void free_msi_irqs(struct pci_dev *dev)
335{
336 struct msi_desc *entry, *tmp;
337
338 list_for_each_entry(entry, &dev->msi_list, list) {
339 int i, nvec;
340 if (!entry->irq)
341 continue;
Alexander Gordeev65f6ae62013-05-13 11:05:48 +0200342 if (entry->nvec_used)
343 nvec = entry->nvec_used;
344 else
345 nvec = 1 << entry->msi_attrib.multiple;
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100346#ifdef CONFIG_GENERIC_HARDIRQS
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900347 for (i = 0; i < nvec; i++)
348 BUG_ON(irq_has_action(entry->irq + i));
Jan Glauber9a4da8a2012-11-29 13:05:05 +0100349#endif
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900350 }
351
352 arch_teardown_msi_irqs(dev);
353
354 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
355 if (entry->msi_attrib.is_msix) {
356 if (list_is_last(&entry->list, &dev->msi_list))
357 iounmap(entry->mask_base);
358 }
Neil Horman424eb392012-01-03 10:29:54 -0500359
360 /*
361 * Its possible that we get into this path
362 * When populate_msi_sysfs fails, which means the entries
363 * were not registered with sysfs. In that case don't
364 * unregister them.
365 */
366 if (entry->kobj.parent) {
367 kobject_del(&entry->kobj);
368 kobject_put(&entry->kobj);
369 }
370
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900371 list_del(&entry->list);
372 kfree(entry);
373 }
374}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900375
Matthew Wilcox379f5322009-03-17 08:54:07 -0400376static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Matthew Wilcox379f5322009-03-17 08:54:07 -0400378 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
379 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return NULL;
381
Matthew Wilcox379f5322009-03-17 08:54:07 -0400382 INIT_LIST_HEAD(&desc->list);
383 desc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Matthew Wilcox379f5322009-03-17 08:54:07 -0400385 return desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
David Millerba698ad2007-10-25 01:16:30 -0700388static void pci_intx_for_msi(struct pci_dev *dev, int enable)
389{
390 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
391 pci_intx(dev, enable);
392}
393
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100394static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800395{
Shaohua Li41017f02006-02-08 17:11:38 +0800396 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700397 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800398
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800399 if (!dev->msi_enabled)
400 return;
401
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200402 entry = irq_get_msi_desc(dev->irq);
Shaohua Li41017f02006-02-08 17:11:38 +0800403
David Millerba698ad2007-10-25 01:16:30 -0700404 pci_intx_for_msi(dev, 0);
Gavin Shane375b562013-04-04 16:54:30 +0000405 msi_set_enable(dev, 0);
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500406 arch_restore_msi_irqs(dev, dev->irq);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700407
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600408 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400409 msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700410 control &= ~PCI_MSI_FLAGS_QSIZE;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400411 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600412 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100413}
414
415static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800416{
Shaohua Li41017f02006-02-08 17:11:38 +0800417 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700418 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800419
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700420 if (!dev->msix_enabled)
421 return;
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700422 BUG_ON(list_empty(&dev->msi_list));
Hidetoshi Seto9cc8d542009-08-06 11:32:04 +0900423 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600424 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700425
Shaohua Li41017f02006-02-08 17:11:38 +0800426 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700427 pci_intx_for_msi(dev, 0);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700428 control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600429 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800430
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000431 list_for_each_entry(entry, &dev->msi_list, list) {
Konrad Rzeszutek Wilk76ccc292011-12-16 17:38:18 -0500432 arch_restore_msi_irqs(dev, entry->irq);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400433 msix_mask_irq(entry, entry->masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800434 }
Shaohua Li41017f02006-02-08 17:11:38 +0800435
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700436 control &= ~PCI_MSIX_FLAGS_MASKALL;
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600437 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800438}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100439
440void pci_restore_msi_state(struct pci_dev *dev)
441{
442 __pci_restore_msi_state(dev);
443 __pci_restore_msix_state(dev);
444}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600445EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800446
Neil Hormanda8d1c82011-10-06 14:08:18 -0400447
448#define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr)
449#define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj)
450
451struct msi_attribute {
452 struct attribute attr;
453 ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr,
454 char *buf);
455 ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr,
456 const char *buf, size_t count);
457};
458
459static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr,
460 char *buf)
461{
462 return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi");
463}
464
465static ssize_t msi_irq_attr_show(struct kobject *kobj,
466 struct attribute *attr, char *buf)
467{
468 struct msi_attribute *attribute = to_msi_attr(attr);
469 struct msi_desc *entry = to_msi_desc(kobj);
470
471 if (!attribute->show)
472 return -EIO;
473
474 return attribute->show(entry, attribute, buf);
475}
476
477static const struct sysfs_ops msi_irq_sysfs_ops = {
478 .show = msi_irq_attr_show,
479};
480
481static struct msi_attribute mode_attribute =
482 __ATTR(mode, S_IRUGO, show_msi_mode, NULL);
483
484
Bjorn Helgaas9738abe2013-04-12 11:20:03 -0600485static struct attribute *msi_irq_default_attrs[] = {
Neil Hormanda8d1c82011-10-06 14:08:18 -0400486 &mode_attribute.attr,
487 NULL
488};
489
Bjorn Helgaas9738abe2013-04-12 11:20:03 -0600490static void msi_kobj_release(struct kobject *kobj)
Neil Hormanda8d1c82011-10-06 14:08:18 -0400491{
492 struct msi_desc *entry = to_msi_desc(kobj);
493
494 pci_dev_put(entry->dev);
495}
496
497static struct kobj_type msi_irq_ktype = {
498 .release = msi_kobj_release,
499 .sysfs_ops = &msi_irq_sysfs_ops,
500 .default_attrs = msi_irq_default_attrs,
501};
502
503static int populate_msi_sysfs(struct pci_dev *pdev)
504{
505 struct msi_desc *entry;
506 struct kobject *kobj;
507 int ret;
508 int count = 0;
509
510 pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj);
511 if (!pdev->msi_kset)
512 return -ENOMEM;
513
514 list_for_each_entry(entry, &pdev->msi_list, list) {
515 kobj = &entry->kobj;
516 kobj->kset = pdev->msi_kset;
517 pci_dev_get(pdev);
518 ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL,
519 "%u", entry->irq);
520 if (ret)
521 goto out_unroll;
522
523 count++;
524 }
525
526 return 0;
527
528out_unroll:
529 list_for_each_entry(entry, &pdev->msi_list, list) {
530 if (!count)
531 break;
532 kobject_del(&entry->kobj);
533 kobject_put(&entry->kobj);
534 count--;
535 }
536 return ret;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/**
540 * msi_capability_init - configure device's MSI capability structure
541 * @dev: pointer to the pci_dev data structure of MSI device function
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400542 * @nvec: number of interrupts to allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400544 * Setup the MSI capability structure of the device with the requested
545 * number of interrupts. A return value of zero indicates the successful
546 * setup of an entry with the new MSI irq. A negative return value indicates
547 * an error, and a positive return value indicates the number of interrupts
548 * which could have been allocated.
549 */
550static int msi_capability_init(struct pci_dev *dev, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct msi_desc *entry;
Gavin Shanf4651362013-04-04 16:54:32 +0000553 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 u16 control;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400555 unsigned mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Gavin Shane375b562013-04-04 16:54:30 +0000557 msi_set_enable(dev, 0); /* Disable MSI during set up */
Matthew Wilcox110828c2009-06-16 06:31:45 -0600558
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600559 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* MSI Entry Initialization */
Matthew Wilcox379f5322009-03-17 08:54:07 -0400561 entry = alloc_msi_entry(dev);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700562 if (!entry)
563 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700564
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900565 entry->msi_attrib.is_msix = 0;
Bjorn Helgaas4987ce82013-04-17 17:42:30 -0600566 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900567 entry->msi_attrib.entry_nr = 0;
Bjorn Helgaas4987ce82013-04-17 17:42:30 -0600568 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900569 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Gavin Shanf4651362013-04-04 16:54:32 +0000570 entry->msi_attrib.pos = dev->msi_cap;
Hidetoshi Seto0db29af2008-12-24 17:27:04 +0900571
Dan Carpentere5f66ea2013-04-30 10:44:54 +0300572 if (control & PCI_MSI_FLAGS_64BIT)
573 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
574 else
575 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400576 /* All MSIs are unmasked by default, Mask them all */
577 if (entry->msi_attrib.maskbit)
578 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
579 mask = msi_capable_mask(control);
580 msi_mask_irq(entry, mask, mask);
581
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700582 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Configure MSI capability structure */
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400585 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000586 if (ret) {
Hidetoshi Seto7ba19302009-06-23 17:39:27 +0900587 msi_mask_irq(entry, mask, ~mask);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900588 free_msi_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000589 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500590 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700591
Neil Hormanda8d1c82011-10-06 14:08:18 -0400592 ret = populate_msi_sysfs(dev);
593 if (ret) {
594 msi_mask_irq(entry, mask, ~mask);
595 free_msi_irqs(dev);
596 return ret;
597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700600 pci_intx_for_msi(dev, 0);
Gavin Shane375b562013-04-04 16:54:30 +0000601 msi_set_enable(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800602 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Michael Ellerman7fe37302007-04-18 19:39:21 +1000604 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606}
607
Gavin Shan520fe9d2013-04-04 16:54:33 +0000608static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900609{
Kenji Kaneshige4302e0f2010-06-17 10:42:44 +0900610 resource_size_t phys_addr;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900611 u32 table_offset;
612 u8 bir;
613
Bjorn Helgaas909094c2013-04-17 17:43:40 -0600614 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
615 &table_offset);
Bjorn Helgaas4d187602013-04-17 18:10:07 -0600616 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
617 table_offset &= PCI_MSIX_TABLE_OFFSET;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900618 phys_addr = pci_resource_start(dev, bir) + table_offset;
619
620 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
621}
622
Gavin Shan520fe9d2013-04-04 16:54:33 +0000623static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
624 struct msix_entry *entries, int nvec)
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900625{
626 struct msi_desc *entry;
627 int i;
628
629 for (i = 0; i < nvec; i++) {
630 entry = alloc_msi_entry(dev);
631 if (!entry) {
632 if (!i)
633 iounmap(base);
634 else
635 free_msi_irqs(dev);
636 /* No enough memory. Don't try again */
637 return -ENOMEM;
638 }
639
640 entry->msi_attrib.is_msix = 1;
641 entry->msi_attrib.is_64 = 1;
642 entry->msi_attrib.entry_nr = entries[i].entry;
643 entry->msi_attrib.default_irq = dev->irq;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000644 entry->msi_attrib.pos = dev->msix_cap;
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900645 entry->mask_base = base;
646
647 list_add_tail(&entry->list, &dev->msi_list);
648 }
649
650 return 0;
651}
652
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900653static void msix_program_entries(struct pci_dev *dev,
Gavin Shan520fe9d2013-04-04 16:54:33 +0000654 struct msix_entry *entries)
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900655{
656 struct msi_desc *entry;
657 int i = 0;
658
659 list_for_each_entry(entry, &dev->msi_list, list) {
660 int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
661 PCI_MSIX_ENTRY_VECTOR_CTRL;
662
663 entries[i].vector = entry->irq;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200664 irq_set_msi_desc(entry->irq, entry);
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900665 entry->masked = readl(entry->mask_base + offset);
666 msix_mask_irq(entry, 1);
667 i++;
668 }
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/**
672 * msix_capability_init - configure device's MSI-X capability
673 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700674 * @entries: pointer to an array of struct msix_entry entries
675 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600677 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700678 * single MSI-X irq. A return of zero indicates the successful setup of
679 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 **/
681static int msix_capability_init(struct pci_dev *dev,
682 struct msix_entry *entries, int nvec)
683{
Gavin Shan520fe9d2013-04-04 16:54:33 +0000684 int ret;
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900685 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 void __iomem *base;
687
Gavin Shan520fe9d2013-04-04 16:54:33 +0000688 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700689
690 /* Ensure MSI-X is disabled while it is set up */
691 control &= ~PCI_MSIX_FLAGS_ENABLE;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000692 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Request & Map MSI-X table region */
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600695 base = msix_map_region(dev, msix_table_size(control));
Hidetoshi Seto5a05a9d2009-08-06 11:34:34 +0900696 if (!base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -ENOMEM;
698
Gavin Shan520fe9d2013-04-04 16:54:33 +0000699 ret = msix_setup_entries(dev, base, entries, nvec);
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900700 if (ret)
701 return ret;
Michael Ellerman9c831332007-04-18 19:39:21 +1000702
703 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900704 if (ret)
705 goto error;
Michael Ellerman9c831332007-04-18 19:39:21 +1000706
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700707 /*
708 * Some devices require MSI-X to be enabled before we can touch the
709 * MSI-X registers. We need to mask all the vectors to prevent
710 * interrupts coming in before they're fully set up.
711 */
712 control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000713 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700714
Hidetoshi Seto75cb3422009-08-06 11:35:10 +0900715 msix_program_entries(dev, entries);
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700716
Neil Hormanda8d1c82011-10-06 14:08:18 -0400717 ret = populate_msi_sysfs(dev);
718 if (ret) {
719 ret = 0;
720 goto error;
721 }
722
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700723 /* Set MSI-X enabled bits and unmask the function */
David Millerba698ad2007-10-25 01:16:30 -0700724 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800725 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Matthew Wilcoxf5982822009-06-18 19:15:59 -0700727 control &= ~PCI_MSIX_FLAGS_MASKALL;
Gavin Shan520fe9d2013-04-04 16:54:33 +0000728 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control);
Matthew Wilcox8d181012009-05-08 07:13:33 -0600729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 0;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900731
732error:
733 if (ret < 0) {
734 /*
735 * If we had some success, report the number of irqs
736 * we succeeded in setting up.
737 */
Hidetoshi Setod9d70702009-08-06 11:35:48 +0900738 struct msi_desc *entry;
Hidetoshi Seto583871d2009-08-06 11:33:39 +0900739 int avail = 0;
740
741 list_for_each_entry(entry, &dev->msi_list, list) {
742 if (entry->irq != 0)
743 avail++;
744 }
745 if (avail != 0)
746 ret = avail;
747 }
748
749 free_msi_irqs(dev);
750
751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000755 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400756 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000757 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100758 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400759 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200760 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000761 * to determine if MSI/-X are supported for the device. If MSI/-X is
762 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400763 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900764static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400765{
766 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000767 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400768
Brice Goglin0306ebf2006-10-05 10:24:31 +0200769 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400770 if (!pci_msi_enable || !dev || dev->no_msi)
771 return -EINVAL;
772
Michael Ellerman314e77b2007-04-05 17:19:12 +1000773 /*
774 * You can't ask to have 0 or less MSIs configured.
775 * a) it's stupid ..
776 * b) the list manipulation code assumes nvec >= 1.
777 */
778 if (nvec < 1)
779 return -ERANGE;
780
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900781 /*
782 * Any bridge which does NOT route MSI transactions from its
783 * secondary bus to its primary bus must set NO_MSI flag on
Brice Goglin0306ebf2006-10-05 10:24:31 +0200784 * the secondary pci_bus.
785 * We expect only arch-specific PCI host bus controller driver
786 * or quirks for specific PCI bridges to be setting NO_MSI.
787 */
Brice Goglin24334a12006-08-31 01:55:07 -0400788 for (bus = dev->bus; bus; bus = bus->parent)
789 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
790 return -EINVAL;
791
Michael Ellermanc9953a72007-04-05 17:19:08 +1000792 ret = arch_msi_check_device(dev, nvec, type);
793 if (ret)
794 return ret;
795
Brice Goglin24334a12006-08-31 01:55:07 -0400796 return 0;
797}
798
799/**
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400800 * pci_enable_msi_block - configure device's MSI capability structure
801 * @dev: device to configure
802 * @nvec: number of interrupts to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400804 * Allocate IRQs for a device with the MSI capability.
805 * This function returns a negative errno if an error occurs. If it
806 * is unable to allocate the number of interrupts requested, it returns
807 * the number of interrupts it might be able to allocate. If it successfully
808 * allocates at least the number of interrupts requested, it returns 0 and
809 * updates the @dev's irq member to the lowest new interrupt number; the
810 * other interrupt numbers allocated to this device are consecutive.
811 */
812int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Gavin Shanf4651362013-04-04 16:54:32 +0000814 int status, maxvec;
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400815 u16 msgctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Gavin Shanf4651362013-04-04 16:54:32 +0000817 if (!dev->msi_cap)
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400818 return -EINVAL;
Gavin Shanf4651362013-04-04 16:54:32 +0000819
820 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400821 maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
822 if (nvec > maxvec)
823 return maxvec;
824
825 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
Michael Ellermanc9953a72007-04-05 17:19:08 +1000826 if (status)
827 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700829 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400831 /* Check whether driver already requested MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800832 if (dev->msix_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600833 dev_info(&dev->dev, "can't enable MSI "
834 "(MSI-X already enabled)\n");
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400837
838 status = msi_capability_init(dev, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return status;
840}
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -0400841EXPORT_SYMBOL(pci_enable_msi_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Alexander Gordeev08261d82012-11-19 16:02:10 +0100843int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec)
844{
Gavin Shanf4651362013-04-04 16:54:32 +0000845 int ret, nvec;
Alexander Gordeev08261d82012-11-19 16:02:10 +0100846 u16 msgctl;
847
Gavin Shanf4651362013-04-04 16:54:32 +0000848 if (!dev->msi_cap)
Alexander Gordeev08261d82012-11-19 16:02:10 +0100849 return -EINVAL;
850
Gavin Shanf4651362013-04-04 16:54:32 +0000851 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
Alexander Gordeev08261d82012-11-19 16:02:10 +0100852 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
853
854 if (maxvec)
855 *maxvec = ret;
856
857 do {
858 nvec = ret;
859 ret = pci_enable_msi_block(dev, nvec);
860 } while (ret > 0);
861
862 if (ret < 0)
863 return ret;
864 return nvec;
865}
866EXPORT_SYMBOL(pci_enable_msi_block_auto);
867
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400868void pci_msi_shutdown(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400870 struct msi_desc *desc;
871 u32 mask;
872 u16 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100874 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700875 return;
876
Matthew Wilcox110828c2009-06-16 06:31:45 -0600877 BUG_ON(list_empty(&dev->msi_list));
878 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
Matthew Wilcox110828c2009-06-16 06:31:45 -0600879
Gavin Shane375b562013-04-04 16:54:30 +0000880 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700881 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800882 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700883
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900884 /* Return the device with MSI unmasked as initial states */
Bjorn Helgaasf5322162013-04-17 17:34:36 -0600885 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl);
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400886 mask = msi_capable_mask(ctrl);
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900887 /* Keep cached state to be restored */
888 __msi_mask_irq(desc, mask, ~mask);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100889
890 /* Restore dev->irq to its default pin-assertion irq */
Matthew Wilcoxf2440d92009-03-17 08:54:09 -0400891 dev->irq = desc->msi_attrib.default_irq;
Yinghai Lud52877c2008-04-23 14:58:09 -0700892}
Matthew Wilcox24d27552009-03-17 08:54:06 -0400893
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900894void pci_disable_msi(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700895{
Yinghai Lud52877c2008-04-23 14:58:09 -0700896 if (!pci_msi_enable || !dev || !dev->msi_enabled)
897 return;
898
899 pci_msi_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900900 free_msi_irqs(dev);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400901 kset_unregister(dev->msi_kset);
902 dev->msi_kset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100904EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/**
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100907 * pci_msix_table_size - return the number of device's MSI-X table entries
908 * @dev: pointer to the pci_dev data structure of MSI-X device function
909 */
910int pci_msix_table_size(struct pci_dev *dev)
911{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100912 u16 control;
913
Gavin Shan520fe9d2013-04-04 16:54:33 +0000914 if (!dev->msix_cap)
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100915 return 0;
916
Bjorn Helgaasf84ecd22013-04-17 17:38:32 -0600917 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
Bjorn Helgaas527eee22013-04-17 17:44:48 -0600918 return msix_table_size(control);
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100919}
920
921/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * pci_enable_msix - configure device's MSI-X capability structure
923 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700924 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700925 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700928 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * MSI-X mode enabled on its hardware device function. A return of zero
930 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700931 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * Or a return of > 0 indicates that driver request is exceeding the number
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300933 * of irqs or MSI-X vectors available. Driver should use the returned value to
934 * re-send its request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900936int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100938 int status, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700939 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Gavin Shancdf1fd42013-04-04 16:54:31 +0000941 if (!entries || !dev->msix_cap)
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900942 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Michael Ellermanc9953a72007-04-05 17:19:08 +1000944 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
945 if (status)
946 return status;
947
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100948 nr_entries = pci_msix_table_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (nvec > nr_entries)
Michael S. Tsirkin57fbf522009-05-07 11:28:41 +0300950 return nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* Check for any invalid entries */
953 for (i = 0; i < nvec; i++) {
954 if (entries[i].entry >= nr_entries)
955 return -EINVAL; /* invalid entry */
956 for (j = i + 1; j < nvec; j++) {
957 if (entries[i].entry == entries[j].entry)
958 return -EINVAL; /* duplicate entry */
959 }
960 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700961 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700962
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700963 /* Check whether driver already requested for MSI irq */
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900964 if (dev->msi_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600965 dev_info(&dev->dev, "can't enable MSI-X "
966 "(MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -EINVAL;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return status;
971}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100972EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900974void pci_msix_shutdown(struct pci_dev *dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100975{
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900976 struct msi_desc *entry;
977
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100978 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700979 return;
980
Hidetoshi Seto12abb8b2009-06-24 12:08:09 +0900981 /* Return the device with MSI-X masked as initial states */
982 list_for_each_entry(entry, &dev->msi_list, list) {
983 /* Keep cached states to be restored */
984 __msix_mask_irq(entry, 1);
985 }
986
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800987 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700988 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800989 dev->msix_enabled = 0;
Yinghai Lud52877c2008-04-23 14:58:09 -0700990}
Hidetoshi Setoc9018512009-08-06 11:31:27 +0900991
Hidetoshi Seto500559a2009-08-10 10:14:15 +0900992void pci_disable_msix(struct pci_dev *dev)
Yinghai Lud52877c2008-04-23 14:58:09 -0700993{
994 if (!pci_msi_enable || !dev || !dev->msix_enabled)
995 return;
996
997 pci_msix_shutdown(dev);
Hidetoshi Setof56e4482009-08-06 11:32:51 +0900998 free_msi_irqs(dev);
Neil Hormanda8d1c82011-10-06 14:08:18 -0400999 kset_unregister(dev->msi_kset);
1000 dev->msi_kset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
Michael Ellerman4cc086f2007-03-22 21:51:34 +11001002EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001005 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1007 *
Steven Coleeaae4b32005-05-03 18:38:30 -06001008 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001009 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * allocated for this device function, are reclaimed to unused state,
1011 * which may be used later on.
1012 **/
Hidetoshi Seto500559a2009-08-10 10:14:15 +09001013void msi_remove_pci_irq_vectors(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!pci_msi_enable || !dev)
Hidetoshi Seto500559a2009-08-10 10:14:15 +09001016 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Hidetoshi Setof56e4482009-08-06 11:32:51 +09001018 if (dev->msi_enabled || dev->msix_enabled)
1019 free_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001022void pci_no_msi(void)
1023{
1024 pci_msi_enable = 0;
1025}
Michael Ellermanc9953a72007-04-05 17:19:08 +10001026
Andrew Patterson07ae95f2008-11-10 15:31:05 -07001027/**
1028 * pci_msi_enabled - is MSI enabled?
1029 *
1030 * Returns true if MSI has not been disabled by the command-line option
1031 * pci=nomsi.
1032 **/
1033int pci_msi_enabled(void)
1034{
1035 return pci_msi_enable;
1036}
1037EXPORT_SYMBOL(pci_msi_enabled);
1038
Michael Ellerman4aa9bc92007-04-05 17:19:10 +10001039void pci_msi_init_pci_dev(struct pci_dev *dev)
1040{
1041 INIT_LIST_HEAD(&dev->msi_list);
Eric W. Biedermand5dea7d2011-10-17 11:46:06 -07001042
1043 /* Disable the msi hardware to avoid screaming interrupts
1044 * during boot. This is the power on reset default so
1045 * usually this should be a noop.
1046 */
Gavin Shane375b562013-04-04 16:54:30 +00001047 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1048 if (dev->msi_cap)
1049 msi_set_enable(dev, 0);
1050
1051 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1052 if (dev->msix_cap)
1053 msix_set_enable(dev, 0);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +10001054}