Loading...
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#elif defined(CONFIG_ARM64)
35#include <asm/neon.h>
36#elif defined(CONFIG_LOONGARCH)
37#include <asm/fpu.h>
38#endif
39
40/**
41 * DOC: DC FPU manipulation overview
42 *
43 * DC core uses FPU operations in multiple parts of the code, which requires a
44 * more specialized way to manage these areas' entrance. To fulfill this
45 * requirement, we created some wrapper functions that encapsulate
46 * kernel_fpu_begin/end to better fit our need in the display component. In
47 * summary, in this file, you can find functions related to FPU operation
48 * management.
49 */
50
51static DEFINE_PER_CPU(int, fpu_recursion_depth);
52
53/**
54 * dc_assert_fp_enabled - Check if FPU protection is enabled
55 *
56 * This function tells if the code is already under FPU protection or not. A
57 * function that works as an API for a set of FPU operations can use this
58 * function for checking if the caller invoked it after DC_FP_START(). For
59 * example, take a look at dcn20_fpu.c file.
60 */
61inline void dc_assert_fp_enabled(void)
62{
63 int depth;
64
65 depth = __this_cpu_read(fpu_recursion_depth);
66
67 ASSERT(depth >= 1);
68}
69
70/**
71 * dc_fpu_begin - Enables FPU protection
72 * @function_name: A string containing the function name for debug purposes
73 * (usually __func__)
74 *
75 * @line: A line number where DC_FP_START was invoked for debug purpose
76 * (usually __LINE__)
77 *
78 * This function is responsible for managing the use of kernel_fpu_begin() with
79 * the advantage of providing an event trace for debugging.
80 *
81 * Note: Do not call this function directly; always use DC_FP_START().
82 */
83void dc_fpu_begin(const char *function_name, const int line)
84{
85 int depth;
86
87 WARN_ON_ONCE(!in_task());
88 preempt_disable();
89 depth = __this_cpu_inc_return(fpu_recursion_depth);
90
91 if (depth == 1) {
92#if defined(CONFIG_X86) || defined(CONFIG_LOONGARCH)
93 kernel_fpu_begin();
94#elif defined(CONFIG_PPC64)
95 if (cpu_has_feature(CPU_FTR_VSX_COMP))
96 enable_kernel_vsx();
97 else if (cpu_has_feature(CPU_FTR_ALTIVEC_COMP))
98 enable_kernel_altivec();
99 else if (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE))
100 enable_kernel_fp();
101#elif defined(CONFIG_ARM64)
102 kernel_neon_begin();
103#endif
104 }
105
106 TRACE_DCN_FPU(true, function_name, line, depth);
107}
108
109/**
110 * dc_fpu_end - Disable FPU protection
111 * @function_name: A string containing the function name for debug purposes
112 * @line: A-line number where DC_FP_END was invoked for debug purpose
113 *
114 * This function is responsible for managing the use of kernel_fpu_end() with
115 * the advantage of providing an event trace for debugging.
116 *
117 * Note: Do not call this function directly; always use DC_FP_END().
118 */
119void dc_fpu_end(const char *function_name, const int line)
120{
121 int depth;
122
123 depth = __this_cpu_dec_return(fpu_recursion_depth);
124 if (depth == 0) {
125#if defined(CONFIG_X86) || defined(CONFIG_LOONGARCH)
126 kernel_fpu_end();
127#elif defined(CONFIG_PPC64)
128 if (cpu_has_feature(CPU_FTR_VSX_COMP))
129 disable_kernel_vsx();
130 else if (cpu_has_feature(CPU_FTR_ALTIVEC_COMP))
131 disable_kernel_altivec();
132 else if (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE))
133 disable_kernel_fp();
134#elif defined(CONFIG_ARM64)
135 kernel_neon_end();
136#endif
137 } else {
138 WARN_ON_ONCE(depth < 0);
139 }
140
141 TRACE_DCN_FPU(false, function_name, line, depth);
142 preempt_enable();
143}
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#elif defined(CONFIG_ARM64)
35#include <asm/neon.h>
36#endif
37
38/**
39 * DOC: DC FPU manipulation overview
40 *
41 * DC core uses FPU operations in multiple parts of the code, which requires a
42 * more specialized way to manage these areas' entrance. To fulfill this
43 * requirement, we created some wrapper functions that encapsulate
44 * kernel_fpu_begin/end to better fit our need in the display component. In
45 * summary, in this file, you can find functions related to FPU operation
46 * management.
47 */
48
49static DEFINE_PER_CPU(int, fpu_recursion_depth);
50
51/**
52 * dc_assert_fp_enabled - Check if FPU protection is enabled
53 *
54 * This function tells if the code is already under FPU protection or not. A
55 * function that works as an API for a set of FPU operations can use this
56 * function for checking if the caller invoked it after DC_FP_START(). For
57 * example, take a look at dcn20_fpu.c file.
58 */
59inline void dc_assert_fp_enabled(void)
60{
61 int *pcpu, depth = 0;
62
63 pcpu = get_cpu_ptr(&fpu_recursion_depth);
64 depth = *pcpu;
65 put_cpu_ptr(&fpu_recursion_depth);
66
67 ASSERT(depth >= 1);
68}
69
70/**
71 * dc_fpu_begin - Enables FPU protection
72 * @function_name: A string containing the function name for debug purposes
73 * (usually __func__)
74 *
75 * @line: A line number where DC_FP_START was invoked for debug purpose
76 * (usually __LINE__)
77 *
78 * This function is responsible for managing the use of kernel_fpu_begin() with
79 * the advantage of providing an event trace for debugging.
80 *
81 * Note: Do not call this function directly; always use DC_FP_START().
82 */
83void dc_fpu_begin(const char *function_name, const int line)
84{
85 int *pcpu;
86
87 pcpu = get_cpu_ptr(&fpu_recursion_depth);
88 *pcpu += 1;
89
90 if (*pcpu == 1) {
91#if defined(CONFIG_X86)
92 kernel_fpu_begin();
93#elif defined(CONFIG_PPC64)
94 if (cpu_has_feature(CPU_FTR_VSX_COMP)) {
95 preempt_disable();
96 enable_kernel_vsx();
97 } else if (cpu_has_feature(CPU_FTR_ALTIVEC_COMP)) {
98 preempt_disable();
99 enable_kernel_altivec();
100 } else if (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE)) {
101 preempt_disable();
102 enable_kernel_fp();
103 }
104#elif defined(CONFIG_ARM64)
105 kernel_neon_begin();
106#endif
107 }
108
109 TRACE_DCN_FPU(true, function_name, line, *pcpu);
110 put_cpu_ptr(&fpu_recursion_depth);
111}
112
113/**
114 * dc_fpu_end - Disable FPU protection
115 * @function_name: A string containing the function name for debug purposes
116 * @line: A-line number where DC_FP_END was invoked for debug purpose
117 *
118 * This function is responsible for managing the use of kernel_fpu_end() with
119 * the advantage of providing an event trace for debugging.
120 *
121 * Note: Do not call this function directly; always use DC_FP_END().
122 */
123void dc_fpu_end(const char *function_name, const int line)
124{
125 int *pcpu;
126
127 pcpu = get_cpu_ptr(&fpu_recursion_depth);
128 *pcpu -= 1;
129 if (*pcpu <= 0) {
130#if defined(CONFIG_X86)
131 kernel_fpu_end();
132#elif defined(CONFIG_PPC64)
133 if (cpu_has_feature(CPU_FTR_VSX_COMP)) {
134 disable_kernel_vsx();
135 preempt_enable();
136 } else if (cpu_has_feature(CPU_FTR_ALTIVEC_COMP)) {
137 disable_kernel_altivec();
138 preempt_enable();
139 } else if (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE)) {
140 disable_kernel_fp();
141 preempt_enable();
142 }
143#elif defined(CONFIG_ARM64)
144 kernel_neon_end();
145#endif
146 }
147
148 TRACE_DCN_FPU(false, function_name, line, *pcpu);
149 put_cpu_ptr(&fpu_recursion_depth);
150}