2018-08-16 10:39:40 +05:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2021-08-19 21:07:15 -07:00
|
|
|
#include <device.h>
|
2018-08-16 10:39:40 +05:30
|
|
|
#include <zephyr.h>
|
|
|
|
#include <kernel.h>
|
2021-04-02 10:49:01 -07:00
|
|
|
#include <timeout_q.h>
|
2018-08-16 10:39:40 +05:30
|
|
|
#include <init.h>
|
|
|
|
#include <string.h>
|
2021-08-19 21:07:15 -07:00
|
|
|
#include <pm/device.h>
|
2021-04-29 13:32:28 +02:00
|
|
|
#include <pm/pm.h>
|
|
|
|
#include <pm/state.h>
|
2021-06-11 09:22:33 -07:00
|
|
|
#include <pm/policy.h>
|
2021-05-13 09:49:18 -04:00
|
|
|
#include <tracing/tracing.h>
|
2018-08-16 10:39:40 +05:30
|
|
|
|
2021-02-10 15:45:29 -08:00
|
|
|
#define PM_STATES_LEN (1 + PM_STATE_SOFT_OFF - PM_STATE_ACTIVE)
|
2018-08-16 10:39:40 +05:30
|
|
|
#include <logging/log.h>
|
2021-10-27 13:39:12 +02:00
|
|
|
LOG_MODULE_REGISTER(pm, CONFIG_PM_LOG_LEVEL);
|
2018-08-16 10:39:40 +05:30
|
|
|
|
2021-10-22 10:21:52 -07:00
|
|
|
static bool post_ops_done = true;
|
2020-10-14 21:59:51 -07:00
|
|
|
static sys_slist_t pm_notifiers = SYS_SLIST_STATIC_INIT(&pm_notifiers);
|
2021-10-22 10:44:55 -07:00
|
|
|
static struct pm_state_info z_power_states[CONFIG_MP_NUM_CPUS];
|
2020-10-14 21:59:51 -07:00
|
|
|
static struct k_spinlock pm_notifier_lock;
|
2018-08-16 10:39:40 +05:30
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
#ifdef CONFIG_PM_STATS
|
|
|
|
|
|
|
|
#include <stats/stats.h>
|
|
|
|
#include <sys/util_macro.h>
|
2018-10-24 13:54:20 +05:30
|
|
|
|
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
struct pm_cpu_timing {
|
2021-10-05 11:25:53 -05:00
|
|
|
uint32_t timer_start;
|
|
|
|
uint32_t timer_end;
|
|
|
|
};
|
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
static struct pm_cpu_timing pm_cpu_timings[CONFIG_MP_NUM_CPUS];
|
2018-10-24 13:54:20 +05:30
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
static inline void pm_start_timer(void)
|
2018-10-24 13:54:20 +05:30
|
|
|
{
|
2021-10-12 08:52:30 -05:00
|
|
|
pm_cpu_timings[_current_cpu->id].timer_start = k_cycle_get_32();
|
2018-10-24 13:54:20 +05:30
|
|
|
}
|
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
static inline void pm_stop_timer(void)
|
2018-10-24 13:54:20 +05:30
|
|
|
{
|
2021-10-12 08:52:30 -05:00
|
|
|
pm_cpu_timings[_current_cpu->id].timer_end = k_cycle_get_32();
|
2018-10-24 13:54:20 +05:30
|
|
|
}
|
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
STATS_SECT_START(pm_cpu_stats)
|
|
|
|
STATS_SECT_ENTRY32(state_count)
|
|
|
|
STATS_SECT_ENTRY32(state_last_cycles)
|
|
|
|
STATS_SECT_ENTRY32(state_total_cycles)
|
|
|
|
STATS_SECT_END;
|
2018-10-24 13:54:20 +05:30
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
STATS_NAME_START(pm_cpu_stats)
|
|
|
|
STATS_NAME(pm_cpu_stats, state_count)
|
|
|
|
STATS_NAME(pm_cpu_stats, state_last_cycles)
|
|
|
|
STATS_NAME(pm_cpu_stats, state_total_cycles)
|
|
|
|
STATS_NAME_END(pm_cpu_stats);
|
|
|
|
|
|
|
|
#define PM_STAT_NAME_LEN sizeof("pm_cpu_XXX_state_X_stats")
|
|
|
|
static char pm_cpu_stat_names[CONFIG_MP_NUM_CPUS][PM_STATES_LEN][PM_STAT_NAME_LEN];
|
|
|
|
static struct stats_pm_cpu_stats pm_cpu_stats[CONFIG_MP_NUM_CPUS][PM_STATES_LEN];
|
2018-10-24 13:54:20 +05:30
|
|
|
|
2021-10-12 08:52:30 -05:00
|
|
|
static int pm_stats_init(const struct device *unused)
|
2018-10-24 13:54:20 +05:30
|
|
|
{
|
2021-10-05 11:25:53 -05:00
|
|
|
for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) {
|
|
|
|
for (int j = 0; j < PM_STATES_LEN; j++) {
|
2021-10-12 08:52:30 -05:00
|
|
|
snprintk(pm_cpu_stat_names[i][j], PM_STAT_NAME_LEN,
|
|
|
|
"pm_cpu_%03d_state_%1d_stats", i, j);
|
|
|
|
stats_init(&(pm_cpu_stats[i][j].s_hdr), STATS_SIZE_32, 3,
|
|
|
|
STATS_NAME_INIT_PARMS(pm_cpu_stats));
|
|
|
|
stats_register(pm_cpu_stat_names[i][j], &(pm_cpu_stats[i][j].s_hdr));
|
2021-10-05 11:25:53 -05:00
|
|
|
}
|
2018-10-24 13:54:20 +05:30
|
|
|
}
|
2021-10-12 08:52:30 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(pm_stats_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|
|
|
|
|
|
|
|
static void pm_stats_update(enum pm_state state)
|
|
|
|
{
|
|
|
|
uint8_t cpu = _current_cpu->id;
|
|
|
|
uint32_t time_total =
|
|
|
|
pm_cpu_timings[cpu].timer_end -
|
|
|
|
pm_cpu_timings[cpu].timer_start;
|
|
|
|
|
|
|
|
STATS_INC(pm_cpu_stats[cpu][state], state_count);
|
|
|
|
STATS_INCN(pm_cpu_stats[cpu][state], state_total_cycles, time_total);
|
|
|
|
STATS_SET(pm_cpu_stats[cpu][state], state_last_cycles, time_total);
|
2018-10-24 13:54:20 +05:30
|
|
|
}
|
|
|
|
#else
|
2021-10-12 08:52:30 -05:00
|
|
|
static inline void pm_start_timer(void) {}
|
|
|
|
static inline void pm_stop_timer(void) {}
|
|
|
|
|
|
|
|
|
|
|
|
static void pm_stats_update(enum pm_state state) {}
|
2018-10-24 13:54:20 +05:30
|
|
|
#endif
|
|
|
|
|
2021-08-19 21:07:15 -07:00
|
|
|
#ifdef CONFIG_PM_DEVICE
|
|
|
|
extern const struct device *__pm_device_slots_start[];
|
|
|
|
|
|
|
|
/* Number of devices successfully suspended. */
|
|
|
|
static size_t num_susp;
|
|
|
|
|
|
|
|
static int pm_suspend_devices(void)
|
|
|
|
{
|
|
|
|
const struct device *devs;
|
|
|
|
size_t devc;
|
|
|
|
|
|
|
|
devc = z_device_get_all_static(&devs);
|
|
|
|
|
|
|
|
num_susp = 0;
|
|
|
|
|
|
|
|
for (const struct device *dev = devs + devc - 1; dev >= devs; dev--) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* ignore busy devices */
|
|
|
|
if (pm_device_is_busy(dev) || pm_device_wakeup_is_enabled(dev)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pm_device_state_set(dev, PM_DEVICE_STATE_SUSPENDED);
|
|
|
|
/* ignore devices not supporting or already at the given state */
|
|
|
|
if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) {
|
|
|
|
continue;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
LOG_ERR("Device %s did not enter %s state (%d)",
|
|
|
|
dev->name,
|
|
|
|
pm_device_state_str(PM_DEVICE_STATE_SUSPENDED),
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
__pm_device_slots_start[num_susp] = dev;
|
|
|
|
num_susp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pm_resume_devices(void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_susp; i++) {
|
|
|
|
pm_device_state_set(__pm_device_slots_start[i],
|
|
|
|
PM_DEVICE_STATE_ACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
num_susp = 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PM_DEVICE */
|
|
|
|
|
2021-05-13 13:02:25 -07:00
|
|
|
static inline void exit_pos_ops(struct pm_state_info info)
|
2021-01-07 13:17:57 -08:00
|
|
|
{
|
2021-05-13 13:02:25 -07:00
|
|
|
extern __weak void
|
|
|
|
pm_power_state_exit_post_ops(struct pm_state_info info);
|
|
|
|
|
|
|
|
if (pm_power_state_exit_post_ops != NULL) {
|
|
|
|
pm_power_state_exit_post_ops(info);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This function is supposed to be overridden to do SoC or
|
|
|
|
* architecture specific post ops after sleep state exits.
|
|
|
|
*
|
|
|
|
* The kernel expects that irqs are unlocked after this.
|
|
|
|
*/
|
2021-03-10 15:34:00 -08:00
|
|
|
|
2021-05-13 13:02:25 -07:00
|
|
|
irq_unlock(0);
|
|
|
|
}
|
2021-01-07 13:17:57 -08:00
|
|
|
}
|
|
|
|
|
2021-05-13 13:02:25 -07:00
|
|
|
static inline void pm_state_set(struct pm_state_info info)
|
2021-01-07 13:17:57 -08:00
|
|
|
{
|
2021-05-13 13:02:25 -07:00
|
|
|
extern __weak void
|
|
|
|
pm_power_state_set(struct pm_state_info info);
|
|
|
|
|
|
|
|
if (pm_power_state_set != NULL) {
|
|
|
|
pm_power_state_set(info);
|
|
|
|
}
|
2021-01-07 13:17:57 -08:00
|
|
|
}
|
|
|
|
|
2020-10-14 21:59:51 -07:00
|
|
|
/*
|
|
|
|
* Function called to notify when the system is entering / exiting a
|
|
|
|
* power state
|
|
|
|
*/
|
|
|
|
static inline void pm_state_notify(bool entering_state)
|
|
|
|
{
|
|
|
|
struct pm_notifier *notifier;
|
|
|
|
k_spinlock_key_t pm_notifier_key;
|
2020-12-07 21:51:46 -08:00
|
|
|
void (*callback)(enum pm_state state);
|
2020-10-14 21:59:51 -07:00
|
|
|
|
|
|
|
pm_notifier_key = k_spin_lock(&pm_notifier_lock);
|
|
|
|
SYS_SLIST_FOR_EACH_CONTAINER(&pm_notifiers, notifier, _node) {
|
|
|
|
if (entering_state) {
|
|
|
|
callback = notifier->state_entry;
|
|
|
|
} else {
|
|
|
|
callback = notifier->state_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) {
|
2021-10-22 10:44:55 -07:00
|
|
|
callback(z_power_states[_current_cpu->id].state);
|
2020-10-14 21:59:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
k_spin_unlock(&pm_notifier_lock, pm_notifier_key);
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:29:22 -08:00
|
|
|
void pm_system_resume(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This notification is called from the ISR of the event
|
|
|
|
* that caused exit from kernel idling after PM operations.
|
|
|
|
*
|
|
|
|
* Some CPU low power states require enabling of interrupts
|
|
|
|
* atomically when entering those states. The wake up from
|
|
|
|
* such a state first executes code in the ISR of the interrupt
|
|
|
|
* that caused the wake. This hook will be called from the ISR.
|
|
|
|
* For such CPU LPS states, do post operations and restores here.
|
|
|
|
* The kernel scheduler will get control after the ISR finishes
|
|
|
|
* and it may schedule another thread.
|
|
|
|
*
|
|
|
|
* Call pm_idle_exit_notification_disable() if this
|
|
|
|
* notification is not required.
|
|
|
|
*/
|
|
|
|
if (!post_ops_done) {
|
2021-10-22 10:21:52 -07:00
|
|
|
post_ops_done = true;
|
2021-10-22 10:44:55 -07:00
|
|
|
exit_pos_ops(z_power_states[_current_cpu->id]);
|
2021-03-11 15:02:09 -08:00
|
|
|
pm_state_notify(false);
|
2021-03-11 10:29:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 10:31:35 -08:00
|
|
|
void pm_power_state_force(struct pm_state_info info)
|
|
|
|
{
|
|
|
|
__ASSERT(info.state < PM_STATES_LEN,
|
|
|
|
"Invalid power state %d!", info.state);
|
|
|
|
|
|
|
|
if (info.state == PM_STATE_ACTIVE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)arch_irq_lock();
|
2021-10-22 10:44:55 -07:00
|
|
|
z_power_states[_current_cpu->id] = info;
|
|
|
|
post_ops_done = false;
|
2021-02-23 10:31:35 -08:00
|
|
|
pm_state_notify(true);
|
|
|
|
|
2021-03-11 15:02:09 -08:00
|
|
|
k_sched_lock();
|
2021-10-12 08:52:30 -05:00
|
|
|
pm_start_timer();
|
2021-02-23 10:31:35 -08:00
|
|
|
/* Enter power state */
|
2021-10-22 10:44:55 -07:00
|
|
|
pm_state_set(info);
|
2021-10-12 08:52:30 -05:00
|
|
|
pm_stop_timer();
|
2021-02-23 10:31:35 -08:00
|
|
|
|
2021-03-11 10:29:22 -08:00
|
|
|
pm_system_resume();
|
2021-03-30 15:11:22 -07:00
|
|
|
k_sched_unlock();
|
2021-02-23 10:31:35 -08:00
|
|
|
}
|
|
|
|
|
2021-03-01 13:44:40 -08:00
|
|
|
#if CONFIG_PM_DEVICE
|
2021-01-12 13:23:18 -08:00
|
|
|
static enum pm_state _handle_device_abort(struct pm_state_info info)
|
2020-09-07 12:23:30 -04:00
|
|
|
{
|
|
|
|
LOG_DBG("Some devices didn't enter suspend state!");
|
|
|
|
pm_resume_devices();
|
2020-10-14 21:59:51 -07:00
|
|
|
|
2021-10-22 10:44:55 -07:00
|
|
|
z_power_states[_current_cpu->id].state = PM_STATE_ACTIVE;
|
2021-01-12 13:23:18 -08:00
|
|
|
return PM_STATE_ACTIVE;
|
2020-09-07 12:23:30 -04:00
|
|
|
}
|
2021-03-01 13:44:40 -08:00
|
|
|
#endif
|
2020-09-07 12:23:30 -04:00
|
|
|
|
2021-02-23 15:39:05 -08:00
|
|
|
enum pm_state pm_system_suspend(int32_t ticks)
|
2018-08-16 10:39:40 +05:30
|
|
|
{
|
2021-10-22 10:44:55 -07:00
|
|
|
uint8_t id = _current_cpu->id;
|
|
|
|
|
2021-05-13 09:49:18 -04:00
|
|
|
SYS_PORT_TRACING_FUNC_ENTER(pm, system_suspend, ticks);
|
2021-10-22 10:59:51 -07:00
|
|
|
z_power_states[id] = pm_policy_next_state(id, ticks);
|
2021-10-22 10:44:55 -07:00
|
|
|
if (z_power_states[id].state == PM_STATE_ACTIVE) {
|
2020-12-07 21:51:46 -08:00
|
|
|
LOG_DBG("No PM operations done.");
|
2021-10-22 10:44:55 -07:00
|
|
|
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend, ticks,
|
|
|
|
z_power_states[id].state);
|
|
|
|
return false;
|
2019-01-16 14:49:16 +01:00
|
|
|
}
|
2021-10-22 10:21:52 -07:00
|
|
|
post_ops_done = false;
|
2018-10-24 13:54:20 +05:30
|
|
|
|
2021-04-02 10:49:01 -07:00
|
|
|
if (ticks != K_TICKS_FOREVER) {
|
2021-04-19 19:36:10 -07:00
|
|
|
/*
|
|
|
|
* Just a sanity check in case the policy manager does not
|
|
|
|
* handle this error condition properly.
|
|
|
|
*/
|
2021-10-22 10:44:55 -07:00
|
|
|
__ASSERT(z_power_states[id].min_residency_us >=
|
|
|
|
z_power_states[id].exit_latency_us,
|
2021-04-19 19:36:10 -07:00
|
|
|
"min_residency_us < exit_latency_us");
|
|
|
|
|
2021-04-02 10:49:01 -07:00
|
|
|
/*
|
|
|
|
* We need to set the timer to interrupt a little bit early to
|
|
|
|
* accommodate the time required by the CPU to fully wake up.
|
|
|
|
*/
|
|
|
|
z_set_timeout_expiry(ticks -
|
2021-10-22 10:44:55 -07:00
|
|
|
k_us_to_ticks_ceil32(
|
|
|
|
z_power_states[id].exit_latency_us),
|
|
|
|
true);
|
2021-04-02 10:49:01 -07:00
|
|
|
}
|
|
|
|
|
2021-03-01 13:44:40 -08:00
|
|
|
#if CONFIG_PM_DEVICE
|
2021-09-28 10:54:49 -07:00
|
|
|
bool should_resume_devices = false;
|
2018-08-16 10:39:40 +05:30
|
|
|
|
2021-10-22 10:44:55 -07:00
|
|
|
if (z_power_states[id].state != PM_STATE_RUNTIME_IDLE) {
|
2021-03-01 13:44:40 -08:00
|
|
|
if (pm_suspend_devices()) {
|
2021-05-13 09:49:18 -04:00
|
|
|
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend,
|
2021-10-22 10:44:55 -07:00
|
|
|
ticks, _handle_device_abort(z_power_states[id]));
|
|
|
|
return _handle_device_abort(z_power_states[id]);
|
2018-08-16 10:39:40 +05:30
|
|
|
}
|
2021-09-28 10:54:49 -07:00
|
|
|
should_resume_devices = true;
|
2019-01-16 14:49:16 +01:00
|
|
|
}
|
2021-03-01 13:44:40 -08:00
|
|
|
#endif
|
2021-03-11 15:02:09 -08:00
|
|
|
/*
|
|
|
|
* This function runs with interruptions locked but it is
|
|
|
|
* expected the SoC to unlock them in
|
|
|
|
* pm_power_state_exit_post_ops() when returning to active
|
|
|
|
* state. We don't want to be scheduled out yet, first we need
|
|
|
|
* to send a notification about leaving the idle state. So,
|
|
|
|
* we lock the scheduler here and unlock just after we have
|
|
|
|
* sent the notification in pm_system_resume().
|
|
|
|
*/
|
|
|
|
k_sched_lock();
|
2021-10-12 08:52:30 -05:00
|
|
|
pm_start_timer();
|
2020-09-07 12:23:30 -04:00
|
|
|
/* Enter power state */
|
2021-02-26 15:29:52 -08:00
|
|
|
pm_state_notify(true);
|
2021-10-22 10:44:55 -07:00
|
|
|
pm_state_set(z_power_states[id]);
|
2021-10-12 08:52:30 -05:00
|
|
|
pm_stop_timer();
|
2018-08-16 10:39:40 +05:30
|
|
|
|
2020-09-07 12:23:30 -04:00
|
|
|
/* Wake up sequence starts here */
|
2020-09-01 18:31:40 -04:00
|
|
|
#if CONFIG_PM_DEVICE
|
2021-03-01 13:44:40 -08:00
|
|
|
if (should_resume_devices) {
|
2018-08-16 10:39:40 +05:30
|
|
|
/* Turn on peripherals and restore device states as necessary */
|
2020-09-01 21:46:30 -04:00
|
|
|
pm_resume_devices();
|
2018-08-16 10:39:40 +05:30
|
|
|
}
|
2019-01-29 12:14:17 +01:00
|
|
|
#endif
|
2021-10-22 10:44:55 -07:00
|
|
|
pm_stats_update(z_power_states[id].state);
|
2021-03-11 10:29:22 -08:00
|
|
|
pm_system_resume();
|
2021-03-24 14:05:03 +01:00
|
|
|
k_sched_unlock();
|
2021-10-22 10:44:55 -07:00
|
|
|
SYS_PORT_TRACING_FUNC_EXIT(pm, system_suspend, ticks,
|
|
|
|
z_power_states[id].state);
|
|
|
|
return z_power_states[id].state;
|
2018-08-16 10:39:40 +05:30
|
|
|
}
|
|
|
|
|
2020-10-14 21:59:51 -07:00
|
|
|
void pm_notifier_register(struct pm_notifier *notifier)
|
|
|
|
{
|
|
|
|
k_spinlock_key_t pm_notifier_key = k_spin_lock(&pm_notifier_lock);
|
|
|
|
|
|
|
|
sys_slist_append(&pm_notifiers, ¬ifier->_node);
|
|
|
|
k_spin_unlock(&pm_notifier_lock, pm_notifier_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pm_notifier_unregister(struct pm_notifier *notifier)
|
|
|
|
{
|
|
|
|
int ret = -EINVAL;
|
|
|
|
k_spinlock_key_t pm_notifier_key;
|
|
|
|
|
|
|
|
pm_notifier_key = k_spin_lock(&pm_notifier_lock);
|
|
|
|
if (sys_slist_find_and_remove(&pm_notifiers, &(notifier->_node))) {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
k_spin_unlock(&pm_notifier_lock, pm_notifier_key);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-27 12:38:39 -07:00
|
|
|
|
2021-10-29 16:11:16 -07:00
|
|
|
const struct pm_state_info pm_power_state_next_get(uint8_t cpu)
|
2021-09-27 12:38:39 -07:00
|
|
|
{
|
2021-10-29 16:11:16 -07:00
|
|
|
return z_power_states[cpu];
|
2021-09-27 12:38:39 -07:00
|
|
|
}
|