blob: 46d2ca16b39a156356c54a0a20546c8127ccab63 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Tamas Ban8bd24b72019-02-19 12:13:13 +00002 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mingyang Sunda01a972019-07-12 17:32:59 +08008/* All the APIs defined in this file are common for library and IPC model. */
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"
Mingyang Sunf3d29892019-07-10 17:50:23 +080013#include "tfm_spm_hal.h"
Tamas Ban8bd24b72019-02-19 12:13:13 +000014#include "tfm_memory_utils.h"
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020015#include "spm_db.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020016#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010017#include "tfm_api.h"
Mate Toth-Palce61afa2018-08-03 13:51:01 +020018#include "tfm_nspm.h"
David Hu49a28eb2019-08-14 18:18:15 +080019#include "tfm_core.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020020#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020021#include "spm_partition_defs.h"
22
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020023#define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0
24#define TFM_CORE_INTERNAL_PARTITION_DB_IDX 1
Miklos Balint386b8b52017-11-29 13:12:32 +000025
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020026/* Define SPM DB structure */
27#include "secure_fw/services/tfm_spm_db.inc"
28
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010029uint32_t get_partition_idx(uint32_t partition_id)
30{
Hugues de Valonf704c802019-02-19 14:51:41 +000031 uint32_t i;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010032
33 if (partition_id == INVALID_PARTITION_ID) {
34 return SPM_INVALID_PARTITION_IDX;
35 }
36
37 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
38 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
39 partition_id) {
40 return i;
41 }
42 }
43 return SPM_INVALID_PARTITION_IDX;
44}
45
Miklos Balint386b8b52017-11-29 13:12:32 +000046enum spm_err_t tfm_spm_db_init(void)
47{
Edison Ai66fbdf12019-07-08 16:05:07 +080048 struct spm_partition_desc_t *part_ptr;
49#ifndef TFM_PSA_API
Mate Toth-Pal4341de02018-10-02 12:55:47 +020050 static uint32_t ns_interrupt_ctx_stack[
51 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
52 static uint32_t tfm_core_interrupt_ctx_stack[
53 sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
Edison Ai66fbdf12019-07-08 16:05:07 +080054#endif /* !defined(TFM_PSA_API) */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010055
Mate Toth-Pal349714a2018-02-23 15:30:24 +010056 /* This function initialises partition db */
Miklos Balint386b8b52017-11-29 13:12:32 +000057
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010058 /* There are a few partitions that are used by TF-M internally.
59 * These are explicitly added to the partition db here.
60 */
61
62 /* For the non secure Execution environment */
Edison Ai7aff9e82019-07-11 14:56:46 +080063#if TFM_PSA_API
Tamas Ban56ef3022018-09-13 23:49:16 +010064 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
65 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
66 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
67 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balintdd02bb32019-05-26 21:13:12 +020068#endif
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020069
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010070 part_ptr = &(g_spm_partition_db.partitions[
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +020071 NON_SECURE_INTERNAL_PARTITION_DB_IDX]);
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010072 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Hugues de Valon99578562019-06-18 16:08:51 +010073#ifdef TFM_PSA_API
Edison Ai4dcae6f2019-03-18 10:13:47 +080074 part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
75 SPM_PART_FLAG_IPC;
76 part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
77 part_ptr->static_data.partition_init = tfm_nspm_thread_entry;
78#else
Mate Toth-Pal59398712018-02-28 17:06:40 +010079 part_ptr->static_data.partition_flags = 0;
Edison Ai4dcae6f2019-03-18 10:13:47 +080080#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +020081
Edison Ai7aff9e82019-07-11 14:56:46 +080082#if TFM_PSA_API
Tamas Ban56ef3022018-09-13 23:49:16 +010083 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
84 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +020085 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +010086 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +020087 */
Tamas Ban56ef3022018-09-13 23:49:16 +010088 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balintdd02bb32019-05-26 21:13:12 +020089#endif
Miklos Balint6a139ae2018-04-04 19:44:37 +020090
Edison Ai66fbdf12019-07-08 16:05:07 +080091#ifndef TFM_PSA_API
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010092 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +020093 part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
Edison Ai66fbdf12019-07-08 16:05:07 +080094#endif /* !defined(TFM_PSA_API) */
95
Miklos Balint12735bc2018-08-01 15:45:18 +020096 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010097
Mingyang Sunda01a972019-07-12 17:32:59 +080098#ifndef TFM_PSA_API
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010099 /* For the TF-M core environment itself */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100100 part_ptr = &(g_spm_partition_db.partitions[
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200101 TFM_CORE_INTERNAL_PARTITION_DB_IDX]);
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100102 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100103 part_ptr->static_data.partition_flags =
Edison Aibb614aa2018-11-21 15:15:00 +0800104 SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100105 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200106 part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
Edison Ai66fbdf12019-07-08 16:05:07 +0800107#endif /* !defined(TFM_PSA_API) */
Miklos Balint386b8b52017-11-29 13:12:32 +0000108
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100109 g_spm_partition_db.is_init = 1;
110
Miklos Balint386b8b52017-11-29 13:12:32 +0000111 return SPM_ERR_OK;
112}
113
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100114uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100115{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100116 return g_spm_partition_db.partitions[partition_idx].static_data.
117 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100118}
119
Mate Toth-Pal59398712018-02-28 17:06:40 +0100120uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
121{
122 return g_spm_partition_db.partitions[partition_idx].static_data.
123 partition_flags;
124}
125
Edison Aib5571352019-03-22 10:49:52 +0800126__attribute__((section("SFN")))
127void tfm_spm_partition_change_privilege(uint32_t privileged)
128{
129 CONTROL_Type ctrl;
130
131 ctrl.w = __get_CONTROL();
132
133 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
134 ctrl.b.nPRIV = 0;
135 } else {
136 ctrl.b.nPRIV = 1;
137 }
138
139 __set_CONTROL(ctrl.w);
140}