Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp827395rwo; Sat, 22 Jul 2023 00:32:37 -0700 (PDT) X-Google-Smtp-Source: APBJJlGiXFNgoUnws7g8ejqA8Hm0G8E+F0LiuaT0Bk3ZRVeL0dQynoJElyPui96XCQ9yGNBLC+Sp X-Received: by 2002:a6b:f314:0:b0:780:c38e:e785 with SMTP id m20-20020a6bf314000000b00780c38ee785mr2396319ioh.17.1690011157056; Sat, 22 Jul 2023 00:32:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690011157; cv=none; d=google.com; s=arc-20160816; b=jmdiFczeIZfFojj1bm72ifa9gFBqSEInaHzh1Ej2MyAalaqH2EdwfADKSlIgsxqPmr Sd7eXSiQlSiAd2JpfNL8x9QQII7AcpBm3Stm9s3XDiqdj8NeNWyj25NcA04ooqOt3sIe u8bzzVoYvWF81BJjn8UiJo2Hzb84FOyxIwHR8j7gE27D5f0WLP893tIgOVWNSduGxGNz E7FbEgi9z+dASqodg1CYDJDd9VAwNzl6jnNMS5vK8TNq6gxpQJtJle/9JEhOI7XregcC xklTApKrgasxD36jA7ZJAKMeuFq8+eWrNlSV+jCrO4sSBrRpwGGAfB2Vuw9tLg7rLhai IjIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=GYiox1uuT704PWXLf/CpVdFMEPJ4te04jnTjNWO7K7o=; fh=5xZmlGRRr2qi/otnqDRHuPmlDBMQPQVJKa2ufmsLGQ4=; b=SC2IxxDbXJdCQSCUxiC/3jbfkNlvqISG9fCt7tFKCgNdOkUQKJzEDqE4UilVTQUhht NuWaH+wDhWirdIs4fnR3Affi/2TzI0XV8tP6cD51albZmTOAcwFS1CZUNx+LMvmowzhc riiJ1XhCxwso7wFy9SH9W3Trn41dIG0vpOqiNc/pxCFtrCNvCrolOKCWd8zEFKTj6z2S BYq72VKSY3CyY33BVoZXquftuinlj6MwIWsNh6ThvWXIxh+ZQ1Md66FSlYVuHRO1hrGk jO6ipyzBId0b/JtxqSaJK3o8c3IdbXimEP8kI5estNevtOHkbK8dCt9jEEGhRqK0c1fV 4BDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k67-20020a628446000000b006436618b22bsi4486367pfd.155.2023.07.22.00.32.20; Sat, 22 Jul 2023 00:32:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230034AbjGVHWP (ORCPT + 99 others); Sat, 22 Jul 2023 03:22:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229684AbjGVHWO (ORCPT ); Sat, 22 Jul 2023 03:22:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3C509B for ; Sat, 22 Jul 2023 00:22:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 374C460BA9 for ; Sat, 22 Jul 2023 07:22:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61F2AC433C7; Sat, 22 Jul 2023 07:22:10 +0000 (UTC) From: Huacai Chen To: Huacai Chen Cc: loongarch@lists.linux.dev, Xuefeng Li , Guo Ren , Xuerui Wang , Jiaxun Yang , linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn, Huacai Chen Subject: [PATCH] LoongArch: Allow usage of LSX/LASX in the kernel Date: Sat, 22 Jul 2023 15:22:01 +0800 Message-Id: <20230722072201.2677516-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Allow usage of LSX/LASX in the kernel by extending kernel_fpu_begin() and kernel_fpu_end(). Signed-off-by: Huacai Chen --- arch/loongarch/kernel/kfpu.c | 55 +++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/arch/loongarch/kernel/kfpu.c b/arch/loongarch/kernel/kfpu.c index 5c46ae8c6cac..ec5b28e570c9 100644 --- a/arch/loongarch/kernel/kfpu.c +++ b/arch/loongarch/kernel/kfpu.c @@ -8,19 +8,40 @@ #include #include +static unsigned int euen_mask = CSR_EUEN_FPEN; + +/* + * The critical section between kernel_fpu_begin() and kernel_fpu_end() + * is non-reentrant. It is the caller's responsibility to avoid reentrance. + * See drivers/gpu/drm/amd/display/amdgpu_dm/dc_fpu.c as an example. + */ static DEFINE_PER_CPU(bool, in_kernel_fpu); +static DEFINE_PER_CPU(unsigned int, euen_current); void kernel_fpu_begin(void) { + unsigned int *euen_curr; + preempt_disable(); WARN_ON(this_cpu_read(in_kernel_fpu)); this_cpu_write(in_kernel_fpu, true); + euen_curr = this_cpu_ptr(&euen_current); - if (!is_fpu_owner()) - enable_fpu(); + *euen_curr = csr_xchg32(euen_mask, euen_mask, LOONGARCH_CSR_EUEN); + +#ifdef CONFIG_CPU_HAS_LASX + if (*euen_curr & CSR_EUEN_LASXEN) + _save_lasx(¤t->thread.fpu); + else +#endif +#ifdef CONFIG_CPU_HAS_LSX + if (*euen_curr & CSR_EUEN_LSXEN) + _save_lsx(¤t->thread.fpu); else +#endif + if (*euen_curr & CSR_EUEN_FPEN) _save_fp(¤t->thread.fpu); write_fcsr(LOONGARCH_FCSR0, 0); @@ -29,15 +50,41 @@ EXPORT_SYMBOL_GPL(kernel_fpu_begin); void kernel_fpu_end(void) { + unsigned int *euen_curr; + WARN_ON(!this_cpu_read(in_kernel_fpu)); - if (!is_fpu_owner()) - disable_fpu(); + euen_curr = this_cpu_ptr(&euen_current); + +#ifdef CONFIG_CPU_HAS_LASX + if (*euen_curr & CSR_EUEN_LASXEN) + _restore_lasx(¤t->thread.fpu); else +#endif +#ifdef CONFIG_CPU_HAS_LSX + if (*euen_curr & CSR_EUEN_LSXEN) + _restore_lsx(¤t->thread.fpu); + else +#endif + if (*euen_curr & CSR_EUEN_FPEN) _restore_fp(¤t->thread.fpu); + *euen_curr = csr_xchg32(*euen_curr, euen_mask, LOONGARCH_CSR_EUEN); + this_cpu_write(in_kernel_fpu, false); preempt_enable(); } EXPORT_SYMBOL_GPL(kernel_fpu_end); + +static int __init init_euen_mask(void) +{ + if (cpu_has_lsx) + euen_mask |= CSR_EUEN_LSXEN; + + if (cpu_has_lasx) + euen_mask |= CSR_EUEN_LASXEN; + + return 0; +} +arch_initcall(init_euen_mask); -- 2.39.3