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