gicv3_helpers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2023, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <arch.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <common/interrupt_props.h>
  12. #include <drivers/arm/gic600_multichip.h>
  13. #include <drivers/arm/gic_common.h>
  14. #include <platform_def.h>
  15. #include "../common/gic_common_private.h"
  16. #include "gicv3_private.h"
  17. uintptr_t gicv3_get_multichip_base(uint32_t spi_id, uintptr_t gicd_base)
  18. {
  19. #if GICV3_IMPL_GIC600_MULTICHIP
  20. if (gic600_multichip_is_initialized()) {
  21. return gic600_multichip_gicd_base_for_spi(spi_id);
  22. }
  23. #endif
  24. return gicd_base;
  25. }
  26. /******************************************************************************
  27. * This function marks the core as awake in the re-distributor and
  28. * ensures that the interface is active.
  29. *****************************************************************************/
  30. void gicv3_rdistif_mark_core_awake(uintptr_t gicr_base)
  31. {
  32. /*
  33. * The WAKER_PS_BIT should be changed to 0
  34. * only when WAKER_CA_BIT is 1.
  35. */
  36. assert((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U);
  37. /* Mark the connected core as awake */
  38. gicr_write_waker(gicr_base, gicr_read_waker(gicr_base) & ~WAKER_PS_BIT);
  39. /* Wait till the WAKER_CA_BIT changes to 0 */
  40. while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U) {
  41. }
  42. }
  43. /******************************************************************************
  44. * This function marks the core as asleep in the re-distributor and ensures
  45. * that the interface is quiescent.
  46. *****************************************************************************/
  47. void gicv3_rdistif_mark_core_asleep(uintptr_t gicr_base)
  48. {
  49. /* Mark the connected core as asleep */
  50. gicr_write_waker(gicr_base, gicr_read_waker(gicr_base) | WAKER_PS_BIT);
  51. /* Wait till the WAKER_CA_BIT changes to 1 */
  52. while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) == 0U) {
  53. }
  54. }
  55. /*******************************************************************************
  56. * This function probes the Redistributor frames when the driver is initialised
  57. * and saves their base addresses. These base addresses are used later to
  58. * initialise each Redistributor interface.
  59. ******************************************************************************/
  60. void gicv3_rdistif_base_addrs_probe(uintptr_t *rdistif_base_addrs,
  61. unsigned int rdistif_num,
  62. uintptr_t gicr_base,
  63. mpidr_hash_fn mpidr_to_core_pos)
  64. {
  65. u_register_t mpidr;
  66. unsigned int proc_num;
  67. uint64_t typer_val;
  68. uintptr_t rdistif_base = gicr_base;
  69. assert(rdistif_base_addrs != NULL);
  70. /*
  71. * Iterate over the Redistributor frames. Store the base address of each
  72. * frame in the platform provided array. Use the "Processor Number"
  73. * field to index into the array if the platform has not provided a hash
  74. * function to convert an MPIDR (obtained from the "Affinity Value"
  75. * field into a linear index.
  76. */
  77. do {
  78. typer_val = gicr_read_typer(rdistif_base);
  79. if (mpidr_to_core_pos != NULL) {
  80. mpidr = mpidr_from_gicr_typer(typer_val);
  81. proc_num = mpidr_to_core_pos(mpidr);
  82. } else {
  83. proc_num = (typer_val >> TYPER_PROC_NUM_SHIFT) &
  84. TYPER_PROC_NUM_MASK;
  85. }
  86. if (proc_num < rdistif_num) {
  87. rdistif_base_addrs[proc_num] = rdistif_base;
  88. }
  89. rdistif_base += gicv3_redist_size(typer_val);
  90. } while ((typer_val & TYPER_LAST_BIT) == 0U);
  91. }
  92. /*******************************************************************************
  93. * Helper function to get the maximum SPI INTID + 1.
  94. ******************************************************************************/
  95. unsigned int gicv3_get_spi_limit(uintptr_t gicd_base)
  96. {
  97. unsigned int spi_limit;
  98. unsigned int typer_reg = gicd_read_typer(gicd_base);
  99. /* (maximum SPI INTID + 1) is equal to 32 * (GICD_TYPER.ITLinesNumber+1) */
  100. spi_limit = ((typer_reg & TYPER_IT_LINES_NO_MASK) + 1U) << 5;
  101. /* Filter out special INTIDs 1020-1023 */
  102. if (spi_limit > (MAX_SPI_ID + 1U)) {
  103. return MAX_SPI_ID + 1U;
  104. }
  105. return spi_limit;
  106. }
  107. #if GIC_EXT_INTID
  108. /*******************************************************************************
  109. * Helper function to get the maximum ESPI INTID + 1.
  110. ******************************************************************************/
  111. unsigned int gicv3_get_espi_limit(uintptr_t gicd_base)
  112. {
  113. unsigned int typer_reg = gicd_read_typer(gicd_base);
  114. /* Check if extended SPI range is implemented */
  115. if ((typer_reg & TYPER_ESPI) != 0U) {
  116. /*
  117. * (maximum ESPI INTID + 1) is equal to
  118. * 32 * (GICD_TYPER.ESPI_range + 1) + 4096
  119. */
  120. return ((((typer_reg >> TYPER_ESPI_RANGE_SHIFT) &
  121. TYPER_ESPI_RANGE_MASK) + 1U) << 5) + MIN_ESPI_ID;
  122. }
  123. return 0U;
  124. }
  125. #endif /* GIC_EXT_INTID */
  126. /*******************************************************************************
  127. * Helper function to configure the default attributes of (E)SPIs.
  128. ******************************************************************************/
  129. void gicv3_spis_config_defaults(uintptr_t gicd_base)
  130. {
  131. unsigned int i, num_ints;
  132. #if GIC_EXT_INTID
  133. unsigned int num_eints;
  134. #endif
  135. num_ints = gicv3_get_spi_limit(gicd_base);
  136. INFO("Maximum SPI INTID supported: %u\n", num_ints - 1);
  137. /* Treat all (E)SPIs as G1NS by default. We do 32 at a time. */
  138. for (i = MIN_SPI_ID; i < num_ints; i += (1U << IGROUPR_SHIFT)) {
  139. gicd_write_igroupr(gicv3_get_multichip_base(i, gicd_base), i, ~0U);
  140. }
  141. #if GIC_EXT_INTID
  142. num_eints = gicv3_get_espi_limit(gicd_base);
  143. if (num_eints != 0U) {
  144. INFO("Maximum ESPI INTID supported: %u\n", num_eints - 1);
  145. for (i = MIN_ESPI_ID; i < num_eints;
  146. i += (1U << IGROUPR_SHIFT)) {
  147. gicd_write_igroupr(gicv3_get_multichip_base(i, gicd_base), i, ~0U);
  148. }
  149. } else {
  150. INFO("ESPI range is not implemented.\n");
  151. }
  152. #endif
  153. /* Setup the default (E)SPI priorities doing four at a time */
  154. for (i = MIN_SPI_ID; i < num_ints; i += (1U << IPRIORITYR_SHIFT)) {
  155. gicd_write_ipriorityr(gicv3_get_multichip_base(i, gicd_base), i, GICD_IPRIORITYR_DEF_VAL);
  156. }
  157. #if GIC_EXT_INTID
  158. for (i = MIN_ESPI_ID; i < num_eints;
  159. i += (1U << IPRIORITYR_SHIFT)) {
  160. gicd_write_ipriorityr(gicv3_get_multichip_base(i, gicd_base), i, GICD_IPRIORITYR_DEF_VAL);
  161. }
  162. #endif
  163. /*
  164. * Treat all (E)SPIs as level triggered by default, write 16 at a time
  165. */
  166. for (i = MIN_SPI_ID; i < num_ints; i += (1U << ICFGR_SHIFT)) {
  167. gicd_write_icfgr(gicv3_get_multichip_base(i, gicd_base), i, 0U);
  168. }
  169. #if GIC_EXT_INTID
  170. for (i = MIN_ESPI_ID; i < num_eints; i += (1U << ICFGR_SHIFT)) {
  171. gicd_write_icfgr(gicv3_get_multichip_base(i, gicd_base), i, 0U);
  172. }
  173. #endif
  174. }
  175. /*******************************************************************************
  176. * Helper function to configure properties of secure (E)SPIs
  177. ******************************************************************************/
  178. unsigned int gicv3_secure_spis_config_props(uintptr_t gicd_base,
  179. const interrupt_prop_t *interrupt_props,
  180. unsigned int interrupt_props_num)
  181. {
  182. unsigned int i;
  183. const interrupt_prop_t *current_prop;
  184. unsigned long long gic_affinity_val;
  185. unsigned int ctlr_enable = 0U;
  186. /* Make sure there's a valid property array */
  187. if (interrupt_props_num > 0U) {
  188. assert(interrupt_props != NULL);
  189. }
  190. for (i = 0U; i < interrupt_props_num; i++) {
  191. current_prop = &interrupt_props[i];
  192. unsigned int intr_num = current_prop->intr_num;
  193. uintptr_t multichip_gicd_base;
  194. /* Skip SGI, (E)PPI and LPI interrupts */
  195. if (!IS_SPI(intr_num)) {
  196. continue;
  197. }
  198. multichip_gicd_base =
  199. gicv3_get_multichip_base(intr_num, gicd_base);
  200. /* Configure this interrupt as a secure interrupt */
  201. gicd_clr_igroupr(multichip_gicd_base, intr_num);
  202. /* Configure this interrupt as G0 or a G1S interrupt */
  203. assert((current_prop->intr_grp == INTR_GROUP0) ||
  204. (current_prop->intr_grp == INTR_GROUP1S));
  205. if (current_prop->intr_grp == INTR_GROUP1S) {
  206. gicd_set_igrpmodr(multichip_gicd_base, intr_num);
  207. ctlr_enable |= CTLR_ENABLE_G1S_BIT;
  208. } else {
  209. gicd_clr_igrpmodr(multichip_gicd_base, intr_num);
  210. ctlr_enable |= CTLR_ENABLE_G0_BIT;
  211. }
  212. /* Set interrupt configuration */
  213. gicd_set_icfgr(multichip_gicd_base, intr_num,
  214. current_prop->intr_cfg);
  215. /* Set the priority of this interrupt */
  216. gicd_set_ipriorityr(multichip_gicd_base, intr_num,
  217. current_prop->intr_pri);
  218. /* Target (E)SPIs to the primary CPU */
  219. gic_affinity_val =
  220. gicd_irouter_val_from_mpidr(read_mpidr(), 0U);
  221. gicd_write_irouter(multichip_gicd_base, intr_num,
  222. gic_affinity_val);
  223. /* Enable this interrupt */
  224. gicd_set_isenabler(multichip_gicd_base, intr_num);
  225. }
  226. return ctlr_enable;
  227. }
  228. /*******************************************************************************
  229. * Helper function to configure the default attributes of (E)PPIs/SGIs
  230. ******************************************************************************/
  231. void gicv3_ppi_sgi_config_defaults(uintptr_t gicr_base)
  232. {
  233. unsigned int i, ppi_regs_num, regs_num;
  234. #if GIC_EXT_INTID
  235. /* Calculate number of PPI registers */
  236. ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
  237. TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
  238. /* All other values except PPInum [0-2] are reserved */
  239. if (ppi_regs_num > 3U) {
  240. ppi_regs_num = 1U;
  241. }
  242. #else
  243. ppi_regs_num = 1U;
  244. #endif
  245. /*
  246. * Disable all SGIs (imp. def.)/(E)PPIs before configuring them.
  247. * This is a more scalable approach as it avoids clearing
  248. * the enable bits in the GICD_CTLR.
  249. */
  250. for (i = 0U; i < ppi_regs_num; ++i) {
  251. gicr_write_icenabler(gicr_base, i, ~0U);
  252. }
  253. /* Wait for pending writes to GICR_ICENABLER */
  254. gicr_wait_for_pending_write(gicr_base);
  255. /* 32 interrupt IDs per GICR_IGROUPR register */
  256. for (i = 0U; i < ppi_regs_num; ++i) {
  257. /* Treat all SGIs/(E)PPIs as G1NS by default */
  258. gicr_write_igroupr(gicr_base, i, ~0U);
  259. }
  260. /* 4 interrupt IDs per GICR_IPRIORITYR register */
  261. regs_num = ppi_regs_num << 3;
  262. for (i = 0U; i < regs_num; ++i) {
  263. /* Setup the default (E)PPI/SGI priorities doing 4 at a time */
  264. gicr_write_ipriorityr(gicr_base, i << 2, GICD_IPRIORITYR_DEF_VAL);
  265. }
  266. /* 16 interrupt IDs per GICR_ICFGR register */
  267. regs_num = ppi_regs_num << 1;
  268. for (i = (MIN_PPI_ID >> ICFGR_SHIFT); i < regs_num; ++i) {
  269. /* Configure all (E)PPIs as level triggered by default */
  270. gicr_write_icfgr(gicr_base, i, 0U);
  271. }
  272. }
  273. /*******************************************************************************
  274. * Helper function to configure properties of secure G0 and G1S (E)PPIs and SGIs
  275. ******************************************************************************/
  276. unsigned int gicv3_secure_ppi_sgi_config_props(uintptr_t gicr_base,
  277. const interrupt_prop_t *interrupt_props,
  278. unsigned int interrupt_props_num)
  279. {
  280. unsigned int i;
  281. const interrupt_prop_t *current_prop;
  282. unsigned int ctlr_enable = 0U;
  283. /* Make sure there's a valid property array */
  284. if (interrupt_props_num > 0U) {
  285. assert(interrupt_props != NULL);
  286. }
  287. for (i = 0U; i < interrupt_props_num; i++) {
  288. current_prop = &interrupt_props[i];
  289. unsigned int intr_num = current_prop->intr_num;
  290. /* Skip (E)SPI interrupt */
  291. if (!IS_SGI_PPI(intr_num)) {
  292. continue;
  293. }
  294. /* Configure this interrupt as a secure interrupt */
  295. gicr_clr_igroupr(gicr_base, intr_num);
  296. /* Configure this interrupt as G0 or a G1S interrupt */
  297. assert((current_prop->intr_grp == INTR_GROUP0) ||
  298. (current_prop->intr_grp == INTR_GROUP1S));
  299. if (current_prop->intr_grp == INTR_GROUP1S) {
  300. gicr_set_igrpmodr(gicr_base, intr_num);
  301. ctlr_enable |= CTLR_ENABLE_G1S_BIT;
  302. } else {
  303. gicr_clr_igrpmodr(gicr_base, intr_num);
  304. ctlr_enable |= CTLR_ENABLE_G0_BIT;
  305. }
  306. /* Set the priority of this interrupt */
  307. gicr_set_ipriorityr(gicr_base, intr_num,
  308. current_prop->intr_pri);
  309. /*
  310. * Set interrupt configuration for (E)PPIs.
  311. * Configurations for SGIs 0-15 are ignored.
  312. */
  313. if (intr_num >= MIN_PPI_ID) {
  314. gicr_set_icfgr(gicr_base, intr_num,
  315. current_prop->intr_cfg);
  316. }
  317. /* Enable this interrupt */
  318. gicr_set_isenabler(gicr_base, intr_num);
  319. }
  320. return ctlr_enable;
  321. }
  322. /**
  323. * gicv3_rdistif_get_number_frames() - determine size of GICv3 GICR region
  324. * @gicr_frame: base address of the GICR region to check
  325. *
  326. * This iterates over the GICR_TYPER registers of multiple GICR frames in
  327. * a GICR region, to find the instance which has the LAST bit set. For most
  328. * systems this corresponds to the number of cores handled by a redistributor,
  329. * but there could be disabled cores among them.
  330. * It assumes that each GICR region is fully accessible (till the LAST bit
  331. * marks the end of the region).
  332. * If a platform has multiple GICR regions, this function would need to be
  333. * called multiple times, providing the respective GICR base address each time.
  334. *
  335. * Return: number of valid GICR frames (at least 1, up to PLATFORM_CORE_COUNT)
  336. ******************************************************************************/
  337. unsigned int gicv3_rdistif_get_number_frames(const uintptr_t gicr_frame)
  338. {
  339. uintptr_t rdistif_base = gicr_frame;
  340. unsigned int count;
  341. for (count = 1U; count < PLATFORM_CORE_COUNT; count++) {
  342. uint64_t typer_val = gicr_read_typer(rdistif_base);
  343. if ((typer_val & TYPER_LAST_BIT) != 0U) {
  344. break;
  345. }
  346. rdistif_base += gicv3_redist_size(typer_val);
  347. }
  348. return count;
  349. }
  350. unsigned int gicv3_get_component_partnum(const uintptr_t gic_frame)
  351. {
  352. unsigned int part_id;
  353. /*
  354. * The lower 8 bits of PIDR0, complemented by the lower 4 bits of
  355. * PIDR1 contain a part number identifying the GIC component at a
  356. * particular base address.
  357. */
  358. part_id = mmio_read_32(gic_frame + GICD_PIDR0_GICV3) & 0xff;
  359. part_id |= (mmio_read_32(gic_frame + GICD_PIDR1_GICV3) << 8) & 0xf00;
  360. return part_id;
  361. }
  362. /*******************************************************************************
  363. * Helper function to return product ID and revision of GIC
  364. * @gicd_base: base address of the GIC distributor
  365. * @gic_prod_id: retrieved product id of GIC
  366. * @gic_rev: retrieved revision of GIC
  367. ******************************************************************************/
  368. void gicv3_get_component_prodid_rev(const uintptr_t gicd_base,
  369. unsigned int *gic_prod_id,
  370. uint8_t *gic_rev)
  371. {
  372. unsigned int gicd_iidr;
  373. uint8_t gic_variant;
  374. gicd_iidr = gicd_read_iidr(gicd_base);
  375. *gic_prod_id = gicd_iidr >> IIDR_PRODUCT_ID_SHIFT;
  376. *gic_prod_id &= IIDR_PRODUCT_ID_MASK;
  377. gic_variant = gicd_iidr >> IIDR_VARIANT_SHIFT;
  378. gic_variant &= IIDR_VARIANT_MASK;
  379. *gic_rev = gicd_iidr >> IIDR_REV_SHIFT;
  380. *gic_rev &= IIDR_REV_MASK;
  381. /*
  382. * pack gic variant and gic_rev in 1 byte
  383. * gic_rev = gic_variant[7:4] and gic_rev[0:3]
  384. */
  385. *gic_rev = *gic_rev | gic_variant << 0x4;
  386. }