blob: 55fe83dfd77bd300fa28e43bab4f273c0e2be422 [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. Biederman3b7d1922006-10-04 02:16:59 -0700196static int create_msi_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700198 struct msi_desc *entry;
199 int irq;
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100200
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700201 entry = alloc_msi_entry();
202 if (!entry)
203 return -ENOMEM;
204
205 irq = create_irq();
206 if (irq < 0) {
207 kmem_cache_free(msi_cachep, entry);
208 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700210
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700211 set_irq_data(irq, entry);
212
213 return irq;
214}
215
216static void destroy_msi_irq(unsigned int irq)
217{
218 struct msi_desc *entry;
219
220 entry = get_irq_data(irq);
221 set_irq_chip(irq, NULL);
222 set_irq_data(irq, NULL);
223 destroy_irq(irq);
224 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
228{
229 u16 control;
230
231 pci_read_config_word(dev, msi_control_reg(pos), &control);
232 if (type == PCI_CAP_ID_MSI) {
233 /* Set enabled bits to single MSI & enable MSI_enable bit */
234 msi_enable(control, 1);
235 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800236 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 } else {
238 msix_enable(control);
239 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800240 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Jeff Garzik1769b462006-12-07 17:56:06 -0500242
243 pci_intx(dev, 0); /* disable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Kristen Accardi4602b882005-08-16 15:15:58 -0700246void disable_msi_mode(struct pci_dev *dev, int pos, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 u16 control;
249
250 pci_read_config_word(dev, msi_control_reg(pos), &control);
251 if (type == PCI_CAP_ID_MSI) {
252 /* Set enabled bits to single MSI & enable MSI_enable bit */
253 msi_disable(control);
254 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800255 dev->msi_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } else {
257 msix_disable(control);
258 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800259 dev->msix_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Jeff Garzik1769b462006-12-07 17:56:06 -0500261
262 pci_intx(dev, 1); /* enable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Shaohua Li41017f02006-02-08 17:11:38 +0800265#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100266static int __pci_save_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800267{
268 int pos, i = 0;
269 u16 control;
270 struct pci_cap_saved_state *save_state;
271 u32 *cap;
272
273 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
274 if (pos <= 0 || dev->no_msi)
275 return 0;
276
277 pci_read_config_word(dev, msi_control_reg(pos), &control);
278 if (!(control & PCI_MSI_FLAGS_ENABLE))
279 return 0;
280
281 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
282 GFP_KERNEL);
283 if (!save_state) {
284 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
285 return -ENOMEM;
286 }
287 cap = &save_state->data[0];
288
289 pci_read_config_dword(dev, pos, &cap[i++]);
290 control = cap[0] >> 16;
291 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
292 if (control & PCI_MSI_FLAGS_64BIT) {
293 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
294 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
295 } else
296 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
297 if (control & PCI_MSI_FLAGS_MASKBIT)
298 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800299 save_state->cap_nr = PCI_CAP_ID_MSI;
300 pci_add_saved_cap(dev, save_state);
301 return 0;
302}
303
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100304static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800305{
306 int i = 0, pos;
307 u16 control;
308 struct pci_cap_saved_state *save_state;
309 u32 *cap;
310
311 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
312 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
313 if (!save_state || pos <= 0)
314 return;
315 cap = &save_state->data[0];
316
317 control = cap[i++] >> 16;
318 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
319 if (control & PCI_MSI_FLAGS_64BIT) {
320 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
321 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
322 } else
323 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
324 if (control & PCI_MSI_FLAGS_MASKBIT)
325 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
326 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
327 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
328 pci_remove_saved_cap(save_state);
329 kfree(save_state);
330}
331
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100332static int __pci_save_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800333{
334 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700335 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800336 u16 control;
337 struct pci_cap_saved_state *save_state;
338
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700339 if (!dev->msix_enabled)
340 return 0;
341
Shaohua Li41017f02006-02-08 17:11:38 +0800342 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
343 if (pos <= 0 || dev->no_msi)
344 return 0;
345
Mark Maulefd58e552006-04-10 21:17:48 -0500346 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800347 pci_read_config_word(dev, msi_control_reg(pos), &control);
348 if (!(control & PCI_MSIX_FLAGS_ENABLE))
349 return 0;
350 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
351 GFP_KERNEL);
352 if (!save_state) {
353 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
354 return -ENOMEM;
355 }
356 *((u16 *)&save_state->data[0]) = control;
357
Mark Maulefd58e552006-04-10 21:17:48 -0500358 /* save the table */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700359 irq = head = dev->first_msi_irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500360 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500361 struct msi_desc *entry;
362
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700363 entry = msi_desc[irq];
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700364 read_msi_msg(irq, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500365
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700366 tail = msi_desc[irq]->link.tail;
367 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500368 }
Mark Maulefd58e552006-04-10 21:17:48 -0500369
Shaohua Li41017f02006-02-08 17:11:38 +0800370 save_state->cap_nr = PCI_CAP_ID_MSIX;
371 pci_add_saved_cap(dev, save_state);
372 return 0;
373}
374
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100375int pci_save_msi_state(struct pci_dev *dev)
376{
377 int rc;
378
379 rc = __pci_save_msi_state(dev);
380 if (rc)
381 return rc;
382
383 rc = __pci_save_msix_state(dev);
384
385 return rc;
386}
387
388static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800389{
390 u16 save;
391 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700392 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800393 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800394 struct pci_cap_saved_state *save_state;
395
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700396 if (!dev->msix_enabled)
397 return;
398
Shaohua Li41017f02006-02-08 17:11:38 +0800399 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
400 if (!save_state)
401 return;
402 save = *((u16 *)&save_state->data[0]);
403 pci_remove_saved_cap(save_state);
404 kfree(save_state);
405
406 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
407 if (pos <= 0)
408 return;
409
410 /* route the table */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700411 irq = head = dev->first_msi_irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800412 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700413 entry = msi_desc[irq];
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700414 write_msi_msg(irq, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800415
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700416 tail = msi_desc[irq]->link.tail;
417 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800418 }
Shaohua Li41017f02006-02-08 17:11:38 +0800419
420 pci_write_config_word(dev, msi_control_reg(pos), save);
421 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
422}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100423
424void pci_restore_msi_state(struct pci_dev *dev)
425{
426 __pci_restore_msi_state(dev);
427 __pci_restore_msix_state(dev);
428}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900429#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/**
432 * msi_capability_init - configure device's MSI capability structure
433 * @dev: pointer to the pci_dev data structure of MSI device function
434 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600435 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700436 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700438 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 **/
440static int msi_capability_init(struct pci_dev *dev)
441{
Mark Maulefd58e552006-04-10 21:17:48 -0500442 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700444 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 u16 control;
446
447 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
448 pci_read_config_word(dev, msi_control_reg(pos), &control);
449 /* MSI Entry Initialization */
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700450 irq = create_msi_irq();
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700451 if (irq < 0)
452 return irq;
453
454 entry = get_irq_data(irq);
455 entry->link.head = irq;
456 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700458 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 entry->msi_attrib.entry_nr = 0;
460 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700461 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700462 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (is_mask_bit_support(control)) {
464 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
465 is_64bit_address(control));
466 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700467 entry->dev = dev;
468 if (entry->msi_attrib.maskbit) {
469 unsigned int maskbits, temp;
470 /* All MSIs are unmasked by default, Mask them all */
471 pci_read_config_dword(dev,
472 msi_mask_bits_reg(pos, is_64bit_address(control)),
473 &maskbits);
474 temp = (1 << multi_msi_capable(control));
475 temp = ((temp - 1) & ~temp);
476 maskbits |= temp;
477 pci_write_config_dword(dev,
478 msi_mask_bits_reg(pos, is_64bit_address(control)),
479 maskbits);
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* Configure MSI capability structure */
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700482 status = arch_setup_msi_irq(irq, dev);
483 if (status < 0) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700484 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500485 return status;
486 }
Shaohua Li41017f02006-02-08 17:11:38 +0800487
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700488 dev->first_msi_irq = irq;
Eric W. Biederman1c659d62007-01-28 12:47:52 -0700489 msi_desc[irq] = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Set MSI enabled bits */
491 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
492
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700493 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
496
497/**
498 * msix_capability_init - configure device's MSI-X capability
499 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700500 * @entries: pointer to an array of struct msix_entry entries
501 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600503 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700504 * single MSI-X irq. A return of zero indicates the successful setup of
505 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 **/
507static int msix_capability_init(struct pci_dev *dev,
508 struct msix_entry *entries, int nvec)
509{
510 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Mark Maulefd58e552006-04-10 21:17:48 -0500511 int status;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700512 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800513 unsigned long phys_addr;
514 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 u16 control;
516 u8 bir;
517 void __iomem *base;
518
519 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
520 /* Request & Map MSI-X table region */
521 pci_read_config_word(dev, msi_control_reg(pos), &control);
522 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800523
524 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800526 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
527 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
529 if (base == NULL)
530 return -ENOMEM;
531
532 /* MSI-X Table Initialization */
533 for (i = 0; i < nvec; i++) {
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700534 irq = create_msi_irq();
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700535 if (irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700538 entry = get_irq_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 j = entries[i].entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700540 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700542 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 entry->msi_attrib.entry_nr = j;
544 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700545 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700546 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 entry->dev = dev;
548 entry->mask_base = base;
549 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700550 entry->link.head = irq;
551 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 head = entry;
553 } else {
554 entry->link.head = temp;
555 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700556 tail->link.tail = irq;
557 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700559 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 tail = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Configure MSI-X capability structure */
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700562 status = arch_setup_msi_irq(irq, dev);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700563 if (status < 0) {
564 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500565 break;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700566 }
Mark Maulefd58e552006-04-10 21:17:48 -0500567
Eric W. Biederman1c659d62007-01-28 12:47:52 -0700568 msi_desc[irq] = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700571 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 i--;
573 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700574 irq = (entries + i)->vector;
575 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 (entries + i)->vector = 0;
577 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700578 /* If we had some success report the number of irqs
579 * we succeeded in setting up.
580 */
581 if (avail <= 0)
582 avail = -EBUSY;
583 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700585 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Set MSI-X enabled bits */
587 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
588
589 return 0;
590}
591
592/**
Brice Goglin24334a12006-08-31 01:55:07 -0400593 * pci_msi_supported - check whether MSI may be enabled on device
594 * @dev: pointer to the pci_dev data structure of MSI device function
595 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200596 * Look at global flags, the device itself, and its parent busses
597 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400598 **/
599static
600int pci_msi_supported(struct pci_dev * dev)
601{
602 struct pci_bus *bus;
603
Brice Goglin0306ebf2006-10-05 10:24:31 +0200604 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400605 if (!pci_msi_enable || !dev || dev->no_msi)
606 return -EINVAL;
607
Brice Goglin0306ebf2006-10-05 10:24:31 +0200608 /* Any bridge which does NOT route MSI transactions from it's
609 * secondary bus to it's primary bus must set NO_MSI flag on
610 * the secondary pci_bus.
611 * We expect only arch-specific PCI host bus controller driver
612 * or quirks for specific PCI bridges to be setting NO_MSI.
613 */
Brice Goglin24334a12006-08-31 01:55:07 -0400614 for (bus = dev->bus; bus; bus = bus->parent)
615 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
616 return -EINVAL;
617
618 return 0;
619}
620
621/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * pci_enable_msi - configure device's MSI capability structure
623 * @dev: pointer to the pci_dev data structure of MSI device function
624 *
625 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700626 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * MSI mode enabled on its hardware device function. A return of zero
628 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700629 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 **/
631int pci_enable_msi(struct pci_dev* dev)
632{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700633 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Brice Goglin24334a12006-08-31 01:55:07 -0400635 if (pci_msi_supported(dev) < 0)
636 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200637
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700638 status = msi_init();
639 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return status;
641
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700642 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
643 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
645
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700646 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700648 /* Check whether driver already requested for MSI-X irqs */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700649 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700650 if (pos > 0 && dev->msix_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700652 "Device already has MSI-X enabled\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655 }
656 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return status;
658}
659
660void pci_disable_msi(struct pci_dev* dev)
661{
662 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700663 int pos, default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700666 if (!pci_msi_enable)
667 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700668 if (!dev)
669 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700670
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700671 if (!dev->msi_enabled)
672 return;
673
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700674 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
675 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
677
678 pci_read_config_word(dev, msi_control_reg(pos), &control);
679 if (!(control & PCI_MSI_FLAGS_ENABLE))
680 return;
681
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700682
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700683 disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
684
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700685 entry = msi_desc[dev->first_msi_irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return;
688 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700689 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700691 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700692 pci_name(dev), dev->first_msi_irq);
693 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700695 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700696 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700697
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700698 /* Restore dev->irq to its default pin-assertion irq */
699 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700701 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700704static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct msi_desc *entry;
707 int head, entry_nr, type;
708 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700710 arch_teardown_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500711
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700712 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -EINVAL;
715 }
716 type = entry->msi_attrib.type;
717 entry_nr = entry->msi_attrib.entry_nr;
718 head = entry->link.head;
719 base = entry->mask_base;
720 msi_desc[entry->link.head]->link.tail = entry->link.tail;
721 msi_desc[entry->link.tail]->link.head = entry->link.head;
722 entry->dev = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700723 msi_desc[irq] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700725 destroy_msi_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700728 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
729 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700731 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 return 0;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/**
739 * pci_enable_msix - configure device's MSI-X capability structure
740 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700741 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700742 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
744 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700745 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * MSI-X mode enabled on its hardware device function. A return of zero
747 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700748 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700750 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * its request.
752 **/
753int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
754{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700755 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700756 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Brice Goglin24334a12006-08-31 01:55:07 -0400759 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EINVAL;
761
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700762 status = msi_init();
763 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return status;
765
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700766 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
767 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EINVAL;
769
770 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 nr_entries = multi_msix_capable(control);
772 if (nvec > nr_entries)
773 return -EINVAL;
774
775 /* Check for any invalid entries */
776 for (i = 0; i < nvec; i++) {
777 if (entries[i].entry >= nr_entries)
778 return -EINVAL; /* invalid entry */
779 for (j = i + 1; j < nvec; j++) {
780 if (entries[i].entry == entries[j].entry)
781 return -EINVAL; /* duplicate entry */
782 }
783 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700784 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700785
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700786 /* Check whether driver already requested for MSI irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700788 dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700790 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -EINVAL;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return status;
796}
797
798void pci_disable_msix(struct pci_dev* dev)
799{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700800 int irq, head, tail = 0, warning = 0;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700801 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 u16 control;
803
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700804 if (!pci_msi_enable)
805 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700806 if (!dev)
807 return;
808
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700809 if (!dev->msix_enabled)
810 return;
811
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700812 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
813 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815
816 pci_read_config_word(dev, msi_control_reg(pos), &control);
817 if (!(control & PCI_MSIX_FLAGS_ENABLE))
818 return;
819
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700820 disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
821
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700822 irq = head = dev->first_msi_irq;
823 while (head != tail) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700824 tail = msi_desc[irq]->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700825 if (irq_has_action(irq))
826 warning = 1;
827 else if (irq != head) /* Release MSI-X irq */
828 msi_free_irq(dev, irq);
829 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700831 msi_free_irq(dev, irq);
832 if (warning) {
833 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
834 "free_irq() on all MSI-X irqs\n",
835 pci_name(dev));
836 BUG_ON(warning > 0);
837 }
838 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700842 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * @dev: pointer to the pci_dev data structure of MSI(X) device function
844 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600845 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700846 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * allocated for this device function, are reclaimed to unused state,
848 * which may be used later on.
849 **/
850void msi_remove_pci_irq_vectors(struct pci_dev* dev)
851{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!pci_msi_enable || !dev)
853 return;
854
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700855 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700856 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700858 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700859 pci_name(dev), dev->first_msi_irq);
860 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700861 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700862 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700864 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700865 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 void __iomem *base = NULL;
867
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700868 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700870 tail = msi_desc[irq]->link.tail;
871 base = msi_desc[irq]->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700872 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700874 else if (irq != head) /* Release MSI-X irq */
875 msi_free_irq(dev, irq);
876 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700878 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 iounmap(base);
881 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700882 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pci_name(dev));
884 BUG_ON(warning > 0);
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887}
888
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700889void pci_no_msi(void)
890{
891 pci_msi_enable = 0;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894EXPORT_SYMBOL(pci_enable_msi);
895EXPORT_SYMBOL(pci_disable_msi);
896EXPORT_SYMBOL(pci_enable_msix);
897EXPORT_SYMBOL(pci_disable_msix);