interrupt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * 2018/10/01 Bernard The first version
  9. */
  10. #include <rthw.h>
  11. #include "tick.h"
  12. #include <plic.h>
  13. #include <clint.h>
  14. #include <interrupt.h>
  15. #define CPU_NUM 2
  16. #define MAX_HANDLERS IRQN_MAX
  17. static struct rt_irq_desc irq_desc[CPU_NUM][MAX_HANDLERS];
  18. static rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  19. {
  20. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  21. return RT_NULL;
  22. }
  23. /**
  24. * This function will initialize hardware interrupt
  25. */
  26. void rt_hw_interrupt_init(void)
  27. {
  28. int idx;
  29. int cpuid;
  30. cpuid = current_coreid();
  31. /* Disable all interrupts for the current core. */
  32. for (idx = 0; idx < ((PLIC_NUM_SOURCES + 32u) / 32u); idx ++)
  33. plic->target_enables.target[cpuid].enable[idx] = 0;
  34. /* Set priorities to zero. */
  35. for (idx = 0; idx < PLIC_NUM_SOURCES; idx++)
  36. plic->source_priorities.priority[idx] = 0;
  37. /* Set the threshold to zero. */
  38. plic->targets.target[cpuid].priority_threshold = 0;
  39. /* init exceptions table */
  40. for (idx = 0; idx < MAX_HANDLERS; idx++)
  41. {
  42. rt_hw_interrupt_mask(idx);
  43. irq_desc[cpuid][idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  44. irq_desc[cpuid][idx].param = RT_NULL;
  45. #ifdef RT_USING_INTERRUPT_INFO
  46. rt_snprintf(irq_desc[cpuid][idx].name, RT_NAME_MAX - 1, "default");
  47. irq_desc[idx][cpuid].counter = 0;
  48. #endif
  49. }
  50. /* Enable machine external interrupts. */
  51. set_csr(mie, MIP_MEIP);
  52. }
  53. /**
  54. * This function will mask a interrupt.
  55. * @param vector the interrupt number
  56. */
  57. void rt_hw_interrupt_mask(int vector)
  58. {
  59. plic_irq_disable(vector);
  60. }
  61. /**
  62. * This function will un-mask a interrupt.
  63. * @param vector the interrupt number
  64. */
  65. void rt_hw_interrupt_umask(int vector)
  66. {
  67. plic_set_priority(vector, 1);
  68. plic_irq_enable(vector);
  69. }
  70. /**
  71. * This function will install a interrupt service routine to a interrupt.
  72. * @param vector the interrupt number
  73. * @param new_handler the interrupt service routine to be installed
  74. * @param old_handler the old interrupt service routine
  75. */
  76. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  77. void *param, const char *name)
  78. {
  79. int cpuid;
  80. rt_isr_handler_t old_handler = RT_NULL;
  81. cpuid = current_coreid();
  82. if(vector < MAX_HANDLERS)
  83. {
  84. old_handler = irq_desc[cpuid][vector].handler;
  85. if (handler != RT_NULL)
  86. {
  87. irq_desc[cpuid][vector].handler = (rt_isr_handler_t)handler;
  88. irq_desc[cpuid][vector].param = param;
  89. #ifdef RT_USING_INTERRUPT_INFO
  90. rt_snprintf(irq_desc[cpuid][vector].name, RT_NAME_MAX - 1, "%s", name);
  91. irq_desc[cpuid][vector].counter = 0;
  92. #endif
  93. }
  94. }
  95. return old_handler;
  96. }
  97. uintptr_t handle_irq_m_ext(uintptr_t cause, uintptr_t epc)
  98. {
  99. /*
  100. * After the highest-priority pending interrupt is claimed by a target
  101. * and the corresponding IP bit is cleared, other lower-priority
  102. * pending interrupts might then become visible to the target, and so
  103. * the PLIC EIP bit might not be cleared after a claim. The interrupt
  104. * handler can check the local meip/heip/seip/ueip bits before exiting
  105. * the handler, to allow more efficient service of other interrupts
  106. * without first restoring the interrupted context and taking another
  107. * interrupt trap.
  108. */
  109. if (read_csr(mip) & MIP_MEIP)
  110. {
  111. /* Get current core id */
  112. uint64_t core_id = current_coreid();
  113. /* Get primitive interrupt enable flag */
  114. uint64_t ie_flag = read_csr(mie);
  115. /* Get current IRQ num */
  116. uint32_t int_num = plic->targets.target[core_id].claim_complete;
  117. /* Get primitive IRQ threshold */
  118. uint32_t int_threshold = plic->targets.target[core_id].priority_threshold;
  119. /* Set new IRQ threshold = current IRQ threshold */
  120. plic->targets.target[core_id].priority_threshold = plic->source_priorities.priority[int_num];
  121. /* Disable software interrupt and timer interrupt */
  122. clear_csr(mie, MIP_MTIP | MIP_MSIP);
  123. if (irq_desc[core_id][int_num].handler)
  124. {
  125. irq_desc[core_id][int_num].handler(int_num, irq_desc[core_id][int_num].param);
  126. }
  127. /* Perform IRQ complete */
  128. plic->targets.target[core_id].claim_complete = int_num;
  129. /* Set MPIE and MPP flag used to MRET instructions restore MIE flag */
  130. set_csr(mstatus, MSTATUS_MPIE | MSTATUS_MPP);
  131. /* Restore primitive interrupt enable flag */
  132. write_csr(mie, ie_flag);
  133. /* Restore primitive IRQ threshold */
  134. plic->targets.target[core_id].priority_threshold = int_threshold;
  135. }
  136. else
  137. {
  138. rt_kprintf("unhandled trap!\n");
  139. }
  140. return epc;
  141. }
  142. uintptr_t handle_trap(uintptr_t mcause, uintptr_t epc)
  143. {
  144. int cause = mcause & CAUSE_MACHINE_IRQ_REASON_MASK;
  145. if (mcause & (1UL << 63))
  146. {
  147. switch (cause)
  148. {
  149. case IRQ_M_SOFT:
  150. break;
  151. case IRQ_M_EXT:
  152. handle_irq_m_ext(mcause, epc);
  153. break;
  154. case IRQ_M_TIMER:
  155. tick_isr();
  156. break;
  157. }
  158. }
  159. else
  160. {
  161. rt_thread_t tid;
  162. extern long list_thread();
  163. rt_hw_interrupt_disable();
  164. tid = rt_thread_self();
  165. rt_kprintf("\n");
  166. rt_kprintf("unhandled trap, epc => 0x%08x, INT[%d]\n", epc, rt_interrupt_get_nest());
  167. rt_kprintf("current thread: %.*s\n", RT_NAME_MAX, tid->name);
  168. #ifdef RT_USING_FINSH
  169. list_thread();
  170. #endif
  171. while(1);
  172. }
  173. return epc;
  174. }