]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/pci/msi.c
PCI/MSI: Rename "struct msi_chip" to "struct msi_controller"
[mirror_ubuntu-zesty-kernel.git] / drivers / pci / msi.c
CommitLineData
1da177e4
LT
1/*
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
1ce03373 9#include <linux/err.h>
1da177e4
LT
10#include <linux/mm.h>
11#include <linux/irq.h>
12#include <linux/interrupt.h>
363c75db 13#include <linux/export.h>
1da177e4 14#include <linux/ioport.h>
1da177e4
LT
15#include <linux/pci.h>
16#include <linux/proc_fs.h>
3b7d1921 17#include <linux/msi.h>
4fdadebc 18#include <linux/smp.h>
500559a9
HS
19#include <linux/errno.h>
20#include <linux/io.h>
5a0e3ad6 21#include <linux/slab.h>
1da177e4
LT
22
23#include "pci.h"
1da177e4 24
1da177e4 25static int pci_msi_enable = 1;
38737d82 26int pci_msi_ignore_mask;
1da177e4 27
527eee29
BH
28#define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
29
30
6a9e7f20
AB
31/* Arch hooks */
32
4287d824
TP
33int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
34{
c2791b80 35 struct msi_controller *chip = dev->bus->msi;
0cbdcfcf
TR
36 int err;
37
38 if (!chip || !chip->setup_irq)
39 return -EINVAL;
40
41 err = chip->setup_irq(chip, dev, desc);
42 if (err < 0)
43 return err;
44
45 irq_set_chip_data(desc->irq, chip);
46
47 return 0;
4287d824
TP
48}
49
50void __weak arch_teardown_msi_irq(unsigned int irq)
6a9e7f20 51{
c2791b80 52 struct msi_controller *chip = irq_get_chip_data(irq);
0cbdcfcf
TR
53
54 if (!chip || !chip->teardown_irq)
55 return;
56
57 chip->teardown_irq(chip, irq);
6a9e7f20
AB
58}
59
4287d824 60int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
6a9e7f20
AB
61{
62 struct msi_desc *entry;
63 int ret;
64
1c8d7b0a
MW
65 /*
66 * If an architecture wants to support multiple MSI, it needs to
67 * override arch_setup_msi_irqs()
68 */
69 if (type == PCI_CAP_ID_MSI && nvec > 1)
70 return 1;
71
6a9e7f20
AB
72 list_for_each_entry(entry, &dev->msi_list, list) {
73 ret = arch_setup_msi_irq(dev, entry);
b5fbf533 74 if (ret < 0)
6a9e7f20 75 return ret;
b5fbf533
ME
76 if (ret > 0)
77 return -ENOSPC;
6a9e7f20
AB
78 }
79
80 return 0;
81}
1525bf0d 82
4287d824
TP
83/*
84 * We have a default implementation available as a separate non-weak
85 * function, as it is used by the Xen x86 PCI code
86 */
1525bf0d 87void default_teardown_msi_irqs(struct pci_dev *dev)
6a9e7f20
AB
88{
89 struct msi_desc *entry;
90
91 list_for_each_entry(entry, &dev->msi_list, list) {
1c8d7b0a
MW
92 int i, nvec;
93 if (entry->irq == 0)
94 continue;
65f6ae66
AG
95 if (entry->nvec_used)
96 nvec = entry->nvec_used;
97 else
98 nvec = 1 << entry->msi_attrib.multiple;
1c8d7b0a
MW
99 for (i = 0; i < nvec; i++)
100 arch_teardown_msi_irq(entry->irq + i);
6a9e7f20
AB
101 }
102}
103
4287d824
TP
104void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
105{
106 return default_teardown_msi_irqs(dev);
107}
76ccc297 108
ac8344c4 109static void default_restore_msi_irq(struct pci_dev *dev, int irq)
76ccc297
KRW
110{
111 struct msi_desc *entry;
112
113 entry = NULL;
114 if (dev->msix_enabled) {
115 list_for_each_entry(entry, &dev->msi_list, list) {
116 if (irq == entry->irq)
117 break;
118 }
119 } else if (dev->msi_enabled) {
120 entry = irq_get_msi_desc(irq);
121 }
122
123 if (entry)
56b72b40 124 __write_msi_msg(entry, &entry->msg);
76ccc297 125}
4287d824 126
ac8344c4 127void __weak arch_restore_msi_irqs(struct pci_dev *dev)
4287d824 128{
ac8344c4 129 return default_restore_msi_irqs(dev);
4287d824 130}
76ccc297 131
e375b561 132static void msi_set_enable(struct pci_dev *dev, int enable)
b1cbf4e4 133{
b1cbf4e4
EB
134 u16 control;
135
e375b561 136 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
110828c9
MW
137 control &= ~PCI_MSI_FLAGS_ENABLE;
138 if (enable)
139 control |= PCI_MSI_FLAGS_ENABLE;
e375b561 140 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
5ca5c02f
HS
141}
142
66f0d0c4 143static void msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
b1cbf4e4 144{
66f0d0c4 145 u16 ctrl;
b1cbf4e4 146
66f0d0c4
YW
147 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
148 ctrl &= ~clear;
149 ctrl |= set;
150 pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
b1cbf4e4
EB
151}
152
bffac3c5
MW
153static inline __attribute_const__ u32 msi_mask(unsigned x)
154{
0b49ec37
MW
155 /* Don't shift by >= width of type */
156 if (x >= 5)
157 return 0xffffffff;
158 return (1 << (1 << x)) - 1;
bffac3c5
MW
159}
160
ce6fce42
MW
161/*
162 * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
163 * mask all MSI interrupts by clearing the MSI enable bit does not work
164 * reliably as devices without an INTx disable bit will then generate a
165 * level IRQ which will never be cleared.
ce6fce42 166 */
03f56e42 167u32 __msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
1da177e4 168{
f2440d9a 169 u32 mask_bits = desc->masked;
1da177e4 170
38737d82 171 if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
12abb8ba 172 return 0;
f2440d9a
MW
173
174 mask_bits &= ~mask;
175 mask_bits |= flag;
176 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
12abb8ba
HS
177
178 return mask_bits;
179}
180
181static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
182{
03f56e42 183 desc->masked = __msi_mask_irq(desc, mask, flag);
f2440d9a
MW
184}
185
186/*
187 * This internal function does not flush PCI writes to the device.
188 * All users must ensure that they read from the device before either
189 * assuming that the device state is up to date, or returning out of this
190 * file. This saves a few milliseconds when initialising devices with lots
191 * of MSI-X interrupts.
192 */
03f56e42 193u32 __msix_mask_irq(struct msi_desc *desc, u32 flag)
f2440d9a
MW
194{
195 u32 mask_bits = desc->masked;
196 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
2c21fd4b 197 PCI_MSIX_ENTRY_VECTOR_CTRL;
38737d82
YW
198
199 if (pci_msi_ignore_mask)
200 return 0;
201
8d805286
SY
202 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
203 if (flag)
204 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
f2440d9a 205 writel(mask_bits, desc->mask_base + offset);
12abb8ba
HS
206
207 return mask_bits;
208}
209
210static void msix_mask_irq(struct msi_desc *desc, u32 flag)
211{
03f56e42 212 desc->masked = __msix_mask_irq(desc, flag);
f2440d9a 213}
24d27553 214
1c9db525 215static void msi_set_mask_bit(struct irq_data *data, u32 flag)
f2440d9a 216{
1c9db525 217 struct msi_desc *desc = irq_data_get_msi(data);
24d27553 218
f2440d9a
MW
219 if (desc->msi_attrib.is_msix) {
220 msix_mask_irq(desc, flag);
221 readl(desc->mask_base); /* Flush write to device */
222 } else {
a281b788 223 unsigned offset = data->irq - desc->irq;
1c8d7b0a 224 msi_mask_irq(desc, 1 << offset, flag << offset);
1da177e4 225 }
f2440d9a
MW
226}
227
1c9db525 228void mask_msi_irq(struct irq_data *data)
f2440d9a 229{
1c9db525 230 msi_set_mask_bit(data, 1);
f2440d9a
MW
231}
232
1c9db525 233void unmask_msi_irq(struct irq_data *data)
f2440d9a 234{
1c9db525 235 msi_set_mask_bit(data, 0);
1da177e4
LT
236}
237
ac8344c4
D
238void default_restore_msi_irqs(struct pci_dev *dev)
239{
240 struct msi_desc *entry;
241
242 list_for_each_entry(entry, &dev->msi_list, list) {
243 default_restore_msi_irq(dev, entry->irq);
244 }
245}
246
39431acb 247void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
1da177e4 248{
30da5524
BH
249 BUG_ON(entry->dev->current_state != PCI_D0);
250
251 if (entry->msi_attrib.is_msix) {
252 void __iomem *base = entry->mask_base +
253 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
254
255 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
256 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
257 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
258 } else {
259 struct pci_dev *dev = entry->dev;
f5322169 260 int pos = dev->msi_cap;
30da5524
BH
261 u16 data;
262
9925ad0c
BH
263 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
264 &msg->address_lo);
30da5524 265 if (entry->msi_attrib.is_64) {
9925ad0c
BH
266 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
267 &msg->address_hi);
2f221349 268 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
30da5524
BH
269 } else {
270 msg->address_hi = 0;
2f221349 271 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
30da5524
BH
272 }
273 msg->data = data;
274 }
275}
276
277void read_msi_msg(unsigned int irq, struct msi_msg *msg)
278{
dced35ae 279 struct msi_desc *entry = irq_get_msi_desc(irq);
30da5524 280
39431acb 281 __read_msi_msg(entry, msg);
30da5524
BH
282}
283
39431acb 284void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
30da5524 285{
30da5524 286 /* Assert that the cache is valid, assuming that
fcd097f3
BH
287 * valid messages are not all-zeroes. */
288 BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
289 entry->msg.data));
0366f8f7 290
fcd097f3 291 *msg = entry->msg;
0366f8f7 292}
1da177e4 293
30da5524 294void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
0366f8f7 295{
dced35ae 296 struct msi_desc *entry = irq_get_msi_desc(irq);
3145e941 297
39431acb 298 __get_cached_msi_msg(entry, msg);
3145e941 299}
3b307ffe 300EXPORT_SYMBOL_GPL(get_cached_msi_msg);
3145e941 301
39431acb 302void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
3145e941 303{
fcd097f3
BH
304 if (entry->dev->current_state != PCI_D0) {
305 /* Don't touch the hardware now */
306 } else if (entry->msi_attrib.is_msix) {
24d27553
MW
307 void __iomem *base;
308 base = entry->mask_base +
309 entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
310
2c21fd4b
HS
311 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
312 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
313 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
24d27553 314 } else {
0366f8f7 315 struct pci_dev *dev = entry->dev;
f5322169 316 int pos = dev->msi_cap;
1c8d7b0a
MW
317 u16 msgctl;
318
f84ecd28 319 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
1c8d7b0a
MW
320 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
321 msgctl |= entry->msi_attrib.multiple << 4;
f84ecd28 322 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
0366f8f7 323
9925ad0c
BH
324 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
325 msg->address_lo);
0366f8f7 326 if (entry->msi_attrib.is_64) {
9925ad0c
BH
327 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
328 msg->address_hi);
2f221349
BH
329 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
330 msg->data);
0366f8f7 331 } else {
2f221349
BH
332 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
333 msg->data);
0366f8f7 334 }
1da177e4 335 }
392ee1e6 336 entry->msg = *msg;
1da177e4 337}
0366f8f7 338
3145e941
YL
339void write_msi_msg(unsigned int irq, struct msi_msg *msg)
340{
dced35ae 341 struct msi_desc *entry = irq_get_msi_desc(irq);
3145e941 342
39431acb 343 __write_msi_msg(entry, msg);
3145e941 344}
3b307ffe 345EXPORT_SYMBOL_GPL(write_msi_msg);
3145e941 346
f56e4481
HS
347static void free_msi_irqs(struct pci_dev *dev)
348{
349 struct msi_desc *entry, *tmp;
1c51b50c
GKH
350 struct attribute **msi_attrs;
351 struct device_attribute *dev_attr;
352 int count = 0;
f56e4481
HS
353
354 list_for_each_entry(entry, &dev->msi_list, list) {
355 int i, nvec;
356 if (!entry->irq)
357 continue;
65f6ae66
AG
358 if (entry->nvec_used)
359 nvec = entry->nvec_used;
360 else
361 nvec = 1 << entry->msi_attrib.multiple;
f56e4481
HS
362 for (i = 0; i < nvec; i++)
363 BUG_ON(irq_has_action(entry->irq + i));
364 }
365
366 arch_teardown_msi_irqs(dev);
367
368 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
369 if (entry->msi_attrib.is_msix) {
370 if (list_is_last(&entry->list, &dev->msi_list))
371 iounmap(entry->mask_base);
372 }
424eb391 373
f56e4481
HS
374 list_del(&entry->list);
375 kfree(entry);
376 }
1c51b50c
GKH
377
378 if (dev->msi_irq_groups) {
379 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
380 msi_attrs = dev->msi_irq_groups[0]->attrs;
b701c0b1 381 while (msi_attrs[count]) {
1c51b50c
GKH
382 dev_attr = container_of(msi_attrs[count],
383 struct device_attribute, attr);
384 kfree(dev_attr->attr.name);
385 kfree(dev_attr);
386 ++count;
387 }
388 kfree(msi_attrs);
389 kfree(dev->msi_irq_groups[0]);
390 kfree(dev->msi_irq_groups);
391 dev->msi_irq_groups = NULL;
392 }
f56e4481 393}
c54c1879 394
379f5327 395static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
1da177e4 396{
379f5327
MW
397 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
398 if (!desc)
1da177e4
LT
399 return NULL;
400
379f5327
MW
401 INIT_LIST_HEAD(&desc->list);
402 desc->dev = dev;
1da177e4 403
379f5327 404 return desc;
1da177e4
LT
405}
406
ba698ad4
DM
407static void pci_intx_for_msi(struct pci_dev *dev, int enable)
408{
409 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
410 pci_intx(dev, enable);
411}
412
8fed4b65 413static void __pci_restore_msi_state(struct pci_dev *dev)
41017f0c 414{
41017f0c 415 u16 control;
392ee1e6 416 struct msi_desc *entry;
41017f0c 417
b1cbf4e4
EB
418 if (!dev->msi_enabled)
419 return;
420
dced35ae 421 entry = irq_get_msi_desc(dev->irq);
41017f0c 422
ba698ad4 423 pci_intx_for_msi(dev, 0);
e375b561 424 msi_set_enable(dev, 0);
ac8344c4 425 arch_restore_msi_irqs(dev);
392ee1e6 426
f5322169 427 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
31ea5d4d
YW
428 msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
429 entry->masked);
abad2ec9 430 control &= ~PCI_MSI_FLAGS_QSIZE;
1c8d7b0a 431 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
f5322169 432 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
8fed4b65
ME
433}
434
435static void __pci_restore_msix_state(struct pci_dev *dev)
41017f0c 436{
41017f0c 437 struct msi_desc *entry;
41017f0c 438
ded86d8d
EB
439 if (!dev->msix_enabled)
440 return;
f598282f 441 BUG_ON(list_empty(&dev->msi_list));
ded86d8d 442
41017f0c 443 /* route the table */
ba698ad4 444 pci_intx_for_msi(dev, 0);
66f0d0c4
YW
445 msix_clear_and_set_ctrl(dev, 0,
446 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
41017f0c 447
ac8344c4 448 arch_restore_msi_irqs(dev);
4aa9bc95 449 list_for_each_entry(entry, &dev->msi_list, list) {
f2440d9a 450 msix_mask_irq(entry, entry->masked);
41017f0c 451 }
41017f0c 452
66f0d0c4 453 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
41017f0c 454}
8fed4b65
ME
455
456void pci_restore_msi_state(struct pci_dev *dev)
457{
458 __pci_restore_msi_state(dev);
459 __pci_restore_msix_state(dev);
460}
94688cf2 461EXPORT_SYMBOL_GPL(pci_restore_msi_state);
41017f0c 462
1c51b50c 463static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
da8d1c8b
NH
464 char *buf)
465{
1c51b50c
GKH
466 struct msi_desc *entry;
467 unsigned long irq;
468 int retval;
da8d1c8b 469
1c51b50c
GKH
470 retval = kstrtoul(attr->attr.name, 10, &irq);
471 if (retval)
472 return retval;
da8d1c8b 473
e11ece5a
YW
474 entry = irq_get_msi_desc(irq);
475 if (entry)
476 return sprintf(buf, "%s\n",
477 entry->msi_attrib.is_msix ? "msix" : "msi");
478
1c51b50c 479 return -ENODEV;
da8d1c8b
NH
480}
481
da8d1c8b
NH
482static int populate_msi_sysfs(struct pci_dev *pdev)
483{
1c51b50c
GKH
484 struct attribute **msi_attrs;
485 struct attribute *msi_attr;
486 struct device_attribute *msi_dev_attr;
487 struct attribute_group *msi_irq_group;
488 const struct attribute_group **msi_irq_groups;
da8d1c8b 489 struct msi_desc *entry;
1c51b50c
GKH
490 int ret = -ENOMEM;
491 int num_msi = 0;
da8d1c8b
NH
492 int count = 0;
493
1c51b50c
GKH
494 /* Determine how many msi entries we have */
495 list_for_each_entry(entry, &pdev->msi_list, list) {
496 ++num_msi;
497 }
498 if (!num_msi)
499 return 0;
da8d1c8b 500
1c51b50c
GKH
501 /* Dynamically create the MSI attributes for the PCI device */
502 msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
503 if (!msi_attrs)
504 return -ENOMEM;
da8d1c8b 505 list_for_each_entry(entry, &pdev->msi_list, list) {
1c51b50c 506 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
1406276c 507 if (!msi_dev_attr)
1c51b50c 508 goto error_attrs;
1406276c 509 msi_attrs[count] = &msi_dev_attr->attr;
86bb4f69 510
1c51b50c 511 sysfs_attr_init(&msi_dev_attr->attr);
1406276c
JB
512 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
513 entry->irq);
514 if (!msi_dev_attr->attr.name)
515 goto error_attrs;
1c51b50c
GKH
516 msi_dev_attr->attr.mode = S_IRUGO;
517 msi_dev_attr->show = msi_mode_show;
1c51b50c 518 ++count;
da8d1c8b
NH
519 }
520
1c51b50c
GKH
521 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
522 if (!msi_irq_group)
523 goto error_attrs;
524 msi_irq_group->name = "msi_irqs";
525 msi_irq_group->attrs = msi_attrs;
526
527 msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
528 if (!msi_irq_groups)
529 goto error_irq_group;
530 msi_irq_groups[0] = msi_irq_group;
531
532 ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
533 if (ret)
534 goto error_irq_groups;
535 pdev->msi_irq_groups = msi_irq_groups;
536
da8d1c8b
NH
537 return 0;
538
1c51b50c
GKH
539error_irq_groups:
540 kfree(msi_irq_groups);
541error_irq_group:
542 kfree(msi_irq_group);
543error_attrs:
544 count = 0;
545 msi_attr = msi_attrs[count];
546 while (msi_attr) {
547 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
548 kfree(msi_attr->name);
549 kfree(msi_dev_attr);
550 ++count;
551 msi_attr = msi_attrs[count];
da8d1c8b 552 }
29237756 553 kfree(msi_attrs);
da8d1c8b
NH
554 return ret;
555}
556
d873b4d4
YW
557static struct msi_desc *msi_setup_entry(struct pci_dev *dev)
558{
559 u16 control;
560 struct msi_desc *entry;
561
562 /* MSI Entry Initialization */
563 entry = alloc_msi_entry(dev);
564 if (!entry)
565 return NULL;
566
567 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
568
569 entry->msi_attrib.is_msix = 0;
570 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
571 entry->msi_attrib.entry_nr = 0;
572 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
573 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
d873b4d4
YW
574 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
575
576 if (control & PCI_MSI_FLAGS_64BIT)
577 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
578 else
579 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
580
581 /* Save the initial mask status */
582 if (entry->msi_attrib.maskbit)
583 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
584
585 return entry;
586}
587
1da177e4
LT
588/**
589 * msi_capability_init - configure device's MSI capability structure
590 * @dev: pointer to the pci_dev data structure of MSI device function
1c8d7b0a 591 * @nvec: number of interrupts to allocate
1da177e4 592 *
1c8d7b0a
MW
593 * Setup the MSI capability structure of the device with the requested
594 * number of interrupts. A return value of zero indicates the successful
595 * setup of an entry with the new MSI irq. A negative return value indicates
596 * an error, and a positive return value indicates the number of interrupts
597 * which could have been allocated.
598 */
599static int msi_capability_init(struct pci_dev *dev, int nvec)
1da177e4
LT
600{
601 struct msi_desc *entry;
f465136d 602 int ret;
f2440d9a 603 unsigned mask;
1da177e4 604
e375b561 605 msi_set_enable(dev, 0); /* Disable MSI during set up */
110828c9 606
d873b4d4 607 entry = msi_setup_entry(dev);
f7feaca7
EB
608 if (!entry)
609 return -ENOMEM;
1ce03373 610
f2440d9a 611 /* All MSIs are unmasked by default, Mask them all */
31ea5d4d 612 mask = msi_mask(entry->msi_attrib.multi_cap);
f2440d9a
MW
613 msi_mask_irq(entry, mask, mask);
614
0dd11f9b 615 list_add_tail(&entry->list, &dev->msi_list);
9c831334 616
1da177e4 617 /* Configure MSI capability structure */
1c8d7b0a 618 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
7fe3730d 619 if (ret) {
7ba1930d 620 msi_mask_irq(entry, mask, ~mask);
f56e4481 621 free_msi_irqs(dev);
7fe3730d 622 return ret;
fd58e55f 623 }
f7feaca7 624
da8d1c8b
NH
625 ret = populate_msi_sysfs(dev);
626 if (ret) {
627 msi_mask_irq(entry, mask, ~mask);
628 free_msi_irqs(dev);
629 return ret;
630 }
631
1da177e4 632 /* Set MSI enabled bits */
ba698ad4 633 pci_intx_for_msi(dev, 0);
e375b561 634 msi_set_enable(dev, 1);
b1cbf4e4 635 dev->msi_enabled = 1;
1da177e4 636
7fe3730d 637 dev->irq = entry->irq;
1da177e4
LT
638 return 0;
639}
640
520fe9dc 641static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
5a05a9d8 642{
4302e0fb 643 resource_size_t phys_addr;
5a05a9d8
HS
644 u32 table_offset;
645 u8 bir;
646
909094c6
BH
647 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
648 &table_offset);
4d18760c
BH
649 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
650 table_offset &= PCI_MSIX_TABLE_OFFSET;
5a05a9d8
HS
651 phys_addr = pci_resource_start(dev, bir) + table_offset;
652
653 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
654}
655
520fe9dc
GS
656static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
657 struct msix_entry *entries, int nvec)
d9d7070e
HS
658{
659 struct msi_desc *entry;
660 int i;
661
662 for (i = 0; i < nvec; i++) {
663 entry = alloc_msi_entry(dev);
664 if (!entry) {
665 if (!i)
666 iounmap(base);
667 else
668 free_msi_irqs(dev);
669 /* No enough memory. Don't try again */
670 return -ENOMEM;
671 }
672
673 entry->msi_attrib.is_msix = 1;
674 entry->msi_attrib.is_64 = 1;
675 entry->msi_attrib.entry_nr = entries[i].entry;
676 entry->msi_attrib.default_irq = dev->irq;
d9d7070e
HS
677 entry->mask_base = base;
678
679 list_add_tail(&entry->list, &dev->msi_list);
680 }
681
682 return 0;
683}
684
75cb3426 685static void msix_program_entries(struct pci_dev *dev,
520fe9dc 686 struct msix_entry *entries)
75cb3426
HS
687{
688 struct msi_desc *entry;
689 int i = 0;
690
691 list_for_each_entry(entry, &dev->msi_list, list) {
692 int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
693 PCI_MSIX_ENTRY_VECTOR_CTRL;
694
695 entries[i].vector = entry->irq;
dced35ae 696 irq_set_msi_desc(entry->irq, entry);
75cb3426
HS
697 entry->masked = readl(entry->mask_base + offset);
698 msix_mask_irq(entry, 1);
699 i++;
700 }
701}
702
1da177e4
LT
703/**
704 * msix_capability_init - configure device's MSI-X capability
705 * @dev: pointer to the pci_dev data structure of MSI-X device function
8f7020d3
RD
706 * @entries: pointer to an array of struct msix_entry entries
707 * @nvec: number of @entries
1da177e4 708 *
eaae4b3a 709 * Setup the MSI-X capability structure of device function with a
1ce03373
EB
710 * single MSI-X irq. A return of zero indicates the successful setup of
711 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
1da177e4
LT
712 **/
713static int msix_capability_init(struct pci_dev *dev,
714 struct msix_entry *entries, int nvec)
715{
520fe9dc 716 int ret;
5a05a9d8 717 u16 control;
1da177e4
LT
718 void __iomem *base;
719
f598282f 720 /* Ensure MSI-X is disabled while it is set up */
66f0d0c4 721 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
f598282f 722
66f0d0c4 723 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
1da177e4 724 /* Request & Map MSI-X table region */
527eee29 725 base = msix_map_region(dev, msix_table_size(control));
5a05a9d8 726 if (!base)
1da177e4
LT
727 return -ENOMEM;
728
520fe9dc 729 ret = msix_setup_entries(dev, base, entries, nvec);
d9d7070e
HS
730 if (ret)
731 return ret;
9c831334
ME
732
733 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
583871d4 734 if (ret)
2adc7907 735 goto out_avail;
9c831334 736
f598282f
MW
737 /*
738 * Some devices require MSI-X to be enabled before we can touch the
739 * MSI-X registers. We need to mask all the vectors to prevent
740 * interrupts coming in before they're fully set up.
741 */
66f0d0c4
YW
742 msix_clear_and_set_ctrl(dev, 0,
743 PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
f598282f 744
75cb3426 745 msix_program_entries(dev, entries);
f598282f 746
da8d1c8b 747 ret = populate_msi_sysfs(dev);
2adc7907
AG
748 if (ret)
749 goto out_free;
da8d1c8b 750
f598282f 751 /* Set MSI-X enabled bits and unmask the function */
ba698ad4 752 pci_intx_for_msi(dev, 0);
b1cbf4e4 753 dev->msix_enabled = 1;
1da177e4 754
66f0d0c4 755 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
8d181018 756
1da177e4 757 return 0;
583871d4 758
2adc7907 759out_avail:
583871d4
HS
760 if (ret < 0) {
761 /*
762 * If we had some success, report the number of irqs
763 * we succeeded in setting up.
764 */
d9d7070e 765 struct msi_desc *entry;
583871d4
HS
766 int avail = 0;
767
768 list_for_each_entry(entry, &dev->msi_list, list) {
769 if (entry->irq != 0)
770 avail++;
771 }
772 if (avail != 0)
773 ret = avail;
774 }
775
2adc7907 776out_free:
583871d4
HS
777 free_msi_irqs(dev);
778
779 return ret;
1da177e4
LT
780}
781
24334a12 782/**
a06cd74c 783 * pci_msi_supported - check whether MSI may be enabled on a device
24334a12 784 * @dev: pointer to the pci_dev data structure of MSI device function
c9953a73 785 * @nvec: how many MSIs have been requested ?
24334a12 786 *
f7625980 787 * Look at global flags, the device itself, and its parent buses
17bbc12a 788 * to determine if MSI/-X are supported for the device. If MSI/-X is
a06cd74c 789 * supported return 1, else return 0.
24334a12 790 **/
a06cd74c 791static int pci_msi_supported(struct pci_dev *dev, int nvec)
24334a12
BG
792{
793 struct pci_bus *bus;
794
0306ebfa 795 /* MSI must be globally enabled and supported by the device */
27e20603 796 if (!pci_msi_enable)
a06cd74c 797 return 0;
27e20603
AG
798
799 if (!dev || dev->no_msi || dev->current_state != PCI_D0)
a06cd74c 800 return 0;
24334a12 801
314e77b3
ME
802 /*
803 * You can't ask to have 0 or less MSIs configured.
804 * a) it's stupid ..
805 * b) the list manipulation code assumes nvec >= 1.
806 */
807 if (nvec < 1)
a06cd74c 808 return 0;
314e77b3 809
500559a9
HS
810 /*
811 * Any bridge which does NOT route MSI transactions from its
812 * secondary bus to its primary bus must set NO_MSI flag on
0306ebfa
BG
813 * the secondary pci_bus.
814 * We expect only arch-specific PCI host bus controller driver
815 * or quirks for specific PCI bridges to be setting NO_MSI.
816 */
24334a12
BG
817 for (bus = dev->bus; bus; bus = bus->parent)
818 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
a06cd74c 819 return 0;
24334a12 820
a06cd74c 821 return 1;
24334a12
BG
822}
823
d1ac1d26
AG
824/**
825 * pci_msi_vec_count - Return the number of MSI vectors a device can send
826 * @dev: device to report about
827 *
828 * This function returns the number of MSI vectors a device requested via
829 * Multiple Message Capable register. It returns a negative errno if the
830 * device is not capable sending MSI interrupts. Otherwise, the call succeeds
831 * and returns a power of two, up to a maximum of 2^5 (32), according to the
832 * MSI specification.
833 **/
834int pci_msi_vec_count(struct pci_dev *dev)
835{
836 int ret;
837 u16 msgctl;
838
839 if (!dev->msi_cap)
840 return -EINVAL;
841
842 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
843 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
844
845 return ret;
846}
847EXPORT_SYMBOL(pci_msi_vec_count);
848
f2440d9a 849void pci_msi_shutdown(struct pci_dev *dev)
1da177e4 850{
f2440d9a
MW
851 struct msi_desc *desc;
852 u32 mask;
1da177e4 853
128bc5fc 854 if (!pci_msi_enable || !dev || !dev->msi_enabled)
ded86d8d
EB
855 return;
856
110828c9
MW
857 BUG_ON(list_empty(&dev->msi_list));
858 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
110828c9 859
e375b561 860 msi_set_enable(dev, 0);
ba698ad4 861 pci_intx_for_msi(dev, 1);
b1cbf4e4 862 dev->msi_enabled = 0;
7bd007e4 863
12abb8ba 864 /* Return the device with MSI unmasked as initial states */
31ea5d4d 865 mask = msi_mask(desc->msi_attrib.multi_cap);
12abb8ba 866 /* Keep cached state to be restored */
03f56e42 867 __msi_mask_irq(desc, mask, ~mask);
e387b9ee
ME
868
869 /* Restore dev->irq to its default pin-assertion irq */
f2440d9a 870 dev->irq = desc->msi_attrib.default_irq;
d52877c7 871}
24d27553 872
500559a9 873void pci_disable_msi(struct pci_dev *dev)
d52877c7 874{
d52877c7
YL
875 if (!pci_msi_enable || !dev || !dev->msi_enabled)
876 return;
877
878 pci_msi_shutdown(dev);
f56e4481 879 free_msi_irqs(dev);
1da177e4 880}
4cc086fa 881EXPORT_SYMBOL(pci_disable_msi);
1da177e4 882
a52e2e35 883/**
ff1aa430 884 * pci_msix_vec_count - return the number of device's MSI-X table entries
a52e2e35 885 * @dev: pointer to the pci_dev data structure of MSI-X device function
ff1aa430
AG
886 * This function returns the number of device's MSI-X table entries and
887 * therefore the number of MSI-X vectors device is capable of sending.
888 * It returns a negative errno if the device is not capable of sending MSI-X
889 * interrupts.
890 **/
891int pci_msix_vec_count(struct pci_dev *dev)
a52e2e35 892{
a52e2e35
RW
893 u16 control;
894
520fe9dc 895 if (!dev->msix_cap)
ff1aa430 896 return -EINVAL;
a52e2e35 897
f84ecd28 898 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
527eee29 899 return msix_table_size(control);
a52e2e35 900}
ff1aa430 901EXPORT_SYMBOL(pci_msix_vec_count);
a52e2e35 902
1da177e4
LT
903/**
904 * pci_enable_msix - configure device's MSI-X capability structure
905 * @dev: pointer to the pci_dev data structure of MSI-X device function
70549ad9 906 * @entries: pointer to an array of MSI-X entries
1ce03373 907 * @nvec: number of MSI-X irqs requested for allocation by device driver
1da177e4
LT
908 *
909 * Setup the MSI-X capability structure of device function with the number
1ce03373 910 * of requested irqs upon its software driver call to request for
1da177e4
LT
911 * MSI-X mode enabled on its hardware device function. A return of zero
912 * indicates the successful configuration of MSI-X capability structure
1ce03373 913 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
1da177e4 914 * Or a return of > 0 indicates that driver request is exceeding the number
57fbf52c
MT
915 * of irqs or MSI-X vectors available. Driver should use the returned value to
916 * re-send its request.
1da177e4 917 **/
500559a9 918int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
1da177e4 919{
5ec09405 920 int nr_entries;
ded86d8d 921 int i, j;
1da177e4 922
a06cd74c
AG
923 if (!pci_msi_supported(dev, nvec))
924 return -EINVAL;
c9953a73 925
27e20603
AG
926 if (!entries)
927 return -EINVAL;
928
ff1aa430
AG
929 nr_entries = pci_msix_vec_count(dev);
930 if (nr_entries < 0)
931 return nr_entries;
1da177e4 932 if (nvec > nr_entries)
57fbf52c 933 return nr_entries;
1da177e4
LT
934
935 /* Check for any invalid entries */
936 for (i = 0; i < nvec; i++) {
937 if (entries[i].entry >= nr_entries)
938 return -EINVAL; /* invalid entry */
939 for (j = i + 1; j < nvec; j++) {
940 if (entries[i].entry == entries[j].entry)
941 return -EINVAL; /* duplicate entry */
942 }
943 }
ded86d8d 944 WARN_ON(!!dev->msix_enabled);
7bd007e4 945
1ce03373 946 /* Check whether driver already requested for MSI irq */
500559a9 947 if (dev->msi_enabled) {
227f0647 948 dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
1da177e4
LT
949 return -EINVAL;
950 }
5ec09405 951 return msix_capability_init(dev, entries, nvec);
1da177e4 952}
4cc086fa 953EXPORT_SYMBOL(pci_enable_msix);
1da177e4 954
500559a9 955void pci_msix_shutdown(struct pci_dev *dev)
fc4afc7b 956{
12abb8ba
HS
957 struct msi_desc *entry;
958
128bc5fc 959 if (!pci_msi_enable || !dev || !dev->msix_enabled)
ded86d8d
EB
960 return;
961
12abb8ba
HS
962 /* Return the device with MSI-X masked as initial states */
963 list_for_each_entry(entry, &dev->msi_list, list) {
964 /* Keep cached states to be restored */
03f56e42 965 __msix_mask_irq(entry, 1);
12abb8ba
HS
966 }
967
66f0d0c4 968 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
ba698ad4 969 pci_intx_for_msi(dev, 1);
b1cbf4e4 970 dev->msix_enabled = 0;
d52877c7 971}
c901851f 972
500559a9 973void pci_disable_msix(struct pci_dev *dev)
d52877c7
YL
974{
975 if (!pci_msi_enable || !dev || !dev->msix_enabled)
976 return;
977
978 pci_msix_shutdown(dev);
f56e4481 979 free_msi_irqs(dev);
1da177e4 980}
4cc086fa 981EXPORT_SYMBOL(pci_disable_msix);
1da177e4 982
309e57df
MW
983void pci_no_msi(void)
984{
985 pci_msi_enable = 0;
986}
c9953a73 987
07ae95f9
AP
988/**
989 * pci_msi_enabled - is MSI enabled?
990 *
991 * Returns true if MSI has not been disabled by the command-line option
992 * pci=nomsi.
993 **/
994int pci_msi_enabled(void)
d389fec6 995{
07ae95f9 996 return pci_msi_enable;
d389fec6 997}
07ae95f9 998EXPORT_SYMBOL(pci_msi_enabled);
d389fec6 999
07ae95f9 1000void pci_msi_init_pci_dev(struct pci_dev *dev)
d389fec6 1001{
07ae95f9 1002 INIT_LIST_HEAD(&dev->msi_list);
d5dea7d9
EB
1003
1004 /* Disable the msi hardware to avoid screaming interrupts
1005 * during boot. This is the power on reset default so
1006 * usually this should be a noop.
1007 */
e375b561
GS
1008 dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
1009 if (dev->msi_cap)
1010 msi_set_enable(dev, 0);
1011
1012 dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1013 if (dev->msix_cap)
66f0d0c4 1014 msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
d389fec6 1015}
302a2523
AG
1016
1017/**
1018 * pci_enable_msi_range - configure device's MSI capability structure
1019 * @dev: device to configure
1020 * @minvec: minimal number of interrupts to configure
1021 * @maxvec: maximum number of interrupts to configure
1022 *
1023 * This function tries to allocate a maximum possible number of interrupts in a
1024 * range between @minvec and @maxvec. It returns a negative errno if an error
1025 * occurs. If it succeeds, it returns the actual number of interrupts allocated
1026 * and updates the @dev's irq member to the lowest new interrupt number;
1027 * the other interrupt numbers allocated to this device are consecutive.
1028 **/
1029int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
1030{
034cd97e 1031 int nvec;
302a2523
AG
1032 int rc;
1033
a06cd74c
AG
1034 if (!pci_msi_supported(dev, minvec))
1035 return -EINVAL;
034cd97e
AG
1036
1037 WARN_ON(!!dev->msi_enabled);
1038
1039 /* Check whether driver already requested MSI-X irqs */
1040 if (dev->msix_enabled) {
1041 dev_info(&dev->dev,
1042 "can't enable MSI (MSI-X already enabled)\n");
1043 return -EINVAL;
1044 }
1045
302a2523
AG
1046 if (maxvec < minvec)
1047 return -ERANGE;
1048
034cd97e
AG
1049 nvec = pci_msi_vec_count(dev);
1050 if (nvec < 0)
1051 return nvec;
1052 else if (nvec < minvec)
1053 return -EINVAL;
1054 else if (nvec > maxvec)
1055 nvec = maxvec;
1056
302a2523 1057 do {
034cd97e 1058 rc = msi_capability_init(dev, nvec);
302a2523
AG
1059 if (rc < 0) {
1060 return rc;
1061 } else if (rc > 0) {
1062 if (rc < minvec)
1063 return -ENOSPC;
1064 nvec = rc;
1065 }
1066 } while (rc);
1067
1068 return nvec;
1069}
1070EXPORT_SYMBOL(pci_enable_msi_range);
1071
1072/**
1073 * pci_enable_msix_range - configure device's MSI-X capability structure
1074 * @dev: pointer to the pci_dev data structure of MSI-X device function
1075 * @entries: pointer to an array of MSI-X entries
1076 * @minvec: minimum number of MSI-X irqs requested
1077 * @maxvec: maximum number of MSI-X irqs requested
1078 *
1079 * Setup the MSI-X capability structure of device function with a maximum
1080 * possible number of interrupts in the range between @minvec and @maxvec
1081 * upon its software driver call to request for MSI-X mode enabled on its
1082 * hardware device function. It returns a negative errno if an error occurs.
1083 * If it succeeds, it returns the actual number of interrupts allocated and
1084 * indicates the successful configuration of MSI-X capability structure
1085 * with new allocated MSI-X interrupts.
1086 **/
1087int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1088 int minvec, int maxvec)
1089{
1090 int nvec = maxvec;
1091 int rc;
1092
1093 if (maxvec < minvec)
1094 return -ERANGE;
1095
1096 do {
1097 rc = pci_enable_msix(dev, entries, nvec);
1098 if (rc < 0) {
1099 return rc;
1100 } else if (rc > 0) {
1101 if (rc < minvec)
1102 return -ENOSPC;
1103 nvec = rc;
1104 }
1105 } while (rc);
1106
1107 return nvec;
1108}
1109EXPORT_SYMBOL(pci_enable_msix_range);