xref: /openbsd-src/sys/dev/pci/drm/amd/display/amdgpu_dm/dc_fpu.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2021 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 #include "dc_trace.h"
28 
29 #if defined(CONFIG_X86)
30 #include <asm/fpu/api.h>
31 #elif defined(CONFIG_PPC64)
32 #include <asm/switch_to.h>
33 #include <asm/cputable.h>
34 #endif
35 
36 /**
37  * DOC: DC FPU manipulation overview
38  *
39  * DC core uses FPU operations in multiple parts of the code, which requires a
40  * more specialized way to manage these areas' entrance. To fulfill this
41  * requirement, we created some wrapper functions that encapsulate
42  * kernel_fpu_begin/end to better fit our need in the display component. In
43  * summary, in this file, you can find functions related to FPU operation
44  * management.
45  */
46 
47 #ifdef notyet
48 static DEFINE_PER_CPU(int, fpu_recursion_depth);
49 #else
50 static int fpu_recursion_depth;
51 
52 #define get_cpu_ptr(x)	(x)
53 #define put_cpu_ptr(x)	do {} while(0)
54 #endif
55 
56 /**
57  * dc_assert_fp_enabled - Check if FPU protection is enabled
58  *
59  * This function tells if the code is already under FPU protection or not. A
60  * function that works as an API for a set of FPU operations can use this
61  * function for checking if the caller invoked it after DC_FP_START(). For
62  * example, take a look at dcn20_fpu.c file.
63  */
64 void dc_assert_fp_enabled(void)
65 {
66 	int *pcpu, depth = 0;
67 
68 	pcpu = get_cpu_ptr(&fpu_recursion_depth);
69 	depth = *pcpu;
70 	put_cpu_ptr(&fpu_recursion_depth);
71 
72 	ASSERT(depth >= 1);
73 }
74 
75 /**
76  * dc_fpu_begin - Enables FPU protection
77  * @function_name: A string containing the function name for debug purposes
78  *   (usually __func__)
79  *
80  * @line: A line number where DC_FP_START was invoked for debug purpose
81  *   (usually __LINE__)
82  *
83  * This function is responsible for managing the use of kernel_fpu_begin() with
84  * the advantage of providing an event trace for debugging.
85  *
86  * Note: Do not call this function directly; always use DC_FP_START().
87  */
88 void dc_fpu_begin(const char *function_name, const int line)
89 {
90 	int *pcpu;
91 
92 	pcpu = get_cpu_ptr(&fpu_recursion_depth);
93 	*pcpu += 1;
94 
95 	if (*pcpu == 1) {
96 #if defined(CONFIG_X86)
97 		kernel_fpu_begin();
98 #elif defined(CONFIG_PPC64)
99 		if (cpu_has_feature(CPU_FTR_VSX_COMP)) {
100 			preempt_disable();
101 			enable_kernel_vsx();
102 		} else if (cpu_has_feature(CPU_FTR_ALTIVEC_COMP)) {
103 			preempt_disable();
104 			enable_kernel_altivec();
105 		} else if (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE)) {
106 			preempt_disable();
107 			enable_kernel_fp();
108 		}
109 #endif
110 	}
111 
112 	TRACE_DCN_FPU(true, function_name, line, *pcpu);
113 	put_cpu_ptr(&fpu_recursion_depth);
114 }
115 
116 /**
117  * dc_fpu_end - Disable FPU protection
118  * @function_name: A string containing the function name for debug purposes
119  * @line: A-line number where DC_FP_END was invoked for debug purpose
120  *
121  * This function is responsible for managing the use of kernel_fpu_end() with
122  * the advantage of providing an event trace for debugging.
123  *
124  * Note: Do not call this function directly; always use DC_FP_END().
125  */
126 void dc_fpu_end(const char *function_name, const int line)
127 {
128 	int *pcpu;
129 
130 	pcpu = get_cpu_ptr(&fpu_recursion_depth);
131 	*pcpu -= 1;
132 	if (*pcpu <= 0) {
133 #if defined(CONFIG_X86)
134 		kernel_fpu_end();
135 #elif defined(CONFIG_PPC64)
136 		if (cpu_has_feature(CPU_FTR_VSX_COMP)) {
137 			disable_kernel_vsx();
138 			preempt_enable();
139 		} else if (cpu_has_feature(CPU_FTR_ALTIVEC_COMP)) {
140 			disable_kernel_altivec();
141 			preempt_enable();
142 		} else if (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE)) {
143 			disable_kernel_fp();
144 			preempt_enable();
145 		}
146 #endif
147 	}
148 
149 	TRACE_DCN_FPU(false, function_name, line, *pcpu);
150 	put_cpu_ptr(&fpu_recursion_depth);
151 }
152