From patchwork Mon Jan 15 05:59:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13519343 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 46FADC3DA79 for ; Mon, 15 Jan 2024 07:00:38 +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=NaoTZzb7gWX7J18C1v8XK+WKQ6olPs1636bIs3+NjHI=; b=bCbZZI3CyxE2zZ yPzcv9XX22ukwleCo6mq04grUcXFbbEgZrwphnHVfh1295JoBLEvaAYBNTM4uTG2mh11Lmh//LrV4 ncSkzwxLV1q1O0lWTuGxgkfIwbflRAMWw/rZ9Vzxu2BER44ilPTc3kL73mnCwTOeCJ2DvEV40aauq DnUvNvewsabsnhvP77ZHHYksn4ZUYV4lj3Rh2ihGdwMtA0JkW6YTrCcfFI4w2w/hvpsQC+hY+47jS ssN+FmiOnNmfEyuh/8w1WuV/S2TRkjsE5vL2duh7fx0BoR/VHV4u2HYSqJdtXMEiohbGqP0kJ0vhU SbaB8rcpjtjGg4Lm4nFQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rPGxH-0081NH-05; Mon, 15 Jan 2024 07:00:19 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rPG0q-007qDN-26 for linux-riscv@bombadil.infradead.org; Mon, 15 Jan 2024 05:59:56 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=jq0h2LaQ8fcssQa+y+ckaiZBaoX909gyQ3cg8U3GLk8=; b=WvZT94RDUnf0ylgslthLXnQwG+ mLDz8BXKlEL6VTEUc5AGSpydRUhiAVnnm6EnqnXptJZCIOTH2EMb+Y99nBVtP4CJ8/SV4zL94STXF isZeXLr9AUcswhWt3/fhWODu64qberOsqc3tCPd/XI4K9VaXOQraOpKi0AtlmUdFXHz+GOMqy1bz0 a+Ki/sZH7qTq2pjaDOLMYUthNtoSFaG0quGItSUvDxLti8Yx1Ew5oBGYfxYf4rPo+OitTxwpN1CDB FX7d4uOQE1qKYLP5S1PxLX2X0/xQDPnJzJaQVn516d8H+0XD5B1Iq4+CoEID0yTI20jJn+euufwLm mfrjzTlQ==; Received: from mail-yb1-xb33.google.com ([2607:f8b0:4864:20::b33]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rPG0m-00D1Yz-2R for linux-riscv@lists.infradead.org; Mon, 15 Jan 2024 05:59:55 +0000 Received: by mail-yb1-xb33.google.com with SMTP id 3f1490d57ef6-dbeda700015so7074579276.2 for ; Sun, 14 Jan 2024 21:59:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1705298390; x=1705903190; 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=jq0h2LaQ8fcssQa+y+ckaiZBaoX909gyQ3cg8U3GLk8=; b=LFSzm16PYHa15sqBz+T81WU4F95mu3HyoZvvJqu9bCLfk9Sz3YXDp2Vh1ioLHCzJKN gFUYuE2ebe4nqTtidu8BaA2bJrRzT1QyJlYyfoNB10//W+EvtMLmkQF+bNaFETDKGo/0 HdE+Wc9M2eVmdl3TSe39QiLYew7MSxG7D8zjz2h0jf3WKzjMjyVD1CPH0lnZS+yYkaro j1vkWDBs0r4b1prE5OOiViVTcQiXjVsP/QoCc+YXqxQpjwPNTBAxB3MNbKHaCBwErLix DycA1bKZKlLpTiaVz9Ek4UzxNxWQjvpHTZ/Q1LYBbSKaM10FwutPT2EINZvS4tqbSsRX 6nHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705298390; x=1705903190; 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=jq0h2LaQ8fcssQa+y+ckaiZBaoX909gyQ3cg8U3GLk8=; b=SM++EOyE5TfIMujbBPeMVyA4MO2nwzCYeKUn6JT+idLddQUGmOdLCzHWkSHJG0rsiC hTegvJgOCRMQRTl9FiTBCryDLsnKIw2qs+cFS40v3/C3Vt+jZuGSEQbC487j/+29vTun U6rmM/0yYzF8muHDsCh3b2Mu+6HOkPfxFzUL9AAuJFAZDtIIe94MqOPQXlLyKmxD8Al3 8CdvfvAsRRCcC87zLbI/VlRrDuqAC+RGz5tkuJXyBH/UeZSFPdasRgC5AlCFi4bj9J7n t3fcYcm64cptScnXmeYo0qUa1c4oK0Ea1fzL5h2rYUQFYSDP+N0OBSlvhBrcVHCNmerk Q6TQ== X-Gm-Message-State: AOJu0Ywi6LZNlhlaT8I071QSStFTX8zoVptXU+tJM6/8mIY8+1TpiioP ZOEldUfLv+6r/L2ZqxeQ0PgaI7qZ0BvIZoml1nltV/4IJGwKV74zsOBzdcL+1akEm3x6W0VCeim EliygnVAQWFVDbbAe3zct6xHHAuF1YhCoKW9qD6/DIC5eX51hDye5OJ7aZnwDSAXV4l4djhCZwM woIQUbDy8zA1L4V0/MWTM1 X-Google-Smtp-Source: AGHT+IGYfEwW7xuhy7FqrmA7tn/iqapaLR2CJJlX1WHD4olzgcjOW9mSfOH8LvDojWgeFW/eUCQC/Q== X-Received: by 2002:a25:6b41:0:b0:dbe:9f26:2159 with SMTP id o1-20020a256b41000000b00dbe9f262159mr1924373ybm.48.1705298389863; Sun, 14 Jan 2024 21:59:49 -0800 (PST) Received: from hsinchu26.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id o18-20020a637e52000000b005b9083b81f0sm7392988pgn.36.2024.01.14.21.59.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jan 2024 21:59:49 -0800 (PST) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com Cc: paul.walmsley@sifive.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, bjorn@kernel.org, charlie@rivosinc.com, ardb@kernel.org, arnd@arndb.de, peterz@infradead.org, tglx@linutronix.de, ebiggers@kernel.org, Vincent Chen , Andy Chiu , Albert Ou , Guo Ren , Baoquan He , =?utf-8?b?Q2w=?= =?utf-8?b?w6ltZW50IEzDqWdlcg==?= , Eric Biggers , Heiko Stuebner , Xiao Wang , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Conor Dooley , Alexandre Ghiti , Jisheng Zhang , Evan Green , Anup Patel , Sami Tolvanen Subject: [v11, 01/10] riscv: Add support for kernel mode vector Date: Mon, 15 Jan 2024 05:59:20 +0000 Message-Id: <20240115055929.4736-2-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240115055929.4736-1-andy.chiu@sifive.com> References: <20240115055929.4736-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240115_055953_141611_654841D1 X-CRM114-Status: GOOD ( 24.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 From: Greentime Hu Add kernel_vector_begin() and kernel_vector_end() function declarations and corresponding definitions in kernel_mode_vector.c These are needed to wrap uses of vector in kernel mode. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Eric Biggers --- Changelog v10: - update comment (Eric) Changelog v9: - use bitwise to mask on/off the use of Vector (Eric, Charlie) - BUG_ON when reentrant enablement of Vector happens (Charlie) - Move compiler barrier to the premept_v patch (Eric) Changelog v8: - Refactor unnecessary whitespace change (Eric) Changelog v7: - fix build fail for allmodconfig Changelog v6: - Use 8 bits to track non-preemptible vector context to provide better WARN coverage. Changelog v4: - Use kernel_v_flags and helpers to track vector context. Changelog v3: - Reorder patch 1 to patch 3 to make use of {get,put}_cpu_vector_context later. - Export {get,put}_cpu_vector_context. - Save V context after disabling preemption. (Guo) - Fix a build fail. (Conor) - Remove irqs_disabled() check as it is not needed, fix styling. (Björn) Changelog v2: - 's/kernel_rvv/kernel_vector' and return void in kernel_vector_begin (Conor) - export may_use_simd to include/asm/simd.h --- arch/riscv/include/asm/processor.h | 12 ++- arch/riscv/include/asm/simd.h | 44 ++++++++++ arch/riscv/include/asm/vector.h | 9 ++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_vector.c | 116 +++++++++++++++++++++++++ arch/riscv/kernel/process.c | 1 + 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/simd.h create mode 100644 arch/riscv/kernel/kernel_mode_vector.c diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index e1944ff0757a..3b59fc5c9545 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -73,6 +73,15 @@ struct task_struct; struct pt_regs; +/* + * We use a flag to track in-kernel Vector context. Currently the flag has the + * following meaning: + * + * - bit 0: indicates whether the in-kernel Vector context is active. The + * activation of this state disables the preemption. + */ +#define RISCV_KERNEL_MODE_V 0x1 + /* CPU-specific state of a task */ struct thread_struct { /* Callee-saved registers */ @@ -81,7 +90,8 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; - unsigned long vstate_ctrl; + u32 riscv_v_flags; + u32 vstate_ctrl; struct __riscv_v_ext_state vstate; unsigned long align_ctl; }; diff --git a/arch/riscv/include/asm/simd.h b/arch/riscv/include/asm/simd.h new file mode 100644 index 000000000000..ef8af413a9fc --- /dev/null +++ b/arch/riscv/include/asm/simd.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2017 Linaro Ltd. + * Copyright (C) 2023 SiFive + */ + +#ifndef __ASM_SIMD_H +#define __ASM_SIMD_H + +#include +#include +#include +#include +#include + +#include + +#ifdef CONFIG_RISCV_ISA_V +/* + * may_use_simd - whether it is allowable at this time to issue vector + * instructions or access the vector register file + * + * Callers must not assume that the result remains true beyond the next + * preempt_enable() or return from softirq context. + */ +static __must_check inline bool may_use_simd(void) +{ + /* + * RISCV_KERNEL_MODE_V is only set while preemption is disabled, + * and is clear whenever preemption is enabled. + */ + return !in_hardirq() && !in_nmi() && !(riscv_v_flags() & RISCV_KERNEL_MODE_V); +} + +#else /* ! CONFIG_RISCV_ISA_V */ + +static __must_check inline bool may_use_simd(void) +{ + return false; +} + +#endif /* ! CONFIG_RISCV_ISA_V */ + +#endif diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 87aaef656257..71af3404fda1 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -22,6 +22,15 @@ extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); bool riscv_v_first_use_handler(struct pt_regs *regs); +void kernel_vector_begin(void); +void kernel_vector_end(void); +void get_cpu_vector_context(void); +void put_cpu_vector_context(void); + +static inline u32 riscv_v_flags(void) +{ + return current->thread.riscv_v_flags; +} static __always_inline bool has_vector(void) { diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index a1f5dc145574..5a66432eb520 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -64,6 +64,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o +obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o diff --git a/arch/riscv/kernel/kernel_mode_vector.c b/arch/riscv/kernel/kernel_mode_vector.c new file mode 100644 index 000000000000..114cf4f0a0eb --- /dev/null +++ b/arch/riscv/kernel/kernel_mode_vector.c @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2012 ARM Ltd. + * Author: Catalin Marinas + * Copyright (C) 2017 Linaro Ltd. + * Copyright (C) 2021 SiFive + */ +#include +#include +#include +#include +#include + +#include +#include +#include + +static inline void riscv_v_flags_set(u32 flags) +{ + current->thread.riscv_v_flags = flags; +} + +static inline void riscv_v_start(u32 flags) +{ + int orig; + + orig = riscv_v_flags(); + BUG_ON((orig & flags) != 0); + riscv_v_flags_set(orig | flags); +} + +static inline void riscv_v_stop(u32 flags) +{ + int orig; + + orig = riscv_v_flags(); + BUG_ON((orig & flags) == 0); + riscv_v_flags_set(orig & ~flags); +} + +/* + * Claim ownership of the CPU vector context for use by the calling context. + * + * The caller may freely manipulate the vector context metadata until + * put_cpu_vector_context() is called. + */ +void get_cpu_vector_context(void) +{ + preempt_disable(); + + riscv_v_start(RISCV_KERNEL_MODE_V); +} + +/* + * Release the CPU vector context. + * + * Must be called from a context in which get_cpu_vector_context() was + * previously called, with no call to put_cpu_vector_context() in the + * meantime. + */ +void put_cpu_vector_context(void) +{ + riscv_v_stop(RISCV_KERNEL_MODE_V); + + preempt_enable(); +} + +/* + * kernel_vector_begin(): obtain the CPU vector registers for use by the calling + * context + * + * Must not be called unless may_use_simd() returns true. + * Task context in the vector registers is saved back to memory as necessary. + * + * A matching call to kernel_vector_end() must be made before returning from the + * calling context. + * + * The caller may freely use the vector registers until kernel_vector_end() is + * called. + */ +void kernel_vector_begin(void) +{ + if (WARN_ON(!has_vector())) + return; + + BUG_ON(!may_use_simd()); + + get_cpu_vector_context(); + + riscv_v_vstate_save(current, task_pt_regs(current)); + + riscv_v_enable(); +} +EXPORT_SYMBOL_GPL(kernel_vector_begin); + +/* + * kernel_vector_end(): give the CPU vector registers back to the current task + * + * Must be called from a context in which kernel_vector_begin() was previously + * called, with no call to kernel_vector_end() in the meantime. + * + * The caller must not use the vector registers after this function is called, + * unless kernel_vector_begin() is called again in the meantime. + */ +void kernel_vector_end(void) +{ + if (WARN_ON(!has_vector())) + return; + + riscv_v_vstate_restore(current, task_pt_regs(current)); + + riscv_v_disable(); + + put_cpu_vector_context(); +} +EXPORT_SYMBOL_GPL(kernel_vector_end); diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 4f21d970a129..4a1275db1146 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -221,6 +221,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) childregs->a0 = 0; /* Return value of fork() */ p->thread.s[0] = 0; } + p->thread.riscv_v_flags = 0; p->thread.ra = (unsigned long)ret_from_fork; p->thread.sp = (unsigned long)childregs; /* kernel sp */ return 0;