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