summaryrefslogtreecommitdiffstats
path: root/src/northbridge/amd
diff options
context:
space:
mode:
authorJulius Werner <jwerner@chromium.org>2015-05-22 16:26:40 -0700
committerPatrick Georgi <pgeorgi@google.com>2015-06-05 13:18:55 +0200
commit7a8a4ab1d88a411ee0dad23318f98b4f29fd2f60 (patch)
treee5b139ca17fce1eda310f0af80b53dac9769e823 /src/northbridge/amd
parent3d02b9c79e8dbe661c9e784519c486b8897b6af5 (diff)
downloadcoreboot-staging-7a8a4ab1d88a411ee0dad23318f98b4f29fd2f60.zip
coreboot-staging-7a8a4ab1d88a411ee0dad23318f98b4f29fd2f60.tar.gz
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>
Diffstat (limited to 'src/northbridge/amd')
-rw-r--r--src/northbridge/amd/amdk8/coherent_ht.c1
-rw-r--r--src/northbridge/amd/amdk8/raminit.c5
-rw-r--r--src/northbridge/amd/amdk8/raminit_f.c1
-rw-r--r--src/northbridge/amd/amdk8/raminit_test.c1
4 files changed, 6 insertions, 2 deletions
diff --git a/src/northbridge/amd/amdk8/coherent_ht.c b/src/northbridge/amd/amdk8/coherent_ht.c
index a8d8700..6554e07 100644
--- a/src/northbridge/amd/amdk8/coherent_ht.c
+++ b/src/northbridge/amd/amdk8/coherent_ht.c
@@ -66,6 +66,7 @@
#include <device/pci_def.h>
#include <device/pci_ids.h>
#include <device/hypertransport_def.h>
+#include <lib.h>
#include <stdlib.h>
#include <arch/io.h>
#include <pc80/mc146818rtc.h>
diff --git a/src/northbridge/amd/amdk8/raminit.c b/src/northbridge/amd/amdk8/raminit.c
index 4213cfb..c58abb1 100644
--- a/src/northbridge/amd/amdk8/raminit.c
+++ b/src/northbridge/amd/amdk8/raminit.c
@@ -7,6 +7,7 @@
#include <cpu/x86/cache.h>
#include <cpu/x86/mtrr.h>
#include <cpu/amd/mtrr.h>
+#include <lib.h>
#include <stdlib.h>
#include <arch/acpi.h>
#include <reset.h>
@@ -1655,7 +1656,7 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
/* if the next lower frequency gives a CL at least one whole cycle
* shorter, select that (see end of BKDG 4.1.1.1) */
if (freq < sizeof(cl_at_freq)-1 && cl_at_freq[freq+1] &&
- log2f(cl_at_freq[freq]) - log2f(cl_at_freq[freq+1]) >= 2)
+ __ffs(cl_at_freq[freq]) - __ffs(cl_at_freq[freq+1]) >= 2)
freq++;
if (freq == sizeof(cl_at_freq))
@@ -1690,7 +1691,7 @@ static struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *
/* Update DRAM Timing Low with our selected cas latency */
value = pci_read_config32(ctrl->f2, DRAM_TIMING_LOW);
value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
- value |= latencies[log2f(cl_at_freq[freq]) - 2] << DTL_TCL_SHIFT;
+ value |= latencies[__ffs(cl_at_freq[freq]) - 2] << DTL_TCL_SHIFT;
pci_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
result.dimm_mask = dimm_mask;
diff --git a/src/northbridge/amd/amdk8/raminit_f.c b/src/northbridge/amd/amdk8/raminit_f.c
index 1469684..1c1a6ea 100644
--- a/src/northbridge/amd/amdk8/raminit_f.c
+++ b/src/northbridge/amd/amdk8/raminit_f.c
@@ -25,6 +25,7 @@
#include <cpu/x86/tsc.h>
#include <cpu/amd/mtrr.h>
+#include <lib.h>
#include <stdlib.h>
#include <arch/acpi.h>
#include "raminit.h"
diff --git a/src/northbridge/amd/amdk8/raminit_test.c b/src/northbridge/amd/amdk8/raminit_test.c
index be46f27..87e281d 100644
--- a/src/northbridge/amd/amdk8/raminit_test.c
+++ b/src/northbridge/amd/amdk8/raminit_test.c
@@ -1,5 +1,6 @@
#include <unistd.h>
#include <limits.h>
+#include <lib.h>
#include <stdint.h>
#include <string.h>
#include <setjmp.h>
OpenPOWER on IntegriCloud