blob: 15af618d36e20a0f64c704b67cc14e2c1dc2daf2 [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
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +090073static void __msi_set_enable(struct pci_dev *dev, int pos, int enable)
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080074{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080075 u16 control;
76
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080077 if (pos) {
78 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
79 control &= ~PCI_MSI_FLAGS_ENABLE;
80 if (enable)
81 control |= PCI_MSI_FLAGS_ENABLE;
82 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
83 }
84}
85
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +090086static void msi_set_enable(struct pci_dev *dev, int enable)
87{
88 __msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable);
89}
90
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080091static void msix_set_enable(struct pci_dev *dev, int enable)
92{
93 int pos;
94 u16 control;
95
96 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
97 if (pos) {
98 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
99 control &= ~PCI_MSIX_FLAGS_ENABLE;
100 if (enable)
101 control |= PCI_MSIX_FLAGS_ENABLE;
102 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
103 }
104}
105
Mitch Williams988cbb12007-03-30 11:54:08 -0700106static void msix_flush_writes(unsigned int irq)
107{
108 struct msi_desc *entry;
109
110 entry = get_irq_msi(irq);
111 BUG_ON(!entry || !entry->dev);
112 switch (entry->msi_attrib.type) {
113 case PCI_CAP_ID_MSI:
114 /* nothing to do */
115 break;
116 case PCI_CAP_ID_MSIX:
117 {
118 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
119 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
120 readl(entry->mask_base + offset);
121 break;
122 }
123 default:
124 BUG();
125 break;
126 }
127}
128
Yinghai Lu8e149e02008-04-23 14:56:30 -0700129static void msi_set_mask_bits(unsigned int irq, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct msi_desc *entry;
132
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700133 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700134 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 switch (entry->msi_attrib.type) {
136 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700137 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900138 int pos;
139 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Eric W. Biederman277bc332006-10-04 02:16:57 -0700141 pos = (long)entry->mask_base;
142 pci_read_config_dword(entry->dev, pos, &mask_bits);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700143 mask_bits &= ~(mask);
144 mask_bits |= flag & mask;
Eric W. Biederman277bc332006-10-04 02:16:57 -0700145 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -0800146 } else {
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +0900147 __msi_set_enable(entry->dev, entry->msi_attrib.pos,
148 !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case PCI_CAP_ID_MSIX:
152 {
153 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
154 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
155 writel(flag, entry->mask_base + offset);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -0600156 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158 }
159 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700160 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700163 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700166void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700167{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700168 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700169 switch(entry->msi_attrib.type) {
170 case PCI_CAP_ID_MSI:
171 {
172 struct pci_dev *dev = entry->dev;
173 int pos = entry->msi_attrib.pos;
174 u16 data;
175
176 pci_read_config_dword(dev, msi_lower_address_reg(pos),
177 &msg->address_lo);
178 if (entry->msi_attrib.is_64) {
179 pci_read_config_dword(dev, msi_upper_address_reg(pos),
180 &msg->address_hi);
181 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
182 } else {
183 msg->address_hi = 0;
Roland Dreiercbf5d9e2007-10-03 11:15:11 -0700184 pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700185 }
186 msg->data = data;
187 break;
188 }
189 case PCI_CAP_ID_MSIX:
190 {
191 void __iomem *base;
192 base = entry->mask_base +
193 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
194
195 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
196 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
197 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
198 break;
199 }
200 default:
201 BUG();
202 }
203}
204
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700205void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700206{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700207 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700208 switch (entry->msi_attrib.type) {
209 case PCI_CAP_ID_MSI:
210 {
211 struct pci_dev *dev = entry->dev;
212 int pos = entry->msi_attrib.pos;
213
214 pci_write_config_dword(dev, msi_lower_address_reg(pos),
215 msg->address_lo);
216 if (entry->msi_attrib.is_64) {
217 pci_write_config_dword(dev, msi_upper_address_reg(pos),
218 msg->address_hi);
219 pci_write_config_word(dev, msi_data_reg(pos, 1),
220 msg->data);
221 } else {
222 pci_write_config_word(dev, msi_data_reg(pos, 0),
223 msg->data);
224 }
225 break;
226 }
227 case PCI_CAP_ID_MSIX:
228 {
229 void __iomem *base;
230 base = entry->mask_base +
231 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
232
233 writel(msg->address_lo,
234 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
235 writel(msg->address_hi,
236 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
237 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
238 break;
239 }
240 default:
241 BUG();
242 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700243 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700244}
245
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700246void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Yinghai Lu8e149e02008-04-23 14:56:30 -0700248 msi_set_mask_bits(irq, 1, 1);
Mitch Williams988cbb12007-03-30 11:54:08 -0700249 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700252void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Yinghai Lu8e149e02008-04-23 14:56:30 -0700254 msi_set_mask_bits(irq, 1, 0);
Mitch Williams988cbb12007-03-30 11:54:08 -0700255 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Michael Ellerman032de8e2007-04-18 19:39:22 +1000258static int msi_free_irqs(struct pci_dev* dev);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static struct msi_desc* alloc_msi_entry(void)
262{
263 struct msi_desc *entry;
264
Michael Ellerman3e916c02007-03-22 21:51:36 +1100265 entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!entry)
267 return NULL;
268
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000269 INIT_LIST_HEAD(&entry->list);
270 entry->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 entry->dev = NULL;
272
273 return entry;
274}
275
David Millerba698ad2007-10-25 01:16:30 -0700276static void pci_intx_for_msi(struct pci_dev *dev, int enable)
277{
278 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
279 pci_intx(dev, enable);
280}
281
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100282static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800283{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700284 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800285 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700286 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800287
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800288 if (!dev->msi_enabled)
289 return;
290
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700291 entry = get_irq_msi(dev->irq);
292 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800293
David Millerba698ad2007-10-25 01:16:30 -0700294 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800295 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700296 write_msi_msg(dev->irq, &entry->msg);
297 if (entry->msi_attrib.maskbit)
Yinghai Lu8e149e02008-04-23 14:56:30 -0700298 msi_set_mask_bits(dev->irq, entry->msi_attrib.maskbits_mask,
299 entry->msi_attrib.masked);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700300
301 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
302 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
303 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
304 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800305 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100306}
307
308static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800309{
Shaohua Li41017f02006-02-08 17:11:38 +0800310 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800311 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700312 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800313
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700314 if (!dev->msix_enabled)
315 return;
316
Shaohua Li41017f02006-02-08 17:11:38 +0800317 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700318 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800319 msix_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800320
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000321 list_for_each_entry(entry, &dev->msi_list, list) {
322 write_msi_msg(entry->irq, &entry->msg);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700323 msi_set_mask_bits(entry->irq, 1, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800324 }
Shaohua Li41017f02006-02-08 17:11:38 +0800325
Michael Ellerman314e77b2007-04-05 17:19:12 +1000326 BUG_ON(list_empty(&dev->msi_list));
327 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000328 pos = entry->msi_attrib.pos;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700329 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
330 control &= ~PCI_MSIX_FLAGS_MASKALL;
331 control |= PCI_MSIX_FLAGS_ENABLE;
332 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800333}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100334
335void pci_restore_msi_state(struct pci_dev *dev)
336{
337 __pci_restore_msi_state(dev);
338 __pci_restore_msix_state(dev);
339}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600340EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/**
343 * msi_capability_init - configure device's MSI capability structure
344 * @dev: pointer to the pci_dev data structure of MSI device function
345 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600346 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700347 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700349 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 **/
351static int msi_capability_init(struct pci_dev *dev)
352{
353 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000354 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 u16 control;
356
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800357 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
360 pci_read_config_word(dev, msi_control_reg(pos), &control);
361 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700362 entry = alloc_msi_entry();
363 if (!entry)
364 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700367 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 entry->msi_attrib.entry_nr = 0;
369 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700370 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700371 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700372 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (is_mask_bit_support(control)) {
374 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
375 is_64bit_address(control));
376 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700377 entry->dev = dev;
378 if (entry->msi_attrib.maskbit) {
379 unsigned int maskbits, temp;
380 /* All MSIs are unmasked by default, Mask them all */
381 pci_read_config_dword(dev,
382 msi_mask_bits_reg(pos, is_64bit_address(control)),
383 &maskbits);
384 temp = (1 << multi_msi_capable(control));
385 temp = ((temp - 1) & ~temp);
386 maskbits |= temp;
387 pci_write_config_dword(dev,
388 msi_mask_bits_reg(pos, is_64bit_address(control)),
389 maskbits);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700390 entry->msi_attrib.maskbits_mask = temp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700391 }
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700392 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* Configure MSI capability structure */
Michael Ellerman9c831332007-04-18 19:39:21 +1000395 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000396 if (ret) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000397 msi_free_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000398 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500399 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700402 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800403 msi_set_enable(dev, 1);
404 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Michael Ellerman7fe37302007-04-18 19:39:21 +1000406 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408}
409
410/**
411 * msix_capability_init - configure device's MSI-X capability
412 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700413 * @entries: pointer to an array of struct msix_entry entries
414 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600416 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700417 * single MSI-X irq. A return of zero indicates the successful setup of
418 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 **/
420static int msix_capability_init(struct pci_dev *dev,
421 struct msix_entry *entries, int nvec)
422{
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000423 struct msi_desc *entry;
Michael Ellerman9c831332007-04-18 19:39:21 +1000424 int pos, i, j, nr_entries, ret;
Grant Grundlera0454b42006-02-16 23:58:29 -0800425 unsigned long phys_addr;
426 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 u16 control;
428 u8 bir;
429 void __iomem *base;
430
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800431 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
434 /* Request & Map MSI-X table region */
435 pci_read_config_word(dev, msi_control_reg(pos), &control);
436 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800437
438 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800440 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
441 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
443 if (base == NULL)
444 return -ENOMEM;
445
446 /* MSI-X Table Initialization */
447 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700448 entry = alloc_msi_entry();
449 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700454 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 entry->msi_attrib.entry_nr = j;
456 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700457 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700458 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700459 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 entry->dev = dev;
461 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700462
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700463 list_add_tail(&entry->list, &dev->msi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000465
466 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
467 if (ret) {
468 int avail = 0;
469 list_for_each_entry(entry, &dev->msi_list, list) {
470 if (entry->irq != 0) {
471 avail++;
Michael Ellerman9c831332007-04-18 19:39:21 +1000472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000474
Michael Ellerman032de8e2007-04-18 19:39:22 +1000475 msi_free_irqs(dev);
476
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700477 /* If we had some success report the number of irqs
478 * we succeeded in setting up.
479 */
Michael Ellerman9c831332007-04-18 19:39:21 +1000480 if (avail == 0)
481 avail = ret;
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700482 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000484
485 i = 0;
486 list_for_each_entry(entry, &dev->msi_list, list) {
487 entries[i].vector = entry->irq;
488 set_irq_msi(entry->irq, entry);
489 i++;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Set MSI-X enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700492 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800493 msix_set_enable(dev, 1);
494 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 return 0;
497}
498
499/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000500 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400501 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000502 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100503 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400504 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200505 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000506 * to determine if MSI/-X are supported for the device. If MSI/-X is
507 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400508 **/
Michael Ellermanc9953a72007-04-05 17:19:08 +1000509static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400510{
511 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000512 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400513
Brice Goglin0306ebf2006-10-05 10:24:31 +0200514 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400515 if (!pci_msi_enable || !dev || dev->no_msi)
516 return -EINVAL;
517
Michael Ellerman314e77b2007-04-05 17:19:12 +1000518 /*
519 * You can't ask to have 0 or less MSIs configured.
520 * a) it's stupid ..
521 * b) the list manipulation code assumes nvec >= 1.
522 */
523 if (nvec < 1)
524 return -ERANGE;
525
Brice Goglin0306ebf2006-10-05 10:24:31 +0200526 /* Any bridge which does NOT route MSI transactions from it's
527 * secondary bus to it's primary bus must set NO_MSI flag on
528 * the secondary pci_bus.
529 * We expect only arch-specific PCI host bus controller driver
530 * or quirks for specific PCI bridges to be setting NO_MSI.
531 */
Brice Goglin24334a12006-08-31 01:55:07 -0400532 for (bus = dev->bus; bus; bus = bus->parent)
533 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
534 return -EINVAL;
535
Michael Ellermanc9953a72007-04-05 17:19:08 +1000536 ret = arch_msi_check_device(dev, nvec, type);
537 if (ret)
538 return ret;
539
Michael Ellermanb1e23032007-03-22 21:51:39 +1100540 if (!pci_find_capability(dev, type))
541 return -EINVAL;
542
Brice Goglin24334a12006-08-31 01:55:07 -0400543 return 0;
544}
545
546/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * pci_enable_msi - configure device's MSI capability structure
548 * @dev: pointer to the pci_dev data structure of MSI device function
549 *
550 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700551 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * MSI mode enabled on its hardware device function. A return of zero
553 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700554 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 **/
556int pci_enable_msi(struct pci_dev* dev)
557{
Michael Ellermanb1e23032007-03-22 21:51:39 +1100558 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Michael Ellermanc9953a72007-04-05 17:19:08 +1000560 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
561 if (status)
562 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700564 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700566 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800567 if (dev->msix_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600568 dev_info(&dev->dev, "can't enable MSI "
569 "(MSI-X already enabled)\n");
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return status;
574}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100575EXPORT_SYMBOL(pci_enable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Yinghai Lud52877c2008-04-23 14:58:09 -0700577void pci_msi_shutdown(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct msi_desc *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100581 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700582 return;
583
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800584 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700585 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800586 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700587
Michael Ellerman314e77b2007-04-05 17:19:12 +1000588 BUG_ON(list_empty(&dev->msi_list));
589 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700590 /* Return the the pci reset with msi irqs unmasked */
591 if (entry->msi_attrib.maskbit) {
592 u32 mask = entry->msi_attrib.maskbits_mask;
593 msi_set_mask_bits(dev->irq, mask, ~mask);
594 }
Yinghai Lud52877c2008-04-23 14:58:09 -0700595 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
Michael Ellermane387b9e2007-03-22 21:51:27 +1100597
598 /* Restore dev->irq to its default pin-assertion irq */
Yinghai Lud52877c2008-04-23 14:58:09 -0700599 dev->irq = entry->msi_attrib.default_irq;
600}
601void pci_disable_msi(struct pci_dev* dev)
602{
603 struct msi_desc *entry;
604
605 if (!pci_msi_enable || !dev || !dev->msi_enabled)
606 return;
607
608 pci_msi_shutdown(dev);
609
610 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
611 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
612 return;
613
614 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100616EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Michael Ellerman032de8e2007-04-18 19:39:22 +1000618static int msi_free_irqs(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000620 struct msi_desc *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
David Millerb3b7cc72007-05-11 13:26:44 -0700622 list_for_each_entry(entry, &dev->msi_list, list) {
623 if (entry->irq)
624 BUG_ON(irq_has_action(entry->irq));
625 }
Michael Ellerman7ede9c12007-03-22 21:51:34 +1100626
Michael Ellerman032de8e2007-04-18 19:39:22 +1000627 arch_teardown_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Michael Ellerman032de8e2007-04-18 19:39:22 +1000629 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
630 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000631 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
632 * PCI_MSIX_ENTRY_SIZE
633 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Eric W. Biederman78b76112007-06-01 00:46:33 -0700634
635 if (list_is_last(&entry->list, &dev->msi_list))
636 iounmap(entry->mask_base);
Michael Ellerman032de8e2007-04-18 19:39:22 +1000637 }
638 list_del(&entry->list);
639 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 return 0;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/**
646 * pci_enable_msix - configure device's MSI-X capability structure
647 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700648 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700649 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *
651 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700652 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * MSI-X mode enabled on its hardware device function. A return of zero
654 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700655 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700657 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * its request.
659 **/
660int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
661{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700662 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700663 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Michael Ellermanc9953a72007-04-05 17:19:08 +1000666 if (!entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -EINVAL;
668
Michael Ellermanc9953a72007-04-05 17:19:08 +1000669 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
670 if (status)
671 return status;
672
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700673 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 nr_entries = multi_msix_capable(control);
676 if (nvec > nr_entries)
677 return -EINVAL;
678
679 /* Check for any invalid entries */
680 for (i = 0; i < nvec; i++) {
681 if (entries[i].entry >= nr_entries)
682 return -EINVAL; /* invalid entry */
683 for (j = i + 1; j < nvec; j++) {
684 if (entries[i].entry == entries[j].entry)
685 return -EINVAL; /* duplicate entry */
686 }
687 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700688 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700689
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700690 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800691 if (dev->msi_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600692 dev_info(&dev->dev, "can't enable MSI-X "
693 "(MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return status;
698}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100699EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100701static void msix_free_all_irqs(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000703 msi_free_irqs(dev);
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100704}
705
Yinghai Lud52877c2008-04-23 14:58:09 -0700706void pci_msix_shutdown(struct pci_dev* dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100707{
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100708 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700709 return;
710
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800711 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700712 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800713 dev->msix_enabled = 0;
Yinghai Lud52877c2008-04-23 14:58:09 -0700714}
715void pci_disable_msix(struct pci_dev* dev)
716{
717 if (!pci_msi_enable || !dev || !dev->msix_enabled)
718 return;
719
720 pci_msix_shutdown(dev);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700721
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100722 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100724EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700727 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * @dev: pointer to the pci_dev data structure of MSI(X) device function
729 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600730 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700731 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * allocated for this device function, are reclaimed to unused state,
733 * which may be used later on.
734 **/
735void msi_remove_pci_irq_vectors(struct pci_dev* dev)
736{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!pci_msi_enable || !dev)
738 return;
739
Michael Ellerman032de8e2007-04-18 19:39:22 +1000740 if (dev->msi_enabled)
741 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100743 if (dev->msix_enabled)
744 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700747void pci_no_msi(void)
748{
749 pci_msi_enable = 0;
750}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000751
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000752void pci_msi_init_pci_dev(struct pci_dev *dev)
753{
754 INIT_LIST_HEAD(&dev->msi_list);
755}