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