From patchwork Tue Aug 16 07:18:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suraj Jitindar Singh X-Patchwork-Id: 9282735 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 17E5860574 for ; Tue, 16 Aug 2016 07:18:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 025D828682 for ; Tue, 16 Aug 2016 07:18:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB4282868A; Tue, 16 Aug 2016 07:18:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7ADCF28682 for ; Tue, 16 Aug 2016 07:18:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752223AbcHPHSx (ORCPT ); Tue, 16 Aug 2016 03:18:53 -0400 Received: from mail-pa0-f65.google.com ([209.85.220.65]:36845 "EHLO mail-pa0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751544AbcHPHSw (ORCPT ); Tue, 16 Aug 2016 03:18:52 -0400 Received: by mail-pa0-f65.google.com with SMTP id ez1so4844354pab.3; Tue, 16 Aug 2016 00:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1EerQzLiZUfgzexIh07FrWzXSMSH41403eHzd+M2+uw=; b=wiH7yj32rYflELxCa4y2seZYl6QExJf9r0L1qNeCBIO0tQm9Hw64xhYDWXIVZkwQI4 Nygy6/lqyuBYTQJR+UMUHg3nHyPm6p9Z1SrIKI8lcC4iG266UXZR2yL4U9lX7icJ8KDK oJpWB6idovcOYODLdVT791Zl/7y4X5qFB1J+1bwrhAtWpUBM79cLCmhYoT90KPxrbnEB +FQDFsJ7h0sTSCCUaSUzH4g0nThpuSO6ZC1hyu9DN5gkJGATZc69ANP2AqYzDr+PhrfT kPLNwi7Nc5S+Z+kSHehbl0HLWGL5CHwa9qbIENXEZ1qAE1AinlSOlt+FSxU0pX+qB2Dp ZR8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1EerQzLiZUfgzexIh07FrWzXSMSH41403eHzd+M2+uw=; b=X+Ua97IIPnTOUjgp3IJ/wCeocX9IejvJmCa1npmm4MT/N3EuCHWpvChGqRrN53oZaU eNotJHQQbLyfVAfiI4sR0+kOTRppRV6ZFu0asJ1lEmeI+f06AfvvbA9Tn3Npzwe185MH 9T0AuZElwp2DkvpGD5M+b/40zBTwSY69VxVCaq9znWubEBBZIE7nrcVdL+y9/gVpuMhz 2hjKFraYUycOhgm2KBkIdwnAUJFGgWoRz8xMPe8Y9RJRoiAH8jsYgnMDhMEA5GrbKLZd FHEfsyHOpB77OWBrPqYISee0wn6azPLvA1+SKreq56UJa5nw06+pla93YIYpvzjvtE5K CDyg== X-Gm-Message-State: AEkoousj+Nl3VhKYMR+o7ly80cEaCBZhAHgrTXkUunEpZmZ5E8zC40rkmufsqe01ubqEkg== X-Received: by 10.66.164.227 with SMTP id yt3mr4658086pab.117.1471331931896; Tue, 16 Aug 2016 00:18:51 -0700 (PDT) Received: from dyn253.ozlabs.ibm.com ([122.99.82.10]) by smtp.gmail.com with ESMTPSA id fj19sm36452252pab.37.2016.08.16.00.18.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Aug 2016 00:18:51 -0700 (PDT) From: Suraj Jitindar Singh To: kvm@vger.kernel.org Cc: sjitindarsingh@gmail.com, pbonzini@redhat.com, rkrcmar@redhat.com, kvm-ppc@vger.kernel.org, lvivier@redhat.com, thuth@redhat.com, drjones@redhat.com Subject: [kvm-unit-tests PATCH V3 4/5] lib/powerpc: Implement generic sleep function for use in unit tests Date: Tue, 16 Aug 2016 17:18:14 +1000 Message-Id: <1471331895-29887-4-git-send-email-sjitindarsingh@gmail.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1471331895-29887-1-git-send-email-sjitindarsingh@gmail.com> References: <1471331895-29887-1-git-send-email-sjitindarsingh@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It would be nice if we had a generic sleep function which could be used in unit tests, add one. Add the variable tb_hz used to store the time base frequency which is read from the device tree on setup. Add functions msleep, usleep and sleep in processor.c to sleep for a given number of milliseconds, microseconds and time base ticks respectively. Signed-off-by: Suraj Jitindar Singh --- Change Log: V2 -> V3: - Add patch to series Signed-off-by: Suraj Jitindar Singh --- lib/powerpc/asm/processor.h | 19 +++++++++++++++++++ lib/powerpc/asm/setup.h | 2 ++ lib/powerpc/processor.c | 13 +++++++++++++ lib/powerpc/setup.c | 12 ++++++++++++ 4 files changed, 46 insertions(+) diff --git a/lib/powerpc/asm/processor.h b/lib/powerpc/asm/processor.h index 09692bd..9554e08 100644 --- a/lib/powerpc/asm/processor.h +++ b/lib/powerpc/asm/processor.h @@ -1,11 +1,30 @@ #ifndef _ASMPOWERPC_PROCESSOR_H_ #define _ASMPOWERPC_PROCESSOR_H_ +#include #include +#define cpu_relax() asm volatile ("" : : : "memory") + #ifndef __ASSEMBLY__ void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void *); void do_handle_exception(struct pt_regs *regs); #endif /* __ASSEMBLY__ */ +static inline uint64_t get_tb(void) +{ + uint64_t tb; + asm volatile ("mfspr %[tb],268" : [tb] "=r" (tb)); + return tb; +} + +extern void delay(uint64_t cycles); +extern void udelay(uint64_t us); + +static inline void mdelay(uint64_t ms) +{ + while (ms--) + udelay(1000); +} + #endif /* _ASMPOWERPC_PROCESSOR_H_ */ diff --git a/lib/powerpc/asm/setup.h b/lib/powerpc/asm/setup.h index b1e1e5a..23b4156 100644 --- a/lib/powerpc/asm/setup.h +++ b/lib/powerpc/asm/setup.h @@ -11,6 +11,8 @@ extern u32 cpus[NR_CPUS]; extern int nr_cpus; +extern uint64_t tb_hz; + #define NR_MEM_REGIONS 8 #define MR_F_PRIMARY (1U << 0) struct mem_region { diff --git a/lib/powerpc/processor.c b/lib/powerpc/processor.c index a78bc3c..c84bae5 100644 --- a/lib/powerpc/processor.c +++ b/lib/powerpc/processor.c @@ -5,6 +5,7 @@ #include #include #include +#include static struct { void (*func)(struct pt_regs *, void *data); @@ -36,3 +37,15 @@ void do_handle_exception(struct pt_regs *regs) printf("unhandled cpu exception 0x%lx\n", regs->trap); abort(); } + +void delay(uint64_t cycles) +{ + uint64_t start = get_tb(); + while((get_tb() - start) < cycles) + cpu_relax(); +} + +void udelay(uint64_t us) +{ + delay((us * tb_hz) / 1000000); +} diff --git a/lib/powerpc/setup.c b/lib/powerpc/setup.c index e3d2afa..e7fa525 100644 --- a/lib/powerpc/setup.c +++ b/lib/powerpc/setup.c @@ -24,6 +24,7 @@ extern void setup_args_progname(const char *args); u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) }; int nr_cpus; +uint64_t tb_hz; struct mem_region mem_regions[NR_MEM_REGIONS]; phys_addr_t __physical_start, __physical_end; @@ -51,6 +52,17 @@ static void cpu_set(int fdtnode, u32 regval, void *info) } cpus[cpu] = regval; + /* set timebase frequency */ + if (!tb_hz) { + const struct fdt_property *prop; + u32 *data; + prop = fdt_get_property(dt_fdt(), fdtnode, + "timebase-frequency", NULL); + assert(prop != NULL); + data = (u32 *)prop->data; + tb_hz = fdt32_to_cpu(*data); + } + /* set exception stack address for this CPU (in SPGR0) */ asm volatile ("mtsprg0 %[addr]" ::