gem5 v24.0.0.0
Loading...
Searching...
No Matches
gic.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015-2017, 2021 Arm Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "arch/arm/kvm/gic.hh"
39
40#include <linux/kvm.h>
41
43#include "arch/arm/regs/misc.hh"
44#include "debug/GIC.hh"
45#include "debug/Interrupt.hh"
46#include "params/MuxingKvmGicV2.hh"
47
48namespace gem5
49{
50
51KvmKernelGic::KvmKernelGic(KvmVM &_vm, uint32_t dev, unsigned it_lines)
52 : vm(_vm),
53 kdev(vm.createDevice(dev))
54{
55 // Tell the VM that we will emulate the GIC in the kernel. This
56 // disables IRQ and FIQ handling in the KVM CPU model.
58
59 kdev.setAttr<uint32_t>(KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0, it_lines);
60}
61
65
66void
68{
69 setIntState(KVM_ARM_IRQ_TYPE_SPI, 0, spi, true);
70}
71
72void
74{
75 setIntState(KVM_ARM_IRQ_TYPE_SPI, 0, spi, false);
76}
77
78void
79KvmKernelGic::setPPI(unsigned vcpu, unsigned ppi)
80{
81 setIntState(KVM_ARM_IRQ_TYPE_PPI, vcpu, ppi, true);
82}
83
84void
85KvmKernelGic::clearPPI(unsigned vcpu, unsigned ppi)
86{
87 setIntState(KVM_ARM_IRQ_TYPE_PPI, vcpu, ppi, false);
88}
89
90void
91KvmKernelGic::setIntState(unsigned type, unsigned vcpu, unsigned irq,
92 bool high)
93{
94 const unsigned vcpu_index = vcpu & 0xff;
95 const unsigned vcpu2_index = (vcpu >> 8) & 0xff;
96
97 static const bool vcpu2_enabled = vm.kvm->capIRQLineLayout2();
98 uint32_t kvm_vcpu = (vcpu_index << KVM_ARM_IRQ_VCPU_SHIFT);
99
100#if defined(KVM_ARM_IRQ_VCPU2_SHIFT)
101 if (vcpu2_enabled)
102 kvm_vcpu |= vcpu2_index << KVM_ARM_IRQ_VCPU2_SHIFT;
103#endif
104
105 panic_if((!vcpu2_enabled && vcpu2_index) || kvm_vcpu > 0xffff,
106 "VCPU out of range");
107
108 assert(type <= KVM_ARM_IRQ_TYPE_MASK);
109 assert(irq <= KVM_ARM_IRQ_NUM_MASK);
110 const uint32_t line(
111 kvm_vcpu |
112 (type << KVM_ARM_IRQ_TYPE_SHIFT) |
113 (irq << KVM_ARM_IRQ_NUM_SHIFT));
114
115 vm.setIRQLine(line, high);
116}
117
119 const MuxingKvmGicV2Params &p)
120 : KvmKernelGic(_vm, KVM_DEV_TYPE_ARM_VGIC_V2, p.it_lines),
121 cpuRange(RangeSize(p.cpu_addr, KVM_VGIC_V2_CPU_SIZE)),
122 distRange(RangeSize(p.dist_addr, KVM_VGIC_V2_DIST_SIZE))
123{
124 kdev.setAttr<uint64_t>(
125 KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V2_ADDR_TYPE_DIST, p.dist_addr);
126 kdev.setAttr<uint64_t>(
127 KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V2_ADDR_TYPE_CPU, p.cpu_addr);
128}
129
130uint32_t
131KvmKernelGicV2::getGicReg(unsigned group, unsigned vcpu, unsigned offset)
132{
133 uint64_t reg;
134
135 assert(vcpu <= KVM_ARM_IRQ_VCPU_MASK);
136 const uint64_t attr(
137 ((uint64_t)vcpu << KVM_DEV_ARM_VGIC_CPUID_SHIFT) |
138 (offset << KVM_DEV_ARM_VGIC_OFFSET_SHIFT));
139
140 kdev.getAttrPtr(group, attr, &reg);
141 return (uint32_t) reg;
142}
143
144void
145KvmKernelGicV2::setGicReg(unsigned group, unsigned vcpu, unsigned offset,
146 unsigned value)
147{
148 uint64_t reg = value;
149
150 assert(vcpu <= KVM_ARM_IRQ_VCPU_MASK);
151 const uint64_t attr(
152 ((uint64_t)vcpu << KVM_DEV_ARM_VGIC_CPUID_SHIFT) |
153 (offset << KVM_DEV_ARM_VGIC_OFFSET_SHIFT));
154
155 kdev.setAttrPtr(group, attr, &reg);
156}
157
158uint32_t
160{
161 auto vcpu = vm.contextIdToVCpuId(ctx);
162 return getGicReg(KVM_DEV_ARM_VGIC_GRP_DIST_REGS, vcpu, daddr);
163}
164
165uint32_t
167{
168 auto vcpu = vm.contextIdToVCpuId(ctx);
169 return getGicReg(KVM_DEV_ARM_VGIC_GRP_CPU_REGS, vcpu, daddr);
170}
171
172void
174{
175 auto vcpu = vm.contextIdToVCpuId(ctx);
176 setGicReg(KVM_DEV_ARM_VGIC_GRP_DIST_REGS, vcpu, daddr, data);
177}
178
179void
181{
182 auto vcpu = vm.contextIdToVCpuId(ctx);
183 setGicReg(KVM_DEV_ARM_VGIC_GRP_CPU_REGS, vcpu, daddr, data);
184}
185
186#ifndef SZ_64K
187#define SZ_64K 0x00000040
188#endif
189
191 const MuxingKvmGicV3Params &p)
192 : KvmKernelGic(_vm, KVM_DEV_TYPE_ARM_VGIC_V3, p.it_lines),
193 redistRange(RangeSize(p.redist_addr, KVM_VGIC_V3_REDIST_SIZE)),
194 distRange(RangeSize(p.dist_addr, KVM_VGIC_V3_DIST_SIZE))
195{
196 kdev.setAttr<uint64_t>(
197 KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V3_ADDR_TYPE_DIST, p.dist_addr);
198 kdev.setAttr<uint64_t>(
199 KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V3_ADDR_TYPE_REDIST, p.redist_addr);
200}
201
202void
204{
205 kdev.setAttr<uint64_t>(
206 KVM_DEV_ARM_VGIC_GRP_CTRL, KVM_DEV_ARM_VGIC_CTRL_INIT, 0);
207}
208
209template <typename Ret>
210Ret
211KvmKernelGicV3::getGicReg(unsigned group, unsigned mpidr, unsigned offset)
212{
213 Ret reg;
214
215 assert(mpidr <= KVM_DEV_ARM_VGIC_V3_MPIDR_MASK);
216 const uint64_t attr(
217 ((uint64_t)mpidr << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT) |
218 (offset << KVM_DEV_ARM_VGIC_OFFSET_SHIFT));
219
220 kdev.getAttrPtr(group, attr, &reg);
221 return reg;
222}
223
224template <typename Arg>
225void
226KvmKernelGicV3::setGicReg(unsigned group, unsigned mpidr, unsigned offset,
227 Arg value)
228{
229 Arg reg = value;
230
231 assert(mpidr <= KVM_DEV_ARM_VGIC_V3_MPIDR_MASK);
232 const uint64_t attr(
233 ((uint64_t)mpidr << KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT) |
234 (offset << KVM_DEV_ARM_VGIC_OFFSET_SHIFT));
235
236 kdev.setAttrPtr(group, attr, &reg);
237}
238
239uint32_t
241{
242 return getGicReg<uint32_t>(KVM_DEV_ARM_VGIC_GRP_DIST_REGS, 0, daddr);
243}
244
245uint32_t
246KvmKernelGicV3::readRedistributor(const ArmISA::Affinity &aff, Addr daddr)
247{
248 return getGicReg<uint32_t>(KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, aff, daddr);
249}
250
251RegVal
252KvmKernelGicV3::readCpu(const ArmISA::Affinity &aff,
253 ArmISA::MiscRegIndex misc_reg)
254{
255 std::optional<ArmISA::MiscRegNum64> sys_reg =
257 panic_if(!sys_reg.has_value(), "Invalid system register");
258 return getGicReg<RegVal>(KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS, aff,
259 sys_reg.value().packed());
260}
261
262void
264{
265 setGicReg<uint32_t>(KVM_DEV_ARM_VGIC_GRP_DIST_REGS, 0, daddr, data);
266}
267
268void
269KvmKernelGicV3::writeRedistributor(const ArmISA::Affinity &aff, Addr daddr,
270 uint32_t data)
271{
272 setGicReg<uint32_t>(KVM_DEV_ARM_VGIC_GRP_REDIST_REGS, aff, daddr, data);
273}
274
275void
276KvmKernelGicV3::writeCpu(const ArmISA::Affinity &aff,
277 ArmISA::MiscRegIndex misc_reg,
278 RegVal data)
279{
280 std::optional<ArmISA::MiscRegNum64> sys_reg =
282 panic_if(!sys_reg.has_value(), "Invalid system register");
283 setGicReg<RegVal>(KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS, aff,
284 sys_reg.value().packed(), data);
285}
286
287template <class Types>
289 : SimGic(p),
290 system(*p.system),
291 kernelGic(nullptr),
292 usingKvm(false)
293{
294 auto vm = system.getKvmVM();
295 if (vm && !p.simulate_gic) {
296 kernelGic = new KvmGic(*vm, p);
297 }
298}
299
300template <class Types>
301void
303{
304 SimGic::startup();
305
306 if (kernelGic) {
307 kernelGic->init();
308
309 KvmVM *vm = system.getKvmVM();
310 if (vm && vm->validEnvironment()) {
311 usingKvm = true;
312 fromGicToKvm();
313 }
314 }
315}
316
317template <class Types>
320{
321 if (usingKvm)
322 fromKvmToGic();
323 return SimGic::drain();
324}
325
326template <class Types>
327void
329{
330 SimGic::drainResume();
331
332 KvmVM *vm = system.getKvmVM();
333 bool use_kvm = kernelGic && vm && vm->validEnvironment();
334 if (use_kvm != usingKvm) {
335 // Should only occur due to CPU switches
336 if (use_kvm) // from simulation to KVM emulation
337 fromGicToKvm();
338 // otherwise, drain() already sync'd the state back to the GicV2
339
340 usingKvm = use_kvm;
341 }
342}
343
344template <class Types>
345Tick
347{
348 if (!usingKvm)
349 return SimGic::read(pkt);
350
351 panic("MuxingKvmGic: PIO from gem5 is currently unsupported\n");
352}
353
354template <class Types>
355Tick
357{
358 if (!usingKvm)
359 return SimGic::write(pkt);
360
361 panic("MuxingKvmGic: PIO from gem5 is currently unsupported\n");
362}
363
364template <class Types>
365void
367{
368 if (!usingKvm)
369 return SimGic::sendInt(num);
370
371 DPRINTF(Interrupt, "Set SPI %d\n", num);
372 kernelGic->setSPI(num);
373}
374
375template <class Types>
376void
378{
379 if (!usingKvm)
380 return SimGic::clearInt(num);
381
382 DPRINTF(Interrupt, "Clear SPI %d\n", num);
383 kernelGic->clearSPI(num);
384}
385
386template <class Types>
387void
388MuxingKvmGic<Types>::sendPPInt(uint32_t num, uint32_t cpu)
389{
390 if (!usingKvm)
391 return SimGic::sendPPInt(num, cpu);
392 DPRINTF(Interrupt, "Set PPI %d:%d\n", cpu, num);
393 kernelGic->setPPI(cpu, num);
394}
395
396template <class Types>
397void
398MuxingKvmGic<Types>::clearPPInt(uint32_t num, uint32_t cpu)
399{
400 if (!usingKvm)
401 return SimGic::clearPPInt(num, cpu);
402
403 DPRINTF(Interrupt, "Clear PPI %d:%d\n", cpu, num);
404 kernelGic->clearPPI(cpu, num);
405}
406
407template <class Types>
408bool
410{
411 // During Kvm->Gic state transfer, writes to the Gic will call
412 // updateIntState() which can post an interrupt. Since we're only
413 // using the Gic model for holding state in this circumstance, we
414 // short-circuit this behavior, as the GicV2 is not actually active.
415 return usingKvm;
416}
417
418template <class Types>
419void
421{
422 this->copyGicState(static_cast<SimGic*>(this),
423 static_cast<KvmGic*>(kernelGic));
424}
425
426template <class Types>
427void
429{
430 this->copyGicState(static_cast<KvmGic*>(kernelGic),
431 static_cast<SimGic*>(this));
432
433 // the values read for the Interrupt Priority Mask Register (PMR)
434 // have been shifted by three bits due to its having been emulated by
435 // a VGIC with only 5 PMR bits in its VMCR register. Presently the
436 // Linux kernel does not repair this inaccuracy, so we correct it here.
437 if constexpr(std::is_same<SimGic, GicV2>::value) {
438 for (int cpu = 0; cpu < system.threads.size(); ++cpu) {
439 this->cpuPriority[cpu] <<= 3;
440 assert((this->cpuPriority[cpu] & ~0xff) == 0);
441 }
442 }
443}
444
445template class MuxingKvmGic<GicV2Types>;
446template class MuxingKvmGic<GicV3Types>;
447
448} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
const char data[]
void getAttrPtr(uint32_t group, uint64_t attr, void *data) const
Definition device.cc:63
void setAttrPtr(uint32_t group, uint64_t attr, const void *data) const
Definition device.cc:84
void setAttr(uint32_t group, uint64_t attr, const T &data) const
Get the value of an attribute.
Definition device.hh:95
uint32_t readCpu(ContextID ctx, Addr daddr) override
Definition gic.cc:166
void setGicReg(unsigned group, unsigned vcpu, unsigned offset, unsigned value)
Set value of GIC register "from" a cpu.
Definition gic.cc:145
void writeCpu(ContextID ctx, Addr daddr, uint32_t data) override
Definition gic.cc:180
uint32_t readDistributor(ContextID ctx, Addr daddr) override
Definition gic.cc:159
uint32_t getGicReg(unsigned group, unsigned vcpu, unsigned offset)
Get value of GIC register "from" a cpu.
Definition gic.cc:131
void writeDistributor(ContextID ctx, Addr daddr, uint32_t data) override
Definition gic.cc:173
KvmKernelGicV2(KvmVM &vm, const MuxingKvmGicV2Params &params)
Instantiate a KVM in-kernel GICv2 model.
Definition gic.cc:118
RegVal readCpu(const ArmISA::Affinity &aff, ArmISA::MiscRegIndex misc_reg) override
Definition gic.cc:252
void writeRedistributor(const ArmISA::Affinity &aff, Addr daddr, uint32_t data) override
Definition gic.cc:269
void init() override
Definition gic.cc:203
void writeDistributor(Addr daddr, uint32_t data) override
Definition gic.cc:263
Ret getGicReg(unsigned group, unsigned mpidr, unsigned offset)
Get value of GIC register "from" a cpu.
Definition gic.cc:211
uint32_t readRedistributor(const ArmISA::Affinity &aff, Addr daddr) override
Definition gic.cc:246
void writeCpu(const ArmISA::Affinity &aff, ArmISA::MiscRegIndex misc_reg, RegVal data) override
Definition gic.cc:276
uint32_t readDistributor(Addr daddr) override
Definition gic.cc:240
void setGicReg(unsigned group, unsigned mpidr, unsigned offset, Arg value)
Set value of GIC register "from" a cpu.
Definition gic.cc:226
KvmKernelGicV3(KvmVM &vm, const MuxingKvmGicV3Params &params)
Instantiate a KVM in-kernel GICv3 model.
Definition gic.cc:190
KVM in-kernel GIC abstraction.
Definition gic.hh:62
void setPPI(unsigned vcpu, unsigned ppi)
Raise a private peripheral interrupt.
Definition gic.cc:79
void setSPI(unsigned spi)
Raise a shared peripheral interrupt.
Definition gic.cc:67
KvmKernelGic(KvmVM &vm, uint32_t dev, unsigned it_lines)
Instantiate a KVM in-kernel GIC model.
Definition gic.cc:51
void clearPPI(unsigned vcpu, unsigned ppi)
Clear a private peripheral interrupt.
Definition gic.cc:85
KvmDevice kdev
Kernel interface to the GIC.
Definition gic.hh:137
void setIntState(unsigned type, unsigned vcpu, unsigned irq, bool high)
Update the kernel's VGIC interrupt state.
Definition gic.cc:91
void clearSPI(unsigned spi)
Clear a shared peripheral interrupt.
Definition gic.cc:73
KvmVM & vm
KVM VM in the parent system.
Definition gic.hh:134
virtual ~KvmKernelGic()
Definition gic.cc:62
KVM VM container.
Definition vm.hh:302
long contextIdToVCpuId(ContextID ctx) const
Get the VCPUID for a given context.
Definition vm.cc:566
bool validEnvironment() const
Verify gem5 configuration will support KVM emulation.
Definition vm.cc:555
Kvm * kvm
Global KVM interface.
Definition vm.hh:421
bool capIRQLineLayout2() const
Support for ARM IRQ line layout 2.
Definition vm.cc:208
System & system
System this interrupt controller belongs to.
Definition gic.hh:295
void sendInt(uint32_t num) override
Definition gic.cc:366
Tick write(PacketPtr pkt) override
Definition gic.cc:356
KvmKernelGic * kernelGic
Kernel GIC device.
Definition gic.hh:298
MuxingKvmGic(const Params &p)
Definition gic.cc:288
typename Types::KvmGic KvmGic
Definition gic.hh:269
bool blockIntUpdate() const override
Definition gic.cc:409
void fromKvmToGic()
Definition gic.cc:428
void startup() override
Definition gic.cc:302
Tick read(PacketPtr pkt) override
Definition gic.cc:346
DrainState drain() override
Definition gic.cc:319
void clearInt(uint32_t num) override
Definition gic.cc:377
void clearPPInt(uint32_t num, uint32_t cpu) override
Definition gic.cc:398
void drainResume() override
Definition gic.cc:328
typename Types::Params Params
Definition gic.hh:270
typename Types::SimGic SimGic
Definition gic.hh:268
void fromGicToKvm()
Multiplexing implementation.
Definition gic.cc:420
void sendPPInt(uint32_t num, uint32_t cpu) override
Definition gic.cc:388
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition packet.hh:295
KvmVM * getKvmVM() const
Get a pointer to the Kernel Virtual Machine (KVM) SimObject, if present.
Definition system.hh:333
void setIRQLine(uint32_t irq, bool high)
Set the status of an IRQ line using KVM_IRQ_LINE.
Definition vm.cc:525
void enableKernelIRQChip()
Tell the VM and VCPUs to use an in-kernel IRQ chip for interrupt delivery.
Definition vm.hh:372
AddrRange RangeSize(Addr start, Addr size)
DrainState
Object drain/handover states.
Definition drain.hh:75
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
Bitfield< 23, 0 > offset
Definition types.hh:144
Bitfield< 0 > vm
Bitfield< 1 > irq
std::optional< MiscRegNum64 > encodeAArch64SysReg(MiscRegIndex misc_reg)
Definition misc.cc:2775
Bitfield< 0 > p
Bitfield< 5, 3 > reg
Definition types.hh:92
Bitfield< 15 > system
Definition misc.hh:1032
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t RegVal
Definition types.hh:173
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
uint64_t Tick
Tick count type.
Definition types.hh:58
int ContextID
Globally unique thread context ID.
Definition types.hh:239

Generated on Tue Jun 18 2024 16:23:56 for gem5 by doxygen 1.11.0