summaryrefslogtreecommitdiff
path: root/src/dev/arm/gic_v3_redistributor.cc
blob: 8b6233c2204edd91004d3dcbe63c22f0bd02a43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
/*
 * Copyright (c) 2019 ARM Limited
 * All rights reserved
 *
 * The license below extends only to copyright in the software and shall
 * not be construed as granting a license to any other intellectual
 * property including but not limited to intellectual property relating
 * to a hardware implementation of the functionality of the software
 * licensed hereunder.  You may use the software subject to the license
 * terms below provided that you ensure that this notice is replicated
 * unmodified and in its entirety in all distributions of the software,
 * modified or unmodified, in source code or in binary form.
 *
 * Copyright (c) 2018 Metempsy Technology Consulting
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jairo Balart
 */

#include "dev/arm/gic_v3_redistributor.hh"

#include "arch/arm/utility.hh"
#include "debug/GIC.hh"
#include "dev/arm/gic_v3_cpu_interface.hh"
#include "dev/arm/gic_v3_distributor.hh"
#include "mem/fs_translating_port_proxy.hh"

const AddrRange Gicv3Redistributor::GICR_IPRIORITYR(SGI_base + 0x0400,
                                                    SGI_base + 0x041f);

Gicv3Redistributor::Gicv3Redistributor(Gicv3 * gic, uint32_t cpu_id)
    : gic(gic),
      distributor(nullptr),
      cpuInterface(nullptr),
      cpuId(cpu_id),
      memProxy(nullptr),
      peInLowPowerState(true),
      irqGroup(Gicv3::SGI_MAX + Gicv3::PPI_MAX, 0),
      irqEnabled(Gicv3::SGI_MAX + Gicv3::PPI_MAX, false),
      irqPending(Gicv3::SGI_MAX + Gicv3::PPI_MAX, false),
      irqActive(Gicv3::SGI_MAX + Gicv3::PPI_MAX, false),
      irqPriority(Gicv3::SGI_MAX + Gicv3::PPI_MAX, 0),
      irqConfig(Gicv3::SGI_MAX + Gicv3::PPI_MAX, Gicv3::INT_EDGE_TRIGGERED),
      irqGrpmod(Gicv3::SGI_MAX + Gicv3::PPI_MAX, 0),
      irqNsacr(Gicv3::SGI_MAX + Gicv3::PPI_MAX, 0),
      DPG1S(false),
      DPG1NS(false),
      DPG0(false),
      EnableLPIs(false),
      lpiConfigurationTablePtr(0),
      lpiIDBits(0),
      lpiPendingTablePtr(0),
      addrRangeSize(gic->params()->gicv4 ? 0x40000 : 0x20000)
{
}

void
Gicv3Redistributor::init()
{
    distributor = gic->getDistributor();
    cpuInterface = gic->getCPUInterface(cpuId);

    memProxy = &gic->getSystem()->physProxy;
}

