From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 ECAE1193078 for ; Sun, 20 Oct 2024 19:47:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453668; cv=none; b=uoghhnJB7ZG1rKPBhoxzENmfs6t510rE0fk/WPRwsVnpU6d1b12cTPVdN7TJpJ/k3/uDuccKJow8s7niijyfXXOOSS5QGTIslMVEM6Piz4ldNVI9TiqqL0LhD56U07HG6aV9zf0uS+JGDRtwYFht5bPNmojIhT4iCvcoiCNbiHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453668; c=relaxed/simple; bh=rmXeXWote8XruyHvLXjR3P0uOzIQEpHMI/zHFfQn3bs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W0zLAEZ0ZyOXf2qI7bhmEuQW7u1dSDZigWsVkGy4IUHXlddD7IuZQOfnrj+J7LoPPAKLwn4UzzhnoiTwtP3oap0BBxNJ4zfqkUbD/WyD6OYnaIs8WXEoXDRh5ZC7qCKRy89o3YZqdux/Cc0Rz4KEQoGYFaRLnCaRcqnSvQuWR+4= 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=hnv3Kl3R; arc=none smtp.client-ip=209.85.216.41 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="hnv3Kl3R" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2e2e87153a3so2534623a91.3 for ; Sun, 20 Oct 2024 12:47:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453666; x=1730058466; 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=R8r3hycJZuUwFSSkivtdNHwET+4m1ywMgYdIz6RDPHA=; b=hnv3Kl3RQ+1bTskC6LESXltALpEf9nal4YCegcxXhEa6ZXzRdABHZDQ+6b0Hz83dZx dzXHVAe1H4gogtHUtLvRkv1ISs3yFtxoll92Qx5NfXmyunZtDAvRA/MTdPjMbKdsfEOB wQZ/4ZY9JHjr8eQD+Wd7iFUE2rgsejCL0hrsJ4P3xSs9CTBzG9vdV8d6+rHJ+He8/Dwo /E4tTzNaeVqMYi13yGJyZ0hByClQvQ+7XZruejCYFyMaN93NNdDaXcDEz5V1NGR7l9ZD De4J4XHvB2o7t6WoyTQk9yMuRfAUsSR7CevwIyzFpEtcS60ynXfsV1W7nTATnYkXcSNh mMMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453666; x=1730058466; 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=R8r3hycJZuUwFSSkivtdNHwET+4m1ywMgYdIz6RDPHA=; b=MBkJ470IWhVHFmAmaLibwmF+YMUhVzHuP/3fMv1/jBpa0DcvxFr4PnVsdjPhLmygyw gDsBixo7XnylAfinKBUN12b3NJkBMXs2OCjvuPraThZLlKq9oni7nUT99Qwo5RBKCLFM LKv9OcQOhN1U7oUGHLZoer8liWEK2M85Hv5J0XzuM2X43ZTC1ThGD4sZhPFrj4axZ6sb Y27xqLVk1Qm0A2bEJZtfqyTuDkb7PUYqjj289IZRbMVfgnAh06VQ9U5Qfy+W7a7BNPrm x3wATASxjL2dYl93VwBTCnMOY7h7y0DvDFKbZ6TEub+C6TlJ0iPJ1IA7Qky1J+FYMOCO vFxw== X-Forwarded-Encrypted: i=1; AJvYcCX5QFtqZfLTj4GQ9L+QU2uVBkRU9m1Kq5bkLRLoGDf0VcdOJHNGa9sf8e60jUyxX+QbBJFRVfXdsv0SSn4=@vger.kernel.org X-Gm-Message-State: AOJu0YznMn5yPudda9IHBAuUFryVKq5gMtOlrygMaVjt0rMcFYLomK8a 3gUhWKwv9TUXqEg6z+uKmPaO0F5BX+dpZBUmE2GGBEuRz+6VODL2VrSG+IAUB/o= X-Google-Smtp-Source: AGHT+IFyn/+eyWYLXWNY3Vno/OCeNQB3Y8TPoxaOdFGwMyaqKIUSGpcQDYXeZGWcpxBme0KP3op6hw== X-Received: by 2002:a17:90a:b781:b0:2e2:f044:caaa with SMTP id 98e67ed59e1d1-2e5619f8409mr10947652a91.37.1729453666060; Sun, 20 Oct 2024 12:47:46 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:45 -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 01/13] RISC-V: KVM: Order the object files alphabetically Date: Mon, 21 Oct 2024 01:17:22 +0530 Message-ID: <20241020194734.58686-2-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" Order the object files alphabetically in the Makefile so that it is very predictable inserting new object files in the future. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/Makefile | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index c2cacfbc06a0..c1eac0d093de 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -9,27 +9,29 @@ include $(srctree)/virt/kvm/Makefile.kvm =20 obj-$(CONFIG_KVM) +=3D kvm.o =20 +# Ordered alphabetically +kvm-y +=3D aia.o +kvm-y +=3D aia_aplic.o +kvm-y +=3D aia_device.o +kvm-y +=3D aia_imsic.o kvm-y +=3D main.o -kvm-y +=3D vm.o -kvm-y +=3D vmid.o -kvm-y +=3D tlb.o kvm-y +=3D mmu.o +kvm-y +=3D tlb.o kvm-y +=3D vcpu.o kvm-y +=3D vcpu_exit.o kvm-y +=3D vcpu_fp.o -kvm-y +=3D vcpu_vector.o kvm-y +=3D vcpu_insn.o kvm-y +=3D vcpu_onereg.o -kvm-y +=3D vcpu_switch.o +kvm-$(CONFIG_RISCV_PMU_SBI) +=3D vcpu_pmu.o kvm-y +=3D vcpu_sbi.o -kvm-$(CONFIG_RISCV_SBI_V01) +=3D vcpu_sbi_v01.o kvm-y +=3D vcpu_sbi_base.o -kvm-y +=3D vcpu_sbi_replace.o kvm-y +=3D vcpu_sbi_hsm.o +kvm-$(CONFIG_RISCV_PMU_SBI) +=3D vcpu_sbi_pmu.o +kvm-y +=3D vcpu_sbi_replace.o kvm-y +=3D vcpu_sbi_sta.o +kvm-$(CONFIG_RISCV_SBI_V01) +=3D vcpu_sbi_v01.o +kvm-y +=3D vcpu_switch.o kvm-y +=3D vcpu_timer.o -kvm-$(CONFIG_RISCV_PMU_SBI) +=3D vcpu_pmu.o vcpu_sbi_pmu.o -kvm-y +=3D aia.o -kvm-y +=3D aia_device.o -kvm-y +=3D aia_aplic.o -kvm-y +=3D aia_imsic.o +kvm-y +=3D vcpu_vector.o +kvm-y +=3D vm.o +kvm-y +=3D vmid.o --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 C512B198841 for ; Sun, 20 Oct 2024 19:47:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453670; cv=none; b=P7+eh0FAEAAlwVukQ4uHzKDK42YSyMTW3FzN//W5yzqdOARyDAO1KbogIDn1HaxHNr8HUcfZWdcxJJqj5RBvzXIroRq+Pwvfq7PLNpg6XFIFFPHi2n4gJJVT45iGFH3Wc/j7ZdZshzDQNzGw9MjzWHAzXNhF9d/6GRwyB31flI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453670; c=relaxed/simple; bh=QR25kCjRwR3j6EKmmDoaShaAl2XNdgvpAF+TH9VGHTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZcwWzwLA+lWMbztop8d/7WtqddmWhx6q3nPLCPWNB61FPojUV7TDwQhtvg2ms+wFHd2Cj4qsgSEVQAiYeZhLGr2R966hHIWGrfSXTjCkfpwPy+S1tmXciywhJWTkoVq9pa9qLUhHm1VlTkT4WanB6VwfM9HEcGDDtmE502hFhQ8= 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=kxIgxPms; arc=none smtp.client-ip=209.85.216.51 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="kxIgxPms" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2e3010478e6so3068084a91.1 for ; Sun, 20 Oct 2024 12:47:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453668; x=1730058468; 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=1FyA7P9FbrHeEPhaW0M7UvJndGkwOjlPhJv2AlIq1GM=; b=kxIgxPms01reo/62/cQvzEZAG43eqCiXKBN0zH+DRIrBt5bDPNr1GTx32pgcdvdzqN xt7zaMoRn/aQ5LsRQq1wP93HhzM6KfOeINOJFXQ0yWxdPRGHVT2uex2qXlhDEy3bX64f 6UAt9b8nLpWCVyYa15fFkETo+zT8uZvfIXJIOKIL8yaUykg+PK/Fci6qMIdp/Qoqz6E9 qCB+oK4L9qHs8KkaG2UukZcSK12Sf/Hds4dIqzFDhrJsvWTie8nTBflE062Nvr5NEiKU dfp93223p8cObPHiM0ZSBlQMNcnUiOE6Xk1T91nIWxAExleFjZMxwH79Nl2bv6q2bmpO BMdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453668; x=1730058468; 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=1FyA7P9FbrHeEPhaW0M7UvJndGkwOjlPhJv2AlIq1GM=; b=xLooQb1H/DNK6nnEJA8MqeG8bbc8Q+Boi+TvB5f+Hf4f2X2sihBuuthtXk+7SsgfgT Di3HtYCoMjsPJNPx9NvRGFrFEsYvtA0Ar4vLZ6Wo5i0oenjuedjusfXLZvgETTTemfK3 +J+lWrqQSKK44fma9XcfGcKnNZOTspq8VPVX8xEA1fewOxOvCX1wvUmWfquPEtXnSY1v QEokaM/W0gHWi0cU+P1gUjvZa6KtWL+bAEwvT4EwllRUC3oUnbMQWXodZ6Tb4J0YiU3m N8GwJaKncm2T8+z7OyzfuEPrrI8jFlr7dZhQ5YEFnH9rxPPNdcADS2ztER5FqY3YjoaL Wdxw== X-Forwarded-Encrypted: i=1; AJvYcCX+6Ps4AKhBd5vP2ApUZfqFlcL1FLZh57Kubz3iy+EiBVZG4sf1rnKrVxqNg+GZD28sLRjc+4lFzCZ5SE4=@vger.kernel.org X-Gm-Message-State: AOJu0YxBSCc8PSPlw9YLxYIOHlqP1VreLBFFA+H+616uVghZfo88PXOg m75pOh/MacRC4ZPJssB9+0dMP+jJHEe9qF7IUWZlYviilG2WMF6mN2nfocKBYYQ= X-Google-Smtp-Source: AGHT+IF7lE3B8ClzR6qfjFpDCQHpNymbsHf8YVM1zw/aseb8Rk6lioGovC2ybZ0RbWASOBCIDdfN6A== X-Received: by 2002:a17:90b:4b0d:b0:2e2:de95:34eb with SMTP id 98e67ed59e1d1-2e5616eba4emr12077551a91.10.1729453667879; Sun, 20 Oct 2024 12:47:47 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:47 -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 02/13] RISC-V: KVM: Save/restore HSTATUS in C source Date: Mon, 21 Oct 2024 01:17:23 +0530 Message-ID: <20241020194734.58686-3-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" We will be optimizing HSTATUS CSR access via shared memory setup using the SBI nested acceleration extension. To facilitate this, we first move HSTATUS save/restore in kvm_riscv_vcpu_enter_exit(). Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 9 +++++++++ arch/riscv/kvm/vcpu_switch.S | 36 +++++++++++++----------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index dae12ca3c8b6..53ee29490c7f 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -731,9 +731,18 @@ static __always_inline void kvm_riscv_vcpu_swap_in_hos= t_state(struct kvm_vcpu *v */ static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu) { + struct kvm_cpu_context *gcntx =3D &vcpu->arch.guest_context; + struct kvm_cpu_context *hcntx =3D &vcpu->arch.host_context; + kvm_riscv_vcpu_swap_in_guest_state(vcpu); guest_state_enter_irqoff(); + + hcntx->hstatus =3D csr_swap(CSR_HSTATUS, gcntx->hstatus); + __kvm_riscv_switch_to(&vcpu->arch); + + gcntx->hstatus =3D csr_swap(CSR_HSTATUS, hcntx->hstatus); + vcpu->arch.last_exit_cpu =3D vcpu->cpu; guest_state_exit_irqoff(); kvm_riscv_vcpu_swap_in_host_state(vcpu); diff --git a/arch/riscv/kvm/vcpu_switch.S b/arch/riscv/kvm/vcpu_switch.S index 0c26189aa01c..f83643c4fdb9 100644 --- a/arch/riscv/kvm/vcpu_switch.S +++ b/arch/riscv/kvm/vcpu_switch.S @@ -43,35 +43,30 @@ SYM_FUNC_START(__kvm_riscv_switch_to) =20 /* Load Guest CSR values */ REG_L t0, (KVM_ARCH_GUEST_SSTATUS)(a0) - REG_L t1, (KVM_ARCH_GUEST_HSTATUS)(a0) - REG_L t2, (KVM_ARCH_GUEST_SCOUNTEREN)(a0) - la t4, .Lkvm_switch_return - REG_L t5, (KVM_ARCH_GUEST_SEPC)(a0) + REG_L t1, (KVM_ARCH_GUEST_SCOUNTEREN)(a0) + la t3, .Lkvm_switch_return + REG_L t4, (KVM_ARCH_GUEST_SEPC)(a0) =20 /* Save Host and Restore Guest SSTATUS */ csrrw t0, CSR_SSTATUS, t0 =20 - /* Save Host and Restore Guest HSTATUS */ - csrrw t1, CSR_HSTATUS, t1 - /* Save Host and Restore Guest SCOUNTEREN */ - csrrw t2, CSR_SCOUNTEREN, t2 + csrrw t1, CSR_SCOUNTEREN, t1 =20 /* Save Host STVEC and change it to return path */ - csrrw t4, CSR_STVEC, t4 + csrrw t3, CSR_STVEC, t3 =20 /* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */ - csrrw t3, CSR_SSCRATCH, a0 + csrrw t2, CSR_SSCRATCH, a0 =20 /* Restore Guest SEPC */ - csrw CSR_SEPC, t5 + csrw CSR_SEPC, t4 =20 /* Store Host CSR values */ REG_S t0, (KVM_ARCH_HOST_SSTATUS)(a0) - REG_S t1, (KVM_ARCH_HOST_HSTATUS)(a0) - REG_S t2, (KVM_ARCH_HOST_SCOUNTEREN)(a0) - REG_S t3, (KVM_ARCH_HOST_SSCRATCH)(a0) - REG_S t4, (KVM_ARCH_HOST_STVEC)(a0) + REG_S t1, (KVM_ARCH_HOST_SCOUNTEREN)(a0) + REG_S t2, (KVM_ARCH_HOST_SSCRATCH)(a0) + REG_S t3, (KVM_ARCH_HOST_STVEC)(a0) =20 /* Restore Guest GPRs (except A0) */ REG_L ra, (KVM_ARCH_GUEST_RA)(a0) @@ -153,8 +148,7 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_L t1, (KVM_ARCH_HOST_STVEC)(a0) REG_L t2, (KVM_ARCH_HOST_SSCRATCH)(a0) REG_L t3, (KVM_ARCH_HOST_SCOUNTEREN)(a0) - REG_L t4, (KVM_ARCH_HOST_HSTATUS)(a0) - REG_L t5, (KVM_ARCH_HOST_SSTATUS)(a0) + REG_L t4, (KVM_ARCH_HOST_SSTATUS)(a0) =20 /* Save Guest SEPC */ csrr t0, CSR_SEPC @@ -168,18 +162,14 @@ SYM_FUNC_START(__kvm_riscv_switch_to) /* Save Guest and Restore Host SCOUNTEREN */ csrrw t3, CSR_SCOUNTEREN, t3 =20 - /* Save Guest and Restore Host HSTATUS */ - csrrw t4, CSR_HSTATUS, t4 - /* Save Guest and Restore Host SSTATUS */ - csrrw t5, CSR_SSTATUS, t5 + csrrw t4, CSR_SSTATUS, t4 =20 /* Store Guest CSR values */ REG_S t0, (KVM_ARCH_GUEST_SEPC)(a0) REG_S t2, (KVM_ARCH_GUEST_A0)(a0) REG_S t3, (KVM_ARCH_GUEST_SCOUNTEREN)(a0) - REG_S t4, (KVM_ARCH_GUEST_HSTATUS)(a0) - REG_S t5, (KVM_ARCH_GUEST_SSTATUS)(a0) + REG_S t4, (KVM_ARCH_GUEST_SSTATUS)(a0) =20 /* Restore Host GPRs (except A0 and T0-T6) */ REG_L ra, (KVM_ARCH_HOST_RA)(a0) --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 A3F50198E6F for ; Sun, 20 Oct 2024 19:47:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453672; cv=none; b=qvS6Nz0nELsMyTdbB0vwWyQyjTQsEE1zVM1OGnh5I3XVGOgcFucTT4EujxcZz66NR3MEUQAn06Io9c1qAfqCCuG0DTD3v4uFyet11wsHNrGW76UAOlFK6gUkUNhbPQID2Liagf6iM1UpG6NEhWoHWVqwayCxb7KpPmeM2fVJ7Xw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453672; c=relaxed/simple; bh=6bWzSmvGfg7VcnSp11hW4tETYIx01Z9YISaX0exgR0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RgXD9KYR0KnU7R5Up9q1XalZaCiqPK4nWXG38P5ctd7jlHyw92XmBzBqt/peRiKi/XF8wyNo5NnM0VcOdg9lE100v4rqgbhIU1Cimb0c0kqmaHNl60rV2c8lZmhUTX22uLuCwBGjSEeC3LBZutxu1cfn8ijAlpw7JE5sweP/6/0= 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=WyGg1wgL; arc=none smtp.client-ip=209.85.216.53 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="WyGg1wgL" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2e56df894d4so1792864a91.3 for ; Sun, 20 Oct 2024 12:47:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453670; x=1730058470; 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=YjxqRhm7xrGjz8Ovok1N736zyKzdvbsEt213nSnt+BI=; b=WyGg1wgL7JOXHRMvab/ikKg8sYLdbO1Us56Zyil8Xnc0W/ozeBycJHhZgaUFDH4P97 yYmSkk/uSdzFAcMAxFEbio8/sIbTwUGiBu2c4+e5JMUIwjXflAKB3vuWUYZzHjUa34vF AmAOc/RthluvkNh8t9mm94AtP/xsqAt4ybUAFbV1k0owdGHTVX6oSEJDa7XIJdfBXRfN 7R3KwmFzT60ElOlxWjd8+FEOcf6/UEq5pWYqEfjeOMRp1BM94YL8KDrqG65Tbb2Dwiir bVJxMenx48g0Y8PxbfKeCZqCL7lsNzSRM4vj+KAAnb71VxH8a6Vi95+qgH+DceMZBZlI Bk2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453670; x=1730058470; 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=YjxqRhm7xrGjz8Ovok1N736zyKzdvbsEt213nSnt+BI=; b=d4wWzSjq3qB4DYLieb2xLt3ZGETNMtrW83RsOK7p0q+ZyAehC9aTyiLaOQeavTFPuu YLzJPtsA3gV7lNYJwREiVZOS0512uF7KPrXMJ0SKVdqWUoRE5x8/6cnKWon2uoPJUeiO l6vyAJ5gcb4n4vPhhlsw3T1ESACBUyXusWLtP1zk5Uj2cJ1WSkmM6wcntWg+24XGr//0 c1ZDJLDt2WD2CdWcjHc18WkVNmyrHzISDWSdVuUvLvJP2s++HRFFVzZwEEHfNRgjp7Hm y4b/75vg0VFRgcWr7Oe815AEVQ5fvUSEtX+A0ya1XsmRI5ynVonxPtY2p4M0DgytqGoz Qmzg== X-Forwarded-Encrypted: i=1; AJvYcCXJ5iHsFqnghIdToJEzamDla05q3UAU/hg9wHMEgLPdd6SZg7R7Woa7itJ3k5M38AaCpy7Rap2d/4F8mMU=@vger.kernel.org X-Gm-Message-State: AOJu0YwSP144lc9swfIDS50turyIPqsx5RBMl8vZLR9gfnmmAst/HLDA G8ByUQ1o/Wnc2Rhd7I16it6ty8HruFajNkJOACJYzuHPDyJTCBiNfaS9H6DYnJU= X-Google-Smtp-Source: AGHT+IGSbrFfEsCiKKrMtJuowc85sYHNI6rlNZmFsVNLUA0j4KKYlpfFIvbCLDM9xKhydrTr8zEK0Q== X-Received: by 2002:a17:90a:7086:b0:2e2:c421:894b with SMTP id 98e67ed59e1d1-2e561726f19mr11200428a91.14.1729453669660; Sun, 20 Oct 2024 12:47:49 -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.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:48 -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 03/13] RISC-V: KVM: Save/restore SCOUNTEREN in C source Date: Mon, 21 Oct 2024 01:17:24 +0530 Message-ID: <20241020194734.58686-4-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" The SCOUNTEREN CSR need not be saved/restored in the low-level __kvm_riscv_switch_to() function hence move the SCOUNTEREN CSR save/restore to the kvm_riscv_vcpu_swap_in_guest_state() and kvm_riscv_vcpu_swap_in_host_state() functions in C sources. Also, re-arrange the CSR save/restore and related GPR usage in the low-level __kvm_riscv_switch_to() low-level function. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 2 ++ arch/riscv/kvm/vcpu_switch.S | 52 +++++++++++++++--------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 53ee29490c7f..a972dd46e7e4 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -702,6 +702,7 @@ static __always_inline void kvm_riscv_vcpu_swap_in_gues= t_state(struct kvm_vcpu * struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; struct kvm_vcpu_config *cfg =3D &vcpu->arch.cfg; =20 + vcpu->arch.host_scounteren =3D csr_swap(CSR_SCOUNTEREN, csr->scounteren); vcpu->arch.host_senvcfg =3D csr_swap(CSR_SENVCFG, csr->senvcfg); if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) && (cfg->hstateen0 & SMSTATEEN0_SSTATEEN0)) @@ -715,6 +716,7 @@ static __always_inline void kvm_riscv_vcpu_swap_in_host= _state(struct kvm_vcpu *v struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; struct kvm_vcpu_config *cfg =3D &vcpu->arch.cfg; =20 + csr->scounteren =3D csr_swap(CSR_SCOUNTEREN, vcpu->arch.host_scounteren); csr->senvcfg =3D csr_swap(CSR_SENVCFG, vcpu->arch.host_senvcfg); if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN) && (cfg->hstateen0 & SMSTATEEN0_SSTATEEN0)) diff --git a/arch/riscv/kvm/vcpu_switch.S b/arch/riscv/kvm/vcpu_switch.S index f83643c4fdb9..3f8cbc21a644 100644 --- a/arch/riscv/kvm/vcpu_switch.S +++ b/arch/riscv/kvm/vcpu_switch.S @@ -43,30 +43,25 @@ SYM_FUNC_START(__kvm_riscv_switch_to) =20 /* Load Guest CSR values */ REG_L t0, (KVM_ARCH_GUEST_SSTATUS)(a0) - REG_L t1, (KVM_ARCH_GUEST_SCOUNTEREN)(a0) - la t3, .Lkvm_switch_return - REG_L t4, (KVM_ARCH_GUEST_SEPC)(a0) + la t1, .Lkvm_switch_return + REG_L t2, (KVM_ARCH_GUEST_SEPC)(a0) =20 /* Save Host and Restore Guest SSTATUS */ csrrw t0, CSR_SSTATUS, t0 =20 - /* Save Host and Restore Guest SCOUNTEREN */ - csrrw t1, CSR_SCOUNTEREN, t1 - /* Save Host STVEC and change it to return path */ - csrrw t3, CSR_STVEC, t3 - - /* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */ - csrrw t2, CSR_SSCRATCH, a0 + csrrw t1, CSR_STVEC, t1 =20 /* Restore Guest SEPC */ - csrw CSR_SEPC, t4 + csrw CSR_SEPC, t2 + + /* Save Host SSCRATCH and change it to struct kvm_vcpu_arch pointer */ + csrrw t3, CSR_SSCRATCH, a0 =20 /* Store Host CSR values */ REG_S t0, (KVM_ARCH_HOST_SSTATUS)(a0) - REG_S t1, (KVM_ARCH_HOST_SCOUNTEREN)(a0) - REG_S t2, (KVM_ARCH_HOST_SSCRATCH)(a0) - REG_S t3, (KVM_ARCH_HOST_STVEC)(a0) + REG_S t1, (KVM_ARCH_HOST_STVEC)(a0) + REG_S t3, (KVM_ARCH_HOST_SSCRATCH)(a0) =20 /* Restore Guest GPRs (except A0) */ REG_L ra, (KVM_ARCH_GUEST_RA)(a0) @@ -145,31 +140,26 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_S t6, (KVM_ARCH_GUEST_T6)(a0) =20 /* Load Host CSR values */ - REG_L t1, (KVM_ARCH_HOST_STVEC)(a0) - REG_L t2, (KVM_ARCH_HOST_SSCRATCH)(a0) - REG_L t3, (KVM_ARCH_HOST_SCOUNTEREN)(a0) - REG_L t4, (KVM_ARCH_HOST_SSTATUS)(a0) - - /* Save Guest SEPC */ - csrr t0, CSR_SEPC + REG_L t0, (KVM_ARCH_HOST_STVEC)(a0) + REG_L t1, (KVM_ARCH_HOST_SSCRATCH)(a0) + REG_L t2, (KVM_ARCH_HOST_SSTATUS)(a0) =20 /* Save Guest A0 and Restore Host SSCRATCH */ - csrrw t2, CSR_SSCRATCH, t2 + csrrw t1, CSR_SSCRATCH, t1 =20 - /* Restore Host STVEC */ - csrw CSR_STVEC, t1 + /* Save Guest SEPC */ + csrr t3, CSR_SEPC =20 - /* Save Guest and Restore Host SCOUNTEREN */ - csrrw t3, CSR_SCOUNTEREN, t3 + /* Restore Host STVEC */ + csrw CSR_STVEC, t0 =20 /* Save Guest and Restore Host SSTATUS */ - csrrw t4, CSR_SSTATUS, t4 + csrrw t2, CSR_SSTATUS, t2 =20 /* Store Guest CSR values */ - REG_S t0, (KVM_ARCH_GUEST_SEPC)(a0) - REG_S t2, (KVM_ARCH_GUEST_A0)(a0) - REG_S t3, (KVM_ARCH_GUEST_SCOUNTEREN)(a0) - REG_S t4, (KVM_ARCH_GUEST_SSTATUS)(a0) + REG_S t1, (KVM_ARCH_GUEST_A0)(a0) + REG_S t2, (KVM_ARCH_GUEST_SSTATUS)(a0) + REG_S t3, (KVM_ARCH_GUEST_SEPC)(a0) =20 /* Restore Host GPRs (except A0 and T0-T6) */ REG_L ra, (KVM_ARCH_HOST_RA)(a0) --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 7BC321991AB for ; Sun, 20 Oct 2024 19:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453675; cv=none; b=Bp+FFf3eCzBXbKxsyoJIADE46BudAPzyQgFGs94Xn8SCqhufNaEyrYsbl1uSyw6t4pGoaGJmvpZ0Qf4RF8ylX4YwEZ6f13EG9jtev/VdUU3S89F++jq5pTuZ/KWJ2+DeohgDmvmchamAFJoY6K/edzV8JpGHBtFO9w1pMgRgPKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453675; c=relaxed/simple; bh=aXnA9wY0kQ/Bx5uZ0gzKAYiZGftNnwWnnmMlj07L2Cs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iyNXafV9pxOdk8QQHdmjONFW/4w302g0RfZ2fqFssZRU5nnhRSZCmePUQcIyqkSTUC3s48F4odM/EsovWJiWCeEX2p8/N6DQ2mfqbb4Cd05u3b5MHawzlcP4H0VcLvlxnQn2Y6J0eCR97XOEuFnbTfTjdKpZxU07zALDipj1Vqw= 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=VxW2FnZW; arc=none smtp.client-ip=209.85.216.53 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="VxW2FnZW" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2e5a0177531so544743a91.2 for ; Sun, 20 Oct 2024 12:47:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453672; x=1730058472; 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=yfThf6WSDtXeJLGF9IIFos3c8UFbvclgKv7arl05kb4=; b=VxW2FnZWpASfxmDBWLd0vxi2XqPjT3xyigCPOdS4h3bMmBTEDLazAWccDPrjyB5MA9 sLGmaCIRJY26wRTXCfbKWQ0ExnEyTk3JYo9gNPIBboVP98o8hS8MqP0I3BkcMoRwta/F FTQd5FCgE4/myCcy4cnjzGZqRi7yRU9VC5OeBpbq3cHSWhac7EslEDf39ie9YYyyeCtC IzF9hjvSNIWAPx1bHp8FJvWSNEq3CE0r9pku3+2+UEhs4OsIxM9KlViTRXAdWtyMi9VH 4dBRCLtc5vpsi0OgB/fvVvchhyJEAK4RVQQ9u+Ek12M6gYlmJnDB/QU7rKhIjpyyCN5k VPrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453672; x=1730058472; 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=yfThf6WSDtXeJLGF9IIFos3c8UFbvclgKv7arl05kb4=; b=sbprZ4QPkhjZidb7OlWh9mJVRjyfmVKEHiaH+0mLcwgOo5HiOv7+WP/Gnm9iJlEJoS 7+qBavNYONJs/LDsETQwazdQtp5uQmPXbd8b7mZV9jQQ9DaweFIIVA1U1n9LChiw1C2q QR6JnIMjgplDq2T27KDCUmsRAZVkGA97cFqrFg0LJsKEXuEjrVPULLqZkbN36lOXj4pF a0KYy4m64y8pLWZfrHmS9gjKdCTMIThJX4dJZ+zFxDXRNH6a/l5OyqjYwh8NSNJQ7but G2pBQf1zE43VwnKFZSM4+B6t9snI80KZVjt41h0cmKIHajINAUpq3glUcJLeWyzEpz1q LDQA== X-Forwarded-Encrypted: i=1; AJvYcCXLScE8r1RYeDvfF1OTI46V2INm2+VoQUkQ9qYr3c5FcUOA2Ev+dPBSnIox6jwo5w8a2B5twNHDEKvRg54=@vger.kernel.org X-Gm-Message-State: AOJu0YzUGo0kv4LyfhAwNTaoPW8sMsOQS8LMdI1HPh+u9V/128fPZYkC frk2xQsRMRD7qZ6fxlpz32my8USB9dyGgafesuhdsLWTUbwDoceibOIyYum3N3E= X-Google-Smtp-Source: AGHT+IHJeZE9HrX0oYl24Mbcflj7S1QWZQW1xGijDT99BYjkFVqjYvyBRxHehmfkCt5Hnq51re8yqA== X-Received: by 2002:a17:90b:4a0c:b0:2e2:b3f8:b549 with SMTP id 98e67ed59e1d1-2e561650b59mr11476427a91.23.1729453671610; Sun, 20 Oct 2024 12:47:51 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:51 -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 04/13] RISC-V: KVM: Break down the __kvm_riscv_switch_to() into macros Date: Mon, 21 Oct 2024 01:17:25 +0530 Message-ID: <20241020194734.58686-5-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" Break down the __kvm_riscv_switch_to() function into macros so that these macros can be later re-used by SBI NACL extension based low-level switch function. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu_switch.S | 52 +++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/arch/riscv/kvm/vcpu_switch.S b/arch/riscv/kvm/vcpu_switch.S index 3f8cbc21a644..9f13e5ce6a18 100644 --- a/arch/riscv/kvm/vcpu_switch.S +++ b/arch/riscv/kvm/vcpu_switch.S @@ -11,11 +11,7 @@ #include #include =20 - .text - .altmacro - .option norelax - -SYM_FUNC_START(__kvm_riscv_switch_to) +.macro SAVE_HOST_GPRS /* Save Host GPRs (except A0 and T0-T6) */ REG_S ra, (KVM_ARCH_HOST_RA)(a0) REG_S sp, (KVM_ARCH_HOST_SP)(a0) @@ -40,10 +36,12 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_S s9, (KVM_ARCH_HOST_S9)(a0) REG_S s10, (KVM_ARCH_HOST_S10)(a0) REG_S s11, (KVM_ARCH_HOST_S11)(a0) +.endm =20 +.macro SAVE_HOST_AND_RESTORE_GUEST_CSRS __resume_addr /* Load Guest CSR values */ REG_L t0, (KVM_ARCH_GUEST_SSTATUS)(a0) - la t1, .Lkvm_switch_return + la t1, \__resume_addr REG_L t2, (KVM_ARCH_GUEST_SEPC)(a0) =20 /* Save Host and Restore Guest SSTATUS */ @@ -62,7 +60,9 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_S t0, (KVM_ARCH_HOST_SSTATUS)(a0) REG_S t1, (KVM_ARCH_HOST_STVEC)(a0) REG_S t3, (KVM_ARCH_HOST_SSCRATCH)(a0) +.endm =20 +.macro RESTORE_GUEST_GPRS /* Restore Guest GPRs (except A0) */ REG_L ra, (KVM_ARCH_GUEST_RA)(a0) REG_L sp, (KVM_ARCH_GUEST_SP)(a0) @@ -97,13 +97,9 @@ SYM_FUNC_START(__kvm_riscv_switch_to) =20 /* Restore Guest A0 */ REG_L a0, (KVM_ARCH_GUEST_A0)(a0) +.endm =20 - /* Resume Guest */ - sret - - /* Back to Host */ - .align 2 -.Lkvm_switch_return: +.macro SAVE_GUEST_GPRS /* Swap Guest A0 with SSCRATCH */ csrrw a0, CSR_SSCRATCH, a0 =20 @@ -138,7 +134,9 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_S t4, (KVM_ARCH_GUEST_T4)(a0) REG_S t5, (KVM_ARCH_GUEST_T5)(a0) REG_S t6, (KVM_ARCH_GUEST_T6)(a0) +.endm =20 +.macro SAVE_GUEST_AND_RESTORE_HOST_CSRS /* Load Host CSR values */ REG_L t0, (KVM_ARCH_HOST_STVEC)(a0) REG_L t1, (KVM_ARCH_HOST_SSCRATCH)(a0) @@ -160,7 +158,9 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_S t1, (KVM_ARCH_GUEST_A0)(a0) REG_S t2, (KVM_ARCH_GUEST_SSTATUS)(a0) REG_S t3, (KVM_ARCH_GUEST_SEPC)(a0) +.endm =20 +.macro RESTORE_HOST_GPRS /* Restore Host GPRs (except A0 and T0-T6) */ REG_L ra, (KVM_ARCH_HOST_RA)(a0) REG_L sp, (KVM_ARCH_HOST_SP)(a0) @@ -185,6 +185,34 @@ SYM_FUNC_START(__kvm_riscv_switch_to) REG_L s9, (KVM_ARCH_HOST_S9)(a0) REG_L s10, (KVM_ARCH_HOST_S10)(a0) REG_L s11, (KVM_ARCH_HOST_S11)(a0) +.endm + + .text + .altmacro + .option norelax + + /* + * Parameters: + * A0 <=3D Pointer to struct kvm_vcpu_arch + */ +SYM_FUNC_START(__kvm_riscv_switch_to) + SAVE_HOST_GPRS + + SAVE_HOST_AND_RESTORE_GUEST_CSRS .Lkvm_switch_return + + RESTORE_GUEST_GPRS + + /* Resume Guest using SRET */ + sret + + /* Back to Host */ + .align 2 +.Lkvm_switch_return: + SAVE_GUEST_GPRS + + SAVE_GUEST_AND_RESTORE_HOST_CSRS + + RESTORE_HOST_GPRS =20 /* Return to C code */ ret --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 172A619924F for ; Sun, 20 Oct 2024 19:47:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453676; cv=none; b=ct1znXahQhr58ARfYfIRH0SYhLS/KfHlVvg/vZ0EoJWnCxFjxb9tCAXado/DdbPUuUoKmWGJizBMJvt+iEXn4B4jGjilROLzNAh8iOHn4xFbex0ahFR9wRt7oqPoRI9R39IuMSUf4OfU6pQlPTE+96b6vMNVj/RP5ZKItRXZaao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453676; c=relaxed/simple; bh=oE1Zu8aEJgqleyU7ZpYqwEzohbGPuLf2NxIVozUTJmc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g+qjn6fV4Q4Do8Qc0z7O0VVg7jEzWdUJU5iQDoMt/JYuWl/i2/72NUZBA/gsTitIarUZvsXOk110uCT/+ycQB7fjBxPOFOgSgDa/DqjKAmDR8/m+BjpLf+/mz1hnIYkVV5Btj09zxigRCvcsdP5rs0sTs4//7J7IDi2cJwxTFNI= 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=GTqJfJ0f; arc=none smtp.client-ip=209.85.216.46 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="GTqJfJ0f" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2e2e87153a3so2534658a91.3 for ; Sun, 20 Oct 2024 12:47:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453674; x=1730058474; 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=QEedsq2JUKhoGmqZGJYyzFicNOwkAdZlkiaTwr3TF/o=; b=GTqJfJ0fZETggrR0u/aUBB61ACCeFCa9QThO1XaytMATZ+7loIn2EDm3ywg9x2FvOa AgUQjM+OevitoWqhV6eEFxHn/368BmezDORP3CP1Mo6VngRj8X8SDRG0+Uc7LXqLaOWd WjuVW+eLcrG4UUFjHIGLRT+hOT6p4lM4142HihPs72qk4FnJRKiAzSHgC1nmjjlQLVBU 5B0vKA0WlQ5sMMu/+Y4ZnkHyPDGiMdKSgmsV4bg39ZlaFdeKkPJtwNjySL7t4dgTn67D u9LNWSHBl2l1KTcgoCih5W/j17rIu23BMcgpB1MKFJQGnU1ekLPztqdndj5WH4OsHHWi n+Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453674; x=1730058474; 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=QEedsq2JUKhoGmqZGJYyzFicNOwkAdZlkiaTwr3TF/o=; b=EGENEbT6lFVvPocWBfWW98IWhehxznTnMKkUoWySvTmd1mTtPSuW/ryxd/08EXRbEz mNmAAJ1hf05Z3KxVhb7p3SBiOAqJ9a1oMuYDS3rKyHyUhEH4ekJsyb3Nn/WL7Xu5vbR/ RjJ80ChLU0pyOitFFXpDPD+5F06pN5EpW0Q+y2HazbCwZ2cWYi4/Se6p73z3jm/nOP1d j9PbnQnRwcf/Zx+cIM+jcpC0raGw28UUxL+inmvTj8cDkpNgmBE/2Nqx9Snm7HpzPa+3 vO/Pfab0oTmlyUiWudykO0sVhkGOmWO9qHw+vpsXQVBbHvw2em2VZEjCdc1EiBn+zQ10 ONag== X-Forwarded-Encrypted: i=1; AJvYcCUXmj13TiB5OTMsFiH7eRuqJlc6EQoBG63kwCxsxzz7UY5IFo+0tXByRgygPmkjv5gOz7Mvyjoq83SHVPo=@vger.kernel.org X-Gm-Message-State: AOJu0YyiSjRtgw6I+eFDbxilqISxnAS0Bb1aXoxOzZ9fnz+ugW/xIov9 fdu9CrtL2lxkU3qHkFb7mxv3togScBc+yTaqFf4VlM3e5RqL9CKO/nUtgGOmoS0= X-Google-Smtp-Source: AGHT+IE/fRzVoS+fVEWIeoAKQcgXW0+uJTsQHmeBuYcXiEzRwc7/fSmVJvcmrtukYOBuAWj6RtlBvg== X-Received: by 2002:a17:90a:a586:b0:2e2:cd22:b083 with SMTP id 98e67ed59e1d1-2e5614e5da3mr10891971a91.0.1729453673108; Sun, 20 Oct 2024 12:47:53 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:52 -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 05/13] RISC-V: KVM: Replace aia_set_hvictl() with aia_hvictl_value() Date: Mon, 21 Oct 2024 01:17:26 +0530 Message-ID: <20241020194734.58686-6-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" The aia_set_hvictl() internally writes the HVICTL CSR which makes it difficult to optimize the CSR write using SBI NACL extension for kvm_riscv_vcpu_aia_update_hvip() function so replace aia_set_hvictl() with new aia_hvictl_value() which only computes the HVICTL value. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/aia.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index 2967d305c442..17ae4a7c0e94 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -51,7 +51,7 @@ static int aia_find_hgei(struct kvm_vcpu *owner) return hgei; } =20 -static void aia_set_hvictl(bool ext_irq_pending) +static inline unsigned long aia_hvictl_value(bool ext_irq_pending) { unsigned long hvictl; =20 @@ -62,7 +62,7 @@ static void aia_set_hvictl(bool ext_irq_pending) =20 hvictl =3D (IRQ_S_EXT << HVICTL_IID_SHIFT) & HVICTL_IID; hvictl |=3D ext_irq_pending; - csr_write(CSR_HVICTL, hvictl); + return hvictl; } =20 #ifdef CONFIG_32BIT @@ -130,7 +130,7 @@ void kvm_riscv_vcpu_aia_update_hvip(struct kvm_vcpu *vc= pu) #ifdef CONFIG_32BIT csr_write(CSR_HVIPH, vcpu->arch.aia_context.guest_csr.hviph); #endif - aia_set_hvictl(!!(csr->hvip & BIT(IRQ_VS_EXT))); + csr_write(CSR_HVICTL, aia_hvictl_value(!!(csr->hvip & BIT(IRQ_VS_EXT)))); } =20 void kvm_riscv_vcpu_aia_load(struct kvm_vcpu *vcpu, int cpu) @@ -536,7 +536,7 @@ void kvm_riscv_aia_enable(void) if (!kvm_riscv_aia_available()) return; =20 - aia_set_hvictl(false); + csr_write(CSR_HVICTL, aia_hvictl_value(false)); csr_write(CSR_HVIPRIO1, 0x0); csr_write(CSR_HVIPRIO2, 0x0); #ifdef CONFIG_32BIT @@ -572,7 +572,7 @@ void kvm_riscv_aia_disable(void) csr_clear(CSR_HIE, BIT(IRQ_S_GEXT)); disable_percpu_irq(hgei_parent_irq); =20 - aia_set_hvictl(false); + csr_write(CSR_HVICTL, aia_hvictl_value(false)); =20 raw_spin_lock_irqsave(&hgctrl->lock, flags); =20 --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 DB772199395 for ; Sun, 20 Oct 2024 19:47:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453677; cv=none; b=GUF0HakWSQ4Xi7FvsovDoIaEop1vKl3vjBcJdV6k4UdgLjjCiCdVWXCy8yWezfGQgtJ8oEEgW+FW2aCVHB6JV8EclQzPU/YUXW2/bno+mWMmBMmJDf/tmBFSDuUyl8Jt9HNHNrZpTzmTnHDHeJbCyeJL+UT2OoGhftyym97I4AM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453677; c=relaxed/simple; bh=XeJ4zURiPtRwRC7wqRQygMzynUWW7n969d9Luma7yik=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c6QmAeMp11CfxTzKdH9RJTFzGhMsY952ApoiubCdTLPCgz9tKPZF5oSx6Eu3Wr7TAk1k25urjh3N1lWCQUC27JCHLYsMDBX+06Ym6oNlHim2/8AmGq2jJg3XAhWltNEwAjkVkeTCtn5VI3MDm8jUkBfin+9DaxficghFo3ydJwY= 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=I5pCJdqA; arc=none smtp.client-ip=209.85.216.47 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="I5pCJdqA" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2e3686088c3so2778174a91.0 for ; Sun, 20 Oct 2024 12:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453675; x=1730058475; 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=160oEn3Hw+w/iZ7EFhTc6TJAcZCL3iOZo0psz4xgbB0=; b=I5pCJdqAd2b3aGVebqpW8FkbzPcHmpz8scO/8DjliVrbGRjkqHQyLuuwCez7Bqc3fX 01XvBIRvpM+15C86Tmcb1zEPTwe0okn7z9kLjybEn9+w1atNqJC2NUK/98/BYm7qHfnl k+yZ/Ca+e7dfLMjVhefNOrKGJjGtrxlZiopZHjp4futM8yw2BfcRXmOlIRjHeKvnkVC+ 53IVyb9iLmg2m+vZndIwbO+0ygevLcZmu8Am7MCR35dnzxJ61N1vB2XRQ2zSK0x10y4b HIyrBaM7lth8dW3+sLwqPbr1L6pOTaS04Jj34upU6aoOn8kFTsDrTDGPhdqGhnNSGCn+ pBRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453675; x=1730058475; 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=160oEn3Hw+w/iZ7EFhTc6TJAcZCL3iOZo0psz4xgbB0=; b=w01yuEs8Zg1TbYr6NSw/5Uef3Uj51ccIoy+XSRmSVWA+FW7CkDROI7X/xwGvJI5V7N 6ACq8rdN6SiMyUpJtMFSIYbgFd+FGAUdm0uTJk54QnC3o/EO3WWWaHV2kC8FfH7P5yFS B44CHkN3/rRBmHKSVfx7olZUVoF4voR2Ru00BXKXYBymWzlFrMaCBAazLUHr0SFu/Y6i T7QIQvYB6p7GZiWGIQPlTs0182N9H5Bn3RMifWTZssI/zwZrDgVmOUlrjcei/ey1v23g D+s95UmgqM4XXhgF2NFVDoqG9bY7oqbdlMlsZDzznrcpz6+7++rpH4lPWyaT5bo+psHP i72Q== X-Forwarded-Encrypted: i=1; AJvYcCXR59MYeFXSeCAnB9vAhsxtPTndJg7MM3LkN9Sdr0ub6XDkJliatsFIfLfBVfcjD8Zg+Ptf6BU3uZO+VI8=@vger.kernel.org X-Gm-Message-State: AOJu0YxvZAfFYuG5+TFdQGL+R734JVozcORQ9aUcwZN3ufPSrKkJKmVY KYRIIxYDtHgyJPz7LUHHFYGUASVQzxpttunHDEGrudFUjuGzeEJ/aToKkmo5pEw= X-Google-Smtp-Source: AGHT+IFjSRTaADFfRKm/pM//eGuPDafKddIxjEZ6I/KXrnokbyilNV7LBvfBGmNKmpBhs1ancwD0CA== X-Received: by 2002:a17:90a:ab0e:b0:2e0:74c9:ecf1 with SMTP id 98e67ed59e1d1-2e561718d7amr7870206a91.10.1729453674952; Sun, 20 Oct 2024 12:47:54 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:54 -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 06/13] RISC-V: KVM: Don't setup SGEI for zero guest external interrupts Date: Mon, 21 Oct 2024 01:17:27 +0530 Message-ID: <20241020194734.58686-7-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" No need to setup SGEI local interrupt when there are zero guest external interrupts (i.e. zero HW IMSIC guest files). Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/aia.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index 17ae4a7c0e94..8ffae0330c89 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -499,6 +499,10 @@ static int aia_hgei_init(void) hgctrl->free_bitmap =3D 0; } =20 + /* Skip SGEI interrupt setup for zero guest external interrupts */ + if (!kvm_riscv_aia_nr_hgei) + goto skip_sgei_interrupt; + /* Find INTC irq domain */ domain =3D irq_find_matching_fwnode(riscv_get_intc_hwnode(), DOMAIN_BUS_ANY); @@ -522,11 +526,16 @@ static int aia_hgei_init(void) return rc; } =20 +skip_sgei_interrupt: return 0; } =20 static void aia_hgei_exit(void) { + /* Do nothing for zero guest external interrupts */ + if (!kvm_riscv_aia_nr_hgei) + return; + /* Free per-CPU SGEI interrupt */ free_percpu_irq(hgei_parent_irq, &aia_hgei); } --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 4C3DA199EAF for ; Sun, 20 Oct 2024 19:47:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453680; cv=none; b=J9R6JvzT/R5hgEKcncmPxCr+cIojo4j4a+2Xh6AMnXN4bahFzohjQL/p8R7OP46RX4UL4Qe+4orxn/8UaAMFWLAiOvgCD0A/PstemBRh9UaTnhw3D9SCl+wUddRz5PdQ84U8HNjKmN7zZNhmLJl2ufTpfm0Lkietvhx9DsdsKsA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453680; c=relaxed/simple; bh=T7tELBxGh11eBMeGnumK1jZo8RzYWiaMFUM5/ysdGAw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qto39ki3Pos0G9DkeKPZA5f8vEQAC8+skD+PhofjLwLWShKBiWUQfIdESIlB2UKy4tobaAX/z95j7EzrHpjB3N2twTgaIiQa33UfsbVdiFBOP/pxC2eIkA6HfXtzgyW8/WuBMcOop70CpKJvB9bP7mVgsTPaHWmYQsQLOx3zd9Y= 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=HDg2Lkjc; arc=none smtp.client-ip=209.85.216.50 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="HDg2Lkjc" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2e2fb304e7dso3132822a91.1 for ; Sun, 20 Oct 2024 12:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453678; x=1730058478; 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=KS7Zu5aP5OUy3H1PVCUGTrOwGn7CBou3wja5LgjZ/J0=; b=HDg2Lkjcy3LPRlU4llUeZML7Gg53MVKBPt609ChaOnbjMeCZhwdyMTZir7IoDDNoiG AxVlLAQ71bI1FprfIRua1MkhU8Ta3lyHF7bEevUmmQP3G+h3VbYpCahQgi+X0kq9796X HFAkQ0WsBXQtqIh2TGSOMDiEUF3edQaSmAVMnA1qtabgpyF/WuQmMiIEFSt0PHtYt+8G s7I5Fzl1Y0gcL8SQBhgzZcNzW3/pUc78TqROcD5mT1ZkohpZWO7Drg3JkMRkAmncGKY4 s4ut793bxGVeRlI8K1rUP2IrqSe2YjQkma7RLLh1B+LTOLgYUE47mYdIMavjWn1HMl/8 mrRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453678; x=1730058478; 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=KS7Zu5aP5OUy3H1PVCUGTrOwGn7CBou3wja5LgjZ/J0=; b=RHOkWiGbP+ha+WlsyqKBy9VY7kKD6zlro5ms0fkmmQaKU/alK6J0J7Rt/D4uidbWxP YyaNYppzJsaVdM4qQgCSzGzKX4bdDtRJheEARFN07LoYwRil7w51xuwy9oy0JD2PE+YH hy3WtaFoax/78Z8M0kQV+nleMpyGpSqeWYEaVq4tw2QsgBjphCUbBl4BvSc6l7KT5eKG Eqfc7ojiOPKXnbAd/KbuD5At6ZP9nz5wWCpee4Di8BPCJbvlObtZdThklH0xrgKOqxqw KeX6QUl0heF5m1a/tg+rQLeoue+VNroc+ZunH0XVCVZBVs+la9vB/VFABJqVl1Ed5FkP HbnQ== X-Forwarded-Encrypted: i=1; AJvYcCVgeAr0OAwQhkJQQ9evYI+ouvjOFozbBYRwZfvOL2/7if0+fuX/Bs5BaUWa0rOhFR/ddRbauKcvYhWz+Yk=@vger.kernel.org X-Gm-Message-State: AOJu0YznkFE67WLaxZhFk0LFAkXjYAamsVVf5aXzpENEktB2GDi5h79E NW/r6gxrVQfwGKMS3mnGaKSf0oBbsNSEfZ2pPY1WsqEXgmHjZsRhjQ6lXhzDkiA= X-Google-Smtp-Source: AGHT+IGnS7/U9pCy/yFTaLxnb90oRcnjeE7u+Mb775msHzTrrMgFD70FOBbx5TY/apdqG3+0xp0q3g== X-Received: by 2002:a17:90a:f002:b0:2e2:cd22:b092 with SMTP id 98e67ed59e1d1-2e5617572e6mr11026433a91.16.1729453677600; Sun, 20 Oct 2024 12:47:57 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:56 -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 07/13] RISC-V: Add defines for the SBI nested acceleration extension Date: Mon, 21 Oct 2024 01:17:28 +0530 Message-ID: <20241020194734.58686-8-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" Add defines for the new SBI nested acceleration extension which was ratified as part of the SBI v2.0 specification. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/include/asm/sbi.h | 120 +++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 98f631b051db..6c82318065cf 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -34,6 +34,7 @@ enum sbi_ext_id { SBI_EXT_PMU =3D 0x504D55, SBI_EXT_DBCN =3D 0x4442434E, SBI_EXT_STA =3D 0x535441, + SBI_EXT_NACL =3D 0x4E41434C, =20 /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START =3D 0x08000000, @@ -281,6 +282,125 @@ struct sbi_sta_struct { =20 #define SBI_SHMEM_DISABLE -1 =20 +enum sbi_ext_nacl_fid { + SBI_EXT_NACL_PROBE_FEATURE =3D 0x0, + SBI_EXT_NACL_SET_SHMEM =3D 0x1, + SBI_EXT_NACL_SYNC_CSR =3D 0x2, + SBI_EXT_NACL_SYNC_HFENCE =3D 0x3, + SBI_EXT_NACL_SYNC_SRET =3D 0x4, +}; + +enum sbi_ext_nacl_feature { + SBI_NACL_FEAT_SYNC_CSR =3D 0x0, + SBI_NACL_FEAT_SYNC_HFENCE =3D 0x1, + SBI_NACL_FEAT_SYNC_SRET =3D 0x2, + SBI_NACL_FEAT_AUTOSWAP_CSR =3D 0x3, +}; + +#define SBI_NACL_SHMEM_ADDR_SHIFT 12 +#define SBI_NACL_SHMEM_SCRATCH_OFFSET 0x0000 +#define SBI_NACL_SHMEM_SCRATCH_SIZE 0x1000 +#define SBI_NACL_SHMEM_SRET_OFFSET 0x0000 +#define SBI_NACL_SHMEM_SRET_SIZE 0x0200 +#define SBI_NACL_SHMEM_AUTOSWAP_OFFSET (SBI_NACL_SHMEM_SRET_OFFSET + \ + SBI_NACL_SHMEM_SRET_SIZE) +#define SBI_NACL_SHMEM_AUTOSWAP_SIZE 0x0080 +#define SBI_NACL_SHMEM_UNUSED_OFFSET (SBI_NACL_SHMEM_AUTOSWAP_OFFSET + \ + SBI_NACL_SHMEM_AUTOSWAP_SIZE) +#define SBI_NACL_SHMEM_UNUSED_SIZE 0x0580 +#define SBI_NACL_SHMEM_HFENCE_OFFSET (SBI_NACL_SHMEM_UNUSED_OFFSET + \ + SBI_NACL_SHMEM_UNUSED_SIZE) +#define SBI_NACL_SHMEM_HFENCE_SIZE 0x0780 +#define SBI_NACL_SHMEM_DBITMAP_OFFSET (SBI_NACL_SHMEM_HFENCE_OFFSET + \ + SBI_NACL_SHMEM_HFENCE_SIZE) +#define SBI_NACL_SHMEM_DBITMAP_SIZE 0x0080 +#define SBI_NACL_SHMEM_CSR_OFFSET (SBI_NACL_SHMEM_DBITMAP_OFFSET + \ + SBI_NACL_SHMEM_DBITMAP_SIZE) +#define SBI_NACL_SHMEM_CSR_SIZE ((__riscv_xlen / 8) * 1024) +#define SBI_NACL_SHMEM_SIZE (SBI_NACL_SHMEM_CSR_OFFSET + \ + SBI_NACL_SHMEM_CSR_SIZE) + +#define SBI_NACL_SHMEM_CSR_INDEX(__csr_num) \ + ((((__csr_num) & 0xc00) >> 2) | ((__csr_num) & 0xff)) + +#define SBI_NACL_SHMEM_HFENCE_ENTRY_SZ ((__riscv_xlen / 8) * 4) +#define SBI_NACL_SHMEM_HFENCE_ENTRY_MAX \ + (SBI_NACL_SHMEM_HFENCE_SIZE / \ + SBI_NACL_SHMEM_HFENCE_ENTRY_SZ) +#define SBI_NACL_SHMEM_HFENCE_ENTRY(__num) \ + (SBI_NACL_SHMEM_HFENCE_OFFSET + \ + (__num) * SBI_NACL_SHMEM_HFENCE_ENTRY_SZ) +#define SBI_NACL_SHMEM_HFENCE_ENTRY_CONFIG(__num) \ + SBI_NACL_SHMEM_HFENCE_ENTRY(__num) +#define SBI_NACL_SHMEM_HFENCE_ENTRY_PNUM(__num)\ + (SBI_NACL_SHMEM_HFENCE_ENTRY(__num) + (__riscv_xlen / 8)) +#define SBI_NACL_SHMEM_HFENCE_ENTRY_PCOUNT(__num)\ + (SBI_NACL_SHMEM_HFENCE_ENTRY(__num) + \ + ((__riscv_xlen / 8) * 3)) + +#define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_BITS 1 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_SHIFT \ + (__riscv_xlen - SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_BITS) +#define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_MASK \ + ((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_BITS) - 1) +#define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND \ + (SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_MASK << \ + SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_SHIFT) + +#define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_BITS 3 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_SHIFT \ + (SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_SHIFT - \ + SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_BITS) + +#define SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_BITS 4 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_SHIFT \ + (SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_SHIFT - \ + SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_BITS) +#define SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_MASK \ + ((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_BITS) - 1) + +#define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA 0x0 +#define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_ALL 0x1 +#define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_VMID 0x2 +#define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_VMID_ALL 0x3 +#define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA 0x4 +#define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ALL 0x5 +#define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ASID 0x6 +#define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ASID_ALL 0x7 + +#define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_BITS 1 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_SHIFT \ + (SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_SHIFT - \ + SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_BITS) + +#define SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_BITS 7 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_SHIFT \ + (SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_SHIFT - \ + SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_BITS) +#define SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_MASK \ + ((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_BITS) - 1) +#define SBI_NACL_SHMEM_HFENCE_ORDER_BASE 12 + +#if __riscv_xlen =3D=3D 32 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS 9 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_BITS 7 +#else +#define SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS 16 +#define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_BITS 14 +#endif +#define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_SHIFT \ + SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS +#define SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_MASK \ + ((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS) - 1) +#define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_MASK \ + ((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_BITS) - 1) + +#define SBI_NACL_SHMEM_AUTOSWAP_FLAG_HSTATUS BIT(0) +#define SBI_NACL_SHMEM_AUTOSWAP_HSTATUS ((__riscv_xlen / 8) * 1) + +#define SBI_NACL_SHMEM_SRET_X(__i) ((__riscv_xlen / 8) * (__i)) +#define SBI_NACL_SHMEM_SRET_X_LAST 31 + /* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 1B64A199FDE for ; Sun, 20 Oct 2024 19:47:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453682; cv=none; b=Yy4Ep+Ph57JNXmBAPdzaac1K+5Ye/7OG4TtBPy4LBqjKWw9ORAqjahFrVM0b9B4zIN0Tw93cs6yhV82h+7gIcNI9m54HVBAFrgMb1rSWHX5I4b095zL4UJNj0AxlGkUrb3B0l3fXHnhMRcHAnSB2Rjk8NZY0jubzJvDm1J5t3HA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453682; c=relaxed/simple; bh=aLBJo1op6p7EbwqsjHLsNImpiFMlqml80zE8MJz+O5c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dRdC1gOhXeeWfwzeSmQ+y/c2kPwJU+e5xf0I1QsuhbuAXQmk1dNl7YpPsafgvlVdHI2WnpUN7d5BXkOJbjujmCW5VV0cAN7RPVlS52tkFDwSzySkw4+X6JIzyUQpg8MOioy3asxY+gpGjhOsnQEnPY+Gm/I82SimiMoWCZgrpKU= 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=HTG4Vk3V; arc=none smtp.client-ip=209.85.216.51 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="HTG4Vk3V" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2e31af47681so3057781a91.2 for ; Sun, 20 Oct 2024 12:47:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453679; x=1730058479; 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=2PNHY6Ej4ODA3l2FaZRmpOmWoo1Wxqh6777J5n1pDa4=; b=HTG4Vk3Vv2Llo6fx72mBdtKZmypTPIfaVasAzNJ76U9a/pEPWdAMDgTTbOb1zPdftw 5WaI1mI4dH7q5V6v7q4PaI6qgZVxCdZY6kXVuzTeCXG+Qc+wYVVTZzoJZgVICPJMZFxZ q2e/1QtmX8ks2x0wtT9RuBcU8Rjx9RtBUQcEA6R6ltqZ5WfC4pbZeusGys0nxTieGrYv ZCXN24v1fQPSHGWKDilHe6hr27XIt8jaimmwrqiBARs3NP5lRILmf5Aq+hWMlHIK80U9 zagy76dKyZqq8KH1nuFPCxSkHhP5V2mkh2iYx5Cy9w8S7mPl4r9QxmE6lNSDabVgIKie A7Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453679; x=1730058479; 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=2PNHY6Ej4ODA3l2FaZRmpOmWoo1Wxqh6777J5n1pDa4=; b=aSlpNJnoGtpndjTDIKD/rZoY8doG3omRv5qfse9plCWBSJwLPCSzI7Iq+X5O+a53Qg F9+NJQKfqQsNk1UgJgftz2ZUAXF+Mz8gF7jSByawpVTZo1D751NwsckrRuQPAu1H+MID Yf0Qrh1R7iM896jM4W7OdzD+BAMWDwc0E+5w6ysHC0mH403YIA+1wlqpz7sbxC3ZNPp4 ZA4QiB+5dISZUZRUGzPg3cCH2mXfBhZzkHkCgmoQvZWOleZxvCnbdEBGguDH0SAikTkY RzZ5ef33Vwv0bSOHFTz2TfthMMicMj2POWhek/4OD3HZkU9f43wKaz9BuHtCn+43UFK7 Z8PQ== X-Forwarded-Encrypted: i=1; AJvYcCUHBunGJROG9jzMTwzZTwUSGZwnx2dp15BchGhF2yPYmw6TiKBEqj/iC9a9r62au1gZha9wPsh3/x0j4zw=@vger.kernel.org X-Gm-Message-State: AOJu0YwU/4++Gr3ovGkUPLlDfVJElIjxxhcFDTj1Hf6hcmXxuywA1VtK 2q0xkRzqparZ3J8AU9d1669OgYlf/ewIq0jYDoDg+k91HTG8UvajZ4tYNQhkPQM= X-Google-Smtp-Source: AGHT+IFl/0jkuzkA2N3qfrZw6TQdQzdowPrIIb9QTb2dp0fwvxYkkYmfrYtD1JWF8OFyEwLW30Af0A== X-Received: by 2002:a17:90a:15c9:b0:2e2:bad3:e393 with SMTP id 98e67ed59e1d1-2e5616de72amr10854988a91.3.1729453679214; Sun, 20 Oct 2024 12:47:59 -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.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:47:58 -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 08/13] RISC-V: KVM: Add common nested acceleration support Date: Mon, 21 Oct 2024 01:17:29 +0530 Message-ID: <20241020194734.58686-9-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" Add a common nested acceleration support which will be shared by all parts of KVM RISC-V. This nested acceleration support detects and enables SBI NACL extension usage based on static keys which ensures minimum impact on the non-nested scenario. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/include/asm/kvm_nacl.h | 239 ++++++++++++++++++++++++++++++ arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/main.c | 51 ++++++- arch/riscv/kvm/nacl.c | 152 +++++++++++++++++++ 4 files changed, 441 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/kvm_nacl.h create mode 100644 arch/riscv/kvm/nacl.c diff --git a/arch/riscv/include/asm/kvm_nacl.h b/arch/riscv/include/asm/kvm= _nacl.h new file mode 100644 index 000000000000..8f3e3ebf5017 --- /dev/null +++ b/arch/riscv/include/asm/kvm_nacl.h @@ -0,0 +1,239 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2024 Ventana Micro Systems Inc. + */ + +#ifndef __KVM_NACL_H +#define __KVM_NACL_H + +#include +#include +#include +#include +#include + +DECLARE_STATIC_KEY_FALSE(kvm_riscv_nacl_available); +#define kvm_riscv_nacl_available() \ + static_branch_unlikely(&kvm_riscv_nacl_available) + +DECLARE_STATIC_KEY_FALSE(kvm_riscv_nacl_sync_csr_available); +#define kvm_riscv_nacl_sync_csr_available() \ + static_branch_unlikely(&kvm_riscv_nacl_sync_csr_available) + +DECLARE_STATIC_KEY_FALSE(kvm_riscv_nacl_sync_hfence_available); +#define kvm_riscv_nacl_sync_hfence_available() \ + static_branch_unlikely(&kvm_riscv_nacl_sync_hfence_available) + +DECLARE_STATIC_KEY_FALSE(kvm_riscv_nacl_sync_sret_available); +#define kvm_riscv_nacl_sync_sret_available() \ + static_branch_unlikely(&kvm_riscv_nacl_sync_sret_available) + +DECLARE_STATIC_KEY_FALSE(kvm_riscv_nacl_autoswap_csr_available); +#define kvm_riscv_nacl_autoswap_csr_available() \ + static_branch_unlikely(&kvm_riscv_nacl_autoswap_csr_available) + +struct kvm_riscv_nacl { + void *shmem; + phys_addr_t shmem_phys; +}; +DECLARE_PER_CPU(struct kvm_riscv_nacl, kvm_riscv_nacl); + +void __kvm_riscv_nacl_hfence(void *shmem, + unsigned long control, + unsigned long page_num, + unsigned long page_count); + +int kvm_riscv_nacl_enable(void); + +void kvm_riscv_nacl_disable(void); + +void kvm_riscv_nacl_exit(void); + +int kvm_riscv_nacl_init(void); + +#ifdef CONFIG_32BIT +#define lelong_to_cpu(__x) le32_to_cpu(__x) +#define cpu_to_lelong(__x) cpu_to_le32(__x) +#else +#define lelong_to_cpu(__x) le64_to_cpu(__x) +#define cpu_to_lelong(__x) cpu_to_le64(__x) +#endif + +#define nacl_shmem() \ + this_cpu_ptr(&kvm_riscv_nacl)->shmem + +#define nacl_scratch_read_long(__shmem, __offset) \ +({ \ + unsigned long *__p =3D (__shmem) + \ + SBI_NACL_SHMEM_SCRATCH_OFFSET + \ + (__offset); \ + lelong_to_cpu(*__p); \ +}) + +#define nacl_scratch_write_long(__shmem, __offset, __val) \ +do { \ + unsigned long *__p =3D (__shmem) + \ + SBI_NACL_SHMEM_SCRATCH_OFFSET + \ + (__offset); \ + *__p =3D cpu_to_lelong(__val); \ +} while (0) + +#define nacl_scratch_write_longs(__shmem, __offset, __array, __count) \ +do { \ + unsigned int __i; \ + unsigned long *__p =3D (__shmem) + \ + SBI_NACL_SHMEM_SCRATCH_OFFSET + \ + (__offset); \ + for (__i =3D 0; __i < (__count); __i++) \ + __p[__i] =3D cpu_to_lelong((__array)[__i]); \ +} while (0) + +#define nacl_sync_hfence(__e) \ + sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_SYNC_HFENCE, \ + (__e), 0, 0, 0, 0, 0) + +#define nacl_hfence_mkconfig(__type, __order, __vmid, __asid) \ +({ \ + unsigned long __c =3D SBI_NACL_SHMEM_HFENCE_CONFIG_PEND; \ + __c |=3D ((__type) & SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_MASK) \ + << SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_SHIFT; \ + __c |=3D (((__order) - SBI_NACL_SHMEM_HFENCE_ORDER_BASE) & \ + SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_MASK) \ + << SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_SHIFT; \ + __c |=3D ((__vmid) & SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_MASK) \ + << SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_SHIFT; \ + __c |=3D ((__asid) & SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_MASK); \ + __c; \ +}) + +#define nacl_hfence_mkpnum(__order, __addr) \ + ((__addr) >> (__order)) + +#define nacl_hfence_mkpcount(__order, __size) \ + ((__size) >> (__order)) + +#define nacl_hfence_gvma(__shmem, __gpa, __gpsz, __order) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_GVMA, \ + __order, 0, 0), \ + nacl_hfence_mkpnum(__order, __gpa), \ + nacl_hfence_mkpcount(__order, __gpsz)) + +#define nacl_hfence_gvma_all(__shmem) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_ALL, \ + 0, 0, 0), 0, 0) + +#define nacl_hfence_gvma_vmid(__shmem, __vmid, __gpa, __gpsz, __order) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_VMID, \ + __order, __vmid, 0), \ + nacl_hfence_mkpnum(__order, __gpa), \ + nacl_hfence_mkpcount(__order, __gpsz)) + +#define nacl_hfence_gvma_vmid_all(__shmem, __vmid) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_VMID_ALL, \ + 0, __vmid, 0), 0, 0) + +#define nacl_hfence_vvma(__shmem, __vmid, __gva, __gvsz, __order) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_VVMA, \ + __order, __vmid, 0), \ + nacl_hfence_mkpnum(__order, __gva), \ + nacl_hfence_mkpcount(__order, __gvsz)) + +#define nacl_hfence_vvma_all(__shmem, __vmid) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ALL, \ + 0, __vmid, 0), 0, 0) + +#define nacl_hfence_vvma_asid(__shmem, __vmid, __asid, __gva, __gvsz, __or= der)\ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ASID, \ + __order, __vmid, __asid), \ + nacl_hfence_mkpnum(__order, __gva), \ + nacl_hfence_mkpcount(__order, __gvsz)) + +#define nacl_hfence_vvma_asid_all(__shmem, __vmid, __asid) \ +__kvm_riscv_nacl_hfence(__shmem, \ + nacl_hfence_mkconfig(SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ASID_ALL, \ + 0, __vmid, __asid), 0, 0) + +#define nacl_csr_read(__shmem, __csr) \ +({ \ + unsigned long *__a =3D (__shmem) + SBI_NACL_SHMEM_CSR_OFFSET; \ + lelong_to_cpu(__a[SBI_NACL_SHMEM_CSR_INDEX(__csr)]); \ +}) + +#define nacl_csr_write(__shmem, __csr, __val) \ +do { \ + void *__s =3D (__shmem); \ + unsigned int __i =3D SBI_NACL_SHMEM_CSR_INDEX(__csr); \ + unsigned long *__a =3D (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + u8 *__b =3D (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ + __a[__i] =3D cpu_to_lelong(__val); \ + __b[__i >> 3] |=3D 1U << (__i & 0x7); \ +} while (0) + +#define nacl_csr_swap(__shmem, __csr, __val) \ +({ \ + void *__s =3D (__shmem); \ + unsigned int __i =3D SBI_NACL_SHMEM_CSR_INDEX(__csr); \ + unsigned long *__a =3D (__s) + SBI_NACL_SHMEM_CSR_OFFSET; \ + u8 *__b =3D (__s) + SBI_NACL_SHMEM_DBITMAP_OFFSET; \ + unsigned long __r =3D lelong_to_cpu(__a[__i]); \ + __a[__i] =3D cpu_to_lelong(__val); \ + __b[__i >> 3] |=3D 1U << (__i & 0x7); \ + __r; \ +}) + +#define nacl_sync_csr(__csr) \ + sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_SYNC_CSR, \ + (__csr), 0, 0, 0, 0, 0) + +/* + * Each ncsr_xyz() macro defined below has it's own static-branch so every + * use of ncsr_xyz() macro emits a patchable direct jump. This means multi= ple + * back-to-back ncsr_xyz() macro usage will emit multiple patchable direct + * jumps which is sub-optimal. + * + * Based on the above, it is recommended to avoid multiple back-to-back + * ncsr_xyz() macro usage. + */ + +#define ncsr_read(__csr) \ +({ \ + unsigned long __r; \ + if (kvm_riscv_nacl_available()) \ + __r =3D nacl_csr_read(nacl_shmem(), __csr); \ + else \ + __r =3D csr_read(__csr); \ + __r; \ +}) + +#define ncsr_write(__csr, __val) \ +do { \ + if (kvm_riscv_nacl_sync_csr_available()) \ + nacl_csr_write(nacl_shmem(), __csr, __val); \ + else \ + csr_write(__csr, __val); \ +} while (0) + +#define ncsr_swap(__csr, __val) \ +({ \ + unsigned long __r; \ + if (kvm_riscv_nacl_sync_csr_available()) \ + __r =3D nacl_csr_swap(nacl_shmem(), __csr, __val); \ + else \ + __r =3D csr_swap(__csr, __val); \ + __r; \ +}) + +#define nsync_csr(__csr) \ +do { \ + if (kvm_riscv_nacl_sync_csr_available()) \ + nacl_sync_csr(__csr); \ +} while (0) + +#endif diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index c1eac0d093de..0fb1840c3e0a 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -16,6 +16,7 @@ kvm-y +=3D aia_device.o kvm-y +=3D aia_imsic.o kvm-y +=3D main.o kvm-y +=3D mmu.o +kvm-y +=3D nacl.o kvm-y +=3D tlb.o kvm-y +=3D vcpu.o kvm-y +=3D vcpu_exit.o diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 5682e338ae6d..1fa8be5ee509 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -10,8 +10,8 @@ #include #include #include -#include #include +#include #include =20 long kvm_arch_dev_ioctl(struct file *filp, @@ -22,6 +22,12 @@ long kvm_arch_dev_ioctl(struct file *filp, =20 int kvm_arch_enable_virtualization_cpu(void) { + int rc; + + rc =3D kvm_riscv_nacl_enable(); + if (rc) + return rc; + csr_write(CSR_HEDELEG, KVM_HEDELEG_DEFAULT); csr_write(CSR_HIDELEG, KVM_HIDELEG_DEFAULT); =20 @@ -49,17 +55,21 @@ void kvm_arch_disable_virtualization_cpu(void) csr_write(CSR_HVIP, 0); csr_write(CSR_HEDELEG, 0); csr_write(CSR_HIDELEG, 0); + + kvm_riscv_nacl_disable(); } =20 static void kvm_riscv_teardown(void) { kvm_riscv_aia_exit(); + kvm_riscv_nacl_exit(); kvm_unregister_perf_callbacks(); } =20 static int __init riscv_kvm_init(void) { int rc; + char slist[64]; const char *str; =20 if (!riscv_isa_extension_available(NULL, h)) { @@ -77,16 +87,53 @@ static int __init riscv_kvm_init(void) return -ENODEV; } =20 + rc =3D kvm_riscv_nacl_init(); + if (rc && rc !=3D -ENODEV) + return rc; + kvm_riscv_gstage_mode_detect(); =20 kvm_riscv_gstage_vmid_detect(); =20 rc =3D kvm_riscv_aia_init(); - if (rc && rc !=3D -ENODEV) + if (rc && rc !=3D -ENODEV) { + kvm_riscv_nacl_exit(); return rc; + } =20 kvm_info("hypervisor extension available\n"); =20 + if (kvm_riscv_nacl_available()) { + rc =3D 0; + slist[0] =3D '\0'; + if (kvm_riscv_nacl_sync_csr_available()) { + if (rc) + strcat(slist, ", "); + strcat(slist, "sync_csr"); + rc++; + } + if (kvm_riscv_nacl_sync_hfence_available()) { + if (rc) + strcat(slist, ", "); + strcat(slist, "sync_hfence"); + rc++; + } + if (kvm_riscv_nacl_sync_sret_available()) { + if (rc) + strcat(slist, ", "); + strcat(slist, "sync_sret"); + rc++; + } + if (kvm_riscv_nacl_autoswap_csr_available()) { + if (rc) + strcat(slist, ", "); + strcat(slist, "autoswap_csr"); + rc++; + } + kvm_info("using SBI nested acceleration with %s\n", + (rc) ? slist : "no features"); + } + switch (kvm_riscv_gstage_mode()) { case HGATP_MODE_SV32X4: str =3D "Sv32x4"; diff --git a/arch/riscv/kvm/nacl.c b/arch/riscv/kvm/nacl.c new file mode 100644 index 000000000000..08a95ad9ada2 --- /dev/null +++ b/arch/riscv/kvm/nacl.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2024 Ventana Micro Systems Inc. + */ + +#include +#include +#include + +DEFINE_STATIC_KEY_FALSE(kvm_riscv_nacl_available); +DEFINE_STATIC_KEY_FALSE(kvm_riscv_nacl_sync_csr_available); +DEFINE_STATIC_KEY_FALSE(kvm_riscv_nacl_sync_hfence_available); +DEFINE_STATIC_KEY_FALSE(kvm_riscv_nacl_sync_sret_available); +DEFINE_STATIC_KEY_FALSE(kvm_riscv_nacl_autoswap_csr_available); +DEFINE_PER_CPU(struct kvm_riscv_nacl, kvm_riscv_nacl); + +void __kvm_riscv_nacl_hfence(void *shmem, + unsigned long control, + unsigned long page_num, + unsigned long page_count) +{ + int i, ent =3D -1, try_count =3D 5; + unsigned long *entp; + +again: + for (i =3D 0; i < SBI_NACL_SHMEM_HFENCE_ENTRY_MAX; i++) { + entp =3D shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_CONFIG(i); + if (lelong_to_cpu(*entp) & SBI_NACL_SHMEM_HFENCE_CONFIG_PEND) + continue; + + ent =3D i; + break; + } + + if (ent < 0) { + if (try_count) { + nacl_sync_hfence(-1UL); + goto again; + } else { + pr_warn("KVM: No free entry in NACL shared memory\n"); + return; + } + } + + entp =3D shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_CONFIG(i); + *entp =3D cpu_to_lelong(control); + entp =3D shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_PNUM(i); + *entp =3D cpu_to_lelong(page_num); + entp =3D shmem + SBI_NACL_SHMEM_HFENCE_ENTRY_PCOUNT(i); + *entp =3D cpu_to_lelong(page_count); +} + +int kvm_riscv_nacl_enable(void) +{ + int rc; + struct sbiret ret; + struct kvm_riscv_nacl *nacl; + + if (!kvm_riscv_nacl_available()) + return 0; + nacl =3D this_cpu_ptr(&kvm_riscv_nacl); + + ret =3D sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_SET_SHMEM, + nacl->shmem_phys, 0, 0, 0, 0, 0); + rc =3D sbi_err_map_linux_errno(ret.error); + if (rc) + return rc; + + return 0; +} + +void kvm_riscv_nacl_disable(void) +{ + if (!kvm_riscv_nacl_available()) + return; + + sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_SET_SHMEM, + SBI_SHMEM_DISABLE, SBI_SHMEM_DISABLE, 0, 0, 0, 0); +} + +void kvm_riscv_nacl_exit(void) +{ + int cpu; + struct kvm_riscv_nacl *nacl; + + if (!kvm_riscv_nacl_available()) + return; + + /* Allocate per-CPU shared memory */ + for_each_possible_cpu(cpu) { + nacl =3D per_cpu_ptr(&kvm_riscv_nacl, cpu); + if (!nacl->shmem) + continue; + + free_pages((unsigned long)nacl->shmem, + get_order(SBI_NACL_SHMEM_SIZE)); + nacl->shmem =3D NULL; + nacl->shmem_phys =3D 0; + } +} + +static long nacl_probe_feature(long feature_id) +{ + struct sbiret ret; + + if (!kvm_riscv_nacl_available()) + return 0; + + ret =3D sbi_ecall(SBI_EXT_NACL, SBI_EXT_NACL_PROBE_FEATURE, + feature_id, 0, 0, 0, 0, 0); + return ret.value; +} + +int kvm_riscv_nacl_init(void) +{ + int cpu; + struct page *shmem_page; + struct kvm_riscv_nacl *nacl; + + if (sbi_spec_version < sbi_mk_version(1, 0) || + sbi_probe_extension(SBI_EXT_NACL) <=3D 0) + return -ENODEV; + + /* Enable NACL support */ + static_branch_enable(&kvm_riscv_nacl_available); + + /* Probe NACL features */ + if (nacl_probe_feature(SBI_NACL_FEAT_SYNC_CSR)) + static_branch_enable(&kvm_riscv_nacl_sync_csr_available); + if (nacl_probe_feature(SBI_NACL_FEAT_SYNC_HFENCE)) + static_branch_enable(&kvm_riscv_nacl_sync_hfence_available); + if (nacl_probe_feature(SBI_NACL_FEAT_SYNC_SRET)) + static_branch_enable(&kvm_riscv_nacl_sync_sret_available); + if (nacl_probe_feature(SBI_NACL_FEAT_AUTOSWAP_CSR)) + static_branch_enable(&kvm_riscv_nacl_autoswap_csr_available); + + /* Allocate per-CPU shared memory */ + for_each_possible_cpu(cpu) { + nacl =3D per_cpu_ptr(&kvm_riscv_nacl, cpu); + + shmem_page =3D alloc_pages(GFP_KERNEL | __GFP_ZERO, + get_order(SBI_NACL_SHMEM_SIZE)); + if (!shmem_page) { + kvm_riscv_nacl_exit(); + return -ENOMEM; + } + nacl->shmem =3D page_to_virt(shmem_page); + nacl->shmem_phys =3D page_to_phys(shmem_page); + } + + return 0; +} --=20 2.43.0 From nobody Tue Nov 26 06:25:30 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 From nobody Tue Nov 26 06:25:30 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 BD5D319AD89 for ; Sun, 20 Oct 2024 19:48:03 +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=1729453685; cv=none; b=ft1mXg1SkndCME7prX9A38o/oDX7jk5R10bng0JNXnZn3xZ1vteiWs71ZAPh36y2IEhEEQUS2c6v1kPnftZpUm2/cYBj9U4862T+rRN3x6n9P5dgNzXxSJRAJnuK8erW17nMkukzO9jzInSIoZaCAiQ91A5Trpj6zCPPwnnJC10= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453685; c=relaxed/simple; bh=6Ws710PoAAvwNXc2iPqor0uTF1NrnxyfADVPvl1EQyY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U2Eazpvqn9WmLQ12E6YqAUpXVeQMp9BuuRRhff99OHfD/Dtb7z8kiKmJ4V+4MI1eJKiuQdhdQ0RxAwQDiBH00f1HaWUv0aT6H/rUd5sxV9xAAiG3LZqzrJelKCLtIpLPhpQrdsu8HeOC4N82PBAbw7l5ygcHUMlrPGZGUnb4/bs= 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=SN/SWH3/; 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="SN/SWH3/" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2e2c6bc4840so2731868a91.2 for ; Sun, 20 Oct 2024 12:48:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453683; x=1730058483; 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=KOSHWqnsb0W3cOOf6DyqEOJr3wAY0C67ufwSo4h3/A8=; b=SN/SWH3/38Zi486UqY1ji7eOLCyfus105fyCasRwMy+sTzpmHZYje9+8HEZ/e6G5Iv 2EoFe+BPHlVDk4ZLMS6Jh25Cy1LToyCaO9E0L1N0OSSDkIN6w4v5C6qBRuHnL1lVfJaw EM+qDycjoX5JzgEB3mZtnV97fMOD7o9qErPnQ78/8MdXdmMKgKW1Iz1ykSxvQHaYv1Xn jMMIK5a1CWjOL8Wpb/gJzme7KYmaUmGDXZFkP5JZTi9tukUD4Ea/vm/VODcKrk8BfJYj vqonkT2YNI6639JAgDpskGUB18Hy/7ThKQblO72CNaTEvjF38wBCSFowUyc378M7kCtE sKOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453683; x=1730058483; 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=KOSHWqnsb0W3cOOf6DyqEOJr3wAY0C67ufwSo4h3/A8=; b=Kzw/pwu4YWB+sjKPvSh1RkZ2loHmlKW5sWxrmF9WSd7gd0c2U9cgssiAKRuOW+lz86 Rq+kLfUIuLo+tBsK8DyFflEuLuC3IloVLHIthaT1EuW9KRb3W5bW1berV6mPUsyCHXG5 p/riT96dSvCetl3haky8M/oKJoZKnLNKWXsda0Y99B5xZw/m9SZEbOkq/YUap0AhSj5R r1+XjMgMpLJbUM40d94mJAlcB+epQd5Z+nlNAF80rzeej3j1FgUkoBd2mG3A8KGwtotn kh/N1DHSFVP/a9TNJUZ8yqrX1/5tHS3bXNKhRDD6Xf2C4jeJEiE36OpF0W8YQXhJdIij 4i6Q== X-Forwarded-Encrypted: i=1; AJvYcCXvJ66iV8Ta/hOzrqPfCs/v4c1C3khd1ObcUoxPEgNwUNOWLaiRPSi93IkdJRSR3HowjYkYVIHox63fD1g=@vger.kernel.org X-Gm-Message-State: AOJu0YwdmdFLKUtgOpakl9kb9hKv2yvfuQnJ06dS0UaoXVUiTqKgY2Rq dvnfpJ5Xh5pkyu2i7kSFp98SUcwOGEPgT//lM6WO3PW7lsmZOJmSZWLJ6fLmcdY= X-Google-Smtp-Source: AGHT+IGGt5JhB4IG8+i0HAXwMX0R3ki791FJ5Pz6hTqm+U8OcKSwC7G5E4tWlwhk9qK0fqoTXOXa+A== X-Received: by 2002:a17:90b:20a:b0:2e2:af52:a7b7 with SMTP id 98e67ed59e1d1-2e561a008ebmr11046511a91.34.1729453682889; Sun, 20 Oct 2024 12:48:02 -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.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:48:02 -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 10/13] RISC-V: KVM: Use nacl_csr_xyz() for accessing AIA CSRs Date: Mon, 21 Oct 2024 01:17:31 +0530 Message-ID: <20241020194734.58686-11-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 AIA 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/aia.c | 97 ++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 34 deletions(-) diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index 8ffae0330c89..dcced4db7fe8 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 struct aia_hgei_control { raw_spinlock_t lock; @@ -88,7 +89,7 @@ void kvm_riscv_vcpu_aia_sync_interrupts(struct kvm_vcpu *= vcpu) struct kvm_vcpu_aia_csr *csr =3D &vcpu->arch.aia_context.guest_csr; =20 if (kvm_riscv_aia_available()) - csr->vsieh =3D csr_read(CSR_VSIEH); + csr->vsieh =3D ncsr_read(CSR_VSIEH); } #endif =20 @@ -115,7 +116,7 @@ bool kvm_riscv_vcpu_aia_has_interrupts(struct kvm_vcpu = *vcpu, u64 mask) =20 hgei =3D aia_find_hgei(vcpu); if (hgei > 0) - return !!(csr_read(CSR_HGEIP) & BIT(hgei)); + return !!(ncsr_read(CSR_HGEIP) & BIT(hgei)); =20 return false; } @@ -128,45 +129,73 @@ void kvm_riscv_vcpu_aia_update_hvip(struct kvm_vcpu *= vcpu) return; =20 #ifdef CONFIG_32BIT - csr_write(CSR_HVIPH, vcpu->arch.aia_context.guest_csr.hviph); + ncsr_write(CSR_HVIPH, vcpu->arch.aia_context.guest_csr.hviph); #endif - csr_write(CSR_HVICTL, aia_hvictl_value(!!(csr->hvip & BIT(IRQ_VS_EXT)))); + ncsr_write(CSR_HVICTL, aia_hvictl_value(!!(csr->hvip & BIT(IRQ_VS_EXT)))); } =20 void kvm_riscv_vcpu_aia_load(struct kvm_vcpu *vcpu, int cpu) { struct kvm_vcpu_aia_csr *csr =3D &vcpu->arch.aia_context.guest_csr; + void *nsh; =20 if (!kvm_riscv_aia_available()) return; =20 - csr_write(CSR_VSISELECT, csr->vsiselect); - csr_write(CSR_HVIPRIO1, csr->hviprio1); - csr_write(CSR_HVIPRIO2, csr->hviprio2); + if (kvm_riscv_nacl_sync_csr_available()) { + nsh =3D nacl_shmem(); + nacl_csr_write(nsh, CSR_VSISELECT, csr->vsiselect); + nacl_csr_write(nsh, CSR_HVIPRIO1, csr->hviprio1); + nacl_csr_write(nsh, CSR_HVIPRIO2, csr->hviprio2); +#ifdef CONFIG_32BIT + nacl_csr_write(nsh, CSR_VSIEH, csr->vsieh); + nacl_csr_write(nsh, CSR_HVIPH, csr->hviph); + nacl_csr_write(nsh, CSR_HVIPRIO1H, csr->hviprio1h); + nacl_csr_write(nsh, CSR_HVIPRIO2H, csr->hviprio2h); +#endif + } else { + csr_write(CSR_VSISELECT, csr->vsiselect); + csr_write(CSR_HVIPRIO1, csr->hviprio1); + csr_write(CSR_HVIPRIO2, csr->hviprio2); #ifdef CONFIG_32BIT - csr_write(CSR_VSIEH, csr->vsieh); - csr_write(CSR_HVIPH, csr->hviph); - csr_write(CSR_HVIPRIO1H, csr->hviprio1h); - csr_write(CSR_HVIPRIO2H, csr->hviprio2h); + csr_write(CSR_VSIEH, csr->vsieh); + csr_write(CSR_HVIPH, csr->hviph); + csr_write(CSR_HVIPRIO1H, csr->hviprio1h); + csr_write(CSR_HVIPRIO2H, csr->hviprio2h); #endif + } } =20 void kvm_riscv_vcpu_aia_put(struct kvm_vcpu *vcpu) { struct kvm_vcpu_aia_csr *csr =3D &vcpu->arch.aia_context.guest_csr; + void *nsh; =20 if (!kvm_riscv_aia_available()) return; =20 - csr->vsiselect =3D csr_read(CSR_VSISELECT); - csr->hviprio1 =3D csr_read(CSR_HVIPRIO1); - csr->hviprio2 =3D csr_read(CSR_HVIPRIO2); + if (kvm_riscv_nacl_available()) { + nsh =3D nacl_shmem(); + csr->vsiselect =3D nacl_csr_read(nsh, CSR_VSISELECT); + csr->hviprio1 =3D nacl_csr_read(nsh, CSR_HVIPRIO1); + csr->hviprio2 =3D nacl_csr_read(nsh, CSR_HVIPRIO2); #ifdef CONFIG_32BIT - csr->vsieh =3D csr_read(CSR_VSIEH); - csr->hviph =3D csr_read(CSR_HVIPH); - csr->hviprio1h =3D csr_read(CSR_HVIPRIO1H); - csr->hviprio2h =3D csr_read(CSR_HVIPRIO2H); + csr->vsieh =3D nacl_csr_read(nsh, CSR_VSIEH); + csr->hviph =3D nacl_csr_read(nsh, CSR_HVIPH); + csr->hviprio1h =3D nacl_csr_read(nsh, CSR_HVIPRIO1H); + csr->hviprio2h =3D nacl_csr_read(nsh, CSR_HVIPRIO2H); #endif + } else { + csr->vsiselect =3D csr_read(CSR_VSISELECT); + csr->hviprio1 =3D csr_read(CSR_HVIPRIO1); + csr->hviprio2 =3D csr_read(CSR_HVIPRIO2); +#ifdef CONFIG_32BIT + csr->vsieh =3D csr_read(CSR_VSIEH); + csr->hviph =3D csr_read(CSR_HVIPH); + csr->hviprio1h =3D csr_read(CSR_HVIPRIO1H); + csr->hviprio2h =3D csr_read(CSR_HVIPRIO2H); +#endif + } } =20 int kvm_riscv_vcpu_aia_get_csr(struct kvm_vcpu *vcpu, @@ -250,20 +279,20 @@ static u8 aia_get_iprio8(struct kvm_vcpu *vcpu, unsig= ned int irq) =20 switch (bitpos / BITS_PER_LONG) { case 0: - hviprio =3D csr_read(CSR_HVIPRIO1); + hviprio =3D ncsr_read(CSR_HVIPRIO1); break; case 1: #ifndef CONFIG_32BIT - hviprio =3D csr_read(CSR_HVIPRIO2); + hviprio =3D ncsr_read(CSR_HVIPRIO2); break; #else - hviprio =3D csr_read(CSR_HVIPRIO1H); + hviprio =3D ncsr_read(CSR_HVIPRIO1H); break; case 2: - hviprio =3D csr_read(CSR_HVIPRIO2); + hviprio =3D ncsr_read(CSR_HVIPRIO2); break; case 3: - hviprio =3D csr_read(CSR_HVIPRIO2H); + hviprio =3D ncsr_read(CSR_HVIPRIO2H); break; #endif default: @@ -283,20 +312,20 @@ static void aia_set_iprio8(struct kvm_vcpu *vcpu, uns= igned int irq, u8 prio) =20 switch (bitpos / BITS_PER_LONG) { case 0: - hviprio =3D csr_read(CSR_HVIPRIO1); + hviprio =3D ncsr_read(CSR_HVIPRIO1); break; case 1: #ifndef CONFIG_32BIT - hviprio =3D csr_read(CSR_HVIPRIO2); + hviprio =3D ncsr_read(CSR_HVIPRIO2); break; #else - hviprio =3D csr_read(CSR_HVIPRIO1H); + hviprio =3D ncsr_read(CSR_HVIPRIO1H); break; case 2: - hviprio =3D csr_read(CSR_HVIPRIO2); + hviprio =3D ncsr_read(CSR_HVIPRIO2); break; case 3: - hviprio =3D csr_read(CSR_HVIPRIO2H); + hviprio =3D ncsr_read(CSR_HVIPRIO2H); break; #endif default: @@ -308,20 +337,20 @@ static void aia_set_iprio8(struct kvm_vcpu *vcpu, uns= igned int irq, u8 prio) =20 switch (bitpos / BITS_PER_LONG) { case 0: - csr_write(CSR_HVIPRIO1, hviprio); + ncsr_write(CSR_HVIPRIO1, hviprio); break; case 1: #ifndef CONFIG_32BIT - csr_write(CSR_HVIPRIO2, hviprio); + ncsr_write(CSR_HVIPRIO2, hviprio); break; #else - csr_write(CSR_HVIPRIO1H, hviprio); + ncsr_write(CSR_HVIPRIO1H, hviprio); break; case 2: - csr_write(CSR_HVIPRIO2, hviprio); + ncsr_write(CSR_HVIPRIO2, hviprio); break; case 3: - csr_write(CSR_HVIPRIO2H, hviprio); + ncsr_write(CSR_HVIPRIO2H, hviprio); break; #endif default: @@ -377,7 +406,7 @@ int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, = unsigned int csr_num, return KVM_INSN_ILLEGAL_TRAP; =20 /* First try to emulate in kernel space */ - isel =3D csr_read(CSR_VSISELECT) & ISELECT_MASK; + isel =3D ncsr_read(CSR_VSISELECT) & ISELECT_MASK; if (isel >=3D ISELECT_IPRIO0 && isel <=3D ISELECT_IPRIO15) return aia_rmw_iprio(vcpu, isel, val, new_val, wr_mask); else if (isel >=3D IMSIC_FIRST && isel <=3D IMSIC_LAST && --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 1114419C55C for ; Sun, 20 Oct 2024 19:48:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453688; cv=none; b=VfKsrHqSLr4uBmI3jtfXkmrghsxBXogMED6DJm39aYME+Akd8flYjI2X7MKjp/TypvRoDJC9nbPFmts8njayqzEDkdAwMSnSMZeufkUO37VmAEFLHw8h9Qy7eUYFeXSIIprLh8gmUd/l9NgCNvDMCShnXHIkx1oJoZqwW7x4v+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453688; c=relaxed/simple; bh=vjKQC0zoF7G9cM7ssIkz36wmQJSD3vz5oHrHzckRPHg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JGjVzJrtdQKmL/iRT3YTf4P3fre0a5bePOLdRb2EmpzaxVXFK8cBfvYRjJxUP84Vx3CKWt8Cv4sixjtx2/UqnJLNaREniNbYNhkwBbePAQofJ9U+0t9V5Z5CLpfT/eEVwXMEaZFSjuz5N8LEQh9MSZQ0bCl10hpksPmMkeFxopc= 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=KO2NmrsO; arc=none smtp.client-ip=209.85.216.53 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="KO2NmrsO" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2e31af47681so3057825a91.2 for ; Sun, 20 Oct 2024 12:48:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453685; x=1730058485; 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=4C7ZduGC7pkN/JUl6gfB/F71HinWDzomU4MOEbWcFt4=; b=KO2NmrsONf8EXrQGO9D9vu2AwQwHnUijQsZ8qN/6rI+DAwdi2TGR23yMHng/54/Im/ iQqn0+POZ/DkkIdgYUuE+y7x8g6k7GarwLIkcxUyPjcENYb8X0kWLST3g15ACo+YNuH9 BYd5ST7FU3F4otKoFScOeXrjYHXZi7J+cxyGUchk2ekIwH72xlOL0FJqcKO3J82XxyzG xxeU7jDPNh6AZrtdYV9/s3hAYZvF1WKmkJ4Cq6Gx18/QXahEFDnzTcYXb5alvRmRQdVF MYe3533+zsvCc9MAw4VYuwOVXdy6kUUJQeq0bM2jjpbbq79kfoWyzWQLlaywpUdbAiMD DSKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453685; x=1730058485; 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=4C7ZduGC7pkN/JUl6gfB/F71HinWDzomU4MOEbWcFt4=; b=E/Wp5kenR+mKoZnDSk5qgz21hYBwesItkY0Y48f/5XUl0gkoYHiZ1JSLfTVrPxsvOR wB4o34zPRpR+0Hf6G85HNQZQ0gkO3DxsewZiS+1ZEhIGIpRkeIInAPzqSW0jKLU6wP5L QEPhbQ5N5novjM3TvJL1dxQrkLZAZXLN0biEC1Mr+P5m2DzpI5sQXMxXCblCX0LdUB9C xpRF3k2HNk3N6L8PkEoyZeMdBOln3gNjFAAokkzXfbaHR/feK3AXXqAkfjzdML1GS/Fi IyXkQUV9V4fv9JX7EQdCDXMn5yjuay3Mn4xkaIsvlLjaFE6HKeMhrtuvnoavKmTVvo8I gwFw== X-Forwarded-Encrypted: i=1; AJvYcCU749AE7jA5NfpSZTbjsTZp8ZUZczNE3+6Yb9YKFphETsQpUyd/bfhH5vRs4a2TYOUV8ATQbC88nqy/qKo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy2GXHM0ytGDOnOj0GvXC3piRxkxfS3CyKiMIvO3YIaf3F4KsQ5 DjbqROvzF4FgLGoHvFlMUPg46VqmhrDUafwyYxYlEA8bQ50YiMXoLp2VY63vq8U= X-Google-Smtp-Source: AGHT+IG/11qYdtlLvAcC5cZkvAaWerEVivMA/dGwxaUtWguVGnxVgD9ylKyt8Lr3PM/GRQspnVP0aw== X-Received: by 2002:a17:90a:15c9:b0:2e2:bad3:e393 with SMTP id 98e67ed59e1d1-2e5616de72amr10855206a91.3.1729453685228; Sun, 20 Oct 2024 12:48:05 -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.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:48:04 -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 11/13] RISC-V: KVM: Use SBI sync SRET call when available Date: Mon, 21 Oct 2024 01:17:32 +0530 Message-ID: <20241020194734.58686-12-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" Implement an optimized KVM world-switch using SBI sync SRET call when SBI nested acceleration extension is available. This improves KVM world-switch when KVM RISC-V is running as a Guest under some other hypervisor. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/include/asm/kvm_nacl.h | 6 ++++ arch/riscv/kvm/vcpu.c | 48 ++++++++++++++++++++++++++++--- arch/riscv/kvm/vcpu_switch.S | 29 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/arch/riscv/include/asm/kvm_nacl.h b/arch/riscv/include/asm/kvm= _nacl.h index 8f3e3ebf5017..4124d5e06a0f 100644 --- a/arch/riscv/include/asm/kvm_nacl.h +++ b/arch/riscv/include/asm/kvm_nacl.h @@ -12,6 +12,8 @@ #include #include =20 +struct kvm_vcpu_arch; + DECLARE_STATIC_KEY_FALSE(kvm_riscv_nacl_available); #define kvm_riscv_nacl_available() \ static_branch_unlikely(&kvm_riscv_nacl_available) @@ -43,6 +45,10 @@ void __kvm_riscv_nacl_hfence(void *shmem, unsigned long page_num, unsigned long page_count); =20 +void __kvm_riscv_nacl_switch_to(struct kvm_vcpu_arch *vcpu_arch, + unsigned long sbi_ext_id, + unsigned long sbi_func_id); + int kvm_riscv_nacl_enable(void); =20 void kvm_riscv_nacl_disable(void); diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 0aad58f984ff..e191e6eae0c0 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -770,19 +770,59 @@ static __always_inline void kvm_riscv_vcpu_swap_in_ho= st_state(struct kvm_vcpu *v */ static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu) { + void *nsh; struct kvm_cpu_context *gcntx =3D &vcpu->arch.guest_context; struct kvm_cpu_context *hcntx =3D &vcpu->arch.host_context; =20 kvm_riscv_vcpu_swap_in_guest_state(vcpu); guest_state_enter_irqoff(); =20 - hcntx->hstatus =3D ncsr_swap(CSR_HSTATUS, gcntx->hstatus); + if (kvm_riscv_nacl_sync_sret_available()) { + nsh =3D nacl_shmem(); =20 - nsync_csr(-1UL); + if (kvm_riscv_nacl_autoswap_csr_available()) { + hcntx->hstatus =3D + nacl_csr_read(nsh, CSR_HSTATUS); + nacl_scratch_write_long(nsh, + SBI_NACL_SHMEM_AUTOSWAP_OFFSET + + SBI_NACL_SHMEM_AUTOSWAP_HSTATUS, + gcntx->hstatus); + nacl_scratch_write_long(nsh, + SBI_NACL_SHMEM_AUTOSWAP_OFFSET, + SBI_NACL_SHMEM_AUTOSWAP_FLAG_HSTATUS); + } else if (kvm_riscv_nacl_sync_csr_available()) { + hcntx->hstatus =3D nacl_csr_swap(nsh, + CSR_HSTATUS, gcntx->hstatus); + } else { + hcntx->hstatus =3D csr_swap(CSR_HSTATUS, gcntx->hstatus); + } =20 - __kvm_riscv_switch_to(&vcpu->arch); + nacl_scratch_write_longs(nsh, + SBI_NACL_SHMEM_SRET_OFFSET + + SBI_NACL_SHMEM_SRET_X(1), + &gcntx->ra, + SBI_NACL_SHMEM_SRET_X_LAST); + + __kvm_riscv_nacl_switch_to(&vcpu->arch, SBI_EXT_NACL, + SBI_EXT_NACL_SYNC_SRET); + + if (kvm_riscv_nacl_autoswap_csr_available()) { + nacl_scratch_write_long(nsh, + SBI_NACL_SHMEM_AUTOSWAP_OFFSET, + 0); + gcntx->hstatus =3D nacl_scratch_read_long(nsh, + SBI_NACL_SHMEM_AUTOSWAP_OFFSET + + SBI_NACL_SHMEM_AUTOSWAP_HSTATUS); + } else { + gcntx->hstatus =3D csr_swap(CSR_HSTATUS, hcntx->hstatus); + } + } else { + hcntx->hstatus =3D csr_swap(CSR_HSTATUS, gcntx->hstatus); =20 - gcntx->hstatus =3D csr_swap(CSR_HSTATUS, hcntx->hstatus); + __kvm_riscv_switch_to(&vcpu->arch); + + gcntx->hstatus =3D csr_swap(CSR_HSTATUS, hcntx->hstatus); + } =20 vcpu->arch.last_exit_cpu =3D vcpu->cpu; guest_state_exit_irqoff(); diff --git a/arch/riscv/kvm/vcpu_switch.S b/arch/riscv/kvm/vcpu_switch.S index 9f13e5ce6a18..47686bcb21e0 100644 --- a/arch/riscv/kvm/vcpu_switch.S +++ b/arch/riscv/kvm/vcpu_switch.S @@ -218,6 +218,35 @@ SYM_FUNC_START(__kvm_riscv_switch_to) ret SYM_FUNC_END(__kvm_riscv_switch_to) =20 + /* + * Parameters: + * A0 <=3D Pointer to struct kvm_vcpu_arch + * A1 <=3D SBI extension ID + * A2 <=3D SBI function ID + */ +SYM_FUNC_START(__kvm_riscv_nacl_switch_to) + SAVE_HOST_GPRS + + SAVE_HOST_AND_RESTORE_GUEST_CSRS .Lkvm_nacl_switch_return + + /* Resume Guest using SBI nested acceleration */ + add a6, a2, zero + add a7, a1, zero + ecall + + /* Back to Host */ + .align 2 +.Lkvm_nacl_switch_return: + SAVE_GUEST_GPRS + + SAVE_GUEST_AND_RESTORE_HOST_CSRS + + RESTORE_HOST_GPRS + + /* Return to C code */ + ret +SYM_FUNC_END(__kvm_riscv_nacl_switch_to) + SYM_CODE_START(__kvm_riscv_unpriv_trap) /* * We assume that faulting unpriv load/store instruction is --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.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 9DAC8198845 for ; Sun, 20 Oct 2024 19:48:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453689; cv=none; b=Z7JNdtEVa3aevyCht5ZcxhWzWQ9Uq5CqtALU0ljfDrpe+q95/vJMycChqflhtdA/4Fl1YpbhyPB5IgthFolSIDwCwihCGk48+0eaEsRh0rebAIfn+FyLvhCzyX0x2P25L+UB0d24ru8bdvsx4WbU/FCTeWjnKQfGq/KGJ8MB9X0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453689; c=relaxed/simple; bh=9EjN8nh7V085+BqZAjVob3h2z871sk5Vbt3ysNlVGJk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S48vyYxR6DWQ4fh42te6mcGrL7zod4qmFitWLYW58hXRdrD0n8WXbAq0KDDx49l5nsr83y+EGDpiDdvrbZXeixct4LMjvyW0iJlf4HIS9WqtPI/q2HEYBLLWiSnFbCbhIWk5M7AI20lpxdfID9uhKDTIQ99vk1uylyBxsO/oiQo= 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=NlXB87Xt; arc=none smtp.client-ip=209.85.216.49 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="NlXB87Xt" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2e3d523a24dso2741725a91.0 for ; Sun, 20 Oct 2024 12:48:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453687; x=1730058487; 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=Yr2xZkoHB0nVswj42oFFwW3NpuvwZ9N4MbvDwoyulOQ=; b=NlXB87XtQ5tLb4SEZpxyqpk7lhf2Dp5tYj3DavcFwpmYSGx5j9EIrdLTP4BsT/YSTV LpNPFkdmOt110WUTUjwBN0LCxSr2iZ9rZsr0VGlHuXMBaS4Trp1z5BQc1YkCrrP54qax 3p72Q3KuVT58BQ05Z4jN6Z1wxaaFzuP3K3bmwjNeRjkeQNObYKtcucjbYPDusMH34yik iQcu6OZ0hm99ZC2PQl/eHHfyt6e1frZGw3gu9Q7yjGbnxOzMqAnGvehChurQSRqfWN2V eGDRgOs4TgccjkP+8Ymiwrne10iXY4s1Blz62IA5lv8bsma34aoGGk+iF/Kni9yODfbx 9jsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453687; x=1730058487; 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=Yr2xZkoHB0nVswj42oFFwW3NpuvwZ9N4MbvDwoyulOQ=; b=cvV21xhoL3LuOayhinhRumuS4I+bQmFPVYzegyy4sFHbysqSqQ1a0qDaH45xbWqWoW SH82NvODwdgsSoqU8eCt4RxMTT3HIv77hvRnN4eBYPuumBcwkZbaIzZHKLKiPuLFFWqM 0+EpMGjtlsVHzXTv5UBuiQIMOiYmUjersL02/yMWM9z1gWe+7R2nIPNrdzA61UgpgICY u8DgEjRaED+BCl+eX3lHowyGYtnl1uUxvPLnQF/xYgK0udIns/3uVOgzXrpSQaZq4szO P7ete9WAR4/s4Cx3b7fHuBXOPPFtDxkv69HUJSShaXxwPLR7+m9/xerfVxIqHiWHthzS 5sqQ== X-Forwarded-Encrypted: i=1; AJvYcCWGme1eep9CL3ZXOzIdlMLYuXVjSqg6xVpyJFt7mbKqAROu2ez8qL4QFgcYcZ9yrlOM9OjvmvIYY+2rz1I=@vger.kernel.org X-Gm-Message-State: AOJu0YxM8qujJsp2VE7KFSpEBRvvCyGN5VL2SQ4/lqnF903Tc8cLtmlj LmcIvoMfV11f4SRQb+b7NlCyemKN0e8H7sC4nhWBcNU0cMDpYLhDd3RKWhFtsYo= X-Google-Smtp-Source: AGHT+IGOwsMImEEUFZ7X9qPpWtzyIIzNSFMp0fMdMzDbnnBwV6JmeAnM7Csv5DyXHIZDDlryM2Wx1g== X-Received: by 2002:a17:90a:5147:b0:2e2:c6a6:84da with SMTP id 98e67ed59e1d1-2e5617269c6mr10776191a91.34.1729453686738; Sun, 20 Oct 2024 12:48:06 -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.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:48:06 -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 12/13] RISC-V: KVM: Save trap CSRs in kvm_riscv_vcpu_enter_exit() Date: Mon, 21 Oct 2024 01:17:33 +0530 Message-ID: <20241020194734.58686-13-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" Save trap CSRs in the kvm_riscv_vcpu_enter_exit() function instead of the kvm_arch_vcpu_ioctl_run() function so that HTVAL and HTINST CSRs are accessed in more optimized manner while running under some other hypervisor. Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e191e6eae0c0..e048dcc6e65e 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -768,12 +768,21 @@ static __always_inline void kvm_riscv_vcpu_swap_in_ho= st_state(struct kvm_vcpu *v * This must be noinstr as instrumentation may make use of RCU, and this i= s not * safe during the EQS. */ -static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu) +static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu, + struct kvm_cpu_trap *trap) { void *nsh; struct kvm_cpu_context *gcntx =3D &vcpu->arch.guest_context; struct kvm_cpu_context *hcntx =3D &vcpu->arch.host_context; =20 + /* + * We save trap CSRs (such as SEPC, SCAUSE, STVAL, HTVAL, and + * HTINST) here because we do local_irq_enable() after this + * function in kvm_arch_vcpu_ioctl_run() which can result in + * an interrupt immediately after local_irq_enable() and can + * potentially change trap CSRs. + */ + kvm_riscv_vcpu_swap_in_guest_state(vcpu); guest_state_enter_irqoff(); =20 @@ -816,14 +825,24 @@ static void noinstr kvm_riscv_vcpu_enter_exit(struct = kvm_vcpu *vcpu) } else { gcntx->hstatus =3D csr_swap(CSR_HSTATUS, hcntx->hstatus); } + + trap->htval =3D nacl_csr_read(nsh, CSR_HTVAL); + trap->htinst =3D nacl_csr_read(nsh, CSR_HTINST); } else { hcntx->hstatus =3D csr_swap(CSR_HSTATUS, gcntx->hstatus); =20 __kvm_riscv_switch_to(&vcpu->arch); =20 gcntx->hstatus =3D csr_swap(CSR_HSTATUS, hcntx->hstatus); + + trap->htval =3D csr_read(CSR_HTVAL); + trap->htinst =3D csr_read(CSR_HTINST); } =20 + trap->sepc =3D gcntx->sepc; + trap->scause =3D csr_read(CSR_SCAUSE); + trap->stval =3D csr_read(CSR_STVAL); + vcpu->arch.last_exit_cpu =3D vcpu->cpu; guest_state_exit_irqoff(); kvm_riscv_vcpu_swap_in_host_state(vcpu); @@ -940,22 +959,11 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) =20 guest_timing_enter_irqoff(); =20 - kvm_riscv_vcpu_enter_exit(vcpu); + kvm_riscv_vcpu_enter_exit(vcpu, &trap); =20 vcpu->mode =3D OUTSIDE_GUEST_MODE; vcpu->stat.exits++; =20 - /* - * Save SCAUSE, STVAL, HTVAL, and HTINST because we might - * get an interrupt between __kvm_riscv_switch_to() and - * local_irq_enable() which can potentially change CSRs. - */ - 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 ncsr_read(CSR_HTVAL); - trap.htinst =3D ncsr_read(CSR_HTINST); - /* Syncup interrupts state with HW */ kvm_riscv_vcpu_sync_interrupts(vcpu); =20 --=20 2.43.0 From nobody Tue Nov 26 06:25:30 2024 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 4BF5719CC11 for ; Sun, 20 Oct 2024 19:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453691; cv=none; b=bVPDoKjagris0vVh83JxlKIDIhxmw0rMJatxFG/2tOvIfln3gqaMld4jHZOIiQtZhZRyL0B5syEFoWnpiRgoYzFywnpQ67n+GWa/JomsD1DGRA8x/4CVbJSgHbkbBq9wqQxPqBzSAAG+qsF+mPSW6wUP+KgxoI6Z6SI9Q8fyrUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729453691; c=relaxed/simple; bh=kyOQZveDrOSNJx1NEZtx5mOVCaXh/vOSdCiBaW04TNQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nlEhaKB8rFJZYreMZdq9lOegow1hU829lTNznUCGCdR+tuyajvAvBnk9ET0fAO4UpsLoQ/aaxGe5ZqUZ4LRCtBJIbqBd58e3gagXDkz904vppJP0h12qJ+HiTtbjzQiPvsx7nIYOkooO/nohcgjItbW1tcTzC0HHirCqPWNVkv4= 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=ktSkex3t; arc=none smtp.client-ip=209.85.214.180 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="ktSkex3t" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20cb47387ceso31858405ad.1 for ; Sun, 20 Oct 2024 12:48:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1729453688; x=1730058488; 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=Jpt4DbIfoiuDWJOlua/tAIaTx6f+U6YQBxkTpe5jPP4=; b=ktSkex3thXMEhP3+erwMZhK76nrqVHrBt+YM1RJaBP0EBdqgVQZgpkvKYNL25utKfJ sRGOR9KGInaOpd/4ujukN3ufGmtCmuvJGP4V2m/bhesQYb/8hb2n+ECwRSgmRDUyFCcb 2IGRfefrUA23mGlcBVt0qFS8sP+kel624RuajOXnpJTqDQyaZX8zfqjzWZNtVzLNYtOR FQVN7OSTBsyF3SiW1W/8E3G8IXnU/uQaiI6MRKMx31YvNJNA0WBa94ZtTB/MNqT9MELI u2O0dVX9Ez1YKU4OAYV4Ohb8hm3lHgQQ0vZvDNb8tCwDgVAEy7lpj+w8N1PSnAH0YZlV O4GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729453688; x=1730058488; 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=Jpt4DbIfoiuDWJOlua/tAIaTx6f+U6YQBxkTpe5jPP4=; b=e82VH5pck92PjfInFd+0kJyi/lldWa3wSFCBtafsw87KoZvhJxXIb36RAjSB95oJwy lRXYeDJWFh/gTXjBOLljXE6+NOUiY1IdUp5HyELAGfLwMEHlx2dp5Ma7EH7fpuszPf7l b6Wm+lM/DXcYa5gk2ksN8t+qRW0VrulA3XIGfGJexZcg1F6AM/gP+LNzeIop1VZcIgMe x+0vLI9aKt57X0IzV87iPF6A/aWsSMsvS0jc7kOCiHstoDGmhQIfk10CbdWeV51CfVvO E+LVMCFJUsm99n8Rf5RECUhs7WqXlDTMHwwhxo17rjTiqBgaCEEIHe18hnsXZ4lNWGBC oEFQ== X-Forwarded-Encrypted: i=1; AJvYcCXHhOMgh7KKCxlCjzXdvEFWE9AuuyrIT/yEcXtP0gc2g9cahCQg/HSq29yeVURAFNsfN96O+6CCcCT/HYc=@vger.kernel.org X-Gm-Message-State: AOJu0Yx49uHoNV3dXfnvRy0Rq8XdTB1cEW+1GUVCGE3alAoiH//Ve/jD sPAimZzI9TlQIMNVB6l5bbevlFatDpLATt5tV5EZ3/3vHW7N8HKRGt5U8tN+mxs= X-Google-Smtp-Source: AGHT+IFBmef3AeMvYK7vRhNOkgHPNp9TfRjR+EjTOWv2i/hsD5eBK3uDXWFjXdE3v54cHEOsNVWYNw== X-Received: by 2002:a17:90a:17cc:b0:2e0:5748:6ea1 with SMTP id 98e67ed59e1d1-2e56172aad1mr11159778a91.37.1729453688471; Sun, 20 Oct 2024 12:48:08 -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.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 12:48:07 -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 Subject: [PATCH v2 13/13] RISC-V: KVM: Use NACL HFENCEs for KVM request based HFENCEs Date: Mon, 21 Oct 2024 01:17:34 +0530 Message-ID: <20241020194734.58686-14-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, use SBI NACL based HFENCEs for TLB shoot-down via KVM requests. This makes HFENCEs faster whenever SBI nested acceleration is available. Signed-off-by: Anup Patel --- arch/riscv/kvm/tlb.c | 57 +++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c index 23c0e82b5103..2f91ea5f8493 100644 --- a/arch/riscv/kvm/tlb.c +++ b/arch/riscv/kvm/tlb.c @@ -14,6 +14,7 @@ #include #include #include +#include =20 #define has_svinval() riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL) =20 @@ -186,18 +187,24 @@ void kvm_riscv_fence_i_process(struct kvm_vcpu *vcpu) =20 void kvm_riscv_hfence_gvma_vmid_all_process(struct kvm_vcpu *vcpu) { - struct kvm_vmid *vmid; + struct kvm_vmid *v =3D &vcpu->kvm->arch.vmid; + unsigned long vmid =3D READ_ONCE(v->vmid); =20 - vmid =3D &vcpu->kvm->arch.vmid; - kvm_riscv_local_hfence_gvma_vmid_all(READ_ONCE(vmid->vmid)); + if (kvm_riscv_nacl_available()) + nacl_hfence_gvma_vmid_all(nacl_shmem(), vmid); + else + kvm_riscv_local_hfence_gvma_vmid_all(vmid); } =20 void kvm_riscv_hfence_vvma_all_process(struct kvm_vcpu *vcpu) { - struct kvm_vmid *vmid; + struct kvm_vmid *v =3D &vcpu->kvm->arch.vmid; + unsigned long vmid =3D READ_ONCE(v->vmid); =20 - vmid =3D &vcpu->kvm->arch.vmid; - kvm_riscv_local_hfence_vvma_all(READ_ONCE(vmid->vmid)); + if (kvm_riscv_nacl_available()) + nacl_hfence_vvma_all(nacl_shmem(), vmid); + else + kvm_riscv_local_hfence_vvma_all(vmid); } =20 static bool vcpu_hfence_dequeue(struct kvm_vcpu *vcpu, @@ -251,6 +258,7 @@ static bool vcpu_hfence_enqueue(struct kvm_vcpu *vcpu, =20 void kvm_riscv_hfence_process(struct kvm_vcpu *vcpu) { + unsigned long vmid; struct kvm_riscv_hfence d =3D { 0 }; struct kvm_vmid *v =3D &vcpu->kvm->arch.vmid; =20 @@ -259,26 +267,41 @@ void kvm_riscv_hfence_process(struct kvm_vcpu *vcpu) case KVM_RISCV_HFENCE_UNKNOWN: break; case KVM_RISCV_HFENCE_GVMA_VMID_GPA: - kvm_riscv_local_hfence_gvma_vmid_gpa( - READ_ONCE(v->vmid), - d.addr, d.size, d.order); + vmid =3D READ_ONCE(v->vmid); + if (kvm_riscv_nacl_available()) + nacl_hfence_gvma_vmid(nacl_shmem(), vmid, + d.addr, d.size, d.order); + else + kvm_riscv_local_hfence_gvma_vmid_gpa(vmid, d.addr, + d.size, d.order); break; case KVM_RISCV_HFENCE_VVMA_ASID_GVA: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD); - kvm_riscv_local_hfence_vvma_asid_gva( - READ_ONCE(v->vmid), d.asid, - d.addr, d.size, d.order); + vmid =3D READ_ONCE(v->vmid); + if (kvm_riscv_nacl_available()) + nacl_hfence_vvma_asid(nacl_shmem(), vmid, d.asid, + d.addr, d.size, d.order); + else + kvm_riscv_local_hfence_vvma_asid_gva(vmid, d.asid, d.addr, + d.size, d.order); break; case KVM_RISCV_HFENCE_VVMA_ASID_ALL: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD); - kvm_riscv_local_hfence_vvma_asid_all( - READ_ONCE(v->vmid), d.asid); + vmid =3D READ_ONCE(v->vmid); + if (kvm_riscv_nacl_available()) + nacl_hfence_vvma_asid_all(nacl_shmem(), vmid, d.asid); + else + kvm_riscv_local_hfence_vvma_asid_all(vmid, d.asid); break; case KVM_RISCV_HFENCE_VVMA_GVA: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_HFENCE_VVMA_RCVD); - kvm_riscv_local_hfence_vvma_gva( - READ_ONCE(v->vmid), - d.addr, d.size, d.order); + vmid =3D READ_ONCE(v->vmid); + if (kvm_riscv_nacl_available()) + nacl_hfence_vvma(nacl_shmem(), vmid, + d.addr, d.size, d.order); + else + kvm_riscv_local_hfence_vvma_gva(vmid, d.addr, + d.size, d.order); break; default: break; --=20 2.43.0