summaryrefslogtreecommitdiff
path: root/tests/runtime_services/standard_service/psci/api_tests/cpu_suspend/test_suspend.c
blob: f16eee382723cd44aefe69d7fd5ac633194cef0d (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arch.h>
#include <arch_helpers.h>
#include <assert.h>
#include <debug.h>
#include <events.h>
#include <irq.h>
#include <plat_topology.h>
#include <platform.h>
#include <platform_def.h>
#include <power_management.h>
#include <psci.h>
#include <sgi.h>
#include <tftf_lib.h>
#include <timer.h>

/*
 * Desired affinity level and state type (standby or powerdown) for the next
 * CPU_SUSPEND operation. We need these shared variables because there is no way
 * to pass arguments to non-lead CPUs...
 */
static unsigned int test_aff_level;
static unsigned int test_suspend_type;

static event_t cpu_ready[PLATFORM_CORE_COUNT];

/*
 * Variable used by the non-lead CPUs to tell the lead CPU they
 * were woken up by IRQ_WAKE_SGI
 */
static event_t event_received_wake_irq[PLATFORM_CORE_COUNT];

/* Variable used to confirm the CPU is woken up by IRQ_WAKE_SGI or Timer IRQ */
static volatile int requested_irq_received[PLATFORM_CORE_COUNT];

static int requested_irq_handler(void *data)
{
	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());
#if ENABLE_ASSERTIONS
	unsigned int irq_id = *(unsigned int *) data;
#endif

	assert(irq_id == IRQ_WAKE_SGI || irq_id == tftf_get_timer_irq());
	assert(requested_irq_received[core_pos] == 0);

	requested_irq_received[core_pos] = 1;

	return 0;
}

/*
 * Suspend the calling (non-lead) CPU.
 * 1) Program a wake-up event to come out of suspend state
 * 2) Suspend the CPU to the desired affinity level and power state (standby or
 *    powerdown)
 * 3) Report success/failure of the suspend operation
 */
static test_result_t suspend_non_lead_cpu(void)
{
	unsigned int mpid = read_mpidr_el1();
	unsigned int core_pos = platform_get_core_pos(mpid);
	uint32_t power_state, stateid;
	int rc, expected_return_val;
	u_register_t flags;

	tftf_timer_register_handler(requested_irq_handler);

	/* Tell the lead CPU that the calling CPU is about to suspend itself */
	tftf_send_event(&cpu_ready[core_pos]);

	/* IRQs need to be disabled prior to programming the timer */
	/* Preserve DAIF flags*/
	flags = read_daif();
	disable_irq();
	isb();

	rc = tftf_program_timer(PLAT_SUSPEND_ENTRY_TIME);
	if (rc != 0) {
		/* Restore previous DAIF flags */
		write_daif(flags);
		isb();
		ERROR("Timer programming failed with error %d\n", rc);
		return TEST_RESULT_FAIL;
	}

	expected_return_val = tftf_psci_make_composite_state_id(test_aff_level,
				   test_suspend_type, &stateid);

	/*
	 * Suspend the calling CPU to the desired affinity level and power state
	 */
	power_state =  tftf_make_psci_pstate(test_aff_level,
					     test_suspend_type,
					     stateid);
	rc = tftf_cpu_suspend(power_state);

	/* Restore previous DAIF flags */
	write_daif(flags);
	isb();

	/* Wait until the IRQ wake interrupt is received */
	while (!requested_irq_received[core_pos])
		;

	tftf_send_event(&event_received_wake_irq[core_pos]);
	tftf_timer_unregister_handler();

	if (rc == expected_return_val)
		return TEST_RESULT_SUCCESS;

	tftf_testcase_printf("Wrong value: expected %i, got %i\n",
					expected_return_val, rc);
	return TEST_RESULT_FAIL;
}

/*
 * CPU suspend test to the desired affinity level and power state
 *
 * 1) Power on all cores
 * 2) Each core registers a wake-up event to come out of suspend state
 * 3) Each core tries to enter suspend state
 *
 * The test is skipped if an error occurs during the bring-up of non-lead CPUs.
 */
