blob: dbe6b94b1ee77d294b8b2ba14a0036a1ae62230d [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
Fuad Tabba5c738432019-12-02 11:02:42 +0000104struct vcpu {
105 struct spinlock lock;
106
107 /*
108 * The state is only changed in the context of the vCPU being run. This
109 * ensures the scheduler can easily keep track of the vCPU state as
110 * transitions are indicated by the return code from the run call.
111 */
112 enum vcpu_state state;
113
114 struct cpu *cpu;
115 struct vm *vm;
116 struct arch_regs regs;
117 struct interrupts interrupts;
118
119 /*
120 * Determine whether the 'regs' field is available for use. This is set
121 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +0100122 * back to true when it is descheduled. This is not relevant for the
123 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
124 * in the secure world) as they are pinned to physical CPUs and there
125 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000126 */
127 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000128
129 /*
130 * If the current vCPU is executing as a consequence of a
131 * FFA_MSG_SEND_DIRECT_REQ invocation, then this member holds the
132 * originating VM ID from which the call originated.
133 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
134 * a result of a prior FFA_MSG_SEND_DIRECT_REQ invocation.
135 */
136 ffa_vm_id_t direct_request_origin_vm_id;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100137
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500138 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100139 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500140
141 /**
142 * Determine whether vCPU is currently handling secure interrupt.
143 */
144 bool processing_secure_interrupt;
145 bool secure_interrupt_deactivated;
146
147 /**
148 * INTID of the current secure interrupt being processed by this vCPU.
149 */
150 uint32_t current_sec_interrupt_id;
151
152 /**
153 * Track current vCPU which got pre-empted when secure interrupt
154 * triggered.
155 */
156 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600157
158 /**
159 * Current value of the Priority Mask register which is saved/restored
160 * during secure interrupt handling.
161 */
162 uint8_t priority_mask;
Madhukar Pappireddy0aaadbb2021-12-16 20:58:10 -0600163
164 /**
165 * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple
166 * mechanisms to signal completion of secure interrupt handling. SP
167 * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN,
168 * when in WAITING/BLOCKED state respectively, but has to perform
169 * implicit signal completion mechanism by dropping the priority
170 * of the virtual secure interrupt when SPMC signaled the virtual
171 * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int
172 * while running). This variable helps SPMC to keep a track of such
173 * mechanism and perform appropriate bookkeeping.
174 */
175 bool implicit_completion_signal;
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500176
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500177 /** SP call chain. */
178 struct call_chain call_chain;
179
180 /**
181 * Indicates if the current vCPU is running in SPMC scheduled
182 * mode or Normal World scheduled mode.
183 */
184 enum schedule_mode scheduling_mode;
185
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500186 /**
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600187 * If the action in response to a non-secure or other-secure interrupt
188 * is to queue it, this field is used to save and restore the current
189 * priority mask.
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500190 */
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600191 uint8_t prev_interrupt_priority;
Madhukar Pappireddyc40f55f2022-06-22 11:00:41 -0500192
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500193 /** Partition Runtime Model. */
194 enum partition_runtime_model rt_model;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500195
196 /**
197 * Direct response message has been intercepted to handle virtual
198 * secure interrupt for a S-EL0 partition.
199 */
200 bool direct_resp_intercepted;
201
202 /** Save direct response message args to be resumed later. */
203 struct ffa_value direct_resp_ffa_value;
Olivier Deprez181074b2023-02-02 14:53:23 +0100204
205 struct vcpu *next_boot;
Fuad Tabba5c738432019-12-02 11:02:42 +0000206};
207
208/** Encapsulates a vCPU whose lock is held. */
209struct vcpu_locked {
210 struct vcpu *vcpu;
211};
212
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200213/** Container for two vcpu_locked structures. */
214struct two_vcpu_locked {
215 struct vcpu_locked vcpu1;
216 struct vcpu_locked vcpu2;
217};
218
Fuad Tabba5c738432019-12-02 11:02:42 +0000219struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200220struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000221void vcpu_unlock(struct vcpu_locked *locked);
222void vcpu_init(struct vcpu *vcpu, struct vm *vm);
223void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100224ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000225bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100226bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
227 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000228
229bool vcpu_handle_page_fault(const struct vcpu *current,
230 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200231
J-Alves7ac49052022-02-08 17:20:53 +0000232void vcpu_set_phys_core_idx(struct vcpu *vcpu);
Olivier Deprez632249e2022-09-26 09:18:31 +0200233void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu);
234
Olivier Deprez181074b2023-02-02 14:53:23 +0100235void vcpu_update_boot(struct vcpu *vcpu);
236struct vcpu *vcpu_get_boot_vcpu(void);
J-Alves7ac49052022-02-08 17:20:53 +0000237
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100238static inline bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts,
239 uint32_t intid)
240{
241 return interrupt_bitmap_get_value(&interrupts->interrupt_enabled,
242 intid) == 1U;
243}
244
245static inline void vcpu_virt_interrupt_set_enabled(
246 struct interrupts *interrupts, uint32_t intid)
247{
248 interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid);
249}
250
251static inline void vcpu_virt_interrupt_clear_enabled(
252 struct interrupts *interrupts, uint32_t intid)
253{
254 interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid);
255}
256
257static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts,
258 uint32_t intid)
259{
260 return interrupt_bitmap_get_value(&interrupts->interrupt_pending,
261 intid) == 1U;
262}
263
264static inline void vcpu_virt_interrupt_set_pending(
265 struct interrupts *interrupts, uint32_t intid)
266{
267 interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid);
268}
269
270static inline void vcpu_virt_interrupt_clear_pending(
271 struct interrupts *interrupts, uint32_t intid)
272{
273 interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid);
274}
275
276static inline enum interrupt_type vcpu_virt_interrupt_get_type(
277 struct interrupts *interrupts, uint32_t intid)
278{
279 return (enum interrupt_type)interrupt_bitmap_get_value(
280 &interrupts->interrupt_type, intid);
281}
282
283static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
284 uint32_t intid,
285 enum interrupt_type type)
286{
287 if (type == INTERRUPT_TYPE_IRQ) {
288 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
289 intid);
290 } else {
291 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
292 }
293}
294
Manish Pandey35e452f2021-02-18 21:36:34 +0000295static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked)
296{
297 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++;
298}
299
300static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked)
301{
302 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--;
303}
304
305static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked)
306{
307 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++;
308}
309
310static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked)
311{
312 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--;
313}
314
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100315static inline void vcpu_interrupt_count_increment(
316 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
317 uint32_t intid)
318{
319 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
320 INTERRUPT_TYPE_IRQ) {
321 vcpu_irq_count_increment(vcpu_locked);
322 } else {
323 vcpu_fiq_count_increment(vcpu_locked);
324 }
325}
326
327static inline void vcpu_interrupt_count_decrement(
328 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
329 uint32_t intid)
330{
331 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
332 INTERRUPT_TYPE_IRQ) {
333 vcpu_irq_count_decrement(vcpu_locked);
334 } else {
335 vcpu_fiq_count_decrement(vcpu_locked);
336 }
337}
338
Manish Pandey35e452f2021-02-18 21:36:34 +0000339static inline uint32_t vcpu_interrupt_irq_count_get(
340 struct vcpu_locked vcpu_locked)
341{
342 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count;
343}
344
345static inline uint32_t vcpu_interrupt_fiq_count_get(
346 struct vcpu_locked vcpu_locked)
347{
348 return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
349}
350
351static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked)
352{
353 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count +
354 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
355}
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500356
357static inline void vcpu_call_chain_extend(struct vcpu *vcpu1,
358 struct vcpu *vcpu2)
359{
360 vcpu1->call_chain.next_node = vcpu2;
361 vcpu2->call_chain.prev_node = vcpu1;
362}
363
364static inline void vcpu_call_chain_remove_node(struct vcpu *vcpu1,
365 struct vcpu *vcpu2)
366{
367 vcpu1->call_chain.prev_node = NULL;
368 vcpu2->call_chain.next_node = NULL;
369}