xref: /openbmc/linux/kernel/irq/msi.c (revision d9109698)
1f3cf8bb0SJiang Liu /*
2f3cf8bb0SJiang Liu  * linux/kernel/irq/msi.c
3f3cf8bb0SJiang Liu  *
4f3cf8bb0SJiang Liu  * Copyright (C) 2014 Intel Corp.
5f3cf8bb0SJiang Liu  * Author: Jiang Liu <jiang.liu@linux.intel.com>
6f3cf8bb0SJiang Liu  *
7f3cf8bb0SJiang Liu  * This file is licensed under GPLv2.
8f3cf8bb0SJiang Liu  *
9f3cf8bb0SJiang Liu  * This file contains common code to support Message Signalled Interrupt for
10f3cf8bb0SJiang Liu  * PCI compatible and non PCI compatible devices.
11f3cf8bb0SJiang Liu  */
12f3cf8bb0SJiang Liu #include <linux/irq.h>
13f3cf8bb0SJiang Liu #include <linux/irqdomain.h>
14f3cf8bb0SJiang Liu #include <linux/msi.h>
15f3cf8bb0SJiang Liu 
16d9109698SJiang Liu /* Temparory solution for building, will be removed later */
17d9109698SJiang Liu #include <linux/pci.h>
18d9109698SJiang Liu 
19f3cf8bb0SJiang Liu #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
20f3cf8bb0SJiang Liu /**
21f3cf8bb0SJiang Liu  * msi_domain_set_affinity - Generic affinity setter function for MSI domains
22f3cf8bb0SJiang Liu  * @irq_data:	The irq data associated to the interrupt
23f3cf8bb0SJiang Liu  * @mask:	The affinity mask to set
24f3cf8bb0SJiang Liu  * @force:	Flag to enforce setting (disable online checks)
25f3cf8bb0SJiang Liu  *
26f3cf8bb0SJiang Liu  * Intended to be used by MSI interrupt controllers which are
27f3cf8bb0SJiang Liu  * implemented with hierarchical domains.
28f3cf8bb0SJiang Liu  */
29f3cf8bb0SJiang Liu int msi_domain_set_affinity(struct irq_data *irq_data,
30f3cf8bb0SJiang Liu 			    const struct cpumask *mask, bool force)
31f3cf8bb0SJiang Liu {
32f3cf8bb0SJiang Liu 	struct irq_data *parent = irq_data->parent_data;
33f3cf8bb0SJiang Liu 	struct msi_msg msg;
34f3cf8bb0SJiang Liu 	int ret;
35f3cf8bb0SJiang Liu 
36f3cf8bb0SJiang Liu 	ret = parent->chip->irq_set_affinity(parent, mask, force);
37f3cf8bb0SJiang Liu 	if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
38f3cf8bb0SJiang Liu 		BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg));
39f3cf8bb0SJiang Liu 		irq_chip_write_msi_msg(irq_data, &msg);
40f3cf8bb0SJiang Liu 	}
41f3cf8bb0SJiang Liu 
42f3cf8bb0SJiang Liu 	return ret;
43f3cf8bb0SJiang Liu }
44f3cf8bb0SJiang Liu 
45f3cf8bb0SJiang Liu static void msi_domain_activate(struct irq_domain *domain,
46f3cf8bb0SJiang Liu 				struct irq_data *irq_data)
47f3cf8bb0SJiang Liu {
48f3cf8bb0SJiang Liu 	struct msi_msg msg;
49f3cf8bb0SJiang Liu 
50f3cf8bb0SJiang Liu 	BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg));
51f3cf8bb0SJiang Liu 	irq_chip_write_msi_msg(irq_data, &msg);
52f3cf8bb0SJiang Liu }
53f3cf8bb0SJiang Liu 
54f3cf8bb0SJiang Liu static void msi_domain_deactivate(struct irq_domain *domain,
55f3cf8bb0SJiang Liu 				  struct irq_data *irq_data)
56f3cf8bb0SJiang Liu {
57f3cf8bb0SJiang Liu 	struct msi_msg msg;
58f3cf8bb0SJiang Liu 
59f3cf8bb0SJiang Liu 	memset(&msg, 0, sizeof(msg));
60f3cf8bb0SJiang Liu 	irq_chip_write_msi_msg(irq_data, &msg);
61f3cf8bb0SJiang Liu }
62f3cf8bb0SJiang Liu 
63f3cf8bb0SJiang Liu static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
64f3cf8bb0SJiang Liu 			    unsigned int nr_irqs, void *arg)
65f3cf8bb0SJiang Liu {
66f3cf8bb0SJiang Liu 	struct msi_domain_info *info = domain->host_data;
67f3cf8bb0SJiang Liu 	struct msi_domain_ops *ops = info->ops;
68f3cf8bb0SJiang Liu 	irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
69f3cf8bb0SJiang Liu 	int i, ret;
70f3cf8bb0SJiang Liu 
71f3cf8bb0SJiang Liu 	if (irq_find_mapping(domain, hwirq) > 0)
72f3cf8bb0SJiang Liu 		return -EEXIST;
73f3cf8bb0SJiang Liu 
74f3cf8bb0SJiang Liu 	ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
75f3cf8bb0SJiang Liu 	if (ret < 0)
76f3cf8bb0SJiang Liu 		return ret;
77f3cf8bb0SJiang Liu 
78f3cf8bb0SJiang Liu 	for (i = 0; i < nr_irqs; i++) {
79f3cf8bb0SJiang Liu 		ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
80f3cf8bb0SJiang Liu 		if (ret < 0) {
81f3cf8bb0SJiang Liu 			if (ops->msi_free) {
82f3cf8bb0SJiang Liu 				for (i--; i > 0; i--)
83f3cf8bb0SJiang Liu 					ops->msi_free(domain, info, virq + i);
84f3cf8bb0SJiang Liu 			}
85f3cf8bb0SJiang Liu 			irq_domain_free_irqs_top(domain, virq, nr_irqs);
86f3cf8bb0SJiang Liu 			return ret;
87f3cf8bb0SJiang Liu 		}
88f3cf8bb0SJiang Liu 	}
89f3cf8bb0SJiang Liu 
90f3cf8bb0SJiang Liu 	return 0;
91f3cf8bb0SJiang Liu }
92f3cf8bb0SJiang Liu 
93f3cf8bb0SJiang Liu static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
94f3cf8bb0SJiang Liu 			    unsigned int nr_irqs)
95f3cf8bb0SJiang Liu {
96f3cf8bb0SJiang Liu 	struct msi_domain_info *info = domain->host_data;
97f3cf8bb0SJiang Liu 	int i;
98f3cf8bb0SJiang Liu 
99f3cf8bb0SJiang Liu 	if (info->ops->msi_free) {
100f3cf8bb0SJiang Liu 		for (i = 0; i < nr_irqs; i++)
101f3cf8bb0SJiang Liu 			info->ops->msi_free(domain, info, virq + i);
102f3cf8bb0SJiang Liu 	}
103f3cf8bb0SJiang Liu 	irq_domain_free_irqs_top(domain, virq, nr_irqs);
104f3cf8bb0SJiang Liu }
105f3cf8bb0SJiang Liu 
106f3cf8bb0SJiang Liu static struct irq_domain_ops msi_domain_ops = {
107f3cf8bb0SJiang Liu 	.alloc		= msi_domain_alloc,
108f3cf8bb0SJiang Liu 	.free		= msi_domain_free,
109f3cf8bb0SJiang Liu 	.activate	= msi_domain_activate,
110f3cf8bb0SJiang Liu 	.deactivate	= msi_domain_deactivate,
111f3cf8bb0SJiang Liu };
112f3cf8bb0SJiang Liu 
113f3cf8bb0SJiang Liu /**
114f3cf8bb0SJiang Liu  * msi_create_irq_domain - Create a MSI interrupt domain
115f3cf8bb0SJiang Liu  * @of_node:	Optional device-tree node of the interrupt controller
116f3cf8bb0SJiang Liu  * @info:	MSI domain info
117f3cf8bb0SJiang Liu  * @parent:	Parent irq domain
118f3cf8bb0SJiang Liu  */
119f3cf8bb0SJiang Liu struct irq_domain *msi_create_irq_domain(struct device_node *of_node,
120f3cf8bb0SJiang Liu 					 struct msi_domain_info *info,
121f3cf8bb0SJiang Liu 					 struct irq_domain *parent)
122f3cf8bb0SJiang Liu {
123f3cf8bb0SJiang Liu 	struct irq_domain *domain;
124f3cf8bb0SJiang Liu 
125f3cf8bb0SJiang Liu 	domain = irq_domain_add_tree(of_node, &msi_domain_ops, info);
126f3cf8bb0SJiang Liu 	if (domain)
127f3cf8bb0SJiang Liu 		domain->parent = parent;
128f3cf8bb0SJiang Liu 
129f3cf8bb0SJiang Liu 	return domain;
130f3cf8bb0SJiang Liu }
131f3cf8bb0SJiang Liu 
132f3cf8bb0SJiang Liu /**
133d9109698SJiang Liu  * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
134d9109698SJiang Liu  * @domain:	The domain to allocate from
135d9109698SJiang Liu  * @dev:	Pointer to device struct of the device for which the interrupts
136d9109698SJiang Liu  *		are allocated
137d9109698SJiang Liu  * @nvec:	The number of interrupts to allocate
138d9109698SJiang Liu  *
139d9109698SJiang Liu  * Returns 0 on success or an error code.
140d9109698SJiang Liu  */
141d9109698SJiang Liu int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
142d9109698SJiang Liu 			  int nvec)
143d9109698SJiang Liu {
144d9109698SJiang Liu 	struct msi_domain_info *info = domain->host_data;
145d9109698SJiang Liu 	struct msi_domain_ops *ops = info->ops;
146d9109698SJiang Liu 	msi_alloc_info_t arg;
147d9109698SJiang Liu 	struct msi_desc *desc;
148d9109698SJiang Liu 	int i, ret, virq = -1;
149d9109698SJiang Liu 
150d9109698SJiang Liu 	ret = ops->msi_check(domain, info, dev);
151d9109698SJiang Liu 	if (ret == 0)
152d9109698SJiang Liu 		ret = ops->msi_prepare(domain, dev, nvec, &arg);
153d9109698SJiang Liu 	if (ret)
154d9109698SJiang Liu 		return ret;
155d9109698SJiang Liu 
156d9109698SJiang Liu 	for_each_msi_entry(desc, dev) {
157d9109698SJiang Liu 		ops->set_desc(&arg, desc);
158d9109698SJiang Liu 
159d9109698SJiang Liu 		virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
160d9109698SJiang Liu 					       dev_to_node(dev), &arg, false);
161d9109698SJiang Liu 		if (virq < 0) {
162d9109698SJiang Liu 			ret = -ENOSPC;
163d9109698SJiang Liu 			if (ops->handle_error)
164d9109698SJiang Liu 				ret = ops->handle_error(domain, desc, ret);
165d9109698SJiang Liu 			if (ops->msi_finish)
166d9109698SJiang Liu 				ops->msi_finish(&arg, ret);
167d9109698SJiang Liu 			return ret;
168d9109698SJiang Liu 		}
169d9109698SJiang Liu 
170d9109698SJiang Liu 		for (i = 0; i < desc->nvec_used; i++)
171d9109698SJiang Liu 			irq_set_msi_desc_off(virq, i, desc);
172d9109698SJiang Liu 	}
173d9109698SJiang Liu 
174d9109698SJiang Liu 	if (ops->msi_finish)
175d9109698SJiang Liu 		ops->msi_finish(&arg, 0);
176d9109698SJiang Liu 
177d9109698SJiang Liu 	for_each_msi_entry(desc, dev) {
178d9109698SJiang Liu 		if (desc->nvec_used == 1)
179d9109698SJiang Liu 			dev_dbg(dev, "irq %d for MSI\n", virq);
180d9109698SJiang Liu 		else
181d9109698SJiang Liu 			dev_dbg(dev, "irq [%d-%d] for MSI\n",
182d9109698SJiang Liu 				virq, virq + desc->nvec_used - 1);
183d9109698SJiang Liu 	}
184d9109698SJiang Liu 
185d9109698SJiang Liu 	return 0;
186d9109698SJiang Liu }
187d9109698SJiang Liu 
188d9109698SJiang Liu /**
189d9109698SJiang Liu  * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated tp @dev
190d9109698SJiang Liu  * @domain:	The domain to managing the interrupts
191d9109698SJiang Liu  * @dev:	Pointer to device struct of the device for which the interrupts
192d9109698SJiang Liu  *		are free
193d9109698SJiang Liu  */
194d9109698SJiang Liu void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
195d9109698SJiang Liu {
196d9109698SJiang Liu 	struct msi_desc *desc;
197d9109698SJiang Liu 
198d9109698SJiang Liu 	for_each_msi_entry(desc, dev) {
199d9109698SJiang Liu 		irq_domain_free_irqs(desc->irq, desc->nvec_used);
200d9109698SJiang Liu 		desc->irq = 0;
201d9109698SJiang Liu 	}
202d9109698SJiang Liu }
203d9109698SJiang Liu 
204d9109698SJiang Liu /**
205f3cf8bb0SJiang Liu  * msi_get_domain_info - Get the MSI interrupt domain info for @domain
206f3cf8bb0SJiang Liu  * @domain:	The interrupt domain to retrieve data from
207f3cf8bb0SJiang Liu  *
208f3cf8bb0SJiang Liu  * Returns the pointer to the msi_domain_info stored in
209f3cf8bb0SJiang Liu  * @domain->host_data.
210f3cf8bb0SJiang Liu  */
211f3cf8bb0SJiang Liu struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
212f3cf8bb0SJiang Liu {
213f3cf8bb0SJiang Liu 	return (struct msi_domain_info *)domain->host_data;
214f3cf8bb0SJiang Liu }
215f3cf8bb0SJiang Liu 
216f3cf8bb0SJiang Liu #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */
217