blob: 529113dc3e22dce76fc2e3a3b0bb2874f4aca8a1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL };
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache* msi_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int msi_cache_init(void)
33{
Pekka J Enberg57181782006-09-27 01:51:03 -070034 msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
35 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (!msi_cachep)
37 return -ENOMEM;
38
39 return 0;
40}
41
Eric W. Biederman1ce03372006-10-04 02:16:41 -070042static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct msi_desc *entry;
45
Eric W. Biederman1ce03372006-10-04 02:16:41 -070046 entry = msi_desc[irq];
Eric W. Biederman277bc332006-10-04 02:16:57 -070047 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 switch (entry->msi_attrib.type) {
49 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -070050 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +090051 int pos;
52 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Eric W. Biederman277bc332006-10-04 02:16:57 -070054 pos = (long)entry->mask_base;
55 pci_read_config_dword(entry->dev, pos, &mask_bits);
56 mask_bits &= ~(1);
57 mask_bits |= flag;
58 pci_write_config_dword(entry->dev, pos, mask_bits);
59 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 case PCI_CAP_ID_MSIX:
62 {
63 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
64 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
65 writel(flag, entry->mask_base + offset);
66 break;
67 }
68 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -070069 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
71 }
72}
73
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070074void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -070075{
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070076 struct msi_desc *entry = get_irq_data(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -070077 switch(entry->msi_attrib.type) {
78 case PCI_CAP_ID_MSI:
79 {
80 struct pci_dev *dev = entry->dev;
81 int pos = entry->msi_attrib.pos;
82 u16 data;
83
84 pci_read_config_dword(dev, msi_lower_address_reg(pos),
85 &msg->address_lo);
86 if (entry->msi_attrib.is_64) {
87 pci_read_config_dword(dev, msi_upper_address_reg(pos),
88 &msg->address_hi);
89 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
90 } else {
91 msg->address_hi = 0;
92 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
93 }
94 msg->data = data;
95 break;
96 }
97 case PCI_CAP_ID_MSIX:
98 {
99 void __iomem *base;
100 base = entry->mask_base +
101 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
102
103 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
104 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
105 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
106 break;
107 }
108 default:
109 BUG();
110 }
111}
112
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700113void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700114{
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700115 struct msi_desc *entry = get_irq_data(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700116 switch (entry->msi_attrib.type) {
117 case PCI_CAP_ID_MSI:
118 {
119 struct pci_dev *dev = entry->dev;
120 int pos = entry->msi_attrib.pos;
121
122 pci_write_config_dword(dev, msi_lower_address_reg(pos),
123 msg->address_lo);
124 if (entry->msi_attrib.is_64) {
125 pci_write_config_dword(dev, msi_upper_address_reg(pos),
126 msg->address_hi);
127 pci_write_config_word(dev, msi_data_reg(pos, 1),
128 msg->data);
129 } else {
130 pci_write_config_word(dev, msi_data_reg(pos, 0),
131 msg->data);
132 }
133 break;
134 }
135 case PCI_CAP_ID_MSIX:
136 {
137 void __iomem *base;
138 base = entry->mask_base +
139 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
140
141 writel(msg->address_lo,
142 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
143 writel(msg->address_hi,
144 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
145 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
146 break;
147 }
148 default:
149 BUG();
150 }
151}
152
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700153void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700155 msi_set_mask_bit(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700158void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700160 msi_set_mask_bit(irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700163static int msi_free_irq(struct pci_dev* dev, int irq);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static int msi_init(void)
166{
167 static int status = -ENOMEM;
168
169 if (!status)
170 return status;
171
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700172 status = msi_cache_init();
173 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 pci_msi_enable = 0;
175 printk(KERN_WARNING "PCI: MSI cache init failed\n");
176 return status;
177 }
Mark Maulefd58e552006-04-10 21:17:48 -0500178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return status;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static struct msi_desc* alloc_msi_entry(void)
183{
184 struct msi_desc *entry;
185
Pekka J Enberg57181782006-09-27 01:51:03 -0700186 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!entry)
188 return NULL;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 entry->link.tail = entry->link.head = 0; /* single message */
191 entry->dev = NULL;
192
193 return entry;
194}
195
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700196static void attach_msi_entry(struct msi_desc *entry, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700198 msi_desc[irq] = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700201static int create_msi_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700203 struct msi_desc *entry;
204 int irq;
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100205
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700206 entry = alloc_msi_entry();
207 if (!entry)
208 return -ENOMEM;
209
210 irq = create_irq();
211 if (irq < 0) {
212 kmem_cache_free(msi_cachep, entry);
213 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700215
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700216 set_irq_data(irq, entry);
217
218 return irq;
219}
220
221static void destroy_msi_irq(unsigned int irq)
222{
223 struct msi_desc *entry;
224
225 entry = get_irq_data(irq);
226 set_irq_chip(irq, NULL);
227 set_irq_data(irq, NULL);
228 destroy_irq(irq);
229 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
233{
234 u16 control;
235
236 pci_read_config_word(dev, msi_control_reg(pos), &control);
237 if (type == PCI_CAP_ID_MSI) {
238 /* Set enabled bits to single MSI & enable MSI_enable bit */
239 msi_enable(control, 1);
240 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800241 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 } else {
243 msix_enable(control);
244 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800245 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Jeff Garzik1769b462006-12-07 17:56:06 -0500247
248 pci_intx(dev, 0); /* disable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Kristen Accardi4602b882005-08-16 15:15:58 -0700251void disable_msi_mode(struct pci_dev *dev, int pos, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 u16 control;
254
255 pci_read_config_word(dev, msi_control_reg(pos), &control);
256 if (type == PCI_CAP_ID_MSI) {
257 /* Set enabled bits to single MSI & enable MSI_enable bit */
258 msi_disable(control);
259 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800260 dev->msi_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
262 msix_disable(control);
263 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800264 dev->msix_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Jeff Garzik1769b462006-12-07 17:56:06 -0500266
267 pci_intx(dev, 1); /* enable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Shaohua Li41017f02006-02-08 17:11:38 +0800270#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100271static int __pci_save_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800272{
273 int pos, i = 0;
274 u16 control;
275 struct pci_cap_saved_state *save_state;
276 u32 *cap;
277
278 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
279 if (pos <= 0 || dev->no_msi)
280 return 0;
281
282 pci_read_config_word(dev, msi_control_reg(pos), &control);
283 if (!(control & PCI_MSI_FLAGS_ENABLE))
284 return 0;
285
286 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
287 GFP_KERNEL);
288 if (!save_state) {
289 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
290 return -ENOMEM;
291 }
292 cap = &save_state->data[0];
293
294 pci_read_config_dword(dev, pos, &cap[i++]);
295 control = cap[0] >> 16;
296 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
297 if (control & PCI_MSI_FLAGS_64BIT) {
298 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
299 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
300 } else
301 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
302 if (control & PCI_MSI_FLAGS_MASKBIT)
303 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800304 save_state->cap_nr = PCI_CAP_ID_MSI;
305 pci_add_saved_cap(dev, save_state);
306 return 0;
307}
308
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100309static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800310{
311 int i = 0, pos;
312 u16 control;
313 struct pci_cap_saved_state *save_state;
314 u32 *cap;
315
316 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
317 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
318 if (!save_state || pos <= 0)
319 return;
320 cap = &save_state->data[0];
321
322 control = cap[i++] >> 16;
323 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
324 if (control & PCI_MSI_FLAGS_64BIT) {
325 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
326 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
327 } else
328 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
329 if (control & PCI_MSI_FLAGS_MASKBIT)
330 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
331 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
332 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
333 pci_remove_saved_cap(save_state);
334 kfree(save_state);
335}
336
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100337static int __pci_save_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800338{
339 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700340 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800341 u16 control;
342 struct pci_cap_saved_state *save_state;
343
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700344 if (!dev->msix_enabled)
345 return 0;
346
Shaohua Li41017f02006-02-08 17:11:38 +0800347 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
348 if (pos <= 0 || dev->no_msi)
349 return 0;
350
Mark Maulefd58e552006-04-10 21:17:48 -0500351 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800352 pci_read_config_word(dev, msi_control_reg(pos), &control);
353 if (!(control & PCI_MSIX_FLAGS_ENABLE))
354 return 0;
355 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
356 GFP_KERNEL);
357 if (!save_state) {
358 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
359 return -ENOMEM;
360 }
361 *((u16 *)&save_state->data[0]) = control;
362
Mark Maulefd58e552006-04-10 21:17:48 -0500363 /* save the table */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700364 irq = head = dev->first_msi_irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500365 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500366 struct msi_desc *entry;
367
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700368 entry = msi_desc[irq];
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700369 read_msi_msg(irq, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500370
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700371 tail = msi_desc[irq]->link.tail;
372 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500373 }
Mark Maulefd58e552006-04-10 21:17:48 -0500374
Shaohua Li41017f02006-02-08 17:11:38 +0800375 save_state->cap_nr = PCI_CAP_ID_MSIX;
376 pci_add_saved_cap(dev, save_state);
377 return 0;
378}
379
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100380int pci_save_msi_state(struct pci_dev *dev)
381{
382 int rc;
383
384 rc = __pci_save_msi_state(dev);
385 if (rc)
386 return rc;
387
388 rc = __pci_save_msix_state(dev);
389
390 return rc;
391}
392
393static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800394{
395 u16 save;
396 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700397 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800398 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800399 struct pci_cap_saved_state *save_state;
400
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700401 if (!dev->msix_enabled)
402 return;
403
Shaohua Li41017f02006-02-08 17:11:38 +0800404 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
405 if (!save_state)
406 return;
407 save = *((u16 *)&save_state->data[0]);
408 pci_remove_saved_cap(save_state);
409 kfree(save_state);
410
411 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
412 if (pos <= 0)
413 return;
414
415 /* route the table */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700416 irq = head = dev->first_msi_irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800417 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700418 entry = msi_desc[irq];
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700419 write_msi_msg(irq, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800420
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700421 tail = msi_desc[irq]->link.tail;
422 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800423 }
Shaohua Li41017f02006-02-08 17:11:38 +0800424
425 pci_write_config_word(dev, msi_control_reg(pos), save);
426 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
427}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100428
429void pci_restore_msi_state(struct pci_dev *dev)
430{
431 __pci_restore_msi_state(dev);
432 __pci_restore_msix_state(dev);
433}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900434#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/**
437 * msi_capability_init - configure device's MSI capability structure
438 * @dev: pointer to the pci_dev data structure of MSI device function
439 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600440 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700441 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700443 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 **/
445static int msi_capability_init(struct pci_dev *dev)
446{
Mark Maulefd58e552006-04-10 21:17:48 -0500447 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700449 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 u16 control;
451
452 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
453 pci_read_config_word(dev, msi_control_reg(pos), &control);
454 /* MSI Entry Initialization */
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700455 irq = create_msi_irq();
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700456 if (irq < 0)
457 return irq;
458
459 entry = get_irq_data(irq);
460 entry->link.head = irq;
461 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700463 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 entry->msi_attrib.entry_nr = 0;
465 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700466 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700467 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (is_mask_bit_support(control)) {
469 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
470 is_64bit_address(control));
471 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700472 entry->dev = dev;
473 if (entry->msi_attrib.maskbit) {
474 unsigned int maskbits, temp;
475 /* All MSIs are unmasked by default, Mask them all */
476 pci_read_config_dword(dev,
477 msi_mask_bits_reg(pos, is_64bit_address(control)),
478 &maskbits);
479 temp = (1 << multi_msi_capable(control));
480 temp = ((temp - 1) & ~temp);
481 maskbits |= temp;
482 pci_write_config_dword(dev,
483 msi_mask_bits_reg(pos, is_64bit_address(control)),
484 maskbits);
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Configure MSI capability structure */
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700487 status = arch_setup_msi_irq(irq, dev);
488 if (status < 0) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700489 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500490 return status;
491 }
Shaohua Li41017f02006-02-08 17:11:38 +0800492
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700493 dev->first_msi_irq = irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700494 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Set MSI enabled bits */
496 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
497
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700498 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500}
501
502/**
503 * msix_capability_init - configure device's MSI-X capability
504 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700505 * @entries: pointer to an array of struct msix_entry entries
506 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600508 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700509 * single MSI-X irq. A return of zero indicates the successful setup of
510 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 **/
512static int msix_capability_init(struct pci_dev *dev,
513 struct msix_entry *entries, int nvec)
514{
515 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Mark Maulefd58e552006-04-10 21:17:48 -0500516 int status;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700517 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800518 unsigned long phys_addr;
519 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 u16 control;
521 u8 bir;
522 void __iomem *base;
523
524 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
525 /* Request & Map MSI-X table region */
526 pci_read_config_word(dev, msi_control_reg(pos), &control);
527 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800528
529 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800531 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
532 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
534 if (base == NULL)
535 return -ENOMEM;
536
537 /* MSI-X Table Initialization */
538 for (i = 0; i < nvec; i++) {
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700539 irq = create_msi_irq();
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700540 if (irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700543 entry = get_irq_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 j = entries[i].entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700545 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700547 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 entry->msi_attrib.entry_nr = j;
549 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700550 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700551 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 entry->dev = dev;
553 entry->mask_base = base;
554 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700555 entry->link.head = irq;
556 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 head = entry;
558 } else {
559 entry->link.head = temp;
560 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700561 tail->link.tail = irq;
562 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700564 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 tail = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* Configure MSI-X capability structure */
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700567 status = arch_setup_msi_irq(irq, dev);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700568 if (status < 0) {
569 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500570 break;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700571 }
Mark Maulefd58e552006-04-10 21:17:48 -0500572
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700573 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700576 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 i--;
578 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700579 irq = (entries + i)->vector;
580 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 (entries + i)->vector = 0;
582 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700583 /* If we had some success report the number of irqs
584 * we succeeded in setting up.
585 */
586 if (avail <= 0)
587 avail = -EBUSY;
588 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700590 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* Set MSI-X enabled bits */
592 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
593
594 return 0;
595}
596
597/**
Brice Goglin24334a12006-08-31 01:55:07 -0400598 * pci_msi_supported - check whether MSI may be enabled on device
599 * @dev: pointer to the pci_dev data structure of MSI device function
600 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200601 * Look at global flags, the device itself, and its parent busses
602 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400603 **/
604static
605int pci_msi_supported(struct pci_dev * dev)
606{
607 struct pci_bus *bus;
608
Brice Goglin0306ebf2006-10-05 10:24:31 +0200609 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400610 if (!pci_msi_enable || !dev || dev->no_msi)
611 return -EINVAL;
612
Brice Goglin0306ebf2006-10-05 10:24:31 +0200613 /* Any bridge which does NOT route MSI transactions from it's
614 * secondary bus to it's primary bus must set NO_MSI flag on
615 * the secondary pci_bus.
616 * We expect only arch-specific PCI host bus controller driver
617 * or quirks for specific PCI bridges to be setting NO_MSI.
618 */
Brice Goglin24334a12006-08-31 01:55:07 -0400619 for (bus = dev->bus; bus; bus = bus->parent)
620 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
621 return -EINVAL;
622
623 return 0;
624}
625
626/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * pci_enable_msi - configure device's MSI capability structure
628 * @dev: pointer to the pci_dev data structure of MSI device function
629 *
630 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700631 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * MSI mode enabled on its hardware device function. A return of zero
633 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700634 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 **/
636int pci_enable_msi(struct pci_dev* dev)
637{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700638 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Brice Goglin24334a12006-08-31 01:55:07 -0400640 if (pci_msi_supported(dev) < 0)
641 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200642
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700643 status = msi_init();
644 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return status;
646
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700647 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
648 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
650
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700651 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700653 /* Check whether driver already requested for MSI-X irqs */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700654 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700655 if (pos > 0 && dev->msix_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700657 "Device already has MSI-X enabled\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -EINVAL;
660 }
661 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return status;
663}
664
665void pci_disable_msi(struct pci_dev* dev)
666{
667 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700668 int pos, default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700671 if (!pci_msi_enable)
672 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700673 if (!dev)
674 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700675
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700676 if (!dev->msi_enabled)
677 return;
678
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700679 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
680 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682
683 pci_read_config_word(dev, msi_control_reg(pos), &control);
684 if (!(control & PCI_MSI_FLAGS_ENABLE))
685 return;
686
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700687
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700688 disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
689
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700690 entry = msi_desc[dev->first_msi_irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return;
693 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700694 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700696 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700697 pci_name(dev), dev->first_msi_irq);
698 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700700 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700701 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700702
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700703 /* Restore dev->irq to its default pin-assertion irq */
704 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700706 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700709static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct msi_desc *entry;
712 int head, entry_nr, type;
713 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700715 arch_teardown_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500716
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700717 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -EINVAL;
720 }
721 type = entry->msi_attrib.type;
722 entry_nr = entry->msi_attrib.entry_nr;
723 head = entry->link.head;
724 base = entry->mask_base;
725 msi_desc[entry->link.head]->link.tail = entry->link.tail;
726 msi_desc[entry->link.tail]->link.head = entry->link.head;
727 entry->dev = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700728 msi_desc[irq] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700730 destroy_msi_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700733 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
734 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700736 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 return 0;
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/**
744 * pci_enable_msix - configure device's MSI-X capability structure
745 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700746 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700747 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 *
749 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700750 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * MSI-X mode enabled on its hardware device function. A return of zero
752 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700753 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700755 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * its request.
757 **/
758int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
759{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700760 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700761 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Brice Goglin24334a12006-08-31 01:55:07 -0400764 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -EINVAL;
766
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700767 status = msi_init();
768 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return status;
770
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700771 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
772 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EINVAL;
774
775 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 nr_entries = multi_msix_capable(control);
777 if (nvec > nr_entries)
778 return -EINVAL;
779
780 /* Check for any invalid entries */
781 for (i = 0; i < nvec; i++) {
782 if (entries[i].entry >= nr_entries)
783 return -EINVAL; /* invalid entry */
784 for (j = i + 1; j < nvec; j++) {
785 if (entries[i].entry == entries[j].entry)
786 return -EINVAL; /* duplicate entry */
787 }
788 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700789 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700790
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700791 /* Check whether driver already requested for MSI irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700793 dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700795 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EINVAL;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return status;
801}
802
803void pci_disable_msix(struct pci_dev* dev)
804{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700805 int irq, head, tail = 0, warning = 0;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700806 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 u16 control;
808
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700809 if (!pci_msi_enable)
810 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700811 if (!dev)
812 return;
813
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700814 if (!dev->msix_enabled)
815 return;
816
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700817 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
818 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
820
821 pci_read_config_word(dev, msi_control_reg(pos), &control);
822 if (!(control & PCI_MSIX_FLAGS_ENABLE))
823 return;
824
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700825 disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
826
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700827 irq = head = dev->first_msi_irq;
828 while (head != tail) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700829 tail = msi_desc[irq]->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700830 if (irq_has_action(irq))
831 warning = 1;
832 else if (irq != head) /* Release MSI-X irq */
833 msi_free_irq(dev, irq);
834 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700836 msi_free_irq(dev, irq);
837 if (warning) {
838 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
839 "free_irq() on all MSI-X irqs\n",
840 pci_name(dev));
841 BUG_ON(warning > 0);
842 }
843 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700847 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * @dev: pointer to the pci_dev data structure of MSI(X) device function
849 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600850 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700851 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * allocated for this device function, are reclaimed to unused state,
853 * which may be used later on.
854 **/
855void msi_remove_pci_irq_vectors(struct pci_dev* dev)
856{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!pci_msi_enable || !dev)
858 return;
859
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700860 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700861 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700863 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700864 pci_name(dev), dev->first_msi_irq);
865 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700866 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700867 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700869 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700870 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 void __iomem *base = NULL;
872
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700873 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700875 tail = msi_desc[irq]->link.tail;
876 base = msi_desc[irq]->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700877 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700879 else if (irq != head) /* Release MSI-X irq */
880 msi_free_irq(dev, irq);
881 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700883 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 iounmap(base);
886 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700887 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 pci_name(dev));
889 BUG_ON(warning > 0);
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892}
893
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700894void pci_no_msi(void)
895{
896 pci_msi_enable = 0;
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899EXPORT_SYMBOL(pci_enable_msi);
900EXPORT_SYMBOL(pci_disable_msi);
901EXPORT_SYMBOL(pci_enable_msix);
902EXPORT_SYMBOL(pci_disable_msix);