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