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 struct kmem_cache
* msi_cachep
;
29 static int pci_msi_enable
= 1;
31 static int msi_cache_init(void)
33 msi_cachep
= kmem_cache_create("msi_cache", sizeof(struct msi_desc
),
34 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
41 static void msi_set_enable(struct pci_dev
*dev
, int enable
)
46 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
48 pci_read_config_word(dev
, pos
+ PCI_MSI_FLAGS
, &control
);
49 control
&= ~PCI_MSI_FLAGS_ENABLE
;
51 control
|= PCI_MSI_FLAGS_ENABLE
;
52 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
56 static void msix_set_enable(struct pci_dev
*dev
, int enable
)
61 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
63 pci_read_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, &control
);
64 control
&= ~PCI_MSIX_FLAGS_ENABLE
;
66 control
|= PCI_MSIX_FLAGS_ENABLE
;
67 pci_write_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, control
);
71 static void msi_set_mask_bit(unsigned int irq
, int flag
)
73 struct msi_desc
*entry
;
75 entry
= get_irq_msi(irq
);
76 BUG_ON(!entry
|| !entry
->dev
);
77 switch (entry
->msi_attrib
.type
) {
79 if (entry
->msi_attrib
.maskbit
) {
83 pos
= (long)entry
->mask_base
;
84 pci_read_config_dword(entry
->dev
, pos
, &mask_bits
);
87 pci_write_config_dword(entry
->dev
, pos
, mask_bits
);
89 msi_set_enable(entry
->dev
, !flag
);
94 int offset
= entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
+
95 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
;
96 writel(flag
, entry
->mask_base
+ offset
);
97 readl(entry
->mask_base
+ offset
);
104 entry
->msi_attrib
.masked
= !!flag
;
107 void read_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
109 struct msi_desc
*entry
= get_irq_msi(irq
);
110 switch(entry
->msi_attrib
.type
) {
113 struct pci_dev
*dev
= entry
->dev
;
114 int pos
= entry
->msi_attrib
.pos
;
117 pci_read_config_dword(dev
, msi_lower_address_reg(pos
),
119 if (entry
->msi_attrib
.is_64
) {
120 pci_read_config_dword(dev
, msi_upper_address_reg(pos
),
122 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
125 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
130 case PCI_CAP_ID_MSIX
:
133 base
= entry
->mask_base
+
134 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
136 msg
->address_lo
= readl(base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
137 msg
->address_hi
= readl(base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
138 msg
->data
= readl(base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
146 void write_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
148 struct msi_desc
*entry
= get_irq_msi(irq
);
149 switch (entry
->msi_attrib
.type
) {
152 struct pci_dev
*dev
= entry
->dev
;
153 int pos
= entry
->msi_attrib
.pos
;
155 pci_write_config_dword(dev
, msi_lower_address_reg(pos
),
157 if (entry
->msi_attrib
.is_64
) {
158 pci_write_config_dword(dev
, msi_upper_address_reg(pos
),
160 pci_write_config_word(dev
, msi_data_reg(pos
, 1),
163 pci_write_config_word(dev
, msi_data_reg(pos
, 0),
168 case PCI_CAP_ID_MSIX
:
171 base
= entry
->mask_base
+
172 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
174 writel(msg
->address_lo
,
175 base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
176 writel(msg
->address_hi
,
177 base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
178 writel(msg
->data
, base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
187 void mask_msi_irq(unsigned int irq
)
189 msi_set_mask_bit(irq
, 1);
192 void unmask_msi_irq(unsigned int irq
)
194 msi_set_mask_bit(irq
, 0);
197 static int msi_free_irq(struct pci_dev
* dev
, int irq
);
199 static int msi_init(void)
201 static int status
= -ENOMEM
;
206 status
= msi_cache_init();
209 printk(KERN_WARNING
"PCI: MSI cache init failed\n");
216 static struct msi_desc
* alloc_msi_entry(void)
218 struct msi_desc
*entry
;
220 entry
= kmem_cache_zalloc(msi_cachep
, GFP_KERNEL
);
224 entry
->link
.tail
= entry
->link
.head
= 0; /* single message */
231 static void __pci_restore_msi_state(struct pci_dev
*dev
)
235 struct msi_desc
*entry
;
237 if (!dev
->msi_enabled
)
240 entry
= get_irq_msi(dev
->irq
);
241 pos
= entry
->msi_attrib
.pos
;
243 pci_intx(dev
, 0); /* disable intx */
244 msi_set_enable(dev
, 0);
245 write_msi_msg(dev
->irq
, &entry
->msg
);
246 if (entry
->msi_attrib
.maskbit
)
247 msi_set_mask_bit(dev
->irq
, entry
->msi_attrib
.masked
);
249 pci_read_config_word(dev
, pos
+ PCI_MSI_FLAGS
, &control
);
250 control
&= ~(PCI_MSI_FLAGS_QSIZE
| PCI_MSI_FLAGS_ENABLE
);
251 if (entry
->msi_attrib
.maskbit
|| !entry
->msi_attrib
.masked
)
252 control
|= PCI_MSI_FLAGS_ENABLE
;
253 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
256 static void __pci_restore_msix_state(struct pci_dev
*dev
)
259 int irq
, head
, tail
= 0;
260 struct msi_desc
*entry
;
263 if (!dev
->msix_enabled
)
266 /* route the table */
267 pci_intx(dev
, 0); /* disable intx */
268 msix_set_enable(dev
, 0);
269 irq
= head
= dev
->first_msi_irq
;
270 entry
= get_irq_msi(irq
);
271 pos
= entry
->msi_attrib
.pos
;
272 while (head
!= tail
) {
273 entry
= get_irq_msi(irq
);
274 write_msi_msg(irq
, &entry
->msg
);
275 msi_set_mask_bit(irq
, entry
->msi_attrib
.masked
);
277 tail
= entry
->link
.tail
;
281 pci_read_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, &control
);
282 control
&= ~PCI_MSIX_FLAGS_MASKALL
;
283 control
|= PCI_MSIX_FLAGS_ENABLE
;
284 pci_write_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, control
);
287 void pci_restore_msi_state(struct pci_dev
*dev
)
289 __pci_restore_msi_state(dev
);
290 __pci_restore_msix_state(dev
);
292 #endif /* CONFIG_PM */
295 * msi_capability_init - configure device's MSI capability structure
296 * @dev: pointer to the pci_dev data structure of MSI device function
298 * Setup the MSI capability structure of device function with a single
299 * MSI irq, regardless of device function is capable of handling
300 * multiple messages. A return of zero indicates the successful setup
301 * of an entry zero with the new MSI irq or non-zero for otherwise.
303 static int msi_capability_init(struct pci_dev
*dev
)
305 struct msi_desc
*entry
;
309 msi_set_enable(dev
, 0); /* Ensure msi is disabled as I set it up */
311 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
312 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
313 /* MSI Entry Initialization */
314 entry
= alloc_msi_entry();
318 entry
->msi_attrib
.type
= PCI_CAP_ID_MSI
;
319 entry
->msi_attrib
.is_64
= is_64bit_address(control
);
320 entry
->msi_attrib
.entry_nr
= 0;
321 entry
->msi_attrib
.maskbit
= is_mask_bit_support(control
);
322 entry
->msi_attrib
.masked
= 1;
323 entry
->msi_attrib
.default_irq
= dev
->irq
; /* Save IOAPIC IRQ */
324 entry
->msi_attrib
.pos
= pos
;
325 if (is_mask_bit_support(control
)) {
326 entry
->mask_base
= (void __iomem
*)(long)msi_mask_bits_reg(pos
,
327 is_64bit_address(control
));
330 if (entry
->msi_attrib
.maskbit
) {
331 unsigned int maskbits
, temp
;
332 /* All MSIs are unmasked by default, Mask them all */
333 pci_read_config_dword(dev
,
334 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
336 temp
= (1 << multi_msi_capable(control
));
337 temp
= ((temp
- 1) & ~temp
);
339 pci_write_config_dword(dev
,
340 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
343 /* Configure MSI capability structure */
344 irq
= arch_setup_msi_irq(dev
, entry
);
346 kmem_cache_free(msi_cachep
, entry
);
349 entry
->link
.head
= irq
;
350 entry
->link
.tail
= irq
;
351 dev
->first_msi_irq
= irq
;
352 set_irq_msi(irq
, entry
);
354 /* Set MSI enabled bits */
355 pci_intx(dev
, 0); /* disable intx */
356 msi_set_enable(dev
, 1);
357 dev
->msi_enabled
= 1;
364 * msix_capability_init - configure device's MSI-X capability
365 * @dev: pointer to the pci_dev data structure of MSI-X device function
366 * @entries: pointer to an array of struct msix_entry entries
367 * @nvec: number of @entries
369 * Setup the MSI-X capability structure of device function with a
370 * single MSI-X irq. A return of zero indicates the successful setup of
371 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
373 static int msix_capability_init(struct pci_dev
*dev
,
374 struct msix_entry
*entries
, int nvec
)
376 struct msi_desc
*head
= NULL
, *tail
= NULL
, *entry
= NULL
;
377 int irq
, pos
, i
, j
, nr_entries
, temp
= 0;
378 unsigned long phys_addr
;
384 msix_set_enable(dev
, 0);/* Ensure msix is disabled as I set it up */
386 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
387 /* Request & Map MSI-X table region */
388 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
389 nr_entries
= multi_msix_capable(control
);
391 pci_read_config_dword(dev
, msix_table_offset_reg(pos
), &table_offset
);
392 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
393 table_offset
&= ~PCI_MSIX_FLAGS_BIRMASK
;
394 phys_addr
= pci_resource_start (dev
, bir
) + table_offset
;
395 base
= ioremap_nocache(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
399 /* MSI-X Table Initialization */
400 for (i
= 0; i
< nvec
; i
++) {
401 entry
= alloc_msi_entry();
405 j
= entries
[i
].entry
;
406 entry
->msi_attrib
.type
= PCI_CAP_ID_MSIX
;
407 entry
->msi_attrib
.is_64
= 1;
408 entry
->msi_attrib
.entry_nr
= j
;
409 entry
->msi_attrib
.maskbit
= 1;
410 entry
->msi_attrib
.masked
= 1;
411 entry
->msi_attrib
.default_irq
= dev
->irq
;
412 entry
->msi_attrib
.pos
= pos
;
414 entry
->mask_base
= base
;
416 /* Configure MSI-X capability structure */
417 irq
= arch_setup_msi_irq(dev
, entry
);
419 kmem_cache_free(msi_cachep
, entry
);
422 entries
[i
].vector
= irq
;
424 entry
->link
.head
= irq
;
425 entry
->link
.tail
= irq
;
428 entry
->link
.head
= temp
;
429 entry
->link
.tail
= tail
->link
.tail
;
430 tail
->link
.tail
= irq
;
431 head
->link
.head
= irq
;
436 set_irq_msi(irq
, entry
);
441 for (; i
>= 0; i
--) {
442 irq
= (entries
+ i
)->vector
;
443 msi_free_irq(dev
, irq
);
444 (entries
+ i
)->vector
= 0;
446 /* If we had some success report the number of irqs
447 * we succeeded in setting up.
453 dev
->first_msi_irq
= entries
[0].vector
;
454 /* Set MSI-X enabled bits */
455 pci_intx(dev
, 0); /* disable intx */
456 msix_set_enable(dev
, 1);
457 dev
->msix_enabled
= 1;
463 * pci_msi_supported - check whether MSI may be enabled on device
464 * @dev: pointer to the pci_dev data structure of MSI device function
466 * Look at global flags, the device itself, and its parent busses
467 * to return 0 if MSI are supported for the device.
470 int pci_msi_supported(struct pci_dev
* dev
)
474 /* MSI must be globally enabled and supported by the device */
475 if (!pci_msi_enable
|| !dev
|| dev
->no_msi
)
478 /* Any bridge which does NOT route MSI transactions from it's
479 * secondary bus to it's primary bus must set NO_MSI flag on
480 * the secondary pci_bus.
481 * We expect only arch-specific PCI host bus controller driver
482 * or quirks for specific PCI bridges to be setting NO_MSI.
484 for (bus
= dev
->bus
; bus
; bus
= bus
->parent
)
485 if (bus
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
492 * pci_enable_msi - configure device's MSI capability structure
493 * @dev: pointer to the pci_dev data structure of MSI device function
495 * Setup the MSI capability structure of device function with
496 * a single MSI irq upon its software driver call to request for
497 * MSI mode enabled on its hardware device function. A return of zero
498 * indicates the successful setup of an entry zero with the new MSI
499 * irq or non-zero for otherwise.
501 int pci_enable_msi(struct pci_dev
* dev
)
505 if (pci_msi_supported(dev
) < 0)
512 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
516 WARN_ON(!!dev
->msi_enabled
);
518 /* Check whether driver already requested for MSI-X irqs */
519 if (dev
->msix_enabled
) {
520 printk(KERN_INFO
"PCI: %s: Can't enable MSI. "
521 "Device already has MSI-X enabled\n",
525 status
= msi_capability_init(dev
);
529 void pci_disable_msi(struct pci_dev
* dev
)
531 struct msi_desc
*entry
;
539 if (!dev
->msi_enabled
)
542 msi_set_enable(dev
, 0);
543 pci_intx(dev
, 1); /* enable intx */
544 dev
->msi_enabled
= 0;
546 entry
= get_irq_msi(dev
->first_msi_irq
);
547 if (!entry
|| !entry
->dev
|| entry
->msi_attrib
.type
!= PCI_CAP_ID_MSI
) {
550 if (irq_has_action(dev
->first_msi_irq
)) {
551 printk(KERN_WARNING
"PCI: %s: pci_disable_msi() called without "
552 "free_irq() on MSI irq %d\n",
553 pci_name(dev
), dev
->first_msi_irq
);
554 BUG_ON(irq_has_action(dev
->first_msi_irq
));
556 default_irq
= entry
->msi_attrib
.default_irq
;
557 msi_free_irq(dev
, dev
->first_msi_irq
);
559 /* Restore dev->irq to its default pin-assertion irq */
560 dev
->irq
= default_irq
;
562 dev
->first_msi_irq
= 0;
565 static int msi_free_irq(struct pci_dev
* dev
, int irq
)
567 struct msi_desc
*entry
;
568 int head
, entry_nr
, type
;
571 entry
= get_irq_msi(irq
);
572 if (!entry
|| entry
->dev
!= dev
) {
575 type
= entry
->msi_attrib
.type
;
576 entry_nr
= entry
->msi_attrib
.entry_nr
;
577 head
= entry
->link
.head
;
578 base
= entry
->mask_base
;
579 get_irq_msi(entry
->link
.head
)->link
.tail
= entry
->link
.tail
;
580 get_irq_msi(entry
->link
.tail
)->link
.head
= entry
->link
.head
;
582 arch_teardown_msi_irq(irq
);
583 kmem_cache_free(msi_cachep
, entry
);
585 if (type
== PCI_CAP_ID_MSIX
) {
586 writel(1, base
+ entry_nr
* PCI_MSIX_ENTRY_SIZE
+
587 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
);
597 * pci_enable_msix - configure device's MSI-X capability structure
598 * @dev: pointer to the pci_dev data structure of MSI-X device function
599 * @entries: pointer to an array of MSI-X entries
600 * @nvec: number of MSI-X irqs requested for allocation by device driver
602 * Setup the MSI-X capability structure of device function with the number
603 * of requested irqs upon its software driver call to request for
604 * MSI-X mode enabled on its hardware device function. A return of zero
605 * indicates the successful configuration of MSI-X capability structure
606 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
607 * Or a return of > 0 indicates that driver request is exceeding the number
608 * of irqs available. Driver should use the returned value to re-send
611 int pci_enable_msix(struct pci_dev
* dev
, struct msix_entry
*entries
, int nvec
)
613 int status
, pos
, nr_entries
;
617 if (!entries
|| pci_msi_supported(dev
) < 0)
624 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
628 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
629 nr_entries
= multi_msix_capable(control
);
630 if (nvec
> nr_entries
)
633 /* Check for any invalid entries */
634 for (i
= 0; i
< nvec
; i
++) {
635 if (entries
[i
].entry
>= nr_entries
)
636 return -EINVAL
; /* invalid entry */
637 for (j
= i
+ 1; j
< nvec
; j
++) {
638 if (entries
[i
].entry
== entries
[j
].entry
)
639 return -EINVAL
; /* duplicate entry */
642 WARN_ON(!!dev
->msix_enabled
);
644 /* Check whether driver already requested for MSI irq */
645 if (dev
->msi_enabled
) {
646 printk(KERN_INFO
"PCI: %s: Can't enable MSI-X. "
647 "Device already has an MSI irq assigned\n",
651 status
= msix_capability_init(dev
, entries
, nvec
);
655 void pci_disable_msix(struct pci_dev
* dev
)
657 int irq
, head
, tail
= 0, warning
= 0;
664 if (!dev
->msix_enabled
)
667 msix_set_enable(dev
, 0);
668 pci_intx(dev
, 1); /* enable intx */
669 dev
->msix_enabled
= 0;
671 irq
= head
= dev
->first_msi_irq
;
672 while (head
!= tail
) {
673 tail
= get_irq_msi(irq
)->link
.tail
;
674 if (irq_has_action(irq
))
676 else if (irq
!= head
) /* Release MSI-X irq */
677 msi_free_irq(dev
, irq
);
680 msi_free_irq(dev
, irq
);
682 printk(KERN_WARNING
"PCI: %s: pci_disable_msix() called without "
683 "free_irq() on all MSI-X irqs\n",
687 dev
->first_msi_irq
= 0;
691 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
692 * @dev: pointer to the pci_dev data structure of MSI(X) device function
694 * Being called during hotplug remove, from which the device function
695 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
696 * allocated for this device function, are reclaimed to unused state,
697 * which may be used later on.
699 void msi_remove_pci_irq_vectors(struct pci_dev
* dev
)
701 if (!pci_msi_enable
|| !dev
)
704 if (dev
->msi_enabled
) {
705 if (irq_has_action(dev
->first_msi_irq
)) {
706 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
707 "called without free_irq() on MSI irq %d\n",
708 pci_name(dev
), dev
->first_msi_irq
);
709 BUG_ON(irq_has_action(dev
->first_msi_irq
));
710 } else /* Release MSI irq assigned to this device */
711 msi_free_irq(dev
, dev
->first_msi_irq
);
713 if (dev
->msix_enabled
) {
714 int irq
, head
, tail
= 0, warning
= 0;
715 void __iomem
*base
= NULL
;
717 irq
= head
= dev
->first_msi_irq
;
718 while (head
!= tail
) {
719 tail
= get_irq_msi(irq
)->link
.tail
;
720 base
= get_irq_msi(irq
)->mask_base
;
721 if (irq_has_action(irq
))
723 else if (irq
!= head
) /* Release MSI-X irq */
724 msi_free_irq(dev
, irq
);
727 msi_free_irq(dev
, irq
);
730 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
731 "called without free_irq() on all MSI-X irqs\n",
738 void pci_no_msi(void)
743 EXPORT_SYMBOL(pci_enable_msi
);
744 EXPORT_SYMBOL(pci_disable_msi
);
745 EXPORT_SYMBOL(pci_enable_msix
);
746 EXPORT_SYMBOL(pci_disable_msix
);