1/*
2 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
3 * Copyright (c) 2021-2023 Cypress Semiconductor Corporation (an Infineon
4 * company) or an affiliate of Cypress Semiconductor Corporation. All rights
5 * reserved.
6 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 */
10
11/***********{{utilities.donotedit_warning}}***********/
12
13#include <stdint.h>
14#include <stddef.h>
15#include "config_tfm.h"
16#include "region.h"
17#include "region_defs.h"
18#include "spm.h"
19#include "load/interrupt_defs.h"
20#include "load/partition_defs.h"
21#include "load/service_defs.h"
22#include "load/asset_defs.h"
23#include "tfm_peripherals_def.h"
24#include "psa_manifest/pid.h"
25#include "psa_manifest/sid.h"
26#include "psa_manifest/{{manifest_out_basename}}.h"
27
28{% set counter = namespace() %}
29{% set counter.dep_counter = manifest.dependencies|count + manifest.weak_dependencies|count %}
30#define {{"%-55s"|format(manifest.name|upper + "_NDEPS")}} ({{"%d"|format(counter.dep_counter)}})
31{% set counter.service_counter = manifest.services|count %}
32#define {{"%-55s"|format(manifest.name|upper + "_NSERVS")}} ({{"%d"|format(counter.service_counter)}})
33{% set counter.asset_counter = manifest.mmio_regions|count %}
34#if TFM_ISOLATION_LEVEL == 3
35#define {{"%-55s"|format(manifest.name|upper + "_NASSETS")}} ({{"%d"|format(counter.asset_counter)}} + 1)
36#else
37#define {{"%-55s"|format(manifest.name|upper + "_NASSETS")}} ({{"%d"|format(counter.asset_counter)}})
38#endif
39{% set counter.irq_counter = manifest.irqs|count %}
40#define {{"%-55s"|format(manifest.name|upper + "_NIRQS")}} ({{"%d"|format(counter.irq_counter)}})
41
42/* Memory region declaration */
43#if TFM_ISOLATION_LEVEL == 3
44REGION_DECLARE(Image$$, PT_{{manifest.name}}_PRIVATE, _DATA_START$$Base);
45REGION_DECLARE(Image$$, PT_{{manifest.name}}_PRIVATE, _DATA_END$$Base);
46#endif
47
48{% if config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] == '1' or manifest.model == "IPC" %}
49extern uint8_t {{manifest.name|lower}}_stack[];
50{% endif %}
51
52{% if manifest.model == "IPC" %}
53/* Entrypoint function declaration */
54extern void {{manifest.entry_point}}(void);
55{% elif manifest.entry_init %}
56extern psa_status_t {{manifest.entry_init}}(void);
57{% endif %}
58
59/* Interrupt init functions */
60{% if counter.irq_counter > 0 %}
61    {% for irq in manifest.irqs %}
62        {% if irq.source is number %}}
63extern enum tfm_hal_status_t {{"irq_" + irq.source|string + "_init"}}(void *p_pt,
64                                                                      const struct irq_load_info_t *p_ildi);
65        {% else %}
66extern enum tfm_hal_status_t {{irq.source|lower + "_init"}}(void *p_pt,
67                                                            const struct irq_load_info_t *p_ildi);
68        {% endif %}
69    {% endfor %}
70{% endif %}
71
72/* partition load info type definition */
73struct partition_{{manifest.name|lower}}_load_info_t {
74    /* common length load data */
75    struct partition_load_info_t    load_info;
76    /* per-partition variable length load data */
77    uintptr_t                       stack_addr;
78    uintptr_t                       heap_addr;
79{% if counter.dep_counter > 0 %}
80    uint32_t                        deps[{{(manifest.name|upper + "_NDEPS")}}];
81{% endif %}
82{% if counter.service_counter > 0 %}
83    struct service_load_info_t      services[{{(manifest.name|upper + "_NSERVS")}}];
84{% endif %}
85#if TFM_ISOLATION_LEVEL == 3
86    struct asset_desc_t             assets[{{(manifest.name|upper + "_NASSETS")}}];
87#else
88{% if counter.asset_counter > 0 %}
89    struct asset_desc_t             assets[{{(manifest.name|upper + "_NASSETS")}}];
90{% endif %}
91#endif
92{% if counter.irq_counter > 0 %}
93    struct irq_load_info_t          irqs[{{(manifest.name|upper + "_NIRQS")}}];
94{% endif %}
95} __attribute__((aligned(4)));
96
97/* Partition load, deps, service load data. Put to a dedicated section. */
98#if defined(__ICCARM__)
99#pragma location = ".part_load_priority_{{numbered_priority}}"
100__root
101#endif /* __ICCARM__ */
102const struct partition_{{manifest.name|lower}}_load_info_t {{manifest.name|lower}}_load
103    __attribute__((used, section(".part_load_priority_{{numbered_priority}}"))) = {
104    .load_info = {
105{% if manifest.psa_framework_version == 1.0 %}
106        .psa_ff_ver                 = 0x0100 | PARTITION_INFO_MAGIC,
107{% elif manifest.psa_framework_version == 1.1 %}
108        .psa_ff_ver                 = 0x0101 | PARTITION_INFO_MAGIC,
109{% endif %}
110        .pid                        = {{manifest.name}},
111        .flags                      = 0
112{% if manifest.model == "IPC" %}
113                                    | PARTITION_MODEL_IPC
114{% endif %}
115{% if manifest.type == "PSA-ROT" %}
116                                    | PARTITION_MODEL_PSA_ROT
117{% endif %}
118{% if manifest.ns_agent is sameas true %}
119                                    | PARTITION_NS_AGENT_MB
120{% endif %}
121                                    | PARTITION_PRI_{{manifest.priority}},
122        .entry                      = ENTRY_TO_POSITION({{manifest.entry}}),
123{% if config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] == '1' or manifest.model == "IPC" %}
124        .stack_size                 = {{manifest.stack_size}},
125{% else %}
126        .stack_size                 = 0,
127{% endif %}
128        .heap_size                  = 0,
129        .ndeps                      = {{(manifest.name|upper + "_NDEPS")}},
130        .nservices                  = {{(manifest.name|upper + "_NSERVS")}},
131        .nassets                    = {{(manifest.name|upper + "_NASSETS")}},
132        .nirqs                      = {{(manifest.name|upper + "_NIRQS")}},
133    },
134{% if config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] == '1' or manifest.model == "IPC" %}
135    .stack_addr                     = (uintptr_t){{manifest.name|lower}}_stack,
136{% else %}
137    .stack_addr                     = 0,
138{% endif %}
139    .heap_addr                      = 0,
140{% if counter.dep_counter > 0 %}
141    .deps = {
142    {% for dep in manifest.dependencies %}
143        {{dep}}_SID,
144    {% endfor %}
145    {% for dep in manifest.weak_dependencies %}
146#ifdef {{dep}}_SID
147        {{dep}}_SID,
148#endif
149    {% endfor %}
150    },
151{% endif %}
152{% if counter.service_counter > 0 %}
153    .services = {
154    {% for service in manifest.services %}
155        {
156            .name_strid             = STRING_PTR_TO_STRID("{{service.name}}"),
157        {% if manifest.model == "SFN" %}
158            .sfn                    = ENTRY_TO_POSITION({{service.name|lower}}_sfn),
159        {% else %}
160            .sfn                    = 0,
161        {% endif %}
162{% if config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] == '1' %}
163        {% if manifest.model == "SFN" %}
164            .signal                 = {{service.signal_value}},
165        {% else %}
166            .signal                 = {{service.name}}_SIGNAL,
167        {% endif %}
168{% endif %}
169
170            .sid                    = {{service.sid}},
171            .flags                  = 0
172        {% if service.non_secure_clients is sameas true %}
173                                    | SERVICE_FLAG_NS_ACCESSIBLE
174        {% endif %}
175        {% if service.connection_based is sameas false %}
176                                    | SERVICE_FLAG_STATELESS | 0x{{"%x"|format(service.stateless_handle_index)}}
177        {% endif %}
178        {% if service.mm_iovec == "enable" %}
179                                    | SERVICE_FLAG_MM_IOVEC
180        {% endif %}
181                                    | SERVICE_VERSION_POLICY_{{service.version_policy}},
182            .version                = {{service.version}},
183        },
184    {% endfor %}
185    },
186{% endif %}
187#if TFM_ISOLATION_LEVEL == 3
188    .assets                         = {
189        {
190            .mem.start              = (uintptr_t)&REGION_NAME(Image$$, PT_{{manifest.name}}_PRIVATE, _DATA_START$$Base),
191            .mem.limit              = (uintptr_t)&REGION_NAME(Image$$, PT_{{manifest.name}}_PRIVATE, _DATA_END$$Base),
192            .attr                   = ASSET_ATTR_READ_WRITE,
193        },
194{% if counter.asset_counter > 0 %}
195    {% for region in manifest.mmio_regions %}
196        {% if region.conditional %}
197#ifdef {{region.conditional}}
198        {% endif %}
199        {% if region.base and region.size %}
200        {
201            .mem.start              = {{region.base}},
202            .mem.limit              = {{region.base}} + {{region.size}},
203            .attr                   = ASSET_ATTR_NUMBERED_MMIO
204        {% elif region.name %}
205        {
206            .dev.dev_ref            = PTR_TO_REFERENCE({{region.name}}),
207            .attr                   = ASSET_ATTR_NAMED_MMIO
208        {% endif %}
209        {% if region.permission == "READ-WRITE" %}
210                                    | ASSET_ATTR_READ_WRITE,
211        {% else %}
212                                    | ASSET_ATTR_READ_ONLY,
213        {% endif %}
214        },
215        {% if region.conditional %}
216#endif
217        {% endif %}
218    {% endfor %}
219{% endif %}
220    },
221#else
222{% if counter.asset_counter > 0 %}
223    .assets                         = {
224    {% for region in manifest.mmio_regions %}
225        {% if region.conditional %}
226#ifdef {{region.conditional}}
227        {% endif %}
228        {% if region.base and region.size %}
229        {
230            .mem.start              = {{region.base}},
231            .mem.limit              = {{region.base}} + {{region.size}},
232            .attr                   = ASSET_ATTR_NUMBERED_MMIO
233        {% elif region.name %}
234        {
235            .dev.dev_ref            = PTR_TO_REFERENCE({{region.name}}),
236            .attr                   = ASSET_ATTR_NAMED_MMIO
237        {% endif %}
238        {% if region.permission == "READ-WRITE" %}
239                                    | ASSET_ATTR_READ_WRITE,
240        {% else %}
241                                    | ASSET_ATTR_READ_ONLY,
242        {% endif %}
243        },
244        {% if region.conditional %}
245#endif
246        {% endif %}
247    {% endfor %}
248    },
249{% endif %}
250#endif
251{% if counter.irq_counter > 0 %}
252    .irqs = {
253    {% for irq in manifest.irqs %}
254        {% set irq_info = namespace() %}
255        {% set irq_info.source = irq.source %}
256        {% if irq.source is number %}}
257            {% set irq_info.source_symbol = "irq_" + irq.source|string %}
258        {% else %}
259            {% set irq_info.source_symbol = irq.source|lower %}
260        {% endif %}
261        {% if manifest.psa_framework_version == 1.1 and irq.handling == "FLIH" %}
262            {% set irq_info.flih_func = irq.name|lower + "_flih" %}
263        {% else %}
264            {% set irq_info.flih_func = 0 %}
265        {% endif %}
266        {% if manifest.psa_framework_version == 1.0 %}
267            {% set irq_info.signal = irq.signal %}
268        {% else %}
269            {% set irq_info.signal = irq.name + "_SIGNAL" %}
270        {% endif %}
271        {
272            .init = {{irq_info.source_symbol + "_init"}},
273            .flih_func = {{irq_info.flih_func}},
274            .pid = {{manifest.name}},
275            .source = {{irq_info.source}},
276            .signal = {{irq_info.signal}},
277        },
278    {% endfor %}
279    },
280{% endif %}
281};
282
283/* Placeholder for partition and service runtime space. Do not reference it. */
284#if defined(__ICCARM__)
285#pragma location=".bss.part_runtime_priority_{{numbered_priority}}"
286__root
287#endif /* __ICCARM__ */
288static struct partition_t {{manifest.name|lower}}_partition_runtime_item
289    __attribute__((used, section(".bss.part_runtime_priority_{{numbered_priority}}")));
290{% if counter.service_counter > 0 %}
291#if defined(__ICCARM__)
292#pragma location = ".bss.serv_runtime_priority_{{numbered_priority}}"
293__root
294#endif /* __ICCARM__ */
295static struct service_t {{manifest.name|lower}}_service_runtime_item[{{(manifest.name|upper + "_NSERVS")}}]
296    __attribute__((used, section(".bss.serv_runtime_priority_{{numbered_priority}}")));
297{% endif %}
298