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> |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dan Williams | 4fdadeb | 2007-04-26 18:21:38 -0700 | [diff] [blame] | 19 | #include <linux/smp.h> |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include "pci.h" |
| 25 | #include "msi.h" |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | static int pci_msi_enable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 29 | /* Arch hooks */ |
| 30 | |
Michael Ellerman | 11df1f0 | 2009-01-19 11:31:00 +1100 | [diff] [blame] | 31 | #ifndef arch_msi_check_device |
| 32 | 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] | 33 | { |
| 34 | return 0; |
| 35 | } |
Michael Ellerman | 11df1f0 | 2009-01-19 11:31:00 +1100 | [diff] [blame] | 36 | #endif |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 37 | |
Michael Ellerman | 11df1f0 | 2009-01-19 11:31:00 +1100 | [diff] [blame] | 38 | #ifndef arch_setup_msi_irqs |
Thomas Gleixner | 1525bf0 | 2010-10-06 16:05:35 -0400 | [diff] [blame] | 39 | # define arch_setup_msi_irqs default_setup_msi_irqs |
| 40 | # define HAVE_DEFAULT_MSI_SETUP_IRQS |
| 41 | #endif |
| 42 | |
| 43 | #ifdef HAVE_DEFAULT_MSI_SETUP_IRQS |
| 44 | int default_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 45 | { |
| 46 | struct msi_desc *entry; |
| 47 | int ret; |
| 48 | |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 49 | /* |
| 50 | * If an architecture wants to support multiple MSI, it needs to |
| 51 | * override arch_setup_msi_irqs() |
| 52 | */ |
| 53 | if (type == PCI_CAP_ID_MSI && nvec > 1) |
| 54 | return 1; |
| 55 | |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 56 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 57 | ret = arch_setup_msi_irq(dev, entry); |
Michael Ellerman | b5fbf53 | 2009-02-11 22:27:02 +1100 | [diff] [blame] | 58 | if (ret < 0) |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 59 | return ret; |
Michael Ellerman | b5fbf53 | 2009-02-11 22:27:02 +1100 | [diff] [blame] | 60 | if (ret > 0) |
| 61 | return -ENOSPC; |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
Michael Ellerman | 11df1f0 | 2009-01-19 11:31:00 +1100 | [diff] [blame] | 66 | #endif |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 67 | |
Michael Ellerman | 11df1f0 | 2009-01-19 11:31:00 +1100 | [diff] [blame] | 68 | #ifndef arch_teardown_msi_irqs |
Thomas Gleixner | 1525bf0 | 2010-10-06 16:05:35 -0400 | [diff] [blame] | 69 | # define arch_teardown_msi_irqs default_teardown_msi_irqs |
| 70 | # define HAVE_DEFAULT_MSI_TEARDOWN_IRQS |
| 71 | #endif |
| 72 | |
| 73 | #ifdef HAVE_DEFAULT_MSI_TEARDOWN_IRQS |
| 74 | void default_teardown_msi_irqs(struct pci_dev *dev) |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 75 | { |
| 76 | struct msi_desc *entry; |
| 77 | |
| 78 | list_for_each_entry(entry, &dev->msi_list, list) { |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 79 | int i, nvec; |
| 80 | if (entry->irq == 0) |
| 81 | continue; |
| 82 | nvec = 1 << entry->msi_attrib.multiple; |
| 83 | for (i = 0; i < nvec; i++) |
| 84 | arch_teardown_msi_irq(entry->irq + i); |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 85 | } |
| 86 | } |
Michael Ellerman | 11df1f0 | 2009-01-19 11:31:00 +1100 | [diff] [blame] | 87 | #endif |
Adrian Bunk | 6a9e7f2 | 2007-12-11 23:19:41 +0100 | [diff] [blame] | 88 | |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 89 | #ifndef arch_restore_msi_irqs |
| 90 | # define arch_restore_msi_irqs default_restore_msi_irqs |
| 91 | # define HAVE_DEFAULT_MSI_RESTORE_IRQS |
| 92 | #endif |
| 93 | |
| 94 | #ifdef HAVE_DEFAULT_MSI_RESTORE_IRQS |
| 95 | void default_restore_msi_irqs(struct pci_dev *dev, int irq) |
| 96 | { |
| 97 | struct msi_desc *entry; |
| 98 | |
| 99 | entry = NULL; |
| 100 | if (dev->msix_enabled) { |
| 101 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 102 | if (irq == entry->irq) |
| 103 | break; |
| 104 | } |
| 105 | } else if (dev->msi_enabled) { |
| 106 | entry = irq_get_msi_desc(irq); |
| 107 | } |
| 108 | |
| 109 | if (entry) |
| 110 | write_msi_msg(irq, &entry->msg); |
| 111 | } |
| 112 | #endif |
| 113 | |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 114 | static void msi_set_enable(struct pci_dev *dev, int enable) |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 115 | { |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 116 | u16 control; |
| 117 | |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 118 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 119 | control &= ~PCI_MSI_FLAGS_ENABLE; |
| 120 | if (enable) |
| 121 | control |= PCI_MSI_FLAGS_ENABLE; |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 122 | pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); |
Hidetoshi Seto | 5ca5c02 | 2008-05-19 13:48:17 +0900 | [diff] [blame] | 123 | } |
| 124 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 125 | static void msix_set_enable(struct pci_dev *dev, int enable) |
| 126 | { |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 127 | u16 control; |
| 128 | |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 129 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
| 130 | control &= ~PCI_MSIX_FLAGS_ENABLE; |
| 131 | if (enable) |
| 132 | control |= PCI_MSIX_FLAGS_ENABLE; |
| 133 | pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Matthew Wilcox | bffac3c | 2009-01-21 19:19:19 -0500 | [diff] [blame] | 136 | static inline __attribute_const__ u32 msi_mask(unsigned x) |
| 137 | { |
Matthew Wilcox | 0b49ec37a2 | 2009-02-08 20:27:47 -0700 | [diff] [blame] | 138 | /* Don't shift by >= width of type */ |
| 139 | if (x >= 5) |
| 140 | return 0xffffffff; |
| 141 | return (1 << (1 << x)) - 1; |
Matthew Wilcox | bffac3c | 2009-01-21 19:19:19 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 144 | static inline __attribute_const__ u32 msi_capable_mask(u16 control) |
Mitch Williams | 988cbb1 | 2007-03-30 11:54:08 -0700 | [diff] [blame] | 145 | { |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 146 | return msi_mask((control >> 1) & 7); |
| 147 | } |
Mitch Williams | 988cbb1 | 2007-03-30 11:54:08 -0700 | [diff] [blame] | 148 | |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 149 | static inline __attribute_const__ u32 msi_enabled_mask(u16 control) |
| 150 | { |
| 151 | return msi_mask((control >> 4) & 7); |
Mitch Williams | 988cbb1 | 2007-03-30 11:54:08 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 154 | /* |
| 155 | * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to |
| 156 | * mask all MSI interrupts by clearing the MSI enable bit does not work |
| 157 | * reliably as devices without an INTx disable bit will then generate a |
| 158 | * level IRQ which will never be cleared. |
Matthew Wilcox | ce6fce4 | 2008-07-25 15:42:58 -0600 | [diff] [blame] | 159 | */ |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 160 | static u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 162 | u32 mask_bits = desc->masked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 164 | if (!desc->msi_attrib.maskbit) |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 165 | return 0; |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 166 | |
| 167 | mask_bits &= ~mask; |
| 168 | mask_bits |= flag; |
| 169 | pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits); |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 170 | |
| 171 | return mask_bits; |
| 172 | } |
| 173 | |
| 174 | static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag) |
| 175 | { |
| 176 | desc->masked = __msi_mask_irq(desc, mask, flag); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
| 180 | * This internal function does not flush PCI writes to the device. |
| 181 | * All users must ensure that they read from the device before either |
| 182 | * assuming that the device state is up to date, or returning out of this |
| 183 | * file. This saves a few milliseconds when initialising devices with lots |
| 184 | * of MSI-X interrupts. |
| 185 | */ |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 186 | static u32 __msix_mask_irq(struct msi_desc *desc, u32 flag) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 187 | { |
| 188 | u32 mask_bits = desc->masked; |
| 189 | unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + |
Hidetoshi Seto | 2c21fd4 | 2009-06-23 17:40:04 +0900 | [diff] [blame] | 190 | PCI_MSIX_ENTRY_VECTOR_CTRL; |
Sheng Yang | 8d80528 | 2010-11-11 15:46:55 +0800 | [diff] [blame] | 191 | mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 192 | if (flag) |
| 193 | mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT; |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 194 | writel(mask_bits, desc->mask_base + offset); |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 195 | |
| 196 | return mask_bits; |
| 197 | } |
| 198 | |
| 199 | static void msix_mask_irq(struct msi_desc *desc, u32 flag) |
| 200 | { |
| 201 | desc->masked = __msix_mask_irq(desc, flag); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 204 | #ifdef CONFIG_GENERIC_HARDIRQS |
| 205 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 206 | static void msi_set_mask_bit(struct irq_data *data, u32 flag) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 207 | { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 208 | struct msi_desc *desc = irq_data_get_msi(data); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 209 | |
| 210 | if (desc->msi_attrib.is_msix) { |
| 211 | msix_mask_irq(desc, flag); |
| 212 | readl(desc->mask_base); /* Flush write to device */ |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 213 | } else { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 214 | unsigned offset = data->irq - desc->dev->irq; |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 215 | msi_mask_irq(desc, 1 << offset, flag << offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 219 | void mask_msi_irq(struct irq_data *data) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 220 | { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 221 | msi_set_mask_bit(data, 1); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 224 | void unmask_msi_irq(struct irq_data *data) |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 225 | { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 226 | msi_set_mask_bit(data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 229 | #endif /* CONFIG_GENERIC_HARDIRQS */ |
| 230 | |
Thomas Gleixner | 39431ac | 2010-09-28 19:09:51 +0200 | [diff] [blame] | 231 | void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 232 | { |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 233 | BUG_ON(entry->dev->current_state != PCI_D0); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 234 | |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 235 | if (entry->msi_attrib.is_msix) { |
| 236 | void __iomem *base = entry->mask_base + |
| 237 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 238 | |
| 239 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR); |
| 240 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR); |
| 241 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA); |
| 242 | } else { |
| 243 | struct pci_dev *dev = entry->dev; |
| 244 | int pos = entry->msi_attrib.pos; |
| 245 | u16 data; |
| 246 | |
| 247 | pci_read_config_dword(dev, msi_lower_address_reg(pos), |
| 248 | &msg->address_lo); |
| 249 | if (entry->msi_attrib.is_64) { |
| 250 | pci_read_config_dword(dev, msi_upper_address_reg(pos), |
| 251 | &msg->address_hi); |
| 252 | pci_read_config_word(dev, msi_data_reg(pos, 1), &data); |
| 253 | } else { |
| 254 | msg->address_hi = 0; |
| 255 | pci_read_config_word(dev, msi_data_reg(pos, 0), &data); |
| 256 | } |
| 257 | msg->data = data; |
| 258 | } |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 261 | void read_msi_msg(unsigned int irq, struct msi_msg *msg) |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 262 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 263 | struct msi_desc *entry = irq_get_msi_desc(irq); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 264 | |
Thomas Gleixner | 39431ac | 2010-09-28 19:09:51 +0200 | [diff] [blame] | 265 | __read_msi_msg(entry, msg); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Thomas Gleixner | 39431ac | 2010-09-28 19:09:51 +0200 | [diff] [blame] | 268 | void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 269 | { |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 270 | /* Assert that the cache is valid, assuming that |
| 271 | * valid messages are not all-zeroes. */ |
| 272 | BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo | |
| 273 | entry->msg.data)); |
| 274 | |
| 275 | *msg = entry->msg; |
| 276 | } |
| 277 | |
| 278 | void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) |
| 279 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 280 | struct msi_desc *entry = irq_get_msi_desc(irq); |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 281 | |
Thomas Gleixner | 39431ac | 2010-09-28 19:09:51 +0200 | [diff] [blame] | 282 | __get_cached_msi_msg(entry, msg); |
Ben Hutchings | 30da552 | 2010-07-23 14:56:28 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Thomas Gleixner | 39431ac | 2010-09-28 19:09:51 +0200 | [diff] [blame] | 285 | void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 286 | { |
Ben Hutchings | fcd097f | 2010-06-17 20:16:36 +0100 | [diff] [blame] | 287 | if (entry->dev->current_state != PCI_D0) { |
| 288 | /* Don't touch the hardware now */ |
| 289 | } else if (entry->msi_attrib.is_msix) { |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 290 | void __iomem *base; |
| 291 | base = entry->mask_base + |
| 292 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 293 | |
Hidetoshi Seto | 2c21fd4 | 2009-06-23 17:40:04 +0900 | [diff] [blame] | 294 | writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR); |
| 295 | writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR); |
| 296 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA); |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 297 | } else { |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 298 | struct pci_dev *dev = entry->dev; |
| 299 | int pos = entry->msi_attrib.pos; |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 300 | u16 msgctl; |
| 301 | |
| 302 | pci_read_config_word(dev, msi_control_reg(pos), &msgctl); |
| 303 | msgctl &= ~PCI_MSI_FLAGS_QSIZE; |
| 304 | msgctl |= entry->msi_attrib.multiple << 4; |
| 305 | pci_write_config_word(dev, msi_control_reg(pos), msgctl); |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 306 | |
| 307 | pci_write_config_dword(dev, msi_lower_address_reg(pos), |
| 308 | msg->address_lo); |
| 309 | if (entry->msi_attrib.is_64) { |
| 310 | pci_write_config_dword(dev, msi_upper_address_reg(pos), |
| 311 | msg->address_hi); |
| 312 | pci_write_config_word(dev, msi_data_reg(pos, 1), |
| 313 | msg->data); |
| 314 | } else { |
| 315 | pci_write_config_word(dev, msi_data_reg(pos, 0), |
| 316 | msg->data); |
| 317 | } |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 318 | } |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 319 | entry->msg = *msg; |
Eric W. Biederman | 0366f8f | 2006-10-04 02:16:33 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 322 | void write_msi_msg(unsigned int irq, struct msi_msg *msg) |
| 323 | { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 324 | struct msi_desc *entry = irq_get_msi_desc(irq); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 325 | |
Thomas Gleixner | 39431ac | 2010-09-28 19:09:51 +0200 | [diff] [blame] | 326 | __write_msi_msg(entry, msg); |
Yinghai Lu | 3145e94 | 2008-12-05 18:58:34 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 329 | static void free_msi_irqs(struct pci_dev *dev) |
| 330 | { |
| 331 | struct msi_desc *entry, *tmp; |
| 332 | |
| 333 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 334 | int i, nvec; |
| 335 | if (!entry->irq) |
| 336 | continue; |
| 337 | nvec = 1 << entry->msi_attrib.multiple; |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 338 | #ifdef CONFIG_GENERIC_HARDIRQS |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 339 | for (i = 0; i < nvec; i++) |
| 340 | BUG_ON(irq_has_action(entry->irq + i)); |
Jan Glauber | 9a4da8a | 2012-11-29 13:05:05 +0100 | [diff] [blame] | 341 | #endif |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | arch_teardown_msi_irqs(dev); |
| 345 | |
| 346 | list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) { |
| 347 | if (entry->msi_attrib.is_msix) { |
| 348 | if (list_is_last(&entry->list, &dev->msi_list)) |
| 349 | iounmap(entry->mask_base); |
| 350 | } |
Neil Horman | 424eb39 | 2012-01-03 10:29:54 -0500 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Its possible that we get into this path |
| 354 | * When populate_msi_sysfs fails, which means the entries |
| 355 | * were not registered with sysfs. In that case don't |
| 356 | * unregister them. |
| 357 | */ |
| 358 | if (entry->kobj.parent) { |
| 359 | kobject_del(&entry->kobj); |
| 360 | kobject_put(&entry->kobj); |
| 361 | } |
| 362 | |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 363 | list_del(&entry->list); |
| 364 | kfree(entry); |
| 365 | } |
| 366 | } |
Satoru Takeuchi | c54c187 | 2007-01-18 13:50:05 +0900 | [diff] [blame] | 367 | |
Matthew Wilcox | 379f532 | 2009-03-17 08:54:07 -0400 | [diff] [blame] | 368 | static struct msi_desc *alloc_msi_entry(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Matthew Wilcox | 379f532 | 2009-03-17 08:54:07 -0400 | [diff] [blame] | 370 | struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL); |
| 371 | if (!desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return NULL; |
| 373 | |
Matthew Wilcox | 379f532 | 2009-03-17 08:54:07 -0400 | [diff] [blame] | 374 | INIT_LIST_HEAD(&desc->list); |
| 375 | desc->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Matthew Wilcox | 379f532 | 2009-03-17 08:54:07 -0400 | [diff] [blame] | 377 | return desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 380 | static void pci_intx_for_msi(struct pci_dev *dev, int enable) |
| 381 | { |
| 382 | if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG)) |
| 383 | pci_intx(dev, enable); |
| 384 | } |
| 385 | |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 386 | static void __pci_restore_msi_state(struct pci_dev *dev) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 387 | { |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 388 | int pos; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 389 | u16 control; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 390 | struct msi_desc *entry; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 391 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 392 | if (!dev->msi_enabled) |
| 393 | return; |
| 394 | |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 395 | entry = irq_get_msi_desc(dev->irq); |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 396 | pos = entry->msi_attrib.pos; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 397 | |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 398 | pci_intx_for_msi(dev, 0); |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 399 | msi_set_enable(dev, 0); |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 400 | arch_restore_msi_irqs(dev, dev->irq); |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 401 | |
| 402 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 403 | msi_mask_irq(entry, msi_capable_mask(control), entry->masked); |
Jesse Barnes | abad2ec | 2008-08-07 08:52:37 -0700 | [diff] [blame] | 404 | control &= ~PCI_MSI_FLAGS_QSIZE; |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 405 | control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 406 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static void __pci_restore_msix_state(struct pci_dev *dev) |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 410 | { |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 411 | int pos; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 412 | struct msi_desc *entry; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 413 | u16 control; |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 414 | |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 415 | if (!dev->msix_enabled) |
| 416 | return; |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 417 | BUG_ON(list_empty(&dev->msi_list)); |
Hidetoshi Seto | 9cc8d54 | 2009-08-06 11:32:04 +0900 | [diff] [blame] | 418 | entry = list_first_entry(&dev->msi_list, struct msi_desc, list); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 419 | pos = entry->msi_attrib.pos; |
| 420 | pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control); |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 421 | |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 422 | /* route the table */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 423 | pci_intx_for_msi(dev, 0); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 424 | control |= PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL; |
| 425 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 426 | |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 427 | list_for_each_entry(entry, &dev->msi_list, list) { |
Konrad Rzeszutek Wilk | 76ccc29 | 2011-12-16 17:38:18 -0500 | [diff] [blame] | 428 | arch_restore_msi_irqs(dev, entry->irq); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 429 | msix_mask_irq(entry, entry->masked); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 430 | } |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 431 | |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 432 | control &= ~PCI_MSIX_FLAGS_MASKALL; |
Eric W. Biederman | 392ee1e | 2007-03-08 13:04:57 -0700 | [diff] [blame] | 433 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 434 | } |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 435 | |
| 436 | void pci_restore_msi_state(struct pci_dev *dev) |
| 437 | { |
| 438 | __pci_restore_msi_state(dev); |
| 439 | __pci_restore_msix_state(dev); |
| 440 | } |
Linas Vepstas | 94688cf | 2007-11-07 15:43:59 -0600 | [diff] [blame] | 441 | EXPORT_SYMBOL_GPL(pci_restore_msi_state); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 442 | |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 443 | |
| 444 | #define to_msi_attr(obj) container_of(obj, struct msi_attribute, attr) |
| 445 | #define to_msi_desc(obj) container_of(obj, struct msi_desc, kobj) |
| 446 | |
| 447 | struct msi_attribute { |
| 448 | struct attribute attr; |
| 449 | ssize_t (*show)(struct msi_desc *entry, struct msi_attribute *attr, |
| 450 | char *buf); |
| 451 | ssize_t (*store)(struct msi_desc *entry, struct msi_attribute *attr, |
| 452 | const char *buf, size_t count); |
| 453 | }; |
| 454 | |
| 455 | static ssize_t show_msi_mode(struct msi_desc *entry, struct msi_attribute *atr, |
| 456 | char *buf) |
| 457 | { |
| 458 | return sprintf(buf, "%s\n", entry->msi_attrib.is_msix ? "msix" : "msi"); |
| 459 | } |
| 460 | |
| 461 | static ssize_t msi_irq_attr_show(struct kobject *kobj, |
| 462 | struct attribute *attr, char *buf) |
| 463 | { |
| 464 | struct msi_attribute *attribute = to_msi_attr(attr); |
| 465 | struct msi_desc *entry = to_msi_desc(kobj); |
| 466 | |
| 467 | if (!attribute->show) |
| 468 | return -EIO; |
| 469 | |
| 470 | return attribute->show(entry, attribute, buf); |
| 471 | } |
| 472 | |
| 473 | static const struct sysfs_ops msi_irq_sysfs_ops = { |
| 474 | .show = msi_irq_attr_show, |
| 475 | }; |
| 476 | |
| 477 | static struct msi_attribute mode_attribute = |
| 478 | __ATTR(mode, S_IRUGO, show_msi_mode, NULL); |
| 479 | |
| 480 | |
| 481 | struct attribute *msi_irq_default_attrs[] = { |
| 482 | &mode_attribute.attr, |
| 483 | NULL |
| 484 | }; |
| 485 | |
| 486 | void msi_kobj_release(struct kobject *kobj) |
| 487 | { |
| 488 | struct msi_desc *entry = to_msi_desc(kobj); |
| 489 | |
| 490 | pci_dev_put(entry->dev); |
| 491 | } |
| 492 | |
| 493 | static struct kobj_type msi_irq_ktype = { |
| 494 | .release = msi_kobj_release, |
| 495 | .sysfs_ops = &msi_irq_sysfs_ops, |
| 496 | .default_attrs = msi_irq_default_attrs, |
| 497 | }; |
| 498 | |
| 499 | static int populate_msi_sysfs(struct pci_dev *pdev) |
| 500 | { |
| 501 | struct msi_desc *entry; |
| 502 | struct kobject *kobj; |
| 503 | int ret; |
| 504 | int count = 0; |
| 505 | |
| 506 | pdev->msi_kset = kset_create_and_add("msi_irqs", NULL, &pdev->dev.kobj); |
| 507 | if (!pdev->msi_kset) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | list_for_each_entry(entry, &pdev->msi_list, list) { |
| 511 | kobj = &entry->kobj; |
| 512 | kobj->kset = pdev->msi_kset; |
| 513 | pci_dev_get(pdev); |
| 514 | ret = kobject_init_and_add(kobj, &msi_irq_ktype, NULL, |
| 515 | "%u", entry->irq); |
| 516 | if (ret) |
| 517 | goto out_unroll; |
| 518 | |
| 519 | count++; |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | |
| 524 | out_unroll: |
| 525 | list_for_each_entry(entry, &pdev->msi_list, list) { |
| 526 | if (!count) |
| 527 | break; |
| 528 | kobject_del(&entry->kobj); |
| 529 | kobject_put(&entry->kobj); |
| 530 | count--; |
| 531 | } |
| 532 | return ret; |
| 533 | } |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /** |
| 536 | * msi_capability_init - configure device's MSI capability structure |
| 537 | * @dev: pointer to the pci_dev data structure of MSI device function |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 538 | * @nvec: number of interrupts to allocate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | * |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 540 | * Setup the MSI capability structure of the device with the requested |
| 541 | * number of interrupts. A return value of zero indicates the successful |
| 542 | * setup of an entry with the new MSI irq. A negative return value indicates |
| 543 | * an error, and a positive return value indicates the number of interrupts |
| 544 | * which could have been allocated. |
| 545 | */ |
| 546 | static int msi_capability_init(struct pci_dev *dev, int nvec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
| 548 | struct msi_desc *entry; |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 549 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | u16 control; |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 551 | unsigned mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 553 | msi_set_enable(dev, 0); /* Disable MSI during set up */ |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 554 | |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 555 | pci_read_config_word(dev, msi_control_reg(dev->msi_cap), &control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* MSI Entry Initialization */ |
Matthew Wilcox | 379f532 | 2009-03-17 08:54:07 -0400 | [diff] [blame] | 557 | entry = alloc_msi_entry(dev); |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 558 | if (!entry) |
| 559 | return -ENOMEM; |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 560 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 561 | entry->msi_attrib.is_msix = 0; |
| 562 | entry->msi_attrib.is_64 = is_64bit_address(control); |
| 563 | entry->msi_attrib.entry_nr = 0; |
| 564 | entry->msi_attrib.maskbit = is_mask_bit_support(control); |
| 565 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 566 | entry->msi_attrib.pos = dev->msi_cap; |
Hidetoshi Seto | 0db29af | 2008-12-24 17:27:04 +0900 | [diff] [blame] | 567 | |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 568 | entry->mask_pos = msi_mask_reg(dev->msi_cap, entry->msi_attrib.is_64); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 569 | /* All MSIs are unmasked by default, Mask them all */ |
| 570 | if (entry->msi_attrib.maskbit) |
| 571 | pci_read_config_dword(dev, entry->mask_pos, &entry->masked); |
| 572 | mask = msi_capable_mask(control); |
| 573 | msi_mask_irq(entry, mask, mask); |
| 574 | |
Eric W. Biederman | 0dd11f9 | 2007-06-01 00:46:32 -0700 | [diff] [blame] | 575 | list_add_tail(&entry->list, &dev->msi_list); |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | /* Configure MSI capability structure */ |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 578 | ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI); |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 579 | if (ret) { |
Hidetoshi Seto | 7ba1930 | 2009-06-23 17:39:27 +0900 | [diff] [blame] | 580 | msi_mask_irq(entry, mask, ~mask); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 581 | free_msi_irqs(dev); |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 582 | return ret; |
Mark Maule | fd58e55 | 2006-04-10 21:17:48 -0500 | [diff] [blame] | 583 | } |
Eric W. Biederman | f7feaca | 2007-01-28 12:56:37 -0700 | [diff] [blame] | 584 | |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 585 | ret = populate_msi_sysfs(dev); |
| 586 | if (ret) { |
| 587 | msi_mask_irq(entry, mask, ~mask); |
| 588 | free_msi_irqs(dev); |
| 589 | return ret; |
| 590 | } |
| 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | /* Set MSI enabled bits */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 593 | pci_intx_for_msi(dev, 0); |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 594 | msi_set_enable(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 595 | dev->msi_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Michael Ellerman | 7fe3730 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 597 | dev->irq = entry->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 601 | static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries) |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 602 | { |
Kenji Kaneshige | 4302e0f | 2010-06-17 10:42:44 +0900 | [diff] [blame] | 603 | resource_size_t phys_addr; |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 604 | u32 table_offset; |
| 605 | u8 bir; |
| 606 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 607 | pci_read_config_dword(dev, |
| 608 | msix_table_offset_reg(dev->msix_cap), &table_offset); |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 609 | bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); |
| 610 | table_offset &= ~PCI_MSIX_FLAGS_BIRMASK; |
| 611 | phys_addr = pci_resource_start(dev, bir) + table_offset; |
| 612 | |
| 613 | return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); |
| 614 | } |
| 615 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 616 | static int msix_setup_entries(struct pci_dev *dev, void __iomem *base, |
| 617 | struct msix_entry *entries, int nvec) |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 618 | { |
| 619 | struct msi_desc *entry; |
| 620 | int i; |
| 621 | |
| 622 | for (i = 0; i < nvec; i++) { |
| 623 | entry = alloc_msi_entry(dev); |
| 624 | if (!entry) { |
| 625 | if (!i) |
| 626 | iounmap(base); |
| 627 | else |
| 628 | free_msi_irqs(dev); |
| 629 | /* No enough memory. Don't try again */ |
| 630 | return -ENOMEM; |
| 631 | } |
| 632 | |
| 633 | entry->msi_attrib.is_msix = 1; |
| 634 | entry->msi_attrib.is_64 = 1; |
| 635 | entry->msi_attrib.entry_nr = entries[i].entry; |
| 636 | entry->msi_attrib.default_irq = dev->irq; |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 637 | entry->msi_attrib.pos = dev->msix_cap; |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 638 | entry->mask_base = base; |
| 639 | |
| 640 | list_add_tail(&entry->list, &dev->msi_list); |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 646 | static void msix_program_entries(struct pci_dev *dev, |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 647 | struct msix_entry *entries) |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 648 | { |
| 649 | struct msi_desc *entry; |
| 650 | int i = 0; |
| 651 | |
| 652 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 653 | int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE + |
| 654 | PCI_MSIX_ENTRY_VECTOR_CTRL; |
| 655 | |
| 656 | entries[i].vector = entry->irq; |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 657 | irq_set_msi_desc(entry->irq, entry); |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 658 | entry->masked = readl(entry->mask_base + offset); |
| 659 | msix_mask_irq(entry, 1); |
| 660 | i++; |
| 661 | } |
| 662 | } |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /** |
| 665 | * msix_capability_init - configure device's MSI-X capability |
| 666 | * @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] | 667 | * @entries: pointer to an array of struct msix_entry entries |
| 668 | * @nvec: number of @entries |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 670 | * Setup the MSI-X capability structure of device function with a |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 671 | * single MSI-X irq. A return of zero indicates the successful setup of |
| 672 | * requested MSI-X entries with allocated irqs or non-zero for otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | **/ |
| 674 | static int msix_capability_init(struct pci_dev *dev, |
| 675 | struct msix_entry *entries, int nvec) |
| 676 | { |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 677 | int ret; |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 678 | u16 control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | void __iomem *base; |
| 680 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 681 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 682 | |
| 683 | /* Ensure MSI-X is disabled while it is set up */ |
| 684 | control &= ~PCI_MSIX_FLAGS_ENABLE; |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 685 | pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | /* Request & Map MSI-X table region */ |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 688 | base = msix_map_region(dev, multi_msix_capable(control)); |
Hidetoshi Seto | 5a05a9d | 2009-08-06 11:34:34 +0900 | [diff] [blame] | 689 | if (!base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return -ENOMEM; |
| 691 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 692 | ret = msix_setup_entries(dev, base, entries, nvec); |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 693 | if (ret) |
| 694 | return ret; |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 695 | |
| 696 | ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 697 | if (ret) |
| 698 | goto error; |
Michael Ellerman | 9c83133 | 2007-04-18 19:39:21 +1000 | [diff] [blame] | 699 | |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 700 | /* |
| 701 | * Some devices require MSI-X to be enabled before we can touch the |
| 702 | * MSI-X registers. We need to mask all the vectors to prevent |
| 703 | * interrupts coming in before they're fully set up. |
| 704 | */ |
| 705 | control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE; |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 706 | pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 707 | |
Hidetoshi Seto | 75cb342 | 2009-08-06 11:35:10 +0900 | [diff] [blame] | 708 | msix_program_entries(dev, entries); |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 709 | |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 710 | ret = populate_msi_sysfs(dev); |
| 711 | if (ret) { |
| 712 | ret = 0; |
| 713 | goto error; |
| 714 | } |
| 715 | |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 716 | /* Set MSI-X enabled bits and unmask the function */ |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 717 | pci_intx_for_msi(dev, 0); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 718 | dev->msix_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Matthew Wilcox | f598282 | 2009-06-18 19:15:59 -0700 | [diff] [blame] | 720 | control &= ~PCI_MSIX_FLAGS_MASKALL; |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 721 | pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, control); |
Matthew Wilcox | 8d18101 | 2009-05-08 07:13:33 -0600 | [diff] [blame] | 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | return 0; |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 724 | |
| 725 | error: |
| 726 | if (ret < 0) { |
| 727 | /* |
| 728 | * If we had some success, report the number of irqs |
| 729 | * we succeeded in setting up. |
| 730 | */ |
Hidetoshi Seto | d9d7070 | 2009-08-06 11:35:48 +0900 | [diff] [blame] | 731 | struct msi_desc *entry; |
Hidetoshi Seto | 583871d | 2009-08-06 11:33:39 +0900 | [diff] [blame] | 732 | int avail = 0; |
| 733 | |
| 734 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 735 | if (entry->irq != 0) |
| 736 | avail++; |
| 737 | } |
| 738 | if (avail != 0) |
| 739 | ret = avail; |
| 740 | } |
| 741 | |
| 742 | free_msi_irqs(dev); |
| 743 | |
| 744 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /** |
Michael Ellerman | 17bbc12 | 2007-04-05 17:19:07 +1000 | [diff] [blame] | 748 | * 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] | 749 | * @dev: pointer to the pci_dev data structure of MSI device function |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 750 | * @nvec: how many MSIs have been requested ? |
Michael Ellerman | b1e2303 | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 751 | * @type: are we checking for MSI or MSI-X ? |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 752 | * |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 753 | * Look at global flags, the device itself, and its parent busses |
Michael Ellerman | 17bbc12 | 2007-04-05 17:19:07 +1000 | [diff] [blame] | 754 | * to determine if MSI/-X are supported for the device. If MSI/-X is |
| 755 | * supported return 0, else return an error code. |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 756 | **/ |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 757 | 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] | 758 | { |
| 759 | struct pci_bus *bus; |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 760 | int ret; |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 761 | |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 762 | /* MSI must be globally enabled and supported by the device */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 763 | if (!pci_msi_enable || !dev || dev->no_msi) |
| 764 | return -EINVAL; |
| 765 | |
Michael Ellerman | 314e77b | 2007-04-05 17:19:12 +1000 | [diff] [blame] | 766 | /* |
| 767 | * You can't ask to have 0 or less MSIs configured. |
| 768 | * a) it's stupid .. |
| 769 | * b) the list manipulation code assumes nvec >= 1. |
| 770 | */ |
| 771 | if (nvec < 1) |
| 772 | return -ERANGE; |
| 773 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 774 | /* |
| 775 | * Any bridge which does NOT route MSI transactions from its |
| 776 | * secondary bus to its primary bus must set NO_MSI flag on |
Brice Goglin | 0306ebf | 2006-10-05 10:24:31 +0200 | [diff] [blame] | 777 | * the secondary pci_bus. |
| 778 | * We expect only arch-specific PCI host bus controller driver |
| 779 | * or quirks for specific PCI bridges to be setting NO_MSI. |
| 780 | */ |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 781 | for (bus = dev->bus; bus; bus = bus->parent) |
| 782 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
| 783 | return -EINVAL; |
| 784 | |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 785 | ret = arch_msi_check_device(dev, nvec, type); |
| 786 | if (ret) |
| 787 | return ret; |
| 788 | |
Brice Goglin | 24334a1 | 2006-08-31 01:55:07 -0400 | [diff] [blame] | 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /** |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 793 | * pci_enable_msi_block - configure device's MSI capability structure |
| 794 | * @dev: device to configure |
| 795 | * @nvec: number of interrupts to configure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | * |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 797 | * Allocate IRQs for a device with the MSI capability. |
| 798 | * This function returns a negative errno if an error occurs. If it |
| 799 | * is unable to allocate the number of interrupts requested, it returns |
| 800 | * the number of interrupts it might be able to allocate. If it successfully |
| 801 | * allocates at least the number of interrupts requested, it returns 0 and |
| 802 | * updates the @dev's irq member to the lowest new interrupt number; the |
| 803 | * other interrupt numbers allocated to this device are consecutive. |
| 804 | */ |
| 805 | int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 807 | int status, maxvec; |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 808 | u16 msgctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 810 | if (!dev->msi_cap) |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 811 | return -EINVAL; |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 812 | |
| 813 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl); |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 814 | maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1); |
| 815 | if (nvec > maxvec) |
| 816 | return maxvec; |
| 817 | |
| 818 | status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI); |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 819 | if (status) |
| 820 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 822 | WARN_ON(!!dev->msi_enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 824 | /* Check whether driver already requested MSI-X irqs */ |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 825 | if (dev->msix_enabled) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 826 | dev_info(&dev->dev, "can't enable MSI " |
| 827 | "(MSI-X already enabled)\n"); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 828 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 830 | |
| 831 | status = msi_capability_init(dev, nvec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return status; |
| 833 | } |
Matthew Wilcox | 1c8d7b0 | 2009-03-17 08:54:10 -0400 | [diff] [blame] | 834 | EXPORT_SYMBOL(pci_enable_msi_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Alexander Gordeev | 08261d8 | 2012-11-19 16:02:10 +0100 | [diff] [blame] | 836 | int pci_enable_msi_block_auto(struct pci_dev *dev, unsigned int *maxvec) |
| 837 | { |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 838 | int ret, nvec; |
Alexander Gordeev | 08261d8 | 2012-11-19 16:02:10 +0100 | [diff] [blame] | 839 | u16 msgctl; |
| 840 | |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 841 | if (!dev->msi_cap) |
Alexander Gordeev | 08261d8 | 2012-11-19 16:02:10 +0100 | [diff] [blame] | 842 | return -EINVAL; |
| 843 | |
Gavin Shan | f465136 | 2013-04-04 16:54:32 +0000 | [diff] [blame] | 844 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl); |
Alexander Gordeev | 08261d8 | 2012-11-19 16:02:10 +0100 | [diff] [blame] | 845 | ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1); |
| 846 | |
| 847 | if (maxvec) |
| 848 | *maxvec = ret; |
| 849 | |
| 850 | do { |
| 851 | nvec = ret; |
| 852 | ret = pci_enable_msi_block(dev, nvec); |
| 853 | } while (ret > 0); |
| 854 | |
| 855 | if (ret < 0) |
| 856 | return ret; |
| 857 | return nvec; |
| 858 | } |
| 859 | EXPORT_SYMBOL(pci_enable_msi_block_auto); |
| 860 | |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 861 | void pci_msi_shutdown(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 863 | struct msi_desc *desc; |
| 864 | u32 mask; |
| 865 | u16 ctrl; |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 866 | unsigned pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Michael Ellerman | 128bc5f | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 868 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 869 | return; |
| 870 | |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 871 | BUG_ON(list_empty(&dev->msi_list)); |
| 872 | desc = list_first_entry(&dev->msi_list, struct msi_desc, list); |
| 873 | pos = desc->msi_attrib.pos; |
| 874 | |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 875 | msi_set_enable(dev, 0); |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 876 | pci_intx_for_msi(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 877 | dev->msi_enabled = 0; |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 878 | |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 879 | /* Return the device with MSI unmasked as initial states */ |
Matthew Wilcox | 110828c | 2009-06-16 06:31:45 -0600 | [diff] [blame] | 880 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &ctrl); |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 881 | mask = msi_capable_mask(ctrl); |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 882 | /* Keep cached state to be restored */ |
| 883 | __msi_mask_irq(desc, mask, ~mask); |
Michael Ellerman | e387b9e | 2007-03-22 21:51:27 +1100 | [diff] [blame] | 884 | |
| 885 | /* Restore dev->irq to its default pin-assertion irq */ |
Matthew Wilcox | f2440d9 | 2009-03-17 08:54:09 -0400 | [diff] [blame] | 886 | dev->irq = desc->msi_attrib.default_irq; |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 887 | } |
Matthew Wilcox | 24d2755 | 2009-03-17 08:54:06 -0400 | [diff] [blame] | 888 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 889 | void pci_disable_msi(struct pci_dev *dev) |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 890 | { |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 891 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
| 892 | return; |
| 893 | |
| 894 | pci_msi_shutdown(dev); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 895 | free_msi_irqs(dev); |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 896 | kset_unregister(dev->msi_kset); |
| 897 | dev->msi_kset = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 899 | EXPORT_SYMBOL(pci_disable_msi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /** |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 902 | * pci_msix_table_size - return the number of device's MSI-X table entries |
| 903 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
| 904 | */ |
| 905 | int pci_msix_table_size(struct pci_dev *dev) |
| 906 | { |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 907 | u16 control; |
| 908 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 909 | if (!dev->msix_cap) |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 910 | return 0; |
| 911 | |
Gavin Shan | 520fe9d | 2013-04-04 16:54:33 +0000 | [diff] [blame^] | 912 | pci_read_config_word(dev, msi_control_reg(dev->msix_cap), &control); |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 913 | return multi_msix_capable(control); |
| 914 | } |
| 915 | |
| 916 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | * pci_enable_msix - configure device's MSI-X capability structure |
| 918 | * @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] | 919 | * @entries: pointer to an array of MSI-X entries |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 920 | * @nvec: number of MSI-X irqs requested for allocation by device driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | * |
| 922 | * 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] | 923 | * of requested irqs upon its software driver call to request for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | * MSI-X mode enabled on its hardware device function. A return of zero |
| 925 | * indicates the successful configuration of MSI-X capability structure |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 926 | * 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] | 927 | * Or a return of > 0 indicates that driver request is exceeding the number |
Michael S. Tsirkin | 57fbf52 | 2009-05-07 11:28:41 +0300 | [diff] [blame] | 928 | * of irqs or MSI-X vectors available. Driver should use the returned value to |
| 929 | * re-send its request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | **/ |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 931 | int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | { |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 933 | int status, nr_entries; |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 934 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
Gavin Shan | cdf1fd4 | 2013-04-04 16:54:31 +0000 | [diff] [blame] | 936 | if (!entries || !dev->msix_cap) |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 937 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 939 | status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX); |
| 940 | if (status) |
| 941 | return status; |
| 942 | |
Rafael J. Wysocki | a52e2e3 | 2009-01-24 00:21:14 +0100 | [diff] [blame] | 943 | nr_entries = pci_msix_table_size(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | if (nvec > nr_entries) |
Michael S. Tsirkin | 57fbf52 | 2009-05-07 11:28:41 +0300 | [diff] [blame] | 945 | return nr_entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | /* Check for any invalid entries */ |
| 948 | for (i = 0; i < nvec; i++) { |
| 949 | if (entries[i].entry >= nr_entries) |
| 950 | return -EINVAL; /* invalid entry */ |
| 951 | for (j = i + 1; j < nvec; j++) { |
| 952 | if (entries[i].entry == entries[j].entry) |
| 953 | return -EINVAL; /* duplicate entry */ |
| 954 | } |
| 955 | } |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 956 | WARN_ON(!!dev->msix_enabled); |
Eric W. Biederman | 7bd007e | 2006-10-04 02:16:31 -0700 | [diff] [blame] | 957 | |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 958 | /* Check whether driver already requested for MSI irq */ |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 959 | if (dev->msi_enabled) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 960 | dev_info(&dev->dev, "can't enable MSI-X " |
| 961 | "(MSI IRQ already assigned)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return -EINVAL; |
| 963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | status = msix_capability_init(dev, entries, nvec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | return status; |
| 966 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 967 | EXPORT_SYMBOL(pci_enable_msix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 969 | void pci_msix_shutdown(struct pci_dev *dev) |
Michael Ellerman | fc4afc7 | 2007-03-22 21:51:33 +1100 | [diff] [blame] | 970 | { |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 971 | struct msi_desc *entry; |
| 972 | |
Michael Ellerman | 128bc5f | 2007-03-22 21:51:39 +1100 | [diff] [blame] | 973 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
Eric W. Biederman | ded86d8 | 2007-01-28 12:42:52 -0700 | [diff] [blame] | 974 | return; |
| 975 | |
Hidetoshi Seto | 12abb8b | 2009-06-24 12:08:09 +0900 | [diff] [blame] | 976 | /* Return the device with MSI-X masked as initial states */ |
| 977 | list_for_each_entry(entry, &dev->msi_list, list) { |
| 978 | /* Keep cached states to be restored */ |
| 979 | __msix_mask_irq(entry, 1); |
| 980 | } |
| 981 | |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 982 | msix_set_enable(dev, 0); |
David Miller | ba698ad | 2007-10-25 01:16:30 -0700 | [diff] [blame] | 983 | pci_intx_for_msi(dev, 1); |
Eric W. Biederman | b1cbf4e | 2007-03-05 00:30:10 -0800 | [diff] [blame] | 984 | dev->msix_enabled = 0; |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 985 | } |
Hidetoshi Seto | c901851 | 2009-08-06 11:31:27 +0900 | [diff] [blame] | 986 | |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 987 | void pci_disable_msix(struct pci_dev *dev) |
Yinghai Lu | d52877c | 2008-04-23 14:58:09 -0700 | [diff] [blame] | 988 | { |
| 989 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
| 990 | return; |
| 991 | |
| 992 | pci_msix_shutdown(dev); |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 993 | free_msi_irqs(dev); |
Neil Horman | da8d1c8 | 2011-10-06 14:08:18 -0400 | [diff] [blame] | 994 | kset_unregister(dev->msi_kset); |
| 995 | dev->msi_kset = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | } |
Michael Ellerman | 4cc086f | 2007-03-22 21:51:34 +1100 | [diff] [blame] | 997 | EXPORT_SYMBOL(pci_disable_msix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | /** |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 1000 | * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | * @dev: pointer to the pci_dev data structure of MSI(X) device function |
| 1002 | * |
Steven Cole | eaae4b3 | 2005-05-03 18:38:30 -0600 | [diff] [blame] | 1003 | * Being called during hotplug remove, from which the device function |
Eric W. Biederman | 1ce0337 | 2006-10-04 02:16:41 -0700 | [diff] [blame] | 1004 | * is hot-removed. All previous assigned MSI/MSI-X irqs, if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | * allocated for this device function, are reclaimed to unused state, |
| 1006 | * which may be used later on. |
| 1007 | **/ |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 1008 | void msi_remove_pci_irq_vectors(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (!pci_msi_enable || !dev) |
Hidetoshi Seto | 500559a | 2009-08-10 10:14:15 +0900 | [diff] [blame] | 1011 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Hidetoshi Seto | f56e448 | 2009-08-06 11:32:51 +0900 | [diff] [blame] | 1013 | if (dev->msi_enabled || dev->msix_enabled) |
| 1014 | free_msi_irqs(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 1017 | void pci_no_msi(void) |
| 1018 | { |
| 1019 | pci_msi_enable = 0; |
| 1020 | } |
Michael Ellerman | c9953a7 | 2007-04-05 17:19:08 +1000 | [diff] [blame] | 1021 | |
Andrew Patterson | 07ae95f | 2008-11-10 15:31:05 -0700 | [diff] [blame] | 1022 | /** |
| 1023 | * pci_msi_enabled - is MSI enabled? |
| 1024 | * |
| 1025 | * Returns true if MSI has not been disabled by the command-line option |
| 1026 | * pci=nomsi. |
| 1027 | **/ |
| 1028 | int pci_msi_enabled(void) |
| 1029 | { |
| 1030 | return pci_msi_enable; |
| 1031 | } |
| 1032 | EXPORT_SYMBOL(pci_msi_enabled); |
| 1033 | |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 1034 | void pci_msi_init_pci_dev(struct pci_dev *dev) |
| 1035 | { |
| 1036 | INIT_LIST_HEAD(&dev->msi_list); |
Eric W. Biederman | d5dea7d | 2011-10-17 11:46:06 -0700 | [diff] [blame] | 1037 | |
| 1038 | /* Disable the msi hardware to avoid screaming interrupts |
| 1039 | * during boot. This is the power on reset default so |
| 1040 | * usually this should be a noop. |
| 1041 | */ |
Gavin Shan | e375b56 | 2013-04-04 16:54:30 +0000 | [diff] [blame] | 1042 | dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 1043 | if (dev->msi_cap) |
| 1044 | msi_set_enable(dev, 0); |
| 1045 | |
| 1046 | dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 1047 | if (dev->msix_cap) |
| 1048 | msix_set_enable(dev, 0); |
Michael Ellerman | 4aa9bc9 | 2007-04-05 17:19:10 +1000 | [diff] [blame] | 1049 | } |