Skip to contentSkip to conversation

kvm tools: add support for ARMv7 processors

PATCH Applied Will Deacon 3 participants 4 messages Nov 23, 2012 – Nov 27, 2012 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 kvm tools: add support for ARMv7 processors

This patch adds initial support for ARMv7 processors (more specifically, Cortex-A15) to kvmtool.

Everything is driven by FDT, including dynamic generation of virtio nodes for MMIO devices (PCI is not used due to lack of a suitable host-bridge).

The virtual timers and virtual interrupt controller (VGIC) are provided by the kernel and require very little in terms of userspace code.

---

Ok, here's v3 following the feedback on v2. Changes include:

- Stick the wfi instruction in the SMP pen in a loop, in case we return early

- Fix initcall ordering with SMP pen memcpy (move DT creation to late_init and treat the SMP pen as firmware).

Cheers,

Will

 tools/kvm/Makefile                           |  22 ++-
 tools/kvm/arm/aarch32/cortex-a15.c           |  98 ++++++++++
 tools/kvm/arm/aarch32/include/kvm/barrier.h  |  10 +
 tools/kvm/arm/aarch32/include/kvm/kvm-arch.h |  30 +++
 tools/kvm/arm/aarch32/kvm-cpu.c              | 111 +++++++++++
 tools/kvm/arm/aarch32/smp-pen.S              |  35 ++++
 tools/kvm/arm/fdt.c                          | 266 +++++++++++++++++++++++++++
 tools/kvm/arm/gic.c                          |  92 +++++++++
 tools/kvm/arm/include/arm-common/gic.h       |  34 ++++
 tools/kvm/arm/include/arm-common/kvm-arch.h  |  34 ++++
 tools/kvm/arm/include/kvm/kvm-cpu-arch.h     |  47 +++++
 tools/kvm/arm/ioport.c                       |   5 +
 tools/kvm/arm/irq.c                          |  17 ++
 tools/kvm/arm/kvm-cpu.c                      | 107 +++++++++++
 tools/kvm/arm/kvm.c                          |  69 +++++++
 tools/kvm/arm/smp.c                          |  21 +++
 16 files changed, 997 insertions(+), 1 deletion(-)
 create mode 100644 tools/kvm/arm/aarch32/cortex-a15.c
 create mode 100644 tools/kvm/arm/aarch32/include/kvm/barrier.h
 create mode 100644 tools/kvm/arm/aarch32/include/kvm/kvm-arch.h
 create mode 100644 tools/kvm/arm/aarch32/kvm-cpu.c
 create mode 100644 tools/kvm/arm/aarch32/smp-pen.S
 create mode 100644 tools/kvm/arm/fdt.c
 create mode 100644 tools/kvm/arm/gic.c
 create mode 100644 tools/kvm/arm/include/arm-common/gic.h
 create mode 100644 tools/kvm/arm/include/arm-common/kvm-arch.h
 create mode 100644 tools/kvm/arm/include/kvm/kvm-cpu-arch.h
 create mode 100644 tools/kvm/arm/ioport.c
 create mode 100644 tools/kvm/arm/irq.c
 create mode 100644 tools/kvm/arm/kvm-cpu.c
 create mode 100644 tools/kvm/arm/kvm.c
 create mode 100644 tools/kvm/arm/smp.c
tools/kvm/Makefile +21 -1
index 3f25a14..a83dd10 100644
@@ -102,7 +102,8 @@ OBJS	+= builtin-sandbox.o
 OBJS	+= virtio/mmio.o
 
 # Translate uname -m into ARCH string
-ARCH ?= $(shell uname -m | sed -e s/i.86/i386/ -e s/ppc.*/powerpc/)
+ARCH ?= $(shell uname -m | sed -e s/i.86/i386/ -e s/ppc.*/powerpc/ \
+	  -e s/armv7.*/arm/)
 
 ifeq ($(ARCH),i386)
 	ARCH         := x86
@@ -157,6 +158,25 @@ ifeq ($(ARCH), powerpc)
 	CFLAGS 	+= -m64
 endif
 
