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> |
Madhukar Pappireddy | 11e5748 | 2025-03-28 11:46:16 -0500 | [diff] [blame] | 14 | #include <sp_helpers.h> |
Olivier Deprez | 19626b4 | 2023-12-21 18:29:05 +0100 | [diff] [blame] | 15 | #include <spm_helpers.h> |
Madhukar Pappireddy | 611d095 | 2025-01-31 16:07:08 -0600 | [diff] [blame] | 16 | #include <psci.h> |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Counter of the number of handled requests, for each CPU. The number of |
| 20 | * requests can be accessed from another Cactus SP, or from the normal world |
| 21 | * using a special test command. |
| 22 | */ |
| 23 | static uint32_t requests_counter[PLATFORM_CORE_COUNT]; |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Begin and end of command handler table, respectively. Both symbols defined by |
| 27 | * the linker. |
| 28 | */ |
| 29 | extern struct cactus_cmd_handler cactus_cmd_handler_begin[]; |
| 30 | extern struct cactus_cmd_handler cactus_cmd_handler_end[]; |
| 31 | |
J-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 32 | #define PRINT_CMD(smc_ret) \ |
| 33 | VERBOSE("cmd %lx; args: %lx, %lx, %lx, %lx\n", \ |
J-Alves | b3f13d7 | 2022-07-04 12:03:17 +0100 | [diff] [blame] | 34 | smc_ret.arg3, smc_ret.arg4, smc_ret.arg5, \ |
| 35 | smc_ret.arg6, smc_ret.arg7) |
J-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 36 | |
Madhukar Pappireddy | e9c9093 | 2022-06-22 17:15:45 -0500 | [diff] [blame] | 37 | /* Global FFA_MSG_DIRECT_REQ source ID */ |
| 38 | ffa_id_t g_dir_req_source_id; |
| 39 | |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Traverses command table from section ".cactus_handler", searches for a |
| 42 | * registered command and invokes the respective handler. |
| 43 | */ |
Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 44 | 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] | 45 | struct mailbox_buffers *mb) |
| 46 | { |
J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 47 | uint64_t in_cmd; |
| 48 | |
Olivier Deprez | 19626b4 | 2023-12-21 18:29:05 +0100 | [diff] [blame] | 49 | /* Get vCPU index for currently running vCPU. */ |
| 50 | unsigned int core_pos = spm_get_my_core_pos(); |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 51 | |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 52 | if (cmd_args == NULL || ret == NULL) { |
Madhukar Pappireddy | cd183ef | 2021-08-05 15:34:07 -0500 | [diff] [blame] | 53 | ERROR("Invalid arguments passed to %s!\n", __func__); |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
Madhukar Pappireddy | e9c9093 | 2022-06-22 17:15:45 -0500 | [diff] [blame] | 57 | /* Get the source of the Direct Request message. */ |
| 58 | if (ffa_func_id(*cmd_args) == FFA_MSG_SEND_DIRECT_REQ_SMC32 || |
| 59 | ffa_func_id(*cmd_args) == FFA_MSG_SEND_DIRECT_REQ_SMC64) { |
| 60 | g_dir_req_source_id = ffa_dir_msg_source(*cmd_args); |
| 61 | } |
| 62 | |
J-Alves | eeb2547 | 2021-03-11 09:54:21 +0000 | [diff] [blame] | 63 | PRINT_CMD((*cmd_args)); |
| 64 | |
J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 65 | in_cmd = cactus_get_cmd(*cmd_args); |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 66 | |
| 67 | for (struct cactus_cmd_handler *it_cmd = cactus_cmd_handler_begin; |
| 68 | it_cmd < cactus_cmd_handler_end; |
| 69 | it_cmd++) { |
| 70 | if (it_cmd->id == in_cmd) { |
| 71 | *ret = it_cmd->fn(cmd_args, mb); |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Increment the number of requests handled in current |
| 75 | * core. |
| 76 | */ |
| 77 | requests_counter[core_pos]++; |
| 78 | |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
J-Alves | f7535f4 | 2021-07-30 11:58:41 +0100 | [diff] [blame] | 83 | /* Handle special command. */ |
| 84 | if (in_cmd == CACTUS_GET_REQ_COUNT_CMD) { |
| 85 | uint32_t requests_counter_resp; |
| 86 | |
| 87 | /* Read value from array. */ |
| 88 | requests_counter_resp = requests_counter[core_pos]; |
| 89 | VERBOSE("Requests Counter %u, core: %u\n", requests_counter_resp, |
| 90 | core_pos); |
| 91 | |
| 92 | *ret = cactus_success_resp( |
| 93 | ffa_dir_msg_dest(*cmd_args), |
| 94 | ffa_dir_msg_source(*cmd_args), |
| 95 | requests_counter_resp); |
| 96 | return true; |
| 97 | } |
| 98 | |
J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 99 | *ret = cactus_error_resp(ffa_dir_msg_dest(*cmd_args), |
| 100 | ffa_dir_msg_source(*cmd_args), |
| 101 | CACTUS_ERROR_UNHANDLED); |
| 102 | return true; |
J-Alves | 0e1e7ca | 2021-01-25 14:11:06 +0000 | [diff] [blame] | 103 | } |
Madhukar Pappireddy | 611d095 | 2025-01-31 16:07:08 -0600 | [diff] [blame] | 104 | |
| 105 | struct ffa_value cactus_handle_framework_msg(struct ffa_value args) |
| 106 | { |
| 107 | ffa_id_t source_id = ffa_dir_msg_source(args); |
| 108 | ffa_id_t destination_id = ffa_dir_msg_dest(args); |
| 109 | uint32_t status_code; |
Madhukar Pappireddy | a2b6b37 | 2025-02-11 15:39:32 -0600 | [diff] [blame] | 110 | |
| 111 | #if CACTUS_PWR_MGMT_SUPPORT == 1 |
Madhukar Pappireddy | 611d095 | 2025-01-31 16:07:08 -0600 | [diff] [blame] | 112 | uint32_t framework_msg = ffa_get_framework_msg(args); |
| 113 | uint32_t psci_function = args.arg3; |
Madhukar Pappireddy | 11e5748 | 2025-03-28 11:46:16 -0500 | [diff] [blame] | 114 | struct ffa_value ret; |
Madhukar Pappireddy | 611d095 | 2025-01-31 16:07:08 -0600 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * As of now, Cactus supports receiving only PSCI power management |
| 118 | * request as framework message. |
| 119 | */ |
| 120 | if (framework_msg != FFA_FRAMEWORK_MSG_PSCI_REQ) { |
| 121 | ERROR("Unsupported framework message received by SP:%x\n", |
| 122 | destination_id); |
| 123 | status_code = PSCI_E_DENIED; |
| 124 | goto out; |
| 125 | } |
| 126 | |
| 127 | /* Cactus only supports receiving CPU_OFF PSCI function as message. */ |
| 128 | if (psci_function != SMC_PSCI_CPU_OFF) { |
| 129 | ERROR("Unsupported PSCI function(%x) received by SP:%x through " |
| 130 | "framework message\n", psci_function, destination_id); |
| 131 | status_code = PSCI_E_DENIED; |
| 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | /* Only SPMC can send the PSCI framework message. */ |
| 136 | if (source_id != SPMC_ID) { |
| 137 | ERROR("Framework message source illegal %x\n", source_id); |
| 138 | status_code = PSCI_E_DENIED; |
| 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | status_code = PSCI_E_SUCCESS; |
Madhukar Pappireddy | 11e5748 | 2025-03-28 11:46:16 -0500 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * FF-A spec states that SPs are prohibited from invoking Direct |
| 146 | * request, FFA_RUN and FFA_YIELD interfaces while handling power |
| 147 | * management framework message. Make the Cactus SP intentionally |
| 148 | * invoke prohibited interfaces and attest that SPMC should deny such |
| 149 | * invocations. |
| 150 | */ |
| 151 | ret = cactus_success_resp(destination_id, source_id, status_code); |
| 152 | |
| 153 | /* Non-framework direct response must be denied. */ |
| 154 | EXPECT(ffa_func_id(ret), FFA_ERROR); |
| 155 | EXPECT(ffa_error_code(ret), FFA_ERROR_DENIED); |
| 156 | |
| 157 | ret = cactus_echo_send_cmd(destination_id, SP_ID(4), 0x9999); |
| 158 | |
| 159 | /* Direct request message must be denied. */ |
| 160 | EXPECT(ffa_func_id(ret), FFA_ERROR); |
| 161 | EXPECT(ffa_error_code(ret), FFA_ERROR_DENIED); |
| 162 | |
| 163 | ret = ffa_run(SP_ID(4), 0); |
| 164 | |
| 165 | /* FFA_RUN invocation must be denied. */ |
| 166 | EXPECT(ffa_func_id(ret), FFA_ERROR); |
| 167 | EXPECT(ffa_error_code(ret), FFA_ERROR_DENIED); |
| 168 | |
| 169 | ret = ffa_yield(); |
| 170 | |
| 171 | /* FFA_YIELD invocation must be denied. */ |
| 172 | EXPECT(ffa_func_id(ret), FFA_ERROR); |
| 173 | EXPECT(ffa_error_code(ret), FFA_ERROR_DENIED); |
| 174 | |
Madhukar Pappireddy | 611d095 | 2025-01-31 16:07:08 -0600 | [diff] [blame] | 175 | /* |
| 176 | * Return successful status for PSCI power management request through |
| 177 | * direct response Framework message. |
| 178 | */ |
| 179 | VERBOSE("PSCI power management request handled successfully by SP:%x\n", |
| 180 | destination_id); |
| 181 | out: |
Madhukar Pappireddy | a2b6b37 | 2025-02-11 15:39:32 -0600 | [diff] [blame] | 182 | #else |
| 183 | status_code = PSCI_E_DENIED; |
| 184 | #endif |
Madhukar Pappireddy | 611d095 | 2025-01-31 16:07:08 -0600 | [diff] [blame] | 185 | return ffa_framework_msg_send_direct_resp(destination_id, source_id, |
| 186 | FFA_FRAMEWORK_MSG_PSCI_RESP, status_code); |
| 187 | } |