Add timestamp collecting to coreboot.
[coreboot.git] / src / lib / timestamp.c
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>
24
25 #define MAX_TIMESTAMPS 30
26
27 #ifndef __PRE_RAM__
28 static struct timestamp_table* ts_table;
29 #endif
30
31 static uint64_t tsc_to_uint64(tsc_t tstamp)
32 {
33         return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
34 }
35
36 void timestamp_init(tsc_t base)
37 {
38         struct timestamp_table* tst;
39
40         tst = cbmem_add(CBMEM_ID_TIMESTAMP,
41                         sizeof(struct timestamp_table) +
42                         MAX_TIMESTAMPS * sizeof(struct timestamp_entry));
43
44         if (!tst) {
45                 printk(BIOS_ERR, "ERROR: failed to allocate timstamp table\n");
46                 return;
47         }
48
49         tst->base_time = tsc_to_uint64(base);
50         tst->max_entries = MAX_TIMESTAMPS;
51         tst->num_entries = 0;
52 }
53
54 void timestamp_add(enum timestamp_id id, tsc_t ts_time)
55 {
56         struct timestamp_entry *tse;
57 #ifdef __PRE_RAM__
58         struct timestamp_table *ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
59 #else
60         if (!ts_table)
61                 ts_table = cbmem_find(CBMEM_ID_TIMESTAMP);
62 #endif
63         if (!ts_table || (ts_table->num_entries == ts_table->max_entries))
64                 return;
65
66         tse = &ts_table->entries[ts_table->num_entries++];
67         tse->entry_id = id;
68         tse->entry_stamp = tsc_to_uint64(ts_time) - ts_table->base_time;
69 }
70
71 void timestamp_add_now(enum timestamp_id id)
72 {
73         timestamp_add(id, rdtsc());
74 }