blob: 26938da8f4380c5fd0bdb1786c71f8adb8418046 [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
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700126static void msi_set_mask_bit(unsigned int irq, int 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);
140 mask_bits &= ~(1);
141 mask_bits |= flag;
142 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{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700244 msi_set_mask_bit(irq, 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{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700250 msi_set_mask_bit(irq, 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)
294 msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
295
296 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
297 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
298 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
299 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800300 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100301}
302
303static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800304{
Shaohua Li41017f02006-02-08 17:11:38 +0800305 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800306 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700307 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800308
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700309 if (!dev->msix_enabled)
310 return;
311
Shaohua Li41017f02006-02-08 17:11:38 +0800312 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700313 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800314 msix_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800315
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000316 list_for_each_entry(entry, &dev->msi_list, list) {
317 write_msi_msg(entry->irq, &entry->msg);
318 msi_set_mask_bit(entry->irq, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800319 }
Shaohua Li41017f02006-02-08 17:11:38 +0800320
Michael Ellerman314e77b2007-04-05 17:19:12 +1000321 BUG_ON(list_empty(&dev->msi_list));
322 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000323 pos = entry->msi_attrib.pos;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700324 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
325 control &= ~PCI_MSIX_FLAGS_MASKALL;
326 control |= PCI_MSIX_FLAGS_ENABLE;
327 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800328}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100329
330void pci_restore_msi_state(struct pci_dev *dev)
331{
332 __pci_restore_msi_state(dev);
333 __pci_restore_msix_state(dev);
334}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600335EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/**
338 * msi_capability_init - configure device's MSI capability structure
339 * @dev: pointer to the pci_dev data structure of MSI device function
340 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600341 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700342 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700344 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 **/
346static int msi_capability_init(struct pci_dev *dev)
347{
348 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000349 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 u16 control;
351
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800352 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
355 pci_read_config_word(dev, msi_control_reg(pos), &control);
356 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700357 entry = alloc_msi_entry();
358 if (!entry)
359 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700362 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 entry->msi_attrib.entry_nr = 0;
364 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700365 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700366 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700367 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (is_mask_bit_support(control)) {
369 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
370 is_64bit_address(control));
371 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700372 entry->dev = dev;
373 if (entry->msi_attrib.maskbit) {
374 unsigned int maskbits, temp;
375 /* All MSIs are unmasked by default, Mask them all */
376 pci_read_config_dword(dev,
377 msi_mask_bits_reg(pos, is_64bit_address(control)),
378 &maskbits);
379 temp = (1 << multi_msi_capable(control));
380 temp = ((temp - 1) & ~temp);
381 maskbits |= temp;
382 pci_write_config_dword(dev,
383 msi_mask_bits_reg(pos, is_64bit_address(control)),
384 maskbits);
385 }
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700386 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Configure MSI capability structure */
Michael Ellerman9c831332007-04-18 19:39:21 +1000389 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000390 if (ret) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000391 msi_free_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000392 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500393 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700396 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800397 msi_set_enable(dev, 1);
398 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Michael Ellerman7fe37302007-04-18 19:39:21 +1000400 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402}
403
404/**
405 * msix_capability_init - configure device's MSI-X capability
406 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700407 * @entries: pointer to an array of struct msix_entry entries
408 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600410 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700411 * single MSI-X irq. A return of zero indicates the successful setup of
412 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 **/
414static int msix_capability_init(struct pci_dev *dev,
415 struct msix_entry *entries, int nvec)
416{
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000417 struct msi_desc *entry;
Michael Ellerman9c831332007-04-18 19:39:21 +1000418 int pos, i, j, nr_entries, ret;
Grant Grundlera0454b42006-02-16 23:58:29 -0800419 unsigned long phys_addr;
420 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 u16 control;
422 u8 bir;
423 void __iomem *base;
424
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800425 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
428 /* Request & Map MSI-X table region */
429 pci_read_config_word(dev, msi_control_reg(pos), &control);
430 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800431
432 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800434 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
435 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
437 if (base == NULL)
438 return -ENOMEM;
439
440 /* MSI-X Table Initialization */
441 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700442 entry = alloc_msi_entry();
443 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700448 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 entry->msi_attrib.entry_nr = j;
450 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700451 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700452 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700453 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 entry->dev = dev;
455 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700456
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700457 list_add_tail(&entry->list, &dev->msi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000459
460 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
461 if (ret) {
462 int avail = 0;
463 list_for_each_entry(entry, &dev->msi_list, list) {
464 if (entry->irq != 0) {
465 avail++;
Michael Ellerman9c831332007-04-18 19:39:21 +1000466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000468
Michael Ellerman032de8e2007-04-18 19:39:22 +1000469 msi_free_irqs(dev);
470
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700471 /* If we had some success report the number of irqs
472 * we succeeded in setting up.
473 */
Michael Ellerman9c831332007-04-18 19:39:21 +1000474 if (avail == 0)
475 avail = ret;
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700476 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000478
479 i = 0;
480 list_for_each_entry(entry, &dev->msi_list, list) {
481 entries[i].vector = entry->irq;
482 set_irq_msi(entry->irq, entry);
483 i++;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Set MSI-X enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700486 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800487 msix_set_enable(dev, 1);
488 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 return 0;
491}
492
493/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000494 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400495 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000496 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100497 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400498 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200499 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000500 * to determine if MSI/-X are supported for the device. If MSI/-X is
501 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400502 **/
Michael Ellermanc9953a72007-04-05 17:19:08 +1000503static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400504{
505 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000506 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400507
Brice Goglin0306ebf2006-10-05 10:24:31 +0200508 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400509 if (!pci_msi_enable || !dev || dev->no_msi)
510 return -EINVAL;
511
Michael Ellerman314e77b2007-04-05 17:19:12 +1000512 /*
513 * You can't ask to have 0 or less MSIs configured.
514 * a) it's stupid ..
515 * b) the list manipulation code assumes nvec >= 1.
516 */
517 if (nvec < 1)
518 return -ERANGE;
519
Brice Goglin0306ebf2006-10-05 10:24:31 +0200520 /* Any bridge which does NOT route MSI transactions from it's
521 * secondary bus to it's primary bus must set NO_MSI flag on
522 * the secondary pci_bus.
523 * We expect only arch-specific PCI host bus controller driver
524 * or quirks for specific PCI bridges to be setting NO_MSI.
525 */
Brice Goglin24334a12006-08-31 01:55:07 -0400526 for (bus = dev->bus; bus; bus = bus->parent)
527 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
528 return -EINVAL;
529
Michael Ellermanc9953a72007-04-05 17:19:08 +1000530 ret = arch_msi_check_device(dev, nvec, type);
531 if (ret)
532 return ret;
533
Michael Ellermanb1e23032007-03-22 21:51:39 +1100534 if (!pci_find_capability(dev, type))
535 return -EINVAL;
536
Brice Goglin24334a12006-08-31 01:55:07 -0400537 return 0;
538}
539
540/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * pci_enable_msi - configure device's MSI capability structure
542 * @dev: pointer to the pci_dev data structure of MSI device function
543 *
544 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700545 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * MSI mode enabled on its hardware device function. A return of zero
547 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700548 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 **/
550int pci_enable_msi(struct pci_dev* dev)
551{
Michael Ellermanb1e23032007-03-22 21:51:39 +1100552 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Michael Ellermanc9953a72007-04-05 17:19:08 +1000554 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
555 if (status)
556 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700558 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700560 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800561 if (dev->msix_enabled) {
562 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
563 "Device already has MSI-X enabled\n",
564 pci_name(dev));
565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return status;
569}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100570EXPORT_SYMBOL(pci_enable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572void pci_disable_msi(struct pci_dev* dev)
573{
574 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800575 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100577 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700578 return;
579
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800580 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700581 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800582 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700583
Michael Ellerman314e77b2007-04-05 17:19:12 +1000584 BUG_ON(list_empty(&dev->msi_list));
585 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
586 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return;
588 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700589
Michael Ellermane387b9e2007-03-22 21:51:27 +1100590 default_irq = entry->msi_attrib.default_irq;
Michael Ellerman032de8e2007-04-18 19:39:22 +1000591 msi_free_irqs(dev);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100592
593 /* Restore dev->irq to its default pin-assertion irq */
594 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100596EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Michael Ellerman032de8e2007-04-18 19:39:22 +1000598static int msi_free_irqs(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000600 struct msi_desc *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
David Millerb3b7cc72007-05-11 13:26:44 -0700602 list_for_each_entry(entry, &dev->msi_list, list) {
603 if (entry->irq)
604 BUG_ON(irq_has_action(entry->irq));
605 }
Michael Ellerman7ede9c12007-03-22 21:51:34 +1100606
Michael Ellerman032de8e2007-04-18 19:39:22 +1000607 arch_teardown_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Michael Ellerman032de8e2007-04-18 19:39:22 +1000609 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
610 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000611 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
612 * PCI_MSIX_ENTRY_SIZE
613 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Eric W. Biederman78b76112007-06-01 00:46:33 -0700614
615 if (list_is_last(&entry->list, &dev->msi_list))
616 iounmap(entry->mask_base);
Michael Ellerman032de8e2007-04-18 19:39:22 +1000617 }
618 list_del(&entry->list);
619 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 return 0;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/**
626 * pci_enable_msix - configure device's MSI-X capability structure
627 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700628 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700629 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700632 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * MSI-X mode enabled on its hardware device function. A return of zero
634 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700635 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700637 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * its request.
639 **/
640int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
641{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700642 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700643 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Michael Ellermanc9953a72007-04-05 17:19:08 +1000646 if (!entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -EINVAL;
648
Michael Ellermanc9953a72007-04-05 17:19:08 +1000649 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
650 if (status)
651 return status;
652
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700653 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 nr_entries = multi_msix_capable(control);
656 if (nvec > nr_entries)
657 return -EINVAL;
658
659 /* Check for any invalid entries */
660 for (i = 0; i < nvec; i++) {
661 if (entries[i].entry >= nr_entries)
662 return -EINVAL; /* invalid entry */
663 for (j = i + 1; j < nvec; j++) {
664 if (entries[i].entry == entries[j].entry)
665 return -EINVAL; /* duplicate entry */
666 }
667 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700668 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700669
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700670 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800671 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700673 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -EINVAL;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return status;
679}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100680EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100682static void msix_free_all_irqs(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000684 msi_free_irqs(dev);
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100685}
686
687void pci_disable_msix(struct pci_dev* dev)
688{
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100689 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700690 return;
691
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800692 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700693 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800694 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700695
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100696 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100698EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700701 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * @dev: pointer to the pci_dev data structure of MSI(X) device function
703 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600704 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700705 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * allocated for this device function, are reclaimed to unused state,
707 * which may be used later on.
708 **/
709void msi_remove_pci_irq_vectors(struct pci_dev* dev)
710{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!pci_msi_enable || !dev)
712 return;
713
Michael Ellerman032de8e2007-04-18 19:39:22 +1000714 if (dev->msi_enabled)
715 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100717 if (dev->msix_enabled)
718 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700721void pci_no_msi(void)
722{
723 pci_msi_enable = 0;
724}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000725
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000726void pci_msi_init_pci_dev(struct pci_dev *dev)
727{
728 INIT_LIST_HEAD(&dev->msi_list);
729}