blob: bb3e4866d6d2993355afd88b2913c5954eeaf80a [file] [log] [blame]
Manish V Badarkheb31bc752021-12-24 08:52:52 +00001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdlib.h>
8#include <arch.h>
9#include <arch_helpers.h>
10#include <debug.h>
11#include <test_helpers.h>
12#include <tftf_lib.h>
13
14typedef enum {
15 EXEC_WFIT = 0,
16 EXEC_WFET
17} exec_wfxt;
18
19#ifdef __aarch64__
20static test_result_t test_wfxt_inst(exec_wfxt val, uint64_t ms)
21{
22 __asm__ volatile(".arch armv8.7-a");
23 uint64_t timer_cnt1, timer_cnt2, feed_cnt;
24 uint64_t timer_freq = read_cntfrq_el0();
25 uint64_t ms_to_counts = ((ms * timer_freq) / 1000U);
26
27 timer_cnt1 = virtualcounter_read();
28 feed_cnt = timer_cnt1 + ms_to_counts;
29
30 if (val == EXEC_WFIT) {
31 wfit(feed_cnt);
32 } else {
33 wfet(feed_cnt);
34 }
35
36 timer_cnt2 = virtualcounter_read();
37
38 /* Lapsed time should be at least equal to sleep time */
39 if ((timer_cnt2 - timer_cnt1) >= ms_to_counts) {
40 return TEST_RESULT_SUCCESS;
41 } else {
42 /* unlikely ends up here */
43 uint64_t lapsed_ms = ((timer_cnt2 - timer_cnt1) * 1000) / timer_freq;
44
45 ERROR("Time elapsed: actual(%llu)ms vs requested(%llu)ms \n",
46 lapsed_ms, ms);
47 return TEST_RESULT_FAIL;
48 }
49}
50#endif /* __aarch64__ */
51
52test_result_t test_wfet_instruction(void)
53{
54 SKIP_TEST_IF_AARCH32();
55
56#ifdef __aarch64__
57 SKIP_TEST_IF_WFXT_NOT_SUPPORTED();
58
59 /*
60 * first invocation of wfe returns immediately clearing the event
61 * register
62 */
63 sevl();
64 wfe();
65
66 return test_wfxt_inst(EXEC_WFET, 10);
67#endif /* __aarch64__ */
68}
69
70test_result_t test_wfit_instruction(void)
71{
72 test_result_t ret;
73
74 SKIP_TEST_IF_AARCH32();
75
76#ifdef __aarch64__
77 SKIP_TEST_IF_WFXT_NOT_SUPPORTED();
78
79 /* disable irqs to run wfi till timeout */
80 disable_irq();
81
82 ret = test_wfxt_inst(EXEC_WFIT, 10);
83
84 /* enable irq back */
85 enable_irq();
86#endif /* __aarch64__ */
87
88 return ret;
89}