gic.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  63. GIC_CPU_EOI(_gic_table[index].cpu_hw_base) = irq;
  64. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  65. }
  66. void arm_gic_mask(rt_uint32_t index, int irq)
  67. {
  68. rt_uint32_t mask = 1 << (irq % 32);
  69. RT_ASSERT(index < ARM_GIC_MAX_NR);
  70. irq = irq - _gic_table[index].offset;
  71. RT_ASSERT(irq >= 0);
  72. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  73. }
  74. void arm_gic_clear_pending(rt_uint32_t index, int irq)
  75. {
  76. rt_uint32_t mask = 1 << (irq % 32);
  77. RT_ASSERT(index < ARM_GIC_MAX_NR);
  78. irq = irq - _gic_table[index].offset;
  79. RT_ASSERT(irq >= 0);
  80. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  81. }
  82. void arm_gic_clear_active(rt_uint32_t index, int irq)
  83. {
  84. rt_uint32_t mask = 1 << (irq % 32);
  85. RT_ASSERT(index < ARM_GIC_MAX_NR);
  86. irq = irq - _gic_table[index].offset;
  87. RT_ASSERT(irq >= 0);
  88. GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  89. }
  90. /* Set up the cpu mask for the specific interrupt */
  91. void arm_gic_set_cpu(rt_uint32_t index, int irq, unsigned int cpumask)
  92. {
  93. rt_uint32_t old_tgt;
  94. RT_ASSERT(index < ARM_GIC_MAX_NR);
  95. irq = irq - _gic_table[index].offset;
  96. RT_ASSERT(irq >= 0);
  97. old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);
  98. old_tgt &= ~(0x0FFUL << ((irq % 4)*8));
  99. old_tgt |= cpumask << ((irq % 4)*8);
  100. GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
  101. }
  102. void arm_gic_umask(rt_uint32_t index, int irq)
  103. {
  104. rt_uint32_t mask = 1 << (irq % 32);
  105. RT_ASSERT(index < ARM_GIC_MAX_NR);
  106. irq = irq - _gic_table[index].offset;
  107. RT_ASSERT(irq >= 0);
  108. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  109. }
  110. void arm_gic_dump_type(rt_uint32_t index)
  111. {
  112. unsigned int gic_type;
  113. gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
  114. rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n",
  115. (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4) & 0xf,
  116. _gic_table[index].dist_hw_base,
  117. _gic_max_irq,
  118. gic_type & (1 << 10) ? "has" : "no",
  119. gic_type);
  120. }
  121. void arm_gic_dump(rt_uint32_t index)
  122. {
  123. unsigned int i, k;
  124. k = GIC_CPU_HIGHPRI(_gic_table[index].cpu_hw_base);
  125. rt_kprintf("--- high pending priority: %d(%08x)\n", k, k);
  126. rt_kprintf("--- hw mask ---\n");
  127. for (i = 0; i < _gic_max_irq / 32; i++)
  128. {
  129. rt_kprintf("0x%08x, ",
  130. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base,
  131. i * 32));
  132. }
  133. rt_kprintf("\n--- hw pending ---\n");
  134. for (i = 0; i < _gic_max_irq / 32; i++)
  135. {
  136. rt_kprintf("0x%08x, ",
  137. GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base,
  138. i * 32));
  139. }
  140. rt_kprintf("\n--- hw active ---\n");
  141. for (i = 0; i < _gic_max_irq / 32; i++)
  142. {
  143. rt_kprintf("0x%08x, ",
  144. GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base,
  145. i * 32));
  146. }
  147. rt_kprintf("\n");
  148. }
  149. #ifdef RT_USING_FINSH
  150. #include <finsh.h>
  151. FINSH_FUNCTION_EXPORT_ALIAS(arm_gic_dump, gic, show gic status);
  152. #endif
  153. int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start)
  154. {
  155. unsigned int gic_type, i;
  156. rt_uint32_t cpumask = 1 << 0;
  157. RT_ASSERT(index < ARM_GIC_MAX_NR);
  158. _gic_table[index].dist_hw_base = dist_base;
  159. _gic_table[index].offset = irq_start;
  160. /* Find out how many interrupts are supported. */
  161. gic_type = GIC_DIST_TYPE(dist_base);
  162. _gic_max_irq = ((gic_type & 0x1f) + 1) * 32;
  163. /*
  164. * The GIC only supports up to 1020 interrupt sources.
  165. * Limit this to either the architected maximum, or the
  166. * platform maximum.
  167. */
  168. if (_gic_max_irq > 1020)
  169. _gic_max_irq = 1020;
  170. if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */
  171. _gic_max_irq = ARM_GIC_NR_IRQS;
  172. cpumask |= cpumask << 8;
  173. cpumask |= cpumask << 16;
  174. cpumask |= cpumask << 24;
  175. GIC_DIST_CTRL(dist_base) = 0x0;
  176. /* Set all global interrupts to be level triggered, active low. */
  177. for (i = 32; i < _gic_max_irq; i += 16)
  178. GIC_DIST_CONFIG(dist_base, i) = 0x0;
  179. /* Set all global interrupts to this CPU only. */
  180. for (i = 32; i < _gic_max_irq; i += 4)
  181. GIC_DIST_TARGET(dist_base, i) = cpumask;
  182. /* Set priority on all interrupts. */
  183. for (i = 0; i < _gic_max_irq; i += 4)
  184. GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0;
  185. /* Disable all interrupts. */
  186. for (i = 0; i < _gic_max_irq; i += 32)
  187. GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffff;
  188. #if 0
  189. /* All interrupts defaults to IGROUP1(IRQ). */
  190. for (i = 0; i < _gic_max_irq; i += 32)
  191. GIC_DIST_IGROUP(dist_base, i) = 0xffffffff;
  192. #endif
  193. for (i = 0; i < _gic_max_irq; i += 32)
  194. GIC_DIST_IGROUP(dist_base, i) = 0;
  195. /* Enable group0 and group1 interrupt forwarding. */
  196. GIC_DIST_CTRL(dist_base) = 0x01;
  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. GIC_CPU_BINPOINT(cpu_base) = 0x7;
  205. /* Enable CPU interrupt */
  206. GIC_CPU_CTRL(cpu_base) = 0x01;
  207. return 0;
  208. }
  209. void arm_gic_set_group(rt_uint32_t index, int vector, int group)
  210. {
  211. /* As for GICv2, there are only group0 and group1. */
  212. RT_ASSERT(group <= 1);
  213. RT_ASSERT(vector < _gic_max_irq);
  214. if (group == 0)
  215. {
  216. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  217. vector) &= ~(1 << (vector % 32));
  218. }
  219. else if (group == 1)
  220. {
  221. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  222. vector) |= (1 << (vector % 32));
  223. }
  224. }
  225. #ifdef RT_USING_SMP
  226. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask)
  227. {
  228. /* note: ipi_vector maybe different with irq_vector */
  229. GIC_DIST_SOFTINT(_gic_table[0].dist_hw_base) = (cpu_mask << 16) | ipi_vector;
  230. }
  231. #endif
  232. #ifdef RT_USING_SMP
  233. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler)
  234. {
  235. /* note: ipi_vector maybe different with irq_vector */
  236. rt_hw_interrupt_install(ipi_vector, ipi_isr_handler, 0, "IPI_HANDLER");
  237. }
  238. #endif