From patchwork Mon Feb 10 21:35:45 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: 13969385 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 1BC77C0219D for ; Mon, 10 Feb 2025 23:55:57 +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=0LXtYeLRWg9crOLGuuvPE70Yey2z75YjfI46rFJtsgk=; b=XFVv9IIf/+2IvU xAV3Hl7LGwZKaukoHKs0qGaWi1vJfBUOYKUcnTmBHeyploJjjGkl2TcSRBd0alAdkwVY3Gef5P/Sq SghL3a9wJ9ymYnhlI5rtc+ALKIzEx92CLG2n4o9fQLBTZa+czCb891BNReDun/C74Szllkv6aHL2x UyVKG+2WziLyySO4gA/19S/jh7Qxj6biNQqswFzy5ja0rs5XqerUcjB8zRC7hvPXT654eUPQD7kzg nsBh6t1LbK6WWISctK+Gaf1lRVA4ISvD6tqeM4LfbRNlpYnsv2ZmapzIlNcx0cjDtfYBQtp0vdL5+ NgSGYHpgzHBhKJStetfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thdd0-00000001u3j-39JB; Mon, 10 Feb 2025 23:55:50 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thbS1-00000001VSz-18u7 for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 21:36:22 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-43948f77f1aso7273785e9.0 for ; Mon, 10 Feb 2025 13:36:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739223379; x=1739828179; 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=fzMhLGWSVmu+B6UkQbjgF0/C3X18++BVbDb7RHWPhdM=; b=f6iMZ1HSHak2c6UuxsnzILzW/rkwUGK6MhZ2b33nNmeevkg29ZIj2iJ48ASYJQkoZt oRi26nimqFBZfIft8PMzRuXSGjE5dzUsdYtc09/aM7A9OpRcgiKEux19cQW4KHPiyobN iCHIU46cr198O3igjJA1Riu6NppBz3jPQqbMQZFyTAHqVYneFS9h7cQMxs0ySyz7qExi SR0EC/upvjgLYkkQavEvtXqo2SoqSCZWhKbLmUq8W3lZzNcFkF3vBeNsvk29xNNl5326 rEUZyP9HgSC1+sZqng2deqHHQJI/56arFwAK1or7K4Q7SuSvyUBaDV+UzzXwH4GwJ6AA c8tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739223379; x=1739828179; 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=fzMhLGWSVmu+B6UkQbjgF0/C3X18++BVbDb7RHWPhdM=; b=kfmh0cy1jujCdFpFqIs6U6iu8xvfk66Ptq18U2qwSFVcewi1VkVSzl7lEauRulhrHs XGgjoNKgrDELMFnFO/158sImkm1uwxVrxKCPVCyMA2htu9n5ryMfDoXKWgtSHUU7iE8w SpdchD4Me+Z/CiOXFce7Qm+uNAEjdMld1atGK6M4ldgys/rMfAZrZpDu6XjgSBIW8rv4 aH5/NgL7T8eAtLb1eqpoUVK33GbEQFl4lky7HMXPcL79crDUsSM0a3zkcC74tC7ySBIi aafZQ9VL79i8q6Jpl9TzntLMtq9owUz4r6XyaauhGQ9/oueisYPmTosDPERecqaa7ubC cWGg== X-Forwarded-Encrypted: i=1; AJvYcCVly/0oiISpDcYVj2H+BPxgLpKIR2Bn4FU+IDl5P/ZARkiQSBwhcgGmfZD3KNJ1ctQ5N67xIqfqdfykJw==@lists.infradead.org X-Gm-Message-State: AOJu0YzRgUPPtxJpGfrbeR+7tBySXwc1LOG1UDsWneD3wsjcFTqm1sCd ey97osoJHsjBy6WynbcSCFIr79vJk1A3asT8BQZ75UbUgPdP6qDkEQLBHCs746Y= X-Gm-Gg: ASbGncuUWA+Nk3gKgodwCF4x4G0FMTQcM+f2jot5qBl+HYw77r4itiMJIrLG7YTzUwb NduXXBP7xcmndyJNVWlosvUoldjOMzsyKxsVSjZCONxDxc0dA/RDH4PRU4QrZDbcSCvtGQ/1qUs zfTH4sTBDvic01/ZApDN3u1RgrMoH6Iv8UITtxUpLKqTynT/IEwrvS494I1lMxOU9dOX/A8xFPY UYQuKY11vGkT1dVtmimYkGi0/QDd8QfMeT1qNt+9zJ0KITDneLsg/36BMltRwA6NWTBm9LJmteo afBj7u4P/W8rksDc X-Google-Smtp-Source: AGHT+IEdxZi1LVbl9euup4c2vHOS8bchWH6uTAT2rIkrBK2DWGVQXoH4pwkPsigJnMZZocj6TvzXjg== X-Received: by 2002:a5d:6d0e:0:b0:38d:b34a:679 with SMTP id ffacd0b85a97d-38dc9373577mr10353065f8f.37.1739223379602; Mon, 10 Feb 2025 13:36:19 -0800 (PST) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4394376118esm47541515e9.40.2025.02.10.13.36.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 13:36:18 -0800 (PST) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , Shuah Khan , Jonathan Corbet , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Samuel Holland Subject: [PATCH v2 12/15] RISC-V: KVM: add SBI extension init()/deinit() functions Date: Mon, 10 Feb 2025 22:35:45 +0100 Message-ID: <20250210213549.1867704-13-cleger@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250210213549.1867704-1-cleger@rivosinc.com> References: <20250210213549.1867704-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_133621_309980_9EDED867 X-CRM114-Status: GOOD ( 14.63 ) 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 | 30 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index 4ed6203cdd30..bcb90757b149 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 60d684c76c58..877bcc85c067 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -185,6 +185,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 d1c83a77735e..44f551143498 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -505,8 +505,38 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) continue; } + if (!ext->default_disabled && ext->init && + ext->init(vcpu) != 0) { + scontext->ext_status[idx] = + KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE; + continue; + } + 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_UNAVAILABLE || + !ext->deinit) + continue; + + ext->deinit(vcpu); + } +}