blob: e56e51ef24edf6cf39f8498efcf29ca5681110d4 [file] [log] [blame]
J-Alves0e1e7ca2021-01-25 14:11:06 +00001/*
Daniel Boulbyce386b12022-03-29 18:36:36 +01002 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
J-Alves0e1e7ca2021-01-25 14:11:06 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
J-Alveseeb25472021-03-11 09:54:21 +00007#include <debug.h>
8
J-Alvesf7535f42021-07-30 11:58:41 +01009#include <cactus_message_loop.h>
10#include <cactus_test_cmds.h>
11#include <ffa_helpers.h>
12#include <events.h>
13#include <platform.h>
14
15/**
16 * Counter of the number of handled requests, for each CPU. The number of
17 * requests can be accessed from another Cactus SP, or from the normal world
18 * using a special test command.
19 */
20static uint32_t requests_counter[PLATFORM_CORE_COUNT];
J-Alves0e1e7ca2021-01-25 14:11:06 +000021
22/**
23 * Begin and end of command handler table, respectively. Both symbols defined by
24 * the linker.
25 */
26extern struct cactus_cmd_handler cactus_cmd_handler_begin[];
27extern struct cactus_cmd_handler cactus_cmd_handler_end[];
28
J-Alveseeb25472021-03-11 09:54:21 +000029#define PRINT_CMD(smc_ret) \
30 VERBOSE("cmd %lx; args: %lx, %lx, %lx, %lx\n", \
J-Alvesb3f13d72022-07-04 12:03:17 +010031 smc_ret.arg3, smc_ret.arg4, smc_ret.arg5, \
32 smc_ret.arg6, smc_ret.arg7)
J-Alveseeb25472021-03-11 09:54:21 +000033
Madhukar Pappireddye9c90932022-06-22 17:15:45 -050034/* Global FFA_MSG_DIRECT_REQ source ID */
35ffa_id_t g_dir_req_source_id;
36
J-Alves0e1e7ca2021-01-25 14:11:06 +000037/**
38 * Traverses command table from section ".cactus_handler", searches for a
39 * registered command and invokes the respective handler.
40 */
Daniel Boulbyce386b12022-03-29 18:36:36 +010041bool cactus_handle_cmd(struct ffa_value *cmd_args, struct ffa_value *ret,
J-Alves0e1e7ca2021-01-25 14:11:06 +000042 struct mailbox_buffers *mb)
43{
J-Alves4cb9dee2021-03-03 13:59:52 +000044 uint64_t in_cmd;
45
J-Alvesf7535f42021-07-30 11:58:41 +010046 /* Get which core it is running from. */
47 unsigned int core_pos = platform_get_core_pos(
48 read_mpidr_el1() & MPID_MASK);
49
J-Alves0e1e7ca2021-01-25 14:11:06 +000050 if (cmd_args == NULL || ret == NULL) {
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050051 ERROR("Invalid arguments passed to %s!\n", __func__);
J-Alves0e1e7ca2021-01-25 14:11:06 +000052 return false;
53 }
54
Madhukar Pappireddye9c90932022-06-22 17:15:45 -050055 /* Get the source of the Direct Request message. */
56 if (ffa_func_id(*cmd_args) == FFA_MSG_SEND_DIRECT_REQ_SMC32 ||
57 ffa_func_id(*cmd_args) == FFA_MSG_SEND_DIRECT_REQ_SMC64) {
58 g_dir_req_source_id = ffa_dir_msg_source(*cmd_args);
59 }
60
J-Alveseeb25472021-03-11 09:54:21 +000061 PRINT_CMD((*cmd_args));
62
J-Alves4cb9dee2021-03-03 13:59:52 +000063 in_cmd = cactus_get_cmd(*cmd_args);
J-Alves0e1e7ca2021-01-25 14:11:06 +000064
65 for (struct cactus_cmd_handler *it_cmd = cactus_cmd_handler_begin;
66 it_cmd < cactus_cmd_handler_end;
67 it_cmd++) {
68 if (it_cmd->id == in_cmd) {
69 *ret = it_cmd->fn(cmd_args, mb);
J-Alvesf7535f42021-07-30 11:58:41 +010070
71 /*
72 * Increment the number of requests handled in current
73 * core.
74 */
75 requests_counter[core_pos]++;
76
J-Alves0e1e7ca2021-01-25 14:11:06 +000077 return true;
78 }
79 }
80
J-Alvesf7535f42021-07-30 11:58:41 +010081 /* Handle special command. */
82 if (in_cmd == CACTUS_GET_REQ_COUNT_CMD) {
83 uint32_t requests_counter_resp;
84
85 /* Read value from array. */
86 requests_counter_resp = requests_counter[core_pos];
87 VERBOSE("Requests Counter %u, core: %u\n", requests_counter_resp,
88 core_pos);
89
90 *ret = cactus_success_resp(
91 ffa_dir_msg_dest(*cmd_args),
92 ffa_dir_msg_source(*cmd_args),
93 requests_counter_resp);
94 return true;
95 }
96
J-Alves4cb9dee2021-03-03 13:59:52 +000097 *ret = cactus_error_resp(ffa_dir_msg_dest(*cmd_args),
98 ffa_dir_msg_source(*cmd_args),
99 CACTUS_ERROR_UNHANDLED);
100 return true;
J-Alves0e1e7ca2021-01-25 14:11:06 +0000101}