xref: /rk3399_ARM-atf/include/lib/utils.h (revision 308d359b260d888f024a2d26c76cd4a50789e432)
1ed81f3ebSSandrine Bailleux /*
29edac047SDavid Cunado  * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3ed81f3ebSSandrine Bailleux  *
4ed81f3ebSSandrine Bailleux  * Redistribution and use in source and binary forms, with or without
5ed81f3ebSSandrine Bailleux  * modification, are permitted provided that the following conditions are met:
6ed81f3ebSSandrine Bailleux  *
7ed81f3ebSSandrine Bailleux  * Redistributions of source code must retain the above copyright notice, this
8ed81f3ebSSandrine Bailleux  * list of conditions and the following disclaimer.
9ed81f3ebSSandrine Bailleux  *
10ed81f3ebSSandrine Bailleux  * Redistributions in binary form must reproduce the above copyright notice,
11ed81f3ebSSandrine Bailleux  * this list of conditions and the following disclaimer in the documentation
12ed81f3ebSSandrine Bailleux  * and/or other materials provided with the distribution.
13ed81f3ebSSandrine Bailleux  *
14ed81f3ebSSandrine Bailleux  * Neither the name of ARM nor the names of its contributors may be used
15ed81f3ebSSandrine Bailleux  * to endorse or promote products derived from this software without specific
16ed81f3ebSSandrine Bailleux  * prior written permission.
17ed81f3ebSSandrine Bailleux  *
18ed81f3ebSSandrine Bailleux  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19ed81f3ebSSandrine Bailleux  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ed81f3ebSSandrine Bailleux  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ed81f3ebSSandrine Bailleux  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22ed81f3ebSSandrine Bailleux  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23ed81f3ebSSandrine Bailleux  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24ed81f3ebSSandrine Bailleux  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25ed81f3ebSSandrine Bailleux  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26ed81f3ebSSandrine Bailleux  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ed81f3ebSSandrine Bailleux  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28ed81f3ebSSandrine Bailleux  * POSSIBILITY OF SUCH DAMAGE.
29ed81f3ebSSandrine Bailleux  */
30ed81f3ebSSandrine Bailleux 
31ed81f3ebSSandrine Bailleux #ifndef __UTILS_H__
32ed81f3ebSSandrine Bailleux #define __UTILS_H__
33ed81f3ebSSandrine Bailleux 
34ed81f3ebSSandrine Bailleux /* Compute the number of elements in the given array */
35ed81f3ebSSandrine Bailleux #define ARRAY_SIZE(a)				\
36ed81f3ebSSandrine Bailleux 	(sizeof(a) / sizeof((a)[0]))
37ed81f3ebSSandrine Bailleux 
38ed81f3ebSSandrine Bailleux #define IS_POWER_OF_TWO(x)			\
39ed81f3ebSSandrine Bailleux 	(((x) & ((x) - 1)) == 0)
40ed81f3ebSSandrine Bailleux 
41c45f627dSSoby Mathew #define SIZE_FROM_LOG2_WORDS(n)		(4 << (n))
42c45f627dSSoby Mathew 
43152c8c11SMasahiro Yamada #define BIT(nr)				(1UL << (nr))
44152c8c11SMasahiro Yamada 
450146ae64SSandrine Bailleux /*
460146ae64SSandrine Bailleux  * The round_up() macro rounds up a value to the given boundary in a
470146ae64SSandrine Bailleux  * type-agnostic yet type-safe manner. The boundary must be a power of two.
480146ae64SSandrine Bailleux  * In other words, it computes the smallest multiple of boundary which is
490146ae64SSandrine Bailleux  * greater than or equal to value.
500146ae64SSandrine Bailleux  *
510146ae64SSandrine Bailleux  * round_down() is similar but rounds the value down instead.
520146ae64SSandrine Bailleux  */
530146ae64SSandrine Bailleux #define round_boundary(value, boundary)		\
540146ae64SSandrine Bailleux 	((__typeof__(value))((boundary) - 1))
550146ae64SSandrine Bailleux 
560146ae64SSandrine Bailleux #define round_up(value, boundary)		\
570146ae64SSandrine Bailleux 	((((value) - 1) | round_boundary(value, boundary)) + 1)
580146ae64SSandrine Bailleux 
590146ae64SSandrine Bailleux #define round_down(value, boundary)		\
600146ae64SSandrine Bailleux 	((value) & ~round_boundary(value, boundary))
610146ae64SSandrine Bailleux 
627b6d330cSSandrine Bailleux /*
637b6d330cSSandrine Bailleux  * Evaluates to 1 if (ptr + inc) overflows, 0 otherwise.
647b6d330cSSandrine Bailleux  * Both arguments must be unsigned pointer values (i.e. uintptr_t).
657b6d330cSSandrine Bailleux  */
667b6d330cSSandrine Bailleux #define check_uptr_overflow(ptr, inc)		\
677b6d330cSSandrine Bailleux 	(((ptr) > UINTPTR_MAX - (inc)) ? 1 : 0)
687b6d330cSSandrine Bailleux 
699edac047SDavid Cunado /*
709edac047SDavid Cunado  * For those constants to be shared between C and other sources, apply a 'ull'
719edac047SDavid Cunado  * suffix to the argument only in C, to avoid undefined or unintended behaviour.
729edac047SDavid Cunado  *
739edac047SDavid Cunado  * The GNU assembler and linker do not support the 'ull' suffix (it causes the
749edac047SDavid Cunado  * build process to fail) therefore the suffix is omitted when used in linker
759edac047SDavid Cunado  * scripts and assembler files.
769edac047SDavid Cunado */
779edac047SDavid Cunado #if defined(__LINKER__) || defined(__ASSEMBLY__)
789edac047SDavid Cunado # define ULL(_x)	(_x)
799edac047SDavid Cunado #else
809edac047SDavid Cunado # define ULL(_x)	(_x##ull)
819edac047SDavid Cunado #endif
829edac047SDavid Cunado 
83*308d359bSDouglas Raillard /*
84*308d359bSDouglas Raillard  * C code should be put in this part of the header to avoid breaking ASM files
85*308d359bSDouglas Raillard  * or linker scripts including it.
86*308d359bSDouglas Raillard  */
87*308d359bSDouglas Raillard #if !(defined(__LINKER__) || defined(__ASSEMBLY__))
88*308d359bSDouglas Raillard 
89*308d359bSDouglas Raillard #include <types.h>
90*308d359bSDouglas Raillard 
91*308d359bSDouglas Raillard /*
92*308d359bSDouglas Raillard  * Fill a region of normal memory of size "length" in bytes with zero bytes.
93*308d359bSDouglas Raillard  *
94*308d359bSDouglas Raillard  * WARNING: This function can only operate on normal memory. This means that
95*308d359bSDouglas Raillard  *          the MMU must be enabled when using this function. Otherwise, use
96*308d359bSDouglas Raillard  *          zeromem.
97*308d359bSDouglas Raillard  */
98*308d359bSDouglas Raillard void zero_normalmem(void *mem, u_register_t length);
99*308d359bSDouglas Raillard 
100*308d359bSDouglas Raillard /*
101*308d359bSDouglas Raillard  * Fill a region of memory of size "length" in bytes with null bytes.
102*308d359bSDouglas Raillard  *
103*308d359bSDouglas Raillard  * Unlike zero_normalmem, this function has no restriction on the type of
104*308d359bSDouglas Raillard  * memory targeted and can be used for any device memory as well as normal
105*308d359bSDouglas Raillard  * memory. This function must be used instead of zero_normalmem when MMU is
106*308d359bSDouglas Raillard  * disabled.
107*308d359bSDouglas Raillard  *
108*308d359bSDouglas Raillard  * NOTE: When data cache and MMU are enabled, prefer zero_normalmem for faster
109*308d359bSDouglas Raillard  *       zeroing.
110*308d359bSDouglas Raillard  */
111*308d359bSDouglas Raillard void zeromem(void *mem, u_register_t length);
112*308d359bSDouglas Raillard #endif /* !(defined(__LINKER__) || defined(__ASSEMBLY__)) */
113*308d359bSDouglas Raillard 
114ed81f3ebSSandrine Bailleux #endif /* __UTILS_H__ */
115