blob: 06938fc48e21391245847e2fd819007f61d0b3f4 [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
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010020#define MPU_REGION_VENEERS 0
Miklos Balint386b8b52017-11-29 13:12:32 +000021#define MPU_REGION_TFM_UNPRIV_CODE 1
22#define MPU_REGION_TFM_UNPRIV_DATA 2
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010023#define MPU_REGION_NS_DATA 3
24#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,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010034} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000035
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-Pal52674ab2018-02-26 09:47:56 +010043 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010044 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
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010059uint32_t get_partition_idx(uint32_t partition_id)
60{
61 int i;
62
63 if (partition_id == INVALID_PARTITION_ID) {
64 return SPM_INVALID_PARTITION_IDX;
65 }
66
67 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
68 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
69 partition_id) {
70 return i;
71 }
72 }
73 return SPM_INVALID_PARTITION_IDX;
74}
75
Miklos Balint386b8b52017-11-29 13:12:32 +000076enum spm_err_t tfm_spm_db_init(void)
77{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010078 struct spm_partition_desc_t *part_ptr;
79
Mate Toth-Pal349714a2018-02-23 15:30:24 +010080 /* This function initialises partition db */
81 g_spm_partition_db.is_init = 1;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010082 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
83 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000084
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010085 /* There are a few partitions that are used by TF-M internally.
86 * These are explicitly added to the partition db here.
87 */
88
89 /* For the non secure Execution environment */
90 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
91 return SPM_ERR_INVALID_CONFIG;
92 }
93 part_ptr = &(g_spm_partition_db.partitions[
94 g_spm_partition_db.partition_count]);
95 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010096 part_ptr->static_data.partition_flags = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010097 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
98 ++g_spm_partition_db.partition_count;
99
100 /* For the TF-M core environment itself */
101 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
102 return SPM_ERR_INVALID_CONFIG;
103 }
104 part_ptr = &(g_spm_partition_db.partitions[
105 g_spm_partition_db.partition_count]);
106 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100107 part_ptr->static_data.partition_flags =
108 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100109 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
110 ++g_spm_partition_db.partition_count;
111
112 /* Add user-defined secure partitions */
113 #include "user_partition_defines.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000114
115 return SPM_ERR_OK;
116}
117
118#if TFM_LVL != 1
119REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
120REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
121REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
122REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
123REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
124REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
125
126enum spm_err_t tfm_spm_mpu_init(void)
127{
128 mpu_armv8m_clean(&dev_mpu_s);
129
130 struct mpu_armv8m_region_cfg_t region_cfg;
131
132 /* Veneer region */
133 region_cfg.region_nr = MPU_REGION_VENEERS;
134 region_cfg.region_base = CMSE_VENEER_REGION_START;
135 region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT;
136 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
137 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
138 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
139 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
140
141 /* TFM Core unprivileged code region */
142 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE;
143 region_cfg.region_base =
144 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
145 region_cfg.region_limit =
146 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
147 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
148 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
149 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
150 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
151
152 /* TFM Core unprivileged data region */
153 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA;
154 region_cfg.region_base =
155 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
156 region_cfg.region_limit =
157 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
158 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
159 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
160 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
161 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
162
163 /* TFM Core unprivileged non-secure data region */
164 region_cfg.region_nr = MPU_REGION_NS_DATA;
165 region_cfg.region_base = NS_DATA_START;
166 region_cfg.region_limit = NS_DATA_LIMIT;
167 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
168 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
169 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
170 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
171
172 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
173
174 return SPM_ERR_OK;
175}
176
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100177/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100178 * Set share region to which the partition needs access
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100179 */
180static enum spm_err_t tfm_spm_set_share_region(
181 enum tfm_buffer_share_region_e share)
Miklos Balint386b8b52017-11-29 13:12:32 +0000182{
183 enum spm_err_t res = SPM_ERR_INVALID_CONFIG;
184 uint32_t scratch_base =
185 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
186 uint32_t scratch_limit =
187 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
188
189 mpu_armv8m_disable(&dev_mpu_s);
190
191 if (share == TFM_BUFFER_SHARE_DISABLE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100192 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000193 } else {
194 struct mpu_armv8m_region_cfg_t region_cfg;
195
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100196 region_cfg.region_nr = PARTITION_REGION_SHARE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000197 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
198 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
199 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
200 switch (share) {
201 case TFM_BUFFER_SHARE_SCRATCH:
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100202 /* Use scratch area for SP-to-SP data sharing */
Miklos Balint386b8b52017-11-29 13:12:32 +0000203 region_cfg.region_base = scratch_base;
204 region_cfg.region_limit = scratch_limit;
205 res = SPM_ERR_OK;
206 break;
207 case TFM_BUFFER_SHARE_NS_CODE:
208 region_cfg.region_base = NS_CODE_START;
209 region_cfg.region_limit = NS_CODE_LIMIT;
210 /* Only allow read access to NS code region and keep
211 * exec.never attribute
212 */
213 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
214 res = SPM_ERR_OK;
215 break;
216 default:
217 res = SPM_ERR_INVALID_CONFIG;
218 break;
219 }
220 if (res == SPM_ERR_OK) {
221 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
222 }
223 }
224 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
225
226 return res;
227}
228#endif
229
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100230enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000231{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100232 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000233 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100234 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000235
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100236 /* Call the init function for each partition */
Miklos Balint386b8b52017-11-29 13:12:32 +0000237 /* FixMe: This implementation only fits level 1 isolation.
238 * On higher levels MPU (and PPC) configuration need to be in place to have
239 * proper isolation during init.
240 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100241 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
242 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100243 if (part->static_data.periph_start) {
244 ppc_configure_to_secure(part->static_data.periph_ppc_bank,
245 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000246 }
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100247 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100248 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100249 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100250 int32_t ret = part->static_data.partition_init();
Miklos Balint386b8b52017-11-29 13:12:32 +0000251
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100252 if (ret == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100253 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100254 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100255 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
Miklos Balint386b8b52017-11-29 13:12:32 +0000256 fail_cnt++;
257 }
258 }
259 }
260
261 if (fail_cnt == 0) {
262 return SPM_ERR_OK;
263 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100264 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000265 }
266}
267
268#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100269enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000270{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100271 /* This function takes a partition id and enables the
272 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000273 */
274
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100275 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000276 struct mpu_armv8m_region_cfg_t region_cfg;
277
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100278 if (!g_spm_partition_db.is_init) {
279 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000280 }
281
282 /*brute force id*/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100283 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000284
285 mpu_armv8m_disable(&dev_mpu_s);
286
287 /* Configure Regions */
288
289 /* RO region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100290 region_cfg.region_nr = PARTITION_REGION_RO;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100291 region_cfg.region_base = part->static_data.ro_start;
292 region_cfg.region_limit = part->static_data.ro_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000293 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
294 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
295 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
296
297 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
298
299 /* RW, ZI and stack as one region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100300 region_cfg.region_nr = PARTITION_REGION_RW_STACK;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100301 region_cfg.region_base = part->static_data.rw_start;
302 region_cfg.region_limit = part->static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000303 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
304 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
305 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
306
307 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
308
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100309 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000310 /* Peripheral */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100311 region_cfg.region_nr = PARTITION_REGION_PERIPH;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100312 region_cfg.region_base = part->static_data.periph_start;
313 region_cfg.region_limit = part->static_data.periph_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000314 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
315 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
316 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
317 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
318
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100319 ppc_en_secure_unpriv(part->static_data.periph_ppc_bank,
320 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000321 }
322
323 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
324
325#ifndef UNPRIV_JUMP_TO_NS
326 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
327 * this code can be removed
328 */
329 /* Initialization is done, set thread mode to unprivileged.
330 */
331 CONTROL_Type ctrl;
332
333 ctrl.w = __get_CONTROL();
334 ctrl.b.nPRIV = 1;
335 __set_CONTROL(ctrl.w);
336 __DSB();
337 __ISB();
338#endif
339
340 return SPM_ERR_OK;
341}
342
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100343enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000344{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100345 /* This function takes a partition id and disables the
346 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000347 */
348
349#ifndef UNPRIV_JUMP_TO_NS
350 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
351 * this code can be removed
352 */
353 CONTROL_Type ctrl;
354
355 ctrl.w = __get_CONTROL();
356 ctrl.b.nPRIV = 0;
357 __set_CONTROL(ctrl.w);
358 __DSB();
359 __ISB();
360#endif
361
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100362 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000363
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100364 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000365
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100366 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000367 /* Peripheral */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100368 ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
369 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000370 }
371
372 mpu_armv8m_disable(&dev_mpu_s);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100373 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO);
374 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK);
375 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH);
376 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000377 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
378
379 return SPM_ERR_OK;
380}
381
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100382uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000383{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100384 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100385 static_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000386}
387
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100388uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000389{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100390 return g_spm_partition_db.partitions[partition_idx].static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000391}
392
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100393void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000394{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100395 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100396 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000397}
398#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100399
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100400uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100401{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100402 return g_spm_partition_db.partitions[partition_idx].static_data.
403 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100404}
405
Mate Toth-Pal59398712018-02-28 17:06:40 +0100406uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
407{
408 return g_spm_partition_db.partitions[partition_idx].static_data.
409 partition_flags;
410}
411
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100412const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100413 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100414{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100415 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
416}
417
418void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
419{
420 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
421 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100422 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100423 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100424 }
425}
426
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100427void tfm_spm_partition_set_caller_partition_id(uint32_t partition_idx,
428 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100429{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100430 g_spm_partition_db.partitions[partition_idx].runtime_data.
431 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100432}
433
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100434void tfm_spm_partition_set_orig_psp(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100435 uint32_t orig_psp)
436{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100437 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp =
438 orig_psp;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100439}
440
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100441void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100442 uint32_t orig_psplim)
443{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100444 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim =
445 orig_psplim;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100446}
447
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100448void tfm_spm_partition_set_orig_lr(uint32_t partition_idx, uint32_t orig_lr)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100449{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100450 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_lr = orig_lr;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100451}
452
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100453enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100454 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100455{
456 enum spm_err_t ret = SPM_ERR_OK;
457
458#if TFM_LVL != 1
459 /* Only need to set configuration on levels higher than 1 */
460 ret = tfm_spm_set_share_region(share);
461#endif
462
463 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100464 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100465 }
466 return ret;
467}
468
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100469uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100470{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100471 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100472}
473
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100474void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100475{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100476 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100477 &(g_spm_partition_db.partitions[partition_idx]);
478 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100479 partition->runtime_data.orig_psp = 0;
480 partition->runtime_data.orig_psplim = 0;
481 partition->runtime_data.orig_lr = 0;
482 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100483}