1 // SPDX-License-Identifier: GPL-2.0
4 * Purpose: PCI Message Signaled Interrupt (MSI)
6 * Copyright (C) 2003-2004 Intel
7 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
8 * Copyright (C) 2016 Christoph Hellwig.
11 #include <linux/err.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
15 #include <linux/export.h>
16 #include <linux/ioport.h>
17 #include <linux/pci.h>
18 #include <linux/proc_fs.h>
19 #include <linux/msi.h>
20 #include <linux/smp.h>
21 #include <linux/errno.h>
23 #include <linux/acpi_iort.h>
24 #include <linux/slab.h>
25 #include <linux/irqdomain.h>
26 #include <linux/of_irq.h>
30 static int pci_msi_enable
= 1;
31 int pci_msi_ignore_mask
;
33 #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
35 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
36 static int pci_msi_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
38 struct irq_domain
*domain
;
40 domain
= dev_get_msi_domain(&dev
->dev
);
41 if (domain
&& irq_domain_is_hierarchy(domain
))
42 return msi_domain_alloc_irqs(domain
, &dev
->dev
, nvec
);
44 return arch_setup_msi_irqs(dev
, nvec
, type
);
47 static void pci_msi_teardown_msi_irqs(struct pci_dev
*dev
)
49 struct irq_domain
*domain
;
51 domain
= dev_get_msi_domain(&dev
->dev
);
52 if (domain
&& irq_domain_is_hierarchy(domain
))
53 msi_domain_free_irqs(domain
, &dev
->dev
);
55 arch_teardown_msi_irqs(dev
);
58 #define pci_msi_setup_msi_irqs arch_setup_msi_irqs
59 #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
64 int __weak
arch_setup_msi_irq(struct pci_dev
*dev
, struct msi_desc
*desc
)
66 struct msi_controller
*chip
= dev
->bus
->msi
;
69 if (!chip
|| !chip
->setup_irq
)
72 err
= chip
->setup_irq(chip
, dev
, desc
);
76 irq_set_chip_data(desc
->irq
, chip
);
81 void __weak
arch_teardown_msi_irq(unsigned int irq
)
83 struct msi_controller
*chip
= irq_get_chip_data(irq
);
85 if (!chip
|| !chip
->teardown_irq
)
88 chip
->teardown_irq(chip
, irq
);
91 int __weak
arch_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
93 struct msi_controller
*chip
= dev
->bus
->msi
;
94 struct msi_desc
*entry
;
97 if (chip
&& chip
->setup_irqs
)
98 return chip
->setup_irqs(chip
, dev
, nvec
, type
);
100 * If an architecture wants to support multiple MSI, it needs to
101 * override arch_setup_msi_irqs()
103 if (type
== PCI_CAP_ID_MSI
&& nvec
> 1)
106 for_each_pci_msi_entry(entry
, dev
) {
107 ret
= arch_setup_msi_irq(dev
, entry
);
118 * We have a default implementation available as a separate non-weak
119 * function, as it is used by the Xen x86 PCI code
121 void default_teardown_msi_irqs(struct pci_dev
*dev
)
124 struct msi_desc
*entry
;
126 for_each_pci_msi_entry(entry
, dev
)
128 for (i
= 0; i
< entry
->nvec_used
; i
++)
129 arch_teardown_msi_irq(entry
->irq
+ i
);
132 void __weak
arch_teardown_msi_irqs(struct pci_dev
*dev
)
134 return default_teardown_msi_irqs(dev
);
137 static void default_restore_msi_irq(struct pci_dev
*dev
, int irq
)
139 struct msi_desc
*entry
;
142 if (dev
->msix_enabled
) {
143 for_each_pci_msi_entry(entry
, dev
) {
144 if (irq
== entry
->irq
)
147 } else if (dev
->msi_enabled
) {
148 entry
= irq_get_msi_desc(irq
);
152 __pci_write_msi_msg(entry
, &entry
->msg
);
155 void __weak
arch_restore_msi_irqs(struct pci_dev
*dev
)
157 return default_restore_msi_irqs(dev
);
160 static inline __attribute_const__ u32
msi_mask(unsigned x
)
162 /* Don't shift by >= width of type */
165 return (1 << (1 << x
)) - 1;
169 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
170 * mask all MSI interrupts by clearing the MSI enable bit does not work
171 * reliably as devices without an INTx disable bit will then generate a
172 * level IRQ which will never be cleared.
174 u32
__pci_msi_desc_mask_irq(struct msi_desc
*desc
, u32 mask
, u32 flag
)
176 u32 mask_bits
= desc
->masked
;
178 if (pci_msi_ignore_mask
|| !desc
->msi_attrib
.maskbit
)
183 pci_write_config_dword(msi_desc_to_pci_dev(desc
), desc
->mask_pos
,
189 static void msi_mask_irq(struct msi_desc
*desc
, u32 mask
, u32 flag
)
191 desc
->masked
= __pci_msi_desc_mask_irq(desc
, mask
, flag
);
194 static void __iomem
*pci_msix_desc_addr(struct msi_desc
*desc
)
196 return desc
->mask_base
+
197 desc
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
;
201 * This internal function does not flush PCI writes to the device.
202 * All users must ensure that they read from the device before either
203 * assuming that the device state is up to date, or returning out of this
204 * file. This saves a few milliseconds when initialising devices with lots
205 * of MSI-X interrupts.
207 u32
__pci_msix_desc_mask_irq(struct msi_desc
*desc
, u32 flag
)
209 u32 mask_bits
= desc
->masked
;
211 if (pci_msi_ignore_mask
)
214 mask_bits
&= ~PCI_MSIX_ENTRY_CTRL_MASKBIT
;
216 mask_bits
|= PCI_MSIX_ENTRY_CTRL_MASKBIT
;
217 writel(mask_bits
, pci_msix_desc_addr(desc
) + PCI_MSIX_ENTRY_VECTOR_CTRL
);
222 static void msix_mask_irq(struct msi_desc
*desc
, u32 flag
)
224 desc
->masked
= __pci_msix_desc_mask_irq(desc
, flag
);
227 static void msi_set_mask_bit(struct irq_data
*data
, u32 flag
)
229 struct msi_desc
*desc
= irq_data_get_msi_desc(data
);
231 if (desc
->msi_attrib
.is_msix
) {
232 msix_mask_irq(desc
, flag
);
233 readl(desc
->mask_base
); /* Flush write to device */
235 unsigned offset
= data
->irq
- desc
->irq
;
236 msi_mask_irq(desc
, 1 << offset
, flag
<< offset
);
241 * pci_msi_mask_irq - Generic irq chip callback to mask PCI/MSI interrupts
242 * @data: pointer to irqdata associated to that interrupt
244 void pci_msi_mask_irq(struct irq_data
*data
)
246 msi_set_mask_bit(data
, 1);
248 EXPORT_SYMBOL_GPL(pci_msi_mask_irq
);
251 * pci_msi_unmask_irq - Generic irq chip callback to unmask PCI/MSI interrupts
252 * @data: pointer to irqdata associated to that interrupt
254 void pci_msi_unmask_irq(struct irq_data
*data
)
256 msi_set_mask_bit(data
, 0);
258 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq
);
260 void default_restore_msi_irqs(struct pci_dev
*dev
)
262 struct msi_desc
*entry
;
264 for_each_pci_msi_entry(entry
, dev
)
265 default_restore_msi_irq(dev
, entry
->irq
);
268 void __pci_read_msi_msg(struct msi_desc
*entry
, struct msi_msg
*msg
)
270 struct pci_dev
*dev
= msi_desc_to_pci_dev(entry
);
272 BUG_ON(dev
->current_state
!= PCI_D0
);
274 if (entry
->msi_attrib
.is_msix
) {
275 void __iomem
*base
= pci_msix_desc_addr(entry
);
277 msg
->address_lo
= readl(base
+ PCI_MSIX_ENTRY_LOWER_ADDR
);
278 msg
->address_hi
= readl(base
+ PCI_MSIX_ENTRY_UPPER_ADDR
);
279 msg
->data
= readl(base
+ PCI_MSIX_ENTRY_DATA
);
281 int pos
= dev
->msi_cap
;
284 pci_read_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_LO
,
286 if (entry
->msi_attrib
.is_64
) {
287 pci_read_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_HI
,
289 pci_read_config_word(dev
, pos
+ PCI_MSI_DATA_64
, &data
);
292 pci_read_config_word(dev
, pos
+ PCI_MSI_DATA_32
, &data
);
298 void __pci_write_msi_msg(struct msi_desc
*entry
, struct msi_msg
*msg
)
300 struct pci_dev
*dev
= msi_desc_to_pci_dev(entry
);
302 if (dev
->current_state
!= PCI_D0
|| pci_dev_is_disconnected(dev
)) {
303 /* Don't touch the hardware now */
304 } else if (entry
->msi_attrib
.is_msix
) {
305 void __iomem
*base
= pci_msix_desc_addr(entry
);
307 writel(msg
->address_lo
, base
+ PCI_MSIX_ENTRY_LOWER_ADDR
);
308 writel(msg
->address_hi
, base
+ PCI_MSIX_ENTRY_UPPER_ADDR
);
309 writel(msg
->data
, base
+ PCI_MSIX_ENTRY_DATA
);
311 int pos
= dev
->msi_cap
;
314 pci_read_config_word(dev
, pos
+ PCI_MSI_FLAGS
, &msgctl
);
315 msgctl
&= ~PCI_MSI_FLAGS_QSIZE
;
316 msgctl
|= entry
->msi_attrib
.multiple
<< 4;
317 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, msgctl
);
319 pci_write_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_LO
,
321 if (entry
->msi_attrib
.is_64
) {
322 pci_write_config_dword(dev
, pos
+ PCI_MSI_ADDRESS_HI
,
324 pci_write_config_word(dev
, pos
+ PCI_MSI_DATA_64
,
327 pci_write_config_word(dev
, pos
+ PCI_MSI_DATA_32
,
334 void pci_write_msi_msg(unsigned int irq
, struct msi_msg
*msg
)
336 struct msi_desc
*entry
= irq_get_msi_desc(irq
);
338 __pci_write_msi_msg(entry
, msg
);
340 EXPORT_SYMBOL_GPL(pci_write_msi_msg
);
342 static void free_msi_irqs(struct pci_dev
*dev
)
344 struct list_head
*msi_list
= dev_to_msi_list(&dev
->dev
);
345 struct msi_desc
*entry
, *tmp
;
346 struct attribute
**msi_attrs
;
347 struct device_attribute
*dev_attr
;
350 for_each_pci_msi_entry(entry
, dev
)
352 for (i
= 0; i
< entry
->nvec_used
; i
++)
353 BUG_ON(irq_has_action(entry
->irq
+ i
));
355 pci_msi_teardown_msi_irqs(dev
);
357 list_for_each_entry_safe(entry
, tmp
, msi_list
, list
) {
358 if (entry
->msi_attrib
.is_msix
) {
359 if (list_is_last(&entry
->list
, msi_list
))
360 iounmap(entry
->mask_base
);
363 list_del(&entry
->list
);
364 free_msi_entry(entry
);
367 if (dev
->msi_irq_groups
) {
368 sysfs_remove_groups(&dev
->dev
.kobj
, dev
->msi_irq_groups
);
369 msi_attrs
= dev
->msi_irq_groups
[0]->attrs
;
370 while (msi_attrs
[count
]) {
371 dev_attr
= container_of(msi_attrs
[count
],
372 struct device_attribute
, attr
);
373 kfree(dev_attr
->attr
.name
);
378 kfree(dev
->msi_irq_groups
[0]);
379 kfree(dev
->msi_irq_groups
);
380 dev
->msi_irq_groups
= NULL
;
384 static void pci_intx_for_msi(struct pci_dev
*dev
, int enable
)
386 if (!(dev
->dev_flags
& PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG
))
387 pci_intx(dev
, enable
);
390 static void __pci_restore_msi_state(struct pci_dev
*dev
)
393 struct msi_desc
*entry
;
395 if (!dev
->msi_enabled
)
398 entry
= irq_get_msi_desc(dev
->irq
);
400 pci_intx_for_msi(dev
, 0);
401 pci_msi_set_enable(dev
, 0);
402 arch_restore_msi_irqs(dev
);
404 pci_read_config_word(dev
, dev
->msi_cap
+ PCI_MSI_FLAGS
, &control
);
405 msi_mask_irq(entry
, msi_mask(entry
->msi_attrib
.multi_cap
),
407 control
&= ~PCI_MSI_FLAGS_QSIZE
;
408 control
|= (entry
->msi_attrib
.multiple
<< 4) | PCI_MSI_FLAGS_ENABLE
;
409 pci_write_config_word(dev
, dev
->msi_cap
+ PCI_MSI_FLAGS
, control
);
412 static void __pci_restore_msix_state(struct pci_dev
*dev
)
414 struct msi_desc
*entry
;
416 if (!dev
->msix_enabled
)
418 BUG_ON(list_empty(dev_to_msi_list(&dev
->dev
)));
420 /* route the table */
421 pci_intx_for_msi(dev
, 0);
422 pci_msix_clear_and_set_ctrl(dev
, 0,
423 PCI_MSIX_FLAGS_ENABLE
| PCI_MSIX_FLAGS_MASKALL
);
425 arch_restore_msi_irqs(dev
);
426 for_each_pci_msi_entry(entry
, dev
)
427 msix_mask_irq(entry
, entry
->masked
);
429 pci_msix_clear_and_set_ctrl(dev
, PCI_MSIX_FLAGS_MASKALL
, 0);
432 void pci_restore_msi_state(struct pci_dev
*dev
)
434 __pci_restore_msi_state(dev
);
435 __pci_restore_msix_state(dev
);
437 EXPORT_SYMBOL_GPL(pci_restore_msi_state
);
439 static ssize_t
msi_mode_show(struct device
*dev
, struct device_attribute
*attr
,
442 struct msi_desc
*entry
;
446 retval
= kstrtoul(attr
->attr
.name
, 10, &irq
);
450 entry
= irq_get_msi_desc(irq
);
452 return sprintf(buf
, "%s\n",
453 entry
->msi_attrib
.is_msix
? "msix" : "msi");
458 static int populate_msi_sysfs(struct pci_dev
*pdev
)
460 struct attribute
**msi_attrs
;
461 struct attribute
*msi_attr
;
462 struct device_attribute
*msi_dev_attr
;
463 struct attribute_group
*msi_irq_group
;
464 const struct attribute_group
**msi_irq_groups
;
465 struct msi_desc
*entry
;
471 /* Determine how many msi entries we have */
472 for_each_pci_msi_entry(entry
, pdev
)
473 num_msi
+= entry
->nvec_used
;
477 /* Dynamically create the MSI attributes for the PCI device */
478 msi_attrs
= kzalloc(sizeof(void *) * (num_msi
+ 1), GFP_KERNEL
);
481 for_each_pci_msi_entry(entry
, pdev
) {
482 for (i
= 0; i
< entry
->nvec_used
; i
++) {
483 msi_dev_attr
= kzalloc(sizeof(*msi_dev_attr
), GFP_KERNEL
);
486 msi_attrs
[count
] = &msi_dev_attr
->attr
;
488 sysfs_attr_init(&msi_dev_attr
->attr
);
489 msi_dev_attr
->attr
.name
= kasprintf(GFP_KERNEL
, "%d",
491 if (!msi_dev_attr
->attr
.name
)
493 msi_dev_attr
->attr
.mode
= S_IRUGO
;
494 msi_dev_attr
->show
= msi_mode_show
;
499 msi_irq_group
= kzalloc(sizeof(*msi_irq_group
), GFP_KERNEL
);
502 msi_irq_group
->name
= "msi_irqs";
503 msi_irq_group
->attrs
= msi_attrs
;
505 msi_irq_groups
= kzalloc(sizeof(void *) * 2, GFP_KERNEL
);
507 goto error_irq_group
;
508 msi_irq_groups
[0] = msi_irq_group
;
510 ret
= sysfs_create_groups(&pdev
->dev
.kobj
, msi_irq_groups
);
512 goto error_irq_groups
;
513 pdev
->msi_irq_groups
= msi_irq_groups
;
518 kfree(msi_irq_groups
);
520 kfree(msi_irq_group
);
523 msi_attr
= msi_attrs
[count
];
525 msi_dev_attr
= container_of(msi_attr
, struct device_attribute
, attr
);
526 kfree(msi_attr
->name
);
529 msi_attr
= msi_attrs
[count
];
535 static struct msi_desc
*
536 msi_setup_entry(struct pci_dev
*dev
, int nvec
, const struct irq_affinity
*affd
)
538 struct cpumask
*masks
= NULL
;
539 struct msi_desc
*entry
;
543 masks
= irq_create_affinity_masks(nvec
, affd
);
546 /* MSI Entry Initialization */
547 entry
= alloc_msi_entry(&dev
->dev
, nvec
, masks
);
551 pci_read_config_word(dev
, dev
->msi_cap
+ PCI_MSI_FLAGS
, &control
);
553 entry
->msi_attrib
.is_msix
= 0;
554 entry
->msi_attrib
.is_64
= !!(control
& PCI_MSI_FLAGS_64BIT
);
555 entry
->msi_attrib
.entry_nr
= 0;
556 entry
->msi_attrib
.maskbit
= !!(control
& PCI_MSI_FLAGS_MASKBIT
);
557 entry
->msi_attrib
.default_irq
= dev
->irq
; /* Save IOAPIC IRQ */
558 entry
->msi_attrib
.multi_cap
= (control
& PCI_MSI_FLAGS_QMASK
) >> 1;
559 entry
->msi_attrib
.multiple
= ilog2(__roundup_pow_of_two(nvec
));
561 if (control
& PCI_MSI_FLAGS_64BIT
)
562 entry
->mask_pos
= dev
->msi_cap
+ PCI_MSI_MASK_64
;
564 entry
->mask_pos
= dev
->msi_cap
+ PCI_MSI_MASK_32
;
566 /* Save the initial mask status */
567 if (entry
->msi_attrib
.maskbit
)
568 pci_read_config_dword(dev
, entry
->mask_pos
, &entry
->masked
);
575 static int msi_verify_entries(struct pci_dev
*dev
)
577 struct msi_desc
*entry
;
579 for_each_pci_msi_entry(entry
, dev
) {
580 if (!dev
->no_64bit_msi
|| !entry
->msg
.address_hi
)
582 pci_err(dev
, "Device has broken 64-bit MSI but arch"
583 " tried to assign one above 4G\n");
590 * msi_capability_init - configure device's MSI capability structure
591 * @dev: pointer to the pci_dev data structure of MSI device function
592 * @nvec: number of interrupts to allocate
593 * @affd: description of automatic irq affinity assignments (may be %NULL)
595 * Setup the MSI capability structure of the device with the requested
596 * number of interrupts. A return value of zero indicates the successful
597 * setup of an entry with the new MSI irq. A negative return value indicates
598 * an error, and a positive return value indicates the number of interrupts
599 * which could have been allocated.
601 static int msi_capability_init(struct pci_dev
*dev
, int nvec
,
602 const struct irq_affinity
*affd
)
604 struct msi_desc
*entry
;
608 pci_msi_set_enable(dev
, 0); /* Disable MSI during set up */
610 entry
= msi_setup_entry(dev
, nvec
, affd
);
614 /* All MSIs are unmasked by default, Mask them all */
615 mask
= msi_mask(entry
->msi_attrib
.multi_cap
);
616 msi_mask_irq(entry
, mask
, mask
);
618 list_add_tail(&entry
->list
, dev_to_msi_list(&dev
->dev
));
620 /* Configure MSI capability structure */
621 ret
= pci_msi_setup_msi_irqs(dev
, nvec
, PCI_CAP_ID_MSI
);
623 msi_mask_irq(entry
, mask
, ~mask
);
628 ret
= msi_verify_entries(dev
);
630 msi_mask_irq(entry
, mask
, ~mask
);
635 ret
= populate_msi_sysfs(dev
);
637 msi_mask_irq(entry
, mask
, ~mask
);
642 /* Set MSI enabled bits */
643 pci_intx_for_msi(dev
, 0);
644 pci_msi_set_enable(dev
, 1);
645 dev
->msi_enabled
= 1;
647 pcibios_free_irq(dev
);
648 dev
->irq
= entry
->irq
;
652 static void __iomem
*msix_map_region(struct pci_dev
*dev
, unsigned nr_entries
)
654 resource_size_t phys_addr
;
659 pci_read_config_dword(dev
, dev
->msix_cap
+ PCI_MSIX_TABLE
,
661 bir
= (u8
)(table_offset
& PCI_MSIX_TABLE_BIR
);
662 flags
= pci_resource_flags(dev
, bir
);
663 if (!flags
|| (flags
& IORESOURCE_UNSET
))
666 table_offset
&= PCI_MSIX_TABLE_OFFSET
;
667 phys_addr
= pci_resource_start(dev
, bir
) + table_offset
;
669 return ioremap_nocache(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
672 static int msix_setup_entries(struct pci_dev
*dev
, void __iomem
*base
,
673 struct msix_entry
*entries
, int nvec
,
674 const struct irq_affinity
*affd
)
676 struct cpumask
*curmsk
, *masks
= NULL
;
677 struct msi_desc
*entry
;
681 masks
= irq_create_affinity_masks(nvec
, affd
);
683 for (i
= 0, curmsk
= masks
; i
< nvec
; i
++) {
684 entry
= alloc_msi_entry(&dev
->dev
, 1, curmsk
);
690 /* No enough memory. Don't try again */
695 entry
->msi_attrib
.is_msix
= 1;
696 entry
->msi_attrib
.is_64
= 1;
698 entry
->msi_attrib
.entry_nr
= entries
[i
].entry
;
700 entry
->msi_attrib
.entry_nr
= i
;
701 entry
->msi_attrib
.default_irq
= dev
->irq
;
702 entry
->mask_base
= base
;
704 list_add_tail(&entry
->list
, dev_to_msi_list(&dev
->dev
));
714 static void msix_program_entries(struct pci_dev
*dev
,
715 struct msix_entry
*entries
)
717 struct msi_desc
*entry
;
720 for_each_pci_msi_entry(entry
, dev
) {
722 entries
[i
++].vector
= entry
->irq
;
723 entry
->masked
= readl(pci_msix_desc_addr(entry
) +
724 PCI_MSIX_ENTRY_VECTOR_CTRL
);
725 msix_mask_irq(entry
, 1);
730 * msix_capability_init - configure device's MSI-X capability
731 * @dev: pointer to the pci_dev data structure of MSI-X device function
732 * @entries: pointer to an array of struct msix_entry entries
733 * @nvec: number of @entries
734 * @affd: Optional pointer to enable automatic affinity assignement
736 * Setup the MSI-X capability structure of device function with a
737 * single MSI-X irq. A return of zero indicates the successful setup of
738 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
740 static int msix_capability_init(struct pci_dev
*dev
, struct msix_entry
*entries
,
741 int nvec
, const struct irq_affinity
*affd
)
747 /* Ensure MSI-X is disabled while it is set up */
748 pci_msix_clear_and_set_ctrl(dev
, PCI_MSIX_FLAGS_ENABLE
, 0);
750 pci_read_config_word(dev
, dev
->msix_cap
+ PCI_MSIX_FLAGS
, &control
);
751 /* Request & Map MSI-X table region */
752 base
= msix_map_region(dev
, msix_table_size(control
));
756 ret
= msix_setup_entries(dev
, base
, entries
, nvec
, affd
);
760 ret
= pci_msi_setup_msi_irqs(dev
, nvec
, PCI_CAP_ID_MSIX
);
764 /* Check if all MSI entries honor device restrictions */
765 ret
= msi_verify_entries(dev
);
770 * Some devices require MSI-X to be enabled before we can touch the
771 * MSI-X registers. We need to mask all the vectors to prevent
772 * interrupts coming in before they're fully set up.
774 pci_msix_clear_and_set_ctrl(dev
, 0,
775 PCI_MSIX_FLAGS_MASKALL
| PCI_MSIX_FLAGS_ENABLE
);
777 msix_program_entries(dev
, entries
);
779 ret
= populate_msi_sysfs(dev
);
783 /* Set MSI-X enabled bits and unmask the function */
784 pci_intx_for_msi(dev
, 0);
785 dev
->msix_enabled
= 1;
786 pci_msix_clear_and_set_ctrl(dev
, PCI_MSIX_FLAGS_MASKALL
, 0);
788 pcibios_free_irq(dev
);
794 * If we had some success, report the number of irqs
795 * we succeeded in setting up.
797 struct msi_desc
*entry
;
800 for_each_pci_msi_entry(entry
, dev
) {
815 * pci_msi_supported - check whether MSI may be enabled on a device
816 * @dev: pointer to the pci_dev data structure of MSI device function
817 * @nvec: how many MSIs have been requested ?
819 * Look at global flags, the device itself, and its parent buses
820 * to determine if MSI/-X are supported for the device. If MSI/-X is
821 * supported return 1, else return 0.
823 static int pci_msi_supported(struct pci_dev
*dev
, int nvec
)
827 /* MSI must be globally enabled and supported by the device */
831 if (!dev
|| dev
->no_msi
|| dev
->current_state
!= PCI_D0
)
835 * You can't ask to have 0 or less MSIs configured.
837 * b) the list manipulation code assumes nvec >= 1.
843 * Any bridge which does NOT route MSI transactions from its
844 * secondary bus to its primary bus must set NO_MSI flag on
845 * the secondary pci_bus.
846 * We expect only arch-specific PCI host bus controller driver
847 * or quirks for specific PCI bridges to be setting NO_MSI.
849 for (bus
= dev
->bus
; bus
; bus
= bus
->parent
)
850 if (bus
->bus_flags
& PCI_BUS_FLAGS_NO_MSI
)
857 * pci_msi_vec_count - Return the number of MSI vectors a device can send
858 * @dev: device to report about
860 * This function returns the number of MSI vectors a device requested via
861 * Multiple Message Capable register. It returns a negative errno if the
862 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
863 * and returns a power of two, up to a maximum of 2^5 (32), according to the
866 int pci_msi_vec_count(struct pci_dev
*dev
)
874 pci_read_config_word(dev
, dev
->msi_cap
+ PCI_MSI_FLAGS
, &msgctl
);
875 ret
= 1 << ((msgctl
& PCI_MSI_FLAGS_QMASK
) >> 1);
879 EXPORT_SYMBOL(pci_msi_vec_count
);
881 static void pci_msi_shutdown(struct pci_dev
*dev
)
883 struct msi_desc
*desc
;
886 if (!pci_msi_enable
|| !dev
|| !dev
->msi_enabled
)
889 BUG_ON(list_empty(dev_to_msi_list(&dev
->dev
)));
890 desc
= first_pci_msi_entry(dev
);
892 pci_msi_set_enable(dev
, 0);
893 pci_intx_for_msi(dev
, 1);
894 dev
->msi_enabled
= 0;
896 /* Return the device with MSI unmasked as initial states */
897 mask
= msi_mask(desc
->msi_attrib
.multi_cap
);
898 /* Keep cached state to be restored */
899 __pci_msi_desc_mask_irq(desc
, mask
, ~mask
);
901 /* Restore dev->irq to its default pin-assertion irq */
902 dev
->irq
= desc
->msi_attrib
.default_irq
;
903 pcibios_alloc_irq(dev
);
906 void pci_disable_msi(struct pci_dev
*dev
)
908 if (!pci_msi_enable
|| !dev
|| !dev
->msi_enabled
)
911 pci_msi_shutdown(dev
);
914 EXPORT_SYMBOL(pci_disable_msi
);
917 * pci_msix_vec_count - return the number of device's MSI-X table entries
918 * @dev: pointer to the pci_dev data structure of MSI-X device function
919 * This function returns the number of device's MSI-X table entries and
920 * therefore the number of MSI-X vectors device is capable of sending.
921 * It returns a negative errno if the device is not capable of sending MSI-X
924 int pci_msix_vec_count(struct pci_dev
*dev
)
931 pci_read_config_word(dev
, dev
->msix_cap
+ PCI_MSIX_FLAGS
, &control
);
932 return msix_table_size(control
);
934 EXPORT_SYMBOL(pci_msix_vec_count
);
936 static int __pci_enable_msix(struct pci_dev
*dev
, struct msix_entry
*entries
,
937 int nvec
, const struct irq_affinity
*affd
)
942 if (!pci_msi_supported(dev
, nvec
))
945 nr_entries
= pci_msix_vec_count(dev
);
948 if (nvec
> nr_entries
)
952 /* Check for any invalid entries */
953 for (i
= 0; i
< nvec
; i
++) {
954 if (entries
[i
].entry
>= nr_entries
)
955 return -EINVAL
; /* invalid entry */
956 for (j
= i
+ 1; j
< nvec
; j
++) {
957 if (entries
[i
].entry
== entries
[j
].entry
)
958 return -EINVAL
; /* duplicate entry */
962 WARN_ON(!!dev
->msix_enabled
);
964 /* Check whether driver already requested for MSI irq */
965 if (dev
->msi_enabled
) {
966 pci_info(dev
, "can't enable MSI-X (MSI IRQ already assigned)\n");
969 return msix_capability_init(dev
, entries
, nvec
, affd
);
972 static void pci_msix_shutdown(struct pci_dev
*dev
)
974 struct msi_desc
*entry
;
976 if (!pci_msi_enable
|| !dev
|| !dev
->msix_enabled
)
979 if (pci_dev_is_disconnected(dev
)) {
980 dev
->msix_enabled
= 0;
984 /* Return the device with MSI-X masked as initial states */
985 for_each_pci_msi_entry(entry
, dev
) {
986 /* Keep cached states to be restored */
987 __pci_msix_desc_mask_irq(entry
, 1);
990 pci_msix_clear_and_set_ctrl(dev
, PCI_MSIX_FLAGS_ENABLE
, 0);
991 pci_intx_for_msi(dev
, 1);
992 dev
->msix_enabled
= 0;
993 pcibios_alloc_irq(dev
);
996 void pci_disable_msix(struct pci_dev
*dev
)
998 if (!pci_msi_enable
|| !dev
|| !dev
->msix_enabled
)
1001 pci_msix_shutdown(dev
);
1004 EXPORT_SYMBOL(pci_disable_msix
);
1006 void pci_no_msi(void)
1012 * pci_msi_enabled - is MSI enabled?
1014 * Returns true if MSI has not been disabled by the command-line option
1017 int pci_msi_enabled(void)
1019 return pci_msi_enable
;
1021 EXPORT_SYMBOL(pci_msi_enabled
);
1023 static int __pci_enable_msi_range(struct pci_dev
*dev
, int minvec
, int maxvec
,
1024 const struct irq_affinity
*affd
)
1029 if (!pci_msi_supported(dev
, minvec
))
1032 WARN_ON(!!dev
->msi_enabled
);
1034 /* Check whether driver already requested MSI-X irqs */
1035 if (dev
->msix_enabled
) {
1036 pci_info(dev
, "can't enable MSI (MSI-X already enabled)\n");
1040 if (maxvec
< minvec
)
1043 nvec
= pci_msi_vec_count(dev
);
1054 nvec
= irq_calc_affinity_vectors(minvec
, nvec
, affd
);
1059 rc
= msi_capability_init(dev
, nvec
, affd
);
1072 /* deprecated, don't use */
1073 int pci_enable_msi(struct pci_dev
*dev
)
1075 int rc
= __pci_enable_msi_range(dev
, 1, 1, NULL
);
1080 EXPORT_SYMBOL(pci_enable_msi
);
1082 static int __pci_enable_msix_range(struct pci_dev
*dev
,
1083 struct msix_entry
*entries
, int minvec
,
1084 int maxvec
, const struct irq_affinity
*affd
)
1086 int rc
, nvec
= maxvec
;
1088 if (maxvec
< minvec
)
1093 nvec
= irq_calc_affinity_vectors(minvec
, nvec
, affd
);
1098 rc
= __pci_enable_msix(dev
, entries
, nvec
, affd
);
1112 * pci_enable_msix_range - configure device's MSI-X capability structure
1113 * @dev: pointer to the pci_dev data structure of MSI-X device function
1114 * @entries: pointer to an array of MSI-X entries
1115 * @minvec: minimum number of MSI-X irqs requested
1116 * @maxvec: maximum number of MSI-X irqs requested
1118 * Setup the MSI-X capability structure of device function with a maximum
1119 * possible number of interrupts in the range between @minvec and @maxvec
1120 * upon its software driver call to request for MSI-X mode enabled on its
1121 * hardware device function. It returns a negative errno if an error occurs.
1122 * If it succeeds, it returns the actual number of interrupts allocated and
1123 * indicates the successful configuration of MSI-X capability structure
1124 * with new allocated MSI-X interrupts.
1126 int pci_enable_msix_range(struct pci_dev
*dev
, struct msix_entry
*entries
,
1127 int minvec
, int maxvec
)
1129 return __pci_enable_msix_range(dev
, entries
, minvec
, maxvec
, NULL
);
1131 EXPORT_SYMBOL(pci_enable_msix_range
);
1134 * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1135 * @dev: PCI device to operate on
1136 * @min_vecs: minimum number of vectors required (must be >= 1)
1137 * @max_vecs: maximum (desired) number of vectors
1138 * @flags: flags or quirks for the allocation
1139 * @affd: optional description of the affinity requirements
1141 * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1142 * vectors if available, and fall back to a single legacy vector
1143 * if neither is available. Return the number of vectors allocated,
1144 * (which might be smaller than @max_vecs) if successful, or a negative
1145 * error code on error. If less than @min_vecs interrupt vectors are
1146 * available for @dev the function will fail with -ENOSPC.
1148 * To get the Linux IRQ number used for a vector that can be passed to
1149 * request_irq() use the pci_irq_vector() helper.
1151 int pci_alloc_irq_vectors_affinity(struct pci_dev
*dev
, unsigned int min_vecs
,
1152 unsigned int max_vecs
, unsigned int flags
,
1153 const struct irq_affinity
*affd
)
1155 static const struct irq_affinity msi_default_affd
;
1158 if (flags
& PCI_IRQ_AFFINITY
) {
1160 affd
= &msi_default_affd
;
1166 if (flags
& PCI_IRQ_MSIX
) {
1167 vecs
= __pci_enable_msix_range(dev
, NULL
, min_vecs
, max_vecs
,
1173 if (flags
& PCI_IRQ_MSI
) {
1174 vecs
= __pci_enable_msi_range(dev
, min_vecs
, max_vecs
, affd
);
1179 /* use legacy irq if allowed */
1180 if (flags
& PCI_IRQ_LEGACY
) {
1181 if (min_vecs
== 1 && dev
->irq
) {
1189 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity
);
1192 * pci_free_irq_vectors - free previously allocated IRQs for a device
1193 * @dev: PCI device to operate on
1195 * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1197 void pci_free_irq_vectors(struct pci_dev
*dev
)
1199 pci_disable_msix(dev
);
1200 pci_disable_msi(dev
);
1202 EXPORT_SYMBOL(pci_free_irq_vectors
);
1205 * pci_irq_vector - return Linux IRQ number of a device vector
1206 * @dev: PCI device to operate on
1207 * @nr: device-relative interrupt vector index (0-based).
1209 int pci_irq_vector(struct pci_dev
*dev
, unsigned int nr
)
1211 if (dev
->msix_enabled
) {
1212 struct msi_desc
*entry
;
1215 for_each_pci_msi_entry(entry
, dev
) {
1224 if (dev
->msi_enabled
) {
1225 struct msi_desc
*entry
= first_pci_msi_entry(dev
);
1227 if (WARN_ON_ONCE(nr
>= entry
->nvec_used
))
1230 if (WARN_ON_ONCE(nr
> 0))
1234 return dev
->irq
+ nr
;
1236 EXPORT_SYMBOL(pci_irq_vector
);
1239 * pci_irq_get_affinity - return the affinity of a particular msi vector
1240 * @dev: PCI device to operate on
1241 * @nr: device-relative interrupt vector index (0-based).
1243 const struct cpumask
*pci_irq_get_affinity(struct pci_dev
*dev
, int nr
)
1245 if (dev
->msix_enabled
) {
1246 struct msi_desc
*entry
;
1249 for_each_pci_msi_entry(entry
, dev
) {
1251 return entry
->affinity
;
1256 } else if (dev
->msi_enabled
) {
1257 struct msi_desc
*entry
= first_pci_msi_entry(dev
);
1259 if (WARN_ON_ONCE(!entry
|| !entry
->affinity
||
1260 nr
>= entry
->nvec_used
))
1263 return &entry
->affinity
[nr
];
1265 return cpu_possible_mask
;
1268 EXPORT_SYMBOL(pci_irq_get_affinity
);
1271 * pci_irq_get_node - return the numa node of a particular msi vector
1272 * @pdev: PCI device to operate on
1273 * @vec: device-relative interrupt vector index (0-based).
1275 int pci_irq_get_node(struct pci_dev
*pdev
, int vec
)
1277 const struct cpumask
*mask
;
1279 mask
= pci_irq_get_affinity(pdev
, vec
);
1281 return local_memory_node(cpu_to_node(cpumask_first(mask
)));
1282 return dev_to_node(&pdev
->dev
);
1284 EXPORT_SYMBOL(pci_irq_get_node
);
1286 struct pci_dev
*msi_desc_to_pci_dev(struct msi_desc
*desc
)
1288 return to_pci_dev(desc
->dev
);
1290 EXPORT_SYMBOL(msi_desc_to_pci_dev
);
1292 void *msi_desc_to_pci_sysdata(struct msi_desc
*desc
)
1294 struct pci_dev
*dev
= msi_desc_to_pci_dev(desc
);
1296 return dev
->bus
->sysdata
;
1298 EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata
);
1300 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1302 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1303 * @irq_data: Pointer to interrupt data of the MSI interrupt
1304 * @msg: Pointer to the message
1306 void pci_msi_domain_write_msg(struct irq_data
*irq_data
, struct msi_msg
*msg
)
1308 struct msi_desc
*desc
= irq_data_get_msi_desc(irq_data
);
1311 * For MSI-X desc->irq is always equal to irq_data->irq. For
1312 * MSI only the first interrupt of MULTI MSI passes the test.
1314 if (desc
->irq
== irq_data
->irq
)
1315 __pci_write_msi_msg(desc
, msg
);
1319 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1320 * @dev: Pointer to the PCI device
1321 * @desc: Pointer to the msi descriptor
1323 * The ID number is only used within the irqdomain.
1325 irq_hw_number_t
pci_msi_domain_calc_hwirq(struct pci_dev
*dev
,
1326 struct msi_desc
*desc
)
1328 return (irq_hw_number_t
)desc
->msi_attrib
.entry_nr
|
1329 PCI_DEVID(dev
->bus
->number
, dev
->devfn
) << 11 |
1330 (pci_domain_nr(dev
->bus
) & 0xFFFFFFFF) << 27;
1333 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc
*desc
)
1335 return !desc
->msi_attrib
.is_msix
&& desc
->nvec_used
> 1;
1339 * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
1340 * @domain: The interrupt domain to check
1341 * @info: The domain info for verification
1342 * @dev: The device to check
1345 * 0 if the functionality is supported
1346 * 1 if Multi MSI is requested, but the domain does not support it
1347 * -ENOTSUPP otherwise
1349 int pci_msi_domain_check_cap(struct irq_domain
*domain
,
1350 struct msi_domain_info
*info
, struct device
*dev
)
1352 struct msi_desc
*desc
= first_pci_msi_entry(to_pci_dev(dev
));
1354 /* Special handling to support __pci_enable_msi_range() */
1355 if (pci_msi_desc_is_multi_msi(desc
) &&
1356 !(info
->flags
& MSI_FLAG_MULTI_PCI_MSI
))
1358 else if (desc
->msi_attrib
.is_msix
&& !(info
->flags
& MSI_FLAG_PCI_MSIX
))
1364 static int pci_msi_domain_handle_error(struct irq_domain
*domain
,
1365 struct msi_desc
*desc
, int error
)
1367 /* Special handling to support __pci_enable_msi_range() */
1368 if (pci_msi_desc_is_multi_msi(desc
) && error
== -ENOSPC
)
1374 #ifdef GENERIC_MSI_DOMAIN_OPS
1375 static void pci_msi_domain_set_desc(msi_alloc_info_t
*arg
,
1376 struct msi_desc
*desc
)
1379 arg
->hwirq
= pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc
),
1383 #define pci_msi_domain_set_desc NULL
1386 static struct msi_domain_ops pci_msi_domain_ops_default
= {
1387 .set_desc
= pci_msi_domain_set_desc
,
1388 .msi_check
= pci_msi_domain_check_cap
,
1389 .handle_error
= pci_msi_domain_handle_error
,
1392 static void pci_msi_domain_update_dom_ops(struct msi_domain_info
*info
)
1394 struct msi_domain_ops
*ops
= info
->ops
;
1397 info
->ops
= &pci_msi_domain_ops_default
;
1399 if (ops
->set_desc
== NULL
)
1400 ops
->set_desc
= pci_msi_domain_set_desc
;
1401 if (ops
->msi_check
== NULL
)
1402 ops
->msi_check
= pci_msi_domain_check_cap
;
1403 if (ops
->handle_error
== NULL
)
1404 ops
->handle_error
= pci_msi_domain_handle_error
;
1408 static void pci_msi_domain_update_chip_ops(struct msi_domain_info
*info
)
1410 struct irq_chip
*chip
= info
->chip
;
1413 if (!chip
->irq_write_msi_msg
)
1414 chip
->irq_write_msi_msg
= pci_msi_domain_write_msg
;
1415 if (!chip
->irq_mask
)
1416 chip
->irq_mask
= pci_msi_mask_irq
;
1417 if (!chip
->irq_unmask
)
1418 chip
->irq_unmask
= pci_msi_unmask_irq
;
1422 * pci_msi_create_irq_domain - Create a MSI interrupt domain
1423 * @fwnode: Optional fwnode of the interrupt controller
1424 * @info: MSI domain info
1425 * @parent: Parent irq domain
1427 * Updates the domain and chip ops and creates a MSI interrupt domain.
1430 * A domain pointer or NULL in case of failure.
1432 struct irq_domain
*pci_msi_create_irq_domain(struct fwnode_handle
*fwnode
,
1433 struct msi_domain_info
*info
,
1434 struct irq_domain
*parent
)
1436 struct irq_domain
*domain
;
1438 if (info
->flags
& MSI_FLAG_USE_DEF_DOM_OPS
)
1439 pci_msi_domain_update_dom_ops(info
);
1440 if (info
->flags
& MSI_FLAG_USE_DEF_CHIP_OPS
)
1441 pci_msi_domain_update_chip_ops(info
);
1443 info
->flags
|= MSI_FLAG_ACTIVATE_EARLY
;
1444 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE
))
1445 info
->flags
|= MSI_FLAG_MUST_REACTIVATE
;
1447 domain
= msi_create_irq_domain(fwnode
, info
, parent
);
1451 irq_domain_update_bus_token(domain
, DOMAIN_BUS_PCI_MSI
);
1454 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain
);
1457 * Users of the generic MSI infrastructure expect a device to have a single ID,
1458 * so with DMA aliases we have to pick the least-worst compromise. Devices with
1459 * DMA phantom functions tend to still emit MSIs from the real function number,
1460 * so we ignore those and only consider topological aliases where either the
1461 * alias device or RID appears on a different bus number. We also make the
1462 * reasonable assumption that bridges are walked in an upstream direction (so
1463 * the last one seen wins), and the much braver assumption that the most likely
1464 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1465 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1466 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1467 * for taking ownership all we can really do is close our eyes and hope...
1469 static int get_msi_id_cb(struct pci_dev
*pdev
, u16 alias
, void *data
)
1472 u8 bus
= PCI_BUS_NUM(*pa
);
1474 if (pdev
->bus
->number
!= bus
|| PCI_BUS_NUM(alias
) != bus
)
1481 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1482 * @domain: The interrupt domain
1483 * @pdev: The PCI device.
1485 * The RID for a device is formed from the alias, with a firmware
1486 * supplied mapping applied
1490 u32
pci_msi_domain_get_msi_rid(struct irq_domain
*domain
, struct pci_dev
*pdev
)
1492 struct device_node
*of_node
;
1493 u32 rid
= PCI_DEVID(pdev
->bus
->number
, pdev
->devfn
);
1495 pci_for_each_dma_alias(pdev
, get_msi_id_cb
, &rid
);
1497 of_node
= irq_domain_get_of_node(domain
);
1498 rid
= of_node
? of_msi_map_rid(&pdev
->dev
, of_node
, rid
) :
1499 iort_msi_map_rid(&pdev
->dev
, rid
);
1505 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1506 * @pdev: The PCI device
1508 * Use the firmware data to find a device-specific MSI domain
1509 * (i.e. not one that is set as a default).
1511 * Returns: The corresponding MSI domain or NULL if none has been found.
1513 struct irq_domain
*pci_msi_get_device_domain(struct pci_dev
*pdev
)
1515 struct irq_domain
*dom
;
1516 u32 rid
= PCI_DEVID(pdev
->bus
->number
, pdev
->devfn
);
1518 pci_for_each_dma_alias(pdev
, get_msi_id_cb
, &rid
);
1519 dom
= of_msi_map_get_device_domain(&pdev
->dev
, rid
);
1521 dom
= iort_get_device_domain(&pdev
->dev
, rid
);
1524 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */