tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / r4p0_rel0 / common / mali_kernel_common.h
1 /*
2  * Copyright (C) 2010, 2012 ARM Limited. All rights reserved.
3  * 
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  * 
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 #ifndef __MALI_KERNEL_COMMON_H__
12 #define __MALI_KERNEL_COMMON_H__
13
14 #include "mali_osk.h"
15
16 /* Make sure debug is defined when it should be */
17 #ifndef DEBUG
18 #if defined(_DEBUG)
19 #define DEBUG
20 #endif
21 #endif
22
23 /* MALI_SEC */
24 /* Macro for generating a kernel panic.
25  * Turned on off by compile-time Makefile settings
26  */
27 #if defined(USING_KERNEL_PANIC)
28 #include <linux/kernel.h>
29         #define MALI_PANIC(fmt, args...) panic( fmt, ## args );
30 #else
31         #define MALI_PANIC(fmt, args...) 
32 #endif
33
34 /* The file include several useful macros for error checking, debugging and printing.
35  * - MALI_PRINTF(...)           Do not use this function: Will be included in Release builds.
36  * - MALI_DEBUG_PRINT(nr, (X) ) Prints the second argument if nr<=MALI_DEBUG_LEVEL.
37  * - MALI_DEBUG_ERROR( (X) )    Prints an errortext, a source trace, and the given error message.
38  * - MALI_DEBUG_ASSERT(exp,(X)) If the asserted expr is false, the program will exit.
39  * - MALI_DEBUG_ASSERT_POINTER(pointer)  Triggers if the pointer is a zero pointer.
40  * - MALI_DEBUG_CODE( X )       The code inside the macro is only compiled in Debug builds.
41  *
42  * The (X) means that you must add an extra parenthesis around the argumentlist.
43  *
44  * The  printf function: MALI_PRINTF(...) is routed to _mali_osk_debugmsg
45  *
46  * Suggested range for the DEBUG-LEVEL is [1:6] where
47  * [1:2] Is messages with highest priority, indicate possible errors.
48  * [3:4] Is messages with medium priority, output important variables.
49  * [5:6] Is messages with low priority, used during extensive debugging.
50  */
51
52 /**
53 *  Fundamental error macro. Reports an error code. This is abstracted to allow us to
54 *  easily switch to a different error reporting method if we want, and also to allow
55 *  us to search for error returns easily.
56 *
57 *  Note no closing semicolon - this is supplied in typical usage:
58 *
59 *  MALI_ERROR(MALI_ERROR_OUT_OF_MEMORY);
60 */
61 #define MALI_ERROR(error_code) return (error_code)
62
63 /**
64  *  Basic error macro, to indicate success.
65  *  Note no closing semicolon - this is supplied in typical usage:
66  *
67  *  MALI_SUCCESS;
68  */
69 #define MALI_SUCCESS MALI_ERROR(_MALI_OSK_ERR_OK)
70
71 /**
72  *      Basic error macro. This checks whether the given condition is true, and if not returns
73  *      from this function with the supplied error code. This is a macro so that we can override it
74  *      for stress testing.
75  *
76  *      Note that this uses the do-while-0 wrapping to ensure that we don't get problems with dangling
77  *      else clauses. Note also no closing semicolon - this is supplied in typical usage:
78  *
79  *      MALI_CHECK((p!=NULL), ERROR_NO_OBJECT);
80  */
81 #define MALI_CHECK(condition, error_code) do { if(!(condition)) MALI_ERROR(error_code); } while(0)
82
83 /**
84  *      Error propagation macro. If the expression given is anything other than _MALI_OSK_NO_ERROR,
85  *      then the value is returned from the enclosing function as an error code. This effectively
86  *      acts as a guard clause, and propagates error values up the call stack. This uses a
87  *      temporary value to ensure that the error expression is not evaluated twice.
88  *  If the counter for forcing a failure has been set using _mali_force_error, this error will be
89  *  returned without evaluating the expression in MALI_CHECK_NO_ERROR
90  */
91 #define MALI_CHECK_NO_ERROR(expression) \
92     do { _mali_osk_errcode_t _check_no_error_result=(expression); \
93          if(_check_no_error_result != _MALI_OSK_ERR_OK) \
94          MALI_ERROR(_check_no_error_result); \
95     } while(0)
96
97 /**
98  *  Pointer check macro. Checks non-null pointer.
99  */
100 #define MALI_CHECK_NON_NULL(pointer, error_code) MALI_CHECK( ((pointer)!=NULL), (error_code) )
101
102 /**
103  *      Error macro with goto. This checks whether the given condition is true, and if not jumps
104  *      to the specified label using a goto. The label must therefore be local to the function in
105  *      which this macro appears. This is most usually used to execute some clean-up code before
106  *      exiting with a call to ERROR.
107  *
108  *      Like the other macros, this is a macro to allow us to override the condition if we wish,
109  *      e.g. to force an error during stress testing.
110  */
111 #define MALI_CHECK_GOTO(condition, label) do { if(!(condition)) goto label; } while(0)
112
113 /**
114  *  Explicitly ignore a parameter passed into a function, to suppress compiler warnings.
115  *  Should only be used with parameter names.
116  */
117 #define MALI_IGNORE(x) x=x
118
119 #define MALI_PRINTF(args) _mali_osk_dbgmsg args;
120
121 #define MALI_PRINT_ERROR(args) do{ \
122         MALI_PRINTF(("Mali: ERR: %s\n" ,__FILE__)); \
123         MALI_PRINTF(("           %s()%4d\n           ", __FUNCTION__, __LINE__)) ; \
124         MALI_PRINTF(args); \
125         MALI_PRINTF(("\n")); \
126         } while(0)
127
128 #define MALI_PRINT(args) do{ \
129         MALI_PRINTF(("Mali: ")); \
130         MALI_PRINTF(args); \
131         } while (0)
132
133 #ifdef DEBUG
134 #ifndef mali_debug_level
135 extern int mali_debug_level;
136 #endif
137
138 #define MALI_DEBUG_CODE(code) code
139 #define MALI_DEBUG_PRINT(level, args)  do { \
140         if((level) <=  mali_debug_level)\
141         {MALI_PRINTF(("Mali<" #level ">: ")); MALI_PRINTF(args); } \
142         } while (0)
143
144 #define MALI_DEBUG_PRINT_ERROR(args) MALI_PRINT_ERROR(args)
145
146 #define MALI_DEBUG_PRINT_IF(level,condition,args)  \
147         if((condition)&&((level) <=  mali_debug_level))\
148         {MALI_PRINTF(("Mali<" #level ">: ")); MALI_PRINTF(args); }
149
150 #define MALI_DEBUG_PRINT_ELSE(level, args)\
151         else if((level) <=  mali_debug_level)\
152     { MALI_PRINTF(("Mali<" #level ">: ")); MALI_PRINTF(args); }
153
154 /**
155  * @note these variants of DEBUG ASSERTS will cause a debugger breakpoint
156  * to be entered (see _mali_osk_break() ). An alternative would be to call
157  * _mali_osk_abort(), on OSs that support it.
158  */
159 #define MALI_DEBUG_PRINT_ASSERT(condition, args) do  {if( !(condition)) { MALI_PRINT_ERROR(args); _mali_osk_break(); } } while(0)
160 #define MALI_DEBUG_ASSERT_POINTER(pointer) do  {if( (pointer)== NULL) {MALI_PRINT_ERROR(("NULL pointer " #pointer)); _mali_osk_break();} } while(0)
161 #define MALI_DEBUG_ASSERT(condition) do  {if( !(condition)) {MALI_PRINT_ERROR(("ASSERT failed: " #condition )); _mali_osk_break();} } while(0)
162
163 #else /* DEBUG */
164
165 #define MALI_DEBUG_CODE(code)
166 #define MALI_DEBUG_PRINT(string,args) do {} while(0)
167 #define MALI_DEBUG_PRINT_ERROR(args) do {} while(0)
168 #define MALI_DEBUG_PRINT_IF(level,condition,args) do {} while(0)
169 #define MALI_DEBUG_PRINT_ELSE(level,condition,args) do {} while(0)
170 #define MALI_DEBUG_PRINT_ASSERT(condition,args) do {} while(0)
171 #define MALI_DEBUG_ASSERT_POINTER(pointer) do {} while(0)
172 #define MALI_DEBUG_ASSERT(condition) do {} while(0)
173
174 #endif /* DEBUG */
175
176 /**
177  * variables from user space cannot be dereferenced from kernel space; tagging them
178  * with __user allows the GCC compiler to generate a warning. Other compilers may
179  * not support this so we define it here as an empty macro if the compiler doesn't
180  * define it.
181  */
182 #ifndef __user
183 #define __user
184 #endif
185
186 #endif /* __MALI_KERNEL_COMMON_H__ */