Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 The Hafnium Authors. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
| 7 | */ |
| 8 | |
| 9 | #include "hf/arch/gicv3.h" |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 10 | |
| 11 | #include "hf/api.h" |
| 12 | #include "hf/check.h" |
Karl Meakin | 902af08 | 2024-11-28 14:58:38 +0000 | [diff] [blame] | 13 | #include "hf/ffa.h" |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 14 | #include "hf/ffa/direct_messaging.h" |
Karl Meakin | 902af08 | 2024-11-28 14:58:38 +0000 | [diff] [blame] | 15 | #include "hf/ffa/interrupts.h" |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 16 | #include "hf/ffa/vm.h" |
| 17 | #include "hf/ffa_internal.h" |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 18 | #include "hf/plat/interrupts.h" |
| 19 | #include "hf/vm.h" |
| 20 | |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 21 | bool ffa_cpu_cycles_run_forward(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_idx, |
| 22 | struct ffa_value *ret) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 23 | { |
| 24 | (void)vm_id; |
| 25 | (void)vcpu_idx; |
| 26 | (void)ret; |
| 27 | |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Check if current VM can resume target VM using FFA_RUN ABI. |
| 33 | */ |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 34 | bool ffa_cpu_cycles_run_checks(struct vcpu_locked current_locked, |
| 35 | ffa_id_t target_vm_id, ffa_vcpu_index_t vcpu_idx, |
| 36 | struct ffa_value *run_ret, struct vcpu **next) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 37 | { |
| 38 | /* |
| 39 | * Under the Partition runtime model specified in FF-A v1.1-Beta0 spec, |
| 40 | * SP can invoke FFA_RUN to resume target SP. |
| 41 | */ |
| 42 | struct vcpu *target_vcpu; |
| 43 | struct vcpu *current = current_locked.vcpu; |
| 44 | bool ret = true; |
| 45 | struct vm *vm; |
| 46 | struct vcpu_locked target_locked; |
| 47 | struct two_vcpu_locked vcpus_locked; |
| 48 | |
| 49 | vm = vm_find(target_vm_id); |
| 50 | if (vm == NULL) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if (vm_is_mp(vm) && vm_is_mp(current->vm) && |
| 55 | vcpu_idx != cpu_index(current->cpu)) { |
| 56 | dlog_verbose("vcpu_idx (%d) != pcpu index (%zu)\n", vcpu_idx, |
| 57 | cpu_index(current->cpu)); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | target_vcpu = api_ffa_get_vm_vcpu(vm, current); |
| 62 | |
| 63 | vcpu_unlock(¤t_locked); |
| 64 | |
| 65 | /* Lock both vCPUs at once to avoid deadlock. */ |
| 66 | vcpus_locked = vcpu_lock_both(current, target_vcpu); |
| 67 | current_locked = vcpus_locked.vcpu1; |
| 68 | target_locked = vcpus_locked.vcpu2; |
| 69 | |
| 70 | /* Only the primary VM can turn ON a vCPU that is currently OFF. */ |
| 71 | if (!vm_is_primary(current->vm) && |
| 72 | target_vcpu->state == VCPU_STATE_OFF) { |
| 73 | run_ret->arg2 = FFA_DENIED; |
| 74 | ret = false; |
| 75 | goto out; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * An SPx can resume another SPy only when SPy is in PREEMPTED or |
| 80 | * BLOCKED state. |
| 81 | */ |
| 82 | if (vm_id_is_current_world(current->vm->id) && |
| 83 | vm_id_is_current_world(target_vm_id)) { |
| 84 | /* Target SP must be in preempted or blocked state. */ |
| 85 | if (target_vcpu->state != VCPU_STATE_PREEMPTED && |
| 86 | target_vcpu->state != VCPU_STATE_BLOCKED) { |
| 87 | run_ret->arg2 = FFA_DENIED; |
| 88 | ret = false; |
| 89 | goto out; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* A SP cannot invoke FFA_RUN to resume a normal world VM. */ |
| 94 | if (!vm_id_is_current_world(target_vm_id)) { |
| 95 | run_ret->arg2 = FFA_DENIED; |
| 96 | ret = false; |
| 97 | goto out; |
| 98 | } |
| 99 | |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 100 | if (vm_id_is_current_world(current->vm->id)) { |
| 101 | /* |
| 102 | * Refer FF-A v1.1 EAC0 spec section 8.3.2.2.1 |
| 103 | * Signaling an Other S-Int in blocked state |
| 104 | */ |
| 105 | if (current->preempted_vcpu != NULL) { |
| 106 | /* |
| 107 | * After the target SP execution context has handled |
| 108 | * the interrupt, it uses the FFA_RUN ABI to resume |
| 109 | * the request due to which it had entered the blocked |
| 110 | * state earlier. |
| 111 | * Deny the state transition if the SP didnt perform the |
| 112 | * deactivation of the secure virtual interrupt. |
| 113 | */ |
Daniel Boulby | d21e9b3 | 2025-02-13 15:53:21 +0000 | [diff] [blame] | 114 | if (vcpu_virt_interrupt_count_get(current_locked) > 0) { |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 115 | run_ret->arg2 = FFA_DENIED; |
| 116 | ret = false; |
| 117 | goto out; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Refer Figure 8.13 Scenario 1: Implementation choice: |
| 122 | * SPMC left all intermediate SP execution contexts in |
| 123 | * blocked state. Hence, SPMC now bypasses the |
| 124 | * intermediate these execution contexts and resumes the |
| 125 | * SP execution context that was originally preempted. |
| 126 | */ |
| 127 | *next = current->preempted_vcpu; |
| 128 | if (target_vcpu != current->preempted_vcpu) { |
| 129 | dlog_verbose("Skipping intermediate vCPUs\n"); |
| 130 | } |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Clear fields corresponding to secure interrupt |
| 134 | * handling. |
| 135 | */ |
| 136 | vcpu_secure_interrupt_complete(current_locked); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* Check if a vCPU of SP is being resumed. */ |
| 141 | if (vm_id_is_current_world(target_vm_id)) { |
| 142 | /* |
| 143 | * A call chain cannot span CPUs. The target vCPU can only be |
| 144 | * resumed by FFA_RUN on present CPU. |
| 145 | */ |
| 146 | if ((target_vcpu->call_chain.prev_node != NULL || |
| 147 | target_vcpu->call_chain.next_node != NULL) && |
| 148 | (target_vcpu->cpu != current->cpu)) { |
| 149 | run_ret->arg2 = FFA_DENIED; |
| 150 | ret = false; |
| 151 | goto out; |
| 152 | } |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | out: |
| 156 | vcpu_unlock(&target_locked); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * SPMC scheduled call chain is completely unwound. |
| 162 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 163 | static void ffa_cpu_cycles_exit_spmc_schedule_mode( |
| 164 | struct vcpu_locked current_locked) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 165 | { |
| 166 | struct vcpu *current; |
| 167 | |
| 168 | current = current_locked.vcpu; |
| 169 | assert(current->call_chain.next_node == NULL); |
| 170 | CHECK(current->scheduling_mode == SPMC_MODE); |
| 171 | |
| 172 | current->scheduling_mode = NONE; |
| 173 | current->rt_model = RTM_NONE; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * A SP in running state could have been pre-empted by a secure interrupt. SPM |
| 178 | * would switch the execution to the vCPU of target SP responsible for interupt |
| 179 | * handling. Upon completion of interrupt handling, vCPU performs interrupt |
| 180 | * signal completion through FFA_MSG_WAIT ABI (provided it was in waiting state |
| 181 | * when interrupt was signaled). |
| 182 | * |
| 183 | * SPM then resumes the original SP that was initially pre-empted. |
| 184 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 185 | static struct ffa_value ffa_cpu_cycles_preempted_vcpu_resume( |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 186 | struct vcpu_locked current_locked, struct vcpu **next) |
| 187 | { |
| 188 | struct ffa_value ffa_ret = (struct ffa_value){.func = FFA_MSG_WAIT_32}; |
| 189 | struct vcpu *target_vcpu; |
| 190 | struct vcpu *current = current_locked.vcpu; |
| 191 | struct vcpu_locked target_locked; |
| 192 | struct two_vcpu_locked vcpus_locked; |
| 193 | |
| 194 | CHECK(current->preempted_vcpu != NULL); |
| 195 | CHECK(current->preempted_vcpu->state == VCPU_STATE_PREEMPTED); |
| 196 | |
| 197 | target_vcpu = current->preempted_vcpu; |
| 198 | vcpu_unlock(¤t_locked); |
| 199 | |
| 200 | /* Lock both vCPUs at once to avoid deadlock. */ |
| 201 | vcpus_locked = vcpu_lock_both(current, target_vcpu); |
| 202 | current_locked = vcpus_locked.vcpu1; |
| 203 | target_locked = vcpus_locked.vcpu2; |
| 204 | |
| 205 | /* Reset the fields tracking secure interrupt processing. */ |
| 206 | vcpu_secure_interrupt_complete(current_locked); |
| 207 | |
| 208 | /* SPMC scheduled call chain is completely unwound. */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 209 | ffa_cpu_cycles_exit_spmc_schedule_mode(current_locked); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 210 | assert(current->call_chain.prev_node == NULL); |
| 211 | |
| 212 | current->state = VCPU_STATE_WAITING; |
| 213 | |
| 214 | vcpu_set_running(target_locked, NULL); |
| 215 | |
| 216 | vcpu_unlock(&target_locked); |
| 217 | |
| 218 | /* Restore interrupt priority mask. */ |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 219 | ffa_interrupts_unmask(current); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 220 | |
| 221 | /* The pre-empted vCPU should be run. */ |
| 222 | *next = target_vcpu; |
| 223 | |
| 224 | return ffa_ret; |
| 225 | } |
| 226 | |
J-Alves | 4c334a3 | 2025-03-18 11:49:26 +0000 | [diff] [blame] | 227 | static void ffa_msg_wait_complete(struct vcpu_locked current_locked, |
| 228 | struct vcpu **next) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 229 | { |
| 230 | struct vcpu *current = current_locked.vcpu; |
| 231 | |
| 232 | current->scheduling_mode = NONE; |
| 233 | current->rt_model = RTM_NONE; |
| 234 | |
Daniel Boulby | d799223 | 2025-03-06 17:09:49 +0000 | [diff] [blame] | 235 | /* |
| 236 | * We no longer need to do a managed exit so clear the interrupt if |
| 237 | * needed. |
| 238 | */ |
| 239 | vcpu_virt_interrupt_clear(current_locked, HF_MANAGED_EXIT_INTID); |
| 240 | |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 241 | /* Relinquish control back to the NWd. */ |
| 242 | *next = api_switch_to_other_world( |
| 243 | current_locked, (struct ffa_value){.func = FFA_MSG_WAIT_32}, |
| 244 | VCPU_STATE_WAITING); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Deals with the common case of intercepting an FFA_MSG_WAIT call. |
| 249 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 250 | static bool ffa_cpu_cycles_msg_wait_intercept(struct vcpu_locked current_locked, |
| 251 | struct vcpu **next, |
| 252 | struct ffa_value *ffa_ret) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 253 | { |
| 254 | struct two_vcpu_locked both_vcpu_locks; |
| 255 | struct vcpu *current = current_locked.vcpu; |
| 256 | bool ret = false; |
| 257 | |
| 258 | assert(next != NULL); |
| 259 | assert(*next != NULL); |
| 260 | |
| 261 | vcpu_unlock(¤t_locked); |
| 262 | |
| 263 | both_vcpu_locks = vcpu_lock_both(current, *next); |
| 264 | |
| 265 | /* |
J-Alves | 4796d11 | 2025-02-12 14:39:00 +0000 | [diff] [blame] | 266 | * Check if there is a pending interrupt, and if the partition |
| 267 | * is expects to notify the scheduler or resume straight away. |
| 268 | * Either trigger SRI for later donation of CPU cycles, or |
| 269 | * eret `FFA_INTERRUPT` back to the caller. |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 270 | */ |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 271 | if (ffa_interrupts_intercept_call(both_vcpu_locks.vcpu1, |
| 272 | both_vcpu_locks.vcpu2, ffa_ret)) { |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 273 | *next = NULL; |
| 274 | ret = true; |
| 275 | } |
| 276 | |
| 277 | vcpu_unlock(&both_vcpu_locks.vcpu2); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 282 | static bool sp_boot_next(struct vcpu_locked current_locked, struct vcpu **next) |
| 283 | { |
| 284 | struct vcpu *vcpu_next = NULL; |
| 285 | struct vcpu *current = current_locked.vcpu; |
| 286 | struct vm *next_vm; |
| 287 | size_t cpu_indx = cpu_index(current->cpu); |
| 288 | |
| 289 | if (current->cpu->last_sp_initialized) { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | if (!atomic_load_explicit(¤t->vm->aborting, |
| 294 | memory_order_relaxed)) { |
| 295 | /* vCPU has just returned from successful initialization. */ |
| 296 | dlog_verbose( |
| 297 | "Initialized execution context of VM: %#x on CPU: %zu, " |
| 298 | "boot_order: %u\n", |
| 299 | current->vm->id, cpu_index(current->cpu), |
| 300 | current->vm->boot_order); |
| 301 | } |
| 302 | |
| 303 | if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) { |
| 304 | next_vm = vm_get_next_boot(current->vm); |
| 305 | } else { |
| 306 | /* SP boot chain on secondary CPU. */ |
| 307 | next_vm = vm_get_next_boot_secondary_core(current->vm); |
| 308 | } |
| 309 | |
| 310 | current->state = VCPU_STATE_WAITING; |
| 311 | current->rt_model = RTM_NONE; |
| 312 | current->scheduling_mode = NONE; |
| 313 | |
| 314 | /* |
| 315 | * Pick next SP's vCPU to be booted. Once all SPs have booted |
| 316 | * (next_vm is NULL), then return execution to NWd. |
| 317 | */ |
| 318 | if (next_vm == NULL) { |
| 319 | current->cpu->last_sp_initialized = true; |
| 320 | goto out; |
| 321 | } |
| 322 | |
| 323 | vcpu_next = vm_get_vcpu(next_vm, cpu_indx); |
| 324 | |
| 325 | /* |
| 326 | * An SP's execution context needs to be bootstrapped if: |
| 327 | * - It has never been initialized before. |
| 328 | * - Or it was turned off when the CPU, on which it was pinned, was |
| 329 | * powered down. |
| 330 | */ |
| 331 | if (vcpu_next->rt_model == RTM_SP_INIT || |
| 332 | vcpu_next->state == VCPU_STATE_OFF) { |
| 333 | vcpu_next->rt_model = RTM_SP_INIT; |
| 334 | arch_regs_reset(vcpu_next); |
| 335 | vcpu_next->cpu = current->cpu; |
| 336 | vcpu_next->state = VCPU_STATE_RUNNING; |
| 337 | vcpu_next->regs_available = false; |
| 338 | vcpu_set_phys_core_idx(vcpu_next); |
| 339 | arch_regs_set_pc_arg(&vcpu_next->regs, |
| 340 | vcpu_next->vm->secondary_ep, 0ULL); |
| 341 | |
| 342 | if (cpu_index(current_locked.vcpu->cpu) == PRIMARY_CPU_IDX) { |
| 343 | /* |
| 344 | * Boot information is passed by the SPMC to the SP's |
| 345 | * execution context only on the primary CPU. |
| 346 | */ |
| 347 | vcpu_set_boot_info_gp_reg(vcpu_next); |
| 348 | } |
| 349 | |
| 350 | *next = vcpu_next; |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | out: |
| 355 | dlog_notice("Finished bootstrapping all SPs on CPU%lx\n", cpu_indx); |
| 356 | return false; |
| 357 | } |
| 358 | |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 359 | /** |
| 360 | * The invocation of FFA_MSG_WAIT at secure virtual FF-A instance is compliant |
| 361 | * with FF-A v1.1 EAC0 specification. It only performs the state transition |
| 362 | * from RUNNING to WAITING for the following Partition runtime models: |
| 363 | * RTM_FFA_RUN, RTM_SEC_INTERRUPT, RTM_SP_INIT. |
| 364 | */ |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 365 | struct ffa_value ffa_cpu_cycles_msg_wait_prepare( |
| 366 | struct vcpu_locked current_locked, struct vcpu **next) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 367 | { |
| 368 | struct ffa_value ret = api_ffa_interrupt_return(0); |
| 369 | struct vcpu *current = current_locked.vcpu; |
| 370 | |
| 371 | switch (current->rt_model) { |
| 372 | case RTM_SP_INIT: |
| 373 | if (!sp_boot_next(current_locked, next)) { |
J-Alves | 4c334a3 | 2025-03-18 11:49:26 +0000 | [diff] [blame] | 374 | ffa_msg_wait_complete(current_locked, next); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 375 | |
J-Alves | 1ef5fc4 | 2025-02-27 17:15:29 +0000 | [diff] [blame] | 376 | ffa_cpu_cycles_msg_wait_intercept(current_locked, next, |
| 377 | &ret); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 378 | } |
| 379 | break; |
| 380 | case RTM_SEC_INTERRUPT: |
| 381 | /* |
| 382 | * Either resume the preempted SP or complete the FFA_MSG_WAIT. |
| 383 | */ |
| 384 | assert(current->preempted_vcpu != NULL); |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 385 | ffa_cpu_cycles_preempted_vcpu_resume(current_locked, next); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 386 | |
J-Alves | 1ef5fc4 | 2025-02-27 17:15:29 +0000 | [diff] [blame] | 387 | if (!ffa_cpu_cycles_msg_wait_intercept(current_locked, next, |
| 388 | &ret)) { |
| 389 | /* |
| 390 | * If CPU cycles were allocated through FFA_RUN |
| 391 | * interface, allow the interrupts(if they were masked |
| 392 | * earlier) before returning control to NWd. |
| 393 | */ |
| 394 | ffa_interrupts_unmask(current); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 397 | break; |
| 398 | case RTM_FFA_RUN: |
J-Alves | 4c334a3 | 2025-03-18 11:49:26 +0000 | [diff] [blame] | 399 | ffa_msg_wait_complete(current_locked, next); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 400 | |
J-Alves | 1ef5fc4 | 2025-02-27 17:15:29 +0000 | [diff] [blame] | 401 | if (!ffa_cpu_cycles_msg_wait_intercept(current_locked, next, |
| 402 | &ret)) { |
| 403 | /* |
| 404 | * If CPU cycles were allocated through FFA_RUN |
| 405 | * interface, allow the interrupts(if they were masked |
| 406 | * earlier) before returning control to NWd. |
| 407 | */ |
| 408 | ffa_interrupts_unmask(current); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 411 | break; |
| 412 | default: |
| 413 | panic("%s: unexpected runtime model %x for [%x %x]", |
| 414 | current->rt_model, current->vm->id, |
| 415 | cpu_index(current->cpu)); |
| 416 | } |
| 417 | |
| 418 | vcpu_unlock(¤t_locked); |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 423 | /* |
| 424 | * Initialize the scheduling mode and/or Partition Runtime model of the target |
| 425 | * SP upon being resumed by an FFA_RUN ABI. |
| 426 | */ |
J-Alves | 5d65fe7 | 2025-02-18 16:02:32 +0000 | [diff] [blame] | 427 | void ffa_cpu_cycles_init_schedule_mode_ffa_run( |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 428 | struct vcpu_locked current_locked, struct vcpu_locked target_locked) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 429 | { |
| 430 | struct vcpu *vcpu = target_locked.vcpu; |
| 431 | struct vcpu *current = current_locked.vcpu; |
| 432 | |
| 433 | /* |
| 434 | * Scenario 1 in Table 8.4; Therefore SPMC could be resuming a vCPU |
| 435 | * that was part of NWd scheduled mode. |
| 436 | */ |
| 437 | CHECK(vcpu->scheduling_mode != SPMC_MODE); |
| 438 | |
| 439 | /* Section 8.2.3 bullet 4.2 of spec FF-A v1.1 EAC0. */ |
| 440 | if (vcpu->state == VCPU_STATE_WAITING) { |
| 441 | assert(vcpu->rt_model == RTM_SP_INIT || |
| 442 | vcpu->rt_model == RTM_NONE); |
| 443 | vcpu->rt_model = RTM_FFA_RUN; |
| 444 | |
| 445 | if (!vm_id_is_current_world(current->vm->id) || |
| 446 | (current->scheduling_mode == NWD_MODE)) { |
| 447 | vcpu->scheduling_mode = NWD_MODE; |
| 448 | } |
| 449 | } else { |
| 450 | /* SP vCPU would have been pre-empted earlier or blocked. */ |
| 451 | CHECK(vcpu->state == VCPU_STATE_PREEMPTED || |
| 452 | vcpu->state == VCPU_STATE_BLOCKED); |
| 453 | } |
| 454 | |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 455 | ffa_interrupts_mask(target_locked); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Prepare to yield execution back to the VM/SP that allocated CPU cycles and |
| 460 | * move to BLOCKED state. If the CPU cycles were allocated to the current |
| 461 | * execution context by the SPMC to handle secure virtual interrupt, then |
| 462 | * FFA_YIELD invocation is essentially a no-op. |
| 463 | */ |
Karl Meakin | 117c808 | 2024-12-04 16:03:28 +0000 | [diff] [blame] | 464 | struct ffa_value ffa_cpu_cycles_yield_prepare(struct vcpu_locked current_locked, |
| 465 | struct vcpu **next, |
| 466 | uint32_t timeout_low, |
| 467 | uint32_t timeout_high) |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 468 | { |
| 469 | struct ffa_value ret_args = (struct ffa_value){.func = FFA_SUCCESS_32}; |
| 470 | struct vcpu *current = current_locked.vcpu; |
| 471 | struct ffa_value ret = { |
| 472 | .func = FFA_YIELD_32, |
| 473 | .arg1 = ffa_vm_vcpu(current->vm->id, vcpu_index(current)), |
| 474 | .arg2 = timeout_low, |
| 475 | .arg3 = timeout_high, |
| 476 | }; |
| 477 | |
| 478 | switch (current->rt_model) { |
| 479 | case RTM_FFA_DIR_REQ: |
| 480 | assert(current->direct_request_origin.vm_id != |
| 481 | HF_INVALID_VM_ID); |
| 482 | if (current->call_chain.prev_node == NULL) { |
| 483 | /* |
| 484 | * Relinquish cycles to the NWd VM that sent direct |
| 485 | * request message to the current SP. |
| 486 | */ |
| 487 | *next = api_switch_to_other_world(current_locked, ret, |
| 488 | VCPU_STATE_BLOCKED); |
| 489 | } else { |
| 490 | /* |
| 491 | * Relinquish cycles to the SP that sent direct request |
| 492 | * message to the current SP. |
| 493 | */ |
| 494 | *next = api_switch_to_vm( |
| 495 | current_locked, ret, VCPU_STATE_BLOCKED, |
| 496 | current->direct_request_origin.vm_id); |
| 497 | } |
| 498 | break; |
| 499 | case RTM_SEC_INTERRUPT: { |
| 500 | /* |
| 501 | * SPMC does not implement a scheduler needed to resume the |
| 502 | * current vCPU upon timeout expiration. Hence, SPMC makes the |
| 503 | * implementation defined choice to treat FFA_YIELD invocation |
| 504 | * as a no-op if the SP execution context is in the secure |
| 505 | * interrupt runtime model. This does not violate FF-A spec as |
| 506 | * the spec does not mandate timeout to be honored. Moreover, |
| 507 | * timeout specified by an endpoint is just a hint to the |
| 508 | * partition manager which allocated CPU cycles. |
| 509 | * Resume the current vCPU. |
| 510 | */ |
| 511 | *next = NULL; |
| 512 | break; |
| 513 | } |
| 514 | default: |
| 515 | CHECK(current->rt_model == RTM_FFA_RUN); |
| 516 | *next = api_switch_to_primary(current_locked, ret, |
| 517 | VCPU_STATE_BLOCKED); |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Before yielding CPU cycles, allow the interrupts(if they were |
| 523 | * masked earlier). |
| 524 | */ |
| 525 | if (*next != NULL) { |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 526 | ffa_interrupts_unmask(current); |
Karl Meakin | 5a365d3 | 2024-11-08 23:55:03 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return ret_args; |
| 530 | } |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 531 | |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 532 | /** |
| 533 | * Validates the Runtime model for FFA_RUN. Refer to section 7.2 of the FF-A |
| 534 | * v1.1 EAC0 spec. |
| 535 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 536 | static bool ffa_cpu_cycles_check_rtm_ffa_run(struct vcpu_locked current_locked, |
| 537 | struct vcpu_locked locked_vcpu, |
| 538 | uint32_t func, |
| 539 | enum vcpu_state *next_state) |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 540 | { |
| 541 | switch (func) { |
| 542 | case FFA_MSG_SEND_DIRECT_REQ_64: |
| 543 | case FFA_MSG_SEND_DIRECT_REQ_32: |
| 544 | case FFA_MSG_SEND_DIRECT_REQ2_64: |
Karl Meakin | 402b1fe | 2025-03-20 14:52:55 +0000 | [diff] [blame^] | 545 | [[fallthrough]]; |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 546 | case FFA_RUN_32: { |
| 547 | /* Rules 1,2 section 7.2 EAC0 spec. */ |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 548 | if (ffa_direct_msg_precedes_in_call_chain(current_locked, |
| 549 | locked_vcpu)) { |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 550 | return false; |
| 551 | } |
| 552 | *next_state = VCPU_STATE_BLOCKED; |
| 553 | return true; |
| 554 | } |
| 555 | case FFA_MSG_WAIT_32: |
| 556 | /* Rule 4 section 7.2 EAC0 spec. Fall through. */ |
| 557 | *next_state = VCPU_STATE_WAITING; |
| 558 | return true; |
| 559 | case FFA_YIELD_32: |
| 560 | /* Rule 5 section 7.2 EAC0 spec. */ |
| 561 | *next_state = VCPU_STATE_BLOCKED; |
| 562 | return true; |
| 563 | case FFA_MSG_SEND_DIRECT_RESP_64: |
| 564 | case FFA_MSG_SEND_DIRECT_RESP_32: |
| 565 | case FFA_MSG_SEND_DIRECT_RESP2_64: |
| 566 | /* Rule 3 section 7.2 EAC0 spec. Fall through. */ |
| 567 | default: |
| 568 | /* Deny state transitions by default. */ |
| 569 | return false; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Validates the Runtime model for FFA_MSG_SEND_DIRECT_REQ and |
| 575 | * FFA_MSG_SEND_DIRECT_REQ2. Refer to section 8.3 of the FF-A |
| 576 | * v1.2 spec. |
| 577 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 578 | static bool ffa_cpu_cycles_check_rtm_ffa_dir_req( |
| 579 | struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu, |
| 580 | ffa_id_t receiver_vm_id, uint32_t func, enum vcpu_state *next_state) |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 581 | { |
Madhukar Pappireddy | 454b91d | 2025-02-10 13:05:46 -0600 | [diff] [blame] | 582 | /* |
| 583 | * SPMC denies invocation if the SP's vCPU is processing a PSCI power |
| 584 | * management operation. |
| 585 | */ |
| 586 | if (current_locked.vcpu->pwr_mgmt_op != PWR_MGMT_NONE) { |
| 587 | switch (func) { |
| 588 | case FFA_MSG_SEND_DIRECT_REQ_64: |
| 589 | case FFA_MSG_SEND_DIRECT_REQ_32: |
| 590 | case FFA_MSG_SEND_DIRECT_REQ2_64: |
| 591 | case FFA_RUN_32: |
| 592 | case FFA_YIELD_32: |
| 593 | dlog_verbose( |
| 594 | "State transition denied during power " |
| 595 | "management operation\n"); |
| 596 | return false; |
| 597 | default: |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 602 | switch (func) { |
| 603 | case FFA_MSG_SEND_DIRECT_REQ_64: |
| 604 | case FFA_MSG_SEND_DIRECT_REQ_32: |
| 605 | case FFA_MSG_SEND_DIRECT_REQ2_64: |
Karl Meakin | 402b1fe | 2025-03-20 14:52:55 +0000 | [diff] [blame^] | 606 | [[fallthrough]]; |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 607 | case FFA_RUN_32: { |
| 608 | /* Rules 1,2. */ |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 609 | if (ffa_direct_msg_precedes_in_call_chain(current_locked, |
| 610 | locked_vcpu)) { |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | |
| 614 | *next_state = VCPU_STATE_BLOCKED; |
| 615 | return true; |
| 616 | } |
| 617 | case FFA_MSG_SEND_DIRECT_RESP_64: |
Karl Meakin | 402b1fe | 2025-03-20 14:52:55 +0000 | [diff] [blame^] | 618 | case FFA_MSG_SEND_DIRECT_RESP_32: |
| 619 | case FFA_MSG_SEND_DIRECT_RESP2_64: { |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 620 | /* Rule 3. */ |
| 621 | if (current_locked.vcpu->direct_request_origin.vm_id == |
| 622 | receiver_vm_id) { |
| 623 | *next_state = VCPU_STATE_WAITING; |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | return false; |
| 628 | } |
| 629 | case FFA_YIELD_32: |
| 630 | /* Rule 3, section 8.3 of FF-A v1.2 spec. */ |
| 631 | *next_state = VCPU_STATE_BLOCKED; |
| 632 | return true; |
| 633 | case FFA_MSG_WAIT_32: |
| 634 | /* Rule 4. Fall through. */ |
| 635 | default: |
| 636 | /* Deny state transitions by default. */ |
| 637 | return false; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Validates the Runtime model for Secure interrupt handling. Refer to section |
| 643 | * 8.4 of the FF-A v1.2 ALP0 spec. |
| 644 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 645 | static bool ffa_cpu_cycles_check_rtm_sec_interrupt( |
| 646 | struct vcpu_locked current_locked, struct vcpu_locked locked_vcpu, |
| 647 | uint32_t func, enum vcpu_state *next_state) |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 648 | { |
| 649 | struct vcpu *current = current_locked.vcpu; |
| 650 | struct vcpu *vcpu = locked_vcpu.vcpu; |
| 651 | |
| 652 | CHECK(current->scheduling_mode == SPMC_MODE); |
| 653 | |
| 654 | switch (func) { |
| 655 | case FFA_MSG_SEND_DIRECT_REQ_64: |
| 656 | case FFA_MSG_SEND_DIRECT_REQ_32: |
| 657 | case FFA_MSG_SEND_DIRECT_REQ2_64: |
| 658 | /* Rule 3. */ |
| 659 | *next_state = VCPU_STATE_BLOCKED; |
| 660 | return true; |
| 661 | case FFA_RUN_32: { |
| 662 | /* Rule 6. */ |
| 663 | if (vcpu->state == VCPU_STATE_PREEMPTED) { |
| 664 | *next_state = VCPU_STATE_BLOCKED; |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | return false; |
| 669 | } |
| 670 | case FFA_MSG_WAIT_32: |
| 671 | /* Rule 2. */ |
| 672 | *next_state = VCPU_STATE_WAITING; |
| 673 | return true; |
| 674 | case FFA_YIELD_32: |
| 675 | /* Rule 3, section 8.4 of FF-A v1.2 spec. */ |
| 676 | *next_state = VCPU_STATE_BLOCKED; |
| 677 | return true; |
| 678 | case FFA_MSG_SEND_DIRECT_RESP_64: |
| 679 | case FFA_MSG_SEND_DIRECT_RESP_32: |
| 680 | case FFA_MSG_SEND_DIRECT_RESP2_64: |
| 681 | /* Rule 5. Fall through. */ |
| 682 | default: |
| 683 | /* Deny state transitions by default. */ |
| 684 | return false; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Validates the Runtime model for SP initialization. Refer to section |
| 690 | * 8.3 of the FF-A v1.2 ALP0 spec. |
| 691 | */ |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 692 | static bool ffa_cpu_cycles_check_rtm_sp_init(struct vcpu_locked locked_vcpu, |
| 693 | uint32_t func, |
| 694 | enum vcpu_state *next_state) |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 695 | { |
| 696 | switch (func) { |
| 697 | case FFA_MSG_SEND_DIRECT_REQ_64: |
| 698 | case FFA_MSG_SEND_DIRECT_REQ_32: |
| 699 | case FFA_MSG_SEND_DIRECT_REQ2_64: { |
| 700 | struct vcpu *vcpu = locked_vcpu.vcpu; |
| 701 | |
| 702 | assert(vcpu != NULL); |
| 703 | /* Rule 1. */ |
| 704 | if (vcpu->rt_model != RTM_SP_INIT) { |
| 705 | *next_state = VCPU_STATE_BLOCKED; |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | return false; |
| 710 | } |
| 711 | case FFA_MSG_WAIT_32: |
| 712 | /* Rule 2. Fall through. */ |
| 713 | case FFA_ERROR_32: |
| 714 | /* Rule 3. */ |
| 715 | *next_state = VCPU_STATE_WAITING; |
| 716 | return true; |
| 717 | case FFA_YIELD_32: |
| 718 | /* Rule 4. Fall through. */ |
| 719 | case FFA_RUN_32: |
| 720 | /* Rule 6. Fall through. */ |
| 721 | case FFA_MSG_SEND_DIRECT_RESP_64: |
| 722 | case FFA_MSG_SEND_DIRECT_RESP_32: |
| 723 | case FFA_MSG_SEND_DIRECT_RESP2_64: |
| 724 | /* Rule 5. Fall through. */ |
| 725 | default: |
| 726 | /* Deny state transitions by default. */ |
| 727 | return false; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Check if the runtime model (state machine) of the current SP supports the |
| 733 | * given FF-A ABI invocation. If yes, next_state represents the state to which |
| 734 | * the current vcpu would transition upon the FF-A ABI invocation as determined |
| 735 | * by the Partition runtime model. |
| 736 | */ |
| 737 | bool ffa_cpu_cycles_check_runtime_state_transition( |
| 738 | struct vcpu_locked current_locked, ffa_id_t vm_id, |
| 739 | ffa_id_t receiver_vm_id, struct vcpu_locked locked_vcpu, uint32_t func, |
| 740 | enum vcpu_state *next_state) |
| 741 | { |
| 742 | bool allowed = false; |
| 743 | struct vcpu *current = current_locked.vcpu; |
| 744 | |
| 745 | assert(current != NULL); |
| 746 | |
| 747 | /* Perform state transition checks only for Secure Partitions. */ |
| 748 | if (!vm_id_is_current_world(vm_id)) { |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | switch (current->rt_model) { |
| 753 | case RTM_FFA_RUN: |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 754 | allowed = ffa_cpu_cycles_check_rtm_ffa_run( |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 755 | current_locked, locked_vcpu, func, next_state); |
| 756 | break; |
| 757 | case RTM_FFA_DIR_REQ: |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 758 | allowed = ffa_cpu_cycles_check_rtm_ffa_dir_req( |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 759 | current_locked, locked_vcpu, receiver_vm_id, func, |
| 760 | next_state); |
| 761 | break; |
| 762 | case RTM_SEC_INTERRUPT: |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 763 | allowed = ffa_cpu_cycles_check_rtm_sec_interrupt( |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 764 | current_locked, locked_vcpu, func, next_state); |
| 765 | break; |
| 766 | case RTM_SP_INIT: |
Karl Meakin | ca38ef9 | 2025-02-13 14:20:23 +0000 | [diff] [blame] | 767 | allowed = ffa_cpu_cycles_check_rtm_sp_init(locked_vcpu, func, |
| 768 | next_state); |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 769 | break; |
| 770 | default: |
| 771 | dlog_error( |
| 772 | "Illegal Runtime Model specified by SP%x on CPU%zx\n", |
| 773 | current->vm->id, cpu_index(current->cpu)); |
| 774 | allowed = false; |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | if (!allowed) { |
| 779 | dlog_verbose("State transition denied\n"); |
| 780 | } |
| 781 | |
| 782 | return allowed; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Handle FFA_ERROR_32 call according to the given error code. |
| 787 | * |
| 788 | * Error codes other than FFA_ABORTED, and cases of FFA_ABORTED not |
| 789 | * in RTM_SP_INIT runtime model, not implemented. Refer to section 8.5 |
| 790 | * of FF-A 1.2 spec. |
| 791 | */ |
Karl Meakin | fa1dcb8 | 2025-02-10 16:47:50 +0000 | [diff] [blame] | 792 | struct ffa_value ffa_cpu_cycles_error_32(struct vcpu *current, |
| 793 | struct vcpu **next, |
| 794 | enum ffa_error error_code) |
Karl Meakin | 936ec1e | 2025-01-31 13:17:11 +0000 | [diff] [blame] | 795 | { |
| 796 | struct vcpu_locked current_locked; |
| 797 | struct vm_locked vm_locked; |
| 798 | enum partition_runtime_model rt_model; |
| 799 | struct ffa_value ret = api_ffa_interrupt_return(0); |
| 800 | |
| 801 | vm_locked = vm_lock(current->vm); |
| 802 | current_locked = vcpu_lock(current); |
| 803 | rt_model = current_locked.vcpu->rt_model; |
| 804 | |
| 805 | if (error_code == FFA_ABORTED && rt_model == RTM_SP_INIT) { |
| 806 | dlog_error("Aborting SP %#x from vCPU %u\n", current->vm->id, |
| 807 | vcpu_index(current)); |
| 808 | |
| 809 | atomic_store_explicit(¤t->vm->aborting, true, |
| 810 | memory_order_relaxed); |
| 811 | |
| 812 | ffa_vm_free_resources(vm_locked); |
| 813 | |
| 814 | if (sp_boot_next(current_locked, next)) { |
| 815 | goto out; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Relinquish control back to the NWd. Return |
| 820 | * FFA_MSG_WAIT_32 to indicate to SPMD that SPMC |
| 821 | * has successfully finished initialization. |
| 822 | */ |
| 823 | *next = api_switch_to_other_world( |
| 824 | current_locked, |
| 825 | (struct ffa_value){.func = FFA_MSG_WAIT_32}, |
| 826 | VCPU_STATE_ABORTED); |
| 827 | |
| 828 | goto out; |
| 829 | } |
| 830 | ret = ffa_error(FFA_NOT_SUPPORTED); |
| 831 | out: |
| 832 | vcpu_unlock(¤t_locked); |
| 833 | vm_unlock(&vm_locked); |
| 834 | return ret; |
| 835 | } |