uint64_t
Gicv3Redistributor::read(Addr addr, size_t size, bool is_secure_access)
{
    if (GICR_IPRIORITYR.contains(addr)) { // Interrupt Priority Registers
        uint64_t value = 0;
        int first_intid = addr - GICR_IPRIORITYR.start();

        for (int i = 0, int_id = first_intid; i < size; i++, int_id++) {
            uint8_t prio = irqPriority[int_id];

            if (!distributor->DS && !is_secure_access) {
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    // RAZ/WI for non-secure accesses for secure interrupts
                    continue;
                } else {
                    // NS view
                    prio = (prio << 1) & 0xff;
                }
            }

            value |= prio << (i * 8);
        }

        return value;
    }

    switch (addr) {
      case GICR_CTLR: { // Control Register
          uint64_t value = 0;

          if (DPG1S) {
              value |= GICR_CTLR_DPG1S;
          }

          if (DPG1NS) {
              value |= GICR_CTLR_DPG1NS;
          }

          if (DPG0) {
              value |= GICR_CTLR_DPG0;
          }

          if (EnableLPIs) {
              value |= GICR_CTLR_ENABLE_LPIS;
          }

          return value;
      }

      case GICR_IIDR: // Implementer Identification Register
        //return 0x43b; // r0p0 GIC-500
        return 0;

      case GICR_TYPER: { // Type Register
          /*
           * Affinity_Value   [63:32] == X
           * (The identity of the PE associated with this Redistributor)
           * CommonLPIAff     [25:24] == 01
           * (All Redistributors with the same Aff3 value must share an
           * LPI Configuration table)
           * Processor_Number [23:8]  == X
           * (A unique identifier for the PE)
           * DPGS             [5]     == 1
           * (GICR_CTLR.DPG* bits are supported)
           * Last             [4]     == X
           * (This Redistributor is the highest-numbered Redistributor in
           * a series of contiguous Redistributor pages)
           * DirectLPI        [3]     == 1
           * (direct injection of LPIs supported)
           * VLPIS            [1]     == 0
           * (virtual LPIs not supported)
           * PLPIS            [0]     == 1
           * (physical LPIs supported)
           */
          uint64_t affinity = getAffinity();
          int last = cpuId == (gic->getSystem()->numContexts() - 1);
          return (affinity << 32) | (1 << 24) | (cpuId << 8) |
              (1 << 5) | (last << 4) | (1 << 3) | (1 << 0);
      }

      case GICR_WAKER: // Wake Register
        if (!distributor->DS && !is_secure_access) {
            // RAZ/WI for non-secure accesses
            return 0;
        }

        if (peInLowPowerState) {
            return GICR_WAKER_ChildrenAsleep | GICR_WAKER_ProcessorSleep;
        } else {
            return 0;
        }

      case GICR_PIDR0: { // Peripheral ID0 Register
          return 0x92; // Part number, bits[7:0]
      }

      case GICR_PIDR1: { // Peripheral ID1 Register
          uint8_t des_0 = 0xB; // JEP106 identification code, bits[3:0]
          uint8_t part_1 = 0x4; // Part number, bits[11:8]
          return (des_0 << 4) | (part_1 << 0);
      }

      case GICR_PIDR2: { // Peripheral ID2 Register
          uint8_t arch_rev = 0x3; // 0x3 GICv3
          uint8_t jedec = 0x1; // JEP code
          uint8_t des_1 = 0x3; // JEP106 identification code, bits[6:4]
          return (arch_rev << 4) | (jedec << 3) | (des_1 << 0);
      }

      case GICR_PIDR3: // Peripheral ID3 Register
        return 0x0; // Implementation defined

      case GICR_PIDR4: { // Peripheral ID4 Register
          uint8_t size = 0x4; // 64 KB software visible page
          uint8_t des_2 = 0x4; // ARM implementation
          return (size << 4) | (des_2 << 0);
      }

      case GICR_PIDR5: // Peripheral ID5 Register
      case GICR_PIDR6: // Peripheral ID6 Register
      case GICR_PIDR7: // Peripheral ID7 Register
        return 0; // RES0

      case GICR_IGROUPR0: { // Interrupt Group Register 0
          uint64_t value = 0;

          if (!distributor->DS && !is_secure_access) {
              // RAZ/WI for non-secure accesses
              return 0;
          }

          for (int int_id = 0; int_id < 8 * size; int_id++) {
              value |= (irqGroup[int_id] << int_id);
          }

          return value;
      }

      case GICR_ISENABLER0: // Interrupt Set-Enable Register 0
      case GICR_ICENABLER0: { // Interrupt Clear-Enable Register 0
          uint64_t value = 0;

          for (int int_id = 0; int_id < 8 * size; int_id++) {
              if (!distributor->DS && !is_secure_access) {
                  // RAZ/WI for non-secure accesses for secure interrupts
                  if (getIntGroup(int_id) != Gicv3::G1NS) {
                      continue;
                  }
              }

              if (irqEnabled[int_id]) {
                  value |= (1 << int_id);
              }
          }

          return value;
      }

      case GICR_ISPENDR0: // Interrupt Set-Pending Register 0
      case GICR_ICPENDR0: { // Interrupt Clear-Pending Register 0
          uint64_t value = 0;

          for (int int_id = 0; int_id < 8 * size; int_id++) {
              if (!distributor->DS && !is_secure_access) {
                  // RAZ/WI for non-secure accesses for secure interrupts
                  if (getIntGroup(int_id) != Gicv3::G1NS) {
                      continue;
                  }
              }

              value |= (irqPending[int_id] << int_id);
          }

          return value;
      }

      case GICR_ISACTIVER0: // Interrupt Set-Active Register 0
      case GICR_ICACTIVER0: { // Interrupt Clear-Active Register 0
          uint64_t value = 0;

          for (int int_id = 0; int_id < 8 * size; int_id++) {
              if (!distributor->DS && !is_secure_access) {
                  // RAZ/WI for non-secure accesses for secure interrupts
                  if (getIntGroup(int_id) != Gicv3::G1NS) {
                      continue;
                  }
              }

              value |=  irqActive[int_id] << int_id;
          }

          return value;
      }

      case GICR_ICFGR0: // SGI Configuration Register
      case GICR_ICFGR1: { // PPI Configuration Register
          uint64_t value = 0;
          uint32_t first_int_id = addr == GICR_ICFGR0 ? 0 : Gicv3::SGI_MAX;

          for (int i = 0, int_id = first_int_id; i < 32;
               i = i + 2, int_id++) {
              if (!distributor->DS && !is_secure_access) {
                  // RAZ/WI for non-secure accesses for secure interrupts
                  if (getIntGroup(int_id) != Gicv3::G1NS) {
                      continue;
                  }
              }

              if (irqConfig[int_id] == Gicv3::INT_EDGE_TRIGGERED) {
                  value |= (0x2) << i;
              }
          }

          return value;
      }

      case GICR_IGRPMODR0: { // Interrupt Group Modifier Register 0
          uint64_t value = 0;

          if (distributor->DS) {
              value = 0;
          } else {
              if (!is_secure_access) {
                  // RAZ/WI for non-secure accesses
                  value = 0;
              } else {
                  for (int int_id = 0; int_id < 8 * size; int_id++) {
                      value |= irqGrpmod[int_id] << int_id;
                  }
              }
          }

          return value;
      }

      case GICR_NSACR: { // Non-secure Access Control Register
          uint64_t value = 0;

          if (distributor->DS) {
              // RAZ/WI
              value = 0;
          } else {
              if (!is_secure_access) {
                  // RAZ/WI
                  value = 0;
              } else {
                  for (int i = 0, int_id = 0; i < 8 * size;
                       i = i + 2, int_id++) {
                      value |= irqNsacr[int_id] << i;
                  }
              }
          }

          return value;
      }

      case GICR_PROPBASER: // Redistributor Properties Base Address Register
        // OuterCache, bits [58:56]
        //   000 Memory type defined in InnerCache field
        // Physical_Address, bits [51:12]
        //   Bits [51:12] of the physical address containing the LPI
        //   Configuration table
        // Shareability, bits [11:10]
        //   00 Non-shareable
        // InnerCache, bits [9:7]
        //   000 Device-nGnRnE
        // IDbits, bits [4:0]
        //   limited by GICD_TYPER.IDbits
        return lpiConfigurationTablePtr | lpiIDBits;

      // Redistributor LPI Pending Table Base Address Register
      case GICR_PENDBASER:
        // PTZ, bit [62]
        //   Pending Table Zero
        // OuterCache, bits [58:56]
        //   000 Memory type defined in InnerCache field
        // Physical_Address, bits [51:16]
        //   Bits [51:16] of the physical address containing the LPI Pending
        //   table
        // Shareability, bits [11:10]
        //   00 Non-shareable
        // InnerCache, bits [9:7]
        //   000 Device-nGnRnE
        return lpiPendingTablePtr;

      // Redistributor Synchronize Register
      case GICR_SYNCR:
        return 0;

      default:
        panic("Gicv3Redistributor::read(): invalid offset %#x\n", addr);
        break;
    }
}

