blob: 7669d2b296b595cf0b96be7f30ea62292a12fbb2 [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
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050024/** Maximum number of pending virtual interrupts in the queue per vCPU. */
25#define VINT_QUEUE_MAX 5
26
Fuad Tabba5c738432019-12-02 11:02:42 +000027enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000028 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000029 VCPU_STATE_OFF,
30
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000031 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000032 VCPU_STATE_RUNNING,
33
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050034 /** The vCPU is waiting to be allocated CPU cycles to do work. */
35 VCPU_STATE_WAITING,
36
37 /**
38 * The vCPU is blocked and waiting for some work to complete on
39 * its behalf.
40 */
41 VCPU_STATE_BLOCKED,
42
43 /** The vCPU has been preempted by an interrupt. */
44 VCPU_STATE_PREEMPTED,
Fuad Tabba5c738432019-12-02 11:02:42 +000045
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000046 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000047 VCPU_STATE_BLOCKED_INTERRUPT,
48
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000049 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000050 VCPU_STATE_ABORTED,
51};
52
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -050053/** Refer to section 7 of the FF-A v1.1 EAC0 spec. */
54enum partition_runtime_model {
55 RTM_NONE,
56 /** Runtime model for FFA_RUN. */
57 RTM_FFA_RUN,
58 /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */
59 RTM_FFA_DIR_REQ,
60 /** Runtime model for Secure Interrupt handling. */
61 RTM_SEC_INTERRUPT,
62 /** Runtime model for SP Initialization. */
63 RTM_SP_INIT,
64};
65
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050066/** Refer to section 8.2.3 of the FF-A EAC0 spec. */
67enum schedule_mode {
68 NONE,
69 /** Normal world scheduled mode. */
70 NWD_MODE,
71 /** SPMC scheduled mode. */
72 SPMC_MODE,
73};
74
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050075/*
76 * This queue is implemented as a circular buffer. The entries are managed on
77 * a First In First Out basis.
78 */
79struct interrupt_queue {
80 uint32_t vint_buffer[VINT_QUEUE_MAX];
81 uint16_t head;
82 uint16_t tail;
83};
84
Fuad Tabba5c738432019-12-02 11:02:42 +000085struct interrupts {
86 /** Bitfield keeping track of which interrupts are enabled. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010087 struct interrupt_bitmap interrupt_enabled;
Fuad Tabba5c738432019-12-02 11:02:42 +000088 /** Bitfield keeping track of which interrupts are pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010089 struct interrupt_bitmap interrupt_pending;
Manish Pandey35e452f2021-02-18 21:36:34 +000090 /** Bitfield recording the interrupt pin configuration. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010091 struct interrupt_bitmap interrupt_type;
Fuad Tabba5c738432019-12-02 11:02:42 +000092 /**
93 * The number of interrupts which are currently both enabled and
Manish Pandey35e452f2021-02-18 21:36:34 +000094 * pending. Count independently virtual IRQ and FIQ interrupt types
95 * i.e. the sum of the two counters is the number of bits set in
96 * interrupt_enable & interrupt_pending.
Fuad Tabba5c738432019-12-02 11:02:42 +000097 */
Manish Pandey35e452f2021-02-18 21:36:34 +000098 uint32_t enabled_and_pending_irq_count;
99 uint32_t enabled_and_pending_fiq_count;
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500100
101 /**
102 * Partition Manager maintains a queue of pending virtual interrupts.
103 */
104 struct interrupt_queue vint_q;
Fuad Tabba5c738432019-12-02 11:02:42 +0000105};
106
107struct vcpu_fault_info {
108 ipaddr_t ipaddr;
109 vaddr_t vaddr;
110 vaddr_t pc;
111 uint32_t mode;
112};
113
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500114struct call_chain {
115 /** Previous node in the SP call chain. */
116 struct vcpu *prev_node;
117
118 /** Next node in the SP call chain. */
119 struct vcpu *next_node;
120};
121
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100122#define LOG_BUFFER_SIZE 256
123
124struct log_buffer {
125 char chars[LOG_BUFFER_SIZE];
126 uint16_t len;
127};
128
Fuad Tabba5c738432019-12-02 11:02:42 +0000129struct vcpu {
130 struct spinlock lock;
131
132 /*
133 * The state is only changed in the context of the vCPU being run. This
134 * ensures the scheduler can easily keep track of the vCPU state as
135 * transitions are indicated by the return code from the run call.
136 */
137 enum vcpu_state state;
138
139 struct cpu *cpu;
140 struct vm *vm;
141 struct arch_regs regs;
142 struct interrupts interrupts;
143
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100144 struct log_buffer log_buffer;
145
Fuad Tabba5c738432019-12-02 11:02:42 +0000146 /*
147 * Determine whether the 'regs' field is available for use. This is set
148 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +0100149 * back to true when it is descheduled. This is not relevant for the
150 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
151 * in the secure world) as they are pinned to physical CPUs and there
152 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000153 */
154 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000155
156 /*
157 * If the current vCPU is executing as a consequence of a
Kathleen Capellae468c112023-12-13 17:56:28 -0500158 * direct request invocation, then this member holds the
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000159 * originating VM ID from which the call originated.
160 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
Kathleen Capellae468c112023-12-13 17:56:28 -0500161 * a result of a prior direct request invocation.
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000162 */
Kathleen Capellae468c112023-12-13 17:56:28 -0500163 struct {
164 /**
165 * Indicate whether request is via FFA_MSG_SEND_DIRECT_REQ2.
166 */
167 bool is_ffa_req2;
168 ffa_id_t vm_id;
169 } direct_request_origin;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100170
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500171 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100172 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500173
174 /**
175 * Determine whether vCPU is currently handling secure interrupt.
176 */
177 bool processing_secure_interrupt;
178 bool secure_interrupt_deactivated;
179
180 /**
181 * INTID of the current secure interrupt being processed by this vCPU.
182 */
183 uint32_t current_sec_interrupt_id;
184
185 /**
186 * Track current vCPU which got pre-empted when secure interrupt
187 * triggered.
188 */
189 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600190
191 /**
192 * Current value of the Priority Mask register which is saved/restored
193 * during secure interrupt handling.
194 */
195 uint8_t priority_mask;
Madhukar Pappireddy0aaadbb2021-12-16 20:58:10 -0600196
197 /**
198 * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple
199 * mechanisms to signal completion of secure interrupt handling. SP
200 * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN,
201 * when in WAITING/BLOCKED state respectively, but has to perform
202 * implicit signal completion mechanism by dropping the priority
203 * of the virtual secure interrupt when SPMC signaled the virtual
204 * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int
205 * while running). This variable helps SPMC to keep a track of such
206 * mechanism and perform appropriate bookkeeping.
207 */
208 bool implicit_completion_signal;
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500209
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500210 /** SP call chain. */
211 struct call_chain call_chain;
212
213 /**
214 * Indicates if the current vCPU is running in SPMC scheduled
215 * mode or Normal World scheduled mode.
216 */
217 enum schedule_mode scheduling_mode;
218
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500219 /**
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600220 * If the action in response to a non-secure or other-secure interrupt
221 * is to queue it, this field is used to save and restore the current
222 * priority mask.
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500223 */
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600224 uint8_t prev_interrupt_priority;
Madhukar Pappireddyc40f55f2022-06-22 11:00:41 -0500225
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500226 /** Partition Runtime Model. */
227 enum partition_runtime_model rt_model;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500228
229 /**
Madhukar Pappireddyba248852024-01-04 16:58:09 -0600230 * Direct response message has been intercepted to signal virtual
231 * secure interrupt for an SP.
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500232 */
233 bool direct_resp_intercepted;
234
Madhukar Pappireddyba248852024-01-04 16:58:09 -0600235 /**
236 * FFA_MSG_WAIT invocation has been intercepted to signal virtual
237 * secure interrupt for an SP.
238 */
239 bool msg_wait_intercepted;
240
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500241 /** Save direct response message args to be resumed later. */
242 struct ffa_value direct_resp_ffa_value;
Olivier Deprez181074b2023-02-02 14:53:23 +0100243
244 struct vcpu *next_boot;
Fuad Tabba5c738432019-12-02 11:02:42 +0000245};
246
247/** Encapsulates a vCPU whose lock is held. */
248struct vcpu_locked {
249 struct vcpu *vcpu;
250};
251
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200252/** Container for two vcpu_locked structures. */
253struct two_vcpu_locked {
254 struct vcpu_locked vcpu1;
255 struct vcpu_locked vcpu2;
256};
257
Fuad Tabba5c738432019-12-02 11:02:42 +0000258struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200259struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000260void vcpu_unlock(struct vcpu_locked *locked);
261void vcpu_init(struct vcpu *vcpu, struct vm *vm);
262void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100263ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000264bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100265bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
266 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000267
268bool vcpu_handle_page_fault(const struct vcpu *current,
269 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200270
J-Alves7ac49052022-02-08 17:20:53 +0000271void vcpu_set_phys_core_idx(struct vcpu *vcpu);
Olivier Deprez632249e2022-09-26 09:18:31 +0200272void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu);
273
Olivier Deprez181074b2023-02-02 14:53:23 +0100274void vcpu_update_boot(struct vcpu *vcpu);
275struct vcpu *vcpu_get_boot_vcpu(void);
J-Alves7ac49052022-02-08 17:20:53 +0000276
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100277static inline bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts,
278 uint32_t intid)
279{
280 return interrupt_bitmap_get_value(&interrupts->interrupt_enabled,
281 intid) == 1U;
282}
283
284static inline void vcpu_virt_interrupt_set_enabled(
285 struct interrupts *interrupts, uint32_t intid)
286{
287 interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid);
288}
289
290static inline void vcpu_virt_interrupt_clear_enabled(
291 struct interrupts *interrupts, uint32_t intid)
292{
293 interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid);
294}
295
296static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts,
297 uint32_t intid)
298{
299 return interrupt_bitmap_get_value(&interrupts->interrupt_pending,
300 intid) == 1U;
301}
302
303static inline void vcpu_virt_interrupt_set_pending(
304 struct interrupts *interrupts, uint32_t intid)
305{
306 interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid);
307}
308
309static inline void vcpu_virt_interrupt_clear_pending(
310 struct interrupts *interrupts, uint32_t intid)
311{
312 interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid);
313}
314
315static inline enum interrupt_type vcpu_virt_interrupt_get_type(
316 struct interrupts *interrupts, uint32_t intid)
317{
318 return (enum interrupt_type)interrupt_bitmap_get_value(
319 &interrupts->interrupt_type, intid);
320}
321
322static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
323 uint32_t intid,
324 enum interrupt_type type)
325{
326 if (type == INTERRUPT_TYPE_IRQ) {
327 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
328 intid);
329 } else {
330 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
331 }
332}
333
Manish Pandey35e452f2021-02-18 21:36:34 +0000334static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked)
335{
336 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++;
337}
338
339static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked)
340{
341 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--;
342}
343
344static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked)
345{
346 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++;
347}
348
349static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked)
350{
351 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--;
352}
353
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100354static inline void vcpu_interrupt_count_increment(
355 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
356 uint32_t intid)
357{
358 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
359 INTERRUPT_TYPE_IRQ) {
360 vcpu_irq_count_increment(vcpu_locked);
361 } else {
362 vcpu_fiq_count_increment(vcpu_locked);
363 }
364}
365
366static inline void vcpu_interrupt_count_decrement(
367 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
368 uint32_t intid)
369{
370 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
371 INTERRUPT_TYPE_IRQ) {
372 vcpu_irq_count_decrement(vcpu_locked);
373 } else {
374 vcpu_fiq_count_decrement(vcpu_locked);
375 }
376}
377
Manish Pandey35e452f2021-02-18 21:36:34 +0000378static inline uint32_t vcpu_interrupt_irq_count_get(
379 struct vcpu_locked vcpu_locked)
380{
381 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count;
382}
383
384static inline uint32_t vcpu_interrupt_fiq_count_get(
385 struct vcpu_locked vcpu_locked)
386{
387 return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
388}
389
390static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked)
391{
392 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count +
393 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
394}
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500395
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600396static inline void vcpu_call_chain_extend(struct vcpu_locked vcpu1_locked,
397 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500398{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600399 vcpu1_locked.vcpu->call_chain.next_node = vcpu2_locked.vcpu;
400 vcpu2_locked.vcpu->call_chain.prev_node = vcpu1_locked.vcpu;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500401}
402
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600403static inline void vcpu_call_chain_remove_node(struct vcpu_locked vcpu1_locked,
404 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500405{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600406 vcpu1_locked.vcpu->call_chain.prev_node = NULL;
407 vcpu2_locked.vcpu->call_chain.next_node = NULL;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500408}
J-Alves12cedae2023-08-04 14:37:37 +0100409
J-Alves0247fe62024-02-23 10:21:46 +0000410void vcpu_set_running(struct vcpu_locked target_locked, struct ffa_value *args);
J-Alves12cedae2023-08-04 14:37:37 +0100411void vcpu_save_interrupt_priority(struct vcpu_locked vcpu_locked,
412 uint8_t priority);
413void vcpu_interrupt_inject(struct vcpu_locked target_locked, uint32_t intid);
414void vcpu_set_processing_interrupt(struct vcpu_locked vcpu_locked,
J-Alves41e8d5b2024-02-13 11:01:23 +0000415 uint32_t intid,
416 struct vcpu_locked preempted_locked);
J-Alves12cedae2023-08-04 14:37:37 +0100417void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked);
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500418bool vcpu_interrupt_queue_push(struct vcpu_locked vcpu_locked,
419 uint32_t vint_id);
420bool vcpu_interrupt_queue_pop(struct vcpu_locked vcpu_locked,
421 uint32_t *vint_id);
422bool vcpu_interrupt_queue_peek(struct vcpu_locked vcpu_locked,
423 uint32_t *vint_id);
424bool vcpu_is_interrupt_in_queue(struct vcpu_locked vcpu_locked,
425 uint32_t vint_id);
426bool vcpu_is_interrupt_queue_empty(struct vcpu_locked vcpu_locked);