The code currently is assuming fixed sized memory regions for the distributor and CPU interface. GICv3 needs a dynamic allocation of it's redistributor region, since it's size depends on the number of vCPUs. Also add the necessary code to create a GICv3 IRQ chip instance. Signed-off-by: Andre Przywara <andre.przywara@xxxxxxx> --- tools/kvm/arm/aarch64/include/kvm/kvm-arch.h | 6 ++++++ tools/kvm/arm/gic.c | 29 ++++++++++++++++++++++++-- tools/kvm/arm/include/arm-common/kvm-arch.h | 11 ++++++---- tools/kvm/arm/kvm-cpu.c | 4 +++- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/tools/kvm/arm/aarch64/include/kvm/kvm-arch.h b/tools/kvm/arm/aarch64/include/kvm/kvm-arch.h index 4925736..42d0fd6 100644 --- a/tools/kvm/arm/aarch64/include/kvm/kvm-arch.h +++ b/tools/kvm/arm/aarch64/include/kvm/kvm-arch.h @@ -3,6 +3,12 @@ #define ARM_GIC_DIST_SIZE 0x10000 #define ARM_GIC_CPUI_SIZE 0x20000 +/* + * On a GICv3 there must be one redistributor per vCPU. + * The value here is the size for one, we multiply this at runtime with + * the number of requested vCPUs to get the actual size. + */ +#define ARM_GIC_REDIST_SIZE 0x20000 #define ARM_KERN_OFFSET(kvm) ((kvm)->cfg.arch.aarch32_guest ? \ 0x8000 : \ diff --git a/tools/kvm/arm/gic.c b/tools/kvm/arm/gic.c index 770c6e7..5141868 100644 --- a/tools/kvm/arm/gic.c +++ b/tools/kvm/arm/gic.c @@ -11,6 +11,7 @@ static int irq_ids; static int gic_fd = -1; static u64 cpu_if_addr = ARM_GIC_CPUI_BASE; +static int nr_redists = 0; int gic__alloc_irqnum(void) { @@ -27,6 +28,7 @@ static int gic__create_device(struct kvm *kvm, u32 type) int err; u32 offset = 0; u64 dist_addr = ARM_GIC_DIST_BASE; + u64 redist_addr; struct kvm_create_device gic_device = { .type = type, }; @@ -41,9 +43,13 @@ static int gic__create_device(struct kvm *kvm, u32 type) }; struct kvm_device_attr dist_attr = { .group = KVM_DEV_ARM_VGIC_GRP_ADDR, - .attr = KVM_VGIC_V2_ADDR_TYPE_DIST, .addr = (u64)(unsigned long)&dist_addr, }; + struct kvm_device_attr redist_attr = { + .group = KVM_DEV_ARM_VGIC_GRP_ADDR, + .attr = KVM_VGIC_V3_ADDR_TYPE_REDIST, + .addr = (u64)(unsigned long)&redist_addr, + }; err = ioctl(kvm->vm_fd, KVM_CREATE_DEVICE, &gic_device); if (err) @@ -64,13 +70,23 @@ static int gic__create_device(struct kvm *kvm, u32 type) err = ioctl(gic_fd, KVM_SET_DEVICE_ATTR, &cpu_if_attr); if (err) return err; + dist_attr.attr = KVM_VGIC_V2_ADDR_TYPE_DIST; pr_info("creating GICv2 KVM device"); break; + case KVM_DEV_TYPE_ARM_VGIC_V3: + dist_attr.attr = KVM_VGIC_V3_ADDR_TYPE_DIST; + redist_addr = dist_addr - nr_redists * ARM_GIC_REDIST_SIZE; + break; default: return -ENODEV; } err = ioctl(gic_fd, KVM_SET_DEVICE_ATTR, &dist_attr); + if (err) + return err; + + if (type == KVM_DEV_TYPE_ARM_VGIC_V3) + err = ioctl(gic_fd, KVM_SET_DEVICE_ATTR, &redist_attr); return err; } @@ -166,17 +182,26 @@ void gic__generate_fdt_nodes(void *fdt, u32 phandle, u32 type) u64 reg_prop[] = { cpu_to_fdt64(ARM_GIC_DIST_BASE), cpu_to_fdt64(ARM_GIC_DIST_SIZE), - cpu_to_fdt64(cpu_if_addr), cpu_to_fdt64(ARM_GIC_CPUI_SIZE), + 0, 0, /* to be filled */ }; switch (type) { case KVM_DEV_TYPE_ARM_VGIC_V2: compatible = "arm,cortex-a15-gic"; + reg_prop[2] = cpu_if_addr; + reg_prop[3] = ARM_GIC_CPUI_SIZE; pr_info("creating FDT for a GICv2"); break; + case KVM_DEV_TYPE_ARM_VGIC_V3: + compatible = "arm,gic-v3"; + reg_prop[2] = ARM_GIC_DIST_BASE - nr_redists * ARM_GIC_REDIST_SIZE; + reg_prop[3] = ARM_GIC_REDIST_SIZE * nr_redists; + break; default: return; } + reg_prop[2] = cpu_to_fdt64(reg_prop[2]); + reg_prop[3] = cpu_to_fdt64(reg_prop[3]); _FDT(fdt_begin_node(fdt, "intc")); _FDT(fdt_property_string(fdt, "compatible", compatible)); diff --git a/tools/kvm/arm/include/arm-common/kvm-arch.h b/tools/kvm/arm/include/arm-common/kvm-arch.h index 72b204f..c7bfd9a 100644 --- a/tools/kvm/arm/include/arm-common/kvm-arch.h +++ b/tools/kvm/arm/include/arm-common/kvm-arch.h @@ -15,10 +15,8 @@ #define ARM_GIC_DIST_BASE (ARM_AXI_AREA - ARM_GIC_DIST_SIZE) #define ARM_GIC_CPUI_BASE (ARM_GIC_DIST_BASE - ARM_GIC_CPUI_SIZE) -#define ARM_GIC_SIZE (ARM_GIC_DIST_SIZE + ARM_GIC_CPUI_SIZE) #define ARM_IOPORT_SIZE (ARM_MMIO_AREA - ARM_IOPORT_AREA) -#define ARM_VIRTIO_MMIO_SIZE (ARM_AXI_AREA - (ARM_MMIO_AREA + ARM_GIC_SIZE)) #define ARM_PCI_MMIO_SIZE (ARM_MEMORY_AREA - ARM_AXI_AREA) #define KVM_IOPORT_AREA ARM_IOPORT_AREA @@ -33,9 +31,14 @@ static inline bool arm_addr_in_ioport_region(u64 phys_addr) return phys_addr >= KVM_IOPORT_AREA && phys_addr < limit; } -static inline bool arm_addr_in_virtio_mmio_region(u64 phys_addr) +static inline bool arm_addr_in_virtio_mmio_region(int nr_redists, u64 phys_addr) { - u64 limit = KVM_VIRTIO_MMIO_AREA + ARM_VIRTIO_MMIO_SIZE; + u64 limit = ARM_AXI_AREA - ARM_GIC_DIST_SIZE; + + if (nr_redists) + limit -= ARM_GIC_REDIST_SIZE * nr_redists; + else + limit -= ARM_GIC_CPUI_SIZE; return phys_addr >= KVM_VIRTIO_MMIO_AREA && phys_addr < limit; } diff --git a/tools/kvm/arm/kvm-cpu.c b/tools/kvm/arm/kvm-cpu.c index d31e7b1..30fe702 100644 --- a/tools/kvm/arm/kvm-cpu.c +++ b/tools/kvm/arm/kvm-cpu.c @@ -101,7 +101,9 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) bool kvm_cpu__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_write) { - if (arm_addr_in_virtio_mmio_region(phys_addr)) { + int nr_redists = kvm->cfg.arch.gicv3 ? kvm->nrcpus : 0; + + if (arm_addr_in_virtio_mmio_region(nr_redists, phys_addr)) { return kvm__emulate_mmio(kvm, phys_addr, data, len, is_write); } else if (arm_addr_in_ioport_region(phys_addr)) { int direction = is_write ? KVM_EXIT_IO_OUT : KVM_EXIT_IO_IN; -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html