gic.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. */
  15. #include <rtthread.h>
  16. #include <board.h>
  17. #include "gic.h"
  18. #include "cp15.h"
  19. struct arm_gic
  20. {
  21. rt_uint32_t offset;
  22. rt_uint32_t dist_hw_base;
  23. rt_uint32_t cpu_hw_base;
  24. };
  25. static struct arm_gic _gic_table[ARM_GIC_MAX_NR];
  26. #define GIC_CPU_CTRL(hw_base) __REG32((hw_base) + 0x00)
  27. #define GIC_CPU_PRIMASK(hw_base) __REG32((hw_base) + 0x04)
  28. #define GIC_CPU_BINPOINT(hw_base) __REG32((hw_base) + 0x08)
  29. #define GIC_CPU_INTACK(hw_base) __REG32((hw_base) + 0x0c)
  30. #define GIC_CPU_EOI(hw_base) __REG32((hw_base) + 0x10)
  31. #define GIC_CPU_RUNNINGPRI(hw_base) __REG32((hw_base) + 0x14)
  32. #define GIC_CPU_HIGHPRI(hw_base) __REG32((hw_base) + 0x18)
  33. #define GIC_DIST_CTRL(hw_base) __REG32((hw_base) + 0x000)
  34. #define GIC_DIST_TYPE(hw_base) __REG32((hw_base) + 0x004)
  35. #define GIC_DIST_IGROUP(hw_base, n) __REG32((hw_base) + 0x080 + ((n)/32) * 4)
  36. #define GIC_DIST_ENABLE_SET(hw_base, n) __REG32((hw_base) + 0x100 + ((n)/32) * 4)
  37. #define GIC_DIST_ENABLE_CLEAR(hw_base, n) __REG32((hw_base) + 0x180 + ((n)/32) * 4)
  38. #define GIC_DIST_PENDING_SET(hw_base, n) __REG32((hw_base) + 0x200 + ((n)/32) * 4)
  39. #define GIC_DIST_PENDING_CLEAR(hw_base, n) __REG32((hw_base) + 0x280 + ((n)/32) * 4)
  40. #define GIC_DIST_ACTIVE_SET(hw_base, n) __REG32((hw_base) + 0x300 + ((n)/32) * 4)
  41. #define GIC_DIST_ACTIVE_CLEAR(hw_base, n) __REG32((hw_base) + 0x380 + ((n)/32) * 4)
  42. #define GIC_DIST_PRI(hw_base, n) __REG32((hw_base) + 0x400 + ((n)/4) * 4)
  43. #define GIC_DIST_TARGET(hw_base, n) __REG32((hw_base) + 0x800 + ((n)/4) * 4)
  44. #define GIC_DIST_CONFIG(hw_base, n) __REG32((hw_base) + 0xc00 + ((n)/16) * 4)
  45. #define GIC_DIST_SOFTINT(hw_base) __REG32((hw_base) + 0xf00)
  46. #define GIC_DIST_CPENDSGI(hw_base, n) __REG32((hw_base) + 0xf10 + ((n)/4) * 4)
  47. #define GIC_DIST_ICPIDR2(hw_base) __REG32((hw_base) + 0xfe8)
  48. static unsigned int _gic_max_irq;
  49. int arm_gic_get_active_irq(rt_uint32_t index)
  50. {
  51. int irq;
  52. RT_ASSERT(index < ARM_GIC_MAX_NR);
  53. irq = GIC_CPU_INTACK(_gic_table[index].cpu_hw_base);
  54. irq += _gic_table[index].offset;
  55. return irq;
  56. }
  57. void arm_gic_ack(rt_uint32_t index, int irq)
  58. {
  59. rt_uint32_t mask = 1 << (irq % 32);
  60. RT_ASSERT(index < ARM_GIC_MAX_NR);
  61. irq = irq - _gic_table[index].offset;
  62. RT_ASSERT(irq >= 0);
  63. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  64. GIC_CPU_EOI(_gic_table[index].cpu_hw_base) = irq;
  65. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  66. }
  67. void arm_gic_mask(rt_uint32_t index, int irq)
  68. {
  69. rt_uint32_t mask = 1 << (irq % 32);
  70. RT_ASSERT(index < ARM_GIC_MAX_NR);
  71. irq = irq - _gic_table[index].offset;
  72. RT_ASSERT(irq >= 0);
  73. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  74. }
  75. void arm_gic_clear_pending(rt_uint32_t index, int irq)
  76. {
  77. rt_uint32_t mask = 1 << (irq % 32);
  78. RT_ASSERT(index < ARM_GIC_MAX_NR);
  79. irq = irq - _gic_table[index].offset;
  80. RT_ASSERT(irq >= 0);
  81. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  82. }
  83. void arm_gic_clear_active(rt_uint32_t index, int irq)
  84. {
  85. rt_uint32_t mask = 1 << (irq % 32);
  86. RT_ASSERT(index < ARM_GIC_MAX_NR);
  87. irq = irq - _gic_table[index].offset;
  88. RT_ASSERT(irq >= 0);
  89. GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  90. }
  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)
  171. _gic_max_irq = ARM_GIC_NR_IRQS;
  172. cpumask |= cpumask << 8;
  173. cpumask |= cpumask << 16;
  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. /* Enable group0 and group1 interrupt forwarding. */
  193. GIC_DIST_CTRL(dist_base) = 0x03;
  194. return 0;
  195. }
  196. int arm_gic_cpu_init(rt_uint32_t index, rt_uint32_t cpu_base)
  197. {
  198. RT_ASSERT(index < ARM_GIC_MAX_NR);
  199. _gic_table[index].cpu_hw_base = cpu_base;
  200. GIC_CPU_PRIMASK(cpu_base) = 0xf0;
  201. /* Enable CPU interrupt */
  202. GIC_CPU_CTRL(cpu_base) = 0x01;
  203. return 0;
  204. }
  205. void arm_gic_set_group(rt_uint32_t index, int vector, int group)
  206. {
  207. /* As for GICv2, there are only group0 and group1. */
  208. RT_ASSERT(group <= 1);
  209. RT_ASSERT(vector < _gic_max_irq);
  210. if (group == 0)
  211. {
  212. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  213. vector) &= ~(1 << (vector % 32));
  214. }
  215. else if (group == 1)
  216. {
  217. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  218. vector) |= (1 << (vector % 32));
  219. }
  220. }