Bjorn Helgaas | 7328c8f | 2018-01-26 11:45:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 3 | * PCI Message Signaled Interrupt (MSI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2003-2004 Intel |
| 6 | * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com) |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 7 | * Copyright (C) 2016 Christoph Hellwig. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 10 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | #include <linux/irq.h> |
| 13 | #include <linux/interrupt.h> |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pci.h> |
| 17 | #include <linux/proc_fs.h> |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 18 | #include <linux/msi.h> |
Dan Williams | 4fdadeb | 2007-04-26 18:21:38 -0700 | [diff] [blame] | 19 | #include <linux/smp.h> |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/io.h> |
Tomasz Nowicki | be2021b | 2016-09-12 20:32:22 +0200 | [diff] [blame] | 22 | #include <linux/acpi_iort.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 24 | #include <linux/irqdomain.h> |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 25 | #include <linux/of_irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Bjorn Helgaas | cbc40d5 | 2020-12-03 12:51:08 -0600 | [diff] [blame] | 29 | #ifdef CONFIG_PCI_MSI |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static int pci_msi_enable = 1; |
Yijing Wang | 38737d8 | 2014-10-27 10:44:36 +0800 | [diff] [blame] | 32 | int pci_msi_ignore_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Bjorn Helgaas | 527eee2 | 2013-04-17 17:44:48 -0600 | [diff] [blame] | 34 | #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) |
| 35 | |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 36 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 37 | static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 38 | { |
| 39 | struct irq_domain *domain; |
| 40 | |
Christoph Hellwig | 47feb41 | 2017-02-08 18:17:43 +0100 | [diff] [blame] | 41 | domain = dev_get_msi_domain(&dev->dev); |
Marc Zyngier | 3845d29 | 2015-12-04 10:28:14 -0600 | [diff] [blame] | 42 | if (domain && irq_domain_is_hierarchy(domain)) |
Christoph Hellwig | 699c4ce | 2017-02-08 18:17:44 +0100 | [diff] [blame] | 43 | return msi_domain_alloc_irqs(domain, &dev->dev, nvec); |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 44 | |
| 45 | return arch_setup_msi_irqs(dev, nvec, type); |
| 46 | } |
| 47 | |
| 48 | static void pci_msi_teardown_msi_irqs(struct pci_dev *dev) |
| 49 | { |
| 50 | struct irq_domain *domain; |
| 51 | |
Christoph Hellwig | 47feb41 | 2017-02-08 18:17:43 +0100 | [diff] [blame] | 52 | domain = dev_get_msi_domain(&dev->dev); |
Marc Zyngier | 3845d29 | 2015-12-04 10:28:14 -0600 | [diff] [blame] | 53 | if (domain && irq_domain_is_hierarchy(domain)) |
Christoph Hellwig | 699c4ce | 2017-02-08 18:17:44 +0100 | [diff] [blame] | 54 | msi_domain_free_irqs(domain, &dev->dev); |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 55 | else |
| 56 | arch_teardown_msi_irqs(dev); |
| 57 | } |
| 58 | #else |
| 59 | #define pci_msi_setup_msi_irqs arch_setup_msi_irqs |
| 60 | #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs |
| 61 | #endif |
Bjorn Helgaas | 527eee2 | 2013-04-17 17:44:48 -0600 | [diff] [blame] | 62 | |
Thomas Gleixner | 077ee78 | 2020-08-26 13:17:02 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 64 | /* Arch hooks */ |
Thomas Petazzoni | 4287d82 | 2013-08-09 22:27:06 +0200 | [diff] [blame] | 65 | int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc) |
| 66 | { |
Marc Zyngier | 3a05d08 | 2021-03-30 16:11:38 +0100 | [diff] [blame] | 67 | return -EINVAL; |
Thomas Petazzoni | 4287d82 | 2013-08-09 22:27:06 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void __weak arch_teardown_msi_irq(unsigned int irq) |
| 71 | { |
| 72 | } |
| 73 | |
Thomas Petazzoni | 4287d82 | 2013-08-09 22:27:06 +0200 | [diff] [blame] | 74 | int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 75 | { |
| 76 | struct msi_desc *entry; |
| 77 | int ret; |
| 78 | |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 79 | /* |
| 80 | * If an architecture wants to support multiple MSI, it needs to |
| 81 | * override arch_setup_msi_irqs() |
| 82 | */ |
| 83 | if (type == PCI_CAP_ID_MSI && nvec > 1) |
| 84 | return 1; |
| 85 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 86 | for_each_pci_msi_entry(entry, dev) { |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 87 | ret = arch_setup_msi_irq(dev, entry); |
Michael Ellerman | b5fbf53 | 2009-02-11 22:27:02 +1100 | [diff] [blame] | 88 | if (ret < 0) |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 89 | return ret; |
Michael Ellerman | b5fbf53 | 2009-02-11 22:27:02 +1100 | [diff] [blame] | 90 | if (ret > 0) |
| 91 | return -ENOSPC; |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Marc Zyngier | f8bcf24 | 2021-03-30 16:11:40 +0100 | [diff] [blame] | 97 | void __weak arch_teardown_msi_irqs(struct pci_dev *dev) |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 98 | { |
Jiang Liu | 63a7b17 | 2014-11-06 22:20:32 +0800 | [diff] [blame] | 99 | int i; |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 100 | struct msi_desc *entry; |
| 101 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 102 | for_each_pci_msi_entry(entry, dev) |
Jiang Liu | 63a7b17 | 2014-11-06 22:20:32 +0800 | [diff] [blame] | 103 | if (entry->irq) |
| 104 | for (i = 0; i < entry->nvec_used; i++) |
| 105 | arch_teardown_msi_irq(entry->irq + i); |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 106 | } |
Thomas Gleixner | 077ee78 | 2020-08-26 13:17:02 +0200 | [diff] [blame] | 107 | #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */ |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 108 | |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 109 | static void default_restore_msi_irq(struct pci_dev *dev, int irq) |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 110 | { |
| 111 | struct msi_desc *entry; |
| 112 | |
| 113 | entry = NULL; |
| 114 | if (dev->msix_enabled) { |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 115 | for_each_pci_msi_entry(entry, dev) { |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 116 | if (irq == entry->irq) |
| 117 | break; |
| 118 | } |
| 119 | } else if (dev->msi_enabled) { |
| 120 | entry = irq_get_msi_desc(irq); |
| 121 | } |
| 122 | |
| 123 | if (entry) |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 124 | __pci_write_msi_msg(entry, &entry->msg); |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 125 | } |
Thomas Petazzoni | 4287d82 | 2013-08-09 22:27:06 +0200 | [diff] [blame] | 126 | |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 127 | void __weak arch_restore_msi_irqs(struct pci_dev *dev) |
Thomas Petazzoni | 4287d82 | 2013-08-09 22:27:06 +0200 | [diff] [blame] | 128 | { |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 129 | return default_restore_msi_irqs(dev); |
Thomas Petazzoni | 4287d82 | 2013-08-09 22:27:06 +0200 | [diff] [blame] | 130 | } |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 131 | |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 132 | /* |
| 133 | * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to |
| 134 | * mask all MSI interrupts by clearing the MSI enable bit does not work |
| 135 | * reliably as devices without an INTx disable bit will then generate a |
| 136 | * level IRQ which will never be cleared. |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 137 | */ |
Thomas Gleixner | 7327cef | 2021-07-29 23:51:56 +0200 | [diff] [blame] | 138 | static inline __attribute_const__ u32 msi_multi_mask(struct msi_desc *desc) |
| 139 | { |
| 140 | /* Don't shift by >= width of type */ |
| 141 | if (desc->msi_attrib.multi_cap >= 5) |
| 142 | return 0xffffffff; |
| 143 | return (1 << (1 << desc->msi_attrib.multi_cap)) - 1; |
| 144 | } |
| 145 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 146 | static noinline void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Thomas Gleixner | 77e89af | 2021-07-29 23:51:47 +0200 | [diff] [blame] | 148 | raw_spinlock_t *lock = &desc->dev->msi_lock; |
| 149 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Thomas Gleixner | 77e89af | 2021-07-29 23:51:47 +0200 | [diff] [blame] | 151 | raw_spin_lock_irqsave(lock, flags); |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 152 | desc->msi_mask &= ~clear; |
| 153 | desc->msi_mask |= set; |
Jiang Liu | e39758e | 2015-07-09 16:00:43 +0800 | [diff] [blame] | 154 | pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos, |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 155 | desc->msi_mask); |
Thomas Gleixner | 77e89af | 2021-07-29 23:51:47 +0200 | [diff] [blame] | 156 | raw_spin_unlock_irqrestore(lock, flags); |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 159 | static inline void pci_msi_mask(struct msi_desc *desc, u32 mask) |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 160 | { |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 161 | pci_msi_update_mask(desc, 0, mask); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 164 | static inline void pci_msi_unmask(struct msi_desc *desc, u32 mask) |
Christoph Hellwig | 5eb6d66 | 2016-07-12 18:20:14 +0900 | [diff] [blame] | 165 | { |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 166 | pci_msi_update_mask(desc, mask, 0); |
| 167 | } |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 168 | |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 169 | static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc) |
| 170 | { |
| 171 | return desc->mask_base + desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
Christoph Hellwig | 5eb6d66 | 2016-07-12 18:20:14 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 174 | /* |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 175 | * This internal function does not flush PCI writes to the device. All |
| 176 | * users must ensure that they read from the device before either assuming |
| 177 | * that the device state is up to date, or returning out of this file. |
| 178 | * It does not affect the msi_desc::msix_ctrl cache either. Use with care! |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 179 | */ |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 180 | static void pci_msix_write_vector_ctrl(struct msi_desc *desc, u32 ctrl) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 181 | { |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 182 | void __iomem *desc_addr = pci_msix_desc_addr(desc); |
Yijing Wang | 38737d8 | 2014-10-27 10:44:36 +0800 | [diff] [blame] | 183 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 184 | writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL); |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 185 | } |
| 186 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 187 | static inline void pci_msix_mask(struct msi_desc *desc) |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 188 | { |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 189 | desc->msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 190 | pci_msix_write_vector_ctrl(desc, desc->msix_ctrl); |
| 191 | /* Flush write to device */ |
| 192 | readl(desc->mask_base); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 195 | static inline void pci_msix_unmask(struct msi_desc *desc) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 196 | { |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 197 | desc->msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 198 | pci_msix_write_vector_ctrl(desc, desc->msix_ctrl); |
| 199 | } |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 200 | |
Thomas Gleixner | fcacdfb | 2021-08-09 21:08:56 +0200 | [diff] [blame] | 201 | static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask) |
| 202 | { |
| 203 | if (pci_msi_ignore_mask || desc->msi_attrib.is_virtual) |
| 204 | return; |
| 205 | |
| 206 | if (desc->msi_attrib.is_msix) |
| 207 | pci_msix_mask(desc); |
| 208 | else if (desc->msi_attrib.maskbit) |
| 209 | pci_msi_mask(desc, mask); |
| 210 | } |
| 211 | |
| 212 | static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask) |
| 213 | { |
| 214 | if (pci_msi_ignore_mask || desc->msi_attrib.is_virtual) |
| 215 | return; |
| 216 | |
| 217 | if (desc->msi_attrib.is_msix) |
| 218 | pci_msix_unmask(desc); |
| 219 | else if (desc->msi_attrib.maskbit) |
| 220 | pci_msi_unmask(desc, mask); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Gleixner | 23ed8d5 | 2014-11-23 11:55:58 +0100 | [diff] [blame] | 223 | /** |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 224 | * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts |
Thomas Gleixner | 23ed8d5 | 2014-11-23 11:55:58 +0100 | [diff] [blame] | 225 | * @data: pointer to irqdata associated to that interrupt |
| 226 | */ |
| 227 | void pci_msi_mask_irq(struct irq_data *data) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 228 | { |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 229 | struct msi_desc *desc = irq_data_get_msi_desc(data); |
| 230 | |
| 231 | __pci_msi_mask_desc(desc, BIT(data->irq - desc->irq)); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 232 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(pci_msi_mask_irq); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 234 | |
Thomas Gleixner | 23ed8d5 | 2014-11-23 11:55:58 +0100 | [diff] [blame] | 235 | /** |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 236 | * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts |
Thomas Gleixner | 23ed8d5 | 2014-11-23 11:55:58 +0100 | [diff] [blame] | 237 | * @data: pointer to irqdata associated to that interrupt |
| 238 | */ |
| 239 | void pci_msi_unmask_irq(struct irq_data *data) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 240 | { |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 241 | struct msi_desc *desc = irq_data_get_msi_desc(data); |
| 242 | |
| 243 | __pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 245 | EXPORT_SYMBOL_GPL(pci_msi_unmask_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 247 | void default_restore_msi_irqs(struct pci_dev *dev) |
| 248 | { |
| 249 | struct msi_desc *entry; |
| 250 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 251 | for_each_pci_msi_entry(entry, dev) |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 252 | default_restore_msi_irq(dev, entry->irq); |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 253 | } |
| 254 | |
Jiang Liu | 891d4a4 | 2014-11-09 23:10:33 +0800 | [diff] [blame] | 255 | void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 256 | { |
Jiang Liu | e39758e | 2015-07-09 16:00:43 +0800 | [diff] [blame] | 257 | struct pci_dev *dev = msi_desc_to_pci_dev(entry); |
| 258 | |
| 259 | BUG_ON(dev->current_state != PCI_D0); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 260 | |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 261 | if (entry->msi_attrib.is_msix) { |
Christoph Hellwig | 5eb6d66 | 2016-07-12 18:20:14 +0900 | [diff] [blame] | 262 | void __iomem *base = pci_msix_desc_addr(entry); |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 263 | |
Thomas Gleixner | b296aba | 2021-07-29 23:51:55 +0200 | [diff] [blame] | 264 | if (WARN_ON_ONCE(entry->msi_attrib.is_virtual)) |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 265 | return; |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 266 | |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 267 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR); |
| 268 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR); |
| 269 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA); |
| 270 | } else { |
Bjorn Helgaas | f532216 | 2013-04-17 17:34:36 -0600 | [diff] [blame] | 271 | int pos = dev->msi_cap; |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 272 | u16 data; |
| 273 | |
Bjorn Helgaas | 9925ad0 | 2013-04-17 17:39:57 -0600 | [diff] [blame] | 274 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, |
| 275 | &msg->address_lo); |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 276 | if (entry->msi_attrib.is_64) { |
Bjorn Helgaas | 9925ad0 | 2013-04-17 17:39:57 -0600 | [diff] [blame] | 277 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, |
| 278 | &msg->address_hi); |
Bjorn Helgaas | 2f22134 | 2013-04-17 17:41:13 -0600 | [diff] [blame] | 279 | pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data); |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 280 | } else { |
| 281 | msg->address_hi = 0; |
Bjorn Helgaas | 2f22134 | 2013-04-17 17:41:13 -0600 | [diff] [blame] | 282 | pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data); |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 283 | } |
| 284 | msg->data = data; |
| 285 | } |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 288 | void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 289 | { |
Jiang Liu | e39758e | 2015-07-09 16:00:43 +0800 | [diff] [blame] | 290 | struct pci_dev *dev = msi_desc_to_pci_dev(entry); |
| 291 | |
Keith Busch | 0170591 | 2017-03-29 22:49:11 -0500 | [diff] [blame] | 292 | if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) { |
Ben Hutchings | fcd097f | 2010-06-17 20:16:36 +0100 | [diff] [blame] | 293 | /* Don't touch the hardware now */ |
| 294 | } else if (entry->msi_attrib.is_msix) { |
Christoph Hellwig | 5eb6d66 | 2016-07-12 18:20:14 +0900 | [diff] [blame] | 295 | void __iomem *base = pci_msix_desc_addr(entry); |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 296 | u32 ctrl = entry->msix_ctrl; |
| 297 | bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT); |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 298 | |
Thomas Gleixner | b296aba | 2021-07-29 23:51:55 +0200 | [diff] [blame] | 299 | if (entry->msi_attrib.is_virtual) |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 300 | goto skip; |
| 301 | |
Thomas Gleixner | da181dc | 2021-07-29 23:51:42 +0200 | [diff] [blame] | 302 | /* |
| 303 | * The specification mandates that the entry is masked |
| 304 | * when the message is modified: |
| 305 | * |
| 306 | * "If software changes the Address or Data value of an |
| 307 | * entry while the entry is unmasked, the result is |
| 308 | * undefined." |
| 309 | */ |
| 310 | if (unmasked) |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 311 | pci_msix_write_vector_ctrl(entry, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT); |
Thomas Gleixner | da181dc | 2021-07-29 23:51:42 +0200 | [diff] [blame] | 312 | |
Hidetoshi Seto | 2c21fd4 | 2009-06-23 17:40:04 +0900 | [diff] [blame] | 313 | writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR); |
| 314 | writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR); |
| 315 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA); |
Thomas Gleixner | da181dc | 2021-07-29 23:51:42 +0200 | [diff] [blame] | 316 | |
| 317 | if (unmasked) |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 318 | pci_msix_write_vector_ctrl(entry, ctrl); |
Thomas Gleixner | b9255a7 | 2021-07-29 23:51:43 +0200 | [diff] [blame] | 319 | |
| 320 | /* Ensure that the writes are visible in the device */ |
| 321 | readl(base + PCI_MSIX_ENTRY_DATA); |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 322 | } else { |
Bjorn Helgaas | f532216 | 2013-04-17 17:34:36 -0600 | [diff] [blame] | 323 | int pos = dev->msi_cap; |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 324 | u16 msgctl; |
| 325 | |
Bjorn Helgaas | f84ecd28 | 2013-04-17 17:38:32 -0600 | [diff] [blame] | 326 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 327 | msgctl &= ~PCI_MSI_FLAGS_QSIZE; |
| 328 | msgctl |= entry->msi_attrib.multiple << 4; |
Bjorn Helgaas | f84ecd28 | 2013-04-17 17:38:32 -0600 | [diff] [blame] | 329 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 330 | |
Bjorn Helgaas | 9925ad0 | 2013-04-17 17:39:57 -0600 | [diff] [blame] | 331 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, |
| 332 | msg->address_lo); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 333 | if (entry->msi_attrib.is_64) { |
Bjorn Helgaas | 9925ad0 | 2013-04-17 17:39:57 -0600 | [diff] [blame] | 334 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, |
| 335 | msg->address_hi); |
Bjorn Helgaas | 2f22134 | 2013-04-17 17:41:13 -0600 | [diff] [blame] | 336 | pci_write_config_word(dev, pos + PCI_MSI_DATA_64, |
| 337 | msg->data); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 338 | } else { |
Bjorn Helgaas | 2f22134 | 2013-04-17 17:41:13 -0600 | [diff] [blame] | 339 | pci_write_config_word(dev, pos + PCI_MSI_DATA_32, |
| 340 | msg->data); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 341 | } |
Thomas Gleixner | b9255a7 | 2021-07-29 23:51:43 +0200 | [diff] [blame] | 342 | /* Ensure that the writes are visible in the device */ |
| 343 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 344 | } |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 345 | |
| 346 | skip: |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 347 | entry->msg = *msg; |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 348 | |
| 349 | if (entry->write_msi_msg) |
| 350 | entry->write_msi_msg(entry, entry->write_msi_msg_data); |
| 351 | |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 354 | void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 355 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 356 | struct msi_desc *entry = irq_get_msi_desc(irq); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 357 | |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 358 | __pci_write_msi_msg(entry, msg); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 359 | } |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 360 | EXPORT_SYMBOL_GPL(pci_write_msi_msg); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 361 | |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 362 | static void free_msi_irqs(struct pci_dev *dev) |
| 363 | { |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 364 | struct list_head *msi_list = dev_to_msi_list(&dev->dev); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 365 | struct msi_desc *entry, *tmp; |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 366 | int i; |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 367 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 368 | for_each_pci_msi_entry(entry, dev) |
Jiang Liu | 63a7b17 | 2014-11-06 22:20:32 +0800 | [diff] [blame] | 369 | if (entry->irq) |
| 370 | for (i = 0; i < entry->nvec_used; i++) |
| 371 | BUG_ON(irq_has_action(entry->irq + i)); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 372 | |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 373 | pci_msi_teardown_msi_irqs(dev); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 374 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 375 | list_for_each_entry_safe(entry, tmp, msi_list, list) { |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 376 | if (entry->msi_attrib.is_msix) { |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 377 | if (list_is_last(&entry->list, msi_list)) |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 378 | iounmap(entry->mask_base); |
| 379 | } |
Neil Horman | 424eb39 | 2012-01-03 10:29:54 -0500 | [diff] [blame] | 380 | |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 381 | list_del(&entry->list); |
Prarit Bhargava | 81efbad | 2017-02-15 11:53:08 -0500 | [diff] [blame] | 382 | free_msi_entry(entry); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 383 | } |
Greg Kroah-Hartman | 1c51b50 | 2013-12-19 12:30:17 -0800 | [diff] [blame] | 384 | |
| 385 | if (dev->msi_irq_groups) { |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 386 | msi_destroy_sysfs(&dev->dev, dev->msi_irq_groups); |
Greg Kroah-Hartman | 1c51b50 | 2013-12-19 12:30:17 -0800 | [diff] [blame] | 387 | dev->msi_irq_groups = NULL; |
| 388 | } |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 389 | } |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame] | 390 | |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 391 | static void pci_intx_for_msi(struct pci_dev *dev, int enable) |
| 392 | { |
| 393 | if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG)) |
| 394 | pci_intx(dev, enable); |
| 395 | } |
| 396 | |
Bjorn Helgaas | 830dfe8 | 2020-12-03 12:51:09 -0600 | [diff] [blame] | 397 | static void pci_msi_set_enable(struct pci_dev *dev, int enable) |
| 398 | { |
| 399 | u16 control; |
| 400 | |
| 401 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
| 402 | control &= ~PCI_MSI_FLAGS_ENABLE; |
| 403 | if (enable) |
| 404 | control |= PCI_MSI_FLAGS_ENABLE; |
| 405 | pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); |
| 406 | } |
| 407 | |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 408 | static void __pci_restore_msi_state(struct pci_dev *dev) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 409 | { |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 410 | u16 control; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 411 | struct msi_desc *entry; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 412 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 413 | if (!dev->msi_enabled) |
| 414 | return; |
| 415 | |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 416 | entry = irq_get_msi_desc(dev->irq); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 417 | |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 418 | pci_intx_for_msi(dev, 0); |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 419 | pci_msi_set_enable(dev, 0); |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 420 | arch_restore_msi_irqs(dev); |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 421 | |
Bjorn Helgaas | f532216 | 2013-04-17 17:34:36 -0600 | [diff] [blame] | 422 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 423 | pci_msi_update_mask(entry, 0, 0); |
Jesse Barnes | abad2ec | 2008-08-07 08:52:37 -0700 | [diff] [blame] | 424 | control &= ~PCI_MSI_FLAGS_QSIZE; |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 425 | control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE; |
Bjorn Helgaas | f532216 | 2013-04-17 17:34:36 -0600 | [diff] [blame] | 426 | pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 427 | } |
| 428 | |
Bjorn Helgaas | 830dfe8 | 2020-12-03 12:51:09 -0600 | [diff] [blame] | 429 | static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set) |
| 430 | { |
| 431 | u16 ctrl; |
| 432 | |
| 433 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl); |
| 434 | ctrl &= ~clear; |
| 435 | ctrl |= set; |
| 436 | pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl); |
| 437 | } |
| 438 | |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 439 | static void __pci_restore_msix_state(struct pci_dev *dev) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 440 | { |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 441 | struct msi_desc *entry; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 442 | |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 443 | if (!dev->msix_enabled) |
| 444 | return; |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 445 | BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 446 | |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 447 | /* route the table */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 448 | pci_intx_for_msi(dev, 0); |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 449 | pci_msix_clear_and_set_ctrl(dev, 0, |
Yijing Wang | 66f0d0c | 2014-06-19 16:29:53 +0800 | [diff] [blame] | 450 | PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 451 | |
DuanZhenzhong | ac8344c | 2013-12-04 13:09:16 +0800 | [diff] [blame] | 452 | arch_restore_msi_irqs(dev); |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 453 | for_each_pci_msi_entry(entry, dev) |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 454 | pci_msix_write_vector_ctrl(entry, entry->msix_ctrl); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 455 | |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 456 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 457 | } |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 458 | |
| 459 | void pci_restore_msi_state(struct pci_dev *dev) |
| 460 | { |
| 461 | __pci_restore_msi_state(dev); |
| 462 | __pci_restore_msix_state(dev); |
| 463 | } |
Linas Vepstas | 94688cf | 2007-11-07 15:43:59 -0600 | [diff] [blame] | 464 | EXPORT_SYMBOL_GPL(pci_restore_msi_state); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 465 | |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 466 | static struct msi_desc * |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 467 | msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd) |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 468 | { |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 469 | struct irq_affinity_desc *masks = NULL; |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 470 | struct msi_desc *entry; |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 471 | u16 control; |
| 472 | |
Christoph Hellwig | 8e1101d | 2017-08-25 18:58:42 -0500 | [diff] [blame] | 473 | if (affd) |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 474 | masks = irq_create_affinity_masks(nvec, affd); |
Christoph Hellwig | 8e1101d | 2017-08-25 18:58:42 -0500 | [diff] [blame] | 475 | |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 476 | /* MSI Entry Initialization */ |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 477 | entry = alloc_msi_entry(&dev->dev, nvec, masks); |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 478 | if (!entry) |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 479 | goto out; |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 480 | |
| 481 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
| 482 | |
| 483 | entry->msi_attrib.is_msix = 0; |
| 484 | entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT); |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 485 | entry->msi_attrib.is_virtual = 0; |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 486 | entry->msi_attrib.entry_nr = 0; |
| 487 | entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT); |
| 488 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 489 | entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1; |
Jiang Liu | 63a7b17 | 2014-11-06 22:20:32 +0800 | [diff] [blame] | 490 | entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec)); |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 491 | |
| 492 | if (control & PCI_MSI_FLAGS_64BIT) |
| 493 | entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64; |
| 494 | else |
| 495 | entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32; |
| 496 | |
| 497 | /* Save the initial mask status */ |
| 498 | if (entry->msi_attrib.maskbit) |
Thomas Gleixner | 67961e7 | 2021-07-29 23:51:53 +0200 | [diff] [blame] | 499 | pci_read_config_dword(dev, entry->mask_pos, &entry->msi_mask); |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 500 | |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 501 | out: |
| 502 | kfree(masks); |
Yijing Wang | d873b4d | 2014-07-08 10:07:23 +0800 | [diff] [blame] | 503 | return entry; |
| 504 | } |
| 505 | |
Benjamin Herrenschmidt | f144d14 | 2014-10-03 15:13:24 +1000 | [diff] [blame] | 506 | static int msi_verify_entries(struct pci_dev *dev) |
| 507 | { |
| 508 | struct msi_desc *entry; |
| 509 | |
Thomas Gleixner | a6e8b94 | 2021-07-29 23:51:52 +0200 | [diff] [blame] | 510 | if (!dev->no_64bit_msi) |
| 511 | return 0; |
| 512 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 513 | for_each_pci_msi_entry(entry, dev) { |
Thomas Gleixner | a6e8b94 | 2021-07-29 23:51:52 +0200 | [diff] [blame] | 514 | if (entry->msg.address_hi) { |
Vidya Sagar | 2053230 | 2020-12-03 12:51:10 -0600 | [diff] [blame] | 515 | pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n", |
| 516 | entry->msg.address_hi, entry->msg.address_lo); |
| 517 | return -EIO; |
| 518 | } |
Benjamin Herrenschmidt | f144d14 | 2014-10-03 15:13:24 +1000 | [diff] [blame] | 519 | } |
| 520 | return 0; |
| 521 | } |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | /** |
| 524 | * msi_capability_init - configure device's MSI capability structure |
| 525 | * @dev: pointer to the pci_dev data structure of MSI device function |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 526 | * @nvec: number of interrupts to allocate |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 527 | * @affd: description of automatic IRQ affinity assignments (may be %NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 529 | * Setup the MSI capability structure of the device with the requested |
| 530 | * number of interrupts. A return value of zero indicates the successful |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 531 | * setup of an entry with the new MSI IRQ. A negative return value indicates |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 532 | * an error, and a positive return value indicates the number of interrupts |
| 533 | * which could have been allocated. |
| 534 | */ |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 535 | static int msi_capability_init(struct pci_dev *dev, int nvec, |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 536 | struct irq_affinity *affd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
| 538 | struct msi_desc *entry; |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 539 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 541 | pci_msi_set_enable(dev, 0); /* Disable MSI during set up */ |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 542 | |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 543 | entry = msi_setup_entry(dev, nvec, affd); |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 544 | if (!entry) |
| 545 | return -ENOMEM; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 546 | |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 547 | /* All MSIs are unmasked by default; mask them all */ |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 548 | pci_msi_mask(entry, msi_multi_mask(entry)); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 549 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 550 | list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* Configure MSI capability structure */ |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 553 | ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI); |
Thomas Gleixner | 8eb5ce3 | 2021-07-29 23:51:54 +0200 | [diff] [blame] | 554 | if (ret) |
| 555 | goto err; |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 556 | |
Benjamin Herrenschmidt | f144d14 | 2014-10-03 15:13:24 +1000 | [diff] [blame] | 557 | ret = msi_verify_entries(dev); |
Thomas Gleixner | 8eb5ce3 | 2021-07-29 23:51:54 +0200 | [diff] [blame] | 558 | if (ret) |
| 559 | goto err; |
Benjamin Herrenschmidt | f144d14 | 2014-10-03 15:13:24 +1000 | [diff] [blame] | 560 | |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 561 | dev->msi_irq_groups = msi_populate_sysfs(&dev->dev); |
| 562 | if (IS_ERR(dev->msi_irq_groups)) { |
| 563 | ret = PTR_ERR(dev->msi_irq_groups); |
Thomas Gleixner | 8eb5ce3 | 2021-07-29 23:51:54 +0200 | [diff] [blame] | 564 | goto err; |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 567 | /* Set MSI enabled bits */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 568 | pci_intx_for_msi(dev, 0); |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 569 | pci_msi_set_enable(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 570 | dev->msi_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Jiang Liu | 5f22699 | 2015-07-30 14:00:08 -0500 | [diff] [blame] | 572 | pcibios_free_irq(dev); |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 573 | dev->irq = entry->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return 0; |
Thomas Gleixner | 8eb5ce3 | 2021-07-29 23:51:54 +0200 | [diff] [blame] | 575 | |
| 576 | err: |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 577 | pci_msi_unmask(entry, msi_multi_mask(entry)); |
Thomas Gleixner | 8eb5ce3 | 2021-07-29 23:51:54 +0200 | [diff] [blame] | 578 | free_msi_irqs(dev); |
| 579 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Krzysztof Wilczyński | fd1ae23 | 2021-10-13 01:41:36 +0000 | [diff] [blame^] | 582 | static void __iomem *msix_map_region(struct pci_dev *dev, |
| 583 | unsigned int nr_entries) |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 584 | { |
Kenji Kaneshige | 4302e0f | 2010-06-17 10:42:44 +0900 | [diff] [blame] | 585 | resource_size_t phys_addr; |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 586 | u32 table_offset; |
Yijing Wang | 6a878e5 | 2015-01-28 09:52:17 +0800 | [diff] [blame] | 587 | unsigned long flags; |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 588 | u8 bir; |
| 589 | |
Bjorn Helgaas | 909094c | 2013-04-17 17:43:40 -0600 | [diff] [blame] | 590 | pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE, |
| 591 | &table_offset); |
Bjorn Helgaas | 4d18760 | 2013-04-17 18:10:07 -0600 | [diff] [blame] | 592 | bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR); |
Yijing Wang | 6a878e5 | 2015-01-28 09:52:17 +0800 | [diff] [blame] | 593 | flags = pci_resource_flags(dev, bir); |
| 594 | if (!flags || (flags & IORESOURCE_UNSET)) |
| 595 | return NULL; |
| 596 | |
Bjorn Helgaas | 4d18760 | 2013-04-17 18:10:07 -0600 | [diff] [blame] | 597 | table_offset &= PCI_MSIX_TABLE_OFFSET; |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 598 | phys_addr = pci_resource_start(dev, bir) + table_offset; |
| 599 | |
Christoph Hellwig | 4bdc0d6 | 2020-01-06 09:43:50 +0100 | [diff] [blame] | 600 | return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 601 | } |
| 602 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame] | 603 | static int msix_setup_entries(struct pci_dev *dev, void __iomem *base, |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 604 | struct msix_entry *entries, int nvec, |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 605 | struct irq_affinity *affd) |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 606 | { |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 607 | struct irq_affinity_desc *curmsk, *masks = NULL; |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 608 | struct msi_desc *entry; |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 609 | void __iomem *addr; |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 610 | int ret, i; |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 611 | int vec_count = pci_msix_vec_count(dev); |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 612 | |
Christoph Hellwig | 8e1101d | 2017-08-25 18:58:42 -0500 | [diff] [blame] | 613 | if (affd) |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 614 | masks = irq_create_affinity_masks(nvec, affd); |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 615 | |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 616 | for (i = 0, curmsk = masks; i < nvec; i++) { |
| 617 | entry = alloc_msi_entry(&dev->dev, 1, curmsk); |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 618 | if (!entry) { |
| 619 | if (!i) |
| 620 | iounmap(base); |
| 621 | else |
| 622 | free_msi_irqs(dev); |
| 623 | /* No enough memory. Don't try again */ |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 624 | ret = -ENOMEM; |
| 625 | goto out; |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | entry->msi_attrib.is_msix = 1; |
| 629 | entry->msi_attrib.is_64 = 1; |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 630 | |
Christoph Hellwig | 3ac020e | 2016-07-12 18:20:16 +0900 | [diff] [blame] | 631 | if (entries) |
| 632 | entry->msi_attrib.entry_nr = entries[i].entry; |
| 633 | else |
| 634 | entry->msi_attrib.entry_nr = i; |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 635 | |
| 636 | entry->msi_attrib.is_virtual = |
| 637 | entry->msi_attrib.entry_nr >= vec_count; |
| 638 | |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 639 | entry->msi_attrib.default_irq = dev->irq; |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 640 | entry->mask_base = base; |
| 641 | |
Thomas Gleixner | b296aba | 2021-07-29 23:51:55 +0200 | [diff] [blame] | 642 | if (!entry->msi_attrib.is_virtual) { |
| 643 | addr = pci_msix_desc_addr(entry); |
Thomas Gleixner | 67961e7 | 2021-07-29 23:51:53 +0200 | [diff] [blame] | 644 | entry->msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL); |
Thomas Gleixner | b296aba | 2021-07-29 23:51:55 +0200 | [diff] [blame] | 645 | } |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 646 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 647 | list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 648 | if (masks) |
| 649 | curmsk++; |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 650 | } |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 651 | ret = 0; |
| 652 | out: |
| 653 | kfree(masks); |
Christophe JAILLET | 3adfb57 | 2017-01-27 16:14:53 +0100 | [diff] [blame] | 654 | return ret; |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 655 | } |
| 656 | |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 657 | static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries) |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 658 | { |
| 659 | struct msi_desc *entry; |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 660 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 661 | for_each_pci_msi_entry(entry, dev) { |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 662 | if (entries) { |
| 663 | entries->vector = entry->irq; |
| 664 | entries++; |
| 665 | } |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 669 | static void msix_mask_all(void __iomem *base, int tsize) |
| 670 | { |
| 671 | u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 672 | int i; |
| 673 | |
Marek Marczykowski-Górecki | 1a519dc | 2021-08-26 19:03:42 +0200 | [diff] [blame] | 674 | if (pci_msi_ignore_mask) |
| 675 | return; |
| 676 | |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 677 | for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE) |
| 678 | writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL); |
| 679 | } |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | /** |
| 682 | * msix_capability_init - configure device's MSI-X capability |
| 683 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 684 | * @entries: pointer to an array of struct msix_entry entries |
| 685 | * @nvec: number of @entries |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 686 | * @affd: Optional pointer to enable automatic affinity assignment |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 688 | * Setup the MSI-X capability structure of device function with a |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 689 | * single MSI-X IRQ. A return of zero indicates the successful setup of |
| 690 | * requested MSI-X entries with allocated IRQs or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | **/ |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 692 | static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 693 | int nvec, struct irq_affinity *affd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | void __iomem *base; |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 696 | int ret, tsize; |
| 697 | u16 control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Thomas Gleixner | 4385539 | 2021-07-29 23:51:40 +0200 | [diff] [blame] | 699 | /* |
| 700 | * Some devices require MSI-X to be enabled before the MSI-X |
| 701 | * registers can be accessed. Mask all the vectors to prevent |
| 702 | * interrupts coming in before they're fully set up. |
| 703 | */ |
| 704 | pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL | |
| 705 | PCI_MSIX_FLAGS_ENABLE); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 706 | |
Yijing Wang | 66f0d0c | 2014-06-19 16:29:53 +0800 | [diff] [blame] | 707 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* Request & Map MSI-X table region */ |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 709 | tsize = msix_table_size(control); |
| 710 | base = msix_map_region(dev, tsize); |
Thomas Gleixner | 4385539 | 2021-07-29 23:51:40 +0200 | [diff] [blame] | 711 | if (!base) { |
| 712 | ret = -ENOMEM; |
| 713 | goto out_disable; |
| 714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 716 | /* Ensure that all table entries are masked. */ |
| 717 | msix_mask_all(base, tsize); |
| 718 | |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 719 | ret = msix_setup_entries(dev, base, entries, nvec, affd); |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 720 | if (ret) |
Thomas Gleixner | 4385539 | 2021-07-29 23:51:40 +0200 | [diff] [blame] | 721 | goto out_disable; |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 722 | |
Jiang Liu | 8e047ad | 2014-11-15 22:24:07 +0800 | [diff] [blame] | 723 | ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 724 | if (ret) |
Alexander Gordeev | 2adc790 | 2013-12-16 09:34:56 +0100 | [diff] [blame] | 725 | goto out_avail; |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 726 | |
Benjamin Herrenschmidt | f144d14 | 2014-10-03 15:13:24 +1000 | [diff] [blame] | 727 | /* Check if all MSI entries honor device restrictions */ |
| 728 | ret = msi_verify_entries(dev); |
| 729 | if (ret) |
| 730 | goto out_free; |
| 731 | |
Thomas Gleixner | 7d5ec3d | 2021-07-29 23:51:41 +0200 | [diff] [blame] | 732 | msix_update_entries(dev, entries); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 733 | |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 734 | dev->msi_irq_groups = msi_populate_sysfs(&dev->dev); |
| 735 | if (IS_ERR(dev->msi_irq_groups)) { |
| 736 | ret = PTR_ERR(dev->msi_irq_groups); |
Alexander Gordeev | 2adc790 | 2013-12-16 09:34:56 +0100 | [diff] [blame] | 737 | goto out_free; |
Barry Song | 2f17081 | 2021-08-13 15:56:27 +1200 | [diff] [blame] | 738 | } |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 739 | |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 740 | /* Set MSI-X enabled bits and unmask the function */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 741 | pci_intx_for_msi(dev, 0); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 742 | dev->msix_enabled = 1; |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 743 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); |
Matthew Wilcox | 8d18101 | 2009-05-08 07:13:33 -0600 | [diff] [blame] | 744 | |
Jiang Liu | 5f22699 | 2015-07-30 14:00:08 -0500 | [diff] [blame] | 745 | pcibios_free_irq(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | return 0; |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 747 | |
Alexander Gordeev | 2adc790 | 2013-12-16 09:34:56 +0100 | [diff] [blame] | 748 | out_avail: |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 749 | if (ret < 0) { |
| 750 | /* |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 751 | * If we had some success, report the number of IRQs |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 752 | * we succeeded in setting up. |
| 753 | */ |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 754 | struct msi_desc *entry; |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 755 | int avail = 0; |
| 756 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 757 | for_each_pci_msi_entry(entry, dev) { |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 758 | if (entry->irq != 0) |
| 759 | avail++; |
| 760 | } |
| 761 | if (avail != 0) |
| 762 | ret = avail; |
| 763 | } |
| 764 | |
Alexander Gordeev | 2adc790 | 2013-12-16 09:34:56 +0100 | [diff] [blame] | 765 | out_free: |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 766 | free_msi_irqs(dev); |
| 767 | |
Thomas Gleixner | 4385539 | 2021-07-29 23:51:40 +0200 | [diff] [blame] | 768 | out_disable: |
| 769 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); |
| 770 | |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 771 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 775 | * pci_msi_supported - check whether MSI may be enabled on a device |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 776 | * @dev: pointer to the pci_dev data structure of MSI device function |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 777 | * @nvec: how many MSIs have been requested? |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 778 | * |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 779 | * Look at global flags, the device itself, and its parent buses |
Michael Ellerman | 17bbc12 | 2007-04-05 17:19:07 +1000 | [diff] [blame] | 780 | * to determine if MSI/-X are supported for the device. If MSI/-X is |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 781 | * supported return 1, else return 0. |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 782 | **/ |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 783 | static int pci_msi_supported(struct pci_dev *dev, int nvec) |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 784 | { |
| 785 | struct pci_bus *bus; |
| 786 | |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 787 | /* MSI must be globally enabled and supported by the device */ |
Alexander Gordeev | 27e2060 | 2014-09-23 14:25:11 -0600 | [diff] [blame] | 788 | if (!pci_msi_enable) |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 789 | return 0; |
Alexander Gordeev | 27e2060 | 2014-09-23 14:25:11 -0600 | [diff] [blame] | 790 | |
Bjorn Helgaas | 901c4dd | 2019-10-14 16:17:05 -0500 | [diff] [blame] | 791 | if (!dev || dev->no_msi) |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 792 | return 0; |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 793 | |
Michael Ellerman | 314e77b | 2007-04-05 17:19:12 +1000 | [diff] [blame] | 794 | /* |
| 795 | * You can't ask to have 0 or less MSIs configured. |
| 796 | * a) it's stupid .. |
| 797 | * b) the list manipulation code assumes nvec >= 1. |
| 798 | */ |
| 799 | if (nvec < 1) |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 800 | return 0; |
Michael Ellerman | 314e77b | 2007-04-05 17:19:12 +1000 | [diff] [blame] | 801 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 802 | /* |
| 803 | * Any bridge which does NOT route MSI transactions from its |
| 804 | * secondary bus to its primary bus must set NO_MSI flag on |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 805 | * the secondary pci_bus. |
Marc Zyngier | 61af692 | 2021-03-30 16:11:44 +0100 | [diff] [blame] | 806 | * |
| 807 | * The NO_MSI flag can either be set directly by: |
| 808 | * - arch-specific PCI host bus controller drivers (deprecated) |
| 809 | * - quirks for specific PCI bridges |
| 810 | * |
| 811 | * or indirectly by platform-specific PCI host bridge drivers by |
| 812 | * advertising the 'msi_domain' property, which results in |
| 813 | * the NO_MSI flag when no MSI domain is found for this bridge |
| 814 | * at probe time. |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 815 | */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 816 | for (bus = dev->bus; bus; bus = bus->parent) |
| 817 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 818 | return 0; |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 819 | |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 820 | return 1; |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /** |
Alexander Gordeev | d1ac1d2 | 2013-12-30 08:28:13 +0100 | [diff] [blame] | 824 | * pci_msi_vec_count - Return the number of MSI vectors a device can send |
| 825 | * @dev: device to report about |
| 826 | * |
| 827 | * This function returns the number of MSI vectors a device requested via |
| 828 | * Multiple Message Capable register. It returns a negative errno if the |
| 829 | * device is not capable sending MSI interrupts. Otherwise, the call succeeds |
| 830 | * and returns a power of two, up to a maximum of 2^5 (32), according to the |
| 831 | * MSI specification. |
| 832 | **/ |
| 833 | int pci_msi_vec_count(struct pci_dev *dev) |
| 834 | { |
| 835 | int ret; |
| 836 | u16 msgctl; |
| 837 | |
| 838 | if (!dev->msi_cap) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl); |
| 842 | ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1); |
| 843 | |
| 844 | return ret; |
| 845 | } |
| 846 | EXPORT_SYMBOL(pci_msi_vec_count); |
| 847 | |
Bjorn Helgaas | 688769f | 2017-03-09 15:45:14 -0600 | [diff] [blame] | 848 | static void pci_msi_shutdown(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 850 | struct msi_desc *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
Michael Ellerman | 128bc5f | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 852 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 853 | return; |
| 854 | |
Jiang Liu | 5004e98 | 2015-07-09 16:00:41 +0800 | [diff] [blame] | 855 | BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); |
Jiang Liu | 4a7cc83 | 2015-07-09 16:00:44 +0800 | [diff] [blame] | 856 | desc = first_pci_msi_entry(dev); |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 857 | |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 858 | pci_msi_set_enable(dev, 0); |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 859 | pci_intx_for_msi(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 860 | dev->msi_enabled = 0; |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 861 | |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 862 | /* Return the device with MSI unmasked as initial states */ |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 863 | pci_msi_unmask(desc, msi_multi_mask(desc)); |
Michael Ellerman | e387b9e | 2007-03-22 21:51:27 +1100 | [diff] [blame] | 864 | |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 865 | /* Restore dev->irq to its default pin-assertion IRQ */ |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 866 | dev->irq = desc->msi_attrib.default_irq; |
Jiang Liu | 5f22699 | 2015-07-30 14:00:08 -0500 | [diff] [blame] | 867 | pcibios_alloc_irq(dev); |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 868 | } |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 869 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 870 | void pci_disable_msi(struct pci_dev *dev) |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 871 | { |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 872 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
| 873 | return; |
| 874 | |
| 875 | pci_msi_shutdown(dev); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 876 | free_msi_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 878 | EXPORT_SYMBOL(pci_disable_msi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | /** |
Alexander Gordeev | ff1aa43 | 2013-12-30 08:28:15 +0100 | [diff] [blame] | 881 | * pci_msix_vec_count - return the number of device's MSI-X table entries |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 882 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
Alexander Gordeev | ff1aa43 | 2013-12-30 08:28:15 +0100 | [diff] [blame] | 883 | * This function returns the number of device's MSI-X table entries and |
| 884 | * therefore the number of MSI-X vectors device is capable of sending. |
| 885 | * It returns a negative errno if the device is not capable of sending MSI-X |
| 886 | * interrupts. |
| 887 | **/ |
| 888 | int pci_msix_vec_count(struct pci_dev *dev) |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 889 | { |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 890 | u16 control; |
| 891 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame] | 892 | if (!dev->msix_cap) |
Alexander Gordeev | ff1aa43 | 2013-12-30 08:28:15 +0100 | [diff] [blame] | 893 | return -EINVAL; |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 894 | |
Bjorn Helgaas | f84ecd28 | 2013-04-17 17:38:32 -0600 | [diff] [blame] | 895 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
Bjorn Helgaas | 527eee2 | 2013-04-17 17:44:48 -0600 | [diff] [blame] | 896 | return msix_table_size(control); |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 897 | } |
Alexander Gordeev | ff1aa43 | 2013-12-30 08:28:15 +0100 | [diff] [blame] | 898 | EXPORT_SYMBOL(pci_msix_vec_count); |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 899 | |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 900 | static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 901 | int nvec, struct irq_affinity *affd, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | { |
Bjorn Helgaas | 5ec0940 | 2014-09-23 14:38:28 -0600 | [diff] [blame] | 903 | int nr_entries; |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 904 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Bjorn Helgaas | 901c4dd | 2019-10-14 16:17:05 -0500 | [diff] [blame] | 906 | if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0) |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 907 | return -EINVAL; |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 908 | |
Alexander Gordeev | ff1aa43 | 2013-12-30 08:28:15 +0100 | [diff] [blame] | 909 | nr_entries = pci_msix_vec_count(dev); |
| 910 | if (nr_entries < 0) |
| 911 | return nr_entries; |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 912 | if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL)) |
Michael S. Tsirkin | 57fbf52 | 2009-05-07 11:28:41 +0300 | [diff] [blame] | 913 | return nr_entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Christoph Hellwig | 3ac020e | 2016-07-12 18:20:16 +0900 | [diff] [blame] | 915 | if (entries) { |
| 916 | /* Check for any invalid entries */ |
| 917 | for (i = 0; i < nvec; i++) { |
| 918 | if (entries[i].entry >= nr_entries) |
| 919 | return -EINVAL; /* invalid entry */ |
| 920 | for (j = i + 1; j < nvec; j++) { |
| 921 | if (entries[i].entry == entries[j].entry) |
| 922 | return -EINVAL; /* duplicate entry */ |
| 923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 926 | |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 927 | /* Check whether driver already requested for MSI IRQ */ |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 928 | if (dev->msi_enabled) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 929 | pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | return -EINVAL; |
| 931 | } |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 932 | return msix_capability_init(dev, entries, nvec, affd); |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 933 | } |
| 934 | |
Bjorn Helgaas | 688769f | 2017-03-09 15:45:14 -0600 | [diff] [blame] | 935 | static void pci_msix_shutdown(struct pci_dev *dev) |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 936 | { |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 937 | struct msi_desc *entry; |
| 938 | |
Michael Ellerman | 128bc5f | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 939 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 940 | return; |
| 941 | |
Keith Busch | 0170591 | 2017-03-29 22:49:11 -0500 | [diff] [blame] | 942 | if (pci_dev_is_disconnected(dev)) { |
| 943 | dev->msix_enabled = 0; |
| 944 | return; |
| 945 | } |
| 946 | |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 947 | /* Return the device with MSI-X masked as initial states */ |
Thomas Gleixner | 689e6b5 | 2021-07-29 23:51:45 +0200 | [diff] [blame] | 948 | for_each_pci_msi_entry(entry, dev) |
Thomas Gleixner | 446a98b | 2021-07-29 23:51:58 +0200 | [diff] [blame] | 949 | pci_msix_mask(entry); |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 950 | |
Michael S. Tsirkin | 61b64ab | 2015-05-07 09:52:21 -0500 | [diff] [blame] | 951 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 952 | pci_intx_for_msi(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 953 | dev->msix_enabled = 0; |
Jiang Liu | 5f22699 | 2015-07-30 14:00:08 -0500 | [diff] [blame] | 954 | pcibios_alloc_irq(dev); |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 955 | } |
Hidetoshi Seto | c901851 | 2009-08-06 11:31:27 +0900 | [diff] [blame] | 956 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 957 | void pci_disable_msix(struct pci_dev *dev) |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 958 | { |
| 959 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
| 960 | return; |
| 961 | |
| 962 | pci_msix_shutdown(dev); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 963 | free_msi_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 965 | EXPORT_SYMBOL(pci_disable_msix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 967 | void pci_no_msi(void) |
| 968 | { |
| 969 | pci_msi_enable = 0; |
| 970 | } |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 971 | |
Andrew Patterson | 07ae95f | 2008-11-10 15:31:05 -0700 | [diff] [blame] | 972 | /** |
| 973 | * pci_msi_enabled - is MSI enabled? |
| 974 | * |
| 975 | * Returns true if MSI has not been disabled by the command-line option |
| 976 | * pci=nomsi. |
| 977 | **/ |
| 978 | int pci_msi_enabled(void) |
| 979 | { |
| 980 | return pci_msi_enable; |
| 981 | } |
| 982 | EXPORT_SYMBOL(pci_msi_enabled); |
| 983 | |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 984 | static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec, |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 985 | struct irq_affinity *affd) |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 986 | { |
Alexander Gordeev | 034cd97 | 2014-04-14 15:28:35 +0200 | [diff] [blame] | 987 | int nvec; |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 988 | int rc; |
| 989 | |
Bjorn Helgaas | 901c4dd | 2019-10-14 16:17:05 -0500 | [diff] [blame] | 990 | if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0) |
Alexander Gordeev | a06cd74 | 2014-09-23 12:45:58 -0600 | [diff] [blame] | 991 | return -EINVAL; |
Alexander Gordeev | 034cd97 | 2014-04-14 15:28:35 +0200 | [diff] [blame] | 992 | |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 993 | /* Check whether driver already requested MSI-X IRQs */ |
Alexander Gordeev | 034cd97 | 2014-04-14 15:28:35 +0200 | [diff] [blame] | 994 | if (dev->msix_enabled) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 995 | pci_info(dev, "can't enable MSI (MSI-X already enabled)\n"); |
Alexander Gordeev | 034cd97 | 2014-04-14 15:28:35 +0200 | [diff] [blame] | 996 | return -EINVAL; |
| 997 | } |
| 998 | |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 999 | if (maxvec < minvec) |
| 1000 | return -ERANGE; |
| 1001 | |
Tonghao Zhang | 4c1ef72 | 2018-09-24 07:00:41 -0700 | [diff] [blame] | 1002 | if (WARN_ON_ONCE(dev->msi_enabled)) |
| 1003 | return -EINVAL; |
| 1004 | |
Alexander Gordeev | 034cd97 | 2014-04-14 15:28:35 +0200 | [diff] [blame] | 1005 | nvec = pci_msi_vec_count(dev); |
| 1006 | if (nvec < 0) |
| 1007 | return nvec; |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1008 | if (nvec < minvec) |
Dennis Chen | 948b762 | 2016-12-01 10:15:04 +0800 | [diff] [blame] | 1009 | return -ENOSPC; |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1010 | |
| 1011 | if (nvec > maxvec) |
Alexander Gordeev | 034cd97 | 2014-04-14 15:28:35 +0200 | [diff] [blame] | 1012 | nvec = maxvec; |
| 1013 | |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1014 | for (;;) { |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 1015 | if (affd) { |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 1016 | nvec = irq_calc_affinity_vectors(minvec, nvec, affd); |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1017 | if (nvec < minvec) |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1018 | return -ENOSPC; |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1019 | } |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1020 | |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 1021 | rc = msi_capability_init(dev, nvec, affd); |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1022 | if (rc == 0) |
| 1023 | return nvec; |
| 1024 | |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1025 | if (rc < 0) |
| 1026 | return rc; |
| 1027 | if (rc < minvec) |
| 1028 | return -ENOSPC; |
| 1029 | |
| 1030 | nvec = rc; |
| 1031 | } |
| 1032 | } |
| 1033 | |
Christoph Hellwig | 4fe0395 | 2017-01-09 21:37:40 +0100 | [diff] [blame] | 1034 | /* deprecated, don't use */ |
| 1035 | int pci_enable_msi(struct pci_dev *dev) |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1036 | { |
Christoph Hellwig | 4fe0395 | 2017-01-09 21:37:40 +0100 | [diff] [blame] | 1037 | int rc = __pci_enable_msi_range(dev, 1, 1, NULL); |
| 1038 | if (rc < 0) |
| 1039 | return rc; |
| 1040 | return 0; |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1041 | } |
Christoph Hellwig | 4fe0395 | 2017-01-09 21:37:40 +0100 | [diff] [blame] | 1042 | EXPORT_SYMBOL(pci_enable_msi); |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1043 | |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1044 | static int __pci_enable_msix_range(struct pci_dev *dev, |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 1045 | struct msix_entry *entries, int minvec, |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 1046 | int maxvec, struct irq_affinity *affd, |
| 1047 | int flags) |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1048 | { |
Thomas Gleixner | e75eafb | 2016-09-14 16:18:49 +0200 | [diff] [blame] | 1049 | int rc, nvec = maxvec; |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1050 | |
| 1051 | if (maxvec < minvec) |
| 1052 | return -ERANGE; |
| 1053 | |
Tonghao Zhang | 4c1ef72 | 2018-09-24 07:00:41 -0700 | [diff] [blame] | 1054 | if (WARN_ON_ONCE(dev->msix_enabled)) |
| 1055 | return -EINVAL; |
| 1056 | |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1057 | for (;;) { |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 1058 | if (affd) { |
Michael Hernandez | 6f9a22b | 2017-05-18 10:47:47 -0700 | [diff] [blame] | 1059 | nvec = irq_calc_affinity_vectors(minvec, nvec, affd); |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1060 | if (nvec < minvec) |
| 1061 | return -ENOSPC; |
| 1062 | } |
| 1063 | |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 1064 | rc = __pci_enable_msix(dev, entries, nvec, affd, flags); |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1065 | if (rc == 0) |
| 1066 | return nvec; |
| 1067 | |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1068 | if (rc < 0) |
| 1069 | return rc; |
| 1070 | if (rc < minvec) |
| 1071 | return -ENOSPC; |
| 1072 | |
| 1073 | nvec = rc; |
| 1074 | } |
| 1075 | } |
| 1076 | |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1077 | /** |
| 1078 | * pci_enable_msix_range - configure device's MSI-X capability structure |
| 1079 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
| 1080 | * @entries: pointer to an array of MSI-X entries |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 1081 | * @minvec: minimum number of MSI-X IRQs requested |
| 1082 | * @maxvec: maximum number of MSI-X IRQs requested |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1083 | * |
| 1084 | * Setup the MSI-X capability structure of device function with a maximum |
| 1085 | * possible number of interrupts in the range between @minvec and @maxvec |
| 1086 | * upon its software driver call to request for MSI-X mode enabled on its |
| 1087 | * hardware device function. It returns a negative errno if an error occurs. |
| 1088 | * If it succeeds, it returns the actual number of interrupts allocated and |
| 1089 | * indicates the successful configuration of MSI-X capability structure |
| 1090 | * with new allocated MSI-X interrupts. |
| 1091 | **/ |
| 1092 | int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, |
Christoph Hellwig | 4ef3368 | 2016-07-12 18:20:18 +0900 | [diff] [blame] | 1093 | int minvec, int maxvec) |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1094 | { |
Logan Gunthorpe | d7cc609 | 2019-05-23 16:30:51 -0600 | [diff] [blame] | 1095 | return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0); |
Alexander Gordeev | 302a252 | 2013-12-30 08:28:16 +0100 | [diff] [blame] | 1096 | } |
| 1097 | EXPORT_SYMBOL(pci_enable_msix_range); |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1098 | |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1099 | /** |
Christoph Hellwig | 402723a | 2016-11-08 17:15:05 -0800 | [diff] [blame] | 1100 | * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1101 | * @dev: PCI device to operate on |
| 1102 | * @min_vecs: minimum number of vectors required (must be >= 1) |
| 1103 | * @max_vecs: maximum (desired) number of vectors |
| 1104 | * @flags: flags or quirks for the allocation |
Christoph Hellwig | 402723a | 2016-11-08 17:15:05 -0800 | [diff] [blame] | 1105 | * @affd: optional description of the affinity requirements |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1106 | * |
| 1107 | * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI |
| 1108 | * vectors if available, and fall back to a single legacy vector |
| 1109 | * if neither is available. Return the number of vectors allocated, |
| 1110 | * (which might be smaller than @max_vecs) if successful, or a negative |
| 1111 | * error code on error. If less than @min_vecs interrupt vectors are |
| 1112 | * available for @dev the function will fail with -ENOSPC. |
| 1113 | * |
| 1114 | * To get the Linux IRQ number used for a vector that can be passed to |
| 1115 | * request_irq() use the pci_irq_vector() helper. |
| 1116 | */ |
Christoph Hellwig | 402723a | 2016-11-08 17:15:05 -0800 | [diff] [blame] | 1117 | int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, |
| 1118 | unsigned int max_vecs, unsigned int flags, |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 1119 | struct irq_affinity *affd) |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1120 | { |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 1121 | struct irq_affinity msi_default_affd = {0}; |
Piotr Stankiewicz | 30ff3e8 | 2020-06-16 09:33:16 +0200 | [diff] [blame] | 1122 | int nvecs = -ENOSPC; |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1123 | |
Christoph Hellwig | 402723a | 2016-11-08 17:15:05 -0800 | [diff] [blame] | 1124 | if (flags & PCI_IRQ_AFFINITY) { |
| 1125 | if (!affd) |
| 1126 | affd = &msi_default_affd; |
| 1127 | } else { |
| 1128 | if (WARN_ON(affd)) |
| 1129 | affd = NULL; |
| 1130 | } |
Christoph Hellwig | 61e1c59 | 2016-11-08 17:15:04 -0800 | [diff] [blame] | 1131 | |
Christoph Hellwig | 4fe0d15 | 2016-08-11 07:11:04 -0700 | [diff] [blame] | 1132 | if (flags & PCI_IRQ_MSIX) { |
Piotr Stankiewicz | 30ff3e8 | 2020-06-16 09:33:16 +0200 | [diff] [blame] | 1133 | nvecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs, |
| 1134 | affd, flags); |
| 1135 | if (nvecs > 0) |
| 1136 | return nvecs; |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1137 | } |
| 1138 | |
Christoph Hellwig | 4fe0d15 | 2016-08-11 07:11:04 -0700 | [diff] [blame] | 1139 | if (flags & PCI_IRQ_MSI) { |
Piotr Stankiewicz | 30ff3e8 | 2020-06-16 09:33:16 +0200 | [diff] [blame] | 1140 | nvecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd); |
| 1141 | if (nvecs > 0) |
| 1142 | return nvecs; |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1143 | } |
| 1144 | |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 1145 | /* use legacy IRQ if allowed */ |
Christoph Hellwig | 862290f | 2017-02-01 14:41:42 +0100 | [diff] [blame] | 1146 | if (flags & PCI_IRQ_LEGACY) { |
| 1147 | if (min_vecs == 1 && dev->irq) { |
Ming Lei | c66d4bd | 2019-02-16 18:13:09 +0100 | [diff] [blame] | 1148 | /* |
| 1149 | * Invoke the affinity spreading logic to ensure that |
| 1150 | * the device driver can adjust queue configuration |
| 1151 | * for the single interrupt case. |
| 1152 | */ |
| 1153 | if (affd) |
| 1154 | irq_create_affinity_masks(1, affd); |
Christoph Hellwig | 862290f | 2017-02-01 14:41:42 +0100 | [diff] [blame] | 1155 | pci_intx(dev, 1); |
| 1156 | return 1; |
| 1157 | } |
Christoph Hellwig | 5d0bdf2 | 2016-08-11 07:11:05 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Piotr Stankiewicz | 30ff3e8 | 2020-06-16 09:33:16 +0200 | [diff] [blame] | 1160 | return nvecs; |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1161 | } |
Christoph Hellwig | 402723a | 2016-11-08 17:15:05 -0800 | [diff] [blame] | 1162 | EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity); |
Christoph Hellwig | aff1716 | 2016-07-12 18:20:17 +0900 | [diff] [blame] | 1163 | |
| 1164 | /** |
| 1165 | * pci_free_irq_vectors - free previously allocated IRQs for a device |
| 1166 | * @dev: PCI device to operate on |
| 1167 | * |
| 1168 | * Undoes the allocations and enabling in pci_alloc_irq_vectors(). |
| 1169 | */ |
| 1170 | void pci_free_irq_vectors(struct pci_dev *dev) |
| 1171 | { |
| 1172 | pci_disable_msix(dev); |
| 1173 | pci_disable_msi(dev); |
| 1174 | } |
| 1175 | EXPORT_SYMBOL(pci_free_irq_vectors); |
| 1176 | |
| 1177 | /** |
| 1178 | * pci_irq_vector - return Linux IRQ number of a device vector |
| 1179 | * @dev: PCI device to operate on |
| 1180 | * @nr: device-relative interrupt vector index (0-based). |
| 1181 | */ |
| 1182 | int pci_irq_vector(struct pci_dev *dev, unsigned int nr) |
| 1183 | { |
| 1184 | if (dev->msix_enabled) { |
| 1185 | struct msi_desc *entry; |
| 1186 | int i = 0; |
| 1187 | |
| 1188 | for_each_pci_msi_entry(entry, dev) { |
| 1189 | if (i == nr) |
| 1190 | return entry->irq; |
| 1191 | i++; |
| 1192 | } |
| 1193 | WARN_ON_ONCE(1); |
| 1194 | return -EINVAL; |
| 1195 | } |
| 1196 | |
| 1197 | if (dev->msi_enabled) { |
| 1198 | struct msi_desc *entry = first_pci_msi_entry(dev); |
| 1199 | |
| 1200 | if (WARN_ON_ONCE(nr >= entry->nvec_used)) |
| 1201 | return -EINVAL; |
| 1202 | } else { |
| 1203 | if (WARN_ON_ONCE(nr > 0)) |
| 1204 | return -EINVAL; |
| 1205 | } |
| 1206 | |
| 1207 | return dev->irq + nr; |
| 1208 | } |
| 1209 | EXPORT_SYMBOL(pci_irq_vector); |
| 1210 | |
Thomas Gleixner | ee8d41e | 2016-09-14 16:18:51 +0200 | [diff] [blame] | 1211 | /** |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 1212 | * pci_irq_get_affinity - return the affinity of a particular MSI vector |
Thomas Gleixner | ee8d41e | 2016-09-14 16:18:51 +0200 | [diff] [blame] | 1213 | * @dev: PCI device to operate on |
| 1214 | * @nr: device-relative interrupt vector index (0-based). |
| 1215 | */ |
| 1216 | const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr) |
| 1217 | { |
| 1218 | if (dev->msix_enabled) { |
| 1219 | struct msi_desc *entry; |
| 1220 | int i = 0; |
| 1221 | |
| 1222 | for_each_pci_msi_entry(entry, dev) { |
| 1223 | if (i == nr) |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 1224 | return &entry->affinity->mask; |
Thomas Gleixner | ee8d41e | 2016-09-14 16:18:51 +0200 | [diff] [blame] | 1225 | i++; |
| 1226 | } |
| 1227 | WARN_ON_ONCE(1); |
| 1228 | return NULL; |
| 1229 | } else if (dev->msi_enabled) { |
| 1230 | struct msi_desc *entry = first_pci_msi_entry(dev); |
| 1231 | |
Jan Beulich | d1d111e | 2016-11-08 00:43:54 -0700 | [diff] [blame] | 1232 | if (WARN_ON_ONCE(!entry || !entry->affinity || |
| 1233 | nr >= entry->nvec_used)) |
Thomas Gleixner | ee8d41e | 2016-09-14 16:18:51 +0200 | [diff] [blame] | 1234 | return NULL; |
| 1235 | |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 1236 | return &entry->affinity[nr].mask; |
Thomas Gleixner | ee8d41e | 2016-09-14 16:18:51 +0200 | [diff] [blame] | 1237 | } else { |
| 1238 | return cpu_possible_mask; |
| 1239 | } |
| 1240 | } |
| 1241 | EXPORT_SYMBOL(pci_irq_get_affinity); |
| 1242 | |
Jiang Liu | 25a98bd | 2015-07-09 16:00:45 +0800 | [diff] [blame] | 1243 | struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc) |
| 1244 | { |
| 1245 | return to_pci_dev(desc->dev); |
| 1246 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 1247 | EXPORT_SYMBOL(msi_desc_to_pci_dev); |
Jiang Liu | 25a98bd | 2015-07-09 16:00:45 +0800 | [diff] [blame] | 1248 | |
Jiang Liu | c179c9b | 2015-07-09 16:00:36 +0800 | [diff] [blame] | 1249 | void *msi_desc_to_pci_sysdata(struct msi_desc *desc) |
| 1250 | { |
| 1251 | struct pci_dev *dev = msi_desc_to_pci_dev(desc); |
| 1252 | |
| 1253 | return dev->bus->sysdata; |
| 1254 | } |
| 1255 | EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata); |
| 1256 | |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1257 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
| 1258 | /** |
| 1259 | * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space |
| 1260 | * @irq_data: Pointer to interrupt data of the MSI interrupt |
| 1261 | * @msg: Pointer to the message |
| 1262 | */ |
| 1263 | void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg) |
| 1264 | { |
Jiang Liu | 507a883 | 2015-06-01 16:05:42 +0800 | [diff] [blame] | 1265 | struct msi_desc *desc = irq_data_get_msi_desc(irq_data); |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1266 | |
| 1267 | /* |
| 1268 | * For MSI-X desc->irq is always equal to irq_data->irq. For |
| 1269 | * MSI only the first interrupt of MULTI MSI passes the test. |
| 1270 | */ |
| 1271 | if (desc->irq == irq_data->irq) |
| 1272 | __pci_write_msi_msg(desc, msg); |
| 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 1277 | * @desc: Pointer to the MSI descriptor |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1278 | * |
| 1279 | * The ID number is only used within the irqdomain. |
| 1280 | */ |
Thomas Gleixner | 9006c13 | 2020-08-26 13:16:47 +0200 | [diff] [blame] | 1281 | static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc) |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1282 | { |
Thomas Gleixner | dfb9eb7 | 2020-08-26 13:16:45 +0200 | [diff] [blame] | 1283 | struct pci_dev *dev = msi_desc_to_pci_dev(desc); |
| 1284 | |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1285 | return (irq_hw_number_t)desc->msi_attrib.entry_nr | |
Heiner Kallweit | 4e544ba | 2019-04-24 21:11:58 +0200 | [diff] [blame] | 1286 | pci_dev_id(dev) << 11 | |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1287 | (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27; |
| 1288 | } |
| 1289 | |
| 1290 | static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc) |
| 1291 | { |
| 1292 | return !desc->msi_attrib.is_msix && desc->nvec_used > 1; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 1296 | * pci_msi_domain_check_cap - Verify that @domain supports the capabilities |
| 1297 | * for @dev |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1298 | * @domain: The interrupt domain to check |
| 1299 | * @info: The domain info for verification |
| 1300 | * @dev: The device to check |
| 1301 | * |
| 1302 | * Returns: |
| 1303 | * 0 if the functionality is supported |
| 1304 | * 1 if Multi MSI is requested, but the domain does not support it |
| 1305 | * -ENOTSUPP otherwise |
| 1306 | */ |
| 1307 | int pci_msi_domain_check_cap(struct irq_domain *domain, |
| 1308 | struct msi_domain_info *info, struct device *dev) |
| 1309 | { |
| 1310 | struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev)); |
| 1311 | |
Christoph Hellwig | 4fe0395 | 2017-01-09 21:37:40 +0100 | [diff] [blame] | 1312 | /* Special handling to support __pci_enable_msi_range() */ |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1313 | if (pci_msi_desc_is_multi_msi(desc) && |
| 1314 | !(info->flags & MSI_FLAG_MULTI_PCI_MSI)) |
| 1315 | return 1; |
| 1316 | else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX)) |
| 1317 | return -ENOTSUPP; |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | static int pci_msi_domain_handle_error(struct irq_domain *domain, |
| 1323 | struct msi_desc *desc, int error) |
| 1324 | { |
Christoph Hellwig | 4fe0395 | 2017-01-09 21:37:40 +0100 | [diff] [blame] | 1325 | /* Special handling to support __pci_enable_msi_range() */ |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1326 | if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC) |
| 1327 | return 1; |
| 1328 | |
| 1329 | return error; |
| 1330 | } |
| 1331 | |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1332 | static void pci_msi_domain_set_desc(msi_alloc_info_t *arg, |
| 1333 | struct msi_desc *desc) |
| 1334 | { |
| 1335 | arg->desc = desc; |
Thomas Gleixner | dfb9eb7 | 2020-08-26 13:16:45 +0200 | [diff] [blame] | 1336 | arg->hwirq = pci_msi_domain_calc_hwirq(desc); |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1337 | } |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1338 | |
| 1339 | static struct msi_domain_ops pci_msi_domain_ops_default = { |
| 1340 | .set_desc = pci_msi_domain_set_desc, |
| 1341 | .msi_check = pci_msi_domain_check_cap, |
| 1342 | .handle_error = pci_msi_domain_handle_error, |
| 1343 | }; |
| 1344 | |
| 1345 | static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info) |
| 1346 | { |
| 1347 | struct msi_domain_ops *ops = info->ops; |
| 1348 | |
| 1349 | if (ops == NULL) { |
| 1350 | info->ops = &pci_msi_domain_ops_default; |
| 1351 | } else { |
| 1352 | if (ops->set_desc == NULL) |
| 1353 | ops->set_desc = pci_msi_domain_set_desc; |
| 1354 | if (ops->msi_check == NULL) |
| 1355 | ops->msi_check = pci_msi_domain_check_cap; |
| 1356 | if (ops->handle_error == NULL) |
| 1357 | ops->handle_error = pci_msi_domain_handle_error; |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info) |
| 1362 | { |
| 1363 | struct irq_chip *chip = info->chip; |
| 1364 | |
| 1365 | BUG_ON(!chip); |
| 1366 | if (!chip->irq_write_msi_msg) |
| 1367 | chip->irq_write_msi_msg = pci_msi_domain_write_msg; |
Marc Zyngier | 0701c53 | 2015-10-13 19:14:45 +0100 | [diff] [blame] | 1368 | if (!chip->irq_mask) |
| 1369 | chip->irq_mask = pci_msi_mask_irq; |
| 1370 | if (!chip->irq_unmask) |
| 1371 | chip->irq_unmask = pci_msi_unmask_irq; |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
Marc Zyngier | be5436c | 2015-10-13 12:51:44 +0100 | [diff] [blame] | 1375 | * pci_msi_create_irq_domain - Create a MSI interrupt domain |
| 1376 | * @fwnode: Optional fwnode of the interrupt controller |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1377 | * @info: MSI domain info |
| 1378 | * @parent: Parent irq domain |
| 1379 | * |
| 1380 | * Updates the domain and chip ops and creates a MSI interrupt domain. |
| 1381 | * |
| 1382 | * Returns: |
| 1383 | * A domain pointer or NULL in case of failure. |
| 1384 | */ |
Marc Zyngier | be5436c | 2015-10-13 12:51:44 +0100 | [diff] [blame] | 1385 | struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1386 | struct msi_domain_info *info, |
| 1387 | struct irq_domain *parent) |
| 1388 | { |
Marc Zyngier | 0380839 | 2015-07-28 14:46:09 +0100 | [diff] [blame] | 1389 | struct irq_domain *domain; |
| 1390 | |
Marc Zyngier | 6988e0e | 2018-05-08 13:14:31 +0100 | [diff] [blame] | 1391 | if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE)) |
| 1392 | info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; |
| 1393 | |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1394 | if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS) |
| 1395 | pci_msi_domain_update_dom_ops(info); |
| 1396 | if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) |
| 1397 | pci_msi_domain_update_chip_ops(info); |
| 1398 | |
Marc Zyngier | f3b0946 | 2016-07-13 17:18:33 +0100 | [diff] [blame] | 1399 | info->flags |= MSI_FLAG_ACTIVATE_EARLY; |
Thomas Gleixner | 25e960e | 2017-10-17 09:54:58 +0200 | [diff] [blame] | 1400 | if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE)) |
| 1401 | info->flags |= MSI_FLAG_MUST_REACTIVATE; |
Marc Zyngier | f3b0946 | 2016-07-13 17:18:33 +0100 | [diff] [blame] | 1402 | |
Heiner Kallweit | 923aa4c | 2018-08-05 22:31:03 +0200 | [diff] [blame] | 1403 | /* PCI-MSI is oneshot-safe */ |
| 1404 | info->chip->flags |= IRQCHIP_ONESHOT_SAFE; |
| 1405 | |
Marc Zyngier | be5436c | 2015-10-13 12:51:44 +0100 | [diff] [blame] | 1406 | domain = msi_create_irq_domain(fwnode, info, parent); |
Marc Zyngier | 0380839 | 2015-07-28 14:46:09 +0100 | [diff] [blame] | 1407 | if (!domain) |
| 1408 | return NULL; |
| 1409 | |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 1410 | irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI); |
Marc Zyngier | 0380839 | 2015-07-28 14:46:09 +0100 | [diff] [blame] | 1411 | return domain; |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1412 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 1413 | EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain); |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1414 | |
Robin Murphy | 235b2c7 | 2017-08-01 18:59:08 +0100 | [diff] [blame] | 1415 | /* |
| 1416 | * Users of the generic MSI infrastructure expect a device to have a single ID, |
| 1417 | * so with DMA aliases we have to pick the least-worst compromise. Devices with |
| 1418 | * DMA phantom functions tend to still emit MSIs from the real function number, |
| 1419 | * so we ignore those and only consider topological aliases where either the |
| 1420 | * alias device or RID appears on a different bus number. We also make the |
| 1421 | * reasonable assumption that bridges are walked in an upstream direction (so |
| 1422 | * the last one seen wins), and the much braver assumption that the most likely |
| 1423 | * case is that of PCI->PCIe so we should always use the alias RID. This echoes |
| 1424 | * the logic from intel_irq_remapping's set_msi_sid(), which presumably works |
| 1425 | * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions |
| 1426 | * for taking ownership all we can really do is close our eyes and hope... |
| 1427 | */ |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 1428 | static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data) |
| 1429 | { |
| 1430 | u32 *pa = data; |
Robin Murphy | 235b2c7 | 2017-08-01 18:59:08 +0100 | [diff] [blame] | 1431 | u8 bus = PCI_BUS_NUM(*pa); |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 1432 | |
Robin Murphy | 235b2c7 | 2017-08-01 18:59:08 +0100 | [diff] [blame] | 1433 | if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus) |
| 1434 | *pa = alias; |
| 1435 | |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 1436 | return 0; |
| 1437 | } |
Robin Murphy | 235b2c7 | 2017-08-01 18:59:08 +0100 | [diff] [blame] | 1438 | |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 1439 | /** |
| 1440 | * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID) |
| 1441 | * @domain: The interrupt domain |
| 1442 | * @pdev: The PCI device. |
| 1443 | * |
| 1444 | * The RID for a device is formed from the alias, with a firmware |
| 1445 | * supplied mapping applied |
| 1446 | * |
| 1447 | * Returns: The RID. |
| 1448 | */ |
| 1449 | u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev) |
| 1450 | { |
| 1451 | struct device_node *of_node; |
Heiner Kallweit | 4e544ba | 2019-04-24 21:11:58 +0200 | [diff] [blame] | 1452 | u32 rid = pci_dev_id(pdev); |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 1453 | |
| 1454 | pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); |
| 1455 | |
| 1456 | of_node = irq_domain_get_of_node(domain); |
Lorenzo Pieralisi | 2bcdd8f | 2020-06-19 09:20:11 +0100 | [diff] [blame] | 1457 | rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) : |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame] | 1458 | iort_msi_map_id(&pdev->dev, rid); |
David Daney | b6eec9b | 2015-10-08 15:10:49 -0700 | [diff] [blame] | 1459 | |
| 1460 | return rid; |
| 1461 | } |
Marc Zyngier | 54fa97e | 2015-10-02 14:43:06 +0100 | [diff] [blame] | 1462 | |
| 1463 | /** |
| 1464 | * pci_msi_get_device_domain - Get the MSI domain for a given PCI device |
| 1465 | * @pdev: The PCI device |
| 1466 | * |
| 1467 | * Use the firmware data to find a device-specific MSI domain |
Robin Murphy | 235b2c7 | 2017-08-01 18:59:08 +0100 | [diff] [blame] | 1468 | * (i.e. not one that is set as a default). |
Marc Zyngier | 54fa97e | 2015-10-02 14:43:06 +0100 | [diff] [blame] | 1469 | * |
Robin Murphy | 235b2c7 | 2017-08-01 18:59:08 +0100 | [diff] [blame] | 1470 | * Returns: The corresponding MSI domain or NULL if none has been found. |
Marc Zyngier | 54fa97e | 2015-10-02 14:43:06 +0100 | [diff] [blame] | 1471 | */ |
| 1472 | struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev) |
| 1473 | { |
Tomasz Nowicki | be2021b | 2016-09-12 20:32:22 +0200 | [diff] [blame] | 1474 | struct irq_domain *dom; |
Heiner Kallweit | 4e544ba | 2019-04-24 21:11:58 +0200 | [diff] [blame] | 1475 | u32 rid = pci_dev_id(pdev); |
Marc Zyngier | 54fa97e | 2015-10-02 14:43:06 +0100 | [diff] [blame] | 1476 | |
| 1477 | pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); |
Diana Craciun | 6f881ab | 2020-06-19 09:20:10 +0100 | [diff] [blame] | 1478 | dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI); |
Tomasz Nowicki | be2021b | 2016-09-12 20:32:22 +0200 | [diff] [blame] | 1479 | if (!dom) |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 1480 | dom = iort_get_device_domain(&pdev->dev, rid, |
| 1481 | DOMAIN_BUS_PCI_MSI); |
Tomasz Nowicki | be2021b | 2016-09-12 20:32:22 +0200 | [diff] [blame] | 1482 | return dom; |
Marc Zyngier | 54fa97e | 2015-10-02 14:43:06 +0100 | [diff] [blame] | 1483 | } |
Thomas Gleixner | 2fd6026 | 2020-08-26 13:16:53 +0200 | [diff] [blame] | 1484 | |
| 1485 | /** |
| 1486 | * pci_dev_has_special_msi_domain - Check whether the device is handled by |
| 1487 | * a non-standard PCI-MSI domain |
| 1488 | * @pdev: The PCI device to check. |
| 1489 | * |
| 1490 | * Returns: True if the device irqdomain or the bus irqdomain is |
| 1491 | * non-standard PCI/MSI. |
| 1492 | */ |
| 1493 | bool pci_dev_has_special_msi_domain(struct pci_dev *pdev) |
| 1494 | { |
| 1495 | struct irq_domain *dom = dev_get_msi_domain(&pdev->dev); |
| 1496 | |
| 1497 | if (!dom) |
| 1498 | dom = dev_get_msi_domain(&pdev->bus->dev); |
| 1499 | |
| 1500 | if (!dom) |
| 1501 | return true; |
| 1502 | |
| 1503 | return dom->bus_token != DOMAIN_BUS_PCI_MSI; |
| 1504 | } |
| 1505 | |
Jiang Liu | 3878eae | 2014-11-11 21:02:18 +0800 | [diff] [blame] | 1506 | #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */ |
Bjorn Helgaas | cbc40d5 | 2020-12-03 12:51:08 -0600 | [diff] [blame] | 1507 | #endif /* CONFIG_PCI_MSI */ |
| 1508 | |
| 1509 | void pci_msi_init(struct pci_dev *dev) |
| 1510 | { |
| 1511 | u16 ctrl; |
| 1512 | |
| 1513 | /* |
| 1514 | * Disable the MSI hardware to avoid screaming interrupts |
| 1515 | * during boot. This is the power on reset default so |
| 1516 | * usually this should be a noop. |
| 1517 | */ |
| 1518 | dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 1519 | if (!dev->msi_cap) |
| 1520 | return; |
| 1521 | |
| 1522 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl); |
| 1523 | if (ctrl & PCI_MSI_FLAGS_ENABLE) |
| 1524 | pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, |
| 1525 | ctrl & ~PCI_MSI_FLAGS_ENABLE); |
Vidya Sagar | 2053230 | 2020-12-03 12:51:10 -0600 | [diff] [blame] | 1526 | |
| 1527 | if (!(ctrl & PCI_MSI_FLAGS_64BIT)) |
| 1528 | dev->no_64bit_msi = 1; |
Bjorn Helgaas | cbc40d5 | 2020-12-03 12:51:08 -0600 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | void pci_msix_init(struct pci_dev *dev) |
| 1532 | { |
| 1533 | u16 ctrl; |
| 1534 | |
| 1535 | dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 1536 | if (!dev->msix_cap) |
| 1537 | return; |
| 1538 | |
| 1539 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl); |
| 1540 | if (ctrl & PCI_MSIX_FLAGS_ENABLE) |
| 1541 | pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, |
| 1542 | ctrl & ~PCI_MSIX_FLAGS_ENABLE); |
| 1543 | } |