Skip to contentSkip to conversation

kvm tools: arm/arm64 updates

PATCH New Will Deacon 1 participant 6 messages Jan 6, 2014 KVM

lore
Share & tools
lore
#4 lore raw

PATCH 4/5 kvm tools: support unsigned int options

In reply to Will Deacon

From: Robin Murphy <[email protected]>

Add support for unsigned int command-line options by implementing the OPT_UINTEGER macro.

---
 tools/kvm/include/kvm/parse-options.h | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/kvm/include/kvm/parse-options.h +9 -0
index 09a5fca71117..b03f151240d0 100644
@@ -109,6 +109,15 @@ struct option {
 	.help = (h)                         \
 }
 
+#define OPT_UINTEGER(s, l, v, h)            \
+{                                           \
+	.type = OPTION_UINTEGER,            \
+	.short_name = (s),                  \
+	.long_name = (l),                   \
+	.value = check_vtype(v, unsigned int *), \
+	.help = (h)                         \
+}
+
 #define OPT_U64(s, l, v, h)                 \
 {                                           \
 	.type = OPTION_U64,                 \
Signature · 1 line
1.8.2.2
#5 lore raw

PATCH 5/5 kvm tools: arm: add option to override generic timer frequency

In reply to Will Deacon

From: Robin Murphy <[email protected]>

Some platforms have secure firmware which does not correctly set the CNTFRQ register on boot, preventing the use of the Generic Timer. This patch allows mirroring the necessary host workaround by specifying the clock-frequency property in the guest DT.

This should only be considered a means of KVM bring-up on such systems, such that vendors may be convinced to properly implement their firmware to support the virtualisation capabilities of their hardware.

---
 tools/kvm/arm/include/arm-common/kvm-config-arch.h | 15 ++++++++++-----
 tools/kvm/arm/timer.c                              |  2 ++
 2 files changed, 12 insertions(+), 5 deletions(-)
tools/kvm/arm/include/arm-common/kvm-config-arch.h +10 -5
index 7ac6f6e88550..f3baf392704d 100644
@@ -5,13 +5,18 @@
 
 struct kvm_config_arch {
 	const char *dump_dtb_filename;
+	unsigned int force_cntfrq;
 	bool aarch32_guest;
 };
 
-#define OPT_ARCH_RUN(pfx, cfg)						\
-	pfx,								\
-	ARM_OPT_ARCH_RUN(cfg)						\
-	OPT_STRING('\0', "dump-dtb", &(cfg)->dump_dtb_filename,		\
-		   ".dtb file", "Dump generated .dtb to specified file"),
+#define OPT_ARCH_RUN(pfx, cfg)							\
+	pfx,									\
+	ARM_OPT_ARCH_RUN(cfg)							\
+	OPT_STRING('\0', "dump-dtb", &(cfg)->dump_dtb_filename,			\
+		   ".dtb file", "Dump generated .dtb to specified file"),	\
+	OPT_UINTEGER('\0', "override-bad-firmware-cntfrq", &(cfg)->force_cntfrq,\
+		     "Specify Generic Timer frequency in guest DT to "		\
+		     "work around buggy secure firmware *Firmware should be "	\
+		     "updated to program CNTFRQ correctly*"),
 
 #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */
tools/kvm/arm/timer.c +2 -0
index bd6a0bb2c324..d757c1dd8c08 100644
@@ -33,6 +33,8 @@ void timer__generate_fdt_nodes(void *fdt, struct kvm *kvm, int *irqs)
 	_FDT(fdt_begin_node(fdt, "timer"));
 	_FDT(fdt_property(fdt, "compatible", compatible, sizeof(compatible)));
 	_FDT(fdt_property(fdt, "interrupts", irq_prop, sizeof(irq_prop)));
+	if (kvm->cfg.arch.force_cntfrq > 0)
+		_FDT(fdt_property_cell(fdt, "clock-frequency", kvm->cfg.arch.force_cntfrq));
 	_FDT(fdt_end_node(fdt));
 }
 
