blob: 28938e1c9633b0150d8bce77fd0ed690af0b6e81 [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
11#include "hf/addr.h"
Daniel Boulby801f8ef2022-06-27 14:21:01 +010012#include "hf/interrupt_desc.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000013#include "hf/spinlock.h"
14
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010015#include "vmapi/hf/ffa.h"
Fuad Tabba5c738432019-12-02 11:02:42 +000016
Fuad Tabba5c738432019-12-02 11:02:42 +000017enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000018 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000019 VCPU_STATE_OFF,
20
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000021 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000022 VCPU_STATE_RUNNING,
23
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050024 /** The vCPU is waiting to be allocated CPU cycles to do work. */
25 VCPU_STATE_WAITING,
26
27 /**
28 * The vCPU is blocked and waiting for some work to complete on
29 * its behalf.
30 */
31 VCPU_STATE_BLOCKED,
32
33 /** The vCPU has been preempted by an interrupt. */
34 VCPU_STATE_PREEMPTED,
Fuad Tabba5c738432019-12-02 11:02:42 +000035
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000036 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000037 VCPU_STATE_BLOCKED_INTERRUPT,
38
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000039 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000040 VCPU_STATE_ABORTED,
41};
42
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -050043/** Refer to section 7 of the FF-A v1.1 EAC0 spec. */
44enum partition_runtime_model {
45 RTM_NONE,
46 /** Runtime model for FFA_RUN. */
47 RTM_FFA_RUN,
48 /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */
49 RTM_FFA_DIR_REQ,
50 /** Runtime model for Secure Interrupt handling. */
51 RTM_SEC_INTERRUPT,
52 /** Runtime model for SP Initialization. */
53 RTM_SP_INIT,
54};
55
Fuad Tabba5c738432019-12-02 11:02:42 +000056struct interrupts {
57 /** Bitfield keeping track of which interrupts are enabled. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010058 struct interrupt_bitmap interrupt_enabled;
Fuad Tabba5c738432019-12-02 11:02:42 +000059 /** Bitfield keeping track of which interrupts are pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010060 struct interrupt_bitmap interrupt_pending;
Manish Pandey35e452f2021-02-18 21:36:34 +000061 /** Bitfield recording the interrupt pin configuration. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010062 struct interrupt_bitmap interrupt_type;
Fuad Tabba5c738432019-12-02 11:02:42 +000063 /**
64 * The number of interrupts which are currently both enabled and
Manish Pandey35e452f2021-02-18 21:36:34 +000065 * pending. Count independently virtual IRQ and FIQ interrupt types
66 * i.e. the sum of the two counters is the number of bits set in
67 * interrupt_enable & interrupt_pending.
Fuad Tabba5c738432019-12-02 11:02:42 +000068 */
Manish Pandey35e452f2021-02-18 21:36:34 +000069 uint32_t enabled_and_pending_irq_count;
70 uint32_t enabled_and_pending_fiq_count;
Fuad Tabba5c738432019-12-02 11:02:42 +000071};
72
73struct vcpu_fault_info {
74 ipaddr_t ipaddr;
75 vaddr_t vaddr;
76 vaddr_t pc;
77 uint32_t mode;
78};
79
80struct vcpu {
81 struct spinlock lock;
82
83 /*
84 * The state is only changed in the context of the vCPU being run. This
85 * ensures the scheduler can easily keep track of the vCPU state as
86 * transitions are indicated by the return code from the run call.
87 */
88 enum vcpu_state state;
89
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050090 bool is_bootstrapped;
Fuad Tabba5c738432019-12-02 11:02:42 +000091 struct cpu *cpu;
92 struct vm *vm;
93 struct arch_regs regs;
94 struct interrupts interrupts;
95
96 /*
97 * Determine whether the 'regs' field is available for use. This is set
98 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +010099 * back to true when it is descheduled. This is not relevant for the
100 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
101 * in the secure world) as they are pinned to physical CPUs and there
102 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000103 */
104 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000105
106 /*
107 * If the current vCPU is executing as a consequence of a
108 * FFA_MSG_SEND_DIRECT_REQ invocation, then this member holds the
109 * originating VM ID from which the call originated.
110 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
111 * a result of a prior FFA_MSG_SEND_DIRECT_REQ invocation.
112 */
113 ffa_vm_id_t direct_request_origin_vm_id;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100114
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500115 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100116 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500117
118 /**
119 * Determine whether vCPU is currently handling secure interrupt.
120 */
121 bool processing_secure_interrupt;
122 bool secure_interrupt_deactivated;
123
124 /**
125 * INTID of the current secure interrupt being processed by this vCPU.
126 */
127 uint32_t current_sec_interrupt_id;
128
129 /**
130 * Track current vCPU which got pre-empted when secure interrupt
131 * triggered.
132 */
133 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600134
135 /**
136 * Current value of the Priority Mask register which is saved/restored
137 * during secure interrupt handling.
138 */
139 uint8_t priority_mask;
Madhukar Pappireddy0aaadbb2021-12-16 20:58:10 -0600140
141 /**
142 * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple
143 * mechanisms to signal completion of secure interrupt handling. SP
144 * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN,
145 * when in WAITING/BLOCKED state respectively, but has to perform
146 * implicit signal completion mechanism by dropping the priority
147 * of the virtual secure interrupt when SPMC signaled the virtual
148 * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int
149 * while running). This variable helps SPMC to keep a track of such
150 * mechanism and perform appropriate bookkeeping.
151 */
152 bool implicit_completion_signal;
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500153
154 /** Partition Runtime Model. */
155 enum partition_runtime_model rt_model;
Fuad Tabba5c738432019-12-02 11:02:42 +0000156};
157
158/** Encapsulates a vCPU whose lock is held. */
159struct vcpu_locked {
160 struct vcpu *vcpu;
161};
162
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200163/** Container for two vcpu_locked structures. */
164struct two_vcpu_locked {
165 struct vcpu_locked vcpu1;
166 struct vcpu_locked vcpu2;
167};
168
Fuad Tabba5c738432019-12-02 11:02:42 +0000169struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200170struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000171void vcpu_unlock(struct vcpu_locked *locked);
172void vcpu_init(struct vcpu *vcpu, struct vm *vm);
173void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100174ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000175bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100176bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
177 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000178
179bool vcpu_handle_page_fault(const struct vcpu *current,
180 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200181
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100182void vcpu_reset(struct vcpu *vcpu);
Manish Pandey35e452f2021-02-18 21:36:34 +0000183
J-Alves7ac49052022-02-08 17:20:53 +0000184void vcpu_set_phys_core_idx(struct vcpu *vcpu);
185
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100186static inline bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts,
187 uint32_t intid)
188{
189 return interrupt_bitmap_get_value(&interrupts->interrupt_enabled,
190 intid) == 1U;
191}
192
193static inline void vcpu_virt_interrupt_set_enabled(
194 struct interrupts *interrupts, uint32_t intid)
195{
196 interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid);
197}
198
199static inline void vcpu_virt_interrupt_clear_enabled(
200 struct interrupts *interrupts, uint32_t intid)
201{
202 interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid);
203}
204
205static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts,
206 uint32_t intid)
207{
208 return interrupt_bitmap_get_value(&interrupts->interrupt_pending,
209 intid) == 1U;
210}
211
212static inline void vcpu_virt_interrupt_set_pending(
213 struct interrupts *interrupts, uint32_t intid)
214{
215 interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid);
216}
217
218static inline void vcpu_virt_interrupt_clear_pending(
219 struct interrupts *interrupts, uint32_t intid)
220{
221 interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid);
222}
223
224static inline enum interrupt_type vcpu_virt_interrupt_get_type(
225 struct interrupts *interrupts, uint32_t intid)
226{
227 return (enum interrupt_type)interrupt_bitmap_get_value(
228 &interrupts->interrupt_type, intid);
229}
230
231static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
232 uint32_t intid,
233 enum interrupt_type type)
234{
235 if (type == INTERRUPT_TYPE_IRQ) {
236 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
237 intid);
238 } else {
239 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
240 }
241}
242
Manish Pandey35e452f2021-02-18 21:36:34 +0000243static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked)
244{
245 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++;
246}
247
248static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked)
249{
250 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--;
251}
252
253static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked)
254{
255 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++;
256}
257
258static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked)
259{
260 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--;
261}
262
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100263static inline void vcpu_interrupt_count_increment(
264 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
265 uint32_t intid)
266{
267 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
268 INTERRUPT_TYPE_IRQ) {
269 vcpu_irq_count_increment(vcpu_locked);
270 } else {
271 vcpu_fiq_count_increment(vcpu_locked);
272 }
273}
274
275static inline void vcpu_interrupt_count_decrement(
276 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
277 uint32_t intid)
278{
279 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
280 INTERRUPT_TYPE_IRQ) {
281 vcpu_irq_count_decrement(vcpu_locked);
282 } else {
283 vcpu_fiq_count_decrement(vcpu_locked);
284 }
285}
286
Manish Pandey35e452f2021-02-18 21:36:34 +0000287static inline uint32_t vcpu_interrupt_irq_count_get(
288 struct vcpu_locked vcpu_locked)
289{
290 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count;
291}
292
293static inline uint32_t vcpu_interrupt_fiq_count_get(
294 struct vcpu_locked vcpu_locked)
295{
296 return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
297}
298
299static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked)
300{
301 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count +
302 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
303}