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 | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 31 | #if TFM_LVL != 1 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 32 | uint32_t code_start; |
| 33 | uint32_t code_limit; |
| 34 | uint32_t ro_start; |
| 35 | uint32_t ro_limit; |
| 36 | uint32_t rw_start; |
| 37 | uint32_t rw_limit; |
| 38 | uint32_t zi_start; |
| 39 | uint32_t zi_limit; |
| 40 | uint32_t stack_bottom; |
| 41 | uint32_t stack_top; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 42 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 43 | uint32_t periph_start; |
| 44 | uint32_t periph_limit; |
| 45 | uint16_t periph_ppc_bank; |
| 46 | uint16_t periph_ppc_loc; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 47 | sp_init_function partition_init; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 48 | }; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 49 | |
| 50 | struct spm_partition_desc_t { |
| 51 | struct spm_partition_static_data_t static_data; |
| 52 | struct spm_partition_runtime_data_t runtime_data; |
| 53 | }; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 54 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 55 | struct spm_partition_db_t { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 56 | uint32_t is_init; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 57 | uint32_t partition_count; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 58 | uint32_t running_partition_idx; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 59 | struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 62 | /* 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] | 63 | #define REGION(a, b, c) a##b##c |
| 64 | #define REGION_NAME(a, b, c) REGION(a, b, c) |
| 65 | #if TFM_LVL == 1 |
| 66 | #define REGION_DECLARE(a, b, c) |
| 67 | #else |
| 68 | #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] | 69 | #define PART_REGION_ADDR(partition, region) \ |
| 70 | (uint32_t)®ION_NAME(Image$$, partition, region) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 73 | |
| 74 | #if TFM_LVL == 1 |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 75 | #define PARTITION_INIT_STATIC_DATA(data, partition) \ |
| 76 | do { \ |
| 77 | data.partition_id = partition##_ID; \ |
| 78 | data.periph_start = 0U; \ |
| 79 | data.periph_limit = 0U; \ |
| 80 | data.periph_ppc_bank = 0U; \ |
| 81 | data.periph_ppc_loc = 0U; \ |
| 82 | data.partition_init = 0U; \ |
| 83 | } while (0) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 84 | #else |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 85 | #define PARTITION_INIT_STATIC_DATA(data, partition) \ |
| 86 | do { \ |
| 87 | data.partition_id = partition##_ID; \ |
| 88 | data.code_start = PART_REGION_ADDR(partition, $$Base); \ |
| 89 | data.code_limit = PART_REGION_ADDR(partition, $$Limit); \ |
| 90 | data.ro_start = PART_REGION_ADDR(partition, $$RO$$Base); \ |
| 91 | data.ro_limit = PART_REGION_ADDR(partition, $$RO$$Limit); \ |
| 92 | data.rw_start = PART_REGION_ADDR(partition, _DATA$$RW$$Base); \ |
| 93 | data.rw_limit = PART_REGION_ADDR(partition, _DATA$$RW$$Limit); \ |
| 94 | data.zi_start = PART_REGION_ADDR(partition, _DATA$$ZI$$Base); \ |
| 95 | data.zi_limit = PART_REGION_ADDR(partition, _DATA$$ZI$$Limit); \ |
| 96 | data.stack_bottom = PART_REGION_ADDR(partition, _STACK$$ZI$$Base); \ |
| 97 | data.stack_top = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \ |
| 98 | data.periph_start = 0U; \ |
| 99 | data.periph_limit = 0U; \ |
| 100 | data.periph_ppc_bank = 0U; \ |
| 101 | data.periph_ppc_loc = 0U; \ |
| 102 | data.partition_init = 0U; \ |
| 103 | } while (0) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 106 | #if TFM_LVL == 1 |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 107 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 108 | do { \ |
| 109 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 110 | data.caller_partition_idx = 0U; \ |
| 111 | data.orig_psp = 0U; \ |
| 112 | data.orig_psplim = 0U; \ |
| 113 | data.orig_lr = 0U; \ |
| 114 | data.share = 0U; \ |
| 115 | } while (0) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 116 | #else |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 117 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 118 | do { \ |
| 119 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 120 | data.caller_partition_idx = 0U; \ |
| 121 | data.orig_psp = 0U; \ |
| 122 | data.orig_psplim = 0U; \ |
| 123 | data.orig_lr = 0U; \ |
| 124 | data.share = 0U; \ |
| 125 | data.stack_ptr = \ |
| 126 | PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \ |
| 127 | } while (0) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 128 | #endif |
| 129 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 130 | #define PARTITION_DECLARE(partition) \ |
| 131 | do { \ |
| 132 | REGION_DECLARE(Image$$, partition, $$Base); \ |
| 133 | REGION_DECLARE(Image$$, partition, $$Limit); \ |
| 134 | REGION_DECLARE(Image$$, partition, $$RO$$Base); \ |
| 135 | REGION_DECLARE(Image$$, partition, $$RO$$Limit); \ |
| 136 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base); \ |
| 137 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit); \ |
| 138 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base); \ |
| 139 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit); \ |
| 140 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base); \ |
| 141 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit); \ |
| 142 | struct spm_partition_desc_t *part_ptr; \ |
| 143 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { \ |
| 144 | return SPM_ERR_INVALID_CONFIG; \ |
| 145 | } \ |
| 146 | part_ptr = &(g_spm_partition_db.partitions[ \ |
| 147 | g_spm_partition_db.partition_count]); \ |
| 148 | PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition); \ |
| 149 | PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \ |
| 150 | ++g_spm_partition_db.partition_count; \ |
| 151 | } while (0) |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 152 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 153 | #define PARTITION_ADD_INIT_FUNC(partition, init_func) \ |
| 154 | do { \ |
| 155 | extern int32_t init_func(void); \ |
| 156 | uint32_t partition_idx = get_partition_idx(partition##_ID); \ |
| 157 | struct spm_partition_desc_t *part_ptr = \ |
| 158 | &(g_spm_partition_db.partitions[partition_idx]); \ |
| 159 | part_ptr->static_data.partition_init = init_func; \ |
| 160 | } while (0) |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 161 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 162 | #define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc) \ |
| 163 | do { \ |
| 164 | uint32_t partition_idx = get_partition_idx(partition##_ID); \ |
| 165 | struct spm_partition_desc_t *part_ptr = \ |
| 166 | &(g_spm_partition_db.partitions[partition_idx]); \ |
| 167 | part_ptr->static_data.periph_start = start; \ |
| 168 | part_ptr->static_data.periph_limit = limit; \ |
| 169 | part_ptr->static_data.periph_ppc_bank = bank; \ |
| 170 | part_ptr->static_data.periph_ppc_loc = loc; \ |
| 171 | } while (0) |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 172 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 173 | #endif /* __SPM_DB_H__ */ |