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>
26 #define MSI_TARGET_CPU first_cpu(cpu_online_map)
28 static DEFINE_SPINLOCK(msi_lock);
29 static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL };
30 static kmem_cache_t* msi_cachep;
32 static int pci_msi_enable = 1;
33 static int last_alloc_vector;
34 static int nr_released_vectors;
35 static int nr_reserved_vectors = NR_HP_RESERVED_VECTORS;
36 static int nr_msix_devices;
38 #ifndef CONFIG_X86_IO_APIC
39 int vector_irq[NR_VECTORS] = { [0 ... NR_VECTORS - 1] = -1};
40 u8 irq_vector[NR_IRQ_VECTORS] = { FIRST_DEVICE_VECTOR , 0 };
43 static void msi_cache_ctor(void *p, kmem_cache_t *cache, unsigned long flags)
45 memset(p, 0, NR_IRQS * sizeof(struct msi_desc));
48 static int msi_cache_init(void)
50 msi_cachep = kmem_cache_create("msi_cache",
51 NR_IRQS * sizeof(struct msi_desc),
52 0, SLAB_HWCACHE_ALIGN, msi_cache_ctor, NULL);
59 static void msi_set_mask_bit(unsigned int vector, int flag)
61 struct msi_desc *entry;
63 entry = (struct msi_desc *)msi_desc[vector];
64 if (!entry || !entry->dev || !entry->mask_base)
66 switch (entry->msi_attrib.type) {
72 pos = (long)entry->mask_base;
73 pci_read_config_dword(entry->dev, pos, &mask_bits);
76 pci_write_config_dword(entry->dev, pos, mask_bits);
81 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
82 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
83 writel(flag, entry->mask_base + offset);
92 static void set_msi_affinity(unsigned int vector, cpumask_t cpu_mask)
94 struct msi_desc *entry;
95 struct msg_address address;
96 unsigned int irq = vector;
97 unsigned int dest_cpu = first_cpu(cpu_mask);
99 entry = (struct msi_desc *)msi_desc[vector];
100 if (!entry || !entry->dev)
103 switch (entry->msi_attrib.type) {
106 int pos = pci_find_capability(entry->dev, PCI_CAP_ID_MSI);
111 pci_read_config_dword(entry->dev, msi_lower_address_reg(pos),
112 &address.lo_address.value);
113 address.lo_address.value &= MSI_ADDRESS_DEST_ID_MASK;
114 address.lo_address.value |= (cpu_physical_id(dest_cpu) <<
115 MSI_TARGET_CPU_SHIFT);
116 entry->msi_attrib.current_cpu = cpu_physical_id(dest_cpu);
117 pci_write_config_dword(entry->dev, msi_lower_address_reg(pos),
118 address.lo_address.value);
119 set_native_irq_info(irq, cpu_mask);
122 case PCI_CAP_ID_MSIX:
124 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
125 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET;
127 address.lo_address.value = readl(entry->mask_base + offset);
128 address.lo_address.value &= MSI_ADDRESS_DEST_ID_MASK;
129 address.lo_address.value |= (cpu_physical_id(dest_cpu) <<
130 MSI_TARGET_CPU_SHIFT);
131 entry->msi_attrib.current_cpu = cpu_physical_id(dest_cpu);
132 writel(address.lo_address.value, entry->mask_base + offset);
133 set_native_irq_info(irq, cpu_mask);
141 #define set_msi_affinity NULL
142 #endif /* CONFIG_SMP */
144 static void mask_MSI_irq(unsigned int vector)
146 msi_set_mask_bit(vector, 1);
149 static void unmask_MSI_irq(unsigned int vector)
151 msi_set_mask_bit(vector, 0);
154 static unsigned int startup_msi_irq_wo_maskbit(unsigned int vector)
156 struct msi_desc *entry;
159 spin_lock_irqsave(&msi_lock, flags);
160 entry = msi_desc[vector];
161 if (!entry || !entry->dev) {
162 spin_unlock_irqrestore(&msi_lock, flags);
165 entry->msi_attrib.state = 1; /* Mark it active */
166 spin_unlock_irqrestore(&msi_lock, flags);
168 return 0; /* never anything pending */
171 static unsigned int startup_msi_irq_w_maskbit(unsigned int vector)
173 startup_msi_irq_wo_maskbit(vector);
174 unmask_MSI_irq(vector);
175 return 0; /* never anything pending */
178 static void shutdown_msi_irq(unsigned int vector)
180 struct msi_desc *entry;
183 spin_lock_irqsave(&msi_lock, flags);
184 entry = msi_desc[vector];
185 if (entry && entry->dev)
186 entry->msi_attrib.state = 0; /* Mark it not active */
187 spin_unlock_irqrestore(&msi_lock, flags);
190 static void end_msi_irq_wo_maskbit(unsigned int vector)
192 move_native_irq(vector);
196 static void end_msi_irq_w_maskbit(unsigned int vector)
198 move_native_irq(vector);
199 unmask_MSI_irq(vector);
203 static void do_nothing(unsigned int vector)
208 * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
209 * which implement the MSI-X Capability Structure.
211 static struct hw_interrupt_type msix_irq_type = {
212 .typename = "PCI-MSI-X",
213 .startup = startup_msi_irq_w_maskbit,
214 .shutdown = shutdown_msi_irq,
215 .enable = unmask_MSI_irq,
216 .disable = mask_MSI_irq,
218 .end = end_msi_irq_w_maskbit,
219 .set_affinity = set_msi_affinity
223 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
224 * which implement the MSI Capability Structure with
225 * Mask-and-Pending Bits.
227 static struct hw_interrupt_type msi_irq_w_maskbit_type = {
228 .typename = "PCI-MSI",
229 .startup = startup_msi_irq_w_maskbit,
230 .shutdown = shutdown_msi_irq,
231 .enable = unmask_MSI_irq,
232 .disable = mask_MSI_irq,
234 .end = end_msi_irq_w_maskbit,
235 .set_affinity = set_msi_affinity
239 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
240 * which implement the MSI Capability Structure without
241 * Mask-and-Pending Bits.
243 static struct hw_interrupt_type msi_irq_wo_maskbit_type = {
244 .typename = "PCI-MSI",
245 .startup = startup_msi_irq_wo_maskbit,
246 .shutdown = shutdown_msi_irq,
247 .enable = do_nothing,
248 .disable = do_nothing,
250 .end = end_msi_irq_wo_maskbit,
251 .set_affinity = set_msi_affinity
254 static void msi_data_init(struct msg_data *msi_data,
257 memset(msi_data, 0, sizeof(struct msg_data));
258 msi_data->vector = (u8)vector;
259 msi_data->delivery_mode = MSI_DELIVERY_MODE;
260 msi_data->level = MSI_LEVEL_MODE;
261 msi_data->trigger = MSI_TRIGGER_MODE;
264 static void msi_address_init(struct msg_address *msi_address)
266 unsigned int dest_id;
267 unsigned long dest_phys_id = cpu_physical_id(MSI_TARGET_CPU);
269 memset(msi_address, 0, sizeof(struct msg_address));
270 msi_address->hi_address = (u32)0;
271 dest_id = (MSI_ADDRESS_HEADER << MSI_ADDRESS_HEADER_SHIFT);
272 msi_address->lo_address.u.dest_mode = MSI_PHYSICAL_MODE;
273 msi_address->lo_address.u.redirection_hint = MSI_REDIRECTION_HINT_MODE;
274 msi_address->lo_address.u.dest_id = dest_id;
275 msi_address->lo_address.value |= (dest_phys_id << MSI_TARGET_CPU_SHIFT);
278 static int msi_free_vector(struct pci_dev* dev, int vector, int reassign);
279 static int assign_msi_vector(void)
281 static int new_vector_avail = 1;
286 * msi_lock is provided to ensure that successful allocation of MSI
287 * vector is assigned unique among drivers.
289 spin_lock_irqsave(&msi_lock, flags);
291 if (!new_vector_avail) {
295 * vector_irq[] = -1 indicates that this specific vector is:
296 * - assigned for MSI (since MSI have no associated IRQ) or
297 * - assigned for legacy if less than 16, or
298 * - having no corresponding 1:1 vector-to-IOxAPIC IRQ mapping
299 * vector_irq[] = 0 indicates that this vector, previously
300 * assigned for MSI, is freed by hotplug removed operations.
301 * This vector will be reused for any subsequent hotplug added
303 * vector_irq[] > 0 indicates that this vector is assigned for
304 * IOxAPIC IRQs. This vector and its value provides a 1-to-1
305 * vector-to-IOxAPIC IRQ mapping.
307 for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
308 if (vector_irq[vector] != 0)
310 free_vector = vector;
311 if (!msi_desc[vector])
317 spin_unlock_irqrestore(&msi_lock, flags);
320 vector_irq[free_vector] = -1;
321 nr_released_vectors--;
322 spin_unlock_irqrestore(&msi_lock, flags);
323 if (msi_desc[free_vector] != NULL) {
327 /* free all linked vectors before re-assign */
329 spin_lock_irqsave(&msi_lock, flags);
330 dev = msi_desc[free_vector]->dev;
331 tail = msi_desc[free_vector]->link.tail;
332 spin_unlock_irqrestore(&msi_lock, flags);
333 msi_free_vector(dev, tail, 1);
334 } while (free_vector != tail);
339 vector = assign_irq_vector(AUTO_ASSIGN);
340 last_alloc_vector = vector;
341 if (vector == LAST_DEVICE_VECTOR)
342 new_vector_avail = 0;
344 spin_unlock_irqrestore(&msi_lock, flags);
348 static int get_new_vector(void)
350 int vector = assign_msi_vector();
353 set_intr_gate(vector, interrupt[vector]);
358 static int msi_init(void)
360 static int status = -ENOMEM;
367 printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n");
372 status = msi_cache_init();
375 printk(KERN_WARNING "PCI: MSI cache init failed\n");
378 last_alloc_vector = assign_irq_vector(AUTO_ASSIGN);
379 if (last_alloc_vector < 0) {
381 printk(KERN_WARNING "PCI: No interrupt vectors available for MSI\n");
385 vector_irq[last_alloc_vector] = 0;
386 nr_released_vectors++;
391 static int get_msi_vector(struct pci_dev *dev)
393 return get_new_vector();
396 static struct msi_desc* alloc_msi_entry(void)
398 struct msi_desc *entry;
400 entry = kmem_cache_alloc(msi_cachep, SLAB_KERNEL);
404 memset(entry, 0, sizeof(struct msi_desc));
405 entry->link.tail = entry->link.head = 0; /* single message */
411 static void attach_msi_entry(struct msi_desc *entry, int vector)
415 spin_lock_irqsave(&msi_lock, flags);
416 msi_desc[vector] = entry;
417 spin_unlock_irqrestore(&msi_lock, flags);
420 static void irq_handler_init(int cap_id, int pos, int mask)
424 spin_lock_irqsave(&irq_desc[pos].lock, flags);
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_irqrestore(&irq_desc[pos].lock, flags);
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 */
451 pci_intx(dev, 0); /* disable intx */
455 void disable_msi_mode(struct pci_dev *dev, int pos, int type)
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);
465 msix_disable(control);
466 pci_write_config_word(dev, msi_control_reg(pos), control);
468 if (pci_find_capability(dev, PCI_CAP_ID_EXP)) {
469 /* PCI Express Endpoint device detected */
470 pci_intx(dev, 1); /* enable intx */
474 static int msi_lookup_vector(struct pci_dev *dev, int type)
479 spin_lock_irqsave(&msi_lock, flags);
480 for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) {
481 if (!msi_desc[vector] || msi_desc[vector]->dev != dev ||
482 msi_desc[vector]->msi_attrib.type != type ||
483 msi_desc[vector]->msi_attrib.default_vector != dev->irq)
485 spin_unlock_irqrestore(&msi_lock, flags);
486 /* This pre-assigned MSI vector for this device
487 already exits. Override dev->irq with this vector */
491 spin_unlock_irqrestore(&msi_lock, flags);
496 void pci_scan_msi_device(struct pci_dev *dev)
501 if (pci_find_capability(dev, PCI_CAP_ID_MSIX) > 0)
503 else if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0)
504 nr_reserved_vectors++;
508 * msi_capability_init - configure device's MSI capability structure
509 * @dev: pointer to the pci_dev data structure of MSI device function
511 * Setup the MSI capability structure of device function with a single
512 * MSI vector, regardless of device function is capable of handling
513 * multiple messages. A return of zero indicates the successful setup
514 * of an entry zero with the new MSI vector or non-zero for otherwise.
516 static int msi_capability_init(struct pci_dev *dev)
518 struct msi_desc *entry;
519 struct msg_address address;
520 struct msg_data data;
524 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
525 pci_read_config_word(dev, msi_control_reg(pos), &control);
526 /* MSI Entry Initialization */
527 entry = alloc_msi_entry();
531 vector = get_msi_vector(dev);
533 kmem_cache_free(msi_cachep, entry);
536 entry->link.head = vector;
537 entry->link.tail = vector;
538 entry->msi_attrib.type = PCI_CAP_ID_MSI;
539 entry->msi_attrib.state = 0; /* Mark it not active */
540 entry->msi_attrib.entry_nr = 0;
541 entry->msi_attrib.maskbit = is_mask_bit_support(control);
542 entry->msi_attrib.default_vector = dev->irq; /* Save IOAPIC IRQ */
545 if (is_mask_bit_support(control)) {
546 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
547 is_64bit_address(control));
549 /* Replace with MSI handler */
550 irq_handler_init(PCI_CAP_ID_MSI, vector, entry->msi_attrib.maskbit);
551 /* Configure MSI capability structure */
552 msi_address_init(&address);
553 msi_data_init(&data, vector);
554 entry->msi_attrib.current_cpu = ((address.lo_address.u.dest_id >>
555 MSI_TARGET_CPU_SHIFT) & MSI_TARGET_CPU_MASK);
556 pci_write_config_dword(dev, msi_lower_address_reg(pos),
557 address.lo_address.value);
558 if (is_64bit_address(control)) {
559 pci_write_config_dword(dev,
560 msi_upper_address_reg(pos), address.hi_address);
561 pci_write_config_word(dev,
562 msi_data_reg(pos, 1), *((u32*)&data));
564 pci_write_config_word(dev,
565 msi_data_reg(pos, 0), *((u32*)&data));
566 if (entry->msi_attrib.maskbit) {
567 unsigned int maskbits, temp;
568 /* All MSIs are unmasked by default, Mask them all */
569 pci_read_config_dword(dev,
570 msi_mask_bits_reg(pos, is_64bit_address(control)),
572 temp = (1 << multi_msi_capable(control));
573 temp = ((temp - 1) & ~temp);
575 pci_write_config_dword(dev,
576 msi_mask_bits_reg(pos, is_64bit_address(control)),
579 attach_msi_entry(entry, vector);
580 /* Set MSI enabled bits */
581 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
587 * msix_capability_init - configure device's MSI-X capability
588 * @dev: pointer to the pci_dev data structure of MSI-X device function
589 * @entries: pointer to an array of struct msix_entry entries
590 * @nvec: number of @entries
592 * Setup the MSI-X capability structure of device function with a
593 * single MSI-X vector. A return of zero indicates the successful setup of
594 * requested MSI-X entries with allocated vectors or non-zero for otherwise.
596 static int msix_capability_init(struct pci_dev *dev,
597 struct msix_entry *entries, int nvec)
599 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
600 struct msg_address address;
601 struct msg_data data;
602 int vector, pos, i, j, nr_entries, temp = 0;
603 unsigned long phys_addr;
609 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
610 /* Request & Map MSI-X table region */
611 pci_read_config_word(dev, msi_control_reg(pos), &control);
612 nr_entries = multi_msix_capable(control);
614 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
615 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
616 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
617 phys_addr = pci_resource_start (dev, bir) + table_offset;
618 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
622 /* MSI-X Table Initialization */
623 for (i = 0; i < nvec; i++) {
624 entry = alloc_msi_entry();
627 vector = get_msi_vector(dev);
631 j = entries[i].entry;
632 entries[i].vector = vector;
633 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
634 entry->msi_attrib.state = 0; /* Mark it not active */
635 entry->msi_attrib.entry_nr = j;
636 entry->msi_attrib.maskbit = 1;
637 entry->msi_attrib.default_vector = dev->irq;
639 entry->mask_base = base;
641 entry->link.head = vector;
642 entry->link.tail = vector;
645 entry->link.head = temp;
646 entry->link.tail = tail->link.tail;
647 tail->link.tail = vector;
648 head->link.head = vector;
652 /* Replace with MSI-X handler */
653 irq_handler_init(PCI_CAP_ID_MSIX, vector, 1);
654 /* Configure MSI-X capability structure */
655 msi_address_init(&address);
656 msi_data_init(&data, vector);
657 entry->msi_attrib.current_cpu =
658 ((address.lo_address.u.dest_id >>
659 MSI_TARGET_CPU_SHIFT) & MSI_TARGET_CPU_MASK);
660 writel(address.lo_address.value,
661 base + j * PCI_MSIX_ENTRY_SIZE +
662 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
663 writel(address.hi_address,
664 base + j * PCI_MSIX_ENTRY_SIZE +
665 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
667 base + j * PCI_MSIX_ENTRY_SIZE +
668 PCI_MSIX_ENTRY_DATA_OFFSET);
669 attach_msi_entry(entry, vector);
673 for (; i >= 0; i--) {
674 vector = (entries + i)->vector;
675 msi_free_vector(dev, vector, 0);
676 (entries + i)->vector = 0;
680 /* Set MSI-X enabled bits */
681 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
687 * pci_enable_msi - configure device's MSI capability structure
688 * @dev: pointer to the pci_dev data structure of MSI device function
690 * Setup the MSI capability structure of device function with
691 * a single MSI vector upon its software driver call to request for
692 * MSI mode enabled on its hardware device function. A return of zero
693 * indicates the successful setup of an entry zero with the new MSI
694 * vector or non-zero for otherwise.
696 int pci_enable_msi(struct pci_dev* dev)
698 int pos, temp, status = -EINVAL;
701 if (!pci_msi_enable || !dev)
707 if (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
716 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
720 pci_read_config_word(dev, msi_control_reg(pos), &control);
721 if (control & PCI_MSI_FLAGS_ENABLE)
722 return 0; /* Already in MSI mode */
724 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
728 spin_lock_irqsave(&msi_lock, flags);
729 if (!vector_irq[dev->irq]) {
730 msi_desc[dev->irq]->msi_attrib.state = 0;
731 vector_irq[dev->irq] = -1;
732 nr_released_vectors--;
733 spin_unlock_irqrestore(&msi_lock, flags);
734 enable_msi_mode(dev, pos, PCI_CAP_ID_MSI);
737 spin_unlock_irqrestore(&msi_lock, flags);
740 /* Check whether driver already requested for MSI-X vectors */
741 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
742 if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
743 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
744 "Device already has MSI-X vectors assigned\n",
749 status = msi_capability_init(dev);
752 nr_reserved_vectors--; /* Only MSI capable */
753 else if (nr_msix_devices > 0)
754 nr_msix_devices--; /* Both MSI and MSI-X capable,
755 but choose enabling MSI */
761 void pci_disable_msi(struct pci_dev* dev)
763 struct msi_desc *entry;
764 int pos, default_vector;
770 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
774 pci_read_config_word(dev, msi_control_reg(pos), &control);
775 if (!(control & PCI_MSI_FLAGS_ENABLE))
778 spin_lock_irqsave(&msi_lock, flags);
779 entry = msi_desc[dev->irq];
780 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
781 spin_unlock_irqrestore(&msi_lock, flags);
784 if (entry->msi_attrib.state) {
785 spin_unlock_irqrestore(&msi_lock, flags);
786 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
787 "free_irq() on MSI vector %d\n",
788 pci_name(dev), dev->irq);
789 BUG_ON(entry->msi_attrib.state > 0);
791 vector_irq[dev->irq] = 0; /* free it */
792 nr_released_vectors++;
793 default_vector = entry->msi_attrib.default_vector;
794 spin_unlock_irqrestore(&msi_lock, flags);
795 /* Restore dev->irq to its default pin-assertion vector */
796 dev->irq = default_vector;
797 disable_msi_mode(dev, pci_find_capability(dev, PCI_CAP_ID_MSI),
802 static int msi_free_vector(struct pci_dev* dev, int vector, int reassign)
804 struct msi_desc *entry;
805 int head, entry_nr, type;
809 spin_lock_irqsave(&msi_lock, flags);
810 entry = msi_desc[vector];
811 if (!entry || entry->dev != dev) {
812 spin_unlock_irqrestore(&msi_lock, flags);
815 type = entry->msi_attrib.type;
816 entry_nr = entry->msi_attrib.entry_nr;
817 head = entry->link.head;
818 base = entry->mask_base;
819 msi_desc[entry->link.head]->link.tail = entry->link.tail;
820 msi_desc[entry->link.tail]->link.head = entry->link.head;
823 vector_irq[vector] = 0;
824 nr_released_vectors++;
826 msi_desc[vector] = NULL;
827 spin_unlock_irqrestore(&msi_lock, flags);
829 kmem_cache_free(msi_cachep, entry);
831 if (type == PCI_CAP_ID_MSIX) {
834 entry_nr * PCI_MSIX_ENTRY_SIZE +
835 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
837 if (head == vector) {
839 * Detect last MSI-X vector to be released.
840 * Release the MSI-X memory-mapped table.
844 unsigned long phys_addr;
849 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
850 pci_read_config_word(dev, msi_control_reg(pos),
852 nr_entries = multi_msix_capable(control);
853 pci_read_config_dword(dev, msix_table_offset_reg(pos),
855 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
856 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
857 phys_addr = pci_resource_start(dev, bir) + table_offset;
859 * FIXME! and what did you want to do with phys_addr?
869 static int reroute_msix_table(int head, struct msix_entry *entries, int *nvec)
871 int vector = head, tail = 0;
872 int i, j = 0, nr_entries = 0;
876 spin_lock_irqsave(&msi_lock, flags);
877 while (head != tail) {
879 tail = msi_desc[vector]->link.tail;
880 if (entries[0].entry == msi_desc[vector]->msi_attrib.entry_nr)
884 if (*nvec > nr_entries) {
885 spin_unlock_irqrestore(&msi_lock, flags);
889 vector = ((j > 0) ? j : head);
890 for (i = 0; i < *nvec; i++) {
891 j = msi_desc[vector]->msi_attrib.entry_nr;
892 msi_desc[vector]->msi_attrib.state = 0; /* Mark it not active */
893 vector_irq[vector] = -1; /* Mark it busy */
894 nr_released_vectors--;
895 entries[i].vector = vector;
896 if (j != (entries + i)->entry) {
897 base = msi_desc[vector]->mask_base;
898 msi_desc[vector]->msi_attrib.entry_nr =
899 (entries + i)->entry;
900 writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
901 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET), base +
902 (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
903 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
904 writel( readl(base + j * PCI_MSIX_ENTRY_SIZE +
905 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET), base +
906 (entries + i)->entry * PCI_MSIX_ENTRY_SIZE +
907 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
908 writel( (readl(base + j * PCI_MSIX_ENTRY_SIZE +
909 PCI_MSIX_ENTRY_DATA_OFFSET) & 0xff00) | vector,
910 base + (entries+i)->entry*PCI_MSIX_ENTRY_SIZE +
911 PCI_MSIX_ENTRY_DATA_OFFSET);
913 vector = msi_desc[vector]->link.tail;
915 spin_unlock_irqrestore(&msi_lock, flags);
921 * pci_enable_msix - configure device's MSI-X capability structure
922 * @dev: pointer to the pci_dev data structure of MSI-X device function
923 * @entries: pointer to an array of MSI-X entries
924 * @nvec: number of MSI-X vectors requested for allocation by device driver
926 * Setup the MSI-X capability structure of device function with the number
927 * of requested vectors upon its software driver call to request for
928 * MSI-X mode enabled on its hardware device function. A return of zero
929 * indicates the successful configuration of MSI-X capability structure
930 * with new allocated MSI-X vectors. A return of < 0 indicates a failure.
931 * Or a return of > 0 indicates that driver request is exceeding the number
932 * of vectors available. Driver should use the returned value to re-send
935 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
937 int status, pos, nr_entries, free_vectors;
942 if (!pci_msi_enable || !dev || !entries)
949 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
953 pci_read_config_word(dev, msi_control_reg(pos), &control);
954 if (control & PCI_MSIX_FLAGS_ENABLE)
955 return -EINVAL; /* Already in MSI-X mode */
957 nr_entries = multi_msix_capable(control);
958 if (nvec > nr_entries)
961 /* Check for any invalid entries */
962 for (i = 0; i < nvec; i++) {
963 if (entries[i].entry >= nr_entries)
964 return -EINVAL; /* invalid entry */
965 for (j = i + 1; j < nvec; j++) {
966 if (entries[i].entry == entries[j].entry)
967 return -EINVAL; /* duplicate entry */
971 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
974 /* Reroute MSI-X table */
975 if (reroute_msix_table(dev->irq, entries, &nr_entries)) {
976 /* #requested > #previous-assigned */
981 enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX);
984 /* Check whether driver already requested for MSI vector */
985 if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 &&
986 !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
987 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
988 "Device already has an MSI vector assigned\n",
994 spin_lock_irqsave(&msi_lock, flags);
996 * msi_lock is provided to ensure that enough vectors resources are
997 * available before granting.
999 free_vectors = pci_vector_resources(last_alloc_vector,
1000 nr_released_vectors);
1001 /* Ensure that each MSI/MSI-X device has one vector reserved by
1002 default to avoid any MSI-X driver to take all available
1004 free_vectors -= nr_reserved_vectors;
1005 /* Find the average of free vectors among MSI-X devices */
1006 if (nr_msix_devices > 0)
1007 free_vectors /= nr_msix_devices;
1008 spin_unlock_irqrestore(&msi_lock, flags);
1010 if (nvec > free_vectors) {
1011 if (free_vectors > 0)
1012 return free_vectors;
1017 status = msix_capability_init(dev, entries, nvec);
1018 if (!status && nr_msix_devices > 0)
1024 void pci_disable_msix(struct pci_dev* dev)
1032 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1036 pci_read_config_word(dev, msi_control_reg(pos), &control);
1037 if (!(control & PCI_MSIX_FLAGS_ENABLE))
1041 if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
1042 int state, vector, head, tail = 0, warning = 0;
1043 unsigned long flags;
1045 vector = head = dev->irq;
1046 spin_lock_irqsave(&msi_lock, flags);
1047 while (head != tail) {
1048 state = msi_desc[vector]->msi_attrib.state;
1052 vector_irq[vector] = 0; /* free it */
1053 nr_released_vectors++;
1055 tail = msi_desc[vector]->link.tail;
1058 spin_unlock_irqrestore(&msi_lock, flags);
1061 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
1062 "free_irq() on all MSI-X vectors\n",
1064 BUG_ON(warning > 0);
1067 disable_msi_mode(dev,
1068 pci_find_capability(dev, PCI_CAP_ID_MSIX),
1076 * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state
1077 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1079 * Being called during hotplug remove, from which the device function
1080 * is hot-removed. All previous assigned MSI/MSI-X vectors, if
1081 * allocated for this device function, are reclaimed to unused state,
1082 * which may be used later on.
1084 void msi_remove_pci_irq_vectors(struct pci_dev* dev)
1086 int state, pos, temp;
1087 unsigned long flags;
1089 if (!pci_msi_enable || !dev)
1092 temp = dev->irq; /* Save IOAPIC IRQ */
1093 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
1094 if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) {
1095 spin_lock_irqsave(&msi_lock, flags);
1096 state = msi_desc[dev->irq]->msi_attrib.state;
1097 spin_unlock_irqrestore(&msi_lock, flags);
1099 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
1100 "called without free_irq() on MSI vector %d\n",
1101 pci_name(dev), dev->irq);
1103 } else /* Release MSI vector assigned to this device */
1104 msi_free_vector(dev, dev->irq, 0);
1105 dev->irq = temp; /* Restore IOAPIC IRQ */
1107 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1108 if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) {
1109 int vector, head, tail = 0, warning = 0;
1110 void __iomem *base = NULL;
1112 vector = head = dev->irq;
1113 while (head != tail) {
1114 spin_lock_irqsave(&msi_lock, flags);
1115 state = msi_desc[vector]->msi_attrib.state;
1116 tail = msi_desc[vector]->link.tail;
1117 base = msi_desc[vector]->mask_base;
1118 spin_unlock_irqrestore(&msi_lock, flags);
1121 else if (vector != head) /* Release MSI-X vector */
1122 msi_free_vector(dev, vector, 0);
1125 msi_free_vector(dev, vector, 0);
1127 /* Force to release the MSI-X memory-mapped table */
1129 unsigned long phys_addr;
1134 pci_read_config_word(dev, msi_control_reg(pos),
1136 pci_read_config_dword(dev, msix_table_offset_reg(pos),
1138 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
1139 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
1140 phys_addr = pci_resource_start(dev, bir) + table_offset;
1142 * FIXME! and what did you want to do with phys_addr?
1146 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
1147 "called without free_irq() on all MSI-X vectors\n",
1149 BUG_ON(warning > 0);
1151 dev->irq = temp; /* Restore IOAPIC IRQ */
1155 EXPORT_SYMBOL(pci_enable_msi);
1156 EXPORT_SYMBOL(pci_disable_msi);
1157 EXPORT_SYMBOL(pci_enable_msix);
1158 EXPORT_SYMBOL(pci_disable_msix);