arm_gic.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  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. void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
  549. MemTxAttrs attrs)
  550. {
  551. if (s->security_extn && !attrs.secure) {
  552. if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
  553. return; /* Ignore Non-secure access of Group0 IRQ */
  554. }
  555. val = 0x80 | (val >> 1); /* Non-secure view */
  556. }
  557. if (irq < GIC_INTERNAL) {
  558. s->priority1[irq][cpu] = val;
  559. } else {
  560. s->priority2[(irq) - GIC_INTERNAL] = val;
  561. }
  562. }
  563. static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
  564. MemTxAttrs attrs)
  565. {
  566. uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
  567. if (s->security_extn && !attrs.secure) {
  568. if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
  569. return 0; /* Non-secure access cannot read priority of Group0 IRQ */
  570. }
  571. prio = (prio << 1) & 0xff; /* Non-secure view */
  572. }
  573. return prio;
  574. }
  575. static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
  576. MemTxAttrs attrs)
  577. {
  578. if (gic_cpu_ns_access(s, cpu, attrs)) {
  579. if (s->priority_mask[cpu] & 0x80) {
  580. /* Priority Mask in upper half */
  581. pmask = 0x80 | (pmask >> 1);
  582. } else {
  583. /* Non-secure write ignored if priority mask is in lower half */
  584. return;
  585. }
  586. }
  587. s->priority_mask[cpu] = pmask;
  588. }
  589. static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
  590. {
  591. uint32_t pmask = s->priority_mask[cpu];
  592. if (gic_cpu_ns_access(s, cpu, attrs)) {
  593. if (pmask & 0x80) {
  594. /* Priority Mask in upper half, return Non-secure view */
  595. pmask = (pmask << 1) & 0xff;
  596. } else {
  597. /* Priority Mask in lower half, RAZ */
  598. pmask = 0;
  599. }
  600. }
  601. return pmask;
  602. }
  603. static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
  604. {
  605. uint32_t ret = s->cpu_ctlr[cpu];
  606. if (gic_cpu_ns_access(s, cpu, attrs)) {
  607. /* Construct the NS banked view of GICC_CTLR from the correct
  608. * bits of the S banked view. We don't need to move the bypass
  609. * control bits because we don't implement that (IMPDEF) part
  610. * of the GIC architecture.
  611. */
  612. ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
  613. }
  614. return ret;
  615. }
  616. static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
  617. MemTxAttrs attrs)
  618. {
  619. uint32_t mask;
  620. if (gic_cpu_ns_access(s, cpu, attrs)) {
  621. /* The NS view can only write certain bits in the register;
  622. * the rest are unchanged
  623. */
  624. mask = GICC_CTLR_EN_GRP1;
  625. if (s->revision == 2) {
  626. mask |= GICC_CTLR_EOIMODE_NS;
  627. }
  628. s->cpu_ctlr[cpu] &= ~mask;
  629. s->cpu_ctlr[cpu] |= (value << 1) & mask;
  630. } else {
  631. if (s->revision == 2) {
  632. mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
  633. } else {
  634. mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
  635. }
  636. s->cpu_ctlr[cpu] = value & mask;
  637. }
  638. DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
  639. "Group1 Interrupts %sabled\n", cpu,
  640. (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
  641. (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
  642. }
  643. static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
  644. {
  645. if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
  646. /* Idle priority */
  647. return 0xff;
  648. }
  649. if (gic_cpu_ns_access(s, cpu, attrs)) {
  650. if (s->running_priority[cpu] & 0x80) {
  651. /* Running priority in upper half of range: return the Non-secure
  652. * view of the priority.
  653. */
  654. return s->running_priority[cpu] << 1;
  655. } else {
  656. /* Running priority in lower half of range: RAZ */
  657. return 0;
  658. }
  659. } else {
  660. return s->running_priority[cpu];
  661. }
  662. }
  663. /* Return true if we should split priority drop and interrupt deactivation,
  664. * ie whether the relevant EOIMode bit is set.
  665. */
  666. static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
  667. {
  668. if (s->revision != 2) {
  669. /* Before GICv2 prio-drop and deactivate are not separable */
  670. return false;
  671. }
  672. if (gic_cpu_ns_access(s, cpu, attrs)) {
  673. return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
  674. }
  675. return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
  676. }
  677. static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
  678. {
  679. int group;
  680. if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
  681. /*
  682. * This handles two cases:
  683. * 1. If software writes the ID of a spurious interrupt [ie 1023]
  684. * to the GICC_DIR, the GIC ignores that write.
  685. * 2. If software writes the number of a non-existent interrupt
  686. * this must be a subcase of "value written is not an active interrupt"
  687. * and so this is UNPREDICTABLE. We choose to ignore it. For vCPUs,
  688. * all IRQs potentially exist, so this limit does not apply.
  689. */
  690. return;
  691. }
  692. if (!gic_eoi_split(s, cpu, attrs)) {
  693. /* This is UNPREDICTABLE; we choose to ignore it */
  694. qemu_log_mask(LOG_GUEST_ERROR,
  695. "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
  696. return;
  697. }
  698. if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
  699. /* This vIRQ does not have an LR entry which is either active or
  700. * pending and active. Increment EOICount and ignore the write.
  701. */
  702. int rcpu = gic_get_vcpu_real_id(cpu);
  703. s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
  704. /* Update the virtual interface in case a maintenance interrupt should
  705. * be raised.
  706. */
  707. gic_update_virt(s);
  708. return;
  709. }
  710. group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
  711. if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
  712. DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
  713. return;
  714. }
  715. gic_clear_active(s, irq, cpu);
  716. }
  717. static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
  718. {
  719. int cm = 1 << cpu;
  720. int group;
  721. DPRINTF("EOI %d\n", irq);
  722. if (gic_is_vcpu(cpu)) {
  723. /* The call to gic_prio_drop() will clear a bit in GICH_APR iff the
  724. * running prio is < 0x100.
  725. */
  726. bool prio_drop = s->running_priority[cpu] < 0x100;
  727. if (irq >= GIC_MAXIRQ) {
  728. /* Ignore spurious interrupt */
  729. return;
  730. }
  731. gic_drop_prio(s, cpu, 0);
  732. if (!gic_eoi_split(s, cpu, attrs)) {
  733. bool valid = gic_virq_is_valid(s, irq, cpu);
  734. if (prio_drop && !valid) {
  735. /* We are in a situation where:
  736. * - V_CTRL.EOIMode is false (no EOI split),
  737. * - The call to gic_drop_prio() cleared a bit in GICH_APR,
  738. * - This vIRQ does not have an LR entry which is either
  739. * active or pending and active.
  740. * In that case, we must increment EOICount.
  741. */
  742. int rcpu = gic_get_vcpu_real_id(cpu);
  743. s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
  744. } else if (valid) {
  745. gic_clear_active(s, irq, cpu);
  746. }
  747. }
  748. gic_update_virt(s);
  749. return;
  750. }
  751. if (irq >= s->num_irq) {
  752. /* This handles two cases:
  753. * 1. If software writes the ID of a spurious interrupt [ie 1023]
  754. * to the GICC_EOIR, the GIC ignores that write.
  755. * 2. If software writes the number of a non-existent interrupt
  756. * this must be a subcase of "value written does not match the last
  757. * valid interrupt value read from the Interrupt Acknowledge
  758. * register" and so this is UNPREDICTABLE. We choose to ignore it.
  759. */
  760. return;
  761. }
  762. if (s->running_priority[cpu] == 0x100) {
  763. return; /* No active IRQ. */
  764. }
  765. if (s->revision == REV_11MPCORE) {
  766. /* Mark level triggered interrupts as pending if they are still
  767. raised. */
  768. if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
  769. && GIC_DIST_TEST_LEVEL(irq, cm)
  770. && (GIC_DIST_TARGET(irq) & cm) != 0) {
  771. DPRINTF("Set %d pending mask %x\n", irq, cm);
  772. GIC_DIST_SET_PENDING(irq, cm);
  773. }
  774. }
  775. group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
  776. if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
  777. DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
  778. return;
  779. }
  780. /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
  781. * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
  782. * i.e. go ahead and complete the irq anyway.
  783. */
  784. gic_drop_prio(s, cpu, group);
  785. /* In GICv2 the guest can choose to split priority-drop and deactivate */
  786. if (!gic_eoi_split(s, cpu, attrs)) {
  787. gic_clear_active(s, irq, cpu);
  788. }
  789. gic_update(s);
  790. }
  791. static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
  792. {
  793. GICState *s = (GICState *)opaque;
  794. uint32_t res;
  795. int irq;
  796. int i;
  797. int cpu;
  798. int cm;
  799. int mask;
  800. cpu = gic_get_current_cpu(s);
  801. cm = 1 << cpu;
  802. if (offset < 0x100) {
  803. if (offset == 0) { /* GICD_CTLR */
  804. if (s->security_extn && !attrs.secure) {
  805. /* The NS bank of this register is just an alias of the
  806. * EnableGrp1 bit in the S bank version.
  807. */
  808. return extract32(s->ctlr, 1, 1);
  809. } else {
  810. return s->ctlr;
  811. }
  812. }
  813. if (offset == 4)
  814. /* Interrupt Controller Type Register */
  815. return ((s->num_irq / 32) - 1)
  816. | ((s->num_cpu - 1) << 5)
  817. | (s->security_extn << 10);
  818. if (offset < 0x08)
  819. return 0;
  820. if (offset >= 0x80) {
  821. /* Interrupt Group Registers: these RAZ/WI if this is an NS
  822. * access to a GIC with the security extensions, or if the GIC
  823. * doesn't have groups at all.
  824. */
  825. res = 0;
  826. if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
  827. /* Every byte offset holds 8 group status bits */
  828. irq = (offset - 0x080) * 8;
  829. if (irq >= s->num_irq) {
  830. goto bad_reg;
  831. }
  832. for (i = 0; i < 8; i++) {
  833. if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
  834. res |= (1 << i);
  835. }
  836. }
  837. }
  838. return res;
  839. }
  840. goto bad_reg;
  841. } else if (offset < 0x200) {
  842. /* Interrupt Set/Clear Enable. */
  843. if (offset < 0x180)
  844. irq = (offset - 0x100) * 8;
  845. else
  846. irq = (offset - 0x180) * 8;
  847. if (irq >= s->num_irq)
  848. goto bad_reg;
  849. res = 0;
  850. for (i = 0; i < 8; i++) {
  851. if (s->security_extn && !attrs.secure &&
  852. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  853. continue; /* Ignore Non-secure access of Group0 IRQ */
  854. }
  855. if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
  856. res |= (1 << i);
  857. }
  858. }
  859. } else if (offset < 0x300) {
  860. /* Interrupt Set/Clear Pending. */
  861. if (offset < 0x280)
  862. irq = (offset - 0x200) * 8;
  863. else
  864. irq = (offset - 0x280) * 8;
  865. if (irq >= s->num_irq)
  866. goto bad_reg;
  867. res = 0;
  868. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  869. for (i = 0; i < 8; i++) {
  870. if (s->security_extn && !attrs.secure &&
  871. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  872. continue; /* Ignore Non-secure access of Group0 IRQ */
  873. }
  874. if (gic_test_pending(s, irq + i, mask)) {
  875. res |= (1 << i);
  876. }
  877. }
  878. } else if (offset < 0x400) {
  879. /* Interrupt Set/Clear Active. */
  880. if (offset < 0x380) {
  881. irq = (offset - 0x300) * 8;
  882. } else if (s->revision == 2) {
  883. irq = (offset - 0x380) * 8;
  884. } else {
  885. goto bad_reg;
  886. }
  887. if (irq >= s->num_irq)
  888. goto bad_reg;
  889. res = 0;
  890. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  891. for (i = 0; i < 8; i++) {
  892. if (s->security_extn && !attrs.secure &&
  893. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  894. continue; /* Ignore Non-secure access of Group0 IRQ */
  895. }
  896. if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
  897. res |= (1 << i);
  898. }
  899. }
  900. } else if (offset < 0x800) {
  901. /* Interrupt Priority. */
  902. irq = (offset - 0x400);
  903. if (irq >= s->num_irq)
  904. goto bad_reg;
  905. res = gic_dist_get_priority(s, cpu, irq, attrs);
  906. } else if (offset < 0xc00) {
  907. /* Interrupt CPU Target. */
  908. if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
  909. /* For uniprocessor GICs these RAZ/WI */
  910. res = 0;
  911. } else {
  912. irq = (offset - 0x800);
  913. if (irq >= s->num_irq) {
  914. goto bad_reg;
  915. }
  916. if (irq < 29 && s->revision == REV_11MPCORE) {
  917. res = 0;
  918. } else if (irq < GIC_INTERNAL) {
  919. res = cm;
  920. } else {
  921. res = GIC_DIST_TARGET(irq);
  922. }
  923. }
  924. } else if (offset < 0xf00) {
  925. /* Interrupt Configuration. */
  926. irq = (offset - 0xc00) * 4;
  927. if (irq >= s->num_irq)
  928. goto bad_reg;
  929. res = 0;
  930. for (i = 0; i < 4; i++) {
  931. if (s->security_extn && !attrs.secure &&
  932. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  933. continue; /* Ignore Non-secure access of Group0 IRQ */
  934. }
  935. if (GIC_DIST_TEST_MODEL(irq + i)) {
  936. res |= (1 << (i * 2));
  937. }
  938. if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
  939. res |= (2 << (i * 2));
  940. }
  941. }
  942. } else if (offset < 0xf10) {
  943. goto bad_reg;
  944. } else if (offset < 0xf30) {
  945. if (s->revision == REV_11MPCORE) {
  946. goto bad_reg;
  947. }
  948. if (offset < 0xf20) {
  949. /* GICD_CPENDSGIRn */
  950. irq = (offset - 0xf10);
  951. } else {
  952. irq = (offset - 0xf20);
  953. /* GICD_SPENDSGIRn */
  954. }
  955. if (s->security_extn && !attrs.secure &&
  956. !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
  957. res = 0; /* Ignore Non-secure access of Group0 IRQ */
  958. } else {
  959. res = s->sgi_pending[irq][cpu];
  960. }
  961. } else if (offset < 0xfd0) {
  962. goto bad_reg;
  963. } else if (offset < 0x1000) {
  964. if (offset & 3) {
  965. res = 0;
  966. } else {
  967. switch (s->revision) {
  968. case REV_11MPCORE:
  969. res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
  970. break;
  971. case 1:
  972. res = gic_id_gicv1[(offset - 0xfd0) >> 2];
  973. break;
  974. case 2:
  975. res = gic_id_gicv2[(offset - 0xfd0) >> 2];
  976. break;
  977. default:
  978. res = 0;
  979. }
  980. }
  981. } else {
  982. g_assert_not_reached();
  983. }
  984. return res;
  985. bad_reg:
  986. qemu_log_mask(LOG_GUEST_ERROR,
  987. "gic_dist_readb: Bad offset %x\n", (int)offset);
  988. return 0;
  989. }
  990. static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
  991. unsigned size, MemTxAttrs attrs)
  992. {
  993. switch (size) {
  994. case 1:
  995. *data = gic_dist_readb(opaque, offset, attrs);
  996. break;
  997. case 2:
  998. *data = gic_dist_readb(opaque, offset, attrs);
  999. *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
  1000. break;
  1001. case 4:
  1002. *data = gic_dist_readb(opaque, offset, attrs);
  1003. *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
  1004. *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
  1005. *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
  1006. break;
  1007. default:
  1008. return MEMTX_ERROR;
  1009. }
  1010. trace_gic_dist_read(offset, size, *data);
  1011. return MEMTX_OK;
  1012. }
  1013. static void gic_dist_writeb(void *opaque, hwaddr offset,
  1014. uint32_t value, MemTxAttrs attrs)
  1015. {
  1016. GICState *s = (GICState *)opaque;
  1017. int irq;
  1018. int i;
  1019. int cpu;
  1020. cpu = gic_get_current_cpu(s);
  1021. if (offset < 0x100) {
  1022. if (offset == 0) {
  1023. if (s->security_extn && !attrs.secure) {
  1024. /* NS version is just an alias of the S version's bit 1 */
  1025. s->ctlr = deposit32(s->ctlr, 1, 1, value);
  1026. } else if (gic_has_groups(s)) {
  1027. s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
  1028. } else {
  1029. s->ctlr = value & GICD_CTLR_EN_GRP0;
  1030. }
  1031. DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
  1032. s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
  1033. s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
  1034. } else if (offset < 4) {
  1035. /* ignored. */
  1036. } else if (offset >= 0x80) {
  1037. /* Interrupt Group Registers: RAZ/WI for NS access to secure
  1038. * GIC, or for GICs without groups.
  1039. */
  1040. if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
  1041. /* Every byte offset holds 8 group status bits */
  1042. irq = (offset - 0x80) * 8;
  1043. if (irq >= s->num_irq) {
  1044. goto bad_reg;
  1045. }
  1046. for (i = 0; i < 8; i++) {
  1047. /* Group bits are banked for private interrupts */
  1048. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  1049. if (value & (1 << i)) {
  1050. /* Group1 (Non-secure) */
  1051. GIC_DIST_SET_GROUP(irq + i, cm);
  1052. } else {
  1053. /* Group0 (Secure) */
  1054. GIC_DIST_CLEAR_GROUP(irq + i, cm);
  1055. }
  1056. }
  1057. }
  1058. } else {
  1059. goto bad_reg;
  1060. }
  1061. } else if (offset < 0x180) {
  1062. /* Interrupt Set Enable. */
  1063. irq = (offset - 0x100) * 8;
  1064. if (irq >= s->num_irq)
  1065. goto bad_reg;
  1066. if (irq < GIC_NR_SGIS) {
  1067. value = 0xff;
  1068. }
  1069. for (i = 0; i < 8; i++) {
  1070. if (value & (1 << i)) {
  1071. int mask =
  1072. (irq < GIC_INTERNAL) ? (1 << cpu)
  1073. : GIC_DIST_TARGET(irq + i);
  1074. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  1075. if (s->security_extn && !attrs.secure &&
  1076. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1077. continue; /* Ignore Non-secure access of Group0 IRQ */
  1078. }
  1079. if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
  1080. DPRINTF("Enabled IRQ %d\n", irq + i);
  1081. trace_gic_enable_irq(irq + i);
  1082. }
  1083. GIC_DIST_SET_ENABLED(irq + i, cm);
  1084. /* If a raised level triggered IRQ enabled then mark
  1085. is as pending. */
  1086. if (GIC_DIST_TEST_LEVEL(irq + i, mask)
  1087. && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
  1088. DPRINTF("Set %d pending mask %x\n", irq + i, mask);
  1089. GIC_DIST_SET_PENDING(irq + i, mask);
  1090. }
  1091. }
  1092. }
  1093. } else if (offset < 0x200) {
  1094. /* Interrupt Clear Enable. */
  1095. irq = (offset - 0x180) * 8;
  1096. if (irq >= s->num_irq)
  1097. goto bad_reg;
  1098. if (irq < GIC_NR_SGIS) {
  1099. value = 0;
  1100. }
  1101. for (i = 0; i < 8; i++) {
  1102. if (value & (1 << i)) {
  1103. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  1104. if (s->security_extn && !attrs.secure &&
  1105. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1106. continue; /* Ignore Non-secure access of Group0 IRQ */
  1107. }
  1108. if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
  1109. DPRINTF("Disabled IRQ %d\n", irq + i);
  1110. trace_gic_disable_irq(irq + i);
  1111. }
  1112. GIC_DIST_CLEAR_ENABLED(irq + i, cm);
  1113. }
  1114. }
  1115. } else if (offset < 0x280) {
  1116. /* Interrupt Set Pending. */
  1117. irq = (offset - 0x200) * 8;
  1118. if (irq >= s->num_irq)
  1119. goto bad_reg;
  1120. if (irq < GIC_NR_SGIS) {
  1121. value = 0;
  1122. }
  1123. for (i = 0; i < 8; i++) {
  1124. if (value & (1 << i)) {
  1125. if (s->security_extn && !attrs.secure &&
  1126. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1127. continue; /* Ignore Non-secure access of Group0 IRQ */
  1128. }
  1129. GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
  1130. }
  1131. }
  1132. } else if (offset < 0x300) {
  1133. /* Interrupt Clear Pending. */
  1134. irq = (offset - 0x280) * 8;
  1135. if (irq >= s->num_irq)
  1136. goto bad_reg;
  1137. if (irq < GIC_NR_SGIS) {
  1138. value = 0;
  1139. }
  1140. for (i = 0; i < 8; 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. /* ??? This currently clears the pending bit for all CPUs, even
  1146. for per-CPU interrupts. It's unclear whether this is the
  1147. corect behavior. */
  1148. if (value & (1 << i)) {
  1149. GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
  1150. }
  1151. }
  1152. } else if (offset < 0x380) {
  1153. /* Interrupt Set Active. */
  1154. if (s->revision != 2) {
  1155. goto bad_reg;
  1156. }
  1157. irq = (offset - 0x300) * 8;
  1158. if (irq >= s->num_irq) {
  1159. goto bad_reg;
  1160. }
  1161. /* This register is banked per-cpu for PPIs */
  1162. int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
  1163. for (i = 0; i < 8; 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. if (value & (1 << i)) {
  1169. GIC_DIST_SET_ACTIVE(irq + i, cm);
  1170. }
  1171. }
  1172. } else if (offset < 0x400) {
  1173. /* Interrupt Clear Active. */
  1174. if (s->revision != 2) {
  1175. goto bad_reg;
  1176. }
  1177. irq = (offset - 0x380) * 8;
  1178. if (irq >= s->num_irq) {
  1179. goto bad_reg;
  1180. }
  1181. /* This register is banked per-cpu for PPIs */
  1182. int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
  1183. for (i = 0; i < 8; i++) {
  1184. if (s->security_extn && !attrs.secure &&
  1185. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1186. continue; /* Ignore Non-secure access of Group0 IRQ */
  1187. }
  1188. if (value & (1 << i)) {
  1189. GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
  1190. }
  1191. }
  1192. } else if (offset < 0x800) {
  1193. /* Interrupt Priority. */
  1194. irq = (offset - 0x400);
  1195. if (irq >= s->num_irq)
  1196. goto bad_reg;
  1197. gic_dist_set_priority(s, cpu, irq, value, attrs);
  1198. } else if (offset < 0xc00) {
  1199. /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
  1200. * annoying exception of the 11MPCore's GIC.
  1201. */
  1202. if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
  1203. irq = (offset - 0x800);
  1204. if (irq >= s->num_irq) {
  1205. goto bad_reg;
  1206. }
  1207. if (irq < 29 && s->revision == REV_11MPCORE) {
  1208. value = 0;
  1209. } else if (irq < GIC_INTERNAL) {
  1210. value = ALL_CPU_MASK;
  1211. }
  1212. s->irq_target[irq] = value & ALL_CPU_MASK;
  1213. }
  1214. } else if (offset < 0xf00) {
  1215. /* Interrupt Configuration. */
  1216. irq = (offset - 0xc00) * 4;
  1217. if (irq >= s->num_irq)
  1218. goto bad_reg;
  1219. if (irq < GIC_NR_SGIS)
  1220. value |= 0xaa;
  1221. for (i = 0; i < 4; i++) {
  1222. if (s->security_extn && !attrs.secure &&
  1223. !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
  1224. continue; /* Ignore Non-secure access of Group0 IRQ */
  1225. }
  1226. if (s->revision == REV_11MPCORE) {
  1227. if (value & (1 << (i * 2))) {
  1228. GIC_DIST_SET_MODEL(irq + i);
  1229. } else {
  1230. GIC_DIST_CLEAR_MODEL(irq + i);
  1231. }
  1232. }
  1233. if (value & (2 << (i * 2))) {
  1234. GIC_DIST_SET_EDGE_TRIGGER(irq + i);
  1235. } else {
  1236. GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
  1237. }
  1238. }
  1239. } else if (offset < 0xf10) {
  1240. /* 0xf00 is only handled for 32-bit writes. */
  1241. goto bad_reg;
  1242. } else if (offset < 0xf20) {
  1243. /* GICD_CPENDSGIRn */
  1244. if (s->revision == REV_11MPCORE) {
  1245. goto bad_reg;
  1246. }
  1247. irq = (offset - 0xf10);
  1248. if (!s->security_extn || attrs.secure ||
  1249. GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
  1250. s->sgi_pending[irq][cpu] &= ~value;
  1251. if (s->sgi_pending[irq][cpu] == 0) {
  1252. GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
  1253. }
  1254. }
  1255. } else if (offset < 0xf30) {
  1256. /* GICD_SPENDSGIRn */
  1257. if (s->revision == REV_11MPCORE) {
  1258. goto bad_reg;
  1259. }
  1260. irq = (offset - 0xf20);
  1261. if (!s->security_extn || attrs.secure ||
  1262. GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
  1263. GIC_DIST_SET_PENDING(irq, 1 << cpu);
  1264. s->sgi_pending[irq][cpu] |= value;
  1265. }
  1266. } else {
  1267. goto bad_reg;
  1268. }
  1269. gic_update(s);
  1270. return;
  1271. bad_reg:
  1272. qemu_log_mask(LOG_GUEST_ERROR,
  1273. "gic_dist_writeb: Bad offset %x\n", (int)offset);
  1274. }
  1275. static void gic_dist_writew(void *opaque, hwaddr offset,
  1276. uint32_t value, MemTxAttrs attrs)
  1277. {
  1278. gic_dist_writeb(opaque, offset, value & 0xff, attrs);
  1279. gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
  1280. }
  1281. static void gic_dist_writel(void *opaque, hwaddr offset,
  1282. uint32_t value, MemTxAttrs attrs)
  1283. {
  1284. GICState *s = (GICState *)opaque;
  1285. if (offset == 0xf00) {
  1286. int cpu;
  1287. int irq;
  1288. int mask;
  1289. int target_cpu;
  1290. cpu = gic_get_current_cpu(s);
  1291. irq = value & 0x3ff;
  1292. switch ((value >> 24) & 3) {
  1293. case 0:
  1294. mask = (value >> 16) & ALL_CPU_MASK;
  1295. break;
  1296. case 1:
  1297. mask = ALL_CPU_MASK ^ (1 << cpu);
  1298. break;
  1299. case 2:
  1300. mask = 1 << cpu;
  1301. break;
  1302. default:
  1303. DPRINTF("Bad Soft Int target filter\n");
  1304. mask = ALL_CPU_MASK;
  1305. break;
  1306. }
  1307. GIC_DIST_SET_PENDING(irq, mask);
  1308. target_cpu = ctz32(mask);
  1309. while (target_cpu < GIC_NCPU) {
  1310. s->sgi_pending[irq][target_cpu] |= (1 << cpu);
  1311. mask &= ~(1 << target_cpu);
  1312. target_cpu = ctz32(mask);
  1313. }
  1314. gic_update(s);
  1315. return;
  1316. }
  1317. gic_dist_writew(opaque, offset, value & 0xffff, attrs);
  1318. gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
  1319. }
  1320. static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
  1321. unsigned size, MemTxAttrs attrs)
  1322. {
  1323. trace_gic_dist_write(offset, size, data);
  1324. switch (size) {
  1325. case 1:
  1326. gic_dist_writeb(opaque, offset, data, attrs);
  1327. return MEMTX_OK;
  1328. case 2:
  1329. gic_dist_writew(opaque, offset, data, attrs);
  1330. return MEMTX_OK;
  1331. case 4:
  1332. gic_dist_writel(opaque, offset, data, attrs);
  1333. return MEMTX_OK;
  1334. default:
  1335. return MEMTX_ERROR;
  1336. }
  1337. }
  1338. static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
  1339. {
  1340. /* Return the Nonsecure view of GICC_APR<regno>. This is the
  1341. * second half of GICC_NSAPR.
  1342. */
  1343. switch (GIC_MIN_BPR) {
  1344. case 0:
  1345. if (regno < 2) {
  1346. return s->nsapr[regno + 2][cpu];
  1347. }
  1348. break;
  1349. case 1:
  1350. if (regno == 0) {
  1351. return s->nsapr[regno + 1][cpu];
  1352. }
  1353. break;
  1354. case 2:
  1355. if (regno == 0) {
  1356. return extract32(s->nsapr[0][cpu], 16, 16);
  1357. }
  1358. break;
  1359. case 3:
  1360. if (regno == 0) {
  1361. return extract32(s->nsapr[0][cpu], 8, 8);
  1362. }
  1363. break;
  1364. default:
  1365. g_assert_not_reached();
  1366. }
  1367. return 0;
  1368. }
  1369. static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
  1370. uint32_t value)
  1371. {
  1372. /* Write the Nonsecure view of GICC_APR<regno>. */
  1373. switch (GIC_MIN_BPR) {
  1374. case 0:
  1375. if (regno < 2) {
  1376. s->nsapr[regno + 2][cpu] = value;
  1377. }
  1378. break;
  1379. case 1:
  1380. if (regno == 0) {
  1381. s->nsapr[regno + 1][cpu] = value;
  1382. }
  1383. break;
  1384. case 2:
  1385. if (regno == 0) {
  1386. s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
  1387. }
  1388. break;
  1389. case 3:
  1390. if (regno == 0) {
  1391. s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
  1392. }
  1393. break;
  1394. default:
  1395. g_assert_not_reached();
  1396. }
  1397. }
  1398. static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
  1399. uint64_t *data, MemTxAttrs attrs)
  1400. {
  1401. switch (offset) {
  1402. case 0x00: /* Control */
  1403. *data = gic_get_cpu_control(s, cpu, attrs);
  1404. break;
  1405. case 0x04: /* Priority mask */
  1406. *data = gic_get_priority_mask(s, cpu, attrs);
  1407. break;
  1408. case 0x08: /* Binary Point */
  1409. if (gic_cpu_ns_access(s, cpu, attrs)) {
  1410. if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
  1411. /* NS view of BPR when CBPR is 1 */
  1412. *data = MIN(s->bpr[cpu] + 1, 7);
  1413. } else {
  1414. /* BPR is banked. Non-secure copy stored in ABPR. */
  1415. *data = s->abpr[cpu];
  1416. }
  1417. } else {
  1418. *data = s->bpr[cpu];
  1419. }
  1420. break;
  1421. case 0x0c: /* Acknowledge */
  1422. *data = gic_acknowledge_irq(s, cpu, attrs);
  1423. break;
  1424. case 0x14: /* Running Priority */
  1425. *data = gic_get_running_priority(s, cpu, attrs);
  1426. break;
  1427. case 0x18: /* Highest Pending Interrupt */
  1428. *data = gic_get_current_pending_irq(s, cpu, attrs);
  1429. break;
  1430. case 0x1c: /* Aliased Binary Point */
  1431. /* GIC v2, no security: ABPR
  1432. * GIC v1, no security: not implemented (RAZ/WI)
  1433. * With security extensions, secure access: ABPR (alias of NS BPR)
  1434. * With security extensions, nonsecure access: RAZ/WI
  1435. */
  1436. if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
  1437. *data = 0;
  1438. } else {
  1439. *data = s->abpr[cpu];
  1440. }
  1441. break;
  1442. case 0xd0: case 0xd4: case 0xd8: case 0xdc:
  1443. {
  1444. int regno = (offset - 0xd0) / 4;
  1445. int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
  1446. if (regno >= nr_aprs || s->revision != 2) {
  1447. *data = 0;
  1448. } else if (gic_is_vcpu(cpu)) {
  1449. *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
  1450. } else if (gic_cpu_ns_access(s, cpu, attrs)) {
  1451. /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
  1452. *data = gic_apr_ns_view(s, regno, cpu);
  1453. } else {
  1454. *data = s->apr[regno][cpu];
  1455. }
  1456. break;
  1457. }
  1458. case 0xe0: case 0xe4: case 0xe8: case 0xec:
  1459. {
  1460. int regno = (offset - 0xe0) / 4;
  1461. if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
  1462. gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
  1463. *data = 0;
  1464. } else {
  1465. *data = s->nsapr[regno][cpu];
  1466. }
  1467. break;
  1468. }
  1469. default:
  1470. qemu_log_mask(LOG_GUEST_ERROR,
  1471. "gic_cpu_read: Bad offset %x\n", (int)offset);
  1472. *data = 0;
  1473. break;
  1474. }
  1475. trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
  1476. gic_get_vcpu_real_id(cpu), offset, *data);
  1477. return MEMTX_OK;
  1478. }
  1479. static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
  1480. uint32_t value, MemTxAttrs attrs)
  1481. {
  1482. trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
  1483. gic_get_vcpu_real_id(cpu), offset, value);
  1484. switch (offset) {
  1485. case 0x00: /* Control */
  1486. gic_set_cpu_control(s, cpu, value, attrs);
  1487. break;
  1488. case 0x04: /* Priority mask */
  1489. gic_set_priority_mask(s, cpu, value, attrs);
  1490. break;
  1491. case 0x08: /* Binary Point */
  1492. if (gic_cpu_ns_access(s, cpu, attrs)) {
  1493. if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
  1494. /* WI when CBPR is 1 */
  1495. return MEMTX_OK;
  1496. } else {
  1497. s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
  1498. }
  1499. } else {
  1500. int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
  1501. s->bpr[cpu] = MAX(value & 0x7, min_bpr);
  1502. }
  1503. break;
  1504. case 0x10: /* End Of Interrupt */
  1505. gic_complete_irq(s, cpu, value & 0x3ff, attrs);
  1506. return MEMTX_OK;
  1507. case 0x1c: /* Aliased Binary Point */
  1508. if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
  1509. /* unimplemented, or NS access: RAZ/WI */
  1510. return MEMTX_OK;
  1511. } else {
  1512. s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
  1513. }
  1514. break;
  1515. case 0xd0: case 0xd4: case 0xd8: case 0xdc:
  1516. {
  1517. int regno = (offset - 0xd0) / 4;
  1518. int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
  1519. if (regno >= nr_aprs || s->revision != 2) {
  1520. return MEMTX_OK;
  1521. }
  1522. if (gic_is_vcpu(cpu)) {
  1523. s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
  1524. } else if (gic_cpu_ns_access(s, cpu, attrs)) {
  1525. /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
  1526. gic_apr_write_ns_view(s, regno, cpu, value);
  1527. } else {
  1528. s->apr[regno][cpu] = value;
  1529. }
  1530. break;
  1531. }
  1532. case 0xe0: case 0xe4: case 0xe8: case 0xec:
  1533. {
  1534. int regno = (offset - 0xe0) / 4;
  1535. if (regno >= GIC_NR_APRS || s->revision != 2) {
  1536. return MEMTX_OK;
  1537. }
  1538. if (gic_is_vcpu(cpu)) {
  1539. return MEMTX_OK;
  1540. }
  1541. if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
  1542. return MEMTX_OK;
  1543. }
  1544. s->nsapr[regno][cpu] = value;
  1545. break;
  1546. }
  1547. case 0x1000:
  1548. /* GICC_DIR */
  1549. gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
  1550. break;
  1551. default:
  1552. qemu_log_mask(LOG_GUEST_ERROR,
  1553. "gic_cpu_write: Bad offset %x\n", (int)offset);
  1554. return MEMTX_OK;
  1555. }
  1556. if (gic_is_vcpu(cpu)) {
  1557. gic_update_virt(s);
  1558. } else {
  1559. gic_update(s);
  1560. }
  1561. return MEMTX_OK;
  1562. }
  1563. /* Wrappers to read/write the GIC CPU interface for the current CPU */
  1564. static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
  1565. unsigned size, MemTxAttrs attrs)
  1566. {
  1567. GICState *s = (GICState *)opaque;
  1568. return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
  1569. }
  1570. static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
  1571. uint64_t value, unsigned size,
  1572. MemTxAttrs attrs)
  1573. {
  1574. GICState *s = (GICState *)opaque;
  1575. return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
  1576. }
  1577. /* Wrappers to read/write the GIC CPU interface for a specific CPU.
  1578. * These just decode the opaque pointer into GICState* + cpu id.
  1579. */
  1580. static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
  1581. unsigned size, MemTxAttrs attrs)
  1582. {
  1583. GICState **backref = (GICState **)opaque;
  1584. GICState *s = *backref;
  1585. int id = (backref - s->backref);
  1586. return gic_cpu_read(s, id, addr, data, attrs);
  1587. }
  1588. static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
  1589. uint64_t value, unsigned size,
  1590. MemTxAttrs attrs)
  1591. {
  1592. GICState **backref = (GICState **)opaque;
  1593. GICState *s = *backref;
  1594. int id = (backref - s->backref);
  1595. return gic_cpu_write(s, id, addr, value, attrs);
  1596. }
  1597. static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
  1598. unsigned size, MemTxAttrs attrs)
  1599. {
  1600. GICState *s = (GICState *)opaque;
  1601. return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
  1602. }
  1603. static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
  1604. uint64_t value, unsigned size,
  1605. MemTxAttrs attrs)
  1606. {
  1607. GICState *s = (GICState *)opaque;
  1608. return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
  1609. }
  1610. static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
  1611. {
  1612. int lr_idx;
  1613. uint32_t ret = 0;
  1614. for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
  1615. uint32_t *entry = &s->h_lr[lr_idx][cpu];
  1616. ret = deposit32(ret, lr_idx - lr_start, 1,
  1617. gic_lr_entry_is_eoi(*entry));
  1618. }
  1619. return ret;
  1620. }
  1621. static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
  1622. {
  1623. int lr_idx;
  1624. uint32_t ret = 0;
  1625. for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
  1626. uint32_t *entry = &s->h_lr[lr_idx][cpu];
  1627. ret = deposit32(ret, lr_idx - lr_start, 1,
  1628. gic_lr_entry_is_free(*entry));
  1629. }
  1630. return ret;
  1631. }
  1632. static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
  1633. {
  1634. int vcpu = gic_get_current_vcpu(s);
  1635. uint32_t ctlr;
  1636. uint32_t abpr;
  1637. uint32_t bpr;
  1638. uint32_t prio_mask;
  1639. ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
  1640. abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
  1641. bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
  1642. prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
  1643. gic_set_cpu_control(s, vcpu, ctlr, attrs);
  1644. s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
  1645. s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
  1646. gic_set_priority_mask(s, vcpu, prio_mask, attrs);
  1647. }
  1648. static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
  1649. uint64_t *data, MemTxAttrs attrs)
  1650. {
  1651. GICState *s = ARM_GIC(opaque);
  1652. int vcpu = cpu + GIC_NCPU;
  1653. switch (addr) {
  1654. case A_GICH_HCR: /* Hypervisor Control */
  1655. *data = s->h_hcr[cpu];
  1656. break;
  1657. case A_GICH_VTR: /* VGIC Type */
  1658. *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
  1659. *data = FIELD_DP32(*data, GICH_VTR, PREbits,
  1660. GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
  1661. *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
  1662. (7 - GIC_VIRT_MIN_BPR) - 1);
  1663. break;
  1664. case A_GICH_VMCR: /* Virtual Machine Control */
  1665. *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
  1666. extract32(s->cpu_ctlr[vcpu], 0, 10));
  1667. *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
  1668. *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
  1669. *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
  1670. extract32(s->priority_mask[vcpu], 3, 5));
  1671. break;
  1672. case A_GICH_MISR: /* Maintenance Interrupt Status */
  1673. *data = s->h_misr[cpu];
  1674. break;
  1675. case A_GICH_EISR0: /* End of Interrupt Status 0 and 1 */
  1676. case A_GICH_EISR1:
  1677. *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
  1678. break;
  1679. case A_GICH_ELRSR0: /* Empty List Status 0 and 1 */
  1680. case A_GICH_ELRSR1:
  1681. *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
  1682. break;
  1683. case A_GICH_APR: /* Active Priorities */
  1684. *data = s->h_apr[cpu];
  1685. break;
  1686. case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
  1687. {
  1688. int lr_idx = (addr - A_GICH_LR0) / 4;
  1689. if (lr_idx > s->num_lrs) {
  1690. *data = 0;
  1691. } else {
  1692. *data = s->h_lr[lr_idx][cpu];
  1693. }
  1694. break;
  1695. }
  1696. default:
  1697. qemu_log_mask(LOG_GUEST_ERROR,
  1698. "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
  1699. return MEMTX_OK;
  1700. }
  1701. trace_gic_hyp_read(addr, *data);
  1702. return MEMTX_OK;
  1703. }
  1704. static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
  1705. uint64_t value, MemTxAttrs attrs)
  1706. {
  1707. GICState *s = ARM_GIC(opaque);
  1708. int vcpu = cpu + GIC_NCPU;
  1709. trace_gic_hyp_write(addr, value);
  1710. switch (addr) {
  1711. case A_GICH_HCR: /* Hypervisor Control */
  1712. s->h_hcr[cpu] = value & GICH_HCR_MASK;
  1713. break;
  1714. case A_GICH_VMCR: /* Virtual Machine Control */
  1715. gic_vmcr_write(s, value, attrs);
  1716. break;
  1717. case A_GICH_APR: /* Active Priorities */
  1718. s->h_apr[cpu] = value;
  1719. s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
  1720. break;
  1721. case A_GICH_LR0 ... A_GICH_LR63: /* List Registers */
  1722. {
  1723. int lr_idx = (addr - A_GICH_LR0) / 4;
  1724. if (lr_idx > s->num_lrs) {
  1725. return MEMTX_OK;
  1726. }
  1727. s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
  1728. trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
  1729. break;
  1730. }
  1731. default:
  1732. qemu_log_mask(LOG_GUEST_ERROR,
  1733. "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
  1734. return MEMTX_OK;
  1735. }
  1736. gic_update_virt(s);
  1737. return MEMTX_OK;
  1738. }
  1739. static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
  1740. unsigned size, MemTxAttrs attrs)
  1741. {
  1742. GICState *s = (GICState *)opaque;
  1743. return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
  1744. }
  1745. static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
  1746. uint64_t value, unsigned size,
  1747. MemTxAttrs attrs)
  1748. {
  1749. GICState *s = (GICState *)opaque;
  1750. return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
  1751. }
  1752. static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
  1753. unsigned size, MemTxAttrs attrs)
  1754. {
  1755. GICState **backref = (GICState **)opaque;
  1756. GICState *s = *backref;
  1757. int id = (backref - s->backref);
  1758. return gic_hyp_read(s, id, addr, data, attrs);
  1759. }
  1760. static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
  1761. uint64_t value, unsigned size,
  1762. MemTxAttrs attrs)
  1763. {
  1764. GICState **backref = (GICState **)opaque;
  1765. GICState *s = *backref;
  1766. int id = (backref - s->backref);
  1767. return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
  1768. }
  1769. static const MemoryRegionOps gic_ops[2] = {
  1770. {
  1771. .read_with_attrs = gic_dist_read,
  1772. .write_with_attrs = gic_dist_write,
  1773. .endianness = DEVICE_NATIVE_ENDIAN,
  1774. },
  1775. {
  1776. .read_with_attrs = gic_thiscpu_read,
  1777. .write_with_attrs = gic_thiscpu_write,
  1778. .endianness = DEVICE_NATIVE_ENDIAN,
  1779. }
  1780. };
  1781. static const MemoryRegionOps gic_cpu_ops = {
  1782. .read_with_attrs = gic_do_cpu_read,
  1783. .write_with_attrs = gic_do_cpu_write,
  1784. .endianness = DEVICE_NATIVE_ENDIAN,
  1785. };
  1786. static const MemoryRegionOps gic_virt_ops[2] = {
  1787. {
  1788. .read_with_attrs = gic_thiscpu_hyp_read,
  1789. .write_with_attrs = gic_thiscpu_hyp_write,
  1790. .endianness = DEVICE_NATIVE_ENDIAN,
  1791. },
  1792. {
  1793. .read_with_attrs = gic_thisvcpu_read,
  1794. .write_with_attrs = gic_thisvcpu_write,
  1795. .endianness = DEVICE_NATIVE_ENDIAN,
  1796. }
  1797. };
  1798. static const MemoryRegionOps gic_viface_ops = {
  1799. .read_with_attrs = gic_do_hyp_read,
  1800. .write_with_attrs = gic_do_hyp_write,
  1801. .endianness = DEVICE_NATIVE_ENDIAN,
  1802. };
  1803. static void arm_gic_realize(DeviceState *dev, Error **errp)
  1804. {
  1805. /* Device instance realize function for the GIC sysbus device */
  1806. int i;
  1807. GICState *s = ARM_GIC(dev);
  1808. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1809. ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
  1810. Error *local_err = NULL;
  1811. agc->parent_realize(dev, &local_err);
  1812. if (local_err) {
  1813. error_propagate(errp, local_err);
  1814. return;
  1815. }
  1816. if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
  1817. error_setg(errp, "KVM with user space irqchip only works when the "
  1818. "host kernel supports KVM_CAP_ARM_USER_IRQ");
  1819. return;
  1820. }
  1821. /* This creates distributor, main CPU interface (s->cpuiomem[0]) and if
  1822. * enabled, virtualization extensions related interfaces (main virtual
  1823. * interface (s->vifaceiomem[0]) and virtual CPU interface).
  1824. */
  1825. gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
  1826. /* Extra core-specific regions for the CPU interfaces. This is
  1827. * necessary for "franken-GIC" implementations, for example on
  1828. * Exynos 4.
  1829. * NB that the memory region size of 0x100 applies for the 11MPCore
  1830. * and also cores following the GIC v1 spec (ie A9).
  1831. * GIC v2 defines a larger memory region (0x1000) so this will need
  1832. * to be extended when we implement A15.
  1833. */
  1834. for (i = 0; i < s->num_cpu; i++) {
  1835. s->backref[i] = s;
  1836. memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
  1837. &s->backref[i], "gic_cpu", 0x100);
  1838. sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
  1839. }
  1840. /* Extra core-specific regions for virtual interfaces. This is required by
  1841. * the GICv2 specification.
  1842. */
  1843. if (s->virt_extn) {
  1844. for (i = 0; i < s->num_cpu; i++) {
  1845. memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
  1846. &gic_viface_ops, &s->backref[i],
  1847. "gic_viface", 0x200);
  1848. sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
  1849. }
  1850. }
  1851. }
  1852. static void arm_gic_class_init(ObjectClass *klass, void *data)
  1853. {
  1854. DeviceClass *dc = DEVICE_CLASS(klass);
  1855. ARMGICClass *agc = ARM_GIC_CLASS(klass);
  1856. device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
  1857. }
  1858. static const TypeInfo arm_gic_info = {
  1859. .name = TYPE_ARM_GIC,
  1860. .parent = TYPE_ARM_GIC_COMMON,
  1861. .instance_size = sizeof(GICState),
  1862. .class_init = arm_gic_class_init,
  1863. .class_size = sizeof(ARMGICClass),
  1864. };
  1865. static void arm_gic_register_types(void)
  1866. {
  1867. type_register_static(&arm_gic_info);
  1868. }
  1869. type_init(arm_gic_register_types)