summaryrefslogtreecommitdiff
path: root/tests/runtime_services/standard_service/psci/api_tests/system_suspend/test_psci_system_suspend.c
blob: aedfdc57da8c803e43c422fbeb62d4a28d180163 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/*
 * Copyright (c) 2015-2016, 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 <arm_gic.h>
#include <assert.h>
#include <debug.h>
#include <events.h>
#include <gic_v2.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.h>
#include <tftf_lib.h>
#include <timer.h>

#define SUSPEND_TIME_3_SECS 3000
#define SUSPEND_TIME_10_SECS 10000
#define TEST_ITERATION_COUNT 0x5

/* Helper macro to verify if system suspend API is supported */
#define is_psci_sys_susp_supported(void)	\
			tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND64)

static unsigned int deepest_power_state;
static unsigned int test_target_node = PWR_DOMAIN_INIT;
static event_t cpu_ready[PLATFORM_CORE_COUNT];
static event_t sgi_received[PLATFORM_CORE_COUNT];
static event_t waitq[PLATFORM_CORE_COUNT];

static int wakeup_irq_rcvd[PLATFORM_CORE_COUNT];
static unsigned int sgi_handled[PLATFORM_CORE_COUNT];
static sgi_data_t sgi_data;
static int cpu_ref_count;

extern unsigned long __RO_START__;
#define TFTF_RO_START (unsigned long)(&__RO_START__)
extern unsigned long __RO_END__;
#define TFTF_RO_END (unsigned long)(&__RO_END__)

/*
 * Helper function to verify the system state is ready for system
 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
 */
static int is_sys_suspend_state_ready(void)
{
	unsigned long current_mpid = read_mpidr_el1() & MPID_MASK;
	unsigned int target_mpid, target_node;
	int aff_info;

	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);

		/* Skip current CPU, as it is powered on */
		if (target_mpid == current_mpid)
			continue;

		aff_info = tftf_psci_affinity_info(target_mpid, MPIDR_AFFLVL0);
		if (aff_info != PSCI_STATE_OFF)
			return 0;
	}

	return 1;
}

static int suspend_wakeup_handler(void *data)
{
	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());

	assert(wakeup_irq_rcvd[core_pos] == 0);
	wakeup_irq_rcvd[core_pos] = 1;

	return 0;
}

static int sgi_handler(void *data)
{
	unsigned int mpid = read_mpidr_el1() & MPID_MASK;
	unsigned int core_pos = platform_get_core_pos(mpid);

	sgi_data = *(sgi_data_t *) data;
	sgi_handled[core_pos] = 1;
	return 0;
}

/*
 * Iterate over all cores and issue system suspend
 * After returning from suspend, ensure that the core which entered
 * suspend resumed from suspend.
 */
static test_result_t sys_suspend_from_all_cores(void)
{
	unsigned long long my_mpid = read_mpidr_el1() & MPID_MASK, target_mpid;
	unsigned int core_pos = platform_get_core_pos(my_mpid);
	int psci_ret;

	/* Increment the count of CPUs in the test */
	cpu_ref_count++;
	dsbsy();

	while (!is_sys_suspend_state_ready())
		;

	wakeup_irq_rcvd[core_pos] = 0;

	/* Register timer handler */
	tftf_timer_register_handler(suspend_wakeup_handler);

	/* Program timer to fire after delay */
	tftf_program_timer(PLAT_SUSPEND_ENTRY_TIME);
	psci_ret = tftf_system_suspend();

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

	if (psci_ret != PSCI_E_SUCCESS) {
		tftf_testcase_printf(
				"Failed to suspend system from core"
				"%x ret: %x \n", core_pos, psci_ret);
		return TEST_RESULT_FAIL;
	}

	/* Unregister time handler */
	tftf_timer_unregister_handler();
	tftf_cancel_timer();

	/* Done with the suspend test. Decrement count */
	cpu_ref_count--;
	dsbsy();

	test_target_node = tftf_topology_next_cpu(test_target_node);
	if (test_target_node != PWR_DOMAIN_INIT) {
		target_mpid = tftf_get_mpidr_from_node(test_target_node);
		psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) sys_suspend_from_all_cores,
				0);
		if (psci_ret != PSCI_E_SUCCESS) {
			tftf_testcase_printf("Failed to power on CPU 0x%x (%d) \n",
				(unsigned int)target_mpid, psci_ret);
			return TEST_RESULT_FAIL;
		}

		/*
		 * Wait for the target CPU to enter the test. The TFTF framework
		 * requires more than one CPU to be in the test to detect that the
		 * test has not finished.
		 */
		while (!ACCESS(cpu_ref_count))
			;
	}

	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ Functionality test : Issue system suspend from all cores
 * sequentially. This test ensures that system suspend can be issued
 * from all cores and right core is resumed from system suspend.
 */
