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