blob: e3a05cc9a5951b4bd2cbbeb82b02acf3bc942aec [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
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010028/* Arch hooks */
29
30int __attribute__ ((weak))
31arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
32{
33 return 0;
34}
35
36int __attribute__ ((weak))
37arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
38{
39 return 0;
40}
41
42int __attribute__ ((weak))
43arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
44{
45 struct msi_desc *entry;
46 int ret;
47
48 list_for_each_entry(entry, &dev->msi_list, list) {
49 ret = arch_setup_msi_irq(dev, entry);
50 if (ret)
51 return ret;
52 }
53
54 return 0;
55}
56
57void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
58{
59 return;
60}
61
62void __attribute__ ((weak))
63arch_teardown_msi_irqs(struct pci_dev *dev)
64{
65 struct msi_desc *entry;
66
67 list_for_each_entry(entry, &dev->msi_list, list) {
68 if (entry->irq != 0)
69 arch_teardown_msi_irq(entry->irq);
70 }
71}
72
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080073static void msi_set_enable(struct pci_dev *dev, int enable)
74{
75 int pos;
76 u16 control;
77
78 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
79 if (pos) {
80 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
81 control &= ~PCI_MSI_FLAGS_ENABLE;
82 if (enable)
83 control |= PCI_MSI_FLAGS_ENABLE;
84 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
85 }
86}
87
88static void msix_set_enable(struct pci_dev *dev, int enable)
89{
90 int pos;
91 u16 control;
92
93 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
94 if (pos) {
95 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
96 control &= ~PCI_MSIX_FLAGS_ENABLE;
97 if (enable)
98 control |= PCI_MSIX_FLAGS_ENABLE;
99 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
100 }
101}
102
Mitch Williams988cbb12007-03-30 11:54:08 -0700103static void msix_flush_writes(unsigned int irq)
104{
105 struct msi_desc *entry;
106
107 entry = get_irq_msi(irq);
108 BUG_ON(!entry || !entry->dev);
109 switch (entry->msi_attrib.type) {
110 case PCI_CAP_ID_MSI:
111 /* nothing to do */
112 break;
113 case PCI_CAP_ID_MSIX:
114 {
115 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
116 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
117 readl(entry->mask_base + offset);
118 break;
119 }
120 default:
121 BUG();
122 break;
123 }
124}
125
Yinghai Lu8e149e02008-04-23 14:56:30 -0700126static void msi_set_mask_bits(unsigned int irq, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct msi_desc *entry;
129
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700130 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700131 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 switch (entry->msi_attrib.type) {
133 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700134 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900135 int pos;
136 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Eric W. Biederman277bc332006-10-04 02:16:57 -0700138 pos = (long)entry->mask_base;
139 pci_read_config_dword(entry->dev, pos, &mask_bits);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700140 mask_bits &= ~(mask);
141 mask_bits |= flag & mask;
Eric W. Biederman277bc332006-10-04 02:16:57 -0700142 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -0800143 } else {
144 msi_set_enable(entry->dev, !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case PCI_CAP_ID_MSIX:
148 {
149 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
150 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
151 writel(flag, entry->mask_base + offset);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -0600152 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 }
155 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700156 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700159 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700162void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700163{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700164 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700165 switch(entry->msi_attrib.type) {
166 case PCI_CAP_ID_MSI:
167 {
168 struct pci_dev *dev = entry->dev;
169 int pos = entry->msi_attrib.pos;
170 u16 data;
171
172 pci_read_config_dword(dev, msi_lower_address_reg(pos),
173 &msg->address_lo);
174 if (entry->msi_attrib.is_64) {
175 pci_read_config_dword(dev, msi_upper_address_reg(pos),
176 &msg->address_hi);
177 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
178 } else {
179 msg->address_hi = 0;
Roland Dreiercbf5d9e2007-10-03 11:15:11 -0700180 pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700181 }
182 msg->data = data;
183 break;
184 }
185 case PCI_CAP_ID_MSIX:
186 {
187 void __iomem *base;
188 base = entry->mask_base +
189 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
190
191 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
192 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
193 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
194 break;
195 }
196 default:
197 BUG();
198 }
199}
200
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700201void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700202{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700203 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700204 switch (entry->msi_attrib.type) {
205 case PCI_CAP_ID_MSI:
206 {
207 struct pci_dev *dev = entry->dev;
208 int pos = entry->msi_attrib.pos;
209
210 pci_write_config_dword(dev, msi_lower_address_reg(pos),
211 msg->address_lo);
212 if (entry->msi_attrib.is_64) {
213 pci_write_config_dword(dev, msi_upper_address_reg(pos),
214 msg->address_hi);
215 pci_write_config_word(dev, msi_data_reg(pos, 1),
216 msg->data);
217 } else {
218 pci_write_config_word(dev, msi_data_reg(pos, 0),
219 msg->data);
220 }
221 break;
222 }
223 case PCI_CAP_ID_MSIX:
224 {
225 void __iomem *base;
226 base = entry->mask_base +
227 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
228
229 writel(msg->address_lo,
230 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
231 writel(msg->address_hi,
232 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
233 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
234 break;
235 }
236 default:
237 BUG();
238 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700239 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700240}
241
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700242void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Yinghai Lu8e149e02008-04-23 14:56:30 -0700244 msi_set_mask_bits(irq, 1, 1);
Mitch Williams988cbb12007-03-30 11:54:08 -0700245 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700248void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Yinghai Lu8e149e02008-04-23 14:56:30 -0700250 msi_set_mask_bits(irq, 1, 0);
Mitch Williams988cbb12007-03-30 11:54:08 -0700251 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Michael Ellerman032de8e2007-04-18 19:39:22 +1000254static int msi_free_irqs(struct pci_dev* dev);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static struct msi_desc* alloc_msi_entry(void)
258{
259 struct msi_desc *entry;
260
Michael Ellerman3e916c02007-03-22 21:51:36 +1100261 entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!entry)
263 return NULL;
264
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000265 INIT_LIST_HEAD(&entry->list);
266 entry->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 entry->dev = NULL;
268
269 return entry;
270}
271
David Millerba698ad2007-10-25 01:16:30 -0700272static void pci_intx_for_msi(struct pci_dev *dev, int enable)
273{
274 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
275 pci_intx(dev, enable);
276}
277
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100278static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800279{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700280 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800281 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700282 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800283
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800284 if (!dev->msi_enabled)
285 return;
286
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700287 entry = get_irq_msi(dev->irq);
288 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800289
David Millerba698ad2007-10-25 01:16:30 -0700290 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800291 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700292 write_msi_msg(dev->irq, &entry->msg);
293 if (entry->msi_attrib.maskbit)
Yinghai Lu8e149e02008-04-23 14:56:30 -0700294 msi_set_mask_bits(dev->irq, entry->msi_attrib.maskbits_mask,
295 entry->msi_attrib.masked);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700296
297 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
298 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
299 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
300 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800301 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100302}
303
304static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800305{
Shaohua Li41017f02006-02-08 17:11:38 +0800306 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800307 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700308 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800309
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700310 if (!dev->msix_enabled)
311 return;
312
Shaohua Li41017f02006-02-08 17:11:38 +0800313 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700314 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800315 msix_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800316
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000317 list_for_each_entry(entry, &dev->msi_list, list) {
318 write_msi_msg(entry->irq, &entry->msg);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700319 msi_set_mask_bits(entry->irq, 1, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800320 }
Shaohua Li41017f02006-02-08 17:11:38 +0800321
Michael Ellerman314e77b2007-04-05 17:19:12 +1000322 BUG_ON(list_empty(&dev->msi_list));
323 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000324 pos = entry->msi_attrib.pos;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700325 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
326 control &= ~PCI_MSIX_FLAGS_MASKALL;
327 control |= PCI_MSIX_FLAGS_ENABLE;
328 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800329}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100330
331void pci_restore_msi_state(struct pci_dev *dev)
332{
333 __pci_restore_msi_state(dev);
334 __pci_restore_msix_state(dev);
335}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600336EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/**
339 * msi_capability_init - configure device's MSI capability structure
340 * @dev: pointer to the pci_dev data structure of MSI device function
341 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600342 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700343 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700345 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 **/
347static int msi_capability_init(struct pci_dev *dev)
348{
349 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000350 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 u16 control;
352
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800353 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
356 pci_read_config_word(dev, msi_control_reg(pos), &control);
357 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700358 entry = alloc_msi_entry();
359 if (!entry)
360 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700363 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 entry->msi_attrib.entry_nr = 0;
365 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700366 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700367 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700368 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (is_mask_bit_support(control)) {
370 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
371 is_64bit_address(control));
372 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700373 entry->dev = dev;
374 if (entry->msi_attrib.maskbit) {
375 unsigned int maskbits, temp;
376 /* All MSIs are unmasked by default, Mask them all */
377 pci_read_config_dword(dev,
378 msi_mask_bits_reg(pos, is_64bit_address(control)),
379 &maskbits);
380 temp = (1 << multi_msi_capable(control));
381 temp = ((temp - 1) & ~temp);
382 maskbits |= temp;
383 pci_write_config_dword(dev,
384 msi_mask_bits_reg(pos, is_64bit_address(control)),
385 maskbits);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700386 entry->msi_attrib.maskbits_mask = temp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700387 }
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700388 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Configure MSI capability structure */
Michael Ellerman9c831332007-04-18 19:39:21 +1000391 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000392 if (ret) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000393 msi_free_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000394 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500395 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700398 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800399 msi_set_enable(dev, 1);
400 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Michael Ellerman7fe37302007-04-18 19:39:21 +1000402 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
405
406/**
407 * msix_capability_init - configure device's MSI-X capability
408 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700409 * @entries: pointer to an array of struct msix_entry entries
410 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600412 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700413 * single MSI-X irq. A return of zero indicates the successful setup of
414 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 **/
416static int msix_capability_init(struct pci_dev *dev,
417 struct msix_entry *entries, int nvec)
418{
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000419 struct msi_desc *entry;
Michael Ellerman9c831332007-04-18 19:39:21 +1000420 int pos, i, j, nr_entries, ret;
Grant Grundlera0454b42006-02-16 23:58:29 -0800421 unsigned long phys_addr;
422 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 u16 control;
424 u8 bir;
425 void __iomem *base;
426
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800427 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
430 /* Request & Map MSI-X table region */
431 pci_read_config_word(dev, msi_control_reg(pos), &control);
432 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800433
434 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800436 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
437 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
439 if (base == NULL)
440 return -ENOMEM;
441
442 /* MSI-X Table Initialization */
443 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700444 entry = alloc_msi_entry();
445 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700450 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 entry->msi_attrib.entry_nr = j;
452 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700453 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700454 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700455 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 entry->dev = dev;
457 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700458
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700459 list_add_tail(&entry->list, &dev->msi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000461
462 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
463 if (ret) {
464 int avail = 0;
465 list_for_each_entry(entry, &dev->msi_list, list) {
466 if (entry->irq != 0) {
467 avail++;
Michael Ellerman9c831332007-04-18 19:39:21 +1000468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000470
Michael Ellerman032de8e2007-04-18 19:39:22 +1000471 msi_free_irqs(dev);
472
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700473 /* If we had some success report the number of irqs
474 * we succeeded in setting up.
475 */
Michael Ellerman9c831332007-04-18 19:39:21 +1000476 if (avail == 0)
477 avail = ret;
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700478 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000480
481 i = 0;
482 list_for_each_entry(entry, &dev->msi_list, list) {
483 entries[i].vector = entry->irq;
484 set_irq_msi(entry->irq, entry);
485 i++;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Set MSI-X enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700488 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800489 msix_set_enable(dev, 1);
490 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 return 0;
493}
494
495/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000496 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400497 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000498 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100499 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400500 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200501 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000502 * to determine if MSI/-X are supported for the device. If MSI/-X is
503 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400504 **/
Michael Ellermanc9953a72007-04-05 17:19:08 +1000505static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400506{
507 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000508 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400509
Brice Goglin0306ebf2006-10-05 10:24:31 +0200510 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400511 if (!pci_msi_enable || !dev || dev->no_msi)
512 return -EINVAL;
513
Michael Ellerman314e77b2007-04-05 17:19:12 +1000514 /*
515 * You can't ask to have 0 or less MSIs configured.
516 * a) it's stupid ..
517 * b) the list manipulation code assumes nvec >= 1.
518 */
519 if (nvec < 1)
520 return -ERANGE;
521
Brice Goglin0306ebf2006-10-05 10:24:31 +0200522 /* Any bridge which does NOT route MSI transactions from it's
523 * secondary bus to it's primary bus must set NO_MSI flag on
524 * the secondary pci_bus.
525 * We expect only arch-specific PCI host bus controller driver
526 * or quirks for specific PCI bridges to be setting NO_MSI.
527 */
Brice Goglin24334a12006-08-31 01:55:07 -0400528 for (bus = dev->bus; bus; bus = bus->parent)
529 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
530 return -EINVAL;
531
Michael Ellermanc9953a72007-04-05 17:19:08 +1000532 ret = arch_msi_check_device(dev, nvec, type);
533 if (ret)
534 return ret;
535
Michael Ellermanb1e23032007-03-22 21:51:39 +1100536 if (!pci_find_capability(dev, type))
537 return -EINVAL;
538
Brice Goglin24334a12006-08-31 01:55:07 -0400539 return 0;
540}
541
542/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * pci_enable_msi - configure device's MSI capability structure
544 * @dev: pointer to the pci_dev data structure of MSI device function
545 *
546 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700547 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * MSI mode enabled on its hardware device function. A return of zero
549 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700550 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 **/
552int pci_enable_msi(struct pci_dev* dev)
553{
Michael Ellermanb1e23032007-03-22 21:51:39 +1100554 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Michael Ellermanc9953a72007-04-05 17:19:08 +1000556 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
557 if (status)
558 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700560 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700562 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800563 if (dev->msix_enabled) {
564 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
565 "Device already has MSI-X enabled\n",
566 pci_name(dev));
567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return status;
571}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100572EXPORT_SYMBOL(pci_enable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574void pci_disable_msi(struct pci_dev* dev)
575{
576 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800577 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100579 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700580 return;
581
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800582 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700583 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800584 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700585
Michael Ellerman314e77b2007-04-05 17:19:12 +1000586 BUG_ON(list_empty(&dev->msi_list));
587 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700588 /* Return the the pci reset with msi irqs unmasked */
589 if (entry->msi_attrib.maskbit) {
590 u32 mask = entry->msi_attrib.maskbits_mask;
591 msi_set_mask_bits(dev->irq, mask, ~mask);
592 }
Michael Ellerman314e77b2007-04-05 17:19:12 +1000593 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700596
Michael Ellermane387b9e2007-03-22 21:51:27 +1100597 default_irq = entry->msi_attrib.default_irq;
Michael Ellerman032de8e2007-04-18 19:39:22 +1000598 msi_free_irqs(dev);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100599
600 /* Restore dev->irq to its default pin-assertion irq */
601 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100603EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Michael Ellerman032de8e2007-04-18 19:39:22 +1000605static int msi_free_irqs(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000607 struct msi_desc *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Millerb3b7cc72007-05-11 13:26:44 -0700609 list_for_each_entry(entry, &dev->msi_list, list) {
610 if (entry->irq)
611 BUG_ON(irq_has_action(entry->irq));
612 }
Michael Ellerman7ede9c12007-03-22 21:51:34 +1100613
Michael Ellerman032de8e2007-04-18 19:39:22 +1000614 arch_teardown_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Michael Ellerman032de8e2007-04-18 19:39:22 +1000616 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
617 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000618 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
619 * PCI_MSIX_ENTRY_SIZE
620 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Eric W. Biederman78b76112007-06-01 00:46:33 -0700621
622 if (list_is_last(&entry->list, &dev->msi_list))
623 iounmap(entry->mask_base);
Michael Ellerman032de8e2007-04-18 19:39:22 +1000624 }
625 list_del(&entry->list);
626 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
629 return 0;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/**
633 * pci_enable_msix - configure device's MSI-X capability structure
634 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700635 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700636 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 *
638 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700639 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * MSI-X mode enabled on its hardware device function. A return of zero
641 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700642 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700644 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * its request.
646 **/
647int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
648{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700649 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700650 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Michael Ellermanc9953a72007-04-05 17:19:08 +1000653 if (!entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655
Michael Ellermanc9953a72007-04-05 17:19:08 +1000656 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
657 if (status)
658 return status;
659
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700660 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 nr_entries = multi_msix_capable(control);
663 if (nvec > nr_entries)
664 return -EINVAL;
665
666 /* Check for any invalid entries */
667 for (i = 0; i < nvec; i++) {
668 if (entries[i].entry >= nr_entries)
669 return -EINVAL; /* invalid entry */
670 for (j = i + 1; j < nvec; j++) {
671 if (entries[i].entry == entries[j].entry)
672 return -EINVAL; /* duplicate entry */
673 }
674 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700675 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700676
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700677 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800678 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700680 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EINVAL;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return status;
686}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100687EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100689static void msix_free_all_irqs(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000691 msi_free_irqs(dev);
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100692}
693
694void pci_disable_msix(struct pci_dev* dev)
695{
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100696 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700697 return;
698
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800699 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700700 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800701 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700702
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100703 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100705EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700708 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * @dev: pointer to the pci_dev data structure of MSI(X) device function
710 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600711 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700712 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * allocated for this device function, are reclaimed to unused state,
714 * which may be used later on.
715 **/
716void msi_remove_pci_irq_vectors(struct pci_dev* dev)
717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!pci_msi_enable || !dev)
719 return;
720
Michael Ellerman032de8e2007-04-18 19:39:22 +1000721 if (dev->msi_enabled)
722 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100724 if (dev->msix_enabled)
725 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700728void pci_no_msi(void)
729{
730 pci_msi_enable = 0;
731}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000732
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000733void pci_msi_init_pci_dev(struct pci_dev *dev)
734{
735 INIT_LIST_HEAD(&dev->msi_list);
736}