Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp3702144rdb; Wed, 27 Dec 2023 17:45:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHO/5aHtyZRajEM/5fJCR2V2lmORuYLHu4XDK5tykggMQk59Li/H1olMxVeC6tItDQsPN2V X-Received: by 2002:a05:622a:2287:b0:427:f03e:801a with SMTP id ay7-20020a05622a228700b00427f03e801amr1539216qtb.134.1703727902328; Wed, 27 Dec 2023 17:45:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703727902; cv=none; d=google.com; s=arc-20160816; b=Wb+/hQCTt9LseFFf6UBa34p5l018zyIWCFIkistyQA7COW4zeCqCRdhZA4QcOYF6Nh l89VgrxCqN8oQKx7JSyKz328OOTw1go6PsYK8TLYy5CcBu09GHcYmjf+qijesP/X/LwF BaRn7Bhu8DWDeyiVHLaU1FWC9KoiunNW5+15+5dnMYWciWvMr14RjlIEgomaZKk2k0AX UlmHgdOlMiFSvQx/756Ua/nj7GfDI7ytZAyDSFpEjtD8lqAOiDWvW68TRkpBEzzmasz5 qJx35xfdqab1Hw9zWfzavg7f/95cmebXUSrcRltywCJVelPwHVNBYIT1pT46Z9tmskKe TlUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=7lB3PwOsXsU0eA+wVy54fia689m8tbEsIcyHsrS82lA=; fh=xcb3qhkLQEi6t+Vw3J1T1wFra1a2BdIsNlygM9Jzpag=; b=Kc97cm+ZIdi5JTgtdT7iIGJwkYTJvTWkDi8DvG+ZRvr4nGi5DyN+KInPZXn6RQMxQE +/UDqvf/GmmExV561w6R+qZ8vSTLCb0aELF+7bbHhgkWmd9xL7C54QE2A+jHISfPkeRR 7Bh1fDJP/gTp5i4Vzvw5Qhl4+9SyLHMUkBbjVTV0xqS3cyNAmzrqsZ/KLJ87hZCu38jr k8kukQ9PtMx0LpAvjr/L8ptUuK0GV3LE9IYsEv2X/fD+1wy6C5LAUU9tpAW01pqLyFfz ZrvjYcGvOiuPPWHi97uWE/oGH3eDD2o9qdhcDPpjI3A5KeI0Z5+3gqvr/kLpwHXBJagg 5T0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=R36n+Nhx; spf=pass (google.com: domain of linux-kernel+bounces-12377-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12377-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id l17-20020a05622a051100b004278578a3bcsi15831359qtx.678.2023.12.27.17.45.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Dec 2023 17:45:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12377-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=R36n+Nhx; spf=pass (google.com: domain of linux-kernel+bounces-12377-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12377-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 185D81C20B83 for ; Thu, 28 Dec 2023 01:45:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8CA0F8BE8; Thu, 28 Dec 2023 01:42:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="R36n+Nhx" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (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 72A4563CD for ; Thu, 28 Dec 2023 01:42:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3bbd11777c2so97071b6e.1 for ; Wed, 27 Dec 2023 17:42:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1703727755; x=1704332555; 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=7lB3PwOsXsU0eA+wVy54fia689m8tbEsIcyHsrS82lA=; b=R36n+Nhxv2ci/t7udVXZLBbXXNdq+2qk4+dSW/KR48C9REzX+GNf1WuMlFuHzE7AJI Bt0QG4ouo5TvpSX+PpCTXpBeYh8XO2hG0TXxZJG4H2zHNHZkQGoz5mUBDiu769b5X+Xm G9Za+MuMqsJrE8IE4gJ9M1oGSTc5W7TeQin1Hml5GpqosqXr/dhyqeQ4CfK4GC3gxP4o kNtOA8oHTB6nkFG9pqDpnkAG5F+TUMxovUVHoa0yUZMUWrBD8l8mxmJIKeVMWxR1Lv09 8eaD7oFgk3Wicha4tbOy4PjIxdbVjFLLjlZ39QsF07AaEnfX8sO29FMOYW+AoftaRl+W 7T7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703727755; x=1704332555; 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=7lB3PwOsXsU0eA+wVy54fia689m8tbEsIcyHsrS82lA=; b=e9uAWfbmnJ0/2TcZhdPz/G26tQHLIiVO28j50SbJ411G7hIJCZ4dPudmP3NmPp6b31 +wmaNYS+o18HSsQJrOwiSgOAIQ9DCVoAF7+p8vX3gyw8YSqkGlEigxQDaFvMK+2LIrh+ xNjer/C9lKPJ9sez7auE7I/447vKwYH0W9Msvd62y+2qh+fUuMiTUSJsUzmFjpxq86v6 Qf0Ev4LzMZMr+fd6OoAwJPExG1kPZLDaTaEwwcndsxblDdut3O7cxIUgpiqlx9BJb6cz 8zaRSbvHJcjsnrQVYnPMn9m96ob2XA7UTKL35l+TZ5sDylVSgD012g1mxBRtdtmW87TK 4gpw== X-Gm-Message-State: AOJu0YwkSShGfHDRPwVIK5WCHkOfSJEuYqft0KofHIHZLc+F8pwoVvNV 8/ywzVtYAYGfLqEVfnVbhGPqhr9AwVLQDg== X-Received: by 2002:a05:6358:9213:b0:174:cf3a:749 with SMTP id d19-20020a056358921300b00174cf3a0749mr9824262rwb.36.1703727755495; Wed, 27 Dec 2023 17:42:35 -0800 (PST) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id g24-20020aa78758000000b006d49ed3effasm7335440pfo.63.2023.12.27.17.42.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Dec 2023 17:42:35 -0800 (PST) From: Samuel Holland To: linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-riscv@lists.infradead.org, Christoph Hellwig Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, linux-arch@vger.kernel.org, Samuel Holland Subject: [PATCH v2 10/14] riscv: Add support for kernel-mode FPU Date: Wed, 27 Dec 2023 17:42:00 -0800 Message-ID: <20231228014220.3562640-11-samuel.holland@sifive.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231228014220.3562640-1-samuel.holland@sifive.com> References: <20231228014220.3562640-1-samuel.holland@sifive.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This is motivated by the amdgpu DRM driver, which needs floating-point code to support recent hardware. That code is not performance-critical, so only provide a minimal non-preemptible implementation for now. Signed-off-by: Samuel Holland --- Changes in v2: - Remove RISC-V architecture-specific preprocessor check arch/riscv/Kconfig | 1 + arch/riscv/Makefile | 3 +++ arch/riscv/include/asm/fpu.h | 16 ++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_fpu.c | 28 ++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 arch/riscv/include/asm/fpu.h create mode 100644 arch/riscv/kernel/kernel_mode_fpu.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 24c1799e2ec4..4d4d1d64ce34 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -27,6 +27,7 @@ config RISCV select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_GIGANTIC_PAGE select ARCH_HAS_KCOV + select ARCH_HAS_KERNEL_FPU_SUPPORT if FPU select ARCH_HAS_MMIOWB select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE select ARCH_HAS_PMEM_API diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index a74be78678eb..2e719c369210 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -81,6 +81,9 @@ KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64i KBUILD_AFLAGS += -march=$(riscv-march-y) +# For C code built with floating-point support, exclude V but keep F and D. +CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/') + KBUILD_CFLAGS += -mno-save-restore KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET) diff --git a/arch/riscv/include/asm/fpu.h b/arch/riscv/include/asm/fpu.h new file mode 100644 index 000000000000..91c04c244e12 --- /dev/null +++ b/arch/riscv/include/asm/fpu.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2023 SiFive + */ + +#ifndef _ASM_RISCV_FPU_H +#define _ASM_RISCV_FPU_H + +#include + +#define kernel_fpu_available() has_fpu() + +void kernel_fpu_begin(void); +void kernel_fpu_end(void); + +#endif /* ! _ASM_RISCV_FPU_H */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index fee22a3d1b53..662c483e338d 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -62,6 +62,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o +obj-$(CONFIG_FPU) += kernel_mode_fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o diff --git a/arch/riscv/kernel/kernel_mode_fpu.c b/arch/riscv/kernel/kernel_mode_fpu.c new file mode 100644 index 000000000000..0ac8348876c4 --- /dev/null +++ b/arch/riscv/kernel/kernel_mode_fpu.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 SiFive + */ + +#include +#include + +#include +#include +#include +#include + +void kernel_fpu_begin(void) +{ + preempt_disable(); + fstate_save(current, task_pt_regs(current)); + csr_set(CSR_SSTATUS, SR_FS); +} +EXPORT_SYMBOL_GPL(kernel_fpu_begin); + +void kernel_fpu_end(void) +{ + csr_clear(CSR_SSTATUS, SR_FS); + fstate_restore(current, task_pt_regs(current)); + preempt_enable(); +} +EXPORT_SYMBOL_GPL(kernel_fpu_end); -- 2.42.0