common: Add uptime host command

This moves the EC_CMD_GET_UPTIME_INFO command from behind the
CONFIG_CMD_AP_RESET_LOG config in chipset.c into the generic
common/uptime.c file, so that all boards in the codebase can use it. If
CONFIG_CMD_AP_RESET_LOG is enabled, the "AP reset stats" will be filled.
Otherwise, ap_reset_stats is a no-op and recent_ap_reset is filled with
zero.

BRANCH=none
BUG=chromium:997314
TEST=cat /sys/kernel/debug/cros_fp/uptime

Change-Id: I3b6f91b2dd22d3d55b707309ec1fdfd26d42fd70
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1769393
Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
This commit is contained in:
Tom Hughes 2019-08-23 15:06:57 -07:00 committed by Commit Bot
parent dfbc632cd5
commit af51b9ea19
10 changed files with 167 additions and 27 deletions

View File

@ -55,6 +55,9 @@
#undef CONFIG_WATCHDOG_HELP
#undef CONFIG_CMD_KEYBOARD
/* Not enough RO flash space */
#undef CONFIG_HOSTCMD_GET_UPTIME_INFO
#define CONFIG_HIBERNATE_WAKEUP_PINS (STM32_PWR_CSR_EWUP1 | STM32_PWR_CSR_EWUP6)
#ifndef __ASSEMBLER__

View File

@ -77,6 +77,7 @@ common-$(CONFIG_FLASH_NVMEM_VARS)+=nvmem_vars.o
common-$(CONFIG_FMAP)+=fmap.o
common-$(CONFIG_GESTURE_SW_DETECTION)+=gesture.o
common-$(CONFIG_HOSTCMD_EVENTS)+=host_event_commands.o
common-$(CONFIG_HOSTCMD_GET_UPTIME_INFO)+=uptime.o
common-$(CONFIG_HOSTCMD_PD)+=host_command_master.o
common-$(CONFIG_HOSTCMD_RTC)+=rtc.o
common-$(CONFIG_I2C_DEBUG)+=i2c_trace.o

View File

@ -101,45 +101,29 @@ void report_ap_reset(enum chipset_shutdown_reason reason)
reset_log_checksum = calc_reset_log_checksum();
}
static int host_command_get_uptime_info(struct host_cmd_handler_args *args)
test_mockable enum ec_error_list
get_ap_reset_stats(struct ap_reset_log_entry *reset_log_entries,
size_t num_reset_log_entries, uint32_t *resets_since_ec_boot)
{
/*
* In the current implementation, not all terms are preserved across a
* sysjump. Future implementations may preserve additional information.
*
* time_since_ec_boot_ms: preserved, but wraps at ~50 days
* ec_reset_flags: preserved, with 'sysjump' added
* ap_resets_since_ec_boot: Not preserved
* recent_ap_reset[*]: Not preserved
*/
struct ec_response_uptime_info *r = args->response;
timestamp_t now = get_time();
uint32_t now_ms = (uint32_t)(now.val / MSEC);
size_t log_address = 0;
size_t i = 0;
size_t log_address;
size_t i;
r->time_since_ec_boot_ms = now_ms;
r->ec_reset_flags = system_get_reset_flags();
memset(r->recent_ap_reset, 0, sizeof(r->recent_ap_reset));
if (reset_log_entries == NULL || resets_since_ec_boot == NULL)
return EC_ERROR_INVAL;
mutex_lock(&reset_log_mutex);
r->ap_resets_since_ec_boot = ap_resets_since_ec_boot;
*resets_since_ec_boot = ap_resets_since_ec_boot;
for (i = 0;
i != ARRAY_SIZE(reset_logs) && i != ARRAY_SIZE(r->recent_ap_reset);
i != ARRAY_SIZE(reset_logs) && i != num_reset_log_entries;
++i) {
log_address = (next_reset_log + i) &
(ARRAY_SIZE(reset_logs) - 1);
r->recent_ap_reset[i] = reset_logs[log_address];
reset_log_entries[i] = reset_logs[log_address];
}
mutex_unlock(&reset_log_mutex);
args->response_size = sizeof(*r);
return EC_RES_SUCCESS;
return EC_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_GET_UPTIME_INFO,
host_command_get_uptime_info,
EC_VER_MASK(0));
#endif /* !CONFIG_AP_RESET_LOG */

41
common/uptime.c Normal file
View File

@ -0,0 +1,41 @@
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <stddef.h>
#include "chipset.h"
#include "system.h"
#include "host_command.h"
#include "util.h"
static int host_command_get_uptime_info(struct host_cmd_handler_args *args)
{
/*
* In the current implementation, not all terms are preserved across a
* sysjump. Future implementations may preserve additional information.
*
* time_since_ec_boot_ms: preserved, but wraps at ~50 days
* ec_reset_flags: preserved, with 'sysjump' added
* ap_resets_since_ec_boot: Not preserved
* recent_ap_reset[*]: Not preserved
*/
struct ec_response_uptime_info *r = args->response;
timestamp_t now = get_time();
uint32_t now_ms = (uint32_t)(now.val / MSEC);
enum ec_error_list rc;
r->time_since_ec_boot_ms = now_ms;
r->ec_reset_flags = system_get_reset_flags();
memset(r->recent_ap_reset, 0, sizeof(r->recent_ap_reset));
rc = get_ap_reset_stats(r->recent_ap_reset,
ARRAY_SIZE(r->recent_ap_reset),
&r->ap_resets_since_ec_boot);
args->response_size = sizeof(*r);
return rc == EC_SUCCESS ? EC_RES_SUCCESS : EC_RES_ERROR;
}
DECLARE_HOST_COMMAND(EC_CMD_GET_UPTIME_INFO,
host_command_get_uptime_info,
EC_VER_MASK(0));

