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. |
| 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 20 | #include <stddef.h> |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <console/console.h> |
| 23 | #include <cbmem.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 | |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 30 | #define MAX_TIMESTAMPS 60 |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 31 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 32 | static struct timestamp_table* ts_table_p CAR_GLOBAL = NULL; |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 33 | static uint64_t ts_basetime CAR_GLOBAL = 0; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 34 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 35 | static void timestamp_stash(enum timestamp_id id, uint64_t ts_time); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 36 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 37 | static void timestamp_real_init(uint64_t base) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 38 | { |
| 39 | struct timestamp_table* tst; |
| 40 | |
| 41 | tst = cbmem_add(CBMEM_ID_TIMESTAMP, |
| 42 | sizeof(struct timestamp_table) + |
| 43 | MAX_TIMESTAMPS * sizeof(struct timestamp_entry)); |
| 44 | |
| 45 | if (!tst) { |
Paul Menzel | 2edf77c | 2013-01-22 11:46:34 +0100 | [diff] [blame] | 46 | printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n"); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 50 | tst->base_time = base; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 51 | tst->max_entries = MAX_TIMESTAMPS; |
| 52 | tst->num_entries = 0; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 53 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 54 | car_set_var(ts_table_p, tst); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 57 | /* Determine if one should proceed into timestamp code. This is for protecting |
| 58 | * systems that have multiple processors running in romstage -- namely AMD |
| 59 | * based x86 platforms. */ |
| 60 | static int timestamp_should_run(void) |
| 61 | { |
| 62 | /* Only check boot_cpu() in other stages than ramstage on x86. */ |
| 63 | if ((!ENV_RAMSTAGE && IS_ENABLED(CONFIG_ARCH_X86)) && !boot_cpu()) |
| 64 | return 0; |
| 65 | |
| 66 | return 1; |
| 67 | } |
| 68 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 69 | void timestamp_add(enum timestamp_id id, uint64_t ts_time) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 70 | { |
| 71 | struct timestamp_entry *tse; |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 72 | struct timestamp_table *ts_table = NULL; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 73 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 74 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 75 | return; |
| 76 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 77 | ts_table = car_get_var(ts_table_p); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 78 | if (!ts_table) { |
| 79 | timestamp_stash(id, ts_time); |
| 80 | return; |
| 81 | } |
| 82 | if (ts_table->num_entries == ts_table->max_entries) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 83 | return; |
| 84 | |
| 85 | tse = &ts_table->entries[ts_table->num_entries++]; |
| 86 | tse->entry_id = id; |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 87 | tse->entry_stamp = ts_time - ts_table->base_time; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void timestamp_add_now(enum timestamp_id id) |
| 91 | { |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 92 | timestamp_add(id, timestamp_get()); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 93 | } |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 94 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 95 | #define MAX_TIMESTAMP_CACHE 8 |
| 96 | struct timestamp_cache { |
| 97 | enum timestamp_id id; |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 98 | uint64_t time; |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 99 | } timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL; |
| 100 | |
| 101 | static int timestamp_entries CAR_GLOBAL = 0; |
| 102 | |
| 103 | /** |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 104 | * timestamp_stash() allows to temporarily cache timestamps. |
| 105 | * This is needed when timestamping before the CBMEM area |
| 106 | * is initialized. The function timestamp_do_sync() is used to |
| 107 | * write the timestamps to the CBMEM area and this is done as |
| 108 | * part of CAR migration for romstage, and in ramstage main(). |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 109 | */ |
| 110 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 111 | static void timestamp_stash(enum timestamp_id id, uint64_t ts_time) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 112 | { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 113 | struct timestamp_cache *ts_cache = car_get_var(timestamp_cache); |
| 114 | int ts_entries = car_get_var(timestamp_entries); |
| 115 | |
| 116 | if (ts_entries >= MAX_TIMESTAMP_CACHE) { |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 117 | printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n"); |
| 118 | return; |
| 119 | } |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 120 | ts_cache[ts_entries].id = id; |
| 121 | ts_cache[ts_entries].time = ts_time; |
| 122 | car_set_var(timestamp_entries, ++ts_entries); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 125 | static void timestamp_do_sync(void) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 126 | { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 127 | struct timestamp_cache *ts_cache = car_get_var(timestamp_cache); |
| 128 | int ts_entries = car_get_var(timestamp_entries); |
| 129 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 130 | int i; |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 131 | for (i = 0; i < ts_entries; i++) |
| 132 | timestamp_add(ts_cache[i].id, ts_cache[i].time); |
| 133 | car_set_var(timestamp_entries, 0); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 136 | void timestamp_init(uint64_t base) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 137 | { |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 138 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 139 | return; |
| 140 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 141 | #ifdef __PRE_RAM__ |
| 142 | /* Copy of basetime, it is too early for CBMEM. */ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 143 | car_set_var(ts_basetime, base); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 144 | #else |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 145 | struct timestamp_table* tst; |
| 146 | |
| 147 | /* Locate and use an already existing table. */ |
| 148 | tst = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 149 | if (tst) { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 150 | car_set_var(ts_table_p, tst); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 151 | return; |
| 152 | } |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 153 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 154 | /* Copy of basetime, may be too early for CBMEM. */ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 155 | car_set_var(ts_basetime, base); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 156 | timestamp_real_init(base); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 157 | #endif |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 158 | } |
| 159 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 160 | void timestamp_reinit(void) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 161 | { |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 162 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 163 | return; |
| 164 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 165 | #ifdef __PRE_RAM__ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 166 | timestamp_real_init(car_get_var(ts_basetime)); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 167 | #else |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 168 | if (!car_get_var(ts_table_p)) |
| 169 | timestamp_init(car_get_var(ts_basetime)); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 170 | #endif |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 171 | if (car_get_var(ts_table_p)) |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 172 | timestamp_do_sync(); |
| 173 | } |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 174 | |
| 175 | /* Call timestamp_reinit at CAR migration time. */ |
| 176 | CAR_MIGRATE(timestamp_reinit) |
Aaron Durbin | 9e80e27 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 177 | |
| 178 | /* Provide default timestamp implementation using monotonic timer. */ |
| 179 | uint64_t __attribute__((weak)) timestamp_get(void) |
| 180 | { |
| 181 | struct mono_time t1, t2; |
| 182 | |
| 183 | mono_time_set_usecs(&t1, 0); |
| 184 | timer_monotonic_get(&t2); |
| 185 | |
| 186 | return mono_time_diff_microseconds(&t1, &t2); |
| 187 | } |