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