From nobody Tue Nov 26 08:54:22 2024 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 11D2B199FA0 for ; Sun, 20 Oct 2024 19:48:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453684; cv=none; b=IVaF5ta+JFUYAwLOL94dNHd+8EtNXuUMfGGYnBEoBAoi2HwZX40T9PNvf1omm8pGkkhX6WqUnEg89cKDW3r579SA2a7Ncx56toLtYJ7J5ZaBK7NuydyBgUUKuekhj+XUvQQz/YssUGJiLVlUSjqzIVVwzu9Mjr/xvJYgHeyapJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453684; c=relaxed/simple; bh=mLSaXF8SeaTzcuFAeNbbcWgZuvjL1wd64f2tvLOHLpU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PLv3b39ebHXmaShpOr0l4rNfUB6KdDKYABkH0V4IwR52eFdwkiCZO+F1BndOhTwYUefbFbgi6LjtKtlOS5+60ptmt3Fq23qZmk1xnsvPLBmy1uLhT6COZVO8DMUws4SRVwQAiNkroEaWjaRyP1RrBCMWRpGx8z4Am61Uzo5wbms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=SWHbH/UC; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="SWHbH/UC" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2e2a97c2681so2762070a91.2 for ; Sun, 20 Oct 2024 12:48:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453681; x=1730058481; 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=ff+UABGVAF8CyMX5ftgz7r8rHiofHtO4g4dtHWoIbV8=; b=SWHbH/UCUyCZGHbHmHk+GGz4L2UzVZBPL6JDoO2pSj0vBazFm5qwqSnV4Wo9tmJ3OW AXxSRyaxaZFoMr9+4lCpsiNTmMYKLp8MzfWSG/qx+ytc9Rfd+nwVELM4Obr+l1qzmLZJ G3MdNFvbL0mraS4DBIo2EGcTpsG2xp++pDmXKUWLHxLf9/W3eA0RWQ8rZdVeRy8Pr4ml tpd5GuX3OGZwskK7jW6JLaSHynRN3P2ErOltzJ6wEAkWpebrnqzGc+z9fc30x1kRScbQ DmWp7C2QvKqkwoBeaLeFZbC7iIFPimtaJo90NJ3iKEt4z1mNubfO/sjcXQlzoFb49k53 XCww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453681; x=1730058481; 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=ff+UABGVAF8CyMX5ftgz7r8rHiofHtO4g4dtHWoIbV8=; b=ODgNUIu4C91gFR+JzmS95dTzq5Uj11PSowWjqp1klPYcufsXpTxlFF6doKerxHSysP aFp3qNbZlmlUuwBT8J9PyXryXPwdvNhlGf4bRg7ZOQ79teeojPf2mjzWNn+BGbc02/Sl vhzxY/80zwgJFlkzliozHqSZ9xi8DcWWTYHE/Ky0JDcxJeUBsXvd4yQI2IEjQ6wAq85R +jyQtAMdqV2+ZJvrWs5Vuhzo7JF/mvZVOwZgOdozTsr5bf8yHq4JuijL2YomH8OwYKoh MtNbMoqMsqoh2qUqYElCcLgYslWLIFNwMvYJyuCHEVeoBsNXX/ux0dLhBAVRMOE6rARU Lphw== X-Forwarded-Encrypted: i=1; AJvYcCXsEX2WI0O2dQsmIngoYlVwrV1Et1xePp8sx6h1HUi2P2y8laGi8gsDbJxFG39Ap6Y3jg4L/virTmyD82o=@vger.kernel.org X-Gm-Message-State: AOJu0YynArQvUztLRrYmRc/+xbiSzuDMcoO5U0RCpwElQPxMUAlspnXD pLwkASvgCbJLBnr+N/iRP2rkj0QpKPMYZGDguSNRfCfty+B1hRUX7EgWEoPrA1c= X-Google-Smtp-Source: AGHT+IF4l6B+I6hyFAY3AFIr33vpTZflrnmvY2Oa86iEJb9FRtrh1tBmUfS3dKy06fjYqz44j4hTMA== X-Received: by 2002:a17:90b:1d85:b0:2e2:b937:eeae with SMTP id 98e67ed59e1d1-2e5616db601mr11934750a91.5.1729453681266; Sun, 20 Oct 2024 12:48:01 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([50.238.223.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e5ad365d4dsm1933188a91.14.2024.10.20.12.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:48:00 -0700 (PDT) From: Anup Patel To: Palmer Dabbelt , Paul Walmsley Cc: Atish Patra , Andrew Jones , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra Subject: [PATCH v2 09/13] RISC-V: KVM: Use nacl_csr_xyz() for accessing H-extension CSRs Date: Mon, 21 Oct 2024 01:17:30 +0530 Message-ID: <20241020194734.58686-10-apatel@ventanamicro.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241020194734.58686-1-apatel@ventanamicro.com> References: <20241020194734.58686-1-apatel@ventanamicro.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When running under some other hypervisor, prefer nacl_csr_xyz() for accessing H-extension CSRs in the run-loop. This makes CSR access faster whenever SBI nested acceleration is available. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/mmu.c | 4 +- arch/riscv/kvm/vcpu.c | 103 +++++++++++++++++++++++++----------- arch/riscv/kvm/vcpu_timer.c | 28 +++++----- 3 files changed, 87 insertions(+), 48 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index b63650f9b966..45ace9138947 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include =20 @@ -732,7 +732,7 @@ void kvm_riscv_gstage_update_hgatp(struct kvm_vcpu *vcp= u) hgatp |=3D (READ_ONCE(k->vmid.vmid) << HGATP_VMID_SHIFT) & HGATP_VMID; hgatp |=3D (k->pgd_phys >> PAGE_SHIFT) & HGATP_PPN; =20 - csr_write(CSR_HGATP, hgatp); + ncsr_write(CSR_HGATP, hgatp); =20 if (!kvm_riscv_gstage_vmid_bits()) kvm_riscv_local_hfence_gvma_all(); diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index a972dd46e7e4..0aad58f984ff 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -17,8 +17,8 @@ #include #include #include -#include #include +#include #include =20 #define CREATE_TRACE_POINTS @@ -368,10 +368,10 @@ void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *= vcpu) struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; =20 /* Read current HVIP and VSIE CSRs */ - csr->vsie =3D csr_read(CSR_VSIE); + csr->vsie =3D ncsr_read(CSR_VSIE); =20 /* Sync-up HVIP.VSSIP bit changes does by Guest */ - hvip =3D csr_read(CSR_HVIP); + hvip =3D ncsr_read(CSR_HVIP); if ((csr->hvip ^ hvip) & (1UL << IRQ_VS_SOFT)) { if (hvip & (1UL << IRQ_VS_SOFT)) { if (!test_and_set_bit(IRQ_VS_SOFT, @@ -572,26 +572,49 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vc= pu *vcpu) =20 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { + void *nsh; struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; struct kvm_vcpu_config *cfg =3D &vcpu->arch.cfg; =20 - csr_write(CSR_VSSTATUS, csr->vsstatus); - csr_write(CSR_VSIE, csr->vsie); - csr_write(CSR_VSTVEC, csr->vstvec); - csr_write(CSR_VSSCRATCH, csr->vsscratch); - csr_write(CSR_VSEPC, csr->vsepc); - csr_write(CSR_VSCAUSE, csr->vscause); - csr_write(CSR_VSTVAL, csr->vstval); - csr_write(CSR_HEDELEG, cfg->hedeleg); - csr_write(CSR_HVIP, csr->hvip); - csr_write(CSR_VSATP, csr->vsatp); - csr_write(CSR_HENVCFG, cfg->henvcfg); - if (IS_ENABLED(CONFIG_32BIT)) - csr_write(CSR_HENVCFGH, cfg->henvcfg >> 32); - if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) { - csr_write(CSR_HSTATEEN0, cfg->hstateen0); + if (kvm_riscv_nacl_sync_csr_available()) { + nsh =3D nacl_shmem(); + nacl_csr_write(nsh, CSR_VSSTATUS, csr->vsstatus); + nacl_csr_write(nsh, CSR_VSIE, csr->vsie); + nacl_csr_write(nsh, CSR_VSTVEC, csr->vstvec); + nacl_csr_write(nsh, CSR_VSSCRATCH, csr->vsscratch); + nacl_csr_write(nsh, CSR_VSEPC, csr->vsepc); + nacl_csr_write(nsh, CSR_VSCAUSE, csr->vscause); + nacl_csr_write(nsh, CSR_VSTVAL, csr->vstval); + nacl_csr_write(nsh, CSR_HEDELEG, cfg->hedeleg); + nacl_csr_write(nsh, CSR_HVIP, csr->hvip); + nacl_csr_write(nsh, CSR_VSATP, csr->vsatp); + nacl_csr_write(nsh, CSR_HENVCFG, cfg->henvcfg); + if (IS_ENABLED(CONFIG_32BIT)) + nacl_csr_write(nsh, CSR_HENVCFGH, cfg->henvcfg >> 32); + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) { + nacl_csr_write(nsh, CSR_HSTATEEN0, cfg->hstateen0); + if (IS_ENABLED(CONFIG_32BIT)) + nacl_csr_write(nsh, CSR_HSTATEEN0H, cfg->hstateen0 >> 32); + } + } else { + csr_write(CSR_VSSTATUS, csr->vsstatus); + csr_write(CSR_VSIE, csr->vsie); + csr_write(CSR_VSTVEC, csr->vstvec); + csr_write(CSR_VSSCRATCH, csr->vsscratch); + csr_write(CSR_VSEPC, csr->vsepc); + csr_write(CSR_VSCAUSE, csr->vscause); + csr_write(CSR_VSTVAL, csr->vstval); + csr_write(CSR_HEDELEG, cfg->hedeleg); + csr_write(CSR_HVIP, csr->hvip); + csr_write(CSR_VSATP, csr->vsatp); + csr_write(CSR_HENVCFG, cfg->henvcfg); if (IS_ENABLED(CONFIG_32BIT)) - csr_write(CSR_HSTATEEN0H, cfg->hstateen0 >> 32); + csr_write(CSR_HENVCFGH, cfg->henvcfg >> 32); + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) { + csr_write(CSR_HSTATEEN0, cfg->hstateen0); + if (IS_ENABLED(CONFIG_32BIT)) + csr_write(CSR_HSTATEEN0H, cfg->hstateen0 >> 32); + } } =20 kvm_riscv_gstage_update_hgatp(vcpu); @@ -614,6 +637,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) =20 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) { + void *nsh; struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; =20 vcpu->cpu =3D -1; @@ -629,15 +653,28 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) vcpu->arch.isa); kvm_riscv_vcpu_host_vector_restore(&vcpu->arch.host_context); =20 - csr->vsstatus =3D csr_read(CSR_VSSTATUS); - csr->vsie =3D csr_read(CSR_VSIE); - csr->vstvec =3D csr_read(CSR_VSTVEC); - csr->vsscratch =3D csr_read(CSR_VSSCRATCH); - csr->vsepc =3D csr_read(CSR_VSEPC); - csr->vscause =3D csr_read(CSR_VSCAUSE); - csr->vstval =3D csr_read(CSR_VSTVAL); - csr->hvip =3D csr_read(CSR_HVIP); - csr->vsatp =3D csr_read(CSR_VSATP); + if (kvm_riscv_nacl_available()) { + nsh =3D nacl_shmem(); + csr->vsstatus =3D nacl_csr_read(nsh, CSR_VSSTATUS); + csr->vsie =3D nacl_csr_read(nsh, CSR_VSIE); + csr->vstvec =3D nacl_csr_read(nsh, CSR_VSTVEC); + csr->vsscratch =3D nacl_csr_read(nsh, CSR_VSSCRATCH); + csr->vsepc =3D nacl_csr_read(nsh, CSR_VSEPC); + csr->vscause =3D nacl_csr_read(nsh, CSR_VSCAUSE); + csr->vstval =3D nacl_csr_read(nsh, CSR_VSTVAL); + csr->hvip =3D nacl_csr_read(nsh, CSR_HVIP); + csr->vsatp =3D nacl_csr_read(nsh, CSR_VSATP); + } else { + csr->vsstatus =3D csr_read(CSR_VSSTATUS); + csr->vsie =3D csr_read(CSR_VSIE); + csr->vstvec =3D csr_read(CSR_VSTVEC); + csr->vsscratch =3D csr_read(CSR_VSSCRATCH); + csr->vsepc =3D csr_read(CSR_VSEPC); + csr->vscause =3D csr_read(CSR_VSCAUSE); + csr->vstval =3D csr_read(CSR_VSTVAL); + csr->hvip =3D csr_read(CSR_HVIP); + csr->vsatp =3D csr_read(CSR_VSATP); + } } =20 static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu) @@ -692,7 +729,7 @@ static void kvm_riscv_update_hvip(struct kvm_vcpu *vcpu) { struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; =20 - csr_write(CSR_HVIP, csr->hvip); + ncsr_write(CSR_HVIP, csr->hvip); kvm_riscv_vcpu_aia_update_hvip(vcpu); } =20 @@ -739,7 +776,9 @@ static void noinstr kvm_riscv_vcpu_enter_exit(struct kv= m_vcpu *vcpu) kvm_riscv_vcpu_swap_in_guest_state(vcpu); guest_state_enter_irqoff(); =20 - hcntx->hstatus =3D csr_swap(CSR_HSTATUS, gcntx->hstatus); + hcntx->hstatus =3D ncsr_swap(CSR_HSTATUS, gcntx->hstatus); + + nsync_csr(-1UL); =20 __kvm_riscv_switch_to(&vcpu->arch); =20 @@ -874,8 +913,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) trap.sepc =3D vcpu->arch.guest_context.sepc; trap.scause =3D csr_read(CSR_SCAUSE); trap.stval =3D csr_read(CSR_STVAL); - trap.htval =3D csr_read(CSR_HTVAL); - trap.htinst =3D csr_read(CSR_HTINST); + trap.htval =3D ncsr_read(CSR_HTVAL); + trap.htinst =3D ncsr_read(CSR_HTINST); =20 /* Syncup interrupts state with HW */ kvm_riscv_vcpu_sync_interrupts(vcpu); diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c index 75486b25ac45..96e7a4e463f7 100644 --- a/arch/riscv/kvm/vcpu_timer.c +++ b/arch/riscv/kvm/vcpu_timer.c @@ -11,8 +11,8 @@ #include #include #include -#include #include +#include #include =20 static u64 kvm_riscv_current_cycles(struct kvm_guest_timer *gt) @@ -72,12 +72,12 @@ static int kvm_riscv_vcpu_timer_cancel(struct kvm_vcpu_= timer *t) static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncyc= les) { #if defined(CONFIG_32BIT) - csr_write(CSR_VSTIMECMP, ncycles & 0xFFFFFFFF); - csr_write(CSR_VSTIMECMPH, ncycles >> 32); + ncsr_write(CSR_VSTIMECMP, ncycles & 0xFFFFFFFF); + ncsr_write(CSR_VSTIMECMPH, ncycles >> 32); #else - csr_write(CSR_VSTIMECMP, ncycles); + ncsr_write(CSR_VSTIMECMP, ncycles); #endif - return 0; + return 0; } =20 static int kvm_riscv_vcpu_update_hrtimer(struct kvm_vcpu *vcpu, u64 ncycle= s) @@ -289,10 +289,10 @@ static void kvm_riscv_vcpu_update_timedelta(struct kv= m_vcpu *vcpu) struct kvm_guest_timer *gt =3D &vcpu->kvm->arch.timer; =20 #if defined(CONFIG_32BIT) - csr_write(CSR_HTIMEDELTA, (u32)(gt->time_delta)); - csr_write(CSR_HTIMEDELTAH, (u32)(gt->time_delta >> 32)); + ncsr_write(CSR_HTIMEDELTA, (u32)(gt->time_delta)); + ncsr_write(CSR_HTIMEDELTAH, (u32)(gt->time_delta >> 32)); #else - csr_write(CSR_HTIMEDELTA, gt->time_delta); + ncsr_write(CSR_HTIMEDELTA, gt->time_delta); #endif } =20 @@ -306,10 +306,10 @@ void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vc= pu) return; =20 #if defined(CONFIG_32BIT) - csr_write(CSR_VSTIMECMP, (u32)t->next_cycles); - csr_write(CSR_VSTIMECMPH, (u32)(t->next_cycles >> 32)); + ncsr_write(CSR_VSTIMECMP, (u32)t->next_cycles); + ncsr_write(CSR_VSTIMECMPH, (u32)(t->next_cycles >> 32)); #else - csr_write(CSR_VSTIMECMP, t->next_cycles); + ncsr_write(CSR_VSTIMECMP, t->next_cycles); #endif =20 /* timer should be enabled for the remaining operations */ @@ -327,10 +327,10 @@ void kvm_riscv_vcpu_timer_sync(struct kvm_vcpu *vcpu) return; =20 #if defined(CONFIG_32BIT) - t->next_cycles =3D csr_read(CSR_VSTIMECMP); - t->next_cycles |=3D (u64)csr_read(CSR_VSTIMECMPH) << 32; + t->next_cycles =3D ncsr_read(CSR_VSTIMECMP); + t->next_cycles |=3D (u64)ncsr_read(CSR_VSTIMECMPH) << 32; #else - t->next_cycles =3D csr_read(CSR_VSTIMECMP); + t->next_cycles =3D ncsr_read(CSR_VSTIMECMP); #endif } =20 --=20 2.43.0