blob: 96a7d1ee800c02549b66a394451d834289017222 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -05002 * Copyright (c) 2018-2023, 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
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -050018void (*sp_interrupt_handler[NUM_VINT_ID])(void);
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060019
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
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020033void announce_test_section_start(const char *test_sect_desc)
34{
35 INFO("========================================\n");
36 INFO("Starting %s tests\n", test_sect_desc);
37 INFO("========================================\n");
38}
39void announce_test_section_end(const char *test_sect_desc)
40{
41 INFO("========================================\n");
42 INFO("End of %s tests\n", test_sect_desc);
43 INFO("========================================\n");
44}
45
46void announce_test_start(const char *test_desc)
47{
48 INFO("[+] %s\n", test_desc);
49}
50
51void announce_test_end(const char *test_desc)
52{
J-Alves5aecd982020-06-11 10:25:33 +010053 INFO("Test \"%s\" end.\n", test_desc);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020054}
55
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050056uint64_t sp_sleep_elapsed_time(uint32_t ms)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020057{
Manish Pandeye5400572021-01-12 15:15:32 +000058 uint64_t timer_freq = read_cntfrq_el0();
59
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010060 VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020061
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010062 VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms);
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050063
64 uint64_t time1 = virtualcounter_read();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020065 volatile uint64_t time2 = time1;
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050066
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010067 while ((time2 - time1) < ((ms * timer_freq) / 1000U)) {
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050068 time2 = virtualcounter_read();
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020069 }
Madhukar Pappireddya09d5f72021-10-26 14:50:52 -050070
71 return ((time2 - time1) * 1000) / timer_freq;
72}
73
74void sp_sleep(uint32_t ms)
75{
76 (void)sp_sleep_elapsed_time(ms);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020077}
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060078
79void sp_handler_spin_lock_init(void)
80{
81 for (uint32_t i = 0; i < NUM_VINT_ID; i++) {
82 init_spinlock(&sp_handler_lock[i]);
83 }
84}
85
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -050086void sp_register_interrupt_handler(void (*handler)(void),
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060087 uint32_t interrupt_id)
88{
89 if (interrupt_id >= NUM_VINT_ID) {
90 ERROR("Cannot register handler for interrupt %u\n", interrupt_id);
91 panic();
92 }
93
94 spin_lock(&sp_handler_lock[interrupt_id]);
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -050095 sp_interrupt_handler[interrupt_id] = handler;
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -060096 spin_unlock(&sp_handler_lock[interrupt_id]);
97}
98
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -050099void sp_unregister_interrupt_handler(uint32_t interrupt_id)
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -0600100{
101 if (interrupt_id >= NUM_VINT_ID) {
102 ERROR("Cannot unregister handler for interrupt %u\n", interrupt_id);
103 panic();
104 }
105
106 spin_lock(&sp_handler_lock[interrupt_id]);
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500107 sp_interrupt_handler[interrupt_id] = NULL;
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -0600108 spin_unlock(&sp_handler_lock[interrupt_id]);
109}