Signature · 1 line
1.8.2.2
#2 lore raw

PATCH 2/5 kvm tools: arm: add support for ARM Cortex-A7

In reply to Will Deacon

From: Jonathan Austin <[email protected]>

The Cortex-A7 is very similar to the Cortex-A15 and as such there is very little extra infrastructure required for KVM tool to be able to create A7-guests.

This patch adds the basic support and allows booting of A7 guests on A7 hosts. It depends on Cortex-A7 support patches posted recently to the kvmarm list.

---
 tools/kvm/arm/aarch32/arm-cpu.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/kvm/arm/aarch32/arm-cpu.c +8 -1
index 8817d2a8b772..71b98fe350b2 100644
@@ -28,8 +28,15 @@ static struct kvm_arm_target target_cortex_a15 = {
 	.init		= arm_cpu__vcpu_init,
 };
 
+static struct kvm_arm_target target_cortex_a7 = {
+	.id		= KVM_ARM_TARGET_CORTEX_A7,
+	.compatible	= "arm,cortex-a7",
+	.init		= arm_cpu__vcpu_init,
+};
+
 static int arm_cpu__core_init(struct kvm *kvm)
 {
-	return kvm_cpu__register_kvm_arm_target(&target_cortex_a15);
+	return (kvm_cpu__register_kvm_arm_target(&target_cortex_a15) ||
+		kvm_cpu__register_kvm_arm_target(&target_cortex_a7));
 }
 core_init(arm_cpu__core_init);
Signature · 1 line
1.8.2.2
#3 lore raw

PATCH 3/5 kvm tools: arm: emit the MPIDR in DT instead of cpu_id

In reply to Will Deacon

From: Marc Zyngier <[email protected]>

kvmtools uses the virtual CPU number to emit the DT CPU nodes. While this is correct for a flat topology, it fails on anything else, as the guest expects to find the MPIDR there.

The fix is to ask the kernel for each vcpu MPIDR, and emit this instead.

---
 tools/kvm/arm/aarch32/include/kvm/kvm-cpu-arch.h |  4 ++++
 tools/kvm/arm/aarch32/kvm-cpu.c                  | 26 ++++++++++++++++++++++
 tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h |  4 ++++
 tools/kvm/arm/aarch64/kvm-cpu.c                  | 28 ++++++++++++++++++++++++
 tools/kvm/arm/fdt.c                              |  9 +++++---
 tools/kvm/arm/include/arm-common/kvm-cpu-arch.h  |  2 ++
 6 files changed, 70 insertions(+), 3 deletions(-)
tools/kvm/arm/aarch32/include/kvm/kvm-cpu-arch.h +4 -0
index b9fda07d1e55..d28ea676bb73 100644
@@ -9,4 +9,8 @@
 	[0] = (!!(cpuid) << KVM_ARM_VCPU_POWER_OFF),			\
 }
 
+#define ARM_MPIDR_HWID_BITMASK	0xFFFFFF
+#define ARM_CPU_ID		0, 0, 0
+#define ARM_CPU_ID_MPIDR	5
+
 #endif /* KVM__KVM_CPU_ARCH_H */
tools/kvm/arm/aarch32/kvm-cpu.c +26 -0
index 6a012db871ed..bd71037e7c89 100644
@@ -6,6 +6,32 @@
 #define ARM_CORE_REG(x)	(KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE | \
 			 KVM_REG_ARM_CORE_REG(x))
 
