blob: 9e1321d0d5e683d281c9e10fc42c826c38e8f4cf [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080029static void msi_set_enable(struct pci_dev *dev, int enable)
30{
31 int pos;
32 u16 control;
33
34 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
35 if (pos) {
36 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
37 control &= ~PCI_MSI_FLAGS_ENABLE;
38 if (enable)
39 control |= PCI_MSI_FLAGS_ENABLE;
40 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
41 }
42}
43
44static void msix_set_enable(struct pci_dev *dev, int enable)
45{
46 int pos;
47 u16 control;
48
49 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
50 if (pos) {
51 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
52 control &= ~PCI_MSIX_FLAGS_ENABLE;
53 if (enable)
54 control |= PCI_MSIX_FLAGS_ENABLE;
55 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
56 }
57}
58
Mitch Williams988cbb12007-03-30 11:54:08 -070059static void msix_flush_writes(unsigned int irq)
60{
61 struct msi_desc *entry;
62
63 entry = get_irq_msi(irq);
64 BUG_ON(!entry || !entry->dev);
65 switch (entry->msi_attrib.type) {
66 case PCI_CAP_ID_MSI:
67 /* nothing to do */
68 break;
69 case PCI_CAP_ID_MSIX:
70 {
71 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
72 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
73 readl(entry->mask_base + offset);
74 break;
75 }
76 default:
77 BUG();
78 break;
79 }
80}
81
Eric W. Biederman1ce03372006-10-04 02:16:41 -070082static void msi_set_mask_bit(unsigned int irq, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct msi_desc *entry;
85
Eric W. Biederman5b912c12007-01-28 12:52:03 -070086 entry = get_irq_msi(irq);
Eric W. Biederman277bc332006-10-04 02:16:57 -070087 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 switch (entry->msi_attrib.type) {
89 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -070090 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +090091 int pos;
92 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Eric W. Biederman277bc332006-10-04 02:16:57 -070094 pos = (long)entry->mask_base;
95 pci_read_config_dword(entry->dev, pos, &mask_bits);
96 mask_bits &= ~(1);
97 mask_bits |= flag;
98 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -080099 } else {
100 msi_set_enable(entry->dev, !flag);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 case PCI_CAP_ID_MSIX:
104 {
105 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
106 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
107 writel(flag, entry->mask_base + offset);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -0600108 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
110 }
111 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700112 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700115 entry->msi_attrib.masked = !!flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700118void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700119{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700120 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700121 switch(entry->msi_attrib.type) {
122 case PCI_CAP_ID_MSI:
123 {
124 struct pci_dev *dev = entry->dev;
125 int pos = entry->msi_attrib.pos;
126 u16 data;
127
128 pci_read_config_dword(dev, msi_lower_address_reg(pos),
129 &msg->address_lo);
130 if (entry->msi_attrib.is_64) {
131 pci_read_config_dword(dev, msi_upper_address_reg(pos),
132 &msg->address_hi);
133 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
134 } else {
135 msg->address_hi = 0;
136 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
137 }
138 msg->data = data;
139 break;
140 }
141 case PCI_CAP_ID_MSIX:
142 {
143 void __iomem *base;
144 base = entry->mask_base +
145 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
146
147 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
148 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
149 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
150 break;
151 }
152 default:
153 BUG();
154 }
155}
156
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700157void write_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700158{
Eric W. Biederman5b912c12007-01-28 12:52:03 -0700159 struct msi_desc *entry = get_irq_msi(irq);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700160 switch (entry->msi_attrib.type) {
161 case PCI_CAP_ID_MSI:
162 {
163 struct pci_dev *dev = entry->dev;
164 int pos = entry->msi_attrib.pos;
165
166 pci_write_config_dword(dev, msi_lower_address_reg(pos),
167 msg->address_lo);
168 if (entry->msi_attrib.is_64) {
169 pci_write_config_dword(dev, msi_upper_address_reg(pos),
170 msg->address_hi);
171 pci_write_config_word(dev, msi_data_reg(pos, 1),
172 msg->data);
173 } else {
174 pci_write_config_word(dev, msi_data_reg(pos, 0),
175 msg->data);
176 }
177 break;
178 }
179 case PCI_CAP_ID_MSIX:
180 {
181 void __iomem *base;
182 base = entry->mask_base +
183 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
184
185 writel(msg->address_lo,
186 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
187 writel(msg->address_hi,
188 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
189 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
190 break;
191 }
192 default:
193 BUG();
194 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700195 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700196}
197
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700198void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700200 msi_set_mask_bit(irq, 1);
Mitch Williams988cbb12007-03-30 11:54:08 -0700201 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700204void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700206 msi_set_mask_bit(irq, 0);
Mitch Williams988cbb12007-03-30 11:54:08 -0700207 msix_flush_writes(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Michael Ellerman032de8e2007-04-18 19:39:22 +1000210static int msi_free_irqs(struct pci_dev* dev);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static struct msi_desc* alloc_msi_entry(void)
214{
215 struct msi_desc *entry;
216
Michael Ellerman3e916c02007-03-22 21:51:36 +1100217 entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!entry)
219 return NULL;
220
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000221 INIT_LIST_HEAD(&entry->list);
222 entry->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 entry->dev = NULL;
224
225 return entry;
226}
227
Shaohua Li41017f02006-02-08 17:11:38 +0800228#ifdef CONFIG_PM
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100229static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800230{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700231 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800232 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700233 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800234
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800235 if (!dev->msi_enabled)
236 return;
237
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700238 entry = get_irq_msi(dev->irq);
239 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800240
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800241 pci_intx(dev, 0); /* disable intx */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800242 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700243 write_msi_msg(dev->irq, &entry->msg);
244 if (entry->msi_attrib.maskbit)
245 msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
246
247 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
248 control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
249 if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
250 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800251 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100252}
253
254static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800255{
Shaohua Li41017f02006-02-08 17:11:38 +0800256 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800257 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700258 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800259
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700260 if (!dev->msix_enabled)
261 return;
262
Shaohua Li41017f02006-02-08 17:11:38 +0800263 /* route the table */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800264 pci_intx(dev, 0); /* disable intx */
265 msix_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800266
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000267 list_for_each_entry(entry, &dev->msi_list, list) {
268 write_msi_msg(entry->irq, &entry->msg);
269 msi_set_mask_bit(entry->irq, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800270 }
Shaohua Li41017f02006-02-08 17:11:38 +0800271
Michael Ellerman314e77b2007-04-05 17:19:12 +1000272 BUG_ON(list_empty(&dev->msi_list));
273 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000274 pos = entry->msi_attrib.pos;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700275 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
276 control &= ~PCI_MSIX_FLAGS_MASKALL;
277 control |= PCI_MSIX_FLAGS_ENABLE;
278 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800279}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100280
281void pci_restore_msi_state(struct pci_dev *dev)
282{
283 __pci_restore_msi_state(dev);
284 __pci_restore_msix_state(dev);
285}
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900286#endif /* CONFIG_PM */
Shaohua Li41017f02006-02-08 17:11:38 +0800287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/**
289 * msi_capability_init - configure device's MSI capability structure
290 * @dev: pointer to the pci_dev data structure of MSI device function
291 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600292 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700293 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700295 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 **/
297static int msi_capability_init(struct pci_dev *dev)
298{
299 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000300 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 u16 control;
302
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800303 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
306 pci_read_config_word(dev, msi_control_reg(pos), &control);
307 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700308 entry = alloc_msi_entry();
309 if (!entry)
310 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700313 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 entry->msi_attrib.entry_nr = 0;
315 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700316 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700317 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700318 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (is_mask_bit_support(control)) {
320 entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
321 is_64bit_address(control));
322 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700323 entry->dev = dev;
324 if (entry->msi_attrib.maskbit) {
325 unsigned int maskbits, temp;
326 /* All MSIs are unmasked by default, Mask them all */
327 pci_read_config_dword(dev,
328 msi_mask_bits_reg(pos, is_64bit_address(control)),
329 &maskbits);
330 temp = (1 << multi_msi_capable(control));
331 temp = ((temp - 1) & ~temp);
332 maskbits |= temp;
333 pci_write_config_dword(dev,
334 msi_mask_bits_reg(pos, is_64bit_address(control)),
335 maskbits);
336 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000337 list_add(&entry->list, &dev->msi_list);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* Configure MSI capability structure */
Michael Ellerman9c831332007-04-18 19:39:21 +1000340 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000341 if (ret) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000342 msi_free_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000343 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500344 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Set MSI enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800347 pci_intx(dev, 0); /* disable intx */
348 msi_set_enable(dev, 1);
349 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Michael Ellerman7fe37302007-04-18 19:39:21 +1000351 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
353}
354
355/**
356 * msix_capability_init - configure device's MSI-X capability
357 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700358 * @entries: pointer to an array of struct msix_entry entries
359 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600361 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700362 * single MSI-X irq. A return of zero indicates the successful setup of
363 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 **/
365static int msix_capability_init(struct pci_dev *dev,
366 struct msix_entry *entries, int nvec)
367{
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000368 struct msi_desc *entry;
Michael Ellerman9c831332007-04-18 19:39:21 +1000369 int pos, i, j, nr_entries, ret;
Grant Grundlera0454b42006-02-16 23:58:29 -0800370 unsigned long phys_addr;
371 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 u16 control;
373 u8 bir;
374 void __iomem *base;
375
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800376 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
379 /* Request & Map MSI-X table region */
380 pci_read_config_word(dev, msi_control_reg(pos), &control);
381 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800382
383 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800385 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
386 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
388 if (base == NULL)
389 return -ENOMEM;
390
391 /* MSI-X Table Initialization */
392 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700393 entry = alloc_msi_entry();
394 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700399 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 entry->msi_attrib.entry_nr = j;
401 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700402 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700403 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700404 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 entry->dev = dev;
406 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700407
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000408 list_add(&entry->list, &dev->msi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000410
411 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
412 if (ret) {
413 int avail = 0;
414 list_for_each_entry(entry, &dev->msi_list, list) {
415 if (entry->irq != 0) {
416 avail++;
Michael Ellerman9c831332007-04-18 19:39:21 +1000417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000419
Michael Ellerman032de8e2007-04-18 19:39:22 +1000420 msi_free_irqs(dev);
421
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700422 /* If we had some success report the number of irqs
423 * we succeeded in setting up.
424 */
Michael Ellerman9c831332007-04-18 19:39:21 +1000425 if (avail == 0)
426 avail = ret;
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700427 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000429
430 i = 0;
431 list_for_each_entry(entry, &dev->msi_list, list) {
432 entries[i].vector = entry->irq;
433 set_irq_msi(entry->irq, entry);
434 i++;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Set MSI-X enabled bits */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800437 pci_intx(dev, 0); /* disable intx */
438 msix_set_enable(dev, 1);
439 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 return 0;
442}
443
444/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000445 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400446 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000447 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100448 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400449 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200450 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000451 * to determine if MSI/-X are supported for the device. If MSI/-X is
452 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400453 **/
Michael Ellermanc9953a72007-04-05 17:19:08 +1000454static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400455{
456 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000457 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400458
Brice Goglin0306ebf2006-10-05 10:24:31 +0200459 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400460 if (!pci_msi_enable || !dev || dev->no_msi)
461 return -EINVAL;
462
Michael Ellerman314e77b2007-04-05 17:19:12 +1000463 /*
464 * You can't ask to have 0 or less MSIs configured.
465 * a) it's stupid ..
466 * b) the list manipulation code assumes nvec >= 1.
467 */
468 if (nvec < 1)
469 return -ERANGE;
470
Brice Goglin0306ebf2006-10-05 10:24:31 +0200471 /* Any bridge which does NOT route MSI transactions from it's
472 * secondary bus to it's primary bus must set NO_MSI flag on
473 * the secondary pci_bus.
474 * We expect only arch-specific PCI host bus controller driver
475 * or quirks for specific PCI bridges to be setting NO_MSI.
476 */
Brice Goglin24334a12006-08-31 01:55:07 -0400477 for (bus = dev->bus; bus; bus = bus->parent)
478 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
479 return -EINVAL;
480
Michael Ellermanc9953a72007-04-05 17:19:08 +1000481 ret = arch_msi_check_device(dev, nvec, type);
482 if (ret)
483 return ret;
484
Michael Ellermanb1e23032007-03-22 21:51:39 +1100485 if (!pci_find_capability(dev, type))
486 return -EINVAL;
487
Brice Goglin24334a12006-08-31 01:55:07 -0400488 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{
Michael Ellermanb1e23032007-03-22 21:51:39 +1100503 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Michael Ellermanc9953a72007-04-05 17:19:08 +1000505 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
506 if (status)
507 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700509 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700511 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800512 if (dev->msix_enabled) {
513 printk(KERN_INFO "PCI: %s: Can't enable MSI. "
514 "Device already has MSI-X enabled\n",
515 pci_name(dev));
516 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return status;
520}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100521EXPORT_SYMBOL(pci_enable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523void pci_disable_msi(struct pci_dev* dev)
524{
525 struct msi_desc *entry;
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800526 int default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100528 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700529 return;
530
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800531 msi_set_enable(dev, 0);
532 pci_intx(dev, 1); /* enable intx */
533 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700534
Michael Ellerman314e77b2007-04-05 17:19:12 +1000535 BUG_ON(list_empty(&dev->msi_list));
536 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
537 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
539 }
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700540
Michael Ellermane387b9e2007-03-22 21:51:27 +1100541 default_irq = entry->msi_attrib.default_irq;
Michael Ellerman032de8e2007-04-18 19:39:22 +1000542 msi_free_irqs(dev);
Michael Ellermane387b9e2007-03-22 21:51:27 +1100543
544 /* Restore dev->irq to its default pin-assertion irq */
545 dev->irq = default_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100547EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Michael Ellerman032de8e2007-04-18 19:39:22 +1000549static int msi_free_irqs(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000551 struct msi_desc *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Michael Ellerman032de8e2007-04-18 19:39:22 +1000553 list_for_each_entry(entry, &dev->msi_list, list)
554 BUG_ON(irq_has_action(entry->irq));
Michael Ellerman7ede9c12007-03-22 21:51:34 +1100555
Michael Ellerman032de8e2007-04-18 19:39:22 +1000556 arch_teardown_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Michael Ellerman032de8e2007-04-18 19:39:22 +1000558 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
559 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
560 if (list_is_last(&entry->list, &dev->msi_list))
561 iounmap(entry->mask_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Michael Ellerman032de8e2007-04-18 19:39:22 +1000563 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
564 * PCI_MSIX_ENTRY_SIZE
565 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
566 }
567 list_del(&entry->list);
568 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
571 return 0;
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/**
575 * pci_enable_msix - configure device's MSI-X capability structure
576 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700577 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700578 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *
580 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700581 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * MSI-X mode enabled on its hardware device function. A return of zero
583 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700584 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700586 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * its request.
588 **/
589int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
590{
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700591 int status, pos, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700592 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 u16 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Michael Ellermanc9953a72007-04-05 17:19:08 +1000595 if (!entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return -EINVAL;
597
Michael Ellermanc9953a72007-04-05 17:19:08 +1000598 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
599 if (status)
600 return status;
601
Grant Grundlerb64c05e2006-01-14 00:34:53 -0700602 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 pci_read_config_word(dev, msi_control_reg(pos), &control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 nr_entries = multi_msix_capable(control);
605 if (nvec > nr_entries)
606 return -EINVAL;
607
608 /* Check for any invalid entries */
609 for (i = 0; i < nvec; i++) {
610 if (entries[i].entry >= nr_entries)
611 return -EINVAL; /* invalid entry */
612 for (j = i + 1; j < nvec; j++) {
613 if (entries[i].entry == entries[j].entry)
614 return -EINVAL; /* duplicate entry */
615 }
616 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700617 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700618
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700619 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800620 if (dev->msi_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700622 "Device already has an MSI irq assigned\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EINVAL;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return status;
628}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100629EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100631static void msix_free_all_irqs(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000633 msi_free_irqs(dev);
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100634}
635
636void pci_disable_msix(struct pci_dev* dev)
637{
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100638 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700639 return;
640
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800641 msix_set_enable(dev, 0);
642 pci_intx(dev, 1); /* enable intx */
643 dev->msix_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700644
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100645 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100647EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700650 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * @dev: pointer to the pci_dev data structure of MSI(X) device function
652 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600653 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700654 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * allocated for this device function, are reclaimed to unused state,
656 * which may be used later on.
657 **/
658void msi_remove_pci_irq_vectors(struct pci_dev* dev)
659{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!pci_msi_enable || !dev)
661 return;
662
Michael Ellerman032de8e2007-04-18 19:39:22 +1000663 if (dev->msi_enabled)
664 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100666 if (dev->msix_enabled)
667 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700670void pci_no_msi(void)
671{
672 pci_msi_enable = 0;
673}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000674
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000675void pci_msi_init_pci_dev(struct pci_dev *dev)
676{
677 INIT_LIST_HEAD(&dev->msi_list);
678}
679
Michael Ellermanc9953a72007-04-05 17:19:08 +1000680
681/* Arch hooks */
682
683int __attribute__ ((weak))
684arch_msi_check_device(struct pci_dev* dev, int nvec, int type)
685{
686 return 0;
687}
688
Michael Ellerman9c831332007-04-18 19:39:21 +1000689int __attribute__ ((weak))
690arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
691{
692 return 0;
693}
694
695int __attribute__ ((weak))
696arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
697{
698 struct msi_desc *entry;
699 int ret;
700
701 list_for_each_entry(entry, &dev->msi_list, list) {
702 ret = arch_setup_msi_irq(dev, entry);
703 if (ret)
704 return ret;
705 }
706
707 return 0;
708}
Michael Ellerman032de8e2007-04-18 19:39:22 +1000709
710void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
711{
712 return;
713}
714
715void __attribute__ ((weak))
716arch_teardown_msi_irqs(struct pci_dev *dev)
717{
718 struct msi_desc *entry;
719
720 list_for_each_entry(entry, &dev->msi_list, list) {
721 if (entry->irq != 0)
722 arch_teardown_msi_irq(entry->irq);
723 }
724}