Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp101230pxb; Wed, 8 Sep 2021 18:42:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1JNRdtLKPssLfPBn/nslPLDZ9vMulJ56T5QNTw3woAhKlV9TmiDjJlTd2i4nHULOToeUE X-Received: by 2002:a05:6e02:1546:: with SMTP id j6mr395847ilu.154.1631151747191; Wed, 08 Sep 2021 18:42:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631151747; cv=none; d=google.com; s=arc-20160816; b=HdvMUunhXODKhKkirgWa64NgDjhxzhaIzT7LpGAMewemsA5QzJSS2k3LQJR0yYExN6 uN+KAMljjV1oddLqtOUjU/IrS+73beRnQM/67t54wNkOZQfASfUcn+zXh+AW3nEBOd9K G5AIJ/AbsnwRqLXjUz21CTTRtC7T1ErDx1aWsr4O36Dx9f6HJTuJYsU9tLlA0cKSPgae kbjY+bSjRLfu1nORKFiEzA5OLwICCIiyWRUbbFAILOHEiJfjf3OlHVAanl3WdXvzZYNQ 3rxxVp8fxJv5+BqbBTo9R8ffQfLayvqIb7Ynn231GW2x1g0R8eMTNtiorNnwbYk2KqyU LwOA== 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=ulk7lg3SWnBNJFJ99cX5GWqXCF6sjZDZS2dWa8toKKg=; b=QJ3em5xJuPT8+Wg24Jqaj0ACkZ7+gIlKwRYZ/u41UxhCNiSMxM32ijTLMCakMdBR5f csUssXbnQvYT514TlSMpjUddjuuIRxFNKH1zkEveWY7WfXG7Ra18d3EAchenQQA/py68 tHxlqQe9J6QXfCBeIkNtt95e9NcjJgRN6vn6Yfbi4U0yE/QX42dNxzycD8fxVi8Jc6E7 xNkS2ctZZ6Gjnir39B5DhajpOJYbzETHpFzcud82Vhwbq7a9y1xwYlC0sxxXwNMt9/XO fsizigvyuIq/jUislr8Z9go7TdddJzTgzvgGEV2XzOxJ32fsPlEhuKAkH11aYsYob0bv k8gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jAKHD524; 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 v18si324808ilj.94.2021.09.08.18.42.15; Wed, 08 Sep 2021 18:42:27 -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=jAKHD524; 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 S1349501AbhIIBkK (ORCPT + 99 others); Wed, 8 Sep 2021 21:40:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349556AbhIIBjs (ORCPT ); Wed, 8 Sep 2021 21:39:48 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26DC8C0613CF for ; Wed, 8 Sep 2021 18:38:40 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id b84-20020a253457000000b0059e6b730d45so416857yba.6 for ; Wed, 08 Sep 2021 18:38:40 -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=ulk7lg3SWnBNJFJ99cX5GWqXCF6sjZDZS2dWa8toKKg=; b=jAKHD524wMO49NuE4IB1vnqJEKtcCTjhkAwc3lAUtMyKw7Pk9/nLq9TvVIs7Fu98Xr nSWsoHAe2XeE8Lcln5RyLQ6TtzzvHNH723D9gqRRsYNHzHjZTM0ohw50/OJQx0ba0iq/ VDoROgSTz02R5syU054odn28QPH/NVNwsN95Nx67AY1PpnffxyEtUA5d8Pep3hHETDO5 lmXuKBHKiUbMLZhSKk5VjWX13w/o3OPOiMxDPMoW10YOWiCqhlhROfxubrdXEO3ubUbD SQ9/N1tDjNrG5uM4OOdqLZagmr6EL8nBnD9Q3pqzjIBrGIx2zWPW6WK5ydoB4L+WpGuV jxdQ== 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=ulk7lg3SWnBNJFJ99cX5GWqXCF6sjZDZS2dWa8toKKg=; b=tXZ8TmAVyhBg1RHZMZPsrkt8/sJQPrBsWT9oYsZc8mwZLp+ujHENDv88meTWFIWeUF NO7fqoy0oZWcv9RZ4nqb/oDGvjvMSP3u/jq4wTF+93VeMSjhi/BcLUz1F3vkcysa/3ca +1QQwCrhFvseRLQ0tQN0+/4JCR/kzM3jpuZVKyiqTbmMvlkepmdsan4AKvKSwQuUmozu KJ9ZYhVJCbk37GtLCEcISMc3/PzzGZXXXvUa+GVGwKzLybITFdrQztbG7De6Dx1bccsV ffFF9qIriIzj1HqvNSqQQCRnM7P+xGh7RIPs+cKzHc6rQCHcrjRSsM3LqiEWPMGGXydI y6og== X-Gm-Message-State: AOAM531RGPgYAVnjtcRsoELJBSy8MVCTCDQKsSE1QHF0ENu6jv54wTXM jtxTwTUFwvYB50+ZwP7LPl1+w7TZ3igC X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a5b:791:: with SMTP id b17mr568206ybq.263.1631151519339; Wed, 08 Sep 2021 18:38:39 -0700 (PDT) Date: Thu, 9 Sep 2021 01:38:06 +0000 In-Reply-To: <20210909013818.1191270-1-rananta@google.com> Message-Id: <20210909013818.1191270-7-rananta@google.com> Mime-Version: 1.0 References: <20210909013818.1191270-1-rananta@google.com> X-Mailer: git-send-email 2.33.0.153.gba50c8fa24-goog Subject: [PATCH v4 06/18] 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 --- .../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.153.gba50c8fa24-goog