blob: fd25dc345cd02b4bf802850b29b1e7bf87094e20 [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-Pale1475332018-04-09 17:28:49 +020013#include "platform/include/tfm_spm_hal.h"
14#include "spm_db_setup.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020015#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010016#include "tfm_api.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000017#include "mpu_armv8m_drv.h"
18#include "region_defs.h"
19#include "secure_fw/core/tfm_core.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020020#include "platform_retarget.h"
21#include "target_cfg.h"
22#include "spm_partition_defs.h"
23
Miklos Balint386b8b52017-11-29 13:12:32 +000024
Mate Toth-Pal349714a2018-02-23 15:30:24 +010025struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000026
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010027#define MPU_REGION_VENEERS 0
Miklos Balint386b8b52017-11-29 13:12:32 +000028#define MPU_REGION_TFM_UNPRIV_CODE 1
29#define MPU_REGION_TFM_UNPRIV_DATA 2
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010030#define MPU_REGION_NS_DATA 3
31#define PARTITION_REGION_RO 4
32#define PARTITION_REGION_RW_STACK 5
33#define PARTITION_REGION_PERIPH 6
34#define PARTITION_REGION_SHARE 7
Miklos Balint386b8b52017-11-29 13:12:32 +000035
36/* This should move to platform retarget */
37struct mpu_armv8m_dev_t dev_mpu_s = { MPU_BASE };
38
39typedef enum {
40 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010041} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000042
43/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010044 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010045 * In case of an error in the error handling, a non-zero value have to be
46 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000047 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010048static void tfm_spm_partition_err_handler(
Mate Toth-Pale1475332018-04-09 17:28:49 +020049 struct tfm_spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010050 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010051 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000052{
Miklos Balint386b8b52017-11-29 13:12:32 +000053#ifdef TFM_CORE_DEBUG
54 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010055 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010056 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000057 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010058 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010059 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000060 }
61#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010062 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010063 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000064}
65
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010066uint32_t get_partition_idx(uint32_t partition_id)
67{
68 int i;
69
70 if (partition_id == INVALID_PARTITION_ID) {
71 return SPM_INVALID_PARTITION_IDX;
72 }
73
74 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
75 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
76 partition_id) {
77 return i;
78 }
79 }
80 return SPM_INVALID_PARTITION_IDX;
81}
82
Miklos Balint386b8b52017-11-29 13:12:32 +000083enum spm_err_t tfm_spm_db_init(void)
84{
Mate Toth-Pale1475332018-04-09 17:28:49 +020085 struct tfm_spm_partition_desc_t *part_ptr;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010086
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010087 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
88
Mate Toth-Pal349714a2018-02-23 15:30:24 +010089 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010090 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
91 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000092
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010093 /* There are a few partitions that are used by TF-M internally.
94 * These are explicitly added to the partition db here.
95 */
96
97 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020098#if TFM_LVL != 1
99 extern uint32_t Stack_Mem[];
100 extern uint32_t Stack_top[];
101#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100102 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
103 return SPM_ERR_INVALID_CONFIG;
104 }
105 part_ptr = &(g_spm_partition_db.partitions[
106 g_spm_partition_db.partition_count]);
107 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100108 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200109
110#if TFM_LVL != 1
111 part_ptr->static_data.stack_bottom = (uint32_t)Stack_Mem;
112 part_ptr->static_data.stack_top = (uint32_t)Stack_top;
113 /* Since RW, ZI and stack are configured as one MPU region, configure
114 * RW start address to Stack_Mem to get RW access to stack
115 */
116 part_ptr->static_data.rw_start = (uint32_t)Stack_Mem;
117#endif
118
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100119 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
120 ++g_spm_partition_db.partition_count;
121
122 /* For the TF-M core environment itself */
123 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
124 return SPM_ERR_INVALID_CONFIG;
125 }
126 part_ptr = &(g_spm_partition_db.partitions[
127 g_spm_partition_db.partition_count]);
128 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100129 part_ptr->static_data.partition_flags =
130 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100131 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
132 ++g_spm_partition_db.partition_count;
133
134 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200135 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000136
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100137 g_spm_partition_db.is_init = 1;
138
Miklos Balint386b8b52017-11-29 13:12:32 +0000139 return SPM_ERR_OK;
140}
141
142#if TFM_LVL != 1
143REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
144REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
145REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
146REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
147REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
148REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
149
150enum spm_err_t tfm_spm_mpu_init(void)
151{
152 mpu_armv8m_clean(&dev_mpu_s);
153
154 struct mpu_armv8m_region_cfg_t region_cfg;
155
156 /* Veneer region */
157 region_cfg.region_nr = MPU_REGION_VENEERS;
158 region_cfg.region_base = CMSE_VENEER_REGION_START;
159 region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT;
160 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
161 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
162 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200163 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
164 return SPM_ERR_INVALID_CONFIG;
165 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000166
167 /* TFM Core unprivileged code region */
168 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE;
169 region_cfg.region_base =
170 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
171 region_cfg.region_limit =
172 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
173 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
174 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
175 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200176 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
177 return SPM_ERR_INVALID_CONFIG;
178 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000179
180 /* TFM Core unprivileged data region */
181 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA;
182 region_cfg.region_base =
183 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
184 region_cfg.region_limit =
185 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
186 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
187 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
188 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200189 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
190 return SPM_ERR_INVALID_CONFIG;
191 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000192
193 /* TFM Core unprivileged non-secure data region */
194 region_cfg.region_nr = MPU_REGION_NS_DATA;
195 region_cfg.region_base = NS_DATA_START;
196 region_cfg.region_limit = NS_DATA_LIMIT;
197 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;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200200 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
201 return SPM_ERR_INVALID_CONFIG;
202 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000203
204 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
205
206 return SPM_ERR_OK;
207}
208
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100209/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100210 * Set share region to which the partition needs access
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100211 */
212static enum spm_err_t tfm_spm_set_share_region(
213 enum tfm_buffer_share_region_e share)
Miklos Balint386b8b52017-11-29 13:12:32 +0000214{
215 enum spm_err_t res = SPM_ERR_INVALID_CONFIG;
216 uint32_t scratch_base =
217 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
218 uint32_t scratch_limit =
219 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
220
221 mpu_armv8m_disable(&dev_mpu_s);
222
223 if (share == TFM_BUFFER_SHARE_DISABLE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100224 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000225 } else {
226 struct mpu_armv8m_region_cfg_t region_cfg;
227
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100228 region_cfg.region_nr = PARTITION_REGION_SHARE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000229 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
230 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
231 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
232 switch (share) {
233 case TFM_BUFFER_SHARE_SCRATCH:
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100234 /* Use scratch area for SP-to-SP data sharing */
Miklos Balint386b8b52017-11-29 13:12:32 +0000235 region_cfg.region_base = scratch_base;
236 region_cfg.region_limit = scratch_limit;
237 res = SPM_ERR_OK;
238 break;
239 case TFM_BUFFER_SHARE_NS_CODE:
240 region_cfg.region_base = NS_CODE_START;
241 region_cfg.region_limit = NS_CODE_LIMIT;
242 /* Only allow read access to NS code region and keep
243 * exec.never attribute
244 */
245 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
246 res = SPM_ERR_OK;
247 break;
248 default:
249 res = SPM_ERR_INVALID_CONFIG;
250 break;
251 }
252 if (res == SPM_ERR_OK) {
253 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
254 }
255 }
256 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
257
258 return res;
259}
260#endif
261
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100262enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000263{
Mate Toth-Pale1475332018-04-09 17:28:49 +0200264 struct tfm_spm_partition_desc_t *part;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200265 struct tfm_sfn_req_s desc, *desc_ptr = &desc;
266 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000267 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100268 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000269
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100270 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100271 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
272 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pale1475332018-04-09 17:28:49 +0200273 if (part->platform_data.periph_start) {
274 ppc_configure_to_secure(part->platform_data.periph_ppc_bank,
275 part->platform_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000276 }
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100277 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100278 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200279 tfm_spm_partition_set_caller_partition_idx(idx,
280 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100281 } else {
Miklos Balint6a139ae2018-04-04 19:44:37 +0200282 int32_t ret;
283
284 desc.args = args;
285 desc.exc_num = EXC_NUM_THREAD_MODE;
286 desc.ns_caller = 0;
287 desc.sfn = (sfn_t)part->static_data.partition_init;
288 desc.sp_id = part->static_data.partition_id;
289 __ASM("MOV r0, %1\n"
290 "SVC %2\n"
291 "MOV %0, r0\n"
292 : "=r" (ret)
293 : "r" (desc_ptr), "I" (TFM_SVC_SFN_REQUEST)
294 : "r0");
Miklos Balint386b8b52017-11-29 13:12:32 +0000295
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100296 if (ret == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100297 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100298 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100299 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
Miklos Balint386b8b52017-11-29 13:12:32 +0000300 fail_cnt++;
301 }
302 }
303 }
304
Miklos Balint6a139ae2018-04-04 19:44:37 +0200305 tfm_secure_api_init_done();
306
Miklos Balint386b8b52017-11-29 13:12:32 +0000307 if (fail_cnt == 0) {
308 return SPM_ERR_OK;
309 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100310 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000311 }
312}
313
314#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100315enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000316{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100317 /* This function takes a partition id and enables the
318 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000319 */
320
Mate Toth-Pale1475332018-04-09 17:28:49 +0200321 struct tfm_spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000322 struct mpu_armv8m_region_cfg_t region_cfg;
323
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100324 if (!g_spm_partition_db.is_init) {
325 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000326 }
327
328 /*brute force id*/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100329 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000330
331 mpu_armv8m_disable(&dev_mpu_s);
332
333 /* Configure Regions */
334
Miklos Balint6a139ae2018-04-04 19:44:37 +0200335 if (part->static_data.ro_start) {
336 /* RO region*/
337 region_cfg.region_nr = PARTITION_REGION_RO;
338 region_cfg.region_base = part->static_data.ro_start;
339 region_cfg.region_limit = part->static_data.ro_limit;
340 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
341 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
342 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
Miklos Balint386b8b52017-11-29 13:12:32 +0000343
Miklos Balint6a139ae2018-04-04 19:44:37 +0200344 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg)
345 != MPU_ARMV8M_OK) {
346 return SPM_ERR_INVALID_CONFIG;
347 }
348 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000349
350 /* RW, ZI and stack as one region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100351 region_cfg.region_nr = PARTITION_REGION_RW_STACK;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100352 region_cfg.region_base = part->static_data.rw_start;
353 region_cfg.region_limit = part->static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000354 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
355 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
356 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
357
Miklos Balint6a139ae2018-04-04 19:44:37 +0200358 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
359 return SPM_ERR_INVALID_CONFIG;
360 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000361
Mate Toth-Pale1475332018-04-09 17:28:49 +0200362 if (part->platform_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000363 /* Peripheral */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100364 region_cfg.region_nr = PARTITION_REGION_PERIPH;
Mate Toth-Pale1475332018-04-09 17:28:49 +0200365 region_cfg.region_base = part->platform_data.periph_start;
366 region_cfg.region_limit = part->platform_data.periph_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000367 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
368 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
369 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200370 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg)
371 != MPU_ARMV8M_OK) {
372 return SPM_ERR_INVALID_CONFIG;
373 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000374
Mate Toth-Pale1475332018-04-09 17:28:49 +0200375 ppc_en_secure_unpriv(part->platform_data.periph_ppc_bank,
376 part->platform_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000377 }
378
379 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
380
Miklos Balint386b8b52017-11-29 13:12:32 +0000381 return SPM_ERR_OK;
382}
383
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100384enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000385{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100386 /* This function takes a partition id and disables the
387 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000388 */
389
Mate Toth-Pale1475332018-04-09 17:28:49 +0200390 struct tfm_spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000391
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100392 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000393
Mate Toth-Pale1475332018-04-09 17:28:49 +0200394 if (part->platform_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000395 /* Peripheral */
Mate Toth-Pale1475332018-04-09 17:28:49 +0200396 ppc_clr_secure_unpriv(part->platform_data.periph_ppc_bank,
397 part->platform_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000398 }
399
400 mpu_armv8m_disable(&dev_mpu_s);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100401 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO);
402 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK);
403 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH);
404 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000405 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
406
407 return SPM_ERR_OK;
408}
409
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100410uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000411{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100412 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100413 static_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000414}
415
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100416uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000417{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100418 return g_spm_partition_db.partitions[partition_idx].static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000419}
420
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100421void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000422{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100423 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100424 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000425}
426#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100427
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100428uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100429{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100430 return g_spm_partition_db.partitions[partition_idx].static_data.
431 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100432}
433
Mate Toth-Pal59398712018-02-28 17:06:40 +0100434uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
435{
436 return g_spm_partition_db.partitions[partition_idx].static_data.
437 partition_flags;
438}
439
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100440const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100441 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100442{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100443 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
444}
445
446void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
447{
448 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
449 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100450 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100451 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100452 }
453}
454
Miklos Balint6a139ae2018-04-04 19:44:37 +0200455void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
456 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100457{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100458 g_spm_partition_db.partitions[partition_idx].runtime_data.
459 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100460}
461
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100462void tfm_spm_partition_set_orig_psp(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100463 uint32_t orig_psp)
464{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100465 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp =
466 orig_psp;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100467}
468
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100469void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100470 uint32_t orig_psplim)
471{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100472 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim =
473 orig_psplim;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100474}
475
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100476void tfm_spm_partition_set_orig_lr(uint32_t partition_idx, uint32_t orig_lr)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100477{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100478 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_lr = orig_lr;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100479}
480
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100481enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100482 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100483{
484 enum spm_err_t ret = SPM_ERR_OK;
485
486#if TFM_LVL != 1
487 /* Only need to set configuration on levels higher than 1 */
488 ret = tfm_spm_set_share_region(share);
489#endif
490
491 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100492 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100493 }
494 return ret;
495}
496
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100497uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100498{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100499 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100500}
501
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100502void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100503{
Mate Toth-Pale1475332018-04-09 17:28:49 +0200504 struct tfm_spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100505 &(g_spm_partition_db.partitions[partition_idx]);
506 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100507 partition->runtime_data.orig_psp = 0;
508 partition->runtime_data.orig_psplim = 0;
509 partition->runtime_data.orig_lr = 0;
510 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100511}