blob: fc7dd2a239dde61481dffd35b8ffea312655a543 [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>
18
19#include <asm/errno.h>
20#include <asm/io.h>
21#include <asm/smp.h>
22
23#include "pci.h"
24#include "msi.h"
25
26static DEFINE_SPINLOCK(msi_lock);
27static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL };
28static kmem_cache_t* msi_cachep;
29
30static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Mark Maulefd58e552006-04-10 21:17:48 -050032static struct msi_ops *msi_ops;
33
34int
35msi_register(struct msi_ops *ops)
36{
37 msi_ops = ops;
38 return 0;
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int msi_cache_init(void)
42{
Pekka J Enberg57181782006-09-27 01:51:03 -070043 msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
44 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!msi_cachep)
46 return -ENOMEM;
47
48 return 0;
49}
50
Eric W. Biederman1ce03372006-10-04 02:16:41 -070051static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct msi_desc *entry;
54
Eric W. Biederman1ce03372006-10-04 02:16:41 -070055 entry = msi_desc[irq];
Eric W. Biederman277bc332006-10-04 02:16:57 -070056 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 switch (entry->msi_attrib.type) {
58 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -070059 if (entry->msi_attrib.maskbit) {
60 int pos;
61 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Eric W. Biederman277bc332006-10-04 02:16:57 -070063 pos = (long)entry->mask_base;
64 pci_read_config_dword(entry->dev, pos, &mask_bits);
65 mask_bits &= ~(1);
66 mask_bits |= flag;
67 pci_write_config_dword(entry->dev, pos, mask_bits);
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 case PCI_CAP_ID_MSIX:
71 {
72 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
73 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
74 writel(flag, entry->mask_base + offset);
75 break;
76 }
77 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -070078 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 break;
80 }
81}
82
Eric W. Biederman0366f8f2006-10-04 02:16:33 -070083static void read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
84{
85 switch(entry->msi_attrib.type) {
86 case PCI_CAP_ID_MSI:
87 {
88 struct pci_dev *dev = entry->dev;
89 int pos = entry->msi_attrib.pos;
90 u16 data;
91
92 pci_read_config_dword(dev, msi_lower_address_reg(pos),
93 &msg->address_lo);
94 if (entry->msi_attrib.is_64) {
95 pci_read_config_dword(dev, msi_upper_address_reg(pos),
96 &msg->address_hi);
97 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
98 } else {
99 msg->address_hi = 0;
100 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
101 }
102 msg->data = data;
103 break;
104 }
105 case PCI_CAP_ID_MSIX:
106 {
107 void __iomem *base;
108 base = entry->mask_base +
109 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
110
111 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
112 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
113 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
114 break;
115 }
116 default:
117 BUG();
118 }
119}
120
121static void write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
122{
123 switch (entry->msi_attrib.type) {
124 case PCI_CAP_ID_MSI:
125 {
126 struct pci_dev *dev = entry->dev;
127 int pos = entry->msi_attrib.pos;
128
129 pci_write_config_dword(dev, msi_lower_address_reg(pos),
130 msg->address_lo);
131 if (entry->msi_attrib.is_64) {
132 pci_write_config_dword(dev, msi_upper_address_reg(pos),
133 msg->address_hi);
134 pci_write_config_word(dev, msi_data_reg(pos, 1),
135 msg->data);
136 } else {
137 pci_write_config_word(dev, msi_data_reg(pos, 0),
138 msg->data);
139 }
140 break;
141 }
142 case PCI_CAP_ID_MSIX:
143 {
144 void __iomem *base;
145 base = entry->mask_base +
146 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
147
148 writel(msg->address_lo,
149 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
150 writel(msg->address_hi,
151 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
152 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
153 break;
154 }
155 default:
156 BUG();
157 }
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#ifdef CONFIG_SMP
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700161static void set_msi_affinity(unsigned int irq, cpumask_t cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct msi_desc *entry;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700164 struct msi_msg msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700166 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!entry || !entry->dev)
168 return;
169
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700170 read_msi_msg(entry, &msg);
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700171 msi_ops->target(irq, cpu_mask, &msg);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700172 write_msi_msg(entry, &msg);
173 set_native_irq_info(irq, cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Grant Grundler8169b5d2006-01-03 18:51:46 -0800175#else
176#define set_msi_affinity NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#endif /* CONFIG_SMP */
178
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700179static void mask_MSI_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700181 msi_set_mask_bit(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700184static void unmask_MSI_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700186 msi_set_mask_bit(irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Eric W. Biederman277bc332006-10-04 02:16:57 -0700189static void ack_msi_irq(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700190{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700191 move_native_irq(irq);
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700192 ack_APIC_irq();
193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
Eric W. Biederman277bc332006-10-04 02:16:57 -0700196 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
197 * which implement the MSI or MSI-X Capability Structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Eric W. Biederman277bc332006-10-04 02:16:57 -0700199static struct irq_chip msi_chip = {
200 .name = "PCI-MSI",
201 .unmask = unmask_MSI_irq,
202 .mask = mask_MSI_irq,
203 .ack = ack_msi_irq,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800204 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700207static int msi_free_irq(struct pci_dev* dev, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static int msi_init(void)
209{
210 static int status = -ENOMEM;
211
212 if (!status)
213 return status;
214
215 if (pci_msi_quirk) {
216 pci_msi_enable = 0;
217 printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n");
218 status = -EINVAL;
219 return status;
220 }
221
Mark Maulefd58e552006-04-10 21:17:48 -0500222 status = msi_arch_init();
223 if (status < 0) {
224 pci_msi_enable = 0;
225 printk(KERN_WARNING
226 "PCI: MSI arch init failed. MSI disabled.\n");
227 return status;
228 }
229
230 if (! msi_ops) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700231 pci_msi_enable = 0;
Mark Maulefd58e552006-04-10 21:17:48 -0500232 printk(KERN_WARNING
233 "PCI: MSI ops not registered. MSI disabled.\n");
234 status = -EINVAL;
235 return status;
236 }
237
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700238 status = msi_cache_init();
239 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 pci_msi_enable = 0;
241 printk(KERN_WARNING "PCI: MSI cache init failed\n");
242 return status;
243 }
Mark Maulefd58e552006-04-10 21:17:48 -0500244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return status;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static struct msi_desc* alloc_msi_entry(void)
249{
250 struct msi_desc *entry;
251
Pekka J Enberg57181782006-09-27 01:51:03 -0700252 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!entry)
254 return NULL;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 entry->link.tail = entry->link.head = 0; /* single message */
257 entry->dev = NULL;
258
259 return entry;
260}
261
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700262static void attach_msi_entry(struct msi_desc *entry, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 unsigned long flags;
265
266 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700267 msi_desc[irq] = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_unlock_irqrestore(&msi_lock, flags);
269}
270
Eric W. Biederman277bc332006-10-04 02:16:57 -0700271static int create_msi_irq(struct irq_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700273 struct msi_desc *entry;
274 int irq;
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100275
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700276 entry = alloc_msi_entry();
277 if (!entry)
278 return -ENOMEM;
279
280 irq = create_irq();
281 if (irq < 0) {
282 kmem_cache_free(msi_cachep, entry);
283 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700285
Eric W. Biederman277bc332006-10-04 02:16:57 -0700286 set_irq_chip_and_handler(irq, chip, handle_edge_irq);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700287 set_irq_data(irq, entry);
288
289 return irq;
290}
291
292static void destroy_msi_irq(unsigned int irq)
293{
294 struct msi_desc *entry;
295
296 entry = get_irq_data(irq);
297 set_irq_chip(irq, NULL);
298 set_irq_data(irq, NULL);
299 destroy_irq(irq);
300 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
304{
305 u16 control;
306
307 pci_read_config_word(dev, msi_control_reg(pos), &control);
308 if (type == PCI_CAP_ID_MSI) {
309 /* Set enabled bits to single MSI & enable MSI_enable bit */
310 msi_enable(control, 1);
311 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800312 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 } else {
314 msix_enable(control);
315 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800316 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
319 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400320 pci_intx(dev, 0); /* disable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322}
323
Kristen Accardi4602b882005-08-16 15:15:58 -0700324void disable_msi_mode(struct pci_dev *dev, int pos, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 u16 control;
327
328 pci_read_config_word(dev, msi_control_reg(pos), &control);
329 if (type == PCI_CAP_ID_MSI) {
330 /* Set enabled bits to single MSI & enable MSI_enable bit */
331 msi_disable(control);
332 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800333 dev->msi_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else {
335 msix_disable(control);
336 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800337 dev->msix_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
340 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400341 pci_intx(dev, 1); /* enable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343}
344
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700345static int msi_lookup_irq(struct pci_dev *dev, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700347 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long flags;
349
350 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700351 for (irq = 0; irq < NR_IRQS; irq++) {
352 if (!msi_desc[irq] || msi_desc[irq]->dev != dev ||
353 msi_desc[irq]->msi_attrib.type != type ||
354 msi_desc[irq]->msi_attrib.default_irq != dev->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 continue;
356 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700357 /* This pre-assigned MSI irq for this device
358 already exits. Override dev->irq with this irq */
359 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 }
362 spin_unlock_irqrestore(&msi_lock, flags);
363
364 return -EACCES;
365}
366
367void pci_scan_msi_device(struct pci_dev *dev)
368{
369 if (!dev)
370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Shaohua Li41017f02006-02-08 17:11:38 +0800373#ifdef CONFIG_PM
374int pci_save_msi_state(struct pci_dev *dev)
375{
376 int pos, i = 0;
377 u16 control;
378 struct pci_cap_saved_state *save_state;
379 u32 *cap;
380
381 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
382 if (pos <= 0 || dev->no_msi)
383 return 0;
384
385 pci_read_config_word(dev, msi_control_reg(pos), &control);
386 if (!(control & PCI_MSI_FLAGS_ENABLE))
387 return 0;
388
389 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
390 GFP_KERNEL);
391 if (!save_state) {
392 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
393 return -ENOMEM;
394 }
395 cap = &save_state->data[0];
396
397 pci_read_config_dword(dev, pos, &cap[i++]);
398 control = cap[0] >> 16;
399 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
400 if (control & PCI_MSI_FLAGS_64BIT) {
401 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
402 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
403 } else
404 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
405 if (control & PCI_MSI_FLAGS_MASKBIT)
406 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800407 save_state->cap_nr = PCI_CAP_ID_MSI;
408 pci_add_saved_cap(dev, save_state);
409 return 0;
410}
411
412void pci_restore_msi_state(struct pci_dev *dev)
413{
414 int i = 0, pos;
415 u16 control;
416 struct pci_cap_saved_state *save_state;
417 u32 *cap;
418
419 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
420 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
421 if (!save_state || pos <= 0)
422 return;
423 cap = &save_state->data[0];
424
425 control = cap[i++] >> 16;
426 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
427 if (control & PCI_MSI_FLAGS_64BIT) {
428 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
429 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
430 } else
431 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
432 if (control & PCI_MSI_FLAGS_MASKBIT)
433 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
434 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
435 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
436 pci_remove_saved_cap(save_state);
437 kfree(save_state);
438}
439
440int pci_save_msix_state(struct pci_dev *dev)
441{
442 int pos;
Mark Maulefd58e552006-04-10 21:17:48 -0500443 int temp;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700444 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800445 u16 control;
446 struct pci_cap_saved_state *save_state;
447
448 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
449 if (pos <= 0 || dev->no_msi)
450 return 0;
451
Mark Maulefd58e552006-04-10 21:17:48 -0500452 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800453 pci_read_config_word(dev, msi_control_reg(pos), &control);
454 if (!(control & PCI_MSIX_FLAGS_ENABLE))
455 return 0;
456 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
457 GFP_KERNEL);
458 if (!save_state) {
459 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
460 return -ENOMEM;
461 }
462 *((u16 *)&save_state->data[0]) = control;
463
Mark Maulefd58e552006-04-10 21:17:48 -0500464 /* save the table */
465 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700466 if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Mark Maulefd58e552006-04-10 21:17:48 -0500467 kfree(save_state);
468 return -EINVAL;
469 }
470
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700471 irq = head = dev->irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500472 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500473 struct msi_desc *entry;
474
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700475 entry = msi_desc[irq];
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700476 read_msi_msg(entry, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500477
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700478 tail = msi_desc[irq]->link.tail;
479 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500480 }
481 dev->irq = temp;
482
Shaohua Li41017f02006-02-08 17:11:38 +0800483 save_state->cap_nr = PCI_CAP_ID_MSIX;
484 pci_add_saved_cap(dev, save_state);
485 return 0;
486}
487
488void pci_restore_msix_state(struct pci_dev *dev)
489{
490 u16 save;
491 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700492 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800493 struct msi_desc *entry;
494 int temp;
495 struct pci_cap_saved_state *save_state;
496
497 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
498 if (!save_state)
499 return;
500 save = *((u16 *)&save_state->data[0]);
501 pci_remove_saved_cap(save_state);
502 kfree(save_state);
503
504 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
505 if (pos <= 0)
506 return;
507
508 /* route the table */
509 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700510 if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX))
Shaohua Li41017f02006-02-08 17:11:38 +0800511 return;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700512 irq = head = dev->irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800513 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700514 entry = msi_desc[irq];
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700515 write_msi_msg(entry, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800516
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700517 tail = msi_desc[irq]->link.tail;
518 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800519 }
520 dev->irq = temp;
521
522 pci_write_config_word(dev, msi_control_reg(pos), save);
523 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
524}
525#endif
526
Mark Maulefd58e552006-04-10 21:17:48 -0500527static int msi_register_init(struct pci_dev *dev, struct msi_desc *entry)
Shaohua Li41017f02006-02-08 17:11:38 +0800528{
Mark Maulefd58e552006-04-10 21:17:48 -0500529 int status;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700530 struct msi_msg msg;
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700531 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800532 u16 control;
533
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700534 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800535 pci_read_config_word(dev, msi_control_reg(pos), &control);
Mark Maulefd58e552006-04-10 21:17:48 -0500536
Shaohua Li41017f02006-02-08 17:11:38 +0800537 /* Configure MSI capability structure */
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700538 status = msi_ops->setup(dev, dev->irq, &msg);
Mark Maulefd58e552006-04-10 21:17:48 -0500539 if (status < 0)
540 return status;
541
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700542 write_msi_msg(entry, &msg);
Shaohua Li41017f02006-02-08 17:11:38 +0800543 if (entry->msi_attrib.maskbit) {
544 unsigned int maskbits, temp;
545 /* All MSIs are unmasked by default, Mask them all */
546 pci_read_config_dword(dev,
547 msi_mask_bits_reg(pos, is_64bit_address(control)),
548 &maskbits);
549 temp = (1 << multi_msi_capable(control));
550 temp = ((temp - 1) & ~temp);
551 maskbits |= temp;
552 pci_write_config_dword(dev,
553 msi_mask_bits_reg(pos, is_64bit_address(control)),
554 maskbits);
555 }
Mark Maulefd58e552006-04-10 21:17:48 -0500556
557 return 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/**
561 * msi_capability_init - configure device's MSI capability structure
562 * @dev: pointer to the pci_dev data structure of MSI device function
563 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600564 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700565 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700567 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 **/
569static int msi_capability_init(struct pci_dev *dev)
570{
Mark Maulefd58e552006-04-10 21:17:48 -0500571 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700573 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 u16 control;
575
576 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
577 pci_read_config_word(dev, msi_control_reg(pos), &control);
578 /* MSI Entry Initialization */
Eric W. Biederman277bc332006-10-04 02:16:57 -0700579 irq = create_msi_irq(&msi_chip);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700580 if (irq < 0)
581 return irq;
582
583 entry = get_irq_data(irq);
584 entry->link.head = irq;
585 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700587 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 entry->msi_attrib.entry_nr = 0;
589 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700590 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700591 entry->msi_attrib.pos = pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700592 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 entry->dev = dev;
594 if (is_mask_bit_support(control)) {
595 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
596 is_64bit_address(control));
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* Configure MSI capability structure */
Mark Maulefd58e552006-04-10 21:17:48 -0500599 status = msi_register_init(dev, entry);
600 if (status != 0) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700601 dev->irq = entry->msi_attrib.default_irq;
602 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500603 return status;
604 }
Shaohua Li41017f02006-02-08 17:11:38 +0800605
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700606 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Set MSI enabled bits */
608 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
609
610 return 0;
611}
612
613/**
614 * msix_capability_init - configure device's MSI-X capability
615 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700616 * @entries: pointer to an array of struct msix_entry entries
617 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600619 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700620 * single MSI-X irq. A return of zero indicates the successful setup of
621 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 **/
623static int msix_capability_init(struct pci_dev *dev,
624 struct msix_entry *entries, int nvec)
625{
626 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700627 struct msi_msg msg;
Mark Maulefd58e552006-04-10 21:17:48 -0500628 int status;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700629 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800630 unsigned long phys_addr;
631 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 u16 control;
633 u8 bir;
634 void __iomem *base;
635
636 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
637 /* Request & Map MSI-X table region */
638 pci_read_config_word(dev, msi_control_reg(pos), &control);
639 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800640
641 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800643 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
644 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
646 if (base == NULL)
647 return -ENOMEM;
648
649 /* MSI-X Table Initialization */
650 for (i = 0; i < nvec; i++) {
Eric W. Biederman277bc332006-10-04 02:16:57 -0700651 irq = create_msi_irq(&msi_chip);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700652 if (irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700655 entry = get_irq_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 j = entries[i].entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700657 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700659 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 entry->msi_attrib.entry_nr = j;
661 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700662 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700663 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 entry->dev = dev;
665 entry->mask_base = base;
666 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700667 entry->link.head = irq;
668 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 head = entry;
670 } else {
671 entry->link.head = temp;
672 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700673 tail->link.tail = irq;
674 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700676 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 tail = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* Configure MSI-X capability structure */
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700679 status = msi_ops->setup(dev, irq, &msg);
680 if (status < 0) {
681 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500682 break;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700683 }
Mark Maulefd58e552006-04-10 21:17:48 -0500684
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700685 write_msi_msg(entry, &msg);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700686 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700689 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 i--;
691 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700692 irq = (entries + i)->vector;
693 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 (entries + i)->vector = 0;
695 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700696 /* If we had some success report the number of irqs
697 * we succeeded in setting up.
698 */
699 if (avail <= 0)
700 avail = -EBUSY;
701 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 /* Set MSI-X enabled bits */
704 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
705
706 return 0;
707}
708
709/**
Brice Goglin24334a12006-08-31 01:55:07 -0400710 * pci_msi_supported - check whether MSI may be enabled on device
711 * @dev: pointer to the pci_dev data structure of MSI device function
712 *
713 * MSI must be globally enabled and supported by the device and its root
714 * bus. But, the root bus is not easy to find since some architectures
715 * have virtual busses on top of the PCI hierarchy (for instance the
716 * hypertransport bus), while the actual bus where MSI must be supported
717 * is below. So we test the MSI flag on all parent busses and assume
718 * that no quirk will ever set the NO_MSI flag on a non-root bus.
719 **/
720static
721int pci_msi_supported(struct pci_dev * dev)
722{
723 struct pci_bus *bus;
724
725 if (!pci_msi_enable || !dev || dev->no_msi)
726 return -EINVAL;
727
728 /* check MSI flags of all parent busses */
729 for (bus = dev->bus; bus; bus = bus->parent)
730 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
731 return -EINVAL;
732
733 return 0;
734}
735
736/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * pci_enable_msi - configure device's MSI capability structure
738 * @dev: pointer to the pci_dev data structure of MSI device function
739 *
740 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700741 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * MSI mode enabled on its hardware device function. A return of zero
743 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700744 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 **/
746int pci_enable_msi(struct pci_dev* dev)
747{
Brice Goglin24334a12006-08-31 01:55:07 -0400748 int pos, temp, status;
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700749 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Brice Goglin24334a12006-08-31 01:55:07 -0400751 if (pci_msi_supported(dev) < 0)
752 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 temp = dev->irq;
755
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700756 status = msi_init();
757 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return status;
759
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700760 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
761 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EINVAL;
763
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700764 pci_read_config_word(dev, msi_control_reg(pos), &control);
765 if (!is_64bit_address(control) && msi_ops->needs_64bit_address)
766 return -EINVAL;
767
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700768 WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700770 /* Check whether driver already requested for MSI-X irqs */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700771 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700772 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700774 "Device already has MSI-X irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 pci_name(dev));
776 dev->irq = temp;
777 return -EINVAL;
778 }
779 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return status;
781}
782
783void pci_disable_msi(struct pci_dev* dev)
784{
785 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700786 int pos, default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 u16 control;
788 unsigned long flags;
789
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700790 if (!pci_msi_enable)
791 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700792 if (!dev)
793 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700794
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700795 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
796 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return;
798
799 pci_read_config_word(dev, msi_control_reg(pos), &control);
800 if (!(control & PCI_MSI_FLAGS_ENABLE))
801 return;
802
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700803 disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 spin_lock_irqsave(&msi_lock, flags);
806 entry = msi_desc[dev->irq];
807 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
808 spin_unlock_irqrestore(&msi_lock, flags);
809 return;
810 }
Eric W. Biederman1f800252006-10-04 02:16:56 -0700811 if (irq_has_action(dev->irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 spin_unlock_irqrestore(&msi_lock, flags);
813 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700814 "free_irq() on MSI irq %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 pci_name(dev), dev->irq);
Eric W. Biederman1f800252006-10-04 02:16:56 -0700816 BUG_ON(irq_has_action(dev->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700818 default_irq = entry->msi_attrib.default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700820 msi_free_irq(dev, dev->irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700821
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700822 /* Restore dev->irq to its default pin-assertion irq */
823 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700827static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct msi_desc *entry;
830 int head, entry_nr, type;
831 void __iomem *base;
832 unsigned long flags;
833
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700834 msi_ops->teardown(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700837 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!entry || entry->dev != dev) {
839 spin_unlock_irqrestore(&msi_lock, flags);
840 return -EINVAL;
841 }
842 type = entry->msi_attrib.type;
843 entry_nr = entry->msi_attrib.entry_nr;
844 head = entry->link.head;
845 base = entry->mask_base;
846 msi_desc[entry->link.head]->link.tail = entry->link.tail;
847 msi_desc[entry->link.tail]->link.head = entry->link.head;
848 entry->dev = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700849 msi_desc[irq] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 spin_unlock_irqrestore(&msi_lock, flags);
851
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700852 destroy_msi_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700855 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
856 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700858 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
862 return 0;
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/**
866 * pci_enable_msix - configure device's MSI-X capability structure
867 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700868 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700869 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
871 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700872 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * MSI-X mode enabled on its hardware device function. A return of zero
874 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700875 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700877 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * its request.
879 **/
880int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
881{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700882 int status, pos, nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int i, j, temp;
884 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Brice Goglin24334a12006-08-31 01:55:07 -0400886 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EINVAL;
888
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700889 status = msi_init();
890 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return status;
892
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700893 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
894 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EINVAL;
896
897 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 nr_entries = multi_msix_capable(control);
899 if (nvec > nr_entries)
900 return -EINVAL;
901
902 /* Check for any invalid entries */
903 for (i = 0; i < nvec; i++) {
904 if (entries[i].entry >= nr_entries)
905 return -EINVAL; /* invalid entry */
906 for (j = i + 1; j < nvec; j++) {
907 if (entries[i].entry == entries[j].entry)
908 return -EINVAL; /* duplicate entry */
909 }
910 }
911 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700912 WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSIX));
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700913
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700914 /* Check whether driver already requested for MSI irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700916 !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700918 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 pci_name(dev));
920 dev->irq = temp;
921 return -EINVAL;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return status;
925}
926
927void pci_disable_msix(struct pci_dev* dev)
928{
929 int pos, temp;
930 u16 control;
931
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700932 if (!pci_msi_enable)
933 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700934 if (!dev)
935 return;
936
937 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
938 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return;
940
941 pci_read_config_word(dev, msi_control_reg(pos), &control);
942 if (!(control & PCI_MSIX_FLAGS_ENABLE))
943 return;
944
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700945 disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700948 if (!msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Eric W. Biederman1f800252006-10-04 02:16:56 -0700949 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 unsigned long flags;
951
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700952 irq = head = dev->irq;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700953 dev->irq = temp; /* Restore pin IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 while (head != tail) {
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700955 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700956 tail = msi_desc[irq]->link.tail;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700957 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1f800252006-10-04 02:16:56 -0700958 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700960 else if (irq != head) /* Release MSI-X irq */
961 msi_free_irq(dev, irq);
962 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700964 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700967 "free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 pci_name(dev));
969 BUG_ON(warning > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 }
972}
973
974/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700975 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * @dev: pointer to the pci_dev data structure of MSI(X) device function
977 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600978 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700979 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * allocated for this device function, are reclaimed to unused state,
981 * which may be used later on.
982 **/
983void msi_remove_pci_irq_vectors(struct pci_dev* dev)
984{
Eric W. Biederman1f800252006-10-04 02:16:56 -0700985 int pos, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 unsigned long flags;
987
988 if (!pci_msi_enable || !dev)
989 return;
990
991 temp = dev->irq; /* Save IOAPIC IRQ */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700992 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700993 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
Eric W. Biederman1f800252006-10-04 02:16:56 -0700994 if (irq_has_action(dev->irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700996 "called without free_irq() on MSI irq %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 pci_name(dev), dev->irq);
Eric W. Biederman1f800252006-10-04 02:16:56 -0700998 BUG_ON(irq_has_action(dev->irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700999 } else /* Release MSI irq assigned to this device */
1000 msi_free_irq(dev, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dev->irq = temp; /* Restore IOAPIC IRQ */
1002 }
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001003 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001004 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
1005 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 void __iomem *base = NULL;
1007
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001008 irq = head = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 while (head != tail) {
1010 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001011 tail = msi_desc[irq]->link.tail;
1012 base = msi_desc[irq]->mask_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1f800252006-10-04 02:16:56 -07001014 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001016 else if (irq != head) /* Release MSI-X irq */
1017 msi_free_irq(dev, irq);
1018 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001020 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 iounmap(base);
1023 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001024 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 pci_name(dev));
1026 BUG_ON(warning > 0);
1027 }
1028 dev->irq = temp; /* Restore IOAPIC IRQ */
1029 }
1030}
1031
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001032void pci_no_msi(void)
1033{
1034 pci_msi_enable = 0;
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037EXPORT_SYMBOL(pci_enable_msi);
1038EXPORT_SYMBOL(pci_disable_msi);
1039EXPORT_SYMBOL(pci_enable_msix);
1040EXPORT_SYMBOL(pci_disable_msix);