void
Gicv3Redistributor::write(Addr addr, uint64_t data, size_t size,
                          bool is_secure_access)
{
    if (GICR_IPRIORITYR.contains(addr)) { // Interrupt Priority Registers
        int first_intid = addr - GICR_IPRIORITYR.start();

        for (int i = 0, int_id = first_intid; i < size; i++, int_id++) {
            uint8_t prio = bits(data, (i + 1) * 8 - 1, (i * 8));

            if (!distributor->DS && !is_secure_access) {
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    // RAZ/WI for non-secure accesses for secure interrupts
                    continue;
                } else {
                    // NS view
                    prio = 0x80 | (prio >> 1);
                }
            }

            irqPriority[int_id] = prio;
            DPRINTF(GIC, "Gicv3Redistributor::write(): "
                    "int_id %d priority %d\n", int_id, irqPriority[int_id]);
        }

        return;
    }

    switch (addr) {
      case GICR_CTLR: {
          // GICR_TYPER.LPIS is 0 so EnableLPIs is RES0
          EnableLPIs = data & GICR_CTLR_ENABLE_LPIS;
          DPG1S = data & GICR_CTLR_DPG1S;
          DPG1NS = data & GICR_CTLR_DPG1NS;
          DPG0 = data & GICR_CTLR_DPG0;
          break;
      }

      case GICR_WAKER: // Wake Register
        if (!distributor->DS && !is_secure_access) {
            // RAZ/WI for non-secure accesses
            return;
        }

        if (not peInLowPowerState and
            (data & GICR_WAKER_ProcessorSleep)) {
            DPRINTF(GIC, "Gicv3Redistributor::write(): "
                    "PE entering in low power state\n");
        } else if (peInLowPowerState and
                   not(data & GICR_WAKER_ProcessorSleep)) {
            DPRINTF(GIC, "Gicv3Redistributor::write(): powering up PE\n");
        }

        peInLowPowerState = data & GICR_WAKER_ProcessorSleep;
        break;

      case GICR_IGROUPR0: // Interrupt Group Register 0
        if (!distributor->DS && !is_secure_access) {
            // RAZ/WI for non-secure accesses
            return;
        }

        for (int int_id = 0; int_id < 8 * size; int_id++) {
            irqGroup[int_id] = data & (1 << int_id) ? 1 : 0;
            DPRINTF(GIC, "Gicv3Redistributor::write(): "
                    "int_id %d group %d\n", int_id, irqGroup[int_id]);
        }

        break;

      case GICR_ISENABLER0: // Interrupt Set-Enable Register 0
        for (int int_id = 0; int_id < 8 * size; int_id++) {
            if (!distributor->DS && !is_secure_access) {
                // RAZ/WI for non-secure accesses for secure interrupts
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    continue;
                }
            }

            bool enable = data & (1 << int_id) ? 1 : 0;

            if (enable) {
                irqEnabled[int_id] = true;
            }

            DPRINTF(GIC, "Gicv3Redistributor::write(): "
                    "int_id %d enable %i\n", int_id, irqEnabled[int_id]);
        }

        break;

      case GICR_ICENABLER0: // Interrupt Clear-Enable Register 0
        for (int int_id = 0; int_id < 8 * size; int_id++) {
            if (!distributor->DS && !is_secure_access) {
                // RAZ/WI for non-secure accesses for secure interrupts
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    continue;
                }
            }

            bool disable = data & (1 << int_id) ? 1 : 0;

            if (disable) {
                irqEnabled[int_id] = false;
            }

            DPRINTF(GIC, "Gicv3Redistributor::write(): "
                    "int_id %d enable %i\n", int_id, irqEnabled[int_id]);
        }

        break;

      case GICR_ISPENDR0: // Interrupt Set-Pending Register 0
        for (int int_id = 0; int_id < 8 * size; int_id++) {
            if (!distributor->DS && !is_secure_access) {
                // RAZ/WI for non-secure accesses for secure interrupts
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    continue;
                }
            }

            bool pending = data & (1 << int_id) ? 1 : 0;

            if (pending) {
                DPRINTF(GIC, "Gicv3Redistributor::write() "
                        "(GICR_ISPENDR0): int_id %d (PPI) "
                        "pending bit set\n", int_id);
                irqPending[int_id] = true;
            }
        }

        updateDistributor();
        break;

      case GICR_ICPENDR0:// Interrupt Clear-Pending Register 0
        for (int int_id = 0; int_id < 8 * size; int_id++) {
            if (!distributor->DS && !is_secure_access) {
                // RAZ/WI for non-secure accesses for secure interrupts
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    continue;
                }
            }

            bool clear = data & (1 << int_id) ? 1 : 0;

            if (clear) {
                irqPending[int_id] = false;
            }
        }

        break;

      case GICR_ISACTIVER0: // Interrupt Set-Active Register 0
        for (int int_id = 0; int_id < 8 * size; int_id++) {
            if (!distributor->DS && !is_secure_access) {
                // RAZ/WI for non-secure accesses for secure interrupts
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    continue;
                }
            }

            bool activate = data & (1 << int_id) ? 1 : 0;

            if (activate) {
                if (!irqActive[int_id]) {
                    DPRINTF(GIC, "Gicv3Redistributor::write(): "
                            "int_id %d active set\n", int_id);
                }

                irqActive[int_id] = true;
            }
        }

        break;

      case GICR_ICACTIVER0: // Interrupt Clear-Active Register 0
        for (int int_id = 0; int_id < 8 * size; int_id++) {
            if (!distributor->DS && !is_secure_access) {
                // RAZ/WI for non-secure accesses for secure interrupts
                if (getIntGroup(int_id) != Gicv3::G1NS) {
                    continue;
                }
            }

            bool clear = data & (1 << int_id) ? 1 : 0;

            if (clear) {
                if (irqActive[int_id]) {
                    DPRINTF(GIC, "Gicv3Redistributor::write(): "
                            "int_id %d active cleared\n", int_id);
                }

                irqActive[int_id] = false;
            }
        }

        break;

      case GICR_ICFGR1: { // PPI Configuration Register
          int first_intid = Gicv3::SGI_MAX;

          for (int i = 0, int_id = first_intid; i < 8 * size;
               i = i + 2, int_id++) {
              if (!distributor->DS && !is_secure_access) {
                  // RAZ/WI for non-secure accesses for secure interrupts
                  if (getIntGroup(int_id) != Gicv3::G1NS) {
                      continue;
                  }
              }

              irqConfig[int_id] = data & (0x2 << i) ?
                                  Gicv3::INT_EDGE_TRIGGERED :
                                  Gicv3::INT_LEVEL_SENSITIVE;
              DPRINTF(GIC, "Gicv3Redistributor::write(): "
                      "int_id %d (PPI) config %d\n",
                      int_id, irqConfig[int_id]);
          }

          break;
      }

      case GICR_IGRPMODR0: { // Interrupt Group Modifier Register 0
          if (distributor->DS) {
              // RAZ/WI if secutiry disabled
          } else {
              for (int int_id = 0; int_id < 8 * size; int_id++) {
                  if (!is_secure_access) {
                      // RAZ/WI for non-secure accesses
                      continue;
                  }

                  irqGrpmod[int_id] = data & (1 << int_id);
              }
          }

          break;
      }

      case GICR_NSACR: { // Non-secure Access Control Register
          if (distributor->DS) {
              // RAZ/WI
          } else {
              if (!is_secure_access) {
                  // RAZ/WI
              } else {
                  for (int i = 0, int_id = 0; i < 8 * size;
                       i = i + 2, int_id++) {
                      irqNsacr[int_id] = (data >> i) & 0x3;
                  }
              }
          }

          break;
      }

      case GICR_SETLPIR: // Set LPI Pending Register
        setClrLPI(data, true);
        break;

      case GICR_CLRLPIR: // Clear LPI Pending Register
        setClrLPI(data, false);
        break;

      case GICR_PROPBASER: { // Redistributor Properties Base Address Register
          // OuterCache, bits [58:56]
          //   000 Memory type defined in InnerCache field
          // Physical_Address, bits [51:12]
          //   Bits [51:12] of the physical address containing the LPI
          //   Configuration table
          // Shareability, bits [11:10]
          //   00 Non-shareable
          // InnerCache, bits [9:7]
          //   000 Device-nGnRnE
          // IDbits, bits [4:0]
          //   limited by GICD_TYPER.IDbits (= 0xf)
          lpiConfigurationTablePtr = data & 0xFFFFFFFFFF000;
          lpiIDBits = data & 0x1f;

          // 0xf here matches the value of GICD_TYPER.IDbits.
          // TODO - make GICD_TYPER.IDbits a parameter instead of a hardcoded
          // value
          if (lpiIDBits > 0xf) {
              lpiIDBits = 0xf;
          }

          break;
      }

      // Redistributor LPI Pending Table Base Address Register
      case GICR_PENDBASER:
        // PTZ, bit [62]
        //   Pending Table Zero
        // OuterCache, bits [58:56]
        //   000 Memory type defined in InnerCache field
        // Physical_Address, bits [51:16]
        //   Bits [51:16] of the physical address containing the LPI Pending
        //   table
        // Shareability, bits [11:10]
        //   00 Non-shareable
        // InnerCache, bits [9:7]
        //   000 Device-nGnRnE
        lpiPendingTablePtr = data & 0xFFFFFFFFF0000;
        break;

      case GICR_INVLPIR: { // Redistributor Invalidate LPI Register
          // Do nothing: no caching supported
          break;
      }

      case GICR_INVALLR: { // Redistributor Invalidate All Register
          // Do nothing: no caching supported
          break;
      }

      default:
        panic("Gicv3Redistributor::write(): invalid offset %#x\n", addr);
        break;
    }
}

