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