gic_v3.cc revision 13623:fa728033d933
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_redistributor.hh"
39#include "dev/platform.hh"
40#include "mem/packet.hh"
41#include "mem/packet_access.hh"
42
43Gicv3::Gicv3(const Params * p)
44    : BaseGic(p)
45{
46}
47
48Gicv3::~Gicv3()
49{
50}
51
52void
53Gicv3::init()
54{
55    distRange = RangeSize(params()->dist_addr,
56                          Gicv3Distributor::ADDR_RANGE_SIZE - 1);
57    redistRange = RangeSize(params()->redist_addr,
58        Gicv3Redistributor::ADDR_RANGE_SIZE * sys->numContexts() - 1);
59    addrRanges = {distRange, redistRange};
60    BaseGic::init();
61    distributor = new Gicv3Distributor(this, params()->it_lines);
62    redistributors.resize(sys->numContexts(), nullptr);
63    cpuInterfaces.resize(sys->numContexts(), nullptr);
64
65    for (int i = 0; i < sys->numContexts(); i++) {
66        redistributors[i] = new Gicv3Redistributor(this, i);
67        cpuInterfaces[i] = new Gicv3CPUInterface(this, i);
68    }
69
70    distributor->init();
71
72    for (int i = 0; i < sys->numContexts(); i++) {
73        redistributors[i]->init();
74        cpuInterfaces[i]->init();
75    }
76}
77
78void
79Gicv3::initState()
80{
81    distributor->initState();
82
83    for (int i = 0; i < sys->numContexts(); i++) {
84        redistributors[i]->initState();
85        cpuInterfaces[i]->initState();
86    }
87}
88
89Tick
90Gicv3::read(PacketPtr pkt)
91{
92    const Addr addr = pkt->getAddr();
93    const size_t size = pkt->getSize();
94    bool is_secure_access = pkt->isSecure();
95    uint64_t resp = 0;
96    Tick delay = 0;
97
98    if (distRange.contains(addr)) {
99        const Addr daddr = addr - distRange.start();
100        panic_if(!distributor, "Distributor is null!");
101        resp = distributor->read(daddr, size, is_secure_access);
102        delay = params()->dist_pio_delay;
103        DPRINTF(GIC, "Gicv3::read(): (distributor) context_id %d register %#x "
104                "size %d is_secure_access %d (value %#x)\n",
105                pkt->req->contextId(), daddr, size, is_secure_access, resp);
106    } else if (redistRange.contains(addr)) {
107        Addr daddr = addr - redistRange.start();
108        uint32_t redistributor_id =
109            daddr / Gicv3Redistributor::ADDR_RANGE_SIZE;
110        daddr = daddr % Gicv3Redistributor::ADDR_RANGE_SIZE;
111        panic_if(redistributor_id >= redistributors.size(),
112                 "Invalid redistributor_id!");
113        panic_if(!redistributors[redistributor_id], "Redistributor is null!");
114        resp = redistributors[redistributor_id]->read(daddr, size,
115                is_secure_access);
116        delay = params()->redist_pio_delay;
117        DPRINTF(GIC, "Gicv3::read(): (redistributor %d) context_id %d "
118                "register %#x size %d is_secure_access %d (value %#x)\n",
119                redistributor_id, pkt->req->contextId(), daddr, size,
120                is_secure_access, resp);
121    } else {
122        panic("Gicv3::read(): unknown address %#x\n", addr);
123    }
124
125    pkt->setUintX(resp, LittleEndianByteOrder);
126    pkt->makeAtomicResponse();
127    return delay;
128}
129
130Tick
131Gicv3::write(PacketPtr pkt)
132{
133    const size_t size = pkt->getSize();
134    uint64_t data = pkt->getUintX(LittleEndianByteOrder);
135    const Addr addr = pkt->getAddr();
136    bool is_secure_access = pkt->isSecure();
137    Tick delay = 0;
138
139    if (distRange.contains(addr)) {
140        const Addr daddr = addr - distRange.start();
141        panic_if(!distributor, "Distributor is null!");
142        DPRINTF(GIC, "Gicv3::write(): (distributor) context_id %d "
143                "register %#x size %d is_secure_access %d value %#x\n",
144                pkt->req->contextId(), daddr, size, is_secure_access, data);
145        distributor->write(daddr, data, size, is_secure_access);
146        delay = params()->dist_pio_delay;
147    } else if (redistRange.contains(addr)) {
148        Addr daddr = addr - redistRange.start();
149        uint32_t redistributor_id =
150            daddr / Gicv3Redistributor::ADDR_RANGE_SIZE;
151        daddr = daddr % Gicv3Redistributor::ADDR_RANGE_SIZE;
152        panic_if(redistributor_id >= redistributors.size(),
153                 "Invalid redistributor_id!");
154        panic_if(!redistributors[redistributor_id], "Redistributor is null!");
155        DPRINTF(GIC, "Gicv3::write(): (redistributor %d) context_id %d "
156                "register %#x size %d is_secure_access %d value %#x\n",
157                redistributor_id, pkt->req->contextId(), daddr, size,
158                is_secure_access, data);
159        redistributors[redistributor_id]->write(daddr, data, size,
160                                                is_secure_access);
161        delay = params()->redist_pio_delay;
162    } else {
163        panic("Gicv3::write(): unknown address %#x\n", addr);
164    }
165
166    pkt->makeAtomicResponse();
167    return delay;
168}
169
170void
171Gicv3::sendInt(uint32_t int_id)
172{
173    panic_if(int_id < Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid SPI!");
174    panic_if(int_id >= Gicv3::INTID_SECURE, "Invalid SPI!");
175    DPRINTF(Interrupt, "Gicv3::sendInt(): received SPI %d\n", int_id);
176    distributor->sendInt(int_id);
177}
178
179void
180Gicv3::clearInt(uint32_t number)
181{
182    distributor->intDeasserted(number);
183}
184
185void
186Gicv3::sendPPInt(uint32_t int_id, uint32_t cpu)
187{
188    panic_if(cpu >= redistributors.size(), "Invalid cpuID sending PPI!");
189    panic_if(int_id < Gicv3::SGI_MAX, "Invalid PPI!");
190    panic_if(int_id >= Gicv3::SGI_MAX + Gicv3::PPI_MAX, "Invalid PPI!");
191    DPRINTF(Interrupt, "Gicv3::sendPPInt(): received PPI %d cpuTarget %#x\n",
192            int_id, cpu);
193    redistributors[cpu]->sendPPInt(int_id);
194}
195
196void
197Gicv3::clearPPInt(uint32_t num, uint32_t cpu)
198{
199}
200
201void
202Gicv3::postInt(uint32_t cpu, ArmISA::InterruptTypes int_type)
203{
204    postDelayedInt(cpu, int_type);
205}
206
207void
208Gicv3::deassertInt(uint32_t cpu, ArmISA::InterruptTypes int_type)
209{
210    platform->intrctrl->clear(cpu, int_type, 0);
211}
212
213void
214Gicv3::postDelayedInt(uint32_t cpu, ArmISA::InterruptTypes int_type)
215{
216    platform->intrctrl->post(cpu, int_type, 0);
217}
218
219Gicv3Redistributor *
220Gicv3::getRedistributorByAffinity(uint32_t affinity)
221{
222    for (auto & redistributor : redistributors) {
223        if (redistributor->getAffinity() == affinity) {
224            return redistributor;
225        }
226    }
227
228    return nullptr;
229}
230
231void
232Gicv3::serialize(CheckpointOut & cp) const
233{
234    distributor->serializeSection(cp, "distributor");
235
236    for (uint32_t redistributor_id = 0;
237         redistributor_id < redistributors.size();
238         redistributor_id++)
239        redistributors[redistributor_id]->serializeSection(cp,
240            csprintf("redistributors.%i", redistributor_id));
241
242    for (uint32_t cpu_interface_id = 0;
243         cpu_interface_id < cpuInterfaces.size();
244         cpu_interface_id++)
245        cpuInterfaces[cpu_interface_id]->serializeSection(cp,
246            csprintf("cpuInterface.%i", cpu_interface_id));
247}
248
249void
250Gicv3::unserialize(CheckpointIn & cp)
251{
252    getSystem()->setGIC(this);
253
254    distributor->unserializeSection(cp, "distributor");
255
256    for (uint32_t redistributor_id = 0;
257         redistributor_id < redistributors.size();
258         redistributor_id++)
259        redistributors[redistributor_id]->unserializeSection(cp,
260            csprintf("redistributors.%i", redistributor_id));
261
262    for (uint32_t cpu_interface_id = 0;
263         cpu_interface_id < cpuInterfaces.size();
264         cpu_interface_id++)
265        cpuInterfaces[cpu_interface_id]->unserializeSection(cp,
266            csprintf("cpuInterface.%i", cpu_interface_id));
267}
268
269Gicv3 *
270Gicv3Params::create()
271{
272    return new Gicv3(this);
273}
274