blob: c0abf2b08bad3f01d69e77fd91a7faf6738430d5 [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>
Olivier Deprez19626b42023-12-21 18:29:05 +010014#include <spm_helpers.h>
J-Alvesf7535f42021-07-30 11:58:41 +010015
16/**
17 * Counter of the number of handled requests, for each CPU. The number of
18 * requests can be accessed from another Cactus SP, or from the normal world
19 * using a special test command.
20 */
21static uint32_t requests_counter[PLATFORM_CORE_COUNT];
J-Alves0e1e7ca2021-01-25 14:11:06 +000022
23/**
24 * Begin and end of command handler table, respectively. Both symbols defined by
25 * the linker.
26 */
27extern struct cactus_cmd_handler cactus_cmd_handler_begin[];
28extern struct cactus_cmd_handler cactus_cmd_handler_end[];
29
J-Alveseeb25472021-03-11 09:54:21 +000030#define PRINT_CMD(smc_ret) \
31 VERBOSE("cmd %lx; args: %lx, %lx, %lx, %lx\n", \
J-Alvesb3f13d72022-07-04 12:03:17 +010032 smc_ret.arg3, smc_ret.arg4, smc_ret.arg5, \
33 smc_ret.arg6, smc_ret.arg7)
J-Alveseeb25472021-03-11 09:54:21 +000034
Madhukar Pappireddye9c90932022-06-22 17:15:45 -050035/* Global FFA_MSG_DIRECT_REQ source ID */
36ffa_id_t g_dir_req_source_id;
37
J-Alves0e1e7ca2021-01-25 14:11:06 +000038/**
39 * Traverses command table from section ".cactus_handler", searches for a
40 * registered command and invokes the respective handler.
41 */
Daniel Boulbyce386b12022-03-29 18:36:36 +010042bool cactus_handle_cmd(struct ffa_value *cmd_args, struct ffa_value *ret,
J-Alves0e1e7ca2021-01-25 14:11:06 +000043 struct mailbox_buffers *mb)
44{
J-Alves4cb9dee2021-03-03 13:59:52 +000045 uint64_t in_cmd;
46
Olivier Deprez19626b42023-12-21 18:29:05 +010047 /* Get vCPU index for currently running vCPU. */
48 unsigned int core_pos = spm_get_my_core_pos();
J-Alvesf7535f42021-07-30 11:58:41 +010049
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}