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