blob: 23f59bdc3319ef49e0ec4744e8c6f359446ca553 [file] [log] [blame]
Scott Brandenbf404c02017-04-10 11:45:52 -07001/*
Julius Werner2a231e32019-05-28 21:03:58 -07002 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
Scott Brandenbf404c02017-04-10 11:45:52 -07003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Scott Brandenbf404c02017-04-10 11:45:52 -07005 */
6
Antonio Nino Diazbb77f882018-07-18 13:23:07 +01007#ifndef UTILS_DEF_H
8#define UTILS_DEF_H
Scott Brandenbf404c02017-04-10 11:45:52 -07009
Julius Werner2a231e32019-05-28 21:03:58 -070010#include <export/lib/utils_def_exp.h>
11
Scott Brandenbf404c02017-04-10 11:45:52 -070012/* Compute the number of elements in the given array */
13#define ARRAY_SIZE(a) \
14 (sizeof(a) / sizeof((a)[0]))
15
16#define IS_POWER_OF_TWO(x) \
17 (((x) & ((x) - 1)) == 0)
18
19#define SIZE_FROM_LOG2_WORDS(n) (4 << (n))
20
Yann Gautierc70fed52018-06-14 13:28:31 +020021#define BIT_32(nr) (U(1) << (nr))
22#define BIT_64(nr) (ULL(1) << (nr))
23
Julius Werner8e0ef0f2019-07-09 14:02:43 -070024#ifdef __aarch64__
Yann Gautierc70fed52018-06-14 13:28:31 +020025#define BIT BIT_64
Julius Werner8e0ef0f2019-07-09 14:02:43 -070026#else
27#define BIT BIT_32
Yann Gautierc70fed52018-06-14 13:28:31 +020028#endif
Scott Brandenbf404c02017-04-10 11:45:52 -070029
Soby Mathew327548c2017-07-13 15:19:51 +010030/*
Yann Gautier953e94d2018-06-14 18:35:33 +020031 * Create a contiguous bitmask starting at bit position @l and ending at
32 * position @h. For example
33 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
34 */
Julius Werner53456fc2019-07-09 13:49:11 -070035#if defined(__LINKER__) || defined(__ASSEMBLER__)
Yann Gautierd2c9a682018-11-15 09:49:24 +010036#define GENMASK_32(h, l) \
37 (((0xFFFFFFFF) << (l)) & (0xFFFFFFFF >> (32 - 1 - (h))))
38
39#define GENMASK_64(h, l) \
40 ((~0 << (l)) & (~0 >> (64 - 1 - (h))))
41#else
Yann Gautier953e94d2018-06-14 18:35:33 +020042#define GENMASK_32(h, l) \
43 (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
44
45#define GENMASK_64(h, l) \
46 (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
Yann Gautierd2c9a682018-11-15 09:49:24 +010047#endif
Yann Gautier953e94d2018-06-14 18:35:33 +020048
Julius Werner8e0ef0f2019-07-09 14:02:43 -070049#ifdef __aarch64__
Yann Gautier953e94d2018-06-14 18:35:33 +020050#define GENMASK GENMASK_64
Julius Werner8e0ef0f2019-07-09 14:02:43 -070051#else
52#define GENMASK GENMASK_32
Yann Gautier953e94d2018-06-14 18:35:33 +020053#endif
54
55/*
Soby Mathew327548c2017-07-13 15:19:51 +010056 * This variant of div_round_up can be used in macro definition but should not
57 * be used in C code as the `div` parameter is evaluated twice.
58 */
59#define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d))
60
Julius Werner54b6d202018-01-22 13:56:13 -080061#define div_round_up(val, div) __extension__ ({ \
62 __typeof__(div) _div = (div); \
Sathees Balya006b15e2018-09-19 14:23:03 +010063 ((val) + _div - (__typeof__(div)) 1) / _div; \
Julius Werner54b6d202018-01-22 13:56:13 -080064})
65
Scott Brandenbf404c02017-04-10 11:45:52 -070066#define MIN(x, y) __extension__ ({ \
67 __typeof__(x) _x = (x); \
68 __typeof__(y) _y = (y); \
69 (void)(&_x == &_y); \
70 _x < _y ? _x : _y; \
71})
72
73#define MAX(x, y) __extension__ ({ \
74 __typeof__(x) _x = (x); \
75 __typeof__(y) _y = (y); \
76 (void)(&_x == &_y); \
77 _x > _y ? _x : _y; \
78})
79
Lionel Debievea265ade2020-01-02 11:14:16 +010080#define CLAMP(x, min, max) __extension__ ({ \
81 __typeof__(x) _x = (x); \
82 __typeof__(min) _min = (min); \
83 __typeof__(max) _max = (max); \
84 (void)(&_x == &_min); \
85 (void)(&_x == &_max); \
86 (_x > _max ? _max : (_x < _min ? _min : _x)); \
87})
88
Scott Brandenbf404c02017-04-10 11:45:52 -070089/*
90 * The round_up() macro rounds up a value to the given boundary in a
91 * type-agnostic yet type-safe manner. The boundary must be a power of two.
92 * In other words, it computes the smallest multiple of boundary which is
93 * greater than or equal to value.
94 *
95 * round_down() is similar but rounds the value down instead.
96 */
97#define round_boundary(value, boundary) \
98 ((__typeof__(value))((boundary) - 1))
99
100#define round_up(value, boundary) \
101 ((((value) - 1) | round_boundary(value, boundary)) + 1)
102
103#define round_down(value, boundary) \
104 ((value) & ~round_boundary(value, boundary))
105
106/*
107 * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
108 * Both arguments must be unsigned pointer values (i.e. uintptr_t).
109 */
Antonio Nino Diazbb77f882018-07-18 13:23:07 +0100110#define check_uptr_overflow(_ptr, _inc) \
111 ((_ptr) > (UINTPTR_MAX - (_inc)))
Scott Brandenbf404c02017-04-10 11:45:52 -0700112
113/*
Jeenu Viswambharan19f6cf22017-12-07 08:43:05 +0000114 * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
115 * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
116 */
Antonio Nino Diazbb77f882018-07-18 13:23:07 +0100117#define check_u32_overflow(_u32, _inc) \
118 ((_u32) > (UINT32_MAX - (_inc)))
Jeenu Viswambharan19f6cf22017-12-07 08:43:05 +0000119
Julius Werner02eb7272017-12-12 14:23:26 -0800120/* Register size of the current architecture. */
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700121#ifdef __aarch64__
Julius Werner02eb7272017-12-12 14:23:26 -0800122#define REGSZ U(8)
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700123#else
124#define REGSZ U(4)
Julius Werner02eb7272017-12-12 14:23:26 -0800125#endif
126
Jeenu Viswambharan0bc79d92017-08-16 11:44:25 +0100127/*
128 * Test for the current architecture version to be at least the version
129 * expected.
130 */
131#define ARM_ARCH_AT_LEAST(_maj, _min) \
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100132 ((ARM_ARCH_MAJOR > (_maj)) || \
133 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
Jeenu Viswambharan0bc79d92017-08-16 11:44:25 +0100134
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000135/*
136 * Import an assembly or linker symbol as a C expression with the specified
137 * type
138 */
139#define IMPORT_SYM(type, sym, name) \
140 extern char sym[];\
141 static const __attribute__((unused)) type name = (type) sym;
142
143/*
144 * When the symbol is used to hold a pointer, its alignment can be asserted
145 * with this macro. For example, if there is a linker symbol that is going to
146 * be used as a 64-bit pointer, the value of the linker symbol must also be
147 * aligned to 64 bit. This macro makes sure this is the case.
148 */
149#define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
150
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000151#define COMPILER_BARRIER() __asm__ volatile ("" ::: "memory")
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000152
Joel Huttond5227592018-10-09 14:08:42 +0100153/* Compiler builtin of GCC >= 9 and planned in llvm */
154#ifdef __HAVE_SPECULATION_SAFE_VALUE
155# define SPECULATION_SAFE_VALUE(var) __builtin_speculation_safe_value(var)
156#else
157# define SPECULATION_SAFE_VALUE(var) var
158#endif
159
Antonio Nino Diazbb77f882018-07-18 13:23:07 +0100160#endif /* UTILS_DEF_H */