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