interrupt.c 7.5 KB

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