1
2
3
4
5
6
7
8
9
10#include <linux/err.h>
11#include <linux/mm.h>
12#include <linux/irq.h>
13#include <linux/interrupt.h>
14#include <linux/export.h>
15#include <linux/ioport.h>
16#include <linux/pci.h>
17#include <linux/proc_fs.h>
18#include <linux/msi.h>
19#include <linux/smp.h>
20#include <linux/errno.h>
21#include <linux/io.h>
22#include <linux/slab.h>
23
24#include "pci.h"
25
26static int pci_msi_enable = 1;
27
28#define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
29
30
31
32
33#if defined(CONFIG_GENERIC_HARDIRQS)
34int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
35{
36 struct msi_controller *chip = dev->bus->msi;
37 int err;
38
39 if (!chip || !chip->setup_irq)
40 return -EINVAL;
41
42 err = chip->setup_irq(chip, dev, desc);
43 if (err < 0)
44 return err;
45
46 irq_set_chip_data(desc->irq, chip);
47
48 return 0;
49}
50
51void __weak arch_teardown_msi_irq(unsigned int irq)
52{
53 struct msi_controller *chip = irq_get_chip_data(irq);
54
55 if (!chip || !chip->teardown_irq)
56 return;
57
58 chip->teardown_irq(chip, irq);
59}
60#else
61int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
62{
63 return -ENOSYS;
64}
65
66void __weak arch_teardown_msi_irq(unsigned int irq)
67{
68}
69#endif
70
71int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
72{
73 struct msi_desc *entry;
74 int ret;
75
76
77
78
79
80 if (type == PCI_CAP_ID_MSI && nvec > 1)
81 return 1;
82
83 list_for_each_entry(entry, &dev->msi_list, list) {
84 ret = arch_setup_msi_irq(dev, entry);
85 if (ret < 0)
86 return ret;
87 if (ret > 0)
88 return -ENOSPC;
89 }
90
91 return 0;
92}
93
94
95
96
97
98void default_teardown_msi_irqs(struct pci_dev *dev)
99{
100 int i;
101 struct msi_desc *entry;
102
103 list_for_each_entry(entry, &dev->msi_list, list)
104 if (entry->irq)
105 for (i = 0; i < entry->nvec_used; i++)
106 arch_teardown_msi_irq(entry->irq + i);
107}
108
109void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
110{
111 return default_teardown_msi_irqs(dev);
112}
113
114static void default_restore_msi_irq(struct pci_dev *dev, int irq)
115{
116 struct msi_desc *entry;
117
118 entry = NULL;
119 if (dev->msix_enabled) {
120 list_for_each_entry(entry, &dev->msi_list, list) {
121 if (irq == entry->irq)
122 break;
123 }
124 } else if (dev->msi_enabled) {
125 entry = irq_get_msi_desc(irq);
126 }
127
128 if (entry)
129 __write_msi_msg(entry, &entry->msg);
130}
131
132void __weak arch_restore_msi_irqs(struct pci_dev *dev)
133{
134 return default_restore_msi_irqs(dev);
135}
136
137static inline __attribute_const__ u32 msi_mask(unsigned x)
138{
139
140 if (x >= 5)
141 return 0xffffffff;
142 return (1 << (1 << x)) - 1;
143}
144
145
146
147
148
149
150
151u32 default_msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
152{
153 u32 mask_bits = desc->masked;
154
155 if (!desc->msi_attrib.maskbit)
156 return 0;
157
158 mask_bits &= ~mask;
159 mask_bits |= flag;
160 pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
161
162 return mask_bits;
163}
164
165__weak u32 arch_msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
166{
167 return default_msi_mask_irq(desc, mask, flag);
168}
169
170static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
171{
172 desc->masked = arch_msi_mask_irq(desc, mask, flag);
173}
174
175static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
176{
177 return desc->mask_base +
178 desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
179}
180
181
182
183
184
185
186
187
188u32 default_msix_mask_irq(struct msi_desc *desc, u32 flag)
189{
190 u32 mask_bits = desc->masked;
191
192 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
193 if (flag)
194 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
195 writel(mask_bits, pci_msix_desc_addr(desc) + PCI_MSIX_ENTRY_VECTOR_CTRL);
196
197 return mask_bits;
198}
199
200__weak u32 arch_msix_mask_irq(struct msi_desc *desc, u32 flag)
201{
202 return default_msix_mask_irq(desc, flag);
203}
204
205static void msix_mask_irq(struct msi_desc *desc, u32 flag)
206{
207 desc->masked = arch_msix_mask_irq(desc, flag);
208}
209
210#ifdef CONFIG_GENERIC_HARDIRQS
211
212static void msi_set_mask_bit(struct irq_data *data, u32 flag)
213{
214 struct msi_desc *desc = irq_data_get_msi(data);
215
216 if (desc->msi_attrib.is_msix) {
217 msix_mask_irq(desc, flag);
218 readl(desc->mask_base);
219 } else {
220 unsigned offset = data->irq - desc->irq;
221 msi_mask_irq(desc, 1 << offset, flag << offset);
222 }
223}
224
225void mask_msi_irq(struct irq_data *data)
226{
227 msi_set_mask_bit(data, 1);
228}
229
230void unmask_msi_irq(struct irq_data *data)
231{
232 msi_set_mask_bit(data, 0);
233}
234
235#endif
236
237void default_restore_msi_irqs(struct pci_dev *dev)
238{
239 struct msi_desc *entry;
240
241 list_for_each_entry(entry, &dev->msi_list, list)
242 default_restore_msi_irq(dev, entry->irq);
243}
244
245void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
246{
247 struct pci_dev *dev = entry->dev;
248
249 BUG_ON(dev->current_state != PCI_D0);
250
251 if (entry->msi_attrib.is_msix) {
252 void __iomem *base = pci_msix_desc_addr(entry);
253
254 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
255 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
256 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
257 } else {
258 int pos = dev->msi_cap;
259 u16 data;
260
261 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
262 &msg->address_lo);
263 if (entry->msi_attrib.is_64) {
264 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
265 &msg->address_hi);
266 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
267 } else {
268 msg->address_hi = 0;
269 pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
270 }
271 msg->data = data;
272 }
273}
274
275void read_msi_msg(unsigned int irq, struct msi_msg *msg)
276{
277 struct msi_desc *entry = irq_get_msi_desc(irq);
278
279 __read_msi_msg(entry, msg);
280}
281
282void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
283{
284
285
286 BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
287 entry->msg.data));
288
289 *msg = entry->msg;
290}
291
292void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
293{
294 struct msi_desc *entry = irq_get_msi_desc(irq);
295
296 __get_cached_msi_msg(entry, msg);
297}
298EXPORT_SYMBOL_GPL(get_cached_msi_msg);
299
300void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
301{
302 struct pci_dev *dev = entry->dev;
303
304 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
305
306 } else if (entry->msi_attrib.is_msix) {
307 void __iomem *base = pci_msix_desc_addr(entry);
308
309 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
310 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
311 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
312 } else {
313 int pos = dev->msi_cap;
314 u16 msgctl;
315
316 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
317 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
318 msgctl |= entry->msi_attrib.multiple << 4;
319 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
320
321 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
322 msg->address_lo);
323 if (entry->msi_attrib.is_64) {
324 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
325 msg->address_hi);
326 pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
327 msg->data);
328 } else {
329 pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
330 msg->data);
331 }
332 }
333 entry->msg = *msg;
334}
335
336void write_msi_msg(unsigned int irq, struct msi_msg *msg)
337{
338 struct msi_desc *entry = irq_get_msi_desc(irq);
339
340 __write_msi_msg(entry, msg);
341}
342EXPORT_SYMBOL_GPL(write_msi_msg);
343
344static void free_msi_irqs(struct pci_dev *dev)
345{
346 struct msi_desc *entry, *tmp;
347 struct attribute **msi_attrs;
348 struct device_attribute *dev_attr;
349 int count = 0;
350
351#ifdef CONFIG_GENERIC_HARDIRQS
352 int i;
353
354 list_for_each_entry(entry, &dev->msi_list, list)
355 if (entry->irq)
356 for (i = 0; i < entry->nvec_used; i++)
357 BUG_ON(irq_has_action(entry->irq + i));
358#endif
359
360 arch_teardown_msi_irqs(dev);
361
362 list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
363 if (entry->msi_attrib.is_msix) {
364 if (list_is_last(&entry->list, &dev->msi_list))
365 iounmap(entry->mask_base);
366 }
367
368 list_del(&entry->list);
369 kfree(entry->affinity);
370 kfree(entry);
371 }
372
373 if (dev->msi_irq_groups) {
374 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
375 msi_attrs = dev->msi_irq_groups[0]->attrs;
376 while (msi_attrs[count]) {
377 dev_attr = container_of(msi_attrs[count],
378 struct device_attribute, attr);
379 kfree(dev_attr->attr.name);
380 kfree(dev_attr);
381 ++count;
382 }
383 kfree(msi_attrs);
384 kfree(dev->msi_irq_groups[0]);
385 kfree(dev->msi_irq_groups);
386 dev->msi_irq_groups = NULL;
387 }
388}
389
390
391
392
393
394
395
396
397
398
399static struct msi_desc *alloc_msi_entry(struct pci_dev *dev, int nvec,
400 const struct cpumask *affinity)
401{
402 struct msi_desc *desc;
403
404 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
405 if (!desc)
406 return NULL;
407
408 INIT_LIST_HEAD(&desc->list);
409 desc->dev = dev;
410 desc->nvec_used = nvec;
411 if (affinity) {
412 desc->affinity = kmemdup(affinity,
413 nvec * sizeof(*desc->affinity), GFP_KERNEL);
414 if (!desc->affinity) {
415 kfree(desc);
416 return NULL;
417 }
418 }
419
420 return desc;
421}
422
423static void pci_intx_for_msi(struct pci_dev *dev, int enable)
424{
425 if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
426 pci_intx(dev, enable);
427}
428
429static void __pci_restore_msi_state(struct pci_dev *dev)
430{
431 u16 control;
432 struct msi_desc *entry;
433
434 if (!dev->msi_enabled)
435 return;
436
437 entry = irq_get_msi_desc(dev->irq);
438
439 pci_intx_for_msi(dev, 0);
440 pci_msi_set_enable(dev, 0);
441 arch_restore_msi_irqs(dev);
442
443 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
444 msi_mask_irq(entry, msi_mask(entry->multi_cap),
445 entry->masked);
446 control &= ~PCI_MSI_FLAGS_QSIZE;
447 control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
448 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
449}
450
451static void __pci_restore_msix_state(struct pci_dev *dev)
452{
453 struct msi_desc *entry;
454
455 if (!dev->msix_enabled)
456 return;
457 BUG_ON(list_empty(&dev->msi_list));
458
459
460 pci_intx_for_msi(dev, 0);
461 pci_msix_clear_and_set_ctrl(dev, 0,
462 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
463
464 arch_restore_msi_irqs(dev);
465 list_for_each_entry(entry, &dev->msi_list, list)
466 msix_mask_irq(entry, entry->masked);
467
468 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
469}
470
471void pci_restore_msi_state(struct pci_dev *dev)
472{
473 __pci_restore_msi_state(dev);
474 __pci_restore_msix_state(dev);
475}
476EXPORT_SYMBOL_GPL(pci_restore_msi_state);
477
478static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
479 char *buf)
480{
481 struct msi_desc *entry;
482 unsigned long irq;
483 int retval;
484
485 retval = kstrtoul(attr->attr.name, 10, &irq);
486 if (retval)
487 return retval;
488
489 entry = irq_get_msi_desc(irq);
490 if (entry)
491 return sprintf(buf, "%s\n",
492 entry->msi_attrib.is_msix ? "msix" : "msi");
493
494 return -ENODEV;
495}
496
497static int populate_msi_sysfs(struct pci_dev *pdev)
498{
499 struct attribute **msi_attrs;
500 struct attribute *msi_attr;
501 struct device_attribute *msi_dev_attr;
502 struct attribute_group *msi_irq_group;
503 const struct attribute_group **msi_irq_groups;
504 struct msi_desc *entry;
505 int ret = -ENOMEM;
506 int num_msi = 0;
507 int count = 0;
508 int i;
509
510
511 list_for_each_entry(entry, &pdev->msi_list, list)
512 num_msi += entry->nvec_used;
513 if (!num_msi)
514 return 0;
515
516
517 msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
518 if (!msi_attrs)
519 return -ENOMEM;
520 list_for_each_entry(entry, &pdev->msi_list, list) {
521 for (i = 0; i < entry->nvec_used; i++) {
522 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
523 if (!msi_dev_attr)
524 goto error_attrs;
525 msi_attrs[count] = &msi_dev_attr->attr;
526
527 sysfs_attr_init(&msi_dev_attr->attr);
528 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
529 entry->irq + i);
530 if (!msi_dev_attr->attr.name)
531 goto error_attrs;
532 msi_dev_attr->attr.mode = S_IRUGO;
533 msi_dev_attr->show = msi_mode_show;
534 ++count;
535 }
536 }
537
538 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
539 if (!msi_irq_group)
540 goto error_attrs;
541 msi_irq_group->name = "msi_irqs";
542 msi_irq_group->attrs = msi_attrs;
543
544 msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
545 if (!msi_irq_groups)
546 goto error_irq_group;
547 msi_irq_groups[0] = msi_irq_group;
548
549 ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
550 if (ret)
551 goto error_irq_groups;
552 pdev->msi_irq_groups = msi_irq_groups;
553
554 return 0;
555
556error_irq_groups:
557 kfree(msi_irq_groups);
558error_irq_group:
559 kfree(msi_irq_group);
560error_attrs:
561 count = 0;
562 msi_attr = msi_attrs[count];
563 while (msi_attr) {
564 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
565 kfree(msi_attr->name);
566 kfree(msi_dev_attr);
567 ++count;
568 msi_attr = msi_attrs[count];
569 }
570 kfree(msi_attrs);
571 return ret;
572}
573
574static struct msi_desc *
575msi_setup_entry(struct pci_dev *dev, int nvec, const struct irq_affinity *affd)
576{
577 struct cpumask *masks = NULL;
578 struct msi_desc *entry;
579 u16 control;
580
581 if (affd)
582 masks = irq_create_affinity_masks(nvec, affd);
583
584
585 entry = alloc_msi_entry(dev, nvec, masks);
586 if (!entry)
587 goto out;
588
589 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
590
591 entry->msi_attrib.is_msix = 0;
592 entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
593 entry->msi_attrib.entry_nr = 0;
594 entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
595 entry->msi_attrib.default_irq = dev->irq;
596 entry->msi_attrib.pos = dev->msi_cap;
597 entry->multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
598 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
599
600 if (control & PCI_MSI_FLAGS_64BIT)
601 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
602 else
603 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
604
605
606 if (entry->msi_attrib.maskbit)
607 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
608
609out:
610 kfree(masks);
611 return entry;
612}
613
614static int msi_verify_entries(struct pci_dev *dev)
615{
616 struct msi_desc *entry;
617
618 list_for_each_entry(entry, &dev->msi_list, list) {
619 if (!dev->no_64bit_msi || !entry->msg.address_hi)
620 continue;
621 dev_err(&dev->dev, "Device has broken 64-bit MSI but arch"
622 " tried to assign one above 4G\n");
623 return -EIO;
624 }
625 return 0;
626}
627
628
629
630
631
632
633
634
635
636
637
638
639
640static int msi_capability_init(struct pci_dev *dev, int nvec,
641 const struct irq_affinity *affd)
642{
643 struct msi_desc *entry;
644 int ret;
645 unsigned mask;
646
647 pci_msi_set_enable(dev, 0);
648
649 entry = msi_setup_entry(dev, nvec, affd);
650 if (!entry)
651 return -ENOMEM;
652
653
654 mask = msi_mask(entry->multi_cap);
655 msi_mask_irq(entry, mask, mask);
656
657 list_add_tail(&entry->list, &dev->msi_list);
658
659
660 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
661 if (ret) {
662 msi_mask_irq(entry, mask, ~mask);
663 free_msi_irqs(dev);
664 return ret;
665 }
666
667 ret = msi_verify_entries(dev);
668 if (ret) {
669 msi_mask_irq(entry, mask, ~mask);
670 free_msi_irqs(dev);
671 return ret;
672 }
673
674 ret = populate_msi_sysfs(dev);
675 if (ret) {
676 msi_mask_irq(entry, mask, ~mask);
677 free_msi_irqs(dev);
678 return ret;
679 }
680
681
682 pci_intx_for_msi(dev, 0);
683 pci_msi_set_enable(dev, 1);
684 dev->msi_enabled = 1;
685
686 pcibios_free_irq(dev);
687 dev->irq = entry->irq;
688 return 0;
689}
690
691static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
692{
693 resource_size_t phys_addr;
694 u32 table_offset;
695 unsigned long flags;
696 u8 bir;
697
698 pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
699 &table_offset);
700 bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
701 flags = pci_resource_flags(dev, bir);
702 if (!flags || (flags & IORESOURCE_UNSET))
703 return NULL;
704
705 table_offset &= PCI_MSIX_TABLE_OFFSET;
706 phys_addr = pci_resource_start(dev, bir) + table_offset;
707
708 return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
709}
710
711static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
712 struct msix_entry *entries, int nvec,
713 const struct irq_affinity *affd)
714{
715 struct cpumask *curmsk, *masks = NULL;
716 struct msi_desc *entry;
717 int ret, i;
718
719 if (affd)
720 masks = irq_create_affinity_masks(nvec, affd);
721
722 for (i = 0, curmsk = masks; i < nvec; i++) {
723 entry = alloc_msi_entry(dev, 1, curmsk);
724 if (!entry) {
725 if (!i)
726 iounmap(base);
727 else
728 free_msi_irqs(dev);
729
730 ret = -ENOMEM;
731 goto out;
732 }
733
734 entry->msi_attrib.is_msix = 1;
735 entry->msi_attrib.is_64 = 1;
736 if (entries)
737 entry->msi_attrib.entry_nr = entries[i].entry;
738 else
739 entry->msi_attrib.entry_nr = i;
740 entry->msi_attrib.default_irq = dev->irq;
741 entry->mask_base = base;
742
743 list_add_tail(&entry->list, &dev->msi_list);
744 if (masks)
745 curmsk++;
746 }
747 ret = 0;
748 out:
749 kfree(masks);
750 return ret;
751}
752
753static void msix_program_entries(struct pci_dev *dev,
754 struct msix_entry *entries)
755{
756 struct msi_desc *entry;
757 int i = 0;
758
759 list_for_each_entry(entry, &dev->msi_list, list) {
760 if (entries)
761 entries[i++].vector = entry->irq;
762 irq_set_msi_desc(entry->irq, entry);
763 entry->masked = readl(pci_msix_desc_addr(entry) +
764 PCI_MSIX_ENTRY_VECTOR_CTRL);
765 msix_mask_irq(entry, 1);
766 }
767}
768
769
770
771
772
773
774
775
776
777
778
779
780static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
781 int nvec, const struct irq_affinity *affd)
782{
783 int ret;
784 u16 control;
785 void __iomem *base;
786
787
788 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
789
790 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
791
792 base = msix_map_region(dev, msix_table_size(control));
793 if (!base)
794 return -ENOMEM;
795
796 ret = msix_setup_entries(dev, base, entries, nvec, affd);
797 if (ret)
798 return ret;
799
800 ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
801 if (ret)
802 goto out_avail;
803
804
805 ret = msi_verify_entries(dev);
806 if (ret)
807 goto out_free;
808
809
810
811
812
813
814 pci_msix_clear_and_set_ctrl(dev, 0,
815 PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
816
817 msix_program_entries(dev, entries);
818
819 ret = populate_msi_sysfs(dev);
820 if (ret)
821 goto out_free;
822
823
824 pci_intx_for_msi(dev, 0);
825 dev->msix_enabled = 1;
826 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
827
828 pcibios_free_irq(dev);
829 return 0;
830
831out_avail:
832 if (ret < 0) {
833
834
835
836
837 struct msi_desc *entry;
838 int avail = 0;
839
840 list_for_each_entry(entry, &dev->msi_list, list) {
841 if (entry->irq != 0)
842 avail++;
843 }
844 if (avail != 0)
845 ret = avail;
846 }
847
848out_free:
849 free_msi_irqs(dev);
850
851 return ret;
852}
853
854
855
856
857
858
859
860
861
862
863static int pci_msi_supported(struct pci_dev *dev, int nvec)
864{
865 struct pci_bus *bus;
866
867
868 if (!pci_msi_enable)
869 return 0;
870
871 if (!dev || dev->no_msi || dev->current_state != PCI_D0)
872 return 0;
873
874
875
876
877
878
879 if (nvec < 1)
880 return 0;
881
882
883
884
885
886
887
888
889 for (bus = dev->bus; bus; bus = bus->parent)
890 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
891 return 0;
892
893 return 1;
894}
895
896
897
898
899
900
901
902
903
904
905
906int pci_msi_vec_count(struct pci_dev *dev)
907{
908 int ret;
909 u16 msgctl;
910
911 if (!dev->msi_cap)
912 return -EINVAL;
913
914 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
915 ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
916
917 return ret;
918}
919EXPORT_SYMBOL(pci_msi_vec_count);
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934int pci_enable_msi_block(struct pci_dev *dev, int nvec)
935{
936 int status, maxvec;
937
938 if (dev->current_state != PCI_D0)
939 return -EINVAL;
940
941 maxvec = pci_msi_vec_count(dev);
942 if (maxvec < 0)
943 return maxvec;
944 if (nvec > maxvec)
945 return maxvec;
946
947 if (!pci_msi_supported(dev, nvec))
948 return -EINVAL;
949
950 WARN_ON(!!dev->msi_enabled);
951
952
953 if (dev->msix_enabled) {
954 dev_info(&dev->dev, "can't enable MSI "
955 "(MSI-X already enabled)\n");
956 return -EINVAL;
957 }
958
959 status = msi_capability_init(dev, nvec, false);
960 return status;
961}
962EXPORT_SYMBOL(pci_enable_msi_block);
963
964void pci_msi_shutdown(struct pci_dev *dev)
965{
966 struct msi_desc *desc;
967 u32 mask;
968
969 if (!pci_msi_enable || !dev || !dev->msi_enabled)
970 return;
971
972 BUG_ON(list_empty(&dev->msi_list));
973 desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
974
975 pci_msi_set_enable(dev, 0);
976 pci_intx_for_msi(dev, 1);
977 dev->msi_enabled = 0;
978
979
980 mask = msi_mask(desc->multi_cap);
981
982 arch_msi_mask_irq(desc, mask, ~mask);
983
984
985 dev->irq = desc->msi_attrib.default_irq;
986 pcibios_alloc_irq(dev);
987}
988
989void pci_disable_msi(struct pci_dev *dev)
990{
991 if (!pci_msi_enable || !dev || !dev->msi_enabled)
992 return;
993
994 pci_msi_shutdown(dev);
995 free_msi_irqs(dev);
996}
997EXPORT_SYMBOL(pci_disable_msi);
998
999
1000
1001
1002
1003
1004
1005
1006
1007int pci_msix_vec_count(struct pci_dev *dev)
1008{
1009 u16 control;
1010
1011 if (!dev->msix_cap)
1012 return -EINVAL;
1013
1014 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
1015 return msix_table_size(control);
1016}
1017EXPORT_SYMBOL(pci_msix_vec_count);
1018
1019static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
1020 int nvec, const struct irq_affinity *affd)
1021{
1022 int nr_entries;
1023 int i, j;
1024
1025 if (!pci_msi_supported(dev, nvec))
1026 return -EINVAL;
1027
1028 nr_entries = pci_msix_vec_count(dev);
1029 if (nr_entries < 0)
1030 return nr_entries;
1031 if (nvec > nr_entries)
1032 return nr_entries;
1033
1034 if (entries) {
1035
1036 for (i = 0; i < nvec; i++) {
1037 if (entries[i].entry >= nr_entries)
1038 return -EINVAL;
1039 for (j = i + 1; j < nvec; j++) {
1040 if (entries[i].entry == entries[j].entry)
1041 return -EINVAL;
1042 }
1043 }
1044 }
1045 WARN_ON(!!dev->msix_enabled);
1046
1047
1048 if (dev->msi_enabled) {
1049 dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
1050 return -EINVAL;
1051 }
1052 return msix_capability_init(dev, entries, nvec, affd);
1053}
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
1071{
1072 return __pci_enable_msix(dev, entries, nvec, NULL);
1073}
1074EXPORT_SYMBOL(pci_enable_msix);
1075
1076void pci_msix_shutdown(struct pci_dev *dev)
1077{
1078 struct msi_desc *entry;
1079
1080 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1081 return;
1082
1083 if (pci_dev_is_disconnected(dev)) {
1084 dev->msix_enabled = 0;
1085 return;
1086 }
1087
1088
1089 list_for_each_entry(entry, &dev->msi_list, list) {
1090
1091 arch_msix_mask_irq(entry, 1);
1092 }
1093
1094 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1095 pci_intx_for_msi(dev, 1);
1096 dev->msix_enabled = 0;
1097 pcibios_alloc_irq(dev);
1098}
1099
1100void pci_disable_msix(struct pci_dev *dev)
1101{
1102 if (!pci_msi_enable || !dev || !dev->msix_enabled)
1103 return;
1104
1105 pci_msix_shutdown(dev);
1106 free_msi_irqs(dev);
1107}
1108EXPORT_SYMBOL(pci_disable_msix);
1109
1110void pci_no_msi(void)
1111{
1112 pci_msi_enable = 0;
1113}
1114
1115
1116
1117
1118
1119
1120
1121int pci_msi_enabled(void)
1122{
1123 return pci_msi_enable;
1124}
1125EXPORT_SYMBOL(pci_msi_enabled);
1126
1127void pci_msi_init_pci_dev(struct pci_dev *dev)
1128{
1129 INIT_LIST_HEAD(&dev->msi_list);
1130}
1131
1132static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
1133 const struct irq_affinity *affd)
1134{
1135 int nvec;
1136 int rc;
1137
1138 if (!pci_msi_supported(dev, minvec))
1139 return -EINVAL;
1140
1141 WARN_ON(!!dev->msi_enabled);
1142
1143
1144 if (dev->msix_enabled) {
1145 dev_info(&dev->dev,
1146 "can't enable MSI (MSI-X already enabled)\n");
1147 return -EINVAL;
1148 }
1149
1150 if (maxvec < minvec)
1151 return -ERANGE;
1152
1153 nvec = pci_msi_vec_count(dev);
1154 if (nvec < 0)
1155 return nvec;
1156 if (nvec < minvec)
1157 return -ENOSPC;
1158
1159 if (nvec > maxvec)
1160 nvec = maxvec;
1161
1162 for (;;) {
1163 if (affd) {
1164 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1165 if (nvec < minvec)
1166 return -ENOSPC;
1167 }
1168
1169 rc = msi_capability_init(dev, nvec, affd);
1170 if (rc == 0)
1171 return nvec;
1172
1173 if (rc < 0)
1174 return rc;
1175 if (rc < minvec)
1176 return -ENOSPC;
1177
1178 nvec = rc;
1179 }
1180}
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
1195{
1196 return __pci_enable_msi_range(dev, minvec, maxvec, NULL);
1197}
1198EXPORT_SYMBOL(pci_enable_msi_range);
1199
1200static int __pci_enable_msix_range(struct pci_dev *dev,
1201 struct msix_entry *entries, int minvec,
1202 int maxvec, const struct irq_affinity *affd)
1203{
1204 int rc, nvec = maxvec;
1205
1206 if (maxvec < minvec)
1207 return -ERANGE;
1208
1209 for (;;) {
1210 if (affd) {
1211 nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1212 if (nvec < minvec)
1213 return -ENOSPC;
1214 }
1215
1216 rc = __pci_enable_msix(dev, entries, nvec, affd);
1217 if (rc == 0)
1218 return nvec;
1219
1220 if (rc < 0)
1221 return rc;
1222 if (rc < minvec)
1223 return -ENOSPC;
1224
1225 nvec = rc;
1226 }
1227}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1245 int minvec, int maxvec)
1246{
1247 return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
1248}
1249EXPORT_SYMBOL(pci_enable_msix_range);
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1270 unsigned int max_vecs, unsigned int flags,
1271 const struct irq_affinity *affd)
1272{
1273 static const struct irq_affinity msi_default_affd;
1274 int vecs = -ENOSPC;
1275
1276 if (flags & PCI_IRQ_AFFINITY) {
1277 if (!affd)
1278 affd = &msi_default_affd;
1279 } else {
1280 if (WARN_ON(affd))
1281 affd = NULL;
1282 }
1283
1284 if (flags & PCI_IRQ_MSIX) {
1285 vecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
1286 affd);
1287 if (vecs > 0)
1288 return vecs;
1289 }
1290
1291 if (flags & PCI_IRQ_MSI) {
1292 vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
1293 if (vecs > 0)
1294 return vecs;
1295 }
1296
1297
1298 if (flags & PCI_IRQ_LEGACY) {
1299 if (min_vecs == 1 && dev->irq) {
1300 pci_intx(dev, 1);
1301 return 1;
1302 }
1303 }
1304
1305 return vecs;
1306}
1307EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1308
1309
1310
1311
1312
1313
1314
1315void pci_free_irq_vectors(struct pci_dev *dev)
1316{
1317 pci_disable_msix(dev);
1318 pci_disable_msi(dev);
1319}
1320EXPORT_SYMBOL(pci_free_irq_vectors);
1321
1322
1323
1324
1325
1326
1327int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1328{
1329 if (dev->msix_enabled) {
1330 struct msi_desc *entry;
1331 int i = 0;
1332
1333 list_for_each_entry(entry, &dev->msi_list, list) {
1334 if (i == nr)
1335 return entry->irq;
1336 i++;
1337 }
1338 WARN_ON_ONCE(1);
1339 return -EINVAL;
1340 }
1341
1342 if (dev->msi_enabled) {
1343 struct msi_desc *entry = list_first_entry(&dev->msi_list,
1344 struct msi_desc,
1345 list);
1346
1347 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1348 return -EINVAL;
1349 } else {
1350 if (WARN_ON_ONCE(nr > 0))
1351 return -EINVAL;
1352 }
1353
1354 return dev->irq + nr;
1355}
1356EXPORT_SYMBOL(pci_irq_vector);
1357
1358
1359
1360
1361
1362
1363const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1364{
1365 if (dev->msix_enabled) {
1366 struct msi_desc *entry;
1367 int i = 0;
1368
1369 list_for_each_entry(entry, &dev->msi_list, list) {
1370 if (i == nr)
1371 return entry->affinity;
1372 i++;
1373 }
1374 WARN_ON_ONCE(1);
1375 return NULL;
1376 } else if (dev->msi_enabled) {
1377 struct msi_desc *entry = list_first_entry(&dev->msi_list,
1378 struct msi_desc,
1379 list);
1380 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1381 nr >= entry->nvec_used))
1382 return NULL;
1383
1384 return &entry->affinity[nr];
1385 } else {
1386 return cpu_possible_mask;
1387 }
1388}
1389EXPORT_SYMBOL(pci_irq_get_affinity);
1390
1391
1392
1393
1394
1395
1396int pci_irq_get_node(struct pci_dev *pdev, int vec)
1397{
1398 const struct cpumask *mask;
1399
1400 mask = pci_irq_get_affinity(pdev, vec);
1401 if (mask)
1402 return local_memory_node(cpu_to_node(cpumask_first(mask)));
1403 return dev_to_node(&pdev->dev);
1404}
1405EXPORT_SYMBOL(pci_irq_get_node);
1406