summaryrefslogtreecommitdiff
path: root/zephyr/test/ap_power/src/events.c
blob: 2528d356f6abb192e8dfe4b48ca08dd4e034e7e5 (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
/* 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.
 */

/**
 * @file
 * @brief Unit Tests for AP power events
 */

#include "ap_power/ap_power.h"
#include "ap_power/ap_power_events.h"
#include "hooks.h"
#include "test_state.h"

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

/*
 * Structure passed to event listeners.
 */
struct events {
	struct ap_power_ev_callback cb;
	enum ap_power_events event;
	int count;
};

/*
 * Common handler.
 * Increment count, and store event received.
 */
static void ev_handler(struct ap_power_ev_callback *callback,
		       struct ap_power_ev_data data)
{
	struct events *ev = CONTAINER_OF(callback, struct events, cb);

	ev->count++;
	ev->event = data.event;
}

/**
 * @brief TestPurpose: Check registration
 *
 * @details
 * Validate that listeners can be registered, even multiple times
 *
 * Expected Results
 *  - Multiple registrations do not result in multiple calls.
 */
ZTEST(events, test_registration)
{
	static struct events cb;

	ap_power_ev_init_callback(&cb.cb, ev_handler, AP_POWER_RESET);
	ap_power_ev_add_callback(&cb.cb);
	ap_power_ev_send_callbacks(AP_POWER_RESET);
	zassert_equal(1, cb.count, "Callback not called");
	zassert_equal(AP_POWER_RESET, cb.event, "Wrong event");
	ap_power_ev_send_callbacks(AP_POWER_SUSPEND);
	zassert_equal(1, cb.count, "Callback called");

	ap_power_ev_remove_callback(&cb.cb);
	ap_power_ev_send_callbacks(AP_POWER_RESET);
	zassert_equal(1, cb.count, "Callback called");
	cb.count = 0; /* Reset to make it clear */
	cb.event = 0;
	/* Add it twice */
	ap_power_ev_add_callback(&cb.cb);
	ap_power_ev_add_callback(&cb.cb);
	ap_power_ev_send_callbacks(AP_POWER_RESET);
	zassert_equal(1, cb.count, "Callback not called");
	zassert_equal(AP_POWER_RESET, cb.event, "Wrong event");
	ap_power_ev_remove_callback(&cb.cb);
	/* Second remove should be no-op */
	ap_power_ev_remove_callback(&cb.cb);
}

/**
 * @brief TestPurpose: Verify reset callback from ESPI
 *
 * @details
 * Validate that the reset callback is sent with ESPI PLTRST#
 *
 * Expected Results
 *  - The AP_POWER_RESET event is sent
 */
ZTEST(events, test_pltrst)
{
	static struct events cb;
	const struct device *espi =
		DEVICE_DT_GET_ANY(zephyr_espi_emul_controller);

	zassert_not_null(espi, "Cannot get ESPI device");

	ap_power_ev_init_callback(&cb.cb, ev_handler, AP_POWER_RESET);
	ap_power_ev_add_callback(&cb.cb);

	emul_espi_host_send_vw(espi, ESPI_VWIRE_SIGNAL_PLTRST, 0);
	/*
	 * Since the event is being sent via a deferred function,
	 * wait for the deferral time.
	 */
	k_usleep(2 * 1000);
	zassert_equal(1, cb.count, "Callback not called");
	zassert_equal(AP_POWER_RESET, cb.event, "Wrong event");
}

/**
 * @brief TestPurpose: Check event mask changes
 *
 * @details
 * Validate that listeners adjust the event mask.
 *
 * Expected Results
 *  - Event mask changes are honoured
 */
ZTEST(events, test_event_mask)
{
	static struct events cb;

	ap_power_ev_init_callback(&cb.cb, ev_handler, 0);
	ap_power_ev_add_callback(&cb.cb);
	ap_power_ev_send_callbacks(AP_POWER_RESET);
	zassert_equal(0, cb.count, "Callback called");
	ap_power_ev_init_callback(&cb.cb, ev_handler, AP_POWER_RESET);

	ap_power_ev_send_callbacks(AP_POWER_RESET);
	zassert_equal(1, cb.count, "Callback not called");
	ap_power_ev_send_callbacks(AP_POWER_SUSPEND);
	zassert_equal(1, cb.count, "Callback called");

	/* Add interest in event */
	cb.count = 0;
	ap_power_ev_add_events(&cb.cb, AP_POWER_SUSPEND);
	ap_power_ev_send_callbacks(AP_POWER_RESET);
	zassert_equal(1, cb.count, "Callback not called");
	zassert_equal(AP_POWER_RESET, cb.event, "Wrong event");
	ap_power_ev_send_callbacks(AP_POWER_SUSPEND);
	zassert_equal(2, cb.count, "Callback not called");
	zassert_equal(AP_POWER_SUSPEND, cb.event, "Wrong event");

	ap_power_ev_remove_callback(&cb.cb);
}

static int count_hook_shutdown, count_hook_startup;

static void hook_shutdown(void)
{
	count_hook_shutdown++;
}
DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, hook_shutdown, HOOK_PRIO_DEFAULT);

static void hook_startup(void)
{
	count_hook_startup++;
}
DECLARE_HOOK(HOOK_CHIPSET_STARTUP, hook_startup, HOOK_PRIO_DEFAULT);

/**
 * @brief TestPurpose: Verify correct interconnection with hook framework.
 *
 * @details
 * Validate that events get passed back to the hook subsystem.
 *
 * Expected Results
 *  - Events originating from the AP power event API get delivered via hooks.
 */
ZTEST(events, test_hooks)
{
	count_hook_startup = count_hook_shutdown = 0;
	ap_power_ev_send_callbacks(AP_POWER_STARTUP);
	zassert_equal(0, count_hook_shutdown, "shutdown hook called");
	zassert_equal(1, count_hook_startup, "startup hook not called");
	zassert_equal(0, count_hook_shutdown,
		      "reset event, shutdown hook called");
	zassert_equal(1, count_hook_startup,
		      "reset event, startup hook called");
	ap_power_ev_send_callbacks(AP_POWER_SHUTDOWN);
	zassert_equal(1, count_hook_shutdown, "shutdown hook not called");
	zassert_equal(1, count_hook_startup, "startup hook called");
}

/**
 * @brief Test Suite: Verifies AP power notification functionality.
 */
ZTEST_SUITE(events, ap_power_predicate_post_main, NULL, NULL, NULL, NULL);