Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 1 | /* |
Julius Werner | 2a231e3 | 2019-05-28 21:03:58 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | 0a176e3 | 2020-02-13 13:07:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2020, NVIDIA Corporation. All rights reserved. |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 4 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 5 | * SPDX-License-Identifier: BSD-3-Clause |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Antonio Nino Diaz | bb77f88 | 2018-07-18 13:23:07 +0100 | [diff] [blame] | 8 | #ifndef UTILS_DEF_H |
| 9 | #define UTILS_DEF_H |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 10 | |
Julius Werner | 2a231e3 | 2019-05-28 21:03:58 -0700 | [diff] [blame] | 11 | #include <export/lib/utils_def_exp.h> |
| 12 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 13 | /* Compute the number of elements in the given array */ |
| 14 | #define ARRAY_SIZE(a) \ |
| 15 | (sizeof(a) / sizeof((a)[0])) |
| 16 | |
| 17 | #define IS_POWER_OF_TWO(x) \ |
| 18 | (((x) & ((x) - 1)) == 0) |
| 19 | |
| 20 | #define SIZE_FROM_LOG2_WORDS(n) (4 << (n)) |
| 21 | |
Yann Gautier | c70fed5 | 2018-06-14 13:28:31 +0200 | [diff] [blame] | 22 | #define BIT_32(nr) (U(1) << (nr)) |
| 23 | #define BIT_64(nr) (ULL(1) << (nr)) |
| 24 | |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 25 | #ifdef __aarch64__ |
Yann Gautier | c70fed5 | 2018-06-14 13:28:31 +0200 | [diff] [blame] | 26 | #define BIT BIT_64 |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 27 | #else |
| 28 | #define BIT BIT_32 |
Yann Gautier | c70fed5 | 2018-06-14 13:28:31 +0200 | [diff] [blame] | 29 | #endif |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 30 | |
Soby Mathew | 327548c | 2017-07-13 15:19:51 +0100 | [diff] [blame] | 31 | /* |
Yann Gautier | 953e94d | 2018-06-14 18:35:33 +0200 | [diff] [blame] | 32 | * Create a contiguous bitmask starting at bit position @l and ending at |
| 33 | * position @h. For example |
| 34 | * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000. |
| 35 | */ |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 36 | #if defined(__LINKER__) || defined(__ASSEMBLER__) |
Yann Gautier | d2c9a68 | 2018-11-15 09:49:24 +0100 | [diff] [blame] | 37 | #define GENMASK_32(h, l) \ |
| 38 | (((0xFFFFFFFF) << (l)) & (0xFFFFFFFF >> (32 - 1 - (h)))) |
| 39 | |
| 40 | #define GENMASK_64(h, l) \ |
| 41 | ((~0 << (l)) & (~0 >> (64 - 1 - (h)))) |
| 42 | #else |
Yann Gautier | 953e94d | 2018-06-14 18:35:33 +0200 | [diff] [blame] | 43 | #define GENMASK_32(h, l) \ |
| 44 | (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h)))) |
| 45 | |
| 46 | #define GENMASK_64(h, l) \ |
| 47 | (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h)))) |
Yann Gautier | d2c9a68 | 2018-11-15 09:49:24 +0100 | [diff] [blame] | 48 | #endif |
Yann Gautier | 953e94d | 2018-06-14 18:35:33 +0200 | [diff] [blame] | 49 | |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 50 | #ifdef __aarch64__ |
Yann Gautier | 953e94d | 2018-06-14 18:35:33 +0200 | [diff] [blame] | 51 | #define GENMASK GENMASK_64 |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 52 | #else |
| 53 | #define GENMASK GENMASK_32 |
Yann Gautier | 953e94d | 2018-06-14 18:35:33 +0200 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | /* |
Soby Mathew | 327548c | 2017-07-13 15:19:51 +0100 | [diff] [blame] | 57 | * This variant of div_round_up can be used in macro definition but should not |
| 58 | * be used in C code as the `div` parameter is evaluated twice. |
| 59 | */ |
| 60 | #define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d)) |
| 61 | |
Julius Werner | 54b6d20 | 2018-01-22 13:56:13 -0800 | [diff] [blame] | 62 | #define div_round_up(val, div) __extension__ ({ \ |
| 63 | __typeof__(div) _div = (div); \ |
Sathees Balya | 006b15e | 2018-09-19 14:23:03 +0100 | [diff] [blame] | 64 | ((val) + _div - (__typeof__(div)) 1) / _div; \ |
Julius Werner | 54b6d20 | 2018-01-22 13:56:13 -0800 | [diff] [blame] | 65 | }) |
| 66 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 67 | #define MIN(x, y) __extension__ ({ \ |
| 68 | __typeof__(x) _x = (x); \ |
| 69 | __typeof__(y) _y = (y); \ |
| 70 | (void)(&_x == &_y); \ |
| 71 | _x < _y ? _x : _y; \ |
| 72 | }) |
| 73 | |
| 74 | #define MAX(x, y) __extension__ ({ \ |
| 75 | __typeof__(x) _x = (x); \ |
| 76 | __typeof__(y) _y = (y); \ |
| 77 | (void)(&_x == &_y); \ |
| 78 | _x > _y ? _x : _y; \ |
| 79 | }) |
| 80 | |
Lionel Debieve | a265ade | 2020-01-02 11:14:16 +0100 | [diff] [blame] | 81 | #define CLAMP(x, min, max) __extension__ ({ \ |
| 82 | __typeof__(x) _x = (x); \ |
| 83 | __typeof__(min) _min = (min); \ |
| 84 | __typeof__(max) _max = (max); \ |
| 85 | (void)(&_x == &_min); \ |
| 86 | (void)(&_x == &_max); \ |
| 87 | (_x > _max ? _max : (_x < _min ? _min : _x)); \ |
| 88 | }) |
| 89 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 90 | /* |
| 91 | * The round_up() macro rounds up a value to the given boundary in a |
| 92 | * type-agnostic yet type-safe manner. The boundary must be a power of two. |
| 93 | * In other words, it computes the smallest multiple of boundary which is |
| 94 | * greater than or equal to value. |
| 95 | * |
| 96 | * round_down() is similar but rounds the value down instead. |
| 97 | */ |
| 98 | #define round_boundary(value, boundary) \ |
| 99 | ((__typeof__(value))((boundary) - 1)) |
| 100 | |
| 101 | #define round_up(value, boundary) \ |
| 102 | ((((value) - 1) | round_boundary(value, boundary)) + 1) |
| 103 | |
| 104 | #define round_down(value, boundary) \ |
| 105 | ((value) & ~round_boundary(value, boundary)) |
| 106 | |
| 107 | /* |
| 108 | * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. |
| 109 | * Both arguments must be unsigned pointer values (i.e. uintptr_t). |
| 110 | */ |
Antonio Nino Diaz | bb77f88 | 2018-07-18 13:23:07 +0100 | [diff] [blame] | 111 | #define check_uptr_overflow(_ptr, _inc) \ |
| 112 | ((_ptr) > (UINTPTR_MAX - (_inc))) |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
Jeenu Viswambharan | 19f6cf2 | 2017-12-07 08:43:05 +0000 | [diff] [blame] | 115 | * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise. |
| 116 | * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t). |
| 117 | */ |
Antonio Nino Diaz | bb77f88 | 2018-07-18 13:23:07 +0100 | [diff] [blame] | 118 | #define check_u32_overflow(_u32, _inc) \ |
| 119 | ((_u32) > (UINT32_MAX - (_inc))) |
Jeenu Viswambharan | 19f6cf2 | 2017-12-07 08:43:05 +0000 | [diff] [blame] | 120 | |
Julius Werner | 02eb727 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 121 | /* Register size of the current architecture. */ |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 122 | #ifdef __aarch64__ |
Julius Werner | 02eb727 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 123 | #define REGSZ U(8) |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 124 | #else |
| 125 | #define REGSZ U(4) |
Julius Werner | 02eb727 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 126 | #endif |
| 127 | |
Jeenu Viswambharan | 0bc79d9 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 128 | /* |
| 129 | * Test for the current architecture version to be at least the version |
| 130 | * expected. |
| 131 | */ |
| 132 | #define ARM_ARCH_AT_LEAST(_maj, _min) \ |
Jeenu Viswambharan | 58e8148 | 2018-04-27 15:06:57 +0100 | [diff] [blame] | 133 | ((ARM_ARCH_MAJOR > (_maj)) || \ |
| 134 | ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min)))) |
Jeenu Viswambharan | 0bc79d9 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 135 | |
Joel Hutton | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Import an assembly or linker symbol as a C expression with the specified |
| 138 | * type |
| 139 | */ |
| 140 | #define IMPORT_SYM(type, sym, name) \ |
| 141 | extern char sym[];\ |
| 142 | static const __attribute__((unused)) type name = (type) sym; |
| 143 | |
| 144 | /* |
| 145 | * When the symbol is used to hold a pointer, its alignment can be asserted |
| 146 | * with this macro. For example, if there is a linker symbol that is going to |
| 147 | * be used as a 64-bit pointer, the value of the linker symbol must also be |
| 148 | * aligned to 64 bit. This macro makes sure this is the case. |
| 149 | */ |
| 150 | #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0) |
| 151 | |
Antonio Nino Diaz | 8257f5b | 2018-11-22 15:53:17 +0000 | [diff] [blame] | 152 | #define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory") |
Joel Hutton | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 153 | |
Joel Hutton | d522759 | 2018-10-09 14:08:42 +0100 | [diff] [blame] | 154 | /* Compiler builtin of GCC >= 9 and planned in llvm */ |
| 155 | #ifdef __HAVE_SPECULATION_SAFE_VALUE |
| 156 | # define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var) |
| 157 | #else |
| 158 | # define SPECULATION_SAFE_VALUE(var) var |
| 159 | #endif |
| 160 | |
Varun Wadekar | 0a176e3 | 2020-02-13 13:07:12 -0800 | [diff] [blame] | 161 | /* |
| 162 | * Ticks elapsed in one second with a signal of 1 MHz |
| 163 | */ |
| 164 | #define MHZ_TICKS_PER_SEC U(1000000) |
| 165 | |
Antonio Nino Diaz | bb77f88 | 2018-07-18 13:23:07 +0100 | [diff] [blame] | 166 | #endif /* UTILS_DEF_H */ |