gic.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * File : gic.c, ARM Generic Interrupt Controller
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013-2014, RT-Thread Develop 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-20 Bernard first version
  13. * 2014-04-03 Grissiom many enhancements
  14. * 2018-11-22 Jesven add rt_hw_ipi_send()
  15. * add rt_hw_ipi_handler_install()
  16. */
  17. #include <rthw.h>
  18. #include <rtthread.h>
  19. #include <board.h>
  20. #include "gic.h"
  21. #include "cp15.h"
  22. struct arm_gic
  23. {
  24. rt_uint32_t offset;
  25. rt_uint32_t dist_hw_base;
  26. rt_uint32_t cpu_hw_base;
  27. };
  28. static struct arm_gic _gic_table[ARM_GIC_MAX_NR];
  29. #define GIC_CPU_CTRL(hw_base) __REG32((hw_base) + 0x00)
  30. #define GIC_CPU_PRIMASK(hw_base) __REG32((hw_base) + 0x04)
  31. #define GIC_CPU_BINPOINT(hw_base) __REG32((hw_base) + 0x08)
  32. #define GIC_CPU_INTACK(hw_base) __REG32((hw_base) + 0x0c)
  33. #define GIC_CPU_EOI(hw_base) __REG32((hw_base) + 0x10)
  34. #define GIC_CPU_RUNNINGPRI(hw_base) __REG32((hw_base) + 0x14)
  35. #define GIC_CPU_HIGHPRI(hw_base) __REG32((hw_base) + 0x18)
  36. #define GIC_DIST_CTRL(hw_base) __REG32((hw_base) + 0x000)
  37. #define GIC_DIST_TYPE(hw_base) __REG32((hw_base) + 0x004)
  38. #define GIC_DIST_IGROUP(hw_base, n) __REG32((hw_base) + 0x080 + ((n)/32) * 4)
  39. #define GIC_DIST_ENABLE_SET(hw_base, n) __REG32((hw_base) + 0x100 + ((n)/32) * 4)
  40. #define GIC_DIST_ENABLE_CLEAR(hw_base, n) __REG32((hw_base) + 0x180 + ((n)/32) * 4)
  41. #define GIC_DIST_PENDING_SET(hw_base, n) __REG32((hw_base) + 0x200 + ((n)/32) * 4)
  42. #define GIC_DIST_PENDING_CLEAR(hw_base, n) __REG32((hw_base) + 0x280 + ((n)/32) * 4)
  43. #define GIC_DIST_ACTIVE_SET(hw_base, n) __REG32((hw_base) + 0x300 + ((n)/32) * 4)
  44. #define GIC_DIST_ACTIVE_CLEAR(hw_base, n) __REG32((hw_base) + 0x380 + ((n)/32) * 4)
  45. #define GIC_DIST_PRI(hw_base, n) __REG32((hw_base) + 0x400 + ((n)/4) * 4)
  46. #define GIC_DIST_TARGET(hw_base, n) __REG32((hw_base) + 0x800 + ((n)/4) * 4)
  47. #define GIC_DIST_CONFIG(hw_base, n) __REG32((hw_base) + 0xc00 + ((n)/16) * 4)
  48. #define GIC_DIST_SOFTINT(hw_base) __REG32((hw_base) + 0xf00)
  49. #define GIC_DIST_CPENDSGI(hw_base, n) __REG32((hw_base) + 0xf10 + ((n)/4) * 4)
  50. #define GIC_DIST_ICPIDR2(hw_base) __REG32((hw_base) + 0xfe8)
  51. static unsigned int _gic_max_irq;
  52. int arm_gic_get_active_irq(rt_uint32_t index)
  53. {
  54. int irq;
  55. RT_ASSERT(index < ARM_GIC_MAX_NR);
  56. irq = GIC_CPU_INTACK(_gic_table[index].cpu_hw_base);
  57. irq += _gic_table[index].offset;
  58. return irq;
  59. }
  60. void arm_gic_ack(rt_uint32_t index, int irq)
  61. {
  62. rt_uint32_t mask = 1 << (irq % 32);
  63. RT_ASSERT(index < ARM_GIC_MAX_NR);
  64. irq = irq - _gic_table[index].offset;
  65. RT_ASSERT(irq >= 0);
  66. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  67. GIC_CPU_EOI(_gic_table[index].cpu_hw_base) = irq;
  68. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  69. }
  70. void arm_gic_mask(rt_uint32_t index, int irq)
  71. {
  72. rt_uint32_t mask = 1 << (irq % 32);
  73. RT_ASSERT(index < ARM_GIC_MAX_NR);
  74. irq = irq - _gic_table[index].offset;
  75. RT_ASSERT(irq >= 0);
  76. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  77. }
  78. void arm_gic_clear_pending(rt_uint32_t index, int irq)
  79. {
  80. rt_uint32_t mask = 1 << (irq % 32);
  81. RT_ASSERT(index < ARM_GIC_MAX_NR);
  82. irq = irq - _gic_table[index].offset;
  83. RT_ASSERT(irq >= 0);
  84. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  85. }
  86. void arm_gic_clear_active(rt_uint32_t index, int irq)
  87. {
  88. rt_uint32_t mask = 1 << (irq % 32);
  89. RT_ASSERT(index < ARM_GIC_MAX_NR);
  90. irq = irq - _gic_table[index].offset;
  91. RT_ASSERT(irq >= 0);
  92. GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  93. }
  94. void arm_gic_set_cpu(rt_uint32_t index, int irq, unsigned int cpumask)
  95. {
  96. rt_uint32_t old_tgt;
  97. RT_ASSERT(index < ARM_GIC_MAX_NR);
  98. irq = irq - _gic_table[index].offset;
  99. RT_ASSERT(irq >= 0);
  100. old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);
  101. old_tgt &= ~(0x0FFUL << ((irq % 4)*8));
  102. old_tgt |= cpumask << ((irq % 4)*8);
  103. GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
  104. }
  105. void arm_gic_umask(rt_uint32_t index, int irq)
  106. {
  107. rt_uint32_t mask = 1 << (irq % 32);
  108. RT_ASSERT(index < ARM_GIC_MAX_NR);
  109. irq = irq - _gic_table[index].offset;
  110. RT_ASSERT(irq >= 0);
  111. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  112. }
  113. void arm_gic_dump_type(rt_uint32_t index)
  114. {
  115. unsigned int gic_type;
  116. gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
  117. rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n",
  118. (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4) & 0xf,
  119. _gic_table[index].dist_hw_base,
  120. _gic_max_irq,
  121. gic_type & (1 << 10) ? "has" : "no",
  122. gic_type);
  123. }
  124. void arm_gic_dump(rt_uint32_t index)
  125. {
  126. unsigned int i, k;
  127. k = GIC_CPU_HIGHPRI(_gic_table[index].cpu_hw_base);
  128. rt_kprintf("--- high pending priority: %d(%08x)\n", k, k);
  129. rt_kprintf("--- hw mask ---\n");
  130. for (i = 0; i < _gic_max_irq / 32; i++)
  131. {
  132. rt_kprintf("0x%08x, ",
  133. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base,
  134. i * 32));
  135. }
  136. rt_kprintf("\n--- hw pending ---\n");
  137. for (i = 0; i < _gic_max_irq / 32; i++)
  138. {
  139. rt_kprintf("0x%08x, ",
  140. GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base,
  141. i * 32));
  142. }
  143. rt_kprintf("\n--- hw active ---\n");
  144. for (i = 0; i < _gic_max_irq / 32; i++)
  145. {
  146. rt_kprintf("0x%08x, ",
  147. GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base,
  148. i * 32));
  149. }
  150. rt_kprintf("\n");
  151. }
  152. #ifdef RT_USING_FINSH
  153. #include <finsh.h>
  154. FINSH_FUNCTION_EXPORT_ALIAS(arm_gic_dump, gic, show gic status);
  155. #endif
  156. int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start)
  157. {
  158. unsigned int gic_type, i;
  159. rt_uint32_t cpumask = 1 << 0;
  160. RT_ASSERT(index < ARM_GIC_MAX_NR);
  161. _gic_table[index].dist_hw_base = dist_base;
  162. _gic_table[index].offset = irq_start;
  163. /* Find out how many interrupts are supported. */
  164. gic_type = GIC_DIST_TYPE(dist_base);
  165. _gic_max_irq = ((gic_type & 0x1f) + 1) * 32;
  166. /*
  167. * The GIC only supports up to 1020 interrupt sources.
  168. * Limit this to either the architected maximum, or the
  169. * platform maximum.
  170. */
  171. if (_gic_max_irq > 1020)
  172. _gic_max_irq = 1020;
  173. if (_gic_max_irq > ARM_GIC_NR_IRQS)
  174. _gic_max_irq = ARM_GIC_NR_IRQS;
  175. cpumask |= cpumask << 8;
  176. cpumask |= cpumask << 16;
  177. GIC_DIST_CTRL(dist_base) = 0x0;
  178. /* Set all global interrupts to be level triggered, active low. */
  179. for (i = 32; i < _gic_max_irq; i += 16)
  180. GIC_DIST_CONFIG(dist_base, i) = 0x0;
  181. /* Set all global interrupts to this CPU only. */
  182. for (i = 32; i < _gic_max_irq; i += 4)
  183. GIC_DIST_TARGET(dist_base, i) = cpumask;
  184. /* Set priority on all interrupts. */
  185. for (i = 0; i < _gic_max_irq; i += 4)
  186. GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0;
  187. /* Disable all interrupts. */
  188. for (i = 0; i < _gic_max_irq; i += 32)
  189. GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffff;
  190. #if 0
  191. /* All interrupts defaults to IGROUP1(IRQ). */
  192. for (i = 0; i < _gic_max_irq; i += 32)
  193. GIC_DIST_IGROUP(dist_base, i) = 0xffffffff;
  194. #endif
  195. /* Enable group0 and group1 interrupt forwarding. */
  196. GIC_DIST_CTRL(dist_base) = 0x03;
  197. return 0;
  198. }
  199. int arm_gic_cpu_init(rt_uint32_t index, rt_uint32_t cpu_base)
  200. {
  201. RT_ASSERT(index < ARM_GIC_MAX_NR);
  202. _gic_table[index].cpu_hw_base = cpu_base;
  203. GIC_CPU_PRIMASK(cpu_base) = 0xf0;
  204. /* Enable CPU interrupt */
  205. GIC_CPU_CTRL(cpu_base) = 0x01;
  206. return 0;
  207. }
  208. void arm_gic_set_group(rt_uint32_t index, int vector, int group)
  209. {
  210. /* As for GICv2, there are only group0 and group1. */
  211. RT_ASSERT(group <= 1);
  212. RT_ASSERT(vector < _gic_max_irq);
  213. if (group == 0)
  214. {
  215. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  216. vector) &= ~(1 << (vector % 32));
  217. }
  218. else if (group == 1)
  219. {
  220. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  221. vector) |= (1 << (vector % 32));
  222. }
  223. }
  224. #ifdef RT_USING_SMP
  225. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
  226. {
  227. /* note: ipi_vector maybe different with irq_vector */
  228. GIC_DIST_SOFTINT(_gic_table[0].dist_hw_base) = (cpu_mask << 16) | ipi_vector;
  229. }
  230. #endif
  231. #ifdef RT_USING_SMP
  232. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler)
  233. {
  234. /* note: ipi_vector maybe different with irq_vector */
  235. rt_hw_interrupt_install(ipi_vector, ipi_isr_handler, 0, "IPI_HANDLER");
  236. }
  237. #endif