Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4793561rdb; Fri, 29 Dec 2023 13:53:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IEG/7IcAfSCEpWShZDr+JnW3UWh8ooOqGZ5dRDnx4YScCczKx+SC076+ysHxqFmoRVNhz1A X-Received: by 2002:a6b:6407:0:b0:7ba:ae79:c419 with SMTP id t7-20020a6b6407000000b007baae79c419mr14544684iog.5.1703886805037; Fri, 29 Dec 2023 13:53:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703886805; cv=none; d=google.com; s=arc-20160816; b=vxN///Q+xUDVAVndPDvN366uU+XsoaLeKqxZVoNvgFpurAZX0ZO3HxlKj7TqSUjJuY h48gKhgj3OSzF00dACC0u4k5dqsVDTlsJKoa+1sPiW21jZHI8GMEYpYkCvdimM9iDM1n dPI9SvUhRDkemuK3vW0id9rin/lXAOEUTva0CeN1+txP+35OEtK86wNFfXRdxl60QfBh zIwgBcec0uiC3ZUsKPSAIaTruvM5kDlNSvRRfocRCS774kpoKwVhtcayyXvyM4b+ggZf Efo4mo35NExcyeQ8yGhooVK/htConFZ9JEPODiODrATqBinfdZYID5Us4P8RnN1wSXqp YBIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=tpYxrObtnfGcB1Nw+SGItoa8XcbSGhwW4ZwVbnHzCqQ=; fh=fI5kEua53udNTNan966nBIymbOkavhtrnrSLRiMqZpU=; b=UKoK4r1DanOskoCFDa1PFXFkCZOmJzeBfqj4Gmj0NPkt9WVMsPe/0acIc7lrFcOuFI YTmmwRBdCMOOc53Fca3EsSJFlYeso/SY+1QxhieKKU+8Cm4+oIkuaxUvPwEDTZ1TUOUq fH9IDch5JK09ajKY+fRC0je6cOMrc7u0xz0UAXaCzS+0F1Xo+dN0XtbJKjoE55KhGWDo /mt/khvj1yJlnJH9eAwU2Mh4AgCly8q1X8sF4aMg9Y2S0QqenM91XB+M+Lf9DDeoRcx7 wjUMuyRDMomNW3TsS7lYMnk1G+nMdyA53AY+KGm87vJriPD+uIrzs8mD4ObvKwUZDYn7 hJnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=M8CpVDab; spf=pass (google.com: domain of linux-kernel+bounces-13257-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13257-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ck3-20020a056a00328300b006d28dec60cbsi11211275pfb.251.2023.12.29.13.53.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 13:53:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13257-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=M8CpVDab; spf=pass (google.com: domain of linux-kernel+bounces-13257-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13257-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D08DA281AD9 for ; Fri, 29 Dec 2023 21:53:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C99118628; Fri, 29 Dec 2023 21:50:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="M8CpVDab" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D03A18029 for ; Fri, 29 Dec 2023 21:50:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-5944d1ce0c7so3234693eaf.0 for ; Fri, 29 Dec 2023 13:50:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1703886615; x=1704491415; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tpYxrObtnfGcB1Nw+SGItoa8XcbSGhwW4ZwVbnHzCqQ=; b=M8CpVDabVW2k930K9WEBSAPuY5YkoW/7KT9x5pNcpmV/1TXqOk14wGBIokMnDh3hfw zvYuG+EAZStWS+gY4gTY8SCLLwvEfR1EpMOakz3tORQzMEgi9GW+VUdd+AUwdnLuDzWf QUB0+aBl+OCEGeeOMfjfH1CgwdOlB26ywsf5GrruJHfT5+QiPcUDdkkK7EIWzs77s3Om 6ZEy4H6B5T8qwCr10HQzv2g4gKw+6vyDW/q7EhHMTLwX9L7agXn3nWLcvrpmDAcCM/tw 9hHn03mfgrrHKgvQq3EmC69JK5KoT3TKnzrHBg/M/HtoNKVNrgs1p14Q/iw0e2RuKB+U BeYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703886615; x=1704491415; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tpYxrObtnfGcB1Nw+SGItoa8XcbSGhwW4ZwVbnHzCqQ=; b=gVnqyeSsIDuIjXjrpnzc5l7iUW0MSHCajjbLVJcx+U14+VoUkcCMNw2HYSfTKMuVCd BZImm2aD1xNzg4VkMVaxw1BC3uZdn/ScHce9pZNS98wjXDB7Vre56MBHlwEc4m3+AQi/ Tzx/yMLTl730+d7CUDq4BpE/TwOXjRR+4JdSQF5R7Z0dkvTVp6hXpuxgSPYjkyD+xEJs NAxMRUt3VYuFygXqeccG64dwGTMsSU2yAXXhWFVUNy8m/zxWdRiuADM0W/wkGNkWbmQk QsAEmWbEQhetE7hgUuOm5LoV1x5z2Cbt5L+dj36qJV0/vwX4E4X0/52lGxpFyHkjywdA Ci8A== X-Gm-Message-State: AOJu0YzmrD1fnNerEegyeEANNQM9maRetUsEXCb/T3NIZKyNotPCGpCQ rggexYoClOYmPorge/ssj3vcFgPVnXqKjawVVlP6K0ueUzU= X-Received: by 2002:a05:6820:627:b0:58d:6217:795e with SMTP id e39-20020a056820062700b0058d6217795emr9431871oow.8.1703886615540; Fri, 29 Dec 2023 13:50:15 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id r126-20020a4a4e84000000b00594e32e4364sm1034751ooa.24.2023.12.29.13.50.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 13:50:15 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Albert Ou , Alexandre Ghiti , Andrew Jones , Anup Patel , Atish Patra , Conor Dooley , Guo Ren , Heiko Stuebner , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: [v2 10/10] RISC-V: KVM: Support 64 bit firmware counters on RV32 Date: Fri, 29 Dec 2023 13:49:50 -0800 Message-Id: <20231229214950.4061381-11-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231229214950.4061381-1-atishp@rivosinc.com> References: <20231229214950.4061381-1-atishp@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The SBI v2.0 introduced a fw_read_hi function to read 64 bit firmware counters for RV32 based systems. Add infrastructure to support that. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 4 ++- arch/riscv/kvm/vcpu_pmu.c | 37 ++++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 6 +++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h index af6d0ff5ce41..463c349a9ea5 100644 --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -20,7 +20,7 @@ static_assert(RISCV_KVM_MAX_COUNTERS <= 64); struct kvm_fw_event { /* Current value of the event */ - unsigned long value; + u64 value; /* Event monitoring status */ bool started; @@ -91,6 +91,8 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba struct kvm_vcpu_sbi_return *retdata); int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata); +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx, + struct kvm_vcpu_sbi_return *retdata); void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_pmu_setup_snapshot(struct kvm_vcpu *vcpu, unsigned long saddr_low, unsigned long saddr_high, unsigned long flags, diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index f2bf5b5bdd61..e6ce37819ca2 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -196,6 +196,29 @@ static int pmu_get_pmc_index(struct kvm_pmu *pmu, unsigned long eidx, return kvm_pmu_get_programmable_pmc_index(pmu, eidx, cbase, cmask); } +static int pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx, + unsigned long *out_val) +{ + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; + int fevent_code; + + if (!IS_ENABLED(CONFIG_32BIT)) + return -EINVAL; + + pmc = &kvpmu->pmc[cidx]; + + if (pmc->cinfo.type != SBI_PMU_CTR_TYPE_FW) + return -EINVAL; + + fevent_code = get_event_code(pmc->event_idx); + pmc->counter_val = kvpmu->fw_event[fevent_code].value; + + *out_val = pmc->counter_val >> 32; + + return 0; +} + static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, unsigned long *out_val) { @@ -701,6 +724,18 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba return 0; } +int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx, + struct kvm_vcpu_sbi_return *retdata) +{ + int ret; + + ret = pmu_fw_ctr_read_hi(vcpu, cidx, &retdata->out_val); + if (ret == -EINVAL) + retdata->err_val = SBI_ERR_INVALID_PARAM; + + return 0; +} + int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata) { @@ -774,7 +809,7 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) pmc->cinfo.csr = CSR_CYCLE + i; } else { pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW; - pmc->cinfo.width = BITS_PER_LONG - 1; + pmc->cinfo.width = 63; } } diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c index 9f61136e4bb1..58a0e5587e2a 100644 --- a/arch/riscv/kvm/vcpu_sbi_pmu.c +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c @@ -64,6 +64,12 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, case SBI_EXT_PMU_COUNTER_FW_READ: ret = kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, retdata); break; + case SBI_EXT_PMU_COUNTER_FW_READ_HI: + if (IS_ENABLED(CONFIG_32BIT)) + ret = kvm_riscv_vcpu_pmu_fw_ctr_read_hi(vcpu, cp->a0, retdata); + else + retdata->out_val = 0; + break; case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM: ret = kvm_riscv_vcpu_pmu_setup_snapshot(vcpu, cp->a0, cp->a1, cp->a2, retdata); break; -- 2.34.1