xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/dc_fpu.c (revision aec2f682d47c54ef434b2d440992626d80b1ebdc)
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 #include <linux/fpu.h>
30 
31 /**
32  * DOC: DC FPU manipulation overview
33  *
34  * DC core uses FPU operations in multiple parts of the code, which requires a
35  * more specialized way to manage these areas' entrance. To fulfill this
36  * requirement, we created some wrapper functions that encapsulate
37  * kernel_fpu_begin/end to better fit our need in the display component. In
38  * summary, in this file, you can find functions related to FPU operation
39  * management.
40  */
41 
42 static DEFINE_PER_CPU(int, fpu_recursion_depth);
43 
44 /**
45  * dc_assert_fp_enabled - Check if FPU protection is enabled
46  *
47  * This function tells if the code is already under FPU protection or not. A
48  * function that works as an API for a set of FPU operations can use this
49  * function for checking if the caller invoked it after DC_FP_START(). For
50  * example, take a look at dcn20_fpu.c file.
51  */
52 inline void dc_assert_fp_enabled(void)
53 {
54 	int depth;
55 
56 	depth = this_cpu_read(fpu_recursion_depth);
57 
58 	ASSERT(depth >= 1);
59 }
60 
61 /**
62  * dc_assert_fp_enabled - Check if FPU protection is enabled
63  *
64  * This function tells if the code is already under FPU protection or not. A
65  * function that works as an API for a set of FPU operations can use this
66  * function for checking if the caller invoked it after DC_FP_START(). For
67  * example, take a look at dcn20_fpu.c file.
68  *
69  * Similar to dc_assert_fp_enabled, but does not assert, returns status instead.
70  */
71 inline bool dc_is_fp_enabled(void)
72 {
73 	int depth;
74 
75 	depth = this_cpu_read(fpu_recursion_depth);
76 
77 	return (depth >= 1);
78 }
79 
80 /**
81  * dc_fpu_begin - Enables FPU protection
82  * @function_name: A string containing the function name for debug purposes
83  *   (usually __func__)
84  *
85  * @line: A line number where DC_FP_START was invoked for debug purpose
86  *   (usually __LINE__)
87  *
88  * This function is responsible for managing the use of kernel_fpu_begin() with
89  * the advantage of providing an event trace for debugging.
90  *
91  * Note: Do not call this function directly; always use DC_FP_START().
92  */
93 void dc_fpu_begin(const char *function_name, const int line)
94 {
95 	int depth;
96 
97 	WARN_ON_ONCE(!in_task());
98 	preempt_disable();
99 	depth = this_cpu_inc_return(fpu_recursion_depth);
100 	if (depth == 1) {
101 		BUG_ON(!kernel_fpu_available());
102 		kernel_fpu_begin();
103 	}
104 
105 	TRACE_DCN_FPU(true, function_name, line, depth);
106 }
107 
108 /**
109  * dc_fpu_end - Disable FPU protection
110  * @function_name: A string containing the function name for debug purposes
111  * @line: A-line number where DC_FP_END was invoked for debug purpose
112  *
113  * This function is responsible for managing the use of kernel_fpu_end() with
114  * the advantage of providing an event trace for debugging.
115  *
116  * Note: Do not call this function directly; always use DC_FP_END().
117  */
118 void dc_fpu_end(const char *function_name, const int line)
119 {
120 	int depth;
121 
122 	depth = this_cpu_dec_return(fpu_recursion_depth);
123 	if (depth == 0) {
124 		kernel_fpu_end();
125 	} else {
126 		WARN_ON_ONCE(depth < 0);
127 	}
128 
129 	TRACE_DCN_FPU(false, function_name, line, depth);
130 	preempt_enable();
131 }
132