Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 1 | /* |
Gyorgy Szing | 40a7af0 | 2019-02-06 14:19:47 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __SPM_DB_SETUP_H__ |
| 9 | #define __SPM_DB_SETUP_H__ |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | #include "spm_db.h" |
| 13 | |
| 14 | /** |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 15 | * \brief Get the index of a partition. |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 16 | * |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 17 | * Gets the index of a partition in the partition db based on the partition ID |
| 18 | * provided as a parameter. |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 19 | * |
Gyorgy Szing | 40a7af0 | 2019-02-06 14:19:47 +0100 | [diff] [blame] | 20 | * \param[in] partition_idx The index of the partition |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 21 | * |
Gyorgy Szing | 40a7af0 | 2019-02-06 14:19:47 +0100 | [diff] [blame] | 22 | * \return \ref INVALID_PARTITION_IDX if the provided index is invalid. The |
| 23 | * index of the partition otherwise. |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 24 | */ |
| 25 | uint32_t get_partition_idx(uint32_t partition_id); |
| 26 | |
| 27 | struct spm_partition_db_t { |
| 28 | uint32_t is_init; |
| 29 | uint32_t partition_count; |
| 30 | uint32_t running_partition_idx; |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 31 | struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS]; |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 34 | #define PARTITION_INIT_STATIC_DATA(data, partition, flags, id, priority) \ |
| 35 | do { \ |
| 36 | data.partition_id = partition##_ID; \ |
| 37 | data.partition_flags = flags; \ |
| 38 | data.partition_priority = TFM_PRIORITY(priority); \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 39 | } while (0) |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 40 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 41 | #if (TFM_LVL == 1) && !defined(TFM_PSA_API) |
| 42 | #define PARTITION_INIT_MEMORY_DATA(data, partition) |
| 43 | #else |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 44 | #define PARTITION_INIT_MEMORY_DATA(data, partition) \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 45 | do { \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 46 | data.code_start = PART_REGION_ADDR(partition, $$Base); \ |
| 47 | data.code_limit = PART_REGION_ADDR(partition, $$Limit); \ |
| 48 | data.ro_start = PART_REGION_ADDR(partition, $$RO$$Base); \ |
| 49 | data.ro_limit = PART_REGION_ADDR(partition, $$RO$$Limit); \ |
| 50 | data.rw_start = PART_REGION_ADDR(partition, _DATA$$RW$$Base); \ |
| 51 | data.rw_limit = PART_REGION_ADDR(partition, _DATA$$RW$$Limit); \ |
| 52 | data.zi_start = PART_REGION_ADDR(partition, _DATA$$ZI$$Base); \ |
| 53 | data.zi_limit = PART_REGION_ADDR(partition, _DATA$$ZI$$Limit); \ |
| 54 | data.stack_bottom = PART_REGION_ADDR(partition, _STACK$$ZI$$Base); \ |
| 55 | data.stack_top = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \ |
| 56 | } while (0) |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 57 | #endif |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 58 | |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame^] | 59 | /* The max size of the context stack can be calculated as a function of the IRQ |
| 60 | * count of the secure partition: |
| 61 | * |
| 62 | * max_stack_size = intr_ctx_size + (IRQ_CNT * (intr_ctx_size + hndl_ctx_size)) |
| 63 | * |
| 64 | * where: |
| 65 | * intr_ctx: Frame pushed when the partition is interrupted |
| 66 | * hndl_ctx: Frame pushed when the partition is handling an interrupt |
| 67 | */ |
| 68 | #define DECLARE_CONTEXT_STACK(partition) \ |
| 69 | static uint32_t ctx_stack_ptr_##partition[ \ |
| 70 | (sizeof(struct interrupted_ctx_stack_frame_t) + \ |
| 71 | (TFM_PARTITION_##partition##_IRQ_COUNT) * ( \ |
| 72 | sizeof(struct interrupted_ctx_stack_frame_t) + \ |
| 73 | sizeof(struct handler_ctx_stack_frame_t) \ |
| 74 | )) / sizeof(uint32_t)] |
| 75 | |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 76 | #if TFM_LVL == 1 |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame^] | 77 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 78 | do { \ |
| 79 | DECLARE_CONTEXT_STACK(partition); \ |
| 80 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 81 | data.ctx_stack_ptr = ctx_stack_ptr_##partition; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 82 | } while (0) |
| 83 | #else |
| 84 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 85 | do { \ |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame^] | 86 | DECLARE_CONTEXT_STACK(partition); \ |
| 87 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 88 | /* The top of the stack is reserved for the iovec */ \ |
| 89 | /* parameters of the service called. That's why in */ \ |
| 90 | /* data.stack_ptr we extract sizeof(struct iovec_args_t) */ \ |
| 91 | /* from the limit. */ \ |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame^] | 92 | data.stack_ptr = \ |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 93 | PART_REGION_ADDR(partition, _STACK$$ZI$$Limit - \ |
| 94 | sizeof(struct iovec_args_t)); \ |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame^] | 95 | data.ctx_stack_ptr = ctx_stack_ptr_##partition; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 96 | } while (0) |
| 97 | #endif |
| 98 | |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 99 | #define PARTITION_DECLARE(partition, flag, type, id, priority) \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 100 | do { \ |
| 101 | REGION_DECLARE(Image$$, partition, $$Base); \ |
| 102 | REGION_DECLARE(Image$$, partition, $$Limit); \ |
| 103 | REGION_DECLARE(Image$$, partition, $$RO$$Base); \ |
| 104 | REGION_DECLARE(Image$$, partition, $$RO$$Limit); \ |
| 105 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base); \ |
| 106 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit); \ |
| 107 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base); \ |
| 108 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit); \ |
| 109 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base); \ |
| 110 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit); \ |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 111 | int32_t flags = flag; \ |
| 112 | if (tfm_memcmp(type, TFM_PARTITION_TYPE_APP, \ |
| 113 | strlen(TFM_PARTITION_TYPE_APP)) == 0) { \ |
| 114 | flags |= SPM_PART_FLAG_APP_ROT; \ |
| 115 | } else if (tfm_memcmp(type, TFM_PARTITION_TYPE_PSA, \ |
| 116 | strlen(TFM_PARTITION_TYPE_PSA)) == 0) { \ |
| 117 | flags |= SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT; \ |
| 118 | } else { \ |
| 119 | return SPM_ERR_INVALID_CONFIG; \ |
| 120 | } \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 121 | struct spm_partition_desc_t *part_ptr; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 122 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { \ |
| 123 | return SPM_ERR_INVALID_CONFIG; \ |
| 124 | } \ |
| 125 | part_ptr = &(g_spm_partition_db.partitions[ \ |
| 126 | g_spm_partition_db.partition_count]); \ |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 127 | PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition, flags, \ |
| 128 | id, priority); \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 129 | PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 130 | PARTITION_INIT_MEMORY_DATA(part_ptr->memory_data, partition); \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 131 | ++g_spm_partition_db.partition_count; \ |
| 132 | } while (0) |
| 133 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 134 | #define PARTITION_ADD_INIT_FUNC(partition, init_func) \ |
| 135 | do { \ |
| 136 | extern int32_t init_func(void); \ |
| 137 | uint32_t partition_idx = get_partition_idx(partition##_ID); \ |
| 138 | struct spm_partition_desc_t *part_ptr = \ |
| 139 | &(g_spm_partition_db.partitions[partition_idx]); \ |
| 140 | part_ptr->static_data.partition_init = init_func; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 141 | } while (0) |
| 142 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 143 | #define PARTITION_ADD_PERIPHERAL(partition, peripheral) \ |
| 144 | do { \ |
| 145 | uint32_t partition_idx = get_partition_idx(partition##_ID); \ |
| 146 | struct spm_partition_desc_t *part_ptr = \ |
| 147 | &(g_spm_partition_db.partitions[partition_idx]); \ |
| 148 | part_ptr->platform_data = peripheral; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 149 | } while (0) |
| 150 | |
| 151 | #endif /* __SPM_DB_SETUP_H__ */ |