blob: a6b6bc5d9631c6abeaa5ec5c4dac9a0296da163d [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}