Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: msi.c |
| 3 | * Purpose: PCI Message Signaled Interrupt (MSI) |
| 4 | * |
| 5 | * Copyright (C) 2003-2004 Intel |
| 6 | * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com) |
| 7 | */ |
| 8 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 9 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/irq.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/pci.h> |
| 16 | #include <linux/proc_fs.h> |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 17 | #include <linux/msi.h> |
Dan Williams | 4fdadeb | 2007-04-26 18:21:38 -0700 | [diff] [blame] | 18 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/errno.h> |
| 21 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include "pci.h" |
| 24 | #include "msi.h" |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static int pci_msi_enable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 28 | /* Arch hooks */ |
| 29 | |
| 30 | int __attribute__ ((weak)) |
| 31 | arch_msi_check_device(struct pci_dev *dev, int nvec, int type) |
| 32 | { |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int __attribute__ ((weak)) |
| 37 | arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry) |
| 38 | { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int __attribute__ ((weak)) |
| 43 | arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 44 | { |
| 45 | struct msi_desc *entry; |
| 46 | int ret; |
| 47 | |
| 48 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 49 | ret = arch_setup_msi_irq(dev, entry); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq) |
| 58 | { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | void __attribute__ ((weak)) |
| 63 | arch_teardown_msi_irqs(struct pci_dev *dev) |
| 64 | { |
| 65 | struct msi_desc *entry; |
| 66 | |
| 67 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 68 | if (entry->irq != 0) |
| 69 | arch_teardown_msi_irq(entry->irq); |
| 70 | } |
| 71 | } |
| 72 | |
Hidetoshi Seto | 5ca5c02 | 2008-05-19 13:48:17 +0900 | [diff] [blame] | 73 | static void __msi_set_enable(struct pci_dev *dev, int pos, int enable) |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 74 | { |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 75 | u16 control; |
| 76 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 77 | if (pos) { |
| 78 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); |
| 79 | control &= ~PCI_MSI_FLAGS_ENABLE; |
| 80 | if (enable) |
| 81 | control |= PCI_MSI_FLAGS_ENABLE; |
| 82 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); |
| 83 | } |
| 84 | } |
| 85 | |
Hidetoshi Seto | 5ca5c02 | 2008-05-19 13:48:17 +0900 | [diff] [blame] | 86 | static void msi_set_enable(struct pci_dev *dev, int enable) |
| 87 | { |
| 88 | __msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable); |
| 89 | } |
| 90 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 91 | static void msix_set_enable(struct pci_dev *dev, int enable) |
| 92 | { |
| 93 | int pos; |
| 94 | u16 control; |
| 95 | |
| 96 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 97 | if (pos) { |
| 98 | pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control); |
| 99 | control &= ~PCI_MSIX_FLAGS_ENABLE; |
| 100 | if (enable) |
| 101 | control |= PCI_MSIX_FLAGS_ENABLE; |
| 102 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); |
| 103 | } |
| 104 | } |
| 105 | |
Matthew Wilcox | bffac3c | 2009-01-21 19:19:19 -0500 | [diff] [blame] | 106 | static inline __attribute_const__ u32 msi_mask(unsigned x) |
| 107 | { |
Matthew Wilcox | 0b49ec3 | 2009-02-08 20:27:47 -0700 | [diff] [blame] | 108 | /* Don't shift by >= width of type */ |
| 109 | if (x >= 5) |
| 110 | return 0xffffffff; |
| 111 | return (1 << (1 << x)) - 1; |
Matthew Wilcox | bffac3c | 2009-01-21 19:19:19 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 114 | static void msix_flush_writes(struct irq_desc *desc) |
Mitch Williams | 988cbb1 | 2007-03-30 11:54:08 -0700 | [diff] [blame] | 115 | { |
| 116 | struct msi_desc *entry; |
| 117 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 118 | entry = get_irq_desc_msi(desc); |
Mitch Williams | 988cbb1 | 2007-03-30 11:54:08 -0700 | [diff] [blame] | 119 | BUG_ON(!entry || !entry->dev); |
| 120 | switch (entry->msi_attrib.type) { |
| 121 | case PCI_CAP_ID_MSI: |
| 122 | /* nothing to do */ |
| 123 | break; |
| 124 | case PCI_CAP_ID_MSIX: |
| 125 | { |
| 126 | int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + |
| 127 | PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET; |
| 128 | readl(entry->mask_base + offset); |
| 129 | break; |
| 130 | } |
| 131 | default: |
| 132 | BUG(); |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 137 | /* |
| 138 | * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to |
| 139 | * mask all MSI interrupts by clearing the MSI enable bit does not work |
| 140 | * reliably as devices without an INTx disable bit will then generate a |
| 141 | * level IRQ which will never be cleared. |
| 142 | * |
| 143 | * Returns 1 if it succeeded in masking the interrupt and 0 if the device |
| 144 | * doesn't support MSI masking. |
| 145 | */ |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 146 | static int msi_set_mask_bits(struct irq_desc *desc, u32 mask, u32 flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | struct msi_desc *entry; |
| 149 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 150 | entry = get_irq_desc_msi(desc); |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 151 | BUG_ON(!entry || !entry->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | switch (entry->msi_attrib.type) { |
| 153 | case PCI_CAP_ID_MSI: |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 154 | if (entry->msi_attrib.maskbit) { |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame] | 155 | int pos; |
| 156 | u32 mask_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 158 | pos = (long)entry->mask_base; |
| 159 | pci_read_config_dword(entry->dev, pos, &mask_bits); |
Yinghai Lu | 8e149e0 | 2008-04-23 14:56:30 -0700 | [diff] [blame] | 160 | mask_bits &= ~(mask); |
| 161 | mask_bits |= flag & mask; |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 162 | pci_write_config_dword(entry->dev, pos, mask_bits); |
Eric W. Biederman | 58e0543 | 2007-03-05 00:30:11 -0800 | [diff] [blame] | 163 | } else { |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 164 | return 0; |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | case PCI_CAP_ID_MSIX: |
| 168 | { |
| 169 | int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + |
| 170 | PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET; |
| 171 | writel(flag, entry->mask_base + offset); |
Eric W. Biederman | 348e3fd | 2007-04-03 01:41:49 -0600 | [diff] [blame] | 172 | readl(entry->mask_base + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | default: |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 176 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | break; |
| 178 | } |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 179 | entry->msi_attrib.masked = !!flag; |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 180 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 183 | void read_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 184 | { |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 185 | struct msi_desc *entry = get_irq_desc_msi(desc); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 186 | switch(entry->msi_attrib.type) { |
| 187 | case PCI_CAP_ID_MSI: |
| 188 | { |
| 189 | struct pci_dev *dev = entry->dev; |
| 190 | int pos = entry->msi_attrib.pos; |
| 191 | u16 data; |
| 192 | |
| 193 | pci_read_config_dword(dev, msi_lower_address_reg(pos), |
| 194 | &msg->address_lo); |
| 195 | if (entry->msi_attrib.is_64) { |
| 196 | pci_read_config_dword(dev, msi_upper_address_reg(pos), |
| 197 | &msg->address_hi); |
| 198 | pci_read_config_word(dev, msi_data_reg(pos, 1), &data); |
| 199 | } else { |
| 200 | msg->address_hi = 0; |
Roland Dreier | cbf5d9e | 2007-10-03 11:15:11 -0700 | [diff] [blame] | 201 | pci_read_config_word(dev, msi_data_reg(pos, 0), &data); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 202 | } |
| 203 | msg->data = data; |
| 204 | break; |
| 205 | } |
| 206 | case PCI_CAP_ID_MSIX: |
| 207 | { |
| 208 | void __iomem *base; |
| 209 | base = entry->mask_base + |
| 210 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 211 | |
| 212 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); |
| 213 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); |
| 214 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET); |
| 215 | break; |
| 216 | } |
| 217 | default: |
| 218 | BUG(); |
| 219 | } |
| 220 | } |
| 221 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 222 | void read_msi_msg(unsigned int irq, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 223 | { |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 224 | struct irq_desc *desc = irq_to_desc(irq); |
| 225 | |
| 226 | read_msi_msg_desc(desc, msg); |
| 227 | } |
| 228 | |
| 229 | void write_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg) |
| 230 | { |
| 231 | struct msi_desc *entry = get_irq_desc_msi(desc); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 232 | switch (entry->msi_attrib.type) { |
| 233 | case PCI_CAP_ID_MSI: |
| 234 | { |
| 235 | struct pci_dev *dev = entry->dev; |
| 236 | int pos = entry->msi_attrib.pos; |
| 237 | |
| 238 | pci_write_config_dword(dev, msi_lower_address_reg(pos), |
| 239 | msg->address_lo); |
| 240 | if (entry->msi_attrib.is_64) { |
| 241 | pci_write_config_dword(dev, msi_upper_address_reg(pos), |
| 242 | msg->address_hi); |
| 243 | pci_write_config_word(dev, msi_data_reg(pos, 1), |
| 244 | msg->data); |
| 245 | } else { |
| 246 | pci_write_config_word(dev, msi_data_reg(pos, 0), |
| 247 | msg->data); |
| 248 | } |
| 249 | break; |
| 250 | } |
| 251 | case PCI_CAP_ID_MSIX: |
| 252 | { |
| 253 | void __iomem *base; |
| 254 | base = entry->mask_base + |
| 255 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 256 | |
| 257 | writel(msg->address_lo, |
| 258 | base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); |
| 259 | writel(msg->address_hi, |
| 260 | base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); |
| 261 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET); |
| 262 | break; |
| 263 | } |
| 264 | default: |
| 265 | BUG(); |
| 266 | } |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 267 | entry->msg = *msg; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 270 | void write_msi_msg(unsigned int irq, struct msi_msg *msg) |
| 271 | { |
| 272 | struct irq_desc *desc = irq_to_desc(irq); |
| 273 | |
| 274 | write_msi_msg_desc(desc, msg); |
| 275 | } |
| 276 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 277 | void mask_msi_irq(unsigned int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 279 | struct irq_desc *desc = irq_to_desc(irq); |
| 280 | |
| 281 | msi_set_mask_bits(desc, 1, 1); |
| 282 | msix_flush_writes(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 285 | void unmask_msi_irq(unsigned int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 287 | struct irq_desc *desc = irq_to_desc(irq); |
| 288 | |
| 289 | msi_set_mask_bits(desc, 1, 0); |
| 290 | msix_flush_writes(desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 293 | static int msi_free_irqs(struct pci_dev* dev); |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static struct msi_desc* alloc_msi_entry(void) |
| 296 | { |
| 297 | struct msi_desc *entry; |
| 298 | |
Michael Ellerman | 3e916c0 | 2007-03-22 21:51:36 +1100 | [diff] [blame] | 299 | entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (!entry) |
| 301 | return NULL; |
| 302 | |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 303 | INIT_LIST_HEAD(&entry->list); |
| 304 | entry->irq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | entry->dev = NULL; |
| 306 | |
| 307 | return entry; |
| 308 | } |
| 309 | |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 310 | static void pci_intx_for_msi(struct pci_dev *dev, int enable) |
| 311 | { |
| 312 | if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG)) |
| 313 | pci_intx(dev, enable); |
| 314 | } |
| 315 | |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 316 | static void __pci_restore_msi_state(struct pci_dev *dev) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 317 | { |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 318 | int pos; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 319 | u16 control; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 320 | struct msi_desc *entry; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 321 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 322 | if (!dev->msi_enabled) |
| 323 | return; |
| 324 | |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 325 | entry = get_irq_msi(dev->irq); |
| 326 | pos = entry->msi_attrib.pos; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 327 | |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 328 | pci_intx_for_msi(dev, 0); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 329 | msi_set_enable(dev, 0); |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 330 | write_msi_msg(dev->irq, &entry->msg); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 331 | if (entry->msi_attrib.maskbit) { |
| 332 | struct irq_desc *desc = irq_to_desc(dev->irq); |
| 333 | msi_set_mask_bits(desc, entry->msi_attrib.maskbits_mask, |
Yinghai Lu | 8e149e0 | 2008-04-23 14:56:30 -0700 | [diff] [blame] | 334 | entry->msi_attrib.masked); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 335 | } |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 336 | |
| 337 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); |
Jesse Barnes | abad2ec | 2008-08-07 08:52:37 -0700 | [diff] [blame] | 338 | control &= ~PCI_MSI_FLAGS_QSIZE; |
| 339 | control |= PCI_MSI_FLAGS_ENABLE; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 340 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void __pci_restore_msix_state(struct pci_dev *dev) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 344 | { |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 345 | int pos; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 346 | struct msi_desc *entry; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 347 | u16 control; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 348 | |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 349 | if (!dev->msix_enabled) |
| 350 | return; |
| 351 | |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 352 | /* route the table */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 353 | pci_intx_for_msi(dev, 0); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 354 | msix_set_enable(dev, 0); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 355 | |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 356 | list_for_each_entry(entry, &dev->msi_list, list) { |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 357 | struct irq_desc *desc = irq_to_desc(entry->irq); |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 358 | write_msi_msg(entry->irq, &entry->msg); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 359 | msi_set_mask_bits(desc, 1, entry->msi_attrib.masked); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 360 | } |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 361 | |
Michael Ellerman | 314e77b | 2007-04-05 17:19:12 +1000 | [diff] [blame] | 362 | BUG_ON(list_empty(&dev->msi_list)); |
| 363 | entry = list_entry(dev->msi_list.next, struct msi_desc, list); |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 364 | pos = entry->msi_attrib.pos; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 365 | pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control); |
| 366 | control &= ~PCI_MSIX_FLAGS_MASKALL; |
| 367 | control |= PCI_MSIX_FLAGS_ENABLE; |
| 368 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 369 | } |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 370 | |
| 371 | void pci_restore_msi_state(struct pci_dev *dev) |
| 372 | { |
| 373 | __pci_restore_msi_state(dev); |
| 374 | __pci_restore_msix_state(dev); |
| 375 | } |
Linas Vepstas | 94688cf | 2007-11-07 15:43:59 -0600 | [diff] [blame] | 376 | EXPORT_SYMBOL_GPL(pci_restore_msi_state); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | /** |
| 379 | * msi_capability_init - configure device's MSI capability structure |
| 380 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 381 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 382 | * Setup the MSI capability structure of device function with a single |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 383 | * MSI irq, regardless of device function is capable of handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | * multiple messages. A return of zero indicates the successful setup |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 385 | * of an entry zero with the new MSI irq or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | **/ |
| 387 | static int msi_capability_init(struct pci_dev *dev) |
| 388 | { |
| 389 | struct msi_desc *entry; |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 390 | int pos, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | u16 control; |
| 392 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 393 | msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */ |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 396 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 397 | /* MSI Entry Initialization */ |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 398 | entry = alloc_msi_entry(); |
| 399 | if (!entry) |
| 400 | return -ENOMEM; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | entry->msi_attrib.type = PCI_CAP_ID_MSI; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 403 | entry->msi_attrib.is_64 = is_64bit_address(control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | entry->msi_attrib.entry_nr = 0; |
| 405 | entry->msi_attrib.maskbit = is_mask_bit_support(control); |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 406 | entry->msi_attrib.masked = 1; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 407 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 408 | entry->msi_attrib.pos = pos; |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 409 | entry->dev = dev; |
| 410 | if (entry->msi_attrib.maskbit) { |
Hidetoshi Seto | 0db29af | 2008-12-24 17:27:04 +0900 | [diff] [blame] | 411 | unsigned int base, maskbits, temp; |
| 412 | |
| 413 | base = msi_mask_bits_reg(pos, entry->msi_attrib.is_64); |
| 414 | entry->mask_base = (void __iomem *)(long)base; |
| 415 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 416 | /* All MSIs are unmasked by default, Mask them all */ |
Hidetoshi Seto | 0db29af | 2008-12-24 17:27:04 +0900 | [diff] [blame] | 417 | pci_read_config_dword(dev, base, &maskbits); |
Matthew Wilcox | bffac3c | 2009-01-21 19:19:19 -0500 | [diff] [blame] | 418 | temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1); |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 419 | maskbits |= temp; |
Hidetoshi Seto | 0db29af | 2008-12-24 17:27:04 +0900 | [diff] [blame] | 420 | pci_write_config_dword(dev, base, maskbits); |
Yinghai Lu | 8e149e0 | 2008-04-23 14:56:30 -0700 | [diff] [blame] | 421 | entry->msi_attrib.maskbits_mask = temp; |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 422 | } |
Eric W. Biederman | 0dd11f9 | 2007-06-01 00:46:32 -0700 | [diff] [blame] | 423 | list_add_tail(&entry->list, &dev->msi_list); |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* Configure MSI capability structure */ |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 426 | ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI); |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 427 | if (ret) { |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 428 | msi_free_irqs(dev); |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 429 | return ret; |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 430 | } |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* Set MSI enabled bits */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 433 | pci_intx_for_msi(dev, 0); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 434 | msi_set_enable(dev, 1); |
| 435 | dev->msi_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 437 | dev->irq = entry->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * msix_capability_init - configure device's MSI-X capability |
| 443 | * @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] | 444 | * @entries: pointer to an array of struct msix_entry entries |
| 445 | * @nvec: number of @entries |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 447 | * Setup the MSI-X capability structure of device function with a |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 448 | * single MSI-X irq. A return of zero indicates the successful setup of |
| 449 | * requested MSI-X entries with allocated irqs or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | **/ |
| 451 | static int msix_capability_init(struct pci_dev *dev, |
| 452 | struct msix_entry *entries, int nvec) |
| 453 | { |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 454 | struct msi_desc *entry; |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 455 | int pos, i, j, nr_entries, ret; |
Grant Grundler | a0454b4 | 2006-02-16 23:58:29 -0800 | [diff] [blame] | 456 | unsigned long phys_addr; |
| 457 | u32 table_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | u16 control; |
| 459 | u8 bir; |
| 460 | void __iomem *base; |
| 461 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 462 | msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */ |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 465 | /* Request & Map MSI-X table region */ |
| 466 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 467 | nr_entries = multi_msix_capable(control); |
Grant Grundler | a0454b4 | 2006-02-16 23:58:29 -0800 | [diff] [blame] | 468 | |
| 469 | pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); |
Grant Grundler | a0454b4 | 2006-02-16 23:58:29 -0800 | [diff] [blame] | 471 | table_offset &= ~PCI_MSIX_FLAGS_BIRMASK; |
| 472 | phys_addr = pci_resource_start (dev, bir) + table_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); |
| 474 | if (base == NULL) |
| 475 | return -ENOMEM; |
| 476 | |
| 477 | /* MSI-X Table Initialization */ |
| 478 | for (i = 0; i < nvec; i++) { |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 479 | entry = alloc_msi_entry(); |
| 480 | if (!entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
| 483 | j = entries[i].entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | entry->msi_attrib.type = PCI_CAP_ID_MSIX; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 485 | entry->msi_attrib.is_64 = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | entry->msi_attrib.entry_nr = j; |
| 487 | entry->msi_attrib.maskbit = 1; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 488 | entry->msi_attrib.masked = 1; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 489 | entry->msi_attrib.default_irq = dev->irq; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 490 | entry->msi_attrib.pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | entry->dev = dev; |
| 492 | entry->mask_base = base; |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 493 | |
Eric W. Biederman | 0dd11f9 | 2007-06-01 00:46:32 -0700 | [diff] [blame] | 494 | list_add_tail(&entry->list, &dev->msi_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 496 | |
| 497 | ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); |
| 498 | if (ret) { |
| 499 | int avail = 0; |
| 500 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 501 | if (entry->irq != 0) { |
| 502 | avail++; |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 505 | |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 506 | msi_free_irqs(dev); |
| 507 | |
Eric W. Biederman | 92db6d1 | 2006-10-04 02:16:35 -0700 | [diff] [blame] | 508 | /* If we had some success report the number of irqs |
| 509 | * we succeeded in setting up. |
| 510 | */ |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 511 | if (avail == 0) |
| 512 | avail = ret; |
Eric W. Biederman | 92db6d1 | 2006-10-04 02:16:35 -0700 | [diff] [blame] | 513 | return avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 515 | |
| 516 | i = 0; |
| 517 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 518 | entries[i].vector = entry->irq; |
| 519 | set_irq_msi(entry->irq, entry); |
| 520 | i++; |
| 521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* Set MSI-X enabled bits */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 523 | pci_intx_for_msi(dev, 0); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 524 | msix_set_enable(dev, 1); |
| 525 | dev->msix_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /** |
Michael Ellerman | 17bbc12 | 2007-04-05 17:19:07 +1000 | [diff] [blame] | 531 | * pci_msi_check_device - check whether MSI may be enabled on a device |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 532 | * @dev: pointer to the pci_dev data structure of MSI device function |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 533 | * @nvec: how many MSIs have been requested ? |
Michael Ellerman | b1e2303 | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 534 | * @type: are we checking for MSI or MSI-X ? |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 535 | * |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 536 | * Look at global flags, the device itself, and its parent busses |
Michael Ellerman | 17bbc12 | 2007-04-05 17:19:07 +1000 | [diff] [blame] | 537 | * to determine if MSI/-X are supported for the device. If MSI/-X is |
| 538 | * supported return 0, else return an error code. |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 539 | **/ |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 540 | static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type) |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 541 | { |
| 542 | struct pci_bus *bus; |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 543 | int ret; |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 544 | |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 545 | /* MSI must be globally enabled and supported by the device */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 546 | if (!pci_msi_enable || !dev || dev->no_msi) |
| 547 | return -EINVAL; |
| 548 | |
Michael Ellerman | 314e77b | 2007-04-05 17:19:12 +1000 | [diff] [blame] | 549 | /* |
| 550 | * You can't ask to have 0 or less MSIs configured. |
| 551 | * a) it's stupid .. |
| 552 | * b) the list manipulation code assumes nvec >= 1. |
| 553 | */ |
| 554 | if (nvec < 1) |
| 555 | return -ERANGE; |
| 556 | |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 557 | /* Any bridge which does NOT route MSI transactions from it's |
| 558 | * secondary bus to it's primary bus must set NO_MSI flag on |
| 559 | * the secondary pci_bus. |
| 560 | * We expect only arch-specific PCI host bus controller driver |
| 561 | * or quirks for specific PCI bridges to be setting NO_MSI. |
| 562 | */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 563 | for (bus = dev->bus; bus; bus = bus->parent) |
| 564 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
| 565 | return -EINVAL; |
| 566 | |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 567 | ret = arch_msi_check_device(dev, nvec, type); |
| 568 | if (ret) |
| 569 | return ret; |
| 570 | |
Michael Ellerman | b1e2303 | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 571 | if (!pci_find_capability(dev, type)) |
| 572 | return -EINVAL; |
| 573 | |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | * pci_enable_msi - configure device's MSI capability structure |
| 579 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 580 | * |
| 581 | * Setup the MSI capability structure of device function with |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 582 | * a single MSI irq upon its software driver call to request for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | * MSI mode enabled on its hardware device function. A return of zero |
| 584 | * indicates the successful setup of an entry zero with the new MSI |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 585 | * irq or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | **/ |
| 587 | int pci_enable_msi(struct pci_dev* dev) |
| 588 | { |
Michael Ellerman | b1e2303 | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 589 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 591 | status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI); |
| 592 | if (status) |
| 593 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 595 | WARN_ON(!!dev->msi_enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 597 | /* Check whether driver already requested for MSI-X irqs */ |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 598 | if (dev->msix_enabled) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 599 | dev_info(&dev->dev, "can't enable MSI " |
| 600 | "(MSI-X already enabled)\n"); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 601 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | status = msi_capability_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return status; |
| 605 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 606 | EXPORT_SYMBOL(pci_enable_msi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 608 | void pci_msi_shutdown(struct pci_dev* dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
| 610 | struct msi_desc *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
Michael Ellerman | 128bc5f | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 612 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 613 | return; |
| 614 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 615 | msi_set_enable(dev, 0); |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 616 | pci_intx_for_msi(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 617 | dev->msi_enabled = 0; |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 618 | |
Michael Ellerman | 314e77b | 2007-04-05 17:19:12 +1000 | [diff] [blame] | 619 | BUG_ON(list_empty(&dev->msi_list)); |
| 620 | entry = list_entry(dev->msi_list.next, struct msi_desc, list); |
Yinghai Lu | 8e149e0 | 2008-04-23 14:56:30 -0700 | [diff] [blame] | 621 | /* Return the the pci reset with msi irqs unmasked */ |
| 622 | if (entry->msi_attrib.maskbit) { |
| 623 | u32 mask = entry->msi_attrib.maskbits_mask; |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 624 | struct irq_desc *desc = irq_to_desc(dev->irq); |
| 625 | msi_set_mask_bits(desc, mask, ~mask); |
Yinghai Lu | 8e149e0 | 2008-04-23 14:56:30 -0700 | [diff] [blame] | 626 | } |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 627 | if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | return; |
Michael Ellerman | e387b9e | 2007-03-22 21:51:27 +1100 | [diff] [blame] | 629 | |
| 630 | /* Restore dev->irq to its default pin-assertion irq */ |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 631 | dev->irq = entry->msi_attrib.default_irq; |
| 632 | } |
| 633 | void pci_disable_msi(struct pci_dev* dev) |
| 634 | { |
| 635 | struct msi_desc *entry; |
| 636 | |
| 637 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
| 638 | return; |
| 639 | |
| 640 | pci_msi_shutdown(dev); |
| 641 | |
| 642 | entry = list_entry(dev->msi_list.next, struct msi_desc, list); |
| 643 | if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) |
| 644 | return; |
| 645 | |
| 646 | msi_free_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 648 | EXPORT_SYMBOL(pci_disable_msi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 650 | static int msi_free_irqs(struct pci_dev* dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 652 | struct msi_desc *entry, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
David Miller | b3b7cc7 | 2007-05-11 13:26:44 -0700 | [diff] [blame] | 654 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 655 | if (entry->irq) |
| 656 | BUG_ON(irq_has_action(entry->irq)); |
| 657 | } |
Michael Ellerman | 7ede9c1 | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 658 | |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 659 | arch_teardown_msi_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 661 | list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) { |
| 662 | if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) { |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 663 | writel(1, entry->mask_base + entry->msi_attrib.entry_nr |
| 664 | * PCI_MSIX_ENTRY_SIZE |
| 665 | + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET); |
Eric W. Biederman | 78b7611 | 2007-06-01 00:46:33 -0700 | [diff] [blame] | 666 | |
| 667 | if (list_is_last(&entry->list, &dev->msi_list)) |
| 668 | iounmap(entry->mask_base); |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 669 | } |
| 670 | list_del(&entry->list); |
| 671 | kfree(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /** |
| 678 | * pci_enable_msix - configure device's MSI-X capability structure |
| 679 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
Greg Kroah-Hartman | 70549ad | 2005-06-06 23:07:46 -0700 | [diff] [blame] | 680 | * @entries: pointer to an array of MSI-X entries |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 681 | * @nvec: number of MSI-X irqs requested for allocation by device driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | * |
| 683 | * Setup the MSI-X capability structure of device function with the number |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 684 | * of requested irqs upon its software driver call to request for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | * MSI-X mode enabled on its hardware device function. A return of zero |
| 686 | * indicates the successful configuration of MSI-X capability structure |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 687 | * with new allocated MSI-X irqs. A return of < 0 indicates a failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | * Or a return of > 0 indicates that driver request is exceeding the number |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 689 | * of irqs available. Driver should use the returned value to re-send |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | * its request. |
| 691 | **/ |
| 692 | int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) |
| 693 | { |
Eric W. Biederman | 92db6d1 | 2006-10-04 02:16:35 -0700 | [diff] [blame] | 694 | int status, pos, nr_entries; |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 695 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | u16 control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 698 | if (!entries) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return -EINVAL; |
| 700 | |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 701 | status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX); |
| 702 | if (status) |
| 703 | return status; |
| 704 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 705 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | nr_entries = multi_msix_capable(control); |
| 708 | if (nvec > nr_entries) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | /* Check for any invalid entries */ |
| 712 | for (i = 0; i < nvec; i++) { |
| 713 | if (entries[i].entry >= nr_entries) |
| 714 | return -EINVAL; /* invalid entry */ |
| 715 | for (j = i + 1; j < nvec; j++) { |
| 716 | if (entries[i].entry == entries[j].entry) |
| 717 | return -EINVAL; /* duplicate entry */ |
| 718 | } |
| 719 | } |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 720 | WARN_ON(!!dev->msix_enabled); |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 721 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 722 | /* Check whether driver already requested for MSI irq */ |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 723 | if (dev->msi_enabled) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 724 | dev_info(&dev->dev, "can't enable MSI-X " |
| 725 | "(MSI IRQ already assigned)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | return -EINVAL; |
| 727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | status = msix_capability_init(dev, entries, nvec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | return status; |
| 730 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 731 | EXPORT_SYMBOL(pci_enable_msix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 733 | static void msix_free_all_irqs(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 735 | msi_free_irqs(dev); |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 736 | } |
| 737 | |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 738 | void pci_msix_shutdown(struct pci_dev* dev) |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 739 | { |
Michael Ellerman | 128bc5f | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 740 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 741 | return; |
| 742 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 743 | msix_set_enable(dev, 0); |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 744 | pci_intx_for_msi(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 745 | dev->msix_enabled = 0; |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 746 | } |
| 747 | void pci_disable_msix(struct pci_dev* dev) |
| 748 | { |
| 749 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
| 750 | return; |
| 751 | |
| 752 | pci_msix_shutdown(dev); |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 753 | |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 754 | msix_free_all_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 756 | EXPORT_SYMBOL(pci_disable_msix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | /** |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 759 | * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * @dev: pointer to the pci_dev data structure of MSI(X) device function |
| 761 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 762 | * Being called during hotplug remove, from which the device function |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 763 | * is hot-removed. All previous assigned MSI/MSI-X irqs, if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | * allocated for this device function, are reclaimed to unused state, |
| 765 | * which may be used later on. |
| 766 | **/ |
| 767 | void msi_remove_pci_irq_vectors(struct pci_dev* dev) |
| 768 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | if (!pci_msi_enable || !dev) |
| 770 | return; |
| 771 | |
Michael Ellerman | 032de8e | 2007-04-18 19:39:22 +1000 | [diff] [blame] | 772 | if (dev->msi_enabled) |
| 773 | msi_free_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 775 | if (dev->msix_enabled) |
| 776 | msix_free_all_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 779 | void pci_no_msi(void) |
| 780 | { |
| 781 | pci_msi_enable = 0; |
| 782 | } |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 783 | |
Andrew Patterson | 07ae95f | 2008-11-10 15:31:05 -0700 | [diff] [blame] | 784 | /** |
| 785 | * pci_msi_enabled - is MSI enabled? |
| 786 | * |
| 787 | * Returns true if MSI has not been disabled by the command-line option |
| 788 | * pci=nomsi. |
| 789 | **/ |
| 790 | int pci_msi_enabled(void) |
| 791 | { |
| 792 | return pci_msi_enable; |
| 793 | } |
| 794 | EXPORT_SYMBOL(pci_msi_enabled); |
| 795 | |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 796 | void pci_msi_init_pci_dev(struct pci_dev *dev) |
| 797 | { |
| 798 | INIT_LIST_HEAD(&dev->msi_list); |
| 799 | } |