test_result_t test_system_suspend_from_all_cores(void)
{
	unsigned long long target_mpid, my_mpid;
	int psci_ret;

	test_target_node = PWR_DOMAIN_INIT;
	my_mpid = read_mpidr_el1() & MPID_MASK;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	SKIP_TEST_IF_LESS_THAN_N_CPUS(2);

	for (unsigned int i = 0; i < PLATFORM_CORE_COUNT; ++i)
		tftf_init_event(&cpu_ready[i]);

	test_target_node = tftf_topology_next_cpu(test_target_node);
	assert(test_target_node != PWR_DOMAIN_INIT);

	target_mpid = tftf_get_mpidr_from_node(test_target_node);
	if (target_mpid == my_mpid)
		return sys_suspend_from_all_cores();

	psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) sys_suspend_from_all_cores,
				0);
	if (psci_ret != PSCI_E_SUCCESS) {
		tftf_testcase_printf("Failed to power on CPU 0x%x (%d) \n",
				(unsigned int)target_mpid, psci_ret);
		return TEST_RESULT_FAIL;
	}

	/*
	 * Wait for the target CPU to enter the test. The TFTF framework
	 * requires more than one CPU to be in the test to detect that the
	 * test has not finished.
	 */
	while (!ACCESS(cpu_ref_count))
		;

	return TEST_RESULT_SUCCESS;
}

/*
 * Helper function to issue SYSTEM SUSPEND SMC with custom parameters.
 */
int sys_suspend_helper(uintptr_t entry_point_address,
		       u_register_t context_id)
{
	smc_args args = {
		SMC_PSCI_SYSTEM_SUSPEND64,
		(uintptr_t)entry_point_address,
		(u_register_t)context_id
	};
	smc_ret_values ret_vals;

	ret_vals = tftf_smc(&args);

	return ret_vals.ret0;
}

/*
 * Function to issue system suspend with invalid entry-point on all cores
 * sequentially.
 */
static test_result_t invalid_entrypoint_for_sys_suspend(void)
{
	unsigned long long target_mpid;
	int psci_ret;

	/* Increment the count of CPUs in the test */
	cpu_ref_count++;
	dsbsy();

	while (!is_sys_suspend_state_ready())
		;

	psci_ret = sys_suspend_helper((uintptr_t) 0x1, 0);
	if (psci_ret != PSCI_E_INVALID_ADDRESS) {
		tftf_testcase_printf("Test failed with invalid entry addr %x\n",
				psci_ret);
		return TEST_RESULT_FAIL;
	}

	/* Done with the suspend test. Decrement count */
	cpu_ref_count--;
	dsbsy();

	test_target_node = tftf_topology_next_cpu(test_target_node);
	if (test_target_node != PWR_DOMAIN_INIT) {
		target_mpid = tftf_get_mpidr_from_node(test_target_node);
		psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) invalid_entrypoint_for_sys_suspend,
				0);
		if (psci_ret != PSCI_E_SUCCESS) {
			tftf_testcase_printf("Failed to power on CPU 0x%x (%d) \n",
				(unsigned int)target_mpid, psci_ret);
			return TEST_RESULT_FAIL;
		}

		/*
		 * Wait for the target CPU to enter the test. The TFTF framework
		 * requires more than one CPU to be in the test to detect that the
		 * test has not finished.
		 */
		while (!ACCESS(cpu_ref_count))
			;
	}

	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ API test: Issue system suspend with invalid entrypoint on all
 * cores. It should return error.
 */
