blob: 448084f1fa132797b2d53dbe2dfdb6d7e80673c5 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -06002 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
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
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060016spinlock_t sp_handler_lock[NUM_VINT_ID];
17
18void (*sp_interrupt_tail_end_handler[NUM_VINT_ID])(void);
19
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020020uintptr_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
33void 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
42void 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}
48void 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
55void announce_test_start(const char *test_desc)
56{
57 INFO("[+] %s\n", test_desc);
58}
59
60void announce_test_end(const char *test_desc)
61{
J-Alves5aecd982020-06-11 10:25:33 +010062 INFO("Test \"%s\" end.\n", test_desc);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020063}
64
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050065uint64_t sp_sleep_elapsed_time(uint32_t ms)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020066{
Manish Pandeye5400572021-01-12 15:15:32 +000067 uint64_t timer_freq = read_cntfrq_el0();
68
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010069 VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020070
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010071 VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms);
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050072
73 uint64_t time1 = virtualcounter_read();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020074 volatile uint64_t time2 = time1;
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050075
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010076 while ((time2 - time1) < ((ms * timer_freq) / 1000U)) {
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050077 time2 = virtualcounter_read();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020078 }
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050079
80 return ((time2 - time1) * 1000) / timer_freq;
81}
82
83void sp_sleep(uint32_t ms)
84{
85 (void)sp_sleep_elapsed_time(ms);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020086}
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060087
88void 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
95void 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
108void 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}