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