summaryrefslogtreecommitdiffstats
path: root/src/northbridge
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
parent3d02b9c79e8dbe661c9e784519c486b8897b6af5 (diff)
downloadcoreboot-7a8a4ab1d88a411ee0dad23318f98b4f29fd2f60.tar.gz
coreboot-7a8a4ab1d88a411ee0dad23318f98b4f29fd2f60.tar.bz2
coreboot-7a8a4ab1d88a411ee0dad23318f98b4f29fd2f60.zip
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')
-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
-rw-r--r--src/northbridge/intel/e7501/raminit.c1
-rw-r--r--src/northbridge/intel/i3100/raminit_ep80579.c1
-rw-r--r--src/northbridge/intel/i855/raminit.c1
-rw-r--r--src/northbridge/intel/i945/raminit.c1
8 files changed, 10 insertions, 2 deletions
diff --git a/src/northbridge/amd/amdk8/coherent_ht.c b/src/northbridge/amd/amdk8/coherent_ht.c
index a8d87008f5ce..6554e075eaed 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 4213cfb3a267..c58abb15aeae 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 14696846dc21..1c1a6ea14216 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 be46f27bca21..87e281d64558 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>
diff --git a/src/northbridge/intel/e7501/raminit.c b/src/northbridge/intel/e7501/raminit.c
index f4fc9a8a7740..93a3a5b5a9f2 100644
--- a/src/northbridge/intel/e7501/raminit.c
+++ b/src/northbridge/intel/e7501/raminit.c
@@ -12,6 +12,7 @@
/* converted to C 6/2004 yhlu */
#include <assert.h>
+#include <lib.h>
#include <spd.h>
#include <sdram_mode.h>
#include <stdlib.h>
diff --git a/src/northbridge/intel/i3100/raminit_ep80579.c b/src/northbridge/intel/i3100/raminit_ep80579.c
index 85660e9c087a..ee8c4fd7f7ff 100644
--- a/src/northbridge/intel/i3100/raminit_ep80579.c
+++ b/src/northbridge/intel/i3100/raminit_ep80579.c
@@ -22,6 +22,7 @@
#include <cpu/x86/mtrr.h>
#include <cpu/x86/cache.h>
#include <cpu/intel/speedstep.h>
+#include <lib.h>
#include "raminit_ep80579.h"
#include "ep80579.h"
diff --git a/src/northbridge/intel/i855/raminit.c b/src/northbridge/intel/i855/raminit.c
index 81dcbb507c50..a43f5be6b740 100644
--- a/src/northbridge/intel/i855/raminit.c
+++ b/src/northbridge/intel/i855/raminit.c
@@ -19,6 +19,7 @@
*/
#include <assert.h>
+#include <lib.h>
#include <spd.h>
#include <sdram_mode.h>
#include <stdlib.h>
diff --git a/src/northbridge/intel/i945/raminit.c b/src/northbridge/intel/i945/raminit.c
index a3be6803f05a..7bd62402b681 100644
--- a/src/northbridge/intel/i945/raminit.c
+++ b/src/northbridge/intel/i945/raminit.c
@@ -20,6 +20,7 @@
#include <console/console.h>
#include <cpu/x86/mtrr.h>
#include <cpu/x86/cache.h>
+#include <lib.h>
#include <pc80/mc146818rtc.h>
#include <spd.h>
#include <string.h>