1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "qemu/bitops.h"
17#include "qemu/log.h"
18#include "qemu/main-loop.h"
19#include "trace.h"
20#include "gicv3_internal.h"
21#include "hw/irq.h"
22#include "cpu.h"
23#include "target/arm/cpregs.h"
24
25
26
27
28
29#define HPPVI_INDEX_VLPI 16
30
31static GICv3CPUState *icc_cs_from_env(CPUARMState *env)
32{
33 return env->gicv3state;
34}
35
36static bool gicv3_use_ns_bank(CPUARMState *env)
37{
38
39
40
41
42
43 return !arm_is_secure_below_el3(env);
44}
45
46
47static inline int icv_min_vbpr(GICv3CPUState *cs)
48{
49 return 7 - cs->vprebits;
50}
51
52static inline int ich_num_aprs(GICv3CPUState *cs)
53{
54
55 int aprmax = 1 << (cs->vprebits - 5);
56 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
57 return aprmax;
58}
59
60
61static uint32_t ich_lr_vintid(uint64_t lr)
62{
63 return extract64(lr, ICH_LR_EL2_VINTID_SHIFT, ICH_LR_EL2_VINTID_LENGTH);
64}
65
66static uint32_t ich_lr_pintid(uint64_t lr)
67{
68 return extract64(lr, ICH_LR_EL2_PINTID_SHIFT, ICH_LR_EL2_PINTID_LENGTH);
69}
70
71static uint32_t ich_lr_prio(uint64_t lr)
72{
73 return extract64(lr, ICH_LR_EL2_PRIORITY_SHIFT, ICH_LR_EL2_PRIORITY_LENGTH);
74}
75
76static int ich_lr_state(uint64_t lr)
77{
78 return extract64(lr, ICH_LR_EL2_STATE_SHIFT, ICH_LR_EL2_STATE_LENGTH);
79}
80
81static bool icv_access(CPUARMState *env, int hcr_flags)
82{
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
98 bool flagmatch = hcr_el2 & hcr_flags & (HCR_IMO | HCR_FMO);
99
100 return flagmatch && arm_current_el(env) == 1
101 && !arm_is_secure_below_el3(env);
102}
103
104static int read_vbpr(GICv3CPUState *cs, int grp)
105{
106
107
108
109 if (grp == GICV3_G0) {
110 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
111 ICH_VMCR_EL2_VBPR0_LENGTH);
112 } else {
113 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
114 ICH_VMCR_EL2_VBPR1_LENGTH);
115 }
116}
117
118static void write_vbpr(GICv3CPUState *cs, int grp, int value)
119{
120
121
122
123 int min = icv_min_vbpr(cs);
124
125 if (grp != GICV3_G0) {
126 min++;
127 }
128
129 value = MAX(value, min);
130
131 if (grp == GICV3_G0) {
132 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
133 ICH_VMCR_EL2_VBPR0_LENGTH, value);
134 } else {
135 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
136 ICH_VMCR_EL2_VBPR1_LENGTH, value);
137 }
138}
139
140static uint32_t icv_fullprio_mask(GICv3CPUState *cs)
141{
142
143
144
145
146
147 return ~0U << (8 - cs->vpribits);
148}
149
150static int ich_highest_active_virt_prio(GICv3CPUState *cs)
151{
152
153
154
155 int i;
156 int aprmax = ich_num_aprs(cs);
157
158 for (i = 0; i < aprmax; i++) {
159 uint32_t apr = cs->ich_apr[GICV3_G0][i] |
160 cs->ich_apr[GICV3_G1NS][i];
161
162 if (!apr) {
163 continue;
164 }
165 return (i * 32 + ctz32(apr)) << (icv_min_vbpr(cs) + 1);
166 }
167
168 return 0xff;
169}
170
171static int hppvi_index(GICv3CPUState *cs)
172{
173
174
175
176
177
178
179
180
181
182
183 ARMCPU *cpu = ARM_CPU(cs->cpu);
184 CPUARMState *env = &cpu->env;
185 int idx = -1;
186 int i;
187
188
189
190
191 int prio = 0xff;
192
193 if (!(cs->ich_vmcr_el2 & (ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1))) {
194
195 return idx;
196 }
197
198 for (i = 0; i < cs->num_list_regs; i++) {
199 uint64_t lr = cs->ich_lr_el2[i];
200 int thisprio;
201
202 if (ich_lr_state(lr) != ICH_LR_EL2_STATE_PENDING) {
203
204 continue;
205 }
206
207
208 if (lr & ICH_LR_EL2_GROUP) {
209 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
210 continue;
211 }
212 } else {
213 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
214 continue;
215 }
216 }
217
218 thisprio = ich_lr_prio(lr);
219
220 if (thisprio < prio) {
221 prio = thisprio;
222 idx = i;
223 }
224 }
225
226
227
228
229
230
231 if (cs->hppvlpi.prio < prio && !arm_is_secure(env)) {
232 if (cs->hppvlpi.grp == GICV3_G0) {
233 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0) {
234 return HPPVI_INDEX_VLPI;
235 }
236 } else {
237 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1) {
238 return HPPVI_INDEX_VLPI;
239 }
240 }
241 }
242
243 return idx;
244}
245
246static uint32_t icv_gprio_mask(GICv3CPUState *cs, int group)
247{
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 int bpr;
267
268 if (group == GICV3_G1NS && cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
269 group = GICV3_G0;
270 }
271
272 bpr = read_vbpr(cs, group);
273 if (group == GICV3_G1NS) {
274 assert(bpr > 0);
275 bpr--;
276 }
277
278 return ~0U << (bpr + 1);
279}
280
281static bool icv_hppi_can_preempt(GICv3CPUState *cs, uint64_t lr)
282{
283
284
285
286
287
288
289 int grp;
290 uint32_t mask, prio, rprio, vpmr;
291
292 if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
293
294 return false;
295 }
296
297
298
299
300
301 prio = ich_lr_prio(lr);
302 vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
303 ICH_VMCR_EL2_VPMR_LENGTH);
304
305 if (prio >= vpmr) {
306
307 return false;
308 }
309
310 rprio = ich_highest_active_virt_prio(cs);
311 if (rprio == 0xff) {
312
313 return true;
314 }
315
316 grp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
317
318 mask = icv_gprio_mask(cs, grp);
319
320
321
322
323 if ((prio & mask) < (rprio & mask)) {
324 return true;
325 }
326
327 return false;
328}
329
330static bool icv_hppvlpi_can_preempt(GICv3CPUState *cs)
331{
332
333
334
335
336
337 uint32_t mask, rprio, vpmr;
338
339 if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
340
341 return false;
342 }
343
344 vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
345 ICH_VMCR_EL2_VPMR_LENGTH);
346
347 if (cs->hppvlpi.prio >= vpmr) {
348
349 return false;
350 }
351
352 rprio = ich_highest_active_virt_prio(cs);
353 if (rprio == 0xff) {
354
355 return true;
356 }
357
358 mask = icv_gprio_mask(cs, cs->hppvlpi.grp);
359
360
361
362
363
364 if ((cs->hppvlpi.prio & mask) < (rprio & mask)) {
365 return true;
366 }
367
368 return false;
369}
370
371static uint32_t eoi_maintenance_interrupt_state(GICv3CPUState *cs,
372 uint32_t *misr)
373{
374
375
376
377
378
379
380
381 uint32_t value = 0;
382 int validcount = 0;
383 bool seenpending = false;
384 int i;
385
386 for (i = 0; i < cs->num_list_regs; i++) {
387 uint64_t lr = cs->ich_lr_el2[i];
388
389 if ((lr & (ICH_LR_EL2_STATE_MASK | ICH_LR_EL2_HW | ICH_LR_EL2_EOI))
390 == ICH_LR_EL2_EOI) {
391 value |= (1 << i);
392 }
393 if ((lr & ICH_LR_EL2_STATE_MASK)) {
394 validcount++;
395 }
396 if (ich_lr_state(lr) == ICH_LR_EL2_STATE_PENDING) {
397 seenpending = true;
398 }
399 }
400
401 if (misr) {
402 if (validcount < 2 && (cs->ich_hcr_el2 & ICH_HCR_EL2_UIE)) {
403 *misr |= ICH_MISR_EL2_U;
404 }
405 if (!seenpending && (cs->ich_hcr_el2 & ICH_HCR_EL2_NPIE)) {
406 *misr |= ICH_MISR_EL2_NP;
407 }
408 if (value) {
409 *misr |= ICH_MISR_EL2_EOI;
410 }
411 }
412 return value;
413}
414
415static uint32_t maintenance_interrupt_state(GICv3CPUState *cs)
416{
417
418
419
420 uint32_t value = 0;
421
422
423 eoi_maintenance_interrupt_state(cs, &value);
424
425 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_LRENPIE) &&
426 (cs->ich_hcr_el2 & ICH_HCR_EL2_EOICOUNT_MASK)) {
427 value |= ICH_MISR_EL2_LRENP;
428 }
429
430 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0EIE) &&
431 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
432 value |= ICH_MISR_EL2_VGRP0E;
433 }
434
435 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0DIE) &&
436 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
437 value |= ICH_MISR_EL2_VGRP0D;
438 }
439 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1EIE) &&
440 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
441 value |= ICH_MISR_EL2_VGRP1E;
442 }
443
444 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1DIE) &&
445 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
446 value |= ICH_MISR_EL2_VGRP1D;
447 }
448
449 return value;
450}
451
452void gicv3_cpuif_virt_irq_fiq_update(GICv3CPUState *cs)
453{
454
455
456
457
458
459
460
461
462
463 int idx;
464 int irqlevel = 0;
465 int fiqlevel = 0;
466
467 idx = hppvi_index(cs);
468 trace_gicv3_cpuif_virt_update(gicv3_redist_affid(cs), idx,
469 cs->hppvlpi.irq, cs->hppvlpi.grp,
470 cs->hppvlpi.prio);
471 if (idx == HPPVI_INDEX_VLPI) {
472 if (icv_hppvlpi_can_preempt(cs)) {
473 if (cs->hppvlpi.grp == GICV3_G0) {
474 fiqlevel = 1;
475 } else {
476 irqlevel = 1;
477 }
478 }
479 } else if (idx >= 0) {
480 uint64_t lr = cs->ich_lr_el2[idx];
481
482 if (icv_hppi_can_preempt(cs, lr)) {
483
484 if (lr & ICH_LR_EL2_GROUP) {
485 irqlevel = 1;
486 } else {
487 fiqlevel = 1;
488 }
489 }
490 }
491
492 trace_gicv3_cpuif_virt_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
493 qemu_set_irq(cs->parent_vfiq, fiqlevel);
494 qemu_set_irq(cs->parent_virq, irqlevel);
495}
496
497static void gicv3_cpuif_virt_update(GICv3CPUState *cs)
498{
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517 ARMCPU *cpu = ARM_CPU(cs->cpu);
518 int maintlevel = 0;
519
520 gicv3_cpuif_virt_irq_fiq_update(cs);
521
522 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_EN) &&
523 maintenance_interrupt_state(cs) != 0) {
524 maintlevel = 1;
525 }
526
527 trace_gicv3_cpuif_virt_set_maint_irq(gicv3_redist_affid(cs), maintlevel);
528 qemu_set_irq(cpu->gicv3_maintenance_interrupt, maintlevel);
529}
530
531static uint64_t icv_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
532{
533 GICv3CPUState *cs = icc_cs_from_env(env);
534 int regno = ri->opc2 & 3;
535 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
536 uint64_t value = cs->ich_apr[grp][regno];
537
538 trace_gicv3_icv_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
539 return value;
540}
541
542static void icv_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
543 uint64_t value)
544{
545 GICv3CPUState *cs = icc_cs_from_env(env);
546 int regno = ri->opc2 & 3;
547 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
548
549 trace_gicv3_icv_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
550
551 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
552
553 gicv3_cpuif_virt_irq_fiq_update(cs);
554 return;
555}
556
557static uint64_t icv_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
558{
559 GICv3CPUState *cs = icc_cs_from_env(env);
560 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
561 uint64_t bpr;
562 bool satinc = false;
563
564 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
565
566 grp = GICV3_G0;
567 satinc = true;
568 }
569
570 bpr = read_vbpr(cs, grp);
571
572 if (satinc) {
573 bpr++;
574 bpr = MIN(bpr, 7);
575 }
576
577 trace_gicv3_icv_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
578
579 return bpr;
580}
581
582static void icv_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
583 uint64_t value)
584{
585 GICv3CPUState *cs = icc_cs_from_env(env);
586 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
587
588 trace_gicv3_icv_bpr_write(ri->crm == 8 ? 0 : 1,
589 gicv3_redist_affid(cs), value);
590
591 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
592
593 return;
594 }
595
596 write_vbpr(cs, grp, value);
597
598 gicv3_cpuif_virt_irq_fiq_update(cs);
599}
600
601static uint64_t icv_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
602{
603 GICv3CPUState *cs = icc_cs_from_env(env);
604 uint64_t value;
605
606 value = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
607 ICH_VMCR_EL2_VPMR_LENGTH);
608
609 trace_gicv3_icv_pmr_read(gicv3_redist_affid(cs), value);
610 return value;
611}
612
613static void icv_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
614 uint64_t value)
615{
616 GICv3CPUState *cs = icc_cs_from_env(env);
617
618 trace_gicv3_icv_pmr_write(gicv3_redist_affid(cs), value);
619
620 value &= icv_fullprio_mask(cs);
621
622 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
623 ICH_VMCR_EL2_VPMR_LENGTH, value);
624
625 gicv3_cpuif_virt_irq_fiq_update(cs);
626}
627
628static uint64_t icv_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
629{
630 GICv3CPUState *cs = icc_cs_from_env(env);
631 int enbit;
632 uint64_t value;
633
634 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
635 value = extract64(cs->ich_vmcr_el2, enbit, 1);
636
637 trace_gicv3_icv_igrpen_read(ri->opc2 & 1 ? 1 : 0,
638 gicv3_redist_affid(cs), value);
639 return value;
640}
641
642static void icv_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
643 uint64_t value)
644{
645 GICv3CPUState *cs = icc_cs_from_env(env);
646 int enbit;
647
648 trace_gicv3_icv_igrpen_write(ri->opc2 & 1 ? 1 : 0,
649 gicv3_redist_affid(cs), value);
650
651 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
652
653 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, enbit, 1, value);
654 gicv3_cpuif_virt_update(cs);
655}
656
657static uint64_t icv_ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
658{
659 GICv3CPUState *cs = icc_cs_from_env(env);
660 uint64_t value;
661
662
663
664
665 value = ICC_CTLR_EL1_A3V | (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
666 ((cs->vpribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
667
668 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM) {
669 value |= ICC_CTLR_EL1_EOIMODE;
670 }
671
672 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
673 value |= ICC_CTLR_EL1_CBPR;
674 }
675
676 trace_gicv3_icv_ctlr_read(gicv3_redist_affid(cs), value);
677 return value;
678}
679
680static void icv_ctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 uint64_t value)
682{
683 GICv3CPUState *cs = icc_cs_from_env(env);
684
685 trace_gicv3_icv_ctlr_write(gicv3_redist_affid(cs), value);
686
687 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VCBPR_SHIFT,
688 1, value & ICC_CTLR_EL1_CBPR ? 1 : 0);
689 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VEOIM_SHIFT,
690 1, value & ICC_CTLR_EL1_EOIMODE ? 1 : 0);
691
692 gicv3_cpuif_virt_irq_fiq_update(cs);
693}
694
695static uint64_t icv_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
696{
697 GICv3CPUState *cs = icc_cs_from_env(env);
698 int prio = ich_highest_active_virt_prio(cs);
699
700 trace_gicv3_icv_rpr_read(gicv3_redist_affid(cs), prio);
701 return prio;
702}
703
704static uint64_t icv_hppir_read(CPUARMState *env, const ARMCPRegInfo *ri)
705{
706 GICv3CPUState *cs = icc_cs_from_env(env);
707 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
708 int idx = hppvi_index(cs);
709 uint64_t value = INTID_SPURIOUS;
710
711 if (idx == HPPVI_INDEX_VLPI) {
712 if (cs->hppvlpi.grp == grp) {
713 value = cs->hppvlpi.irq;
714 }
715 } else if (idx >= 0) {
716 uint64_t lr = cs->ich_lr_el2[idx];
717 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
718
719 if (grp == thisgrp) {
720 value = ich_lr_vintid(lr);
721 }
722 }
723
724 trace_gicv3_icv_hppir_read(ri->crm == 8 ? 0 : 1,
725 gicv3_redist_affid(cs), value);
726 return value;
727}
728
729static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp)
730{
731
732
733
734
735 uint32_t mask = icv_gprio_mask(cs, grp);
736 int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask;
737 int aprbit = prio >> (8 - cs->vprebits);
738 int regno = aprbit / 32;
739 int regbit = aprbit % 32;
740
741 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
742 cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT;
743 cs->ich_apr[grp][regno] |= (1 << regbit);
744}
745
746static void icv_activate_vlpi(GICv3CPUState *cs)
747{
748 uint32_t mask = icv_gprio_mask(cs, cs->hppvlpi.grp);
749 int prio = cs->hppvlpi.prio & mask;
750 int aprbit = prio >> (8 - cs->vprebits);
751 int regno = aprbit / 32;
752 int regbit = aprbit % 32;
753
754 cs->ich_apr[cs->hppvlpi.grp][regno] |= (1 << regbit);
755 gicv3_redist_vlpi_pending(cs, cs->hppvlpi.irq, 0);
756}
757
758static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri)
759{
760 GICv3CPUState *cs = icc_cs_from_env(env);
761 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
762 int idx = hppvi_index(cs);
763 uint64_t intid = INTID_SPURIOUS;
764
765 if (idx == HPPVI_INDEX_VLPI) {
766 if (cs->hppvlpi.grp == grp && icv_hppvlpi_can_preempt(cs)) {
767 intid = cs->hppvlpi.irq;
768 icv_activate_vlpi(cs);
769 }
770 } else if (idx >= 0) {
771 uint64_t lr = cs->ich_lr_el2[idx];
772 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
773
774 if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) {
775 intid = ich_lr_vintid(lr);
776 if (!gicv3_intid_is_special(intid)) {
777 icv_activate_irq(cs, idx, grp);
778 } else {
779
780 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
781
782
783
784 }
785 }
786 }
787
788 trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1,
789 gicv3_redist_affid(cs), intid);
790
791 gicv3_cpuif_virt_update(cs);
792
793 return intid;
794}
795
796static uint32_t icc_fullprio_mask(GICv3CPUState *cs)
797{
798
799
800
801
802
803
804 return ~0U << (8 - cs->pribits);
805}
806
807static inline int icc_min_bpr(GICv3CPUState *cs)
808{
809
810 return 7 - cs->prebits;
811}
812
813static inline int icc_min_bpr_ns(GICv3CPUState *cs)
814{
815 return icc_min_bpr(cs) + 1;
816}
817
818static inline int icc_num_aprs(GICv3CPUState *cs)
819{
820
821 int aprmax = 1 << MAX(cs->prebits - 5, 0);
822 assert(aprmax <= ARRAY_SIZE(cs->icc_apr[0]));
823 return aprmax;
824}
825
826static int icc_highest_active_prio(GICv3CPUState *cs)
827{
828
829
830
831 int i;
832
833 for (i = 0; i < icc_num_aprs(cs); i++) {
834 uint32_t apr = cs->icc_apr[GICV3_G0][i] |
835 cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
836
837 if (!apr) {
838 continue;
839 }
840 return (i * 32 + ctz32(apr)) << (icc_min_bpr(cs) + 1);
841 }
842
843 return 0xff;
844}
845
846static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
847{
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865 int bpr;
866
867 if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
868 (group == GICV3_G1NS &&
869 cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
870 group = GICV3_G0;
871 }
872
873 bpr = cs->icc_bpr[group] & 7;
874
875 if (group == GICV3_G1NS) {
876 assert(bpr > 0);
877 bpr--;
878 }
879
880 return ~0U << (bpr + 1);
881}
882
883static bool icc_no_enabled_hppi(GICv3CPUState *cs)
884{
885
886
887
888
889 return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
890}
891
892static bool icc_hppi_can_preempt(GICv3CPUState *cs)
893{
894
895
896
897 int rprio;
898 uint32_t mask;
899
900 if (icc_no_enabled_hppi(cs)) {
901 return false;
902 }
903
904 if (cs->hppi.prio >= cs->icc_pmr_el1) {
905
906 return false;
907 }
908
909 rprio = icc_highest_active_prio(cs);
910 if (rprio == 0xff) {
911
912 return true;
913 }
914
915 mask = icc_gprio_mask(cs, cs->hppi.grp);
916
917
918
919
920 if ((cs->hppi.prio & mask) < (rprio & mask)) {
921 return true;
922 }
923
924 return false;
925}
926
927void gicv3_cpuif_update(GICv3CPUState *cs)
928{
929
930 int irqlevel = 0;
931 int fiqlevel = 0;
932 ARMCPU *cpu = ARM_CPU(cs->cpu);
933 CPUARMState *env = &cpu->env;
934
935 g_assert(qemu_mutex_iothread_locked());
936
937 trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
938 cs->hppi.grp, cs->hppi.prio);
939
940 if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
941
942
943
944 cs->hppi.grp = GICV3_G0;
945 }
946
947 if (icc_hppi_can_preempt(cs)) {
948
949
950
951 bool isfiq;
952
953 switch (cs->hppi.grp) {
954 case GICV3_G0:
955 isfiq = true;
956 break;
957 case GICV3_G1:
958 isfiq = (!arm_is_secure(env) ||
959 (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
960 break;
961 case GICV3_G1NS:
962 isfiq = arm_is_secure(env);
963 break;
964 default:
965 g_assert_not_reached();
966 }
967
968 if (isfiq) {
969 fiqlevel = 1;
970 } else {
971 irqlevel = 1;
972 }
973 }
974
975 trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
976
977 qemu_set_irq(cs->parent_fiq, fiqlevel);
978 qemu_set_irq(cs->parent_irq, irqlevel);
979}
980
981static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
982{
983 GICv3CPUState *cs = icc_cs_from_env(env);
984 uint32_t value = cs->icc_pmr_el1;
985
986 if (icv_access(env, HCR_FMO | HCR_IMO)) {
987 return icv_pmr_read(env, ri);
988 }
989
990 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
991 (env->cp15.scr_el3 & SCR_FIQ)) {
992
993
994
995 if ((value & 0x80) == 0) {
996
997 value = 0;
998 } else if (value != 0xff) {
999 value = (value << 1) & 0xff;
1000 }
1001 }
1002
1003 trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
1004
1005 return value;
1006}
1007
1008static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1009 uint64_t value)
1010{
1011 GICv3CPUState *cs = icc_cs_from_env(env);
1012
1013 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1014 return icv_pmr_write(env, ri, value);
1015 }
1016
1017 trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
1018
1019 value &= icc_fullprio_mask(cs);
1020
1021 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
1022 (env->cp15.scr_el3 & SCR_FIQ)) {
1023
1024
1025
1026 if (!(cs->icc_pmr_el1 & 0x80)) {
1027
1028 return;
1029 }
1030 value = (value >> 1) | 0x80;
1031 }
1032 cs->icc_pmr_el1 = value;
1033 gicv3_cpuif_update(cs);
1034}
1035
1036static void icc_activate_irq(GICv3CPUState *cs, int irq)
1037{
1038
1039
1040
1041 uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
1042 int prio = cs->hppi.prio & mask;
1043 int aprbit = prio >> (8 - cs->prebits);
1044 int regno = aprbit / 32;
1045 int regbit = aprbit % 32;
1046
1047 cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit);
1048
1049 if (irq < GIC_INTERNAL) {
1050 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
1051 cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
1052 gicv3_redist_update(cs);
1053 } else if (irq < GICV3_LPI_INTID_START) {
1054 gicv3_gicd_active_set(cs->gic, irq);
1055 gicv3_gicd_pending_clear(cs->gic, irq);
1056 gicv3_update(cs->gic, irq, 1);
1057 } else {
1058 gicv3_redist_lpi_pending(cs, irq, 0);
1059 }
1060}
1061
1062static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
1063{
1064
1065
1066
1067 bool irq_is_secure;
1068
1069 if (cs->hppi.prio == 0xff) {
1070 return INTID_SPURIOUS;
1071 }
1072
1073
1074
1075
1076
1077
1078 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
1079 (cs->hppi.grp != GICV3_G1NS));
1080
1081 if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
1082 return INTID_SPURIOUS;
1083 }
1084 if (irq_is_secure && !arm_is_secure(env)) {
1085
1086 return INTID_SPURIOUS;
1087 }
1088
1089 if (cs->hppi.grp != GICV3_G0) {
1090
1091
1092
1093 return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
1094 }
1095
1096 return cs->hppi.irq;
1097}
1098
1099static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
1100{
1101
1102
1103
1104 bool irq_is_secure;
1105
1106 if (cs->hppi.prio == 0xff) {
1107 return INTID_SPURIOUS;
1108 }
1109
1110
1111
1112
1113
1114
1115 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
1116 (cs->hppi.grp != GICV3_G1NS));
1117
1118 if (cs->hppi.grp == GICV3_G0) {
1119
1120 return INTID_SPURIOUS;
1121 }
1122 if (irq_is_secure) {
1123 if (!arm_is_secure(env)) {
1124
1125 return INTID_SPURIOUS;
1126 }
1127 } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
1128
1129 return INTID_SPURIOUS;
1130 }
1131
1132 return cs->hppi.irq;
1133}
1134
1135static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1136{
1137 GICv3CPUState *cs = icc_cs_from_env(env);
1138 uint64_t intid;
1139
1140 if (icv_access(env, HCR_FMO)) {
1141 return icv_iar_read(env, ri);
1142 }
1143
1144 if (!icc_hppi_can_preempt(cs)) {
1145 intid = INTID_SPURIOUS;
1146 } else {
1147 intid = icc_hppir0_value(cs, env);
1148 }
1149
1150 if (!gicv3_intid_is_special(intid)) {
1151 icc_activate_irq(cs, intid);
1152 }
1153
1154 trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
1155 return intid;
1156}
1157
1158static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1159{
1160 GICv3CPUState *cs = icc_cs_from_env(env);
1161 uint64_t intid;
1162
1163 if (icv_access(env, HCR_IMO)) {
1164 return icv_iar_read(env, ri);
1165 }
1166
1167 if (!icc_hppi_can_preempt(cs)) {
1168 intid = INTID_SPURIOUS;
1169 } else {
1170 intid = icc_hppir1_value(cs, env);
1171 }
1172
1173 if (!gicv3_intid_is_special(intid)) {
1174 icc_activate_irq(cs, intid);
1175 }
1176
1177 trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
1178 return intid;
1179}
1180
1181static void icc_drop_prio(GICv3CPUState *cs, int grp)
1182{
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199 int i;
1200
1201 for (i = 0; i < icc_num_aprs(cs); i++) {
1202 uint64_t *papr = &cs->icc_apr[grp][i];
1203
1204 if (!*papr) {
1205 continue;
1206 }
1207
1208 *papr &= *papr - 1;
1209 break;
1210 }
1211
1212
1213 gicv3_cpuif_update(cs);
1214}
1215
1216static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1217{
1218
1219
1220
1221 if (arm_is_el3_or_mon(env)) {
1222 return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
1223 }
1224 if (arm_is_secure_below_el3(env)) {
1225 return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
1226 } else {
1227 return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
1228 }
1229}
1230
1231static int icc_highest_active_group(GICv3CPUState *cs)
1232{
1233
1234
1235
1236
1237
1238
1239 int i;
1240
1241 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
1242 int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
1243 int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
1244 int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
1245
1246 if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
1247 return GICV3_G1NS;
1248 }
1249 if (g1ctz < g0ctz) {
1250 return GICV3_G1;
1251 }
1252 if (g0ctz < 32) {
1253 return GICV3_G0;
1254 }
1255 }
1256
1257
1258
1259 return -1;
1260}
1261
1262static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
1263{
1264 if (irq < GIC_INTERNAL) {
1265 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
1266 gicv3_redist_update(cs);
1267 } else {
1268 gicv3_gicd_active_clear(cs->gic, irq);
1269 gicv3_update(cs->gic, irq, 1);
1270 }
1271}
1272
1273static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1274{
1275
1276
1277
1278 return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM;
1279}
1280
1281static int icv_find_active(GICv3CPUState *cs, int irq)
1282{
1283
1284
1285
1286
1287 int i;
1288
1289 for (i = 0; i < cs->num_list_regs; i++) {
1290 uint64_t lr = cs->ich_lr_el2[i];
1291
1292 if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) {
1293 return i;
1294 }
1295 }
1296
1297 return -1;
1298}
1299
1300static void icv_deactivate_irq(GICv3CPUState *cs, int idx)
1301{
1302
1303 uint64_t lr = cs->ich_lr_el2[idx];
1304
1305 if (lr & ICH_LR_EL2_HW) {
1306
1307 int pirq = ich_lr_pintid(lr);
1308
1309 if (pirq < INTID_SECURE) {
1310 icc_deactivate_irq(cs, pirq);
1311 }
1312 }
1313
1314
1315
1316
1317 lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT;
1318 cs->ich_lr_el2[idx] = lr;
1319}
1320
1321static void icv_increment_eoicount(GICv3CPUState *cs)
1322{
1323
1324 int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1325 ICH_HCR_EL2_EOICOUNT_LENGTH);
1326
1327 cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1328 ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1);
1329}
1330
1331static int icv_drop_prio(GICv3CPUState *cs)
1332{
1333
1334
1335
1336
1337
1338
1339
1340
1341 int i;
1342 int aprmax = ich_num_aprs(cs);
1343
1344 for (i = 0; i < aprmax; i++) {
1345 uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i];
1346 uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i];
1347 int apr0count, apr1count;
1348
1349 if (!*papr0 && !*papr1) {
1350 continue;
1351 }
1352
1353
1354
1355
1356
1357 apr0count = ctz32(*papr0);
1358 apr1count = ctz32(*papr1);
1359
1360 if (apr0count <= apr1count) {
1361 *papr0 &= *papr0 - 1;
1362 return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1);
1363 } else {
1364 *papr1 &= *papr1 - 1;
1365 return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1);
1366 }
1367 }
1368 return 0xff;
1369}
1370
1371static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1372 uint64_t value)
1373{
1374
1375 GICv3CPUState *cs = icc_cs_from_env(env);
1376 int idx;
1377 int irq = value & 0xffffff;
1378
1379 trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value);
1380
1381 if (irq >= GICV3_MAXIRQ) {
1382
1383 return;
1384 }
1385
1386 if (!icv_eoi_split(env, cs)) {
1387 return;
1388 }
1389
1390 idx = icv_find_active(cs, irq);
1391
1392 if (idx < 0) {
1393
1394
1395
1396 icv_increment_eoicount(cs);
1397 } else {
1398 icv_deactivate_irq(cs, idx);
1399 }
1400
1401 gicv3_cpuif_virt_update(cs);
1402}
1403
1404static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1405 uint64_t value)
1406{
1407
1408 GICv3CPUState *cs = icc_cs_from_env(env);
1409 int irq = value & 0xffffff;
1410 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
1411 int idx, dropprio;
1412
1413 trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1,
1414 gicv3_redist_affid(cs), value);
1415
1416 if (gicv3_intid_is_special(irq)) {
1417 return;
1418 }
1419
1420
1421
1422
1423
1424 dropprio = icv_drop_prio(cs);
1425 if (dropprio == 0xff) {
1426
1427
1428
1429
1430 return;
1431 }
1432
1433 idx = icv_find_active(cs, irq);
1434
1435 if (idx < 0) {
1436
1437 icv_increment_eoicount(cs);
1438 } else {
1439 uint64_t lr = cs->ich_lr_el2[idx];
1440 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
1441 int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp);
1442
1443 if (thisgrp == grp && lr_gprio == dropprio) {
1444 if (!icv_eoi_split(env, cs)) {
1445
1446 icv_deactivate_irq(cs, idx);
1447 }
1448 }
1449 }
1450
1451 gicv3_cpuif_virt_update(cs);
1452}
1453
1454static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1455 uint64_t value)
1456{
1457
1458 GICv3CPUState *cs = icc_cs_from_env(env);
1459 int irq = value & 0xffffff;
1460 int grp;
1461 bool is_eoir0 = ri->crm == 8;
1462
1463 if (icv_access(env, is_eoir0 ? HCR_FMO : HCR_IMO)) {
1464 icv_eoir_write(env, ri, value);
1465 return;
1466 }
1467
1468 trace_gicv3_icc_eoir_write(is_eoir0 ? 0 : 1,
1469 gicv3_redist_affid(cs), value);
1470
1471 if ((irq >= cs->gic->num_irq) &&
1472 !(cs->gic->lpi_enable && (irq >= GICV3_LPI_INTID_START))) {
1473
1474
1475
1476
1477
1478
1479
1480
1481 return;
1482 }
1483
1484 grp = icc_highest_active_group(cs);
1485 switch (grp) {
1486 case GICV3_G0:
1487 if (!is_eoir0) {
1488 return;
1489 }
1490 if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS)
1491 && arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) {
1492 return;
1493 }
1494 break;
1495 case GICV3_G1:
1496 if (is_eoir0) {
1497 return;
1498 }
1499 if (!arm_is_secure(env)) {
1500 return;
1501 }
1502 break;
1503 case GICV3_G1NS:
1504 if (is_eoir0) {
1505 return;
1506 }
1507 if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
1508 return;
1509 }
1510 break;
1511 default:
1512 qemu_log_mask(LOG_GUEST_ERROR,
1513 "%s: IRQ %d isn't active\n", __func__, irq);
1514 return;
1515 }
1516
1517 icc_drop_prio(cs, grp);
1518
1519 if (!icc_eoi_split(env, cs)) {
1520
1521 icc_deactivate_irq(cs, irq);
1522 }
1523}
1524
1525static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1526{
1527 GICv3CPUState *cs = icc_cs_from_env(env);
1528 uint64_t value;
1529
1530 if (icv_access(env, HCR_FMO)) {
1531 return icv_hppir_read(env, ri);
1532 }
1533
1534 value = icc_hppir0_value(cs, env);
1535 trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
1536 return value;
1537}
1538
1539static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1540{
1541 GICv3CPUState *cs = icc_cs_from_env(env);
1542 uint64_t value;
1543
1544 if (icv_access(env, HCR_IMO)) {
1545 return icv_hppir_read(env, ri);
1546 }
1547
1548 value = icc_hppir1_value(cs, env);
1549 trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
1550 return value;
1551}
1552
1553static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1554{
1555 GICv3CPUState *cs = icc_cs_from_env(env);
1556 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1557 bool satinc = false;
1558 uint64_t bpr;
1559
1560 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1561 return icv_bpr_read(env, ri);
1562 }
1563
1564 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1565 grp = GICV3_G1NS;
1566 }
1567
1568 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1569 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1570
1571
1572
1573 grp = GICV3_G0;
1574 }
1575
1576 if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1577 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1578
1579 grp = GICV3_G0;
1580 satinc = true;
1581 }
1582
1583 bpr = cs->icc_bpr[grp];
1584 if (satinc) {
1585 bpr++;
1586 bpr = MIN(bpr, 7);
1587 }
1588
1589 trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
1590
1591 return bpr;
1592}
1593
1594static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1595 uint64_t value)
1596{
1597 GICv3CPUState *cs = icc_cs_from_env(env);
1598 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1599 uint64_t minval;
1600
1601 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1602 icv_bpr_write(env, ri, value);
1603 return;
1604 }
1605
1606 trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
1607 gicv3_redist_affid(cs), value);
1608
1609 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1610 grp = GICV3_G1NS;
1611 }
1612
1613 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1614 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1615
1616
1617
1618 grp = GICV3_G0;
1619 }
1620
1621 if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1622 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1623
1624 return;
1625 }
1626
1627 minval = (grp == GICV3_G1NS) ? icc_min_bpr_ns(cs) : icc_min_bpr(cs);
1628 if (value < minval) {
1629 value = minval;
1630 }
1631
1632 cs->icc_bpr[grp] = value & 7;
1633 gicv3_cpuif_update(cs);
1634}
1635
1636static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1637{
1638 GICv3CPUState *cs = icc_cs_from_env(env);
1639 uint64_t value;
1640
1641 int regno = ri->opc2 & 3;
1642 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1643
1644 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1645 return icv_ap_read(env, ri);
1646 }
1647
1648 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1649 grp = GICV3_G1NS;
1650 }
1651
1652 value = cs->icc_apr[grp][regno];
1653
1654 trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1655 return value;
1656}
1657
1658static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1659 uint64_t value)
1660{
1661 GICv3CPUState *cs = icc_cs_from_env(env);
1662
1663 int regno = ri->opc2 & 3;
1664 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1665
1666 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1667 icv_ap_write(env, ri, value);
1668 return;
1669 }
1670
1671 trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1672
1673 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1674 grp = GICV3_G1NS;
1675 }
1676
1677
1678
1679
1680
1681
1682 if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
1683 return;
1684 }
1685
1686 cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
1687 gicv3_cpuif_update(cs);
1688}
1689
1690static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1691 uint64_t value)
1692{
1693
1694 GICv3CPUState *cs = icc_cs_from_env(env);
1695 int irq = value & 0xffffff;
1696 bool irq_is_secure, single_sec_state, irq_is_grp0;
1697 bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
1698
1699 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1700 icv_dir_write(env, ri, value);
1701 return;
1702 }
1703
1704 trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
1705
1706 if (irq >= cs->gic->num_irq) {
1707
1708 return;
1709 }
1710
1711 if (!icc_eoi_split(env, cs)) {
1712 return;
1713 }
1714
1715 int grp = gicv3_irq_group(cs->gic, cs, irq);
1716
1717 single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
1718 irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
1719 irq_is_grp0 = grp == GICV3_G0;
1720
1721
1722
1723
1724
1725 route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
1726 route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
1727
1728
1729
1730 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1731 route_fiq_to_el2 = hcr_el2 & HCR_FMO;
1732 route_irq_to_el2 = hcr_el2 & HCR_IMO;
1733
1734 switch (arm_current_el(env)) {
1735 case 3:
1736 break;
1737 case 2:
1738 if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
1739 break;
1740 }
1741 if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
1742 break;
1743 }
1744 return;
1745 case 1:
1746 if (!arm_is_secure_below_el3(env)) {
1747 if (single_sec_state && irq_is_grp0 &&
1748 !route_fiq_to_el3 && !route_fiq_to_el2) {
1749 break;
1750 }
1751 if (!irq_is_secure && !irq_is_grp0 &&
1752 !route_irq_to_el3 && !route_irq_to_el2) {
1753 break;
1754 }
1755 } else {
1756 if (irq_is_grp0 && !route_fiq_to_el3) {
1757 break;
1758 }
1759 if (!irq_is_grp0 &&
1760 (!irq_is_secure || !single_sec_state) &&
1761 !route_irq_to_el3) {
1762 break;
1763 }
1764 }
1765 return;
1766 default:
1767 g_assert_not_reached();
1768 }
1769
1770 icc_deactivate_irq(cs, irq);
1771}
1772
1773static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1774{
1775 GICv3CPUState *cs = icc_cs_from_env(env);
1776 int prio;
1777
1778 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1779 return icv_rpr_read(env, ri);
1780 }
1781
1782 prio = icc_highest_active_prio(cs);
1783
1784 if (arm_feature(env, ARM_FEATURE_EL3) &&
1785 !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
1786
1787 if ((prio & 0x80) == 0) {
1788
1789 prio = 0;
1790 } else if (prio != 0xff) {
1791
1792 prio = (prio << 1) & 0xff;
1793 }
1794 }
1795
1796 trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
1797 return prio;
1798}
1799
1800static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
1801 uint64_t value, int grp, bool ns)
1802{
1803 GICv3State *s = cs->gic;
1804
1805
1806 uint64_t aff = extract64(value, 48, 8) << 16 |
1807 extract64(value, 32, 8) << 8 |
1808 extract64(value, 16, 8);
1809 uint32_t targetlist = extract64(value, 0, 16);
1810 uint32_t irq = extract64(value, 24, 4);
1811 bool irm = extract64(value, 40, 1);
1812 int i;
1813
1814 if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
1815
1816
1817
1818
1819 grp = GICV3_G0;
1820 }
1821
1822 trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
1823 aff, targetlist);
1824
1825 for (i = 0; i < s->num_cpu; i++) {
1826 GICv3CPUState *ocs = &s->cpu[i];
1827
1828 if (irm) {
1829
1830 if (cs == ocs) {
1831 continue;
1832 }
1833 } else {
1834
1835
1836
1837 int aff0;
1838
1839 if (ocs->gicr_typer >> 40 != aff) {
1840 continue;
1841 }
1842 aff0 = extract64(ocs->gicr_typer, 32, 8);
1843 if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
1844 continue;
1845 }
1846 }
1847
1848
1849 gicv3_redist_send_sgi(ocs, grp, irq, ns);
1850 }
1851}
1852
1853static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1854 uint64_t value)
1855{
1856
1857 GICv3CPUState *cs = icc_cs_from_env(env);
1858 bool ns = !arm_is_secure(env);
1859
1860 icc_generate_sgi(env, cs, value, GICV3_G0, ns);
1861}
1862
1863static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1864 uint64_t value)
1865{
1866
1867 GICv3CPUState *cs = icc_cs_from_env(env);
1868 int grp;
1869 bool ns = !arm_is_secure(env);
1870
1871 grp = ns ? GICV3_G1NS : GICV3_G1;
1872 icc_generate_sgi(env, cs, value, grp, ns);
1873}
1874
1875static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1876 uint64_t value)
1877{
1878
1879
1880
1881 GICv3CPUState *cs = icc_cs_from_env(env);
1882 int grp;
1883 bool ns = !arm_is_secure(env);
1884
1885 grp = ns ? GICV3_G1 : GICV3_G1NS;
1886 icc_generate_sgi(env, cs, value, grp, ns);
1887}
1888
1889static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
1890{
1891 GICv3CPUState *cs = icc_cs_from_env(env);
1892 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1893 uint64_t value;
1894
1895 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1896 return icv_igrpen_read(env, ri);
1897 }
1898
1899 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1900 grp = GICV3_G1NS;
1901 }
1902
1903 value = cs->icc_igrpen[grp];
1904 trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
1905 gicv3_redist_affid(cs), value);
1906 return value;
1907}
1908
1909static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
1910 uint64_t value)
1911{
1912 GICv3CPUState *cs = icc_cs_from_env(env);
1913 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1914
1915 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1916 icv_igrpen_write(env, ri, value);
1917 return;
1918 }
1919
1920 trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
1921 gicv3_redist_affid(cs), value);
1922
1923 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1924 grp = GICV3_G1NS;
1925 }
1926
1927 cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
1928 gicv3_cpuif_update(cs);
1929}
1930
1931static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1932{
1933 GICv3CPUState *cs = icc_cs_from_env(env);
1934 uint64_t value;
1935
1936
1937 value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
1938 trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
1939 return value;
1940}
1941
1942static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1943 uint64_t value)
1944{
1945 GICv3CPUState *cs = icc_cs_from_env(env);
1946
1947 trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
1948
1949
1950 cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
1951 cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
1952 gicv3_cpuif_update(cs);
1953}
1954
1955static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1956{
1957 GICv3CPUState *cs = icc_cs_from_env(env);
1958 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1959 uint64_t value;
1960
1961 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1962 return icv_ctlr_read(env, ri);
1963 }
1964
1965 value = cs->icc_ctlr_el1[bank];
1966 trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
1967 return value;
1968}
1969
1970static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1971 uint64_t value)
1972{
1973 GICv3CPUState *cs = icc_cs_from_env(env);
1974 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1975 uint64_t mask;
1976
1977 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1978 icv_ctlr_write(env, ri, value);
1979 return;
1980 }
1981
1982 trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
1983
1984
1985
1986
1987
1988
1989 if (arm_feature(env, ARM_FEATURE_EL3) &&
1990 ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
1991 mask = ICC_CTLR_EL1_EOIMODE;
1992 } else {
1993 mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
1994 }
1995
1996 cs->icc_ctlr_el1[bank] &= ~mask;
1997 cs->icc_ctlr_el1[bank] |= (value & mask);
1998 gicv3_cpuif_update(cs);
1999}
2000
2001
2002static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
2003{
2004 GICv3CPUState *cs = icc_cs_from_env(env);
2005 uint64_t value;
2006
2007 value = cs->icc_ctlr_el3;
2008 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
2009 value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
2010 }
2011 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
2012 value |= ICC_CTLR_EL3_CBPR_EL1NS;
2013 }
2014 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
2015 value |= ICC_CTLR_EL3_EOIMODE_EL1S;
2016 }
2017 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
2018 value |= ICC_CTLR_EL3_CBPR_EL1S;
2019 }
2020
2021 trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
2022 return value;
2023}
2024
2025static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2026 uint64_t value)
2027{
2028 GICv3CPUState *cs = icc_cs_from_env(env);
2029 uint64_t mask;
2030
2031 trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
2032
2033
2034 cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
2035 if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
2036 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
2037 }
2038 if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
2039 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
2040 }
2041
2042 cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
2043 if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
2044 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
2045 }
2046 if (value & ICC_CTLR_EL3_CBPR_EL1S) {
2047 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
2048 }
2049
2050
2051 mask = ICC_CTLR_EL3_EOIMODE_EL3;
2052
2053 cs->icc_ctlr_el3 &= ~mask;
2054 cs->icc_ctlr_el3 |= (value & mask);
2055 gicv3_cpuif_update(cs);
2056}
2057
2058static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
2059 const ARMCPRegInfo *ri, bool isread)
2060{
2061 CPAccessResult r = CP_ACCESS_OK;
2062 GICv3CPUState *cs = icc_cs_from_env(env);
2063 int el = arm_current_el(env);
2064
2065 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
2066 el == 1 && !arm_is_secure_below_el3(env)) {
2067
2068 return CP_ACCESS_TRAP_EL2;
2069 }
2070
2071 if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
2072 switch (el) {
2073 case 1:
2074
2075 if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) {
2076 r = CP_ACCESS_TRAP_EL3;
2077 }
2078 break;
2079 case 2:
2080 r = CP_ACCESS_TRAP_EL3;
2081 break;
2082 case 3:
2083 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2084 r = CP_ACCESS_TRAP_EL3;
2085 }
2086 break;
2087 default:
2088 g_assert_not_reached();
2089 }
2090 }
2091
2092 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2093 r = CP_ACCESS_TRAP;
2094 }
2095 return r;
2096}
2097
2098static CPAccessResult gicv3_dir_access(CPUARMState *env,
2099 const ARMCPRegInfo *ri, bool isread)
2100{
2101 GICv3CPUState *cs = icc_cs_from_env(env);
2102
2103 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
2104 arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
2105
2106 return CP_ACCESS_TRAP_EL2;
2107 }
2108
2109 return gicv3_irqfiq_access(env, ri, isread);
2110}
2111
2112static CPAccessResult gicv3_sgi_access(CPUARMState *env,
2113 const ARMCPRegInfo *ri, bool isread)
2114{
2115 if (arm_current_el(env) == 1 &&
2116 (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) {
2117
2118 return CP_ACCESS_TRAP_EL2;
2119 }
2120
2121 return gicv3_irqfiq_access(env, ri, isread);
2122}
2123
2124static CPAccessResult gicv3_fiq_access(CPUARMState *env,
2125 const ARMCPRegInfo *ri, bool isread)
2126{
2127 CPAccessResult r = CP_ACCESS_OK;
2128 GICv3CPUState *cs = icc_cs_from_env(env);
2129 int el = arm_current_el(env);
2130
2131 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
2132 el == 1 && !arm_is_secure_below_el3(env)) {
2133
2134 return CP_ACCESS_TRAP_EL2;
2135 }
2136
2137 if (env->cp15.scr_el3 & SCR_FIQ) {
2138 switch (el) {
2139 case 1:
2140 if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) {
2141 r = CP_ACCESS_TRAP_EL3;
2142 }
2143 break;
2144 case 2:
2145 r = CP_ACCESS_TRAP_EL3;
2146 break;
2147 case 3:
2148 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2149 r = CP_ACCESS_TRAP_EL3;
2150 }
2151 break;
2152 default:
2153 g_assert_not_reached();
2154 }
2155 }
2156
2157 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2158 r = CP_ACCESS_TRAP;
2159 }
2160 return r;
2161}
2162
2163static CPAccessResult gicv3_irq_access(CPUARMState *env,
2164 const ARMCPRegInfo *ri, bool isread)
2165{
2166 CPAccessResult r = CP_ACCESS_OK;
2167 GICv3CPUState *cs = icc_cs_from_env(env);
2168 int el = arm_current_el(env);
2169
2170 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
2171 el == 1 && !arm_is_secure_below_el3(env)) {
2172
2173 return CP_ACCESS_TRAP_EL2;
2174 }
2175
2176 if (env->cp15.scr_el3 & SCR_IRQ) {
2177 switch (el) {
2178 case 1:
2179 if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) {
2180 r = CP_ACCESS_TRAP_EL3;
2181 }
2182 break;
2183 case 2:
2184 r = CP_ACCESS_TRAP_EL3;
2185 break;
2186 case 3:
2187 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2188 r = CP_ACCESS_TRAP_EL3;
2189 }
2190 break;
2191 default:
2192 g_assert_not_reached();
2193 }
2194 }
2195
2196 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2197 r = CP_ACCESS_TRAP;
2198 }
2199 return r;
2200}
2201
2202static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2203{
2204 GICv3CPUState *cs = icc_cs_from_env(env);
2205
2206 cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
2207 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2208 ((cs->pribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
2209 cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
2210 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2211 ((cs->pribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
2212 cs->icc_pmr_el1 = 0;
2213 cs->icc_bpr[GICV3_G0] = icc_min_bpr(cs);
2214 cs->icc_bpr[GICV3_G1] = icc_min_bpr(cs);
2215 cs->icc_bpr[GICV3_G1NS] = icc_min_bpr_ns(cs);
2216 memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
2217 memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
2218 cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
2219 (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
2220 ((cs->pribits - 1) << ICC_CTLR_EL3_PRIBITS_SHIFT);
2221
2222 memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
2223 cs->ich_hcr_el2 = 0;
2224 memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
2225 cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
2226 ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
2227 (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
2228}
2229
2230static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
2231 { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
2232 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
2233 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2234 .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2235 .readfn = icc_pmr_read,
2236 .writefn = icc_pmr_write,
2237
2238
2239
2240
2241 .resetfn = icc_reset,
2242 },
2243 { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
2244 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
2245 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2246 .access = PL1_R, .accessfn = gicv3_fiq_access,
2247 .readfn = icc_iar0_read,
2248 },
2249 { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
2250 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
2251 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2252 .access = PL1_W, .accessfn = gicv3_fiq_access,
2253 .writefn = icc_eoir_write,
2254 },
2255 { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
2256 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
2257 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2258 .access = PL1_R, .accessfn = gicv3_fiq_access,
2259 .readfn = icc_hppir0_read,
2260 },
2261 { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
2262 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
2263 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2264 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2265 .readfn = icc_bpr_read,
2266 .writefn = icc_bpr_write,
2267 },
2268 { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
2269 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
2270 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2271 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2272 .readfn = icc_ap_read,
2273 .writefn = icc_ap_write,
2274 },
2275
2276 { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
2277 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
2278 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2279 .access = PL1_RW, .accessfn = gicv3_irq_access,
2280 .readfn = icc_ap_read,
2281 .writefn = icc_ap_write,
2282 },
2283 { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
2284 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
2285 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2286 .access = PL1_W, .accessfn = gicv3_dir_access,
2287 .writefn = icc_dir_write,
2288 },
2289 { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
2290 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
2291 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2292 .access = PL1_R, .accessfn = gicv3_irqfiq_access,
2293 .readfn = icc_rpr_read,
2294 },
2295 { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
2296 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
2297 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2298 .access = PL1_W, .accessfn = gicv3_sgi_access,
2299 .writefn = icc_sgi1r_write,
2300 },
2301 { .name = "ICC_SGI1R",
2302 .cp = 15, .opc1 = 0, .crm = 12,
2303 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2304 .access = PL1_W, .accessfn = gicv3_sgi_access,
2305 .writefn = icc_sgi1r_write,
2306 },
2307 { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
2308 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
2309 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2310 .access = PL1_W, .accessfn = gicv3_sgi_access,
2311 .writefn = icc_asgi1r_write,
2312 },
2313 { .name = "ICC_ASGI1R",
2314 .cp = 15, .opc1 = 1, .crm = 12,
2315 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2316 .access = PL1_W, .accessfn = gicv3_sgi_access,
2317 .writefn = icc_asgi1r_write,
2318 },
2319 { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
2320 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
2321 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2322 .access = PL1_W, .accessfn = gicv3_sgi_access,
2323 .writefn = icc_sgi0r_write,
2324 },
2325 { .name = "ICC_SGI0R",
2326 .cp = 15, .opc1 = 2, .crm = 12,
2327 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2328 .access = PL1_W, .accessfn = gicv3_sgi_access,
2329 .writefn = icc_sgi0r_write,
2330 },
2331 { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
2332 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
2333 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2334 .access = PL1_R, .accessfn = gicv3_irq_access,
2335 .readfn = icc_iar1_read,
2336 },
2337 { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
2338 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
2339 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2340 .access = PL1_W, .accessfn = gicv3_irq_access,
2341 .writefn = icc_eoir_write,
2342 },
2343 { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
2344 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
2345 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2346 .access = PL1_R, .accessfn = gicv3_irq_access,
2347 .readfn = icc_hppir1_read,
2348 },
2349
2350 { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
2351 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
2352 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2353 .access = PL1_RW, .accessfn = gicv3_irq_access,
2354 .readfn = icc_bpr_read,
2355 .writefn = icc_bpr_write,
2356 },
2357
2358 { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
2359 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
2360 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2361 .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2362 .readfn = icc_ctlr_el1_read,
2363 .writefn = icc_ctlr_el1_write,
2364 },
2365 { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
2366 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
2367 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2368 .access = PL1_RW,
2369
2370
2371
2372
2373
2374 .resetvalue = 0x7,
2375 },
2376 { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
2377 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
2378 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2379 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2380 .readfn = icc_igrpen_read,
2381 .writefn = icc_igrpen_write,
2382 },
2383
2384 { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
2385 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
2386 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2387 .access = PL1_RW, .accessfn = gicv3_irq_access,
2388 .readfn = icc_igrpen_read,
2389 .writefn = icc_igrpen_write,
2390 },
2391 { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
2392 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
2393 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2394 .access = PL2_RW,
2395
2396
2397
2398 .resetvalue = 0xf,
2399 },
2400 { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
2401 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
2402 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2403 .access = PL3_RW,
2404 .readfn = icc_ctlr_el3_read,
2405 .writefn = icc_ctlr_el3_write,
2406 },
2407 { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
2408 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
2409 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2410 .access = PL3_RW,
2411
2412
2413
2414 .resetvalue = 0xf,
2415 },
2416 { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
2417 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
2418 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2419 .access = PL3_RW,
2420 .readfn = icc_igrpen1_el3_read,
2421 .writefn = icc_igrpen1_el3_write,
2422 },
2423};
2424
2425static const ARMCPRegInfo gicv3_cpuif_icc_apxr1_reginfo[] = {
2426 { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
2427 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
2428 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2429 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2430 .readfn = icc_ap_read,
2431 .writefn = icc_ap_write,
2432 },
2433 { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
2434 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
2435 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2436 .access = PL1_RW, .accessfn = gicv3_irq_access,
2437 .readfn = icc_ap_read,
2438 .writefn = icc_ap_write,
2439 },
2440};
2441
2442static const ARMCPRegInfo gicv3_cpuif_icc_apxr23_reginfo[] = {
2443 { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
2444 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
2445 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2446 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2447 .readfn = icc_ap_read,
2448 .writefn = icc_ap_write,
2449 },
2450 { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
2451 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
2452 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2453 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2454 .readfn = icc_ap_read,
2455 .writefn = icc_ap_write,
2456 },
2457 { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
2458 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
2459 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2460 .access = PL1_RW, .accessfn = gicv3_irq_access,
2461 .readfn = icc_ap_read,
2462 .writefn = icc_ap_write,
2463 },
2464 { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
2465 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
2466 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2467 .access = PL1_RW, .accessfn = gicv3_irq_access,
2468 .readfn = icc_ap_read,
2469 .writefn = icc_ap_write,
2470 },
2471};
2472
2473static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2474{
2475 GICv3CPUState *cs = icc_cs_from_env(env);
2476 int regno = ri->opc2 & 3;
2477 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2478 uint64_t value;
2479
2480 value = cs->ich_apr[grp][regno];
2481 trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2482 return value;
2483}
2484
2485static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2486 uint64_t value)
2487{
2488 GICv3CPUState *cs = icc_cs_from_env(env);
2489 int regno = ri->opc2 & 3;
2490 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2491
2492 trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2493
2494 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
2495 gicv3_cpuif_virt_irq_fiq_update(cs);
2496}
2497
2498static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2499{
2500 GICv3CPUState *cs = icc_cs_from_env(env);
2501 uint64_t value = cs->ich_hcr_el2;
2502
2503 trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
2504 return value;
2505}
2506
2507static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2508 uint64_t value)
2509{
2510 GICv3CPUState *cs = icc_cs_from_env(env);
2511
2512 trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
2513
2514 value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
2515 ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
2516 ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
2517 ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
2518 ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
2519
2520 cs->ich_hcr_el2 = value;
2521 gicv3_cpuif_virt_update(cs);
2522}
2523
2524static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2525{
2526 GICv3CPUState *cs = icc_cs_from_env(env);
2527 uint64_t value = cs->ich_vmcr_el2;
2528
2529 trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
2530 return value;
2531}
2532
2533static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2534 uint64_t value)
2535{
2536 GICv3CPUState *cs = icc_cs_from_env(env);
2537
2538 trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
2539
2540 value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
2541 ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
2542 ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
2543 value |= ICH_VMCR_EL2_VFIQEN;
2544
2545 cs->ich_vmcr_el2 = value;
2546
2547
2548
2549 write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0));
2550 write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
2551
2552 gicv3_cpuif_virt_update(cs);
2553}
2554
2555static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2556{
2557 GICv3CPUState *cs = icc_cs_from_env(env);
2558 int regno = ri->opc2 | ((ri->crm & 1) << 3);
2559 uint64_t value;
2560
2561
2562
2563
2564
2565
2566 if (ri->state == ARM_CP_STATE_AA32) {
2567 if (ri->crm >= 14) {
2568 value = extract64(cs->ich_lr_el2[regno], 32, 32);
2569 trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
2570 } else {
2571 value = extract64(cs->ich_lr_el2[regno], 0, 32);
2572 trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
2573 }
2574 } else {
2575 value = cs->ich_lr_el2[regno];
2576 trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
2577 }
2578
2579 return value;
2580}
2581
2582static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2583 uint64_t value)
2584{
2585 GICv3CPUState *cs = icc_cs_from_env(env);
2586 int regno = ri->opc2 | ((ri->crm & 1) << 3);
2587
2588
2589
2590
2591
2592
2593 if (ri->state == ARM_CP_STATE_AA32) {
2594 if (ri->crm >= 14) {
2595 trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
2596 value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
2597 } else {
2598 trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
2599 value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
2600 }
2601 } else {
2602 trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
2603 }
2604
2605
2606 if (cs->vpribits < 8) {
2607 value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
2608 8 - cs->vpribits, 0);
2609 }
2610
2611 cs->ich_lr_el2[regno] = value;
2612 gicv3_cpuif_virt_update(cs);
2613}
2614
2615static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2616{
2617 GICv3CPUState *cs = icc_cs_from_env(env);
2618 uint64_t value;
2619
2620 value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
2621 | ICH_VTR_EL2_TDS | ICH_VTR_EL2_A3V
2622 | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
2623 | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
2624 | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
2625
2626 if (cs->gic->revision < 4) {
2627 value |= ICH_VTR_EL2_NV4;
2628 }
2629
2630 trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
2631 return value;
2632}
2633
2634static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2635{
2636 GICv3CPUState *cs = icc_cs_from_env(env);
2637 uint64_t value = maintenance_interrupt_state(cs);
2638
2639 trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
2640 return value;
2641}
2642
2643static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2644{
2645 GICv3CPUState *cs = icc_cs_from_env(env);
2646 uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
2647
2648 trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
2649 return value;
2650}
2651
2652static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2653{
2654 GICv3CPUState *cs = icc_cs_from_env(env);
2655 uint64_t value = 0;
2656 int i;
2657
2658 for (i = 0; i < cs->num_list_regs; i++) {
2659 uint64_t lr = cs->ich_lr_el2[i];
2660
2661 if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
2662 ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
2663 value |= (1 << i);
2664 }
2665 }
2666
2667 trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
2668 return value;
2669}
2670
2671static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
2672 { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
2673 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
2674 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2675 .access = PL2_RW,
2676 .readfn = ich_ap_read,
2677 .writefn = ich_ap_write,
2678 },
2679 { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
2680 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
2681 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2682 .access = PL2_RW,
2683 .readfn = ich_ap_read,
2684 .writefn = ich_ap_write,
2685 },
2686 { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
2687 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
2688 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2689 .access = PL2_RW,
2690 .readfn = ich_hcr_read,
2691 .writefn = ich_hcr_write,
2692 },
2693 { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
2694 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
2695 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2696 .access = PL2_R,
2697 .readfn = ich_vtr_read,
2698 },
2699 { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
2700 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
2701 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2702 .access = PL2_R,
2703 .readfn = ich_misr_read,
2704 },
2705 { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
2706 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
2707 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2708 .access = PL2_R,
2709 .readfn = ich_eisr_read,
2710 },
2711 { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
2712 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
2713 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2714 .access = PL2_R,
2715 .readfn = ich_elrsr_read,
2716 },
2717 { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
2718 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
2719 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2720 .access = PL2_RW,
2721 .readfn = ich_vmcr_read,
2722 .writefn = ich_vmcr_write,
2723 },
2724};
2725
2726static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
2727 { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
2728 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
2729 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2730 .access = PL2_RW,
2731 .readfn = ich_ap_read,
2732 .writefn = ich_ap_write,
2733 },
2734 { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
2735 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
2736 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2737 .access = PL2_RW,
2738 .readfn = ich_ap_read,
2739 .writefn = ich_ap_write,
2740 },
2741};
2742
2743static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
2744 { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
2745 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
2746 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2747 .access = PL2_RW,
2748 .readfn = ich_ap_read,
2749 .writefn = ich_ap_write,
2750 },
2751 { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
2752 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
2753 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2754 .access = PL2_RW,
2755 .readfn = ich_ap_read,
2756 .writefn = ich_ap_write,
2757 },
2758 { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
2759 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
2760 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2761 .access = PL2_RW,
2762 .readfn = ich_ap_read,
2763 .writefn = ich_ap_write,
2764 },
2765 { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
2766 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
2767 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2768 .access = PL2_RW,
2769 .readfn = ich_ap_read,
2770 .writefn = ich_ap_write,
2771 },
2772};
2773
2774static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
2775{
2776 GICv3CPUState *cs = opaque;
2777
2778 gicv3_cpuif_update(cs);
2779
2780
2781
2782
2783
2784 gicv3_cpuif_virt_irq_fiq_update(cs);
2785}
2786
2787void gicv3_init_cpuif(GICv3State *s)
2788{
2789
2790
2791
2792 int i;
2793
2794 for (i = 0; i < s->num_cpu; i++) {
2795 ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
2796 GICv3CPUState *cs = &s->cpu[i];
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815 define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
2816
2817
2818
2819
2820
2821
2822
2823 if (s->force_8bit_prio) {
2824 cs->pribits = 8;
2825 } else {
2826 cs->pribits = cpu->gic_pribits ?: 5;
2827 }
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839 cs->prebits = cs->pribits;
2840 if (cs->prebits == 8) {
2841 cs->prebits--;
2842 }
2843
2844
2845
2846
2847 g_assert(cs->pribits >= 4 && cs->pribits <= 8);
2848
2849
2850
2851
2852
2853 if (cs->prebits >= 6) {
2854 define_arm_cp_regs(cpu, gicv3_cpuif_icc_apxr1_reginfo);
2855 }
2856 if (cs->prebits == 7) {
2857 define_arm_cp_regs(cpu, gicv3_cpuif_icc_apxr23_reginfo);
2858 }
2859
2860 if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
2861 int j;
2862
2863 cs->num_list_regs = cpu->gic_num_lrs ?: 4;
2864 cs->vpribits = cpu->gic_vpribits ?: 5;
2865 cs->vprebits = cpu->gic_vprebits ?: 5;
2866
2867
2868
2869
2870
2871 g_assert(cs->vprebits <= cs->vpribits);
2872 g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
2873 g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
2874
2875 define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
2876
2877 for (j = 0; j < cs->num_list_regs; j++) {
2878
2879
2880
2881
2882 ARMCPRegInfo lr_regset[] = {
2883 { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
2884 .opc0 = 3, .opc1 = 4, .crn = 12,
2885 .crm = 12 + (j >> 3), .opc2 = j & 7,
2886 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2887 .access = PL2_RW,
2888 .readfn = ich_lr_read,
2889 .writefn = ich_lr_write,
2890 },
2891 { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
2892 .cp = 15, .opc1 = 4, .crn = 12,
2893 .crm = 14 + (j >> 3), .opc2 = j & 7,
2894 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2895 .access = PL2_RW,
2896 .readfn = ich_lr_read,
2897 .writefn = ich_lr_write,
2898 },
2899 };
2900 define_arm_cp_regs(cpu, lr_regset);
2901 }
2902 if (cs->vprebits >= 6) {
2903 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
2904 }
2905 if (cs->vprebits == 7) {
2906 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
2907 }
2908 }
2909 arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);
2910 }
2911}
2912