blob: 36d02516a39e739b73c03b4b7f2e100f81ff49b4 [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>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070025#include <arch/early_variables.h>
Aaron Durbin2daadf82015-05-01 16:48:54 -050026#include <rules.h>
Kyösti Mälkki5a5c8862014-01-26 14:41:54 +020027#include <smp/node.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070028
Raul E Rangelb3e02202018-05-11 11:08:08 -060029#define MAX_TIMESTAMPS 192
Vadim Bendebury6f72d692011-09-21 16:12:39 -070030
Julius Werner85b1aad2016-08-19 15:17:42 -070031/* When changing this number, adjust TIMESTAMP() size ASSERT() in memlayout.h */
Julius Werner69d20c42016-02-08 19:48:11 -080032#define MAX_BSS_TIMESTAMP_CACHE 16
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030033
Stefan Reinauer6a001132017-07-13 02:20:27 +020034struct __packed timestamp_cache {
Aaron Durbinbd1499d2015-07-10 01:51:14 -050035 uint32_t cache_state;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050036 struct timestamp_table table;
37 /* The struct timestamp_table has a 0 length array as its last field.
38 * The following 'entries' array serves as the storage space for the
Julius Werner69d20c42016-02-08 19:48:11 -080039 * cache when allocated in the BSS. */
40 struct timestamp_entry entries[MAX_BSS_TIMESTAMP_CACHE];
Aaron Durbin1936f6c2015-07-03 17:04:21 -050041};
Vadim Bendebury6f72d692011-09-21 16:12:39 -070042
Julius Werner8c093772016-02-09 16:09:15 -080043DECLARE_OPTIONAL_REGION(timestamp);
44
45#if defined(__PRE_RAM__)
46#define USE_TIMESTAMP_REGION (_timestamp_size > 0)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050047#else
48#define USE_TIMESTAMP_REGION 0
49#endif
50
51/* The cache location will sit in BSS when in ramstage. */
52#define TIMESTAMP_CACHE_IN_BSS ENV_RAMSTAGE
53
Furquan Shaikhe85de022016-08-02 11:19:53 -070054#define HAS_CBMEM (ENV_ROMSTAGE || ENV_RAMSTAGE || ENV_POSTCAR)
Aaron Durbin1936f6c2015-07-03 17:04:21 -050055
56/* Storage of cache entries during ramstage prior to cbmem coming online. */
57static struct timestamp_cache timestamp_cache;
58
59enum {
60 TIMESTAMP_CACHE_UNINITIALIZED = 0,
61 TIMESTAMP_CACHE_INITIALIZED,
62 TIMESTAMP_CACHE_NOT_NEEDED,
63};
64
65static void timestamp_cache_init(struct timestamp_cache *ts_cache,
66 uint64_t base)
67{
68 ts_cache->table.num_entries = 0;
Julius Werner69d20c42016-02-08 19:48:11 -080069 ts_cache->table.max_entries = MAX_BSS_TIMESTAMP_CACHE;
Aaron Durbin1936f6c2015-07-03 17:04:21 -050070 ts_cache->table.base_time = base;
71 ts_cache->cache_state = TIMESTAMP_CACHE_INITIALIZED;
Julius Werner69d20c42016-02-08 19:48:11 -080072
73 if (USE_TIMESTAMP_REGION)
74 ts_cache->table.max_entries = (_timestamp_size -
75 offsetof(struct timestamp_cache, entries))
76 / sizeof(struct timestamp_entry);
Aaron Durbin1936f6c2015-07-03 17:04:21 -050077}
78
79static struct timestamp_cache *timestamp_cache_get(void)
80{
81 struct timestamp_cache *ts_cache = NULL;
82
83 if (TIMESTAMP_CACHE_IN_BSS) {
84 ts_cache = &timestamp_cache;
85 } else if (USE_TIMESTAMP_REGION) {
86 if (_timestamp_size < sizeof(*ts_cache))
87 BUG();
88 ts_cache = car_get_var_ptr((void *)_timestamp);
89 }
90
Aaron Durbin1936f6c2015-07-03 17:04:21 -050091 return ts_cache;
92}
93
94static struct timestamp_table *timestamp_alloc_cbmem_table(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070095{
Lee Leahyb2d834a2017-03-08 16:52:22 -080096 struct timestamp_table *tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070097
98 tst = cbmem_add(CBMEM_ID_TIMESTAMP,
99 sizeof(struct timestamp_table) +
100 MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
101
Kyösti Mälkkie0344172015-05-26 06:23:02 +0300102 if (!tst)
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500103 return NULL;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700104
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500105 tst->base_time = 0;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700106 tst->max_entries = MAX_TIMESTAMPS;
107 tst->num_entries = 0;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300108
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500109 return tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700110}
111
Aaron Durbin2daadf82015-05-01 16:48:54 -0500112/* Determine if one should proceed into timestamp code. This is for protecting
113 * systems that have multiple processors running in romstage -- namely AMD
114 * based x86 platforms. */
115static int timestamp_should_run(void)
116{
117 /* Only check boot_cpu() in other stages than ramstage on x86. */
118 if ((!ENV_RAMSTAGE && IS_ENABLED(CONFIG_ARCH_X86)) && !boot_cpu())
119 return 0;
120
121 return 1;
122}
123
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500124static struct timestamp_table *timestamp_table_get(void)
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700125{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500126 MAYBE_STATIC struct timestamp_table *ts_table = NULL;
127 struct timestamp_cache *ts_cache;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300128
Aaron Durbin2daadf82015-05-01 16:48:54 -0500129 if (!timestamp_should_run())
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500130 return NULL;
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300131
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500132 if (ts_table != NULL)
133 return ts_table;
134
135 ts_cache = timestamp_cache_get();
136
137 if (ts_cache == NULL) {
138 if (HAS_CBMEM)
139 ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
140 return ts_table;
141 }
142
143 /* Cache is required. */
144 if (ts_cache->cache_state != TIMESTAMP_CACHE_NOT_NEEDED)
145 return &ts_cache->table;
146
147 /* Cache shouldn't be used but there's no backing store. */
148 if (!HAS_CBMEM)
149 return NULL;
150
151 ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
152
153 return ts_table;
154}
155
Martin Rothb22bbe22018-03-07 15:32:16 -0700156static const char *timestamp_name(enum timestamp_id id)
157{
158 int i;
159
160 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
161 if (timestamp_ids[i].id == id)
162 return timestamp_ids[i].name;
163 }
164
165 return "Unknown timestamp ID";
166}
167
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500168static void timestamp_add_table_entry(struct timestamp_table *ts_table,
169 enum timestamp_id id, uint64_t ts_time)
170{
171 struct timestamp_entry *tse;
172
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600173 if (ts_table->num_entries >= ts_table->max_entries)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300174 return;
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700175
176 tse = &ts_table->entries[ts_table->num_entries++];
177 tse->entry_id = id;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700178 tse->entry_stamp = ts_time - ts_table->base_time;
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600179
Martin Rothb22bbe22018-03-07 15:32:16 -0700180 if (IS_ENABLED(CONFIG_TIMESTAMPS_ON_CONSOLE))
181 printk(BIOS_SPEW, "Timestamp - %s: %" PRIu64 "\n",
182 timestamp_name(id), ts_time);
183
Ben Gardnerf5fd4c92015-11-20 13:25:25 -0600184 if (ts_table->num_entries == ts_table->max_entries)
185 printk(BIOS_ERR, "ERROR: Timestamp table full\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700186}
187
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500188void timestamp_add(enum timestamp_id id, uint64_t ts_time)
189{
190 struct timestamp_table *ts_table;
191
192 ts_table = timestamp_table_get();
193
194 if (!ts_table) {
195 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
196 return;
197 }
198
199 timestamp_add_table_entry(ts_table, id, ts_time);
200}
201
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700202void timestamp_add_now(enum timestamp_id id)
203{
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700204 timestamp_add(id, timestamp_get());
Vadim Bendebury6f72d692011-09-21 16:12:39 -0700205}
Stefan Reinauer4221a192012-10-15 15:23:20 -0700206
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700207void timestamp_init(uint64_t base)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300208{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500209 struct timestamp_cache *ts_cache;
210
Aaron Durbin2daadf82015-05-01 16:48:54 -0500211 if (!timestamp_should_run())
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300212 return;
213
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500214 ts_cache = timestamp_cache_get();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300215
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500216 if (!ts_cache) {
217 printk(BIOS_ERR, "ERROR: No timestamp cache to init\n");
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300218 return;
219 }
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300220
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300221 /* Timestamps could have already been recovered.
222 * In those circumstances honor the cache which sits in BSS
Aaron Durbinbd1499d2015-07-10 01:51:14 -0500223 * as it has already been initialized. */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300224 if (ENV_RAMSTAGE &&
Aaron Durbinbd1499d2015-07-10 01:51:14 -0500225 ts_cache->cache_state != TIMESTAMP_CACHE_UNINITIALIZED)
226 return;
227
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500228 timestamp_cache_init(ts_cache, base);
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300229}
230
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500231static void timestamp_sync_cache_to_cbmem(int is_recovery)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300232{
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500233 uint32_t i;
234 struct timestamp_cache *ts_cache;
235 struct timestamp_table *ts_cache_table;
236 struct timestamp_table *ts_cbmem_table = NULL;
237
Aaron Durbin2daadf82015-05-01 16:48:54 -0500238 if (!timestamp_should_run())
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300239 return;
240
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500241 ts_cache = timestamp_cache_get();
242
243 /* No timestamp cache found */
244 if (ts_cache == NULL) {
245 printk(BIOS_ERR, "ERROR: No timestamp cache found\n");
246 return;
247 }
248
249 ts_cache_table = &ts_cache->table;
250
251 /* cbmem is being recovered. */
252 if (is_recovery) {
253 /* x86 resume path expects timestamps to be reset. */
254 if (IS_ENABLED(CONFIG_ARCH_ROMSTAGE_X86_32) && ENV_ROMSTAGE)
255 ts_cbmem_table = timestamp_alloc_cbmem_table();
256 else {
257 /* Find existing table in cbmem. */
258 ts_cbmem_table = cbmem_find(CBMEM_ID_TIMESTAMP);
259 /* No existing timestamp table. */
260 if (ts_cbmem_table == NULL)
261 ts_cbmem_table = timestamp_alloc_cbmem_table();
262 }
263 } else
264 /* First time sync. Add new table. */
265 ts_cbmem_table = timestamp_alloc_cbmem_table();
266
267 if (ts_cbmem_table == NULL) {
268 printk(BIOS_ERR, "ERROR: No timestamp table allocated\n");
269 return;
270 }
271
272 /*
273 * There's no need to worry about the base_time fields being out of
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300274 * sync because only the following configuration is used/supported:
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500275 *
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300276 * Timestamps get initialized before ramstage, which implies
277 * CBMEM initialization in romstage.
Julius Werner8c093772016-02-09 16:09:15 -0800278 * This requires the board to define a TIMESTAMP() region in its
279 * memlayout.ld (default on x86). The base_time from timestamp_init()
280 * (usually called from bootblock.c on most non-x86 boards) persists
281 * in that region until it gets synced to CBMEM in romstage.
282 * In ramstage, the BSS cache's base_time will be 0 until the second
283 * sync, which will adjust the timestamps in there to the correct
284 * base_time (from CBMEM) with the timestamp_add_table_entry() below.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500285 *
Julius Werner8c093772016-02-09 16:09:15 -0800286 * If you try to initialize timestamps before ramstage but don't define
287 * a TIMESTAMP region, all operations will fail (safely), and coreboot
288 * will behave as if timestamps only get initialized in ramstage.
289 *
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300290 * If timestamps only get initialized in ramstage, the base_time from
291 * timestamp_init() will get ignored and all timestamps will be 0-based.
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500292 */
293 for (i = 0; i < ts_cache_table->num_entries; i++) {
294 struct timestamp_entry *tse = &ts_cache_table->entries[i];
295 timestamp_add_table_entry(ts_cbmem_table, tse->entry_id,
296 tse->entry_stamp);
297 }
298
299 /* Freshly added cbmem table has base_time 0. Inherit cache base_time */
300 if (ts_cbmem_table->base_time == 0)
301 ts_cbmem_table->base_time = ts_cache_table->base_time;
302
Aaron Durbinc49014e2015-08-30 21:19:55 -0500303 /* Seed the timestamp tick frequency in ramstage. */
304 if (ENV_RAMSTAGE)
305 ts_cbmem_table->tick_freq_mhz = timestamp_tick_freq_mhz();
306
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500307 /* Cache no longer required. */
308 ts_cache_table->num_entries = 0;
309 ts_cache->cache_state = TIMESTAMP_CACHE_NOT_NEEDED;
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300310}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300311
Kyösti Mälkki806ea08b2017-07-15 20:42:20 +0300312void timestamp_rescale_table(uint16_t N, uint16_t M)
313{
314 uint32_t i;
315 struct timestamp_table *ts_table;
316
317 if (!timestamp_should_run())
318 return;
319
320 if (N == 0 || M == 0)
321 return;
322
323 ts_table = timestamp_table_get();
324
325 /* No timestamp table found */
326 if (ts_table == NULL) {
327 printk(BIOS_ERR, "ERROR: No timestamp table found\n");
328 return;
329 }
330
331 ts_table->base_time /= M;
332 ts_table->base_time *= N;
333 for (i = 0; i < ts_table->num_entries; i++) {
334 struct timestamp_entry *tse = &ts_table->entries[i];
335 tse->entry_stamp /= M;
336 tse->entry_stamp *= N;
337 }
338}
339
Werner Zeh35cceb82017-09-12 07:42:54 +0200340/*
341 * Get the time in microseconds since boot (or more precise: since timestamp
342 * table was initialized).
343 */
344uint32_t get_us_since_boot(void)
345{
346 struct timestamp_table *ts = timestamp_table_get();
347
348 if (ts == NULL || ts->tick_freq_mhz == 0)
349 return 0;
350 return (timestamp_get() - ts->base_time) / ts->tick_freq_mhz;
351}
352
Aaron Durbin1936f6c2015-07-03 17:04:21 -0500353ROMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem)
354RAMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500355
356/* Provide default timestamp implementation using monotonic timer. */
Aaron Durbin64031672018-04-21 14:45:32 -0600357uint64_t __weak timestamp_get(void)
Aaron Durbin9e80e272015-05-01 16:48:54 -0500358{
359 struct mono_time t1, t2;
360
Aaron Durbin7aafe532015-05-07 11:32:30 -0500361 if (!IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER))
362 return 0;
363
Aaron Durbin9e80e272015-05-01 16:48:54 -0500364 mono_time_set_usecs(&t1, 0);
365 timer_monotonic_get(&t2);
366
367 return mono_time_diff_microseconds(&t1, &t2);
368}
Aaron Durbinc49014e2015-08-30 21:19:55 -0500369
370/* Like timestamp_get() above this matches up with microsecond granularity. */
Aaron Durbin64031672018-04-21 14:45:32 -0600371int __weak timestamp_tick_freq_mhz(void)
Aaron Durbinc49014e2015-08-30 21:19:55 -0500372{
373 return 1;
374}