J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 1 | /* |
Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022, Arm Limited. All rights reserved. |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
J-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 7 | #include <debug.h> |
| 8 | |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 9 | #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 | */ |
| 20 | static uint32_t requests_counter[PLATFORM_CORE_COUNT]; |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Begin and end of command handler table, respectively. Both symbols defined by |
| 24 | * the linker. |
| 25 | */ |
| 26 | extern struct cactus_cmd_handler cactus_cmd_handler_begin[]; |
| 27 | extern struct cactus_cmd_handler cactus_cmd_handler_end[]; |
| 28 | |
J-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 29 | #define PRINT_CMD(smc_ret) \ |
| 30 | VERBOSE("cmd %lx; args: %lx, %lx, %lx, %lx\n", \ |
J-Alves | b3f13d7 | 2022-07-04 12:03:17 +0100 | [diff] [blame] | 31 | smc_ret.arg3, smc_ret.arg4, smc_ret.arg5, \ |
| 32 | smc_ret.arg6, smc_ret.arg7) |
J-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 33 | |
Madhukar Pappireddy | e9c9093 | 2022-06-22 17:15:45 -0500 | [diff] [blame^] | 34 | /* Global FFA_MSG_DIRECT_REQ source ID */ |
| 35 | ffa_id_t g_dir_req_source_id; |
| 36 | |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Traverses command table from section ".cactus_handler", searches for a |
| 39 | * registered command and invokes the respective handler. |
| 40 | */ |
Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 41 | bool cactus_handle_cmd(struct ffa_value *cmd_args, struct ffa_value *ret, |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 42 | struct mailbox_buffers *mb) |
| 43 | { |
J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 44 | uint64_t in_cmd; |
| 45 | |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 46 | /* Get which core it is running from. */ |
| 47 | unsigned int core_pos = platform_get_core_pos( |
| 48 | read_mpidr_el1() & MPID_MASK); |
| 49 | |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 50 | if (cmd_args == NULL || ret == NULL) { |
Madhukar Pappireddy | cd183ef | 2021-08-05 15:34:07 -0500 | [diff] [blame] | 51 | ERROR("Invalid arguments passed to %s!\n", __func__); |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
Madhukar Pappireddy | e9c9093 | 2022-06-22 17:15:45 -0500 | [diff] [blame^] | 55 | /* 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-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 61 | PRINT_CMD((*cmd_args)); |
| 62 | |
J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 63 | in_cmd = cactus_get_cmd(*cmd_args); |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 64 | |
| 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-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Increment the number of requests handled in current |
| 73 | * core. |
| 74 | */ |
| 75 | requests_counter[core_pos]++; |
| 76 | |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 81 | /* 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-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 97 | *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-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 101 | } |