From patchwork Tue Nov 19 20:29:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13880516 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 AECC61D359E for ; Tue, 19 Nov 2024 20:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732048261; cv=none; b=ECmlL7kRTdhUROsUZuAhgk5zYsQvwLJqucJx9MaT0jUIPNDSof7caSrnBFOIH9kVQAOUj0VgI3Pxob9+V5oKkh45EjcTGyTqlOeCxRGDhPuJ4IKmFngk5TsISTBcD1pxGt3QrN+Nr/WTQvmoCZrQrISShzdRZtuauCsRW7xQ/2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732048261; c=relaxed/simple; bh=G5yi3iNf9DP3MtmXZ7tHjmD2q1hrql1veCYCyXy2ahA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DObDTNYGpoCyeak+6ahuWl1milV2fBLUZD4/5ItQrptez9Gihv4ax+yT2tCwluBt5GJUKbjkTeFx3gmUQjjwGlB/yf+PKNn096HtpRF/CWH2EuO0ImuZ0UXsx8S68KTN0cZjj1BHOEW7KJyiJkIPiT/D2LlVelZT9jnWnL5s680= 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=G2r91jeA; arc=none smtp.client-ip=209.85.214.173 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 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="G2r91jeA" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-21269c8df64so1899265ad.2 for ; Tue, 19 Nov 2024 12:30:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1732048257; x=1732653057; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=bs99zjzT6d12gvM6MWyIJ6XWCfETAhHft1AvWE2UFvk=; b=G2r91jeA/OQC4RS6mdOaVl+FkMzCE7fuHinMYOjECeEFdAA/Slrc1w2HTsBwxnEx/f 7oCSCbXZNdCkAxUw74tpzD797/7mToCg1MR7XyucGFUtJSbdlV35FE551Dr291pBWFdO hKyIFq7woZ87Cf6uBDWgwCgKuBX1n5tuE7AwuvBuxxx36QeGPDQ1BDY44TFxvl+gSgq2 2V5raGVquJNsNyGDfU/Es8DtUMUtnk/zf30gA5kzM8pLi1vLzSTgzY4EnYtbsrYpi3C3 OrssxWFN/YQS3GrXnUkiAcynEDz4Yx3xX4H3s7WysJoL2Rw7JNEVgQfs6qOTm7+yS9D9 IKQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732048257; x=1732653057; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bs99zjzT6d12gvM6MWyIJ6XWCfETAhHft1AvWE2UFvk=; b=ha78atGi/tbDmErQeO6gypH9xB3qrPG4ZfwnII2VKfBbZIp+gBGYX3KOpVrGjeuQ4a CHby3utjihbrvmEilIyXkEi5GmcaCKa1nMP54bu43T1FTzTGfKcti54uwUZyPXsnHThq aYjS/N3GgMkj2RVDrjuAV4eqZ3V451dkEVeT/NtbX5+/rTdJpXvOQNJLw475vxRit71j LTL1hBe19nIlMKtf+Ts3sDhH9YQFGG3PlgPXpLfrVxarZXW+lgiF5PEoMYVBJpkdKg3i qeMUR2cievrZ7AhwsR0b8CA9SvW8QYAXF0Mp0ulK18OZF1o83FUJuz2GG629YEJV3+wX /gOw== X-Forwarded-Encrypted: i=1; AJvYcCXGJI/losssfDxjBmLSxtSAq/PoWGIAPq80dHFVvCxI/VFuUQS+OBucnEhjNNi2Ti7nQD4=@vger.kernel.org X-Gm-Message-State: AOJu0Yykk3Se/5eI1KenUbxI+83NRbpdmrK5LS0AchiQoi/HET9abxKt 51cmVrLw2h3mLDR98F5OcIv/YShz6EbMey7fcgi/YKX+/Q2NwIHo1eO6DwTnNVU= X-Google-Smtp-Source: AGHT+IESHBYfIB+Rw/ycY/laHoMRI9Uw6Iu8jOPbVMITks5jvKXyAK0AlmSBcZiVndbkDkPBOO2Y/A== X-Received: by 2002:a17:903:230d:b0:211:ee35:830c with SMTP id d9443c01a7336-2126a34d311mr3124645ad.15.1732048256798; Tue, 19 Nov 2024 12:30:56 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211d0f34f2fsm79001315ad.159.2024.11.19.12.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 12:30:56 -0800 (PST) From: Atish Patra Date: Tue, 19 Nov 2024 12:29:55 -0800 Subject: [PATCH 7/8] RISC-V: KVM: Implement get event info function Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241119-pmu_event_info-v1-7-a4f9691421f8@rivosinc.com> References: <20241119-pmu_event_info-v1-0-a4f9691421f8@rivosinc.com> In-Reply-To: <20241119-pmu_event_info-v1-0-a4f9691421f8@rivosinc.com> To: Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Mayuresh Chitale Cc: linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Palmer Dabbelt , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Atish Patra X-Mailer: b4 0.15-dev-13183 The new get_event_info funciton allows the guest to query the presence of multiple events with single SBI call. Currently, the perf driver in linux guest invokes it for all the standard SBI PMU events. Support the SBI function implementation in KVM as well. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 3 ++ arch/riscv/kvm/vcpu_pmu.c | 67 +++++++++++++++++++++++++++++++++++ arch/riscv/kvm/vcpu_sbi_pmu.c | 3 ++ 3 files changed, 73 insertions(+) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h index 1d85b6617508..9a930afc8f57 100644 --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -98,6 +98,9 @@ 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, struct kvm_vcpu_sbi_return *retdata); +int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low, + unsigned long saddr_high, unsigned long num_events, + unsigned long flags, struct kvm_vcpu_sbi_return *retdata); void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index efd66835c2b8..a30f7ec31479 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -456,6 +456,73 @@ int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long s return 0; } +int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low, + unsigned long saddr_high, unsigned long num_events, + unsigned long flags, struct kvm_vcpu_sbi_return *retdata) +{ + unsigned long hva; + struct riscv_pmu_event_info *einfo; + int shmem_size = num_events * sizeof(*einfo); + bool writable; + gpa_t shmem; + u32 eidx, etype; + u64 econfig; + int ret; + + if (flags != 0 || (saddr_low & (SZ_16 - 1))) { + ret = SBI_ERR_INVALID_PARAM; + goto out; + } + + shmem = saddr_low; + if (saddr_high != 0) { + if (IS_ENABLED(CONFIG_32BIT)) { + shmem |= ((gpa_t)saddr_high << 32); + } else { + ret = SBI_ERR_INVALID_ADDRESS; + goto out; + } + } + + hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable); + if (kvm_is_error_hva(hva) || !writable) { + ret = SBI_ERR_INVALID_ADDRESS; + goto out; + } + + einfo = kzalloc(shmem_size, GFP_KERNEL); + if (!einfo) + return -ENOMEM; + + ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size); + if (ret) { + ret = SBI_ERR_FAILURE; + goto free_mem; + } + + for (int i = 0; i < num_events; i++) { + eidx = einfo[i].event_idx; + etype = kvm_pmu_get_perf_event_type(eidx); + econfig = kvm_pmu_get_perf_event_config(eidx, einfo[i].event_data); + ret = riscv_pmu_get_event_info(etype, econfig, NULL); + if (ret > 0) + einfo[i].output = 1; + } + + kvm_vcpu_write_guest(vcpu, shmem, einfo, shmem_size); + if (ret) { + ret = SBI_ERR_FAILURE; + goto free_mem; + } + +free_mem: + kfree(einfo); +out: + retdata->err_val = ret; + + return 0; +} + int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_return *retdata) { diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c index e4be34e03e83..a020d979d179 100644 --- a/arch/riscv/kvm/vcpu_sbi_pmu.c +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c @@ -73,6 +73,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM: ret = kvm_riscv_vcpu_pmu_snapshot_set_shmem(vcpu, cp->a0, cp->a1, cp->a2, retdata); break; + case SBI_EXT_PMU_EVENT_GET_INFO: + ret = kvm_riscv_vcpu_pmu_event_info(vcpu, cp->a0, cp->a1, cp->a2, cp->a3, retdata); + break; default: retdata->err_val = SBI_ERR_NOT_SUPPORTED; }