3 * Purpose: PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
11 #include <linux/irq.h>
12 #include <linux/interrupt.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/smp_lock.h>
16 #include <linux/pci.h>
17 #include <linux/proc_fs.h>
18 #include <linux/msi.h>
20 #include <asm/errno.h>
27 static DEFINE_SPINLOCK(msi_lock
);
28 static struct msi_desc
* msi_desc
[NR_IRQS
] = { [0 ... NR_IRQS
-1] = NULL
};
29 static struct kmem_cache
* msi_cachep
;
31 static int pci_msi_enable
= 1;
33 static int msi_cache_init(void)
35 msi_cachep
= kmem_cache_create("msi_cache", sizeof(struct msi_desc
),
36 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
43 static void msi_set_mask_bit(unsigned int irq
, int flag
)
45 struct msi_desc
*entry
;
47 entry
= msi_desc
[irq
];
48 BUG_ON(!entry
|| !entry
->dev
);
49 switch (entry
->msi_attrib
.type
) {
51 if (entry
->msi_attrib
.maskbit
) {
55 pos
= (long)entry
->mask_base
;
56 pci_read_config_dword(entry
->dev
, pos
, &mask_bits
);
59 pci_write_config_dword(entry
->dev
, pos
, mask_bits
);
64 int offset
= entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
+
65 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
;
66 writel(flag
, entry
->mask_base
+ offset
);
75 void read_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
77 struct msi_desc
*entry
= get_irq_data(irq
);
78 switch(entry
->msi_attrib
.type
) {
81 struct pci_dev
*dev
= entry
->dev
;
82 int pos
= entry
->msi_attrib
.pos
;
85 pci_read_config_dword(dev
, msi_lower_address_reg(pos
),
87 if (entry
->msi_attrib
.is_64
) {
88 pci_read_config_dword(dev
, msi_upper_address_reg(pos
),
90 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
93 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
101 base
= entry
->mask_base
+
102 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
104 msg
->address_lo
= readl(base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
105 msg
->address_hi
= readl(base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
106 msg
->data
= readl(base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
114 void write_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
116 struct msi_desc
*entry
= get_irq_data(irq
);
117 switch (entry
->msi_attrib
.type
) {
120 struct pci_dev
*dev
= entry
->dev
;
121 int pos
= entry
->msi_attrib
.pos
;
123 pci_write_config_dword(dev
, msi_lower_address_reg(pos
),
125 if (entry
->msi_attrib
.is_64
) {
126 pci_write_config_dword(dev
, msi_upper_address_reg(pos
),
128 pci_write_config_word(dev
, msi_data_reg(pos
, 1),
131 pci_write_config_word(dev
, msi_data_reg(pos
, 0),
136 case PCI_CAP_ID_MSIX
:
139 base
= entry
->mask_base
+
140 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
142 writel(msg
->address_lo
,
143 base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
144 writel(msg
->address_hi
,
145 base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
146 writel(msg
->data
, base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
154 void mask_msi_irq(unsigned int irq
)
156 msi_set_mask_bit(irq
, 1);
159 void unmask_msi_irq(unsigned int irq
)
161 msi_set_mask_bit(irq
, 0);
164 static int msi_free_irq(struct pci_dev
* dev
, int irq
);
165 static int msi_init(void)
167 static int status
= -ENOMEM
;
174 printk(KERN_WARNING
"PCI: MSI quirk detected. MSI disabled.\n");
179 status
= msi_cache_init();
182 printk(KERN_WARNING
"PCI: MSI cache init failed\n");
189 static struct msi_desc
* alloc_msi_entry(void)
191 struct msi_desc
*entry
;
193 entry
= kmem_cache_zalloc(msi_cachep
, GFP_KERNEL
);
197 entry
->link
.tail
= entry
->link
.head
= 0; /* single message */
203 static void attach_msi_entry(struct msi_desc
*entry
, int irq
)
207 spin_lock_irqsave(&msi_lock
, flags
);
208 msi_desc
[irq
] = entry
;
209 spin_unlock_irqrestore(&msi_lock
, flags
);
212 static int create_msi_irq(void)
214 struct msi_desc
*entry
;
217 entry
= alloc_msi_entry();
223 kmem_cache_free(msi_cachep
, entry
);
227 set_irq_data(irq
, entry
);
232 static void destroy_msi_irq(unsigned int irq
)
234 struct msi_desc
*entry
;
236 entry
= get_irq_data(irq
);
237 set_irq_chip(irq
, NULL
);
238 set_irq_data(irq
, NULL
);
240 kmem_cache_free(msi_cachep
, entry
);
243 static void enable_msi_mode(struct pci_dev
*dev
, int pos
, int type
)
247 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
248 if (type
== PCI_CAP_ID_MSI
) {
249 /* Set enabled bits to single MSI & enable MSI_enable bit */
250 msi_enable(control
, 1);
251 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
252 dev
->msi_enabled
= 1;
254 msix_enable(control
);
255 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
256 dev
->msix_enabled
= 1;
259 pci_intx(dev
, 0); /* disable intx */
262 void disable_msi_mode(struct pci_dev
*dev
, int pos
, int type
)
266 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
267 if (type
== PCI_CAP_ID_MSI
) {
268 /* Set enabled bits to single MSI & enable MSI_enable bit */
269 msi_disable(control
);
270 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
271 dev
->msi_enabled
= 0;
273 msix_disable(control
);
274 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
275 dev
->msix_enabled
= 0;
278 pci_intx(dev
, 1); /* enable intx */
281 static int msi_lookup_irq(struct pci_dev
*dev
, int type
)
286 spin_lock_irqsave(&msi_lock
, flags
);
287 for (irq
= 0; irq
< NR_IRQS
; irq
++) {
288 if (!msi_desc
[irq
] || msi_desc
[irq
]->dev
!= dev
||
289 msi_desc
[irq
]->msi_attrib
.type
!= type
||
290 msi_desc
[irq
]->msi_attrib
.default_irq
!= dev
->irq
)
292 spin_unlock_irqrestore(&msi_lock
, flags
);
293 /* This pre-assigned MSI irq for this device
294 already exits. Override dev->irq with this irq */
298 spin_unlock_irqrestore(&msi_lock
, flags
);
303 void pci_scan_msi_device(struct pci_dev
*dev
)
310 int pci_save_msi_state(struct pci_dev
*dev
)
314 struct pci_cap_saved_state
*save_state
;
317 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
318 if (pos
<= 0 || dev
->no_msi
)
321 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
322 if (!(control
& PCI_MSI_FLAGS_ENABLE
))
325 save_state
= kzalloc(sizeof(struct pci_cap_saved_state
) + sizeof(u32
) * 5,
328 printk(KERN_ERR
"Out of memory in pci_save_msi_state\n");
331 cap
= &save_state
->data
[0];
333 pci_read_config_dword(dev
, pos
, &cap
[i
++]);
334 control
= cap
[0] >> 16;
335 pci_read_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_LO
, &cap
[i
++]);
336 if (control
& PCI_MSI_FLAGS_64BIT
) {
337 pci_read_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_HI
, &cap
[i
++]);
338 pci_read_config_dword(dev
, pos
+ PCI_MSI_DATA_64
, &cap
[i
++]);
340 pci_read_config_dword(dev
, pos
+ PCI_MSI_DATA_32
, &cap
[i
++]);
341 if (control
& PCI_MSI_FLAGS_MASKBIT
)
342 pci_read_config_dword(dev
, pos
+ PCI_MSI_MASK_BIT
, &cap
[i
++]);
343 save_state
->cap_nr
= PCI_CAP_ID_MSI
;
344 pci_add_saved_cap(dev
, save_state
);
348 void pci_restore_msi_state(struct pci_dev
*dev
)
352 struct pci_cap_saved_state
*save_state
;
355 save_state
= pci_find_saved_cap(dev
, PCI_CAP_ID_MSI
);
356 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
357 if (!save_state
|| pos
<= 0)
359 cap
= &save_state
->data
[0];
361 control
= cap
[i
++] >> 16;
362 pci_write_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_LO
, cap
[i
++]);
363 if (control
& PCI_MSI_FLAGS_64BIT
) {
364 pci_write_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_HI
, cap
[i
++]);
365 pci_write_config_dword(dev
, pos
+ PCI_MSI_DATA_64
, cap
[i
++]);
367 pci_write_config_dword(dev
, pos
+ PCI_MSI_DATA_32
, cap
[i
++]);
368 if (control
& PCI_MSI_FLAGS_MASKBIT
)
369 pci_write_config_dword(dev
, pos
+ PCI_MSI_MASK_BIT
, cap
[i
++]);
370 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
371 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSI
);
372 pci_remove_saved_cap(save_state
);
376 int pci_save_msix_state(struct pci_dev
*dev
)
380 int irq
, head
, tail
= 0;
382 struct pci_cap_saved_state
*save_state
;
384 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
385 if (pos
<= 0 || dev
->no_msi
)
388 /* save the capability */
389 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
390 if (!(control
& PCI_MSIX_FLAGS_ENABLE
))
392 save_state
= kzalloc(sizeof(struct pci_cap_saved_state
) + sizeof(u16
),
395 printk(KERN_ERR
"Out of memory in pci_save_msix_state\n");
398 *((u16
*)&save_state
->data
[0]) = control
;
402 if (msi_lookup_irq(dev
, PCI_CAP_ID_MSIX
)) {
407 irq
= head
= dev
->irq
;
408 while (head
!= tail
) {
409 struct msi_desc
*entry
;
411 entry
= msi_desc
[irq
];
412 read_msi_msg(irq
, &entry
->msg_save
);
414 tail
= msi_desc
[irq
]->link
.tail
;
419 save_state
->cap_nr
= PCI_CAP_ID_MSIX
;
420 pci_add_saved_cap(dev
, save_state
);
424 void pci_restore_msix_state(struct pci_dev
*dev
)
428 int irq
, head
, tail
= 0;
429 struct msi_desc
*entry
;
431 struct pci_cap_saved_state
*save_state
;
433 save_state
= pci_find_saved_cap(dev
, PCI_CAP_ID_MSIX
);
436 save
= *((u16
*)&save_state
->data
[0]);
437 pci_remove_saved_cap(save_state
);
440 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
444 /* route the table */
446 if (msi_lookup_irq(dev
, PCI_CAP_ID_MSIX
))
448 irq
= head
= dev
->irq
;
449 while (head
!= tail
) {
450 entry
= msi_desc
[irq
];
451 write_msi_msg(irq
, &entry
->msg_save
);
453 tail
= msi_desc
[irq
]->link
.tail
;
458 pci_write_config_word(dev
, msi_control_reg(pos
), save
);
459 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSIX
);
464 * msi_capability_init - configure device's MSI capability structure
465 * @dev: pointer to the pci_dev data structure of MSI device function
467 * Setup the MSI capability structure of device function with a single
468 * MSI irq, regardless of device function is capable of handling
469 * multiple messages. A return of zero indicates the successful setup
470 * of an entry zero with the new MSI irq or non-zero for otherwise.
472 static int msi_capability_init(struct pci_dev
*dev
)
475 struct msi_desc
*entry
;
479 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
480 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
481 /* MSI Entry Initialization */
482 irq
= create_msi_irq();
486 entry
= get_irq_data(irq
);
487 entry
->link
.head
= irq
;
488 entry
->link
.tail
= irq
;
489 entry
->msi_attrib
.type
= PCI_CAP_ID_MSI
;
490 entry
->msi_attrib
.is_64
= is_64bit_address(control
);
491 entry
->msi_attrib
.entry_nr
= 0;
492 entry
->msi_attrib
.maskbit
= is_mask_bit_support(control
);
493 entry
->msi_attrib
.default_irq
= dev
->irq
; /* Save IOAPIC IRQ */
494 entry
->msi_attrib
.pos
= pos
;
495 if (is_mask_bit_support(control
)) {
496 entry
->mask_base
= (void __iomem
*)(long)msi_mask_bits_reg(pos
,
497 is_64bit_address(control
));
500 if (entry
->msi_attrib
.maskbit
) {
501 unsigned int maskbits
, temp
;
502 /* All MSIs are unmasked by default, Mask them all */
503 pci_read_config_dword(dev
,
504 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
506 temp
= (1 << multi_msi_capable(control
));
507 temp
= ((temp
- 1) & ~temp
);
509 pci_write_config_dword(dev
,
510 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
513 /* Configure MSI capability structure */
514 status
= arch_setup_msi_irq(irq
, dev
);
516 destroy_msi_irq(irq
);
520 attach_msi_entry(entry
, irq
);
521 /* Set MSI enabled bits */
522 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSI
);
529 * msix_capability_init - configure device's MSI-X capability
530 * @dev: pointer to the pci_dev data structure of MSI-X device function
531 * @entries: pointer to an array of struct msix_entry entries
532 * @nvec: number of @entries
534 * Setup the MSI-X capability structure of device function with a
535 * single MSI-X irq. A return of zero indicates the successful setup of
536 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
538 static int msix_capability_init(struct pci_dev
*dev
,
539 struct msix_entry
*entries
, int nvec
)
541 struct msi_desc
*head
= NULL
, *tail
= NULL
, *entry
= NULL
;
543 int irq
, pos
, i
, j
, nr_entries
, temp
= 0;
544 unsigned long phys_addr
;
550 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
551 /* Request & Map MSI-X table region */
552 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
553 nr_entries
= multi_msix_capable(control
);
555 pci_read_config_dword(dev
, msix_table_offset_reg(pos
), &table_offset
);
556 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
557 table_offset
&= ~PCI_MSIX_FLAGS_BIRMASK
;
558 phys_addr
= pci_resource_start (dev
, bir
) + table_offset
;
559 base
= ioremap_nocache(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
563 /* MSI-X Table Initialization */
564 for (i
= 0; i
< nvec
; i
++) {
565 irq
= create_msi_irq();
569 entry
= get_irq_data(irq
);
570 j
= entries
[i
].entry
;
571 entries
[i
].vector
= irq
;
572 entry
->msi_attrib
.type
= PCI_CAP_ID_MSIX
;
573 entry
->msi_attrib
.is_64
= 1;
574 entry
->msi_attrib
.entry_nr
= j
;
575 entry
->msi_attrib
.maskbit
= 1;
576 entry
->msi_attrib
.default_irq
= dev
->irq
;
577 entry
->msi_attrib
.pos
= pos
;
579 entry
->mask_base
= base
;
581 entry
->link
.head
= irq
;
582 entry
->link
.tail
= irq
;
585 entry
->link
.head
= temp
;
586 entry
->link
.tail
= tail
->link
.tail
;
587 tail
->link
.tail
= irq
;
588 head
->link
.head
= irq
;
592 /* Configure MSI-X capability structure */
593 status
= arch_setup_msi_irq(irq
, dev
);
595 destroy_msi_irq(irq
);
599 attach_msi_entry(entry
, irq
);
604 for (; i
>= 0; i
--) {
605 irq
= (entries
+ i
)->vector
;
606 msi_free_irq(dev
, irq
);
607 (entries
+ i
)->vector
= 0;
609 /* If we had some success report the number of irqs
610 * we succeeded in setting up.
616 /* Set MSI-X enabled bits */
617 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSIX
);
623 * pci_msi_supported - check whether MSI may be enabled on device
624 * @dev: pointer to the pci_dev data structure of MSI device function
626 * Look at global flags, the device itself, and its parent busses
627 * to return 0 if MSI are supported for the device.
630 int pci_msi_supported(struct pci_dev
* dev
)
634 /* MSI must be globally enabled and supported by the device */
635 if (!pci_msi_enable
|| !dev
|| dev
->no_msi
)
638 /* Any bridge which does NOT route MSI transactions from it's
639 * secondary bus to it's primary bus must set NO_MSI flag on
640 * the secondary pci_bus.
641 * We expect only arch-specific PCI host bus controller driver
642 * or quirks for specific PCI bridges to be setting NO_MSI.
644 for (bus
= dev
->bus
; bus
; bus
= bus
->parent
)
645 if (bus
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
652 * pci_enable_msi - configure device's MSI capability structure
653 * @dev: pointer to the pci_dev data structure of MSI device function
655 * Setup the MSI capability structure of device function with
656 * a single MSI irq upon its software driver call to request for
657 * MSI mode enabled on its hardware device function. A return of zero
658 * indicates the successful setup of an entry zero with the new MSI
659 * irq or non-zero for otherwise.
661 int pci_enable_msi(struct pci_dev
* dev
)
663 int pos
, temp
, status
;
665 if (pci_msi_supported(dev
) < 0)
674 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
678 WARN_ON(!msi_lookup_irq(dev
, PCI_CAP_ID_MSI
));
680 /* Check whether driver already requested for MSI-X irqs */
681 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
682 if (pos
> 0 && !msi_lookup_irq(dev
, PCI_CAP_ID_MSIX
)) {
683 printk(KERN_INFO
"PCI: %s: Can't enable MSI. "
684 "Device already has MSI-X irq assigned\n",
689 status
= msi_capability_init(dev
);
693 void pci_disable_msi(struct pci_dev
* dev
)
695 struct msi_desc
*entry
;
696 int pos
, default_irq
;
705 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
709 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
710 if (!(control
& PCI_MSI_FLAGS_ENABLE
))
713 disable_msi_mode(dev
, pos
, PCI_CAP_ID_MSI
);
715 spin_lock_irqsave(&msi_lock
, flags
);
716 entry
= msi_desc
[dev
->irq
];
717 if (!entry
|| !entry
->dev
|| entry
->msi_attrib
.type
!= PCI_CAP_ID_MSI
) {
718 spin_unlock_irqrestore(&msi_lock
, flags
);
721 if (irq_has_action(dev
->irq
)) {
722 spin_unlock_irqrestore(&msi_lock
, flags
);
723 printk(KERN_WARNING
"PCI: %s: pci_disable_msi() called without "
724 "free_irq() on MSI irq %d\n",
725 pci_name(dev
), dev
->irq
);
726 BUG_ON(irq_has_action(dev
->irq
));
728 default_irq
= entry
->msi_attrib
.default_irq
;
729 spin_unlock_irqrestore(&msi_lock
, flags
);
730 msi_free_irq(dev
, dev
->irq
);
732 /* Restore dev->irq to its default pin-assertion irq */
733 dev
->irq
= default_irq
;
737 static int msi_free_irq(struct pci_dev
* dev
, int irq
)
739 struct msi_desc
*entry
;
740 int head
, entry_nr
, type
;
744 arch_teardown_msi_irq(irq
);
746 spin_lock_irqsave(&msi_lock
, flags
);
747 entry
= msi_desc
[irq
];
748 if (!entry
|| entry
->dev
!= dev
) {
749 spin_unlock_irqrestore(&msi_lock
, flags
);
752 type
= entry
->msi_attrib
.type
;
753 entry_nr
= entry
->msi_attrib
.entry_nr
;
754 head
= entry
->link
.head
;
755 base
= entry
->mask_base
;
756 msi_desc
[entry
->link
.head
]->link
.tail
= entry
->link
.tail
;
757 msi_desc
[entry
->link
.tail
]->link
.head
= entry
->link
.head
;
759 msi_desc
[irq
] = NULL
;
760 spin_unlock_irqrestore(&msi_lock
, flags
);
762 destroy_msi_irq(irq
);
764 if (type
== PCI_CAP_ID_MSIX
) {
765 writel(1, base
+ entry_nr
* PCI_MSIX_ENTRY_SIZE
+
766 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
);
776 * pci_enable_msix - configure device's MSI-X capability structure
777 * @dev: pointer to the pci_dev data structure of MSI-X device function
778 * @entries: pointer to an array of MSI-X entries
779 * @nvec: number of MSI-X irqs requested for allocation by device driver
781 * Setup the MSI-X capability structure of device function with the number
782 * of requested irqs upon its software driver call to request for
783 * MSI-X mode enabled on its hardware device function. A return of zero
784 * indicates the successful configuration of MSI-X capability structure
785 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
786 * Or a return of > 0 indicates that driver request is exceeding the number
787 * of irqs available. Driver should use the returned value to re-send
790 int pci_enable_msix(struct pci_dev
* dev
, struct msix_entry
*entries
, int nvec
)
792 int status
, pos
, nr_entries
;
796 if (!entries
|| pci_msi_supported(dev
) < 0)
803 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
807 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
808 nr_entries
= multi_msix_capable(control
);
809 if (nvec
> nr_entries
)
812 /* Check for any invalid entries */
813 for (i
= 0; i
< nvec
; i
++) {
814 if (entries
[i
].entry
>= nr_entries
)
815 return -EINVAL
; /* invalid entry */
816 for (j
= i
+ 1; j
< nvec
; j
++) {
817 if (entries
[i
].entry
== entries
[j
].entry
)
818 return -EINVAL
; /* duplicate entry */
822 WARN_ON(!msi_lookup_irq(dev
, PCI_CAP_ID_MSIX
));
824 /* Check whether driver already requested for MSI irq */
825 if (pci_find_capability(dev
, PCI_CAP_ID_MSI
) > 0 &&
826 !msi_lookup_irq(dev
, PCI_CAP_ID_MSI
)) {
827 printk(KERN_INFO
"PCI: %s: Can't enable MSI-X. "
828 "Device already has an MSI irq assigned\n",
833 status
= msix_capability_init(dev
, entries
, nvec
);
837 void pci_disable_msix(struct pci_dev
* dev
)
847 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
851 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
852 if (!(control
& PCI_MSIX_FLAGS_ENABLE
))
855 disable_msi_mode(dev
, pos
, PCI_CAP_ID_MSIX
);
858 if (!msi_lookup_irq(dev
, PCI_CAP_ID_MSIX
)) {
859 int irq
, head
, tail
= 0, warning
= 0;
862 irq
= head
= dev
->irq
;
863 dev
->irq
= temp
; /* Restore pin IRQ */
864 while (head
!= tail
) {
865 spin_lock_irqsave(&msi_lock
, flags
);
866 tail
= msi_desc
[irq
]->link
.tail
;
867 spin_unlock_irqrestore(&msi_lock
, flags
);
868 if (irq_has_action(irq
))
870 else if (irq
!= head
) /* Release MSI-X irq */
871 msi_free_irq(dev
, irq
);
874 msi_free_irq(dev
, irq
);
876 printk(KERN_WARNING
"PCI: %s: pci_disable_msix() called without "
877 "free_irq() on all MSI-X irqs\n",
885 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
886 * @dev: pointer to the pci_dev data structure of MSI(X) device function
888 * Being called during hotplug remove, from which the device function
889 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
890 * allocated for this device function, are reclaimed to unused state,
891 * which may be used later on.
893 void msi_remove_pci_irq_vectors(struct pci_dev
* dev
)
898 if (!pci_msi_enable
|| !dev
)
901 temp
= dev
->irq
; /* Save IOAPIC IRQ */
902 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
903 if (pos
> 0 && !msi_lookup_irq(dev
, PCI_CAP_ID_MSI
)) {
904 if (irq_has_action(dev
->irq
)) {
905 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
906 "called without free_irq() on MSI irq %d\n",
907 pci_name(dev
), dev
->irq
);
908 BUG_ON(irq_has_action(dev
->irq
));
909 } else /* Release MSI irq assigned to this device */
910 msi_free_irq(dev
, dev
->irq
);
911 dev
->irq
= temp
; /* Restore IOAPIC IRQ */
913 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
914 if (pos
> 0 && !msi_lookup_irq(dev
, PCI_CAP_ID_MSIX
)) {
915 int irq
, head
, tail
= 0, warning
= 0;
916 void __iomem
*base
= NULL
;
918 irq
= head
= dev
->irq
;
919 while (head
!= tail
) {
920 spin_lock_irqsave(&msi_lock
, flags
);
921 tail
= msi_desc
[irq
]->link
.tail
;
922 base
= msi_desc
[irq
]->mask_base
;
923 spin_unlock_irqrestore(&msi_lock
, flags
);
924 if (irq_has_action(irq
))
926 else if (irq
!= head
) /* Release MSI-X irq */
927 msi_free_irq(dev
, irq
);
930 msi_free_irq(dev
, irq
);
933 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
934 "called without free_irq() on all MSI-X irqs\n",
938 dev
->irq
= temp
; /* Restore IOAPIC IRQ */
942 void pci_no_msi(void)
947 EXPORT_SYMBOL(pci_enable_msi
);
948 EXPORT_SYMBOL(pci_disable_msi
);
949 EXPORT_SYMBOL(pci_enable_msix
);
950 EXPORT_SYMBOL(pci_disable_msix
);