support.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: support.h
  5. *
  6. * Description: misc utilities definition.
  7. *
  8. * Version: 2.0
  9. * Create: 2017-11-03 11:34:34
  10. * Revision: none
  11. * Compiler: gcc version 6.3.0 (crosstool-NG crosstool-ng-1.23.0)
  12. *
  13. * Author: caozilong@allwinnertech.com
  14. * Organization: BU1-PSW
  15. * Last Modified: 2020-03-25 12:20:13
  16. *
  17. * =====================================================================================
  18. */
  19. #ifndef __SUPPORT_H__
  20. #define __SUPPORT_H__
  21. #include <typedef.h>
  22. #include <kapi.h>
  23. #include <libc.h>
  24. /*
  25. * Generic macro to convert pointers to values for comparison purposes.
  26. */
  27. #ifndef p2n
  28. #define p2n(p) ((ptrdiff_t)((ptrdiff_t*)(p)))
  29. #endif
  30. /*
  31. * min()/max() macros that also do
  32. * strict type-checking.. See the
  33. * "unnecessary" pointer comparison.
  34. */
  35. #ifndef min
  36. #define min(x,y) ({ \
  37. typeof(x) _x = (x); \
  38. typeof(y) _y = (y); \
  39. (void) (&_x == &_y); \
  40. _x < _y ? _x : _y; })
  41. #endif
  42. #ifndef max
  43. #define max(x,y) ({ \
  44. typeof(x) _x = (x); \
  45. typeof(y) _y = (y); \
  46. (void) (&_x == &_y); \
  47. _x > _y ? _x : _y; })
  48. #endif
  49. /*
  50. * ..and if you can't take the strict
  51. * types, you can specify one yourself.
  52. *
  53. * Or not use min/max at all, of course.
  54. */
  55. #define min_t(type,x,y) \
  56. ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
  57. #define max_t(type,x,y) \
  58. ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
  59. //#define BITS_PER_LONG 32
  60. //#define BITS_PER_LONG_LONG 64
  61. #ifndef ALIGN
  62. #define ALIGN(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
  63. #endif
  64. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  65. #ifndef INT_MAX
  66. #define INT_MAX ((int)(~0U>>1))
  67. #endif
  68. #ifndef INT_MIN
  69. #define INT_MIN (-INT_MAX - 1)
  70. #endif
  71. #ifndef UINT_MAX
  72. #define UINT_MAX (~0U)
  73. #endif
  74. #ifndef LONG_MAX
  75. #define LONG_MAX ((long)(~0UL>>1))
  76. #endif
  77. #ifndef LONG_MIN
  78. #define LONG_MIN (-LONG_MAX - 1)
  79. #endif
  80. #ifndef ULONG_MAX
  81. #define ULONG_MAX (~0UL)
  82. #endif
  83. #ifndef LLONG_MAX
  84. #define LLONG_MAX ((long long)(~0ULL>>1))
  85. #endif
  86. #ifndef LLONG_MIN
  87. #define LLONG_MIN (-LLONG_MAX - 1)
  88. #endif
  89. #ifndef ULLONG_MAX
  90. #define ULLONG_MAX (~0ULL)
  91. #endif
  92. #ifndef DATA_TYPE_X_BOOL
  93. #define DATA_TYPE_X_BOOL
  94. typedef enum
  95. {
  96. #ifndef FALSE
  97. FALSE = 0,
  98. #endif
  99. #ifndef NO
  100. NO = 0,
  101. #endif
  102. #ifndef ZERO
  103. ZERO = 0,
  104. #endif
  105. #ifndef TRUE
  106. TRUE = 1,
  107. #endif
  108. #ifndef YES
  109. YES = 1,
  110. #endif
  111. #ifndef ONE
  112. ONE = 1,
  113. #endif
  114. #ifndef OK
  115. OK = 0,
  116. #endif
  117. #ifndef FAIL
  118. FAIL = -1,
  119. #endif
  120. } BOOL;
  121. #endif
  122. /*
  123. * Check at compile time that something is of a particular type.
  124. * Always evaluates to 1 so you may use it easily in comparisons.
  125. */
  126. #define typecheck(type,x) \
  127. ({ type __dummy; \
  128. typeof(x) __dummy2; \
  129. (void)(&__dummy == &__dummy2); \
  130. 1; \
  131. })
  132. static inline int is_power_of_2(unsigned long n)
  133. {
  134. return (n != 0 && ((n & (n - 1)) == 0));
  135. }
  136. /* round "x" up/down to next multiple of "align" (which must be a power of 2) */
  137. #define ROUND_UP(x, align) \
  138. (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
  139. ~((unsigned long)(align) - 1))
  140. #define ROUND_DOWN(x, align) \
  141. ((unsigned long)(x) & ~((unsigned long)(align) - 1))
  142. //#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  143. /**
  144. * test_bit - Determine whether a bit is set
  145. * @nr: bit number to test
  146. * @addr: Address to start counting from
  147. */
  148. static inline int test_bit(int nr, long *addr)
  149. {
  150. int mask;
  151. addr += nr >> 5;
  152. mask = 1 << (nr & 0x1f);
  153. return ((mask & *addr) != 0);
  154. }
  155. /*
  156. * These functions are the basis of our bit ops.
  157. *
  158. * First, the atomic bitops. These use native endian.
  159. */
  160. static inline void set_bit(unsigned int bit, volatile unsigned long *p)
  161. {
  162. unsigned long flags;
  163. unsigned long mask = 1UL << (bit & 31);
  164. p += bit >> 5;
  165. ENTER_CRITICAL(flags);
  166. *p |= mask;
  167. EXIT_CRITICAL(flags);
  168. }
  169. static inline void clear_bit(unsigned int bit, volatile unsigned long *p)
  170. {
  171. unsigned long flags;
  172. unsigned long mask = 1UL << (bit & 31);
  173. p += bit >> 5;
  174. ENTER_CRITICAL(flags);
  175. *p &= ~mask;
  176. EXIT_CRITICAL(flags);
  177. }
  178. static inline void change_bit(unsigned int bit, volatile unsigned long *p)
  179. {
  180. unsigned long flags;
  181. unsigned long mask = 1UL << (bit & 31);
  182. p += bit >> 5;
  183. ENTER_CRITICAL(flags);
  184. *p ^= mask;
  185. EXIT_CRITICAL(flags);
  186. }
  187. static inline int test_and_set_bit(unsigned int bit, volatile unsigned long *p)
  188. {
  189. unsigned long flags;
  190. unsigned int res;
  191. unsigned long mask = 1UL << (bit & 31);
  192. p += bit >> 5;
  193. ENTER_CRITICAL(flags);
  194. res = *p;
  195. *p = res | mask;
  196. EXIT_CRITICAL(flags);
  197. return res & mask;
  198. }
  199. static inline int test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
  200. {
  201. unsigned long flags;
  202. unsigned int res;
  203. unsigned long mask = 1UL << (bit & 31);
  204. p += bit >> 5;
  205. ENTER_CRITICAL(flags);
  206. res = *p;
  207. *p = res & ~mask;
  208. EXIT_CRITICAL(flags);
  209. return res & mask;
  210. }
  211. static inline int test_and_change_bit(unsigned int bit, volatile unsigned long *p)
  212. {
  213. unsigned long flags;
  214. unsigned int res;
  215. unsigned long mask = 1UL << (bit & 31);
  216. p += bit >> 5;
  217. ENTER_CRITICAL(flags);
  218. res = *p;
  219. *p = res ^ mask;
  220. EXIT_CRITICAL(flags);
  221. return res & mask;
  222. }
  223. /* -------------------------------- jiffies -----------------------------*/
  224. #define HZ 100
  225. #define jiffies ((unsigned long)rt_tick_get())
  226. /*
  227. * These inlines deal with timer wrapping correctly. You are
  228. * strongly encouraged to use them
  229. * 1. Because people otherwise forget
  230. * 2. Because if the timer wrap changes in future you won't have to
  231. * alter your driver code.
  232. *
  233. * time_after(a,b) returns true if the time a is after time b.
  234. *
  235. * Do this with "<0" and ">=0" to only test the sign of the result. A
  236. * good compiler would generate better code (and a really good compiler
  237. * wouldn't care). Gcc is currently neither.
  238. */
  239. #define time_after(a,b) \
  240. (typecheck(unsigned long, a) && \
  241. typecheck(unsigned long, b) && \
  242. ((int)(b) - (int)(a) < 0))
  243. #define time_before(a,b) time_after(b,a)
  244. #define time_after_eq(a,b) \
  245. (typecheck(unsigned long, a) && \
  246. typecheck(unsigned long, b) && \
  247. ((int)(a) - (int)(b) >= 0))
  248. #define time_before_eq(a,b) time_after_eq(b,a)
  249. #endif /* __SUPPORT_H__ */