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 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 8 | #ifndef __SPM_DB_H__ |
| 9 | #define __SPM_DB_H__ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 10 | |
| 11 | #include <stdint.h> |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 12 | #include "platform_retarget.h" |
| 13 | #include "target_cfg.h" |
| 14 | #include "spm_partition_defs.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 15 | |
| 16 | /* 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] | 17 | * partitions. There's no requirement that it match the number of partitions |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 18 | * that get registered in a specific build |
| 19 | */ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 20 | #define SPM_MAX_PARTITIONS (6) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 21 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 22 | struct spm_partition_desc_t; |
| 23 | struct spm_partition_db_t; |
| 24 | |
| 25 | uint32_t get_partition_idx(uint32_t partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 26 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 27 | typedef int32_t(*sp_init_function)(void); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 28 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 29 | struct spm_partition_static_data_t { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 30 | uint32_t partition_id; |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 31 | uint32_t partition_flags; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 32 | #if TFM_LVL != 1 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 33 | 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-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 43 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 44 | uint32_t periph_start; |
| 45 | uint32_t periph_limit; |
| 46 | uint16_t periph_ppc_bank; |
| 47 | uint16_t periph_ppc_loc; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 48 | sp_init_function partition_init; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 49 | }; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 50 | |
| 51 | struct spm_partition_desc_t { |
| 52 | struct spm_partition_static_data_t static_data; |
| 53 | struct spm_partition_runtime_data_t runtime_data; |
| 54 | }; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 55 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 56 | struct spm_partition_db_t { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 57 | uint32_t is_init; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 58 | uint32_t partition_count; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 59 | uint32_t running_partition_idx; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 60 | struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 63 | /* 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] | 64 | #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-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 70 | #define PART_REGION_ADDR(partition, region) \ |
| 71 | (uint32_t)®ION_NAME(Image$$, partition, region) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 74 | |
| 75 | #if TFM_LVL == 1 |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 76 | #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-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 85 | } while (0) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 86 | #else |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 87 | #define PARTITION_INIT_STATIC_DATA(data, partition, flags) \ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 88 | do { \ |
| 89 | data.partition_id = partition##_ID; \ |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 90 | data.partition_flags = flags; \ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 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; \ |
| 106 | } while (0) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 109 | #if TFM_LVL == 1 |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 110 | #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-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 119 | #else |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 120 | #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-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 131 | #endif |
| 132 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 133 | #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-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 154 | } while (0) |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 155 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 156 | #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-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 164 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 165 | #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-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 175 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 176 | #endif /* __SPM_DB_H__ */ |