blob: ffb1ec8c13f914359927480ba20b053d7dc2b3b8 [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"
Karl Meakin07a69ab2025-02-07 14:53:19 +000016#include "hf/mm.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000017#include "hf/spinlock.h"
18
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010019#include "vmapi/hf/ffa.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000020
Madhukar Pappireddy84154052022-06-21 18:30:25 -050021/** Action for non secure interrupt by SPMC. */
22#define NS_ACTION_QUEUED 0
23#define NS_ACTION_ME 1
24#define NS_ACTION_SIGNALED 2
Madhukar Pappireddy84154052022-06-21 18:30:25 -050025
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050026/** Maximum number of pending virtual interrupts in the queue per vCPU. */
J-Alvesa835bdc2025-02-28 18:38:43 +000027#define VINT_QUEUE_MAX 10
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050028
Fuad Tabba5c738432019-12-02 11:02:42 +000029enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000030 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000031 VCPU_STATE_OFF,
32
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000033 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000034 VCPU_STATE_RUNNING,
35
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050036 /** The vCPU is waiting to be allocated CPU cycles to do work. */
37 VCPU_STATE_WAITING,
38
39 /**
40 * The vCPU is blocked and waiting for some work to complete on
41 * its behalf.
42 */
43 VCPU_STATE_BLOCKED,
44
45 /** The vCPU has been preempted by an interrupt. */
46 VCPU_STATE_PREEMPTED,
Fuad Tabba5c738432019-12-02 11:02:42 +000047
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000048 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000049 VCPU_STATE_BLOCKED_INTERRUPT,
50
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000051 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000052 VCPU_STATE_ABORTED,
53};
54
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -050055/** Refer to section 7 of the FF-A v1.1 EAC0 spec. */
56enum partition_runtime_model {
57 RTM_NONE,
58 /** Runtime model for FFA_RUN. */
59 RTM_FFA_RUN,
60 /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */
61 RTM_FFA_DIR_REQ,
62 /** Runtime model for Secure Interrupt handling. */
63 RTM_SEC_INTERRUPT,
64 /** Runtime model for SP Initialization. */
65 RTM_SP_INIT,
66};
67
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050068/** Refer to section 8.2.3 of the FF-A EAC0 spec. */
69enum schedule_mode {
70 NONE,
71 /** Normal world scheduled mode. */
72 NWD_MODE,
73 /** SPMC scheduled mode. */
74 SPMC_MODE,
75};
76
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050077/*
78 * This queue is implemented as a circular buffer. The entries are managed on
79 * a First In First Out basis.
80 */
81struct interrupt_queue {
82 uint32_t vint_buffer[VINT_QUEUE_MAX];
83 uint16_t head;
84 uint16_t tail;
J-Alves23a73032025-03-04 16:20:54 +000085 size_t queued_vint_count;
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -050086};
87
Fuad Tabba5c738432019-12-02 11:02:42 +000088struct interrupts {
89 /** Bitfield keeping track of which interrupts are enabled. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010090 struct interrupt_bitmap interrupt_enabled;
Fuad Tabba5c738432019-12-02 11:02:42 +000091 /** Bitfield keeping track of which interrupts are pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010092 struct interrupt_bitmap interrupt_pending;
Manish Pandey35e452f2021-02-18 21:36:34 +000093 /** Bitfield recording the interrupt pin configuration. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010094 struct interrupt_bitmap interrupt_type;
Fuad Tabba5c738432019-12-02 11:02:42 +000095 /**
96 * The number of interrupts which are currently both enabled and
Manish Pandey35e452f2021-02-18 21:36:34 +000097 * pending. Count independently virtual IRQ and FIQ interrupt types
98 * i.e. the sum of the two counters is the number of bits set in
99 * interrupt_enable & interrupt_pending.
Fuad Tabba5c738432019-12-02 11:02:42 +0000100 */
Manish Pandey35e452f2021-02-18 21:36:34 +0000101 uint32_t enabled_and_pending_irq_count;
102 uint32_t enabled_and_pending_fiq_count;
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500103
104 /**
105 * Partition Manager maintains a queue of pending virtual interrupts.
106 */
107 struct interrupt_queue vint_q;
Fuad Tabba5c738432019-12-02 11:02:42 +0000108};
109
110struct vcpu_fault_info {
111 ipaddr_t ipaddr;
112 vaddr_t vaddr;
113 vaddr_t pc;
Karl Meakin07a69ab2025-02-07 14:53:19 +0000114 mm_mode_t mode;
Fuad Tabba5c738432019-12-02 11:02:42 +0000115};
116
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500117struct call_chain {
118 /** Previous node in the SP call chain. */
119 struct vcpu *prev_node;
120
121 /** Next node in the SP call chain. */
122 struct vcpu *next_node;
123};
124
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100125#define LOG_BUFFER_SIZE 256
126
127struct log_buffer {
128 char chars[LOG_BUFFER_SIZE];
129 uint16_t len;
130};
131
Fuad Tabba5c738432019-12-02 11:02:42 +0000132struct vcpu {
133 struct spinlock lock;
134
135 /*
136 * The state is only changed in the context of the vCPU being run. This
137 * ensures the scheduler can easily keep track of the vCPU state as
138 * transitions are indicated by the return code from the run call.
139 */
140 enum vcpu_state state;
141
142 struct cpu *cpu;
143 struct vm *vm;
144 struct arch_regs regs;
145 struct interrupts interrupts;
146
Karl Meakinc5cebbc2024-06-17 11:30:27 +0100147 struct log_buffer log_buffer;
148
Fuad Tabba5c738432019-12-02 11:02:42 +0000149 /*
150 * Determine whether the 'regs' field is available for use. This is set
151 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +0100152 * back to true when it is descheduled. This is not relevant for the
153 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
154 * in the secure world) as they are pinned to physical CPUs and there
155 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000156 */
157 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000158
159 /*
160 * If the current vCPU is executing as a consequence of a
Kathleen Capellae468c112023-12-13 17:56:28 -0500161 * direct request invocation, then this member holds the
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000162 * originating VM ID from which the call originated.
163 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
Kathleen Capellae468c112023-12-13 17:56:28 -0500164 * a result of a prior direct request invocation.
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000165 */
Kathleen Capellae468c112023-12-13 17:56:28 -0500166 struct {
Kathleen Capellae468c112023-12-13 17:56:28 -0500167 ffa_id_t vm_id;
Karl Meakin06e8b732024-09-20 18:26:49 +0100168 /** Indicate whether request is via FFA_MSG_SEND_DIRECT_REQ2. */
169 bool is_ffa_req2;
170 /** Indicate whether request is a framework message. */
171 bool is_framework;
Kathleen Capellae468c112023-12-13 17:56:28 -0500172 } direct_request_origin;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100173
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500174 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100175 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500176
177 /**
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500178 * Track current vCPU which got pre-empted when secure interrupt
179 * triggered.
180 */
181 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600182
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500183 /** SP call chain. */
184 struct call_chain call_chain;
185
186 /**
Daniel Boulby1f2babf2024-08-29 16:39:47 +0100187 * Track if the pending IPI has been retrieved by
188 * FFA_NOTIFICATION_INFO_GET.
189 */
190 bool ipi_info_get_retrieved;
191
192 /**
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500193 * Indicates if the current vCPU is running in SPMC scheduled
194 * mode or Normal World scheduled mode.
195 */
196 enum schedule_mode scheduling_mode;
197
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500198 /**
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600199 * If the action in response to a non-secure or other-secure interrupt
200 * is to queue it, this field is used to save and restore the current
201 * priority mask.
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500202 */
Madhukar Pappireddy94da32d2023-02-22 16:04:10 -0600203 uint8_t prev_interrupt_priority;
Madhukar Pappireddyc40f55f2022-06-22 11:00:41 -0500204
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500205 /** Partition Runtime Model. */
206 enum partition_runtime_model rt_model;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500207
Madhukar Pappireddyeed861e2024-09-25 13:50:54 -0500208 /**
209 * An entry in a list maintained by Hafnium for pending arch timers.
210 * It exists in the list on behalf of its parent vCPU. The `prev` and
211 * `next` fields point to the adjacent entries in the list. The list
212 * itself is protected by a spinlock therefore timer entry is
213 * safeguarded from concurrent accesses.
214 */
215 struct list_entry timer_node;
Daniel Boulby7011b5a2024-10-15 18:27:26 +0100216
217 /*
218 * List entry pointing to the next vcpu with an IPI pending on the
219 * same pinned CPU.
220 */
221 struct list_entry ipi_list_node;
Fuad Tabba5c738432019-12-02 11:02:42 +0000222};
223
224/** Encapsulates a vCPU whose lock is held. */
225struct vcpu_locked {
226 struct vcpu *vcpu;
227};
228
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200229/** Container for two vcpu_locked structures. */
230struct two_vcpu_locked {
231 struct vcpu_locked vcpu1;
232 struct vcpu_locked vcpu2;
233};
234
Fuad Tabba5c738432019-12-02 11:02:42 +0000235struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200236struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000237void vcpu_unlock(struct vcpu_locked *locked);
238void vcpu_init(struct vcpu *vcpu, struct vm *vm);
239void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100240ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000241bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100242bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
243 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000244
245bool vcpu_handle_page_fault(const struct vcpu *current,
246 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200247
J-Alves7ac49052022-02-08 17:20:53 +0000248void vcpu_set_phys_core_idx(struct vcpu *vcpu);
Olivier Deprez632249e2022-09-26 09:18:31 +0200249void vcpu_set_boot_info_gp_reg(struct vcpu *vcpu);
250
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600251static inline void vcpu_call_chain_extend(struct vcpu_locked vcpu1_locked,
252 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500253{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600254 vcpu1_locked.vcpu->call_chain.next_node = vcpu2_locked.vcpu;
255 vcpu2_locked.vcpu->call_chain.prev_node = vcpu1_locked.vcpu;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500256}
257
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600258static inline void vcpu_call_chain_remove_node(struct vcpu_locked vcpu1_locked,
259 struct vcpu_locked vcpu2_locked)
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500260{
Madhukar Pappireddybd10e572023-03-06 16:39:49 -0600261 vcpu1_locked.vcpu->call_chain.prev_node = NULL;
262 vcpu2_locked.vcpu->call_chain.next_node = NULL;
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500263}
J-Alves12cedae2023-08-04 14:37:37 +0100264
J-Alves478faac2024-10-23 10:35:57 +0100265void vcpu_set_running(struct vcpu_locked target_locked,
266 const struct ffa_value *args);
Daniel Boulby1f2babf2024-08-29 16:39:47 +0100267
268static inline void vcpu_ipi_set_info_get_retrieved(
269 struct vcpu_locked vcpu_locked)
270{
271 vcpu_locked.vcpu->ipi_info_get_retrieved = true;
272}
273
274static inline bool vcpu_ipi_is_info_get_retrieved(
275 struct vcpu_locked vcpu_locked)
276{
277 return vcpu_locked.vcpu->ipi_info_get_retrieved;
278}
279
280/**
281 * Clear the flag tracking if the IPI has been retrieved by
282 * FFA_NOTIFCATION_INFO_GET.
283 */
284static inline void vcpu_ipi_clear_info_get_retrieved(
285 struct vcpu_locked vcpu_locked)
286{
287 vcpu_locked.vcpu->ipi_info_get_retrieved = false;
288}
289
J-Alves12cedae2023-08-04 14:37:37 +0100290void vcpu_save_interrupt_priority(struct vcpu_locked vcpu_locked,
291 uint8_t priority);
J-Alves3b31f092024-08-07 13:26:29 +0100292
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000293void vcpu_enter_secure_interrupt_rtm(struct vcpu_locked vcpu_locked);
294
295void vcpu_secure_interrupt_complete(struct vcpu_locked vcpu_locked);
296
Daniel Boulbyd21e9b32025-02-13 15:53:21 +0000297static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts,
298 uint32_t intid)
299{
300 return interrupt_bitmap_get_value(&interrupts->interrupt_pending,
301 intid) == 1U;
302}
303
304static inline enum interrupt_type vcpu_virt_interrupt_get_type(
305 struct interrupts *interrupts, uint32_t intid)
306{
307 return (enum interrupt_type)interrupt_bitmap_get_value(
308 &interrupts->interrupt_type, intid);
309}
310
311static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
312 uint32_t intid,
313 enum interrupt_type type)
314{
315 if (type == INTERRUPT_TYPE_IRQ) {
316 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
317 intid);
318 } else {
319 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
320 }
321}
322
323uint32_t vcpu_virt_interrupt_irq_count_get(struct vcpu_locked vcpu_locked);
324uint32_t vcpu_virt_interrupt_fiq_count_get(struct vcpu_locked vcpu_locked);
325uint32_t vcpu_virt_interrupt_count_get(struct vcpu_locked vcpu_locked);
326
327void vcpu_virt_interrupt_enable(struct vcpu_locked vcpu_locked,
328 uint32_t vint_id, bool enable);
329
Daniel Boulby3c1506b2025-02-25 10:49:51 +0000330uint32_t vcpu_virt_interrupt_peek_pending_and_enabled(
331 struct vcpu_locked vcpu_locked);
332uint32_t vcpu_virt_interrupt_get_pending_and_enabled(
333 struct vcpu_locked vcpu_locked);
334void vcpu_virt_interrupt_inject(struct vcpu_locked vcpu_locked,
Madhukar Pappireddy32913cb2024-07-19 13:04:05 -0500335 uint32_t vint_id);
Daniel Boulbyd7992232025-03-06 17:09:49 +0000336void vcpu_virt_interrupt_clear(struct vcpu_locked vcpu_locked,
337 uint32_t vint_id);