1 /*
2  * Copyright (c) 2024 Arm Limited. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * \file host_atu_base_address.h
19  * \brief This file defines the host memory ATU addresses.
20  */
21 
22 #ifndef __HOST_ATU_BASE_ADDRESS_H__
23 #define __HOST_ATU_BASE_ADDRESS_H__
24 
25 #include "platform_base_address.h"
26 #include "size_defs.h"
27 
28 #define ALIGN_UP(num, align)    (((num) + ((align) - 1)) & ~((align) - 1))
29 
30 #define RSE_ATU_BASE_ID 0
31 
32 enum rse_atu_ids {
33     /*
34      * ATU regions for loading firmware in BL2. Reused per firmware. Not used
35      * outside BL2.
36      */
37 
38     /* ID to use for region loading the header of an image */
39     RSE_ATU_IMG_HDR_LOAD_ID = RSE_ATU_BASE_ID,
40     /* ID to use for region loading the rest of an image */
41     RSE_ATU_IMG_CODE_LOAD_ID,
42     /* ID to use for region initializing firmware */
43     RSE_ATU_FW_INIT_ID,
44 
45     /* ATU region ID for programming NI-Tower */
46     RSE_ATU_NI_TOWER_ID,
47     /* ATU region ID for SYSCTRL SMMU */
48     RSE_ATU_SYSCTRL_SMMU_ID,
49 };
50 
51 /*
52  * ATU controller enforces a minimum size and all regions are restricted to
53  * align with it
54  */
55 #define RSE_ATU_PAGE_SIZE       0x2000U /* 8KB */
56 
57 /*
58  * In flash the image is layed out like so:
59  *
60  * ┌────────┬──────┬─────────┐
61  * │ HEADER │ CODE │ TRAILER │
62  * ├────────┴──────┴─────────┤
63  * │ Flash Image             │
64  * └─────────────────────────┘
65  *
66  * However, when the image is loaded the CODE region needs to be at the start
67  * of the memory for the host firmware to load.
68  *
69  * ┌──────┐
70  * │ CODE │
71  * ├──────┴─────────┐
72  * │ ITCM           │
73  * └────────────────┘
74  *
75  * MCUBoot requires a contiguous copy. To allow this while moving the header to
76  * a different memory region, the ATU can be used to remap contiguous logical
77  * addresses to different locations.
78  *
79  * The ATU has a fixed size page which limits where boundaries can be. In order
80  * to have the ATU boundary match the header code boundary of the image, the
81  * image is aligned in logical memory so the boundaries match.
82  *
83  *                  ┌───────────┐         ┌─────────────────────────┐
84  * Physical Memory  │ Somewhere │         │ ITCM                    │
85  *                  └──┬────────┤         ├──────┬─────────┬────────┘
86  *                     │ HEADER │         │ CODE │ TRAILER │
87  *                     └───▲────┘         └──────┴─▲───────┘
88  *                         │                       │
89  *                         │                       │
90  *                  ┌──────┴─────┬─────────────────┴───────┐
91  *  Logical Memory  │ ATU HEADER │ ATU CODE                │
92  *                  └───┬────────┼──────┬─────────┬────────┘
93  *                      │ HEADER │ CODE │ TRAILER │
94  *                      └────────┴──────┴─────────┘
95  *
96  * If there is space at the end of the ITCM then the header can be placed there
97  * effectively wrapping the start of the copy to end.
98  *
99  *                     ┌──────┬─────────┬────────┐
100  *  Bootable Image     │ CODE │ TRAILER │ HEADER │
101  *                     ├──────┴─────────┴────────┤
102  *                     │ ATU CODE + TRAILER      │
103  *  ATU window in host └──────────▲─┬────────────┤
104  *  memory space                  │ │ ATU HEADER │
105  *                                │ └──────▲─────┘
106  *                            ┌───┼────────┘
107  *                            │   └────────────┐
108  *  ATU window in      ┌──────┴─────┬──────────┴──────────────┐
109  *  local memory space │ ATU HEADER │ ATU CODE + TRAILER      │
110  *                     ├───┬────────┼──────┬─────────┬────────┘
111  *  MCUBoot Image      │   │ HEADER │ CODE │ TRAILER │
112  *                     │   ├────────┼──────┴─────────┘
113  *                     │   │        │
114  *                     │   │        └─────► HOST_<IMG>_IMG_CODE_BASE_S
115  *                     │   │
116  *                     │   └──────────────► HOST_<IMG>_IMG_HDR_BASE_S
117  *                     │
118  *                     └──────────────────► HOST_<IMG>_HDR_ATU_WINDOW_BASE_S
119  *
120  * Alternatively the header and/or trailer could be mapped to a different
121  * memory such as the DTCM allowing the total image to be larger than the
122  * original memory as long as the code fits. Remapping the trailer as well will
123  * require an additional ATU region.
124  *
125  * Note: Regions not to scale
126  */
127 
128 /*
129  * The ATU has a minimum page size of 8K and the page must be aligned to the
130  * same sized block. The BL2_HEADER_SIZE is typically smaller than that at 1K.
131  * The end of the header needs to align to the end of the ATU so the code can
132  * be at the start of the next ATU region. So create an ATU window of 8K and
133  * place the header at 7K offset.
134  */
135 #define RSE_IMG_HDR_ATU_WINDOW_SIZE ALIGN_UP(BL2_HEADER_SIZE, RSE_ATU_PAGE_SIZE)
136 
137 /*
138  * RSE ATU Regions for image loading
139  * Note: these need to consistent with values used when signing the images.
140  * Note: MCUBoot requires that the logical addresses do not overlap.
141  */
142 
143 /* SCP */
144 
145 /* SCP ATU HEADER logical address start */
146 #define HOST_SCP_HDR_ATU_WINDOW_BASE_S  HOST_ACCESS_BASE_S
147 /* SCP Image address start, offset so end of HEADER is at end of ATU HEADER */
148 #define HOST_SCP_IMG_HDR_BASE_S         (HOST_SCP_HDR_ATU_WINDOW_BASE_S +   \
149                                          RSE_IMG_HDR_ATU_WINDOW_SIZE -      \
150                                          BL2_HEADER_SIZE)
151 /* SCP Code region and SCP ATU CODE logical address start */
152 #define HOST_SCP_IMG_CODE_BASE_S        (HOST_SCP_HDR_ATU_WINDOW_BASE_S +   \
153                                          RSE_IMG_HDR_ATU_WINDOW_SIZE)
154 /* SCP ATU CODE size (aligned size of SCP image) */
155 #define HOST_SCP_ATU_SIZE               ALIGN_UP(SIZE_DEF_SCP_IMAGE,        \
156                                                  RSE_ATU_PAGE_SIZE)
157 /* SCP HEADER physical address start (mapped to end of SCP ITCM) */
158 #define HOST_SCP_HDR_PHYS_BASE          (HOST_SCP_PHYS_BASE +               \
159                                          HOST_SCP_ATU_SIZE -                \
160                                          RSE_IMG_HDR_ATU_WINDOW_SIZE)
161 
162 /* MCP */
163 
164 /* MCP ATU HEADER logical address start */
165 #define HOST_MCP_HDR_ATU_WINDOW_BASE_S  (HOST_SCP_IMG_CODE_BASE_S +         \
166                                          HOST_SCP_ATU_SIZE)
167 /* MCP Image address start, offset so end of HEADER is at end of ATU HEADER */
168 #define HOST_MCP_IMG_HDR_BASE_S         (HOST_MCP_HDR_ATU_WINDOW_BASE_S +   \
169                                          RSE_IMG_HDR_ATU_WINDOW_SIZE -      \
170                                          BL2_HEADER_SIZE)
171 /* MCP Code region and MCP ATU CODE logical address start */
172 #define HOST_MCP_IMG_CODE_BASE_S        (HOST_MCP_HDR_ATU_WINDOW_BASE_S +   \
173                                          RSE_IMG_HDR_ATU_WINDOW_SIZE)
174 /* MCP ATU CODE size (aligned size of MCP image) */
175 #define HOST_MCP_ATU_SIZE               ALIGN_UP(SIZE_DEF_MCP_IMAGE,        \
176                                                  RSE_ATU_PAGE_SIZE)
177 /* MCP HEADER physical address start (mapped to end of MCP ITCM) */
178 #define HOST_MCP_HDR_PHYS_BASE          (HOST_MCP_PHYS_BASE +               \
179                                          HOST_MCP_ATU_SIZE -                \
180                                          RSE_IMG_HDR_ATU_WINDOW_SIZE)
181 
182 /* LCP */
183 
184 /*
185  * All LCP use the same ATU region but map it to a different physical address
186  * The physical address is the cluster utility space in SCP which has an
187  * address translation window for each LCP.
188  */
189 
190 /* LCP ATU HEADER logical address start */
191 #define HOST_LCP_HDR_ATU_WINDOW_BASE_S  (HOST_MCP_IMG_CODE_BASE_S +         \
192                                          HOST_MCP_ATU_SIZE)
193 /* LCP Image address start, offset so end of HEADER is at end of ATU HEADER */
194 #define HOST_LCP_IMG_HDR_BASE_S         (HOST_LCP_HDR_ATU_WINDOW_BASE_S +   \
195                                          RSE_IMG_HDR_ATU_WINDOW_SIZE -      \
196                                          BL2_HEADER_SIZE)
197 /* LCP Code region and LCP ATU CODE logical address start */
198 #define HOST_LCP_IMG_CODE_BASE_S        (HOST_LCP_HDR_ATU_WINDOW_BASE_S +   \
199                                          RSE_IMG_HDR_ATU_WINDOW_SIZE)
200 /* LCP0 ITCM window in AP address start */
201 #define HOST_LCP_0_PHYS_BASE            (HOST_CLUST_UTIL_PHYS_BASE + 0x50000ULL)
202 /* Offset between LCP ITCM windows */
203 #define HOST_LCP_N_PHYS_OFFSET          0x200000U /* 2 MB */
204 /* LCP ITCM window in AP address start for LCP N */
205 #define HOST_LCP_N_PHYS_BASE(n)         (HOST_LCP_0_PHYS_BASE +             \
206                                          HOST_LCP_N_PHYS_OFFSET * (n))
207 /* LCP ATU CODE size (aligned size of LCP image) */
208 #define HOST_LCP_ATU_SIZE               ALIGN_UP(SIZE_DEF_LCP_IMAGE,        \
209                                                  RSE_ATU_PAGE_SIZE)
210 /* LCP HEADER physical address start (mapped to end of LCP0 ITCM) */
211 #define HOST_LCP_0_PHYS_HDR_BASE        (HOST_LCP_0_PHYS_BASE +             \
212                                          HOST_LCP_ATU_SIZE -                \
213                                          RSE_IMG_HDR_ATU_WINDOW_SIZE)
214 
215 /* AP BL1 */
216 
217 /* AP BL1 ATU HEADER logical address start */
218 #define HOST_AP_BL1_HDR_ATU_WINDOW_BASE_S (HOST_LCP_IMG_CODE_BASE_S +          \
219                                            HOST_LCP_ATU_SIZE)
220 /* AP BL1 Image address start, offset so end of HEADER at end of ATU HEADER */
221 #define HOST_AP_BL1_IMG_HDR_BASE_S        (HOST_AP_BL1_HDR_ATU_WINDOW_BASE_S + \
222                                            RSE_IMG_HDR_ATU_WINDOW_SIZE -       \
223                                            BL2_HEADER_SIZE)
224 /* AP BL1 Code region and SCP ATU CODE logical address start */
225 #define HOST_AP_BL1_IMG_CODE_BASE_S       (HOST_AP_BL1_HDR_ATU_WINDOW_BASE_S + \
226                                            RSE_IMG_HDR_ATU_WINDOW_SIZE)
227 /* AP BL1 Shared SRAM physical address start */
228 #define HOST_AP_BL1_PHYS_BASE             HOST_AP_SHARED_SRAM_PHYS_BASE
229 /* AP BL1 ATU CODE size (aligned size of SCP image) */
230 #define HOST_AP_BL1_ATU_SIZE              ALIGN_UP(SIZE_DEF_AP_BL1_IMAGE,      \
231                                                    RSE_ATU_PAGE_SIZE)
232 /* AP BL1 HEADER physical address start (mapped to end of AP ITCM) */
233 #define HOST_AP_BL1_HDR_PHYS_BASE         (HOST_AP_BL1_PHYS_BASE +             \
234                                            HOST_AP_BL1_ATU_SIZE -              \
235                                            RSE_IMG_HDR_ATU_WINDOW_SIZE)
236 
237 /* Last RSE logical address used for loading images */
238 #define RSE_IMAGE_LOADING_END             (HOST_AP_BL1_IMG_CODE_BASE_S +       \
239                                            HOST_AP_BL1_ATU_SIZE)
240 
241 /* SCP sysctrl region logical address start */
242 #define HOST_SCP_INIT_CTRL_BASE_S    RSE_IMAGE_LOADING_END
243 /* SCP sysctrl region physical address start */
244 #define HOST_SCP_INIT_CTRL_PHYS_BASE 0x1000050050000ULL
245 /* SCP sysctrl region ATU size */
246 #define HOST_SCP_INIT_CTRL_SIZE      ALIGN_UP(0x1000U, RSE_ATU_PAGE_SIZE)
247 /* SCP sysctrl region ATU id */
248 #define HOST_SCP_INIT_CTRL_ATU_ID    RSE_ATU_FW_INIT_ID
249 
250 /* MCP sysctrl region logical address start */
251 #define HOST_MCP_INIT_CTRL_BASE_S    RSE_IMAGE_LOADING_END
252 /* MCP sysctrl region physical address start */
253 #define HOST_MCP_INIT_CTRL_PHYS_BASE 0x2000050020000ULL
254 /* MCP sysctrl region ATU size */
255 #define HOST_MCP_INIT_CTRL_SIZE      ALIGN_UP(0x1000U, RSE_ATU_PAGE_SIZE)
256 /* MCP sysctrl region ATU id */
257 #define HOST_MCP_INIT_CTRL_ATU_ID    RSE_ATU_FW_INIT_ID
258 
259 /*
260  * ATU region mapping to access System Control NI-Tower and Peripheral
261  * NI-Tower
262  */
263 #define HOST_NI_TOWER_BASE      (HOST_MCP_INIT_CTRL_BASE_S +                \
264                                  HOST_MCP_INIT_CTRL_SIZE)
265 #define HOST_NI_TOWER_SIZE      ALIGN_UP(0x1000000U, RSE_ATU_PAGE_SIZE)
266 #define HOST_NI_TOWER_ATU_ID    RSE_ATU_NI_TOWER_ID
267 
268 /* ATU region mapping to access SYSCTRL SMMU */
269 #define HOST_SYSCTRL_SMMU_BASE      (HOST_NI_TOWER_BASE + HOST_NI_TOWER_SIZE)
270 #define HOST_SYSCTRL_SMMU_SIZE      0x100000U
271 #define HOST_SYSCTRL_SMMU_ATU_ID    RSE_ATU_SYSCTRL_SMMU_ID
272 
273 #endif  /* __HOST_ATU_BASE_ADDRESS_H__ */
274