blob: d2206f613463979c9738e27a7c2442d2254db7e0 [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.
Mario Scheithauera39aede2017-11-06 16:47:27 +01005 * Copyright (C) 2017 Siemens AG
Vadim Bendebury6f72d692011-09-21 16:12:39 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Vadim Bendebury6f72d692011-09-21 16:12:39 -070015 */
16
Aaron Durbin1936f6c2015-07-03 17:04:21 -050017#include <assert.h>
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030018#include <stddef.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070019#include <stdint.h>
20#include <console/console.h>
21#include <cbmem.h>
Aaron Durbin1936f6c2015-07-03 17:04:21 -050022#include <symbols.h>
Aaron Durbin2daadf82015-05-01 16:48:54 -050023#include <timer.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070024#include <timestamp.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +020025#include <smp/node.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070026
Raul E Rangelb3e02202018-05-11 11:08:08 -060027#define MAX_TIMESTAMPS 192
Vadim Bendebury6f72d692011-09-21 16:12:39 -070028
Julius Werner8c093772016-02-09 16:09:15 -080029DECLARE_OPTIONAL_REGION(timestamp);
30
Kyösti Mälkki8b936892019-09-12 13:45:15 +030031/* This points to the active timestamp_table and can change within a stage
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +030032 as CBMEM comes available. */
Arthur Heymans1a711632019-11-20 20:38:29 +010033static struct timestamp_table *glob_ts_table;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050034
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030035static void timestamp_cache_init(struct timestamp_table *ts_cache,
Aaron Durbin1936f6c2015-07-03 17:04:21 -050036 uint64_t base)
37{
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030038 ts_cache->num_entries = 0;
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030039 ts_cache->base_time = base;
Kyösti Mälkki8b936892019-09-12 13:45:15 +030040 ts_cache->max_entries = (REGION_SIZE(timestamp) -
41 offsetof(struct timestamp_table, entries))
42 / sizeof(struct timestamp_entry);
Aaron Durbin1936f6c2015-07-03 17:04:21 -050043}
44
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030045static struct timestamp_table *timestamp_cache_get(void)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050046{
Kyösti Mälkki3c559e72019-09-07 13:25:47 +030047 struct timestamp_table *ts_cache = NULL;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050048
Kyösti Mälkki8b936892019-09-12 13:45:15 +030049 if (!ENV_ROMSTAGE_OR_BEFORE)
50 return NULL;
51
52 if (REGION_SIZE(timestamp) < sizeof(*ts_cache)) {
53 BUG();
54 } else {
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +030055 ts_cache = (void *)_timestamp;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050056 }
57
Aaron Durbin1936f6c2015-07-03 17:04:21 -050058 return ts_cache;
59}
60
61static struct timestamp_table *timestamp_alloc_cbmem_table(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070062{
Lee Leahyb2d834a2017-03-08 16:52:22 -080063 struct timestamp_table *tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070064
65 tst = cbmem_add(CBMEM_ID_TIMESTAMP,
66 sizeof(struct timestamp_table) +
67 MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
68
Kyösti Mälkkie0344172015-05-26 06:23:02 +030069 if (!tst)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050070 return NULL;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070071
Aaron Durbin1936f6c2015-07-03 17:04:21 -050072 tst->base_time = 0;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070073 tst->max_entries = MAX_TIMESTAMPS;
74 tst->num_entries = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030075
Aaron Durbin1936f6c2015-07-03 17:04:21 -050076 return tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070077}
78
Aaron Durbin2daadf82015-05-01 16:48:54 -050079/* Determine if one should proceed into timestamp code. This is for protecting
80 * systems that have multiple processors running in romstage -- namely AMD
81 * based x86 platforms. */
82static int timestamp_should_run(void)
83{
Subrata Banik42c44c22019-05-15 20:27:04 +053084 /*
85 * Only check boot_cpu() in other stages than
86 * ENV_PAYLOAD_LOADER on x86.
87 */
88 if ((!ENV_PAYLOAD_LOADER && CONFIG(ARCH_X86)) && !boot_cpu())
Aaron Durbin2daadf82015-05-01 16:48:54 -050089 return 0;
90
91 return 1;
92}
93
Aaron Durbin1936f6c2015-07-03 17:04:21 -050094static struct timestamp_table *timestamp_table_get(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070095{
Arthur Heymans1a711632019-11-20 20:38:29 +010096 if (glob_ts_table)
97 return glob_ts_table;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030098
Arthur Heymans1a711632019-11-20 20:38:29 +010099 glob_ts_table = timestamp_cache_get();
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500100
Arthur Heymans1a711632019-11-20 20:38:29 +0100101 return glob_ts_table;
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500102}
103
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300104static void timestamp_table_set(struct timestamp_table *ts)
105{
Arthur Heymans1a711632019-11-20 20:38:29 +0100106 glob_ts_table = ts;
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300107}
108
Martin Rothb22bbe22018-03-07 15:32:16 -0700109static const char *timestamp_name(enum timestamp_id id)
110{
111 int i;
112
113 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
114 if (timestamp_ids[i].id == id)
115 return timestamp_ids[i].name;
116 }
117
118 return "Unknown timestamp ID";
119}
120
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500121static void timestamp_add_table_entry(struct timestamp_table *ts_table,
122 enum timestamp_id id, uint64_t ts_time)
123{
124 struct timestamp_entry *tse;
125
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600126 if (ts_table->num_entries >= ts_table->max_entries)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300127 return;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700128
129 tse = &ts_table->entries[ts_table->num_entries++];
130 tse->entry_id = id;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700131 tse->entry_stamp = ts_time - ts_table->base_time;
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600132
133 if (ts_table->num_entries == ts_table->max_entries)
134 printk(BIOS_ERR, "ERROR: Timestamp table full\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700135}
136
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500137void timestamp_add(enum timestamp_id id, uint64_t ts_time)
138{
139 struct timestamp_table *ts_table;
140
Kyösti Mälkki187e4c42019-02-20 17:38:45 +0200141 if (!timestamp_should_run())
142 return;
143
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500144 ts_table = timestamp_table_get();
145
146 if (!ts_table) {
147 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
148 return;
149 }
150
151 timestamp_add_table_entry(ts_table, id, ts_time);
Kyösti Mälkki8b93cb72020-01-09 08:41:46 +0200152
153 if (CONFIG(TIMESTAMPS_ON_CONSOLE))
154 printk(BIOS_INFO, "Timestamp - %s: %llu\n", timestamp_name(id), ts_time);
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500155}
156
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700157void timestamp_add_now(enum timestamp_id id)
158{
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700159 timestamp_add(id, timestamp_get());
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700160}
Stefan Reinauer4221a192012-10-15 15:23:20 -0700161
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700162void timestamp_init(uint64_t base)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300163{
Kyösti Mälkki3c559e72019-09-07 13:25:47 +0300164 struct timestamp_table *ts_cache;
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500165
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300166 assert(ENV_ROMSTAGE_OR_BEFORE);
167
Aaron Durbin2daadf82015-05-01 16:48:54 -0500168 if (!timestamp_should_run())
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300169 return;
170
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500171 ts_cache = timestamp_cache_get();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300172
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500173 if (!ts_cache) {
174 printk(BIOS_ERR, "ERROR: No timestamp cache to init\n");
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300175 return;
176 }
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300177
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500178 timestamp_cache_init(ts_cache, base);
Kyösti Mälkki3c559e72019-09-07 13:25:47 +0300179 timestamp_table_set(ts_cache);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300180}
181
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300182static void timestamp_sync_cache_to_cbmem(struct timestamp_table *ts_cbmem_table)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300183{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500184 uint32_t i;
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500185 struct timestamp_table *ts_cache_table;
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300186
187 ts_cache_table = timestamp_table_get();
188 if (!ts_cache_table) {
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300189 printk(BIOS_ERR, "ERROR: No timestamp cache found\n");
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500190 return;
191 }
192
193 /*
194 * There's no need to worry about the base_time fields being out of
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300195 * sync because only the following configuration is used/supported:
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500196 *
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300197 * Timestamps get initialized before ramstage, which implies
198 * CBMEM initialization in romstage.
Julius Werner8c093772016-02-09 16:09:15 -0800199 * This requires the board to define a TIMESTAMP() region in its
200 * memlayout.ld (default on x86). The base_time from timestamp_init()
201 * (usually called from bootblock.c on most non-x86 boards) persists
202 * in that region until it gets synced to CBMEM in romstage.
203 * In ramstage, the BSS cache's base_time will be 0 until the second
204 * sync, which will adjust the timestamps in there to the correct
205 * base_time (from CBMEM) with the timestamp_add_table_entry() below.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500206 *
Julius Werner8c093772016-02-09 16:09:15 -0800207 * If you try to initialize timestamps before ramstage but don't define
208 * a TIMESTAMP region, all operations will fail (safely), and coreboot
209 * will behave as if timestamps only get initialized in ramstage.
210 *
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300211 * If timestamps only get initialized in ramstage, the base_time from
212 * timestamp_init() will get ignored and all timestamps will be 0-based.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500213 */
Kyösti Mälkki3dd23a52019-08-22 15:06:50 +0300214
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500215 for (i = 0; i < ts_cache_table->num_entries; i++) {
216 struct timestamp_entry *tse = &ts_cache_table->entries[i];
217 timestamp_add_table_entry(ts_cbmem_table, tse->entry_id,
218 tse->entry_stamp);
219 }
220
221 /* Freshly added cbmem table has base_time 0. Inherit cache base_time */
222 if (ts_cbmem_table->base_time == 0)
223 ts_cbmem_table->base_time = ts_cache_table->base_time;
224
225 /* Cache no longer required. */
226 ts_cache_table->num_entries = 0;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300227}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300228
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300229static void timestamp_reinit(int is_recovery)
230{
231 struct timestamp_table *ts_cbmem_table;
232
233 if (!timestamp_should_run())
234 return;
235
Kyösti Mälkkib6b13c92019-09-12 12:58:50 +0300236 /* First time into romstage we make a clean new table. For platforms that travel
237 through this path on resume, ARCH_X86 S3, timestamps are also reset. */
238 if (ENV_ROMSTAGE) {
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300239 ts_cbmem_table = timestamp_alloc_cbmem_table();
Kyösti Mälkkib6b13c92019-09-12 12:58:50 +0300240 } else {
241 /* Find existing table in cbmem. */
242 ts_cbmem_table = cbmem_find(CBMEM_ID_TIMESTAMP);
243 }
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300244
245 if (ts_cbmem_table == NULL) {
246 printk(BIOS_ERR, "ERROR: No timestamp table allocated\n");
247 timestamp_table_set(NULL);
248 return;
249 }
250
251 if (ENV_ROMSTAGE)
252 timestamp_sync_cache_to_cbmem(ts_cbmem_table);
253
254 /* Seed the timestamp tick frequency in ENV_PAYLOAD_LOADER. */
255 if (ENV_PAYLOAD_LOADER)
256 ts_cbmem_table->tick_freq_mhz = timestamp_tick_freq_mhz();
257
258 timestamp_table_set(ts_cbmem_table);
259}
260
Kyösti Mälkki806ea08b2017-07-15 20:42:20 +0300261void timestamp_rescale_table(uint16_t N, uint16_t M)
262{
263 uint32_t i;
264 struct timestamp_table *ts_table;
265
266 if (!timestamp_should_run())
267 return;
268
269 if (N == 0 || M == 0)
270 return;
271
272 ts_table = timestamp_table_get();
273
274 /* No timestamp table found */
275 if (ts_table == NULL) {
276 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
277 return;
278 }
279
280 ts_table->base_time /= M;
281 ts_table->base_time *= N;
282 for (i = 0; i < ts_table->num_entries; i++) {
283 struct timestamp_entry *tse = &ts_table->entries[i];
284 tse->entry_stamp /= M;
285 tse->entry_stamp *= N;
286 }
287}
288
Werner Zeh35cceb82017-09-12 07:42:54 +0200289/*
290 * Get the time in microseconds since boot (or more precise: since timestamp
291 * table was initialized).
292 */
293uint32_t get_us_since_boot(void)
294{
295 struct timestamp_table *ts = timestamp_table_get();
296
297 if (ts == NULL || ts->tick_freq_mhz == 0)
298 return 0;
299 return (timestamp_get() - ts->base_time) / ts->tick_freq_mhz;
300}
301
Kyösti Mälkki72e634f2019-09-05 18:16:25 +0300302ROMSTAGE_CBMEM_INIT_HOOK(timestamp_reinit)
303POSTCAR_CBMEM_INIT_HOOK(timestamp_reinit)
304RAMSTAGE_CBMEM_INIT_HOOK(timestamp_reinit)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500305
306/* Provide default timestamp implementation using monotonic timer. */
Aaron Durbin64031672018-04-21 14:45:32 -0600307uint64_t __weak timestamp_get(void)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500308{
309 struct mono_time t1, t2;
310
Julius Wernercd49cce2019-03-05 16:53:33 -0800311 if (!CONFIG(HAVE_MONOTONIC_TIMER))
Aaron Durbin7aafe532015-05-07 11:32:30 -0500312 return 0;
313
Aaron Durbin9e80e272015-05-01 16:48:54 -0500314 mono_time_set_usecs(&t1, 0);
315 timer_monotonic_get(&t2);
316
317 return mono_time_diff_microseconds(&t1, &t2);
318}
Aaron Durbinc49014e2015-08-30 21:19:55 -0500319
320/* Like timestamp_get() above this matches up with microsecond granularity. */
Aaron Durbin64031672018-04-21 14:45:32 -0600321int __weak timestamp_tick_freq_mhz(void)
Aaron Durbinc49014e2015-08-30 21:19:55 -0500322{
323 return 1;
324}