blob: da2c6c2b6b11cff6ed117ec11dbb1a67bcca3c1d [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{
191 struct msi_desc *entry;
192 unsigned long flags;
193
194 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700195 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!entry || !entry->dev) {
197 spin_unlock_irqrestore(&msi_lock, flags);
198 return 0;
199 }
200 entry->msi_attrib.state = 1; /* Mark it active */
201 spin_unlock_irqrestore(&msi_lock, flags);
202
203 return 0; /* never anything pending */
204}
205
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700206static unsigned int startup_msi_irq_w_maskbit(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700208 startup_msi_irq_wo_maskbit(irq);
209 unmask_MSI_irq(irq);
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700210 return 0; /* never anything pending */
211}
212
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700213static void shutdown_msi_irq(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct msi_desc *entry;
216 unsigned long flags;
217
218 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700219 entry = msi_desc[irq];
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700220 if (entry && entry->dev)
221 entry->msi_attrib.state = 0; /* Mark it not active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 spin_unlock_irqrestore(&msi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700225static void end_msi_irq_wo_maskbit(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700226{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700227 move_native_irq(irq);
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700228 ack_APIC_irq();
229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700231static void end_msi_irq_w_maskbit(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700233 move_native_irq(irq);
234 unmask_MSI_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ack_APIC_irq();
236}
237
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700238static void do_nothing(unsigned int irq)
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700239{
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*
243 * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
244 * which implement the MSI-X Capability Structure.
245 */
246static struct hw_interrupt_type msix_irq_type = {
247 .typename = "PCI-MSI-X",
248 .startup = startup_msi_irq_w_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700249 .shutdown = shutdown_msi_irq,
250 .enable = unmask_MSI_irq,
251 .disable = mask_MSI_irq,
252 .ack = mask_MSI_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .end = end_msi_irq_w_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800254 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
257/*
258 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
259 * which implement the MSI Capability Structure with
260 * Mask-and-Pending Bits.
261 */
262static struct hw_interrupt_type msi_irq_w_maskbit_type = {
263 .typename = "PCI-MSI",
264 .startup = startup_msi_irq_w_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700265 .shutdown = shutdown_msi_irq,
266 .enable = unmask_MSI_irq,
267 .disable = mask_MSI_irq,
268 .ack = mask_MSI_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .end = end_msi_irq_w_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800270 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
273/*
274 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
275 * which implement the MSI Capability Structure without
276 * Mask-and-Pending Bits.
277 */
278static struct hw_interrupt_type msi_irq_wo_maskbit_type = {
279 .typename = "PCI-MSI",
280 .startup = startup_msi_irq_wo_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700281 .shutdown = shutdown_msi_irq,
282 .enable = do_nothing,
283 .disable = do_nothing,
284 .ack = do_nothing,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 .end = end_msi_irq_wo_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800286 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700289static int msi_free_irq(struct pci_dev* dev, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int msi_init(void)
291{
292 static int status = -ENOMEM;
293
294 if (!status)
295 return status;
296
297 if (pci_msi_quirk) {
298 pci_msi_enable = 0;
299 printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n");
300 status = -EINVAL;
301 return status;
302 }
303
Mark Maulefd58e552006-04-10 21:17:48 -0500304 status = msi_arch_init();
305 if (status < 0) {
306 pci_msi_enable = 0;
307 printk(KERN_WARNING
308 "PCI: MSI arch init failed. MSI disabled.\n");
309 return status;
310 }
311
312 if (! msi_ops) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700313 pci_msi_enable = 0;
Mark Maulefd58e552006-04-10 21:17:48 -0500314 printk(KERN_WARNING
315 "PCI: MSI ops not registered. MSI disabled.\n");
316 status = -EINVAL;
317 return status;
318 }
319
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700320 status = msi_cache_init();
321 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 pci_msi_enable = 0;
323 printk(KERN_WARNING "PCI: MSI cache init failed\n");
324 return status;
325 }
Mark Maulefd58e552006-04-10 21:17:48 -0500326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return status;
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static struct msi_desc* alloc_msi_entry(void)
331{
332 struct msi_desc *entry;
333
Pekka J Enberg57181782006-09-27 01:51:03 -0700334 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!entry)
336 return NULL;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 entry->link.tail = entry->link.head = 0; /* single message */
339 entry->dev = NULL;
340
341 return entry;
342}
343
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700344static void attach_msi_entry(struct msi_desc *entry, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned long flags;
347
348 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700349 msi_desc[irq] = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 spin_unlock_irqrestore(&msi_lock, flags);
351}
352
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700353static int create_msi_irq(struct hw_interrupt_type *handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700355 struct msi_desc *entry;
356 int irq;
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100357
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700358 entry = alloc_msi_entry();
359 if (!entry)
360 return -ENOMEM;
361
362 irq = create_irq();
363 if (irq < 0) {
364 kmem_cache_free(msi_cachep, entry);
365 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700367
368 set_irq_chip(irq, handler);
369 set_irq_data(irq, entry);
370
371 return irq;
372}
373
374static void destroy_msi_irq(unsigned int irq)
375{
376 struct msi_desc *entry;
377
378 entry = get_irq_data(irq);
379 set_irq_chip(irq, NULL);
380 set_irq_data(irq, NULL);
381 destroy_irq(irq);
382 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
386{
387 u16 control;
388
389 pci_read_config_word(dev, msi_control_reg(pos), &control);
390 if (type == PCI_CAP_ID_MSI) {
391 /* Set enabled bits to single MSI & enable MSI_enable bit */
392 msi_enable(control, 1);
393 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800394 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 } else {
396 msix_enable(control);
397 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800398 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
401 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400402 pci_intx(dev, 0); /* disable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404}
405
Kristen Accardi4602b882005-08-16 15:15:58 -0700406void disable_msi_mode(struct pci_dev *dev, int pos, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 u16 control;
409
410 pci_read_config_word(dev, msi_control_reg(pos), &control);
411 if (type == PCI_CAP_ID_MSI) {
412 /* Set enabled bits to single MSI & enable MSI_enable bit */
413 msi_disable(control);
414 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800415 dev->msi_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 } else {
417 msix_disable(control);
418 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800419 dev->msix_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
422 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400423 pci_intx(dev, 1); /* enable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425}
426
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700427static int msi_lookup_irq(struct pci_dev *dev, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700429 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long flags;
431
432 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700433 for (irq = 0; irq < NR_IRQS; irq++) {
434 if (!msi_desc[irq] || msi_desc[irq]->dev != dev ||
435 msi_desc[irq]->msi_attrib.type != type ||
436 msi_desc[irq]->msi_attrib.default_irq != dev->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 continue;
438 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700439 /* This pre-assigned MSI irq for this device
440 already exits. Override dev->irq with this irq */
441 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443 }
444 spin_unlock_irqrestore(&msi_lock, flags);
445
446 return -EACCES;
447}
448
449void pci_scan_msi_device(struct pci_dev *dev)
450{
451 if (!dev)
452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Shaohua Li41017f02006-02-08 17:11:38 +0800455#ifdef CONFIG_PM
456int pci_save_msi_state(struct pci_dev *dev)
457{
458 int pos, i = 0;
459 u16 control;
460 struct pci_cap_saved_state *save_state;
461 u32 *cap;
462
463 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
464 if (pos <= 0 || dev->no_msi)
465 return 0;
466
467 pci_read_config_word(dev, msi_control_reg(pos), &control);
468 if (!(control & PCI_MSI_FLAGS_ENABLE))
469 return 0;
470
471 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
472 GFP_KERNEL);
473 if (!save_state) {
474 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
475 return -ENOMEM;
476 }
477 cap = &save_state->data[0];
478
479 pci_read_config_dword(dev, pos, &cap[i++]);
480 control = cap[0] >> 16;
481 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
482 if (control & PCI_MSI_FLAGS_64BIT) {
483 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
484 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
485 } else
486 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
487 if (control & PCI_MSI_FLAGS_MASKBIT)
488 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800489 save_state->cap_nr = PCI_CAP_ID_MSI;
490 pci_add_saved_cap(dev, save_state);
491 return 0;
492}
493
494void pci_restore_msi_state(struct pci_dev *dev)
495{
496 int i = 0, pos;
497 u16 control;
498 struct pci_cap_saved_state *save_state;
499 u32 *cap;
500
501 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
502 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
503 if (!save_state || pos <= 0)
504 return;
505 cap = &save_state->data[0];
506
507 control = cap[i++] >> 16;
508 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
509 if (control & PCI_MSI_FLAGS_64BIT) {
510 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
511 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
512 } else
513 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
514 if (control & PCI_MSI_FLAGS_MASKBIT)
515 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
516 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
517 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
518 pci_remove_saved_cap(save_state);
519 kfree(save_state);
520}
521
522int pci_save_msix_state(struct pci_dev *dev)
523{
524 int pos;
Mark Maulefd58e552006-04-10 21:17:48 -0500525 int temp;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700526 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800527 u16 control;
528 struct pci_cap_saved_state *save_state;
529
530 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
531 if (pos <= 0 || dev->no_msi)
532 return 0;
533
Mark Maulefd58e552006-04-10 21:17:48 -0500534 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800535 pci_read_config_word(dev, msi_control_reg(pos), &control);
536 if (!(control & PCI_MSIX_FLAGS_ENABLE))
537 return 0;
538 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
539 GFP_KERNEL);
540 if (!save_state) {
541 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
542 return -ENOMEM;
543 }
544 *((u16 *)&save_state->data[0]) = control;
545
Mark Maulefd58e552006-04-10 21:17:48 -0500546 /* save the table */
547 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700548 if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Mark Maulefd58e552006-04-10 21:17:48 -0500549 kfree(save_state);
550 return -EINVAL;
551 }
552
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700553 irq = head = dev->irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500554 while (head != tail) {
Mark Maulefd58e552006-04-10 21:17:48 -0500555 struct msi_desc *entry;
556
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700557 entry = msi_desc[irq];
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700558 read_msi_msg(entry, &entry->msg_save);
Mark Maulefd58e552006-04-10 21:17:48 -0500559
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700560 tail = msi_desc[irq]->link.tail;
561 irq = tail;
Mark Maulefd58e552006-04-10 21:17:48 -0500562 }
563 dev->irq = temp;
564
Shaohua Li41017f02006-02-08 17:11:38 +0800565 save_state->cap_nr = PCI_CAP_ID_MSIX;
566 pci_add_saved_cap(dev, save_state);
567 return 0;
568}
569
570void pci_restore_msix_state(struct pci_dev *dev)
571{
572 u16 save;
573 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700574 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800575 struct msi_desc *entry;
576 int temp;
577 struct pci_cap_saved_state *save_state;
578
579 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
580 if (!save_state)
581 return;
582 save = *((u16 *)&save_state->data[0]);
583 pci_remove_saved_cap(save_state);
584 kfree(save_state);
585
586 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
587 if (pos <= 0)
588 return;
589
590 /* route the table */
591 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700592 if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX))
Shaohua Li41017f02006-02-08 17:11:38 +0800593 return;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700594 irq = head = dev->irq;
Shaohua Li41017f02006-02-08 17:11:38 +0800595 while (head != tail) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700596 entry = msi_desc[irq];
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700597 write_msi_msg(entry, &entry->msg_save);
Shaohua Li41017f02006-02-08 17:11:38 +0800598
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700599 tail = msi_desc[irq]->link.tail;
600 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800601 }
602 dev->irq = temp;
603
604 pci_write_config_word(dev, msi_control_reg(pos), save);
605 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
606}
607#endif
608
Mark Maulefd58e552006-04-10 21:17:48 -0500609static int msi_register_init(struct pci_dev *dev, struct msi_desc *entry)
Shaohua Li41017f02006-02-08 17:11:38 +0800610{
Mark Maulefd58e552006-04-10 21:17:48 -0500611 int status;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700612 struct msi_msg msg;
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700613 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800614 u16 control;
615
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700616 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800617 pci_read_config_word(dev, msi_control_reg(pos), &control);
Mark Maulefd58e552006-04-10 21:17:48 -0500618
Shaohua Li41017f02006-02-08 17:11:38 +0800619 /* Configure MSI capability structure */
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700620 status = msi_ops->setup(dev, dev->irq, &msg);
Mark Maulefd58e552006-04-10 21:17:48 -0500621 if (status < 0)
622 return status;
623
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700624 write_msi_msg(entry, &msg);
Shaohua Li41017f02006-02-08 17:11:38 +0800625 if (entry->msi_attrib.maskbit) {
626 unsigned int maskbits, temp;
627 /* All MSIs are unmasked by default, Mask them all */
628 pci_read_config_dword(dev,
629 msi_mask_bits_reg(pos, is_64bit_address(control)),
630 &maskbits);
631 temp = (1 << multi_msi_capable(control));
632 temp = ((temp - 1) & ~temp);
633 maskbits |= temp;
634 pci_write_config_dword(dev,
635 msi_mask_bits_reg(pos, is_64bit_address(control)),
636 maskbits);
637 }
Mark Maulefd58e552006-04-10 21:17:48 -0500638
639 return 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/**
643 * msi_capability_init - configure device's MSI capability structure
644 * @dev: pointer to the pci_dev data structure of MSI device function
645 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600646 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700647 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700649 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 **/
651static int msi_capability_init(struct pci_dev *dev)
652{
Mark Maulefd58e552006-04-10 21:17:48 -0500653 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700655 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u16 control;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700657 struct hw_interrupt_type *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
660 pci_read_config_word(dev, msi_control_reg(pos), &control);
661 /* MSI Entry Initialization */
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700662 handler = &msi_irq_wo_maskbit_type;
663 if (is_mask_bit_support(control))
664 handler = &msi_irq_w_maskbit_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700666 irq = create_msi_irq(handler);
667 if (irq < 0)
668 return irq;
669
670 entry = get_irq_data(irq);
671 entry->link.head = irq;
672 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 entry->msi_attrib.type = PCI_CAP_ID_MSI;
674 entry->msi_attrib.state = 0; /* Mark it not active */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700675 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 entry->msi_attrib.entry_nr = 0;
677 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700678 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700679 entry->msi_attrib.pos = pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700680 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 entry->dev = dev;
682 if (is_mask_bit_support(control)) {
683 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
684 is_64bit_address(control));
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* Configure MSI capability structure */
Mark Maulefd58e552006-04-10 21:17:48 -0500687 status = msi_register_init(dev, entry);
688 if (status != 0) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700689 dev->irq = entry->msi_attrib.default_irq;
690 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500691 return status;
692 }
Shaohua Li41017f02006-02-08 17:11:38 +0800693
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700694 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Set MSI enabled bits */
696 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
697
698 return 0;
699}
700
701/**
702 * msix_capability_init - configure device's MSI-X capability
703 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700704 * @entries: pointer to an array of struct msix_entry entries
705 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600707 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700708 * single MSI-X irq. A return of zero indicates the successful setup of
709 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 **/
711static int msix_capability_init(struct pci_dev *dev,
712 struct msix_entry *entries, int nvec)
713{
714 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700715 struct msi_msg msg;
Mark Maulefd58e552006-04-10 21:17:48 -0500716 int status;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700717 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800718 unsigned long phys_addr;
719 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 u16 control;
721 u8 bir;
722 void __iomem *base;
723
724 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
725 /* Request & Map MSI-X table region */
726 pci_read_config_word(dev, msi_control_reg(pos), &control);
727 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800728
729 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800731 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
732 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
734 if (base == NULL)
735 return -ENOMEM;
736
737 /* MSI-X Table Initialization */
738 for (i = 0; i < nvec; i++) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700739 irq = create_msi_irq(&msix_irq_type);
740 if (irq < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700743 entry = get_irq_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 j = entries[i].entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700745 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
747 entry->msi_attrib.state = 0; /* Mark it not active */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700748 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 entry->msi_attrib.entry_nr = j;
750 entry->msi_attrib.maskbit = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700751 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700752 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 entry->dev = dev;
754 entry->mask_base = base;
755 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700756 entry->link.head = irq;
757 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 head = entry;
759 } else {
760 entry->link.head = temp;
761 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700762 tail->link.tail = irq;
763 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700765 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 tail = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* Configure MSI-X capability structure */
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700768 status = msi_ops->setup(dev, irq, &msg);
769 if (status < 0) {
770 destroy_msi_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500771 break;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700772 }
Mark Maulefd58e552006-04-10 21:17:48 -0500773
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700774 write_msi_msg(entry, &msg);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700775 attach_msi_entry(entry, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700778 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 i--;
780 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700781 irq = (entries + i)->vector;
782 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 (entries + i)->vector = 0;
784 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700785 /* If we had some success report the number of irqs
786 * we succeeded in setting up.
787 */
788 if (avail <= 0)
789 avail = -EBUSY;
790 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 /* Set MSI-X enabled bits */
793 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
794
795 return 0;
796}
797
798/**
Brice Goglin24334a12006-08-31 01:55:07 -0400799 * pci_msi_supported - check whether MSI may be enabled on device
800 * @dev: pointer to the pci_dev data structure of MSI device function
801 *
802 * MSI must be globally enabled and supported by the device and its root
803 * bus. But, the root bus is not easy to find since some architectures
804 * have virtual busses on top of the PCI hierarchy (for instance the
805 * hypertransport bus), while the actual bus where MSI must be supported
806 * is below. So we test the MSI flag on all parent busses and assume
807 * that no quirk will ever set the NO_MSI flag on a non-root bus.
808 **/
809static
810int pci_msi_supported(struct pci_dev * dev)
811{
812 struct pci_bus *bus;
813
814 if (!pci_msi_enable || !dev || dev->no_msi)
815 return -EINVAL;
816
817 /* check MSI flags of all parent busses */
818 for (bus = dev->bus; bus; bus = bus->parent)
819 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
820 return -EINVAL;
821
822 return 0;
823}
824
825/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * pci_enable_msi - configure device's MSI capability structure
827 * @dev: pointer to the pci_dev data structure of MSI device function
828 *
829 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700830 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * MSI mode enabled on its hardware device function. A return of zero
832 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700833 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 **/
835int pci_enable_msi(struct pci_dev* dev)
836{
Brice Goglin24334a12006-08-31 01:55:07 -0400837 int pos, temp, status;
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700838 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Brice Goglin24334a12006-08-31 01:55:07 -0400840 if (pci_msi_supported(dev) < 0)
841 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 temp = dev->irq;
844
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700845 status = msi_init();
846 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return status;
848
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700849 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
850 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -EINVAL;
852
Eric W. Biederman38bc0362006-10-04 02:16:34 -0700853 pci_read_config_word(dev, msi_control_reg(pos), &control);
854 if (!is_64bit_address(control) && msi_ops->needs_64bit_address)
855 return -EINVAL;
856
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700857 WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700859 /* Check whether driver already requested for MSI-X irqs */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700860 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700861 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700863 "Device already has MSI-X irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 pci_name(dev));
865 dev->irq = temp;
866 return -EINVAL;
867 }
868 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return status;
870}
871
872void pci_disable_msi(struct pci_dev* dev)
873{
874 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700875 int pos, default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 u16 control;
877 unsigned long flags;
878
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700879 if (!pci_msi_enable)
880 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700881 if (!dev)
882 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700883
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700884 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
885 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return;
887
888 pci_read_config_word(dev, msi_control_reg(pos), &control);
889 if (!(control & PCI_MSI_FLAGS_ENABLE))
890 return;
891
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700892 disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 spin_lock_irqsave(&msi_lock, flags);
895 entry = msi_desc[dev->irq];
896 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
897 spin_unlock_irqrestore(&msi_lock, flags);
898 return;
899 }
900 if (entry->msi_attrib.state) {
901 spin_unlock_irqrestore(&msi_lock, flags);
902 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700903 "free_irq() on MSI irq %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 pci_name(dev), dev->irq);
905 BUG_ON(entry->msi_attrib.state > 0);
906 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700907 default_irq = entry->msi_attrib.default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700909 msi_free_irq(dev, dev->irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700910
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700911 /* Restore dev->irq to its default pin-assertion irq */
912 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914}
915
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700916static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct msi_desc *entry;
919 int head, entry_nr, type;
920 void __iomem *base;
921 unsigned long flags;
922
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700923 msi_ops->teardown(irq);
Mark Maulefd58e552006-04-10 21:17:48 -0500924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700926 entry = msi_desc[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!entry || entry->dev != dev) {
928 spin_unlock_irqrestore(&msi_lock, flags);
929 return -EINVAL;
930 }
931 type = entry->msi_attrib.type;
932 entry_nr = entry->msi_attrib.entry_nr;
933 head = entry->link.head;
934 base = entry->mask_base;
935 msi_desc[entry->link.head]->link.tail = entry->link.tail;
936 msi_desc[entry->link.tail]->link.head = entry->link.head;
937 entry->dev = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700938 msi_desc[irq] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 spin_unlock_irqrestore(&msi_lock, flags);
940
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700941 destroy_msi_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700944 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
945 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700947 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
951 return 0;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/**
955 * pci_enable_msix - configure device's MSI-X capability structure
956 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700957 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700958 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 *
960 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700961 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * MSI-X mode enabled on its hardware device function. A return of zero
963 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700964 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700966 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * its request.
968 **/
969int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
970{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700971 int status, pos, nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int i, j, temp;
973 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Brice Goglin24334a12006-08-31 01:55:07 -0400975 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return -EINVAL;
977
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700978 status = msi_init();
979 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return status;
981
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700982 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
983 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EINVAL;
985
986 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 nr_entries = multi_msix_capable(control);
988 if (nvec > nr_entries)
989 return -EINVAL;
990
991 /* Check for any invalid entries */
992 for (i = 0; i < nvec; i++) {
993 if (entries[i].entry >= nr_entries)
994 return -EINVAL; /* invalid entry */
995 for (j = i + 1; j < nvec; j++) {
996 if (entries[i].entry == entries[j].entry)
997 return -EINVAL; /* duplicate entry */
998 }
999 }
1000 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001001 WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSIX));
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001002
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001003 /* Check whether driver already requested for MSI irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001005 !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001007 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 pci_name(dev));
1009 dev->irq = temp;
1010 return -EINVAL;
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return status;
1014}
1015
1016void pci_disable_msix(struct pci_dev* dev)
1017{
1018 int pos, temp;
1019 u16 control;
1020
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001021 if (!pci_msi_enable)
1022 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001023 if (!dev)
1024 return;
1025
1026 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1027 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return;
1029
1030 pci_read_config_word(dev, msi_control_reg(pos), &control);
1031 if (!(control & PCI_MSIX_FLAGS_ENABLE))
1032 return;
1033
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001034 disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 temp = dev->irq;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001037 if (!msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
1038 int state, irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 unsigned long flags;
1040
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001041 irq = head = dev->irq;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001042 dev->irq = temp; /* Restore pin IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 while (head != tail) {
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001044 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001045 state = msi_desc[irq]->msi_attrib.state;
1046 tail = msi_desc[irq]->link.tail;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001047 spin_unlock_irqrestore(&msi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (state)
1049 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001050 else if (irq != head) /* Release MSI-X irq */
1051 msi_free_irq(dev, irq);
1052 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001054 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001057 "free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 pci_name(dev));
1059 BUG_ON(warning > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 }
1062}
1063
1064/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001065 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1067 *
Steven Coleeaae4b32005-05-03 18:38:30 -06001068 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001069 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 * allocated for this device function, are reclaimed to unused state,
1071 * which may be used later on.
1072 **/
1073void msi_remove_pci_irq_vectors(struct pci_dev* dev)
1074{
1075 int state, pos, temp;
1076 unsigned long flags;
1077
1078 if (!pci_msi_enable || !dev)
1079 return;
1080
1081 temp = dev->irq; /* Save IOAPIC IRQ */
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001082 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001083 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_lock_irqsave(&msi_lock, flags);
1085 state = msi_desc[dev->irq]->msi_attrib.state;
1086 spin_unlock_irqrestore(&msi_lock, flags);
1087 if (state) {
1088 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001089 "called without free_irq() on MSI irq %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 pci_name(dev), dev->irq);
1091 BUG_ON(state > 0);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001092 } else /* Release MSI irq assigned to this device */
1093 msi_free_irq(dev, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dev->irq = temp; /* Restore IOAPIC IRQ */
1095 }
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001096 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001097 if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) {
1098 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 void __iomem *base = NULL;
1100
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001101 irq = head = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 while (head != tail) {
1103 spin_lock_irqsave(&msi_lock, flags);
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001104 state = msi_desc[irq]->msi_attrib.state;
1105 tail = msi_desc[irq]->link.tail;
1106 base = msi_desc[irq]->mask_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 spin_unlock_irqrestore(&msi_lock, flags);
1108 if (state)
1109 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001110 else if (irq != head) /* Release MSI-X irq */
1111 msi_free_irq(dev, irq);
1112 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001114 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 iounmap(base);
1117 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -07001118 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 pci_name(dev));
1120 BUG_ON(warning > 0);
1121 }
1122 dev->irq = temp; /* Restore IOAPIC IRQ */
1123 }
1124}
1125
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001126void pci_no_msi(void)
1127{
1128 pci_msi_enable = 0;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131EXPORT_SYMBOL(pci_enable_msi);
1132EXPORT_SYMBOL(pci_disable_msi);
1133EXPORT_SYMBOL(pci_enable_msix);
1134EXPORT_SYMBOL(pci_disable_msix);