interrupt.c 9.0 KB

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