blob: d62be91ebde04590cfa750e8bc2c80941aadd8c7 [file] [log] [blame]
Marc Bonnici8e1a7552021-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>
Marc Bonnici9a297042022-02-14 17:06:09 +000014#include <services/el3_spmc_logical_sp.h>
Marc Bonnici8e1a7552021-12-01 17:57:04 +000015#include "spm_common.h"
16
17/*
18 * Ranges of FF-A IDs for Normal world and Secure world components. The
19 * convention matches that used by other SPMCs i.e. Hafnium and OP-TEE.
20 */
21#define FFA_NWD_ID_BASE 0x0
22#define FFA_NWD_ID_LIMIT 0x7FFF
23#define FFA_SWD_ID_BASE 0x8000
24#define FFA_SWD_ID_LIMIT SPMD_DIRECT_MSG_ENDPOINT_ID - 1
25#define FFA_SWD_ID_MASK 0x8000
26
Marc Bonnici8eb15202021-11-29 17:05:33 +000027/* ID 0 is reserved for the normal world entity, (Hypervisor or OS Kernel). */
28#define FFA_NWD_ID U(0)
Marc Bonnici8e1a7552021-12-01 17:57:04 +000029/* First ID is reserved for the SPMC */
30#define FFA_SPMC_ID U(FFA_SWD_ID_BASE)
31/* SP IDs are allocated after the SPMC ID */
32#define FFA_SP_ID_BASE (FFA_SPMC_ID + 1)
33/* Align with Hafnium implementation */
34#define INV_SP_ID 0x7FFF
35
Marc Bonnicid4bb2452021-12-13 11:08:59 +000036/* FF-A Related helper macros. */
Marc Bonnici764e6672021-08-31 17:57:04 +010037#define FFA_ID_MASK U(0xFFFF)
38#define FFA_PARTITION_ID_SHIFT U(16)
Marc Bonnicid4bb2452021-12-13 11:08:59 +000039#define FFA_FEATURES_BIT31_MASK U(0x1u << 31)
Marc Bonnici08f28ef2022-04-19 16:52:59 +010040#define FFA_FEATURES_RET_REQ_NS_BIT U(0x1 << 1)
Marc Bonnicid4bb2452021-12-13 11:08:59 +000041
Marc Bonnici764e6672021-08-31 17:57:04 +010042#define FFA_RUN_EP_ID(ep_vcpu_ids) \
43 ((ep_vcpu_ids >> FFA_PARTITION_ID_SHIFT) & FFA_ID_MASK)
44#define FFA_RUN_VCPU_ID(ep_vcpu_ids) \
45 (ep_vcpu_ids & FFA_ID_MASK)
46
Marc Bonnici0cf1a152021-08-25 12:09:37 +010047#define FFA_PAGE_SIZE (4096)
48#define FFA_RXTX_PAGE_COUNT_MASK 0x1F
49
50/* Ensure that the page size used by TF-A is 4k aligned. */
51CASSERT((PAGE_SIZE % FFA_PAGE_SIZE) == 0, assert_aligned_page_size);
52
Marc Bonnici8e1a7552021-12-01 17:57:04 +000053/*
Marc Bonnici25f4b542022-04-12 17:18:13 +010054 * Defines to allow an SP to subscribe for power management messages
55 */
56#define FFA_PM_MSG_SUB_CPU_OFF U(1 << 0)
57#define FFA_PM_MSG_SUB_CPU_SUSPEND U(1 << 1)
58#define FFA_PM_MSG_SUB_CPU_SUSPEND_RESUME U(1 << 2)
59
60/*
Marc Bonnici8e1a7552021-12-01 17:57:04 +000061 * Runtime states of an execution context as per the FF-A v1.1 specification.
62 */
63enum sp_runtime_states {
64 RT_STATE_WAITING,
65 RT_STATE_RUNNING,
66 RT_STATE_PREEMPTED,
67 RT_STATE_BLOCKED
68};
69
70/*
71 * Runtime model of an execution context as per the FF-A v1.1 specification. Its
72 * value is valid only if the execution context is not in the waiting state.
73 */
74enum sp_runtime_model {
75 RT_MODEL_DIR_REQ,
76 RT_MODEL_RUN,
77 RT_MODEL_INIT,
78 RT_MODEL_INTR
79};
80
81enum sp_runtime_el {
82 EL1 = 0,
83 S_EL0,
84 S_EL1
85};
86
87enum sp_execution_state {
88 SP_STATE_AARCH64 = 0,
89 SP_STATE_AARCH32
90};
91
Marc Bonniciecc460a2021-09-02 13:18:41 +010092enum mailbox_state {
93 /* There is no message in the mailbox. */
94 MAILBOX_STATE_EMPTY,
95
96 /* There is a message that has been populated in the mailbox. */
97 MAILBOX_STATE_FULL,
98};
99
100struct mailbox {
101 enum mailbox_state state;
102
103 /* RX/TX Buffers. */
104 void *rx_buffer;
105 const void *tx_buffer;
106
107 /* Size of RX/TX Buffer. */
108 uint32_t rxtx_page_count;
109
110 /* Lock access to mailbox. */
111 spinlock_t lock;
112};
113
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000114/*
115 * Execution context members for an SP. This is a bit like struct
116 * vcpu in a hypervisor.
117 */
118struct sp_exec_ctx {
119 /*
120 * Store the stack address to restore C runtime context from after
121 * returning from a synchronous entry into the SP.
122 */
123 uint64_t c_rt_ctx;
124
125 /* Space to maintain the architectural state of an SP. */
126 cpu_context_t cpu_ctx;
127
128 /* Track the current runtime state of the SP. */
129 enum sp_runtime_states rt_state;
130
131 /* Track the current runtime model of the SP. */
132 enum sp_runtime_model rt_model;
133};
134
135/*
136 * Structure to describe the cumulative properties of an SP.
137 */
138struct secure_partition_desc {
139 /*
140 * Execution contexts allocated to this endpoint. Ideally,
141 * we need as many contexts as there are physical cpus only
142 * for a S-EL1 SP which is MP-pinned.
143 */
144 struct sp_exec_ctx ec[PLATFORM_CORE_COUNT];
145
146 /* ID of the Secure Partition. */
147 uint16_t sp_id;
148
149 /* Runtime EL. */
150 enum sp_runtime_el runtime_el;
151
152 /* Partition UUID. */
153 uint32_t uuid[4];
154
155 /* Partition Properties. */
156 uint32_t properties;
157
158 /* Supported FF-A Version. */
159 uint32_t ffa_version;
160
161 /* Execution State. */
162 enum sp_execution_state execution_state;
163
Marc Bonniciecc460a2021-09-02 13:18:41 +0100164 /* Mailbox tracking. */
165 struct mailbox mailbox;
166
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000167 /* Secondary entrypoint. Only valid for a S-EL1 SP. */
168 uintptr_t secondary_ep;
Marc Bonnici25f4b542022-04-12 17:18:13 +0100169
170 /*
171 * Store whether the SP has subscribed to any power management messages.
172 */
173 uint16_t pwr_mgmt_msgs;
Marc Bonnici08f28ef2022-04-19 16:52:59 +0100174
175 /*
176 * Store whether the SP has requested the use of the NS bit for memory
177 * management transactions if it is using FF-A v1.0.
178 */
179 bool ns_bit_requested;
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000180};
181
182/*
183 * This define identifies the only SP that will be initialised and participate
184 * in FF-A communication. The implementation leaves the door open for more SPs
185 * to be managed in future but for now it is reasonable to assume that either a
186 * single S-EL0 or a single S-EL1 SP will be supported. This define will be used
187 * to identify which SP descriptor to initialise and manage during SP runtime.
188 */
189#define ACTIVE_SP_DESC_INDEX 0
190
191/*
192 * Structure to describe the cumulative properties of the Hypervisor and
193 * NS-Endpoints.
194 */
195struct ns_endpoint_desc {
196 /*
197 * ID of the NS-Endpoint or Hypervisor.
198 */
199 uint16_t ns_ep_id;
200
201 /*
Marc Bonniciecc460a2021-09-02 13:18:41 +0100202 * Mailbox tracking.
203 */
204 struct mailbox mailbox;
205
206 /*
207 * Supported FF-A Version
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000208 */
209 uint32_t ffa_version;
210};
211
Marc Bonnici37dd8e12021-08-17 18:00:07 +0100212/**
213 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
214 * interface.
215 */
216struct ffa_partition_info_v1_0 {
217 uint16_t ep_id;
218 uint16_t execution_ctx_count;
219 uint32_t properties;
220};
221
222/* Extended structure for v1.1. */
223struct ffa_partition_info_v1_1 {
224 uint16_t ep_id;
225 uint16_t execution_ctx_count;
226 uint32_t properties;
227 uint32_t uuid[4];
228};
229
Marc Bonnici25f4b542022-04-12 17:18:13 +0100230/* Reference to power management hooks */
231extern const spd_pm_ops_t spmc_pm;
232
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000233/* Setup Function for different SP types. */
234void spmc_sp_common_setup(struct secure_partition_desc *sp,
Achin Guptaeaf17162021-10-19 12:21:16 +0100235 entry_point_info_t *ep_info,
236 int32_t boot_info_reg);
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000237void spmc_el1_sp_setup(struct secure_partition_desc *sp,
238 entry_point_info_t *ep_info);
239void spmc_sp_common_ep_commit(struct secure_partition_desc *sp,
240 entry_point_info_t *ep_info);
241
242/*
243 * Helper function to perform a synchronous entry into a SP.
244 */
245uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec);
246
247/*
248 * Helper function to obtain the descriptor of the current SP on a physical cpu.
249 */
250struct secure_partition_desc *spmc_get_current_sp_ctx(void);
251
252/*
253 * Helper function to obtain the execution context of an SP on a
254 * physical cpu.
255 */
256struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp);
257
258/*
259 * Helper function to obtain the index of the execution context of an SP on a
260 * physical cpu.
261 */
262unsigned int get_ec_index(struct secure_partition_desc *sp);
263
264uint64_t spmc_ffa_error_return(void *handle, int error_code);
265
266/*
267 * Ensure a partition ID does not clash and follows the secure world convention.
268 */
269bool is_ffa_secure_id_valid(uint16_t partition_id);
270
Marc Bonnici9a297042022-02-14 17:06:09 +0000271/*
272 * Helper function to obtain the array storing the EL3
273 * Logical Partition descriptors.
274 */
275struct el3_lp_desc *get_el3_lp_array(void);
276
Marc Bonnicia2cfa612021-11-24 10:33:48 +0000277/*
278 * Helper function to obtain the RX/TX buffer pair descriptor of the Hypervisor
279 * or OS kernel in the normal world or the last SP that was run.
280 */
281struct mailbox *spmc_get_mbox_desc(bool secure_origin);
282
Marc Bonnici336630f2022-01-13 11:39:10 +0000283/*
284 * Helper function to obtain the context of an SP with a given partition ID.
285 */
286struct secure_partition_desc *spmc_get_sp_ctx(uint16_t id);
287
Marc Bonnicid1907f02022-04-19 17:42:53 +0100288/*
289 * Add helper function to obtain the FF-A version of the calling
290 * partition.
291 */
292uint32_t get_partition_ffa_version(bool secure_origin);
293
Marc Bonnici336630f2022-01-13 11:39:10 +0000294
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000295#endif /* SPMC_H */