xref: /rk3399_ARM-atf/include/lib/utils_def.h (revision 0605b7e8af4980d4e26afc6720dcbf2644633c53)
153d9c9c8SScott Branden /*
25ee4deb8SBoyan Karatotev  * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
3d4b29105SVarun Wadekar  * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
453d9c9c8SScott Branden  *
582cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
653d9c9c8SScott Branden  */
753d9c9c8SScott Branden 
8f00119deSAntonio Nino Diaz #ifndef UTILS_DEF_H
9f00119deSAntonio Nino Diaz #define UTILS_DEF_H
1053d9c9c8SScott Branden 
1157bf6057SJulius Werner #include <export/lib/utils_def_exp.h>
1257bf6057SJulius Werner 
1353d9c9c8SScott Branden /* Compute the number of elements in the given array */
1453d9c9c8SScott Branden #define ARRAY_SIZE(a)				\
1553d9c9c8SScott Branden 	(sizeof(a) / sizeof((a)[0]))
1653d9c9c8SScott Branden 
1753d9c9c8SScott Branden #define IS_POWER_OF_TWO(x)			\
1853d9c9c8SScott Branden 	(((x) & ((x) - 1)) == 0)
1953d9c9c8SScott Branden 
203443a702SJohn Powell #define SIZE_FROM_LOG2_WORDS(n)		(U(4) << (n))
2153d9c9c8SScott Branden 
22*0605b7e8SGhennadi Procopciuc #if defined(__LINKER__) || defined(__ASSEMBLER__)
23167c5f80SYann Gautier #define BIT_32(nr)			(U(1) << (nr))
24167c5f80SYann Gautier #define BIT_64(nr)			(ULL(1) << (nr))
25*0605b7e8SGhennadi Procopciuc #else
26*0605b7e8SGhennadi Procopciuc #define BIT_32(nr)			(((uint32_t)(1U)) << (nr))
27*0605b7e8SGhennadi Procopciuc #define BIT_64(nr)			(((uint64_t)(1ULL)) << (nr))
28*0605b7e8SGhennadi Procopciuc #endif
29167c5f80SYann Gautier 
30402b3cf8SJulius Werner #ifdef __aarch64__
31167c5f80SYann Gautier #define BIT				BIT_64
32402b3cf8SJulius Werner #else
33402b3cf8SJulius Werner #define BIT				BIT_32
34167c5f80SYann Gautier #endif
3553d9c9c8SScott Branden 
36ebf1ca10SSoby Mathew /*
3739676357SYann Gautier  * Create a contiguous bitmask starting at bit position @l and ending at
3839676357SYann Gautier  * position @h. For example
3939676357SYann Gautier  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
4039676357SYann Gautier  */
41d5dfdeb6SJulius Werner #if defined(__LINKER__) || defined(__ASSEMBLER__)
4246c613eeSYann Gautier #define GENMASK_32(h, l) \
4346c613eeSYann Gautier 	(((0xFFFFFFFF) << (l)) & (0xFFFFFFFF >> (32 - 1 - (h))))
4446c613eeSYann Gautier 
4546c613eeSYann Gautier #define GENMASK_64(h, l) \
4646c613eeSYann Gautier 	((~0 << (l)) & (~0 >> (64 - 1 - (h))))
4746c613eeSYann Gautier #else
4839676357SYann Gautier #define GENMASK_32(h, l) \
4939676357SYann Gautier 	(((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
5039676357SYann Gautier 
5139676357SYann Gautier #define GENMASK_64(h, l) \
5239676357SYann Gautier 	(((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
5346c613eeSYann Gautier #endif
5439676357SYann Gautier 
55402b3cf8SJulius Werner #ifdef __aarch64__
5639676357SYann Gautier #define GENMASK				GENMASK_64
57402b3cf8SJulius Werner #else
58402b3cf8SJulius Werner #define GENMASK				GENMASK_32
5939676357SYann Gautier #endif
6039676357SYann Gautier 
615ee4deb8SBoyan Karatotev #define HI(addr)			(addr >> 32)
625ee4deb8SBoyan Karatotev #define LO(addr)			(addr & 0xffffffff)
635ee4deb8SBoyan Karatotev 
6439676357SYann Gautier /*
65ebf1ca10SSoby Mathew  * This variant of div_round_up can be used in macro definition but should not
66ebf1ca10SSoby Mathew  * be used in C code as the `div` parameter is evaluated twice.
67ebf1ca10SSoby Mathew  */
68ebf1ca10SSoby Mathew #define DIV_ROUND_UP_2EVAL(n, d)	(((n) + (d) - 1) / (d))
69ebf1ca10SSoby Mathew 
707baa7bcaSJulius Werner #define div_round_up(val, div) __extension__ ({	\
717baa7bcaSJulius Werner 	__typeof__(div) _div = (div);		\
72d47509d6SSathees Balya 	((val) + _div - (__typeof__(div)) 1) / _div;		\
737baa7bcaSJulius Werner })
747baa7bcaSJulius Werner 
7553d9c9c8SScott Branden #define MIN(x, y) __extension__ ({	\
7653d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
7753d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
7853d9c9c8SScott Branden 	(void)(&_x == &_y);		\
798406db14SYann Gautier 	(_x < _y) ? _x : _y;		\
8053d9c9c8SScott Branden })
8153d9c9c8SScott Branden 
8253d9c9c8SScott Branden #define MAX(x, y) __extension__ ({	\
8353d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
8453d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
8553d9c9c8SScott Branden 	(void)(&_x == &_y);		\
868406db14SYann Gautier 	(_x > _y) ? _x : _y;		\
8753d9c9c8SScott Branden })
8853d9c9c8SScott Branden 
89e76d9fc4SLionel Debieve #define CLAMP(x, min, max) __extension__ ({ \
90e76d9fc4SLionel Debieve 	__typeof__(x) _x = (x); \
91e76d9fc4SLionel Debieve 	__typeof__(min) _min = (min); \
92e76d9fc4SLionel Debieve 	__typeof__(max) _max = (max); \
93e76d9fc4SLionel Debieve 	(void)(&_x == &_min); \
94e76d9fc4SLionel Debieve 	(void)(&_x == &_max); \
958406db14SYann Gautier 	((_x > _max) ? _max : ((_x < _min) ? _min : _x)); \
96e76d9fc4SLionel Debieve })
97e76d9fc4SLionel Debieve 
9853d9c9c8SScott Branden /*
9953d9c9c8SScott Branden  * The round_up() macro rounds up a value to the given boundary in a
10053d9c9c8SScott Branden  * type-agnostic yet type-safe manner. The boundary must be a power of two.
10153d9c9c8SScott Branden  * In other words, it computes the smallest multiple of boundary which is
10253d9c9c8SScott Branden  * greater than or equal to value.
10353d9c9c8SScott Branden  *
10453d9c9c8SScott Branden  * round_down() is similar but rounds the value down instead.
10553d9c9c8SScott Branden  */
10653d9c9c8SScott Branden #define round_boundary(value, boundary)		\
10753d9c9c8SScott Branden 	((__typeof__(value))((boundary) - 1))
10853d9c9c8SScott Branden 
10953d9c9c8SScott Branden #define round_up(value, boundary)		\
11053d9c9c8SScott Branden 	((((value) - 1) | round_boundary(value, boundary)) + 1)
11153d9c9c8SScott Branden 
11253d9c9c8SScott Branden #define round_down(value, boundary)		\
11353d9c9c8SScott Branden 	((value) & ~round_boundary(value, boundary))
11453d9c9c8SScott Branden 
1153ba2c151SRaymond Mao /* add operation together with checking whether the operation overflowed
1163ba2c151SRaymond Mao  * The result is '*res',
1173ba2c151SRaymond Mao  * return 0 on success and 1 on overflow
1183ba2c151SRaymond Mao  */
1193ba2c151SRaymond Mao #define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
1203ba2c151SRaymond Mao 
1213ba2c151SRaymond Mao /*
1223ba2c151SRaymond Mao  * Round up a value to align with a given size and
1233ba2c151SRaymond Mao  * check whether overflow happens.
1243ba2c151SRaymond Mao  * The rounduped value is '*res',
1253ba2c151SRaymond Mao  * return 0 on success and 1 on overflow
1263ba2c151SRaymond Mao  */
1273ba2c151SRaymond Mao #define round_up_overflow(v, size, res) (__extension__({ \
1283ba2c151SRaymond Mao 	typeof(res) __res = res; \
1293ba2c151SRaymond Mao 	typeof(*(__res)) __roundup_tmp = 0; \
1303ba2c151SRaymond Mao 	typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
1313ba2c151SRaymond Mao 	\
1323ba2c151SRaymond Mao 	add_overflow((v), __roundup_mask, &__roundup_tmp) ? 1 : \
1333ba2c151SRaymond Mao 		(void)(*(__res) = __roundup_tmp & ~__roundup_mask), 0; \
1343ba2c151SRaymond Mao }))
1353ba2c151SRaymond Mao 
1363ba2c151SRaymond Mao /*
1373ba2c151SRaymond Mao  * Add a with b, then round up the result to align with a given size and
1383ba2c151SRaymond Mao  * check whether overflow happens.
1393ba2c151SRaymond Mao  * The rounduped value is '*res',
1403ba2c151SRaymond Mao  * return 0 on success and 1 on overflow
1413ba2c151SRaymond Mao  */
1423ba2c151SRaymond Mao #define add_with_round_up_overflow(a, b, size, res) (__extension__({ \
1433ba2c151SRaymond Mao 	typeof(a) __a = (a); \
1443ba2c151SRaymond Mao 	typeof(__a) __add_res = 0; \
1453ba2c151SRaymond Mao 	\
1463ba2c151SRaymond Mao 	add_overflow((__a), (b), &__add_res) ? 1 : \
1473ba2c151SRaymond Mao 		round_up_overflow(__add_res, (size), (res)) ? 1 : 0; \
1483ba2c151SRaymond Mao }))
1493ba2c151SRaymond Mao 
1507e804f96SMarc Bonnici /**
1517e804f96SMarc Bonnici  * Helper macro to ensure a value lies on a given boundary.
1527e804f96SMarc Bonnici  */
1537e804f96SMarc Bonnici #define is_aligned(value, boundary)			\
1547e804f96SMarc Bonnici 	(round_up((uintptr_t) value, boundary) ==	\
1557e804f96SMarc Bonnici 	 round_down((uintptr_t) value, boundary))
1567e804f96SMarc Bonnici 
15753d9c9c8SScott Branden /*
15853d9c9c8SScott Branden  * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
15953d9c9c8SScott Branden  * Both arguments must be unsigned pointer values (i.e. uintptr_t).
16053d9c9c8SScott Branden  */
161f00119deSAntonio Nino Diaz #define check_uptr_overflow(_ptr, _inc)		\
162f00119deSAntonio Nino Diaz 	((_ptr) > (UINTPTR_MAX - (_inc)))
16353d9c9c8SScott Branden 
16453d9c9c8SScott Branden /*
16530d81c36SJeenu Viswambharan  * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
16630d81c36SJeenu Viswambharan  * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
16730d81c36SJeenu Viswambharan  */
168f00119deSAntonio Nino Diaz #define check_u32_overflow(_u32, _inc) \
169f00119deSAntonio Nino Diaz 	((_u32) > (UINT32_MAX - (_inc)))
17030d81c36SJeenu Viswambharan 
171155a1006SJulius Werner /* Register size of the current architecture. */
172402b3cf8SJulius Werner #ifdef __aarch64__
173155a1006SJulius Werner #define REGSZ		U(8)
174402b3cf8SJulius Werner #else
175402b3cf8SJulius Werner #define REGSZ		U(4)
176155a1006SJulius Werner #endif
177155a1006SJulius Werner 
178f45e232aSJeenu Viswambharan /*
179f45e232aSJeenu Viswambharan  * Test for the current architecture version to be at least the version
180f45e232aSJeenu Viswambharan  * expected.
181f45e232aSJeenu Viswambharan  */
182f45e232aSJeenu Viswambharan #define ARM_ARCH_AT_LEAST(_maj, _min) \
1830cc7aa89SJeenu Viswambharan 	((ARM_ARCH_MAJOR > (_maj)) || \
1840cc7aa89SJeenu Viswambharan 	 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
185f45e232aSJeenu Viswambharan 
1869f85f9e3SJoel Hutton /*
1879f85f9e3SJoel Hutton  * Import an assembly or linker symbol as a C expression with the specified
1889f85f9e3SJoel Hutton  * type
1899f85f9e3SJoel Hutton  */
1909f85f9e3SJoel Hutton #define IMPORT_SYM(type, sym, name) \
1919f85f9e3SJoel Hutton 	extern char sym[];\
1929f85f9e3SJoel Hutton 	static const __attribute__((unused)) type name = (type) sym;
1939f85f9e3SJoel Hutton 
1949f85f9e3SJoel Hutton /*
1959f85f9e3SJoel Hutton  * When the symbol is used to hold a pointer, its alignment can be asserted
1969f85f9e3SJoel Hutton  * with this macro. For example, if there is a linker symbol that is going to
1979f85f9e3SJoel Hutton  * be used as a 64-bit pointer, the value of the linker symbol must also be
1989f85f9e3SJoel Hutton  * aligned to 64 bit. This macro makes sure this is the case.
1999f85f9e3SJoel Hutton  */
2009f85f9e3SJoel Hutton #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
2019f85f9e3SJoel Hutton 
202932b3ae2SAntonio Nino Diaz #define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
2039f85f9e3SJoel Hutton 
2049edd8912SJoel Hutton /* Compiler builtin of GCC >= 9 and planned in llvm */
2059edd8912SJoel Hutton #ifdef __HAVE_SPECULATION_SAFE_VALUE
2069edd8912SJoel Hutton # define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
2079edd8912SJoel Hutton #else
2089edd8912SJoel Hutton # define SPECULATION_SAFE_VALUE(var) var
2099edd8912SJoel Hutton #endif
2109edd8912SJoel Hutton 
211d4b29105SVarun Wadekar /*
212d4b29105SVarun Wadekar  * Ticks elapsed in one second with a signal of 1 MHz
213d4b29105SVarun Wadekar  */
214d4b29105SVarun Wadekar #define MHZ_TICKS_PER_SEC	U(1000000)
215d4b29105SVarun Wadekar 
216447a42e7SPankaj Gupta /*
217447a42e7SPankaj Gupta  * Ticks elapsed in one second with a signal of 1 KHz
218447a42e7SPankaj Gupta  */
219447a42e7SPankaj Gupta #define KHZ_TICKS_PER_SEC U(1000)
220447a42e7SPankaj Gupta 
221f00119deSAntonio Nino Diaz #endif /* UTILS_DEF_H */
222