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