summaryrefslogtreecommitdiff
path: root/common/system_boot_time.c
blob: 62ce39be7e143e74b056d2a5341452d4050bc2cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "common.h"
#include "console.h"
#include "host_command.h"
#include "system.h"
#include "util.h"

#include <stdbool.h>

#ifdef CONFIG_SYSTEM_BOOT_TIME_LOGGING
/* Content of ap_boot_time will be lost on sysjump */
static struct ec_response_get_boot_time ap_boot_time;
#endif

/* This function updates timestamp for ap boot time params */
void update_ap_boot_time(enum boot_time_param param)
{
#ifdef CONFIG_SYSTEM_BOOT_TIME_LOGGING
	static bool ap_booted; /* tracks AP booted after #PLTRST */
	static bool pltrst_transition; /* tracks #PLTRST transtion */

	if (param > RESET_CNT) {
		ccprintf("invalid boot_time_param: %d\n", param);
		return;
	}
	if (param < RESET_CNT) {
		ap_boot_time.timestamp[param] = get_time().val;
		ccprintf("Boot Time: %d, %llu\n", param,
			 ap_boot_time.timestamp[param]);
	}

	/* warm reboot counter set to 1 only when #PLTRST transtion is complete
	 * and AP is already booted (ap_booted = 1).
	 * In initial boot or cold reboot scenario (ap_booted = 0) warm reboot
	 * counter will be set to 0.
	 */
	switch (param) {
	case PLTRST_HIGH:
		/* warm reboot happened */
		if (ap_booted && pltrst_transition)
			ap_boot_time.cnt = 1;
		else /* cold reboot happened */
			ap_boot_time.cnt = 0;

		if (pltrst_transition) {
			ap_booted = true;
			pltrst_transition = false;
		}
		break;
	case PLTRST_LOW:
		/* #PLTRST is in transition state */
		pltrst_transition = true;
		break;
	case RESET_CNT:
		ap_boot_time.cnt = 0;

		if (ap_booted)
			pltrst_transition = false;

		ap_booted = false;
		break;
	default:
		break;
	}
#endif
}

#ifdef CONFIG_SYSTEM_BOOT_TIME_LOGGING
/* Returns system boot time data */
static enum ec_status
host_command_get_boot_time(struct host_cmd_handler_args *args)
{
	struct ec_response_get_boot_time *boot_time = args->response;

	if (args->response_max < sizeof(*boot_time)) {
		return EC_RES_RESPONSE_TOO_BIG;
	}

	/* update current time */
	update_ap_boot_time(EC_CUR_TIME);

	/* copy data from ap_boot_time struct */
	memcpy(boot_time, &ap_boot_time, sizeof(*boot_time));

	args->response_size = sizeof(*boot_time);

	return EC_RES_SUCCESS;
}

DECLARE_HOST_COMMAND(EC_CMD_GET_BOOT_TIME, host_command_get_boot_time,
		     EC_VER_MASK(0));
#endif