From patchwork Mon Feb 28 09:42:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 12762621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 158B1C433EF for ; Mon, 28 Feb 2022 09:43:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=k5uA/2W5LknkurGg712dkRxFcfDWiZtEp6lNeLGSVtY=; b=qdc7RDorUeqtEE wLIE3EEVmnmLlxnVLbB/04nD47Q1XPgK/VgNQp+ectGkQ8Buv3spU8BBNlJPgjt4GIk1+ODW7StT6 QLfLp+u5Vv6NsZEIv4MNCYxqCT8cvdUBBNifPV5lMiASvU05WnX32w9aEY6K3V/MxveLwKiUrzHyW I8mYbg5+7u13l+qBPxeMH9rdfBaYn3xZR+fcVUZdU+92hZoQjxv9Yc5a7PMrFad95CHb2YIOJnKii hCScwWMJrbVWjFjOb28zD+H9nmcqFCm9pcuKyUNJNJswTz5+LV0BJezHSamzeL7cp/bj/8xrFyC8Q aHyKDWNN20uMELSUlseg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nOcYh-00BIcz-4M; Mon, 28 Feb 2022 09:43:11 +0000 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nOcYP-00BINf-HU for linux-riscv@lists.infradead.org; Mon, 28 Feb 2022 09:42:56 +0000 Received: by mail-oi1-x232.google.com with SMTP id p15so12657578oip.3 for ; Mon, 28 Feb 2022 01:42:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=G0qKKKRBgRaNdfPc1uehv7o+JmYHFw6tz68sq2JtWhM=; b=LWEQsBbo0uuMsIqIyvljJA9+TbP+mkMr4T0oW+c6sdKdUCVhfLia2668el+/EC7L/J 8mPxHKLVNSjfchNi4UtYsZkeMMztoi2FeQ6TRYpg0fj6DILrGNVW5BluJ7OqS/2VAwfE zsXBTA0/C0f0RGHA9RAAIO2Wt+LiAp/OhmobBmlnGbcdC5P9wgjywYvYHFvkrOEOZLA3 r7mZZIWiGIjF9qqN8N8jRocOHd4A6V33+GObtULOkYp3ctRvWgTSxy+2w4WpWRzfAtAs 8fsWDKuzQyDEz8xQ2JYD5oZB3uByf7KywDW5DWOWWx0hqYIfvhX9PuNu5Ni2260qQeNH NJRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=G0qKKKRBgRaNdfPc1uehv7o+JmYHFw6tz68sq2JtWhM=; b=JilyoZhd9zwfKTC4ls4wcMpZezJjekXOJbwgsqv6JI4+UViHBicFU8ifZz56tVy31B 2wyELNzuXQu3GzRr/ellw68flmmz65Akv/+3l7Ti0YixQLZ6l2BU8v6WHEZfzNQnVMJa WatgxEZ4uOVxnFDEYI8nn8IXgas05ZO1oQ4TC5WraJGm2fEicaQScpujn8pCDVxqzsLh C12vJjzLfZ671ydhtkPa4TN70rrjKQATNXuv1G0y2ZCAWEOI3FQPKJHQcyHEY5jjdZz+ 0H8pckOL6b7BuNwyGOzuYge0G3B485574fDgwf7LXxyLew774lyudZk02tV80VzkB0kH ofXw== X-Gm-Message-State: AOAM530n7dn/HBf5dm7lh3USITvtpyKfXxK7Fw9dselvqluh0g180SuX qc0imaqraLtx6lzkyo74xp3YR0iY4dWy/g== X-Google-Smtp-Source: ABdhPJwCT4OOGCBVqoqEjhLyOO7deop4I2mzez1Gsk8u3FyUguLbTB0XXtcw1JMXni+YNEOE3B13gQ== X-Received: by 2002:a05:6808:20a9:b0:2d4:b8de:887d with SMTP id s41-20020a05680820a900b002d4b8de887dmr9718414oiw.33.1646041372765; Mon, 28 Feb 2022 01:42:52 -0800 (PST) Received: from rivos-atish.. (adsl-70-228-75-190.dsl.akrnoh.ameritech.net. [70.228.75.190]) by smtp.gmail.com with ESMTPSA id bx10-20020a0568081b0a00b002d70da1ac54sm5936852oib.19.2022.02.28.01.42.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 01:42:52 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Albert Ou , Atish Patra , kvm-riscv@lists.infradead.org, Anup Patel , Damien Le Moal , devicetree@vger.kernel.org, Jisheng Zhang , Krzysztof Kozlowski , linux-riscv@lists.infradead.org, Palmer Dabbelt , Paul Walmsley , Rob Herring Subject: [RFC PATCH 6/6] RISC-V: KVM: Support sstc extension Date: Mon, 28 Feb 2022 01:42:33 -0800 Message-Id: <20220228094234.3773153-7-atishp@rivosinc.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220228094234.3773153-1-atishp@rivosinc.com> References: <20220228094234.3773153-1-atishp@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220228_014253_659610_62090FDE X-CRM114-Status: GOOD ( 20.71 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Sstc extension allows the guest to program the vstimecmp CSR directly instead of making an SBI call to the hypervisor to program the next event. The timer interrupt is also directly injected to the guest by the hardware in this case. To maintain backward compatibility, the hypervisors also update the vstimecmp in an SBI set_time call if the hardware supports it. Thus, the older kernels in guest also take advantage of the sstc extension. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_host.h | 4 + arch/riscv/include/asm/kvm_vcpu_timer.h | 3 +- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 8 ++ arch/riscv/kvm/vcpu.c | 4 +- arch/riscv/kvm/vcpu_sbi_replace.c | 10 +- arch/riscv/kvm/vcpu_timer.c | 136 +++++++++++++++++++++++- 7 files changed, 158 insertions(+), 8 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 99ef6a120617..fb8c993ba022 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -135,6 +135,7 @@ struct kvm_vcpu_csr { unsigned long hvip; unsigned long vsatp; unsigned long scounteren; + u64 vstimecmp; }; struct kvm_vcpu_arch { @@ -179,6 +180,9 @@ struct kvm_vcpu_arch { /* VCPU Timer */ struct kvm_vcpu_timer timer; + /* VCPU Timer for vstimecmp */ + struct kvm_vcpu_timer vstimer; + /* MMIO instruction details */ struct kvm_mmio_decode mmio_decode; diff --git a/arch/riscv/include/asm/kvm_vcpu_timer.h b/arch/riscv/include/asm/kvm_vcpu_timer.h index 375281eb49e0..10715b81db86 100644 --- a/arch/riscv/include/asm/kvm_vcpu_timer.h +++ b/arch/riscv/include/asm/kvm_vcpu_timer.h @@ -39,6 +39,7 @@ int kvm_riscv_vcpu_timer_init(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_timer_deinit(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_timer_reset(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu); int kvm_riscv_guest_timer_init(struct kvm *kvm); - +bool kvm_riscv_vcpu_timer_pending(struct kvm_vcpu *vcpu); #endif diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index e01678aa2a55..c7c313272c0b 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -97,6 +97,7 @@ enum KVM_RISCV_ISA_EXT_ID { KVM_RISCV_ISA_EXT_H, KVM_RISCV_ISA_EXT_I, KVM_RISCV_ISA_EXT_M, + KVM_RISCV_ISA_EXT_SSTC, KVM_RISCV_ISA_EXT_MAX, }; diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 2e5ca43c8c49..8485f59d2db3 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -13,6 +13,7 @@ #include #include #include +#include long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) @@ -50,6 +51,13 @@ int kvm_arch_hardware_enable(void) csr_write(CSR_HIDELEG, hideleg); csr_write(CSR_HCOUNTEREN, -1UL); + if (cpu_sstc_ext_available) { +#ifdef CONFIG_64BIT + csr_write(CSR_HENVCFG, 1UL<arch.isa); kvm_riscv_vcpu_host_fp_restore(&vcpu->arch.host_context); + kvm_riscv_vcpu_timer_save(vcpu); csr_write(CSR_HGATP, 0); csr->vsstatus = csr_read(CSR_VSSTATUS); diff --git a/arch/riscv/kvm/vcpu_sbi_replace.c b/arch/riscv/kvm/vcpu_sbi_replace.c index 1bc0608a5bfd..e34fc9e1f41b 100644 --- a/arch/riscv/kvm/vcpu_sbi_replace.c +++ b/arch/riscv/kvm/vcpu_sbi_replace.c @@ -30,7 +30,15 @@ static int kvm_sbi_ext_time_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, #else next_cycle = (u64)cp->a0; #endif - kvm_riscv_vcpu_timer_next_event(vcpu, next_cycle); + if (cpu_sstc_ext_available) { +#if __riscv_xlen == 32 + csr_write(CSR_VSTIMECMP, next_cycle & 0xFFFFFFFF); + csr_write(CSR_VSTIMECMPH, next_cycle >> 32); +#else + csr_write(CSR_VSTIMECMP, next_cycle); +#endif + } else + kvm_riscv_vcpu_timer_next_event(vcpu, next_cycle); return ret; } diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c index 5c4c37ff2d48..5647c234fea3 100644 --- a/arch/riscv/kvm/vcpu_timer.c +++ b/arch/riscv/kvm/vcpu_timer.c @@ -14,6 +14,7 @@ #include #include #include +#include static u64 kvm_riscv_current_cycles(struct kvm_guest_timer *gt) { @@ -88,10 +89,66 @@ int kvm_riscv_vcpu_timer_next_event(struct kvm_vcpu *vcpu, u64 ncycles) return 0; } +static enum hrtimer_restart kvm_riscv_vcpu_vstimer_expired(struct hrtimer *h) +{ + u64 delta_ns; + struct kvm_vcpu_timer *vst = container_of(h, struct kvm_vcpu_timer, hrt); + struct kvm_vcpu *vcpu = container_of(vst, struct kvm_vcpu, arch.vstimer); + struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; + + if (kvm_riscv_current_cycles(gt) < vst->next_cycles) { + delta_ns = kvm_riscv_delta_cycles2ns(vst->next_cycles, gt, vst); + hrtimer_forward_now(&vst->hrt, ktime_set(0, delta_ns)); + return HRTIMER_RESTART; + } + + vst->next_set = false; + kvm_vcpu_kick(vcpu); + + return HRTIMER_NORESTART; +} + +bool kvm_riscv_vcpu_timer_pending(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_timer *vst = &vcpu->arch.vstimer; + struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; + u64 vstimecmp_val = vcpu->arch.guest_csr.vstimecmp; + + if (!kvm_riscv_delta_cycles2ns(vstimecmp_val, gt, vst) || + kvm_riscv_vcpu_has_interrupts(vcpu, 1UL << IRQ_VS_TIMER)) + return true; + else + return false; +} + +static void kvm_riscv_vcpu_timer_blocking(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_timer *vst = &vcpu->arch.vstimer; + struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; + u64 delta_ns; + u64 vstimecmp_val = vcpu->arch.guest_csr.vstimecmp; + + if (!vst->init_done) + return; + + delta_ns = kvm_riscv_delta_cycles2ns(vstimecmp_val, gt, vst); + if (delta_ns) { + vst->next_cycles = vstimecmp_val; + hrtimer_start(&vst->hrt, ktime_set(0, delta_ns), HRTIMER_MODE_REL); + vst->next_set = true; + } +} + +static void kvm_riscv_vcpu_timer_unblocking(struct kvm_vcpu *vcpu) +{ + kvm_riscv_vcpu_timer_cancel(&vcpu->arch.vstimer); +} + int kvm_riscv_vcpu_get_reg_timer(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { struct kvm_vcpu_timer *t = &vcpu->arch.timer; + struct kvm_vcpu_timer *vst = &vcpu->arch.vstimer; struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | @@ -112,7 +169,10 @@ int kvm_riscv_vcpu_get_reg_timer(struct kvm_vcpu *vcpu, reg_val = kvm_riscv_current_cycles(gt); break; case KVM_REG_RISCV_TIMER_REG(compare): - reg_val = t->next_cycles; + if (cpu_sstc_ext_available) + reg_val = vst->next_cycles; + else + reg_val = t->next_cycles; break; case KVM_REG_RISCV_TIMER_REG(state): reg_val = (t->next_set) ? KVM_RISCV_TIMER_STATE_ON : @@ -132,6 +192,7 @@ int kvm_riscv_vcpu_set_reg_timer(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { struct kvm_vcpu_timer *t = &vcpu->arch.timer; + struct kvm_vcpu_timer *vst = &vcpu->arch.vstimer; struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | @@ -156,7 +217,10 @@ int kvm_riscv_vcpu_set_reg_timer(struct kvm_vcpu *vcpu, gt->time_delta = reg_val - get_cycles64(); break; case KVM_REG_RISCV_TIMER_REG(compare): - t->next_cycles = reg_val; + if (cpu_sstc_ext_available) + vst->next_cycles = reg_val; + else + t->next_cycles = reg_val; break; case KVM_REG_RISCV_TIMER_REG(state): if (reg_val == KVM_RISCV_TIMER_STATE_ON) @@ -175,8 +239,9 @@ int kvm_riscv_vcpu_set_reg_timer(struct kvm_vcpu *vcpu, int kvm_riscv_vcpu_timer_init(struct kvm_vcpu *vcpu) { struct kvm_vcpu_timer *t = &vcpu->arch.timer; + struct kvm_vcpu_timer *vst = &vcpu->arch.vstimer; - if (t->init_done) + if (t->init_done || vst->init_done) return -EINVAL; hrtimer_init(&t->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); @@ -184,6 +249,11 @@ int kvm_riscv_vcpu_timer_init(struct kvm_vcpu *vcpu) t->init_done = true; t->next_set = false; + hrtimer_init(&vst->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + vst->hrt.function = kvm_riscv_vcpu_vstimer_expired; + vst->init_done = true; + vst->next_set = false; + return 0; } @@ -194,15 +264,21 @@ int kvm_riscv_vcpu_timer_deinit(struct kvm_vcpu *vcpu) ret = kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer); vcpu->arch.timer.init_done = false; + ret = kvm_riscv_vcpu_timer_cancel(&vcpu->arch.vstimer); + vcpu->arch.vstimer.init_done = false; + return ret; } int kvm_riscv_vcpu_timer_reset(struct kvm_vcpu *vcpu) { - return kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer); + kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer); + kvm_riscv_vcpu_timer_cancel(&vcpu->arch.vstimer); + + return 0; } -void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) +static void kvm_riscv_vcpu_update_timedelta(struct kvm_vcpu *vcpu) { struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; @@ -214,6 +290,56 @@ void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) #endif } +void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_timer *vst; + struct kvm_vcpu_csr *csr; + + kvm_riscv_vcpu_update_timedelta(vcpu); + + if (!cpu_sstc_ext_available) + return; + + vst = &vcpu->arch.vstimer; + csr = &vcpu->arch.guest_csr; +#ifdef CONFIG_64BIT + csr_write(CSR_VSTIMECMP, csr->vstimecmp); +#else + csr_write(CSR_VSTIMECMP, (u32)csr->vstimecmp); + csr_write(CSR_VSTIMECMPH, (u32)(csr->vstimecmp >> 32)); +#endif + + /* vstimer should be enabled for the remaining operations */ + if (unlikely(!vst->init_done)) + return; + + if (kvm_vcpu_is_blocking(vcpu)) + kvm_riscv_vcpu_timer_blocking(vcpu); +} + +void kvm_riscv_vcpu_timer_save(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr; + struct kvm_vcpu_timer *vst; + + if (!cpu_sstc_ext_available) + return; + + csr = &vcpu->arch.guest_csr; + vst = &vcpu->arch.vstimer; +#ifdef CONFIG_64BIT + csr->vstimecmp = csr_read(CSR_VSTIMECMP); +#else + csr->vstimecmp = csr_read(CSR_VSTIMECMP); + csr->vstimecmp |= (u64)csr_read(CSR_VSTIMECMPH) >> 32; +#endif + /* vstimer should be enabled for the remaining operations */ + if (unlikely(!vst->init_done)) + return; + + kvm_riscv_vcpu_timer_unblocking(vcpu); +} + int kvm_riscv_guest_timer_init(struct kvm *kvm) { struct kvm_guest_timer *gt = &kvm->arch.timer;