Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Madhukar Pappireddy | 7caaa4a | 2022-01-28 17:01:35 -0600 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 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 | |
Madhukar Pappireddy | 7caaa4a | 2022-01-28 17:01:35 -0600 | [diff] [blame] | 16 | spinlock_t sp_handler_lock[NUM_VINT_ID]; |
| 17 | |
| 18 | void (*sp_interrupt_tail_end_handler[NUM_VINT_ID])(void); |
| 19 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 20 | uintptr_t bound_rand(uintptr_t min, uintptr_t max) |
| 21 | { |
| 22 | /* |
| 23 | * This is not ideal as some numbers will never be generated because of |
| 24 | * the integer arithmetic rounding. |
| 25 | */ |
| 26 | return ((rand() * (UINT64_MAX/RAND_MAX)) % (max - min)) + min; |
| 27 | } |
| 28 | |
| 29 | /******************************************************************************* |
| 30 | * Test framework helpers |
| 31 | ******************************************************************************/ |
| 32 | |
| 33 | void expect(int expr, int expected) |
| 34 | { |
| 35 | if (expr != expected) { |
| 36 | ERROR("Expected value %i, got %i\n", expected, expr); |
| 37 | while (1) |
| 38 | continue; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void announce_test_section_start(const char *test_sect_desc) |
| 43 | { |
| 44 | INFO("========================================\n"); |
| 45 | INFO("Starting %s tests\n", test_sect_desc); |
| 46 | INFO("========================================\n"); |
| 47 | } |
| 48 | void announce_test_section_end(const char *test_sect_desc) |
| 49 | { |
| 50 | INFO("========================================\n"); |
| 51 | INFO("End of %s tests\n", test_sect_desc); |
| 52 | INFO("========================================\n"); |
| 53 | } |
| 54 | |
| 55 | void announce_test_start(const char *test_desc) |
| 56 | { |
| 57 | INFO("[+] %s\n", test_desc); |
| 58 | } |
| 59 | |
| 60 | void announce_test_end(const char *test_desc) |
| 61 | { |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame] | 62 | INFO("Test \"%s\" end.\n", test_desc); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Madhukar Pappireddy | a09d5f7 | 2021-10-26 14:50:52 -0500 | [diff] [blame] | 65 | uint64_t sp_sleep_elapsed_time(uint32_t ms) |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 66 | { |
Manish Pandey | e540057 | 2021-01-12 15:15:32 +0000 | [diff] [blame] | 67 | uint64_t timer_freq = read_cntfrq_el0(); |
| 68 | |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 69 | VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 70 | |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 71 | VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms); |
Madhukar Pappireddy | a09d5f7 | 2021-10-26 14:50:52 -0500 | [diff] [blame] | 72 | |
| 73 | uint64_t time1 = virtualcounter_read(); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 74 | volatile uint64_t time2 = time1; |
Madhukar Pappireddy | a09d5f7 | 2021-10-26 14:50:52 -0500 | [diff] [blame] | 75 | |
Antonio Nino Diaz | 2ac6f8f | 2018-07-02 09:04:07 +0100 | [diff] [blame] | 76 | while ((time2 - time1) < ((ms * timer_freq) / 1000U)) { |
Madhukar Pappireddy | a09d5f7 | 2021-10-26 14:50:52 -0500 | [diff] [blame] | 77 | time2 = virtualcounter_read(); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 78 | } |
Madhukar Pappireddy | a09d5f7 | 2021-10-26 14:50:52 -0500 | [diff] [blame] | 79 | |
| 80 | return ((time2 - time1) * 1000) / timer_freq; |
| 81 | } |
| 82 | |
| 83 | void sp_sleep(uint32_t ms) |
| 84 | { |
| 85 | (void)sp_sleep_elapsed_time(ms); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 86 | } |
Madhukar Pappireddy | 7caaa4a | 2022-01-28 17:01:35 -0600 | [diff] [blame] | 87 | |
| 88 | void sp_handler_spin_lock_init(void) |
| 89 | { |
| 90 | for (uint32_t i = 0; i < NUM_VINT_ID; i++) { |
| 91 | init_spinlock(&sp_handler_lock[i]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void sp_register_interrupt_tail_end_handler(void (*handler)(void), |
| 96 | uint32_t interrupt_id) |
| 97 | { |
| 98 | if (interrupt_id >= NUM_VINT_ID) { |
| 99 | ERROR("Cannot register handler for interrupt %u\n", interrupt_id); |
| 100 | panic(); |
| 101 | } |
| 102 | |
| 103 | spin_lock(&sp_handler_lock[interrupt_id]); |
| 104 | sp_interrupt_tail_end_handler[interrupt_id] = handler; |
| 105 | spin_unlock(&sp_handler_lock[interrupt_id]); |
| 106 | } |
| 107 | |
| 108 | void sp_unregister_interrupt_tail_end_handler(uint32_t interrupt_id) |
| 109 | { |
| 110 | if (interrupt_id >= NUM_VINT_ID) { |
| 111 | ERROR("Cannot unregister handler for interrupt %u\n", interrupt_id); |
| 112 | panic(); |
| 113 | } |
| 114 | |
| 115 | spin_lock(&sp_handler_lock[interrupt_id]); |
| 116 | sp_interrupt_tail_end_handler[interrupt_id] = NULL; |
| 117 | spin_unlock(&sp_handler_lock[interrupt_id]); |
| 118 | } |