blob: f0ee48df721c21c740189979fac609ce5b07a586 [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;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030031static tsc_t ts_basetime CAR_GLOBAL = { .lo = 0, .hi =0 };
32
33static void timestamp_stash(enum timestamp_id id, tsc_t ts_time);
Vadim Bendebury6f72d692011-09-21 16:12:39 -070034
35static uint64_t tsc_to_uint64(tsc_t tstamp)
36{
37 return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
38}
39
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030040static void timestamp_real_init(tsc_t base)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070041{
42 struct timestamp_table* tst;
43
44 tst = cbmem_add(CBMEM_ID_TIMESTAMP,
45 sizeof(struct timestamp_table) +
46 MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
47
48 if (!tst) {
Paul Menzel2edf77c2013-01-22 11:46:34 +010049 printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -070050 return;
51 }
52
53 tst->base_time = tsc_to_uint64(base);
54 tst->max_entries = MAX_TIMESTAMPS;
55 tst->num_entries = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030056
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030057 car_set_var(ts_table_p, tst);
Vadim Bendebury6f72d692011-09-21 16:12:39 -070058}
59
60void timestamp_add(enum timestamp_id id, tsc_t ts_time)
61{
62 struct timestamp_entry *tse;
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030063 struct timestamp_table *ts_table = NULL;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030064
Kyösti Mälkkif56ff902013-09-08 13:10:28 +030065 if (!boot_cpu())
66 return;
67
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030068 ts_table = car_get_var(ts_table_p);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030069 if (!ts_table) {
70 timestamp_stash(id, ts_time);
71 return;
72 }
73 if (ts_table->num_entries == ts_table->max_entries)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070074 return;
75
76 tse = &ts_table->entries[ts_table->num_entries++];
77 tse->entry_id = id;
78 tse->entry_stamp = tsc_to_uint64(ts_time) - ts_table->base_time;
79}
80
81void timestamp_add_now(enum timestamp_id id)
82{
83 timestamp_add(id, rdtsc());
84}
Stefan Reinauer4221a192012-10-15 15:23:20 -070085
Stefan Reinauer4221a192012-10-15 15:23:20 -070086#define MAX_TIMESTAMP_CACHE 8
87struct timestamp_cache {
88 enum timestamp_id id;
89 tsc_t time;
90} timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL;
91
92static int timestamp_entries CAR_GLOBAL = 0;
93
94/**
Kyösti Mälkki29e9c222013-10-13 13:27:56 +030095 * timestamp_stash() allows to temporarily cache timestamps.
96 * This is needed when timestamping before the CBMEM area
97 * is initialized. The function timestamp_do_sync() is used to
98 * write the timestamps to the CBMEM area and this is done as
99 * part of CAR migration for romstage, and in ramstage main().
Stefan Reinauer4221a192012-10-15 15:23:20 -0700100 */
101
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300102static void timestamp_stash(enum timestamp_id id, tsc_t ts_time)
Stefan Reinauer4221a192012-10-15 15:23:20 -0700103{
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300104 struct timestamp_cache *ts_cache = car_get_var(timestamp_cache);
105 int ts_entries = car_get_var(timestamp_entries);
106
107 if (ts_entries >= MAX_TIMESTAMP_CACHE) {
Stefan Reinauer4221a192012-10-15 15:23:20 -0700108 printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n");
109 return;
110 }
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300111 ts_cache[ts_entries].id = id;
112 ts_cache[ts_entries].time = ts_time;
113 car_set_var(timestamp_entries, ++ts_entries);
Stefan Reinauer4221a192012-10-15 15:23:20 -0700114}
115
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300116static void timestamp_do_sync(void)
Stefan Reinauer4221a192012-10-15 15:23:20 -0700117{
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300118 struct timestamp_cache *ts_cache = car_get_var(timestamp_cache);
119 int ts_entries = car_get_var(timestamp_entries);
120
Stefan Reinauer4221a192012-10-15 15:23:20 -0700121 int i;
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300122 for (i = 0; i < ts_entries; i++)
123 timestamp_add(ts_cache[i].id, ts_cache[i].time);
124 car_set_var(timestamp_entries, 0);
Stefan Reinauer4221a192012-10-15 15:23:20 -0700125}
126
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300127void timestamp_init(tsc_t base)
128{
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300129 if (!boot_cpu())
130 return;
131
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300132#ifdef __PRE_RAM__
133 /* Copy of basetime, it is too early for CBMEM. */
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300134 car_set_var(ts_basetime, base);
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300135#else
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300136 struct timestamp_table* tst;
137
138 /* Locate and use an already existing table. */
139 tst = cbmem_find(CBMEM_ID_TIMESTAMP);
140 if (tst) {
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300141 car_set_var(ts_table_p, tst);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300142 return;
143 }
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300144
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300145 /* Copy of basetime, may be too early for CBMEM. */
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300146 car_set_var(ts_basetime, base);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300147 timestamp_real_init(base);
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300148#endif
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300149}
150
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300151void timestamp_reinit(void)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300152{
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300153 if (!boot_cpu())
154 return;
155
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300156#ifdef __PRE_RAM__
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300157 timestamp_real_init(car_get_var(ts_basetime));
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300158#else
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300159 if (!car_get_var(ts_table_p))
160 timestamp_init(car_get_var(ts_basetime));
Stefan Reinauer4221a192012-10-15 15:23:20 -0700161#endif
Kyösti Mälkki29e9c222013-10-13 13:27:56 +0300162 if (car_get_var(ts_table_p))
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300163 timestamp_do_sync();
164}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300165
166/* Call timestamp_reinit at CAR migration time. */
167CAR_MIGRATE(timestamp_reinit)