1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *
4  * (C) COPYRIGHT 2012-2015, 2017, 2020-2021 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 #ifndef _KBASE_DEBUG_H
23 #define _KBASE_DEBUG_H
24 
25 #include <linux/bug.h>
26 
27 /** @brief If equals to 0, a trace containing the file, line, and function will be displayed before each message. */
28 #define KBASE_DEBUG_SKIP_TRACE 0
29 
30 /** @brief If different from 0, the trace will only contain the file and line. */
31 #define KBASE_DEBUG_SKIP_FUNCTION_NAME 0
32 
33 /** @brief Disable the asserts tests if set to 1. Default is to disable the asserts in release. */
34 #ifndef KBASE_DEBUG_DISABLE_ASSERTS
35 #ifdef CONFIG_MALI_BIFROST_DEBUG
36 #define KBASE_DEBUG_DISABLE_ASSERTS 0
37 #else
38 #define KBASE_DEBUG_DISABLE_ASSERTS 1
39 #endif
40 #endif				/* KBASE_DEBUG_DISABLE_ASSERTS */
41 
42 /** Function type that is called on an KBASE_DEBUG_ASSERT() or KBASE_DEBUG_ASSERT_MSG() */
43 typedef void kbase_debug_assert_hook(void *);
44 
45 struct kbasep_debug_assert_cb {
46 	kbase_debug_assert_hook *func;
47 	void *param;
48 };
49 
50 /**
51  * KBASEP_DEBUG_PRINT_TRACE - Private macro containing the format of the trace
52  * to display before every message @sa KBASE_DEBUG_SKIP_TRACE,
53  * KBASE_DEBUG_SKIP_FUNCTION_NAME
54  */
55 #if !KBASE_DEBUG_SKIP_TRACE
56 #define KBASEP_DEBUG_PRINT_TRACE \
57 		"In file: " __FILE__ " line: " CSTD_STR2(__LINE__)
58 #if !KBASE_DEBUG_SKIP_FUNCTION_NAME
59 #define KBASEP_DEBUG_PRINT_FUNCTION __func__
60 #else
61 #define KBASEP_DEBUG_PRINT_FUNCTION ""
62 #endif
63 #else
64 #define KBASEP_DEBUG_PRINT_TRACE ""
65 #endif
66 
67 /**
68  * KBASEP_DEBUG_ASSERT_OUT(trace, function, ...) - (Private) system printing
69  * function associated to the @ref KBASE_DEBUG_ASSERT_MSG event.
70  * @trace: location in the code from where the message is printed
71  * @function: function from where the message is printed
72  * @...: Format string followed by format arguments.
73  *
74  * @note function parameter cannot be concatenated with other strings
75  */
76 /* Select the correct system output function*/
77 #ifdef CONFIG_MALI_BIFROST_DEBUG
78 #define KBASEP_DEBUG_ASSERT_OUT(trace, function, ...)                          \
79 	do {                                                                   \
80 		pr_err("Mali<ASSERT>: %s function:%s ", trace, function);      \
81 		pr_err(__VA_ARGS__);                                           \
82 		pr_err("\n");                                                  \
83 	} while (false)
84 #else
85 #define KBASEP_DEBUG_ASSERT_OUT(trace, function, ...) CSTD_NOP()
86 #endif
87 
88 #ifdef CONFIG_MALI_BIFROST_DEBUG
89 #define KBASE_CALL_ASSERT_HOOK() kbasep_debug_assert_call_hook()
90 #else
91 #define KBASE_CALL_ASSERT_HOOK() CSTD_NOP()
92 #endif
93 
94 /**
95  * KBASE_DEBUG_ASSERT(expr) - Calls @ref KBASE_PRINT_ASSERT and prints the
96  * expression @a expr if @a expr is false
97  * @expr: Boolean expression
98  *
99  * @note This macro does nothing if the flag @ref KBASE_DEBUG_DISABLE_ASSERTS is set to 1
100  *
101  */
102 #define KBASE_DEBUG_ASSERT(expr) \
103 	KBASE_DEBUG_ASSERT_MSG(expr, #expr)
104 
105 #if KBASE_DEBUG_DISABLE_ASSERTS
106 #define KBASE_DEBUG_ASSERT_MSG(expr, ...) CSTD_NOP()
107 #else
108 /**
109  * KBASE_DEBUG_ASSERT_MSG() - Calls @ref KBASEP_DEBUG_ASSERT_OUT and prints the
110  * given message if @a expr is false
111  * @expr: Boolean expression
112  * @...:  Message to display when @a expr is false, as a format string followed
113  *        by format arguments.
114  *
115  * This macro does nothing if the flag KBASE_DEBUG_DISABLE_ASSERTS is set to 1
116  */
117 #define KBASE_DEBUG_ASSERT_MSG(expr, ...) \
118 		do { \
119 			if (!(expr)) { \
120 				KBASEP_DEBUG_ASSERT_OUT(KBASEP_DEBUG_PRINT_TRACE, KBASEP_DEBUG_PRINT_FUNCTION, __VA_ARGS__);\
121 				KBASE_CALL_ASSERT_HOOK();\
122 				BUG();\
123 			} \
124 		} while (false)
125 #endif				/* KBASE_DEBUG_DISABLE_ASSERTS */
126 
127 /**
128  * KBASE_DEBUG_CODE( X ) - Executes the code inside the macro only in debug mode
129  * @X: Code to compile only in debug mode.
130  */
131 #ifdef CONFIG_MALI_BIFROST_DEBUG
132 #define KBASE_DEBUG_CODE(X) X
133 #else
134 #define KBASE_DEBUG_CODE(X) CSTD_NOP()
135 #endif				/* CONFIG_MALI_BIFROST_DEBUG */
136 
137 /** @} */
138 
139 /**
140  * kbase_debug_assert_register_hook - Register a function to call on ASSERT
141  * @func: the function to call when an assert is triggered.
142  * @param: the parameter to pass to \a func when calling it
143  *
144  * Such functions will \b only be called during Debug mode, and for debugging
145  * features \b only. Do not rely on them to be called in general use.
146  *
147  * To disable the hook, supply NULL to \a func.
148  *
149  * @note This function is not thread-safe, and should only be used to
150  * register/deregister once in the module's lifetime.
151  *
152  */
153 void kbase_debug_assert_register_hook(kbase_debug_assert_hook *func, void *param);
154 
155 /**
156  * kbasep_debug_assert_call_hook - Call a debug assert hook previously
157  * registered with kbase_debug_assert_register_hook()
158  *
159  * @note This function is not thread-safe with respect to multiple threads
160  * registering functions and parameters with
161  * kbase_debug_assert_register_hook(). Otherwise, thread safety is the
162  * responsibility of the registered hook.
163  */
164 void kbasep_debug_assert_call_hook(void);
165 
166 #endif				/* _KBASE_DEBUG_H */
167