1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Intel Corp.
4 * Author: Jiang Liu <jiang.liu@linux.intel.com>
6 * This file is licensed under GPLv2.
8 * This file contains common code to support Message Signaled Interrupts for
9 * PCI compatible and non PCI compatible devices.
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/msi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/pci.h>
20 #include "internals.h"
23 * alloc_msi_entry - Allocate an initialized msi_desc
24 * @dev: Pointer to the device for which this is allocated
25 * @nvec: The number of vectors used in this entry
26 * @affinity: Optional pointer to an affinity mask array size of @nvec
28 * If @affinity is not %NULL then an affinity array[@nvec] is allocated
29 * and the affinity masks and flags from @affinity are copied.
31 * Return: pointer to allocated &msi_desc on success or %NULL on failure
33 struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
34 const struct irq_affinity_desc *affinity)
36 struct msi_desc *desc;
38 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
42 INIT_LIST_HEAD(&desc->list);
44 desc->nvec_used = nvec;
46 desc->affinity = kmemdup(affinity,
47 nvec * sizeof(*desc->affinity), GFP_KERNEL);
48 if (!desc->affinity) {
57 void free_msi_entry(struct msi_desc *entry)
59 kfree(entry->affinity);
64 * msi_add_msi_desc - Allocate and initialize a MSI descriptor
65 * @dev: Pointer to the device for which the descriptor is allocated
66 * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
68 * Return: 0 on success or an appropriate failure code.
70 int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc)
72 struct msi_desc *desc;
74 lockdep_assert_held(&dev->msi.data->mutex);
76 desc = alloc_msi_entry(dev, init_desc->nvec_used, init_desc->affinity);
80 /* Copy the MSI index and type specific data to the new descriptor. */
81 desc->msi_index = init_desc->msi_index;
82 desc->pci = init_desc->pci;
84 list_add_tail(&desc->list, &dev->msi.data->list);
89 * msi_add_simple_msi_descs - Allocate and initialize MSI descriptors
90 * @dev: Pointer to the device for which the descriptors are allocated
91 * @index: Index for the first MSI descriptor
92 * @ndesc: Number of descriptors to allocate
94 * Return: 0 on success or an appropriate failure code.
96 static int msi_add_simple_msi_descs(struct device *dev, unsigned int index, unsigned int ndesc)
98 struct msi_desc *desc, *tmp;
102 lockdep_assert_held(&dev->msi.data->mutex);
104 for (i = 0; i < ndesc; i++) {
105 desc = alloc_msi_entry(dev, 1, NULL);
108 desc->msi_index = index + i;
109 list_add_tail(&desc->list, &list);
111 list_splice_tail(&list, &dev->msi.data->list);
115 list_for_each_entry_safe(desc, tmp, &list, list) {
116 list_del(&desc->list);
117 free_msi_entry(desc);
123 * msi_free_msi_descs_range - Free MSI descriptors of a device
124 * @dev: Device to free the descriptors
125 * @filter: Descriptor state filter
126 * @first_index: Index to start freeing from
127 * @last_index: Last index to be freed
129 void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter,
130 unsigned int first_index, unsigned int last_index)
132 struct msi_desc *desc;
134 lockdep_assert_held(&dev->msi.data->mutex);
136 msi_for_each_desc(desc, dev, filter) {
138 * Stupid for now to handle MSI device domain until the
139 * storage is switched over to an xarray.
141 if (desc->msi_index < first_index || desc->msi_index > last_index)
143 list_del(&desc->list);
144 free_msi_entry(desc);
148 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
153 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
155 struct msi_desc *entry = irq_get_msi_desc(irq);
157 __get_cached_msi_msg(entry, msg);
159 EXPORT_SYMBOL_GPL(get_cached_msi_msg);
161 static void msi_device_data_release(struct device *dev, void *res)
163 struct msi_device_data *md = res;
165 WARN_ON_ONCE(!list_empty(&md->list));
166 dev->msi.data = NULL;
170 * msi_setup_device_data - Setup MSI device data
171 * @dev: Device for which MSI device data should be set up
173 * Return: 0 on success, appropriate error code otherwise
175 * This can be called more than once for @dev. If the MSI device data is
176 * already allocated the call succeeds. The allocated memory is
177 * automatically released when the device is destroyed.
179 int msi_setup_device_data(struct device *dev)
181 struct msi_device_data *md;
186 md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
190 INIT_LIST_HEAD(&md->list);
191 mutex_init(&md->mutex);
198 * msi_lock_descs - Lock the MSI descriptor storage of a device
199 * @dev: Device to operate on
201 void msi_lock_descs(struct device *dev)
203 mutex_lock(&dev->msi.data->mutex);
205 EXPORT_SYMBOL_GPL(msi_lock_descs);
208 * msi_unlock_descs - Unlock the MSI descriptor storage of a device
209 * @dev: Device to operate on
211 void msi_unlock_descs(struct device *dev)
213 /* Clear the next pointer which was cached by the iterator */
214 dev->msi.data->__next = NULL;
215 mutex_unlock(&dev->msi.data->mutex);
217 EXPORT_SYMBOL_GPL(msi_unlock_descs);
219 static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter)
224 case MSI_DESC_NOTASSOCIATED:
226 case MSI_DESC_ASSOCIATED:
233 static struct msi_desc *msi_find_first_desc(struct device *dev, enum msi_desc_filter filter)
235 struct msi_desc *desc;
237 list_for_each_entry(desc, dev_to_msi_list(dev), list) {
238 if (msi_desc_match(desc, filter))
245 * msi_first_desc - Get the first MSI descriptor of a device
246 * @dev: Device to operate on
247 * @filter: Descriptor state filter
249 * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
250 * must be invoked before the call.
252 * Return: Pointer to the first MSI descriptor matching the search
253 * criteria, NULL if none found.
255 struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter)
257 struct msi_desc *desc;
259 if (WARN_ON_ONCE(!dev->msi.data))
262 lockdep_assert_held(&dev->msi.data->mutex);
264 desc = msi_find_first_desc(dev, filter);
265 dev->msi.data->__next = desc ? list_next_entry(desc, list) : NULL;
268 EXPORT_SYMBOL_GPL(msi_first_desc);
270 static struct msi_desc *__msi_next_desc(struct device *dev, enum msi_desc_filter filter,
271 struct msi_desc *from)
273 struct msi_desc *desc = from;
275 list_for_each_entry_from(desc, dev_to_msi_list(dev), list) {
276 if (msi_desc_match(desc, filter))
283 * msi_next_desc - Get the next MSI descriptor of a device
284 * @dev: Device to operate on
286 * The first invocation of msi_next_desc() has to be preceeded by a
287 * successful incovation of __msi_first_desc(). Consecutive invocations are
288 * only valid if the previous one was successful. All these operations have
289 * to be done within the same MSI mutex held region.
291 * Return: Pointer to the next MSI descriptor matching the search
292 * criteria, NULL if none found.
294 struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter)
296 struct msi_device_data *data = dev->msi.data;
297 struct msi_desc *desc;
299 if (WARN_ON_ONCE(!data))
302 lockdep_assert_held(&data->mutex);
307 desc = __msi_next_desc(dev, filter, data->__next);
308 dev->msi.data->__next = desc ? list_next_entry(desc, list) : NULL;
311 EXPORT_SYMBOL_GPL(msi_next_desc);
314 * msi_get_virq - Return Linux interrupt number of a MSI interrupt
315 * @dev: Device to operate on
316 * @index: MSI interrupt index to look for (0-based)
318 * Return: The Linux interrupt number on success (> 0), 0 if not found
320 unsigned int msi_get_virq(struct device *dev, unsigned int index)
322 struct msi_desc *desc;
328 pcimsi = dev_is_pci(dev) ? to_pci_dev(dev)->msi_enabled : false;
330 for_each_msi_entry(desc, dev) {
331 /* PCI-MSI has only one descriptor for multiple interrupts. */
333 if (desc->irq && index < desc->nvec_used)
334 return desc->irq + index;
339 * PCI-MSIX and platform MSI use a descriptor per
342 if (desc->msi_index == index)
347 EXPORT_SYMBOL_GPL(msi_get_virq);
350 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
353 /* MSI vs. MSIX is per device not per interrupt */
354 bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
356 return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
360 * msi_populate_sysfs - Populate msi_irqs sysfs entries for devices
361 * @dev: The device(PCI, platform etc) who will get sysfs entries
363 static const struct attribute_group **msi_populate_sysfs(struct device *dev)
365 const struct attribute_group **msi_irq_groups;
366 struct attribute **msi_attrs, *msi_attr;
367 struct device_attribute *msi_dev_attr;
368 struct attribute_group *msi_irq_group;
369 struct msi_desc *entry;
375 /* Determine how many msi entries we have */
376 for_each_msi_entry(entry, dev)
377 num_msi += entry->nvec_used;
381 /* Dynamically create the MSI attributes for the device */
382 msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL);
384 return ERR_PTR(-ENOMEM);
386 for_each_msi_entry(entry, dev) {
387 for (i = 0; i < entry->nvec_used; i++) {
388 msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
391 msi_attrs[count] = &msi_dev_attr->attr;
393 sysfs_attr_init(&msi_dev_attr->attr);
394 msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
396 if (!msi_dev_attr->attr.name)
398 msi_dev_attr->attr.mode = 0444;
399 msi_dev_attr->show = msi_mode_show;
404 msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
407 msi_irq_group->name = "msi_irqs";
408 msi_irq_group->attrs = msi_attrs;
410 msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL);
412 goto error_irq_group;
413 msi_irq_groups[0] = msi_irq_group;
415 ret = sysfs_create_groups(&dev->kobj, msi_irq_groups);
417 goto error_irq_groups;
419 return msi_irq_groups;
422 kfree(msi_irq_groups);
424 kfree(msi_irq_group);
427 msi_attr = msi_attrs[count];
429 msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
430 kfree(msi_attr->name);
433 msi_attr = msi_attrs[count];
440 * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
441 * @dev: The device (PCI, platform etc) which will get sysfs entries
443 int msi_device_populate_sysfs(struct device *dev)
445 const struct attribute_group **group = msi_populate_sysfs(dev);
448 return PTR_ERR(group);
449 dev->msi.data->attrs = group;
454 * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
455 * @dev: The device (PCI, platform etc) for which to remove
458 void msi_device_destroy_sysfs(struct device *dev)
460 const struct attribute_group **msi_irq_groups = dev->msi.data->attrs;
461 struct device_attribute *dev_attr;
462 struct attribute **msi_attrs;
465 dev->msi.data->attrs = NULL;
469 sysfs_remove_groups(&dev->kobj, msi_irq_groups);
470 msi_attrs = msi_irq_groups[0]->attrs;
471 while (msi_attrs[count]) {
472 dev_attr = container_of(msi_attrs[count], struct device_attribute, attr);
473 kfree(dev_attr->attr.name);
478 kfree(msi_irq_groups[0]);
479 kfree(msi_irq_groups);
483 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
484 static inline void irq_chip_write_msi_msg(struct irq_data *data,
487 data->chip->irq_write_msi_msg(data, msg);
490 static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
492 struct msi_domain_info *info = domain->host_data;
495 * If the MSI provider has messed with the second message and
496 * not advertized that it is level-capable, signal the breakage.
498 WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
499 (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
500 (msg[1].address_lo || msg[1].address_hi || msg[1].data));
504 * msi_domain_set_affinity - Generic affinity setter function for MSI domains
505 * @irq_data: The irq data associated to the interrupt
506 * @mask: The affinity mask to set
507 * @force: Flag to enforce setting (disable online checks)
509 * Intended to be used by MSI interrupt controllers which are
510 * implemented with hierarchical domains.
512 * Return: IRQ_SET_MASK_* result code
514 int msi_domain_set_affinity(struct irq_data *irq_data,
515 const struct cpumask *mask, bool force)
517 struct irq_data *parent = irq_data->parent_data;
518 struct msi_msg msg[2] = { [1] = { }, };
521 ret = parent->chip->irq_set_affinity(parent, mask, force);
522 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
523 BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
524 msi_check_level(irq_data->domain, msg);
525 irq_chip_write_msi_msg(irq_data, msg);
531 static int msi_domain_activate(struct irq_domain *domain,
532 struct irq_data *irq_data, bool early)
534 struct msi_msg msg[2] = { [1] = { }, };
536 BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
537 msi_check_level(irq_data->domain, msg);
538 irq_chip_write_msi_msg(irq_data, msg);
542 static void msi_domain_deactivate(struct irq_domain *domain,
543 struct irq_data *irq_data)
545 struct msi_msg msg[2];
547 memset(msg, 0, sizeof(msg));
548 irq_chip_write_msi_msg(irq_data, msg);
551 static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
552 unsigned int nr_irqs, void *arg)
554 struct msi_domain_info *info = domain->host_data;
555 struct msi_domain_ops *ops = info->ops;
556 irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
559 if (irq_find_mapping(domain, hwirq) > 0)
562 if (domain->parent) {
563 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
568 for (i = 0; i < nr_irqs; i++) {
569 ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
572 for (i--; i > 0; i--)
573 ops->msi_free(domain, info, virq + i);
575 irq_domain_free_irqs_top(domain, virq, nr_irqs);
583 static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
584 unsigned int nr_irqs)
586 struct msi_domain_info *info = domain->host_data;
589 if (info->ops->msi_free) {
590 for (i = 0; i < nr_irqs; i++)
591 info->ops->msi_free(domain, info, virq + i);
593 irq_domain_free_irqs_top(domain, virq, nr_irqs);
596 static const struct irq_domain_ops msi_domain_ops = {
597 .alloc = msi_domain_alloc,
598 .free = msi_domain_free,
599 .activate = msi_domain_activate,
600 .deactivate = msi_domain_deactivate,
603 static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
604 msi_alloc_info_t *arg)
609 static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
610 int nvec, msi_alloc_info_t *arg)
612 memset(arg, 0, sizeof(*arg));
616 static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
617 struct msi_desc *desc)
622 static int msi_domain_ops_init(struct irq_domain *domain,
623 struct msi_domain_info *info,
624 unsigned int virq, irq_hw_number_t hwirq,
625 msi_alloc_info_t *arg)
627 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
629 if (info->handler && info->handler_name) {
630 __irq_set_handler(virq, info->handler, 0, info->handler_name);
631 if (info->handler_data)
632 irq_set_handler_data(virq, info->handler_data);
637 static int msi_domain_ops_check(struct irq_domain *domain,
638 struct msi_domain_info *info,
644 static struct msi_domain_ops msi_domain_ops_default = {
645 .get_hwirq = msi_domain_ops_get_hwirq,
646 .msi_init = msi_domain_ops_init,
647 .msi_check = msi_domain_ops_check,
648 .msi_prepare = msi_domain_ops_prepare,
649 .set_desc = msi_domain_ops_set_desc,
650 .domain_alloc_irqs = __msi_domain_alloc_irqs,
651 .domain_free_irqs = __msi_domain_free_irqs,
654 static void msi_domain_update_dom_ops(struct msi_domain_info *info)
656 struct msi_domain_ops *ops = info->ops;
659 info->ops = &msi_domain_ops_default;
663 if (ops->domain_alloc_irqs == NULL)
664 ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs;
665 if (ops->domain_free_irqs == NULL)
666 ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs;
668 if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
671 if (ops->get_hwirq == NULL)
672 ops->get_hwirq = msi_domain_ops_default.get_hwirq;
673 if (ops->msi_init == NULL)
674 ops->msi_init = msi_domain_ops_default.msi_init;
675 if (ops->msi_check == NULL)
676 ops->msi_check = msi_domain_ops_default.msi_check;
677 if (ops->msi_prepare == NULL)
678 ops->msi_prepare = msi_domain_ops_default.msi_prepare;
679 if (ops->set_desc == NULL)
680 ops->set_desc = msi_domain_ops_default.set_desc;
683 static void msi_domain_update_chip_ops(struct msi_domain_info *info)
685 struct irq_chip *chip = info->chip;
687 BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
688 if (!chip->irq_set_affinity)
689 chip->irq_set_affinity = msi_domain_set_affinity;
693 * msi_create_irq_domain - Create an MSI interrupt domain
694 * @fwnode: Optional fwnode of the interrupt controller
695 * @info: MSI domain info
696 * @parent: Parent irq domain
698 * Return: pointer to the created &struct irq_domain or %NULL on failure
700 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
701 struct msi_domain_info *info,
702 struct irq_domain *parent)
704 struct irq_domain *domain;
706 msi_domain_update_dom_ops(info);
707 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
708 msi_domain_update_chip_ops(info);
710 domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
711 fwnode, &msi_domain_ops, info);
713 if (domain && !domain->name && info->chip)
714 domain->name = info->chip->name;
719 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
720 int nvec, msi_alloc_info_t *arg)
722 struct msi_domain_info *info = domain->host_data;
723 struct msi_domain_ops *ops = info->ops;
726 ret = ops->msi_check(domain, info, dev);
728 ret = ops->msi_prepare(domain, dev, nvec, arg);
733 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
734 int virq, int nvec, msi_alloc_info_t *arg)
736 struct msi_domain_info *info = domain->host_data;
737 struct msi_domain_ops *ops = info->ops;
738 struct msi_desc *desc;
741 for_each_msi_entry(desc, dev) {
742 /* Don't even try the multi-MSI brain damage. */
743 if (WARN_ON(!desc->irq || desc->nvec_used != 1)) {
748 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
751 ops->set_desc(arg, desc);
752 /* Assumes the domain mutex is held! */
753 ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1,
758 irq_set_msi_desc_off(desc->irq, 0, desc);
762 /* Mop up the damage */
763 for_each_msi_entry(desc, dev) {
764 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
767 irq_domain_free_irqs_common(domain, desc->irq, 1);
775 * Carefully check whether the device can use reservation mode. If
776 * reservation mode is enabled then the early activation will assign a
777 * dummy vector to the device. If the PCI/MSI device does not support
778 * masking of the entry then this can result in spurious interrupts when
779 * the device driver is not absolutely careful. But even then a malfunction
780 * of the hardware could result in a spurious interrupt on the dummy vector
781 * and render the device unusable. If the entry can be masked then the core
782 * logic will prevent the spurious interrupt and reservation mode can be
783 * used. For now reservation mode is restricted to PCI/MSI.
785 static bool msi_check_reservation_mode(struct irq_domain *domain,
786 struct msi_domain_info *info,
789 struct msi_desc *desc;
791 switch(domain->bus_token) {
792 case DOMAIN_BUS_PCI_MSI:
793 case DOMAIN_BUS_VMD_MSI:
799 if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
802 if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
806 * Checking the first MSI descriptor is sufficient. MSIX supports
807 * masking and MSI does so when the can_mask attribute is set.
809 desc = first_msi_entry(dev);
810 return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
813 static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
816 switch(domain->bus_token) {
817 case DOMAIN_BUS_PCI_MSI:
818 case DOMAIN_BUS_VMD_MSI:
819 if (IS_ENABLED(CONFIG_PCI_MSI))
826 /* Let a failed PCI multi MSI allocation retry */
827 if (desc->nvec_used > 1)
830 /* If there was a successful allocation let the caller know */
831 return allocated ? allocated : -ENOSPC;
834 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
837 struct msi_domain_info *info = domain->host_data;
838 struct msi_domain_ops *ops = info->ops;
839 struct irq_data *irq_data;
840 struct msi_desc *desc;
841 msi_alloc_info_t arg = { };
846 ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
850 for_each_msi_entry(desc, dev) {
851 ops->set_desc(&arg, desc);
853 virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
854 dev_to_node(dev), &arg, false,
857 return msi_handle_pci_fail(domain, desc, allocated);
859 for (i = 0; i < desc->nvec_used; i++) {
860 irq_set_msi_desc_off(virq, i, desc);
861 irq_debugfs_copy_devname(virq + i, dev);
866 can_reserve = msi_check_reservation_mode(domain, info, dev);
869 * This flag is set by the PCI layer as we need to activate
870 * the MSI entries before the PCI layer enables MSI in the
871 * card. Otherwise the card latches a random msi message.
873 if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY))
876 for_each_msi_vector(desc, i, dev) {
877 if (desc->irq == i) {
879 dev_dbg(dev, "irq [%d-%d] for MSI\n",
880 virq, virq + desc->nvec_used - 1);
883 irq_data = irq_domain_get_irq_data(domain, i);
885 irqd_clr_can_reserve(irq_data);
886 if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK)
887 irqd_set_msi_nomask_quirk(irq_data);
889 ret = irq_domain_activate_irq(irq_data, can_reserve);
896 * If these interrupts use reservation mode, clear the activated bit
897 * so request_irq() will assign the final vector.
900 for_each_msi_vector(desc, i, dev) {
901 irq_data = irq_domain_get_irq_data(domain, i);
902 irqd_clr_activated(irq_data);
908 static int msi_domain_add_simple_msi_descs(struct msi_domain_info *info,
910 unsigned int num_descs)
912 if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS))
915 return msi_add_simple_msi_descs(dev, 0, num_descs);
919 * msi_domain_alloc_irqs_descs_locked - Allocate interrupts from a MSI interrupt domain
920 * @domain: The domain to allocate from
921 * @dev: Pointer to device struct of the device for which the interrupts
923 * @nvec: The number of interrupts to allocate
925 * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
926 * pair. Use this for MSI irqdomains which implement their own vector
929 * Return: %0 on success or an error code.
931 int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev,
934 struct msi_domain_info *info = domain->host_data;
935 struct msi_domain_ops *ops = info->ops;
938 lockdep_assert_held(&dev->msi.data->mutex);
940 ret = msi_domain_add_simple_msi_descs(info, dev, nvec);
944 ret = ops->domain_alloc_irqs(domain, dev, nvec);
948 if (!(info->flags & MSI_FLAG_DEV_SYSFS))
951 ret = msi_device_populate_sysfs(dev);
957 msi_domain_free_irqs_descs_locked(domain, dev);
962 * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
963 * @domain: The domain to allocate from
964 * @dev: Pointer to device struct of the device for which the interrupts
966 * @nvec: The number of interrupts to allocate
968 * Return: %0 on success or an error code.
970 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, int nvec)
975 ret = msi_domain_alloc_irqs_descs_locked(domain, dev, nvec);
976 msi_unlock_descs(dev);
980 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
982 struct irq_data *irq_data;
983 struct msi_desc *desc;
986 for_each_msi_vector(desc, i, dev) {
987 irq_data = irq_domain_get_irq_data(domain, i);
988 if (irqd_is_activated(irq_data))
989 irq_domain_deactivate_irq(irq_data);
992 for_each_msi_entry(desc, dev) {
994 * We might have failed to allocate an MSI early
995 * enough that there is no IRQ associated to this
996 * entry. If that's the case, don't do anything.
999 irq_domain_free_irqs(desc->irq, desc->nvec_used);
1005 static void msi_domain_free_msi_descs(struct msi_domain_info *info,
1008 if (info->flags & MSI_FLAG_FREE_MSI_DESCS)
1009 msi_free_msi_descs(dev);
1013 * msi_domain_free_irqs_descs_locked - Free interrupts from a MSI interrupt @domain associated to @dev
1014 * @domain: The domain to managing the interrupts
1015 * @dev: Pointer to device struct of the device for which the interrupts
1018 * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
1019 * pair. Use this for MSI irqdomains which implement their own vector
1022 void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev)
1024 struct msi_domain_info *info = domain->host_data;
1025 struct msi_domain_ops *ops = info->ops;
1027 lockdep_assert_held(&dev->msi.data->mutex);
1029 if (info->flags & MSI_FLAG_DEV_SYSFS)
1030 msi_device_destroy_sysfs(dev);
1031 ops->domain_free_irqs(domain, dev);
1032 msi_domain_free_msi_descs(info, dev);
1036 * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev
1037 * @domain: The domain to managing the interrupts
1038 * @dev: Pointer to device struct of the device for which the interrupts
1041 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
1043 msi_lock_descs(dev);
1044 msi_domain_free_irqs_descs_locked(domain, dev);
1045 msi_unlock_descs(dev);
1049 * msi_get_domain_info - Get the MSI interrupt domain info for @domain
1050 * @domain: The interrupt domain to retrieve data from
1052 * Return: the pointer to the msi_domain_info stored in @domain->host_data.
1054 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
1056 return (struct msi_domain_info *)domain->host_data;
1059 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */