blob: e891516738c0bd1db67bc1e8e446334050b7db85 [file] [log] [blame]
Marc Bonnici5096aeb2021-12-01 17:57:04 +00001/*
2 * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef SPMC_H
8#define SPMC_H
9
10#include <stdint.h>
11
12#include <lib/psci/psci.h>
13#include <lib/spinlock.h>
14#include "spm_common.h"
15
16/*
17 * Ranges of FF-A IDs for Normal world and Secure world components. The
18 * convention matches that used by other SPMCs i.e. Hafnium and OP-TEE.
19 */
20#define FFA_NWD_ID_BASE 0x0
21#define FFA_NWD_ID_LIMIT 0x7FFF
22#define FFA_SWD_ID_BASE 0x8000
23#define FFA_SWD_ID_LIMIT SPMD_DIRECT_MSG_ENDPOINT_ID - 1
24#define FFA_SWD_ID_MASK 0x8000
25
26/* First ID is reserved for the SPMC */
27#define FFA_SPMC_ID U(FFA_SWD_ID_BASE)
28/* SP IDs are allocated after the SPMC ID */
29#define FFA_SP_ID_BASE (FFA_SPMC_ID + 1)
30/* Align with Hafnium implementation */
31#define INV_SP_ID 0x7FFF
32
33/* FF-A warm boot types. */
34#define FFA_WB_TYPE_S2RAM 0
35#define FFA_WB_TYPE_NOTS2RAM 1
36
37/*
38 * Runtime states of an execution context as per the FF-A v1.1 specification.
39 */
40enum sp_runtime_states {
41 RT_STATE_WAITING,
42 RT_STATE_RUNNING,
43 RT_STATE_PREEMPTED,
44 RT_STATE_BLOCKED
45};
46
47/*
48 * Runtime model of an execution context as per the FF-A v1.1 specification. Its
49 * value is valid only if the execution context is not in the waiting state.
50 */
51enum sp_runtime_model {
52 RT_MODEL_DIR_REQ,
53 RT_MODEL_RUN,
54 RT_MODEL_INIT,
55 RT_MODEL_INTR
56};
57
58enum sp_runtime_el {
59 EL1 = 0,
60 S_EL0,
61 S_EL1
62};
63
64enum sp_execution_state {
65 SP_STATE_AARCH64 = 0,
66 SP_STATE_AARCH32
67};
68
69/*
70 * Execution context members for an SP. This is a bit like struct
71 * vcpu in a hypervisor.
72 */
73struct sp_exec_ctx {
74 /*
75 * Store the stack address to restore C runtime context from after
76 * returning from a synchronous entry into the SP.
77 */
78 uint64_t c_rt_ctx;
79
80 /* Space to maintain the architectural state of an SP. */
81 cpu_context_t cpu_ctx;
82
83 /* Track the current runtime state of the SP. */
84 enum sp_runtime_states rt_state;
85
86 /* Track the current runtime model of the SP. */
87 enum sp_runtime_model rt_model;
88};
89
90/*
91 * Structure to describe the cumulative properties of an SP.
92 */
93struct secure_partition_desc {
94 /*
95 * Execution contexts allocated to this endpoint. Ideally,
96 * we need as many contexts as there are physical cpus only
97 * for a S-EL1 SP which is MP-pinned.
98 */
99 struct sp_exec_ctx ec[PLATFORM_CORE_COUNT];
100
101 /* ID of the Secure Partition. */
102 uint16_t sp_id;
103
104 /* Runtime EL. */
105 enum sp_runtime_el runtime_el;
106
107 /* Partition UUID. */
108 uint32_t uuid[4];
109
110 /* Partition Properties. */
111 uint32_t properties;
112
113 /* Supported FF-A Version. */
114 uint32_t ffa_version;
115
116 /* Execution State. */
117 enum sp_execution_state execution_state;
118
119 /* Secondary entrypoint. Only valid for a S-EL1 SP. */
120 uintptr_t secondary_ep;
121};
122
123/*
124 * This define identifies the only SP that will be initialised and participate
125 * in FF-A communication. The implementation leaves the door open for more SPs
126 * to be managed in future but for now it is reasonable to assume that either a
127 * single S-EL0 or a single S-EL1 SP will be supported. This define will be used
128 * to identify which SP descriptor to initialise and manage during SP runtime.
129 */
130#define ACTIVE_SP_DESC_INDEX 0
131
132/*
133 * Structure to describe the cumulative properties of the Hypervisor and
134 * NS-Endpoints.
135 */
136struct ns_endpoint_desc {
137 /*
138 * ID of the NS-Endpoint or Hypervisor.
139 */
140 uint16_t ns_ep_id;
141
142 /*
143 * Supported FF-A Version.
144 */
145 uint32_t ffa_version;
146};
147
148/* Setup Function for different SP types. */
149void spmc_sp_common_setup(struct secure_partition_desc *sp,
150 entry_point_info_t *ep_info);
151void spmc_el1_sp_setup(struct secure_partition_desc *sp,
152 entry_point_info_t *ep_info);
153void spmc_sp_common_ep_commit(struct secure_partition_desc *sp,
154 entry_point_info_t *ep_info);
155
156/*
157 * Helper function to perform a synchronous entry into a SP.
158 */
159uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec);
160
161/*
162 * Helper function to obtain the descriptor of the current SP on a physical cpu.
163 */
164struct secure_partition_desc *spmc_get_current_sp_ctx(void);
165
166/*
167 * Helper function to obtain the execution context of an SP on a
168 * physical cpu.
169 */
170struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp);
171
172/*
173 * Helper function to obtain the index of the execution context of an SP on a
174 * physical cpu.
175 */
176unsigned int get_ec_index(struct secure_partition_desc *sp);
177
178uint64_t spmc_ffa_error_return(void *handle, int error_code);
179
180/*
181 * Ensure a partition ID does not clash and follows the secure world convention.
182 */
183bool is_ffa_secure_id_valid(uint16_t partition_id);
184
185#endif /* SPMC_H */