+#define ARM_CP15_REG_SHIFT_MASK(x,n)		\
+	(((x) << KVM_REG_ARM_ ## n ## _SHIFT) & KVM_REG_ARM_ ## n ## _MASK)
+
+#define __ARM_CP15_REG(op1,crn,crm,op2)			\
+	(KVM_REG_ARM | KVM_REG_SIZE_U32		|	\
+	 (15 << KVM_REG_ARM_COPROC_SHIFT)	|	\
+	 ARM_CP15_REG_SHIFT_MASK(op1, OPC1)	|	\
+	 ARM_CP15_REG_SHIFT_MASK(crn, 32_CRN)	|	\
+	 ARM_CP15_REG_SHIFT_MASK(crm, CRM)	|	\
+	 ARM_CP15_REG_SHIFT_MASK(op2, 32_OPC2))
+
+#define ARM_CP15_REG(...)	__ARM_CP15_REG(__VA_ARGS__)
+
+unsigned long kvm_cpu__get_vcpu_mpidr(struct kvm_cpu *vcpu)
+{
+	struct kvm_one_reg reg;
+	u32 mpidr;
+
+	reg.id = ARM_CP15_REG(ARM_CPU_ID, ARM_CPU_ID_MPIDR);
+	reg.addr = (u64)(unsigned long)&mpidr;
+	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
+		die("KVM_GET_ONE_REG failed (get_mpidr vcpu%ld", vcpu->cpu_id);
+
+	return mpidr;
+}
+
 void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
 {
 	struct kvm *kvm	= vcpu->kvm;
tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h +4 -0
index d85c583421c5..7d70c3baa53a 100644
@@ -10,4 +10,8 @@
 	       (!!(kvm)->cfg.arch.aarch32_guest << KVM_ARM_VCPU_EL1_32BIT))	\
 }
 
+#define ARM_MPIDR_HWID_BITMASK	0xFF00FFFFFFUL
+#define ARM_CPU_ID		3, 0, 0, 0
+#define ARM_CPU_ID_MPIDR	5
+
 #endif /* KVM__KVM_CPU_ARCH_H */
tools/kvm/arm/aarch64/kvm-cpu.c +28 -0
index 7cdcb70e6fa8..059e42c14fd2 100644
@@ -10,6 +10,34 @@
 #define ARM64_CORE_REG(x)	(KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
 				 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
 
+#define ARM64_SYS_REG_SHIFT_MASK(x,n)				\
+	(((x) << KVM_REG_ARM64_SYSREG_ ## n ## _SHIFT) &	\
+	 KVM_REG_ARM64_SYSREG_ ## n ## _MASK)
+
+#define __ARM64_SYS_REG(op0,op1,crn,crm,op2)			\
+	(KVM_REG_ARM64 | KVM_REG_SIZE_U64		|	\
+	 KVM_REG_ARM64_SYSREG				|	\
+	 ARM64_SYS_REG_SHIFT_MASK(op0, OP0)		|	\
+	 ARM64_SYS_REG_SHIFT_MASK(op1, OP1)		|	\
+	 ARM64_SYS_REG_SHIFT_MASK(crn, CRN)		|	\
+	 ARM64_SYS_REG_SHIFT_MASK(crm, CRM)		|	\
+	 ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
+
+#define ARM64_SYS_REG(...)	__ARM64_SYS_REG(__VA_ARGS__)
+
+unsigned long kvm_cpu__get_vcpu_mpidr(struct kvm_cpu *vcpu)
+{
+	struct kvm_one_reg reg;
+	u64 mpidr;
+
+	reg.id = ARM64_SYS_REG(ARM_CPU_ID, ARM_CPU_ID_MPIDR);
+	reg.addr = (u64)&mpidr;
+	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
+		die("KVM_GET_ONE_REG failed (get_mpidr vcpu%ld", vcpu->cpu_id);
+
+	return mpidr;
+}
+
 static void reset_vcpu_aarch32(struct kvm_cpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
tools/kvm/arm/fdt.c +6 -3
index 5e18c11da3d0..9a34d9804930 100644
@@ -52,17 +52,20 @@ static void generate_cpu_nodes(void *fdt, struct kvm *kvm)
 
 	for (cpu = 0; cpu < kvm->nrcpus; ++cpu) {
 		char cpu_name[CPU_NAME_MAX_LEN];
+		struct kvm_cpu *vcpu = kvm->cpus[cpu];
+		unsigned long mpidr = kvm_cpu__get_vcpu_mpidr(vcpu);
 
-		snprintf(cpu_name, CPU_NAME_MAX_LEN, "cpu@%d", cpu);
+		mpidr &= ARM_MPIDR_HWID_BITMASK;
+		snprintf(cpu_name, CPU_NAME_MAX_LEN, "cpu@%lx", mpidr);
 
 		_FDT(fdt_begin_node(fdt, cpu_name));
 		_FDT(fdt_property_string(fdt, "device_type", "cpu"));
-		_FDT(fdt_property_string(fdt, "compatible", kvm->cpus[cpu]->cpu_compatible));
+		_FDT(fdt_property_string(fdt, "compatible", vcpu->cpu_compatible));
 
 		if (kvm->nrcpus > 1)
 			_FDT(fdt_property_string(fdt, "enable-method", "psci"));
 
-		_FDT(fdt_property_cell(fdt, "reg", cpu));
+		_FDT(fdt_property_cell(fdt, "reg", mpidr));
 		_FDT(fdt_end_node(fdt));
 	}
 
tools/kvm/arm/include/arm-common/kvm-cpu-arch.h +2 -0
index b514dd568fc1..bef176145264 100644
@@ -45,4 +45,6 @@ static inline bool kvm_cpu__emulate_io(struct kvm *kvm, u16 port, void *data,
 bool kvm_cpu__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len,
 			   u8 is_write);
 
+unsigned long kvm_cpu__get_vcpu_mpidr(struct kvm_cpu *vcpu);
+
 #endif /* ARM_COMMON__KVM_CPU_ARCH_H */
Signature · 1 line
1.8.2.2
#0 lore raw

cover kvm tools: arm/arm64 updates

Hi Pekka,

Here are some patches I've been sitting on for a while (although I believe they've all been on the list) due to dependencies on kvm tools moving to a more recent kernel. Now that you've merged in 3.13-rc1 (hurrah!), these all apply without problems.

The main features are:

* Support for Cortex-A7
* Architected timer frequency override, as it turns out firmware tends to get this wrong on the host
* Misc cleanup/refactoring which should make future CPU support (e.g. Cortex-A12) trivial once supported by KVM
* A fix to the way in which devicetree CPU nodes are generated

Cheers,

Will

Jonathan Austin (2):
kvm tools: arm: extract common timer support code for ARM cpus kvm tools: arm: add support for ARM Cortex-A7

Marc Zyngier (1):
kvm tools: arm: emit the MPIDR in DT instead of cpu_id

Robin Murphy (2):
kvm tools: support unsigned int options
kvm tools: arm: add option to override generic timer frequency

 tools/kvm/Makefile                                 |  6 +-
 tools/kvm/arm/aarch32/arm-cpu.c                    | 42 ++++++++++++
 tools/kvm/arm/aarch32/cortex-a15.c                 | 61 -----------------
 tools/kvm/arm/aarch32/include/kvm/kvm-cpu-arch.h   |  4 ++
 tools/kvm/arm/aarch32/kvm-cpu.c                    | 26 +++++++
 tools/kvm/arm/aarch64/arm-cpu.c                    | 50 ++++++++++++++
 tools/kvm/arm/aarch64/cortex-a57.c                 | 80 ----------------------
 tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h   |  4 ++
 tools/kvm/arm/aarch64/kvm-cpu.c                    | 28 ++++++++
 tools/kvm/arm/fdt.c                                |  9 ++-
 tools/kvm/arm/include/arm-common/kvm-config-arch.h | 15 ++--
 tools/kvm/arm/include/arm-common/kvm-cpu-arch.h    |  2 +
 tools/kvm/arm/include/arm-common/timer.h           |  6 ++
 tools/kvm/arm/timer.c                              | 40 +++++++++++
 tools/kvm/include/kvm/parse-options.h              |  9 +++
 15 files changed, 230 insertions(+), 152 deletions(-)
 create mode 100644 tools/kvm/arm/aarch32/arm-cpu.c
 delete mode 100644 tools/kvm/arm/aarch32/cortex-a15.c
 create mode 100644 tools/kvm/arm/aarch64/arm-cpu.c
 delete mode 100644 tools/kvm/arm/aarch64/cortex-a57.c
 create mode 100644 tools/kvm/arm/include/arm-common/timer.h
 create mode 100644 tools/kvm/arm/timer.c
Signature · 1 line
1.8.2.2
#1 lore raw

PATCH 1/5 kvm tools: arm: extract common timer support code for ARM cpus

From: Jonathan Austin <[email protected]>

The ARM V7 and V8 CPUs use the nearly identical support code for generating timer DT nodes as they both use ARM's architected timers. This code is currently duplicated for AArch32 and AArch64.

This cleanup patch generalises timer DT node generation to follow the same pattern as for the GIC. The ability of a DT node to contain multiple compatible strings is exploited to allow an identical DT node to be used on V7 and V8 platforms.

---
 tools/kvm/Makefile                       |  6 +--
 tools/kvm/arm/aarch32/arm-cpu.c          | 35 ++++++++++++++
 tools/kvm/arm/aarch32/cortex-a15.c       | 61 ------------------------
 tools/kvm/arm/aarch64/arm-cpu.c          | 50 ++++++++++++++++++++
 tools/kvm/arm/aarch64/cortex-a57.c       | 80 --------------------------------
 tools/kvm/arm/include/arm-common/timer.h |  6 +++
 tools/kvm/arm/timer.c                    | 38 +++++++++++++++
 7 files changed, 132 insertions(+), 144 deletions(-)
 create mode 100644 tools/kvm/arm/aarch32/arm-cpu.c
 delete mode 100644 tools/kvm/arm/aarch32/cortex-a15.c
 create mode 100644 tools/kvm/arm/aarch64/arm-cpu.c
 delete mode 100644 tools/kvm/arm/aarch64/cortex-a57.c
 create mode 100644 tools/kvm/arm/include/arm-common/timer.h
 create mode 100644 tools/kvm/arm/timer.c
tools/kvm/Makefile +3 -3
index b614aab77c59..27fb2fbeae8d 100644
@@ -157,12 +157,12 @@ 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/kvm.o arm/kvm-cpu.o arm/timer.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/arm-cpu.o
 	OBJS		+= arm/aarch32/kvm-cpu.o
 	ARCH_INCLUDE	:= $(HDRS_ARM_COMMON)
 	ARCH_INCLUDE	+= -Iarm/aarch32/include
@@ -175,7 +175,7 @@ endif
 ifeq ($(ARCH), arm64)
 	DEFINES		+= -DCONFIG_ARM64
 	OBJS		+= $(OBJS_ARM_COMMON)
-	OBJS		+= arm/aarch64/cortex-a57.o
+	OBJS		+= arm/aarch64/arm-cpu.o
 	OBJS		+= arm/aarch64/kvm-cpu.o
 	ARCH_INCLUDE	:= $(HDRS_ARM_COMMON)
 	ARCH_INCLUDE	+= -Iarm/aarch64/include
tools/kvm/arm/aarch32/arm-cpu.c new file +35 -0
new file mode 100644
index 000000000000..8817d2a8b772
@@ -0,0 +1,35 @@
+#include "kvm/kvm.h"
+#include "kvm/kvm-cpu.h"
+#include "kvm/util.h"
+
+#include "arm-common/gic.h"
+#include "arm-common/timer.h"
+
+#include <linux/byteorder.h>
+#include <linux/types.h>
+
+static void generate_fdt_nodes(void *fdt, struct kvm *kvm, u32 gic_phandle)
+{
+	int timer_interrupts[4] = {13, 14, 11, 10};
+
+	gic__generate_fdt_nodes(fdt, gic_phandle);
+	timer__generate_fdt_nodes(fdt, kvm, timer_interrupts);
+}
+
+static int arm_cpu__vcpu_init(struct kvm_cpu *vcpu)
+{
+	vcpu->generate_fdt_nodes = generate_fdt_nodes;
+	return 0;
+}
+
+static struct kvm_arm_target target_cortex_a15 = {
+	.id		= KVM_ARM_TARGET_CORTEX_A15,
+	.compatible	= "arm,cortex-a15",
+	.init		= arm_cpu__vcpu_init,
+};
+
+static int arm_cpu__core_init(struct kvm *kvm)
+{
+	return kvm_cpu__register_kvm_arm_target(&target_cortex_a15);
+}
+core_init(arm_cpu__core_init);
tools/kvm/arm/aarch32/cortex-a15.c deleted +0 -61

61 lines not on this page · raw message

tools/kvm/arm/aarch64/arm-cpu.c new file +50 -0

50 lines not on this page · raw message

tools/kvm/arm/aarch64/cortex-a57.c deleted +0 -80

80 lines not on this page · raw message

tools/kvm/arm/include/arm-common/timer.h new file +6 -0
new file mode 100644
index 000000000000..928e9ea7a9fc
@@ -0,0 +1,6 @@
+#ifndef ARM_COMMON__TIMER_H
+#define ARM_COMMON__TIMER_H
+
+void timer__generate_fdt_nodes(void *fdt, struct kvm *kvm, int *irqs);
+
+#endif /* ARM_COMMON__TIMER_H */
tools/kvm/arm/timer.c new file +38 -0
new file mode 100644
index 000000000000..bd6a0bb2c324
@@ -0,0 +1,38 @@
+#include "kvm/fdt.h"
+#include "kvm/kvm.h"
+#include "kvm/kvm-cpu.h"
+#include "kvm/util.h"
+
+#include "arm-common/gic.h"
+#include "arm-common/timer.h"
+
+void timer__generate_fdt_nodes(void *fdt, struct kvm *kvm, int *irqs)
+{
+	const char compatible[] = "arm,armv8-timer\0arm,armv7-timer";
+
+	u32 cpu_mask = (((1 << kvm->nrcpus) - 1) << GIC_FDT_IRQ_PPI_CPU_SHIFT) \
+		       & GIC_FDT_IRQ_PPI_CPU_MASK;
+	u32 irq_prop[] = {
+		cpu_to_fdt32(GIC_FDT_IRQ_TYPE_PPI),
+		cpu_to_fdt32(irqs[0]),
+		cpu_to_fdt32(cpu_mask | GIC_FDT_IRQ_FLAGS_EDGE_LO_HI),
+
+		cpu_to_fdt32(GIC_FDT_IRQ_TYPE_PPI),
+		cpu_to_fdt32(irqs[1]),
+		cpu_to_fdt32(cpu_mask | GIC_FDT_IRQ_FLAGS_EDGE_LO_HI),
+
+		cpu_to_fdt32(GIC_FDT_IRQ_TYPE_PPI),
+		cpu_to_fdt32(irqs[2]),
+		cpu_to_fdt32(cpu_mask | GIC_FDT_IRQ_FLAGS_EDGE_LO_HI),
+
+		cpu_to_fdt32(GIC_FDT_IRQ_TYPE_PPI),
+		cpu_to_fdt32(irqs[3]),
+		cpu_to_fdt32(cpu_mask | GIC_FDT_IRQ_FLAGS_EDGE_LO_HI),
+	};
+
+	_FDT(fdt_begin_node(fdt, "timer"));
+	_FDT(fdt_property(fdt, "compatible", compatible, sizeof(compatible)));
+	_FDT(fdt_property(fdt, "interrupts", irq_prop, sizeof(irq_prop)));
+	_FDT(fdt_end_node(fdt));
+}
+
Signature · 1 line
1.8.2.2