blob: 7d16b6a7193b5e1769e41551aff4a989abf4f91e [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"
J-Alves67f5ba32024-09-27 18:07:11 +010015#include "hf/list.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000016#include "hf/spinlock.h"
17
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018#include "vmapi/hf/ffa.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000019
Madhukar Pappireddy84154052022-06-21 18:30:25 -050020/** Action for non secure interrupt by SPMC. */
21#define NS_ACTION_QUEUED 0
22#define NS_ACTION_ME 1
23#define NS_ACTION_SIGNALED 2
Madhukar Pappireddy84154052022-06-21 18:30:25 -050024
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050025/** Maximum number of pending virtual interrupts in the queue per vCPU. */
J-Alvesa835bdc2025-02-28 18:38:43 +000026#define VINT_QUEUE_MAX 10
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050027
Fuad Tabba5c738432019-12-02 11:02:42 +000028enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000029 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000030 VCPU_STATE_OFF,
31
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000032 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000033 VCPU_STATE_RUNNING,
34
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050035 /** The vCPU is waiting to be allocated CPU cycles to do work. */
36 VCPU_STATE_WAITING,
37
38 /**
39 * The vCPU is blocked and waiting for some work to complete on
40 * its behalf.
41 */
42 VCPU_STATE_BLOCKED,
43
44 /** The vCPU has been preempted by an interrupt. */
45 VCPU_STATE_PREEMPTED,
Fuad Tabba5c738432019-12-02 11:02:42 +000046
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000047 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000048 VCPU_STATE_BLOCKED_INTERRUPT,
49
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000050 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000051 VCPU_STATE_ABORTED,
52};
53
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -050054/** Refer to section 7 of the FF-A v1.1 EAC0 spec. */
55enum partition_runtime_model {
56 RTM_NONE,
57 /** Runtime model for FFA_RUN. */
58 RTM_FFA_RUN,
59 /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */
60 RTM_FFA_DIR_REQ,
61 /** Runtime model for Secure Interrupt handling. */
62 RTM_SEC_INTERRUPT,
63 /** Runtime model for SP Initialization. */
64 RTM_SP_INIT,
65};
66
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050067/** Refer to section 8.2.3 of the FF-A EAC0 spec. */
68enum schedule_mode {
69 NONE,
70 /** Normal world scheduled mode. */
71 NWD_MODE,
72 /** SPMC scheduled mode. */
73 SPMC_MODE,
74};
75
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050076/*
77 * This queue is implemented as a circular buffer. The entries are managed on
78 * a First In First Out basis.
79 */
80struct interrupt_queue {
81 uint32_t vint_buffer[VINT_QUEUE_MAX];
82 uint16_t head;
83 uint16_t tail;
J-Alves23a73032025-03-04 16:20:54 +000084 size_t queued_vint_count;
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050085};
86
Fuad Tabba5c738432019-12-02 11:02:42 +000087struct interrupts {
88 /** Bitfield keeping track of which interrupts are enabled. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010089 struct interrupt_bitmap interrupt_enabled;
Fuad Tabba5c738432019-12-02 11:02:42 +000090 /** Bitfield keeping track of which interrupts are pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010091 struct interrupt_bitmap interrupt_pending;
Manish Pandey35e452f2021-02-18 21:36:34 +000092 /** Bitfield recording the interrupt pin configuration. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010093 struct interrupt_bitmap interrupt_type;
Fuad Tabba5c738432019-12-02 11:02:42 +000094 /**
95 * The number of interrupts which are currently both enabled and
Manish Pandey35e452f2021-02-18 21:36:34 +000096 * pending. Count independently virtual IRQ and FIQ interrupt types
97 * i.e. the sum of the two counters is the number of bits set in
98 * interrupt_enable & interrupt_pending.
Fuad Tabba5c738432019-12-02 11:02:42 +000099 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000100 uint32_t enabled_and_pending_irq_count;
101 uint32_t enabled_and_pending_fiq_count;
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500102
103 /**
104 * Partition Manager maintains a queue of pending virtual interrupts.
105 */
106 struct interrupt_queue vint_q;
Fuad Tabba5c738432019-12-02 11:02:42 +0000107};
108
109struct vcpu_fault_info {
110 ipaddr_t ipaddr;
111 vaddr_t vaddr;
112 vaddr_t pc;
113 uint32_t mode;
114};
115
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500116struct call_chain {
117 /** Previous node in the SP call chain. */
118 struct vcpu *prev_node;
119
120 /** Next node in the SP call chain. */
121 struct vcpu *next_node;
122};
123
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100124#define LOG_BUFFER_SIZE 256
125
126struct log_buffer {
127 char chars[LOG_BUFFER_SIZE];
128 uint16_t len;
129};
130
Fuad Tabba5c738432019-12-02 11:02:42 +0000131struct vcpu {
132 struct spinlock lock;
133
134 /*
135 * The state is only changed in the context of the vCPU being run. This
136 * ensures the scheduler can easily keep track of the vCPU state as
137 * transitions are indicated by the return code from the run call.
138 */
139 enum vcpu_state state;
140
141 struct cpu *cpu;
142 struct vm *vm;
143 struct arch_regs regs;
144 struct interrupts interrupts;
145
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100146 struct log_buffer log_buffer;
147
Fuad Tabba5c738432019-12-02 11:02:42 +0000148 /*
149 * Determine whether the 'regs' field is available for use. This is set
150 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +0100151 * back to true when it is descheduled. This is not relevant for the
152 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
153 * in the secure world) as they are pinned to physical CPUs and there
154 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000155 */
156 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000157
158 /*
159 * If the current vCPU is executing as a consequence of a
Kathleen Capellae468c112023-12-13 17:56:28 -0500160 * direct request invocation, then this member holds the
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000161 * originating VM ID from which the call originated.
162 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
Kathleen Capellae468c112023-12-13 17:56:28 -0500163 * a result of a prior direct request invocation.
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000164 */
Kathleen Capellae468c112023-12-13 17:56:28 -0500165 struct {
Kathleen Capellae468c112023-12-13 17:56:28 -0500166 ffa_id_t vm_id;
Karl Meakin06e8b732024-09-20 18:26:49 +0100167 /** Indicate whether request is via FFA_MSG_SEND_DIRECT_REQ2. */
168 bool is_ffa_req2;
169 /** Indicate whether request is a framework message. */
170 bool is_framework;
Kathleen Capellae468c112023-12-13 17:56:28 -0500171 } direct_request_origin;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100172
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500173 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100174 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500175
176 /**
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500177 * Track current vCPU which got pre-empted when secure interrupt
178 * triggered.
179 */
180 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600181
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500182 /** SP call chain. */
183 struct call_chain call_chain;
184
185 /**
Daniel Boulby1f2babf2024-08-29 16:39:47 +0100186 * Track if the pending IPI has been retrieved by
187 * FFA_NOTIFICATION_INFO_GET.
188 */
189 bool ipi_info_get_retrieved;
190
191 /**
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500192 * Indicates if the current vCPU is running in SPMC scheduled
193 * mode or Normal World scheduled mode.
194 */
195 enum schedule_mode scheduling_mode;
196
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500197 /**
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600198 * If the action in response to a non-secure or other-secure interrupt
199 * is to queue it, this field is used to save and restore the current
200 * priority mask.
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500201 */
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600202 uint8_t prev_interrupt_priority;
Madhukar Pappireddyc40f55f2022-06-22 11:00:41 -0500203
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500204 /** Partition Runtime Model. */
205 enum partition_runtime_model rt_model;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500206
Madhukar Pappireddyeed861e2024-09-25 13:50:54 -0500207 /**
208 * An entry in a list maintained by Hafnium for pending arch timers.
209 * It exists in the list on behalf of its parent vCPU. The `prev` and
210 * `next` fields point to the adjacent entries in the list. The list
211 * itself is protected by a spinlock therefore timer entry is
212 * safeguarded from concurrent accesses.
213 */
214 struct list_entry timer_node;
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100215
216 /*
217 * List entry pointing to the next vcpu with an IPI pending on the
218 * same pinned CPU.
219 */
220 struct list_entry ipi_list_node;
Fuad Tabba5c738432019-12-02 11:02:42 +0000221};
222
223/** Encapsulates a vCPU whose lock is held. */
224struct vcpu_locked {
225 struct vcpu *vcpu;
226};
227
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200228/** Container for two vcpu_locked structures. */
229struct two_vcpu_locked {
230 struct vcpu_locked vcpu1;
231 struct vcpu_locked vcpu2;
232};
233
Fuad Tabba5c738432019-12-02 11:02:42 +0000234struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200235struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000236void vcpu_unlock(struct vcpu_locked *locked);
237void vcpu_init(struct vcpu *vcpu, struct vm *vm);
238void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100239ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000240bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100241bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
242 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000243
244bool vcpu_handle_page_fault(const struct vcpu *current,
245 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200246
J-Alves7ac49052022-02-08 17:20:53 +0000247void vcpu_set_phys_core_idx(struct vcpu *vcpu);
Olivier Deprez632249e2022-09-26 09:18:31 +0200248void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu);
249
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600250static inline void vcpu_call_chain_extend(struct vcpu_locked vcpu1_locked,
251 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500252{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600253 vcpu1_locked.vcpu->call_chain.next_node = vcpu2_locked.vcpu;
254 vcpu2_locked.vcpu->call_chain.prev_node = vcpu1_locked.vcpu;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500255}
256
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600257static inline void vcpu_call_chain_remove_node(struct vcpu_locked vcpu1_locked,
258 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500259{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600260 vcpu1_locked.vcpu->call_chain.prev_node = NULL;
261 vcpu2_locked.vcpu->call_chain.next_node = NULL;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500262}
J-Alves12cedae2023-08-04 14:37:37 +0100263
J-Alves478faac2024-10-23 10:35:57 +0100264void vcpu_set_running(struct vcpu_locked target_locked,
265 const struct ffa_value *args);
Daniel Boulby1f2babf2024-08-29 16:39:47 +0100266
267static inline void vcpu_ipi_set_info_get_retrieved(
268 struct vcpu_locked vcpu_locked)
269{
270 vcpu_locked.vcpu->ipi_info_get_retrieved = true;
271}
272
273static inline bool vcpu_ipi_is_info_get_retrieved(
274 struct vcpu_locked vcpu_locked)
275{
276 return vcpu_locked.vcpu->ipi_info_get_retrieved;
277}
278
279/**
280 * Clear the flag tracking if the IPI has been retrieved by
281 * FFA_NOTIFCATION_INFO_GET.
282 */
283static inline void vcpu_ipi_clear_info_get_retrieved(
284 struct vcpu_locked vcpu_locked)
285{
286 vcpu_locked.vcpu->ipi_info_get_retrieved = false;
287}
288
J-Alves12cedae2023-08-04 14:37:37 +0100289void vcpu_save_interrupt_priority(struct vcpu_locked vcpu_locked,
290 uint8_t priority);
J-Alves3b31f092024-08-07 13:26:29 +0100291
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000292void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked);
293
294void vcpu_secure_interrupt_complete(struct vcpu_locked vcpu_locked);
295
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000296static 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 enum interrupt_type vcpu_virt_interrupt_get_type(
304 struct interrupts *interrupts, uint32_t intid)
305{
306 return (enum interrupt_type)interrupt_bitmap_get_value(
307 &interrupts->interrupt_type, intid);
308}
309
310static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
311 uint32_t intid,
312 enum interrupt_type type)
313{
314 if (type == INTERRUPT_TYPE_IRQ) {
315 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
316 intid);
317 } else {
318 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
319 }
320}
321
322uint32_t vcpu_virt_interrupt_irq_count_get(struct vcpu_locked vcpu_locked);
323uint32_t vcpu_virt_interrupt_fiq_count_get(struct vcpu_locked vcpu_locked);
324uint32_t vcpu_virt_interrupt_count_get(struct vcpu_locked vcpu_locked);
325
326void vcpu_virt_interrupt_enable(struct vcpu_locked vcpu_locked,
327 uint32_t vint_id, bool enable);
328
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000329uint32_t vcpu_virt_interrupt_peek_pending_and_enabled(
330 struct vcpu_locked vcpu_locked);
331uint32_t vcpu_virt_interrupt_get_pending_and_enabled(
332 struct vcpu_locked vcpu_locked);
333void vcpu_virt_interrupt_inject(struct vcpu_locked vcpu_locked,
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500334 uint32_t vint_id);
Daniel Boulbyd7992232025-03-06 17:09:49 +0000335void vcpu_virt_interrupt_clear(struct vcpu_locked vcpu_locked,
336 uint32_t vint_id);