utils_def.h 5.9 KB

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