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