View File

@ -15,7 +15,9 @@
#include "common.h"
#include "compile_time_macros.h"
#include "ec_commands.h"
#include "gpio.h"
#include "stddef.h"
/*
* Chipset state mask
@ -256,10 +258,29 @@ __override_proto enum critical_shutdown board_system_is_idle(
*/
void report_ap_reset(enum chipset_shutdown_reason reason);
/**
* Get statistics about AP resets.
*
* @param reset_log_entries Pointer to array of log entries.
* @param num_reset_log_entries Number of items in reset_log_entries.
* @param resets_since_ec_boot Number of AP resets since EC boot.
*/
test_mockable enum ec_error_list
get_ap_reset_stats(struct ap_reset_log_entry *reset_log_entries,
size_t num_reset_log_entries,
uint32_t *resets_since_ec_boot);
#else
static inline void report_ap_reset(enum chipset_shutdown_reason reason) { }
test_mockable_static_inline enum ec_error_list
get_ap_reset_stats(struct ap_reset_log_entry *reset_log_entries,
size_t num_reset_log_entries, uint32_t *resets_since_ec_boot)
{
return EC_SUCCESS;
}
#endif /* !CONFIG_CMD_AP_RESET_LOG */
#endif /* __CROS_EC_CHIPSET_H */

View File

@ -225,10 +225,12 @@ enum ec_error_list {
#ifdef TEST_BUILD
#define test_mockable __attribute__((weak))
#define test_mockable_static __attribute__((weak))
#define test_mockable_static_inline __attribute__((weak))
#define test_export_static
#else
#define test_mockable
#define test_mockable_static static
#define test_mockable_static_inline static inline
#define test_export_static static
#endif

View File

@ -2127,6 +2127,9 @@
#define CONFIG_HOSTCMD_LOCATE_CHIP
#endif
/* Command to get the EC uptime (and optionally AP reset stats) */
#define CONFIG_HOSTCMD_GET_UPTIME_INFO
/* List of host commands whose debug output will be suppressed */
#undef CONFIG_SUPPRESSED_HOST_COMMANDS

View File

@ -66,6 +66,7 @@ test-list-host += static_if_error
test-list-host += system
test-list-host += thermal
test-list-host += timer_dos
test-list-host += uptime
test-list-host += usb_pd
test-list-host += usb_pd_giveback
test-list-host += usb_pd_rev30
@ -140,6 +141,7 @@ system-y=system.o
thermal-y=thermal.o
timer_calib-y=timer_calib.o
timer_dos-y=timer_dos.o
uptime-y=uptime.o
usb_pd-y=usb_pd.o
usb_pd_giveback-y=usb_pd.o
usb_pd_rev30-y=usb_pd.o

73
test/uptime.c Normal file
View File

@ -0,0 +1,73 @@
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <stdbool.h>
#include "common.h"
#include "ec_commands.h"
#include "host_command.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"
static bool get_ap_reset_stats_should_succeed = true;
/* Mocks */
enum ec_error_list
get_ap_reset_stats(struct ap_reset_log_entry *reset_log_entries,
size_t num_reset_log_entries, uint32_t *resets_since_ec_boot)
{
return get_ap_reset_stats_should_succeed ? EC_SUCCESS : EC_ERROR_INVAL;
}
timestamp_t get_time(void)
{
timestamp_t fake_time = { .val = 42 * MSEC };
return fake_time;
}
/* Tests */
test_static int test_host_uptime_info_command_success(void)
{
int rv;
struct ec_response_uptime_info resp = { 0 };
get_ap_reset_stats_should_succeed = true;
rv = test_send_host_command(EC_CMD_GET_UPTIME_INFO, 0, NULL, 0, &resp,
sizeof(resp));
TEST_ASSERT(rv == EC_RES_SUCCESS);
TEST_ASSERT(resp.time_since_ec_boot_ms == 42);
return EC_RES_SUCCESS;
}
test_static int test_host_uptime_info_command_failure(void)
{
int rv;
struct ec_response_uptime_info resp = { 0 };
get_ap_reset_stats_should_succeed = false;
rv = test_send_host_command(EC_CMD_GET_UPTIME_INFO, 0, NULL, 0, &resp,
sizeof(resp));
TEST_ASSERT(rv == EC_RES_ERROR);
return EC_RES_SUCCESS;
}
void run_test(void)
{
test_reset();
RUN_TEST(test_host_uptime_info_command_success);
RUN_TEST(test_host_uptime_info_command_failure);
test_print_result();
}

10
test/uptime.tasklist Normal file
View File

@ -0,0 +1,10 @@
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* See CONFIG_TASK_LIST in config.h for details.
*/
#define CONFIG_TEST_TASK_LIST