interrupt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "gic.h"
  15. #include "gicv3.h"
  16. #include "armv8.h"
  17. #include "mmu.h"
  18. #include "cpuport.h"
  19. /* exception and interrupt handler table */
  20. struct rt_irq_desc isr_table[MAX_HANDLERS];
  21. /* Those variables will be accessed in ISR, so we need to share them. */
  22. rt_ubase_t rt_interrupt_from_thread = 0;
  23. rt_ubase_t rt_interrupt_to_thread = 0;
  24. rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  25. extern int system_vectors;
  26. void rt_hw_vector_init(void)
  27. {
  28. rt_hw_set_current_vbar((rt_ubase_t)&system_vectors);
  29. }
  30. /**
  31. * This function will initialize hardware interrupt
  32. */
  33. void rt_hw_interrupt_init(void)
  34. {
  35. /* initialize vector table */
  36. rt_hw_vector_init();
  37. /* initialize exceptions table */
  38. rt_memset(isr_table, 0x00, sizeof(isr_table));
  39. #ifndef BSP_USING_GIC
  40. /* mask all of interrupts */
  41. IRQ_DISABLE_BASIC = 0x000000ff;
  42. IRQ_DISABLE1 = 0xffffffff;
  43. IRQ_DISABLE2 = 0xffffffff;
  44. #else
  45. /* initialize ARM GIC */
  46. arm_gic_dist_init(0, platform_get_gic_dist_base(), GIC_IRQ_START);
  47. arm_gic_cpu_init(0, platform_get_gic_cpu_base());
  48. #ifdef BSP_USING_GICV3
  49. arm_gic_redist_init(0, platform_get_gic_redist_base());
  50. #endif
  51. #endif
  52. }
  53. /**
  54. * This function will mask a interrupt.
  55. * @param vector the interrupt number
  56. */
  57. void rt_hw_interrupt_mask(int vector)
  58. {
  59. #ifndef BSP_USING_GIC
  60. if (vector < 32)
  61. {
  62. IRQ_DISABLE1 = (1 << vector);
  63. }
  64. else if (vector < 64)
  65. {
  66. vector = vector % 32;
  67. IRQ_DISABLE2 = (1 << vector);
  68. }
  69. else
  70. {
  71. vector = vector - 64;
  72. IRQ_DISABLE_BASIC = (1 << vector);
  73. }
  74. #else
  75. arm_gic_mask(0, vector);
  76. #endif
  77. }
  78. /**
  79. * This function will un-mask a interrupt.
  80. * @param vector the interrupt number
  81. */
  82. void rt_hw_interrupt_umask(int vector)
  83. {
  84. #ifndef BSP_USING_GIC
  85. if (vector < 32)
  86. {
  87. IRQ_ENABLE1 = (1 << vector);
  88. }
  89. else if (vector < 64)
  90. {
  91. vector = vector % 32;
  92. IRQ_ENABLE2 = (1 << vector);
  93. }
  94. else
  95. {
  96. vector = vector - 64;
  97. IRQ_ENABLE_BASIC = (1 << vector);
  98. }
  99. #else
  100. arm_gic_umask(0, vector);
  101. #endif
  102. }
  103. /**
  104. * This function returns the active interrupt number.
  105. * @param none
  106. */
  107. int rt_hw_interrupt_get_irq(void)
  108. {
  109. #ifdef BSP_USING_GIC
  110. return arm_gic_get_active_irq(0);
  111. #else
  112. return 0;
  113. #endif
  114. }
  115. /**
  116. * This function acknowledges the interrupt.
  117. * @param vector the interrupt number
  118. */
  119. void rt_hw_interrupt_ack(int vector)
  120. {
  121. #ifdef BSP_USING_GIC
  122. arm_gic_ack(0, vector);
  123. #endif
  124. }
  125. /**
  126. * This function set interrupt CPU targets.
  127. * @param vector: the interrupt number
  128. * cpu_mask: target cpus mask, one bit for one core
  129. */
  130. void rt_hw_interrupt_set_target_cpus(int vector, unsigned int cpu_mask)
  131. {
  132. #ifdef BSP_USING_GIC
  133. arm_gic_set_cpu(0, vector, cpu_mask);
  134. #endif
  135. }
  136. /**
  137. * This function get interrupt CPU targets.
  138. * @param vector: the interrupt number
  139. * @return target cpus mask, one bit for one core
  140. */
  141. unsigned int rt_hw_interrupt_get_target_cpus(int vector)
  142. {
  143. #ifdef BSP_USING_GIC
  144. return arm_gic_get_target_cpu(0, vector);
  145. #else
  146. return -RT_ERROR;
  147. #endif
  148. }
  149. /**
  150. * This function set interrupt triger mode.
  151. * @param vector: the interrupt number
  152. * mode: interrupt triger mode; 0: level triger, 1: edge triger
  153. */
  154. void rt_hw_interrupt_set_triger_mode(int vector, unsigned int mode)
  155. {
  156. #ifdef BSP_USING_GIC
  157. arm_gic_set_configuration(0, vector, mode);
  158. #endif
  159. }
  160. /**
  161. * This function get interrupt triger mode.
  162. * @param vector: the interrupt number
  163. * @return interrupt triger mode; 0: level triger, 1: edge triger
  164. */
  165. unsigned int rt_hw_interrupt_get_triger_mode(int vector)
  166. {
  167. #ifdef BSP_USING_GIC
  168. return arm_gic_get_configuration(0, vector);
  169. #else
  170. return -RT_ERROR;
  171. #endif
  172. }
  173. /**
  174. * This function set interrupt pending flag.
  175. * @param vector: the interrupt number
  176. */
  177. void rt_hw_interrupt_set_pending(int vector)
  178. {
  179. #ifdef BSP_USING_GIC
  180. arm_gic_set_pending_irq(0, vector);
  181. #endif
  182. }
  183. /**
  184. * This function get interrupt pending flag.
  185. * @param vector: the interrupt number
  186. * @return interrupt pending flag, 0: not pending; 1: pending
  187. */
  188. unsigned int rt_hw_interrupt_get_pending(int vector)
  189. {
  190. #ifdef BSP_USING_GIC
  191. return arm_gic_get_pending_irq(0, vector);
  192. #else
  193. return -RT_ERROR;
  194. #endif
  195. }
  196. /**
  197. * This function clear interrupt pending flag.
  198. * @param vector: the interrupt number
  199. */
  200. void rt_hw_interrupt_clear_pending(int vector)
  201. {
  202. #ifdef BSP_USING_GIC
  203. arm_gic_clear_pending_irq(0, vector);
  204. #endif
  205. }
  206. /**
  207. * This function set interrupt priority value.
  208. * @param vector: the interrupt number
  209. * priority: the priority of interrupt to set
  210. */
  211. void rt_hw_interrupt_set_priority(int vector, unsigned int priority)
  212. {
  213. #ifdef BSP_USING_GIC
  214. arm_gic_set_priority(0, vector, priority);
  215. #endif
  216. }
  217. /**
  218. * This function get interrupt priority.
  219. * @param vector: the interrupt number
  220. * @return interrupt priority value
  221. */
  222. unsigned int rt_hw_interrupt_get_priority(int vector)
  223. {
  224. #ifdef BSP_USING_GIC
  225. return arm_gic_get_priority(0, vector);
  226. #else
  227. return -RT_ERROR;
  228. #endif
  229. }
  230. /**
  231. * This function set priority masking threshold.
  232. * @param priority: priority masking threshold
  233. */
  234. void rt_hw_interrupt_set_priority_mask(unsigned int priority)
  235. {
  236. #ifdef BSP_USING_GIC
  237. arm_gic_set_interface_prior_mask(0, priority);
  238. #endif
  239. }
  240. /**
  241. * This function get priority masking threshold.
  242. * @param none
  243. * @return priority masking threshold
  244. */
  245. unsigned int rt_hw_interrupt_get_priority_mask(void)
  246. {
  247. #ifdef BSP_USING_GIC
  248. return arm_gic_get_interface_prior_mask(0);
  249. #else
  250. return -RT_ERROR;
  251. #endif
  252. }
  253. /**
  254. * This function set priority grouping field split point.
  255. * @param bits: priority grouping field split point
  256. * @return 0: success; -1: failed
  257. */
  258. int rt_hw_interrupt_set_prior_group_bits(unsigned int bits)
  259. {
  260. #ifdef BSP_USING_GIC
  261. int status;
  262. if (bits < 8)
  263. {
  264. arm_gic_set_binary_point(0, (7 - bits));
  265. status = 0;
  266. }
  267. else
  268. {
  269. status = -1;
  270. }
  271. return (status);
  272. #else
  273. return -RT_ERROR;
  274. #endif
  275. }
  276. /**
  277. * This function get priority grouping field split point.
  278. * @param none
  279. * @return priority grouping field split point
  280. */
  281. unsigned int rt_hw_interrupt_get_prior_group_bits(void)
  282. {
  283. #ifdef BSP_USING_GIC
  284. unsigned int bp;
  285. bp = arm_gic_get_binary_point(0) & 0x07;
  286. return (7 - bp);
  287. #else
  288. return -RT_ERROR;
  289. #endif
  290. }
  291. /**
  292. * This function will install a interrupt service routine to a interrupt.
  293. * @param vector the interrupt number
  294. * @param new_handler the interrupt service routine to be installed
  295. * @param old_handler the old interrupt service routine
  296. */
  297. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  298. void *param, const char *name)
  299. {
  300. rt_isr_handler_t old_handler = RT_NULL;
  301. if (vector < MAX_HANDLERS)
  302. {
  303. old_handler = isr_table[vector].handler;
  304. if (handler != RT_NULL)
  305. {
  306. #ifdef RT_USING_INTERRUPT_INFO
  307. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  308. #endif /* RT_USING_INTERRUPT_INFO */
  309. isr_table[vector].handler = handler;
  310. isr_table[vector].param = param;
  311. }
  312. }
  313. return old_handler;
  314. }
  315. #ifdef RT_USING_SMP
  316. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
  317. {
  318. #ifdef BSP_USING_GIC
  319. #ifdef BSP_USING_GICV2
  320. arm_gic_send_sgi(0, ipi_vector, cpu_mask, 0);
  321. #else
  322. arm_gic_send_affinity_sgi(0, ipi_vector, (rt_uint64_t *)&cpu_mask, GICV3_ROUTED_TO_SPEC);
  323. #endif
  324. #else
  325. int i;
  326. __DSB();
  327. for (i = 0; i < RT_CPUS_NR; ++i)
  328. {
  329. if (cpu_mask & (1 << i))
  330. {
  331. IPI_MAILBOX_SET(i) = 1 << ipi_vector;
  332. }
  333. }
  334. __DSB();
  335. #endif
  336. }
  337. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler)
  338. {
  339. /* note: ipi_vector maybe different with irq_vector */
  340. rt_hw_interrupt_install(ipi_vector, ipi_isr_handler, 0, "IPI_HANDLER");
  341. }
  342. #endif