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.
|
|
|
|
*/
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
#include <assert.h>
|
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-07-04 00:04:21 +02:00
|
|
|
#include <symbols.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
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
#define MAX_TIMESTAMP_CACHE 16
|
2013-09-07 16:26:08 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
struct __attribute__((__packed__)) timestamp_cache {
|
2015-07-10 08:51:14 +02:00
|
|
|
uint32_t cache_state;
|
2015-07-04 00:04:21 +02:00
|
|
|
struct timestamp_table table;
|
|
|
|
/* The struct timestamp_table has a 0 length array as its last field.
|
|
|
|
* The following 'entries' array serves as the storage space for the
|
|
|
|
* cache. */
|
|
|
|
struct timestamp_entry entries[MAX_TIMESTAMP_CACHE];
|
|
|
|
};
|
2011-09-22 01:12:39 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
#if (IS_ENABLED(CONFIG_HAS_PRECBMEM_TIMESTAMP_REGION) && defined(__PRE_RAM__))
|
|
|
|
#define USE_TIMESTAMP_REGION 1
|
|
|
|
#else
|
|
|
|
#define USE_TIMESTAMP_REGION 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The cache location will sit in BSS when in ramstage. */
|
|
|
|
#define TIMESTAMP_CACHE_IN_BSS ENV_RAMSTAGE
|
|
|
|
|
|
|
|
#define HAS_CBMEM (ENV_ROMSTAGE || ENV_RAMSTAGE)
|
|
|
|
|
|
|
|
/* Storage of cache entries during ramstage prior to cbmem coming online. */
|
|
|
|
static struct timestamp_cache timestamp_cache;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TIMESTAMP_CACHE_UNINITIALIZED = 0,
|
|
|
|
TIMESTAMP_CACHE_INITIALIZED,
|
|
|
|
TIMESTAMP_CACHE_NOT_NEEDED,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void timestamp_cache_init(struct timestamp_cache *ts_cache,
|
|
|
|
uint64_t base)
|
|
|
|
{
|
|
|
|
ts_cache->table.num_entries = 0;
|
|
|
|
ts_cache->table.max_entries = MAX_TIMESTAMP_CACHE;
|
|
|
|
ts_cache->table.base_time = base;
|
|
|
|
ts_cache->cache_state = TIMESTAMP_CACHE_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct timestamp_cache *timestamp_cache_get(void)
|
|
|
|
{
|
|
|
|
struct timestamp_cache *ts_cache = NULL;
|
|
|
|
|
|
|
|
if (TIMESTAMP_CACHE_IN_BSS) {
|
|
|
|
ts_cache = ×tamp_cache;
|
|
|
|
} else if (USE_TIMESTAMP_REGION) {
|
|
|
|
if (_timestamp_size < sizeof(*ts_cache))
|
|
|
|
BUG();
|
|
|
|
ts_cache = car_get_var_ptr((void *)_timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ts_cache && ts_cache->cache_state == TIMESTAMP_CACHE_UNINITIALIZED)
|
|
|
|
timestamp_cache_init(ts_cache, 0);
|
|
|
|
|
|
|
|
return ts_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct timestamp_table *timestamp_alloc_cbmem_table(void)
|
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));
|
|
|
|
|
2015-05-26 05:23:02 +02:00
|
|
|
if (!tst)
|
2015-07-04 00:04:21 +02:00
|
|
|
return NULL;
|
2011-09-22 01:12:39 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
tst->base_time = 0;
|
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
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
return 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;
|
|
|
|
}
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
static struct timestamp_table *timestamp_table_get(void)
|
2011-09-22 01:12:39 +02:00
|
|
|
{
|
2015-07-04 00:04:21 +02:00
|
|
|
MAYBE_STATIC struct timestamp_table *ts_table = NULL;
|
|
|
|
struct timestamp_cache *ts_cache;
|
2013-09-07 16:26:08 +02:00
|
|
|
|
2015-05-01 23:48:54 +02:00
|
|
|
if (!timestamp_should_run())
|
2015-07-04 00:04:21 +02:00
|
|
|
return NULL;
|
2013-09-08 12:10:28 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
if (ts_table != NULL)
|
|
|
|
return ts_table;
|
|
|
|
|
|
|
|
ts_cache = timestamp_cache_get();
|
|
|
|
|
|
|
|
if (ts_cache == NULL) {
|
|
|
|
if (HAS_CBMEM)
|
|
|
|
ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
|
|
|
|
return ts_table;
|
2013-09-07 16:26:08 +02:00
|
|
|
}
|
2015-07-04 00:04:21 +02:00
|
|
|
|
|
|
|
/* Cache is required. */
|
|
|
|
if (ts_cache->cache_state != TIMESTAMP_CACHE_NOT_NEEDED)
|
|
|
|
return &ts_cache->table;
|
|
|
|
|
|
|
|
/* Cache shouldn't be used but there's no backing store. */
|
|
|
|
if (!HAS_CBMEM)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
|
|
|
|
|
|
|
|
return ts_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timestamp_add_table_entry(struct timestamp_table *ts_table,
|
|
|
|
enum timestamp_id id, uint64_t ts_time)
|
|
|
|
{
|
|
|
|
struct timestamp_entry *tse;
|
|
|
|
|
2015-11-20 20:25:25 +01:00
|
|
|
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;
|
2015-11-20 20:25:25 +01:00
|
|
|
|
|
|
|
if (ts_table->num_entries == ts_table->max_entries)
|
|
|
|
printk(BIOS_ERR, "ERROR: Timestamp table full\n");
|
2011-09-22 01:12:39 +02:00
|
|
|
}
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
void timestamp_add(enum timestamp_id id, uint64_t ts_time)
|
2011-09-22 01:12:39 +02:00
|
|
|
{
|
2015-07-04 00:04:21 +02:00
|
|
|
struct timestamp_table *ts_table;
|
2012-10-16 00:23:20 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
ts_table = timestamp_table_get();
|
2013-10-13 12:27:56 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
if (!ts_table) {
|
|
|
|
printk(BIOS_ERR, "ERROR: No timestamp table found\n");
|
2012-10-16 00:23:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-07-04 00:04:21 +02:00
|
|
|
|
|
|
|
timestamp_add_table_entry(ts_table, id, ts_time);
|
2012-10-16 00:23:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
void timestamp_add_now(enum timestamp_id id)
|
2012-10-16 00:23:20 +02:00
|
|
|
{
|
2015-07-04 00:04:21 +02:00
|
|
|
timestamp_add(id, timestamp_get());
|
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-07-04 00:04:21 +02:00
|
|
|
struct timestamp_cache *ts_cache;
|
|
|
|
|
2015-05-01 23:48:54 +02:00
|
|
|
if (!timestamp_should_run())
|
2013-09-08 12:10:28 +02:00
|
|
|
return;
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
ts_cache = timestamp_cache_get();
|
2015-05-26 05:23:02 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
if (!ts_cache) {
|
|
|
|
printk(BIOS_ERR, "ERROR: No timestamp cache to init\n");
|
2013-09-07 16:26:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-10 08:51:14 +02:00
|
|
|
/* In the EARLY_CBMEM_INIT case timestamps could have already been
|
|
|
|
* recovered. In those circumstances honor the cache which sits in BSS
|
|
|
|
* as it has already been initialized. */
|
2015-07-11 20:36:01 +02:00
|
|
|
if (ENV_RAMSTAGE && IS_ENABLED(CONFIG_EARLY_CBMEM_INIT) &&
|
2015-07-10 08:51:14 +02:00
|
|
|
ts_cache->cache_state != TIMESTAMP_CACHE_UNINITIALIZED)
|
|
|
|
return;
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
timestamp_cache_init(ts_cache, base);
|
2013-09-07 16:26:08 +02:00
|
|
|
}
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
static void timestamp_sync_cache_to_cbmem(int is_recovery)
|
2013-09-07 16:26:08 +02:00
|
|
|
{
|
2015-07-04 00:04:21 +02:00
|
|
|
uint32_t i;
|
|
|
|
struct timestamp_cache *ts_cache;
|
|
|
|
struct timestamp_table *ts_cache_table;
|
|
|
|
struct timestamp_table *ts_cbmem_table = NULL;
|
|
|
|
|
2015-05-01 23:48:54 +02:00
|
|
|
if (!timestamp_should_run())
|
2013-09-08 12:10:28 +02:00
|
|
|
return;
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
ts_cache = timestamp_cache_get();
|
|
|
|
|
|
|
|
/* No timestamp cache found */
|
|
|
|
if (ts_cache == NULL) {
|
|
|
|
printk(BIOS_ERR, "ERROR: No timestamp cache found\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ts_cache_table = &ts_cache->table;
|
|
|
|
|
|
|
|
/* cbmem is being recovered. */
|
|
|
|
if (is_recovery) {
|
|
|
|
/* x86 resume path expects timestamps to be reset. */
|
|
|
|
if (IS_ENABLED(CONFIG_ARCH_ROMSTAGE_X86_32) && ENV_ROMSTAGE)
|
|
|
|
ts_cbmem_table = timestamp_alloc_cbmem_table();
|
|
|
|
else {
|
|
|
|
/* Find existing table in cbmem. */
|
|
|
|
ts_cbmem_table = cbmem_find(CBMEM_ID_TIMESTAMP);
|
|
|
|
/* No existing timestamp table. */
|
|
|
|
if (ts_cbmem_table == NULL)
|
|
|
|
ts_cbmem_table = timestamp_alloc_cbmem_table();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
/* First time sync. Add new table. */
|
|
|
|
ts_cbmem_table = timestamp_alloc_cbmem_table();
|
|
|
|
|
|
|
|
if (ts_cbmem_table == NULL) {
|
|
|
|
printk(BIOS_ERR, "ERROR: No timestamp table allocated\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There's no need to worry about the base_time fields being out of
|
|
|
|
* sync because the following configurations are used/supported:
|
|
|
|
*
|
|
|
|
* 1. CONFIG_HAS_PRECBMEM_TIMESTAMP_REGION is enabled. This
|
|
|
|
* implies CONFIG_EARLY_CBMEM_INIT so once cbmem comes
|
|
|
|
* online we sync the timestamps to the cbmem storage while
|
|
|
|
* running in romstage. In ramstage the cbmem area is
|
|
|
|
* recovered and utilized.
|
|
|
|
*
|
|
|
|
* 2. CONFIG_LATE_CBMEM_INIT (!CONFIG_EARLY_CBMEM_INIT) is
|
|
|
|
* being used. That means the only cache that exists is
|
|
|
|
* in ramstage. Once cbmem comes online in ramstage those
|
|
|
|
* values are sync'd over.
|
|
|
|
*
|
|
|
|
* Any other combinations will result in inconsistent base_time
|
|
|
|
* values including bizarre timestamp values.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < ts_cache_table->num_entries; i++) {
|
|
|
|
struct timestamp_entry *tse = &ts_cache_table->entries[i];
|
|
|
|
timestamp_add_table_entry(ts_cbmem_table, tse->entry_id,
|
|
|
|
tse->entry_stamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Freshly added cbmem table has base_time 0. Inherit cache base_time */
|
|
|
|
if (ts_cbmem_table->base_time == 0)
|
|
|
|
ts_cbmem_table->base_time = ts_cache_table->base_time;
|
|
|
|
|
2015-08-31 04:19:55 +02:00
|
|
|
/* Seed the timestamp tick frequency in ramstage. */
|
|
|
|
if (ENV_RAMSTAGE)
|
|
|
|
ts_cbmem_table->tick_freq_mhz = timestamp_tick_freq_mhz();
|
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
/* Cache no longer required. */
|
|
|
|
ts_cache_table->num_entries = 0;
|
|
|
|
ts_cache->cache_state = TIMESTAMP_CACHE_NOT_NEEDED;
|
2013-09-07 19:26:36 +02:00
|
|
|
}
|
2013-09-09 23:07:21 +02:00
|
|
|
|
2015-07-04 00:04:21 +02:00
|
|
|
ROMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem)
|
|
|
|
RAMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem)
|
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);
|
|
|
|
}
|
2015-08-31 04:19:55 +02:00
|
|
|
|
|
|
|
/* Like timestamp_get() above this matches up with microsecond granularity. */
|
|
|
|
int __attribute__((weak)) timestamp_tick_freq_mhz(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|