Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp166991lqb; Tue, 16 Apr 2024 11:50:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXfX6AVbfnqegxNKe4YlkeCCcyWF4IX+N+XhSMhCOxbcZ/DEsKQNNhRrj8KaJ1fGPz/Xk75AC+2P70a8ewA7UKMq3MoaAT+7raKfakoNw== X-Google-Smtp-Source: AGHT+IGGiZFEIXWU3QY+yclcEp8kycp7d2h6gCCbg3hxuGkD6I2p0tt5KFdKixPQvGl0Sb7ghMUq X-Received: by 2002:a0c:cd84:0:b0:69b:7301:8a67 with SMTP id v4-20020a0ccd84000000b0069b73018a67mr7290067qvm.41.1713293410299; Tue, 16 Apr 2024 11:50:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713293410; cv=pass; d=google.com; s=arc-20160816; b=U1dD4SPHDgyKcyt8VCr/cuNFgXUOwlij2ACfGMfOibWcYFWLw3CbL3litXTJn6UI9a BAhkECVl8phG03V5UItcW6c8WylWG/74Sk/PjkDYGpFSDbjTY0RyXxKpVn3nMd+lcbae NQhj9lo/ile+4n/762ajBRnjgya3RIN/ogrwOPhf8Y8xLPRQgDA9KlWgU9rc0P5EUzOU Ah2qAURgob0rn4jxu1zd2ze2gZFrQRoJSQ9zV5RHn2b0kGWiMLShRx1p6ykdmCYl0w+Z AgMFkGqVonWwwpYjgGjCJzUxp9M6I2+oFS9B4dPq9HRKCNYkPvENLJ+ogJwnCPghHYHw SsNA== 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=miPuCJ12mtzzszcREievCfVnZyZXF4zzROB3NKono6Q=; b=iyZau/5xRPHWxQ1AFWVAUrCbmc4k1y3ws6KsdAyPckDN6yzj85nKiXPFfxiPezYx2K K5tS9Mrx5eDS7PEe3VtNK+nhdEWLc4uXxKHATaAm6Rzykj8y+VrsZx1WLB/3q54MM3/d e4jbVXjqBAIu9WmbNPgPRUvDzXu+IOSTnHT/vXfuoLd354zfQ8ypRnl6DujU/6BLm6a2 13xUxZwDrEk8kQvkcTxixSqBiJQWlCkUOsL9F2CS189EfjkmPgzOqwpv6DUZTdd+KvHO z3bsZcbDiu39eVqOFz2osNh0kwVT7vP5T4DwT0nuUcsoaDgwqc2O1K6uNjrKoDRI/epZ 604w==; 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=Hh90rara; 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-147414-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-147414-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id l15-20020a0ce84f000000b0069b1e2d49adsi13327849qvo.589.2024.04.16.11.50.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 11:50:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-147414-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Hh90rara; 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-147414-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-147414-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 D40961C2187E for ; Tue, 16 Apr 2024 18:50:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AFA5413D244; Tue, 16 Apr 2024 18:45:06 +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="Hh90rara" Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (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 42FF913C91E for ; Tue, 16 Apr 2024 18:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713293105; cv=none; b=CyhOdNFL51KM5CHgFTB1nYkBnSqMBu+OFHwV6iyVy6lVFZSijthYDroqMkbwqQyRXOpgS659bj6HKQ6EXq5Mwe4iCwv2hynpA0lj4Ji64JzgOj10P8jL2/nX9wVS9Ee2A95qvISF+qnE1gyj1fBTHaXQFWQFCUZcrXtaOKj/lHQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713293105; c=relaxed/simple; bh=kMQT5NtU/3H2nu/pPUysYDHLMBWHMS5imSpFNSr8k7Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UPm4bgZEPmbSs93oQHZJxCyC9gcUTwOEJ+TV9WbuwMG9Jx6IO9Sd6VcuEinNV9RMufXnh9oLEcG1xW21V2+sDHN2mSqmXf/WPmIlbdm1mjWUKzUAMl4sfyoPunrarYsC3S5sYEZbR6BB129QNaESRVQHxgkVJaZeu9zbFRJgM7A= 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=Hh90rara; arc=none smtp.client-ip=209.85.215.174 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-f174.google.com with SMTP id 41be03b00d2f7-5e42b4bbfa4so2819027a12.1 for ; Tue, 16 Apr 2024 11:45:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1713293103; x=1713897903; 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=Hh90rarak4bXNazgHP9M/I+P/HDCwQG9snUUWhvWMyo96S/MlDQCOeoZ1YW8reHElx fih0vBX715whnEkSJe2R1YwXJHB+oiliK5WVkLMKEzc4yoe1dEyODgGGbZZq4OiQpY+C qfYumXI2pszLETJs1pVCMJ5v/8cG1GIa+wCXHwSenI7I1hk5VlpRWsp+BojGBzIhqftX KToD/r1LXZZzPbcCFCJfm1JvkSr9VFm3blKaJAY91ZyIUfkNJlMJtgsyZd6WPkbXZWDQ GwYvoy0/ayDpOmlecswZOCgedcNq7tJq0vahgmhQxMSFxxjSU4LEe73VMg3TuxZl6w0M f2lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713293103; x=1713897903; 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=e+e9rPW3zncRatLA0QGbPPjTYHrOcqv3xeaVAFnPx5Hy77Y/H5ZACsFo9tELRPxvvT jr7+WvIuJMl3ZrkRIazAyoKS5ATapksSIi92s95WcJI+YYoqUvm7az/DuD5HJ1F4YkcK lax+tyL8hlFlHMXQIvmlx90+uAmwLbfn86ijxjvxtrSJtzC+bFt7kPR64KVZx5RjcpIT iYdF7du8qFjXrCFH9hai9ev0v0rxKtG4482R+Upb8cqOqRbaz9ywLcYieLAdNEaiTbVP amj08f9b/IPJrQWaFoAClMzQqhYuj9+34WIPOPPJ1xC4QavAXIWtC8aMfc7zUiZlDy6O HqJQ== X-Gm-Message-State: AOJu0YzQdYSdj/ARq27CGaUtzVC1g0cKTRfxB5Cu7MCTOO7/3vpu6jOU 2i2JUayoR4v3R9u7qziQ3r0SP5dYluqtI2SMT+LnuW5IWpJp28XTJYsqr1dtt5SND6DNZjr5cH2 i X-Received: by 2002:a17:90a:fd8f:b0:2a2:bbb9:c937 with SMTP id cx15-20020a17090afd8f00b002a2bbb9c937mr12054639pjb.49.1713293102765; Tue, 16 Apr 2024 11:45:02 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id o19-20020a17090aac1300b002a269828bb8sm8883645pjq.40.2024.04.16.11.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 11:45:02 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Andrew Jones , Anup Patel , Ajay Kaher , Albert Ou , Alexandre Ghiti , 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 v7 15/24] RISC-V: KVM: Support 64 bit firmware counters on RV32 Date: Tue, 16 Apr 2024 11:44:12 -0700 Message-Id: <20240416184421.3693802-16-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240416184421.3693802-1-atishp@rivosinc.com> References: <20240416184421.3693802-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