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