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