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> |
| 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 | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 26 | #include <cpu/x86/lapic.h> |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 27 | |
| 28 | #define MAX_TIMESTAMPS 30 |
| 29 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 30 | static struct timestamp_table* ts_table_p CAR_GLOBAL = NULL; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 31 | static tsc_t ts_basetime CAR_GLOBAL = { .lo = 0, .hi =0 }; |
| 32 | |
| 33 | static void timestamp_stash(enum timestamp_id id, tsc_t ts_time); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 34 | |
| 35 | static uint64_t tsc_to_uint64(tsc_t tstamp) |
| 36 | { |
| 37 | return (((uint64_t)tstamp.hi) << 32) + tstamp.lo; |
| 38 | } |
| 39 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 40 | static void timestamp_real_init(tsc_t base) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 41 | { |
| 42 | struct timestamp_table* tst; |
| 43 | |
| 44 | tst = cbmem_add(CBMEM_ID_TIMESTAMP, |
| 45 | sizeof(struct timestamp_table) + |
| 46 | MAX_TIMESTAMPS * sizeof(struct timestamp_entry)); |
| 47 | |
| 48 | if (!tst) { |
Paul Menzel | 2edf77c | 2013-01-22 11:46:34 +0100 | [diff] [blame] | 49 | printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n"); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | tst->base_time = tsc_to_uint64(base); |
| 54 | tst->max_entries = MAX_TIMESTAMPS; |
| 55 | tst->num_entries = 0; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 56 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 57 | car_set_var(ts_table_p, tst); |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void timestamp_add(enum timestamp_id id, tsc_t ts_time) |
| 61 | { |
| 62 | struct timestamp_entry *tse; |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 63 | struct timestamp_table *ts_table = NULL; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 64 | |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 65 | if (!boot_cpu()) |
| 66 | return; |
| 67 | |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 68 | ts_table = car_get_var(ts_table_p); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 69 | if (!ts_table) { |
| 70 | timestamp_stash(id, ts_time); |
| 71 | return; |
| 72 | } |
| 73 | if (ts_table->num_entries == ts_table->max_entries) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 74 | return; |
| 75 | |
| 76 | tse = &ts_table->entries[ts_table->num_entries++]; |
| 77 | tse->entry_id = id; |
| 78 | tse->entry_stamp = tsc_to_uint64(ts_time) - ts_table->base_time; |
| 79 | } |
| 80 | |
| 81 | void timestamp_add_now(enum timestamp_id id) |
| 82 | { |
| 83 | timestamp_add(id, rdtsc()); |
| 84 | } |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 85 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 86 | #define MAX_TIMESTAMP_CACHE 8 |
| 87 | struct timestamp_cache { |
| 88 | enum timestamp_id id; |
| 89 | tsc_t time; |
| 90 | } timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL; |
| 91 | |
| 92 | static int timestamp_entries CAR_GLOBAL = 0; |
| 93 | |
| 94 | /** |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 95 | * timestamp_stash() allows to temporarily cache timestamps. |
| 96 | * This is needed when timestamping before the CBMEM area |
| 97 | * is initialized. The function timestamp_do_sync() is used to |
| 98 | * write the timestamps to the CBMEM area and this is done as |
| 99 | * part of CAR migration for romstage, and in ramstage main(). |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 102 | static void timestamp_stash(enum timestamp_id id, tsc_t ts_time) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 103 | { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 104 | struct timestamp_cache *ts_cache = car_get_var(timestamp_cache); |
| 105 | int ts_entries = car_get_var(timestamp_entries); |
| 106 | |
| 107 | if (ts_entries >= MAX_TIMESTAMP_CACHE) { |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 108 | printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n"); |
| 109 | return; |
| 110 | } |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 111 | ts_cache[ts_entries].id = id; |
| 112 | ts_cache[ts_entries].time = ts_time; |
| 113 | car_set_var(timestamp_entries, ++ts_entries); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 116 | static void timestamp_do_sync(void) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 117 | { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 118 | struct timestamp_cache *ts_cache = car_get_var(timestamp_cache); |
| 119 | int ts_entries = car_get_var(timestamp_entries); |
| 120 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 121 | int i; |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 122 | for (i = 0; i < ts_entries; i++) |
| 123 | timestamp_add(ts_cache[i].id, ts_cache[i].time); |
| 124 | car_set_var(timestamp_entries, 0); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 127 | void timestamp_init(tsc_t base) |
| 128 | { |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 129 | if (!boot_cpu()) |
| 130 | return; |
| 131 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 132 | #ifdef __PRE_RAM__ |
| 133 | /* Copy of basetime, it is too early for CBMEM. */ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 134 | car_set_var(ts_basetime, base); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 135 | #else |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 136 | struct timestamp_table* tst; |
| 137 | |
| 138 | /* Locate and use an already existing table. */ |
| 139 | tst = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 140 | if (tst) { |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 141 | car_set_var(ts_table_p, tst); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 142 | return; |
| 143 | } |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 144 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 145 | /* Copy of basetime, may be too early for CBMEM. */ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 146 | car_set_var(ts_basetime, base); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 147 | timestamp_real_init(base); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 148 | #endif |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 149 | } |
| 150 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 151 | void timestamp_reinit(void) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 152 | { |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 153 | if (!boot_cpu()) |
| 154 | return; |
| 155 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 156 | #ifdef __PRE_RAM__ |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 157 | timestamp_real_init(car_get_var(ts_basetime)); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 158 | #else |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 159 | if (!car_get_var(ts_table_p)) |
| 160 | timestamp_init(car_get_var(ts_basetime)); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 161 | #endif |
Kyösti Mälkki | 29e9c22 | 2013-10-13 13:27:56 +0300 | [diff] [blame] | 162 | if (car_get_var(ts_table_p)) |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 163 | timestamp_do_sync(); |
| 164 | } |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 165 | |
| 166 | /* Call timestamp_reinit at CAR migration time. */ |
| 167 | CAR_MIGRATE(timestamp_reinit) |