blob: 67fc42c5af37831b6015b56d418e4324441f7c90 [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"
Miklos Balint6a139ae2018-04-04 19:44:37 +020014#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010015#include "tfm_api.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000016#include "mpu_armv8m_drv.h"
17#include "region_defs.h"
18#include "secure_fw/core/tfm_core.h"
19
Mate Toth-Pal349714a2018-02-23 15:30:24 +010020struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000021
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010022#define MPU_REGION_VENEERS 0
Miklos Balint386b8b52017-11-29 13:12:32 +000023#define MPU_REGION_TFM_UNPRIV_CODE 1
24#define MPU_REGION_TFM_UNPRIV_DATA 2
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010025#define MPU_REGION_NS_DATA 3
26#define PARTITION_REGION_RO 4
27#define PARTITION_REGION_RW_STACK 5
28#define PARTITION_REGION_PERIPH 6
29#define PARTITION_REGION_SHARE 7
Miklos Balint386b8b52017-11-29 13:12:32 +000030
31/* This should move to platform retarget */
32struct mpu_armv8m_dev_t dev_mpu_s = { MPU_BASE };
33
34typedef enum {
35 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010036} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000037
38/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010039 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010040 * In case of an error in the error handling, a non-zero value have to be
41 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000042 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010043static void tfm_spm_partition_err_handler(
Mate Toth-Pal18b83922018-02-26 17:58:18 +010044 struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010045 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010046 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000047{
Miklos Balint386b8b52017-11-29 13:12:32 +000048#ifdef TFM_CORE_DEBUG
49 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010050 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010051 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000052 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010053 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010054 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000055 }
56#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010057 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010058 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000059}
60
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010061uint32_t get_partition_idx(uint32_t partition_id)
62{
63 int i;
64
65 if (partition_id == INVALID_PARTITION_ID) {
66 return SPM_INVALID_PARTITION_IDX;
67 }
68
69 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
70 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
71 partition_id) {
72 return i;
73 }
74 }
75 return SPM_INVALID_PARTITION_IDX;
76}
77
Miklos Balint386b8b52017-11-29 13:12:32 +000078enum spm_err_t tfm_spm_db_init(void)
79{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010080 struct spm_partition_desc_t *part_ptr;
81
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010082 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
83
Mate Toth-Pal349714a2018-02-23 15:30:24 +010084 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010085 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
86 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000087
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010088 /* There are a few partitions that are used by TF-M internally.
89 * These are explicitly added to the partition db here.
90 */
91
92 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020093#if TFM_LVL != 1
94 extern uint32_t Stack_Mem[];
95 extern uint32_t Stack_top[];
96#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010097 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
98 return SPM_ERR_INVALID_CONFIG;
99 }
100 part_ptr = &(g_spm_partition_db.partitions[
101 g_spm_partition_db.partition_count]);
102 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100103 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200104
105#if TFM_LVL != 1
106 part_ptr->static_data.stack_bottom = (uint32_t)Stack_Mem;
107 part_ptr->static_data.stack_top = (uint32_t)Stack_top;
108 /* Since RW, ZI and stack are configured as one MPU region, configure
109 * RW start address to Stack_Mem to get RW access to stack
110 */
111 part_ptr->static_data.rw_start = (uint32_t)Stack_Mem;
112#endif
113
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100114 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
115 ++g_spm_partition_db.partition_count;
116
117 /* For the TF-M core environment itself */
118 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
119 return SPM_ERR_INVALID_CONFIG;
120 }
121 part_ptr = &(g_spm_partition_db.partitions[
122 g_spm_partition_db.partition_count]);
123 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100124 part_ptr->static_data.partition_flags =
125 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100126 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
127 ++g_spm_partition_db.partition_count;
128
129 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200130 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000131
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100132 g_spm_partition_db.is_init = 1;
133
Miklos Balint386b8b52017-11-29 13:12:32 +0000134 return SPM_ERR_OK;
135}
136
137#if TFM_LVL != 1
138REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
139REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
140REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
141REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
142REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
143REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
144
145enum spm_err_t tfm_spm_mpu_init(void)
146{
147 mpu_armv8m_clean(&dev_mpu_s);
148
149 struct mpu_armv8m_region_cfg_t region_cfg;
150
151 /* Veneer region */
152 region_cfg.region_nr = MPU_REGION_VENEERS;
153 region_cfg.region_base = CMSE_VENEER_REGION_START;
154 region_cfg.region_limit = CMSE_VENEER_REGION_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_OK;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200158 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
159 return SPM_ERR_INVALID_CONFIG;
160 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000161
162 /* TFM Core unprivileged code region */
163 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE;
164 region_cfg.region_base =
165 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
166 region_cfg.region_limit =
167 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
168 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
169 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
170 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200171 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
172 return SPM_ERR_INVALID_CONFIG;
173 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000174
175 /* TFM Core unprivileged data region */
176 region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA;
177 region_cfg.region_base =
178 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base);
179 region_cfg.region_limit =
180 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit);
181 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
182 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
183 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200184 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
185 return SPM_ERR_INVALID_CONFIG;
186 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000187
188 /* TFM Core unprivileged non-secure data region */
189 region_cfg.region_nr = MPU_REGION_NS_DATA;
190 region_cfg.region_base = NS_DATA_START;
191 region_cfg.region_limit = NS_DATA_LIMIT;
192 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
193 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
194 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200195 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
196 return SPM_ERR_INVALID_CONFIG;
197 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000198
199 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
200
201 return SPM_ERR_OK;
202}
203
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100204/**
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100205 * Set share region to which the partition needs access
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100206 */
207static enum spm_err_t tfm_spm_set_share_region(
208 enum tfm_buffer_share_region_e share)
Miklos Balint386b8b52017-11-29 13:12:32 +0000209{
210 enum spm_err_t res = SPM_ERR_INVALID_CONFIG;
211 uint32_t scratch_base =
212 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
213 uint32_t scratch_limit =
214 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit);
215
216 mpu_armv8m_disable(&dev_mpu_s);
217
218 if (share == TFM_BUFFER_SHARE_DISABLE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100219 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000220 } else {
221 struct mpu_armv8m_region_cfg_t region_cfg;
222
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100223 region_cfg.region_nr = PARTITION_REGION_SHARE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000224 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
225 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
226 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
227 switch (share) {
228 case TFM_BUFFER_SHARE_SCRATCH:
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100229 /* Use scratch area for SP-to-SP data sharing */
Miklos Balint386b8b52017-11-29 13:12:32 +0000230 region_cfg.region_base = scratch_base;
231 region_cfg.region_limit = scratch_limit;
232 res = SPM_ERR_OK;
233 break;
234 case TFM_BUFFER_SHARE_NS_CODE:
235 region_cfg.region_base = NS_CODE_START;
236 region_cfg.region_limit = NS_CODE_LIMIT;
237 /* Only allow read access to NS code region and keep
238 * exec.never attribute
239 */
240 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
241 res = SPM_ERR_OK;
242 break;
243 default:
244 res = SPM_ERR_INVALID_CONFIG;
245 break;
246 }
247 if (res == SPM_ERR_OK) {
248 mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg);
249 }
250 }
251 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
252
253 return res;
254}
255#endif
256
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100257enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000258{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100259 struct spm_partition_desc_t *part;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200260 struct tfm_sfn_req_s desc, *desc_ptr = &desc;
261 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000262 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100263 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000264
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100265 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100266 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
267 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100268 if (part->static_data.periph_start) {
269 ppc_configure_to_secure(part->static_data.periph_ppc_bank,
270 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000271 }
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100272 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100273 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200274 tfm_spm_partition_set_caller_partition_idx(idx,
275 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100276 } else {
Miklos Balint6a139ae2018-04-04 19:44:37 +0200277 int32_t ret;
278
279 desc.args = args;
280 desc.exc_num = EXC_NUM_THREAD_MODE;
281 desc.ns_caller = 0;
282 desc.sfn = (sfn_t)part->static_data.partition_init;
283 desc.sp_id = part->static_data.partition_id;
284 __ASM("MOV r0, %1\n"
285 "SVC %2\n"
286 "MOV %0, r0\n"
287 : "=r" (ret)
288 : "r" (desc_ptr), "I" (TFM_SVC_SFN_REQUEST)
289 : "r0");
Miklos Balint386b8b52017-11-29 13:12:32 +0000290
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100291 if (ret == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100292 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100293 } else {
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100294 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret);
Miklos Balint386b8b52017-11-29 13:12:32 +0000295 fail_cnt++;
296 }
297 }
298 }
299
Miklos Balint6a139ae2018-04-04 19:44:37 +0200300 tfm_secure_api_init_done();
301
Miklos Balint386b8b52017-11-29 13:12:32 +0000302 if (fail_cnt == 0) {
303 return SPM_ERR_OK;
304 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100305 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000306 }
307}
308
309#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100310enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000311{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100312 /* This function takes a partition id and enables the
313 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000314 */
315
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100316 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000317 struct mpu_armv8m_region_cfg_t region_cfg;
318
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100319 if (!g_spm_partition_db.is_init) {
320 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000321 }
322
323 /*brute force id*/
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100324 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000325
326 mpu_armv8m_disable(&dev_mpu_s);
327
328 /* Configure Regions */
329
Miklos Balint6a139ae2018-04-04 19:44:37 +0200330 if (part->static_data.ro_start) {
331 /* RO region*/
332 region_cfg.region_nr = PARTITION_REGION_RO;
333 region_cfg.region_base = part->static_data.ro_start;
334 region_cfg.region_limit = part->static_data.ro_limit;
335 region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV;
336 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
337 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK;
Miklos Balint386b8b52017-11-29 13:12:32 +0000338
Miklos Balint6a139ae2018-04-04 19:44:37 +0200339 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg)
340 != MPU_ARMV8M_OK) {
341 return SPM_ERR_INVALID_CONFIG;
342 }
343 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000344
345 /* RW, ZI and stack as one region*/
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100346 region_cfg.region_nr = PARTITION_REGION_RW_STACK;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100347 region_cfg.region_base = part->static_data.rw_start;
348 region_cfg.region_limit = part->static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000349 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
350 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
351 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
352
Miklos Balint6a139ae2018-04-04 19:44:37 +0200353 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg) != MPU_ARMV8M_OK) {
354 return SPM_ERR_INVALID_CONFIG;
355 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000356
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100357 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000358 /* Peripheral */
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100359 region_cfg.region_nr = PARTITION_REGION_PERIPH;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100360 region_cfg.region_base = part->static_data.periph_start;
361 region_cfg.region_limit = part->static_data.periph_limit;
Miklos Balint386b8b52017-11-29 13:12:32 +0000362 region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
363 region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
364 region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200365 if (mpu_armv8m_region_enable(&dev_mpu_s, &region_cfg)
366 != MPU_ARMV8M_OK) {
367 return SPM_ERR_INVALID_CONFIG;
368 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000369
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100370 ppc_en_secure_unpriv(part->static_data.periph_ppc_bank,
371 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000372 }
373
374 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
375
Miklos Balint386b8b52017-11-29 13:12:32 +0000376 return SPM_ERR_OK;
377}
378
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100379enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000380{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100381 /* This function takes a partition id and disables the
382 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000383 */
384
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100385 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000386
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100387 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000388
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100389 if (part->static_data.periph_start) {
Miklos Balint386b8b52017-11-29 13:12:32 +0000390 /* Peripheral */
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100391 ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
392 part->static_data.periph_ppc_loc);
Miklos Balint386b8b52017-11-29 13:12:32 +0000393 }
394
395 mpu_armv8m_disable(&dev_mpu_s);
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100396 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO);
397 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK);
398 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH);
399 mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE);
Miklos Balint386b8b52017-11-29 13:12:32 +0000400 mpu_armv8m_enable(&dev_mpu_s, 1, 1);
401
402 return SPM_ERR_OK;
403}
404
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100405uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000406{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100407 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100408 static_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000409}
410
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100411uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000412{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100413 return g_spm_partition_db.partitions[partition_idx].static_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000414}
415
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100416void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000417{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100418 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100419 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000420}
421#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100422
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100423uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100424{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100425 return g_spm_partition_db.partitions[partition_idx].static_data.
426 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100427}
428
Mate Toth-Pal59398712018-02-28 17:06:40 +0100429uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
430{
431 return g_spm_partition_db.partitions[partition_idx].static_data.
432 partition_flags;
433}
434
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100435const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100436 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100437{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100438 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
439}
440
441void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
442{
443 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
444 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100445 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100446 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100447 }
448}
449
Miklos Balint6a139ae2018-04-04 19:44:37 +0200450void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
451 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100452{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100453 g_spm_partition_db.partitions[partition_idx].runtime_data.
454 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100455}
456
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100457void tfm_spm_partition_set_orig_psp(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100458 uint32_t orig_psp)
459{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100460 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp =
461 orig_psp;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100462}
463
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100464void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100465 uint32_t orig_psplim)
466{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100467 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim =
468 orig_psplim;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100469}
470
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100471void tfm_spm_partition_set_orig_lr(uint32_t partition_idx, uint32_t orig_lr)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100472{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100473 g_spm_partition_db.partitions[partition_idx].runtime_data.orig_lr = orig_lr;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100474}
475
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100476enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100477 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100478{
479 enum spm_err_t ret = SPM_ERR_OK;
480
481#if TFM_LVL != 1
482 /* Only need to set configuration on levels higher than 1 */
483 ret = tfm_spm_set_share_region(share);
484#endif
485
486 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100487 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100488 }
489 return ret;
490}
491
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100492uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100493{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100494 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100495}
496
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100497void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100498{
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100499 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100500 &(g_spm_partition_db.partitions[partition_idx]);
501 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100502 partition->runtime_data.orig_psp = 0;
503 partition->runtime_data.orig_psplim = 0;
504 partition->runtime_data.orig_lr = 0;
505 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100506}