interrupt_mgmt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2014-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef INTERRUPT_MGMT_H
  7. #define INTERRUPT_MGMT_H
  8. #include <arch.h>
  9. #include <lib/utils_def.h>
  10. /*******************************************************************************
  11. * Constants for the types of interrupts recognised by the IM framework
  12. ******************************************************************************/
  13. #define INTR_TYPE_S_EL1 U(0)
  14. #define INTR_TYPE_EL3 U(1)
  15. #define INTR_TYPE_NS U(2)
  16. #define MAX_INTR_TYPES U(3)
  17. #define INTR_TYPE_INVAL MAX_INTR_TYPES
  18. /* Interrupt routing modes */
  19. #define INTR_ROUTING_MODE_PE 0
  20. #define INTR_ROUTING_MODE_ANY 1
  21. /*
  22. * Constant passed to the interrupt handler in the 'id' field when the
  23. * framework does not read the gic registers to determine the interrupt id.
  24. */
  25. #define INTR_ID_UNAVAILABLE U(0xFFFFFFFF)
  26. /*******************************************************************************
  27. * Mask for _both_ the routing model bits in the 'flags' parameter and
  28. * constants to define the valid routing models for each supported interrupt
  29. * type
  30. ******************************************************************************/
  31. #define INTR_RM_FLAGS_SHIFT U(0x0)
  32. #define INTR_RM_FLAGS_MASK U(0x3)
  33. /* Routed to EL3 from NS. Taken to S-EL1 from Secure */
  34. #define INTR_SEL1_VALID_RM0 U(0x2)
  35. /* Routed to EL3 from NS and Secure */
  36. #define INTR_SEL1_VALID_RM1 U(0x3)
  37. /* Routed to EL1/EL2 from NS and to S-EL1 from Secure */
  38. #define INTR_NS_VALID_RM0 U(0x0)
  39. /* Routed to EL1/EL2 from NS and to EL3 from Secure */
  40. #define INTR_NS_VALID_RM1 U(0x1)
  41. /* Routed to EL3 from NS. Taken to S-EL1 from Secure and handed over to EL3 */
  42. #define INTR_EL3_VALID_RM0 U(0x2)
  43. /* Routed to EL3 from NS and Secure */
  44. #define INTR_EL3_VALID_RM1 U(0x3)
  45. /* This is the default routing model */
  46. #define INTR_DEFAULT_RM U(0x0)
  47. /*******************************************************************************
  48. * Constants for the _individual_ routing model bits in the 'flags' field for
  49. * each interrupt type and mask to validate the 'flags' parameter while
  50. * registering an interrupt handler
  51. ******************************************************************************/
  52. #define INTR_TYPE_FLAGS_MASK U(0xFFFFFFFC)
  53. #define INTR_RM_FROM_SEC_SHIFT SECURE /* BIT[0] */
  54. #define INTR_RM_FROM_NS_SHIFT NON_SECURE /* BIT[1] */
  55. #define INTR_RM_FROM_FLAG_MASK U(1)
  56. #define get_interrupt_rm_flag(flag, ss) \
  57. ((((flag) >> INTR_RM_FLAGS_SHIFT) >> (ss)) & INTR_RM_FROM_FLAG_MASK)
  58. #define set_interrupt_rm_flag(flag, ss) ((flag) |= U(1) << (ss))
  59. #define clr_interrupt_rm_flag(flag, ss) ((flag) &= ~(U(1) << (ss)))
  60. /*******************************************************************************
  61. * Macros to set the 'flags' parameter passed to an interrupt type handler. Only
  62. * the flag to indicate the security state when the exception was generated is
  63. * supported.
  64. ******************************************************************************/
  65. #define INTR_SRC_SS_FLAG_SHIFT U(0) /* BIT[0] */
  66. #define INTR_SRC_SS_FLAG_MASK U(1)
  67. #define set_interrupt_src_ss(flag, val) ((flag) |= (val) << INTR_SRC_SS_FLAG_SHIFT)
  68. #define clr_interrupt_src_ss(flag) ((flag) &= ~(U(1) << INTR_SRC_SS_FLAG_SHIFT))
  69. #define get_interrupt_src_ss(flag) (((flag) >> INTR_SRC_SS_FLAG_SHIFT) & \
  70. INTR_SRC_SS_FLAG_MASK)
  71. #ifndef __ASSEMBLER__
  72. #include <errno.h>
  73. #include <stdint.h>
  74. /*******************************************************************************
  75. * Helpers to validate the routing model bits in the 'flags' for a type
  76. * of interrupt. If the model does not match one of the valid masks
  77. * -EINVAL is returned.
  78. ******************************************************************************/
  79. static inline int32_t validate_sel1_interrupt_rm(uint32_t x)
  80. {
  81. if ((x == INTR_SEL1_VALID_RM0) || (x == INTR_SEL1_VALID_RM1))
  82. return 0;
  83. return -EINVAL;
  84. }
  85. static inline int32_t validate_ns_interrupt_rm(uint32_t x)
  86. {
  87. if ((x == INTR_NS_VALID_RM0) || (x == INTR_NS_VALID_RM1))
  88. return 0;
  89. return -EINVAL;
  90. }
  91. static inline int32_t validate_el3_interrupt_rm(uint32_t x)
  92. {
  93. #if EL3_EXCEPTION_HANDLING && SPM_MM
  94. /*
  95. * With EL3 exception handling, EL3 interrupts are always routed to EL3
  96. * from Non-secure and from secure only if SPM_MM is present.
  97. * Therefore INTR_EL3_VALID_RM1 is the only valid routing model.
  98. */
  99. if (x == INTR_EL3_VALID_RM1)
  100. return 0;
  101. #else
  102. /*
  103. * When EL3_EXCEPTION_HANDLING is not defined both routing modes are
  104. * valid. This is the most common case. The exception to this rule is
  105. * when EL3_EXCEPTION_HANDLING is defined but also when the SPMC lives
  106. * at S-EL2. In this case, Group0 Interrupts are trapped to the SPMC
  107. * when running in S-EL0 and S-EL1. The SPMC may handle the interrupt
  108. * itself, delegate it to an SP or forward to EL3 for handling.
  109. */
  110. if ((x == INTR_EL3_VALID_RM0) || (x == INTR_EL3_VALID_RM1))
  111. return 0;
  112. #endif
  113. return -EINVAL;
  114. }
  115. /*******************************************************************************
  116. * Prototype for defining a handler for an interrupt type
  117. ******************************************************************************/
  118. typedef uint64_t (*interrupt_type_handler_t)(uint32_t id,
  119. uint32_t flags,
  120. void *handle,
  121. void *cookie);
  122. /*******************************************************************************
  123. * Function & variable prototypes
  124. ******************************************************************************/
  125. u_register_t get_scr_el3_from_routing_model(uint32_t security_state);
  126. int32_t set_routing_model(uint32_t type, uint32_t flags);
  127. int32_t register_interrupt_type_handler(uint32_t type,
  128. interrupt_type_handler_t handler,
  129. uint32_t flags);
  130. interrupt_type_handler_t get_interrupt_type_handler(uint32_t type);
  131. int disable_intr_rm_local(uint32_t type, uint32_t security_state);
  132. int enable_intr_rm_local(uint32_t type, uint32_t security_state);
  133. #endif /*__ASSEMBLER__*/
  134. #endif /* INTERRUPT_MGMT_H */