From patchwork Mon Jan 6 15:48:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13927607 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9DB18E7719D for ; Mon, 6 Jan 2025 15:51:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=t5UEwnzS9ll6KPlPBGFDZkGlyOoxF4zZFdfqjK6yJYA=; b=iEJUUilm921KdN Mv5lZRoxXxZfiGv367fACiUCIO+NxQFAVTSCGEA3usy17r8r2vidiXL+J/xaDwFnonrYhAvjuXX/E Ets85huogUWJ2iQ/LeJqHDgBU/3z8bSYjf/CS+uteONORrwSd8F5RF1LeZISE0+t8Kinmem+XZWbO +yPr0sOmzuBq62Uc0n8A5FQYN7yascR4jR856D1xUvxsGAiao7bd8m7RnZ5xQTvd1PDRQqC1bql9c QRZZIMwIGWafL5Q3WFqR2La5GwMIY3FtLjLIg4fwQhc+hkgVSRIsul9yNJEv3VY1861d1+GePRIAX tq79zCr2xxi1zm+1aqFA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tUpNe-00000001qXc-26eL; Mon, 06 Jan 2025 15:51:02 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tUpM7-00000001q68-2Z3v for linux-riscv@lists.infradead.org; Mon, 06 Jan 2025 15:49:28 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-2163bd70069so20013545ad.0 for ; Mon, 06 Jan 2025 07:49:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736178566; x=1736783366; darn=lists.infradead.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=mxBjx4Pwy3s9wNFrCu0QzGcTxzWHmeeGktVNtPbGFVU=; b=yrBXisn9kLaZDUsi+ru2LW7uTx/o035pO1LasK5ueTllt3JBljp42ALy9Noe5lixaA flzp7Mq79rfQazp3jrSeBpqWJBv0Iqpy87LAdSyj1UyYF+olPP9UqdYb133AyCiS/xZG bmtMVlKD7dwW7wEsoZBoT4I5z8QBO4K/kflHMSl51a0QURAB/NMWcirPENvXwBgFuAoF PJj7/vcE0m7ZC5EyDSA8c0liCViPuPv5BG9TLjsKuL2QCMdoe0HZxAMLY4ntukkcSBrs HVjC0SWSLp2LfUTlkMEuU/MPDdZAwniM9WU9iGphzifPmBiRaxMkwNFCNdgrT12eFmjs pRCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736178566; x=1736783366; 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=mxBjx4Pwy3s9wNFrCu0QzGcTxzWHmeeGktVNtPbGFVU=; b=vazl4TadJ0UAbYG5pRlSsgTTc/JSA4qBfg3zsD3pKP96GFg1aqT5SSokU0Pn3g/JOR fMAkIU17zYYf/fcAC+mTgpO8lACEqF4v7coK7ZbrL7DHKROOK6+LR5haBbEk09DjrIcV vy0dA6NHJR9Xv2tPNKuNfUxZfBwFa5L0adI7C+DFtG4++Rof7gBxEvOQLvtlMVUdmkw2 AC2oaXAYKzljekHdSEGU9pfyBXoldUUOOLmylMpeQyU0nFYu3FQWVT9a282RO/wCZ5UJ peAe9J81fwLABPDV8Qk1B9Ex7seJ9qjX/pwzEbxaZ3V0r7luQSJpIMogagMhqLyQV/Wr 9DKQ== X-Forwarded-Encrypted: i=1; AJvYcCWYFug0b6ozpQKIA3oQIqBTyZnhkmTS29Eikoickrwyho58E3673nhTIWdTNKR6CbJt5ZKT1buuz89yTQ==@lists.infradead.org X-Gm-Message-State: AOJu0YydTHuLJs/neML0uuK2ItKChMrtDDGR4mX9Jp4XHVgKfSFspmd0 0TIar+pbvd8wpsNZRu3QUHo8oKFoG0zTZu6tPDdmJChj5pHyHytayRFlGNs6vYU= X-Gm-Gg: ASbGncsGyt+G1OYfZPDOgPwB+b0rEJ0FelP/mC7fd3nobbrWWCCW/H9snu7j3HgUGyT u1hhAY9Om3romPZoHuUzvt+/mPa/+GrlwxuXGCopj6DrC5x+yNl+g3dsxZ0Dda4/oK4EAwV/6n+ DukciO/knWbKOuGkcQ0CffP/DFkFLx1vClvTepmyFewthhO4MzhQ5dPvhJi4nkdDT9XVh6VGzGr xF54xyY7g0PRRCuT20THr8onpdlYZkDMD/e+y/RdMR39nW2Xnsbwj91KQ== X-Google-Smtp-Source: AGHT+IFBFcio9MaUGZCTb/Jg2857ywT0hQvVtOg6GlbEZCyc6I2hLialdck8Jevr/BDDD1XTJo/mLA== X-Received: by 2002:a17:902:c406:b0:216:2bd7:1c4a with SMTP id d9443c01a7336-219e6ebb716mr872299045ad.26.1736178564924; Mon, 06 Jan 2025 07:49:24 -0800 (PST) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9f6967sm292479535ad.214.2025.01.06.07.49.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 07:49:24 -0800 (PST) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PATCH 3/6] RISC-V: KVM: add SBI extension init()/deinit() functions Date: Mon, 6 Jan 2025 16:48:40 +0100 Message-ID: <20250106154847.1100344-4-cleger@rivosinc.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250106154847.1100344-1-cleger@rivosinc.com> References: <20250106154847.1100344-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250106_074927_643178_02EB5AD9 X-CRM114-Status: GOOD ( 16.15 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The FWFT SBI extension will need to dynamically allocate memory and do init time specific initialization. Add an init/deinit callbacks that allows to do so. Signed-off-by: Clément Léger --- arch/riscv/include/asm/kvm_vcpu_sbi.h | 9 ++++++++ arch/riscv/kvm/vcpu.c | 2 ++ arch/riscv/kvm/vcpu_sbi.c | 31 ++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index b96705258cf9..8c465ce90e73 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -49,6 +49,14 @@ struct kvm_vcpu_sbi_extension { /* Extension specific probe function */ unsigned long (*probe)(struct kvm_vcpu *vcpu); + + /* + * Init/deinit function called once during VCPU init/destroy. These + * might be use if the SBI extensions need to allocate or do specific + * init time only configuration. + */ + int (*init)(struct kvm_vcpu *vcpu); + void (*deinit)(struct kvm_vcpu *vcpu); }; void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run); @@ -69,6 +77,7 @@ const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext( bool riscv_vcpu_supports_sbi_ext(struct kvm_vcpu *vcpu, int idx); int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run); void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu); int kvm_riscv_vcpu_get_reg_sbi_sta(struct kvm_vcpu *vcpu, unsigned long reg_num, unsigned long *reg_val); diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e048dcc6e65e..3420a4a62c94 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -180,6 +180,8 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) { + kvm_riscv_vcpu_sbi_deinit(vcpu); + /* Cleanup VCPU AIA context */ kvm_riscv_vcpu_aia_deinit(vcpu); diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index 6e704ed86a83..d2dbb0762072 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -486,7 +486,7 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; const struct kvm_riscv_sbi_extension_entry *entry; const struct kvm_vcpu_sbi_extension *ext; - int idx, i; + int idx, i, ret; for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) { entry = &sbi_ext[i]; @@ -501,8 +501,37 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) continue; } + if (ext->init) { + ret = ext->init(vcpu); + if (ret) + scontext->ext_status[idx] = + KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE; + } + scontext->ext_status[idx] = ext->default_disabled ? KVM_RISCV_SBI_EXT_STATUS_DISABLED : KVM_RISCV_SBI_EXT_STATUS_ENABLED; } } + +void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; + const struct kvm_riscv_sbi_extension_entry *entry; + const struct kvm_vcpu_sbi_extension *ext; + int idx, i; + + for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) { + entry = &sbi_ext[i]; + ext = entry->ext_ptr; + idx = entry->ext_idx; + + if (idx < 0 || idx >= ARRAY_SIZE(scontext->ext_status)) + continue; + + if (scontext->ext_status[idx] != KVM_RISCV_SBI_EXT_STATUS_ENABLED || !ext->deinit) + continue; + + ext->deinit(vcpu); + } +}