blob: 33adf323f064536ae5501f209b4b29d1945b8961 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File: msi.c
3 * Purpose: PCI Message Signaled Interrupt (MSI)
4 *
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7 */
8
Eric W. Biederman1ce03372006-10-04 02:16:41 -07009#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
11#include <linux/irq.h>
12#include <linux/interrupt.h>
13#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pci.h>
16#include <linux/proc_fs.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070017#include <linux/msi.h>
Dan Williams4fdadeb2007-04-26 18:21:38 -070018#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/errno.h>
21#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "pci.h"
24#include "msi.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int pci_msi_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010028/* Arch hooks */
29
Michael Ellerman11df1f02009-01-19 11:31:00 +110030#ifndef arch_msi_check_device
31int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010032{
33 return 0;
34}
Michael Ellerman11df1f02009-01-19 11:31:00 +110035#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010036
Michael Ellerman11df1f02009-01-19 11:31:00 +110037#ifndef arch_setup_msi_irqs
38int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010039{
40 struct msi_desc *entry;
41 int ret;
42
43 list_for_each_entry(entry, &dev->msi_list, list) {
44 ret = arch_setup_msi_irq(dev, entry);
45 if (ret)
46 return ret;
47 }
48
49 return 0;
50}
Michael Ellerman11df1f02009-01-19 11:31:00 +110051#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010052
Michael Ellerman11df1f02009-01-19 11:31:00 +110053#ifndef arch_teardown_msi_irqs
54void arch_teardown_msi_irqs(struct pci_dev *dev)
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010055{
56 struct msi_desc *entry;
57
58 list_for_each_entry(entry, &dev->msi_list, list) {
59 if (entry->irq != 0)
60 arch_teardown_msi_irq(entry->irq);
61 }
62}
Michael Ellerman11df1f02009-01-19 11:31:00 +110063#endif
Adrian Bunk6a9e7f22007-12-11 23:19:41 +010064
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +090065static void __msi_set_enable(struct pci_dev *dev, int pos, int enable)
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080066{
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080067 u16 control;
68
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080069 if (pos) {
70 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
71 control &= ~PCI_MSI_FLAGS_ENABLE;
72 if (enable)
73 control |= PCI_MSI_FLAGS_ENABLE;
74 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
75 }
76}
77
Hidetoshi Seto5ca5c022008-05-19 13:48:17 +090078static void msi_set_enable(struct pci_dev *dev, int enable)
79{
80 __msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable);
81}
82
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -080083static void msix_set_enable(struct pci_dev *dev, int enable)
84{
85 int pos;
86 u16 control;
87
88 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
89 if (pos) {
90 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
91 control &= ~PCI_MSIX_FLAGS_ENABLE;
92 if (enable)
93 control |= PCI_MSIX_FLAGS_ENABLE;
94 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
95 }
96}
97
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -050098static inline __attribute_const__ u32 msi_mask(unsigned x)
99{
Matthew Wilcox0b49ec32009-02-08 20:27:47 -0700100 /* Don't shift by >= width of type */
101 if (x >= 5)
102 return 0xffffffff;
103 return (1 << (1 << x)) - 1;
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500104}
105
Yinghai Lu3145e942008-12-05 18:58:34 -0800106static void msix_flush_writes(struct irq_desc *desc)
Mitch Williams988cbb12007-03-30 11:54:08 -0700107{
108 struct msi_desc *entry;
109
Yinghai Lu3145e942008-12-05 18:58:34 -0800110 entry = get_irq_desc_msi(desc);
Mitch Williams988cbb12007-03-30 11:54:08 -0700111 BUG_ON(!entry || !entry->dev);
112 switch (entry->msi_attrib.type) {
113 case PCI_CAP_ID_MSI:
114 /* nothing to do */
115 break;
116 case PCI_CAP_ID_MSIX:
117 {
118 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
119 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
120 readl(entry->mask_base + offset);
121 break;
122 }
123 default:
124 BUG();
125 break;
126 }
127}
128
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600129/*
130 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
131 * mask all MSI interrupts by clearing the MSI enable bit does not work
132 * reliably as devices without an INTx disable bit will then generate a
133 * level IRQ which will never be cleared.
134 *
135 * Returns 1 if it succeeded in masking the interrupt and 0 if the device
136 * doesn't support MSI masking.
137 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800138static int msi_set_mask_bits(struct irq_desc *desc, u32 mask, u32 flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct msi_desc *entry;
141
Yinghai Lu3145e942008-12-05 18:58:34 -0800142 entry = get_irq_desc_msi(desc);
Eric W. Biederman277bc332006-10-04 02:16:57 -0700143 BUG_ON(!entry || !entry->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (entry->msi_attrib.type) {
145 case PCI_CAP_ID_MSI:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700146 if (entry->msi_attrib.maskbit) {
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900147 int pos;
148 u32 mask_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Eric W. Biederman277bc332006-10-04 02:16:57 -0700150 pos = (long)entry->mask_base;
151 pci_read_config_dword(entry->dev, pos, &mask_bits);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700152 mask_bits &= ~(mask);
153 mask_bits |= flag & mask;
Eric W. Biederman277bc332006-10-04 02:16:57 -0700154 pci_write_config_dword(entry->dev, pos, mask_bits);
Eric W. Biederman58e05432007-03-05 00:30:11 -0800155 } else {
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600156 return 0;
Eric W. Biederman277bc332006-10-04 02:16:57 -0700157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 case PCI_CAP_ID_MSIX:
160 {
161 int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
162 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
163 writel(flag, entry->mask_base + offset);
Eric W. Biederman348e3fd2007-04-03 01:41:49 -0600164 readl(entry->mask_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break;
166 }
167 default:
Eric W. Biederman277bc332006-10-04 02:16:57 -0700168 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700171 entry->msi_attrib.masked = !!flag;
Matthew Wilcoxce6fce42008-07-25 15:42:58 -0600172 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Yinghai Lu3145e942008-12-05 18:58:34 -0800175void read_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700176{
Yinghai Lu3145e942008-12-05 18:58:34 -0800177 struct msi_desc *entry = get_irq_desc_msi(desc);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700178 switch(entry->msi_attrib.type) {
179 case PCI_CAP_ID_MSI:
180 {
181 struct pci_dev *dev = entry->dev;
182 int pos = entry->msi_attrib.pos;
183 u16 data;
184
185 pci_read_config_dword(dev, msi_lower_address_reg(pos),
186 &msg->address_lo);
187 if (entry->msi_attrib.is_64) {
188 pci_read_config_dword(dev, msi_upper_address_reg(pos),
189 &msg->address_hi);
190 pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
191 } else {
192 msg->address_hi = 0;
Roland Dreiercbf5d9e2007-10-03 11:15:11 -0700193 pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700194 }
195 msg->data = data;
196 break;
197 }
198 case PCI_CAP_ID_MSIX:
199 {
200 void __iomem *base;
201 base = entry->mask_base +
202 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
203
204 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
205 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
206 msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
207 break;
208 }
209 default:
210 BUG();
211 }
212}
213
Yinghai Lu3145e942008-12-05 18:58:34 -0800214void read_msi_msg(unsigned int irq, struct msi_msg *msg)
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700215{
Yinghai Lu3145e942008-12-05 18:58:34 -0800216 struct irq_desc *desc = irq_to_desc(irq);
217
218 read_msi_msg_desc(desc, msg);
219}
220
221void write_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
222{
223 struct msi_desc *entry = get_irq_desc_msi(desc);
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700224 switch (entry->msi_attrib.type) {
225 case PCI_CAP_ID_MSI:
226 {
227 struct pci_dev *dev = entry->dev;
228 int pos = entry->msi_attrib.pos;
229
230 pci_write_config_dword(dev, msi_lower_address_reg(pos),
231 msg->address_lo);
232 if (entry->msi_attrib.is_64) {
233 pci_write_config_dword(dev, msi_upper_address_reg(pos),
234 msg->address_hi);
235 pci_write_config_word(dev, msi_data_reg(pos, 1),
236 msg->data);
237 } else {
238 pci_write_config_word(dev, msi_data_reg(pos, 0),
239 msg->data);
240 }
241 break;
242 }
243 case PCI_CAP_ID_MSIX:
244 {
245 void __iomem *base;
246 base = entry->mask_base +
247 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
248
249 writel(msg->address_lo,
250 base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
251 writel(msg->address_hi,
252 base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
253 writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
254 break;
255 }
256 default:
257 BUG();
258 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700259 entry->msg = *msg;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700260}
261
Yinghai Lu3145e942008-12-05 18:58:34 -0800262void write_msi_msg(unsigned int irq, struct msi_msg *msg)
263{
264 struct irq_desc *desc = irq_to_desc(irq);
265
266 write_msi_msg_desc(desc, msg);
267}
268
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700269void mask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Yinghai Lu3145e942008-12-05 18:58:34 -0800271 struct irq_desc *desc = irq_to_desc(irq);
272
273 msi_set_mask_bits(desc, 1, 1);
274 msix_flush_writes(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700277void unmask_msi_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Yinghai Lu3145e942008-12-05 18:58:34 -0800279 struct irq_desc *desc = irq_to_desc(irq);
280
281 msi_set_mask_bits(desc, 1, 0);
282 msix_flush_writes(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Michael Ellerman032de8e2007-04-18 19:39:22 +1000285static int msi_free_irqs(struct pci_dev* dev);
Satoru Takeuchic54c1872007-01-18 13:50:05 +0900286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static struct msi_desc* alloc_msi_entry(void)
288{
289 struct msi_desc *entry;
290
Michael Ellerman3e916c02007-03-22 21:51:36 +1100291 entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!entry)
293 return NULL;
294
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000295 INIT_LIST_HEAD(&entry->list);
296 entry->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 entry->dev = NULL;
298
299 return entry;
300}
301
David Millerba698ad2007-10-25 01:16:30 -0700302static void pci_intx_for_msi(struct pci_dev *dev, int enable)
303{
304 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
305 pci_intx(dev, enable);
306}
307
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100308static void __pci_restore_msi_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800309{
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700310 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800311 u16 control;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700312 struct msi_desc *entry;
Shaohua Li41017f02006-02-08 17:11:38 +0800313
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800314 if (!dev->msi_enabled)
315 return;
316
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700317 entry = get_irq_msi(dev->irq);
318 pos = entry->msi_attrib.pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800319
David Millerba698ad2007-10-25 01:16:30 -0700320 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800321 msi_set_enable(dev, 0);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700322 write_msi_msg(dev->irq, &entry->msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800323 if (entry->msi_attrib.maskbit) {
324 struct irq_desc *desc = irq_to_desc(dev->irq);
325 msi_set_mask_bits(desc, entry->msi_attrib.maskbits_mask,
Yinghai Lu8e149e02008-04-23 14:56:30 -0700326 entry->msi_attrib.masked);
Yinghai Lu3145e942008-12-05 18:58:34 -0800327 }
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700328
329 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
Jesse Barnesabad2ec2008-08-07 08:52:37 -0700330 control &= ~PCI_MSI_FLAGS_QSIZE;
331 control |= PCI_MSI_FLAGS_ENABLE;
Shaohua Li41017f02006-02-08 17:11:38 +0800332 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100333}
334
335static void __pci_restore_msix_state(struct pci_dev *dev)
Shaohua Li41017f02006-02-08 17:11:38 +0800336{
Shaohua Li41017f02006-02-08 17:11:38 +0800337 int pos;
Shaohua Li41017f02006-02-08 17:11:38 +0800338 struct msi_desc *entry;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700339 u16 control;
Shaohua Li41017f02006-02-08 17:11:38 +0800340
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700341 if (!dev->msix_enabled)
342 return;
343
Shaohua Li41017f02006-02-08 17:11:38 +0800344 /* route the table */
David Millerba698ad2007-10-25 01:16:30 -0700345 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800346 msix_set_enable(dev, 0);
Shaohua Li41017f02006-02-08 17:11:38 +0800347
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000348 list_for_each_entry(entry, &dev->msi_list, list) {
Yinghai Lu3145e942008-12-05 18:58:34 -0800349 struct irq_desc *desc = irq_to_desc(entry->irq);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000350 write_msi_msg(entry->irq, &entry->msg);
Yinghai Lu3145e942008-12-05 18:58:34 -0800351 msi_set_mask_bits(desc, 1, entry->msi_attrib.masked);
Shaohua Li41017f02006-02-08 17:11:38 +0800352 }
Shaohua Li41017f02006-02-08 17:11:38 +0800353
Michael Ellerman314e77b2007-04-05 17:19:12 +1000354 BUG_ON(list_empty(&dev->msi_list));
355 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000356 pos = entry->msi_attrib.pos;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700357 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
358 control &= ~PCI_MSIX_FLAGS_MASKALL;
359 control |= PCI_MSIX_FLAGS_ENABLE;
360 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
Shaohua Li41017f02006-02-08 17:11:38 +0800361}
Michael Ellerman8fed4b62007-01-25 19:34:08 +1100362
363void pci_restore_msi_state(struct pci_dev *dev)
364{
365 __pci_restore_msi_state(dev);
366 __pci_restore_msix_state(dev);
367}
Linas Vepstas94688cf2007-11-07 15:43:59 -0600368EXPORT_SYMBOL_GPL(pci_restore_msi_state);
Shaohua Li41017f02006-02-08 17:11:38 +0800369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/**
371 * msi_capability_init - configure device's MSI capability structure
372 * @dev: pointer to the pci_dev data structure of MSI device function
373 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600374 * Setup the MSI capability structure of device function with a single
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700375 * MSI irq, regardless of device function is capable of handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * multiple messages. A return of zero indicates the successful setup
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700377 * of an entry zero with the new MSI irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 **/
379static int msi_capability_init(struct pci_dev *dev)
380{
381 struct msi_desc *entry;
Michael Ellerman7fe37302007-04-18 19:39:21 +1000382 int pos, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 u16 control;
384
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800385 msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
388 pci_read_config_word(dev, msi_control_reg(pos), &control);
389 /* MSI Entry Initialization */
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700390 entry = alloc_msi_entry();
391 if (!entry)
392 return -ENOMEM;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 entry->msi_attrib.type = PCI_CAP_ID_MSI;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700395 entry->msi_attrib.is_64 = is_64bit_address(control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 entry->msi_attrib.entry_nr = 0;
397 entry->msi_attrib.maskbit = is_mask_bit_support(control);
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700398 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700399 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700400 entry->msi_attrib.pos = pos;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700401 entry->dev = dev;
402 if (entry->msi_attrib.maskbit) {
Hidetoshi Seto0db29af2008-12-24 17:27:04 +0900403 unsigned int base, maskbits, temp;
404
405 base = msi_mask_bits_reg(pos, entry->msi_attrib.is_64);
406 entry->mask_base = (void __iomem *)(long)base;
407
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700408 /* All MSIs are unmasked by default, Mask them all */
Hidetoshi Seto0db29af2008-12-24 17:27:04 +0900409 pci_read_config_dword(dev, base, &maskbits);
Matthew Wilcoxbffac3c2009-01-21 19:19:19 -0500410 temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700411 maskbits |= temp;
Hidetoshi Seto0db29af2008-12-24 17:27:04 +0900412 pci_write_config_dword(dev, base, maskbits);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700413 entry->msi_attrib.maskbits_mask = temp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700414 }
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700415 list_add_tail(&entry->list, &dev->msi_list);
Michael Ellerman9c831332007-04-18 19:39:21 +1000416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Configure MSI capability structure */
Michael Ellerman9c831332007-04-18 19:39:21 +1000418 ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000419 if (ret) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000420 msi_free_irqs(dev);
Michael Ellerman7fe37302007-04-18 19:39:21 +1000421 return ret;
Mark Maulefd58e552006-04-10 21:17:48 -0500422 }
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Set MSI enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700425 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800426 msi_set_enable(dev, 1);
427 dev->msi_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Michael Ellerman7fe37302007-04-18 19:39:21 +1000429 dev->irq = entry->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433/**
434 * msix_capability_init - configure device's MSI-X capability
435 * @dev: pointer to the pci_dev data structure of MSI-X device function
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700436 * @entries: pointer to an array of struct msix_entry entries
437 * @nvec: number of @entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600439 * Setup the MSI-X capability structure of device function with a
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700440 * single MSI-X irq. A return of zero indicates the successful setup of
441 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 **/
443static int msix_capability_init(struct pci_dev *dev,
444 struct msix_entry *entries, int nvec)
445{
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000446 struct msi_desc *entry;
Michael Ellerman9c831332007-04-18 19:39:21 +1000447 int pos, i, j, nr_entries, ret;
Grant Grundlera0454b42006-02-16 23:58:29 -0800448 unsigned long phys_addr;
449 u32 table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 u16 control;
451 u8 bir;
452 void __iomem *base;
453
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800454 msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
457 /* Request & Map MSI-X table region */
458 pci_read_config_word(dev, msi_control_reg(pos), &control);
459 nr_entries = multi_msix_capable(control);
Grant Grundlera0454b42006-02-16 23:58:29 -0800460
461 pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
Grant Grundlera0454b42006-02-16 23:58:29 -0800463 table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
464 phys_addr = pci_resource_start (dev, bir) + table_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
466 if (base == NULL)
467 return -ENOMEM;
468
469 /* MSI-X Table Initialization */
470 for (i = 0; i < nvec; i++) {
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700471 entry = alloc_msi_entry();
472 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 j = entries[i].entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 entry->msi_attrib.type = PCI_CAP_ID_MSIX;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700477 entry->msi_attrib.is_64 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 entry->msi_attrib.entry_nr = j;
479 entry->msi_attrib.maskbit = 1;
Eric W. Biederman392ee1e2007-03-08 13:04:57 -0700480 entry->msi_attrib.masked = 1;
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700481 entry->msi_attrib.default_irq = dev->irq;
Eric W. Biederman0366f8f2006-10-04 02:16:33 -0700482 entry->msi_attrib.pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 entry->dev = dev;
484 entry->mask_base = base;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700485
Eric W. Biederman0dd11f92007-06-01 00:46:32 -0700486 list_add_tail(&entry->list, &dev->msi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000488
489 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
490 if (ret) {
491 int avail = 0;
492 list_for_each_entry(entry, &dev->msi_list, list) {
493 if (entry->irq != 0) {
494 avail++;
Michael Ellerman9c831332007-04-18 19:39:21 +1000495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000497
Michael Ellerman032de8e2007-04-18 19:39:22 +1000498 msi_free_irqs(dev);
499
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700500 /* If we had some success report the number of irqs
501 * we succeeded in setting up.
502 */
Michael Ellerman9c831332007-04-18 19:39:21 +1000503 if (avail == 0)
504 avail = ret;
Eric W. Biederman92db6d12006-10-04 02:16:35 -0700505 return avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Michael Ellerman9c831332007-04-18 19:39:21 +1000507
508 i = 0;
509 list_for_each_entry(entry, &dev->msi_list, list) {
510 entries[i].vector = entry->irq;
511 set_irq_msi(entry->irq, entry);
512 i++;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Set MSI-X enabled bits */
David Millerba698ad2007-10-25 01:16:30 -0700515 pci_intx_for_msi(dev, 0);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800516 msix_set_enable(dev, 1);
517 dev->msix_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 return 0;
520}
521
522/**
Michael Ellerman17bbc122007-04-05 17:19:07 +1000523 * pci_msi_check_device - check whether MSI may be enabled on a device
Brice Goglin24334a12006-08-31 01:55:07 -0400524 * @dev: pointer to the pci_dev data structure of MSI device function
Michael Ellermanc9953a72007-04-05 17:19:08 +1000525 * @nvec: how many MSIs have been requested ?
Michael Ellermanb1e23032007-03-22 21:51:39 +1100526 * @type: are we checking for MSI or MSI-X ?
Brice Goglin24334a12006-08-31 01:55:07 -0400527 *
Brice Goglin0306ebf2006-10-05 10:24:31 +0200528 * Look at global flags, the device itself, and its parent busses
Michael Ellerman17bbc122007-04-05 17:19:07 +1000529 * to determine if MSI/-X are supported for the device. If MSI/-X is
530 * supported return 0, else return an error code.
Brice Goglin24334a12006-08-31 01:55:07 -0400531 **/
Michael Ellermanc9953a72007-04-05 17:19:08 +1000532static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
Brice Goglin24334a12006-08-31 01:55:07 -0400533{
534 struct pci_bus *bus;
Michael Ellermanc9953a72007-04-05 17:19:08 +1000535 int ret;
Brice Goglin24334a12006-08-31 01:55:07 -0400536
Brice Goglin0306ebf2006-10-05 10:24:31 +0200537 /* MSI must be globally enabled and supported by the device */
Brice Goglin24334a12006-08-31 01:55:07 -0400538 if (!pci_msi_enable || !dev || dev->no_msi)
539 return -EINVAL;
540
Michael Ellerman314e77b2007-04-05 17:19:12 +1000541 /*
542 * You can't ask to have 0 or less MSIs configured.
543 * a) it's stupid ..
544 * b) the list manipulation code assumes nvec >= 1.
545 */
546 if (nvec < 1)
547 return -ERANGE;
548
Brice Goglin0306ebf2006-10-05 10:24:31 +0200549 /* Any bridge which does NOT route MSI transactions from it's
550 * secondary bus to it's primary bus must set NO_MSI flag on
551 * the secondary pci_bus.
552 * We expect only arch-specific PCI host bus controller driver
553 * or quirks for specific PCI bridges to be setting NO_MSI.
554 */
Brice Goglin24334a12006-08-31 01:55:07 -0400555 for (bus = dev->bus; bus; bus = bus->parent)
556 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
557 return -EINVAL;
558
Michael Ellermanc9953a72007-04-05 17:19:08 +1000559 ret = arch_msi_check_device(dev, nvec, type);
560 if (ret)
561 return ret;
562
Michael Ellermanb1e23032007-03-22 21:51:39 +1100563 if (!pci_find_capability(dev, type))
564 return -EINVAL;
565
Brice Goglin24334a12006-08-31 01:55:07 -0400566 return 0;
567}
568
569/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * pci_enable_msi - configure device's MSI capability structure
571 * @dev: pointer to the pci_dev data structure of MSI device function
572 *
573 * Setup the MSI capability structure of device function with
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700574 * a single MSI irq upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * MSI mode enabled on its hardware device function. A return of zero
576 * indicates the successful setup of an entry zero with the new MSI
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700577 * irq or non-zero for otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 **/
579int pci_enable_msi(struct pci_dev* dev)
580{
Michael Ellermanb1e23032007-03-22 21:51:39 +1100581 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Michael Ellermanc9953a72007-04-05 17:19:08 +1000583 status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
584 if (status)
585 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700587 WARN_ON(!!dev->msi_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700589 /* Check whether driver already requested for MSI-X irqs */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800590 if (dev->msix_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600591 dev_info(&dev->dev, "can't enable MSI "
592 "(MSI-X already enabled)\n");
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 status = msi_capability_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return status;
597}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100598EXPORT_SYMBOL(pci_enable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Yinghai Lud52877c2008-04-23 14:58:09 -0700600void pci_msi_shutdown(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct msi_desc *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100604 if (!pci_msi_enable || !dev || !dev->msi_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700605 return;
606
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800607 msi_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700608 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800609 dev->msi_enabled = 0;
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700610
Michael Ellerman314e77b2007-04-05 17:19:12 +1000611 BUG_ON(list_empty(&dev->msi_list));
612 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700613 /* Return the the pci reset with msi irqs unmasked */
614 if (entry->msi_attrib.maskbit) {
615 u32 mask = entry->msi_attrib.maskbits_mask;
Yinghai Lu3145e942008-12-05 18:58:34 -0800616 struct irq_desc *desc = irq_to_desc(dev->irq);
617 msi_set_mask_bits(desc, mask, ~mask);
Yinghai Lu8e149e02008-04-23 14:56:30 -0700618 }
Yinghai Lud52877c2008-04-23 14:58:09 -0700619 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return;
Michael Ellermane387b9e2007-03-22 21:51:27 +1100621
622 /* Restore dev->irq to its default pin-assertion irq */
Yinghai Lud52877c2008-04-23 14:58:09 -0700623 dev->irq = entry->msi_attrib.default_irq;
624}
625void pci_disable_msi(struct pci_dev* dev)
626{
627 struct msi_desc *entry;
628
629 if (!pci_msi_enable || !dev || !dev->msi_enabled)
630 return;
631
632 pci_msi_shutdown(dev);
633
634 entry = list_entry(dev->msi_list.next, struct msi_desc, list);
635 if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
636 return;
637
638 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100640EXPORT_SYMBOL(pci_disable_msi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Michael Ellerman032de8e2007-04-18 19:39:22 +1000642static int msi_free_irqs(struct pci_dev* dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000644 struct msi_desc *entry, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
David Millerb3b7cc72007-05-11 13:26:44 -0700646 list_for_each_entry(entry, &dev->msi_list, list) {
647 if (entry->irq)
648 BUG_ON(irq_has_action(entry->irq));
649 }
Michael Ellerman7ede9c12007-03-22 21:51:34 +1100650
Michael Ellerman032de8e2007-04-18 19:39:22 +1000651 arch_teardown_msi_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Michael Ellerman032de8e2007-04-18 19:39:22 +1000653 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
654 if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
Michael Ellerman032de8e2007-04-18 19:39:22 +1000655 writel(1, entry->mask_base + entry->msi_attrib.entry_nr
656 * PCI_MSIX_ENTRY_SIZE
657 + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
Eric W. Biederman78b76112007-06-01 00:46:33 -0700658
659 if (list_is_last(&entry->list, &dev->msi_list))
660 iounmap(entry->mask_base);
Michael Ellerman032de8e2007-04-18 19:39:22 +1000661 }
662 list_del(&entry->list);
663 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 return 0;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/**
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100670 * pci_msix_table_size - return the number of device's MSI-X table entries
671 * @dev: pointer to the pci_dev data structure of MSI-X device function
672 */
673int pci_msix_table_size(struct pci_dev *dev)
674{
675 int pos;
676 u16 control;
677
678 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
679 if (!pos)
680 return 0;
681
682 pci_read_config_word(dev, msi_control_reg(pos), &control);
683 return multi_msix_capable(control);
684}
685
686/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * pci_enable_msix - configure device's MSI-X capability structure
688 * @dev: pointer to the pci_dev data structure of MSI-X device function
Greg Kroah-Hartman70549ad2005-06-06 23:07:46 -0700689 * @entries: pointer to an array of MSI-X entries
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700690 * @nvec: number of MSI-X irqs requested for allocation by device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 *
692 * Setup the MSI-X capability structure of device function with the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700693 * of requested irqs upon its software driver call to request for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 * MSI-X mode enabled on its hardware device function. A return of zero
695 * indicates the successful configuration of MSI-X capability structure
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700696 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * Or a return of > 0 indicates that driver request is exceeding the number
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700698 * of irqs available. Driver should use the returned value to re-send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * its request.
700 **/
701int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
702{
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100703 int status, nr_entries;
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700704 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Michael Ellermanc9953a72007-04-05 17:19:08 +1000706 if (!entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EINVAL;
708
Michael Ellermanc9953a72007-04-05 17:19:08 +1000709 status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
710 if (status)
711 return status;
712
Rafael J. Wysockia52e2e32009-01-24 00:21:14 +0100713 nr_entries = pci_msix_table_size(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (nvec > nr_entries)
715 return -EINVAL;
716
717 /* Check for any invalid entries */
718 for (i = 0; i < nvec; i++) {
719 if (entries[i].entry >= nr_entries)
720 return -EINVAL; /* invalid entry */
721 for (j = i + 1; j < nvec; j++) {
722 if (entries[i].entry == entries[j].entry)
723 return -EINVAL; /* duplicate entry */
724 }
725 }
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700726 WARN_ON(!!dev->msix_enabled);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700727
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700728 /* Check whether driver already requested for MSI irq */
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800729 if (dev->msi_enabled) {
Bjorn Helgaas80ccba12008-06-13 10:52:11 -0600730 dev_info(&dev->dev, "can't enable MSI-X "
731 "(MSI IRQ already assigned)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -EINVAL;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 status = msix_capability_init(dev, entries, nvec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return status;
736}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100737EXPORT_SYMBOL(pci_enable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100739static void msix_free_all_irqs(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Michael Ellerman032de8e2007-04-18 19:39:22 +1000741 msi_free_irqs(dev);
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100742}
743
Yinghai Lud52877c2008-04-23 14:58:09 -0700744void pci_msix_shutdown(struct pci_dev* dev)
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100745{
Michael Ellerman128bc5f2007-03-22 21:51:39 +1100746 if (!pci_msi_enable || !dev || !dev->msix_enabled)
Eric W. Biedermanded86d82007-01-28 12:42:52 -0700747 return;
748
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800749 msix_set_enable(dev, 0);
David Millerba698ad2007-10-25 01:16:30 -0700750 pci_intx_for_msi(dev, 1);
Eric W. Biedermanb1cbf4e2007-03-05 00:30:10 -0800751 dev->msix_enabled = 0;
Yinghai Lud52877c2008-04-23 14:58:09 -0700752}
753void pci_disable_msix(struct pci_dev* dev)
754{
755 if (!pci_msi_enable || !dev || !dev->msix_enabled)
756 return;
757
758 pci_msix_shutdown(dev);
Eric W. Biederman7bd007e2006-10-04 02:16:31 -0700759
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100760 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Michael Ellerman4cc086f2007-03-22 21:51:34 +1100762EXPORT_SYMBOL(pci_disable_msix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/**
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700765 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * @dev: pointer to the pci_dev data structure of MSI(X) device function
767 *
Steven Coleeaae4b32005-05-03 18:38:30 -0600768 * Being called during hotplug remove, from which the device function
Eric W. Biederman1ce03372006-10-04 02:16:41 -0700769 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * allocated for this device function, are reclaimed to unused state,
771 * which may be used later on.
772 **/
773void msi_remove_pci_irq_vectors(struct pci_dev* dev)
774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!pci_msi_enable || !dev)
776 return;
777
Michael Ellerman032de8e2007-04-18 19:39:22 +1000778 if (dev->msi_enabled)
779 msi_free_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Michael Ellermanfc4afc72007-03-22 21:51:33 +1100781 if (dev->msix_enabled)
782 msix_free_all_irqs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Matthew Wilcox309e57d2006-03-05 22:33:34 -0700785void pci_no_msi(void)
786{
787 pci_msi_enable = 0;
788}
Michael Ellermanc9953a72007-04-05 17:19:08 +1000789
Andrew Patterson07ae95f2008-11-10 15:31:05 -0700790/**
791 * pci_msi_enabled - is MSI enabled?
792 *
793 * Returns true if MSI has not been disabled by the command-line option
794 * pci=nomsi.
795 **/
796int pci_msi_enabled(void)
797{
798 return pci_msi_enable;
799}
800EXPORT_SYMBOL(pci_msi_enabled);
801
Michael Ellerman4aa9bc92007-04-05 17:19:10 +1000802void pci_msi_init_pci_dev(struct pci_dev *dev)
803{
804 INIT_LIST_HEAD(&dev->msi_list);
805}