+# ARM
+OBJS_ARM_COMMON		:= arm/fdt.o arm/gic.o arm/ioport.o arm/irq.o \
+			   arm/kvm.o arm/kvm-cpu.o arm/smp.o
+HDRS_ARM_COMMON		:= arm/include
+ifeq ($(ARCH), arm)
+	DEFINES		+= -DCONFIG_ARM
+	OBJS		+= $(OBJS_ARM_COMMON)
+	OBJS		+= arm/aarch32/cortex-a15.o
+	OBJS		+= arm/aarch32/kvm-cpu.o
+	OBJS       	+= arm/aarch32/smp-pen.o
+	ARCH_INCLUDE	:= $(HDRS_ARM_COMMON)
+	ARCH_INCLUDE	+= -Iarm/aarch32/include
+	ASFLAGS		+= -D__ASSEMBLY__
+	ASFLAGS		+= -I$(ARCH_INCLUDE)
+	CFLAGS		+= -march=armv7-a
+	CFLAGS		+= -I../../scripts/dtc/libfdt
+	OTHEROBJS	+= $(LIBFDT_OBJS)
+endif
+
 ###
 
 ifeq (,$(ARCH_INCLUDE))
tools/kvm/arm/aarch32/cortex-a15.c new file +98 -0

98 lines not on this page · raw message

tools/kvm/arm/aarch32/include/kvm/barrier.h new file +10 -0
new file mode 100644
index 0000000..94913a9
@@ -0,0 +1,10 @@
+#ifndef KVM__KVM_BARRIER_H
+#define KVM__KVM_BARRIER_H
+
+#define dmb()	asm volatile ("dmb" : : : "memory")
+
+#define mb()	dmb()
+#define rmb()	dmb()
+#define wmb()	dmb()
+
+#endif /* KVM__KVM_BARRIER_H */
tools/kvm/arm/aarch32/include/kvm/kvm-arch.h new file +30 -0
new file mode 100644
index 0000000..f236895
@@ -0,0 +1,30 @@
+#ifndef KVM__KVM_ARCH_H
+#define KVM__KVM_ARCH_H
+
+#include <linux/const.h>
+
+#define ARM_LOMAP_MMIO_AREA	_AC(0x00000000, UL)
+#define ARM_LOMAP_AXI_AREA	_AC(0x40000000, UL)
+#define ARM_LOMAP_MEMORY_AREA	_AC(0x80000000, UL)
+#define ARM_LOMAP_MAX_MEMORY	_AC(0x7fffffff, UL)
+
+#define ARM_GIC_DIST_SIZE	0x1000
+#define ARM_GIC_DIST_BASE	(ARM_LOMAP_AXI_AREA - ARM_GIC_DIST_SIZE)
+#define ARM_GIC_CPUI_SIZE	0x2000
+#define ARM_GIC_CPUI_BASE	(ARM_GIC_DIST_BASE - ARM_GIC_CPUI_SIZE)
+
+#define ARM_KERN_OFFSET		0x8000
+
+#define ARM_SMP_PEN_SIZE	PAGE_SIZE
+#define ARM_VIRTIO_MMIO_SIZE	(ARM_GIC_DIST_BASE - ARM_LOMAP_MMIO_AREA)
+#define ARM_PCI_MMIO_SIZE	(ARM_LOMAP_MEMORY_AREA - ARM_LOMAP_AXI_AREA)
+
+#define ARM_MEMORY_AREA		ARM_LOMAP_MEMORY_AREA
+#define ARM_MAX_MEMORY		ARM_LOMAP_MAX_MEMORY
+
+#define KVM_PCI_MMIO_AREA	ARM_LOMAP_AXI_AREA
+#define KVM_VIRTIO_MMIO_AREA	ARM_LOMAP_MMIO_AREA
+
+#include "arm-common/kvm-arch.h"
+
+#endif /* KVM__KVM_ARCH_H */
tools/kvm/arm/aarch32/kvm-cpu.c new file +111 -0

111 lines not on this page · raw message

