// SPDX-License-Identifier: GPL-2.0
/*
* vgic init sequence tests
*
* Copyright (C) 2020, Red Hat, Inc.
*/
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <asm/kvm.h>
#include <asm/kvm_para.h>
#include <arm64/gic_v3.h>
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
#include "vgic.h"
#include "gic_v3.h"
#define NR_VCPUS 4
#define REG_OFFSET(vcpu, offset) (((u64)vcpu << 32) | offset)
#define VGIC_DEV_IS_V2(_d) ((_d) == KVM_DEV_TYPE_ARM_VGIC_V2)
#define VGIC_DEV_IS_V3(_d) ((_d) == KVM_DEV_TYPE_ARM_VGIC_V3)
struct vm_gic {
struct kvm_vm *vm;
int gic_fd;
u32 gic_dev_type;
};
static u64 max_phys_size;
/*
* Helpers to access a redistributor register and verify the ioctl() failed or
* succeeded as expected, and provided the correct value on success.
*/
static void v3_redist_reg_get_errno(int gicv3_fd, int vcpu, int offset,
int want, const char *msg)
{
u32 ignored_val;
int ret = __kvm_device_attr_get(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
REG_OFFSET(vcpu, offset), &ignored_val);
TEST_ASSERT(ret && errno == want, "%s; want errno = %d", msg, want);
}
static void v3_redist_reg_get(int gicv3_fd, int vcpu, int offset, u32 want,
const char *msg)
{
u32 val;
kvm_device_attr_get(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
REG_OFFSET(vcpu, offset), &val);
TEST_ASSERT(val == want, "%s; want '0x%x', got '0x%x'", msg, want, val);
}
/* dummy guest code */
static void guest_code(void)
{
GUEST_SYNC(0);
GUEST_SYNC(1);
GUEST_SYNC(2);
GUEST_DONE();
}
/* we don't want to assert on run execution, hence that helper */
static int run_vcpu(struct kvm_vcpu *vcpu)
{
return __vcpu_run(vcpu) ? -errno : 0;
}
static struct vm_gic vm_gic_create_with_vcpus(u32 gic_dev_type,
u32 nr_vcpus,
struct kvm_vcpu *vcpus[])
{
struct vm_gic v;
v.gic_dev_type = gic_dev_type;
v.vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
v.gic_fd = kvm_create_device(v.vm, gic_dev_type);
return v;
}
static struct vm_gic vm_gic_create_barebones(u32 gic_dev_type)
{
struct vm_gic v;
v.gic_dev_type = gic_dev_type;
v.vm = vm_create_barebones();
v.gic_fd = kvm_create_device(v.vm, gic_dev_type);
return v;
}
static void vm_gic_destroy(struct vm_gic *v)
{
close(v->gic_fd);
kvm_vm_free(v->vm);
}
struct vgic_region_attr {
u64 attr;
u64 size;
u64 alignment;
};
struct vgic_region_attr gic_v3_dist_region = {
.attr = KVM_VGIC_V3_ADDR_TYPE_DIST,
.size = 0x10000,
.alignment = 0x10000,
};
struct vgic_region_attr gic_v3_redist_region = {
.attr = KVM_VGIC_V3_ADDR_TYPE_REDIST,
.size = NR_VCPUS * 0x20000,
.alignment = 0x10000,
};
struct vgic_region_attr gic_v2_dist_region = {
.attr = KVM_VGIC_V2_ADDR_TYPE_DIST,
.size = 0x1000,
.alignment = 0x1000,
};
struct vgic_region_attr gic_v2_cpu_region = {
.attr = KVM_VGIC_V2_ADDR_TYPE_CPU,
.size = 0x2000,
.alignment = 0x1000,
};
/**
* Helper routine that performs KVM device tests in general. Eventually the
* ARM_VGIC (GICv2 or GICv3) device gets created with an overlapping
* DIST/REDIST (or DIST/CPUIF for GICv2). Assumption is 4 vcpus are going to be
* used hence the overlap. In the case of GICv3, A RDIST region is set at @0x0
* and a DIST region is set @0x70000. The GICv2 case sets a CPUIF @0x0 and a
* DIST region @0x1000.
*/
static void subtest_dist_rdist(struct vm_gic *v)
{
int ret;
u64 addr;
struct vgic_region_attr rdist; /* CPU interface in GICv2*/
struct vgic_region_attr dist;
rdist = VGIC_DEV_IS_V3(v->gic_dev_type) ? gic_v3_redist_region
: gic_v2_cpu_region;
dist = VGIC_DEV_IS_V3(v->gic_dev_type) ? gic_v3_dist_region
: gic_v2_dist_region;
/* Check existing group/attributes */
kvm_has_device_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, dist.attr);
kvm_has_device_attr(v