void
Gicv3Redistributor::sendPPInt(uint32_t int_id)
{
    assert((int_id >= Gicv3::SGI_MAX) &&
           (int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX));
    irqPending[int_id] = true;
    DPRINTF(GIC, "Gicv3Redistributor::sendPPInt(): "
            "int_id %d (PPI) pending bit set\n", int_id);
    updateDistributor();
}

void
Gicv3Redistributor::sendSGI(uint32_t int_id, Gicv3::GroupId group, bool ns)
{
    assert(int_id < Gicv3::SGI_MAX);
    Gicv3::GroupId int_group = getIntGroup(int_id);

    bool forward = false;

    if (ns) {
        // Non-Secure EL1 and EL2 access
        int nsaccess = irqNsacr[int_id];
        if (int_group == Gicv3::G0S) {

            forward = distributor->DS || (nsaccess >= 1);

        } else if (int_group == Gicv3::G1S) {
            forward = ((group == Gicv3::G1S || group == Gicv3::G1NS ) &&
                      nsaccess == 2);
        } else {
            // G1NS
            forward = group == Gicv3::G1NS;
        }
    } else {
        // Secure EL1 and EL3 access
        forward = (group == int_group) ||
            (group == Gicv3::G1S && int_group == Gicv3::G0S &&
            distributor->DS);
    }

    if (!forward) return;

    irqPending[int_id] = true;
    DPRINTF(GIC, "Gicv3ReDistributor::sendSGI(): "
            "int_id %d (SGI) pending bit set\n", int_id);
    updateDistributor();
}

