genirq/msi: Handle PCI/MSI allocation fail in core code
[linux-2.6-microblaze.git] / drivers / pci / msi / irqdomain.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCI Message Signaled Interrupt (MSI) - irqdomain support
4  */
5 #include <linux/acpi_iort.h>
6 #include <linux/irqdomain.h>
7 #include <linux/of_irq.h>
8
9 #include "msi.h"
10
11 int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
12 {
13         struct irq_domain *domain;
14
15         domain = dev_get_msi_domain(&dev->dev);
16         if (domain && irq_domain_is_hierarchy(domain))
17                 return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
18
19         return pci_msi_legacy_setup_msi_irqs(dev, nvec, type);
20 }
21
22 void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
23 {
24         struct irq_domain *domain;
25
26         domain = dev_get_msi_domain(&dev->dev);
27         if (domain && irq_domain_is_hierarchy(domain))
28                 msi_domain_free_irqs(domain, &dev->dev);
29         else
30                 pci_msi_legacy_teardown_msi_irqs(dev);
31 }
32
33 /**
34  * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
35  * @irq_data:   Pointer to interrupt data of the MSI interrupt
36  * @msg:        Pointer to the message
37  */
38 static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
39 {
40         struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
41
42         /*
43          * For MSI-X desc->irq is always equal to irq_data->irq. For
44          * MSI only the first interrupt of MULTI MSI passes the test.
45          */
46         if (desc->irq == irq_data->irq)
47                 __pci_write_msi_msg(desc, msg);
48 }
49
50 /**
51  * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
52  * @desc:       Pointer to the MSI descriptor
53  *
54  * The ID number is only used within the irqdomain.
55  */
56 static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
57 {
58         struct pci_dev *dev = msi_desc_to_pci_dev(desc);
59
60         return (irq_hw_number_t)desc->pci.msi_attrib.entry_nr |
61                 pci_dev_id(dev) << 11 |
62                 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
63 }
64
65 static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
66 {
67         return !desc->pci.msi_attrib.is_msix && desc->nvec_used > 1;
68 }
69
70 /**
71  * pci_msi_domain_check_cap - Verify that @domain supports the capabilities
72  *                            for @dev
73  * @domain:     The interrupt domain to check
74  * @info:       The domain info for verification
75  * @dev:        The device to check
76  *
77  * Returns:
78  *  0 if the functionality is supported
79  *  1 if Multi MSI is requested, but the domain does not support it
80  *  -ENOTSUPP otherwise
81  */
82 static int pci_msi_domain_check_cap(struct irq_domain *domain,
83                                     struct msi_domain_info *info,
84                                     struct device *dev)
85 {
86         struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
87
88         /* Special handling to support __pci_enable_msi_range() */
89         if (pci_msi_desc_is_multi_msi(desc) &&
90             !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
91                 return 1;
92         else if (desc->pci.msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
93                 return -ENOTSUPP;
94
95         return 0;
96 }
97
98 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
99                                     struct msi_desc *desc)
100 {
101         arg->desc = desc;
102         arg->hwirq = pci_msi_domain_calc_hwirq(desc);
103 }
104
105 static struct msi_domain_ops pci_msi_domain_ops_default = {
106         .set_desc       = pci_msi_domain_set_desc,
107         .msi_check      = pci_msi_domain_check_cap,
108 };
109
110 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
111 {
112         struct msi_domain_ops *ops = info->ops;
113
114         if (ops == NULL) {
115                 info->ops = &pci_msi_domain_ops_default;
116         } else {
117                 if (ops->set_desc == NULL)
118                         ops->set_desc = pci_msi_domain_set_desc;
119                 if (ops->msi_check == NULL)
120                         ops->msi_check = pci_msi_domain_check_cap;
121         }
122 }
123
124 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
125 {
126         struct irq_chip *chip = info->chip;
127
128         BUG_ON(!chip);
129         if (!chip->irq_write_msi_msg)
130                 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
131         if (!chip->irq_mask)
132                 chip->irq_mask = pci_msi_mask_irq;
133         if (!chip->irq_unmask)
134                 chip->irq_unmask = pci_msi_unmask_irq;
135 }
136
137 /**
138  * pci_msi_create_irq_domain - Create a MSI interrupt domain
139  * @fwnode:     Optional fwnode of the interrupt controller
140  * @info:       MSI domain info
141  * @parent:     Parent irq domain
142  *
143  * Updates the domain and chip ops and creates a MSI interrupt domain.
144  *
145  * Returns:
146  * A domain pointer or NULL in case of failure.
147  */
148 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
149                                              struct msi_domain_info *info,
150                                              struct irq_domain *parent)
151 {
152         struct irq_domain *domain;
153
154         if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
155                 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
156
157         if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
158                 pci_msi_domain_update_dom_ops(info);
159         if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
160                 pci_msi_domain_update_chip_ops(info);
161
162         info->flags |= MSI_FLAG_ACTIVATE_EARLY;
163         if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
164                 info->flags |= MSI_FLAG_MUST_REACTIVATE;
165
166         /* PCI-MSI is oneshot-safe */
167         info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
168
169         domain = msi_create_irq_domain(fwnode, info, parent);
170         if (!domain)
171                 return NULL;
172
173         irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
174         return domain;
175 }
176 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
177
178 /*
179  * Users of the generic MSI infrastructure expect a device to have a single ID,
180  * so with DMA aliases we have to pick the least-worst compromise. Devices with
181  * DMA phantom functions tend to still emit MSIs from the real function number,
182  * so we ignore those and only consider topological aliases where either the
183  * alias device or RID appears on a different bus number. We also make the
184  * reasonable assumption that bridges are walked in an upstream direction (so
185  * the last one seen wins), and the much braver assumption that the most likely
186  * case is that of PCI->PCIe so we should always use the alias RID. This echoes
187  * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
188  * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
189  * for taking ownership all we can really do is close our eyes and hope...
190  */
191 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
192 {
193         u32 *pa = data;
194         u8 bus = PCI_BUS_NUM(*pa);
195
196         if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
197                 *pa = alias;
198
199         return 0;
200 }
201
202 /**
203  * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
204  * @domain:     The interrupt domain
205  * @pdev:       The PCI device.
206  *
207  * The RID for a device is formed from the alias, with a firmware
208  * supplied mapping applied
209  *
210  * Returns: The RID.
211  */
212 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
213 {
214         struct device_node *of_node;
215         u32 rid = pci_dev_id(pdev);
216
217         pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
218
219         of_node = irq_domain_get_of_node(domain);
220         rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
221                         iort_msi_map_id(&pdev->dev, rid);
222
223         return rid;
224 }
225
226 /**
227  * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
228  * @pdev:       The PCI device
229  *
230  * Use the firmware data to find a device-specific MSI domain
231  * (i.e. not one that is set as a default).
232  *
233  * Returns: The corresponding MSI domain or NULL if none has been found.
234  */
235 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
236 {
237         struct irq_domain *dom;
238         u32 rid = pci_dev_id(pdev);
239
240         pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
241         dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
242         if (!dom)
243                 dom = iort_get_device_domain(&pdev->dev, rid,
244                                              DOMAIN_BUS_PCI_MSI);
245         return dom;
246 }
247
248 /**
249  * pci_dev_has_special_msi_domain - Check whether the device is handled by
250  *                                  a non-standard PCI-MSI domain
251  * @pdev:       The PCI device to check.
252  *
253  * Returns: True if the device irqdomain or the bus irqdomain is
254  * non-standard PCI/MSI.
255  */
256 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev)
257 {
258         struct irq_domain *dom = dev_get_msi_domain(&pdev->dev);
259
260         if (!dom)
261                 dom = dev_get_msi_domain(&pdev->bus->dev);
262
263         if (!dom)
264                 return true;
265
266         return dom->bus_token != DOMAIN_BUS_PCI_MSI;
267 }