2020-04-02 23:48:27 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2009-10-28 17:13:28 +01:00
|
|
|
|
|
|
|
/* This file is for "nuisance prototypes" that have no other home. */
|
|
|
|
|
2010-03-19 03:33:40 +01:00
|
|
|
#ifndef __LIB_H__
|
|
|
|
#define __LIB_H__
|
2020-05-28 08:54:42 +02:00
|
|
|
|
2013-12-27 04:53:52 +01:00
|
|
|
#include <types.h>
|
2010-03-19 03:33:40 +01:00
|
|
|
|
2016-02-08 20:46:22 +01:00
|
|
|
/* Defined in src/lib/lzma.c. Returns decompressed size or 0 on error. */
|
|
|
|
size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn);
|
2009-10-28 17:13:28 +01:00
|
|
|
|
2010-03-19 03:33:40 +01:00
|
|
|
/* Defined in src/lib/ramtest.c */
|
2019-03-18 14:26:48 +01:00
|
|
|
/* Assumption is 32-bit addressable UC memory. */
|
2019-03-23 09:00:31 +01:00
|
|
|
void ram_check(uintptr_t start);
|
|
|
|
int ram_check_nodie(uintptr_t start);
|
|
|
|
int ram_check_noprint_nodie(uintptr_t start);
|
2019-03-18 14:26:48 +01:00
|
|
|
void quick_ram_check_or_die(uintptr_t dst);
|
2010-03-19 03:33:40 +01:00
|
|
|
|
2014-02-13 22:07:50 +01:00
|
|
|
/* Defined in primitive_memtest.c */
|
|
|
|
int primitive_memtest(uintptr_t base, uintptr_t size);
|
|
|
|
|
2012-06-13 01:29:32 +02:00
|
|
|
/* Defined in src/lib/stack.c */
|
2012-10-16 00:19:43 +02:00
|
|
|
int checkstack(void *top_of_stack, int core);
|
2012-06-13 01:29:32 +02:00
|
|
|
|
2015-05-14 23:50:42 +02:00
|
|
|
/*
|
|
|
|
* Defined in src/lib/hexdump.c
|
|
|
|
* Use the Linux command "xxd" for matching output. xxd is found in package
|
|
|
|
* https://packages.debian.org/jessie/amd64/vim-common/filelist
|
|
|
|
*/
|
2013-12-27 04:53:52 +01:00
|
|
|
void hexdump(const void *memory, size_t length);
|
2013-07-19 01:24:08 +02:00
|
|
|
|
2016-05-09 19:58:03 +02:00
|
|
|
/*
|
|
|
|
* hexstrtobin - Turn a string of ASCII hex characters into binary
|
|
|
|
*
|
|
|
|
* @str: String of hex characters to parse
|
|
|
|
* @buf: Buffer to store the resulting bytes into
|
|
|
|
* @len: Maximum length of buffer to fill
|
|
|
|
*
|
|
|
|
* Defined in src/lib/hexstrtobin.c
|
|
|
|
* Ignores non-hex characters in the string.
|
2020-07-02 15:32:00 +02:00
|
|
|
* Ignores the last hex character if the number of hex characters in the string
|
|
|
|
* is odd.
|
2016-05-09 19:58:03 +02:00
|
|
|
* Returns the number of bytes that have been put in the buffer.
|
|
|
|
*/
|
|
|
|
size_t hexstrtobin(const char *str, uint8_t *buf, size_t len);
|
|
|
|
|
2020-10-14 17:48:05 +02:00
|
|
|
/* Population Count: number of bits that are one */
|
|
|
|
static inline int popcnt(u32 x) { return __builtin_popcount(x); }
|
lib: Unify log2() and related functions
This patch adds a few bit counting functions that are commonly needed
for certain register calculations. We previously had a log2()
implementation already, but it was awkwardly split between some C code
that's only available in ramstage and an optimized x86-specific
implementation in pre-RAM that prevented other archs from pulling it
into earlier stages.
Using __builtin_clz() as the baseline allows GCC to inline optimized
assembly for most archs (including CLZ on ARM/ARM64 and BSR on x86), and
to perform constant-folding if possible. What was previously named log2f
on pre-RAM x86 is now ffs, since that's the standard name for that
operation and I honestly don't have the slightest idea how it could've
ever ended up being called log2f (which in POSIX is 'binary(2) LOGarithm
with Float result, whereas the Find First Set operation has no direct
correlation to logarithms that I know of). Make ffs result 0-based
instead of the POSIX standard's 1-based since that is consistent with
clz, log2 and the former log2f, and generally closer to what you want
for most applications (a value that can directly be used as a shift to
reach the found bit). Call it __ffs() instead of ffs() to avoid problems
when importing code, since that's what Linux uses for the 0-based
operation.
CQ-DEPEND=CL:273023
BRANCH=None
BUG=None
TEST=Built on Big, Falco, Jerry, Oak and Urara. Compared old and new
log2() and __ffs() results on Falco for a bunch of test values.
Change-Id: I599209b342059e17b3130621edb6b6bbeae26876
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 3701a16ae944ecff9c54fa9a50d28015690fcb2f
Original-Change-Id: I60f7cf893792508188fa04d088401a8bca4b4af6
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/273008
Original-Reviewed-by: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-on: http://review.coreboot.org/10394
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
2015-05-23 01:26:40 +02:00
|
|
|
/* Count Leading Zeroes: clz(0) == 32, clz(0xf) == 28, clz(1 << 31) == 0 */
|
|
|
|
static inline int clz(u32 x) { return x ? __builtin_clz(x) : sizeof(x) * 8; }
|
|
|
|
/* Integer binary logarithm (rounding down): log2(0) == -1, log2(5) == 2 */
|
|
|
|
static inline int log2(u32 x) { return sizeof(x) * 8 - clz(x) - 1; }
|
|
|
|
/* Find First Set: __ffs(1) == 0, __ffs(0) == -1, __ffs(1<<31) == 31 */
|
|
|
|
static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
|
2021-11-30 03:51:53 +01:00
|
|
|
/* Find Last Set: __fls(1) == 0, __fls(5) == 2, __fls(1 << 31) == 31 */
|
|
|
|
static inline int __fls(u32 x) { return log2(x); }
|
lib: Unify log2() and related functions
This patch adds a few bit counting functions that are commonly needed
for certain register calculations. We previously had a log2()
implementation already, but it was awkwardly split between some C code
that's only available in ramstage and an optimized x86-specific
implementation in pre-RAM that prevented other archs from pulling it
into earlier stages.
Using __builtin_clz() as the baseline allows GCC to inline optimized
assembly for most archs (including CLZ on ARM/ARM64 and BSR on x86), and
to perform constant-folding if possible. What was previously named log2f
on pre-RAM x86 is now ffs, since that's the standard name for that
operation and I honestly don't have the slightest idea how it could've
ever ended up being called log2f (which in POSIX is 'binary(2) LOGarithm
with Float result, whereas the Find First Set operation has no direct
correlation to logarithms that I know of). Make ffs result 0-based
instead of the POSIX standard's 1-based since that is consistent with
clz, log2 and the former log2f, and generally closer to what you want
for most applications (a value that can directly be used as a shift to
reach the found bit). Call it __ffs() instead of ffs() to avoid problems
when importing code, since that's what Linux uses for the 0-based
operation.
CQ-DEPEND=CL:273023
BRANCH=None
BUG=None
TEST=Built on Big, Falco, Jerry, Oak and Urara. Compared old and new
log2() and __ffs() results on Falco for a bunch of test values.
Change-Id: I599209b342059e17b3130621edb6b6bbeae26876
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 3701a16ae944ecff9c54fa9a50d28015690fcb2f
Original-Change-Id: I60f7cf893792508188fa04d088401a8bca4b4af6
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/273008
Original-Reviewed-by: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-on: http://review.coreboot.org/10394
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
2015-05-23 01:26:40 +02:00
|
|
|
|
2021-12-02 03:54:32 +01:00
|
|
|
/* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2_ceil(5) == 3 */
|
|
|
|
static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x - 1) + 1; }
|
lib: Unify log2() and related functions
This patch adds a few bit counting functions that are commonly needed
for certain register calculations. We previously had a log2()
implementation already, but it was awkwardly split between some C code
that's only available in ramstage and an optimized x86-specific
implementation in pre-RAM that prevented other archs from pulling it
into earlier stages.
Using __builtin_clz() as the baseline allows GCC to inline optimized
assembly for most archs (including CLZ on ARM/ARM64 and BSR on x86), and
to perform constant-folding if possible. What was previously named log2f
on pre-RAM x86 is now ffs, since that's the standard name for that
operation and I honestly don't have the slightest idea how it could've
ever ended up being called log2f (which in POSIX is 'binary(2) LOGarithm
with Float result, whereas the Find First Set operation has no direct
correlation to logarithms that I know of). Make ffs result 0-based
instead of the POSIX standard's 1-based since that is consistent with
clz, log2 and the former log2f, and generally closer to what you want
for most applications (a value that can directly be used as a shift to
reach the found bit). Call it __ffs() instead of ffs() to avoid problems
when importing code, since that's what Linux uses for the 0-based
operation.
CQ-DEPEND=CL:273023
BRANCH=None
BUG=None
TEST=Built on Big, Falco, Jerry, Oak and Urara. Compared old and new
log2() and __ffs() results on Falco for a bunch of test values.
Change-Id: I599209b342059e17b3130621edb6b6bbeae26876
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 3701a16ae944ecff9c54fa9a50d28015690fcb2f
Original-Change-Id: I60f7cf893792508188fa04d088401a8bca4b4af6
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/273008
Original-Reviewed-by: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-on: http://review.coreboot.org/10394
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
2015-05-23 01:26:40 +02:00
|
|
|
|
2020-10-14 17:48:05 +02:00
|
|
|
static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
|
2020-10-01 23:36:42 +02:00
|
|
|
static inline int clz64(u64 x) { return x ? __builtin_clzll(x) : sizeof(x) * 8; }
|
|
|
|
static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
|
|
|
|
static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
|
2021-11-30 03:51:53 +01:00
|
|
|
static inline int __fls64(u64 x) { return log2_64(x); }
|
2020-10-01 23:36:42 +02:00
|
|
|
|
2010-03-19 03:33:40 +01:00
|
|
|
#endif /* __LIB_H__ */
|