From patchwork Thu Oct 3 05:08:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 11172137 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 047DB14DB for ; Thu, 3 Oct 2019 05:08:25 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id D3195222BE for ; Thu, 3 Oct 2019 05:08:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="cnKd1av4"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=wdc.com header.i=@wdc.com header.b="bi85nejV"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.b="DcFBP7op" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D3195222BE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:In-Reply-To:References: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0lg7jYjGAmy1ks/1xdJefJf6uwpHeFbpQqbprRYVUfo=; b=cnKd1av4ixdigA zDSEaIdBTWcZAooGuLnO3JzCE1xdogdNazk6YCLLhUAFIniLWRVf/8/hxOIwVMbO3d9LULI47Y8kh 5QTku04KOKwhuo/BRvbfTKNyKoNCblaui3sT/AbeqkfNdGHnvPv1GKmEVkxvDoCZ4BdtK9h+zLff1 BjdqaNImTdBqIcDPbHUoos8sDwGKhBg/DHdMsaFtDdwOvKFfHXuSorh6G/0EWkvuKhguZJzIZhjNf ofjC3spMkoliDbFVagvaBKNwgVPXZzFzX5D84482EJUw/4hnVyiLhTpX46GOJ9R6h3Y9Kw8Rae5/6 Hw1TzamsqryoLDt1IZTA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.2 #3 (Red Hat Linux)) id 1iFtLb-0004Iq-Pt; Thu, 03 Oct 2019 05:08:15 +0000 Received: from esa6.hgst.iphmx.com ([216.71.154.45]) by bombadil.infradead.org with esmtps (Exim 4.92.2 #3 (Red Hat Linux)) id 1iFtLY-0004EU-26 for linux-riscv@lists.infradead.org; Thu, 03 Oct 2019 05:08:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1570079293; x=1601615293; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=lOJAywCHTuOrxDJOG+3ZaH/TCwB411tVPRgTGN6Xj4Y=; b=bi85nejVA3DxZ4C/8yWIGfa4sBa7fUBnnTmw1386OYhejVOBaLT/a1D5 wuKz/S/2ihEjFdTvEApcp3meGyaabo3vYnRUwQhlot32Yag81ZwxvQC6I clNcYrCBpbFQ/AOVtMbUm4EwqRzB2HbchRnffk1sMJJ2gYYGHaWHqh4sl 4lWheYrdg0tL1o7Xa9FQPhAmliqfxXswzRfxU5ZV7nblboShT4jGgmzao iKWoryJXXa7MsEPivXzvuB/+2xAiTLdbRe+s0BxemnwjR0tAITZrl5P2C bihWuuUjCnMnpkWvE56jAtcYouNk/ww9XVpzq0IDDVYzpb3lgI5fLHWkr w==; IronPort-SDR: Hq10t2d+V90LaLYoaGZ0cVaEPJv++MWJt0EmkeLyc52Fmx9H9d6B7dfJ189Cx/ohZRySNou3YS 6nIFM4QaIRyrSibPmjbpOE4vjZ/UKVZ8IBltuLrDalLdl772FXg8IYNqnEJOtcZV/xW4DrJ8J9 7aiEF85DcDWawB7VncYPMYuQi8SDDwi38AcU3ySr5R+j3QH5u3rtwGaFm/NfopS3L1qwnLaOum 9w8pm7bHhiZDL9ZLJaLwL8nzwTlGMsyxAi3j5Hb0QnrGS0FlZItyJVpZlCs/+OGChfPtbZqub8 +5Y= X-IronPort-AV: E=Sophos;i="5.67,251,1566835200"; d="scan'208";a="121261265" Received: from mail-co1nam05lp2052.outbound.protection.outlook.com (HELO NAM05-CO1-obe.outbound.protection.outlook.com) ([104.47.48.52]) by ob1.hgst.iphmx.com with ESMTP; 03 Oct 2019 13:08:11 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z5QkATmwIepfoE57hP4OuxteV0vHTne0Ze6DXOK4/HqTZcWlTqav6t7ozNu5W2hjUFs2FFrHoe+ZT9oxMPoYhgO08LIR+A3ZCCG11HhuoJr4CDE7s9YKVw/9pGeEg2AJLCJo5yK7wP0topPIptEUSzs1rE/6bl+iWA+wKzd05uPxMkhnQd1fy962kzclnTGbmrZ2qFdD/bFG3VPqWjwLwCFQK7Z8TEBbBbjIuJ5uWKnJCrQUiAg6lwe8QefRxhjTSGV9b/pbXa71c86ZEwRHax/81XXdWTtGW+xkCf6lSJ1Gf9pfYl/fEo2Tie0+6hXNEcRgk3ZxFwBOMe6O5A1sRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uv0pNyEq0essSb6JkldgDaPmXnUQriI+W5YmGG8s2gA=; b=VgdLx0H0475+vN4MLgFOAjvoYgu4Qj4A0U2MJSm88odk+x13tIafUYMkLPwBVwYn6aWezY63bQetFr1pRi3TYyc/cc82SJN9l/MfCo1oWtKFg2wggyXRtbdcvUbVeeDN3H45qndFdVM0zdz1c3IP2T2dxt/V4EFOz0O9js7BGF6RoJnxZCMu6u66mzftHGQMtmloWyStbMkW30OxW/7jCU3+3UuLEAl6E3QpG2JAdACMn+7pwSFY3pxkEIS6X8CUh309QmKOKg7U2ciDQJ08qq8AzcNXO3opouWgLbyITASs7KiQ2z/fO95+cHicnm6uzEMj+ON88mZF6/PlPvziYw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uv0pNyEq0essSb6JkldgDaPmXnUQriI+W5YmGG8s2gA=; b=DcFBP7opGkG+I10BaghtzQdtr7rtPTpYA7iba6HcVuRz0/sgJOxMdaVnFroFAMaEJLLYGMo7z6cY4AdvFDFa1jM9kmM6aA4Q91DhKw2EJA4gT3pBn7VpcPA/7EXz2z2XEYzH3Gzzt/81X+fTxXGG39wnig8V9pnXyts2oIeha6M= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB6272.namprd04.prod.outlook.com (20.178.248.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2305.17; Thu, 3 Oct 2019 05:08:08 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a%7]) with mapi id 15.20.2305.023; Thu, 3 Oct 2019 05:08:08 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K Subject: [PATCH v8 13/19] RISC-V: KVM: Add timer functionality Thread-Topic: [PATCH v8 13/19] RISC-V: KVM: Add timer functionality Thread-Index: AQHVeaiLAuoxqOOF8EGJxcrPYc3oaA== Date: Thu, 3 Oct 2019 05:08:08 +0000 Message-ID: <20191003050558.9031-14-anup.patel@wdc.com> References: <20191003050558.9031-1-anup.patel@wdc.com> In-Reply-To: <20191003050558.9031-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BMXPR01CA0030.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:c::16) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [111.235.74.37] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: f519fe5b-5afd-4bb3-af69-08d747bfae2a x-ms-office365-filtering-ht: Tenant x-ms-traffictypediagnostic: MN2PR04MB6272: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:6108; x-forefront-prvs: 01792087B6 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(136003)(376002)(346002)(366004)(396003)(39860400002)(189003)(199004)(6512007)(6486002)(81166006)(81156014)(8936002)(6116002)(3846002)(8676002)(50226002)(2906002)(110136005)(66066001)(6436002)(316002)(66476007)(66556008)(7416002)(66446008)(36756003)(1076003)(5660300002)(54906003)(7736002)(305945005)(76176011)(25786009)(52116002)(256004)(14444005)(71190400001)(86362001)(99286004)(71200400001)(2616005)(446003)(14454004)(476003)(102836004)(186003)(26005)(11346002)(44832011)(6506007)(486006)(478600001)(4326008)(66946007)(386003)(64756008); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB6272; H:MN2PR04MB6061.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: +PNY7gkXIhjMr99u6xSDEWpysGx9Obda8L7gonjOFiIS5RHdIC4lF5QBcvJRIA28TEXq3fPb1kUQTpewh+JLSJN5lmmoE6xoZGc6nmaat0x3LndpU9j1bFBw2BDrjpLNI/tJ8JT4XFLYrU1xJuBCZO1laMRckNx7oBYknBZW7WBbjvN4RfmLBL0pWLMaClboXRTAXaKuzaG3B1FYZS0osmF3ChVdmUmN90zeeB+TY919WhRxckqGFdz/0V4zq6cG7CWdHw0QFkBucW8xYoASFonxTMoEgaaBVs82sjGyP1C0Yta39xh5YxCGTtTrc8XiFsjQb8EpOGFW+4LrSyNC8gCx2cQu7q4eud9k6hBrrDW8zUp7ZzcIhv0tuI6gZxtfXqcrhYfz4tl0L8JuIglAsGNN+gUwHoD9CtjyYKM4EA0= MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: f519fe5b-5afd-4bb3-af69-08d747bfae2a X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Oct 2019 05:08:08.6081 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: LlnQXbKDEerEuu1zI1kbNhFfJCuarJrlvfEQjB3ck4wsjtuMuQOWdG2a3k5ip6cpyfpDmVLfHrO0VjLvMwQheQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6272 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191002_220812_701623_6D8653A8 X-CRM114-Status: GOOD ( 22.47 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [216.71.154.45 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Le Moal , Anup Patel , "kvm@vger.kernel.org" , Anup Patel , Daniel Lezcano , "linux-kernel@vger.kernel.org" , Christoph Hellwig , Atish Patra , Alexander Graf , Alistair Francis , Thomas Gleixner , "linux-riscv@lists.infradead.org" Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org From: Atish Patra The RISC-V hypervisor specification doesn't have any virtual timer feature. Due to this, the guest VCPU timer will be programmed via SBI calls. The host will use a separate hrtimer event for each guest VCPU to provide timer functionality. We inject a virtual timer interrupt to the guest VCPU whenever the guest VCPU hrtimer event expires. The following features are not supported yet and will be added in future: 1. A time offset to adjust guest time from host time 2. A saved next event in guest vcpu for vm migration Signed-off-by: Atish Patra Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini --- arch/riscv/include/asm/kvm_host.h | 4 + arch/riscv/include/asm/kvm_vcpu_timer.h | 30 +++++++ arch/riscv/kvm/Makefile | 2 +- arch/riscv/kvm/vcpu.c | 6 ++ arch/riscv/kvm/vcpu_timer.c | 113 ++++++++++++++++++++++++ drivers/clocksource/timer-riscv.c | 8 ++ include/clocksource/timer-riscv.h | 16 ++++ 7 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/kvm_vcpu_timer.h create mode 100644 arch/riscv/kvm/vcpu_timer.c create mode 100644 include/clocksource/timer-riscv.h diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 79ceb2aa8ae6..9179ff019235 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef CONFIG_64BIT #define KVM_MAX_VCPUS (1U << 16) @@ -168,6 +169,9 @@ struct kvm_vcpu_arch { unsigned long irqs_pending; unsigned long irqs_pending_mask; + /* VCPU Timer */ + struct kvm_vcpu_timer timer; + /* 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 new file mode 100644 index 000000000000..6f904d49e27e --- /dev/null +++ b/arch/riscv/include/asm/kvm_vcpu_timer.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#ifndef __KVM_VCPU_RISCV_TIMER_H +#define __KVM_VCPU_RISCV_TIMER_H + +#include + +struct kvm_vcpu_timer { + bool init_done; + /* Check if the timer is programmed */ + bool next_set; + u64 next_cycles; + struct hrtimer hrt; + /* Mult & Shift values to get nanosec from cycles */ + u32 mult; + u32 shift; +}; + +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); +int kvm_riscv_vcpu_timer_next_event(struct kvm_vcpu *vcpu, u64 ncycles); + +#endif diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index c0f57f26c13d..3e0c7558320d 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -9,6 +9,6 @@ ccflags-y := -Ivirt/kvm -Iarch/riscv/kvm kvm-objs := $(common-objs-y) kvm-objs += main.o vm.o vmid.o tlb.o mmu.o -kvm-objs += vcpu.o vcpu_exit.o vcpu_switch.o +kvm-objs += vcpu.o vcpu_exit.o vcpu_switch.o vcpu_timer.o obj-$(CONFIG_KVM) += kvm.o diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 12bd837f564a..2ca913f00570 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -54,6 +54,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) memcpy(cntx, reset_cntx, sizeof(*cntx)); + kvm_riscv_vcpu_timer_reset(vcpu); + WRITE_ONCE(vcpu->arch.irqs_pending, 0); WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); } @@ -108,6 +110,9 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) cntx->hstatus |= HSTATUS_SP2P; cntx->hstatus |= HSTATUS_SPV; + /* Setup VCPU timer */ + kvm_riscv_vcpu_timer_init(vcpu); + /* Reset VCPU */ kvm_riscv_reset_vcpu(vcpu); @@ -116,6 +121,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) { + kvm_riscv_vcpu_timer_deinit(vcpu); kvm_riscv_stage2_flush_cache(vcpu); kmem_cache_free(kvm_vcpu_cache, vcpu); } diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c new file mode 100644 index 000000000000..9ffdd6ff8d6e --- /dev/null +++ b/arch/riscv/kvm/vcpu_timer.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include +#include + +#define VCPU_TIMER_PROGRAM_THRESHOLD_NS 1000 + +static enum hrtimer_restart kvm_riscv_vcpu_hrtimer_expired(struct hrtimer *h) +{ + struct kvm_vcpu_timer *t = container_of(h, struct kvm_vcpu_timer, hrt); + struct kvm_vcpu *vcpu = container_of(t, struct kvm_vcpu, arch.timer); + + t->next_set = false; + kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_S_TIMER); + + return HRTIMER_NORESTART; +} + +static u64 kvm_riscv_delta_cycles2ns(u64 cycles, struct kvm_vcpu_timer *t) +{ + unsigned long flags; + u64 cycles_now, cycles_delta, delta_ns; + + local_irq_save(flags); + cycles_now = get_cycles64(); + if (cycles_now < cycles) + cycles_delta = cycles - cycles_now; + else + cycles_delta = 0; + delta_ns = (cycles_delta * t->mult) >> t->shift; + local_irq_restore(flags); + + return delta_ns; +} + +static int kvm_riscv_vcpu_timer_cancel(struct kvm_vcpu_timer *t) +{ + if (!t->init_done || !t->next_set) + return -EINVAL; + + hrtimer_cancel(&t->hrt); + t->next_set = false; + + return 0; +} + +int kvm_riscv_vcpu_timer_next_event(struct kvm_vcpu *vcpu, u64 ncycles) +{ + struct kvm_vcpu_timer *t = &vcpu->arch.timer; + u64 delta_ns = kvm_riscv_delta_cycles2ns(ncycles, t); + + if (!t->init_done) + return -EINVAL; + + if (t->next_set) { + hrtimer_cancel(&t->hrt); + t->next_set = false; + } + + kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_S_TIMER); + + if (delta_ns > VCPU_TIMER_PROGRAM_THRESHOLD_NS) { + hrtimer_start(&t->hrt, ktime_add_ns(ktime_get(), delta_ns), + HRTIMER_MODE_ABS); + t->next_cycles = ncycles; + t->next_set = true; + } else + kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_S_TIMER); + + return 0; +} + +int kvm_riscv_vcpu_timer_init(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_timer *t = &vcpu->arch.timer; + + if (t->init_done) + return -EINVAL; + + hrtimer_init(&t->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); + t->hrt.function = kvm_riscv_vcpu_hrtimer_expired; + t->init_done = true; + t->next_set = false; + + riscv_cs_get_mult_shift(&t->mult, &t->shift); + + return 0; +} + +int kvm_riscv_vcpu_timer_deinit(struct kvm_vcpu *vcpu) +{ + int ret; + + ret = kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer); + vcpu->arch.timer.init_done = false; + + return ret; +} + +int kvm_riscv_vcpu_timer_reset(struct kvm_vcpu *vcpu) +{ + return kvm_riscv_vcpu_timer_cancel(&vcpu->arch.timer); +} diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-riscv.c index 470c7ef02ea4..a48036459f3c 100644 --- a/drivers/clocksource/timer-riscv.c +++ b/drivers/clocksource/timer-riscv.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,13 @@ static int riscv_timer_dying_cpu(unsigned int cpu) return 0; } +void riscv_cs_get_mult_shift(u32 *mult, u32 *shift) +{ + *mult = riscv_clocksource.mult; + *shift = riscv_clocksource.shift; +} +EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift); + /* called directly from the low-level interrupt handler */ void riscv_timer_interrupt(void) { diff --git a/include/clocksource/timer-riscv.h b/include/clocksource/timer-riscv.h new file mode 100644 index 000000000000..e94e4feecbe8 --- /dev/null +++ b/include/clocksource/timer-riscv.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Atish Patra + */ + +#ifndef __TIMER_RISCV_H +#define __TIMER_RISCV_H + +#include + +void riscv_cs_get_mult_shift(u32 *mult, u32 *shift); + +#endif