gic_pl400.c 7.0 KB

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