blob: eac33a2affed240d588008df3ea02e09a98f76c5 [file] [log] [blame]
Vadim Bendebury6f72d692011-09-21 16:12:39 -07001/*
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älkkib766b1c2013-09-07 17:26:08 +030020#include <stddef.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070021#include <stdint.h>
22#include <console/console.h>
23#include <cbmem.h>
24#include <timestamp.h>
Stefan Reinauer4221a192012-10-15 15:23:20 -070025#include <cpu/x86/car.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070026
27#define MAX_TIMESTAMPS 30
28
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030029static struct timestamp_table* ts_table CAR_GLOBAL = NULL;
30static tsc_t ts_basetime CAR_GLOBAL = { .lo = 0, .hi =0 };
31
32static void timestamp_stash(enum timestamp_id id, tsc_t ts_time);
Vadim Bendebury6f72d692011-09-21 16:12:39 -070033
34static uint64_t tsc_to_uint64(tsc_t tstamp)
35{
36 return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
37}
38
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030039static void timestamp_real_init(tsc_t base)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070040{
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 Menzel2edf77c2013-01-22 11:46:34 +010048 printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -070049 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älkkib766b1c2013-09-07 17:26:08 +030055
56 ts_table = tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070057}
58
59void timestamp_add(enum timestamp_id id, tsc_t ts_time)
60{
61 struct timestamp_entry *tse;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030062
63 if (!ts_table) {
64 timestamp_stash(id, ts_time);
65 return;
66 }
67 if (ts_table->num_entries == ts_table->max_entries)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070068 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
75void timestamp_add_now(enum timestamp_id id)
76{
77 timestamp_add(id, rdtsc());
78}
Stefan Reinauer4221a192012-10-15 15:23:20 -070079
Stefan Reinauer4221a192012-10-15 15:23:20 -070080#define MAX_TIMESTAMP_CACHE 8
81struct timestamp_cache {
82 enum timestamp_id id;
83 tsc_t time;
84} timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL;
85
86static 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 Reinauer6adef082013-05-09 16:30:06 -070093 * ram stage main()
Stefan Reinauer4221a192012-10-15 15:23:20 -070094 */
95
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030096static void timestamp_stash(enum timestamp_id id, tsc_t ts_time)
Stefan Reinauer4221a192012-10-15 15:23:20 -070097{
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älkkib766b1c2013-09-07 17:26:08 +0300103 timestamp_cache[timestamp_entries].time = ts_time;
Stefan Reinauer4221a192012-10-15 15:23:20 -0700104 timestamp_entries++;
105}
106
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300107static void timestamp_do_sync(void)
Stefan Reinauer4221a192012-10-15 15:23:20 -0700108{
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älkkib766b1c2013-09-07 17:26:08 +0300115void timestamp_init(tsc_t base)
116{
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300117#ifdef __PRE_RAM__
118 /* Copy of basetime, it is too early for CBMEM. */
119 ts_basetime = base;
120#else
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300121 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älkkib766b1c2013-09-07 17:26:08 +0300129
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300130 /* Copy of basetime, may be too early for CBMEM. */
131 ts_basetime = base;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300132 timestamp_real_init(base);
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300133#endif
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300134}
135
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300136void timestamp_sync(void)
137{
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300138#ifdef __PRE_RAM__
139 timestamp_real_init(ts_basetime);
140#else
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300141 if (!ts_table)
142 timestamp_init(ts_basetime);
Stefan Reinauer4221a192012-10-15 15:23:20 -0700143#endif
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300144 if (ts_table)
145 timestamp_do_sync();
146}