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