summaryrefslogtreecommitdiff
path: root/zephyr/test/ap_power/src/ap_pwrseq.c
blob: a968b0c38d1ce6b4fe14208b179852349bd57163 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* 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/ap_power.h"
#include "ap_power/ap_power_interface.h"
#include "chipset.h"
#include "ec_commands.h"
#include "emul/emul_power_signals.h"
#include "host_command.h"
#include "test_mocks.h"
#include "test_state.h"

#include <zephyr/drivers/espi.h>
#include <zephyr/drivers/espi_emul.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/ztest.h>

#include <ap_power/ap_pwrseq.h>

static struct ap_power_ev_callback test_cb;
static int power_resume_count;
static int power_start_up_count;
static int power_hard_off_count;
static int power_shutdown_count;
static int power_shutdown_complete_count;
static int power_suspend_count;

static void emul_ev_handler(struct ap_power_ev_callback *callback,
			    struct ap_power_ev_data data)
{
	switch (data.event) {
	case AP_POWER_RESUME:
		power_resume_count++;
		break;

	case AP_POWER_STARTUP:
		power_start_up_count++;
		break;

	case AP_POWER_HARD_OFF:
		power_hard_off_count++;
		break;

	case AP_POWER_SHUTDOWN:
		power_shutdown_count++;
		break;

	case AP_POWER_SHUTDOWN_COMPLETE:
		power_shutdown_complete_count++;
		break;

	case AP_POWER_SUSPEND:
		power_suspend_count++;
		break;
	default:
		break;
	};
}

static void ap_pwrseq_reset_ev_counters(void)
{
	power_resume_count = 0;
	power_start_up_count = 0;
	power_hard_off_count = 0;
	power_shutdown_count = 0;
	power_shutdown_complete_count = 0;
	power_suspend_count = 0;
}

ZTEST(ap_pwrseq, test_ap_pwrseq_0)
{
	zassert_equal(0,
		      power_signal_emul_load(
			      EMUL_POWER_SIGNAL_TEST_PLATFORM(tp_sys_g3_to_s0)),
		      "Unable to load test platform `tp_sys_g3_to_s0`");

	k_msleep(500);

	zassert_equal(1, power_start_up_count,
		      "AP_POWER_STARTUP event not generated");
	zassert_equal(1, power_resume_count,
		      "AP_POWER_RESUME event not generated");
}

ZTEST(ap_pwrseq, test_ap_pwrseq_0_sleep)
{
	struct ec_params_host_sleep_event_v1 host_sleep_ev_p = {
		.sleep_event = HOST_SLEEP_EVENT_S0IX_SUSPEND,
		.suspend_params = { EC_HOST_SLEEP_TIMEOUT_DEFAULT },
	};
	struct ec_response_host_sleep_event_v1 host_sleep_ev_r;
	struct host_cmd_handler_args host_sleep_ev_args = BUILD_HOST_COMMAND(
		EC_CMD_HOST_SLEEP_EVENT, 1, host_sleep_ev_r, host_sleep_ev_p);

	struct ec_params_s0ix_cnt s0ix_cnt_ev_p = {
		.flags = EC_S0IX_COUNTER_RESET
	};
	struct ec_response_s0ix_cnt s0ix_cnt_ev_r;
	struct host_cmd_handler_args s0ix_cnt_ev_args = BUILD_HOST_COMMAND(
		EC_CMD_GET_S0IX_COUNTER, 0, s0ix_cnt_ev_r, s0ix_cnt_ev_p);

	/* Verify that counter is set to 0 */
	zassert_ok(host_command_process(&s0ix_cnt_ev_args),
		   "Failed to get s0ix counter");
	zassert_equal(s0ix_cnt_ev_r.s0ix_counter, 0);

	/* Send host sleep event */
	zassert_ok(host_command_process(&host_sleep_ev_args));

	/* Assert SLP_S0# */
	zassert_equal(0,
		      power_signal_emul_load(
			      EMUL_POWER_SIGNAL_TEST_PLATFORM(tp_sys_sleep)),
		      "Unable to load test platform `tp_sys_g3_to_s0`");

	k_msleep(500);

	/*
	 * Verify that counter has been increased,
	 * clear the flag for get command
	 */
	s0ix_cnt_ev_p.flags = 0;
	zassert_ok(host_command_process(&s0ix_cnt_ev_args),
		   "Failed to get s0ix counter");
	zassert_equal(s0ix_cnt_ev_r.s0ix_counter, 1);
}

