xref: /openbmc/linux/drivers/irqchip/irq-mvebu-gicp.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1a68a63cbSThomas Petazzoni /*
2a68a63cbSThomas Petazzoni  * Copyright (C) 2017 Marvell
3a68a63cbSThomas Petazzoni  *
4a68a63cbSThomas Petazzoni  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
5a68a63cbSThomas Petazzoni  *
6a68a63cbSThomas Petazzoni  * This file is licensed under the terms of the GNU General Public
7a68a63cbSThomas Petazzoni  * License version 2. This program is licensed "as is" without any
8a68a63cbSThomas Petazzoni  * warranty of any kind, whether express or implied.
9a68a63cbSThomas Petazzoni  */
10a68a63cbSThomas Petazzoni 
11a68a63cbSThomas Petazzoni #include <linux/io.h>
12a68a63cbSThomas Petazzoni #include <linux/irq.h>
13a68a63cbSThomas Petazzoni #include <linux/irqdomain.h>
14a68a63cbSThomas Petazzoni #include <linux/msi.h>
15a68a63cbSThomas Petazzoni #include <linux/of.h>
16a68a63cbSThomas Petazzoni #include <linux/of_irq.h>
17a68a63cbSThomas Petazzoni #include <linux/of_platform.h>
18a68a63cbSThomas Petazzoni #include <linux/platform_device.h>
19a68a63cbSThomas Petazzoni 
20a68a63cbSThomas Petazzoni #include <dt-bindings/interrupt-controller/arm-gic.h>
21a68a63cbSThomas Petazzoni 
22a68a63cbSThomas Petazzoni #define GICP_SETSPI_NSR_OFFSET	0x0
23a68a63cbSThomas Petazzoni #define GICP_CLRSPI_NSR_OFFSET	0x8
24a68a63cbSThomas Petazzoni 
25a68a63cbSThomas Petazzoni struct mvebu_gicp_spi_range {
26a68a63cbSThomas Petazzoni 	unsigned int start;
27a68a63cbSThomas Petazzoni 	unsigned int count;
28a68a63cbSThomas Petazzoni };
29a68a63cbSThomas Petazzoni 
30a68a63cbSThomas Petazzoni struct mvebu_gicp {
31a68a63cbSThomas Petazzoni 	struct mvebu_gicp_spi_range *spi_ranges;
32a68a63cbSThomas Petazzoni 	unsigned int spi_ranges_cnt;
33a68a63cbSThomas Petazzoni 	unsigned int spi_cnt;
34a68a63cbSThomas Petazzoni 	unsigned long *spi_bitmap;
35a68a63cbSThomas Petazzoni 	spinlock_t spi_lock;
36a68a63cbSThomas Petazzoni 	struct resource *res;
37a68a63cbSThomas Petazzoni 	struct device *dev;
38a68a63cbSThomas Petazzoni };
39a68a63cbSThomas Petazzoni 
gicp_idx_to_spi(struct mvebu_gicp * gicp,int idx)40a68a63cbSThomas Petazzoni static int gicp_idx_to_spi(struct mvebu_gicp *gicp, int idx)
41a68a63cbSThomas Petazzoni {
42a68a63cbSThomas Petazzoni 	int i;
43a68a63cbSThomas Petazzoni 
44a68a63cbSThomas Petazzoni 	for (i = 0; i < gicp->spi_ranges_cnt; i++) {
45a68a63cbSThomas Petazzoni 		struct mvebu_gicp_spi_range *r = &gicp->spi_ranges[i];
46a68a63cbSThomas Petazzoni 
47a68a63cbSThomas Petazzoni 		if (idx < r->count)
48a68a63cbSThomas Petazzoni 			return r->start + idx;
49a68a63cbSThomas Petazzoni 
50a68a63cbSThomas Petazzoni 		idx -= r->count;
51a68a63cbSThomas Petazzoni 	}
52a68a63cbSThomas Petazzoni 
53a68a63cbSThomas Petazzoni 	return -EINVAL;
54a68a63cbSThomas Petazzoni }
55a68a63cbSThomas Petazzoni 
gicp_compose_msi_msg(struct irq_data * data,struct msi_msg * msg)56a68a63cbSThomas Petazzoni static void gicp_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
57a68a63cbSThomas Petazzoni {
58a68a63cbSThomas Petazzoni 	struct mvebu_gicp *gicp = data->chip_data;
59a68a63cbSThomas Petazzoni 	phys_addr_t setspi = gicp->res->start + GICP_SETSPI_NSR_OFFSET;
6025eaaabbSMarc Zyngier 	phys_addr_t clrspi = gicp->res->start + GICP_CLRSPI_NSR_OFFSET;
61a68a63cbSThomas Petazzoni 
6225eaaabbSMarc Zyngier 	msg[0].data = data->hwirq;
6325eaaabbSMarc Zyngier 	msg[0].address_lo = lower_32_bits(setspi);
6425eaaabbSMarc Zyngier 	msg[0].address_hi = upper_32_bits(setspi);
6525eaaabbSMarc Zyngier 	msg[1].data = data->hwirq;
6625eaaabbSMarc Zyngier 	msg[1].address_lo = lower_32_bits(clrspi);
6725eaaabbSMarc Zyngier 	msg[1].address_hi = upper_32_bits(clrspi);
68a68a63cbSThomas Petazzoni }
69a68a63cbSThomas Petazzoni 
70a68a63cbSThomas Petazzoni static struct irq_chip gicp_irq_chip = {
71a68a63cbSThomas Petazzoni 	.name			= "GICP",
72a68a63cbSThomas Petazzoni 	.irq_mask		= irq_chip_mask_parent,
73a68a63cbSThomas Petazzoni 	.irq_unmask		= irq_chip_unmask_parent,
74a68a63cbSThomas Petazzoni 	.irq_eoi		= irq_chip_eoi_parent,
75a68a63cbSThomas Petazzoni 	.irq_set_affinity	= irq_chip_set_affinity_parent,
76a68a63cbSThomas Petazzoni 	.irq_set_type		= irq_chip_set_type_parent,
77a68a63cbSThomas Petazzoni 	.irq_compose_msi_msg	= gicp_compose_msi_msg,
78a68a63cbSThomas Petazzoni };
79a68a63cbSThomas Petazzoni 
gicp_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)80a68a63cbSThomas Petazzoni static int gicp_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
81a68a63cbSThomas Petazzoni 				 unsigned int nr_irqs, void *args)
82a68a63cbSThomas Petazzoni {
83a68a63cbSThomas Petazzoni 	struct mvebu_gicp *gicp = domain->host_data;
84a68a63cbSThomas Petazzoni 	struct irq_fwspec fwspec;
85a68a63cbSThomas Petazzoni 	unsigned int hwirq;
86a68a63cbSThomas Petazzoni 	int ret;
87a68a63cbSThomas Petazzoni 
88a68a63cbSThomas Petazzoni 	spin_lock(&gicp->spi_lock);
89a68a63cbSThomas Petazzoni 	hwirq = find_first_zero_bit(gicp->spi_bitmap, gicp->spi_cnt);
90a68a63cbSThomas Petazzoni 	if (hwirq == gicp->spi_cnt) {
91a68a63cbSThomas Petazzoni 		spin_unlock(&gicp->spi_lock);
92a68a63cbSThomas Petazzoni 		return -ENOSPC;
93a68a63cbSThomas Petazzoni 	}
94a68a63cbSThomas Petazzoni 	__set_bit(hwirq, gicp->spi_bitmap);
95a68a63cbSThomas Petazzoni 	spin_unlock(&gicp->spi_lock);
96a68a63cbSThomas Petazzoni 
97a68a63cbSThomas Petazzoni 	fwspec.fwnode = domain->parent->fwnode;
98a68a63cbSThomas Petazzoni 	fwspec.param_count = 3;
99a68a63cbSThomas Petazzoni 	fwspec.param[0] = GIC_SPI;
100a68a63cbSThomas Petazzoni 	fwspec.param[1] = gicp_idx_to_spi(gicp, hwirq) - 32;
101a68a63cbSThomas Petazzoni 	/*
102a68a63cbSThomas Petazzoni 	 * Assume edge rising for now, it will be properly set when
103a68a63cbSThomas Petazzoni 	 * ->set_type() is called
104a68a63cbSThomas Petazzoni 	 */
105a68a63cbSThomas Petazzoni 	fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
106a68a63cbSThomas Petazzoni 
107a68a63cbSThomas Petazzoni 	ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
108a68a63cbSThomas Petazzoni 	if (ret) {
109a68a63cbSThomas Petazzoni 		dev_err(gicp->dev, "Cannot allocate parent IRQ\n");
110a68a63cbSThomas Petazzoni 		goto free_hwirq;
111a68a63cbSThomas Petazzoni 	}
112a68a63cbSThomas Petazzoni 
113a68a63cbSThomas Petazzoni 	ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
114a68a63cbSThomas Petazzoni 					    &gicp_irq_chip, gicp);
115a68a63cbSThomas Petazzoni 	if (ret)
116a68a63cbSThomas Petazzoni 		goto free_irqs_parent;
117a68a63cbSThomas Petazzoni 
118a68a63cbSThomas Petazzoni 	return 0;
119a68a63cbSThomas Petazzoni 
120a68a63cbSThomas Petazzoni free_irqs_parent:
121a68a63cbSThomas Petazzoni 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
122a68a63cbSThomas Petazzoni free_hwirq:
123a68a63cbSThomas Petazzoni 	spin_lock(&gicp->spi_lock);
124a68a63cbSThomas Petazzoni 	__clear_bit(hwirq, gicp->spi_bitmap);
125a68a63cbSThomas Petazzoni 	spin_unlock(&gicp->spi_lock);
126a68a63cbSThomas Petazzoni 	return ret;
127a68a63cbSThomas Petazzoni }
128a68a63cbSThomas Petazzoni 
gicp_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)129a68a63cbSThomas Petazzoni static void gicp_irq_domain_free(struct irq_domain *domain,
130a68a63cbSThomas Petazzoni 				 unsigned int virq, unsigned int nr_irqs)
131a68a63cbSThomas Petazzoni {
132a68a63cbSThomas Petazzoni 	struct mvebu_gicp *gicp = domain->host_data;
133a68a63cbSThomas Petazzoni 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
134a68a63cbSThomas Petazzoni 
135a68a63cbSThomas Petazzoni 	if (d->hwirq >= gicp->spi_cnt) {
136a68a63cbSThomas Petazzoni 		dev_err(gicp->dev, "Invalid hwirq %lu\n", d->hwirq);
137a68a63cbSThomas Petazzoni 		return;
138a68a63cbSThomas Petazzoni 	}
139a68a63cbSThomas Petazzoni 
140a68a63cbSThomas Petazzoni 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
141a68a63cbSThomas Petazzoni 
142a68a63cbSThomas Petazzoni 	spin_lock(&gicp->spi_lock);
143a68a63cbSThomas Petazzoni 	__clear_bit(d->hwirq, gicp->spi_bitmap);
144a68a63cbSThomas Petazzoni 	spin_unlock(&gicp->spi_lock);
145a68a63cbSThomas Petazzoni }
146a68a63cbSThomas Petazzoni 
147a68a63cbSThomas Petazzoni static const struct irq_domain_ops gicp_domain_ops = {
148a68a63cbSThomas Petazzoni 	.alloc	= gicp_irq_domain_alloc,
149a68a63cbSThomas Petazzoni 	.free	= gicp_irq_domain_free,
150a68a63cbSThomas Petazzoni };
151a68a63cbSThomas Petazzoni 
152a68a63cbSThomas Petazzoni static struct irq_chip gicp_msi_irq_chip = {
153a68a63cbSThomas Petazzoni 	.name		= "GICP",
154a68a63cbSThomas Petazzoni 	.irq_set_type	= irq_chip_set_type_parent,
15525eaaabbSMarc Zyngier 	.flags		= IRQCHIP_SUPPORTS_LEVEL_MSI,
156a68a63cbSThomas Petazzoni };
157a68a63cbSThomas Petazzoni 
158a68a63cbSThomas Petazzoni static struct msi_domain_ops gicp_msi_ops = {
159a68a63cbSThomas Petazzoni };
160a68a63cbSThomas Petazzoni 
161a68a63cbSThomas Petazzoni static struct msi_domain_info gicp_msi_domain_info = {
16225eaaabbSMarc Zyngier 	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
16325eaaabbSMarc Zyngier 		   MSI_FLAG_LEVEL_CAPABLE),
164a68a63cbSThomas Petazzoni 	.ops	= &gicp_msi_ops,
165a68a63cbSThomas Petazzoni 	.chip	= &gicp_msi_irq_chip,
166a68a63cbSThomas Petazzoni };
167a68a63cbSThomas Petazzoni 
mvebu_gicp_probe(struct platform_device * pdev)168a68a63cbSThomas Petazzoni static int mvebu_gicp_probe(struct platform_device *pdev)
169a68a63cbSThomas Petazzoni {
170a68a63cbSThomas Petazzoni 	struct mvebu_gicp *gicp;
171a68a63cbSThomas Petazzoni 	struct irq_domain *inner_domain, *plat_domain, *parent_domain;
172a68a63cbSThomas Petazzoni 	struct device_node *node = pdev->dev.of_node;
173a68a63cbSThomas Petazzoni 	struct device_node *irq_parent_dn;
174a68a63cbSThomas Petazzoni 	int ret, i;
175a68a63cbSThomas Petazzoni 
176a68a63cbSThomas Petazzoni 	gicp = devm_kzalloc(&pdev->dev, sizeof(*gicp), GFP_KERNEL);
177a68a63cbSThomas Petazzoni 	if (!gicp)
178a68a63cbSThomas Petazzoni 		return -ENOMEM;
179a68a63cbSThomas Petazzoni 
180a68a63cbSThomas Petazzoni 	gicp->dev = &pdev->dev;
181c9bb8633SAntoine Tenart 	spin_lock_init(&gicp->spi_lock);
182a68a63cbSThomas Petazzoni 
183a68a63cbSThomas Petazzoni 	gicp->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
184a68a63cbSThomas Petazzoni 	if (!gicp->res)
185a68a63cbSThomas Petazzoni 		return -ENODEV;
186a68a63cbSThomas Petazzoni 
187a68a63cbSThomas Petazzoni 	ret = of_property_count_u32_elems(node, "marvell,spi-ranges");
188a68a63cbSThomas Petazzoni 	if (ret < 0)
189a68a63cbSThomas Petazzoni 		return ret;
190a68a63cbSThomas Petazzoni 
191a68a63cbSThomas Petazzoni 	gicp->spi_ranges_cnt = ret / 2;
192a68a63cbSThomas Petazzoni 
193a68a63cbSThomas Petazzoni 	gicp->spi_ranges =
194a86854d0SKees Cook 		devm_kcalloc(&pdev->dev,
195a86854d0SKees Cook 			     gicp->spi_ranges_cnt,
196a68a63cbSThomas Petazzoni 			     sizeof(struct mvebu_gicp_spi_range),
197a68a63cbSThomas Petazzoni 			     GFP_KERNEL);
198a68a63cbSThomas Petazzoni 	if (!gicp->spi_ranges)
199a68a63cbSThomas Petazzoni 		return -ENOMEM;
200a68a63cbSThomas Petazzoni 
201a68a63cbSThomas Petazzoni 	for (i = 0; i < gicp->spi_ranges_cnt; i++) {
202a68a63cbSThomas Petazzoni 		of_property_read_u32_index(node, "marvell,spi-ranges",
203a68a63cbSThomas Petazzoni 					   i * 2,
204a68a63cbSThomas Petazzoni 					   &gicp->spi_ranges[i].start);
205a68a63cbSThomas Petazzoni 
206a68a63cbSThomas Petazzoni 		of_property_read_u32_index(node, "marvell,spi-ranges",
207a68a63cbSThomas Petazzoni 					   i * 2 + 1,
208a68a63cbSThomas Petazzoni 					   &gicp->spi_ranges[i].count);
209a68a63cbSThomas Petazzoni 
210a68a63cbSThomas Petazzoni 		gicp->spi_cnt += gicp->spi_ranges[i].count;
211a68a63cbSThomas Petazzoni 	}
212a68a63cbSThomas Petazzoni 
2133db3969fSAndy Shevchenko 	gicp->spi_bitmap = devm_bitmap_zalloc(&pdev->dev, gicp->spi_cnt, GFP_KERNEL);
214a68a63cbSThomas Petazzoni 	if (!gicp->spi_bitmap)
215a68a63cbSThomas Petazzoni 		return -ENOMEM;
216a68a63cbSThomas Petazzoni 
217a68a63cbSThomas Petazzoni 	irq_parent_dn = of_irq_find_parent(node);
218a68a63cbSThomas Petazzoni 	if (!irq_parent_dn) {
219a68a63cbSThomas Petazzoni 		dev_err(&pdev->dev, "failed to find parent IRQ node\n");
220a68a63cbSThomas Petazzoni 		return -ENODEV;
221a68a63cbSThomas Petazzoni 	}
222a68a63cbSThomas Petazzoni 
223a68a63cbSThomas Petazzoni 	parent_domain = irq_find_host(irq_parent_dn);
224*9419e700SMiaoqian Lin 	of_node_put(irq_parent_dn);
225a68a63cbSThomas Petazzoni 	if (!parent_domain) {
226a68a63cbSThomas Petazzoni 		dev_err(&pdev->dev, "failed to find parent IRQ domain\n");
227a68a63cbSThomas Petazzoni 		return -ENODEV;
228a68a63cbSThomas Petazzoni 	}
229a68a63cbSThomas Petazzoni 
230a68a63cbSThomas Petazzoni 	inner_domain = irq_domain_create_hierarchy(parent_domain, 0,
231a68a63cbSThomas Petazzoni 						   gicp->spi_cnt,
232a68a63cbSThomas Petazzoni 						   of_node_to_fwnode(node),
233a68a63cbSThomas Petazzoni 						   &gicp_domain_ops, gicp);
234a68a63cbSThomas Petazzoni 	if (!inner_domain)
235a68a63cbSThomas Petazzoni 		return -ENOMEM;
236a68a63cbSThomas Petazzoni 
237a68a63cbSThomas Petazzoni 
238a68a63cbSThomas Petazzoni 	plat_domain = platform_msi_create_irq_domain(of_node_to_fwnode(node),
239a68a63cbSThomas Petazzoni 						     &gicp_msi_domain_info,
240a68a63cbSThomas Petazzoni 						     inner_domain);
241a68a63cbSThomas Petazzoni 	if (!plat_domain) {
242a68a63cbSThomas Petazzoni 		irq_domain_remove(inner_domain);
243a68a63cbSThomas Petazzoni 		return -ENOMEM;
244a68a63cbSThomas Petazzoni 	}
245a68a63cbSThomas Petazzoni 
246a68a63cbSThomas Petazzoni 	platform_set_drvdata(pdev, gicp);
247a68a63cbSThomas Petazzoni 
248a68a63cbSThomas Petazzoni 	return 0;
249a68a63cbSThomas Petazzoni }
250a68a63cbSThomas Petazzoni 
251a68a63cbSThomas Petazzoni static const struct of_device_id mvebu_gicp_of_match[] = {
252a68a63cbSThomas Petazzoni 	{ .compatible = "marvell,ap806-gicp", },
253a68a63cbSThomas Petazzoni 	{},
254a68a63cbSThomas Petazzoni };
255a68a63cbSThomas Petazzoni 
256a68a63cbSThomas Petazzoni static struct platform_driver mvebu_gicp_driver = {
257a68a63cbSThomas Petazzoni 	.probe  = mvebu_gicp_probe,
258a68a63cbSThomas Petazzoni 	.driver = {
259a68a63cbSThomas Petazzoni 		.name = "mvebu-gicp",
260a68a63cbSThomas Petazzoni 		.of_match_table = mvebu_gicp_of_match,
261a68a63cbSThomas Petazzoni 	},
262a68a63cbSThomas Petazzoni };
263a68a63cbSThomas Petazzoni builtin_platform_driver(mvebu_gicp_driver);
264