fkernel.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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: kernel.h
  15. * Date: 2022-02-10 14:53:41
  16. * LastEditTime: 2022-02-17 17:35:07
  17. * Description:  This files is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #ifndef KERNEL_H
  24. #define KERNEL_H
  25. #ifdef __ASSEMBLY__
  26. #define _AC(X, Y) X
  27. #define _AT(T, X) X
  28. #else
  29. #define __AC(X, Y) (X##Y)
  30. #define _AC(X, Y) __AC(X, Y)
  31. #define _AT(T, X) ((T)(X))
  32. #endif
  33. #define _UL(x) (_AC(x, UL))
  34. #define _ULL(x) (_AC(x, ULL))
  35. #define _BITUL(x) (_UL(1) << (x))
  36. #define _BITULL(x) (_ULL(1) << (x))
  37. #define UL(x) (_UL(x))
  38. #define ULL(x) (_ULL(x))
  39. #define min(x, y) ( \
  40. { \
  41. typeof(x) _min1 = (x); \
  42. typeof(y) _min2 = (y); \
  43. (void)(&_min1 == &_min2); \
  44. _min1 < _min2 ? _min1 : _min2; \
  45. })
  46. #define max(x, y) ( \
  47. { \
  48. typeof(x) _max1 = (x); \
  49. typeof(y) _max2 = (y); \
  50. (void)(&_max1 == &_max2); \
  51. _max1 > _max2 ? _max1 : _max2; \
  52. })
  53. #define min3(x, y, z) min((typeof(x))min(x, y), z)
  54. #define max3(x, y, z) max((typeof(x))max(x, y), z)
  55. /**
  56. * clamp - return a value clamped to a given range with strict typechecking
  57. * @val: current value
  58. * @lo: lowest allowable value
  59. * @hi: highest allowable value
  60. *
  61. * This macro does strict typechecking of @lo/@hi to make sure they are of the
  62. * same type as @val. See the unnecessary pointer comparisons.
  63. */
  64. #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  65. /**
  66. * do_div - returns 2 values: calculate remainder and update new dividend
  67. * @n: uint64_t dividend (will be updated)
  68. * @base: uint32_t divisor
  69. *
  70. * Summary:
  71. * ``uint32_t remainder = n % base;``
  72. * ``n = n / base;``
  73. *
  74. * Return: (uint32_t)remainder
  75. *
  76. * NOTE: macro parameter @n is evaluated multiple times,
  77. * beware of side effects!
  78. */
  79. #define do_div(n, base) ( \
  80. { \
  81. uint32_t __base = (base); \
  82. uint32_t __rem; \
  83. __rem = ((uint64_t)(n)) % __base; \
  84. (n) = ((uint64_t)(n)) / __base; \
  85. __rem; \
  86. })
  87. /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
  88. #define roundup(x, y) ( \
  89. { \
  90. const typeof(y) __y = y; \
  91. ((x + (__y - 1)) / __y) * __y; \
  92. })
  93. #define rounddown(x, y) ( \
  94. { \
  95. typeof(x) __x = (x); \
  96. __x - (__x % (y)); \
  97. })
  98. #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
  99. #if defined(__aarch64__)
  100. #define BITS_PER_LONG 64
  101. #else
  102. #define BITS_PER_LONG 32
  103. #endif
  104. #ifndef BITS_PER_LONG_LONG
  105. #define BITS_PER_LONG_LONG 64
  106. #endif
  107. #define BIT(nr) (1ULL << (nr))
  108. #define BIT_ULL(nr) (1ULL << (nr))
  109. #define BIT_MASK(nr) (BIT(nr) - 1UL)
  110. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  111. #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
  112. #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
  113. #define BITS_PER_BYTE 8
  114. #define DIV_ROUND_DOWN_ULL(ll, d) \
  115. ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
  116. #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
  117. #if BITS_PER_LONG == 32
  118. #define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
  119. #else
  120. #define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
  121. #endif
  122. /*
  123. * Create a contiguous bitmask starting at bit position @l and ending at
  124. * position @h. For example
  125. * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  126. */
  127. #define GENMASK(h, l) \
  128. (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
  129. #define GENMASK_ULL(h, l) \
  130. (((~0ULL) - (1ULL << (l)) + 1) & \
  131. (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
  132. #define SZ_1 0x00000001
  133. #define SZ_2 0x00000002
  134. #define SZ_4 0x00000004
  135. #define SZ_8 0x00000008
  136. #define SZ_16 0x00000010
  137. #define SZ_32 0x00000020
  138. #define SZ_64 0x00000040
  139. #define SZ_128 0x00000080
  140. #define SZ_256 0x00000100
  141. #define SZ_512 0x00000200
  142. #define SZ_1K 0x00000400
  143. #define SZ_2K 0x00000800
  144. #define SZ_4K 0x00001000
  145. #define SZ_8K 0x00002000
  146. #define SZ_16K 0x00004000
  147. #define SZ_32K 0x00008000
  148. #define SZ_64K 0x00010000
  149. #define SZ_128K 0x00020000
  150. #define SZ_256K 0x00040000
  151. #define SZ_512K 0x00080000
  152. #define SZ_1M 0x00100000
  153. #define SZ_2M 0x00200000
  154. #define SZ_4M 0x00400000
  155. #define SZ_8M 0x00800000
  156. #define SZ_16M 0x01000000
  157. #define SZ_32M 0x02000000
  158. #define SZ_64M 0x04000000
  159. #define SZ_128M 0x08000000
  160. #define SZ_256M 0x10000000
  161. #define SZ_512M 0x20000000
  162. #define SZ_1G 0x40000000
  163. #define SZ_2G 0x80000000
  164. #define SZ_3G 0xC0000000
  165. #define SZ_4G 0x100000000ULL
  166. #define SZ_8G 0x200000000ULL
  167. #define NANO_TO_MICRO 1000
  168. #define NANO_TO_KILO 1000000
  169. /**
  170. * UPPER_32_BITS - return bits 32-63 of a number
  171. * @n: the number we're accessing
  172. *
  173. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  174. * the "right shift count >= width of type" warning when that quantity is
  175. * 32-bits.
  176. * Note that do not input signed int 'n'
  177. */
  178. #define UPPER_32_BITS(n) ((uint32_t)(((n) >> 16) >> 16))
  179. /**
  180. * LOWER_32_BITS - return bits 0-31 of a number
  181. * @n: the number we're accessing
  182. * Note that do not input signed int 'n'
  183. */
  184. #define LOWER_32_BITS(n) ((uint32_t)((n)&0xffffffff))
  185. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a)-1)) == 0)
  186. #ifndef __aligned
  187. #define __aligned(x) __attribute__((__aligned__(x)))
  188. #endif
  189. /**
  190. * CONTAINER_OF - return the member address of ptr, if the type of ptr is the
  191. * struct type.
  192. */
  193. #define CONTAINER_OF(ptr, type, member) \
  194. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  195. #ifndef ARRAY_SIZE
  196. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  197. #endif
  198. /* set 32-bit register [a:b] as x, where a is high bit, b is low bit, x is setting/getting value */
  199. #define GET_REG32_BITS(x, a, b) (u32)((((u32)(x)) & GENMASK(a, b)) >> b)
  200. #define SET_REG32_BITS(x, a, b) (u32)((((u32)(x)) << b) & GENMASK(a, b))
  201. /* Integer alignment down */
  202. #define PALIGN_DOWN(x,align) (x & ~(align-1))
  203. /* Integer alignment up */
  204. #define PALIGN_UP(x,align) ((x + (align-1)) & ~(align-1))
  205. #endif