test_result_t test_system_suspend_invalid_entrypoint(void)
{
	unsigned long long target_mpid, my_mpid;
	int psci_ret;

	test_target_node = PWR_DOMAIN_INIT;
	my_mpid = read_mpidr_el1() & MPID_MASK;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	SKIP_TEST_IF_LESS_THAN_N_CPUS(2);

	for (unsigned int i = 0; i < PLATFORM_CORE_COUNT; ++i)
		tftf_init_event(&cpu_ready[i]);

	test_target_node = tftf_topology_next_cpu(test_target_node);
	assert(test_target_node != PWR_DOMAIN_INIT);

	target_mpid = tftf_get_mpidr_from_node(test_target_node);
	if (target_mpid == my_mpid)
		return invalid_entrypoint_for_sys_suspend();

	psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) invalid_entrypoint_for_sys_suspend,
				0);
	if (psci_ret != PSCI_E_SUCCESS) {
		tftf_testcase_printf("Failed to power on CPU 0x%x (%d) \n",
				(unsigned int)target_mpid, psci_ret);
		return TEST_RESULT_FAIL;
	}

	/*
	 * Wait for the target CPU to enter the test. The TFTF framework
	 * requires more than one CPU to be in the test to detect that the
	 * test has not finished.
	 */
	while (!ACCESS(cpu_ref_count))
		;

	return TEST_RESULT_SUCCESS;
}

/*
 * Function to test Non lead CPU response to SGIs after multiple invocations
 * of system suspend.
 */
static test_result_t non_lead_cpu_sgi_test(void)
{
	unsigned int mpid = read_mpidr_el1();
	unsigned int core_pos = platform_get_core_pos(mpid);
	const unsigned int sgi_id = IRQ_NS_SGI_0;
	int sgi_ret;

	/* Register the local IRQ handler for the SGI */
	sgi_ret = tftf_irq_register_handler(sgi_id, sgi_handler);
	if (sgi_ret != 0) {
		tftf_testcase_printf("Failed to register IRQ %u (%d)",
				sgi_id, sgi_ret);
		return TEST_RESULT_FAIL;
	}
	/* Enable SGI */
	tftf_irq_enable(sgi_id, GIC_HIGHEST_NS_PRIORITY);

	/* Signal to the lead CPU that we are ready to receive SGI */
	tftf_send_event(&cpu_ready[core_pos]);

	/* Wait for SGI */
	while (ACCESS(sgi_handled[core_pos]) == 0)
		;
	/* Send event to indicate reception of SGI */
	tftf_send_event(&sgi_received[core_pos]);

	/* Unregister SGI handler */
	tftf_irq_disable(sgi_id);
	tftf_irq_unregister_handler(sgi_id);
	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ Functionality test: Issue system suspend multiple times with
 * all non-lead cores as OFF. This test ensures invoking system suspend
 * multiple times on lead core does not have any issue.
 * Steps:
 *    - Register timer wakeup event and issue system suspend multiple
 *      times. Ensure that the system suspend succeeds.
 *    - Turn on all the non lead CPU and send SGIs to them to ensure that
 *      all the non lead CPU are responsive.
 */
test_result_t test_psci_sys_susp_multiple_iteration(void)
{
	unsigned int target_mpid, target_node;
	unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK;
	unsigned int core_pos = platform_get_core_pos(lead_mpid);
	const unsigned int sgi_id = IRQ_NS_SGI_0;
	test_result_t psci_ret;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	for (unsigned int i = 0; i < PLATFORM_CORE_COUNT; ++i) {
		tftf_init_event(&cpu_ready[i]);
		tftf_init_event(&sgi_received[i]);
	}

	/* Register timer handler */
	tftf_timer_register_handler(suspend_wakeup_handler);

	for (unsigned int i = 0; i < TEST_ITERATION_COUNT; i++) {
		wakeup_irq_rcvd[core_pos] = 0;

		/*
		 * Program the wakeup timer, this will serve as the wake-up event
		 * to come out of suspend state
		 */
		tftf_program_timer(PLAT_SUSPEND_ENTRY_TIME);

		/* Issue system suspend */
		psci_ret = tftf_system_suspend();

		while (!ACCESS(wakeup_irq_rcvd[core_pos]))
			;

		if (psci_ret != PSCI_E_SUCCESS) {
			tftf_testcase_printf("Failed with return value %x\n",
					psci_ret);
			return TEST_RESULT_FAIL;
		}
	}

	tftf_cancel_timer();
	/* Unregister timer handler */
	tftf_timer_unregister_handler();

	/* Turn on all cores after test to ensure all cores boot up*/
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);
		core_pos = platform_get_core_pos(target_mpid);

		if (target_mpid == lead_mpid)
			continue;

		psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) non_lead_cpu_sgi_test,
				0);
		if (psci_ret != PSCI_E_SUCCESS) {
			tftf_testcase_printf(
					"Failed to power on CPU 0x%x (%d)\n",
					target_mpid, psci_ret);
			return TEST_RESULT_FAIL;
		}
		tftf_wait_for_event(&cpu_ready[core_pos]);
	}

	/* Send SGI to all non lead CPUs and ensure that they receive it */
	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_send_sgi(sgi_id, core_pos);
		tftf_wait_for_event(&sgi_received[core_pos]);
	}

	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ Functionality test : Issue system suspend with pending
 * SGI on calling core. System suspend call should return prior to the
 * programmed wake-up interval.
 * Steps:
 *   - Mask the interrupts on lead CPU and send SGI to current CPU
 *   - Configure a wake-up timer and issue SYSTEM SUSPEND
 *   - Unmask the interrupt and verify that current CPU has woken
 *     prior to the wake-up timer firing.
 */
