utils_def.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef UTILS_DEF_H
  8. #define UTILS_DEF_H
  9. #include <export/lib/utils_def_exp.h>
  10. /* Compute the number of elements in the given array */
  11. #define ARRAY_SIZE(a) \
  12. (sizeof(a) / sizeof((a)[0]))
  13. #define IS_POWER_OF_TWO(x) \
  14. (((x) & ((x) - 1)) == 0)
  15. #define SIZE_FROM_LOG2_WORDS(n) (U(4) << (n))
  16. #if defined(__LINKER__) || defined(__ASSEMBLER__)
  17. #define BIT_32(nr) (U(1) << (nr))
  18. #define BIT_64(nr) (ULL(1) << (nr))
  19. #else
  20. #define BIT_32(nr) (((uint32_t)(1U)) << (nr))
  21. #define BIT_64(nr) (((uint64_t)(1ULL)) << (nr))
  22. #endif
  23. #ifdef __aarch64__
  24. #define BIT BIT_64
  25. #else
  26. #define BIT BIT_32
  27. #endif
  28. /*
  29. * Create a contiguous bitmask starting at bit position @low and ending at
  30. * position @high. For example
  31. * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  32. */
  33. #if defined(__LINKER__) || defined(__ASSEMBLER__)
  34. #define GENMASK_32(high, low) \
  35. (((0xFFFFFFFF) << (low)) & (0xFFFFFFFF >> (32 - 1 - (high))))
  36. #define GENMASK_64(high, low) \
  37. ((~0 << (low)) & (~0 >> (64 - 1 - (high))))
  38. #else
  39. #define GENMASK_32(high, low) \
  40. ((~UINT32_C(0) >> (32U - 1U - (high))) ^ ((BIT_32(low) - 1U)))
  41. #define GENMASK_64(high, low) \
  42. ((~UINT64_C(0) >> (64U - 1U - (high))) ^ ((BIT_64(low) - 1U)))
  43. #endif
  44. #ifdef __aarch64__
  45. #define GENMASK GENMASK_64
  46. #else
  47. #define GENMASK GENMASK_32
  48. #endif
  49. #define HI(addr) (addr >> 32)
  50. #define LO(addr) (addr & 0xffffffff)
  51. /*
  52. * This variant of div_round_up can be used in macro definition but should not
  53. * be used in C code as the `div` parameter is evaluated twice.
  54. */
  55. #define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d))
  56. #define div_round_up(val, div) __extension__ ({ \
  57. __typeof__(div) _div = (div); \
  58. ((val) + _div - (__typeof__(div)) 1) / _div; \
  59. })
  60. #define MIN(x, y) __extension__ ({ \
  61. __typeof__(x) _x = (x); \
  62. __typeof__(y) _y = (y); \
  63. (void)(&_x == &_y); \
  64. (_x < _y) ? _x : _y; \
  65. })
  66. #define MAX(x, y) __extension__ ({ \
  67. __typeof__(x) _x = (x); \
  68. __typeof__(y) _y = (y); \
  69. (void)(&_x == &_y); \
  70. (_x > _y) ? _x : _y; \
  71. })
  72. #define CLAMP(x, min, max) __extension__ ({ \
  73. __typeof__(x) _x = (x); \
  74. __typeof__(min) _min = (min); \
  75. __typeof__(max) _max = (max); \
  76. (void)(&_x == &_min); \
  77. (void)(&_x == &_max); \
  78. ((_x > _max) ? _max : ((_x < _min) ? _min : _x)); \
  79. })
  80. /*
  81. * The round_up() macro rounds up a value to the given boundary in a
  82. * type-agnostic yet type-safe manner. The boundary must be a power of two.
  83. * In other words, it computes the smallest multiple of boundary which is
  84. * greater than or equal to value.
  85. *
  86. * round_down() is similar but rounds the value down instead.
  87. */
  88. #define round_boundary(value, boundary) \
  89. ((__typeof__(value))((boundary) - 1))
  90. #define round_up(value, boundary) \
  91. ((((value) - 1) | round_boundary(value, boundary)) + 1)
  92. #define round_down(value, boundary) \
  93. ((value) & ~round_boundary(value, boundary))
  94. /* add operation together with checking whether the operation overflowed
  95. * The result is '*res',
  96. * return 0 on success and 1 on overflow
  97. */
  98. #define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
  99. /*
  100. * Round up a value to align with a given size and
  101. * check whether overflow happens.
  102. * The rounduped value is '*res',
  103. * return 0 on success and 1 on overflow
  104. */
  105. #define round_up_overflow(v, size, res) (__extension__({ \
  106. typeof(res) __res = res; \
  107. typeof(*(__res)) __roundup_tmp = 0; \
  108. typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
  109. \
  110. add_overflow((v), __roundup_mask, &__roundup_tmp) ? 1 : \
  111. (void)(*(__res) = __roundup_tmp & ~__roundup_mask), 0; \
  112. }))
  113. /*
  114. * Add a with b, then round up the result to align with a given size and
  115. * check whether overflow happens.
  116. * The rounduped value is '*res',
  117. * return 0 on success and 1 on overflow
  118. */
  119. #define add_with_round_up_overflow(a, b, size, res) (__extension__({ \
  120. typeof(a) __a = (a); \
  121. typeof(__a) __add_res = 0; \
  122. \
  123. add_overflow((__a), (b), &__add_res) ? 1 : \
  124. round_up_overflow(__add_res, (size), (res)) ? 1 : 0; \
  125. }))
  126. /**
  127. * Helper macro to ensure a value lies on a given boundary.
  128. */
  129. #define is_aligned(value, boundary) \
  130. (round_up((uintptr_t) value, boundary) == \
  131. round_down((uintptr_t) value, boundary))
  132. /*
  133. * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
  134. * Both arguments must be unsigned pointer values (i.e. uintptr_t).
  135. */
  136. #define check_uptr_overflow(_ptr, _inc) \
  137. ((_ptr) > (UINTPTR_MAX - (_inc)))
  138. /*
  139. * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
  140. * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
  141. */
  142. #define check_u32_overflow(_u32, _inc) \
  143. ((_u32) > (UINT32_MAX - (_inc)))
  144. /* Register size of the current architecture. */
  145. #ifdef __aarch64__
  146. #define REGSZ U(8)
  147. #else
  148. #define REGSZ U(4)
  149. #endif
  150. /*
  151. * Test for the current architecture version to be at least the version
  152. * expected.
  153. */
  154. #define ARM_ARCH_AT_LEAST(_maj, _min) \
  155. ((ARM_ARCH_MAJOR > (_maj)) || \
  156. ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
  157. /*
  158. * Import an assembly or linker symbol as a C expression with the specified
  159. * type
  160. */
  161. #define IMPORT_SYM(type, sym, name) \
  162. extern char sym[];\
  163. static const __attribute__((unused)) type name = (type) sym;
  164. /*
  165. * When the symbol is used to hold a pointer, its alignment can be asserted
  166. * with this macro. For example, if there is a linker symbol that is going to
  167. * be used as a 64-bit pointer, the value of the linker symbol must also be
  168. * aligned to 64 bit. This macro makes sure this is the case.
  169. */
  170. #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
  171. #define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
  172. /* Compiler builtin of GCC >= 9 and planned in llvm */
  173. #ifdef __HAVE_SPECULATION_SAFE_VALUE
  174. # define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
  175. #else
  176. # define SPECULATION_SAFE_VALUE(var) var
  177. #endif
  178. /*
  179. * Ticks elapsed in one second with a signal of 1 MHz
  180. */
  181. #define MHZ_TICKS_PER_SEC U(1000000)
  182. /*
  183. * Ticks elapsed in one second with a signal of 1 KHz
  184. */
  185. #define KHZ_TICKS_PER_SEC U(1000)
  186. #endif /* UTILS_DEF_H */