ZTEST(ap_pwrseq, test_ap_pwrseq_0_wake)
{
	struct ec_params_host_sleep_event_v1 p = {
		.sleep_event = HOST_SLEEP_EVENT_S0IX_RESUME,
		.suspend_params = { EC_HOST_SLEEP_TIMEOUT_DEFAULT },
	};
	struct ec_response_host_sleep_event_v1 r;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_HOST_SLEEP_EVENT, 1, r, p);
	struct ec_params_s0ix_cnt s0ix_cnt_ev_p = { .flags = 0 };
	struct ec_response_s0ix_cnt s0ix_cnt_ev_r;
	struct host_cmd_handler_args s0ix_cnt_ev_args = BUILD_HOST_COMMAND(
		EC_CMD_GET_S0IX_COUNTER, 0, s0ix_cnt_ev_r, s0ix_cnt_ev_p);

	/* Confirm that counters keeps the same value through wakeup */
	zassert_ok(host_command_process(&s0ix_cnt_ev_args),
		   "Failed to get s0ix counter");
	zassert_equal(s0ix_cnt_ev_r.s0ix_counter, 1);

	zassert_equal(0,
		      power_signal_emul_load(
			      EMUL_POWER_SIGNAL_TEST_PLATFORM(tp_sys_wake)),
		      "Unable to load test platform `tp_sys_g3_to_s0`");

	k_msleep(500);
	zassert_ok(host_command_process(&args));
	zassert_ok(host_command_process(&s0ix_cnt_ev_args),
		   "Failed to get sleep counter");
	zassert_equal(s0ix_cnt_ev_r.s0ix_counter, 1);

	/* Verify the reset command sets the counter to zero */
	s0ix_cnt_ev_p.flags = EC_S0IX_COUNTER_RESET;
	zassert_ok(host_command_process(&s0ix_cnt_ev_args),
		   "Failed to get s0ix counter");

	s0ix_cnt_ev_p.flags = 0;
	zassert_ok(host_command_process(&s0ix_cnt_ev_args),
		   "Failed to get s0ix counter");
	zassert_equal(s0ix_cnt_ev_r.s0ix_counter, 0);
}

ZTEST(ap_pwrseq, test_ap_pwrseq_1)
{
	zassert_equal(0,
		      power_signal_emul_load(EMUL_POWER_SIGNAL_TEST_PLATFORM(
			      tp_sys_s0_power_fail)),
		      "Unable to load test platform `tp_sys_s0_power_fail`");

	/*
	 * Once emulated power signals are loaded, we need to wake AP power
	 * Sequence thread up to start executing new set of power signals
	 */
	ap_pwrseq_wake();
	k_msleep(500);
	zassert_equal(1, power_shutdown_count,
		      "AP_POWER_SHUTDOWN event not generated");
	zassert_equal(1, power_shutdown_complete_count,
		      "AP_POWER_SHUTDOWN_COMPLETE event not generated");
	zassert_equal(0, power_suspend_count,
		      "AP_POWER_SUSPEND event generated");
}

ZTEST(ap_pwrseq, test_ap_pwrseq_2)
{
	zassert_equal(
		0,
		power_signal_emul_load(EMUL_POWER_SIGNAL_TEST_PLATFORM(
			tp_sys_g3_to_s0_power_down)),
		"Unable to load test platform `tp_sys_g3_to_s0_power_down`");

	ap_power_exit_hardoff();
	k_msleep(2000);
	zassert_equal(2, power_shutdown_count,
		      "AP_POWER_SHUTDOWN event not generated");
	zassert_equal(2, power_shutdown_complete_count,
		      "AP_POWER_SHUTDOWN_COMPLETE event not generated");
	zassert_equal(1, power_suspend_count,
		      "AP_POWER_SUSPEND event not generated");
	zassert_equal(1, power_hard_off_count,
		      "AP_POWER_HARD_OFF event not generated");
}