Gicv3::IntStatus
Gicv3Redistributor::intStatus(uint32_t int_id) const
{
    assert(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX);

    if (irqPending[int_id]) {
        if (irqActive[int_id]) {
            return Gicv3::INT_ACTIVE_PENDING;
        }

        return Gicv3::INT_PENDING;
    } else if (irqActive[int_id]) {
        return Gicv3::INT_ACTIVE;
    } else {
        return Gicv3::INT_INACTIVE;
    }
}

void
Gicv3Redistributor::updateDistributor()
{
    distributor->update();
}

/*
 * Recalculate the highest priority pending interrupt after a
 * change to redistributor state.
 */
void
Gicv3Redistributor::update()
{
    for (int int_id = 0; int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX; int_id++) {
        Gicv3::GroupId int_group = getIntGroup(int_id);
        bool group_enabled = distributor->groupEnabled(int_group);

        if (irqPending[int_id] && irqEnabled[int_id] &&
                !irqActive[int_id] && group_enabled) {
            if ((irqPriority[int_id] < cpuInterface->hppi.prio) ||
                /*
                 * Multiple pending ints with same priority.
                 * Implementation choice which one to signal.
                 * Our implementation selects the one with the lower id.
                 */
                (irqPriority[int_id] == cpuInterface->hppi.prio &&
                 int_id < cpuInterface->hppi.intid)) {
                cpuInterface->hppi.intid = int_id;
                cpuInterface->hppi.prio = irqPriority[int_id];
                cpuInterface->hppi.group = int_group;
            }
        }
    }

    // Check LPIs
    if (EnableLPIs) {

        const uint32_t largest_lpi_id = 1 << (lpiIDBits + 1);
        const uint32_t number_lpis = largest_lpi_id - SMALLEST_LPI_ID + 1;

        uint8_t lpi_pending_table[largest_lpi_id / 8];
        uint8_t lpi_config_table[number_lpis];

        memProxy->readBlob(lpiPendingTablePtr,
                           lpi_pending_table,
                           sizeof(lpi_pending_table));

        memProxy->readBlob(lpiConfigurationTablePtr,
                           lpi_config_table,
                           sizeof(lpi_config_table));

        for (int lpi_id = SMALLEST_LPI_ID; lpi_id < largest_lpi_id;
             lpi_id++) {
            uint32_t lpi_pending_entry_byte = lpi_id / 8;
            uint8_t lpi_pending_entry_bit_position = lpi_id % 8;
            bool lpi_is_pending = lpi_pending_table[lpi_pending_entry_byte] &
                                  1 << lpi_pending_entry_bit_position;
            uint32_t lpi_configuration_entry_index = lpi_id - SMALLEST_LPI_ID;

            LPIConfigurationTableEntry config_entry =
                lpi_config_table[lpi_configuration_entry_index];

            bool lpi_is_enable = config_entry.enable;

            // LPIs are always Non-secure Group 1 interrupts,
            // in a system where two Security states are enabled.
            Gicv3::GroupId lpi_group = Gicv3::G1NS;
            bool group_enabled = distributor->groupEnabled(lpi_group);

            if (lpi_is_pending && lpi_is_enable && group_enabled) {
                uint8_t lpi_priority = config_entry.priority << 2;

                if ((lpi_priority < cpuInterface->hppi.prio) ||
                    (lpi_priority == cpuInterface->hppi.prio &&
                     lpi_id < cpuInterface->hppi.intid)) {
                    cpuInterface->hppi.intid = lpi_id;
                    cpuInterface->hppi.prio = lpi_priority;
                    cpuInterface->hppi.group = lpi_group;
                }
            }
        }
    }

    cpuInterface->update();
}

