interrupt.c 3.7 KB

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