gic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright (c) 2006-2021, 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_uint64_t offset; /* the first interrupt index in the vector table */
  20. rt_uint64_t dist_hw_base; /* the base address of the gic distributor */
  21. rt_uint64_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. /** Macro to access the Generic Interrupt Controller Interface (GICC)
  26. */
  27. #define GIC_CPU_CTRL(hw_base) __REG32((hw_base) + 0x00U)
  28. #define GIC_CPU_PRIMASK(hw_base) __REG32((hw_base) + 0x04U)
  29. #define GIC_CPU_BINPOINT(hw_base) __REG32((hw_base) + 0x08U)
  30. #define GIC_CPU_INTACK(hw_base) __REG32((hw_base) + 0x0cU)
  31. #define GIC_CPU_EOI(hw_base) __REG32((hw_base) + 0x10U)
  32. #define GIC_CPU_RUNNINGPRI(hw_base) __REG32((hw_base) + 0x14U)
  33. #define GIC_CPU_HIGHPRI(hw_base) __REG32((hw_base) + 0x18U)
  34. #define GIC_CPU_IIDR(hw_base) __REG32((hw_base) + 0xFCU)
  35. /** Macro to access the Generic Interrupt Controller Distributor (GICD)
  36. */
  37. #define GIC_DIST_CTRL(hw_base) __REG32((hw_base) + 0x000U)
  38. #define GIC_DIST_TYPE(hw_base) __REG32((hw_base) + 0x004U)
  39. #define GIC_DIST_IGROUP(hw_base, n) __REG32((hw_base) + 0x080U + ((n)/32U) * 4U)
  40. #define GIC_DIST_ENABLE_SET(hw_base, n) __REG32((hw_base) + 0x100U + ((n)/32U) * 4U)
  41. #define GIC_DIST_ENABLE_CLEAR(hw_base, n) __REG32((hw_base) + 0x180U + ((n)/32U) * 4U)
  42. #define GIC_DIST_PENDING_SET(hw_base, n) __REG32((hw_base) + 0x200U + ((n)/32U) * 4U)
  43. #define GIC_DIST_PENDING_CLEAR(hw_base, n) __REG32((hw_base) + 0x280U + ((n)/32U) * 4U)
  44. #define GIC_DIST_ACTIVE_SET(hw_base, n) __REG32((hw_base) + 0x300U + ((n)/32U) * 4U)
  45. #define GIC_DIST_ACTIVE_CLEAR(hw_base, n) __REG32((hw_base) + 0x380U + ((n)/32U) * 4U)
  46. #define GIC_DIST_PRI(hw_base, n) __REG32((hw_base) + 0x400U + ((n)/4U) * 4U)
  47. #define GIC_DIST_TARGET(hw_base, n) __REG32((hw_base) + 0x800U + ((n)/4U) * 4U)
  48. #define GIC_DIST_CONFIG(hw_base, n) __REG32((hw_base) + 0xc00U + ((n)/16U) * 4U)
  49. #define GIC_DIST_SOFTINT(hw_base) __REG32((hw_base) + 0xf00U)
  50. #define GIC_DIST_CPENDSGI(hw_base, n) __REG32((hw_base) + 0xf10U + ((n)/4U) * 4U)
  51. #define GIC_DIST_SPENDSGI(hw_base, n) __REG32((hw_base) + 0xf20U + ((n)/4U) * 4U)
  52. #define GIC_DIST_ICPIDR2(hw_base) __REG32((hw_base) + 0xfe8U)
  53. static unsigned int _gic_max_irq;
  54. int arm_gic_get_active_irq(rt_uint64_t index)
  55. {
  56. int irq;
  57. RT_ASSERT(index < ARM_GIC_MAX_NR);
  58. irq = GIC_CPU_INTACK(_gic_table[index].cpu_hw_base);
  59. irq += _gic_table[index].offset;
  60. return irq;
  61. }
  62. void arm_gic_ack(rt_uint64_t index, int irq)
  63. {
  64. rt_uint64_t mask = 1U << (irq % 32U);
  65. RT_ASSERT(index < ARM_GIC_MAX_NR);
  66. irq = irq - _gic_table[index].offset;
  67. RT_ASSERT(irq >= 0U);
  68. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  69. GIC_CPU_EOI(_gic_table[index].cpu_hw_base) = irq;
  70. }
  71. void arm_gic_mask(rt_uint64_t index, int irq)
  72. {
  73. rt_uint64_t mask = 1U << (irq % 32U);
  74. RT_ASSERT(index < ARM_GIC_MAX_NR);
  75. irq = irq - _gic_table[index].offset;
  76. RT_ASSERT(irq >= 0U);
  77. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  78. }
  79. void arm_gic_umask(rt_uint64_t index, int irq)
  80. {
  81. rt_uint64_t mask = 1U << (irq % 32U);
  82. RT_ASSERT(index < ARM_GIC_MAX_NR);
  83. irq = irq - _gic_table[index].offset;
  84. RT_ASSERT(irq >= 0U);
  85. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  86. }
  87. rt_uint64_t arm_gic_get_pending_irq(rt_uint64_t index, int irq)
  88. {
  89. rt_uint64_t pend;
  90. RT_ASSERT(index < ARM_GIC_MAX_NR);
  91. irq = irq - _gic_table[index].offset;
  92. RT_ASSERT(irq >= 0U);
  93. if (irq >= 16U)
  94. {
  95. pend = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32U)) & 0x1UL;
  96. }
  97. else
  98. {
  99. /* INTID 0-15 Software Generated Interrupt */
  100. pend = (GIC_DIST_SPENDSGI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4U) * 8U)) & 0xFFUL;
  101. /* No CPU identification offered */
  102. if (pend != 0U)
  103. {
  104. pend = 1U;
  105. }
  106. else
  107. {
  108. pend = 0U;
  109. }
  110. }
  111. return (pend);
  112. }
  113. void arm_gic_set_pending_irq(rt_uint64_t index, int irq)
  114. {
  115. RT_ASSERT(index < ARM_GIC_MAX_NR);
  116. irq = irq - _gic_table[index].offset;
  117. RT_ASSERT(irq >= 0U);
  118. if (irq >= 16U)
  119. {
  120. GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) = 1U << (irq % 32U);
  121. }
  122. else
  123. {
  124. /* INTID 0-15 Software Generated Interrupt */
  125. /* Forward the interrupt to the CPU interface that requested it */
  126. GIC_DIST_SOFTINT(_gic_table[index].dist_hw_base) = (irq | 0x02000000U);
  127. }
  128. }
  129. void arm_gic_clear_pending_irq(rt_uint64_t index, int irq)
  130. {
  131. rt_uint64_t mask;
  132. RT_ASSERT(index < ARM_GIC_MAX_NR);
  133. irq = irq - _gic_table[index].offset;
  134. RT_ASSERT(irq >= 0U);
  135. if (irq >= 16U)
  136. {
  137. mask = 1U << (irq % 32U);
  138. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  139. }
  140. else
  141. {
  142. mask = 1U << ((irq % 4U) * 8U);
  143. GIC_DIST_CPENDSGI(_gic_table[index].dist_hw_base, irq) = mask;
  144. }
  145. }
  146. void arm_gic_set_configuration(rt_uint64_t index, int irq, uint32_t config)
  147. {
  148. rt_uint64_t icfgr;
  149. rt_uint64_t shift;
  150. RT_ASSERT(index < ARM_GIC_MAX_NR);
  151. irq = irq - _gic_table[index].offset;
  152. RT_ASSERT(irq >= 0U);
  153. icfgr = GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq);
  154. shift = (irq % 16U) << 1U;
  155. icfgr &= (~(3U << shift));
  156. icfgr |= (config << shift);
  157. GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq) = icfgr;
  158. }
  159. rt_uint64_t arm_gic_get_configuration(rt_uint64_t index, int irq)
  160. {
  161. RT_ASSERT(index < ARM_GIC_MAX_NR);
  162. irq = irq - _gic_table[index].offset;
  163. RT_ASSERT(irq >= 0U);
  164. return (GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq) >> ((irq % 16U) >> 1U));
  165. }
  166. void arm_gic_clear_active(rt_uint64_t index, int irq)
  167. {
  168. rt_uint64_t mask = 1U << (irq % 32U);
  169. RT_ASSERT(index < ARM_GIC_MAX_NR);
  170. irq = irq - _gic_table[index].offset;
  171. RT_ASSERT(irq >= 0U);
  172. GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  173. }
  174. /* Set up the cpu mask for the specific interrupt */
  175. void arm_gic_set_cpu(rt_uint64_t index, int irq, unsigned int cpumask)
  176. {
  177. rt_uint64_t old_tgt;
  178. RT_ASSERT(index < ARM_GIC_MAX_NR);
  179. irq = irq - _gic_table[index].offset;
  180. RT_ASSERT(irq >= 0U);
  181. old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);
  182. old_tgt &= ~(0x0FFUL << ((irq % 4U)*8U));
  183. old_tgt |= cpumask << ((irq % 4U)*8U);
  184. GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
  185. }
  186. rt_uint64_t arm_gic_get_target_cpu(rt_uint64_t index, int irq)
  187. {
  188. RT_ASSERT(index < ARM_GIC_MAX_NR);
  189. irq = irq - _gic_table[index].offset;
  190. RT_ASSERT(irq >= 0U);
  191. return (GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) >> ((irq % 4U) * 8U)) & 0xFFUL;
  192. }
  193. void arm_gic_set_priority(rt_uint64_t index, int irq, rt_uint64_t priority)
  194. {
  195. rt_uint64_t mask;
  196. RT_ASSERT(index < ARM_GIC_MAX_NR);
  197. irq = irq - _gic_table[index].offset;
  198. RT_ASSERT(irq >= 0U);
  199. mask = GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq);
  200. mask &= ~(0xFFUL << ((irq % 4U) * 8U));
  201. mask |= ((priority & 0xFFUL) << ((irq % 4U) * 8U));
  202. GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) = mask;
  203. }
  204. rt_uint64_t arm_gic_get_priority(rt_uint64_t index, int irq)
  205. {
  206. RT_ASSERT(index < ARM_GIC_MAX_NR);
  207. irq = irq - _gic_table[index].offset;
  208. RT_ASSERT(irq >= 0U);
  209. return (GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4U) * 8U)) & 0xFFUL;
  210. }
  211. void arm_gic_set_interface_prior_mask(rt_uint64_t index, rt_uint64_t priority)
  212. {
  213. RT_ASSERT(index < ARM_GIC_MAX_NR);
  214. /* set priority mask */
  215. GIC_CPU_PRIMASK(_gic_table[index].cpu_hw_base) = priority & 0xFFUL;
  216. }
  217. rt_uint64_t arm_gic_get_interface_prior_mask(rt_uint64_t index)
  218. {
  219. RT_ASSERT(index < ARM_GIC_MAX_NR);
  220. return GIC_CPU_PRIMASK(_gic_table[index].cpu_hw_base);
  221. }
  222. void arm_gic_set_binary_point(rt_uint64_t index, rt_uint64_t binary_point)
  223. {
  224. GIC_CPU_BINPOINT(_gic_table[index].cpu_hw_base) = binary_point & 0x7U;
  225. }
  226. rt_uint64_t arm_gic_get_binary_point(rt_uint64_t index)
  227. {
  228. return GIC_CPU_BINPOINT(_gic_table[index].cpu_hw_base);
  229. }
  230. rt_uint64_t arm_gic_get_irq_status(rt_uint64_t index, int irq)
  231. {
  232. rt_uint64_t pending;
  233. rt_uint64_t active;
  234. RT_ASSERT(index < ARM_GIC_MAX_NR);
  235. irq = irq - _gic_table[index].offset;
  236. RT_ASSERT(irq >= 0U);
  237. active = (GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32U)) & 0x1UL;
  238. pending = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32U)) & 0x1UL;
  239. return ((active << 1U) | pending);
  240. }
  241. void arm_gic_send_sgi(rt_uint64_t index, int irq, rt_uint64_t target_list, rt_uint64_t filter_list)
  242. {
  243. RT_ASSERT(index < ARM_GIC_MAX_NR);
  244. irq = irq - _gic_table[index].offset;
  245. RT_ASSERT(irq >= 0U);
  246. GIC_DIST_SOFTINT(_gic_table[index].dist_hw_base) =
  247. ((filter_list & 0x3U) << 24U) | ((target_list & 0xFFUL) << 16U) | (irq & 0x0FUL);
  248. }
  249. rt_uint64_t arm_gic_get_high_pending_irq(rt_uint64_t index)
  250. {
  251. RT_ASSERT(index < ARM_GIC_MAX_NR);
  252. return GIC_CPU_HIGHPRI(_gic_table[index].cpu_hw_base);
  253. }
  254. rt_uint64_t arm_gic_get_interface_id(rt_uint64_t index)
  255. {
  256. RT_ASSERT(index < ARM_GIC_MAX_NR);
  257. return GIC_CPU_IIDR(_gic_table[index].cpu_hw_base);
  258. }
  259. void arm_gic_set_group(rt_uint64_t index, int irq, rt_uint64_t group)
  260. {
  261. uint32_t igroupr;
  262. uint32_t shift;
  263. RT_ASSERT(index < ARM_GIC_MAX_NR);
  264. RT_ASSERT(group <= 1U);
  265. irq = irq - _gic_table[index].offset;
  266. RT_ASSERT(irq >= 0U);
  267. igroupr = GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq);
  268. shift = (irq % 32U);
  269. igroupr &= (~(1U << shift));
  270. igroupr |= ((group & 0x1U) << shift);
  271. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) = igroupr;
  272. }
  273. rt_uint64_t arm_gic_get_group(rt_uint64_t index, int irq)
  274. {
  275. RT_ASSERT(index < ARM_GIC_MAX_NR);
  276. irq = irq - _gic_table[index].offset;
  277. RT_ASSERT(irq >= 0U);
  278. return (GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) >> (irq % 32U)) & 0x1UL;
  279. }
  280. int arm_gic_dist_init(rt_uint64_t index, rt_uint64_t dist_base, int irq_start)
  281. {
  282. unsigned int gic_type, i;
  283. rt_uint64_t cpumask = 1U << 0U;
  284. RT_ASSERT(index < ARM_GIC_MAX_NR);
  285. _gic_table[index].dist_hw_base = dist_base;
  286. _gic_table[index].offset = irq_start;
  287. /* Find out how many interrupts are supported. */
  288. gic_type = GIC_DIST_TYPE(dist_base);
  289. _gic_max_irq = ((gic_type & 0x1fU) + 1U) * 32U;
  290. /*
  291. * The GIC only supports up to 1020 interrupt sources.
  292. * Limit this to either the architected maximum, or the
  293. * platform maximum.
  294. */
  295. if (_gic_max_irq > 1020U)
  296. {
  297. _gic_max_irq = 1020U;
  298. }
  299. if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */
  300. {
  301. _gic_max_irq = ARM_GIC_NR_IRQS;
  302. }
  303. cpumask |= cpumask << 8U;
  304. cpumask |= cpumask << 16U;
  305. cpumask |= cpumask << 24U;
  306. GIC_DIST_CTRL(dist_base) = 0x0U;
  307. /* Set all global interrupts to be level triggered, active low. */
  308. for (i = 32U; i < _gic_max_irq; i += 16U)
  309. {
  310. GIC_DIST_CONFIG(dist_base, i) = 0x0U;
  311. }
  312. /* Set all global interrupts to this CPU only. */
  313. for (i = 32U; i < _gic_max_irq; i += 4U)
  314. {
  315. GIC_DIST_TARGET(dist_base, i) = cpumask;
  316. }
  317. /* Set priority on all interrupts. */
  318. for (i = 0U; i < _gic_max_irq; i += 4U)
  319. {
  320. GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0U;
  321. }
  322. /* Disable all interrupts. */
  323. for (i = 0U; i < _gic_max_irq; i += 32U)
  324. {
  325. GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffffU;
  326. }
  327. /* All interrupts defaults to IGROUP1(IRQ). */
  328. for (i = 0U; i < _gic_max_irq; i += 32U)
  329. {
  330. GIC_DIST_IGROUP(dist_base, i) = 0U;
  331. }
  332. /* Enable group0 and group1 interrupt forwarding. */
  333. GIC_DIST_CTRL(dist_base) = 0x01U;
  334. return 0;
  335. }
  336. int arm_gic_cpu_init(rt_uint64_t index, rt_uint64_t cpu_base)
  337. {
  338. RT_ASSERT(index < ARM_GIC_MAX_NR);
  339. if (!_gic_table[index].cpu_hw_base)
  340. {
  341. _gic_table[index].cpu_hw_base = cpu_base;
  342. }
  343. cpu_base = _gic_table[index].cpu_hw_base;
  344. GIC_CPU_PRIMASK(cpu_base) = 0xf0U;
  345. GIC_CPU_BINPOINT(cpu_base) = 0x7U;
  346. /* Enable CPU interrupt */
  347. GIC_CPU_CTRL(cpu_base) = 0x01U;
  348. return 0;
  349. }
  350. void arm_gic_dump_type(rt_uint64_t index)
  351. {
  352. unsigned int gic_type;
  353. gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
  354. rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n",
  355. (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4U) & 0xfUL,
  356. _gic_table[index].dist_hw_base,
  357. _gic_max_irq,
  358. gic_type & (1U << 10U) ? "has" : "no",
  359. gic_type);
  360. }
  361. void arm_gic_dump(rt_uint64_t index)
  362. {
  363. unsigned int i, k;
  364. k = GIC_CPU_HIGHPRI(_gic_table[index].cpu_hw_base);
  365. rt_kprintf("--- high pending priority: %d(%08x)\n", k, k);
  366. rt_kprintf("--- hw mask ---\n");
  367. for (i = 0U; i < _gic_max_irq / 32U; i++)
  368. {
  369. rt_kprintf("0x%08x, ", GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, i * 32U));
  370. }
  371. rt_kprintf("\n--- hw pending ---\n");
  372. for (i = 0U; i < _gic_max_irq / 32U; i++)
  373. {
  374. rt_kprintf("0x%08x, ", GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, i * 32U));
  375. }
  376. rt_kprintf("\n--- hw active ---\n");
  377. for (i = 0U; i < _gic_max_irq / 32U; i++)
  378. {
  379. rt_kprintf("0x%08x, ", GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base, i * 32U));
  380. }
  381. rt_kprintf("\n");
  382. }
  383. long gic_dump(void)
  384. {
  385. arm_gic_dump_type(0);
  386. arm_gic_dump(0);
  387. return 0;
  388. }
  389. MSH_CMD_EXPORT(gic_dump, show gic status);