interrupt.c 7.9 KB

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