uint8_t
Gicv3Redistributor::readEntryLPI(uint32_t lpi_id)
{
    Addr lpi_pending_entry_ptr = lpiPendingTablePtr + (lpi_id / 8);

    uint8_t lpi_pending_entry;
    memProxy->readBlob(lpi_pending_entry_ptr,
                       &lpi_pending_entry,
                       sizeof(lpi_pending_entry));

    return lpi_pending_entry;
}

void
Gicv3Redistributor::writeEntryLPI(uint32_t lpi_id, uint8_t lpi_pending_entry)
{
    Addr lpi_pending_entry_ptr = lpiPendingTablePtr + (lpi_id / 8);

    memProxy->writeBlob(lpi_pending_entry_ptr,
                        &lpi_pending_entry,
                        sizeof(lpi_pending_entry));
}

bool
Gicv3Redistributor::isPendingLPI(uint32_t lpi_id)
{
    // Fetch the LPI pending entry from memory
    uint8_t lpi_pending_entry = readEntryLPI(lpi_id);

    uint8_t lpi_pending_entry_bit_position = lpi_id % 8;
    bool is_set = lpi_pending_entry & (1 << lpi_pending_entry_bit_position);

    return is_set;
}

void
Gicv3Redistributor::setClrLPI(uint64_t data, bool set)
{
    if (!EnableLPIs) {
        // Writes to GICR_SETLPIR or GICR_CLRLPIR have not effect if
        // GICR_CTLR.EnableLPIs == 0.
        return;
    }

    uint32_t lpi_id = data & 0xffffffff;
    uint32_t largest_lpi_id = 1 << (lpiIDBits + 1);

    if (lpi_id > largest_lpi_id) {
        // Writes to GICR_SETLPIR or GICR_CLRLPIR have not effect if
        // pINTID value specifies an unimplemented LPI.
        return;
    }

    // Fetch the LPI pending entry from memory
    uint8_t lpi_pending_entry = readEntryLPI(lpi_id);

    uint8_t lpi_pending_entry_bit_position = lpi_id % 8;
    bool is_set = lpi_pending_entry & (1 << lpi_pending_entry_bit_position);

    if (set) {
        if (is_set) {
            // Writes to GICR_SETLPIR have not effect if the pINTID field
            // corresponds to an LPI that is already pending.
            return;
        }

        lpi_pending_entry |= 1 << (lpi_pending_entry_bit_position);
    } else {
        if (!is_set) {
            // Writes to GICR_SETLPIR have not effect if the pINTID field
            // corresponds to an LPI that is not pending.
            return;
        }

        lpi_pending_entry &= ~(1 << (lpi_pending_entry_bit_position));
    }

    writeEntryLPI(lpi_id, lpi_pending_entry);

    updateDistributor();
}

