Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __SPM_SPM_DB_H__ |
| 9 | #define __SPM_SPM_DB_H__ |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | /* This limit is only used to define the size of the database reserved for |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 14 | * partitions. There's no requirement that it match the number of partitions |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 15 | * that get registered in a specific build |
| 16 | */ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 17 | #define SPM_MAX_PARTITIONS (6) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 18 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 19 | #define PARTITION_ID_GET(id) (id - TFM_SP_BASE) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 20 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 21 | typedef int32_t(*sp_init_function)(void); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 22 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 23 | struct spm_partition_static_data_t { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 24 | uint32_t partition_id; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 25 | #if TFM_LVL != 1 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 26 | uint32_t code_start; |
| 27 | uint32_t code_limit; |
| 28 | uint32_t ro_start; |
| 29 | uint32_t ro_limit; |
| 30 | uint32_t rw_start; |
| 31 | uint32_t rw_limit; |
| 32 | uint32_t zi_start; |
| 33 | uint32_t zi_limit; |
| 34 | uint32_t stack_bottom; |
| 35 | uint32_t stack_top; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 36 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 37 | uint32_t periph_start; |
| 38 | uint32_t periph_limit; |
| 39 | uint16_t periph_ppc_bank; |
| 40 | uint16_t periph_ppc_loc; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 41 | sp_init_function partition_init; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 42 | }; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 43 | |
| 44 | struct spm_partition_desc_t { |
| 45 | struct spm_partition_static_data_t static_data; |
| 46 | struct spm_partition_runtime_data_t runtime_data; |
| 47 | }; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 48 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 49 | struct spm_partition_db_t { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 50 | uint32_t is_init; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 51 | uint32_t partition_count; |
| 52 | uint32_t running_partition_id; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 53 | struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 56 | /* Macros to pick linker symbols and allow to form the partition data base */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 57 | #define REGION(a, b, c) a##b##c |
| 58 | #define REGION_NAME(a, b, c) REGION(a, b, c) |
| 59 | #if TFM_LVL == 1 |
| 60 | #define REGION_DECLARE(a, b, c) |
| 61 | #else |
| 62 | #define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c) |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 63 | #define PART_REGION_ADDR(partition, region) \ |
| 64 | (uint32_t)®ION_NAME(Image$$, partition, region) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 67 | #define PARTITION_DECLARE(partition) \ |
| 68 | REGION_DECLARE(Image$$, partition, $$Base); \ |
| 69 | REGION_DECLARE(Image$$, partition, $$Limit); \ |
| 70 | REGION_DECLARE(Image$$, partition, $$RO$$Base); \ |
| 71 | REGION_DECLARE(Image$$, partition, $$RO$$Limit); \ |
| 72 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base); \ |
| 73 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit); \ |
| 74 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base); \ |
| 75 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit); \ |
| 76 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base); \ |
| 77 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit); \ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | #if TFM_LVL == 1 |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 81 | #define PARTITION_INIT_STATIC_DATA(data, partition) \ |
| 82 | data.partition_id = partition##_ID; \ |
| 83 | data.periph_start = 0U; \ |
| 84 | data.periph_limit = 0U; \ |
| 85 | data.periph_ppc_bank = 0U; \ |
| 86 | data.periph_ppc_loc = 0U; \ |
| 87 | data.partition_init = 0U |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 88 | #else |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 89 | #define PARTITION_INIT_STATIC_DATA(data, partition) \ |
| 90 | data.partition_id = partition##_ID; \ |
| 91 | 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 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 108 | #if TFM_LVL == 1 |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 109 | #define DUMMY_PARTITION_INIT_STATIC_DATA(data, partition) \ |
| 110 | /* In case of TFM_LVL1 the static data is initialised the same way */ \ |
| 111 | PARTITION_INIT_STATIC_DATA(data, partition) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 112 | #else |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 113 | #define DUMMY_PARTITION_INIT_STATIC_DATA(data, partition) \ |
| 114 | data.partition_id = partition##_ID; \ |
| 115 | data.code_start = 0U; \ |
| 116 | data.code_limit = 0U; \ |
| 117 | data.ro_start = 0U; \ |
| 118 | data.ro_limit = 0U; \ |
| 119 | data.rw_start = 0U; \ |
| 120 | data.rw_limit = 0U; \ |
| 121 | data.zi_start = 0U; \ |
| 122 | data.zi_limit = 0U; \ |
| 123 | data.stack_bottom = 0U; \ |
| 124 | data.stack_top = 0U; \ |
| 125 | data.periph_start = 0U; \ |
| 126 | data.periph_limit = 0U; \ |
| 127 | data.periph_ppc_bank = 0U; \ |
| 128 | data.periph_ppc_loc = 0U; \ |
| 129 | data.partition_init = 0U |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 130 | #endif |
| 131 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 132 | |
| 133 | #if TFM_LVL == 1 |
| 134 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 135 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 136 | data.caller_partition_id = 0U; \ |
| 137 | data.orig_psp = 0U; \ |
| 138 | data.orig_psplim = 0U; \ |
| 139 | data.orig_lr = 0U; \ |
| 140 | data.share = 0U |
| 141 | #else |
| 142 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 143 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 144 | data.caller_partition_id = 0U; \ |
| 145 | data.orig_psp = 0U; \ |
| 146 | data.orig_psplim = 0U; \ |
| 147 | data.orig_lr = 0U; \ |
| 148 | data.share = 0U; \ |
| 149 | data.stack_ptr = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit) |
| 150 | #endif |
| 151 | |
| 152 | #if TFM_LVL == 1 |
| 153 | #define DUMMY_PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 154 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 155 | data.caller_partition_id = 0U; \ |
| 156 | data.orig_psp = 0U; \ |
| 157 | data.orig_psplim = 0U; \ |
| 158 | data.orig_lr = 0U; \ |
| 159 | data.share = 0U |
| 160 | #else |
| 161 | #define DUMMY_PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 162 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 163 | data.caller_partition_id = 0U; \ |
| 164 | data.orig_psp = 0U; \ |
| 165 | data.orig_psplim = 0U; \ |
| 166 | data.orig_lr = 0U; \ |
| 167 | data.share = 0U; \ |
| 168 | data.stack_ptr = 0U |
| 169 | #endif |
| 170 | |
| 171 | #define PARTITION_ADD(partition) { \ |
| 172 | if (index >= max_partitions) { \ |
| 173 | return max_partitions; \ |
| 174 | } \ |
| 175 | part_ptr = (struct spm_partition_desc_t *)&(db->partitions[index]); \ |
| 176 | PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition); \ |
| 177 | PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \ |
| 178 | index++; \ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 181 | #define DUMMY_PARTITION_ADD(partition) { \ |
| 182 | if (index >= max_partitions) { \ |
| 183 | return max_partitions; \ |
| 184 | } \ |
| 185 | part_ptr = (struct spm_partition_desc_t *)&(db->partitions[index]); \ |
| 186 | DUMMY_PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition); \ |
| 187 | /* The runtime data is the same for the dummy partitions*/ \ |
| 188 | DUMMY_PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \ |
| 189 | index++; \ |
| 190 | } |
| 191 | |
| 192 | #define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc) { \ |
| 193 | part_ptr = &(db->partitions[PARTITION_ID_GET(partition##_ID)]); \ |
| 194 | ((struct spm_partition_desc_t *)part_ptr)-> \ |
| 195 | static_data.periph_start = start; \ |
| 196 | ((struct spm_partition_desc_t *)part_ptr)-> \ |
| 197 | static_data.periph_limit = limit; \ |
| 198 | ((struct spm_partition_desc_t *)part_ptr)-> \ |
| 199 | static_data.periph_ppc_bank = bank; \ |
| 200 | ((struct spm_partition_desc_t *)part_ptr)-> \ |
| 201 | static_data.periph_ppc_loc = loc; \ |
| 202 | } |
| 203 | |
| 204 | #define PARTITION_ADD_INIT_FUNC(partition, init_func) { \ |
| 205 | extern int32_t init_func(void); \ |
| 206 | part_ptr = &(db->partitions[PARTITION_ID_GET(partition##_ID)]); \ |
| 207 | ((struct spm_partition_desc_t *)part_ptr)-> \ |
| 208 | static_data.partition_init = init_func; \ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /*This file is meant to be included twice*/ |
| 212 | #include "user_service_defines.inc" |
| 213 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 214 | struct spm_partition_db_t; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 215 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 216 | uint32_t create_user_partition_db(struct spm_partition_db_t *db, |
| 217 | uint32_t max_partitions) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 218 | { |
| 219 | uint32_t index = 0; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 220 | struct spm_partition_desc_t *part_ptr; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 221 | |
| 222 | #include "user_service_defines.inc" |
| 223 | |
| 224 | return index; |
| 225 | } |
| 226 | |
| 227 | #endif /* __SPM_SPM_DB_H__ */ |