1aa423ac4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2aa423ac4SThomas Gleixner /*
3aa423ac4SThomas Gleixner * PCI Message Signaled Interrupt (MSI) - irqdomain support
4aa423ac4SThomas Gleixner */
5aa423ac4SThomas Gleixner #include <linux/acpi_iort.h>
6aa423ac4SThomas Gleixner #include <linux/irqdomain.h>
7aa423ac4SThomas Gleixner #include <linux/of_irq.h>
8aa423ac4SThomas Gleixner
9aa423ac4SThomas Gleixner #include "msi.h"
10aa423ac4SThomas Gleixner
pci_msi_setup_msi_irqs(struct pci_dev * dev,int nvec,int type)11aa423ac4SThomas Gleixner int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
12aa423ac4SThomas Gleixner {
13aa423ac4SThomas Gleixner struct irq_domain *domain;
14aa423ac4SThomas Gleixner
15aa423ac4SThomas Gleixner domain = dev_get_msi_domain(&dev->dev);
16aa423ac4SThomas Gleixner if (domain && irq_domain_is_hierarchy(domain))
17d3a11deeSThomas Gleixner return msi_domain_alloc_irqs_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN, nvec);
18aa423ac4SThomas Gleixner
19aa423ac4SThomas Gleixner return pci_msi_legacy_setup_msi_irqs(dev, nvec, type);
20aa423ac4SThomas Gleixner }
21aa423ac4SThomas Gleixner
pci_msi_teardown_msi_irqs(struct pci_dev * dev)22aa423ac4SThomas Gleixner void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
23aa423ac4SThomas Gleixner {
24aa423ac4SThomas Gleixner struct irq_domain *domain;
25aa423ac4SThomas Gleixner
26aa423ac4SThomas Gleixner domain = dev_get_msi_domain(&dev->dev);
27b2bdda20SAhmed S. Darwish if (domain && irq_domain_is_hierarchy(domain)) {
28d3a11deeSThomas Gleixner msi_domain_free_irqs_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN);
29b2bdda20SAhmed S. Darwish } else {
30aa423ac4SThomas Gleixner pci_msi_legacy_teardown_msi_irqs(dev);
31a0af3d11SThomas Gleixner msi_free_msi_descs(&dev->dev);
32aa423ac4SThomas Gleixner }
33b2bdda20SAhmed S. Darwish }
34aa423ac4SThomas Gleixner
35aa423ac4SThomas Gleixner /**
36aa423ac4SThomas Gleixner * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
37aa423ac4SThomas Gleixner * @irq_data: Pointer to interrupt data of the MSI interrupt
38aa423ac4SThomas Gleixner * @msg: Pointer to the message
39aa423ac4SThomas Gleixner */
pci_msi_domain_write_msg(struct irq_data * irq_data,struct msi_msg * msg)40aa423ac4SThomas Gleixner static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
41aa423ac4SThomas Gleixner {
42aa423ac4SThomas Gleixner struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
43aa423ac4SThomas Gleixner
44aa423ac4SThomas Gleixner /*
45aa423ac4SThomas Gleixner * For MSI-X desc->irq is always equal to irq_data->irq. For
46aa423ac4SThomas Gleixner * MSI only the first interrupt of MULTI MSI passes the test.
47aa423ac4SThomas Gleixner */
48aa423ac4SThomas Gleixner if (desc->irq == irq_data->irq)
49aa423ac4SThomas Gleixner __pci_write_msi_msg(desc, msg);
50aa423ac4SThomas Gleixner }
51aa423ac4SThomas Gleixner
52aa423ac4SThomas Gleixner /**
53aa423ac4SThomas Gleixner * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
54aa423ac4SThomas Gleixner * @desc: Pointer to the MSI descriptor
55aa423ac4SThomas Gleixner *
56aa423ac4SThomas Gleixner * The ID number is only used within the irqdomain.
57aa423ac4SThomas Gleixner */
pci_msi_domain_calc_hwirq(struct msi_desc * desc)58aa423ac4SThomas Gleixner static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
59aa423ac4SThomas Gleixner {
60aa423ac4SThomas Gleixner struct pci_dev *dev = msi_desc_to_pci_dev(desc);
61aa423ac4SThomas Gleixner
62173ffad7SThomas Gleixner return (irq_hw_number_t)desc->msi_index |
63aa423ac4SThomas Gleixner pci_dev_id(dev) << 11 |
645ef293c3SVidya Sagar ((irq_hw_number_t)(pci_domain_nr(dev->bus) & 0xFFFFFFFF)) << 27;
65aa423ac4SThomas Gleixner }
66aa423ac4SThomas Gleixner
pci_msi_domain_set_desc(msi_alloc_info_t * arg,struct msi_desc * desc)67aa423ac4SThomas Gleixner static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
68aa423ac4SThomas Gleixner struct msi_desc *desc)
69aa423ac4SThomas Gleixner {
70aa423ac4SThomas Gleixner arg->desc = desc;
71aa423ac4SThomas Gleixner arg->hwirq = pci_msi_domain_calc_hwirq(desc);
72aa423ac4SThomas Gleixner }
73aa423ac4SThomas Gleixner
74aa423ac4SThomas Gleixner static struct msi_domain_ops pci_msi_domain_ops_default = {
75aa423ac4SThomas Gleixner .set_desc = pci_msi_domain_set_desc,
76aa423ac4SThomas Gleixner };
77aa423ac4SThomas Gleixner
pci_msi_domain_update_dom_ops(struct msi_domain_info * info)78aa423ac4SThomas Gleixner static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
79aa423ac4SThomas Gleixner {
80aa423ac4SThomas Gleixner struct msi_domain_ops *ops = info->ops;
81aa423ac4SThomas Gleixner
82aa423ac4SThomas Gleixner if (ops == NULL) {
83aa423ac4SThomas Gleixner info->ops = &pci_msi_domain_ops_default;
84aa423ac4SThomas Gleixner } else {
85aa423ac4SThomas Gleixner if (ops->set_desc == NULL)
86aa423ac4SThomas Gleixner ops->set_desc = pci_msi_domain_set_desc;
87aa423ac4SThomas Gleixner }
88aa423ac4SThomas Gleixner }
89aa423ac4SThomas Gleixner
pci_msi_domain_update_chip_ops(struct msi_domain_info * info)90aa423ac4SThomas Gleixner static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
91aa423ac4SThomas Gleixner {
92aa423ac4SThomas Gleixner struct irq_chip *chip = info->chip;
93aa423ac4SThomas Gleixner
94aa423ac4SThomas Gleixner BUG_ON(!chip);
95aa423ac4SThomas Gleixner if (!chip->irq_write_msi_msg)
96aa423ac4SThomas Gleixner chip->irq_write_msi_msg = pci_msi_domain_write_msg;
97aa423ac4SThomas Gleixner if (!chip->irq_mask)
98aa423ac4SThomas Gleixner chip->irq_mask = pci_msi_mask_irq;
99aa423ac4SThomas Gleixner if (!chip->irq_unmask)
100aa423ac4SThomas Gleixner chip->irq_unmask = pci_msi_unmask_irq;
101aa423ac4SThomas Gleixner }
102aa423ac4SThomas Gleixner
103aa423ac4SThomas Gleixner /**
104aa423ac4SThomas Gleixner * pci_msi_create_irq_domain - Create a MSI interrupt domain
105aa423ac4SThomas Gleixner * @fwnode: Optional fwnode of the interrupt controller
106aa423ac4SThomas Gleixner * @info: MSI domain info
107aa423ac4SThomas Gleixner * @parent: Parent irq domain
108aa423ac4SThomas Gleixner *
109aa423ac4SThomas Gleixner * Updates the domain and chip ops and creates a MSI interrupt domain.
110aa423ac4SThomas Gleixner *
111aa423ac4SThomas Gleixner * Returns:
112aa423ac4SThomas Gleixner * A domain pointer or NULL in case of failure.
113aa423ac4SThomas Gleixner */
pci_msi_create_irq_domain(struct fwnode_handle * fwnode,struct msi_domain_info * info,struct irq_domain * parent)114aa423ac4SThomas Gleixner struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
115aa423ac4SThomas Gleixner struct msi_domain_info *info,
116aa423ac4SThomas Gleixner struct irq_domain *parent)
117aa423ac4SThomas Gleixner {
118aa423ac4SThomas Gleixner if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
119aa423ac4SThomas Gleixner info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
120aa423ac4SThomas Gleixner
121aa423ac4SThomas Gleixner if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
122aa423ac4SThomas Gleixner pci_msi_domain_update_dom_ops(info);
123aa423ac4SThomas Gleixner if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
124aa423ac4SThomas Gleixner pci_msi_domain_update_chip_ops(info);
125aa423ac4SThomas Gleixner
126b2bdda20SAhmed S. Darwish /* Let the core code free MSI descriptors when freeing interrupts */
127b2bdda20SAhmed S. Darwish info->flags |= MSI_FLAG_FREE_MSI_DESCS;
128b2bdda20SAhmed S. Darwish
129a0af3d11SThomas Gleixner info->flags |= MSI_FLAG_ACTIVATE_EARLY | MSI_FLAG_DEV_SYSFS;
130aa423ac4SThomas Gleixner if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
131aa423ac4SThomas Gleixner info->flags |= MSI_FLAG_MUST_REACTIVATE;
132aa423ac4SThomas Gleixner
133aa423ac4SThomas Gleixner /* PCI-MSI is oneshot-safe */
134aa423ac4SThomas Gleixner info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
13538c0c10aSAhmed S. Darwish /* Let the core update the bus token */
13638c0c10aSAhmed S. Darwish info->bus_token = DOMAIN_BUS_PCI_MSI;
137aa423ac4SThomas Gleixner
13838c0c10aSAhmed S. Darwish return msi_create_irq_domain(fwnode, info, parent);
139aa423ac4SThomas Gleixner }
140aa423ac4SThomas Gleixner EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
141aa423ac4SThomas Gleixner
14215c72f82SThomas Gleixner /*
14315c72f82SThomas Gleixner * Per device MSI[-X] domain functionality
14415c72f82SThomas Gleixner */
pci_device_domain_set_desc(msi_alloc_info_t * arg,struct msi_desc * desc)14515c72f82SThomas Gleixner static void pci_device_domain_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
14615c72f82SThomas Gleixner {
14715c72f82SThomas Gleixner arg->desc = desc;
14815c72f82SThomas Gleixner arg->hwirq = desc->msi_index;
14915c72f82SThomas Gleixner }
15015c72f82SThomas Gleixner
pci_irq_mask_msi(struct irq_data * data)15115c72f82SThomas Gleixner static void pci_irq_mask_msi(struct irq_data *data)
15215c72f82SThomas Gleixner {
15315c72f82SThomas Gleixner struct msi_desc *desc = irq_data_get_msi_desc(data);
15415c72f82SThomas Gleixner
15515c72f82SThomas Gleixner pci_msi_mask(desc, BIT(data->irq - desc->irq));
15615c72f82SThomas Gleixner }
15715c72f82SThomas Gleixner
pci_irq_unmask_msi(struct irq_data * data)15815c72f82SThomas Gleixner static void pci_irq_unmask_msi(struct irq_data *data)
15915c72f82SThomas Gleixner {
16015c72f82SThomas Gleixner struct msi_desc *desc = irq_data_get_msi_desc(data);
16115c72f82SThomas Gleixner
16215c72f82SThomas Gleixner pci_msi_unmask(desc, BIT(data->irq - desc->irq));
16315c72f82SThomas Gleixner }
16415c72f82SThomas Gleixner
16515c72f82SThomas Gleixner #ifdef CONFIG_GENERIC_IRQ_RESERVATION_MODE
16615c72f82SThomas Gleixner # define MSI_REACTIVATE MSI_FLAG_MUST_REACTIVATE
16715c72f82SThomas Gleixner #else
16815c72f82SThomas Gleixner # define MSI_REACTIVATE 0
16915c72f82SThomas Gleixner #endif
17015c72f82SThomas Gleixner
17115c72f82SThomas Gleixner #define MSI_COMMON_FLAGS (MSI_FLAG_FREE_MSI_DESCS | \
17215c72f82SThomas Gleixner MSI_FLAG_ACTIVATE_EARLY | \
17315c72f82SThomas Gleixner MSI_FLAG_DEV_SYSFS | \
17415c72f82SThomas Gleixner MSI_REACTIVATE)
17515c72f82SThomas Gleixner
17615c72f82SThomas Gleixner static const struct msi_domain_template pci_msi_template = {
17715c72f82SThomas Gleixner .chip = {
17815c72f82SThomas Gleixner .name = "PCI-MSI",
17915c72f82SThomas Gleixner .irq_mask = pci_irq_mask_msi,
18015c72f82SThomas Gleixner .irq_unmask = pci_irq_unmask_msi,
18115c72f82SThomas Gleixner .irq_write_msi_msg = pci_msi_domain_write_msg,
18215c72f82SThomas Gleixner .flags = IRQCHIP_ONESHOT_SAFE,
18315c72f82SThomas Gleixner },
18415c72f82SThomas Gleixner
18515c72f82SThomas Gleixner .ops = {
18615c72f82SThomas Gleixner .set_desc = pci_device_domain_set_desc,
18715c72f82SThomas Gleixner },
18815c72f82SThomas Gleixner
18915c72f82SThomas Gleixner .info = {
19015c72f82SThomas Gleixner .flags = MSI_COMMON_FLAGS | MSI_FLAG_MULTI_PCI_MSI,
19115c72f82SThomas Gleixner .bus_token = DOMAIN_BUS_PCI_DEVICE_MSI,
19215c72f82SThomas Gleixner },
19315c72f82SThomas Gleixner };
19415c72f82SThomas Gleixner
pci_irq_mask_msix(struct irq_data * data)19515c72f82SThomas Gleixner static void pci_irq_mask_msix(struct irq_data *data)
19615c72f82SThomas Gleixner {
19715c72f82SThomas Gleixner pci_msix_mask(irq_data_get_msi_desc(data));
19815c72f82SThomas Gleixner }
19915c72f82SThomas Gleixner
pci_irq_unmask_msix(struct irq_data * data)20015c72f82SThomas Gleixner static void pci_irq_unmask_msix(struct irq_data *data)
20115c72f82SThomas Gleixner {
20215c72f82SThomas Gleixner pci_msix_unmask(irq_data_get_msi_desc(data));
20315c72f82SThomas Gleixner }
20415c72f82SThomas Gleixner
pci_msix_prepare_desc(struct irq_domain * domain,msi_alloc_info_t * arg,struct msi_desc * desc)20573bd063cSThomas Gleixner static void pci_msix_prepare_desc(struct irq_domain *domain, msi_alloc_info_t *arg,
20673bd063cSThomas Gleixner struct msi_desc *desc)
20773bd063cSThomas Gleixner {
20873bd063cSThomas Gleixner /* Don't fiddle with preallocated MSI descriptors */
20973bd063cSThomas Gleixner if (!desc->pci.mask_base)
21073bd063cSThomas Gleixner msix_prepare_msi_desc(to_pci_dev(desc->dev), desc);
21173bd063cSThomas Gleixner }
21273bd063cSThomas Gleixner
21315c72f82SThomas Gleixner static const struct msi_domain_template pci_msix_template = {
21415c72f82SThomas Gleixner .chip = {
21515c72f82SThomas Gleixner .name = "PCI-MSIX",
21615c72f82SThomas Gleixner .irq_mask = pci_irq_mask_msix,
21715c72f82SThomas Gleixner .irq_unmask = pci_irq_unmask_msix,
21815c72f82SThomas Gleixner .irq_write_msi_msg = pci_msi_domain_write_msg,
21915c72f82SThomas Gleixner .flags = IRQCHIP_ONESHOT_SAFE,
22015c72f82SThomas Gleixner },
22115c72f82SThomas Gleixner
22215c72f82SThomas Gleixner .ops = {
22373bd063cSThomas Gleixner .prepare_desc = pci_msix_prepare_desc,
22415c72f82SThomas Gleixner .set_desc = pci_device_domain_set_desc,
22515c72f82SThomas Gleixner },
22615c72f82SThomas Gleixner
22715c72f82SThomas Gleixner .info = {
22834026364SThomas Gleixner .flags = MSI_COMMON_FLAGS | MSI_FLAG_PCI_MSIX |
22934026364SThomas Gleixner MSI_FLAG_PCI_MSIX_ALLOC_DYN,
23015c72f82SThomas Gleixner .bus_token = DOMAIN_BUS_PCI_DEVICE_MSIX,
23115c72f82SThomas Gleixner },
23215c72f82SThomas Gleixner };
23315c72f82SThomas Gleixner
pci_match_device_domain(struct pci_dev * pdev,enum irq_domain_bus_token bus_token)23415c72f82SThomas Gleixner static bool pci_match_device_domain(struct pci_dev *pdev, enum irq_domain_bus_token bus_token)
23515c72f82SThomas Gleixner {
23615c72f82SThomas Gleixner return msi_match_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, bus_token);
23715c72f82SThomas Gleixner }
23815c72f82SThomas Gleixner
pci_create_device_domain(struct pci_dev * pdev,const struct msi_domain_template * tmpl,unsigned int hwsize)23915c72f82SThomas Gleixner static bool pci_create_device_domain(struct pci_dev *pdev, const struct msi_domain_template *tmpl,
24015c72f82SThomas Gleixner unsigned int hwsize)
24115c72f82SThomas Gleixner {
24215c72f82SThomas Gleixner struct irq_domain *domain = dev_get_msi_domain(&pdev->dev);
24315c72f82SThomas Gleixner
24415c72f82SThomas Gleixner if (!domain || !irq_domain_is_msi_parent(domain))
24515c72f82SThomas Gleixner return true;
24615c72f82SThomas Gleixner
24715c72f82SThomas Gleixner return msi_create_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, tmpl,
24815c72f82SThomas Gleixner hwsize, NULL, NULL);
24915c72f82SThomas Gleixner }
25015c72f82SThomas Gleixner
25115c72f82SThomas Gleixner /**
25215c72f82SThomas Gleixner * pci_setup_msi_device_domain - Setup a device MSI interrupt domain
25315c72f82SThomas Gleixner * @pdev: The PCI device to create the domain on
25415c72f82SThomas Gleixner *
25515c72f82SThomas Gleixner * Return:
25615c72f82SThomas Gleixner * True when:
25715c72f82SThomas Gleixner * - The device does not have a MSI parent irq domain associated,
25815c72f82SThomas Gleixner * which keeps the legacy architecture specific and the global
25915c72f82SThomas Gleixner * PCI/MSI domain models working
26015c72f82SThomas Gleixner * - The MSI domain exists already
26115c72f82SThomas Gleixner * - The MSI domain was successfully allocated
26215c72f82SThomas Gleixner * False when:
26315c72f82SThomas Gleixner * - MSI-X is enabled
26415c72f82SThomas Gleixner * - The domain creation fails.
26515c72f82SThomas Gleixner *
26615c72f82SThomas Gleixner * The created MSI domain is preserved until:
26715c72f82SThomas Gleixner * - The device is removed
26815c72f82SThomas Gleixner * - MSI is disabled and a MSI-X domain is created
26915c72f82SThomas Gleixner */
pci_setup_msi_device_domain(struct pci_dev * pdev)27015c72f82SThomas Gleixner bool pci_setup_msi_device_domain(struct pci_dev *pdev)
27115c72f82SThomas Gleixner {
27215c72f82SThomas Gleixner if (WARN_ON_ONCE(pdev->msix_enabled))
27315c72f82SThomas Gleixner return false;
27415c72f82SThomas Gleixner
27515c72f82SThomas Gleixner if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
27615c72f82SThomas Gleixner return true;
27715c72f82SThomas Gleixner if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
27815c72f82SThomas Gleixner msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
27915c72f82SThomas Gleixner
28015c72f82SThomas Gleixner return pci_create_device_domain(pdev, &pci_msi_template, 1);
28115c72f82SThomas Gleixner }
28215c72f82SThomas Gleixner
28315c72f82SThomas Gleixner /**
28415c72f82SThomas Gleixner * pci_setup_msix_device_domain - Setup a device MSI-X interrupt domain
28515c72f82SThomas Gleixner * @pdev: The PCI device to create the domain on
28615c72f82SThomas Gleixner * @hwsize: The size of the MSI-X vector table
28715c72f82SThomas Gleixner *
28815c72f82SThomas Gleixner * Return:
28915c72f82SThomas Gleixner * True when:
29015c72f82SThomas Gleixner * - The device does not have a MSI parent irq domain associated,
29115c72f82SThomas Gleixner * which keeps the legacy architecture specific and the global
29215c72f82SThomas Gleixner * PCI/MSI domain models working
29315c72f82SThomas Gleixner * - The MSI-X domain exists already
29415c72f82SThomas Gleixner * - The MSI-X domain was successfully allocated
29515c72f82SThomas Gleixner * False when:
29615c72f82SThomas Gleixner * - MSI is enabled
29715c72f82SThomas Gleixner * - The domain creation fails.
29815c72f82SThomas Gleixner *
29915c72f82SThomas Gleixner * The created MSI-X domain is preserved until:
30015c72f82SThomas Gleixner * - The device is removed
30115c72f82SThomas Gleixner * - MSI-X is disabled and a MSI domain is created
30215c72f82SThomas Gleixner */
pci_setup_msix_device_domain(struct pci_dev * pdev,unsigned int hwsize)30315c72f82SThomas Gleixner bool pci_setup_msix_device_domain(struct pci_dev *pdev, unsigned int hwsize)
30415c72f82SThomas Gleixner {
30515c72f82SThomas Gleixner if (WARN_ON_ONCE(pdev->msi_enabled))
30615c72f82SThomas Gleixner return false;
30715c72f82SThomas Gleixner
30815c72f82SThomas Gleixner if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
30915c72f82SThomas Gleixner return true;
31015c72f82SThomas Gleixner if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
31115c72f82SThomas Gleixner msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
31215c72f82SThomas Gleixner
31315c72f82SThomas Gleixner return pci_create_device_domain(pdev, &pci_msix_template, hwsize);
31415c72f82SThomas Gleixner }
31515c72f82SThomas Gleixner
316d2a463b2SThomas Gleixner /**
317d2a463b2SThomas Gleixner * pci_msi_domain_supports - Check for support of a particular feature flag
318d2a463b2SThomas Gleixner * @pdev: The PCI device to operate on
319d2a463b2SThomas Gleixner * @feature_mask: The feature mask to check for (full match)
320d2a463b2SThomas Gleixner * @mode: If ALLOW_LEGACY this grants the feature when there is no irq domain
321d2a463b2SThomas Gleixner * associated to the device. If DENY_LEGACY the lack of an irq domain
322d2a463b2SThomas Gleixner * makes the feature unsupported
323d2a463b2SThomas Gleixner */
pci_msi_domain_supports(struct pci_dev * pdev,unsigned int feature_mask,enum support_mode mode)324d2a463b2SThomas Gleixner bool pci_msi_domain_supports(struct pci_dev *pdev, unsigned int feature_mask,
325d2a463b2SThomas Gleixner enum support_mode mode)
326d2a463b2SThomas Gleixner {
327d2a463b2SThomas Gleixner struct msi_domain_info *info;
328d2a463b2SThomas Gleixner struct irq_domain *domain;
32915c72f82SThomas Gleixner unsigned int supported;
330d2a463b2SThomas Gleixner
331d2a463b2SThomas Gleixner domain = dev_get_msi_domain(&pdev->dev);
332d2a463b2SThomas Gleixner
333*b1f7476eSThomas Gleixner if (!domain || !irq_domain_is_hierarchy(domain)) {
334*b1f7476eSThomas Gleixner if (IS_ENABLED(CONFIG_PCI_MSI_ARCH_FALLBACKS))
335d2a463b2SThomas Gleixner return mode == ALLOW_LEGACY;
336*b1f7476eSThomas Gleixner return false;
337*b1f7476eSThomas Gleixner }
33815c72f82SThomas Gleixner
33915c72f82SThomas Gleixner if (!irq_domain_is_msi_parent(domain)) {
34015c72f82SThomas Gleixner /*
34115c72f82SThomas Gleixner * For "global" PCI/MSI interrupt domains the associated
34286b4ad7dSBjorn Helgaas * msi_domain_info::flags is the authoritative source of
34315c72f82SThomas Gleixner * information.
34415c72f82SThomas Gleixner */
345d2a463b2SThomas Gleixner info = domain->host_data;
34615c72f82SThomas Gleixner supported = info->flags;
34715c72f82SThomas Gleixner } else {
34815c72f82SThomas Gleixner /*
34915c72f82SThomas Gleixner * For MSI parent domains the supported feature set
35086b4ad7dSBjorn Helgaas * is available in the parent ops. This makes checks
35115c72f82SThomas Gleixner * possible before actually instantiating the
35215c72f82SThomas Gleixner * per device domain because the parent is never
35315c72f82SThomas Gleixner * expanding the PCI/MSI functionality.
35415c72f82SThomas Gleixner */
35515c72f82SThomas Gleixner supported = domain->msi_parent_ops->supported_flags;
35615c72f82SThomas Gleixner }
35715c72f82SThomas Gleixner
35815c72f82SThomas Gleixner return (supported & feature_mask) == feature_mask;
359d2a463b2SThomas Gleixner }
360d2a463b2SThomas Gleixner
3610194425aSThomas Gleixner /**
3620194425aSThomas Gleixner * pci_create_ims_domain - Create a secondary IMS domain for a PCI device
3630194425aSThomas Gleixner * @pdev: The PCI device to operate on
3640194425aSThomas Gleixner * @template: The MSI info template which describes the domain
3650194425aSThomas Gleixner * @hwsize: The size of the hardware entry table or 0 if the domain
3660194425aSThomas Gleixner * is purely software managed
3670194425aSThomas Gleixner * @data: Optional pointer to domain specific data to be stored
3680194425aSThomas Gleixner * in msi_domain_info::data
3690194425aSThomas Gleixner *
3700194425aSThomas Gleixner * Return: True on success, false otherwise
3710194425aSThomas Gleixner *
3720194425aSThomas Gleixner * An IMS domain is expected to have the following constraints:
3730194425aSThomas Gleixner * - The index space is managed by the core code
3740194425aSThomas Gleixner *
3750194425aSThomas Gleixner * - There is no requirement for consecutive index ranges
3760194425aSThomas Gleixner *
3770194425aSThomas Gleixner * - The interrupt chip must provide the following callbacks:
3780194425aSThomas Gleixner * - irq_mask()
3790194425aSThomas Gleixner * - irq_unmask()
3800194425aSThomas Gleixner * - irq_write_msi_msg()
3810194425aSThomas Gleixner *
3820194425aSThomas Gleixner * - The interrupt chip must provide the following optional callbacks
3830194425aSThomas Gleixner * when the irq_mask(), irq_unmask() and irq_write_msi_msg() callbacks
3840194425aSThomas Gleixner * cannot operate directly on hardware, e.g. in the case that the
3850194425aSThomas Gleixner * interrupt message store is in queue memory:
3860194425aSThomas Gleixner * - irq_bus_lock()
3870194425aSThomas Gleixner * - irq_bus_unlock()
3880194425aSThomas Gleixner *
3890194425aSThomas Gleixner * These callbacks are invoked from preemptible task context and are
3900194425aSThomas Gleixner * allowed to sleep. In this case the mandatory callbacks above just
3910194425aSThomas Gleixner * store the information. The irq_bus_unlock() callback is supposed
3920194425aSThomas Gleixner * to make the change effective before returning.
3930194425aSThomas Gleixner *
3940194425aSThomas Gleixner * - Interrupt affinity setting is handled by the underlying parent
3950194425aSThomas Gleixner * interrupt domain and communicated to the IMS domain via
3960194425aSThomas Gleixner * irq_write_msi_msg().
3970194425aSThomas Gleixner *
3980194425aSThomas Gleixner * The domain is automatically destroyed when the PCI device is removed.
3990194425aSThomas Gleixner */
pci_create_ims_domain(struct pci_dev * pdev,const struct msi_domain_template * template,unsigned int hwsize,void * data)4000194425aSThomas Gleixner bool pci_create_ims_domain(struct pci_dev *pdev, const struct msi_domain_template *template,
4010194425aSThomas Gleixner unsigned int hwsize, void *data)
4020194425aSThomas Gleixner {
4030194425aSThomas Gleixner struct irq_domain *domain = dev_get_msi_domain(&pdev->dev);
4040194425aSThomas Gleixner
4050194425aSThomas Gleixner if (!domain || !irq_domain_is_msi_parent(domain))
4060194425aSThomas Gleixner return false;
4070194425aSThomas Gleixner
4080194425aSThomas Gleixner if (template->info.bus_token != DOMAIN_BUS_PCI_DEVICE_IMS ||
4090194425aSThomas Gleixner !(template->info.flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS) ||
4100194425aSThomas Gleixner !(template->info.flags & MSI_FLAG_FREE_MSI_DESCS) ||
4110194425aSThomas Gleixner !template->chip.irq_mask || !template->chip.irq_unmask ||
4120194425aSThomas Gleixner !template->chip.irq_write_msi_msg || template->chip.irq_set_affinity)
4130194425aSThomas Gleixner return false;
4140194425aSThomas Gleixner
4150194425aSThomas Gleixner return msi_create_device_irq_domain(&pdev->dev, MSI_SECONDARY_DOMAIN, template,
4160194425aSThomas Gleixner hwsize, data, NULL);
4170194425aSThomas Gleixner }
4180194425aSThomas Gleixner EXPORT_SYMBOL_GPL(pci_create_ims_domain);
4190194425aSThomas Gleixner
420aa423ac4SThomas Gleixner /*
421aa423ac4SThomas Gleixner * Users of the generic MSI infrastructure expect a device to have a single ID,
422aa423ac4SThomas Gleixner * so with DMA aliases we have to pick the least-worst compromise. Devices with
423aa423ac4SThomas Gleixner * DMA phantom functions tend to still emit MSIs from the real function number,
424aa423ac4SThomas Gleixner * so we ignore those and only consider topological aliases where either the
425aa423ac4SThomas Gleixner * alias device or RID appears on a different bus number. We also make the
426aa423ac4SThomas Gleixner * reasonable assumption that bridges are walked in an upstream direction (so
427aa423ac4SThomas Gleixner * the last one seen wins), and the much braver assumption that the most likely
428aa423ac4SThomas Gleixner * case is that of PCI->PCIe so we should always use the alias RID. This echoes
429aa423ac4SThomas Gleixner * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
430aa423ac4SThomas Gleixner * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
431aa423ac4SThomas Gleixner * for taking ownership all we can really do is close our eyes and hope...
432aa423ac4SThomas Gleixner */
get_msi_id_cb(struct pci_dev * pdev,u16 alias,void * data)433aa423ac4SThomas Gleixner static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
434aa423ac4SThomas Gleixner {
435aa423ac4SThomas Gleixner u32 *pa = data;
436aa423ac4SThomas Gleixner u8 bus = PCI_BUS_NUM(*pa);
437aa423ac4SThomas Gleixner
438aa423ac4SThomas Gleixner if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
439aa423ac4SThomas Gleixner *pa = alias;
440aa423ac4SThomas Gleixner
441aa423ac4SThomas Gleixner return 0;
442aa423ac4SThomas Gleixner }
443aa423ac4SThomas Gleixner
444aa423ac4SThomas Gleixner /**
445aa423ac4SThomas Gleixner * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
446aa423ac4SThomas Gleixner * @domain: The interrupt domain
447aa423ac4SThomas Gleixner * @pdev: The PCI device.
448aa423ac4SThomas Gleixner *
449aa423ac4SThomas Gleixner * The RID for a device is formed from the alias, with a firmware
450aa423ac4SThomas Gleixner * supplied mapping applied
451aa423ac4SThomas Gleixner *
452aa423ac4SThomas Gleixner * Returns: The RID.
453aa423ac4SThomas Gleixner */
pci_msi_domain_get_msi_rid(struct irq_domain * domain,struct pci_dev * pdev)454aa423ac4SThomas Gleixner u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
455aa423ac4SThomas Gleixner {
456aa423ac4SThomas Gleixner struct device_node *of_node;
457aa423ac4SThomas Gleixner u32 rid = pci_dev_id(pdev);
458aa423ac4SThomas Gleixner
459aa423ac4SThomas Gleixner pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
460aa423ac4SThomas Gleixner
461aa423ac4SThomas Gleixner of_node = irq_domain_get_of_node(domain);
462aa423ac4SThomas Gleixner rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
463aa423ac4SThomas Gleixner iort_msi_map_id(&pdev->dev, rid);
464aa423ac4SThomas Gleixner
465aa423ac4SThomas Gleixner return rid;
466aa423ac4SThomas Gleixner }
467aa423ac4SThomas Gleixner
468aa423ac4SThomas Gleixner /**
469aa423ac4SThomas Gleixner * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
470aa423ac4SThomas Gleixner * @pdev: The PCI device
471aa423ac4SThomas Gleixner *
472aa423ac4SThomas Gleixner * Use the firmware data to find a device-specific MSI domain
473aa423ac4SThomas Gleixner * (i.e. not one that is set as a default).
474aa423ac4SThomas Gleixner *
475aa423ac4SThomas Gleixner * Returns: The corresponding MSI domain or NULL if none has been found.
476aa423ac4SThomas Gleixner */
pci_msi_get_device_domain(struct pci_dev * pdev)477aa423ac4SThomas Gleixner struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
478aa423ac4SThomas Gleixner {
479aa423ac4SThomas Gleixner struct irq_domain *dom;
480aa423ac4SThomas Gleixner u32 rid = pci_dev_id(pdev);
481aa423ac4SThomas Gleixner
482aa423ac4SThomas Gleixner pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
483aa423ac4SThomas Gleixner dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
484aa423ac4SThomas Gleixner if (!dom)
485aa423ac4SThomas Gleixner dom = iort_get_device_domain(&pdev->dev, rid,
486aa423ac4SThomas Gleixner DOMAIN_BUS_PCI_MSI);
487aa423ac4SThomas Gleixner return dom;
488aa423ac4SThomas Gleixner }
489