test_result_t test_psci_sys_susp_pending_irq(void)
{
	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());
	const unsigned int sgi_id = IRQ_NS_SGI_0;
	int sgi_ret;
	test_result_t psci_ret;
	test_result_t ret = TEST_RESULT_SUCCESS;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	/* Initialize variables */
	sgi_handled[core_pos] = 0;
	wakeup_irq_rcvd[core_pos] = 0;

	/* Register the local IRQ handler for the SGI */
	sgi_ret = tftf_irq_register_handler(sgi_id, sgi_handler);
	if (sgi_ret != 0) {
		tftf_testcase_printf("Failed to register IRQ %u (%d)",
				sgi_id, sgi_ret);
		return TEST_RESULT_FAIL;
	}

	/* Register for timer interrupt */
	tftf_timer_register_handler(suspend_wakeup_handler);

	/*
	 * Program the MB timer, for 3 secs to fire timer interrupt if
	 * system enters suspend state with pending IRQ
	 */
	tftf_program_timer(SUSPEND_TIME_3_SECS);

	tftf_irq_enable(sgi_id, GIC_HIGHEST_NS_PRIORITY);
	disable_irq();
	isb();

	/* Send the SGI to the lead CPU */
	tftf_send_sgi(sgi_id, core_pos);

	/* Check if system enters suspend state with pending IRQ or not */
	psci_ret = tftf_system_suspend();

	/* Un-mask the interrupt */
	enable_irq();
	isb();

	/*
	 * If the wake-up timer has fired, then the pending interrupt did
	 * not have any effect on the SYSTEM SUSPEND which means the
	 * test case failed.
	 *
	 */
	if (ACCESS(wakeup_irq_rcvd[core_pos])) {
		tftf_testcase_printf("Timer irq received\n");
		ret = TEST_RESULT_FAIL;
	}

	/* Wait for the SGI to be handled */
	while (ACCESS(sgi_handled[core_pos]) == 0)
		;

	/* Verify the sgi data received by the SGI handler */
	if (sgi_data.irq_id != sgi_id) {
		tftf_testcase_printf("Wrong IRQ ID, expected %u, got %u\n",
				sgi_id, sgi_data.irq_id);
		ret = TEST_RESULT_FAIL;
	}

	if (psci_ret != PSCI_E_SUCCESS)
		ret = TEST_RESULT_FAIL;

	/* Unregister timer handler */
	tftf_timer_unregister_handler();
	tftf_cancel_timer();

	/* Unregister SGI handler */
	tftf_irq_disable(sgi_id);
	tftf_irq_unregister_handler(sgi_id);

	return ret;
}

/* Helper function to calculate checksum of a given DRAM area  */
unsigned long check_data_integrity(unsigned int *addr, unsigned int size)
{
	unsigned int chksum = 0;
	unsigned int i;

	for (i = 0; i < (size/sizeof(unsigned int)); i++)
		chksum += *(addr + i);
	return chksum;
}

/*
 * @Test_Aim@ Functionality Test: Ensure that RAM contents are preserved on
 * resume from system suspend
 * Steps:
 *    - Write a known pattern to the DRAM and calculate the hash.
 *    - Configure wake-up timer and issue SYSTEM SUSPEND on lead CPU.
 *    - Recalculate the hash of the DRAM and compare it with the previous
 *      value. Both the hash values should match.
 *
 */
