interrupt.c 4.3 KB

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