fkernel.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fkernel.h
  15. * Date: 2022-02-10 14:53:41
  16. * LastEditTime: 2022-02-17 17:35:07
  17. * Description:  This file is for kernel definition functions
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 huanghe 2021/10/20 first release
  23. * 1.1 zhugengyu 2022/2/17 add extra functionality
  24. */
  25. #ifndef FKERNEL_H
  26. #define FKERNEL_H
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. #ifdef __ASSEMBLY__
  32. #define _AC(X, Y) X
  33. #define _AT(T, X) X
  34. #else
  35. #define __AC(X, Y) (X##Y)
  36. #define _AC(X, Y) __AC(X, Y)
  37. #define _AT(T, X) ((T)(X))
  38. #endif
  39. #define _UL(x) (_AC(x, UL))
  40. #define _ULL(x) (_AC(x, ULL))
  41. #define _BITUL(x) (_UL(1) << (x))
  42. #define _BITULL(x) (_ULL(1) << (x))
  43. #define UL(x) (_UL(x))
  44. #define ULL(x) (_ULL(x))
  45. #define min(x, y) ( \
  46. { \
  47. typeof(x) _min1 = (x); \
  48. typeof(y) _min2 = (y); \
  49. (void)(&_min1 == &_min2); \
  50. _min1 < _min2 ? _min1 : _min2; \
  51. })
  52. #define max(x, y) ( \
  53. { \
  54. typeof(x) _max1 = (x); \
  55. typeof(y) _max2 = (y); \
  56. (void)(&_max1 == &_max2); \
  57. _max1 > _max2 ? _max1 : _max2; \
  58. })
  59. #define min3(x, y, z) min((typeof(x))min(x, y), z)
  60. #define max3(x, y, z) max((typeof(x))max(x, y), z)
  61. #define min_t(type, a, b) min(((type) a), ((type) b))
  62. #define max_t(type, a, b) max(((type) a), ((type) b))
  63. /**
  64. * clamp - return a value clamped to a given range with strict typechecking
  65. * @val: current value
  66. * @lo: lowest allowable value
  67. * @hi: highest allowable value
  68. *
  69. * This macro does strict typechecking of @lo/@hi to make sure they are of the
  70. * same type as @val. See the unnecessary pointer comparisons.
  71. */
  72. #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  73. /**
  74. * do_div - returns 2 values: calculate remainder and update new dividend
  75. * @n: uint64_t dividend (will be updated)
  76. * @base: uint32_t divisor
  77. *
  78. * Summary:
  79. * ``uint32_t remainder = n % base;``
  80. * ``n = n / base;``
  81. *
  82. * Return: (uint32_t)remainder
  83. *
  84. * NOTE: macro parameter @n is evaluated multiple times,
  85. * beware of side effects!
  86. */
  87. #define do_div(n, base) ( \
  88. { \
  89. uint32_t __base = (base); \
  90. uint32_t __rem; \
  91. __rem = ((uint64_t)(n)) % __base; \
  92. (n) = ((uint64_t)(n)) / __base; \
  93. __rem; \
  94. })
  95. /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
  96. #define roundup(x, y) ( \
  97. { \
  98. const typeof(y) __y = y; \
  99. ((x + (__y - 1)) / __y) * __y; \
  100. })
  101. #define rounddown(x, y) ( \
  102. { \
  103. typeof(x) __x = (x); \
  104. __x - (__x % (y)); \
  105. })
  106. #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
  107. #if defined(__aarch64__)
  108. #define BITS_PER_LONG 64
  109. #else
  110. #define BITS_PER_LONG 32
  111. #endif
  112. #ifndef BITS_PER_LONG_LONG
  113. #define BITS_PER_LONG_LONG 64
  114. #endif
  115. #define BIT(nr) (1ULL << (nr))
  116. #define BIT_ULL(nr) (1ULL << (nr))
  117. #define BIT_MASK(nr) (BIT(nr) - 1UL)
  118. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  119. #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
  120. #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
  121. #define BITS_PER_BYTE 8
  122. #define DIV_ROUND_DOWN_ULL(ll, d) \
  123. ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
  124. #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
  125. #if BITS_PER_LONG == 32
  126. #define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
  127. #else
  128. #define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
  129. #endif
  130. /*
  131. * Create a contiguous bitmask starting at bit position @l and ending at
  132. * position @h. For example
  133. * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  134. */
  135. #define GENMASK(h, l) \
  136. (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
  137. #define GENMASK_ULL(h, l) \
  138. (((~0ULL) - (1ULL << (l)) + 1) & \
  139. (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
  140. #define SZ_1 0x00000001
  141. #define SZ_2 0x00000002
  142. #define SZ_4 0x00000004
  143. #define SZ_8 0x00000008
  144. #define SZ_16 0x00000010
  145. #define SZ_32 0x00000020
  146. #define SZ_64 0x00000040
  147. #define SZ_128 0x00000080
  148. #define SZ_256 0x00000100
  149. #define SZ_512 0x00000200
  150. #define SZ_1K 0x00000400
  151. #define SZ_2K 0x00000800
  152. #define SZ_4K 0x00001000
  153. #define SZ_8K 0x00002000
  154. #define SZ_16K 0x00004000
  155. #define SZ_32K 0x00008000
  156. #define SZ_64K 0x00010000
  157. #define SZ_128K 0x00020000
  158. #define SZ_256K 0x00040000
  159. #define SZ_512K 0x00080000
  160. #define SZ_1M 0x00100000
  161. #define SZ_2M 0x00200000
  162. #define SZ_4M 0x00400000
  163. #define SZ_8M 0x00800000
  164. #define SZ_16M 0x01000000
  165. #define SZ_32M 0x02000000
  166. #define SZ_64M 0x04000000
  167. #define SZ_128M 0x08000000
  168. #define SZ_256M 0x10000000
  169. #define SZ_512M 0x20000000
  170. #define SZ_1G 0x40000000
  171. #define SZ_2G 0x80000000
  172. #define SZ_3G 0xC0000000
  173. #define SZ_4G 0x100000000ULL
  174. #define SZ_8G 0x200000000ULL
  175. #define NANO_TO_MICRO 1000
  176. #define NANO_TO_KILO 1000000
  177. /**
  178. * UPPER_32_BITS - return bits 32-63 of a number
  179. * @n: the number we're accessing
  180. *
  181. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  182. * the "right shift count >= width of type" warning when that quantity is
  183. * 32-bits.
  184. * Note that do not input signed int 'n'
  185. */
  186. #define UPPER_32_BITS(n) ((uint32_t)(((n) >> 16) >> 16))
  187. /**
  188. * LOWER_32_BITS - return bits 0-31 of a number
  189. * @n: the number we're accessing
  190. * Note that do not input signed int 'n'
  191. */
  192. #define LOWER_32_BITS(n) ((uint32_t)((n)&0xffffffff))
  193. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
  194. #ifndef __aligned
  195. #define __aligned(x) __attribute__((__aligned__(x)))
  196. #endif
  197. /**
  198. * CONTAINER_OF - return the member address of ptr, if the type of ptr is the
  199. * struct type.
  200. */
  201. #define CONTAINER_OF(ptr, type, member) \
  202. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  203. #ifndef ARRAY_SIZE
  204. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  205. #endif
  206. /* set 32-bit register [a:b] as x, where a is high bit, b is low bit, x is setting/getting value */
  207. #define GET_REG32_BITS(x, a, b) (u32)((((u32)(x)) & GENMASK(a, b)) >> b)
  208. #define SET_REG32_BITS(x, a, b) (u32)((((u32)(x)) << b) & GENMASK(a, b))
  209. /* Integer alignment down */
  210. #define PALIGN_DOWN(x,align) (x & ~(align-1))
  211. /* Integer alignment up */
  212. #define PALIGN_UP(x,align) ((x + (align-1)) & ~(align-1))
  213. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  214. #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  215. BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
  216. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  217. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  218. /**
  219. * COMILETIME_ASSERT - break build and emit msg if condition is false
  220. * @condition: a compile-time constant condition to check
  221. * @msg: a message to emit if condition is false
  222. *
  223. * In tradition of POSIX assert, this macro will break the build if the
  224. * supplied condition is *false*, emitting the supplied error message if the
  225. * compiler has support to do so.
  226. */
  227. # define COMILETIME_ASSERT(condition, msg, prefix, suffix) \
  228. do { \
  229. extern void prefix ## suffix(void) __attribute__((error(msg))); \
  230. if (!(condition)) \
  231. prefix ## suffix(); \
  232. } while (0)
  233. /**
  234. * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
  235. * error message.
  236. * @condition: the condition which the compiler should know is false.
  237. *
  238. * See BUILD_BUG_ON for description.
  239. */
  240. #define BUILD_BUG_ON_MSG(cond, msg) COMILETIME_ASSERT(!(cond), msg, __compiletime_assert_, __COUNTER__)
  241. /*
  242. * Bitfield access macros
  243. *
  244. * FIELD_{GET,PREP} macros take as first parameter shifted mask
  245. * from which they extract the base mask and shift amount.
  246. * Mask must be a compilation time constant.
  247. *
  248. * Example:
  249. *
  250. * #define REG_FIELD_A GENMASK(6, 0)
  251. * #define REG_FIELD_B BIT(7)
  252. * #define REG_FIELD_C GENMASK(15, 8)
  253. * #define REG_FIELD_D GENMASK(31, 16)
  254. *
  255. * Get:
  256. * a = FIELD_GET(REG_FIELD_A, reg);
  257. * b = FIELD_GET(REG_FIELD_B, reg);
  258. *
  259. * Set:
  260. * reg = FIELD_PREP(REG_FIELD_A, 1) |
  261. * FIELD_PREP(REG_FIELD_B, 0) |
  262. * FIELD_PREP(REG_FIELD_C, c) |
  263. * FIELD_PREP(REG_FIELD_D, 0x40);
  264. *
  265. * Modify:
  266. * reg &= ~REG_FIELD_C;
  267. * reg |= FIELD_PREP(REG_FIELD_C, c);
  268. */
  269. #define BF_SHF(x) (__builtin_ffsll(x) - 1)
  270. #define BF_FIELD_CHECK(mask, reg, val, pfx) \
  271. ({ \
  272. BUILD_BUG_ON_MSG(!__builtin_constant_p(mask), \
  273. pfx "mask is not constant"); \
  274. BUILD_BUG_ON_MSG((mask) == 0, pfx "mask is zero"); \
  275. BUILD_BUG_ON_MSG(__builtin_constant_p(val) ? \
  276. ~((mask) >> BF_SHF(mask)) & (val) : 0, \
  277. pfx "value too large for the field"); \
  278. BUILD_BUG_ON_MSG((mask) > (typeof(reg))~0ull, \
  279. pfx "type of reg too small for mask"); \
  280. __BUILD_BUG_ON_NOT_POWER_OF_2((mask) + \
  281. (1ULL << BF_SHF(mask))); \
  282. })
  283. /**
  284. * FIELD_PREP() - prepare a bitfield element
  285. * @mask: shifted mask defining the field's length and position
  286. * @val: value to put in the field
  287. *
  288. * FIELD_PREP() masks and shifts up the value. The result should
  289. * be combined with other fields of the bitfield using logical OR.
  290. */
  291. #define FIELD_PREP(mask, val) \
  292. ({ \
  293. BF_FIELD_CHECK(mask, 0ULL, val, "FIELD_PREP: "); \
  294. ((typeof(mask))(val) << BF_SHF(mask)) & (mask); \
  295. })
  296. #ifdef __cplusplus
  297. }
  298. #endif
  299. #endif