blob: 68e464a6ca2ba9aadb078a1cce15635f1e26dd98 [file] [log] [blame]
Scott Branden53d9c9c2017-04-10 11:45:52 -07001/*
Manish V Badarkheaf1dd6e2025-03-09 11:51:21 +00002 * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved.
Varun Wadekard4b29102020-02-13 13:07:12 -08003 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
Scott Branden53d9c9c2017-04-10 11:45:52 -07004 *
dp-arm82cb2c12017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Scott Branden53d9c9c2017-04-10 11:45:52 -07006 */
7
Antonio Nino Diazf00119d2018-07-18 13:23:07 +01008#ifndef UTILS_DEF_H
9#define UTILS_DEF_H
Scott Branden53d9c9c2017-04-10 11:45:52 -070010
Julius Werner57bf6052019-05-28 21:03:58 -070011#include <export/lib/utils_def_exp.h>
12
Scott Branden53d9c9c2017-04-10 11:45:52 -070013/* Compute the number of elements in the given array */
14#define ARRAY_SIZE(a) \
15 (sizeof(a) / sizeof((a)[0]))
16
17#define IS_POWER_OF_TWO(x) \
18 (((x) & ((x) - 1)) == 0)
19
John Powell3443a702020-03-20 14:21:05 -050020#define SIZE_FROM_LOG2_WORDS(n) (U(4) << (n))
Scott Branden53d9c9c2017-04-10 11:45:52 -070021
Ghennadi Procopciuc0605b7e2024-06-18 09:41:29 +030022#if defined(__LINKER__) || defined(__ASSEMBLER__)
Yann Gautier167c5f82018-06-14 13:28:31 +020023#define BIT_32(nr) (U(1) << (nr))
24#define BIT_64(nr) (ULL(1) << (nr))
Ghennadi Procopciuc0605b7e2024-06-18 09:41:29 +030025#else
26#define BIT_32(nr) (((uint32_t)(1U)) << (nr))
27#define BIT_64(nr) (((uint64_t)(1ULL)) << (nr))
28#endif
Yann Gautier167c5f82018-06-14 13:28:31 +020029
Julius Werner402b3cf2019-07-09 14:02:43 -070030#ifdef __aarch64__
Yann Gautier167c5f82018-06-14 13:28:31 +020031#define BIT BIT_64
Julius Werner402b3cf2019-07-09 14:02:43 -070032#else
33#define BIT BIT_32
Yann Gautier167c5f82018-06-14 13:28:31 +020034#endif
Scott Branden53d9c9c2017-04-10 11:45:52 -070035
Soby Mathewebf1ca12017-07-13 15:19:51 +010036/*
Ghennadi Procopciuc8f375e42024-06-26 19:37:18 +030037 * Create a contiguous bitmask starting at bit position @low and ending at
38 * position @high. For example
Yann Gautier39676352018-06-14 18:35:33 +020039 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
40 */
Julius Wernerd5dfdeb2019-07-09 13:49:11 -070041#if defined(__LINKER__) || defined(__ASSEMBLER__)
Ghennadi Procopciuc8f375e42024-06-26 19:37:18 +030042#define GENMASK_32(high, low) \
43 (((0xFFFFFFFF) << (low)) & (0xFFFFFFFF >> (32 - 1 - (high))))
Yann Gautier46c613e2018-11-15 09:49:24 +010044
Ghennadi Procopciuc8f375e42024-06-26 19:37:18 +030045#define GENMASK_64(high, low) \
46 ((~0 << (low)) & (~0 >> (64 - 1 - (high))))
Yann Gautier46c613e2018-11-15 09:49:24 +010047#else
Ghennadi Procopciuc8f375e42024-06-26 19:37:18 +030048#define GENMASK_32(high, low) \
49 ((~UINT32_C(0) >> (32U - 1U - (high))) ^ ((BIT_32(low) - 1U)))
Yann Gautier39676352018-06-14 18:35:33 +020050
Ghennadi Procopciuc8f375e42024-06-26 19:37:18 +030051#define GENMASK_64(high, low) \
52 ((~UINT64_C(0) >> (64U - 1U - (high))) ^ ((BIT_64(low) - 1U)))
Yann Gautier46c613e2018-11-15 09:49:24 +010053#endif
Yann Gautier39676352018-06-14 18:35:33 +020054
Julius Werner402b3cf2019-07-09 14:02:43 -070055#ifdef __aarch64__
Yann Gautier39676352018-06-14 18:35:33 +020056#define GENMASK GENMASK_64
Julius Werner402b3cf2019-07-09 14:02:43 -070057#else
58#define GENMASK GENMASK_32
Yann Gautier39676352018-06-14 18:35:33 +020059#endif
60
Boyan Karatotevf9635782025-02-05 10:48:20 +000061/*
62 * Similar to GENMASK_64 but uses a named register field to compute the mask.
63 * For a register field REG_FIELD, the macros REG_FIELD_WIDTH and
64 * REG_FIELD_SHIFT must be defined.
65 */
66#define MASK(regfield) \
67 ((~0ULL >> (64ULL - (regfield##_WIDTH))) << (regfield##_SHIFT))
68
Boyan Karatotev5ee4deb2023-12-04 16:09:14 +000069#define HI(addr) (addr >> 32)
70#define LO(addr) (addr & 0xffffffff)
71
Boyan Karatotevf8138052025-04-02 11:02:44 +010072#define HI_64(addr) (addr >> 64)
73#define LO_64(addr) (addr & 0xffffffffffffffff)
74
Boyan Karatotevf9635782025-02-05 10:48:20 +000075/**
76 * EXTRACT_FIELD - Extracts a specific bit field from a value.
77 *
78 * @reg: The input value containing the field.
79
80 * @regfield: A bitmask representing the field. For a register field REG_FIELD,
81 * the macros REG_FIELD_WIDTH and REG_FIELD_SHIFT must be defined.
82
83 * The result of this macro is the contents of the field right shifted to the
84 * least significant bit positions, with the rest being zero.
85 */
86#define EXTRACT(regfield, reg) \
87 (((reg) & MASK(regfield)) >> (regfield##_SHIFT))
88
Yann Gautier39676352018-06-14 18:35:33 +020089/*
Soby Mathewebf1ca12017-07-13 15:19:51 +010090 * This variant of div_round_up can be used in macro definition but should not
91 * be used in C code as the `div` parameter is evaluated twice.
92 */
93#define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d))
94
Julius Werner7baa7bc2018-01-22 13:56:13 -080095#define div_round_up(val, div) __extension__ ({ \
96 __typeof__(div) _div = (div); \
Sathees Balyad47509d2018-09-19 14:23:03 +010097 ((val) + _div - (__typeof__(div)) 1) / _div; \
Julius Werner7baa7bc2018-01-22 13:56:13 -080098})
99
Scott Branden53d9c9c2017-04-10 11:45:52 -0700100#define MIN(x, y) __extension__ ({ \
101 __typeof__(x) _x = (x); \
102 __typeof__(y) _y = (y); \
103 (void)(&_x == &_y); \
Yann Gautier8406db12022-11-18 15:00:43 +0100104 (_x < _y) ? _x : _y; \
Scott Branden53d9c9c2017-04-10 11:45:52 -0700105})
106
107#define MAX(x, y) __extension__ ({ \
108 __typeof__(x) _x = (x); \
109 __typeof__(y) _y = (y); \
110 (void)(&_x == &_y); \
Yann Gautier8406db12022-11-18 15:00:43 +0100111 (_x > _y) ? _x : _y; \
Scott Branden53d9c9c2017-04-10 11:45:52 -0700112})
113
Lionel Debievee76d9fc2020-01-02 11:14:16 +0100114#define CLAMP(x, min, max) __extension__ ({ \
115 __typeof__(x) _x = (x); \
116 __typeof__(min) _min = (min); \
117 __typeof__(max) _max = (max); \
118 (void)(&_x == &_min); \
119 (void)(&_x == &_max); \
Yann Gautier8406db12022-11-18 15:00:43 +0100120 ((_x > _max) ? _max : ((_x < _min) ? _min : _x)); \
Lionel Debievee76d9fc2020-01-02 11:14:16 +0100121})
122
Scott Branden53d9c9c2017-04-10 11:45:52 -0700123/*
124 * The round_up() macro rounds up a value to the given boundary in a
125 * type-agnostic yet type-safe manner. The boundary must be a power of two.
126 * In other words, it computes the smallest multiple of boundary which is
127 * greater than or equal to value.
128 *
129 * round_down() is similar but rounds the value down instead.
130 */
131#define round_boundary(value, boundary) \
132 ((__typeof__(value))((boundary) - 1))
133
134#define round_up(value, boundary) \
135 ((((value) - 1) | round_boundary(value, boundary)) + 1)
136
137#define round_down(value, boundary) \
138 ((value) & ~round_boundary(value, boundary))
139
Raymond Mao3ba2c152023-07-25 07:53:35 -0700140/* add operation together with checking whether the operation overflowed
141 * The result is '*res',
142 * return 0 on success and 1 on overflow
143 */
144#define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
145
146/*
147 * Round up a value to align with a given size and
148 * check whether overflow happens.
149 * The rounduped value is '*res',
150 * return 0 on success and 1 on overflow
151 */
152#define round_up_overflow(v, size, res) (__extension__({ \
153 typeof(res) __res = res; \
154 typeof(*(__res)) __roundup_tmp = 0; \
155 typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
156 \
157 add_overflow((v), __roundup_mask, &__roundup_tmp) ? 1 : \
158 (void)(*(__res) = __roundup_tmp & ~__roundup_mask), 0; \
159}))
160
161/*
162 * Add a with b, then round up the result to align with a given size and
163 * check whether overflow happens.
164 * The rounduped value is '*res',
165 * return 0 on success and 1 on overflow
166 */
167#define add_with_round_up_overflow(a, b, size, res) (__extension__({ \
168 typeof(a) __a = (a); \
169 typeof(__a) __add_res = 0; \
170 \
171 add_overflow((__a), (b), &__add_res) ? 1 : \
172 round_up_overflow(__add_res, (size), (res)) ? 1 : 0; \
173}))
174
Marc Bonnici7e804f92022-04-19 17:42:53 +0100175/**
176 * Helper macro to ensure a value lies on a given boundary.
177 */
178#define is_aligned(value, boundary) \
179 (round_up((uintptr_t) value, boundary) == \
180 round_down((uintptr_t) value, boundary))
181
Scott Branden53d9c9c2017-04-10 11:45:52 -0700182/*
183 * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
184 * Both arguments must be unsigned pointer values (i.e. uintptr_t).
185 */
Antonio Nino Diazf00119d2018-07-18 13:23:07 +0100186#define check_uptr_overflow(_ptr, _inc) \
187 ((_ptr) > (UINTPTR_MAX - (_inc)))
Scott Branden53d9c9c2017-04-10 11:45:52 -0700188
189/*
Jeenu Viswambharan30d81c32017-12-07 08:43:05 +0000190 * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
191 * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
192 */
Antonio Nino Diazf00119d2018-07-18 13:23:07 +0100193#define check_u32_overflow(_u32, _inc) \
194 ((_u32) > (UINT32_MAX - (_inc)))
Jeenu Viswambharan30d81c32017-12-07 08:43:05 +0000195
Julius Werner155a1002017-12-12 14:23:26 -0800196/* Register size of the current architecture. */
Julius Werner402b3cf2019-07-09 14:02:43 -0700197#ifdef __aarch64__
Julius Werner155a1002017-12-12 14:23:26 -0800198#define REGSZ U(8)
Julius Werner402b3cf2019-07-09 14:02:43 -0700199#else
200#define REGSZ U(4)
Julius Werner155a1002017-12-12 14:23:26 -0800201#endif
202
Jeenu Viswambharanf45e2322017-08-16 11:44:25 +0100203/*
204 * Test for the current architecture version to be at least the version
205 * expected.
206 */
207#define ARM_ARCH_AT_LEAST(_maj, _min) \
Jeenu Viswambharan0cc7aa82018-04-27 15:06:57 +0100208 ((ARM_ARCH_MAJOR > (_maj)) || \
209 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
Jeenu Viswambharanf45e2322017-08-16 11:44:25 +0100210
Joel Hutton9f85f9e2018-03-21 11:40:57 +0000211/*
212 * Import an assembly or linker symbol as a C expression with the specified
213 * type
214 */
215#define IMPORT_SYM(type, sym, name) \
216 extern char sym[];\
217 static const __attribute__((unused)) type name = (type) sym;
218
219/*
220 * When the symbol is used to hold a pointer, its alignment can be asserted
221 * with this macro. For example, if there is a linker symbol that is going to
222 * be used as a 64-bit pointer, the value of the linker symbol must also be
223 * aligned to 64 bit. This macro makes sure this is the case.
224 */
225#define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
226
Antonio Nino Diaz932b3ae2018-11-22 15:53:17 +0000227#define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
Joel Hutton9f85f9e2018-03-21 11:40:57 +0000228
Joel Hutton9edd8912018-10-09 14:08:42 +0100229/* Compiler builtin of GCC >= 9 and planned in llvm */
230#ifdef __HAVE_SPECULATION_SAFE_VALUE
231# define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
232#else
233# define SPECULATION_SAFE_VALUE(var) var
234#endif
235
Varun Wadekard4b29102020-02-13 13:07:12 -0800236/*
237 * Ticks elapsed in one second with a signal of 1 MHz
238 */
239#define MHZ_TICKS_PER_SEC U(1000000)
240
Pankaj Gupta447a42e2020-12-09 14:02:38 +0530241/*
242 * Ticks elapsed in one second with a signal of 1 KHz
243 */
244#define KHZ_TICKS_PER_SEC U(1000)
245
Antonio Nino Diazf00119d2018-07-18 13:23:07 +0100246#endif /* UTILS_DEF_H */