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
);
103 entry
->msi_attrib
.masked
= !!flag
;
106 void read_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
108 struct msi_desc
*entry
= get_irq_msi(irq
);
109 switch(entry
->msi_attrib
.type
) {
112 struct pci_dev
*dev
= entry
->dev
;
113 int pos
= entry
->msi_attrib
.pos
;
116 pci_read_config_dword(dev
, msi_lower_address_reg(pos
),
118 if (entry
->msi_attrib
.is_64
) {
119 pci_read_config_dword(dev
, msi_upper_address_reg(pos
),
121 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
124 pci_read_config_word(dev
, msi_data_reg(pos
, 1), &data
);
129 case PCI_CAP_ID_MSIX
:
132 base
= entry
->mask_base
+
133 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
135 msg
->address_lo
= readl(base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
136 msg
->address_hi
= readl(base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
137 msg
->data
= readl(base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
145 void write_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
147 struct msi_desc
*entry
= get_irq_msi(irq
);
148 switch (entry
->msi_attrib
.type
) {
151 struct pci_dev
*dev
= entry
->dev
;
152 int pos
= entry
->msi_attrib
.pos
;
154 pci_write_config_dword(dev
, msi_lower_address_reg(pos
),
156 if (entry
->msi_attrib
.is_64
) {
157 pci_write_config_dword(dev
, msi_upper_address_reg(pos
),
159 pci_write_config_word(dev
, msi_data_reg(pos
, 1),
162 pci_write_config_word(dev
, msi_data_reg(pos
, 0),
167 case PCI_CAP_ID_MSIX
:
170 base
= entry
->mask_base
+
171 entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
173 writel(msg
->address_lo
,
174 base
+ PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
175 writel(msg
->address_hi
,
176 base
+ PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
177 writel(msg
->data
, base
+ PCI_MSIX_ENTRY_DATA_OFFSET
);
186 void mask_msi_irq(unsigned int irq
)
188 msi_set_mask_bit(irq
, 1);
191 void unmask_msi_irq(unsigned int irq
)
193 msi_set_mask_bit(irq
, 0);
196 static int msi_free_irq(struct pci_dev
* dev
, int irq
);
198 static int msi_init(void)
200 static int status
= -ENOMEM
;
205 status
= msi_cache_init();
208 printk(KERN_WARNING
"PCI: MSI cache init failed\n");
215 static struct msi_desc
* alloc_msi_entry(void)
217 struct msi_desc
*entry
;
219 entry
= kmem_cache_zalloc(msi_cachep
, GFP_KERNEL
);
223 entry
->link
.tail
= entry
->link
.head
= 0; /* single message */
230 static void __pci_restore_msi_state(struct pci_dev
*dev
)
234 struct msi_desc
*entry
;
236 if (!dev
->msi_enabled
)
239 entry
= get_irq_msi(dev
->irq
);
240 pos
= entry
->msi_attrib
.pos
;
242 pci_intx(dev
, 0); /* disable intx */
243 msi_set_enable(dev
, 0);
244 write_msi_msg(dev
->irq
, &entry
->msg
);
245 if (entry
->msi_attrib
.maskbit
)
246 msi_set_mask_bit(dev
->irq
, entry
->msi_attrib
.masked
);
248 pci_read_config_word(dev
, pos
+ PCI_MSI_FLAGS
, &control
);
249 control
&= ~(PCI_MSI_FLAGS_QSIZE
| PCI_MSI_FLAGS_ENABLE
);
250 if (entry
->msi_attrib
.maskbit
|| !entry
->msi_attrib
.masked
)
251 control
|= PCI_MSI_FLAGS_ENABLE
;
252 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
255 static void __pci_restore_msix_state(struct pci_dev
*dev
)
258 int irq
, head
, tail
= 0;
259 struct msi_desc
*entry
;
262 if (!dev
->msix_enabled
)
265 /* route the table */
266 pci_intx(dev
, 0); /* disable intx */
267 msix_set_enable(dev
, 0);
268 irq
= head
= dev
->first_msi_irq
;
269 entry
= get_irq_msi(irq
);
270 pos
= entry
->msi_attrib
.pos
;
271 while (head
!= tail
) {
272 entry
= get_irq_msi(irq
);
273 write_msi_msg(irq
, &entry
->msg
);
274 msi_set_mask_bit(irq
, entry
->msi_attrib
.masked
);
276 tail
= entry
->link
.tail
;
280 pci_read_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, &control
);
281 control
&= ~PCI_MSIX_FLAGS_MASKALL
;
282 control
|= PCI_MSIX_FLAGS_ENABLE
;
283 pci_write_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, control
);
286 void pci_restore_msi_state(struct pci_dev
*dev
)
288 __pci_restore_msi_state(dev
);
289 __pci_restore_msix_state(dev
);
291 #endif /* CONFIG_PM */
294 * msi_capability_init - configure device's MSI capability structure
295 * @dev: pointer to the pci_dev data structure of MSI device function
297 * Setup the MSI capability structure of device function with a single
298 * MSI irq, regardless of device function is capable of handling
299 * multiple messages. A return of zero indicates the successful setup
300 * of an entry zero with the new MSI irq or non-zero for otherwise.
302 static int msi_capability_init(struct pci_dev
*dev
)
304 struct msi_desc
*entry
;
308 msi_set_enable(dev
, 0); /* Ensure msi is disabled as I set it up */
310 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
311 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
312 /* MSI Entry Initialization */
313 entry
= alloc_msi_entry();
317 entry
->msi_attrib
.type
= PCI_CAP_ID_MSI
;
318 entry
->msi_attrib
.is_64
= is_64bit_address(control
);
319 entry
->msi_attrib
.entry_nr
= 0;
320 entry
->msi_attrib
.maskbit
= is_mask_bit_support(control
);
321 entry
->msi_attrib
.masked
= 1;
322 entry
->msi_attrib
.default_irq
= dev
->irq
; /* Save IOAPIC IRQ */
323 entry
->msi_attrib
.pos
= pos
;
324 if (is_mask_bit_support(control
)) {
325 entry
->mask_base
= (void __iomem
*)(long)msi_mask_bits_reg(pos
,
326 is_64bit_address(control
));
329 if (entry
->msi_attrib
.maskbit
) {
330 unsigned int maskbits
, temp
;
331 /* All MSIs are unmasked by default, Mask them all */
332 pci_read_config_dword(dev
,
333 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
335 temp
= (1 << multi_msi_capable(control
));
336 temp
= ((temp
- 1) & ~temp
);
338 pci_write_config_dword(dev
,
339 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
342 /* Configure MSI capability structure */
343 irq
= arch_setup_msi_irq(dev
, entry
);
345 kmem_cache_free(msi_cachep
, entry
);
348 entry
->link
.head
= irq
;
349 entry
->link
.tail
= irq
;
350 dev
->first_msi_irq
= irq
;
351 set_irq_msi(irq
, entry
);
353 /* Set MSI enabled bits */
354 pci_intx(dev
, 0); /* disable intx */
355 msi_set_enable(dev
, 1);
356 dev
->msi_enabled
= 1;
363 * msix_capability_init - configure device's MSI-X capability
364 * @dev: pointer to the pci_dev data structure of MSI-X device function
365 * @entries: pointer to an array of struct msix_entry entries
366 * @nvec: number of @entries
368 * Setup the MSI-X capability structure of device function with a
369 * single MSI-X irq. A return of zero indicates the successful setup of
370 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
372 static int msix_capability_init(struct pci_dev
*dev
,
373 struct msix_entry
*entries
, int nvec
)
375 struct msi_desc
*head
= NULL
, *tail
= NULL
, *entry
= NULL
;
376 int irq
, pos
, i
, j
, nr_entries
, temp
= 0;
377 unsigned long phys_addr
;
383 msix_set_enable(dev
, 0);/* Ensure msix is disabled as I set it up */
385 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
386 /* Request & Map MSI-X table region */
387 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
388 nr_entries
= multi_msix_capable(control
);
390 pci_read_config_dword(dev
, msix_table_offset_reg(pos
), &table_offset
);
391 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
392 table_offset
&= ~PCI_MSIX_FLAGS_BIRMASK
;
393 phys_addr
= pci_resource_start (dev
, bir
) + table_offset
;
394 base
= ioremap_nocache(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
398 /* MSI-X Table Initialization */
399 for (i
= 0; i
< nvec
; i
++) {
400 entry
= alloc_msi_entry();
404 j
= entries
[i
].entry
;
405 entry
->msi_attrib
.type
= PCI_CAP_ID_MSIX
;
406 entry
->msi_attrib
.is_64
= 1;
407 entry
->msi_attrib
.entry_nr
= j
;
408 entry
->msi_attrib
.maskbit
= 1;
409 entry
->msi_attrib
.masked
= 1;
410 entry
->msi_attrib
.default_irq
= dev
->irq
;
411 entry
->msi_attrib
.pos
= pos
;
413 entry
->mask_base
= base
;
415 /* Configure MSI-X capability structure */
416 irq
= arch_setup_msi_irq(dev
, entry
);
418 kmem_cache_free(msi_cachep
, entry
);
421 entries
[i
].vector
= irq
;
423 entry
->link
.head
= irq
;
424 entry
->link
.tail
= irq
;
427 entry
->link
.head
= temp
;
428 entry
->link
.tail
= tail
->link
.tail
;
429 tail
->link
.tail
= irq
;
430 head
->link
.head
= irq
;
435 set_irq_msi(irq
, entry
);
440 for (; i
>= 0; i
--) {
441 irq
= (entries
+ i
)->vector
;
442 msi_free_irq(dev
, irq
);
443 (entries
+ i
)->vector
= 0;
445 /* If we had some success report the number of irqs
446 * we succeeded in setting up.
452 dev
->first_msi_irq
= entries
[0].vector
;
453 /* Set MSI-X enabled bits */
454 pci_intx(dev
, 0); /* disable intx */
455 msix_set_enable(dev
, 1);
456 dev
->msix_enabled
= 1;
462 * pci_msi_supported - check whether MSI may be enabled on device
463 * @dev: pointer to the pci_dev data structure of MSI device function
465 * Look at global flags, the device itself, and its parent busses
466 * to return 0 if MSI are supported for the device.
469 int pci_msi_supported(struct pci_dev
* dev
)
473 /* MSI must be globally enabled and supported by the device */
474 if (!pci_msi_enable
|| !dev
|| dev
->no_msi
)
477 /* Any bridge which does NOT route MSI transactions from it's
478 * secondary bus to it's primary bus must set NO_MSI flag on
479 * the secondary pci_bus.
480 * We expect only arch-specific PCI host bus controller driver
481 * or quirks for specific PCI bridges to be setting NO_MSI.
483 for (bus
= dev
->bus
; bus
; bus
= bus
->parent
)
484 if (bus
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
491 * pci_enable_msi - configure device's MSI capability structure
492 * @dev: pointer to the pci_dev data structure of MSI device function
494 * Setup the MSI capability structure of device function with
495 * a single MSI irq upon its software driver call to request for
496 * MSI mode enabled on its hardware device function. A return of zero
497 * indicates the successful setup of an entry zero with the new MSI
498 * irq or non-zero for otherwise.
500 int pci_enable_msi(struct pci_dev
* dev
)
504 if (pci_msi_supported(dev
) < 0)
511 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
515 WARN_ON(!!dev
->msi_enabled
);
517 /* Check whether driver already requested for MSI-X irqs */
518 if (dev
->msix_enabled
) {
519 printk(KERN_INFO
"PCI: %s: Can't enable MSI. "
520 "Device already has MSI-X enabled\n",
524 status
= msi_capability_init(dev
);
528 void pci_disable_msi(struct pci_dev
* dev
)
530 struct msi_desc
*entry
;
538 if (!dev
->msi_enabled
)
541 msi_set_enable(dev
, 0);
542 pci_intx(dev
, 1); /* enable intx */
543 dev
->msi_enabled
= 0;
545 entry
= get_irq_msi(dev
->first_msi_irq
);
546 if (!entry
|| !entry
->dev
|| entry
->msi_attrib
.type
!= PCI_CAP_ID_MSI
) {
549 if (irq_has_action(dev
->first_msi_irq
)) {
550 printk(KERN_WARNING
"PCI: %s: pci_disable_msi() called without "
551 "free_irq() on MSI irq %d\n",
552 pci_name(dev
), dev
->first_msi_irq
);
553 BUG_ON(irq_has_action(dev
->first_msi_irq
));
555 default_irq
= entry
->msi_attrib
.default_irq
;
556 msi_free_irq(dev
, dev
->first_msi_irq
);
558 /* Restore dev->irq to its default pin-assertion irq */
559 dev
->irq
= default_irq
;
561 dev
->first_msi_irq
= 0;
564 static int msi_free_irq(struct pci_dev
* dev
, int irq
)
566 struct msi_desc
*entry
;
567 int head
, entry_nr
, type
;
570 entry
= get_irq_msi(irq
);
571 if (!entry
|| entry
->dev
!= dev
) {
574 type
= entry
->msi_attrib
.type
;
575 entry_nr
= entry
->msi_attrib
.entry_nr
;
576 head
= entry
->link
.head
;
577 base
= entry
->mask_base
;
578 get_irq_msi(entry
->link
.head
)->link
.tail
= entry
->link
.tail
;
579 get_irq_msi(entry
->link
.tail
)->link
.head
= entry
->link
.head
;
581 arch_teardown_msi_irq(irq
);
582 kmem_cache_free(msi_cachep
, entry
);
584 if (type
== PCI_CAP_ID_MSIX
) {
585 writel(1, base
+ entry_nr
* PCI_MSIX_ENTRY_SIZE
+
586 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
);
596 * pci_enable_msix - configure device's MSI-X capability structure
597 * @dev: pointer to the pci_dev data structure of MSI-X device function
598 * @entries: pointer to an array of MSI-X entries
599 * @nvec: number of MSI-X irqs requested for allocation by device driver
601 * Setup the MSI-X capability structure of device function with the number
602 * of requested irqs upon its software driver call to request for
603 * MSI-X mode enabled on its hardware device function. A return of zero
604 * indicates the successful configuration of MSI-X capability structure
605 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
606 * Or a return of > 0 indicates that driver request is exceeding the number
607 * of irqs available. Driver should use the returned value to re-send
610 int pci_enable_msix(struct pci_dev
* dev
, struct msix_entry
*entries
, int nvec
)
612 int status
, pos
, nr_entries
;
616 if (!entries
|| pci_msi_supported(dev
) < 0)
623 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
627 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
628 nr_entries
= multi_msix_capable(control
);
629 if (nvec
> nr_entries
)
632 /* Check for any invalid entries */
633 for (i
= 0; i
< nvec
; i
++) {
634 if (entries
[i
].entry
>= nr_entries
)
635 return -EINVAL
; /* invalid entry */
636 for (j
= i
+ 1; j
< nvec
; j
++) {
637 if (entries
[i
].entry
== entries
[j
].entry
)
638 return -EINVAL
; /* duplicate entry */
641 WARN_ON(!!dev
->msix_enabled
);
643 /* Check whether driver already requested for MSI irq */
644 if (dev
->msi_enabled
) {
645 printk(KERN_INFO
"PCI: %s: Can't enable MSI-X. "
646 "Device already has an MSI irq assigned\n",
650 status
= msix_capability_init(dev
, entries
, nvec
);
654 void pci_disable_msix(struct pci_dev
* dev
)
656 int irq
, head
, tail
= 0, warning
= 0;
663 if (!dev
->msix_enabled
)
666 msix_set_enable(dev
, 0);
667 pci_intx(dev
, 1); /* enable intx */
668 dev
->msix_enabled
= 0;
670 irq
= head
= dev
->first_msi_irq
;
671 while (head
!= tail
) {
672 tail
= get_irq_msi(irq
)->link
.tail
;
673 if (irq_has_action(irq
))
675 else if (irq
!= head
) /* Release MSI-X irq */
676 msi_free_irq(dev
, irq
);
679 msi_free_irq(dev
, irq
);
681 printk(KERN_WARNING
"PCI: %s: pci_disable_msix() called without "
682 "free_irq() on all MSI-X irqs\n",
686 dev
->first_msi_irq
= 0;
690 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
691 * @dev: pointer to the pci_dev data structure of MSI(X) device function
693 * Being called during hotplug remove, from which the device function
694 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
695 * allocated for this device function, are reclaimed to unused state,
696 * which may be used later on.
698 void msi_remove_pci_irq_vectors(struct pci_dev
* dev
)
700 if (!pci_msi_enable
|| !dev
)
703 if (dev
->msi_enabled
) {
704 if (irq_has_action(dev
->first_msi_irq
)) {
705 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
706 "called without free_irq() on MSI irq %d\n",
707 pci_name(dev
), dev
->first_msi_irq
);
708 BUG_ON(irq_has_action(dev
->first_msi_irq
));
709 } else /* Release MSI irq assigned to this device */
710 msi_free_irq(dev
, dev
->first_msi_irq
);
712 if (dev
->msix_enabled
) {
713 int irq
, head
, tail
= 0, warning
= 0;
714 void __iomem
*base
= NULL
;
716 irq
= head
= dev
->first_msi_irq
;
717 while (head
!= tail
) {
718 tail
= get_irq_msi(irq
)->link
.tail
;
719 base
= get_irq_msi(irq
)->mask_base
;
720 if (irq_has_action(irq
))
722 else if (irq
!= head
) /* Release MSI-X irq */
723 msi_free_irq(dev
, irq
);
726 msi_free_irq(dev
, irq
);
729 printk(KERN_WARNING
"PCI: %s: msi_remove_pci_irq_vectors() "
730 "called without free_irq() on all MSI-X irqs\n",
737 void pci_no_msi(void)
742 EXPORT_SYMBOL(pci_enable_msi
);
743 EXPORT_SYMBOL(pci_disable_msi
);
744 EXPORT_SYMBOL(pci_enable_msix
);
745 EXPORT_SYMBOL(pci_disable_msix
);