blob: 5b4fd7808fc94a5f023af839ce51dc42adca70f7 [file] [log] [blame]
Scott Brandenbf404c02017-04-10 11:45:52 -07001/*
Joel Hutton5cc3bc82018-03-21 11:40:57 +00002 * Copyright (c) 2016-2018, 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
10/* Compute the number of elements in the given array */
11#define ARRAY_SIZE(a) \
12 (sizeof(a) / sizeof((a)[0]))
13
14#define IS_POWER_OF_TWO(x) \
15 (((x) & ((x) - 1)) == 0)
16
17#define SIZE_FROM_LOG2_WORDS(n) (4 << (n))
18
Yann Gautierc70fed52018-06-14 13:28:31 +020019#define BIT_32(nr) (U(1) << (nr))
20#define BIT_64(nr) (ULL(1) << (nr))
21
22#ifdef AARCH32
23#define BIT BIT_32
24#else
25#define BIT BIT_64
26#endif
Scott Brandenbf404c02017-04-10 11:45:52 -070027
Soby Mathew327548c2017-07-13 15:19:51 +010028/*
Yann Gautier953e94d2018-06-14 18:35:33 +020029 * Create a contiguous bitmask starting at bit position @l and ending at
30 * position @h. For example
31 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000.
32 */
33#define GENMASK_32(h, l) \
34 (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h))))
35
36#define GENMASK_64(h, l) \
37 (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h))))
38
39#ifdef AARCH32
40#define GENMASK GENMASK_32
41#else
42#define GENMASK GENMASK_64
43#endif
44
45/*
Soby Mathew327548c2017-07-13 15:19:51 +010046 * This variant of div_round_up can be used in macro definition but should not
47 * be used in C code as the `div` parameter is evaluated twice.
48 */
49#define DIV_ROUND_UP_2EVAL(n, d) (((n) + (d) - 1) / (d))
50
Julius Werner54b6d202018-01-22 13:56:13 -080051#define div_round_up(val, div) __extension__ ({ \
52 __typeof__(div) _div = (div); \
53 ((val) + _div - 1) / _div; \
54})
55
Scott Brandenbf404c02017-04-10 11:45:52 -070056#define MIN(x, y) __extension__ ({ \
57 __typeof__(x) _x = (x); \
58 __typeof__(y) _y = (y); \
59 (void)(&_x == &_y); \
60 _x < _y ? _x : _y; \
61})
62
63#define MAX(x, y) __extension__ ({ \
64 __typeof__(x) _x = (x); \
65 __typeof__(y) _y = (y); \
66 (void)(&_x == &_y); \
67 _x > _y ? _x : _y; \
68})
69
70/*
71 * The round_up() macro rounds up a value to the given boundary in a
72 * type-agnostic yet type-safe manner. The boundary must be a power of two.
73 * In other words, it computes the smallest multiple of boundary which is
74 * greater than or equal to value.
75 *
76 * round_down() is similar but rounds the value down instead.
77 */
78#define round_boundary(value, boundary) \
79 ((__typeof__(value))((boundary) - 1))
80
81#define round_up(value, boundary) \
82 ((((value) - 1) | round_boundary(value, boundary)) + 1)
83
84#define round_down(value, boundary) \
85 ((value) & ~round_boundary(value, boundary))
86
87/*
88 * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
89 * Both arguments must be unsigned pointer values (i.e. uintptr_t).
90 */
Antonio Nino Diazbb77f882018-07-18 13:23:07 +010091#define check_uptr_overflow(_ptr, _inc) \
92 ((_ptr) > (UINTPTR_MAX - (_inc)))
Scott Brandenbf404c02017-04-10 11:45:52 -070093
94/*
Jeenu Viswambharan19f6cf22017-12-07 08:43:05 +000095 * Evaluates to 1 if (u32 + inc) overflows, 0 otherwise.
96 * Both arguments must be 32-bit unsigned integers (i.e. effectively uint32_t).
97 */
Antonio Nino Diazbb77f882018-07-18 13:23:07 +010098#define check_u32_overflow(_u32, _inc) \
99 ((_u32) > (UINT32_MAX - (_inc)))
Jeenu Viswambharan19f6cf22017-12-07 08:43:05 +0000100
101/*
Antonio Nino Diaz29135962018-07-18 11:56:00 +0100102 * For those constants to be shared between C and other sources, apply a 'U',
103 * 'UL', 'ULL', 'L' or 'LL' suffix to the argument only in C, to avoid
104 * undefined or unintended behaviour.
Scott Brandenbf404c02017-04-10 11:45:52 -0700105 *
Antonio Nino Diaz29135962018-07-18 11:56:00 +0100106 * The GNU assembler and linker do not support these suffixes (it causes the
107 * build process to fail) therefore the suffix is omitted when used in linker
108 * scripts and assembler files.
Scott Brandenbf404c02017-04-10 11:45:52 -0700109*/
110#if defined(__LINKER__) || defined(__ASSEMBLY__)
Antonio Nino Diaz29135962018-07-18 11:56:00 +0100111# define U(_x) (_x)
112# define UL(_x) (_x)
Scott Brandenbf404c02017-04-10 11:45:52 -0700113# define ULL(_x) (_x)
Antonio Nino Diaz29135962018-07-18 11:56:00 +0100114# define L(_x) (_x)
115# define LL(_x) (_x)
Scott Brandenbf404c02017-04-10 11:45:52 -0700116#else
Antonio Nino Diaz29135962018-07-18 11:56:00 +0100117# define U(_x) (_x##U)
118# define UL(_x) (_x##UL)
David Cunadoc1503122018-02-16 21:12:58 +0000119# define ULL(_x) (_x##ULL)
Antonio Nino Diaz29135962018-07-18 11:56:00 +0100120# define L(_x) (_x##L)
121# define LL(_x) (_x##LL)
Scott Brandenbf404c02017-04-10 11:45:52 -0700122#endif
123
Julius Werner02eb7272017-12-12 14:23:26 -0800124/* Register size of the current architecture. */
125#ifdef AARCH32
126#define REGSZ U(4)
127#else
128#define REGSZ U(8)
129#endif
130
Jeenu Viswambharan0bc79d92017-08-16 11:44:25 +0100131/*
132 * Test for the current architecture version to be at least the version
133 * expected.
134 */
135#define ARM_ARCH_AT_LEAST(_maj, _min) \
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100136 ((ARM_ARCH_MAJOR > (_maj)) || \
137 ((ARM_ARCH_MAJOR == (_maj)) && (ARM_ARCH_MINOR >= (_min))))
Jeenu Viswambharan0bc79d92017-08-16 11:44:25 +0100138
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000139/*
140 * Import an assembly or linker symbol as a C expression with the specified
141 * type
142 */
143#define IMPORT_SYM(type, sym, name) \
144 extern char sym[];\
145 static const __attribute__((unused)) type name = (type) sym;
146
147/*
148 * When the symbol is used to hold a pointer, its alignment can be asserted
149 * with this macro. For example, if there is a linker symbol that is going to
150 * be used as a 64-bit pointer, the value of the linker symbol must also be
151 * aligned to 64 bit. This macro makes sure this is the case.
152 */
153#define ASSERT_SYM_PTR_ALIGN(sym) assert(((size_t)(sym) % __alignof__(*(sym))) == 0)
154
155
Antonio Nino Diazbb77f882018-07-18 13:23:07 +0100156#endif /* UTILS_DEF_H */