Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1062087pxb; Tue, 14 Sep 2021 15:34:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKfLBGJl7H9e1C4gQ/KrwcOeBhtriTXICLFFOmHV2o/HF4Xxrv3ilEbHNgGBomGj1plGB1 X-Received: by 2002:a6b:3e84:: with SMTP id l126mr16010915ioa.151.1631658851534; Tue, 14 Sep 2021 15:34:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631658851; cv=none; d=google.com; s=arc-20160816; b=XYWbDKiKyphQr8UfQVDtEy36PknlUHrvIx4pDHAAUIfGMT1fFjFWU/rc2FyUHtcLda 7EdiANw0O9FvEYqVxL50XmP4DMFUVD6Afm9NBqhT/RBfDdYwmmEiQ9lz/UaPDo630iWY jbiEMj+3I5YxbAbITZoXl4S4h6Z8towAMp81iWaObX+70CR9Ua1G6L7hDegDBpi0NgTt hJNHuLZ1lk5y9WyL/Vi2/zXxubyjcN5LxXH9+8cFzlefPwWV7Ze84GpjpM6xNFQKJHEF ZqrFTwVA0h61vn0atVHtZWzmA30EzKlTpLu2BFEOS0bQJCU1/zlM4EyznXcW2wE9CA1t uR1Q== 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=DdZbhJsx3TMPbqbog6U814aMbOhRl4pBO3jBDMYzGY5CcmaOve26zQn201yLR4dcF6 hY7imptpN0P2Vtnr0i2DngMrMO+b22S5qhVG49cnyy4sebkOUScItJgkGxeC4UvId6Gg 7EXr1N6QXgh8J0eGsQd8Yyi4N40YOLmTFazUDS/xpE2Bugog3wHrL2uZ096dvo9eRArG ithzfq4d6SSwSeWjL/5VfmapqxU7tHnNeYgCUFqHJ7dl6oDK1iaam0tM98SFdTzmwrQ0 d6Jdbm4E0D5VH8o8xCmosVkWREdE/Wro6DtK7ikPm9muYloCfduR6mWOkV3tAg2NRb5u MGag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Cxqjrt6M; 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 z10si8470679ioe.51.2021.09.14.15.33.56; Tue, 14 Sep 2021 15:34:11 -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=Cxqjrt6M; 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 S235770AbhINWdn (ORCPT + 99 others); Tue, 14 Sep 2021 18:33:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235789AbhINWcy (ORCPT ); Tue, 14 Sep 2021 18:32:54 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 684F3C061762 for ; Tue, 14 Sep 2021 15:31:36 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id c2-20020a63d5020000b029023ae853b72cso350935pgg.18 for ; Tue, 14 Sep 2021 15:31:36 -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=Cxqjrt6MIDY5BNziJtdIMob/7ap8FTBnmolK0zN46M/i5VFR6lgkp5WFw5tU1+tGZ2 Q4R3ZfC09qMRBSwEIFNQHWkEVwJ9QCmyBEc/4BWoDNwLxKTZxO+kMhg5yMdAlSmatyQr /pn/EckAp+GXZagsGDWHoubuDck6Co+v7eqNn4pfm5gVG9KBA7Xte2g2EvI72FeEHwuR GZ/xexlBKrLjOkq26YGzApDeKNlE+rB44ckrMx6poTyTisSY8w1dKZsk7B/tgaUCxbMs UAIPrSMdzDLWj6oIVDQFDeybxv5edPZq0kfEQgw6oVpcMlLw9i7EO1BpGanUi7by5cG4 OD7g== 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=qz2z9tTDI7/3TCD+NRbrFDJDkG/lFAMLyzPzfqE1yAHUpBzN7+oLitCEGFycEgWRGO QgiME91kv0u84DiGwJsaWwZZLBgvWiWzFg+7g6A/BW90F/crV8aFEk/RyAHkud1baPZA d69WfR5sRsYDPjlIO3MLHBd2mwwAqdTH8FzQ1brEqEXGdh1YAjobFnunfcvaQztpeO7e vYV13SQNw9X2SueqM21VTOW7g1dJndG9ksNDHo8yORKnoI9Fvdrzpjdm6XSPjSXKbzWR tT1SvPgTci75U9wNr9Gd0S2vhGZoolgVgQ5MA7BdWMSPtFXh/DHYbLnKvXe3rLwm2Z/o j+zA== X-Gm-Message-State: AOAM533VzknMaynxmiz0qtLqBb9fnDJnAoEoqeU80FujEVFFG2rqAkpD qMUG9EED69BPrbbFwNA8tgDBaVKQCfGX X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a17:90b:fd7:: with SMTP id gd23mr57307pjb.1.1631658695556; Tue, 14 Sep 2021 15:31:35 -0700 (PDT) Date: Tue, 14 Sep 2021 22:31:05 +0000 In-Reply-To: <20210914223114.435273-1-rananta@google.com> Message-Id: <20210914223114.435273-7-rananta@google.com> Mime-Version: 1.0 References: <20210914223114.435273-1-rananta@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH v7 06/15] 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