Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp977505pxb; Wed, 1 Sep 2021 14:16:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTTD98Vp3mBDtEeQvYqBBzzqkS4UsUkyUOIOrZDkJ8KWCW82mjvfnhI8jRKqkctbvc8V/W X-Received: by 2002:a05:6638:1613:: with SMTP id x19mr1347175jas.77.1630530968113; Wed, 01 Sep 2021 14:16:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630530968; cv=none; d=google.com; s=arc-20160816; b=eunbcq/4roxcM/KHWTt3mFJmn2IFHksuDIwCrrRsGZj9ixWu0bQb/lnIjpEuAosZPu qNTMvp5exEXdkDPD4pLoGKiQsayGzFnKU7IETx8/aC8/uTRiXRzUDYN8TsimbpT1UdNY Q4POzNEnIozUapN+IAkXlmu7Msy733p+lD1eOsYB5raEuG3koZEm3sc+hACeN/ebI6h+ HuEW70MVYr15YHIKfhR7PAf4XneQ79nC10b3uMagQa5G7sxeDzQz12TlN9AKQcq0nBtF TBuj8qfG1yJD3yK03yR5cAXgm2dwNnqRbPzRGNsEFmxE8C76aqzu2p2jXNmu8z+0gnY4 5vIQ== 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=bqVlFpnmP8NmTTti9CdOhbMypDVSrXZ53ek7S7Y13dg=; b=dLX7IRAK74qYhZlHxp1IfW8oT+xffSDT0tz4/QUzwybCaVzI0RULRCUpsKUHFsU8Ff SGvL8HDYH+dgxrEB7DJWn4MwqTL1F46rg8pWZmqaRFWEa2h6WGRYHIgw090DYwKCFR/T pRZs5Xf1ivzV2tO50/nuH5XeTv/8cisjj218Go7VbjPrTvBFkBRKiP6/5+tG1htom/se gzIpHFezypOyB8Cku+wZV1gbbIjI7vVJhaT7UYFkwTTGA8m3rxjwPm0dY4opA514+jHL 3TX0cBTjRtlj5wcpKiE397c4ogrP9fddRvPn0AtNeD6r60bNgKklST5QmQeYHtT3deWE bmOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AR+RDnLH; 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 l8si618635ilt.110.2021.09.01.14.15.34; Wed, 01 Sep 2021 14:16:08 -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=20161025 header.b=AR+RDnLH; 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 S245517AbhIAVPe (ORCPT + 99 others); Wed, 1 Sep 2021 17:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343900AbhIAVP3 (ORCPT ); Wed, 1 Sep 2021 17:15:29 -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 BB7FCC06179A for ; Wed, 1 Sep 2021 14:14:30 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id g6-20020a655946000000b00255ef826275so358820pgu.23 for ; Wed, 01 Sep 2021 14:14:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=bqVlFpnmP8NmTTti9CdOhbMypDVSrXZ53ek7S7Y13dg=; b=AR+RDnLHNZuVu6FIvRaDOeDqjj39uN9iakX22xaIHLpaOytDQhgjx40e8j9ZNl6RzS wSgDgLBA0CeS9FO09aRCRjcUwfaVNTJrkjMa4RUQ19m5yOwQSapCy+0U3MxFECUV9ht7 INthlaNZa7PH6ekx/zmAF3sTgxATpQ5CWSu4KTRyzo6r30dkkNfTTH1622pPsjkPIAFS LiwzJdoE2PpDyuUCyFXPLTu/JX31eL29eHuJPl8XwVtHC3r7k35FVQVG8PGeY6v9t68G NXxZ9jTChQOtO3NPmeuDEqCa8eCtO0+0NWQenjgAfTYc9gc3nlCizX9UAa4Ia51iDKLZ wjpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=bqVlFpnmP8NmTTti9CdOhbMypDVSrXZ53ek7S7Y13dg=; b=cbituo6hfzHS0BF7A8Oav16CdEkAVhRFN5gZ3Pl30MXpSmH+Ic7b6NV3X7I5+O+Pnj +Uic+4Afubbg80dypS7OTeGWEac4Z56XbrUHfH/Ji+kfb/2u9yAF2nZQvsDnTNFyy764 OFJimYFiyFwIhn9ToyuEHOcVeTRI/DowbZStod4FJ3N7ewVuLFskoIeeocYm8yDyBNjI jGcVRsN+OvzBXbaitR5AzTwLEnUG3qm6D4o+aHId9LVBdW0rLwNMAtWTh+5kxfxvdL+6 Cw3MUhjMETDl2gh550dDeXiYiOJSbGTc8nHjBfN54VJrBEeI+irAYg6jw4YeFy0Cyebz 4bcA== X-Gm-Message-State: AOAM5334dEgsC4MnAdeAIbgJqL9c7DF6gFoy5S+eAc+XdcomnKLm0wX7 HoMo2g6kWIOoODlyDVCoBaB0ODbNOl7J X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a65:5887:: with SMTP id d7mr967741pgu.285.1630530870253; Wed, 01 Sep 2021 14:14:30 -0700 (PDT) Date: Wed, 1 Sep 2021 21:14:04 +0000 In-Reply-To: <20210901211412.4171835-1-rananta@google.com> Message-Id: <20210901211412.4171835-5-rananta@google.com> Mime-Version: 1.0 References: <20210901211412.4171835-1-rananta@google.com> X-Mailer: git-send-email 2.33.0.153.gba50c8fa24-goog Subject: [PATCH v3 04/12] KVM: arm64: selftests: Add basic support for arch_timers From: Raghavendra Rao Ananta To: Paolo Bonzini , Marc Zyngier , 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 --- .../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..9df5b63abc47 --- /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(cntv_cval_el0, cval); + break; + case PHYSICAL: + write_sysreg(cntp_cval_el0, cval); + 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(cntv_tval_el0, tval); + break; + case PHYSICAL: + write_sysreg(cntp_tval_el0, tval); + 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(cntv_ctl_el0, ctl); + break; + case PHYSICAL: + write_sysreg(cntp_ctl_el0, ctl); + 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.153.gba50c8fa24-goog