xref: /openbmc/linux/drivers/irqchip/irq-alpine-msi.c (revision b181f7029bd71238ac2754ce7052dffd69432085)
1e6b78f2cSAntoine Tenart /*
2e6b78f2cSAntoine Tenart  * Annapurna Labs MSIX support services
3e6b78f2cSAntoine Tenart  *
4e6b78f2cSAntoine Tenart  * Copyright (C) 2016, Amazon.com, Inc. or its affiliates. All Rights Reserved.
5e6b78f2cSAntoine Tenart  *
6e6b78f2cSAntoine Tenart  * Antoine Tenart <antoine.tenart@free-electrons.com>
7e6b78f2cSAntoine Tenart  *
8e6b78f2cSAntoine Tenart  * This file is licensed under the terms of the GNU General Public
9e6b78f2cSAntoine Tenart  * License version 2. This program is licensed "as is" without any
10e6b78f2cSAntoine Tenart  * warranty of any kind, whether express or implied.
11e6b78f2cSAntoine Tenart  */
12e6b78f2cSAntoine Tenart 
13e6b78f2cSAntoine Tenart #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14e6b78f2cSAntoine Tenart 
15e6b78f2cSAntoine Tenart #include <linux/irqchip.h>
16e6b78f2cSAntoine Tenart #include <linux/irqchip/arm-gic.h>
17e6b78f2cSAntoine Tenart #include <linux/msi.h>
18e6b78f2cSAntoine Tenart #include <linux/of.h>
19e6b78f2cSAntoine Tenart #include <linux/of_address.h>
20e6b78f2cSAntoine Tenart #include <linux/of_irq.h>
21e6b78f2cSAntoine Tenart #include <linux/of_pci.h>
22e6b78f2cSAntoine Tenart #include <linux/pci.h>
23e6b78f2cSAntoine Tenart #include <linux/slab.h>
24e6b78f2cSAntoine Tenart 
25e6b78f2cSAntoine Tenart #include <asm/irq.h>
269d9b7eedSChristoph Hellwig #include <asm/msi.h>
27e6b78f2cSAntoine Tenart 
28e6b78f2cSAntoine Tenart /* MSIX message address format: local GIC target */
29e6b78f2cSAntoine Tenart #define ALPINE_MSIX_SPI_TARGET_CLUSTER0		BIT(16)
30e6b78f2cSAntoine Tenart 
31e6b78f2cSAntoine Tenart struct alpine_msix_data {
32e6b78f2cSAntoine Tenart 	spinlock_t msi_map_lock;
33e6b78f2cSAntoine Tenart 	phys_addr_t addr;
34e6b78f2cSAntoine Tenart 	u32 spi_first;		/* The SGI number that MSIs start */
35e6b78f2cSAntoine Tenart 	u32 num_spis;		/* The number of SGIs for MSIs */
36e6b78f2cSAntoine Tenart 	unsigned long *msi_map;
37e6b78f2cSAntoine Tenart };
38e6b78f2cSAntoine Tenart 
alpine_msix_mask_msi_irq(struct irq_data * d)39e6b78f2cSAntoine Tenart static void alpine_msix_mask_msi_irq(struct irq_data *d)
40e6b78f2cSAntoine Tenart {
41e6b78f2cSAntoine Tenart 	pci_msi_mask_irq(d);
42e6b78f2cSAntoine Tenart 	irq_chip_mask_parent(d);
43e6b78f2cSAntoine Tenart }
44e6b78f2cSAntoine Tenart 
alpine_msix_unmask_msi_irq(struct irq_data * d)45e6b78f2cSAntoine Tenart static void alpine_msix_unmask_msi_irq(struct irq_data *d)
46e6b78f2cSAntoine Tenart {
47e6b78f2cSAntoine Tenart 	pci_msi_unmask_irq(d);
48e6b78f2cSAntoine Tenart 	irq_chip_unmask_parent(d);
49e6b78f2cSAntoine Tenart }
50e6b78f2cSAntoine Tenart 
51e6b78f2cSAntoine Tenart static struct irq_chip alpine_msix_irq_chip = {
52e6b78f2cSAntoine Tenart 	.name			= "MSIx",
53e6b78f2cSAntoine Tenart 	.irq_mask		= alpine_msix_mask_msi_irq,
54e6b78f2cSAntoine Tenart 	.irq_unmask		= alpine_msix_unmask_msi_irq,
55e6b78f2cSAntoine Tenart 	.irq_eoi		= irq_chip_eoi_parent,
56e6b78f2cSAntoine Tenart 	.irq_set_affinity	= irq_chip_set_affinity_parent,
57e6b78f2cSAntoine Tenart };
58e6b78f2cSAntoine Tenart 
alpine_msix_allocate_sgi(struct alpine_msix_data * priv,int num_req)59e6b78f2cSAntoine Tenart static int alpine_msix_allocate_sgi(struct alpine_msix_data *priv, int num_req)
60e6b78f2cSAntoine Tenart {
61e6b78f2cSAntoine Tenart 	int first;
62e6b78f2cSAntoine Tenart 
63e6b78f2cSAntoine Tenart 	spin_lock(&priv->msi_map_lock);
64e6b78f2cSAntoine Tenart 
65e6b78f2cSAntoine Tenart 	first = bitmap_find_next_zero_area(priv->msi_map, priv->num_spis, 0,
66e6b78f2cSAntoine Tenart 					   num_req, 0);
67e6b78f2cSAntoine Tenart 	if (first >= priv->num_spis) {
68e6b78f2cSAntoine Tenart 		spin_unlock(&priv->msi_map_lock);
69e6b78f2cSAntoine Tenart 		return -ENOSPC;
70e6b78f2cSAntoine Tenart 	}
71e6b78f2cSAntoine Tenart 
72e6b78f2cSAntoine Tenart 	bitmap_set(priv->msi_map, first, num_req);
73e6b78f2cSAntoine Tenart 
74e6b78f2cSAntoine Tenart 	spin_unlock(&priv->msi_map_lock);
75e6b78f2cSAntoine Tenart 
76e6b78f2cSAntoine Tenart 	return priv->spi_first + first;
77e6b78f2cSAntoine Tenart }
78e6b78f2cSAntoine Tenart 
alpine_msix_free_sgi(struct alpine_msix_data * priv,unsigned sgi,int num_req)79e6b78f2cSAntoine Tenart static void alpine_msix_free_sgi(struct alpine_msix_data *priv, unsigned sgi,
80e6b78f2cSAntoine Tenart 				 int num_req)
81e6b78f2cSAntoine Tenart {
82e6b78f2cSAntoine Tenart 	int first = sgi - priv->spi_first;
83e6b78f2cSAntoine Tenart 
84e6b78f2cSAntoine Tenart 	spin_lock(&priv->msi_map_lock);
85e6b78f2cSAntoine Tenart 
86e6b78f2cSAntoine Tenart 	bitmap_clear(priv->msi_map, first, num_req);
87e6b78f2cSAntoine Tenart 
88e6b78f2cSAntoine Tenart 	spin_unlock(&priv->msi_map_lock);
89e6b78f2cSAntoine Tenart }
90e6b78f2cSAntoine Tenart 
alpine_msix_compose_msi_msg(struct irq_data * data,struct msi_msg * msg)91e6b78f2cSAntoine Tenart static void alpine_msix_compose_msi_msg(struct irq_data *data,
92e6b78f2cSAntoine Tenart 					struct msi_msg *msg)
93e6b78f2cSAntoine Tenart {
94e6b78f2cSAntoine Tenart 	struct alpine_msix_data *priv = irq_data_get_irq_chip_data(data);
95e6b78f2cSAntoine Tenart 	phys_addr_t msg_addr = priv->addr;
96e6b78f2cSAntoine Tenart 
97e6b78f2cSAntoine Tenart 	msg_addr |= (data->hwirq << 3);
98e6b78f2cSAntoine Tenart 
99e6b78f2cSAntoine Tenart 	msg->address_hi = upper_32_bits(msg_addr);
100e6b78f2cSAntoine Tenart 	msg->address_lo = lower_32_bits(msg_addr);
101e6b78f2cSAntoine Tenart 	msg->data = 0;
102e6b78f2cSAntoine Tenart }
103e6b78f2cSAntoine Tenart 
104e6b78f2cSAntoine Tenart static struct msi_domain_info alpine_msix_domain_info = {
105e6b78f2cSAntoine Tenart 	.flags	= MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
106e6b78f2cSAntoine Tenart 		  MSI_FLAG_PCI_MSIX,
107e6b78f2cSAntoine Tenart 	.chip	= &alpine_msix_irq_chip,
108e6b78f2cSAntoine Tenart };
109e6b78f2cSAntoine Tenart 
110e6b78f2cSAntoine Tenart static struct irq_chip middle_irq_chip = {
111e6b78f2cSAntoine Tenart 	.name			= "alpine_msix_middle",
112e6b78f2cSAntoine Tenart 	.irq_mask		= irq_chip_mask_parent,
113e6b78f2cSAntoine Tenart 	.irq_unmask		= irq_chip_unmask_parent,
114e6b78f2cSAntoine Tenart 	.irq_eoi		= irq_chip_eoi_parent,
115e6b78f2cSAntoine Tenart 	.irq_set_affinity	= irq_chip_set_affinity_parent,
116e6b78f2cSAntoine Tenart 	.irq_compose_msi_msg	= alpine_msix_compose_msi_msg,
117e6b78f2cSAntoine Tenart };
118e6b78f2cSAntoine Tenart 
alpine_msix_gic_domain_alloc(struct irq_domain * domain,unsigned int virq,int sgi)119e6b78f2cSAntoine Tenart static int alpine_msix_gic_domain_alloc(struct irq_domain *domain,
120e6b78f2cSAntoine Tenart 					unsigned int virq, int sgi)
121e6b78f2cSAntoine Tenart {
122e6b78f2cSAntoine Tenart 	struct irq_fwspec fwspec;
123e6b78f2cSAntoine Tenart 	struct irq_data *d;
124e6b78f2cSAntoine Tenart 	int ret;
125e6b78f2cSAntoine Tenart 
126e6b78f2cSAntoine Tenart 	if (!is_of_node(domain->parent->fwnode))
127e6b78f2cSAntoine Tenart 		return -EINVAL;
128e6b78f2cSAntoine Tenart 
129e6b78f2cSAntoine Tenart 	fwspec.fwnode = domain->parent->fwnode;
130e6b78f2cSAntoine Tenart 	fwspec.param_count = 3;
131e6b78f2cSAntoine Tenart 	fwspec.param[0] = 0;
132e6b78f2cSAntoine Tenart 	fwspec.param[1] = sgi;
133e6b78f2cSAntoine Tenart 	fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
134e6b78f2cSAntoine Tenart 
135e6b78f2cSAntoine Tenart 	ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
136e6b78f2cSAntoine Tenart 	if (ret)
137e6b78f2cSAntoine Tenart 		return ret;
138e6b78f2cSAntoine Tenart 
139e6b78f2cSAntoine Tenart 	d = irq_domain_get_irq_data(domain->parent, virq);
140e6b78f2cSAntoine Tenart 	d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
141e6b78f2cSAntoine Tenart 
142e6b78f2cSAntoine Tenart 	return 0;
143e6b78f2cSAntoine Tenart }
144e6b78f2cSAntoine Tenart 
alpine_msix_middle_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)145e6b78f2cSAntoine Tenart static int alpine_msix_middle_domain_alloc(struct irq_domain *domain,
146e6b78f2cSAntoine Tenart 					   unsigned int virq,
147e6b78f2cSAntoine Tenart 					   unsigned int nr_irqs, void *args)
148e6b78f2cSAntoine Tenart {
149e6b78f2cSAntoine Tenart 	struct alpine_msix_data *priv = domain->host_data;
150e6b78f2cSAntoine Tenart 	int sgi, err, i;
151e6b78f2cSAntoine Tenart 
152e6b78f2cSAntoine Tenart 	sgi = alpine_msix_allocate_sgi(priv, nr_irqs);
153e6b78f2cSAntoine Tenart 	if (sgi < 0)
154e6b78f2cSAntoine Tenart 		return sgi;
155e6b78f2cSAntoine Tenart 
156e6b78f2cSAntoine Tenart 	for (i = 0; i < nr_irqs; i++) {
157e6b78f2cSAntoine Tenart 		err = alpine_msix_gic_domain_alloc(domain, virq + i, sgi + i);
158e6b78f2cSAntoine Tenart 		if (err)
159e6b78f2cSAntoine Tenart 			goto err_sgi;
160e6b78f2cSAntoine Tenart 
161e6b78f2cSAntoine Tenart 		irq_domain_set_hwirq_and_chip(domain, virq + i, sgi + i,
162e6b78f2cSAntoine Tenart 					      &middle_irq_chip, priv);
163e6b78f2cSAntoine Tenart 	}
164e6b78f2cSAntoine Tenart 
165e6b78f2cSAntoine Tenart 	return 0;
166e6b78f2cSAntoine Tenart 
167e6b78f2cSAntoine Tenart err_sgi:
168*a9bbafa4SZenghui Yu 	irq_domain_free_irqs_parent(domain, virq, i);
169e6b78f2cSAntoine Tenart 	alpine_msix_free_sgi(priv, sgi, nr_irqs);
170e6b78f2cSAntoine Tenart 	return err;
171e6b78f2cSAntoine Tenart }
172e6b78f2cSAntoine Tenart 
alpine_msix_middle_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)173e6b78f2cSAntoine Tenart static void alpine_msix_middle_domain_free(struct irq_domain *domain,
174e6b78f2cSAntoine Tenart 					   unsigned int virq,
175e6b78f2cSAntoine Tenart 					   unsigned int nr_irqs)
176e6b78f2cSAntoine Tenart {
177e6b78f2cSAntoine Tenart 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
178e6b78f2cSAntoine Tenart 	struct alpine_msix_data *priv = irq_data_get_irq_chip_data(d);
179e6b78f2cSAntoine Tenart 
180e6b78f2cSAntoine Tenart 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
181e6b78f2cSAntoine Tenart 	alpine_msix_free_sgi(priv, d->hwirq, nr_irqs);
182e6b78f2cSAntoine Tenart }
183e6b78f2cSAntoine Tenart 
184e6b78f2cSAntoine Tenart static const struct irq_domain_ops alpine_msix_middle_domain_ops = {
185e6b78f2cSAntoine Tenart 	.alloc	= alpine_msix_middle_domain_alloc,
186e6b78f2cSAntoine Tenart 	.free	= alpine_msix_middle_domain_free,
187e6b78f2cSAntoine Tenart };
188e6b78f2cSAntoine Tenart 
alpine_msix_init_domains(struct alpine_msix_data * priv,struct device_node * node)189e6b78f2cSAntoine Tenart static int alpine_msix_init_domains(struct alpine_msix_data *priv,
190e6b78f2cSAntoine Tenart 				    struct device_node *node)
191e6b78f2cSAntoine Tenart {
192e6b78f2cSAntoine Tenart 	struct irq_domain *middle_domain, *msi_domain, *gic_domain;
193e6b78f2cSAntoine Tenart 	struct device_node *gic_node;
194e6b78f2cSAntoine Tenart 
195e6b78f2cSAntoine Tenart 	gic_node = of_irq_find_parent(node);
196e6b78f2cSAntoine Tenart 	if (!gic_node) {
197e6b78f2cSAntoine Tenart 		pr_err("Failed to find the GIC node\n");
198e6b78f2cSAntoine Tenart 		return -ENODEV;
199e6b78f2cSAntoine Tenart 	}
200e6b78f2cSAntoine Tenart 
201e6b78f2cSAntoine Tenart 	gic_domain = irq_find_host(gic_node);
202071d068bSMiaoqian Lin 	of_node_put(gic_node);
203e6b78f2cSAntoine Tenart 	if (!gic_domain) {
204e6b78f2cSAntoine Tenart 		pr_err("Failed to find the GIC domain\n");
205e6b78f2cSAntoine Tenart 		return -ENXIO;
206e6b78f2cSAntoine Tenart 	}
207e6b78f2cSAntoine Tenart 
2086c889231SJohan Hovold 	middle_domain = irq_domain_add_hierarchy(gic_domain, 0, 0, NULL,
209e6b78f2cSAntoine Tenart 						 &alpine_msix_middle_domain_ops,
210e6b78f2cSAntoine Tenart 						 priv);
211e6b78f2cSAntoine Tenart 	if (!middle_domain) {
212e6b78f2cSAntoine Tenart 		pr_err("Failed to create the MSIX middle domain\n");
213e6b78f2cSAntoine Tenart 		return -ENOMEM;
214e6b78f2cSAntoine Tenart 	}
215e6b78f2cSAntoine Tenart 
216e6b78f2cSAntoine Tenart 	msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(node),
217e6b78f2cSAntoine Tenart 					       &alpine_msix_domain_info,
218e6b78f2cSAntoine Tenart 					       middle_domain);
219e6b78f2cSAntoine Tenart 	if (!msi_domain) {
220e6b78f2cSAntoine Tenart 		pr_err("Failed to create MSI domain\n");
221143d36a3SDan Carpenter 		irq_domain_remove(middle_domain);
222e6b78f2cSAntoine Tenart 		return -ENOMEM;
223e6b78f2cSAntoine Tenart 	}
224e6b78f2cSAntoine Tenart 
225e6b78f2cSAntoine Tenart 	return 0;
226e6b78f2cSAntoine Tenart }
227e6b78f2cSAntoine Tenart 
alpine_msix_init(struct device_node * node,struct device_node * parent)228e6b78f2cSAntoine Tenart static int alpine_msix_init(struct device_node *node,
229e6b78f2cSAntoine Tenart 			    struct device_node *parent)
230e6b78f2cSAntoine Tenart {
231e6b78f2cSAntoine Tenart 	struct alpine_msix_data *priv;
232e6b78f2cSAntoine Tenart 	struct resource res;
233e6b78f2cSAntoine Tenart 	int ret;
234e6b78f2cSAntoine Tenart 
235e6b78f2cSAntoine Tenart 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
236e6b78f2cSAntoine Tenart 	if (!priv)
237e6b78f2cSAntoine Tenart 		return -ENOMEM;
238e6b78f2cSAntoine Tenart 
239e6b78f2cSAntoine Tenart 	spin_lock_init(&priv->msi_map_lock);
240e6b78f2cSAntoine Tenart 
241e6b78f2cSAntoine Tenart 	ret = of_address_to_resource(node, 0, &res);
242e6b78f2cSAntoine Tenart 	if (ret) {
243e6b78f2cSAntoine Tenart 		pr_err("Failed to allocate resource\n");
244e6b78f2cSAntoine Tenart 		goto err_priv;
245e6b78f2cSAntoine Tenart 	}
246e6b78f2cSAntoine Tenart 
247e6b78f2cSAntoine Tenart 	/*
248e6b78f2cSAntoine Tenart 	 * The 20 least significant bits of addr provide direct information
249e6b78f2cSAntoine Tenart 	 * regarding the interrupt destination.
250e6b78f2cSAntoine Tenart 	 *
251e6b78f2cSAntoine Tenart 	 * To select the primary GIC as the target GIC, bits [18:17] must be set
252e6b78f2cSAntoine Tenart 	 * to 0x0. In this case, bit 16 (SPI_TARGET_CLUSTER0) must be set.
253e6b78f2cSAntoine Tenart 	 */
254e6b78f2cSAntoine Tenart 	priv->addr = res.start & GENMASK_ULL(63,20);
255e6b78f2cSAntoine Tenart 	priv->addr |= ALPINE_MSIX_SPI_TARGET_CLUSTER0;
256e6b78f2cSAntoine Tenart 
257e6b78f2cSAntoine Tenart 	if (of_property_read_u32(node, "al,msi-base-spi", &priv->spi_first)) {
258e6b78f2cSAntoine Tenart 		pr_err("Unable to parse MSI base\n");
259e6b78f2cSAntoine Tenart 		ret = -EINVAL;
260e6b78f2cSAntoine Tenart 		goto err_priv;
261e6b78f2cSAntoine Tenart 	}
262e6b78f2cSAntoine Tenart 
263e6b78f2cSAntoine Tenart 	if (of_property_read_u32(node, "al,msi-num-spis", &priv->num_spis)) {
264e6b78f2cSAntoine Tenart 		pr_err("Unable to parse MSI numbers\n");
265e6b78f2cSAntoine Tenart 		ret = -EINVAL;
266e6b78f2cSAntoine Tenart 		goto err_priv;
267e6b78f2cSAntoine Tenart 	}
268e6b78f2cSAntoine Tenart 
2693f1808f6SAndy Shevchenko 	priv->msi_map = bitmap_zalloc(priv->num_spis, GFP_KERNEL);
270e6b78f2cSAntoine Tenart 	if (!priv->msi_map) {
271e6b78f2cSAntoine Tenart 		ret = -ENOMEM;
272e6b78f2cSAntoine Tenart 		goto err_priv;
273e6b78f2cSAntoine Tenart 	}
274e6b78f2cSAntoine Tenart 
275e6b78f2cSAntoine Tenart 	pr_debug("Registering %d msixs, starting at %d\n",
276e6b78f2cSAntoine Tenart 		 priv->num_spis, priv->spi_first);
277e6b78f2cSAntoine Tenart 
278e6b78f2cSAntoine Tenart 	ret = alpine_msix_init_domains(priv, node);
279e6b78f2cSAntoine Tenart 	if (ret)
280e6b78f2cSAntoine Tenart 		goto err_map;
281e6b78f2cSAntoine Tenart 
282e6b78f2cSAntoine Tenart 	return 0;
283e6b78f2cSAntoine Tenart 
284e6b78f2cSAntoine Tenart err_map:
2853f1808f6SAndy Shevchenko 	bitmap_free(priv->msi_map);
286e6b78f2cSAntoine Tenart err_priv:
287e6b78f2cSAntoine Tenart 	kfree(priv);
288e6b78f2cSAntoine Tenart 	return ret;
289e6b78f2cSAntoine Tenart }
290e6b78f2cSAntoine Tenart IRQCHIP_DECLARE(alpine_msix, "al,alpine-msix", alpine_msix_init);
291