2 * Copyright (c) 2015-2017 ARM Limited
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.
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.
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.
37 * Authors: Andreas Sandberg
41 #include "arch/arm/kvm/gic.hh"
43 #include <linux/kvm.h>
45 #include "arch/arm/kvm/base_cpu.hh"
46 #include "debug/Interrupt.hh"
47 #include "params/KvmGic.hh"
48 #include "params/MuxingKvmGic.hh"
50 KvmKernelGicV2::KvmKernelGicV2(KvmVM
&_vm
, Addr cpu_addr
, Addr dist_addr
,
52 : cpuRange(RangeSize(cpu_addr
, KVM_VGIC_V2_CPU_SIZE
)),
53 distRange(RangeSize(dist_addr
, KVM_VGIC_V2_DIST_SIZE
)),
55 kdev(vm
.createDevice(KVM_DEV_TYPE_ARM_VGIC_V2
))
57 kdev
.setAttr
<uint64_t>(
58 KVM_DEV_ARM_VGIC_GRP_ADDR
, KVM_VGIC_V2_ADDR_TYPE_DIST
, dist_addr
);
59 kdev
.setAttr
<uint64_t>(
60 KVM_DEV_ARM_VGIC_GRP_ADDR
, KVM_VGIC_V2_ADDR_TYPE_CPU
, cpu_addr
);
62 kdev
.setAttr
<uint32_t>(KVM_DEV_ARM_VGIC_GRP_NR_IRQS
, 0, it_lines
);
65 KvmKernelGicV2::~KvmKernelGicV2()
70 KvmKernelGicV2::setSPI(unsigned spi
)
72 setIntState(KVM_ARM_IRQ_TYPE_SPI
, 0, spi
, true);
76 KvmKernelGicV2::clearSPI(unsigned spi
)
78 setIntState(KVM_ARM_IRQ_TYPE_SPI
, 0, spi
, false);
82 KvmKernelGicV2::setPPI(unsigned vcpu
, unsigned ppi
)
84 setIntState(KVM_ARM_IRQ_TYPE_PPI
, vcpu
, ppi
, true);
88 KvmKernelGicV2::clearPPI(unsigned vcpu
, unsigned ppi
)
90 setIntState(KVM_ARM_IRQ_TYPE_PPI
, vcpu
, ppi
, false);
94 KvmKernelGicV2::setIntState(unsigned type
, unsigned vcpu
, unsigned irq
,
97 assert(type
<= KVM_ARM_IRQ_TYPE_MASK
);
98 assert(vcpu
<= KVM_ARM_IRQ_VCPU_MASK
);
99 assert(irq
<= KVM_ARM_IRQ_NUM_MASK
);
101 (type
<< KVM_ARM_IRQ_TYPE_SHIFT
) |
102 (vcpu
<< KVM_ARM_IRQ_VCPU_SHIFT
) |
103 (irq
<< KVM_ARM_IRQ_NUM_SHIFT
));
105 vm
.setIRQLine(line
, high
);
109 KvmGic::KvmGic(const KvmGicParams
*p
)
112 kernelGic(*system
.getKvmVM(),
113 p
->cpu_addr
, p
->dist_addr
, p
->it_lines
),
114 addrRanges
{kernelGic
.distRange
, kernelGic
.cpuRange
}
123 KvmGic::serialize(CheckpointOut
&cp
) const
125 panic("Checkpointing unsupported\n");
129 KvmGic::unserialize(CheckpointIn
&cp
)
131 panic("Checkpointing unsupported\n");
135 KvmGic::read(PacketPtr pkt
)
137 panic("KvmGic: PIO from gem5 is currently unsupported\n");
141 KvmGic::write(PacketPtr pkt
)
143 panic("KvmGic: PIO from gem5 is currently unsupported\n");
147 KvmGic::sendInt(uint32_t num
)
149 DPRINTF(Interrupt
, "Set SPI %d\n", num
);
150 kernelGic
.setSPI(num
);
154 KvmGic::clearInt(uint32_t num
)
156 DPRINTF(Interrupt
, "Clear SPI %d\n", num
);
157 kernelGic
.clearSPI(num
);
161 KvmGic::sendPPInt(uint32_t num
, uint32_t cpu
)
163 DPRINTF(Interrupt
, "Set PPI %d:%d\n", cpu
, num
);
164 kernelGic
.setPPI(cpu
, num
);
168 KvmGic::clearPPInt(uint32_t num
, uint32_t cpu
)
170 DPRINTF(Interrupt
, "Clear PPI %d:%d\n", cpu
, num
);
171 kernelGic
.clearPPI(cpu
, num
);
175 KvmGic::verifyMemoryMode() const
177 if (!(system
.isAtomicMode() && system
.bypassCaches())) {
178 fatal("The in-kernel KVM GIC can only be used with KVM CPUs, but the "
179 "current memory mode does not support KVM.\n");
185 KvmGicParams::create()
187 return new KvmGic(this);
191 MuxingKvmGic::MuxingKvmGic(const MuxingKvmGicParams
*p
)
197 if (auto vm
= system
.getKvmVM()) {
198 kernelGic
= new KvmKernelGicV2(*vm
, p
->cpu_addr
, p
->dist_addr
,
203 MuxingKvmGic::~MuxingKvmGic()
208 MuxingKvmGic::startup()
210 usingKvm
= (kernelGic
!= nullptr) && validKvmEnvironment();
214 MuxingKvmGic::drainResume()
216 bool use_kvm
= (kernelGic
!= nullptr) && validKvmEnvironment();
217 if (use_kvm
!= usingKvm
) {
218 if (use_kvm
) // from simulation to KVM emulation
220 else // from KVM emulation to simulation
228 MuxingKvmGic::serialize(CheckpointOut
&cp
) const
231 return Pl390::serialize(cp
);
233 panic("Checkpointing unsupported\n");
237 MuxingKvmGic::unserialize(CheckpointIn
&cp
)
240 return Pl390::unserialize(cp
);
242 panic("Checkpointing unsupported\n");
246 MuxingKvmGic::read(PacketPtr pkt
)
249 return Pl390::read(pkt
);
251 panic("MuxingKvmGic: PIO from gem5 is currently unsupported\n");
255 MuxingKvmGic::write(PacketPtr pkt
)
258 return Pl390::write(pkt
);
260 panic("MuxingKvmGic: PIO from gem5 is currently unsupported\n");
264 MuxingKvmGic::sendInt(uint32_t num
)
267 return Pl390::sendInt(num
);
269 DPRINTF(Interrupt
, "Set SPI %d\n", num
);
270 kernelGic
->setSPI(num
);
274 MuxingKvmGic::clearInt(uint32_t num
)
277 return Pl390::clearInt(num
);
279 DPRINTF(Interrupt
, "Clear SPI %d\n", num
);
280 kernelGic
->clearSPI(num
);
284 MuxingKvmGic::sendPPInt(uint32_t num
, uint32_t cpu
)
287 return Pl390::sendPPInt(num
, cpu
);
288 DPRINTF(Interrupt
, "Set PPI %d:%d\n", cpu
, num
);
289 kernelGic
->setPPI(cpu
, num
);
293 MuxingKvmGic::clearPPInt(uint32_t num
, uint32_t cpu
)
296 return Pl390::clearPPInt(num
, cpu
);
298 DPRINTF(Interrupt
, "Clear PPI %d:%d\n", cpu
, num
);
299 kernelGic
->clearPPI(cpu
, num
);
303 MuxingKvmGic::validKvmEnvironment() const
305 if (system
.threadContexts
.empty())
308 for (auto tc
: system
.threadContexts
) {
309 if (dynamic_cast<BaseArmKvmCPU
*>(tc
->getCpuPtr()) == nullptr) {
317 MuxingKvmGic::fromPl390ToKvm()
319 panic("Gic multiplexing not implemented.\n");
323 MuxingKvmGic::fromKvmToPl390()
325 panic("Gic multiplexing not implemented.\n");
329 MuxingKvmGicParams::create()
331 return new MuxingKvmGic(this);