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