blob: 40d054d57c94fd0cc48da26ba3c16dee026bc9f3 [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>
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010011#include <string.h>
Miklos Balint386b8b52017-11-29 13:12:32 +000012#include "spm_api.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010013#include "spm_db.h"
14#include "tfm_api.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000015#include "mpu_armv8m_drv.h"
16#include "region_defs.h"
17#include "secure_fw/core/tfm_core.h"
18
Mate Toth-Pal349714a2018-02-23 15:30:24 +010019struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000020
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010021#define MPU_REGION_VENEERS 0
Miklos Balint386b8b52017-11-29 13:12:32 +000022#define MPU_REGION_TFM_UNPRIV_CODE 1
23#define MPU_REGION_TFM_UNPRIV_DATA 2
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010024#define MPU_REGION_NS_DATA 3
25#define PARTITION_REGION_RO 4
26#define PARTITION_REGION_RW_STACK 5
27#define PARTITION_REGION_PERIPH 6
28#define PARTITION_REGION_SHARE 7
Miklos Balint386b8b52017-11-29 13:12:32 +000029
30/* This should move to platform retarget */
31struct mpu_armv8m_dev_t dev_mpu_s = { MPU_BASE };
32
33typedef enum {
34 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010035} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000036
37/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010038 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010039 * In case of an error in the error handling, a non-zero value have to be
40 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000041 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010042static void tfm_spm_partition_err_handler(
Mate Toth-Pal18b83922018-02-26 17:58:18 +010043 struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010044 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010045 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000046{
Miklos Balint386b8b52017-11-29 13:12:32 +000047#ifdef TFM_CORE_DEBUG
48 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010049 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010050 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000051 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010052 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010053 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000054 }
55#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010056 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010057 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000058}
59
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010060uint32_t get_partition_idx(uint32_t partition_id)
61{
62 int i;
63
64 if (partition_id == INVALID_PARTITION_ID) {
65 return SPM_INVALID_PARTITION_IDX;
66 }
67
68 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
69 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
70 partition_id) {
71 return i;
72 }
73 }
74 return SPM_INVALID_PARTITION_IDX;
75}
76
Miklos Balint386b8b52017-11-29 13:12:32 +000077enum spm_err_t tfm_spm_db_init(void)
78{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010079 struct spm_partition_desc_t *part_ptr;
80
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010081 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
82
Mate Toth-Pal349714a2018-02-23 15:30:24 +010083 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010084 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
85 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000086
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010087 /* There are a few partitions that are used by TF-M internally.
88 * These are explicitly added to the partition db here.
89 */
90
91 /* For the non secure Execution environment */
92 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
93 return SPM_ERR_INVALID_CONFIG;
94 }
95 part_ptr = &(g_spm_partition_db.partitions[
96 g_spm_partition_db.partition_count]);
97 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010098 part_ptr->static_data.partition_flags = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010099 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
100 ++g_spm_partition_db.partition_count;
101
102 /* For the TF-M core environment itself */
103 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
104 return SPM_ERR_INVALID_CONFIG;
105 }
106 part_ptr = &(g_spm_partition_db.partitions[
107 g_spm_partition_db.partition_count]);
108 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100109 part_ptr->static_data.partition_flags =
110 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100111 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
112 ++g_spm_partition_db.partition_count;
113
114 /* Add user-defined secure partitions */
115 #include "user_partition_defines.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000116
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100117 g_spm_partition_db.is_init = 1;
118
Miklos Balint386b8b52017-11-29 13:12:32 +0000119 return SPM_ERR_OK;
120}
121
122#if TFM_LVL != 1
123REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
124REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
125REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
126REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
127REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
128REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
129
130enum spm_err_t tfm_spm_mpu_init(void)
131{
132 mpu_armv8m_clean(&dev_mpu_s);
133
134 struct mpu_armv8m_region_cfg_t region_cfg;
135
136 /* Veneer region */
137 region_cfg.region_nr = MPU_REGION_VENEERS;
138 region_cfg.region_base = CMSE_VENEER_REGION_START;
139 region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT;
140 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
141 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
142 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
143 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
144
145 /* TFM Core unprivileged code region */
146 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE;
147 region_cfg.region_base =
148 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
149 region_cfg.region_limit =
150 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
151 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
152 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
153 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
154 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
155
156 /* TFM Core unprivileged data region */
157 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA;
158 region_cfg.region_base =
159 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
160 region_cfg.region_limit =
161 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
162 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
163 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
164 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
165 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
166
167 /* TFM Core unprivileged non-secure data region */
168 region_cfg.region_nr = MPU_REGION_NS_DATA;
169 region_cfg.region_base = NS_DATA_START;
170 region_cfg.region_limit = NS_DATA_LIMIT;
171 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
172 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
173 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
174 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
175
176 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
177
178 return SPM_ERR_OK;
179}
180
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100181/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100182 * Set share region to which the partition needs access
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100183 */
184static enum spm_err_t tfm_spm_set_share_region(
185 enum tfm_buffer_share_region_e share)
Miklos Balint386b8b52017-11-29 13:12:32 +0000186{
187 enum spm_err_t res = SPM_ERR_INVALID_CONFIG;
188 uint32_t scratch_base =
189 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
190 uint32_t scratch_limit =
191 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
192
193 mpu_armv8m_disable(&dev_mpu_s);
194
195 if (share == TFM_BUFFER_SHARE_DISABLE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100196 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000197 } else {
198 struct mpu_armv8m_region_cfg_t region_cfg;
199
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100200 region_cfg.region_nr = PARTITION_REGION_SHARE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000201 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
202 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
203 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
204 switch (share) {
205 case TFM_BUFFER_SHARE_SCRATCH:
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100206 /* Use scratch area for SP-to-SP data sharing */
Miklos Balint386b8b52017-11-29 13:12:32 +0000207 region_cfg.region_base = scratch_base;
208 region_cfg.region_limit = scratch_limit;
209 res = SPM_ERR_OK;
210 break;
211 case TFM_BUFFER_SHARE_NS_CODE:
212 region_cfg.region_base = NS_CODE_START;
213 region_cfg.region_limit = NS_CODE_LIMIT;
214 /* Only allow read access to NS code region and keep
215 * exec.never attribute
216 */
217 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
218 res = SPM_ERR_OK;
219 break;
220 default:
221 res = SPM_ERR_INVALID_CONFIG;
222 break;
223 }
224 if (res == SPM_ERR_OK) {
225 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
226 }
227 }
228 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
229
230 return res;
231}
232#endif
233
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100234enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000235{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100236 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000237 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100238 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000239
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100240 /* Call the init function for each partition */
Miklos Balint386b8b52017-11-29 13:12:32 +0000241 /* FixMe: This implementation only fits level 1 isolation.
242 * On higher levels MPU (and PPC) configuration need to be in place to have
243 * proper isolation during init.
244 */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100245 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
246 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100247 if (part->static_data.periph_start) {
248 ppc_configure_to_secure(part->static_data.periph_ppc_bank,
249 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000250 }
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100251 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100252 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100253 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100254 int32_t ret = part->static_data.partition_init();
Miklos Balint386b8b52017-11-29 13:12:32 +0000255
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100256 if (ret == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100257 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100258 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100259 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
Miklos Balint386b8b52017-11-29 13:12:32 +0000260 fail_cnt++;
261 }
262 }
263 }
264
265 if (fail_cnt == 0) {
266 return SPM_ERR_OK;
267 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100268 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000269 }
270}
271
272#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100273enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000274{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100275 /* This function takes a partition id and enables the
276 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000277 */
278
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100279 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000280 struct mpu_armv8m_region_cfg_t region_cfg;
281
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100282 if (!g_spm_partition_db.is_init) {
283 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000284 }
285
286 /*brute force id*/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100287 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000288
289 mpu_armv8m_disable(&dev_mpu_s);
290
291 /* Configure Regions */
292
293 /* RO region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100294 region_cfg.region_nr = PARTITION_REGION_RO;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100295 region_cfg.region_base = part->static_data.ro_start;
296 region_cfg.region_limit = part->static_data.ro_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000297 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
298 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
299 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
300
301 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
302
303 /* RW, ZI and stack as one region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100304 region_cfg.region_nr = PARTITION_REGION_RW_STACK;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100305 region_cfg.region_base = part->static_data.rw_start;
306 region_cfg.region_limit = part->static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000307 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
308 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
309 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
310
311 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
312
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100313 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000314 /* Peripheral */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100315 region_cfg.region_nr = PARTITION_REGION_PERIPH;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100316 region_cfg.region_base = part->static_data.periph_start;
317 region_cfg.region_limit = part->static_data.periph_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000318 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
319 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
320 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
321 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
322
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100323 ppc_en_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_enable(&dev_mpu_s, 1, 1);
328
329#ifndef UNPRIV_JUMP_TO_NS
330 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
331 * this code can be removed
332 */
333 /* Initialization is done, set thread mode to unprivileged.
334 */
335 CONTROL_Type ctrl;
336
337 ctrl.w = __get_CONTROL();
338 ctrl.b.nPRIV = 1;
339 __set_CONTROL(ctrl.w);
340 __DSB();
341 __ISB();
342#endif
343
344 return SPM_ERR_OK;
345}
346
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100347enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000348{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100349 /* This function takes a partition id and disables the
350 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000351 */
352
353#ifndef UNPRIV_JUMP_TO_NS
354 /* FixMe: if jump_to_ns_code() from unprivileged is solved,
355 * this code can be removed
356 */
357 CONTROL_Type ctrl;
358
359 ctrl.w = __get_CONTROL();
360 ctrl.b.nPRIV = 0;
361 __set_CONTROL(ctrl.w);
362 __DSB();
363 __ISB();
364#endif
365
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100366 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000367
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100368 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000369
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100370 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000371 /* Peripheral */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100372 ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
373 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000374 }
375
376 mpu_armv8m_disable(&dev_mpu_s);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100377 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO);
378 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK);
379 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH);
380 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000381 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
382
383 return SPM_ERR_OK;
384}
385
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100386uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000387{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100388 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100389 static_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000390}
391
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100392uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000393{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100394 return g_spm_partition_db.partitions[partition_idx].static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000395}
396
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100397void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000398{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100399 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100400 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000401}
402#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100403
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100404uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100405{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100406 return g_spm_partition_db.partitions[partition_idx].static_data.
407 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100408}
409
Mate Toth-Pal59398712018-02-28 17:06:40 +0100410uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
411{
412 return g_spm_partition_db.partitions[partition_idx].static_data.
413 partition_flags;
414}
415
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100416const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100417 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100418{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100419 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
420}
421
422void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
423{
424 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
425 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100426 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100427 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100428 }
429}
430
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100431void tfm_spm_partition_set_caller_partition_id(uint32_t partition_idx,
432 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100433{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100434 g_spm_partition_db.partitions[partition_idx].runtime_data.
435 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100436}
437
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100438void tfm_spm_partition_set_orig_psp(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100439 uint32_t orig_psp)
440{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100441 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp =
442 orig_psp;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100443}
444
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100445void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100446 uint32_t orig_psplim)
447{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100448 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim =
449 orig_psplim;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100450}
451
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100452void tfm_spm_partition_set_orig_lr(uint32_t partition_idx, uint32_t orig_lr)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100453{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100454 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_lr = orig_lr;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100455}
456
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100457enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100458 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100459{
460 enum spm_err_t ret = SPM_ERR_OK;
461
462#if TFM_LVL != 1
463 /* Only need to set configuration on levels higher than 1 */
464 ret = tfm_spm_set_share_region(share);
465#endif
466
467 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100468 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100469 }
470 return ret;
471}
472
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100473uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100474{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100475 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100476}
477
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100478void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100479{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100480 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100481 &(g_spm_partition_db.partitions[partition_idx]);
482 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100483 partition->runtime_data.orig_psp = 0;
484 partition->runtime_data.orig_psplim = 0;
485 partition->runtime_data.orig_lr = 0;
486 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100487}