Gicv3::GroupId
Gicv3Redistributor::getIntGroup(int int_id) const
{
    assert(int_id < (Gicv3::SGI_MAX + Gicv3::PPI_MAX));

    if (distributor->DS) {
        if (irqGroup[int_id] == 0) {
            return Gicv3::G0S;
        } else {
            return Gicv3::G1NS;
        }
    } else {
        if (irqGrpmod[int_id] == 0 && irqGroup[int_id] == 0) {
            return Gicv3::G0S;
        } else if (irqGrpmod[int_id] == 0 && irqGroup[int_id] == 1) {
            return Gicv3::G1NS;
        } else if (irqGrpmod[int_id] == 1 && irqGroup[int_id] == 0) {
            return Gicv3::G1S;
        } else if (irqGrpmod[int_id] == 1 && irqGroup[int_id] == 1) {
            return Gicv3::G1NS;
        }
    }

    M5_UNREACHABLE;
}

void
Gicv3Redistributor::activateIRQ(uint32_t int_id)
{
    irqPending[int_id] = false;
    irqActive[int_id] = true;
}

void
Gicv3Redistributor::deactivateIRQ(uint32_t int_id)
{
    irqActive[int_id] = false;
}

uint32_t
Gicv3Redistributor::getAffinity() const
{
    ThreadContext * tc = gic->getSystem()->getThreadContext(cpuId);
    uint64_t mpidr = getMPIDR(gic->getSystem(), tc);
    /*
     * Aff3 = MPIDR[39:32]
     * (Note getMPIDR() returns uint32_t so Aff3 is always 0...)
     * Aff2 = MPIDR[23:16]
     * Aff1 = MPIDR[15:8]
     * Aff0 = MPIDR[7:0]
     * affinity = Aff3.Aff2.Aff1.Aff0
     */
    uint64_t affinity = ((mpidr & 0xff00000000) >> 8) | (mpidr & (0xffffff));
    return affinity;
}

