interrupt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. * 2018/5/3 Bernard first version
  9. * 2019-07-28 zdzn add smp support
  10. * 2019-08-09 zhangjun fixup the problem of smp startup and scheduling issues,
  11. * write addr to mailbox3 to startup smp, and we use mailbox0 for ipi
  12. * 2021-07-31 GuEe-GUI Add gic pl390 branch
  13. */
  14. #include <rthw.h>
  15. #include <board.h>
  16. #include <rtthread.h>
  17. #include "cp15.h"
  18. #include "armv8.h"
  19. #include "interrupt.h"
  20. #ifdef BSP_USING_GIC390
  21. #include <gic_pl390.h>
  22. #endif
  23. #define MAX_HANDLERS 72
  24. #ifdef RT_USING_SMP
  25. #define rt_interrupt_nest rt_cpu_self()->irq_nest
  26. #else
  27. extern volatile rt_uint8_t rt_interrupt_nest;
  28. #endif
  29. extern int system_vectors;
  30. /* exception and interrupt handler table */
  31. struct rt_irq_desc isr_table[MAX_HANDLERS];
  32. rt_ubase_t rt_interrupt_from_thread;
  33. rt_ubase_t rt_interrupt_to_thread;
  34. rt_ubase_t rt_thread_switch_interrupt_flag;
  35. void rt_hw_vector_init(void)
  36. {
  37. rt_hw_set_current_vbar((rt_ubase_t)&system_vectors); // cpu_gcc.S
  38. }
  39. #ifndef BSP_USING_GIC
  40. static void default_isr_handler(int vector, void *param)
  41. {
  42. #ifdef RT_USING_SMP
  43. rt_kprintf("cpu %d unhandled irq: %d\n", rt_hw_cpu_id(),vector);
  44. #else
  45. rt_kprintf("unhandled irq: %d\n",vector);
  46. #endif
  47. }
  48. #endif
  49. /**
  50. * This function will initialize hardware interrupt
  51. */
  52. void rt_hw_interrupt_init(void)
  53. {
  54. #ifdef BSP_USING_GIC390
  55. arm_gic_irq_init();
  56. #else
  57. rt_uint32_t index;
  58. /* mask all of interrupts */
  59. IRQ_DISABLE_BASIC = 0x000000ff;
  60. IRQ_DISABLE1 = 0xffffffff;
  61. IRQ_DISABLE2 = 0xffffffff;
  62. for (index = 0; index < MAX_HANDLERS; index ++)
  63. {
  64. isr_table[index].handler = default_isr_handler;
  65. isr_table[index].param = NULL;
  66. #ifdef RT_USING_INTERRUPT_INFO
  67. rt_strncpy(isr_table[index].name, "unknown", RT_NAME_MAX);
  68. isr_table[index].counter = 0;
  69. #endif
  70. }
  71. /* init interrupt nest, and context in thread sp */
  72. rt_interrupt_nest = 0;
  73. rt_interrupt_from_thread = 0;
  74. rt_interrupt_to_thread = 0;
  75. rt_thread_switch_interrupt_flag = 0;
  76. #endif
  77. }
  78. /**
  79. * This function will mask a interrupt.
  80. * @param vector the interrupt number
  81. */
  82. void rt_hw_interrupt_mask(int vector)
  83. {
  84. #ifdef BSP_USING_GIC390
  85. arm_gic_mask(vector);
  86. #else
  87. if (vector < 32)
  88. {
  89. IRQ_DISABLE1 = (1 << vector);
  90. }
  91. else if (vector<64)
  92. {
  93. vector = vector % 32;
  94. IRQ_DISABLE2 = (1 << vector);
  95. }
  96. else
  97. {
  98. vector = vector - 64;
  99. IRQ_DISABLE_BASIC = (1 << vector);
  100. }
  101. #endif
  102. }
  103. /**
  104. * This function will un-mask a interrupt.
  105. * @param vector the interrupt number
  106. */
  107. void rt_hw_interrupt_umask(int vector)
  108. {
  109. #ifdef BSP_USING_GIC390
  110. arm_gic_umask(vector);
  111. #else
  112. if (vector < 32)
  113. {
  114. IRQ_ENABLE1 = (1 << vector);
  115. }
  116. else if (vector < 64)
  117. {
  118. vector = vector % 32;
  119. IRQ_ENABLE2 = (1 << vector);
  120. }
  121. else
  122. {
  123. vector = vector - 64;
  124. IRQ_ENABLE_BASIC = (1 << vector);
  125. }
  126. #endif
  127. }
  128. /**
  129. * This function will install a interrupt service routine to a interrupt.
  130. * @param vector the interrupt number
  131. * @param new_handler the interrupt service routine to be installed
  132. * @param old_handler the old interrupt service routine
  133. */
  134. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  135. void *param, const char *name)
  136. {
  137. rt_isr_handler_t old_handler = RT_NULL;
  138. if (vector < MAX_HANDLERS)
  139. {
  140. old_handler = isr_table[vector].handler;
  141. if (handler != RT_NULL)
  142. {
  143. #ifdef RT_USING_INTERRUPT_INFO
  144. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  145. #endif /* RT_USING_INTERRUPT_INFO */
  146. isr_table[vector].handler = handler;
  147. isr_table[vector].param = param;
  148. }
  149. }
  150. return old_handler;
  151. }
  152. #ifdef RT_USING_SMP
  153. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
  154. {
  155. __DSB();
  156. if(cpu_mask & 0x1)
  157. {
  158. send_ipi_msg(0, ipi_vector);
  159. }
  160. if(cpu_mask & 0x2)
  161. {
  162. send_ipi_msg(1, ipi_vector);
  163. }
  164. if(cpu_mask & 0x4)
  165. {
  166. send_ipi_msg(2, ipi_vector);
  167. }
  168. if(cpu_mask & 0x8)
  169. {
  170. send_ipi_msg(3, ipi_vector);
  171. }
  172. __DSB();
  173. }
  174. #endif
  175. #ifdef RT_USING_SMP
  176. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler)
  177. {
  178. /* note: ipi_vector maybe different with irq_vector */
  179. rt_hw_interrupt_install(ipi_vector, ipi_isr_handler, 0, "IPI_HANDLER");
  180. }
  181. #endif