1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 * Copyright (c) 2022, Arm Limited. All rights reserved.
5 */
6 #ifndef UTIL_H
7 #define UTIL_H
8
9 #include <compiler.h>
10 #include <inttypes.h>
11
12 #define SIZE_4K UINTPTR_C(0x1000)
13 #define SIZE_1M UINTPTR_C(0x100000)
14 #define SIZE_2M UINTPTR_C(0x200000)
15 #define SIZE_4M UINTPTR_C(0x400000)
16 #define SIZE_8M UINTPTR_C(0x800000)
17 #define SIZE_2G UINTPTR_C(0x80000000)
18
19 #ifndef MAX
20 #ifndef __ASSEMBLER__
21 #define MAX(a, b) \
22 (__extension__({ __typeof__(a) _a = (a); \
23 __typeof__(b) _b = (b); \
24 _a > _b ? _a : _b; }))
25
26 #define MIN(a, b) \
27 (__extension__({ __typeof__(a) _a = (a); \
28 __typeof__(b) _b = (b); \
29 _a < _b ? _a : _b; }))
30 #else
31 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
32 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
33 #endif
34 #endif
35
36 /*
37 * In some particular conditions MAX and MIN macros fail to
38 * build from C source file implmentation. In such case one
39 * need to use MAX_UNSAFE/MIN_UNSAFE instead.
40 */
41 #define MAX_UNSAFE(a, b) (((a) > (b)) ? (a) : (b))
42 #define MIN_UNSAFE(a, b) (((a) < (b)) ? (a) : (b))
43
44 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
45
46 #ifndef __ASSEMBLER__
47 /* Round up the even multiple of size, size has to be a multiple of 2 */
48 #define ROUNDUP(v, size) (((v) + ((__typeof__(v))(size) - 1)) & \
49 ~((__typeof__(v))(size) - 1))
50
51 #define ROUNDUP_OVERFLOW(v, size, res) (__extension__({ \
52 __typeof__(*(res)) __roundup_tmp = 0; \
53 __typeof__(v) __roundup_mask = (__typeof__(v))(size) - 1; \
54 \
55 ADD_OVERFLOW((v), __roundup_mask, &__roundup_tmp) ? 1 : \
56 (void)(*(res) = __roundup_tmp & ~__roundup_mask), 0; \
57 }))
58
59 /*
60 * Rounds up to the nearest multiple of y and then divides by y. Safe
61 * against overflow, y has to be a multiple of 2.
62 *
63 * This macro is intended to be used to convert from "number of bytes" to
64 * "number of pages" or similar units. Example:
65 * num_pages = ROUNDUP_DIV(num_bytes, SMALL_PAGE_SIZE);
66 */
67 #define ROUNDUP_DIV(x, y) (__extension__({ \
68 __typeof__(x) __roundup_x = (x); \
69 __typeof__(y) __roundup_mask = (__typeof__(x))(y) - 1; \
70 \
71 (__roundup_x / (y)) + (__roundup_x & __roundup_mask ? 1 : 0); \
72 }))
73
74 /* Round down the even multiple of size, size has to be a multiple of 2 */
75 #define ROUNDDOWN(v, size) ((v) & ~((__typeof__(v))(size) - 1))
76
77 /* Unsigned integer division with nearest rounding variant */
78 #define UDIV_ROUND_NEAREST(x, y) \
79 (__extension__ ({ __typeof__(x) _x = (x); \
80 __typeof__(y) _y = (y); \
81 (_x + (_y / 2)) / _y; }))
82 #else
83 #define ROUNDUP(x, y) ((((x) + (y) - 1) / (y)) * (y))
84 #define ROUNDDOWN(x, y) (((x) / (y)) * (y))
85 #define UDIV_ROUND_NEAREST(x, y) (((x) + ((y) / 2)) / (y))
86 #endif
87
88 /* x has to be of an unsigned type */
89 #define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & (~(x) + 1)) == (x)))
90
91 #define ALIGNMENT_IS_OK(p, type) \
92 (((uintptr_t)(p) & (__alignof__(type) - 1)) == 0)
93
94 #define TO_STR(x) _TO_STR(x)
95 #define _TO_STR(x) #x
96
97 #define CONCAT(x, y) _CONCAT(x, y)
98 #define _CONCAT(x, y) x##y
99
100 #define container_of(ptr, type, member) \
101 (__extension__({ \
102 const __typeof__(((type *)0)->member) *__ptr = (ptr); \
103 (type *)((unsigned long)(__ptr) - offsetof(type, member)); \
104 }))
105
106 #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
107
108 #ifdef __ASSEMBLER__
109 #define BIT32(nr) (1 << (nr))
110 #define BIT64(nr) (1 << (nr))
111 #define SHIFT_U32(v, shift) ((v) << (shift))
112 #define SHIFT_U64(v, shift) ((v) << (shift))
113 #else
114 #define BIT32(nr) (UINT32_C(1) << (nr))
115 #define BIT64(nr) (UINT64_C(1) << (nr))
116 #define SHIFT_U32(v, shift) ((uint32_t)(v) << (shift))
117 #define SHIFT_U64(v, shift) ((uint64_t)(v) << (shift))
118 #endif
119 #define BIT(nr) BIT32(nr)
120
121 /*
122 * Create a contiguous bitmask starting at bit position @l and ending at
123 * position @h. For example
124 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
125 */
126 #define GENMASK_32(h, l) \
127 (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
128
129 #define GENMASK_64(h, l) \
130 (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
131
132 /*
133 * Checking overflow for addition, subtraction and multiplication. Result
134 * of operation is stored in res which is a pointer to some kind of
135 * integer.
136 *
137 * The macros return true if an overflow occurred and *res is undefined.
138 */
139 #define ADD_OVERFLOW(a, b, res) __compiler_add_overflow((a), (b), (res))
140 #define SUB_OVERFLOW(a, b, res) __compiler_sub_overflow((a), (b), (res))
141 #define MUL_OVERFLOW(a, b, res) __compiler_mul_overflow((a), (b), (res))
142
143 /* Return a signed +1, 0 or -1 value based on data comparison */
144 #define CMP_TRILEAN(a, b) \
145 (__extension__({ \
146 __typeof__(a) _a = (a); \
147 __typeof__(b) _b = (b); \
148 \
149 _a > _b ? 1 : _a < _b ? -1 : 0; \
150 }))
151
152 #ifndef __ASSEMBLER__
reg_pair_to_64(uint32_t reg0,uint32_t reg1)153 static inline uint64_t reg_pair_to_64(uint32_t reg0, uint32_t reg1)
154 {
155 return (uint64_t)reg0 << 32 | reg1;
156 }
157
reg_pair_from_64(uint64_t val,uint32_t * reg0,uint32_t * reg1)158 static inline void reg_pair_from_64(uint64_t val, uint32_t *reg0,
159 uint32_t *reg1)
160 {
161 *reg0 = val >> 32;
162 *reg1 = val;
163 }
164 #endif
165
166 #endif /*UTIL_H*/
167