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