blob: 75d1c0dacffa1dd27d4be050adb756811f8d15da [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
9#include <linux/mm.h>
10#include <linux/irq.h>
11#include <linux/interrupt.h>
12#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/ioport.h>
14#include <linux/smp_lock.h>
15#include <linux/pci.h>
16#include <linux/proc_fs.h>
17
18#include <asm/errno.h>
19#include <asm/io.h>
20#include <asm/smp.h>
21
22#include "pci.h"
23#include "msi.h"
24
25static DEFINE_SPINLOCK(msi_lock);
26static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL };
27static kmem_cache_t* msi_cachep;
28
29static int pci_msi_enable = 1;
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -070030static int last_alloc_vector;
31static int nr_released_vectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int nr_reserved_vectors = NR_HP_RESERVED_VECTORS;
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -070033static int nr_msix_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#ifndef CONFIG_X86_IO_APIC
36int vector_irq[NR_VECTORS] = { [0 ... NR_VECTORS - 1] = -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif
38
Mark Maulefd58e552006-04-10 21:17:48 -050039static struct msi_ops *msi_ops;
40
41int
42msi_register(struct msi_ops *ops)
43{
44 msi_ops = ops;
45 return 0;
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int msi_cache_init(void)
49{
Pekka J Enberg57181782006-09-27 01:51:03 -070050 msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
51 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!msi_cachep)
53 return -ENOMEM;
54
55 return 0;
56}
57
58static void msi_set_mask_bit(unsigned int vector, int flag)
59{
60 struct msi_desc *entry;
61
62 entry = (struct msi_desc *)msi_desc[vector];
63 if (!entry || !entry->dev || !entry->mask_base)
64 return;
65 switch (entry->msi_attrib.type) {
66 case PCI_CAP_ID_MSI:
67 {
68 int pos;
69 u32 mask_bits;
70
71 pos = (long)entry->mask_base;
72 pci_read_config_dword(entry->dev, pos, &mask_bits);
73 mask_bits &= ~(1);
74 mask_bits |= flag;
75 pci_write_config_dword(entry->dev, pos, mask_bits);
76 break;
77 }
78 case PCI_CAP_ID_MSIX:
79 {
80 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
81 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
82 writel(flag, entry->mask_base + offset);
83 break;
84 }
85 default:
86 break;
87 }
88}
89
90#ifdef CONFIG_SMP
91static void set_msi_affinity(unsigned int vector, cpumask_t cpu_mask)
92{
93 struct msi_desc *entry;
Mark Maulefd58e552006-04-10 21:17:48 -050094 u32 address_hi, address_lo;
Ashok Raj54d5d422005-09-06 15:16:15 -070095 unsigned int irq = vector;
Ashok Rajb4033c12005-11-08 21:42:33 -080096 unsigned int dest_cpu = first_cpu(cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 entry = (struct msi_desc *)msi_desc[vector];
99 if (!entry || !entry->dev)
100 return;
101
102 switch (entry->msi_attrib.type) {
103 case PCI_CAP_ID_MSI:
104 {
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700105 int pos = pci_find_capability(entry->dev, PCI_CAP_ID_MSI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700107 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
109
Mark Maulefd58e552006-04-10 21:17:48 -0500110 pci_read_config_dword(entry->dev, msi_upper_address_reg(pos),
111 &address_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 pci_read_config_dword(entry->dev, msi_lower_address_reg(pos),
Mark Maulefd58e552006-04-10 21:17:48 -0500113 &address_lo);
114
115 msi_ops->target(vector, dest_cpu, &address_hi, &address_lo);
116
117 pci_write_config_dword(entry->dev, msi_upper_address_reg(pos),
118 address_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 pci_write_config_dword(entry->dev, msi_lower_address_reg(pos),
Mark Maulefd58e552006-04-10 21:17:48 -0500120 address_lo);
Ashok Raj54d5d422005-09-06 15:16:15 -0700121 set_native_irq_info(irq, cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 break;
123 }
124 case PCI_CAP_ID_MSIX:
125 {
Mark Maulefd58e552006-04-10 21:17:48 -0500126 int offset_hi =
127 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
128 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET;
129 int offset_lo =
130 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
131 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Mark Maulefd58e552006-04-10 21:17:48 -0500133 address_hi = readl(entry->mask_base + offset_hi);
134 address_lo = readl(entry->mask_base + offset_lo);
135
136 msi_ops->target(vector, dest_cpu, &address_hi, &address_lo);
137
138 writel(address_hi, entry->mask_base + offset_hi);
139 writel(address_lo, entry->mask_base + offset_lo);
Ashok Raj54d5d422005-09-06 15:16:15 -0700140 set_native_irq_info(irq, cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
143 default:
144 break;
145 }
146}
Grant Grundler8169b5d2006-01-03 18:51:46 -0800147#else
148#define set_msi_affinity NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif /* CONFIG_SMP */
150
151static void mask_MSI_irq(unsigned int vector)
152{
153 msi_set_mask_bit(vector, 1);
154}
155
156static void unmask_MSI_irq(unsigned int vector)
157{
158 msi_set_mask_bit(vector, 0);
159}
160
161static unsigned int startup_msi_irq_wo_maskbit(unsigned int vector)
162{
163 struct msi_desc *entry;
164 unsigned long flags;
165
166 spin_lock_irqsave(&msi_lock, flags);
167 entry = msi_desc[vector];
168 if (!entry || !entry->dev) {
169 spin_unlock_irqrestore(&msi_lock, flags);
170 return 0;
171 }
172 entry->msi_attrib.state = 1; /* Mark it active */
173 spin_unlock_irqrestore(&msi_lock, flags);
174
175 return 0; /* never anything pending */
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static unsigned int startup_msi_irq_w_maskbit(unsigned int vector)
179{
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700180 startup_msi_irq_wo_maskbit(vector);
181 unmask_MSI_irq(vector);
182 return 0; /* never anything pending */
183}
184
185static void shutdown_msi_irq(unsigned int vector)
186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct msi_desc *entry;
188 unsigned long flags;
189
190 spin_lock_irqsave(&msi_lock, flags);
191 entry = msi_desc[vector];
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700192 if (entry && entry->dev)
193 entry->msi_attrib.state = 0; /* Mark it not active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 spin_unlock_irqrestore(&msi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700197static void end_msi_irq_wo_maskbit(unsigned int vector)
198{
Ashok Raj54d5d422005-09-06 15:16:15 -0700199 move_native_irq(vector);
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700200 ack_APIC_irq();
201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203static void end_msi_irq_w_maskbit(unsigned int vector)
204{
Ashok Raj54d5d422005-09-06 15:16:15 -0700205 move_native_irq(vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 unmask_MSI_irq(vector);
207 ack_APIC_irq();
208}
209
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700210static void do_nothing(unsigned int vector)
211{
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
216 * which implement the MSI-X Capability Structure.
217 */
218static struct hw_interrupt_type msix_irq_type = {
219 .typename = "PCI-MSI-X",
220 .startup = startup_msi_irq_w_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700221 .shutdown = shutdown_msi_irq,
222 .enable = unmask_MSI_irq,
223 .disable = mask_MSI_irq,
224 .ack = mask_MSI_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .end = end_msi_irq_w_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800226 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229/*
230 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
231 * which implement the MSI Capability Structure with
232 * Mask-and-Pending Bits.
233 */
234static struct hw_interrupt_type msi_irq_w_maskbit_type = {
235 .typename = "PCI-MSI",
236 .startup = startup_msi_irq_w_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700237 .shutdown = shutdown_msi_irq,
238 .enable = unmask_MSI_irq,
239 .disable = mask_MSI_irq,
240 .ack = mask_MSI_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .end = end_msi_irq_w_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800242 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
245/*
246 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
247 * which implement the MSI Capability Structure without
248 * Mask-and-Pending Bits.
249 */
250static struct hw_interrupt_type msi_irq_wo_maskbit_type = {
251 .typename = "PCI-MSI",
252 .startup = startup_msi_irq_wo_maskbit,
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700253 .shutdown = shutdown_msi_irq,
254 .enable = do_nothing,
255 .disable = do_nothing,
256 .ack = do_nothing,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .end = end_msi_irq_wo_maskbit,
Grant Grundler8169b5d2006-01-03 18:51:46 -0800258 .set_affinity = set_msi_affinity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static int msi_free_vector(struct pci_dev* dev, int vector, int reassign);
262static int assign_msi_vector(void)
263{
264 static int new_vector_avail = 1;
265 int vector;
266 unsigned long flags;
267
268 /*
269 * msi_lock is provided to ensure that successful allocation of MSI
270 * vector is assigned unique among drivers.
271 */
272 spin_lock_irqsave(&msi_lock, flags);
273
274 if (!new_vector_avail) {
275 int free_vector = 0;
276
277 /*
278 * vector_irq[] = -1 indicates that this specific vector is:
279 * - assigned for MSI (since MSI have no associated IRQ) or
280 * - assigned for legacy if less than 16, or
281 * - having no corresponding 1:1 vector-to-IOxAPIC IRQ mapping
282 * vector_irq[] = 0 indicates that this vector, previously
283 * assigned for MSI, is freed by hotplug removed operations.
284 * This vector will be reused for any subsequent hotplug added
285 * operations.
286 * vector_irq[] > 0 indicates that this vector is assigned for
287 * IOxAPIC IRQs. This vector and its value provides a 1-to-1
288 * vector-to-IOxAPIC IRQ mapping.
289 */
290 for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
291 if (vector_irq[vector] != 0)
292 continue;
293 free_vector = vector;
294 if (!msi_desc[vector])
295 break;
296 else
297 continue;
298 }
299 if (!free_vector) {
300 spin_unlock_irqrestore(&msi_lock, flags);
301 return -EBUSY;
302 }
303 vector_irq[free_vector] = -1;
304 nr_released_vectors--;
305 spin_unlock_irqrestore(&msi_lock, flags);
306 if (msi_desc[free_vector] != NULL) {
307 struct pci_dev *dev;
308 int tail;
309
310 /* free all linked vectors before re-assign */
311 do {
312 spin_lock_irqsave(&msi_lock, flags);
313 dev = msi_desc[free_vector]->dev;
314 tail = msi_desc[free_vector]->link.tail;
315 spin_unlock_irqrestore(&msi_lock, flags);
316 msi_free_vector(dev, tail, 1);
317 } while (free_vector != tail);
318 }
319
320 return free_vector;
321 }
322 vector = assign_irq_vector(AUTO_ASSIGN);
323 last_alloc_vector = vector;
324 if (vector == LAST_DEVICE_VECTOR)
325 new_vector_avail = 0;
326
327 spin_unlock_irqrestore(&msi_lock, flags);
328 return vector;
329}
330
331static int get_new_vector(void)
332{
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700333 int vector = assign_msi_vector();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700335 if (vector > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 set_intr_gate(vector, interrupt[vector]);
337
338 return vector;
339}
340
341static int msi_init(void)
342{
343 static int status = -ENOMEM;
344
345 if (!status)
346 return status;
347
348 if (pci_msi_quirk) {
349 pci_msi_enable = 0;
350 printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n");
351 status = -EINVAL;
352 return status;
353 }
354
Mark Maulefd58e552006-04-10 21:17:48 -0500355 status = msi_arch_init();
356 if (status < 0) {
357 pci_msi_enable = 0;
358 printk(KERN_WARNING
359 "PCI: MSI arch init failed. MSI disabled.\n");
360 return status;
361 }
362
363 if (! msi_ops) {
364 printk(KERN_WARNING
365 "PCI: MSI ops not registered. MSI disabled.\n");
366 status = -EINVAL;
367 return status;
368 }
369
370 last_alloc_vector = assign_irq_vector(AUTO_ASSIGN);
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700371 status = msi_cache_init();
372 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 pci_msi_enable = 0;
374 printk(KERN_WARNING "PCI: MSI cache init failed\n");
375 return status;
376 }
Mark Maulefd58e552006-04-10 21:17:48 -0500377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (last_alloc_vector < 0) {
379 pci_msi_enable = 0;
380 printk(KERN_WARNING "PCI: No interrupt vectors available for MSI\n");
381 status = -EBUSY;
382 return status;
383 }
384 vector_irq[last_alloc_vector] = 0;
385 nr_released_vectors++;
386
387 return status;
388}
389
390static int get_msi_vector(struct pci_dev *dev)
391{
392 return get_new_vector();
393}
394
395static struct msi_desc* alloc_msi_entry(void)
396{
397 struct msi_desc *entry;
398
Pekka J Enberg57181782006-09-27 01:51:03 -0700399 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!entry)
401 return NULL;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 entry->link.tail = entry->link.head = 0; /* single message */
404 entry->dev = NULL;
405
406 return entry;
407}
408
409static void attach_msi_entry(struct msi_desc *entry, int vector)
410{
411 unsigned long flags;
412
413 spin_lock_irqsave(&msi_lock, flags);
414 msi_desc[vector] = entry;
415 spin_unlock_irqrestore(&msi_lock, flags);
416}
417
418static void irq_handler_init(int cap_id, int pos, int mask)
419{
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100420 unsigned long flags;
421
422 spin_lock_irqsave(&irq_desc[pos].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (cap_id == PCI_CAP_ID_MSIX)
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700424 irq_desc[pos].chip = &msix_irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 else {
426 if (!mask)
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700427 irq_desc[pos].chip = &msi_irq_wo_maskbit_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 else
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700429 irq_desc[pos].chip = &msi_irq_w_maskbit_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Ingo Molnarf6bc2662006-01-26 01:42:11 +0100431 spin_unlock_irqrestore(&irq_desc[pos].lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
435{
436 u16 control;
437
438 pci_read_config_word(dev, msi_control_reg(pos), &control);
439 if (type == PCI_CAP_ID_MSI) {
440 /* Set enabled bits to single MSI & enable MSI_enable bit */
441 msi_enable(control, 1);
442 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800443 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else {
445 msix_enable(control);
446 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800447 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
450 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400451 pci_intx(dev, 0); /* disable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453}
454
Kristen Accardi4602b882005-08-16 15:15:58 -0700455void disable_msi_mode(struct pci_dev *dev, int pos, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 u16 control;
458
459 pci_read_config_word(dev, msi_control_reg(pos), &control);
460 if (type == PCI_CAP_ID_MSI) {
461 /* Set enabled bits to single MSI & enable MSI_enable bit */
462 msi_disable(control);
463 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800464 dev->msi_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 } else {
466 msix_disable(control);
467 pci_write_config_word(dev, msi_control_reg(pos), control);
Shaohua Li99dc8042006-05-26 10:58:27 +0800468 dev->msix_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
471 /* PCI Express Endpoint device detected */
Brett M Russa04ce0f2005-08-15 15:23:41 -0400472 pci_intx(dev, 1); /* enable intx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474}
475
476static int msi_lookup_vector(struct pci_dev *dev, int type)
477{
478 int vector;
479 unsigned long flags;
480
481 spin_lock_irqsave(&msi_lock, flags);
482 for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
483 if (!msi_desc[vector] || msi_desc[vector]->dev != dev ||
484 msi_desc[vector]->msi_attrib.type != type ||
485 msi_desc[vector]->msi_attrib.default_vector != dev->irq)
486 continue;
487 spin_unlock_irqrestore(&msi_lock, flags);
488 /* This pre-assigned MSI vector for this device
489 already exits. Override dev->irq with this vector */
490 dev->irq = vector;
491 return 0;
492 }
493 spin_unlock_irqrestore(&msi_lock, flags);
494
495 return -EACCES;
496}
497
498void pci_scan_msi_device(struct pci_dev *dev)
499{
500 if (!dev)
501 return;
502
503 if (pci_find_capability(dev, PCI_CAP_ID_MSIX) > 0)
504 nr_msix_devices++;
505 else if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0)
506 nr_reserved_vectors++;
507}
508
Shaohua Li41017f02006-02-08 17:11:38 +0800509#ifdef CONFIG_PM
510int pci_save_msi_state(struct pci_dev *dev)
511{
512 int pos, i = 0;
513 u16 control;
514 struct pci_cap_saved_state *save_state;
515 u32 *cap;
516
517 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
518 if (pos <= 0 || dev->no_msi)
519 return 0;
520
521 pci_read_config_word(dev, msi_control_reg(pos), &control);
522 if (!(control & PCI_MSI_FLAGS_ENABLE))
523 return 0;
524
525 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5,
526 GFP_KERNEL);
527 if (!save_state) {
528 printk(KERN_ERR "Out of memory in pci_save_msi_state\n");
529 return -ENOMEM;
530 }
531 cap = &save_state->data[0];
532
533 pci_read_config_dword(dev, pos, &cap[i++]);
534 control = cap[0] >> 16;
535 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]);
536 if (control & PCI_MSI_FLAGS_64BIT) {
537 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]);
538 pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]);
539 } else
540 pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]);
541 if (control & PCI_MSI_FLAGS_MASKBIT)
542 pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]);
Shaohua Li41017f02006-02-08 17:11:38 +0800543 save_state->cap_nr = PCI_CAP_ID_MSI;
544 pci_add_saved_cap(dev, save_state);
545 return 0;
546}
547
548void pci_restore_msi_state(struct pci_dev *dev)
549{
550 int i = 0, pos;
551 u16 control;
552 struct pci_cap_saved_state *save_state;
553 u32 *cap;
554
555 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI);
556 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
557 if (!save_state || pos <= 0)
558 return;
559 cap = &save_state->data[0];
560
561 control = cap[i++] >> 16;
562 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]);
563 if (control & PCI_MSI_FLAGS_64BIT) {
564 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]);
565 pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]);
566 } else
567 pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]);
568 if (control & PCI_MSI_FLAGS_MASKBIT)
569 pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]);
570 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
571 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
572 pci_remove_saved_cap(save_state);
573 kfree(save_state);
574}
575
576int pci_save_msix_state(struct pci_dev *dev)
577{
578 int pos;
Mark Maulefd58e552006-04-10 21:17:48 -0500579 int temp;
580 int vector, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800581 u16 control;
582 struct pci_cap_saved_state *save_state;
583
584 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
585 if (pos <= 0 || dev->no_msi)
586 return 0;
587
Mark Maulefd58e552006-04-10 21:17:48 -0500588 /* save the capability */
Shaohua Li41017f02006-02-08 17:11:38 +0800589 pci_read_config_word(dev, msi_control_reg(pos), &control);
590 if (!(control & PCI_MSIX_FLAGS_ENABLE))
591 return 0;
592 save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16),
593 GFP_KERNEL);
594 if (!save_state) {
595 printk(KERN_ERR "Out of memory in pci_save_msix_state\n");
596 return -ENOMEM;
597 }
598 *((u16 *)&save_state->data[0]) = control;
599
Mark Maulefd58e552006-04-10 21:17:48 -0500600 /* save the table */
601 temp = dev->irq;
602 if (msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
603 kfree(save_state);
604 return -EINVAL;
605 }
606
607 vector = head = dev->irq;
608 while (head != tail) {
609 int j;
610 void __iomem *base;
611 struct msi_desc *entry;
612
613 entry = msi_desc[vector];
614 base = entry->mask_base;
615 j = entry->msi_attrib.entry_nr;
616
617 entry->address_lo_save =
618 readl(base + j * PCI_MSIX_ENTRY_SIZE +
619 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
620 entry->address_hi_save =
621 readl(base + j * PCI_MSIX_ENTRY_SIZE +
622 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
623 entry->data_save =
624 readl(base + j * PCI_MSIX_ENTRY_SIZE +
625 PCI_MSIX_ENTRY_DATA_OFFSET);
626
627 tail = msi_desc[vector]->link.tail;
628 vector = tail;
629 }
630 dev->irq = temp;
631
Shaohua Li41017f02006-02-08 17:11:38 +0800632 save_state->cap_nr = PCI_CAP_ID_MSIX;
633 pci_add_saved_cap(dev, save_state);
634 return 0;
635}
636
637void pci_restore_msix_state(struct pci_dev *dev)
638{
639 u16 save;
640 int pos;
641 int vector, head, tail = 0;
642 void __iomem *base;
643 int j;
Shaohua Li41017f02006-02-08 17:11:38 +0800644 struct msi_desc *entry;
645 int temp;
646 struct pci_cap_saved_state *save_state;
647
648 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX);
649 if (!save_state)
650 return;
651 save = *((u16 *)&save_state->data[0]);
652 pci_remove_saved_cap(save_state);
653 kfree(save_state);
654
655 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
656 if (pos <= 0)
657 return;
658
659 /* route the table */
660 temp = dev->irq;
661 if (msi_lookup_vector(dev, PCI_CAP_ID_MSIX))
662 return;
663 vector = head = dev->irq;
664 while (head != tail) {
665 entry = msi_desc[vector];
666 base = entry->mask_base;
667 j = entry->msi_attrib.entry_nr;
668
Mark Maulefd58e552006-04-10 21:17:48 -0500669 writel(entry->address_lo_save,
Shaohua Li41017f02006-02-08 17:11:38 +0800670 base + j * PCI_MSIX_ENTRY_SIZE +
671 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
Mark Maulefd58e552006-04-10 21:17:48 -0500672 writel(entry->address_hi_save,
Shaohua Li41017f02006-02-08 17:11:38 +0800673 base + j * PCI_MSIX_ENTRY_SIZE +
674 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
Mark Maulefd58e552006-04-10 21:17:48 -0500675 writel(entry->data_save,
Shaohua Li41017f02006-02-08 17:11:38 +0800676 base + j * PCI_MSIX_ENTRY_SIZE +
677 PCI_MSIX_ENTRY_DATA_OFFSET);
678
679 tail = msi_desc[vector]->link.tail;
680 vector = tail;
681 }
682 dev->irq = temp;
683
684 pci_write_config_word(dev, msi_control_reg(pos), save);
685 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
686}
687#endif
688
Mark Maulefd58e552006-04-10 21:17:48 -0500689static int msi_register_init(struct pci_dev *dev, struct msi_desc *entry)
Shaohua Li41017f02006-02-08 17:11:38 +0800690{
Mark Maulefd58e552006-04-10 21:17:48 -0500691 int status;
692 u32 address_hi;
693 u32 address_lo;
694 u32 data;
Shaohua Li41017f02006-02-08 17:11:38 +0800695 int pos, vector = dev->irq;
696 u16 control;
697
698 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
699 pci_read_config_word(dev, msi_control_reg(pos), &control);
Mark Maulefd58e552006-04-10 21:17:48 -0500700
Shaohua Li41017f02006-02-08 17:11:38 +0800701 /* Configure MSI capability structure */
Mark Maulefd58e552006-04-10 21:17:48 -0500702 status = msi_ops->setup(dev, vector, &address_hi, &address_lo, &data);
703 if (status < 0)
704 return status;
705
706 pci_write_config_dword(dev, msi_lower_address_reg(pos), address_lo);
Shaohua Li41017f02006-02-08 17:11:38 +0800707 if (is_64bit_address(control)) {
708 pci_write_config_dword(dev,
Mark Maulefd58e552006-04-10 21:17:48 -0500709 msi_upper_address_reg(pos), address_hi);
Shaohua Li41017f02006-02-08 17:11:38 +0800710 pci_write_config_word(dev,
Mark Maulefd58e552006-04-10 21:17:48 -0500711 msi_data_reg(pos, 1), data);
Shaohua Li41017f02006-02-08 17:11:38 +0800712 } else
713 pci_write_config_word(dev,
Mark Maulefd58e552006-04-10 21:17:48 -0500714 msi_data_reg(pos, 0), data);
Shaohua Li41017f02006-02-08 17:11:38 +0800715 if (entry->msi_attrib.maskbit) {
716 unsigned int maskbits, temp;
717 /* All MSIs are unmasked by default, Mask them all */
718 pci_read_config_dword(dev,
719 msi_mask_bits_reg(pos, is_64bit_address(control)),
720 &maskbits);
721 temp = (1 << multi_msi_capable(control));
722 temp = ((temp - 1) & ~temp);
723 maskbits |= temp;
724 pci_write_config_dword(dev,
725 msi_mask_bits_reg(pos, is_64bit_address(control)),
726 maskbits);
727 }
Mark Maulefd58e552006-04-10 21:17:48 -0500728
729 return 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/**
733 * msi_capability_init - configure device's MSI capability structure
734 * @dev: pointer to the pci_dev data structure of MSI device function
735 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600736 * Setup the MSI capability structure of device function with a single
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * MSI vector, regardless of device function is capable of handling
738 * multiple messages. A return of zero indicates the successful setup
739 * of an entry zero with the new MSI vector or non-zero for otherwise.
740 **/
741static int msi_capability_init(struct pci_dev *dev)
742{
Mark Maulefd58e552006-04-10 21:17:48 -0500743 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct msi_desc *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int pos, vector;
746 u16 control;
747
748 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
749 pci_read_config_word(dev, msi_control_reg(pos), &control);
750 /* MSI Entry Initialization */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700751 entry = alloc_msi_entry();
752 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -ENOMEM;
754
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700755 vector = get_msi_vector(dev);
756 if (vector < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 kmem_cache_free(msi_cachep, entry);
758 return -EBUSY;
759 }
760 entry->link.head = vector;
761 entry->link.tail = vector;
762 entry->msi_attrib.type = PCI_CAP_ID_MSI;
763 entry->msi_attrib.state = 0; /* Mark it not active */
764 entry->msi_attrib.entry_nr = 0;
765 entry->msi_attrib.maskbit = is_mask_bit_support(control);
766 entry->msi_attrib.default_vector = dev->irq; /* Save IOAPIC IRQ */
767 dev->irq = vector;
768 entry->dev = dev;
769 if (is_mask_bit_support(control)) {
770 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
771 is_64bit_address(control));
772 }
773 /* Replace with MSI handler */
774 irq_handler_init(PCI_CAP_ID_MSI, vector, entry->msi_attrib.maskbit);
775 /* Configure MSI capability structure */
Mark Maulefd58e552006-04-10 21:17:48 -0500776 status = msi_register_init(dev, entry);
777 if (status != 0) {
778 dev->irq = entry->msi_attrib.default_vector;
779 kmem_cache_free(msi_cachep, entry);
780 return status;
781 }
Shaohua Li41017f02006-02-08 17:11:38 +0800782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 attach_msi_entry(entry, vector);
784 /* Set MSI enabled bits */
785 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
786
787 return 0;
788}
789
790/**
791 * msix_capability_init - configure device's MSI-X capability
792 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700793 * @entries: pointer to an array of struct msix_entry entries
794 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600796 * Setup the MSI-X capability structure of device function with a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * single MSI-X vector. A return of zero indicates the successful setup of
798 * requested MSI-X entries with allocated vectors or non-zero for otherwise.
799 **/
800static int msix_capability_init(struct pci_dev *dev,
801 struct msix_entry *entries, int nvec)
802{
803 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Mark Maulefd58e552006-04-10 21:17:48 -0500804 u32 address_hi;
805 u32 address_lo;
806 u32 data;
807 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int vector, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800809 unsigned long phys_addr;
810 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 u16 control;
812 u8 bir;
813 void __iomem *base;
814
815 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
816 /* Request & Map MSI-X table region */
817 pci_read_config_word(dev, msi_control_reg(pos), &control);
818 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800819
820 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800822 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
823 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
825 if (base == NULL)
826 return -ENOMEM;
827
828 /* MSI-X Table Initialization */
829 for (i = 0; i < nvec; i++) {
830 entry = alloc_msi_entry();
831 if (!entry)
832 break;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700833 vector = get_msi_vector(dev);
Jesper Juhlf01f4182006-04-17 04:02:54 +0200834 if (vector < 0) {
835 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
Jesper Juhlf01f4182006-04-17 04:02:54 +0200837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 j = entries[i].entry;
840 entries[i].vector = vector;
841 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
842 entry->msi_attrib.state = 0; /* Mark it not active */
843 entry->msi_attrib.entry_nr = j;
844 entry->msi_attrib.maskbit = 1;
845 entry->msi_attrib.default_vector = dev->irq;
846 entry->dev = dev;
847 entry->mask_base = base;
848 if (!head) {
849 entry->link.head = vector;
850 entry->link.tail = vector;
851 head = entry;
852 } else {
853 entry->link.head = temp;
854 entry->link.tail = tail->link.tail;
855 tail->link.tail = vector;
856 head->link.head = vector;
857 }
858 temp = vector;
859 tail = entry;
860 /* Replace with MSI-X handler */
861 irq_handler_init(PCI_CAP_ID_MSIX, vector, 1);
862 /* Configure MSI-X capability structure */
Mark Maulefd58e552006-04-10 21:17:48 -0500863 status = msi_ops->setup(dev, vector,
864 &address_hi,
865 &address_lo,
866 &data);
867 if (status < 0)
868 break;
869
870 writel(address_lo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 base + j * PCI_MSIX_ENTRY_SIZE +
872 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
Mark Maulefd58e552006-04-10 21:17:48 -0500873 writel(address_hi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 base + j * PCI_MSIX_ENTRY_SIZE +
875 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
Mark Maulefd58e552006-04-10 21:17:48 -0500876 writel(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 base + j * PCI_MSIX_ENTRY_SIZE +
878 PCI_MSIX_ENTRY_DATA_OFFSET);
879 attach_msi_entry(entry, vector);
880 }
881 if (i != nvec) {
882 i--;
883 for (; i >= 0; i--) {
884 vector = (entries + i)->vector;
885 msi_free_vector(dev, vector, 0);
886 (entries + i)->vector = 0;
887 }
888 return -EBUSY;
889 }
890 /* Set MSI-X enabled bits */
891 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
892
893 return 0;
894}
895
896/**
Brice Goglin24334a12006-08-31 01:55:07 -0400897 * pci_msi_supported - check whether MSI may be enabled on device
898 * @dev: pointer to the pci_dev data structure of MSI device function
899 *
900 * MSI must be globally enabled and supported by the device and its root
901 * bus. But, the root bus is not easy to find since some architectures
902 * have virtual busses on top of the PCI hierarchy (for instance the
903 * hypertransport bus), while the actual bus where MSI must be supported
904 * is below. So we test the MSI flag on all parent busses and assume
905 * that no quirk will ever set the NO_MSI flag on a non-root bus.
906 **/
907static
908int pci_msi_supported(struct pci_dev * dev)
909{
910 struct pci_bus *bus;
911
912 if (!pci_msi_enable || !dev || dev->no_msi)
913 return -EINVAL;
914
915 /* check MSI flags of all parent busses */
916 for (bus = dev->bus; bus; bus = bus->parent)
917 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
918 return -EINVAL;
919
920 return 0;
921}
922
923/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * pci_enable_msi - configure device's MSI capability structure
925 * @dev: pointer to the pci_dev data structure of MSI device function
926 *
927 * Setup the MSI capability structure of device function with
928 * a single MSI vector upon its software driver call to request for
929 * MSI mode enabled on its hardware device function. A return of zero
930 * indicates the successful setup of an entry zero with the new MSI
931 * vector or non-zero for otherwise.
932 **/
933int pci_enable_msi(struct pci_dev* dev)
934{
Brice Goglin24334a12006-08-31 01:55:07 -0400935 int pos, temp, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Brice Goglin24334a12006-08-31 01:55:07 -0400937 if (pci_msi_supported(dev) < 0)
938 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 temp = dev->irq;
941
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700942 status = msi_init();
943 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return status;
945
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700946 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
947 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700950 WARN_ON(!msi_lookup_vector(dev, PCI_CAP_ID_MSI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Check whether driver already requested for MSI-X vectors */
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700953 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
954 if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
956 "Device already has MSI-X vectors assigned\n",
957 pci_name(dev));
958 dev->irq = temp;
959 return -EINVAL;
960 }
961 status = msi_capability_init(dev);
962 if (!status) {
963 if (!pos)
964 nr_reserved_vectors--; /* Only MSI capable */
965 else if (nr_msix_devices > 0)
966 nr_msix_devices--; /* Both MSI and MSI-X capable,
967 but choose enabling MSI */
968 }
969
970 return status;
971}
972
973void pci_disable_msi(struct pci_dev* dev)
974{
975 struct msi_desc *entry;
976 int pos, default_vector;
977 u16 control;
978 unsigned long flags;
979
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700980 if (!pci_msi_enable)
981 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700982 if (!dev)
983 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700984
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700985 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
986 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return;
988
989 pci_read_config_word(dev, msi_control_reg(pos), &control);
990 if (!(control & PCI_MSI_FLAGS_ENABLE))
991 return;
992
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700993 disable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 spin_lock_irqsave(&msi_lock, flags);
996 entry = msi_desc[dev->irq];
997 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
998 spin_unlock_irqrestore(&msi_lock, flags);
999 return;
1000 }
1001 if (entry->msi_attrib.state) {
1002 spin_unlock_irqrestore(&msi_lock, flags);
1003 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
1004 "free_irq() on MSI vector %d\n",
1005 pci_name(dev), dev->irq);
1006 BUG_ON(entry->msi_attrib.state > 0);
1007 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 default_vector = entry->msi_attrib.default_vector;
1009 spin_unlock_irqrestore(&msi_lock, flags);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001010 msi_free_vector(dev, dev->irq, 0);
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* Restore dev->irq to its default pin-assertion vector */
1013 dev->irq = default_vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017static int msi_free_vector(struct pci_dev* dev, int vector, int reassign)
1018{
1019 struct msi_desc *entry;
1020 int head, entry_nr, type;
1021 void __iomem *base;
1022 unsigned long flags;
1023
Mark Maulefd58e552006-04-10 21:17:48 -05001024 msi_ops->teardown(vector);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_lock_irqsave(&msi_lock, flags);
1027 entry = msi_desc[vector];
1028 if (!entry || entry->dev != dev) {
1029 spin_unlock_irqrestore(&msi_lock, flags);
1030 return -EINVAL;
1031 }
1032 type = entry->msi_attrib.type;
1033 entry_nr = entry->msi_attrib.entry_nr;
1034 head = entry->link.head;
1035 base = entry->mask_base;
1036 msi_desc[entry->link.head]->link.tail = entry->link.tail;
1037 msi_desc[entry->link.tail]->link.head = entry->link.head;
1038 entry->dev = NULL;
1039 if (!reassign) {
1040 vector_irq[vector] = 0;
1041 nr_released_vectors++;
1042 }
1043 msi_desc[vector] = NULL;
1044 spin_unlock_irqrestore(&msi_lock, flags);
1045
1046 kmem_cache_free(msi_cachep, entry);
1047
1048 if (type == PCI_CAP_ID_MSIX) {
1049 if (!reassign)
1050 writel(1, base +
1051 entry_nr * PCI_MSIX_ENTRY_SIZE +
1052 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
1053
Grant Grundlerf7e66002006-05-31 23:35:47 -07001054 if (head == vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
1058 return 0;
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/**
1062 * pci_enable_msix - configure device's MSI-X capability structure
1063 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -07001064 * @entries: pointer to an array of MSI-X entries
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * @nvec: number of MSI-X vectors requested for allocation by device driver
1066 *
1067 * Setup the MSI-X capability structure of device function with the number
1068 * of requested vectors upon its software driver call to request for
1069 * MSI-X mode enabled on its hardware device function. A return of zero
1070 * indicates the successful configuration of MSI-X capability structure
1071 * with new allocated MSI-X vectors. A return of < 0 indicates a failure.
1072 * Or a return of > 0 indicates that driver request is exceeding the number
1073 * of vectors available. Driver should use the returned value to re-send
1074 * its request.
1075 **/
1076int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
1077{
1078 int status, pos, nr_entries, free_vectors;
1079 int i, j, temp;
1080 u16 control;
1081 unsigned long flags;
1082
Brice Goglin24334a12006-08-31 01:55:07 -04001083 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -EINVAL;
1085
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001086 status = msi_init();
1087 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return status;
1089
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001090 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1091 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EINVAL;
1093
1094 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 nr_entries = multi_msix_capable(control);
1096 if (nvec > nr_entries)
1097 return -EINVAL;
1098
1099 /* Check for any invalid entries */
1100 for (i = 0; i < nvec; i++) {
1101 if (entries[i].entry >= nr_entries)
1102 return -EINVAL; /* invalid entry */
1103 for (j = i + 1; j < nvec; j++) {
1104 if (entries[i].entry == entries[j].entry)
1105 return -EINVAL; /* duplicate entry */
1106 }
1107 }
1108 temp = dev->irq;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001109 WARN_ON(!msi_lookup_vector(dev, PCI_CAP_ID_MSIX));
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Check whether driver already requested for MSI vector */
1112 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
1113 !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
1114 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
1115 "Device already has an MSI vector assigned\n",
1116 pci_name(dev));
1117 dev->irq = temp;
1118 return -EINVAL;
1119 }
1120
1121 spin_lock_irqsave(&msi_lock, flags);
1122 /*
1123 * msi_lock is provided to ensure that enough vectors resources are
1124 * available before granting.
1125 */
1126 free_vectors = pci_vector_resources(last_alloc_vector,
1127 nr_released_vectors);
1128 /* Ensure that each MSI/MSI-X device has one vector reserved by
1129 default to avoid any MSI-X driver to take all available
1130 resources */
1131 free_vectors -= nr_reserved_vectors;
1132 /* Find the average of free vectors among MSI-X devices */
1133 if (nr_msix_devices > 0)
1134 free_vectors /= nr_msix_devices;
1135 spin_unlock_irqrestore(&msi_lock, flags);
1136
1137 if (nvec > free_vectors) {
1138 if (free_vectors > 0)
1139 return free_vectors;
1140 else
1141 return -EBUSY;
1142 }
1143
1144 status = msix_capability_init(dev, entries, nvec);
1145 if (!status && nr_msix_devices > 0)
1146 nr_msix_devices--;
1147
1148 return status;
1149}
1150
1151void pci_disable_msix(struct pci_dev* dev)
1152{
1153 int pos, temp;
1154 u16 control;
1155
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001156 if (!pci_msi_enable)
1157 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001158 if (!dev)
1159 return;
1160
1161 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1162 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return;
1164
1165 pci_read_config_word(dev, msi_control_reg(pos), &control);
1166 if (!(control & PCI_MSIX_FLAGS_ENABLE))
1167 return;
1168
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001169 disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 temp = dev->irq;
1172 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
1173 int state, vector, head, tail = 0, warning = 0;
1174 unsigned long flags;
1175
1176 vector = head = dev->irq;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001177 dev->irq = temp; /* Restore pin IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 while (head != tail) {
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001179 spin_lock_irqsave(&msi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 state = msi_desc[vector]->msi_attrib.state;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001181 tail = msi_desc[vector]->link.tail;
1182 spin_unlock_irqrestore(&msi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (state)
1184 warning = 1;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001185 else if (vector != head) /* Release MSI-X vector */
1186 msi_free_vector(dev, vector, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 vector = tail;
1188 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -07001189 msi_free_vector(dev, vector, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
1192 "free_irq() on all MSI-X vectors\n",
1193 pci_name(dev));
1194 BUG_ON(warning > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 }
1197}
1198
1199/**
1200 * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state
1201 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1202 *
Steven Coleeaae4b32005-05-03 18:38:30 -06001203 * Being called during hotplug remove, from which the device function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 * is hot-removed. All previous assigned MSI/MSI-X vectors, if
1205 * allocated for this device function, are reclaimed to unused state,
1206 * which may be used later on.
1207 **/
1208void msi_remove_pci_irq_vectors(struct pci_dev* dev)
1209{
1210 int state, pos, temp;
1211 unsigned long flags;
1212
1213 if (!pci_msi_enable || !dev)
1214 return;
1215
1216 temp = dev->irq; /* Save IOAPIC IRQ */
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001217 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
1218 if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 spin_lock_irqsave(&msi_lock, flags);
1220 state = msi_desc[dev->irq]->msi_attrib.state;
1221 spin_unlock_irqrestore(&msi_lock, flags);
1222 if (state) {
1223 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
1224 "called without free_irq() on MSI vector %d\n",
1225 pci_name(dev), dev->irq);
1226 BUG_ON(state > 0);
1227 } else /* Release MSI vector assigned to this device */
1228 msi_free_vector(dev, dev->irq, 0);
1229 dev->irq = temp; /* Restore IOAPIC IRQ */
1230 }
Grant Grundlerb64c05e2006-01-14 00:34:53 -07001231 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1232 if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int vector, head, tail = 0, warning = 0;
1234 void __iomem *base = NULL;
1235
1236 vector = head = dev->irq;
1237 while (head != tail) {
1238 spin_lock_irqsave(&msi_lock, flags);
1239 state = msi_desc[vector]->msi_attrib.state;
1240 tail = msi_desc[vector]->link.tail;
1241 base = msi_desc[vector]->mask_base;
1242 spin_unlock_irqrestore(&msi_lock, flags);
1243 if (state)
1244 warning = 1;
1245 else if (vector != head) /* Release MSI-X vector */
1246 msi_free_vector(dev, vector, 0);
1247 vector = tail;
1248 }
1249 msi_free_vector(dev, vector, 0);
1250 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 iounmap(base);
1252 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
1253 "called without free_irq() on all MSI-X vectors\n",
1254 pci_name(dev));
1255 BUG_ON(warning > 0);
1256 }
1257 dev->irq = temp; /* Restore IOAPIC IRQ */
1258 }
1259}
1260
Matthew Wilcox309e57d2006-03-05 22:33:34 -07001261void pci_no_msi(void)
1262{
1263 pci_msi_enable = 0;
1264}
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266EXPORT_SYMBOL(pci_enable_msi);
1267EXPORT_SYMBOL(pci_disable_msi);
1268EXPORT_SYMBOL(pci_enable_msix);
1269EXPORT_SYMBOL(pci_disable_msix);