1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments' K3 Interrupt Aggregator irqchip driver
5 * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
11 #include <linux/irqchip.h>
12 #include <linux/irqdomain.h>
13 #include <linux/interrupt.h>
14 #include <linux/msi.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/irqchip/chained_irq.h>
21 #include <linux/soc/ti/ti_sci_inta_msi.h>
22 #include <linux/soc/ti/ti_sci_protocol.h>
23 #include <asm-generic/msi.h>
25 #define TI_SCI_DEV_ID_MASK 0xffff
26 #define TI_SCI_DEV_ID_SHIFT 16
27 #define TI_SCI_IRQ_ID_MASK 0xffff
28 #define TI_SCI_IRQ_ID_SHIFT 0
29 #define HWIRQ_TO_DEVID(hwirq) (((hwirq) >> (TI_SCI_DEV_ID_SHIFT)) & \
31 #define HWIRQ_TO_IRQID(hwirq) ((hwirq) & (TI_SCI_IRQ_ID_MASK))
32 #define TO_HWIRQ(dev, index) ((((dev) & TI_SCI_DEV_ID_MASK) << \
33 TI_SCI_DEV_ID_SHIFT) | \
34 ((index) & TI_SCI_IRQ_ID_MASK))
36 #define MAX_EVENTS_PER_VINT 64
37 #define VINT_ENABLE_SET_OFFSET 0x0
38 #define VINT_ENABLE_CLR_OFFSET 0x8
39 #define VINT_STATUS_OFFSET 0x18
42 * struct ti_sci_inta_event_desc - Description of an event coming to
43 * Interrupt Aggregator. This serves
44 * as a mapping table for global event,
46 * @global_event: Global event number corresponding to this event
47 * @hwirq: Hwirq of the incoming interrupt
48 * @vint_bit: Corresponding vint bit to which this event is attached.
50 struct ti_sci_inta_event_desc
{
57 * struct ti_sci_inta_vint_desc - Description of a virtual interrupt coming out
58 * of Interrupt Aggregator.
59 * @domain: Pointer to IRQ domain to which this vint belongs.
60 * @list: List entry for the vint list
61 * @event_map: Bitmap to manage the allocation of events to vint.
62 * @events: Array of event descriptors assigned to this vint.
63 * @parent_virq: Linux IRQ number that gets attached to parent
64 * @vint_id: TISCI vint ID
66 struct ti_sci_inta_vint_desc
{
67 struct irq_domain
*domain
;
68 struct list_head list
;
69 DECLARE_BITMAP(event_map
, MAX_EVENTS_PER_VINT
);
70 struct ti_sci_inta_event_desc events
[MAX_EVENTS_PER_VINT
];
71 unsigned int parent_virq
;
76 * struct ti_sci_inta_irq_domain - Structure representing a TISCI based
77 * Interrupt Aggregator IRQ domain.
78 * @sci: Pointer to TISCI handle
79 * @vint: TISCI resource pointer representing IA inerrupts.
80 * @global_event: TISCI resource pointer representing global events.
81 * @vint_list: List of the vints active in the system
82 * @vint_mutex: Mutex to protect vint_list
83 * @base: Base address of the memory mapped IO registers
84 * @pdev: Pointer to platform device.
86 struct ti_sci_inta_irq_domain
{
87 const struct ti_sci_handle
*sci
;
88 struct ti_sci_resource
*vint
;
89 struct ti_sci_resource
*global_event
;
90 struct list_head vint_list
;
91 /* Mutex to protect vint list */
92 struct mutex vint_mutex
;
94 struct platform_device
*pdev
;
97 #define to_vint_desc(e, i) container_of(e, struct ti_sci_inta_vint_desc, \
101 * ti_sci_inta_irq_handler() - Chained IRQ handler for the vint irqs
102 * @desc: Pointer to irq_desc corresponding to the irq
104 static void ti_sci_inta_irq_handler(struct irq_desc
*desc
)
106 struct ti_sci_inta_vint_desc
*vint_desc
;
107 struct ti_sci_inta_irq_domain
*inta
;
108 struct irq_domain
*domain
;
109 unsigned int virq
, bit
;
112 vint_desc
= irq_desc_get_handler_data(desc
);
113 domain
= vint_desc
->domain
;
114 inta
= domain
->host_data
;
116 chained_irq_enter(irq_desc_get_chip(desc
), desc
);
118 val
= readq_relaxed(inta
->base
+ vint_desc
->vint_id
* 0x1000 +
121 for_each_set_bit(bit
, &val
, MAX_EVENTS_PER_VINT
) {
122 virq
= irq_find_mapping(domain
, vint_desc
->events
[bit
].hwirq
);
124 generic_handle_irq(virq
);
127 chained_irq_exit(irq_desc_get_chip(desc
), desc
);
131 * ti_sci_inta_alloc_parent_irq() - Allocate parent irq to Interrupt aggregator
132 * @domain: IRQ domain corresponding to Interrupt Aggregator
134 * Return 0 if all went well else corresponding error value.
136 static struct ti_sci_inta_vint_desc
*ti_sci_inta_alloc_parent_irq(struct irq_domain
*domain
)
138 struct ti_sci_inta_irq_domain
*inta
= domain
->host_data
;
139 struct ti_sci_inta_vint_desc
*vint_desc
;
140 struct irq_fwspec parent_fwspec
;
141 unsigned int parent_virq
;
144 vint_id
= ti_sci_get_free_resource(inta
->vint
);
145 if (vint_id
== TI_SCI_RESOURCE_NULL
)
146 return ERR_PTR(-EINVAL
);
148 vint_desc
= kzalloc(sizeof(*vint_desc
), GFP_KERNEL
);
150 return ERR_PTR(-ENOMEM
);
152 vint_desc
->domain
= domain
;
153 vint_desc
->vint_id
= vint_id
;
154 INIT_LIST_HEAD(&vint_desc
->list
);
156 parent_fwspec
.fwnode
= of_node_to_fwnode(of_irq_find_parent(dev_of_node(&inta
->pdev
->dev
)));
157 parent_fwspec
.param_count
= 2;
158 parent_fwspec
.param
[0] = inta
->pdev
->id
;
159 parent_fwspec
.param
[1] = vint_desc
->vint_id
;
161 parent_virq
= irq_create_fwspec_mapping(&parent_fwspec
);
162 if (parent_virq
== 0) {
164 return ERR_PTR(-EINVAL
);
166 vint_desc
->parent_virq
= parent_virq
;
168 list_add_tail(&vint_desc
->list
, &inta
->vint_list
);
169 irq_set_chained_handler_and_data(vint_desc
->parent_virq
,
170 ti_sci_inta_irq_handler
, vint_desc
);
176 * ti_sci_inta_alloc_event() - Attach an event to a IA vint.
177 * @vint_desc: Pointer to vint_desc to which the event gets attached
178 * @free_bit: Bit inside vint to which event gets attached
179 * @hwirq: hwirq of the input event
181 * Return event_desc pointer if all went ok else appropriate error value.
183 static struct ti_sci_inta_event_desc
*ti_sci_inta_alloc_event(struct ti_sci_inta_vint_desc
*vint_desc
,
187 struct ti_sci_inta_irq_domain
*inta
= vint_desc
->domain
->host_data
;
188 struct ti_sci_inta_event_desc
*event_desc
;
189 u16 dev_id
, dev_index
;
192 dev_id
= HWIRQ_TO_DEVID(hwirq
);
193 dev_index
= HWIRQ_TO_IRQID(hwirq
);
195 event_desc
= &vint_desc
->events
[free_bit
];
196 event_desc
->hwirq
= hwirq
;
197 event_desc
->vint_bit
= free_bit
;
198 event_desc
->global_event
= ti_sci_get_free_resource(inta
->global_event
);
199 if (event_desc
->global_event
== TI_SCI_RESOURCE_NULL
)
200 return ERR_PTR(-EINVAL
);
202 err
= inta
->sci
->ops
.rm_irq_ops
.set_event_map(inta
->sci
,
206 event_desc
->global_event
,
209 goto free_global_event
;
213 ti_sci_release_resource(inta
->global_event
, event_desc
->global_event
);
218 * ti_sci_inta_alloc_irq() - Allocate an irq within INTA domain
219 * @domain: irq_domain pointer corresponding to INTA
220 * @hwirq: hwirq of the input event
222 * Note: Allocation happens in the following manner:
223 * - Find a free bit available in any of the vints available in the list.
224 * - If not found, allocate a vint from the vint pool
225 * - Attach the free bit to input hwirq.
226 * Return event_desc if all went ok else appropriate error value.
228 static struct ti_sci_inta_event_desc
*ti_sci_inta_alloc_irq(struct irq_domain
*domain
,
231 struct ti_sci_inta_irq_domain
*inta
= domain
->host_data
;
232 struct ti_sci_inta_vint_desc
*vint_desc
= NULL
;
233 struct ti_sci_inta_event_desc
*event_desc
;
236 mutex_lock(&inta
->vint_mutex
);
237 list_for_each_entry(vint_desc
, &inta
->vint_list
, list
) {
238 free_bit
= find_first_zero_bit(vint_desc
->event_map
,
239 MAX_EVENTS_PER_VINT
);
240 if (free_bit
!= MAX_EVENTS_PER_VINT
) {
241 set_bit(free_bit
, vint_desc
->event_map
);
246 /* No free bits available. Allocate a new vint */
247 vint_desc
= ti_sci_inta_alloc_parent_irq(domain
);
248 if (IS_ERR(vint_desc
)) {
249 event_desc
= ERR_CAST(vint_desc
);
253 free_bit
= find_first_zero_bit(vint_desc
->event_map
,
254 MAX_EVENTS_PER_VINT
);
255 set_bit(free_bit
, vint_desc
->event_map
);
258 event_desc
= ti_sci_inta_alloc_event(vint_desc
, free_bit
, hwirq
);
259 if (IS_ERR(event_desc
))
260 clear_bit(free_bit
, vint_desc
->event_map
);
263 mutex_unlock(&inta
->vint_mutex
);
268 * ti_sci_inta_free_parent_irq() - Free a parent irq to INTA
269 * @inta: Pointer to inta domain.
270 * @vint_desc: Pointer to vint_desc that needs to be freed.
272 static void ti_sci_inta_free_parent_irq(struct ti_sci_inta_irq_domain
*inta
,
273 struct ti_sci_inta_vint_desc
*vint_desc
)
275 if (find_first_bit(vint_desc
->event_map
, MAX_EVENTS_PER_VINT
) == MAX_EVENTS_PER_VINT
) {
276 list_del(&vint_desc
->list
);
277 ti_sci_release_resource(inta
->vint
, vint_desc
->vint_id
);
278 irq_dispose_mapping(vint_desc
->parent_virq
);
284 * ti_sci_inta_free_irq() - Free an IRQ within INTA domain
285 * @event_desc: Pointer to event_desc that needs to be freed.
286 * @hwirq: Hwirq number within INTA domain that needs to be freed
288 static void ti_sci_inta_free_irq(struct ti_sci_inta_event_desc
*event_desc
,
291 struct ti_sci_inta_vint_desc
*vint_desc
;
292 struct ti_sci_inta_irq_domain
*inta
;
294 vint_desc
= to_vint_desc(event_desc
, event_desc
->vint_bit
);
295 inta
= vint_desc
->domain
->host_data
;
297 mutex_lock(&inta
->vint_mutex
);
298 inta
->sci
->ops
.rm_irq_ops
.free_event_map(inta
->sci
,
299 HWIRQ_TO_DEVID(hwirq
),
300 HWIRQ_TO_IRQID(hwirq
),
303 event_desc
->global_event
,
304 event_desc
->vint_bit
);
306 clear_bit(event_desc
->vint_bit
, vint_desc
->event_map
);
307 ti_sci_release_resource(inta
->global_event
, event_desc
->global_event
);
308 event_desc
->global_event
= TI_SCI_RESOURCE_NULL
;
309 event_desc
->hwirq
= 0;
311 ti_sci_inta_free_parent_irq(inta
, vint_desc
);
312 mutex_unlock(&inta
->vint_mutex
);
316 * ti_sci_inta_request_resources() - Allocate resources for input irq
317 * @data: Pointer to corresponding irq_data
319 * Note: This is the core api where the actual allocation happens for input
320 * hwirq. This allocation involves creating a parent irq for vint.
321 * If this is done in irq_domain_ops.alloc() then a deadlock is reached
322 * for allocation. So this allocation is being done in request_resources()
324 * Return: 0 if all went well else corresponding error.
326 static int ti_sci_inta_request_resources(struct irq_data
*data
)
328 struct ti_sci_inta_event_desc
*event_desc
;
330 event_desc
= ti_sci_inta_alloc_irq(data
->domain
, data
->hwirq
);
331 if (IS_ERR(event_desc
))
332 return PTR_ERR(event_desc
);
334 data
->chip_data
= event_desc
;
340 * ti_sci_inta_release_resources - Release resources for input irq
341 * @data: Pointer to corresponding irq_data
343 * Note: Corresponding to request_resources(), all the unmapping and deletion
344 * of parent vint irqs happens in this api.
346 static void ti_sci_inta_release_resources(struct irq_data
*data
)
348 struct ti_sci_inta_event_desc
*event_desc
;
350 event_desc
= irq_data_get_irq_chip_data(data
);
351 ti_sci_inta_free_irq(event_desc
, data
->hwirq
);
355 * ti_sci_inta_manage_event() - Control the event based on the offset
356 * @data: Pointer to corresponding irq_data
357 * @offset: register offset using which event is controlled.
359 static void ti_sci_inta_manage_event(struct irq_data
*data
, u32 offset
)
361 struct ti_sci_inta_event_desc
*event_desc
;
362 struct ti_sci_inta_vint_desc
*vint_desc
;
363 struct ti_sci_inta_irq_domain
*inta
;
365 event_desc
= irq_data_get_irq_chip_data(data
);
366 vint_desc
= to_vint_desc(event_desc
, event_desc
->vint_bit
);
367 inta
= data
->domain
->host_data
;
369 writeq_relaxed(BIT(event_desc
->vint_bit
),
370 inta
->base
+ vint_desc
->vint_id
* 0x1000 + offset
);
374 * ti_sci_inta_mask_irq() - Mask an event
375 * @data: Pointer to corresponding irq_data
377 static void ti_sci_inta_mask_irq(struct irq_data
*data
)
379 ti_sci_inta_manage_event(data
, VINT_ENABLE_CLR_OFFSET
);
383 * ti_sci_inta_unmask_irq() - Unmask an event
384 * @data: Pointer to corresponding irq_data
386 static void ti_sci_inta_unmask_irq(struct irq_data
*data
)
388 ti_sci_inta_manage_event(data
, VINT_ENABLE_SET_OFFSET
);
392 * ti_sci_inta_ack_irq() - Ack an event
393 * @data: Pointer to corresponding irq_data
395 static void ti_sci_inta_ack_irq(struct irq_data
*data
)
398 * Do not clear the event if hardware is capable of sending
401 if (irqd_get_trigger_type(data
) != IRQF_TRIGGER_HIGH
)
402 ti_sci_inta_manage_event(data
, VINT_STATUS_OFFSET
);
405 static int ti_sci_inta_set_affinity(struct irq_data
*d
,
406 const struct cpumask
*mask_val
, bool force
)
412 * ti_sci_inta_set_type() - Update the trigger type of the irq.
413 * @data: Pointer to corresponding irq_data
414 * @type: Trigger type as specified by user
416 * Note: This updates the handle_irq callback for level msi.
418 * Return 0 if all went well else appropriate error.
420 static int ti_sci_inta_set_type(struct irq_data
*data
, unsigned int type
)
423 * .alloc default sets handle_edge_irq. But if the user specifies
424 * that IRQ is level MSI, then update the handle to handle_level_irq
426 switch (type
& IRQ_TYPE_SENSE_MASK
) {
427 case IRQF_TRIGGER_HIGH
:
428 irq_set_handler_locked(data
, handle_level_irq
);
430 case IRQF_TRIGGER_RISING
:
439 static struct irq_chip ti_sci_inta_irq_chip
= {
441 .irq_ack
= ti_sci_inta_ack_irq
,
442 .irq_mask
= ti_sci_inta_mask_irq
,
443 .irq_set_type
= ti_sci_inta_set_type
,
444 .irq_unmask
= ti_sci_inta_unmask_irq
,
445 .irq_set_affinity
= ti_sci_inta_set_affinity
,
446 .irq_request_resources
= ti_sci_inta_request_resources
,
447 .irq_release_resources
= ti_sci_inta_release_resources
,
451 * ti_sci_inta_irq_domain_free() - Free an IRQ from the IRQ domain
452 * @domain: Domain to which the irqs belong
453 * @virq: base linux virtual IRQ to be freed.
454 * @nr_irqs: Number of continuous irqs to be freed
456 static void ti_sci_inta_irq_domain_free(struct irq_domain
*domain
,
457 unsigned int virq
, unsigned int nr_irqs
)
459 struct irq_data
*data
= irq_domain_get_irq_data(domain
, virq
);
461 irq_domain_reset_irq_data(data
);
465 * ti_sci_inta_irq_domain_alloc() - Allocate Interrupt aggregator IRQs
466 * @domain: Point to the interrupt aggregator IRQ domain
467 * @virq: Corresponding Linux virtual IRQ number
468 * @nr_irqs: Continuous irqs to be allocated
469 * @data: Pointer to firmware specifier
471 * No actual allocation happens here.
473 * Return 0 if all went well else appropriate error value.
475 static int ti_sci_inta_irq_domain_alloc(struct irq_domain
*domain
,
476 unsigned int virq
, unsigned int nr_irqs
,
479 msi_alloc_info_t
*arg
= data
;
481 irq_domain_set_info(domain
, virq
, arg
->hwirq
, &ti_sci_inta_irq_chip
,
482 NULL
, handle_edge_irq
, NULL
, NULL
);
487 static const struct irq_domain_ops ti_sci_inta_irq_domain_ops
= {
488 .free
= ti_sci_inta_irq_domain_free
,
489 .alloc
= ti_sci_inta_irq_domain_alloc
,
492 static struct irq_chip ti_sci_inta_msi_irq_chip
= {
494 .flags
= IRQCHIP_SUPPORTS_LEVEL_MSI
,
497 static void ti_sci_inta_msi_set_desc(msi_alloc_info_t
*arg
,
498 struct msi_desc
*desc
)
500 struct platform_device
*pdev
= to_platform_device(desc
->dev
);
503 arg
->hwirq
= TO_HWIRQ(pdev
->id
, desc
->inta
.dev_index
);
506 static struct msi_domain_ops ti_sci_inta_msi_ops
= {
507 .set_desc
= ti_sci_inta_msi_set_desc
,
510 static struct msi_domain_info ti_sci_inta_msi_domain_info
= {
511 .flags
= (MSI_FLAG_USE_DEF_DOM_OPS
| MSI_FLAG_USE_DEF_CHIP_OPS
|
512 MSI_FLAG_LEVEL_CAPABLE
),
513 .ops
= &ti_sci_inta_msi_ops
,
514 .chip
= &ti_sci_inta_msi_irq_chip
,
517 static int ti_sci_inta_irq_domain_probe(struct platform_device
*pdev
)
519 struct irq_domain
*parent_domain
, *domain
, *msi_domain
;
520 struct device_node
*parent_node
, *node
;
521 struct ti_sci_inta_irq_domain
*inta
;
522 struct device
*dev
= &pdev
->dev
;
523 struct resource
*res
;
526 node
= dev_of_node(dev
);
527 parent_node
= of_irq_find_parent(node
);
529 dev_err(dev
, "Failed to get IRQ parent node\n");
533 parent_domain
= irq_find_host(parent_node
);
535 return -EPROBE_DEFER
;
537 inta
= devm_kzalloc(dev
, sizeof(*inta
), GFP_KERNEL
);
542 inta
->sci
= devm_ti_sci_get_by_phandle(dev
, "ti,sci");
543 if (IS_ERR(inta
->sci
)) {
544 ret
= PTR_ERR(inta
->sci
);
545 if (ret
!= -EPROBE_DEFER
)
546 dev_err(dev
, "ti,sci read fail %d\n", ret
);
551 ret
= of_property_read_u32(dev
->of_node
, "ti,sci-dev-id", &pdev
->id
);
553 dev_err(dev
, "missing 'ti,sci-dev-id' property\n");
557 inta
->vint
= devm_ti_sci_get_of_resource(inta
->sci
, dev
, pdev
->id
,
558 "ti,sci-rm-range-vint");
559 if (IS_ERR(inta
->vint
)) {
560 dev_err(dev
, "VINT resource allocation failed\n");
561 return PTR_ERR(inta
->vint
);
564 inta
->global_event
= devm_ti_sci_get_of_resource(inta
->sci
, dev
, pdev
->id
,
565 "ti,sci-rm-range-global-event");
566 if (IS_ERR(inta
->global_event
)) {
567 dev_err(dev
, "Global event resource allocation failed\n");
568 return PTR_ERR(inta
->global_event
);
571 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
572 inta
->base
= devm_ioremap_resource(dev
, res
);
573 if (IS_ERR(inta
->base
))
576 domain
= irq_domain_add_linear(dev_of_node(dev
),
577 ti_sci_get_num_resources(inta
->vint
),
578 &ti_sci_inta_irq_domain_ops
, inta
);
580 dev_err(dev
, "Failed to allocate IRQ domain\n");
584 msi_domain
= ti_sci_inta_msi_create_irq_domain(of_node_to_fwnode(node
),
585 &ti_sci_inta_msi_domain_info
,
588 irq_domain_remove(domain
);
589 dev_err(dev
, "Failed to allocate msi domain\n");
593 INIT_LIST_HEAD(&inta
->vint_list
);
594 mutex_init(&inta
->vint_mutex
);
599 static const struct of_device_id ti_sci_inta_irq_domain_of_match
[] = {
600 { .compatible
= "ti,sci-inta", },
603 MODULE_DEVICE_TABLE(of
, ti_sci_inta_irq_domain_of_match
);
605 static struct platform_driver ti_sci_inta_irq_domain_driver
= {
606 .probe
= ti_sci_inta_irq_domain_probe
,
608 .name
= "ti-sci-inta",
609 .of_match_table
= ti_sci_inta_irq_domain_of_match
,
612 module_platform_driver(ti_sci_inta_irq_domain_driver
);
614 MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ticom>");
615 MODULE_DESCRIPTION("K3 Interrupt Aggregator driver over TI SCI protocol");
616 MODULE_LICENSE("GPL v2");