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 | |
| 41 | #if TFM_LVL == 1 |
| 42 | #define PARTITION_INIT_MEMORY_DATA(data, partition) |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 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) |
| 57 | #endif |
| 58 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 59 | |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 60 | #if TFM_LVL == 1 |
| 61 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 62 | do { \ |
| 63 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
| 64 | } while (0) |
| 65 | #else |
| 66 | #define PARTITION_INIT_RUNTIME_DATA(data, partition) \ |
| 67 | do { \ |
| 68 | data.partition_state = SPM_PARTITION_STATE_UNINIT; \ |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 69 | /* The top of the stack is reserved for the iovec */ \ |
| 70 | /* parameters of the service called. That's why in */ \ |
| 71 | /* data.stack_ptr we extract sizeof(struct iovec_args_t) */ \ |
| 72 | /* from the limit. */ \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 73 | data.stack_ptr = \ |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 74 | PART_REGION_ADDR(partition, _STACK$$ZI$$Limit - \ |
| 75 | sizeof(struct iovec_args_t)); \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 76 | } while (0) |
| 77 | #endif |
| 78 | |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 79 | #define PARTITION_DECLARE(partition, flag, type, id, priority) \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 80 | do { \ |
| 81 | REGION_DECLARE(Image$$, partition, $$Base); \ |
| 82 | REGION_DECLARE(Image$$, partition, $$Limit); \ |
| 83 | REGION_DECLARE(Image$$, partition, $$RO$$Base); \ |
| 84 | REGION_DECLARE(Image$$, partition, $$RO$$Limit); \ |
| 85 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base); \ |
| 86 | REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit); \ |
| 87 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base); \ |
| 88 | REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit); \ |
| 89 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base); \ |
| 90 | REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit); \ |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 91 | int32_t flags = flag; \ |
| 92 | if (tfm_memcmp(type, TFM_PARTITION_TYPE_APP, \ |
| 93 | strlen(TFM_PARTITION_TYPE_APP)) == 0) { \ |
| 94 | flags |= SPM_PART_FLAG_APP_ROT; \ |
| 95 | } else if (tfm_memcmp(type, TFM_PARTITION_TYPE_PSA, \ |
| 96 | strlen(TFM_PARTITION_TYPE_PSA)) == 0) { \ |
| 97 | flags |= SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT; \ |
| 98 | } else { \ |
| 99 | return SPM_ERR_INVALID_CONFIG; \ |
| 100 | } \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 101 | struct spm_partition_desc_t *part_ptr; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 102 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { \ |
| 103 | return SPM_ERR_INVALID_CONFIG; \ |
| 104 | } \ |
| 105 | part_ptr = &(g_spm_partition_db.partitions[ \ |
| 106 | g_spm_partition_db.partition_count]); \ |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 107 | PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition, flags, \ |
| 108 | id, priority); \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 109 | PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 110 | PARTITION_INIT_MEMORY_DATA(part_ptr->memory_data, partition); \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 111 | ++g_spm_partition_db.partition_count; \ |
| 112 | } while (0) |
| 113 | |
| 114 | #define PARTITION_ADD_INIT_FUNC(partition, init_func) \ |
| 115 | do { \ |
| 116 | extern int32_t init_func(void); \ |
| 117 | uint32_t partition_idx = get_partition_idx(partition##_ID); \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 118 | struct spm_partition_desc_t *part_ptr = \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 119 | &(g_spm_partition_db.partitions[partition_idx]); \ |
| 120 | part_ptr->static_data.partition_init = init_func; \ |
| 121 | } while (0) |
| 122 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 123 | #define PARTITION_ADD_PERIPHERAL(partition, peripheral) \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 124 | do { \ |
| 125 | uint32_t partition_idx = get_partition_idx(partition##_ID); \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 126 | struct spm_partition_desc_t *part_ptr = \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 127 | &(g_spm_partition_db.partitions[partition_idx]); \ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 128 | part_ptr->platform_data = peripheral; \ |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 129 | } while (0) |
| 130 | |
| 131 | #endif /* __SPM_DB_SETUP_H__ */ |