xref: /openbmc/linux/kernel/irq/msi.c (revision bf6e054e)
152a65ff5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2f3cf8bb0SJiang Liu /*
3f3cf8bb0SJiang Liu  * Copyright (C) 2014 Intel Corp.
4f3cf8bb0SJiang Liu  * Author: Jiang Liu <jiang.liu@linux.intel.com>
5f3cf8bb0SJiang Liu  *
6f3cf8bb0SJiang Liu  * This file is licensed under GPLv2.
7f3cf8bb0SJiang Liu  *
8a359f757SIngo Molnar  * This file contains common code to support Message Signaled Interrupts for
9f3cf8bb0SJiang Liu  * PCI compatible and non PCI compatible devices.
10f3cf8bb0SJiang Liu  */
11aeeb5965SJiang Liu #include <linux/types.h>
12aeeb5965SJiang Liu #include <linux/device.h>
13f3cf8bb0SJiang Liu #include <linux/irq.h>
14f3cf8bb0SJiang Liu #include <linux/irqdomain.h>
15f3cf8bb0SJiang Liu #include <linux/msi.h>
164e201566SMarc Zyngier #include <linux/slab.h>
173ba1f050SThomas Gleixner #include <linux/sysfs.h>
182f170814SBarry Song #include <linux/pci.h>
19d9109698SJiang Liu 
2007557ccbSThomas Gleixner #include "internals.h"
2107557ccbSThomas Gleixner 
2228f4b041SThomas Gleixner /**
233b35e7e6SRandy Dunlap  * alloc_msi_entry - Allocate an initialized msi_desc
2428f4b041SThomas Gleixner  * @dev:	Pointer to the device for which this is allocated
2528f4b041SThomas Gleixner  * @nvec:	The number of vectors used in this entry
2628f4b041SThomas Gleixner  * @affinity:	Optional pointer to an affinity mask array size of @nvec
2728f4b041SThomas Gleixner  *
283b35e7e6SRandy Dunlap  * If @affinity is not %NULL then an affinity array[@nvec] is allocated
29bec04037SDou Liyang  * and the affinity masks and flags from @affinity are copied.
303b35e7e6SRandy Dunlap  *
313b35e7e6SRandy Dunlap  * Return: pointer to allocated &msi_desc on success or %NULL on failure
3228f4b041SThomas Gleixner  */
33bec04037SDou Liyang struct msi_desc *alloc_msi_entry(struct device *dev, int nvec,
34bec04037SDou Liyang 				 const struct irq_affinity_desc *affinity)
35aa48b6f7SJiang Liu {
3628f4b041SThomas Gleixner 	struct msi_desc *desc;
3728f4b041SThomas Gleixner 
3828f4b041SThomas Gleixner 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
39aa48b6f7SJiang Liu 	if (!desc)
40aa48b6f7SJiang Liu 		return NULL;
41aa48b6f7SJiang Liu 
42aa48b6f7SJiang Liu 	INIT_LIST_HEAD(&desc->list);
43aa48b6f7SJiang Liu 	desc->dev = dev;
4428f4b041SThomas Gleixner 	desc->nvec_used = nvec;
4528f4b041SThomas Gleixner 	if (affinity) {
4628f4b041SThomas Gleixner 		desc->affinity = kmemdup(affinity,
4728f4b041SThomas Gleixner 			nvec * sizeof(*desc->affinity), GFP_KERNEL);
4828f4b041SThomas Gleixner 		if (!desc->affinity) {
4928f4b041SThomas Gleixner 			kfree(desc);
5028f4b041SThomas Gleixner 			return NULL;
5128f4b041SThomas Gleixner 		}
5228f4b041SThomas Gleixner 	}
53aa48b6f7SJiang Liu 
54aa48b6f7SJiang Liu 	return desc;
55aa48b6f7SJiang Liu }
56aa48b6f7SJiang Liu 
57aa48b6f7SJiang Liu void free_msi_entry(struct msi_desc *entry)
58aa48b6f7SJiang Liu {
5928f4b041SThomas Gleixner 	kfree(entry->affinity);
60aa48b6f7SJiang Liu 	kfree(entry);
61aa48b6f7SJiang Liu }
62aa48b6f7SJiang Liu 
6338b6a1cfSJiang Liu void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
6438b6a1cfSJiang Liu {
6538b6a1cfSJiang Liu 	*msg = entry->msg;
6638b6a1cfSJiang Liu }
6738b6a1cfSJiang Liu 
6838b6a1cfSJiang Liu void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
6938b6a1cfSJiang Liu {
7038b6a1cfSJiang Liu 	struct msi_desc *entry = irq_get_msi_desc(irq);
7138b6a1cfSJiang Liu 
7238b6a1cfSJiang Liu 	__get_cached_msi_msg(entry, msg);
7338b6a1cfSJiang Liu }
7438b6a1cfSJiang Liu EXPORT_SYMBOL_GPL(get_cached_msi_msg);
7538b6a1cfSJiang Liu 
76013bd8e5SThomas Gleixner static void msi_device_data_release(struct device *dev, void *res)
77013bd8e5SThomas Gleixner {
78013bd8e5SThomas Gleixner 	WARN_ON_ONCE(!list_empty(&dev->msi_list));
79013bd8e5SThomas Gleixner 	dev->msi.data = NULL;
80013bd8e5SThomas Gleixner }
81013bd8e5SThomas Gleixner 
82013bd8e5SThomas Gleixner /**
83013bd8e5SThomas Gleixner  * msi_setup_device_data - Setup MSI device data
84013bd8e5SThomas Gleixner  * @dev:	Device for which MSI device data should be set up
85013bd8e5SThomas Gleixner  *
86013bd8e5SThomas Gleixner  * Return: 0 on success, appropriate error code otherwise
87013bd8e5SThomas Gleixner  *
88013bd8e5SThomas Gleixner  * This can be called more than once for @dev. If the MSI device data is
89013bd8e5SThomas Gleixner  * already allocated the call succeeds. The allocated memory is
90013bd8e5SThomas Gleixner  * automatically released when the device is destroyed.
91013bd8e5SThomas Gleixner  */
92013bd8e5SThomas Gleixner int msi_setup_device_data(struct device *dev)
93013bd8e5SThomas Gleixner {
94013bd8e5SThomas Gleixner 	struct msi_device_data *md;
95013bd8e5SThomas Gleixner 
96013bd8e5SThomas Gleixner 	if (dev->msi.data)
97013bd8e5SThomas Gleixner 		return 0;
98013bd8e5SThomas Gleixner 
99013bd8e5SThomas Gleixner 	md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
100013bd8e5SThomas Gleixner 	if (!md)
101013bd8e5SThomas Gleixner 		return -ENOMEM;
102013bd8e5SThomas Gleixner 
103013bd8e5SThomas Gleixner 	dev->msi.data = md;
104013bd8e5SThomas Gleixner 	devres_add(dev, md);
105013bd8e5SThomas Gleixner 	return 0;
106013bd8e5SThomas Gleixner }
107013bd8e5SThomas Gleixner 
1081197528aSThomas Gleixner #ifdef CONFIG_SYSFS
1092f170814SBarry Song static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
1102f170814SBarry Song 			     char *buf)
1112f170814SBarry Song {
1126ef7f771SThomas Gleixner 	/* MSI vs. MSIX is per device not per interrupt */
1136ef7f771SThomas Gleixner 	bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
1142f170814SBarry Song 
1152f170814SBarry Song 	return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
1162f170814SBarry Song }
1172f170814SBarry Song 
1182f170814SBarry Song /**
1192f170814SBarry Song  * msi_populate_sysfs - Populate msi_irqs sysfs entries for devices
1202f170814SBarry Song  * @dev:	The device(PCI, platform etc) who will get sysfs entries
1212f170814SBarry Song  *
1222f170814SBarry Song  * Return attribute_group ** so that specific bus MSI can save it to
1232f170814SBarry Song  * somewhere during initilizing msi irqs. If devices has no MSI irq,
1242f170814SBarry Song  * return NULL; if it fails to populate sysfs, return ERR_PTR
1252f170814SBarry Song  */
1262f170814SBarry Song const struct attribute_group **msi_populate_sysfs(struct device *dev)
1272f170814SBarry Song {
1282f170814SBarry Song 	const struct attribute_group **msi_irq_groups;
1292f170814SBarry Song 	struct attribute **msi_attrs, *msi_attr;
1302f170814SBarry Song 	struct device_attribute *msi_dev_attr;
1312f170814SBarry Song 	struct attribute_group *msi_irq_group;
1322f170814SBarry Song 	struct msi_desc *entry;
1332f170814SBarry Song 	int ret = -ENOMEM;
1342f170814SBarry Song 	int num_msi = 0;
1352f170814SBarry Song 	int count = 0;
1362f170814SBarry Song 	int i;
1372f170814SBarry Song 
1382f170814SBarry Song 	/* Determine how many msi entries we have */
1392f170814SBarry Song 	for_each_msi_entry(entry, dev)
1402f170814SBarry Song 		num_msi += entry->nvec_used;
1412f170814SBarry Song 	if (!num_msi)
1422f170814SBarry Song 		return NULL;
1432f170814SBarry Song 
1442f170814SBarry Song 	/* Dynamically create the MSI attributes for the device */
1452f170814SBarry Song 	msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL);
1462f170814SBarry Song 	if (!msi_attrs)
1472f170814SBarry Song 		return ERR_PTR(-ENOMEM);
1482f170814SBarry Song 
1492f170814SBarry Song 	for_each_msi_entry(entry, dev) {
1502f170814SBarry Song 		for (i = 0; i < entry->nvec_used; i++) {
1512f170814SBarry Song 			msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
1522f170814SBarry Song 			if (!msi_dev_attr)
1532f170814SBarry Song 				goto error_attrs;
1542f170814SBarry Song 			msi_attrs[count] = &msi_dev_attr->attr;
1552f170814SBarry Song 
1562f170814SBarry Song 			sysfs_attr_init(&msi_dev_attr->attr);
1572f170814SBarry Song 			msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
1582f170814SBarry Song 							    entry->irq + i);
1592f170814SBarry Song 			if (!msi_dev_attr->attr.name)
1602f170814SBarry Song 				goto error_attrs;
1612f170814SBarry Song 			msi_dev_attr->attr.mode = 0444;
1622f170814SBarry Song 			msi_dev_attr->show = msi_mode_show;
1632f170814SBarry Song 			++count;
1642f170814SBarry Song 		}
1652f170814SBarry Song 	}
1662f170814SBarry Song 
1672f170814SBarry Song 	msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
1682f170814SBarry Song 	if (!msi_irq_group)
1692f170814SBarry Song 		goto error_attrs;
1702f170814SBarry Song 	msi_irq_group->name = "msi_irqs";
1712f170814SBarry Song 	msi_irq_group->attrs = msi_attrs;
1722f170814SBarry Song 
1732f170814SBarry Song 	msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL);
1742f170814SBarry Song 	if (!msi_irq_groups)
1752f170814SBarry Song 		goto error_irq_group;
1762f170814SBarry Song 	msi_irq_groups[0] = msi_irq_group;
1772f170814SBarry Song 
1782f170814SBarry Song 	ret = sysfs_create_groups(&dev->kobj, msi_irq_groups);
1792f170814SBarry Song 	if (ret)
1802f170814SBarry Song 		goto error_irq_groups;
1812f170814SBarry Song 
1822f170814SBarry Song 	return msi_irq_groups;
1832f170814SBarry Song 
1842f170814SBarry Song error_irq_groups:
1852f170814SBarry Song 	kfree(msi_irq_groups);
1862f170814SBarry Song error_irq_group:
1872f170814SBarry Song 	kfree(msi_irq_group);
1882f170814SBarry Song error_attrs:
1892f170814SBarry Song 	count = 0;
1902f170814SBarry Song 	msi_attr = msi_attrs[count];
1912f170814SBarry Song 	while (msi_attr) {
1922f170814SBarry Song 		msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
1932f170814SBarry Song 		kfree(msi_attr->name);
1942f170814SBarry Song 		kfree(msi_dev_attr);
1952f170814SBarry Song 		++count;
1962f170814SBarry Song 		msi_attr = msi_attrs[count];
1972f170814SBarry Song 	}
1982f170814SBarry Song 	kfree(msi_attrs);
1992f170814SBarry Song 	return ERR_PTR(ret);
2002f170814SBarry Song }
2012f170814SBarry Song 
2022f170814SBarry Song /**
203*bf6e054eSThomas Gleixner  * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
204*bf6e054eSThomas Gleixner  * @dev:	The device (PCI, platform etc) which will get sysfs entries
205*bf6e054eSThomas Gleixner  */
206*bf6e054eSThomas Gleixner int msi_device_populate_sysfs(struct device *dev)
207*bf6e054eSThomas Gleixner {
208*bf6e054eSThomas Gleixner 	const struct attribute_group **group = msi_populate_sysfs(dev);
209*bf6e054eSThomas Gleixner 
210*bf6e054eSThomas Gleixner 	if (IS_ERR(group))
211*bf6e054eSThomas Gleixner 		return PTR_ERR(group);
212*bf6e054eSThomas Gleixner 	dev->msi.data->attrs = group;
213*bf6e054eSThomas Gleixner 	return 0;
214*bf6e054eSThomas Gleixner }
215*bf6e054eSThomas Gleixner 
216*bf6e054eSThomas Gleixner /**
2172f170814SBarry Song  * msi_destroy_sysfs - Destroy msi_irqs sysfs entries for devices
2182f170814SBarry Song  * @dev:		The device(PCI, platform etc) who will remove sysfs entries
2192f170814SBarry Song  * @msi_irq_groups:	attribute_group for device msi_irqs entries
2202f170814SBarry Song  */
2212f170814SBarry Song void msi_destroy_sysfs(struct device *dev, const struct attribute_group **msi_irq_groups)
2222f170814SBarry Song {
2232f170814SBarry Song 	struct device_attribute *dev_attr;
2242f170814SBarry Song 	struct attribute **msi_attrs;
2252f170814SBarry Song 	int count = 0;
2262f170814SBarry Song 
2272f170814SBarry Song 	if (msi_irq_groups) {
2282f170814SBarry Song 		sysfs_remove_groups(&dev->kobj, msi_irq_groups);
2292f170814SBarry Song 		msi_attrs = msi_irq_groups[0]->attrs;
2302f170814SBarry Song 		while (msi_attrs[count]) {
2312f170814SBarry Song 			dev_attr = container_of(msi_attrs[count],
2322f170814SBarry Song 					struct device_attribute, attr);
2332f170814SBarry Song 			kfree(dev_attr->attr.name);
2342f170814SBarry Song 			kfree(dev_attr);
2352f170814SBarry Song 			++count;
2362f170814SBarry Song 		}
2372f170814SBarry Song 		kfree(msi_attrs);
2382f170814SBarry Song 		kfree(msi_irq_groups[0]);
2392f170814SBarry Song 		kfree(msi_irq_groups);
2402f170814SBarry Song 	}
2412f170814SBarry Song }
242*bf6e054eSThomas Gleixner 
243*bf6e054eSThomas Gleixner /**
244*bf6e054eSThomas Gleixner  * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
245*bf6e054eSThomas Gleixner  * @dev:		The device (PCI, platform etc) for which to remove
246*bf6e054eSThomas Gleixner  *			sysfs entries
247*bf6e054eSThomas Gleixner  */
248*bf6e054eSThomas Gleixner void msi_device_destroy_sysfs(struct device *dev)
249*bf6e054eSThomas Gleixner {
250*bf6e054eSThomas Gleixner 	msi_destroy_sysfs(dev, dev->msi.data->attrs);
251*bf6e054eSThomas Gleixner 	dev->msi.data->attrs = NULL;
252*bf6e054eSThomas Gleixner }
2531197528aSThomas Gleixner #endif
2542f170814SBarry Song 
255f3cf8bb0SJiang Liu #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
25674faaf7aSThomas Gleixner static inline void irq_chip_write_msi_msg(struct irq_data *data,
25774faaf7aSThomas Gleixner 					  struct msi_msg *msg)
25874faaf7aSThomas Gleixner {
25974faaf7aSThomas Gleixner 	data->chip->irq_write_msi_msg(data, msg);
26074faaf7aSThomas Gleixner }
26174faaf7aSThomas Gleixner 
2620be8153cSMarc Zyngier static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
2630be8153cSMarc Zyngier {
2640be8153cSMarc Zyngier 	struct msi_domain_info *info = domain->host_data;
2650be8153cSMarc Zyngier 
2660be8153cSMarc Zyngier 	/*
2670be8153cSMarc Zyngier 	 * If the MSI provider has messed with the second message and
2680be8153cSMarc Zyngier 	 * not advertized that it is level-capable, signal the breakage.
2690be8153cSMarc Zyngier 	 */
2700be8153cSMarc Zyngier 	WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
2710be8153cSMarc Zyngier 		  (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
2720be8153cSMarc Zyngier 		(msg[1].address_lo || msg[1].address_hi || msg[1].data));
2730be8153cSMarc Zyngier }
2740be8153cSMarc Zyngier 
275f3cf8bb0SJiang Liu /**
276f3cf8bb0SJiang Liu  * msi_domain_set_affinity - Generic affinity setter function for MSI domains
277f3cf8bb0SJiang Liu  * @irq_data:	The irq data associated to the interrupt
278f3cf8bb0SJiang Liu  * @mask:	The affinity mask to set
279f3cf8bb0SJiang Liu  * @force:	Flag to enforce setting (disable online checks)
280f3cf8bb0SJiang Liu  *
281f3cf8bb0SJiang Liu  * Intended to be used by MSI interrupt controllers which are
282f3cf8bb0SJiang Liu  * implemented with hierarchical domains.
2833b35e7e6SRandy Dunlap  *
2843b35e7e6SRandy Dunlap  * Return: IRQ_SET_MASK_* result code
285f3cf8bb0SJiang Liu  */
286f3cf8bb0SJiang Liu int msi_domain_set_affinity(struct irq_data *irq_data,
287f3cf8bb0SJiang Liu 			    const struct cpumask *mask, bool force)
288f3cf8bb0SJiang Liu {
289f3cf8bb0SJiang Liu 	struct irq_data *parent = irq_data->parent_data;
2900be8153cSMarc Zyngier 	struct msi_msg msg[2] = { [1] = { }, };
291f3cf8bb0SJiang Liu 	int ret;
292f3cf8bb0SJiang Liu 
293f3cf8bb0SJiang Liu 	ret = parent->chip->irq_set_affinity(parent, mask, force);
294f3cf8bb0SJiang Liu 	if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
2950be8153cSMarc Zyngier 		BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
2960be8153cSMarc Zyngier 		msi_check_level(irq_data->domain, msg);
2970be8153cSMarc Zyngier 		irq_chip_write_msi_msg(irq_data, msg);
298f3cf8bb0SJiang Liu 	}
299f3cf8bb0SJiang Liu 
300f3cf8bb0SJiang Liu 	return ret;
301f3cf8bb0SJiang Liu }
302f3cf8bb0SJiang Liu 
30372491643SThomas Gleixner static int msi_domain_activate(struct irq_domain *domain,
30472491643SThomas Gleixner 			       struct irq_data *irq_data, bool early)
305f3cf8bb0SJiang Liu {
3060be8153cSMarc Zyngier 	struct msi_msg msg[2] = { [1] = { }, };
307f3cf8bb0SJiang Liu 
3080be8153cSMarc Zyngier 	BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
3090be8153cSMarc Zyngier 	msi_check_level(irq_data->domain, msg);
3100be8153cSMarc Zyngier 	irq_chip_write_msi_msg(irq_data, msg);
31172491643SThomas Gleixner 	return 0;
312f3cf8bb0SJiang Liu }
313f3cf8bb0SJiang Liu 
314f3cf8bb0SJiang Liu static void msi_domain_deactivate(struct irq_domain *domain,
315f3cf8bb0SJiang Liu 				  struct irq_data *irq_data)
316f3cf8bb0SJiang Liu {
3170be8153cSMarc Zyngier 	struct msi_msg msg[2];
318f3cf8bb0SJiang Liu 
3190be8153cSMarc Zyngier 	memset(msg, 0, sizeof(msg));
3200be8153cSMarc Zyngier 	irq_chip_write_msi_msg(irq_data, msg);
321f3cf8bb0SJiang Liu }
322f3cf8bb0SJiang Liu 
323f3cf8bb0SJiang Liu static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
324f3cf8bb0SJiang Liu 			    unsigned int nr_irqs, void *arg)
325f3cf8bb0SJiang Liu {
326f3cf8bb0SJiang Liu 	struct msi_domain_info *info = domain->host_data;
327f3cf8bb0SJiang Liu 	struct msi_domain_ops *ops = info->ops;
328f3cf8bb0SJiang Liu 	irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
329f3cf8bb0SJiang Liu 	int i, ret;
330f3cf8bb0SJiang Liu 
331f3cf8bb0SJiang Liu 	if (irq_find_mapping(domain, hwirq) > 0)
332f3cf8bb0SJiang Liu 		return -EEXIST;
333f3cf8bb0SJiang Liu 
334bf6f869fSLiu Jiang 	if (domain->parent) {
335f3cf8bb0SJiang Liu 		ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
336f3cf8bb0SJiang Liu 		if (ret < 0)
337f3cf8bb0SJiang Liu 			return ret;
338bf6f869fSLiu Jiang 	}
339f3cf8bb0SJiang Liu 
340f3cf8bb0SJiang Liu 	for (i = 0; i < nr_irqs; i++) {
341f3cf8bb0SJiang Liu 		ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
342f3cf8bb0SJiang Liu 		if (ret < 0) {
343f3cf8bb0SJiang Liu 			if (ops->msi_free) {
344f3cf8bb0SJiang Liu 				for (i--; i > 0; i--)
345f3cf8bb0SJiang Liu 					ops->msi_free(domain, info, virq + i);
346f3cf8bb0SJiang Liu 			}
347f3cf8bb0SJiang Liu 			irq_domain_free_irqs_top(domain, virq, nr_irqs);
348f3cf8bb0SJiang Liu 			return ret;
349f3cf8bb0SJiang Liu 		}
350f3cf8bb0SJiang Liu 	}
351f3cf8bb0SJiang Liu 
352f3cf8bb0SJiang Liu 	return 0;
353f3cf8bb0SJiang Liu }
354f3cf8bb0SJiang Liu 
355f3cf8bb0SJiang Liu static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
356f3cf8bb0SJiang Liu 			    unsigned int nr_irqs)
357f3cf8bb0SJiang Liu {
358f3cf8bb0SJiang Liu 	struct msi_domain_info *info = domain->host_data;
359f3cf8bb0SJiang Liu 	int i;
360f3cf8bb0SJiang Liu 
361f3cf8bb0SJiang Liu 	if (info->ops->msi_free) {
362f3cf8bb0SJiang Liu 		for (i = 0; i < nr_irqs; i++)
363f3cf8bb0SJiang Liu 			info->ops->msi_free(domain, info, virq + i);
364f3cf8bb0SJiang Liu 	}
365f3cf8bb0SJiang Liu 	irq_domain_free_irqs_top(domain, virq, nr_irqs);
366f3cf8bb0SJiang Liu }
367f3cf8bb0SJiang Liu 
36801364028SKrzysztof Kozlowski static const struct irq_domain_ops msi_domain_ops = {
369f3cf8bb0SJiang Liu 	.alloc		= msi_domain_alloc,
370f3cf8bb0SJiang Liu 	.free		= msi_domain_free,
371f3cf8bb0SJiang Liu 	.activate	= msi_domain_activate,
372f3cf8bb0SJiang Liu 	.deactivate	= msi_domain_deactivate,
373f3cf8bb0SJiang Liu };
374f3cf8bb0SJiang Liu 
375aeeb5965SJiang Liu static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
376aeeb5965SJiang Liu 						msi_alloc_info_t *arg)
377aeeb5965SJiang Liu {
378aeeb5965SJiang Liu 	return arg->hwirq;
379aeeb5965SJiang Liu }
380aeeb5965SJiang Liu 
381aeeb5965SJiang Liu static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
382aeeb5965SJiang Liu 				  int nvec, msi_alloc_info_t *arg)
383aeeb5965SJiang Liu {
384aeeb5965SJiang Liu 	memset(arg, 0, sizeof(*arg));
385aeeb5965SJiang Liu 	return 0;
386aeeb5965SJiang Liu }
387aeeb5965SJiang Liu 
388aeeb5965SJiang Liu static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
389aeeb5965SJiang Liu 				    struct msi_desc *desc)
390aeeb5965SJiang Liu {
391aeeb5965SJiang Liu 	arg->desc = desc;
392aeeb5965SJiang Liu }
393aeeb5965SJiang Liu 
394aeeb5965SJiang Liu static int msi_domain_ops_init(struct irq_domain *domain,
395aeeb5965SJiang Liu 			       struct msi_domain_info *info,
396aeeb5965SJiang Liu 			       unsigned int virq, irq_hw_number_t hwirq,
397aeeb5965SJiang Liu 			       msi_alloc_info_t *arg)
398aeeb5965SJiang Liu {
399aeeb5965SJiang Liu 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
400aeeb5965SJiang Liu 				      info->chip_data);
401aeeb5965SJiang Liu 	if (info->handler && info->handler_name) {
402aeeb5965SJiang Liu 		__irq_set_handler(virq, info->handler, 0, info->handler_name);
403aeeb5965SJiang Liu 		if (info->handler_data)
404aeeb5965SJiang Liu 			irq_set_handler_data(virq, info->handler_data);
405aeeb5965SJiang Liu 	}
406aeeb5965SJiang Liu 	return 0;
407aeeb5965SJiang Liu }
408aeeb5965SJiang Liu 
409aeeb5965SJiang Liu static int msi_domain_ops_check(struct irq_domain *domain,
410aeeb5965SJiang Liu 				struct msi_domain_info *info,
411aeeb5965SJiang Liu 				struct device *dev)
412aeeb5965SJiang Liu {
413aeeb5965SJiang Liu 	return 0;
414aeeb5965SJiang Liu }
415aeeb5965SJiang Liu 
416aeeb5965SJiang Liu static struct msi_domain_ops msi_domain_ops_default = {
417aeeb5965SJiang Liu 	.get_hwirq		= msi_domain_ops_get_hwirq,
418aeeb5965SJiang Liu 	.msi_init		= msi_domain_ops_init,
419aeeb5965SJiang Liu 	.msi_check		= msi_domain_ops_check,
420aeeb5965SJiang Liu 	.msi_prepare		= msi_domain_ops_prepare,
421aeeb5965SJiang Liu 	.set_desc		= msi_domain_ops_set_desc,
42243e9e705SThomas Gleixner 	.domain_alloc_irqs	= __msi_domain_alloc_irqs,
42343e9e705SThomas Gleixner 	.domain_free_irqs	= __msi_domain_free_irqs,
424aeeb5965SJiang Liu };
425aeeb5965SJiang Liu 
426aeeb5965SJiang Liu static void msi_domain_update_dom_ops(struct msi_domain_info *info)
427aeeb5965SJiang Liu {
428aeeb5965SJiang Liu 	struct msi_domain_ops *ops = info->ops;
429aeeb5965SJiang Liu 
430aeeb5965SJiang Liu 	if (ops == NULL) {
431aeeb5965SJiang Liu 		info->ops = &msi_domain_ops_default;
432aeeb5965SJiang Liu 		return;
433aeeb5965SJiang Liu 	}
434aeeb5965SJiang Liu 
43543e9e705SThomas Gleixner 	if (ops->domain_alloc_irqs == NULL)
43643e9e705SThomas Gleixner 		ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs;
43743e9e705SThomas Gleixner 	if (ops->domain_free_irqs == NULL)
43843e9e705SThomas Gleixner 		ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs;
43943e9e705SThomas Gleixner 
44043e9e705SThomas Gleixner 	if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
44143e9e705SThomas Gleixner 		return;
44243e9e705SThomas Gleixner 
443aeeb5965SJiang Liu 	if (ops->get_hwirq == NULL)
444aeeb5965SJiang Liu 		ops->get_hwirq = msi_domain_ops_default.get_hwirq;
445aeeb5965SJiang Liu 	if (ops->msi_init == NULL)
446aeeb5965SJiang Liu 		ops->msi_init = msi_domain_ops_default.msi_init;
447aeeb5965SJiang Liu 	if (ops->msi_check == NULL)
448aeeb5965SJiang Liu 		ops->msi_check = msi_domain_ops_default.msi_check;
449aeeb5965SJiang Liu 	if (ops->msi_prepare == NULL)
450aeeb5965SJiang Liu 		ops->msi_prepare = msi_domain_ops_default.msi_prepare;
451aeeb5965SJiang Liu 	if (ops->set_desc == NULL)
452aeeb5965SJiang Liu 		ops->set_desc = msi_domain_ops_default.set_desc;
453aeeb5965SJiang Liu }
454aeeb5965SJiang Liu 
455aeeb5965SJiang Liu static void msi_domain_update_chip_ops(struct msi_domain_info *info)
456aeeb5965SJiang Liu {
457aeeb5965SJiang Liu 	struct irq_chip *chip = info->chip;
458aeeb5965SJiang Liu 
4590701c53eSMarc Zyngier 	BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
460aeeb5965SJiang Liu 	if (!chip->irq_set_affinity)
461aeeb5965SJiang Liu 		chip->irq_set_affinity = msi_domain_set_affinity;
462aeeb5965SJiang Liu }
463aeeb5965SJiang Liu 
464f3cf8bb0SJiang Liu /**
4653b35e7e6SRandy Dunlap  * msi_create_irq_domain - Create an MSI interrupt domain
466be5436c8SMarc Zyngier  * @fwnode:	Optional fwnode of the interrupt controller
467f3cf8bb0SJiang Liu  * @info:	MSI domain info
468f3cf8bb0SJiang Liu  * @parent:	Parent irq domain
4693b35e7e6SRandy Dunlap  *
4703b35e7e6SRandy Dunlap  * Return: pointer to the created &struct irq_domain or %NULL on failure
471f3cf8bb0SJiang Liu  */
472be5436c8SMarc Zyngier struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
473f3cf8bb0SJiang Liu 					 struct msi_domain_info *info,
474f3cf8bb0SJiang Liu 					 struct irq_domain *parent)
475f3cf8bb0SJiang Liu {
476a97b852bSMarc Zyngier 	struct irq_domain *domain;
477a97b852bSMarc Zyngier 
478aeeb5965SJiang Liu 	msi_domain_update_dom_ops(info);
479aeeb5965SJiang Liu 	if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
480aeeb5965SJiang Liu 		msi_domain_update_chip_ops(info);
481f3cf8bb0SJiang Liu 
482a97b852bSMarc Zyngier 	domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
48388156f00SEric Auger 					     fwnode, &msi_domain_ops, info);
4840165308aSThomas Gleixner 
4850165308aSThomas Gleixner 	if (domain && !domain->name && info->chip)
486a97b852bSMarc Zyngier 		domain->name = info->chip->name;
487a97b852bSMarc Zyngier 
488a97b852bSMarc Zyngier 	return domain;
489f3cf8bb0SJiang Liu }
490f3cf8bb0SJiang Liu 
491b2eba39bSMarc Zyngier int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
492b2eba39bSMarc Zyngier 			    int nvec, msi_alloc_info_t *arg)
493b2eba39bSMarc Zyngier {
494b2eba39bSMarc Zyngier 	struct msi_domain_info *info = domain->host_data;
495b2eba39bSMarc Zyngier 	struct msi_domain_ops *ops = info->ops;
496b2eba39bSMarc Zyngier 	int ret;
497b2eba39bSMarc Zyngier 
498b2eba39bSMarc Zyngier 	ret = ops->msi_check(domain, info, dev);
499b2eba39bSMarc Zyngier 	if (ret == 0)
500b2eba39bSMarc Zyngier 		ret = ops->msi_prepare(domain, dev, nvec, arg);
501b2eba39bSMarc Zyngier 
502b2eba39bSMarc Zyngier 	return ret;
503b2eba39bSMarc Zyngier }
504b2eba39bSMarc Zyngier 
5052145ac93SMarc Zyngier int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
5062145ac93SMarc Zyngier 			     int virq, int nvec, msi_alloc_info_t *arg)
5072145ac93SMarc Zyngier {
5082145ac93SMarc Zyngier 	struct msi_domain_info *info = domain->host_data;
5092145ac93SMarc Zyngier 	struct msi_domain_ops *ops = info->ops;
5102145ac93SMarc Zyngier 	struct msi_desc *desc;
5112145ac93SMarc Zyngier 	int ret = 0;
5122145ac93SMarc Zyngier 
5132145ac93SMarc Zyngier 	for_each_msi_entry(desc, dev) {
5142145ac93SMarc Zyngier 		/* Don't even try the multi-MSI brain damage. */
5152145ac93SMarc Zyngier 		if (WARN_ON(!desc->irq || desc->nvec_used != 1)) {
5162145ac93SMarc Zyngier 			ret = -EINVAL;
5172145ac93SMarc Zyngier 			break;
5182145ac93SMarc Zyngier 		}
5192145ac93SMarc Zyngier 
5202145ac93SMarc Zyngier 		if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
5212145ac93SMarc Zyngier 			continue;
5222145ac93SMarc Zyngier 
5232145ac93SMarc Zyngier 		ops->set_desc(arg, desc);
5242145ac93SMarc Zyngier 		/* Assumes the domain mutex is held! */
525596a7a1dSJohn Keeping 		ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1,
526596a7a1dSJohn Keeping 						      arg);
5272145ac93SMarc Zyngier 		if (ret)
5282145ac93SMarc Zyngier 			break;
5292145ac93SMarc Zyngier 
530596a7a1dSJohn Keeping 		irq_set_msi_desc_off(desc->irq, 0, desc);
5312145ac93SMarc Zyngier 	}
5322145ac93SMarc Zyngier 
5332145ac93SMarc Zyngier 	if (ret) {
5342145ac93SMarc Zyngier 		/* Mop up the damage */
5352145ac93SMarc Zyngier 		for_each_msi_entry(desc, dev) {
5362145ac93SMarc Zyngier 			if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
5372145ac93SMarc Zyngier 				continue;
5382145ac93SMarc Zyngier 
5392145ac93SMarc Zyngier 			irq_domain_free_irqs_common(domain, desc->irq, 1);
5402145ac93SMarc Zyngier 		}
5412145ac93SMarc Zyngier 	}
5422145ac93SMarc Zyngier 
5432145ac93SMarc Zyngier 	return ret;
5442145ac93SMarc Zyngier }
5452145ac93SMarc Zyngier 
546bc976233SThomas Gleixner /*
547bc976233SThomas Gleixner  * Carefully check whether the device can use reservation mode. If
548bc976233SThomas Gleixner  * reservation mode is enabled then the early activation will assign a
549bc976233SThomas Gleixner  * dummy vector to the device. If the PCI/MSI device does not support
550bc976233SThomas Gleixner  * masking of the entry then this can result in spurious interrupts when
551bc976233SThomas Gleixner  * the device driver is not absolutely careful. But even then a malfunction
552bc976233SThomas Gleixner  * of the hardware could result in a spurious interrupt on the dummy vector
553bc976233SThomas Gleixner  * and render the device unusable. If the entry can be masked then the core
554bc976233SThomas Gleixner  * logic will prevent the spurious interrupt and reservation mode can be
555bc976233SThomas Gleixner  * used. For now reservation mode is restricted to PCI/MSI.
556bc976233SThomas Gleixner  */
557bc976233SThomas Gleixner static bool msi_check_reservation_mode(struct irq_domain *domain,
558bc976233SThomas Gleixner 				       struct msi_domain_info *info,
559bc976233SThomas Gleixner 				       struct device *dev)
560da5dd9e8SThomas Gleixner {
561bc976233SThomas Gleixner 	struct msi_desc *desc;
562bc976233SThomas Gleixner 
563c6c9e283SThomas Gleixner 	switch(domain->bus_token) {
564c6c9e283SThomas Gleixner 	case DOMAIN_BUS_PCI_MSI:
565c6c9e283SThomas Gleixner 	case DOMAIN_BUS_VMD_MSI:
566c6c9e283SThomas Gleixner 		break;
567c6c9e283SThomas Gleixner 	default:
568bc976233SThomas Gleixner 		return false;
569c6c9e283SThomas Gleixner 	}
570bc976233SThomas Gleixner 
571da5dd9e8SThomas Gleixner 	if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
572da5dd9e8SThomas Gleixner 		return false;
573bc976233SThomas Gleixner 
574bc976233SThomas Gleixner 	if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
575bc976233SThomas Gleixner 		return false;
576bc976233SThomas Gleixner 
577bc976233SThomas Gleixner 	/*
578bc976233SThomas Gleixner 	 * Checking the first MSI descriptor is sufficient. MSIX supports
5799c8e9c96SThomas Gleixner 	 * masking and MSI does so when the can_mask attribute is set.
580bc976233SThomas Gleixner 	 */
581bc976233SThomas Gleixner 	desc = first_msi_entry(dev);
582e58f2259SThomas Gleixner 	return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
583da5dd9e8SThomas Gleixner }
584da5dd9e8SThomas Gleixner 
58589033762SThomas Gleixner static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
58689033762SThomas Gleixner 			       int allocated)
58789033762SThomas Gleixner {
58889033762SThomas Gleixner 	switch(domain->bus_token) {
58989033762SThomas Gleixner 	case DOMAIN_BUS_PCI_MSI:
59089033762SThomas Gleixner 	case DOMAIN_BUS_VMD_MSI:
59189033762SThomas Gleixner 		if (IS_ENABLED(CONFIG_PCI_MSI))
59289033762SThomas Gleixner 			break;
59389033762SThomas Gleixner 		fallthrough;
59489033762SThomas Gleixner 	default:
59589033762SThomas Gleixner 		return -ENOSPC;
59689033762SThomas Gleixner 	}
59789033762SThomas Gleixner 
59889033762SThomas Gleixner 	/* Let a failed PCI multi MSI allocation retry */
59989033762SThomas Gleixner 	if (desc->nvec_used > 1)
60089033762SThomas Gleixner 		return 1;
60189033762SThomas Gleixner 
60289033762SThomas Gleixner 	/* If there was a successful allocation let the caller know */
60389033762SThomas Gleixner 	return allocated ? allocated : -ENOSPC;
60489033762SThomas Gleixner }
60589033762SThomas Gleixner 
60643e9e705SThomas Gleixner int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
607d9109698SJiang Liu 			    int nvec)
608d9109698SJiang Liu {
609d9109698SJiang Liu 	struct msi_domain_info *info = domain->host_data;
610d9109698SJiang Liu 	struct msi_domain_ops *ops = info->ops;
611da5dd9e8SThomas Gleixner 	struct irq_data *irq_data;
612d9109698SJiang Liu 	struct msi_desc *desc;
61306fde695SZenghui Yu 	msi_alloc_info_t arg = { };
61489033762SThomas Gleixner 	int allocated = 0;
615b6140914SThomas Gleixner 	int i, ret, virq;
616da5dd9e8SThomas Gleixner 	bool can_reserve;
617d9109698SJiang Liu 
618b2eba39bSMarc Zyngier 	ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
619d9109698SJiang Liu 	if (ret)
620d9109698SJiang Liu 		return ret;
621d9109698SJiang Liu 
622d9109698SJiang Liu 	for_each_msi_entry(desc, dev) {
623d9109698SJiang Liu 		ops->set_desc(&arg, desc);
624d9109698SJiang Liu 
625b6140914SThomas Gleixner 		virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
62606ee6d57SThomas Gleixner 					       dev_to_node(dev), &arg, false,
6270972fa57SThomas Gleixner 					       desc->affinity);
628d9109698SJiang Liu 		if (virq < 0) {
62989033762SThomas Gleixner 			ret = msi_handle_pci_fail(domain, desc, allocated);
63089033762SThomas Gleixner 			goto cleanup;
631d9109698SJiang Liu 		}
632d9109698SJiang Liu 
63307557ccbSThomas Gleixner 		for (i = 0; i < desc->nvec_used; i++) {
634d9109698SJiang Liu 			irq_set_msi_desc_off(virq, i, desc);
63507557ccbSThomas Gleixner 			irq_debugfs_copy_devname(virq + i, dev);
63607557ccbSThomas Gleixner 		}
63789033762SThomas Gleixner 		allocated++;
638d9109698SJiang Liu 	}
639d9109698SJiang Liu 
640bc976233SThomas Gleixner 	can_reserve = msi_check_reservation_mode(domain, info, dev);
641da5dd9e8SThomas Gleixner 
642f3b0946dSMarc Zyngier 	/*
643f3b0946dSMarc Zyngier 	 * This flag is set by the PCI layer as we need to activate
644f3b0946dSMarc Zyngier 	 * the MSI entries before the PCI layer enables MSI in the
645f3b0946dSMarc Zyngier 	 * card. Otherwise the card latches a random msi message.
646f3b0946dSMarc Zyngier 	 */
647da5dd9e8SThomas Gleixner 	if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY))
6484c457e8cSMarc Zyngier 		goto skip_activate;
649f3b0946dSMarc Zyngier 
6504c457e8cSMarc Zyngier 	for_each_msi_vector(desc, i, dev) {
6514c457e8cSMarc Zyngier 		if (desc->irq == i) {
6524c457e8cSMarc Zyngier 			virq = desc->irq;
6534c457e8cSMarc Zyngier 			dev_dbg(dev, "irq [%d-%d] for MSI\n",
6544c457e8cSMarc Zyngier 				virq, virq + desc->nvec_used - 1);
6554c457e8cSMarc Zyngier 		}
6564c457e8cSMarc Zyngier 
6574c457e8cSMarc Zyngier 		irq_data = irq_domain_get_irq_data(domain, i);
6586f1a4891SThomas Gleixner 		if (!can_reserve) {
659bc976233SThomas Gleixner 			irqd_clr_can_reserve(irq_data);
6606f1a4891SThomas Gleixner 			if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK)
6616f1a4891SThomas Gleixner 				irqd_set_msi_nomask_quirk(irq_data);
6626f1a4891SThomas Gleixner 		}
663bc976233SThomas Gleixner 		ret = irq_domain_activate_irq(irq_data, can_reserve);
664bb9b428aSThomas Gleixner 		if (ret)
665bb9b428aSThomas Gleixner 			goto cleanup;
666da5dd9e8SThomas Gleixner 	}
667da5dd9e8SThomas Gleixner 
6684c457e8cSMarc Zyngier skip_activate:
669da5dd9e8SThomas Gleixner 	/*
670da5dd9e8SThomas Gleixner 	 * If these interrupts use reservation mode, clear the activated bit
671da5dd9e8SThomas Gleixner 	 * so request_irq() will assign the final vector.
672da5dd9e8SThomas Gleixner 	 */
673da5dd9e8SThomas Gleixner 	if (can_reserve) {
6744c457e8cSMarc Zyngier 		for_each_msi_vector(desc, i, dev) {
6754c457e8cSMarc Zyngier 			irq_data = irq_domain_get_irq_data(domain, i);
67622d0b12fSThomas Gleixner 			irqd_clr_activated(irq_data);
677f3b0946dSMarc Zyngier 		}
678d9109698SJiang Liu 	}
679d9109698SJiang Liu 	return 0;
680bb9b428aSThomas Gleixner 
681bb9b428aSThomas Gleixner cleanup:
682bb9b428aSThomas Gleixner 	msi_domain_free_irqs(domain, dev);
683bb9b428aSThomas Gleixner 	return ret;
684d9109698SJiang Liu }
685d9109698SJiang Liu 
686d9109698SJiang Liu /**
68743e9e705SThomas Gleixner  * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
68843e9e705SThomas Gleixner  * @domain:	The domain to allocate from
689d9109698SJiang Liu  * @dev:	Pointer to device struct of the device for which the interrupts
69043e9e705SThomas Gleixner  *		are allocated
69143e9e705SThomas Gleixner  * @nvec:	The number of interrupts to allocate
69243e9e705SThomas Gleixner  *
6933b35e7e6SRandy Dunlap  * Return: %0 on success or an error code.
694d9109698SJiang Liu  */
69543e9e705SThomas Gleixner int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
69643e9e705SThomas Gleixner 			  int nvec)
69743e9e705SThomas Gleixner {
69843e9e705SThomas Gleixner 	struct msi_domain_info *info = domain->host_data;
69943e9e705SThomas Gleixner 	struct msi_domain_ops *ops = info->ops;
700*bf6e054eSThomas Gleixner 	int ret;
70143e9e705SThomas Gleixner 
702*bf6e054eSThomas Gleixner 	ret = ops->domain_alloc_irqs(domain, dev, nvec);
703*bf6e054eSThomas Gleixner 	if (ret)
704*bf6e054eSThomas Gleixner 		return ret;
705*bf6e054eSThomas Gleixner 
706*bf6e054eSThomas Gleixner 	if (!(info->flags & MSI_FLAG_DEV_SYSFS))
707*bf6e054eSThomas Gleixner 		return 0;
708*bf6e054eSThomas Gleixner 
709*bf6e054eSThomas Gleixner 	ret = msi_device_populate_sysfs(dev);
710*bf6e054eSThomas Gleixner 	if (ret)
711*bf6e054eSThomas Gleixner 		msi_domain_free_irqs(domain, dev);
712*bf6e054eSThomas Gleixner 	return ret;
71343e9e705SThomas Gleixner }
71443e9e705SThomas Gleixner 
71543e9e705SThomas Gleixner void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
716d9109698SJiang Liu {
717dbbc9357SBixuan Cui 	struct irq_data *irq_data;
718d9109698SJiang Liu 	struct msi_desc *desc;
719dbbc9357SBixuan Cui 	int i;
720dbbc9357SBixuan Cui 
721dbbc9357SBixuan Cui 	for_each_msi_vector(desc, i, dev) {
722dbbc9357SBixuan Cui 		irq_data = irq_domain_get_irq_data(domain, i);
723dbbc9357SBixuan Cui 		if (irqd_is_activated(irq_data))
724dbbc9357SBixuan Cui 			irq_domain_deactivate_irq(irq_data);
725dbbc9357SBixuan Cui 	}
726d9109698SJiang Liu 
727d9109698SJiang Liu 	for_each_msi_entry(desc, dev) {
728fe0c52fcSMarc Zyngier 		/*
729fe0c52fcSMarc Zyngier 		 * We might have failed to allocate an MSI early
730fe0c52fcSMarc Zyngier 		 * enough that there is no IRQ associated to this
731fe0c52fcSMarc Zyngier 		 * entry. If that's the case, don't do anything.
732fe0c52fcSMarc Zyngier 		 */
733fe0c52fcSMarc Zyngier 		if (desc->irq) {
734d9109698SJiang Liu 			irq_domain_free_irqs(desc->irq, desc->nvec_used);
735d9109698SJiang Liu 			desc->irq = 0;
736d9109698SJiang Liu 		}
737d9109698SJiang Liu 	}
738fe0c52fcSMarc Zyngier }
739d9109698SJiang Liu 
740d9109698SJiang Liu /**
7413b35e7e6SRandy Dunlap  * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev
74243e9e705SThomas Gleixner  * @domain:	The domain to managing the interrupts
74343e9e705SThomas Gleixner  * @dev:	Pointer to device struct of the device for which the interrupts
74443e9e705SThomas Gleixner  *		are free
74543e9e705SThomas Gleixner  */
74643e9e705SThomas Gleixner void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
74743e9e705SThomas Gleixner {
74843e9e705SThomas Gleixner 	struct msi_domain_info *info = domain->host_data;
74943e9e705SThomas Gleixner 	struct msi_domain_ops *ops = info->ops;
75043e9e705SThomas Gleixner 
751*bf6e054eSThomas Gleixner 	if (info->flags & MSI_FLAG_DEV_SYSFS)
752*bf6e054eSThomas Gleixner 		msi_device_destroy_sysfs(dev);
753*bf6e054eSThomas Gleixner 	ops->domain_free_irqs(domain, dev);
75443e9e705SThomas Gleixner }
75543e9e705SThomas Gleixner 
75643e9e705SThomas Gleixner /**
757f3cf8bb0SJiang Liu  * msi_get_domain_info - Get the MSI interrupt domain info for @domain
758f3cf8bb0SJiang Liu  * @domain:	The interrupt domain to retrieve data from
759f3cf8bb0SJiang Liu  *
7603b35e7e6SRandy Dunlap  * Return: the pointer to the msi_domain_info stored in @domain->host_data.
761f3cf8bb0SJiang Liu  */
762f3cf8bb0SJiang Liu struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
763f3cf8bb0SJiang Liu {
764f3cf8bb0SJiang Liu 	return (struct msi_domain_info *)domain->host_data;
765f3cf8bb0SJiang Liu }
766f3cf8bb0SJiang Liu 
767f3cf8bb0SJiang Liu #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
768