blob: ad33e0159514bd17dd796b52603d8ff0b83b602d [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>
15#include <linux/smp_lock.h>
16#include <linux/pci.h>
17#include <linux/proc_fs.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070018#include <linux/msi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/errno.h>
21#include <asm/io.h>
22#include <asm/smp.h>
23
24#include "pci.h"
25#include "msi.h"
26
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache* msi_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int msi_cache_init(void)
32{
Pekka J Enberg57181782006-09-27 01:51:03 -070033 msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
34 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (!msi_cachep)
36 return -ENOMEM;
37
38 return 0;
39}
40
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080041static void msi_set_enable(struct pci_dev *dev, int enable)
42{
43 int pos;
44 u16 control;
45
46 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
47 if (pos) {
48 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
49 control &= ~PCI_MSI_FLAGS_ENABLE;
50 if (enable)
51 control |= PCI_MSI_FLAGS_ENABLE;
52 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
53 }
54}
55
56static void msix_set_enable(struct pci_dev *dev, int enable)
57{
58 int pos;
59 u16 control;
60
61 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
62 if (pos) {
63 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
64 control &= ~PCI_MSIX_FLAGS_ENABLE;
65 if (enable)
66 control |= PCI_MSIX_FLAGS_ENABLE;
67 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
68 }
69}
70
Eric W. Biederman1ce03372006-10-04 02:16:41 -070071static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct msi_desc *entry;
74
Eric W. Biederman5b912c12007-01-28 12:52:03 -070075 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -070076 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 switch (entry->msi_attrib.type) {
78 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -070079 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +090080 int pos;
81 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Eric W. Biederman277bc332006-10-04 02:16:57 -070083 pos = (long)entry->mask_base;
84 pci_read_config_dword(entry->dev, pos, &mask_bits);
85 mask_bits &= ~(1);
86 mask_bits |= flag;
87 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -080088 } else {
89 msi_set_enable(entry->dev, !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 case PCI_CAP_ID_MSIX:
93 {
94 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
95 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
96 writel(flag, entry->mask_base + offset);
97 break;
98 }
99 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700100 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
102 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700103 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700106void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700107{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700108 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700109 switch(entry->msi_attrib.type) {
110 case PCI_CAP_ID_MSI:
111 {
112 struct pci_dev *dev = entry->dev;
113 int pos = entry->msi_attrib.pos;
114 u16 data;
115
116 pci_read_config_dword(dev, msi_lower_address_reg(pos),
117 &msg->address_lo);
118 if (entry->msi_attrib.is_64) {
119 pci_read_config_dword(dev, msi_upper_address_reg(pos),
120 &msg->address_hi);
121 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
122 } else {
123 msg->address_hi = 0;
124 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
125 }
126 msg->data = data;
127 break;
128 }
129 case PCI_CAP_ID_MSIX:
130 {
131 void __iomem *base;
132 base = entry->mask_base +
133 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
134
135 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
136 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
137 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
138 break;
139 }
140 default:
141 BUG();
142 }
143}
144
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700145void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700146{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700147 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700148 switch (entry->msi_attrib.type) {
149 case PCI_CAP_ID_MSI:
150 {
151 struct pci_dev *dev = entry->dev;
152 int pos = entry->msi_attrib.pos;
153
154 pci_write_config_dword(dev, msi_lower_address_reg(pos),
155 msg->address_lo);
156 if (entry->msi_attrib.is_64) {
157 pci_write_config_dword(dev, msi_upper_address_reg(pos),
158 msg->address_hi);
159 pci_write_config_word(dev, msi_data_reg(pos, 1),
160 msg->data);
161 } else {
162 pci_write_config_word(dev, msi_data_reg(pos, 0),
163 msg->data);
164 }
165 break;
166 }
167 case PCI_CAP_ID_MSIX:
168 {
169 void __iomem *base;
170 base = entry->mask_base +
171 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
172
173 writel(msg->address_lo,
174 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
175 writel(msg->address_hi,
176 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
177 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
178 break;
179 }
180 default:
181 BUG();
182 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700183 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700184}
185
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700186void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700188 msi_set_mask_bit(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700191void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700193 msi_set_mask_bit(irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700196static int msi_free_irq(struct pci_dev* dev, int irq);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int msi_init(void)
199{
200 static int status = -ENOMEM;
201
202 if (!status)
203 return status;
204
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700205 status = msi_cache_init();
206 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 pci_msi_enable = 0;
208 printk(KERN_WARNING "PCI: MSI cache init failed\n");
209 return status;
210 }
Mark Maulefd58e552006-04-10 21:17:48 -0500211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return status;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static struct msi_desc* alloc_msi_entry(void)
216{
217 struct msi_desc *entry;
218
Pekka J Enberg57181782006-09-27 01:51:03 -0700219 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!entry)
221 return NULL;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 entry->link.tail = entry->link.head = 0; /* single message */
224 entry->dev = NULL;
225
226 return entry;
227}
228
Shaohua Li41017f02006-02-08 17:11:38 +0800229#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100230static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800231{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700232 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800233 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700234 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800235
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800236 if (!dev->msi_enabled)
237 return;
238
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700239 entry = get_irq_msi(dev->irq);
240 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800241
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800242 pci_intx(dev, 0); /* disable intx */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800243 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700244 write_msi_msg(dev->irq, &entry->msg);
245 if (entry->msi_attrib.maskbit)
246 msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
247
248 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
249 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
250 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
251 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800252 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100253}
254
255static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800256{
Shaohua Li41017f02006-02-08 17:11:38 +0800257 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700258 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800259 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700260 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800261
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700262 if (!dev->msix_enabled)
263 return;
264
Shaohua Li41017f02006-02-08 17:11:38 +0800265 /* route the table */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800266 pci_intx(dev, 0); /* disable intx */
267 msix_set_enable(dev, 0);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700268 irq = head = dev->first_msi_irq;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700269 entry = get_irq_msi(irq);
270 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800271 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700272 entry = get_irq_msi(irq);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700273 write_msi_msg(irq, &entry->msg);
274 msi_set_mask_bit(irq, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800275
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700276 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700277 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800278 }
Shaohua Li41017f02006-02-08 17:11:38 +0800279
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700280 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
281 control &= ~PCI_MSIX_FLAGS_MASKALL;
282 control |= PCI_MSIX_FLAGS_ENABLE;
283 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800284}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100285
286void pci_restore_msi_state(struct pci_dev *dev)
287{
288 __pci_restore_msi_state(dev);
289 __pci_restore_msix_state(dev);
290}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900291#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/**
294 * msi_capability_init - configure device's MSI capability structure
295 * @dev: pointer to the pci_dev data structure of MSI device function
296 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600297 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700298 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700300 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 **/
302static int msi_capability_init(struct pci_dev *dev)
303{
304 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700305 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 u16 control;
307
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800308 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
311 pci_read_config_word(dev, msi_control_reg(pos), &control);
312 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700313 entry = alloc_msi_entry();
314 if (!entry)
315 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700318 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 entry->msi_attrib.entry_nr = 0;
320 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700321 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700322 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700323 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (is_mask_bit_support(control)) {
325 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
326 is_64bit_address(control));
327 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700328 entry->dev = dev;
329 if (entry->msi_attrib.maskbit) {
330 unsigned int maskbits, temp;
331 /* All MSIs are unmasked by default, Mask them all */
332 pci_read_config_dword(dev,
333 msi_mask_bits_reg(pos, is_64bit_address(control)),
334 &maskbits);
335 temp = (1 << multi_msi_capable(control));
336 temp = ((temp - 1) & ~temp);
337 maskbits |= temp;
338 pci_write_config_dword(dev,
339 msi_mask_bits_reg(pos, is_64bit_address(control)),
340 maskbits);
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Configure MSI capability structure */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700343 irq = arch_setup_msi_irq(dev, entry);
344 if (irq < 0) {
345 kmem_cache_free(msi_cachep, entry);
346 return irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500347 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700348 entry->link.head = irq;
349 entry->link.tail = irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700350 dev->first_msi_irq = irq;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700351 set_irq_msi(irq, entry);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Set MSI enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800354 pci_intx(dev, 0); /* disable intx */
355 msi_set_enable(dev, 1);
356 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700358 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360}
361
362/**
363 * msix_capability_init - configure device's MSI-X capability
364 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700365 * @entries: pointer to an array of struct msix_entry entries
366 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600368 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700369 * single MSI-X irq. A return of zero indicates the successful setup of
370 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 **/
372static int msix_capability_init(struct pci_dev *dev,
373 struct msix_entry *entries, int nvec)
374{
375 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700376 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800377 unsigned long phys_addr;
378 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u16 control;
380 u8 bir;
381 void __iomem *base;
382
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800383 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
386 /* Request & Map MSI-X table region */
387 pci_read_config_word(dev, msi_control_reg(pos), &control);
388 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800389
390 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800392 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
393 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
395 if (base == NULL)
396 return -ENOMEM;
397
398 /* MSI-X Table Initialization */
399 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700400 entry = alloc_msi_entry();
401 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700406 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 entry->msi_attrib.entry_nr = j;
408 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700409 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700410 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700411 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 entry->dev = dev;
413 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700414
415 /* Configure MSI-X capability structure */
416 irq = arch_setup_msi_irq(dev, entry);
417 if (irq < 0) {
418 kmem_cache_free(msi_cachep, entry);
419 break;
420 }
421 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700423 entry->link.head = irq;
424 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 head = entry;
426 } else {
427 entry->link.head = temp;
428 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700429 tail->link.tail = irq;
430 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700432 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 tail = entry;
Mark Maulefd58e552006-04-10 21:17:48 -0500434
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700435 set_irq_msi(irq, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700438 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 i--;
440 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700441 irq = (entries + i)->vector;
442 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 (entries + i)->vector = 0;
444 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700445 /* If we had some success report the number of irqs
446 * we succeeded in setting up.
447 */
448 if (avail <= 0)
449 avail = -EBUSY;
450 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700452 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Set MSI-X enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800454 pci_intx(dev, 0); /* disable intx */
455 msix_set_enable(dev, 1);
456 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return 0;
459}
460
461/**
Brice Goglin24334a12006-08-31 01:55:07 -0400462 * pci_msi_supported - check whether MSI may be enabled on device
463 * @dev: pointer to the pci_dev data structure of MSI device function
464 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200465 * Look at global flags, the device itself, and its parent busses
466 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400467 **/
468static
469int pci_msi_supported(struct pci_dev * dev)
470{
471 struct pci_bus *bus;
472
Brice Goglin0306ebf2006-10-05 10:24:31 +0200473 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400474 if (!pci_msi_enable || !dev || dev->no_msi)
475 return -EINVAL;
476
Brice Goglin0306ebf2006-10-05 10:24:31 +0200477 /* Any bridge which does NOT route MSI transactions from it's
478 * secondary bus to it's primary bus must set NO_MSI flag on
479 * the secondary pci_bus.
480 * We expect only arch-specific PCI host bus controller driver
481 * or quirks for specific PCI bridges to be setting NO_MSI.
482 */
Brice Goglin24334a12006-08-31 01:55:07 -0400483 for (bus = dev->bus; bus; bus = bus->parent)
484 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
485 return -EINVAL;
486
487 return 0;
488}
489
490/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * pci_enable_msi - configure device's MSI capability structure
492 * @dev: pointer to the pci_dev data structure of MSI device function
493 *
494 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700495 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * MSI mode enabled on its hardware device function. A return of zero
497 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700498 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 **/
500int pci_enable_msi(struct pci_dev* dev)
501{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700502 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Brice Goglin24334a12006-08-31 01:55:07 -0400504 if (pci_msi_supported(dev) < 0)
505 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200506
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700507 status = msi_init();
508 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return status;
510
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700511 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
512 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EINVAL;
514
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700515 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700517 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800518 if (dev->msix_enabled) {
519 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
520 "Device already has MSI-X enabled\n",
521 pci_name(dev));
522 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return status;
526}
527
528void pci_disable_msi(struct pci_dev* dev)
529{
530 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800531 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700533 if (!pci_msi_enable)
534 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700535 if (!dev)
536 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700537
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700538 if (!dev->msi_enabled)
539 return;
540
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800541 msi_set_enable(dev, 0);
542 pci_intx(dev, 1); /* enable intx */
543 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700544
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700545 entry = get_irq_msi(dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return;
548 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700549 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700551 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700552 pci_name(dev), dev->first_msi_irq);
553 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700555 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700556 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700557
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700558 /* Restore dev->irq to its default pin-assertion irq */
559 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700561 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700564static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct msi_desc *entry;
567 int head, entry_nr, type;
568 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700570 entry = get_irq_msi(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return -EINVAL;
573 }
574 type = entry->msi_attrib.type;
575 entry_nr = entry->msi_attrib.entry_nr;
576 head = entry->link.head;
577 base = entry->mask_base;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700578 get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
579 get_irq_msi(entry->link.tail)->link.head = entry->link.head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700581 arch_teardown_msi_irq(irq);
582 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700585 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
586 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700588 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 return 0;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/**
596 * pci_enable_msix - configure device's MSI-X capability structure
597 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700598 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700599 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
601 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700602 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * MSI-X mode enabled on its hardware device function. A return of zero
604 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700605 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700607 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * its request.
609 **/
610int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
611{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700612 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700613 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Brice Goglin24334a12006-08-31 01:55:07 -0400616 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EINVAL;
618
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700619 status = msi_init();
620 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return status;
622
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700623 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
624 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EINVAL;
626
627 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 nr_entries = multi_msix_capable(control);
629 if (nvec > nr_entries)
630 return -EINVAL;
631
632 /* Check for any invalid entries */
633 for (i = 0; i < nvec; i++) {
634 if (entries[i].entry >= nr_entries)
635 return -EINVAL; /* invalid entry */
636 for (j = i + 1; j < nvec; j++) {
637 if (entries[i].entry == entries[j].entry)
638 return -EINVAL; /* duplicate entry */
639 }
640 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700641 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700642
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700643 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800644 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700646 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -EINVAL;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return status;
652}
653
654void pci_disable_msix(struct pci_dev* dev)
655{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700656 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700658 if (!pci_msi_enable)
659 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700660 if (!dev)
661 return;
662
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700663 if (!dev->msix_enabled)
664 return;
665
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800666 msix_set_enable(dev, 0);
667 pci_intx(dev, 1); /* enable intx */
668 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700669
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700670 irq = head = dev->first_msi_irq;
671 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700672 tail = get_irq_msi(irq)->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700673 if (irq_has_action(irq))
674 warning = 1;
675 else if (irq != head) /* Release MSI-X irq */
676 msi_free_irq(dev, irq);
677 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700679 msi_free_irq(dev, irq);
680 if (warning) {
681 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
682 "free_irq() on all MSI-X irqs\n",
683 pci_name(dev));
684 BUG_ON(warning > 0);
685 }
686 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700690 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * @dev: pointer to the pci_dev data structure of MSI(X) device function
692 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600693 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700694 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * allocated for this device function, are reclaimed to unused state,
696 * which may be used later on.
697 **/
698void msi_remove_pci_irq_vectors(struct pci_dev* dev)
699{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (!pci_msi_enable || !dev)
701 return;
702
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700703 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700704 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700706 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700707 pci_name(dev), dev->first_msi_irq);
708 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700709 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700710 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700712 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700713 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 void __iomem *base = NULL;
715
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700716 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700718 tail = get_irq_msi(irq)->link.tail;
719 base = get_irq_msi(irq)->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700720 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700722 else if (irq != head) /* Release MSI-X irq */
723 msi_free_irq(dev, irq);
724 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700726 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 iounmap(base);
729 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700730 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pci_name(dev));
732 BUG_ON(warning > 0);
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735}
736
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700737void pci_no_msi(void)
738{
739 pci_msi_enable = 0;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742EXPORT_SYMBOL(pci_enable_msi);
743EXPORT_SYMBOL(pci_disable_msi);
744EXPORT_SYMBOL(pci_enable_msix);
745EXPORT_SYMBOL(pci_disable_msix);