blob: 0df50b6d2f621cab4cdcfeb6ea6ef46c970504af [file] [log] [blame]
Achin Gupta191e86e2014-05-09 10:03:15 +01001/*
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +01002 * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
Achin Gupta191e86e2014-05-09 10:03:15 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta191e86e2014-05-09 10:03:15 +01005 */
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>
Achin Gupta191e86e2014-05-09 10:03:15 +010013
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 ******************************************************************************/
36typedef 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
42static intr_type_desc_t intr_type_descs[MAX_INTR_TYPES];
43
44/*******************************************************************************
Soby Mathew58e32d12015-11-23 13:58:45 +000045 * This function validates the interrupt type.
Achin Gupta191e86e2014-05-09 10:03:15 +010046 ******************************************************************************/
47static int32_t validate_interrupt_type(uint32_t type)
48{
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +010049 if ((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_NS) ||
50 (type == INTR_TYPE_EL3))
Soby Mathew58e32d12015-11-23 13:58:45 +000051 return 0;
Achin Gupta191e86e2014-05-09 10:03:15 +010052
Soby Mathew58e32d12015-11-23 13:58:45 +000053 return -EINVAL;
Achin Gupta191e86e2014-05-09 10:03:15 +010054}
55
56/*******************************************************************************
57* This function validates the routing model for this type of interrupt
58 ******************************************************************************/
59static int32_t validate_routing_model(uint32_t type, uint32_t flags)
60{
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +010061 uint32_t rm_flags = (flags >> INTR_RM_FLAGS_SHIFT) & INTR_RM_FLAGS_MASK;
Achin Gupta191e86e2014-05-09 10:03:15 +010062
63 if (type == INTR_TYPE_S_EL1)
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +010064 return validate_sel1_interrupt_rm(rm_flags);
Achin Gupta191e86e2014-05-09 10:03:15 +010065
66 if (type == INTR_TYPE_NS)
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +010067 return validate_ns_interrupt_rm(rm_flags);
Achin Gupta191e86e2014-05-09 10:03:15 +010068
Soby Mathew58e32d12015-11-23 13:58:45 +000069 if (type == INTR_TYPE_EL3)
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +010070 return validate_el3_interrupt_rm(rm_flags);
Soby Mathew58e32d12015-11-23 13:58:45 +000071
Achin Gupta191e86e2014-05-09 10:03:15 +010072 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 ******************************************************************************/
80uint32_t get_scr_el3_from_routing_model(uint32_t security_state)
81{
82 uint32_t scr_el3;
83
Juan Castillof558cac2014-06-05 09:45:36 +010084 assert(sec_state_is_valid(security_state));
Achin Gupta191e86e2014-05-09 10:03:15 +010085 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 ******************************************************************************/
97static 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;
Juan Castillo53c51842015-10-30 14:53:24 +0000106
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100107 /*
108 * Update scr_el3 only if there is a context available. If not, it
Juan Castillo53c51842015-10-30 14:53:24 +0000109 * will be updated later during context initialization which will obtain
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100110 * the scr_el3 value to be used via get_scr_el3_from_routing_model()
111 */
112 if (cm_get_context(security_state) != NULL)
Juan Castillo53c51842015-10-30 14:53:24 +0000113 cm_write_scr_el3_bit(security_state, bit_pos, flag);
Achin Gupta191e86e2014-05-09 10:03:15 +0100114}
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 ******************************************************************************/
122int32_t set_routing_model(uint32_t type, uint32_t flags)
123{
124 int32_t rc;
125
126 rc = validate_interrupt_type(type);
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100127 if (rc != 0)
Achin Gupta191e86e2014-05-09 10:03:15 +0100128 return rc;
129
130 rc = validate_routing_model(type, flags);
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100131 if (rc != 0)
Achin Gupta191e86e2014-05-09 10:03:15 +0100132 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
Soby Mathew47903c02015-01-13 15:48:26 +0000142/******************************************************************************
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 *****************************************************************************/
148int disable_intr_rm_local(uint32_t type, uint32_t security_state)
149{
150 uint32_t bit_pos, flag;
151
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100152 assert(intr_type_descs[type].handler != NULL);
Soby Mathew47903c02015-01-13 15:48:26 +0000153
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 *****************************************************************************/
166int enable_intr_rm_local(uint32_t type, uint32_t security_state)
167{
168 uint32_t bit_pos, flag;
169
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100170 assert(intr_type_descs[type].handler != NULL);
Soby Mathew47903c02015-01-13 15:48:26 +0000171
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
Achin Gupta191e86e2014-05-09 10:03:15 +0100181/*******************************************************************************
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 ******************************************************************************/
186int32_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 */
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100193 if (handler == NULL)
Achin Gupta191e86e2014-05-09 10:03:15 +0100194 return -EINVAL;
195
196 /* Validate the 'flags' parameter */
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100197 if ((flags & INTR_TYPE_FLAGS_MASK) != 0U)
Achin Gupta191e86e2014-05-09 10:03:15 +0100198 return -EINVAL;
199
200 /* Check if a handler has already been registered */
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100201 if (intr_type_descs[type].handler != NULL)
Achin Gupta191e86e2014-05-09 10:03:15 +0100202 return -EALREADY;
203
204 rc = set_routing_model(type, flags);
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100205 if (rc != 0)
Achin Gupta191e86e2014-05-09 10:03:15 +0100206 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 ******************************************************************************/
219interrupt_type_handler_t get_interrupt_type_handler(uint32_t type)
220{
Antonio Nino Diaze0b757d2018-08-24 16:30:29 +0100221 if (validate_interrupt_type(type) != 0)
Achin Gupta191e86e2014-05-09 10:03:15 +0100222 return NULL;
223
224 return intr_type_descs[type].handler;
225}
226