xref: /rk3399_ARM-atf/plat/common/plat_gicv3.c (revision eb68ea9b10c190c237216dee92166f9e7b2ce3d4)
1f14d1886SSoby Mathew /*
2*eb68ea9bSJeenu Viswambharan  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3f14d1886SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5f14d1886SSoby Mathew  */
6f14d1886SSoby Mathew #include <arch_helpers.h>
7f14d1886SSoby Mathew #include <assert.h>
8f14d1886SSoby Mathew #include <bl_common.h>
9f14d1886SSoby Mathew #include <cassert.h>
10f14d1886SSoby Mathew #include <gic_common.h>
11f14d1886SSoby Mathew #include <gicv3.h>
12f14d1886SSoby Mathew #include <interrupt_mgmt.h>
13f14d1886SSoby Mathew #include <platform.h>
14f14d1886SSoby Mathew 
153d8256b2SMasahiro Yamada #ifdef IMAGE_BL31
16f14d1886SSoby Mathew 
17f14d1886SSoby Mathew /*
18f14d1886SSoby Mathew  * The following platform GIC functions are weakly defined. They
19f14d1886SSoby Mathew  * provide typical implementations that may be re-used by multiple
20f14d1886SSoby Mathew  * platforms but may also be overridden by a platform if required.
21f14d1886SSoby Mathew  */
22f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id
23f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_type
24f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt
25f14d1886SSoby Mathew #pragma weak plat_ic_get_interrupt_type
26f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt
27f14d1886SSoby Mathew #pragma weak plat_interrupt_type_to_line
28f14d1886SSoby Mathew 
29*eb68ea9bSJeenu Viswambharan #pragma weak plat_ic_get_running_priority
30*eb68ea9bSJeenu Viswambharan 
31f14d1886SSoby Mathew CASSERT((INTR_TYPE_S_EL1 == INTR_GROUP1S) &&
32f14d1886SSoby Mathew 	(INTR_TYPE_NS == INTR_GROUP1NS) &&
33f14d1886SSoby Mathew 	(INTR_TYPE_EL3 == INTR_GROUP0), assert_interrupt_type_mismatch);
34f14d1886SSoby Mathew 
35f14d1886SSoby Mathew /*
36f14d1886SSoby Mathew  * This function returns the highest priority pending interrupt at
37f14d1886SSoby Mathew  * the Interrupt controller
38f14d1886SSoby Mathew  */
39f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void)
40f14d1886SSoby Mathew {
41f14d1886SSoby Mathew 	unsigned int irqnr;
42f14d1886SSoby Mathew 
43f14d1886SSoby Mathew 	assert(IS_IN_EL3());
44f14d1886SSoby Mathew 	irqnr = gicv3_get_pending_interrupt_id();
45f14d1886SSoby Mathew 	return (gicv3_is_intr_id_special_identifier(irqnr)) ?
46f14d1886SSoby Mathew 				INTR_ID_UNAVAILABLE : irqnr;
47f14d1886SSoby Mathew }
48f14d1886SSoby Mathew 
49f14d1886SSoby Mathew /*
50f14d1886SSoby Mathew  * This function returns the type of the highest priority pending interrupt
51f14d1886SSoby Mathew  * at the Interrupt controller. In the case of GICv3, the Highest Priority
52f14d1886SSoby Mathew  * Pending interrupt system register (`ICC_HPPIR0_EL1`) is read to determine
53f14d1886SSoby Mathew  * the id of the pending interrupt. The type of interrupt depends upon the
54f14d1886SSoby Mathew  * id value as follows.
55f14d1886SSoby Mathew  *   1. id = PENDING_G1S_INTID (1020) is reported as a S-EL1 interrupt
56f14d1886SSoby Mathew  *   2. id = PENDING_G1NS_INTID (1021) is reported as a Non-secure interrupt.
57f14d1886SSoby Mathew  *   3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt
58f14d1886SSoby Mathew  *           type.
59f14d1886SSoby Mathew  *   4. All other interrupt id's are reported as EL3 interrupt.
60f14d1886SSoby Mathew  */
61f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_type(void)
62f14d1886SSoby Mathew {
63f14d1886SSoby Mathew 	unsigned int irqnr;
64f14d1886SSoby Mathew 
65f14d1886SSoby Mathew 	assert(IS_IN_EL3());
66f14d1886SSoby Mathew 	irqnr = gicv3_get_pending_interrupt_type();
67f14d1886SSoby Mathew 
68f14d1886SSoby Mathew 	switch (irqnr) {
69f14d1886SSoby Mathew 	case PENDING_G1S_INTID:
70f14d1886SSoby Mathew 		return INTR_TYPE_S_EL1;
71f14d1886SSoby Mathew 	case PENDING_G1NS_INTID:
72f14d1886SSoby Mathew 		return INTR_TYPE_NS;
73f14d1886SSoby Mathew 	case GIC_SPURIOUS_INTERRUPT:
74f14d1886SSoby Mathew 		return INTR_TYPE_INVAL;
75f14d1886SSoby Mathew 	default:
76f14d1886SSoby Mathew 		return INTR_TYPE_EL3;
77f14d1886SSoby Mathew 	}
78f14d1886SSoby Mathew }
79f14d1886SSoby Mathew 
80f14d1886SSoby Mathew /*
81f14d1886SSoby Mathew  * This function returns the highest priority pending interrupt at
82f14d1886SSoby Mathew  * the Interrupt controller and indicates to the Interrupt controller
83f14d1886SSoby Mathew  * that the interrupt processing has started.
84f14d1886SSoby Mathew  */
85f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void)
86f14d1886SSoby Mathew {
87f14d1886SSoby Mathew 	assert(IS_IN_EL3());
88f14d1886SSoby Mathew 	return gicv3_acknowledge_interrupt();
89f14d1886SSoby Mathew }
90f14d1886SSoby Mathew 
91f14d1886SSoby Mathew /*
92f14d1886SSoby Mathew  * This function returns the type of the interrupt `id`, depending on how
93f14d1886SSoby Mathew  * the interrupt has been configured in the interrupt controller
94f14d1886SSoby Mathew  */
95f14d1886SSoby Mathew uint32_t plat_ic_get_interrupt_type(uint32_t id)
96f14d1886SSoby Mathew {
97f14d1886SSoby Mathew 	assert(IS_IN_EL3());
98f14d1886SSoby Mathew 	return gicv3_get_interrupt_type(id, plat_my_core_pos());
99f14d1886SSoby Mathew }
100f14d1886SSoby Mathew 
101f14d1886SSoby Mathew /*
102f14d1886SSoby Mathew  * This functions is used to indicate to the interrupt controller that
103f14d1886SSoby Mathew  * the processing of the interrupt corresponding to the `id` has
104f14d1886SSoby Mathew  * finished.
105f14d1886SSoby Mathew  */
106f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id)
107f14d1886SSoby Mathew {
108f14d1886SSoby Mathew 	assert(IS_IN_EL3());
109f14d1886SSoby Mathew 	gicv3_end_of_interrupt(id);
110f14d1886SSoby Mathew }
111f14d1886SSoby Mathew 
112f14d1886SSoby Mathew /*
113f14d1886SSoby Mathew  * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
114f14d1886SSoby Mathew  * The interrupt controller knows which pin/line it uses to signal a type of
115f14d1886SSoby Mathew  * interrupt. It lets the interrupt management framework determine for a type of
116f14d1886SSoby Mathew  * interrupt and security state, which line should be used in the SCR_EL3 to
117f14d1886SSoby Mathew  * control its routing to EL3. The interrupt line is represented as the bit
118f14d1886SSoby Mathew  * position of the IRQ or FIQ bit in the SCR_EL3.
119f14d1886SSoby Mathew  */
120f14d1886SSoby Mathew uint32_t plat_interrupt_type_to_line(uint32_t type,
121f14d1886SSoby Mathew 				uint32_t security_state)
122f14d1886SSoby Mathew {
123f14d1886SSoby Mathew 	assert(type == INTR_TYPE_S_EL1 ||
124f14d1886SSoby Mathew 	       type == INTR_TYPE_EL3 ||
125f14d1886SSoby Mathew 	       type == INTR_TYPE_NS);
126f14d1886SSoby Mathew 
127f14d1886SSoby Mathew 	assert(sec_state_is_valid(security_state));
128f14d1886SSoby Mathew 	assert(IS_IN_EL3());
129f14d1886SSoby Mathew 
130f14d1886SSoby Mathew 	switch (type) {
131f14d1886SSoby Mathew 	case INTR_TYPE_S_EL1:
132f14d1886SSoby Mathew 		/*
133f14d1886SSoby Mathew 		 * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 contexts
134f14d1886SSoby Mathew 		 * and as FIQ in the NS-EL0/1/2 contexts
135f14d1886SSoby Mathew 		 */
136f14d1886SSoby Mathew 		if (security_state == SECURE)
137f14d1886SSoby Mathew 			return __builtin_ctz(SCR_IRQ_BIT);
138f14d1886SSoby Mathew 		else
139f14d1886SSoby Mathew 			return __builtin_ctz(SCR_FIQ_BIT);
140f14d1886SSoby Mathew 	case INTR_TYPE_NS:
141f14d1886SSoby Mathew 		/*
142f14d1886SSoby Mathew 		 * The Non secure interrupts will be signaled as FIQ in S-EL0/1
143f14d1886SSoby Mathew 		 * contexts and as IRQ in the NS-EL0/1/2 contexts.
144f14d1886SSoby Mathew 		 */
145f14d1886SSoby Mathew 		if (security_state == SECURE)
146f14d1886SSoby Mathew 			return __builtin_ctz(SCR_FIQ_BIT);
147f14d1886SSoby Mathew 		else
148f14d1886SSoby Mathew 			return __builtin_ctz(SCR_IRQ_BIT);
149f14d1886SSoby Mathew 	default:
150f14d1886SSoby Mathew 		assert(0);
151f14d1886SSoby Mathew 		/* Fall through in the release build */
152f14d1886SSoby Mathew 	case INTR_TYPE_EL3:
153f14d1886SSoby Mathew 		/*
154f14d1886SSoby Mathew 		 * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and
155f14d1886SSoby Mathew 		 * NS-EL0/1/2 contexts
156f14d1886SSoby Mathew 		 */
157f14d1886SSoby Mathew 		return __builtin_ctz(SCR_FIQ_BIT);
158f14d1886SSoby Mathew 	}
159f14d1886SSoby Mathew }
160*eb68ea9bSJeenu Viswambharan 
161*eb68ea9bSJeenu Viswambharan unsigned int plat_ic_get_running_priority(void)
162*eb68ea9bSJeenu Viswambharan {
163*eb68ea9bSJeenu Viswambharan 	return gicv3_get_running_priority();
164*eb68ea9bSJeenu Viswambharan }
165*eb68ea9bSJeenu Viswambharan 
166f14d1886SSoby Mathew #endif
1673d8256b2SMasahiro Yamada #ifdef IMAGE_BL32
168f14d1886SSoby Mathew 
169f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id
170f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt
171f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt
172f14d1886SSoby Mathew 
173877cf3ffSSoby Mathew /* In AArch32, the secure group1 interrupts are targeted to Secure PL1 */
174877cf3ffSSoby Mathew #ifdef AARCH32
175877cf3ffSSoby Mathew #define IS_IN_EL1()	IS_IN_SECURE()
176877cf3ffSSoby Mathew #endif
177877cf3ffSSoby Mathew 
178f14d1886SSoby Mathew /*
179f14d1886SSoby Mathew  * This function returns the highest priority pending interrupt at
180f14d1886SSoby Mathew  * the Interrupt controller
181f14d1886SSoby Mathew  */
182f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void)
183f14d1886SSoby Mathew {
184f14d1886SSoby Mathew 	unsigned int irqnr;
185f14d1886SSoby Mathew 
186f14d1886SSoby Mathew 	assert(IS_IN_EL1());
187f14d1886SSoby Mathew 	irqnr = gicv3_get_pending_interrupt_id_sel1();
188f14d1886SSoby Mathew 	return (irqnr == GIC_SPURIOUS_INTERRUPT) ?
189f14d1886SSoby Mathew 				INTR_ID_UNAVAILABLE : irqnr;
190f14d1886SSoby Mathew }
191f14d1886SSoby Mathew 
192f14d1886SSoby Mathew /*
193f14d1886SSoby Mathew  * This function returns the highest priority pending interrupt at
194f14d1886SSoby Mathew  * the Interrupt controller and indicates to the Interrupt controller
195f14d1886SSoby Mathew  * that the interrupt processing has started.
196f14d1886SSoby Mathew  */
197f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void)
198f14d1886SSoby Mathew {
199f14d1886SSoby Mathew 	assert(IS_IN_EL1());
200f14d1886SSoby Mathew 	return gicv3_acknowledge_interrupt_sel1();
201f14d1886SSoby Mathew }
202f14d1886SSoby Mathew 
203f14d1886SSoby Mathew /*
204f14d1886SSoby Mathew  * This functions is used to indicate to the interrupt controller that
205f14d1886SSoby Mathew  * the processing of the interrupt corresponding to the `id` has
206f14d1886SSoby Mathew  * finished.
207f14d1886SSoby Mathew  */
208f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id)
209f14d1886SSoby Mathew {
210f14d1886SSoby Mathew 	assert(IS_IN_EL1());
211f14d1886SSoby Mathew 	gicv3_end_of_interrupt_sel1(id);
212f14d1886SSoby Mathew }
213f14d1886SSoby Mathew #endif
214