blob: c600885a29f5e3a414ed6956f0d3d4e22ffcb238 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
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
AlexeiFedorov1ba649f2023-10-19 13:56:02 +010030# define U(_x) (unsigned int)(_x)
Soby Mathewb4c6df42022-11-09 11:13:29 +000031# define UL(_x) (_x##UL)
32# define ULL(_x) (_x##ULL)
33# define L(_x) (_x##L)
34# define LL(_x) (_x##LL)
35#endif /* __ASSEMBLER__ */
36
37/* Short forms for commonly used attributes */
38#define __dead2 __attribute__((__noreturn__))
39#define __deprecated __attribute__((__deprecated__))
40#define __packed __attribute__((__packed__))
41#define __used __attribute__((__used__))
42#define __unused __attribute__((__unused__))
43#define __aligned(x) __attribute__((__aligned__(x)))
44#define __section(x) __attribute__((__section__(x)))
45
46#define __printflike(fmtarg, firstvararg) \
47 __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
48
49/*
50 * The round_up() macro rounds up a value to the given boundary in a
51 * type-agnostic yet type-safe manner. The boundary must be a power of two.
52 * In other words, it computes the smallest multiple of boundary which is
53 * greater than or equal to value.
54 *
55 * round_down() is similar but rounds the value down instead.
56 */
57#define round_boundary(value, boundary) \
58 ((__typeof__(value))((boundary) - 1))
59
60#define round_up(value, boundary) \
61 ((((value) - 1) | round_boundary(value, boundary)) + 1)
62
63#define round_down(value, boundary) \
64 ((value) & ~round_boundary(value, boundary))
65
66/* Compute the number of elements in the given array */
67#define ARRAY_SIZE(a) \
68 (sizeof(a) / sizeof((a)[0]))
69
70#define ARRAY_LEN(_a) \
Chuyue Luoe95902b2023-10-17 15:46:25 +010071 ((sizeof(_a) / sizeof((_a)[0])) + CHECK_TYPE_IS_ARRAY(_a))
Soby Mathewb4c6df42022-11-09 11:13:29 +000072
73/*
74 * Macro checks types of array and variable/value to write
75 * and reports compilation error if they mismatch.
76 */
AlexeiFedorov7b3c3042023-06-28 15:41:11 +010077#define CHECK_ARRAY_TYPE(_a, _v) \
Chuyue Luoe95902b2023-10-17 15:46:25 +010078 _Static_assert(__builtin_types_compatible_p(typeof((_a)[0]), typeof(_v)), \
Soby Mathewb4c6df42022-11-09 11:13:29 +000079 "array type mismatch")
80
81/*
82 * Array read/write macros with boundary and types checks
83 * _a: name of array
84 * _i: index
85 * _v: variable/value to write
86 */
AlexeiFedorov7b3c3042023-06-28 15:41:11 +010087#define SAFE_ARRAY_READ(_a, _i, _v) \
Soby Mathewb4c6df42022-11-09 11:13:29 +000088({ \
89 CHECK_ARRAY_TYPE(_a, _v); \
Chuyue Luoe95902b2023-10-17 15:46:25 +010090 if ((_i) >= ARRAY_SIZE(_a)) { \
Soby Mathewb4c6df42022-11-09 11:13:29 +000091 panic(); \
92 } \
Chuyue Luoe95902b2023-10-17 15:46:25 +010093 (_v) = (_a)[_i]; \
Soby Mathewb4c6df42022-11-09 11:13:29 +000094})
95
AlexeiFedorov7b3c3042023-06-28 15:41:11 +010096#define SAFE_ARRAY_WRITE(_a, _i, _v) \
Soby Mathewb4c6df42022-11-09 11:13:29 +000097({ \
98 CHECK_ARRAY_TYPE(_a, _v); \
Chuyue Luoe95902b2023-10-17 15:46:25 +010099 if ((_i) >= ARRAY_SIZE(_a)) { \
Soby Mathewb4c6df42022-11-09 11:13:29 +0000100 panic(); \
101 } \
Chuyue Luoe95902b2023-10-17 15:46:25 +0100102 (_a)[_i] = (_v); \
Soby Mathewb4c6df42022-11-09 11:13:29 +0000103})
104
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100105#define COMPILER_ASSERT(_condition) \
106 extern char compiler_assert[(_condition) ? 1 : -1]
Soby Mathewb4c6df42022-11-09 11:13:29 +0000107
108/*
109 * If _expr is false, this will result in a compile time error as it tries to
110 * define a bitfield of size -1 in that case. Otherwise, it will define a
111 * bitfield of size 0, which is valid, and not create a compiler warning.
112 *
113 * The return value is only relevant when the compilation succeeds, and by
114 * subtracting the size of the same struct, this should always return 0 as a
115 * value and can be included in other expressions.
116 */
117#define COMPILER_ASSERT_ZERO(_expr) (sizeof(struct { char: (-!(_expr)); }) \
118 - sizeof(struct { char: 0; }))
119
120#define CHECK_TYPE_IS_ARRAY(_v) \
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100121 COMPILER_ASSERT_ZERO(!__builtin_types_compatible_p(typeof(_v), \
Chuyue Luoe95902b2023-10-17 15:46:25 +0100122 typeof(&((_v)[0]))))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000123
124#define IS_POWER_OF_TWO(x) \
125 ((((x) + UL(0)) & ((x) - UL(1))) == UL(0))
126
127#define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
128
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100129#define ALIGNED(_size, _alignment) \
130 (((unsigned long)(_size) % (_alignment)) == UL(0))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000131
132#define GRANULE_ALIGNED(_addr) ALIGNED((void *)(_addr), GRANULE_SIZE)
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100133#define ALIGNED_TO_ARRAY(_addr, _array) \
Chuyue Luoe95902b2023-10-17 15:46:25 +0100134 (((uintptr_t)(_addr) >= (uintptr_t)&(_array)[0]) && \
135 ((((uintptr_t)(_addr) - (uintptr_t)&(_array)[0]) % \
136 sizeof((_array)[0])) == UL(0)))
Mate Toth-Palac0cfbb2023-06-19 16:29:52 +0200137
138#define GRANULE_SIZE (UL(1) << GRANULE_SHIFT)
139#define GRANULE_MASK (~(GRANULE_SIZE - 1U))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000140
141#define HAS_MPAM 0
142
143#if HAS_MPAM
144#define MPAM(_x...) _x
145#else
146#define MPAM(_x...)
147#endif
148
149#define HAS_SPE 0
150
151#if HAS_SPE
152#define SPE(_x...) _x
153#else
154#define SPE(_x...)
155#endif
156
157#if !(defined(__ASSEMBLER__) || defined(__LINKER__))
158
159/*
160 * System register field definitions.
161 *
162 * For any register field we define:
163 * - <register>_<field>_SHIFT
164 * The bit offset of the LSB of the field.
165 * - <register>_<field>_WIDTH
166 * The width of the field in bits.
167 *
168 * For single bit fields, we define:
169 * - <register>_<field>_BIT
170 * The in-place value of the field with the bit set.
171 *
172 * For multi-bit fields, we define:
173 * - <register>_<field>_<enum>
174 * The in-place value of the field set to the value corresponding to the
175 * enumeration name.
176 *
177 * For any register field, we define:
178 * - INPLACE(<register>_<field>, val)
179 * The in-place value of the field set to val, handling any necessary type
180 * promotion to avoid truncation of val.
181 * - MASK(<register>_<field)
182 * An in-place bitmask covering all bits of the field.
183 * - EXTRACT(<register_field> <register_value>)
184 * A macro to extract the value of a register field shifted down so the
185 * value can be evaluated directly.
186 * - EXTRACT_BIT(<register_field> <register_value>)
187 * A macro to extract the value of a register bit shifted down so the
188 * value can be evaluated directly.
189 */
190#define INPLACE(regfield, val) \
191 (((val) + UL(0)) << (regfield##_SHIFT))
192
193#define MASK(regfield) \
194 ((~0UL >> (64UL - (regfield##_WIDTH))) << (regfield##_SHIFT))
195
196#define EXTRACT(regfield, reg) \
197 (((reg) & MASK(regfield)) >> (regfield##_SHIFT))
198
199#define EXTRACT_BIT(regfield, reg) \
200 (((reg) >> (regfield##_SHIFT)) & UL(1))
201
202/*
203 * Generates an unsigned long long (64-bit) value where the bits @_msb
204 * through @_lsb (inclusive) are set to one and all other bits are zero. The
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100205 * parameters can hold values from 0 through 63 and if _msb == _lsb a single
206 * bit is set at that location.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000207 */
208#define BIT_MASK_ULL(_msb, _lsb) \
209 ((~ULL(0) >> (63UL - (_msb))) & (~ULL(0) << (_lsb)))
210
211/*
212 * Stringify the result of expansion of a macro argument
213 */
214#ifndef __XSTRING
215#define __STRING(x) #x
216#define __XSTRING(x) __STRING(x)
217#endif
218
219/*
220 * Defines member of structure and reserves space
221 * for the next member with specified offset.
222 */
223#define SET_MEMBER(member, start, end) \
224 union { \
225 member; \
AlexeiFedorov80618d72023-10-23 14:15:28 +0100226 unsigned char reserved##end[((end) - (start))]; \
Soby Mathewb4c6df42022-11-09 11:13:29 +0000227 }
228
AlexeiFedorov7b3c3042023-06-28 15:41:11 +0100229#define FALLTHROUGH __attribute__((fallthrough))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000230
231#endif /* !(defined(__ASSEMBLER__) || defined(__LINKER__)) */
232
233#endif /* UTILS_DEF_H */