blob: 642b577358c9b1d244fb11f5a8651397e6ebbe2c [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Mate Toth-Pal65291f32018-02-23 14:35:22 +01002 * Copyright (c) 2017-2018, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mate Toth-Pal349714a2018-02-23 15:30:24 +01008/* This file contains the APIs exported by the SPM to tfm core */
Miklos Balint386b8b52017-11-29 13:12:32 +00009
10#include <stdio.h>
Miklos Balint386b8b52017-11-29 13:12:32 +000011#include "spm_api.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010012#include "spm_db.h"
13#include "tfm_api.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000014#include "mpu_armv8m_drv.h"
15#include "region_defs.h"
16#include "secure_fw/core/tfm_core.h"
17
Mate Toth-Pal349714a2018-02-23 15:30:24 +010018struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000019
20#define MPU_REGION_VENEERS 0
21#define MPU_REGION_TFM_UNPRIV_CODE 1
22#define MPU_REGION_TFM_UNPRIV_DATA 2
23#define MPU_REGION_NS_DATA 3
Mate Toth-Pal349714a2018-02-23 15:30:24 +010024#define PARTITION_REGION_RO 4
25#define PARTITION_REGION_RW_STACK 5
26#define PARTITION_REGION_PERIPH 6
27#define PARTITION_REGION_SHARE 7
Miklos Balint386b8b52017-11-29 13:12:32 +000028
29/* This should move to platform retarget */
30struct mpu_armv8m_dev_t dev_mpu_s = { MPU_BASE };
31
32typedef enum {
33 TFM_INIT_FAILURE,
34} ss_error_type_t;
35
36/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010037 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010038 * In case of an error in the error handling, a non-zero value have to be
39 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000040 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010041static void tfm_spm_partition_err_handler(
Mate Toth-Pal18b83922018-02-26 17:58:18 +010042 struct spm_partition_desc_t *partition,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010043 ss_error_type_t err_type,
44 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000045{
Miklos Balint386b8b52017-11-29 13:12:32 +000046#ifdef TFM_CORE_DEBUG
47 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010048 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010049 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000050 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010051 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010052 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000053 }
54#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010055 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010056 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000057}
58
59enum spm_err_t tfm_spm_db_init(void)
60{
Mate Toth-Pal349714a2018-02-23 15:30:24 +010061 /* This function initialises partition db */
62 g_spm_partition_db.is_init = 1;
63 g_spm_partition_db.running_partition_id = INVALID_PARITION_ID;
Miklos Balint386b8b52017-11-29 13:12:32 +000064
Mate Toth-Pal349714a2018-02-23 15:30:24 +010065 g_spm_partition_db.partition_count =
66 create_user_partition_db(&g_spm_partition_db, SPM_MAX_PARTITIONS);
Miklos Balint386b8b52017-11-29 13:12:32 +000067
68 return SPM_ERR_OK;
69}
70
71#if TFM_LVL != 1
72REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
73REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
74REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
75REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
76REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
77REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
78
79enum spm_err_t tfm_spm_mpu_init(void)
80{
81 mpu_armv8m_clean(&dev_mpu_s);
82
83 struct mpu_armv8m_region_cfg_t region_cfg;
84
85 /* Veneer region */
86 region_cfg.region_nr = MPU_REGION_VENEERS;
87 region_cfg.region_base = CMSE_VENEER_REGION_START;
88 region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT;
89 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
90 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
91 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
92 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
93
94 /* TFM Core unprivileged code region */
95 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE;
96 region_cfg.region_base =
97 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
98 region_cfg.region_limit =
99 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
100 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
101 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
102 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
103 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
104
105 /* TFM Core unprivileged data region */
106 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA;
107 region_cfg.region_base =
108 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
109 region_cfg.region_limit =
110 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
111 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
112 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
113 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
114 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
115
116 /* TFM Core unprivileged non-secure data region */
117 region_cfg.region_nr = MPU_REGION_NS_DATA;
118 region_cfg.region_base = NS_DATA_START;
119 region_cfg.region_limit = NS_DATA_LIMIT;
120 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
121 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
122 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
123 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
124
125 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
126
127 return SPM_ERR_OK;
128}
129
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100130/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100131 * Set share region to which the partition needs access
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100132 */
133static enum spm_err_t tfm_spm_set_share_region(
134 enum tfm_buffer_share_region_e share)
Miklos Balint386b8b52017-11-29 13:12:32 +0000135{
136 enum spm_err_t res = SPM_ERR_INVALID_CONFIG;
137 uint32_t scratch_base =
138 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
139 uint32_t scratch_limit =
140 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
141
142 mpu_armv8m_disable(&dev_mpu_s);
143
144 if (share == TFM_BUFFER_SHARE_DISABLE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100145 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000146 } else {
147 struct mpu_armv8m_region_cfg_t region_cfg;
148
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100149 region_cfg.region_nr = PARTITION_REGION_SHARE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000150 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
151 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
152 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
153 switch (share) {
154 case TFM_BUFFER_SHARE_SCRATCH:
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100155 /* Use scratch area for SP-to-SP data sharing */
Miklos Balint386b8b52017-11-29 13:12:32 +0000156 region_cfg.region_base = scratch_base;
157 region_cfg.region_limit = scratch_limit;
158 res = SPM_ERR_OK;
159 break;
160 case TFM_BUFFER_SHARE_NS_CODE:
161 region_cfg.region_base = NS_CODE_START;
162 region_cfg.region_limit = NS_CODE_LIMIT;
163 /* Only allow read access to NS code region and keep
164 * exec.never attribute
165 */
166 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
167 res = SPM_ERR_OK;
168 break;
169 default:
170 res = SPM_ERR_INVALID_CONFIG;
171 break;
172 }
173 if (res == SPM_ERR_OK) {
174 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
175 }
176 }
177 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
178
179 return res;
180}
181#endif
182
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100183enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000184{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100185 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000186 int32_t fail_cnt = 0;
187 uint32_t i;
188
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100189 /* Call the init function for each partition */
Miklos Balint386b8b52017-11-29 13:12:32 +0000190 /* FixMe: This implementation only fits level 1 isolation.
191 * On higher levels MPU (and PPC) configuration need to be in place to have
192 * proper isolation during init.
193 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100194 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100195 part = &g_spm_partition_db.partitions[i];
196 if (part->static_data.periph_start) {
197 ppc_configure_to_secure(part->static_data.periph_ppc_bank,
198 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000199 }
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100200 if (part->static_data.partition_init == NULL) {
201 tfm_spm_partition_set_state(part->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100202 SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100203 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100204 int32_t ret = part->static_data.partition_init();
Miklos Balint386b8b52017-11-29 13:12:32 +0000205
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100206 if (ret == TFM_SUCCESS) {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100207 tfm_spm_partition_set_state(part->static_data.partition_id,
208 SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100209 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100210 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
Miklos Balint386b8b52017-11-29 13:12:32 +0000211 fail_cnt++;
212 }
213 }
214 }
215
216 if (fail_cnt == 0) {
217 return SPM_ERR_OK;
218 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100219 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000220 }
221}
222
223#if TFM_LVL != 1
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100224enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_id)
Miklos Balint386b8b52017-11-29 13:12:32 +0000225{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100226 /* This function takes a partition id and enables the
227 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000228 */
229
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100230 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000231 struct mpu_armv8m_region_cfg_t region_cfg;
232
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100233 if (!g_spm_partition_db.is_init) {
234 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000235 }
236
237 /*brute force id*/
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100238 part = &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
Miklos Balint386b8b52017-11-29 13:12:32 +0000239
240 mpu_armv8m_disable(&dev_mpu_s);
241
242 /* Configure Regions */
243
244 /* RO region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100245 region_cfg.region_nr = PARTITION_REGION_RO;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100246 region_cfg.region_base = part->static_data.ro_start;
247 region_cfg.region_limit = part->static_data.ro_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000248 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
249 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
250 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
251
252 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
253
254 /* RW, ZI and stack as one region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100255 region_cfg.region_nr = PARTITION_REGION_RW_STACK;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100256 region_cfg.region_base = part->static_data.rw_start;
257 region_cfg.region_limit = part->static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000258 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
259 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
260 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
261
262 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
263
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100264 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000265 /* Peripheral */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100266 region_cfg.region_nr = PARTITION_REGION_PERIPH;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100267 region_cfg.region_base = part->static_data.periph_start;
268 region_cfg.region_limit = part->static_data.periph_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000269 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
270 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
271 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
272 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
273
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100274 ppc_en_secure_unpriv(part->static_data.periph_ppc_bank,
275 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000276 }
277
278 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
279
280#ifndef UNPRIV_JUMP_TO_NS
281 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
282 * this code can be removed
283 */
284 /* Initialization is done, set thread mode to unprivileged.
285 */
286 CONTROL_Type ctrl;
287
288 ctrl.w = __get_CONTROL();
289 ctrl.b.nPRIV = 1;
290 __set_CONTROL(ctrl.w);
291 __DSB();
292 __ISB();
293#endif
294
295 return SPM_ERR_OK;
296}
297
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100298enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_id)
Miklos Balint386b8b52017-11-29 13:12:32 +0000299{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100300 /* This function takes a partition id and disables the
301 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000302 */
303
304#ifndef UNPRIV_JUMP_TO_NS
305 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
306 * this code can be removed
307 */
308 CONTROL_Type ctrl;
309
310 ctrl.w = __get_CONTROL();
311 ctrl.b.nPRIV = 0;
312 __set_CONTROL(ctrl.w);
313 __DSB();
314 __ISB();
315#endif
316
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100317 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000318
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100319 part = &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
Miklos Balint386b8b52017-11-29 13:12:32 +0000320
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100321 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000322 /* Peripheral */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100323 ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
324 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000325 }
326
327 mpu_armv8m_disable(&dev_mpu_s);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100328 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO);
329 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK);
330 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH);
331 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000332 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
333
334 return SPM_ERR_OK;
335}
336
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100337uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_id)
Miklos Balint386b8b52017-11-29 13:12:32 +0000338{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100339 return g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100340 static_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000341}
342
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100343uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_id)
Miklos Balint386b8b52017-11-29 13:12:32 +0000344{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100345 return
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100346 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
347 static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000348}
349
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100350void tfm_spm_partition_set_stack(uint32_t partition_id, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000351{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100352 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
353 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000354}
355#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100356
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100357
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100358const struct spm_partition_runtime_data_t *
359 tfm_spm_partition_get_runtime_data(uint32_t partition_id)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100360{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100361 return &(g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
362 runtime_data);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100363}
364
365void tfm_spm_partition_set_state(uint32_t partition_id, uint32_t state)
366{
367 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100368 runtime_data.partition_state = state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100369 if (state == SPM_PARTITION_STATE_RUNNING) {
370 g_spm_partition_db.running_partition_id = partition_id;
371 }
372}
373
374void tfm_spm_partition_set_caller_partition_id(uint32_t partition_id,
375 uint32_t caller_partition_id)
376{
377 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100378 runtime_data.caller_partition_id = caller_partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100379}
380
381void tfm_spm_partition_set_orig_psp(uint32_t partition_id,
382 uint32_t orig_psp)
383{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100384 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
385 runtime_data.orig_psp = orig_psp;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100386}
387
388void tfm_spm_partition_set_orig_psplim(uint32_t partition_id,
389 uint32_t orig_psplim)
390{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100391 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
392 runtime_data.orig_psplim = orig_psplim;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100393}
394
395void tfm_spm_partition_set_orig_lr(uint32_t partition_id, uint32_t orig_lr)
396{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100397 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
398 runtime_data.orig_lr = orig_lr;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100399}
400
401enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_id,
402 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100403{
404 enum spm_err_t ret = SPM_ERR_OK;
405
406#if TFM_LVL != 1
407 /* Only need to set configuration on levels higher than 1 */
408 ret = tfm_spm_set_share_region(share);
409#endif
410
411 if (ret == SPM_ERR_OK) {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100412 g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)].
413 runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100414 }
415 return ret;
416}
417
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100418uint32_t tfm_spm_partition_get_running_partition_id(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100419{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100420 return g_spm_partition_db.running_partition_id;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100421}
422
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100423void tfm_spm_partition_cleanup_context(uint32_t partition_id)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100424{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100425 struct spm_partition_desc_t *partition =
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100426 &g_spm_partition_db.partitions[PARTITION_ID_GET(partition_id)];
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100427 partition->runtime_data.caller_partition_id = 0;
428 partition->runtime_data.orig_psp = 0;
429 partition->runtime_data.orig_psplim = 0;
430 partition->runtime_data.orig_lr = 0;
431 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100432}