xref: /rk3399_ARM-atf/include/lib/utils_def.h (revision fa8b74958e57abb7866cd4611fb62afea795aa68)
153d9c9c8SScott Branden /*
2af1dd6e1SManish V Badarkhe  * Copyright (c) 2016-2025, 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 
220605b7e8SGhennadi Procopciuc #if defined(__LINKER__) || defined(__ASSEMBLER__)
23167c5f80SYann Gautier #define BIT_32(nr)			(U(1) << (nr))
24167c5f80SYann Gautier #define BIT_64(nr)			(ULL(1) << (nr))
250605b7e8SGhennadi Procopciuc #else
260605b7e8SGhennadi Procopciuc #define BIT_32(nr)			(((uint32_t)(1U)) << (nr))
270605b7e8SGhennadi Procopciuc #define BIT_64(nr)			(((uint64_t)(1ULL)) << (nr))
280605b7e8SGhennadi 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 /*
378f375e46SGhennadi Procopciuc  * Create a contiguous bitmask starting at bit position @low and ending at
388f375e46SGhennadi Procopciuc  * position @high. For example
3939676357SYann Gautier  * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
4039676357SYann Gautier  */
41d5dfdeb6SJulius Werner #if defined(__LINKER__) || defined(__ASSEMBLER__)
428f375e46SGhennadi Procopciuc #define GENMASK_32(high, low) \
438f375e46SGhennadi Procopciuc 	(((0xFFFFFFFF) << (low)) & (0xFFFFFFFF >> (32 - 1 - (high))))
4446c613eeSYann Gautier 
458f375e46SGhennadi Procopciuc #define GENMASK_64(high, low) \
468f375e46SGhennadi Procopciuc 	((~0 << (low)) & (~0 >> (64 - 1 - (high))))
4746c613eeSYann Gautier #else
488f375e46SGhennadi Procopciuc #define GENMASK_32(high, low) \
498f375e46SGhennadi Procopciuc 	((~UINT32_C(0) >> (32U - 1U - (high))) ^ ((BIT_32(low) - 1U)))
5039676357SYann Gautier 
518f375e46SGhennadi Procopciuc #define GENMASK_64(high, low) \
528f375e46SGhennadi Procopciuc 	((~UINT64_C(0) >> (64U - 1U - (high))) ^ ((BIT_64(low) - 1U)))
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 
61f963578bSBoyan Karatotev /*
62f963578bSBoyan Karatotev  * Similar to GENMASK_64 but uses a named register field to compute the mask.
63f963578bSBoyan Karatotev  * For a register field REG_FIELD, the macros REG_FIELD_WIDTH and
64f963578bSBoyan Karatotev  * REG_FIELD_SHIFT must be defined.
65f963578bSBoyan Karatotev  */
66f963578bSBoyan Karatotev #define MASK(regfield)							\
67f963578bSBoyan Karatotev 	((~0ULL >> (64ULL - (regfield##_WIDTH))) << (regfield##_SHIFT))
68f963578bSBoyan Karatotev 
695ee4deb8SBoyan Karatotev #define HI(addr)			(addr >> 32)
705ee4deb8SBoyan Karatotev #define LO(addr)			(addr & 0xffffffff)
715ee4deb8SBoyan Karatotev 
72f8138056SBoyan Karatotev #define HI_64(addr)			(addr >> 64)
73f8138056SBoyan Karatotev #define LO_64(addr)			(addr & 0xffffffffffffffff)
74f8138056SBoyan Karatotev 
75f963578bSBoyan Karatotev /**
76f963578bSBoyan Karatotev  * EXTRACT_FIELD - Extracts a specific bit field from a value.
77f963578bSBoyan Karatotev  *
78f963578bSBoyan Karatotev  * @reg:      The input value containing the field.
79f963578bSBoyan Karatotev 
80f963578bSBoyan Karatotev  * @regfield: A bitmask representing the field. For a register field REG_FIELD,
81f963578bSBoyan Karatotev  *            the macros REG_FIELD_WIDTH and REG_FIELD_SHIFT must be defined.
82f963578bSBoyan Karatotev 
83f963578bSBoyan Karatotev  * The result of this macro is the contents of the field right shifted to the
84f963578bSBoyan Karatotev  * least significant bit positions, with the rest being zero.
85f963578bSBoyan Karatotev  */
86f963578bSBoyan Karatotev #define EXTRACT(regfield, reg) \
87f963578bSBoyan Karatotev 	(((reg) & MASK(regfield)) >> (regfield##_SHIFT))
88f963578bSBoyan Karatotev 
89d52ff2b3SArvind Ram Prakash #define UPDATE_REG_FIELD(regfield, reg, val) \
90d52ff2b3SArvind Ram Prakash 	do { \
91d52ff2b3SArvind Ram Prakash 		(reg) &= ~(MASK(regfield)); \
92d52ff2b3SArvind Ram Prakash 		(reg) |= ((uint64_t)(val) << (regfield##_SHIFT)); \
93d52ff2b3SArvind Ram Prakash 	} while (0)
94d52ff2b3SArvind Ram Prakash 
9539676357SYann Gautier /*
96ebf1ca10SSoby Mathew  * This variant of div_round_up can be used in macro definition but should not
97ebf1ca10SSoby Mathew  * be used in C code as the `div` parameter is evaluated twice.
98ebf1ca10SSoby Mathew  */
99ebf1ca10SSoby Mathew #define DIV_ROUND_UP_2EVAL(n, d)	(((n) + (d) - 1) / (d))
100ebf1ca10SSoby Mathew 
1017baa7bcaSJulius Werner #define div_round_up(val, div) __extension__ ({	\
1027baa7bcaSJulius Werner 	__typeof__(div) _div = (div);		\
103d47509d6SSathees Balya 	((val) + _div - (__typeof__(div)) 1) / _div;		\
1047baa7bcaSJulius Werner })
1057baa7bcaSJulius Werner 
10653d9c9c8SScott Branden #define MIN(x, y) __extension__ ({	\
10753d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
10853d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
10953d9c9c8SScott Branden 	(void)(&_x == &_y);		\
1108406db14SYann Gautier 	(_x < _y) ? _x : _y;		\
11153d9c9c8SScott Branden })
11253d9c9c8SScott Branden 
11353d9c9c8SScott Branden #define MAX(x, y) __extension__ ({	\
11453d9c9c8SScott Branden 	__typeof__(x) _x = (x);		\
11553d9c9c8SScott Branden 	__typeof__(y) _y = (y);		\
11653d9c9c8SScott Branden 	(void)(&_x == &_y);		\
1178406db14SYann Gautier 	(_x > _y) ? _x : _y;		\
11853d9c9c8SScott Branden })
11953d9c9c8SScott Branden 
120e76d9fc4SLionel Debieve #define CLAMP(x, min, max) __extension__ ({ \
121e76d9fc4SLionel Debieve 	__typeof__(x) _x = (x); \
122e76d9fc4SLionel Debieve 	__typeof__(min) _min = (min); \
123e76d9fc4SLionel Debieve 	__typeof__(max) _max = (max); \
124e76d9fc4SLionel Debieve 	(void)(&_x == &_min); \
125e76d9fc4SLionel Debieve 	(void)(&_x == &_max); \
1268406db14SYann Gautier 	((_x > _max) ? _max : ((_x < _min) ? _min : _x)); \
127e76d9fc4SLionel Debieve })
128e76d9fc4SLionel Debieve 
12953d9c9c8SScott Branden /*
13053d9c9c8SScott Branden  * The round_up() macro rounds up a value to the given boundary in a
13153d9c9c8SScott Branden  * type-agnostic yet type-safe manner. The boundary must be a power of two.
13253d9c9c8SScott Branden  * In other words, it computes the smallest multiple of boundary which is
13353d9c9c8SScott Branden  * greater than or equal to value.
13453d9c9c8SScott Branden  *
13553d9c9c8SScott Branden  * round_down() is similar but rounds the value down instead.
13653d9c9c8SScott Branden  */
13753d9c9c8SScott Branden #define round_boundary(value, boundary)		\
138*fa8b7495SKhristine Andreea Barbulescu 	((__typeof__(value))((boundary) - ((__typeof__(value))1U)))
13953d9c9c8SScott Branden 
14053d9c9c8SScott Branden #define round_up(value, boundary)		\
141*fa8b7495SKhristine Andreea Barbulescu 	((((value) - ((__typeof__(value))1U)) | round_boundary(value, boundary)) + ((__typeof__(value))1U))
14253d9c9c8SScott Branden 
14353d9c9c8SScott Branden #define round_down(value, boundary)		\
14453d9c9c8SScott Branden 	((value) & ~round_boundary(value, boundary))
14553d9c9c8SScott Branden 
1463ba2c151SRaymond Mao /* add operation together with checking whether the operation overflowed
1473ba2c151SRaymond Mao  * The result is '*res',
1483ba2c151SRaymond Mao  * return 0 on success and 1 on overflow
1493ba2c151SRaymond Mao  */
1503ba2c151SRaymond Mao #define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
1513ba2c151SRaymond Mao 
1523ba2c151SRaymond Mao /*
1533ba2c151SRaymond Mao  * Round up a value to align with a given size and
1543ba2c151SRaymond Mao  * check whether overflow happens.
1553ba2c151SRaymond Mao  * The rounduped value is '*res',
1563ba2c151SRaymond Mao  * return 0 on success and 1 on overflow
1573ba2c151SRaymond Mao  */
1583ba2c151SRaymond Mao #define round_up_overflow(v, size, res) (__extension__({ \
1593ba2c151SRaymond Mao 	typeof(res) __res = res; \
1603ba2c151SRaymond Mao 	typeof(*(__res)) __roundup_tmp = 0; \
1613ba2c151SRaymond Mao 	typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
1623ba2c151SRaymond Mao 	\
1633ba2c151SRaymond Mao 	add_overflow((v), __roundup_mask, &__roundup_tmp) ? 1 : \
1643ba2c151SRaymond Mao 		(void)(*(__res) = __roundup_tmp & ~__roundup_mask), 0; \
1653ba2c151SRaymond Mao }))
1663ba2c151SRaymond Mao 
1673ba2c151SRaymond Mao /*
1683ba2c151SRaymond Mao  * Add a with b, then round up the result to align with a given size and
1693ba2c151SRaymond Mao  * check whether overflow happens.
1703ba2c151SRaymond Mao  * The rounduped value is '*res',
1713ba2c151SRaymond Mao  * return 0 on success and 1 on overflow
1723ba2c151SRaymond Mao  */
1733ba2c151SRaymond Mao #define add_with_round_up_overflow(a, b, size, res) (__extension__({ \
1743ba2c151SRaymond Mao 	typeof(a) __a = (a); \
1753ba2c151SRaymond Mao 	typeof(__a) __add_res = 0; \
1763ba2c151SRaymond Mao 	\
1773ba2c151SRaymond Mao 	add_overflow((__a), (b), &__add_res) ? 1 : \
1783ba2c151SRaymond Mao 		round_up_overflow(__add_res, (size), (res)) ? 1 : 0; \
1793ba2c151SRaymond Mao }))
1803ba2c151SRaymond Mao 
1817e804f96SMarc Bonnici /**
1827e804f96SMarc Bonnici  * Helper macro to ensure a value lies on a given boundary.
1837e804f96SMarc Bonnici  */
1847e804f96SMarc Bonnici #define is_aligned(value, boundary)			\
1857e804f96SMarc Bonnici 	(round_up((uintptr_t) value, boundary) ==	\
1867e804f96SMarc Bonnici 	 round_down((uintptr_t) value, boundary))
1877e804f96SMarc Bonnici 
18853d9c9c8SScott Branden /*
18953d9c9c8SScott Branden  * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
19053d9c9c8SScott Branden  * Both arguments must be unsigned pointer values (i.e. uintptr_t).
19153d9c9c8SScott Branden  */
192f00119deSAntonio Nino Diaz #define check_uptr_overflow(_ptr, _inc)		\
193f00119deSAntonio Nino Diaz 	((_ptr) > (UINTPTR_MAX - (_inc)))
19453d9c9c8SScott Branden 
19553d9c9c8SScott Branden /*
19630d81c36SJeenu Viswambharan  * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
19730d81c36SJeenu Viswambharan  * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
19830d81c36SJeenu Viswambharan  */
199f00119deSAntonio Nino Diaz #define check_u32_overflow(_u32, _inc) \
200f00119deSAntonio Nino Diaz 	((_u32) > (UINT32_MAX - (_inc)))
20130d81c36SJeenu Viswambharan 
202155a1006SJulius Werner /* Register size of the current architecture. */
203402b3cf8SJulius Werner #ifdef __aarch64__
204155a1006SJulius Werner #define REGSZ		U(8)
205402b3cf8SJulius Werner #else
206402b3cf8SJulius Werner #define REGSZ		U(4)
207155a1006SJulius Werner #endif
208155a1006SJulius Werner 
209f45e232aSJeenu Viswambharan /*
210f45e232aSJeenu Viswambharan  * Test for the current architecture version to be at least the version
211f45e232aSJeenu Viswambharan  * expected.
212f45e232aSJeenu Viswambharan  */
213f45e232aSJeenu Viswambharan #define ARM_ARCH_AT_LEAST(_maj, _min) \
2140cc7aa89SJeenu Viswambharan 	((ARM_ARCH_MAJOR > (_maj)) || \
2150cc7aa89SJeenu Viswambharan 	 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
216f45e232aSJeenu Viswambharan 
2179f85f9e3SJoel Hutton /*
2189f85f9e3SJoel Hutton  * Import an assembly or linker symbol as a C expression with the specified
2199f85f9e3SJoel Hutton  * type
2209f85f9e3SJoel Hutton  */
2219f85f9e3SJoel Hutton #define IMPORT_SYM(type, sym, name) \
2229f85f9e3SJoel Hutton 	extern char sym[];\
2239f85f9e3SJoel Hutton 	static const __attribute__((unused)) type name = (type) sym;
2249f85f9e3SJoel Hutton 
2259f85f9e3SJoel Hutton /*
2269f85f9e3SJoel Hutton  * When the symbol is used to hold a pointer, its alignment can be asserted
2279f85f9e3SJoel Hutton  * with this macro. For example, if there is a linker symbol that is going to
2289f85f9e3SJoel Hutton  * be used as a 64-bit pointer, the value of the linker symbol must also be
2299f85f9e3SJoel Hutton  * aligned to 64 bit. This macro makes sure this is the case.
2309f85f9e3SJoel Hutton  */
2319f85f9e3SJoel Hutton #define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
2329f85f9e3SJoel Hutton 
233932b3ae2SAntonio Nino Diaz #define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
2349f85f9e3SJoel Hutton 
2359edd8912SJoel Hutton /* Compiler builtin of GCC >= 9 and planned in llvm */
2369edd8912SJoel Hutton #ifdef __HAVE_SPECULATION_SAFE_VALUE
2379edd8912SJoel Hutton # define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
2389edd8912SJoel Hutton #else
2399edd8912SJoel Hutton # define SPECULATION_SAFE_VALUE(var) var
2409edd8912SJoel Hutton #endif
2419edd8912SJoel Hutton 
242d4b29105SVarun Wadekar /*
243d4b29105SVarun Wadekar  * Ticks elapsed in one second with a signal of 1 MHz
244d4b29105SVarun Wadekar  */
245d4b29105SVarun Wadekar #define MHZ_TICKS_PER_SEC	U(1000000)
246d4b29105SVarun Wadekar 
247447a42e7SPankaj Gupta /*
248447a42e7SPankaj Gupta  * Ticks elapsed in one second with a signal of 1 KHz
249447a42e7SPankaj Gupta  */
250447a42e7SPankaj Gupta #define KHZ_TICKS_PER_SEC U(1000)
251447a42e7SPankaj Gupta 
252f00119deSAntonio Nino Diaz #endif /* UTILS_DEF_H */
253