xref: /arm-trusted-firmware/bl31/interrupt_mgmt.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2014-2023, Arm Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <errno.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <common/bl_common.h>
11*91f16700Schasinglulu #include <bl31/interrupt_mgmt.h>
12*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
13*91f16700Schasinglulu #include <plat/common/platform.h>
14*91f16700Schasinglulu 
15*91f16700Schasinglulu /*******************************************************************************
16*91f16700Schasinglulu  * Local structure and corresponding array to keep track of the state of the
17*91f16700Schasinglulu  * registered interrupt handlers for each interrupt type.
18*91f16700Schasinglulu  * The field descriptions are:
19*91f16700Schasinglulu  *
20*91f16700Schasinglulu  * 'scr_el3[2]'  : Mapping of the routing model in the 'flags' field to the
21*91f16700Schasinglulu  *                 value of the SCR_EL3.IRQ or FIQ bit for each security state.
22*91f16700Schasinglulu  *                 There are two instances of this field corresponding to the
23*91f16700Schasinglulu  *                 two security states.
24*91f16700Schasinglulu  *
25*91f16700Schasinglulu  * 'flags' : Bit[0], Routing model for this interrupt type when execution is
26*91f16700Schasinglulu  *                   not in EL3 in the secure state. '1' implies that this
27*91f16700Schasinglulu  *                   interrupt will be routed to EL3. '0' implies that this
28*91f16700Schasinglulu  *                   interrupt will be routed to the current exception level.
29*91f16700Schasinglulu  *
30*91f16700Schasinglulu  *           Bit[1], Routing model for this interrupt type when execution is
31*91f16700Schasinglulu  *                   not in EL3 in the non-secure state. '1' implies that this
32*91f16700Schasinglulu  *                   interrupt will be routed to EL3. '0' implies that this
33*91f16700Schasinglulu  *                   interrupt will be routed to the current exception level.
34*91f16700Schasinglulu  *
35*91f16700Schasinglulu  *           All other bits are reserved and SBZ.
36*91f16700Schasinglulu  ******************************************************************************/
37*91f16700Schasinglulu typedef struct intr_type_desc {
38*91f16700Schasinglulu 	interrupt_type_handler_t handler;
39*91f16700Schasinglulu 	u_register_t scr_el3[2];
40*91f16700Schasinglulu 	uint32_t flags;
41*91f16700Schasinglulu } intr_type_desc_t;
42*91f16700Schasinglulu 
43*91f16700Schasinglulu static intr_type_desc_t intr_type_descs[MAX_INTR_TYPES];
44*91f16700Schasinglulu 
45*91f16700Schasinglulu /*******************************************************************************
46*91f16700Schasinglulu  * This function validates the interrupt type.
47*91f16700Schasinglulu  ******************************************************************************/
48*91f16700Schasinglulu static int32_t validate_interrupt_type(uint32_t type)
49*91f16700Schasinglulu {
50*91f16700Schasinglulu 	if (plat_ic_has_interrupt_type(type)) {
51*91f16700Schasinglulu 		return 0;
52*91f16700Schasinglulu 	}
53*91f16700Schasinglulu 
54*91f16700Schasinglulu 	return -EINVAL;
55*91f16700Schasinglulu }
56*91f16700Schasinglulu 
57*91f16700Schasinglulu /*******************************************************************************
58*91f16700Schasinglulu * This function validates the routing model for this type of interrupt
59*91f16700Schasinglulu  ******************************************************************************/
60*91f16700Schasinglulu static int32_t validate_routing_model(uint32_t type, uint32_t flags)
61*91f16700Schasinglulu {
62*91f16700Schasinglulu 	uint32_t rm_flags = (flags >> INTR_RM_FLAGS_SHIFT) & INTR_RM_FLAGS_MASK;
63*91f16700Schasinglulu 
64*91f16700Schasinglulu 	if (type == INTR_TYPE_S_EL1)
65*91f16700Schasinglulu 		return validate_sel1_interrupt_rm(rm_flags);
66*91f16700Schasinglulu 
67*91f16700Schasinglulu 	if (type == INTR_TYPE_NS)
68*91f16700Schasinglulu 		return validate_ns_interrupt_rm(rm_flags);
69*91f16700Schasinglulu 
70*91f16700Schasinglulu 	if (type == INTR_TYPE_EL3)
71*91f16700Schasinglulu 		return validate_el3_interrupt_rm(rm_flags);
72*91f16700Schasinglulu 
73*91f16700Schasinglulu 	return -EINVAL;
74*91f16700Schasinglulu }
75*91f16700Schasinglulu 
76*91f16700Schasinglulu /*******************************************************************************
77*91f16700Schasinglulu  * This function returns the cached copy of the SCR_EL3 which contains the
78*91f16700Schasinglulu  * routing model (expressed through the IRQ and FIQ bits) for a security state
79*91f16700Schasinglulu  * which was stored through a call to 'set_routing_model()' earlier.
80*91f16700Schasinglulu  ******************************************************************************/
81*91f16700Schasinglulu u_register_t get_scr_el3_from_routing_model(uint32_t security_state)
82*91f16700Schasinglulu {
83*91f16700Schasinglulu 	u_register_t scr_el3;
84*91f16700Schasinglulu 
85*91f16700Schasinglulu 	assert(sec_state_is_valid(security_state));
86*91f16700Schasinglulu 	scr_el3 = intr_type_descs[INTR_TYPE_NS].scr_el3[security_state];
87*91f16700Schasinglulu 	scr_el3 |= intr_type_descs[INTR_TYPE_S_EL1].scr_el3[security_state];
88*91f16700Schasinglulu 	scr_el3 |= intr_type_descs[INTR_TYPE_EL3].scr_el3[security_state];
89*91f16700Schasinglulu 	return scr_el3;
90*91f16700Schasinglulu }
91*91f16700Schasinglulu 
92*91f16700Schasinglulu /*******************************************************************************
93*91f16700Schasinglulu  * This function uses the 'interrupt_type_flags' parameter to obtain the value
94*91f16700Schasinglulu  * of the trap bit (IRQ/FIQ) in the SCR_EL3 for a security state for this
95*91f16700Schasinglulu  * interrupt type. It uses it to update the SCR_EL3 in the cpu context and the
96*91f16700Schasinglulu  * 'intr_type_desc' for that security state.
97*91f16700Schasinglulu  ******************************************************************************/
98*91f16700Schasinglulu static void set_scr_el3_from_rm(uint32_t type,
99*91f16700Schasinglulu 				uint32_t interrupt_type_flags,
100*91f16700Schasinglulu 				uint32_t security_state)
101*91f16700Schasinglulu {
102*91f16700Schasinglulu 	uint32_t flag, bit_pos;
103*91f16700Schasinglulu 
104*91f16700Schasinglulu 	flag = get_interrupt_rm_flag(interrupt_type_flags, security_state);
105*91f16700Schasinglulu 	bit_pos = plat_interrupt_type_to_line(type, security_state);
106*91f16700Schasinglulu 	intr_type_descs[type].scr_el3[security_state] = (u_register_t)flag << bit_pos;
107*91f16700Schasinglulu 
108*91f16700Schasinglulu 	/*
109*91f16700Schasinglulu 	 * Update scr_el3 only if there is a context available. If not, it
110*91f16700Schasinglulu 	 * will be updated later during context initialization which will obtain
111*91f16700Schasinglulu 	 * the scr_el3 value to be used via get_scr_el3_from_routing_model()
112*91f16700Schasinglulu 	 */
113*91f16700Schasinglulu 	if (cm_get_context(security_state) != NULL)
114*91f16700Schasinglulu 		cm_write_scr_el3_bit(security_state, bit_pos, flag);
115*91f16700Schasinglulu }
116*91f16700Schasinglulu 
117*91f16700Schasinglulu /*******************************************************************************
118*91f16700Schasinglulu  * This function validates the routing model specified in the 'flags' and
119*91f16700Schasinglulu  * updates internal data structures to reflect the new routing model. It also
120*91f16700Schasinglulu  * updates the copy of SCR_EL3 for each security state with the new routing
121*91f16700Schasinglulu  * model in the 'cpu_context' structure for this cpu.
122*91f16700Schasinglulu  ******************************************************************************/
123*91f16700Schasinglulu int32_t set_routing_model(uint32_t type, uint32_t flags)
124*91f16700Schasinglulu {
125*91f16700Schasinglulu 	int32_t rc;
126*91f16700Schasinglulu 
127*91f16700Schasinglulu 	rc = validate_interrupt_type(type);
128*91f16700Schasinglulu 	if (rc != 0)
129*91f16700Schasinglulu 		return rc;
130*91f16700Schasinglulu 
131*91f16700Schasinglulu 	rc = validate_routing_model(type, flags);
132*91f16700Schasinglulu 	if (rc != 0)
133*91f16700Schasinglulu 		return rc;
134*91f16700Schasinglulu 
135*91f16700Schasinglulu 	/* Update the routing model in internal data structures */
136*91f16700Schasinglulu 	intr_type_descs[type].flags = flags;
137*91f16700Schasinglulu 	set_scr_el3_from_rm(type, flags, SECURE);
138*91f16700Schasinglulu 	set_scr_el3_from_rm(type, flags, NON_SECURE);
139*91f16700Schasinglulu 
140*91f16700Schasinglulu 	return 0;
141*91f16700Schasinglulu }
142*91f16700Schasinglulu 
143*91f16700Schasinglulu /******************************************************************************
144*91f16700Schasinglulu  * This function disables the routing model of interrupt 'type' from the
145*91f16700Schasinglulu  * specified 'security_state' on the local core. The disable is in effect
146*91f16700Schasinglulu  * till the core powers down or till the next enable for that interrupt
147*91f16700Schasinglulu  * type.
148*91f16700Schasinglulu  *****************************************************************************/
149*91f16700Schasinglulu int disable_intr_rm_local(uint32_t type, uint32_t security_state)
150*91f16700Schasinglulu {
151*91f16700Schasinglulu 	uint32_t bit_pos, flag;
152*91f16700Schasinglulu 
153*91f16700Schasinglulu 	assert(intr_type_descs[type].handler != NULL);
154*91f16700Schasinglulu 
155*91f16700Schasinglulu 	flag = get_interrupt_rm_flag(INTR_DEFAULT_RM, security_state);
156*91f16700Schasinglulu 
157*91f16700Schasinglulu 	bit_pos = plat_interrupt_type_to_line(type, security_state);
158*91f16700Schasinglulu 	cm_write_scr_el3_bit(security_state, bit_pos, flag);
159*91f16700Schasinglulu 
160*91f16700Schasinglulu 	return 0;
161*91f16700Schasinglulu }
162*91f16700Schasinglulu 
163*91f16700Schasinglulu /******************************************************************************
164*91f16700Schasinglulu  * This function enables the routing model of interrupt 'type' from the
165*91f16700Schasinglulu  * specified 'security_state' on the local core.
166*91f16700Schasinglulu  *****************************************************************************/
167*91f16700Schasinglulu int enable_intr_rm_local(uint32_t type, uint32_t security_state)
168*91f16700Schasinglulu {
169*91f16700Schasinglulu 	uint32_t bit_pos, flag;
170*91f16700Schasinglulu 
171*91f16700Schasinglulu 	assert(intr_type_descs[type].handler != NULL);
172*91f16700Schasinglulu 
173*91f16700Schasinglulu 	flag = get_interrupt_rm_flag(intr_type_descs[type].flags,
174*91f16700Schasinglulu 				security_state);
175*91f16700Schasinglulu 
176*91f16700Schasinglulu 	bit_pos = plat_interrupt_type_to_line(type, security_state);
177*91f16700Schasinglulu 	cm_write_scr_el3_bit(security_state, bit_pos, flag);
178*91f16700Schasinglulu 
179*91f16700Schasinglulu 	return 0;
180*91f16700Schasinglulu }
181*91f16700Schasinglulu 
182*91f16700Schasinglulu /*******************************************************************************
183*91f16700Schasinglulu  * This function registers a handler for the 'type' of interrupt specified. It
184*91f16700Schasinglulu  * also validates the routing model specified in the 'flags' for this type of
185*91f16700Schasinglulu  * interrupt.
186*91f16700Schasinglulu  ******************************************************************************/
187*91f16700Schasinglulu int32_t register_interrupt_type_handler(uint32_t type,
188*91f16700Schasinglulu 					interrupt_type_handler_t handler,
189*91f16700Schasinglulu 					uint32_t flags)
190*91f16700Schasinglulu {
191*91f16700Schasinglulu 	int32_t rc;
192*91f16700Schasinglulu 
193*91f16700Schasinglulu 	/* Validate the 'handler' parameter */
194*91f16700Schasinglulu 	if (handler == NULL)
195*91f16700Schasinglulu 		return -EINVAL;
196*91f16700Schasinglulu 
197*91f16700Schasinglulu 	/* Validate the 'flags' parameter */
198*91f16700Schasinglulu 	if ((flags & INTR_TYPE_FLAGS_MASK) != 0U)
199*91f16700Schasinglulu 		return -EINVAL;
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	/* Check if a handler has already been registered */
202*91f16700Schasinglulu 	if (intr_type_descs[type].handler != NULL)
203*91f16700Schasinglulu 		return -EALREADY;
204*91f16700Schasinglulu 
205*91f16700Schasinglulu 	rc = set_routing_model(type, flags);
206*91f16700Schasinglulu 	if (rc != 0)
207*91f16700Schasinglulu 		return rc;
208*91f16700Schasinglulu 
209*91f16700Schasinglulu 	/* Save the handler */
210*91f16700Schasinglulu 	intr_type_descs[type].handler = handler;
211*91f16700Schasinglulu 
212*91f16700Schasinglulu 	return 0;
213*91f16700Schasinglulu }
214*91f16700Schasinglulu 
215*91f16700Schasinglulu /*******************************************************************************
216*91f16700Schasinglulu  * This function is called when an interrupt is generated and returns the
217*91f16700Schasinglulu  * handler for the interrupt type (if registered). It returns NULL if the
218*91f16700Schasinglulu  * interrupt type is not supported or its handler has not been registered.
219*91f16700Schasinglulu  ******************************************************************************/
220*91f16700Schasinglulu interrupt_type_handler_t get_interrupt_type_handler(uint32_t type)
221*91f16700Schasinglulu {
222*91f16700Schasinglulu 	if (validate_interrupt_type(type) != 0)
223*91f16700Schasinglulu 		return NULL;
224*91f16700Schasinglulu 
225*91f16700Schasinglulu 	return intr_type_descs[type].handler;
226*91f16700Schasinglulu }
227*91f16700Schasinglulu 
228