gicv3.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 <cp15.h>
  28. #include <board.h>
  29. #ifndef ARM_SPI_BIND_CPU_ID
  30. #define ARM_SPI_BIND_CPU_ID 0
  31. #endif
  32. #if !defined(RT_USING_SMP) && !defined(RT_USING_AMP)
  33. #define RT_CPUS_NR 1
  34. #else
  35. extern rt_uint64_t rt_cpu_mpidr_table[];
  36. #endif /* RT_USING_SMP */
  37. /* 'ARM_GIC_MAX_NR' is the number of cores */
  38. static struct arm_gic _gic_table[ARM_GIC_MAX_NR];
  39. static unsigned int _gic_max_irq;
  40. int arm_gic_get_active_irq(rt_uint64_t index)
  41. {
  42. rt_base_t irq;
  43. RT_ASSERT(index < ARM_GIC_MAX_NR);
  44. GET_GICV3_REG(ICC_IAR1_EL1, irq);
  45. irq = (irq & 0x1ffffff) + _gic_table[index].offset;
  46. return irq;
  47. }
  48. void arm_gic_ack(rt_uint64_t index, int irq)
  49. {
  50. RT_ASSERT(index < ARM_GIC_MAX_NR);
  51. RT_ASSERT(irq >= 0);
  52. __DSB();
  53. SET_GICV3_REG(ICC_EOIR1_EL1, (rt_base_t)irq);
  54. }
  55. void arm_gic_mask(rt_uint64_t index, int irq)
  56. {
  57. rt_uint64_t mask = 1 << (irq % 32);
  58. RT_ASSERT(index < ARM_GIC_MAX_NR);
  59. irq = irq - _gic_table[index].offset;
  60. RT_ASSERT(irq >= 0);
  61. if (irq < 32)
  62. {
  63. rt_int32_t cpu_id = rt_hw_cpu_id();
  64. GIC_RDISTSGI_ICENABLER0(_gic_table[index].redist_hw_base[cpu_id]) = mask;
  65. }
  66. else
  67. {
  68. GIC_DIST_ENABLE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  69. }
  70. }
  71. void arm_gic_umask(rt_uint64_t index, int irq)
  72. {
  73. rt_uint64_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. if (irq < 32)
  78. {
  79. rt_int32_t cpu_id = rt_hw_cpu_id();
  80. GIC_RDISTSGI_ISENABLER0(_gic_table[index].redist_hw_base[cpu_id]) = mask;
  81. }
  82. else
  83. {
  84. GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, irq) = mask;
  85. }
  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 >= 0);
  93. if (irq >= 16)
  94. {
  95. pend = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1;
  96. }
  97. else
  98. {
  99. /* INTID 0-15 Software Generated Interrupt */
  100. pend = (GIC_DIST_SPENDSGI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4) * 8)) & 0xff;
  101. /* No CPU identification offered */
  102. if (pend != 0)
  103. {
  104. pend = 1;
  105. }
  106. else
  107. {
  108. pend = 0;
  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 >= 0);
  118. if (irq >= 16)
  119. {
  120. GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) = 1 << (irq % 32);
  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 | 0x02000000);
  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 >= 0);
  135. if (irq >= 16)
  136. {
  137. mask = 1 << (irq % 32);
  138. GIC_DIST_PENDING_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  139. }
  140. else
  141. {
  142. mask = 1 << ((irq % 4) * 8);
  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, rt_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 >= 0);
  153. icfgr = GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq);
  154. shift = (irq % 16) << 1;
  155. icfgr &= (~(3 << shift));
  156. icfgr |= (config << (shift + 1));
  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 >= 0);
  164. return (GIC_DIST_CONFIG(_gic_table[index].dist_hw_base, irq) >> ((irq % 16) >> 1));
  165. }
  166. void arm_gic_clear_active(rt_uint64_t index, int irq)
  167. {
  168. rt_uint64_t mask = 1 << (irq % 32);
  169. RT_ASSERT(index < ARM_GIC_MAX_NR);
  170. irq = irq - _gic_table[index].offset;
  171. RT_ASSERT(irq >= 0);
  172. GIC_DIST_ACTIVE_CLEAR(_gic_table[index].dist_hw_base, irq) = mask;
  173. }
  174. void arm_gic_set_router_cpu(rt_uint64_t index, int irq, rt_uint64_t aff)
  175. {
  176. RT_ASSERT(index < ARM_GIC_MAX_NR);
  177. irq = irq - _gic_table[index].offset;
  178. RT_ASSERT(irq >= 32);
  179. GIC_DIST_IROUTER(_gic_table[index].dist_hw_base, irq) = aff & 0xff00ffffffULL;
  180. }
  181. rt_uint64_t arm_gic_get_router_cpu(rt_uint64_t index, int irq)
  182. {
  183. RT_ASSERT(index < ARM_GIC_MAX_NR);
  184. irq = irq - _gic_table[index].offset;
  185. RT_ASSERT(irq >= 32);
  186. return GIC_DIST_IROUTER(_gic_table[index].dist_hw_base, irq);
  187. }
  188. /* Set up the cpu mask for the specific interrupt */
  189. void arm_gic_set_cpu(rt_uint64_t index, int irq, unsigned int cpumask)
  190. {
  191. rt_uint64_t old_tgt;
  192. RT_ASSERT(index < ARM_GIC_MAX_NR);
  193. irq = irq - _gic_table[index].offset;
  194. RT_ASSERT(irq >= 0);
  195. old_tgt = GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq);
  196. old_tgt &= ~(0x0ff << ((irq % 4) * 8));
  197. old_tgt |= cpumask << ((irq % 4) * 8);
  198. GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) = old_tgt;
  199. }
  200. rt_uint64_t arm_gic_get_target_cpu(rt_uint64_t index, int irq)
  201. {
  202. RT_ASSERT(index < ARM_GIC_MAX_NR);
  203. irq = irq - _gic_table[index].offset;
  204. RT_ASSERT(irq >= 0);
  205. return (GIC_DIST_TARGET(_gic_table[index].dist_hw_base, irq) >> ((irq % 4) * 8)) & 0xff;
  206. }
  207. void arm_gic_set_priority(rt_uint64_t index, int irq, rt_uint64_t priority)
  208. {
  209. rt_uint64_t mask;
  210. RT_ASSERT(index < ARM_GIC_MAX_NR);
  211. irq = irq - _gic_table[index].offset;
  212. RT_ASSERT(irq >= 0);
  213. if (irq < 32)
  214. {
  215. rt_int32_t cpu_id = rt_hw_cpu_id();
  216. mask = GIC_RDISTSGI_IPRIORITYR(_gic_table[index].redist_hw_base[cpu_id], irq);
  217. mask &= ~(0xffUL << ((irq % 4) * 8));
  218. mask |= ((priority & 0xff) << ((irq % 4) * 8));
  219. GIC_RDISTSGI_IPRIORITYR(_gic_table[index].redist_hw_base[cpu_id], irq) = mask;
  220. }
  221. else
  222. {
  223. mask = GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq);
  224. mask &= ~(0xff << ((irq % 4) * 8));
  225. mask |= ((priority & 0xff) << ((irq % 4) * 8));
  226. GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) = mask;
  227. }
  228. }
  229. rt_uint64_t arm_gic_get_priority(rt_uint64_t index, int irq)
  230. {
  231. RT_ASSERT(index < ARM_GIC_MAX_NR);
  232. irq = irq - _gic_table[index].offset;
  233. RT_ASSERT(irq >= 0);
  234. if (irq < 32)
  235. {
  236. rt_int32_t cpu_id = rt_hw_cpu_id();
  237. return (GIC_RDISTSGI_IPRIORITYR(_gic_table[index].redist_hw_base[cpu_id], irq) >> ((irq % 4) * 8)) & 0xff;
  238. }
  239. else
  240. {
  241. return (GIC_DIST_PRI(_gic_table[index].dist_hw_base, irq) >> ((irq % 4) * 8)) & 0xff;
  242. }
  243. }
  244. void arm_gic_set_system_register_enable_mask(rt_uint64_t index, rt_uint64_t value)
  245. {
  246. RT_ASSERT(index < ARM_GIC_MAX_NR);
  247. value &= 0xff;
  248. /* set priority mask */
  249. SET_GICV3_REG(ICC_SRE_EL1, value);
  250. __ISB();
  251. }
  252. rt_uint64_t arm_gic_get_system_register_enable_mask(rt_uint64_t index)
  253. {
  254. RT_ASSERT(index < ARM_GIC_MAX_NR);
  255. rt_uint64_t value;
  256. GET_GICV3_REG(ICC_SRE_EL1, value);
  257. return value;
  258. }
  259. void arm_gic_set_interface_prior_mask(rt_uint64_t index, rt_uint64_t priority)
  260. {
  261. RT_ASSERT(index < ARM_GIC_MAX_NR);
  262. priority &= 0xff;
  263. /* set priority mask */
  264. SET_GICV3_REG(ICC_PMR_EL1, priority);
  265. }
  266. rt_uint64_t arm_gic_get_interface_prior_mask(rt_uint64_t index)
  267. {
  268. RT_ASSERT(index < ARM_GIC_MAX_NR);
  269. rt_uint64_t priority;
  270. GET_GICV3_REG(ICC_PMR_EL1, priority);
  271. return priority;
  272. }
  273. void arm_gic_set_binary_point(rt_uint64_t index, rt_uint64_t binary_point)
  274. {
  275. RT_UNUSED(index);
  276. binary_point &= 0x7;
  277. SET_GICV3_REG(ICC_BPR1_EL1, binary_point);
  278. }
  279. rt_uint64_t arm_gic_get_binary_point(rt_uint64_t index)
  280. {
  281. rt_uint64_t binary_point;
  282. RT_UNUSED(index);
  283. GET_GICV3_REG(ICC_BPR1_EL1, binary_point);
  284. return binary_point;
  285. }
  286. rt_uint64_t arm_gic_get_irq_status(rt_uint64_t index, int irq)
  287. {
  288. rt_uint64_t pending, active;
  289. RT_ASSERT(index < ARM_GIC_MAX_NR);
  290. irq = irq - _gic_table[index].offset;
  291. RT_ASSERT(irq >= 0);
  292. active = (GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1;
  293. pending = (GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1;
  294. return ((active << 1) | pending);
  295. }
  296. #if defined(RT_USING_SMP) || defined(RT_USING_AMP)
  297. struct gicv3_sgi_aff
  298. {
  299. rt_uint64_t aff;
  300. rt_uint32_t cpu_mask[(RT_CPUS_NR + 31) >> 5];
  301. rt_uint16_t target_list;
  302. };
  303. static struct gicv3_sgi_aff sgi_aff_table[RT_CPUS_NR];
  304. static rt_uint64_t sgi_aff_table_num;
  305. static void sgi_aff_add_table(rt_uint64_t aff, rt_uint64_t cpu_index)
  306. {
  307. rt_uint64_t i;
  308. for (i = 0; i < sgi_aff_table_num; i++)
  309. {
  310. if (sgi_aff_table[i].aff == aff)
  311. {
  312. sgi_aff_table[i].cpu_mask[cpu_index >> 5] |= (1 << (cpu_index & 0x1F));
  313. return;
  314. }
  315. }
  316. sgi_aff_table[sgi_aff_table_num].aff = aff;
  317. sgi_aff_table[sgi_aff_table_num].cpu_mask[cpu_index >> 5] |= (1 << (cpu_index & 0x1F));
  318. sgi_aff_table_num++;
  319. }
  320. static rt_uint64_t gicv3_sgi_init(void)
  321. {
  322. rt_uint64_t i, icc_sgi1r_value;
  323. for (i = 0; i < RT_CPUS_NR; i++)
  324. {
  325. icc_sgi1r_value = (rt_uint64_t)((rt_cpu_mpidr_table[i] >> 8) & 0xFF) << 16;
  326. icc_sgi1r_value |= (rt_uint64_t)((rt_cpu_mpidr_table[i] >> 16) & 0xFF) << 32;
  327. icc_sgi1r_value |= (rt_uint64_t)((rt_cpu_mpidr_table[i] >> 32) & 0xFF) << 48;
  328. icc_sgi1r_value |= (rt_uint64_t)((rt_cpu_mpidr_table[i] >> 4) & 0xF) << 44;
  329. sgi_aff_add_table(icc_sgi1r_value, i);
  330. }
  331. return (RT_CPUS_NR + 31) >> 5;
  332. }
  333. rt_inline void gicv3_sgi_send(rt_uint64_t int_id)
  334. {
  335. rt_uint64_t i;
  336. for (i = 0; i < sgi_aff_table_num; i++)
  337. {
  338. if (sgi_aff_table[i].target_list)
  339. {
  340. __DSB();
  341. /* Interrupts routed to the PEs specified by Aff3.Aff2.Aff1.<target list>. */
  342. SET_GICV3_REG(ICC_SGI1R_EL1, sgi_aff_table[i].aff | int_id | sgi_aff_table[i].target_list);
  343. __ISB();
  344. sgi_aff_table[i].target_list = 0;
  345. }
  346. }
  347. }
  348. rt_inline void gicv3_sgi_target_list_set(rt_uint64_t array, rt_uint32_t cpu_mask)
  349. {
  350. rt_uint64_t i, value;
  351. for (i = 0; i < sgi_aff_table_num; i++)
  352. {
  353. if (sgi_aff_table[i].cpu_mask[array] & cpu_mask)
  354. {
  355. while (cpu_mask)
  356. {
  357. value = __builtin_ctzl(cpu_mask);
  358. cpu_mask &= ~(1 << value);
  359. sgi_aff_table[i].target_list |= 1 << (rt_cpu_mpidr_table[(array << 5) | value] & 0xF);
  360. }
  361. }
  362. }
  363. }
  364. void arm_gic_send_affinity_sgi(rt_uint64_t index, int irq, rt_uint32_t cpu_masks[], rt_uint64_t routing_mode)
  365. {
  366. rt_uint64_t i;
  367. rt_uint64_t int_id = (irq & 0xf) << 24;
  368. static rt_uint64_t masks_nrs = 0;
  369. if (routing_mode == GICV3_ROUTED_TO_SPEC)
  370. {
  371. if (!masks_nrs)
  372. {
  373. masks_nrs = gicv3_sgi_init();
  374. }
  375. for (i = 0; i < masks_nrs; i++)
  376. {
  377. if (cpu_masks[i] == 0)
  378. {
  379. continue;
  380. }
  381. gicv3_sgi_target_list_set(i, cpu_masks[i]);
  382. }
  383. gicv3_sgi_send(int_id);
  384. }
  385. else
  386. {
  387. __DSB();
  388. /* Interrupts routed to all PEs in the system, excluding "self". */
  389. SET_GICV3_REG(ICC_SGI1R_EL1, (0x10000000000ULL) | int_id);
  390. __ISB();
  391. }
  392. }
  393. #endif /* defined(RT_USING_SMP) || defined(RT_USING_AMP) */
  394. rt_uint64_t arm_gic_get_high_pending_irq(rt_uint64_t index)
  395. {
  396. rt_uint64_t irq;
  397. RT_ASSERT(index < ARM_GIC_MAX_NR);
  398. RT_UNUSED(index);
  399. GET_GICV3_REG(ICC_HPPIR1_EL1, irq);
  400. return irq;
  401. }
  402. rt_uint64_t arm_gic_get_interface_id(rt_uint64_t index)
  403. {
  404. rt_uint64_t ret = 0;
  405. rt_base_t level;
  406. int cpuid;
  407. RT_ASSERT(index < ARM_GIC_MAX_NR);
  408. level = rt_hw_local_irq_disable();
  409. cpuid = rt_hw_cpu_id();
  410. if (_gic_table[index].cpu_hw_base[cpuid] != RT_NULL)
  411. {
  412. ret = GIC_CPU_IIDR(_gic_table[index].cpu_hw_base[cpuid]);
  413. }
  414. rt_hw_local_irq_enable(level);
  415. return ret;
  416. }
  417. void arm_gic_set_group(rt_uint64_t index, int irq, rt_uint64_t group)
  418. {
  419. rt_uint64_t igroupr;
  420. rt_uint64_t shift;
  421. RT_ASSERT(index < ARM_GIC_MAX_NR);
  422. RT_ASSERT(group <= 1);
  423. irq = irq - _gic_table[index].offset;
  424. RT_ASSERT(irq >= 0);
  425. igroupr = GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq);
  426. shift = (irq % 32);
  427. igroupr &= (~(1U << shift));
  428. igroupr |= ((group & 0x1U) << shift);
  429. GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) = igroupr;
  430. }
  431. rt_uint64_t arm_gic_get_group(rt_uint64_t index, int irq)
  432. {
  433. RT_ASSERT(index < ARM_GIC_MAX_NR);
  434. irq = irq - _gic_table[index].offset;
  435. RT_ASSERT(irq >= 0);
  436. return (GIC_DIST_IGROUP(_gic_table[index].dist_hw_base, irq) >> (irq % 32)) & 0x1UL;
  437. }
  438. static int arm_gicv3_wait_rwp(rt_uint64_t index, rt_uint64_t irq)
  439. {
  440. rt_uint64_t rwp_bit;
  441. rt_uint64_t base;
  442. RT_ASSERT(index < ARM_GIC_MAX_NR);
  443. if (irq < 32)
  444. {
  445. rt_int32_t cpu_id = rt_hw_cpu_id();
  446. base = _gic_table[index].redist_hw_base[cpu_id];
  447. rwp_bit = GICR_CTLR_RWP;
  448. }
  449. else
  450. {
  451. base = _gic_table[index].dist_hw_base;
  452. rwp_bit = GICD_CTLR_RWP;
  453. }
  454. while (HWREG32(base) & rwp_bit)
  455. {
  456. }
  457. return 0;
  458. }
  459. int arm_gic_dist_init(rt_uint64_t index, rt_uint64_t dist_base, int irq_start)
  460. {
  461. int i;
  462. unsigned int gic_type;
  463. rt_uint64_t main_cpu_affinity_val;
  464. RT_UNUSED(i);
  465. RT_UNUSED(main_cpu_affinity_val);
  466. RT_ASSERT(index < ARM_GIC_MAX_NR);
  467. _gic_table[index].dist_hw_base = dist_base;
  468. _gic_table[index].offset = irq_start;
  469. /* Find out how many interrupts are supported. */
  470. gic_type = GIC_DIST_TYPE(dist_base);
  471. _gic_max_irq = ((gic_type & 0x1f) + 1) * 32;
  472. /*
  473. * The GIC only supports up to 1020 interrupt sources.
  474. * Limit this to either the architected maximum, or the
  475. * platform maximum.
  476. */
  477. if (_gic_max_irq > 1020)
  478. {
  479. _gic_max_irq = 1020;
  480. }
  481. if (_gic_max_irq > ARM_GIC_NR_IRQS) /* the platform maximum interrupts */
  482. {
  483. _gic_max_irq = ARM_GIC_NR_IRQS;
  484. }
  485. #ifndef RT_AMP_SLAVE
  486. GIC_DIST_CTRL(dist_base) = 0;
  487. /* Wait for register write pending */
  488. arm_gicv3_wait_rwp(0, 32);
  489. /* Set all global interrupts to be level triggered, active low. */
  490. for (i = 32; i < _gic_max_irq; i += 16)
  491. {
  492. GIC_DIST_CONFIG(dist_base, i) = 0;
  493. }
  494. arm_gicv3_wait_rwp(0, 32);
  495. #ifdef RT_USING_SMP
  496. main_cpu_affinity_val = rt_cpu_mpidr_table[ARM_SPI_BIND_CPU_ID];
  497. #else
  498. __asm__ volatile ("mrs %0, mpidr_el1":"=r"(main_cpu_affinity_val));
  499. #endif
  500. /* aff3[39:32], aff2[23:16], aff1[15:8], aff0[7:0] */
  501. main_cpu_affinity_val &= 0xff00ffffffULL;
  502. /* Set all global interrupts to this CPU only. */
  503. for (i = 32; i < _gic_max_irq; i++)
  504. {
  505. GIC_DIST_IROUTER(dist_base, i) = main_cpu_affinity_val | (GICV3_ROUTED_TO_SPEC << 31);
  506. }
  507. arm_gicv3_wait_rwp(0, 32);
  508. /* Set priority on spi interrupts. */
  509. for (i = 32; i < _gic_max_irq; i += 4)
  510. {
  511. GIC_DIST_PRI(dist_base, i) = 0xa0a0a0a0;
  512. }
  513. arm_gicv3_wait_rwp(0, 32);
  514. /* Disable all interrupts. */
  515. for (i = 0; i < _gic_max_irq; i += 32)
  516. {
  517. GIC_DIST_PENDING_CLEAR(dist_base, i) = 0xffffffff;
  518. GIC_DIST_ENABLE_CLEAR(dist_base, i) = 0xffffffff;
  519. }
  520. arm_gicv3_wait_rwp(0, 32);
  521. /* All interrupts defaults to IGROUP1(IRQ). */
  522. for (i = 0; i < _gic_max_irq; i += 32)
  523. {
  524. GIC_DIST_IGROUP(dist_base, i) = 0xffffffff;
  525. }
  526. arm_gicv3_wait_rwp(0, 32);
  527. /*
  528. * The Distributor control register (GICD_CTLR) must be configured to enable the interrupt groups and to set the routing mode.
  529. * Enable Affinity routing (ARE bits) The ARE bits in GICD_CTLR control whether affinity routing is enabled.
  530. * If affinity routing is not enabled, GICv3 can be configured for legacy operation.
  531. * Whether affinity routing is enabled or not can be controlled separately for Secure and Non-secure state.
  532. * Enables GICD_CTLR contains separate enable bits for Group 0, Secure Group 1 and Non-secure Group 1:
  533. * GICD_CTLR.EnableGrp1S enables distribution of Secure Group 1 interrupts.
  534. * GICD_CTLR.EnableGrp1NS enables distribution of Non-secure Group 1 interrupts.
  535. * GICD_CTLR.EnableGrp0 enables distribution of Group 0 interrupts.
  536. */
  537. GIC_DIST_CTRL(dist_base) = GICD_CTLR_ARE_NS | GICD_CTLR_ENGRP1NS;
  538. #endif /* RT_AMP_SLAVE */
  539. return 0;
  540. }
  541. int arm_gic_redist_init(rt_uint64_t index, rt_uint64_t redist_base)
  542. {
  543. int i;
  544. int cpu_id = rt_hw_cpu_id();
  545. static int master_cpu_id = -1;
  546. RT_ASSERT(index < ARM_GIC_MAX_NR);
  547. if (master_cpu_id < 0)
  548. {
  549. master_cpu_id = 0;
  550. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, &master_cpu_id, sizeof(master_cpu_id));
  551. }
  552. if (!_gic_table[index].redist_hw_base[master_cpu_id])
  553. {
  554. _gic_table[index].redist_hw_base[master_cpu_id] = redist_base;
  555. }
  556. redist_base = _gic_table[index].redist_hw_base[master_cpu_id];
  557. redist_base += cpu_id * (2 << 16);
  558. _gic_table[index].redist_hw_base[cpu_id] = redist_base;
  559. /* redistributor enable */
  560. GIC_RDIST_WAKER(redist_base) &= ~(1 << 1);
  561. while (GIC_RDIST_WAKER(redist_base) & (1 << 2))
  562. {
  563. }
  564. /* Disable all sgi and ppi interrupt */
  565. GIC_RDISTSGI_ICENABLER0(redist_base) = 0xffffffff;
  566. arm_gicv3_wait_rwp(0, 0);
  567. /* Clear all inetrrupt pending */
  568. GIC_RDISTSGI_ICPENDR0(redist_base) = 0xffffffff;
  569. /* the corresponding interrupt is Group 1 or Non-secure Group 1. */
  570. GIC_RDISTSGI_IGROUPR0(redist_base, 0) = 0xffffffff;
  571. GIC_RDISTSGI_IGRPMODR0(redist_base, 0) = 0xffffffff;
  572. /* Configure default priorities for SGI 0:15 and PPI 16:31. */
  573. for (i = 0; i < 32; i += 4)
  574. {
  575. GIC_RDISTSGI_IPRIORITYR(redist_base, i) = 0xa0a0a0a0U;
  576. }
  577. /* Trigger level for PPI interrupts*/
  578. GIC_RDISTSGI_ICFGR1(redist_base) = 0;
  579. return 0;
  580. }
  581. int arm_gic_cpu_init(rt_uint64_t index, rt_uint64_t cpu_base)
  582. {
  583. rt_uint64_t value;
  584. int cpu_id = rt_hw_cpu_id();
  585. RT_ASSERT(index < ARM_GIC_MAX_NR);
  586. _gic_table[index].cpu_hw_base[cpu_id] = cpu_base;
  587. value = arm_gic_get_system_register_enable_mask(index);
  588. value |= (1 << 0);
  589. arm_gic_set_system_register_enable_mask(index, value);
  590. SET_GICV3_REG(ICC_CTLR_EL1, 0l);
  591. arm_gic_set_interface_prior_mask(index, 0xff);
  592. /* Enable group1 interrupt */
  593. value = 1;
  594. SET_GICV3_REG(ICC_IGRPEN1_EL1, value);
  595. arm_gic_set_binary_point(0, 0);
  596. /* ICC_BPR0_EL1 determines the preemption group for both Group 0 and Group 1 interrupts. */
  597. value = 1; /* ICC_BPR0_EL1 determines the preemption group for both Group 0 and Group 1 interrupts.*/
  598. value |= 1 << 18; /* Targeted SGIs with affinity level 0 values of 0 - 255 are supported. */
  599. SET_GICV3_REG(ICC_CTLR_EL1, value);
  600. return 0;
  601. }
  602. void arm_gic_dump_type(rt_uint64_t index)
  603. {
  604. unsigned int gic_type;
  605. unsigned int gic_version;
  606. unsigned int gic_rp;
  607. gic_version = (GIC_DIST_IIDR(_gic_table[index].dist_hw_base) >> 24) & 0xfUL;
  608. gic_rp = (GIC_DIST_IIDR(_gic_table[index].dist_hw_base) >> 12) & 0xfUL;
  609. gic_type = GIC_DIST_TYPE(_gic_table[index].dist_hw_base);
  610. rt_kprintf("GICv3-%d r%dp%d on %p, max IRQs: %d, %s security extension(%08x)\n",
  611. (gic_version == 0) ? 500 : (gic_version == 2) ? 600 : 0,
  612. (gic_rp >> 4) & 0xF,
  613. gic_rp & 0xF,
  614. _gic_table[index].dist_hw_base,
  615. _gic_max_irq,
  616. gic_type & (1U << 10U) ? "has" : "no",
  617. gic_type);
  618. }
  619. void arm_gic_dump(rt_uint64_t index)
  620. {
  621. int i;
  622. unsigned int val;
  623. val = arm_gic_get_high_pending_irq(0);
  624. rt_kprintf("--- high pending priority: %d(%08x)\n", val, val);
  625. rt_kprintf("--- hw mask ---\n");
  626. for (i = 0; i < _gic_max_irq / 32; ++i)
  627. {
  628. rt_kprintf("0x%08x, ", GIC_DIST_ENABLE_SET(_gic_table[index].dist_hw_base, i * 32));
  629. }
  630. rt_kprintf("\b\b\n--- hw pending ---\n");
  631. for (i = 0; i < _gic_max_irq / 32; ++i)
  632. {
  633. rt_kprintf("0x%08x, ", GIC_DIST_PENDING_SET(_gic_table[index].dist_hw_base, i * 32));
  634. }
  635. rt_kprintf("\b\b\n--- hw active ---\n");
  636. for (i = 0; i < _gic_max_irq / 32; ++i)
  637. {
  638. rt_kprintf("0x%08x, ", GIC_DIST_ACTIVE_SET(_gic_table[index].dist_hw_base, i * 32));
  639. }
  640. rt_kprintf("\b\b\n");
  641. }
  642. static void arm_gic_bind_dump(void)
  643. {
  644. #ifdef BSP_USING_GICV3
  645. int i;
  646. for (i = 32; i < _gic_max_irq; i++)
  647. {
  648. rt_kprintf("irq(%d) -> 0x%X\n", i, arm_gic_get_router_cpu(0, i));
  649. }
  650. #endif /* BSP_USING_GICV3 */
  651. }
  652. rt_uint64_t *arm_gic_get_gic_table_addr(void)
  653. {
  654. return (rt_uint64_t *)&_gic_table[0];
  655. }
  656. static void arm_gic_sgi_dump(rt_uint64_t index)
  657. {
  658. rt_int32_t cpu_id = rt_hw_cpu_id();
  659. rt_kprintf("redist_hw_base = 0x%X\n", _gic_table[index].redist_hw_base[cpu_id]);
  660. rt_kprintf("--- sgi mask ---\n");
  661. rt_kprintf("0x%08x\n", GIC_RDISTSGI_ISENABLER0(_gic_table[index].redist_hw_base[cpu_id]));
  662. rt_kprintf("--- sgi pending ---\n");
  663. rt_kprintf("0x%08x\n", GIC_RDISTSGI_ISPENDR0(_gic_table[index].redist_hw_base[cpu_id]));
  664. rt_kprintf("--- sgi active ---\n");
  665. rt_kprintf("0x%08x\n", GIC_RDISTSGI_ISACTIVER0(_gic_table[index].redist_hw_base[cpu_id]));
  666. }
  667. long gic_dump(void)
  668. {
  669. arm_gic_dump_type(0);
  670. arm_gic_dump(0);
  671. arm_gic_bind_dump();
  672. arm_gic_sgi_dump(0);
  673. return 0;
  674. }
  675. MSH_CMD_EXPORT(gic_dump, show gic status);
  676. #endif /* defined(BSP_USING_GIC) && defined(BSP_USING_GICV3) */