interrupt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-07-06 Bernard first version
  9. * 2018-11-22 Jesven add smp support
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "interrupt.h"
  14. #ifdef RT_USING_GIC_V2
  15. #include "gic.h"
  16. #else
  17. #include "gicv3.h"
  18. #endif
  19. /* exception and interrupt handler table */
  20. struct rt_irq_desc isr_table[MAX_HANDLERS];
  21. #ifndef RT_USING_SMP
  22. /* Those varibles will be accessed in ISR, so we need to share them. */
  23. rt_uint32_t rt_interrupt_from_thread = 0;
  24. rt_uint32_t rt_interrupt_to_thread = 0;
  25. rt_uint32_t rt_thread_switch_interrupt_flag = 0;
  26. #endif
  27. const unsigned int VECTOR_BASE = 0x00;
  28. extern void rt_cpu_vector_set_base(unsigned int addr);
  29. extern int system_vectors;
  30. void rt_hw_vector_init(void)
  31. {
  32. rt_cpu_vector_set_base((unsigned int)&system_vectors);
  33. }
  34. #ifdef RT_USING_GIC_V2
  35. /**
  36. * This function will initialize hardware interrupt
  37. */
  38. void rt_hw_interrupt_init(void)
  39. {
  40. rt_uint32_t gic_cpu_base;
  41. rt_uint32_t gic_dist_base;
  42. rt_uint32_t gic_irq_start;
  43. /* initialize vector table */
  44. rt_hw_vector_init();
  45. /* initialize exceptions table */
  46. rt_memset(isr_table, 0x00, sizeof(isr_table));
  47. /* initialize ARM GIC */
  48. gic_dist_base = platform_get_gic_dist_base();
  49. gic_cpu_base = platform_get_gic_cpu_base();
  50. gic_irq_start = GIC_IRQ_START;
  51. arm_gic_dist_init(0, gic_dist_base, gic_irq_start);
  52. arm_gic_cpu_init(0, gic_cpu_base);
  53. }
  54. #else
  55. /**
  56. * This function will initialize hardware interrupt
  57. * Called by the primary cpu(cpu0)
  58. */
  59. void rt_hw_interrupt_init(void)
  60. {
  61. rt_uint32_t gic_dist_base;
  62. rt_uint32_t gic_irq_start;
  63. /* initialize vector table */
  64. rt_hw_vector_init();
  65. /* initialize exceptions table */
  66. rt_memset(isr_table, 0x00, sizeof(isr_table));
  67. /* initialize ARM GIC */
  68. gic_dist_base = platform_get_gic_dist_base();
  69. gic_irq_start = GIC_IRQ_START;
  70. arm_gic_dist_init(0, gic_dist_base, gic_irq_start);
  71. arm_gic_cpu_init(0);
  72. arm_gic_redist_init(0);
  73. }
  74. #endif
  75. /**
  76. * This function will mask a interrupt.
  77. * @param vector the interrupt number
  78. */
  79. void rt_hw_interrupt_mask(int vector)
  80. {
  81. arm_gic_mask(0, vector);
  82. }
  83. /**
  84. * This function will un-mask a interrupt.
  85. * @param vector the interrupt number
  86. */
  87. void rt_hw_interrupt_umask(int vector)
  88. {
  89. arm_gic_umask(0, vector);
  90. }
  91. /**
  92. * This function returns the active interrupt number.
  93. * @param none
  94. */
  95. int rt_hw_interrupt_get_irq(void)
  96. {
  97. return arm_gic_get_active_irq(0);
  98. }
  99. /**
  100. * This function acknowledges the interrupt.
  101. * @param vector the interrupt number
  102. */
  103. void rt_hw_interrupt_ack(int vector)
  104. {
  105. arm_gic_ack(0, vector);
  106. }
  107. /**
  108. * This function set interrupt CPU targets.
  109. * @param vector: the interrupt number
  110. * cpu_mask: target cpus mask, one bit for one core
  111. */
  112. void rt_hw_interrupt_set_target_cpus(int vector, unsigned int cpu_mask)
  113. {
  114. arm_gic_set_cpu(0, vector, cpu_mask);
  115. }
  116. /**
  117. * This function get interrupt CPU targets.
  118. * @param vector: the interrupt number
  119. * @return target cpus mask, one bit for one core
  120. */
  121. unsigned int rt_hw_interrupt_get_target_cpus(int vector)
  122. {
  123. return arm_gic_get_target_cpu(0, vector);
  124. }
  125. /**
  126. * This function set interrupt triger mode.
  127. * @param vector: the interrupt number
  128. * mode: interrupt triger mode; 0: level triger, 1: edge triger
  129. */
  130. void rt_hw_interrupt_set_triger_mode(int vector, unsigned int mode)
  131. {
  132. arm_gic_set_configuration(0, vector, mode);
  133. }
  134. /**
  135. * This function get interrupt triger mode.
  136. * @param vector: the interrupt number
  137. * @return interrupt triger mode; 0: level triger, 1: edge triger
  138. */
  139. unsigned int rt_hw_interrupt_get_triger_mode(int vector)
  140. {
  141. return arm_gic_get_configuration(0, vector);
  142. }
  143. /**
  144. * This function set interrupt pending flag.
  145. * @param vector: the interrupt number
  146. */
  147. void rt_hw_interrupt_set_pending(int vector)
  148. {
  149. arm_gic_set_pending_irq(0, vector);
  150. }
  151. /**
  152. * This function get interrupt pending flag.
  153. * @param vector: the interrupt number
  154. * @return interrupt pending flag, 0: not pending; 1: pending
  155. */
  156. unsigned int rt_hw_interrupt_get_pending(int vector)
  157. {
  158. return arm_gic_get_pending_irq(0, vector);
  159. }
  160. /**
  161. * This function clear interrupt pending flag.
  162. * @param vector: the interrupt number
  163. */
  164. void rt_hw_interrupt_clear_pending(int vector)
  165. {
  166. arm_gic_clear_pending_irq(0, vector);
  167. }
  168. /**
  169. * This function set interrupt priority value.
  170. * @param vector: the interrupt number
  171. * priority: the priority of interrupt to set
  172. */
  173. void rt_hw_interrupt_set_priority(int vector, unsigned int priority)
  174. {
  175. arm_gic_set_priority(0, vector, priority);
  176. }
  177. /**
  178. * This function get interrupt priority.
  179. * @param vector: the interrupt number
  180. * @return interrupt priority value
  181. */
  182. unsigned int rt_hw_interrupt_get_priority(int vector)
  183. {
  184. return arm_gic_get_priority(0, vector);
  185. }
  186. /**
  187. * This function set priority masking threshold.
  188. * @param priority: priority masking threshold
  189. */
  190. void rt_hw_interrupt_set_priority_mask(unsigned int priority)
  191. {
  192. arm_gic_set_interface_prior_mask(0, priority);
  193. }
  194. /**
  195. * This function get priority masking threshold.
  196. * @param none
  197. * @return priority masking threshold
  198. */
  199. unsigned int rt_hw_interrupt_get_priority_mask(void)
  200. {
  201. return arm_gic_get_interface_prior_mask(0);
  202. }
  203. /**
  204. * This function set priority grouping field split point.
  205. * @param bits: priority grouping field split point
  206. * @return 0: success; -1: failed
  207. */
  208. int rt_hw_interrupt_set_prior_group_bits(unsigned int bits)
  209. {
  210. int status;
  211. if (bits < 8)
  212. {
  213. arm_gic_set_binary_point(0, (7 - bits));
  214. status = 0;
  215. }
  216. else
  217. {
  218. status = -1;
  219. }
  220. return (status);
  221. }
  222. /**
  223. * This function get priority grouping field split point.
  224. * @param none
  225. * @return priority grouping field split point
  226. */
  227. unsigned int rt_hw_interrupt_get_prior_group_bits(void)
  228. {
  229. unsigned int bp;
  230. bp = arm_gic_get_binary_point(0) & 0x07;
  231. return (7 - bp);
  232. }
  233. /**
  234. * This function will install a interrupt service routine to a interrupt.
  235. * @param vector the interrupt number
  236. * @param new_handler the interrupt service routine to be installed
  237. * @param old_handler the old interrupt service routine
  238. */
  239. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  240. void *param, const char *name)
  241. {
  242. rt_isr_handler_t old_handler = RT_NULL;
  243. if (vector < MAX_HANDLERS)
  244. {
  245. old_handler = isr_table[vector].handler;
  246. if (handler != RT_NULL)
  247. {
  248. #ifdef RT_USING_INTERRUPT_INFO
  249. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  250. #endif /* RT_USING_INTERRUPT_INFO */
  251. isr_table[vector].handler = handler;
  252. isr_table[vector].param = param;
  253. }
  254. }
  255. return old_handler;
  256. }
  257. #ifdef RT_USING_SMP
  258. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
  259. {
  260. #ifdef RT_USING_GIC_V2
  261. arm_gic_send_sgi(0, ipi_vector, cpu_mask, 0);
  262. #else
  263. arm_gic_send_affinity_sgi(0, ipi_vector, cpu_mask, ROUTED_TO_SPEC);
  264. #endif
  265. }
  266. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler)
  267. {
  268. /* note: ipi_vector maybe different with irq_vector */
  269. rt_hw_interrupt_install(ipi_vector, ipi_isr_handler, 0, "IPI_HANDLER");
  270. }
  271. #endif