Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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> |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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 | /******************************************************************************* |
Soby Mathew | 58e32d1 | 2015-11-23 13:58:45 +0000 | [diff] [blame] | 45 | * This function validates the interrupt type. |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 46 | ******************************************************************************/ |
| 47 | static int32_t validate_interrupt_type(uint32_t type) |
| 48 | { |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 49 | if ((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_NS) || |
| 50 | (type == INTR_TYPE_EL3)) |
Soby Mathew | 58e32d1 | 2015-11-23 13:58:45 +0000 | [diff] [blame] | 51 | return 0; |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 52 | |
Soby Mathew | 58e32d1 | 2015-11-23 13:58:45 +0000 | [diff] [blame] | 53 | return -EINVAL; |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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 | { |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 61 | uint32_t rm_flags = (flags >> INTR_RM_FLAGS_SHIFT) & INTR_RM_FLAGS_MASK; |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 62 | |
| 63 | if (type == INTR_TYPE_S_EL1) |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 64 | return validate_sel1_interrupt_rm(rm_flags); |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 65 | |
| 66 | if (type == INTR_TYPE_NS) |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 67 | return validate_ns_interrupt_rm(rm_flags); |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 68 | |
Soby Mathew | 58e32d1 | 2015-11-23 13:58:45 +0000 | [diff] [blame] | 69 | if (type == INTR_TYPE_EL3) |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 70 | return validate_el3_interrupt_rm(rm_flags); |
Soby Mathew | 58e32d1 | 2015-11-23 13:58:45 +0000 | [diff] [blame] | 71 | |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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 | |
Juan Castillo | f558cac | 2014-06-05 09:45:36 +0100 | [diff] [blame] | 84 | assert(sec_state_is_valid(security_state)); |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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; |
Juan Castillo | 53c5184 | 2015-10-30 14:53:24 +0000 | [diff] [blame] | 106 | |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 107 | /* |
| 108 | * Update scr_el3 only if there is a context available. If not, it |
Juan Castillo | 53c5184 | 2015-10-30 14:53:24 +0000 | [diff] [blame] | 109 | * will be updated later during context initialization which will obtain |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 110 | * the scr_el3 value to be used via get_scr_el3_from_routing_model() |
| 111 | */ |
| 112 | if (cm_get_context(security_state) != NULL) |
Juan Castillo | 53c5184 | 2015-10-30 14:53:24 +0000 | [diff] [blame] | 113 | cm_write_scr_el3_bit(security_state, bit_pos, flag); |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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); |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 127 | if (rc != 0) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 128 | return rc; |
| 129 | |
| 130 | rc = validate_routing_model(type, flags); |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 131 | if (rc != 0) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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 | |
Soby Mathew | 47903c0 | 2015-01-13 15:48:26 +0000 | [diff] [blame] | 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 | |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 152 | assert(intr_type_descs[type].handler != NULL); |
Soby Mathew | 47903c0 | 2015-01-13 15:48:26 +0000 | [diff] [blame] | 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 | |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 170 | assert(intr_type_descs[type].handler != NULL); |
Soby Mathew | 47903c0 | 2015-01-13 15:48:26 +0000 | [diff] [blame] | 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 | |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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 */ |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 193 | if (handler == NULL) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 194 | return -EINVAL; |
| 195 | |
| 196 | /* Validate the 'flags' parameter */ |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 197 | if ((flags & INTR_TYPE_FLAGS_MASK) != 0U) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | |
| 200 | /* Check if a handler has already been registered */ |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 201 | if (intr_type_descs[type].handler != NULL) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 202 | return -EALREADY; |
| 203 | |
| 204 | rc = set_routing_model(type, flags); |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 205 | if (rc != 0) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 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 | { |
Antonio Nino Diaz | e0b757d | 2018-08-24 16:30:29 +0100 | [diff] [blame^] | 221 | if (validate_interrupt_type(type) != 0) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 222 | return NULL; |
| 223 | |
| 224 | return intr_type_descs[type].handler; |
| 225 | } |
| 226 | |