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