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> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/pci.h> |
| 17 | #include <linux/proc_fs.h> |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 18 | #include <linux/msi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/errno.h> |
| 21 | #include <asm/io.h> |
| 22 | #include <asm/smp.h> |
| 23 | |
| 24 | #include "pci.h" |
| 25 | #include "msi.h" |
| 26 | |
| 27 | static DEFINE_SPINLOCK(msi_lock); |
| 28 | static struct msi_desc* msi_desc[NR_IRQS] = { [0 ... NR_IRQS-1] = NULL }; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 29 | static struct kmem_cache* msi_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | static int pci_msi_enable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | static int msi_cache_init(void) |
| 34 | { |
Pekka J Enberg | 5718178 | 2006-09-27 01:51:03 -0700 | [diff] [blame] | 35 | msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc), |
| 36 | 0, SLAB_HWCACHE_ALIGN, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | if (!msi_cachep) |
| 38 | return -ENOMEM; |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 43 | static void msi_set_mask_bit(unsigned int irq, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | struct msi_desc *entry; |
| 46 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 47 | entry = msi_desc[irq]; |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 48 | BUG_ON(!entry || !entry->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | switch (entry->msi_attrib.type) { |
| 50 | case PCI_CAP_ID_MSI: |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 51 | if (entry->msi_attrib.maskbit) { |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame^] | 52 | int pos; |
| 53 | u32 mask_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 55 | pos = (long)entry->mask_base; |
| 56 | pci_read_config_dword(entry->dev, pos, &mask_bits); |
| 57 | mask_bits &= ~(1); |
| 58 | mask_bits |= flag; |
| 59 | pci_write_config_dword(entry->dev, pos, mask_bits); |
| 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | case PCI_CAP_ID_MSIX: |
| 63 | { |
| 64 | int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + |
| 65 | PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET; |
| 66 | writel(flag, entry->mask_base + offset); |
| 67 | break; |
| 68 | } |
| 69 | default: |
Eric W. Biederman | 277bc33 | 2006-10-04 02:16:57 -0700 | [diff] [blame] | 70 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 75 | void read_msi_msg(unsigned int irq, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 76 | { |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 77 | struct msi_desc *entry = get_irq_data(irq); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 78 | switch(entry->msi_attrib.type) { |
| 79 | case PCI_CAP_ID_MSI: |
| 80 | { |
| 81 | struct pci_dev *dev = entry->dev; |
| 82 | int pos = entry->msi_attrib.pos; |
| 83 | u16 data; |
| 84 | |
| 85 | pci_read_config_dword(dev, msi_lower_address_reg(pos), |
| 86 | &msg->address_lo); |
| 87 | if (entry->msi_attrib.is_64) { |
| 88 | pci_read_config_dword(dev, msi_upper_address_reg(pos), |
| 89 | &msg->address_hi); |
| 90 | pci_read_config_word(dev, msi_data_reg(pos, 1), &data); |
| 91 | } else { |
| 92 | msg->address_hi = 0; |
| 93 | pci_read_config_word(dev, msi_data_reg(pos, 1), &data); |
| 94 | } |
| 95 | msg->data = data; |
| 96 | break; |
| 97 | } |
| 98 | case PCI_CAP_ID_MSIX: |
| 99 | { |
| 100 | void __iomem *base; |
| 101 | base = entry->mask_base + |
| 102 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 103 | |
| 104 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); |
| 105 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); |
| 106 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET); |
| 107 | break; |
| 108 | } |
| 109 | default: |
| 110 | BUG(); |
| 111 | } |
| 112 | } |
| 113 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 114 | void write_msi_msg(unsigned int irq, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 115 | { |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 116 | struct msi_desc *entry = get_irq_data(irq); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 117 | switch (entry->msi_attrib.type) { |
| 118 | case PCI_CAP_ID_MSI: |
| 119 | { |
| 120 | struct pci_dev *dev = entry->dev; |
| 121 | int pos = entry->msi_attrib.pos; |
| 122 | |
| 123 | pci_write_config_dword(dev, msi_lower_address_reg(pos), |
| 124 | msg->address_lo); |
| 125 | if (entry->msi_attrib.is_64) { |
| 126 | pci_write_config_dword(dev, msi_upper_address_reg(pos), |
| 127 | msg->address_hi); |
| 128 | pci_write_config_word(dev, msi_data_reg(pos, 1), |
| 129 | msg->data); |
| 130 | } else { |
| 131 | pci_write_config_word(dev, msi_data_reg(pos, 0), |
| 132 | msg->data); |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | case PCI_CAP_ID_MSIX: |
| 137 | { |
| 138 | void __iomem *base; |
| 139 | base = entry->mask_base + |
| 140 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 141 | |
| 142 | writel(msg->address_lo, |
| 143 | base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); |
| 144 | writel(msg->address_hi, |
| 145 | base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); |
| 146 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET); |
| 147 | break; |
| 148 | } |
| 149 | default: |
| 150 | BUG(); |
| 151 | } |
| 152 | } |
| 153 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 154 | void mask_msi_irq(unsigned int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 156 | msi_set_mask_bit(irq, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 159 | void unmask_msi_irq(unsigned int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 161 | msi_set_mask_bit(irq, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 164 | static int msi_free_irq(struct pci_dev* dev, int irq); |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame^] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static int msi_init(void) |
| 167 | { |
| 168 | static int status = -ENOMEM; |
| 169 | |
| 170 | if (!status) |
| 171 | return status; |
| 172 | |
| 173 | if (pci_msi_quirk) { |
| 174 | pci_msi_enable = 0; |
| 175 | printk(KERN_WARNING "PCI: MSI quirk detected. MSI disabled.\n"); |
| 176 | status = -EINVAL; |
| 177 | return status; |
| 178 | } |
| 179 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 180 | status = msi_cache_init(); |
| 181 | if (status < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | pci_msi_enable = 0; |
| 183 | printk(KERN_WARNING "PCI: MSI cache init failed\n"); |
| 184 | return status; |
| 185 | } |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return status; |
| 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | static struct msi_desc* alloc_msi_entry(void) |
| 191 | { |
| 192 | struct msi_desc *entry; |
| 193 | |
Pekka J Enberg | 5718178 | 2006-09-27 01:51:03 -0700 | [diff] [blame] | 194 | entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (!entry) |
| 196 | return NULL; |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | entry->link.tail = entry->link.head = 0; /* single message */ |
| 199 | entry->dev = NULL; |
| 200 | |
| 201 | return entry; |
| 202 | } |
| 203 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 204 | static void attach_msi_entry(struct msi_desc *entry, int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | unsigned long flags; |
| 207 | |
| 208 | spin_lock_irqsave(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 209 | msi_desc[irq] = entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | spin_unlock_irqrestore(&msi_lock, flags); |
| 211 | } |
| 212 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 213 | static int create_msi_irq(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 215 | struct msi_desc *entry; |
| 216 | int irq; |
Ingo Molnar | f6bc266 | 2006-01-26 01:42:11 +0100 | [diff] [blame] | 217 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 218 | entry = alloc_msi_entry(); |
| 219 | if (!entry) |
| 220 | return -ENOMEM; |
| 221 | |
| 222 | irq = create_irq(); |
| 223 | if (irq < 0) { |
| 224 | kmem_cache_free(msi_cachep, entry); |
| 225 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 227 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 228 | set_irq_data(irq, entry); |
| 229 | |
| 230 | return irq; |
| 231 | } |
| 232 | |
| 233 | static void destroy_msi_irq(unsigned int irq) |
| 234 | { |
| 235 | struct msi_desc *entry; |
| 236 | |
| 237 | entry = get_irq_data(irq); |
| 238 | set_irq_chip(irq, NULL); |
| 239 | set_irq_data(irq, NULL); |
| 240 | destroy_irq(irq); |
| 241 | kmem_cache_free(msi_cachep, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void enable_msi_mode(struct pci_dev *dev, int pos, int type) |
| 245 | { |
| 246 | u16 control; |
| 247 | |
| 248 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 249 | if (type == PCI_CAP_ID_MSI) { |
| 250 | /* Set enabled bits to single MSI & enable MSI_enable bit */ |
| 251 | msi_enable(control, 1); |
| 252 | pci_write_config_word(dev, msi_control_reg(pos), control); |
Shaohua Li | 99dc804 | 2006-05-26 10:58:27 +0800 | [diff] [blame] | 253 | dev->msi_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } else { |
| 255 | msix_enable(control); |
| 256 | pci_write_config_word(dev, msi_control_reg(pos), control); |
Shaohua Li | 99dc804 | 2006-05-26 10:58:27 +0800 | [diff] [blame] | 257 | dev->msix_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
Jeff Garzik | 1769b46 | 2006-12-07 17:56:06 -0500 | [diff] [blame] | 259 | |
| 260 | pci_intx(dev, 0); /* disable intx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Kristen Accardi | 4602b88 | 2005-08-16 15:15:58 -0700 | [diff] [blame] | 263 | void disable_msi_mode(struct pci_dev *dev, int pos, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | u16 control; |
| 266 | |
| 267 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 268 | if (type == PCI_CAP_ID_MSI) { |
| 269 | /* Set enabled bits to single MSI & enable MSI_enable bit */ |
| 270 | msi_disable(control); |
| 271 | pci_write_config_word(dev, msi_control_reg(pos), control); |
Shaohua Li | 99dc804 | 2006-05-26 10:58:27 +0800 | [diff] [blame] | 272 | dev->msi_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } else { |
| 274 | msix_disable(control); |
| 275 | pci_write_config_word(dev, msi_control_reg(pos), control); |
Shaohua Li | 99dc804 | 2006-05-26 10:58:27 +0800 | [diff] [blame] | 276 | dev->msix_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
Jeff Garzik | 1769b46 | 2006-12-07 17:56:06 -0500 | [diff] [blame] | 278 | |
| 279 | pci_intx(dev, 1); /* enable intx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 282 | static int msi_lookup_irq(struct pci_dev *dev, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 284 | int irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | unsigned long flags; |
| 286 | |
| 287 | spin_lock_irqsave(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 288 | for (irq = 0; irq < NR_IRQS; irq++) { |
| 289 | if (!msi_desc[irq] || msi_desc[irq]->dev != dev || |
| 290 | msi_desc[irq]->msi_attrib.type != type || |
| 291 | msi_desc[irq]->msi_attrib.default_irq != dev->irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | continue; |
| 293 | spin_unlock_irqrestore(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 294 | /* This pre-assigned MSI irq for this device |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame^] | 295 | already exists. Override dev->irq with this irq */ |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 296 | dev->irq = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | spin_unlock_irqrestore(&msi_lock, flags); |
| 300 | |
| 301 | return -EACCES; |
| 302 | } |
| 303 | |
| 304 | void pci_scan_msi_device(struct pci_dev *dev) |
| 305 | { |
| 306 | if (!dev) |
| 307 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 310 | #ifdef CONFIG_PM |
| 311 | int pci_save_msi_state(struct pci_dev *dev) |
| 312 | { |
| 313 | int pos, i = 0; |
| 314 | u16 control; |
| 315 | struct pci_cap_saved_state *save_state; |
| 316 | u32 *cap; |
| 317 | |
| 318 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 319 | if (pos <= 0 || dev->no_msi) |
| 320 | return 0; |
| 321 | |
| 322 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 323 | if (!(control & PCI_MSI_FLAGS_ENABLE)) |
| 324 | return 0; |
| 325 | |
| 326 | save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u32) * 5, |
| 327 | GFP_KERNEL); |
| 328 | if (!save_state) { |
| 329 | printk(KERN_ERR "Out of memory in pci_save_msi_state\n"); |
| 330 | return -ENOMEM; |
| 331 | } |
| 332 | cap = &save_state->data[0]; |
| 333 | |
| 334 | pci_read_config_dword(dev, pos, &cap[i++]); |
| 335 | control = cap[0] >> 16; |
| 336 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, &cap[i++]); |
| 337 | if (control & PCI_MSI_FLAGS_64BIT) { |
| 338 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, &cap[i++]); |
| 339 | pci_read_config_dword(dev, pos + PCI_MSI_DATA_64, &cap[i++]); |
| 340 | } else |
| 341 | pci_read_config_dword(dev, pos + PCI_MSI_DATA_32, &cap[i++]); |
| 342 | if (control & PCI_MSI_FLAGS_MASKBIT) |
| 343 | pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT, &cap[i++]); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 344 | save_state->cap_nr = PCI_CAP_ID_MSI; |
| 345 | pci_add_saved_cap(dev, save_state); |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | void pci_restore_msi_state(struct pci_dev *dev) |
| 350 | { |
| 351 | int i = 0, pos; |
| 352 | u16 control; |
| 353 | struct pci_cap_saved_state *save_state; |
| 354 | u32 *cap; |
| 355 | |
| 356 | save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSI); |
| 357 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 358 | if (!save_state || pos <= 0) |
| 359 | return; |
| 360 | cap = &save_state->data[0]; |
| 361 | |
| 362 | control = cap[i++] >> 16; |
| 363 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, cap[i++]); |
| 364 | if (control & PCI_MSI_FLAGS_64BIT) { |
| 365 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, cap[i++]); |
| 366 | pci_write_config_dword(dev, pos + PCI_MSI_DATA_64, cap[i++]); |
| 367 | } else |
| 368 | pci_write_config_dword(dev, pos + PCI_MSI_DATA_32, cap[i++]); |
| 369 | if (control & PCI_MSI_FLAGS_MASKBIT) |
| 370 | pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT, cap[i++]); |
| 371 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); |
| 372 | enable_msi_mode(dev, pos, PCI_CAP_ID_MSI); |
| 373 | pci_remove_saved_cap(save_state); |
| 374 | kfree(save_state); |
| 375 | } |
| 376 | |
| 377 | int pci_save_msix_state(struct pci_dev *dev) |
| 378 | { |
| 379 | int pos; |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 380 | int temp; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 381 | int irq, head, tail = 0; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 382 | u16 control; |
| 383 | struct pci_cap_saved_state *save_state; |
| 384 | |
| 385 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 386 | if (pos <= 0 || dev->no_msi) |
| 387 | return 0; |
| 388 | |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 389 | /* save the capability */ |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 390 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 391 | if (!(control & PCI_MSIX_FLAGS_ENABLE)) |
| 392 | return 0; |
| 393 | save_state = kzalloc(sizeof(struct pci_cap_saved_state) + sizeof(u16), |
| 394 | GFP_KERNEL); |
| 395 | if (!save_state) { |
| 396 | printk(KERN_ERR "Out of memory in pci_save_msix_state\n"); |
| 397 | return -ENOMEM; |
| 398 | } |
| 399 | *((u16 *)&save_state->data[0]) = control; |
| 400 | |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 401 | /* save the table */ |
| 402 | temp = dev->irq; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 403 | if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) { |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 404 | kfree(save_state); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 408 | irq = head = dev->irq; |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 409 | while (head != tail) { |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 410 | struct msi_desc *entry; |
| 411 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 412 | entry = msi_desc[irq]; |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 413 | read_msi_msg(irq, &entry->msg_save); |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 414 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 415 | tail = msi_desc[irq]->link.tail; |
| 416 | irq = tail; |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 417 | } |
| 418 | dev->irq = temp; |
| 419 | |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 420 | save_state->cap_nr = PCI_CAP_ID_MSIX; |
| 421 | pci_add_saved_cap(dev, save_state); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | void pci_restore_msix_state(struct pci_dev *dev) |
| 426 | { |
| 427 | u16 save; |
| 428 | int pos; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 429 | int irq, head, tail = 0; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 430 | struct msi_desc *entry; |
| 431 | int temp; |
| 432 | struct pci_cap_saved_state *save_state; |
| 433 | |
| 434 | save_state = pci_find_saved_cap(dev, PCI_CAP_ID_MSIX); |
| 435 | if (!save_state) |
| 436 | return; |
| 437 | save = *((u16 *)&save_state->data[0]); |
| 438 | pci_remove_saved_cap(save_state); |
| 439 | kfree(save_state); |
| 440 | |
| 441 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 442 | if (pos <= 0) |
| 443 | return; |
| 444 | |
| 445 | /* route the table */ |
| 446 | temp = dev->irq; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 447 | if (msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 448 | return; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 449 | irq = head = dev->irq; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 450 | while (head != tail) { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 451 | entry = msi_desc[irq]; |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 452 | write_msi_msg(irq, &entry->msg_save); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 453 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 454 | tail = msi_desc[irq]->link.tail; |
| 455 | irq = tail; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 456 | } |
| 457 | dev->irq = temp; |
| 458 | |
| 459 | pci_write_config_word(dev, msi_control_reg(pos), save); |
| 460 | enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX); |
| 461 | } |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame^] | 462 | #endif /* CONFIG_PM */ |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /** |
| 465 | * msi_capability_init - configure device's MSI capability structure |
| 466 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 467 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 468 | * Setup the MSI capability structure of device function with a single |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 469 | * MSI irq, regardless of device function is capable of handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | * multiple messages. A return of zero indicates the successful setup |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 471 | * 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] | 472 | **/ |
| 473 | static int msi_capability_init(struct pci_dev *dev) |
| 474 | { |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 475 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | struct msi_desc *entry; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 477 | int pos, irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | u16 control; |
| 479 | |
| 480 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 481 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 482 | /* MSI Entry Initialization */ |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 483 | irq = create_msi_irq(); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 484 | if (irq < 0) |
| 485 | return irq; |
| 486 | |
| 487 | entry = get_irq_data(irq); |
| 488 | entry->link.head = irq; |
| 489 | entry->link.tail = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | entry->msi_attrib.type = PCI_CAP_ID_MSI; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 491 | entry->msi_attrib.is_64 = is_64bit_address(control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | entry->msi_attrib.entry_nr = 0; |
| 493 | entry->msi_attrib.maskbit = is_mask_bit_support(control); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 494 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 495 | entry->msi_attrib.pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (is_mask_bit_support(control)) { |
| 497 | entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos, |
| 498 | is_64bit_address(control)); |
| 499 | } |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 500 | entry->dev = dev; |
| 501 | if (entry->msi_attrib.maskbit) { |
| 502 | unsigned int maskbits, temp; |
| 503 | /* All MSIs are unmasked by default, Mask them all */ |
| 504 | pci_read_config_dword(dev, |
| 505 | msi_mask_bits_reg(pos, is_64bit_address(control)), |
| 506 | &maskbits); |
| 507 | temp = (1 << multi_msi_capable(control)); |
| 508 | temp = ((temp - 1) & ~temp); |
| 509 | maskbits |= temp; |
| 510 | pci_write_config_dword(dev, |
| 511 | msi_mask_bits_reg(pos, is_64bit_address(control)), |
| 512 | maskbits); |
| 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | /* Configure MSI capability structure */ |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 515 | status = arch_setup_msi_irq(irq, dev); |
| 516 | if (status < 0) { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 517 | destroy_msi_irq(irq); |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 518 | return status; |
| 519 | } |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 520 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 521 | attach_msi_entry(entry, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* Set MSI enabled bits */ |
| 523 | enable_msi_mode(dev, pos, PCI_CAP_ID_MSI); |
| 524 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 525 | dev->irq = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * msix_capability_init - configure device's MSI-X capability |
| 531 | * @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] | 532 | * @entries: pointer to an array of struct msix_entry entries |
| 533 | * @nvec: number of @entries |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 535 | * Setup the MSI-X capability structure of device function with a |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 536 | * single MSI-X irq. A return of zero indicates the successful setup of |
| 537 | * requested MSI-X entries with allocated irqs or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | **/ |
| 539 | static int msix_capability_init(struct pci_dev *dev, |
| 540 | struct msix_entry *entries, int nvec) |
| 541 | { |
| 542 | struct msi_desc *head = NULL, *tail = NULL, *entry = NULL; |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 543 | int status; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 544 | int irq, pos, i, j, nr_entries, temp = 0; |
Grant Grundler | a0454b4 | 2006-02-16 23:58:29 -0800 | [diff] [blame] | 545 | unsigned long phys_addr; |
| 546 | u32 table_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | u16 control; |
| 548 | u8 bir; |
| 549 | void __iomem *base; |
| 550 | |
| 551 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 552 | /* Request & Map MSI-X table region */ |
| 553 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 554 | nr_entries = multi_msix_capable(control); |
Grant Grundler | a0454b4 | 2006-02-16 23:58:29 -0800 | [diff] [blame] | 555 | |
| 556 | pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); |
Grant Grundler | a0454b4 | 2006-02-16 23:58:29 -0800 | [diff] [blame] | 558 | table_offset &= ~PCI_MSIX_FLAGS_BIRMASK; |
| 559 | phys_addr = pci_resource_start (dev, bir) + table_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); |
| 561 | if (base == NULL) |
| 562 | return -ENOMEM; |
| 563 | |
| 564 | /* MSI-X Table Initialization */ |
| 565 | for (i = 0; i < nvec; i++) { |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 566 | irq = create_msi_irq(); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 567 | if (irq < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 570 | entry = get_irq_data(irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | j = entries[i].entry; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 572 | entries[i].vector = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | entry->msi_attrib.type = PCI_CAP_ID_MSIX; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 574 | entry->msi_attrib.is_64 = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | entry->msi_attrib.entry_nr = j; |
| 576 | entry->msi_attrib.maskbit = 1; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 577 | entry->msi_attrib.default_irq = dev->irq; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 578 | entry->msi_attrib.pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | entry->dev = dev; |
| 580 | entry->mask_base = base; |
| 581 | if (!head) { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 582 | entry->link.head = irq; |
| 583 | entry->link.tail = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | head = entry; |
| 585 | } else { |
| 586 | entry->link.head = temp; |
| 587 | entry->link.tail = tail->link.tail; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 588 | tail->link.tail = irq; |
| 589 | head->link.head = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 591 | temp = irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | tail = entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* Configure MSI-X capability structure */ |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 594 | status = arch_setup_msi_irq(irq, dev); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 595 | if (status < 0) { |
| 596 | destroy_msi_irq(irq); |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 597 | break; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 598 | } |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 599 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 600 | attach_msi_entry(entry, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | if (i != nvec) { |
Eric W. Biederman | 92db6d1 | 2006-10-04 02:16:35 -0700 | [diff] [blame] | 603 | int avail = i - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | i--; |
| 605 | for (; i >= 0; i--) { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 606 | irq = (entries + i)->vector; |
| 607 | msi_free_irq(dev, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | (entries + i)->vector = 0; |
| 609 | } |
Eric W. Biederman | 92db6d1 | 2006-10-04 02:16:35 -0700 | [diff] [blame] | 610 | /* If we had some success report the number of irqs |
| 611 | * we succeeded in setting up. |
| 612 | */ |
| 613 | if (avail <= 0) |
| 614 | avail = -EBUSY; |
| 615 | return avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | /* Set MSI-X enabled bits */ |
| 618 | enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX); |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | /** |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 624 | * pci_msi_supported - check whether MSI may be enabled on device |
| 625 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 626 | * |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 627 | * Look at global flags, the device itself, and its parent busses |
| 628 | * to return 0 if MSI are supported for the device. |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 629 | **/ |
| 630 | static |
| 631 | int pci_msi_supported(struct pci_dev * dev) |
| 632 | { |
| 633 | struct pci_bus *bus; |
| 634 | |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 635 | /* MSI must be globally enabled and supported by the device */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 636 | if (!pci_msi_enable || !dev || dev->no_msi) |
| 637 | return -EINVAL; |
| 638 | |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 639 | /* Any bridge which does NOT route MSI transactions from it's |
| 640 | * secondary bus to it's primary bus must set NO_MSI flag on |
| 641 | * the secondary pci_bus. |
| 642 | * We expect only arch-specific PCI host bus controller driver |
| 643 | * or quirks for specific PCI bridges to be setting NO_MSI. |
| 644 | */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 645 | for (bus = dev->bus; bus; bus = bus->parent) |
| 646 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
| 647 | return -EINVAL; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | * pci_enable_msi - configure device's MSI capability structure |
| 654 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 655 | * |
| 656 | * Setup the MSI capability structure of device function with |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 657 | * a single MSI irq upon its software driver call to request for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | * MSI mode enabled on its hardware device function. A return of zero |
| 659 | * 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] | 660 | * irq or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | **/ |
| 662 | int pci_enable_msi(struct pci_dev* dev) |
| 663 | { |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 664 | int pos, temp, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 666 | if (pci_msi_supported(dev) < 0) |
| 667 | return -EINVAL; |
Michael S. Tsirkin | 6e325a6 | 2006-02-14 18:52:22 +0200 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | temp = dev->irq; |
| 670 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 671 | status = msi_init(); |
| 672 | if (status < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return status; |
| 674 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 675 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 676 | if (!pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return -EINVAL; |
| 678 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 679 | WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSI)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 681 | /* Check whether driver already requested for MSI-X irqs */ |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 682 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 683 | if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | printk(KERN_INFO "PCI: %s: Can't enable MSI. " |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 685 | "Device already has MSI-X irq assigned\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | pci_name(dev)); |
| 687 | dev->irq = temp; |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | status = msi_capability_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return status; |
| 692 | } |
| 693 | |
| 694 | void pci_disable_msi(struct pci_dev* dev) |
| 695 | { |
| 696 | struct msi_desc *entry; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 697 | int pos, default_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | u16 control; |
| 699 | unsigned long flags; |
| 700 | |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 701 | if (!pci_msi_enable) |
| 702 | return; |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 703 | if (!dev) |
| 704 | return; |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 705 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 706 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 707 | if (!pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return; |
| 709 | |
| 710 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 711 | if (!(control & PCI_MSI_FLAGS_ENABLE)) |
| 712 | return; |
| 713 | |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 714 | disable_msi_mode(dev, pos, PCI_CAP_ID_MSI); |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | spin_lock_irqsave(&msi_lock, flags); |
| 717 | entry = msi_desc[dev->irq]; |
| 718 | if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) { |
| 719 | spin_unlock_irqrestore(&msi_lock, flags); |
| 720 | return; |
| 721 | } |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 722 | if (irq_has_action(dev->irq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | spin_unlock_irqrestore(&msi_lock, flags); |
| 724 | printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without " |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 725 | "free_irq() on MSI irq %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | pci_name(dev), dev->irq); |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 727 | BUG_ON(irq_has_action(dev->irq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } else { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 729 | default_irq = entry->msi_attrib.default_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | spin_unlock_irqrestore(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 731 | msi_free_irq(dev, dev->irq); |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 732 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 733 | /* Restore dev->irq to its default pin-assertion irq */ |
| 734 | dev->irq = default_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 738 | static int msi_free_irq(struct pci_dev* dev, int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
| 740 | struct msi_desc *entry; |
| 741 | int head, entry_nr, type; |
| 742 | void __iomem *base; |
| 743 | unsigned long flags; |
| 744 | |
Eric W. Biederman | 3b7d192 | 2006-10-04 02:16:59 -0700 | [diff] [blame] | 745 | arch_teardown_msi_irq(irq); |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | spin_lock_irqsave(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 748 | entry = msi_desc[irq]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if (!entry || entry->dev != dev) { |
| 750 | spin_unlock_irqrestore(&msi_lock, flags); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | type = entry->msi_attrib.type; |
| 754 | entry_nr = entry->msi_attrib.entry_nr; |
| 755 | head = entry->link.head; |
| 756 | base = entry->mask_base; |
| 757 | msi_desc[entry->link.head]->link.tail = entry->link.tail; |
| 758 | msi_desc[entry->link.tail]->link.head = entry->link.head; |
| 759 | entry->dev = NULL; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 760 | msi_desc[irq] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | spin_unlock_irqrestore(&msi_lock, flags); |
| 762 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 763 | destroy_msi_irq(irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | if (type == PCI_CAP_ID_MSIX) { |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 766 | writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE + |
| 767 | PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 769 | if (head == irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | iounmap(base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | /** |
| 777 | * pci_enable_msix - configure device's MSI-X capability structure |
| 778 | * @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] | 779 | * @entries: pointer to an array of MSI-X entries |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 780 | * @nvec: number of MSI-X irqs requested for allocation by device driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * |
| 782 | * 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] | 783 | * of requested irqs upon its software driver call to request for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | * MSI-X mode enabled on its hardware device function. A return of zero |
| 785 | * indicates the successful configuration of MSI-X capability structure |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 786 | * 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] | 787 | * 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] | 788 | * of irqs available. Driver should use the returned value to re-send |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | * its request. |
| 790 | **/ |
| 791 | int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) |
| 792 | { |
Eric W. Biederman | 92db6d1 | 2006-10-04 02:16:35 -0700 | [diff] [blame] | 793 | int status, pos, nr_entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | int i, j, temp; |
| 795 | u16 control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 797 | if (!entries || pci_msi_supported(dev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 800 | status = msi_init(); |
| 801 | if (status < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | return status; |
| 803 | |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 804 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 805 | if (!pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | return -EINVAL; |
| 807 | |
| 808 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | nr_entries = multi_msix_capable(control); |
| 810 | if (nvec > nr_entries) |
| 811 | return -EINVAL; |
| 812 | |
| 813 | /* Check for any invalid entries */ |
| 814 | for (i = 0; i < nvec; i++) { |
| 815 | if (entries[i].entry >= nr_entries) |
| 816 | return -EINVAL; /* invalid entry */ |
| 817 | for (j = i + 1; j < nvec; j++) { |
| 818 | if (entries[i].entry == entries[j].entry) |
| 819 | return -EINVAL; /* duplicate entry */ |
| 820 | } |
| 821 | } |
| 822 | temp = dev->irq; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 823 | WARN_ON(!msi_lookup_irq(dev, PCI_CAP_ID_MSIX)); |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 824 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 825 | /* Check whether driver already requested for MSI irq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (pci_find_capability(dev, PCI_CAP_ID_MSI) > 0 && |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 827 | !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | printk(KERN_INFO "PCI: %s: Can't enable MSI-X. " |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 829 | "Device already has an MSI irq assigned\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | pci_name(dev)); |
| 831 | dev->irq = temp; |
| 832 | return -EINVAL; |
| 833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | status = msix_capability_init(dev, entries, nvec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | return status; |
| 836 | } |
| 837 | |
| 838 | void pci_disable_msix(struct pci_dev* dev) |
| 839 | { |
| 840 | int pos, temp; |
| 841 | u16 control; |
| 842 | |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 843 | if (!pci_msi_enable) |
| 844 | return; |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 845 | if (!dev) |
| 846 | return; |
| 847 | |
| 848 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 849 | if (!pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | return; |
| 851 | |
| 852 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
| 853 | if (!(control & PCI_MSIX_FLAGS_ENABLE)) |
| 854 | return; |
| 855 | |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 856 | disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX); |
| 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | temp = dev->irq; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 859 | if (!msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) { |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 860 | int irq, head, tail = 0, warning = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | unsigned long flags; |
| 862 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 863 | irq = head = dev->irq; |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 864 | dev->irq = temp; /* Restore pin IRQ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | while (head != tail) { |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 866 | spin_lock_irqsave(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 867 | tail = msi_desc[irq]->link.tail; |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 868 | spin_unlock_irqrestore(&msi_lock, flags); |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 869 | if (irq_has_action(irq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | warning = 1; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 871 | else if (irq != head) /* Release MSI-X irq */ |
| 872 | msi_free_irq(dev, irq); |
| 873 | irq = tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 875 | msi_free_irq(dev, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (warning) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without " |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 878 | "free_irq() on all MSI-X irqs\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | pci_name(dev)); |
| 880 | BUG_ON(warning > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /** |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 886 | * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | * @dev: pointer to the pci_dev data structure of MSI(X) device function |
| 888 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 889 | * Being called during hotplug remove, from which the device function |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 890 | * is hot-removed. All previous assigned MSI/MSI-X irqs, if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | * allocated for this device function, are reclaimed to unused state, |
| 892 | * which may be used later on. |
| 893 | **/ |
| 894 | void msi_remove_pci_irq_vectors(struct pci_dev* dev) |
| 895 | { |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 896 | int pos, temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | unsigned long flags; |
| 898 | |
| 899 | if (!pci_msi_enable || !dev) |
| 900 | return; |
| 901 | |
| 902 | temp = dev->irq; /* Save IOAPIC IRQ */ |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 903 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 904 | if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSI)) { |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 905 | if (irq_has_action(dev->irq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() " |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 907 | "called without free_irq() on MSI irq %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | pci_name(dev), dev->irq); |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 909 | BUG_ON(irq_has_action(dev->irq)); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 910 | } else /* Release MSI irq assigned to this device */ |
| 911 | msi_free_irq(dev, dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | dev->irq = temp; /* Restore IOAPIC IRQ */ |
| 913 | } |
Grant Grundler | b64c05e | 2006-01-14 00:34:53 -0700 | [diff] [blame] | 914 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 915 | if (pos > 0 && !msi_lookup_irq(dev, PCI_CAP_ID_MSIX)) { |
| 916 | int irq, head, tail = 0, warning = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | void __iomem *base = NULL; |
| 918 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 919 | irq = head = dev->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | while (head != tail) { |
| 921 | spin_lock_irqsave(&msi_lock, flags); |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 922 | tail = msi_desc[irq]->link.tail; |
| 923 | base = msi_desc[irq]->mask_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | spin_unlock_irqrestore(&msi_lock, flags); |
Eric W. Biederman | 1f80025 | 2006-10-04 02:16:56 -0700 | [diff] [blame] | 925 | if (irq_has_action(irq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | warning = 1; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 927 | else if (irq != head) /* Release MSI-X irq */ |
| 928 | msi_free_irq(dev, irq); |
| 929 | irq = tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 931 | msi_free_irq(dev, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | if (warning) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | iounmap(base); |
| 934 | printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() " |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 935 | "called without free_irq() on all MSI-X irqs\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | pci_name(dev)); |
| 937 | BUG_ON(warning > 0); |
| 938 | } |
| 939 | dev->irq = temp; /* Restore IOAPIC IRQ */ |
| 940 | } |
| 941 | } |
| 942 | |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 943 | void pci_no_msi(void) |
| 944 | { |
| 945 | pci_msi_enable = 0; |
| 946 | } |
| 947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | EXPORT_SYMBOL(pci_enable_msi); |
| 949 | EXPORT_SYMBOL(pci_disable_msi); |
| 950 | EXPORT_SYMBOL(pci_enable_msix); |
| 951 | EXPORT_SYMBOL(pci_disable_msix); |