blob: b9b344a17538f5b3d04568666291a6f6104a68a3 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Gyorgy Szing40a7af02019-02-06 14:19:47 +01002 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __SPM_API_H__
9#define __SPM_API_H__
10
11/* This file contains the apis exported by the SPM to tfm core */
Mate Toth-Pal3db437a2018-06-22 16:15:13 +020012#include "tfm_api.h"
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010013#include "spm_partition_defs.h"
David Hu49a28eb2019-08-14 18:18:15 +080014#include "tfm_secure_api.h"
Mingyang Sunf3d29892019-07-10 17:50:23 +080015#include <stdbool.h>
Edison Ai66fbdf12019-07-08 16:05:07 +080016#ifdef TFM_PSA_API
17#include "tfm_list.h"
18#include "tfm_wait.h"
Mingyang Sunf3d29892019-07-10 17:50:23 +080019#include "tfm_message_queue.h"
20#include "tfm_secure_api.h"
Summer Qinb5da9cc2019-08-26 15:19:45 +080021#include "tfm_thread.h"
Edison Ai66fbdf12019-07-08 16:05:07 +080022#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000023
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010024#define SPM_INVALID_PARTITION_IDX (~0U)
25
Summer Qineb537e52019-03-29 09:57:10 +080026/* Privileged definitions for partition thread mode */
27#define TFM_PARTITION_PRIVILEGED_MODE 1
28#define TFM_PARTITION_UNPRIVILEGED_MODE 0
29
Miklos Balint386b8b52017-11-29 13:12:32 +000030enum spm_err_t {
31 SPM_ERR_OK = 0,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010032 SPM_ERR_PARTITION_DB_NOT_INIT,
33 SPM_ERR_PARTITION_ALREADY_ACTIVE,
34 SPM_ERR_PARTITION_NOT_AVAILABLE,
Hugues de Valonf704c802019-02-19 14:51:41 +000035 SPM_ERR_INVALID_PARAMETER,
Miklos Balint386b8b52017-11-29 13:12:32 +000036 SPM_ERR_INVALID_CONFIG,
37};
38
Hugues de Valon99578562019-06-18 16:08:51 +010039#define SPM_PARTITION_STATE_UNINIT 0
40#define SPM_PARTITION_STATE_IDLE 1
41#define SPM_PARTITION_STATE_RUNNING 2
42#define SPM_PARTITION_STATE_HANDLING_IRQ 3
43#define SPM_PARTITION_STATE_SUSPENDED 4
44#define SPM_PARTITION_STATE_BLOCKED 5
45#define SPM_PARTITION_STATE_CLOSED 6
Mate Toth-Pal65291f32018-02-23 14:35:22 +010046
Hugues de Valon99578562019-06-18 16:08:51 +010047#define SPM_PART_FLAG_APP_ROT 0x01
48#define SPM_PART_FLAG_PSA_ROT 0x02
49#define SPM_PART_FLAG_IPC 0x04
Mate Toth-Pal59398712018-02-28 17:06:40 +010050
Edison Ai66fbdf12019-07-08 16:05:07 +080051#ifndef TFM_PSA_API
Miklos Balint386b8b52017-11-29 13:12:32 +000052/**
Mate Toth-Pal3db437a2018-06-22 16:15:13 +020053 * \brief Holds the iovec parameters that are passed to a service
54 *
55 * \note The size of the structure is (and have to be) multiple of 8 bytes
56 */
57struct iovec_args_t {
58 psa_invec in_vec[PSA_MAX_IOVEC]; /*!< Array of psa_invec objects */
59 size_t in_len; /*!< Number psa_invec objects in in_vec
60 */
61 psa_outvec out_vec[PSA_MAX_IOVEC]; /*!< Array of psa_outvec objects */
62 size_t out_len; /*!< Number psa_outvec objects in out_vec
63 */
64};
Mingyang Sunda01a972019-07-12 17:32:59 +080065
66/* The size of this struct must be multiple of 4 bytes as it is stacked to an
67 * uint32_t[] array
68 */
69struct interrupted_ctx_stack_frame_t {
Mingyang Sunda01a972019-07-12 17:32:59 +080070 uint32_t partition_state;
71};
72
73/* The size of this struct must be multiple of 4 bytes as it is stacked to an
74 * uint32_t[] array
75 */
76struct handler_ctx_stack_frame_t {
77 uint32_t partition_state;
78 uint32_t caller_partition_idx;
79};
Edison Ai66fbdf12019-07-08 16:05:07 +080080#endif /* !define(TFM_PSA_API) */
Mate Toth-Pal3db437a2018-06-22 16:15:13 +020081
82/**
Mate Toth-Pal18b83922018-02-26 17:58:18 +010083 * \brief Runtime context information of a partition
84 */
85struct spm_partition_runtime_data_t {
Edison Ai66fbdf12019-07-08 16:05:07 +080086#ifdef TFM_PSA_API
87 struct tfm_event_t signal_evnt; /* Event signal */
88 uint32_t signals; /* Service signals had been triggered*/
89 struct tfm_list_node_t service_list;/* Service list */
Summer Qinb5da9cc2019-08-26 15:19:45 +080090 struct tfm_thrd_ctx sp_thrd; /* Thread context */
Edison Ai66fbdf12019-07-08 16:05:07 +080091#else /* TFM_PSA_API */
Mate Toth-Pal18b83922018-02-26 17:58:18 +010092 uint32_t partition_state;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010093 uint32_t caller_partition_idx;
Mate Toth-Pal21a74c92018-04-13 14:05:41 +020094 int32_t caller_client_id;
Mate Toth-Pal18b83922018-02-26 17:58:18 +010095 uint32_t share;
Mate Toth-Pal18b83922018-02-26 17:58:18 +010096 uint32_t stack_ptr;
Miklos Balintace4c3f2018-07-30 12:31:15 +020097 uint32_t lr;
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +010098 int32_t iovec_api; /*!< Whether the function in the partition
99 * had been called using the iovec API.
100 * FIXME: Remove the field once this is the
101 * only option
102 */
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200103 struct iovec_args_t iovec_args;
104 psa_outvec *orig_outvec;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200105 uint32_t *ctx_stack_ptr;
Edison Ai66fbdf12019-07-08 16:05:07 +0800106#endif /* TFM_PSA_API */
107 uint32_t signal_mask; /*
108 * Service signal mask passed by
109 * psa_wait()
110 */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100111};
112
Mingyang Sunda01a972019-07-12 17:32:59 +0800113#ifdef TFM_PSA_API
114
Mingyang Sunda01a972019-07-12 17:32:59 +0800115#define TFM_VERSION_POLICY_RELAXED 0
116#define TFM_VERSION_POLICY_STRICT 1
117
Edison Ai97115822019-08-01 14:22:19 +0800118#define TFM_CONN_HANDLE_MAX_NUM 16
Mingyang Sunda01a972019-07-12 17:32:59 +0800119
120/* RoT connection handle list */
121struct tfm_conn_handle_t {
Edison Ai9cc26242019-08-06 11:28:04 +0800122 void *rhandle; /* Reverse handle value */
Edison Ai97115822019-08-01 14:22:19 +0800123 struct tfm_msg_body_t internal_msg; /* Internal message for message queue */
Edison Ai9cc26242019-08-06 11:28:04 +0800124 struct tfm_spm_service_t *service; /* RoT service pointer */
125 struct tfm_list_node_t list; /* list node */
Mingyang Sunda01a972019-07-12 17:32:59 +0800126};
127
128/* Service database defined by manifest */
129struct tfm_spm_service_db_t {
130 char *name; /* Service name */
131 uint32_t partition_id; /* Partition ID which service belong to */
132 psa_signal_t signal; /* Service signal */
133 uint32_t sid; /* Service identifier */
134 bool non_secure_client; /* If can be called by non secure client */
135 uint32_t minor_version; /* Minor version */
136 uint32_t minor_policy; /* Minor version policy */
137};
138
139/* RoT Service data */
140struct tfm_spm_service_t {
Summer Qine578c5b2019-08-16 16:42:16 +0800141 const struct tfm_spm_service_db_t *service_db;/* Service database pointer */
Mingyang Sunda01a972019-07-12 17:32:59 +0800142 struct spm_partition_desc_t *partition; /*
143 * Point to secure partition
144 * data
145 */
146 struct tfm_list_node_t handle_list; /* Service handle list */
147 struct tfm_msg_queue_t msg_queue; /* Message queue */
148 struct tfm_list_node_t list; /* For list operation */
149};
150#endif /* ifdef(TFM_PSA_API) */
151
152/*********************** common definitions ***********************/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100153
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100154/**
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100155 * \brief Returns the index of the partition with the given partition ID.
Miklos Balint386b8b52017-11-29 13:12:32 +0000156 *
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100157 * \param[in] partition_id Partition id
Miklos Balint386b8b52017-11-29 13:12:32 +0000158 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100159 * \return the partition idx if partition_id is valid,
160 * \ref SPM_INVALID_PARTITION_IDX othervise
161 */
162uint32_t get_partition_idx(uint32_t partition_id);
163
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200164/**
Summer Qinb4a854d2019-05-29 15:31:22 +0800165 * \brief Get the id of the partition for its index from the db
166 *
167 * \param[in] partition_idx Partition index
168 *
169 * \return Partition ID for that partition
170 *
171 * \note This function doesn't check if partition_idx is valid.
172 */
173uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx);
174
175/**
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200176 * \brief Get the flags associated with a partition
177 *
178 * \param[in] partition_idx Partition index
179 *
180 * \return Flags associated with the partition
181 *
182 * \note This function doesn't check if partition_idx is valid.
183 */
184uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx);
185
Mingyang Sunda01a972019-07-12 17:32:59 +0800186/**
187 * \brief Initialize partition database
188 *
189 * \return Error code \ref spm_err_t
190 */
191enum spm_err_t tfm_spm_db_init(void);
192
193/**
194 * \brief Change the privilege mode for partition thread mode.
195 *
196 * \param[in] privileged Privileged mode,
197 * \ref TFM_PARTITION_PRIVILEGED_MODE
198 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
199 *
200 * \note Barrier instructions are not called by this function, and if
201 * it is called in thread mode, it might be necessary to call
Edison Ai7aff9e82019-07-11 14:56:46 +0800202 * them after this function returns.
Mingyang Sunda01a972019-07-12 17:32:59 +0800203 */
204void tfm_spm_partition_change_privilege(uint32_t privileged);
205
206/*********************** library definitions ***********************/
207
Summer Qinb4a854d2019-05-29 15:31:22 +0800208#ifndef TFM_PSA_API
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200209/**
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200210 * \brief Save interrupted partition context on ctx stack
211 *
212 * \param[in] partition_idx Partition index
213 *
214 * \note This function doesn't check if partition_idx is valid.
215 * \note This function doesn't whether the ctx stack overflows.
216 */
217void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx);
218
219/**
220 * \brief Restores interrupted partition context on ctx stack
221 *
222 * \param[in] partition_idx Partition index
223 *
224 * \note This function doesn't check if partition_idx is valid.
225 * \note This function doesn't whether the ctx stack underflows.
226 */
227void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx);
228
229/**
230 * \brief Save handler partition context on ctx stack
231 *
232 * \param[in] partition_idx Partition index
233 *
234 * \note This function doesn't check if partition_idx is valid.
235 * \note This function doesn't whether the ctx stack overflows.
236 */
237void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx);
238
239/**
240 * \brief Restores handler partition context on ctx stack
241 *
242 * \param[in] partition_idx Partition index
243 *
244 * \note This function doesn't check if partition_idx is valid.
245 * \note This function doesn't whether the ctx stack underflows.
246 */
247void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx);
248
249/**
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100250 * \brief Get the current runtime data of a partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100251 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100252 * \param[in] partition_idx Partition index
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100253 *
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100254 * \return The runtime data of the specified partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100255 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100256 * \note This function doesn't check if partition_idx is valid.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100257 */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100258const struct spm_partition_runtime_data_t *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100259 tfm_spm_partition_get_runtime_data(uint32_t partition_idx);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100260
261/**
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100262 * \brief Returns the index of the partition that has running state
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100263 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100264 * \return The index of the partition with the running state, if there is any
265 * set. 0 otherwise.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100266 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100267uint32_t tfm_spm_partition_get_running_partition_idx(void);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100268
269/**
Miklos Balintace4c3f2018-07-30 12:31:15 +0200270 * \brief Save stack pointer and link register for partition in database
271 *
272 * \param[in] partition_idx Partition index
273 * \param[in] stack_ptr Stack pointer to be stored
274 * \param[in] lr Link register to be stored
275 *
276 * \note This function doesn't check if partition_idx is valid.
277 */
278void tfm_spm_partition_store_context(uint32_t partition_idx,
279 uint32_t stack_ptr, uint32_t lr);
280
281/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100282 * \brief Set the current state of a partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100283 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100284 * \param[in] partition_idx Partition index
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100285 * \param[in] state The state to be set
286 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100287 * \note This function doesn't check if partition_idx is valid.
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100288 * \note The state has to have the value set of \ref spm_part_state_t.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100289 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100290void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100291
292/**
Miklos Balint6a139ae2018-04-04 19:44:37 +0200293 * \brief Set the caller partition index for a given partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100294 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100295 * \param[in] partition_idx Partition index
Miklos Balint6a139ae2018-04-04 19:44:37 +0200296 * \param[in] caller_partition_idx The index of the caller partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100297 *
Miklos Balint6a139ae2018-04-04 19:44:37 +0200298 * \note This function doesn't check if any of the partition_idxs are valid.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100299 */
Miklos Balint6a139ae2018-04-04 19:44:37 +0200300void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
301 uint32_t caller_partition_idx);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100302
303/**
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200304* \brief Set the caller client ID for a given partition
305*
306* \param[in] partition_idx Partition index
307* \param[in] caller_client_id The ID of the calling client
308*
309* \note This function doesn't check if any of the partition_idxs are valid.
310*/
311void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
312 int32_t caller_client_id);
313
314/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100315 * \brief Set the buffer share region of the partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100316 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100317 * \param[in] partition_idx Partition index
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100318 * \param[in] share The buffer share region to be set
319 *
320 * \return Error code \ref spm_err_t
321 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100322 * \note This function doesn't check if partition_idx is valid.
Hugues de Valon99578562019-06-18 16:08:51 +0100323 * \note share has to have one of the buffer share values:
324 * - TFM_BUFFER_SHARE_DISABLE
325 * - TFM_BUFFER_SHARE_NS_CODE
326 * - TFM_BUFFER_SHARE_SCRATCH
327 * - TFM_BUFFER_SHARE_PRIV
328 * - TFM_BUFFER_SHARE_DEFAULT
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100329 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100330enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100331 uint32_t share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100332
333/**
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200334 * \brief Set the iovec parameters for the partition
335 *
336 * \param[in] partition_idx Partition index
337 * \param[in] args The arguments of the secure function
338 *
339 * args is expected to be of type int32_t[4] where:
340 * args[0] is in_vec
341 * args[1] is in_len
342 * args[2] is out_vec
343 * args[3] is out_len
344 *
Hugues de Valonf704c802019-02-19 14:51:41 +0000345 * \return Error code \ref spm_err_t
346 *
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200347 * \note This function doesn't check if partition_idx is valid.
348 * \note This function assumes that the iovecs that are passed in args are
349 * valid, and does no sanity check on them at all.
350 */
Hugues de Valonf704c802019-02-19 14:51:41 +0000351enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx,
352 const int32_t *args);
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200353
354/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100355 * \brief Execute partition init function
Miklos Balint386b8b52017-11-29 13:12:32 +0000356 *
357 * \return Error code \ref spm_err_t
358 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100359enum spm_err_t tfm_spm_partition_init(void);
Miklos Balint386b8b52017-11-29 13:12:32 +0000360
361/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100362 * \brief Clears the context info from the database for a partition.
Miklos Balint386b8b52017-11-29 13:12:32 +0000363 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100364 * \param[in] partition_idx Partition index
Miklos Balint386b8b52017-11-29 13:12:32 +0000365 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100366 * \note This function doesn't check if partition_idx is valid.
Miklos Balint386b8b52017-11-29 13:12:32 +0000367 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100368void tfm_spm_partition_cleanup_context(uint32_t partition_idx);
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200369
370/**
371 * \brief Set the signal mask for a given partition
372 *
373 * \param[in] partition_idx Partition index
374 * \param[in] signal_mask The signal mask to be set for the partition
375 *
376 * \note This function doesn't check if any of the partition_idxs are valid.
377 */
378void tfm_spm_partition_set_signal_mask(uint32_t partition_idx,
379 uint32_t signal_mask);
Summer Qinb4a854d2019-05-29 15:31:22 +0800380#endif /* !defined(TFM_PSA_API) */
381
Mingyang Sunf3d29892019-07-10 17:50:23 +0800382#ifdef TFM_PSA_API
Mingyang Sunda01a972019-07-12 17:32:59 +0800383/*************************** IPC definitions **************************/
Edison Ai7aff9e82019-07-11 14:56:46 +0800384
385/**
386 * \brief Get bottom of stack region for a partition
387 *
388 * \param[in] partition_idx Partition index
389 *
390 * \return Stack region bottom value
391 *
392 * \note This function doesn't check if partition_idx is valid.
393 */
394uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx);
395
396/**
397 * \brief Get top of stack region for a partition
398 *
399 * \param[in] partition_idx Partition index
400 *
401 * \return Stack region top value
402 *
403 * \note This function doesn't check if partition_idx is valid.
404 */
405uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800406
407/**
408 * \brief Get the running partition ID.
409 *
410 * \return Returns the partition ID
411 */
412uint32_t tfm_spm_partition_get_running_partition_id(void);
413
414/**
415 * \brief Get the current partition mode.
416 *
Summer Qin75f0d752019-08-27 14:38:20 +0800417 * \param[in] partition_flags Flags of current partition
Mingyang Sunf3d29892019-07-10 17:50:23 +0800418 *
419 * \retval TFM_PARTITION_PRIVILEGED_MODE Privileged mode
420 * \retval TFM_PARTITION_UNPRIVILEGED_MODE Unprivileged mode
421 */
Summer Qin75f0d752019-08-27 14:38:20 +0800422uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800423
424/******************** Service handle management functions ********************/
425
426/**
427 * \brief Create connection handle for client connect
428 *
429 * \param[in] service Target service context pointer
430 *
431 * \retval PSA_NULL_HANDLE Create failed \ref PSA_NULL_HANDLE
432 * \retval >0 Service handle created, \ref psa_handle_t
433 */
434psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service);
435
436/**
437 * \brief Free connection handle which not used anymore.
438 *
439 * \param[in] service Target service context pointer
440 * \param[in] conn_handle Connection handle created by
441 * tfm_spm_create_conn_handle(), \ref psa_handle_t
442 *
443 * \retval IPC_SUCCESS Success
444 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
445 * \retval "Does not return" Panic for not find service by handle
446 */
447int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
448 psa_handle_t conn_handle);
449
450/**
451 * \brief Set reverse handle value for connection.
452 *
453 * \param[in] service Target service context pointer
454 * \param[in] conn_handle Connection handle created by
455 * tfm_spm_create_conn_handle(), \ref psa_handle_t
456 * \param[in] rhandle rhandle need to save
457 *
458 * \retval IPC_SUCCESS Success
459 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
460 * \retval "Does not return" Panic for not find handle node
461 */
462int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
463 psa_handle_t conn_handle,
464 void *rhandle);
465
466/**
467 * \brief Get reverse handle value from connection hanlde.
468 *
469 * \param[in] service Target service context pointer
470 * \param[in] conn_handle Connection handle created by
471 * tfm_spm_create_conn_handle(), \ref psa_handle_t
472 *
473 * \retval void * Success
474 * \retval "Does not return" Panic for those:
475 * service pointer are NULL
476 * hanlde is \ref PSA_NULL_HANDLE
477 * handle node does not be found
478 */
479void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
480 psa_handle_t conn_handle);
481
482/******************** Partition management functions *************************/
483
484/**
485 * \brief Get current running partition context.
486 *
487 * \retval NULL Failed
488 * \retval "Not NULL" Return the parttion context pointer
489 * \ref spm_partition_desc_t structures
490 */
491struct spm_partition_desc_t *tfm_spm_get_running_partition(void);
492
493/**
494 * \brief Get the service context by signal.
495 *
496 * \param[in] partition Partition context pointer
497 * \ref spm_partition_desc_t structures
498 * \param[in] signal Signal associated with inputs to the Secure
499 * Partition, \ref psa_signal_t
500 *
501 * \retval NULL Failed
502 * \retval "Not NULL" Target service context pointer,
503 * \ref tfm_spm_service_t structures
504 */
505struct tfm_spm_service_t *
506 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
507 psa_signal_t signal);
508
509/**
510 * \brief Get the service context by service ID.
511 *
512 * \param[in] sid RoT Service identity
513 *
514 * \retval NULL Failed
515 * \retval "Not NULL" Target service context pointer,
516 * \ref tfm_spm_service_t structures
517 */
518struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid);
519
520/**
521 * \brief Get the service context by connection handle.
522 *
523 * \param[in] conn_handle Connection handle created by
524 * tfm_spm_create_conn_handle()
525 *
526 * \retval NULL Failed
527 * \retval "Not NULL" Target service context pointer,
528 * \ref tfm_spm_service_t structures
529 */
530struct tfm_spm_service_t *
531 tfm_spm_get_service_by_handle(psa_handle_t conn_handle);
532
533/**
534 * \brief Get the partition context by partition ID.
535 *
536 * \param[in] partition_id Partition identity
537 *
538 * \retval NULL Failed
539 * \retval "Not NULL" Target partition context pointer,
540 * \ref spm_partition_desc_t structures
541 */
542struct spm_partition_desc_t *
543 tfm_spm_get_partition_by_id(int32_t partition_id);
544
545/************************ Message functions **********************************/
546
547/**
548 * \brief Get message context by message handle.
549 *
550 * \param[in] msg_handle Message handle which is a reference generated
551 * by the SPM to a specific message.
552 *
553 * \return The message body context pointer
554 * \ref tfm_msg_body_t structures
555 */
556struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle);
557
558/**
Edison Ai97115822019-08-01 14:22:19 +0800559 * \brief Get message context by connect handle.
Mingyang Sunf3d29892019-07-10 17:50:23 +0800560 *
Edison Ai97115822019-08-01 14:22:19 +0800561 * \param[in] conn_handle Service connect handle.
562 *
563 * \return The message body context pointer
564 * \ref msg_body_t structures
565 */
566struct tfm_msg_body_t *
567 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle);
568
569/**
570 * \brief Fill the message for PSA client call.
571 *
572 * \param[in] msg Service Message Queue buffer pointer
Mingyang Sunf3d29892019-07-10 17:50:23 +0800573 * \param[in] service Target service context pointer, which can be
574 * obtained by partition management functions
575 * \prarm[in] handle Connect handle return by psa_connect().
576 * \param[in] type Message type, PSA_IPC_CONNECT, PSA_IPC_CALL or
577 * PSA_IPC_DISCONNECT
578 * \param[in] ns_caller Whether from NS caller
579 * \param[in] invec Array of input \ref psa_invec structures
580 * \param[in] in_len Number of input \ref psa_invec structures
581 * \param[in] outvec Array of output \ref psa_outvec structures
582 * \param[in] out_len Number of output \ref psa_outvec structures
583 * \param[in] caller_outvec Array of caller output \ref psa_outvec structures
Mingyang Sunf3d29892019-07-10 17:50:23 +0800584 */
Edison Ai97115822019-08-01 14:22:19 +0800585void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
586 struct tfm_spm_service_t *service,
587 psa_handle_t handle,
588 int32_t type, int32_t ns_caller,
589 psa_invec *invec, size_t in_len,
590 psa_outvec *outvec, size_t out_len,
591 psa_outvec *caller_outvec);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800592
593/**
594 * \brief Send message and wake up the SP who is waiting on
595 * message queue, block the current thread and
596 * scheduler triggered
597 *
598 * \param[in] service Target service context pointer, which can be
599 * obtained by partition management functions
600 * \param[in] msg message created by tfm_spm_create_msg()
601 * \ref tfm_msg_body_t structures
602 *
603 * \retval IPC_SUCCESS Success
604 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
605 * \retval IPC_ERROR_GENERIC Failed to enqueue message to service message queue
606 */
607int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
608 struct tfm_msg_body_t *msg);
609
610/**
611 * \brief Check the client minor version according to
612 * version policy
613 *
614 * \param[in] service Target service context pointer, which can be get
615 * by partition management functions
616 * \param[in] minor_version Client support minor version
617 *
618 * \retval IPC_SUCCESS Success
619 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
620 * \retval IPC_ERROR_VERSION Check failed
621 */
622int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
623 uint32_t minor_version);
624
625/**
626 * \brief Check the memory reference is valid.
627 *
628 * \param[in] buffer Pointer of memory reference
629 * \param[in] len Length of memory reference in bytes
630 * \param[in] ns_caller From non-secure caller
631 * \param[in] access Type of access specified by the
632 * \ref tfm_memory_access_e
633 * \param[in] privileged Privileged mode or unprivileged mode:
634 * \ref TFM_PARTITION_UNPRIVILEGED_MODE
635 * \ref TFM_PARTITION_PRIVILEGED_MODE
636 *
637 * \retval IPC_SUCCESS Success
638 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
639 * \retval IPC_ERROR_MEMORY_CHECK Check failed
640 */
David Hucb05d972019-08-06 18:10:11 +0800641int32_t tfm_memory_check(const void *buffer, size_t len, int32_t ns_caller,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800642 enum tfm_memory_access_e access,
643 uint32_t privileged);
644
645/* This function should be called before schedule function */
646void tfm_spm_init(void);
647
648/*
649 * PendSV specified function.
650 *
651 * Parameters :
652 * ctxb - State context storage pointer
653 *
654 * Notes:
655 * This is a staging API. Scheduler should be called in SPM finally and
656 * this function will be obsoleted later.
657 */
658void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb);
659
660#endif /* ifdef(TFM_PSA_API) */
661
Miklos Balint386b8b52017-11-29 13:12:32 +0000662#endif /*__SPM_API_H__ */