blob: 1705de8a52b883c05f22a193b4670eaf3e8ebfa1 [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;
96 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
97 ++g_spm_partition_db.partition_count;
98
99 /* For the TF-M core environment itself */
100 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
101 return SPM_ERR_INVALID_CONFIG;
102 }
103 part_ptr = &(g_spm_partition_db.partitions[
104 g_spm_partition_db.partition_count]);
105 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
106 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
107 ++g_spm_partition_db.partition_count;
108
109 /* Add user-defined secure partitions */
110 #include "user_partition_defines.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000111
112 return SPM_ERR_OK;
113}
114
115#if TFM_LVL != 1
116REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
117REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
118REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
119REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
120REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
121REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
122
123enum spm_err_t tfm_spm_mpu_init(void)
124{
125 mpu_armv8m_clean(&dev_mpu_s);
126
127 struct mpu_armv8m_region_cfg_t region_cfg;
128
129 /* Veneer region */
130 region_cfg.region_nr = MPU_REGION_VENEERS;
131 region_cfg.region_base = CMSE_VENEER_REGION_START;
132 region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT;
133 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
134 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
135 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
136 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
137
138 /* TFM Core unprivileged code region */
139 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE;
140 region_cfg.region_base =
141 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
142 region_cfg.region_limit =
143 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
144 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
145 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
146 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
147 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
148
149 /* TFM Core unprivileged data region */
150 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA;
151 region_cfg.region_base =
152 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
153 region_cfg.region_limit =
154 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
155 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
156 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
157 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
158 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
159
160 /* TFM Core unprivileged non-secure data region */
161 region_cfg.region_nr = MPU_REGION_NS_DATA;
162 region_cfg.region_base = NS_DATA_START;
163 region_cfg.region_limit = NS_DATA_LIMIT;
164 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
165 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
166 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
167 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
168
169 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
170
171 return SPM_ERR_OK;
172}
173
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100174/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100175 * Set share region to which the partition needs access
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100176 */
177static enum spm_err_t tfm_spm_set_share_region(
178 enum tfm_buffer_share_region_e share)
Miklos Balint386b8b52017-11-29 13:12:32 +0000179{
180 enum spm_err_t res = SPM_ERR_INVALID_CONFIG;
181 uint32_t scratch_base =
182 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
183 uint32_t scratch_limit =
184 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
185
186 mpu_armv8m_disable(&dev_mpu_s);
187
188 if (share == TFM_BUFFER_SHARE_DISABLE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100189 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000190 } else {
191 struct mpu_armv8m_region_cfg_t region_cfg;
192
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100193 region_cfg.region_nr = PARTITION_REGION_SHARE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000194 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
195 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
196 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
197 switch (share) {
198 case TFM_BUFFER_SHARE_SCRATCH:
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100199 /* Use scratch area for SP-to-SP data sharing */
Miklos Balint386b8b52017-11-29 13:12:32 +0000200 region_cfg.region_base = scratch_base;
201 region_cfg.region_limit = scratch_limit;
202 res = SPM_ERR_OK;
203 break;
204 case TFM_BUFFER_SHARE_NS_CODE:
205 region_cfg.region_base = NS_CODE_START;
206 region_cfg.region_limit = NS_CODE_LIMIT;
207 /* Only allow read access to NS code region and keep
208 * exec.never attribute
209 */
210 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
211 res = SPM_ERR_OK;
212 break;
213 default:
214 res = SPM_ERR_INVALID_CONFIG;
215 break;
216 }
217 if (res == SPM_ERR_OK) {
218 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
219 }
220 }
221 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
222
223 return res;
224}
225#endif
226
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100227enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000228{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100229 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000230 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100231 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000232
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100233 /* Call the init function for each partition */
Miklos Balint386b8b52017-11-29 13:12:32 +0000234 /* FixMe: This implementation only fits level 1 isolation.
235 * On higher levels MPU (and PPC) configuration need to be in place to have
236 * proper isolation during init.
237 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100238 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
239 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100240 if (part->static_data.periph_start) {
241 ppc_configure_to_secure(part->static_data.periph_ppc_bank,
242 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000243 }
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100244 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100245 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100246 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100247 int32_t ret = part->static_data.partition_init();
Miklos Balint386b8b52017-11-29 13:12:32 +0000248
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100249 if (ret == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100250 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100251 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100252 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
Miklos Balint386b8b52017-11-29 13:12:32 +0000253 fail_cnt++;
254 }
255 }
256 }
257
258 if (fail_cnt == 0) {
259 return SPM_ERR_OK;
260 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100261 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000262 }
263}
264
265#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100266enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000267{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100268 /* This function takes a partition id and enables the
269 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000270 */
271
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100272 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000273 struct mpu_armv8m_region_cfg_t region_cfg;
274
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100275 if (!g_spm_partition_db.is_init) {
276 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000277 }
278
279 /*brute force id*/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100280 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000281
282 mpu_armv8m_disable(&dev_mpu_s);
283
284 /* Configure Regions */
285
286 /* RO region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100287 region_cfg.region_nr = PARTITION_REGION_RO;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100288 region_cfg.region_base = part->static_data.ro_start;
289 region_cfg.region_limit = part->static_data.ro_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000290 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
291 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
292 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
293
294 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
295
296 /* RW, ZI and stack as one region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100297 region_cfg.region_nr = PARTITION_REGION_RW_STACK;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100298 region_cfg.region_base = part->static_data.rw_start;
299 region_cfg.region_limit = part->static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000300 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
301 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
302 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
303
304 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
305
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100306 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000307 /* Peripheral */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100308 region_cfg.region_nr = PARTITION_REGION_PERIPH;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100309 region_cfg.region_base = part->static_data.periph_start;
310 region_cfg.region_limit = part->static_data.periph_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000311 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
312 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
313 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
314 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
315
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100316 ppc_en_secure_unpriv(part->static_data.periph_ppc_bank,
317 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000318 }
319
320 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
321
322#ifndef UNPRIV_JUMP_TO_NS
323 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
324 * this code can be removed
325 */
326 /* Initialization is done, set thread mode to unprivileged.
327 */
328 CONTROL_Type ctrl;
329
330 ctrl.w = __get_CONTROL();
331 ctrl.b.nPRIV = 1;
332 __set_CONTROL(ctrl.w);
333 __DSB();
334 __ISB();
335#endif
336
337 return SPM_ERR_OK;
338}
339
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100340enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000341{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100342 /* This function takes a partition id and disables the
343 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000344 */
345
346#ifndef UNPRIV_JUMP_TO_NS
347 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
348 * this code can be removed
349 */
350 CONTROL_Type ctrl;
351
352 ctrl.w = __get_CONTROL();
353 ctrl.b.nPRIV = 0;
354 __set_CONTROL(ctrl.w);
355 __DSB();
356 __ISB();
357#endif
358
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100359 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000360
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100361 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000362
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100363 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000364 /* Peripheral */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100365 ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
366 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000367 }
368
369 mpu_armv8m_disable(&dev_mpu_s);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100370 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO);
371 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK);
372 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH);
373 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000374 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
375
376 return SPM_ERR_OK;
377}
378
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100379uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000380{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100381 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100382 static_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000383}
384
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100385uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000386{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100387 return g_spm_partition_db.partitions[partition_idx].static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000388}
389
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100390void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000391{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100392 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100393 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000394}
395#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100396
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100397uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100398{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100399 return g_spm_partition_db.partitions[partition_idx].static_data.
400 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100401}
402
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100403const struct spm_partition_runtime_data_t *
404 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100405{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100406 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
407}
408
409void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
410{
411 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
412 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100413 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100414 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100415 }
416}
417
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100418void tfm_spm_partition_set_caller_partition_id(uint32_t partition_idx,
419 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100420{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100421 g_spm_partition_db.partitions[partition_idx].runtime_data.
422 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100423}
424
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100425void tfm_spm_partition_set_orig_psp(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100426 uint32_t orig_psp)
427{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100428 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp =
429 orig_psp;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100430}
431
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100432void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100433 uint32_t orig_psplim)
434{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100435 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim =
436 orig_psplim;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100437}
438
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100439void tfm_spm_partition_set_orig_lr(uint32_t partition_idx, uint32_t orig_lr)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100440{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100441 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_lr = orig_lr;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100442}
443
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100444enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100445 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100446{
447 enum spm_err_t ret = SPM_ERR_OK;
448
449#if TFM_LVL != 1
450 /* Only need to set configuration on levels higher than 1 */
451 ret = tfm_spm_set_share_region(share);
452#endif
453
454 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100455 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100456 }
457 return ret;
458}
459
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100460uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100461{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100462 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100463}
464
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100465void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100466{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100467 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100468 &(g_spm_partition_db.partitions[partition_idx]);
469 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100470 partition->runtime_data.orig_psp = 0;
471 partition->runtime_data.orig_psplim = 0;
472 partition->runtime_data.orig_lr = 0;
473 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100474}