blob: 67635f87eddc515bc662d343d81552e4a7eb8c8b [file] [log] [blame]
Vadim Bendebury6f72d692011-09-21 16:12:39 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030020#include <stddef.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070021#include <stdint.h>
22#include <console/console.h>
23#include <cbmem.h>
24#include <timestamp.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070025#include <arch/early_variables.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +020026#include <smp/node.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070027
28#define MAX_TIMESTAMPS 30
29
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030030static struct timestamp_table* ts_table_p CAR_GLOBAL = NULL;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070031static uint64_t ts_basetime CAR_GLOBAL = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030032
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070033static void timestamp_stash(enum timestamp_id id, uint64_t ts_time);
Vadim Bendebury6f72d692011-09-21 16:12:39 -070034
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070035static void timestamp_real_init(uint64_t base)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070036{
37 struct timestamp_table* tst;
38
39 tst = cbmem_add(CBMEM_ID_TIMESTAMP,
40 sizeof(struct timestamp_table) +
41 MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
42
43 if (!tst) {
Paul Menzel2edf77c2013-01-22 11:46:34 +010044 printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -070045 return;
46 }
47
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070048 tst->base_time = base;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070049 tst->max_entries = MAX_TIMESTAMPS;
50 tst->num_entries = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030051
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030052 car_set_var(ts_table_p, tst);
Vadim Bendebury6f72d692011-09-21 16:12:39 -070053}
54
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070055void timestamp_add(enum timestamp_id id, uint64_t ts_time)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070056{
57 struct timestamp_entry *tse;
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030058 struct timestamp_table *ts_table = NULL;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030059
Kyösti Mälkkif56ff902013-09-08 13:10:28 +030060 if (!boot_cpu())
61 return;
62
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030063 ts_table = car_get_var(ts_table_p);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030064 if (!ts_table) {
65 timestamp_stash(id, ts_time);
66 return;
67 }
68 if (ts_table->num_entries == ts_table->max_entries)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070069 return;
70
71 tse = &ts_table->entries[ts_table->num_entries++];
72 tse->entry_id = id;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070073 tse->entry_stamp = ts_time - ts_table->base_time;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070074}
75
76void timestamp_add_now(enum timestamp_id id)
77{
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070078 timestamp_add(id, timestamp_get());
Vadim Bendebury6f72d692011-09-21 16:12:39 -070079}
Stefan Reinauer4221a192012-10-15 15:23:20 -070080
Stefan Reinauer4221a192012-10-15 15:23:20 -070081#define MAX_TIMESTAMP_CACHE 8
82struct timestamp_cache {
83 enum timestamp_id id;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070084 uint64_t time;
Stefan Reinauer4221a192012-10-15 15:23:20 -070085} timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL;
86
87static int timestamp_entries CAR_GLOBAL = 0;
88
89/**
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030090 * timestamp_stash() allows to temporarily cache timestamps.
91 * This is needed when timestamping before the CBMEM area
92 * is initialized. The function timestamp_do_sync() is used to
93 * write the timestamps to the CBMEM area and this is done as
94 * part of CAR migration for romstage, and in ramstage main().
Stefan Reinauer4221a192012-10-15 15:23:20 -070095 */
96
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070097static void timestamp_stash(enum timestamp_id id, uint64_t ts_time)
Stefan Reinauer4221a192012-10-15 15:23:20 -070098{
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030099 struct timestamp_cache *ts_cache = car_get_var(timestamp_cache);
100 int ts_entries = car_get_var(timestamp_entries);
101
102 if (ts_entries >= MAX_TIMESTAMP_CACHE) {
Stefan Reinauer4221a192012-10-15 15:23:20 -0700103 printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n");
104 return;
105 }
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300106 ts_cache[ts_entries].id = id;
107 ts_cache[ts_entries].time = ts_time;
108 car_set_var(timestamp_entries, ++ts_entries);
Stefan Reinauer4221a192012-10-15 15:23:20 -0700109}
110
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300111static void timestamp_do_sync(void)
Stefan Reinauer4221a192012-10-15 15:23:20 -0700112{
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300113 struct timestamp_cache *ts_cache = car_get_var(timestamp_cache);
114 int ts_entries = car_get_var(timestamp_entries);
115
Stefan Reinauer4221a192012-10-15 15:23:20 -0700116 int i;
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300117 for (i = 0; i < ts_entries; i++)
118 timestamp_add(ts_cache[i].id, ts_cache[i].time);
119 car_set_var(timestamp_entries, 0);
Stefan Reinauer4221a192012-10-15 15:23:20 -0700120}
121
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700122void timestamp_init(uint64_t base)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300123{
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300124 if (!boot_cpu())
125 return;
126
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300127#ifdef __PRE_RAM__
128 /* Copy of basetime, it is too early for CBMEM. */
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300129 car_set_var(ts_basetime, base);
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300130#else
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300131 struct timestamp_table* tst;
132
133 /* Locate and use an already existing table. */
134 tst = cbmem_find(CBMEM_ID_TIMESTAMP);
135 if (tst) {
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300136 car_set_var(ts_table_p, tst);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300137 return;
138 }
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300139
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300140 /* Copy of basetime, may be too early for CBMEM. */
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300141 car_set_var(ts_basetime, base);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300142 timestamp_real_init(base);
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300143#endif
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300144}
145
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300146void timestamp_reinit(void)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300147{
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300148 if (!boot_cpu())
149 return;
150
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300151#ifdef __PRE_RAM__
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300152 timestamp_real_init(car_get_var(ts_basetime));
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300153#else
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300154 if (!car_get_var(ts_table_p))
155 timestamp_init(car_get_var(ts_basetime));
Stefan Reinauer4221a192012-10-15 15:23:20 -0700156#endif
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300157 if (car_get_var(ts_table_p))
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300158 timestamp_do_sync();
159}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300160
161/* Call timestamp_reinit at CAR migration time. */
162CAR_MIGRATE(timestamp_reinit)