blob: 5d76dc8e0b38925f9b33807927724b265f9b3057 [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{
Manish Pandeye5400572021-01-12 15:15:32 +000063 uint64_t timer_freq = read_cntfrq_el0();
64
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010065 VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020066
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010067 VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms);
Manish Pandeye5400572021-01-12 15:15:32 +000068 uint64_t time1 = read_cntvct_el0();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020069 volatile uint64_t time2 = time1;
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010070 while ((time2 - time1) < ((ms * timer_freq) / 1000U)) {
Manish Pandeye5400572021-01-12 15:15:32 +000071 time2 = read_cntvct_el0();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020072 }
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020073}
J-Alves5aecd982020-06-11 10:25:33 +010074
75/*******************************************************************************
76 * Hypervisor Calls Wrappers
77 ******************************************************************************/
78
Manish Pandey6b3840a2020-09-15 22:31:58 +010079ffa_int_id_t spm_interrupt_get(void)
80{
81 hvc_args args = {
82 .fid = SPM_INTERRUPT_GET
83 };
84
85 hvc_ret_values ret = tftf_hvc(&args);
86
87 return ret.ret0;
88}
89
J-Alves5aecd982020-06-11 10:25:33 +010090void spm_debug_log(char c)
91{
92 hvc_args args = {
93 .fid = SPM_DEBUG_LOG,
94 .arg1 = c
95 };
96
97 (void)tftf_hvc(&args);
98}