Merge tag 'uml-for-linus-6.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / pci / msi / irqdomain.c
blobd7ba8795d60f81622744c58f44db6dada5e8d7e9
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>
9 #include "msi.h"
11 int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
13 struct irq_domain *domain;
15 domain = dev_get_msi_domain(&dev->dev);
16 if (domain && irq_domain_is_hierarchy(domain))
17 return msi_domain_alloc_irqs_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN, nvec);
19 return pci_msi_legacy_setup_msi_irqs(dev, nvec, type);
22 void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
24 struct irq_domain *domain;
26 domain = dev_get_msi_domain(&dev->dev);
27 if (domain && irq_domain_is_hierarchy(domain)) {
28 msi_domain_free_irqs_all_locked(&dev->dev, MSI_DEFAULT_DOMAIN);
29 } else {
30 pci_msi_legacy_teardown_msi_irqs(dev);
31 msi_free_msi_descs(&dev->dev);
35 /**
36 * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
37 * @irq_data: Pointer to interrupt data of the MSI interrupt
38 * @msg: Pointer to the message
40 static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
42 struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
45 * For MSI-X desc->irq is always equal to irq_data->irq. For
46 * MSI only the first interrupt of MULTI MSI passes the test.
48 if (desc->irq == irq_data->irq)
49 __pci_write_msi_msg(desc, msg);
52 /**
53 * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
54 * @desc: Pointer to the MSI descriptor
56 * The ID number is only used within the irqdomain.
58 static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc)
60 struct pci_dev *dev = msi_desc_to_pci_dev(desc);
62 return (irq_hw_number_t)desc->msi_index |
63 pci_dev_id(dev) << 11 |
64 ((irq_hw_number_t)(pci_domain_nr(dev->bus) & 0xFFFFFFFF)) << 27;
67 static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
68 struct msi_desc *desc)
70 arg->desc = desc;
71 arg->hwirq = pci_msi_domain_calc_hwirq(desc);
74 static struct msi_domain_ops pci_msi_domain_ops_default = {
75 .set_desc = pci_msi_domain_set_desc,
78 static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
80 struct msi_domain_ops *ops = info->ops;
82 if (ops == NULL) {
83 info->ops = &pci_msi_domain_ops_default;
84 } else {
85 if (ops->set_desc == NULL)
86 ops->set_desc = pci_msi_domain_set_desc;
90 static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
92 struct irq_chip *chip = info->chip;
94 BUG_ON(!chip);
95 if (!chip->irq_write_msi_msg)
96 chip->irq_write_msi_msg = pci_msi_domain_write_msg;
97 if (!chip->irq_mask)
98 chip->irq_mask = pci_msi_mask_irq;
99 if (!chip->irq_unmask)
100 chip->irq_unmask = pci_msi_unmask_irq;
104 * pci_msi_create_irq_domain - Create a MSI interrupt domain
105 * @fwnode: Optional fwnode of the interrupt controller
106 * @info: MSI domain info
107 * @parent: Parent irq domain
109 * Updates the domain and chip ops and creates a MSI interrupt domain.
111 * Returns:
112 * A domain pointer or NULL in case of failure.
114 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
115 struct msi_domain_info *info,
116 struct irq_domain *parent)
118 if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
119 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
121 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
122 pci_msi_domain_update_dom_ops(info);
123 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
124 pci_msi_domain_update_chip_ops(info);
126 /* Let the core code free MSI descriptors when freeing interrupts */
127 info->flags |= MSI_FLAG_FREE_MSI_DESCS;
129 info->flags |= MSI_FLAG_ACTIVATE_EARLY | MSI_FLAG_DEV_SYSFS;
130 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
131 info->flags |= MSI_FLAG_MUST_REACTIVATE;
133 /* PCI-MSI is oneshot-safe */
134 info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
135 /* Let the core update the bus token */
136 info->bus_token = DOMAIN_BUS_PCI_MSI;
138 return msi_create_irq_domain(fwnode, info, parent);
140 EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
143 * Per device MSI[-X] domain functionality
145 static void pci_device_domain_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
147 arg->desc = desc;
148 arg->hwirq = desc->msi_index;
151 static __always_inline void cond_mask_parent(struct irq_data *data)
153 struct msi_domain_info *info = data->domain->host_data;
155 if (unlikely(info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT))
156 irq_chip_mask_parent(data);
159 static __always_inline void cond_unmask_parent(struct irq_data *data)
161 struct msi_domain_info *info = data->domain->host_data;
163 if (unlikely(info->flags & MSI_FLAG_PCI_MSI_MASK_PARENT))
164 irq_chip_unmask_parent(data);
167 static void pci_irq_mask_msi(struct irq_data *data)
169 struct msi_desc *desc = irq_data_get_msi_desc(data);
171 pci_msi_mask(desc, BIT(data->irq - desc->irq));
172 cond_mask_parent(data);
175 static void pci_irq_unmask_msi(struct irq_data *data)
177 struct msi_desc *desc = irq_data_get_msi_desc(data);
179 cond_unmask_parent(data);
180 pci_msi_unmask(desc, BIT(data->irq - desc->irq));
183 #ifdef CONFIG_GENERIC_IRQ_RESERVATION_MODE
184 # define MSI_REACTIVATE MSI_FLAG_MUST_REACTIVATE
185 #else
186 # define MSI_REACTIVATE 0
187 #endif
189 #define MSI_COMMON_FLAGS (MSI_FLAG_FREE_MSI_DESCS | \
190 MSI_FLAG_ACTIVATE_EARLY | \
191 MSI_FLAG_DEV_SYSFS | \
192 MSI_REACTIVATE)
194 static const struct msi_domain_template pci_msi_template = {
195 .chip = {
196 .name = "PCI-MSI",
197 .irq_mask = pci_irq_mask_msi,
198 .irq_unmask = pci_irq_unmask_msi,
199 .irq_write_msi_msg = pci_msi_domain_write_msg,
200 .flags = IRQCHIP_ONESHOT_SAFE,
203 .ops = {
204 .set_desc = pci_device_domain_set_desc,
207 .info = {
208 .flags = MSI_COMMON_FLAGS | MSI_FLAG_MULTI_PCI_MSI,
209 .bus_token = DOMAIN_BUS_PCI_DEVICE_MSI,
213 static void pci_irq_mask_msix(struct irq_data *data)
215 pci_msix_mask(irq_data_get_msi_desc(data));
216 cond_mask_parent(data);
219 static void pci_irq_unmask_msix(struct irq_data *data)
221 cond_unmask_parent(data);
222 pci_msix_unmask(irq_data_get_msi_desc(data));
225 static void pci_msix_prepare_desc(struct irq_domain *domain, msi_alloc_info_t *arg,
226 struct msi_desc *desc)
228 /* Don't fiddle with preallocated MSI descriptors */
229 if (!desc->pci.mask_base)
230 msix_prepare_msi_desc(to_pci_dev(desc->dev), desc);
233 static const struct msi_domain_template pci_msix_template = {
234 .chip = {
235 .name = "PCI-MSIX",
236 .irq_mask = pci_irq_mask_msix,
237 .irq_unmask = pci_irq_unmask_msix,
238 .irq_write_msi_msg = pci_msi_domain_write_msg,
239 .flags = IRQCHIP_ONESHOT_SAFE,
242 .ops = {
243 .prepare_desc = pci_msix_prepare_desc,
244 .set_desc = pci_device_domain_set_desc,
247 .info = {
248 .flags = MSI_COMMON_FLAGS | MSI_FLAG_PCI_MSIX |
249 MSI_FLAG_PCI_MSIX_ALLOC_DYN,
250 .bus_token = DOMAIN_BUS_PCI_DEVICE_MSIX,
254 static bool pci_match_device_domain(struct pci_dev *pdev, enum irq_domain_bus_token bus_token)
256 return msi_match_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, bus_token);
259 static bool pci_create_device_domain(struct pci_dev *pdev, const struct msi_domain_template *tmpl,
260 unsigned int hwsize)
262 struct irq_domain *domain = dev_get_msi_domain(&pdev->dev);
264 if (!domain || !irq_domain_is_msi_parent(domain))
265 return true;
267 return msi_create_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN, tmpl,
268 hwsize, NULL, NULL);
272 * pci_setup_msi_device_domain - Setup a device MSI interrupt domain
273 * @pdev: The PCI device to create the domain on
275 * Return:
276 * True when:
277 * - The device does not have a MSI parent irq domain associated,
278 * which keeps the legacy architecture specific and the global
279 * PCI/MSI domain models working
280 * - The MSI domain exists already
281 * - The MSI domain was successfully allocated
282 * False when:
283 * - MSI-X is enabled
284 * - The domain creation fails.
286 * The created MSI domain is preserved until:
287 * - The device is removed
288 * - MSI is disabled and a MSI-X domain is created
290 bool pci_setup_msi_device_domain(struct pci_dev *pdev)
292 if (WARN_ON_ONCE(pdev->msix_enabled))
293 return false;
295 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
296 return true;
297 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
298 msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
300 return pci_create_device_domain(pdev, &pci_msi_template, 1);
304 * pci_setup_msix_device_domain - Setup a device MSI-X interrupt domain
305 * @pdev: The PCI device to create the domain on
306 * @hwsize: The size of the MSI-X vector table
308 * Return:
309 * True when:
310 * - The device does not have a MSI parent irq domain associated,
311 * which keeps the legacy architecture specific and the global
312 * PCI/MSI domain models working
313 * - The MSI-X domain exists already
314 * - The MSI-X domain was successfully allocated
315 * False when:
316 * - MSI is enabled
317 * - The domain creation fails.
319 * The created MSI-X domain is preserved until:
320 * - The device is removed
321 * - MSI-X is disabled and a MSI domain is created
323 bool pci_setup_msix_device_domain(struct pci_dev *pdev, unsigned int hwsize)
325 if (WARN_ON_ONCE(pdev->msi_enabled))
326 return false;
328 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSIX))
329 return true;
330 if (pci_match_device_domain(pdev, DOMAIN_BUS_PCI_DEVICE_MSI))
331 msi_remove_device_irq_domain(&pdev->dev, MSI_DEFAULT_DOMAIN);
333 return pci_create_device_domain(pdev, &pci_msix_template, hwsize);
337 * pci_msi_domain_supports - Check for support of a particular feature flag
338 * @pdev: The PCI device to operate on
339 * @feature_mask: The feature mask to check for (full match)
340 * @mode: If ALLOW_LEGACY this grants the feature when there is no irq domain
341 * associated to the device. If DENY_LEGACY the lack of an irq domain
342 * makes the feature unsupported
344 bool pci_msi_domain_supports(struct pci_dev *pdev, unsigned int feature_mask,
345 enum support_mode mode)
347 struct msi_domain_info *info;
348 struct irq_domain *domain;
349 unsigned int supported;
351 domain = dev_get_msi_domain(&pdev->dev);
353 if (!domain || !irq_domain_is_hierarchy(domain)) {
354 if (IS_ENABLED(CONFIG_PCI_MSI_ARCH_FALLBACKS))
355 return mode == ALLOW_LEGACY;
356 return false;
359 if (!irq_domain_is_msi_parent(domain)) {
361 * For "global" PCI/MSI interrupt domains the associated
362 * msi_domain_info::flags is the authoritative source of
363 * information.
365 info = domain->host_data;
366 supported = info->flags;
367 } else {
369 * For MSI parent domains the supported feature set
370 * is available in the parent ops. This makes checks
371 * possible before actually instantiating the
372 * per device domain because the parent is never
373 * expanding the PCI/MSI functionality.
375 supported = domain->msi_parent_ops->supported_flags;
378 return (supported & feature_mask) == feature_mask;
382 * Users of the generic MSI infrastructure expect a device to have a single ID,
383 * so with DMA aliases we have to pick the least-worst compromise. Devices with
384 * DMA phantom functions tend to still emit MSIs from the real function number,
385 * so we ignore those and only consider topological aliases where either the
386 * alias device or RID appears on a different bus number. We also make the
387 * reasonable assumption that bridges are walked in an upstream direction (so
388 * the last one seen wins), and the much braver assumption that the most likely
389 * case is that of PCI->PCIe so we should always use the alias RID. This echoes
390 * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
391 * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
392 * for taking ownership all we can really do is close our eyes and hope...
394 static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
396 u32 *pa = data;
397 u8 bus = PCI_BUS_NUM(*pa);
399 if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
400 *pa = alias;
402 return 0;
406 * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
407 * @domain: The interrupt domain
408 * @pdev: The PCI device.
410 * The RID for a device is formed from the alias, with a firmware
411 * supplied mapping applied
413 * Returns: The RID.
415 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
417 struct device_node *of_node;
418 u32 rid = pci_dev_id(pdev);
420 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
422 of_node = irq_domain_get_of_node(domain);
423 rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) :
424 iort_msi_map_id(&pdev->dev, rid);
426 return rid;
430 * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
431 * @pdev: The PCI device
433 * Use the firmware data to find a device-specific MSI domain
434 * (i.e. not one that is set as a default).
436 * Returns: The corresponding MSI domain or NULL if none has been found.
438 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
440 struct irq_domain *dom;
441 u32 rid = pci_dev_id(pdev);
443 pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
444 dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI);
445 if (!dom)
446 dom = iort_get_device_domain(&pdev->dev, rid,
447 DOMAIN_BUS_PCI_MSI);
448 return dom;