utils_def.h 4.55 KB
Newer Older
1
/*
2
 * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
3
 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
4
 *
dp-arm's avatar
dp-arm committed
5
 * SPDX-License-Identifier: BSD-3-Clause
6
7
 */

8
9
#ifndef UTILS_DEF_H
#define UTILS_DEF_H
10

11
12
#include <export/lib/utils_def_exp.h>

13
14
15
16
17
18
19
/* Compute the number of elements in the given array */
#define ARRAY_SIZE(a)				\
	(sizeof(a) / sizeof((a)[0]))

#define IS_POWER_OF_TWO(x)			\
	(((x) & ((x) - 1)) == 0)

20
#define SIZE_FROM_LOG2_WORDS(n)		(U(4) << (n))
21

22
23
24
#define BIT_32(nr)			(U(1) << (nr))
#define BIT_64(nr)			(ULL(1) << (nr))

25
#ifdef __aarch64__
26
#define BIT				BIT_64
27
28
#else
#define BIT				BIT_32
29
#endif
30

Yann Gautier's avatar
Yann Gautier committed
31
32
33
34
35
/*
 * Create a contiguous bitmask starting at bit position @l and ending at
 * position @h. For example
 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
 */
36
#if defined(__LINKER__) || defined(__ASSEMBLER__)
37
38
39
40
41
42
#define GENMASK_32(h, l) \
	(((0xFFFFFFFF) << (l)) & (0xFFFFFFFF >> (32 - 1 - (h))))

#define GENMASK_64(h, l) \
	((~0 << (l)) & (~0 >> (64 - 1 - (h))))
#else
Yann Gautier's avatar
Yann Gautier committed
43
44
45
46
47
#define GENMASK_32(h, l) \
	(((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))

#define GENMASK_64(h, l) \
	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
48
#endif
Yann Gautier's avatar
Yann Gautier committed
49

50
#ifdef __aarch64__
Yann Gautier's avatar
Yann Gautier committed
51
#define GENMASK				GENMASK_64
52
53
#else
#define GENMASK				GENMASK_32
Yann Gautier's avatar
Yann Gautier committed
54
55
#endif

56
57
58
59
60
61
/*
 * This variant of div_round_up can be used in macro definition but should not
 * be used in C code as the `div` parameter is evaluated twice.
 */
#define DIV_ROUND_UP_2EVAL(n, d)	(((n) + (d) - 1) / (d))

62
63
#define div_round_up(val, div) __extension__ ({	\
	__typeof__(div) _div = (div);		\
64
	((val) + _div - (__typeof__(div)) 1) / _div;		\
65
66
})

67
68
69
70
71
72
73
74
75
76
77
78
79
80
#define MIN(x, y) __extension__ ({	\
	__typeof__(x) _x = (x);		\
	__typeof__(y) _y = (y);		\
	(void)(&_x == &_y);		\
	_x < _y ? _x : _y;		\
})

#define MAX(x, y) __extension__ ({	\
	__typeof__(x) _x = (x);		\
	__typeof__(y) _y = (y);		\
	(void)(&_x == &_y);		\
	_x > _y ? _x : _y;		\
})

81
82
83
84
85
86
87
88
89
#define CLAMP(x, min, max) __extension__ ({ \
	__typeof__(x) _x = (x); \
	__typeof__(min) _min = (min); \
	__typeof__(max) _max = (max); \
	(void)(&_x == &_min); \
	(void)(&_x == &_max); \
	(_x > _max ? _max : (_x < _min ? _min : _x)); \
})

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * The round_up() macro rounds up a value to the given boundary in a
 * type-agnostic yet type-safe manner. The boundary must be a power of two.
 * In other words, it computes the smallest multiple of boundary which is
 * greater than or equal to value.
 *
 * round_down() is similar but rounds the value down instead.
 */
#define round_boundary(value, boundary)		\
	((__typeof__(value))((boundary) - 1))

#define round_up(value, boundary)		\
	((((value) - 1) | round_boundary(value, boundary)) + 1)

#define round_down(value, boundary)		\
	((value) & ~round_boundary(value, boundary))

/*
 * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
 * Both arguments must be unsigned pointer values (i.e. uintptr_t).
 */
111
112
#define check_uptr_overflow(_ptr, _inc)		\
	((_ptr) > (UINTPTR_MAX - (_inc)))
113

114
115
116
117
/*
 * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
 * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
 */
118
119
#define check_u32_overflow(_u32, _inc) \
	((_u32) > (UINT32_MAX - (_inc)))
120

121
/* Register size of the current architecture. */
122
#ifdef __aarch64__
123
#define REGSZ		U(8)
124
125
#else
#define REGSZ		U(4)
126
127
#endif

128
129
130
131
132
/*
 * Test for the current architecture version to be at least the version
 * expected.
 */
#define ARM_ARCH_AT_LEAST(_maj, _min) \
133
134
	((ARM_ARCH_MAJOR > (_maj)) || \
	 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Import an assembly or linker symbol as a C expression with the specified
 * type
 */
#define IMPORT_SYM(type, sym, name) \
	extern char sym[];\
	static const __attribute__((unused)) type name = (type) sym;

/*
 * When the symbol is used to hold a pointer, its alignment can be asserted
 * with this macro. For example, if there is a linker symbol that is going to
 * be used as a 64-bit pointer, the value of the linker symbol must also be
 * aligned to 64 bit. This macro makes sure this is the case.
 */
#define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)

152
#define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
153

154
155
156
157
158
159
160
/* Compiler builtin of GCC >= 9 and planned in llvm */
#ifdef __HAVE_SPECULATION_SAFE_VALUE
# define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
#else
# define SPECULATION_SAFE_VALUE(var) var
#endif

161
162
163
164
165
/*
 * Ticks elapsed in one second with a signal of 1 MHz
 */
#define MHZ_TICKS_PER_SEC	U(1000000)

166
167
168
169
170
/*
 * Ticks elapsed in one second with a signal of 1 KHz
 */
#define KHZ_TICKS_PER_SEC U(1000)

171
#endif /* UTILS_DEF_H */