summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/power_host_sleep/src/test_power_host_sleep.c
blob: 616951c55854cf03f5da00b938e112d9a9b9d1ad (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
308
309
310
/* 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 "ec_commands.h"
#include "hooks.h"
#include "host_command.h"
#include "power.h"
#include "test/drivers/test_mocks.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"

#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/ztest_assert.h>

#define ARBITRARY_SLEEP_TRANSITIONS 1

/*
 * TODO(b/253224061): Reorganize fakes by public interface
 */
/* Fake to allow full linking */
FAKE_VOID_FUNC(chipset_reset, enum chipset_shutdown_reason);
FAKE_VALUE_FUNC(enum power_state, power_chipset_init);
const struct power_signal_info power_signal_list[] = {};

FAKE_VOID_FUNC(power_chipset_handle_host_sleep_event, enum host_sleep_event,
	       struct host_sleep_event_context *);
FAKE_VOID_FUNC(power_chipset_handle_sleep_hang, enum sleep_hang_type);
FAKE_VOID_FUNC(power_board_handle_sleep_hang, enum sleep_hang_type);

/* Per-Test storage of host_sleep_event_context to validate argument values */
static struct host_sleep_event_context test_saved_context;

/* Test-specific custom fake */
static void _test_power_chipset_handle_host_sleep_event(
	enum host_sleep_event state, struct host_sleep_event_context *ctx)
{
	switch (state) {
	case HOST_SLEEP_EVENT_S0IX_RESUME:
	case HOST_SLEEP_EVENT_S3_RESUME:
		ctx->sleep_transitions = ARBITRARY_SLEEP_TRANSITIONS;
		break;

	case HOST_SLEEP_EVENT_S3_SUSPEND:
	case HOST_SLEEP_EVENT_S0IX_SUSPEND:
	case HOST_SLEEP_EVENT_S3_WAKEABLE_SUSPEND:
		break;
	}

	memcpy(&test_saved_context, ctx,
	       sizeof(struct host_sleep_event_context));
}

static void power_host_sleep_before_after(void *test_data)
{
	ARG_UNUSED(test_data);

	RESET_FAKE(power_chipset_handle_host_sleep_event);
	RESET_FAKE(power_chipset_handle_sleep_hang);
	RESET_FAKE(power_board_handle_sleep_hang);
	memset(&test_saved_context, 0, sizeof(struct host_sleep_event_context));

	sleep_reset_tracking();
}

ZTEST_USER(power_host_sleep, test_non_existent_sleep_event_v1__bad_event)
{
	struct ec_params_host_sleep_event_v1 p = {
		/* No such sleep event */
		.sleep_event = UINT8_MAX,
		/* Non-existent sleep event, so suspend params don't matter */
		.suspend_params = { 0 },
	};
	struct ec_response_host_sleep_event_v1 r;
	struct host_cmd_handler_args args;

	/* Clear garbage for verifiable value */
	r.resume_response.sleep_transitions = 0;

	power_chipset_handle_host_sleep_event_fake.custom_fake =
		_test_power_chipset_handle_host_sleep_event;

	zassert_ok(ec_cmd_host_sleep_event_v1(&args, &p, &r));
	zassert_equal(args.response_size, 0);
	zassert_equal(power_chipset_handle_host_sleep_event_fake.call_count, 1);
	zassert_equal(power_chipset_handle_host_sleep_event_fake.arg0_val,
		      p.sleep_event);

	/*
	 * Unknown host sleep events don't retrieve sleep transitions from
	 * chip-specific handler.
	 */
	zassert_equal(r.resume_response.sleep_transitions, 0);
}

ZTEST_USER(power_host_sleep, test_non_existent_sleep_event_v1__s3_suspend)
{
	struct ec_params_host_sleep_event_v1 p = {
		.sleep_event = HOST_SLEEP_EVENT_S3_SUSPEND,
	};
	struct ec_response_host_sleep_event_v1 r;
	struct host_cmd_handler_args args;

	/* Set m/lsb of uint16_t to check for type coercion errors */
	p.suspend_params.sleep_timeout_ms = BIT(15) + 1;

	power_chipset_handle_host_sleep_event_fake.custom_fake =
		_test_power_chipset_handle_host_sleep_event;

	zassert_ok(ec_cmd_host_sleep_event_v1(&args, &p, &r));
	zassert_equal(args.response_size, 0);
	zassert_equal(power_chipset_handle_host_sleep_event_fake.call_count, 1);
	zassert_equal(power_chipset_handle_host_sleep_event_fake.arg0_val,
		      p.sleep_event);

	/*
	 * Verify sleep timeout propagated to chip-specific handler to use.
	 */
	zassert_equal(test_saved_context.sleep_timeout_ms,
		      p.suspend_params.sleep_timeout_ms);
}

ZTEST_USER(power_host_sleep, test_non_existent_sleep_event_v1__s3_resume)
{
	struct ec_params_host_sleep_event_v1 p = {
		.sleep_event = HOST_SLEEP_EVENT_S3_RESUME,
	};
	struct ec_response_host_sleep_event_v1 r;
	struct host_cmd_handler_args args;

	power_chipset_handle_host_sleep_event_fake.custom_fake =
		_test_power_chipset_handle_host_sleep_event;

	zassert_ok(ec_cmd_host_sleep_event_v1(&args, &p, &r));
	zassert_equal(args.response_size, sizeof(r));
	zassert_equal(power_chipset_handle_host_sleep_event_fake.call_count, 1);
	zassert_equal(power_chipset_handle_host_sleep_event_fake.arg0_val,
		      p.sleep_event);

	/*
	 * Verify sleep context propagated from chip-specific handler.
	 */
	zassert_equal(r.resume_response.sleep_transitions,
		      ARBITRARY_SLEEP_TRANSITIONS);
}

ZTEST(power_host_sleep, test_sleep_start_suspend_custom_timeout)
{
	struct host_sleep_event_context context = {
		/* Arbitrary 5ms timeout */
		.sleep_timeout_ms = 5,
	};

	sleep_start_suspend(&context);
	/*
	 * Validate that function idempotent wrt calling chip-specific handlers
	 */
	sleep_start_suspend(&context);

	/* Verify handlers not called because timeout didn't occur yet */
	zassert_equal(power_chipset_handle_sleep_hang_fake.call_count, 0);
	zassert_equal(power_board_handle_sleep_hang_fake.call_count, 0);

	/* Allow timeout to occur */
	/*
	 * TODO(b/253284635): Why can't we just wait 5ms?
	 */
	k_sleep(K_SECONDS(1));

	/* Check timeout handlers fired only *once* after multiple calls */
	zassert_equal(power_chipset_handle_sleep_hang_fake.call_count, 1);
	zassert_equal(power_board_handle_sleep_hang_fake.call_count, 1);

	zassert_equal(power_chipset_handle_sleep_hang_fake.arg0_val,
		      SLEEP_HANG_S0IX_SUSPEND);
	zassert_equal(power_board_handle_sleep_hang_fake.arg0_val,
		      SLEEP_HANG_S0IX_SUSPEND);
}

ZTEST(power_host_sleep, test_sleep_start_suspend_default_timeout)
{
	struct host_sleep_event_context context = {
		.sleep_timeout_ms = EC_HOST_SLEEP_TIMEOUT_DEFAULT,
	};

	sleep_start_suspend(&context);

	/*
	 * TODO(b/253284635): Why can't we just wait CONFIG_SLEEP_TIMEOUT_MS?
	 */
	k_msleep(CONFIG_SLEEP_TIMEOUT_MS * 2);

	zassert_equal(power_chipset_handle_sleep_hang_fake.call_count, 1);
	zassert_equal(power_board_handle_sleep_hang_fake.call_count, 1);

	zassert_equal(power_chipset_handle_sleep_hang_fake.arg0_val,
		      SLEEP_HANG_S0IX_SUSPEND);
	zassert_equal(power_board_handle_sleep_hang_fake.arg0_val,
		      SLEEP_HANG_S0IX_SUSPEND);
}

ZTEST(power_host_sleep, test_sleep_start_suspend_infinite_timeout)
{
	struct host_sleep_event_context context = {
		.sleep_timeout_ms = EC_HOST_SLEEP_TIMEOUT_INFINITE,
	};

	sleep_start_suspend(&context);

	k_sleep(K_MSEC(CONFIG_SLEEP_TIMEOUT_MS * 2));

	/* Verify that default handlers were never called */
	zassert_equal(power_chipset_handle_sleep_hang_fake.call_count, 0);
	zassert_equal(power_board_handle_sleep_hang_fake.call_count, 0);
}

ZTEST(power_host_sleep, test_suspend_then_resume_with_timeout)
{
	struct host_sleep_event_context context = {
		.sleep_timeout_ms = EC_HOST_SLEEP_TIMEOUT_DEFAULT,
		.sleep_transitions = 0.
	};

	/* Start then suspend process with deferred hook call */
	sleep_start_suspend(&context);
	/* Register the suspend transition (cancels timeout hook) */
	sleep_suspend_transition();
	k_sleep(K_MSEC(CONFIG_SLEEP_TIMEOUT_MS * 2));

	/* No timeout hooks should've fired */
	zassert_equal(power_chipset_handle_sleep_hang_fake.call_count, 0);
	zassert_equal(power_board_handle_sleep_hang_fake.call_count, 0);

	/* Transition to resume state and wait for hang timeout */
	sleep_resume_transition();
	k_sleep(K_MSEC(CONFIG_SLEEP_TIMEOUT_MS * 2));

	/* Resume state transition timeout hook should've fired */
	zassert_equal(power_chipset_handle_sleep_hang_fake.call_count, 1);
	zassert_equal(power_board_handle_sleep_hang_fake.call_count, 1);
	zassert_equal(power_chipset_handle_sleep_hang_fake.arg0_val,
		      SLEEP_HANG_S0IX_RESUME);
	zassert_equal(power_board_handle_sleep_hang_fake.arg0_val,
		      SLEEP_HANG_S0IX_RESUME);

	/* Complete resume so we can inspect the state transitions */
	sleep_complete_resume(&context);

	/* Transitioned to suspend and then to resume state. */
	zassert_equal(context.sleep_transitions &
			      EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK,
		      2);
	/* There was a timeout */
	zassert_true(context.sleep_transitions & EC_HOST_RESUME_SLEEP_TIMEOUT);
}

/* Only used in test_sleep_set_notify */
static bool _test_host_sleep_hook_called;

static void _test_sleep_notify_hook(void)
{
	_test_host_sleep_hook_called = true;
}
DECLARE_HOOK(HOOK_TEST_1, _test_sleep_notify_hook, HOOK_PRIO_DEFAULT);

ZTEST(power_host_sleep, test_sleep_set_notify)
{
	/* Init as none */
	sleep_set_notify(SLEEP_NOTIFY_NONE);

	/* Verify hook may be notified for a specific NOTIFY state */
	_test_host_sleep_hook_called = false;
	sleep_set_notify(SLEEP_NOTIFY_SUSPEND);
	sleep_notify_transition(SLEEP_NOTIFY_SUSPEND, HOOK_TEST_1);
	k_sleep(K_SECONDS(1));

	zassert_true(_test_host_sleep_hook_called);

	/* Verify NOTIFY state is reset after firing hook */
	_test_host_sleep_hook_called = false;
	sleep_notify_transition(SLEEP_NOTIFY_SUSPEND, HOOK_TEST_1);
	k_sleep(K_SECONDS(1));

	zassert_false(_test_host_sleep_hook_called);

	/*
	 * Verify that SLEEP_NOTIFY_NONE is a potential hook state to fire
	 * TODO(b/253480505) Should this really be allowed?
	 */
	_test_host_sleep_hook_called = false;
	sleep_notify_transition(SLEEP_NOTIFY_NONE, HOOK_TEST_1);
	k_sleep(K_SECONDS(1));

	zassert_true(_test_host_sleep_hook_called);
}

ZTEST(power_host_sleep, test_set_get_host_sleep_state)
{
	power_set_host_sleep_state(HOST_SLEEP_EVENT_S3_RESUME);
	zassert_equal(power_get_host_sleep_state(), HOST_SLEEP_EVENT_S3_RESUME);

	power_set_host_sleep_state(HOST_SLEEP_EVENT_S0IX_RESUME);
	zassert_equal(power_get_host_sleep_state(),
		      HOST_SLEEP_EVENT_S0IX_RESUME);
}

ZTEST_SUITE(power_host_sleep, drivers_predicate_post_main, NULL,
	    power_host_sleep_before_after, power_host_sleep_before_after, NULL);