blob: 7dc164847226921079396b9e01a5f8bb0f95ad1e [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <debug.h>
8#include <mmio.h>
9#include <platform_def.h>
10#include <stdint.h>
11#include <stdlib.h>
J-Alves5aecd982020-06-11 10:25:33 +010012#include <ffa_svc.h>
13
14#include "sp_helpers.h"
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020015
16uintptr_t bound_rand(uintptr_t min, uintptr_t max)
17{
18 /*
19 * This is not ideal as some numbers will never be generated because of
20 * the integer arithmetic rounding.
21 */
22 return ((rand() * (UINT64_MAX/RAND_MAX)) % (max - min)) + min;
23}
24
25/*******************************************************************************
26 * Test framework helpers
27 ******************************************************************************/
28
29void expect(int expr, int expected)
30{
31 if (expr != expected) {
32 ERROR("Expected value %i, got %i\n", expected, expr);
33 while (1)
34 continue;
35 }
36}
37
38void announce_test_section_start(const char *test_sect_desc)
39{
40 INFO("========================================\n");
41 INFO("Starting %s tests\n", test_sect_desc);
42 INFO("========================================\n");
43}
44void announce_test_section_end(const char *test_sect_desc)
45{
46 INFO("========================================\n");
47 INFO("End of %s tests\n", test_sect_desc);
48 INFO("========================================\n");
49}
50
51void announce_test_start(const char *test_desc)
52{
53 INFO("[+] %s\n", test_desc);
54}
55
56void announce_test_end(const char *test_desc)
57{
J-Alves5aecd982020-06-11 10:25:33 +010058 INFO("Test \"%s\" end.\n", test_desc);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020059}
60
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010061void sp_sleep(uint32_t ms)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020062{
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010063 uint64_t timer_freq = mmio_read_32(SYS_CNT_CONTROL_BASE + CNTFID_OFF);
64 VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020065
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010066 VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020067 uint64_t time1 = mmio_read_64(SYS_CNT_READ_BASE);
68 volatile uint64_t time2 = time1;
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010069 while ((time2 - time1) < ((ms * timer_freq) / 1000U)) {
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020070 time2 = mmio_read_64(SYS_CNT_READ_BASE);
71 }
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020072}
J-Alves5aecd982020-06-11 10:25:33 +010073
74/*******************************************************************************
75 * Hypervisor Calls Wrappers
76 ******************************************************************************/
77
78ffa_vcpu_count_t spm_vcpu_get_count(ffa_vm_id_t vm_id)
79{
80 hvc_args args = {
81 .fid = SPM_VCPU_GET_COUNT,
82 .arg1 = vm_id
83 };
84
85 hvc_ret_values ret = tftf_hvc(&args);
86
87 return ret.ret0;
88}
89
90ffa_vm_count_t spm_vm_get_count(void)
91{
92 hvc_args args = {
93 .fid = SPM_VM_GET_COUNT
94 };
95
96 hvc_ret_values ret = tftf_hvc(&args);
97
98 return ret.ret0;
99}
100
101void spm_debug_log(char c)
102{
103 hvc_args args = {
104 .fid = SPM_DEBUG_LOG,
105 .arg1 = c
106 };
107
108 (void)tftf_hvc(&args);
109}