interrupt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. */
  9. #include <rthw.h>
  10. #include <plic_driver.h>
  11. #include <platform.h>
  12. #include <encoding.h>
  13. #include <interrupt.h>
  14. #define MAX_HANDLERS PLIC_NUM_INTERRUPTS
  15. /* exception and interrupt handler table */
  16. static struct rt_irq_desc irq_desc[MAX_HANDLERS];
  17. static plic_instance_t g_plic;
  18. /**
  19. * This function will mask a interrupt.
  20. * @param vector the interrupt number
  21. */
  22. void rt_hw_interrupt_mask(int irq)
  23. {
  24. PLIC_disable_interrupt(&g_plic, irq);
  25. }
  26. /**
  27. * This function will un-mask a interrupt.
  28. * @param vector the interrupt number
  29. */
  30. void rt_hw_interrupt_unmask(int irq)
  31. {
  32. PLIC_enable_interrupt(&g_plic, irq);
  33. PLIC_set_priority(&g_plic, irq, 1);
  34. }
  35. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  36. {
  37. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  38. return RT_NULL;
  39. }
  40. void rt_hw_interrupt_init(void)
  41. {
  42. int idx;
  43. /* config interrupt vector*/
  44. asm volatile(
  45. "la t0, trap_entry\n"
  46. "csrw mtvec, t0"
  47. );
  48. /* enable global interrupt*/
  49. PLIC_init(&g_plic,
  50. PLIC_CTRL_ADDR,
  51. PLIC_NUM_INTERRUPTS,
  52. PLIC_NUM_PRIORITIES);
  53. /* init exceptions table */
  54. for (idx = 0; idx < MAX_HANDLERS; idx++)
  55. {
  56. rt_hw_interrupt_mask(idx);
  57. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  58. irq_desc[idx].param = RT_NULL;
  59. #ifdef RT_USING_INTERRUPT_INFO
  60. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  61. irq_desc[idx].counter = 0;
  62. #endif
  63. }
  64. // enable machine external interrupt
  65. set_csr(mie, MIP_MEIP);
  66. }
  67. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  68. {
  69. return (rt_uint32_t)PLIC_claim_interrupt(&g_plic);
  70. }
  71. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  72. {
  73. PLIC_complete_interrupt(&g_plic, id);
  74. }
  75. /**
  76. * This function will install a interrupt service routine to a interrupt.
  77. * @param vector the interrupt number
  78. * @param handler the interrupt service routine to be installed
  79. * @param param the interrupt service function parameter
  80. * @param name the interrupt name
  81. * @return old handler
  82. */
  83. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  84. void *param, const char *name)
  85. {
  86. rt_isr_handler_t old_handler = RT_NULL;
  87. if(vector < MAX_HANDLERS)
  88. {
  89. old_handler = irq_desc[vector].handler;
  90. if (handler != RT_NULL)
  91. {
  92. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  93. irq_desc[vector].param = param;
  94. #ifdef RT_USING_INTERRUPT_INFO
  95. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  96. irq_desc[vector].counter = 0;
  97. #endif
  98. }
  99. }
  100. return old_handler;
  101. }
  102. /**
  103. * This function will be call when external machine-level
  104. * interrupt from PLIC occurred.
  105. */
  106. void handle_m_ext_interrupt(void)
  107. {
  108. rt_isr_handler_t isr_func;
  109. rt_uint32_t irq;
  110. void *param;
  111. /* get irq number */
  112. irq = rt_hw_interrupt_get_active(0);
  113. /* get interrupt service routine */
  114. isr_func = irq_desc[irq].handler;
  115. param = irq_desc[irq].param;
  116. /* turn to interrupt service routine */
  117. isr_func(irq, param);
  118. rt_hw_interrupt_ack(0, irq);
  119. #ifdef RT_USING_INTERRUPT_INFO
  120. irq_desc[irq].counter ++;
  121. #endif
  122. }