1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
296ca848eSSricharan R /*
396ca848eSSricharan R * drivers/irqchip/irq-crossbar.c
496ca848eSSricharan R *
596ca848eSSricharan R * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
696ca848eSSricharan R * Author: Sricharan R <r.sricharan@ti.com>
796ca848eSSricharan R */
896ca848eSSricharan R #include <linux/err.h>
996ca848eSSricharan R #include <linux/io.h>
1041a83e06SJoel Porquet #include <linux/irqchip.h>
11783d3186SMarc Zyngier #include <linux/irqdomain.h>
1296ca848eSSricharan R #include <linux/of_address.h>
1396ca848eSSricharan R #include <linux/of_irq.h>
1496ca848eSSricharan R #include <linux/slab.h>
15783d3186SMarc Zyngier
1696ca848eSSricharan R #define IRQ_FREE -1
171d50d2ceSNishanth Menon #define IRQ_RESERVED -2
1864e0f8baSNishanth Menon #define IRQ_SKIP -3
1996ca848eSSricharan R #define GIC_IRQ_START 32
2096ca848eSSricharan R
21e30ef8abSNishanth Menon /**
22e30ef8abSNishanth Menon * struct crossbar_device - crossbar device description
23783d3186SMarc Zyngier * @lock: spinlock serializing access to @irq_map
2496ca848eSSricharan R * @int_max: maximum number of supported interrupts
25a35057d1SNishanth Menon * @safe_map: safe default value to initialize the crossbar
262f7d2fb7SNishanth Menon * @max_crossbar_sources: Maximum number of crossbar sources
2796ca848eSSricharan R * @irq_map: array of interrupts to crossbar number mapping
2896ca848eSSricharan R * @crossbar_base: crossbar base address
2996ca848eSSricharan R * @register_offsets: offsets for each irq number
30e30ef8abSNishanth Menon * @write: register write function pointer
3196ca848eSSricharan R */
3296ca848eSSricharan R struct crossbar_device {
33783d3186SMarc Zyngier raw_spinlock_t lock;
3496ca848eSSricharan R uint int_max;
35a35057d1SNishanth Menon uint safe_map;
362f7d2fb7SNishanth Menon uint max_crossbar_sources;
3796ca848eSSricharan R uint *irq_map;
3896ca848eSSricharan R void __iomem *crossbar_base;
3996ca848eSSricharan R int *register_offsets;
4096ca848eSSricharan R void (*write)(int, int);
4196ca848eSSricharan R };
4296ca848eSSricharan R
4396ca848eSSricharan R static struct crossbar_device *cb;
4496ca848eSSricharan R
crossbar_writel(int irq_no,int cb_no)45783d3186SMarc Zyngier static void crossbar_writel(int irq_no, int cb_no)
4696ca848eSSricharan R {
4796ca848eSSricharan R writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
4896ca848eSSricharan R }
4996ca848eSSricharan R
crossbar_writew(int irq_no,int cb_no)50783d3186SMarc Zyngier static void crossbar_writew(int irq_no, int cb_no)
5196ca848eSSricharan R {
5296ca848eSSricharan R writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
5396ca848eSSricharan R }
5496ca848eSSricharan R
crossbar_writeb(int irq_no,int cb_no)55783d3186SMarc Zyngier static void crossbar_writeb(int irq_no, int cb_no)
5696ca848eSSricharan R {
5796ca848eSSricharan R writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
5896ca848eSSricharan R }
5996ca848eSSricharan R
60783d3186SMarc Zyngier static struct irq_chip crossbar_chip = {
61783d3186SMarc Zyngier .name = "CBAR",
62783d3186SMarc Zyngier .irq_eoi = irq_chip_eoi_parent,
63783d3186SMarc Zyngier .irq_mask = irq_chip_mask_parent,
64783d3186SMarc Zyngier .irq_unmask = irq_chip_unmask_parent,
65783d3186SMarc Zyngier .irq_retrigger = irq_chip_retrigger_hierarchy,
66e269ec42SGrygorii Strashko .irq_set_type = irq_chip_set_type_parent,
678200fe43SGrygorii Strashko .flags = IRQCHIP_MASK_ON_SUSPEND |
688200fe43SGrygorii Strashko IRQCHIP_SKIP_SET_WAKE,
69783d3186SMarc Zyngier #ifdef CONFIG_SMP
70783d3186SMarc Zyngier .irq_set_affinity = irq_chip_set_affinity_parent,
71783d3186SMarc Zyngier #endif
72783d3186SMarc Zyngier };
73783d3186SMarc Zyngier
allocate_gic_irq(struct irq_domain * domain,unsigned virq,irq_hw_number_t hwirq)74783d3186SMarc Zyngier static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
75783d3186SMarc Zyngier irq_hw_number_t hwirq)
766f16fc87SNishanth Menon {
77f833f57fSMarc Zyngier struct irq_fwspec fwspec;
786f16fc87SNishanth Menon int i;
79783d3186SMarc Zyngier int err;
806f16fc87SNishanth Menon
81f833f57fSMarc Zyngier if (!irq_domain_get_of_node(domain->parent))
82f833f57fSMarc Zyngier return -EINVAL;
83f833f57fSMarc Zyngier
84783d3186SMarc Zyngier raw_spin_lock(&cb->lock);
85ddee0fb4SNishanth Menon for (i = cb->int_max - 1; i >= 0; i--) {
8696ca848eSSricharan R if (cb->irq_map[i] == IRQ_FREE) {
87783d3186SMarc Zyngier cb->irq_map[i] = hwirq;
88783d3186SMarc Zyngier break;
8996ca848eSSricharan R }
9096ca848eSSricharan R }
91783d3186SMarc Zyngier raw_spin_unlock(&cb->lock);
9296ca848eSSricharan R
93783d3186SMarc Zyngier if (i < 0)
9496ca848eSSricharan R return -ENODEV;
95783d3186SMarc Zyngier
96f833f57fSMarc Zyngier fwspec.fwnode = domain->parent->fwnode;
97f833f57fSMarc Zyngier fwspec.param_count = 3;
98f833f57fSMarc Zyngier fwspec.param[0] = 0; /* SPI */
99f833f57fSMarc Zyngier fwspec.param[1] = i;
100f833f57fSMarc Zyngier fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
101783d3186SMarc Zyngier
102f833f57fSMarc Zyngier err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
103783d3186SMarc Zyngier if (err)
104783d3186SMarc Zyngier cb->irq_map[i] = IRQ_FREE;
105783d3186SMarc Zyngier else
106783d3186SMarc Zyngier cb->write(i, hwirq);
107783d3186SMarc Zyngier
108783d3186SMarc Zyngier return err;
10996ca848eSSricharan R }
11096ca848eSSricharan R
crossbar_domain_alloc(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs,void * data)111783d3186SMarc Zyngier static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
112783d3186SMarc Zyngier unsigned int nr_irqs, void *data)
11329918b67SNishanth Menon {
114f833f57fSMarc Zyngier struct irq_fwspec *fwspec = data;
115783d3186SMarc Zyngier irq_hw_number_t hwirq;
116783d3186SMarc Zyngier int i;
117d360892dSNishanth Menon
118f833f57fSMarc Zyngier if (fwspec->param_count != 3)
119783d3186SMarc Zyngier return -EINVAL; /* Not GIC compliant */
120f833f57fSMarc Zyngier if (fwspec->param[0] != 0)
121783d3186SMarc Zyngier return -EINVAL; /* No PPI should point to this domain */
122783d3186SMarc Zyngier
123f833f57fSMarc Zyngier hwirq = fwspec->param[1];
124783d3186SMarc Zyngier if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
125783d3186SMarc Zyngier return -EINVAL; /* Can't deal with this */
126783d3186SMarc Zyngier
127783d3186SMarc Zyngier for (i = 0; i < nr_irqs; i++) {
128783d3186SMarc Zyngier int err = allocate_gic_irq(d, virq + i, hwirq + i);
129783d3186SMarc Zyngier
130783d3186SMarc Zyngier if (err)
131783d3186SMarc Zyngier return err;
132783d3186SMarc Zyngier
133783d3186SMarc Zyngier irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
134783d3186SMarc Zyngier &crossbar_chip, NULL);
135d360892dSNishanth Menon }
13629918b67SNishanth Menon
13796ca848eSSricharan R return 0;
13896ca848eSSricharan R }
13996ca848eSSricharan R
1408b09a45dSSricharan R /**
141783d3186SMarc Zyngier * crossbar_domain_free - unmap/free a crossbar<->irq connection
142783d3186SMarc Zyngier * @domain: domain of irq to unmap
143783d3186SMarc Zyngier * @virq: virq number
144783d3186SMarc Zyngier * @nr_irqs: number of irqs to free
1458b09a45dSSricharan R *
1468b09a45dSSricharan R * We do not maintain a use count of total number of map/unmap
1478b09a45dSSricharan R * calls for a particular irq to find out if a irq can be really
1488b09a45dSSricharan R * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
1498b09a45dSSricharan R * after which irq is anyways unusable. So an explicit map has to be called
1508b09a45dSSricharan R * after that.
1518b09a45dSSricharan R */
crossbar_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)152783d3186SMarc Zyngier static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
153783d3186SMarc Zyngier unsigned int nr_irqs)
15496ca848eSSricharan R {
155783d3186SMarc Zyngier int i;
15696ca848eSSricharan R
157783d3186SMarc Zyngier raw_spin_lock(&cb->lock);
158783d3186SMarc Zyngier for (i = 0; i < nr_irqs; i++) {
159783d3186SMarc Zyngier struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
160783d3186SMarc Zyngier
161783d3186SMarc Zyngier irq_domain_reset_irq_data(d);
162783d3186SMarc Zyngier cb->irq_map[d->hwirq] = IRQ_FREE;
163783d3186SMarc Zyngier cb->write(d->hwirq, cb->safe_map);
164a35057d1SNishanth Menon }
165783d3186SMarc Zyngier raw_spin_unlock(&cb->lock);
16696ca848eSSricharan R }
16796ca848eSSricharan R
crossbar_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)168f833f57fSMarc Zyngier static int crossbar_domain_translate(struct irq_domain *d,
169f833f57fSMarc Zyngier struct irq_fwspec *fwspec,
170f833f57fSMarc Zyngier unsigned long *hwirq,
171f833f57fSMarc Zyngier unsigned int *type)
17296ca848eSSricharan R {
173f833f57fSMarc Zyngier if (is_of_node(fwspec->fwnode)) {
174f833f57fSMarc Zyngier if (fwspec->param_count != 3)
175f833f57fSMarc Zyngier return -EINVAL;
17696ca848eSSricharan R
177f833f57fSMarc Zyngier /* No PPI should point to this domain */
178f833f57fSMarc Zyngier if (fwspec->param[0] != 0)
179f833f57fSMarc Zyngier return -EINVAL;
180f833f57fSMarc Zyngier
181f833f57fSMarc Zyngier *hwirq = fwspec->param[1];
182a2a8fa55SJon Hunter *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
18396ca848eSSricharan R return 0;
18496ca848eSSricharan R }
18596ca848eSSricharan R
186f833f57fSMarc Zyngier return -EINVAL;
187f833f57fSMarc Zyngier }
188f833f57fSMarc Zyngier
189783d3186SMarc Zyngier static const struct irq_domain_ops crossbar_domain_ops = {
190783d3186SMarc Zyngier .alloc = crossbar_domain_alloc,
191783d3186SMarc Zyngier .free = crossbar_domain_free,
192f833f57fSMarc Zyngier .translate = crossbar_domain_translate,
19396ca848eSSricharan R };
19496ca848eSSricharan R
crossbar_of_init(struct device_node * node)19596ca848eSSricharan R static int __init crossbar_of_init(struct device_node *node)
19696ca848eSSricharan R {
1974b9de5daSFranck Demathieu u32 max = 0, entry, reg_size;
198b28ace12SFranck Demathieu int i, size, reserved = 0;
19996ca848eSSricharan R const __be32 *irqsr;
200edb442deSNishanth Menon int ret = -ENOMEM;
20196ca848eSSricharan R
2023894e9e8SDan Carpenter cb = kzalloc(sizeof(*cb), GFP_KERNEL);
20396ca848eSSricharan R
20496ca848eSSricharan R if (!cb)
205edb442deSNishanth Menon return ret;
20696ca848eSSricharan R
20796ca848eSSricharan R cb->crossbar_base = of_iomap(node, 0);
20896ca848eSSricharan R if (!cb->crossbar_base)
2093c44d515SNishanth Menon goto err_cb;
21096ca848eSSricharan R
2112f7d2fb7SNishanth Menon of_property_read_u32(node, "ti,max-crossbar-sources",
2122f7d2fb7SNishanth Menon &cb->max_crossbar_sources);
2132f7d2fb7SNishanth Menon if (!cb->max_crossbar_sources) {
2142f7d2fb7SNishanth Menon pr_err("missing 'ti,max-crossbar-sources' property\n");
2152f7d2fb7SNishanth Menon ret = -EINVAL;
2162f7d2fb7SNishanth Menon goto err_base;
2172f7d2fb7SNishanth Menon }
2182f7d2fb7SNishanth Menon
21996ca848eSSricharan R of_property_read_u32(node, "ti,max-irqs", &max);
220edb442deSNishanth Menon if (!max) {
221edb442deSNishanth Menon pr_err("missing 'ti,max-irqs' property\n");
222edb442deSNishanth Menon ret = -EINVAL;
2233c44d515SNishanth Menon goto err_base;
224edb442deSNishanth Menon }
2254dbf45e3SNishanth Menon cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
22696ca848eSSricharan R if (!cb->irq_map)
2273c44d515SNishanth Menon goto err_base;
22896ca848eSSricharan R
22996ca848eSSricharan R cb->int_max = max;
23096ca848eSSricharan R
23196ca848eSSricharan R for (i = 0; i < max; i++)
23296ca848eSSricharan R cb->irq_map[i] = IRQ_FREE;
23396ca848eSSricharan R
23496ca848eSSricharan R /* Get and mark reserved irqs */
23596ca848eSSricharan R irqsr = of_get_property(node, "ti,irqs-reserved", &size);
23696ca848eSSricharan R if (irqsr) {
23796ca848eSSricharan R size /= sizeof(__be32);
23896ca848eSSricharan R
23996ca848eSSricharan R for (i = 0; i < size; i++) {
24096ca848eSSricharan R of_property_read_u32_index(node,
24196ca848eSSricharan R "ti,irqs-reserved",
24296ca848eSSricharan R i, &entry);
243702f7e36SDan Carpenter if (entry >= max) {
24496ca848eSSricharan R pr_err("Invalid reserved entry\n");
245edb442deSNishanth Menon ret = -EINVAL;
2463c44d515SNishanth Menon goto err_irq_map;
24796ca848eSSricharan R }
2481d50d2ceSNishanth Menon cb->irq_map[entry] = IRQ_RESERVED;
24996ca848eSSricharan R }
25096ca848eSSricharan R }
25196ca848eSSricharan R
25264e0f8baSNishanth Menon /* Skip irqs hardwired to bypass the crossbar */
25364e0f8baSNishanth Menon irqsr = of_get_property(node, "ti,irqs-skip", &size);
25464e0f8baSNishanth Menon if (irqsr) {
25564e0f8baSNishanth Menon size /= sizeof(__be32);
25664e0f8baSNishanth Menon
25764e0f8baSNishanth Menon for (i = 0; i < size; i++) {
25864e0f8baSNishanth Menon of_property_read_u32_index(node,
25964e0f8baSNishanth Menon "ti,irqs-skip",
26064e0f8baSNishanth Menon i, &entry);
261702f7e36SDan Carpenter if (entry >= max) {
26264e0f8baSNishanth Menon pr_err("Invalid skip entry\n");
26364e0f8baSNishanth Menon ret = -EINVAL;
2643c44d515SNishanth Menon goto err_irq_map;
26564e0f8baSNishanth Menon }
26664e0f8baSNishanth Menon cb->irq_map[entry] = IRQ_SKIP;
26764e0f8baSNishanth Menon }
26864e0f8baSNishanth Menon }
26964e0f8baSNishanth Menon
27064e0f8baSNishanth Menon
2714dbf45e3SNishanth Menon cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
27296ca848eSSricharan R if (!cb->register_offsets)
2733c44d515SNishanth Menon goto err_irq_map;
27496ca848eSSricharan R
2754b9de5daSFranck Demathieu of_property_read_u32(node, "ti,reg-size", ®_size);
27696ca848eSSricharan R
2774b9de5daSFranck Demathieu switch (reg_size) {
27896ca848eSSricharan R case 1:
27996ca848eSSricharan R cb->write = crossbar_writeb;
28096ca848eSSricharan R break;
28196ca848eSSricharan R case 2:
28296ca848eSSricharan R cb->write = crossbar_writew;
28396ca848eSSricharan R break;
28496ca848eSSricharan R case 4:
28596ca848eSSricharan R cb->write = crossbar_writel;
28696ca848eSSricharan R break;
28796ca848eSSricharan R default:
28896ca848eSSricharan R pr_err("Invalid reg-size property\n");
289edb442deSNishanth Menon ret = -EINVAL;
2903c44d515SNishanth Menon goto err_reg_offset;
29196ca848eSSricharan R break;
29296ca848eSSricharan R }
29396ca848eSSricharan R
29496ca848eSSricharan R /*
29596ca848eSSricharan R * Register offsets are not linear because of the
29696ca848eSSricharan R * reserved irqs. so find and store the offsets once.
29796ca848eSSricharan R */
29896ca848eSSricharan R for (i = 0; i < max; i++) {
2991d50d2ceSNishanth Menon if (cb->irq_map[i] == IRQ_RESERVED)
30096ca848eSSricharan R continue;
30196ca848eSSricharan R
30296ca848eSSricharan R cb->register_offsets[i] = reserved;
3034b9de5daSFranck Demathieu reserved += reg_size;
30496ca848eSSricharan R }
30596ca848eSSricharan R
306a35057d1SNishanth Menon of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
307a35057d1SNishanth Menon /* Initialize the crossbar with safe map to start with */
308a35057d1SNishanth Menon for (i = 0; i < max; i++) {
309a35057d1SNishanth Menon if (cb->irq_map[i] == IRQ_RESERVED ||
310a35057d1SNishanth Menon cb->irq_map[i] == IRQ_SKIP)
311a35057d1SNishanth Menon continue;
312a35057d1SNishanth Menon
313a35057d1SNishanth Menon cb->write(i, cb->safe_map);
314a35057d1SNishanth Menon }
315a35057d1SNishanth Menon
316783d3186SMarc Zyngier raw_spin_lock_init(&cb->lock);
317783d3186SMarc Zyngier
31896ca848eSSricharan R return 0;
31996ca848eSSricharan R
3203c44d515SNishanth Menon err_reg_offset:
32196ca848eSSricharan R kfree(cb->register_offsets);
3223c44d515SNishanth Menon err_irq_map:
32396ca848eSSricharan R kfree(cb->irq_map);
3243c44d515SNishanth Menon err_base:
32596ca848eSSricharan R iounmap(cb->crossbar_base);
3263c44d515SNishanth Menon err_cb:
32796ca848eSSricharan R kfree(cb);
32899e37d0eSSricharan R
32999e37d0eSSricharan R cb = NULL;
330edb442deSNishanth Menon return ret;
33196ca848eSSricharan R }
33296ca848eSSricharan R
irqcrossbar_init(struct device_node * node,struct device_node * parent)333783d3186SMarc Zyngier static int __init irqcrossbar_init(struct device_node *node,
334783d3186SMarc Zyngier struct device_node *parent)
33596ca848eSSricharan R {
336783d3186SMarc Zyngier struct irq_domain *parent_domain, *domain;
337783d3186SMarc Zyngier int err;
33896ca848eSSricharan R
339783d3186SMarc Zyngier if (!parent) {
340e81f54c6SRob Herring pr_err("%pOF: no parent, giving up\n", node);
341783d3186SMarc Zyngier return -ENODEV;
342783d3186SMarc Zyngier }
343783d3186SMarc Zyngier
344783d3186SMarc Zyngier parent_domain = irq_find_host(parent);
345783d3186SMarc Zyngier if (!parent_domain) {
346e81f54c6SRob Herring pr_err("%pOF: unable to obtain parent domain\n", node);
347783d3186SMarc Zyngier return -ENXIO;
348783d3186SMarc Zyngier }
349783d3186SMarc Zyngier
350783d3186SMarc Zyngier err = crossbar_of_init(node);
351783d3186SMarc Zyngier if (err)
352783d3186SMarc Zyngier return err;
353783d3186SMarc Zyngier
354783d3186SMarc Zyngier domain = irq_domain_add_hierarchy(parent_domain, 0,
355783d3186SMarc Zyngier cb->max_crossbar_sources,
356783d3186SMarc Zyngier node, &crossbar_domain_ops,
357783d3186SMarc Zyngier NULL);
358783d3186SMarc Zyngier if (!domain) {
359e81f54c6SRob Herring pr_err("%pOF: failed to allocated domain\n", node);
360783d3186SMarc Zyngier return -ENOMEM;
361783d3186SMarc Zyngier }
362783d3186SMarc Zyngier
36396ca848eSSricharan R return 0;
36496ca848eSSricharan R }
365783d3186SMarc Zyngier
366783d3186SMarc Zyngier IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);
367