gicv3_main.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. /*
  2. * Copyright (c) 2015-2024, 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/gicv3.h>
  14. #include <lib/spinlock.h>
  15. #include <plat/common/platform.h>
  16. #include "gicv3_private.h"
  17. const gicv3_driver_data_t *gicv3_driver_data;
  18. /*
  19. * Spinlock to guard registers needing read-modify-write. APIs protected by this
  20. * spinlock are used either at boot time (when only a single CPU is active), or
  21. * when the system is fully coherent.
  22. */
  23. static spinlock_t gic_lock;
  24. /*
  25. * Redistributor power operations are weakly bound so that they can be
  26. * overridden
  27. */
  28. #pragma weak gicv3_rdistif_off
  29. #pragma weak gicv3_rdistif_on
  30. /* Check for valid SGI/PPI or SPI interrupt ID */
  31. static bool is_valid_interrupt(unsigned int id);
  32. /*
  33. * Helper macros to save and restore GICR and GICD registers
  34. * corresponding to their numbers to and from the context
  35. */
  36. #define RESTORE_GICR_REG(base, ctx, name, i) \
  37. gicr_write_##name((base), (i), (ctx)->gicr_##name[(i)])
  38. #define SAVE_GICR_REG(base, ctx, name, i) \
  39. (ctx)->gicr_##name[(i)] = gicr_read_##name((base), (i))
  40. /* Helper macros to save and restore GICD registers to and from the context */
  41. #define RESTORE_GICD_REGS(base, ctx, intr_num, reg, REG) \
  42. do { \
  43. for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num);\
  44. int_id += (1U << REG##R_SHIFT)) { \
  45. gicd_write_##reg((base), int_id, \
  46. (ctx)->gicd_##reg[(int_id - MIN_SPI_ID) >> \
  47. REG##R_SHIFT]); \
  48. } \
  49. } while (false)
  50. #define SAVE_GICD_REGS(base, ctx, intr_num, reg, REG) \
  51. do { \
  52. for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num);\
  53. int_id += (1U << REG##R_SHIFT)) { \
  54. (ctx)->gicd_##reg[(int_id - MIN_SPI_ID) >> \
  55. REG##R_SHIFT] = gicd_read_##reg((base), int_id); \
  56. } \
  57. } while (false)
  58. #if GIC_EXT_INTID
  59. #define RESTORE_GICD_EREGS(base, ctx, intr_num, reg, REG) \
  60. do { \
  61. for (unsigned int int_id = MIN_ESPI_ID; int_id < (intr_num);\
  62. int_id += (1U << REG##R_SHIFT)) { \
  63. gicd_write_##reg((base), int_id, \
  64. (ctx)->gicd_##reg[(int_id - (MIN_ESPI_ID - \
  65. round_up(TOTAL_SPI_INTR_NUM, 1U << REG##R_SHIFT)))\
  66. >> REG##R_SHIFT]); \
  67. } \
  68. } while (false)
  69. #define SAVE_GICD_EREGS(base, ctx, intr_num, reg, REG) \
  70. do { \
  71. for (unsigned int int_id = MIN_ESPI_ID; int_id < (intr_num);\
  72. int_id += (1U << REG##R_SHIFT)) { \
  73. (ctx)->gicd_##reg[(int_id - (MIN_ESPI_ID - \
  74. round_up(TOTAL_SPI_INTR_NUM, 1U << REG##R_SHIFT)))\
  75. >> REG##R_SHIFT] = gicd_read_##reg((base), int_id);\
  76. } \
  77. } while (false)
  78. #else
  79. #define SAVE_GICD_EREGS(base, ctx, intr_num, reg, REG)
  80. #define RESTORE_GICD_EREGS(base, ctx, intr_num, reg, REG)
  81. #endif /* GIC_EXT_INTID */
  82. /*******************************************************************************
  83. * This function initialises the ARM GICv3 driver in EL3 with provided platform
  84. * inputs.
  85. ******************************************************************************/
  86. void __init gicv3_driver_init(const gicv3_driver_data_t *plat_driver_data)
  87. {
  88. unsigned int gic_version;
  89. unsigned int gicv2_compat;
  90. assert(plat_driver_data != NULL);
  91. assert(plat_driver_data->gicd_base != 0U);
  92. assert(plat_driver_data->rdistif_num != 0U);
  93. assert(plat_driver_data->rdistif_base_addrs != NULL);
  94. assert(IS_IN_EL3());
  95. assert((plat_driver_data->interrupt_props_num != 0U) ?
  96. (plat_driver_data->interrupt_props != NULL) : 1);
  97. /* Check for system register support */
  98. #ifndef __aarch64__
  99. assert((read_id_pfr1() &
  100. (ID_PFR1_GIC_MASK << ID_PFR1_GIC_SHIFT)) != 0U);
  101. #else
  102. assert((read_id_aa64pfr0_el1() &
  103. (ID_AA64PFR0_GIC_MASK << ID_AA64PFR0_GIC_SHIFT)) != 0U);
  104. #endif /* !__aarch64__ */
  105. gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
  106. gic_version >>= PIDR2_ARCH_REV_SHIFT;
  107. gic_version &= PIDR2_ARCH_REV_MASK;
  108. /* Check GIC version */
  109. #if !GIC_ENABLE_V4_EXTN
  110. assert(gic_version == ARCH_REV_GICV3);
  111. #endif
  112. /*
  113. * Find out whether the GIC supports the GICv2 compatibility mode.
  114. * The ARE_S bit resets to 0 if supported
  115. */
  116. gicv2_compat = gicd_read_ctlr(plat_driver_data->gicd_base);
  117. gicv2_compat >>= CTLR_ARE_S_SHIFT;
  118. gicv2_compat = gicv2_compat & CTLR_ARE_S_MASK;
  119. if (plat_driver_data->gicr_base != 0U) {
  120. /*
  121. * Find the base address of each implemented Redistributor interface.
  122. * The number of interfaces should be equal to the number of CPUs in the
  123. * system. The memory for saving these addresses has to be allocated by
  124. * the platform port
  125. */
  126. gicv3_rdistif_base_addrs_probe(plat_driver_data->rdistif_base_addrs,
  127. plat_driver_data->rdistif_num,
  128. plat_driver_data->gicr_base,
  129. plat_driver_data->mpidr_to_core_pos);
  130. #if !HW_ASSISTED_COHERENCY
  131. /*
  132. * Flush the rdistif_base_addrs[] contents linked to the GICv3 driver.
  133. */
  134. flush_dcache_range((uintptr_t)(plat_driver_data->rdistif_base_addrs),
  135. plat_driver_data->rdistif_num *
  136. sizeof(*(plat_driver_data->rdistif_base_addrs)));
  137. #endif
  138. }
  139. gicv3_driver_data = plat_driver_data;
  140. /*
  141. * The GIC driver data is initialized by the primary CPU with caches
  142. * enabled. When the secondary CPU boots up, it initializes the
  143. * GICC/GICR interface with the caches disabled. Hence flush the
  144. * driver data to ensure coherency. This is not required if the
  145. * platform has HW_ASSISTED_COHERENCY enabled.
  146. */
  147. #if !HW_ASSISTED_COHERENCY
  148. flush_dcache_range((uintptr_t)&gicv3_driver_data,
  149. sizeof(gicv3_driver_data));
  150. flush_dcache_range((uintptr_t)gicv3_driver_data,
  151. sizeof(*gicv3_driver_data));
  152. #endif
  153. gicv3_check_erratas_applies(plat_driver_data->gicd_base);
  154. INFO("GICv%u with%s legacy support detected.\n", gic_version,
  155. (gicv2_compat == 0U) ? "" : "out");
  156. INFO("ARM GICv%u driver initialized in EL3\n", gic_version);
  157. }
  158. /*******************************************************************************
  159. * This function initialises the GIC distributor interface based upon the data
  160. * provided by the platform while initialising the driver.
  161. ******************************************************************************/
  162. void __init gicv3_distif_init(void)
  163. {
  164. unsigned int bitmap;
  165. assert(gicv3_driver_data != NULL);
  166. assert(gicv3_driver_data->gicd_base != 0U);
  167. assert(IS_IN_EL3());
  168. /*
  169. * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
  170. * the ARE_S bit. The Distributor might generate a system error
  171. * otherwise.
  172. */
  173. gicd_clr_ctlr(gicv3_driver_data->gicd_base,
  174. CTLR_ENABLE_G0_BIT |
  175. CTLR_ENABLE_G1S_BIT |
  176. CTLR_ENABLE_G1NS_BIT,
  177. RWP_TRUE);
  178. /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
  179. gicd_set_ctlr(gicv3_driver_data->gicd_base,
  180. CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
  181. /* Set the default attribute of all (E)SPIs */
  182. gicv3_spis_config_defaults(gicv3_driver_data->gicd_base);
  183. bitmap = gicv3_secure_spis_config_props(
  184. gicv3_driver_data->gicd_base,
  185. gicv3_driver_data->interrupt_props,
  186. gicv3_driver_data->interrupt_props_num);
  187. /* Enable the secure (E)SPIs now that they have been configured */
  188. gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
  189. }
  190. /*******************************************************************************
  191. * This function initialises the GIC Redistributor interface of the calling CPU
  192. * (identified by the 'proc_num' parameter) based upon the data provided by the
  193. * platform while initialising the driver.
  194. ******************************************************************************/
  195. void gicv3_rdistif_init(unsigned int proc_num)
  196. {
  197. uintptr_t gicr_base;
  198. unsigned int bitmap;
  199. uint32_t ctlr;
  200. assert(gicv3_driver_data != NULL);
  201. assert(proc_num < gicv3_driver_data->rdistif_num);
  202. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  203. assert(gicv3_driver_data->gicd_base != 0U);
  204. ctlr = gicd_read_ctlr(gicv3_driver_data->gicd_base);
  205. assert((ctlr & CTLR_ARE_S_BIT) != 0U);
  206. assert(IS_IN_EL3());
  207. /* Power on redistributor */
  208. gicv3_rdistif_on(proc_num);
  209. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  210. assert(gicr_base != 0U);
  211. /* Set the default attribute of all SGIs and (E)PPIs */
  212. gicv3_ppi_sgi_config_defaults(gicr_base);
  213. bitmap = gicv3_secure_ppi_sgi_config_props(gicr_base,
  214. gicv3_driver_data->interrupt_props,
  215. gicv3_driver_data->interrupt_props_num);
  216. /* Enable interrupt groups as required, if not already */
  217. if ((ctlr & bitmap) != bitmap) {
  218. gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
  219. }
  220. }
  221. /*******************************************************************************
  222. * Functions to perform power operations on GIC Redistributor
  223. ******************************************************************************/
  224. void gicv3_rdistif_off(unsigned int proc_num)
  225. {
  226. }
  227. void gicv3_rdistif_on(unsigned int proc_num)
  228. {
  229. }
  230. /*******************************************************************************
  231. * This function enables the GIC CPU interface of the calling CPU using only
  232. * system register accesses.
  233. ******************************************************************************/
  234. void gicv3_cpuif_enable(unsigned int proc_num)
  235. {
  236. uintptr_t gicr_base;
  237. u_register_t scr_el3;
  238. unsigned int icc_sre_el3;
  239. assert(gicv3_driver_data != NULL);
  240. assert(proc_num < gicv3_driver_data->rdistif_num);
  241. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  242. assert(IS_IN_EL3());
  243. /* Mark the connected core as awake */
  244. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  245. gicv3_rdistif_mark_core_awake(gicr_base);
  246. /* Disable the legacy interrupt bypass */
  247. icc_sre_el3 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT;
  248. /*
  249. * Enable system register access for EL3 and allow lower exception
  250. * levels to configure the same for themselves. If the legacy mode is
  251. * not supported, the SRE bit is RAO/WI
  252. */
  253. icc_sre_el3 |= (ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT);
  254. write_icc_sre_el3(read_icc_sre_el3() | icc_sre_el3);
  255. scr_el3 = read_scr_el3();
  256. /*
  257. * Switch to NS state to write Non secure ICC_SRE_EL1 and
  258. * ICC_SRE_EL2 registers.
  259. */
  260. write_scr_el3(scr_el3 | SCR_NS_BIT);
  261. isb();
  262. write_icc_sre_el2(read_icc_sre_el2() | icc_sre_el3);
  263. write_icc_sre_el1(ICC_SRE_SRE_BIT);
  264. isb();
  265. /* Switch to secure state. */
  266. write_scr_el3(scr_el3 & (~SCR_NS_BIT));
  267. isb();
  268. /* Write the secure ICC_SRE_EL1 register */
  269. write_icc_sre_el1(ICC_SRE_SRE_BIT);
  270. isb();
  271. /* Program the idle priority in the PMR */
  272. write_icc_pmr_el1(GIC_PRI_MASK);
  273. /* Enable Group0 interrupts */
  274. write_icc_igrpen0_el1(IGRPEN1_EL1_ENABLE_G0_BIT);
  275. /* Enable Group1 Secure interrupts */
  276. write_icc_igrpen1_el3(read_icc_igrpen1_el3() |
  277. IGRPEN1_EL3_ENABLE_G1S_BIT);
  278. /* and restore the original */
  279. write_scr_el3(scr_el3);
  280. isb();
  281. /* Add DSB to ensure visibility of System register writes */
  282. dsb();
  283. }
  284. /*******************************************************************************
  285. * This function disables the GIC CPU interface of the calling CPU using
  286. * only system register accesses.
  287. ******************************************************************************/
  288. void gicv3_cpuif_disable(unsigned int proc_num)
  289. {
  290. uintptr_t gicr_base;
  291. assert(gicv3_driver_data != NULL);
  292. assert(proc_num < gicv3_driver_data->rdistif_num);
  293. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  294. assert(IS_IN_EL3());
  295. /* Disable legacy interrupt bypass */
  296. write_icc_sre_el3(read_icc_sre_el3() |
  297. (ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT));
  298. /* Disable Group0 interrupts */
  299. write_icc_igrpen0_el1(read_icc_igrpen0_el1() &
  300. ~IGRPEN1_EL1_ENABLE_G0_BIT);
  301. /* Disable Group1 Secure and Non-Secure interrupts */
  302. write_icc_igrpen1_el3(read_icc_igrpen1_el3() &
  303. ~(IGRPEN1_EL3_ENABLE_G1NS_BIT |
  304. IGRPEN1_EL3_ENABLE_G1S_BIT));
  305. /* Synchronise accesses to group enable registers */
  306. isb();
  307. /* Add DSB to ensure visibility of System register writes */
  308. dsb();
  309. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  310. assert(gicr_base != 0UL);
  311. /*
  312. * dsb() already issued previously after clearing the CPU group
  313. * enabled, apply below workaround to toggle the "DPG*"
  314. * bits of GICR_CTLR register for unblocking event.
  315. */
  316. gicv3_apply_errata_wa_2384374(gicr_base);
  317. /* Mark the connected core as asleep */
  318. gicv3_rdistif_mark_core_asleep(gicr_base);
  319. }
  320. /*******************************************************************************
  321. * This function returns the id of the highest priority pending interrupt at
  322. * the GIC cpu interface.
  323. ******************************************************************************/
  324. unsigned int gicv3_get_pending_interrupt_id(void)
  325. {
  326. unsigned int id;
  327. assert(IS_IN_EL3());
  328. id = (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
  329. /*
  330. * If the ID is special identifier corresponding to G1S or G1NS
  331. * interrupt, then read the highest pending group 1 interrupt.
  332. */
  333. if ((id == PENDING_G1S_INTID) || (id == PENDING_G1NS_INTID)) {
  334. return (uint32_t)read_icc_hppir1_el1() & HPPIR1_EL1_INTID_MASK;
  335. }
  336. return id;
  337. }
  338. /*******************************************************************************
  339. * This function returns the type of the highest priority pending interrupt at
  340. * the GIC cpu interface. The return values can be one of the following :
  341. * PENDING_G1S_INTID : The interrupt type is secure Group 1.
  342. * PENDING_G1NS_INTID : The interrupt type is non secure Group 1.
  343. * 0 - 1019 : The interrupt type is secure Group 0.
  344. * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
  345. * sufficient priority to be signaled
  346. ******************************************************************************/
  347. unsigned int gicv3_get_pending_interrupt_type(void)
  348. {
  349. assert(IS_IN_EL3());
  350. return (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
  351. }
  352. /*******************************************************************************
  353. * This function returns the group that has been configured under by the
  354. * interrupt controller for the given interrupt id i.e. either group0 or group1
  355. * Secure / Non Secure. The return value can be one of the following :
  356. * INTR_GROUP0 : The interrupt type is a Secure Group 0 interrupt
  357. * INTR_GROUP1S : The interrupt type is a Secure Group 1 secure interrupt
  358. * INTR_GROUP1NS: The interrupt type is a Secure Group 1 non secure
  359. * interrupt.
  360. ******************************************************************************/
  361. unsigned int gicv3_get_interrupt_group(unsigned int id, unsigned int proc_num)
  362. {
  363. unsigned int igroup, grpmodr;
  364. uintptr_t gicr_base;
  365. uintptr_t gicd_base;
  366. assert(IS_IN_EL3());
  367. assert(gicv3_driver_data != NULL);
  368. /* Ensure the parameters are valid */
  369. assert((id < PENDING_G1S_INTID) || (id >= MIN_LPI_ID));
  370. assert(proc_num < gicv3_driver_data->rdistif_num);
  371. /* All LPI interrupts are Group 1 non secure */
  372. if (id >= MIN_LPI_ID) {
  373. return INTR_GROUP1NS;
  374. }
  375. if (!is_valid_interrupt(id)) {
  376. panic();
  377. }
  378. /* Check interrupt ID */
  379. if (IS_SGI_PPI(id)) {
  380. /* SGIs: 0-15, PPIs: 16-31, EPPIs: 1056-1119 */
  381. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  382. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  383. igroup = gicr_get_igroupr(gicr_base, id);
  384. grpmodr = gicr_get_igrpmodr(gicr_base, id);
  385. } else {
  386. /* SPIs: 32-1019, ESPIs: 4096-5119 */
  387. assert(gicv3_driver_data->gicd_base != 0U);
  388. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  389. igroup = gicd_get_igroupr(gicd_base, id);
  390. grpmodr = gicd_get_igrpmodr(gicd_base, id);
  391. }
  392. /*
  393. * If the IGROUP bit is set, then it is a Group 1 Non secure
  394. * interrupt
  395. */
  396. if (igroup != 0U) {
  397. return INTR_GROUP1NS;
  398. }
  399. /* If the GRPMOD bit is set, then it is a Group 1 Secure interrupt */
  400. if (grpmodr != 0U) {
  401. return INTR_GROUP1S;
  402. }
  403. /* Else it is a Group 0 Secure interrupt */
  404. return INTR_GROUP0;
  405. }
  406. /*****************************************************************************
  407. * Function to save and disable the GIC ITS register context. The power
  408. * management of GIC ITS is implementation-defined and this function doesn't
  409. * save any memory structures required to support ITS. As the sequence to save
  410. * this state is implementation defined, it should be executed in platform
  411. * specific code. Calling this function alone and then powering down the GIC and
  412. * ITS without implementing the aforementioned platform specific code will
  413. * corrupt the ITS state.
  414. *
  415. * This function must be invoked after the GIC CPU interface is disabled.
  416. *****************************************************************************/
  417. void gicv3_its_save_disable(uintptr_t gits_base,
  418. gicv3_its_ctx_t * const its_ctx)
  419. {
  420. unsigned int i;
  421. assert(gicv3_driver_data != NULL);
  422. assert(IS_IN_EL3());
  423. assert(its_ctx != NULL);
  424. assert(gits_base != 0U);
  425. its_ctx->gits_ctlr = gits_read_ctlr(gits_base);
  426. /* Disable the ITS */
  427. gits_write_ctlr(gits_base, its_ctx->gits_ctlr & ~GITS_CTLR_ENABLED_BIT);
  428. /* Wait for quiescent state */
  429. gits_wait_for_quiescent_bit(gits_base);
  430. its_ctx->gits_cbaser = gits_read_cbaser(gits_base);
  431. its_ctx->gits_cwriter = gits_read_cwriter(gits_base);
  432. for (i = 0U; i < ARRAY_SIZE(its_ctx->gits_baser); i++) {
  433. its_ctx->gits_baser[i] = gits_read_baser(gits_base, i);
  434. }
  435. }
  436. /*****************************************************************************
  437. * Function to restore the GIC ITS register context. The power
  438. * management of GIC ITS is implementation defined and this function doesn't
  439. * restore any memory structures required to support ITS. The assumption is
  440. * that these structures are in memory and are retained during system suspend.
  441. *
  442. * This must be invoked before the GIC CPU interface is enabled.
  443. *****************************************************************************/
  444. void gicv3_its_restore(uintptr_t gits_base,
  445. const gicv3_its_ctx_t * const its_ctx)
  446. {
  447. unsigned int i;
  448. assert(gicv3_driver_data != NULL);
  449. assert(IS_IN_EL3());
  450. assert(its_ctx != NULL);
  451. assert(gits_base != 0U);
  452. /* Assert that the GITS is disabled and quiescent */
  453. assert((gits_read_ctlr(gits_base) & GITS_CTLR_ENABLED_BIT) == 0U);
  454. assert((gits_read_ctlr(gits_base) & GITS_CTLR_QUIESCENT_BIT) != 0U);
  455. gits_write_cbaser(gits_base, its_ctx->gits_cbaser);
  456. gits_write_cwriter(gits_base, its_ctx->gits_cwriter);
  457. for (i = 0U; i < ARRAY_SIZE(its_ctx->gits_baser); i++) {
  458. gits_write_baser(gits_base, i, its_ctx->gits_baser[i]);
  459. }
  460. /* Restore the ITS CTLR but leave the ITS disabled */
  461. gits_write_ctlr(gits_base, its_ctx->gits_ctlr & ~GITS_CTLR_ENABLED_BIT);
  462. }
  463. /*****************************************************************************
  464. * Function to save the GIC Redistributor register context. This function
  465. * must be invoked after CPU interface disable and prior to Distributor save.
  466. *****************************************************************************/
  467. void gicv3_rdistif_save(unsigned int proc_num,
  468. gicv3_redist_ctx_t * const rdist_ctx)
  469. {
  470. uintptr_t gicr_base;
  471. unsigned int i, ppi_regs_num, regs_num;
  472. assert(gicv3_driver_data != NULL);
  473. assert(proc_num < gicv3_driver_data->rdistif_num);
  474. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  475. assert(IS_IN_EL3());
  476. assert(rdist_ctx != NULL);
  477. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  478. #if GIC_EXT_INTID
  479. /* Calculate number of PPI registers */
  480. ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
  481. TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
  482. /* All other values except PPInum [0-2] are reserved */
  483. if (ppi_regs_num > 3U) {
  484. ppi_regs_num = 1U;
  485. }
  486. #else
  487. ppi_regs_num = 1U;
  488. #endif
  489. /*
  490. * Wait for any write to GICR_CTLR to complete before trying to save any
  491. * state.
  492. */
  493. gicr_wait_for_pending_write(gicr_base);
  494. rdist_ctx->gicr_ctlr = gicr_read_ctlr(gicr_base);
  495. rdist_ctx->gicr_propbaser = gicr_read_propbaser(gicr_base);
  496. rdist_ctx->gicr_pendbaser = gicr_read_pendbaser(gicr_base);
  497. /* 32 interrupt IDs per register */
  498. for (i = 0U; i < ppi_regs_num; ++i) {
  499. SAVE_GICR_REG(gicr_base, rdist_ctx, igroupr, i);
  500. SAVE_GICR_REG(gicr_base, rdist_ctx, isenabler, i);
  501. SAVE_GICR_REG(gicr_base, rdist_ctx, ispendr, i);
  502. SAVE_GICR_REG(gicr_base, rdist_ctx, isactiver, i);
  503. SAVE_GICR_REG(gicr_base, rdist_ctx, igrpmodr, i);
  504. }
  505. /* 16 interrupt IDs per GICR_ICFGR register */
  506. regs_num = ppi_regs_num << 1;
  507. for (i = 0U; i < regs_num; ++i) {
  508. SAVE_GICR_REG(gicr_base, rdist_ctx, icfgr, i);
  509. }
  510. rdist_ctx->gicr_nsacr = gicr_read_nsacr(gicr_base);
  511. /* 4 interrupt IDs per GICR_IPRIORITYR register */
  512. regs_num = ppi_regs_num << 3;
  513. for (i = 0U; i < regs_num; ++i) {
  514. rdist_ctx->gicr_ipriorityr[i] =
  515. gicr_ipriorityr_read(gicr_base, i);
  516. }
  517. /*
  518. * Call the pre-save hook that implements the IMP DEF sequence that may
  519. * be required on some GIC implementations. As this may need to access
  520. * the Redistributor registers, we pass it proc_num.
  521. */
  522. gicv3_distif_pre_save(proc_num);
  523. }
  524. /*****************************************************************************
  525. * Function to restore the GIC Redistributor register context. We disable
  526. * LPI and per-cpu interrupts before we start restore of the Redistributor.
  527. * This function must be invoked after Distributor restore but prior to
  528. * CPU interface enable. The pending and active interrupts are restored
  529. * after the interrupts are fully configured and enabled.
  530. *****************************************************************************/
  531. void gicv3_rdistif_init_restore(unsigned int proc_num,
  532. const gicv3_redist_ctx_t * const rdist_ctx)
  533. {
  534. uintptr_t gicr_base;
  535. unsigned int i, ppi_regs_num, regs_num;
  536. assert(gicv3_driver_data != NULL);
  537. assert(proc_num < gicv3_driver_data->rdistif_num);
  538. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  539. assert(IS_IN_EL3());
  540. assert(rdist_ctx != NULL);
  541. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  542. #if GIC_EXT_INTID
  543. /* Calculate number of PPI registers */
  544. ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
  545. TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
  546. /* All other values except PPInum [0-2] are reserved */
  547. if (ppi_regs_num > 3U) {
  548. ppi_regs_num = 1U;
  549. }
  550. #else
  551. ppi_regs_num = 1U;
  552. #endif
  553. /* Power on redistributor */
  554. gicv3_rdistif_on(proc_num);
  555. /*
  556. * Call the post-restore hook that implements the IMP DEF sequence that
  557. * may be required on some GIC implementations. As this may need to
  558. * access the Redistributor registers, we pass it proc_num.
  559. */
  560. gicv3_distif_post_restore(proc_num);
  561. /*
  562. * Disable all SGIs (imp. def.)/(E)PPIs before configuring them.
  563. * This is a more scalable approach as it avoids clearing the enable
  564. * bits in the GICD_CTLR.
  565. */
  566. for (i = 0U; i < ppi_regs_num; ++i) {
  567. gicr_write_icenabler(gicr_base, i, ~0U);
  568. }
  569. /* Wait for pending writes to GICR_ICENABLER */
  570. gicr_wait_for_pending_write(gicr_base);
  571. /*
  572. * Disable the LPIs to avoid unpredictable behavior when writing to
  573. * GICR_PROPBASER and GICR_PENDBASER.
  574. */
  575. gicr_write_ctlr(gicr_base,
  576. rdist_ctx->gicr_ctlr & ~(GICR_CTLR_EN_LPIS_BIT));
  577. gicr_wait_for_pending_write(gicr_base);
  578. /* Restore registers' content */
  579. gicr_write_propbaser(gicr_base, rdist_ctx->gicr_propbaser);
  580. gicr_write_pendbaser(gicr_base, rdist_ctx->gicr_pendbaser);
  581. /* 32 interrupt IDs per register */
  582. for (i = 0U; i < ppi_regs_num; ++i) {
  583. RESTORE_GICR_REG(gicr_base, rdist_ctx, igroupr, i);
  584. RESTORE_GICR_REG(gicr_base, rdist_ctx, igrpmodr, i);
  585. }
  586. /* 4 interrupt IDs per GICR_IPRIORITYR register */
  587. regs_num = ppi_regs_num << 3;
  588. for (i = 0U; i < regs_num; ++i) {
  589. gicr_ipriorityr_write(gicr_base, i,
  590. rdist_ctx->gicr_ipriorityr[i]);
  591. }
  592. /* 16 interrupt IDs per GICR_ICFGR register */
  593. regs_num = ppi_regs_num << 1;
  594. for (i = 0U; i < regs_num; ++i) {
  595. RESTORE_GICR_REG(gicr_base, rdist_ctx, icfgr, i);
  596. }
  597. gicr_write_nsacr(gicr_base, rdist_ctx->gicr_nsacr);
  598. /* Restore after group and priorities are set.
  599. * 32 interrupt IDs per register
  600. */
  601. for (i = 0U; i < ppi_regs_num; ++i) {
  602. RESTORE_GICR_REG(gicr_base, rdist_ctx, ispendr, i);
  603. RESTORE_GICR_REG(gicr_base, rdist_ctx, isactiver, i);
  604. }
  605. /*
  606. * Wait for all writes to the Distributor to complete before enabling
  607. * the SGI and (E)PPIs.
  608. */
  609. gicr_wait_for_upstream_pending_write(gicr_base);
  610. /* 32 interrupt IDs per GICR_ISENABLER register */
  611. for (i = 0U; i < ppi_regs_num; ++i) {
  612. RESTORE_GICR_REG(gicr_base, rdist_ctx, isenabler, i);
  613. }
  614. /*
  615. * Restore GICR_CTLR.Enable_LPIs bit and wait for pending writes in case
  616. * the first write to GICR_CTLR was still in flight (this write only
  617. * restores GICR_CTLR.Enable_LPIs and no waiting is required for this
  618. * bit).
  619. */
  620. gicr_write_ctlr(gicr_base, rdist_ctx->gicr_ctlr);
  621. gicr_wait_for_pending_write(gicr_base);
  622. }
  623. /*****************************************************************************
  624. * Function to save the GIC Distributor register context. This function
  625. * must be invoked after CPU interface disable and Redistributor save.
  626. *****************************************************************************/
  627. void gicv3_distif_save(gicv3_dist_ctx_t * const dist_ctx)
  628. {
  629. assert(gicv3_driver_data != NULL);
  630. assert(gicv3_driver_data->gicd_base != 0U);
  631. assert(IS_IN_EL3());
  632. assert(dist_ctx != NULL);
  633. uintptr_t gicd_base = gicv3_driver_data->gicd_base;
  634. unsigned int num_ints = gicv3_get_spi_limit(gicd_base);
  635. #if GIC_EXT_INTID
  636. unsigned int num_eints = gicv3_get_espi_limit(gicd_base);
  637. #endif
  638. /* Wait for pending write to complete */
  639. gicd_wait_for_pending_write(gicd_base);
  640. /* Save the GICD_CTLR */
  641. dist_ctx->gicd_ctlr = gicd_read_ctlr(gicd_base);
  642. /* Save GICD_IGROUPR for INTIDs 32 - 1019 */
  643. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUP);
  644. /* Save GICD_IGROUPRE for INTIDs 4096 - 5119 */
  645. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igroupr, IGROUP);
  646. /* Save GICD_ISENABLER for INT_IDs 32 - 1019 */
  647. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLE);
  648. /* Save GICD_ISENABLERE for INT_IDs 4096 - 5119 */
  649. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isenabler, ISENABLE);
  650. /* Save GICD_ISPENDR for INTIDs 32 - 1019 */
  651. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPEND);
  652. /* Save GICD_ISPENDRE for INTIDs 4096 - 5119 */
  653. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ispendr, ISPEND);
  654. /* Save GICD_ISACTIVER for INTIDs 32 - 1019 */
  655. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVE);
  656. /* Save GICD_ISACTIVERE for INTIDs 4096 - 5119 */
  657. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isactiver, ISACTIVE);
  658. /* Save GICD_IPRIORITYR for INTIDs 32 - 1019 */
  659. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITY);
  660. /* Save GICD_IPRIORITYRE for INTIDs 4096 - 5119 */
  661. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ipriorityr, IPRIORITY);
  662. /* Save GICD_ICFGR for INTIDs 32 - 1019 */
  663. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFG);
  664. /* Save GICD_ICFGRE for INTIDs 4096 - 5119 */
  665. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, icfgr, ICFG);
  666. /* Save GICD_IGRPMODR for INTIDs 32 - 1019 */
  667. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMOD);
  668. /* Save GICD_IGRPMODRE for INTIDs 4096 - 5119 */
  669. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igrpmodr, IGRPMOD);
  670. /* Save GICD_NSACR for INTIDs 32 - 1019 */
  671. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSAC);
  672. /* Save GICD_NSACRE for INTIDs 4096 - 5119 */
  673. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, nsacr, NSAC);
  674. /* Save GICD_IROUTER for INTIDs 32 - 1019 */
  675. SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTE);
  676. /* Save GICD_IROUTERE for INTIDs 4096 - 5119 */
  677. SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, irouter, IROUTE);
  678. /*
  679. * GICD_ITARGETSR<n> and GICD_SPENDSGIR<n> are RAZ/WI when
  680. * GICD_CTLR.ARE_(S|NS) bits are set which is the case for our GICv3
  681. * driver.
  682. */
  683. }
  684. /*****************************************************************************
  685. * Function to restore the GIC Distributor register context. We disable G0, G1S
  686. * and G1NS interrupt groups before we start restore of the Distributor. This
  687. * function must be invoked prior to Redistributor restore and CPU interface
  688. * enable. The pending and active interrupts are restored after the interrupts
  689. * are fully configured and enabled.
  690. *****************************************************************************/
  691. void gicv3_distif_init_restore(const gicv3_dist_ctx_t * const dist_ctx)
  692. {
  693. assert(gicv3_driver_data != NULL);
  694. assert(gicv3_driver_data->gicd_base != 0U);
  695. assert(IS_IN_EL3());
  696. assert(dist_ctx != NULL);
  697. uintptr_t gicd_base = gicv3_driver_data->gicd_base;
  698. /*
  699. * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
  700. * the ARE_S bit. The Distributor might generate a system error
  701. * otherwise.
  702. */
  703. gicd_clr_ctlr(gicd_base,
  704. CTLR_ENABLE_G0_BIT |
  705. CTLR_ENABLE_G1S_BIT |
  706. CTLR_ENABLE_G1NS_BIT,
  707. RWP_TRUE);
  708. /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
  709. gicd_set_ctlr(gicd_base, CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
  710. unsigned int num_ints = gicv3_get_spi_limit(gicd_base);
  711. #if GIC_EXT_INTID
  712. unsigned int num_eints = gicv3_get_espi_limit(gicd_base);
  713. #endif
  714. /* Restore GICD_IGROUPR for INTIDs 32 - 1019 */
  715. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUP);
  716. /* Restore GICD_IGROUPRE for INTIDs 4096 - 5119 */
  717. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igroupr, IGROUP);
  718. /* Restore GICD_IPRIORITYR for INTIDs 32 - 1019 */
  719. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITY);
  720. /* Restore GICD_IPRIORITYRE for INTIDs 4096 - 5119 */
  721. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ipriorityr, IPRIORITY);
  722. /* Restore GICD_ICFGR for INTIDs 32 - 1019 */
  723. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFG);
  724. /* Restore GICD_ICFGRE for INTIDs 4096 - 5119 */
  725. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, icfgr, ICFG);
  726. /* Restore GICD_IGRPMODR for INTIDs 32 - 1019 */
  727. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMOD);
  728. /* Restore GICD_IGRPMODRE for INTIDs 4096 - 5119 */
  729. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igrpmodr, IGRPMOD);
  730. /* Restore GICD_NSACR for INTIDs 32 - 1019 */
  731. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSAC);
  732. /* Restore GICD_NSACRE for INTIDs 4096 - 5119 */
  733. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, nsacr, NSAC);
  734. /* Restore GICD_IROUTER for INTIDs 32 - 1019 */
  735. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTE);
  736. /* Restore GICD_IROUTERE for INTIDs 4096 - 5119 */
  737. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, irouter, IROUTE);
  738. /*
  739. * Restore ISENABLER(E), ISPENDR(E) and ISACTIVER(E) after
  740. * the interrupts are configured.
  741. */
  742. /* Restore GICD_ISENABLER for INT_IDs 32 - 1019 */
  743. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLE);
  744. /* Restore GICD_ISENABLERE for INT_IDs 4096 - 5119 */
  745. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isenabler, ISENABLE);
  746. /* Restore GICD_ISPENDR for INTIDs 32 - 1019 */
  747. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPEND);
  748. /* Restore GICD_ISPENDRE for INTIDs 4096 - 5119 */
  749. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ispendr, ISPEND);
  750. /* Restore GICD_ISACTIVER for INTIDs 32 - 1019 */
  751. RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVE);
  752. /* Restore GICD_ISACTIVERE for INTIDs 4096 - 5119 */
  753. RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isactiver, ISACTIVE);
  754. /* Restore the GICD_CTLR */
  755. gicd_write_ctlr(gicd_base, dist_ctx->gicd_ctlr);
  756. gicd_wait_for_pending_write(gicd_base);
  757. }
  758. /*******************************************************************************
  759. * This function gets the priority of the interrupt the processor is currently
  760. * servicing.
  761. ******************************************************************************/
  762. unsigned int gicv3_get_running_priority(void)
  763. {
  764. return (unsigned int)read_icc_rpr_el1();
  765. }
  766. /*******************************************************************************
  767. * This function checks if the interrupt identified by id is active (whether the
  768. * state is either active, or active and pending). The proc_num is used if the
  769. * interrupt is SGI or (E)PPI and programs the corresponding Redistributor
  770. * interface.
  771. ******************************************************************************/
  772. unsigned int gicv3_get_interrupt_active(unsigned int id, unsigned int proc_num)
  773. {
  774. uintptr_t gicd_base;
  775. assert(gicv3_driver_data != NULL);
  776. assert(gicv3_driver_data->gicd_base != 0U);
  777. assert(proc_num < gicv3_driver_data->rdistif_num);
  778. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  779. if (!is_valid_interrupt(id)) {
  780. panic();
  781. }
  782. /* Check interrupt ID */
  783. if (IS_SGI_PPI(id)) {
  784. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  785. return gicr_get_isactiver(
  786. gicv3_driver_data->rdistif_base_addrs[proc_num], id);
  787. }
  788. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  789. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  790. return gicd_get_isactiver(gicd_base, id);
  791. }
  792. /*******************************************************************************
  793. * This function enables the interrupt identified by id. The proc_num
  794. * is used if the interrupt is SGI or PPI, and programs the corresponding
  795. * Redistributor interface.
  796. ******************************************************************************/
  797. void gicv3_enable_interrupt(unsigned int id, unsigned int proc_num)
  798. {
  799. uintptr_t gicd_base;
  800. assert(gicv3_driver_data != NULL);
  801. assert(gicv3_driver_data->gicd_base != 0U);
  802. assert(proc_num < gicv3_driver_data->rdistif_num);
  803. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  804. /*
  805. * Ensure that any shared variable updates depending on out of band
  806. * interrupt trigger are observed before enabling interrupt.
  807. */
  808. dsbishst();
  809. if (!is_valid_interrupt(id)) {
  810. panic();
  811. }
  812. /* Check interrupt ID */
  813. if (IS_SGI_PPI(id)) {
  814. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  815. gicr_set_isenabler(
  816. gicv3_driver_data->rdistif_base_addrs[proc_num], id);
  817. } else {
  818. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  819. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  820. gicd_set_isenabler(gicd_base, id);
  821. }
  822. }
  823. /*******************************************************************************
  824. * This function disables the interrupt identified by id. The proc_num
  825. * is used if the interrupt is SGI or PPI, and programs the corresponding
  826. * Redistributor interface.
  827. ******************************************************************************/
  828. void gicv3_disable_interrupt(unsigned int id, unsigned int proc_num)
  829. {
  830. uintptr_t gicd_base;
  831. assert(gicv3_driver_data != NULL);
  832. assert(gicv3_driver_data->gicd_base != 0U);
  833. assert(proc_num < gicv3_driver_data->rdistif_num);
  834. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  835. /*
  836. * Disable interrupt, and ensure that any shared variable updates
  837. * depending on out of band interrupt trigger are observed afterwards.
  838. */
  839. if (!is_valid_interrupt(id)) {
  840. panic();
  841. }
  842. /* Check interrupt ID */
  843. if (IS_SGI_PPI(id)) {
  844. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  845. gicr_set_icenabler(
  846. gicv3_driver_data->rdistif_base_addrs[proc_num], id);
  847. /* Write to clear enable requires waiting for pending writes */
  848. gicr_wait_for_pending_write(
  849. gicv3_driver_data->rdistif_base_addrs[proc_num]);
  850. } else {
  851. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  852. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  853. gicd_set_icenabler(gicd_base, id);
  854. /* Write to clear enable requires waiting for pending writes */
  855. gicd_wait_for_pending_write(gicd_base);
  856. }
  857. dsbishst();
  858. }
  859. /*******************************************************************************
  860. * This function sets the interrupt priority as supplied for the given interrupt
  861. * id.
  862. ******************************************************************************/
  863. void gicv3_set_interrupt_priority(unsigned int id, unsigned int proc_num,
  864. unsigned int priority)
  865. {
  866. uintptr_t gicr_base;
  867. uintptr_t gicd_base;
  868. assert(gicv3_driver_data != NULL);
  869. assert(gicv3_driver_data->gicd_base != 0U);
  870. assert(proc_num < gicv3_driver_data->rdistif_num);
  871. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  872. if (!is_valid_interrupt(id)) {
  873. panic();
  874. }
  875. /* Check interrupt ID */
  876. if (IS_SGI_PPI(id)) {
  877. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  878. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  879. gicr_set_ipriorityr(gicr_base, id, priority);
  880. } else {
  881. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  882. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  883. gicd_set_ipriorityr(gicd_base, id, priority);
  884. }
  885. }
  886. /*******************************************************************************
  887. * This function assigns group for the interrupt identified by id. The proc_num
  888. * is used if the interrupt is SGI or (E)PPI, and programs the corresponding
  889. * Redistributor interface. The group can be any of GICV3_INTR_GROUP*
  890. ******************************************************************************/
  891. void gicv3_set_interrupt_group(unsigned int id, unsigned int proc_num,
  892. unsigned int group)
  893. {
  894. bool igroup = false, grpmod = false;
  895. uintptr_t gicr_base;
  896. uintptr_t gicd_base;
  897. assert(gicv3_driver_data != NULL);
  898. assert(gicv3_driver_data->gicd_base != 0U);
  899. assert(proc_num < gicv3_driver_data->rdistif_num);
  900. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  901. switch (group) {
  902. case INTR_GROUP1S:
  903. igroup = false;
  904. grpmod = true;
  905. break;
  906. case INTR_GROUP0:
  907. igroup = false;
  908. grpmod = false;
  909. break;
  910. case INTR_GROUP1NS:
  911. igroup = true;
  912. grpmod = false;
  913. break;
  914. default:
  915. assert(false);
  916. break;
  917. }
  918. if (!is_valid_interrupt(id)) {
  919. panic();
  920. }
  921. /* Check interrupt ID */
  922. if (IS_SGI_PPI(id)) {
  923. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  924. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  925. igroup ? gicr_set_igroupr(gicr_base, id) :
  926. gicr_clr_igroupr(gicr_base, id);
  927. grpmod ? gicr_set_igrpmodr(gicr_base, id) :
  928. gicr_clr_igrpmodr(gicr_base, id);
  929. } else {
  930. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  931. /* Serialize read-modify-write to Distributor registers */
  932. spin_lock(&gic_lock);
  933. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  934. igroup ? gicd_set_igroupr(gicd_base, id) :
  935. gicd_clr_igroupr(gicd_base, id);
  936. grpmod ? gicd_set_igrpmodr(gicd_base, id) :
  937. gicd_clr_igrpmodr(gicd_base, id);
  938. spin_unlock(&gic_lock);
  939. }
  940. }
  941. /*******************************************************************************
  942. * This function raises the specified SGI of the specified group.
  943. *
  944. * The target parameter must be a valid MPIDR in the system.
  945. ******************************************************************************/
  946. void gicv3_raise_sgi(unsigned int sgi_num, gicv3_irq_group_t group,
  947. u_register_t target)
  948. {
  949. unsigned int tgt, aff3, aff2, aff1, aff0;
  950. uint64_t sgi_val;
  951. /* Verify interrupt number is in the SGI range */
  952. assert((sgi_num >= MIN_SGI_ID) && (sgi_num < MIN_PPI_ID));
  953. /* Extract affinity fields from target */
  954. aff0 = MPIDR_AFFLVL0_VAL(target);
  955. aff1 = MPIDR_AFFLVL1_VAL(target);
  956. aff2 = MPIDR_AFFLVL2_VAL(target);
  957. aff3 = MPIDR_AFFLVL3_VAL(target);
  958. /*
  959. * Make target list from affinity 0, and ensure GICv3 SGI can target
  960. * this PE.
  961. */
  962. assert(aff0 < GICV3_MAX_SGI_TARGETS);
  963. tgt = BIT_32(aff0);
  964. /* Raise SGI to PE specified by its affinity */
  965. sgi_val = GICV3_SGIR_VALUE(aff3, aff2, aff1, sgi_num, SGIR_IRM_TO_AFF,
  966. tgt);
  967. /*
  968. * Ensure that any shared variable updates depending on out of band
  969. * interrupt trigger are observed before raising SGI.
  970. */
  971. dsbishst();
  972. switch (group) {
  973. case GICV3_G0:
  974. write_icc_sgi0r_el1(sgi_val);
  975. break;
  976. case GICV3_G1NS:
  977. write_icc_asgi1r(sgi_val);
  978. break;
  979. case GICV3_G1S:
  980. write_icc_sgi1r(sgi_val);
  981. break;
  982. default:
  983. assert(false);
  984. break;
  985. }
  986. isb();
  987. }
  988. /*******************************************************************************
  989. * This function sets the interrupt routing for the given (E)SPI interrupt id.
  990. * The interrupt routing is specified in routing mode and mpidr.
  991. *
  992. * The routing mode can be either of:
  993. * - GICV3_IRM_ANY
  994. * - GICV3_IRM_PE
  995. *
  996. * The mpidr is the affinity of the PE to which the interrupt will be routed,
  997. * and is ignored for routing mode GICV3_IRM_ANY.
  998. ******************************************************************************/
  999. void gicv3_set_spi_routing(unsigned int id, unsigned int irm, u_register_t mpidr)
  1000. {
  1001. unsigned long long aff;
  1002. uint64_t router;
  1003. uintptr_t gicd_base;
  1004. assert(gicv3_driver_data != NULL);
  1005. assert(gicv3_driver_data->gicd_base != 0U);
  1006. assert((irm == GICV3_IRM_ANY) || (irm == GICV3_IRM_PE));
  1007. assert(IS_SPI(id));
  1008. aff = gicd_irouter_val_from_mpidr(mpidr, irm);
  1009. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  1010. gicd_write_irouter(gicd_base, id, aff);
  1011. /*
  1012. * In implementations that do not require 1 of N distribution of SPIs,
  1013. * IRM might be RAZ/WI. Read back and verify IRM bit.
  1014. */
  1015. if (irm == GICV3_IRM_ANY) {
  1016. router = gicd_read_irouter(gicd_base, id);
  1017. if (((router >> IROUTER_IRM_SHIFT) & IROUTER_IRM_MASK) == 0U) {
  1018. ERROR("GICv3 implementation doesn't support routing ANY\n");
  1019. panic();
  1020. }
  1021. }
  1022. }
  1023. /*******************************************************************************
  1024. * This function clears the pending status of an interrupt identified by id.
  1025. * The proc_num is used if the interrupt is SGI or (E)PPI, and programs the
  1026. * corresponding Redistributor interface.
  1027. ******************************************************************************/
  1028. void gicv3_clear_interrupt_pending(unsigned int id, unsigned int proc_num)
  1029. {
  1030. uintptr_t gicd_base;
  1031. assert(gicv3_driver_data != NULL);
  1032. assert(gicv3_driver_data->gicd_base != 0U);
  1033. assert(proc_num < gicv3_driver_data->rdistif_num);
  1034. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  1035. /*
  1036. * Clear pending interrupt, and ensure that any shared variable updates
  1037. * depending on out of band interrupt trigger are observed afterwards.
  1038. */
  1039. if (!is_valid_interrupt(id)) {
  1040. panic();
  1041. }
  1042. /* Check interrupt ID */
  1043. if (IS_SGI_PPI(id)) {
  1044. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  1045. gicr_set_icpendr(
  1046. gicv3_driver_data->rdistif_base_addrs[proc_num], id);
  1047. } else {
  1048. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  1049. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  1050. gicd_set_icpendr(gicd_base, id);
  1051. }
  1052. dsbishst();
  1053. }
  1054. /*******************************************************************************
  1055. * This function sets the pending status of an interrupt identified by id.
  1056. * The proc_num is used if the interrupt is SGI or PPI and programs the
  1057. * corresponding Redistributor interface.
  1058. ******************************************************************************/
  1059. void gicv3_set_interrupt_pending(unsigned int id, unsigned int proc_num)
  1060. {
  1061. uintptr_t gicd_base;
  1062. assert(gicv3_driver_data != NULL);
  1063. assert(gicv3_driver_data->gicd_base != 0U);
  1064. assert(proc_num < gicv3_driver_data->rdistif_num);
  1065. assert(gicv3_driver_data->rdistif_base_addrs != NULL);
  1066. /*
  1067. * Ensure that any shared variable updates depending on out of band
  1068. * interrupt trigger are observed before setting interrupt pending.
  1069. */
  1070. dsbishst();
  1071. if (!is_valid_interrupt(id)) {
  1072. panic();
  1073. }
  1074. /* Check interrupt ID */
  1075. if (IS_SGI_PPI(id)) {
  1076. /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
  1077. gicr_set_ispendr(
  1078. gicv3_driver_data->rdistif_base_addrs[proc_num], id);
  1079. } else {
  1080. /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
  1081. gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
  1082. gicd_set_ispendr(gicd_base, id);
  1083. }
  1084. }
  1085. /*******************************************************************************
  1086. * This function sets the PMR register with the supplied value. Returns the
  1087. * original PMR.
  1088. ******************************************************************************/
  1089. unsigned int gicv3_set_pmr(unsigned int mask)
  1090. {
  1091. unsigned int old_mask;
  1092. old_mask = (unsigned int)read_icc_pmr_el1();
  1093. /*
  1094. * Order memory updates w.r.t. PMR write, and ensure they're visible
  1095. * before potential out of band interrupt trigger because of PMR update.
  1096. * PMR system register writes are self-synchronizing, so no ISB required
  1097. * thereafter.
  1098. */
  1099. dsbishst();
  1100. write_icc_pmr_el1(mask);
  1101. return old_mask;
  1102. }
  1103. /*******************************************************************************
  1104. * This function restores the PMR register to old value and also triggers
  1105. * gicv3_apply_errata_wa_2384374() that flushes the GIC buffer allowing any
  1106. * pending interrupts to processed. Returns the original PMR.
  1107. ******************************************************************************/
  1108. unsigned int gicv3_deactivate_priority(unsigned int mask)
  1109. {
  1110. unsigned int old_mask, proc_num;
  1111. uintptr_t gicr_base;
  1112. old_mask = gicv3_set_pmr(mask);
  1113. proc_num = plat_my_core_pos();
  1114. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  1115. assert(gicr_base != 0UL);
  1116. /* Add DSB to ensure visibility of System register writes */
  1117. dsb();
  1118. gicv3_apply_errata_wa_2384374(gicr_base);
  1119. return old_mask;
  1120. }
  1121. /*******************************************************************************
  1122. * This function delegates the responsibility of discovering the corresponding
  1123. * Redistributor frames to each CPU itself. It is a modified version of
  1124. * gicv3_rdistif_base_addrs_probe() and is executed by each CPU in the platform
  1125. * unlike the previous way in which only the Primary CPU did the discovery of
  1126. * all the Redistributor frames for every CPU. It also handles the scenario in
  1127. * which the frames of various CPUs are not contiguous in physical memory.
  1128. ******************************************************************************/
  1129. int gicv3_rdistif_probe(const uintptr_t gicr_frame)
  1130. {
  1131. u_register_t mpidr, mpidr_self;
  1132. unsigned int proc_num;
  1133. uint64_t typer_val;
  1134. uintptr_t rdistif_base;
  1135. bool gicr_frame_found = false;
  1136. assert(gicv3_driver_data->gicr_base == 0U);
  1137. if (plat_can_cmo()) {
  1138. /* Ensure this function is called with Data Cache enabled */
  1139. #ifndef __aarch64__
  1140. assert((read_sctlr() & SCTLR_C_BIT) != 0U);
  1141. #else
  1142. assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
  1143. #endif /* !__aarch64__ */
  1144. }
  1145. mpidr_self = read_mpidr_el1() & MPIDR_AFFINITY_MASK;
  1146. rdistif_base = gicr_frame;
  1147. do {
  1148. typer_val = gicr_read_typer(rdistif_base);
  1149. mpidr = mpidr_from_gicr_typer(typer_val);
  1150. if (gicv3_driver_data->mpidr_to_core_pos != NULL) {
  1151. proc_num = gicv3_driver_data->mpidr_to_core_pos(mpidr);
  1152. } else {
  1153. proc_num = (unsigned int)(typer_val >>
  1154. TYPER_PROC_NUM_SHIFT) & TYPER_PROC_NUM_MASK;
  1155. }
  1156. if (mpidr == mpidr_self) {
  1157. /* The base address doesn't need to be initialized on
  1158. * every warm boot.
  1159. */
  1160. if (gicv3_driver_data->rdistif_base_addrs[proc_num]
  1161. != 0U) {
  1162. return 0;
  1163. }
  1164. gicv3_driver_data->rdistif_base_addrs[proc_num] =
  1165. rdistif_base;
  1166. gicr_frame_found = true;
  1167. break;
  1168. }
  1169. rdistif_base += gicv3_redist_size(typer_val);
  1170. } while ((typer_val & TYPER_LAST_BIT) == 0U);
  1171. if (!gicr_frame_found) {
  1172. return -1;
  1173. }
  1174. /*
  1175. * Flush the driver data to ensure coherency. This is
  1176. * not required if platform has HW_ASSISTED_COHERENCY
  1177. * enabled.
  1178. */
  1179. #if !HW_ASSISTED_COHERENCY
  1180. /*
  1181. * Flush the rdistif_base_addrs[] contents linked to the GICv3 driver.
  1182. */
  1183. flush_dcache_range((uintptr_t)&(gicv3_driver_data->rdistif_base_addrs[proc_num]),
  1184. sizeof(*(gicv3_driver_data->rdistif_base_addrs)));
  1185. #endif
  1186. return 0; /* Found matching GICR frame */
  1187. }
  1188. /******************************************************************************
  1189. * This function checks the interrupt ID and returns true for SGIs, (E)PPIs
  1190. * and (E)SPIs IDs. Any interrupt ID outside the range is invalid and returns
  1191. * false.
  1192. *****************************************************************************/
  1193. static bool is_valid_interrupt(unsigned int id)
  1194. {
  1195. /* Valid interrupts:
  1196. * SGIs: 0-15, PPIs: 16-31, EPPIs: 1056-1119
  1197. * SPIs: 32-1019, ESPIs: 4096-5119
  1198. */
  1199. if ((IS_SGI_PPI(id)) || (IS_SPI(id))) {
  1200. return true;
  1201. }
  1202. return false;
  1203. }