arm_gicv3_cpuif.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178
  1. /*
  2. * ARM Generic Interrupt Controller v3 (emulation)
  3. *
  4. * Copyright (c) 2016 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This code is licensed under the GPL, version 2 or (at your option)
  8. * any later version.
  9. */
  10. /* This file contains the code for the system register interface
  11. * portions of the GICv3.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/bitops.h"
  15. #include "qemu/log.h"
  16. #include "qemu/main-loop.h"
  17. #include "trace.h"
  18. #include "gicv3_internal.h"
  19. #include "hw/irq.h"
  20. #include "cpu.h"
  21. #include "target/arm/cpregs.h"
  22. #include "target/arm/cpu-features.h"
  23. #include "system/tcg.h"
  24. #include "system/qtest.h"
  25. /*
  26. * Special case return value from hppvi_index(); must be larger than
  27. * the architecturally maximum possible list register index (which is 15)
  28. */
  29. #define HPPVI_INDEX_VLPI 16
  30. static GICv3CPUState *icc_cs_from_env(CPUARMState *env)
  31. {
  32. return env->gicv3state;
  33. }
  34. static bool gicv3_use_ns_bank(CPUARMState *env)
  35. {
  36. /* Return true if we should use the NonSecure bank for a banked GIC
  37. * CPU interface register. Note that this differs from the
  38. * access_secure_reg() function because GICv3 banked registers are
  39. * banked even for AArch64, unlike the other CPU system registers.
  40. */
  41. return !arm_is_secure_below_el3(env);
  42. }
  43. /* The minimum BPR for the virtual interface is a configurable property */
  44. static inline int icv_min_vbpr(GICv3CPUState *cs)
  45. {
  46. return 7 - cs->vprebits;
  47. }
  48. static inline int ich_num_aprs(GICv3CPUState *cs)
  49. {
  50. /* Return the number of virtual APR registers (1, 2, or 4) */
  51. int aprmax = 1 << (cs->vprebits - 5);
  52. assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
  53. return aprmax;
  54. }
  55. /* Simple accessor functions for LR fields */
  56. static uint32_t ich_lr_vintid(uint64_t lr)
  57. {
  58. return extract64(lr, ICH_LR_EL2_VINTID_SHIFT, ICH_LR_EL2_VINTID_LENGTH);
  59. }
  60. static uint32_t ich_lr_pintid(uint64_t lr)
  61. {
  62. return extract64(lr, ICH_LR_EL2_PINTID_SHIFT, ICH_LR_EL2_PINTID_LENGTH);
  63. }
  64. static uint32_t ich_lr_prio(uint64_t lr)
  65. {
  66. return extract64(lr, ICH_LR_EL2_PRIORITY_SHIFT, ICH_LR_EL2_PRIORITY_LENGTH);
  67. }
  68. static int ich_lr_state(uint64_t lr)
  69. {
  70. return extract64(lr, ICH_LR_EL2_STATE_SHIFT, ICH_LR_EL2_STATE_LENGTH);
  71. }
  72. static bool icv_access(CPUARMState *env, int hcr_flags)
  73. {
  74. /* Return true if this ICC_ register access should really be
  75. * directed to an ICV_ access. hcr_flags is a mask of
  76. * HCR_EL2 bits to check: we treat this as an ICV_ access
  77. * if we are in NS EL1 and at least one of the specified
  78. * HCR_EL2 bits is set.
  79. *
  80. * ICV registers fall into four categories:
  81. * * access if NS EL1 and HCR_EL2.FMO == 1:
  82. * all ICV regs with '0' in their name
  83. * * access if NS EL1 and HCR_EL2.IMO == 1:
  84. * all ICV regs with '1' in their name
  85. * * access if NS EL1 and either IMO or FMO == 1:
  86. * CTLR, DIR, PMR, RPR
  87. */
  88. uint64_t hcr_el2 = arm_hcr_el2_eff(env);
  89. bool flagmatch = hcr_el2 & hcr_flags & (HCR_IMO | HCR_FMO);
  90. return flagmatch && arm_current_el(env) == 1
  91. && !arm_is_secure_below_el3(env);
  92. }
  93. static int read_vbpr(GICv3CPUState *cs, int grp)
  94. {
  95. /* Read VBPR value out of the VMCR field (caller must handle
  96. * VCBPR effects if required)
  97. */
  98. if (grp == GICV3_G0) {
  99. return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
  100. ICH_VMCR_EL2_VBPR0_LENGTH);
  101. } else {
  102. return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
  103. ICH_VMCR_EL2_VBPR1_LENGTH);
  104. }
  105. }
  106. static void write_vbpr(GICv3CPUState *cs, int grp, int value)
  107. {
  108. /* Write new VBPR1 value, handling the "writing a value less than
  109. * the minimum sets it to the minimum" semantics.
  110. */
  111. int min = icv_min_vbpr(cs);
  112. if (grp != GICV3_G0) {
  113. min++;
  114. }
  115. value = MAX(value, min);
  116. if (grp == GICV3_G0) {
  117. cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
  118. ICH_VMCR_EL2_VBPR0_LENGTH, value);
  119. } else {
  120. cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
  121. ICH_VMCR_EL2_VBPR1_LENGTH, value);
  122. }
  123. }
  124. static uint32_t icv_fullprio_mask(GICv3CPUState *cs)
  125. {
  126. /* Return a mask word which clears the unimplemented priority bits
  127. * from a priority value for a virtual interrupt. (Not to be confused
  128. * with the group priority, whose mask depends on the value of VBPR
  129. * for the interrupt group.)
  130. */
  131. return (~0U << (8 - cs->vpribits)) & 0xff;
  132. }
  133. static int ich_highest_active_virt_prio(GICv3CPUState *cs)
  134. {
  135. /* Calculate the current running priority based on the set bits
  136. * in the ICH Active Priority Registers.
  137. */
  138. int i;
  139. int aprmax = ich_num_aprs(cs);
  140. if (cs->ich_apr[GICV3_G1NS][0] & ICV_AP1R_EL1_NMI) {
  141. return 0x0;
  142. }
  143. for (i = 0; i < aprmax; i++) {
  144. uint32_t apr = cs->ich_apr[GICV3_G0][i] |
  145. cs->ich_apr[GICV3_G1NS][i];
  146. if (!apr) {
  147. continue;
  148. }
  149. return (i * 32 + ctz32(apr)) << (icv_min_vbpr(cs) + 1);
  150. }
  151. /* No current active interrupts: return idle priority */
  152. return 0xff;
  153. }
  154. static int hppvi_index(GICv3CPUState *cs)
  155. {
  156. /*
  157. * Return the list register index of the highest priority pending
  158. * virtual interrupt, as per the HighestPriorityVirtualInterrupt
  159. * pseudocode. If no pending virtual interrupts, return -1.
  160. * If the highest priority pending virtual interrupt is a vLPI,
  161. * return HPPVI_INDEX_VLPI.
  162. * (The pseudocode handles checking whether the vLPI is higher
  163. * priority than the highest priority list register at every
  164. * callsite of HighestPriorityVirtualInterrupt; we check it here.)
  165. */
  166. ARMCPU *cpu = ARM_CPU(cs->cpu);
  167. CPUARMState *env = &cpu->env;
  168. int idx = -1;
  169. int i;
  170. /* Note that a list register entry with a priority of 0xff will
  171. * never be reported by this function; this is the architecturally
  172. * correct behaviour.
  173. */
  174. int prio = 0xff;
  175. bool nmi = false;
  176. if (!(cs->ich_vmcr_el2 & (ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1))) {
  177. /* Both groups disabled, definitely nothing to do */
  178. return idx;
  179. }
  180. for (i = 0; i < cs->num_list_regs; i++) {
  181. uint64_t lr = cs->ich_lr_el2[i];
  182. bool thisnmi;
  183. int thisprio;
  184. if (ich_lr_state(lr) != ICH_LR_EL2_STATE_PENDING) {
  185. /* Not Pending */
  186. continue;
  187. }
  188. /* Ignore interrupts if relevant group enable not set */
  189. if (lr & ICH_LR_EL2_GROUP) {
  190. if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
  191. continue;
  192. }
  193. } else {
  194. if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
  195. continue;
  196. }
  197. }
  198. thisnmi = lr & ICH_LR_EL2_NMI;
  199. thisprio = ich_lr_prio(lr);
  200. if ((thisprio < prio) || ((thisprio == prio) && (thisnmi & (!nmi)))) {
  201. prio = thisprio;
  202. nmi = thisnmi;
  203. idx = i;
  204. }
  205. }
  206. /*
  207. * "no pending vLPI" is indicated with prio = 0xff, which always
  208. * fails the priority check here. vLPIs are only considered
  209. * when we are in Non-Secure state.
  210. */
  211. if (cs->hppvlpi.prio < prio && !arm_is_secure(env)) {
  212. if (cs->hppvlpi.grp == GICV3_G0) {
  213. if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0) {
  214. return HPPVI_INDEX_VLPI;
  215. }
  216. } else {
  217. if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1) {
  218. return HPPVI_INDEX_VLPI;
  219. }
  220. }
  221. }
  222. return idx;
  223. }
  224. static uint32_t icv_gprio_mask(GICv3CPUState *cs, int group)
  225. {
  226. /* Return a mask word which clears the subpriority bits from
  227. * a priority value for a virtual interrupt in the specified group.
  228. * This depends on the VBPR value.
  229. * If using VBPR0 then:
  230. * a BPR of 0 means the group priority bits are [7:1];
  231. * a BPR of 1 means they are [7:2], and so on down to
  232. * a BPR of 7 meaning no group priority bits at all.
  233. * If using VBPR1 then:
  234. * a BPR of 0 is impossible (the minimum value is 1)
  235. * a BPR of 1 means the group priority bits are [7:1];
  236. * a BPR of 2 means they are [7:2], and so on down to
  237. * a BPR of 7 meaning the group priority is [7].
  238. *
  239. * Which BPR to use depends on the group of the interrupt and
  240. * the current ICH_VMCR_EL2.VCBPR settings.
  241. *
  242. * This corresponds to the VGroupBits() pseudocode.
  243. */
  244. int bpr;
  245. if (group == GICV3_G1NS && cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
  246. group = GICV3_G0;
  247. }
  248. bpr = read_vbpr(cs, group);
  249. if (group == GICV3_G1NS) {
  250. assert(bpr > 0);
  251. bpr--;
  252. }
  253. return ~0U << (bpr + 1);
  254. }
  255. static bool icv_hppi_can_preempt(GICv3CPUState *cs, uint64_t lr)
  256. {
  257. /* Return true if we can signal this virtual interrupt defined by
  258. * the given list register value; see the pseudocode functions
  259. * CanSignalVirtualInterrupt and CanSignalVirtualInt.
  260. * Compare also icc_hppi_can_preempt() which is the non-virtual
  261. * equivalent of these checks.
  262. */
  263. int grp;
  264. bool is_nmi;
  265. uint32_t mask, prio, rprio, vpmr;
  266. if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
  267. /* Virtual interface disabled */
  268. return false;
  269. }
  270. /* We don't need to check that this LR is in Pending state because
  271. * that has already been done in hppvi_index().
  272. */
  273. prio = ich_lr_prio(lr);
  274. is_nmi = lr & ICH_LR_EL2_NMI;
  275. vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
  276. ICH_VMCR_EL2_VPMR_LENGTH);
  277. if (!is_nmi && prio >= vpmr) {
  278. /* Priority mask masks this interrupt */
  279. return false;
  280. }
  281. rprio = ich_highest_active_virt_prio(cs);
  282. if (rprio == 0xff) {
  283. /* No running interrupt so we can preempt */
  284. return true;
  285. }
  286. grp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
  287. mask = icv_gprio_mask(cs, grp);
  288. /* We only preempt a running interrupt if the pending interrupt's
  289. * group priority is sufficient (the subpriorities are not considered).
  290. */
  291. if ((prio & mask) < (rprio & mask)) {
  292. return true;
  293. }
  294. if ((prio & mask) == (rprio & mask) && is_nmi &&
  295. !(cs->ich_apr[GICV3_G1NS][0] & ICV_AP1R_EL1_NMI)) {
  296. return true;
  297. }
  298. return false;
  299. }
  300. static bool icv_hppvlpi_can_preempt(GICv3CPUState *cs)
  301. {
  302. /*
  303. * Return true if we can signal the highest priority pending vLPI.
  304. * We can assume we're Non-secure because hppvi_index() already
  305. * tested for that.
  306. */
  307. uint32_t mask, rprio, vpmr;
  308. if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
  309. /* Virtual interface disabled */
  310. return false;
  311. }
  312. vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
  313. ICH_VMCR_EL2_VPMR_LENGTH);
  314. if (cs->hppvlpi.prio >= vpmr) {
  315. /* Priority mask masks this interrupt */
  316. return false;
  317. }
  318. rprio = ich_highest_active_virt_prio(cs);
  319. if (rprio == 0xff) {
  320. /* No running interrupt so we can preempt */
  321. return true;
  322. }
  323. mask = icv_gprio_mask(cs, cs->hppvlpi.grp);
  324. /*
  325. * We only preempt a running interrupt if the pending interrupt's
  326. * group priority is sufficient (the subpriorities are not considered).
  327. */
  328. if ((cs->hppvlpi.prio & mask) < (rprio & mask)) {
  329. return true;
  330. }
  331. return false;
  332. }
  333. static uint32_t eoi_maintenance_interrupt_state(GICv3CPUState *cs,
  334. uint32_t *misr)
  335. {
  336. /* Return a set of bits indicating the EOI maintenance interrupt status
  337. * for each list register. The EOI maintenance interrupt status is
  338. * 1 if LR.State == 0 && LR.HW == 0 && LR.EOI == 1
  339. * (see the GICv3 spec for the ICH_EISR_EL2 register).
  340. * If misr is not NULL then we should also collect the information
  341. * about the MISR.EOI, MISR.NP and MISR.U bits.
  342. */
  343. uint32_t value = 0;
  344. int validcount = 0;
  345. bool seenpending = false;
  346. int i;
  347. for (i = 0; i < cs->num_list_regs; i++) {
  348. uint64_t lr = cs->ich_lr_el2[i];
  349. if ((lr & (ICH_LR_EL2_STATE_MASK | ICH_LR_EL2_HW | ICH_LR_EL2_EOI))
  350. == ICH_LR_EL2_EOI) {
  351. value |= (1 << i);
  352. }
  353. if ((lr & ICH_LR_EL2_STATE_MASK)) {
  354. validcount++;
  355. }
  356. if (ich_lr_state(lr) == ICH_LR_EL2_STATE_PENDING) {
  357. seenpending = true;
  358. }
  359. }
  360. if (misr) {
  361. if (validcount < 2 && (cs->ich_hcr_el2 & ICH_HCR_EL2_UIE)) {
  362. *misr |= ICH_MISR_EL2_U;
  363. }
  364. if (!seenpending && (cs->ich_hcr_el2 & ICH_HCR_EL2_NPIE)) {
  365. *misr |= ICH_MISR_EL2_NP;
  366. }
  367. if (value) {
  368. *misr |= ICH_MISR_EL2_EOI;
  369. }
  370. }
  371. return value;
  372. }
  373. static uint32_t maintenance_interrupt_state(GICv3CPUState *cs)
  374. {
  375. /* Return a set of bits indicating the maintenance interrupt status
  376. * (as seen in the ICH_MISR_EL2 register).
  377. */
  378. uint32_t value = 0;
  379. /* Scan list registers and fill in the U, NP and EOI bits */
  380. eoi_maintenance_interrupt_state(cs, &value);
  381. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_LRENPIE) &&
  382. (cs->ich_hcr_el2 & ICH_HCR_EL2_EOICOUNT_MASK)) {
  383. value |= ICH_MISR_EL2_LRENP;
  384. }
  385. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0EIE) &&
  386. (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
  387. value |= ICH_MISR_EL2_VGRP0E;
  388. }
  389. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0DIE) &&
  390. !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
  391. value |= ICH_MISR_EL2_VGRP0D;
  392. }
  393. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1EIE) &&
  394. (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
  395. value |= ICH_MISR_EL2_VGRP1E;
  396. }
  397. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1DIE) &&
  398. !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
  399. value |= ICH_MISR_EL2_VGRP1D;
  400. }
  401. return value;
  402. }
  403. void gicv3_cpuif_virt_irq_fiq_update(GICv3CPUState *cs)
  404. {
  405. /*
  406. * Tell the CPU about any pending virtual interrupts.
  407. * This should only be called for changes that affect the
  408. * vIRQ and vFIQ status and do not change the maintenance
  409. * interrupt status. This means that unlike gicv3_cpuif_virt_update()
  410. * this function won't recursively call back into the GIC code.
  411. * The main use of this is when the redistributor has changed the
  412. * highest priority pending virtual LPI.
  413. */
  414. int idx;
  415. int irqlevel = 0;
  416. int fiqlevel = 0;
  417. int nmilevel = 0;
  418. idx = hppvi_index(cs);
  419. trace_gicv3_cpuif_virt_update(gicv3_redist_affid(cs), idx,
  420. cs->hppvlpi.irq, cs->hppvlpi.grp,
  421. cs->hppvlpi.prio);
  422. if (idx == HPPVI_INDEX_VLPI) {
  423. if (icv_hppvlpi_can_preempt(cs)) {
  424. if (cs->hppvlpi.grp == GICV3_G0) {
  425. fiqlevel = 1;
  426. } else {
  427. irqlevel = 1;
  428. }
  429. }
  430. } else if (idx >= 0) {
  431. uint64_t lr = cs->ich_lr_el2[idx];
  432. if (icv_hppi_can_preempt(cs, lr)) {
  433. /*
  434. * Virtual interrupts are simple: G0 are always FIQ, and G1 are
  435. * IRQ or NMI which depends on the ICH_LR<n>_EL2.NMI to have
  436. * non-maskable property.
  437. */
  438. if (lr & ICH_LR_EL2_GROUP) {
  439. if (lr & ICH_LR_EL2_NMI) {
  440. nmilevel = 1;
  441. } else {
  442. irqlevel = 1;
  443. }
  444. } else {
  445. fiqlevel = 1;
  446. }
  447. }
  448. }
  449. trace_gicv3_cpuif_virt_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
  450. qemu_set_irq(cs->parent_vfiq, fiqlevel);
  451. qemu_set_irq(cs->parent_virq, irqlevel);
  452. qemu_set_irq(cs->parent_vnmi, nmilevel);
  453. }
  454. static void gicv3_cpuif_virt_update(GICv3CPUState *cs)
  455. {
  456. /*
  457. * Tell the CPU about any pending virtual interrupts or
  458. * maintenance interrupts, following a change to the state
  459. * of the CPU interface relevant to virtual interrupts.
  460. *
  461. * CAUTION: this function will call qemu_set_irq() on the
  462. * CPU maintenance IRQ line, which is typically wired up
  463. * to the GIC as a per-CPU interrupt. This means that it
  464. * will recursively call back into the GIC code via
  465. * gicv3_redist_set_irq() and thus into the CPU interface code's
  466. * gicv3_cpuif_update(). It is therefore important that this
  467. * function is only called as the final action of a CPU interface
  468. * register write implementation, after all the GIC state
  469. * fields have been updated. gicv3_cpuif_update() also must
  470. * not cause this function to be called, but that happens
  471. * naturally as a result of there being no architectural
  472. * linkage between the physical and virtual GIC logic.
  473. */
  474. ARMCPU *cpu = ARM_CPU(cs->cpu);
  475. int maintlevel = 0;
  476. gicv3_cpuif_virt_irq_fiq_update(cs);
  477. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_EN) &&
  478. maintenance_interrupt_state(cs) != 0) {
  479. maintlevel = 1;
  480. }
  481. trace_gicv3_cpuif_virt_set_maint_irq(gicv3_redist_affid(cs), maintlevel);
  482. qemu_set_irq(cpu->gicv3_maintenance_interrupt, maintlevel);
  483. }
  484. static uint64_t icv_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
  485. {
  486. GICv3CPUState *cs = icc_cs_from_env(env);
  487. int regno = ri->opc2 & 3;
  488. int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
  489. uint64_t value = cs->ich_apr[grp][regno];
  490. trace_gicv3_icv_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
  491. return value;
  492. }
  493. static void icv_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
  494. uint64_t value)
  495. {
  496. GICv3CPUState *cs = icc_cs_from_env(env);
  497. int regno = ri->opc2 & 3;
  498. int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
  499. trace_gicv3_icv_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
  500. if (cs->nmi_support) {
  501. cs->ich_apr[grp][regno] = value & (0xFFFFFFFFU | ICV_AP1R_EL1_NMI);
  502. } else {
  503. cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
  504. }
  505. gicv3_cpuif_virt_irq_fiq_update(cs);
  506. return;
  507. }
  508. static uint64_t icv_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  509. {
  510. GICv3CPUState *cs = icc_cs_from_env(env);
  511. int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
  512. uint64_t bpr;
  513. bool satinc = false;
  514. if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
  515. /* reads return bpr0 + 1 saturated to 7, writes ignored */
  516. grp = GICV3_G0;
  517. satinc = true;
  518. }
  519. bpr = read_vbpr(cs, grp);
  520. if (satinc) {
  521. bpr++;
  522. bpr = MIN(bpr, 7);
  523. }
  524. trace_gicv3_icv_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
  525. return bpr;
  526. }
  527. static void icv_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  528. uint64_t value)
  529. {
  530. GICv3CPUState *cs = icc_cs_from_env(env);
  531. int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
  532. trace_gicv3_icv_bpr_write(ri->crm == 8 ? 0 : 1,
  533. gicv3_redist_affid(cs), value);
  534. if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
  535. /* reads return bpr0 + 1 saturated to 7, writes ignored */
  536. return;
  537. }
  538. write_vbpr(cs, grp, value);
  539. gicv3_cpuif_virt_irq_fiq_update(cs);
  540. }
  541. static uint64_t icv_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  542. {
  543. GICv3CPUState *cs = icc_cs_from_env(env);
  544. uint64_t value;
  545. value = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
  546. ICH_VMCR_EL2_VPMR_LENGTH);
  547. trace_gicv3_icv_pmr_read(gicv3_redist_affid(cs), value);
  548. return value;
  549. }
  550. static void icv_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  551. uint64_t value)
  552. {
  553. GICv3CPUState *cs = icc_cs_from_env(env);
  554. trace_gicv3_icv_pmr_write(gicv3_redist_affid(cs), value);
  555. value &= icv_fullprio_mask(cs);
  556. cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
  557. ICH_VMCR_EL2_VPMR_LENGTH, value);
  558. gicv3_cpuif_virt_irq_fiq_update(cs);
  559. }
  560. static uint64_t icv_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
  561. {
  562. GICv3CPUState *cs = icc_cs_from_env(env);
  563. int enbit;
  564. uint64_t value;
  565. enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
  566. value = extract64(cs->ich_vmcr_el2, enbit, 1);
  567. trace_gicv3_icv_igrpen_read(ri->opc2 & 1 ? 1 : 0,
  568. gicv3_redist_affid(cs), value);
  569. return value;
  570. }
  571. static void icv_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
  572. uint64_t value)
  573. {
  574. GICv3CPUState *cs = icc_cs_from_env(env);
  575. int enbit;
  576. trace_gicv3_icv_igrpen_write(ri->opc2 & 1 ? 1 : 0,
  577. gicv3_redist_affid(cs), value);
  578. enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
  579. cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, enbit, 1, value);
  580. gicv3_cpuif_virt_update(cs);
  581. }
  582. static uint64_t icv_ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  583. {
  584. GICv3CPUState *cs = icc_cs_from_env(env);
  585. uint64_t value;
  586. /* Note that the fixed fields here (A3V, SEIS, IDbits, PRIbits)
  587. * should match the ones reported in ich_vtr_read().
  588. */
  589. value = ICC_CTLR_EL1_A3V | (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
  590. ((cs->vpribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
  591. if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM) {
  592. value |= ICC_CTLR_EL1_EOIMODE;
  593. }
  594. if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
  595. value |= ICC_CTLR_EL1_CBPR;
  596. }
  597. trace_gicv3_icv_ctlr_read(gicv3_redist_affid(cs), value);
  598. return value;
  599. }
  600. static void icv_ctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  601. uint64_t value)
  602. {
  603. GICv3CPUState *cs = icc_cs_from_env(env);
  604. trace_gicv3_icv_ctlr_write(gicv3_redist_affid(cs), value);
  605. cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VCBPR_SHIFT,
  606. 1, value & ICC_CTLR_EL1_CBPR ? 1 : 0);
  607. cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VEOIM_SHIFT,
  608. 1, value & ICC_CTLR_EL1_EOIMODE ? 1 : 0);
  609. gicv3_cpuif_virt_irq_fiq_update(cs);
  610. }
  611. static uint64_t icv_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  612. {
  613. GICv3CPUState *cs = icc_cs_from_env(env);
  614. uint64_t prio = ich_highest_active_virt_prio(cs);
  615. if (cs->ich_apr[GICV3_G1NS][0] & ICV_AP1R_EL1_NMI) {
  616. prio |= ICV_RPR_EL1_NMI;
  617. }
  618. trace_gicv3_icv_rpr_read(gicv3_redist_affid(cs), prio);
  619. return prio;
  620. }
  621. static uint64_t icv_hppir_read(CPUARMState *env, const ARMCPRegInfo *ri)
  622. {
  623. GICv3CPUState *cs = icc_cs_from_env(env);
  624. int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
  625. int idx = hppvi_index(cs);
  626. uint64_t value = INTID_SPURIOUS;
  627. if (idx == HPPVI_INDEX_VLPI) {
  628. if (cs->hppvlpi.grp == grp) {
  629. value = cs->hppvlpi.irq;
  630. }
  631. } else if (idx >= 0) {
  632. uint64_t lr = cs->ich_lr_el2[idx];
  633. int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
  634. if (grp == thisgrp) {
  635. value = ich_lr_vintid(lr);
  636. }
  637. }
  638. trace_gicv3_icv_hppir_read(ri->crm == 8 ? 0 : 1,
  639. gicv3_redist_affid(cs), value);
  640. return value;
  641. }
  642. static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp)
  643. {
  644. /* Activate the interrupt in the specified list register
  645. * by moving it from Pending to Active state, and update the
  646. * Active Priority Registers.
  647. */
  648. uint32_t mask = icv_gprio_mask(cs, grp);
  649. int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask;
  650. bool nmi = cs->ich_lr_el2[idx] & ICH_LR_EL2_NMI;
  651. int aprbit = prio >> (8 - cs->vprebits);
  652. int regno = aprbit / 32;
  653. int regbit = aprbit % 32;
  654. cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
  655. cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT;
  656. if (nmi) {
  657. cs->ich_apr[grp][regno] |= ICV_AP1R_EL1_NMI;
  658. } else {
  659. cs->ich_apr[grp][regno] |= (1U << regbit);
  660. }
  661. }
  662. static void icv_activate_vlpi(GICv3CPUState *cs)
  663. {
  664. uint32_t mask = icv_gprio_mask(cs, cs->hppvlpi.grp);
  665. int prio = cs->hppvlpi.prio & mask;
  666. int aprbit = prio >> (8 - cs->vprebits);
  667. int regno = aprbit / 32;
  668. int regbit = aprbit % 32;
  669. cs->ich_apr[cs->hppvlpi.grp][regno] |= (1U << regbit);
  670. gicv3_redist_vlpi_pending(cs, cs->hppvlpi.irq, 0);
  671. }
  672. static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri)
  673. {
  674. GICv3CPUState *cs = icc_cs_from_env(env);
  675. int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
  676. int idx = hppvi_index(cs);
  677. uint64_t intid = INTID_SPURIOUS;
  678. int el = arm_current_el(env);
  679. if (idx == HPPVI_INDEX_VLPI) {
  680. if (cs->hppvlpi.grp == grp && icv_hppvlpi_can_preempt(cs)) {
  681. intid = cs->hppvlpi.irq;
  682. icv_activate_vlpi(cs);
  683. }
  684. } else if (idx >= 0) {
  685. uint64_t lr = cs->ich_lr_el2[idx];
  686. int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
  687. bool nmi = env->cp15.sctlr_el[el] & SCTLR_NMI && lr & ICH_LR_EL2_NMI;
  688. if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) {
  689. intid = ich_lr_vintid(lr);
  690. if (!gicv3_intid_is_special(intid)) {
  691. if (!nmi) {
  692. icv_activate_irq(cs, idx, grp);
  693. } else {
  694. intid = INTID_NMI;
  695. }
  696. } else {
  697. /* Interrupt goes from Pending to Invalid */
  698. cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
  699. /* We will now return the (bogus) ID from the list register,
  700. * as per the pseudocode.
  701. */
  702. }
  703. }
  704. }
  705. trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1,
  706. gicv3_redist_affid(cs), intid);
  707. gicv3_cpuif_virt_update(cs);
  708. return intid;
  709. }
  710. static uint64_t icv_nmiar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
  711. {
  712. GICv3CPUState *cs = icc_cs_from_env(env);
  713. int idx = hppvi_index(cs);
  714. uint64_t intid = INTID_SPURIOUS;
  715. if (idx >= 0 && idx != HPPVI_INDEX_VLPI) {
  716. uint64_t lr = cs->ich_lr_el2[idx];
  717. int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
  718. if ((thisgrp == GICV3_G1NS) && icv_hppi_can_preempt(cs, lr)) {
  719. intid = ich_lr_vintid(lr);
  720. if (!gicv3_intid_is_special(intid)) {
  721. if (lr & ICH_LR_EL2_NMI) {
  722. icv_activate_irq(cs, idx, GICV3_G1NS);
  723. } else {
  724. intid = INTID_SPURIOUS;
  725. }
  726. } else {
  727. /* Interrupt goes from Pending to Invalid */
  728. cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
  729. /*
  730. * We will now return the (bogus) ID from the list register,
  731. * as per the pseudocode.
  732. */
  733. }
  734. }
  735. }
  736. trace_gicv3_icv_nmiar1_read(gicv3_redist_affid(cs), intid);
  737. gicv3_cpuif_virt_update(cs);
  738. return intid;
  739. }
  740. static uint32_t icc_fullprio_mask(GICv3CPUState *cs)
  741. {
  742. /*
  743. * Return a mask word which clears the unimplemented priority bits
  744. * from a priority value for a physical interrupt. (Not to be confused
  745. * with the group priority, whose mask depends on the value of BPR
  746. * for the interrupt group.)
  747. */
  748. return (~0U << (8 - cs->pribits)) & 0xff;
  749. }
  750. static inline int icc_min_bpr(GICv3CPUState *cs)
  751. {
  752. /* The minimum BPR for the physical interface. */
  753. return 7 - cs->prebits;
  754. }
  755. static inline int icc_min_bpr_ns(GICv3CPUState *cs)
  756. {
  757. return icc_min_bpr(cs) + 1;
  758. }
  759. static inline int icc_num_aprs(GICv3CPUState *cs)
  760. {
  761. /* Return the number of APR registers (1, 2, or 4) */
  762. int aprmax = 1 << MAX(cs->prebits - 5, 0);
  763. assert(aprmax <= ARRAY_SIZE(cs->icc_apr[0]));
  764. return aprmax;
  765. }
  766. static int icc_highest_active_prio(GICv3CPUState *cs)
  767. {
  768. /* Calculate the current running priority based on the set bits
  769. * in the Active Priority Registers.
  770. */
  771. int i;
  772. if (cs->nmi_support) {
  773. /*
  774. * If an NMI is active this takes precedence over anything else
  775. * for priority purposes; the NMI bit is only in the AP1R0 bit.
  776. * We return here the effective priority of the NMI, which is
  777. * either 0x0 or 0x80. Callers will need to check NMI again for
  778. * purposes of either setting the RPR register bits or for
  779. * prioritization of NMI vs non-NMI.
  780. */
  781. if (cs->icc_apr[GICV3_G1][0] & ICC_AP1R_EL1_NMI) {
  782. return 0;
  783. }
  784. if (cs->icc_apr[GICV3_G1NS][0] & ICC_AP1R_EL1_NMI) {
  785. return (cs->gic->gicd_ctlr & GICD_CTLR_DS) ? 0 : 0x80;
  786. }
  787. }
  788. for (i = 0; i < icc_num_aprs(cs); i++) {
  789. uint32_t apr = cs->icc_apr[GICV3_G0][i] |
  790. cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
  791. if (!apr) {
  792. continue;
  793. }
  794. return (i * 32 + ctz32(apr)) << (icc_min_bpr(cs) + 1);
  795. }
  796. /* No current active interrupts: return idle priority */
  797. return 0xff;
  798. }
  799. static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
  800. {
  801. /* Return a mask word which clears the subpriority bits from
  802. * a priority value for an interrupt in the specified group.
  803. * This depends on the BPR value. For CBPR0 (S or NS):
  804. * a BPR of 0 means the group priority bits are [7:1];
  805. * a BPR of 1 means they are [7:2], and so on down to
  806. * a BPR of 7 meaning no group priority bits at all.
  807. * For CBPR1 NS:
  808. * a BPR of 0 is impossible (the minimum value is 1)
  809. * a BPR of 1 means the group priority bits are [7:1];
  810. * a BPR of 2 means they are [7:2], and so on down to
  811. * a BPR of 7 meaning the group priority is [7].
  812. *
  813. * Which BPR to use depends on the group of the interrupt and
  814. * the current ICC_CTLR.CBPR settings.
  815. *
  816. * This corresponds to the GroupBits() pseudocode.
  817. */
  818. int bpr;
  819. if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
  820. (group == GICV3_G1NS &&
  821. cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
  822. group = GICV3_G0;
  823. }
  824. bpr = cs->icc_bpr[group] & 7;
  825. if (group == GICV3_G1NS) {
  826. assert(bpr > 0);
  827. bpr--;
  828. }
  829. return ~0U << (bpr + 1);
  830. }
  831. static bool icc_no_enabled_hppi(GICv3CPUState *cs)
  832. {
  833. /* Return true if there is no pending interrupt, or the
  834. * highest priority pending interrupt is in a group which has been
  835. * disabled at the CPU interface by the ICC_IGRPEN* register enable bits.
  836. */
  837. return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
  838. }
  839. static bool icc_hppi_can_preempt(GICv3CPUState *cs)
  840. {
  841. /* Return true if we have a pending interrupt of sufficient
  842. * priority to preempt.
  843. */
  844. int rprio;
  845. uint32_t mask;
  846. ARMCPU *cpu = ARM_CPU(cs->cpu);
  847. CPUARMState *env = &cpu->env;
  848. if (icc_no_enabled_hppi(cs)) {
  849. return false;
  850. }
  851. if (cs->hppi.nmi) {
  852. if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
  853. cs->hppi.grp == GICV3_G1NS) {
  854. if (cs->icc_pmr_el1 < 0x80) {
  855. return false;
  856. }
  857. if (arm_is_secure(env) && cs->icc_pmr_el1 == 0x80) {
  858. return false;
  859. }
  860. }
  861. } else if (cs->hppi.prio >= cs->icc_pmr_el1) {
  862. /* Priority mask masks this interrupt */
  863. return false;
  864. }
  865. rprio = icc_highest_active_prio(cs);
  866. if (rprio == 0xff) {
  867. /* No currently running interrupt so we can preempt */
  868. return true;
  869. }
  870. mask = icc_gprio_mask(cs, cs->hppi.grp);
  871. /* We only preempt a running interrupt if the pending interrupt's
  872. * group priority is sufficient (the subpriorities are not considered).
  873. */
  874. if ((cs->hppi.prio & mask) < (rprio & mask)) {
  875. return true;
  876. }
  877. if (cs->hppi.nmi && (cs->hppi.prio & mask) == (rprio & mask)) {
  878. if (!(cs->icc_apr[cs->hppi.grp][0] & ICC_AP1R_EL1_NMI)) {
  879. return true;
  880. }
  881. }
  882. return false;
  883. }
  884. void gicv3_cpuif_update(GICv3CPUState *cs)
  885. {
  886. /* Tell the CPU about its highest priority pending interrupt */
  887. int irqlevel = 0;
  888. int fiqlevel = 0;
  889. int nmilevel = 0;
  890. ARMCPU *cpu = ARM_CPU(cs->cpu);
  891. CPUARMState *env = &cpu->env;
  892. g_assert(bql_locked());
  893. trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
  894. cs->hppi.grp, cs->hppi.prio);
  895. if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
  896. /* If a Security-enabled GIC sends a G1S interrupt to a
  897. * Security-disabled CPU, we must treat it as if it were G0.
  898. */
  899. cs->hppi.grp = GICV3_G0;
  900. }
  901. if (icc_hppi_can_preempt(cs)) {
  902. /* We have an interrupt: should we signal it as IRQ or FIQ?
  903. * This is described in the GICv3 spec section 4.6.2.
  904. */
  905. bool isfiq;
  906. switch (cs->hppi.grp) {
  907. case GICV3_G0:
  908. isfiq = true;
  909. break;
  910. case GICV3_G1:
  911. isfiq = (!arm_is_secure(env) ||
  912. (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
  913. break;
  914. case GICV3_G1NS:
  915. isfiq = arm_is_secure(env);
  916. break;
  917. default:
  918. g_assert_not_reached();
  919. }
  920. if (isfiq) {
  921. fiqlevel = 1;
  922. } else if (cs->hppi.nmi) {
  923. nmilevel = 1;
  924. } else {
  925. irqlevel = 1;
  926. }
  927. }
  928. trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
  929. qemu_set_irq(cs->parent_fiq, fiqlevel);
  930. qemu_set_irq(cs->parent_irq, irqlevel);
  931. qemu_set_irq(cs->parent_nmi, nmilevel);
  932. }
  933. static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  934. {
  935. GICv3CPUState *cs = icc_cs_from_env(env);
  936. uint32_t value = cs->icc_pmr_el1;
  937. if (icv_access(env, HCR_FMO | HCR_IMO)) {
  938. return icv_pmr_read(env, ri);
  939. }
  940. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
  941. (env->cp15.scr_el3 & SCR_FIQ)) {
  942. /* NS access and Group 0 is inaccessible to NS: return the
  943. * NS view of the current priority
  944. */
  945. if ((value & 0x80) == 0) {
  946. /* Secure priorities not visible to NS */
  947. value = 0;
  948. } else if (value != 0xff) {
  949. value = (value << 1) & 0xff;
  950. }
  951. }
  952. trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
  953. return value;
  954. }
  955. static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  956. uint64_t value)
  957. {
  958. GICv3CPUState *cs = icc_cs_from_env(env);
  959. if (icv_access(env, HCR_FMO | HCR_IMO)) {
  960. return icv_pmr_write(env, ri, value);
  961. }
  962. trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
  963. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
  964. (env->cp15.scr_el3 & SCR_FIQ)) {
  965. /* NS access and Group 0 is inaccessible to NS: return the
  966. * NS view of the current priority
  967. */
  968. if (!(cs->icc_pmr_el1 & 0x80)) {
  969. /* Current PMR in the secure range, don't allow NS to change it */
  970. return;
  971. }
  972. value = (value >> 1) | 0x80;
  973. }
  974. value &= icc_fullprio_mask(cs);
  975. cs->icc_pmr_el1 = value;
  976. gicv3_cpuif_update(cs);
  977. }
  978. static void icc_activate_irq(GICv3CPUState *cs, int irq)
  979. {
  980. /* Move the interrupt from the Pending state to Active, and update
  981. * the Active Priority Registers
  982. */
  983. uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
  984. int prio = cs->hppi.prio & mask;
  985. int aprbit = prio >> (8 - cs->prebits);
  986. int regno = aprbit / 32;
  987. int regbit = aprbit % 32;
  988. bool nmi = cs->hppi.nmi;
  989. if (nmi) {
  990. cs->icc_apr[cs->hppi.grp][regno] |= ICC_AP1R_EL1_NMI;
  991. } else {
  992. cs->icc_apr[cs->hppi.grp][regno] |= (1U << regbit);
  993. }
  994. if (irq < GIC_INTERNAL) {
  995. cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
  996. cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
  997. gicv3_redist_update(cs);
  998. } else if (irq < GICV3_LPI_INTID_START) {
  999. gicv3_gicd_active_set(cs->gic, irq);
  1000. gicv3_gicd_pending_clear(cs->gic, irq);
  1001. gicv3_update(cs->gic, irq, 1);
  1002. } else {
  1003. gicv3_redist_lpi_pending(cs, irq, 0);
  1004. }
  1005. }
  1006. static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
  1007. {
  1008. /* Return the highest priority pending interrupt register value
  1009. * for group 0.
  1010. */
  1011. bool irq_is_secure;
  1012. if (icc_no_enabled_hppi(cs)) {
  1013. return INTID_SPURIOUS;
  1014. }
  1015. /* Check whether we can return the interrupt or if we should return
  1016. * a special identifier, as per the CheckGroup0ForSpecialIdentifiers
  1017. * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
  1018. * is always zero.)
  1019. */
  1020. irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
  1021. (cs->hppi.grp != GICV3_G1NS));
  1022. if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
  1023. return INTID_SPURIOUS;
  1024. }
  1025. if (irq_is_secure && !arm_is_secure(env)) {
  1026. /* Secure interrupts not visible to Nonsecure */
  1027. return INTID_SPURIOUS;
  1028. }
  1029. if (cs->hppi.grp != GICV3_G0) {
  1030. /* Indicate to EL3 that there's a Group 1 interrupt for the other
  1031. * state pending.
  1032. */
  1033. return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
  1034. }
  1035. return cs->hppi.irq;
  1036. }
  1037. static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
  1038. {
  1039. /* Return the highest priority pending interrupt register value
  1040. * for group 1.
  1041. */
  1042. bool irq_is_secure;
  1043. if (icc_no_enabled_hppi(cs)) {
  1044. return INTID_SPURIOUS;
  1045. }
  1046. /* Check whether we can return the interrupt or if we should return
  1047. * a special identifier, as per the CheckGroup1ForSpecialIdentifiers
  1048. * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
  1049. * is always zero.)
  1050. */
  1051. irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
  1052. (cs->hppi.grp != GICV3_G1NS));
  1053. if (cs->hppi.grp == GICV3_G0) {
  1054. /* Group 0 interrupts not visible via HPPIR1 */
  1055. return INTID_SPURIOUS;
  1056. }
  1057. if (irq_is_secure) {
  1058. if (!arm_is_secure(env)) {
  1059. /* Secure interrupts not visible in Non-secure */
  1060. return INTID_SPURIOUS;
  1061. }
  1062. } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
  1063. /* Group 1 non-secure interrupts not visible in Secure EL1 */
  1064. return INTID_SPURIOUS;
  1065. }
  1066. return cs->hppi.irq;
  1067. }
  1068. static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1069. {
  1070. GICv3CPUState *cs = icc_cs_from_env(env);
  1071. uint64_t intid;
  1072. if (icv_access(env, HCR_FMO)) {
  1073. return icv_iar_read(env, ri);
  1074. }
  1075. if (!icc_hppi_can_preempt(cs)) {
  1076. intid = INTID_SPURIOUS;
  1077. } else {
  1078. intid = icc_hppir0_value(cs, env);
  1079. }
  1080. if (!gicv3_intid_is_special(intid)) {
  1081. icc_activate_irq(cs, intid);
  1082. }
  1083. trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
  1084. return intid;
  1085. }
  1086. static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1087. {
  1088. GICv3CPUState *cs = icc_cs_from_env(env);
  1089. int el = arm_current_el(env);
  1090. uint64_t intid;
  1091. if (icv_access(env, HCR_IMO)) {
  1092. return icv_iar_read(env, ri);
  1093. }
  1094. if (!icc_hppi_can_preempt(cs)) {
  1095. intid = INTID_SPURIOUS;
  1096. } else {
  1097. intid = icc_hppir1_value(cs, env);
  1098. }
  1099. if (!gicv3_intid_is_special(intid)) {
  1100. if (cs->hppi.nmi && env->cp15.sctlr_el[el] & SCTLR_NMI) {
  1101. intid = INTID_NMI;
  1102. } else {
  1103. icc_activate_irq(cs, intid);
  1104. }
  1105. }
  1106. trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
  1107. return intid;
  1108. }
  1109. static uint64_t icc_nmiar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1110. {
  1111. GICv3CPUState *cs = icc_cs_from_env(env);
  1112. uint64_t intid;
  1113. if (icv_access(env, HCR_IMO)) {
  1114. return icv_nmiar1_read(env, ri);
  1115. }
  1116. if (!icc_hppi_can_preempt(cs)) {
  1117. intid = INTID_SPURIOUS;
  1118. } else {
  1119. intid = icc_hppir1_value(cs, env);
  1120. }
  1121. if (!gicv3_intid_is_special(intid)) {
  1122. if (!cs->hppi.nmi) {
  1123. intid = INTID_SPURIOUS;
  1124. } else {
  1125. icc_activate_irq(cs, intid);
  1126. }
  1127. }
  1128. trace_gicv3_icc_nmiar1_read(gicv3_redist_affid(cs), intid);
  1129. return intid;
  1130. }
  1131. static void icc_drop_prio(GICv3CPUState *cs, int grp)
  1132. {
  1133. /* Drop the priority of the currently active interrupt in
  1134. * the specified group.
  1135. *
  1136. * Note that we can guarantee (because of the requirement to nest
  1137. * ICC_IAR reads [which activate an interrupt and raise priority]
  1138. * with ICC_EOIR writes [which drop the priority for the interrupt])
  1139. * that the interrupt we're being called for is the highest priority
  1140. * active interrupt, meaning that it has the lowest set bit in the
  1141. * APR registers.
  1142. *
  1143. * If the guest does not honour the ordering constraints then the
  1144. * behaviour of the GIC is UNPREDICTABLE, which for us means that
  1145. * the values of the APR registers might become incorrect and the
  1146. * running priority will be wrong, so interrupts that should preempt
  1147. * might not do so, and interrupts that should not preempt might do so.
  1148. */
  1149. int i;
  1150. for (i = 0; i < icc_num_aprs(cs); i++) {
  1151. uint64_t *papr = &cs->icc_apr[grp][i];
  1152. if (!*papr) {
  1153. continue;
  1154. }
  1155. if (i == 0 && cs->nmi_support && (*papr & ICC_AP1R_EL1_NMI)) {
  1156. *papr &= (~ICC_AP1R_EL1_NMI);
  1157. break;
  1158. }
  1159. /* Clear the lowest set bit */
  1160. *papr &= *papr - 1;
  1161. break;
  1162. }
  1163. /* running priority change means we need an update for this cpu i/f */
  1164. gicv3_cpuif_update(cs);
  1165. }
  1166. static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
  1167. {
  1168. /* Return true if we should split priority drop and interrupt
  1169. * deactivation, ie whether the relevant EOIMode bit is set.
  1170. */
  1171. if (arm_is_el3_or_mon(env)) {
  1172. return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
  1173. }
  1174. if (arm_is_secure_below_el3(env)) {
  1175. return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
  1176. } else {
  1177. return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
  1178. }
  1179. }
  1180. static int icc_highest_active_group(GICv3CPUState *cs)
  1181. {
  1182. /* Return the group with the highest priority active interrupt.
  1183. * We can do this by just comparing the APRs to see which one
  1184. * has the lowest set bit.
  1185. * (If more than one group is active at the same priority then
  1186. * we're in UNPREDICTABLE territory.)
  1187. */
  1188. int i;
  1189. if (cs->nmi_support) {
  1190. if (cs->icc_apr[GICV3_G1][0] & ICC_AP1R_EL1_NMI) {
  1191. return GICV3_G1;
  1192. }
  1193. if (cs->icc_apr[GICV3_G1NS][0] & ICC_AP1R_EL1_NMI) {
  1194. return GICV3_G1NS;
  1195. }
  1196. }
  1197. for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
  1198. int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
  1199. int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
  1200. int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
  1201. if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
  1202. return GICV3_G1NS;
  1203. }
  1204. if (g1ctz < g0ctz) {
  1205. return GICV3_G1;
  1206. }
  1207. if (g0ctz < 32) {
  1208. return GICV3_G0;
  1209. }
  1210. }
  1211. /* No set active bits? UNPREDICTABLE; return -1 so the caller
  1212. * ignores the spurious EOI attempt.
  1213. */
  1214. return -1;
  1215. }
  1216. static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
  1217. {
  1218. if (irq < GIC_INTERNAL) {
  1219. cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
  1220. gicv3_redist_update(cs);
  1221. } else {
  1222. gicv3_gicd_active_clear(cs->gic, irq);
  1223. gicv3_update(cs->gic, irq, 1);
  1224. }
  1225. }
  1226. static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs)
  1227. {
  1228. /* Return true if we should split priority drop and interrupt
  1229. * deactivation, ie whether the virtual EOIMode bit is set.
  1230. */
  1231. return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM;
  1232. }
  1233. static int icv_find_active(GICv3CPUState *cs, int irq)
  1234. {
  1235. /* Given an interrupt number for an active interrupt, return the index
  1236. * of the corresponding list register, or -1 if there is no match.
  1237. * Corresponds to FindActiveVirtualInterrupt pseudocode.
  1238. */
  1239. int i;
  1240. for (i = 0; i < cs->num_list_regs; i++) {
  1241. uint64_t lr = cs->ich_lr_el2[i];
  1242. if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) {
  1243. return i;
  1244. }
  1245. }
  1246. return -1;
  1247. }
  1248. static void icv_deactivate_irq(GICv3CPUState *cs, int idx)
  1249. {
  1250. /* Deactivate the interrupt in the specified list register index */
  1251. uint64_t lr = cs->ich_lr_el2[idx];
  1252. if (lr & ICH_LR_EL2_HW) {
  1253. /* Deactivate the associated physical interrupt */
  1254. int pirq = ich_lr_pintid(lr);
  1255. if (pirq < INTID_SECURE) {
  1256. icc_deactivate_irq(cs, pirq);
  1257. }
  1258. }
  1259. /* Clear the 'active' part of the state, so ActivePending->Pending
  1260. * and Active->Invalid.
  1261. */
  1262. lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT;
  1263. cs->ich_lr_el2[idx] = lr;
  1264. }
  1265. static void icv_increment_eoicount(GICv3CPUState *cs)
  1266. {
  1267. /* Increment the EOICOUNT field in ICH_HCR_EL2 */
  1268. int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
  1269. ICH_HCR_EL2_EOICOUNT_LENGTH);
  1270. cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
  1271. ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1);
  1272. }
  1273. static int icv_drop_prio(GICv3CPUState *cs, bool *nmi)
  1274. {
  1275. /* Drop the priority of the currently active virtual interrupt
  1276. * (favouring group 0 if there is a set active bit at
  1277. * the same priority for both group 0 and group 1).
  1278. * Return the priority value for the bit we just cleared,
  1279. * or 0xff if no bits were set in the AP registers at all.
  1280. * Note that though the ich_apr[] are uint64_t only the low
  1281. * 32 bits are actually relevant.
  1282. */
  1283. int i;
  1284. int aprmax = ich_num_aprs(cs);
  1285. for (i = 0; i < aprmax; i++) {
  1286. uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i];
  1287. uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i];
  1288. int apr0count, apr1count;
  1289. if (!*papr0 && !*papr1) {
  1290. continue;
  1291. }
  1292. if (i == 0 && cs->nmi_support && (*papr1 & ICV_AP1R_EL1_NMI)) {
  1293. *papr1 &= (~ICV_AP1R_EL1_NMI);
  1294. *nmi = true;
  1295. return 0xff;
  1296. }
  1297. /* We can't just use the bit-twiddling hack icc_drop_prio() does
  1298. * because we need to return the bit number we cleared so
  1299. * it can be compared against the list register's priority field.
  1300. */
  1301. apr0count = ctz32(*papr0);
  1302. apr1count = ctz32(*papr1);
  1303. if (apr0count <= apr1count) {
  1304. *papr0 &= *papr0 - 1;
  1305. return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1);
  1306. } else {
  1307. *papr1 &= *papr1 - 1;
  1308. return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1);
  1309. }
  1310. }
  1311. return 0xff;
  1312. }
  1313. static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1314. uint64_t value)
  1315. {
  1316. /* Deactivate interrupt */
  1317. GICv3CPUState *cs = icc_cs_from_env(env);
  1318. int idx;
  1319. int irq = value & 0xffffff;
  1320. trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value);
  1321. if (irq >= GICV3_MAXIRQ) {
  1322. /* Also catches special interrupt numbers and LPIs */
  1323. return;
  1324. }
  1325. if (!icv_eoi_split(env, cs)) {
  1326. return;
  1327. }
  1328. idx = icv_find_active(cs, irq);
  1329. if (idx < 0) {
  1330. /* No list register matching this, so increment the EOI count
  1331. * (might trigger a maintenance interrupt)
  1332. */
  1333. icv_increment_eoicount(cs);
  1334. } else {
  1335. icv_deactivate_irq(cs, idx);
  1336. }
  1337. gicv3_cpuif_virt_update(cs);
  1338. }
  1339. static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1340. uint64_t value)
  1341. {
  1342. /* End of Interrupt */
  1343. GICv3CPUState *cs = icc_cs_from_env(env);
  1344. int irq = value & 0xffffff;
  1345. int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
  1346. int idx, dropprio;
  1347. bool nmi = false;
  1348. trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1,
  1349. gicv3_redist_affid(cs), value);
  1350. if (gicv3_intid_is_special(irq)) {
  1351. return;
  1352. }
  1353. /* We implement the IMPDEF choice of "drop priority before doing
  1354. * error checks" (because that lets us avoid scanning the AP
  1355. * registers twice).
  1356. */
  1357. dropprio = icv_drop_prio(cs, &nmi);
  1358. if (dropprio == 0xff && !nmi) {
  1359. /* No active interrupt. It is CONSTRAINED UNPREDICTABLE
  1360. * whether the list registers are checked in this
  1361. * situation; we choose not to.
  1362. */
  1363. return;
  1364. }
  1365. idx = icv_find_active(cs, irq);
  1366. if (idx < 0) {
  1367. /*
  1368. * No valid list register corresponding to EOI ID; if this is a vLPI
  1369. * not in the list regs then do nothing; otherwise increment EOI count
  1370. */
  1371. if (irq < GICV3_LPI_INTID_START) {
  1372. icv_increment_eoicount(cs);
  1373. }
  1374. } else {
  1375. uint64_t lr = cs->ich_lr_el2[idx];
  1376. int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
  1377. int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp);
  1378. bool thisnmi = lr & ICH_LR_EL2_NMI;
  1379. if (thisgrp == grp && (lr_gprio == dropprio || (thisnmi & nmi))) {
  1380. if (!icv_eoi_split(env, cs) || irq >= GICV3_LPI_INTID_START) {
  1381. /*
  1382. * Priority drop and deactivate not split: deactivate irq now.
  1383. * LPIs always get their active state cleared immediately
  1384. * because no separate deactivate is expected.
  1385. */
  1386. icv_deactivate_irq(cs, idx);
  1387. }
  1388. }
  1389. }
  1390. gicv3_cpuif_virt_update(cs);
  1391. }
  1392. static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1393. uint64_t value)
  1394. {
  1395. /* End of Interrupt */
  1396. GICv3CPUState *cs = icc_cs_from_env(env);
  1397. int irq = value & 0xffffff;
  1398. int grp;
  1399. bool is_eoir0 = ri->crm == 8;
  1400. if (icv_access(env, is_eoir0 ? HCR_FMO : HCR_IMO)) {
  1401. icv_eoir_write(env, ri, value);
  1402. return;
  1403. }
  1404. trace_gicv3_icc_eoir_write(is_eoir0 ? 0 : 1,
  1405. gicv3_redist_affid(cs), value);
  1406. if ((irq >= cs->gic->num_irq) &&
  1407. !(cs->gic->lpi_enable && (irq >= GICV3_LPI_INTID_START))) {
  1408. /* This handles two cases:
  1409. * 1. If software writes the ID of a spurious interrupt [ie 1020-1023]
  1410. * to the GICC_EOIR, the GIC ignores that write.
  1411. * 2. If software writes the number of a non-existent interrupt
  1412. * this must be a subcase of "value written does not match the last
  1413. * valid interrupt value read from the Interrupt Acknowledge
  1414. * register" and so this is UNPREDICTABLE. We choose to ignore it.
  1415. */
  1416. return;
  1417. }
  1418. grp = icc_highest_active_group(cs);
  1419. switch (grp) {
  1420. case GICV3_G0:
  1421. if (!is_eoir0) {
  1422. return;
  1423. }
  1424. if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS)
  1425. && arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) {
  1426. return;
  1427. }
  1428. break;
  1429. case GICV3_G1:
  1430. if (is_eoir0) {
  1431. return;
  1432. }
  1433. if (!arm_is_secure(env)) {
  1434. return;
  1435. }
  1436. break;
  1437. case GICV3_G1NS:
  1438. if (is_eoir0) {
  1439. return;
  1440. }
  1441. if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
  1442. return;
  1443. }
  1444. break;
  1445. default:
  1446. qemu_log_mask(LOG_GUEST_ERROR,
  1447. "%s: IRQ %d isn't active\n", __func__, irq);
  1448. return;
  1449. }
  1450. icc_drop_prio(cs, grp);
  1451. if (!icc_eoi_split(env, cs)) {
  1452. /* Priority drop and deactivate not split: deactivate irq now */
  1453. icc_deactivate_irq(cs, irq);
  1454. }
  1455. }
  1456. static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1457. {
  1458. GICv3CPUState *cs = icc_cs_from_env(env);
  1459. uint64_t value;
  1460. if (icv_access(env, HCR_FMO)) {
  1461. return icv_hppir_read(env, ri);
  1462. }
  1463. value = icc_hppir0_value(cs, env);
  1464. trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
  1465. return value;
  1466. }
  1467. static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1468. {
  1469. GICv3CPUState *cs = icc_cs_from_env(env);
  1470. uint64_t value;
  1471. if (icv_access(env, HCR_IMO)) {
  1472. return icv_hppir_read(env, ri);
  1473. }
  1474. value = icc_hppir1_value(cs, env);
  1475. trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
  1476. return value;
  1477. }
  1478. static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1479. {
  1480. GICv3CPUState *cs = icc_cs_from_env(env);
  1481. int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
  1482. bool satinc = false;
  1483. uint64_t bpr;
  1484. if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
  1485. return icv_bpr_read(env, ri);
  1486. }
  1487. if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
  1488. grp = GICV3_G1NS;
  1489. }
  1490. if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
  1491. (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
  1492. /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
  1493. * modify BPR0
  1494. */
  1495. grp = GICV3_G0;
  1496. }
  1497. if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
  1498. (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
  1499. /* reads return bpr0 + 1 sat to 7, writes ignored */
  1500. grp = GICV3_G0;
  1501. satinc = true;
  1502. }
  1503. bpr = cs->icc_bpr[grp];
  1504. if (satinc) {
  1505. bpr++;
  1506. bpr = MIN(bpr, 7);
  1507. }
  1508. trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
  1509. return bpr;
  1510. }
  1511. static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1512. uint64_t value)
  1513. {
  1514. GICv3CPUState *cs = icc_cs_from_env(env);
  1515. int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
  1516. uint64_t minval;
  1517. if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
  1518. icv_bpr_write(env, ri, value);
  1519. return;
  1520. }
  1521. trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
  1522. gicv3_redist_affid(cs), value);
  1523. if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
  1524. grp = GICV3_G1NS;
  1525. }
  1526. if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
  1527. (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
  1528. /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
  1529. * modify BPR0
  1530. */
  1531. grp = GICV3_G0;
  1532. }
  1533. if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
  1534. (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
  1535. /* reads return bpr0 + 1 sat to 7, writes ignored */
  1536. return;
  1537. }
  1538. minval = (grp == GICV3_G1NS) ? icc_min_bpr_ns(cs) : icc_min_bpr(cs);
  1539. if (value < minval) {
  1540. value = minval;
  1541. }
  1542. cs->icc_bpr[grp] = value & 7;
  1543. gicv3_cpuif_update(cs);
  1544. }
  1545. static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1546. {
  1547. GICv3CPUState *cs = icc_cs_from_env(env);
  1548. uint64_t value;
  1549. int regno = ri->opc2 & 3;
  1550. int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
  1551. if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
  1552. return icv_ap_read(env, ri);
  1553. }
  1554. if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
  1555. grp = GICV3_G1NS;
  1556. }
  1557. value = cs->icc_apr[grp][regno];
  1558. trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
  1559. return value;
  1560. }
  1561. static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1562. uint64_t value)
  1563. {
  1564. GICv3CPUState *cs = icc_cs_from_env(env);
  1565. int regno = ri->opc2 & 3;
  1566. int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
  1567. if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
  1568. icv_ap_write(env, ri, value);
  1569. return;
  1570. }
  1571. trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
  1572. if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
  1573. grp = GICV3_G1NS;
  1574. }
  1575. /* It's not possible to claim that a Non-secure interrupt is active
  1576. * at a priority outside the Non-secure range (128..255), since this
  1577. * would otherwise allow malicious NS code to block delivery of S interrupts
  1578. * by writing a bad value to these registers.
  1579. */
  1580. if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
  1581. return;
  1582. }
  1583. if (cs->nmi_support) {
  1584. cs->icc_apr[grp][regno] = value & (0xFFFFFFFFU | ICC_AP1R_EL1_NMI);
  1585. } else {
  1586. cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
  1587. }
  1588. gicv3_cpuif_update(cs);
  1589. }
  1590. static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1591. uint64_t value)
  1592. {
  1593. /* Deactivate interrupt */
  1594. GICv3CPUState *cs = icc_cs_from_env(env);
  1595. int irq = value & 0xffffff;
  1596. bool irq_is_secure, single_sec_state, irq_is_grp0;
  1597. bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
  1598. if (icv_access(env, HCR_FMO | HCR_IMO)) {
  1599. icv_dir_write(env, ri, value);
  1600. return;
  1601. }
  1602. trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
  1603. if (irq >= cs->gic->num_irq) {
  1604. /* Also catches special interrupt numbers and LPIs */
  1605. return;
  1606. }
  1607. if (!icc_eoi_split(env, cs)) {
  1608. return;
  1609. }
  1610. int grp = gicv3_irq_group(cs->gic, cs, irq);
  1611. single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
  1612. irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
  1613. irq_is_grp0 = grp == GICV3_G0;
  1614. /* Check whether we're allowed to deactivate this interrupt based
  1615. * on its group and the current CPU state.
  1616. * These checks are laid out to correspond to the spec's pseudocode.
  1617. */
  1618. route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
  1619. route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
  1620. /* No need to include !IsSecure in route_*_to_el2 as it's only
  1621. * tested in cases where we know !IsSecure is true.
  1622. */
  1623. uint64_t hcr_el2 = arm_hcr_el2_eff(env);
  1624. route_fiq_to_el2 = hcr_el2 & HCR_FMO;
  1625. route_irq_to_el2 = hcr_el2 & HCR_IMO;
  1626. switch (arm_current_el(env)) {
  1627. case 3:
  1628. break;
  1629. case 2:
  1630. if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
  1631. break;
  1632. }
  1633. if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
  1634. break;
  1635. }
  1636. return;
  1637. case 1:
  1638. if (!arm_is_secure_below_el3(env)) {
  1639. if (single_sec_state && irq_is_grp0 &&
  1640. !route_fiq_to_el3 && !route_fiq_to_el2) {
  1641. break;
  1642. }
  1643. if (!irq_is_secure && !irq_is_grp0 &&
  1644. !route_irq_to_el3 && !route_irq_to_el2) {
  1645. break;
  1646. }
  1647. } else {
  1648. if (irq_is_grp0 && !route_fiq_to_el3) {
  1649. break;
  1650. }
  1651. if (!irq_is_grp0 &&
  1652. (!irq_is_secure || !single_sec_state) &&
  1653. !route_irq_to_el3) {
  1654. break;
  1655. }
  1656. }
  1657. return;
  1658. default:
  1659. g_assert_not_reached();
  1660. }
  1661. icc_deactivate_irq(cs, irq);
  1662. }
  1663. static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1664. {
  1665. GICv3CPUState *cs = icc_cs_from_env(env);
  1666. uint64_t prio;
  1667. if (icv_access(env, HCR_FMO | HCR_IMO)) {
  1668. return icv_rpr_read(env, ri);
  1669. }
  1670. prio = icc_highest_active_prio(cs);
  1671. if (arm_feature(env, ARM_FEATURE_EL3) &&
  1672. !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
  1673. /* NS GIC access and Group 0 is inaccessible to NS */
  1674. if ((prio & 0x80) == 0) {
  1675. /* NS mustn't see priorities in the Secure half of the range */
  1676. prio = 0;
  1677. } else if (prio != 0xff) {
  1678. /* Non-idle priority: show the Non-secure view of it */
  1679. prio = (prio << 1) & 0xff;
  1680. }
  1681. }
  1682. if (cs->nmi_support) {
  1683. /* NMI info is reported in the high bits of RPR */
  1684. if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) {
  1685. if (cs->icc_apr[GICV3_G1NS][0] & ICC_AP1R_EL1_NMI) {
  1686. prio |= ICC_RPR_EL1_NMI;
  1687. }
  1688. } else {
  1689. if (cs->icc_apr[GICV3_G1NS][0] & ICC_AP1R_EL1_NMI) {
  1690. prio |= ICC_RPR_EL1_NSNMI;
  1691. }
  1692. if (cs->icc_apr[GICV3_G1][0] & ICC_AP1R_EL1_NMI) {
  1693. prio |= ICC_RPR_EL1_NMI;
  1694. }
  1695. }
  1696. }
  1697. trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
  1698. return prio;
  1699. }
  1700. static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
  1701. uint64_t value, int grp, bool ns)
  1702. {
  1703. GICv3State *s = cs->gic;
  1704. /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
  1705. uint64_t aff = extract64(value, 48, 8) << 16 |
  1706. extract64(value, 32, 8) << 8 |
  1707. extract64(value, 16, 8);
  1708. uint32_t targetlist = extract64(value, 0, 16);
  1709. uint32_t irq = extract64(value, 24, 4);
  1710. bool irm = extract64(value, 40, 1);
  1711. int i;
  1712. if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
  1713. /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
  1714. * interrupts as Group 0 interrupts and must send Secure Group 0
  1715. * interrupts to the target CPUs.
  1716. */
  1717. grp = GICV3_G0;
  1718. }
  1719. trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
  1720. aff, targetlist);
  1721. for (i = 0; i < s->num_cpu; i++) {
  1722. GICv3CPUState *ocs = &s->cpu[i];
  1723. if (irm) {
  1724. /* IRM == 1 : route to all CPUs except self */
  1725. if (cs == ocs) {
  1726. continue;
  1727. }
  1728. } else {
  1729. /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
  1730. * where the corresponding bit is set in targetlist
  1731. */
  1732. int aff0;
  1733. if (ocs->gicr_typer >> 40 != aff) {
  1734. continue;
  1735. }
  1736. aff0 = extract64(ocs->gicr_typer, 32, 8);
  1737. if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
  1738. continue;
  1739. }
  1740. }
  1741. /* The redistributor will check against its own GICR_NSACR as needed */
  1742. gicv3_redist_send_sgi(ocs, grp, irq, ns);
  1743. }
  1744. }
  1745. static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1746. uint64_t value)
  1747. {
  1748. /* Generate Secure Group 0 SGI. */
  1749. GICv3CPUState *cs = icc_cs_from_env(env);
  1750. bool ns = !arm_is_secure(env);
  1751. icc_generate_sgi(env, cs, value, GICV3_G0, ns);
  1752. }
  1753. static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1754. uint64_t value)
  1755. {
  1756. /* Generate Group 1 SGI for the current Security state */
  1757. GICv3CPUState *cs = icc_cs_from_env(env);
  1758. int grp;
  1759. bool ns = !arm_is_secure(env);
  1760. grp = ns ? GICV3_G1NS : GICV3_G1;
  1761. icc_generate_sgi(env, cs, value, grp, ns);
  1762. }
  1763. static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1764. uint64_t value)
  1765. {
  1766. /* Generate Group 1 SGI for the Security state that is not
  1767. * the current state
  1768. */
  1769. GICv3CPUState *cs = icc_cs_from_env(env);
  1770. int grp;
  1771. bool ns = !arm_is_secure(env);
  1772. grp = ns ? GICV3_G1 : GICV3_G1NS;
  1773. icc_generate_sgi(env, cs, value, grp, ns);
  1774. }
  1775. static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1776. {
  1777. GICv3CPUState *cs = icc_cs_from_env(env);
  1778. int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
  1779. uint64_t value;
  1780. if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
  1781. return icv_igrpen_read(env, ri);
  1782. }
  1783. if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
  1784. grp = GICV3_G1NS;
  1785. }
  1786. value = cs->icc_igrpen[grp];
  1787. trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
  1788. gicv3_redist_affid(cs), value);
  1789. return value;
  1790. }
  1791. static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1792. uint64_t value)
  1793. {
  1794. GICv3CPUState *cs = icc_cs_from_env(env);
  1795. int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
  1796. if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
  1797. icv_igrpen_write(env, ri, value);
  1798. return;
  1799. }
  1800. trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
  1801. gicv3_redist_affid(cs), value);
  1802. if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
  1803. grp = GICV3_G1NS;
  1804. }
  1805. cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
  1806. gicv3_cpuif_update(cs);
  1807. }
  1808. static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1809. {
  1810. GICv3CPUState *cs = icc_cs_from_env(env);
  1811. uint64_t value;
  1812. /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
  1813. value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
  1814. trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
  1815. return value;
  1816. }
  1817. static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1818. uint64_t value)
  1819. {
  1820. GICv3CPUState *cs = icc_cs_from_env(env);
  1821. trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
  1822. /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
  1823. cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
  1824. cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
  1825. gicv3_cpuif_update(cs);
  1826. }
  1827. static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1828. {
  1829. GICv3CPUState *cs = icc_cs_from_env(env);
  1830. int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
  1831. uint64_t value;
  1832. if (icv_access(env, HCR_FMO | HCR_IMO)) {
  1833. return icv_ctlr_read(env, ri);
  1834. }
  1835. value = cs->icc_ctlr_el1[bank];
  1836. trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
  1837. return value;
  1838. }
  1839. static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1840. uint64_t value)
  1841. {
  1842. GICv3CPUState *cs = icc_cs_from_env(env);
  1843. int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
  1844. uint64_t mask;
  1845. if (icv_access(env, HCR_FMO | HCR_IMO)) {
  1846. icv_ctlr_write(env, ri, value);
  1847. return;
  1848. }
  1849. trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
  1850. /* Only CBPR and EOIMODE can be RW;
  1851. * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
  1852. * the asseciated priority-based routing of them);
  1853. * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
  1854. */
  1855. if (arm_feature(env, ARM_FEATURE_EL3) &&
  1856. ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
  1857. mask = ICC_CTLR_EL1_EOIMODE;
  1858. } else {
  1859. mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
  1860. }
  1861. cs->icc_ctlr_el1[bank] &= ~mask;
  1862. cs->icc_ctlr_el1[bank] |= (value & mask);
  1863. gicv3_cpuif_update(cs);
  1864. }
  1865. static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
  1866. {
  1867. GICv3CPUState *cs = icc_cs_from_env(env);
  1868. uint64_t value;
  1869. value = cs->icc_ctlr_el3;
  1870. if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
  1871. value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
  1872. }
  1873. if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
  1874. value |= ICC_CTLR_EL3_CBPR_EL1NS;
  1875. }
  1876. if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
  1877. value |= ICC_CTLR_EL3_EOIMODE_EL1S;
  1878. }
  1879. if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
  1880. value |= ICC_CTLR_EL3_CBPR_EL1S;
  1881. }
  1882. trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
  1883. return value;
  1884. }
  1885. static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
  1886. uint64_t value)
  1887. {
  1888. GICv3CPUState *cs = icc_cs_from_env(env);
  1889. uint64_t mask;
  1890. trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
  1891. /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
  1892. cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
  1893. if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
  1894. cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
  1895. }
  1896. if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
  1897. cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
  1898. }
  1899. cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
  1900. if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
  1901. cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
  1902. }
  1903. if (value & ICC_CTLR_EL3_CBPR_EL1S) {
  1904. cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
  1905. }
  1906. /* The only bit stored in icc_ctlr_el3 which is writable is EOIMODE_EL3: */
  1907. mask = ICC_CTLR_EL3_EOIMODE_EL3;
  1908. cs->icc_ctlr_el3 &= ~mask;
  1909. cs->icc_ctlr_el3 |= (value & mask);
  1910. gicv3_cpuif_update(cs);
  1911. }
  1912. static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
  1913. const ARMCPRegInfo *ri, bool isread)
  1914. {
  1915. CPAccessResult r = CP_ACCESS_OK;
  1916. GICv3CPUState *cs = icc_cs_from_env(env);
  1917. int el = arm_current_el(env);
  1918. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
  1919. el == 1 && !arm_is_secure_below_el3(env)) {
  1920. /* Takes priority over a possible EL3 trap */
  1921. return CP_ACCESS_TRAP_EL2;
  1922. }
  1923. if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
  1924. switch (el) {
  1925. case 1:
  1926. /* Note that arm_hcr_el2_eff takes secure state into account. */
  1927. if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) {
  1928. r = CP_ACCESS_TRAP_EL3;
  1929. }
  1930. break;
  1931. case 2:
  1932. r = CP_ACCESS_TRAP_EL3;
  1933. break;
  1934. case 3:
  1935. if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
  1936. r = CP_ACCESS_TRAP_EL3;
  1937. }
  1938. break;
  1939. default:
  1940. g_assert_not_reached();
  1941. }
  1942. }
  1943. if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
  1944. r = CP_ACCESS_TRAP;
  1945. }
  1946. return r;
  1947. }
  1948. static CPAccessResult gicv3_dir_access(CPUARMState *env,
  1949. const ARMCPRegInfo *ri, bool isread)
  1950. {
  1951. GICv3CPUState *cs = icc_cs_from_env(env);
  1952. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
  1953. arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
  1954. /* Takes priority over a possible EL3 trap */
  1955. return CP_ACCESS_TRAP_EL2;
  1956. }
  1957. return gicv3_irqfiq_access(env, ri, isread);
  1958. }
  1959. static CPAccessResult gicv3_sgi_access(CPUARMState *env,
  1960. const ARMCPRegInfo *ri, bool isread)
  1961. {
  1962. if (arm_current_el(env) == 1 &&
  1963. (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) {
  1964. /* Takes priority over a possible EL3 trap */
  1965. return CP_ACCESS_TRAP_EL2;
  1966. }
  1967. return gicv3_irqfiq_access(env, ri, isread);
  1968. }
  1969. static CPAccessResult gicv3_fiq_access(CPUARMState *env,
  1970. const ARMCPRegInfo *ri, bool isread)
  1971. {
  1972. CPAccessResult r = CP_ACCESS_OK;
  1973. GICv3CPUState *cs = icc_cs_from_env(env);
  1974. int el = arm_current_el(env);
  1975. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
  1976. el == 1 && !arm_is_secure_below_el3(env)) {
  1977. /* Takes priority over a possible EL3 trap */
  1978. return CP_ACCESS_TRAP_EL2;
  1979. }
  1980. if (env->cp15.scr_el3 & SCR_FIQ) {
  1981. switch (el) {
  1982. case 1:
  1983. if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) {
  1984. r = CP_ACCESS_TRAP_EL3;
  1985. }
  1986. break;
  1987. case 2:
  1988. r = CP_ACCESS_TRAP_EL3;
  1989. break;
  1990. case 3:
  1991. if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
  1992. r = CP_ACCESS_TRAP_EL3;
  1993. }
  1994. break;
  1995. default:
  1996. g_assert_not_reached();
  1997. }
  1998. }
  1999. if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
  2000. r = CP_ACCESS_TRAP;
  2001. }
  2002. return r;
  2003. }
  2004. static CPAccessResult gicv3_irq_access(CPUARMState *env,
  2005. const ARMCPRegInfo *ri, bool isread)
  2006. {
  2007. CPAccessResult r = CP_ACCESS_OK;
  2008. GICv3CPUState *cs = icc_cs_from_env(env);
  2009. int el = arm_current_el(env);
  2010. if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
  2011. el == 1 && !arm_is_secure_below_el3(env)) {
  2012. /* Takes priority over a possible EL3 trap */
  2013. return CP_ACCESS_TRAP_EL2;
  2014. }
  2015. if (env->cp15.scr_el3 & SCR_IRQ) {
  2016. switch (el) {
  2017. case 1:
  2018. if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) {
  2019. r = CP_ACCESS_TRAP_EL3;
  2020. }
  2021. break;
  2022. case 2:
  2023. r = CP_ACCESS_TRAP_EL3;
  2024. break;
  2025. case 3:
  2026. if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
  2027. r = CP_ACCESS_TRAP_EL3;
  2028. }
  2029. break;
  2030. default:
  2031. g_assert_not_reached();
  2032. }
  2033. }
  2034. if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
  2035. r = CP_ACCESS_TRAP;
  2036. }
  2037. return r;
  2038. }
  2039. static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  2040. {
  2041. GICv3CPUState *cs = icc_cs_from_env(env);
  2042. cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
  2043. (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
  2044. ((cs->pribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
  2045. cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
  2046. (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
  2047. ((cs->pribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
  2048. cs->icc_pmr_el1 = 0;
  2049. cs->icc_bpr[GICV3_G0] = icc_min_bpr(cs);
  2050. cs->icc_bpr[GICV3_G1] = icc_min_bpr(cs);
  2051. cs->icc_bpr[GICV3_G1NS] = icc_min_bpr_ns(cs);
  2052. memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
  2053. memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
  2054. cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
  2055. (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
  2056. ((cs->pribits - 1) << ICC_CTLR_EL3_PRIBITS_SHIFT);
  2057. memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
  2058. cs->ich_hcr_el2 = 0;
  2059. memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
  2060. cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
  2061. ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
  2062. (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
  2063. }
  2064. static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
  2065. { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
  2066. .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
  2067. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2068. .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
  2069. .readfn = icc_pmr_read,
  2070. .writefn = icc_pmr_write,
  2071. /* We hang the whole cpu interface reset routine off here
  2072. * rather than parcelling it out into one little function
  2073. * per register
  2074. */
  2075. .resetfn = icc_reset,
  2076. },
  2077. { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
  2078. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
  2079. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2080. .access = PL1_R, .accessfn = gicv3_fiq_access,
  2081. .readfn = icc_iar0_read,
  2082. },
  2083. { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
  2084. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
  2085. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2086. .access = PL1_W, .accessfn = gicv3_fiq_access,
  2087. .writefn = icc_eoir_write,
  2088. },
  2089. { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
  2090. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
  2091. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2092. .access = PL1_R, .accessfn = gicv3_fiq_access,
  2093. .readfn = icc_hppir0_read,
  2094. },
  2095. { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
  2096. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
  2097. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2098. .access = PL1_RW, .accessfn = gicv3_fiq_access,
  2099. .readfn = icc_bpr_read,
  2100. .writefn = icc_bpr_write,
  2101. },
  2102. { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
  2103. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
  2104. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2105. .access = PL1_RW, .accessfn = gicv3_fiq_access,
  2106. .readfn = icc_ap_read,
  2107. .writefn = icc_ap_write,
  2108. },
  2109. /* All the ICC_AP1R*_EL1 registers are banked */
  2110. { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
  2111. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
  2112. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2113. .access = PL1_RW, .accessfn = gicv3_irq_access,
  2114. .readfn = icc_ap_read,
  2115. .writefn = icc_ap_write,
  2116. },
  2117. { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
  2118. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
  2119. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2120. .access = PL1_W, .accessfn = gicv3_dir_access,
  2121. .writefn = icc_dir_write,
  2122. },
  2123. { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
  2124. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
  2125. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2126. .access = PL1_R, .accessfn = gicv3_irqfiq_access,
  2127. .readfn = icc_rpr_read,
  2128. },
  2129. { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
  2130. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
  2131. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2132. .access = PL1_W, .accessfn = gicv3_sgi_access,
  2133. .writefn = icc_sgi1r_write,
  2134. },
  2135. { .name = "ICC_SGI1R",
  2136. .cp = 15, .opc1 = 0, .crm = 12,
  2137. .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
  2138. .access = PL1_W, .accessfn = gicv3_sgi_access,
  2139. .writefn = icc_sgi1r_write,
  2140. },
  2141. { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
  2142. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
  2143. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2144. .access = PL1_W, .accessfn = gicv3_sgi_access,
  2145. .writefn = icc_asgi1r_write,
  2146. },
  2147. { .name = "ICC_ASGI1R",
  2148. .cp = 15, .opc1 = 1, .crm = 12,
  2149. .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
  2150. .access = PL1_W, .accessfn = gicv3_sgi_access,
  2151. .writefn = icc_asgi1r_write,
  2152. },
  2153. { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
  2154. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
  2155. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2156. .access = PL1_W, .accessfn = gicv3_sgi_access,
  2157. .writefn = icc_sgi0r_write,
  2158. },
  2159. { .name = "ICC_SGI0R",
  2160. .cp = 15, .opc1 = 2, .crm = 12,
  2161. .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
  2162. .access = PL1_W, .accessfn = gicv3_sgi_access,
  2163. .writefn = icc_sgi0r_write,
  2164. },
  2165. { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
  2166. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
  2167. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2168. .access = PL1_R, .accessfn = gicv3_irq_access,
  2169. .readfn = icc_iar1_read,
  2170. },
  2171. { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
  2172. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
  2173. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2174. .access = PL1_W, .accessfn = gicv3_irq_access,
  2175. .writefn = icc_eoir_write,
  2176. },
  2177. { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
  2178. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
  2179. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2180. .access = PL1_R, .accessfn = gicv3_irq_access,
  2181. .readfn = icc_hppir1_read,
  2182. },
  2183. /* This register is banked */
  2184. { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
  2185. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
  2186. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2187. .access = PL1_RW, .accessfn = gicv3_irq_access,
  2188. .readfn = icc_bpr_read,
  2189. .writefn = icc_bpr_write,
  2190. },
  2191. /* This register is banked */
  2192. { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
  2193. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
  2194. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2195. .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
  2196. .readfn = icc_ctlr_el1_read,
  2197. .writefn = icc_ctlr_el1_write,
  2198. },
  2199. { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
  2200. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
  2201. .type = ARM_CP_NO_RAW | ARM_CP_CONST,
  2202. .access = PL1_RW,
  2203. /* We don't support IRQ/FIQ bypass and system registers are
  2204. * always enabled, so all our bits are RAZ/WI or RAO/WI.
  2205. * This register is banked but since it's constant we don't
  2206. * need to do anything special.
  2207. */
  2208. .resetvalue = 0x7,
  2209. },
  2210. { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
  2211. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
  2212. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2213. .access = PL1_RW, .accessfn = gicv3_fiq_access,
  2214. .fgt = FGT_ICC_IGRPENN_EL1,
  2215. .readfn = icc_igrpen_read,
  2216. .writefn = icc_igrpen_write,
  2217. },
  2218. /* This register is banked */
  2219. { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
  2220. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
  2221. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2222. .access = PL1_RW, .accessfn = gicv3_irq_access,
  2223. .fgt = FGT_ICC_IGRPENN_EL1,
  2224. .readfn = icc_igrpen_read,
  2225. .writefn = icc_igrpen_write,
  2226. },
  2227. { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
  2228. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
  2229. .type = ARM_CP_NO_RAW | ARM_CP_CONST,
  2230. .access = PL2_RW,
  2231. /* We don't support IRQ/FIQ bypass and system registers are
  2232. * always enabled, so all our bits are RAZ/WI or RAO/WI.
  2233. */
  2234. .resetvalue = 0xf,
  2235. },
  2236. { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
  2237. .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
  2238. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2239. .access = PL3_RW,
  2240. .readfn = icc_ctlr_el3_read,
  2241. .writefn = icc_ctlr_el3_write,
  2242. },
  2243. { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
  2244. .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
  2245. .type = ARM_CP_NO_RAW | ARM_CP_CONST,
  2246. .access = PL3_RW,
  2247. /* We don't support IRQ/FIQ bypass and system registers are
  2248. * always enabled, so all our bits are RAZ/WI or RAO/WI.
  2249. */
  2250. .resetvalue = 0xf,
  2251. },
  2252. { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
  2253. .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
  2254. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2255. .access = PL3_RW,
  2256. .readfn = icc_igrpen1_el3_read,
  2257. .writefn = icc_igrpen1_el3_write,
  2258. },
  2259. };
  2260. static const ARMCPRegInfo gicv3_cpuif_icc_apxr1_reginfo[] = {
  2261. { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
  2262. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
  2263. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2264. .access = PL1_RW, .accessfn = gicv3_fiq_access,
  2265. .readfn = icc_ap_read,
  2266. .writefn = icc_ap_write,
  2267. },
  2268. { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
  2269. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
  2270. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2271. .access = PL1_RW, .accessfn = gicv3_irq_access,
  2272. .readfn = icc_ap_read,
  2273. .writefn = icc_ap_write,
  2274. },
  2275. };
  2276. static const ARMCPRegInfo gicv3_cpuif_icc_apxr23_reginfo[] = {
  2277. { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
  2278. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
  2279. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2280. .access = PL1_RW, .accessfn = gicv3_fiq_access,
  2281. .readfn = icc_ap_read,
  2282. .writefn = icc_ap_write,
  2283. },
  2284. { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
  2285. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
  2286. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2287. .access = PL1_RW, .accessfn = gicv3_fiq_access,
  2288. .readfn = icc_ap_read,
  2289. .writefn = icc_ap_write,
  2290. },
  2291. { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
  2292. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
  2293. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2294. .access = PL1_RW, .accessfn = gicv3_irq_access,
  2295. .readfn = icc_ap_read,
  2296. .writefn = icc_ap_write,
  2297. },
  2298. { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
  2299. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
  2300. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2301. .access = PL1_RW, .accessfn = gicv3_irq_access,
  2302. .readfn = icc_ap_read,
  2303. .writefn = icc_ap_write,
  2304. },
  2305. };
  2306. static const ARMCPRegInfo gicv3_cpuif_gicv3_nmi_reginfo[] = {
  2307. { .name = "ICC_NMIAR1_EL1", .state = ARM_CP_STATE_BOTH,
  2308. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 5,
  2309. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2310. .access = PL1_R, .accessfn = gicv3_irq_access,
  2311. .readfn = icc_nmiar1_read,
  2312. },
  2313. };
  2314. static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2315. {
  2316. GICv3CPUState *cs = icc_cs_from_env(env);
  2317. int regno = ri->opc2 & 3;
  2318. int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
  2319. uint64_t value;
  2320. value = cs->ich_apr[grp][regno];
  2321. trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
  2322. return value;
  2323. }
  2324. static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2325. uint64_t value)
  2326. {
  2327. GICv3CPUState *cs = icc_cs_from_env(env);
  2328. int regno = ri->opc2 & 3;
  2329. int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
  2330. trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
  2331. if (cs->nmi_support) {
  2332. cs->ich_apr[grp][regno] = value & (0xFFFFFFFFU | ICV_AP1R_EL1_NMI);
  2333. } else {
  2334. cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
  2335. }
  2336. gicv3_cpuif_virt_irq_fiq_update(cs);
  2337. }
  2338. static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2339. {
  2340. GICv3CPUState *cs = icc_cs_from_env(env);
  2341. uint64_t value = cs->ich_hcr_el2;
  2342. trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
  2343. return value;
  2344. }
  2345. static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2346. uint64_t value)
  2347. {
  2348. GICv3CPUState *cs = icc_cs_from_env(env);
  2349. trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
  2350. value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
  2351. ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
  2352. ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
  2353. ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
  2354. ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
  2355. cs->ich_hcr_el2 = value;
  2356. gicv3_cpuif_virt_update(cs);
  2357. }
  2358. static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2359. {
  2360. GICv3CPUState *cs = icc_cs_from_env(env);
  2361. uint64_t value = cs->ich_vmcr_el2;
  2362. trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
  2363. return value;
  2364. }
  2365. static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2366. uint64_t value)
  2367. {
  2368. GICv3CPUState *cs = icc_cs_from_env(env);
  2369. trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
  2370. value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
  2371. ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
  2372. ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
  2373. value |= ICH_VMCR_EL2_VFIQEN;
  2374. cs->ich_vmcr_el2 = value;
  2375. /* Enforce "writing BPRs to less than minimum sets them to the minimum"
  2376. * by reading and writing back the fields.
  2377. */
  2378. write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0));
  2379. write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
  2380. gicv3_cpuif_virt_update(cs);
  2381. }
  2382. static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2383. {
  2384. GICv3CPUState *cs = icc_cs_from_env(env);
  2385. int regno = ri->opc2 | ((ri->crm & 1) << 3);
  2386. uint64_t value;
  2387. /* This read function handles all of:
  2388. * 64-bit reads of the whole LR
  2389. * 32-bit reads of the low half of the LR
  2390. * 32-bit reads of the high half of the LR
  2391. */
  2392. if (ri->state == ARM_CP_STATE_AA32) {
  2393. if (ri->crm >= 14) {
  2394. value = extract64(cs->ich_lr_el2[regno], 32, 32);
  2395. trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
  2396. } else {
  2397. value = extract64(cs->ich_lr_el2[regno], 0, 32);
  2398. trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
  2399. }
  2400. } else {
  2401. value = cs->ich_lr_el2[regno];
  2402. trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
  2403. }
  2404. return value;
  2405. }
  2406. static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
  2407. uint64_t value)
  2408. {
  2409. GICv3CPUState *cs = icc_cs_from_env(env);
  2410. int regno = ri->opc2 | ((ri->crm & 1) << 3);
  2411. /* This write function handles all of:
  2412. * 64-bit writes to the whole LR
  2413. * 32-bit writes to the low half of the LR
  2414. * 32-bit writes to the high half of the LR
  2415. */
  2416. if (ri->state == ARM_CP_STATE_AA32) {
  2417. if (ri->crm >= 14) {
  2418. trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
  2419. value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
  2420. } else {
  2421. trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
  2422. value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
  2423. }
  2424. } else {
  2425. trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
  2426. }
  2427. /* Enforce RES0 bits in priority field */
  2428. if (cs->vpribits < 8) {
  2429. value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
  2430. 8 - cs->vpribits, 0);
  2431. }
  2432. /* Enforce RES0 bit in NMI field when FEAT_GICv3_NMI is not implemented */
  2433. if (!cs->nmi_support) {
  2434. value &= ~ICH_LR_EL2_NMI;
  2435. }
  2436. cs->ich_lr_el2[regno] = value;
  2437. gicv3_cpuif_virt_update(cs);
  2438. }
  2439. static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2440. {
  2441. GICv3CPUState *cs = icc_cs_from_env(env);
  2442. uint64_t value;
  2443. value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
  2444. | ICH_VTR_EL2_TDS | ICH_VTR_EL2_A3V
  2445. | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
  2446. | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
  2447. | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
  2448. if (cs->gic->revision < 4) {
  2449. value |= ICH_VTR_EL2_NV4;
  2450. }
  2451. trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
  2452. return value;
  2453. }
  2454. static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2455. {
  2456. GICv3CPUState *cs = icc_cs_from_env(env);
  2457. uint64_t value = maintenance_interrupt_state(cs);
  2458. trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
  2459. return value;
  2460. }
  2461. static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2462. {
  2463. GICv3CPUState *cs = icc_cs_from_env(env);
  2464. uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
  2465. trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
  2466. return value;
  2467. }
  2468. static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
  2469. {
  2470. GICv3CPUState *cs = icc_cs_from_env(env);
  2471. uint64_t value = 0;
  2472. int i;
  2473. for (i = 0; i < cs->num_list_regs; i++) {
  2474. uint64_t lr = cs->ich_lr_el2[i];
  2475. if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
  2476. ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
  2477. value |= (1 << i);
  2478. }
  2479. }
  2480. trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
  2481. return value;
  2482. }
  2483. static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
  2484. { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
  2485. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
  2486. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2487. .nv2_redirect_offset = 0x480,
  2488. .access = PL2_RW,
  2489. .readfn = ich_ap_read,
  2490. .writefn = ich_ap_write,
  2491. },
  2492. { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
  2493. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
  2494. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2495. .nv2_redirect_offset = 0x4a0,
  2496. .access = PL2_RW,
  2497. .readfn = ich_ap_read,
  2498. .writefn = ich_ap_write,
  2499. },
  2500. { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
  2501. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
  2502. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2503. .nv2_redirect_offset = 0x4c0,
  2504. .access = PL2_RW,
  2505. .readfn = ich_hcr_read,
  2506. .writefn = ich_hcr_write,
  2507. },
  2508. { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
  2509. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
  2510. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2511. .access = PL2_R,
  2512. .readfn = ich_vtr_read,
  2513. },
  2514. { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
  2515. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
  2516. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2517. .access = PL2_R,
  2518. .readfn = ich_misr_read,
  2519. },
  2520. { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
  2521. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
  2522. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2523. .access = PL2_R,
  2524. .readfn = ich_eisr_read,
  2525. },
  2526. { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
  2527. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
  2528. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2529. .access = PL2_R,
  2530. .readfn = ich_elrsr_read,
  2531. },
  2532. { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
  2533. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
  2534. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2535. .nv2_redirect_offset = 0x4c8,
  2536. .access = PL2_RW,
  2537. .readfn = ich_vmcr_read,
  2538. .writefn = ich_vmcr_write,
  2539. },
  2540. };
  2541. static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
  2542. { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
  2543. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
  2544. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2545. .nv2_redirect_offset = 0x488,
  2546. .access = PL2_RW,
  2547. .readfn = ich_ap_read,
  2548. .writefn = ich_ap_write,
  2549. },
  2550. { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
  2551. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
  2552. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2553. .nv2_redirect_offset = 0x4a8,
  2554. .access = PL2_RW,
  2555. .readfn = ich_ap_read,
  2556. .writefn = ich_ap_write,
  2557. },
  2558. };
  2559. static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
  2560. { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
  2561. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
  2562. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2563. .nv2_redirect_offset = 0x490,
  2564. .access = PL2_RW,
  2565. .readfn = ich_ap_read,
  2566. .writefn = ich_ap_write,
  2567. },
  2568. { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
  2569. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
  2570. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2571. .nv2_redirect_offset = 0x498,
  2572. .access = PL2_RW,
  2573. .readfn = ich_ap_read,
  2574. .writefn = ich_ap_write,
  2575. },
  2576. { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
  2577. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
  2578. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2579. .nv2_redirect_offset = 0x4b0,
  2580. .access = PL2_RW,
  2581. .readfn = ich_ap_read,
  2582. .writefn = ich_ap_write,
  2583. },
  2584. { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
  2585. .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
  2586. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2587. .nv2_redirect_offset = 0x4b8,
  2588. .access = PL2_RW,
  2589. .readfn = ich_ap_read,
  2590. .writefn = ich_ap_write,
  2591. },
  2592. };
  2593. static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
  2594. {
  2595. GICv3CPUState *cs = opaque;
  2596. gicv3_cpuif_update(cs);
  2597. /*
  2598. * Because vLPIs are only pending in NonSecure state,
  2599. * an EL change can change the VIRQ/VFIQ status (but
  2600. * cannot affect the maintenance interrupt state)
  2601. */
  2602. gicv3_cpuif_virt_irq_fiq_update(cs);
  2603. }
  2604. void gicv3_init_cpuif(GICv3State *s)
  2605. {
  2606. /* Called from the GICv3 realize function; register our system
  2607. * registers with the CPU
  2608. */
  2609. int i;
  2610. for (i = 0; i < s->num_cpu; i++) {
  2611. ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
  2612. GICv3CPUState *cs = &s->cpu[i];
  2613. /*
  2614. * If the CPU doesn't define a GICv3 configuration, probably because
  2615. * in real hardware it doesn't have one, then we use default values
  2616. * matching the one used by most Arm CPUs. This applies to:
  2617. * cpu->gic_num_lrs
  2618. * cpu->gic_vpribits
  2619. * cpu->gic_vprebits
  2620. * cpu->gic_pribits
  2621. */
  2622. /* Note that we can't just use the GICv3CPUState as an opaque pointer
  2623. * in define_arm_cp_regs_with_opaque(), because when we're called back
  2624. * it might be with code translated by CPU 0 but run by CPU 1, in
  2625. * which case we'd get the wrong value.
  2626. * So instead we define the regs with no ri->opaque info, and
  2627. * get back to the GICv3CPUState from the CPUARMState.
  2628. *
  2629. * These CP regs callbacks can be called from either TCG or HVF code.
  2630. */
  2631. define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
  2632. /*
  2633. * If the CPU implements FEAT_NMI and FEAT_GICv3 it must also
  2634. * implement FEAT_GICv3_NMI, which is the CPU interface part
  2635. * of NMI support. This is distinct from whether the GIC proper
  2636. * (redistributors and distributor) have NMI support. In QEMU
  2637. * that is a property of the GIC device in s->nmi_support;
  2638. * cs->nmi_support indicates the CPU interface's support.
  2639. */
  2640. if (cpu_isar_feature(aa64_nmi, cpu)) {
  2641. cs->nmi_support = true;
  2642. define_arm_cp_regs(cpu, gicv3_cpuif_gicv3_nmi_reginfo);
  2643. }
  2644. /*
  2645. * The CPU implementation specifies the number of supported
  2646. * bits of physical priority. For backwards compatibility
  2647. * of migration, we have a compat property that forces use
  2648. * of 8 priority bits regardless of what the CPU really has.
  2649. */
  2650. if (s->force_8bit_prio) {
  2651. cs->pribits = 8;
  2652. } else {
  2653. cs->pribits = cpu->gic_pribits ?: 5;
  2654. }
  2655. /*
  2656. * The GICv3 has separate ID register fields for virtual priority
  2657. * and preemption bit values, but only a single ID register field
  2658. * for the physical priority bits. The preemption bit count is
  2659. * always the same as the priority bit count, except that 8 bits
  2660. * of priority means 7 preemption bits. We precalculate the
  2661. * preemption bits because it simplifies the code and makes the
  2662. * parallels between the virtual and physical bits of the GIC
  2663. * a bit clearer.
  2664. */
  2665. cs->prebits = cs->pribits;
  2666. if (cs->prebits == 8) {
  2667. cs->prebits--;
  2668. }
  2669. /*
  2670. * Check that CPU code defining pribits didn't violate
  2671. * architectural constraints our implementation relies on.
  2672. */
  2673. g_assert(cs->pribits >= 4 && cs->pribits <= 8);
  2674. /*
  2675. * gicv3_cpuif_reginfo[] defines ICC_AP*R0_EL1; add definitions
  2676. * for ICC_AP*R{1,2,3}_EL1 if the prebits value requires them.
  2677. */
  2678. if (cs->prebits >= 6) {
  2679. define_arm_cp_regs(cpu, gicv3_cpuif_icc_apxr1_reginfo);
  2680. }
  2681. if (cs->prebits == 7) {
  2682. define_arm_cp_regs(cpu, gicv3_cpuif_icc_apxr23_reginfo);
  2683. }
  2684. if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
  2685. int j;
  2686. cs->num_list_regs = cpu->gic_num_lrs ?: 4;
  2687. cs->vpribits = cpu->gic_vpribits ?: 5;
  2688. cs->vprebits = cpu->gic_vprebits ?: 5;
  2689. /* Check against architectural constraints: getting these
  2690. * wrong would be a bug in the CPU code defining these,
  2691. * and the implementation relies on them holding.
  2692. */
  2693. g_assert(cs->vprebits <= cs->vpribits);
  2694. g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
  2695. g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
  2696. define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
  2697. for (j = 0; j < cs->num_list_regs; j++) {
  2698. /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs
  2699. * are split into two cp15 regs, LR (the low part, with the
  2700. * same encoding as the AArch64 LR) and LRC (the high part).
  2701. */
  2702. ARMCPRegInfo lr_regset[] = {
  2703. { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
  2704. .opc0 = 3, .opc1 = 4, .crn = 12,
  2705. .crm = 12 + (j >> 3), .opc2 = j & 7,
  2706. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2707. .nv2_redirect_offset = 0x400 + 8 * j,
  2708. .access = PL2_RW,
  2709. .readfn = ich_lr_read,
  2710. .writefn = ich_lr_write,
  2711. },
  2712. { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
  2713. .cp = 15, .opc1 = 4, .crn = 12,
  2714. .crm = 14 + (j >> 3), .opc2 = j & 7,
  2715. .type = ARM_CP_IO | ARM_CP_NO_RAW,
  2716. .access = PL2_RW,
  2717. .readfn = ich_lr_read,
  2718. .writefn = ich_lr_write,
  2719. },
  2720. };
  2721. define_arm_cp_regs(cpu, lr_regset);
  2722. }
  2723. if (cs->vprebits >= 6) {
  2724. define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
  2725. }
  2726. if (cs->vprebits == 7) {
  2727. define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
  2728. }
  2729. }
  2730. if (tcg_enabled() || qtest_enabled()) {
  2731. /*
  2732. * We can only trap EL changes with TCG. However the GIC interrupt
  2733. * state only changes on EL changes involving EL2 or EL3, so for
  2734. * the non-TCG case this is OK, as EL2 and EL3 can't exist.
  2735. */
  2736. arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);
  2737. } else {
  2738. assert(!arm_feature(&cpu->env, ARM_FEATURE_EL2));
  2739. assert(!arm_feature(&cpu->env, ARM_FEATURE_EL3));
  2740. }
  2741. }
  2742. }