Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp265549pxb; Mon, 13 Sep 2021 18:50:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzyAIVmyFrUgAiiWaDEUjEjw1bKOy5NDWSGww27NJexKotVJklcu/7c46pZuTpzCgrK27kM X-Received: by 2002:a17:907:7f93:: with SMTP id qk19mr15536071ejc.433.1631584235164; Mon, 13 Sep 2021 18:50:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631584235; cv=none; d=google.com; s=arc-20160816; b=OwTHyqqpyrhNAapqs+DFrB+DW3do8p1E5zRa8wchyzYN2x+3pKARphfCRI+252x1e9 UKmtMHGStXsRyHhsGtwFZfo4/YvoyvzMWzAFRpv4LCAdAZ6ueEGe0wCiXzp7TZ3reFlf MsR7GVprdtLzIa0Gfar205HUArjJMki6+txcaDwmDQbauauZ4pkgY/7AKiclszXQ7K8u Sm7dAdw6+6zu9TDygJJ487z4V6oZQ03/8zR0T3C9vUnGMVLuKntrPkj2cJjsMbr6ZCsC bM4sX6NO/2rHd/rLQ82l0vcABTTHN/96P3I0I7Xv2//qeEAVb352pnNg6BmdVBORBr4d UTSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=T9aisqSrZe7Zpv0T/yzDlGfSGVHfKhvu7NylFmNxams=; b=ZCJHh6dwvnDC6GSeHxL9ytP1h+KjTc7ac6fQUxlnvJbQcmhjH3ZD+VOhLXOuDT93aw mqJTypSFdY946B427mpJ1J1ec0zeuJLiq+7qNRILemvgtSXaIVAAnGT4u44eYZrYcl9x uoBoQBAgU1ndjFljWNilp2vKlag85jVHs253+Ea+nbQwM8Wwf2bPzXEM1//YDR/0L3P7 MNg/ct/24bHXXv+lYXKx/Zmy3HMPTE0EdIuw4dmBQmu+gALOketE9uGXrTfv+jPANG43 owvC5s8tWQNecV0Ukog/WFi4zI8Z9Myc5fh14wU5MaO1QxLEr/83AY+VTvuQzn/vqogq 0LuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=U2aSQcO7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 8si8667373ejd.768.2021.09.13.18.50.11; Mon, 13 Sep 2021 18:50:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=U2aSQcO7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347974AbhIMUvm (ORCPT + 99 others); Mon, 13 Sep 2021 16:51:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347910AbhIMUvN (ORCPT ); Mon, 13 Sep 2021 16:51:13 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29EB0C061760 for ; Mon, 13 Sep 2021 13:49:57 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id ci14-20020a056214054e00b0037a75ff56f9so11042311qvb.23 for ; Mon, 13 Sep 2021 13:49:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=T9aisqSrZe7Zpv0T/yzDlGfSGVHfKhvu7NylFmNxams=; b=U2aSQcO7IF/eNgrxE2U1PKMyzcXNYTpA8dyjXxngUFTYIAyGHZQaKe5mB+pm0sd56+ zcwE31/OSEWNP25QNYX+1eNlX2R+6qSK4mJ6f/6EdDEuPMpdp2q5IOLfqrFBFotCJsfM /RsHyWW56TowqEGRbXS1y7f6IrHLtliCa9jiNLGHgPo8B6t0amoVNU0nYrjCKqN928n/ 9ZTh5nFb3cfLGdCtstXLFiNTODxSdp0cIByCDp54Js0PFztmizCt6B8XdONWtzkOS6Is GwwWhWRsUdV6uhCm0UEAw29fYCsGBbAA+cJ+Z9pG77GuRwNajjSHqSMpnZ3zh7PSFQwr p++Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=T9aisqSrZe7Zpv0T/yzDlGfSGVHfKhvu7NylFmNxams=; b=XLR5Cf8QzOGmVcxzg3N9zgmU3En9pBcaVz0x2RbTC/FGgMbdCWNfJuiHZr1uTiTI18 4OgtLkQF1qCiiGOg5YOXL2Sm4iOkX2z/JpAO4Y0VFB+hTW0Xgw+R0z0DlRLGQKMrcVQI ZdoIn2xRYkawJfUBemDm9dCgdD0hgf8vXxpVj1lfeFcutplwV+wqwSJIxfdj4rG5kPc4 7Nd+uoBP2JVUNx/O3etVVwwN6IzqFPoCnYCjUBohZ5l7MPiBg+HAPGjhwg690msteZS7 fQj9IC2fHwnWcMx9ylZJCnlprMEfyutJU17GXdwXC1Ce62J1rs8R4HNRsdkKZQPwfTwt FJsw== X-Gm-Message-State: AOAM530Q4RL8o/Dh5nQF+JmA9X5kra8BDUJp6xjjt0nDL6Ik+wakOkcH sGElUzQTL+yIlW2/g259kSB2b0Ddqjhq X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a0c:9c8a:: with SMTP id i10mr1496124qvf.59.1631566196297; Mon, 13 Sep 2021 13:49:56 -0700 (PDT) Date: Mon, 13 Sep 2021 20:49:22 +0000 In-Reply-To: <20210913204930.130715-1-rananta@google.com> Message-Id: <20210913204930.130715-7-rananta@google.com> Mime-Version: 1.0 References: <20210913204930.130715-1-rananta@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH v5 06/14] KVM: arm64: selftests: Add basic support for arch_timers From: Raghavendra Rao Ananta To: Paolo Bonzini , Marc Zyngier , Andrew Jones , James Morse , Alexandru Elisei , Suzuki K Poulose Cc: Catalin Marinas , Will Deacon , Peter Shier , Ricardo Koller , Oliver Upton , Reiji Watanabe , Jing Zhang , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a minimalistic library support to access the virtual timers, that can be used for simple timing functionalities, such as introducing delays in the guest. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: Andrew Jones --- .../kvm/include/aarch64/arch_timer.h | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/aarch64/arch_timer.h diff --git a/tools/testing/selftests/kvm/include/aarch64/arch_timer.h b/tools/testing/selftests/kvm/include/aarch64/arch_timer.h new file mode 100644 index 000000000000..cb7c03de3a21 --- /dev/null +++ b/tools/testing/selftests/kvm/include/aarch64/arch_timer.h @@ -0,0 +1,142 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * ARM Generic Timer specific interface + */ + +#ifndef SELFTEST_KVM_ARCH_TIMER_H +#define SELFTEST_KVM_ARCH_TIMER_H + +#include "processor.h" + +enum arch_timer { + VIRTUAL, + PHYSICAL, +}; + +#define CTL_ENABLE (1 << 0) +#define CTL_IMASK (1 << 1) +#define CTL_ISTATUS (1 << 2) + +#define msec_to_cycles(msec) \ + (timer_get_cntfrq() * (uint64_t)(msec) / 1000) + +#define usec_to_cycles(usec) \ + (timer_get_cntfrq() * (uint64_t)(usec) / 1000000) + +#define cycles_to_usec(cycles) \ + ((uint64_t)(cycles) * 1000000 / timer_get_cntfrq()) + +static inline uint32_t timer_get_cntfrq(void) +{ + return read_sysreg(cntfrq_el0); +} + +static inline uint64_t timer_get_cntct(enum arch_timer timer) +{ + isb(); + + switch (timer) { + case VIRTUAL: + return read_sysreg(cntvct_el0); + case PHYSICAL: + return read_sysreg(cntpct_el0); + default: + GUEST_ASSERT_1(0, timer); + } + + /* We should not reach here */ + return 0; +} + +static inline void timer_set_cval(enum arch_timer timer, uint64_t cval) +{ + switch (timer) { + case VIRTUAL: + write_sysreg(cval, cntv_cval_el0); + break; + case PHYSICAL: + write_sysreg(cval, cntp_cval_el0); + break; + default: + GUEST_ASSERT_1(0, timer); + } + + isb(); +} + +static inline uint64_t timer_get_cval(enum arch_timer timer) +{ + switch (timer) { + case VIRTUAL: + return read_sysreg(cntv_cval_el0); + case PHYSICAL: + return read_sysreg(cntp_cval_el0); + default: + GUEST_ASSERT_1(0, timer); + } + + /* We should not reach here */ + return 0; +} + +static inline void timer_set_tval(enum arch_timer timer, uint32_t tval) +{ + switch (timer) { + case VIRTUAL: + write_sysreg(tval, cntv_tval_el0); + break; + case PHYSICAL: + write_sysreg(tval, cntp_tval_el0); + break; + default: + GUEST_ASSERT_1(0, timer); + } + + isb(); +} + +static inline void timer_set_ctl(enum arch_timer timer, uint32_t ctl) +{ + switch (timer) { + case VIRTUAL: + write_sysreg(ctl, cntv_ctl_el0); + break; + case PHYSICAL: + write_sysreg(ctl, cntp_ctl_el0); + break; + default: + GUEST_ASSERT_1(0, timer); + } + + isb(); +} + +static inline uint32_t timer_get_ctl(enum arch_timer timer) +{ + switch (timer) { + case VIRTUAL: + return read_sysreg(cntv_ctl_el0); + case PHYSICAL: + return read_sysreg(cntp_ctl_el0); + default: + GUEST_ASSERT_1(0, timer); + } + + /* We should not reach here */ + return 0; +} + +static inline void timer_set_next_cval_ms(enum arch_timer timer, uint32_t msec) +{ + uint64_t now_ct = timer_get_cntct(timer); + uint64_t next_ct = now_ct + msec_to_cycles(msec); + + timer_set_cval(timer, next_ct); +} + +static inline void timer_set_next_tval_ms(enum arch_timer timer, uint32_t msec) +{ + timer_set_tval(timer, msec_to_cycles(msec)); +} + +#endif /* SELFTEST_KVM_ARCH_TIMER_H */ -- 2.33.0.309.g3052b89438-goog