3 * Purpose: PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
10 #include <linux/irq.h>
11 #include <linux/interrupt.h>
12 #include <linux/init.h>
13 #include <linux/config.h>
14 #include <linux/ioport.h>
15 #include <linux/smp_lock.h>
16 #include <linux/pci.h>
17 #include <linux/proc_fs.h>
19 #include <asm/errno.h>
25 static DEFINE_SPINLOCK(msi_lock);
26 static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL };
27 static kmem_cache_t* msi_cachep;
29 static int pci_msi_enable = 1;
30 static int last_alloc_vector = 0;
31 static int nr_released_vectors = 0;
32 static int nr_reserved_vectors = NR_HP_RESERVED_VECTORS;
33 static int nr_msix_devices = 0;
35 #ifndef CONFIG_X86_IO_APIC
36 int vector_irq[NR_VECTORS] = { [0 ... NR_VECTORS - 1] = -1};
37 u8 irq_vector[NR_IRQ_VECTORS] = { FIRST_DEVICE_VECTOR , 0 };
40 static void msi_cache_ctor(void *p, kmem_cache_t *cache, unsigned long flags)
42 memset(p, 0, NR_IRQS * sizeof(struct msi_desc));
45 static int msi_cache_init(void)
47 msi_cachep = kmem_cache_create("msi_cache",
48 NR_IRQS * sizeof(struct msi_desc),
49 0, SLAB_HWCACHE_ALIGN, msi_cache_ctor, NULL);
56 static void msi_set_mask_bit(unsigned int vector, int flag)
58 struct msi_desc *entry;
60 entry = (struct msi_desc *)msi_desc[vector];
61 if (!entry || !entry->dev || !entry->mask_base)
63 switch (entry->msi_attrib.type) {
69 pos = (int)entry->mask_base;
70 pci_read_config_dword(entry->dev, pos, &mask_bits);
73 pci_write_config_dword(entry->dev, pos, mask_bits);
78 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
79 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
80 writel(flag, entry->mask_base + offset);
89 static void set_msi_affinity(unsigned int vector, cpumask_t cpu_mask)
91 struct msi_desc *entry;
92 struct msg_address address;
94 entry = (struct msi_desc *)msi_desc[vector];
95 if (!entry || !entry->dev)
98 switch (entry->msi_attrib.type) {
103 if (!(pos = pci_find_capability(entry->dev, PCI_CAP_ID_MSI)))
106 pci_read_config_dword(entry->dev, msi_lower_address_reg(pos),
107 &address.lo_address.value);
108 address.lo_address.value &= MSI_ADDRESS_DEST_ID_MASK;
109 address.lo_address.value |= (cpu_mask_to_apicid(cpu_mask) <<
110 MSI_TARGET_CPU_SHIFT);
111 entry->msi_attrib.current_cpu = cpu_mask_to_apicid(cpu_mask);
112 pci_write_config_dword(entry->dev, msi_lower_address_reg(pos),
113 address.lo_address.value);
116 case PCI_CAP_ID_MSIX:
118 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
119 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET;
121 address.lo_address.value = readl(entry->mask_base + offset);
122 address.lo_address.value &= MSI_ADDRESS_DEST_ID_MASK;
123 address.lo_address.value |= (cpu_mask_to_apicid(cpu_mask) <<
124 MSI_TARGET_CPU_SHIFT);
125 entry->msi_attrib.current_cpu = cpu_mask_to_apicid(cpu_mask);
126 writel(address.lo_address.value, entry->mask_base + offset);
134 #ifdef CONFIG_IRQBALANCE
135 static inline void move_msi(int vector)
137 if (!cpus_empty(pending_irq_balance_cpumask[vector])) {
138 set_msi_affinity(vector, pending_irq_balance_cpumask[vector]);
139 cpus_clear(pending_irq_balance_cpumask[vector]);
142 #endif /* CONFIG_IRQBALANCE */
143 #endif /* CONFIG_SMP */
145 static void mask_MSI_irq(unsigned int vector)
147 msi_set_mask_bit(vector, 1);
150 static void unmask_MSI_irq(unsigned int vector)
152 msi_set_mask_bit(vector, 0);
155 static unsigned int startup_msi_irq_wo_maskbit(unsigned int vector)
157 struct msi_desc *entry;
160 spin_lock_irqsave(&msi_lock, flags);
161 entry = msi_desc[vector];
162 if (!entry || !entry->dev) {
163 spin_unlock_irqrestore(&msi_lock, flags);
166 entry->msi_attrib.state = 1; /* Mark it active */
167 spin_unlock_irqrestore(&msi_lock, flags);
169 return 0; /* never anything pending */
172 static void release_msi(unsigned int vector);
173 static void shutdown_msi_irq(unsigned int vector)
178 #define shutdown_msi_irq_wo_maskbit shutdown_msi_irq
179 static void enable_msi_irq_wo_maskbit(unsigned int vector) {}
180 static void disable_msi_irq_wo_maskbit(unsigned int vector) {}
181 static void ack_msi_irq_wo_maskbit(unsigned int vector) {}
182 static void end_msi_irq_wo_maskbit(unsigned int vector)
188 static unsigned int startup_msi_irq_w_maskbit(unsigned int vector)
190 struct msi_desc *entry;
193 spin_lock_irqsave(&msi_lock, flags);
194 entry = msi_desc[vector];
195 if (!entry || !entry->dev) {
196 spin_unlock_irqrestore(&msi_lock, flags);
199 entry->msi_attrib.state = 1; /* Mark it active */
200 spin_unlock_irqrestore(&msi_lock, flags);
202 unmask_MSI_irq(vector);
203 return 0; /* never anything pending */
206 #define shutdown_msi_irq_w_maskbit shutdown_msi_irq
207 #define enable_msi_irq_w_maskbit unmask_MSI_irq
208 #define disable_msi_irq_w_maskbit mask_MSI_irq
209 #define ack_msi_irq_w_maskbit mask_MSI_irq
211 static void end_msi_irq_w_maskbit(unsigned int vector)
214 unmask_MSI_irq(vector);
219 * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
220 * which implement the MSI-X Capability Structure.
222 static struct hw_interrupt_type msix_irq_type = {
223 .typename = "PCI-MSI-X",
224 .startup = startup_msi_irq_w_maskbit,
225 .shutdown = shutdown_msi_irq_w_maskbit,
226 .enable = enable_msi_irq_w_maskbit,
227 .disable = disable_msi_irq_w_maskbit,
228 .ack = ack_msi_irq_w_maskbit,
229 .end = end_msi_irq_w_maskbit,
230 .set_affinity = set_msi_irq_affinity
234 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
235 * which implement the MSI Capability Structure with
236 * Mask-and-Pending Bits.
238 static struct hw_interrupt_type msi_irq_w_maskbit_type = {
239 .typename = "PCI-MSI",
240 .startup = startup_msi_irq_w_maskbit,
241 .shutdown = shutdown_msi_irq_w_maskbit,
242 .enable = enable_msi_irq_w_maskbit,
243 .disable = disable_msi_irq_w_maskbit,
244 .ack = ack_msi_irq_w_maskbit,
245 .end = end_msi_irq_w_maskbit,
246 .set_affinity = set_msi_irq_affinity
250 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
251 * which implement the MSI Capability Structure without
252 * Mask-and-Pending Bits.
254 static struct hw_interrupt_type msi_irq_wo_maskbit_type = {
255 .typename = "PCI-MSI",
256 .startup = startup_msi_irq_wo_maskbit,
257 .shutdown = shutdown_msi_irq_wo_maskbit,
258 .enable = enable_msi_irq_wo_maskbit,
259 .disable = disable_msi_irq_wo_maskbit,
260 .ack = ack_msi_irq_wo_maskbit,
261 .end = end_msi_irq_wo_maskbit,
262 .set_affinity = set_msi_irq_affinity
265 static void msi_data_init(struct msg_data *msi_data,
268 memset(msi_data, 0, sizeof(struct msg_data));
269 msi_data->vector = (u8)vector;
270 msi_data->delivery_mode = MSI_DELIVERY_MODE;
271 msi_data->level = MSI_LEVEL_MODE;
272 msi_data->trigger = MSI_TRIGGER_MODE;
275 static void msi_address_init(struct msg_address *msi_address)
277 unsigned int dest_id;
279 memset(msi_address, 0, sizeof(struct msg_address));
280 msi_address->hi_address = (u32)0;
281 dest_id = (MSI_ADDRESS_HEADER << MSI_ADDRESS_HEADER_SHIFT);
282 msi_address->lo_address.u.dest_mode = MSI_DEST_MODE;
283 msi_address->lo_address.u.redirection_hint = MSI_REDIRECTION_HINT_MODE;
284 msi_address->lo_address.u.dest_id = dest_id;
285 msi_address->lo_address.value |= (MSI_TARGET_CPU << MSI_TARGET_CPU_SHIFT);
288 static int msi_free_vector(struct pci_dev* dev, int vector, int reassign);
289 static int assign_msi_vector(void)
291 static int new_vector_avail = 1;
296 * msi_lock is provided to ensure that successful allocation of MSI
297 * vector is assigned unique among drivers.
299 spin_lock_irqsave(&msi_lock, flags);
301 if (!new_vector_avail) {
305 * vector_irq[] = -1 indicates that this specific vector is:
306 * - assigned for MSI (since MSI have no associated IRQ) or
307 * - assigned for legacy if less than 16, or
308 * - having no corresponding 1:1 vector-to-IOxAPIC IRQ mapping
309 * vector_irq[] = 0 indicates that this vector, previously
310 * assigned for MSI, is freed by hotplug removed operations.
311 * This vector will be reused for any subsequent hotplug added
313 * vector_irq[] > 0 indicates that this vector is assigned for
314 * IOxAPIC IRQs. This vector and its value provides a 1-to-1
315 * vector-to-IOxAPIC IRQ mapping.
317 for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
318 if (vector_irq[vector] != 0)
320 free_vector = vector;
321 if (!msi_desc[vector])
327 spin_unlock_irqrestore(&msi_lock, flags);
330 vector_irq[free_vector] = -1;
331 nr_released_vectors--;
332 spin_unlock_irqrestore(&msi_lock, flags);
333 if (msi_desc[free_vector] != NULL) {
337 /* free all linked vectors before re-assign */
339 spin_lock_irqsave(&msi_lock, flags);
340 dev = msi_desc[free_vector]->dev;
341 tail = msi_desc[free_vector]->link.tail;
342 spin_unlock_irqrestore(&msi_lock, flags);
343 msi_free_vector(dev, tail, 1);
344 } while (free_vector != tail);
349 vector = assign_irq_vector(AUTO_ASSIGN);
350 last_alloc_vector = vector;
351 if (vector == LAST_DEVICE_VECTOR)
352 new_vector_avail = 0;
354 spin_unlock_irqrestore(&msi_lock, flags);
358 static int get_new_vector(void)
362 if ((vector = assign_msi_vector()) > 0)
363 set_intr_gate(vector, interrupt[vector]);
368 static int msi_init(void)
370 static int status = -ENOMEM;
375 if ((status = msi_cache_init()) < 0) {
377 printk(KERN_WARNING "PCI: MSI cache init failed\n");
380 last_alloc_vector = assign_irq_vector(AUTO_ASSIGN);
381 if (last_alloc_vector < 0) {
383 printk(KERN_WARNING "PCI: No interrupt vectors available for MSI\n");
387 vector_irq[last_alloc_vector] = 0;
388 nr_released_vectors++;
393 static int get_msi_vector(struct pci_dev *dev)
395 return get_new_vector();
398 static struct msi_desc* alloc_msi_entry(void)
400 struct msi_desc *entry;
402 entry = (struct msi_desc*) kmem_cache_alloc(msi_cachep, SLAB_KERNEL);
406 memset(entry, 0, sizeof(struct msi_desc));
407 entry->link.tail = entry->link.head = 0; /* single message */
413 static void attach_msi_entry(struct msi_desc *entry, int vector)
417 spin_lock_irqsave(&msi_lock, flags);
418 msi_desc[vector] = entry;
419 spin_unlock_irqrestore(&msi_lock, flags);
422 static void irq_handler_init(int cap_id, int pos, int mask)
424 spin_lock(&irq_desc[pos].lock);
425 if (cap_id == PCI_CAP_ID_MSIX)
426 irq_desc[pos].handler = &msix_irq_type;
429 irq_desc[pos].handler = &msi_irq_wo_maskbit_type;
431 irq_desc[pos].handler = &msi_irq_w_maskbit_type;
433 spin_unlock(&irq_desc[pos].lock);
436 static void enable_msi_mode(struct pci_dev *dev, int pos, int type)
440 pci_read_config_word(dev, msi_control_reg(pos), &control);
441 if (type == PCI_CAP_ID_MSI) {
442 /* Set enabled bits to single MSI & enable MSI_enable bit */
443 msi_enable(control, 1);
444 pci_write_config_word(dev, msi_control_reg(pos), control);
446 msix_enable(control);
447 pci_write_config_word(dev, msi_control_reg(pos), control);
449 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
450 /* PCI Express Endpoint device detected */
452 pci_read_config_word(dev, PCI_COMMAND, &cmd);
453 cmd |= PCI_COMMAND_INTX_DISABLE;
454 pci_write_config_word(dev, PCI_COMMAND, cmd);
458 static void disable_msi_mode(struct pci_dev *dev, int pos, int type)
462 pci_read_config_word(dev, msi_control_reg(pos), &control);
463 if (type == PCI_CAP_ID_MSI) {
464 /* Set enabled bits to single MSI & enable MSI_enable bit */
465 msi_disable(control);
466 pci_write_config_word(dev, msi_control_reg(pos), control);
468 msix_disable(control);
469 pci_write_config_word(dev, msi_control_reg(pos), control);
471 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
472 /* PCI Express Endpoint device detected */
474 pci_read_config_word(dev, PCI_COMMAND, &cmd);
475 cmd &= ~PCI_COMMAND_INTX_DISABLE;
476 pci_write_config_word(dev, PCI_COMMAND, cmd);
480 static int msi_lookup_vector(struct pci_dev *dev, int type)
485 spin_lock_irqsave(&msi_lock, flags);
486 for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
487 if (!msi_desc[vector] || msi_desc[vector]->dev != dev ||
488 msi_desc[vector]->msi_attrib.type != type ||
489 msi_desc[vector]->msi_attrib.default_vector != dev->irq)
491 spin_unlock_irqrestore(&msi_lock, flags);
492 /* This pre-assigned MSI vector for this device
493 already exits. Override dev->irq with this vector */
497 spin_unlock_irqrestore(&msi_lock, flags);
502 void pci_scan_msi_device(struct pci_dev *dev)
507 if (pci_find_capability(dev, PCI_CAP_ID_MSIX) > 0)
509 else if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0)
510 nr_reserved_vectors++;
514 * msi_capability_init - configure device's MSI capability structure
515 * @dev: pointer to the pci_dev data structure of MSI device function
517 * Setup the MSI capability structure of device funtion with a single
518 * MSI vector, regardless of device function is capable of handling
519 * multiple messages. A return of zero indicates the successful setup
520 * of an entry zero with the new MSI vector or non-zero for otherwise.
522 static int msi_capability_init(struct pci_dev *dev)
524 struct msi_desc *entry;
525 struct msg_address address;
526 struct msg_data data;
530 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
531 pci_read_config_word(dev, msi_control_reg(pos), &control);
532 /* MSI Entry Initialization */
533 if (!(entry = alloc_msi_entry()))
536 if ((vector = get_msi_vector(dev)) < 0) {
537 kmem_cache_free(msi_cachep, entry);
540 entry->link.head = vector;
541 entry->link.tail = vector;
542 entry->msi_attrib.type = PCI_CAP_ID_MSI;
543 entry->msi_attrib.state = 0; /* Mark it not active */
544 entry->msi_attrib.entry_nr = 0;
545 entry->msi_attrib.maskbit = is_mask_bit_support(control);
546 entry->msi_attrib.default_vector = dev->irq; /* Save IOAPIC IRQ */
549 if (is_mask_bit_support(control)) {
550 entry->mask_base = (void __iomem *)msi_mask_bits_reg(pos,
551 is_64bit_address(control));
553 /* Replace with MSI handler */
554 irq_handler_init(PCI_CAP_ID_MSI, vector, entry->msi_attrib.maskbit);
555 /* Configure MSI capability structure */
556 msi_address_init(&address);
557 msi_data_init(&data, vector);
558 entry->msi_attrib.current_cpu = ((address.lo_address.u.dest_id >>
559 MSI_TARGET_CPU_SHIFT) & MSI_TARGET_CPU_MASK);
560 pci_write_config_dword(dev, msi_lower_address_reg(pos),
561 address.lo_address.value);
562 if (is_64bit_address(control)) {
563 pci_write_config_dword(dev,
564 msi_upper_address_reg(pos), address.hi_address);
565 pci_write_config_word(dev,
566 msi_data_reg(pos, 1), *((u32*)&data));
568 pci_write_config_word(dev,
569 msi_data_reg(pos, 0), *((u32*)&data));
570 if (entry->msi_attrib.maskbit) {
571 unsigned int maskbits, temp;
572 /* All MSIs are unmasked by default, Mask them all */
573 pci_read_config_dword(dev,
574 msi_mask_bits_reg(pos, is_64bit_address(control)),
576 temp = (1 << multi_msi_capable(control));
577 temp = ((temp - 1) & ~temp);
579 pci_write_config_dword(dev,
580 msi_mask_bits_reg(pos, is_64bit_address(control)),
583 attach_msi_entry(entry, vector);
584 /* Set MSI enabled bits */
585 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
591 * msix_capability_init - configure device's MSI-X capability
592 * @dev: pointer to the pci_dev data structure of MSI-X device function
594 * Setup the MSI-X capability structure of device funtion with a
595 * single MSI-X vector. A return of zero indicates the successful setup of
596 * requested MSI-X entries with allocated vectors or non-zero for otherwise.
598 static int msix_capability_init(struct pci_dev *dev,
599 struct msix_entry *entries, int nvec)
601 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
602 struct msg_address address;
603 struct msg_data data;
604 int vector, pos, i, j, nr_entries, temp = 0;
605 u32 phys_addr, table_offset;
610 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
611 /* Request & Map MSI-X table region */
612 pci_read_config_word(dev, msi_control_reg(pos), &control);
613 nr_entries = multi_msix_capable(control);
614 pci_read_config_dword(dev, msix_table_offset_reg(pos),
616 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
617 phys_addr = pci_resource_start (dev, bir);
618 phys_addr += (u32)(table_offset & ~PCI_MSIX_FLAGS_BIRMASK);
619 if (!request_mem_region(phys_addr,
620 nr_entries * PCI_MSIX_ENTRY_SIZE,
621 "MSI-X vector table"))
623 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
625 release_mem_region(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
628 /* MSI-X Table Initialization */
629 for (i = 0; i < nvec; i++) {
630 entry = alloc_msi_entry();
633 if ((vector = get_msi_vector(dev)) < 0)
636 j = entries[i].entry;
637 entries[i].vector = vector;
638 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
639 entry->msi_attrib.state = 0; /* Mark it not active */
640 entry->msi_attrib.entry_nr = j;
641 entry->msi_attrib.maskbit = 1;
642 entry->msi_attrib.default_vector = dev->irq;
644 entry->mask_base = base;
646 entry->link.head = vector;
647 entry->link.tail = vector;
650 entry->link.head = temp;
651 entry->link.tail = tail->link.tail;
652 tail->link.tail = vector;
653 head->link.head = vector;
657 /* Replace with MSI-X handler */
658 irq_handler_init(PCI_CAP_ID_MSIX, vector, 1);
659 /* Configure MSI-X capability structure */
660 msi_address_init(&address);
661 msi_data_init(&data, vector);
662 entry->msi_attrib.current_cpu =
663 ((address.lo_address.u.dest_id >>
664 MSI_TARGET_CPU_SHIFT) & MSI_TARGET_CPU_MASK);
665 writel(address.lo_address.value,
666 base + j * PCI_MSIX_ENTRY_SIZE +
667 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
668 writel(address.hi_address,
669 base + j * PCI_MSIX_ENTRY_SIZE +
670 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
672 base + j * PCI_MSIX_ENTRY_SIZE +
673 PCI_MSIX_ENTRY_DATA_OFFSET);
674 attach_msi_entry(entry, vector);
678 for (; i >= 0; i--) {
679 vector = (entries + i)->vector;
680 msi_free_vector(dev, vector, 0);
681 (entries + i)->vector = 0;
685 /* Set MSI-X enabled bits */
686 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
692 * pci_enable_msi - configure device's MSI capability structure
693 * @dev: pointer to the pci_dev data structure of MSI device function
695 * Setup the MSI capability structure of device function with
696 * a single MSI vector upon its software driver call to request for
697 * MSI mode enabled on its hardware device function. A return of zero
698 * indicates the successful setup of an entry zero with the new MSI
699 * vector or non-zero for otherwise.
701 int pci_enable_msi(struct pci_dev* dev)
703 int pos, temp = dev->irq, status = -EINVAL;
706 if (!pci_msi_enable || !dev)
709 if ((status = msi_init()) < 0)
712 if (!(pos = pci_find_capability(dev, PCI_CAP_ID_MSI)))
715 pci_read_config_word(dev, msi_control_reg(pos), &control);
716 if (control & PCI_MSI_FLAGS_ENABLE)
717 return 0; /* Already in MSI mode */
719 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
723 spin_lock_irqsave(&msi_lock, flags);
724 if (!vector_irq[dev->irq]) {
725 msi_desc[dev->irq]->msi_attrib.state = 0;
726 vector_irq[dev->irq] = -1;
727 nr_released_vectors--;
728 spin_unlock_irqrestore(&msi_lock, flags);
729 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
732 spin_unlock_irqrestore(&msi_lock, flags);
735 /* Check whether driver already requested for MSI-X vectors */
736 if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)) > 0 &&
737 !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
738 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
739 "Device already has MSI-X vectors assigned\n",
744 status = msi_capability_init(dev);
747 nr_reserved_vectors--; /* Only MSI capable */
748 else if (nr_msix_devices > 0)
749 nr_msix_devices--; /* Both MSI and MSI-X capable,
750 but choose enabling MSI */
756 void pci_disable_msi(struct pci_dev* dev)
758 struct msi_desc *entry;
759 int pos, default_vector;
763 if (!dev || !(pos = pci_find_capability(dev, PCI_CAP_ID_MSI)))
766 pci_read_config_word(dev, msi_control_reg(pos), &control);
767 if (!(control & PCI_MSI_FLAGS_ENABLE))
770 spin_lock_irqsave(&msi_lock, flags);
771 entry = msi_desc[dev->irq];
772 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
773 spin_unlock_irqrestore(&msi_lock, flags);
776 if (entry->msi_attrib.state) {
777 spin_unlock_irqrestore(&msi_lock, flags);
778 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
779 "free_irq() on MSI vector %d\n",
780 pci_name(dev), dev->irq);
781 BUG_ON(entry->msi_attrib.state > 0);
783 vector_irq[dev->irq] = 0; /* free it */
784 nr_released_vectors++;
785 default_vector = entry->msi_attrib.default_vector;
786 spin_unlock_irqrestore(&msi_lock, flags);
787 /* Restore dev->irq to its default pin-assertion vector */
788 dev->irq = default_vector;
789 disable_msi_mode(dev, pci_find_capability(dev, PCI_CAP_ID_MSI),
794 static void release_msi(unsigned int vector)
796 struct msi_desc *entry;
799 spin_lock_irqsave(&msi_lock, flags);
800 entry = msi_desc[vector];
801 if (entry && entry->dev)
802 entry->msi_attrib.state = 0; /* Mark it not active */
803 spin_unlock_irqrestore(&msi_lock, flags);
806 static int msi_free_vector(struct pci_dev* dev, int vector, int reassign)
808 struct msi_desc *entry;
809 int head, entry_nr, type;
813 spin_lock_irqsave(&msi_lock, flags);
814 entry = msi_desc[vector];
815 if (!entry || entry->dev != dev) {
816 spin_unlock_irqrestore(&msi_lock, flags);
819 type = entry->msi_attrib.type;
820 entry_nr = entry->msi_attrib.entry_nr;
821 head = entry->link.head;
822 base = entry->mask_base;
823 msi_desc[entry->link.head]->link.tail = entry->link.tail;
824 msi_desc[entry->link.tail]->link.head = entry->link.head;
827 vector_irq[vector] = 0;
828 nr_released_vectors++;
830 msi_desc[vector] = NULL;
831 spin_unlock_irqrestore(&msi_lock, flags);
833 kmem_cache_free(msi_cachep, entry);
835 if (type == PCI_CAP_ID_MSIX) {
838 entry_nr * PCI_MSIX_ENTRY_SIZE +
839 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
841 if (head == vector) {
843 * Detect last MSI-X vector to be released.
844 * Release the MSI-X memory-mapped table.
847 u32 phys_addr, table_offset;
851 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
852 pci_read_config_word(dev, msi_control_reg(pos),
854 nr_entries = multi_msix_capable(control);
855 pci_read_config_dword(dev, msix_table_offset_reg(pos),
857 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
858 phys_addr = pci_resource_start (dev, bir);
859 phys_addr += (u32)(table_offset &
860 ~PCI_MSIX_FLAGS_BIRMASK);
862 release_mem_region(phys_addr,
863 nr_entries * PCI_MSIX_ENTRY_SIZE);
870 static int reroute_msix_table(int head, struct msix_entry *entries, int *nvec)
872 int vector = head, tail = 0;
873 int i, j = 0, nr_entries = 0;
877 spin_lock_irqsave(&msi_lock, flags);
878 while (head != tail) {
880 tail = msi_desc[vector]->link.tail;
881 if (entries[0].entry == msi_desc[vector]->msi_attrib.entry_nr)
885 if (*nvec > nr_entries) {
886 spin_unlock_irqrestore(&msi_lock, flags);
890 vector = ((j > 0) ? j : head);
891 for (i = 0; i < *nvec; i++) {
892 j = msi_desc[vector]->msi_attrib.entry_nr;
893 msi_desc[vector]->msi_attrib.state = 0; /* Mark it not active */
894 vector_irq[vector] = -1; /* Mark it busy */
895 nr_released_vectors--;
896 entries[i].vector = vector;
897 if (j != (entries + i)->entry) {
898 base = msi_desc[vector]->mask_base;
899 msi_desc[vector]->msi_attrib.entry_nr =
900 (entries + i)->entry;
901 writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
902 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET), base +
903 (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
904 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
905 writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
906 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET), base +
907 (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
908 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
909 writel( (readl(base + j * PCI_MSIX_ENTRY_SIZE +
910 PCI_MSIX_ENTRY_DATA_OFFSET) & 0xff00) | vector,
911 base + (entries+i)->entry*PCI_MSIX_ENTRY_SIZE +
912 PCI_MSIX_ENTRY_DATA_OFFSET);
914 vector = msi_desc[vector]->link.tail;
916 spin_unlock_irqrestore(&msi_lock, flags);
922 * pci_enable_msix - configure device's MSI-X capability structure
923 * @dev: pointer to the pci_dev data structure of MSI-X device function
924 * @data: pointer to an array of MSI-X entries
925 * @nvec: number of MSI-X vectors requested for allocation by device driver
927 * Setup the MSI-X capability structure of device function with the number
928 * of requested vectors upon its software driver call to request for
929 * MSI-X mode enabled on its hardware device function. A return of zero
930 * indicates the successful configuration of MSI-X capability structure
931 * with new allocated MSI-X vectors. A return of < 0 indicates a failure.
932 * Or a return of > 0 indicates that driver request is exceeding the number
933 * of vectors available. Driver should use the returned value to re-send
936 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
938 int status, pos, nr_entries, free_vectors;
943 if (!pci_msi_enable || !dev || !entries)
946 if ((status = msi_init()) < 0)
949 if (!(pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)))
952 pci_read_config_word(dev, msi_control_reg(pos), &control);
953 if (control & PCI_MSIX_FLAGS_ENABLE)
954 return -EINVAL; /* Already in MSI-X mode */
956 nr_entries = multi_msix_capable(control);
957 if (nvec > nr_entries)
960 /* Check for any invalid entries */
961 for (i = 0; i < nvec; i++) {
962 if (entries[i].entry >= nr_entries)
963 return -EINVAL; /* invalid entry */
964 for (j = i + 1; j < nvec; j++) {
965 if (entries[i].entry == entries[j].entry)
966 return -EINVAL; /* duplicate entry */
970 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
973 /* Reroute MSI-X table */
974 if (reroute_msix_table(dev->irq, entries, &nr_entries)) {
975 /* #requested > #previous-assigned */
980 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
983 /* Check whether driver already requested for MSI vector */
984 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
985 !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
986 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
987 "Device already has an MSI vector assigned\n",
993 spin_lock_irqsave(&msi_lock, flags);
995 * msi_lock is provided to ensure that enough vectors resources are
996 * available before granting.
998 free_vectors = pci_vector_resources(last_alloc_vector,
999 nr_released_vectors);
1000 /* Ensure that each MSI/MSI-X device has one vector reserved by
1001 default to avoid any MSI-X driver to take all available
1003 free_vectors -= nr_reserved_vectors;
1004 /* Find the average of free vectors among MSI-X devices */
1005 if (nr_msix_devices > 0)
1006 free_vectors /= nr_msix_devices;
1007 spin_unlock_irqrestore(&msi_lock, flags);
1009 if (nvec > free_vectors) {
1010 if (free_vectors > 0)
1011 return free_vectors;
1016 status = msix_capability_init(dev, entries, nvec);
1017 if (!status && nr_msix_devices > 0)
1023 void pci_disable_msix(struct pci_dev* dev)
1028 if (!dev || !(pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)))
1031 pci_read_config_word(dev, msi_control_reg(pos), &control);
1032 if (!(control & PCI_MSIX_FLAGS_ENABLE))
1036 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
1037 int state, vector, head, tail = 0, warning = 0;
1038 unsigned long flags;
1040 vector = head = dev->irq;
1041 spin_lock_irqsave(&msi_lock, flags);
1042 while (head != tail) {
1043 state = msi_desc[vector]->msi_attrib.state;
1047 vector_irq[vector] = 0; /* free it */
1048 nr_released_vectors++;
1050 tail = msi_desc[vector]->link.tail;
1053 spin_unlock_irqrestore(&msi_lock, flags);
1056 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
1057 "free_irq() on all MSI-X vectors\n",
1059 BUG_ON(warning > 0);
1062 disable_msi_mode(dev,
1063 pci_find_capability(dev, PCI_CAP_ID_MSIX),
1071 * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state
1072 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1074 * Being called during hotplug remove, from which the device funciton
1075 * is hot-removed. All previous assigned MSI/MSI-X vectors, if
1076 * allocated for this device function, are reclaimed to unused state,
1077 * which may be used later on.
1079 void msi_remove_pci_irq_vectors(struct pci_dev* dev)
1081 int state, pos, temp;
1082 unsigned long flags;
1084 if (!pci_msi_enable || !dev)
1087 temp = dev->irq; /* Save IOAPIC IRQ */
1088 if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) > 0 &&
1089 !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
1090 spin_lock_irqsave(&msi_lock, flags);
1091 state = msi_desc[dev->irq]->msi_attrib.state;
1092 spin_unlock_irqrestore(&msi_lock, flags);
1094 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
1095 "called without free_irq() on MSI vector %d\n",
1096 pci_name(dev), dev->irq);
1098 } else /* Release MSI vector assigned to this device */
1099 msi_free_vector(dev, dev->irq, 0);
1100 dev->irq = temp; /* Restore IOAPIC IRQ */
1102 if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)) > 0 &&
1103 !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
1104 int vector, head, tail = 0, warning = 0;
1105 void __iomem *base = NULL;
1107 vector = head = dev->irq;
1108 while (head != tail) {
1109 spin_lock_irqsave(&msi_lock, flags);
1110 state = msi_desc[vector]->msi_attrib.state;
1111 tail = msi_desc[vector]->link.tail;
1112 base = msi_desc[vector]->mask_base;
1113 spin_unlock_irqrestore(&msi_lock, flags);
1116 else if (vector != head) /* Release MSI-X vector */
1117 msi_free_vector(dev, vector, 0);
1120 msi_free_vector(dev, vector, 0);
1122 /* Force to release the MSI-X memory-mapped table */
1123 u32 phys_addr, table_offset;
1127 pci_read_config_word(dev, msi_control_reg(pos),
1129 pci_read_config_dword(dev, msix_table_offset_reg(pos),
1131 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
1132 phys_addr = pci_resource_start (dev, bir);
1133 phys_addr += (u32)(table_offset &
1134 ~PCI_MSIX_FLAGS_BIRMASK);
1136 release_mem_region(phys_addr, PCI_MSIX_ENTRY_SIZE *
1137 multi_msix_capable(control));
1138 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
1139 "called without free_irq() on all MSI-X vectors\n",
1141 BUG_ON(warning > 0);
1143 dev->irq = temp; /* Restore IOAPIC IRQ */
1147 EXPORT_SYMBOL(pci_enable_msi);
1148 EXPORT_SYMBOL(pci_disable_msi);
1149 EXPORT_SYMBOL(pci_enable_msix);
1150 EXPORT_SYMBOL(pci_disable_msix);