Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp963662lqt; Fri, 19 Apr 2024 16:52:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUM4bPiqEFnfx7JcOTaSYTPsfoN/sikfELZIX0101GJXouDTeMn3XwUshgbJqLrx+yYzz0fTb1XGwDDLLneYbfDC9x78ggb0z9omv00JQ== X-Google-Smtp-Source: AGHT+IHeEiePGopHuxsZ5GIzCjeSk99Dn0o6tQbnpl2IaN495R04y/TA2wa2vtj8buJ4iiciibL2 X-Received: by 2002:a17:907:1b03:b0:a55:6ad9:3c6b with SMTP id mp3-20020a1709071b0300b00a556ad93c6bmr7073829ejc.25.1713570751936; Fri, 19 Apr 2024 16:52:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713570751; cv=pass; d=google.com; s=arc-20160816; b=c6Jo1kZeJErTj64/kPbWyYzpeM0yeVC1Udtsz3L4cZDNOSScFxulJuuTon/1fAq9S/ 56wyMX6XBTmYw/hx5hPoWga4FLRbXdhUZv7SH+hMJPoonUQH3/RQGXsFIKzqMzwP7FQV 8O07hUikF0JHjE5kpuzWHm0lZs1Wf7K0EBrXkNLAJ7JoV1KYcGG10D/HXrO1G401h+fy lWG//+FebgCdCKUg+FUKiQfJQjKRmWRgCvoGfXsxY5BRLcuwYfCkOeQS3sJ3P9tU1534 A3DvHPiTzvTmqfr0g3zELuVHrHOWswH8Hs72cG4ScuDvLWvXoDYQ75AHAzGr+GtJyRyL SL7w== ARC-Message-Signature: i=2; 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=K/zHwGjJUr6D05KIztpGDsyJZSWroCpzob1LwbCt9c8=; fh=T3o9ytfnOI0rfpWkEpCj/g73RiGldXLT4R9OSoqDo9w=; b=RMdKNGmsXrDz0tQmx228ImxEjPFvLSrKRKejM9TBOfziYeh/FGlkoaZwQc2ZAsRBXm faqjg9ay85or7BBnxNQn0HRHflrsJ9Wq1b952hYqKMtwOU/P39mkGn3F7SNxNvr3Fk5Y tSv0Sh2SjXtDRIyqhvb8Kuam55r9W2knqMkBOBr7S+yBPExY+fgzQbxpiCk+G1wDf5hJ pwOIaG33SQPRcfJ7DgIX027VpQD3BTAsRFKIh5a5BNEeevotTexLKmvlwnWip9lx/TgI h06PIsugo6jfLDRfwDCZDyRcetv/H/gqfSBPWTsDPcHWJMYue4gEidIoqSbHFTW1z3Cm Zz9A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=n76RWuKa; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-152038-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152038-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q18-20020a1709066b1200b00a5210154eb2si2752506ejr.195.2024.04.19.16.52.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 16:52:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152038-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=n76RWuKa; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-152038-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152038-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 825291F21D69 for ; Fri, 19 Apr 2024 23:52:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 221ED149DFC; Fri, 19 Apr 2024 23:48:16 +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="n76RWuKa" Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 A88C31494DF for ; Fri, 19 Apr 2024 23:48:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713570495; cv=none; b=GCtSNX34Gh/SrUg+vAjv6Z/R7MVIv/kNGYngiLJlqnCdjtmKD9t+lQWoqs4ocpphbbC2lgr2EgOgeRr/RHkGIXfKj3lFh5mDIoA5HXdXwrf6ZyeZE1OWASHVL69MfjhrvYr0fSt0Frv0wn6QPcP4SsD2v9UvEZ0MMwMwjgIwhQM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713570495; c=relaxed/simple; bh=kMQT5NtU/3H2nu/pPUysYDHLMBWHMS5imSpFNSr8k7Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=B/dFsYpl4EDI27L/aIPXj6bevIyG8NKAMCIQipGP6XkFfZ0TNoPEklRMIFsBibGRc4nWETkCqU05DPfZonXTC5IC9no4eR/PfpIchXvSQLAuOp+h/jQyi6guP6sXOg2UBJbZlFT7UQYlbW+D8ykcgmhAxpcG4ytWnYXekh+dY8U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=n76RWuKa; arc=none smtp.client-ip=209.85.215.180 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-pg1-f180.google.com with SMTP id 41be03b00d2f7-5ce07cf1e5dso1754150a12.2 for ; Fri, 19 Apr 2024 16:48:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1713570492; x=1714175292; 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=K/zHwGjJUr6D05KIztpGDsyJZSWroCpzob1LwbCt9c8=; b=n76RWuKahJGrkdRXqJIEf3XQhiBLYtxZaiuLHZBD7UF+S4ihg7sySC2K/xkijiuUpG DsIMiGhxyekNlr923Y1SblC/gp9ENE9lygp5tANmphgatam8RzeG0rQ06EOOyfQDFAJP ro3DX/TrSDi8sOojxXp+L4SPZKupMCMJASZV9/Mzpp50zCnGsF+RlHnunYTFkKDvGsf2 /cTjGD8nUyLL75Yc1RfAeRDgK/KwuwMO3MgARlDa6KTDvmtI1AgYz0DyeaPhZLQRnFSo 4MWljLs18tSyU9OECINvgpEpNuD3WUrrtm8iFVW4Vc9zDkBoPtS1eODYuaGZKEPH3Spt WrLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713570492; x=1714175292; 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=K/zHwGjJUr6D05KIztpGDsyJZSWroCpzob1LwbCt9c8=; b=OdZ7s0BWaIHpwWLtijHPRq9L0w37QtUvdQaGrkRPM30g7UY6NGYESCdKw0NKHM+NmL iCg6y2ZBoQdrYj2T9LgihniJqpKPpgBsKlBQGkC7e04UafUd3JHd2jx2F6+tlH4+HfLl U6PJ5i0HsYLspqAdRoeB56Jwco9A0GgZKwChu+6+9tyldY46Id6mzrvIF0y6GQFyQgeq IRzMSkm1vDkoYgf8cWiKYK8T+L55VSmfW+Y76DErVypYAC9tt9ZtL70I44FLUbtWv1Tv WvB1A2iza8yMurO5abKodZD61WbKgS5I91GlFiUrNd/cOroQ93gxgoZxAZpjN3sgbxXZ /UJg== X-Gm-Message-State: AOJu0Ywso0zBrxJ/yTNM8w0cw8DhNByTYfb2eei06cHPzKWRWvodJg4n QCXl5h3Yz/QoMVPgBejm2L/370gfN1f4HTzsIqhnLxgL5pQAN1cyo1x0UW4OShsE4DyNP7lhYU2 q X-Received: by 2002:a05:6a20:da8c:b0:1aa:5b05:7925 with SMTP id iy12-20020a056a20da8c00b001aa5b057925mr4706514pzb.4.1713570492271; Fri, 19 Apr 2024 16:48:12 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id w17-20020a170902d11100b001e42f215f33sm3924017plw.85.2024.04.19.16.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 16:48:11 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Andrew Jones , Anup Patel , Ajay Kaher , Albert Ou , Alexandre Ghiti , samuel.holland@sifive.com, Conor Dooley , Juergen Gross , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Shuah Khan , virtualization@lists.linux.dev, Will Deacon , x86@kernel.org Subject: [PATCH v8 15/24] RISC-V: KVM: Support 64 bit firmware counters on RV32 Date: Sat, 20 Apr 2024 08:17:31 -0700 Message-Id: <20240420151741.962500-16-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240420151741.962500-1-atishp@rivosinc.com> References: <20240420151741.962500-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. Reviewed-by: Andrew Jones Reviewed-by: Anup Patel Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 4 ++- arch/riscv/kvm/vcpu_pmu.c | 44 ++++++++++++++++++++++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 6 ++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h index 257f17641e00..55861b5d3382 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_snapshot_set_shmem(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 a801ed52dc9b..e1409ec9afc0 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -197,6 +197,36 @@ 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)) { + pr_warn("%s: should be invoked for only RV32\n", __func__); + return -EINVAL; + } + + if (cidx >= kvm_pmu_num_counters(kvpmu) || cidx == 1) { + pr_warn("Invalid counter id [%ld]during read\n", cidx); + 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) { @@ -705,6 +735,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) { @@ -778,7 +820,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 d3e7625fb2d2..cf111de51bdb 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_snapshot_set_shmem(vcpu, cp->a0, cp->a1, cp->a2, retdata); break; -- 2.34.1