bool
Gicv3Redistributor::canBeSelectedFor1toNInterrupt(Gicv3::GroupId group) const
{
    if (peInLowPowerState) {
        return false;
    }

    if (!distributor->groupEnabled(group)) {
        return false;
    }

    if ((group == Gicv3::G1S) && DPG1S) {
        return false;
    }

    if ((group == Gicv3::G1NS) && DPG1NS) {
        return false;
    }

    if ((group == Gicv3::G0S) && DPG0) {
        return false;
    }

    return true;
}

void
Gicv3Redistributor::serialize(CheckpointOut & cp) const
{
    SERIALIZE_SCALAR(peInLowPowerState);
    SERIALIZE_CONTAINER(irqGroup);
    SERIALIZE_CONTAINER(irqEnabled);
    SERIALIZE_CONTAINER(irqPending);
    SERIALIZE_CONTAINER(irqActive);
    SERIALIZE_CONTAINER(irqPriority);
    SERIALIZE_CONTAINER(irqConfig);
    SERIALIZE_CONTAINER(irqGrpmod);
    SERIALIZE_CONTAINER(irqNsacr);
    SERIALIZE_SCALAR(DPG1S);
    SERIALIZE_SCALAR(DPG1NS);
    SERIALIZE_SCALAR(DPG0);
    SERIALIZE_SCALAR(EnableLPIs);
    SERIALIZE_SCALAR(lpiConfigurationTablePtr);
    SERIALIZE_SCALAR(lpiIDBits);
    SERIALIZE_SCALAR(lpiPendingTablePtr);
}

void
Gicv3Redistributor::unserialize(CheckpointIn & cp)
{
    UNSERIALIZE_SCALAR(peInLowPowerState);
    UNSERIALIZE_CONTAINER(irqGroup);
    UNSERIALIZE_CONTAINER(irqEnabled);
    UNSERIALIZE_CONTAINER(irqPending);
    UNSERIALIZE_CONTAINER(irqActive);
    UNSERIALIZE_CONTAINER(irqPriority);
    UNSERIALIZE_CONTAINER(irqConfig);
    UNSERIALIZE_CONTAINER(irqGrpmod);
    UNSERIALIZE_CONTAINER(irqNsacr);
    UNSERIALIZE_SCALAR(DPG1S);
    UNSERIALIZE_SCALAR(DPG1NS);
    UNSERIALIZE_SCALAR(DPG0);
    UNSERIALIZE_SCALAR(EnableLPIs);
    UNSERIALIZE_SCALAR(lpiConfigurationTablePtr);
    UNSERIALIZE_SCALAR(lpiIDBits);
    UNSERIALIZE_SCALAR(lpiPendingTablePtr);
}