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 | |
| 7 | #ifndef __UTILS_DEF_H__ |
| 8 | #define __UTILS_DEF_H__ |
| 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 | */ |
| 33 | #define GENMASK_32(h, l) \ |
| 34 | (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h)))) |
| 35 | |
| 36 | #define GENMASK_64(h, l) \ |
| 37 | (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h)))) |
| 38 | |
| 39 | #ifdef AARCH32 |
| 40 | #define GENMASK GENMASK_32 |
| 41 | #else |
| 42 | #define GENMASK GENMASK_64 |
| 43 | #endif |
| 44 | |
| 45 | /* |
Soby Mathew | 327548c | 2017-07-13 15:19:51 +0100 | [diff] [blame] | 46 | * This variant of div_round_up can be used in macro definition but should not |
| 47 | * be used in C code as the `div` parameter is evaluated twice. |
| 48 | */ |
| 49 | #define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d)) |
| 50 | |
Julius Werner | 54b6d20 | 2018-01-22 13:56:13 -0800 | [diff] [blame] | 51 | #define div_round_up(val, div) __extension__ ({ \ |
| 52 | __typeof__(div) _div = (div); \ |
| 53 | ((val) + _div - 1) / _div; \ |
| 54 | }) |
| 55 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 56 | #define MIN(x, y) __extension__ ({ \ |
| 57 | __typeof__(x) _x = (x); \ |
| 58 | __typeof__(y) _y = (y); \ |
| 59 | (void)(&_x == &_y); \ |
| 60 | _x < _y ? _x : _y; \ |
| 61 | }) |
| 62 | |
| 63 | #define MAX(x, y) __extension__ ({ \ |
| 64 | __typeof__(x) _x = (x); \ |
| 65 | __typeof__(y) _y = (y); \ |
| 66 | (void)(&_x == &_y); \ |
| 67 | _x > _y ? _x : _y; \ |
| 68 | }) |
| 69 | |
| 70 | /* |
| 71 | * The round_up() macro rounds up a value to the given boundary in a |
| 72 | * type-agnostic yet type-safe manner. The boundary must be a power of two. |
| 73 | * In other words, it computes the smallest multiple of boundary which is |
| 74 | * greater than or equal to value. |
| 75 | * |
| 76 | * round_down() is similar but rounds the value down instead. |
| 77 | */ |
| 78 | #define round_boundary(value, boundary) \ |
| 79 | ((__typeof__(value))((boundary) - 1)) |
| 80 | |
| 81 | #define round_up(value, boundary) \ |
| 82 | ((((value) - 1) | round_boundary(value, boundary)) + 1) |
| 83 | |
| 84 | #define round_down(value, boundary) \ |
| 85 | ((value) & ~round_boundary(value, boundary)) |
| 86 | |
| 87 | /* |
| 88 | * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise. |
| 89 | * Both arguments must be unsigned pointer values (i.e. uintptr_t). |
| 90 | */ |
| 91 | #define check_uptr_overflow(ptr, inc) \ |
| 92 | (((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0) |
| 93 | |
| 94 | /* |
Jeenu Viswambharan | 19f6cf2 | 2017-12-07 08:43:05 +0000 | [diff] [blame] | 95 | * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise. |
| 96 | * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t). |
| 97 | */ |
| 98 | #define check_u32_overflow(u32, inc) \ |
| 99 | ((u32) > (UINT32_MAX - (inc)) ? 1 : 0) |
| 100 | |
| 101 | /* |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 102 | * For those constants to be shared between C and other sources, apply a 'u' |
| 103 | * or 'ull' suffix to the argument only in C, to avoid undefined or unintended |
| 104 | * behaviour. |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 105 | * |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 106 | * The GNU assembler and linker do not support the 'u' and 'ull' suffix (it |
| 107 | * causes the build process to fail) therefore the suffix is omitted when used |
| 108 | * in linker scripts and assembler files. |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 109 | */ |
| 110 | #if defined(__LINKER__) || defined(__ASSEMBLY__) |
Varun Wadekar | 05597a9 | 2017-05-18 10:32:51 -0700 | [diff] [blame] | 111 | # define U(_x) (_x) |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 112 | # define ULL(_x) (_x) |
| 113 | #else |
David Cunado | c150312 | 2018-02-16 21:12:58 +0000 | [diff] [blame] | 114 | # define U(_x) (_x##U) |
| 115 | # define ULL(_x) (_x##ULL) |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 116 | #endif |
| 117 | |
Julius Werner | 02eb727 | 2017-12-12 14:23:26 -0800 | [diff] [blame] | 118 | /* Register size of the current architecture. */ |
| 119 | #ifdef AARCH32 |
| 120 | #define REGSZ U(4) |
| 121 | #else |
| 122 | #define REGSZ U(8) |
| 123 | #endif |
| 124 | |
Jeenu Viswambharan | 0bc79d9 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 125 | /* |
| 126 | * Test for the current architecture version to be at least the version |
| 127 | * expected. |
| 128 | */ |
| 129 | #define ARM_ARCH_AT_LEAST(_maj, _min) \ |
Jeenu Viswambharan | 58e8148 | 2018-04-27 15:06:57 +0100 | [diff] [blame] | 130 | ((ARM_ARCH_MAJOR > (_maj)) || \ |
| 131 | ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min)))) |
Jeenu Viswambharan | 0bc79d9 | 2017-08-16 11:44:25 +0100 | [diff] [blame] | 132 | |
Joel Hutton | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 133 | /* |
| 134 | * Import an assembly or linker symbol as a C expression with the specified |
| 135 | * type |
| 136 | */ |
| 137 | #define IMPORT_SYM(type, sym, name) \ |
| 138 | extern char sym[];\ |
| 139 | static const __attribute__((unused)) type name = (type) sym; |
| 140 | |
| 141 | /* |
| 142 | * When the symbol is used to hold a pointer, its alignment can be asserted |
| 143 | * with this macro. For example, if there is a linker symbol that is going to |
| 144 | * be used as a 64-bit pointer, the value of the linker symbol must also be |
| 145 | * aligned to 64 bit. This macro makes sure this is the case. |
| 146 | */ |
| 147 | #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0) |
| 148 | |
| 149 | |
Scott Branden | bf404c0 | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 150 | #endif /* __UTILS_DEF_H__ */ |