Received: by 2002:a05:7412:a9a3:b0:f9:327e:43ab with SMTP id o35csp10255rdh; Mon, 18 Dec 2023 02:45:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IES0bhRvrjsi51mZvkifev3y5I2qB2wi6giolRYVX7SUctP2fcbeDtMtBsimvYi9/6HhMLo X-Received: by 2002:a05:6214:262a:b0:67f:1f0c:b311 with SMTP id gv10-20020a056214262a00b0067f1f0cb311mr5415357qvb.29.1702896338197; Mon, 18 Dec 2023 02:45:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702896338; cv=none; d=google.com; s=arc-20160816; b=g0eGCDW2cToPsfOuIDspc9WtG8inmvQbVeGGv72NrWetl5gbkNhYuXy0cndsyBhaxK xc0UfW0+qiT5pIqvKx+cNNwoVOO1u7ch5VeD6pdsc3gXZAeMJFSq5qgtrFdS7PGfDIWU TTAb8hRiTuFoyPUpaFAfSeE9dArN7nVyDPnC9Nw5OrXEd5CasGJe2G6KVEIKdIncj7Ki rY7HZfbm7LPKVn2MOAzAMe6Duccf9QMnMVapM5mWVlRJ+RvlLY/BVXEBirR9V7ATgWED M2BkK4t5clbXMOUrd/rj/C2b7i9F26yIDQqJxNzzqcONpSr0FyGhMHT+97lTl3ECwvDz tWSw== 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=bgKUGLZYGcoLOqmpRwG/psCt/ko1c3dSc+eXyp7wwIo=; fh=edP9Vyhdx3wHlRWwndjClkZeqsrAD5QaYzj9PwYQ7RY=; b=PsaKYN5WFPEeq42Ub0cWJQpPMmuByAguNMdUGEq+mudbR9qNBL14UGhGNpp0pkj3Sy LtODEM8aB7iZeU4Z0n5UEjmkKHANk6B28/wTqNfwwkzNEt22LMRhei5dYXgNTB4zMPIw LEQW1Y6nqRzBHNajGhumQLXZzwlRz78nAOundHPUPL04QXwUOpCFqIcPy//5bp4Vd5Y5 d2d0gewT5h6NPyg0GMmnFpKM5heuPIRCY+F0bp9x4Hwxxrg3Q8y5cqoQMlYVIbTVB7We sUX5GbLW7ule6hi6yoX4QgPldSxzNZPiRdMZWgtgRcQ8TPYDhoQYrJS9j9GVMbwngwyC vy8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RLenRxYF; spf=pass (google.com: domain of linux-kernel+bounces-3358-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3358-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w17-20020a05620a425100b0077fb3183654si7086727qko.40.2023.12.18.02.45.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 02:45:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3358-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RLenRxYF; spf=pass (google.com: domain of linux-kernel+bounces-3358-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3358-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E18D31C22A63 for ; Mon, 18 Dec 2023 10:45:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D4C9225CF; Mon, 18 Dec 2023 10:41:37 +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="RLenRxYF" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oo1-f49.google.com (mail-oo1-f49.google.com [209.85.161.49]) (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 4473120B1B for ; Mon, 18 Dec 2023 10:41:35 +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-f49.google.com with SMTP id 006d021491bc7-58d1b767b2bso1988974eaf.2 for ; Mon, 18 Dec 2023 02:41:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1702896093; x=1703500893; 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=bgKUGLZYGcoLOqmpRwG/psCt/ko1c3dSc+eXyp7wwIo=; b=RLenRxYFw6A3fZx+0Zx3eiixfBQAkmV3OATc9TLtr3Fs9PQfjF5EV3hZVb/fsL1s6B 3+brTwthsveYi74Enw6Q1HUOP9WYamPPxpAO2xiJvDN9YxnZrFEIpI4u1cUVyxt8n3Yt /o2hx3DrQLKddLANqOMSDGv0wX0eqkEwipcd5IOay8lMlMwpBTFxXrLWA/AhZd5ttpqL 78ufyhSWwmlpNHFELjfi4wGORrMjUXLAOxRBOtX0iH+X+XX9KX1H7RLWcBYG4+SxrOQo NG9ViV0qwipTqV6LrGp9bWjviFDGCjBBc1rWAABo60V/9jJcJmbT/l2YXj3FqwDPjFuA /sMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702896093; x=1703500893; 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=bgKUGLZYGcoLOqmpRwG/psCt/ko1c3dSc+eXyp7wwIo=; b=K0ZtK7I1mIhiP6Zgxl63nbVffRG0em4AiatEHZz8otavQGqxSounC6mUuD/xwMtoo7 xszDkDxQglpkeCMBYGysW9KvAAebe3m9+D529gRDnNmjO5YCmJ+XjgjnhC6R8zoRUlP/ SE1C17KbjoarnkI+6wvvhJbKxhwkP4Q5RIgXUrivps4AcvodX6PMLrIPzgwuAYGhKAkZ kYPO8LfFtXll/ciQIgGFO/gw3NGwJMEKuXPXm2dU9XtaKqe0ml1SoFCAabPQXt4dM4pg zwjt/QZ0f0Av7ObwY/e+w5ct5tYw58vSUQ1psmGOA4POD1g4zNh15xlIgz56nqWzfCO6 xgaQ== X-Gm-Message-State: AOJu0YwrlUZ6YnHZESLfKGBvHM6+U+qHMIGYtvrVVtpSvYRepQlCJeej ipx5L20wtaJGlY1oRn8uLz1k0lPRrqNGUXV+yfs= X-Received: by 2002:a4a:ab8e:0:b0:591:98d0:c5e0 with SMTP id m14-20020a4aab8e000000b0059198d0c5e0mr2860672oon.11.1702896093564; Mon, 18 Dec 2023 02:41:33 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 185-20020a4a1ac2000000b005907ad9f302sm574970oof.37.2023.12.18.02.41.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 02:41:33 -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 , Icenowy Zheng , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: [v1 10/10] RISC-V: KVM: Support 64 bit firmware counters on RV32 Date: Mon, 18 Dec 2023 02:41:07 -0800 Message-Id: <20231218104107.2976925-11-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231218104107.2976925-1-atishp@rivosinc.com> References: <20231218104107.2976925-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 931887e07a66..5da5df21ec54 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; + uint64_t 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 368c3a8a8d4a..5c21035e284d 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