GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / pci / msi.c
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  * Copyright (C) 2016 Christoph Hellwig.
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/acpi_iort.h>
23 #include <linux/slab.h>
24 #include <linux/irqdomain.h>
25 #include <linux/of_irq.h>
26
27 #include "pci.h"
28
29 static int pci_msi_enable = 1;
30 int pci_msi_ignore_mask;
31
32 #define msix_table_size(flags)  ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
33
34 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
35 static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
36 {
37         struct irq_domain *domain;
38
39         domain = dev_get_msi_domain(&dev->dev);
40         if (domain && irq_domain_is_hierarchy(domain))
41                 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
42
43         return arch_setup_msi_irqs(dev, nvec, type);
44 }
45
46 static void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
47 {
48         struct irq_domain *domain;
49
50         domain = dev_get_msi_domain(&dev->dev);
51         if (domain && irq_domain_is_hierarchy(domain))
52                 msi_domain_free_irqs(domain, &dev->dev);
53         else
54                 arch_teardown_msi_irqs(dev);
55 }
56 #else
57 #define pci_msi_setup_msi_irqs          arch_setup_msi_irqs
58 #define pci_msi_teardown_msi_irqs       arch_teardown_msi_irqs
59 #endif
60
61 /* Arch hooks */
62
63 int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
64 {
65         struct msi_controller *chip = dev->bus->msi;
66         int err;
67
68         if (!chip || !chip->setup_irq)
69                 return -EINVAL;
70
71         err = chip->setup_irq(chip, dev, desc);
72         if (err < 0)
73                 return err;
74
75         irq_set_chip_data(desc->irq, chip);
76
77         return 0;
78 }
79
80 void __weak arch_teardown_msi_irq(unsigned int irq)
81 {
82         struct msi_controller *chip = irq_get_chip_data(irq);
83
84         if (!chip || !chip->teardown_irq)
85                 return;
86
87         chip->teardown_irq(chip, irq);
88 }
89
90 int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
91 {
92         struct msi_controller *chip = dev->bus->msi;
93         struct msi_desc *entry;
94         int ret;
95
96         if (chip && chip->setup_irqs)
97                 return chip->setup_irqs(chip, dev, nvec, type);
98         /*
99          * If an architecture wants to support multiple MSI, it needs to
100          * override arch_setup_msi_irqs()
101          */
102         if (type == PCI_CAP_ID_MSI && nvec > 1)
103                 return 1;
104
105         for_each_pci_msi_entry(entry, dev) {
106                 ret = arch_setup_msi_irq(dev, entry);
107                 if (ret < 0)
108                         return ret;
109                 if (ret > 0)
110                         return -ENOSPC;
111         }
112
113         return 0;
114 }
115
116 /*
117  * We have a default implementation available as a separate non-weak
118  * function, as it is used by the Xen x86 PCI code
119  */
120 void default_teardown_msi_irqs(struct pci_dev *dev)
121 {
122         int i;
123         struct msi_desc *entry;
124
125         for_each_pci_msi_entry(entry, dev)
126                 if (entry->irq)
127                         for (i = 0; i < entry->nvec_used; i++)
128                                 arch_teardown_msi_irq(entry->irq + i);
129 }
130
131 void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
132 {
133         return default_teardown_msi_irqs(dev);
134 }
135
136 static void default_restore_msi_irq(struct pci_dev *dev, int irq)
137 {
138         struct msi_desc *entry;
139
140         entry = NULL;
141         if (dev->msix_enabled) {
142                 for_each_pci_msi_entry(entry, dev) {
143                         if (irq == entry->irq)
144                                 break;
145                 }
146         } else if (dev->msi_enabled)  {
147                 entry = irq_get_msi_desc(irq);
148         }
149
150         if (entry)
151                 __pci_write_msi_msg(entry, &entry->msg);
152 }
153
154 void __weak arch_restore_msi_irqs(struct pci_dev *dev)
155 {
156         return default_restore_msi_irqs(dev);
157 }
158
159 static inline __attribute_const__ u32 msi_mask(unsigned x)
160 {
161         /* Don't shift by >= width of type */
162         if (x >= 5)
163                 return 0xffffffff;
164         return (1 << (1 << x)) - 1;
165 }
166
167 /*
168  * PCI 2.3 does not specify mask bits for each MSI interrupt.  Attempting to
169  * mask all MSI interrupts by clearing the MSI enable bit does not work
170  * reliably as devices without an INTx disable bit will then generate a
171  * level IRQ which will never be cleared.
172  */
173 void __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
174 {
175         raw_spinlock_t *lock = &desc->dev->msi_lock;
176         unsigned long flags;
177
178         if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
179                 return;
180
181         raw_spin_lock_irqsave(lock, flags);
182         desc->masked &= ~mask;
183         desc->masked |= flag;
184         pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
185                                desc->masked);
186         raw_spin_unlock_irqrestore(lock, flags);
187 }
188
189 static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
190 {
191         __pci_msi_desc_mask_irq(desc, mask, flag);
192 }
193
194 static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
195 {
196         return desc->mask_base +
197                 desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
198 }
199
200 /*
201  * This internal function does not flush PCI writes to the device.
202  * All users must ensure that they read from the device before either
203  * assuming that the device state is up to date, or returning out of this
204  * file.  This saves a few milliseconds when initialising devices with lots
205  * of MSI-X interrupts.
206  */
207 u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag)
208 {
209         u32 mask_bits = desc->masked;
210
211         if (pci_msi_ignore_mask)
212                 return 0;
213
214         mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
215         if (flag & PCI_MSIX_ENTRY_CTRL_MASKBIT)
216                 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
217         writel(mask_bits, pci_msix_desc_addr(desc) + PCI_MSIX_ENTRY_VECTOR_CTRL);
218
219         return mask_bits;
220 }
221
222 static void msix_mask_irq(struct msi_desc *desc, u32 flag)
223 {
224         desc->masked = __pci_msix_desc_mask_irq(desc, flag);
225 }
226
227 static void msi_set_mask_bit(struct irq_data *data, u32 flag)
228 {
229         struct msi_desc *desc = irq_data_get_msi_desc(data);
230
231         if (desc->msi_attrib.is_msix) {
232                 msix_mask_irq(desc, flag);
233                 readl(desc->mask_base);         /* Flush write to device */
234         } else {
235                 unsigned offset = data->irq - desc->irq;
236                 msi_mask_irq(desc, 1 << offset, flag << offset);
237         }
238 }
239
240 /**
241  * pci_msi_mask_irq - Generic irq chip callback to mask PCI/MSI interrupts
242  * @data:       pointer to irqdata associated to that interrupt
243  */
244 void pci_msi_mask_irq(struct irq_data *data)
245 {
246         msi_set_mask_bit(data, 1);
247 }
248 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
249
250 /**
251  * pci_msi_unmask_irq - Generic irq chip callback to unmask PCI/MSI interrupts
252  * @data:       pointer to irqdata associated to that interrupt
253  */
254 void pci_msi_unmask_irq(struct irq_data *data)
255 {
256         msi_set_mask_bit(data, 0);
257 }
258 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
259
260 void default_restore_msi_irqs(struct pci_dev *dev)
261 {
262         struct msi_desc *entry;
263
264         for_each_pci_msi_entry(entry, dev)
265                 default_restore_msi_irq(dev, entry->irq);
266 }
267
268 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
269 {
270         struct pci_dev *dev = msi_desc_to_pci_dev(entry);
271
272         BUG_ON(dev->current_state != PCI_D0);
273
274         if (entry->msi_attrib.is_msix) {
275                 void __iomem *base = pci_msix_desc_addr(entry);
276
277                 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
278                 msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
279                 msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
280         } else {
281                 int pos = dev->msi_cap;
282                 u16 data;
283
284                 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
285                                       &msg->address_lo);
286                 if (entry->msi_attrib.is_64) {
287                         pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
288                                               &msg->address_hi);
289                         pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
290                 } else {
291                         msg->address_hi = 0;
292                         pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
293                 }
294                 msg->data = data;
295         }
296 }
297
298 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
299 {
300         struct pci_dev *dev = msi_desc_to_pci_dev(entry);
301
302         if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
303                 /* Don't touch the hardware now */
304         } else if (entry->msi_attrib.is_msix) {
305                 void __iomem *base = pci_msix_desc_addr(entry);
306                 bool unmasked = !(entry->masked & PCI_MSIX_ENTRY_CTRL_MASKBIT);
307
308                 /*
309                  * The specification mandates that the entry is masked
310                  * when the message is modified:
311                  *
312                  * "If software changes the Address or Data value of an
313                  * entry while the entry is unmasked, the result is
314                  * undefined."
315                  */
316                 if (unmasked)
317                         __pci_msix_desc_mask_irq(entry, PCI_MSIX_ENTRY_CTRL_MASKBIT);
318
319                 writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
320                 writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
321                 writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
322
323                 if (unmasked)
324                         __pci_msix_desc_mask_irq(entry, 0);
325
326                 /* Ensure that the writes are visible in the device */
327                 readl(base + PCI_MSIX_ENTRY_DATA);
328         } else {
329                 int pos = dev->msi_cap;
330                 u16 msgctl;
331
332                 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
333                 msgctl &= ~PCI_MSI_FLAGS_QSIZE;
334                 msgctl |= entry->msi_attrib.multiple << 4;
335                 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
336
337                 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
338                                        msg->address_lo);
339                 if (entry->msi_attrib.is_64) {
340                         pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
341                                                msg->address_hi);
342                         pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
343                                               msg->data);
344                 } else {
345                         pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
346                                               msg->data);
347                 }
348                 /* Ensure that the writes are visible in the device */
349                 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
350         }
351         entry->msg = *msg;
352 }
353
354 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
355 {
356         struct msi_desc *entry = irq_get_msi_desc(irq);
357
358         __pci_write_msi_msg(entry, msg);
359 }
360 EXPORT_SYMBOL_GPL(pci_write_msi_msg);
361
362 static void free_msi_irqs(struct pci_dev *dev)
363 {
364         struct list_head *msi_list = dev_to_msi_list(&dev->dev);
365         struct msi_desc *entry, *tmp;
366         struct attribute **msi_attrs;
367         struct device_attribute *dev_attr;
368         int i, count = 0;
369
370         for_each_pci_msi_entry(entry, dev)
371                 if (entry->irq)
372                         for (i = 0; i < entry->nvec_used; i++)
373                                 BUG_ON(irq_has_action(entry->irq + i));
374
375         if (dev->msi_irq_groups) {
376                 sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
377                 msi_attrs = dev->msi_irq_groups[0]->attrs;
378                 while (msi_attrs[count]) {
379                         dev_attr = container_of(msi_attrs[count],
380                                                 struct device_attribute, attr);
381                         kfree(dev_attr->attr.name);
382                         kfree(dev_attr);
383                         ++count;
384                 }
385                 kfree(msi_attrs);
386                 kfree(dev->msi_irq_groups[0]);
387                 kfree(dev->msi_irq_groups);
388                 dev->msi_irq_groups = NULL;
389         }
390
391         pci_msi_teardown_msi_irqs(dev);
392
393         list_for_each_entry_safe(entry, tmp, msi_list, list) {
394                 if (entry->msi_attrib.is_msix) {
395                         if (list_is_last(&entry->list, msi_list))
396                                 iounmap(entry->mask_base);
397                 }
398
399                 list_del(&entry->list);
400                 free_msi_entry(entry);
401         }
402 }
403
404 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
405 {
406         if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
407                 pci_intx(dev, enable);
408 }
409
410 static void __pci_restore_msi_state(struct pci_dev *dev)
411 {
412         u16 control;
413         struct msi_desc *entry;
414
415         if (!dev->msi_enabled)
416                 return;
417
418         entry = irq_get_msi_desc(dev->irq);
419
420         pci_intx_for_msi(dev, 0);
421         pci_msi_set_enable(dev, 0);
422         arch_restore_msi_irqs(dev);
423
424         pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
425         msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
426                      entry->masked);
427         control &= ~PCI_MSI_FLAGS_QSIZE;
428         control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
429         pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
430 }
431
432 static void __pci_restore_msix_state(struct pci_dev *dev)
433 {
434         struct msi_desc *entry;
435
436         if (!dev->msix_enabled)
437                 return;
438         BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
439
440         /* route the table */
441         pci_intx_for_msi(dev, 0);
442         pci_msix_clear_and_set_ctrl(dev, 0,
443                                 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
444
445         arch_restore_msi_irqs(dev);
446         for_each_pci_msi_entry(entry, dev)
447                 msix_mask_irq(entry, entry->masked);
448
449         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
450 }
451
452 void pci_restore_msi_state(struct pci_dev *dev)
453 {
454         __pci_restore_msi_state(dev);
455         __pci_restore_msix_state(dev);
456 }
457 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
458
459 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
460                              char *buf)
461 {
462         struct msi_desc *entry;
463         unsigned long irq;
464         int retval;
465
466         retval = kstrtoul(attr->attr.name, 10, &irq);
467         if (retval)
468                 return retval;
469
470         entry = irq_get_msi_desc(irq);
471         if (entry)
472                 return sprintf(buf, "%s\n",
473                                 entry->msi_attrib.is_msix ? "msix" : "msi");
474
475         return -ENODEV;
476 }
477
478 static int populate_msi_sysfs(struct pci_dev *pdev)
479 {
480         struct attribute **msi_attrs;
481         struct attribute *msi_attr;
482         struct device_attribute *msi_dev_attr;
483         struct attribute_group *msi_irq_group;
484         const struct attribute_group **msi_irq_groups;
485         struct msi_desc *entry;
486         int ret = -ENOMEM;
487         int num_msi = 0;
488         int count = 0;
489         int i;
490
491         /* Determine how many msi entries we have */
492         for_each_pci_msi_entry(entry, pdev)
493                 num_msi += entry->nvec_used;
494         if (!num_msi)
495                 return 0;
496
497         /* Dynamically create the MSI attributes for the PCI device */
498         msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
499         if (!msi_attrs)
500                 return -ENOMEM;
501         for_each_pci_msi_entry(entry, pdev) {
502                 for (i = 0; i < entry->nvec_used; i++) {
503                         msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
504                         if (!msi_dev_attr)
505                                 goto error_attrs;
506                         msi_attrs[count] = &msi_dev_attr->attr;
507
508                         sysfs_attr_init(&msi_dev_attr->attr);
509                         msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
510                                                             entry->irq + i);
511                         if (!msi_dev_attr->attr.name)
512                                 goto error_attrs;
513                         msi_dev_attr->attr.mode = S_IRUGO;
514                         msi_dev_attr->show = msi_mode_show;
515                         ++count;
516                 }
517         }
518
519         msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
520         if (!msi_irq_group)
521                 goto error_attrs;
522         msi_irq_group->name = "msi_irqs";
523         msi_irq_group->attrs = msi_attrs;
524
525         msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
526         if (!msi_irq_groups)
527                 goto error_irq_group;
528         msi_irq_groups[0] = msi_irq_group;
529
530         ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
531         if (ret)
532                 goto error_irq_groups;
533         pdev->msi_irq_groups = msi_irq_groups;
534
535         return 0;
536
537 error_irq_groups:
538         kfree(msi_irq_groups);
539 error_irq_group:
540         kfree(msi_irq_group);
541 error_attrs:
542         count = 0;
543         msi_attr = msi_attrs[count];
544         while (msi_attr) {
545                 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
546                 kfree(msi_attr->name);
547                 kfree(msi_dev_attr);
548                 ++count;
549                 msi_attr = msi_attrs[count];
550         }
551         kfree(msi_attrs);
552         return ret;
553 }
554
555 static struct msi_desc *
556 msi_setup_entry(struct pci_dev *dev, int nvec, const struct irq_affinity *affd)
557 {
558         struct cpumask *masks = NULL;
559         struct msi_desc *entry;
560         u16 control;
561
562         if (affd)
563                 masks = irq_create_affinity_masks(nvec, affd);
564
565
566         /* MSI Entry Initialization */
567         entry = alloc_msi_entry(&dev->dev, nvec, masks);
568         if (!entry)
569                 goto out;
570
571         pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
572
573         entry->msi_attrib.is_msix       = 0;
574         entry->msi_attrib.is_64         = !!(control & PCI_MSI_FLAGS_64BIT);
575         entry->msi_attrib.entry_nr      = 0;
576         entry->msi_attrib.maskbit       = !!(control & PCI_MSI_FLAGS_MASKBIT);
577         entry->msi_attrib.default_irq   = dev->irq;     /* Save IOAPIC IRQ */
578         entry->msi_attrib.multi_cap     = (control & PCI_MSI_FLAGS_QMASK) >> 1;
579         entry->msi_attrib.multiple      = ilog2(__roundup_pow_of_two(nvec));
580
581         if (control & PCI_MSI_FLAGS_64BIT)
582                 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
583         else
584                 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
585
586         /* Save the initial mask status */
587         if (entry->msi_attrib.maskbit)
588                 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
589
590 out:
591         kfree(masks);
592         return entry;
593 }
594
595 static int msi_verify_entries(struct pci_dev *dev)
596 {
597         struct msi_desc *entry;
598
599         for_each_pci_msi_entry(entry, dev) {
600                 if (!dev->no_64bit_msi || !entry->msg.address_hi)
601                         continue;
602                 dev_err(&dev->dev, "Device has broken 64-bit MSI but arch"
603                         " tried to assign one above 4G\n");
604                 return -EIO;
605         }
606         return 0;
607 }
608
609 /**
610  * msi_capability_init - configure device's MSI capability structure
611  * @dev: pointer to the pci_dev data structure of MSI device function
612  * @nvec: number of interrupts to allocate
613  * @affd: description of automatic irq affinity assignments (may be %NULL)
614  *
615  * Setup the MSI capability structure of the device with the requested
616  * number of interrupts.  A return value of zero indicates the successful
617  * setup of an entry with the new MSI irq.  A negative return value indicates
618  * an error, and a positive return value indicates the number of interrupts
619  * which could have been allocated.
620  */
621 static int msi_capability_init(struct pci_dev *dev, int nvec,
622                                const struct irq_affinity *affd)
623 {
624         struct msi_desc *entry;
625         int ret;
626         unsigned mask;
627
628         pci_msi_set_enable(dev, 0);     /* Disable MSI during set up */
629
630         entry = msi_setup_entry(dev, nvec, affd);
631         if (!entry)
632                 return -ENOMEM;
633
634         /* All MSIs are unmasked by default, Mask them all */
635         mask = msi_mask(entry->msi_attrib.multi_cap);
636         msi_mask_irq(entry, mask, mask);
637
638         list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
639
640         /* Configure MSI capability structure */
641         ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
642         if (ret) {
643                 msi_mask_irq(entry, mask, 0);
644                 free_msi_irqs(dev);
645                 return ret;
646         }
647
648         ret = msi_verify_entries(dev);
649         if (ret) {
650                 msi_mask_irq(entry, mask, 0);
651                 free_msi_irqs(dev);
652                 return ret;
653         }
654
655         ret = populate_msi_sysfs(dev);
656         if (ret) {
657                 msi_mask_irq(entry, mask, 0);
658                 free_msi_irqs(dev);
659                 return ret;
660         }
661
662         /* Set MSI enabled bits  */
663         pci_intx_for_msi(dev, 0);
664         pci_msi_set_enable(dev, 1);
665         dev->msi_enabled = 1;
666
667         pcibios_free_irq(dev);
668         dev->irq = entry->irq;
669         return 0;
670 }
671
672 static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
673 {
674         resource_size_t phys_addr;
675         u32 table_offset;
676         unsigned long flags;
677         u8 bir;
678
679         pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
680                               &table_offset);
681         bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
682         flags = pci_resource_flags(dev, bir);
683         if (!flags || (flags & IORESOURCE_UNSET))
684                 return NULL;
685
686         table_offset &= PCI_MSIX_TABLE_OFFSET;
687         phys_addr = pci_resource_start(dev, bir) + table_offset;
688
689         return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
690 }
691
692 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
693                               struct msix_entry *entries, int nvec,
694                               const struct irq_affinity *affd)
695 {
696         struct cpumask *curmsk, *masks = NULL;
697         struct msi_desc *entry;
698         void __iomem *addr;
699         int ret, i;
700
701         if (affd)
702                 masks = irq_create_affinity_masks(nvec, affd);
703
704         for (i = 0, curmsk = masks; i < nvec; i++) {
705                 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
706                 if (!entry) {
707                         if (!i)
708                                 iounmap(base);
709                         else
710                                 free_msi_irqs(dev);
711                         /* No enough memory. Don't try again */
712                         ret = -ENOMEM;
713                         goto out;
714                 }
715
716                 entry->msi_attrib.is_msix       = 1;
717                 entry->msi_attrib.is_64         = 1;
718
719                 if (entries)
720                         entry->msi_attrib.entry_nr = entries[i].entry;
721                 else
722                         entry->msi_attrib.entry_nr = i;
723                 entry->msi_attrib.default_irq   = dev->irq;
724                 entry->mask_base                = base;
725
726                 addr = pci_msix_desc_addr(entry);
727                 if (addr)
728                         entry->masked = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
729
730                 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
731                 if (masks)
732                         curmsk++;
733         }
734         ret = 0;
735 out:
736         kfree(masks);
737         return ret;
738 }
739
740 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
741 {
742         struct msi_desc *entry;
743
744         for_each_pci_msi_entry(entry, dev) {
745                 if (entries) {
746                         entries->vector = entry->irq;
747                         entries++;
748                 }
749         }
750 }
751
752 static void msix_mask_all(void __iomem *base, int tsize)
753 {
754         u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
755         int i;
756
757         if (pci_msi_ignore_mask)
758                 return;
759
760         for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
761                 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
762 }
763
764 /**
765  * msix_capability_init - configure device's MSI-X capability
766  * @dev: pointer to the pci_dev data structure of MSI-X device function
767  * @entries: pointer to an array of struct msix_entry entries
768  * @nvec: number of @entries
769  * @affd: Optional pointer to enable automatic affinity assignement
770  *
771  * Setup the MSI-X capability structure of device function with a
772  * single MSI-X irq. A return of zero indicates the successful setup of
773  * requested MSI-X entries with allocated irqs or non-zero for otherwise.
774  **/
775 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
776                                 int nvec, const struct irq_affinity *affd)
777 {
778         void __iomem *base;
779         int ret, tsize;
780         u16 control;
781
782         /*
783          * Some devices require MSI-X to be enabled before the MSI-X
784          * registers can be accessed.  Mask all the vectors to prevent
785          * interrupts coming in before they're fully set up.
786          */
787         pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
788                                     PCI_MSIX_FLAGS_ENABLE);
789
790         pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
791         /* Request & Map MSI-X table region */
792         tsize = msix_table_size(control);
793         base = msix_map_region(dev, tsize);
794         if (!base) {
795                 ret = -ENOMEM;
796                 goto out_disable;
797         }
798
799         ret = msix_setup_entries(dev, base, entries, nvec, affd);
800         if (ret)
801                 goto out_disable;
802
803         ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
804         if (ret)
805                 goto out_avail;
806
807         /* Check if all MSI entries honor device restrictions */
808         ret = msi_verify_entries(dev);
809         if (ret)
810                 goto out_free;
811
812         msix_update_entries(dev, entries);
813
814         ret = populate_msi_sysfs(dev);
815         if (ret)
816                 goto out_free;
817
818         /* Set MSI-X enabled bits and unmask the function */
819         pci_intx_for_msi(dev, 0);
820         dev->msix_enabled = 1;
821
822         /*
823          * Ensure that all table entries are masked to prevent
824          * stale entries from firing in a crash kernel.
825          *
826          * Done late to deal with a broken Marvell NVME device
827          * which takes the MSI-X mask bits into account even
828          * when MSI-X is disabled, which prevents MSI delivery.
829          */
830         msix_mask_all(base, tsize);
831         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
832
833         pcibios_free_irq(dev);
834         return 0;
835
836 out_avail:
837         if (ret < 0) {
838                 /*
839                  * If we had some success, report the number of irqs
840                  * we succeeded in setting up.
841                  */
842                 struct msi_desc *entry;
843                 int avail = 0;
844
845                 for_each_pci_msi_entry(entry, dev) {
846                         if (entry->irq != 0)
847                                 avail++;
848                 }
849                 if (avail != 0)
850                         ret = avail;
851         }
852
853 out_free:
854         free_msi_irqs(dev);
855
856 out_disable:
857         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
858
859         return ret;
860 }
861
862 /**
863  * pci_msi_supported - check whether MSI may be enabled on a device
864  * @dev: pointer to the pci_dev data structure of MSI device function
865  * @nvec: how many MSIs have been requested ?
866  *
867  * Look at global flags, the device itself, and its parent buses
868  * to determine if MSI/-X are supported for the device. If MSI/-X is
869  * supported return 1, else return 0.
870  **/
871 static int pci_msi_supported(struct pci_dev *dev, int nvec)
872 {
873         struct pci_bus *bus;
874
875         /* MSI must be globally enabled and supported by the device */
876         if (!pci_msi_enable)
877                 return 0;
878
879         if (!dev || dev->no_msi || dev->current_state != PCI_D0)
880                 return 0;
881
882         /*
883          * You can't ask to have 0 or less MSIs configured.
884          *  a) it's stupid ..
885          *  b) the list manipulation code assumes nvec >= 1.
886          */
887         if (nvec < 1)
888                 return 0;
889
890         /*
891          * Any bridge which does NOT route MSI transactions from its
892          * secondary bus to its primary bus must set NO_MSI flag on
893          * the secondary pci_bus.
894          * We expect only arch-specific PCI host bus controller driver
895          * or quirks for specific PCI bridges to be setting NO_MSI.
896          */
897         for (bus = dev->bus; bus; bus = bus->parent)
898                 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
899                         return 0;
900
901         return 1;
902 }
903
904 /**
905  * pci_msi_vec_count - Return the number of MSI vectors a device can send
906  * @dev: device to report about
907  *
908  * This function returns the number of MSI vectors a device requested via
909  * Multiple Message Capable register. It returns a negative errno if the
910  * device is not capable sending MSI interrupts. Otherwise, the call succeeds
911  * and returns a power of two, up to a maximum of 2^5 (32), according to the
912  * MSI specification.
913  **/
914 int pci_msi_vec_count(struct pci_dev *dev)
915 {
916         int ret;
917         u16 msgctl;
918
919         if (!dev->msi_cap)
920                 return -EINVAL;
921
922         pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
923         ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
924
925         return ret;
926 }
927 EXPORT_SYMBOL(pci_msi_vec_count);
928
929 static void pci_msi_shutdown(struct pci_dev *dev)
930 {
931         struct msi_desc *desc;
932         u32 mask;
933
934         if (!pci_msi_enable || !dev || !dev->msi_enabled)
935                 return;
936
937         BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
938         desc = first_pci_msi_entry(dev);
939
940         pci_msi_set_enable(dev, 0);
941         pci_intx_for_msi(dev, 1);
942         dev->msi_enabled = 0;
943
944         /* Return the device with MSI unmasked as initial states */
945         mask = msi_mask(desc->msi_attrib.multi_cap);
946         msi_mask_irq(desc, mask, 0);
947
948         /* Restore dev->irq to its default pin-assertion irq */
949         dev->irq = desc->msi_attrib.default_irq;
950         pcibios_alloc_irq(dev);
951 }
952
953 void pci_disable_msi(struct pci_dev *dev)
954 {
955         if (!pci_msi_enable || !dev || !dev->msi_enabled)
956                 return;
957
958         pci_msi_shutdown(dev);
959         free_msi_irqs(dev);
960 }
961 EXPORT_SYMBOL(pci_disable_msi);
962
963 /**
964  * pci_msix_vec_count - return the number of device's MSI-X table entries
965  * @dev: pointer to the pci_dev data structure of MSI-X device function
966  * This function returns the number of device's MSI-X table entries and
967  * therefore the number of MSI-X vectors device is capable of sending.
968  * It returns a negative errno if the device is not capable of sending MSI-X
969  * interrupts.
970  **/
971 int pci_msix_vec_count(struct pci_dev *dev)
972 {
973         u16 control;
974
975         if (!dev->msix_cap)
976                 return -EINVAL;
977
978         pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
979         return msix_table_size(control);
980 }
981 EXPORT_SYMBOL(pci_msix_vec_count);
982
983 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
984                              int nvec, const struct irq_affinity *affd)
985 {
986         int nr_entries;
987         int i, j;
988
989         if (!pci_msi_supported(dev, nvec))
990                 return -EINVAL;
991
992         nr_entries = pci_msix_vec_count(dev);
993         if (nr_entries < 0)
994                 return nr_entries;
995         if (nvec > nr_entries)
996                 return nr_entries;
997
998         if (entries) {
999                 /* Check for any invalid entries */
1000                 for (i = 0; i < nvec; i++) {
1001                         if (entries[i].entry >= nr_entries)
1002                                 return -EINVAL;         /* invalid entry */
1003                         for (j = i + 1; j < nvec; j++) {
1004                                 if (entries[i].entry == entries[j].entry)
1005                                         return -EINVAL; /* duplicate entry */
1006                         }
1007                 }
1008         }
1009
1010         /* Check whether driver already requested for MSI irq */
1011         if (dev->msi_enabled) {
1012                 dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
1013                 return -EINVAL;
1014         }
1015         return msix_capability_init(dev, entries, nvec, affd);
1016 }
1017
1018 static void pci_msix_shutdown(struct pci_dev *dev)
1019 {
1020         struct msi_desc *entry;
1021
1022         if (!pci_msi_enable || !dev || !dev->msix_enabled)
1023                 return;
1024
1025         if (pci_dev_is_disconnected(dev)) {
1026                 dev->msix_enabled = 0;
1027                 return;
1028         }
1029
1030         /* Return the device with MSI-X masked as initial states */
1031         for_each_pci_msi_entry(entry, dev)
1032                 __pci_msix_desc_mask_irq(entry, 1);
1033
1034         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1035         pci_intx_for_msi(dev, 1);
1036         dev->msix_enabled = 0;
1037         pcibios_alloc_irq(dev);
1038 }
1039
1040 void pci_disable_msix(struct pci_dev *dev)
1041 {
1042         if (!pci_msi_enable || !dev || !dev->msix_enabled)
1043                 return;
1044
1045         pci_msix_shutdown(dev);
1046         free_msi_irqs(dev);
1047 }
1048 EXPORT_SYMBOL(pci_disable_msix);
1049
1050 void pci_no_msi(void)
1051 {
1052         pci_msi_enable = 0;
1053 }
1054
1055 /**
1056  * pci_msi_enabled - is MSI enabled?
1057  *
1058  * Returns true if MSI has not been disabled by the command-line option
1059  * pci=nomsi.
1060  **/
1061 int pci_msi_enabled(void)
1062 {
1063         return pci_msi_enable;
1064 }
1065 EXPORT_SYMBOL(pci_msi_enabled);
1066
1067 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
1068                                   const struct irq_affinity *affd)
1069 {
1070         int nvec;
1071         int rc;
1072
1073         if (!pci_msi_supported(dev, minvec))
1074                 return -EINVAL;
1075
1076         /* Check whether driver already requested MSI-X irqs */
1077         if (dev->msix_enabled) {
1078                 dev_info(&dev->dev,
1079                          "can't enable MSI (MSI-X already enabled)\n");
1080                 return -EINVAL;
1081         }
1082
1083         if (maxvec < minvec)
1084                 return -ERANGE;
1085
1086         if (WARN_ON_ONCE(dev->msi_enabled))
1087                 return -EINVAL;
1088
1089         nvec = pci_msi_vec_count(dev);
1090         if (nvec < 0)
1091                 return nvec;
1092         if (nvec < minvec)
1093                 return -ENOSPC;
1094
1095         if (nvec > maxvec)
1096                 nvec = maxvec;
1097
1098         for (;;) {
1099                 if (affd) {
1100                         nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1101                         if (nvec < minvec)
1102                                 return -ENOSPC;
1103                 }
1104
1105                 rc = msi_capability_init(dev, nvec, affd);
1106                 if (rc == 0)
1107                         return nvec;
1108
1109                 if (rc < 0)
1110                         return rc;
1111                 if (rc < minvec)
1112                         return -ENOSPC;
1113
1114                 nvec = rc;
1115         }
1116 }
1117
1118 /* deprecated, don't use */
1119 int pci_enable_msi(struct pci_dev *dev)
1120 {
1121         int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1122         if (rc < 0)
1123                 return rc;
1124         return 0;
1125 }
1126 EXPORT_SYMBOL(pci_enable_msi);
1127
1128 static int __pci_enable_msix_range(struct pci_dev *dev,
1129                                    struct msix_entry *entries, int minvec,
1130                                    int maxvec, const struct irq_affinity *affd)
1131 {
1132         int rc, nvec = maxvec;
1133
1134         if (maxvec < minvec)
1135                 return -ERANGE;
1136
1137         if (WARN_ON_ONCE(dev->msix_enabled))
1138                 return -EINVAL;
1139
1140         for (;;) {
1141                 if (affd) {
1142                         nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1143                         if (nvec < minvec)
1144                                 return -ENOSPC;
1145                 }
1146
1147                 rc = __pci_enable_msix(dev, entries, nvec, affd);
1148                 if (rc == 0)
1149                         return nvec;
1150
1151                 if (rc < 0)
1152                         return rc;
1153                 if (rc < minvec)
1154                         return -ENOSPC;
1155
1156                 nvec = rc;
1157         }
1158 }
1159
1160 /**
1161  * pci_enable_msix_range - configure device's MSI-X capability structure
1162  * @dev: pointer to the pci_dev data structure of MSI-X device function
1163  * @entries: pointer to an array of MSI-X entries
1164  * @minvec: minimum number of MSI-X irqs requested
1165  * @maxvec: maximum number of MSI-X irqs requested
1166  *
1167  * Setup the MSI-X capability structure of device function with a maximum
1168  * possible number of interrupts in the range between @minvec and @maxvec
1169  * upon its software driver call to request for MSI-X mode enabled on its
1170  * hardware device function. It returns a negative errno if an error occurs.
1171  * If it succeeds, it returns the actual number of interrupts allocated and
1172  * indicates the successful configuration of MSI-X capability structure
1173  * with new allocated MSI-X interrupts.
1174  **/
1175 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1176                 int minvec, int maxvec)
1177 {
1178         return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
1179 }
1180 EXPORT_SYMBOL(pci_enable_msix_range);
1181
1182 /**
1183  * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1184  * @dev:                PCI device to operate on
1185  * @min_vecs:           minimum number of vectors required (must be >= 1)
1186  * @max_vecs:           maximum (desired) number of vectors
1187  * @flags:              flags or quirks for the allocation
1188  * @affd:               optional description of the affinity requirements
1189  *
1190  * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1191  * vectors if available, and fall back to a single legacy vector
1192  * if neither is available.  Return the number of vectors allocated,
1193  * (which might be smaller than @max_vecs) if successful, or a negative
1194  * error code on error. If less than @min_vecs interrupt vectors are
1195  * available for @dev the function will fail with -ENOSPC.
1196  *
1197  * To get the Linux IRQ number used for a vector that can be passed to
1198  * request_irq() use the pci_irq_vector() helper.
1199  */
1200 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1201                                    unsigned int max_vecs, unsigned int flags,
1202                                    const struct irq_affinity *affd)
1203 {
1204         static const struct irq_affinity msi_default_affd;
1205         int msix_vecs = -ENOSPC;
1206         int msi_vecs = -ENOSPC;
1207
1208         if (flags & PCI_IRQ_AFFINITY) {
1209                 if (!affd)
1210                         affd = &msi_default_affd;
1211         } else {
1212                 if (WARN_ON(affd))
1213                         affd = NULL;
1214         }
1215
1216         if (flags & PCI_IRQ_MSIX) {
1217                 msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs,
1218                                                     max_vecs, affd);
1219                 if (msix_vecs > 0)
1220                         return msix_vecs;
1221         }
1222
1223         if (flags & PCI_IRQ_MSI) {
1224                 msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs,
1225                                                   affd);
1226                 if (msi_vecs > 0)
1227                         return msi_vecs;
1228         }
1229
1230         /* use legacy irq if allowed */
1231         if (flags & PCI_IRQ_LEGACY) {
1232                 if (min_vecs == 1 && dev->irq) {
1233                         pci_intx(dev, 1);
1234                         return 1;
1235                 }
1236         }
1237
1238         if (msix_vecs == -ENOSPC)
1239                 return -ENOSPC;
1240         return msi_vecs;
1241 }
1242 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1243
1244 /**
1245  * pci_free_irq_vectors - free previously allocated IRQs for a device
1246  * @dev:                PCI device to operate on
1247  *
1248  * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1249  */
1250 void pci_free_irq_vectors(struct pci_dev *dev)
1251 {
1252         pci_disable_msix(dev);
1253         pci_disable_msi(dev);
1254 }
1255 EXPORT_SYMBOL(pci_free_irq_vectors);
1256
1257 /**
1258  * pci_irq_vector - return Linux IRQ number of a device vector
1259  * @dev: PCI device to operate on
1260  * @nr: device-relative interrupt vector index (0-based).
1261  */
1262 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1263 {
1264         if (dev->msix_enabled) {
1265                 struct msi_desc *entry;
1266                 int i = 0;
1267
1268                 for_each_pci_msi_entry(entry, dev) {
1269                         if (i == nr)
1270                                 return entry->irq;
1271                         i++;
1272                 }
1273                 WARN_ON_ONCE(1);
1274                 return -EINVAL;
1275         }
1276
1277         if (dev->msi_enabled) {
1278                 struct msi_desc *entry = first_pci_msi_entry(dev);
1279
1280                 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1281                         return -EINVAL;
1282         } else {
1283                 if (WARN_ON_ONCE(nr > 0))
1284                         return -EINVAL;
1285         }
1286
1287         return dev->irq + nr;
1288 }
1289 EXPORT_SYMBOL(pci_irq_vector);
1290
1291 /**
1292  * pci_irq_get_affinity - return the affinity of a particular msi vector
1293  * @dev:        PCI device to operate on
1294  * @nr:         device-relative interrupt vector index (0-based).
1295  */
1296 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1297 {
1298         if (dev->msix_enabled) {
1299                 struct msi_desc *entry;
1300                 int i = 0;
1301
1302                 for_each_pci_msi_entry(entry, dev) {
1303                         if (i == nr)
1304                                 return entry->affinity;
1305                         i++;
1306                 }
1307                 WARN_ON_ONCE(1);
1308                 return NULL;
1309         } else if (dev->msi_enabled) {
1310                 struct msi_desc *entry = first_pci_msi_entry(dev);
1311
1312                 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1313                                  nr >= entry->nvec_used))
1314                         return NULL;
1315
1316                 return &entry->affinity[nr];
1317         } else {
1318                 return cpu_possible_mask;
1319         }
1320 }
1321 EXPORT_SYMBOL(pci_irq_get_affinity);
1322
1323 /**
1324  * pci_irq_get_node - return the numa node of a particular msi vector
1325  * @pdev:       PCI device to operate on
1326  * @vec:        device-relative interrupt vector index (0-based).
1327  */
1328 int pci_irq_get_node(struct pci_dev *pdev, int vec)
1329 {
1330         const struct cpumask *mask;
1331
1332         mask = pci_irq_get_affinity(pdev, vec);
1333         if (mask)
1334                 return local_memory_node(cpu_to_node(cpumask_first(mask)));
1335         return dev_to_node(&pdev->dev);
1336 }
1337 EXPORT_SYMBOL(pci_irq_get_node);
1338
1339 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1340 {
1341         return to_pci_dev(desc->dev);
1342 }
1343 EXPORT_SYMBOL(msi_desc_to_pci_dev);
1344
1345 void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1346 {
1347         struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1348
1349         return dev->bus->sysdata;
1350 }
1351 EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1352
1353 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1354 /**
1355  * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1356  * @irq_data:   Pointer to interrupt data of the MSI interrupt
1357  * @msg:        Pointer to the message
1358  */
1359 void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1360 {
1361         struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
1362
1363         /*
1364          * For MSI-X desc->irq is always equal to irq_data->irq. For
1365          * MSI only the first interrupt of MULTI MSI passes the test.
1366          */
1367         if (desc->irq == irq_data->irq)
1368                 __pci_write_msi_msg(desc, msg);
1369 }
1370
1371 /**
1372  * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1373  * @dev:        Pointer to the PCI device
1374  * @desc:       Pointer to the msi descriptor
1375  *
1376  * The ID number is only used within the irqdomain.
1377  */
1378 irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
1379                                           struct msi_desc *desc)
1380 {
1381         return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1382                 PCI_DEVID(dev->bus->number, dev->devfn) << 11 |
1383                 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1384 }
1385
1386 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1387 {
1388         return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1389 }
1390
1391 /**
1392  * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
1393  * @domain:     The interrupt domain to check
1394  * @info:       The domain info for verification
1395  * @dev:        The device to check
1396  *
1397  * Returns:
1398  *  0 if the functionality is supported
1399  *  1 if Multi MSI is requested, but the domain does not support it
1400  *  -ENOTSUPP otherwise
1401  */
1402 int pci_msi_domain_check_cap(struct irq_domain *domain,
1403                              struct msi_domain_info *info, struct device *dev)
1404 {
1405         struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1406
1407         /* Special handling to support __pci_enable_msi_range() */
1408         if (pci_msi_desc_is_multi_msi(desc) &&
1409             !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1410                 return 1;
1411         else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1412                 return -ENOTSUPP;
1413
1414         return 0;
1415 }
1416
1417 static int pci_msi_domain_handle_error(struct irq_domain *domain,
1418                                        struct msi_desc *desc, int error)
1419 {
1420         /* Special handling to support __pci_enable_msi_range() */
1421         if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1422                 return 1;
1423
1424         return error;
1425 }
1426
1427 #ifdef GENERIC_MSI_DOMAIN_OPS
1428 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1429                                     struct msi_desc *desc)
1430 {
1431         arg->desc = desc;
1432         arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
1433                                                desc);
1434 }
1435 #else
1436 #define pci_msi_domain_set_desc         NULL
1437 #endif
1438
1439 static struct msi_domain_ops pci_msi_domain_ops_default = {
1440         .set_desc       = pci_msi_domain_set_desc,
1441         .msi_check      = pci_msi_domain_check_cap,
1442         .handle_error   = pci_msi_domain_handle_error,
1443 };
1444
1445 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1446 {
1447         struct msi_domain_ops *ops = info->ops;
1448
1449         if (ops == NULL) {
1450                 info->ops = &pci_msi_domain_ops_default;
1451         } else {
1452                 if (ops->set_desc == NULL)
1453                         ops->set_desc = pci_msi_domain_set_desc;
1454                 if (ops->msi_check == NULL)
1455                         ops->msi_check = pci_msi_domain_check_cap;
1456                 if (ops->handle_error == NULL)
1457                         ops->handle_error = pci_msi_domain_handle_error;
1458         }
1459 }
1460
1461 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1462 {
1463         struct irq_chip *chip = info->chip;
1464
1465         BUG_ON(!chip);
1466         if (!chip->irq_write_msi_msg)
1467                 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1468         if (!chip->irq_mask)
1469                 chip->irq_mask = pci_msi_mask_irq;
1470         if (!chip->irq_unmask)
1471                 chip->irq_unmask = pci_msi_unmask_irq;
1472 }
1473
1474 /**
1475  * pci_msi_create_irq_domain - Create a MSI interrupt domain
1476  * @fwnode:     Optional fwnode of the interrupt controller
1477  * @info:       MSI domain info
1478  * @parent:     Parent irq domain
1479  *
1480  * Updates the domain and chip ops and creates a MSI interrupt domain.
1481  *
1482  * Returns:
1483  * A domain pointer or NULL in case of failure.
1484  */
1485 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
1486                                              struct msi_domain_info *info,
1487                                              struct irq_domain *parent)
1488 {
1489         struct irq_domain *domain;
1490
1491         if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1492                 pci_msi_domain_update_dom_ops(info);
1493         if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1494                 pci_msi_domain_update_chip_ops(info);
1495
1496         info->flags |= MSI_FLAG_ACTIVATE_EARLY;
1497
1498         domain = msi_create_irq_domain(fwnode, info, parent);
1499         if (!domain)
1500                 return NULL;
1501
1502         irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
1503         return domain;
1504 }
1505 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
1506
1507 /*
1508  * Users of the generic MSI infrastructure expect a device to have a single ID,
1509  * so with DMA aliases we have to pick the least-worst compromise. Devices with
1510  * DMA phantom functions tend to still emit MSIs from the real function number,
1511  * so we ignore those and only consider topological aliases where either the
1512  * alias device or RID appears on a different bus number. We also make the
1513  * reasonable assumption that bridges are walked in an upstream direction (so
1514  * the last one seen wins), and the much braver assumption that the most likely
1515  * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1516  * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1517  * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1518  * for taking ownership all we can really do is close our eyes and hope...
1519  */
1520 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1521 {
1522         u32 *pa = data;
1523         u8 bus = PCI_BUS_NUM(*pa);
1524
1525         if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1526                 *pa = alias;
1527
1528         return 0;
1529 }
1530
1531 /**
1532  * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1533  * @domain:     The interrupt domain
1534  * @pdev:       The PCI device.
1535  *
1536  * The RID for a device is formed from the alias, with a firmware
1537  * supplied mapping applied
1538  *
1539  * Returns: The RID.
1540  */
1541 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1542 {
1543         struct device_node *of_node;
1544         u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
1545
1546         pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1547
1548         of_node = irq_domain_get_of_node(domain);
1549         rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
1550                         iort_msi_map_rid(&pdev->dev, rid);
1551
1552         return rid;
1553 }
1554
1555 /**
1556  * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1557  * @pdev:       The PCI device
1558  *
1559  * Use the firmware data to find a device-specific MSI domain
1560  * (i.e. not one that is set as a default).
1561  *
1562  * Returns: The corresponding MSI domain or NULL if none has been found.
1563  */
1564 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1565 {
1566         struct irq_domain *dom;
1567         u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
1568
1569         pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1570         dom = of_msi_map_get_device_domain(&pdev->dev, rid);
1571         if (!dom)
1572                 dom = iort_get_device_domain(&pdev->dev, rid);
1573         return dom;
1574 }
1575 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */