Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | * SPDX-FileCopyrightText: Copyright NVIDIA Corporation. |
| 5 | */ |
| 6 | |
| 7 | #ifndef UTILS_DEF_H |
| 8 | #define UTILS_DEF_H |
| 9 | |
| 10 | #if !(defined(__ASSEMBLER__) || defined(__LINKER__)) |
| 11 | #include <stdint.h> |
| 12 | #endif |
| 13 | |
| 14 | /* |
| 15 | * For those constants to be shared between C and other sources, apply a 'U', |
| 16 | * 'UL', 'ULL', 'L' or 'LL' suffix to the argument only in C, to avoid |
| 17 | * undefined or unintended behaviour. |
| 18 | * |
| 19 | * The GNU assembler and linker do not support these suffixes (it causes the |
| 20 | * build process to fail) therefore the suffix is omitted when used in linker |
| 21 | * scripts and assembler files. |
| 22 | */ |
| 23 | #if defined(__ASSEMBLER__) || defined(__LINKER__) |
| 24 | # define U(_x) (_x) |
| 25 | # define UL(_x) (_x) |
| 26 | # define ULL(_x) (_x) |
| 27 | # define L(_x) (_x) |
| 28 | # define LL(_x) (_x) |
| 29 | #else |
| 30 | # define U_(_x) (_x##U) |
| 31 | # define U(_x) U_(_x) |
| 32 | # define UL(_x) (_x##UL) |
| 33 | # define ULL(_x) (_x##ULL) |
| 34 | # define L(_x) (_x##L) |
| 35 | # define LL(_x) (_x##LL) |
| 36 | #endif /* __ASSEMBLER__ */ |
| 37 | |
| 38 | /* Short forms for commonly used attributes */ |
| 39 | #define __dead2 __attribute__((__noreturn__)) |
| 40 | #define __deprecated __attribute__((__deprecated__)) |
| 41 | #define __packed __attribute__((__packed__)) |
| 42 | #define __used __attribute__((__used__)) |
| 43 | #define __unused __attribute__((__unused__)) |
| 44 | #define __aligned(x) __attribute__((__aligned__(x))) |
| 45 | #define __section(x) __attribute__((__section__(x))) |
| 46 | |
| 47 | #define __printflike(fmtarg, firstvararg) \ |
| 48 | __attribute__((__format__ (__printf__, fmtarg, firstvararg))) |
| 49 | |
| 50 | /* |
| 51 | * The round_up() macro rounds up a value to the given boundary in a |
| 52 | * type-agnostic yet type-safe manner. The boundary must be a power of two. |
| 53 | * In other words, it computes the smallest multiple of boundary which is |
| 54 | * greater than or equal to value. |
| 55 | * |
| 56 | * round_down() is similar but rounds the value down instead. |
| 57 | */ |
| 58 | #define round_boundary(value, boundary) \ |
| 59 | ((__typeof__(value))((boundary) - 1)) |
| 60 | |
| 61 | #define round_up(value, boundary) \ |
| 62 | ((((value) - 1) | round_boundary(value, boundary)) + 1) |
| 63 | |
| 64 | #define round_down(value, boundary) \ |
| 65 | ((value) & ~round_boundary(value, boundary)) |
| 66 | |
| 67 | /* Compute the number of elements in the given array */ |
| 68 | #define ARRAY_SIZE(a) \ |
| 69 | (sizeof(a) / sizeof((a)[0])) |
| 70 | |
| 71 | #define ARRAY_LEN(_a) \ |
| 72 | ((sizeof(_a) / sizeof(_a[0])) + CHECK_TYPE_IS_ARRAY(_a)) |
| 73 | |
| 74 | /* |
| 75 | * Macro checks types of array and variable/value to write |
| 76 | * and reports compilation error if they mismatch. |
| 77 | */ |
AlexeiFedorov | 7b3c304 | 2023-06-28 15:41:11 +0100 | [diff] [blame] | 78 | #define CHECK_ARRAY_TYPE(_a, _v) \ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 79 | _Static_assert(__builtin_types_compatible_p(typeof(_a[0]), typeof(_v)), \ |
| 80 | "array type mismatch") |
| 81 | |
| 82 | /* |
| 83 | * Array read/write macros with boundary and types checks |
| 84 | * _a: name of array |
| 85 | * _i: index |
| 86 | * _v: variable/value to write |
| 87 | */ |
AlexeiFedorov | 7b3c304 | 2023-06-28 15:41:11 +0100 | [diff] [blame] | 88 | #define SAFE_ARRAY_READ(_a, _i, _v) \ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 89 | ({ \ |
| 90 | CHECK_ARRAY_TYPE(_a, _v); \ |
| 91 | if (_i >= ARRAY_SIZE(_a)) { \ |
| 92 | panic(); \ |
| 93 | } \ |
| 94 | _v = _a[_i]; \ |
| 95 | }) |
| 96 | |
AlexeiFedorov | 7b3c304 | 2023-06-28 15:41:11 +0100 | [diff] [blame] | 97 | #define SAFE_ARRAY_WRITE(_a, _i, _v) \ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 98 | ({ \ |
| 99 | CHECK_ARRAY_TYPE(_a, _v); \ |
| 100 | if (_i >= ARRAY_SIZE(_a)) { \ |
| 101 | panic(); \ |
| 102 | } \ |
| 103 | _a[_i] = _v; \ |
| 104 | }) |
| 105 | |
Soby Mathew | e02e0bd | 2023-01-18 10:57:18 +0100 | [diff] [blame] | 106 | #define COMPILER_ASSERT(_condition) \ |
| 107 | extern char compiler_assert[(_condition) ? 1 : -1] |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * If _expr is false, this will result in a compile time error as it tries to |
| 111 | * define a bitfield of size -1 in that case. Otherwise, it will define a |
| 112 | * bitfield of size 0, which is valid, and not create a compiler warning. |
| 113 | * |
| 114 | * The return value is only relevant when the compilation succeeds, and by |
| 115 | * subtracting the size of the same struct, this should always return 0 as a |
| 116 | * value and can be included in other expressions. |
| 117 | */ |
| 118 | #define COMPILER_ASSERT_ZERO(_expr) (sizeof(struct { char: (-!(_expr)); }) \ |
| 119 | - sizeof(struct { char: 0; })) |
| 120 | |
| 121 | #define CHECK_TYPE_IS_ARRAY(_v) \ |
Soby Mathew | e02e0bd | 2023-01-18 10:57:18 +0100 | [diff] [blame] | 122 | COMPILER_ASSERT_ZERO(!__builtin_types_compatible_p(typeof(_v), \ |
| 123 | typeof(&(_v[0])))) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 124 | |
| 125 | #define IS_POWER_OF_TWO(x) \ |
| 126 | ((((x) + UL(0)) & ((x) - UL(1))) == UL(0)) |
| 127 | |
| 128 | #define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory") |
| 129 | |
Soby Mathew | e02e0bd | 2023-01-18 10:57:18 +0100 | [diff] [blame] | 130 | #define ALIGNED(_size, _alignment) \ |
| 131 | (((unsigned long)(_size) % (_alignment)) == UL(0)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 132 | |
| 133 | #define GRANULE_ALIGNED(_addr) ALIGNED((void *)(_addr), GRANULE_SIZE) |
Soby Mathew | e02e0bd | 2023-01-18 10:57:18 +0100 | [diff] [blame] | 134 | #define ALIGNED_TO_ARRAY(_addr, _array) \ |
| 135 | (((uintptr_t)_addr >= (uintptr_t)&_array[0]) && \ |
| 136 | ((((uintptr_t)_addr - (uintptr_t)&_array[0]) % \ |
| 137 | sizeof(_array[0])) == UL(0))) |
Mate Toth-Pal | ac0cfbb | 2023-06-19 16:29:52 +0200 | [diff] [blame^] | 138 | |
| 139 | #define GRANULE_SIZE (UL(1) << GRANULE_SHIFT) |
| 140 | #define GRANULE_MASK (~(GRANULE_SIZE - 1U)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 141 | |
| 142 | #define HAS_MPAM 0 |
| 143 | |
| 144 | #if HAS_MPAM |
| 145 | #define MPAM(_x...) _x |
| 146 | #else |
| 147 | #define MPAM(_x...) |
| 148 | #endif |
| 149 | |
| 150 | #define HAS_SPE 0 |
| 151 | |
| 152 | #if HAS_SPE |
| 153 | #define SPE(_x...) _x |
| 154 | #else |
| 155 | #define SPE(_x...) |
| 156 | #endif |
| 157 | |
| 158 | #if !(defined(__ASSEMBLER__) || defined(__LINKER__)) |
| 159 | |
| 160 | /* |
| 161 | * System register field definitions. |
| 162 | * |
| 163 | * For any register field we define: |
| 164 | * - <register>_<field>_SHIFT |
| 165 | * The bit offset of the LSB of the field. |
| 166 | * - <register>_<field>_WIDTH |
| 167 | * The width of the field in bits. |
| 168 | * |
| 169 | * For single bit fields, we define: |
| 170 | * - <register>_<field>_BIT |
| 171 | * The in-place value of the field with the bit set. |
| 172 | * |
| 173 | * For multi-bit fields, we define: |
| 174 | * - <register>_<field>_<enum> |
| 175 | * The in-place value of the field set to the value corresponding to the |
| 176 | * enumeration name. |
| 177 | * |
| 178 | * For any register field, we define: |
| 179 | * - INPLACE(<register>_<field>, val) |
| 180 | * The in-place value of the field set to val, handling any necessary type |
| 181 | * promotion to avoid truncation of val. |
| 182 | * - MASK(<register>_<field) |
| 183 | * An in-place bitmask covering all bits of the field. |
| 184 | * - EXTRACT(<register_field> <register_value>) |
| 185 | * A macro to extract the value of a register field shifted down so the |
| 186 | * value can be evaluated directly. |
| 187 | * - EXTRACT_BIT(<register_field> <register_value>) |
| 188 | * A macro to extract the value of a register bit shifted down so the |
| 189 | * value can be evaluated directly. |
| 190 | */ |
| 191 | #define INPLACE(regfield, val) \ |
| 192 | (((val) + UL(0)) << (regfield##_SHIFT)) |
| 193 | |
| 194 | #define MASK(regfield) \ |
| 195 | ((~0UL >> (64UL - (regfield##_WIDTH))) << (regfield##_SHIFT)) |
| 196 | |
| 197 | #define EXTRACT(regfield, reg) \ |
| 198 | (((reg) & MASK(regfield)) >> (regfield##_SHIFT)) |
| 199 | |
| 200 | #define EXTRACT_BIT(regfield, reg) \ |
| 201 | (((reg) >> (regfield##_SHIFT)) & UL(1)) |
| 202 | |
| 203 | /* |
| 204 | * Generates an unsigned long long (64-bit) value where the bits @_msb |
| 205 | * through @_lsb (inclusive) are set to one and all other bits are zero. The |
Soby Mathew | e02e0bd | 2023-01-18 10:57:18 +0100 | [diff] [blame] | 206 | * parameters can hold values from 0 through 63 and if _msb == _lsb a single |
| 207 | * bit is set at that location. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 208 | */ |
| 209 | #define BIT_MASK_ULL(_msb, _lsb) \ |
| 210 | ((~ULL(0) >> (63UL - (_msb))) & (~ULL(0) << (_lsb))) |
| 211 | |
| 212 | /* |
| 213 | * Stringify the result of expansion of a macro argument |
| 214 | */ |
| 215 | #ifndef __XSTRING |
| 216 | #define __STRING(x) #x |
| 217 | #define __XSTRING(x) __STRING(x) |
| 218 | #endif |
| 219 | |
| 220 | /* |
| 221 | * Defines member of structure and reserves space |
| 222 | * for the next member with specified offset. |
| 223 | */ |
| 224 | #define SET_MEMBER(member, start, end) \ |
| 225 | union { \ |
| 226 | member; \ |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 227 | unsigned char reserved##end[(end) - (start)]; \ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 228 | } |
| 229 | |
AlexeiFedorov | 7b3c304 | 2023-06-28 15:41:11 +0100 | [diff] [blame] | 230 | #define FALLTHROUGH __attribute__((fallthrough)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 231 | |
| 232 | #endif /* !(defined(__ASSEMBLER__) || defined(__LINKER__)) */ |
| 233 | |
| 234 | #endif /* UTILS_DEF_H */ |