interrupt_mgmt.c 8.1 KB

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