GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / pci / msi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 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 = kcalloc(num_msi + 1, sizeof(void *), 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 = kcalloc(2, sizeof(void *), 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         /* Lies, damned lies, and MSIs */
573         if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING)
574                 control |= PCI_MSI_FLAGS_MASKBIT;
575
576         entry->msi_attrib.is_msix       = 0;
577         entry->msi_attrib.is_64         = !!(control & PCI_MSI_FLAGS_64BIT);
578         entry->msi_attrib.entry_nr      = 0;
579         entry->msi_attrib.maskbit       = !!(control & PCI_MSI_FLAGS_MASKBIT);
580         entry->msi_attrib.default_irq   = dev->irq;     /* Save IOAPIC IRQ */
581         entry->msi_attrib.multi_cap     = (control & PCI_MSI_FLAGS_QMASK) >> 1;
582         entry->msi_attrib.multiple      = ilog2(__roundup_pow_of_two(nvec));
583
584         if (control & PCI_MSI_FLAGS_64BIT)
585                 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
586         else
587                 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
588
589         /* Save the initial mask status */
590         if (entry->msi_attrib.maskbit)
591                 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
592
593 out:
594         kfree(masks);
595         return entry;
596 }
597
598 static int msi_verify_entries(struct pci_dev *dev)
599 {
600         struct msi_desc *entry;
601
602         for_each_pci_msi_entry(entry, dev) {
603                 if (!dev->no_64bit_msi || !entry->msg.address_hi)
604                         continue;
605                 pci_err(dev, "Device has broken 64-bit MSI but arch"
606                         " tried to assign one above 4G\n");
607                 return -EIO;
608         }
609         return 0;
610 }
611
612 /**
613  * msi_capability_init - configure device's MSI capability structure
614  * @dev: pointer to the pci_dev data structure of MSI device function
615  * @nvec: number of interrupts to allocate
616  * @affd: description of automatic irq affinity assignments (may be %NULL)
617  *
618  * Setup the MSI capability structure of the device with the requested
619  * number of interrupts.  A return value of zero indicates the successful
620  * setup of an entry with the new MSI irq.  A negative return value indicates
621  * an error, and a positive return value indicates the number of interrupts
622  * which could have been allocated.
623  */
624 static int msi_capability_init(struct pci_dev *dev, int nvec,
625                                const struct irq_affinity *affd)
626 {
627         struct msi_desc *entry;
628         int ret;
629         unsigned mask;
630
631         pci_msi_set_enable(dev, 0);     /* Disable MSI during set up */
632
633         entry = msi_setup_entry(dev, nvec, affd);
634         if (!entry)
635                 return -ENOMEM;
636
637         /* All MSIs are unmasked by default, Mask them all */
638         mask = msi_mask(entry->msi_attrib.multi_cap);
639         msi_mask_irq(entry, mask, mask);
640
641         list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
642
643         /* Configure MSI capability structure */
644         ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
645         if (ret) {
646                 msi_mask_irq(entry, mask, 0);
647                 free_msi_irqs(dev);
648                 return ret;
649         }
650
651         ret = msi_verify_entries(dev);
652         if (ret) {
653                 msi_mask_irq(entry, mask, 0);
654                 free_msi_irqs(dev);
655                 return ret;
656         }
657
658         ret = populate_msi_sysfs(dev);
659         if (ret) {
660                 msi_mask_irq(entry, mask, 0);
661                 free_msi_irqs(dev);
662                 return ret;
663         }
664
665         /* Set MSI enabled bits  */
666         pci_intx_for_msi(dev, 0);
667         pci_msi_set_enable(dev, 1);
668         dev->msi_enabled = 1;
669
670         pcibios_free_irq(dev);
671         dev->irq = entry->irq;
672         return 0;
673 }
674
675 static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
676 {
677         resource_size_t phys_addr;
678         u32 table_offset;
679         unsigned long flags;
680         u8 bir;
681
682         pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
683                               &table_offset);
684         bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
685         flags = pci_resource_flags(dev, bir);
686         if (!flags || (flags & IORESOURCE_UNSET))
687                 return NULL;
688
689         table_offset &= PCI_MSIX_TABLE_OFFSET;
690         phys_addr = pci_resource_start(dev, bir) + table_offset;
691
692         return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
693 }
694
695 static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
696                               struct msix_entry *entries, int nvec,
697                               const struct irq_affinity *affd)
698 {
699         struct cpumask *curmsk, *masks = NULL;
700         struct msi_desc *entry;
701         void __iomem *addr;
702         int ret, i;
703
704         if (affd)
705                 masks = irq_create_affinity_masks(nvec, affd);
706
707         for (i = 0, curmsk = masks; i < nvec; i++) {
708                 entry = alloc_msi_entry(&dev->dev, 1, curmsk);
709                 if (!entry) {
710                         if (!i)
711                                 iounmap(base);
712                         else
713                                 free_msi_irqs(dev);
714                         /* No enough memory. Don't try again */
715                         ret = -ENOMEM;
716                         goto out;
717                 }
718
719                 entry->msi_attrib.is_msix       = 1;
720                 entry->msi_attrib.is_64         = 1;
721
722                 if (entries)
723                         entry->msi_attrib.entry_nr = entries[i].entry;
724                 else
725                         entry->msi_attrib.entry_nr = i;
726                 entry->msi_attrib.default_irq   = dev->irq;
727                 entry->mask_base                = base;
728
729                 addr = pci_msix_desc_addr(entry);
730                 if (addr)
731                         entry->masked = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
732
733                 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
734                 if (masks)
735                         curmsk++;
736         }
737         ret = 0;
738 out:
739         kfree(masks);
740         return ret;
741 }
742
743 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
744 {
745         struct msi_desc *entry;
746
747         for_each_pci_msi_entry(entry, dev) {
748                 if (entries) {
749                         entries->vector = entry->irq;
750                         entries++;
751                 }
752         }
753 }
754
755 static void msix_mask_all(void __iomem *base, int tsize)
756 {
757         u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
758         int i;
759
760         if (pci_msi_ignore_mask)
761                 return;
762
763         for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
764                 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
765 }
766
767 /**
768  * msix_capability_init - configure device's MSI-X capability
769  * @dev: pointer to the pci_dev data structure of MSI-X device function
770  * @entries: pointer to an array of struct msix_entry entries
771  * @nvec: number of @entries
772  * @affd: Optional pointer to enable automatic affinity assignement
773  *
774  * Setup the MSI-X capability structure of device function with a
775  * single MSI-X irq. A return of zero indicates the successful setup of
776  * requested MSI-X entries with allocated irqs or non-zero for otherwise.
777  **/
778 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
779                                 int nvec, const struct irq_affinity *affd)
780 {
781         void __iomem *base;
782         int ret, tsize;
783         u16 control;
784
785         /*
786          * Some devices require MSI-X to be enabled before the MSI-X
787          * registers can be accessed.  Mask all the vectors to prevent
788          * interrupts coming in before they're fully set up.
789          */
790         pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
791                                     PCI_MSIX_FLAGS_ENABLE);
792
793         pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
794         /* Request & Map MSI-X table region */
795         tsize = msix_table_size(control);
796         base = msix_map_region(dev, tsize);
797         if (!base) {
798                 ret = -ENOMEM;
799                 goto out_disable;
800         }
801
802         ret = msix_setup_entries(dev, base, entries, nvec, affd);
803         if (ret)
804                 goto out_disable;
805
806         ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
807         if (ret)
808                 goto out_avail;
809
810         /* Check if all MSI entries honor device restrictions */
811         ret = msi_verify_entries(dev);
812         if (ret)
813                 goto out_free;
814
815         msix_update_entries(dev, entries);
816
817         ret = populate_msi_sysfs(dev);
818         if (ret)
819                 goto out_free;
820
821         /* Set MSI-X enabled bits and unmask the function */
822         pci_intx_for_msi(dev, 0);
823         dev->msix_enabled = 1;
824
825         /*
826          * Ensure that all table entries are masked to prevent
827          * stale entries from firing in a crash kernel.
828          *
829          * Done late to deal with a broken Marvell NVME device
830          * which takes the MSI-X mask bits into account even
831          * when MSI-X is disabled, which prevents MSI delivery.
832          */
833         msix_mask_all(base, tsize);
834         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
835
836         pcibios_free_irq(dev);
837         return 0;
838
839 out_avail:
840         if (ret < 0) {
841                 /*
842                  * If we had some success, report the number of irqs
843                  * we succeeded in setting up.
844                  */
845                 struct msi_desc *entry;
846                 int avail = 0;
847
848                 for_each_pci_msi_entry(entry, dev) {
849                         if (entry->irq != 0)
850                                 avail++;
851                 }
852                 if (avail != 0)
853                         ret = avail;
854         }
855
856 out_free:
857         free_msi_irqs(dev);
858
859 out_disable:
860         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
861
862         return ret;
863 }
864
865 /**
866  * pci_msi_supported - check whether MSI may be enabled on a device
867  * @dev: pointer to the pci_dev data structure of MSI device function
868  * @nvec: how many MSIs have been requested ?
869  *
870  * Look at global flags, the device itself, and its parent buses
871  * to determine if MSI/-X are supported for the device. If MSI/-X is
872  * supported return 1, else return 0.
873  **/
874 static int pci_msi_supported(struct pci_dev *dev, int nvec)
875 {
876         struct pci_bus *bus;
877
878         /* MSI must be globally enabled and supported by the device */
879         if (!pci_msi_enable)
880                 return 0;
881
882         if (!dev || dev->no_msi || dev->current_state != PCI_D0)
883                 return 0;
884
885         /*
886          * You can't ask to have 0 or less MSIs configured.
887          *  a) it's stupid ..
888          *  b) the list manipulation code assumes nvec >= 1.
889          */
890         if (nvec < 1)
891                 return 0;
892
893         /*
894          * Any bridge which does NOT route MSI transactions from its
895          * secondary bus to its primary bus must set NO_MSI flag on
896          * the secondary pci_bus.
897          * We expect only arch-specific PCI host bus controller driver
898          * or quirks for specific PCI bridges to be setting NO_MSI.
899          */
900         for (bus = dev->bus; bus; bus = bus->parent)
901                 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
902                         return 0;
903
904         return 1;
905 }
906
907 /**
908  * pci_msi_vec_count - Return the number of MSI vectors a device can send
909  * @dev: device to report about
910  *
911  * This function returns the number of MSI vectors a device requested via
912  * Multiple Message Capable register. It returns a negative errno if the
913  * device is not capable sending MSI interrupts. Otherwise, the call succeeds
914  * and returns a power of two, up to a maximum of 2^5 (32), according to the
915  * MSI specification.
916  **/
917 int pci_msi_vec_count(struct pci_dev *dev)
918 {
919         int ret;
920         u16 msgctl;
921
922         if (!dev->msi_cap)
923                 return -EINVAL;
924
925         pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
926         ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
927
928         return ret;
929 }
930 EXPORT_SYMBOL(pci_msi_vec_count);
931
932 static void pci_msi_shutdown(struct pci_dev *dev)
933 {
934         struct msi_desc *desc;
935         u32 mask;
936
937         if (!pci_msi_enable || !dev || !dev->msi_enabled)
938                 return;
939
940         BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
941         desc = first_pci_msi_entry(dev);
942
943         pci_msi_set_enable(dev, 0);
944         pci_intx_for_msi(dev, 1);
945         dev->msi_enabled = 0;
946
947         /* Return the device with MSI unmasked as initial states */
948         mask = msi_mask(desc->msi_attrib.multi_cap);
949         msi_mask_irq(desc, mask, 0);
950
951         /* Restore dev->irq to its default pin-assertion irq */
952         dev->irq = desc->msi_attrib.default_irq;
953         pcibios_alloc_irq(dev);
954 }
955
956 void pci_disable_msi(struct pci_dev *dev)
957 {
958         if (!pci_msi_enable || !dev || !dev->msi_enabled)
959                 return;
960
961         pci_msi_shutdown(dev);
962         free_msi_irqs(dev);
963 }
964 EXPORT_SYMBOL(pci_disable_msi);
965
966 /**
967  * pci_msix_vec_count - return the number of device's MSI-X table entries
968  * @dev: pointer to the pci_dev data structure of MSI-X device function
969  * This function returns the number of device's MSI-X table entries and
970  * therefore the number of MSI-X vectors device is capable of sending.
971  * It returns a negative errno if the device is not capable of sending MSI-X
972  * interrupts.
973  **/
974 int pci_msix_vec_count(struct pci_dev *dev)
975 {
976         u16 control;
977
978         if (!dev->msix_cap)
979                 return -EINVAL;
980
981         pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
982         return msix_table_size(control);
983 }
984 EXPORT_SYMBOL(pci_msix_vec_count);
985
986 static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
987                              int nvec, const struct irq_affinity *affd)
988 {
989         int nr_entries;
990         int i, j;
991
992         if (!pci_msi_supported(dev, nvec))
993                 return -EINVAL;
994
995         nr_entries = pci_msix_vec_count(dev);
996         if (nr_entries < 0)
997                 return nr_entries;
998         if (nvec > nr_entries)
999                 return nr_entries;
1000
1001         if (entries) {
1002                 /* Check for any invalid entries */
1003                 for (i = 0; i < nvec; i++) {
1004                         if (entries[i].entry >= nr_entries)
1005                                 return -EINVAL;         /* invalid entry */
1006                         for (j = i + 1; j < nvec; j++) {
1007                                 if (entries[i].entry == entries[j].entry)
1008                                         return -EINVAL; /* duplicate entry */
1009                         }
1010                 }
1011         }
1012
1013         /* Check whether driver already requested for MSI irq */
1014         if (dev->msi_enabled) {
1015                 pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
1016                 return -EINVAL;
1017         }
1018         return msix_capability_init(dev, entries, nvec, affd);
1019 }
1020
1021 static void pci_msix_shutdown(struct pci_dev *dev)
1022 {
1023         struct msi_desc *entry;
1024
1025         if (!pci_msi_enable || !dev || !dev->msix_enabled)
1026                 return;
1027
1028         if (pci_dev_is_disconnected(dev)) {
1029                 dev->msix_enabled = 0;
1030                 return;
1031         }
1032
1033         /* Return the device with MSI-X masked as initial states */
1034         for_each_pci_msi_entry(entry, dev)
1035                 __pci_msix_desc_mask_irq(entry, 1);
1036
1037         pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
1038         pci_intx_for_msi(dev, 1);
1039         dev->msix_enabled = 0;
1040         pcibios_alloc_irq(dev);
1041 }
1042
1043 void pci_disable_msix(struct pci_dev *dev)
1044 {
1045         if (!pci_msi_enable || !dev || !dev->msix_enabled)
1046                 return;
1047
1048         pci_msix_shutdown(dev);
1049         free_msi_irqs(dev);
1050 }
1051 EXPORT_SYMBOL(pci_disable_msix);
1052
1053 void pci_no_msi(void)
1054 {
1055         pci_msi_enable = 0;
1056 }
1057
1058 /**
1059  * pci_msi_enabled - is MSI enabled?
1060  *
1061  * Returns true if MSI has not been disabled by the command-line option
1062  * pci=nomsi.
1063  **/
1064 int pci_msi_enabled(void)
1065 {
1066         return pci_msi_enable;
1067 }
1068 EXPORT_SYMBOL(pci_msi_enabled);
1069
1070 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
1071                                   const struct irq_affinity *affd)
1072 {
1073         int nvec;
1074         int rc;
1075
1076         if (!pci_msi_supported(dev, minvec))
1077                 return -EINVAL;
1078
1079         /* Check whether driver already requested MSI-X irqs */
1080         if (dev->msix_enabled) {
1081                 pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
1082                 return -EINVAL;
1083         }
1084
1085         if (maxvec < minvec)
1086                 return -ERANGE;
1087
1088         if (WARN_ON_ONCE(dev->msi_enabled))
1089                 return -EINVAL;
1090
1091         nvec = pci_msi_vec_count(dev);
1092         if (nvec < 0)
1093                 return nvec;
1094         if (nvec < minvec)
1095                 return -ENOSPC;
1096
1097         if (nvec > maxvec)
1098                 nvec = maxvec;
1099
1100         for (;;) {
1101                 if (affd) {
1102                         nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1103                         if (nvec < minvec)
1104                                 return -ENOSPC;
1105                 }
1106
1107                 rc = msi_capability_init(dev, nvec, affd);
1108                 if (rc == 0)
1109                         return nvec;
1110
1111                 if (rc < 0)
1112                         return rc;
1113                 if (rc < minvec)
1114                         return -ENOSPC;
1115
1116                 nvec = rc;
1117         }
1118 }
1119
1120 /* deprecated, don't use */
1121 int pci_enable_msi(struct pci_dev *dev)
1122 {
1123         int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
1124         if (rc < 0)
1125                 return rc;
1126         return 0;
1127 }
1128 EXPORT_SYMBOL(pci_enable_msi);
1129
1130 static int __pci_enable_msix_range(struct pci_dev *dev,
1131                                    struct msix_entry *entries, int minvec,
1132                                    int maxvec, const struct irq_affinity *affd)
1133 {
1134         int rc, nvec = maxvec;
1135
1136         if (maxvec < minvec)
1137                 return -ERANGE;
1138
1139         if (WARN_ON_ONCE(dev->msix_enabled))
1140                 return -EINVAL;
1141
1142         for (;;) {
1143                 if (affd) {
1144                         nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
1145                         if (nvec < minvec)
1146                                 return -ENOSPC;
1147                 }
1148
1149                 rc = __pci_enable_msix(dev, entries, nvec, affd);
1150                 if (rc == 0)
1151                         return nvec;
1152
1153                 if (rc < 0)
1154                         return rc;
1155                 if (rc < minvec)
1156                         return -ENOSPC;
1157
1158                 nvec = rc;
1159         }
1160 }
1161
1162 /**
1163  * pci_enable_msix_range - configure device's MSI-X capability structure
1164  * @dev: pointer to the pci_dev data structure of MSI-X device function
1165  * @entries: pointer to an array of MSI-X entries
1166  * @minvec: minimum number of MSI-X irqs requested
1167  * @maxvec: maximum number of MSI-X irqs requested
1168  *
1169  * Setup the MSI-X capability structure of device function with a maximum
1170  * possible number of interrupts in the range between @minvec and @maxvec
1171  * upon its software driver call to request for MSI-X mode enabled on its
1172  * hardware device function. It returns a negative errno if an error occurs.
1173  * If it succeeds, it returns the actual number of interrupts allocated and
1174  * indicates the successful configuration of MSI-X capability structure
1175  * with new allocated MSI-X interrupts.
1176  **/
1177 int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
1178                 int minvec, int maxvec)
1179 {
1180         return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
1181 }
1182 EXPORT_SYMBOL(pci_enable_msix_range);
1183
1184 /**
1185  * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
1186  * @dev:                PCI device to operate on
1187  * @min_vecs:           minimum number of vectors required (must be >= 1)
1188  * @max_vecs:           maximum (desired) number of vectors
1189  * @flags:              flags or quirks for the allocation
1190  * @affd:               optional description of the affinity requirements
1191  *
1192  * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
1193  * vectors if available, and fall back to a single legacy vector
1194  * if neither is available.  Return the number of vectors allocated,
1195  * (which might be smaller than @max_vecs) if successful, or a negative
1196  * error code on error. If less than @min_vecs interrupt vectors are
1197  * available for @dev the function will fail with -ENOSPC.
1198  *
1199  * To get the Linux IRQ number used for a vector that can be passed to
1200  * request_irq() use the pci_irq_vector() helper.
1201  */
1202 int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
1203                                    unsigned int max_vecs, unsigned int flags,
1204                                    const struct irq_affinity *affd)
1205 {
1206         static const struct irq_affinity msi_default_affd;
1207         int msix_vecs = -ENOSPC;
1208         int msi_vecs = -ENOSPC;
1209
1210         if (flags & PCI_IRQ_AFFINITY) {
1211                 if (!affd)
1212                         affd = &msi_default_affd;
1213         } else {
1214                 if (WARN_ON(affd))
1215                         affd = NULL;
1216         }
1217
1218         if (flags & PCI_IRQ_MSIX) {
1219                 msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs,
1220                                                     max_vecs, affd);
1221                 if (msix_vecs > 0)
1222                         return msix_vecs;
1223         }
1224
1225         if (flags & PCI_IRQ_MSI) {
1226                 msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs,
1227                                                   affd);
1228                 if (msi_vecs > 0)
1229                         return msi_vecs;
1230         }
1231
1232         /* use legacy irq if allowed */
1233         if (flags & PCI_IRQ_LEGACY) {
1234                 if (min_vecs == 1 && dev->irq) {
1235                         pci_intx(dev, 1);
1236                         return 1;
1237                 }
1238         }
1239
1240         if (msix_vecs == -ENOSPC)
1241                 return -ENOSPC;
1242         return msi_vecs;
1243 }
1244 EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
1245
1246 /**
1247  * pci_free_irq_vectors - free previously allocated IRQs for a device
1248  * @dev:                PCI device to operate on
1249  *
1250  * Undoes the allocations and enabling in pci_alloc_irq_vectors().
1251  */
1252 void pci_free_irq_vectors(struct pci_dev *dev)
1253 {
1254         pci_disable_msix(dev);
1255         pci_disable_msi(dev);
1256 }
1257 EXPORT_SYMBOL(pci_free_irq_vectors);
1258
1259 /**
1260  * pci_irq_vector - return Linux IRQ number of a device vector
1261  * @dev: PCI device to operate on
1262  * @nr: device-relative interrupt vector index (0-based).
1263  */
1264 int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
1265 {
1266         if (dev->msix_enabled) {
1267                 struct msi_desc *entry;
1268                 int i = 0;
1269
1270                 for_each_pci_msi_entry(entry, dev) {
1271                         if (i == nr)
1272                                 return entry->irq;
1273                         i++;
1274                 }
1275                 WARN_ON_ONCE(1);
1276                 return -EINVAL;
1277         }
1278
1279         if (dev->msi_enabled) {
1280                 struct msi_desc *entry = first_pci_msi_entry(dev);
1281
1282                 if (WARN_ON_ONCE(nr >= entry->nvec_used))
1283                         return -EINVAL;
1284         } else {
1285                 if (WARN_ON_ONCE(nr > 0))
1286                         return -EINVAL;
1287         }
1288
1289         return dev->irq + nr;
1290 }
1291 EXPORT_SYMBOL(pci_irq_vector);
1292
1293 /**
1294  * pci_irq_get_affinity - return the affinity of a particular msi vector
1295  * @dev:        PCI device to operate on
1296  * @nr:         device-relative interrupt vector index (0-based).
1297  */
1298 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
1299 {
1300         if (dev->msix_enabled) {
1301                 struct msi_desc *entry;
1302                 int i = 0;
1303
1304                 for_each_pci_msi_entry(entry, dev) {
1305                         if (i == nr)
1306                                 return entry->affinity;
1307                         i++;
1308                 }
1309                 WARN_ON_ONCE(1);
1310                 return NULL;
1311         } else if (dev->msi_enabled) {
1312                 struct msi_desc *entry = first_pci_msi_entry(dev);
1313
1314                 if (WARN_ON_ONCE(!entry || !entry->affinity ||
1315                                  nr >= entry->nvec_used))
1316                         return NULL;
1317
1318                 return &entry->affinity[nr];
1319         } else {
1320                 return cpu_possible_mask;
1321         }
1322 }
1323 EXPORT_SYMBOL(pci_irq_get_affinity);
1324
1325 /**
1326  * pci_irq_get_node - return the numa node of a particular msi vector
1327  * @pdev:       PCI device to operate on
1328  * @vec:        device-relative interrupt vector index (0-based).
1329  */
1330 int pci_irq_get_node(struct pci_dev *pdev, int vec)
1331 {
1332         const struct cpumask *mask;
1333
1334         mask = pci_irq_get_affinity(pdev, vec);
1335         if (mask)
1336                 return local_memory_node(cpu_to_node(cpumask_first(mask)));
1337         return dev_to_node(&pdev->dev);
1338 }
1339 EXPORT_SYMBOL(pci_irq_get_node);
1340
1341 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
1342 {
1343         return to_pci_dev(desc->dev);
1344 }
1345 EXPORT_SYMBOL(msi_desc_to_pci_dev);
1346
1347 void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
1348 {
1349         struct pci_dev *dev = msi_desc_to_pci_dev(desc);
1350
1351         return dev->bus->sysdata;
1352 }
1353 EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
1354
1355 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
1356 /**
1357  * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
1358  * @irq_data:   Pointer to interrupt data of the MSI interrupt
1359  * @msg:        Pointer to the message
1360  */
1361 void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
1362 {
1363         struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
1364
1365         /*
1366          * For MSI-X desc->irq is always equal to irq_data->irq. For
1367          * MSI only the first interrupt of MULTI MSI passes the test.
1368          */
1369         if (desc->irq == irq_data->irq)
1370                 __pci_write_msi_msg(desc, msg);
1371 }
1372
1373 /**
1374  * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
1375  * @dev:        Pointer to the PCI device
1376  * @desc:       Pointer to the msi descriptor
1377  *
1378  * The ID number is only used within the irqdomain.
1379  */
1380 irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
1381                                           struct msi_desc *desc)
1382 {
1383         return (irq_hw_number_t)desc->msi_attrib.entry_nr |
1384                 PCI_DEVID(dev->bus->number, dev->devfn) << 11 |
1385                 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
1386 }
1387
1388 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
1389 {
1390         return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
1391 }
1392
1393 /**
1394  * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
1395  * @domain:     The interrupt domain to check
1396  * @info:       The domain info for verification
1397  * @dev:        The device to check
1398  *
1399  * Returns:
1400  *  0 if the functionality is supported
1401  *  1 if Multi MSI is requested, but the domain does not support it
1402  *  -ENOTSUPP otherwise
1403  */
1404 int pci_msi_domain_check_cap(struct irq_domain *domain,
1405                              struct msi_domain_info *info, struct device *dev)
1406 {
1407         struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
1408
1409         /* Special handling to support __pci_enable_msi_range() */
1410         if (pci_msi_desc_is_multi_msi(desc) &&
1411             !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
1412                 return 1;
1413         else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
1414                 return -ENOTSUPP;
1415
1416         return 0;
1417 }
1418
1419 static int pci_msi_domain_handle_error(struct irq_domain *domain,
1420                                        struct msi_desc *desc, int error)
1421 {
1422         /* Special handling to support __pci_enable_msi_range() */
1423         if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
1424                 return 1;
1425
1426         return error;
1427 }
1428
1429 #ifdef GENERIC_MSI_DOMAIN_OPS
1430 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
1431                                     struct msi_desc *desc)
1432 {
1433         arg->desc = desc;
1434         arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
1435                                                desc);
1436 }
1437 #else
1438 #define pci_msi_domain_set_desc         NULL
1439 #endif
1440
1441 static struct msi_domain_ops pci_msi_domain_ops_default = {
1442         .set_desc       = pci_msi_domain_set_desc,
1443         .msi_check      = pci_msi_domain_check_cap,
1444         .handle_error   = pci_msi_domain_handle_error,
1445 };
1446
1447 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
1448 {
1449         struct msi_domain_ops *ops = info->ops;
1450
1451         if (ops == NULL) {
1452                 info->ops = &pci_msi_domain_ops_default;
1453         } else {
1454                 if (ops->set_desc == NULL)
1455                         ops->set_desc = pci_msi_domain_set_desc;
1456                 if (ops->msi_check == NULL)
1457                         ops->msi_check = pci_msi_domain_check_cap;
1458                 if (ops->handle_error == NULL)
1459                         ops->handle_error = pci_msi_domain_handle_error;
1460         }
1461 }
1462
1463 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
1464 {
1465         struct irq_chip *chip = info->chip;
1466
1467         BUG_ON(!chip);
1468         if (!chip->irq_write_msi_msg)
1469                 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
1470         if (!chip->irq_mask)
1471                 chip->irq_mask = pci_msi_mask_irq;
1472         if (!chip->irq_unmask)
1473                 chip->irq_unmask = pci_msi_unmask_irq;
1474 }
1475
1476 /**
1477  * pci_msi_create_irq_domain - Create a MSI interrupt domain
1478  * @fwnode:     Optional fwnode of the interrupt controller
1479  * @info:       MSI domain info
1480  * @parent:     Parent irq domain
1481  *
1482  * Updates the domain and chip ops and creates a MSI interrupt domain.
1483  *
1484  * Returns:
1485  * A domain pointer or NULL in case of failure.
1486  */
1487 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
1488                                              struct msi_domain_info *info,
1489                                              struct irq_domain *parent)
1490 {
1491         struct irq_domain *domain;
1492
1493         if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
1494                 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
1495
1496         if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
1497                 pci_msi_domain_update_dom_ops(info);
1498         if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
1499                 pci_msi_domain_update_chip_ops(info);
1500
1501         info->flags |= MSI_FLAG_ACTIVATE_EARLY;
1502         if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
1503                 info->flags |= MSI_FLAG_MUST_REACTIVATE;
1504
1505         /* PCI-MSI is oneshot-safe */
1506         info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
1507
1508         domain = msi_create_irq_domain(fwnode, info, parent);
1509         if (!domain)
1510                 return NULL;
1511
1512         irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
1513         return domain;
1514 }
1515 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
1516
1517 /*
1518  * Users of the generic MSI infrastructure expect a device to have a single ID,
1519  * so with DMA aliases we have to pick the least-worst compromise. Devices with
1520  * DMA phantom functions tend to still emit MSIs from the real function number,
1521  * so we ignore those and only consider topological aliases where either the
1522  * alias device or RID appears on a different bus number. We also make the
1523  * reasonable assumption that bridges are walked in an upstream direction (so
1524  * the last one seen wins), and the much braver assumption that the most likely
1525  * case is that of PCI->PCIe so we should always use the alias RID. This echoes
1526  * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
1527  * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
1528  * for taking ownership all we can really do is close our eyes and hope...
1529  */
1530 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
1531 {
1532         u32 *pa = data;
1533         u8 bus = PCI_BUS_NUM(*pa);
1534
1535         if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
1536                 *pa = alias;
1537
1538         return 0;
1539 }
1540
1541 /**
1542  * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
1543  * @domain:     The interrupt domain
1544  * @pdev:       The PCI device.
1545  *
1546  * The RID for a device is formed from the alias, with a firmware
1547  * supplied mapping applied
1548  *
1549  * Returns: The RID.
1550  */
1551 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
1552 {
1553         struct device_node *of_node;
1554         u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
1555
1556         pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1557
1558         of_node = irq_domain_get_of_node(domain);
1559         rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
1560                         iort_msi_map_rid(&pdev->dev, rid);
1561
1562         return rid;
1563 }
1564
1565 /**
1566  * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
1567  * @pdev:       The PCI device
1568  *
1569  * Use the firmware data to find a device-specific MSI domain
1570  * (i.e. not one that is set as a default).
1571  *
1572  * Returns: The corresponding MSI domain or NULL if none has been found.
1573  */
1574 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
1575 {
1576         struct irq_domain *dom;
1577         u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
1578
1579         pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
1580         dom = of_msi_map_get_device_domain(&pdev->dev, rid);
1581         if (!dom)
1582                 dom = iort_get_device_domain(&pdev->dev, rid);
1583         return dom;
1584 }
1585 #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */