xref: /rk3399_ARM-atf/include/lib/utils_def.h (revision 402b3cf8766fe2cb4ae462f7ee7761d08a1ba56c)
153d9c9c8SScott Branden /*
257bf6057SJulius Werner  * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
353d9c9c8SScott Branden  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
553d9c9c8SScott Branden  */
653d9c9c8SScott Branden 
7f00119deSAntonio Nino Diaz #ifndef UTILS_DEF_H
8f00119deSAntonio Nino Diaz #define UTILS_DEF_H
953d9c9c8SScott Branden 
1057bf6057SJulius Werner #include <export/lib/utils_def_exp.h>
1157bf6057SJulius Werner 
1253d9c9c8SScott Branden /* Compute the number of elements in the given array */
1353d9c9c8SScott Branden #define ARRAY_SIZE(a)				\
1453d9c9c8SScott Branden 	(sizeof(a) / sizeof((a)[0]))
1553d9c9c8SScott Branden 
1653d9c9c8SScott Branden #define IS_POWER_OF_TWO(x)			\
1753d9c9c8SScott Branden 	(((x) & ((x) - 1)) == 0)
1853d9c9c8SScott Branden 
1953d9c9c8SScott Branden #define SIZE_FROM_LOG2_WORDS(n)		(4 << (n))
2053d9c9c8SScott Branden 
21167c5f80SYann Gautier #define BIT_32(nr)			(U(1) << (nr))
22167c5f80SYann Gautier #define BIT_64(nr)			(ULL(1) << (nr))
23167c5f80SYann Gautier 
24*402b3cf8SJulius Werner #ifdef __aarch64__
25167c5f80SYann Gautier #define BIT				BIT_64
26*402b3cf8SJulius Werner #else
27*402b3cf8SJulius Werner #define BIT				BIT_32
28167c5f80SYann Gautier #endif
2953d9c9c8SScott Branden 
30ebf1ca10SSoby Mathew /*
3139676357SYann Gautier  * Create a contiguous bitmask starting at bit position @l and ending at
3239676357SYann Gautier  * position @h. For example
3339676357SYann Gautier  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
3439676357SYann Gautier  */
35d5dfdeb6SJulius Werner #if defined(__LINKER__) || defined(__ASSEMBLER__)
3646c613eeSYann Gautier #define GENMASK_32(h, l) \
3746c613eeSYann Gautier 	(((0xFFFFFFFF) << (l)) & (0xFFFFFFFF >> (32 - 1 - (h))))
3846c613eeSYann Gautier 
3946c613eeSYann Gautier #define GENMASK_64(h, l) \
4046c613eeSYann Gautier 	((~0 << (l)) & (~0 >> (64 - 1 - (h))))
4146c613eeSYann Gautier #else
4239676357SYann Gautier #define GENMASK_32(h, l) \
4339676357SYann Gautier 	(((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
4439676357SYann Gautier 
4539676357SYann Gautier #define GENMASK_64(h, l) \
4639676357SYann Gautier 	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
4746c613eeSYann Gautier #endif
4839676357SYann Gautier 
49*402b3cf8SJulius Werner #ifdef __aarch64__
5039676357SYann Gautier #define GENMASK				GENMASK_64
51*402b3cf8SJulius Werner #else
52*402b3cf8SJulius Werner #define GENMASK				GENMASK_32
5339676357SYann Gautier #endif
5439676357SYann Gautier 
5539676357SYann Gautier /*
56ebf1ca10SSoby Mathew  * This variant of div_round_up can be used in macro definition but should not
57ebf1ca10SSoby Mathew  * be used in C code as the `div` parameter is evaluated twice.
58ebf1ca10SSoby Mathew  */
59ebf1ca10SSoby Mathew #define DIV_ROUND_UP_2EVAL(n, d)	(((n) + (d) - 1) / (d))
60ebf1ca10SSoby Mathew 
617baa7bcaSJulius Werner #define div_round_up(val, div) __extension__ ({	\
627baa7bcaSJulius Werner 	__typeof__(div) _div = (div);		\
63d47509d6SSathees Balya 	((val) + _div - (__typeof__(div)) 1) / _div;		\
647baa7bcaSJulius Werner })
657baa7bcaSJulius Werner 
6653d9c9c8SScott Branden #define MIN(x, y) __extension__ ({	\
6753d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
6853d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
6953d9c9c8SScott Branden 	(void)(&_x == &_y);		\
7053d9c9c8SScott Branden 	_x < _y ? _x : _y;		\
7153d9c9c8SScott Branden })
7253d9c9c8SScott Branden 
7353d9c9c8SScott Branden #define MAX(x, y) __extension__ ({	\
7453d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
7553d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
7653d9c9c8SScott Branden 	(void)(&_x == &_y);		\
7753d9c9c8SScott Branden 	_x > _y ? _x : _y;		\
7853d9c9c8SScott Branden })
7953d9c9c8SScott Branden 
8053d9c9c8SScott Branden /*
8153d9c9c8SScott Branden  * The round_up() macro rounds up a value to the given boundary in a
8253d9c9c8SScott Branden  * type-agnostic yet type-safe manner. The boundary must be a power of two.
8353d9c9c8SScott Branden  * In other words, it computes the smallest multiple of boundary which is
8453d9c9c8SScott Branden  * greater than or equal to value.
8553d9c9c8SScott Branden  *
8653d9c9c8SScott Branden  * round_down() is similar but rounds the value down instead.
8753d9c9c8SScott Branden  */
8853d9c9c8SScott Branden #define round_boundary(value, boundary)		\
8953d9c9c8SScott Branden 	((__typeof__(value))((boundary) - 1))
9053d9c9c8SScott Branden 
9153d9c9c8SScott Branden #define round_up(value, boundary)		\
9253d9c9c8SScott Branden 	((((value) - 1) | round_boundary(value, boundary)) + 1)
9353d9c9c8SScott Branden 
9453d9c9c8SScott Branden #define round_down(value, boundary)		\
9553d9c9c8SScott Branden 	((value) & ~round_boundary(value, boundary))
9653d9c9c8SScott Branden 
9753d9c9c8SScott Branden /*
9853d9c9c8SScott Branden  * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
9953d9c9c8SScott Branden  * Both arguments must be unsigned pointer values (i.e. uintptr_t).
10053d9c9c8SScott Branden  */
101f00119deSAntonio Nino Diaz #define check_uptr_overflow(_ptr, _inc)		\
102f00119deSAntonio Nino Diaz 	((_ptr) > (UINTPTR_MAX - (_inc)))
10353d9c9c8SScott Branden 
10453d9c9c8SScott Branden /*
10530d81c36SJeenu Viswambharan  * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
10630d81c36SJeenu Viswambharan  * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
10730d81c36SJeenu Viswambharan  */
108f00119deSAntonio Nino Diaz #define check_u32_overflow(_u32, _inc) \
109f00119deSAntonio Nino Diaz 	((_u32) > (UINT32_MAX - (_inc)))
11030d81c36SJeenu Viswambharan 
111155a1006SJulius Werner /* Register size of the current architecture. */
112*402b3cf8SJulius Werner #ifdef __aarch64__
113155a1006SJulius Werner #define REGSZ		U(8)
114*402b3cf8SJulius Werner #else
115*402b3cf8SJulius Werner #define REGSZ		U(4)
116155a1006SJulius Werner #endif
117155a1006SJulius Werner 
118f45e232aSJeenu Viswambharan /*
119f45e232aSJeenu Viswambharan  * Test for the current architecture version to be at least the version
120f45e232aSJeenu Viswambharan  * expected.
121f45e232aSJeenu Viswambharan  */
122f45e232aSJeenu Viswambharan #define ARM_ARCH_AT_LEAST(_maj, _min) \
1230cc7aa89SJeenu Viswambharan 	((ARM_ARCH_MAJOR > (_maj)) || \
1240cc7aa89SJeenu Viswambharan 	 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
125f45e232aSJeenu Viswambharan 
1269f85f9e3SJoel Hutton /*
1279f85f9e3SJoel Hutton  * Import an assembly or linker symbol as a C expression with the specified
1289f85f9e3SJoel Hutton  * type
1299f85f9e3SJoel Hutton  */
1309f85f9e3SJoel Hutton #define IMPORT_SYM(type, sym, name) \
1319f85f9e3SJoel Hutton 	extern char sym[];\
1329f85f9e3SJoel Hutton 	static const __attribute__((unused)) type name = (type) sym;
1339f85f9e3SJoel Hutton 
1349f85f9e3SJoel Hutton /*
1359f85f9e3SJoel Hutton  * When the symbol is used to hold a pointer, its alignment can be asserted
1369f85f9e3SJoel Hutton  * with this macro. For example, if there is a linker symbol that is going to
1379f85f9e3SJoel Hutton  * be used as a 64-bit pointer, the value of the linker symbol must also be
1389f85f9e3SJoel Hutton  * aligned to 64 bit. This macro makes sure this is the case.
1399f85f9e3SJoel Hutton  */
1409f85f9e3SJoel Hutton #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
1419f85f9e3SJoel Hutton 
142932b3ae2SAntonio Nino Diaz #define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
1439f85f9e3SJoel Hutton 
1449edd8912SJoel Hutton /* Compiler builtin of GCC >= 9 and planned in llvm */
1459edd8912SJoel Hutton #ifdef __HAVE_SPECULATION_SAFE_VALUE
1469edd8912SJoel Hutton # define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
1479edd8912SJoel Hutton #else
1489edd8912SJoel Hutton # define SPECULATION_SAFE_VALUE(var) var
1499edd8912SJoel Hutton #endif
1509edd8912SJoel Hutton 
151f00119deSAntonio Nino Diaz #endif /* UTILS_DEF_H */
152