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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 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 | |
Kyösti Mälkki | e034417 | 2015-05-26 06:23:02 +0300 | [diff] [blame] | 45 | if (!tst) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 46 | return; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 47 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 48 | tst->base_time = base; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 49 | tst->max_entries = MAX_TIMESTAMPS; |
| 50 | tst->num_entries = 0; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 51 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 52 | car_set_var(ts_table_p, tst); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 55 | /* Determine if one should proceed into timestamp code. This is for protecting |
| 56 | * systems that have multiple processors running in romstage -- namely AMD |
| 57 | * based x86 platforms. */ |
| 58 | static int timestamp_should_run(void) |
| 59 | { |
| 60 | /* Only check boot_cpu() in other stages than ramstage on x86. */ |
| 61 | if ((!ENV_RAMSTAGE && IS_ENABLED(CONFIG_ARCH_X86)) && !boot_cpu()) |
| 62 | return 0; |
| 63 | |
| 64 | return 1; |
| 65 | } |
| 66 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 67 | void timestamp_add(enum timestamp_id id, uint64_t ts_time) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 68 | { |
| 69 | struct timestamp_entry *tse; |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 70 | struct timestamp_table *ts_table = NULL; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 71 | |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 72 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 73 | return; |
| 74 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 75 | ts_table = car_get_var(ts_table_p); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 76 | if (!ts_table) { |
| 77 | timestamp_stash(id, ts_time); |
| 78 | return; |
| 79 | } |
| 80 | if (ts_table->num_entries == ts_table->max_entries) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 81 | return; |
| 82 | |
| 83 | tse = &ts_table->entries[ts_table->num_entries++]; |
| 84 | tse->entry_id = id; |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 85 | tse->entry_stamp = ts_time - ts_table->base_time; |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void timestamp_add_now(enum timestamp_id id) |
| 89 | { |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 90 | timestamp_add(id, timestamp_get()); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 91 | } |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 92 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 93 | #define MAX_TIMESTAMP_CACHE 8 |
| 94 | struct timestamp_cache { |
| 95 | enum timestamp_id id; |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 96 | uint64_t time; |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 97 | } timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL; |
| 98 | |
| 99 | static int timestamp_entries CAR_GLOBAL = 0; |
| 100 | |
| 101 | /** |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 102 | * timestamp_stash() allows to temporarily cache timestamps. |
| 103 | * This is needed when timestamping before the CBMEM area |
| 104 | * is initialized. The function timestamp_do_sync() is used to |
| 105 | * write the timestamps to the CBMEM area and this is done as |
| 106 | * part of CAR migration for romstage, and in ramstage main(). |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 107 | */ |
| 108 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 109 | static void timestamp_stash(enum timestamp_id id, uint64_t ts_time) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 110 | { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 111 | struct timestamp_cache *ts_cache = car_get_var(timestamp_cache); |
| 112 | int ts_entries = car_get_var(timestamp_entries); |
| 113 | |
| 114 | if (ts_entries >= MAX_TIMESTAMP_CACHE) { |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 115 | printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n"); |
| 116 | return; |
| 117 | } |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 118 | ts_cache[ts_entries].id = id; |
| 119 | ts_cache[ts_entries].time = ts_time; |
| 120 | car_set_var(timestamp_entries, ++ts_entries); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 123 | static void timestamp_do_sync(void) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 124 | { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 125 | struct timestamp_cache *ts_cache = car_get_var(timestamp_cache); |
| 126 | int ts_entries = car_get_var(timestamp_entries); |
| 127 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 128 | int i; |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 129 | for (i = 0; i < ts_entries; i++) |
| 130 | timestamp_add(ts_cache[i].id, ts_cache[i].time); |
| 131 | car_set_var(timestamp_entries, 0); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 134 | void timestamp_init(uint64_t base) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 135 | { |
Aaron Durbin | 2daadf8 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 136 | if (!timestamp_should_run()) |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 137 | return; |
| 138 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 139 | #ifdef __PRE_RAM__ |
| 140 | /* Copy of basetime, it is too early for CBMEM. */ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 141 | car_set_var(ts_basetime, base); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 142 | #else |
Kyösti Mälkki | e034417 | 2015-05-26 06:23:02 +0300 | [diff] [blame] | 143 | struct timestamp_table *tst = NULL; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 144 | |
| 145 | /* Locate and use an already existing table. */ |
Kyösti Mälkki | e034417 | 2015-05-26 06:23:02 +0300 | [diff] [blame] | 146 | if (!IS_ENABLED(CONFIG_LATE_CBMEM_INIT)) |
| 147 | tst = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 148 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 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 | |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame^] | 160 | static void timestamp_reinit(int is_recovery) |
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 | |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 175 | /* Call timestamp_reinit CBMEM init hooks. */ |
| 176 | ROMSTAGE_CBMEM_INIT_HOOK(timestamp_reinit) |
| 177 | RAMSTAGE_CBMEM_INIT_HOOK(timestamp_reinit) |
Aaron Durbin | 9e80e27 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 178 | |
| 179 | /* Provide default timestamp implementation using monotonic timer. */ |
| 180 | uint64_t __attribute__((weak)) timestamp_get(void) |
| 181 | { |
| 182 | struct mono_time t1, t2; |
| 183 | |
Aaron Durbin | 7aafe53 | 2015-05-07 11:32:30 -0500 | [diff] [blame] | 184 | if (!IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)) |
| 185 | return 0; |
| 186 | |
Aaron Durbin | 9e80e27 | 2015-05-01 16:48:54 -0500 | [diff] [blame] | 187 | mono_time_set_usecs(&t1, 0); |
| 188 | timer_monotonic_get(&t2); |
| 189 | |
| 190 | return mono_time_diff_microseconds(&t1, &t2); |
| 191 | } |