xref: /rk3399_ARM-atf/bl31/interrupt_mgmt.c (revision c42d0d8754ae8818a7e7a63e873ca7699a7f102b)
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  ******************************************************************************/
37*c42d0d87SArvind 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  ******************************************************************************/
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  ******************************************************************************/
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 
64e1333f75SAchin Gupta 	if (type == INTR_TYPE_S_EL1)
65c9512bcaSAntonio Nino Diaz 		return validate_sel1_interrupt_rm(rm_flags);
66e1333f75SAchin Gupta 
67e1333f75SAchin Gupta 	if (type == INTR_TYPE_NS)
68c9512bcaSAntonio Nino Diaz 		return validate_ns_interrupt_rm(rm_flags);
69e1333f75SAchin Gupta 
704e0e0f44SSoby Mathew 	if (type == INTR_TYPE_EL3)
71c9512bcaSAntonio Nino Diaz 		return validate_el3_interrupt_rm(rm_flags);
724e0e0f44SSoby Mathew 
73e1333f75SAchin Gupta 	return -EINVAL;
74e1333f75SAchin Gupta }
75e1333f75SAchin Gupta 
76e1333f75SAchin Gupta /*******************************************************************************
77e1333f75SAchin Gupta  * This function returns the cached copy of the SCR_EL3 which contains the
78e1333f75SAchin Gupta  * routing model (expressed through the IRQ and FIQ bits) for a security state
79e1333f75SAchin Gupta  * which was stored through a call to 'set_routing_model()' earlier.
80e1333f75SAchin Gupta  ******************************************************************************/
81f1be00daSLouis Mayencourt u_register_t get_scr_el3_from_routing_model(uint32_t security_state)
82e1333f75SAchin Gupta {
83f1be00daSLouis Mayencourt 	u_register_t scr_el3;
84e1333f75SAchin Gupta 
85d3280bebSJuan Castillo 	assert(sec_state_is_valid(security_state));
86e1333f75SAchin Gupta 	scr_el3 = intr_type_descs[INTR_TYPE_NS].scr_el3[security_state];
87e1333f75SAchin Gupta 	scr_el3 |= intr_type_descs[INTR_TYPE_S_EL1].scr_el3[security_state];
88e1333f75SAchin Gupta 	scr_el3 |= intr_type_descs[INTR_TYPE_EL3].scr_el3[security_state];
89e1333f75SAchin Gupta 	return scr_el3;
90e1333f75SAchin Gupta }
91e1333f75SAchin Gupta 
92e1333f75SAchin Gupta /*******************************************************************************
93e1333f75SAchin Gupta  * This function uses the 'interrupt_type_flags' parameter to obtain the value
94e1333f75SAchin Gupta  * of the trap bit (IRQ/FIQ) in the SCR_EL3 for a security state for this
95e1333f75SAchin Gupta  * interrupt type. It uses it to update the SCR_EL3 in the cpu context and the
96e1333f75SAchin Gupta  * 'intr_type_desc' for that security state.
97e1333f75SAchin Gupta  ******************************************************************************/
98e1333f75SAchin Gupta static void set_scr_el3_from_rm(uint32_t type,
99e1333f75SAchin Gupta 				uint32_t interrupt_type_flags,
100e1333f75SAchin Gupta 				uint32_t security_state)
101e1333f75SAchin Gupta {
102e1333f75SAchin Gupta 	uint32_t flag, bit_pos;
103e1333f75SAchin Gupta 
104e1333f75SAchin Gupta 	flag = get_interrupt_rm_flag(interrupt_type_flags, security_state);
105e1333f75SAchin Gupta 	bit_pos = plat_interrupt_type_to_line(type, security_state);
106f1be00daSLouis Mayencourt 	intr_type_descs[type].scr_el3[security_state] = (u_register_t)flag << bit_pos;
1075c943f7fSJuan Castillo 
108c9512bcaSAntonio Nino Diaz 	/*
109c9512bcaSAntonio Nino Diaz 	 * Update scr_el3 only if there is a context available. If not, it
1105c943f7fSJuan Castillo 	 * will be updated later during context initialization which will obtain
111c9512bcaSAntonio Nino Diaz 	 * the scr_el3 value to be used via get_scr_el3_from_routing_model()
112c9512bcaSAntonio Nino Diaz 	 */
113c9512bcaSAntonio Nino Diaz 	if (cm_get_context(security_state) != NULL)
114e1333f75SAchin Gupta 		cm_write_scr_el3_bit(security_state, bit_pos, flag);
115e1333f75SAchin Gupta }
116e1333f75SAchin Gupta 
117e1333f75SAchin Gupta /*******************************************************************************
118e1333f75SAchin Gupta  * This function validates the routing model specified in the 'flags' and
119e1333f75SAchin Gupta  * updates internal data structures to reflect the new routing model. It also
120e1333f75SAchin Gupta  * updates the copy of SCR_EL3 for each security state with the new routing
121e1333f75SAchin Gupta  * model in the 'cpu_context' structure for this cpu.
122e1333f75SAchin Gupta  ******************************************************************************/
123e1333f75SAchin Gupta int32_t set_routing_model(uint32_t type, uint32_t flags)
124e1333f75SAchin Gupta {
125e1333f75SAchin Gupta 	int32_t rc;
126e1333f75SAchin Gupta 
127e1333f75SAchin Gupta 	rc = validate_interrupt_type(type);
128c9512bcaSAntonio Nino Diaz 	if (rc != 0)
129e1333f75SAchin Gupta 		return rc;
130e1333f75SAchin Gupta 
131e1333f75SAchin Gupta 	rc = validate_routing_model(type, flags);
132c9512bcaSAntonio Nino Diaz 	if (rc != 0)
133e1333f75SAchin Gupta 		return rc;
134e1333f75SAchin Gupta 
135e1333f75SAchin Gupta 	/* Update the routing model in internal data structures */
136e1333f75SAchin Gupta 	intr_type_descs[type].flags = flags;
137e1333f75SAchin Gupta 	set_scr_el3_from_rm(type, flags, SECURE);
138e1333f75SAchin Gupta 	set_scr_el3_from_rm(type, flags, NON_SECURE);
139e1333f75SAchin Gupta 
140e1333f75SAchin Gupta 	return 0;
141e1333f75SAchin Gupta }
142e1333f75SAchin Gupta 
143f4f1ae77SSoby Mathew /******************************************************************************
144f4f1ae77SSoby Mathew  * This function disables the routing model of interrupt 'type' from the
145f4f1ae77SSoby Mathew  * specified 'security_state' on the local core. The disable is in effect
146f4f1ae77SSoby Mathew  * till the core powers down or till the next enable for that interrupt
147f4f1ae77SSoby Mathew  * type.
148f4f1ae77SSoby Mathew  *****************************************************************************/
149f4f1ae77SSoby Mathew int disable_intr_rm_local(uint32_t type, uint32_t security_state)
150f4f1ae77SSoby Mathew {
151f4f1ae77SSoby Mathew 	uint32_t bit_pos, flag;
152f4f1ae77SSoby Mathew 
153c9512bcaSAntonio Nino Diaz 	assert(intr_type_descs[type].handler != NULL);
154f4f1ae77SSoby Mathew 
155f4f1ae77SSoby Mathew 	flag = get_interrupt_rm_flag(INTR_DEFAULT_RM, security_state);
156f4f1ae77SSoby Mathew 
157f4f1ae77SSoby Mathew 	bit_pos = plat_interrupt_type_to_line(type, security_state);
158f4f1ae77SSoby Mathew 	cm_write_scr_el3_bit(security_state, bit_pos, flag);
159f4f1ae77SSoby Mathew 
160f4f1ae77SSoby Mathew 	return 0;
161f4f1ae77SSoby Mathew }
162f4f1ae77SSoby Mathew 
163f4f1ae77SSoby Mathew /******************************************************************************
164f4f1ae77SSoby Mathew  * This function enables the routing model of interrupt 'type' from the
165f4f1ae77SSoby Mathew  * specified 'security_state' on the local core.
166f4f1ae77SSoby Mathew  *****************************************************************************/
167f4f1ae77SSoby Mathew int enable_intr_rm_local(uint32_t type, uint32_t security_state)
168f4f1ae77SSoby Mathew {
169f4f1ae77SSoby Mathew 	uint32_t bit_pos, flag;
170f4f1ae77SSoby Mathew 
171c9512bcaSAntonio Nino Diaz 	assert(intr_type_descs[type].handler != NULL);
172f4f1ae77SSoby Mathew 
173f4f1ae77SSoby Mathew 	flag = get_interrupt_rm_flag(intr_type_descs[type].flags,
174f4f1ae77SSoby Mathew 				security_state);
175f4f1ae77SSoby Mathew 
176f4f1ae77SSoby Mathew 	bit_pos = plat_interrupt_type_to_line(type, security_state);
177f4f1ae77SSoby Mathew 	cm_write_scr_el3_bit(security_state, bit_pos, flag);
178f4f1ae77SSoby Mathew 
179f4f1ae77SSoby Mathew 	return 0;
180f4f1ae77SSoby Mathew }
181f4f1ae77SSoby Mathew 
182e1333f75SAchin Gupta /*******************************************************************************
183e1333f75SAchin Gupta  * This function registers a handler for the 'type' of interrupt specified. It
184e1333f75SAchin Gupta  * also validates the routing model specified in the 'flags' for this type of
185e1333f75SAchin Gupta  * interrupt.
186e1333f75SAchin Gupta  ******************************************************************************/
187e1333f75SAchin Gupta int32_t register_interrupt_type_handler(uint32_t type,
188e1333f75SAchin Gupta 					interrupt_type_handler_t handler,
189e1333f75SAchin Gupta 					uint32_t flags)
190e1333f75SAchin Gupta {
191e1333f75SAchin Gupta 	int32_t rc;
192e1333f75SAchin Gupta 
193e1333f75SAchin Gupta 	/* Validate the 'handler' parameter */
194c9512bcaSAntonio Nino Diaz 	if (handler == NULL)
195e1333f75SAchin Gupta 		return -EINVAL;
196e1333f75SAchin Gupta 
197e1333f75SAchin Gupta 	/* Validate the 'flags' parameter */
198c9512bcaSAntonio Nino Diaz 	if ((flags & INTR_TYPE_FLAGS_MASK) != 0U)
199e1333f75SAchin Gupta 		return -EINVAL;
200e1333f75SAchin Gupta 
201e1333f75SAchin Gupta 	/* Check if a handler has already been registered */
202c9512bcaSAntonio Nino Diaz 	if (intr_type_descs[type].handler != NULL)
203e1333f75SAchin Gupta 		return -EALREADY;
204e1333f75SAchin Gupta 
205e1333f75SAchin Gupta 	rc = set_routing_model(type, flags);
206c9512bcaSAntonio Nino Diaz 	if (rc != 0)
207e1333f75SAchin Gupta 		return rc;
208e1333f75SAchin Gupta 
209e1333f75SAchin Gupta 	/* Save the handler */
210e1333f75SAchin Gupta 	intr_type_descs[type].handler = handler;
211e1333f75SAchin Gupta 
212e1333f75SAchin Gupta 	return 0;
213e1333f75SAchin Gupta }
214e1333f75SAchin Gupta 
215e1333f75SAchin Gupta /*******************************************************************************
216e1333f75SAchin Gupta  * This function is called when an interrupt is generated and returns the
217e1333f75SAchin Gupta  * handler for the interrupt type (if registered). It returns NULL if the
218e1333f75SAchin Gupta  * interrupt type is not supported or its handler has not been registered.
219e1333f75SAchin Gupta  ******************************************************************************/
220e1333f75SAchin Gupta interrupt_type_handler_t get_interrupt_type_handler(uint32_t type)
221e1333f75SAchin Gupta {
222c9512bcaSAntonio Nino Diaz 	if (validate_interrupt_type(type) != 0)
223e1333f75SAchin Gupta 		return NULL;
224e1333f75SAchin Gupta 
225e1333f75SAchin Gupta 	return intr_type_descs[type].handler;
226e1333f75SAchin Gupta }
227e1333f75SAchin Gupta 
228