gicv2_main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
  3. * Portions copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <stdbool.h>
  9. #include <arch.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.h>
  12. #include <common/interrupt_props.h>
  13. #include <drivers/arm/gic_common.h>
  14. #include <drivers/arm/gicv2.h>
  15. #include <lib/spinlock.h>
  16. #include "../common/gic_common_private.h"
  17. #include "gicv2_private.h"
  18. static const gicv2_driver_data_t *driver_data;
  19. /*
  20. * Spinlock to guard registers needing read-modify-write. APIs protected by this
  21. * spinlock are used either at boot time (when only a single CPU is active), or
  22. * when the system is fully coherent.
  23. */
  24. static spinlock_t gic_lock;
  25. /*******************************************************************************
  26. * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
  27. * and set the priority mask register to allow all interrupts to trickle in.
  28. ******************************************************************************/
  29. void gicv2_cpuif_enable(void)
  30. {
  31. unsigned int val;
  32. assert(driver_data != NULL);
  33. assert(driver_data->gicc_base != 0U);
  34. /*
  35. * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
  36. * bypass.
  37. */
  38. val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
  39. val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
  40. /* Program the idle priority in the PMR */
  41. gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
  42. gicc_write_ctlr(driver_data->gicc_base, val);
  43. }
  44. /*******************************************************************************
  45. * Place the cpu interface in a state where it can never make a cpu exit wfi as
  46. * as result of an asserted interrupt. This is critical for powering down a cpu
  47. ******************************************************************************/
  48. void gicv2_cpuif_disable(void)
  49. {
  50. unsigned int val;
  51. assert(driver_data != NULL);
  52. assert(driver_data->gicc_base != 0U);
  53. /* Disable secure, non-secure interrupts and disable their bypass */
  54. val = gicc_read_ctlr(driver_data->gicc_base);
  55. val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
  56. val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
  57. val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
  58. gicc_write_ctlr(driver_data->gicc_base, val);
  59. }
  60. /*******************************************************************************
  61. * Per cpu gic distributor setup which will be done by all cpus after a cold
  62. * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
  63. ******************************************************************************/
  64. void gicv2_pcpu_distif_init(void)
  65. {
  66. unsigned int ctlr;
  67. assert(driver_data != NULL);
  68. assert(driver_data->gicd_base != 0U);
  69. gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base,
  70. driver_data->interrupt_props,
  71. driver_data->interrupt_props_num);
  72. /* Enable G0 interrupts if not already */
  73. ctlr = gicd_read_ctlr(driver_data->gicd_base);
  74. if ((ctlr & CTLR_ENABLE_G0_BIT) == 0U) {
  75. gicd_write_ctlr(driver_data->gicd_base,
  76. ctlr | CTLR_ENABLE_G0_BIT);
  77. }
  78. }
  79. /*******************************************************************************
  80. * Global gic distributor init which will be done by the primary cpu after a
  81. * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
  82. * then enables the secure GIC distributor interface.
  83. ******************************************************************************/
  84. void gicv2_distif_init(void)
  85. {
  86. unsigned int ctlr;
  87. assert(driver_data != NULL);
  88. assert(driver_data->gicd_base != 0U);
  89. /* Disable the distributor before going further */
  90. ctlr = gicd_read_ctlr(driver_data->gicd_base);
  91. gicd_write_ctlr(driver_data->gicd_base,
  92. ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
  93. /* Set the default attribute of all SPIs */
  94. gicv2_spis_configure_defaults(driver_data->gicd_base);
  95. gicv2_secure_spis_configure_props(driver_data->gicd_base,
  96. driver_data->interrupt_props,
  97. driver_data->interrupt_props_num);
  98. /* Re-enable the secure SPIs now that they have been configured */
  99. gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
  100. }
  101. /*******************************************************************************
  102. * Initialize the ARM GICv2 driver with the provided platform inputs
  103. ******************************************************************************/
  104. void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
  105. {
  106. unsigned int gic_version;
  107. assert(plat_driver_data != NULL);
  108. assert(plat_driver_data->gicd_base != 0U);
  109. assert(plat_driver_data->gicc_base != 0U);
  110. assert(plat_driver_data->interrupt_props_num > 0 ?
  111. plat_driver_data->interrupt_props != NULL : 1);
  112. /* Ensure that this is a GICv2 system */
  113. gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
  114. gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
  115. & PIDR2_ARCH_REV_MASK;
  116. /*
  117. * GICv1 with security extension complies with trusted firmware
  118. * GICv2 driver as far as virtualization and few tricky power
  119. * features are not used. GICv2 features that are not supported
  120. * by GICv1 with Security Extensions are:
  121. * - virtual interrupt support.
  122. * - wake up events.
  123. * - writeable GIC state register (for power sequences)
  124. * - interrupt priority drop.
  125. * - interrupt signal bypass.
  126. */
  127. assert((gic_version == ARCH_REV_GICV2) ||
  128. (gic_version == ARCH_REV_GICV1));
  129. driver_data = plat_driver_data;
  130. /*
  131. * The GIC driver data is initialized by the primary CPU with caches
  132. * enabled. When the secondary CPU boots up, it initializes the
  133. * GICC/GICR interface with the caches disabled. Hence flush the
  134. * driver_data to ensure coherency. This is not required if the
  135. * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY
  136. * enabled.
  137. */
  138. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  139. flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
  140. flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
  141. #endif
  142. INFO("ARM GICv2 driver initialized\n");
  143. }
  144. /******************************************************************************
  145. * This function returns whether FIQ is enabled in the GIC CPU interface.
  146. *****************************************************************************/
  147. unsigned int gicv2_is_fiq_enabled(void)
  148. {
  149. unsigned int gicc_ctlr;
  150. assert(driver_data != NULL);
  151. assert(driver_data->gicc_base != 0U);
  152. gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
  153. return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1U;
  154. }
  155. /*******************************************************************************
  156. * This function returns the type of the highest priority pending interrupt at
  157. * the GIC cpu interface. The return values can be one of the following :
  158. * PENDING_G1_INTID : The interrupt type is non secure Group 1.
  159. * 0 - 1019 : The interrupt type is secure Group 0.
  160. * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
  161. * sufficient priority to be signaled
  162. ******************************************************************************/
  163. unsigned int gicv2_get_pending_interrupt_type(void)
  164. {
  165. assert(driver_data != NULL);
  166. assert(driver_data->gicc_base != 0U);
  167. return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
  168. }
  169. /*******************************************************************************
  170. * This function returns the id of the highest priority pending interrupt at
  171. * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
  172. * interrupt pending.
  173. ******************************************************************************/
  174. unsigned int gicv2_get_pending_interrupt_id(void)
  175. {
  176. unsigned int id;
  177. assert(driver_data != NULL);
  178. assert(driver_data->gicc_base != 0U);
  179. id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
  180. /*
  181. * Find out which non-secure interrupt it is under the assumption that
  182. * the GICC_CTLR.AckCtl bit is 0.
  183. */
  184. if (id == PENDING_G1_INTID)
  185. id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
  186. return id;
  187. }
  188. /*******************************************************************************
  189. * This functions reads the GIC cpu interface Interrupt Acknowledge register
  190. * to start handling the pending secure 0 interrupt. It returns the
  191. * contents of the IAR.
  192. ******************************************************************************/
  193. unsigned int gicv2_acknowledge_interrupt(void)
  194. {
  195. assert(driver_data != NULL);
  196. assert(driver_data->gicc_base != 0U);
  197. return gicc_read_IAR(driver_data->gicc_base);
  198. }
  199. /*******************************************************************************
  200. * This functions writes the GIC cpu interface End Of Interrupt register with
  201. * the passed value to finish handling the active secure group 0 interrupt.
  202. ******************************************************************************/
  203. void gicv2_end_of_interrupt(unsigned int id)
  204. {
  205. assert(driver_data != NULL);
  206. assert(driver_data->gicc_base != 0U);
  207. /*
  208. * Ensure the write to peripheral registers are *complete* before the write
  209. * to GIC_EOIR.
  210. *
  211. * Note: The completion guarantee depends on various factors of system design
  212. * and the barrier is the best core can do by which execution of further
  213. * instructions waits till the barrier is alive.
  214. */
  215. dsbishst();
  216. gicc_write_EOIR(driver_data->gicc_base, id);
  217. }
  218. /*******************************************************************************
  219. * This function returns the type of the interrupt id depending upon the group
  220. * this interrupt has been configured under by the interrupt controller i.e.
  221. * group0 secure or group1 non secure. It returns zero for Group 0 secure and
  222. * one for Group 1 non secure interrupt.
  223. ******************************************************************************/
  224. unsigned int gicv2_get_interrupt_group(unsigned int id)
  225. {
  226. assert(driver_data != NULL);
  227. assert(driver_data->gicd_base != 0U);
  228. return gicd_get_igroupr(driver_data->gicd_base, id);
  229. }
  230. /*******************************************************************************
  231. * This function returns the priority of the interrupt the processor is
  232. * currently servicing.
  233. ******************************************************************************/
  234. unsigned int gicv2_get_running_priority(void)
  235. {
  236. assert(driver_data != NULL);
  237. assert(driver_data->gicc_base != 0U);
  238. return gicc_read_rpr(driver_data->gicc_base);
  239. }
  240. /*******************************************************************************
  241. * This function sets the GICv2 target mask pattern for the current PE. The PE
  242. * target mask is used to translate linear PE index (returned by platform core
  243. * position) to a bit mask used when targeting interrupts to a PE (for example
  244. * when raising SGIs and routing SPIs).
  245. ******************************************************************************/
  246. void gicv2_set_pe_target_mask(unsigned int proc_num)
  247. {
  248. assert(driver_data != NULL);
  249. assert(driver_data->gicd_base != 0U);
  250. assert(driver_data->target_masks != NULL);
  251. assert(proc_num < GICV2_MAX_TARGET_PE);
  252. assert(proc_num < driver_data->target_masks_num);
  253. /* Return if the target mask is already populated */
  254. if (driver_data->target_masks[proc_num] != 0U)
  255. return;
  256. /*
  257. * Update target register corresponding to this CPU and flush for it to
  258. * be visible to other CPUs.
  259. */
  260. if (driver_data->target_masks[proc_num] == 0U) {
  261. driver_data->target_masks[proc_num] =
  262. gicv2_get_cpuif_id(driver_data->gicd_base);
  263. #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
  264. /*
  265. * PEs only update their own masks. Primary updates it with
  266. * caches on. But because secondaries does it with caches off,
  267. * all updates go to memory directly, and there's no danger of
  268. * secondaries overwriting each others' mask, despite
  269. * target_masks[] not being cache line aligned.
  270. */
  271. flush_dcache_range((uintptr_t)
  272. &driver_data->target_masks[proc_num],
  273. sizeof(driver_data->target_masks[proc_num]));
  274. #endif
  275. }
  276. }
  277. /*******************************************************************************
  278. * This function returns the active status of the interrupt (either because the
  279. * state is active, or active and pending).
  280. ******************************************************************************/
  281. unsigned int gicv2_get_interrupt_active(unsigned int id)
  282. {
  283. assert(driver_data != NULL);
  284. assert(driver_data->gicd_base != 0U);
  285. assert(id <= MAX_SPI_ID);
  286. return gicd_get_isactiver(driver_data->gicd_base, id);
  287. }
  288. /*******************************************************************************
  289. * This function enables the interrupt identified by id.
  290. ******************************************************************************/
  291. void gicv2_enable_interrupt(unsigned int id)
  292. {
  293. assert(driver_data != NULL);
  294. assert(driver_data->gicd_base != 0U);
  295. assert(id <= MAX_SPI_ID);
  296. /*
  297. * Ensure that any shared variable updates depending on out of band
  298. * interrupt trigger are observed before enabling interrupt.
  299. */
  300. dsbishst();
  301. gicd_set_isenabler(driver_data->gicd_base, id);
  302. }
  303. /*******************************************************************************
  304. * This function disables the interrupt identified by id.
  305. ******************************************************************************/
  306. void gicv2_disable_interrupt(unsigned int id)
  307. {
  308. assert(driver_data != NULL);
  309. assert(driver_data->gicd_base != 0U);
  310. assert(id <= MAX_SPI_ID);
  311. /*
  312. * Disable interrupt, and ensure that any shared variable updates
  313. * depending on out of band interrupt trigger are observed afterwards.
  314. */
  315. gicd_set_icenabler(driver_data->gicd_base, id);
  316. dsbishst();
  317. }
  318. /*******************************************************************************
  319. * This function sets the interrupt priority as supplied for the given interrupt
  320. * id.
  321. ******************************************************************************/
  322. void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
  323. {
  324. assert(driver_data != NULL);
  325. assert(driver_data->gicd_base != 0U);
  326. assert(id <= MAX_SPI_ID);
  327. gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
  328. }
  329. /*******************************************************************************
  330. * This function assigns group for the interrupt identified by id. The group can
  331. * be any of GICV2_INTR_GROUP*
  332. ******************************************************************************/
  333. void gicv2_set_interrupt_group(unsigned int id, unsigned int group)
  334. {
  335. assert(driver_data != NULL);
  336. assert(driver_data->gicd_base != 0U);
  337. assert(id <= MAX_SPI_ID);
  338. /* Serialize read-modify-write to Distributor registers */
  339. spin_lock(&gic_lock);
  340. switch (group) {
  341. case GICV2_INTR_GROUP1:
  342. gicd_set_igroupr(driver_data->gicd_base, id);
  343. break;
  344. case GICV2_INTR_GROUP0:
  345. gicd_clr_igroupr(driver_data->gicd_base, id);
  346. break;
  347. default:
  348. assert(false);
  349. break;
  350. }
  351. spin_unlock(&gic_lock);
  352. }
  353. /*******************************************************************************
  354. * This function raises the specified SGI to requested targets.
  355. *
  356. * The proc_num parameter must be the linear index of the target PE in the
  357. * system.
  358. ******************************************************************************/
  359. void gicv2_raise_sgi(int sgi_num, bool ns, int proc_num)
  360. {
  361. unsigned int sgir_val, target;
  362. assert(driver_data != NULL);
  363. assert(proc_num >= 0);
  364. assert(proc_num < (int)GICV2_MAX_TARGET_PE);
  365. assert(driver_data->gicd_base != 0U);
  366. /*
  367. * Target masks array must have been supplied, and the core position
  368. * should be valid.
  369. */
  370. assert(driver_data->target_masks != NULL);
  371. assert(proc_num < (int)driver_data->target_masks_num);
  372. /* Don't raise SGI if the mask hasn't been populated */
  373. target = driver_data->target_masks[proc_num];
  374. assert(target != 0U);
  375. sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, ns, sgi_num);
  376. /*
  377. * Ensure that any shared variable updates depending on out of band
  378. * interrupt trigger are observed before raising SGI.
  379. */
  380. dsbishst();
  381. gicd_write_sgir(driver_data->gicd_base, sgir_val);
  382. }
  383. /*******************************************************************************
  384. * This function sets the interrupt routing for the given SPI interrupt id.
  385. * The interrupt routing is specified in routing mode. The proc_num parameter is
  386. * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
  387. * all PEs.
  388. ******************************************************************************/
  389. void gicv2_set_spi_routing(unsigned int id, int proc_num)
  390. {
  391. unsigned int target;
  392. assert(driver_data != NULL);
  393. assert(driver_data->gicd_base != 0U);
  394. assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID));
  395. /*
  396. * Target masks array must have been supplied, and the core position
  397. * should be valid.
  398. */
  399. assert(driver_data->target_masks != NULL);
  400. assert(proc_num < (int)GICV2_MAX_TARGET_PE);
  401. assert(driver_data->target_masks_num < INT_MAX);
  402. assert(proc_num < (int)driver_data->target_masks_num);
  403. if (proc_num < 0) {
  404. /* Target all PEs */
  405. target = GIC_TARGET_CPU_MASK;
  406. } else {
  407. /* Don't route interrupt if the mask hasn't been populated */
  408. target = driver_data->target_masks[proc_num];
  409. assert(target != 0U);
  410. }
  411. gicd_set_itargetsr(driver_data->gicd_base, id, target);
  412. }
  413. /*******************************************************************************
  414. * This function clears the pending status of an interrupt identified by id.
  415. ******************************************************************************/
  416. void gicv2_clear_interrupt_pending(unsigned int id)
  417. {
  418. assert(driver_data != NULL);
  419. assert(driver_data->gicd_base != 0U);
  420. /* SGIs can't be cleared pending */
  421. assert(id >= MIN_PPI_ID);
  422. /*
  423. * Clear pending interrupt, and ensure that any shared variable updates
  424. * depending on out of band interrupt trigger are observed afterwards.
  425. */
  426. gicd_set_icpendr(driver_data->gicd_base, id);
  427. dsbishst();
  428. }
  429. /*******************************************************************************
  430. * This function sets the pending status of an interrupt identified by id.
  431. ******************************************************************************/
  432. void gicv2_set_interrupt_pending(unsigned int id)
  433. {
  434. assert(driver_data != NULL);
  435. assert(driver_data->gicd_base != 0U);
  436. /* SGIs can't be cleared pending */
  437. assert(id >= MIN_PPI_ID);
  438. /*
  439. * Ensure that any shared variable updates depending on out of band
  440. * interrupt trigger are observed before setting interrupt pending.
  441. */
  442. dsbishst();
  443. gicd_set_ispendr(driver_data->gicd_base, id);
  444. }
  445. /*******************************************************************************
  446. * This function sets the PMR register with the supplied value. Returns the
  447. * original PMR.
  448. ******************************************************************************/
  449. unsigned int gicv2_set_pmr(unsigned int mask)
  450. {
  451. unsigned int old_mask;
  452. assert(driver_data != NULL);
  453. assert(driver_data->gicc_base != 0U);
  454. old_mask = gicc_read_pmr(driver_data->gicc_base);
  455. /*
  456. * Order memory updates w.r.t. PMR write, and ensure they're visible
  457. * before potential out of band interrupt trigger because of PMR update.
  458. */
  459. dmbishst();
  460. gicc_write_pmr(driver_data->gicc_base, mask);
  461. dsbishst();
  462. return old_mask;
  463. }
  464. /*******************************************************************************
  465. * This function updates single interrupt configuration to be level/edge
  466. * triggered
  467. ******************************************************************************/
  468. void gicv2_interrupt_set_cfg(unsigned int id, unsigned int cfg)
  469. {
  470. gicd_set_icfgr(driver_data->gicd_base, id, cfg);
  471. }