From patchwork Mon Jan 22 20:18:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Zyngier X-Patchwork-Id: 13526206 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 CCD80C47DD3 for ; Mon, 22 Jan 2024 20:19:42 +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=22yeJyj8hML/txli34rQ6Z4Nc9Rv1BhaCumRP26DKbs=; b=qMnh5D4s+DKwy8 jnAV3zZE+Zh1N7my2NsJAMNdVUcz7c4MJb8cQ0EY+XRDSeFV+TwCY1qQxMsCyPZkp1pHQgBk4Iwf9 6dVFAj3sHvnqXdwa2G222fyl7W+ZP9a7fQ9S2WIv4YNMs5rhsAKKchjouleB/PdkO9KLSdkjgVhl/ Uu2IjciZAEQefHxXeIjcUiYYa64Sy8B97NrwF1F2AvqGjakETw6+11LkYhC+4dBkgri3+iVTgLg/X k+B45wy4CrqoI3IIexzSWcp010ugHUary8eg1fuyIg8yjZxHGbMBxJJkxfIt1t0JGcxmwOgKuVO+8 vBzlDr/0H585FhGExwcg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rS0l9-00Drxf-0X; Mon, 22 Jan 2024 20:19:07 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rS0l1-00DrqC-01 for linux-arm-kernel@lists.infradead.org; Mon, 22 Jan 2024 20:19:00 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 81FF3616F5; Mon, 22 Jan 2024 20:18:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDAE2C43399; Mon, 22 Jan 2024 20:18:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705954737; bh=D8XAWTe4sEY9QGE49AHls5BaX4fye0job2m9Dj0QDSA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gPaB3uuxJWmRAsz0u6hyiLQtT/C2HocfO5x5Uk01DwJYya4xX7bbvzmxR/isSqh0R SllJtgmwMYLjB6x3T6ulc2a7S8woG3g2H1gCxCZXpmRTXb74skyrFpdrSQKYuUVote 1V1Qb1WJqbMTteYY7gm6UXapXSm01ajAwmQ4/P/bJlewn9NpOUz6uaCDku0Sw1xVQb vA9NfD5ly1pnU5poKk3/LHeTWBkzoidzAboHMVbcc1OY6CaK0uxGo9tAEGQHp2QcoO M7u4hi4ir5Hy+fpaGaQ1wb8rItXuxk8tJiIejQN4VwjScal8Sq8B3fzMXTYzVB8ZqX 0LhzoAKPe+cCA== Received: from sofa.misterjones.org ([185.219.108.64] helo=valley-girl.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rS0kx-00DjkQ-VA; Mon, 22 Jan 2024 20:18:56 +0000 From: Marc Zyngier To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: James Morse , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Catalin Marinas , Will Deacon , Joey Gouly , Mark Brown Subject: [PATCH 02/25] KVM: arm64: Add feature checking helpers Date: Mon, 22 Jan 2024 20:18:29 +0000 Message-Id: <20240122201852.262057-3-maz@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240122201852.262057-1-maz@kernel.org> References: <20240122201852.262057-1-maz@kernel.org> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, joey.gouly@arm.com, broonie@kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240122_121859_131610_E07966AB X-CRM114-Status: GOOD ( 11.21 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In order to make it easier to check whether a particular feature is exposed to a guest, add a new set of helpers, with kvm_has_feat() being the most useful. Follow-up work will make heavy use of these. Signed-off-by: Marc Zyngier --- arch/arm64/include/asm/kvm_host.h | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 21c57b812569..c0cf9c5f5e8d 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1233,4 +1233,57 @@ static inline void kvm_hyp_reserve(void) { } void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu); +#define __expand_field_sign_unsigned(id, fld, val) \ + ((u64)(id##_##fld##_##val)) + +#define __expand_field_sign_signed(id, fld, val) \ + ({ \ + s64 __val = id##_##fld##_##val; \ + __val <<= 64 - id##_##fld##_WIDTH; \ + __val >>= 64 - id##_##fld##_SHIFT - id##_##fld##_WIDTH; \ + \ + __val; \ + }) + +#define expand_field_sign(id, fld, val) \ + (id##_##fld##_SIGNED ? \ + __expand_field_sign_signed(id, fld, val) : \ + __expand_field_sign_unsigned(id, fld, val)) + +#define get_idreg_field_unsigned(kvm, id, fld) \ + ({ \ + u64 __val = IDREG(kvm, SYS_##id); \ + __val &= id##_##fld##_MASK; \ + __val >>= id##_##fld##_SHIFT; \ + \ + __val; \ + }) + +#define get_idreg_field_signed(kvm, id, fld) \ + ({ \ + s64 __val = IDREG(kvm, SYS_##id); \ + __val <<= 64 - id##_##fld##_SHIFT - id##_##fld##_WIDTH; \ + __val >>= id##_##fld##_SHIFT; \ + \ + __val; \ + }) + +#define get_idreg_field_enum(kvm, id, fld) \ + get_idreg_field_unsigned(kvm, id, fld) + +#define get_idreg_field(kvm, id, fld) \ + (id##_##fld##_SIGNED ? \ + get_idreg_field_signed(kvm, id, fld) : \ + get_idreg_field_unsigned(kvm, id, fld)) + +#define kvm_has_feat(kvm, id, fld, limit) \ + (get_idreg_field((kvm), id, fld) >= expand_field_sign(id, fld, limit)) + +#define kvm_has_feat_enum(kvm, id, fld, limit) \ + (get_idreg_field_unsigned((kvm), id, fld) == id##_##fld##_##limit) + +#define kvm_has_feat_range(kvm, id, fld, min, max) \ + (get_idreg_field((kvm), id, fld) >= expand_field_sign(id, fld, min) && \ + get_idreg_field((kvm), id, fld) <= expand_field_sign(id, fld, max)) + #endif /* __ARM64_KVM_HOST_H__ */