ZTEST(ap_pwrseq, test_ap_pwrseq_3)
{
	zassert_equal(0,
		      power_signal_emul_load(EMUL_POWER_SIGNAL_TEST_PLATFORM(
			      tp_sys_s5_slp_sus_fail)),
		      "Unable to load test platform `tp_sys_s5_slp_sus_fail`");

	ap_power_exit_hardoff();
	k_msleep(500);

	zassert_equal(1, power_hard_off_count,
		      "AP_POWER_HARD_OFF event not generated");
}

ZTEST(ap_pwrseq, test_ap_pwrseq_4)
{
	zassert_equal(
		0,
		power_signal_emul_load(EMUL_POWER_SIGNAL_TEST_PLATFORM(
			tp_sys_s4_dsw_pwrok_fail)),
		"Unable to load test platform `tp_sys_s4_dsw_pwrok_fail`");

	ap_power_exit_hardoff();
	k_msleep(500);

	zassert_equal(0, power_hard_off_count,
		      "AP_POWER_HARD_OFF event generated");
	zassert_equal(1, power_shutdown_count,
		      "AP_POWER_SHUTDOWN event not generated");
	zassert_equal(1, power_shutdown_complete_count,
		      "AP_POWER_SHUTDOWN_COMPLETE event not generated");
}

ZTEST(ap_pwrseq, test_ap_pwrseq_5)
{
	zassert_equal(
		0,
		power_signal_emul_load(EMUL_POWER_SIGNAL_TEST_PLATFORM(
			tp_sys_s3_dsw_pwrok_fail)),
		"Unable to load test platform `tp_sys_s3_dsw_pwrok_fail`");

	ap_power_exit_hardoff();
	k_msleep(500);

	zassert_equal(0, power_hard_off_count,
		      "AP_POWER_HARD_OFF event generated");
	zassert_equal(1, power_shutdown_count,
		      "AP_POWER_SHUTDOWN event not generated");
	zassert_equal(1, power_shutdown_complete_count,
		      "AP_POWER_SHUTDOWN_COMPLETE event not generated");
}

ZTEST(ap_pwrseq, test_insufficient_power_blocks_s5)
{
	zassert_equal(0,
		      power_signal_emul_load(
			      EMUL_POWER_SIGNAL_TEST_PLATFORM(tp_sys_g3_to_s0)),
		      "Unable to load test platform `tp_sys_g3_to_s0`");
	system_can_boot_ap_fake.return_val = 0;

	ap_power_exit_hardoff();
	k_msleep(5000);

	zassert_equal(40, system_can_boot_ap_fake.call_count);
	zassert_true(
		chipset_in_or_transitioning_to_state(CHIPSET_STATE_HARD_OFF));
}

void ap_pwrseq_after_test(void *data)
{
	power_signal_emul_unload();
	ap_pwrseq_reset_ev_counters();
}

void *ap_pwrseq_setup_suite(void)
{
	ap_power_ev_init_callback(&test_cb, emul_ev_handler,
				  AP_POWER_RESUME | AP_POWER_STARTUP |
					  AP_POWER_HARD_OFF | AP_POWER_SUSPEND |
					  AP_POWER_SHUTDOWN |
					  AP_POWER_SHUTDOWN_COMPLETE);

	ap_power_ev_add_callback(&test_cb);

	return NULL;
}

void ap_pwrseq_teardown_suite(void *data)
{
	ap_power_ev_remove_callback(&test_cb);
}

ZTEST_SUITE(ap_pwrseq, ap_power_predicate_post_main, ap_pwrseq_setup_suite,
	    NULL, ap_pwrseq_after_test, NULL);