summaryrefslogtreecommitdiff
path: root/tests/performance_tests/smc_latencies.c
blob: 3317962d0cb174111d350eae89e378e078692166 (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
/*
 * Copyright (c) 2015, 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.
 */

/*
 * This file contains tests that measure the round trip latency of an SMC.
 * The SMC calls used are simple ones (PSCI_VERSION and the Standard Service
 * UID) that involve almost no handling on the EL3 firmware's side so that we
 * come close to measuring the overhead of the SMC itself.
 */

#include <arch_helpers.h>
#include <debug.h>
#include <psci.h>
#include <std_svc.h>
#include <string.h>
#include <tftf_lib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define ITERATIONS_CNT 1000
static unsigned long long raw_results[ITERATIONS_CNT];

/* Latency information in nano-seconds */
struct latency_info {
	unsigned long long min;
	unsigned long long max;
	unsigned long long avg;
};

static inline unsigned long long cycles_to_ns(unsigned long long cycles)
{
	unsigned long long freq = read_cntfrq_el0();
	return (cycles * 1000000000) / freq;
}

/*
 * Send the given SMC 'ITERATIONS_CNT' times, measure the time it takes to
 * return back from the SMC call each time, and gather some statistics across
 * the whole series.
 *
 * The statistics consist of:
 *  - minimum latency (i.e the shortest duration across the whole series);
 *  - maximum latency (i.e the longest duration across the whole series);
 *  - average latency.
 *
 * These statistics are stored in the latency_info structure whose address
 * is passed as an argument.
 *
 * This function also prints some additional, intermediate information, like the
 * number of cycles for each SMC and the average number of cycles for an SMC
 * round trip.
 */
static void test_measure_smc_latency(const smc64_args *smc_args,
				     struct latency_info *latency)
{
	unsigned long long cycles;
	unsigned long long min_cycles;
	unsigned long long max_cycles;
	unsigned long long avg_cycles;
	unsigned long long cycles_sum = 0;

	min_cycles = UINT64_MAX;
	max_cycles = 0;
	memset(raw_results, 0, sizeof(raw_results));

	for (unsigned int i = 0; i < ITERATIONS_CNT; ++i) {
		cycles = read_cntpct_el0();
		tftf_smc64(smc_args);
		cycles = read_cntpct_el0() - cycles;

		min_cycles = MIN(min_cycles, cycles);
		max_cycles = MAX(max_cycles, cycles);

		cycles_sum += cycles;

		raw_results[i] = cycles;
	}

	avg_cycles = cycles_sum / ITERATIONS_CNT;
	tftf_testcase_printf("Average number of cycles: %llu\n",
		(unsigned long long) avg_cycles);
	latency->min = cycles_to_ns(min_cycles);
	latency->max = cycles_to_ns(max_cycles);
	latency->avg = cycles_to_ns(avg_cycles);

	NOTICE("Raw results:\n");
	for (unsigned int i = 0; i < ITERATIONS_CNT; ++i) {
		NOTICE("%llu cycles\t%llu ns\n",
			raw_results[i], cycles_to_ns(raw_results[i]));
	}
}

/*
 * Measure the latency of the PSCI_VERSION SMC and print the result.
 * This test always succeed.
 */
test_result_t smc_psci_version_latency(void)
{
	struct latency_info latency;
	smc64_args args = { PSCI_VERSION };

	test_measure_smc_latency(&args, &latency);
	tftf_testcase_printf(
		"Average time: %llu ns (ranging from %llu to %llu)\n",
		(unsigned long long) latency.avg,
		(unsigned long long) latency.min,
		(unsigned long long) latency.max);

	return TEST_RESULT_SUCCESS;
}

/*
 * Measure the latency of the Standard Service Call UID SMC and print the
 * result.
 * This test always succeed.
 */
test_result_t smc_std_svc_call_uid_latency(void)
{
	struct latency_info latency;
	smc64_args args = { SMC_STD_SVC_UID };

	test_measure_smc_latency(&args, &latency);
	tftf_testcase_printf(
		"Average time: %llu ns (ranging from %llu to %llu)\n",
		(unsigned long long) latency.avg,
		(unsigned long long) latency.min,
		(unsigned long long) latency.max);

	return TEST_RESULT_SUCCESS;
}