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