blob: 30de51fcd68c1e675e4018d24bb247b9d1a427cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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. Biederman1ce03372006-10-04 02:16:41 -07009#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
11#include <linux/irq.h>
12#include <linux/interrupt.h>
13#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pci.h>
16#include <linux/proc_fs.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070017#include <linux/msi.h>
Dan Williams4fdadeb2007-04-26 18:21:38 -070018#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/errno.h>
21#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "pci.h"
24#include "msi.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080028static void msi_set_enable(struct pci_dev *dev, int enable)
29{
30 int pos;
31 u16 control;
32
33 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
34 if (pos) {
35 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
36 control &= ~PCI_MSI_FLAGS_ENABLE;
37 if (enable)
38 control |= PCI_MSI_FLAGS_ENABLE;
39 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
40 }
41}
42
43static void msix_set_enable(struct pci_dev *dev, int enable)
44{
45 int pos;
46 u16 control;
47
48 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
49 if (pos) {
50 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
51 control &= ~PCI_MSIX_FLAGS_ENABLE;
52 if (enable)
53 control |= PCI_MSIX_FLAGS_ENABLE;
54 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
55 }
56}
57
Mitch Williams988cbb12007-03-30 11:54:08 -070058static void msix_flush_writes(unsigned int irq)
59{
60 struct msi_desc *entry;
61
62 entry = get_irq_msi(irq);
63 BUG_ON(!entry || !entry->dev);
64 switch (entry->msi_attrib.type) {
65 case PCI_CAP_ID_MSI:
66 /* nothing to do */
67 break;
68 case PCI_CAP_ID_MSIX:
69 {
70 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
71 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
72 readl(entry->mask_base + offset);
73 break;
74 }
75 default:
76 BUG();
77 break;
78 }
79}
80
Eric W. Biederman1ce03372006-10-04 02:16:41 -070081static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct msi_desc *entry;
84
Eric W. Biederman5b912c12007-01-28 12:52:03 -070085 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -070086 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 switch (entry->msi_attrib.type) {
88 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -070089 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +090090 int pos;
91 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Eric W. Biederman277bc332006-10-04 02:16:57 -070093 pos = (long)entry->mask_base;
94 pci_read_config_dword(entry->dev, pos, &mask_bits);
95 mask_bits &= ~(1);
96 mask_bits |= flag;
97 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -080098 } else {
99 msi_set_enable(entry->dev, !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case PCI_CAP_ID_MSIX:
103 {
104 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
105 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
106 writel(flag, entry->mask_base + offset);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -0600107 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 break;
109 }
110 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700111 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700114 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700117void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700118{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700119 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700120 switch(entry->msi_attrib.type) {
121 case PCI_CAP_ID_MSI:
122 {
123 struct pci_dev *dev = entry->dev;
124 int pos = entry->msi_attrib.pos;
125 u16 data;
126
127 pci_read_config_dword(dev, msi_lower_address_reg(pos),
128 &msg->address_lo);
129 if (entry->msi_attrib.is_64) {
130 pci_read_config_dword(dev, msi_upper_address_reg(pos),
131 &msg->address_hi);
132 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
133 } else {
134 msg->address_hi = 0;
Roland Dreiercbf5d9e2007-10-03 11:15:11 -0700135 pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700136 }
137 msg->data = data;
138 break;
139 }
140 case PCI_CAP_ID_MSIX:
141 {
142 void __iomem *base;
143 base = entry->mask_base +
144 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
145
146 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
147 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
148 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
149 break;
150 }
151 default:
152 BUG();
153 }
154}
155
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700156void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700157{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700158 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700159 switch (entry->msi_attrib.type) {
160 case PCI_CAP_ID_MSI:
161 {
162 struct pci_dev *dev = entry->dev;
163 int pos = entry->msi_attrib.pos;
164
165 pci_write_config_dword(dev, msi_lower_address_reg(pos),
166 msg->address_lo);
167 if (entry->msi_attrib.is_64) {
168 pci_write_config_dword(dev, msi_upper_address_reg(pos),
169 msg->address_hi);
170 pci_write_config_word(dev, msi_data_reg(pos, 1),
171 msg->data);
172 } else {
173 pci_write_config_word(dev, msi_data_reg(pos, 0),
174 msg->data);
175 }
176 break;
177 }
178 case PCI_CAP_ID_MSIX:
179 {
180 void __iomem *base;
181 base = entry->mask_base +
182 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
183
184 writel(msg->address_lo,
185 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
186 writel(msg->address_hi,
187 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
188 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
189 break;
190 }
191 default:
192 BUG();
193 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700194 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700195}
196
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700197void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700199 msi_set_mask_bit(irq, 1);
Mitch Williams988cbb12007-03-30 11:54:08 -0700200 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700203void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700205 msi_set_mask_bit(irq, 0);
Mitch Williams988cbb12007-03-30 11:54:08 -0700206 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Michael Ellerman032de8e2007-04-18 19:39:22 +1000209static int msi_free_irqs(struct pci_dev* dev);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static struct msi_desc* alloc_msi_entry(void)
213{
214 struct msi_desc *entry;
215
Michael Ellerman3e916c02007-03-22 21:51:36 +1100216 entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!entry)
218 return NULL;
219
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000220 INIT_LIST_HEAD(&entry->list);
221 entry->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 entry->dev = NULL;
223
224 return entry;
225}
226
David Millerba698ad2007-10-25 01:16:30 -0700227static void pci_intx_for_msi(struct pci_dev *dev, int enable)
228{
229 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
230 pci_intx(dev, enable);
231}
232
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100233static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800234{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700235 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800236 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700237 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800238
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800239 if (!dev->msi_enabled)
240 return;
241
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700242 entry = get_irq_msi(dev->irq);
243 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800244
David Millerba698ad2007-10-25 01:16:30 -0700245 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800246 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700247 write_msi_msg(dev->irq, &entry->msg);
248 if (entry->msi_attrib.maskbit)
249 msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
250
251 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
252 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
253 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
254 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800255 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100256}
257
258static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800259{
Shaohua Li41017f02006-02-08 17:11:38 +0800260 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800261 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700262 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800263
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700264 if (!dev->msix_enabled)
265 return;
266
Shaohua Li41017f02006-02-08 17:11:38 +0800267 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700268 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800269 msix_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800270
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000271 list_for_each_entry(entry, &dev->msi_list, list) {
272 write_msi_msg(entry->irq, &entry->msg);
273 msi_set_mask_bit(entry->irq, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800274 }
Shaohua Li41017f02006-02-08 17:11:38 +0800275
Michael Ellerman314e77b2007-04-05 17:19:12 +1000276 BUG_ON(list_empty(&dev->msi_list));
277 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000278 pos = entry->msi_attrib.pos;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700279 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
280 control &= ~PCI_MSIX_FLAGS_MASKALL;
281 control |= PCI_MSIX_FLAGS_ENABLE;
282 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800283}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100284
285void pci_restore_msi_state(struct pci_dev *dev)
286{
287 __pci_restore_msi_state(dev);
288 __pci_restore_msix_state(dev);
289}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600290EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/**
293 * msi_capability_init - configure device's MSI capability structure
294 * @dev: pointer to the pci_dev data structure of MSI device function
295 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600296 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700297 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700299 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 **/
301static int msi_capability_init(struct pci_dev *dev)
302{
303 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000304 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u16 control;
306
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800307 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
310 pci_read_config_word(dev, msi_control_reg(pos), &control);
311 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700312 entry = alloc_msi_entry();
313 if (!entry)
314 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700317 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 entry->msi_attrib.entry_nr = 0;
319 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700320 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700321 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700322 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (is_mask_bit_support(control)) {
324 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
325 is_64bit_address(control));
326 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700327 entry->dev = dev;
328 if (entry->msi_attrib.maskbit) {
329 unsigned int maskbits, temp;
330 /* All MSIs are unmasked by default, Mask them all */
331 pci_read_config_dword(dev,
332 msi_mask_bits_reg(pos, is_64bit_address(control)),
333 &maskbits);
334 temp = (1 << multi_msi_capable(control));
335 temp = ((temp - 1) & ~temp);
336 maskbits |= temp;
337 pci_write_config_dword(dev,
338 msi_mask_bits_reg(pos, is_64bit_address(control)),
339 maskbits);
340 }
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700341 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Configure MSI capability structure */
Michael Ellerman9c831332007-04-18 19:39:21 +1000344 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000345 if (ret) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000346 msi_free_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000347 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500348 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700351 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800352 msi_set_enable(dev, 1);
353 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Michael Ellerman7fe37302007-04-18 19:39:21 +1000355 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
359/**
360 * msix_capability_init - configure device's MSI-X capability
361 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700362 * @entries: pointer to an array of struct msix_entry entries
363 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600365 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700366 * single MSI-X irq. A return of zero indicates the successful setup of
367 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 **/
369static int msix_capability_init(struct pci_dev *dev,
370 struct msix_entry *entries, int nvec)
371{
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000372 struct msi_desc *entry;
Michael Ellerman9c831332007-04-18 19:39:21 +1000373 int pos, i, j, nr_entries, ret;
Grant Grundlera0454b42006-02-16 23:58:29 -0800374 unsigned long phys_addr;
375 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 u16 control;
377 u8 bir;
378 void __iomem *base;
379
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800380 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
383 /* Request & Map MSI-X table region */
384 pci_read_config_word(dev, msi_control_reg(pos), &control);
385 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800386
387 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800389 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
390 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
392 if (base == NULL)
393 return -ENOMEM;
394
395 /* MSI-X Table Initialization */
396 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700397 entry = alloc_msi_entry();
398 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700403 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 entry->msi_attrib.entry_nr = j;
405 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700406 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700407 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700408 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 entry->dev = dev;
410 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700411
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700412 list_add_tail(&entry->list, &dev->msi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000414
415 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
416 if (ret) {
417 int avail = 0;
418 list_for_each_entry(entry, &dev->msi_list, list) {
419 if (entry->irq != 0) {
420 avail++;
Michael Ellerman9c831332007-04-18 19:39:21 +1000421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000423
Michael Ellerman032de8e2007-04-18 19:39:22 +1000424 msi_free_irqs(dev);
425
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700426 /* If we had some success report the number of irqs
427 * we succeeded in setting up.
428 */
Michael Ellerman9c831332007-04-18 19:39:21 +1000429 if (avail == 0)
430 avail = ret;
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700431 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000433
434 i = 0;
435 list_for_each_entry(entry, &dev->msi_list, list) {
436 entries[i].vector = entry->irq;
437 set_irq_msi(entry->irq, entry);
438 i++;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Set MSI-X enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700441 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800442 msix_set_enable(dev, 1);
443 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return 0;
446}
447
448/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000449 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400450 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000451 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100452 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400453 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200454 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000455 * to determine if MSI/-X are supported for the device. If MSI/-X is
456 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400457 **/
Michael Ellermanc9953a72007-04-05 17:19:08 +1000458static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400459{
460 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000461 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400462
Brice Goglin0306ebf2006-10-05 10:24:31 +0200463 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400464 if (!pci_msi_enable || !dev || dev->no_msi)
465 return -EINVAL;
466
Michael Ellerman314e77b2007-04-05 17:19:12 +1000467 /*
468 * You can't ask to have 0 or less MSIs configured.
469 * a) it's stupid ..
470 * b) the list manipulation code assumes nvec >= 1.
471 */
472 if (nvec < 1)
473 return -ERANGE;
474
Brice Goglin0306ebf2006-10-05 10:24:31 +0200475 /* Any bridge which does NOT route MSI transactions from it's
476 * secondary bus to it's primary bus must set NO_MSI flag on
477 * the secondary pci_bus.
478 * We expect only arch-specific PCI host bus controller driver
479 * or quirks for specific PCI bridges to be setting NO_MSI.
480 */
Brice Goglin24334a12006-08-31 01:55:07 -0400481 for (bus = dev->bus; bus; bus = bus->parent)
482 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
483 return -EINVAL;
484
Michael Ellermanc9953a72007-04-05 17:19:08 +1000485 ret = arch_msi_check_device(dev, nvec, type);
486 if (ret)
487 return ret;
488
Michael Ellermanb1e23032007-03-22 21:51:39 +1100489 if (!pci_find_capability(dev, type))
490 return -EINVAL;
491
Brice Goglin24334a12006-08-31 01:55:07 -0400492 return 0;
493}
494
495/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * pci_enable_msi - configure device's MSI capability structure
497 * @dev: pointer to the pci_dev data structure of MSI device function
498 *
499 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700500 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * MSI mode enabled on its hardware device function. A return of zero
502 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700503 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 **/
505int pci_enable_msi(struct pci_dev* dev)
506{
Michael Ellermanb1e23032007-03-22 21:51:39 +1100507 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Michael Ellermanc9953a72007-04-05 17:19:08 +1000509 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
510 if (status)
511 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700513 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700515 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800516 if (dev->msix_enabled) {
517 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
518 "Device already has MSI-X enabled\n",
519 pci_name(dev));
520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return status;
524}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100525EXPORT_SYMBOL(pci_enable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527void pci_disable_msi(struct pci_dev* dev)
528{
529 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800530 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100532 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700533 return;
534
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800535 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700536 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800537 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700538
Michael Ellerman314e77b2007-04-05 17:19:12 +1000539 BUG_ON(list_empty(&dev->msi_list));
540 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
541 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return;
543 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700544
Michael Ellermane387b9e2007-03-22 21:51:27 +1100545 default_irq = entry->msi_attrib.default_irq;
Michael Ellerman032de8e2007-04-18 19:39:22 +1000546 msi_free_irqs(dev);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100547
548 /* Restore dev->irq to its default pin-assertion irq */
549 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100551EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Michael Ellerman032de8e2007-04-18 19:39:22 +1000553static int msi_free_irqs(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000555 struct msi_desc *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
David Millerb3b7cc72007-05-11 13:26:44 -0700557 list_for_each_entry(entry, &dev->msi_list, list) {
558 if (entry->irq)
559 BUG_ON(irq_has_action(entry->irq));
560 }
Michael Ellerman7ede9c12007-03-22 21:51:34 +1100561
Michael Ellerman032de8e2007-04-18 19:39:22 +1000562 arch_teardown_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Michael Ellerman032de8e2007-04-18 19:39:22 +1000564 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
565 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000566 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
567 * PCI_MSIX_ENTRY_SIZE
568 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Eric W. Biederman78b76112007-06-01 00:46:33 -0700569
570 if (list_is_last(&entry->list, &dev->msi_list))
571 iounmap(entry->mask_base);
Michael Ellerman032de8e2007-04-18 19:39:22 +1000572 }
573 list_del(&entry->list);
574 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 return 0;
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/**
581 * pci_enable_msix - configure device's MSI-X capability structure
582 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700583 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700584 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700587 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * MSI-X mode enabled on its hardware device function. A return of zero
589 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700590 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700592 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * its request.
594 **/
595int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
596{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700597 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700598 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Michael Ellermanc9953a72007-04-05 17:19:08 +1000601 if (!entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EINVAL;
603
Michael Ellermanc9953a72007-04-05 17:19:08 +1000604 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
605 if (status)
606 return status;
607
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700608 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 nr_entries = multi_msix_capable(control);
611 if (nvec > nr_entries)
612 return -EINVAL;
613
614 /* Check for any invalid entries */
615 for (i = 0; i < nvec; i++) {
616 if (entries[i].entry >= nr_entries)
617 return -EINVAL; /* invalid entry */
618 for (j = i + 1; j < nvec; j++) {
619 if (entries[i].entry == entries[j].entry)
620 return -EINVAL; /* duplicate entry */
621 }
622 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700623 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700624
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700625 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800626 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700628 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -EINVAL;
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return status;
634}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100635EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100637static void msix_free_all_irqs(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000639 msi_free_irqs(dev);
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100640}
641
642void pci_disable_msix(struct pci_dev* dev)
643{
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100644 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700645 return;
646
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800647 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700648 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800649 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700650
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100651 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100653EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700656 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * @dev: pointer to the pci_dev data structure of MSI(X) device function
658 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600659 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700660 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * allocated for this device function, are reclaimed to unused state,
662 * which may be used later on.
663 **/
664void msi_remove_pci_irq_vectors(struct pci_dev* dev)
665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!pci_msi_enable || !dev)
667 return;
668
Michael Ellerman032de8e2007-04-18 19:39:22 +1000669 if (dev->msi_enabled)
670 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100672 if (dev->msix_enabled)
673 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700676void pci_no_msi(void)
677{
678 pci_msi_enable = 0;
679}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000680
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000681void pci_msi_init_pci_dev(struct pci_dev *dev)
682{
683 INIT_LIST_HEAD(&dev->msi_list);
684}
685
Michael Ellermanc9953a72007-04-05 17:19:08 +1000686
687/* Arch hooks */
688
689int __attribute__ ((weak))
690arch_msi_check_device(struct pci_dev* dev, int nvec, int type)
691{
692 return 0;
693}
694
Michael Ellerman9c831332007-04-18 19:39:21 +1000695int __attribute__ ((weak))
696arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
697{
698 return 0;
699}
700
701int __attribute__ ((weak))
702arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
703{
704 struct msi_desc *entry;
705 int ret;
706
707 list_for_each_entry(entry, &dev->msi_list, list) {
708 ret = arch_setup_msi_irq(dev, entry);
709 if (ret)
710 return ret;
711 }
712
713 return 0;
714}
Michael Ellerman032de8e2007-04-18 19:39:22 +1000715
716void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
717{
718 return;
719}
720
721void __attribute__ ((weak))
722arch_teardown_msi_irqs(struct pci_dev *dev)
723{
724 struct msi_desc *entry;
725
726 list_for_each_entry(entry, &dev->msi_list, list) {
727 if (entry->irq != 0)
728 arch_teardown_msi_irq(entry->irq);
729 }
730}