Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 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. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "hf/addr.h" |
Daniel Boulby | 801f8ef | 2022-06-27 14:21:01 +0100 | [diff] [blame] | 12 | #include "hf/interrupt_desc.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 13 | #include "hf/spinlock.h" |
| 14 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 15 | #include "vmapi/hf/ffa.h" |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 16 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 17 | enum vcpu_state { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 18 | /** The vCPU is switched off. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 19 | VCPU_STATE_OFF, |
| 20 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 21 | /** The vCPU is currently running. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 22 | VCPU_STATE_RUNNING, |
| 23 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 24 | /** The vCPU is waiting to be allocated CPU cycles to do work. */ |
| 25 | VCPU_STATE_WAITING, |
| 26 | |
| 27 | /** |
| 28 | * The vCPU is blocked and waiting for some work to complete on |
| 29 | * its behalf. |
| 30 | */ |
| 31 | VCPU_STATE_BLOCKED, |
| 32 | |
| 33 | /** The vCPU has been preempted by an interrupt. */ |
| 34 | VCPU_STATE_PREEMPTED, |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 35 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 36 | /** The vCPU is waiting for an interrupt. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 37 | VCPU_STATE_BLOCKED_INTERRUPT, |
| 38 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 39 | /** The vCPU has aborted. */ |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 40 | VCPU_STATE_ABORTED, |
| 41 | }; |
| 42 | |
Madhukar Pappireddy | fe297a3 | 2022-06-21 16:42:13 -0500 | [diff] [blame] | 43 | /** Refer to section 7 of the FF-A v1.1 EAC0 spec. */ |
| 44 | enum partition_runtime_model { |
| 45 | RTM_NONE, |
| 46 | /** Runtime model for FFA_RUN. */ |
| 47 | RTM_FFA_RUN, |
| 48 | /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */ |
| 49 | RTM_FFA_DIR_REQ, |
| 50 | /** Runtime model for Secure Interrupt handling. */ |
| 51 | RTM_SEC_INTERRUPT, |
| 52 | /** Runtime model for SP Initialization. */ |
| 53 | RTM_SP_INIT, |
| 54 | }; |
| 55 | |
Madhukar Pappireddy | 5992fbc | 2022-06-21 17:15:16 -0500 | [diff] [blame^] | 56 | /** Refer to section 8.2.3 of the FF-A EAC0 spec. */ |
| 57 | enum schedule_mode { |
| 58 | NONE, |
| 59 | /** Normal world scheduled mode. */ |
| 60 | NWD_MODE, |
| 61 | /** SPMC scheduled mode. */ |
| 62 | SPMC_MODE, |
| 63 | }; |
| 64 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 65 | struct interrupts { |
| 66 | /** Bitfield keeping track of which interrupts are enabled. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 67 | struct interrupt_bitmap interrupt_enabled; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 68 | /** Bitfield keeping track of which interrupts are pending. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 69 | struct interrupt_bitmap interrupt_pending; |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 70 | /** Bitfield recording the interrupt pin configuration. */ |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 71 | struct interrupt_bitmap interrupt_type; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 72 | /** |
| 73 | * The number of interrupts which are currently both enabled and |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 74 | * pending. Count independently virtual IRQ and FIQ interrupt types |
| 75 | * i.e. the sum of the two counters is the number of bits set in |
| 76 | * interrupt_enable & interrupt_pending. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 77 | */ |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 78 | uint32_t enabled_and_pending_irq_count; |
| 79 | uint32_t enabled_and_pending_fiq_count; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct vcpu_fault_info { |
| 83 | ipaddr_t ipaddr; |
| 84 | vaddr_t vaddr; |
| 85 | vaddr_t pc; |
| 86 | uint32_t mode; |
| 87 | }; |
| 88 | |
Madhukar Pappireddy | 5992fbc | 2022-06-21 17:15:16 -0500 | [diff] [blame^] | 89 | struct call_chain { |
| 90 | /** Previous node in the SP call chain. */ |
| 91 | struct vcpu *prev_node; |
| 92 | |
| 93 | /** Next node in the SP call chain. */ |
| 94 | struct vcpu *next_node; |
| 95 | }; |
| 96 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 97 | struct vcpu { |
| 98 | struct spinlock lock; |
| 99 | |
| 100 | /* |
| 101 | * The state is only changed in the context of the vCPU being run. This |
| 102 | * ensures the scheduler can easily keep track of the vCPU state as |
| 103 | * transitions are indicated by the return code from the run call. |
| 104 | */ |
| 105 | enum vcpu_state state; |
| 106 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 107 | bool is_bootstrapped; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 108 | struct cpu *cpu; |
| 109 | struct vm *vm; |
| 110 | struct arch_regs regs; |
| 111 | struct interrupts interrupts; |
| 112 | |
| 113 | /* |
| 114 | * Determine whether the 'regs' field is available for use. This is set |
| 115 | * to false when a vCPU is about to run on a physical CPU, and is set |
Olivier Deprez | 3caed1c | 2021-02-05 12:07:36 +0100 | [diff] [blame] | 116 | * back to true when it is descheduled. This is not relevant for the |
| 117 | * primary VM vCPUs in the normal world (or the "other world VM" vCPUs |
| 118 | * in the secure world) as they are pinned to physical CPUs and there |
| 119 | * is no contention to take care of. |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 120 | */ |
| 121 | bool regs_available; |
Olivier Deprez | ee9d6a9 | 2019-11-26 09:14:11 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * If the current vCPU is executing as a consequence of a |
| 125 | * FFA_MSG_SEND_DIRECT_REQ invocation, then this member holds the |
| 126 | * originating VM ID from which the call originated. |
| 127 | * The value HF_INVALID_VM_ID implies the vCPU is not executing as |
| 128 | * a result of a prior FFA_MSG_SEND_DIRECT_REQ invocation. |
| 129 | */ |
| 130 | ffa_vm_id_t direct_request_origin_vm_id; |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 131 | |
Madhukar Pappireddy | b11e0d1 | 2021-08-02 19:44:35 -0500 | [diff] [blame] | 132 | /** Determine whether partition is currently handling managed exit. */ |
Manish Pandey | a5f39fb | 2020-09-11 09:47:11 +0100 | [diff] [blame] | 133 | bool processing_managed_exit; |
Madhukar Pappireddy | f675bb6 | 2021-08-03 12:57:10 -0500 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Determine whether vCPU is currently handling secure interrupt. |
| 137 | */ |
| 138 | bool processing_secure_interrupt; |
| 139 | bool secure_interrupt_deactivated; |
| 140 | |
| 141 | /** |
| 142 | * INTID of the current secure interrupt being processed by this vCPU. |
| 143 | */ |
| 144 | uint32_t current_sec_interrupt_id; |
| 145 | |
| 146 | /** |
| 147 | * Track current vCPU which got pre-empted when secure interrupt |
| 148 | * triggered. |
| 149 | */ |
| 150 | struct vcpu *preempted_vcpu; |
Madhukar Pappireddy | dd6fdfb | 2021-12-14 12:30:36 -0600 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Current value of the Priority Mask register which is saved/restored |
| 154 | * during secure interrupt handling. |
| 155 | */ |
| 156 | uint8_t priority_mask; |
Madhukar Pappireddy | 0aaadbb | 2021-12-16 20:58:10 -0600 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple |
| 160 | * mechanisms to signal completion of secure interrupt handling. SP |
| 161 | * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN, |
| 162 | * when in WAITING/BLOCKED state respectively, but has to perform |
| 163 | * implicit signal completion mechanism by dropping the priority |
| 164 | * of the virtual secure interrupt when SPMC signaled the virtual |
| 165 | * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int |
| 166 | * while running). This variable helps SPMC to keep a track of such |
| 167 | * mechanism and perform appropriate bookkeeping. |
| 168 | */ |
| 169 | bool implicit_completion_signal; |
Madhukar Pappireddy | fe297a3 | 2022-06-21 16:42:13 -0500 | [diff] [blame] | 170 | |
Madhukar Pappireddy | 5992fbc | 2022-06-21 17:15:16 -0500 | [diff] [blame^] | 171 | /** SP call chain. */ |
| 172 | struct call_chain call_chain; |
| 173 | |
| 174 | /** |
| 175 | * Indicates if the current vCPU is running in SPMC scheduled |
| 176 | * mode or Normal World scheduled mode. |
| 177 | */ |
| 178 | enum schedule_mode scheduling_mode; |
| 179 | |
Madhukar Pappireddy | fe297a3 | 2022-06-21 16:42:13 -0500 | [diff] [blame] | 180 | /** Partition Runtime Model. */ |
| 181 | enum partition_runtime_model rt_model; |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /** Encapsulates a vCPU whose lock is held. */ |
| 185 | struct vcpu_locked { |
| 186 | struct vcpu *vcpu; |
| 187 | }; |
| 188 | |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 189 | /** Container for two vcpu_locked structures. */ |
| 190 | struct two_vcpu_locked { |
| 191 | struct vcpu_locked vcpu1; |
| 192 | struct vcpu_locked vcpu2; |
| 193 | }; |
| 194 | |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 195 | struct vcpu_locked vcpu_lock(struct vcpu *vcpu); |
Olivier Deprez | 0b6f10a | 2020-08-05 18:21:33 +0200 | [diff] [blame] | 196 | struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 197 | void vcpu_unlock(struct vcpu_locked *locked); |
| 198 | void vcpu_init(struct vcpu *vcpu, struct vm *vm); |
| 199 | void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 200 | ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 201 | bool vcpu_is_off(struct vcpu_locked vcpu); |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 202 | bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked, |
| 203 | ipaddr_t entry, uintreg_t arg); |
Fuad Tabba | 5c73843 | 2019-12-02 11:02:42 +0000 | [diff] [blame] | 204 | |
| 205 | bool vcpu_handle_page_fault(const struct vcpu *current, |
| 206 | struct vcpu_fault_info *f); |
Olivier Deprez | 2ebae3a | 2020-06-11 16:34:30 +0200 | [diff] [blame] | 207 | |
Olivier Deprez | e6f7b9d | 2021-02-01 11:55:48 +0100 | [diff] [blame] | 208 | void vcpu_reset(struct vcpu *vcpu); |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 209 | |
J-Alves | 7ac4905 | 2022-02-08 17:20:53 +0000 | [diff] [blame] | 210 | void vcpu_set_phys_core_idx(struct vcpu *vcpu); |
| 211 | |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 212 | static inline bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts, |
| 213 | uint32_t intid) |
| 214 | { |
| 215 | return interrupt_bitmap_get_value(&interrupts->interrupt_enabled, |
| 216 | intid) == 1U; |
| 217 | } |
| 218 | |
| 219 | static inline void vcpu_virt_interrupt_set_enabled( |
| 220 | struct interrupts *interrupts, uint32_t intid) |
| 221 | { |
| 222 | interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid); |
| 223 | } |
| 224 | |
| 225 | static inline void vcpu_virt_interrupt_clear_enabled( |
| 226 | struct interrupts *interrupts, uint32_t intid) |
| 227 | { |
| 228 | interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid); |
| 229 | } |
| 230 | |
| 231 | static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts, |
| 232 | uint32_t intid) |
| 233 | { |
| 234 | return interrupt_bitmap_get_value(&interrupts->interrupt_pending, |
| 235 | intid) == 1U; |
| 236 | } |
| 237 | |
| 238 | static inline void vcpu_virt_interrupt_set_pending( |
| 239 | struct interrupts *interrupts, uint32_t intid) |
| 240 | { |
| 241 | interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid); |
| 242 | } |
| 243 | |
| 244 | static inline void vcpu_virt_interrupt_clear_pending( |
| 245 | struct interrupts *interrupts, uint32_t intid) |
| 246 | { |
| 247 | interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid); |
| 248 | } |
| 249 | |
| 250 | static inline enum interrupt_type vcpu_virt_interrupt_get_type( |
| 251 | struct interrupts *interrupts, uint32_t intid) |
| 252 | { |
| 253 | return (enum interrupt_type)interrupt_bitmap_get_value( |
| 254 | &interrupts->interrupt_type, intid); |
| 255 | } |
| 256 | |
| 257 | static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts, |
| 258 | uint32_t intid, |
| 259 | enum interrupt_type type) |
| 260 | { |
| 261 | if (type == INTERRUPT_TYPE_IRQ) { |
| 262 | interrupt_bitmap_clear_value(&interrupts->interrupt_type, |
| 263 | intid); |
| 264 | } else { |
| 265 | interrupt_bitmap_set_value(&interrupts->interrupt_type, intid); |
| 266 | } |
| 267 | } |
| 268 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 269 | static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked) |
| 270 | { |
| 271 | vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++; |
| 272 | } |
| 273 | |
| 274 | static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked) |
| 275 | { |
| 276 | vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--; |
| 277 | } |
| 278 | |
| 279 | static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked) |
| 280 | { |
| 281 | vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++; |
| 282 | } |
| 283 | |
| 284 | static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked) |
| 285 | { |
| 286 | vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--; |
| 287 | } |
| 288 | |
Daniel Boulby | 4ca50f0 | 2022-07-29 18:29:34 +0100 | [diff] [blame] | 289 | static inline void vcpu_interrupt_count_increment( |
| 290 | struct vcpu_locked vcpu_locked, struct interrupts *interrupts, |
| 291 | uint32_t intid) |
| 292 | { |
| 293 | if (vcpu_virt_interrupt_get_type(interrupts, intid) == |
| 294 | INTERRUPT_TYPE_IRQ) { |
| 295 | vcpu_irq_count_increment(vcpu_locked); |
| 296 | } else { |
| 297 | vcpu_fiq_count_increment(vcpu_locked); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static inline void vcpu_interrupt_count_decrement( |
| 302 | struct vcpu_locked vcpu_locked, struct interrupts *interrupts, |
| 303 | uint32_t intid) |
| 304 | { |
| 305 | if (vcpu_virt_interrupt_get_type(interrupts, intid) == |
| 306 | INTERRUPT_TYPE_IRQ) { |
| 307 | vcpu_irq_count_decrement(vcpu_locked); |
| 308 | } else { |
| 309 | vcpu_fiq_count_decrement(vcpu_locked); |
| 310 | } |
| 311 | } |
| 312 | |
Manish Pandey | 35e452f | 2021-02-18 21:36:34 +0000 | [diff] [blame] | 313 | static inline uint32_t vcpu_interrupt_irq_count_get( |
| 314 | struct vcpu_locked vcpu_locked) |
| 315 | { |
| 316 | return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count; |
| 317 | } |
| 318 | |
| 319 | static inline uint32_t vcpu_interrupt_fiq_count_get( |
| 320 | struct vcpu_locked vcpu_locked) |
| 321 | { |
| 322 | return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count; |
| 323 | } |
| 324 | |
| 325 | static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked) |
| 326 | { |
| 327 | return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count + |
| 328 | vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count; |
| 329 | } |
Madhukar Pappireddy | 5992fbc | 2022-06-21 17:15:16 -0500 | [diff] [blame^] | 330 | |
| 331 | static inline void vcpu_call_chain_extend(struct vcpu *vcpu1, |
| 332 | struct vcpu *vcpu2) |
| 333 | { |
| 334 | vcpu1->call_chain.next_node = vcpu2; |
| 335 | vcpu2->call_chain.prev_node = vcpu1; |
| 336 | } |
| 337 | |
| 338 | static inline void vcpu_call_chain_remove_node(struct vcpu *vcpu1, |
| 339 | struct vcpu *vcpu2) |
| 340 | { |
| 341 | vcpu1->call_chain.prev_node = NULL; |
| 342 | vcpu2->call_chain.next_node = NULL; |
| 343 | } |