arm_gic.c 67 KB

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