static test_result_t test_psci_suspend(unsigned int aff_level,
				     unsigned int suspend_type)
{
	unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK;
	unsigned int target_mpid, target_node;
	unsigned int core_pos;
	uint32_t power_state, stateid;
	int rc, expected_return_val;
	u_register_t flags;

	if (aff_level > MPIDR_MAX_AFFLVL)
		return TEST_RESULT_SKIPPED;

	assert((suspend_type == PSTATE_TYPE_POWERDOWN) ||
	       (suspend_type == PSTATE_TYPE_STANDBY));

	/* Export these variables for the non-lead CPUs */
	test_aff_level = aff_level;
	test_suspend_type = suspend_type;

	/*
	 * All testcases in this file use the same cpu_ready[] array so it needs
	 * to be re-initialised each time.
	 */
	for (unsigned int i = 0; i < PLATFORM_CORE_COUNT; ++i) {
		tftf_init_event(&cpu_ready[i]);
		tftf_init_event(&event_received_wake_irq[i]);
		requested_irq_received[i] = 0;
	}
	/* Ensure the above writes are seen before any read */
	dmbsy();

	/*
	 * Preparation step: Power on all cores.
	 */
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);
		/* Skip lead CPU as it is already on */
		if (target_mpid == lead_mpid)
			continue;

		rc = tftf_cpu_on(target_mpid,
				(uintptr_t) suspend_non_lead_cpu,
				0);
		if (rc != PSCI_E_SUCCESS) {
			tftf_testcase_printf(
				"Failed to power on CPU 0x%x (%d)\n",
				target_mpid, rc);
			return TEST_RESULT_SKIPPED;
		}
	}

	/* Wait for all non-lead CPUs to be ready */
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);
		/* Skip lead CPU */
		if (target_mpid == lead_mpid)
			continue;

		core_pos = platform_get_core_pos(target_mpid);
		tftf_wait_for_event(&cpu_ready[core_pos]);
	}

	/* IRQs need to be disabled prior to programming the timer */
	/* Preserve DAIF flags*/
	flags = read_daif();
	disable_irq();
	isb();

	/*
	 * Program the timer, this will serve as the
	 * wake-up event to come out of suspend state.
	 */
	rc = tftf_program_timer(PLAT_SUSPEND_ENTRY_TIME);
	if (rc) {
		/* Restore previous DAIF flags */
		write_daif(flags);
		isb();
		ERROR("Timer programming failed with error %d\n", rc);
		return TEST_RESULT_FAIL;
	}

	expected_return_val = tftf_psci_make_composite_state_id(test_aff_level,
				   test_suspend_type, &stateid);

	/*
	 * Suspend the calling CPU to the desired affinity level and power state
	 */
	power_state = tftf_make_psci_pstate(test_aff_level,
					    test_suspend_type,
					    stateid);
	if (test_aff_level >= PSTATE_AFF_LVL_2)
		rc = tftf_cpu_suspend_save_sys_ctx(power_state);
	else
		rc = tftf_cpu_suspend(power_state);

	/* Restore previous DAIF flags */
	write_daif(flags);
	isb();

	/*
	 * Cancel the timer set up by lead CPU in case we have returned early
	 * due to invalid parameters or it will lead to spurious wake-up later.
	 */
	tftf_cancel_timer();

	/*
	 * Wait for all non-lead CPUs to receive IRQ_WAKE_SGI. This will also
	 * ensure that the lead CPU has received the system timer IRQ
	 * because SGI #IRQ_WAKE_SGI is sent only after that.
	 */
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);
		/* Skip lead CPU */
		if (target_mpid == lead_mpid)
			continue;

		core_pos = platform_get_core_pos(target_mpid);
		tftf_wait_for_event(&event_received_wake_irq[core_pos]);
	}

	if (rc == expected_return_val)
		return TEST_RESULT_SUCCESS;

	tftf_testcase_printf("Wrong value: expected %i, got %i\n",
					expected_return_val, rc);
	return TEST_RESULT_FAIL;
}

/*
 * @Test_Aim@ Suspend to powerdown state targeted at affinity level 0
 */
test_result_t test_psci_suspend_powerdown_level0(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_0, PSTATE_TYPE_POWERDOWN);
}

/*
 * @Test_Aim@ Suspend to standby state targeted at affinity level 0
 */
test_result_t test_psci_suspend_standby_level0(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_0, PSTATE_TYPE_STANDBY);
}

/*
 * @Test_Aim@ Suspend to powerdown state targeted at affinity level 1
 */
test_result_t test_psci_suspend_powerdown_level1(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_1, PSTATE_TYPE_POWERDOWN);
}

/*
 * @Test_Aim@ Suspend to standby state targeted at affinity level 1
 */
test_result_t test_psci_suspend_standby_level1(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_1, PSTATE_TYPE_STANDBY);
}

/*
 * @Test_Aim@ Suspend to powerdown state targeted at affinity level 2
 */
test_result_t test_psci_suspend_powerdown_level2(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_2, PSTATE_TYPE_POWERDOWN);
}

/*
 * @Test_Aim@ Suspend to standby state targeted at affinity level 2
 */
test_result_t test_psci_suspend_standby_level2(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_2, PSTATE_TYPE_STANDBY);
}

/*
 * @Test_Aim@ Suspend to powerdown state targeted at affinity level 3
 */
test_result_t test_psci_suspend_powerdown_level3(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_3, PSTATE_TYPE_POWERDOWN);
}

/*
 * @Test_Aim@ Suspend to standby state targeted at affinity level 3
 */
test_result_t test_psci_suspend_standby_level3(void)
{
	return test_psci_suspend(PSTATE_AFF_LVL_3, PSTATE_TYPE_STANDBY);
}