blob: 5641bf11c0c197a35b685b05bc0376224f81f4be [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-Pal52674ab2018-02-26 09:47:56 +01008#ifndef __SPM_DB_H__
9#define __SPM_DB_H__
Miklos Balint386b8b52017-11-29 13:12:32 +000010
11#include <stdint.h>
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010012#include "platform_retarget.h"
13#include "target_cfg.h"
14#include "spm_partition_defs.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000015
16/* This limit is only used to define the size of the database reserved for
Mate Toth-Pal349714a2018-02-23 15:30:24 +010017 * partitions. There's no requirement that it match the number of partitions
Miklos Balint386b8b52017-11-29 13:12:32 +000018 * that get registered in a specific build
19 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010020#define SPM_MAX_PARTITIONS (6)
Miklos Balint386b8b52017-11-29 13:12:32 +000021
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010022struct spm_partition_desc_t;
23struct spm_partition_db_t;
24
25uint32_t get_partition_idx(uint32_t partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000026
Mate Toth-Pal349714a2018-02-23 15:30:24 +010027typedef int32_t(*sp_init_function)(void);
Miklos Balint386b8b52017-11-29 13:12:32 +000028
Mate Toth-Pal18b83922018-02-26 17:58:18 +010029struct spm_partition_static_data_t {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010030 uint32_t partition_id;
Mate Toth-Pal59398712018-02-28 17:06:40 +010031 uint32_t partition_flags;
Mate Toth-Pal18b83922018-02-26 17:58:18 +010032#if TFM_LVL != 1
Miklos Balint386b8b52017-11-29 13:12:32 +000033 uint32_t code_start;
34 uint32_t code_limit;
35 uint32_t ro_start;
36 uint32_t ro_limit;
37 uint32_t rw_start;
38 uint32_t rw_limit;
39 uint32_t zi_start;
40 uint32_t zi_limit;
41 uint32_t stack_bottom;
42 uint32_t stack_top;
Mate Toth-Pal18b83922018-02-26 17:58:18 +010043#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000044 uint32_t periph_start;
45 uint32_t periph_limit;
46 uint16_t periph_ppc_bank;
47 uint16_t periph_ppc_loc;
Mate Toth-Pal349714a2018-02-23 15:30:24 +010048 sp_init_function partition_init;
Miklos Balint386b8b52017-11-29 13:12:32 +000049};
Mate Toth-Pal18b83922018-02-26 17:58:18 +010050
51struct spm_partition_desc_t {
52 struct spm_partition_static_data_t static_data;
53 struct spm_partition_runtime_data_t runtime_data;
54};
Miklos Balint386b8b52017-11-29 13:12:32 +000055
Mate Toth-Pal349714a2018-02-23 15:30:24 +010056struct spm_partition_db_t {
Miklos Balint386b8b52017-11-29 13:12:32 +000057 uint32_t is_init;
Mate Toth-Pal349714a2018-02-23 15:30:24 +010058 uint32_t partition_count;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010059 uint32_t running_partition_idx;
Mate Toth-Pal18b83922018-02-26 17:58:18 +010060 struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
Miklos Balint386b8b52017-11-29 13:12:32 +000061};
62
Mate Toth-Pal349714a2018-02-23 15:30:24 +010063/* Macros to pick linker symbols and allow to form the partition data base */
Miklos Balint386b8b52017-11-29 13:12:32 +000064#define REGION(a, b, c) a##b##c
65#define REGION_NAME(a, b, c) REGION(a, b, c)
66#if TFM_LVL == 1
67#define REGION_DECLARE(a, b, c)
68#else
69#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
Mate Toth-Pal18b83922018-02-26 17:58:18 +010070#define PART_REGION_ADDR(partition, region) \
71 (uint32_t)&REGION_NAME(Image$$, partition, region)
Miklos Balint386b8b52017-11-29 13:12:32 +000072#endif
73
Miklos Balint386b8b52017-11-29 13:12:32 +000074
75#if TFM_LVL == 1
Mate Toth-Pal59398712018-02-28 17:06:40 +010076#define PARTITION_INIT_STATIC_DATA(data, partition, flags) \
77 do { \
78 data.partition_id = partition##_ID; \
79 data.partition_flags = flags; \
80 data.periph_start = 0U; \
81 data.periph_limit = 0U; \
82 data.periph_ppc_bank = 0U; \
83 data.periph_ppc_loc = 0U; \
84 data.partition_init = 0U; \
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010085 } while (0)
Miklos Balint386b8b52017-11-29 13:12:32 +000086#else
Mate Toth-Pal59398712018-02-28 17:06:40 +010087#define PARTITION_INIT_STATIC_DATA(data, partition, flags) \
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010088 do { \
89 data.partition_id = partition##_ID; \
Mate Toth-Pal59398712018-02-28 17:06:40 +010090 data.partition_flags = flags; \
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010091 data.code_start = PART_REGION_ADDR(partition, $$Base); \
92 data.code_limit = PART_REGION_ADDR(partition, $$Limit); \
93 data.ro_start = PART_REGION_ADDR(partition, $$RO$$Base); \
94 data.ro_limit = PART_REGION_ADDR(partition, $$RO$$Limit); \
95 data.rw_start = PART_REGION_ADDR(partition, _DATA$$RW$$Base); \
96 data.rw_limit = PART_REGION_ADDR(partition, _DATA$$RW$$Limit); \
97 data.zi_start = PART_REGION_ADDR(partition, _DATA$$ZI$$Base); \
98 data.zi_limit = PART_REGION_ADDR(partition, _DATA$$ZI$$Limit); \
99 data.stack_bottom = PART_REGION_ADDR(partition, _STACK$$ZI$$Base); \
100 data.stack_top = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \
101 data.periph_start = 0U; \
102 data.periph_limit = 0U; \
103 data.periph_ppc_bank = 0U; \
104 data.periph_ppc_loc = 0U; \
105 data.partition_init = 0U; \
106 } while (0)
Miklos Balint386b8b52017-11-29 13:12:32 +0000107#endif
108
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100109#if TFM_LVL == 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100110#define PARTITION_INIT_RUNTIME_DATA(data, partition) \
111 do { \
112 data.partition_state = SPM_PARTITION_STATE_UNINIT; \
113 data.caller_partition_idx = 0U; \
114 data.orig_psp = 0U; \
115 data.orig_psplim = 0U; \
116 data.orig_lr = 0U; \
117 data.share = 0U; \
118 } while (0)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100119#else
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100120#define PARTITION_INIT_RUNTIME_DATA(data, partition) \
121 do { \
122 data.partition_state = SPM_PARTITION_STATE_UNINIT; \
123 data.caller_partition_idx = 0U; \
124 data.orig_psp = 0U; \
125 data.orig_psplim = 0U; \
126 data.orig_lr = 0U; \
127 data.share = 0U; \
128 data.stack_ptr = \
129 PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \
130 } while (0)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100131#endif
132
Mate Toth-Pal59398712018-02-28 17:06:40 +0100133#define PARTITION_DECLARE(partition, flags) \
134 do { \
135 REGION_DECLARE(Image$$, partition, $$Base); \
136 REGION_DECLARE(Image$$, partition, $$Limit); \
137 REGION_DECLARE(Image$$, partition, $$RO$$Base); \
138 REGION_DECLARE(Image$$, partition, $$RO$$Limit); \
139 REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base); \
140 REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit); \
141 REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base); \
142 REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit); \
143 REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base); \
144 REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit); \
145 struct spm_partition_desc_t *part_ptr; \
146 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { \
147 return SPM_ERR_INVALID_CONFIG; \
148 } \
149 part_ptr = &(g_spm_partition_db.partitions[ \
150 g_spm_partition_db.partition_count]); \
151 PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition, flags); \
152 PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \
153 ++g_spm_partition_db.partition_count; \
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100154 } while (0)
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100155
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100156#define PARTITION_ADD_INIT_FUNC(partition, init_func) \
157 do { \
158 extern int32_t init_func(void); \
159 uint32_t partition_idx = get_partition_idx(partition##_ID); \
160 struct spm_partition_desc_t *part_ptr = \
161 &(g_spm_partition_db.partitions[partition_idx]); \
162 part_ptr->static_data.partition_init = init_func; \
163 } while (0)
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100164
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100165#define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc) \
166 do { \
167 uint32_t partition_idx = get_partition_idx(partition##_ID); \
168 struct spm_partition_desc_t *part_ptr = \
169 &(g_spm_partition_db.partitions[partition_idx]); \
170 part_ptr->static_data.periph_start = start; \
171 part_ptr->static_data.periph_limit = limit; \
172 part_ptr->static_data.periph_ppc_bank = bank; \
173 part_ptr->static_data.periph_ppc_loc = loc; \
174 } while (0)
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100175
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100176#endif /* __SPM_DB_H__ */