blob: 8942649099c51b0c33fd47208f534afa8bd61ebd [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 Reinauerfd4f4132013-06-19 12:25:44 -070025#include <arch/early_variables.h>
Kyösti Mälkkif56ff902013-09-08 13:10:28 +030026#include <cpu/x86/lapic.h>
Vadim Bendebury6f72d692011-09-21 16:12:39 -070027
28#define MAX_TIMESTAMPS 30
29
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030030static struct timestamp_table* ts_table CAR_GLOBAL = NULL;
31static tsc_t ts_basetime CAR_GLOBAL = { .lo = 0, .hi =0 };
32
33static void timestamp_stash(enum timestamp_id id, tsc_t ts_time);
Vadim Bendebury6f72d692011-09-21 16:12:39 -070034
35static uint64_t tsc_to_uint64(tsc_t tstamp)
36{
37 return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
38}
39
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030040static void timestamp_real_init(tsc_t base)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070041{
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 Menzel2edf77c2013-01-22 11:46:34 +010049 printk(BIOS_ERR, "ERROR: failed to allocate timestamp table\n");
Vadim Bendebury6f72d692011-09-21 16:12:39 -070050 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älkkib766b1c2013-09-07 17:26:08 +030056
57 ts_table = tst;
Vadim Bendebury6f72d692011-09-21 16:12:39 -070058}
59
60void timestamp_add(enum timestamp_id id, tsc_t ts_time)
61{
62 struct timestamp_entry *tse;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030063
Kyösti Mälkkif56ff902013-09-08 13:10:28 +030064 if (!boot_cpu())
65 return;
66
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030067 if (!ts_table) {
68 timestamp_stash(id, ts_time);
69 return;
70 }
71 if (ts_table->num_entries == ts_table->max_entries)
Vadim Bendebury6f72d692011-09-21 16:12:39 -070072 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
79void timestamp_add_now(enum timestamp_id id)
80{
81 timestamp_add(id, rdtsc());
82}
Stefan Reinauer4221a192012-10-15 15:23:20 -070083
Stefan Reinauer4221a192012-10-15 15:23:20 -070084#define MAX_TIMESTAMP_CACHE 8
85struct timestamp_cache {
86 enum timestamp_id id;
87 tsc_t time;
88} timestamp_cache[MAX_TIMESTAMP_CACHE] CAR_GLOBAL;
89
90static 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 Reinauer6adef082013-05-09 16:30:06 -070097 * ram stage main()
Stefan Reinauer4221a192012-10-15 15:23:20 -070098 */
99
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300100static void timestamp_stash(enum timestamp_id id, tsc_t ts_time)
Stefan Reinauer4221a192012-10-15 15:23:20 -0700101{
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älkkib766b1c2013-09-07 17:26:08 +0300107 timestamp_cache[timestamp_entries].time = ts_time;
Stefan Reinauer4221a192012-10-15 15:23:20 -0700108 timestamp_entries++;
109}
110
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300111static void timestamp_do_sync(void)
Stefan Reinauer4221a192012-10-15 15:23:20 -0700112{
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älkkib766b1c2013-09-07 17:26:08 +0300119void timestamp_init(tsc_t base)
120{
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300121 if (!boot_cpu())
122 return;
123
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300124#ifdef __PRE_RAM__
125 /* Copy of basetime, it is too early for CBMEM. */
126 ts_basetime = base;
127#else
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300128 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älkkib766b1c2013-09-07 17:26:08 +0300136
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300137 /* Copy of basetime, may be too early for CBMEM. */
138 ts_basetime = base;
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300139 timestamp_real_init(base);
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300140#endif
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300141}
142
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300143void timestamp_reinit(void)
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300144{
Kyösti Mälkkif56ff902013-09-08 13:10:28 +0300145 if (!boot_cpu())
146 return;
147
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300148#ifdef __PRE_RAM__
149 timestamp_real_init(ts_basetime);
150#else
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300151 if (!ts_table)
152 timestamp_init(ts_basetime);
Stefan Reinauer4221a192012-10-15 15:23:20 -0700153#endif
Kyösti Mälkki3d45c402013-09-07 20:26:36 +0300154 if (ts_table)
155 timestamp_do_sync();
156}
Kyösti Mälkkicbf5bdf2013-09-10 00:07:21 +0300157
158/* Call timestamp_reinit at CAR migration time. */
159CAR_MIGRATE(timestamp_reinit)