interrupt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-16 bigmagic first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <gic_pl400.h>
  13. #include <board.h>
  14. #include <armv8.h>
  15. #define MAX_HANDLERS 256
  16. #define GIC_ACK_INTID_MASK 0x000003ff
  17. #ifdef RT_USING_SMP
  18. #define rt_interrupt_nest rt_cpu_self()->irq_nest
  19. #else
  20. extern volatile rt_uint8_t rt_interrupt_nest;
  21. #endif
  22. extern int system_vectors;
  23. /* exception and interrupt handler table */
  24. struct rt_irq_desc isr_table[MAX_HANDLERS];
  25. rt_ubase_t rt_interrupt_from_thread;
  26. rt_ubase_t rt_interrupt_to_thread;
  27. rt_ubase_t rt_thread_switch_interrupt_flag;
  28. void rt_hw_vector_init(void)
  29. {
  30. rt_hw_set_current_vbar((rt_ubase_t)&system_vectors); // cpu_gcc.S
  31. }
  32. /**
  33. * This function will initialize hardware interrupt
  34. */
  35. void rt_hw_interrupt_init(void)
  36. {
  37. rt_uint32_t gic_cpu_base = 0;
  38. rt_uint32_t gic_dist_base = 0;
  39. /* initialize ARM GIC */
  40. gic_dist_base = GIC_PL400_DISTRIBUTOR_PPTR;
  41. gic_cpu_base = GIC_PL400_CONTROLLER_PPTR;
  42. arm_gic_dist_init(0, gic_dist_base, 0);
  43. arm_gic_cpu_init(0, gic_cpu_base);
  44. }
  45. /**
  46. * This function will install a interrupt service routine to a interrupt.
  47. * @param vector the interrupt number
  48. * @param new_handler the interrupt service routine to be installed
  49. * @param old_handler the old interrupt service routine
  50. */
  51. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  52. void *param, const char *name)
  53. {
  54. rt_isr_handler_t old_handler = RT_NULL;
  55. if (vector < MAX_HANDLERS)
  56. {
  57. old_handler = isr_table[vector].handler;
  58. if (handler != RT_NULL)
  59. {
  60. #ifdef RT_USING_INTERRUPT_INFO
  61. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  62. #endif /* RT_USING_INTERRUPT_INFO */
  63. isr_table[vector].handler = handler;
  64. isr_table[vector].param = param;
  65. }
  66. }
  67. return old_handler;
  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. arm_gic_mask(0, vector);
  76. }
  77. /**
  78. * This function will un-mask a interrupt.
  79. * @param vector the interrupt number
  80. */
  81. void rt_hw_interrupt_umask(int vector)
  82. {
  83. arm_gic_umask(0, vector);
  84. }
  85. /**
  86. * This function returns the active interrupt number.
  87. * @param none
  88. */
  89. int rt_hw_interrupt_get_irq(void)
  90. {
  91. return arm_gic_get_active_irq(0) & GIC_ACK_INTID_MASK;
  92. }
  93. /**
  94. * This function acknowledges the interrupt.
  95. * @param vector the interrupt number
  96. */
  97. void rt_hw_interrupt_ack(int vector)
  98. {
  99. arm_gic_ack(0, vector);
  100. }