Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved. |
Mario Scheithauer | a39aede | 2017-11-06 16:47:27 +0100 | [diff] [blame] | 5 | * Copyright (C) 2017 Siemens AG |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 6 | * |
| 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 Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 17 | #include <assert.h> |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 18 | #include <stddef.h> |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 20 | #include <compiler.h> |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 21 | #include <console/console.h> |
| 22 | #include <cbmem.h> |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 23 | #include <symbols.h> |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 24 | #include <timer.h> |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 25 | #include <timestamp.h> |
Stefan Reinauer | fd4f413 | 2013-06-19 12:25:44 -0700 | [diff] [blame] | 26 | #include <arch/early_variables.h> |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 27 | #include <rules.h> |
Kyösti Mälkki | 5a5c886 | 2014-01-26 14:41:54 +0200 | [diff] [blame] | 28 | #include <smp/node.h> |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 29 | |
Raul E Rangel | b3e0220 | 2018-05-11 11:08:08 -0600 | [diff] [blame^] | 30 | #define MAX_TIMESTAMPS 192 |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 31 | |
Julius Werner | 85b1aad | 2016-08-19 15:17:42 -0700 | [diff] [blame] | 32 | /* When changing this number, adjust TIMESTAMP() size ASSERT() in memlayout.h */ |
Julius Werner | 69d20c4 | 2016-02-08 19:48:11 -0800 | [diff] [blame] | 33 | #define MAX_BSS_TIMESTAMP_CACHE 16 |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 34 | |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 35 | struct __packed timestamp_cache { |
Aaron Durbin | bd1499d | 2015-07-10 01:51:14 -0500 | [diff] [blame] | 36 | uint32_t cache_state; |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 37 | struct timestamp_table table; |
| 38 | /* The struct timestamp_table has a 0 length array as its last field. |
| 39 | * The following 'entries' array serves as the storage space for the |
Julius Werner | 69d20c4 | 2016-02-08 19:48:11 -0800 | [diff] [blame] | 40 | * cache when allocated in the BSS. */ |
| 41 | struct timestamp_entry entries[MAX_BSS_TIMESTAMP_CACHE]; |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 42 | }; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 43 | |
Julius Werner | 8c09377 | 2016-02-09 16:09:15 -0800 | [diff] [blame] | 44 | DECLARE_OPTIONAL_REGION(timestamp); |
| 45 | |
| 46 | #if defined(__PRE_RAM__) |
| 47 | #define USE_TIMESTAMP_REGION (_timestamp_size > 0) |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 48 | #else |
| 49 | #define USE_TIMESTAMP_REGION 0 |
| 50 | #endif |
| 51 | |
| 52 | /* The cache location will sit in BSS when in ramstage. */ |
| 53 | #define TIMESTAMP_CACHE_IN_BSS ENV_RAMSTAGE |
| 54 | |
Furquan Shaikh | e85de02 | 2016-08-02 11:19:53 -0700 | [diff] [blame] | 55 | #define HAS_CBMEM (ENV_ROMSTAGE || ENV_RAMSTAGE || ENV_POSTCAR) |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 56 | |
| 57 | /* Storage of cache entries during ramstage prior to cbmem coming online. */ |
| 58 | static struct timestamp_cache timestamp_cache; |
| 59 | |
| 60 | enum { |
| 61 | TIMESTAMP_CACHE_UNINITIALIZED = 0, |
| 62 | TIMESTAMP_CACHE_INITIALIZED, |
| 63 | TIMESTAMP_CACHE_NOT_NEEDED, |
| 64 | }; |
| 65 | |
| 66 | static void timestamp_cache_init(struct timestamp_cache *ts_cache, |
| 67 | uint64_t base) |
| 68 | { |
| 69 | ts_cache->table.num_entries = 0; |
Julius Werner | 69d20c4 | 2016-02-08 19:48:11 -0800 | [diff] [blame] | 70 | ts_cache->table.max_entries = MAX_BSS_TIMESTAMP_CACHE; |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 71 | ts_cache->table.base_time = base; |
| 72 | ts_cache->cache_state = TIMESTAMP_CACHE_INITIALIZED; |
Julius Werner | 69d20c4 | 2016-02-08 19:48:11 -0800 | [diff] [blame] | 73 | |
| 74 | if (USE_TIMESTAMP_REGION) |
| 75 | ts_cache->table.max_entries = (_timestamp_size - |
| 76 | offsetof(struct timestamp_cache, entries)) |
| 77 | / sizeof(struct timestamp_entry); |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static struct timestamp_cache *timestamp_cache_get(void) |
| 81 | { |
| 82 | struct timestamp_cache *ts_cache = NULL; |
| 83 | |
| 84 | if (TIMESTAMP_CACHE_IN_BSS) { |
| 85 | ts_cache = ×tamp_cache; |
| 86 | } else if (USE_TIMESTAMP_REGION) { |
| 87 | if (_timestamp_size < sizeof(*ts_cache)) |
| 88 | BUG(); |
| 89 | ts_cache = car_get_var_ptr((void *)_timestamp); |
| 90 | } |
| 91 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 92 | return ts_cache; |
| 93 | } |
| 94 | |
| 95 | static struct timestamp_table *timestamp_alloc_cbmem_table(void) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 96 | { |
Lee Leahy | b2d834a | 2017-03-08 16:52:22 -0800 | [diff] [blame] | 97 | struct timestamp_table *tst; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 98 | |
| 99 | tst = cbmem_add(CBMEM_ID_TIMESTAMP, |
| 100 | sizeof(struct timestamp_table) + |
| 101 | MAX_TIMESTAMPS * sizeof(struct timestamp_entry)); |
| 102 | |
Kyösti Mälkki | e034417 | 2015-05-26 06:23:02 +0300 | [diff] [blame] | 103 | if (!tst) |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 104 | return NULL; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 105 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 106 | tst->base_time = 0; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 107 | tst->max_entries = MAX_TIMESTAMPS; |
| 108 | tst->num_entries = 0; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 109 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 110 | return tst; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 113 | /* Determine if one should proceed into timestamp code. This is for protecting |
| 114 | * systems that have multiple processors running in romstage -- namely AMD |
| 115 | * based x86 platforms. */ |
| 116 | static int timestamp_should_run(void) |
| 117 | { |
| 118 | /* Only check boot_cpu() in other stages than ramstage on x86. */ |
| 119 | if ((!ENV_RAMSTAGE && IS_ENABLED(CONFIG_ARCH_X86)) && !boot_cpu()) |
| 120 | return 0; |
| 121 | |
| 122 | return 1; |
| 123 | } |
| 124 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 125 | static struct timestamp_table *timestamp_table_get(void) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 126 | { |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 127 | MAYBE_STATIC struct timestamp_table *ts_table = NULL; |
| 128 | struct timestamp_cache *ts_cache; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 129 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 130 | if (!timestamp_should_run()) |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 131 | return NULL; |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 132 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 133 | if (ts_table != NULL) |
| 134 | return ts_table; |
| 135 | |
| 136 | ts_cache = timestamp_cache_get(); |
| 137 | |
| 138 | if (ts_cache == NULL) { |
| 139 | if (HAS_CBMEM) |
| 140 | ts_table = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 141 | return ts_table; |
| 142 | } |
| 143 | |
| 144 | /* Cache is required. */ |
| 145 | if (ts_cache->cache_state != TIMESTAMP_CACHE_NOT_NEEDED) |
| 146 | return &ts_cache->table; |
| 147 | |
| 148 | /* Cache shouldn't be used but there's no backing store. */ |
| 149 | if (!HAS_CBMEM) |
| 150 | return NULL; |
| 151 | |
| 152 | ts_table = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 153 | |
| 154 | return ts_table; |
| 155 | } |
| 156 | |
Martin Roth | b22bbe2 | 2018-03-07 15:32:16 -0700 | [diff] [blame] | 157 | static const char *timestamp_name(enum timestamp_id id) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) { |
| 162 | if (timestamp_ids[i].id == id) |
| 163 | return timestamp_ids[i].name; |
| 164 | } |
| 165 | |
| 166 | return "Unknown timestamp ID"; |
| 167 | } |
| 168 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 169 | static void timestamp_add_table_entry(struct timestamp_table *ts_table, |
| 170 | enum timestamp_id id, uint64_t ts_time) |
| 171 | { |
| 172 | struct timestamp_entry *tse; |
| 173 | |
Ben Gardner | f5fd4c9 | 2015-11-20 13:25:25 -0600 | [diff] [blame] | 174 | if (ts_table->num_entries >= ts_table->max_entries) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 175 | return; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 176 | |
| 177 | tse = &ts_table->entries[ts_table->num_entries++]; |
| 178 | tse->entry_id = id; |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 179 | tse->entry_stamp = ts_time - ts_table->base_time; |
Ben Gardner | f5fd4c9 | 2015-11-20 13:25:25 -0600 | [diff] [blame] | 180 | |
Martin Roth | b22bbe2 | 2018-03-07 15:32:16 -0700 | [diff] [blame] | 181 | if (IS_ENABLED(CONFIG_TIMESTAMPS_ON_CONSOLE)) |
| 182 | printk(BIOS_SPEW, "Timestamp - %s: %" PRIu64 "\n", |
| 183 | timestamp_name(id), ts_time); |
| 184 | |
Ben Gardner | f5fd4c9 | 2015-11-20 13:25:25 -0600 | [diff] [blame] | 185 | if (ts_table->num_entries == ts_table->max_entries) |
| 186 | printk(BIOS_ERR, "ERROR: Timestamp table full\n"); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 189 | void timestamp_add(enum timestamp_id id, uint64_t ts_time) |
| 190 | { |
| 191 | struct timestamp_table *ts_table; |
| 192 | |
| 193 | ts_table = timestamp_table_get(); |
| 194 | |
| 195 | if (!ts_table) { |
| 196 | printk(BIOS_ERR, "ERROR: No timestamp table found\n"); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | timestamp_add_table_entry(ts_table, id, ts_time); |
| 201 | } |
| 202 | |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 203 | void timestamp_add_now(enum timestamp_id id) |
| 204 | { |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 205 | timestamp_add(id, timestamp_get()); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 206 | } |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 207 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 208 | void timestamp_init(uint64_t base) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 209 | { |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 210 | struct timestamp_cache *ts_cache; |
| 211 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 212 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 213 | return; |
| 214 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 215 | ts_cache = timestamp_cache_get(); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 216 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 217 | if (!ts_cache) { |
| 218 | printk(BIOS_ERR, "ERROR: No timestamp cache to init\n"); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 219 | return; |
| 220 | } |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 221 | |
Aaron Durbin | bd1499d | 2015-07-10 01:51:14 -0500 | [diff] [blame] | 222 | /* In the EARLY_CBMEM_INIT case timestamps could have already been |
| 223 | * recovered. In those circumstances honor the cache which sits in BSS |
| 224 | * as it has already been initialized. */ |
Aaron Durbin | 2a983bd | 2015-07-11 13:36:01 -0500 | [diff] [blame] | 225 | if (ENV_RAMSTAGE && IS_ENABLED(CONFIG_EARLY_CBMEM_INIT) && |
Aaron Durbin | bd1499d | 2015-07-10 01:51:14 -0500 | [diff] [blame] | 226 | ts_cache->cache_state != TIMESTAMP_CACHE_UNINITIALIZED) |
| 227 | return; |
| 228 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 229 | timestamp_cache_init(ts_cache, base); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 230 | } |
| 231 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 232 | static void timestamp_sync_cache_to_cbmem(int is_recovery) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 233 | { |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 234 | uint32_t i; |
| 235 | struct timestamp_cache *ts_cache; |
| 236 | struct timestamp_table *ts_cache_table; |
| 237 | struct timestamp_table *ts_cbmem_table = NULL; |
| 238 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 239 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 240 | return; |
| 241 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 242 | ts_cache = timestamp_cache_get(); |
| 243 | |
| 244 | /* No timestamp cache found */ |
| 245 | if (ts_cache == NULL) { |
| 246 | printk(BIOS_ERR, "ERROR: No timestamp cache found\n"); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | ts_cache_table = &ts_cache->table; |
| 251 | |
| 252 | /* cbmem is being recovered. */ |
| 253 | if (is_recovery) { |
| 254 | /* x86 resume path expects timestamps to be reset. */ |
| 255 | if (IS_ENABLED(CONFIG_ARCH_ROMSTAGE_X86_32) && ENV_ROMSTAGE) |
| 256 | ts_cbmem_table = timestamp_alloc_cbmem_table(); |
| 257 | else { |
| 258 | /* Find existing table in cbmem. */ |
| 259 | ts_cbmem_table = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 260 | /* No existing timestamp table. */ |
| 261 | if (ts_cbmem_table == NULL) |
| 262 | ts_cbmem_table = timestamp_alloc_cbmem_table(); |
| 263 | } |
| 264 | } else |
| 265 | /* First time sync. Add new table. */ |
| 266 | ts_cbmem_table = timestamp_alloc_cbmem_table(); |
| 267 | |
| 268 | if (ts_cbmem_table == NULL) { |
| 269 | printk(BIOS_ERR, "ERROR: No timestamp table allocated\n"); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * There's no need to worry about the base_time fields being out of |
Julius Werner | 8c09377 | 2016-02-09 16:09:15 -0800 | [diff] [blame] | 275 | * sync because only the following configurations are used/supported: |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 276 | * |
Julius Werner | 8c09377 | 2016-02-09 16:09:15 -0800 | [diff] [blame] | 277 | * 1. Timestamps get initialized before ramstage, which implies |
| 278 | * CONFIG_EARLY_CBMEM_INIT and CBMEM initialization in romstage. |
| 279 | * This requires the board to define a TIMESTAMP() region in its |
| 280 | * memlayout.ld (default on x86). The base_time from timestamp_init() |
| 281 | * (usually called from bootblock.c on most non-x86 boards) persists |
| 282 | * in that region until it gets synced to CBMEM in romstage. |
| 283 | * In ramstage, the BSS cache's base_time will be 0 until the second |
| 284 | * sync, which will adjust the timestamps in there to the correct |
| 285 | * base_time (from CBMEM) with the timestamp_add_table_entry() below. |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 286 | * |
Julius Werner | 8c09377 | 2016-02-09 16:09:15 -0800 | [diff] [blame] | 287 | * 2. Timestamps only get initialized in ramstage *and* |
| 288 | * CONFIG_LATE_CBMEM_INIT is set. main() will call timestamp_init() |
| 289 | * very early (before any timestamps get logged) to set a base_time |
| 290 | * in the BSS cache, which will later get synced over to CBMEM. |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 291 | * |
Julius Werner | 8c09377 | 2016-02-09 16:09:15 -0800 | [diff] [blame] | 292 | * If you try to initialize timestamps before ramstage but don't define |
| 293 | * a TIMESTAMP region, all operations will fail (safely), and coreboot |
| 294 | * will behave as if timestamps only get initialized in ramstage. |
| 295 | * |
| 296 | * If CONFIG_EARLY_CBMEM_INIT is set but timestamps only get |
| 297 | * initialized in ramstage, the base_time from timestamp_init() will |
| 298 | * get ignored and all timestamps will be 0-based. |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 299 | */ |
| 300 | for (i = 0; i < ts_cache_table->num_entries; i++) { |
| 301 | struct timestamp_entry *tse = &ts_cache_table->entries[i]; |
| 302 | timestamp_add_table_entry(ts_cbmem_table, tse->entry_id, |
| 303 | tse->entry_stamp); |
| 304 | } |
| 305 | |
| 306 | /* Freshly added cbmem table has base_time 0. Inherit cache base_time */ |
| 307 | if (ts_cbmem_table->base_time == 0) |
| 308 | ts_cbmem_table->base_time = ts_cache_table->base_time; |
| 309 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 310 | /* Seed the timestamp tick frequency in ramstage. */ |
| 311 | if (ENV_RAMSTAGE) |
| 312 | ts_cbmem_table->tick_freq_mhz = timestamp_tick_freq_mhz(); |
| 313 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 314 | /* Cache no longer required. */ |
| 315 | ts_cache_table->num_entries = 0; |
| 316 | ts_cache->cache_state = TIMESTAMP_CACHE_NOT_NEEDED; |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 317 | } |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 318 | |
Kyösti Mälkki | 806ea08b | 2017-07-15 20:42:20 +0300 | [diff] [blame] | 319 | void timestamp_rescale_table(uint16_t N, uint16_t M) |
| 320 | { |
| 321 | uint32_t i; |
| 322 | struct timestamp_table *ts_table; |
| 323 | |
| 324 | if (!timestamp_should_run()) |
| 325 | return; |
| 326 | |
| 327 | if (N == 0 || M == 0) |
| 328 | return; |
| 329 | |
| 330 | ts_table = timestamp_table_get(); |
| 331 | |
| 332 | /* No timestamp table found */ |
| 333 | if (ts_table == NULL) { |
| 334 | printk(BIOS_ERR, "ERROR: No timestamp table found\n"); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | ts_table->base_time /= M; |
| 339 | ts_table->base_time *= N; |
| 340 | for (i = 0; i < ts_table->num_entries; i++) { |
| 341 | struct timestamp_entry *tse = &ts_table->entries[i]; |
| 342 | tse->entry_stamp /= M; |
| 343 | tse->entry_stamp *= N; |
| 344 | } |
| 345 | } |
| 346 | |
Werner Zeh | 35cceb8 | 2017-09-12 07:42:54 +0200 | [diff] [blame] | 347 | /* |
| 348 | * Get the time in microseconds since boot (or more precise: since timestamp |
| 349 | * table was initialized). |
| 350 | */ |
| 351 | uint32_t get_us_since_boot(void) |
| 352 | { |
| 353 | struct timestamp_table *ts = timestamp_table_get(); |
| 354 | |
| 355 | if (ts == NULL || ts->tick_freq_mhz == 0) |
| 356 | return 0; |
| 357 | return (timestamp_get() - ts->base_time) / ts->tick_freq_mhz; |
| 358 | } |
| 359 | |
Aaron Durbin | 1936f6c | 2015-07-03 17:04:21 -0500 | [diff] [blame] | 360 | ROMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem) |
| 361 | RAMSTAGE_CBMEM_INIT_HOOK(timestamp_sync_cache_to_cbmem) |
Aaron Durbin | 9e80e27 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 362 | |
| 363 | /* Provide default timestamp implementation using monotonic timer. */ |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 364 | uint64_t __weak timestamp_get(void) |
Aaron Durbin | 9e80e27 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 365 | { |
| 366 | struct mono_time t1, t2; |
| 367 | |
Aaron Durbin | 7aafe53 | 2015-05-07 11:32:30 -0500 | [diff] [blame] | 368 | if (!IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)) |
| 369 | return 0; |
| 370 | |
Aaron Durbin | 9e80e27 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 371 | mono_time_set_usecs(&t1, 0); |
| 372 | timer_monotonic_get(&t2); |
| 373 | |
| 374 | return mono_time_diff_microseconds(&t1, &t2); |
| 375 | } |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 376 | |
| 377 | /* Like timestamp_get() above this matches up with microsecond granularity. */ |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 378 | int __weak timestamp_tick_freq_mhz(void) |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 379 | { |
| 380 | return 1; |
| 381 | } |