1
0

gic.c 14 KB

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