2011-09-22 01:12:39 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-09-07 16:26:08 +02:00
|
|
|
#include <stddef.h>
|
2011-09-22 01:12:39 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <console/console.h>
|
|
|
|
#include <cbmem.h>
|
2015-05-01 23:48:54 +02:00
|
|
|
#include <timer.h>
|
2011-09-22 01:12:39 +02:00
|
|
|
#include <timestamp.h>
|
2013-06-19 21:25:44 +02:00
|
|
|
#include <arch/early_variables.h>
|
2015-05-01 23:48:54 +02:00
|
|
|
#include <rules.h>
|
2014-01-26 13:41:54 +01:00
|
|
|
#include <smp/node.h>
|
2011-09-22 01:12:39 +02:00
|
|
|
|
2014-12-03 05:51:19 +01:00
|
|
|
#define MAX_TIMESTAMPS 60
|
2011-09-22 01:12:39 +02:00
|
|
|
|
2013-10-13 12:27:56 +02:00
|
|
|
static struct timestamp_table* ts_table_p CAR_GLOBAL = NULL;
|
2013-08-01 22:31:44 +02:00
|
|
|
static uint64_t ts_basetime CAR_GLOBAL = 0;
|
2013-09-07 16:26:08 +02:00
|
|
|
|
2013-08-01 22:31:44 +02:00
|
|
|
static void timestamp_stash(enum timestamp_id id, uint64_t ts_time);
|
2011-09-22 01:12:39 +02:00
|
|
|
|
2013-08-01 22:31:44 +02:00
|
|
|
static void timestamp_real_init(uint64_t base)
|
2011-09-22 01:12:39 +02:00
|
|
|
{
|
|
|
|
struct timestamp_table* tst;
|
|
|
|
|
|
|
|
tst = cbmem_add(CBMEM_ID_TIMESTAMP,
|
|
|
|
sizeof(struct timestamp_table) +
|
|
|
|
MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
|
|
|
|
|
|
|
|
if (!tst) {
|
2013-01-22 11:46:34 +01:00
|
|
|
printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n");
|
2011-09-22 01:12:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-01 22:31:44 +02:00
|
|
|
tst->base_time = base;
|
2011-09-22 01:12:39 +02:00
|
|
|
tst->max_entries = MAX_TIMESTAMPS;
|
|
|
|
tst->num_entries = 0;
|
2013-09-07 16:26:08 +02:00
|
|
|
|
2013-10-13 12:27:56 +02:00
|
|
|
car_set_var(ts_table_p, tst);
|
2011-09-22 01:12:39 +02:00
|
|
|
}
|
|
|
|
|
2015-05-01 23:48:54 +02:00
|
|
|
/* Determine if one should proceed into timestamp code. This is for protecting
|
|
|
|
* systems that have multiple processors running in romstage -- namely AMD
|
|
|
|
* based x86 platforms. */
|
|
|
|
static int timestamp_should_run(void)
|
|
|
|
{
|
|
|
|
/* Only check boot_cpu() in other stages than ramstage on x86. */
|
|
|
|
if ((!ENV_RAMSTAGE && IS_ENABLED(CONFIG_ARCH_X86)) && !boot_cpu())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-01 22:31:44 +02:00
|
|
|
void timestamp_add(enum timestamp_id id, uint64_t ts_time)
|
2011-09-22 01:12:39 +02:00
|
|
|
{
|
|
|
|
struct timestamp_entry *tse;
|
2013-10-13 12:27:56 +02:00
|
|
|
struct timestamp_table *ts_table = NULL;
|
2013-09-07 16:26:08 +02:00
|
|
|
|
2015-05-01 23:48:54 +02:00
|
|
|
if (!timestamp_should_run())
|
2013-09-08 12:10:28 +02:00
|
|
|
return;
|
|
|
|
|
2013-10-13 12:27:56 +02:00
|
|
|
ts_table = car_get_var(ts_table_p);
|
2013-09-07 16:26:08 +02:00
|
|
|
if (!ts_table) {
|
|
|
|
timestamp_stash(id, ts_time);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ts_table->num_entries == ts_table->max_entries)
|
2011-09-22 01:12:39 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
tse = &ts_table->entries[ts_table->num_entries++];
|
|
|
|
tse->entry_id = id;
|
2013-08-01 22:31:44 +02:00
|
|
|
tse->entry_stamp = ts_time - ts_table->base_time;
|
2011-09-22 01:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void timestamp_add_now(enum timestamp_id id)
|
|
|
|
{
|
2013-08-01 22:31:44 +02:00
|
|
|
timestamp_add(id, timestamp_get());
|
2011-09-22 01:12:39 +02:00
|
|
|
}
|
2012-10-16 00:23:20 +02:00
|
|
|
|
|
|
|
#define MAX_TIMESTAMP_CACHE 8
|
|
|
|
struct timestamp_cache {
|
|
|
|
enum timestamp_id id;
|
2013-08-01 22:31:44 +02:00
|
|
|
uint64_t time;
|
2012-10-16 00:23:20 +02:00
|
|
|
} timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL;
|
|
|
|
|
|
|
|
static int timestamp_entries CAR_GLOBAL = 0;
|
|
|
|
|
|
|
|
/**
|
2013-10-13 12:27:56 +02:00
|
|
|
* timestamp_stash() allows to temporarily cache timestamps.
|
|
|
|
* This is needed when timestamping before the CBMEM area
|
|
|
|
* is initialized. The function timestamp_do_sync() is used to
|
|
|
|
* write the timestamps to the CBMEM area and this is done as
|
|
|
|
* part of CAR migration for romstage, and in ramstage main().
|
2012-10-16 00:23:20 +02:00
|
|
|
*/
|
|
|
|
|
2013-08-01 22:31:44 +02:00
|
|
|
static void timestamp_stash(enum timestamp_id id, uint64_t ts_time)
|
2012-10-16 00:23:20 +02:00
|
|
|
{
|
2013-10-13 12:27:56 +02:00
|
|
|
struct timestamp_cache *ts_cache = car_get_var(timestamp_cache);
|
|
|
|
int ts_entries = car_get_var(timestamp_entries);
|
|
|
|
|
|
|
|
if (ts_entries >= MAX_TIMESTAMP_CACHE) {
|
2012-10-16 00:23:20 +02:00
|
|
|
printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-10-13 12:27:56 +02:00
|
|
|
ts_cache[ts_entries].id = id;
|
|
|
|
ts_cache[ts_entries].time = ts_time;
|
|
|
|
car_set_var(timestamp_entries, ++ts_entries);
|
2012-10-16 00:23:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 16:26:08 +02:00
|
|
|
static void timestamp_do_sync(void)
|
2012-10-16 00:23:20 +02:00
|
|
|
{
|
2013-10-13 12:27:56 +02:00
|
|
|
struct timestamp_cache *ts_cache = car_get_var(timestamp_cache);
|
|
|
|
int ts_entries = car_get_var(timestamp_entries);
|
|
|
|
|
2012-10-16 00:23:20 +02:00
|
|
|
int i;
|
2013-10-13 12:27:56 +02:00
|
|
|
for (i = 0; i < ts_entries; i++)
|
|
|
|
timestamp_add(ts_cache[i].id, ts_cache[i].time);
|
|
|
|
car_set_var(timestamp_entries, 0);
|
2012-10-16 00:23:20 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 22:31:44 +02:00
|
|
|
void timestamp_init(uint64_t base)
|
2013-09-07 16:26:08 +02:00
|
|
|
{
|
2015-05-01 23:48:54 +02:00
|
|
|
if (!timestamp_should_run())
|
2013-09-08 12:10:28 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-07 19:26:36 +02:00
|
|
|
#ifdef __PRE_RAM__
|
|
|
|
/* Copy of basetime, it is too early for CBMEM. */
|
2013-10-13 12:27:56 +02:00
|
|
|
car_set_var(ts_basetime, base);
|
2013-09-07 19:26:36 +02:00
|
|
|
#else
|
2013-09-07 16:26:08 +02:00
|
|
|
struct timestamp_table* tst;
|
|
|
|
|
|
|
|
/* Locate and use an already existing table. */
|
|
|
|
tst = cbmem_find(CBMEM_ID_TIMESTAMP);
|
|
|
|
if (tst) {
|
2013-10-13 12:27:56 +02:00
|
|
|
car_set_var(ts_table_p, tst);
|
2013-09-07 16:26:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-07 19:26:36 +02:00
|
|
|
/* Copy of basetime, may be too early for CBMEM. */
|
2013-10-13 12:27:56 +02:00
|
|
|
car_set_var(ts_basetime, base);
|
2013-09-07 16:26:08 +02:00
|
|
|
timestamp_real_init(base);
|
2013-09-07 19:26:36 +02:00
|
|
|
#endif
|
2013-09-07 16:26:08 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 23:07:21 +02:00
|
|
|
void timestamp_reinit(void)
|
2013-09-07 16:26:08 +02:00
|
|
|
{
|
2015-05-01 23:48:54 +02:00
|
|
|
if (!timestamp_should_run())
|
2013-09-08 12:10:28 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-07 19:26:36 +02:00
|
|
|
#ifdef __PRE_RAM__
|
2013-10-13 12:27:56 +02:00
|
|
|
timestamp_real_init(car_get_var(ts_basetime));
|
2013-09-07 19:26:36 +02:00
|
|
|
#else
|
2013-10-13 12:27:56 +02:00
|
|
|
if (!car_get_var(ts_table_p))
|
|
|
|
timestamp_init(car_get_var(ts_basetime));
|
2012-10-16 00:23:20 +02:00
|
|
|
#endif
|
2013-10-13 12:27:56 +02:00
|
|
|
if (car_get_var(ts_table_p))
|
2013-09-07 19:26:36 +02:00
|
|
|
timestamp_do_sync();
|
|
|
|
}
|
2013-09-09 23:07:21 +02:00
|
|
|
|
|
|
|
/* Call timestamp_reinit at CAR migration time. */
|
|
|
|
CAR_MIGRATE(timestamp_reinit)
|
2015-05-01 23:48:54 +02:00
|
|
|
|
|
|
|
/* Provide default timestamp implementation using monotonic timer. */
|
|
|
|
uint64_t __attribute__((weak)) timestamp_get(void)
|
|
|
|
{
|
|
|
|
struct mono_time t1, t2;
|
|
|
|
|
2015-05-07 18:32:30 +02:00
|
|
|
if (!IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
|
|
|
|
return 0;
|
|
|
|
|
2015-05-01 23:48:54 +02:00
|
|
|
mono_time_set_usecs(&t1, 0);
|
|
|
|
timer_monotonic_get(&t2);
|
|
|
|
|
|
|
|
return mono_time_diff_microseconds(&t1, &t2);
|
|
|
|
}
|