blob: df07bececb19d3140bc7d4dafeebd800e74fee2f [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Vadim Bendebury6f72d692011-09-21 16:12:39 -07003
Aaron Durbin1936f6c2015-07-03 17:04:21 -05004#include <assert.h>
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +03005#include <stddef.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -07006#include <stdint.h>
7#include <console/console.h>
8#include <cbmem.h>
Aaron Durbin1936f6c2015-07-03 17:04:21 -05009#include <symbols.h>
Aaron Durbin2daadf82015-05-01 16:48:54 -050010#include <timer.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070011#include <timestamp.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +020012#include <smp/node.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070013
Raul E Rangelb3e02202018-05-11 11:08:08 -060014#define MAX_TIMESTAMPS 192
Vadim Bendebury6f72d692011-09-21 16:12:39 -070015
Julius Werner8c093772016-02-09 16:09:15 -080016DECLARE_OPTIONAL_REGION(timestamp);
17
Kyösti Mälkki8b936892019-09-12 13:45:15 +030018/* This points to the active timestamp_table and can change within a stage
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +030019 as CBMEM comes available. */
Arthur Heymans1a711632019-11-20 20:38:29 +010020static struct timestamp_table *glob_ts_table;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050021
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030022static void timestamp_cache_init(struct timestamp_table *ts_cache,
Aaron Durbin1936f6c2015-07-03 17:04:21 -050023 uint64_t base)
24{
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030025 ts_cache->num_entries = 0;
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030026 ts_cache->base_time = base;
Kyösti Mälkki8b936892019-09-12 13:45:15 +030027 ts_cache->max_entries = (REGION_SIZE(timestamp) -
28 offsetof(struct timestamp_table, entries))
29 / sizeof(struct timestamp_entry);
Aaron Durbin1936f6c2015-07-03 17:04:21 -050030}
31
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030032static struct timestamp_table *timestamp_cache_get(void)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050033{
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030034 struct timestamp_table *ts_cache = NULL;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050035
Kyösti Mälkki8b936892019-09-12 13:45:15 +030036 if (!ENV_ROMSTAGE_OR_BEFORE)
37 return NULL;
38
39 if (REGION_SIZE(timestamp) < sizeof(*ts_cache)) {
40 BUG();
41 } else {
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +030042 ts_cache = (void *)_timestamp;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050043 }
44
Aaron Durbin1936f6c2015-07-03 17:04:21 -050045 return ts_cache;
46}
47
48static struct timestamp_table *timestamp_alloc_cbmem_table(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070049{
Lee Leahyb2d834a2017-03-08 16:52:22 -080050 struct timestamp_table *tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070051
52 tst = cbmem_add(CBMEM_ID_TIMESTAMP,
53 sizeof(struct timestamp_table) +
54 MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
55
Kyösti Mälkkie0344172015-05-26 06:23:02 +030056 if (!tst)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050057 return NULL;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070058
Aaron Durbin1936f6c2015-07-03 17:04:21 -050059 tst->base_time = 0;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070060 tst->max_entries = MAX_TIMESTAMPS;
61 tst->num_entries = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030062
Aaron Durbin1936f6c2015-07-03 17:04:21 -050063 return tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070064}
65
Aaron Durbin2daadf82015-05-01 16:48:54 -050066/* Determine if one should proceed into timestamp code. This is for protecting
67 * systems that have multiple processors running in romstage -- namely AMD
68 * based x86 platforms. */
69static int timestamp_should_run(void)
70{
Subrata Banik42c44c22019-05-15 20:27:04 +053071 /*
72 * Only check boot_cpu() in other stages than
73 * ENV_PAYLOAD_LOADER on x86.
74 */
75 if ((!ENV_PAYLOAD_LOADER && CONFIG(ARCH_X86)) && !boot_cpu())
Aaron Durbin2daadf82015-05-01 16:48:54 -050076 return 0;
77
78 return 1;
79}
80
Aaron Durbin1936f6c2015-07-03 17:04:21 -050081static struct timestamp_table *timestamp_table_get(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070082{
Arthur Heymans1a711632019-11-20 20:38:29 +010083 if (glob_ts_table)
84 return glob_ts_table;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030085
Arthur Heymans1a711632019-11-20 20:38:29 +010086 glob_ts_table = timestamp_cache_get();
Aaron Durbin1936f6c2015-07-03 17:04:21 -050087
Arthur Heymans1a711632019-11-20 20:38:29 +010088 return glob_ts_table;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050089}
90
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +030091static void timestamp_table_set(struct timestamp_table *ts)
92{
Arthur Heymans1a711632019-11-20 20:38:29 +010093 glob_ts_table = ts;
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +030094}
95
Martin Rothb22bbe22018-03-07 15:32:16 -070096static const char *timestamp_name(enum timestamp_id id)
97{
98 int i;
99
100 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
101 if (timestamp_ids[i].id == id)
102 return timestamp_ids[i].name;
103 }
104
105 return "Unknown timestamp ID";
106}
107
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500108static void timestamp_add_table_entry(struct timestamp_table *ts_table,
109 enum timestamp_id id, uint64_t ts_time)
110{
111 struct timestamp_entry *tse;
112
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600113 if (ts_table->num_entries >= ts_table->max_entries)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300114 return;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700115
116 tse = &ts_table->entries[ts_table->num_entries++];
117 tse->entry_id = id;
Kyösti Mälkkid548edd2020-01-09 08:41:46 +0200118 tse->entry_stamp = ts_time;
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600119
120 if (ts_table->num_entries == ts_table->max_entries)
121 printk(BIOS_ERR, "ERROR: Timestamp table full\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700122}
123
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500124void timestamp_add(enum timestamp_id id, uint64_t ts_time)
125{
126 struct timestamp_table *ts_table;
127
Kyösti Mälkki187e4c42019-02-20 17:38:45 +0200128 if (!timestamp_should_run())
129 return;
130
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500131 ts_table = timestamp_table_get();
132
133 if (!ts_table) {
134 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
135 return;
136 }
137
Kyösti Mälkkid548edd2020-01-09 08:41:46 +0200138 ts_time -= ts_table->base_time;
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500139 timestamp_add_table_entry(ts_table, id, ts_time);
Kyösti Mälkki8b93cb72020-01-09 08:41:46 +0200140
141 if (CONFIG(TIMESTAMPS_ON_CONSOLE))
142 printk(BIOS_INFO, "Timestamp - %s: %llu\n", timestamp_name(id), ts_time);
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500143}
144
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700145void timestamp_add_now(enum timestamp_id id)
146{
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700147 timestamp_add(id, timestamp_get());
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700148}
Stefan Reinauer4221a192012-10-15 15:23:20 -0700149
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700150void timestamp_init(uint64_t base)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300151{
Kyösti Mälkki3c559e72019-09-07 13:25:47 +0300152 struct timestamp_table *ts_cache;
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500153
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300154 assert(ENV_ROMSTAGE_OR_BEFORE);
155
Aaron Durbin2daadf82015-05-01 16:48:54 -0500156 if (!timestamp_should_run())
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300157 return;
158
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500159 ts_cache = timestamp_cache_get();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300160
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500161 if (!ts_cache) {
162 printk(BIOS_ERR, "ERROR: No timestamp cache to init\n");
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300163 return;
164 }
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300165
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500166 timestamp_cache_init(ts_cache, base);
Kyösti Mälkki3c559e72019-09-07 13:25:47 +0300167 timestamp_table_set(ts_cache);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300168}
169
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300170static void timestamp_sync_cache_to_cbmem(struct timestamp_table *ts_cbmem_table)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300171{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500172 uint32_t i;
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500173 struct timestamp_table *ts_cache_table;
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300174
175 ts_cache_table = timestamp_table_get();
176 if (!ts_cache_table) {
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300177 printk(BIOS_ERR, "ERROR: No timestamp cache found\n");
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500178 return;
179 }
180
181 /*
182 * There's no need to worry about the base_time fields being out of
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300183 * sync because only the following configuration is used/supported:
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500184 *
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300185 * Timestamps get initialized before ramstage, which implies
186 * CBMEM initialization in romstage.
Julius Werner8c093772016-02-09 16:09:15 -0800187 * This requires the board to define a TIMESTAMP() region in its
188 * memlayout.ld (default on x86). The base_time from timestamp_init()
189 * (usually called from bootblock.c on most non-x86 boards) persists
190 * in that region until it gets synced to CBMEM in romstage.
191 * In ramstage, the BSS cache's base_time will be 0 until the second
192 * sync, which will adjust the timestamps in there to the correct
193 * base_time (from CBMEM) with the timestamp_add_table_entry() below.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500194 *
Julius Werner8c093772016-02-09 16:09:15 -0800195 * If you try to initialize timestamps before ramstage but don't define
196 * a TIMESTAMP region, all operations will fail (safely), and coreboot
Kyösti Mälkkid548edd2020-01-09 08:41:46 +0200197 * will behave as if timestamps collection was disabled.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500198 */
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300199
Kyösti Mälkkid548edd2020-01-09 08:41:46 +0200200 /* Inherit cache base_time. */
201 ts_cbmem_table->base_time = ts_cache_table->base_time;
202
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500203 for (i = 0; i < ts_cache_table->num_entries; i++) {
204 struct timestamp_entry *tse = &ts_cache_table->entries[i];
205 timestamp_add_table_entry(ts_cbmem_table, tse->entry_id,
206 tse->entry_stamp);
207 }
208
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500209 /* Cache no longer required. */
210 ts_cache_table->num_entries = 0;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300211}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300212
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300213static void timestamp_reinit(int is_recovery)
214{
215 struct timestamp_table *ts_cbmem_table;
216
217 if (!timestamp_should_run())
218 return;
219
Kyösti Mälkkib6b13c92019-09-12 12:58:50 +0300220 /* First time into romstage we make a clean new table. For platforms that travel
221 through this path on resume, ARCH_X86 S3, timestamps are also reset. */
222 if (ENV_ROMSTAGE) {
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300223 ts_cbmem_table = timestamp_alloc_cbmem_table();
Kyösti Mälkkib6b13c92019-09-12 12:58:50 +0300224 } else {
225 /* Find existing table in cbmem. */
226 ts_cbmem_table = cbmem_find(CBMEM_ID_TIMESTAMP);
227 }
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300228
229 if (ts_cbmem_table == NULL) {
230 printk(BIOS_ERR, "ERROR: No timestamp table allocated\n");
231 timestamp_table_set(NULL);
232 return;
233 }
234
235 if (ENV_ROMSTAGE)
236 timestamp_sync_cache_to_cbmem(ts_cbmem_table);
237
238 /* Seed the timestamp tick frequency in ENV_PAYLOAD_LOADER. */
239 if (ENV_PAYLOAD_LOADER)
240 ts_cbmem_table->tick_freq_mhz = timestamp_tick_freq_mhz();
241
242 timestamp_table_set(ts_cbmem_table);
243}
244
Kyösti Mälkki806ea08b2017-07-15 20:42:20 +0300245void timestamp_rescale_table(uint16_t N, uint16_t M)
246{
247 uint32_t i;
248 struct timestamp_table *ts_table;
249
250 if (!timestamp_should_run())
251 return;
252
253 if (N == 0 || M == 0)
254 return;
255
256 ts_table = timestamp_table_get();
257
258 /* No timestamp table found */
259 if (ts_table == NULL) {
260 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
261 return;
262 }
263
264 ts_table->base_time /= M;
265 ts_table->base_time *= N;
266 for (i = 0; i < ts_table->num_entries; i++) {
267 struct timestamp_entry *tse = &ts_table->entries[i];
268 tse->entry_stamp /= M;
269 tse->entry_stamp *= N;
270 }
271}
272
Werner Zeh35cceb82017-09-12 07:42:54 +0200273/*
274 * Get the time in microseconds since boot (or more precise: since timestamp
275 * table was initialized).
276 */
277uint32_t get_us_since_boot(void)
278{
279 struct timestamp_table *ts = timestamp_table_get();
280
281 if (ts == NULL || ts->tick_freq_mhz == 0)
282 return 0;
283 return (timestamp_get() - ts->base_time) / ts->tick_freq_mhz;
284}
285
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300286ROMSTAGE_CBMEM_INIT_HOOK(timestamp_reinit)
287POSTCAR_CBMEM_INIT_HOOK(timestamp_reinit)
288RAMSTAGE_CBMEM_INIT_HOOK(timestamp_reinit)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500289
290/* Provide default timestamp implementation using monotonic timer. */
Aaron Durbin64031672018-04-21 14:45:32 -0600291uint64_t __weak timestamp_get(void)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500292{
293 struct mono_time t1, t2;
294
Julius Wernercd49cce2019-03-05 16:53:33 -0800295 if (!CONFIG(HAVE_MONOTONIC_TIMER))
Aaron Durbin7aafe532015-05-07 11:32:30 -0500296 return 0;
297
Aaron Durbin9e80e272015-05-01 16:48:54 -0500298 mono_time_set_usecs(&t1, 0);
299 timer_monotonic_get(&t2);
300
301 return mono_time_diff_microseconds(&t1, &t2);
302}
Aaron Durbinc49014e2015-08-30 21:19:55 -0500303
304/* Like timestamp_get() above this matches up with microsecond granularity. */
Aaron Durbin64031672018-04-21 14:45:32 -0600305int __weak timestamp_tick_freq_mhz(void)
Aaron Durbinc49014e2015-08-30 21:19:55 -0500306{
307 return 1;
308}