blob: f39b61ef591df254d0bb15af89407563337e8f82 [file] [log] [blame]
Fuad Tabba5c738432019-12-02 11:02:42 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Tabba5c738432019-12-02 11:02:42 +00007 */
8
9#pragma once
10
Olivier Deprezc5203fb2022-09-29 13:49:24 +020011#include "hf/arch/types.h"
12
Fuad Tabba5c738432019-12-02 11:02:42 +000013#include "hf/addr.h"
Daniel Boulby801f8ef2022-06-27 14:21:01 +010014#include "hf/interrupt_desc.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000015#include "hf/spinlock.h"
16
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010017#include "vmapi/hf/ffa.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000018
Madhukar Pappireddy84154052022-06-21 18:30:25 -050019/** Action for non secure interrupt by SPMC. */
20#define NS_ACTION_QUEUED 0
21#define NS_ACTION_ME 1
22#define NS_ACTION_SIGNALED 2
Madhukar Pappireddy84154052022-06-21 18:30:25 -050023
Fuad Tabba5c738432019-12-02 11:02:42 +000024enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000025 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000026 VCPU_STATE_OFF,
27
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000028 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000029 VCPU_STATE_RUNNING,
30
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050031 /** The vCPU is waiting to be allocated CPU cycles to do work. */
32 VCPU_STATE_WAITING,
33
34 /**
35 * The vCPU is blocked and waiting for some work to complete on
36 * its behalf.
37 */
38 VCPU_STATE_BLOCKED,
39
40 /** The vCPU has been preempted by an interrupt. */
41 VCPU_STATE_PREEMPTED,
Fuad Tabba5c738432019-12-02 11:02:42 +000042
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000043 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000044 VCPU_STATE_BLOCKED_INTERRUPT,
45
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000046 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000047 VCPU_STATE_ABORTED,
48};
49
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -050050/** Refer to section 7 of the FF-A v1.1 EAC0 spec. */
51enum partition_runtime_model {
52 RTM_NONE,
53 /** Runtime model for FFA_RUN. */
54 RTM_FFA_RUN,
55 /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */
56 RTM_FFA_DIR_REQ,
57 /** Runtime model for Secure Interrupt handling. */
58 RTM_SEC_INTERRUPT,
59 /** Runtime model for SP Initialization. */
60 RTM_SP_INIT,
61};
62
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050063/** Refer to section 8.2.3 of the FF-A EAC0 spec. */
64enum schedule_mode {
65 NONE,
66 /** Normal world scheduled mode. */
67 NWD_MODE,
68 /** SPMC scheduled mode. */
69 SPMC_MODE,
70};
71
Fuad Tabba5c738432019-12-02 11:02:42 +000072struct interrupts {
73 /** Bitfield keeping track of which interrupts are enabled. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010074 struct interrupt_bitmap interrupt_enabled;
Fuad Tabba5c738432019-12-02 11:02:42 +000075 /** Bitfield keeping track of which interrupts are pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010076 struct interrupt_bitmap interrupt_pending;
Manish Pandey35e452f2021-02-18 21:36:34 +000077 /** Bitfield recording the interrupt pin configuration. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010078 struct interrupt_bitmap interrupt_type;
Fuad Tabba5c738432019-12-02 11:02:42 +000079 /**
80 * The number of interrupts which are currently both enabled and
Manish Pandey35e452f2021-02-18 21:36:34 +000081 * pending. Count independently virtual IRQ and FIQ interrupt types
82 * i.e. the sum of the two counters is the number of bits set in
83 * interrupt_enable & interrupt_pending.
Fuad Tabba5c738432019-12-02 11:02:42 +000084 */
Manish Pandey35e452f2021-02-18 21:36:34 +000085 uint32_t enabled_and_pending_irq_count;
86 uint32_t enabled_and_pending_fiq_count;
Fuad Tabba5c738432019-12-02 11:02:42 +000087};
88
89struct vcpu_fault_info {
90 ipaddr_t ipaddr;
91 vaddr_t vaddr;
92 vaddr_t pc;
93 uint32_t mode;
94};
95
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050096struct call_chain {
97 /** Previous node in the SP call chain. */
98 struct vcpu *prev_node;
99
100 /** Next node in the SP call chain. */
101 struct vcpu *next_node;
102};
103
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100104#define LOG_BUFFER_SIZE 256
105
106struct log_buffer {
107 char chars[LOG_BUFFER_SIZE];
108 uint16_t len;
109};
110
Fuad Tabba5c738432019-12-02 11:02:42 +0000111struct vcpu {
112 struct spinlock lock;
113
114 /*
115 * The state is only changed in the context of the vCPU being run. This
116 * ensures the scheduler can easily keep track of the vCPU state as
117 * transitions are indicated by the return code from the run call.
118 */
119 enum vcpu_state state;
120
121 struct cpu *cpu;
122 struct vm *vm;
123 struct arch_regs regs;
124 struct interrupts interrupts;
125
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100126 struct log_buffer log_buffer;
127
Fuad Tabba5c738432019-12-02 11:02:42 +0000128 /*
129 * Determine whether the 'regs' field is available for use. This is set
130 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +0100131 * back to true when it is descheduled. This is not relevant for the
132 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
133 * in the secure world) as they are pinned to physical CPUs and there
134 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000135 */
136 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000137
138 /*
139 * If the current vCPU is executing as a consequence of a
Kathleen Capellae468c112023-12-13 17:56:28 -0500140 * direct request invocation, then this member holds the
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000141 * originating VM ID from which the call originated.
142 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
Kathleen Capellae468c112023-12-13 17:56:28 -0500143 * a result of a prior direct request invocation.
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000144 */
Kathleen Capellae468c112023-12-13 17:56:28 -0500145 struct {
146 /**
147 * Indicate whether request is via FFA_MSG_SEND_DIRECT_REQ2.
148 */
149 bool is_ffa_req2;
150 ffa_id_t vm_id;
151 } direct_request_origin;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100152
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500153 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100154 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500155
156 /**
157 * Determine whether vCPU is currently handling secure interrupt.
158 */
159 bool processing_secure_interrupt;
160 bool secure_interrupt_deactivated;
161
162 /**
163 * INTID of the current secure interrupt being processed by this vCPU.
164 */
165 uint32_t current_sec_interrupt_id;
166
167 /**
168 * Track current vCPU which got pre-empted when secure interrupt
169 * triggered.
170 */
171 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600172
173 /**
174 * Current value of the Priority Mask register which is saved/restored
175 * during secure interrupt handling.
176 */
177 uint8_t priority_mask;
Madhukar Pappireddy0aaadbb2021-12-16 20:58:10 -0600178
179 /**
180 * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple
181 * mechanisms to signal completion of secure interrupt handling. SP
182 * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN,
183 * when in WAITING/BLOCKED state respectively, but has to perform
184 * implicit signal completion mechanism by dropping the priority
185 * of the virtual secure interrupt when SPMC signaled the virtual
186 * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int
187 * while running). This variable helps SPMC to keep a track of such
188 * mechanism and perform appropriate bookkeeping.
189 */
190 bool implicit_completion_signal;
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500191
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500192 /** SP call chain. */
193 struct call_chain call_chain;
194
195 /**
196 * Indicates if the current vCPU is running in SPMC scheduled
197 * mode or Normal World scheduled mode.
198 */
199 enum schedule_mode scheduling_mode;
200
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500201 /**
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600202 * If the action in response to a non-secure or other-secure interrupt
203 * is to queue it, this field is used to save and restore the current
204 * priority mask.
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500205 */
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600206 uint8_t prev_interrupt_priority;
Madhukar Pappireddyc40f55f2022-06-22 11:00:41 -0500207
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500208 /** Partition Runtime Model. */
209 enum partition_runtime_model rt_model;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500210
211 /**
Madhukar Pappireddyba248852024-01-04 16:58:09 -0600212 * Direct response message has been intercepted to signal virtual
213 * secure interrupt for an SP.
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500214 */
215 bool direct_resp_intercepted;
216
Madhukar Pappireddyba248852024-01-04 16:58:09 -0600217 /**
218 * FFA_MSG_WAIT invocation has been intercepted to signal virtual
219 * secure interrupt for an SP.
220 */
221 bool msg_wait_intercepted;
222
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500223 /** Save direct response message args to be resumed later. */
224 struct ffa_value direct_resp_ffa_value;
Olivier Deprez181074b2023-02-02 14:53:23 +0100225
226 struct vcpu *next_boot;
Fuad Tabba5c738432019-12-02 11:02:42 +0000227};
228
229/** Encapsulates a vCPU whose lock is held. */
230struct vcpu_locked {
231 struct vcpu *vcpu;
232};
233
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200234/** Container for two vcpu_locked structures. */
235struct two_vcpu_locked {
236 struct vcpu_locked vcpu1;
237 struct vcpu_locked vcpu2;
238};
239
Fuad Tabba5c738432019-12-02 11:02:42 +0000240struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200241struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000242void vcpu_unlock(struct vcpu_locked *locked);
243void vcpu_init(struct vcpu *vcpu, struct vm *vm);
244void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100245ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000246bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100247bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
248 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000249
250bool vcpu_handle_page_fault(const struct vcpu *current,
251 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200252
J-Alves7ac49052022-02-08 17:20:53 +0000253void vcpu_set_phys_core_idx(struct vcpu *vcpu);
Olivier Deprez632249e2022-09-26 09:18:31 +0200254void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu);
255
Olivier Deprez181074b2023-02-02 14:53:23 +0100256void vcpu_update_boot(struct vcpu *vcpu);
257struct vcpu *vcpu_get_boot_vcpu(void);
J-Alves7ac49052022-02-08 17:20:53 +0000258
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100259static inline bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts,
260 uint32_t intid)
261{
262 return interrupt_bitmap_get_value(&interrupts->interrupt_enabled,
263 intid) == 1U;
264}
265
266static inline void vcpu_virt_interrupt_set_enabled(
267 struct interrupts *interrupts, uint32_t intid)
268{
269 interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid);
270}
271
272static inline void vcpu_virt_interrupt_clear_enabled(
273 struct interrupts *interrupts, uint32_t intid)
274{
275 interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid);
276}
277
278static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts,
279 uint32_t intid)
280{
281 return interrupt_bitmap_get_value(&interrupts->interrupt_pending,
282 intid) == 1U;
283}
284
285static inline void vcpu_virt_interrupt_set_pending(
286 struct interrupts *interrupts, uint32_t intid)
287{
288 interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid);
289}
290
291static inline void vcpu_virt_interrupt_clear_pending(
292 struct interrupts *interrupts, uint32_t intid)
293{
294 interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid);
295}
296
297static inline enum interrupt_type vcpu_virt_interrupt_get_type(
298 struct interrupts *interrupts, uint32_t intid)
299{
300 return (enum interrupt_type)interrupt_bitmap_get_value(
301 &interrupts->interrupt_type, intid);
302}
303
304static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
305 uint32_t intid,
306 enum interrupt_type type)
307{
308 if (type == INTERRUPT_TYPE_IRQ) {
309 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
310 intid);
311 } else {
312 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
313 }
314}
315
Manish Pandey35e452f2021-02-18 21:36:34 +0000316static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked)
317{
318 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++;
319}
320
321static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked)
322{
323 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--;
324}
325
326static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked)
327{
328 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++;
329}
330
331static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked)
332{
333 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--;
334}
335
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100336static inline void vcpu_interrupt_count_increment(
337 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
338 uint32_t intid)
339{
340 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
341 INTERRUPT_TYPE_IRQ) {
342 vcpu_irq_count_increment(vcpu_locked);
343 } else {
344 vcpu_fiq_count_increment(vcpu_locked);
345 }
346}
347
348static inline void vcpu_interrupt_count_decrement(
349 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
350 uint32_t intid)
351{
352 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
353 INTERRUPT_TYPE_IRQ) {
354 vcpu_irq_count_decrement(vcpu_locked);
355 } else {
356 vcpu_fiq_count_decrement(vcpu_locked);
357 }
358}
359
Manish Pandey35e452f2021-02-18 21:36:34 +0000360static inline uint32_t vcpu_interrupt_irq_count_get(
361 struct vcpu_locked vcpu_locked)
362{
363 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count;
364}
365
366static inline uint32_t vcpu_interrupt_fiq_count_get(
367 struct vcpu_locked vcpu_locked)
368{
369 return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
370}
371
372static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked)
373{
374 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count +
375 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
376}
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500377
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600378static inline void vcpu_call_chain_extend(struct vcpu_locked vcpu1_locked,
379 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500380{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600381 vcpu1_locked.vcpu->call_chain.next_node = vcpu2_locked.vcpu;
382 vcpu2_locked.vcpu->call_chain.prev_node = vcpu1_locked.vcpu;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500383}
384
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600385static inline void vcpu_call_chain_remove_node(struct vcpu_locked vcpu1_locked,
386 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500387{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600388 vcpu1_locked.vcpu->call_chain.prev_node = NULL;
389 vcpu2_locked.vcpu->call_chain.next_node = NULL;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500390}
J-Alves12cedae2023-08-04 14:37:37 +0100391
J-Alves0247fe62024-02-23 10:21:46 +0000392void vcpu_set_running(struct vcpu_locked target_locked, struct ffa_value *args);
J-Alves12cedae2023-08-04 14:37:37 +0100393void vcpu_save_interrupt_priority(struct vcpu_locked vcpu_locked,
394 uint8_t priority);
395void vcpu_interrupt_inject(struct vcpu_locked target_locked, uint32_t intid);
396void vcpu_set_processing_interrupt(struct vcpu_locked vcpu_locked,
J-Alves41e8d5b2024-02-13 11:01:23 +0000397 uint32_t intid,
398 struct vcpu_locked preempted_locked);
J-Alves12cedae2023-08-04 14:37:37 +0100399void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked);