gic_v3_distributor.cc revision 14167
113531Sjairo.balart@metempsy.com/*
214167Sgiacomo.travaglini@arm.com * Copyright (c) 2019 ARM Limited
314167Sgiacomo.travaglini@arm.com * All rights reserved
414167Sgiacomo.travaglini@arm.com *
514167Sgiacomo.travaglini@arm.com * The license below extends only to copyright in the software and shall
614167Sgiacomo.travaglini@arm.com * not be construed as granting a license to any other intellectual
714167Sgiacomo.travaglini@arm.com * property including but not limited to intellectual property relating
814167Sgiacomo.travaglini@arm.com * to a hardware implementation of the functionality of the software
914167Sgiacomo.travaglini@arm.com * licensed hereunder.  You may use the software subject to the license
1014167Sgiacomo.travaglini@arm.com * terms below provided that you ensure that this notice is replicated
1114167Sgiacomo.travaglini@arm.com * unmodified and in its entirety in all distributions of the software,
1214167Sgiacomo.travaglini@arm.com * modified or unmodified, in source code or in binary form.
1314167Sgiacomo.travaglini@arm.com *
1413531Sjairo.balart@metempsy.com * Copyright (c) 2018 Metempsy Technology Consulting
1513531Sjairo.balart@metempsy.com * All rights reserved.
1613531Sjairo.balart@metempsy.com *
1713531Sjairo.balart@metempsy.com * Redistribution and use in source and binary forms, with or without
1813531Sjairo.balart@metempsy.com * modification, are permitted provided that the following conditions are
1913531Sjairo.balart@metempsy.com * met: redistributions of source code must retain the above copyright
2013531Sjairo.balart@metempsy.com * notice, this list of conditions and the following disclaimer;
2113531Sjairo.balart@metempsy.com * redistributions in binary form must reproduce the above copyright
2213531Sjairo.balart@metempsy.com * notice, this list of conditions and the following disclaimer in the
2313531Sjairo.balart@metempsy.com * documentation and/or other materials provided with the distribution;
2413531Sjairo.balart@metempsy.com * neither the name of the copyright holders nor the names of its
2513531Sjairo.balart@metempsy.com * contributors may be used to endorse or promote products derived from
2613531Sjairo.balart@metempsy.com * this software without specific prior written permission.
2713531Sjairo.balart@metempsy.com *
2813531Sjairo.balart@metempsy.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2913531Sjairo.balart@metempsy.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3013531Sjairo.balart@metempsy.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3113531Sjairo.balart@metempsy.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3213531Sjairo.balart@metempsy.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3313531Sjairo.balart@metempsy.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3413531Sjairo.balart@metempsy.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3513531Sjairo.balart@metempsy.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3613531Sjairo.balart@metempsy.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3713531Sjairo.balart@metempsy.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3813531Sjairo.balart@metempsy.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3913531Sjairo.balart@metempsy.com *
4013531Sjairo.balart@metempsy.com * Authors: Jairo Balart
4113531Sjairo.balart@metempsy.com */
4213756Sjairo.balart@metempsy.com
4313531Sjairo.balart@metempsy.com#include "dev/arm/gic_v3_distributor.hh"
4413531Sjairo.balart@metempsy.com
4513531Sjairo.balart@metempsy.com#include <algorithm>
4613531Sjairo.balart@metempsy.com
4713531Sjairo.balart@metempsy.com#include "debug/GIC.hh"
4813531Sjairo.balart@metempsy.com#include "dev/arm/gic_v3.hh"
4913531Sjairo.balart@metempsy.com#include "dev/arm/gic_v3_cpu_interface.hh"
5013531Sjairo.balart@metempsy.com#include "dev/arm/gic_v3_redistributor.hh"
5113531Sjairo.balart@metempsy.com
5213756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_IGROUPR   (0x0080, 0x00ff);
5313756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ISENABLER (0x0100, 0x017f);
5413756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ICENABLER (0x0180, 0x01ff);
5513756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ISPENDR   (0x0200, 0x027f);
5613756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ICPENDR   (0x0280, 0x02ff);
5713756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ISACTIVER (0x0300, 0x037f);
5813756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ICACTIVER (0x0380, 0x03ff);
5913531Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_IPRIORITYR(0x0400, 0x07ff);
6013756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ITARGETSR (0x0800, 0x08ff);
6113756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_ICFGR     (0x0c00, 0x0cff);
6213756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_IGRPMODR  (0x0d00, 0x0d7f);
6313756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_NSACR     (0x0e00, 0x0eff);
6413756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_CPENDSGIR (0x0f10, 0x0f1f);
6513756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_SPENDSGIR (0x0f20, 0x0f2f);
6613756Sjairo.balart@metempsy.comconst AddrRange Gicv3Distributor::GICD_IROUTER   (0x6000, 0x7fe0);
6713531Sjairo.balart@metempsy.com
6813531Sjairo.balart@metempsy.comGicv3Distributor::Gicv3Distributor(Gicv3 * gic, uint32_t it_lines)
6913531Sjairo.balart@metempsy.com    : gic(gic),
7013531Sjairo.balart@metempsy.com      itLines(it_lines),
7113531Sjairo.balart@metempsy.com      irqGroup(it_lines),
7213531Sjairo.balart@metempsy.com      irqEnabled(it_lines),
7313531Sjairo.balart@metempsy.com      irqPending(it_lines),
7413531Sjairo.balart@metempsy.com      irqActive(it_lines),
7513531Sjairo.balart@metempsy.com      irqPriority(it_lines),
7613531Sjairo.balart@metempsy.com      irqConfig(it_lines),
7713531Sjairo.balart@metempsy.com      irqGrpmod(it_lines),
7813531Sjairo.balart@metempsy.com      irqNsacr(it_lines),
7914167Sgiacomo.travaglini@arm.com      irqAffinityRouting(it_lines),
8014167Sgiacomo.travaglini@arm.com      gicdPidr0(0x92),
8114167Sgiacomo.travaglini@arm.com      gicdPidr1(0xb4),
8214167Sgiacomo.travaglini@arm.com      gicdPidr2(0x3b),
8314167Sgiacomo.travaglini@arm.com      gicdPidr3(0),
8414167Sgiacomo.travaglini@arm.com      gicdPidr4(0x44)
8513531Sjairo.balart@metempsy.com{
8613531Sjairo.balart@metempsy.com    panic_if(it_lines > Gicv3::INTID_SECURE, "Invalid value for it_lines!");
8713531Sjairo.balart@metempsy.com}
8813531Sjairo.balart@metempsy.com
8913531Sjairo.balart@metempsy.comvoid
9013531Sjairo.balart@metempsy.comGicv3Distributor::init()
9113531Sjairo.balart@metempsy.com{
9213531Sjairo.balart@metempsy.com}
9313531Sjairo.balart@metempsy.com
9413531Sjairo.balart@metempsy.comvoid
9513531Sjairo.balart@metempsy.comGicv3Distributor::initState()
9613531Sjairo.balart@metempsy.com{
9713531Sjairo.balart@metempsy.com    reset();
9813531Sjairo.balart@metempsy.com}
9913531Sjairo.balart@metempsy.com
10013531Sjairo.balart@metempsy.comvoid
10113531Sjairo.balart@metempsy.comGicv3Distributor::reset()
10213531Sjairo.balart@metempsy.com{
10313531Sjairo.balart@metempsy.com    std::fill(irqGroup.begin(), irqGroup.end(), 0);
10413531Sjairo.balart@metempsy.com    // Imp. defined reset value
10513531Sjairo.balart@metempsy.com    std::fill(irqEnabled.begin(), irqEnabled.end(), false);
10613531Sjairo.balart@metempsy.com    std::fill(irqPending.begin(), irqPending.end(), false);
10713531Sjairo.balart@metempsy.com    std::fill(irqActive.begin(), irqActive.end(), false);
10813531Sjairo.balart@metempsy.com    // Imp. defined reset value
10913531Sjairo.balart@metempsy.com    std::fill(irqPriority.begin(), irqPriority.end(), 0xAAAAAAAA);
11013531Sjairo.balart@metempsy.com    std::fill(irqConfig.begin(), irqConfig.end(),
11113531Sjairo.balart@metempsy.com              Gicv3::INT_LEVEL_SENSITIVE); // Imp. defined reset value
11213531Sjairo.balart@metempsy.com    std::fill(irqGrpmod.begin(), irqGrpmod.end(), 0);
11313531Sjairo.balart@metempsy.com    std::fill(irqNsacr.begin(), irqNsacr.end(), 0);
11413531Sjairo.balart@metempsy.com    /*
11513531Sjairo.balart@metempsy.com     * For our implementation affinity routing is always enabled,
11613531Sjairo.balart@metempsy.com     * no GICv2 legacy
11713531Sjairo.balart@metempsy.com     */
11813531Sjairo.balart@metempsy.com    ARE = true;
11913531Sjairo.balart@metempsy.com
12013531Sjairo.balart@metempsy.com    if (gic->getSystem()->haveSecurity()) {
12113531Sjairo.balart@metempsy.com        DS = false;
12213531Sjairo.balart@metempsy.com    } else {
12313531Sjairo.balart@metempsy.com        DS = true;
12413531Sjairo.balart@metempsy.com    }
12513531Sjairo.balart@metempsy.com
12613531Sjairo.balart@metempsy.com    EnableGrp0 = 0;
12713531Sjairo.balart@metempsy.com    EnableGrp1NS = 0;
12813531Sjairo.balart@metempsy.com    EnableGrp1S = 0;
12913531Sjairo.balart@metempsy.com}
13013531Sjairo.balart@metempsy.com
13113531Sjairo.balart@metempsy.comuint64_t
13213531Sjairo.balart@metempsy.comGicv3Distributor::read(Addr addr, size_t size, bool is_secure_access)
13313531Sjairo.balart@metempsy.com{
13413531Sjairo.balart@metempsy.com    if (GICD_IGROUPR.contains(addr)) { // Interrupt Group Registers
13513531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
13613531Sjairo.balart@metempsy.com
13713531Sjairo.balart@metempsy.com        if (!DS && !is_secure_access) {
13813531Sjairo.balart@metempsy.com            // RAZ/WI for non-secure accesses
13913531Sjairo.balart@metempsy.com            return 0;
14013531Sjairo.balart@metempsy.com        }
14113531Sjairo.balart@metempsy.com
14213531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_IGROUPR.start()) * 8;
14313531Sjairo.balart@metempsy.com
14413531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
14513531Sjairo.balart@metempsy.com            return 0;
14613531Sjairo.balart@metempsy.com        }
14713531Sjairo.balart@metempsy.com
14813531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
14913756Sjairo.balart@metempsy.com             i++, int_id++) {
15013531Sjairo.balart@metempsy.com            val |= irqGroup[int_id] << i;
15113531Sjairo.balart@metempsy.com        }
15213531Sjairo.balart@metempsy.com
15313531Sjairo.balart@metempsy.com        return val;
15413756Sjairo.balart@metempsy.com    } else if (GICD_ISENABLER.contains(addr)) {
15513531Sjairo.balart@metempsy.com        // Interrupt Set-Enable Registers
15613531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
15713531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ISENABLER.start()) * 8;
15813531Sjairo.balart@metempsy.com
15913531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
16013531Sjairo.balart@metempsy.com            return 0;
16113531Sjairo.balart@metempsy.com        }
16213531Sjairo.balart@metempsy.com
16313531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
16413756Sjairo.balart@metempsy.com             i++, int_id++) {
16513756Sjairo.balart@metempsy.com
16613531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
16713531Sjairo.balart@metempsy.com            {
16813531Sjairo.balart@metempsy.com                continue;
16913531Sjairo.balart@metempsy.com            }
17013531Sjairo.balart@metempsy.com
17113531Sjairo.balart@metempsy.com            val |= irqEnabled[int_id] << i;
17213531Sjairo.balart@metempsy.com        }
17313531Sjairo.balart@metempsy.com
17413531Sjairo.balart@metempsy.com        return val;
17513531Sjairo.balart@metempsy.com    } else if (GICD_ICENABLER.contains(addr)) {
17613531Sjairo.balart@metempsy.com        // Interrupt Clear-Enable Registers
17713531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
17813531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICENABLER.start()) * 8;
17913531Sjairo.balart@metempsy.com
18013531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
18113531Sjairo.balart@metempsy.com            return 0;
18213531Sjairo.balart@metempsy.com        }
18313531Sjairo.balart@metempsy.com
18413531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
18513756Sjairo.balart@metempsy.com             i++, int_id++) {
18613756Sjairo.balart@metempsy.com
18713531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
18813531Sjairo.balart@metempsy.com            {
18913531Sjairo.balart@metempsy.com                continue;
19013531Sjairo.balart@metempsy.com            }
19113531Sjairo.balart@metempsy.com
19213531Sjairo.balart@metempsy.com            val |= (irqEnabled[int_id] << i);
19313531Sjairo.balart@metempsy.com        }
19413531Sjairo.balart@metempsy.com
19513531Sjairo.balart@metempsy.com        return val;
19613531Sjairo.balart@metempsy.com    } else if (GICD_ISPENDR.contains(addr)) {
19713756Sjairo.balart@metempsy.com        // Interrupt Set-Pending Registers
19813531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
19913531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ISPENDR.start()) * 8;
20013531Sjairo.balart@metempsy.com
20113531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
20213531Sjairo.balart@metempsy.com            return 0;
20313531Sjairo.balart@metempsy.com        }
20413531Sjairo.balart@metempsy.com
20513531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
20613756Sjairo.balart@metempsy.com             i++, int_id++) {
20713756Sjairo.balart@metempsy.com
20813531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
20913531Sjairo.balart@metempsy.com            {
21013531Sjairo.balart@metempsy.com                if (irqNsacr[int_id] == 0) {
21113531Sjairo.balart@metempsy.com                    // Group 0 or Secure Group 1 interrupts are RAZ/WI
21213531Sjairo.balart@metempsy.com                    continue;
21313531Sjairo.balart@metempsy.com                }
21413531Sjairo.balart@metempsy.com            }
21513531Sjairo.balart@metempsy.com
21613531Sjairo.balart@metempsy.com            val |= (irqPending[int_id] << i);
21713531Sjairo.balart@metempsy.com        }
21813531Sjairo.balart@metempsy.com
21913531Sjairo.balart@metempsy.com        return val;
22013531Sjairo.balart@metempsy.com    } else if (GICD_ICPENDR.contains(addr)) {
22113756Sjairo.balart@metempsy.com        // Interrupt Clear-Pending Registers
22213531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
22313531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICPENDR.start()) * 8;
22413531Sjairo.balart@metempsy.com
22513531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
22613531Sjairo.balart@metempsy.com            return 0;
22713531Sjairo.balart@metempsy.com        }
22813531Sjairo.balart@metempsy.com
22913531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
23013756Sjairo.balart@metempsy.com             i++, int_id++) {
23113756Sjairo.balart@metempsy.com
23213531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
23313531Sjairo.balart@metempsy.com            {
23413531Sjairo.balart@metempsy.com                if (irqNsacr[int_id] < 2) {
23513531Sjairo.balart@metempsy.com                    // Group 0 or Secure Group 1 interrupts are RAZ/WI
23613531Sjairo.balart@metempsy.com                    continue;
23713531Sjairo.balart@metempsy.com                }
23813531Sjairo.balart@metempsy.com            }
23913531Sjairo.balart@metempsy.com
24013531Sjairo.balart@metempsy.com            val |= (irqPending[int_id] << i);
24113531Sjairo.balart@metempsy.com        }
24213531Sjairo.balart@metempsy.com
24313531Sjairo.balart@metempsy.com        return val;
24413756Sjairo.balart@metempsy.com    } else if (GICD_ISACTIVER.contains(addr)) {
24513531Sjairo.balart@metempsy.com        // Interrupt Set-Active Registers
24613531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ISACTIVER.start()) * 8;
24713531Sjairo.balart@metempsy.com
24813531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
24913531Sjairo.balart@metempsy.com            return 0;
25013531Sjairo.balart@metempsy.com        }
25113531Sjairo.balart@metempsy.com
25213531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
25313531Sjairo.balart@metempsy.com
25413531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
25513756Sjairo.balart@metempsy.com             i++, int_id++) {
25613756Sjairo.balart@metempsy.com
25713531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
25813531Sjairo.balart@metempsy.com            {
25913531Sjairo.balart@metempsy.com                // Group 0 or Secure Group 1 interrupts are RAZ/WI
26013531Sjairo.balart@metempsy.com                if (irqNsacr[int_id] < 2) {
26113531Sjairo.balart@metempsy.com                    continue;
26213531Sjairo.balart@metempsy.com                }
26313531Sjairo.balart@metempsy.com            }
26413531Sjairo.balart@metempsy.com
26513531Sjairo.balart@metempsy.com            val |= (irqActive[int_id] << i);
26613531Sjairo.balart@metempsy.com        }
26713531Sjairo.balart@metempsy.com
26813531Sjairo.balart@metempsy.com        return val;
26913756Sjairo.balart@metempsy.com    } else if (GICD_ICACTIVER.contains(addr)) {
27013531Sjairo.balart@metempsy.com        // Interrupt Clear-Active Registers
27113531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICACTIVER.start()) * 8;
27213531Sjairo.balart@metempsy.com
27313531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
27413531Sjairo.balart@metempsy.com            return 0;
27513531Sjairo.balart@metempsy.com        }
27613531Sjairo.balart@metempsy.com
27713531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
27813531Sjairo.balart@metempsy.com
27913531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
28013756Sjairo.balart@metempsy.com             i++, int_id++) {
28113756Sjairo.balart@metempsy.com
28213531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
28313531Sjairo.balart@metempsy.com            {
28413531Sjairo.balart@metempsy.com                if (irqNsacr[int_id] < 2) {
28513531Sjairo.balart@metempsy.com                    continue;
28613531Sjairo.balart@metempsy.com                }
28713531Sjairo.balart@metempsy.com            }
28813531Sjairo.balart@metempsy.com
28913531Sjairo.balart@metempsy.com            val |= (irqActive[int_id] << i);
29013531Sjairo.balart@metempsy.com        }
29113531Sjairo.balart@metempsy.com
29213531Sjairo.balart@metempsy.com        return val;
29313756Sjairo.balart@metempsy.com    } else if (GICD_IPRIORITYR.contains(addr)) {
29413531Sjairo.balart@metempsy.com        // Interrupt Priority Registers
29513531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
29613531Sjairo.balart@metempsy.com        int first_intid = addr - GICD_IPRIORITYR.start();
29713531Sjairo.balart@metempsy.com
29813531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
29913531Sjairo.balart@metempsy.com            return 0;
30013531Sjairo.balart@metempsy.com        }
30113531Sjairo.balart@metempsy.com
30213531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < size && int_id < itLines;
30313756Sjairo.balart@metempsy.com             i++, int_id++) {
30413756Sjairo.balart@metempsy.com
30513531Sjairo.balart@metempsy.com            uint8_t prio = irqPriority[int_id];
30613531Sjairo.balart@metempsy.com
30713531Sjairo.balart@metempsy.com            if (!DS && !is_secure_access) {
30813531Sjairo.balart@metempsy.com                if (getIntGroup(int_id) != Gicv3::G1NS) {
30913531Sjairo.balart@metempsy.com                    // RAZ/WI for non-secure accesses for secure interrupts
31013531Sjairo.balart@metempsy.com                    continue;
31113531Sjairo.balart@metempsy.com                } else {
31213531Sjairo.balart@metempsy.com                    // NS view
31313531Sjairo.balart@metempsy.com                    prio = (prio << 1) & 0xff;
31413531Sjairo.balart@metempsy.com                }
31513531Sjairo.balart@metempsy.com            }
31613531Sjairo.balart@metempsy.com
31713531Sjairo.balart@metempsy.com            val |= prio << (i * 8);
31813531Sjairo.balart@metempsy.com        }
31913531Sjairo.balart@metempsy.com
32013531Sjairo.balart@metempsy.com        return val;
32113531Sjairo.balart@metempsy.com    } else if (GICD_ITARGETSR.contains(addr)) {
32213531Sjairo.balart@metempsy.com        // Interrupt Processor Targets Registers
32313531Sjairo.balart@metempsy.com        // ARE always on, RAZ/WI
32413531Sjairo.balart@metempsy.com        warn("Gicv3Distributor::read(): "
32513531Sjairo.balart@metempsy.com             "GICD_ITARGETSR is RAZ/WI, legacy not supported!\n");
32613531Sjairo.balart@metempsy.com        return 0;
32713756Sjairo.balart@metempsy.com    } else if (GICD_ICFGR.contains(addr)) {
32813531Sjairo.balart@metempsy.com        // Interrupt Configuration Registers
32913531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICFGR.start()) * 4;
33013531Sjairo.balart@metempsy.com
33113531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
33213531Sjairo.balart@metempsy.com            return 0;
33313531Sjairo.balart@metempsy.com        }
33413531Sjairo.balart@metempsy.com
33513531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
33613531Sjairo.balart@metempsy.com
33713531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
33813756Sjairo.balart@metempsy.com             i = i + 2, int_id++) {
33913756Sjairo.balart@metempsy.com
34013531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
34113531Sjairo.balart@metempsy.com            {
34213531Sjairo.balart@metempsy.com                continue;
34313531Sjairo.balart@metempsy.com            }
34413531Sjairo.balart@metempsy.com
34513531Sjairo.balart@metempsy.com            if (irqConfig[int_id] == Gicv3::INT_EDGE_TRIGGERED) {
34613531Sjairo.balart@metempsy.com                val |= (0x2 << i);
34713531Sjairo.balart@metempsy.com            }
34813531Sjairo.balart@metempsy.com        }
34913531Sjairo.balart@metempsy.com
35013531Sjairo.balart@metempsy.com        return val;
35113531Sjairo.balart@metempsy.com    } else if (GICD_IGRPMODR.contains(addr)) {
35213531Sjairo.balart@metempsy.com        // Interrupt Group Modifier Registers
35313531Sjairo.balart@metempsy.com        if (DS) {
35413531Sjairo.balart@metempsy.com            // RAZ/WI if security disabled
35513531Sjairo.balart@metempsy.com            return 0;
35613531Sjairo.balart@metempsy.com        } else {
35713531Sjairo.balart@metempsy.com            if (!is_secure_access) {
35813531Sjairo.balart@metempsy.com                // RAZ/WI for non-secure accesses
35913531Sjairo.balart@metempsy.com                return 0;
36013531Sjairo.balart@metempsy.com            } else {
36113531Sjairo.balart@metempsy.com                int first_intid = (addr - GICD_IGRPMODR.start()) * 8;
36213531Sjairo.balart@metempsy.com
36313531Sjairo.balart@metempsy.com                if (isNotSPI(first_intid)) {
36413531Sjairo.balart@metempsy.com                    return 0;
36513531Sjairo.balart@metempsy.com                }
36613531Sjairo.balart@metempsy.com
36713531Sjairo.balart@metempsy.com                uint64_t val = 0x0;
36813531Sjairo.balart@metempsy.com
36913531Sjairo.balart@metempsy.com                for (int i = 0, int_id = first_intid;
37013756Sjairo.balart@metempsy.com                     i < 8 * size && int_id < itLines; i++, int_id++) {
37113531Sjairo.balart@metempsy.com                    val |= irqGrpmod[int_id] << i;
37213531Sjairo.balart@metempsy.com                }
37313531Sjairo.balart@metempsy.com
37413531Sjairo.balart@metempsy.com                return val;
37513531Sjairo.balart@metempsy.com            }
37613531Sjairo.balart@metempsy.com        }
37713756Sjairo.balart@metempsy.com    } else if (GICD_NSACR.contains(addr)) {
37813531Sjairo.balart@metempsy.com        // Non-secure Access Control Registers
37913531Sjairo.balart@metempsy.com        // 2 bits per interrupt
38013531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_NSACR.start()) * 4;
38113531Sjairo.balart@metempsy.com
38213531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
38313531Sjairo.balart@metempsy.com            return 0;
38413531Sjairo.balart@metempsy.com        }
38513531Sjairo.balart@metempsy.com
38613531Sjairo.balart@metempsy.com        if (DS || (!DS && !is_secure_access)) {
38713531Sjairo.balart@metempsy.com            return 0;
38813531Sjairo.balart@metempsy.com        }
38913531Sjairo.balart@metempsy.com
39013531Sjairo.balart@metempsy.com        uint64_t val = 0x0;
39113531Sjairo.balart@metempsy.com
39213531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid;
39313756Sjairo.balart@metempsy.com             i < 8 * size && int_id < itLines; i = i + 2, int_id++) {
39413531Sjairo.balart@metempsy.com            val |= irqNsacr[int_id] << i;
39513531Sjairo.balart@metempsy.com        }
39613531Sjairo.balart@metempsy.com
39713531Sjairo.balart@metempsy.com        return val;
39813531Sjairo.balart@metempsy.com    } else if (GICD_CPENDSGIR.contains(addr)) { // SGI Clear-Pending Registers
39913531Sjairo.balart@metempsy.com        // ARE always on, RAZ/WI
40013531Sjairo.balart@metempsy.com        warn("Gicv3Distributor::read(): "
40113531Sjairo.balart@metempsy.com             "GICD_CPENDSGIR is RAZ/WI, legacy not supported!\n");
40213531Sjairo.balart@metempsy.com        return 0x0;
40313531Sjairo.balart@metempsy.com    } else if (GICD_SPENDSGIR.contains(addr)) { // SGI Set-Pending Registers
40413531Sjairo.balart@metempsy.com        // ARE always on, RAZ/WI
40513531Sjairo.balart@metempsy.com        warn("Gicv3Distributor::read(): "
40613531Sjairo.balart@metempsy.com             "GICD_SPENDSGIR is RAZ/WI, legacy not supported!\n");
40713531Sjairo.balart@metempsy.com        return 0x0;
40813531Sjairo.balart@metempsy.com    } else if (GICD_IROUTER.contains(addr)) { // Interrupt Routing Registers
40913531Sjairo.balart@metempsy.com        // 64 bit registers. 2 or 1 access.
41013531Sjairo.balart@metempsy.com        int int_id = (addr - GICD_IROUTER.start()) / 8;
41113531Sjairo.balart@metempsy.com
41213531Sjairo.balart@metempsy.com        if (isNotSPI(int_id)) {
41313531Sjairo.balart@metempsy.com            return 0;
41413531Sjairo.balart@metempsy.com        }
41513531Sjairo.balart@metempsy.com
41613531Sjairo.balart@metempsy.com        if (nsAccessToSecInt(int_id, is_secure_access))
41713531Sjairo.balart@metempsy.com        {
41813531Sjairo.balart@metempsy.com            if (irqNsacr[int_id] < 3) {
41913531Sjairo.balart@metempsy.com                return 0;
42013531Sjairo.balart@metempsy.com            }
42113531Sjairo.balart@metempsy.com        }
42213531Sjairo.balart@metempsy.com
42313531Sjairo.balart@metempsy.com        if (size == 4) {
42413531Sjairo.balart@metempsy.com            if (addr & 7) { // high half of 64 bit register
42513531Sjairo.balart@metempsy.com                return irqAffinityRouting[int_id] >> 32;
42613531Sjairo.balart@metempsy.com            } else { // high low of 64 bit register
42713531Sjairo.balart@metempsy.com                return irqAffinityRouting[int_id] & 0xFFFFFFFF;
42813531Sjairo.balart@metempsy.com            }
42913531Sjairo.balart@metempsy.com        } else {
43013531Sjairo.balart@metempsy.com            return irqAffinityRouting[int_id];
43113531Sjairo.balart@metempsy.com        }
43213531Sjairo.balart@metempsy.com    }
43313531Sjairo.balart@metempsy.com
43413531Sjairo.balart@metempsy.com    switch (addr) {
43513531Sjairo.balart@metempsy.com      case GICD_CTLR: // Control Register
43613531Sjairo.balart@metempsy.com        if (!DS) {
43713531Sjairo.balart@metempsy.com            if (is_secure_access) {
43813531Sjairo.balart@metempsy.com                // E1NWF [7] RAZ/WI
43913531Sjairo.balart@metempsy.com                // DS [6] - Disable Security
44013531Sjairo.balart@metempsy.com                // ARE_NS [5] RAO/WI
44113531Sjairo.balart@metempsy.com                // ARE_S [4] RAO/WI
44213531Sjairo.balart@metempsy.com                // EnableGrp1S [2]
44313531Sjairo.balart@metempsy.com                // EnableGrp1NS [1]
44413531Sjairo.balart@metempsy.com                // EnableGrp0 [0]
44513531Sjairo.balart@metempsy.com                return (EnableGrp0 << 0) |
44613531Sjairo.balart@metempsy.com                    (EnableGrp1NS << 1) |
44713531Sjairo.balart@metempsy.com                    (EnableGrp1S << 2) |
44813531Sjairo.balart@metempsy.com                    (1 << 4) |
44913531Sjairo.balart@metempsy.com                    (1 << 5) |
45013531Sjairo.balart@metempsy.com                    (DS << 6);
45113531Sjairo.balart@metempsy.com            } else {
45213531Sjairo.balart@metempsy.com                // ARE_NS [4] RAO/WI;
45313531Sjairo.balart@metempsy.com                // EnableGrp1A [1] is a read-write alias of the Secure
45413531Sjairo.balart@metempsy.com                // GICD_CTLR.EnableGrp1NS
45513531Sjairo.balart@metempsy.com                // EnableGrp1 [0] RES0
45613531Sjairo.balart@metempsy.com                return (1 << 4) | (EnableGrp1NS << 1);
45713531Sjairo.balart@metempsy.com            }
45813531Sjairo.balart@metempsy.com        } else {
45913531Sjairo.balart@metempsy.com            return (DS << 6) | (ARE << 4) |
46013531Sjairo.balart@metempsy.com                (EnableGrp1NS << 1) | (EnableGrp0 << 0);
46113531Sjairo.balart@metempsy.com        }
46213531Sjairo.balart@metempsy.com
46313531Sjairo.balart@metempsy.com      case GICD_TYPER: // Interrupt Controller Type Register
46413531Sjairo.balart@metempsy.com        /*
46513531Sjairo.balart@metempsy.com         * RSS           [26]    == 1
46613531Sjairo.balart@metempsy.com         * (The implementation does supports targeted SGIs with affinity
46713531Sjairo.balart@metempsy.com         * level 0 values of 0 - 255)
46813531Sjairo.balart@metempsy.com         * No1N          [25]    == 1
46913531Sjairo.balart@metempsy.com         * (1 of N SPI interrupts are not supported)
47013531Sjairo.balart@metempsy.com         * A3V           [24]    == 1
47113531Sjairo.balart@metempsy.com         * (Supports nonzero values of Affinity level 3)
47213531Sjairo.balart@metempsy.com         * IDbits        [23:19] == 0xf
47313531Sjairo.balart@metempsy.com         * (The number of interrupt identifier bits supported, minus one)
47413531Sjairo.balart@metempsy.com         * DVIS          [18]    == 0
47513531Sjairo.balart@metempsy.com         * (The implementation does not support Direct Virtual LPI
47613531Sjairo.balart@metempsy.com         * injection)
47713690Sjairo.balart@metempsy.com         * LPIS          [17]    == 1
47813531Sjairo.balart@metempsy.com         * (The implementation does not support LPIs)
47913531Sjairo.balart@metempsy.com         * MBIS          [16]    == 0
48013531Sjairo.balart@metempsy.com         * (The implementation does not support message-based interrupts
48113531Sjairo.balart@metempsy.com         * by writing to Distributor registers)
48213531Sjairo.balart@metempsy.com         * SecurityExtn  [10]    == X
48313531Sjairo.balart@metempsy.com         * (The GIC implementation supports two Security states)
48413531Sjairo.balart@metempsy.com         * CPUNumber     [7:5]   == 0
48513531Sjairo.balart@metempsy.com         * (since for us ARE is always 1 [(ARE = 0) == Gicv2 legacy])
48613531Sjairo.balart@metempsy.com         * ITLinesNumber [4:0]   == N
48713531Sjairo.balart@metempsy.com         * (MaxSPIIntId = 32 (N + 1) - 1)
48813531Sjairo.balart@metempsy.com         */
48913531Sjairo.balart@metempsy.com        {
49013531Sjairo.balart@metempsy.com            int max_spi_int_id = itLines - 1;
49113531Sjairo.balart@metempsy.com            int it_lines_number = ceil((max_spi_int_id + 1) / 32.0) - 1;
49213927Sgiacomo.travaglini@arm.com            return (1 << 26) | (1 << 25) | (1 << 24) | (IDBITS << 19) |
49313690Sjairo.balart@metempsy.com                (1 << 17) | (gic->getSystem()->haveSecurity() << 10) |
49413531Sjairo.balart@metempsy.com                (it_lines_number << 0);
49513531Sjairo.balart@metempsy.com        }
49613531Sjairo.balart@metempsy.com
49713531Sjairo.balart@metempsy.com      case GICD_IIDR: // Implementer Identification Register
49813531Sjairo.balart@metempsy.com        //return 0x43b; // ARM JEP106 code (r0p0 GIC-500)
49913531Sjairo.balart@metempsy.com        return 0;
50013531Sjairo.balart@metempsy.com
50113531Sjairo.balart@metempsy.com      case GICD_STATUSR: // Error Reporting Status Register
50213531Sjairo.balart@metempsy.com        // Optional register, RAZ/WI
50313531Sjairo.balart@metempsy.com        return 0x0;
50413531Sjairo.balart@metempsy.com
50513756Sjairo.balart@metempsy.com      case GICD_PIDR0: // Peripheral ID0 Register
50614167Sgiacomo.travaglini@arm.com        return gicdPidr0;
50713531Sjairo.balart@metempsy.com
50814167Sgiacomo.travaglini@arm.com      case GICD_PIDR1: // Peripheral ID1 Register
50914167Sgiacomo.travaglini@arm.com        return gicdPidr1;
51013531Sjairo.balart@metempsy.com
51114167Sgiacomo.travaglini@arm.com      case GICD_PIDR2: // Peripheral ID2 Register
51214167Sgiacomo.travaglini@arm.com        return gicdPidr2;
51313531Sjairo.balart@metempsy.com
51413531Sjairo.balart@metempsy.com      case GICD_PIDR3: // Peripheral ID3 Register
51514167Sgiacomo.travaglini@arm.com        return gicdPidr3;
51613531Sjairo.balart@metempsy.com
51714167Sgiacomo.travaglini@arm.com      case GICD_PIDR4: // Peripheral ID4 Register
51814167Sgiacomo.travaglini@arm.com        return gicdPidr4;
51913531Sjairo.balart@metempsy.com
52013531Sjairo.balart@metempsy.com      case GICD_PIDR5: // Peripheral ID5 Register
52113531Sjairo.balart@metempsy.com      case GICD_PIDR6: // Peripheral ID6 Register
52213531Sjairo.balart@metempsy.com      case GICD_PIDR7: // Peripheral ID7 Register
52313531Sjairo.balart@metempsy.com        return 0; // RES0
52413531Sjairo.balart@metempsy.com
52513531Sjairo.balart@metempsy.com      default:
52613531Sjairo.balart@metempsy.com        panic("Gicv3Distributor::read(): invalid offset %#x\n", addr);
52713531Sjairo.balart@metempsy.com        break;
52813531Sjairo.balart@metempsy.com    }
52913531Sjairo.balart@metempsy.com}
53013531Sjairo.balart@metempsy.com
53113531Sjairo.balart@metempsy.comvoid
53213531Sjairo.balart@metempsy.comGicv3Distributor::write(Addr addr, uint64_t data, size_t size,
53313531Sjairo.balart@metempsy.com                        bool is_secure_access)
53413531Sjairo.balart@metempsy.com{
53513531Sjairo.balart@metempsy.com    if (GICD_IGROUPR.contains(addr)) { // Interrupt Group Registers
53613531Sjairo.balart@metempsy.com        if (!DS && !is_secure_access) {
53713531Sjairo.balart@metempsy.com            // RAZ/WI for non-secure accesses
53813531Sjairo.balart@metempsy.com            return;
53913531Sjairo.balart@metempsy.com        }
54013531Sjairo.balart@metempsy.com
54113531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_IGROUPR.start()) * 8;
54213531Sjairo.balart@metempsy.com
54313531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
54413531Sjairo.balart@metempsy.com            return;
54513531Sjairo.balart@metempsy.com        }
54613531Sjairo.balart@metempsy.com
54713531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
54813756Sjairo.balart@metempsy.com             i++, int_id++) {
54913531Sjairo.balart@metempsy.com            irqGroup[int_id] = data & (1 << i) ? 1 : 0;
55013531Sjairo.balart@metempsy.com            DPRINTF(GIC, "Gicv3Distributor::write(): int_id %d group %d\n",
55113531Sjairo.balart@metempsy.com                    int_id, irqGroup[int_id]);
55213531Sjairo.balart@metempsy.com        }
55313531Sjairo.balart@metempsy.com
55413531Sjairo.balart@metempsy.com        return;
55513756Sjairo.balart@metempsy.com    } else if (GICD_ISENABLER.contains(addr)) {
55613531Sjairo.balart@metempsy.com        // Interrupt Set-Enable Registers
55713531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ISENABLER.start()) * 8;
55813531Sjairo.balart@metempsy.com
55913531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
56013531Sjairo.balart@metempsy.com            return;
56113531Sjairo.balart@metempsy.com        }
56213531Sjairo.balart@metempsy.com
56313531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
56413756Sjairo.balart@metempsy.com             i++, int_id++) {
56513756Sjairo.balart@metempsy.com
56613531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
56713531Sjairo.balart@metempsy.com            {
56813531Sjairo.balart@metempsy.com                continue;
56913531Sjairo.balart@metempsy.com            }
57013531Sjairo.balart@metempsy.com
57113531Sjairo.balart@metempsy.com            bool enable = data & (1 << i) ? 1 : 0;
57213531Sjairo.balart@metempsy.com
57313531Sjairo.balart@metempsy.com            if (enable) {
57413531Sjairo.balart@metempsy.com                if (!irqEnabled[int_id]) {
57513531Sjairo.balart@metempsy.com                    DPRINTF(GIC, "Gicv3Distributor::write(): "
57613531Sjairo.balart@metempsy.com                            "int_id %d enabled\n", int_id);
57713531Sjairo.balart@metempsy.com                }
57813531Sjairo.balart@metempsy.com
57913531Sjairo.balart@metempsy.com                irqEnabled[int_id] = true;
58013531Sjairo.balart@metempsy.com            }
58113531Sjairo.balart@metempsy.com        }
58213531Sjairo.balart@metempsy.com
58313531Sjairo.balart@metempsy.com        return;
58413531Sjairo.balart@metempsy.com    } else if (GICD_ICENABLER.contains(addr)) {
58513531Sjairo.balart@metempsy.com        // Interrupt Clear-Enable Registers
58613531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICENABLER.start()) * 8;
58713531Sjairo.balart@metempsy.com
58813812Sgiacomo.travaglini@arm.com        if (isNotSPI(first_intid)) {
58913812Sgiacomo.travaglini@arm.com            return;
59013812Sgiacomo.travaglini@arm.com        }
59113812Sgiacomo.travaglini@arm.com
59213531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
59313756Sjairo.balart@metempsy.com             i++, int_id++) {
59413756Sjairo.balart@metempsy.com
59513531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
59613531Sjairo.balart@metempsy.com            {
59713531Sjairo.balart@metempsy.com                continue;
59813531Sjairo.balart@metempsy.com            }
59913531Sjairo.balart@metempsy.com
60013531Sjairo.balart@metempsy.com            bool disable = data & (1 << i) ? 1 : 0;
60113531Sjairo.balart@metempsy.com
60213531Sjairo.balart@metempsy.com            if (disable) {
60313531Sjairo.balart@metempsy.com                if (irqEnabled[int_id]) {
60413531Sjairo.balart@metempsy.com                    DPRINTF(GIC, "Gicv3Distributor::write(): "
60513531Sjairo.balart@metempsy.com                            "int_id %d disabled\n", int_id);
60613531Sjairo.balart@metempsy.com                }
60713531Sjairo.balart@metempsy.com
60813531Sjairo.balart@metempsy.com                irqEnabled[int_id] = false;
60913531Sjairo.balart@metempsy.com            }
61013531Sjairo.balart@metempsy.com        }
61113531Sjairo.balart@metempsy.com
61213531Sjairo.balart@metempsy.com        return;
61313531Sjairo.balart@metempsy.com    } else if (GICD_ISPENDR.contains(addr)) {
61413756Sjairo.balart@metempsy.com        // Interrupt Set-Pending Registers
61513531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ISPENDR.start()) * 8;
61613531Sjairo.balart@metempsy.com
61713531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
61813531Sjairo.balart@metempsy.com            return;
61913531Sjairo.balart@metempsy.com        }
62013531Sjairo.balart@metempsy.com
62113531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
62213756Sjairo.balart@metempsy.com             i++, int_id++) {
62313756Sjairo.balart@metempsy.com
62413531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
62513531Sjairo.balart@metempsy.com            {
62613531Sjairo.balart@metempsy.com                if (irqNsacr[int_id] == 0) {
62713531Sjairo.balart@metempsy.com                    // Group 0 or Secure Group 1 interrupts are RAZ/WI
62813531Sjairo.balart@metempsy.com                    continue;
62913531Sjairo.balart@metempsy.com                }
63013531Sjairo.balart@metempsy.com            }
63113531Sjairo.balart@metempsy.com
63213531Sjairo.balart@metempsy.com            bool pending = data & (1 << i) ? 1 : 0;
63313531Sjairo.balart@metempsy.com
63413531Sjairo.balart@metempsy.com            if (pending) {
63513531Sjairo.balart@metempsy.com                DPRINTF(GIC, "Gicv3Distributor::write() (GICD_ISPENDR): "
63613531Sjairo.balart@metempsy.com                        "int_id %d (SPI) pending bit set\n", int_id);
63713531Sjairo.balart@metempsy.com                irqPending[int_id] = true;
63813531Sjairo.balart@metempsy.com            }
63913531Sjairo.balart@metempsy.com        }
64013531Sjairo.balart@metempsy.com
64113531Sjairo.balart@metempsy.com        updateAndInformCPUInterfaces();
64213531Sjairo.balart@metempsy.com        return;
64313531Sjairo.balart@metempsy.com    } else if (GICD_ICPENDR.contains(addr)) {
64413756Sjairo.balart@metempsy.com        // Interrupt Clear-Pending Registers
64513531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICPENDR.start()) * 8;
64613531Sjairo.balart@metempsy.com
64713531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
64813531Sjairo.balart@metempsy.com            return;
64913531Sjairo.balart@metempsy.com        }
65013531Sjairo.balart@metempsy.com
65113531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
65213756Sjairo.balart@metempsy.com             i++, int_id++) {
65313756Sjairo.balart@metempsy.com
65413531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
65513531Sjairo.balart@metempsy.com            {
65613531Sjairo.balart@metempsy.com                if (irqNsacr[int_id] < 2) {
65713531Sjairo.balart@metempsy.com                    // Group 0 or Secure Group 1 interrupts are RAZ/WI
65813531Sjairo.balart@metempsy.com                    continue;
65913531Sjairo.balart@metempsy.com                }
66013531Sjairo.balart@metempsy.com            }
66113531Sjairo.balart@metempsy.com
66213531Sjairo.balart@metempsy.com            bool clear = data & (1 << i) ? 1 : 0;
66313531Sjairo.balart@metempsy.com
66413531Sjairo.balart@metempsy.com            if (clear) {
66513531Sjairo.balart@metempsy.com                irqPending[int_id] = false;
66613531Sjairo.balart@metempsy.com            }
66713531Sjairo.balart@metempsy.com        }
66813531Sjairo.balart@metempsy.com
66913531Sjairo.balart@metempsy.com        updateAndInformCPUInterfaces();
67013531Sjairo.balart@metempsy.com        return;
67113756Sjairo.balart@metempsy.com    } else if (GICD_ISACTIVER.contains(addr)) {
67213531Sjairo.balart@metempsy.com        // Interrupt Set-Active Registers
67313531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ISACTIVER.start()) * 8;
67413531Sjairo.balart@metempsy.com
67513531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
67613531Sjairo.balart@metempsy.com            return;
67713531Sjairo.balart@metempsy.com        }
67813531Sjairo.balart@metempsy.com
67913531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
68013756Sjairo.balart@metempsy.com             i++, int_id++) {
68113756Sjairo.balart@metempsy.com
68213531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
68313531Sjairo.balart@metempsy.com            {
68413531Sjairo.balart@metempsy.com                continue;
68513531Sjairo.balart@metempsy.com            }
68613531Sjairo.balart@metempsy.com
68713531Sjairo.balart@metempsy.com            bool active = data & (1 << i) ? 1 : 0;
68813531Sjairo.balart@metempsy.com
68913531Sjairo.balart@metempsy.com            if (active) {
69013531Sjairo.balart@metempsy.com                irqActive[int_id] = 1;
69113531Sjairo.balart@metempsy.com            }
69213531Sjairo.balart@metempsy.com        }
69313531Sjairo.balart@metempsy.com
69413531Sjairo.balart@metempsy.com        return;
69513531Sjairo.balart@metempsy.com    } else if (GICD_ICACTIVER.contains(addr)) {
69613531Sjairo.balart@metempsy.com        // Interrupt Clear-Active Registers
69713531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICACTIVER.start()) * 8;
69813531Sjairo.balart@metempsy.com
69913531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
70013531Sjairo.balart@metempsy.com            return;
70113531Sjairo.balart@metempsy.com        }
70213531Sjairo.balart@metempsy.com
70313531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
70413756Sjairo.balart@metempsy.com             i++, int_id++) {
70513756Sjairo.balart@metempsy.com
70613531Sjairo.balart@metempsy.com            if (nsAccessToSecInt(int_id, is_secure_access))
70713531Sjairo.balart@metempsy.com            {
70813531Sjairo.balart@metempsy.com                continue;
70913531Sjairo.balart@metempsy.com            }
71013531Sjairo.balart@metempsy.com
71113531Sjairo.balart@metempsy.com            bool clear = data & (1 << i) ? 1 : 0;
71213531Sjairo.balart@metempsy.com
71313531Sjairo.balart@metempsy.com            if (clear) {
71413531Sjairo.balart@metempsy.com                if (irqActive[int_id]) {
71513531Sjairo.balart@metempsy.com                    DPRINTF(GIC, "Gicv3Distributor::write(): "
71613531Sjairo.balart@metempsy.com                            "int_id %d active cleared\n", int_id);
71713531Sjairo.balart@metempsy.com                }
71813531Sjairo.balart@metempsy.com
71913531Sjairo.balart@metempsy.com                irqActive[int_id] = false;
72013531Sjairo.balart@metempsy.com            }
72113531Sjairo.balart@metempsy.com        }
72213531Sjairo.balart@metempsy.com
72313531Sjairo.balart@metempsy.com        return;
72413756Sjairo.balart@metempsy.com    } else if (GICD_IPRIORITYR.contains(addr)) {
72513531Sjairo.balart@metempsy.com        // Interrupt Priority Registers
72613531Sjairo.balart@metempsy.com        int first_intid = addr - GICD_IPRIORITYR.start();
72713531Sjairo.balart@metempsy.com
72813531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
72913531Sjairo.balart@metempsy.com            return;
73013531Sjairo.balart@metempsy.com        }
73113531Sjairo.balart@metempsy.com
73213531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < size && int_id < itLines;
73313531Sjairo.balart@metempsy.com                i++, int_id++) {
73413531Sjairo.balart@metempsy.com            uint8_t prio = bits(data, (i + 1) * 8 - 1, (i * 8));
73513531Sjairo.balart@metempsy.com
73613531Sjairo.balart@metempsy.com            if (!DS && !is_secure_access) {
73713531Sjairo.balart@metempsy.com                if (getIntGroup(int_id) != Gicv3::G1NS) {
73813531Sjairo.balart@metempsy.com                    // RAZ/WI for non-secure accesses to secure interrupts
73913531Sjairo.balart@metempsy.com                    continue;
74013531Sjairo.balart@metempsy.com                } else {
74113531Sjairo.balart@metempsy.com                    prio = 0x80 | (prio >> 1);
74213531Sjairo.balart@metempsy.com                }
74313531Sjairo.balart@metempsy.com            }
74413531Sjairo.balart@metempsy.com
74513531Sjairo.balart@metempsy.com            irqPriority[int_id] = prio;
74613531Sjairo.balart@metempsy.com            DPRINTF(GIC, "Gicv3Distributor::write(): int_id %d priority %d\n",
74713531Sjairo.balart@metempsy.com                    int_id, irqPriority[int_id]);
74813531Sjairo.balart@metempsy.com        }
74913531Sjairo.balart@metempsy.com
75013531Sjairo.balart@metempsy.com        return;
75113531Sjairo.balart@metempsy.com    } else if (GICD_ITARGETSR.contains(addr)) {
75213531Sjairo.balart@metempsy.com        // Interrupt Processor Targets Registers
75313531Sjairo.balart@metempsy.com        // ARE always on, RAZ/WI
75413531Sjairo.balart@metempsy.com        warn("Gicv3Distributor::write(): "
75513531Sjairo.balart@metempsy.com             "GICD_ITARGETSR is RAZ/WI, legacy not supported!\n");
75613531Sjairo.balart@metempsy.com        return;
75713756Sjairo.balart@metempsy.com    } else if (GICD_ICFGR.contains(addr)) {
75813531Sjairo.balart@metempsy.com        // Interrupt Configuration Registers
75913756Sjairo.balart@metempsy.com        // for x = 0 to 15:
76013756Sjairo.balart@metempsy.com        //   GICD_ICFGR[2x] = RES0
76113756Sjairo.balart@metempsy.com        //   GICD_ICFGR[2x + 1] =
76213756Sjairo.balart@metempsy.com        //     0 level-sensitive
76313756Sjairo.balart@metempsy.com        //     1 edge-triggered
76413531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_ICFGR.start()) * 4;
76513531Sjairo.balart@metempsy.com
76613531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
76713531Sjairo.balart@metempsy.com            return;
76813531Sjairo.balart@metempsy.com        }
76913531Sjairo.balart@metempsy.com
77013531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid; i < 8 * size && int_id < itLines;
77113756Sjairo.balart@metempsy.com             i = i + 2, int_id++) {
77213531Sjairo.balart@metempsy.com            irqConfig[int_id] = data & (0x2 << i) ?
77313531Sjairo.balart@metempsy.com                                Gicv3::INT_EDGE_TRIGGERED :
77413531Sjairo.balart@metempsy.com                                Gicv3::INT_LEVEL_SENSITIVE;
77513531Sjairo.balart@metempsy.com            DPRINTF(GIC, "Gicv3Distributor::write(): int_id %d config %d\n",
77613531Sjairo.balart@metempsy.com                    int_id, irqConfig[int_id]);
77713531Sjairo.balart@metempsy.com        }
77813531Sjairo.balart@metempsy.com
77913531Sjairo.balart@metempsy.com        return;
78013531Sjairo.balart@metempsy.com    } else if (GICD_IGRPMODR.contains(addr)) {
78113531Sjairo.balart@metempsy.com        // Interrupt Group Modifier Registers
78213531Sjairo.balart@metempsy.com        if (DS) {
78313531Sjairo.balart@metempsy.com            return;
78413531Sjairo.balart@metempsy.com        } else {
78513531Sjairo.balart@metempsy.com            if (!is_secure_access) {
78613531Sjairo.balart@metempsy.com                // RAZ/WI for non-secure accesses
78713531Sjairo.balart@metempsy.com                return;
78813531Sjairo.balart@metempsy.com            } else {
78913531Sjairo.balart@metempsy.com                int first_intid = (addr - GICD_IGRPMODR.start()) * 8;
79013531Sjairo.balart@metempsy.com
79113531Sjairo.balart@metempsy.com                if (isNotSPI(first_intid)) {
79213531Sjairo.balart@metempsy.com                    return;
79313531Sjairo.balart@metempsy.com                }
79413531Sjairo.balart@metempsy.com
79513531Sjairo.balart@metempsy.com                for (int i = 0, int_id = first_intid;
79613756Sjairo.balart@metempsy.com                     i < 8 * size && int_id < itLines; i++, int_id++) {
79713531Sjairo.balart@metempsy.com                    irqGrpmod[int_id] = data & (0x1 << i);
79813531Sjairo.balart@metempsy.com                }
79913531Sjairo.balart@metempsy.com
80013531Sjairo.balart@metempsy.com                return ;
80113531Sjairo.balart@metempsy.com            }
80213531Sjairo.balart@metempsy.com        }
80313531Sjairo.balart@metempsy.com
80413756Sjairo.balart@metempsy.com    } else if (GICD_NSACR.contains(addr)) {
80513531Sjairo.balart@metempsy.com        // Non-secure Access Control Registers
80613531Sjairo.balart@metempsy.com        // 2 bits per interrupt
80713531Sjairo.balart@metempsy.com        int first_intid = (addr - GICD_NSACR.start()) * 4;
80813531Sjairo.balart@metempsy.com
80913531Sjairo.balart@metempsy.com        if (isNotSPI(first_intid)) {
81013531Sjairo.balart@metempsy.com            return;
81113531Sjairo.balart@metempsy.com        }
81213531Sjairo.balart@metempsy.com
81313531Sjairo.balart@metempsy.com        if (DS || (!DS && !is_secure_access)) {
81413531Sjairo.balart@metempsy.com            return;
81513531Sjairo.balart@metempsy.com        }
81613531Sjairo.balart@metempsy.com
81713531Sjairo.balart@metempsy.com        for (int i = 0, int_id = first_intid;
81813756Sjairo.balart@metempsy.com             i < 8 * size && int_id < itLines; i = i + 2, int_id++) {
81913531Sjairo.balart@metempsy.com            irqNsacr[int_id] = (data >> (2 * int_id)) & 0x3;
82013531Sjairo.balart@metempsy.com        }
82113531Sjairo.balart@metempsy.com
82213531Sjairo.balart@metempsy.com        return;
82313531Sjairo.balart@metempsy.com    } else if (GICD_IROUTER.contains(addr)) { // Interrupt Routing Registers
82413531Sjairo.balart@metempsy.com        // 64 bit registers. 2 accesses.
82513531Sjairo.balart@metempsy.com        int int_id = (addr - GICD_IROUTER.start()) / 8;
82613531Sjairo.balart@metempsy.com
82713531Sjairo.balart@metempsy.com        if (isNotSPI(int_id)) {
82813531Sjairo.balart@metempsy.com            return;
82913531Sjairo.balart@metempsy.com        }
83013531Sjairo.balart@metempsy.com
83113531Sjairo.balart@metempsy.com        if (nsAccessToSecInt(int_id, is_secure_access))
83213531Sjairo.balart@metempsy.com        {
83313531Sjairo.balart@metempsy.com            if (irqNsacr[int_id] < 3) {
83413531Sjairo.balart@metempsy.com                // Group 0 or Secure Group 1 interrupts are RAZ/WI
83513531Sjairo.balart@metempsy.com                return;
83613531Sjairo.balart@metempsy.com            }
83713531Sjairo.balart@metempsy.com        }
83813531Sjairo.balart@metempsy.com
83913531Sjairo.balart@metempsy.com        if (size == 4) {
84013531Sjairo.balart@metempsy.com            if (addr & 7) { // high half of 64 bit register
84113531Sjairo.balart@metempsy.com                irqAffinityRouting[int_id] =
84213531Sjairo.balart@metempsy.com                    (irqAffinityRouting[int_id] & 0xffffffff) | (data << 32);
84313531Sjairo.balart@metempsy.com            } else { // low half of 64 bit register
84413531Sjairo.balart@metempsy.com                irqAffinityRouting[int_id] =
84513531Sjairo.balart@metempsy.com                    (irqAffinityRouting[int_id] & 0xffffffff00000000) |
84613531Sjairo.balart@metempsy.com                    (data & 0xffffffff);
84713531Sjairo.balart@metempsy.com            }
84813531Sjairo.balart@metempsy.com        } else {
84913531Sjairo.balart@metempsy.com            irqAffinityRouting[int_id] = data;
85013531Sjairo.balart@metempsy.com        }
85113531Sjairo.balart@metempsy.com
85213531Sjairo.balart@metempsy.com        DPRINTF(GIC, "Gicv3Distributor::write(): "
85313531Sjairo.balart@metempsy.com                "int_id %d GICD_IROUTER %#llx\n",
85413531Sjairo.balart@metempsy.com                int_id, irqAffinityRouting[int_id]);
85513531Sjairo.balart@metempsy.com        return;
85613531Sjairo.balart@metempsy.com    }
85713531Sjairo.balart@metempsy.com
85813531Sjairo.balart@metempsy.com    switch (addr) {
85913531Sjairo.balart@metempsy.com      case GICD_CTLR: // Control Register
86013531Sjairo.balart@metempsy.com        if (DS) {
86113531Sjairo.balart@metempsy.com            /*
86213531Sjairo.balart@metempsy.com             * E1NWF [7]
86313531Sjairo.balart@metempsy.com             * 1 of N wakeup functionality not supported, RAZ/WI
86413531Sjairo.balart@metempsy.com             * DS [6] - RAO/WI
86513531Sjairo.balart@metempsy.com             * ARE [4]
86613531Sjairo.balart@metempsy.com             * affinity routing always on, no GICv2 legacy, RAO/WI
86713531Sjairo.balart@metempsy.com             * EnableGrp1 [1]
86813531Sjairo.balart@metempsy.com             * EnableGrp0 [0]
86913531Sjairo.balart@metempsy.com             */
87013531Sjairo.balart@metempsy.com            if ((data & (1 << 4)) == 0) {
87113531Sjairo.balart@metempsy.com                warn("Gicv3Distributor::write(): "
87213531Sjairo.balart@metempsy.com                        "setting ARE to 0 is not supported!\n");
87313531Sjairo.balart@metempsy.com            }
87413531Sjairo.balart@metempsy.com
87513531Sjairo.balart@metempsy.com            EnableGrp1NS = data & GICD_CTLR_ENABLEGRP1NS;
87613531Sjairo.balart@metempsy.com            EnableGrp0 = data & GICD_CTLR_ENABLEGRP0;
87713531Sjairo.balart@metempsy.com            DPRINTF(GIC, "Gicv3Distributor::write(): (DS 1)"
87813531Sjairo.balart@metempsy.com                    "EnableGrp1NS %d EnableGrp0 %d\n",
87913531Sjairo.balart@metempsy.com                    EnableGrp1NS, EnableGrp0);
88013531Sjairo.balart@metempsy.com        } else {
88113531Sjairo.balart@metempsy.com            if (is_secure_access) {
88213531Sjairo.balart@metempsy.com                /*
88313531Sjairo.balart@metempsy.com                 * E1NWF [7]
88413531Sjairo.balart@metempsy.com                 * 1 of N wakeup functionality not supported, RAZ/WI
88513531Sjairo.balart@metempsy.com                 * DS [6]
88613531Sjairo.balart@metempsy.com                 * ARE_NS [5]
88713531Sjairo.balart@metempsy.com                 * affinity routing always on, no GICv2 legacy, RAO/WI
88813531Sjairo.balart@metempsy.com                 * ARE_S [4]
88913531Sjairo.balart@metempsy.com                 * affinity routing always on, no GICv2 legacy, RAO/WI
89013531Sjairo.balart@metempsy.com                 * EnableGrp1S [2]
89113531Sjairo.balart@metempsy.com                 * EnableGrp1NS [1]
89213531Sjairo.balart@metempsy.com                 * EnableGrp0 [0]
89313531Sjairo.balart@metempsy.com                 */
89413531Sjairo.balart@metempsy.com                if ((data & (1 << 5)) == 0) {
89513531Sjairo.balart@metempsy.com                    warn("Gicv3Distributor::write(): "
89613531Sjairo.balart@metempsy.com                            "setting ARE_NS to 0 is not supported!\n");
89713531Sjairo.balart@metempsy.com                }
89813531Sjairo.balart@metempsy.com
89913531Sjairo.balart@metempsy.com                if ((data & (1 << 4)) == 0) {
90013531Sjairo.balart@metempsy.com                    warn("Gicv3Distributor::write(): "
90113531Sjairo.balart@metempsy.com                            "setting ARE_S to 0 is not supported!\n");
90213531Sjairo.balart@metempsy.com                }
90313531Sjairo.balart@metempsy.com
90413531Sjairo.balart@metempsy.com                DS = data & GICD_CTLR_DS;
90513531Sjairo.balart@metempsy.com                EnableGrp1S = data & GICD_CTLR_ENABLEGRP1S;
90613531Sjairo.balart@metempsy.com                EnableGrp1NS = data & GICD_CTLR_ENABLEGRP1NS;
90713531Sjairo.balart@metempsy.com                EnableGrp0 = data & GICD_CTLR_ENABLEGRP0;
90813531Sjairo.balart@metempsy.com                DPRINTF(GIC, "Gicv3Distributor::write(): (DS 0 secure)"
90913531Sjairo.balart@metempsy.com                        "DS %d "
91013531Sjairo.balart@metempsy.com                        "EnableGrp1S %d EnableGrp1NS %d EnableGrp0 %d\n",
91113531Sjairo.balart@metempsy.com                        DS, EnableGrp1S, EnableGrp1NS, EnableGrp0);
91213531Sjairo.balart@metempsy.com
91313531Sjairo.balart@metempsy.com                if (data & GICD_CTLR_DS) {
91413531Sjairo.balart@metempsy.com                    EnableGrp1S = 0;
91513531Sjairo.balart@metempsy.com                }
91613531Sjairo.balart@metempsy.com            } else {
91713531Sjairo.balart@metempsy.com                /*
91813531Sjairo.balart@metempsy.com                 * ARE_NS [4] RAO/WI;
91913531Sjairo.balart@metempsy.com                 * EnableGrp1A [1] is a read-write alias of the Secure
92013531Sjairo.balart@metempsy.com                 * GICD_CTLR.EnableGrp1NS
92113531Sjairo.balart@metempsy.com                 * EnableGrp1 [0] RES0
92213531Sjairo.balart@metempsy.com                 */
92313531Sjairo.balart@metempsy.com                if ((data & (1 << 4)) == 0) {
92413531Sjairo.balart@metempsy.com                    warn("Gicv3Distributor::write(): "
92513531Sjairo.balart@metempsy.com                            "setting ARE_NS to 0 is not supported!\n");
92613531Sjairo.balart@metempsy.com                }
92713531Sjairo.balart@metempsy.com
92813531Sjairo.balart@metempsy.com                EnableGrp1NS = data & GICD_CTLR_ENABLEGRP1A;
92913531Sjairo.balart@metempsy.com                DPRINTF(GIC, "Gicv3Distributor::write(): (DS 0 non-secure)"
93013531Sjairo.balart@metempsy.com                        "EnableGrp1NS %d\n", EnableGrp1NS);
93113531Sjairo.balart@metempsy.com            }
93213531Sjairo.balart@metempsy.com        }
93313531Sjairo.balart@metempsy.com
93413531Sjairo.balart@metempsy.com        break;
93513531Sjairo.balart@metempsy.com
93613531Sjairo.balart@metempsy.com      default:
93713531Sjairo.balart@metempsy.com        panic("Gicv3Distributor::write(): invalid offset %#x\n", addr);
93813531Sjairo.balart@metempsy.com        break;
93913531Sjairo.balart@metempsy.com    }
94013531Sjairo.balart@metempsy.com}
94113531Sjairo.balart@metempsy.com
94213531Sjairo.balart@metempsy.comvoid
94313531Sjairo.balart@metempsy.comGicv3Distributor::sendInt(uint32_t int_id)
94413531Sjairo.balart@metempsy.com{
94513531Sjairo.balart@metempsy.com    panic_if(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid SPI!");
94613531Sjairo.balart@metempsy.com    panic_if(int_id > itLines, "Invalid SPI!");
94713531Sjairo.balart@metempsy.com    irqPending[int_id] = true;
94813531Sjairo.balart@metempsy.com    DPRINTF(GIC, "Gicv3Distributor::sendInt(): "
94913531Sjairo.balart@metempsy.com            "int_id %d (SPI) pending bit set\n", int_id);
95013531Sjairo.balart@metempsy.com    updateAndInformCPUInterfaces();
95113531Sjairo.balart@metempsy.com}
95213531Sjairo.balart@metempsy.com
95313531Sjairo.balart@metempsy.comvoid
95413756Sjairo.balart@metempsy.comGicv3Distributor::deassertSPI(uint32_t int_id)
95513531Sjairo.balart@metempsy.com{
95613531Sjairo.balart@metempsy.com    panic_if(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid SPI!");
95713531Sjairo.balart@metempsy.com    panic_if(int_id > itLines, "Invalid SPI!");
95813531Sjairo.balart@metempsy.com    irqPending[int_id] = false;
95913531Sjairo.balart@metempsy.com    updateAndInformCPUInterfaces();
96013531Sjairo.balart@metempsy.com}
96113531Sjairo.balart@metempsy.com
96213531Sjairo.balart@metempsy.comvoid
96313531Sjairo.balart@metempsy.comGicv3Distributor::updateAndInformCPUInterfaces()
96413531Sjairo.balart@metempsy.com{
96513531Sjairo.balart@metempsy.com    update();
96613531Sjairo.balart@metempsy.com
96713531Sjairo.balart@metempsy.com    for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
96813531Sjairo.balart@metempsy.com        gic->getCPUInterface(i)->update();
96913531Sjairo.balart@metempsy.com    }
97013531Sjairo.balart@metempsy.com}
97113531Sjairo.balart@metempsy.com
97213531Sjairo.balart@metempsy.comvoid
97313531Sjairo.balart@metempsy.comGicv3Distributor::fullUpdate()
97413531Sjairo.balart@metempsy.com{
97513531Sjairo.balart@metempsy.com    for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
97613531Sjairo.balart@metempsy.com        Gicv3CPUInterface * cpu_interface_i = gic->getCPUInterface(i);
97713531Sjairo.balart@metempsy.com        cpu_interface_i->hppi.prio = 0xff;
97813531Sjairo.balart@metempsy.com    }
97913531Sjairo.balart@metempsy.com
98013531Sjairo.balart@metempsy.com    update();
98113531Sjairo.balart@metempsy.com
98213531Sjairo.balart@metempsy.com    for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
98313531Sjairo.balart@metempsy.com        Gicv3Redistributor * redistributor_i = gic->getRedistributor(i);
98413531Sjairo.balart@metempsy.com        redistributor_i->update();
98513531Sjairo.balart@metempsy.com    }
98613531Sjairo.balart@metempsy.com}
98713531Sjairo.balart@metempsy.com
98813531Sjairo.balart@metempsy.comvoid
98913531Sjairo.balart@metempsy.comGicv3Distributor::update()
99013531Sjairo.balart@metempsy.com{
99113531Sjairo.balart@metempsy.com    std::vector<bool> new_hppi(gic->getSystem()->numContexts(), false);
99213531Sjairo.balart@metempsy.com
99313531Sjairo.balart@metempsy.com    // Find the highest priority pending SPI
99413531Sjairo.balart@metempsy.com    for (int int_id = Gicv3::SGI_MAX + Gicv3::PPI_MAX; int_id < itLines;
99513756Sjairo.balart@metempsy.com         int_id++) {
99613531Sjairo.balart@metempsy.com        Gicv3::GroupId int_group = getIntGroup(int_id);
99713531Sjairo.balart@metempsy.com        bool group_enabled = groupEnabled(int_group);
99813531Sjairo.balart@metempsy.com
99913531Sjairo.balart@metempsy.com        if (irqPending[int_id] && irqEnabled[int_id] &&
100013756Sjairo.balart@metempsy.com            !irqActive[int_id] && group_enabled) {
100113531Sjairo.balart@metempsy.com            IROUTER affinity_routing = irqAffinityRouting[int_id];
100213531Sjairo.balart@metempsy.com            Gicv3Redistributor * target_redistributor = nullptr;
100313531Sjairo.balart@metempsy.com
100413531Sjairo.balart@metempsy.com            if (affinity_routing.IRM) {
100513531Sjairo.balart@metempsy.com                // Interrupts routed to any PE defined as a participating node
100613531Sjairo.balart@metempsy.com                for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
100713531Sjairo.balart@metempsy.com                    Gicv3Redistributor * redistributor_i =
100813531Sjairo.balart@metempsy.com                        gic->getRedistributor(i);
100913531Sjairo.balart@metempsy.com
101013531Sjairo.balart@metempsy.com                    if (redistributor_i->
101113531Sjairo.balart@metempsy.com                            canBeSelectedFor1toNInterrupt(int_group)) {
101213531Sjairo.balart@metempsy.com                        target_redistributor = redistributor_i;
101313531Sjairo.balart@metempsy.com                        break;
101413531Sjairo.balart@metempsy.com                    }
101513531Sjairo.balart@metempsy.com                }
101613531Sjairo.balart@metempsy.com            } else {
101713531Sjairo.balart@metempsy.com                uint32_t affinity = (affinity_routing.Aff3 << 24) |
101813531Sjairo.balart@metempsy.com                                    (affinity_routing.Aff3 << 16) |
101913531Sjairo.balart@metempsy.com                                    (affinity_routing.Aff1 << 8) |
102013531Sjairo.balart@metempsy.com                                    (affinity_routing.Aff0 << 0);
102113531Sjairo.balart@metempsy.com                target_redistributor =
102213531Sjairo.balart@metempsy.com                    gic->getRedistributorByAffinity(affinity);
102313531Sjairo.balart@metempsy.com            }
102413531Sjairo.balart@metempsy.com
102513531Sjairo.balart@metempsy.com            if (!target_redistributor) {
102613531Sjairo.balart@metempsy.com                // Interrrupts targeting not present cpus must remain pending
102713531Sjairo.balart@metempsy.com                return;
102813531Sjairo.balart@metempsy.com            }
102913531Sjairo.balart@metempsy.com
103013531Sjairo.balart@metempsy.com            Gicv3CPUInterface * target_cpu_interface =
103113531Sjairo.balart@metempsy.com                target_redistributor->getCPUInterface();
103213531Sjairo.balart@metempsy.com            uint32_t target_cpu = target_redistributor->cpuId;
103313531Sjairo.balart@metempsy.com
103413531Sjairo.balart@metempsy.com            if ((irqPriority[int_id] < target_cpu_interface->hppi.prio) ||
103513756Sjairo.balart@metempsy.com                /*
103613756Sjairo.balart@metempsy.com                 * Multiple pending ints with same priority.
103713756Sjairo.balart@metempsy.com                 * Implementation choice which one to signal.
103813756Sjairo.balart@metempsy.com                 * Our implementation selects the one with the lower id.
103913756Sjairo.balart@metempsy.com                 */
104013756Sjairo.balart@metempsy.com                (irqPriority[int_id] == target_cpu_interface->hppi.prio &&
104113756Sjairo.balart@metempsy.com                int_id < target_cpu_interface->hppi.intid)) {
104213531Sjairo.balart@metempsy.com                target_cpu_interface->hppi.intid = int_id;
104313531Sjairo.balart@metempsy.com                target_cpu_interface->hppi.prio = irqPriority[int_id];
104413531Sjairo.balart@metempsy.com                target_cpu_interface->hppi.group = int_group;
104513531Sjairo.balart@metempsy.com                new_hppi[target_cpu] = true;
104613531Sjairo.balart@metempsy.com            }
104713531Sjairo.balart@metempsy.com        }
104813531Sjairo.balart@metempsy.com    }
104913531Sjairo.balart@metempsy.com
105013531Sjairo.balart@metempsy.com    for (int i = 0; i < gic->getSystem()->numContexts(); i++) {
105113531Sjairo.balart@metempsy.com        Gicv3Redistributor * redistributor_i = gic->getRedistributor(i);
105213531Sjairo.balart@metempsy.com        Gicv3CPUInterface * cpu_interface_i =
105313531Sjairo.balart@metempsy.com            redistributor_i->getCPUInterface();
105413531Sjairo.balart@metempsy.com
105513531Sjairo.balart@metempsy.com        if (!new_hppi[i] && cpu_interface_i->hppi.prio != 0xff &&
105613756Sjairo.balart@metempsy.com            cpu_interface_i->hppi.intid >= (Gicv3::SGI_MAX + Gicv3::PPI_MAX) &&
105713756Sjairo.balart@metempsy.com            cpu_interface_i->hppi.intid < Gicv3::INTID_SECURE) {
105813531Sjairo.balart@metempsy.com            fullUpdate();
105913531Sjairo.balart@metempsy.com        }
106013531Sjairo.balart@metempsy.com    }
106113531Sjairo.balart@metempsy.com}
106213531Sjairo.balart@metempsy.com
106313531Sjairo.balart@metempsy.comGicv3::IntStatus
106413756Sjairo.balart@metempsy.comGicv3Distributor::intStatus(uint32_t int_id) const
106513531Sjairo.balart@metempsy.com{
106613531Sjairo.balart@metempsy.com    panic_if(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid SPI!");
106713531Sjairo.balart@metempsy.com    panic_if(int_id > itLines, "Invalid SPI!");
106813531Sjairo.balart@metempsy.com
106913531Sjairo.balart@metempsy.com    if (irqPending[int_id]) {
107013531Sjairo.balart@metempsy.com        if (irqActive[int_id]) {
107113531Sjairo.balart@metempsy.com            return Gicv3::INT_ACTIVE_PENDING;
107213531Sjairo.balart@metempsy.com        }
107313531Sjairo.balart@metempsy.com
107413531Sjairo.balart@metempsy.com        return Gicv3::INT_PENDING;
107513531Sjairo.balart@metempsy.com    } else if (irqActive[int_id]) {
107613531Sjairo.balart@metempsy.com        return Gicv3::INT_ACTIVE;
107713531Sjairo.balart@metempsy.com    } else {
107813531Sjairo.balart@metempsy.com        return Gicv3::INT_INACTIVE;
107913531Sjairo.balart@metempsy.com    }
108013531Sjairo.balart@metempsy.com}
108113531Sjairo.balart@metempsy.com
108213531Sjairo.balart@metempsy.comGicv3::GroupId
108313756Sjairo.balart@metempsy.comGicv3Distributor::getIntGroup(int int_id) const
108413531Sjairo.balart@metempsy.com{
108513531Sjairo.balart@metempsy.com    panic_if(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid SPI!");
108613531Sjairo.balart@metempsy.com    panic_if(int_id > itLines, "Invalid SPI!");
108713531Sjairo.balart@metempsy.com
108813531Sjairo.balart@metempsy.com    if (DS) {
108913531Sjairo.balart@metempsy.com        if (irqGroup[int_id] == 1) {
109013531Sjairo.balart@metempsy.com            return Gicv3::G1NS;
109113531Sjairo.balart@metempsy.com        } else {
109213531Sjairo.balart@metempsy.com            return Gicv3::G0S;
109313531Sjairo.balart@metempsy.com        }
109413531Sjairo.balart@metempsy.com    } else {
109513531Sjairo.balart@metempsy.com        if (irqGrpmod[int_id] == 0 && irqGroup[int_id] == 0) {
109613531Sjairo.balart@metempsy.com            return Gicv3::G0S;
109713531Sjairo.balart@metempsy.com        } else if (irqGrpmod[int_id] == 0 && irqGroup[int_id] == 1) {
109813531Sjairo.balart@metempsy.com            return Gicv3::G1NS;
109913531Sjairo.balart@metempsy.com        } else if (irqGrpmod[int_id] == 1 && irqGroup[int_id] == 0) {
110013531Sjairo.balart@metempsy.com            return Gicv3::G1S;
110113531Sjairo.balart@metempsy.com        } else if (irqGrpmod[int_id] == 1 && irqGroup[int_id] == 1) {
110213531Sjairo.balart@metempsy.com            return Gicv3::G1NS;
110313531Sjairo.balart@metempsy.com        }
110413531Sjairo.balart@metempsy.com    }
110513531Sjairo.balart@metempsy.com
110613531Sjairo.balart@metempsy.com    M5_UNREACHABLE;
110713531Sjairo.balart@metempsy.com}
110813531Sjairo.balart@metempsy.com
110913531Sjairo.balart@metempsy.comvoid
111013531Sjairo.balart@metempsy.comGicv3Distributor::activateIRQ(uint32_t int_id)
111113531Sjairo.balart@metempsy.com{
111213531Sjairo.balart@metempsy.com    irqPending[int_id] = false;
111313531Sjairo.balart@metempsy.com    irqActive[int_id] = true;
111413531Sjairo.balart@metempsy.com}
111513531Sjairo.balart@metempsy.com
111613531Sjairo.balart@metempsy.comvoid
111713531Sjairo.balart@metempsy.comGicv3Distributor::deactivateIRQ(uint32_t int_id)
111813531Sjairo.balart@metempsy.com{
111913531Sjairo.balart@metempsy.com    irqActive[int_id] = false;
112013531Sjairo.balart@metempsy.com}
112113531Sjairo.balart@metempsy.com
112213531Sjairo.balart@metempsy.comvoid
112313531Sjairo.balart@metempsy.comGicv3Distributor::serialize(CheckpointOut & cp) const
112413531Sjairo.balart@metempsy.com{
112513531Sjairo.balart@metempsy.com    SERIALIZE_SCALAR(ARE);
112613531Sjairo.balart@metempsy.com    SERIALIZE_SCALAR(DS);
112713531Sjairo.balart@metempsy.com    SERIALIZE_SCALAR(EnableGrp1S);
112813531Sjairo.balart@metempsy.com    SERIALIZE_SCALAR(EnableGrp1NS);
112913531Sjairo.balart@metempsy.com    SERIALIZE_SCALAR(EnableGrp0);
113013531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqGroup);
113113531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqEnabled);
113213531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqPending);
113313531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqActive);
113413531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqPriority);
113513531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqConfig);
113613531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqGrpmod);
113713531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqNsacr);
113813531Sjairo.balart@metempsy.com    SERIALIZE_CONTAINER(irqAffinityRouting);
113913531Sjairo.balart@metempsy.com}
114013531Sjairo.balart@metempsy.com
114113531Sjairo.balart@metempsy.comvoid
114213531Sjairo.balart@metempsy.comGicv3Distributor::unserialize(CheckpointIn & cp)
114313531Sjairo.balart@metempsy.com{
114413531Sjairo.balart@metempsy.com    UNSERIALIZE_SCALAR(ARE);
114513531Sjairo.balart@metempsy.com    UNSERIALIZE_SCALAR(DS);
114613531Sjairo.balart@metempsy.com    UNSERIALIZE_SCALAR(EnableGrp1S);
114713531Sjairo.balart@metempsy.com    UNSERIALIZE_SCALAR(EnableGrp1NS);
114813531Sjairo.balart@metempsy.com    UNSERIALIZE_SCALAR(EnableGrp0);
114913531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqGroup);
115013531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqEnabled);
115113531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqPending);
115213531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqActive);
115313531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqPriority);
115413531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqConfig);
115513531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqGrpmod);
115613531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqNsacr);
115713531Sjairo.balart@metempsy.com    UNSERIALIZE_CONTAINER(irqAffinityRouting);
115813531Sjairo.balart@metempsy.com}
1159