blob: 435c1958a7b766a3a7a119f58ce16041e8d33464 [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);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -060097 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 }
100 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700101 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
103 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700104 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700107void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700108{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700109 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700110 switch(entry->msi_attrib.type) {
111 case PCI_CAP_ID_MSI:
112 {
113 struct pci_dev *dev = entry->dev;
114 int pos = entry->msi_attrib.pos;
115 u16 data;
116
117 pci_read_config_dword(dev, msi_lower_address_reg(pos),
118 &msg->address_lo);
119 if (entry->msi_attrib.is_64) {
120 pci_read_config_dword(dev, msi_upper_address_reg(pos),
121 &msg->address_hi);
122 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
123 } else {
124 msg->address_hi = 0;
125 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
126 }
127 msg->data = data;
128 break;
129 }
130 case PCI_CAP_ID_MSIX:
131 {
132 void __iomem *base;
133 base = entry->mask_base +
134 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
135
136 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
137 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
138 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
139 break;
140 }
141 default:
142 BUG();
143 }
144}
145
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700146void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700147{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700148 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700149 switch (entry->msi_attrib.type) {
150 case PCI_CAP_ID_MSI:
151 {
152 struct pci_dev *dev = entry->dev;
153 int pos = entry->msi_attrib.pos;
154
155 pci_write_config_dword(dev, msi_lower_address_reg(pos),
156 msg->address_lo);
157 if (entry->msi_attrib.is_64) {
158 pci_write_config_dword(dev, msi_upper_address_reg(pos),
159 msg->address_hi);
160 pci_write_config_word(dev, msi_data_reg(pos, 1),
161 msg->data);
162 } else {
163 pci_write_config_word(dev, msi_data_reg(pos, 0),
164 msg->data);
165 }
166 break;
167 }
168 case PCI_CAP_ID_MSIX:
169 {
170 void __iomem *base;
171 base = entry->mask_base +
172 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
173
174 writel(msg->address_lo,
175 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
176 writel(msg->address_hi,
177 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
178 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
179 break;
180 }
181 default:
182 BUG();
183 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700184 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700185}
186
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700187void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700189 msi_set_mask_bit(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700192void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700194 msi_set_mask_bit(irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700197static int msi_free_irq(struct pci_dev* dev, int irq);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int msi_init(void)
200{
201 static int status = -ENOMEM;
202
203 if (!status)
204 return status;
205
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700206 status = msi_cache_init();
207 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 pci_msi_enable = 0;
209 printk(KERN_WARNING "PCI: MSI cache init failed\n");
210 return status;
211 }
Mark Maulefd58e552006-04-10 21:17:48 -0500212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return status;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static struct msi_desc* alloc_msi_entry(void)
217{
218 struct msi_desc *entry;
219
Pekka J Enberg57181782006-09-27 01:51:03 -0700220 entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!entry)
222 return NULL;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 entry->link.tail = entry->link.head = 0; /* single message */
225 entry->dev = NULL;
226
227 return entry;
228}
229
Shaohua Li41017f02006-02-08 17:11:38 +0800230#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100231static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800232{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700233 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800234 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700235 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800236
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800237 if (!dev->msi_enabled)
238 return;
239
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700240 entry = get_irq_msi(dev->irq);
241 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800242
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800243 pci_intx(dev, 0); /* disable intx */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800244 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700245 write_msi_msg(dev->irq, &entry->msg);
246 if (entry->msi_attrib.maskbit)
247 msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
248
249 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
250 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
251 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
252 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800253 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100254}
255
256static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800257{
Shaohua Li41017f02006-02-08 17:11:38 +0800258 int pos;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700259 int irq, head, tail = 0;
Shaohua Li41017f02006-02-08 17:11:38 +0800260 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700261 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800262
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700263 if (!dev->msix_enabled)
264 return;
265
Shaohua Li41017f02006-02-08 17:11:38 +0800266 /* route the table */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800267 pci_intx(dev, 0); /* disable intx */
268 msix_set_enable(dev, 0);
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700269 irq = head = dev->first_msi_irq;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700270 entry = get_irq_msi(irq);
271 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800272 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700273 entry = get_irq_msi(irq);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700274 write_msi_msg(irq, &entry->msg);
275 msi_set_mask_bit(irq, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800276
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700277 tail = entry->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700278 irq = tail;
Shaohua Li41017f02006-02-08 17:11:38 +0800279 }
Shaohua Li41017f02006-02-08 17:11:38 +0800280
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700281 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
282 control &= ~PCI_MSIX_FLAGS_MASKALL;
283 control |= PCI_MSIX_FLAGS_ENABLE;
284 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800285}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100286
287void pci_restore_msi_state(struct pci_dev *dev)
288{
289 __pci_restore_msi_state(dev);
290 __pci_restore_msix_state(dev);
291}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900292#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/**
295 * msi_capability_init - configure device's MSI capability structure
296 * @dev: pointer to the pci_dev data structure of MSI device function
297 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600298 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700299 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700301 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 **/
303static int msi_capability_init(struct pci_dev *dev)
304{
305 struct msi_desc *entry;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700306 int pos, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u16 control;
308
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800309 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
312 pci_read_config_word(dev, msi_control_reg(pos), &control);
313 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700314 entry = alloc_msi_entry();
315 if (!entry)
316 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700319 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 entry->msi_attrib.entry_nr = 0;
321 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700322 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700323 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700324 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (is_mask_bit_support(control)) {
326 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
327 is_64bit_address(control));
328 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700329 entry->dev = dev;
330 if (entry->msi_attrib.maskbit) {
331 unsigned int maskbits, temp;
332 /* All MSIs are unmasked by default, Mask them all */
333 pci_read_config_dword(dev,
334 msi_mask_bits_reg(pos, is_64bit_address(control)),
335 &maskbits);
336 temp = (1 << multi_msi_capable(control));
337 temp = ((temp - 1) & ~temp);
338 maskbits |= temp;
339 pci_write_config_dword(dev,
340 msi_mask_bits_reg(pos, is_64bit_address(control)),
341 maskbits);
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Configure MSI capability structure */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700344 irq = arch_setup_msi_irq(dev, entry);
345 if (irq < 0) {
346 kmem_cache_free(msi_cachep, entry);
347 return irq;
Mark Maulefd58e552006-04-10 21:17:48 -0500348 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700349 entry->link.head = irq;
350 entry->link.tail = irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700351 dev->first_msi_irq = irq;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700352 set_irq_msi(irq, entry);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* Set MSI enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800355 pci_intx(dev, 0); /* disable intx */
356 msi_set_enable(dev, 1);
357 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700359 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
363/**
364 * msix_capability_init - configure device's MSI-X capability
365 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700366 * @entries: pointer to an array of struct msix_entry entries
367 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600369 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700370 * single MSI-X irq. A return of zero indicates the successful setup of
371 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 **/
373static int msix_capability_init(struct pci_dev *dev,
374 struct msix_entry *entries, int nvec)
375{
376 struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700377 int irq, pos, i, j, nr_entries, temp = 0;
Grant Grundlera0454b42006-02-16 23:58:29 -0800378 unsigned long phys_addr;
379 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 u16 control;
381 u8 bir;
382 void __iomem *base;
383
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800384 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
387 /* Request & Map MSI-X table region */
388 pci_read_config_word(dev, msi_control_reg(pos), &control);
389 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800390
391 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800393 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
394 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
396 if (base == NULL)
397 return -ENOMEM;
398
399 /* MSI-X Table Initialization */
400 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700401 entry = alloc_msi_entry();
402 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700407 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 entry->msi_attrib.entry_nr = j;
409 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700410 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700411 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700412 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 entry->dev = dev;
414 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700415
416 /* Configure MSI-X capability structure */
417 irq = arch_setup_msi_irq(dev, entry);
418 if (irq < 0) {
419 kmem_cache_free(msi_cachep, entry);
420 break;
421 }
422 entries[i].vector = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!head) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700424 entry->link.head = irq;
425 entry->link.tail = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 head = entry;
427 } else {
428 entry->link.head = temp;
429 entry->link.tail = tail->link.tail;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700430 tail->link.tail = irq;
431 head->link.head = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700433 temp = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 tail = entry;
Mark Maulefd58e552006-04-10 21:17:48 -0500435
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700436 set_irq_msi(irq, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 if (i != nvec) {
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700439 int avail = i - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 i--;
441 for (; i >= 0; i--) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700442 irq = (entries + i)->vector;
443 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 (entries + i)->vector = 0;
445 }
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700446 /* If we had some success report the number of irqs
447 * we succeeded in setting up.
448 */
449 if (avail <= 0)
450 avail = -EBUSY;
451 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700453 dev->first_msi_irq = entries[0].vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Set MSI-X enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800455 pci_intx(dev, 0); /* disable intx */
456 msix_set_enable(dev, 1);
457 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 return 0;
460}
461
462/**
Brice Goglin24334a12006-08-31 01:55:07 -0400463 * pci_msi_supported - check whether MSI may be enabled on device
464 * @dev: pointer to the pci_dev data structure of MSI device function
465 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200466 * Look at global flags, the device itself, and its parent busses
467 * to return 0 if MSI are supported for the device.
Brice Goglin24334a12006-08-31 01:55:07 -0400468 **/
469static
470int pci_msi_supported(struct pci_dev * dev)
471{
472 struct pci_bus *bus;
473
Brice Goglin0306ebf2006-10-05 10:24:31 +0200474 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400475 if (!pci_msi_enable || !dev || dev->no_msi)
476 return -EINVAL;
477
Brice Goglin0306ebf2006-10-05 10:24:31 +0200478 /* Any bridge which does NOT route MSI transactions from it's
479 * secondary bus to it's primary bus must set NO_MSI flag on
480 * the secondary pci_bus.
481 * We expect only arch-specific PCI host bus controller driver
482 * or quirks for specific PCI bridges to be setting NO_MSI.
483 */
Brice Goglin24334a12006-08-31 01:55:07 -0400484 for (bus = dev->bus; bus; bus = bus->parent)
485 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
486 return -EINVAL;
487
488 return 0;
489}
490
491/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * pci_enable_msi - configure device's MSI capability structure
493 * @dev: pointer to the pci_dev data structure of MSI device function
494 *
495 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700496 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * MSI mode enabled on its hardware device function. A return of zero
498 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700499 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 **/
501int pci_enable_msi(struct pci_dev* dev)
502{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700503 int pos, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Brice Goglin24334a12006-08-31 01:55:07 -0400505 if (pci_msi_supported(dev) < 0)
506 return -EINVAL;
Michael S. Tsirkin6e325a62006-02-14 18:52:22 +0200507
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700508 status = msi_init();
509 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return status;
511
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700512 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
513 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EINVAL;
515
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700516 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700518 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800519 if (dev->msix_enabled) {
520 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
521 "Device already has MSI-X enabled\n",
522 pci_name(dev));
523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return status;
527}
528
529void pci_disable_msi(struct pci_dev* dev)
530{
531 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800532 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700534 if (!pci_msi_enable)
535 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700536 if (!dev)
537 return;
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700538
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700539 if (!dev->msi_enabled)
540 return;
541
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800542 msi_set_enable(dev, 0);
543 pci_intx(dev, 1); /* enable intx */
544 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700545
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700546 entry = get_irq_msi(dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return;
549 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700550 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700552 "free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700553 pci_name(dev), dev->first_msi_irq);
554 BUG_ON(irq_has_action(dev->first_msi_irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700556 default_irq = entry->msi_attrib.default_irq;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700557 msi_free_irq(dev, dev->first_msi_irq);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700558
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700559 /* Restore dev->irq to its default pin-assertion irq */
560 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700562 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700565static int msi_free_irq(struct pci_dev* dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct msi_desc *entry;
568 int head, entry_nr, type;
569 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700571 entry = get_irq_msi(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!entry || entry->dev != dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -EINVAL;
574 }
575 type = entry->msi_attrib.type;
576 entry_nr = entry->msi_attrib.entry_nr;
577 head = entry->link.head;
578 base = entry->mask_base;
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700579 get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
580 get_irq_msi(entry->link.tail)->link.head = entry->link.head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700582 arch_teardown_msi_irq(irq);
583 kmem_cache_free(msi_cachep, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (type == PCI_CAP_ID_MSIX) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700586 writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
587 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700589 if (head == irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 iounmap(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 return 0;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/**
597 * pci_enable_msix - configure device's MSI-X capability structure
598 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700599 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700600 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700603 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * MSI-X mode enabled on its hardware device function. A return of zero
605 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700606 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700608 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * its request.
610 **/
611int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
612{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700613 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700614 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Brice Goglin24334a12006-08-31 01:55:07 -0400617 if (!entries || pci_msi_supported(dev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -EINVAL;
619
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700620 status = msi_init();
621 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return status;
623
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700624 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
625 if (!pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -EINVAL;
627
628 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 nr_entries = multi_msix_capable(control);
630 if (nvec > nr_entries)
631 return -EINVAL;
632
633 /* Check for any invalid entries */
634 for (i = 0; i < nvec; i++) {
635 if (entries[i].entry >= nr_entries)
636 return -EINVAL; /* invalid entry */
637 for (j = i + 1; j < nvec; j++) {
638 if (entries[i].entry == entries[j].entry)
639 return -EINVAL; /* duplicate entry */
640 }
641 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700642 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700643
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700644 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800645 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700647 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return status;
653}
654
655void pci_disable_msix(struct pci_dev* dev)
656{
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700657 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700659 if (!pci_msi_enable)
660 return;
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700661 if (!dev)
662 return;
663
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700664 if (!dev->msix_enabled)
665 return;
666
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800667 msix_set_enable(dev, 0);
668 pci_intx(dev, 1); /* enable intx */
669 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700670
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700671 irq = head = dev->first_msi_irq;
672 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700673 tail = get_irq_msi(irq)->link.tail;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700674 if (irq_has_action(irq))
675 warning = 1;
676 else if (irq != head) /* Release MSI-X irq */
677 msi_free_irq(dev, irq);
678 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700680 msi_free_irq(dev, irq);
681 if (warning) {
682 printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
683 "free_irq() on all MSI-X irqs\n",
684 pci_name(dev));
685 BUG_ON(warning > 0);
686 }
687 dev->first_msi_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700691 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * @dev: pointer to the pci_dev data structure of MSI(X) device function
693 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600694 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700695 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 * allocated for this device function, are reclaimed to unused state,
697 * which may be used later on.
698 **/
699void msi_remove_pci_irq_vectors(struct pci_dev* dev)
700{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!pci_msi_enable || !dev)
702 return;
703
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700704 if (dev->msi_enabled) {
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700705 if (irq_has_action(dev->first_msi_irq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700707 "called without free_irq() on MSI irq %d\n",
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700708 pci_name(dev), dev->first_msi_irq);
709 BUG_ON(irq_has_action(dev->first_msi_irq));
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700710 } else /* Release MSI irq assigned to this device */
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700711 msi_free_irq(dev, dev->first_msi_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Eric W. Biederman866a8c82007-01-28 12:45:54 -0700713 if (dev->msix_enabled) {
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700714 int irq, head, tail = 0, warning = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 void __iomem *base = NULL;
716
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700717 irq = head = dev->first_msi_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 while (head != tail) {
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700719 tail = get_irq_msi(irq)->link.tail;
720 base = get_irq_msi(irq)->mask_base;
Eric W. Biederman1f800252006-10-04 02:16:56 -0700721 if (irq_has_action(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 warning = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700723 else if (irq != head) /* Release MSI-X irq */
724 msi_free_irq(dev, irq);
725 irq = tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700727 msi_free_irq(dev, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (warning) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 iounmap(base);
730 printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700731 "called without free_irq() on all MSI-X irqs\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 pci_name(dev));
733 BUG_ON(warning > 0);
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736}
737
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700738void pci_no_msi(void)
739{
740 pci_msi_enable = 0;
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743EXPORT_SYMBOL(pci_enable_msi);
744EXPORT_SYMBOL(pci_disable_msi);
745EXPORT_SYMBOL(pci_enable_msix);
746EXPORT_SYMBOL(pci_disable_msix);