plat_gicv3.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. * Portions copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <stdbool.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <bl31/interrupt_mgmt.h>
  13. #include <drivers/arm/gic_common.h>
  14. #include <drivers/arm/gicv3.h>
  15. #include <lib/cassert.h>
  16. #include <plat/common/platform.h>
  17. #ifdef IMAGE_BL31
  18. /*
  19. * The following platform GIC functions are weakly defined. They
  20. * provide typical implementations that may be re-used by multiple
  21. * platforms but may also be overridden by a platform if required.
  22. */
  23. #pragma weak plat_ic_get_pending_interrupt_id
  24. #pragma weak plat_ic_get_pending_interrupt_type
  25. #pragma weak plat_ic_acknowledge_interrupt
  26. #pragma weak plat_ic_get_interrupt_type
  27. #pragma weak plat_ic_end_of_interrupt
  28. #pragma weak plat_interrupt_type_to_line
  29. #pragma weak plat_ic_get_running_priority
  30. #pragma weak plat_ic_is_spi
  31. #pragma weak plat_ic_is_ppi
  32. #pragma weak plat_ic_is_sgi
  33. #pragma weak plat_ic_get_interrupt_active
  34. #pragma weak plat_ic_enable_interrupt
  35. #pragma weak plat_ic_disable_interrupt
  36. #pragma weak plat_ic_set_interrupt_priority
  37. #pragma weak plat_ic_set_interrupt_type
  38. #pragma weak plat_ic_raise_el3_sgi
  39. #pragma weak plat_ic_raise_ns_sgi
  40. #pragma weak plat_ic_raise_s_el1_sgi
  41. #pragma weak plat_ic_set_spi_routing
  42. #pragma weak plat_ic_set_interrupt_pending
  43. #pragma weak plat_ic_clear_interrupt_pending
  44. /*
  45. * This function returns the highest priority pending interrupt at
  46. * the Interrupt controller
  47. */
  48. uint32_t plat_ic_get_pending_interrupt_id(void)
  49. {
  50. unsigned int irqnr;
  51. assert(IS_IN_EL3());
  52. irqnr = gicv3_get_pending_interrupt_id();
  53. return gicv3_is_intr_id_special_identifier(irqnr) ?
  54. INTR_ID_UNAVAILABLE : irqnr;
  55. }
  56. /*
  57. * This function returns the type of the highest priority pending interrupt
  58. * at the Interrupt controller. In the case of GICv3, the Highest Priority
  59. * Pending interrupt system register (`ICC_HPPIR0_EL1`) is read to determine
  60. * the id of the pending interrupt. The type of interrupt depends upon the
  61. * id value as follows.
  62. * 1. id = PENDING_G1S_INTID (1020) is reported as a S-EL1 interrupt
  63. * 2. id = PENDING_G1NS_INTID (1021) is reported as a Non-secure interrupt.
  64. * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt
  65. * type.
  66. * 4. All other interrupt id's are reported as EL3 interrupt.
  67. */
  68. uint32_t plat_ic_get_pending_interrupt_type(void)
  69. {
  70. unsigned int irqnr;
  71. uint32_t type;
  72. assert(IS_IN_EL3());
  73. irqnr = gicv3_get_pending_interrupt_type();
  74. switch (irqnr) {
  75. case PENDING_G1S_INTID:
  76. type = INTR_TYPE_S_EL1;
  77. break;
  78. case PENDING_G1NS_INTID:
  79. type = INTR_TYPE_NS;
  80. break;
  81. case GIC_SPURIOUS_INTERRUPT:
  82. type = INTR_TYPE_INVAL;
  83. break;
  84. default:
  85. type = INTR_TYPE_EL3;
  86. break;
  87. }
  88. return type;
  89. }
  90. /*
  91. * This function returns the highest priority pending interrupt at
  92. * the Interrupt controller and indicates to the Interrupt controller
  93. * that the interrupt processing has started.
  94. */
  95. uint32_t plat_ic_acknowledge_interrupt(void)
  96. {
  97. assert(IS_IN_EL3());
  98. return gicv3_acknowledge_interrupt();
  99. }
  100. /*
  101. * This function returns the type of the interrupt `id`, depending on how
  102. * the interrupt has been configured in the interrupt controller.
  103. */
  104. uint32_t plat_ic_get_interrupt_type(uint32_t id)
  105. {
  106. unsigned int group;
  107. assert(IS_IN_EL3());
  108. group = gicv3_get_interrupt_group(id, plat_my_core_pos());
  109. switch (group) {
  110. case INTR_GROUP0:
  111. return INTR_TYPE_EL3;
  112. case INTR_GROUP1S:
  113. return INTR_TYPE_S_EL1;
  114. case INTR_GROUP1NS:
  115. return INTR_TYPE_NS;
  116. default:
  117. assert(false); /* Unreachable */
  118. return INTR_TYPE_EL3;
  119. }
  120. }
  121. /*
  122. * This functions is used to indicate to the interrupt controller that
  123. * the processing of the interrupt corresponding to the `id` has
  124. * finished.
  125. */
  126. void plat_ic_end_of_interrupt(uint32_t id)
  127. {
  128. assert(IS_IN_EL3());
  129. gicv3_end_of_interrupt(id);
  130. }
  131. /*
  132. * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
  133. * The interrupt controller knows which pin/line it uses to signal a type of
  134. * interrupt. It lets the interrupt management framework determine for a type of
  135. * interrupt and security state, which line should be used in the SCR_EL3 to
  136. * control its routing to EL3. The interrupt line is represented as the bit
  137. * position of the IRQ or FIQ bit in the SCR_EL3.
  138. */
  139. uint32_t plat_interrupt_type_to_line(uint32_t type,
  140. uint32_t security_state)
  141. {
  142. assert((type == INTR_TYPE_S_EL1) ||
  143. (type == INTR_TYPE_EL3) ||
  144. (type == INTR_TYPE_NS));
  145. assert(sec_state_is_valid(security_state));
  146. assert(IS_IN_EL3());
  147. switch (type) {
  148. case INTR_TYPE_S_EL1:
  149. /*
  150. * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 contexts
  151. * and as FIQ in the NS-EL0/1/2 contexts
  152. */
  153. if (security_state == SECURE)
  154. return __builtin_ctz(SCR_IRQ_BIT);
  155. else
  156. return __builtin_ctz(SCR_FIQ_BIT);
  157. assert(0); /* Unreachable */
  158. case INTR_TYPE_NS:
  159. /*
  160. * The Non secure interrupts will be signaled as FIQ in S-EL0/1
  161. * contexts and as IRQ in the NS-EL0/1/2 contexts.
  162. */
  163. if (security_state == SECURE)
  164. return __builtin_ctz(SCR_FIQ_BIT);
  165. else
  166. return __builtin_ctz(SCR_IRQ_BIT);
  167. assert(0); /* Unreachable */
  168. case INTR_TYPE_EL3:
  169. /*
  170. * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and
  171. * NS-EL0/1/2 contexts
  172. */
  173. return __builtin_ctz(SCR_FIQ_BIT);
  174. default:
  175. panic();
  176. }
  177. }
  178. unsigned int plat_ic_get_running_priority(void)
  179. {
  180. return gicv3_get_running_priority();
  181. }
  182. int plat_ic_is_spi(unsigned int id)
  183. {
  184. return (id >= MIN_SPI_ID) && (id <= MAX_SPI_ID);
  185. }
  186. int plat_ic_is_ppi(unsigned int id)
  187. {
  188. return (id >= MIN_PPI_ID) && (id < MIN_SPI_ID);
  189. }
  190. int plat_ic_is_sgi(unsigned int id)
  191. {
  192. return (id >= MIN_SGI_ID) && (id < MIN_PPI_ID);
  193. }
  194. unsigned int plat_ic_get_interrupt_active(unsigned int id)
  195. {
  196. return gicv3_get_interrupt_active(id, plat_my_core_pos());
  197. }
  198. void plat_ic_enable_interrupt(unsigned int id)
  199. {
  200. gicv3_enable_interrupt(id, plat_my_core_pos());
  201. }
  202. void plat_ic_disable_interrupt(unsigned int id)
  203. {
  204. gicv3_disable_interrupt(id, plat_my_core_pos());
  205. }
  206. void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority)
  207. {
  208. gicv3_set_interrupt_priority(id, plat_my_core_pos(), priority);
  209. }
  210. bool plat_ic_has_interrupt_type(unsigned int type)
  211. {
  212. if ((type == INTR_TYPE_EL3) || (type == INTR_TYPE_S_EL1) ||
  213. (type == INTR_TYPE_NS)) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. void plat_ic_set_interrupt_type(unsigned int id, unsigned int type)
  219. {
  220. unsigned int group;
  221. switch (type) {
  222. case INTR_TYPE_EL3:
  223. group = INTR_GROUP0;
  224. break;
  225. case INTR_TYPE_S_EL1:
  226. group = INTR_GROUP1S;
  227. break;
  228. case INTR_TYPE_NS:
  229. group = INTR_GROUP1NS;
  230. break;
  231. default:
  232. assert(false); /* Unreachable */
  233. group = INTR_GROUP0;
  234. break;
  235. }
  236. gicv3_set_interrupt_group(id, plat_my_core_pos(), group);
  237. }
  238. void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target)
  239. {
  240. /* Target must be a valid MPIDR in the system */
  241. assert(plat_core_pos_by_mpidr(target) >= 0);
  242. /* Verify that this is a secure EL3 SGI */
  243. assert(plat_ic_get_interrupt_type((unsigned int)sgi_num) ==
  244. INTR_TYPE_EL3);
  245. gicv3_raise_sgi((unsigned int)sgi_num, GICV3_G0, target);
  246. }
  247. void plat_ic_raise_ns_sgi(int sgi_num, u_register_t target)
  248. {
  249. /* Target must be a valid MPIDR in the system */
  250. assert(plat_core_pos_by_mpidr(target) >= 0);
  251. /* Verify that this is a non-secure SGI */
  252. assert(plat_ic_get_interrupt_type((unsigned int)sgi_num) ==
  253. INTR_TYPE_NS);
  254. gicv3_raise_sgi((unsigned int)sgi_num, GICV3_G1NS, target);
  255. }
  256. void plat_ic_raise_s_el1_sgi(int sgi_num, u_register_t target)
  257. {
  258. /* Target must be a valid MPIDR in the system */
  259. assert(plat_core_pos_by_mpidr(target) >= 0);
  260. /* Verify that this is a secure EL1 SGI */
  261. assert(plat_ic_get_interrupt_type((unsigned int)sgi_num) ==
  262. INTR_TYPE_S_EL1);
  263. gicv3_raise_sgi((unsigned int)sgi_num, GICV3_G1S, target);
  264. }
  265. void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode,
  266. u_register_t mpidr)
  267. {
  268. unsigned int irm = 0;
  269. switch (routing_mode) {
  270. case INTR_ROUTING_MODE_PE:
  271. assert(plat_core_pos_by_mpidr(mpidr) >= 0);
  272. irm = GICV3_IRM_PE;
  273. break;
  274. case INTR_ROUTING_MODE_ANY:
  275. irm = GICV3_IRM_ANY;
  276. break;
  277. default:
  278. assert(0); /* Unreachable */
  279. break;
  280. }
  281. gicv3_set_spi_routing(id, irm, mpidr);
  282. }
  283. void plat_ic_set_interrupt_pending(unsigned int id)
  284. {
  285. /* Disallow setting SGIs pending */
  286. assert(id >= MIN_PPI_ID);
  287. gicv3_set_interrupt_pending(id, plat_my_core_pos());
  288. }
  289. void plat_ic_clear_interrupt_pending(unsigned int id)
  290. {
  291. /* Disallow setting SGIs pending */
  292. assert(id >= MIN_PPI_ID);
  293. gicv3_clear_interrupt_pending(id, plat_my_core_pos());
  294. }
  295. unsigned int plat_ic_set_priority_mask(unsigned int mask)
  296. {
  297. return gicv3_set_pmr(mask);
  298. }
  299. unsigned int plat_ic_deactivate_priority(unsigned int mask)
  300. {
  301. return gicv3_deactivate_priority(mask);
  302. }
  303. unsigned int plat_ic_get_interrupt_id(unsigned int raw)
  304. {
  305. unsigned int id = raw & INT_ID_MASK;
  306. return gicv3_is_intr_id_special_identifier(id) ?
  307. INTR_ID_UNAVAILABLE : id;
  308. }
  309. #endif
  310. #ifdef IMAGE_BL32
  311. #pragma weak plat_ic_get_pending_interrupt_id
  312. #pragma weak plat_ic_acknowledge_interrupt
  313. #pragma weak plat_ic_end_of_interrupt
  314. /* In AArch32, the secure group1 interrupts are targeted to Secure PL1 */
  315. #ifndef __aarch64__
  316. #define IS_IN_EL1() IS_IN_SECURE()
  317. #endif
  318. /*
  319. * This function returns the highest priority pending interrupt at
  320. * the Interrupt controller
  321. */
  322. uint32_t plat_ic_get_pending_interrupt_id(void)
  323. {
  324. unsigned int irqnr;
  325. assert(IS_IN_EL1());
  326. irqnr = gicv3_get_pending_interrupt_id_sel1();
  327. return (irqnr == GIC_SPURIOUS_INTERRUPT) ?
  328. INTR_ID_UNAVAILABLE : irqnr;
  329. }
  330. /*
  331. * This function returns the highest priority pending interrupt at
  332. * the Interrupt controller and indicates to the Interrupt controller
  333. * that the interrupt processing has started.
  334. */
  335. uint32_t plat_ic_acknowledge_interrupt(void)
  336. {
  337. assert(IS_IN_EL1());
  338. return gicv3_acknowledge_interrupt_sel1();
  339. }
  340. /*
  341. * This functions is used to indicate to the interrupt controller that
  342. * the processing of the interrupt corresponding to the `id` has
  343. * finished.
  344. */
  345. void plat_ic_end_of_interrupt(uint32_t id)
  346. {
  347. assert(IS_IN_EL1());
  348. gicv3_end_of_interrupt_sel1(id);
  349. }
  350. #endif