interrupt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2006-2018, 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 <board.h>
  14. #include "interrupt.h"
  15. #include "gic.h"
  16. /* exception and interrupt handler table */
  17. struct rt_irq_desc isr_table[MAX_HANDLERS];
  18. #ifndef RT_USING_SMP
  19. /* Those variables will be accessed in ISR, so we need to share them. */
  20. rt_uint32_t rt_interrupt_from_thread = 0;
  21. rt_uint32_t rt_interrupt_to_thread = 0;
  22. rt_uint32_t rt_thread_switch_interrupt_flag = 0;
  23. #endif
  24. const unsigned int VECTOR_BASE = 0x00;
  25. extern void rt_cpu_vector_set_base(unsigned int addr);
  26. extern int system_vectors;
  27. #ifdef RT_USING_SMP
  28. #define rt_interrupt_nest rt_cpu_self()->irq_nest
  29. #else
  30. extern volatile rt_uint8_t rt_interrupt_nest;
  31. #endif
  32. #ifdef SOC_BCM283x
  33. static void default_isr_handler(int vector, void *param)
  34. {
  35. #ifdef RT_USING_SMP
  36. rt_kprintf("cpu %d unhandled irq: %d\n", rt_hw_cpu_id(),vector);
  37. #else
  38. rt_kprintf("unhandled irq: %d\n",vector);
  39. #endif
  40. }
  41. #endif
  42. void rt_hw_vector_init(void)
  43. {
  44. rt_cpu_vector_set_base((unsigned int)&system_vectors);
  45. }
  46. /**
  47. * This function will initialize hardware interrupt
  48. */
  49. void rt_hw_interrupt_init(void)
  50. {
  51. #ifdef SOC_BCM283x
  52. rt_uint32_t index;
  53. /* initialize vector table */
  54. rt_hw_vector_init();
  55. /* initialize exceptions table */
  56. rt_memset(isr_table, 0x00, sizeof(isr_table));
  57. /* mask all of interrupts */
  58. IRQ_DISABLE_BASIC = 0x000000ff;
  59. IRQ_DISABLE1 = 0xffffffff;
  60. IRQ_DISABLE2 = 0xffffffff;
  61. for (index = 0; index < MAX_HANDLERS; index ++)
  62. {
  63. isr_table[index].handler = default_isr_handler;
  64. isr_table[index].param = RT_NULL;
  65. #ifdef RT_USING_INTERRUPT_INFO
  66. rt_strncpy(isr_table[index].name, "unknown", RT_NAME_MAX);
  67. isr_table[index].counter = 0;
  68. #endif
  69. }
  70. /* init interrupt nest, and context in thread sp */
  71. rt_interrupt_nest = 0;
  72. rt_interrupt_from_thread = 0;
  73. rt_interrupt_to_thread = 0;
  74. rt_thread_switch_interrupt_flag = 0;
  75. #else
  76. rt_uint32_t gic_cpu_base;
  77. rt_uint32_t gic_dist_base;
  78. rt_uint32_t gic_irq_start;
  79. /* initialize vector table */
  80. rt_hw_vector_init();
  81. /* initialize exceptions table */
  82. rt_memset(isr_table, 0x00, sizeof(isr_table));
  83. /* initialize ARM GIC */
  84. #ifdef RT_USING_USERSPACE
  85. gic_dist_base = (uint32_t)rt_hw_mmu_map(&mmu_info, 0, (void*)platform_get_gic_dist_base(), 0x2000, MMU_MAP_K_RW);
  86. gic_cpu_base = (uint32_t)rt_hw_mmu_map(&mmu_info, 0, (void*)platform_get_gic_cpu_base(), 0x1000, MMU_MAP_K_RW);
  87. #else
  88. gic_dist_base = platform_get_gic_dist_base();
  89. gic_cpu_base = platform_get_gic_cpu_base();
  90. #endif
  91. gic_irq_start = GIC_IRQ_START;
  92. arm_gic_dist_init(0, gic_dist_base, gic_irq_start);
  93. arm_gic_cpu_init(0, gic_cpu_base);
  94. #endif
  95. }
  96. /**
  97. * This function will mask a interrupt.
  98. * @param vector the interrupt number
  99. */
  100. void rt_hw_interrupt_mask(int vector)
  101. {
  102. #ifdef SOC_BCM283x
  103. if (vector < 32)
  104. {
  105. IRQ_DISABLE1 = (1 << vector);
  106. }
  107. else if (vector < 64)
  108. {
  109. vector = vector % 32;
  110. IRQ_DISABLE2 = (1 << vector);
  111. }
  112. else
  113. {
  114. vector = vector - 64;
  115. IRQ_DISABLE_BASIC = (1 << vector);
  116. }
  117. #else
  118. arm_gic_mask(0, vector);
  119. #endif
  120. }
  121. /**
  122. * This function will un-mask a interrupt.
  123. * @param vector the interrupt number
  124. */
  125. void rt_hw_interrupt_umask(int vector)
  126. {
  127. #ifdef SOC_BCM283x
  128. if (vector < 32)
  129. {
  130. IRQ_ENABLE1 = (1 << vector);
  131. }
  132. else if (vector < 64)
  133. {
  134. vector = vector % 32;
  135. IRQ_ENABLE2 = (1 << vector);
  136. }
  137. else
  138. {
  139. vector = vector - 64;
  140. IRQ_ENABLE_BASIC = (1 << vector);
  141. }
  142. #else
  143. arm_gic_umask(0, vector);
  144. #endif
  145. }
  146. /**
  147. * This function returns the active interrupt number.
  148. * @param none
  149. */
  150. int rt_hw_interrupt_get_irq(void)
  151. {
  152. #ifndef SOC_BCM283x
  153. return arm_gic_get_active_irq(0) & GIC_ACK_INTID_MASK;
  154. #else
  155. return 0;
  156. #endif
  157. }
  158. /**
  159. * This function acknowledges the interrupt.
  160. * @param vector the interrupt number
  161. */
  162. void rt_hw_interrupt_ack(int vector)
  163. {
  164. #ifndef SOC_BCM283x
  165. arm_gic_ack(0, vector);
  166. #endif
  167. }
  168. /**
  169. * This function will install a interrupt service routine to a interrupt.
  170. * @param vector the interrupt number
  171. * @param new_handler the interrupt service routine to be installed
  172. * @param old_handler the old interrupt service routine
  173. */
  174. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  175. void *param, const char *name)
  176. {
  177. rt_isr_handler_t old_handler = RT_NULL;
  178. if (vector < MAX_HANDLERS)
  179. {
  180. old_handler = isr_table[vector].handler;
  181. if (handler != RT_NULL)
  182. {
  183. #ifdef RT_USING_INTERRUPT_INFO
  184. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  185. #endif /* RT_USING_INTERRUPT_INFO */
  186. isr_table[vector].handler = handler;
  187. isr_table[vector].param = param;
  188. }
  189. }
  190. return old_handler;
  191. }