blob: 217cfaa39bd480fc40c39369c412989591953b30 [file] [log] [blame]
Mate Toth-Pale1475332018-04-09 17:28:49 +02001/*
Edison Aibb614aa2018-11-21 15:15:00 +08002 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Mate Toth-Pale1475332018-04-09 17:28:49 +02003 *
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-Pal936c33b2018-04-10 14:02:07 +020015 * \brief Get the index of a partition.
Mate Toth-Pale1475332018-04-09 17:28:49 +020016 *
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020017 * Gets the index of a partition in the partition db based on the partition ID
18 * provided as a parameter.
Mate Toth-Pale1475332018-04-09 17:28:49 +020019 *
20 * \param[in] partition_id The ID of the partition
21 *
22 * \return \ref INVALID_PARTITION_IDX if the provided ID is invalid. The index
23 * of the partition otherwise.
24 */
25uint32_t get_partition_idx(uint32_t partition_id);
26
27struct spm_partition_db_t {
28 uint32_t is_init;
29 uint32_t partition_count;
30 uint32_t running_partition_idx;
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020031 struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
Mate Toth-Pale1475332018-04-09 17:28:49 +020032};
33
Edison Aibb614aa2018-11-21 15:15:00 +080034#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-Pale1475332018-04-09 17:28:49 +020039 } while (0)
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020040
41#if TFM_LVL == 1
42#define PARTITION_INIT_MEMORY_DATA(data, partition)
Mate Toth-Pale1475332018-04-09 17:28:49 +020043#else
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020044#define PARTITION_INIT_MEMORY_DATA(data, partition) \
Mate Toth-Pale1475332018-04-09 17:28:49 +020045 do { \
Mate Toth-Pale1475332018-04-09 17:28:49 +020046 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-Pal936c33b2018-04-10 14:02:07 +020059
Mate Toth-Pale1475332018-04-09 17:28:49 +020060#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; \
69 data.stack_ptr = \
70 PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \
71 } while (0)
72#endif
73
Edison Aibb614aa2018-11-21 15:15:00 +080074#define PARTITION_DECLARE(partition, flag, type, id, priority) \
Mate Toth-Pale1475332018-04-09 17:28:49 +020075 do { \
76 REGION_DECLARE(Image$$, partition, $$Base); \
77 REGION_DECLARE(Image$$, partition, $$Limit); \
78 REGION_DECLARE(Image$$, partition, $$RO$$Base); \
79 REGION_DECLARE(Image$$, partition, $$RO$$Limit); \
80 REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base); \
81 REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit); \
82 REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base); \
83 REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit); \
84 REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base); \
85 REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit); \
Edison Aibb614aa2018-11-21 15:15:00 +080086 int32_t flags = flag; \
87 if (tfm_memcmp(type, TFM_PARTITION_TYPE_APP, \
88 strlen(TFM_PARTITION_TYPE_APP)) == 0) { \
89 flags |= SPM_PART_FLAG_APP_ROT; \
90 } else if (tfm_memcmp(type, TFM_PARTITION_TYPE_PSA, \
91 strlen(TFM_PARTITION_TYPE_PSA)) == 0) { \
92 flags |= SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT; \
93 } else { \
94 return SPM_ERR_INVALID_CONFIG; \
95 } \
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020096 struct spm_partition_desc_t *part_ptr; \
Mate Toth-Pale1475332018-04-09 17:28:49 +020097 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { \
98 return SPM_ERR_INVALID_CONFIG; \
99 } \
100 part_ptr = &(g_spm_partition_db.partitions[ \
101 g_spm_partition_db.partition_count]); \
Edison Aibb614aa2018-11-21 15:15:00 +0800102 PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition, flags, \
103 id, priority); \
Mate Toth-Pale1475332018-04-09 17:28:49 +0200104 PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200105 PARTITION_INIT_MEMORY_DATA(part_ptr->memory_data, partition); \
Mate Toth-Pale1475332018-04-09 17:28:49 +0200106 ++g_spm_partition_db.partition_count; \
107 } while (0)
108
109#define PARTITION_ADD_INIT_FUNC(partition, init_func) \
110 do { \
111 extern int32_t init_func(void); \
112 uint32_t partition_idx = get_partition_idx(partition##_ID); \
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200113 struct spm_partition_desc_t *part_ptr = \
Mate Toth-Pale1475332018-04-09 17:28:49 +0200114 &(g_spm_partition_db.partitions[partition_idx]); \
115 part_ptr->static_data.partition_init = init_func; \
116 } while (0)
117
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200118#define PARTITION_ADD_PERIPHERAL(partition, peripheral) \
Mate Toth-Pale1475332018-04-09 17:28:49 +0200119 do { \
120 uint32_t partition_idx = get_partition_idx(partition##_ID); \
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200121 struct spm_partition_desc_t *part_ptr = \
Mate Toth-Pale1475332018-04-09 17:28:49 +0200122 &(g_spm_partition_db.partitions[partition_idx]); \
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200123 part_ptr->platform_data = peripheral; \
Mate Toth-Pale1475332018-04-09 17:28:49 +0200124 } while (0)
125
126#endif /* __SPM_DB_SETUP_H__ */