interrupt.c 4.2 KB

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