tools/kvm/arm/aarch32/smp-pen.S new file +35 -0
new file mode 100644
index 0000000..0861171
@@ -0,0 +1,35 @@
+#include "kvm/kvm-arch.h"
+
+#include "arm-common/gic.h"
+
+#define AARCH32_SMP_BAD_MAGIC	0xdeadc0de
+
+	.arm
+
+	.globl	smp_pen_start
+	.globl	smp_jump_addr
+	.globl	smp_pen_end
+
+	.align
+smp_pen_start:
+	@ Ensure that the CPU interface is enabled for the wfi wakeup
+	ldr	r0, =ARM_GIC_CPUI_BASE
+	mov	r1, #GIC_CPUI_CTLR_EN
+	str	r1, [r0]
+
+	@ Now wait for the primary to poke us
+	adr	r0, smp_jump_addr
+	ldr	r1, =AARCH32_SMP_BAD_MAGIC
+	dsb
+1:	wfi
+	ldr	r2, [r0]
+	cmp	r1, r2
+	beq	1b
+	mov	pc, r2
+
+	.ltorg
+
+	.align
+smp_jump_addr:
+	.long	AARCH32_SMP_BAD_MAGIC
+smp_pen_end:
tools/kvm/arm/fdt.c new file +266 -0

266 lines not on this page · raw message

tools/kvm/arm/gic.c new file +92 -0

92 lines not on this page · raw message

tools/kvm/arm/include/arm-common/gic.h new file +34 -0
new file mode 100644
index 0000000..d534174
@@ -0,0 +1,34 @@
+#ifndef ARM_COMMON__GIC_H
+#define ARM_COMMON__GIC_H
+
+#define GIC_SGI_IRQ_BASE		0
+#define GIC_PPI_IRQ_BASE		16
+#define GIC_SPI_IRQ_BASE		32
+
+#define GIC_FDT_IRQ_NUM_CELLS		3
+
+#define GIC_FDT_IRQ_TYPE_SPI		0
+#define GIC_FDT_IRQ_TYPE_PPI		1
+
+#define GIC_FDT_IRQ_FLAGS_EDGE_LO_HI	1
+#define GIC_FDT_IRQ_FLAGS_EDGE_HI_LO	2
+#define GIC_FDT_IRQ_FLAGS_LEVEL_HI	4
+#define GIC_FDT_IRQ_FLAGS_LEVEL_LO	8
+
+#define GIC_FDT_IRQ_PPI_CPU_SHIFT	8
+#define GIC_FDT_IRQ_PPI_CPU_MASK	(0xff << GIC_FDT_IRQ_PPI_CPU_SHIFT)
+
+#define GIC_CPUI_CTLR_EN		(1 << 0)
+
+#define GIC_MAX_CPUS			8
+#define GIC_MAX_IRQ			255
+
+#ifndef __ASSEMBLY__
+struct kvm;
+
+int gic__alloc_irqnum(void);
+int gic__init_irqchip(struct kvm *kvm);
+void gic__generate_fdt_nodes(void *fdt, u32 phandle);
+
+#endif /* __ASSEMBLY__ */
+#endif /* ARM_COMMON__GIC_H */
tools/kvm/arm/include/arm-common/kvm-arch.h new file +34 -0
new file mode 100644
index 0000000..c910f07
@@ -0,0 +1,34 @@
+#ifndef ARM_COMMON__KVM_ARCH_H
+#define ARM_COMMON__KVM_ARCH_H
+
+#define VIRTIO_DEFAULT_TRANS	VIRTIO_MMIO
+
+#ifndef __ASSEMBLY__
+
+#include <stdbool.h>
+#include <linux/types.h>
+
+static inline bool arm_addr_in_virtio_mmio_region(u64 phys_addr)
+{
+	u64 limit = KVM_VIRTIO_MMIO_AREA + ARM_VIRTIO_MMIO_SIZE;
+	return phys_addr >= KVM_VIRTIO_MMIO_AREA && phys_addr < limit;
+}
+
+static inline bool arm_addr_in_pci_mmio_region(u64 phys_addr)
+{
+	u64 limit = KVM_PCI_MMIO_AREA + ARM_PCI_MMIO_SIZE;
+	return phys_addr >= KVM_PCI_MMIO_AREA && phys_addr < limit;
+}
+
+struct kvm_arch {
+	u64	memory_guest_start;
+	u64	kern_guest_start;
+	u64	initrd_guest_start;
+	u64	initrd_size;
+	u64	dtb_guest_start;
+	u64	smp_pen_guest_start;
+	u64	smp_jump_guest_start;
+};
+
+#endif /* __ASSEMBLY__ */
+#endif /* ARM_COMMON__KVM_ARCH_H */
tools/kvm/arm/include/kvm/kvm-cpu-arch.h new file +47 -0