test_result_t test_psci_sys_susp_validate_ram(void)
{
	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());
	unsigned long prev_hash_val = 0;
	unsigned long present_hash_val = 0;
	unsigned int psci_ret;
	test_result_t ret = TEST_RESULT_SUCCESS;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	wakeup_irq_rcvd[core_pos] = 0;

	/* Check hash on known region of RAM before putting into suspend */
	prev_hash_val = check_data_integrity((unsigned int *)TFTF_RO_START,
						TFTF_RO_END - TFTF_RO_START);

	tftf_timer_register_handler(suspend_wakeup_handler);

	/* Program timer to fire interrupt after timer expires */
	tftf_program_timer(SUSPEND_TIME_10_SECS);

	/* Issue system suspend */
	psci_ret = tftf_system_suspend();
	while (!ACCESS(wakeup_irq_rcvd[core_pos]))
		;
	if (psci_ret == PSCI_E_SUCCESS) {
		/*
		 * Check hash on known region of RAM after returning
		 * from suspend
		 */
		present_hash_val = check_data_integrity(
				(unsigned int *)TFTF_RO_START,
				TFTF_RO_END - TFTF_RO_START);
		if (present_hash_val != prev_hash_val) {
			tftf_testcase_printf("ERROR: RAM data not retained \n");
			ret = TEST_RESULT_FAIL;
		}
	} else {
		tftf_testcase_printf("Failed: system suspend to RAM \n");
		ret = TEST_RESULT_FAIL;
	}

	/* Unregister timer handler */
	tftf_timer_unregister_handler();
	tftf_cancel_timer();

	return ret;
}

/* Helper function to get deepest power state */
static unsigned int get_deepest_power_state(void)
{
	unsigned int test_suspend_type;
	unsigned int suspend_state_id;
	unsigned int power_level, ret;
	unsigned int power_state = 0;
	unsigned int pstate_id_idx[PLAT_MAX_PWR_LEVEL + 1];

	INIT_PWR_LEVEL_INDEX(pstate_id_idx);
	do {
		tftf_set_next_state_id_idx(PLAT_MAX_PWR_LEVEL, pstate_id_idx);

		if (pstate_id_idx[0] == PWR_STATE_INIT_INDEX)
			break;

		ret = tftf_get_pstate_vars(&power_level,
				&test_suspend_type,
				&suspend_state_id,
				pstate_id_idx);
		if (ret)
			continue;

		power_state = tftf_make_psci_pstate(power_level,
				test_suspend_type,
				suspend_state_id);

	} while (1);

	return power_state;
}

/*
 * Suspend non-lead cores
 */
static test_result_t suspend_non_lead_cpu(void)
{
	unsigned long long mpid = read_mpidr_el1();
	unsigned int core_pos = platform_get_core_pos(mpid);
	int ret;

	tftf_irq_enable(IRQ_NS_SGI_0, GIC_HIGHEST_NS_PRIORITY);

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

	ret = tftf_cpu_suspend(deepest_power_state);
	tftf_irq_disable(IRQ_NS_SGI_0);

	if (ret) {
		ERROR(" CPU suspend failed with error %x\n", ret);
		return TEST_RESULT_FAIL;
	}

	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ API Test: Issue system suspend on a core while other
 * cores are in suspend. This test ensures that system suspend will
 * not be successful if cores other than core issuing suspend are not
 * in OFF state.
 * Steps:
 *    - Turn on non lead CPUs and suspend it to the deepest suspend
 *      power state.
 *    - Issue SYSTEM SUSPEND on primary CPU. The API should return
 *      error.
 *
 */
test_result_t test_psci_sys_susp_with_cores_in_suspend(void)
{
	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());
	unsigned int target_mpid, target_node;
	unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK;
	test_result_t psci_ret;
	test_result_t ret = TEST_RESULT_SUCCESS;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	for (unsigned int j = 0; j < PLATFORM_CORE_COUNT; j++)
		tftf_init_event(&cpu_ready[j]);

	wakeup_irq_rcvd[core_pos] = 0;
	deepest_power_state = get_deepest_power_state();

	/* Suspend all cores other than lead core */
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);

		if (target_mpid == lead_mpid)
			continue;

		/* Turn on the non lead CPU and suspend it. */
		psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) suspend_non_lead_cpu,
				0);
		if (psci_ret != PSCI_E_SUCCESS) {
			tftf_testcase_printf(
					"Failed to power on CPU 0x%x (%d)\n",
					(unsigned int)target_mpid, psci_ret);
			return TEST_RESULT_FAIL;
		}
	}

	/* 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]);
	}

	/* Wait for 10 ms to ensure all the secondaries have suspended */
	waitms(10);

	/* Register and program timer */
	tftf_timer_register_handler(suspend_wakeup_handler);
	tftf_program_timer(PLAT_SUSPEND_ENTRY_TIME);

	/* Issue system suspend when other cores are in suspend state */
	psci_ret = tftf_system_suspend();

	/* Wake all non-lead CPUs */
	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_send_sgi(IRQ_NS_SGI_0, core_pos);
	}

	/* Check return from value from system suspend API */
	if (psci_ret != PSCI_E_DENIED) {
		tftf_testcase_printf("Entered suspend with cores in suspend\n");
		ret = TEST_RESULT_FAIL;
	}
	/* Unregister and cancel timer */
	tftf_timer_unregister_handler();
	tftf_cancel_timer();

	return ret;
}

