From patchwork Thu Dec 28 01:42:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13505571 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 1EBDDC3DA6E for ; Thu, 28 Dec 2023 03:45:47 +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=tNUmNnoauJNUqqlcb2djaLUZhrUnNjp91jQw4d4EJh0=; b=2JuF0uaH+WpmcO 99Tv5dlws9s54vSKeQ/RUlSoQHle8GTzRs+MHMYY33oBw35Gtx97/QeCFfSn7U5ZdUA0j/+K8Z71u v+0wHlYbqsUQC9JuVL6ywNpNGfXQu9lLOgkg8ogYxAsd0T3DVkEw4qv8ELiSsL1tzLQJIDWtHSBzC GqEnWBwtNgxsIOfBCOMZoDyZ9u8OpgkKIsUdPlbDnB686Hknw5TAN75RBsayPaBggk+N2fYOw2K0P SBle9XL6nlnCV9lST5QJ184ioiRDI5KcTQ+ncBDczqaO8+kvxrpqH3DpVu0dZZz4h5sRQM+QTp58r W8bSMtUY/zmW7EK3pcUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rIhL7-00G3On-1y; Thu, 28 Dec 2023 03:45:45 +0000 Received: from mail-oi1-x22d.google.com ([2607:f8b0:4864:20::22d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rIfPx-00FrsW-1I for linux-arm-kernel@lists.infradead.org; Thu, 28 Dec 2023 01:42:41 +0000 Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3bbd1e9c5f7so57406b6e.3 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=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=7lB3PwOsXsU0eA+wVy54fia689m8tbEsIcyHsrS82lA=; b=QhTuPNe0G7MIA5moKhgToAA+LBq4UDsFq8BZMHZlCLe488dqCAnL/RwLuwkiIuLjnw nYt285QBc89spGKtGF2K8bwYdhV6+vYwRAVeAxXHSI2VSRPIsQJ92t9AgRkQqBs24pSe +o1W43I2ABiqA2Ed1lnRNlLwod0u9GArJYnOvUtrVpkaog72dApXfhQnQwZtFzN0Ea/8 Q3MSiPsseJik7MQuvnju/rFhBk5cop+/DhIWtrAIqhw9ch5i5qNKGybfSvFugs8Jy61H i0hfaP5KTZljrr+AcvDmFGmXq5Qxh26j69/RxCI2Deg9QNQLOhMz3hVK6IboJ/WVyF1H 6wOA== 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=V6pOcaii6GlDHHed2ExDfIWPb1TIFN/Rs+wGTiIbHhn1dXni3p692sLoxQMWazd7cV +U/Wm+rK/VJXh4tdE+q5DqKatWCNOwB/jhs8QDYGCJri3uimwt9EUyuNppaiRc0qwpkM pFV8DTBKI413ovXGHWsH+RwhQ2ccqmc4bioqbUP0Fk65I+2yn5nP08cnfKD/0ziYDsp7 dxOvZsCF4pMbb+yAhVmuCWsiwWe3GqZtiaXGFAy+w5zQ8YY1jSGI7ck6ZZOzsjv9aJas GCJS99CDOsdWaXCti/IQ3iGjQRd/gRbsVwGNk8T+PLHHX5WO+9kUisrGoVpmKdwoeEoJ 2AgA== X-Gm-Message-State: AOJu0Yx1OAFtzB+vRFVG1Ir7ZYmFLwnWNHtNHLqlRt2Th6xHZFYAY5AA GJs5bOn93D58SlioGNF+Xk83La2qdb7IM9C1Lwc55D/nbiJCIKZe7IRG4ddUC/9ZcoZehIYrTVZ OIfytV5e1cQQjqtq3zekufPbOVvMYwBFBqcLd634j9VFKa98uRZdE9a5eOAyb9sJgPgOvZSzgW5 99eV99isYDULIoJERTceIBWesQ9yHjBPnfpQ== X-Google-Smtp-Source: AGHT+IGOumGBELysHMV6R76DMfrPeDaWnM5H7Bg8VEJaO8mkFNmjt6T/b5crzc3IAojRuB/OVWCSfQ== 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231227_174237_503410_493E8611 X-CRM114-Status: GOOD ( 15.95 ) 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 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 Reviewed-by: Christoph Hellwig Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt --- 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);