blob: db761adef652b63f256987ac2f2fcf8925c35a2c [file] [log] [blame]
Thomas Gleixnera01e09e2021-12-06 23:27:51 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCI Message Signaled Interrupt (MSI).
4 *
5 * Legacy architecture specific setup and teardown mechanism.
6 */
Thomas Gleixneraa423ac2021-12-06 23:27:52 +01007#include "msi.h"
Thomas Gleixnera01e09e2021-12-06 23:27:51 +01008
9/* Arch hooks */
10int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
11{
12 return -EINVAL;
13}
14
15void __weak arch_teardown_msi_irq(unsigned int irq)
16{
17}
18
19int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
20{
21 struct msi_desc *desc;
22 int ret;
23
24 /*
25 * If an architecture wants to support multiple MSI, it needs to
26 * override arch_setup_msi_irqs()
27 */
28 if (type == PCI_CAP_ID_MSI && nvec > 1)
29 return 1;
30
Thomas Gleixnerae24e282021-12-06 23:51:18 +010031 msi_for_each_desc(desc, &dev->dev, MSI_DESC_NOTASSOCIATED) {
Thomas Gleixnera01e09e2021-12-06 23:27:51 +010032 ret = arch_setup_msi_irq(dev, desc);
33 if (ret)
34 return ret < 0 ? ret : -ENOSPC;
35 }
36
37 return 0;
38}
39
40void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
41{
42 struct msi_desc *desc;
43 int i;
44
Thomas Gleixnerae24e282021-12-06 23:51:18 +010045 msi_for_each_desc(desc, &dev->dev, MSI_DESC_ASSOCIATED) {
46 for (i = 0; i < desc->nvec_used; i++)
47 arch_teardown_msi_irq(desc->irq + i);
Thomas Gleixnera01e09e2021-12-06 23:27:51 +010048 }
49}
Thomas Gleixneraa423ac2021-12-06 23:27:52 +010050
Thomas Gleixner60bf9b32021-12-06 23:28:00 +010051static int pci_msi_setup_check_result(struct pci_dev *dev, int type, int ret)
52{
Thomas Gleixnerae24e282021-12-06 23:51:18 +010053 struct msi_desc *desc;
Thomas Gleixner60bf9b32021-12-06 23:28:00 +010054 int avail = 0;
55
56 if (type != PCI_CAP_ID_MSIX || ret >= 0)
57 return ret;
58
59 /* Scan the MSI descriptors for successfully allocated ones. */
Thomas Gleixnerae24e282021-12-06 23:51:18 +010060 msi_for_each_desc(desc, &dev->dev, MSI_DESC_ASSOCIATED)
61 avail++;
62
Thomas Gleixner60bf9b32021-12-06 23:28:00 +010063 return avail ? avail : ret;
64}
65
Thomas Gleixneraa423ac2021-12-06 23:27:52 +010066int pci_msi_legacy_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
67{
Thomas Gleixner60bf9b32021-12-06 23:28:00 +010068 int ret = arch_setup_msi_irqs(dev, nvec, type);
69
Thomas Gleixnerffd84482021-12-10 23:19:05 +010070 ret = pci_msi_setup_check_result(dev, type, ret);
71 if (!ret)
72 ret = msi_device_populate_sysfs(&dev->dev);
73 return ret;
Thomas Gleixneraa423ac2021-12-06 23:27:52 +010074}
75
76void pci_msi_legacy_teardown_msi_irqs(struct pci_dev *dev)
77{
Thomas Gleixnerffd84482021-12-10 23:19:05 +010078 msi_device_destroy_sysfs(&dev->dev);
Thomas Gleixneraa423ac2021-12-06 23:27:52 +010079 arch_teardown_msi_irqs(dev);
80}