blob: 1887164fb429d34e10e3b218631a89698d20f879 [file] [log] [blame]
Juan Pablo Conde88ffad22024-10-11 21:22:29 -05001/*
Javier Almansa Sobrino82cd82e2025-01-17 17:37:42 +00002 * Copyright (c) 2024-2025, Arm Limited. All rights reserved.
Juan Pablo Conde88ffad22024-10-11 21:22:29 -05003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Javier Almansa Sobrino82cd82e2025-01-17 17:37:42 +00007#include <debug.h>
Juan Pablo Conde88ffad22024-10-11 21:22:29 -05008#include <stdlib.h>
Javier Almansa Sobrino82cd82e2025-01-17 17:37:42 +00009
Shruti Gupta91105082024-11-27 05:29:55 +000010#include <realm_helpers.h>
11#include <realm_psi.h>
12#include <realm_rsi.h>
13#include <smccc.h>
Juan Pablo Conde88ffad22024-10-11 21:22:29 -050014
Javier Almansa Sobrino82cd82e2025-01-17 17:37:42 +000015static unsigned int volatile realm_got_undef_abort;
16
Juan Pablo Conde88ffad22024-10-11 21:22:29 -050017/* Generate 64-bit random number */
18unsigned long long realm_rand64(void)
19{
20 return ((unsigned long long)rand() << 32) | rand();
21}
22
Shruti Gupta91105082024-11-27 05:29:55 +000023/* This function will call the Host to request IPA of the NS shared buffer */
24u_register_t realm_get_ns_buffer(void)
25{
26 smc_ret_values res = {};
27 struct rsi_host_call host_cal __aligned(sizeof(struct rsi_host_call));
28
29 host_cal.imm = HOST_CALL_GET_SHARED_BUFF_CMD;
30 res = tftf_smc(&(smc_args) {RSI_HOST_CALL, (u_register_t)&host_cal,
31 0UL, 0UL, 0UL, 0UL, 0UL, 0UL});
32
33 if (res.ret0 != RSI_SUCCESS) {
34 /* retry with PSI */
35 hvc_ret_values ret = tftf_hvc(&(hvc_args) {PSI_CALL_GET_SHARED_BUFF_CMD, 0UL, 0UL,
36 0UL, 0UL, 0UL, 0UL, 0UL});
37
38 if (ret.ret0 != RSI_SUCCESS) {
39 return 0U;
40 }
41 return ret.ret1;
42 }
43
44 return host_cal.gprs[0];
45}
Javier Almansa Sobrino82cd82e2025-01-17 17:37:42 +000046
47bool realm_sync_exception_handler(void)
48{
49 uint64_t esr_el1 = read_esr_el1();
50
51 if (EC_BITS(esr_el1) == EC_UNKNOWN) {
52 realm_printf("received undefined abort. "
53 "ESR_EL1: 0x%llx ELR_EL1: 0x%llx\n",
54 esr_el1, read_elr_el1());
55 realm_got_undef_abort++;
56 }
Javier Almansa Sobrino82cd82e2025-01-17 17:37:42 +000057 return true;
58}
59
60void realm_reset_undef_abort_count(void)
61{
62 realm_got_undef_abort = 0U;
63}
64
65unsigned int realm_get_undef_abort_count(void)
66{
67 return realm_got_undef_abort;
68}