gic.c 9.1 KB

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