summaryrefslogtreecommitdiff
path: root/zephyr/subsys/ap_pwrseq/x86_non_dsx_common_pwrseq_host_sleep.c
blob: 2e16aba1bbed99dea577d397538f521f452f0dc1 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* 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 <ap_power_host_sleep.h>
#include <x86_non_dsx_common_pwrseq_sm_handler.h>

static uint16_t sleep_signal_timeout;
static uint16_t host_sleep_timeout_default = CONFIG_SLEEP_TIMEOUT_MS;
static uint32_t sleep_signal_transitions;
static enum sleep_hang_type timeout_hang_type;

static void sleep_transition_timeout(struct k_work *work);

static K_WORK_DELAYABLE_DEFINE(sleep_transition_timeout_data,
			       sleep_transition_timeout);

/**
 * Type of sleep hang detected
 */
enum sleep_hang_type {
	SLEEP_HANG_NONE,
	SLEEP_HANG_S0IX_SUSPEND,
	SLEEP_HANG_S0IX_RESUME
};

void power_chipset_handle_sleep_hang(enum sleep_hang_type hang_type)
{
	/*
	 * Wake up the AP so they don't just chill in a non-suspended state and
	 * burn power. Overload a vaguely related event bit since event bits are
	 * at a premium. If the system never entered S0ix, then manually set the
	 * wake mask to pretend it did, so that the hang detect event wakes the
	 * system.
	 */
#ifndef CONFIG_AP_PWRSEQ_DRIVER
	if (pwr_sm_get_state() == SYS_POWER_STATE_S0) {
		host_event_t sleep_wake_mask;

		ap_power_get_lazy_wake_mask(SYS_POWER_STATE_S0ix,
					    &sleep_wake_mask);
		lpc_set_host_event_mask(LPC_HOST_EVENT_WAKE, sleep_wake_mask);
	}
#else
	const struct device *dev = ap_pwrseq_get_instance();

	if (ap_pwrseq_get_current_state(dev) == AP_POWER_STATE_S0) {
		host_event_t sleep_wake_mask;

		ap_power_get_lazy_wake_mask(AP_POWER_STATE_S0IX,
					    &sleep_wake_mask);
		lpc_set_host_event_mask(LPC_HOST_EVENT_WAKE, sleep_wake_mask);
	}
#endif

	ccprintf("Warning: Detected sleep hang! Waking host up!");
	host_set_single_event(EC_HOST_EVENT_HANG_DETECT);
}

static void sleep_transition_timeout(struct k_work *work)
{
	/* Mark the timeout. */
	sleep_signal_transitions |= EC_HOST_RESUME_SLEEP_TIMEOUT;
	k_work_cancel_delayable(&sleep_transition_timeout_data);

	if (timeout_hang_type != SLEEP_HANG_NONE) {
		power_chipset_handle_sleep_hang(timeout_hang_type);
	}
}

static void sleep_increment_transition(void)
{
	if ((sleep_signal_transitions & EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK) <
	    EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK)
		sleep_signal_transitions += 1;
}

void sleep_suspend_transition(void)
{
	sleep_increment_transition();
	k_work_cancel_delayable(&sleep_transition_timeout_data);
}

void sleep_resume_transition(void)
{
	sleep_increment_transition();

	/*
	 * Start the timer again to ensure the AP doesn't get itself stuck in
	 * a state where it's no longer in a sleep state (S0ix/S3), but from
	 * the Linux perspective is still suspended. Perhaps a bug in the SoC-
	 * internal periodic housekeeping code might result in a situation
	 * like this.
	 */
	if (sleep_signal_timeout) {
		timeout_hang_type = SLEEP_HANG_S0IX_RESUME;
		k_work_schedule(&sleep_transition_timeout_data,
				K_MSEC(sleep_signal_timeout));
	}
}

void sleep_start_suspend(void)
{
	uint16_t timeout = host_get_sleep_timeout();

	sleep_signal_transitions = 0;

	/* Use 0xFFFF to disable the timeout */
	if (timeout == EC_HOST_SLEEP_TIMEOUT_INFINITE) {
		sleep_signal_timeout = 0;
		return;
	}

	/* Use zero internally to indicate host doesn't set timeout value;
	 * we will use default timeout.
	 */
	if (timeout == EC_HOST_SLEEP_TIMEOUT_DEFAULT) {
		timeout = host_sleep_timeout_default;
	}

	sleep_signal_timeout = timeout;
	timeout_hang_type = SLEEP_HANG_S0IX_SUSPEND;
	k_work_schedule(&sleep_transition_timeout_data, K_MSEC(timeout));
}

void sleep_complete_resume(void)
{
	/*
	 * Ensure we don't schedule another sleep_transition_timeout
	 * if the the HOST_SLEEP_EVENT_S0IX_RESUME message arrives before
	 * the CHIPSET task transitions to the POWER_S0ixS0 state.
	 */
	sleep_signal_timeout = 0;
	k_work_cancel_delayable(&sleep_transition_timeout_data);
	host_set_sleep_transitions(sleep_signal_transitions);
}

void sleep_reset_tracking(void)
{
	sleep_signal_transitions = 0;
	sleep_signal_timeout = 0;
	timeout_hang_type = SLEEP_HANG_NONE;
}

/*
 * s0ix event handler.
 */
static void ap_power_sleep_event_handler(struct ap_power_ev_callback *cb,
					 struct ap_power_ev_data data)
{
	switch (data.event) {
	case AP_POWER_S0IX_SUSPEND_START:
		sleep_start_suspend();
		break;
	case AP_POWER_S0IX_SUSPEND:
		sleep_suspend_transition();
		break;
	case AP_POWER_S0IX_RESUME:
		sleep_resume_transition();
		break;
	case AP_POWER_S0IX_RESUME_COMPLETE:
		sleep_complete_resume();
		break;
	case AP_POWER_S0IX_RESET_TRACKING:
		sleep_reset_tracking();
		break;
	default:
		break;
	}
}

/*
 * Registers callback for s0ix events.
 */
static int ap_power_sleep_s0ix_event(const struct device *unused)
{
	static struct ap_power_ev_callback cb;

	/*
	 * Register for all events.
	 */
	ap_power_ev_init_callback(
		&cb, ap_power_sleep_event_handler,
		AP_POWER_S0IX_SUSPEND_START | AP_POWER_S0IX_SUSPEND |
			AP_POWER_S0IX_RESUME | AP_POWER_S0IX_RESUME_COMPLETE |
			AP_POWER_S0IX_RESET_TRACKING);
	ap_power_ev_add_callback(&cb);
	return 0;
}
SYS_INIT(ap_power_sleep_s0ix_event, APPLICATION, 1);