Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Texas Instruments' K3 Interrupt Router irqchip driver
4 *
5 * Copyright (C) 2018-2019 Texas Instruments Incorporated - https://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
7 */
8
9#include <linux/err.h>
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/io.h>
13#include <linux/irqchip.h>
14#include <linux/irqdomain.h>
15#include <linux/of.h>
16#include <linux/of_irq.h>
17#include <linux/platform_device.h>
18#include <linux/soc/ti/ti_sci_protocol.h>
19
20/**
21 * struct ti_sci_intr_irq_domain - Structure representing a TISCI based
22 * Interrupt Router IRQ domain.
23 * @sci: Pointer to TISCI handle
24 * @out_irqs: TISCI resource pointer representing INTR irqs.
25 * @dev: Struct device pointer.
26 * @ti_sci_id: TI-SCI device identifier
27 * @type: Specifies the trigger type supported by this Interrupt Router
28 */
29struct ti_sci_intr_irq_domain {
30 const struct ti_sci_handle *sci;
31 struct ti_sci_resource *out_irqs;
32 struct device *dev;
33 u32 ti_sci_id;
34 u32 type;
35};
36
37static struct irq_chip ti_sci_intr_irq_chip = {
38 .name = "INTR",
39 .irq_eoi = irq_chip_eoi_parent,
40 .irq_mask = irq_chip_mask_parent,
41 .irq_unmask = irq_chip_unmask_parent,
42 .irq_set_type = irq_chip_set_type_parent,
43 .irq_retrigger = irq_chip_retrigger_hierarchy,
44 .irq_set_affinity = irq_chip_set_affinity_parent,
45};
46
47/**
48 * ti_sci_intr_irq_domain_translate() - Retrieve hwirq and type from
49 * IRQ firmware specific handler.
50 * @domain: Pointer to IRQ domain
51 * @fwspec: Pointer to IRQ specific firmware structure
52 * @hwirq: IRQ number identified by hardware
53 * @type: IRQ type
54 *
55 * Return 0 if all went ok else appropriate error.
56 */
57static int ti_sci_intr_irq_domain_translate(struct irq_domain *domain,
58 struct irq_fwspec *fwspec,
59 unsigned long *hwirq,
60 unsigned int *type)
61{
62 struct ti_sci_intr_irq_domain *intr = domain->host_data;
63
64 if (fwspec->param_count != 1)
65 return -EINVAL;
66
67 *hwirq = fwspec->param[0];
68 *type = intr->type;
69
70 return 0;
71}
72
73/**
74 * ti_sci_intr_xlate_irq() - Translate hwirq to parent's hwirq.
75 * @intr: IRQ domain corresponding to Interrupt Router
76 * @irq: Hardware irq corresponding to the above irq domain
77 *
78 * Return parent irq number if translation is available else -ENOENT.
79 */
80static int ti_sci_intr_xlate_irq(struct ti_sci_intr_irq_domain *intr, u32 irq)
81{
82 struct device_node *np = dev_of_node(intr->dev);
83 u32 base, pbase, size, len;
84 const __be32 *range;
85
86 range = of_get_property(np, "ti,interrupt-ranges", &len);
87 if (!range)
88 return irq;
89
90 for (len /= sizeof(*range); len >= 3; len -= 3) {
91 base = be32_to_cpu(*range++);
92 pbase = be32_to_cpu(*range++);
93 size = be32_to_cpu(*range++);
94
95 if (base <= irq && irq < base + size)
96 return irq - base + pbase;
97 }
98
99 return -ENOENT;
100}
101
102/**
103 * ti_sci_intr_irq_domain_free() - Free the specified IRQs from the domain.
104 * @domain: Domain to which the irqs belong
105 * @virq: Linux virtual IRQ to be freed.
106 * @nr_irqs: Number of continuous irqs to be freed
107 */
108static void ti_sci_intr_irq_domain_free(struct irq_domain *domain,
109 unsigned int virq, unsigned int nr_irqs)
110{
111 struct ti_sci_intr_irq_domain *intr = domain->host_data;
112 struct irq_data *data;
113 int out_irq;
114
115 data = irq_domain_get_irq_data(domain, virq);
116 out_irq = (uintptr_t)data->chip_data;
117
118 intr->sci->ops.rm_irq_ops.free_irq(intr->sci,
119 intr->ti_sci_id, data->hwirq,
120 intr->ti_sci_id, out_irq);
121 ti_sci_release_resource(intr->out_irqs, out_irq);
122 irq_domain_free_irqs_parent(domain, virq, 1);
123 irq_domain_reset_irq_data(data);
124}
125
126/**
127 * ti_sci_intr_alloc_parent_irq() - Allocate parent IRQ
128 * @domain: Pointer to the interrupt router IRQ domain
129 * @virq: Corresponding Linux virtual IRQ number
130 * @hwirq: Corresponding hwirq for the IRQ within this IRQ domain
131 *
132 * Returns intr output irq if all went well else appropriate error pointer.
133 */
134static int ti_sci_intr_alloc_parent_irq(struct irq_domain *domain,
135 unsigned int virq, u32 hwirq)
136{
137 struct ti_sci_intr_irq_domain *intr = domain->host_data;
138 struct device_node *parent_node;
139 struct irq_fwspec fwspec;
140 int p_hwirq, err = 0;
141 u16 out_irq;
142
143 out_irq = ti_sci_get_free_resource(intr->out_irqs);
144 if (out_irq == TI_SCI_RESOURCE_NULL)
145 return -EINVAL;
146
147 p_hwirq = ti_sci_intr_xlate_irq(intr, out_irq);
148 if (p_hwirq < 0)
149 goto err_irqs;
150
151 parent_node = of_irq_find_parent(dev_of_node(intr->dev));
152 fwspec.fwnode = of_node_to_fwnode(parent_node);
153
154 if (of_device_is_compatible(parent_node, "arm,gic-v3")) {
155 /* Parent is GIC */
156 fwspec.param_count = 3;
157 fwspec.param[0] = 0; /* SPI */
158 fwspec.param[1] = p_hwirq - 32; /* SPI offset */
159 fwspec.param[2] = intr->type;
160 } else {
161 /* Parent is Interrupt Router */
162 fwspec.param_count = 1;
163 fwspec.param[0] = p_hwirq;
164 }
165
166 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
167 if (err)
168 goto err_irqs;
169
170 err = intr->sci->ops.rm_irq_ops.set_irq(intr->sci,
171 intr->ti_sci_id, hwirq,
172 intr->ti_sci_id, out_irq);
173 if (err)
174 goto err_msg;
175
176 return out_irq;
177
178err_msg:
179 irq_domain_free_irqs_parent(domain, virq, 1);
180err_irqs:
181 ti_sci_release_resource(intr->out_irqs, out_irq);
182 return err;
183}
184
185/**
186 * ti_sci_intr_irq_domain_alloc() - Allocate Interrupt router IRQs
187 * @domain: Point to the interrupt router IRQ domain
188 * @virq: Corresponding Linux virtual IRQ number
189 * @nr_irqs: Continuous irqs to be allocated
190 * @data: Pointer to firmware specifier
191 *
192 * Return 0 if all went well else appropriate error value.
193 */
194static int ti_sci_intr_irq_domain_alloc(struct irq_domain *domain,
195 unsigned int virq, unsigned int nr_irqs,
196 void *data)
197{
198 struct irq_fwspec *fwspec = data;
199 unsigned long hwirq;
200 unsigned int flags;
201 int err, out_irq;
202
203 err = ti_sci_intr_irq_domain_translate(domain, fwspec, &hwirq, &flags);
204 if (err)
205 return err;
206
207 out_irq = ti_sci_intr_alloc_parent_irq(domain, virq, hwirq);
208 if (out_irq < 0)
209 return out_irq;
210
211 irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
212 &ti_sci_intr_irq_chip,
213 (void *)(uintptr_t)out_irq);
214
215 return 0;
216}
217
218static const struct irq_domain_ops ti_sci_intr_irq_domain_ops = {
219 .free = ti_sci_intr_irq_domain_free,
220 .alloc = ti_sci_intr_irq_domain_alloc,
221 .translate = ti_sci_intr_irq_domain_translate,
222};
223
224static int ti_sci_intr_irq_domain_probe(struct platform_device *pdev)
225{
226 struct irq_domain *parent_domain, *domain;
227 struct ti_sci_intr_irq_domain *intr;
228 struct device_node *parent_node;
229 struct device *dev = &pdev->dev;
230 int ret;
231
232 parent_node = of_irq_find_parent(dev_of_node(dev));
233 if (!parent_node) {
234 dev_err(dev, "Failed to get IRQ parent node\n");
235 return -ENODEV;
236 }
237
238 parent_domain = irq_find_host(parent_node);
239 of_node_put(parent_node);
240 if (!parent_domain) {
241 dev_err(dev, "Failed to find IRQ parent domain\n");
242 return -ENODEV;
243 }
244
245 intr = devm_kzalloc(dev, sizeof(*intr), GFP_KERNEL);
246 if (!intr)
247 return -ENOMEM;
248
249 intr->dev = dev;
250 ret = of_property_read_u32(dev_of_node(dev), "ti,intr-trigger-type",
251 &intr->type);
252 if (ret) {
253 dev_err(dev, "missing ti,intr-trigger-type property\n");
254 return -EINVAL;
255 }
256
257 intr->sci = devm_ti_sci_get_by_phandle(dev, "ti,sci");
258 if (IS_ERR(intr->sci))
259 return dev_err_probe(dev, PTR_ERR(intr->sci),
260 "ti,sci read fail\n");
261
262 ret = of_property_read_u32(dev_of_node(dev), "ti,sci-dev-id",
263 &intr->ti_sci_id);
264 if (ret) {
265 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
266 return -EINVAL;
267 }
268
269 intr->out_irqs = devm_ti_sci_get_resource(intr->sci, dev,
270 intr->ti_sci_id,
271 TI_SCI_RESASG_SUBTYPE_IR_OUTPUT);
272 if (IS_ERR(intr->out_irqs)) {
273 dev_err(dev, "Destination irq resource allocation failed\n");
274 return PTR_ERR(intr->out_irqs);
275 }
276
277 domain = irq_domain_add_hierarchy(parent_domain, 0, 0, dev_of_node(dev),
278 &ti_sci_intr_irq_domain_ops, intr);
279 if (!domain) {
280 dev_err(dev, "Failed to allocate IRQ domain\n");
281 return -ENOMEM;
282 }
283
284 dev_info(dev, "Interrupt Router %d domain created\n", intr->ti_sci_id);
285
286 return 0;
287}
288
289static const struct of_device_id ti_sci_intr_irq_domain_of_match[] = {
290 { .compatible = "ti,sci-intr", },
291 { /* sentinel */ },
292};
293MODULE_DEVICE_TABLE(of, ti_sci_intr_irq_domain_of_match);
294
295static struct platform_driver ti_sci_intr_irq_domain_driver = {
296 .probe = ti_sci_intr_irq_domain_probe,
297 .driver = {
298 .name = "ti-sci-intr",
299 .of_match_table = ti_sci_intr_irq_domain_of_match,
300 },
301};
302module_platform_driver(ti_sci_intr_irq_domain_driver);
303
304MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ticom>");
305MODULE_DESCRIPTION("K3 Interrupt Router driver over TI SCI protocol");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Texas Instruments' K3 Interrupt Router irqchip driver
4 *
5 * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
7 */
8
9#include <linux/err.h>
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/io.h>
13#include <linux/irqchip.h>
14#include <linux/irqdomain.h>
15#include <linux/of_platform.h>
16#include <linux/of_address.h>
17#include <linux/of_irq.h>
18#include <linux/soc/ti/ti_sci_protocol.h>
19
20#define TI_SCI_DEV_ID_MASK 0xffff
21#define TI_SCI_DEV_ID_SHIFT 16
22#define TI_SCI_IRQ_ID_MASK 0xffff
23#define TI_SCI_IRQ_ID_SHIFT 0
24#define HWIRQ_TO_DEVID(hwirq) (((hwirq) >> (TI_SCI_DEV_ID_SHIFT)) & \
25 (TI_SCI_DEV_ID_MASK))
26#define HWIRQ_TO_IRQID(hwirq) ((hwirq) & (TI_SCI_IRQ_ID_MASK))
27#define TO_HWIRQ(dev, index) ((((dev) & TI_SCI_DEV_ID_MASK) << \
28 TI_SCI_DEV_ID_SHIFT) | \
29 ((index) & TI_SCI_IRQ_ID_MASK))
30
31/**
32 * struct ti_sci_intr_irq_domain - Structure representing a TISCI based
33 * Interrupt Router IRQ domain.
34 * @sci: Pointer to TISCI handle
35 * @dst_irq: TISCI resource pointer representing GIC irq controller.
36 * @dst_id: TISCI device ID of the GIC irq controller.
37 * @type: Specifies the trigger type supported by this Interrupt Router
38 */
39struct ti_sci_intr_irq_domain {
40 const struct ti_sci_handle *sci;
41 struct ti_sci_resource *dst_irq;
42 u32 dst_id;
43 u32 type;
44};
45
46static struct irq_chip ti_sci_intr_irq_chip = {
47 .name = "INTR",
48 .irq_eoi = irq_chip_eoi_parent,
49 .irq_mask = irq_chip_mask_parent,
50 .irq_unmask = irq_chip_unmask_parent,
51 .irq_set_type = irq_chip_set_type_parent,
52 .irq_retrigger = irq_chip_retrigger_hierarchy,
53 .irq_set_affinity = irq_chip_set_affinity_parent,
54};
55
56/**
57 * ti_sci_intr_irq_domain_translate() - Retrieve hwirq and type from
58 * IRQ firmware specific handler.
59 * @domain: Pointer to IRQ domain
60 * @fwspec: Pointer to IRQ specific firmware structure
61 * @hwirq: IRQ number identified by hardware
62 * @type: IRQ type
63 *
64 * Return 0 if all went ok else appropriate error.
65 */
66static int ti_sci_intr_irq_domain_translate(struct irq_domain *domain,
67 struct irq_fwspec *fwspec,
68 unsigned long *hwirq,
69 unsigned int *type)
70{
71 struct ti_sci_intr_irq_domain *intr = domain->host_data;
72
73 if (fwspec->param_count != 2)
74 return -EINVAL;
75
76 *hwirq = TO_HWIRQ(fwspec->param[0], fwspec->param[1]);
77 *type = intr->type;
78
79 return 0;
80}
81
82/**
83 * ti_sci_intr_irq_domain_free() - Free the specified IRQs from the domain.
84 * @domain: Domain to which the irqs belong
85 * @virq: Linux virtual IRQ to be freed.
86 * @nr_irqs: Number of continuous irqs to be freed
87 */
88static void ti_sci_intr_irq_domain_free(struct irq_domain *domain,
89 unsigned int virq, unsigned int nr_irqs)
90{
91 struct ti_sci_intr_irq_domain *intr = domain->host_data;
92 struct irq_data *data, *parent_data;
93 u16 dev_id, irq_index;
94
95 parent_data = irq_domain_get_irq_data(domain->parent, virq);
96 data = irq_domain_get_irq_data(domain, virq);
97 irq_index = HWIRQ_TO_IRQID(data->hwirq);
98 dev_id = HWIRQ_TO_DEVID(data->hwirq);
99
100 intr->sci->ops.rm_irq_ops.free_irq(intr->sci, dev_id, irq_index,
101 intr->dst_id, parent_data->hwirq);
102 ti_sci_release_resource(intr->dst_irq, parent_data->hwirq);
103 irq_domain_free_irqs_parent(domain, virq, 1);
104 irq_domain_reset_irq_data(data);
105}
106
107/**
108 * ti_sci_intr_alloc_gic_irq() - Allocate GIC specific IRQ
109 * @domain: Pointer to the interrupt router IRQ domain
110 * @virq: Corresponding Linux virtual IRQ number
111 * @hwirq: Corresponding hwirq for the IRQ within this IRQ domain
112 *
113 * Returns 0 if all went well else appropriate error pointer.
114 */
115static int ti_sci_intr_alloc_gic_irq(struct irq_domain *domain,
116 unsigned int virq, u32 hwirq)
117{
118 struct ti_sci_intr_irq_domain *intr = domain->host_data;
119 struct irq_fwspec fwspec;
120 u16 dev_id, irq_index;
121 u16 dst_irq;
122 int err;
123
124 dev_id = HWIRQ_TO_DEVID(hwirq);
125 irq_index = HWIRQ_TO_IRQID(hwirq);
126
127 dst_irq = ti_sci_get_free_resource(intr->dst_irq);
128 if (dst_irq == TI_SCI_RESOURCE_NULL)
129 return -EINVAL;
130
131 fwspec.fwnode = domain->parent->fwnode;
132 fwspec.param_count = 3;
133 fwspec.param[0] = 0; /* SPI */
134 fwspec.param[1] = dst_irq - 32; /* SPI offset */
135 fwspec.param[2] = intr->type;
136
137 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
138 if (err)
139 goto err_irqs;
140
141 err = intr->sci->ops.rm_irq_ops.set_irq(intr->sci, dev_id, irq_index,
142 intr->dst_id, dst_irq);
143 if (err)
144 goto err_msg;
145
146 return 0;
147
148err_msg:
149 irq_domain_free_irqs_parent(domain, virq, 1);
150err_irqs:
151 ti_sci_release_resource(intr->dst_irq, dst_irq);
152 return err;
153}
154
155/**
156 * ti_sci_intr_irq_domain_alloc() - Allocate Interrupt router IRQs
157 * @domain: Point to the interrupt router IRQ domain
158 * @virq: Corresponding Linux virtual IRQ number
159 * @nr_irqs: Continuous irqs to be allocated
160 * @data: Pointer to firmware specifier
161 *
162 * Return 0 if all went well else appropriate error value.
163 */
164static int ti_sci_intr_irq_domain_alloc(struct irq_domain *domain,
165 unsigned int virq, unsigned int nr_irqs,
166 void *data)
167{
168 struct irq_fwspec *fwspec = data;
169 unsigned long hwirq;
170 unsigned int flags;
171 int err;
172
173 err = ti_sci_intr_irq_domain_translate(domain, fwspec, &hwirq, &flags);
174 if (err)
175 return err;
176
177 err = ti_sci_intr_alloc_gic_irq(domain, virq, hwirq);
178 if (err)
179 return err;
180
181 irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
182 &ti_sci_intr_irq_chip, NULL);
183
184 return 0;
185}
186
187static const struct irq_domain_ops ti_sci_intr_irq_domain_ops = {
188 .free = ti_sci_intr_irq_domain_free,
189 .alloc = ti_sci_intr_irq_domain_alloc,
190 .translate = ti_sci_intr_irq_domain_translate,
191};
192
193static int ti_sci_intr_irq_domain_probe(struct platform_device *pdev)
194{
195 struct irq_domain *parent_domain, *domain;
196 struct ti_sci_intr_irq_domain *intr;
197 struct device_node *parent_node;
198 struct device *dev = &pdev->dev;
199 int ret;
200
201 parent_node = of_irq_find_parent(dev_of_node(dev));
202 if (!parent_node) {
203 dev_err(dev, "Failed to get IRQ parent node\n");
204 return -ENODEV;
205 }
206
207 parent_domain = irq_find_host(parent_node);
208 if (!parent_domain) {
209 dev_err(dev, "Failed to find IRQ parent domain\n");
210 return -ENODEV;
211 }
212
213 intr = devm_kzalloc(dev, sizeof(*intr), GFP_KERNEL);
214 if (!intr)
215 return -ENOMEM;
216
217 ret = of_property_read_u32(dev_of_node(dev), "ti,intr-trigger-type",
218 &intr->type);
219 if (ret) {
220 dev_err(dev, "missing ti,intr-trigger-type property\n");
221 return -EINVAL;
222 }
223
224 intr->sci = devm_ti_sci_get_by_phandle(dev, "ti,sci");
225 if (IS_ERR(intr->sci)) {
226 ret = PTR_ERR(intr->sci);
227 if (ret != -EPROBE_DEFER)
228 dev_err(dev, "ti,sci read fail %d\n", ret);
229 intr->sci = NULL;
230 return ret;
231 }
232
233 ret = of_property_read_u32(dev_of_node(dev), "ti,sci-dst-id",
234 &intr->dst_id);
235 if (ret) {
236 dev_err(dev, "missing 'ti,sci-dst-id' property\n");
237 return -EINVAL;
238 }
239
240 intr->dst_irq = devm_ti_sci_get_of_resource(intr->sci, dev,
241 intr->dst_id,
242 "ti,sci-rm-range-girq");
243 if (IS_ERR(intr->dst_irq)) {
244 dev_err(dev, "Destination irq resource allocation failed\n");
245 return PTR_ERR(intr->dst_irq);
246 }
247
248 domain = irq_domain_add_hierarchy(parent_domain, 0, 0, dev_of_node(dev),
249 &ti_sci_intr_irq_domain_ops, intr);
250 if (!domain) {
251 dev_err(dev, "Failed to allocate IRQ domain\n");
252 return -ENOMEM;
253 }
254
255 return 0;
256}
257
258static const struct of_device_id ti_sci_intr_irq_domain_of_match[] = {
259 { .compatible = "ti,sci-intr", },
260 { /* sentinel */ },
261};
262MODULE_DEVICE_TABLE(of, ti_sci_intr_irq_domain_of_match);
263
264static struct platform_driver ti_sci_intr_irq_domain_driver = {
265 .probe = ti_sci_intr_irq_domain_probe,
266 .driver = {
267 .name = "ti-sci-intr",
268 .of_match_table = ti_sci_intr_irq_domain_of_match,
269 },
270};
271module_platform_driver(ti_sci_intr_irq_domain_driver);
272
273MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ticom>");
274MODULE_DESCRIPTION("K3 Interrupt Router driver over TI SCI protocol");
275MODULE_LICENSE("GPL v2");