blob: e3ba3963988c227a0859e8a728fe322975e44e79 [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];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (!entry || !entry->dev || !entry->mask_base)
57 return;
58 switch (entry->msi_attrib.type) {
59 case PCI_CAP_ID_MSI:
60 {
61 int pos;
62 u32 mask_bits;
63
64 pos = (long)entry->mask_base;
65 pci_read_config_dword(entry->dev, pos, &mask_bits);
66 mask_bits &= ~(1);
67 mask_bits |= flag;
68 pci_write_config_dword(entry->dev, pos, mask_bits);
69 break;
70 }
71 case PCI_CAP_ID_MSIX:
72 {
73 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
74 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
75 writel(flag, entry->mask_base + offset);
76 break;
77 }
78 default:
79 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. Biederman1ce03372006-10-04 02:16:41 -0700189static unsigned int startup_msi_irq_wo_maskbit(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0; /* never anything pending */
192}
193
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700194static unsigned int startup_msi_irq_w_maskbit(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700196 startup_msi_irq_wo_maskbit(irq);
197 unmask_MSI_irq(irq);
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700198 return 0; /* never anything pending */
199}
200
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700201static void shutdown_msi_irq(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700205static void end_msi_irq_wo_maskbit(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700206{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700207 move_native_irq(irq);
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700208 ack_APIC_irq();
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700211static void end_msi_irq_w_maskbit(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700213 move_native_irq(irq);
214 unmask_MSI_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ack_APIC_irq();
216}
217
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700218static void do_nothing(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700219{
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
224 * which implement the MSI-X Capability Structure.
225 */
226static struct hw_interrupt_type msix_irq_type = {
227 .typename = "PCI-MSI-X",
228 .startup = startup_msi_irq_w_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700229 .shutdown = shutdown_msi_irq,
230 .enable = unmask_MSI_irq,
231 .disable = mask_MSI_irq,
232 .ack = mask_MSI_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .end = end_msi_irq_w_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800234 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237/*
238 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
239 * which implement the MSI Capability Structure with
240 * Mask-and-Pending Bits.
241 */
242static struct hw_interrupt_type msi_irq_w_maskbit_type = {
243 .typename = "PCI-MSI",
244 .startup = startup_msi_irq_w_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700245 .shutdown = shutdown_msi_irq,
246 .enable = unmask_MSI_irq,
247 .disable = mask_MSI_irq,
248 .ack = mask_MSI_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .end = end_msi_irq_w_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800250 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253/*
254 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
255 * which implement the MSI Capability Structure without
256 * Mask-and-Pending Bits.
257 */
258static struct hw_interrupt_type msi_irq_wo_maskbit_type = {
259 .typename = "PCI-MSI",
260 .startup = startup_msi_irq_wo_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700261 .shutdown = shutdown_msi_irq,
262 .enable = do_nothing,
263 .disable = do_nothing,
264 .ack = do_nothing,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .end = end_msi_irq_wo_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800266 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};
268
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700269static int msi_free_irq(struct pci_dev* dev, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static int msi_init(void)
271{
272 static int status = -ENOMEM;
273
274 if (!status)
275 return status;
276
277 if (pci_msi_quirk) {
278 pci_msi_enable = 0;
279 printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n");
280 status = -EINVAL;
281 return status;
282 }
283
Mark Maulefd58e552006-04-10 21:17:48 -0500284 status = msi_arch_init();
285 if (status < 0) {
286 pci_msi_enable = 0;
287 printk(KERN_WARNING
288 "PCI: MSI arch init failed. MSI disabled.\n");
289 return status;
290 }
291
292 if (! msi_ops) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700293 pci_msi_enable = 0;
Mark Maulefd58e552006-04-10 21:17:48 -0500294 printk(KERN_WARNING
295 "PCI: MSI ops not registered. MSI disabled.\n");
296 status = -EINVAL;
297 return status;
298 }
299
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700300 status = msi_cache_init();
301 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 pci_msi_enable = 0;
303 printk(KERN_WARNING "PCI: MSI cache init failed\n");
304 return status;
305 }
Mark Maulefd58e552006-04-10 21:17:48 -0500306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return status;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static struct msi_desc* alloc_msi_entry(void)
311{
312 struct msi_desc *entry;
313
Pekka J Enberg57181782006-09-27 01:51:03 -0700314 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!entry)
316 return NULL;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 entry->link.tail = entry->link.head = 0; /* single message */
319 entry->dev = NULL;
320
321 return entry;
322}
323
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700324static void attach_msi_entry(struct msi_desc *entry, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 unsigned long flags;
327
328 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700329 msi_desc[irq] = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 spin_unlock_irqrestore(&msi_lock, flags);
331}
332
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700333static int create_msi_irq(struct hw_interrupt_type *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700335 struct msi_desc *entry;
336 int irq;
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100337
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700338 entry = alloc_msi_entry();
339 if (!entry)
340 return -ENOMEM;
341
342 irq = create_irq();
343 if (irq < 0) {
344 kmem_cache_free(msi_cachep, entry);
345 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700347
348 set_irq_chip(irq, handler);
349 set_irq_data(irq, entry);
350
351 return irq;
352}
353
354static void destroy_msi_irq(unsigned int irq)
355{
356 struct msi_desc *entry;
357
358 entry = get_irq_data(irq);
359 set_irq_chip(irq, NULL);
360 set_irq_data(irq, NULL);
361 destroy_irq(irq);
362 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
366{
367 u16 control;
368
369 pci_read_config_word(dev, msi_control_reg(pos), &control);
370 if (type == PCI_CAP_ID_MSI) {
371 /* Set enabled bits to single MSI & enable MSI_enable bit */
372 msi_enable(control, 1);
373 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800374 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else {
376 msix_enable(control);
377 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800378 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
381 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400382 pci_intx(dev, 0); /* disable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384}
385
Kristen Accardi4602b882005-08-16 15:15:58 -0700386void disable_msi_mode(struct pci_dev *dev, int pos, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 u16 control;
389
390 pci_read_config_word(dev, msi_control_reg(pos), &control);
391 if (type == PCI_CAP_ID_MSI) {
392 /* Set enabled bits to single MSI & enable MSI_enable bit */
393 msi_disable(control);
394 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800395 dev->msi_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
397 msix_disable(control);
398 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800399 dev->msix_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
402 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400403 pci_intx(dev, 1); /* enable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405}
406
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700407static int msi_lookup_irq(struct pci_dev *dev, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700409 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 unsigned long flags;
411
412 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700413 for (irq = 0; irq < NR_IRQS; irq++) {
414 if (!msi_desc[irq] || msi_desc[irq]->dev != dev ||
415 msi_desc[irq]->msi_attrib.type != type ||
416 msi_desc[irq]->msi_attrib.default_irq != dev->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 continue;
418 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700419 /* This pre-assigned MSI irq for this device
420 already exits. Override dev->irq with this irq */
421 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423 }
424 spin_unlock_irqrestore(&msi_lock, flags);
425
426 return -EACCES;
427}
428
429void pci_scan_msi_device(struct pci_dev *dev)
430{
431 if (!dev)
432 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Shaohua Li41017f02006-02-08 17:11:38 +0800435#ifdef CONFIG_PM
436int pci_save_msi_state(struct pci_dev *dev)
437{
438 int pos, i = 0;
439 u16 control;
440 struct pci_cap_saved_state *save_state;
441 u32 *cap;
442
443 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
444 if (pos <= 0 || dev->no_msi)
445 return 0;
446
447 pci_read_config_word(dev, msi_control_reg(pos), &control);
448 if (!(control & PCI_MSI_FLAGS_ENABLE))
449 return 0;
450
451 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
452 GFP_KERNEL);
453 if (!save_state) {
454 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
455 return -ENOMEM;
456 }
457 cap = &save_state->data[0];
458
459 pci_read_config_dword(dev, pos, &cap[i++]);
460 control = cap[0] >> 16;
461 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
462 if (control & PCI_MSI_FLAGS_64BIT) {
463 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
464 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
465 } else
466 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
467 if (control & PCI_MSI_FLAGS_MASKBIT)
468 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800469 save_state->cap_nr = PCI_CAP_ID_MSI;
470 pci_add_saved_cap(dev, save_state);
471 return 0;
472}
473
474void pci_restore_msi_state(struct pci_dev *dev)
475{
476 int i = 0, pos;
477 u16 control;
478 struct pci_cap_saved_state *save_state;
479 u32 *cap;
480
481 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
482 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
483 if (!save_state || pos <= 0)
484 return;
485 cap = &save_state->data[0];
486
487 control = cap[i++] >> 16;
488 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
489 if (control & PCI_MSI_FLAGS_64BIT) {
490 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
491 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
492 } else
493 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
494 if (control & PCI_MSI_FLAGS_MASKBIT)
495 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
496 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
497 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
498 pci_remove_saved_cap(save_state);
499 kfree(save_state);
500}
501
502int pci_save_msix_state(struct pci_dev *dev)
503{
504 int pos;
Mark Maulefd58e552006-04-10 21:17:48 -0500505 int temp;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700506 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800507 u16 control;
508 struct pci_cap_saved_state *save_state;
509
510 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
511 if (pos <= 0 || dev->no_msi)
512 return 0;
513
Mark Maulefd58e552006-04-10 21:17:48 -0500514 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800515 pci_read_config_word(dev, msi_control_reg(pos), &control);
516 if (!(control & PCI_MSIX_FLAGS_ENABLE))
517 return 0;
518 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
519 GFP_KERNEL);
520 if (!save_state) {
521 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
522 return -ENOMEM;
523 }
524 *((u16 *)&save_state->data[0]) = control;
525
Mark Maulefd58e552006-04-10 21:17:48 -0500526 /* save the table */
527 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700528 if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Mark Maulefd58e552006-04-10 21:17:48 -0500529 kfree(save_state);
530 return -EINVAL;
531 }
532
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700533 irq = head = dev->irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500534 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500535 struct msi_desc *entry;
536
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700537 entry = msi_desc[irq];
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700538 read_msi_msg(entry, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500539
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700540 tail = msi_desc[irq]->link.tail;
541 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500542 }
543 dev->irq = temp;
544
Shaohua Li41017f02006-02-08 17:11:38 +0800545 save_state->cap_nr = PCI_CAP_ID_MSIX;
546 pci_add_saved_cap(dev, save_state);
547 return 0;
548}
549
550void pci_restore_msix_state(struct pci_dev *dev)
551{
552 u16 save;
553 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700554 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800555 struct msi_desc *entry;
556 int temp;
557 struct pci_cap_saved_state *save_state;
558
559 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
560 if (!save_state)
561 return;
562 save = *((u16 *)&save_state->data[0]);
563 pci_remove_saved_cap(save_state);
564 kfree(save_state);
565
566 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
567 if (pos <= 0)
568 return;
569
570 /* route the table */
571 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700572 if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX))
Shaohua Li41017f02006-02-08 17:11:38 +0800573 return;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700574 irq = head = dev->irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800575 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700576 entry = msi_desc[irq];
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700577 write_msi_msg(entry, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800578
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700579 tail = msi_desc[irq]->link.tail;
580 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800581 }
582 dev->irq = temp;
583
584 pci_write_config_word(dev, msi_control_reg(pos), save);
585 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
586}
587#endif
588
Mark Maulefd58e552006-04-10 21:17:48 -0500589static int msi_register_init(struct pci_dev *dev, struct msi_desc *entry)
Shaohua Li41017f02006-02-08 17:11:38 +0800590{
Mark Maulefd58e552006-04-10 21:17:48 -0500591 int status;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700592 struct msi_msg msg;
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700593 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800594 u16 control;
595
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700596 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800597 pci_read_config_word(dev, msi_control_reg(pos), &control);
Mark Maulefd58e552006-04-10 21:17:48 -0500598
Shaohua Li41017f02006-02-08 17:11:38 +0800599 /* Configure MSI capability structure */
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700600 status = msi_ops->setup(dev, dev->irq, &msg);
Mark Maulefd58e552006-04-10 21:17:48 -0500601 if (status < 0)
602 return status;
603
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700604 write_msi_msg(entry, &msg);
Shaohua Li41017f02006-02-08 17:11:38 +0800605 if (entry->msi_attrib.maskbit) {
606 unsigned int maskbits, temp;
607 /* All MSIs are unmasked by default, Mask them all */
608 pci_read_config_dword(dev,
609 msi_mask_bits_reg(pos, is_64bit_address(control)),
610 &maskbits);
611 temp = (1 << multi_msi_capable(control));
612 temp = ((temp - 1) & ~temp);
613 maskbits |= temp;
614 pci_write_config_dword(dev,
615 msi_mask_bits_reg(pos, is_64bit_address(control)),
616 maskbits);
617 }
Mark Maulefd58e552006-04-10 21:17:48 -0500618
619 return 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/**
623 * msi_capability_init - configure device's MSI capability structure
624 * @dev: pointer to the pci_dev data structure of MSI device function
625 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600626 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700627 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700629 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 **/
631static int msi_capability_init(struct pci_dev *dev)
632{
Mark Maulefd58e552006-04-10 21:17:48 -0500633 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700635 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 u16 control;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700637 struct hw_interrupt_type *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
640 pci_read_config_word(dev, msi_control_reg(pos), &control);
641 /* MSI Entry Initialization */
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700642 handler = &msi_irq_wo_maskbit_type;
643 if (is_mask_bit_support(control))
644 handler = &msi_irq_w_maskbit_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700646 irq = create_msi_irq(handler);
647 if (irq < 0)
648 return irq;
649
650 entry = get_irq_data(irq);
651 entry->link.head = irq;
652 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700654 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 entry->msi_attrib.entry_nr = 0;
656 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700657 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700658 entry->msi_attrib.pos = pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700659 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 entry->dev = dev;
661 if (is_mask_bit_support(control)) {
662 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
663 is_64bit_address(control));
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Configure MSI capability structure */
Mark Maulefd58e552006-04-10 21:17:48 -0500666 status = msi_register_init(dev, entry);
667 if (status != 0) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700668 dev->irq = entry->msi_attrib.default_irq;
669 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500670 return status;
671 }
Shaohua Li41017f02006-02-08 17:11:38 +0800672
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700673 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Set MSI enabled bits */
675 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
676
677 return 0;
678}
679
680/**
681 * msix_capability_init - configure device's MSI-X capability
682 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700683 * @entries: pointer to an array of struct msix_entry entries
684 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600686 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700687 * single MSI-X irq. A return of zero indicates the successful setup of
688 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 **/
690static int msix_capability_init(struct pci_dev *dev,
691 struct msix_entry *entries, int nvec)
692{
693 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700694 struct msi_msg msg;
Mark Maulefd58e552006-04-10 21:17:48 -0500695 int status;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700696 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800697 unsigned long phys_addr;
698 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 u16 control;
700 u8 bir;
701 void __iomem *base;
702
703 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
704 /* Request & Map MSI-X table region */
705 pci_read_config_word(dev, msi_control_reg(pos), &control);
706 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800707
708 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800710 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
711 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
713 if (base == NULL)
714 return -ENOMEM;
715
716 /* MSI-X Table Initialization */
717 for (i = 0; i < nvec; i++) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700718 irq = create_msi_irq(&msix_irq_type);
719 if (irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700722 entry = get_irq_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 j = entries[i].entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700724 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700726 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 entry->msi_attrib.entry_nr = j;
728 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700729 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700730 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 entry->dev = dev;
732 entry->mask_base = base;
733 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700734 entry->link.head = irq;
735 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 head = entry;
737 } else {
738 entry->link.head = temp;
739 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700740 tail->link.tail = irq;
741 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700743 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 tail = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* Configure MSI-X capability structure */
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700746 status = msi_ops->setup(dev, irq, &msg);
747 if (status < 0) {
748 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500749 break;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700750 }
Mark Maulefd58e552006-04-10 21:17:48 -0500751
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700752 write_msi_msg(entry, &msg);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700753 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700756 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 i--;
758 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700759 irq = (entries + i)->vector;
760 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 (entries + i)->vector = 0;
762 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700763 /* If we had some success report the number of irqs
764 * we succeeded in setting up.
765 */
766 if (avail <= 0)
767 avail = -EBUSY;
768 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 /* Set MSI-X enabled bits */
771 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
772
773 return 0;
774}
775
776/**
Brice Goglin24334a12006-08-31 01:55:07 -0400777 * pci_msi_supported - check whether MSI may be enabled on device
778 * @dev: pointer to the pci_dev data structure of MSI device function
779 *
780 * MSI must be globally enabled and supported by the device and its root
781 * bus. But, the root bus is not easy to find since some architectures
782 * have virtual busses on top of the PCI hierarchy (for instance the
783 * hypertransport bus), while the actual bus where MSI must be supported
784 * is below. So we test the MSI flag on all parent busses and assume
785 * that no quirk will ever set the NO_MSI flag on a non-root bus.
786 **/
787static
788int pci_msi_supported(struct pci_dev * dev)
789{
790 struct pci_bus *bus;
791
792 if (!pci_msi_enable || !dev || dev->no_msi)
793 return -EINVAL;
794
795 /* check MSI flags of all parent busses */
796 for (bus = dev->bus; bus; bus = bus->parent)
797 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
798 return -EINVAL;
799
800 return 0;
801}
802
803/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * pci_enable_msi - configure device's MSI capability structure
805 * @dev: pointer to the pci_dev data structure of MSI device function
806 *
807 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700808 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * MSI mode enabled on its hardware device function. A return of zero
810 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700811 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 **/
813int pci_enable_msi(struct pci_dev* dev)
814{
Brice Goglin24334a12006-08-31 01:55:07 -0400815 int pos, temp, status;
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700816 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Brice Goglin24334a12006-08-31 01:55:07 -0400818 if (pci_msi_supported(dev) < 0)
819 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 temp = dev->irq;
822
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700823 status = msi_init();
824 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return status;
826
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700827 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
828 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EINVAL;
830
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700831 pci_read_config_word(dev, msi_control_reg(pos), &control);
832 if (!is_64bit_address(control) && msi_ops->needs_64bit_address)
833 return -EINVAL;
834
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700835 WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700837 /* Check whether driver already requested for MSI-X irqs */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700838 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700839 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700841 "Device already has MSI-X irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 pci_name(dev));
843 dev->irq = temp;
844 return -EINVAL;
845 }
846 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return status;
848}
849
850void pci_disable_msi(struct pci_dev* dev)
851{
852 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700853 int pos, default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 u16 control;
855 unsigned long flags;
856
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700857 if (!pci_msi_enable)
858 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700859 if (!dev)
860 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700861
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700862 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
863 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return;
865
866 pci_read_config_word(dev, msi_control_reg(pos), &control);
867 if (!(control & PCI_MSI_FLAGS_ENABLE))
868 return;
869
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700870 disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 spin_lock_irqsave(&msi_lock, flags);
873 entry = msi_desc[dev->irq];
874 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
875 spin_unlock_irqrestore(&msi_lock, flags);
876 return;
877 }
Eric W. Biederman1f800252006-10-04 02:16:56 -0700878 if (irq_has_action(dev->irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 spin_unlock_irqrestore(&msi_lock, flags);
880 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700881 "free_irq() on MSI irq %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 pci_name(dev), dev->irq);
Eric W. Biederman1f800252006-10-04 02:16:56 -0700883 BUG_ON(irq_has_action(dev->irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700885 default_irq = entry->msi_attrib.default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700887 msi_free_irq(dev, dev->irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700888
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700889 /* Restore dev->irq to its default pin-assertion irq */
890 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892}
893
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700894static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 struct msi_desc *entry;
897 int head, entry_nr, type;
898 void __iomem *base;
899 unsigned long flags;
900
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700901 msi_ops->teardown(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700904 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!entry || entry->dev != dev) {
906 spin_unlock_irqrestore(&msi_lock, flags);
907 return -EINVAL;
908 }
909 type = entry->msi_attrib.type;
910 entry_nr = entry->msi_attrib.entry_nr;
911 head = entry->link.head;
912 base = entry->mask_base;
913 msi_desc[entry->link.head]->link.tail = entry->link.tail;
914 msi_desc[entry->link.tail]->link.head = entry->link.head;
915 entry->dev = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700916 msi_desc[irq] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 spin_unlock_irqrestore(&msi_lock, flags);
918
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700919 destroy_msi_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700922 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
923 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700925 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 return 0;
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/**
933 * pci_enable_msix - configure device's MSI-X capability structure
934 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700935 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700936 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
938 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700939 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * MSI-X mode enabled on its hardware device function. A return of zero
941 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700942 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700944 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * its request.
946 **/
947int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
948{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700949 int status, pos, nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int i, j, temp;
951 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Brice Goglin24334a12006-08-31 01:55:07 -0400953 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return -EINVAL;
955
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700956 status = msi_init();
957 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return status;
959
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700960 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
961 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EINVAL;
963
964 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 nr_entries = multi_msix_capable(control);
966 if (nvec > nr_entries)
967 return -EINVAL;
968
969 /* Check for any invalid entries */
970 for (i = 0; i < nvec; i++) {
971 if (entries[i].entry >= nr_entries)
972 return -EINVAL; /* invalid entry */
973 for (j = i + 1; j < nvec; j++) {
974 if (entries[i].entry == entries[j].entry)
975 return -EINVAL; /* duplicate entry */
976 }
977 }
978 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700979 WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSIX));
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700980
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700981 /* Check whether driver already requested for MSI irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700983 !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700985 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 pci_name(dev));
987 dev->irq = temp;
988 return -EINVAL;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return status;
992}
993
994void pci_disable_msix(struct pci_dev* dev)
995{
996 int pos, temp;
997 u16 control;
998
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700999 if (!pci_msi_enable)
1000 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001001 if (!dev)
1002 return;
1003
1004 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1005 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return;
1007
1008 pci_read_config_word(dev, msi_control_reg(pos), &control);
1009 if (!(control & PCI_MSIX_FLAGS_ENABLE))
1010 return;
1011
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001012 disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001015 if (!msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Eric W. Biederman1f800252006-10-04 02:16:56 -07001016 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned long flags;
1018
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001019 irq = head = dev->irq;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001020 dev->irq = temp; /* Restore pin IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 while (head != tail) {
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001022 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001023 tail = msi_desc[irq]->link.tail;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001024 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1f800252006-10-04 02:16:56 -07001025 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001027 else if (irq != head) /* Release MSI-X irq */
1028 msi_free_irq(dev, irq);
1029 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001031 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001034 "free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 pci_name(dev));
1036 BUG_ON(warning > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 }
1039}
1040
1041/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001042 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1044 *
Steven Coleeaae4b32005-05-03 18:38:30 -06001045 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001046 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * allocated for this device function, are reclaimed to unused state,
1048 * which may be used later on.
1049 **/
1050void msi_remove_pci_irq_vectors(struct pci_dev* dev)
1051{
Eric W. Biederman1f800252006-10-04 02:16:56 -07001052 int pos, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 unsigned long flags;
1054
1055 if (!pci_msi_enable || !dev)
1056 return;
1057
1058 temp = dev->irq; /* Save IOAPIC IRQ */
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001059 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001060 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
Eric W. Biederman1f800252006-10-04 02:16:56 -07001061 if (irq_has_action(dev->irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001063 "called without free_irq() on MSI irq %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 pci_name(dev), dev->irq);
Eric W. Biederman1f800252006-10-04 02:16:56 -07001065 BUG_ON(irq_has_action(dev->irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001066 } else /* Release MSI irq assigned to this device */
1067 msi_free_irq(dev, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 dev->irq = temp; /* Restore IOAPIC IRQ */
1069 }
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001070 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001071 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
1072 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 void __iomem *base = NULL;
1074
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001075 irq = head = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 while (head != tail) {
1077 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001078 tail = msi_desc[irq]->link.tail;
1079 base = msi_desc[irq]->mask_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1f800252006-10-04 02:16:56 -07001081 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001083 else if (irq != head) /* Release MSI-X irq */
1084 msi_free_irq(dev, irq);
1085 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001087 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 iounmap(base);
1090 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001091 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 pci_name(dev));
1093 BUG_ON(warning > 0);
1094 }
1095 dev->irq = temp; /* Restore IOAPIC IRQ */
1096 }
1097}
1098
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001099void pci_no_msi(void)
1100{
1101 pci_msi_enable = 0;
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104EXPORT_SYMBOL(pci_enable_msi);
1105EXPORT_SYMBOL(pci_disable_msi);
1106EXPORT_SYMBOL(pci_enable_msix);
1107EXPORT_SYMBOL(pci_disable_msix);