interrupt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * 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 "realview.h"
  14. #include "gic.h"
  15. #include "cp15.h"
  16. #ifdef RT_USING_VMM
  17. #include <vmm.h>
  18. #endif
  19. #define MAX_HANDLERS NR_IRQS_PBA8
  20. extern volatile rt_uint8_t rt_interrupt_nest;
  21. /* exception and interrupt handler table */
  22. struct rt_irq_desc isr_table[MAX_HANDLERS];
  23. rt_uint32_t rt_interrupt_from_thread;
  24. rt_uint32_t rt_interrupt_to_thread;
  25. rt_uint32_t rt_thread_switch_interrupt_flag;
  26. extern void rt_cpu_vector_set_base(unsigned int addr);
  27. extern int system_vectors;
  28. static void rt_hw_vector_init(void)
  29. {
  30. int sctrl;
  31. unsigned int *src = (unsigned int *)&system_vectors;
  32. /* C12-C0 is only active when SCTLR.V = 0 */
  33. asm volatile ("mrc p15, #0, %0, c1, c0, #0"
  34. :"=r" (sctrl));
  35. sctrl &= ~(1 << 13);
  36. asm volatile ("mcr p15, #0, %0, c1, c0, #0"
  37. :
  38. :"r" (sctrl));
  39. asm volatile ("mcr p15, #0, %0, c12, c0, #0"
  40. :
  41. :"r" (src));
  42. }
  43. /**
  44. * This function will initialize hardware interrupt
  45. */
  46. void rt_hw_interrupt_init(void)
  47. {
  48. rt_uint32_t gic_cpu_base;
  49. rt_uint32_t gic_dist_base;
  50. /* initialize vector table */
  51. rt_hw_vector_init();
  52. /* initialize exceptions table */
  53. rt_memset(isr_table, 0x00, sizeof(isr_table));
  54. /* initialize ARM GIC */
  55. gic_dist_base = REALVIEW_GIC_DIST_BASE;
  56. gic_cpu_base = REALVIEW_GIC_CPU_BASE;
  57. arm_gic_dist_init(0, gic_dist_base, 0);
  58. arm_gic_cpu_init(0, gic_cpu_base);
  59. /*arm_gic_dump_type(0);*/
  60. /* init interrupt nest, and context in thread sp */
  61. rt_interrupt_nest = 0;
  62. rt_interrupt_from_thread = 0;
  63. rt_interrupt_to_thread = 0;
  64. rt_thread_switch_interrupt_flag = 0;
  65. }
  66. /**
  67. * This function will mask a interrupt.
  68. * @param vector the interrupt number
  69. */
  70. void rt_hw_interrupt_mask(int vector)
  71. {
  72. arm_gic_mask(0, vector);
  73. }
  74. /**
  75. * This function will un-mask a interrupt.
  76. * @param vector the interrupt number
  77. */
  78. void rt_hw_interrupt_umask(int vector)
  79. {
  80. arm_gic_umask(0, vector);
  81. }
  82. /**
  83. * This function will install a interrupt service routine to a interrupt.
  84. * @param vector the interrupt number
  85. * @param new_handler the interrupt service routine to be installed
  86. * @param old_handler the old interrupt service routine
  87. */
  88. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  89. void *param, const char *name)
  90. {
  91. rt_isr_handler_t old_handler = RT_NULL;
  92. if (vector < MAX_HANDLERS)
  93. {
  94. old_handler = isr_table[vector].handler;
  95. if (handler != RT_NULL)
  96. {
  97. #ifdef RT_USING_INTERRUPT_INFO
  98. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  99. #endif /* RT_USING_INTERRUPT_INFO */
  100. isr_table[vector].handler = handler;
  101. isr_table[vector].param = param;
  102. }
  103. arm_gic_set_cpu(0, vector, 1 << rt_cpu_get_smp_id());
  104. }
  105. return old_handler;
  106. }
  107. /**
  108. * Trigger a software IRQ
  109. *
  110. * Since we are running in single core, the target CPU are always CPU0.
  111. */
  112. void rt_hw_interrupt_trigger(int vector)
  113. {
  114. arm_gic_trigger(0, 1, vector);
  115. }
  116. void rt_hw_interrupt_clear(int vector)
  117. {
  118. arm_gic_clear_sgi(0, 1, vector);
  119. }