blob: a4ef93ea4c547b78bf80b5b12c9c20101bd3a1ec [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ioport.h>
15#include <linux/smp_lock.h>
16#include <linux/pci.h>
17#include <linux/proc_fs.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070018#include <linux/msi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/errno.h>
21#include <asm/io.h>
22#include <asm/smp.h>
23
24#include "pci.h"
25#include "msi.h"
26
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache* msi_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int msi_cache_init(void)
32{
Pekka J Enberg57181782006-09-27 01:51:03 -070033 msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
34 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (!msi_cachep)
36 return -ENOMEM;
37
38 return 0;
39}
40
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080041static void msi_set_enable(struct pci_dev *dev, int enable)
42{
43 int pos;
44 u16 control;
45
46 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
47 if (pos) {
48 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
49 control &= ~PCI_MSI_FLAGS_ENABLE;
50 if (enable)
51 control |= PCI_MSI_FLAGS_ENABLE;
52 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
53 }
54}
55
56static void msix_set_enable(struct pci_dev *dev, int enable)
57{
58 int pos;
59 u16 control;
60
61 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
62 if (pos) {
63 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
64 control &= ~PCI_MSIX_FLAGS_ENABLE;
65 if (enable)
66 control |= PCI_MSIX_FLAGS_ENABLE;
67 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
68 }
69}
70
Mitch Williams988cbb12007-03-30 11:54:08 -070071static void msix_flush_writes(unsigned int irq)
72{
73 struct msi_desc *entry;
74
75 entry = get_irq_msi(irq);
76 BUG_ON(!entry || !entry->dev);
77 switch (entry->msi_attrib.type) {
78 case PCI_CAP_ID_MSI:
79 /* nothing to do */
80 break;
81 case PCI_CAP_ID_MSIX:
82 {
83 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
84 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
85 readl(entry->mask_base + offset);
86 break;
87 }
88 default:
89 BUG();
90 break;
91 }
92}
93
Eric W. Biederman1ce03372006-10-04 02:16:41 -070094static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct msi_desc *entry;
97
Eric W. Biederman5b912c12007-01-28 12:52:03 -070098 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -070099 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 switch (entry->msi_attrib.type) {
101 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700102 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900103 int pos;
104 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Eric W. Biederman277bc332006-10-04 02:16:57 -0700106 pos = (long)entry->mask_base;
107 pci_read_config_dword(entry->dev, pos, &mask_bits);
108 mask_bits &= ~(1);
109 mask_bits |= flag;
110 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -0800111 } else {
112 msi_set_enable(entry->dev, !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 case PCI_CAP_ID_MSIX:
116 {
117 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
118 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
119 writel(flag, entry->mask_base + offset);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -0600120 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 }
123 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700124 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
126 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700127 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700130void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700131{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700132 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700133 switch(entry->msi_attrib.type) {
134 case PCI_CAP_ID_MSI:
135 {
136 struct pci_dev *dev = entry->dev;
137 int pos = entry->msi_attrib.pos;
138 u16 data;
139
140 pci_read_config_dword(dev, msi_lower_address_reg(pos),
141 &msg->address_lo);
142 if (entry->msi_attrib.is_64) {
143 pci_read_config_dword(dev, msi_upper_address_reg(pos),
144 &msg->address_hi);
145 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
146 } else {
147 msg->address_hi = 0;
148 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
149 }
150 msg->data = data;
151 break;
152 }
153 case PCI_CAP_ID_MSIX:
154 {
155 void __iomem *base;
156 base = entry->mask_base +
157 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
158
159 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
160 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
161 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
162 break;
163 }
164 default:
165 BUG();
166 }
167}
168
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700169void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700170{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700171 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700172 switch (entry->msi_attrib.type) {
173 case PCI_CAP_ID_MSI:
174 {
175 struct pci_dev *dev = entry->dev;
176 int pos = entry->msi_attrib.pos;
177
178 pci_write_config_dword(dev, msi_lower_address_reg(pos),
179 msg->address_lo);
180 if (entry->msi_attrib.is_64) {
181 pci_write_config_dword(dev, msi_upper_address_reg(pos),
182 msg->address_hi);
183 pci_write_config_word(dev, msi_data_reg(pos, 1),
184 msg->data);
185 } else {
186 pci_write_config_word(dev, msi_data_reg(pos, 0),
187 msg->data);
188 }
189 break;
190 }
191 case PCI_CAP_ID_MSIX:
192 {
193 void __iomem *base;
194 base = entry->mask_base +
195 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
196
197 writel(msg->address_lo,
198 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
199 writel(msg->address_hi,
200 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
201 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
202 break;
203 }
204 default:
205 BUG();
206 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700207 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700208}
209
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700210void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700212 msi_set_mask_bit(irq, 1);
Mitch Williams988cbb12007-03-30 11:54:08 -0700213 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700216void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700218 msi_set_mask_bit(irq, 0);
Mitch Williams988cbb12007-03-30 11:54:08 -0700219 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700222static int msi_free_irq(struct pci_dev* dev, int irq);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int msi_init(void)
225{
226 static int status = -ENOMEM;
227
228 if (!status)
229 return status;
230
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700231 status = msi_cache_init();
232 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 pci_msi_enable = 0;
234 printk(KERN_WARNING "PCI: MSI cache init failed\n");
235 return status;
236 }
Mark Maulefd58e552006-04-10 21:17:48 -0500237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return status;
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static struct msi_desc* alloc_msi_entry(void)
242{
243 struct msi_desc *entry;
244
Pekka J Enberg57181782006-09-27 01:51:03 -0700245 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!entry)
247 return NULL;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 entry->link.tail = entry->link.head = 0; /* single message */
250 entry->dev = NULL;
251
252 return entry;
253}
254
Shaohua Li41017f02006-02-08 17:11:38 +0800255#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100256static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800257{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700258 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800259 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700260 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800261
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800262 if (!dev->msi_enabled)
263 return;
264
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700265 entry = get_irq_msi(dev->irq);
266 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800267
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800268 pci_intx(dev, 0); /* disable intx */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800269 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700270 write_msi_msg(dev->irq, &entry->msg);
271 if (entry->msi_attrib.maskbit)
272 msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
273
274 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
275 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
276 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
277 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800278 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100279}
280
281static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800282{
Shaohua Li41017f02006-02-08 17:11:38 +0800283 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700284 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800285 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700286 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800287
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700288 if (!dev->msix_enabled)
289 return;
290
Shaohua Li41017f02006-02-08 17:11:38 +0800291 /* route the table */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800292 pci_intx(dev, 0); /* disable intx */
293 msix_set_enable(dev, 0);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700294 irq = head = dev->first_msi_irq;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700295 entry = get_irq_msi(irq);
296 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800297 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700298 entry = get_irq_msi(irq);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700299 write_msi_msg(irq, &entry->msg);
300 msi_set_mask_bit(irq, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800301
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700302 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700303 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800304 }
Shaohua Li41017f02006-02-08 17:11:38 +0800305
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700306 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
307 control &= ~PCI_MSIX_FLAGS_MASKALL;
308 control |= PCI_MSIX_FLAGS_ENABLE;
309 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800310}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100311
312void pci_restore_msi_state(struct pci_dev *dev)
313{
314 __pci_restore_msi_state(dev);
315 __pci_restore_msix_state(dev);
316}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900317#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/**
320 * msi_capability_init - configure device's MSI capability structure
321 * @dev: pointer to the pci_dev data structure of MSI device function
322 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600323 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700324 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700326 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 **/
328static int msi_capability_init(struct pci_dev *dev)
329{
330 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700331 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 u16 control;
333
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800334 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
337 pci_read_config_word(dev, msi_control_reg(pos), &control);
338 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700339 entry = alloc_msi_entry();
340 if (!entry)
341 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700344 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 entry->msi_attrib.entry_nr = 0;
346 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700347 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700348 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700349 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (is_mask_bit_support(control)) {
351 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
352 is_64bit_address(control));
353 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700354 entry->dev = dev;
355 if (entry->msi_attrib.maskbit) {
356 unsigned int maskbits, temp;
357 /* All MSIs are unmasked by default, Mask them all */
358 pci_read_config_dword(dev,
359 msi_mask_bits_reg(pos, is_64bit_address(control)),
360 &maskbits);
361 temp = (1 << multi_msi_capable(control));
362 temp = ((temp - 1) & ~temp);
363 maskbits |= temp;
364 pci_write_config_dword(dev,
365 msi_mask_bits_reg(pos, is_64bit_address(control)),
366 maskbits);
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Configure MSI capability structure */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700369 irq = arch_setup_msi_irq(dev, entry);
370 if (irq < 0) {
371 kmem_cache_free(msi_cachep, entry);
372 return irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500373 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700374 entry->link.head = irq;
375 entry->link.tail = irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700376 dev->first_msi_irq = irq;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700377 set_irq_msi(irq, entry);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Set MSI enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800380 pci_intx(dev, 0); /* disable intx */
381 msi_set_enable(dev, 1);
382 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700384 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
388/**
389 * msix_capability_init - configure device's MSI-X capability
390 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700391 * @entries: pointer to an array of struct msix_entry entries
392 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600394 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700395 * single MSI-X irq. A return of zero indicates the successful setup of
396 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 **/
398static int msix_capability_init(struct pci_dev *dev,
399 struct msix_entry *entries, int nvec)
400{
401 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700402 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800403 unsigned long phys_addr;
404 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 u16 control;
406 u8 bir;
407 void __iomem *base;
408
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800409 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
412 /* Request & Map MSI-X table region */
413 pci_read_config_word(dev, msi_control_reg(pos), &control);
414 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800415
416 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800418 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
419 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
421 if (base == NULL)
422 return -ENOMEM;
423
424 /* MSI-X Table Initialization */
425 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700426 entry = alloc_msi_entry();
427 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700432 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 entry->msi_attrib.entry_nr = j;
434 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700435 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700436 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700437 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 entry->dev = dev;
439 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700440
441 /* Configure MSI-X capability structure */
442 irq = arch_setup_msi_irq(dev, entry);
443 if (irq < 0) {
444 kmem_cache_free(msi_cachep, entry);
445 break;
446 }
447 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700449 entry->link.head = irq;
450 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 head = entry;
452 } else {
453 entry->link.head = temp;
454 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700455 tail->link.tail = irq;
456 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700458 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 tail = entry;
Mark Maulefd58e552006-04-10 21:17:48 -0500460
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700461 set_irq_msi(irq, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700464 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 i--;
466 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700467 irq = (entries + i)->vector;
468 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 (entries + i)->vector = 0;
470 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700471 /* If we had some success report the number of irqs
472 * we succeeded in setting up.
473 */
474 if (avail <= 0)
475 avail = -EBUSY;
476 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700478 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* Set MSI-X enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800480 pci_intx(dev, 0); /* disable intx */
481 msix_set_enable(dev, 1);
482 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return 0;
485}
486
487/**
Brice Goglin24334a12006-08-31 01:55:07 -0400488 * pci_msi_supported - check whether MSI may be enabled on device
489 * @dev: pointer to the pci_dev data structure of MSI device function
490 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200491 * Look at global flags, the device itself, and its parent busses
492 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400493 **/
494static
495int pci_msi_supported(struct pci_dev * dev)
496{
497 struct pci_bus *bus;
498
Brice Goglin0306ebf2006-10-05 10:24:31 +0200499 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400500 if (!pci_msi_enable || !dev || dev->no_msi)
501 return -EINVAL;
502
Brice Goglin0306ebf2006-10-05 10:24:31 +0200503 /* Any bridge which does NOT route MSI transactions from it's
504 * secondary bus to it's primary bus must set NO_MSI flag on
505 * the secondary pci_bus.
506 * We expect only arch-specific PCI host bus controller driver
507 * or quirks for specific PCI bridges to be setting NO_MSI.
508 */
Brice Goglin24334a12006-08-31 01:55:07 -0400509 for (bus = dev->bus; bus; bus = bus->parent)
510 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
511 return -EINVAL;
512
513 return 0;
514}
515
516/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * pci_enable_msi - configure device's MSI capability structure
518 * @dev: pointer to the pci_dev data structure of MSI device function
519 *
520 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700521 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * MSI mode enabled on its hardware device function. A return of zero
523 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700524 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 **/
526int pci_enable_msi(struct pci_dev* dev)
527{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700528 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Brice Goglin24334a12006-08-31 01:55:07 -0400530 if (pci_msi_supported(dev) < 0)
531 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200532
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700533 status = msi_init();
534 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return status;
536
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700537 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
538 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EINVAL;
540
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700541 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700543 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800544 if (dev->msix_enabled) {
545 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
546 "Device already has MSI-X enabled\n",
547 pci_name(dev));
548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return status;
552}
553
554void pci_disable_msi(struct pci_dev* dev)
555{
556 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800557 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700559 if (!pci_msi_enable)
560 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700561 if (!dev)
562 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700563
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700564 if (!dev->msi_enabled)
565 return;
566
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800567 msi_set_enable(dev, 0);
568 pci_intx(dev, 1); /* enable intx */
569 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700570
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700571 entry = get_irq_msi(dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return;
574 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700575 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700577 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700578 pci_name(dev), dev->first_msi_irq);
579 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700581 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700582 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700583
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700584 /* Restore dev->irq to its default pin-assertion irq */
585 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700587 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700590static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct msi_desc *entry;
593 int head, entry_nr, type;
594 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700596 entry = get_irq_msi(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -EINVAL;
599 }
600 type = entry->msi_attrib.type;
601 entry_nr = entry->msi_attrib.entry_nr;
602 head = entry->link.head;
603 base = entry->mask_base;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700604 get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
605 get_irq_msi(entry->link.tail)->link.head = entry->link.head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700607 arch_teardown_msi_irq(irq);
608 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700611 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
612 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700614 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 return 0;
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/**
622 * pci_enable_msix - configure device's MSI-X capability structure
623 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700624 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700625 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
627 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700628 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * MSI-X mode enabled on its hardware device function. A return of zero
630 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700631 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700633 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * its request.
635 **/
636int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
637{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700638 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700639 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Brice Goglin24334a12006-08-31 01:55:07 -0400642 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EINVAL;
644
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700645 status = msi_init();
646 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return status;
648
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700649 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
650 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -EINVAL;
652
653 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 nr_entries = multi_msix_capable(control);
655 if (nvec > nr_entries)
656 return -EINVAL;
657
658 /* Check for any invalid entries */
659 for (i = 0; i < nvec; i++) {
660 if (entries[i].entry >= nr_entries)
661 return -EINVAL; /* invalid entry */
662 for (j = i + 1; j < nvec; j++) {
663 if (entries[i].entry == entries[j].entry)
664 return -EINVAL; /* duplicate entry */
665 }
666 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700667 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700668
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700669 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800670 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700672 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return status;
678}
679
680void pci_disable_msix(struct pci_dev* dev)
681{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700682 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700684 if (!pci_msi_enable)
685 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700686 if (!dev)
687 return;
688
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700689 if (!dev->msix_enabled)
690 return;
691
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800692 msix_set_enable(dev, 0);
693 pci_intx(dev, 1); /* enable intx */
694 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700695
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700696 irq = head = dev->first_msi_irq;
697 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700698 tail = get_irq_msi(irq)->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700699 if (irq_has_action(irq))
700 warning = 1;
701 else if (irq != head) /* Release MSI-X irq */
702 msi_free_irq(dev, irq);
703 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700705 msi_free_irq(dev, irq);
706 if (warning) {
707 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
708 "free_irq() on all MSI-X irqs\n",
709 pci_name(dev));
710 BUG_ON(warning > 0);
711 }
712 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700716 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * @dev: pointer to the pci_dev data structure of MSI(X) device function
718 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600719 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700720 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * allocated for this device function, are reclaimed to unused state,
722 * which may be used later on.
723 **/
724void msi_remove_pci_irq_vectors(struct pci_dev* dev)
725{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!pci_msi_enable || !dev)
727 return;
728
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700729 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700730 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700732 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700733 pci_name(dev), dev->first_msi_irq);
734 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700735 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700736 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700738 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700739 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 void __iomem *base = NULL;
741
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700742 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700744 tail = get_irq_msi(irq)->link.tail;
745 base = get_irq_msi(irq)->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700746 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700748 else if (irq != head) /* Release MSI-X irq */
749 msi_free_irq(dev, irq);
750 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700752 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 iounmap(base);
755 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700756 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 pci_name(dev));
758 BUG_ON(warning > 0);
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761}
762
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700763void pci_no_msi(void)
764{
765 pci_msi_enable = 0;
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768EXPORT_SYMBOL(pci_enable_msi);
769EXPORT_SYMBOL(pci_disable_msi);
770EXPORT_SYMBOL(pci_enable_msix);
771EXPORT_SYMBOL(pci_disable_msix);