xref: /openbmc/linux/arch/loongarch/kernel/kfpu.c (revision 61f4d204)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2023 Loongson Technology Corporation Limited
4  */
5 
6 #include <linux/cpu.h>
7 #include <linux/init.h>
8 #include <asm/fpu.h>
9 #include <asm/smp.h>
10 
11 static DEFINE_PER_CPU(bool, in_kernel_fpu);
12 
13 void kernel_fpu_begin(void)
14 {
15 	preempt_disable();
16 
17 	WARN_ON(this_cpu_read(in_kernel_fpu));
18 
19 	this_cpu_write(in_kernel_fpu, true);
20 
21 	if (!is_fpu_owner())
22 		enable_fpu();
23 	else
24 		_save_fp(&current->thread.fpu);
25 
26 	write_fcsr(LOONGARCH_FCSR0, 0);
27 }
28 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
29 
30 void kernel_fpu_end(void)
31 {
32 	WARN_ON(!this_cpu_read(in_kernel_fpu));
33 
34 	if (!is_fpu_owner())
35 		disable_fpu();
36 	else
37 		_restore_fp(&current->thread.fpu);
38 
39 	this_cpu_write(in_kernel_fpu, false);
40 
41 	preempt_enable();
42 }
43 EXPORT_SYMBOL_GPL(kernel_fpu_end);
44