blob: 0013d197d567005af54a5a866225b8ed3f11c925 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
AlexeiFedorovb69eae02023-04-06 10:27:58 +01002 * Copyright (c) 2016-2023, Arm Limited and Contributors. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
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
19#define BIT_32(nr) (U(1) << (nr))
20#define BIT_64(nr) (ULL(1) << (nr))
21
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060022#ifndef __aarch64__
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020023#define BIT BIT_32
24#else
25#define BIT BIT_64
26#endif
27
28/*
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 */
Antonio Nino Diazdcfc4832018-11-22 15:53:23 +000033#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
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020040#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))))
Antonio Nino Diazdcfc4832018-11-22 15:53:23 +000045#endif
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020046
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060047#ifndef __aarch64__
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020048#define GENMASK GENMASK_32
49#else
50#define GENMASK GENMASK_64
51#endif
52
53/*
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
59#define div_round_up(val, div) __extension__ ({ \
60 __typeof__(div) _div = (div); \
Antonio Nino Diazdcfc4832018-11-22 15:53:23 +000061 ((val) + _div - (__typeof__(div)) 1) / _div; \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020062})
63
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 */
99#define check_uptr_overflow(_ptr, _inc) \
100 ((_ptr) > (UINTPTR_MAX - (_inc)))
101
102/*
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 */
106#define check_u32_overflow(_u32, _inc) \
107 ((_u32) > (UINT32_MAX - (_inc)))
108
109/*
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.
113 *
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.
117*/
118#if defined(__LINKER__) || defined(__ASSEMBLY__)
119# define U(_x) (_x)
120# define UL(_x) (_x)
121# define ULL(_x) (_x)
122# define L(_x) (_x)
123# define LL(_x) (_x)
124#else
125# define U(_x) (_x##U)
126# define UL(_x) (_x##UL)
127# define ULL(_x) (_x##ULL)
128# define L(_x) (_x##L)
129# define LL(_x) (_x##LL)
130#endif
131
132/* Register size of the current architecture. */
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -0600133#ifndef __aarch64__
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200134#define REGSZ U(4)
135#else
136#define REGSZ U(8)
137#endif
138
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) \
144 ((ARM_ARCH_MAJOR > (_maj)) || \
145 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
146
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 Diazdcfc4832018-11-22 15:53:23 +0000163#define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200164
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +0100165#define INPLACE(regfield, val) \
166 (((val) + UL(0)) << (regfield##_SHIFT))
167
168#define MASK(regfield) \
nabkah01002e5692022-10-10 12:36:46 +0100169 ((~0ULL >> (64ULL - (regfield##_WIDTH))) << (regfield##_SHIFT))
170
171#define EXTRACT(regfield, reg) \
172 (((reg) & MASK(regfield)) >> (regfield##_SHIFT))
173
174/*
175 * Defines member of structure and reserves space
176 * for the next member with specified offset.
177 */
178#define SET_MEMBER(member, start, end) \
179 union { \
180 member; \
181 unsigned char reserved##end[end - start]; \
182 }
183
AlexeiFedorov380b2af2022-11-23 17:31:27 +0000184#define CONCAT(x, y) x##y
185#define CONC(x, y) CONCAT(x, y)
186
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200187#endif /* UTILS_DEF_H */