interrupt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * 2014-04-03 Grissiom port to VMM
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <irq_numbers.h>
  14. #include <interrupt.h>
  15. #include <gic.h>
  16. #include "cp15.h"
  17. #define MAX_HANDLERS IMX_INTERRUPT_COUNT
  18. extern volatile rt_uint8_t rt_interrupt_nest;
  19. /* exception and interrupt handler table */
  20. struct rt_irq_desc isr_table[MAX_HANDLERS];
  21. rt_uint32_t rt_interrupt_from_thread;
  22. rt_uint32_t rt_interrupt_to_thread;
  23. rt_uint32_t rt_thread_switch_interrupt_flag;
  24. extern void rt_cpu_vector_set_base(unsigned int addr);
  25. extern int system_vectors;
  26. /* keep compatible with platform SDK */
  27. void register_interrupt_routine(uint32_t irq_id, irq_hdlr_t isr)
  28. {
  29. rt_hw_interrupt_install(irq_id, (rt_isr_handler_t)isr, NULL, "unknown");
  30. }
  31. void enable_interrupt(uint32_t irq_id, uint32_t cpu_id, uint32_t priority)
  32. {
  33. gic_set_irq_priority(irq_id, priority);
  34. gic_set_irq_security(irq_id, false); // set IRQ as non-secure
  35. gic_set_cpu_target(irq_id, cpu_id, true);
  36. gic_enable_irq(irq_id, true);
  37. }
  38. void disable_interrupt(uint32_t irq_id, uint32_t cpu_id)
  39. {
  40. gic_enable_irq(irq_id, false);
  41. gic_set_cpu_target(irq_id, cpu_id, false);
  42. }
  43. static void rt_hw_vector_init(void)
  44. {
  45. int sctrl;
  46. unsigned int *src = (unsigned int *)&system_vectors;
  47. /* C12-C0 is only active when SCTLR.V = 0 */
  48. asm volatile ("mrc p15, #0, %0, c1, c0, #0"
  49. :"=r" (sctrl));
  50. sctrl &= ~(1 << 13);
  51. asm volatile ("mcr p15, #0, %0, c1, c0, #0"
  52. :
  53. :"r" (sctrl));
  54. asm volatile ("mcr p15, #0, %0, c12, c0, #0"
  55. :
  56. :"r" (src));
  57. }
  58. /**
  59. * This function will initialize hardware interrupt
  60. */
  61. void rt_hw_interrupt_init(void)
  62. {
  63. rt_hw_vector_init();
  64. gic_init();
  65. /* init interrupt nest, and context in thread sp */
  66. rt_interrupt_nest = 0;
  67. rt_interrupt_from_thread = 0;
  68. rt_interrupt_to_thread = 0;
  69. rt_thread_switch_interrupt_flag = 0;
  70. }
  71. /**
  72. * This function will mask a interrupt.
  73. * @param vector the interrupt number
  74. */
  75. void rt_hw_interrupt_mask(int vector)
  76. {
  77. disable_interrupt(vector, 0);
  78. }
  79. /**
  80. * This function will un-mask a interrupt.
  81. * @param vector the interrupt number
  82. */
  83. void rt_hw_interrupt_umask(int vector)
  84. {
  85. enable_interrupt(vector, 0, 0);
  86. }
  87. /**
  88. * This function will install a interrupt service routine to a interrupt.
  89. * @param vector the interrupt number
  90. * @param new_handler the interrupt service routine to be installed
  91. * @param old_handler the old interrupt service routine
  92. */
  93. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  94. void *param, const char *name)
  95. {
  96. rt_isr_handler_t old_handler = RT_NULL;
  97. if (vector < MAX_HANDLERS)
  98. {
  99. old_handler = isr_table[vector].handler;
  100. if (handler != RT_NULL)
  101. {
  102. #ifdef RT_USING_INTERRUPT_INFO
  103. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  104. #endif /* RT_USING_INTERRUPT_INFO */
  105. isr_table[vector].handler = handler;
  106. isr_table[vector].param = param;
  107. }
  108. // arm_gic_set_cpu(0, vector, 1 << rt_cpu_get_smp_id());
  109. }
  110. return old_handler;
  111. }
  112. /**
  113. * Trigger a software IRQ
  114. *
  115. * Since we are running in single core, the target CPU are always CPU0.
  116. */
  117. void rt_hw_interrupt_trigger(int vector)
  118. {
  119. // arm_gic_trigger(0, 1, vector);
  120. }
  121. void rt_hw_interrupt_clear(int vector)
  122. {
  123. gic_write_end_of_irq(vector);
  124. }