/*
 * This functions holds the CPU till a `waitq` event is received.
 */
static test_result_t cpu_waitq(void)
{
	unsigned int mpid = read_mpidr_el1();
	unsigned int core_pos = platform_get_core_pos(mpid);

	tftf_send_event(&cpu_ready[core_pos]);

	/* Wait for event from primary cpu */
	tftf_wait_for_event(&waitq[core_pos]);
	return TEST_RESULT_SUCCESS;
}

/*
 * @Test_Aim@ API TEST: Ensure that system suspend will not be successful
 * if cores other than core issuing suspend are in running state
 *
 * Steps :
 *    - Turn on multiple cores on the non lead cluster
 *    - Issue SYSTEM SUSPEND. The API should return error.
 */
test_result_t test_psci_sys_susp_with_cores_on(void)
{
	unsigned int lead_cluster = MPIDR_CLUSTER_ID(read_mpidr_el1());
	unsigned int core_pos;
	unsigned int target_mpid, target_node;
	test_result_t psci_ret;
	test_result_t ret = TEST_RESULT_SUCCESS;

	if ((is_psci_sys_susp_supported()) == PSCI_E_NOT_SUPPORTED) {
		tftf_testcase_printf("System suspend is not supported "
				"by the EL3 firmware\n");
		return TEST_RESULT_SKIPPED;
	}

	SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(2);

	for (unsigned int j = 0; j < PLATFORM_CORE_COUNT; j++) {
		tftf_init_event(&waitq[j]);
		tftf_init_event(&cpu_ready[j]);
		wakeup_irq_rcvd[j] = 0;
	}

	/* Turn on cores in non-lead cluster */
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);

		if (MPIDR_CLUSTER_ID(target_mpid) == lead_cluster)
			continue;

		psci_ret = tftf_cpu_on(target_mpid,
				(uintptr_t) cpu_waitq,
				0);
		if (psci_ret != PSCI_E_SUCCESS) {
			tftf_testcase_printf("Failed to power "
				"on CPU 0x%x (%d)\n",
				(unsigned int)target_mpid, psci_ret);
			return TEST_RESULT_FAIL;
		}

		core_pos = platform_get_core_pos(target_mpid);
		/* Ensure that the core has booted */
		tftf_wait_for_event(&cpu_ready[core_pos]);
	}

	/* Register timer handler */
	tftf_timer_register_handler(suspend_wakeup_handler);

	/* Program timer to fire after delay */
	tftf_program_timer(PLAT_SUSPEND_ENTRY_TIME);

	/* Issue system suspend with other cores in ON state */
	psci_ret = tftf_system_suspend();

	/* Send event to CPUs waiting for `waitq` event. */
	for_each_cpu(target_node) {
		target_mpid = tftf_get_mpidr_from_node(target_node);

		/* Skip lead cluster */
		if (MPIDR_CLUSTER_ID(target_mpid) == lead_cluster)
			continue;

		core_pos = platform_get_core_pos(target_mpid);
		tftf_send_event(&waitq[core_pos]);
	}

	/* Check return value from system suspend API */
	if (psci_ret != PSCI_E_DENIED) {
		tftf_testcase_printf("Test failed with return value: %x \n",
				psci_ret);
		ret = TEST_RESULT_FAIL;
	}

	tftf_timer_unregister_handler();
	tftf_cancel_timer();
	return ret;
}