blob: 179863ffeaa1e1fa3527d50364165f40323f18df [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
23#define NS_ACTION_INVALID 3
24
Fuad Tabba5c738432019-12-02 11:02:42 +000025enum vcpu_state {
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000026 /** The vCPU is switched off. */
Fuad Tabba5c738432019-12-02 11:02:42 +000027 VCPU_STATE_OFF,
28
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000029 /** The vCPU is currently running. */
Fuad Tabba5c738432019-12-02 11:02:42 +000030 VCPU_STATE_RUNNING,
31
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -050032 /** The vCPU is waiting to be allocated CPU cycles to do work. */
33 VCPU_STATE_WAITING,
34
35 /**
36 * The vCPU is blocked and waiting for some work to complete on
37 * its behalf.
38 */
39 VCPU_STATE_BLOCKED,
40
41 /** The vCPU has been preempted by an interrupt. */
42 VCPU_STATE_PREEMPTED,
Fuad Tabba5c738432019-12-02 11:02:42 +000043
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000044 /** The vCPU is waiting for an interrupt. */
Fuad Tabba5c738432019-12-02 11:02:42 +000045 VCPU_STATE_BLOCKED_INTERRUPT,
46
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000047 /** The vCPU has aborted. */
Fuad Tabba5c738432019-12-02 11:02:42 +000048 VCPU_STATE_ABORTED,
49};
50
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -050051/** Refer to section 7 of the FF-A v1.1 EAC0 spec. */
52enum partition_runtime_model {
53 RTM_NONE,
54 /** Runtime model for FFA_RUN. */
55 RTM_FFA_RUN,
56 /** Runtime model for FFA_MSG_SEND_DIRECT_REQUEST. */
57 RTM_FFA_DIR_REQ,
58 /** Runtime model for Secure Interrupt handling. */
59 RTM_SEC_INTERRUPT,
60 /** Runtime model for SP Initialization. */
61 RTM_SP_INIT,
62};
63
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050064/** Refer to section 8.2.3 of the FF-A EAC0 spec. */
65enum schedule_mode {
66 NONE,
67 /** Normal world scheduled mode. */
68 NWD_MODE,
69 /** SPMC scheduled mode. */
70 SPMC_MODE,
71};
72
Fuad Tabba5c738432019-12-02 11:02:42 +000073struct interrupts {
74 /** Bitfield keeping track of which interrupts are enabled. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010075 struct interrupt_bitmap interrupt_enabled;
Fuad Tabba5c738432019-12-02 11:02:42 +000076 /** Bitfield keeping track of which interrupts are pending. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010077 struct interrupt_bitmap interrupt_pending;
Manish Pandey35e452f2021-02-18 21:36:34 +000078 /** Bitfield recording the interrupt pin configuration. */
Daniel Boulby4ca50f02022-07-29 18:29:34 +010079 struct interrupt_bitmap interrupt_type;
Fuad Tabba5c738432019-12-02 11:02:42 +000080 /**
81 * The number of interrupts which are currently both enabled and
Manish Pandey35e452f2021-02-18 21:36:34 +000082 * pending. Count independently virtual IRQ and FIQ interrupt types
83 * i.e. the sum of the two counters is the number of bits set in
84 * interrupt_enable & interrupt_pending.
Fuad Tabba5c738432019-12-02 11:02:42 +000085 */
Manish Pandey35e452f2021-02-18 21:36:34 +000086 uint32_t enabled_and_pending_irq_count;
87 uint32_t enabled_and_pending_fiq_count;
Fuad Tabba5c738432019-12-02 11:02:42 +000088};
89
90struct vcpu_fault_info {
91 ipaddr_t ipaddr;
92 vaddr_t vaddr;
93 vaddr_t pc;
94 uint32_t mode;
95};
96
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -050097struct call_chain {
98 /** Previous node in the SP call chain. */
99 struct vcpu *prev_node;
100
101 /** Next node in the SP call chain. */
102 struct vcpu *next_node;
103};
104
Fuad Tabba5c738432019-12-02 11:02:42 +0000105struct vcpu {
106 struct spinlock lock;
107
108 /*
109 * The state is only changed in the context of the vCPU being run. This
110 * ensures the scheduler can easily keep track of the vCPU state as
111 * transitions are indicated by the return code from the run call.
112 */
113 enum vcpu_state state;
114
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500115 bool is_bootstrapped;
Fuad Tabba5c738432019-12-02 11:02:42 +0000116 struct cpu *cpu;
117 struct vm *vm;
118 struct arch_regs regs;
119 struct interrupts interrupts;
120
121 /*
122 * Determine whether the 'regs' field is available for use. This is set
123 * to false when a vCPU is about to run on a physical CPU, and is set
Olivier Deprez3caed1c2021-02-05 12:07:36 +0100124 * back to true when it is descheduled. This is not relevant for the
125 * primary VM vCPUs in the normal world (or the "other world VM" vCPUs
126 * in the secure world) as they are pinned to physical CPUs and there
127 * is no contention to take care of.
Fuad Tabba5c738432019-12-02 11:02:42 +0000128 */
129 bool regs_available;
Olivier Deprezee9d6a92019-11-26 09:14:11 +0000130
131 /*
132 * If the current vCPU is executing as a consequence of a
133 * FFA_MSG_SEND_DIRECT_REQ invocation, then this member holds the
134 * originating VM ID from which the call originated.
135 * The value HF_INVALID_VM_ID implies the vCPU is not executing as
136 * a result of a prior FFA_MSG_SEND_DIRECT_REQ invocation.
137 */
138 ffa_vm_id_t direct_request_origin_vm_id;
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100139
Madhukar Pappireddyb11e0d12021-08-02 19:44:35 -0500140 /** Determine whether partition is currently handling managed exit. */
Manish Pandeya5f39fb2020-09-11 09:47:11 +0100141 bool processing_managed_exit;
Madhukar Pappireddyf675bb62021-08-03 12:57:10 -0500142
143 /**
144 * Determine whether vCPU is currently handling secure interrupt.
145 */
146 bool processing_secure_interrupt;
147 bool secure_interrupt_deactivated;
148
149 /**
150 * INTID of the current secure interrupt being processed by this vCPU.
151 */
152 uint32_t current_sec_interrupt_id;
153
154 /**
155 * Track current vCPU which got pre-empted when secure interrupt
156 * triggered.
157 */
158 struct vcpu *preempted_vcpu;
Madhukar Pappireddydd6fdfb2021-12-14 12:30:36 -0600159
160 /**
161 * Current value of the Priority Mask register which is saved/restored
162 * during secure interrupt handling.
163 */
164 uint8_t priority_mask;
Madhukar Pappireddy0aaadbb2021-12-16 20:58:10 -0600165
166 /**
167 * Per FF-A v1.1-Beta0 spec section 8.3, an SP can use multiple
168 * mechanisms to signal completion of secure interrupt handling. SP
169 * can invoke explicit FF-A ABIs, namely FFA_MSG_WAIT and FFA_RUN,
170 * when in WAITING/BLOCKED state respectively, but has to perform
171 * implicit signal completion mechanism by dropping the priority
172 * of the virtual secure interrupt when SPMC signaled the virtual
173 * interrupt in PREEMPTED state(The vCPU was preempted by a Self S-Int
174 * while running). This variable helps SPMC to keep a track of such
175 * mechanism and perform appropriate bookkeeping.
176 */
177 bool implicit_completion_signal;
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500178
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500179 /** SP call chain. */
180 struct call_chain call_chain;
181
182 /**
183 * Indicates if the current vCPU is running in SPMC scheduled
184 * mode or Normal World scheduled mode.
185 */
186 enum schedule_mode scheduling_mode;
187
Madhukar Pappireddy84154052022-06-21 18:30:25 -0500188 /**
189 * Present action taken by SP in response to a non secure interrupt
190 * based on the precedence rules as specified in section 8.3.1.4 of
191 * the FF-A v1.1 EAC0 spec.
192 */
193 uint8_t present_action_ns_interrupts;
194
Madhukar Pappireddyc40f55f2022-06-22 11:00:41 -0500195 /**
196 * If the action in response to a non secure interrupt is to queue it,
197 * this field is used to save and restore the current priority mask.
198 */
199 uint8_t mask_ns_interrupts;
200
Madhukar Pappireddyfe297a32022-06-21 16:42:13 -0500201 /** Partition Runtime Model. */
202 enum partition_runtime_model rt_model;
Madhukar Pappireddy2f76e492022-09-06 15:21:59 -0500203
204 /**
205 * Direct response message has been intercepted to handle virtual
206 * secure interrupt for a S-EL0 partition.
207 */
208 bool direct_resp_intercepted;
209
210 /** Save direct response message args to be resumed later. */
211 struct ffa_value direct_resp_ffa_value;
Fuad Tabba5c738432019-12-02 11:02:42 +0000212};
213
214/** Encapsulates a vCPU whose lock is held. */
215struct vcpu_locked {
216 struct vcpu *vcpu;
217};
218
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200219/** Container for two vcpu_locked structures. */
220struct two_vcpu_locked {
221 struct vcpu_locked vcpu1;
222 struct vcpu_locked vcpu2;
223};
224
Fuad Tabba5c738432019-12-02 11:02:42 +0000225struct vcpu_locked vcpu_lock(struct vcpu *vcpu);
Olivier Deprez0b6f10a2020-08-05 18:21:33 +0200226struct two_vcpu_locked vcpu_lock_both(struct vcpu *vcpu1, struct vcpu *vcpu2);
Fuad Tabba5c738432019-12-02 11:02:42 +0000227void vcpu_unlock(struct vcpu_locked *locked);
228void vcpu_init(struct vcpu *vcpu, struct vm *vm);
229void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100230ffa_vcpu_index_t vcpu_index(const struct vcpu *vcpu);
Fuad Tabba5c738432019-12-02 11:02:42 +0000231bool vcpu_is_off(struct vcpu_locked vcpu);
Max Shvetsov40108e72020-08-27 12:39:50 +0100232bool vcpu_secondary_reset_and_start(struct vcpu_locked vcpu_locked,
233 ipaddr_t entry, uintreg_t arg);
Fuad Tabba5c738432019-12-02 11:02:42 +0000234
235bool vcpu_handle_page_fault(const struct vcpu *current,
236 struct vcpu_fault_info *f);
Olivier Deprez2ebae3a2020-06-11 16:34:30 +0200237
Olivier Depreze6f7b9d2021-02-01 11:55:48 +0100238void vcpu_reset(struct vcpu *vcpu);
Manish Pandey35e452f2021-02-18 21:36:34 +0000239
J-Alves7ac49052022-02-08 17:20:53 +0000240void vcpu_set_phys_core_idx(struct vcpu *vcpu);
241
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100242static inline bool vcpu_is_virt_interrupt_enabled(struct interrupts *interrupts,
243 uint32_t intid)
244{
245 return interrupt_bitmap_get_value(&interrupts->interrupt_enabled,
246 intid) == 1U;
247}
248
249static inline void vcpu_virt_interrupt_set_enabled(
250 struct interrupts *interrupts, uint32_t intid)
251{
252 interrupt_bitmap_set_value(&interrupts->interrupt_enabled, intid);
253}
254
255static inline void vcpu_virt_interrupt_clear_enabled(
256 struct interrupts *interrupts, uint32_t intid)
257{
258 interrupt_bitmap_clear_value(&interrupts->interrupt_enabled, intid);
259}
260
261static inline bool vcpu_is_virt_interrupt_pending(struct interrupts *interrupts,
262 uint32_t intid)
263{
264 return interrupt_bitmap_get_value(&interrupts->interrupt_pending,
265 intid) == 1U;
266}
267
268static inline void vcpu_virt_interrupt_set_pending(
269 struct interrupts *interrupts, uint32_t intid)
270{
271 interrupt_bitmap_set_value(&interrupts->interrupt_pending, intid);
272}
273
274static inline void vcpu_virt_interrupt_clear_pending(
275 struct interrupts *interrupts, uint32_t intid)
276{
277 interrupt_bitmap_clear_value(&interrupts->interrupt_pending, intid);
278}
279
280static inline enum interrupt_type vcpu_virt_interrupt_get_type(
281 struct interrupts *interrupts, uint32_t intid)
282{
283 return (enum interrupt_type)interrupt_bitmap_get_value(
284 &interrupts->interrupt_type, intid);
285}
286
287static inline void vcpu_virt_interrupt_set_type(struct interrupts *interrupts,
288 uint32_t intid,
289 enum interrupt_type type)
290{
291 if (type == INTERRUPT_TYPE_IRQ) {
292 interrupt_bitmap_clear_value(&interrupts->interrupt_type,
293 intid);
294 } else {
295 interrupt_bitmap_set_value(&interrupts->interrupt_type, intid);
296 }
297}
298
Manish Pandey35e452f2021-02-18 21:36:34 +0000299static inline void vcpu_irq_count_increment(struct vcpu_locked vcpu_locked)
300{
301 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count++;
302}
303
304static inline void vcpu_irq_count_decrement(struct vcpu_locked vcpu_locked)
305{
306 vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count--;
307}
308
309static inline void vcpu_fiq_count_increment(struct vcpu_locked vcpu_locked)
310{
311 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count++;
312}
313
314static inline void vcpu_fiq_count_decrement(struct vcpu_locked vcpu_locked)
315{
316 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count--;
317}
318
Daniel Boulby4ca50f02022-07-29 18:29:34 +0100319static inline void vcpu_interrupt_count_increment(
320 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
321 uint32_t intid)
322{
323 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
324 INTERRUPT_TYPE_IRQ) {
325 vcpu_irq_count_increment(vcpu_locked);
326 } else {
327 vcpu_fiq_count_increment(vcpu_locked);
328 }
329}
330
331static inline void vcpu_interrupt_count_decrement(
332 struct vcpu_locked vcpu_locked, struct interrupts *interrupts,
333 uint32_t intid)
334{
335 if (vcpu_virt_interrupt_get_type(interrupts, intid) ==
336 INTERRUPT_TYPE_IRQ) {
337 vcpu_irq_count_decrement(vcpu_locked);
338 } else {
339 vcpu_fiq_count_decrement(vcpu_locked);
340 }
341}
342
Manish Pandey35e452f2021-02-18 21:36:34 +0000343static inline uint32_t vcpu_interrupt_irq_count_get(
344 struct vcpu_locked vcpu_locked)
345{
346 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count;
347}
348
349static inline uint32_t vcpu_interrupt_fiq_count_get(
350 struct vcpu_locked vcpu_locked)
351{
352 return vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
353}
354
355static inline uint32_t vcpu_interrupt_count_get(struct vcpu_locked vcpu_locked)
356{
357 return vcpu_locked.vcpu->interrupts.enabled_and_pending_irq_count +
358 vcpu_locked.vcpu->interrupts.enabled_and_pending_fiq_count;
359}
Madhukar Pappireddy5992fbc2022-06-21 17:15:16 -0500360
361static inline void vcpu_call_chain_extend(struct vcpu *vcpu1,
362 struct vcpu *vcpu2)
363{
364 vcpu1->call_chain.next_node = vcpu2;
365 vcpu2->call_chain.prev_node = vcpu1;
366}
367
368static inline void vcpu_call_chain_remove_node(struct vcpu *vcpu1,
369 struct vcpu *vcpu2)
370{
371 vcpu1->call_chain.prev_node = NULL;
372 vcpu2->call_chain.next_node = NULL;
373}