arm_gic.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. /*
  2. * ARM Generic/Distributed Interrupt Controller
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. /* This file contains implementation code for the RealView EB interrupt
  10. * controller, MPCore distributed interrupt controller and ARMv7-M
  11. * Nested Vectored Interrupt Controller.
  12. * It is compiled in two ways:
  13. * (1) as a standalone file to produce a sysbus device which is a GIC
  14. * that can be used on the realview board and as one of the builtin
  15. * private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
  16. * (2) by being directly #included into armv7m_nvic.c to produce the
  17. * armv7m_nvic device.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/irq.h"
  21. #include "hw/sysbus.h"
  22. #include "gic_internal.h"
  23. #include "qapi/error.h"
  24. #include "hw/core/cpu.h"
  25. #include "qemu/log.h"
  26. #include "qemu/module.h"
  27. #include "trace.h"
  28. #include "sysemu/kvm.h"
  29. /* #define DEBUG_GIC */
  30. #ifdef DEBUG_GIC
  31. #define DEBUG_GIC_GATE 1
  32. #else
  33. #define DEBUG_GIC_GATE 0
  34. #endif
  35. #define DPRINTF(fmt, ...) do { \
  36. if (DEBUG_GIC_GATE) { \
  37. fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
  38. } \
  39. } while (0)
  40. static const uint8_t gic_id_11mpcore[] = {
  41. 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  42. };
  43. static const uint8_t gic_id_gicv1[] = {
  44. 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  45. };
  46. static const uint8_t gic_id_gicv2[] = {
  47. 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  48. };
  49. static inline int gic_get_current_cpu(GICState *s)
  50. {
  51. if (s->num_cpu > 1) {
  52. return current_cpu->cpu_index;
  53. }
  54. return 0;
  55. }
  56. static inline int gic_get_current_vcpu(GICState *s)
  57. {
  58. return gic_get_current_cpu(s) + GIC_NCPU;
  59. }
  60. /* Return true if this GIC config has interrupt groups, which is
  61. * true if we're a GICv2, or a GICv1 with the security extensions.
  62. */
  63. static inline bool gic_has_groups(GICState *s)
  64. {
  65. return s->revision == 2 || s->security_extn;
  66. }
  67. static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs)
  68. {
  69. return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure;
  70. }
  71. static inline void gic_get_best_irq(GICState *s, int cpu,
  72. int *best_irq, int *best_prio, int *group)
  73. {
  74. int irq;
  75. int cm = 1 << cpu;
  76. *best_irq = 1023;
  77. *best_prio = 0x100;
  78. for (irq = 0; irq < s->num_irq; irq++) {
  79. if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
  80. (!GIC_DIST_TEST_ACTIVE(irq, cm)) &&
  81. (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) {
  82. if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) {
  83. *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu);
  84. *best_irq = irq;
  85. }
  86. }
  87. }
  88. if (*best_irq < 1023) {
  89. *group = GIC_DIST_TEST_GROUP(*best_irq, cm);
  90. }
  91. }
  92. static inline void gic_get_best_virq(GICState *s, int cpu,
  93. int *best_irq, int *best_prio, int *group)
  94. {
  95. int lr_idx = 0;
  96. *best_irq = 1023;
  97. *best_prio = 0x100;
  98. for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
  99. uint32_t lr_entry = s->h_lr[lr_idx][cpu];
  100. int state = GICH_LR_STATE(lr_entry);
  101. if (state == GICH_LR_STATE_PENDING) {
  102. int prio = GICH_LR_PRIORITY(lr_entry);
  103. if (prio < *best_prio) {
  104. *best_prio = prio;
  105. *best_irq = GICH_LR_VIRT_ID(lr_entry);
  106. *group = GICH_LR_GROUP(lr_entry);
  107. }
  108. }
  109. }
  110. }
  111. /* Return true if IRQ signaling is enabled for the given cpu and at least one
  112. * of the given groups:
  113. * - in the non-virt case, the distributor must be enabled for one of the
  114. * given groups
  115. * - in the virt case, the virtual interface must be enabled.
  116. * - in all cases, the (v)CPU interface must be enabled for one of the given
  117. * groups.
  118. */
  119. static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt,
  120. int group_mask)
  121. {
  122. if (!virt && !(s->ctlr & group_mask)) {
  123. return false;
  124. }
  125. if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) {
  126. return false;
  127. }
  128. if (!(s->cpu_ctlr[cpu] & group_mask)) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /* TODO: Many places that call this routine could be optimized. */
  134. /* Update interrupt status after enabled or pending bits have been changed. */
  135. static inline void gic_update_internal(GICState *s, bool virt)
  136. {
  137. int best_irq;
  138. int best_prio;
  139. int irq_level, fiq_level;
  140. int cpu, cpu_iface;
  141. int group = 0;
  142. qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq;
  143. qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq;
  144. for (cpu = 0; cpu < s->num_cpu; cpu++) {
  145. cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
  146. s->current_pending[cpu_iface] = 1023;
  147. if (!gic_irq_signaling_enabled(s, cpu, virt,
  148. GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) {
  149. qemu_irq_lower(irq_lines[cpu]);
  150. qemu_irq_lower(fiq_lines[cpu]);
  151. continue;
  152. }
  153. if (virt) {
  154. gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group);
  155. } else {
  156. gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group);
  157. }
  158. if (best_irq != 1023) {
  159. trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu,
  160. best_irq, best_prio,
  161. s->priority_mask[cpu_iface],
  162. s->running_priority[cpu_iface]);
  163. }
  164. irq_level = fiq_level = 0;
  165. if (best_prio < s->priority_mask[cpu_iface]) {
  166. s->current_pending[cpu_iface] = best_irq;
  167. if (best_prio < s->running_priority[cpu_iface]) {
  168. if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
  169. if (group == 0 &&
  170. s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
  171. DPRINTF("Raised pending FIQ %d (cpu %d)\n",
  172. best_irq, cpu_iface);
  173. fiq_level = 1;
  174. trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
  175. fiq_level);
  176. } else {
  177. DPRINTF("Raised pending IRQ %d (cpu %d)\n",
  178. best_irq, cpu_iface);
  179. irq_level = 1;
  180. trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
  181. irq_level);
  182. }
  183. }
  184. }
  185. }
  186. qemu_set_irq(irq_lines[cpu], irq_level);
  187. qemu_set_irq(fiq_lines[cpu], fiq_level);
  188. }
  189. }
  190. static void gic_update(GICState *s)
  191. {
  192. gic_update_internal(s, false);
  193. }
  194. /* Return true if this LR is empty, i.e. the corresponding bit
  195. * in ELRSR is set.
  196. */
  197. static inline bool gic_lr_entry_is_free(uint32_t entry)
  198. {
  199. return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
  200. && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
  201. }
  202. /* Return true if this LR should trigger an EOI maintenance interrupt, i.e. the
  203. * corrsponding bit in EISR is set.
  204. */
  205. static inline bool gic_lr_entry_is_eoi(uint32_t entry)
  206. {
  207. return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
  208. && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
  209. }
  210. static inline void gic_extract_lr_info(GICState *s, int cpu,
  211. int *num_eoi, int *num_valid, int *num_pending)
  212. {
  213. int lr_idx;
  214. *num_eoi = 0;
  215. *num_valid = 0;
  216. *num_pending = 0;
  217. for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
  218. uint32_t *entry = &s->h_lr[lr_idx][cpu];
  219. if (gic_lr_entry_is_eoi(*entry)) {
  220. (*num_eoi)++;
  221. }
  222. if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
  223. (*num_valid)++;
  224. }
  225. if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
  226. (*num_pending)++;
  227. }
  228. }
  229. }
  230. static void gic_compute_misr(GICState *s, int cpu)
  231. {
  232. uint32_t value = 0;
  233. int vcpu = cpu + GIC_NCPU;
  234. int num_eoi, num_valid, num_pending;
  235. gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
  236. /* EOI */
  237. if (num_eoi) {
  238. value |= R_GICH_MISR_EOI_MASK;
  239. }
  240. /* U: true if only 0 or 1 LR entry is valid */
  241. if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
  242. value |= R_GICH_MISR_U_MASK;
  243. }
  244. /* LRENP: EOICount is not 0 */
  245. if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
  246. ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
  247. value |= R_GICH_MISR_LRENP_MASK;
  248. }
  249. /* NP: no pending interrupts */
  250. if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
  251. value |= R_GICH_MISR_NP_MASK;
  252. }
  253. /* VGrp0E: group0 virq signaling enabled */
  254. if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
  255. (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
  256. value |= R_GICH_MISR_VGrp0E_MASK;
  257. }
  258. /* VGrp0D: group0 virq signaling disabled */
  259. if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
  260. !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
  261. value |= R_GICH_MISR_VGrp0D_MASK;
  262. }
  263. /* VGrp1E: group1 virq signaling enabled */
  264. if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
  265. (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
  266. value |= R_GICH_MISR_VGrp1E_MASK;
  267. }
  268. /* VGrp1D: group1 virq signaling disabled */
  269. if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
  270. !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
  271. value |= R_GICH_MISR_VGrp1D_MASK;
  272. }
  273. s->h_misr[cpu] = value;
  274. }
  275. static void gic_update_maintenance(GICState *s)
  276. {
  277. int cpu = 0;
  278. int maint_level;
  279. for (cpu = 0; cpu < s->num_cpu; cpu++) {
  280. gic_compute_misr(s, cpu);
  281. maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
  282. trace_gic_update_maintenance_irq(cpu, maint_level);
  283. qemu_set_irq(s->maintenance_irq[cpu], maint_level);
  284. }
  285. }
  286. static void gic_update_virt(GICState *s)
  287. {
  288. gic_update_internal(s, true);
  289. gic_update_maintenance(s);
  290. }
  291. static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
  292. int cm, int target)
  293. {
  294. if (level) {
  295. GIC_DIST_SET_LEVEL(irq, cm);
  296. if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
  297. DPRINTF("Set %d pending mask %x\n", irq, target);
  298. GIC_DIST_SET_PENDING(irq, target);
  299. }
  300. } else {
  301. GIC_DIST_CLEAR_LEVEL(irq, cm);
  302. }
  303. }
  304. static void gic_set_irq_generic(GICState *s, int irq, int level,
  305. int cm, int target)
  306. {
  307. if (level) {
  308. GIC_DIST_SET_LEVEL(irq, cm);
  309. DPRINTF("Set %d pending mask %x\n", irq, target);
  310. if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
  311. GIC_DIST_SET_PENDING(irq, target);
  312. }
  313. } else {
  314. GIC_DIST_CLEAR_LEVEL(irq, cm);
  315. }
  316. }
  317. /* Process a change in an external IRQ input. */
  318. static void gic_set_irq(void *opaque, int irq, int level)
  319. {
  320. /* Meaning of the 'irq' parameter:
  321. * [0..N-1] : external interrupts
  322. * [N..N+31] : PPI (internal) interrupts for CPU 0
  323. * [N+32..N+63] : PPI (internal interrupts for CPU 1
  324. * ...
  325. */
  326. GICState *s = (GICState *)opaque;
  327. int cm, target;
  328. if (irq < (s->num_irq - GIC_INTERNAL)) {
  329. /* The first external input line is internal interrupt 32. */
  330. cm = ALL_CPU_MASK;
  331. irq += GIC_INTERNAL;
  332. target = GIC_DIST_TARGET(irq);
  333. } else {
  334. int cpu;
  335. irq -= (s->num_irq - GIC_INTERNAL);
  336. cpu = irq / GIC_INTERNAL;
  337. irq %= GIC_INTERNAL;
  338. cm = 1 << cpu;
  339. target = cm;
  340. }
  341. assert(irq >= GIC_NR_SGIS);
  342. if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
  343. return;
  344. }
  345. if (s->revision == REV_11MPCORE) {
  346. gic_set_irq_11mpcore(s, irq, level, cm, target);
  347. } else {
  348. gic_set_irq_generic(s, irq, level, cm, target);
  349. }
  350. trace_gic_set_irq(irq, level, cm, target);
  351. gic_update(s);
  352. }
  353. static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
  354. MemTxAttrs attrs)
  355. {
  356. uint16_t pending_irq = s->current_pending[cpu];
  357. if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
  358. int group = gic_test_group(s, pending_irq, cpu);
  359. /* On a GIC without the security extensions, reading this register
  360. * behaves in the same way as a secure access to a GIC with them.
  361. */
  362. bool secure = !gic_cpu_ns_access(s, cpu, attrs);
  363. if (group == 0 && !secure) {
  364. /* Group0 interrupts hidden from Non-secure access */
  365. return 1023;
  366. }
  367. if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
  368. /* Group1 interrupts only seen by Secure access if
  369. * AckCtl bit set.
  370. */
  371. return 1022;
  372. }
  373. }
  374. return pending_irq;
  375. }
  376. static int gic_get_group_priority(GICState *s, int cpu, int irq)
  377. {
  378. /* Return the group priority of the specified interrupt
  379. * (which is the top bits of its priority, with the number
  380. * of bits masked determined by the applicable binary point register).
  381. */
  382. int bpr;
  383. uint32_t mask;
  384. if (gic_has_groups(s) &&
  385. !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
  386. gic_test_group(s, irq, cpu)) {
  387. bpr = s->abpr[cpu] - 1;
  388. assert(bpr >= 0);
  389. } else {
  390. bpr = s->bpr[cpu];
  391. }
  392. /* a BPR of 0 means the group priority bits are [7:1];
  393. * a BPR of 1 means they are [7:2], and so on down to
  394. * a BPR of 7 meaning no group priority bits at all.
  395. */
  396. mask = ~0U << ((bpr & 7) + 1);
  397. return gic_get_priority(s, irq, cpu) & mask;
  398. }
  399. static void gic_activate_irq(GICState *s, int cpu, int irq)
  400. {
  401. /* Set the appropriate Active Priority Register bit for this IRQ,
  402. * and update the running priority.
  403. */
  404. int prio = gic_get_group_priority(s, cpu, irq);
  405. int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
  406. int preemption_level = prio >> (min_bpr + 1);
  407. int regno = preemption_level / 32;
  408. int bitno = preemption_level % 32;
  409. uint32_t *papr = NULL;
  410. if (gic_is_vcpu(cpu)) {
  411. assert(regno == 0);
  412. papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
  413. } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
  414. papr = &s->nsapr[regno][cpu];
  415. } else {
  416. papr = &s->apr[regno][cpu];
  417. }
  418. *papr |= (1 << bitno);
  419. s->running_priority[cpu] = prio;
  420. gic_set_active(s, irq, cpu);
  421. }
  422. static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
  423. {
  424. /* Recalculate the current running priority for this CPU based
  425. * on the set bits in the Active Priority Registers.
  426. */
  427. int i;
  428. if (gic_is_vcpu(cpu)) {
  429. uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
  430. if (apr) {
  431. return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
  432. } else {
  433. return 0x100;
  434. }
  435. }
  436. for (i = 0; i < GIC_NR_APRS; i++) {
  437. uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
  438. if (!apr) {
  439. continue;
  440. }
  441. return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
  442. }
  443. return 0x100;
  444. }
  445. static void gic_drop_prio(GICState *s, int cpu, int group)
  446. {
  447. /* Drop the priority of the currently active interrupt in the
  448. * specified group.
  449. *
  450. * Note that we can guarantee (because of the requirement to nest
  451. * GICC_IAR reads [which activate an interrupt and raise priority]
  452. * with GICC_EOIR writes [which drop the priority for the interrupt])
  453. * that the interrupt we're being called for is the highest priority
  454. * active interrupt, meaning that it has the lowest set bit in the
  455. * APR registers.
  456. *
  457. * If the guest does not honour the ordering constraints then the
  458. * behaviour of the GIC is UNPREDICTABLE, which for us means that
  459. * the values of the APR registers might become incorrect and the
  460. * running priority will be wrong, so interrupts that should preempt
  461. * might not do so, and interrupts that should not preempt might do so.
  462. */
  463. if (gic_is_vcpu(cpu)) {
  464. int rcpu = gic_get_vcpu_real_id(cpu);
  465. if (s->h_apr[rcpu]) {
  466. /* Clear lowest set bit */
  467. s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
  468. }
  469. } else {
  470. int i;
  471. for (i = 0; i < GIC_NR_APRS; i++) {
  472. uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
  473. if (!*papr) {
  474. continue;
  475. }
  476. /* Clear lowest set bit */
  477. *papr &= *papr - 1;
  478. break;
  479. }
  480. }
  481. s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
  482. }
  483. static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
  484. {
  485. int src;
  486. uint32_t ret;
  487. if (!gic_is_vcpu(cpu)) {
  488. /* Lookup the source CPU for the SGI and clear this in the
  489. * sgi_pending map. Return the src and clear the overall pending
  490. * state on this CPU if the SGI is not pending from any CPUs.
  491. */
  492. assert(s->sgi_pending[irq][cpu] != 0);
  493. src = ctz32(s->sgi_pending[irq][cpu]);
  494. s->sgi_pending[irq][cpu] &= ~(1 << src);
  495. if (s->sgi_pending[irq][cpu] == 0) {
  496. gic_clear_pending(s, irq, cpu);
  497. }
  498. ret = irq | ((src & 0x7) << 10);
  499. } else {
  500. uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
  501. src = GICH_LR_CPUID(*lr_entry);
  502. gic_clear_pending(s, irq, cpu);
  503. ret = irq | (src << 10);
  504. }
  505. return ret;
  506. }
  507. uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
  508. {
  509. int ret, irq;
  510. /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
  511. * for the case where this GIC supports grouping and the pending interrupt
  512. * is in the wrong group.
  513. */
  514. irq = gic_get_current_pending_irq(s, cpu, attrs);
  515. trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
  516. gic_get_vcpu_real_id(cpu), irq);
  517. if (irq >= GIC_MAXIRQ) {
  518. DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
  519. return irq;
  520. }
  521. if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
  522. DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
  523. return 1023;
  524. }
  525. gic_activate_irq(s, cpu, irq);
  526. if (s->revision == REV_11MPCORE) {
  527. /* Clear pending flags for both level and edge triggered interrupts.
  528. * Level triggered IRQs will be reasserted once they become inactive.
  529. */
  530. gic_clear_pending(s, irq, cpu);
  531. ret = irq;
  532. } else {
  533. if (irq < GIC_NR_SGIS) {
  534. ret = gic_clear_pending_sgi(s, irq, cpu);
  535. } else {
  536. gic_clear_pending(s, irq, cpu);
  537. ret = irq;
  538. }
  539. }
  540. if (gic_is_vcpu(cpu)) {
  541. gic_update_virt(s);
  542. } else {
  543. gic_update(s);
  544. }
  545. DPRINTF("ACK %d\n", irq);
  546. return ret;
  547. }
  548. static uint32_t gic_fullprio_mask(GICState *s, int cpu)
  549. {
  550. /*
  551. * Return a mask word which clears the unimplemented priority
  552. * bits from a priority value for an interrupt. (Not to be
  553. * confused with the group priority, whose mask depends on BPR.)
  554. */
  555. int priBits;
  556. if (gic_is_vcpu(cpu)) {
  557. priBits = GIC_VIRT_MAX_GROUP_PRIO_BITS;
  558. } else {
  559. priBits = s->n_prio_bits;
  560. }
  561. return ~0U << (8 - priBits);
  562. }
  563. void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
  564. MemTxAttrs attrs)
  565. {
  566. if (s->security_extn && !attrs.secure) {
  567. if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
  568. return; /* Ignore Non-secure access of Group0 IRQ */
  569. }
  570. val = 0x80 | (val >> 1); /* Non-secure view */
  571. }
  572. val &= gic_fullprio_mask(s, cpu);
  573. if (irq < GIC_INTERNAL) {
  574. s->priority1[irq][cpu] = val;
  575. } else {
  576. s->priority2[(irq) - GIC_INTERNAL] = val;
  577. }
  578. }
  579. static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
  580. MemTxAttrs attrs)
  581. {
  582. uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
  583. if (s->security_extn && !attrs.secure) {
  584. if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
  585. return 0; /* Non-secure access cannot read priority of Group0 IRQ */
  586. }
  587. prio = (prio << 1) & 0xff; /* Non-secure view */
  588. }
  589. return prio & gic_fullprio_mask(s, cpu);
  590. }
  591. static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
  592. MemTxAttrs attrs)
  593. {
  594. if (gic_cpu_ns_access(s, cpu, attrs)) {
  595. if (s->priority_mask[cpu] & 0x80) {
  596. /* Priority Mask in upper half */
  597. pmask = 0x80 | (pmask >> 1);
  598. } else {
  599. /* Non-secure write ignored if priority mask is in lower half */
  600. return;
  601. }
  602. }
  603. s->priority_mask[cpu] = pmask & gic_fullprio_mask(s, cpu);
  604. }
  605. static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
  606. {
  607. uint32_t pmask = s->priority_mask[cpu];
  608. if (gic_cpu_ns_access(s, cpu, attrs)) {
  609. if (pmask & 0x80) {
  610. /* Priority Mask in upper half, return Non-secure view */
  611. pmask = (pmask << 1) & 0xff;
  612. } else {
  613. /* Priority Mask in lower half, RAZ */
  614. pmask = 0;
  615. }
  616. }
  617. return pmask;
  618. }
  619. static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
  620. {
  621. uint32_t ret = s->cpu_ctlr[cpu];
  622. if (gic_cpu_ns_access(s, cpu, attrs)) {
  623. /* Construct the NS banked view of GICC_CTLR from the correct
  624. * bits of the S banked view. We don't need to move the bypass
  625. * control bits because we don't implement that (IMPDEF) part
  626. * of the GIC architecture.
  627. */
  628. ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
  629. }
  630. return ret;
  631. }
  632. static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
  633. MemTxAttrs attrs)
  634. {
  635. uint32_t mask;
  636. if (gic_cpu_ns_access(s, cpu, attrs)) {
  637. /* The NS view can only write certain bits in the register;
  638. * the rest are unchanged
  639. */
  640. mask = GICC_CTLR_EN_GRP1;
  641. if (s->revision == 2) {
  642. mask |= GICC_CTLR_EOIMODE_NS;
  643. }
  644. s->cpu_ctlr[cpu] &= ~mask;
  645. s->cpu_ctlr[cpu] |= (value << 1) & mask;
  646. } else {
  647. if (s->revision == 2) {
  648. mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
  649. } else {
  650. mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
  651. }
  652. s->cpu_ctlr[cpu] = value & mask;
  653. }
  654. DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
  655. "Group1 Interrupts %sabled\n", cpu,
  656. (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
  657. (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
  658. }
  659. static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
  660. {
  661. if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
  662. /* Idle priority */
  663. return 0xff;
  664. }
  665. if (gic_cpu_ns_access(s, cpu, attrs)) {
  666. if (s->running_priority[cpu] & 0x80) {
  667. /* Running priority in upper half of range: return the Non-secure
  668. * view of the priority.
  669. */
  670. return s->running_priority[cpu] << 1;
  671. } else {
  672. /* Running priority in lower half of range: RAZ */
  673. return 0;
  674. }
  675. } else {
  676. return s->running_priority[cpu];
  677. }
  678. }
  679. /* Return true if we should split priority drop and interrupt deactivation,
  680. * ie whether the relevant EOIMode bit is set.
  681. */
  682. static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
  683. {
  684. if (s->revision != 2) {
  685. /* Before GICv2 prio-drop and deactivate are not separable */
  686. return false;
  687. }
  688. if (gic_cpu_ns_access(s, cpu, attrs)) {
  689. return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
  690. }
  691. return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
  692. }
  693. static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
  694. {
  695. int group;
  696. if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
  697. /*
  698. * This handles two cases:
  699. * 1. If software writes the ID of a spurious interrupt [ie 1023]
  700. * to the GICC_DIR, the GIC ignores that write.
  701. * 2. If software writes the number of a non-existent interrupt
  702. * this must be a subcase of "value written is not an active interrupt"
  703. * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
  704. * all IRQs potentially exist, so this limit does not apply.
  705. */
  706. return;
  707. }
  708. if (!gic_eoi_split(s, cpu, attrs)) {
  709. /* This is UNPREDICTABLE; we choose to ignore it */
  710. qemu_log_mask(LOG_GUEST_ERROR,
  711. "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
  712. return;
  713. }
  714. if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
  715. /* This vIRQ does not have an LR entry which is either active or
  716. * pending and active. Increment EOICount and ignore the write.
  717. */
  718. int rcpu = gic_get_vcpu_real_id(cpu);
  719. s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
  720. /* Update the virtual interface in case a maintenance interrupt should
  721. * be raised.
  722. */
  723. gic_update_virt(s);
  724. return;
  725. }
  726. group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
  727. if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
  728. DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
  729. return;
  730. }
  731. gic_clear_active(s, irq, cpu);
  732. }
  733. static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
  734. {
  735. int cm = 1 << cpu;
  736. int group;
  737. DPRINTF("EOI %d\n", irq);
  738. if (gic_is_vcpu(cpu)) {
  739. /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
  740. * running prio is < 0x100.
  741. */
  742. bool prio_drop = s->running_priority[cpu] < 0x100;
  743. if (irq >= GIC_MAXIRQ) {
  744. /* Ignore spurious interrupt */
  745. return;
  746. }
  747. gic_drop_prio(s, cpu, 0);
  748. if (!gic_eoi_split(s, cpu, attrs)) {
  749. bool valid = gic_virq_is_valid(s, irq, cpu);
  750. if (prio_drop && !valid) {
  751. /* We are in a situation where:
  752. * - V_CTRL.EOIMode is false (no EOI split),
  753. * - The call to gic_drop_prio() cleared a bit in GICH_APR,
  754. * - This vIRQ does not have an LR entry which is either
  755. * active or pending and active.
  756. * In that case, we must increment EOICount.
  757. */
  758. int rcpu = gic_get_vcpu_real_id(cpu);
  759. s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
  760. } else if (valid) {
  761. gic_clear_active(s, irq, cpu);
  762. }
  763. }
  764. gic_update_virt(s);
  765. return;
  766. }
  767. if (irq >= s->num_irq) {
  768. /* This handles two cases:
  769. * 1. If software writes the ID of a spurious interrupt [ie 1023]
  770. * to the GICC_EOIR, the GIC ignores that write.
  771. * 2. If software writes the number of a non-existent interrupt
  772. * this must be a subcase of "value written does not match the last
  773. * valid interrupt value read from the Interrupt Acknowledge
  774. * register" and so this is UNPREDICTABLE. We choose to ignore it.
  775. */
  776. return;
  777. }
  778. if (s->running_priority[cpu] == 0x100) {
  779. return; /* No active IRQ. */
  780. }
  781. if (s->revision == REV_11MPCORE) {
  782. /* Mark level triggered interrupts as pending if they are still
  783. raised. */
  784. if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
  785. && GIC_DIST_TEST_LEVEL(irq, cm)
  786. && (GIC_DIST_TARGET(irq) & cm) != 0) {
  787. DPRINTF("Set %d pending mask %x\n", irq, cm);
  788. GIC_DIST_SET_PENDING(irq, cm);
  789. }
  790. }
  791. group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
  792. if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
  793. DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
  794. return;
  795. }
  796. /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
  797. * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
  798. * i.e. go ahead and complete the irq anyway.
  799. */
  800. gic_drop_prio(s, cpu, group);
  801. /* In GICv2 the guest can choose to split priority-drop and deactivate */
  802. if (!gic_eoi_split(s, cpu, attrs)) {
  803. gic_clear_active(s, irq, cpu);
  804. }
  805. gic_update(s);
  806. }
  807. static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
  808. {
  809. GICState *s = (GICState *)opaque;
  810. uint32_t res;
  811. int irq;
  812. int i;
  813. int cpu;
  814. int cm;
  815. int mask;
  816. cpu = gic_get_current_cpu(s);
  817. cm = 1 << cpu;
  818. if (offset < 0x100) {
  819. if (offset == 0) { /* GICD_CTLR */
  820. if (s->security_extn && !attrs.secure) {
  821. /* The NS bank of this register is just an alias of the
  822. * EnableGrp1 bit in the S bank version.
  823. */
  824. return extract32(s->ctlr, 1, 1);
  825. } else {
  826. return s->ctlr;
  827. }
  828. }
  829. if (offset == 4)
  830. /* Interrupt Controller Type Register */
  831. return ((s->num_irq / 32) - 1)
  832. | ((s->num_cpu - 1) << 5)
  833. | (s->security_extn << 10);
  834. if (offset < 0x08)
  835. return 0;
  836. if (offset >= 0x80) {
  837. /* Interrupt Group Registers: these RAZ/WI if this is an NS
  838. * access to a GIC with the security extensions, or if the GIC
  839. * doesn't have groups at all.
  840. */
  841. res = 0;
  842. if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
  843. /* Every byte offset holds 8 group status bits */
  844. irq = (offset - 0x080) * 8;
  845. if (irq >= s->num_irq) {
  846. goto bad_reg;
  847. }
  848. for (i = 0; i < 8; i++) {
  849. if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
  850. res |= (1 << i);
  851. }
  852. }
  853. }
  854. return res;
  855. }
  856. goto bad_reg;
  857. } else if (offset < 0x200) {
  858. /* Interrupt Set/Clear Enable. */
  859. if (offset < 0x180)
  860. irq = (offset - 0x100) * 8;
  861. else
  862. irq = (offset - 0x180) * 8;
  863. if (irq >= s->num_irq)
  864. goto bad_reg;
  865. res = 0;
  866. for (i = 0; i < 8; i++) {
  867. if (s->security_extn && !attrs.secure &&
  868. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  869. continue; /* Ignore Non-secure access of Group0 IRQ */
  870. }
  871. if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
  872. res |= (1 << i);
  873. }
  874. }
  875. } else if (offset < 0x300) {
  876. /* Interrupt Set/Clear Pending. */
  877. if (offset < 0x280)
  878. irq = (offset - 0x200) * 8;
  879. else
  880. irq = (offset - 0x280) * 8;
  881. if (irq >= s->num_irq)
  882. goto bad_reg;
  883. res = 0;
  884. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  885. for (i = 0; i < 8; i++) {
  886. if (s->security_extn && !attrs.secure &&
  887. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  888. continue; /* Ignore Non-secure access of Group0 IRQ */
  889. }
  890. if (gic_test_pending(s, irq + i, mask)) {
  891. res |= (1 << i);
  892. }
  893. }
  894. } else if (offset < 0x400) {
  895. /* Interrupt Set/Clear Active. */
  896. if (offset < 0x380) {
  897. irq = (offset - 0x300) * 8;
  898. } else if (s->revision == 2) {
  899. irq = (offset - 0x380) * 8;
  900. } else {
  901. goto bad_reg;
  902. }
  903. if (irq >= s->num_irq)
  904. goto bad_reg;
  905. res = 0;
  906. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  907. for (i = 0; i < 8; i++) {
  908. if (s->security_extn && !attrs.secure &&
  909. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  910. continue; /* Ignore Non-secure access of Group0 IRQ */
  911. }
  912. if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
  913. res |= (1 << i);
  914. }
  915. }
  916. } else if (offset < 0x800) {
  917. /* Interrupt Priority. */
  918. irq = (offset - 0x400);
  919. if (irq >= s->num_irq)
  920. goto bad_reg;
  921. res = gic_dist_get_priority(s, cpu, irq, attrs);
  922. } else if (offset < 0xc00) {
  923. /* Interrupt CPU Target. */
  924. if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
  925. /* For uniprocessor GICs these RAZ/WI */
  926. res = 0;
  927. } else {
  928. irq = (offset - 0x800);
  929. if (irq >= s->num_irq) {
  930. goto bad_reg;
  931. }
  932. if (irq < 29 && s->revision == REV_11MPCORE) {
  933. res = 0;
  934. } else if (irq < GIC_INTERNAL) {
  935. res = cm;
  936. } else {
  937. res = GIC_DIST_TARGET(irq);
  938. }
  939. }
  940. } else if (offset < 0xf00) {
  941. /* Interrupt Configuration. */
  942. irq = (offset - 0xc00) * 4;
  943. if (irq >= s->num_irq)
  944. goto bad_reg;
  945. res = 0;
  946. for (i = 0; i < 4; i++) {
  947. if (s->security_extn && !attrs.secure &&
  948. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  949. continue; /* Ignore Non-secure access of Group0 IRQ */
  950. }
  951. if (GIC_DIST_TEST_MODEL(irq + i)) {
  952. res |= (1 << (i * 2));
  953. }
  954. if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
  955. res |= (2 << (i * 2));
  956. }
  957. }
  958. } else if (offset < 0xf10) {
  959. goto bad_reg;
  960. } else if (offset < 0xf30) {
  961. if (s->revision == REV_11MPCORE) {
  962. goto bad_reg;
  963. }
  964. if (offset < 0xf20) {
  965. /* GICD_CPENDSGIRn */
  966. irq = (offset - 0xf10);
  967. } else {
  968. irq = (offset - 0xf20);
  969. /* GICD_SPENDSGIRn */
  970. }
  971. if (s->security_extn && !attrs.secure &&
  972. !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
  973. res = 0; /* Ignore Non-secure access of Group0 IRQ */
  974. } else {
  975. res = s->sgi_pending[irq][cpu];
  976. }
  977. } else if (offset < 0xfd0) {
  978. goto bad_reg;
  979. } else if (offset < 0x1000) {
  980. if (offset & 3) {
  981. res = 0;
  982. } else {
  983. switch (s->revision) {
  984. case REV_11MPCORE:
  985. res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
  986. break;
  987. case 1:
  988. res = gic_id_gicv1[(offset - 0xfd0) >> 2];
  989. break;
  990. case 2:
  991. res = gic_id_gicv2[(offset - 0xfd0) >> 2];
  992. break;
  993. default:
  994. res = 0;
  995. }
  996. }
  997. } else {
  998. g_assert_not_reached();
  999. }
  1000. return res;
  1001. bad_reg:
  1002. qemu_log_mask(LOG_GUEST_ERROR,
  1003. "gic_dist_readb: Bad offset %x\n", (int)offset);
  1004. return 0;
  1005. }
  1006. static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
  1007. unsigned size, MemTxAttrs attrs)
  1008. {
  1009. switch (size) {
  1010. case 1:
  1011. *data = gic_dist_readb(opaque, offset, attrs);
  1012. break;
  1013. case 2:
  1014. *data = gic_dist_readb(opaque, offset, attrs);
  1015. *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
  1016. break;
  1017. case 4:
  1018. *data = gic_dist_readb(opaque, offset, attrs);
  1019. *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
  1020. *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
  1021. *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
  1022. break;
  1023. default:
  1024. return MEMTX_ERROR;
  1025. }
  1026. trace_gic_dist_read(offset, size, *data);
  1027. return MEMTX_OK;
  1028. }
  1029. static void gic_dist_writeb(void *opaque, hwaddr offset,
  1030. uint32_t value, MemTxAttrs attrs)
  1031. {
  1032. GICState *s = (GICState *)opaque;
  1033. int irq;
  1034. int i;
  1035. int cpu;
  1036. cpu = gic_get_current_cpu(s);
  1037. if (offset < 0x100) {
  1038. if (offset == 0) {
  1039. if (s->security_extn && !attrs.secure) {
  1040. /* NS version is just an alias of the S version's bit 1 */
  1041. s->ctlr = deposit32(s->ctlr, 1, 1, value);
  1042. } else if (gic_has_groups(s)) {
  1043. s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
  1044. } else {
  1045. s->ctlr = value & GICD_CTLR_EN_GRP0;
  1046. }
  1047. DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
  1048. s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
  1049. s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
  1050. } else if (offset < 4) {
  1051. /* ignored. */
  1052. } else if (offset >= 0x80) {
  1053. /* Interrupt Group Registers: RAZ/WI for NS access to secure
  1054. * GIC, or for GICs without groups.
  1055. */
  1056. if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
  1057. /* Every byte offset holds 8 group status bits */
  1058. irq = (offset - 0x80) * 8;
  1059. if (irq >= s->num_irq) {
  1060. goto bad_reg;
  1061. }
  1062. for (i = 0; i < 8; i++) {
  1063. /* Group bits are banked for private interrupts */
  1064. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  1065. if (value & (1 << i)) {
  1066. /* Group1 (Non-secure) */
  1067. GIC_DIST_SET_GROUP(irq + i, cm);
  1068. } else {
  1069. /* Group0 (Secure) */
  1070. GIC_DIST_CLEAR_GROUP(irq + i, cm);
  1071. }
  1072. }
  1073. }
  1074. } else {
  1075. goto bad_reg;
  1076. }
  1077. } else if (offset < 0x180) {
  1078. /* Interrupt Set Enable. */
  1079. irq = (offset - 0x100) * 8;
  1080. if (irq >= s->num_irq)
  1081. goto bad_reg;
  1082. if (irq < GIC_NR_SGIS) {
  1083. value = 0xff;
  1084. }
  1085. for (i = 0; i < 8; i++) {
  1086. if (value & (1 << i)) {
  1087. int mask =
  1088. (irq < GIC_INTERNAL) ? (1 << cpu)
  1089. : GIC_DIST_TARGET(irq + i);
  1090. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  1091. if (s->security_extn && !attrs.secure &&
  1092. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1093. continue; /* Ignore Non-secure access of Group0 IRQ */
  1094. }
  1095. if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
  1096. DPRINTF("Enabled IRQ %d\n", irq + i);
  1097. trace_gic_enable_irq(irq + i);
  1098. }
  1099. GIC_DIST_SET_ENABLED(irq + i, cm);
  1100. /* If a raised level triggered IRQ enabled then mark
  1101. is as pending. */
  1102. if (GIC_DIST_TEST_LEVEL(irq + i, mask)
  1103. && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
  1104. DPRINTF("Set %d pending mask %x\n", irq + i, mask);
  1105. GIC_DIST_SET_PENDING(irq + i, mask);
  1106. }
  1107. }
  1108. }
  1109. } else if (offset < 0x200) {
  1110. /* Interrupt Clear Enable. */
  1111. irq = (offset - 0x180) * 8;
  1112. if (irq >= s->num_irq)
  1113. goto bad_reg;
  1114. if (irq < GIC_NR_SGIS) {
  1115. value = 0;
  1116. }
  1117. for (i = 0; i < 8; i++) {
  1118. if (value & (1 << i)) {
  1119. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  1120. if (s->security_extn && !attrs.secure &&
  1121. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1122. continue; /* Ignore Non-secure access of Group0 IRQ */
  1123. }
  1124. if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
  1125. DPRINTF("Disabled IRQ %d\n", irq + i);
  1126. trace_gic_disable_irq(irq + i);
  1127. }
  1128. GIC_DIST_CLEAR_ENABLED(irq + i, cm);
  1129. }
  1130. }
  1131. } else if (offset < 0x280) {
  1132. /* Interrupt Set Pending. */
  1133. irq = (offset - 0x200) * 8;
  1134. if (irq >= s->num_irq)
  1135. goto bad_reg;
  1136. if (irq < GIC_NR_SGIS) {
  1137. value = 0;
  1138. }
  1139. for (i = 0; i < 8; i++) {
  1140. if (value & (1 << i)) {
  1141. if (s->security_extn && !attrs.secure &&
  1142. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1143. continue; /* Ignore Non-secure access of Group0 IRQ */
  1144. }
  1145. GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
  1146. }
  1147. }
  1148. } else if (offset < 0x300) {
  1149. /* Interrupt Clear Pending. */
  1150. irq = (offset - 0x280) * 8;
  1151. if (irq >= s->num_irq)
  1152. goto bad_reg;
  1153. if (irq < GIC_NR_SGIS) {
  1154. value = 0;
  1155. }
  1156. for (i = 0; i < 8; i++) {
  1157. if (s->security_extn && !attrs.secure &&
  1158. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1159. continue; /* Ignore Non-secure access of Group0 IRQ */
  1160. }
  1161. /* ??? This currently clears the pending bit for all CPUs, even
  1162. for per-CPU interrupts. It's unclear whether this is the
  1163. corect behavior. */
  1164. if (value & (1 << i)) {
  1165. GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
  1166. }
  1167. }
  1168. } else if (offset < 0x380) {
  1169. /* Interrupt Set Active. */
  1170. if (s->revision != 2) {
  1171. goto bad_reg;
  1172. }
  1173. irq = (offset - 0x300) * 8;
  1174. if (irq >= s->num_irq) {
  1175. goto bad_reg;
  1176. }
  1177. /* This register is banked per-cpu for PPIs */
  1178. int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
  1179. for (i = 0; i < 8; i++) {
  1180. if (s->security_extn && !attrs.secure &&
  1181. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1182. continue; /* Ignore Non-secure access of Group0 IRQ */
  1183. }
  1184. if (value & (1 << i)) {
  1185. GIC_DIST_SET_ACTIVE(irq + i, cm);
  1186. }
  1187. }
  1188. } else if (offset < 0x400) {
  1189. /* Interrupt Clear Active. */
  1190. if (s->revision != 2) {
  1191. goto bad_reg;
  1192. }
  1193. irq = (offset - 0x380) * 8;
  1194. if (irq >= s->num_irq) {
  1195. goto bad_reg;
  1196. }
  1197. /* This register is banked per-cpu for PPIs */
  1198. int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
  1199. for (i = 0; i < 8; i++) {
  1200. if (s->security_extn && !attrs.secure &&
  1201. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1202. continue; /* Ignore Non-secure access of Group0 IRQ */
  1203. }
  1204. if (value & (1 << i)) {
  1205. GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
  1206. }
  1207. }
  1208. } else if (offset < 0x800) {
  1209. /* Interrupt Priority. */
  1210. irq = (offset - 0x400);
  1211. if (irq >= s->num_irq)
  1212. goto bad_reg;
  1213. gic_dist_set_priority(s, cpu, irq, value, attrs);
  1214. } else if (offset < 0xc00) {
  1215. /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
  1216. * annoying exception of the 11MPCore's GIC.
  1217. */
  1218. if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
  1219. irq = (offset - 0x800);
  1220. if (irq >= s->num_irq) {
  1221. goto bad_reg;
  1222. }
  1223. if (irq < 29 && s->revision == REV_11MPCORE) {
  1224. value = 0;
  1225. } else if (irq < GIC_INTERNAL) {
  1226. value = ALL_CPU_MASK;
  1227. }
  1228. s->irq_target[irq] = value & ALL_CPU_MASK;
  1229. }
  1230. } else if (offset < 0xf00) {
  1231. /* Interrupt Configuration. */
  1232. irq = (offset - 0xc00) * 4;
  1233. if (irq >= s->num_irq)
  1234. goto bad_reg;
  1235. if (irq < GIC_NR_SGIS)
  1236. value |= 0xaa;
  1237. for (i = 0; i < 4; i++) {
  1238. if (s->security_extn && !attrs.secure &&
  1239. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1240. continue; /* Ignore Non-secure access of Group0 IRQ */
  1241. }
  1242. if (s->revision == REV_11MPCORE) {
  1243. if (value & (1 << (i * 2))) {
  1244. GIC_DIST_SET_MODEL(irq + i);
  1245. } else {
  1246. GIC_DIST_CLEAR_MODEL(irq + i);
  1247. }
  1248. }
  1249. if (value & (2 << (i * 2))) {
  1250. GIC_DIST_SET_EDGE_TRIGGER(irq + i);
  1251. } else {
  1252. GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
  1253. }
  1254. }
  1255. } else if (offset < 0xf10) {
  1256. /* 0xf00 is only handled for 32-bit writes. */
  1257. goto bad_reg;
  1258. } else if (offset < 0xf20) {
  1259. /* GICD_CPENDSGIRn */
  1260. if (s->revision == REV_11MPCORE) {
  1261. goto bad_reg;
  1262. }
  1263. irq = (offset - 0xf10);
  1264. if (!s->security_extn || attrs.secure ||
  1265. GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
  1266. s->sgi_pending[irq][cpu] &= ~value;
  1267. if (s->sgi_pending[irq][cpu] == 0) {
  1268. GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
  1269. }
  1270. }
  1271. } else if (offset < 0xf30) {
  1272. /* GICD_SPENDSGIRn */
  1273. if (s->revision == REV_11MPCORE) {
  1274. goto bad_reg;
  1275. }
  1276. irq = (offset - 0xf20);
  1277. if (!s->security_extn || attrs.secure ||
  1278. GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
  1279. GIC_DIST_SET_PENDING(irq, 1 << cpu);
  1280. s->sgi_pending[irq][cpu] |= value;
  1281. }
  1282. } else {
  1283. goto bad_reg;
  1284. }
  1285. gic_update(s);
  1286. return;
  1287. bad_reg:
  1288. qemu_log_mask(LOG_GUEST_ERROR,
  1289. "gic_dist_writeb: Bad offset %x\n", (int)offset);
  1290. }
  1291. static void gic_dist_writew(void *opaque, hwaddr offset,
  1292. uint32_t value, MemTxAttrs attrs)
  1293. {
  1294. gic_dist_writeb(opaque, offset, value & 0xff, attrs);
  1295. gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
  1296. }
  1297. static void gic_dist_writel(void *opaque, hwaddr offset,
  1298. uint32_t value, MemTxAttrs attrs)
  1299. {
  1300. GICState *s = (GICState *)opaque;
  1301. if (offset == 0xf00) {
  1302. int cpu;
  1303. int irq;
  1304. int mask;
  1305. int target_cpu;
  1306. cpu = gic_get_current_cpu(s);
  1307. irq = value & 0x3ff;
  1308. switch ((value >> 24) & 3) {
  1309. case 0:
  1310. mask = (value >> 16) & ALL_CPU_MASK;
  1311. break;
  1312. case 1:
  1313. mask = ALL_CPU_MASK ^ (1 << cpu);
  1314. break;
  1315. case 2:
  1316. mask = 1 << cpu;
  1317. break;
  1318. default:
  1319. DPRINTF("Bad Soft Int target filter\n");
  1320. mask = ALL_CPU_MASK;
  1321. break;
  1322. }
  1323. GIC_DIST_SET_PENDING(irq, mask);
  1324. target_cpu = ctz32(mask);
  1325. while (target_cpu < GIC_NCPU) {
  1326. s->sgi_pending[irq][target_cpu] |= (1 << cpu);
  1327. mask &= ~(1 << target_cpu);
  1328. target_cpu = ctz32(mask);
  1329. }
  1330. gic_update(s);
  1331. return;
  1332. }
  1333. gic_dist_writew(opaque, offset, value & 0xffff, attrs);
  1334. gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
  1335. }
  1336. static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
  1337. unsigned size, MemTxAttrs attrs)
  1338. {
  1339. trace_gic_dist_write(offset, size, data);
  1340. switch (size) {
  1341. case 1:
  1342. gic_dist_writeb(opaque, offset, data, attrs);
  1343. return MEMTX_OK;
  1344. case 2:
  1345. gic_dist_writew(opaque, offset, data, attrs);
  1346. return MEMTX_OK;
  1347. case 4:
  1348. gic_dist_writel(opaque, offset, data, attrs);
  1349. return MEMTX_OK;
  1350. default:
  1351. return MEMTX_ERROR;
  1352. }
  1353. }
  1354. static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
  1355. {
  1356. /* Return the Nonsecure view of GICC_APR<regno>. This is the
  1357. * second half of GICC_NSAPR.
  1358. */
  1359. switch (GIC_MIN_BPR) {
  1360. case 0:
  1361. if (regno < 2) {
  1362. return s->nsapr[regno + 2][cpu];
  1363. }
  1364. break;
  1365. case 1:
  1366. if (regno == 0) {
  1367. return s->nsapr[regno + 1][cpu];
  1368. }
  1369. break;
  1370. case 2:
  1371. if (regno == 0) {
  1372. return extract32(s->nsapr[0][cpu], 16, 16);
  1373. }
  1374. break;
  1375. case 3:
  1376. if (regno == 0) {
  1377. return extract32(s->nsapr[0][cpu], 8, 8);
  1378. }
  1379. break;
  1380. default:
  1381. g_assert_not_reached();
  1382. }
  1383. return 0;
  1384. }
  1385. static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
  1386. uint32_t value)
  1387. {
  1388. /* Write the Nonsecure view of GICC_APR<regno>. */
  1389. switch (GIC_MIN_BPR) {
  1390. case 0:
  1391. if (regno < 2) {
  1392. s->nsapr[regno + 2][cpu] = value;
  1393. }
  1394. break;
  1395. case 1:
  1396. if (regno == 0) {
  1397. s->nsapr[regno + 1][cpu] = value;
  1398. }
  1399. break;
  1400. case 2:
  1401. if (regno == 0) {
  1402. s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
  1403. }
  1404. break;
  1405. case 3:
  1406. if (regno == 0) {
  1407. s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
  1408. }
  1409. break;
  1410. default:
  1411. g_assert_not_reached();
  1412. }
  1413. }
  1414. static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
  1415. uint64_t *data, MemTxAttrs attrs)
  1416. {
  1417. switch (offset) {
  1418. case 0x00: /* Control */
  1419. *data = gic_get_cpu_control(s, cpu, attrs);
  1420. break;
  1421. case 0x04: /* Priority mask */
  1422. *data = gic_get_priority_mask(s, cpu, attrs);
  1423. break;
  1424. case 0x08: /* Binary Point */
  1425. if (gic_cpu_ns_access(s, cpu, attrs)) {
  1426. if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
  1427. /* NS view of BPR when CBPR is 1 */
  1428. *data = MIN(s->bpr[cpu] + 1, 7);
  1429. } else {
  1430. /* BPR is banked. Non-secure copy stored in ABPR. */
  1431. *data = s->abpr[cpu];
  1432. }
  1433. } else {
  1434. *data = s->bpr[cpu];
  1435. }
  1436. break;
  1437. case 0x0c: /* Acknowledge */
  1438. *data = gic_acknowledge_irq(s, cpu, attrs);
  1439. break;
  1440. case 0x14: /* Running Priority */
  1441. *data = gic_get_running_priority(s, cpu, attrs);
  1442. break;
  1443. case 0x18: /* Highest Pending Interrupt */
  1444. *data = gic_get_current_pending_irq(s, cpu, attrs);
  1445. break;
  1446. case 0x1c: /* Aliased Binary Point */
  1447. /* GIC v2, no security: ABPR
  1448. * GIC v1, no security: not implemented (RAZ/WI)
  1449. * With security extensions, secure access: ABPR (alias of NS BPR)
  1450. * With security extensions, nonsecure access: RAZ/WI
  1451. */
  1452. if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
  1453. *data = 0;
  1454. } else {
  1455. *data = s->abpr[cpu];
  1456. }
  1457. break;
  1458. case 0xd0: case 0xd4: case 0xd8: case 0xdc:
  1459. {
  1460. int regno = (offset - 0xd0) / 4;
  1461. int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
  1462. if (regno >= nr_aprs || s->revision != 2) {
  1463. *data = 0;
  1464. } else if (gic_is_vcpu(cpu)) {
  1465. *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
  1466. } else if (gic_cpu_ns_access(s, cpu, attrs)) {
  1467. /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
  1468. *data = gic_apr_ns_view(s, regno, cpu);
  1469. } else {
  1470. *data = s->apr[regno][cpu];
  1471. }
  1472. break;
  1473. }
  1474. case 0xe0: case 0xe4: case 0xe8: case 0xec:
  1475. {
  1476. int regno = (offset - 0xe0) / 4;
  1477. if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
  1478. gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
  1479. *data = 0;
  1480. } else {
  1481. *data = s->nsapr[regno][cpu];
  1482. }
  1483. break;
  1484. }
  1485. default:
  1486. qemu_log_mask(LOG_GUEST_ERROR,
  1487. "gic_cpu_read: Bad offset %x\n", (int)offset);
  1488. *data = 0;
  1489. break;
  1490. }
  1491. trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
  1492. gic_get_vcpu_real_id(cpu), offset, *data);
  1493. return MEMTX_OK;
  1494. }
  1495. static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
  1496. uint32_t value, MemTxAttrs attrs)
  1497. {
  1498. trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
  1499. gic_get_vcpu_real_id(cpu), offset, value);
  1500. switch (offset) {
  1501. case 0x00: /* Control */
  1502. gic_set_cpu_control(s, cpu, value, attrs);
  1503. break;
  1504. case 0x04: /* Priority mask */
  1505. gic_set_priority_mask(s, cpu, value, attrs);
  1506. break;
  1507. case 0x08: /* Binary Point */
  1508. if (gic_cpu_ns_access(s, cpu, attrs)) {
  1509. if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
  1510. /* WI when CBPR is 1 */
  1511. return MEMTX_OK;
  1512. } else {
  1513. s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
  1514. }
  1515. } else {
  1516. int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
  1517. s->bpr[cpu] = MAX(value & 0x7, min_bpr);
  1518. }
  1519. break;
  1520. case 0x10: /* End Of Interrupt */
  1521. gic_complete_irq(s, cpu, value & 0x3ff, attrs);
  1522. return MEMTX_OK;
  1523. case 0x1c: /* Aliased Binary Point */
  1524. if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
  1525. /* unimplemented, or NS access: RAZ/WI */
  1526. return MEMTX_OK;
  1527. } else {
  1528. s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
  1529. }
  1530. break;
  1531. case 0xd0: case 0xd4: case 0xd8: case 0xdc:
  1532. {
  1533. int regno = (offset - 0xd0) / 4;
  1534. int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
  1535. if (regno >= nr_aprs || s->revision != 2) {
  1536. return MEMTX_OK;
  1537. }
  1538. if (gic_is_vcpu(cpu)) {
  1539. s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
  1540. } else if (gic_cpu_ns_access(s, cpu, attrs)) {
  1541. /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
  1542. gic_apr_write_ns_view(s, regno, cpu, value);
  1543. } else {
  1544. s->apr[regno][cpu] = value;
  1545. }
  1546. break;
  1547. }
  1548. case 0xe0: case 0xe4: case 0xe8: case 0xec:
  1549. {
  1550. int regno = (offset - 0xe0) / 4;
  1551. if (regno >= GIC_NR_APRS || s->revision != 2) {
  1552. return MEMTX_OK;
  1553. }
  1554. if (gic_is_vcpu(cpu)) {
  1555. return MEMTX_OK;
  1556. }
  1557. if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
  1558. return MEMTX_OK;
  1559. }
  1560. s->nsapr[regno][cpu] = value;
  1561. break;
  1562. }
  1563. case 0x1000:
  1564. /* GICC_DIR */
  1565. gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
  1566. break;
  1567. default:
  1568. qemu_log_mask(LOG_GUEST_ERROR,
  1569. "gic_cpu_write: Bad offset %x\n", (int)offset);
  1570. return MEMTX_OK;
  1571. }
  1572. if (gic_is_vcpu(cpu)) {
  1573. gic_update_virt(s);
  1574. } else {
  1575. gic_update(s);
  1576. }
  1577. return MEMTX_OK;
  1578. }
  1579. /* Wrappers to read/write the GIC CPU interface for the current CPU */
  1580. static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
  1581. unsigned size, MemTxAttrs attrs)
  1582. {
  1583. GICState *s = (GICState *)opaque;
  1584. return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
  1585. }
  1586. static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
  1587. uint64_t value, unsigned size,
  1588. MemTxAttrs attrs)
  1589. {
  1590. GICState *s = (GICState *)opaque;
  1591. return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
  1592. }
  1593. /* Wrappers to read/write the GIC CPU interface for a specific CPU.
  1594. * These just decode the opaque pointer into GICState* + cpu id.
  1595. */
  1596. static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
  1597. unsigned size, MemTxAttrs attrs)
  1598. {
  1599. GICState **backref = (GICState **)opaque;
  1600. GICState *s = *backref;
  1601. int id = (backref - s->backref);
  1602. return gic_cpu_read(s, id, addr, data, attrs);
  1603. }
  1604. static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
  1605. uint64_t value, unsigned size,
  1606. MemTxAttrs attrs)
  1607. {
  1608. GICState **backref = (GICState **)opaque;
  1609. GICState *s = *backref;
  1610. int id = (backref - s->backref);
  1611. return gic_cpu_write(s, id, addr, value, attrs);
  1612. }
  1613. static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
  1614. unsigned size, MemTxAttrs attrs)
  1615. {
  1616. GICState *s = (GICState *)opaque;
  1617. return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
  1618. }
  1619. static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
  1620. uint64_t value, unsigned size,
  1621. MemTxAttrs attrs)
  1622. {
  1623. GICState *s = (GICState *)opaque;
  1624. return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
  1625. }
  1626. static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
  1627. {
  1628. int lr_idx;
  1629. uint32_t ret = 0;
  1630. for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
  1631. uint32_t *entry = &s->h_lr[lr_idx][cpu];
  1632. ret = deposit32(ret, lr_idx - lr_start, 1,
  1633. gic_lr_entry_is_eoi(*entry));
  1634. }
  1635. return ret;
  1636. }
  1637. static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
  1638. {
  1639. int lr_idx;
  1640. uint32_t ret = 0;
  1641. for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
  1642. uint32_t *entry = &s->h_lr[lr_idx][cpu];
  1643. ret = deposit32(ret, lr_idx - lr_start, 1,
  1644. gic_lr_entry_is_free(*entry));
  1645. }
  1646. return ret;
  1647. }
  1648. static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
  1649. {
  1650. int vcpu = gic_get_current_vcpu(s);
  1651. uint32_t ctlr;
  1652. uint32_t abpr;
  1653. uint32_t bpr;
  1654. uint32_t prio_mask;
  1655. ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
  1656. abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
  1657. bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
  1658. prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
  1659. gic_set_cpu_control(s, vcpu, ctlr, attrs);
  1660. s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
  1661. s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
  1662. gic_set_priority_mask(s, vcpu, prio_mask, attrs);
  1663. }
  1664. static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
  1665. uint64_t *data, MemTxAttrs attrs)
  1666. {
  1667. GICState *s = ARM_GIC(opaque);
  1668. int vcpu = cpu + GIC_NCPU;
  1669. switch (addr) {
  1670. case A_GICH_HCR: /* Hypervisor Control */
  1671. *data = s->h_hcr[cpu];
  1672. break;
  1673. case A_GICH_VTR: /* VGIC Type */
  1674. *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
  1675. *data = FIELD_DP32(*data, GICH_VTR, PREbits,
  1676. GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
  1677. *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
  1678. (7 - GIC_VIRT_MIN_BPR) - 1);
  1679. break;
  1680. case A_GICH_VMCR: /* Virtual Machine Control */
  1681. *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
  1682. extract32(s->cpu_ctlr[vcpu], 0, 10));
  1683. *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
  1684. *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
  1685. *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
  1686. extract32(s->priority_mask[vcpu], 3, 5));
  1687. break;
  1688. case A_GICH_MISR: /* Maintenance Interrupt Status */
  1689. *data = s->h_misr[cpu];
  1690. break;
  1691. case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
  1692. case A_GICH_EISR1:
  1693. *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
  1694. break;
  1695. case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
  1696. case A_GICH_ELRSR1:
  1697. *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
  1698. break;
  1699. case A_GICH_APR: /* Active Priorities */
  1700. *data = s->h_apr[cpu];
  1701. break;
  1702. case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
  1703. {
  1704. int lr_idx = (addr - A_GICH_LR0) / 4;
  1705. if (lr_idx > s->num_lrs) {
  1706. *data = 0;
  1707. } else {
  1708. *data = s->h_lr[lr_idx][cpu];
  1709. }
  1710. break;
  1711. }
  1712. default:
  1713. qemu_log_mask(LOG_GUEST_ERROR,
  1714. "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
  1715. return MEMTX_OK;
  1716. }
  1717. trace_gic_hyp_read(addr, *data);
  1718. return MEMTX_OK;
  1719. }
  1720. static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
  1721. uint64_t value, MemTxAttrs attrs)
  1722. {
  1723. GICState *s = ARM_GIC(opaque);
  1724. int vcpu = cpu + GIC_NCPU;
  1725. trace_gic_hyp_write(addr, value);
  1726. switch (addr) {
  1727. case A_GICH_HCR: /* Hypervisor Control */
  1728. s->h_hcr[cpu] = value & GICH_HCR_MASK;
  1729. break;
  1730. case A_GICH_VMCR: /* Virtual Machine Control */
  1731. gic_vmcr_write(s, value, attrs);
  1732. break;
  1733. case A_GICH_APR: /* Active Priorities */
  1734. s->h_apr[cpu] = value;
  1735. s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
  1736. break;
  1737. case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
  1738. {
  1739. int lr_idx = (addr - A_GICH_LR0) / 4;
  1740. if (lr_idx > s->num_lrs) {
  1741. return MEMTX_OK;
  1742. }
  1743. s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
  1744. trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
  1745. break;
  1746. }
  1747. default:
  1748. qemu_log_mask(LOG_GUEST_ERROR,
  1749. "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
  1750. return MEMTX_OK;
  1751. }
  1752. gic_update_virt(s);
  1753. return MEMTX_OK;
  1754. }
  1755. static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
  1756. unsigned size, MemTxAttrs attrs)
  1757. {
  1758. GICState *s = (GICState *)opaque;
  1759. return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
  1760. }
  1761. static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
  1762. uint64_t value, unsigned size,
  1763. MemTxAttrs attrs)
  1764. {
  1765. GICState *s = (GICState *)opaque;
  1766. return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
  1767. }
  1768. static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
  1769. unsigned size, MemTxAttrs attrs)
  1770. {
  1771. GICState **backref = (GICState **)opaque;
  1772. GICState *s = *backref;
  1773. int id = (backref - s->backref);
  1774. return gic_hyp_read(s, id, addr, data, attrs);
  1775. }
  1776. static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
  1777. uint64_t value, unsigned size,
  1778. MemTxAttrs attrs)
  1779. {
  1780. GICState **backref = (GICState **)opaque;
  1781. GICState *s = *backref;
  1782. int id = (backref - s->backref);
  1783. return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
  1784. }
  1785. static const MemoryRegionOps gic_ops[2] = {
  1786. {
  1787. .read_with_attrs = gic_dist_read,
  1788. .write_with_attrs = gic_dist_write,
  1789. .endianness = DEVICE_NATIVE_ENDIAN,
  1790. },
  1791. {
  1792. .read_with_attrs = gic_thiscpu_read,
  1793. .write_with_attrs = gic_thiscpu_write,
  1794. .endianness = DEVICE_NATIVE_ENDIAN,
  1795. }
  1796. };
  1797. static const MemoryRegionOps gic_cpu_ops = {
  1798. .read_with_attrs = gic_do_cpu_read,
  1799. .write_with_attrs = gic_do_cpu_write,
  1800. .endianness = DEVICE_NATIVE_ENDIAN,
  1801. };
  1802. static const MemoryRegionOps gic_virt_ops[2] = {
  1803. {
  1804. .read_with_attrs = gic_thiscpu_hyp_read,
  1805. .write_with_attrs = gic_thiscpu_hyp_write,
  1806. .endianness = DEVICE_NATIVE_ENDIAN,
  1807. },
  1808. {
  1809. .read_with_attrs = gic_thisvcpu_read,
  1810. .write_with_attrs = gic_thisvcpu_write,
  1811. .endianness = DEVICE_NATIVE_ENDIAN,
  1812. }
  1813. };
  1814. static const MemoryRegionOps gic_viface_ops = {
  1815. .read_with_attrs = gic_do_hyp_read,
  1816. .write_with_attrs = gic_do_hyp_write,
  1817. .endianness = DEVICE_NATIVE_ENDIAN,
  1818. };
  1819. static void arm_gic_realize(DeviceState *dev, Error **errp)
  1820. {
  1821. /* Device instance realize function for the GIC sysbus device */
  1822. int i;
  1823. GICState *s = ARM_GIC(dev);
  1824. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1825. ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
  1826. Error *local_err = NULL;
  1827. agc->parent_realize(dev, &local_err);
  1828. if (local_err) {
  1829. error_propagate(errp, local_err);
  1830. return;
  1831. }
  1832. if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
  1833. error_setg(errp, "KVM with user space irqchip only works when the "
  1834. "host kernel supports KVM_CAP_ARM_USER_IRQ");
  1835. return;
  1836. }
  1837. if (s->n_prio_bits > GIC_MAX_PRIORITY_BITS ||
  1838. (s->virt_extn ? s->n_prio_bits < GIC_VIRT_MAX_GROUP_PRIO_BITS :
  1839. s->n_prio_bits < GIC_MIN_PRIORITY_BITS)) {
  1840. error_setg(errp, "num-priority-bits cannot be greater than %d"
  1841. " or less than %d", GIC_MAX_PRIORITY_BITS,
  1842. s->virt_extn ? GIC_VIRT_MAX_GROUP_PRIO_BITS :
  1843. GIC_MIN_PRIORITY_BITS);
  1844. return;
  1845. }
  1846. /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
  1847. * enabled, virtualization extensions related interfaces (main virtual
  1848. * interface (s->vifaceiomem[0]) and virtual CPU interface).
  1849. */
  1850. gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
  1851. /* Extra core-specific regions for the CPU interfaces. This is
  1852. * necessary for "franken-GIC" implementations, for example on
  1853. * Exynos 4.
  1854. * NB that the memory region size of 0x100 applies for the 11MPCore
  1855. * and also cores following the GIC v1 spec (ie A9).
  1856. * GIC v2 defines a larger memory region (0x1000) so this will need
  1857. * to be extended when we implement A15.
  1858. */
  1859. for (i = 0; i < s->num_cpu; i++) {
  1860. s->backref[i] = s;
  1861. memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
  1862. &s->backref[i], "gic_cpu", 0x100);
  1863. sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
  1864. }
  1865. /* Extra core-specific regions for virtual interfaces. This is required by
  1866. * the GICv2 specification.
  1867. */
  1868. if (s->virt_extn) {
  1869. for (i = 0; i < s->num_cpu; i++) {
  1870. memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
  1871. &gic_viface_ops, &s->backref[i],
  1872. "gic_viface", 0x200);
  1873. sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
  1874. }
  1875. }
  1876. }
  1877. static void arm_gic_class_init(ObjectClass *klass, void *data)
  1878. {
  1879. DeviceClass *dc = DEVICE_CLASS(klass);
  1880. ARMGICClass *agc = ARM_GIC_CLASS(klass);
  1881. device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
  1882. }
  1883. static const TypeInfo arm_gic_info = {
  1884. .name = TYPE_ARM_GIC,
  1885. .parent = TYPE_ARM_GIC_COMMON,
  1886. .instance_size = sizeof(GICState),
  1887. .class_init = arm_gic_class_init,
  1888. .class_size = sizeof(ARMGICClass),
  1889. };
  1890. static void arm_gic_register_types(void)
  1891. {
  1892. type_register_static(&arm_gic_info);
  1893. }
  1894. type_init(arm_gic_register_types)