2013-03-05 08:34:43 +08:00
|
|
|
/*
|
|
|
|
* ARM Generic Interrupt Controller using KVM in-kernel support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Linaro Limited
|
|
|
|
* Written by Peter Maydell
|
2014-02-27 01:20:01 +08:00
|
|
|
* Save/Restore logic added by Christoffer Dall.
|
2013-03-05 08:34:43 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-27 02:17:05 +08:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2016-03-15 23:58:45 +08:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "cpu.h"
|
2013-03-05 08:34:43 +08:00
|
|
|
#include "hw/sysbus.h"
|
2017-04-06 18:00:28 +08:00
|
|
|
#include "migration/blocker.h"
|
2013-03-05 08:34:43 +08:00
|
|
|
#include "sysemu/kvm.h"
|
|
|
|
#include "kvm_arm.h"
|
2013-03-19 00:36:02 +08:00
|
|
|
#include "gic_internal.h"
|
2015-09-24 08:29:36 +08:00
|
|
|
#include "vgic_common.h"
|
2013-03-05 08:34:43 +08:00
|
|
|
|
|
|
|
#define TYPE_KVM_ARM_GIC "kvm-arm-gic"
|
|
|
|
#define KVM_ARM_GIC(obj) \
|
|
|
|
OBJECT_CHECK(GICState, (obj), TYPE_KVM_ARM_GIC)
|
|
|
|
#define KVM_ARM_GIC_CLASS(klass) \
|
|
|
|
OBJECT_CLASS_CHECK(KVMARMGICClass, (klass), TYPE_KVM_ARM_GIC)
|
|
|
|
#define KVM_ARM_GIC_GET_CLASS(obj) \
|
|
|
|
OBJECT_GET_CLASS(KVMARMGICClass, (obj), TYPE_KVM_ARM_GIC)
|
|
|
|
|
|
|
|
typedef struct KVMARMGICClass {
|
|
|
|
ARMGICCommonClass parent_class;
|
|
|
|
DeviceRealize parent_realize;
|
|
|
|
void (*parent_reset)(DeviceState *dev);
|
|
|
|
} KVMARMGICClass;
|
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
void kvm_arm_gic_set_irq(uint32_t num_irq, int irq, int level)
|
2013-03-05 08:34:43 +08:00
|
|
|
{
|
|
|
|
/* Meaning of the 'irq' parameter:
|
|
|
|
* [0..N-1] : external interrupts
|
|
|
|
* [N..N+31] : PPI (internal) interrupts for CPU 0
|
|
|
|
* [N+32..N+63] : PPI (internal interrupts for CPU 1
|
|
|
|
* ...
|
|
|
|
* Convert this to the kernel's desired encoding, which
|
|
|
|
* has separate fields in the irq number for type,
|
|
|
|
* CPU number and interrupt number.
|
|
|
|
*/
|
|
|
|
int kvm_irq, irqtype, cpu;
|
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
if (irq < (num_irq - GIC_INTERNAL)) {
|
2013-03-05 08:34:43 +08:00
|
|
|
/* External interrupt. The kernel numbers these like the GIC
|
|
|
|
* hardware, with external interrupt IDs starting after the
|
|
|
|
* internal ones.
|
|
|
|
*/
|
|
|
|
irqtype = KVM_ARM_IRQ_TYPE_SPI;
|
|
|
|
cpu = 0;
|
|
|
|
irq += GIC_INTERNAL;
|
|
|
|
} else {
|
|
|
|
/* Internal interrupt: decode into (cpu, interrupt id) */
|
|
|
|
irqtype = KVM_ARM_IRQ_TYPE_PPI;
|
2015-09-24 08:29:36 +08:00
|
|
|
irq -= (num_irq - GIC_INTERNAL);
|
2013-03-05 08:34:43 +08:00
|
|
|
cpu = irq / GIC_INTERNAL;
|
|
|
|
irq %= GIC_INTERNAL;
|
|
|
|
}
|
|
|
|
kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT)
|
|
|
|
| (cpu << KVM_ARM_IRQ_VCPU_SHIFT) | irq;
|
|
|
|
|
|
|
|
kvm_set_irq(kvm_state, kvm_irq, !!level);
|
|
|
|
}
|
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
static void kvm_arm_gicv2_set_irq(void *opaque, int irq, int level)
|
2014-12-11 20:07:53 +08:00
|
|
|
{
|
2015-09-24 08:29:36 +08:00
|
|
|
GICState *s = (GICState *)opaque;
|
2014-12-11 20:07:53 +08:00
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
kvm_arm_gic_set_irq(s->num_irq, irq, level);
|
2014-12-11 20:07:53 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
static bool kvm_arm_gic_can_save_restore(GICState *s)
|
2014-02-27 01:20:01 +08:00
|
|
|
{
|
2015-09-24 08:29:36 +08:00
|
|
|
return s->dev_fd >= 0;
|
2014-02-27 01:20:01 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
#define KVM_VGIC_ATTR(offset, cpu) \
|
|
|
|
((((uint64_t)(cpu) << KVM_DEV_ARM_VGIC_CPUID_SHIFT) & \
|
|
|
|
KVM_DEV_ARM_VGIC_CPUID_MASK) | \
|
|
|
|
(((uint64_t)(offset) << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) & \
|
|
|
|
KVM_DEV_ARM_VGIC_OFFSET_MASK))
|
|
|
|
|
2014-02-27 01:20:01 +08:00
|
|
|
static void kvm_gicd_access(GICState *s, int offset, int cpu,
|
|
|
|
uint32_t *val, bool write)
|
|
|
|
{
|
2015-09-24 08:29:36 +08:00
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
|
2017-06-13 21:57:00 +08:00
|
|
|
KVM_VGIC_ATTR(offset, cpu), val, write, &error_abort);
|
2014-02-27 01:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_gicc_access(GICState *s, int offset, int cpu,
|
|
|
|
uint32_t *val, bool write)
|
|
|
|
{
|
2015-09-24 08:29:36 +08:00
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
|
2017-06-13 21:57:00 +08:00
|
|
|
KVM_VGIC_ATTR(offset, cpu), val, write, &error_abort);
|
2014-02-27 01:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define for_each_irq_reg(_ctr, _max_irq, _field_width) \
|
|
|
|
for (_ctr = 0; _ctr < ((_max_irq) / (32 / (_field_width))); _ctr++)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate from the in-kernel field for an IRQ value to/from the qemu
|
|
|
|
* representation.
|
|
|
|
*/
|
|
|
|
typedef void (*vgic_translate_fn)(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel);
|
|
|
|
|
|
|
|
/* synthetic translate function used for clear/set registers to completely
|
2014-03-08 02:48:59 +08:00
|
|
|
* clear a setting using a clear-register before setting the remaining bits
|
2014-02-27 01:20:01 +08:00
|
|
|
* using a set-register */
|
|
|
|
static void translate_clear(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = ~0;
|
|
|
|
} else {
|
|
|
|
/* does not make sense: qemu model doesn't use set/clear regs */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:57:17 +08:00
|
|
|
static void translate_group(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
|
|
|
|
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = GIC_TEST_GROUP(irq, cm);
|
|
|
|
} else {
|
|
|
|
if (*field & 1) {
|
|
|
|
GIC_SET_GROUP(irq, cm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 01:20:01 +08:00
|
|
|
static void translate_enabled(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
|
|
|
|
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = GIC_TEST_ENABLED(irq, cm);
|
|
|
|
} else {
|
|
|
|
if (*field & 1) {
|
|
|
|
GIC_SET_ENABLED(irq, cm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void translate_pending(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
|
|
|
|
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = gic_test_pending(s, irq, cm);
|
|
|
|
} else {
|
|
|
|
if (*field & 1) {
|
|
|
|
GIC_SET_PENDING(irq, cm);
|
|
|
|
/* TODO: Capture is level-line is held high in the kernel */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void translate_active(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
|
|
|
|
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = GIC_TEST_ACTIVE(irq, cm);
|
|
|
|
} else {
|
|
|
|
if (*field & 1) {
|
|
|
|
GIC_SET_ACTIVE(irq, cm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void translate_trigger(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = (GIC_TEST_EDGE_TRIGGER(irq)) ? 0x2 : 0x0;
|
|
|
|
} else {
|
|
|
|
if (*field & 0x2) {
|
|
|
|
GIC_SET_EDGE_TRIGGER(irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void translate_priority(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = GIC_GET_PRIORITY(irq, cpu) & 0xff;
|
|
|
|
} else {
|
2015-05-12 18:57:17 +08:00
|
|
|
gic_set_priority(s, cpu, irq, *field & 0xff, MEMTXATTRS_UNSPECIFIED);
|
2014-02-27 01:20:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void translate_targets(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = s->irq_target[irq] & 0xff;
|
|
|
|
} else {
|
|
|
|
s->irq_target[irq] = *field & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void translate_sgisource(GICState *s, int irq, int cpu,
|
|
|
|
uint32_t *field, bool to_kernel)
|
|
|
|
{
|
|
|
|
if (to_kernel) {
|
|
|
|
*field = s->sgi_pending[irq][cpu] & 0xff;
|
|
|
|
} else {
|
|
|
|
s->sgi_pending[irq][cpu] = *field & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a register group from the kernel VGIC */
|
|
|
|
static void kvm_dist_get(GICState *s, uint32_t offset, int width,
|
|
|
|
int maxirq, vgic_translate_fn translate_fn)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int irq;
|
|
|
|
int cpu;
|
|
|
|
int regsz = 32 / width; /* irqs per kernel register */
|
|
|
|
uint32_t field;
|
|
|
|
|
|
|
|
for_each_irq_reg(i, maxirq, width) {
|
|
|
|
irq = i * regsz;
|
|
|
|
cpu = 0;
|
|
|
|
while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) {
|
|
|
|
kvm_gicd_access(s, offset, cpu, ®, false);
|
|
|
|
for (j = 0; j < regsz; j++) {
|
|
|
|
field = extract32(reg, j * width, width);
|
|
|
|
translate_fn(s, irq + j, cpu, &field, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu++;
|
|
|
|
}
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a register group to the kernel VGIC */
|
|
|
|
static void kvm_dist_put(GICState *s, uint32_t offset, int width,
|
|
|
|
int maxirq, vgic_translate_fn translate_fn)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int irq;
|
|
|
|
int cpu;
|
|
|
|
int regsz = 32 / width; /* irqs per kernel register */
|
|
|
|
uint32_t field;
|
|
|
|
|
|
|
|
for_each_irq_reg(i, maxirq, width) {
|
|
|
|
irq = i * regsz;
|
|
|
|
cpu = 0;
|
|
|
|
while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) {
|
|
|
|
reg = 0;
|
|
|
|
for (j = 0; j < regsz; j++) {
|
|
|
|
translate_fn(s, irq + j, cpu, &field, true);
|
|
|
|
reg = deposit32(reg, j * width, width, field);
|
|
|
|
}
|
|
|
|
kvm_gicd_access(s, offset, cpu, ®, true);
|
|
|
|
|
|
|
|
cpu++;
|
|
|
|
}
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 08:34:43 +08:00
|
|
|
static void kvm_arm_gic_put(GICState *s)
|
|
|
|
{
|
2014-02-27 01:20:01 +08:00
|
|
|
uint32_t reg;
|
|
|
|
int i;
|
|
|
|
int cpu;
|
|
|
|
int num_cpu;
|
|
|
|
int num_irq;
|
|
|
|
|
|
|
|
/* Note: We do the restore in a slightly different order than the save
|
|
|
|
* (where the order doesn't matter and is simply ordered according to the
|
|
|
|
* register offset values */
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* Distributor State
|
|
|
|
*/
|
|
|
|
|
2015-05-12 18:57:17 +08:00
|
|
|
/* s->ctlr -> GICD_CTLR */
|
|
|
|
reg = s->ctlr;
|
2014-02-27 01:20:01 +08:00
|
|
|
kvm_gicd_access(s, 0x0, 0, ®, true);
|
|
|
|
|
|
|
|
/* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
|
|
|
|
kvm_gicd_access(s, 0x4, 0, ®, false);
|
|
|
|
num_irq = ((reg & 0x1f) + 1) * 32;
|
|
|
|
num_cpu = ((reg & 0xe0) >> 5) + 1;
|
|
|
|
|
|
|
|
if (num_irq < s->num_irq) {
|
|
|
|
fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
|
|
|
|
s->num_irq, num_irq);
|
|
|
|
abort();
|
|
|
|
} else if (num_cpu != s->num_cpu) {
|
|
|
|
fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
|
|
|
|
s->num_cpu, num_cpu);
|
|
|
|
/* Did we not create the VCPUs in the kernel yet? */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Consider checking compatibility with the IIDR ? */
|
|
|
|
|
|
|
|
/* irq_state[n].enabled -> GICD_ISENABLERn */
|
|
|
|
kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
|
|
|
|
kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
|
|
|
|
|
2015-05-12 18:57:17 +08:00
|
|
|
/* irq_state[n].group -> GICD_IGROUPRn */
|
|
|
|
kvm_dist_put(s, 0x80, 1, s->num_irq, translate_group);
|
|
|
|
|
2014-02-27 01:20:01 +08:00
|
|
|
/* s->irq_target[irq] -> GICD_ITARGETSRn
|
|
|
|
* (restore targets before pending to ensure the pending state is set on
|
|
|
|
* the appropriate CPU interfaces in the kernel) */
|
|
|
|
kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
|
|
|
|
|
2015-04-02 00:57:30 +08:00
|
|
|
/* irq_state[n].trigger -> GICD_ICFGRn
|
|
|
|
* (restore configuration registers before pending IRQs so we treat
|
|
|
|
* level/edge correctly) */
|
|
|
|
kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
|
|
|
|
|
2014-02-27 01:20:01 +08:00
|
|
|
/* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
|
|
|
|
kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
|
|
|
|
kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
|
|
|
|
|
|
|
|
/* irq_state[n].active -> GICD_ISACTIVERn */
|
|
|
|
kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
|
|
|
|
kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);
|
|
|
|
|
|
|
|
|
|
|
|
/* s->priorityX[irq] -> ICD_IPRIORITYRn */
|
|
|
|
kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
|
|
|
|
|
|
|
|
/* s->sgi_pending -> ICD_CPENDSGIRn */
|
|
|
|
kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
|
|
|
|
kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* CPU Interface(s) State
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (cpu = 0; cpu < s->num_cpu; cpu++) {
|
2015-05-12 18:57:17 +08:00
|
|
|
/* s->cpu_ctlr[cpu] -> GICC_CTLR */
|
|
|
|
reg = s->cpu_ctlr[cpu];
|
2014-02-27 01:20:01 +08:00
|
|
|
kvm_gicc_access(s, 0x00, cpu, ®, true);
|
|
|
|
|
|
|
|
/* s->priority_mask[cpu] -> GICC_PMR */
|
|
|
|
reg = (s->priority_mask[cpu] & 0xff);
|
|
|
|
kvm_gicc_access(s, 0x04, cpu, ®, true);
|
|
|
|
|
|
|
|
/* s->bpr[cpu] -> GICC_BPR */
|
|
|
|
reg = (s->bpr[cpu] & 0x7);
|
|
|
|
kvm_gicc_access(s, 0x08, cpu, ®, true);
|
|
|
|
|
|
|
|
/* s->abpr[cpu] -> GICC_ABPR */
|
|
|
|
reg = (s->abpr[cpu] & 0x7);
|
|
|
|
kvm_gicc_access(s, 0x1c, cpu, ®, true);
|
|
|
|
|
|
|
|
/* s->apr[n][cpu] -> GICC_APRn */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
reg = s->apr[i][cpu];
|
|
|
|
kvm_gicc_access(s, 0xd0 + i * 4, cpu, ®, true);
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 08:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_arm_gic_get(GICState *s)
|
|
|
|
{
|
2014-02-27 01:20:01 +08:00
|
|
|
uint32_t reg;
|
|
|
|
int i;
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* Distributor State
|
|
|
|
*/
|
|
|
|
|
2015-05-12 18:57:17 +08:00
|
|
|
/* GICD_CTLR -> s->ctlr */
|
2014-02-27 01:20:01 +08:00
|
|
|
kvm_gicd_access(s, 0x0, 0, ®, false);
|
2015-05-12 18:57:17 +08:00
|
|
|
s->ctlr = reg;
|
2014-02-27 01:20:01 +08:00
|
|
|
|
|
|
|
/* Sanity checking on GICD_TYPER -> s->num_irq, s->num_cpu */
|
|
|
|
kvm_gicd_access(s, 0x4, 0, ®, false);
|
|
|
|
s->num_irq = ((reg & 0x1f) + 1) * 32;
|
|
|
|
s->num_cpu = ((reg & 0xe0) >> 5) + 1;
|
|
|
|
|
|
|
|
if (s->num_irq > GIC_MAXIRQ) {
|
|
|
|
fprintf(stderr, "Too many IRQs reported from the kernel: %d\n",
|
|
|
|
s->num_irq);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GICD_IIDR -> ? */
|
|
|
|
kvm_gicd_access(s, 0x8, 0, ®, false);
|
|
|
|
|
|
|
|
/* Clear all the IRQ settings */
|
|
|
|
for (i = 0; i < s->num_irq; i++) {
|
|
|
|
memset(&s->irq_state[i], 0, sizeof(s->irq_state[0]));
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:57:17 +08:00
|
|
|
/* GICD_IGROUPRn -> irq_state[n].group */
|
|
|
|
kvm_dist_get(s, 0x80, 1, s->num_irq, translate_group);
|
|
|
|
|
2014-02-27 01:20:01 +08:00
|
|
|
/* GICD_ISENABLERn -> irq_state[n].enabled */
|
|
|
|
kvm_dist_get(s, 0x100, 1, s->num_irq, translate_enabled);
|
|
|
|
|
|
|
|
/* GICD_ISPENDRn -> irq_state[n].pending + irq_state[n].level */
|
|
|
|
kvm_dist_get(s, 0x200, 1, s->num_irq, translate_pending);
|
|
|
|
|
|
|
|
/* GICD_ISACTIVERn -> irq_state[n].active */
|
|
|
|
kvm_dist_get(s, 0x300, 1, s->num_irq, translate_active);
|
|
|
|
|
|
|
|
/* GICD_ICFRn -> irq_state[n].trigger */
|
|
|
|
kvm_dist_get(s, 0xc00, 2, s->num_irq, translate_trigger);
|
|
|
|
|
|
|
|
/* GICD_IPRIORITYRn -> s->priorityX[irq] */
|
|
|
|
kvm_dist_get(s, 0x400, 8, s->num_irq, translate_priority);
|
|
|
|
|
|
|
|
/* GICD_ITARGETSRn -> s->irq_target[irq] */
|
|
|
|
kvm_dist_get(s, 0x800, 8, s->num_irq, translate_targets);
|
|
|
|
|
|
|
|
/* GICD_CPENDSGIRn -> s->sgi_pending */
|
|
|
|
kvm_dist_get(s, 0xf10, 8, GIC_NR_SGIS, translate_sgisource);
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
* CPU Interface(s) State
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (cpu = 0; cpu < s->num_cpu; cpu++) {
|
2015-05-12 18:57:17 +08:00
|
|
|
/* GICC_CTLR -> s->cpu_ctlr[cpu] */
|
2014-02-27 01:20:01 +08:00
|
|
|
kvm_gicc_access(s, 0x00, cpu, ®, false);
|
2015-05-12 18:57:17 +08:00
|
|
|
s->cpu_ctlr[cpu] = reg;
|
2014-02-27 01:20:01 +08:00
|
|
|
|
|
|
|
/* GICC_PMR -> s->priority_mask[cpu] */
|
|
|
|
kvm_gicc_access(s, 0x04, cpu, ®, false);
|
|
|
|
s->priority_mask[cpu] = (reg & 0xff);
|
|
|
|
|
|
|
|
/* GICC_BPR -> s->bpr[cpu] */
|
|
|
|
kvm_gicc_access(s, 0x08, cpu, ®, false);
|
|
|
|
s->bpr[cpu] = (reg & 0x7);
|
|
|
|
|
|
|
|
/* GICC_ABPR -> s->abpr[cpu] */
|
|
|
|
kvm_gicc_access(s, 0x1c, cpu, ®, false);
|
|
|
|
s->abpr[cpu] = (reg & 0x7);
|
|
|
|
|
|
|
|
/* GICC_APRn -> s->apr[n][cpu] */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
kvm_gicc_access(s, 0xd0 + i * 4, cpu, ®, false);
|
|
|
|
s->apr[i][cpu] = reg;
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 08:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_arm_gic_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
GICState *s = ARM_GIC_COMMON(dev);
|
|
|
|
KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
|
|
|
|
|
|
|
|
kgc->parent_reset(dev);
|
2015-10-27 20:00:50 +08:00
|
|
|
|
|
|
|
if (kvm_arm_gic_can_save_restore(s)) {
|
|
|
|
kvm_arm_gic_put(s);
|
|
|
|
}
|
2013-03-05 08:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GICState *s = KVM_ARM_GIC(dev);
|
|
|
|
KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
|
2014-04-25 18:44:23 +08:00
|
|
|
Error *local_err = NULL;
|
2014-02-27 01:20:00 +08:00
|
|
|
int ret;
|
2013-03-05 08:34:43 +08:00
|
|
|
|
2014-04-25 18:44:23 +08:00
|
|
|
kgc->parent_realize(dev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2013-03-05 08:34:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:57:16 +08:00
|
|
|
if (s->security_extn) {
|
|
|
|
error_setg(errp, "the in-kernel VGIC does not implement the "
|
|
|
|
"security extensions");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-16 19:31:53 +08:00
|
|
|
if (!kvm_arm_gic_can_save_restore(s)) {
|
|
|
|
error_setg(&s->migration_blocker, "This operating system kernel does "
|
|
|
|
"not support vGICv2 migration");
|
|
|
|
migrate_add_blocker(s->migration_blocker, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
error_free(s->migration_blocker);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 08:29:36 +08:00
|
|
|
gic_init_irqs_and_mmio(s, kvm_arm_gicv2_set_irq, NULL);
|
2015-07-07 02:15:13 +08:00
|
|
|
|
|
|
|
for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
|
|
|
|
qemu_irq irq = qdev_get_gpio_in(dev, i);
|
|
|
|
kvm_irqchip_set_qemuirq_gsi(kvm_state, irq, i);
|
|
|
|
}
|
|
|
|
|
2014-02-27 01:20:00 +08:00
|
|
|
/* Try to create the device via the device control API */
|
|
|
|
s->dev_fd = -1;
|
|
|
|
ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V2, false);
|
|
|
|
if (ret >= 0) {
|
|
|
|
s->dev_fd = ret;
|
2015-09-24 08:29:36 +08:00
|
|
|
|
|
|
|
/* Newstyle API is used, we may have attributes */
|
|
|
|
if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0)) {
|
|
|
|
uint32_t numirqs = s->num_irq;
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0,
|
2017-06-13 21:57:00 +08:00
|
|
|
&numirqs, true, &error_abort);
|
2015-09-24 08:29:36 +08:00
|
|
|
}
|
|
|
|
/* Tell the kernel to complete VGIC initialization now */
|
|
|
|
if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
|
|
|
KVM_DEV_ARM_VGIC_CTRL_INIT)) {
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
2017-06-13 21:57:00 +08:00
|
|
|
KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true,
|
|
|
|
&error_abort);
|
2015-09-24 08:29:36 +08:00
|
|
|
}
|
2014-02-27 01:20:00 +08:00
|
|
|
} else if (ret != -ENODEV && ret != -ENOTSUP) {
|
|
|
|
error_setg_errno(errp, -ret, "error creating in-kernel VGIC");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-05 08:34:43 +08:00
|
|
|
/* Distributor */
|
|
|
|
kvm_arm_register_device(&s->iomem,
|
|
|
|
(KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
|
2014-02-27 01:20:00 +08:00
|
|
|
| KVM_VGIC_V2_ADDR_TYPE_DIST,
|
|
|
|
KVM_DEV_ARM_VGIC_GRP_ADDR,
|
|
|
|
KVM_VGIC_V2_ADDR_TYPE_DIST,
|
|
|
|
s->dev_fd);
|
2013-03-05 08:34:43 +08:00
|
|
|
/* CPU interface for current core. Unlike arm_gic, we don't
|
|
|
|
* provide the "interface for core #N" memory regions, because
|
|
|
|
* cores with a VGIC don't have those.
|
|
|
|
*/
|
|
|
|
kvm_arm_register_device(&s->cpuiomem[0],
|
|
|
|
(KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
|
2014-02-27 01:20:00 +08:00
|
|
|
| KVM_VGIC_V2_ADDR_TYPE_CPU,
|
|
|
|
KVM_DEV_ARM_VGIC_GRP_ADDR,
|
|
|
|
KVM_VGIC_V2_ADDR_TYPE_CPU,
|
|
|
|
s->dev_fd);
|
2015-10-27 20:00:50 +08:00
|
|
|
|
2016-10-04 20:28:08 +08:00
|
|
|
if (kvm_has_gsi_routing()) {
|
|
|
|
/* set up irq routing */
|
|
|
|
kvm_init_irq_routing(kvm_state);
|
|
|
|
for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
|
|
|
|
kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_gsi_routing_allowed = true;
|
|
|
|
|
|
|
|
kvm_irqchip_commit_routes(kvm_state);
|
|
|
|
}
|
2013-03-05 08:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_arm_gic_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
ARMGICCommonClass *agcc = ARM_GIC_COMMON_CLASS(klass);
|
|
|
|
KVMARMGICClass *kgc = KVM_ARM_GIC_CLASS(klass);
|
|
|
|
|
|
|
|
agcc->pre_save = kvm_arm_gic_get;
|
|
|
|
agcc->post_load = kvm_arm_gic_put;
|
2018-01-14 10:04:12 +08:00
|
|
|
device_class_set_parent_realize(dc, kvm_arm_gic_realize,
|
|
|
|
&kgc->parent_realize);
|
|
|
|
device_class_set_parent_reset(dc, kvm_arm_gic_reset, &kgc->parent_reset);
|
2013-03-05 08:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo kvm_arm_gic_info = {
|
|
|
|
.name = TYPE_KVM_ARM_GIC,
|
|
|
|
.parent = TYPE_ARM_GIC_COMMON,
|
|
|
|
.instance_size = sizeof(GICState),
|
|
|
|
.class_init = kvm_arm_gic_class_init,
|
|
|
|
.class_size = sizeof(KVMARMGICClass),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_arm_gic_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&kvm_arm_gic_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(kvm_arm_gic_register_types)
|