xref: /rk3399_ARM-atf/bl31/interrupt_mgmt.c (revision e1333f753f2760499ceed5c2d8f816574c3feb02)
1*e1333f75SAchin Gupta /*
2*e1333f75SAchin Gupta  * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3*e1333f75SAchin Gupta  *
4*e1333f75SAchin Gupta  * Redistribution and use in source and binary forms, with or without
5*e1333f75SAchin Gupta  * modification, are permitted provided that the following conditions are met:
6*e1333f75SAchin Gupta  *
7*e1333f75SAchin Gupta  * Redistributions of source code must retain the above copyright notice, this
8*e1333f75SAchin Gupta  * list of conditions and the following disclaimer.
9*e1333f75SAchin Gupta  *
10*e1333f75SAchin Gupta  * Redistributions in binary form must reproduce the above copyright notice,
11*e1333f75SAchin Gupta  * this list of conditions and the following disclaimer in the documentation
12*e1333f75SAchin Gupta  * and/or other materials provided with the distribution.
13*e1333f75SAchin Gupta  *
14*e1333f75SAchin Gupta  * Neither the name of ARM nor the names of its contributors may be used
15*e1333f75SAchin Gupta  * to endorse or promote products derived from this software without specific
16*e1333f75SAchin Gupta  * prior written permission.
17*e1333f75SAchin Gupta  *
18*e1333f75SAchin Gupta  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*e1333f75SAchin Gupta  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*e1333f75SAchin Gupta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*e1333f75SAchin Gupta  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*e1333f75SAchin Gupta  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*e1333f75SAchin Gupta  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*e1333f75SAchin Gupta  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*e1333f75SAchin Gupta  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*e1333f75SAchin Gupta  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*e1333f75SAchin Gupta  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*e1333f75SAchin Gupta  * POSSIBILITY OF SUCH DAMAGE.
29*e1333f75SAchin Gupta  */
30*e1333f75SAchin Gupta 
31*e1333f75SAchin Gupta #include <assert.h>
32*e1333f75SAchin Gupta #include <bl_common.h>
33*e1333f75SAchin Gupta #include <context_mgmt.h>
34*e1333f75SAchin Gupta #include <errno.h>
35*e1333f75SAchin Gupta #include <interrupt_mgmt.h>
36*e1333f75SAchin Gupta #include <platform.h>
37*e1333f75SAchin Gupta #include <stdio.h>
38*e1333f75SAchin Gupta 
39*e1333f75SAchin Gupta /*******************************************************************************
40*e1333f75SAchin Gupta  * Local structure and corresponding array to keep track of the state of the
41*e1333f75SAchin Gupta  * registered interrupt handlers for each interrupt type.
42*e1333f75SAchin Gupta  * The field descriptions are:
43*e1333f75SAchin Gupta  *
44*e1333f75SAchin Gupta  * 'flags' : Bit[0], Routing model for this interrupt type when execution is
45*e1333f75SAchin Gupta  *                   not in EL3 in the secure state. '1' implies that this
46*e1333f75SAchin Gupta  *                   interrupt will be routed to EL3. '0' implies that this
47*e1333f75SAchin Gupta  *                   interrupt will be routed to the current exception level.
48*e1333f75SAchin Gupta  *
49*e1333f75SAchin Gupta  *           Bit[1], Routing model for this interrupt type when execution is
50*e1333f75SAchin Gupta  *                   not in EL3 in the non-secure state. '1' implies that this
51*e1333f75SAchin Gupta  *                   interrupt will be routed to EL3. '0' implies that this
52*e1333f75SAchin Gupta  *                   interrupt will be routed to the current exception level.
53*e1333f75SAchin Gupta  *
54*e1333f75SAchin Gupta  *           All other bits are reserved and SBZ.
55*e1333f75SAchin Gupta  *
56*e1333f75SAchin Gupta  * 'scr_el3[2]'  : Mapping of the routing model in the 'flags' field to the
57*e1333f75SAchin Gupta  *                 value of the SCR_EL3.IRQ or FIQ bit for each security state.
58*e1333f75SAchin Gupta  *                 There are two instances of this field corresponding to the
59*e1333f75SAchin Gupta  *                 two security states.
60*e1333f75SAchin Gupta  ******************************************************************************/
61*e1333f75SAchin Gupta typedef struct intr_type_desc {
62*e1333f75SAchin Gupta 	interrupt_type_handler_t handler;
63*e1333f75SAchin Gupta 	uint32_t flags;
64*e1333f75SAchin Gupta 	uint32_t scr_el3[2];
65*e1333f75SAchin Gupta } intr_type_desc_t;
66*e1333f75SAchin Gupta 
67*e1333f75SAchin Gupta static intr_type_desc_t intr_type_descs[MAX_INTR_TYPES];
68*e1333f75SAchin Gupta 
69*e1333f75SAchin Gupta /*******************************************************************************
70*e1333f75SAchin Gupta  * This function validates the interrupt type. EL3 interrupts are currently not
71*e1333f75SAchin Gupta  * supported.
72*e1333f75SAchin Gupta  ******************************************************************************/
73*e1333f75SAchin Gupta static int32_t validate_interrupt_type(uint32_t type)
74*e1333f75SAchin Gupta {
75*e1333f75SAchin Gupta 	if (type == INTR_TYPE_EL3)
76*e1333f75SAchin Gupta 		return -ENOTSUP;
77*e1333f75SAchin Gupta 
78*e1333f75SAchin Gupta 	if (type != INTR_TYPE_S_EL1 && type != INTR_TYPE_NS)
79*e1333f75SAchin Gupta 		return -EINVAL;
80*e1333f75SAchin Gupta 
81*e1333f75SAchin Gupta 	return 0;
82*e1333f75SAchin Gupta }
83*e1333f75SAchin Gupta 
84*e1333f75SAchin Gupta /*******************************************************************************
85*e1333f75SAchin Gupta * This function validates the routing model for this type of interrupt
86*e1333f75SAchin Gupta  ******************************************************************************/
87*e1333f75SAchin Gupta static int32_t validate_routing_model(uint32_t type, uint32_t flags)
88*e1333f75SAchin Gupta {
89*e1333f75SAchin Gupta 	flags >>= INTR_RM_FLAGS_SHIFT;
90*e1333f75SAchin Gupta 	flags &= INTR_RM_FLAGS_MASK;
91*e1333f75SAchin Gupta 
92*e1333f75SAchin Gupta 	if (type == INTR_TYPE_S_EL1)
93*e1333f75SAchin Gupta 		return validate_sel1_interrupt_rm(flags);
94*e1333f75SAchin Gupta 
95*e1333f75SAchin Gupta 	if (type == INTR_TYPE_NS)
96*e1333f75SAchin Gupta 		return validate_ns_interrupt_rm(flags);
97*e1333f75SAchin Gupta 
98*e1333f75SAchin Gupta 	return -EINVAL;
99*e1333f75SAchin Gupta }
100*e1333f75SAchin Gupta 
101*e1333f75SAchin Gupta /*******************************************************************************
102*e1333f75SAchin Gupta  * This function returns the cached copy of the SCR_EL3 which contains the
103*e1333f75SAchin Gupta  * routing model (expressed through the IRQ and FIQ bits) for a security state
104*e1333f75SAchin Gupta  * which was stored through a call to 'set_routing_model()' earlier.
105*e1333f75SAchin Gupta  ******************************************************************************/
106*e1333f75SAchin Gupta uint32_t get_scr_el3_from_routing_model(uint32_t security_state)
107*e1333f75SAchin Gupta {
108*e1333f75SAchin Gupta 	uint32_t scr_el3;
109*e1333f75SAchin Gupta 
110*e1333f75SAchin Gupta 	assert(security_state <= NON_SECURE);
111*e1333f75SAchin Gupta 	scr_el3 = intr_type_descs[INTR_TYPE_NS].scr_el3[security_state];
112*e1333f75SAchin Gupta 	scr_el3 |= intr_type_descs[INTR_TYPE_S_EL1].scr_el3[security_state];
113*e1333f75SAchin Gupta 	scr_el3 |= intr_type_descs[INTR_TYPE_EL3].scr_el3[security_state];
114*e1333f75SAchin Gupta 	return scr_el3;
115*e1333f75SAchin Gupta }
116*e1333f75SAchin Gupta 
117*e1333f75SAchin Gupta /*******************************************************************************
118*e1333f75SAchin Gupta  * This function uses the 'interrupt_type_flags' parameter to obtain the value
119*e1333f75SAchin Gupta  * of the trap bit (IRQ/FIQ) in the SCR_EL3 for a security state for this
120*e1333f75SAchin Gupta  * interrupt type. It uses it to update the SCR_EL3 in the cpu context and the
121*e1333f75SAchin Gupta  * 'intr_type_desc' for that security state.
122*e1333f75SAchin Gupta  ******************************************************************************/
123*e1333f75SAchin Gupta static void set_scr_el3_from_rm(uint32_t type,
124*e1333f75SAchin Gupta 				uint32_t interrupt_type_flags,
125*e1333f75SAchin Gupta 				uint32_t security_state)
126*e1333f75SAchin Gupta {
127*e1333f75SAchin Gupta 	uint32_t flag, bit_pos;
128*e1333f75SAchin Gupta 
129*e1333f75SAchin Gupta 	flag = get_interrupt_rm_flag(interrupt_type_flags, security_state);
130*e1333f75SAchin Gupta 	bit_pos = plat_interrupt_type_to_line(type, security_state);
131*e1333f75SAchin Gupta 	intr_type_descs[type].scr_el3[security_state] = flag << bit_pos;
132*e1333f75SAchin Gupta 	cm_write_scr_el3_bit(security_state, bit_pos, flag);
133*e1333f75SAchin Gupta }
134*e1333f75SAchin Gupta 
135*e1333f75SAchin Gupta /*******************************************************************************
136*e1333f75SAchin Gupta  * This function validates the routing model specified in the 'flags' and
137*e1333f75SAchin Gupta  * updates internal data structures to reflect the new routing model. It also
138*e1333f75SAchin Gupta  * updates the copy of SCR_EL3 for each security state with the new routing
139*e1333f75SAchin Gupta  * model in the 'cpu_context' structure for this cpu.
140*e1333f75SAchin Gupta  ******************************************************************************/
141*e1333f75SAchin Gupta int32_t set_routing_model(uint32_t type, uint32_t flags)
142*e1333f75SAchin Gupta {
143*e1333f75SAchin Gupta 	int32_t rc;
144*e1333f75SAchin Gupta 
145*e1333f75SAchin Gupta 	rc = validate_interrupt_type(type);
146*e1333f75SAchin Gupta 	if (rc)
147*e1333f75SAchin Gupta 		return rc;
148*e1333f75SAchin Gupta 
149*e1333f75SAchin Gupta 	rc = validate_routing_model(type, flags);
150*e1333f75SAchin Gupta 	if (rc)
151*e1333f75SAchin Gupta 		return rc;
152*e1333f75SAchin Gupta 
153*e1333f75SAchin Gupta 	/* Update the routing model in internal data structures */
154*e1333f75SAchin Gupta 	intr_type_descs[type].flags = flags;
155*e1333f75SAchin Gupta 	set_scr_el3_from_rm(type, flags, SECURE);
156*e1333f75SAchin Gupta 	set_scr_el3_from_rm(type, flags, NON_SECURE);
157*e1333f75SAchin Gupta 
158*e1333f75SAchin Gupta 	return 0;
159*e1333f75SAchin Gupta }
160*e1333f75SAchin Gupta 
161*e1333f75SAchin Gupta /*******************************************************************************
162*e1333f75SAchin Gupta  * This function registers a handler for the 'type' of interrupt specified. It
163*e1333f75SAchin Gupta  * also validates the routing model specified in the 'flags' for this type of
164*e1333f75SAchin Gupta  * interrupt.
165*e1333f75SAchin Gupta  ******************************************************************************/
166*e1333f75SAchin Gupta int32_t register_interrupt_type_handler(uint32_t type,
167*e1333f75SAchin Gupta 					interrupt_type_handler_t handler,
168*e1333f75SAchin Gupta 					uint32_t flags)
169*e1333f75SAchin Gupta {
170*e1333f75SAchin Gupta 	int32_t rc;
171*e1333f75SAchin Gupta 
172*e1333f75SAchin Gupta 	/* Validate the 'handler' parameter */
173*e1333f75SAchin Gupta 	if (!handler)
174*e1333f75SAchin Gupta 		return -EINVAL;
175*e1333f75SAchin Gupta 
176*e1333f75SAchin Gupta 	/* Validate the 'flags' parameter */
177*e1333f75SAchin Gupta 	if (flags & INTR_TYPE_FLAGS_MASK)
178*e1333f75SAchin Gupta 		return -EINVAL;
179*e1333f75SAchin Gupta 
180*e1333f75SAchin Gupta 	/* Check if a handler has already been registered */
181*e1333f75SAchin Gupta 	if (intr_type_descs[type].handler)
182*e1333f75SAchin Gupta 		return -EALREADY;
183*e1333f75SAchin Gupta 
184*e1333f75SAchin Gupta 	rc = set_routing_model(type, flags);
185*e1333f75SAchin Gupta 	if (rc)
186*e1333f75SAchin Gupta 		return rc;
187*e1333f75SAchin Gupta 
188*e1333f75SAchin Gupta 	/* Save the handler */
189*e1333f75SAchin Gupta 	intr_type_descs[type].handler = handler;
190*e1333f75SAchin Gupta 
191*e1333f75SAchin Gupta 	return 0;
192*e1333f75SAchin Gupta }
193*e1333f75SAchin Gupta 
194*e1333f75SAchin Gupta /*******************************************************************************
195*e1333f75SAchin Gupta  * This function is called when an interrupt is generated and returns the
196*e1333f75SAchin Gupta  * handler for the interrupt type (if registered). It returns NULL if the
197*e1333f75SAchin Gupta  * interrupt type is not supported or its handler has not been registered.
198*e1333f75SAchin Gupta  ******************************************************************************/
199*e1333f75SAchin Gupta interrupt_type_handler_t get_interrupt_type_handler(uint32_t type)
200*e1333f75SAchin Gupta {
201*e1333f75SAchin Gupta 	if (validate_interrupt_type(type))
202*e1333f75SAchin Gupta 		return NULL;
203*e1333f75SAchin Gupta 
204*e1333f75SAchin Gupta 	return intr_type_descs[type].handler;
205*e1333f75SAchin Gupta }
206*e1333f75SAchin Gupta 
207