47 lines not on this page · raw message

tools/kvm/arm/ioport.c new file +5 -0
new file mode 100644
index 0000000..3c03fa0
@@ -0,0 +1,5 @@
+#include "kvm/ioport.h"
+
+void ioport__setup_arch(struct kvm *kvm)
+{
+}
tools/kvm/arm/irq.c new file +17 -0
new file mode 100644
index 0000000..e173e04
@@ -0,0 +1,17 @@
+#include "kvm/irq.h"
+#include "kvm/kvm.h"
+#include "kvm/util.h"
+
+#include "arm-common/gic.h"
+
+int irq__register_device(u32 dev, u8 *pin, u8 *line)
+{
+	*line = gic__alloc_irqnum();
+	return 0;
+}
+
+int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg)
+{
+	die(__FUNCTION__);
+	return 0;
+}
tools/kvm/arm/kvm-cpu.c new file +107 -0

107 lines not on this page · raw message

tools/kvm/arm/kvm.c new file +69 -0

69 lines not on this page · raw message

tools/kvm/arm/smp.c new file +21 -0
new file mode 100644
index 0000000..c1e59d2
@@ -0,0 +1,21 @@
+#include "kvm/kvm.h"
+
+extern u8 smp_pen_start, smp_pen_end, smp_jump_addr;
+
+static int smp_pen_init(struct kvm *kvm)
+{
+	unsigned long pen_size, pen_start, jump_offset;
+
+	if (!(kvm->nrcpus > 1))
+		return 0;
+
+	pen_size = &smp_pen_end - &smp_pen_start;
+	pen_start = kvm->arch.smp_pen_guest_start;
+	jump_offset = &smp_jump_addr - &smp_pen_start;
+
+	kvm->arch.smp_jump_guest_start = pen_start + jump_offset;
+	memcpy(guest_flat_to_host(kvm, pen_start), &smp_pen_start, pen_size);
+
+	return 0;
+}
+firmware_init(smp_pen_init);
Signature · 1 line
1.8.0
Tested-by on v3 1/1
#1 lore raw
Quoted 10 lines · Will Deacon

This patch adds initial support for ARMv7 processors (more specifically, Cortex-A15) to kvmtool.

Everything is driven by FDT, including dynamic generation of virtio nodes for MMIO devices (PCI is not used due to lack of a suitable host-bridge).

The virtual timers and virtual interrupt controller (VGIC) are provided by the kernel and require very little in terms of userspace code.

FWIW:

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Applied
#2 lore raw
Quoted 7 lines · Marc Zyngier
Quoted 6 lines · Will Deacon

This patch adds initial support for ARMv7 processors (more specifically, Cortex-A15) to kvmtool.

Everything is driven by FDT, including dynamic generation of virtio nodes for MMIO devices (PCI is not used due to lack of a suitable host-bridge).

Quoted 8 lines
Quoted 4 lines

The virtual timers and virtual interrupt controller (VGIC) are provided by the kernel and require very little in terms of userspace code.

FWIW:

Applied, thanks guys!

#3 lore raw
Quoted 11 lines · Pekka Enberg
Quoted 10 lines · Marc Zyngier
Quoted 9 lines · Will Deacon

This patch adds initial support for ARMv7 processors (more specifically, Cortex-A15) to kvmtool.

Everything is driven by FDT, including dynamic generation of virtio nodes for MMIO devices (PCI is not used due to lack of a suitable host-bridge).

The virtual timers and virtual interrupt controller (VGIC) are provided by the kernel and require very little in terms of userspace code.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

FWIW:

Applied, thanks guys!

Cheers Pekka! I got my ppc64 machine up and running at the weekend and it looks like I broke the build with my device registration stuff, so I'll post a fix in a second...

Will