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