1 /*
2  * Copyright (c) 2021-2024, Arm Limited. All rights reserved.
3  * Copyright (c) 2022-2024 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 #ifndef __PARTITION_DEFS_H__
12 #define __PARTITION_DEFS_H__
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 /* TF-M internal partition ID */
18 #define TFM_SP_IDLE                          (1U)
19 #define TFM_SP_TZ_AGENT                      (2U)
20 #define INVALID_PARTITION_ID                 (0U)
21 
22 /* Encode a magic number into version for validating partition info */
23 #define PARTITION_INFO_VERSION_MASK             (0x0000FFFF)
24 #define PARTITION_INFO_MAGIC_MASK               (0xFFFF0000)
25 #define PARTITION_INFO_MAGIC                    (0x5F5F0000)
26 
27 /*
28  * Partition flag start
29  *
30  * 31      12 11 10  9   8  7         0
31  * +---------+--+--+---+---+----------+
32  * | RES[20] |TZ|MB|I/S|A/P| Priority |
33  * +---------+--+--+---+---+----------+
34  *
35  * Field                Desc                        Value
36  * Priority, bits[7:0]:  Partition Priority          Lowest, low, normal, high, highest
37  *                       set in manifest.
38  * A/P, bit[8]:          ARoT or PRoT domain         1: PRoT              0: ARoT
39  * I/S, bit[9]:          IPC or SFN typed partition  1: IPC               0: SFN
40  * MB,  bit[10]:         NS Agent Mailbox or not     1: NS Agent mailbox  0: Not
41  * TZ,  bit[11]:         NS Agent TZ or not          1: NS Agent TZ       0: Not
42  * RES, bits[31:12]:     20 bits reserved            0
43  */
44 #define PARTITION_PRI_HIGHEST                   (0x0)
45 #define PARTITION_PRI_HIGH                      (0xF)
46 #define PARTITION_PRI_NORMAL                    (0x1F)
47 #define PARTITION_PRI_LOW                       (0x7F)
48 #define PARTITION_PRI_LOWEST                    (0xFF)
49 #define PARTITION_PRI_MASK                      (0xFF)
50 
51 #define PARTITION_MODEL_PSA_ROT                 (1UL << 8)
52 #define PARTITION_MODEL_IPC                     (1UL << 9)
53 
54 #define PARTITION_NS_AGENT_MB                   (1UL << 10)
55 #define PARTITION_NS_AGENT_TZ                   (1UL << 11)
56 
57 #define TO_THREAD_PRIORITY(x)                   (x)
58 
59 #define ENTRY_TO_POSITION(x)                    (uintptr_t)(x)
60 #define POSITION_TO_ENTRY(x, t)                 (t)(x)
61 
62 #define PTR_TO_REFERENCE(x)                     (uintptr_t)(x)
63 #define REFERENCE_TO_PTR(x, t)                  (t)(x)
64 
65 #define IS_PSA_ROT(pldi)                        (!!((pldi)->flags \
66                                                      & PARTITION_MODEL_PSA_ROT))
67 #define IS_IPC_MODEL(pldi)                      (!!((pldi)->flags \
68                                                      & PARTITION_MODEL_IPC))
69 #define IS_NS_AGENT(pldi)                       (!!((pldi)->flags \
70                                                      & (PARTITION_NS_AGENT_MB | PARTITION_NS_AGENT_TZ)))
71 #ifdef CONFIG_TFM_USE_TRUSTZONE
72 #define IS_NS_AGENT_TZ(pldi)                    (!!((pldi)->flags & PARTITION_NS_AGENT_TZ))
73 #else
74 #define IS_NS_AGENT_TZ(pldi)                    ((void)pldi, false)
75 #endif
76 #ifdef TFM_PARTITION_NS_AGENT_MAILBOX
77 #define IS_NS_AGENT_MAILBOX(pldi)               (!!((pldi)->flags & PARTITION_NS_AGENT_MB))
78 #else
79 #define IS_NS_AGENT_MAILBOX(pldi)               ((void)pldi, false)
80 #endif
81 
82 #define PARTITION_TYPE_TO_INDEX(type)           (!!((type) & PARTITION_NS_AGENT_TZ))
83 
84 /* Partition flag end */
85 
86 /*
87  * Calculate partition loading order according to its priority only.
88  * Dependencies are excluded from the calculation.
89  * This macro shall only be called by Secure Partition statically defined
90  * without manifest file, such as idle partition and TrustZone NS Agent.
91  */
92 #define LOAD_ORDER_BY_PRIORITY(priority)        (((uint16_t)(priority)) << 8)
93 
94 /*
95  * Common partition structure type, the extendable data is right after it.
96  * Extendable data has different size for each partition, and must be 4-byte
97  * aligned. It includes: stack and heap position, dependencies, services and
98  * assets data.
99  */
100 struct partition_load_info_t {
101     uint32_t        psa_ff_ver;         /* Encode the version with magic    */
102     int32_t         pid;                /* Partition ID                     */
103     uint32_t        flags;              /* ARoT/PRoT, SFN/IPC, priority set
104                                          * in Secure Partition manifest.
105                                          */
106     uintptr_t       entry;              /* Entry point                      */
107     size_t          stack_size;         /* Stack size                       */
108     size_t          heap_size;          /* Heap size                        */
109     uint32_t        ndeps;              /* Dependency number                */
110     uint32_t        nservices;          /* Service number                   */
111     uint32_t        nassets;            /* Asset numbers                    */
112     uint32_t        nirqs;              /* Number of IRQ owned by Partition */
113     int32_t         client_id_base;     /* The min translated client ID     */
114     int32_t         client_id_limit;    /* The max translated client ID     */
115     uint16_t        load_order;         /* loading order of this partition
116                                          * The value is calculated based on the
117                                          * priority set in partition manifest
118                                          * and its dependencies.
119                                          * A partition with a smaller loading
120                                          * order value is loaded earlier.
121                                          * - A partition with a higher manifest
122                                          *   priority is loaded earlier than
123                                          *   those with lower priorities.
124                                          * - A partition is loaded later than
125                                          *   its dependencies.
126                                          */
127 } __attribute__((aligned(4)));
128 
129 #endif /* __PARTITION_DEFS_H__ */
130