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 | |
| 20 | #include <stdint.h> |
| 21 | #include <console/console.h> |
| 22 | #include <cbmem.h> |
| 23 | #include <timestamp.h> |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 24 | #ifndef __PRE_RAM__ |
| 25 | /* For CAR_GLOBAL... This should move out of x86 specific code */ |
| 26 | #include <cpu/x86/car.h> |
| 27 | #endif |
Vadim Bendebury | 6f72d69 | 2011-09-21 16:12:39 -0700 | [diff] [blame] | 28 | |
| 29 | #define MAX_TIMESTAMPS 30 |
| 30 | |
| 31 | #ifndef __PRE_RAM__ |
| 32 | static struct timestamp_table* ts_table; |
| 33 | #endif |
| 34 | |
| 35 | static uint64_t tsc_to_uint64(tsc_t tstamp) |
| 36 | { |
| 37 | return (((uint64_t)tstamp.hi) << 32) + tstamp.lo; |
| 38 | } |
| 39 | |
| 40 | void timestamp_init(tsc_t base) |
| 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; |
| 56 | } |
| 57 | |
| 58 | void timestamp_add(enum timestamp_id id, tsc_t ts_time) |
| 59 | { |
| 60 | struct timestamp_entry *tse; |
| 61 | #ifdef __PRE_RAM__ |
| 62 | struct timestamp_table *ts_table = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 63 | #else |
| 64 | if (!ts_table) |
| 65 | ts_table = cbmem_find(CBMEM_ID_TIMESTAMP); |
| 66 | #endif |
| 67 | if (!ts_table || (ts_table->num_entries == ts_table->max_entries)) |
| 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 | |
| 80 | #ifndef __PRE_RAM__ |
| 81 | |
| 82 | #define MAX_TIMESTAMP_CACHE 8 |
| 83 | struct timestamp_cache { |
| 84 | enum timestamp_id id; |
| 85 | tsc_t time; |
| 86 | } timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL; |
| 87 | |
| 88 | static int timestamp_entries CAR_GLOBAL = 0; |
| 89 | |
| 90 | /** |
| 91 | * timestamp_stash() allows to temporarily cache time stamps. |
| 92 | * This is needed when time stamping before the CBMEM area |
| 93 | * is initialized. The function timestamp_sync() is used to |
| 94 | * write the time stamps to the CBMEM area. This is done in |
| 95 | * hardwaremain() |
| 96 | */ |
| 97 | |
| 98 | void timestamp_stash(enum timestamp_id id) |
| 99 | { |
| 100 | if (timestamp_entries >= MAX_TIMESTAMP_CACHE) { |
| 101 | printk(BIOS_ERR, "ERROR: failed to add timestamp to cache\n"); |
| 102 | return; |
| 103 | } |
| 104 | timestamp_cache[timestamp_entries].id = id; |
| 105 | timestamp_cache[timestamp_entries].time = rdtsc(); |
| 106 | timestamp_entries++; |
| 107 | } |
| 108 | |
| 109 | void timestamp_sync(void) |
| 110 | { |
| 111 | int i; |
| 112 | for (i = 0; i < timestamp_entries; i++) |
| 113 | timestamp_add(timestamp_cache[i].id, timestamp_cache[i].time); |
| 114 | timestamp_entries = 0; |
| 115 | } |
| 116 | |
| 117 | #endif |