From patchwork Thu Dec 8 17:50:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 704152 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tZNHp52vZz9t2G for ; Fri, 9 Dec 2016 04:52:54 +1100 (AEDT) Received: from localhost ([::1]:48011 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cF2sG-0008WC-EU for incoming@patchwork.ozlabs.org; Thu, 08 Dec 2016 12:52:52 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59778) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cF2qI-00038f-9K for qemu-devel@nongnu.org; Thu, 08 Dec 2016 12:50:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cF2qH-0004AZ-9k for qemu-devel@nongnu.org; Thu, 08 Dec 2016 12:50:50 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47410) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cF2qC-00047S-1g; Thu, 08 Dec 2016 12:50:44 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DF8A180514; Thu, 8 Dec 2016 17:50:42 +0000 (UTC) Received: from kamzik.brq.redhat.com (kamzik.brq.redhat.com [10.34.1.143]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uB8HoWn4022068; Thu, 8 Dec 2016 12:50:40 -0500 From: Andrew Jones To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, qemu-devel@nongnu.org, qemu-arm@nongnu.org Date: Thu, 8 Dec 2016 18:50:23 +0100 Message-Id: <20161208175030.12269-4-drjones@redhat.com> In-Reply-To: <20161208175030.12269-1-drjones@redhat.com> References: <20161208175030.12269-1-drjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 08 Dec 2016 17:50:43 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH kvm-unit-tests v8 03/10] arm/arm64: add some delay routines X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, marc.zyngier@arm.com, andre.przywara@arm.com, eric.auger@redhat.com, pbonzini@redhat.com, alex.bennee@linaro.org, christoffer.dall@linaro.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Allow a thread to wait some specified amount of time. Can specify in cycles, usecs, and msecs. Signed-off-by: Andrew Jones Reviewed-by: Andre Przywara --- v8: rewrote basing on new sysreg framework. Also decided delay functions warrant their own files (delay.[ch]) --- arm/Makefile.common | 1 + lib/arm/asm/delay.h | 14 ++++++++++++++ lib/arm/asm/processor.h | 15 +++++++++++++++ lib/arm64/asm/delay.h | 1 + lib/arm64/asm/processor.h | 12 ++++++++++++ lib/arm/delay.c | 29 +++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 lib/arm/asm/delay.h create mode 100644 lib/arm64/asm/delay.h create mode 100644 lib/arm/delay.c diff --git a/arm/Makefile.common b/arm/Makefile.common index b2c0fc8a2fdc..89fe3f69eb44 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -48,6 +48,7 @@ cflatobjs += lib/arm/mmu.o cflatobjs += lib/arm/bitops.o cflatobjs += lib/arm/psci.o cflatobjs += lib/arm/smp.o +cflatobjs += lib/arm/delay.o libeabi = lib/arm/libeabi.a eabiobjs = lib/arm/eabi_compat.o diff --git a/lib/arm/asm/delay.h b/lib/arm/asm/delay.h new file mode 100644 index 000000000000..2436b28c77ae --- /dev/null +++ b/lib/arm/asm/delay.h @@ -0,0 +1,14 @@ +#ifndef _ASMARM_DELAY_H_ +#define _ASMARM_DELAY_H_ +/* + * Copyright (C) 2016, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ +#include + +extern void delay(u64 cycles); +extern void udelay(unsigned long usecs); +extern void mdelay(unsigned long msecs); + +#endif /* _ASMARM_DELAY_H_ */ diff --git a/lib/arm/asm/processor.h b/lib/arm/asm/processor.h index 6b0d36b87817..857bdd96a3cc 100644 --- a/lib/arm/asm/processor.h +++ b/lib/arm/asm/processor.h @@ -7,6 +7,7 @@ */ #include #include +#include enum vector { EXCPTN_RST, @@ -51,4 +52,18 @@ extern int mpidr_to_cpu(uint64_t mpidr); extern void start_usr(void (*func)(void *arg), void *arg, unsigned long sp_usr); extern bool is_user(void); +#define CNTVCT __ACCESS_CP15_64(1, c14) +#define CNTFRQ __ACCESS_CP15(c14, 0, c0, 0) + +static inline u64 get_cntvct(void) +{ + isb(); + return read_sysreg(CNTVCT); +} + +static inline u32 get_cntfrq(void) +{ + return read_sysreg(CNTFRQ); +} + #endif /* _ASMARM_PROCESSOR_H_ */ diff --git a/lib/arm64/asm/delay.h b/lib/arm64/asm/delay.h new file mode 100644 index 000000000000..288e4b3fe610 --- /dev/null +++ b/lib/arm64/asm/delay.h @@ -0,0 +1 @@ +#include "../../arm/asm/delay.h" diff --git a/lib/arm64/asm/processor.h b/lib/arm64/asm/processor.h index 48abf2c9e358..0898d89f9761 100644 --- a/lib/arm64/asm/processor.h +++ b/lib/arm64/asm/processor.h @@ -20,6 +20,7 @@ #include #include #include +#include enum vector { EL1T_SYNC, @@ -83,5 +84,16 @@ extern int mpidr_to_cpu(uint64_t mpidr); extern void start_usr(void (*func)(void *arg), void *arg, unsigned long sp_usr); extern bool is_user(void); +static inline u64 get_cntvct(void) +{ + isb(); + return read_sysreg(cntvct_el0); +} + +static inline u32 get_cntfrq(void) +{ + return read_sysreg(cntfrq_el0); +} + #endif /* !__ASSEMBLY__ */ #endif /* _ASMARM64_PROCESSOR_H_ */ diff --git a/lib/arm/delay.c b/lib/arm/delay.c new file mode 100644 index 000000000000..fa65e2dc9e35 --- /dev/null +++ b/lib/arm/delay.c @@ -0,0 +1,29 @@ +/* + * Delay loops + * + * Copyright (C) 2016, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ +#include +#include +#include + +void delay(u64 cycles) +{ + u64 start = get_cntvct(); + + while ((get_cntvct() - start) < cycles) + cpu_relax(); +} + +void udelay(unsigned long usec) +{ + delay((u64)usec * get_cntfrq() / 1000000); +} + +void mdelay(unsigned long msecs) +{ + while (msecs--) + udelay(1000); +}