gic_v3.cc revision 13996:8a567118e670
1/*
2 * Copyright (c) 2018 Metempsy Technology Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Jairo Balart
29 */
30
31#include "dev/arm/gic_v3.hh"
32
33#include "cpu/intr_control.hh"
34#include "debug/GIC.hh"
35#include "debug/Interrupt.hh"
36#include "dev/arm/gic_v3_cpu_interface.hh"
37#include "dev/arm/gic_v3_distributor.hh"
38#include "dev/arm/gic_v3_its.hh"
39#include "dev/arm/gic_v3_redistributor.hh"
40#include "dev/platform.hh"
41#include "mem/packet.hh"
42#include "mem/packet_access.hh"
43
44Gicv3::Gicv3(const Params * p)
45    : BaseGic(p)
46{
47}
48
49void
50Gicv3::init()
51{
52    distributor = new Gicv3Distributor(this, params()->it_lines);
53    redistributors.resize(sys->numContexts(), nullptr);
54    cpuInterfaces.resize(sys->numContexts(), nullptr);
55
56    panic_if(sys->numContexts() > params()->cpu_max,
57        "Exceeding maximum number of PEs supported by GICv3: "
58        "using %u while maximum is %u\n", sys->numContexts(),
59        params()->cpu_max);
60
61    for (int i = 0; i < sys->numContexts(); i++) {
62        redistributors[i] = new Gicv3Redistributor(this, i);
63        cpuInterfaces[i] = new Gicv3CPUInterface(this, i);
64    }
65
66    distRange = RangeSize(params()->dist_addr,
67        Gicv3Distributor::ADDR_RANGE_SIZE - 1);
68
69    redistSize = redistributors[0]->addrRangeSize;
70    redistRange = RangeSize(params()->redist_addr,
71         redistSize * sys->numContexts() - 1);
72
73    addrRanges = {distRange, redistRange};
74
75    distributor->init();
76
77    for (int i = 0; i < sys->numContexts(); i++) {
78        redistributors[i]->init();
79        cpuInterfaces[i]->init();
80    }
81
82    params()->its->setGIC(this);
83
84    BaseGic::init();
85}
86
87void
88Gicv3::initState()
89{
90    distributor->initState();
91
92    for (int i = 0; i < sys->numContexts(); i++) {
93        redistributors[i]->initState();
94        cpuInterfaces[i]->initState();
95    }
96}
97
98Tick
99Gicv3::read(PacketPtr pkt)
100{
101    const Addr addr = pkt->getAddr();
102    const size_t size = pkt->getSize();
103    bool is_secure_access = pkt->isSecure();
104    uint64_t resp = 0;
105    Tick delay = 0;
106
107    if (distRange.contains(addr)) {
108        const Addr daddr = addr - distRange.start();
109        panic_if(!distributor, "Distributor is null!");
110        resp = distributor->read(daddr, size, is_secure_access);
111        delay = params()->dist_pio_delay;
112        DPRINTF(GIC, "Gicv3::read(): (distributor) context_id %d register %#x "
113                "size %d is_secure_access %d (value %#x)\n",
114                pkt->req->contextId(), daddr, size, is_secure_access, resp);
115    } else if (redistRange.contains(addr)) {
116        Addr daddr = (addr - redistRange.start()) % redistSize;
117
118        Gicv3Redistributor *redist = getRedistributorByAddr(addr);
119        resp = redist->read(daddr, size, is_secure_access);
120
121        delay = params()->redist_pio_delay;
122        DPRINTF(GIC, "Gicv3::read(): (redistributor %d) context_id %d "
123                "register %#x size %d is_secure_access %d (value %#x)\n",
124                redist->processorNumber(), pkt->req->contextId(), daddr, size,
125                is_secure_access, resp);
126    } else {
127        panic("Gicv3::read(): unknown address %#x\n", addr);
128    }
129
130    pkt->setUintX(resp, LittleEndianByteOrder);
131    pkt->makeAtomicResponse();
132    return delay;
133}
134
135Tick
136Gicv3::write(PacketPtr pkt)
137{
138    const size_t size = pkt->getSize();
139    uint64_t data = pkt->getUintX(LittleEndianByteOrder);
140    const Addr addr = pkt->getAddr();
141    bool is_secure_access = pkt->isSecure();
142    Tick delay = 0;
143
144    if (distRange.contains(addr)) {
145        const Addr daddr = addr - distRange.start();
146        panic_if(!distributor, "Distributor is null!");
147        DPRINTF(GIC, "Gicv3::write(): (distributor) context_id %d "
148                "register %#x size %d is_secure_access %d value %#x\n",
149                pkt->req->contextId(), daddr, size, is_secure_access, data);
150        distributor->write(daddr, data, size, is_secure_access);
151        delay = params()->dist_pio_delay;
152    } else if (redistRange.contains(addr)) {
153        Addr daddr = (addr - redistRange.start()) % redistSize;
154
155        Gicv3Redistributor *redist = getRedistributorByAddr(addr);
156        DPRINTF(GIC, "Gicv3::write(): (redistributor %d) context_id %d "
157                "register %#x size %d is_secure_access %d value %#x\n",
158                redist->processorNumber(), pkt->req->contextId(), daddr, size,
159                is_secure_access, data);
160
161        redist->write(daddr, data, size, is_secure_access);
162
163        delay = params()->redist_pio_delay;
164    } else {
165        panic("Gicv3::write(): unknown address %#x\n", addr);
166    }
167
168    pkt->makeAtomicResponse();
169    return delay;
170}
171
172void
173Gicv3::sendInt(uint32_t int_id)
174{
175    panic_if(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid SPI!");
176    panic_if(int_id >= Gicv3::INTID_SECURE, "Invalid SPI!");
177    DPRINTF(Interrupt, "Gicv3::sendInt(): received SPI %d\n", int_id);
178    distributor->sendInt(int_id);
179}
180
181void
182Gicv3::clearInt(uint32_t number)
183{
184    distributor->deassertSPI(number);
185}
186
187void
188Gicv3::sendPPInt(uint32_t int_id, uint32_t cpu)
189{
190    panic_if(cpu >= redistributors.size(), "Invalid cpuID sending PPI!");
191    panic_if(int_id < Gicv3::SGI_MAX, "Invalid PPI!");
192    panic_if(int_id >= Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid PPI!");
193    DPRINTF(Interrupt, "Gicv3::sendPPInt(): received PPI %d cpuTarget %#x\n",
194            int_id, cpu);
195    redistributors[cpu]->sendPPInt(int_id);
196}
197
198void
199Gicv3::clearPPInt(uint32_t num, uint32_t cpu)
200{
201}
202
203void
204Gicv3::postInt(uint32_t cpu, ArmISA::InterruptTypes int_type)
205{
206    platform->intrctrl->post(cpu, int_type, 0);
207}
208
209void
210Gicv3::deassertInt(uint32_t cpu, ArmISA::InterruptTypes int_type)
211{
212    platform->intrctrl->clear(cpu, int_type, 0);
213}
214
215Gicv3Redistributor *
216Gicv3::getRedistributorByAffinity(uint32_t affinity) const
217{
218    for (auto & redistributor : redistributors) {
219        if (redistributor->getAffinity() == affinity) {
220            return redistributor;
221        }
222    }
223
224    return nullptr;
225}
226
227Gicv3Redistributor *
228Gicv3::getRedistributorByAddr(Addr addr) const
229{
230    panic_if(!redistRange.contains(addr),
231        "Address not pointing to a valid redistributor\n");
232
233    const Addr daddr = addr - redistRange.start();
234    const uint32_t redistributor_id = daddr / redistSize;
235
236    panic_if(redistributor_id >= redistributors.size(),
237             "Invalid redistributor_id!");
238    panic_if(!redistributors[redistributor_id], "Redistributor is null!");
239
240    return redistributors[redistributor_id];
241}
242
243void
244Gicv3::serialize(CheckpointOut & cp) const
245{
246    distributor->serializeSection(cp, "distributor");
247
248    for (uint32_t redistributor_id = 0;
249         redistributor_id < redistributors.size(); redistributor_id++)
250        redistributors[redistributor_id]->serializeSection(cp,
251            csprintf("redistributors.%i", redistributor_id));
252
253    for (uint32_t cpu_interface_id = 0;
254         cpu_interface_id < cpuInterfaces.size(); cpu_interface_id++)
255        cpuInterfaces[cpu_interface_id]->serializeSection(cp,
256            csprintf("cpuInterface.%i", cpu_interface_id));
257}
258
259void
260Gicv3::unserialize(CheckpointIn & cp)
261{
262    getSystem()->setGIC(this);
263
264    distributor->unserializeSection(cp, "distributor");
265
266    for (uint32_t redistributor_id = 0;
267         redistributor_id < redistributors.size(); redistributor_id++)
268        redistributors[redistributor_id]->unserializeSection(cp,
269            csprintf("redistributors.%i", redistributor_id));
270
271    for (uint32_t cpu_interface_id = 0;
272         cpu_interface_id < cpuInterfaces.size(); cpu_interface_id++)
273        cpuInterfaces[cpu_interface_id]->unserializeSection(cp,
274            csprintf("cpuInterface.%i", cpu_interface_id));
275}
276
277Gicv3 *
278Gicv3Params::create()
279{
280    return new Gicv3(this);
281}
282