1 /*
2  *
3  * (C) COPYRIGHT 2014-2015 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15 
16 
17 
18 /**
19  * Kernel-wide include for common macros and types.
20  */
21 
22 #ifndef _MALISW_H_
23 #define _MALISW_H_
24 
25 #include <linux/version.h>
26 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
27 #define U8_MAX          ((u8)~0U)
28 #define S8_MAX          ((s8)(U8_MAX>>1))
29 #define S8_MIN          ((s8)(-S8_MAX - 1))
30 #define U16_MAX         ((u16)~0U)
31 #define S16_MAX         ((s16)(U16_MAX>>1))
32 #define S16_MIN         ((s16)(-S16_MAX - 1))
33 #define U32_MAX         ((u32)~0U)
34 #define S32_MAX         ((s32)(U32_MAX>>1))
35 #define S32_MIN         ((s32)(-S32_MAX - 1))
36 #define U64_MAX         ((u64)~0ULL)
37 #define S64_MAX         ((s64)(U64_MAX>>1))
38 #define S64_MIN         ((s64)(-S64_MAX - 1))
39 #endif /* LINUX_VERSION_CODE */
40 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0)
41 #define SIZE_MAX        (~(size_t)0)
42 #endif /* LINUX_VERSION_CODE */
43 
44 /**
45  * MIN - Return the lesser of two values.
46  *
47  * As a macro it may evaluate its arguments more than once.
48  * Refer to MAX macro for more details
49  */
50 #define MIN(x, y)	((x) < (y) ? (x) : (y))
51 
52 /**
53  * MAX -  Return the greater of two values.
54  *
55  * As a macro it may evaluate its arguments more than once.
56  * If called on the same two arguments as MIN it is guaranteed to return
57  * the one that MIN didn't return. This is significant for types where not
58  * all values are comparable e.g. NaNs in floating-point types. But if you want
59  * to retrieve the min and max of two values, consider using a conditional swap
60  * instead.
61  */
62 #define MAX(x, y)	((x) < (y) ? (y) : (x))
63 
64 /**
65  * @hideinitializer
66  * Function-like macro for suppressing unused variable warnings. Where possible
67  * such variables should be removed; this macro is present for cases where we
68  * much support API backwards compatibility.
69  */
70 #define CSTD_UNUSED(x)	((void)(x))
71 
72 /**
73  * @hideinitializer
74  * Function-like macro for use where "no behavior" is desired. This is useful
75  * when compile time macros turn a function-like macro in to a no-op, but
76  * where having no statement is otherwise invalid.
77  */
78 #define CSTD_NOP(...)	((void)#__VA_ARGS__)
79 
80 /**
81  * Function-like macro for converting a pointer in to a u64 for storing into
82  * an external data structure. This is commonly used when pairing a 32-bit
83  * CPU with a 64-bit peripheral, such as a Midgard GPU. C's type promotion
84  * is complex and a straight cast does not work reliably as pointers are
85  * often considered as signed.
86  */
87 #define PTR_TO_U64(x)	((uint64_t)((uintptr_t)(x)))
88 
89 /**
90  * @hideinitializer
91  * Function-like macro for stringizing a single level macro.
92  * @code
93  * #define MY_MACRO 32
94  * CSTD_STR1( MY_MACRO )
95  * > "MY_MACRO"
96  * @endcode
97  */
98 #define CSTD_STR1(x)	#x
99 
100 /**
101  * @hideinitializer
102  * Function-like macro for stringizing a macro's value. This should not be used
103  * if the macro is defined in a way which may have no value; use the
104  * alternative @c CSTD_STR2N macro should be used instead.
105  * @code
106  * #define MY_MACRO 32
107  * CSTD_STR2( MY_MACRO )
108  * > "32"
109  * @endcode
110  */
111 #define CSTD_STR2(x)	CSTD_STR1(x)
112 
113 /**
114  * Specify an assertion value which is evaluated at compile time. Recommended
115  * usage is specification of a @c static @c INLINE function containing all of
116  * the assertions thus:
117  *
118  * @code
119  * static INLINE [module]_compile_time_assertions( void )
120  * {
121  *     COMPILE_TIME_ASSERT( sizeof(uintptr_t) == sizeof(intptr_t) );
122  * }
123  * @endcode
124  *
125  * @note Use @c static not @c STATIC. We never want to turn off this @c static
126  * specification for testing purposes.
127  */
128 #define CSTD_COMPILE_TIME_ASSERT(expr) \
129 	do { switch (0) { case 0: case (expr):; } } while (false)
130 
131 #endif /* _MALISW_H_ */
132