gicv3.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * Copyright (c) 2006-2022, 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. * add GICv3 AArch64 system register interface
  14. * modify arm_gic_redist_init() args
  15. * modify arm_gic_cpu_init() args
  16. * modify arm_gic_send_affinity_sgi() args
  17. * remove arm_gic_redist_address_set()
  18. * remove arm_gic_cpu_interface_address_set()
  19. * remove arm_gic_secondary_cpu_init()
  20. * remove get_main_cpu_affval()
  21. * remove arm_gic_cpumask_to_affval()
  22. */
  23. #include <rthw.h>
  24. #include <rtthread.h>
  25. #if defined(BSP_USING_GIC) && defined(BSP_USING_GICV3)
  26. #include <gicv3.h>
  27. #include <cpuport.h>
  28. #include <board.h>
  29. #ifndef ARM_SPI_BIND_CPU_ID
  30. #define ARM_SPI_BIND_CPU_ID 0
  31. #endif
  32. #ifndef RT_USING_SMP
  33. #define RT_CPUS_NR 1
  34. extern int rt_hw_cpu_id(void);
  35. #else
  36. extern rt_uint64_t rt_cpu_mpidr_early[];
  37. #endif /* RT_USING_SMP */
  38. struct arm_gic
  39. {
  40. rt_uint64_t offset; /* the first interrupt index in the vector table */
  41. rt_uint64_t redist_hw_base[RT_CPUS_NR]; /* the pointer of the gic redistributor */
  42. rt_uint64_t dist_hw_base; /* the base address of the gic distributor */
  43. rt_uint64_t cpu_hw_base[RT_CPUS_NR]; /* the base address of the gic cpu interface */
  44. };
  45. /* 'ARM_GIC_MAX_NR' is the number of cores */
  46. static struct arm_gic _gic_table[ARM_GIC_MAX_NR];
  47. static unsigned int _gic_max_irq;
  48. #define GET_GICV3_REG(reg, out) __asm__ volatile ("mrs %0, " reg:"=r"(out)::"memory");
  49. #define SET_GICV3_REG(reg, in) __asm__ volatile ("msr " reg ", %0"::"r"(in):"memory");
  50. /* AArch64 System register interface to GICv3 */
  51. #define ICC_IAR0_EL1 "S3_0_C12_C8_0"
  52. #define ICC_IAR1_EL1 "S3_0_C12_C12_0"
  53. #define ICC_EOIR0_EL1 "S3_0_C12_C8_1"
  54. #define ICC_EOIR1_EL1 "S3_0_C12_C12_1"
  55. #define ICC_HPPIR0_EL1 "S3_0_C12_C8_2"
  56. #define ICC_HPPIR1_EL1 "S3_0_C12_C12_2"
  57. #define ICC_BPR0_EL1 "S3_0_C12_C8_3"
  58. #define ICC_BPR1_EL1 "S3_0_C12_C12_3"
  59. #define ICC_DIR_EL1 "S3_0_C12_C11_1"
  60. #define ICC_PMR_EL1 "S3_0_C4_C6_0"
  61. #define ICC_RPR_EL1 "S3_0_C12_C11_3"
  62. #define ICC_CTLR_EL1 "S3_0_C12_C12_4"
  63. #define ICC_CTLR_EL3 "S3_6_C12_C12_4"
  64. #define ICC_SRE_EL1 "S3_0_C12_C12_5"
  65. #define ICC_SRE_EL2 "S3_4_C12_C9_5"
  66. #define ICC_SRE_EL3 "S3_6_C12_C12_5"
  67. #define ICC_IGRPEN0_EL1 "S3_0_C12_C12_6"
  68. #define ICC_IGRPEN1_EL1 "S3_0_C12_C12_7"
  69. #define ICC_IGRPEN1_EL3 "S3_6_C12_C12_7"
  70. #define ICC_SGI0R_EL1 "S3_0_C12_C11_7"
  71. #define ICC_SGI1R_EL1 "S3_0_C12_C11_5"
  72. #define ICC_ASGI1R_EL1 "S3_0_C12_C11_6"
  73. /* Macro to access the Distributor Control Register (GICD_CTLR) */
  74. #define GICD_CTLR_RWP (1 << 31)
  75. #define GICD_CTLR_E1NWF (1 << 7)
  76. #define GICD_CTLR_DS (1 << 6)
  77. #define GICD_CTLR_ARE_NS (1 << 5)
  78. #define GICD_CTLR_ARE_S (1 << 4)
  79. #define GICD_CTLR_ENGRP1S (1 << 2)
  80. #define GICD_CTLR_ENGRP1NS (1 << 1)
  81. #define GICD_CTLR_ENGRP0 (1 << 0)
  82. /* Macro to access the Redistributor Control Register (GICR_CTLR) */
  83. #define GICR_CTLR_UWP (1 << 31)
  84. #define GICR_CTLR_DPG1S (1 << 26)
  85. #define GICR_CTLR_DPG1NS (1 << 25)
  86. #define GICR_CTLR_DPG0 (1 << 24)
  87. #define GICR_CTLR_RWP (1 << 3)
  88. #define GICR_CTLR_IR (1 << 2)
  89. #define GICR_CTLR_CES (1 << 1)
  90. #define GICR_CTLR_EnableLPI (1 << 0)
  91. /* Macro to access the Generic Interrupt Controller Interface (GICC) */
  92. #define GIC_CPU_CTRL(hw_base) HWREG32((hw_base) + 0x00U)
  93. #define GIC_CPU_PRIMASK(hw_base) HWREG32((hw_base) + 0x04U)
  94. #define GIC_CPU_BINPOINT(hw_base) HWREG32((hw_base) + 0x08U)
  95. #define GIC_CPU_INTACK(hw_base) HWREG32((hw_base) + 0x0cU)
  96. #define GIC_CPU_EOI(hw_base) HWREG32((hw_base) + 0x10U)
  97. #define GIC_CPU_RUNNINGPRI(hw_base) HWREG32((hw_base) + 0x14U)
  98. #define GIC_CPU_HIGHPRI(hw_base) HWREG32((hw_base) + 0x18U)
  99. #define GIC_CPU_IIDR(hw_base) HWREG32((hw_base) + 0xFCU)
  100. /* Macro to access the Generic Interrupt Controller Distributor (GICD) */
  101. #define GIC_DIST_CTRL(hw_base) HWREG32((hw_base) + 0x000U)
  102. #define GIC_DIST_TYPE(hw_base) HWREG32((hw_base) + 0x004U)
  103. #define GIC_DIST_IGROUP(hw_base, n) HWREG32((hw_base) + 0x080U + ((n) / 32U) * 4U)
  104. #define GIC_DIST_ENABLE_SET(hw_base, n) HWREG32((hw_base) + 0x100U + ((n) / 32U) * 4U)
  105. #define GIC_DIST_ENABLE_CLEAR(hw_base, n) HWREG32((hw_base) + 0x180U + ((n) / 32U) * 4U)
  106. #define GIC_DIST_PENDING_SET(hw_base, n) HWREG32((hw_base) + 0x200U + ((n) / 32U) * 4U)
  107. #define GIC_DIST_PENDING_CLEAR(hw_base, n) HWREG32((hw_base) + 0x280U + ((n) / 32U) * 4U)
  108. #define GIC_DIST_ACTIVE_SET(hw_base, n) HWREG32((hw_base) + 0x300U + ((n) / 32U) * 4U)
  109. #define GIC_DIST_ACTIVE_CLEAR(hw_base, n) HWREG32((hw_base) + 0x380U + ((n) / 32U) * 4U)
  110. #define GIC_DIST_PRI(hw_base, n) HWREG32((hw_base) + 0x400U + ((n) / 4U) * 4U)
  111. #define GIC_DIST_TARGET(hw_base, n) HWREG32((hw_base) + 0x800U + ((n) / 4U) * 4U)
  112. #define GIC_DIST_CONFIG(hw_base, n) HWREG32((hw_base) + 0xc00U + ((n) / 16U) * 4U)
  113. #define GIC_DIST_SOFTINT(hw_base) HWREG32((hw_base) + 0xf00U)
  114. #define GIC_DIST_CPENDSGI(hw_base, n) HWREG32((hw_base) + 0xf10U + ((n) / 4U) * 4U)
  115. #define GIC_DIST_SPENDSGI(hw_base, n) HWREG32((hw_base) + 0xf20U + ((n) / 4U) * 4U)
  116. #define GIC_DIST_ICPIDR2(hw_base) HWREG32((hw_base) + 0xfe8U)
  117. #define GIC_DIST_IROUTER(hw_base, n) HWREG64((hw_base) + 0x6000U + (n) * 8U)
  118. /* SGI base address is at 64K offset from Redistributor base address */
  119. #define GIC_RSGI_OFFSET 0x10000
  120. /* Macro to access the Generic Interrupt Controller Redistributor (GICR) */
  121. #define GIC_RDIST_CTRL(hw_base) HWREG32((hw_base) + 0x000U)
  122. #define GIC_RDIST_IIDR(hw_base) HWREG32((hw_base) + 0x004U)
  123. #define GIC_RDIST_TYPER(hw_base) HWREG64((hw_base) + 0x008U)
  124. #define GIC_RDIST_TSTATUSR(hw_base) HWREG32((hw_base) + 0x010U)
  125. #define GIC_RDIST_WAKER(hw_base) HWREG32((hw_base) + 0x014U)
  126. #define GIC_RDIST_SETLPIR(hw_base) HWREG32((hw_base) + 0x040U)
  127. #define GIC_RDIST_CLRLPIR(hw_base) HWREG32((hw_base) + 0x048U)
  128. #define GIC_RDIST_PROPBASER(hw_base) HWREG32((hw_base) + 0x070U)
  129. #define GIC_RDIST_PENDBASER(hw_base) HWREG32((hw_base) + 0x078U)
  130. #define GIC_RDIST_INVLPIR(hw_base) HWREG32((hw_base) + 0x0A0U)
  131. #define GIC_RDIST_INVALLR(hw_base) HWREG32((hw_base) + 0x0B0U)
  132. #define GIC_RDIST_SYNCR(hw_base) HWREG32((hw_base) + 0x0C0U)
  133. #define GIC_RDISTSGI_IGROUPR0(hw_base, n) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x080U + (n) * 4U)
  134. #define GIC_RDISTSGI_ISENABLER0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x100U)
  135. #define GIC_RDISTSGI_ICENABLER0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x180U)
  136. #define GIC_RDISTSGI_ISPENDR0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x200U)
  137. #define GIC_RDISTSGI_ICPENDR0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x280U)
  138. #define GIC_RDISTSGI_ISACTIVER0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x300U)
  139. #define GIC_RDISTSGI_ICACTIVER0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x380U)
  140. #define GIC_RDISTSGI_IPRIORITYR(hw_base, n) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0x400U + ((n) / 4U) * 4U)
  141. #define GIC_RDISTSGI_ICFGR0(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0xC00U)
  142. #define GIC_RDISTSGI_ICFGR1(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0xC04U)
  143. #define GIC_RDISTSGI_IGRPMODR0(hw_base, n) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0xD00U + (n) * 4)
  144. #define GIC_RDISTSGI_NSACR(hw_base) HWREG32((hw_base) + GIC_RSGI_OFFSET + 0xE00U)
  145. int arm_gic_get_active_irq(rt_uint64_t index)
  146. {
  147. int irq;
  148. RT_ASSERT(index < ARM_GIC_MAX_NR);
  149. GET_GICV3_REG(ICC_IAR1_EL1, irq);
  150. irq = (irq & 0x1ffffff) + _gic_table[index].offset;
  151. return irq;
  152. }
  153. void arm_gic_ack(rt_uint64_t index, int irq)
  154. {
  155. RT_ASSERT(index < ARM_GIC_MAX_NR);
  156. RT_ASSERT(irq >= 0);
  157. __DSB();
  158. SET_GICV3_REG(ICC_EOIR1_EL1, irq);
  159. }
  160. void arm_gic_mask(rt_uint64_t index, int irq)
  161. {
  162. rt_uint64_t mask = 1 << (irq % 32);
  163. RT_ASSERT(index < ARM_GIC_MAX_NR);
  164. irq = irq - _gic_table[index].offset;
  165. RT_ASSERT(irq >= 0);
  166. if (irq < 32)
  167. {
  168. rt_int32_t cpu_id = rt_hw_cpu_id();
  169. GIC_RDISTSGI_ICENABLER0(_gic_table[index].redist_hw_base[cpu_id]) = mask;
  170. }
  171. else
  172. {
  173. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  174. }
  175. }
  176. void arm_gic_umask(rt_uint64_t index, int irq)
  177. {
  178. rt_uint64_t mask = 1 << (irq % 32);
  179. RT_ASSERT(index < ARM_GIC_MAX_NR);
  180. irq = irq - _gic_table[index].offset;
  181. RT_ASSERT(irq >= 0);
  182. if (irq < 32)
  183. {
  184. rt_int32_t cpu_id = rt_hw_cpu_id();
  185. GIC_RDISTSGI_ISENABLER0(_gic_table[index].redist_hw_base[cpu_id]) = mask;
  186. }
  187. else
  188. {
  189. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  190. }
  191. }
  192. rt_uint64_t arm_gic_get_pending_irq(rt_uint64_t index, int irq)
  193. {
  194. rt_uint64_t pend;
  195. RT_ASSERT(index < ARM_GIC_MAX_NR);
  196. irq = irq - _gic_table[index].offset;
  197. RT_ASSERT(irq >= 0);
  198. if (irq >= 16)
  199. {
  200. pend = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1;
  201. }
  202. else
  203. {
  204. /* INTID 0-15 Software Generated Interrupt */
  205. pend = (GIC_DIST_SPENDSGI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4) * 8)) & 0xff;
  206. /* No CPU identification offered */
  207. if (pend != 0)
  208. {
  209. pend = 1;
  210. }
  211. else
  212. {
  213. pend = 0;
  214. }
  215. }
  216. return pend;
  217. }
  218. void arm_gic_set_pending_irq(rt_uint64_t index, int irq)
  219. {
  220. RT_ASSERT(index < ARM_GIC_MAX_NR);
  221. irq = irq - _gic_table[index].offset;
  222. RT_ASSERT(irq >= 0);
  223. if (irq >= 16)
  224. {
  225. GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) = 1 << (irq % 32);
  226. }
  227. else
  228. {
  229. /* INTID 0-15 Software Generated Interrupt */
  230. /* Forward the interrupt to the CPU interface that requested it */
  231. GIC_DIST_SOFTINT(_gic_table[index].dist_hw_base) = (irq | 0x02000000);
  232. }
  233. }
  234. void arm_gic_clear_pending_irq(rt_uint64_t index, int irq)
  235. {
  236. rt_uint64_t mask;
  237. RT_ASSERT(index < ARM_GIC_MAX_NR);
  238. irq = irq - _gic_table[index].offset;
  239. RT_ASSERT(irq >= 0);
  240. if (irq >= 16)
  241. {
  242. mask = 1 << (irq % 32);
  243. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  244. }
  245. else
  246. {
  247. mask = 1 << ((irq % 4) * 8);
  248. GIC_DIST_CPENDSGI(_gic_table[index].dist_hw_base, irq) = mask;
  249. }
  250. }
  251. void arm_gic_set_configuration(rt_uint64_t index, int irq, rt_uint32_t config)
  252. {
  253. rt_uint64_t icfgr;
  254. rt_uint64_t shift;
  255. RT_ASSERT(index < ARM_GIC_MAX_NR);
  256. irq = irq - _gic_table[index].offset;
  257. RT_ASSERT(irq >= 0);
  258. icfgr = GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq);
  259. shift = (irq % 16) << 1;
  260. icfgr &= (~(3 << shift));
  261. icfgr |= (config << shift);
  262. GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq) = icfgr;
  263. }
  264. rt_uint64_t arm_gic_get_configuration(rt_uint64_t index, int irq)
  265. {
  266. RT_ASSERT(index < ARM_GIC_MAX_NR);
  267. irq = irq - _gic_table[index].offset;
  268. RT_ASSERT(irq >= 0);
  269. return (GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq) >> ((irq % 16) >> 1));
  270. }
  271. void arm_gic_clear_active(rt_uint64_t index, int irq)
  272. {
  273. rt_uint64_t mask = 1 << (irq % 32);
  274. RT_ASSERT(index < ARM_GIC_MAX_NR);
  275. irq = irq - _gic_table[index].offset;
  276. RT_ASSERT(irq >= 0);
  277. GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  278. }
  279. /* Set up the cpu mask for the specific interrupt */
  280. void arm_gic_set_cpu(rt_uint64_t index, int irq, unsigned int cpumask)
  281. {
  282. rt_uint64_t old_tgt;
  283. RT_ASSERT(index < ARM_GIC_MAX_NR);
  284. irq = irq - _gic_table[index].offset;
  285. RT_ASSERT(irq >= 0);
  286. old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);
  287. old_tgt &= ~(0x0ff << ((irq % 4) * 8));
  288. old_tgt |= cpumask << ((irq % 4) * 8);
  289. GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
  290. }
  291. rt_uint64_t arm_gic_get_target_cpu(rt_uint64_t index, int irq)
  292. {
  293. RT_ASSERT(index < ARM_GIC_MAX_NR);
  294. irq = irq - _gic_table[index].offset;
  295. RT_ASSERT(irq >= 0);
  296. return (GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) >> ((irq % 4) * 8)) & 0xff;
  297. }
  298. void arm_gic_set_priority(rt_uint64_t index, int irq, rt_uint64_t priority)
  299. {
  300. rt_uint64_t mask;
  301. RT_ASSERT(index < ARM_GIC_MAX_NR);
  302. irq = irq - _gic_table[index].offset;
  303. RT_ASSERT(irq >= 0);
  304. if (irq < 32)
  305. {
  306. rt_int32_t cpu_id = rt_hw_cpu_id();
  307. mask = GIC_RDISTSGI_IPRIORITYR(_gic_table[index].redist_hw_base[cpu_id], irq);
  308. mask &= ~(0xff << ((irq % 4) * 8));
  309. mask |= ((priority & 0xff) << ((irq % 4) * 8));
  310. GIC_RDISTSGI_IPRIORITYR(_gic_table[index].redist_hw_base[cpu_id], irq) = mask;
  311. }
  312. else
  313. {
  314. mask = GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq);
  315. mask &= ~(0xff << ((irq % 4) * 8));
  316. mask |= ((priority & 0xff) << ((irq % 4) * 8));
  317. GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) = mask;
  318. }
  319. }
  320. rt_uint64_t arm_gic_get_priority(rt_uint64_t index, int irq)
  321. {
  322. RT_ASSERT(index < ARM_GIC_MAX_NR);
  323. irq = irq - _gic_table[index].offset;
  324. RT_ASSERT(irq >= 0);
  325. if (irq < 32)
  326. {
  327. rt_int32_t cpu_id = rt_hw_cpu_id();
  328. return (GIC_RDISTSGI_IPRIORITYR(_gic_table[index].redist_hw_base[cpu_id], irq) >> ((irq % 4) * 8)) & 0xff;
  329. }
  330. else
  331. {
  332. return (GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4) * 8)) & 0xff;
  333. }
  334. }
  335. void arm_gic_set_system_register_enable_mask(rt_uint64_t index, rt_uint64_t value)
  336. {
  337. RT_ASSERT(index < ARM_GIC_MAX_NR);
  338. value &= 0xff;
  339. /* set priority mask */
  340. SET_GICV3_REG(ICC_SRE_EL1, value);
  341. __ISB();
  342. }
  343. rt_uint64_t arm_gic_get_system_register_enable_mask(rt_uint64_t index)
  344. {
  345. RT_ASSERT(index < ARM_GIC_MAX_NR);
  346. rt_uint64_t value;
  347. GET_GICV3_REG(ICC_SRE_EL1, value);
  348. return value;
  349. }
  350. void arm_gic_set_interface_prior_mask(rt_uint64_t index, rt_uint64_t priority)
  351. {
  352. RT_ASSERT(index < ARM_GIC_MAX_NR);
  353. priority &= 0xff;
  354. /* set priority mask */
  355. SET_GICV3_REG(ICC_PMR_EL1, priority);
  356. }
  357. rt_uint64_t arm_gic_get_interface_prior_mask(rt_uint64_t index)
  358. {
  359. RT_ASSERT(index < ARM_GIC_MAX_NR);
  360. rt_uint64_t priority;
  361. GET_GICV3_REG(ICC_PMR_EL1, priority);
  362. return priority;
  363. }
  364. void arm_gic_set_binary_point(rt_uint64_t index, rt_uint64_t binary_point)
  365. {
  366. index = index;
  367. binary_point &= 0x7;
  368. SET_GICV3_REG(ICC_BPR1_EL1, binary_point);
  369. }
  370. rt_uint64_t arm_gic_get_binary_point(rt_uint64_t index)
  371. {
  372. rt_uint64_t binary_point;
  373. index = index;
  374. GET_GICV3_REG(ICC_BPR1_EL1, binary_point);
  375. return binary_point;
  376. }
  377. rt_uint64_t arm_gic_get_irq_status(rt_uint64_t index, int irq)
  378. {
  379. rt_uint64_t pending, active;
  380. RT_ASSERT(index < ARM_GIC_MAX_NR);
  381. irq = irq - _gic_table[index].offset;
  382. RT_ASSERT(irq >= 0);
  383. active = (GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1;
  384. pending = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1;
  385. return ((active << 1) | pending);
  386. }
  387. #ifdef RT_USING_SMP
  388. void arm_gic_send_affinity_sgi(rt_uint64_t index, int irq, rt_uint64_t cpu_masks[], rt_uint64_t routing_mode)
  389. {
  390. const int cpu_mask_cpu_max_nr = sizeof(cpu_masks[0]) * 8;
  391. rt_uint64_t int_id = (irq & 0xf) << 24;
  392. rt_uint64_t irm = routing_mode << 40; /* Interrupt Routing Mode */
  393. if (routing_mode == GICV3_ROUTED_TO_SPEC)
  394. {
  395. int cpu_id, cpu_mask_bit, i, cpu_masks_nr = RT_CPUS_NR / cpu_mask_cpu_max_nr;
  396. rt_uint16_t target_list;
  397. rt_uint64_t rs = 0; /* Range Selector */
  398. rt_uint64_t affinity_val, next_affinity_val;
  399. if (cpu_masks_nr * cpu_mask_cpu_max_nr != RT_CPUS_NR)
  400. {
  401. ++cpu_masks_nr;
  402. }
  403. for (i = cpu_id = 0; i < cpu_masks_nr;)
  404. {
  405. /* No cpu in this mask */
  406. if (cpu_masks[i] == 0)
  407. {
  408. ++i;
  409. cpu_id += cpu_mask_cpu_max_nr;
  410. continue;
  411. }
  412. /* Get last cpu affinity value */
  413. affinity_val = rt_cpu_mpidr_early[cpu_id] & 0xff00ffff00ULL;
  414. /* Read 16 cpus information */
  415. for (cpu_mask_bit = 0; cpu_mask_bit < 16; ++cpu_mask_bit, ++cpu_id)
  416. {
  417. /* MPIDR_EL1: aff3[39:32], aff2[23:16], aff1[15:8] */
  418. next_affinity_val = rt_cpu_mpidr_early[cpu_id] & 0xff00ffff00ULL;
  419. /* Affinity value is different, read end */
  420. if (affinity_val != next_affinity_val)
  421. {
  422. break;
  423. }
  424. }
  425. /* Get all valid cpu mask */
  426. target_list = (0xffff >> (16 - cpu_mask_bit)) & cpu_masks[i];
  427. /* Clear read mask */
  428. cpu_masks[i] >>= cpu_mask_bit;
  429. /* ICC_SGI1R_EL1: aff3[55:48], aff2[39:32], aff1[23:16] */
  430. affinity_val <<= 8;
  431. __DSB();
  432. /* Interrupts routed to the PEs specified by Aff3.Aff2.Aff1.<target list>. */
  433. SET_GICV3_REG(ICC_SGI1R_EL1, affinity_val | (rs << 44) | irm | int_id | target_list);
  434. __ISB();
  435. /* Check if reset the range selector */
  436. rs = affinity_val != next_affinity_val ? 0 : rs + 1;
  437. }
  438. }
  439. else
  440. {
  441. __DSB();
  442. /* Interrupts routed to all PEs in the system, excluding "self". */
  443. SET_GICV3_REG(ICC_SGI1R_EL1, irm | int_id);
  444. __ISB();
  445. }
  446. }
  447. #endif /* RT_USING_SMP */
  448. rt_uint64_t arm_gic_get_high_pending_irq(rt_uint64_t index)
  449. {
  450. rt_uint64_t irq;
  451. RT_ASSERT(index < ARM_GIC_MAX_NR);
  452. index = index;
  453. GET_GICV3_REG(ICC_HPPIR1_EL1, irq);
  454. return irq;
  455. }
  456. rt_uint64_t arm_gic_get_interface_id(rt_uint64_t index)
  457. {
  458. rt_uint64_t ret = 0;
  459. RT_ASSERT(index < ARM_GIC_MAX_NR);
  460. if (_gic_table[index].cpu_hw_base != RT_NULL)
  461. {
  462. ret = GIC_CPU_IIDR(_gic_table[index].cpu_hw_base);
  463. }
  464. return ret;
  465. }
  466. void arm_gic_set_group(rt_uint64_t index, int irq, rt_uint64_t group)
  467. {
  468. rt_uint64_t igroupr;
  469. rt_uint64_t shift;
  470. RT_ASSERT(index < ARM_GIC_MAX_NR);
  471. RT_ASSERT(group <= 1);
  472. irq = irq - _gic_table[index].offset;
  473. RT_ASSERT(irq >= 0);
  474. igroupr = GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq);
  475. shift = (irq % 32);
  476. igroupr &= (~(1U << shift));
  477. igroupr |= ((group & 0x1U) << shift);
  478. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) = igroupr;
  479. }
  480. rt_uint64_t arm_gic_get_group(rt_uint64_t index, int irq)
  481. {
  482. RT_ASSERT(index < ARM_GIC_MAX_NR);
  483. irq = irq - _gic_table[index].offset;
  484. RT_ASSERT(irq >= 0);
  485. return (GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1UL;
  486. }
  487. static int arm_gicv3_wait_rwp(rt_uint64_t index, rt_uint64_t irq)
  488. {
  489. rt_uint64_t rwp_bit;
  490. rt_uint64_t base;
  491. RT_ASSERT(index < ARM_GIC_MAX_NR);
  492. if (irq < 32)
  493. {
  494. rt_int32_t cpu_id = rt_hw_cpu_id();
  495. base = _gic_table[index].redist_hw_base[cpu_id];
  496. rwp_bit = GICR_CTLR_RWP;
  497. }
  498. else
  499. {
  500. base = _gic_table[index].dist_hw_base;
  501. rwp_bit = GICD_CTLR_RWP;
  502. }
  503. while (HWREG32(base) & rwp_bit)
  504. {
  505. }
  506. return 0;
  507. }
  508. int arm_gic_dist_init(rt_uint64_t index, rt_uint64_t dist_base, int irq_start)
  509. {
  510. int i;
  511. unsigned int gic_type;
  512. rt_uint64_t main_cpu_affinity_val;
  513. RT_ASSERT(index < ARM_GIC_MAX_NR);
  514. _gic_table[index].dist_hw_base = dist_base;
  515. _gic_table[index].offset = irq_start;
  516. /* Find out how many interrupts are supported. */
  517. gic_type = GIC_DIST_TYPE(dist_base);
  518. _gic_max_irq = ((gic_type & 0x1f) + 1) * 32;
  519. /*
  520. * The GIC only supports up to 1020 interrupt sources.
  521. * Limit this to either the architected maximum, or the
  522. * platform maximum.
  523. */
  524. if (_gic_max_irq > 1020)
  525. {
  526. _gic_max_irq = 1020;
  527. }
  528. if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */
  529. {
  530. _gic_max_irq = ARM_GIC_NR_IRQS;
  531. }
  532. GIC_DIST_CTRL(dist_base) = 0;
  533. /* Wait for register write pending */
  534. arm_gicv3_wait_rwp(0, 32);
  535. /* Set all global interrupts to be level triggered, active low. */
  536. for (i = 32; i < _gic_max_irq; i += 16)
  537. {
  538. GIC_DIST_CONFIG(dist_base, i) = 0;
  539. }
  540. arm_gicv3_wait_rwp(0, 32);
  541. #ifdef RT_USING_SMP
  542. main_cpu_affinity_val = rt_cpu_mpidr_early[ARM_SPI_BIND_CPU_ID];
  543. #else
  544. __asm__ volatile ("mrs %0, mpidr_el1":"=r"(main_cpu_affinity_val));
  545. #endif
  546. /* aff3[39:32], aff2[23:16], aff1[15:8], aff0[7:0] */
  547. main_cpu_affinity_val &= 0xff00ffffffULL;
  548. /* Set all global interrupts to this CPU only. */
  549. for (i = 32; i < _gic_max_irq; i++)
  550. {
  551. GIC_DIST_IROUTER(dist_base, i) = main_cpu_affinity_val | (GICV3_ROUTED_TO_SPEC << 31);
  552. }
  553. arm_gicv3_wait_rwp(0, 32);
  554. /* Set priority on spi interrupts. */
  555. for (i = 32; i < _gic_max_irq; i += 4)
  556. {
  557. GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0;
  558. }
  559. arm_gicv3_wait_rwp(0, 32);
  560. /* Disable all interrupts. */
  561. for (i = 0; i < _gic_max_irq; i += 32)
  562. {
  563. GIC_DIST_PENDING_CLEAR(dist_base, i) = 0xffffffff;
  564. GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffff;
  565. }
  566. arm_gicv3_wait_rwp(0, 32);
  567. /* All interrupts defaults to IGROUP1(IRQ). */
  568. for (i = 0; i < _gic_max_irq; i += 32)
  569. {
  570. GIC_DIST_IGROUP(dist_base, i) = 0xffffffff;
  571. }
  572. arm_gicv3_wait_rwp(0, 32);
  573. /*
  574. * The Distributor control register (GICD_CTLR) must be configured to enable the interrupt groups and to set the routing mode.
  575. * Enable Affinity routing (ARE bits) The ARE bits in GICD_CTLR control whether affinity routing is enabled.
  576. * If affinity routing is not enabled, GICv3 can be configured for legacy operation.
  577. * Whether affinity routing is enabled or not can be controlled separately for Secure and Non-secure state.
  578. * Enables GICD_CTLR contains separate enable bits for Group 0, Secure Group 1 and Non-secure Group 1:
  579. * GICD_CTLR.EnableGrp1S enables distribution of Secure Group 1 interrupts.
  580. * GICD_CTLR.EnableGrp1NS enables distribution of Non-secure Group 1 interrupts.
  581. * GICD_CTLR.EnableGrp0 enables distribution of Group 0 interrupts.
  582. */
  583. GIC_DIST_CTRL(dist_base) = GICD_CTLR_ARE_NS | GICD_CTLR_ENGRP1NS;
  584. return 0;
  585. }
  586. int arm_gic_redist_init(rt_uint64_t index, rt_uint64_t redist_base)
  587. {
  588. int i;
  589. int cpu_id = rt_hw_cpu_id();
  590. RT_ASSERT(index < ARM_GIC_MAX_NR);
  591. redist_base += cpu_id * (2 << 16);
  592. _gic_table[index].redist_hw_base[cpu_id] = redist_base;
  593. /* redistributor enable */
  594. GIC_RDIST_WAKER(redist_base) &= ~(1 << 1);
  595. while (GIC_RDIST_WAKER(redist_base) & (1 << 2))
  596. {
  597. }
  598. /* Disable all sgi and ppi interrupt */
  599. GIC_RDISTSGI_ICENABLER0(redist_base) = 0xffffffff;
  600. arm_gicv3_wait_rwp(0, 0);
  601. /* Clear all inetrrupt pending */
  602. GIC_RDISTSGI_ICPENDR0(redist_base) = 0xffffffff;
  603. /* the corresponding interrupt is Group 1 or Non-secure Group 1. */
  604. GIC_RDISTSGI_IGROUPR0(redist_base, 0) = 0xffffffff;
  605. GIC_RDISTSGI_IGRPMODR0(redist_base, 0) = 0xffffffff;
  606. /* Configure default priorities for SGI 0:15 and PPI 16:31. */
  607. for (i = 0; i < 32; i += 4)
  608. {
  609. GIC_RDISTSGI_IPRIORITYR(redist_base, i) = 0xa0a0a0a0U;
  610. }
  611. /* Trigger level for PPI interrupts*/
  612. GIC_RDISTSGI_ICFGR1(redist_base) = 0;
  613. return 0;
  614. }
  615. int arm_gic_cpu_init(rt_uint64_t index, rt_uint64_t cpu_base)
  616. {
  617. rt_uint64_t value;
  618. int cpu_id = rt_hw_cpu_id();
  619. RT_ASSERT(index < ARM_GIC_MAX_NR);
  620. _gic_table[index].cpu_hw_base[cpu_id] = cpu_base;
  621. value = arm_gic_get_system_register_enable_mask(index);
  622. value |= (1 << 0);
  623. arm_gic_set_system_register_enable_mask(index, value);
  624. SET_GICV3_REG(ICC_CTLR_EL1, 0);
  625. arm_gic_set_interface_prior_mask(index, 0xff);
  626. /* Enable group1 interrupt */
  627. value = 1;
  628. SET_GICV3_REG(ICC_IGRPEN1_EL1, value);
  629. arm_gic_set_binary_point(0, 0);
  630. /* ICC_BPR0_EL1 determines the preemption group for both Group 0 and Group 1 interrupts. */
  631. value = 1; /* ICC_BPR0_EL1 determines the preemption group for both Group 0 and Group 1 interrupts.*/
  632. value |= 1 << 18; /* Targeted SGIs with affinity level 0 values of 0 - 255 are supported. */
  633. SET_GICV3_REG(ICC_CTLR_EL1, value);
  634. return 0;
  635. }
  636. void arm_gic_dump_type(rt_uint64_t index)
  637. {
  638. unsigned int gic_type;
  639. gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
  640. rt_kprintf("GICv%d on %p, max IRQs: %d, %s security extension(%08x)\n",
  641. (GIC_DIST_ICPIDR2(_gic_table[index].dist_hw_base) >> 4) & 0xf,
  642. _gic_table[index].dist_hw_base,
  643. _gic_max_irq,
  644. gic_type & (1 << 10) ? "has" : "no",
  645. gic_type);
  646. }
  647. void arm_gic_dump(rt_uint64_t index)
  648. {
  649. int i;
  650. unsigned int val;
  651. val = arm_gic_get_high_pending_irq(0);
  652. rt_kprintf("--- high pending priority: %d(%08x)\n", val, val);
  653. rt_kprintf("--- hw mask ---\n");
  654. for (i = 0; i < _gic_max_irq / 32; ++i)
  655. {
  656. rt_kprintf("0x%08x, ", GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, i * 32));
  657. }
  658. rt_kprintf("\b\b\n--- hw pending ---\n");
  659. for (i = 0; i < _gic_max_irq / 32; ++i)
  660. {
  661. rt_kprintf("0x%08x, ", GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, i * 32));
  662. }
  663. rt_kprintf("\b\b\n--- hw active ---\n");
  664. for (i = 0; i < _gic_max_irq / 32; ++i)
  665. {
  666. rt_kprintf("0x%08x, ", GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base, i * 32));
  667. }
  668. rt_kprintf("\b\b\n");
  669. }
  670. long gic_dump(void)
  671. {
  672. arm_gic_dump_type(0);
  673. arm_gic_dump(0);
  674. return 0;
  675. }
  676. MSH_CMD_EXPORT(gic_dump, show gic status);
  677. #endif /* defined(BSP_USING_GIC) && defined(BSP_USING_GICV3) */