blob: db2bad83337835a520ca24d9acfb22b7e3c99a1c [file] [log] [blame] [view]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001ARM Trusted Firmware Porting Guide
2==================================
3
4Contents
5--------
6
71. Introduction
82. Common Modifications
9 * Common mandatory modifications
Vikram Kanigirie452cd82014-05-23 15:56:12 +010010 * Handling reset
Achin Gupta4f6ad662013-10-25 09:08:21 +010011 * Common optional modifications
123. Boot Loader stage specific modifications
13 * Boot Loader stage 1 (BL1)
14 * Boot Loader stage 2 (BL2)
15 * Boot Loader stage 3-1 (BL3-1)
16 * PSCI implementation (in BL3-1)
Achin Guptaa4fa3cb2014-06-02 22:27:36 +010017 * Interrupt Management framework (in BL3-1)
Soby Mathewc67b09b2014-07-14 16:57:23 +010018 * Crash Reporting mechanism (in BL3-1)
Harry Liebeld265bd72014-01-31 19:04:10 +0000194. C Library
205. Storage abstraction layer
Achin Gupta4f6ad662013-10-25 09:08:21 +010021
22- - - - - - - - - - - - - - - - - -
23
241. Introduction
25----------------
26
27Porting the ARM Trusted Firmware to a new platform involves making some
28mandatory and optional modifications for both the cold and warm boot paths.
29Modifications consist of:
30
31* Implementing a platform-specific function or variable,
32* Setting up the execution context in a certain way, or
33* Defining certain constants (for example #defines).
34
Dan Handleyb68954c2014-05-29 12:30:24 +010035The platform-specific functions and variables are all declared in
36[include/plat/common/platform.h]. The firmware provides a default implementation
37of variables and functions to fulfill the optional requirements. These
38implementations are all weakly defined; they are provided to ease the porting
39effort. Each platform port can override them with its own implementation if the
40default implementation is inadequate.
Achin Gupta4f6ad662013-10-25 09:08:21 +010041
42Some modifications are common to all Boot Loader (BL) stages. Section 2
43discusses these in detail. The subsequent sections discuss the remaining
44modifications for each BL stage in detail.
45
46This document should be read in conjunction with the ARM Trusted Firmware
47[User Guide].
48
49
502. Common modifications
51------------------------
52
53This section covers the modifications that should be made by the platform for
54each BL stage to correctly port the firmware stack. They are categorized as
55either mandatory or optional.
56
57
582.1 Common mandatory modifications
59----------------------------------
60A platform port must enable the Memory Management Unit (MMU) with identity
61mapped page tables, and enable both the instruction and data caches for each BL
62stage. In the ARM FVP port, each BL stage configures the MMU in its platform-
63specific architecture setup function, for example `blX_plat_arch_setup()`.
64
65Each platform must allocate a block of identity mapped secure memory with
66Device-nGnRE attributes aligned to page boundary (4K) for each BL stage. This
67memory is identified by the section name `tzfw_coherent_mem` so that its
68possible for the firmware to place variables in it using the following C code
69directive:
70
71 __attribute__ ((section("tzfw_coherent_mem")))
72
73Or alternatively the following assembler code directive:
74
75 .section tzfw_coherent_mem
76
77The `tzfw_coherent_mem` section is used to allocate any data structures that are
78accessed both when a CPU is executing with its MMU and caches enabled, and when
79it's running with its MMU and caches disabled. Examples are given below.
80
81The following variables, functions and constants must be defined by the platform
82for the firmware to work correctly.
83
84
Dan Handleyb68954c2014-05-29 12:30:24 +010085### File : platform_def.h [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +010086
Dan Handleyb68954c2014-05-29 12:30:24 +010087Each platform must ensure that a header file of this name is in the system
88include path with the following constants defined. This may require updating the
89list of `PLAT_INCLUDES` in the `platform.mk` file. In the ARM FVP port, this
90file is found in [plat/fvp/include/platform_def.h].
Achin Gupta4f6ad662013-10-25 09:08:21 +010091
James Morrisseyba3155b2013-10-29 10:56:46 +000092* **#define : PLATFORM_LINKER_FORMAT**
Achin Gupta4f6ad662013-10-25 09:08:21 +010093
94 Defines the linker format used by the platform, for example
95 `elf64-littleaarch64` used by the FVP.
96
James Morrisseyba3155b2013-10-29 10:56:46 +000097* **#define : PLATFORM_LINKER_ARCH**
Achin Gupta4f6ad662013-10-25 09:08:21 +010098
99 Defines the processor architecture for the linker by the platform, for
100 example `aarch64` used by the FVP.
101
James Morrisseyba3155b2013-10-29 10:56:46 +0000102* **#define : PLATFORM_STACK_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103
104 Defines the normal stack memory available to each CPU. This constant is used
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000105 by [plat/common/aarch64/platform_mp_stack.S] and
106 [plat/common/aarch64/platform_up_stack.S].
107
James Morrisseyba3155b2013-10-29 10:56:46 +0000108* **#define : FIRMWARE_WELCOME_STR**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100109
110 Defines the character string printed by BL1 upon entry into the `bl1_main()`
111 function.
112
James Morrisseyba3155b2013-10-29 10:56:46 +0000113* **#define : BL2_IMAGE_NAME**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100114
115 Name of the BL2 binary image on the host file-system. This name is used by
Harry Liebeld265bd72014-01-31 19:04:10 +0000116 BL1 to load BL2 into secure memory from non-volatile storage.
117
118* **#define : BL31_IMAGE_NAME**
119
120 Name of the BL3-1 binary image on the host file-system. This name is used by
121 BL2 to load BL3-1 into secure memory from platform storage.
122
123* **#define : BL33_IMAGE_NAME**
124
125 Name of the BL3-3 binary image on the host file-system. This name is used by
126 BL2 to load BL3-3 into non-secure memory from platform storage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100127
James Morrisseyba3155b2013-10-29 10:56:46 +0000128* **#define : PLATFORM_CACHE_LINE_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129
130 Defines the size (in bytes) of the largest cache line across all the cache
131 levels in the platform.
132
James Morrisseyba3155b2013-10-29 10:56:46 +0000133* **#define : PLATFORM_CLUSTER_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100134
135 Defines the total number of clusters implemented by the platform in the
136 system.
137
James Morrisseyba3155b2013-10-29 10:56:46 +0000138* **#define : PLATFORM_CORE_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100139
140 Defines the total number of CPUs implemented by the platform across all
141 clusters in the system.
142
James Morrisseyba3155b2013-10-29 10:56:46 +0000143* **#define : PLATFORM_MAX_CPUS_PER_CLUSTER**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100144
145 Defines the maximum number of CPUs that can be implemented within a cluster
146 on the platform.
147
Andrew Thoelke6c0b45d2014-06-20 00:36:14 +0100148* **#define : PLATFORM_NUM_AFFS**
149
150 Defines the total number of nodes in the affinity heirarchy at all affinity
151 levels used by the platform.
152
James Morrisseyba3155b2013-10-29 10:56:46 +0000153* **#define : TZROM_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100154
155 Defines the base address of secure ROM on the platform, where the BL1 binary
156 is loaded. This constant is used by the linker scripts to ensure that the
157 BL1 image fits into the available memory.
158
James Morrisseyba3155b2013-10-29 10:56:46 +0000159* **#define : TZROM_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100160
161 Defines the size of secure ROM on the platform. This constant is used by the
162 linker scripts to ensure that the BL1 image fits into the available memory.
163
James Morrisseyba3155b2013-10-29 10:56:46 +0000164* **#define : TZRAM_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100165
166 Defines the base address of the secure RAM on platform, where the data
167 section of the BL1 binary is loaded. The BL2 and BL3-1 images are also
168 loaded in this secure RAM region. This constant is used by the linker
169 scripts to ensure that the BL1 data section and BL2/BL3-1 binary images fit
170 into the available memory.
171
James Morrisseyba3155b2013-10-29 10:56:46 +0000172* **#define : TZRAM_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100173
174 Defines the size of the secure RAM on the platform. This constant is used by
175 the linker scripts to ensure that the BL1 data section and BL2/BL3-1 binary
176 images fit into the available memory.
177
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100178* **#define : BL1_RO_BASE**
179
180 Defines the base address in secure ROM where BL1 originally lives. Must be
181 aligned on a page-size boundary.
182
183* **#define : BL1_RO_LIMIT**
184
185 Defines the maximum address in secure ROM that BL1's actual content (i.e.
186 excluding any data section allocated at runtime) can occupy.
187
188* **#define : BL1_RW_BASE**
189
190 Defines the base address in secure RAM where BL1's read-write data will live
191 at runtime. Must be aligned on a page-size boundary.
192
193* **#define : BL1_RW_LIMIT**
194
195 Defines the maximum address in secure RAM that BL1's read-write data can
196 occupy at runtime.
197
James Morrisseyba3155b2013-10-29 10:56:46 +0000198* **#define : BL2_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100199
200 Defines the base address in secure RAM where BL1 loads the BL2 binary image.
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000201 Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100202
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100203* **#define : BL2_LIMIT**
204
205 Defines the maximum address in secure RAM that the BL2 image can occupy.
206
James Morrisseyba3155b2013-10-29 10:56:46 +0000207* **#define : BL31_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100208
209 Defines the base address in secure RAM where BL2 loads the BL3-1 binary
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000210 image. Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100211
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100212* **#define : BL31_LIMIT**
213
214 Defines the maximum address in secure RAM that the BL3-1 image can occupy.
215
Harry Liebeld265bd72014-01-31 19:04:10 +0000216* **#define : NS_IMAGE_OFFSET**
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100217
Harry Liebeld265bd72014-01-31 19:04:10 +0000218 Defines the base address in non-secure DRAM where BL2 loads the BL3-3 binary
219 image. Must be aligned on a page-size boundary.
220
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100221If the BL3-2 image is supported by the platform, the following constants must
222be defined as well:
223
224* **#define : TSP_SEC_MEM_BASE**
225
226 Defines the base address of the secure memory used by the BL3-2 image on the
227 platform.
228
229* **#define : TSP_SEC_MEM_SIZE**
230
231 Defines the size of the secure memory used by the BL3-2 image on the
232 platform.
233
234* **#define : BL32_BASE**
235
236 Defines the base address in secure memory where BL2 loads the BL3-2 binary
237 image. Must be inside the secure memory identified by `TSP_SEC_MEM_BASE` and
238 `TSP_SEC_MEM_SIZE` constants. Must also be aligned on a page-size boundary.
239
240* **#define : BL32_LIMIT**
241
242 Defines the maximum address that the BL3-2 image can occupy. Must be inside
243 the secure memory identified by `TSP_SEC_MEM_BASE` and `TSP_SEC_MEM_SIZE`
244 constants.
245
Sandrine Bailleux46d49f632014-06-23 17:00:23 +0100246The following constants are optional. They should be defined when the platform
247memory layout implies some image overlaying like on FVP.
248
249* **#define : BL31_PROGBITS_LIMIT**
250
251 Defines the maximum address in secure RAM that the BL3-1's progbits sections
252 can occupy.
253
254* **#define : BL32_PROGBITS_LIMIT**
255
256 Defines the maximum address that the TSP's progbits sections can occupy.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100257
Dan Handleyb68954c2014-05-29 12:30:24 +0100258### File : plat_macros.S [mandatory]
Soby Mathewa43d4312014-04-07 15:28:55 +0100259
Dan Handleyb68954c2014-05-29 12:30:24 +0100260Each platform must ensure a file of this name is in the system include path with
261the following macro defined. In the ARM FVP port, this file is found in
262[plat/fvp/include/plat_macros.S].
Soby Mathewa43d4312014-04-07 15:28:55 +0100263
264* **Macro : plat_print_gic_regs**
265
266 This macro allows the crash reporting routine to print GIC registers
Soby Mathew8c106902014-07-16 09:23:52 +0100267 in case of an unhandled exception in BL3-1. This aids in debugging and
Soby Mathewa43d4312014-04-07 15:28:55 +0100268 this macro can be defined to be empty in case GIC register reporting is
269 not desired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100270
Soby Mathew8c106902014-07-16 09:23:52 +0100271* **Macro : plat_print_interconnect_regs**
272
273 This macro allows the crash reporting routine to print interconnect registers
274 in case of an unhandled exception in BL3-1. This aids in debugging and
275 this macro can be defined to be empty in case interconnect register reporting
276 is not desired. In the ARM FVP port, the CCI snoop control registers are
277 reported.
278
Achin Gupta4f6ad662013-10-25 09:08:21 +0100279### Other mandatory modifications
280
James Morrisseyba3155b2013-10-29 10:56:46 +0000281The following mandatory modifications may be implemented in any file
Achin Gupta4f6ad662013-10-25 09:08:21 +0100282the implementer chooses. In the ARM FVP port, they are implemented in
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000283[plat/fvp/aarch64/plat_common.c].
Achin Gupta4f6ad662013-10-25 09:08:21 +0100284
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100285* **Function : uint64_t plat_get_syscnt_freq(void)**
286
287 This function is used by the architecture setup code to retrieve the
288 counter frequency for the CPU's generic timer. This value will be
289 programmed into the `CNTFRQ_EL0` register.
290 In the ARM FVP port, it returns the base frequency of the system counter,
291 which is retrieved from the first entry in the frequency modes table.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100292
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000293
Vikram Kanigirie452cd82014-05-23 15:56:12 +01002942.2 Handling Reset
295------------------
296
297BL1 by default implements the reset vector where execution starts from a cold
298or warm boot. BL3-1 can be optionally set as a reset vector using the
299RESET_TO_BL31 make variable.
300
301For each CPU, the reset vector code is responsible for the following tasks:
302
3031. Distinguishing between a cold boot and a warm boot.
304
3052. In the case of a cold boot and the CPU being a secondary CPU, ensuring that
306 the CPU is placed in a platform-specific state until the primary CPU
307 performs the necessary steps to remove it from this state.
308
3093. In the case of a warm boot, ensuring that the CPU jumps to a platform-
310 specific address in the BL3-1 image in the same processor mode as it was
311 when released from reset.
312
313The following functions need to be implemented by the platform port to enable
314reset vector code to perform the above tasks.
315
316
317### Function : platform_get_entrypoint() [mandatory]
318
319 Argument : unsigned long
320 Return : unsigned int
321
322This function is called with the `SCTLR.M` and `SCTLR.C` bits disabled. The CPU
323is identified by its `MPIDR`, which is passed as the argument. The function is
324responsible for distinguishing between a warm and cold reset using platform-
325specific means. If it's a warm reset then it returns the entrypoint into the
326BL3-1 image that the CPU must jump to. If it's a cold reset then this function
327must return zero.
328
329This function is also responsible for implementing a platform-specific mechanism
330to handle the condition where the CPU has been warm reset but there is no
331entrypoint to jump to.
332
333This function does not follow the Procedure Call Standard used by the
334Application Binary Interface for the ARM 64-bit architecture. The caller should
335not assume that callee saved registers are preserved across a call to this
336function.
337
338This function fulfills requirement 1 and 3 listed above.
339
340
341### Function : plat_secondary_cold_boot_setup() [mandatory]
342
343 Argument : void
344 Return : void
345
346This function is called with the MMU and data caches disabled. It is responsible
347for placing the executing secondary CPU in a platform-specific state until the
348primary CPU performs the necessary actions to bring it out of that state and
349allow entry into the OS.
350
351In the ARM FVP port, each secondary CPU powers itself off. The primary CPU is
352responsible for powering up the secondary CPU when normal world software
353requires them.
354
355This function fulfills requirement 2 above.
356
357
Juan Castillo53fdceb2014-07-16 15:53:43 +0100358### Function : platform_is_primary_cpu() [mandatory]
359
360 Argument : unsigned long
361 Return : unsigned int
362
363This function identifies a CPU by its `MPIDR`, which is passed as the argument,
364to determine whether this CPU is the primary CPU or a secondary CPU. A return
365value of zero indicates that the CPU is not the primary CPU, while a non-zero
366return value indicates that the CPU is the primary CPU.
367
368
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100369### Function : platform_mem_init() [mandatory]
370
371 Argument : void
372 Return : void
373
374This function is called before any access to data is made by the firmware, in
375order to carry out any essential memory initialization.
376
377The ARM FVP port uses this function to initialize the mailbox memory used for
378providing the warm-boot entry-point addresses.
379
380
381
3822.3 Common optional modifications
Achin Gupta4f6ad662013-10-25 09:08:21 +0100383---------------------------------
384
385The following are helper functions implemented by the firmware that perform
386common platform-specific tasks. A platform may choose to override these
387definitions.
388
389
390### Function : platform_get_core_pos()
391
392 Argument : unsigned long
393 Return : int
394
395A platform may need to convert the `MPIDR` of a CPU to an absolute number, which
396can be used as a CPU-specific linear index into blocks of memory (for example
397while allocating per-CPU stacks). This routine contains a simple mechanism
398to perform this conversion, using the assumption that each cluster contains a
399maximum of 4 CPUs:
400
401 linear index = cpu_id + (cluster_id * 4)
402
403 cpu_id = 8-bit value in MPIDR at affinity level 0
404 cluster_id = 8-bit value in MPIDR at affinity level 1
405
406
Achin Gupta4f6ad662013-10-25 09:08:21 +0100407### Function : platform_set_stack()
408
409 Argument : unsigned long
410 Return : void
411
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000412This function sets the current stack pointer to the normal memory stack that
413has been allocated for the CPU specificed by MPIDR. For BL images that only
414require a stack for the primary CPU the parameter is ignored. The size of
415the stack allocated to each CPU is specified by the platform defined constant
416`PLATFORM_STACK_SIZE`.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100417
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000418Common implementations of this function for the UP and MP BL images are
419provided in [plat/common/aarch64/platform_up_stack.S] and
420[plat/common/aarch64/platform_mp_stack.S]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100421
422
Achin Guptac8afc782013-11-25 18:45:02 +0000423### Function : platform_get_stack()
424
425 Argument : unsigned long
426 Return : unsigned long
427
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000428This function returns the base address of the normal memory stack that
429has been allocated for the CPU specificed by MPIDR. For BL images that only
430require a stack for the primary CPU the parameter is ignored. The size of
431the stack allocated to each CPU is specified by the platform defined constant
432`PLATFORM_STACK_SIZE`.
Achin Guptac8afc782013-11-25 18:45:02 +0000433
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000434Common implementations of this function for the UP and MP BL images are
435provided in [plat/common/aarch64/platform_up_stack.S] and
436[plat/common/aarch64/platform_mp_stack.S]
Achin Guptac8afc782013-11-25 18:45:02 +0000437
438
Achin Gupta4f6ad662013-10-25 09:08:21 +0100439### Function : plat_report_exception()
440
441 Argument : unsigned int
442 Return : void
443
444A platform may need to report various information about its status when an
445exception is taken, for example the current exception level, the CPU security
446state (secure/non-secure), the exception type, and so on. This function is
447called in the following circumstances:
448
449* In BL1, whenever an exception is taken.
450* In BL2, whenever an exception is taken.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100451
452The default implementation doesn't do anything, to avoid making assumptions
453about the way the platform displays its status information.
454
455This function receives the exception type as its argument. Possible values for
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000456exceptions types are listed in the [include/runtime_svc.h] header file. Note
Achin Gupta4f6ad662013-10-25 09:08:21 +0100457that these constants are not related to any architectural exception code; they
458are just an ARM Trusted Firmware convention.
459
460
4613. Modifications specific to a Boot Loader stage
462-------------------------------------------------
463
4643.1 Boot Loader Stage 1 (BL1)
465-----------------------------
466
467BL1 implements the reset vector where execution starts from after a cold or
468warm boot. For each CPU, BL1 is responsible for the following tasks:
469
Vikram Kanigirie452cd82014-05-23 15:56:12 +01004701. Handling the reset as described in section 2.2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100471
4722. In the case of a cold boot and the CPU being the primary CPU, ensuring that
473 only this CPU executes the remaining BL1 code, including loading and passing
474 control to the BL2 stage.
475
Vikram Kanigirie452cd82014-05-23 15:56:12 +01004763. Loading the BL2 image from non-volatile storage into secure memory at the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100477 address specified by the platform defined constant `BL2_BASE`.
478
Vikram Kanigirie452cd82014-05-23 15:56:12 +01004794. Populating a `meminfo` structure with the following information in memory,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100480 accessible by BL2 immediately upon entry.
481
482 meminfo.total_base = Base address of secure RAM visible to BL2
483 meminfo.total_size = Size of secure RAM visible to BL2
484 meminfo.free_base = Base address of secure RAM available for
485 allocation to BL2
486 meminfo.free_size = Size of secure RAM available for allocation to BL2
487
488 BL1 places this `meminfo` structure at the beginning of the free memory
489 available for its use. Since BL1 cannot allocate memory dynamically at the
490 moment, its free memory will be available for BL2's use as-is. However, this
491 means that BL2 must read the `meminfo` structure before it starts using its
492 free memory (this is discussed in Section 3.2).
493
494 In future releases of the ARM Trusted Firmware it will be possible for
495 the platform to decide where it wants to place the `meminfo` structure for
496 BL2.
497
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100498 BL1 implements the `bl1_init_bl2_mem_layout()` function to populate the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100499 BL2 `meminfo` structure. The platform may override this implementation, for
500 example if the platform wants to restrict the amount of memory visible to
501 BL2. Details of how to do this are given below.
502
503The following functions need to be implemented by the platform port to enable
504BL1 to perform the above tasks.
505
506
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100507### Function : bl1_plat_arch_setup() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100508
509 Argument : void
510 Return : void
511
Achin Gupta4f6ad662013-10-25 09:08:21 +0100512This function performs any platform-specific and architectural setup that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100513platform requires. Platform-specific setup might include configuration of
514memory controllers, configuration of the interconnect to allow the cluster
515to service cache snoop requests from another cluster, and so on.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100516
517In the ARM FVP port, this function enables CCI snoops into the cluster that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100518primary CPU is part of. It also enables the MMU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100519
520This function helps fulfill requirement 2 above.
521
522
523### Function : bl1_platform_setup() [mandatory]
524
525 Argument : void
526 Return : void
527
528This function executes with the MMU and data caches enabled. It is responsible
529for performing any remaining platform-specific setup that can occur after the
530MMU and data cache have been enabled.
531
Harry Liebeld265bd72014-01-31 19:04:10 +0000532This function is also responsible for initializing the storage abstraction layer
533which is used to load further bootloader images.
534
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100535This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100536
537
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000538### Function : bl1_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100539
540 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000541 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100542
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000543This function should only be called on the cold boot path. It executes with the
544MMU and data caches enabled. The pointer returned by this function must point to
545a `meminfo` structure containing the extents and availability of secure RAM for
546the BL1 stage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100547
548 meminfo.total_base = Base address of secure RAM visible to BL1
549 meminfo.total_size = Size of secure RAM visible to BL1
550 meminfo.free_base = Base address of secure RAM available for allocation
551 to BL1
552 meminfo.free_size = Size of secure RAM available for allocation to BL1
553
554This information is used by BL1 to load the BL2 image in secure RAM. BL1 also
555populates a similar structure to tell BL2 the extents of memory available for
556its own use.
557
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100558This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100559
560
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100561### Function : bl1_init_bl2_mem_layout() [optional]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100562
563 Argument : meminfo *, meminfo *, unsigned int, unsigned long
564 Return : void
565
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100566BL1 needs to tell the next stage the amount of secure RAM available
567for it to use. This information is populated in a `meminfo`
Achin Gupta4f6ad662013-10-25 09:08:21 +0100568structure.
569
570Depending upon where BL2 has been loaded in secure RAM (determined by
571`BL2_BASE`), BL1 calculates the amount of free memory available for BL2 to use.
572BL1 also ensures that its data sections resident in secure RAM are not visible
573to BL2. An illustration of how this is done in the ARM FVP port is given in the
574[User Guide], in the Section "Memory layout on Base FVP".
575
576
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100577### Function : bl1_plat_set_bl2_ep_info() [mandatory]
578
579 Argument : image_info *, entry_point_info *
580 Return : void
581
582This function is called after loading BL2 image and it can be used to overwrite
583the entry point set by loader and also set the security state and SPSR which
584represents the entry point system state for BL2.
585
586On FVP, we are setting the security state and the SPSR for the BL2 entrypoint
587
588
Achin Gupta4f6ad662013-10-25 09:08:21 +01005893.2 Boot Loader Stage 2 (BL2)
590-----------------------------
591
592The BL2 stage is executed only by the primary CPU, which is determined in BL1
593using the `platform_is_primary_cpu()` function. BL1 passed control to BL2 at
594`BL2_BASE`. BL2 executes in Secure EL1 and is responsible for:
595
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01005961. (Optional) Loading the BL3-0 binary image (if present) from platform
597 provided non-volatile storage. To load the BL3-0 image, BL2 makes use of
598 the `meminfo` returned by the `bl2_plat_get_bl30_meminfo()` function.
599 The platform also defines the address in memory where BL3-0 is loaded
600 through the optional constant `BL30_BASE`. BL2 uses this information
601 to determine if there is enough memory to load the BL3-0 image.
602 Subsequent handling of the BL3-0 image is platform-specific and is
603 implemented in the `bl2_plat_handle_bl30()` function.
604 If `BL30_BASE` is not defined then this step is not performed.
605
6062. Loading the BL3-1 binary image into secure RAM from non-volatile storage. To
Harry Liebeld265bd72014-01-31 19:04:10 +0000607 load the BL3-1 image, BL2 makes use of the `meminfo` structure passed to it
608 by BL1. This structure allows BL2 to calculate how much secure RAM is
609 available for its use. The platform also defines the address in secure RAM
610 where BL3-1 is loaded through the constant `BL31_BASE`. BL2 uses this
611 information to determine if there is enough memory to load the BL3-1 image.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100612
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006133. (Optional) Loading the BL3-2 binary image (if present) from platform
Dan Handley1151c822014-04-15 11:38:38 +0100614 provided non-volatile storage. To load the BL3-2 image, BL2 makes use of
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100615 the `meminfo` returned by the `bl2_plat_get_bl32_meminfo()` function.
616 The platform also defines the address in memory where BL3-2 is loaded
617 through the optional constant `BL32_BASE`. BL2 uses this information
618 to determine if there is enough memory to load the BL3-2 image.
619 If `BL32_BASE` is not defined then this and the next step is not performed.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000620
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006214. (Optional) Arranging to pass control to the BL3-2 image (if present) that
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100622 has been pre-loaded at `BL32_BASE`. BL2 populates an `entry_point_info`
Dan Handley1151c822014-04-15 11:38:38 +0100623 structure in memory provided by the platform with information about how
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100624 BL3-1 should pass control to the BL3-2 image.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000625
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006265. Loading the normal world BL3-3 binary image into non-secure DRAM from
627 platform storage and arranging for BL3-1 to pass control to this image. This
628 address is determined using the `plat_get_ns_image_entrypoint()` function
629 described below.
630
6316. BL2 populates an `entry_point_info` structure in memory provided by the
632 platform with information about how BL3-1 should pass control to the
633 other BL images.
634
Achin Gupta4f6ad662013-10-25 09:08:21 +0100635The following functions must be implemented by the platform port to enable BL2
636to perform the above tasks.
637
638
639### Function : bl2_early_platform_setup() [mandatory]
640
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100641 Argument : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100642 Return : void
643
644This function executes with the MMU and data caches disabled. It is only called
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100645by the primary CPU. The arguments to this function is the address of the
646`meminfo` structure populated by BL1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100647
648The platform must copy the contents of the `meminfo` structure into a private
649variable as the original memory may be subsequently overwritten by BL2. The
650copied structure is made available to all BL2 code through the
Achin Guptae4d084e2014-02-19 17:18:23 +0000651`bl2_plat_sec_mem_layout()` function.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100652
653
654### Function : bl2_plat_arch_setup() [mandatory]
655
656 Argument : void
657 Return : void
658
659This function executes with the MMU and data caches disabled. It is only called
660by the primary CPU.
661
662The purpose of this function is to perform any architectural initialization
663that varies across platforms, for example enabling the MMU (since the memory
664map differs across platforms).
665
666
667### Function : bl2_platform_setup() [mandatory]
668
669 Argument : void
670 Return : void
671
672This function may execute with the MMU and data caches enabled if the platform
673port does the necessary initialization in `bl2_plat_arch_setup()`. It is only
674called by the primary CPU.
675
Achin Guptae4d084e2014-02-19 17:18:23 +0000676The purpose of this function is to perform any platform initialization
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100677specific to BL2. Platform security components are configured if required.
678For the Base FVP the TZC-400 TrustZone controller is configured to only
679grant non-secure access to DRAM. This avoids aliasing between secure and
680non-secure accesses in the TLB and cache - secure execution states can use
681the NS attributes in the MMU translation tables to access the DRAM.
Harry Liebelce19cf12014-04-01 19:28:07 +0100682
Harry Liebeld265bd72014-01-31 19:04:10 +0000683This function is also responsible for initializing the storage abstraction layer
684which is used to load further bootloader images.
685
Achin Gupta4f6ad662013-10-25 09:08:21 +0100686
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000687### Function : bl2_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100688
689 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000690 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100691
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000692This function should only be called on the cold boot path. It may execute with
693the MMU and data caches enabled if the platform port does the necessary
694initialization in `bl2_plat_arch_setup()`. It is only called by the primary CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100695
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000696The purpose of this function is to return a pointer to a `meminfo` structure
697populated with the extents of secure RAM available for BL2 to use. See
Achin Gupta4f6ad662013-10-25 09:08:21 +0100698`bl2_early_platform_setup()` above.
699
700
Sandrine Bailleux93d81d62014-06-24 14:19:36 +0100701### Function : bl2_plat_get_bl30_meminfo() [mandatory]
702
703 Argument : meminfo *
704 Return : void
705
706This function is used to get the memory limits where BL2 can load the
707BL3-0 image. The meminfo provided by this is used by load_image() to
708validate whether the BL3-0 image can be loaded within the given
709memory from the given base.
710
711
712### Function : bl2_plat_handle_bl30() [mandatory]
713
714 Argument : image_info *
715 Return : int
716
717This function is called after loading BL3-0 image and it is used to perform any
718platform-specific actions required to handle the SCP firmware. Typically it
719transfers the image into SCP memory using a platform-specific protocol and waits
720until SCP executes it and signals to the Application Processor (AP) for BL2
721execution to continue.
722
723This function returns 0 on success, a negative error code otherwise.
724
725
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100726### Function : bl2_plat_get_bl31_params() [mandatory]
Harry Liebeld265bd72014-01-31 19:04:10 +0000727
728 Argument : void
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100729 Return : bl31_params *
Harry Liebeld265bd72014-01-31 19:04:10 +0000730
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100731BL2 platform code needs to return a pointer to a `bl31_params` structure it
732will use for passing information to BL3-1. The `bl31_params` structure carries
733the following information.
734 - Header describing the version information for interpreting the bl31_param
735 structure
736 - Information about executing the BL3-3 image in the `bl33_ep_info` field
737 - Information about executing the BL3-2 image in the `bl32_ep_info` field
738 - Information about the type and extents of BL3-1 image in the
739 `bl31_image_info` field
740 - Information about the type and extents of BL3-2 image in the
741 `bl32_image_info` field
742 - Information about the type and extents of BL3-3 image in the
743 `bl33_image_info` field
744
745The memory pointed by this structure and its sub-structures should be
746accessible from BL3-1 initialisation code. BL3-1 might choose to copy the
747necessary content, or maintain the structures until BL3-3 is initialised.
Harry Liebeld265bd72014-01-31 19:04:10 +0000748
749
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100750### Funtion : bl2_plat_get_bl31_ep_info() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100751
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100752 Argument : void
753 Return : entry_point_info *
754
755BL2 platform code returns a pointer which is used to populate the entry point
756information for BL3-1 entry point. The location pointed by it should be
757accessible from BL1 while processing the synchronous exception to run to BL3-1.
758
759On FVP this is allocated inside an bl2_to_bl31_params_mem structure which
760is allocated at an address pointed by PARAMS_BASE.
761
762
763### Function : bl2_plat_set_bl31_ep_info() [mandatory]
764
765 Argument : image_info *, entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100766 Return : void
767
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100768This function is called after loading BL3-1 image and it can be used to
769overwrite the entry point set by loader and also set the security state
770and SPSR which represents the entry point system state for BL3-1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100771
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100772On FVP, we are setting the security state and the SPSR for the BL3-1
773entrypoint.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100774
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100775### Function : bl2_plat_set_bl32_ep_info() [mandatory]
776
777 Argument : image_info *, entry_point_info *
778 Return : void
779
780This function is called after loading BL3-2 image and it can be used to
781overwrite the entry point set by loader and also set the security state
782and SPSR which represents the entry point system state for BL3-2.
783
784On FVP, we are setting the security state and the SPSR for the BL3-2
785entrypoint
786
787### Function : bl2_plat_set_bl33_ep_info() [mandatory]
788
789 Argument : image_info *, entry_point_info *
790 Return : void
791
792This function is called after loading BL3-3 image and it can be used to
793overwrite the entry point set by loader and also set the security state
794and SPSR which represents the entry point system state for BL3-3.
795
796On FVP, we are setting the security state and the SPSR for the BL3-3
797entrypoint
798
799### Function : bl2_plat_get_bl32_meminfo() [mandatory]
800
801 Argument : meminfo *
802 Return : void
803
804This function is used to get the memory limits where BL2 can load the
805BL3-2 image. The meminfo provided by this is used by load_image() to
806validate whether the BL3-2 image can be loaded with in the given
807memory from the given base.
808
809### Function : bl2_plat_get_bl33_meminfo() [mandatory]
810
811 Argument : meminfo *
812 Return : void
813
814This function is used to get the memory limits where BL2 can load the
815BL3-3 image. The meminfo provided by this is used by load_image() to
816validate whether the BL3-3 image can be loaded with in the given
817memory from the given base.
818
819### Function : bl2_plat_flush_bl31_params() [mandatory]
820
821 Argument : void
822 Return : void
823
824Once BL2 has populated all the structures that needs to be read by BL1
825and BL3-1 including the bl31_params structures and its sub-structures,
826the bl31_ep_info structure and any platform specific data. It flushes
827all these data to the main memory so that it is available when we jump to
828later Bootloader stages with MMU off
Achin Gupta4f6ad662013-10-25 09:08:21 +0100829
830### Function : plat_get_ns_image_entrypoint() [mandatory]
831
832 Argument : void
833 Return : unsigned long
834
835As previously described, BL2 is responsible for arranging for control to be
836passed to a normal world BL image through BL3-1. This function returns the
837entrypoint of that image, which BL3-1 uses to jump to it.
838
Harry Liebeld265bd72014-01-31 19:04:10 +0000839BL2 is responsible for loading the normal world BL3-3 image (e.g. UEFI).
Achin Gupta4f6ad662013-10-25 09:08:21 +0100840
841
8423.2 Boot Loader Stage 3-1 (BL3-1)
843---------------------------------
844
845During cold boot, the BL3-1 stage is executed only by the primary CPU. This is
846determined in BL1 using the `platform_is_primary_cpu()` function. BL1 passes
847control to BL3-1 at `BL31_BASE`. During warm boot, BL3-1 is executed by all
848CPUs. BL3-1 executes at EL3 and is responsible for:
849
8501. Re-initializing all architectural and platform state. Although BL1 performs
851 some of this initialization, BL3-1 remains resident in EL3 and must ensure
852 that EL3 architectural and platform state is completely initialized. It
853 should make no assumptions about the system state when it receives control.
854
8552. Passing control to a normal world BL image, pre-loaded at a platform-
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100856 specific address by BL2. BL3-1 uses the `entry_point_info` structure that BL2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100857 populated in memory to do this.
858
8593. Providing runtime firmware services. Currently, BL3-1 only implements a
860 subset of the Power State Coordination Interface (PSCI) API as a runtime
861 service. See Section 3.3 below for details of porting the PSCI
862 implementation.
863
Achin Gupta35ca3512014-02-19 17:58:33 +00008644. Optionally passing control to the BL3-2 image, pre-loaded at a platform-
865 specific address by BL2. BL3-1 exports a set of apis that allow runtime
866 services to specify the security state in which the next image should be
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100867 executed and run the corresponding image. BL3-1 uses the `entry_point_info`
868 structure populated by BL2 to do this.
869
870If BL3-1 is a reset vector, It also needs to handle the reset as specified in
871section 2.2 before the tasks described above.
Achin Gupta35ca3512014-02-19 17:58:33 +0000872
Achin Gupta4f6ad662013-10-25 09:08:21 +0100873The following functions must be implemented by the platform port to enable BL3-1
874to perform the above tasks.
875
876
877### Function : bl31_early_platform_setup() [mandatory]
878
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100879 Argument : bl31_params *, void *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100880 Return : void
881
882This function executes with the MMU and data caches disabled. It is only called
883by the primary CPU. The arguments to this function are:
884
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100885* The address of the `bl31_params` structure populated by BL2.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100886* An opaque pointer that the platform may use as needed.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100887
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100888The platform can copy the contents of the `bl31_params` structure and its
889sub-structures into private variables if the original memory may be
890subsequently overwritten by BL3-1 and similarly the `void *` pointing
891to the platform data also needs to be saved.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100892
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100893On the ARM FVP port, BL2 passes a pointer to a `bl31_params` structure populated
894in the secure DRAM at address `0x6000000` in the bl31_params * argument and it
895does not use opaque pointer mentioned earlier. BL3-1 does not copy this
896information to internal data structures as it guarantees that the secure
897DRAM memory will not be overwritten. It maintains an internal reference to this
898information in the `bl2_to_bl31_params` variable.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100899
900### Function : bl31_plat_arch_setup() [mandatory]
901
902 Argument : void
903 Return : void
904
905This function executes with the MMU and data caches disabled. It is only called
906by the primary CPU.
907
908The purpose of this function is to perform any architectural initialization
909that varies across platforms, for example enabling the MMU (since the memory
910map differs across platforms).
911
912
913### Function : bl31_platform_setup() [mandatory]
914
915 Argument : void
916 Return : void
917
918This function may execute with the MMU and data caches enabled if the platform
919port does the necessary initialization in `bl31_plat_arch_setup()`. It is only
920called by the primary CPU.
921
922The purpose of this function is to complete platform initialization so that both
923BL3-1 runtime services and normal world software can function correctly.
924
925The ARM FVP port does the following:
926* Initializes the generic interrupt controller.
927* Configures the CLCD controller.
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100928* Enables system-level implementation of the generic timer counter.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100929* Grants access to the system counter timer module
930* Initializes the FVP power controller device
931* Detects the system topology.
932
933
934### Function : bl31_get_next_image_info() [mandatory]
935
Achin Gupta35ca3512014-02-19 17:58:33 +0000936 Argument : unsigned int
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100937 Return : entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100938
939This function may execute with the MMU and data caches enabled if the platform
940port does the necessary initializations in `bl31_plat_arch_setup()`.
941
942This function is called by `bl31_main()` to retrieve information provided by
Achin Gupta35ca3512014-02-19 17:58:33 +0000943BL2 for the next image in the security state specified by the argument. BL3-1
944uses this information to pass control to that image in the specified security
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100945state. This function must return a pointer to the `entry_point_info` structure
Achin Gupta35ca3512014-02-19 17:58:33 +0000946(that was copied during `bl31_early_platform_setup()`) if the image exists. It
947should return NULL otherwise.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100948
949
Achin Gupta4f6ad662013-10-25 09:08:21 +01009503.3 Power State Coordination Interface (in BL3-1)
951------------------------------------------------
952
953The ARM Trusted Firmware's implementation of the PSCI API is based around the
954concept of an _affinity instance_. Each _affinity instance_ can be uniquely
955identified in a system by a CPU ID (the processor `MPIDR` is used in the PSCI
956interface) and an _affinity level_. A processing element (for example, a
957CPU) is at level 0. If the CPUs in the system are described in a tree where the
958node above a CPU is a logical grouping of CPUs that share some state, then
959affinity level 1 is that group of CPUs (for example, a cluster), and affinity
960level 2 is a group of clusters (for example, the system). The implementation
961assumes that the affinity level 1 ID can be computed from the affinity level 0
962ID (for example, a unique cluster ID can be computed from the CPU ID). The
963current implementation computes this on the basis of the recommended use of
964`MPIDR` affinity fields in the ARM Architecture Reference Manual.
965
966BL3-1's platform initialization code exports a pointer to the platform-specific
967power management operations required for the PSCI implementation to function
968correctly. This information is populated in the `plat_pm_ops` structure. The
969PSCI implementation calls members of the `plat_pm_ops` structure for performing
970power management operations for each affinity instance. For example, the target
971CPU is specified by its `MPIDR` in a PSCI `CPU_ON` call. The `affinst_on()`
972handler (if present) is called for each affinity instance as the PSCI
973implementation powers up each affinity level implemented in the `MPIDR` (for
974example, CPU, cluster and system).
975
976The following functions must be implemented to initialize PSCI functionality in
977the ARM Trusted Firmware.
978
979
980### Function : plat_get_aff_count() [mandatory]
981
982 Argument : unsigned int, unsigned long
983 Return : unsigned int
984
985This function may execute with the MMU and data caches enabled if the platform
986port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
987called by the primary CPU.
988
989This function is called by the PSCI initialization code to detect the system
990topology. Its purpose is to return the number of affinity instances implemented
991at a given `affinity level` (specified by the first argument) and a given
992`MPIDR` (specified by the second argument). For example, on a dual-cluster
993system where first cluster implements 2 CPUs and the second cluster implements 4
994CPUs, a call to this function with an `MPIDR` corresponding to the first cluster
995(`0x0`) and affinity level 0, would return 2. A call to this function with an
996`MPIDR` corresponding to the second cluster (`0x100`) and affinity level 0,
997would return 4.
998
999
1000### Function : plat_get_aff_state() [mandatory]
1001
1002 Argument : unsigned int, unsigned long
1003 Return : unsigned int
1004
1005This function may execute with the MMU and data caches enabled if the platform
1006port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1007called by the primary CPU.
1008
1009This function is called by the PSCI initialization code. Its purpose is to
1010return the state of an affinity instance. The affinity instance is determined by
1011the affinity ID at a given `affinity level` (specified by the first argument)
1012and an `MPIDR` (specified by the second argument). The state can be one of
1013`PSCI_AFF_PRESENT` or `PSCI_AFF_ABSENT`. The latter state is used to cater for
1014system topologies where certain affinity instances are unimplemented. For
1015example, consider a platform that implements a single cluster with 4 CPUs and
1016another CPU implemented directly on the interconnect with the cluster. The
1017`MPIDR`s of the cluster would range from `0x0-0x3`. The `MPIDR` of the single
1018CPU would be 0x100 to indicate that it does not belong to cluster 0. Cluster 1
1019is missing but needs to be accounted for to reach this single CPU in the
1020topology tree. Hence it is marked as `PSCI_AFF_ABSENT`.
1021
1022
1023### Function : plat_get_max_afflvl() [mandatory]
1024
1025 Argument : void
1026 Return : int
1027
1028This function may execute with the MMU and data caches enabled if the platform
1029port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1030called by the primary CPU.
1031
1032This function is called by the PSCI implementation both during cold and warm
1033boot, to determine the maximum affinity level that the power management
James Morrisseyba3155b2013-10-29 10:56:46 +00001034operations should apply to. ARMv8-A has support for 4 affinity levels. It is
Achin Gupta4f6ad662013-10-25 09:08:21 +01001035likely that hardware will implement fewer affinity levels. This function allows
1036the PSCI implementation to consider only those affinity levels in the system
1037that the platform implements. For example, the Base AEM FVP implements two
1038clusters with a configurable number of CPUs. It reports the maximum affinity
1039level as 1, resulting in PSCI power control up to the cluster level.
1040
1041
1042### Function : platform_setup_pm() [mandatory]
1043
1044 Argument : plat_pm_ops **
1045 Return : int
1046
1047This function may execute with the MMU and data caches enabled if the platform
1048port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1049called by the primary CPU.
1050
1051This function is called by PSCI initialization code. Its purpose is to export
1052handler routines for platform-specific power management actions by populating
1053the passed pointer with a pointer to BL3-1's private `plat_pm_ops` structure.
1054
1055A description of each member of this structure is given below. Please refer to
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001056the ARM FVP specific implementation of these handlers in [plat/fvp/plat_pm.c]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001057as an example. A platform port may choose not implement some of the power
1058management operations. For example, the ARM FVP port does not implement the
1059`affinst_standby()` function.
1060
1061#### plat_pm_ops.affinst_standby()
1062
1063Perform the platform-specific setup to enter the standby state indicated by the
1064passed argument.
1065
1066#### plat_pm_ops.affinst_on()
1067
1068Perform the platform specific setup to power on an affinity instance, specified
1069by the `MPIDR` (first argument) and `affinity level` (fourth argument). The
1070`state` (fifth argument) contains the current state of that affinity instance
1071(ON or OFF). This is useful to determine whether any action must be taken. For
1072example, while powering on a CPU, the cluster that contains this CPU might
1073already be in the ON state. The platform decides what actions must be taken to
1074transition from the current state to the target state (indicated by the power
1075management operation).
1076
1077#### plat_pm_ops.affinst_off()
1078
1079Perform the platform specific setup to power off an affinity instance in the
1080`MPIDR` of the calling CPU. It is called by the PSCI `CPU_OFF` API
1081implementation.
1082
1083The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1084(third argument) have a similar meaning as described in the `affinst_on()`
1085operation. They are used to identify the affinity instance on which the call
1086is made and its current state. This gives the platform port an indication of the
1087state transition it must make to perform the requested action. For example, if
1088the calling CPU is the last powered on CPU in the cluster, after powering down
1089affinity level 0 (CPU), the platform port should power down affinity level 1
1090(the cluster) as well.
1091
Achin Gupta4f6ad662013-10-25 09:08:21 +01001092#### plat_pm_ops.affinst_suspend()
1093
1094Perform the platform specific setup to power off an affinity instance in the
1095`MPIDR` of the calling CPU. It is called by the PSCI `CPU_SUSPEND` API
1096implementation.
1097
1098The `MPIDR` (first argument), `affinity level` (third argument) and `state`
1099(fifth argument) have a similar meaning as described in the `affinst_on()`
1100operation. They are used to identify the affinity instance on which the call
1101is made and its current state. This gives the platform port an indication of the
1102state transition it must make to perform the requested action. For example, if
1103the calling CPU is the last powered on CPU in the cluster, after powering down
1104affinity level 0 (CPU), the platform port should power down affinity level 1
1105(the cluster) as well.
1106
1107The difference between turning an affinity instance off versus suspending it
1108is that in the former case, the affinity instance is expected to re-initialize
1109its state when its next powered on (see `affinst_on_finish()`). In the latter
1110case, the affinity instance is expected to save enough state so that it can
1111resume execution by restoring this state when its powered on (see
1112`affinst_suspend_finish()`).
1113
Achin Gupta4f6ad662013-10-25 09:08:21 +01001114#### plat_pm_ops.affinst_on_finish()
1115
1116This function is called by the PSCI implementation after the calling CPU is
1117powered on and released from reset in response to an earlier PSCI `CPU_ON` call.
1118It performs the platform-specific setup required to initialize enough state for
1119this CPU to enter the normal world and also provide secure runtime firmware
1120services.
1121
1122The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1123(third argument) have a similar meaning as described in the previous operations.
1124
Achin Gupta4f6ad662013-10-25 09:08:21 +01001125#### plat_pm_ops.affinst_on_suspend()
1126
1127This function is called by the PSCI implementation after the calling CPU is
1128powered on and released from reset in response to an asynchronous wakeup
1129event, for example a timer interrupt that was programmed by the CPU during the
1130`CPU_SUSPEND` call. It performs the platform-specific setup required to
1131restore the saved state for this CPU to resume execution in the normal world
1132and also provide secure runtime firmware services.
1133
1134The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1135(third argument) have a similar meaning as described in the previous operations.
1136
Achin Gupta4f6ad662013-10-25 09:08:21 +01001137BL3-1 platform initialization code must also detect the system topology and
1138the state of each affinity instance in the topology. This information is
1139critical for the PSCI runtime service to function correctly. More details are
1140provided in the description of the `plat_get_aff_count()` and
1141`plat_get_aff_state()` functions above.
1142
Achin Guptaa4fa3cb2014-06-02 22:27:36 +010011433.4 Interrupt Management framework (in BL3-1)
1144----------------------------------------------
1145BL3-1 implements an Interrupt Management Framework (IMF) to manage interrupts
1146generated in either security state and targeted to EL1 or EL2 in the non-secure
1147state or EL3/S-EL1 in the secure state. The design of this framework is
1148described in the [IMF Design Guide]
1149
1150A platform should export the following APIs to support the IMF. The following
1151text briefly describes each api and its implementation on the FVP port. The API
1152implementation depends upon the type of interrupt controller present in the
1153platform. The FVP implements an ARM Generic Interrupt Controller (ARM GIC) as
1154per the version 2.0 of the [ARM GIC Architecture Specification]
1155
1156### Function : plat_interrupt_type_to_line() [mandatory]
1157
1158 Argument : uint32_t, uint32_t
1159 Return : uint32_t
1160
1161The ARM processor signals an interrupt exception either through the IRQ or FIQ
1162interrupt line. The specific line that is signaled depends on how the interrupt
1163controller (IC) reports different interrupt types from an execution context in
1164either security state. The IMF uses this API to determine which interrupt line
1165the platform IC uses to signal each type of interrupt supported by the framework
1166from a given security state.
1167
1168The first parameter will be one of the `INTR_TYPE_*` values (see [IMF Design
1169Guide]) indicating the target type of the interrupt, the second parameter is the
1170security state of the originating execution context. The return result is the
1171bit position in the `SCR_EL3` register of the respective interrupt trap: IRQ=1,
1172FIQ=2.
1173
1174The FVP port configures the ARM GIC to signal S-EL1 interrupts as FIQs and
1175Non-secure interrupts as IRQs from either security state.
1176
1177
1178### Function : plat_ic_get_pending_interrupt_type() [mandatory]
1179
1180 Argument : void
1181 Return : uint32_t
1182
1183This API returns the type of the highest priority pending interrupt at the
1184platform IC. The IMF uses the interrupt type to retrieve the corresponding
1185handler function. `INTR_TYPE_INVAL` is returned when there is no interrupt
1186pending. The valid interrupt types that can be returned are `INTR_TYPE_EL3`,
1187`INTR_TYPE_S_EL1` and `INTR_TYPE_NS`.
1188
1189The FVP port reads the _Highest Priority Pending Interrupt Register_
1190(`GICC_HPPIR`) to determine the id of the pending interrupt. The type of interrupt
1191depends upon the id value as follows.
1192
11931. id < 1022 is reported as a S-EL1 interrupt
11942. id = 1022 is reported as a Non-secure interrupt.
11953. id = 1023 is reported as an invalid interrupt type.
1196
1197
1198### Function : plat_ic_get_pending_interrupt_id() [mandatory]
1199
1200 Argument : void
1201 Return : uint32_t
1202
1203This API returns the id of the highest priority pending interrupt at the
1204platform IC. The IMF passes the id returned by this API to the registered
1205handler for the pending interrupt if the `IMF_READ_INTERRUPT_ID` build time flag
1206is set. INTR_ID_UNAVAILABLE is returned when there is no interrupt pending.
1207
1208The FVP port reads the _Highest Priority Pending Interrupt Register_
1209(`GICC_HPPIR`) to determine the id of the pending interrupt. The id that is
1210returned by API depends upon the value of the id read from the interrupt
1211controller as follows.
1212
12131. id < 1022. id is returned as is.
12142. id = 1022. The _Aliased Highest Priority Pending Interrupt Register_
1215 (`GICC_AHPPIR`) is read to determine the id of the non-secure interrupt. This
1216 id is returned by the API.
12173. id = 1023. `INTR_ID_UNAVAILABLE` is returned.
1218
1219
1220### Function : plat_ic_acknowledge_interrupt() [mandatory]
1221
1222 Argument : void
1223 Return : uint32_t
1224
1225This API is used by the CPU to indicate to the platform IC that processing of
1226the highest pending interrupt has begun. It should return the id of the
1227interrupt which is being processed.
1228
1229The FVP port reads the _Interrupt Acknowledge Register_ (`GICC_IAR`). This
1230changes the state of the highest priority pending interrupt from pending to
1231active in the interrupt controller. It returns the value read from the
1232`GICC_IAR`. This value is the id of the interrupt whose state has been changed.
1233
1234The TSP uses this API to start processing of the secure physical timer
1235interrupt.
1236
1237
1238### Function : plat_ic_end_of_interrupt() [mandatory]
1239
1240 Argument : uint32_t
1241 Return : void
1242
1243This API is used by the CPU to indicate to the platform IC that processing of
1244the interrupt corresponding to the id (passed as the parameter) has
1245finished. The id should be the same as the id returned by the
1246`plat_ic_acknowledge_interrupt()` API.
1247
1248The FVP port writes the id to the _End of Interrupt Register_
1249(`GICC_EOIR`). This deactivates the corresponding interrupt in the interrupt
1250controller.
1251
1252The TSP uses this API to finish processing of the secure physical timer
1253interrupt.
1254
1255
1256### Function : plat_ic_get_interrupt_type() [mandatory]
1257
1258 Argument : uint32_t
1259 Return : uint32_t
1260
1261This API returns the type of the interrupt id passed as the parameter.
1262`INTR_TYPE_INVAL` is returned if the id is invalid. If the id is valid, a valid
1263interrupt type (one of `INTR_TYPE_EL3`, `INTR_TYPE_S_EL1` and `INTR_TYPE_NS`) is
1264returned depending upon how the interrupt has been configured by the platform
1265IC.
1266
1267The FVP port configures S-EL1 interrupts as Group0 interrupts and Non-secure
1268interrupts as Group1 interrupts. It reads the group value corresponding to the
1269interrupt id from the relevant _Interrupt Group Register_ (`GICD_IGROUPRn`). It
1270uses the group value to determine the type of interrupt.
1271
Soby Mathewc67b09b2014-07-14 16:57:23 +010012723.5 Crash Reporting mechanism (in BL3-1)
1273----------------------------------------------
1274BL3-1 implements a crash reporting mechanism which prints the various registers
1275of the CPU to enable quick crash analysis and debugging. It requires that a console
1276is designated as the crash console by the platform which will used to print the
1277register dump.
1278
1279The following functions must be implemented by the platform if it wants crash reporting
1280mechanism in BL3-1. The functions are implemented in assembly so that they can be
1281invoked without a C Runtime stack.
1282
1283### Function : plat_crash_console_init
1284
1285 Argument : void
1286 Return : int
1287
1288This API is used by the crash reporting mechanism to intialize the crash console.
1289It should only use the general purpose registers x0 to x2 to do the initiaization
1290and returns 1 on success.
1291
1292The FVP port designates the PL011_UART0 as the crash console and calls the
1293console_core_init() to initialize the console.
1294
1295### Function : plat_crash_console_putc
1296
1297 Argument : int
1298 Return : int
1299
1300This API is used by the crash reporting mechanism to print a character on the
1301designated crash console. It should only use general purpose registers x1 and
1302x2 to do its work. The parameter and the return value are in general purpose
1303register x0.
1304
1305The FVP port designates the PL011_UART0 as the crash console and calls the
1306console_core_putc() to print the character on the console.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001307
Harry Liebela960f282013-12-12 16:03:44 +000013084. C Library
1309-------------
1310
1311To avoid subtle toolchain behavioral dependencies, the header files provided
1312by the compiler are not used. The software is built with the `-nostdinc` flag
1313to ensure no headers are included from the toolchain inadvertently. Instead the
1314required headers are included in the ARM Trusted Firmware source tree. The
1315library only contains those C library definitions required by the local
1316implementation. If more functionality is required, the needed library functions
1317will need to be added to the local implementation.
1318
1319Versions of [FreeBSD] headers can be found in `include/stdlib`. Some of these
1320headers have been cut down in order to simplify the implementation. In order to
1321minimize changes to the header files, the [FreeBSD] layout has been maintained.
1322The generic C library definitions can be found in `include/stdlib` with more
1323system and machine specific declarations in `include/stdlib/sys` and
1324`include/stdlib/machine`.
1325
1326The local C library implementations can be found in `lib/stdlib`. In order to
1327extend the C library these files may need to be modified. It is recommended to
1328use a release version of [FreeBSD] as a starting point.
1329
1330The C library header files in the [FreeBSD] source tree are located in the
1331`include` and `sys/sys` directories. [FreeBSD] machine specific definitions
1332can be found in the `sys/<machine-type>` directories. These files define things
1333like 'the size of a pointer' and 'the range of an integer'. Since an AArch64
1334port for [FreeBSD] does not yet exist, the machine specific definitions are
1335based on existing machine types with similar properties (for example SPARC64).
1336
1337Where possible, C library function implementations were taken from [FreeBSD]
1338as found in the `lib/libc` directory.
1339
1340A copy of the [FreeBSD] sources can be downloaded with `git`.
1341
1342 git clone git://github.com/freebsd/freebsd.git -b origin/release/9.2.0
1343
1344
Harry Liebeld265bd72014-01-31 19:04:10 +000013455. Storage abstraction layer
1346-----------------------------
1347
1348In order to improve platform independence and portability an storage abstraction
1349layer is used to load data from non-volatile platform storage.
1350
1351Each platform should register devices and their drivers via the Storage layer.
1352These drivers then need to be initialized by bootloader phases as
1353required in their respective `blx_platform_setup()` functions. Currently
1354storage access is only required by BL1 and BL2 phases. The `load_image()`
1355function uses the storage layer to access non-volatile platform storage.
1356
1357It is mandatory to implement at least one storage driver. For the FVP the
1358Firmware Image Package(FIP) driver is provided as the default means to load data
1359from storage (see the "Firmware Image Package" section in the [User Guide]).
1360The storage layer is described in the header file `include/io_storage.h`. The
1361implementation of the common library is in `lib/io_storage.c` and the driver
1362files are located in `drivers/io/`.
1363
1364Each IO driver must provide `io_dev_*` structures, as described in
1365`drivers/io/io_driver.h`. These are returned via a mandatory registration
1366function that is called on platform initialization. The semi-hosting driver
1367implementation in `io_semihosting.c` can be used as an example.
1368
1369The Storage layer provides mechanisms to initialize storage devices before
1370IO operations are called. The basic operations supported by the layer
1371include `open()`, `close()`, `read()`, `write()`, `size()` and `seek()`.
1372Drivers do not have to implement all operations, but each platform must
1373provide at least one driver for a device capable of supporting generic
1374operations such as loading a bootloader image.
1375
1376The current implementation only allows for known images to be loaded by the
Dan Handleyb68954c2014-05-29 12:30:24 +01001377firmware. These images are specified by using their names, as defined in
1378[include/plat/common/platform.h]. The platform layer (`plat_get_image_source()`)
1379then returns a reference to a device and a driver-specific `spec` which will be
1380understood by the driver to allow access to the image data.
Harry Liebeld265bd72014-01-31 19:04:10 +00001381
1382The layer is designed in such a way that is it possible to chain drivers with
1383other drivers. For example, file-system drivers may be implemented on top of
1384physical block devices, both represented by IO devices with corresponding
1385drivers. In such a case, the file-system "binding" with the block device may
1386be deferred until the file-system device is initialised.
1387
1388The abstraction currently depends on structures being statically allocated
1389by the drivers and callers, as the system does not yet provide a means of
1390dynamically allocating memory. This may also have the affect of limiting the
1391amount of open resources per driver.
1392
1393
Achin Gupta4f6ad662013-10-25 09:08:21 +01001394- - - - - - - - - - - - - - - - - - - - - - - - - -
1395
Dan Handleye83b0ca2014-01-14 18:17:09 +00001396_Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved._
Achin Gupta4f6ad662013-10-25 09:08:21 +01001397
1398
Achin Guptaa4fa3cb2014-06-02 22:27:36 +01001399[ARM GIC Architecture Specification]: http://arminfo.emea.arm.com/help/topic/com.arm.doc.ihi0048b/IHI0048B_gic_architecture_specification.pdf
1400[IMF Design Guide]: interrupt-framework-design.md
1401[User Guide]: user-guide.md
1402[FreeBSD]: http://www.freebsd.org
Achin Gupta4f6ad662013-10-25 09:08:21 +01001403
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001404[plat/common/aarch64/platform_mp_stack.S]: ../plat/common/aarch64/platform_mp_stack.S
1405[plat/common/aarch64/platform_up_stack.S]: ../plat/common/aarch64/platform_up_stack.S
Dan Handleyb68954c2014-05-29 12:30:24 +01001406[plat/fvp/include/platform_def.h]: ../plat/fvp/include/platform_def.h
1407[plat/fvp/include/plat_macros.S]: ../plat/fvp/include/plat_macros.S
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001408[plat/fvp/aarch64/plat_common.c]: ../plat/fvp/aarch64/plat_common.c
1409[plat/fvp/plat_pm.c]: ../plat/fvp/plat_pm.c
1410[include/runtime_svc.h]: ../include/runtime_svc.h
Dan Handleyb68954c2014-05-29 12:30:24 +01001411[include/plat/common/platform.h]: ../include/plat/common/platform.h