blob: 7451ee020286c0af836a962722a47fd26c10420a [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
Shawn Shancc39fcb2019-11-13 15:38:16 +080051#define TFM_HANDLE_STATUS_IDLE 0
52#define TFM_HANDLE_STATUS_ACTIVE 1
53
Edison Ai66fbdf12019-07-08 16:05:07 +080054#ifndef TFM_PSA_API
Miklos Balint386b8b52017-11-29 13:12:32 +000055/**
Mate Toth-Pal3db437a2018-06-22 16:15:13 +020056 * \brief Holds the iovec parameters that are passed to a service
57 *
58 * \note The size of the structure is (and have to be) multiple of 8 bytes
59 */
60struct iovec_args_t {
61 psa_invec in_vec[PSA_MAX_IOVEC]; /*!< Array of psa_invec objects */
62 size_t in_len; /*!< Number psa_invec objects in in_vec
63 */
64 psa_outvec out_vec[PSA_MAX_IOVEC]; /*!< Array of psa_outvec objects */
65 size_t out_len; /*!< Number psa_outvec objects in out_vec
66 */
67};
Mingyang Sunda01a972019-07-12 17:32:59 +080068
69/* The size of this struct must be multiple of 4 bytes as it is stacked to an
70 * uint32_t[] array
71 */
72struct interrupted_ctx_stack_frame_t {
Mingyang Sunda01a972019-07-12 17:32:59 +080073 uint32_t partition_state;
74};
75
76/* The size of this struct must be multiple of 4 bytes as it is stacked to an
77 * uint32_t[] array
78 */
79struct handler_ctx_stack_frame_t {
80 uint32_t partition_state;
81 uint32_t caller_partition_idx;
82};
Edison Ai66fbdf12019-07-08 16:05:07 +080083#endif /* !define(TFM_PSA_API) */
Mate Toth-Pal3db437a2018-06-22 16:15:13 +020084
85/**
Mate Toth-Pal18b83922018-02-26 17:58:18 +010086 * \brief Runtime context information of a partition
87 */
88struct spm_partition_runtime_data_t {
Edison Ai66fbdf12019-07-08 16:05:07 +080089#ifdef TFM_PSA_API
90 struct tfm_event_t signal_evnt; /* Event signal */
91 uint32_t signals; /* Service signals had been triggered*/
92 struct tfm_list_node_t service_list;/* Service list */
Summer Qinb5da9cc2019-08-26 15:19:45 +080093 struct tfm_thrd_ctx sp_thrd; /* Thread context */
Shawn Shan9b0e0c72019-10-22 13:43:07 +080094 uint32_t assigned_signals; /* All assigned signals */
Edison Ai66fbdf12019-07-08 16:05:07 +080095#else /* TFM_PSA_API */
Mate Toth-Pal18b83922018-02-26 17:58:18 +010096 uint32_t partition_state;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010097 uint32_t caller_partition_idx;
Mate Toth-Pal21a74c92018-04-13 14:05:41 +020098 int32_t caller_client_id;
Mate Toth-Pal18b83922018-02-26 17:58:18 +010099 uint32_t stack_ptr;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200100 uint32_t lr;
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200101 struct iovec_args_t iovec_args;
102 psa_outvec *orig_outvec;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200103 uint32_t *ctx_stack_ptr;
Edison Ai66fbdf12019-07-08 16:05:07 +0800104#endif /* TFM_PSA_API */
105 uint32_t signal_mask; /*
106 * Service signal mask passed by
107 * psa_wait()
108 */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100109};
110
Mingyang Sunda01a972019-07-12 17:32:59 +0800111#ifdef TFM_PSA_API
112
Mingyang Sunda01a972019-07-12 17:32:59 +0800113#define TFM_VERSION_POLICY_RELAXED 0
114#define TFM_VERSION_POLICY_STRICT 1
115
Edison Ai97115822019-08-01 14:22:19 +0800116#define TFM_CONN_HANDLE_MAX_NUM 16
Mingyang Sunda01a972019-07-12 17:32:59 +0800117
118/* RoT connection handle list */
119struct tfm_conn_handle_t {
Edison Ai9cc26242019-08-06 11:28:04 +0800120 void *rhandle; /* Reverse handle value */
Shawn Shancc39fcb2019-11-13 15:38:16 +0800121 uint32_t status; /*
122 * Status of handle, two valid options:
123 * TFM_HANDLE_STATUS_ACTIVE and
124 * TFM_HANDLE_STATUS_IDLE
125 */
Summer Qin1ce712a2019-10-14 18:04:05 +0800126 int32_t client_id; /*
127 * Partition ID of the sender of the
128 * message:
129 * - secure partition id;
130 * - non secure client endpoint id.
131 */
Edison Ai97115822019-08-01 14:22:19 +0800132 struct tfm_msg_body_t internal_msg; /* Internal message for message queue */
Edison Ai9cc26242019-08-06 11:28:04 +0800133 struct tfm_spm_service_t *service; /* RoT service pointer */
134 struct tfm_list_node_t list; /* list node */
Mingyang Sunda01a972019-07-12 17:32:59 +0800135};
136
137/* Service database defined by manifest */
138struct tfm_spm_service_db_t {
139 char *name; /* Service name */
140 uint32_t partition_id; /* Partition ID which service belong to */
141 psa_signal_t signal; /* Service signal */
142 uint32_t sid; /* Service identifier */
143 bool non_secure_client; /* If can be called by non secure client */
Shawn Shancc39fcb2019-11-13 15:38:16 +0800144 uint32_t version; /* Service version */
145 uint32_t version_policy; /* Service version policy */
Mingyang Sunda01a972019-07-12 17:32:59 +0800146};
147
148/* RoT Service data */
149struct tfm_spm_service_t {
Summer Qine578c5b2019-08-16 16:42:16 +0800150 const struct tfm_spm_service_db_t *service_db;/* Service database pointer */
Mingyang Sunda01a972019-07-12 17:32:59 +0800151 struct spm_partition_desc_t *partition; /*
152 * Point to secure partition
153 * data
154 */
155 struct tfm_list_node_t handle_list; /* Service handle list */
156 struct tfm_msg_queue_t msg_queue; /* Message queue */
157 struct tfm_list_node_t list; /* For list operation */
158};
159#endif /* ifdef(TFM_PSA_API) */
160
161/*********************** common definitions ***********************/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100162
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100163/**
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100164 * \brief Returns the index of the partition with the given partition ID.
Miklos Balint386b8b52017-11-29 13:12:32 +0000165 *
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100166 * \param[in] partition_id Partition id
Miklos Balint386b8b52017-11-29 13:12:32 +0000167 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100168 * \return the partition idx if partition_id is valid,
169 * \ref SPM_INVALID_PARTITION_IDX othervise
170 */
171uint32_t get_partition_idx(uint32_t partition_id);
172
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200173/**
Summer Qinb4a854d2019-05-29 15:31:22 +0800174 * \brief Get the id of the partition for its index from the db
175 *
176 * \param[in] partition_idx Partition index
177 *
178 * \return Partition ID for that partition
179 *
180 * \note This function doesn't check if partition_idx is valid.
181 */
182uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx);
183
184/**
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200185 * \brief Get the flags associated with a partition
186 *
187 * \param[in] partition_idx Partition index
188 *
189 * \return Flags associated with the partition
190 *
191 * \note This function doesn't check if partition_idx is valid.
192 */
193uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx);
194
Mingyang Sunda01a972019-07-12 17:32:59 +0800195/**
196 * \brief Initialize partition database
197 *
198 * \return Error code \ref spm_err_t
199 */
200enum spm_err_t tfm_spm_db_init(void);
201
202/**
203 * \brief Change the privilege mode for partition thread mode.
204 *
205 * \param[in] privileged Privileged mode,
206 * \ref TFM_PARTITION_PRIVILEGED_MODE
207 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
208 *
209 * \note Barrier instructions are not called by this function, and if
210 * it is called in thread mode, it might be necessary to call
Edison Ai7aff9e82019-07-11 14:56:46 +0800211 * them after this function returns.
Mingyang Sunda01a972019-07-12 17:32:59 +0800212 */
213void tfm_spm_partition_change_privilege(uint32_t privileged);
214
215/*********************** library definitions ***********************/
216
Summer Qinb4a854d2019-05-29 15:31:22 +0800217#ifndef TFM_PSA_API
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200218/**
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200219 * \brief Save interrupted partition context on ctx stack
220 *
221 * \param[in] partition_idx Partition index
222 *
223 * \note This function doesn't check if partition_idx is valid.
224 * \note This function doesn't whether the ctx stack overflows.
225 */
226void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx);
227
228/**
229 * \brief Restores interrupted partition context on ctx stack
230 *
231 * \param[in] partition_idx Partition index
232 *
233 * \note This function doesn't check if partition_idx is valid.
234 * \note This function doesn't whether the ctx stack underflows.
235 */
236void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx);
237
238/**
239 * \brief Save handler partition context on ctx stack
240 *
241 * \param[in] partition_idx Partition index
242 *
243 * \note This function doesn't check if partition_idx is valid.
244 * \note This function doesn't whether the ctx stack overflows.
245 */
246void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx);
247
248/**
249 * \brief Restores handler partition context on ctx stack
250 *
251 * \param[in] partition_idx Partition index
252 *
253 * \note This function doesn't check if partition_idx is valid.
254 * \note This function doesn't whether the ctx stack underflows.
255 */
256void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx);
257
258/**
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100259 * \brief Get the current runtime data of a partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100260 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100261 * \param[in] partition_idx Partition index
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100262 *
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100263 * \return The runtime data of the specified partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100264 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100265 * \note This function doesn't check if partition_idx is valid.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100266 */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100267const struct spm_partition_runtime_data_t *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100268 tfm_spm_partition_get_runtime_data(uint32_t partition_idx);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100269
270/**
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100271 * \brief Returns the index of the partition that has running state
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100272 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100273 * \return The index of the partition with the running state, if there is any
274 * set. 0 otherwise.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100275 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100276uint32_t tfm_spm_partition_get_running_partition_idx(void);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100277
278/**
Miklos Balintace4c3f2018-07-30 12:31:15 +0200279 * \brief Save stack pointer and link register for partition in database
280 *
281 * \param[in] partition_idx Partition index
282 * \param[in] stack_ptr Stack pointer to be stored
283 * \param[in] lr Link register to be stored
284 *
285 * \note This function doesn't check if partition_idx is valid.
286 */
287void tfm_spm_partition_store_context(uint32_t partition_idx,
288 uint32_t stack_ptr, uint32_t lr);
289
290/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100291 * \brief Set the current state of a partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100292 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100293 * \param[in] partition_idx Partition index
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100294 * \param[in] state The state to be set
295 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100296 * \note This function doesn't check if partition_idx is valid.
Gyorgy Szing40a7af02019-02-06 14:19:47 +0100297 * \note The state has to have the value set of \ref spm_part_state_t.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100298 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100299void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100300
301/**
Miklos Balint6a139ae2018-04-04 19:44:37 +0200302 * \brief Set the caller partition index for a given partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100303 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100304 * \param[in] partition_idx Partition index
Miklos Balint6a139ae2018-04-04 19:44:37 +0200305 * \param[in] caller_partition_idx The index of the caller partition
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100306 *
Miklos Balint6a139ae2018-04-04 19:44:37 +0200307 * \note This function doesn't check if any of the partition_idxs are valid.
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100308 */
Miklos Balint6a139ae2018-04-04 19:44:37 +0200309void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
310 uint32_t caller_partition_idx);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100311
312/**
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200313* \brief Set the caller client ID for a given partition
314*
315* \param[in] partition_idx Partition index
316* \param[in] caller_client_id The ID of the calling client
317*
318* \note This function doesn't check if any of the partition_idxs are valid.
319*/
320void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
321 int32_t caller_client_id);
322
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100323
324/**
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200325 * \brief Set the iovec parameters for the partition
326 *
327 * \param[in] partition_idx Partition index
328 * \param[in] args The arguments of the secure function
329 *
330 * args is expected to be of type int32_t[4] where:
331 * args[0] is in_vec
332 * args[1] is in_len
333 * args[2] is out_vec
334 * args[3] is out_len
335 *
Hugues de Valonf704c802019-02-19 14:51:41 +0000336 * \return Error code \ref spm_err_t
337 *
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200338 * \note This function doesn't check if partition_idx is valid.
339 * \note This function assumes that the iovecs that are passed in args are
340 * valid, and does no sanity check on them at all.
341 */
Hugues de Valonf704c802019-02-19 14:51:41 +0000342enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx,
343 const int32_t *args);
Mate Toth-Pal3db437a2018-06-22 16:15:13 +0200344
345/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100346 * \brief Execute partition init function
Miklos Balint386b8b52017-11-29 13:12:32 +0000347 *
348 * \return Error code \ref spm_err_t
349 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100350enum spm_err_t tfm_spm_partition_init(void);
Miklos Balint386b8b52017-11-29 13:12:32 +0000351
352/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100353 * \brief Clears the context info from the database for a partition.
Miklos Balint386b8b52017-11-29 13:12:32 +0000354 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100355 * \param[in] partition_idx Partition index
Miklos Balint386b8b52017-11-29 13:12:32 +0000356 *
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100357 * \note This function doesn't check if partition_idx is valid.
Miklos Balint386b8b52017-11-29 13:12:32 +0000358 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100359void tfm_spm_partition_cleanup_context(uint32_t partition_idx);
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200360
361/**
362 * \brief Set the signal mask for a given partition
363 *
364 * \param[in] partition_idx Partition index
365 * \param[in] signal_mask The signal mask to be set for the partition
366 *
367 * \note This function doesn't check if any of the partition_idxs are valid.
368 */
369void tfm_spm_partition_set_signal_mask(uint32_t partition_idx,
370 uint32_t signal_mask);
Summer Qinb4a854d2019-05-29 15:31:22 +0800371#endif /* !defined(TFM_PSA_API) */
372
Mingyang Sunf3d29892019-07-10 17:50:23 +0800373#ifdef TFM_PSA_API
Mingyang Sunda01a972019-07-12 17:32:59 +0800374/*************************** IPC definitions **************************/
Edison Ai7aff9e82019-07-11 14:56:46 +0800375
376/**
377 * \brief Get bottom of stack region for a partition
378 *
379 * \param[in] partition_idx Partition index
380 *
381 * \return Stack region bottom value
382 *
383 * \note This function doesn't check if partition_idx is valid.
384 */
385uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx);
386
387/**
388 * \brief Get top of stack region for a partition
389 *
390 * \param[in] partition_idx Partition index
391 *
392 * \return Stack region top value
393 *
394 * \note This function doesn't check if partition_idx is valid.
395 */
396uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800397
398/**
399 * \brief Get the running partition ID.
400 *
401 * \return Returns the partition ID
402 */
403uint32_t tfm_spm_partition_get_running_partition_id(void);
404
405/**
406 * \brief Get the current partition mode.
407 *
Summer Qin75f0d752019-08-27 14:38:20 +0800408 * \param[in] partition_flags Flags of current partition
Mingyang Sunf3d29892019-07-10 17:50:23 +0800409 *
410 * \retval TFM_PARTITION_PRIVILEGED_MODE Privileged mode
411 * \retval TFM_PARTITION_UNPRIVILEGED_MODE Unprivileged mode
412 */
Summer Qin75f0d752019-08-27 14:38:20 +0800413uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800414
415/******************** Service handle management functions ********************/
416
417/**
418 * \brief Create connection handle for client connect
419 *
420 * \param[in] service Target service context pointer
Summer Qin1ce712a2019-10-14 18:04:05 +0800421 * \param[in] client_id Partition ID of the sender of the message
Mingyang Sunf3d29892019-07-10 17:50:23 +0800422 *
423 * \retval PSA_NULL_HANDLE Create failed \ref PSA_NULL_HANDLE
424 * \retval >0 Service handle created, \ref psa_handle_t
425 */
Summer Qin1ce712a2019-10-14 18:04:05 +0800426psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service,
427 int32_t client_id);
428
429/**
430 * \brief Validate connection handle for client connect
431 *
432 * \param[in] conn_handle Handle to be validated
433 * \param[in] client_id Partition ID of the sender of the message
434 *
435 * \retval IPC_SUCCESS Success
436 * \retval IPC_ERROR_GENERIC Invalid handle
437 */
438int32_t tfm_spm_validate_conn_handle(psa_handle_t conn_handle,
439 int32_t client_id);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800440
441/**
442 * \brief Free connection handle which not used anymore.
443 *
444 * \param[in] service Target service context pointer
445 * \param[in] conn_handle Connection handle created by
446 * tfm_spm_create_conn_handle(), \ref psa_handle_t
447 *
448 * \retval IPC_SUCCESS Success
449 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
450 * \retval "Does not return" Panic for not find service by handle
451 */
452int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
453 psa_handle_t conn_handle);
454
455/**
456 * \brief Set reverse handle value for connection.
457 *
458 * \param[in] service Target service context pointer
459 * \param[in] conn_handle Connection handle created by
460 * tfm_spm_create_conn_handle(), \ref psa_handle_t
461 * \param[in] rhandle rhandle need to save
462 *
463 * \retval IPC_SUCCESS Success
464 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
465 * \retval "Does not return" Panic for not find handle node
466 */
467int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
468 psa_handle_t conn_handle,
469 void *rhandle);
470
471/**
472 * \brief Get reverse handle value from connection hanlde.
473 *
474 * \param[in] service Target service context pointer
475 * \param[in] conn_handle Connection handle created by
476 * tfm_spm_create_conn_handle(), \ref psa_handle_t
477 *
478 * \retval void * Success
479 * \retval "Does not return" Panic for those:
480 * service pointer are NULL
481 * hanlde is \ref PSA_NULL_HANDLE
482 * handle node does not be found
483 */
484void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
485 psa_handle_t conn_handle);
486
487/******************** Partition management functions *************************/
488
489/**
490 * \brief Get current running partition context.
491 *
492 * \retval NULL Failed
493 * \retval "Not NULL" Return the parttion context pointer
494 * \ref spm_partition_desc_t structures
495 */
496struct spm_partition_desc_t *tfm_spm_get_running_partition(void);
497
498/**
499 * \brief Get the service context by signal.
500 *
501 * \param[in] partition Partition context pointer
502 * \ref spm_partition_desc_t structures
503 * \param[in] signal Signal associated with inputs to the Secure
504 * Partition, \ref psa_signal_t
505 *
506 * \retval NULL Failed
507 * \retval "Not NULL" Target service context pointer,
508 * \ref tfm_spm_service_t structures
509 */
510struct tfm_spm_service_t *
511 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
512 psa_signal_t signal);
513
514/**
515 * \brief Get the service context by service ID.
516 *
517 * \param[in] sid RoT Service identity
518 *
519 * \retval NULL Failed
520 * \retval "Not NULL" Target service context pointer,
521 * \ref tfm_spm_service_t structures
522 */
523struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid);
524
525/**
526 * \brief Get the service context by connection handle.
527 *
528 * \param[in] conn_handle Connection handle created by
529 * tfm_spm_create_conn_handle()
530 *
531 * \retval NULL Failed
532 * \retval "Not NULL" Target service context pointer,
533 * \ref tfm_spm_service_t structures
534 */
535struct tfm_spm_service_t *
536 tfm_spm_get_service_by_handle(psa_handle_t conn_handle);
537
538/**
539 * \brief Get the partition context by partition ID.
540 *
541 * \param[in] partition_id Partition identity
542 *
543 * \retval NULL Failed
544 * \retval "Not NULL" Target partition context pointer,
545 * \ref spm_partition_desc_t structures
546 */
547struct spm_partition_desc_t *
548 tfm_spm_get_partition_by_id(int32_t partition_id);
549
550/************************ Message functions **********************************/
551
552/**
553 * \brief Get message context by message handle.
554 *
555 * \param[in] msg_handle Message handle which is a reference generated
556 * by the SPM to a specific message.
557 *
558 * \return The message body context pointer
559 * \ref tfm_msg_body_t structures
560 */
561struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle);
562
563/**
Edison Ai97115822019-08-01 14:22:19 +0800564 * \brief Get message context by connect handle.
Mingyang Sunf3d29892019-07-10 17:50:23 +0800565 *
Edison Ai97115822019-08-01 14:22:19 +0800566 * \param[in] conn_handle Service connect handle.
567 *
568 * \return The message body context pointer
569 * \ref msg_body_t structures
570 */
571struct tfm_msg_body_t *
572 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle);
573
574/**
575 * \brief Fill the message for PSA client call.
576 *
577 * \param[in] msg Service Message Queue buffer pointer
Mingyang Sunf3d29892019-07-10 17:50:23 +0800578 * \param[in] service Target service context pointer, which can be
579 * obtained by partition management functions
580 * \prarm[in] handle Connect handle return by psa_connect().
581 * \param[in] type Message type, PSA_IPC_CONNECT, PSA_IPC_CALL or
582 * PSA_IPC_DISCONNECT
Summer Qin1ce712a2019-10-14 18:04:05 +0800583 * \param[in] client_id Partition ID of the sender of the message
Mingyang Sunf3d29892019-07-10 17:50:23 +0800584 * \param[in] invec Array of input \ref psa_invec structures
585 * \param[in] in_len Number of input \ref psa_invec structures
586 * \param[in] outvec Array of output \ref psa_outvec structures
587 * \param[in] out_len Number of output \ref psa_outvec structures
588 * \param[in] caller_outvec Array of caller output \ref psa_outvec structures
Mingyang Sunf3d29892019-07-10 17:50:23 +0800589 */
Edison Ai97115822019-08-01 14:22:19 +0800590void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
591 struct tfm_spm_service_t *service,
592 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800593 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800594 psa_invec *invec, size_t in_len,
595 psa_outvec *outvec, size_t out_len,
596 psa_outvec *caller_outvec);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800597
598/**
599 * \brief Send message and wake up the SP who is waiting on
600 * message queue, block the current thread and
601 * scheduler triggered
602 *
603 * \param[in] service Target service context pointer, which can be
604 * obtained by partition management functions
605 * \param[in] msg message created by tfm_spm_create_msg()
606 * \ref tfm_msg_body_t structures
607 *
608 * \retval IPC_SUCCESS Success
609 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
610 * \retval IPC_ERROR_GENERIC Failed to enqueue message to service message queue
611 */
612int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
613 struct tfm_msg_body_t *msg);
614
615/**
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530616 * \brief Check the client version according to
Mingyang Sunf3d29892019-07-10 17:50:23 +0800617 * version policy
618 *
619 * \param[in] service Target service context pointer, which can be get
620 * by partition management functions
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530621 * \param[in] version Client support version
Mingyang Sunf3d29892019-07-10 17:50:23 +0800622 *
623 * \retval IPC_SUCCESS Success
624 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
625 * \retval IPC_ERROR_VERSION Check failed
626 */
627int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530628 uint32_t version);
Mingyang Sunf3d29892019-07-10 17:50:23 +0800629
630/**
Edison Aie728fbf2019-11-13 09:37:12 +0800631 * \brief Check the client access authorization
632 *
633 * \param[in] sid Target RoT Service identity
634 * \param[in] service Target service context pointer, which can be get
635 * by partition management functions
636 * \param[in] ns_caller Whether from NS caller
637 *
638 * \retval IPC_SUCCESS Success
639 * \retval IPC_ERROR_GENERIC Authorization check failed
640 */
641int32_t tfm_spm_check_authorization(uint32_t sid,
642 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800643 bool ns_caller);
Edison Aie728fbf2019-11-13 09:37:12 +0800644
645/**
Mingyang Sunf3d29892019-07-10 17:50:23 +0800646 * \brief Check the memory reference is valid.
647 *
648 * \param[in] buffer Pointer of memory reference
649 * \param[in] len Length of memory reference in bytes
650 * \param[in] ns_caller From non-secure caller
651 * \param[in] access Type of access specified by the
652 * \ref tfm_memory_access_e
653 * \param[in] privileged Privileged mode or unprivileged mode:
654 * \ref TFM_PARTITION_UNPRIVILEGED_MODE
655 * \ref TFM_PARTITION_PRIVILEGED_MODE
656 *
657 * \retval IPC_SUCCESS Success
658 * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input
659 * \retval IPC_ERROR_MEMORY_CHECK Check failed
660 */
Summer Qin43c185d2019-10-10 15:44:42 +0800661int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Mingyang Sunf3d29892019-07-10 17:50:23 +0800662 enum tfm_memory_access_e access,
663 uint32_t privileged);
664
665/* This function should be called before schedule function */
666void tfm_spm_init(void);
667
668/*
669 * PendSV specified function.
670 *
671 * Parameters :
672 * ctxb - State context storage pointer
673 *
674 * Notes:
675 * This is a staging API. Scheduler should be called in SPM finally and
676 * this function will be obsoleted later.
677 */
678void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb);
679
680#endif /* ifdef(TFM_PSA_API) */
681
Miklos Balint386b8b52017-11-29 13:12:32 +0000682#endif /*__SPM_API_H__ */