Received: by 2002:a05:7208:3003:b0:81:def:69cd with SMTP id f3csp828379rba; Wed, 27 Mar 2024 13:04:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVZdb+/NJYmPTijfrJqEPIfkk0XDMGOM3+CW6wt7dXnKAWPM84oGSnvw9B3fD9hy2/DQNjkMCsBDBDUrYgwKPEZT9cuEsapEl+j4+l6+A== X-Google-Smtp-Source: AGHT+IFXoPaj4eTzQrQuiHhx2aqGXWuBtg40dLLvKjRdJZjwv8FtuCrN4LTkk/DTv6HMjXZ1wErr X-Received: by 2002:a05:622a:1016:b0:431:2006:79c3 with SMTP id d22-20020a05622a101600b00431200679c3mr883178qte.22.1711569857078; Wed, 27 Mar 2024 13:04:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711569857; cv=pass; d=google.com; s=arc-20160816; b=f3yWmzzIGvfnm3GhoCWGrIcK488+vvg0Ny14/oMwkOQrVjY84MIoptXRFf5518tgfM o0dGjqsbkLrTaPXkGT4sMhM2QOmwA5tAvBc6Ry0EimY3pDkyUOHWAd8IYtlGFkB6diHr keQMBSwNjlKt3Fw2rIw39++xTpzTBiXfLrt8TOryFADIAGMTXrDG6La5itFwowqJSmlQ Vfj6H+8sBqupunzJytfnQzZeNBnuxmyqqoAyxczQM1L17wiKqcdttJzWnH33Q7Ekdx4x aI8mf0iax//PKJMJQG+TSEFEJidajc0zowNUfeVzVQ5jQcNmt/dcXHaY1AfrasgOSVky r6EA== ARC-Message-Signature: i=2; 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=tGWc9ebaY2QC87evSRTtIDPTTKODWYBNdAFlf6sP+x8=; fh=a/t+X/NCktzp2OESF9zoyYwudLo9b34qJio2XQYj43E=; b=emHqN7RWM9EkAoRrU28LItubYvGXs1HU4MEZ24zFVL0db4f3PNytaf8sqv8CPiDcYm qunTyMDUKW6YqneB4cwlq5GnS+r+OPFTtqXZXlL5gD2hVTF+csVaSLCLEx22X5BVkgfD JzWhrpAQDwGkJ6FXsPED1SNx+pNtC0daVkxkEW8paeoFtR2ilC2D5SggndT7pVF4ge6q oueC4NMMX39P1OGz1pfbExOtSKonDjyKUlf/foZQUteVI5+YWd5Fyx2g7d9rQtFMAtWG jh2hyFi56DbUodzSUL/J4YRn71euCWB/6SkgI7xzZ3mmJwgIocVLz78nZkUuZikjbJ78 jc8w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=cFsvDM3u; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-121943-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121943-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 b21-20020a05622a021500b00431257303bbsi10702642qtx.78.2024.03.27.13.04.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 13:04:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121943-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=cFsvDM3u; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-121943-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121943-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 C3ECB1C31E34 for ; Wed, 27 Mar 2024 20:04:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB54D154433; Wed, 27 Mar 2024 20:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="cFsvDM3u" Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 235EF153BD7 for ; Wed, 27 Mar 2024 20:02:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711569734; cv=none; b=tJV5OKJkfhRPjWgznyciHauwTHQeUbUU8uZ3aRY295Glh82MViqwQdyvumKWz/gK6ck9eKrba6vpczyzc+P1r7CGtO3R3KTMWNr6Jrz0a9MhFGlR0ybHHFtJeRRdLTN6V8UrFbm6wzJo+ATpIVaADHbYoAeUg6LrmN15tmOym5U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711569734; c=relaxed/simple; bh=D6H+zfohyEBjWK7u8MlCfJa00G/AECIZNSXMg7mgjSI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cG/R9QYCE192d1Yd1CVA/gXY4YOdyG9UIvr0n+syZAmIxhmhXhxtZ8PQAqMilEqUfEhmdtCrGde7HshR5g6HJ/MgPtboeSaUgh/ZcNp7ukVMzqNAlQr78W6o6w6S20y4UIYRfg2MWLwQypWdYo8oxd2D95rmc19u0rkqRfhk3HM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=cFsvDM3u; arc=none smtp.client-ip=209.85.214.173 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-pl1-f173.google.com with SMTP id d9443c01a7336-1e0d654590bso1859665ad.2 for ; Wed, 27 Mar 2024 13:02:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1711569732; x=1712174532; 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=tGWc9ebaY2QC87evSRTtIDPTTKODWYBNdAFlf6sP+x8=; b=cFsvDM3ur9SJxx0hQBb8eyFb7Ufq4lsaUSiRrERC7eJuRqPGqI6BQso4U865sz5uOv P725SmFzzkx4lDG7RGZhBvRpgD9y6wiirhwyoAXl2X/5fYTAKeyx/N3GqaWMC/DiZkvw W/NRgsfZiMxUMl0g/ViLX9LMS33VeuCXYiDoxNb80zoAF60hucvl8b5frEFZQg6hOpKJ 50amoL31Lxe/jSjuS9PUjYGuwtPT8ecqVRvDj9dH9L5OOeevOEkN8jMFf7m7uM63nlMH RCbZQUWV6m7WVR/vRdNUPGm1eFy+P/Zo3zkuLToaIstuoDftyFYSaeasOyg0MEdPuaUW RyTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711569732; x=1712174532; 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=tGWc9ebaY2QC87evSRTtIDPTTKODWYBNdAFlf6sP+x8=; b=sgyGAswL0wF0105gX881VXqHztI00gmd8wt6oGiP3/RTIOUtDnJaJmfHnhi6G/jtj9 Qp+UUFYtFCTHZsP+Tjng1ZtNSH6w0Tf2Nj0UnAj7m2ClNPdhhNnab9/IEPnhqz9tU8Pc ikJ+7F11KcBZs3GYU/vhneXKownX1CVm1D2g890du7LclApETBvUS1uGdeWawT0ic//N wNl9bH/2dfSU4UPwG4N+d2IwHNvA0fb3oNPoYtRZFCju+TmwdtoUt23TXNeSwtgwqnX/ y//mNuST33mV7iSRNFU648dO3cToBXXjlrQ0+MQbXhXEBY05gxW3z2w4ovzJdGpYAKvR qSVg== X-Gm-Message-State: AOJu0Yyz6FAdV34aNiL701LLgJhl/OigKG8noqAcFhNM+uFC9sMMIYFj ljokU+0cdTGnJdcip/Jki28kYo/Z4OAKj8NgxFjfxCA18z4CtzNA8OsHTqVGkDI= X-Received: by 2002:a17:902:ec92:b0:1e0:73d:9175 with SMTP id x18-20020a170902ec9200b001e0073d9175mr916865plg.45.1711569732549; Wed, 27 Mar 2024 13:02:12 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id u4-20020a170902e5c400b001dd0d0d26a4sm9446459plf.147.2024.03.27.13.02.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 13:02:12 -0700 (PDT) From: Samuel Holland To: Andrew Morton , linux-arm-kernel@lists.infradead.org, x86@kernel.org Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, Christoph Hellwig , loongarch@lists.linux.dev, amd-gfx@lists.freedesktop.org, Samuel Holland , Palmer Dabbelt Subject: [PATCH v3 10/14] riscv: Add support for kernel-mode FPU Date: Wed, 27 Mar 2024 13:00:41 -0700 Message-ID: <20240327200157.1097089-11-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240327200157.1097089-1-samuel.holland@sifive.com> References: <20240327200157.1097089-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. Support is limited to riscv64 because riscv32 requires runtime (libgcc) assistance to convert between doubles and 64-bit integers. Acked-by: Palmer Dabbelt Reviewed-by: Palmer Dabbelt Reviewed-by: Christoph Hellwig Signed-off-by: Samuel Holland --- Changes in v3: - Rebase on v6.9-rc1 - Limit ARCH_HAS_KERNEL_FPU_SUPPORT to 64BIT 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 be09c8836d56..3bcd0d250810 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 64BIT && FPU select ARCH_HAS_MEMBARRIER_CALLBACKS select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_MMIOWB diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 252d63942f34..76ff4033c854 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -84,6 +84,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 81d94a8ee10f..5b243d46f4b1 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -67,6 +67,7 @@ obj-$(CONFIG_RISCV_MISALIGNED) += unaligned_access_speed.o obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) += copy-unaligned.o obj-$(CONFIG_FPU) += fpu.o +obj-$(CONFIG_FPU) += kernel_mode_fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o obj-$(CONFIG_SMP) += smpboot.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.43.1