timestamps: Use stash before CBMEM is usable

Change-Id: I9e927abdb1d7d9c233de5620a9a65b419e803ebf
Signed-off-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
Reviewed-on: http://review.coreboot.org/3909
Tested-by: build bot (Jenkins)
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
Reviewed-by: Aaron Durbin <adurbin@google.com>
diff --git a/src/lib/timestamp.c b/src/lib/timestamp.c
index 3f8a7bd..7e2f701 100644
--- a/src/lib/timestamp.c
+++ b/src/lib/timestamp.c
@@ -17,27 +17,26 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
  */
 
+#include <stddef.h>
 #include <stdint.h>
 #include <console/console.h>
 #include <cbmem.h>
 #include <timestamp.h>
-#ifndef __PRE_RAM__
-/* For CAR_GLOBAL... This should move out of x86 specific code */
 #include <cpu/x86/car.h>
-#endif
 
 #define MAX_TIMESTAMPS 30
 
-#ifndef __PRE_RAM__
-static struct timestamp_table* ts_table;
-#endif
+static struct timestamp_table* ts_table CAR_GLOBAL = NULL;
+static tsc_t ts_basetime CAR_GLOBAL = { .lo = 0, .hi =0 };
+
+static void timestamp_stash(enum timestamp_id id, tsc_t ts_time);
 
 static uint64_t tsc_to_uint64(tsc_t tstamp)
 {
 	return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
 }
 
-void timestamp_init(tsc_t base)
+static void timestamp_real_init(tsc_t base)
 {
 	struct timestamp_table* tst;
 
@@ -53,18 +52,19 @@
 	tst->base_time = tsc_to_uint64(base);
 	tst->max_entries = MAX_TIMESTAMPS;
 	tst->num_entries = 0;
+
+	ts_table = tst;
 }
 
 void timestamp_add(enum timestamp_id id, tsc_t ts_time)
 {
 	struct timestamp_entry *tse;
-#ifdef __PRE_RAM__
-	struct timestamp_table *ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
-#else
-	if (!ts_table)
-		ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
-#endif
-	if (!ts_table || (ts_table->num_entries == ts_table->max_entries))
+
+	if (!ts_table) {
+		timestamp_stash(id, ts_time);
+		return;
+	}
+	if (ts_table->num_entries == ts_table->max_entries)
 		return;
 
 	tse = &ts_table->entries[ts_table->num_entries++];
@@ -77,8 +77,6 @@
 	timestamp_add(id, rdtsc());
 }
 
-#ifndef __PRE_RAM__
-
 #define MAX_TIMESTAMP_CACHE 8
 struct timestamp_cache {
 	enum timestamp_id id;
@@ -95,18 +93,18 @@
  * ram stage main()
  */
 
-void timestamp_stash(enum timestamp_id id)
+static void timestamp_stash(enum timestamp_id id, tsc_t ts_time)
 {
 	if (timestamp_entries >= MAX_TIMESTAMP_CACHE) {
 		printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n");
 		return;
 	}
 	timestamp_cache[timestamp_entries].id = id;
-	timestamp_cache[timestamp_entries].time = rdtsc();
+	timestamp_cache[timestamp_entries].time = ts_time;
 	timestamp_entries++;
 }
 
-void timestamp_sync(void)
+static void timestamp_do_sync(void)
 {
 	int i;
 	for (i = 0; i < timestamp_entries; i++)
@@ -114,4 +112,30 @@
 	timestamp_entries = 0;
 }
 
+void timestamp_init(tsc_t base)
+{
+#ifndef __PRE_RAM__
+	struct timestamp_table* tst;
+
+	/* Locate and use an already existing table. */
+	tst = cbmem_find(CBMEM_ID_TIMESTAMP);
+	if (tst) {
+		ts_table = tst;
+		return;
+	}
+#endif
+
+	timestamp_real_init(base);
+	if (ts_table)
+		timestamp_do_sync();
+	else
+		ts_basetime = base;
+}
+
+#ifndef __PRE_RAM__
+void timestamp_sync(void)
+{
+	if (!ts_table)
+		timestamp_init(ts_basetime);
+}
 #endif