blob: c43e7d22e180ad92143e763fab9f6598fe7209b5 [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);
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 case PCI_CAP_ID_MSIX:
91 {
92 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
93 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
94 writel(flag, entry->mask_base + offset);
95 break;
96 }
97 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -070098 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 break;
100 }
101}
102
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700103void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700104{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700105 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700106 switch(entry->msi_attrib.type) {
107 case PCI_CAP_ID_MSI:
108 {
109 struct pci_dev *dev = entry->dev;
110 int pos = entry->msi_attrib.pos;
111 u16 data;
112
113 pci_read_config_dword(dev, msi_lower_address_reg(pos),
114 &msg->address_lo);
115 if (entry->msi_attrib.is_64) {
116 pci_read_config_dword(dev, msi_upper_address_reg(pos),
117 &msg->address_hi);
118 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
119 } else {
120 msg->address_hi = 0;
121 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
122 }
123 msg->data = data;
124 break;
125 }
126 case PCI_CAP_ID_MSIX:
127 {
128 void __iomem *base;
129 base = entry->mask_base +
130 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
131
132 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
133 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
134 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
135 break;
136 }
137 default:
138 BUG();
139 }
140}
141
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700142void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700143{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700144 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700145 switch (entry->msi_attrib.type) {
146 case PCI_CAP_ID_MSI:
147 {
148 struct pci_dev *dev = entry->dev;
149 int pos = entry->msi_attrib.pos;
150
151 pci_write_config_dword(dev, msi_lower_address_reg(pos),
152 msg->address_lo);
153 if (entry->msi_attrib.is_64) {
154 pci_write_config_dword(dev, msi_upper_address_reg(pos),
155 msg->address_hi);
156 pci_write_config_word(dev, msi_data_reg(pos, 1),
157 msg->data);
158 } else {
159 pci_write_config_word(dev, msi_data_reg(pos, 0),
160 msg->data);
161 }
162 break;
163 }
164 case PCI_CAP_ID_MSIX:
165 {
166 void __iomem *base;
167 base = entry->mask_base +
168 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
169
170 writel(msg->address_lo,
171 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
172 writel(msg->address_hi,
173 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
174 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
175 break;
176 }
177 default:
178 BUG();
179 }
180}
181
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700182void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700184 msi_set_mask_bit(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700187void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700189 msi_set_mask_bit(irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700192static int msi_free_irq(struct pci_dev* dev, int irq);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int msi_init(void)
195{
196 static int status = -ENOMEM;
197
198 if (!status)
199 return status;
200
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700201 status = msi_cache_init();
202 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 pci_msi_enable = 0;
204 printk(KERN_WARNING "PCI: MSI cache init failed\n");
205 return status;
206 }
Mark Maulefd58e552006-04-10 21:17:48 -0500207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return status;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static struct msi_desc* alloc_msi_entry(void)
212{
213 struct msi_desc *entry;
214
Pekka J Enberg57181782006-09-27 01:51:03 -0700215 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!entry)
217 return NULL;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 entry->link.tail = entry->link.head = 0; /* single message */
220 entry->dev = NULL;
221
222 return entry;
223}
224
Shaohua Li41017f02006-02-08 17:11:38 +0800225#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100226static int __pci_save_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800227{
228 int pos, i = 0;
229 u16 control;
230 struct pci_cap_saved_state *save_state;
231 u32 *cap;
232
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800233 if (!dev->msi_enabled)
Shaohua Li41017f02006-02-08 17:11:38 +0800234 return 0;
235
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800236 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
237 if (pos <= 0)
Shaohua Li41017f02006-02-08 17:11:38 +0800238 return 0;
239
240 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
241 GFP_KERNEL);
242 if (!save_state) {
243 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
244 return -ENOMEM;
245 }
246 cap = &save_state->data[0];
247
248 pci_read_config_dword(dev, pos, &cap[i++]);
249 control = cap[0] >> 16;
250 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
251 if (control & PCI_MSI_FLAGS_64BIT) {
252 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
253 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
254 } else
255 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
256 if (control & PCI_MSI_FLAGS_MASKBIT)
257 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800258 save_state->cap_nr = PCI_CAP_ID_MSI;
259 pci_add_saved_cap(dev, save_state);
260 return 0;
261}
262
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100263static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800264{
265 int i = 0, pos;
266 u16 control;
267 struct pci_cap_saved_state *save_state;
268 u32 *cap;
269
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800270 if (!dev->msi_enabled)
271 return;
272
Shaohua Li41017f02006-02-08 17:11:38 +0800273 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
274 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
275 if (!save_state || pos <= 0)
276 return;
277 cap = &save_state->data[0];
278
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800279 pci_intx(dev, 0); /* disable intx */
Shaohua Li41017f02006-02-08 17:11:38 +0800280 control = cap[i++] >> 16;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800281 msi_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800282 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
283 if (control & PCI_MSI_FLAGS_64BIT) {
284 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
285 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
286 } else
287 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
288 if (control & PCI_MSI_FLAGS_MASKBIT)
289 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
290 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800291 pci_remove_saved_cap(save_state);
292 kfree(save_state);
293}
294
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100295static int __pci_save_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800296{
297 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700298 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800299 u16 control;
300 struct pci_cap_saved_state *save_state;
301
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700302 if (!dev->msix_enabled)
303 return 0;
304
Shaohua Li41017f02006-02-08 17:11:38 +0800305 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800306 if (pos <= 0)
Shaohua Li41017f02006-02-08 17:11:38 +0800307 return 0;
308
Mark Maulefd58e552006-04-10 21:17:48 -0500309 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800310 pci_read_config_word(dev, msi_control_reg(pos), &control);
Shaohua Li41017f02006-02-08 17:11:38 +0800311 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
312 GFP_KERNEL);
313 if (!save_state) {
314 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
315 return -ENOMEM;
316 }
317 *((u16 *)&save_state->data[0]) = control;
318
Mark Maulefd58e552006-04-10 21:17:48 -0500319 /* save the table */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700320 irq = head = dev->first_msi_irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500321 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500322 struct msi_desc *entry;
323
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700324 entry = get_irq_msi(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700325 read_msi_msg(irq, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500326
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700327 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700328 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500329 }
Mark Maulefd58e552006-04-10 21:17:48 -0500330
Shaohua Li41017f02006-02-08 17:11:38 +0800331 save_state->cap_nr = PCI_CAP_ID_MSIX;
332 pci_add_saved_cap(dev, save_state);
333 return 0;
334}
335
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100336int pci_save_msi_state(struct pci_dev *dev)
337{
338 int rc;
339
340 rc = __pci_save_msi_state(dev);
341 if (rc)
342 return rc;
343
344 rc = __pci_save_msix_state(dev);
345
346 return rc;
347}
348
349static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800350{
351 u16 save;
352 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700353 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800354 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800355 struct pci_cap_saved_state *save_state;
356
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700357 if (!dev->msix_enabled)
358 return;
359
Shaohua Li41017f02006-02-08 17:11:38 +0800360 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
361 if (!save_state)
362 return;
363 save = *((u16 *)&save_state->data[0]);
364 pci_remove_saved_cap(save_state);
365 kfree(save_state);
366
367 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
368 if (pos <= 0)
369 return;
370
371 /* route the table */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800372 pci_intx(dev, 0); /* disable intx */
373 msix_set_enable(dev, 0);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700374 irq = head = dev->first_msi_irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800375 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700376 entry = get_irq_msi(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700377 write_msi_msg(irq, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800378
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700379 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700380 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800381 }
Shaohua Li41017f02006-02-08 17:11:38 +0800382
383 pci_write_config_word(dev, msi_control_reg(pos), save);
Shaohua Li41017f02006-02-08 17:11:38 +0800384}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100385
386void pci_restore_msi_state(struct pci_dev *dev)
387{
388 __pci_restore_msi_state(dev);
389 __pci_restore_msix_state(dev);
390}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900391#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
394 * msi_capability_init - configure device's MSI capability structure
395 * @dev: pointer to the pci_dev data structure of MSI device function
396 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600397 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700398 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700400 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 **/
402static int msi_capability_init(struct pci_dev *dev)
403{
404 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700405 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 u16 control;
407
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800408 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
411 pci_read_config_word(dev, msi_control_reg(pos), &control);
412 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700413 entry = alloc_msi_entry();
414 if (!entry)
415 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700418 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 entry->msi_attrib.entry_nr = 0;
420 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700421 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700422 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (is_mask_bit_support(control)) {
424 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
425 is_64bit_address(control));
426 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700427 entry->dev = dev;
428 if (entry->msi_attrib.maskbit) {
429 unsigned int maskbits, temp;
430 /* All MSIs are unmasked by default, Mask them all */
431 pci_read_config_dword(dev,
432 msi_mask_bits_reg(pos, is_64bit_address(control)),
433 &maskbits);
434 temp = (1 << multi_msi_capable(control));
435 temp = ((temp - 1) & ~temp);
436 maskbits |= temp;
437 pci_write_config_dword(dev,
438 msi_mask_bits_reg(pos, is_64bit_address(control)),
439 maskbits);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* Configure MSI capability structure */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700442 irq = arch_setup_msi_irq(dev, entry);
443 if (irq < 0) {
444 kmem_cache_free(msi_cachep, entry);
445 return irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500446 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700447 entry->link.head = irq;
448 entry->link.tail = irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700449 dev->first_msi_irq = irq;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700450 set_irq_msi(irq, entry);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* Set MSI enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800453 pci_intx(dev, 0); /* disable intx */
454 msi_set_enable(dev, 1);
455 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700457 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459}
460
461/**
462 * msix_capability_init - configure device's MSI-X capability
463 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700464 * @entries: pointer to an array of struct msix_entry entries
465 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600467 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700468 * single MSI-X irq. A return of zero indicates the successful setup of
469 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 **/
471static int msix_capability_init(struct pci_dev *dev,
472 struct msix_entry *entries, int nvec)
473{
474 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700475 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800476 unsigned long phys_addr;
477 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 u16 control;
479 u8 bir;
480 void __iomem *base;
481
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800482 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
485 /* Request & Map MSI-X table region */
486 pci_read_config_word(dev, msi_control_reg(pos), &control);
487 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800488
489 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800491 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
492 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
494 if (base == NULL)
495 return -ENOMEM;
496
497 /* MSI-X Table Initialization */
498 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700499 entry = alloc_msi_entry();
500 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700505 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 entry->msi_attrib.entry_nr = j;
507 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700508 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700509 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 entry->dev = dev;
511 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700512
513 /* Configure MSI-X capability structure */
514 irq = arch_setup_msi_irq(dev, entry);
515 if (irq < 0) {
516 kmem_cache_free(msi_cachep, entry);
517 break;
518 }
519 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700521 entry->link.head = irq;
522 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 head = entry;
524 } else {
525 entry->link.head = temp;
526 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700527 tail->link.tail = irq;
528 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700530 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 tail = entry;
Mark Maulefd58e552006-04-10 21:17:48 -0500532
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700533 set_irq_msi(irq, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700536 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 i--;
538 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700539 irq = (entries + i)->vector;
540 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 (entries + i)->vector = 0;
542 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700543 /* If we had some success report the number of irqs
544 * we succeeded in setting up.
545 */
546 if (avail <= 0)
547 avail = -EBUSY;
548 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700550 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Set MSI-X enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800552 pci_intx(dev, 0); /* disable intx */
553 msix_set_enable(dev, 1);
554 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return 0;
557}
558
559/**
Brice Goglin24334a12006-08-31 01:55:07 -0400560 * pci_msi_supported - check whether MSI may be enabled on device
561 * @dev: pointer to the pci_dev data structure of MSI device function
562 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200563 * Look at global flags, the device itself, and its parent busses
564 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400565 **/
566static
567int pci_msi_supported(struct pci_dev * dev)
568{
569 struct pci_bus *bus;
570
Brice Goglin0306ebf2006-10-05 10:24:31 +0200571 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400572 if (!pci_msi_enable || !dev || dev->no_msi)
573 return -EINVAL;
574
Brice Goglin0306ebf2006-10-05 10:24:31 +0200575 /* Any bridge which does NOT route MSI transactions from it's
576 * secondary bus to it's primary bus must set NO_MSI flag on
577 * the secondary pci_bus.
578 * We expect only arch-specific PCI host bus controller driver
579 * or quirks for specific PCI bridges to be setting NO_MSI.
580 */
Brice Goglin24334a12006-08-31 01:55:07 -0400581 for (bus = dev->bus; bus; bus = bus->parent)
582 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
583 return -EINVAL;
584
585 return 0;
586}
587
588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * pci_enable_msi - configure device's MSI capability structure
590 * @dev: pointer to the pci_dev data structure of MSI device function
591 *
592 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700593 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * MSI mode enabled on its hardware device function. A return of zero
595 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700596 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 **/
598int pci_enable_msi(struct pci_dev* dev)
599{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700600 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Brice Goglin24334a12006-08-31 01:55:07 -0400602 if (pci_msi_supported(dev) < 0)
603 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200604
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700605 status = msi_init();
606 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return status;
608
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700609 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
610 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -EINVAL;
612
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700613 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700615 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800616 if (dev->msix_enabled) {
617 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
618 "Device already has MSI-X enabled\n",
619 pci_name(dev));
620 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return status;
624}
625
626void pci_disable_msi(struct pci_dev* dev)
627{
628 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800629 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700631 if (!pci_msi_enable)
632 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700633 if (!dev)
634 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700635
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700636 if (!dev->msi_enabled)
637 return;
638
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800639 msi_set_enable(dev, 0);
640 pci_intx(dev, 1); /* enable intx */
641 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700642
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700643 entry = get_irq_msi(dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return;
646 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700647 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700649 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700650 pci_name(dev), dev->first_msi_irq);
651 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700653 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700654 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700655
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700656 /* Restore dev->irq to its default pin-assertion irq */
657 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700659 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700662static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct msi_desc *entry;
665 int head, entry_nr, type;
666 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700668 entry = get_irq_msi(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EINVAL;
671 }
672 type = entry->msi_attrib.type;
673 entry_nr = entry->msi_attrib.entry_nr;
674 head = entry->link.head;
675 base = entry->mask_base;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700676 get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
677 get_irq_msi(entry->link.tail)->link.head = entry->link.head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700679 arch_teardown_msi_irq(irq);
680 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700683 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
684 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700686 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 return 0;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/**
694 * pci_enable_msix - configure device's MSI-X capability structure
695 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700696 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700697 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
699 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700700 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * MSI-X mode enabled on its hardware device function. A return of zero
702 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700703 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700705 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * its request.
707 **/
708int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
709{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700710 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700711 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Brice Goglin24334a12006-08-31 01:55:07 -0400714 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EINVAL;
716
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700717 status = msi_init();
718 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return status;
720
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700721 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
722 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -EINVAL;
724
725 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 nr_entries = multi_msix_capable(control);
727 if (nvec > nr_entries)
728 return -EINVAL;
729
730 /* Check for any invalid entries */
731 for (i = 0; i < nvec; i++) {
732 if (entries[i].entry >= nr_entries)
733 return -EINVAL; /* invalid entry */
734 for (j = i + 1; j < nvec; j++) {
735 if (entries[i].entry == entries[j].entry)
736 return -EINVAL; /* duplicate entry */
737 }
738 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700739 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700740
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700741 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800742 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700744 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EINVAL;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return status;
750}
751
752void pci_disable_msix(struct pci_dev* dev)
753{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700754 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700756 if (!pci_msi_enable)
757 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700758 if (!dev)
759 return;
760
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700761 if (!dev->msix_enabled)
762 return;
763
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800764 msix_set_enable(dev, 0);
765 pci_intx(dev, 1); /* enable intx */
766 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700767
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700768 irq = head = dev->first_msi_irq;
769 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700770 tail = get_irq_msi(irq)->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700771 if (irq_has_action(irq))
772 warning = 1;
773 else if (irq != head) /* Release MSI-X irq */
774 msi_free_irq(dev, irq);
775 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700777 msi_free_irq(dev, irq);
778 if (warning) {
779 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
780 "free_irq() on all MSI-X irqs\n",
781 pci_name(dev));
782 BUG_ON(warning > 0);
783 }
784 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700788 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * @dev: pointer to the pci_dev data structure of MSI(X) device function
790 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600791 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700792 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * allocated for this device function, are reclaimed to unused state,
794 * which may be used later on.
795 **/
796void msi_remove_pci_irq_vectors(struct pci_dev* dev)
797{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!pci_msi_enable || !dev)
799 return;
800
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700801 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700802 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700804 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700805 pci_name(dev), dev->first_msi_irq);
806 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700807 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700808 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700810 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700811 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 void __iomem *base = NULL;
813
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700814 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700816 tail = get_irq_msi(irq)->link.tail;
817 base = get_irq_msi(irq)->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700818 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700820 else if (irq != head) /* Release MSI-X irq */
821 msi_free_irq(dev, irq);
822 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700824 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 iounmap(base);
827 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700828 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 pci_name(dev));
830 BUG_ON(warning > 0);
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833}
834
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700835void pci_no_msi(void)
836{
837 pci_msi_enable = 0;
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840EXPORT_SYMBOL(pci_enable_msi);
841EXPORT_SYMBOL(pci_disable_msi);
842EXPORT_SYMBOL(pci_enable_msix);
843EXPORT_SYMBOL(pci_disable_msix);