blob: 414c329c46634459615e959c008b19cb283e1f3c [file] [log] [blame]
nabkah01002e5692022-10-10 12:36:46 +01001/*
AlexeiFedorov2f30f102023-03-13 19:37:46 +00002 * Copyright (c) 2022-2023, Arm Limited. All rights reserved.
nabkah01002e5692022-10-10 12:36:46 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stdio.h>
9
10#include <debug.h>
11#include <host_realm_helper.h>
12#include <host_shared_data.h>
13#include "realm_def.h"
14#include <realm_rsi.h>
AlexeiFedorov2f30f102023-03-13 19:37:46 +000015#include <realm_tests.h>
nabkah01002e5692022-10-10 12:36:46 +010016#include <tftf_lib.h>
17
18/*
AlexeiFedorov2f30f102023-03-13 19:37:46 +000019 * This function reads sleep time in ms from shared buffer and spins PE
20 * in a loop for that time period.
nabkah01002e5692022-10-10 12:36:46 +010021 */
22static void realm_sleep_cmd(void)
23{
24 uint64_t sleep = realm_shared_data_get_host_val(HOST_SLEEP_INDEX);
25
AlexeiFedorov2f30f102023-03-13 19:37:46 +000026 realm_printf("Realm: going to sleep for %llums\n", sleep);
nabkah01002e5692022-10-10 12:36:46 +010027 waitms(sleep);
28}
29
30/*
31 * This function requests RSI/ABI version from RMM.
32 */
33static void realm_get_rsi_version(void)
34{
35 u_register_t version;
36
37 version = rsi_get_version();
38 if (version == (u_register_t)SMC_UNKNOWN) {
AlexeiFedorov2f30f102023-03-13 19:37:46 +000039 realm_printf("SMC_RSI_ABI_VERSION failed (%ld)", (long)version);
nabkah01002e5692022-10-10 12:36:46 +010040 return;
41 }
42
AlexeiFedorov2f30f102023-03-13 19:37:46 +000043 realm_printf("RSI ABI version %u.%u (expected: %u.%u)",
nabkah01002e5692022-10-10 12:36:46 +010044 RSI_ABI_VERSION_GET_MAJOR(version),
45 RSI_ABI_VERSION_GET_MINOR(version),
46 RSI_ABI_VERSION_GET_MAJOR(RSI_ABI_VERSION),
47 RSI_ABI_VERSION_GET_MINOR(RSI_ABI_VERSION));
48}
49
50/*
51 * This is the entry function for Realm payload, it first requests the shared buffer
52 * IPA address from Host using HOST_CALL/RSI, it reads the command to be executed,
53 * performs the request, and returns to Host with the execution state SUCCESS/FAILED
54 *
55 * Host in NS world requests Realm to execute certain operations using command
56 * depending on the test case the Host wants to perform.
57 */
58void realm_payload_main(void)
59{
nabkah01002e5692022-10-10 12:36:46 +010060 bool test_succeed = false;
61
62 realm_set_shared_structure((host_shared_data_t *)rsi_get_ns_buffer());
AlexeiFedorov2f30f102023-03-13 19:37:46 +000063
nabkah01002e5692022-10-10 12:36:46 +010064 if (realm_get_shared_structure() != NULL) {
AlexeiFedorov2f30f102023-03-13 19:37:46 +000065 uint8_t cmd = realm_shared_data_get_realm_cmd();
66
nabkah01002e5692022-10-10 12:36:46 +010067 switch (cmd) {
68 case REALM_SLEEP_CMD:
69 realm_sleep_cmd();
70 test_succeed = true;
71 break;
72 case REALM_GET_RSI_VERSION:
73 realm_get_rsi_version();
74 test_succeed = true;
75 break;
AlexeiFedorov2f30f102023-03-13 19:37:46 +000076 case REALM_PMU_CYCLE:
77 test_succeed = test_pmuv3_cycle_works_realm();
78 break;
79 case REALM_PMU_EVENT:
80 test_succeed = test_pmuv3_event_works_realm();
81 break;
82 case REALM_PMU_PRESERVE:
83 test_succeed = test_pmuv3_rmm_preserves();
84 break;
85 case REALM_PMU_INTERRUPT:
86 test_succeed = test_pmuv3_overflow_interrupt();
87 break;
nabkah01002e5692022-10-10 12:36:46 +010088 default:
AlexeiFedorov2f30f102023-03-13 19:37:46 +000089 realm_printf("%s() invalid cmd %u\n", __func__, cmd);
nabkah01002e5692022-10-10 12:36:46 +010090 break;
91 }
92 }
93
94 if (test_succeed) {
95 rsi_exit_to_host(HOST_CALL_EXIT_SUCCESS_CMD);
96 } else {
97 rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD);
98 }
99}