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 | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 30 | static struct timestamp_table* ts_table CAR_GLOBAL = NULL; |
| 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 | |
| 57 | ts_table = 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 | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 63 | |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 64 | if (!boot_cpu()) |
| 65 | return; |
| 66 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 67 | if (!ts_table) { |
| 68 | timestamp_stash(id, ts_time); |
| 69 | return; |
| 70 | } |
| 71 | if (ts_table->num_entries == ts_table->max_entries) |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 72 | return; |
| 73 | |
| 74 | tse = &ts_table->entries[ts_table->num_entries++]; |
| 75 | tse->entry_id = id; |
| 76 | tse->entry_stamp = tsc_to_uint64(ts_time) - ts_table->base_time; |
| 77 | } |
| 78 | |
| 79 | void timestamp_add_now(enum timestamp_id id) |
| 80 | { |
| 81 | timestamp_add(id, rdtsc()); |
| 82 | } |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 83 | |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 84 | #define MAX_TIMESTAMP_CACHE 8 |
| 85 | struct timestamp_cache { |
| 86 | enum timestamp_id id; |
| 87 | tsc_t time; |
| 88 | } timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL; |
| 89 | |
| 90 | static int timestamp_entries CAR_GLOBAL = 0; |
| 91 | |
| 92 | /** |
| 93 | * timestamp_stash() allows to temporarily cache time stamps. |
| 94 | * This is needed when time stamping before the CBMEM area |
| 95 | * is initialized. The function timestamp_sync() is used to |
| 96 | * write the time stamps to the CBMEM area. This is done in |
Stefan Reinauer | 6adef08 | 2013-05-09 16:30:06 -0700 | [diff] [blame] | 97 | * ram stage main() |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 98 | */ |
| 99 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 100 | static void timestamp_stash(enum timestamp_id id, tsc_t ts_time) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 101 | { |
| 102 | if (timestamp_entries >= MAX_TIMESTAMP_CACHE) { |
| 103 | printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n"); |
| 104 | return; |
| 105 | } |
| 106 | timestamp_cache[timestamp_entries].id = id; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 107 | timestamp_cache[timestamp_entries].time = ts_time; |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 108 | timestamp_entries++; |
| 109 | } |
| 110 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 111 | static void timestamp_do_sync(void) |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 112 | { |
| 113 | int i; |
| 114 | for (i = 0; i < timestamp_entries; i++) |
| 115 | timestamp_add(timestamp_cache[i].id, timestamp_cache[i].time); |
| 116 | timestamp_entries = 0; |
| 117 | } |
| 118 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 119 | void timestamp_init(tsc_t base) |
| 120 | { |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 121 | if (!boot_cpu()) |
| 122 | return; |
| 123 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 124 | #ifdef __PRE_RAM__ |
| 125 | /* Copy of basetime, it is too early for CBMEM. */ |
| 126 | ts_basetime = base; |
| 127 | #else |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 128 | struct timestamp_table* tst; |
| 129 | |
| 130 | /* Locate and use an already existing table. */ |
| 131 | tst = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 132 | if (tst) { |
| 133 | ts_table = tst; |
| 134 | return; |
| 135 | } |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 136 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 137 | /* Copy of basetime, may be too early for CBMEM. */ |
| 138 | ts_basetime = base; |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 139 | timestamp_real_init(base); |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 140 | #endif |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 141 | } |
| 142 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 143 | void timestamp_reinit(void) |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 144 | { |
Kyösti Mälkki | f56ff90 | 2013-09-08 13:10:28 +0300 | [diff] [blame] | 145 | if (!boot_cpu()) |
| 146 | return; |
| 147 | |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 148 | #ifdef __PRE_RAM__ |
| 149 | timestamp_real_init(ts_basetime); |
| 150 | #else |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 151 | if (!ts_table) |
| 152 | timestamp_init(ts_basetime); |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 153 | #endif |
Kyösti Mälkki | 3d45c40 | 2013-09-07 20:26:36 +0300 | [diff] [blame] | 154 | if (ts_table) |
| 155 | timestamp_do_sync(); |
| 156 | } |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 157 | |
| 158 | /* Call timestamp_reinit at CAR migration time. */ |
| 159 | CAR_MIGRATE(timestamp_reinit) |