Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
| 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-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame^] | 12 | #include <ffa_svc.h> |
| 13 | |
| 14 | #include "sp_helpers.h" |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 15 | |
| 16 | uintptr_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 | |
| 29 | void 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 | |
| 38 | void 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 | } |
| 44 | void 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 | |
| 51 | void announce_test_start(const char *test_desc) |
| 52 | { |
| 53 | INFO("[+] %s\n", test_desc); |
| 54 | } |
| 55 | |
| 56 | void announce_test_end(const char *test_desc) |
| 57 | { |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame^] | 58 | INFO("Test \"%s\" end.\n", test_desc); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 61 | void sp_sleep(uint32_t ms) |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 62 | { |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 63 | uint64_t timer_freq = mmio_read_32(SYS_CNT_CONTROL_BASE + CNTFID_OFF); |
| 64 | VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 65 | |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 66 | VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 67 | uint64_t time1 = mmio_read_64(SYS_CNT_READ_BASE); |
| 68 | volatile uint64_t time2 = time1; |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 69 | while ((time2 - time1) < ((ms * timer_freq) / 1000U)) { |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 70 | time2 = mmio_read_64(SYS_CNT_READ_BASE); |
| 71 | } |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 72 | } |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame^] | 73 | |
| 74 | /******************************************************************************* |
| 75 | * Hypervisor Calls Wrappers |
| 76 | ******************************************************************************/ |
| 77 | |
| 78 | ffa_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 | |
| 90 | ffa_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 | |
| 101 | void 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 | } |