xref: /openbmc/u-boot/arch/x86/cpu/coreboot/timestamp.c (revision ad3098f7)
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 The ChromiumOS Authors.  All rights reserved.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <asm/arch/timestamp.h>
11 #include <asm/arch/sysinfo.h>
12 #include <linux/compiler.h>
13 
14 struct timestamp_entry {
15 	uint32_t	entry_id;
16 	uint64_t	entry_stamp;
17 } __packed;
18 
19 struct timestamp_table {
20 	uint64_t	base_time;
21 	uint32_t	max_entries;
22 	uint32_t	num_entries;
23 	struct timestamp_entry entries[0]; /* Variable number of entries */
24 } __packed;
25 
26 static struct timestamp_table *ts_table  __attribute__((section(".data")));
27 
28 void timestamp_init(void)
29 {
30 	timestamp_add_now(TS_U_BOOT_INITTED);
31 }
32 
33 void timestamp_add(enum timestamp_id id, uint64_t ts_time)
34 {
35 	struct timestamp_entry *tse;
36 
37 	if (!ts_table || (ts_table->num_entries == ts_table->max_entries))
38 		return;
39 
40 	tse = &ts_table->entries[ts_table->num_entries++];
41 	tse->entry_id = id;
42 	tse->entry_stamp = ts_time - ts_table->base_time;
43 }
44 
45 void timestamp_add_now(enum timestamp_id id)
46 {
47 	timestamp_add(id, rdtsc());
48 }
49 
50 int timestamp_add_to_bootstage(void)
51 {
52 	uint i;
53 
54 	if (!ts_table)
55 		return -1;
56 
57 	for (i = 0; i < ts_table->num_entries; i++) {
58 		struct timestamp_entry *tse = &ts_table->entries[i];
59 		const char *name = NULL;
60 
61 		switch (tse->entry_id) {
62 		case TS_START_ROMSTAGE:
63 			name = "start-romstage";
64 			break;
65 		case TS_BEFORE_INITRAM:
66 			name = "before-initram";
67 			break;
68 		case TS_DEVICE_INITIALIZE:
69 			name = "device-initialize";
70 			break;
71 		case TS_DEVICE_DONE:
72 			name = "device-done";
73 			break;
74 		case TS_SELFBOOT_JUMP:
75 			name = "selfboot-jump";
76 			break;
77 		}
78 		if (name) {
79 			bootstage_add_record(0, name, BOOTSTAGEF_ALLOC,
80 					     tse->entry_stamp /
81 							get_tbclk_mhz());
82 		}
83 	}
84 
85 	return 0;
86 }
87