blob: 5f84cf4ac87bb48e5c279295418a755ae7645e20 [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.
Vadim Bendebury6f72d692011-09-21 16:12:39 -070014 */
15
Aaron Durbin1936f6c2015-07-03 17:04:21 -050016#include <assert.h>
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030017#include <stddef.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070018#include <stdint.h>
19#include <console/console.h>
20#include <cbmem.h>
Aaron Durbin1936f6c2015-07-03 17:04:21 -050021#include <symbols.h>
Aaron Durbin2daadf82015-05-01 16:48:54 -050022#include <timer.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070023#include <timestamp.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070024#include <arch/early_variables.h>
Aaron Durbin2daadf82015-05-01 16:48:54 -050025#include <rules.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +020026#include <smp/node.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070027
Julius Werner69d20c42016-02-08 19:48:11 -080028#define MAX_TIMESTAMPS 84
Vadim Bendebury6f72d692011-09-21 16:12:39 -070029
Julius Werner85b1aad2016-08-19 15:17:42 -070030/* When changing this number, adjust TIMESTAMP() size ASSERT() in memlayout.h */
Julius Werner69d20c42016-02-08 19:48:11 -080031#define MAX_BSS_TIMESTAMP_CACHE 16
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030032
Aaron Durbin1936f6c2015-07-03 17:04:21 -050033struct __attribute__((__packed__)) timestamp_cache {
Aaron Durbinbd1499d2015-07-10 01:51:14 -050034 uint32_t cache_state;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050035 struct timestamp_table table;
36 /* The struct timestamp_table has a 0 length array as its last field.
37 * The following 'entries' array serves as the storage space for the
Julius Werner69d20c42016-02-08 19:48:11 -080038 * cache when allocated in the BSS. */
39 struct timestamp_entry entries[MAX_BSS_TIMESTAMP_CACHE];
Aaron Durbin1936f6c2015-07-03 17:04:21 -050040};
Vadim Bendebury6f72d692011-09-21 16:12:39 -070041
Julius Werner8c093772016-02-09 16:09:15 -080042DECLARE_OPTIONAL_REGION(timestamp);
43
44#if defined(__PRE_RAM__)
45#define USE_TIMESTAMP_REGION (_timestamp_size > 0)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050046#else
47#define USE_TIMESTAMP_REGION 0
48#endif
49
50/* The cache location will sit in BSS when in ramstage. */
51#define TIMESTAMP_CACHE_IN_BSS ENV_RAMSTAGE
52
Furquan Shaikhe85de022016-08-02 11:19:53 -070053#define HAS_CBMEM (ENV_ROMSTAGE || ENV_RAMSTAGE || ENV_POSTCAR)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050054
55/* Storage of cache entries during ramstage prior to cbmem coming online. */
56static struct timestamp_cache timestamp_cache;
57
58enum {
59 TIMESTAMP_CACHE_UNINITIALIZED = 0,
60 TIMESTAMP_CACHE_INITIALIZED,
61 TIMESTAMP_CACHE_NOT_NEEDED,
62};
63
64static void timestamp_cache_init(struct timestamp_cache *ts_cache,
65 uint64_t base)
66{
67 ts_cache->table.num_entries = 0;
Julius Werner69d20c42016-02-08 19:48:11 -080068 ts_cache->table.max_entries = MAX_BSS_TIMESTAMP_CACHE;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050069 ts_cache->table.base_time = base;
70 ts_cache->cache_state = TIMESTAMP_CACHE_INITIALIZED;
Julius Werner69d20c42016-02-08 19:48:11 -080071
72 if (USE_TIMESTAMP_REGION)
73 ts_cache->table.max_entries = (_timestamp_size -
74 offsetof(struct timestamp_cache, entries))
75 / sizeof(struct timestamp_entry);
Aaron Durbin1936f6c2015-07-03 17:04:21 -050076}
77
78static struct timestamp_cache *timestamp_cache_get(void)
79{
80 struct timestamp_cache *ts_cache = NULL;
81
82 if (TIMESTAMP_CACHE_IN_BSS) {
83 ts_cache = &timestamp_cache;
84 } else if (USE_TIMESTAMP_REGION) {
85 if (_timestamp_size < sizeof(*ts_cache))
86 BUG();
87 ts_cache = car_get_var_ptr((void *)_timestamp);
88 }
89
Aaron Durbin1936f6c2015-07-03 17:04:21 -050090 return ts_cache;
91}
92
93static struct timestamp_table *timestamp_alloc_cbmem_table(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070094{
Lee Leahyb2d834a2017-03-08 16:52:22 -080095 struct timestamp_table *tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070096
97 tst = cbmem_add(CBMEM_ID_TIMESTAMP,
98 sizeof(struct timestamp_table) +
99 MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
100
Kyösti Mälkkie0344172015-05-26 06:23:02 +0300101 if (!tst)
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500102 return NULL;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700103
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500104 tst->base_time = 0;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700105 tst->max_entries = MAX_TIMESTAMPS;
106 tst->num_entries = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300107
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500108 return tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700109}
110
Aaron Durbin2daadf82015-05-01 16:48:54 -0500111/* Determine if one should proceed into timestamp code. This is for protecting
112 * systems that have multiple processors running in romstage -- namely AMD
113 * based x86 platforms. */
114static int timestamp_should_run(void)
115{
116 /* Only check boot_cpu() in other stages than ramstage on x86. */
117 if ((!ENV_RAMSTAGE && IS_ENABLED(CONFIG_ARCH_X86)) && !boot_cpu())
118 return 0;
119
120 return 1;
121}
122
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500123static struct timestamp_table *timestamp_table_get(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700124{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500125 MAYBE_STATIC struct timestamp_table *ts_table = NULL;
126 struct timestamp_cache *ts_cache;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300127
Aaron Durbin2daadf82015-05-01 16:48:54 -0500128 if (!timestamp_should_run())
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500129 return NULL;
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300130
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500131 if (ts_table != NULL)
132 return ts_table;
133
134 ts_cache = timestamp_cache_get();
135
136 if (ts_cache == NULL) {
137 if (HAS_CBMEM)
138 ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
139 return ts_table;
140 }
141
142 /* Cache is required. */
143 if (ts_cache->cache_state != TIMESTAMP_CACHE_NOT_NEEDED)
144 return &ts_cache->table;
145
146 /* Cache shouldn't be used but there's no backing store. */
147 if (!HAS_CBMEM)
148 return NULL;
149
150 ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
151
152 return ts_table;
153}
154
155static void timestamp_add_table_entry(struct timestamp_table *ts_table,
156 enum timestamp_id id, uint64_t ts_time)
157{
158 struct timestamp_entry *tse;
159
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600160 if (ts_table->num_entries >= ts_table->max_entries)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300161 return;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700162
163 tse = &ts_table->entries[ts_table->num_entries++];
164 tse->entry_id = id;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700165 tse->entry_stamp = ts_time - ts_table->base_time;
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600166
167 if (ts_table->num_entries == ts_table->max_entries)
168 printk(BIOS_ERR, "ERROR: Timestamp table full\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700169}
170
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500171void timestamp_add(enum timestamp_id id, uint64_t ts_time)
172{
173 struct timestamp_table *ts_table;
174
175 ts_table = timestamp_table_get();
176
177 if (!ts_table) {
178 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
179 return;
180 }
181
182 timestamp_add_table_entry(ts_table, id, ts_time);
183}
184
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700185void timestamp_add_now(enum timestamp_id id)
186{
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700187 timestamp_add(id, timestamp_get());
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700188}
Stefan Reinauer4221a192012-10-15 15:23:20 -0700189
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700190void timestamp_init(uint64_t base)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300191{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500192 struct timestamp_cache *ts_cache;
193
Aaron Durbin2daadf82015-05-01 16:48:54 -0500194 if (!timestamp_should_run())
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300195 return;
196
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500197 ts_cache = timestamp_cache_get();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300198
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500199 if (!ts_cache) {
200 printk(BIOS_ERR, "ERROR: No timestamp cache to init\n");
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300201 return;
202 }
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300203
Aaron Durbinbd1499d2015-07-10 01:51:14 -0500204 /* In the EARLY_CBMEM_INIT case timestamps could have already been
205 * recovered. In those circumstances honor the cache which sits in BSS
206 * as it has already been initialized. */
Aaron Durbin2a983bd2015-07-11 13:36:01 -0500207 if (ENV_RAMSTAGE && IS_ENABLED(CONFIG_EARLY_CBMEM_INIT) &&
Aaron Durbinbd1499d2015-07-10 01:51:14 -0500208 ts_cache->cache_state != TIMESTAMP_CACHE_UNINITIALIZED)
209 return;
210
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500211 timestamp_cache_init(ts_cache, base);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300212}
213
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500214static void timestamp_sync_cache_to_cbmem(int is_recovery)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300215{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500216 uint32_t i;
217 struct timestamp_cache *ts_cache;
218 struct timestamp_table *ts_cache_table;
219 struct timestamp_table *ts_cbmem_table = NULL;
220
Aaron Durbin2daadf82015-05-01 16:48:54 -0500221 if (!timestamp_should_run())
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300222 return;
223
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500224 ts_cache = timestamp_cache_get();
225
226 /* No timestamp cache found */
227 if (ts_cache == NULL) {
228 printk(BIOS_ERR, "ERROR: No timestamp cache found\n");
229 return;
230 }
231
232 ts_cache_table = &ts_cache->table;
233
234 /* cbmem is being recovered. */
235 if (is_recovery) {
236 /* x86 resume path expects timestamps to be reset. */
237 if (IS_ENABLED(CONFIG_ARCH_ROMSTAGE_X86_32) && ENV_ROMSTAGE)
238 ts_cbmem_table = timestamp_alloc_cbmem_table();
239 else {
240 /* Find existing table in cbmem. */
241 ts_cbmem_table = cbmem_find(CBMEM_ID_TIMESTAMP);
242 /* No existing timestamp table. */
243 if (ts_cbmem_table == NULL)
244 ts_cbmem_table = timestamp_alloc_cbmem_table();
245 }
246 } else
247 /* First time sync. Add new table. */
248 ts_cbmem_table = timestamp_alloc_cbmem_table();
249
250 if (ts_cbmem_table == NULL) {
251 printk(BIOS_ERR, "ERROR: No timestamp table allocated\n");
252 return;
253 }
254
255 /*
256 * There's no need to worry about the base_time fields being out of
Julius Werner8c093772016-02-09 16:09:15 -0800257 * sync because only the following configurations are used/supported:
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500258 *
Julius Werner8c093772016-02-09 16:09:15 -0800259 * 1. Timestamps get initialized before ramstage, which implies
260 * CONFIG_EARLY_CBMEM_INIT and CBMEM initialization in romstage.
261 * This requires the board to define a TIMESTAMP() region in its
262 * memlayout.ld (default on x86). The base_time from timestamp_init()
263 * (usually called from bootblock.c on most non-x86 boards) persists
264 * in that region until it gets synced to CBMEM in romstage.
265 * In ramstage, the BSS cache's base_time will be 0 until the second
266 * sync, which will adjust the timestamps in there to the correct
267 * base_time (from CBMEM) with the timestamp_add_table_entry() below.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500268 *
Julius Werner8c093772016-02-09 16:09:15 -0800269 * 2. Timestamps only get initialized in ramstage *and*
270 * CONFIG_LATE_CBMEM_INIT is set. main() will call timestamp_init()
271 * very early (before any timestamps get logged) to set a base_time
272 * in the BSS cache, which will later get synced over to CBMEM.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500273 *
Julius Werner8c093772016-02-09 16:09:15 -0800274 * If you try to initialize timestamps before ramstage but don't define
275 * a TIMESTAMP region, all operations will fail (safely), and coreboot
276 * will behave as if timestamps only get initialized in ramstage.
277 *
278 * If CONFIG_EARLY_CBMEM_INIT is set but timestamps only get
279 * initialized in ramstage, the base_time from timestamp_init() will
280 * get ignored and all timestamps will be 0-based.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500281 */
282 for (i = 0; i < ts_cache_table->num_entries; i++) {
283 struct timestamp_entry *tse = &ts_cache_table->entries[i];
284 timestamp_add_table_entry(ts_cbmem_table, tse->entry_id,
285 tse->entry_stamp);
286 }
287
288 /* Freshly added cbmem table has base_time 0. Inherit cache base_time */
289 if (ts_cbmem_table->base_time == 0)
290 ts_cbmem_table->base_time = ts_cache_table->base_time;
291
Aaron Durbinc49014e2015-08-30 21:19:55 -0500292 /* Seed the timestamp tick frequency in ramstage. */
293 if (ENV_RAMSTAGE)
294 ts_cbmem_table->tick_freq_mhz = timestamp_tick_freq_mhz();
295
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500296 /* Cache no longer required. */
297 ts_cache_table->num_entries = 0;
298 ts_cache->cache_state = TIMESTAMP_CACHE_NOT_NEEDED;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300299}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300300
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500301ROMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem)
302RAMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500303
304/* Provide default timestamp implementation using monotonic timer. */
305uint64_t __attribute__((weak)) timestamp_get(void)
306{
307 struct mono_time t1, t2;
308
Aaron Durbin7aafe532015-05-07 11:32:30 -0500309 if (!IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
310 return 0;
311
Aaron Durbin9e80e272015-05-01 16:48:54 -0500312 mono_time_set_usecs(&t1, 0);
313 timer_monotonic_get(&t2);
314
315 return mono_time_diff_microseconds(&t1, &t2);
316}
Aaron Durbinc49014e2015-08-30 21:19:55 -0500317
318/* Like timestamp_get() above this matches up with microsecond granularity. */
319int __attribute__((weak)) timestamp_tick_freq_mhz(void)
320{
321 return 1;
322}