blob: 01869b1782e4c460affcd7d655a340f620e9e7a8 [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
Eric W. Biederman1ce03372006-10-04 02:16:41 -070071static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct msi_desc *entry;
74
Eric W. Biederman5b912c12007-01-28 12:52:03 -070075 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -070076 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 switch (entry->msi_attrib.type) {
78 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -070079 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +090080 int pos;
81 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Eric W. Biederman277bc332006-10-04 02:16:57 -070083 pos = (long)entry->mask_base;
84 pci_read_config_dword(entry->dev, pos, &mask_bits);
85 mask_bits &= ~(1);
86 mask_bits |= flag;
87 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -080088 } else {
89 msi_set_enable(entry->dev, !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 case PCI_CAP_ID_MSIX:
93 {
94 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
95 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
96 writel(flag, entry->mask_base + offset);
97 break;
98 }
99 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700100 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
102 }
103}
104
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700105void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700106{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700107 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700108 switch(entry->msi_attrib.type) {
109 case PCI_CAP_ID_MSI:
110 {
111 struct pci_dev *dev = entry->dev;
112 int pos = entry->msi_attrib.pos;
113 u16 data;
114
115 pci_read_config_dword(dev, msi_lower_address_reg(pos),
116 &msg->address_lo);
117 if (entry->msi_attrib.is_64) {
118 pci_read_config_dword(dev, msi_upper_address_reg(pos),
119 &msg->address_hi);
120 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
121 } else {
122 msg->address_hi = 0;
123 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
124 }
125 msg->data = data;
126 break;
127 }
128 case PCI_CAP_ID_MSIX:
129 {
130 void __iomem *base;
131 base = entry->mask_base +
132 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
133
134 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
135 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
136 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
137 break;
138 }
139 default:
140 BUG();
141 }
142}
143
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700144void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700145{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700146 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700147 switch (entry->msi_attrib.type) {
148 case PCI_CAP_ID_MSI:
149 {
150 struct pci_dev *dev = entry->dev;
151 int pos = entry->msi_attrib.pos;
152
153 pci_write_config_dword(dev, msi_lower_address_reg(pos),
154 msg->address_lo);
155 if (entry->msi_attrib.is_64) {
156 pci_write_config_dword(dev, msi_upper_address_reg(pos),
157 msg->address_hi);
158 pci_write_config_word(dev, msi_data_reg(pos, 1),
159 msg->data);
160 } else {
161 pci_write_config_word(dev, msi_data_reg(pos, 0),
162 msg->data);
163 }
164 break;
165 }
166 case PCI_CAP_ID_MSIX:
167 {
168 void __iomem *base;
169 base = entry->mask_base +
170 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
171
172 writel(msg->address_lo,
173 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
174 writel(msg->address_hi,
175 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
176 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
177 break;
178 }
179 default:
180 BUG();
181 }
182}
183
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700184void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700186 msi_set_mask_bit(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700189void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700191 msi_set_mask_bit(irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700194static int msi_free_irq(struct pci_dev* dev, int irq);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int msi_init(void)
197{
198 static int status = -ENOMEM;
199
200 if (!status)
201 return status;
202
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700203 status = msi_cache_init();
204 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 pci_msi_enable = 0;
206 printk(KERN_WARNING "PCI: MSI cache init failed\n");
207 return status;
208 }
Mark Maulefd58e552006-04-10 21:17:48 -0500209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return status;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static struct msi_desc* alloc_msi_entry(void)
214{
215 struct msi_desc *entry;
216
Pekka J Enberg57181782006-09-27 01:51:03 -0700217 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!entry)
219 return NULL;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 entry->link.tail = entry->link.head = 0; /* single message */
222 entry->dev = NULL;
223
224 return entry;
225}
226
Shaohua Li41017f02006-02-08 17:11:38 +0800227#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100228static int __pci_save_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800229{
230 int pos, i = 0;
231 u16 control;
232 struct pci_cap_saved_state *save_state;
233 u32 *cap;
234
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800235 if (!dev->msi_enabled)
Shaohua Li41017f02006-02-08 17:11:38 +0800236 return 0;
237
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800238 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
239 if (pos <= 0)
Shaohua Li41017f02006-02-08 17:11:38 +0800240 return 0;
241
242 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
243 GFP_KERNEL);
244 if (!save_state) {
245 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
246 return -ENOMEM;
247 }
248 cap = &save_state->data[0];
249
250 pci_read_config_dword(dev, pos, &cap[i++]);
251 control = cap[0] >> 16;
252 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
253 if (control & PCI_MSI_FLAGS_64BIT) {
254 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
255 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
256 } else
257 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
258 if (control & PCI_MSI_FLAGS_MASKBIT)
259 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800260 save_state->cap_nr = PCI_CAP_ID_MSI;
261 pci_add_saved_cap(dev, save_state);
262 return 0;
263}
264
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100265static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800266{
267 int i = 0, pos;
268 u16 control;
269 struct pci_cap_saved_state *save_state;
270 u32 *cap;
271
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800272 if (!dev->msi_enabled)
273 return;
274
Shaohua Li41017f02006-02-08 17:11:38 +0800275 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
276 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
277 if (!save_state || pos <= 0)
278 return;
279 cap = &save_state->data[0];
280
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800281 pci_intx(dev, 0); /* disable intx */
Shaohua Li41017f02006-02-08 17:11:38 +0800282 control = cap[i++] >> 16;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800283 msi_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800284 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
285 if (control & PCI_MSI_FLAGS_64BIT) {
286 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
287 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
288 } else
289 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
290 if (control & PCI_MSI_FLAGS_MASKBIT)
291 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
292 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800293 pci_remove_saved_cap(save_state);
294 kfree(save_state);
295}
296
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100297static int __pci_save_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800298{
299 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700300 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800301 u16 control;
302 struct pci_cap_saved_state *save_state;
303
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700304 if (!dev->msix_enabled)
305 return 0;
306
Shaohua Li41017f02006-02-08 17:11:38 +0800307 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800308 if (pos <= 0)
Shaohua Li41017f02006-02-08 17:11:38 +0800309 return 0;
310
Mark Maulefd58e552006-04-10 21:17:48 -0500311 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800312 pci_read_config_word(dev, msi_control_reg(pos), &control);
Shaohua Li41017f02006-02-08 17:11:38 +0800313 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
314 GFP_KERNEL);
315 if (!save_state) {
316 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
317 return -ENOMEM;
318 }
319 *((u16 *)&save_state->data[0]) = control;
320
Mark Maulefd58e552006-04-10 21:17:48 -0500321 /* save the table */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700322 irq = head = dev->first_msi_irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500323 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500324 struct msi_desc *entry;
325
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700326 entry = get_irq_msi(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700327 read_msi_msg(irq, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500328
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700329 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700330 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500331 }
Mark Maulefd58e552006-04-10 21:17:48 -0500332
Shaohua Li41017f02006-02-08 17:11:38 +0800333 save_state->cap_nr = PCI_CAP_ID_MSIX;
334 pci_add_saved_cap(dev, save_state);
335 return 0;
336}
337
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100338int pci_save_msi_state(struct pci_dev *dev)
339{
340 int rc;
341
342 rc = __pci_save_msi_state(dev);
343 if (rc)
344 return rc;
345
346 rc = __pci_save_msix_state(dev);
347
348 return rc;
349}
350
351static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800352{
353 u16 save;
354 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700355 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800356 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800357 struct pci_cap_saved_state *save_state;
358
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700359 if (!dev->msix_enabled)
360 return;
361
Shaohua Li41017f02006-02-08 17:11:38 +0800362 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
363 if (!save_state)
364 return;
365 save = *((u16 *)&save_state->data[0]);
366 pci_remove_saved_cap(save_state);
367 kfree(save_state);
368
369 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
370 if (pos <= 0)
371 return;
372
373 /* route the table */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800374 pci_intx(dev, 0); /* disable intx */
375 msix_set_enable(dev, 0);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700376 irq = head = dev->first_msi_irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800377 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700378 entry = get_irq_msi(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700379 write_msi_msg(irq, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800380
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700381 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700382 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800383 }
Shaohua Li41017f02006-02-08 17:11:38 +0800384
385 pci_write_config_word(dev, msi_control_reg(pos), save);
Shaohua Li41017f02006-02-08 17:11:38 +0800386}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100387
388void pci_restore_msi_state(struct pci_dev *dev)
389{
390 __pci_restore_msi_state(dev);
391 __pci_restore_msix_state(dev);
392}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900393#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
396 * msi_capability_init - configure device's MSI capability structure
397 * @dev: pointer to the pci_dev data structure of MSI device function
398 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600399 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700400 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700402 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 **/
404static int msi_capability_init(struct pci_dev *dev)
405{
406 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700407 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 u16 control;
409
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800410 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
413 pci_read_config_word(dev, msi_control_reg(pos), &control);
414 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700415 entry = alloc_msi_entry();
416 if (!entry)
417 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700420 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 entry->msi_attrib.entry_nr = 0;
422 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700423 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700424 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (is_mask_bit_support(control)) {
426 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
427 is_64bit_address(control));
428 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700429 entry->dev = dev;
430 if (entry->msi_attrib.maskbit) {
431 unsigned int maskbits, temp;
432 /* All MSIs are unmasked by default, Mask them all */
433 pci_read_config_dword(dev,
434 msi_mask_bits_reg(pos, is_64bit_address(control)),
435 &maskbits);
436 temp = (1 << multi_msi_capable(control));
437 temp = ((temp - 1) & ~temp);
438 maskbits |= temp;
439 pci_write_config_dword(dev,
440 msi_mask_bits_reg(pos, is_64bit_address(control)),
441 maskbits);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* Configure MSI capability structure */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700444 irq = arch_setup_msi_irq(dev, entry);
445 if (irq < 0) {
446 kmem_cache_free(msi_cachep, entry);
447 return irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500448 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700449 entry->link.head = irq;
450 entry->link.tail = irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700451 dev->first_msi_irq = irq;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700452 set_irq_msi(irq, entry);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Set MSI enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800455 pci_intx(dev, 0); /* disable intx */
456 msi_set_enable(dev, 1);
457 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700459 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461}
462
463/**
464 * msix_capability_init - configure device's MSI-X capability
465 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700466 * @entries: pointer to an array of struct msix_entry entries
467 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600469 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700470 * single MSI-X irq. A return of zero indicates the successful setup of
471 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 **/
473static int msix_capability_init(struct pci_dev *dev,
474 struct msix_entry *entries, int nvec)
475{
476 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700477 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800478 unsigned long phys_addr;
479 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 u16 control;
481 u8 bir;
482 void __iomem *base;
483
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800484 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
487 /* Request & Map MSI-X table region */
488 pci_read_config_word(dev, msi_control_reg(pos), &control);
489 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800490
491 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800493 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
494 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
496 if (base == NULL)
497 return -ENOMEM;
498
499 /* MSI-X Table Initialization */
500 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700501 entry = alloc_msi_entry();
502 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700507 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 entry->msi_attrib.entry_nr = j;
509 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700510 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700511 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 entry->dev = dev;
513 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700514
515 /* Configure MSI-X capability structure */
516 irq = arch_setup_msi_irq(dev, entry);
517 if (irq < 0) {
518 kmem_cache_free(msi_cachep, entry);
519 break;
520 }
521 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700523 entry->link.head = irq;
524 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 head = entry;
526 } else {
527 entry->link.head = temp;
528 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700529 tail->link.tail = irq;
530 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700532 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 tail = entry;
Mark Maulefd58e552006-04-10 21:17:48 -0500534
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700535 set_irq_msi(irq, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700538 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 i--;
540 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700541 irq = (entries + i)->vector;
542 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 (entries + i)->vector = 0;
544 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700545 /* If we had some success report the number of irqs
546 * we succeeded in setting up.
547 */
548 if (avail <= 0)
549 avail = -EBUSY;
550 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700552 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Set MSI-X enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800554 pci_intx(dev, 0); /* disable intx */
555 msix_set_enable(dev, 1);
556 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 return 0;
559}
560
561/**
Brice Goglin24334a12006-08-31 01:55:07 -0400562 * pci_msi_supported - check whether MSI may be enabled on device
563 * @dev: pointer to the pci_dev data structure of MSI device function
564 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200565 * Look at global flags, the device itself, and its parent busses
566 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400567 **/
568static
569int pci_msi_supported(struct pci_dev * dev)
570{
571 struct pci_bus *bus;
572
Brice Goglin0306ebf2006-10-05 10:24:31 +0200573 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400574 if (!pci_msi_enable || !dev || dev->no_msi)
575 return -EINVAL;
576
Brice Goglin0306ebf2006-10-05 10:24:31 +0200577 /* Any bridge which does NOT route MSI transactions from it's
578 * secondary bus to it's primary bus must set NO_MSI flag on
579 * the secondary pci_bus.
580 * We expect only arch-specific PCI host bus controller driver
581 * or quirks for specific PCI bridges to be setting NO_MSI.
582 */
Brice Goglin24334a12006-08-31 01:55:07 -0400583 for (bus = dev->bus; bus; bus = bus->parent)
584 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
585 return -EINVAL;
586
587 return 0;
588}
589
590/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * pci_enable_msi - configure device's MSI capability structure
592 * @dev: pointer to the pci_dev data structure of MSI device function
593 *
594 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700595 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * MSI mode enabled on its hardware device function. A return of zero
597 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700598 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 **/
600int pci_enable_msi(struct pci_dev* dev)
601{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700602 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Brice Goglin24334a12006-08-31 01:55:07 -0400604 if (pci_msi_supported(dev) < 0)
605 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200606
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700607 status = msi_init();
608 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return status;
610
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700611 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
612 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EINVAL;
614
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700615 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700617 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800618 if (dev->msix_enabled) {
619 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
620 "Device already has MSI-X enabled\n",
621 pci_name(dev));
622 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return status;
626}
627
628void pci_disable_msi(struct pci_dev* dev)
629{
630 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800631 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700633 if (!pci_msi_enable)
634 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700635 if (!dev)
636 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700637
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700638 if (!dev->msi_enabled)
639 return;
640
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800641 msi_set_enable(dev, 0);
642 pci_intx(dev, 1); /* enable intx */
643 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700644
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700645 entry = get_irq_msi(dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return;
648 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700649 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700651 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700652 pci_name(dev), dev->first_msi_irq);
653 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700655 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700656 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700657
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700658 /* Restore dev->irq to its default pin-assertion irq */
659 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700661 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700664static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct msi_desc *entry;
667 int head, entry_nr, type;
668 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700670 entry = get_irq_msi(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -EINVAL;
673 }
674 type = entry->msi_attrib.type;
675 entry_nr = entry->msi_attrib.entry_nr;
676 head = entry->link.head;
677 base = entry->mask_base;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700678 get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
679 get_irq_msi(entry->link.tail)->link.head = entry->link.head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700681 arch_teardown_msi_irq(irq);
682 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700685 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
686 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700688 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 return 0;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/**
696 * pci_enable_msix - configure device's MSI-X capability structure
697 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700698 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700699 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
701 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700702 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * MSI-X mode enabled on its hardware device function. A return of zero
704 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700705 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700707 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * its request.
709 **/
710int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
711{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700712 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700713 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Brice Goglin24334a12006-08-31 01:55:07 -0400716 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -EINVAL;
718
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700719 status = msi_init();
720 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return status;
722
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700723 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
724 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EINVAL;
726
727 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 nr_entries = multi_msix_capable(control);
729 if (nvec > nr_entries)
730 return -EINVAL;
731
732 /* Check for any invalid entries */
733 for (i = 0; i < nvec; i++) {
734 if (entries[i].entry >= nr_entries)
735 return -EINVAL; /* invalid entry */
736 for (j = i + 1; j < nvec; j++) {
737 if (entries[i].entry == entries[j].entry)
738 return -EINVAL; /* duplicate entry */
739 }
740 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700741 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700742
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700743 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800744 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700746 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EINVAL;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return status;
752}
753
754void pci_disable_msix(struct pci_dev* dev)
755{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700756 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700758 if (!pci_msi_enable)
759 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700760 if (!dev)
761 return;
762
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700763 if (!dev->msix_enabled)
764 return;
765
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800766 msix_set_enable(dev, 0);
767 pci_intx(dev, 1); /* enable intx */
768 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700769
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700770 irq = head = dev->first_msi_irq;
771 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700772 tail = get_irq_msi(irq)->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700773 if (irq_has_action(irq))
774 warning = 1;
775 else if (irq != head) /* Release MSI-X irq */
776 msi_free_irq(dev, irq);
777 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700779 msi_free_irq(dev, irq);
780 if (warning) {
781 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
782 "free_irq() on all MSI-X irqs\n",
783 pci_name(dev));
784 BUG_ON(warning > 0);
785 }
786 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700790 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * @dev: pointer to the pci_dev data structure of MSI(X) device function
792 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600793 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700794 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 * allocated for this device function, are reclaimed to unused state,
796 * which may be used later on.
797 **/
798void msi_remove_pci_irq_vectors(struct pci_dev* dev)
799{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!pci_msi_enable || !dev)
801 return;
802
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700803 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700804 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700806 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700807 pci_name(dev), dev->first_msi_irq);
808 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700809 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700810 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700812 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700813 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 void __iomem *base = NULL;
815
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700816 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700818 tail = get_irq_msi(irq)->link.tail;
819 base = get_irq_msi(irq)->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700820 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700822 else if (irq != head) /* Release MSI-X irq */
823 msi_free_irq(dev, irq);
824 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700826 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 iounmap(base);
829 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700830 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 pci_name(dev));
832 BUG_ON(warning > 0);
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835}
836
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700837void pci_no_msi(void)
838{
839 pci_msi_enable = 0;
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842EXPORT_SYMBOL(pci_enable_msi);
843EXPORT_SYMBOL(pci_disable_msi);
844EXPORT_SYMBOL(pci_enable_msix);
845EXPORT_SYMBOL(pci_disable_msix);