2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
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.
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.
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
22 #include <console/console.h>
24 #include <timestamp.h>
25 #include <arch/early_variables.h>
28 #define MAX_TIMESTAMPS 30
30 static struct timestamp_table
* ts_table_p CAR_GLOBAL
= NULL
;
31 static tsc_t ts_basetime CAR_GLOBAL
= { .lo
= 0, .hi
=0 };
33 static void timestamp_stash(enum timestamp_id id
, tsc_t ts_time
);
35 static uint64_t tsc_to_uint64(tsc_t tstamp
)
37 return (((uint64_t)tstamp
.hi
) << 32) + tstamp
.lo
;
40 static void timestamp_real_init(tsc_t base
)
42 struct timestamp_table
* tst
;
44 tst
= cbmem_add(CBMEM_ID_TIMESTAMP
,
45 sizeof(struct timestamp_table
) +
46 MAX_TIMESTAMPS
* sizeof(struct timestamp_entry
));
49 printk(BIOS_ERR
, "ERROR: failed to allocate timestamp table\n");
53 tst
->base_time
= tsc_to_uint64(base
);
54 tst
->max_entries
= MAX_TIMESTAMPS
;
57 car_set_var(ts_table_p
, tst
);
60 void timestamp_add(enum timestamp_id id
, tsc_t ts_time
)
62 struct timestamp_entry
*tse
;
63 struct timestamp_table
*ts_table
= NULL
;
68 ts_table
= car_get_var(ts_table_p
);
70 timestamp_stash(id
, ts_time
);
73 if (ts_table
->num_entries
== ts_table
->max_entries
)
76 tse
= &ts_table
->entries
[ts_table
->num_entries
++];
78 tse
->entry_stamp
= tsc_to_uint64(ts_time
) - ts_table
->base_time
;
81 void timestamp_add_now(enum timestamp_id id
)
83 timestamp_add(id
, rdtsc());
86 #define MAX_TIMESTAMP_CACHE 8
87 struct timestamp_cache
{
90 } timestamp_cache
[MAX_TIMESTAMP_CACHE
] CAR_GLOBAL
;
92 static int timestamp_entries CAR_GLOBAL
= 0;
95 * timestamp_stash() allows to temporarily cache timestamps.
96 * This is needed when timestamping before the CBMEM area
97 * is initialized. The function timestamp_do_sync() is used to
98 * write the timestamps to the CBMEM area and this is done as
99 * part of CAR migration for romstage, and in ramstage main().
102 static void timestamp_stash(enum timestamp_id id
, tsc_t ts_time
)
104 struct timestamp_cache
*ts_cache
= car_get_var(timestamp_cache
);
105 int ts_entries
= car_get_var(timestamp_entries
);
107 if (ts_entries
>= MAX_TIMESTAMP_CACHE
) {
108 printk(BIOS_ERR
, "ERROR: failed to add timestamp to cache\n");
111 ts_cache
[ts_entries
].id
= id
;
112 ts_cache
[ts_entries
].time
= ts_time
;
113 car_set_var(timestamp_entries
, ++ts_entries
);
116 static void timestamp_do_sync(void)
118 struct timestamp_cache
*ts_cache
= car_get_var(timestamp_cache
);
119 int ts_entries
= car_get_var(timestamp_entries
);
122 for (i
= 0; i
< ts_entries
; i
++)
123 timestamp_add(ts_cache
[i
].id
, ts_cache
[i
].time
);
124 car_set_var(timestamp_entries
, 0);
127 void timestamp_init(tsc_t base
)
133 /* Copy of basetime, it is too early for CBMEM. */
134 car_set_var(ts_basetime
, base
);
136 struct timestamp_table
* tst
;
138 /* Locate and use an already existing table. */
139 tst
= cbmem_find(CBMEM_ID_TIMESTAMP
);
141 car_set_var(ts_table_p
, tst
);
145 /* Copy of basetime, may be too early for CBMEM. */
146 car_set_var(ts_basetime
, base
);
147 timestamp_real_init(base
);
151 void timestamp_reinit(void)
157 timestamp_real_init(car_get_var(ts_basetime
));
159 if (!car_get_var(ts_table_p
))
160 timestamp_init(car_get_var(ts_basetime
));
162 if (car_get_var(ts_table_p
))
166 /* Call timestamp_reinit at CAR migration time. */
167 CAR_MIGRATE(timestamp_reinit
)