gic.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * COPYRIGHT (C) 2013-2014, Shanghai Real-Thread Technology Co., Ltd
  3. *
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <rtthread.h>
  21. #include <board.h>
  22. #include "gic.h"
  23. #include "cp15.h"
  24. struct arm_gic
  25. {
  26. rt_uint32_t offset;
  27. rt_uint32_t dist_hw_base;
  28. rt_uint32_t cpu_hw_base;
  29. };
  30. static struct arm_gic _gic_table[ARM_GIC_MAX_NR];
  31. #define GIC_CPU_CTRL(hw_base) __REG32((hw_base) + 0x00)
  32. #define GIC_CPU_PRIMASK(hw_base) __REG32((hw_base) + 0x04)
  33. #define GIC_CPU_BINPOINT(hw_base) __REG32((hw_base) + 0x08)
  34. #define GIC_CPU_INTACK(hw_base) __REG32((hw_base) + 0x0c)
  35. #define GIC_CPU_EOI(hw_base) __REG32((hw_base) + 0x10)
  36. #define GIC_CPU_RUNNINGPRI(hw_base) __REG32((hw_base) + 0x14)
  37. #define GIC_CPU_HIGHPRI(hw_base) __REG32((hw_base) + 0x18)
  38. #define GIC_DIST_CTRL(hw_base) __REG32((hw_base) + 0x000)
  39. #define GIC_DIST_TYPE(hw_base) __REG32((hw_base) + 0x004)
  40. #define GIC_DIST_IGROUP(hw_base, n) __REG32((hw_base) + 0x080 + (n/32) * 4)
  41. #define GIC_DIST_ENABLE_SET(hw_base, n) __REG32((hw_base) + 0x100 + (n/32) * 4)
  42. #define GIC_DIST_ENABLE_CLEAR(hw_base, n) __REG32((hw_base) + 0x180 + (n/32) * 4)
  43. #define GIC_DIST_PENDING_SET(hw_base, n) __REG32((hw_base) + 0x200)
  44. #define GIC_DIST_PENDING_CLEAR(hw_base, n) __REG32((hw_base) + 0x280)
  45. #define GIC_DIST_ACTIVE_BIT(hw_base) __REG32((hw_base) + 0x300)
  46. #define GIC_DIST_PRI(hw_base, n) __REG32((hw_base) + 0x400 + (n/4) * 4)
  47. #define GIC_DIST_TARGET(hw_base, n) __REG32((hw_base) + 0x800 + (n/4) * 4)
  48. #define GIC_DIST_CONFIG(hw_base, n) __REG32((hw_base) + 0xc00 + (n/16) * 4)
  49. #define GIC_DIST_SOFTINT(hw_base) __REG32((hw_base) + 0xf00)
  50. #define GIC_DIST_CPENDSGI(hw_base, n) __REG32((hw_base) + 0xf10 + (n/4) * 4)
  51. #define GIC_DIST_ICPIDR2(hw_base) __REG32((hw_base) + 0xfe8)
  52. static unsigned int _gic_max_irq;
  53. int arm_gic_get_active_irq(rt_uint32_t index)
  54. {
  55. int irq;
  56. RT_ASSERT(index < ARM_GIC_MAX_NR);
  57. irq = GIC_CPU_INTACK(_gic_table[index].cpu_hw_base);
  58. irq += _gic_table[index].offset;
  59. return irq;
  60. }
  61. void arm_gic_ack(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_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  68. GIC_CPU_EOI(_gic_table[index].cpu_hw_base) = irq;
  69. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  70. }
  71. void arm_gic_mask(rt_uint32_t index, int irq)
  72. {
  73. rt_uint32_t mask = 1 << (irq % 32);
  74. RT_ASSERT(index < ARM_GIC_MAX_NR);
  75. irq = irq - _gic_table[index].offset;
  76. RT_ASSERT(irq >= 0);
  77. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  78. }
  79. void arm_gic_set_cpu(rt_uint32_t index, int irq, unsigned int cpumask)
  80. {
  81. rt_uint32_t old_tgt;
  82. RT_ASSERT(index < ARM_GIC_MAX_NR);
  83. irq = irq - _gic_table[index].offset;
  84. RT_ASSERT(irq >= 0);
  85. old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);
  86. old_tgt &= ~(0x0FFUL << ((irq % 4)*8));
  87. old_tgt |= cpumask << ((irq % 4)*8);
  88. GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
  89. }
  90. void arm_gic_umask(rt_uint32_t index, int irq)
  91. {
  92. rt_uint32_t mask = 1 << (irq % 32);
  93. RT_ASSERT(index < ARM_GIC_MAX_NR);
  94. irq = irq - _gic_table[index].offset;
  95. RT_ASSERT(irq >= 0);
  96. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  97. }
  98. void arm_gic_dump_type(rt_uint32_t index)
  99. {
  100. unsigned int gic_type;
  101. gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
  102. rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n",
  103. (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4) & 0xf,
  104. _gic_table[index].dist_hw_base,
  105. _gic_max_irq,
  106. gic_type & (1 << 10) ? "has" : "no",
  107. gic_type);
  108. }
  109. int arm_gic_dist_init(rt_uint32_t index, rt_uint32_t dist_base, int irq_start)
  110. {
  111. unsigned int gic_type, i;
  112. rt_uint32_t cpumask = 1 << 0;
  113. RT_ASSERT(index < ARM_GIC_MAX_NR);
  114. _gic_table[index].dist_hw_base = dist_base;
  115. _gic_table[index].offset = irq_start;
  116. /* Find out how many interrupts are supported. */
  117. gic_type = GIC_DIST_TYPE(dist_base);
  118. _gic_max_irq = ((gic_type & 0x1f) + 1) * 32;
  119. /*
  120. * The GIC only supports up to 1020 interrupt sources.
  121. * Limit this to either the architected maximum, or the
  122. * platform maximum.
  123. */
  124. if (_gic_max_irq > 1020)
  125. _gic_max_irq = 1020;
  126. if (_gic_max_irq > ARM_GIC_NR_IRQS)
  127. _gic_max_irq = ARM_GIC_NR_IRQS;
  128. cpumask |= cpumask << 8;
  129. cpumask |= cpumask << 16;
  130. GIC_DIST_CTRL(dist_base) = 0x0;
  131. /* Set all global interrupts to be level triggered, active low. */
  132. for (i = 32; i < _gic_max_irq; i += 16)
  133. GIC_DIST_CONFIG(dist_base, i) = 0x0;
  134. /* Set all global interrupts to this CPU only. */
  135. for (i = 32; i < _gic_max_irq; i += 4)
  136. GIC_DIST_TARGET(dist_base, i) = cpumask;
  137. /* Set priority on all interrupts. */
  138. for (i = 0; i < _gic_max_irq; i += 4)
  139. GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0;
  140. /* Disable all interrupts. */
  141. for (i = 0; i < _gic_max_irq; i += 32)
  142. GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffff;
  143. /* Set the FIQEn bit, signal FIQ for IGROUP0. */
  144. GIC_DIST_CTRL(dist_base) = 0x01;
  145. return 0;
  146. }
  147. int arm_gic_cpu_init(rt_uint32_t index, rt_uint32_t cpu_base)
  148. {
  149. RT_ASSERT(index < ARM_GIC_MAX_NR);
  150. _gic_table[index].cpu_hw_base = cpu_base;
  151. GIC_CPU_PRIMASK(cpu_base) = 0xf0;
  152. /* Enable CPU interrupt */
  153. GIC_CPU_CTRL(cpu_base) = 0x01;
  154. return 0;
  155. }
  156. void arm_gic_set_group(rt_uint32_t index, int vector, int group)
  157. {
  158. /* As for GICv2, there are only group0 and group1. */
  159. RT_ASSERT(group <= 1);
  160. RT_ASSERT(vector < _gic_max_irq);
  161. if (group == 0)
  162. {
  163. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  164. vector) &= ~(1 << (vector % 32));
  165. }
  166. else if (group == 1)
  167. {
  168. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base,
  169. vector) |= (1 << (vector % 32));
  170. }
  171. }
  172. void arm_gic_trigger(rt_uint32_t index, int target_cpu, int irq)
  173. {
  174. unsigned int reg;
  175. RT_ASSERT(irq <= 15);
  176. RT_ASSERT(target_cpu <= 255);
  177. reg = (target_cpu << 16) | irq;
  178. GIC_DIST_SOFTINT(_gic_table[index].dist_hw_base) = reg;
  179. }
  180. void arm_gic_clear_sgi(rt_uint32_t index, int target_cpu, int irq)
  181. {
  182. RT_ASSERT(irq <= 15);
  183. RT_ASSERT(target_cpu <= 255);
  184. GIC_DIST_CPENDSGI(_gic_table[index].dist_hw_base, irq) = target_cpu << (irq % 4);
  185. }