blob: 21c77087ad39bec65103215780757a56a65422fb [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
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 */
78#define CHECK_ARRAY_TYPE(_a, _v) \
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 */
AlexeiFedorovfeaef162022-12-23 16:59:51 +000088#define SAFE_ARRAY_READ(_a, _i, _v) \
Soby Mathewb4c6df42022-11-09 11:13:29 +000089({ \
90 CHECK_ARRAY_TYPE(_a, _v); \
91 if (_i >= ARRAY_SIZE(_a)) { \
92 panic(); \
93 } \
94 _v = _a[_i]; \
95})
96
AlexeiFedorovfeaef162022-12-23 16:59:51 +000097#define SAFE_ARRAY_WRITE(_a, _i, _v) \
Soby Mathewb4c6df42022-11-09 11:13:29 +000098({ \
99 CHECK_ARRAY_TYPE(_a, _v); \
100 if (_i >= ARRAY_SIZE(_a)) { \
101 panic(); \
102 } \
103 _a[_i] = _v; \
104})
105
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100106#define COMPILER_ASSERT(_condition) \
107 extern char compiler_assert[(_condition) ? 1 : -1]
Soby Mathewb4c6df42022-11-09 11:13:29 +0000108
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 Mathewe02e0bd2023-01-18 10:57:18 +0100122 COMPILER_ASSERT_ZERO(!__builtin_types_compatible_p(typeof(_v), \
123 typeof(&(_v[0]))))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000124
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 Mathewe02e0bd2023-01-18 10:57:18 +0100130#define ALIGNED(_size, _alignment) \
131 (((unsigned long)(_size) % (_alignment)) == UL(0))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000132
133#define GRANULE_ALIGNED(_addr) ALIGNED((void *)(_addr), GRANULE_SIZE)
Soby Mathewe02e0bd2023-01-18 10:57:18 +0100134#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)))
Soby Mathewb4c6df42022-11-09 11:13:29 +0000138#define GRANULE_SHIFT (UL(12))
139#define GRANULE_MASK (~0xfffUL)
140
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; \
226 unsigned char reserved##end[end - start]; \
227 }
228
229#define FALLTHROUGH __attribute__((fallthrough))
230
231#endif /* !(defined(__ASSEMBLER__) || defined(__LINKER__)) */
232
233#endif /* UTILS_DEF_H */