blob: 82dcf9d01659a2fec44f47d5b8850d07b95b007a [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
108* **#define : PCPU_DV_MEM_STACK_SIZE**
109
110 Defines the coherent stack memory available to each CPU. This constant is used
111 by [plat/common/aarch64/platform_mp_stack.S] and
112 [plat/common/aarch64/platform_up_stack.S].
Achin Gupta4f6ad662013-10-25 09:08:21 +0100113
James Morrisseyba3155b2013-10-29 10:56:46 +0000114* **#define : FIRMWARE_WELCOME_STR**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100115
116 Defines the character string printed by BL1 upon entry into the `bl1_main()`
117 function.
118
James Morrisseyba3155b2013-10-29 10:56:46 +0000119* **#define : BL2_IMAGE_NAME**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100120
121 Name of the BL2 binary image on the host file-system. This name is used by
Harry Liebeld265bd72014-01-31 19:04:10 +0000122 BL1 to load BL2 into secure memory from non-volatile storage.
123
124* **#define : BL31_IMAGE_NAME**
125
126 Name of the BL3-1 binary image on the host file-system. This name is used by
127 BL2 to load BL3-1 into secure memory from platform storage.
128
129* **#define : BL33_IMAGE_NAME**
130
131 Name of the BL3-3 binary image on the host file-system. This name is used by
132 BL2 to load BL3-3 into non-secure memory from platform storage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100133
James Morrisseyba3155b2013-10-29 10:56:46 +0000134* **#define : PLATFORM_CACHE_LINE_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100135
136 Defines the size (in bytes) of the largest cache line across all the cache
137 levels in the platform.
138
James Morrisseyba3155b2013-10-29 10:56:46 +0000139* **#define : PLATFORM_CLUSTER_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100140
141 Defines the total number of clusters implemented by the platform in the
142 system.
143
James Morrisseyba3155b2013-10-29 10:56:46 +0000144* **#define : PLATFORM_CORE_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100145
146 Defines the total number of CPUs implemented by the platform across all
147 clusters in the system.
148
James Morrisseyba3155b2013-10-29 10:56:46 +0000149* **#define : PLATFORM_MAX_CPUS_PER_CLUSTER**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100150
151 Defines the maximum number of CPUs that can be implemented within a cluster
152 on the platform.
153
Andrew Thoelke6c0b45d2014-06-20 00:36:14 +0100154* **#define : PLATFORM_NUM_AFFS**
155
156 Defines the total number of nodes in the affinity heirarchy at all affinity
157 levels used by the platform.
158
James Morrisseyba3155b2013-10-29 10:56:46 +0000159* **#define : PRIMARY_CPU**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100160
161 Defines the `MPIDR` of the primary CPU on the platform. This value is used
162 after a cold boot to distinguish between primary and secondary CPUs.
163
James Morrisseyba3155b2013-10-29 10:56:46 +0000164* **#define : TZROM_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100165
166 Defines the base address of secure ROM on the platform, where the BL1 binary
167 is loaded. This constant is used by the linker scripts to ensure that the
168 BL1 image fits into the available memory.
169
James Morrisseyba3155b2013-10-29 10:56:46 +0000170* **#define : TZROM_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100171
172 Defines the size of secure ROM on the platform. This constant is used by the
173 linker scripts to ensure that the BL1 image fits into the available memory.
174
James Morrisseyba3155b2013-10-29 10:56:46 +0000175* **#define : TZRAM_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100176
177 Defines the base address of the secure RAM on platform, where the data
178 section of the BL1 binary is loaded. The BL2 and BL3-1 images are also
179 loaded in this secure RAM region. This constant is used by the linker
180 scripts to ensure that the BL1 data section and BL2/BL3-1 binary images fit
181 into the available memory.
182
James Morrisseyba3155b2013-10-29 10:56:46 +0000183* **#define : TZRAM_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100184
185 Defines the size of the secure RAM on the platform. This constant is used by
186 the linker scripts to ensure that the BL1 data section and BL2/BL3-1 binary
187 images fit into the available memory.
188
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100189* **#define : BL1_RO_BASE**
190
191 Defines the base address in secure ROM where BL1 originally lives. Must be
192 aligned on a page-size boundary.
193
194* **#define : BL1_RO_LIMIT**
195
196 Defines the maximum address in secure ROM that BL1's actual content (i.e.
197 excluding any data section allocated at runtime) can occupy.
198
199* **#define : BL1_RW_BASE**
200
201 Defines the base address in secure RAM where BL1's read-write data will live
202 at runtime. Must be aligned on a page-size boundary.
203
204* **#define : BL1_RW_LIMIT**
205
206 Defines the maximum address in secure RAM that BL1's read-write data can
207 occupy at runtime.
208
James Morrisseyba3155b2013-10-29 10:56:46 +0000209* **#define : BL2_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100210
211 Defines the base address in secure RAM where BL1 loads the BL2 binary image.
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000212 Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100213
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100214* **#define : BL2_LIMIT**
215
216 Defines the maximum address in secure RAM that the BL2 image can occupy.
217
James Morrisseyba3155b2013-10-29 10:56:46 +0000218* **#define : BL31_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100219
220 Defines the base address in secure RAM where BL2 loads the BL3-1 binary
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000221 image. Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100222
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100223* **#define : BL31_LIMIT**
224
225 Defines the maximum address in secure RAM that the BL3-1 image can occupy.
226
Harry Liebeld265bd72014-01-31 19:04:10 +0000227* **#define : NS_IMAGE_OFFSET**
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100228
Harry Liebeld265bd72014-01-31 19:04:10 +0000229 Defines the base address in non-secure DRAM where BL2 loads the BL3-3 binary
230 image. Must be aligned on a page-size boundary.
231
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100232If the BL3-2 image is supported by the platform, the following constants must
233be defined as well:
234
235* **#define : TSP_SEC_MEM_BASE**
236
237 Defines the base address of the secure memory used by the BL3-2 image on the
238 platform.
239
240* **#define : TSP_SEC_MEM_SIZE**
241
242 Defines the size of the secure memory used by the BL3-2 image on the
243 platform.
244
245* **#define : BL32_BASE**
246
247 Defines the base address in secure memory where BL2 loads the BL3-2 binary
248 image. Must be inside the secure memory identified by `TSP_SEC_MEM_BASE` and
249 `TSP_SEC_MEM_SIZE` constants. Must also be aligned on a page-size boundary.
250
251* **#define : BL32_LIMIT**
252
253 Defines the maximum address that the BL3-2 image can occupy. Must be inside
254 the secure memory identified by `TSP_SEC_MEM_BASE` and `TSP_SEC_MEM_SIZE`
255 constants.
256
Sandrine Bailleux46d49f632014-06-23 17:00:23 +0100257The following constants are optional. They should be defined when the platform
258memory layout implies some image overlaying like on FVP.
259
260* **#define : BL31_PROGBITS_LIMIT**
261
262 Defines the maximum address in secure RAM that the BL3-1's progbits sections
263 can occupy.
264
265* **#define : BL32_PROGBITS_LIMIT**
266
267 Defines the maximum address that the TSP's progbits sections can occupy.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100268
Dan Handleyb68954c2014-05-29 12:30:24 +0100269### File : plat_macros.S [mandatory]
Soby Mathewa43d4312014-04-07 15:28:55 +0100270
Dan Handleyb68954c2014-05-29 12:30:24 +0100271Each platform must ensure a file of this name is in the system include path with
272the following macro defined. In the ARM FVP port, this file is found in
273[plat/fvp/include/plat_macros.S].
Soby Mathewa43d4312014-04-07 15:28:55 +0100274
275* **Macro : plat_print_gic_regs**
276
277 This macro allows the crash reporting routine to print GIC registers
Soby Mathew8c106902014-07-16 09:23:52 +0100278 in case of an unhandled exception in BL3-1. This aids in debugging and
Soby Mathewa43d4312014-04-07 15:28:55 +0100279 this macro can be defined to be empty in case GIC register reporting is
280 not desired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100281
Soby Mathew8c106902014-07-16 09:23:52 +0100282* **Macro : plat_print_interconnect_regs**
283
284 This macro allows the crash reporting routine to print interconnect registers
285 in case of an unhandled exception in BL3-1. This aids in debugging and
286 this macro can be defined to be empty in case interconnect register reporting
287 is not desired. In the ARM FVP port, the CCI snoop control registers are
288 reported.
289
Achin Gupta4f6ad662013-10-25 09:08:21 +0100290### Other mandatory modifications
291
James Morrisseyba3155b2013-10-29 10:56:46 +0000292The following mandatory modifications may be implemented in any file
Achin Gupta4f6ad662013-10-25 09:08:21 +0100293the implementer chooses. In the ARM FVP port, they are implemented in
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000294[plat/fvp/aarch64/plat_common.c].
Achin Gupta4f6ad662013-10-25 09:08:21 +0100295
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100296* **Function : uint64_t plat_get_syscnt_freq(void)**
297
298 This function is used by the architecture setup code to retrieve the
299 counter frequency for the CPU's generic timer. This value will be
300 programmed into the `CNTFRQ_EL0` register.
301 In the ARM FVP port, it returns the base frequency of the system counter,
302 which is retrieved from the first entry in the frequency modes table.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100303
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000304
Vikram Kanigirie452cd82014-05-23 15:56:12 +01003052.2 Handling Reset
306------------------
307
308BL1 by default implements the reset vector where execution starts from a cold
309or warm boot. BL3-1 can be optionally set as a reset vector using the
310RESET_TO_BL31 make variable.
311
312For each CPU, the reset vector code is responsible for the following tasks:
313
3141. Distinguishing between a cold boot and a warm boot.
315
3162. In the case of a cold boot and the CPU being a secondary CPU, ensuring that
317 the CPU is placed in a platform-specific state until the primary CPU
318 performs the necessary steps to remove it from this state.
319
3203. In the case of a warm boot, ensuring that the CPU jumps to a platform-
321 specific address in the BL3-1 image in the same processor mode as it was
322 when released from reset.
323
324The following functions need to be implemented by the platform port to enable
325reset vector code to perform the above tasks.
326
327
328### Function : platform_get_entrypoint() [mandatory]
329
330 Argument : unsigned long
331 Return : unsigned int
332
333This function is called with the `SCTLR.M` and `SCTLR.C` bits disabled. The CPU
334is identified by its `MPIDR`, which is passed as the argument. The function is
335responsible for distinguishing between a warm and cold reset using platform-
336specific means. If it's a warm reset then it returns the entrypoint into the
337BL3-1 image that the CPU must jump to. If it's a cold reset then this function
338must return zero.
339
340This function is also responsible for implementing a platform-specific mechanism
341to handle the condition where the CPU has been warm reset but there is no
342entrypoint to jump to.
343
344This function does not follow the Procedure Call Standard used by the
345Application Binary Interface for the ARM 64-bit architecture. The caller should
346not assume that callee saved registers are preserved across a call to this
347function.
348
349This function fulfills requirement 1 and 3 listed above.
350
351
352### Function : plat_secondary_cold_boot_setup() [mandatory]
353
354 Argument : void
355 Return : void
356
357This function is called with the MMU and data caches disabled. It is responsible
358for placing the executing secondary CPU in a platform-specific state until the
359primary CPU performs the necessary actions to bring it out of that state and
360allow entry into the OS.
361
362In the ARM FVP port, each secondary CPU powers itself off. The primary CPU is
363responsible for powering up the secondary CPU when normal world software
364requires them.
365
366This function fulfills requirement 2 above.
367
368
369### 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
407### Function : platform_set_coherent_stack()
408
409 Argument : unsigned long
410 Return : void
411
412A platform may need stack memory that is coherent with main memory to perform
413certain operations like:
414
415* Turning the MMU on, or
416* Flushing caches prior to powering down a CPU or cluster.
417
418Each BL stage allocates this coherent stack memory for each CPU in the
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000419`tzfw_coherent_mem` section.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100420
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000421This function sets the current stack pointer to the coherent stack that
422has been allocated for the CPU specified by MPIDR. For BL images that only
423require a stack for the primary CPU the parameter is ignored. The size of
424the stack allocated to each CPU is specified by the platform defined constant
Achin Gupta4f6ad662013-10-25 09:08:21 +0100425`PCPU_DV_MEM_STACK_SIZE`.
426
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000427Common implementations of this function for the UP and MP BL images are
428provided in [plat/common/aarch64/platform_up_stack.S] and
429[plat/common/aarch64/platform_mp_stack.S]
430
Achin Gupta4f6ad662013-10-25 09:08:21 +0100431
432### Function : platform_is_primary_cpu()
433
434 Argument : unsigned long
435 Return : unsigned int
436
437This function identifies a CPU by its `MPIDR`, which is passed as the argument,
438to determine whether this CPU is the primary CPU or a secondary CPU. A return
439value of zero indicates that the CPU is not the primary CPU, while a non-zero
440return value indicates that the CPU is the primary CPU.
441
442
443### Function : platform_set_stack()
444
445 Argument : unsigned long
446 Return : void
447
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000448This function sets the current stack pointer to the normal memory stack that
449has been allocated for the CPU specificed by MPIDR. For BL images that only
450require a stack for the primary CPU the parameter is ignored. The size of
451the stack allocated to each CPU is specified by the platform defined constant
452`PLATFORM_STACK_SIZE`.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100453
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000454Common implementations of this function for the UP and MP BL images are
455provided in [plat/common/aarch64/platform_up_stack.S] and
456[plat/common/aarch64/platform_mp_stack.S]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100457
458
Achin Guptac8afc782013-11-25 18:45:02 +0000459### Function : platform_get_stack()
460
461 Argument : unsigned long
462 Return : unsigned long
463
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000464This function returns the base address of the normal memory stack that
465has been allocated for the CPU specificed by MPIDR. For BL images that only
466require a stack for the primary CPU the parameter is ignored. The size of
467the stack allocated to each CPU is specified by the platform defined constant
468`PLATFORM_STACK_SIZE`.
Achin Guptac8afc782013-11-25 18:45:02 +0000469
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000470Common implementations of this function for the UP and MP BL images are
471provided in [plat/common/aarch64/platform_up_stack.S] and
472[plat/common/aarch64/platform_mp_stack.S]
Achin Guptac8afc782013-11-25 18:45:02 +0000473
474
Achin Gupta4f6ad662013-10-25 09:08:21 +0100475### Function : plat_report_exception()
476
477 Argument : unsigned int
478 Return : void
479
480A platform may need to report various information about its status when an
481exception is taken, for example the current exception level, the CPU security
482state (secure/non-secure), the exception type, and so on. This function is
483called in the following circumstances:
484
485* In BL1, whenever an exception is taken.
486* In BL2, whenever an exception is taken.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100487
488The default implementation doesn't do anything, to avoid making assumptions
489about the way the platform displays its status information.
490
491This function receives the exception type as its argument. Possible values for
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000492exceptions types are listed in the [include/runtime_svc.h] header file. Note
Achin Gupta4f6ad662013-10-25 09:08:21 +0100493that these constants are not related to any architectural exception code; they
494are just an ARM Trusted Firmware convention.
495
496
4973. Modifications specific to a Boot Loader stage
498-------------------------------------------------
499
5003.1 Boot Loader Stage 1 (BL1)
501-----------------------------
502
503BL1 implements the reset vector where execution starts from after a cold or
504warm boot. For each CPU, BL1 is responsible for the following tasks:
505
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005061. Handling the reset as described in section 2.2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100507
5082. In the case of a cold boot and the CPU being the primary CPU, ensuring that
509 only this CPU executes the remaining BL1 code, including loading and passing
510 control to the BL2 stage.
511
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005123. Loading the BL2 image from non-volatile storage into secure memory at the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100513 address specified by the platform defined constant `BL2_BASE`.
514
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005154. Populating a `meminfo` structure with the following information in memory,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100516 accessible by BL2 immediately upon entry.
517
518 meminfo.total_base = Base address of secure RAM visible to BL2
519 meminfo.total_size = Size of secure RAM visible to BL2
520 meminfo.free_base = Base address of secure RAM available for
521 allocation to BL2
522 meminfo.free_size = Size of secure RAM available for allocation to BL2
523
524 BL1 places this `meminfo` structure at the beginning of the free memory
525 available for its use. Since BL1 cannot allocate memory dynamically at the
526 moment, its free memory will be available for BL2's use as-is. However, this
527 means that BL2 must read the `meminfo` structure before it starts using its
528 free memory (this is discussed in Section 3.2).
529
530 In future releases of the ARM Trusted Firmware it will be possible for
531 the platform to decide where it wants to place the `meminfo` structure for
532 BL2.
533
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100534 BL1 implements the `bl1_init_bl2_mem_layout()` function to populate the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100535 BL2 `meminfo` structure. The platform may override this implementation, for
536 example if the platform wants to restrict the amount of memory visible to
537 BL2. Details of how to do this are given below.
538
539The following functions need to be implemented by the platform port to enable
540BL1 to perform the above tasks.
541
542
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100543### Function : bl1_plat_arch_setup() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100544
545 Argument : void
546 Return : void
547
Achin Gupta4f6ad662013-10-25 09:08:21 +0100548This function performs any platform-specific and architectural setup that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100549platform requires. Platform-specific setup might include configuration of
550memory controllers, configuration of the interconnect to allow the cluster
551to service cache snoop requests from another cluster, and so on.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100552
553In the ARM FVP port, this function enables CCI snoops into the cluster that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100554primary CPU is part of. It also enables the MMU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100555
556This function helps fulfill requirement 2 above.
557
558
559### Function : bl1_platform_setup() [mandatory]
560
561 Argument : void
562 Return : void
563
564This function executes with the MMU and data caches enabled. It is responsible
565for performing any remaining platform-specific setup that can occur after the
566MMU and data cache have been enabled.
567
Harry Liebeld265bd72014-01-31 19:04:10 +0000568This function is also responsible for initializing the storage abstraction layer
569which is used to load further bootloader images.
570
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100571This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100572
573
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000574### Function : bl1_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100575
576 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000577 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100578
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000579This function should only be called on the cold boot path. It executes with the
580MMU and data caches enabled. The pointer returned by this function must point to
581a `meminfo` structure containing the extents and availability of secure RAM for
582the BL1 stage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100583
584 meminfo.total_base = Base address of secure RAM visible to BL1
585 meminfo.total_size = Size of secure RAM visible to BL1
586 meminfo.free_base = Base address of secure RAM available for allocation
587 to BL1
588 meminfo.free_size = Size of secure RAM available for allocation to BL1
589
590This information is used by BL1 to load the BL2 image in secure RAM. BL1 also
591populates a similar structure to tell BL2 the extents of memory available for
592its own use.
593
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100594This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100595
596
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100597### Function : bl1_init_bl2_mem_layout() [optional]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100598
599 Argument : meminfo *, meminfo *, unsigned int, unsigned long
600 Return : void
601
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100602BL1 needs to tell the next stage the amount of secure RAM available
603for it to use. This information is populated in a `meminfo`
Achin Gupta4f6ad662013-10-25 09:08:21 +0100604structure.
605
606Depending upon where BL2 has been loaded in secure RAM (determined by
607`BL2_BASE`), BL1 calculates the amount of free memory available for BL2 to use.
608BL1 also ensures that its data sections resident in secure RAM are not visible
609to BL2. An illustration of how this is done in the ARM FVP port is given in the
610[User Guide], in the Section "Memory layout on Base FVP".
611
612
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100613### Function : bl1_plat_set_bl2_ep_info() [mandatory]
614
615 Argument : image_info *, entry_point_info *
616 Return : void
617
618This function is called after loading BL2 image and it can be used to overwrite
619the entry point set by loader and also set the security state and SPSR which
620represents the entry point system state for BL2.
621
622On FVP, we are setting the security state and the SPSR for the BL2 entrypoint
623
624
Achin Gupta4f6ad662013-10-25 09:08:21 +01006253.2 Boot Loader Stage 2 (BL2)
626-----------------------------
627
628The BL2 stage is executed only by the primary CPU, which is determined in BL1
629using the `platform_is_primary_cpu()` function. BL1 passed control to BL2 at
630`BL2_BASE`. BL2 executes in Secure EL1 and is responsible for:
631
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006321. (Optional) Loading the BL3-0 binary image (if present) from platform
633 provided non-volatile storage. To load the BL3-0 image, BL2 makes use of
634 the `meminfo` returned by the `bl2_plat_get_bl30_meminfo()` function.
635 The platform also defines the address in memory where BL3-0 is loaded
636 through the optional constant `BL30_BASE`. BL2 uses this information
637 to determine if there is enough memory to load the BL3-0 image.
638 Subsequent handling of the BL3-0 image is platform-specific and is
639 implemented in the `bl2_plat_handle_bl30()` function.
640 If `BL30_BASE` is not defined then this step is not performed.
641
6422. Loading the BL3-1 binary image into secure RAM from non-volatile storage. To
Harry Liebeld265bd72014-01-31 19:04:10 +0000643 load the BL3-1 image, BL2 makes use of the `meminfo` structure passed to it
644 by BL1. This structure allows BL2 to calculate how much secure RAM is
645 available for its use. The platform also defines the address in secure RAM
646 where BL3-1 is loaded through the constant `BL31_BASE`. BL2 uses this
647 information to determine if there is enough memory to load the BL3-1 image.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100648
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006493. (Optional) Loading the BL3-2 binary image (if present) from platform
Dan Handley1151c822014-04-15 11:38:38 +0100650 provided non-volatile storage. To load the BL3-2 image, BL2 makes use of
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100651 the `meminfo` returned by the `bl2_plat_get_bl32_meminfo()` function.
652 The platform also defines the address in memory where BL3-2 is loaded
653 through the optional constant `BL32_BASE`. BL2 uses this information
654 to determine if there is enough memory to load the BL3-2 image.
655 If `BL32_BASE` is not defined then this and the next step is not performed.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000656
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006574. (Optional) Arranging to pass control to the BL3-2 image (if present) that
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100658 has been pre-loaded at `BL32_BASE`. BL2 populates an `entry_point_info`
Dan Handley1151c822014-04-15 11:38:38 +0100659 structure in memory provided by the platform with information about how
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100660 BL3-1 should pass control to the BL3-2 image.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000661
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006625. Loading the normal world BL3-3 binary image into non-secure DRAM from
663 platform storage and arranging for BL3-1 to pass control to this image. This
664 address is determined using the `plat_get_ns_image_entrypoint()` function
665 described below.
666
6676. BL2 populates an `entry_point_info` structure in memory provided by the
668 platform with information about how BL3-1 should pass control to the
669 other BL images.
670
Achin Gupta4f6ad662013-10-25 09:08:21 +0100671The following functions must be implemented by the platform port to enable BL2
672to perform the above tasks.
673
674
675### Function : bl2_early_platform_setup() [mandatory]
676
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100677 Argument : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100678 Return : void
679
680This function executes with the MMU and data caches disabled. It is only called
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100681by the primary CPU. The arguments to this function is the address of the
682`meminfo` structure populated by BL1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100683
684The platform must copy the contents of the `meminfo` structure into a private
685variable as the original memory may be subsequently overwritten by BL2. The
686copied structure is made available to all BL2 code through the
Achin Guptae4d084e2014-02-19 17:18:23 +0000687`bl2_plat_sec_mem_layout()` function.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100688
689
690### Function : bl2_plat_arch_setup() [mandatory]
691
692 Argument : void
693 Return : void
694
695This function executes with the MMU and data caches disabled. It is only called
696by the primary CPU.
697
698The purpose of this function is to perform any architectural initialization
699that varies across platforms, for example enabling the MMU (since the memory
700map differs across platforms).
701
702
703### Function : bl2_platform_setup() [mandatory]
704
705 Argument : void
706 Return : void
707
708This function may execute with the MMU and data caches enabled if the platform
709port does the necessary initialization in `bl2_plat_arch_setup()`. It is only
710called by the primary CPU.
711
Achin Guptae4d084e2014-02-19 17:18:23 +0000712The purpose of this function is to perform any platform initialization
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100713specific to BL2. Platform security components are configured if required.
714For the Base FVP the TZC-400 TrustZone controller is configured to only
715grant non-secure access to DRAM. This avoids aliasing between secure and
716non-secure accesses in the TLB and cache - secure execution states can use
717the NS attributes in the MMU translation tables to access the DRAM.
Harry Liebelce19cf12014-04-01 19:28:07 +0100718
Harry Liebeld265bd72014-01-31 19:04:10 +0000719This function is also responsible for initializing the storage abstraction layer
720which is used to load further bootloader images.
721
Achin Gupta4f6ad662013-10-25 09:08:21 +0100722
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000723### Function : bl2_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100724
725 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000726 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100727
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000728This function should only be called on the cold boot path. It may execute with
729the MMU and data caches enabled if the platform port does the necessary
730initialization in `bl2_plat_arch_setup()`. It is only called by the primary CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100731
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000732The purpose of this function is to return a pointer to a `meminfo` structure
733populated with the extents of secure RAM available for BL2 to use. See
Achin Gupta4f6ad662013-10-25 09:08:21 +0100734`bl2_early_platform_setup()` above.
735
736
Sandrine Bailleux93d81d62014-06-24 14:19:36 +0100737### Function : bl2_plat_get_bl30_meminfo() [mandatory]
738
739 Argument : meminfo *
740 Return : void
741
742This function is used to get the memory limits where BL2 can load the
743BL3-0 image. The meminfo provided by this is used by load_image() to
744validate whether the BL3-0 image can be loaded within the given
745memory from the given base.
746
747
748### Function : bl2_plat_handle_bl30() [mandatory]
749
750 Argument : image_info *
751 Return : int
752
753This function is called after loading BL3-0 image and it is used to perform any
754platform-specific actions required to handle the SCP firmware. Typically it
755transfers the image into SCP memory using a platform-specific protocol and waits
756until SCP executes it and signals to the Application Processor (AP) for BL2
757execution to continue.
758
759This function returns 0 on success, a negative error code otherwise.
760
761
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100762### Function : bl2_plat_get_bl31_params() [mandatory]
Harry Liebeld265bd72014-01-31 19:04:10 +0000763
764 Argument : void
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100765 Return : bl31_params *
Harry Liebeld265bd72014-01-31 19:04:10 +0000766
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100767BL2 platform code needs to return a pointer to a `bl31_params` structure it
768will use for passing information to BL3-1. The `bl31_params` structure carries
769the following information.
770 - Header describing the version information for interpreting the bl31_param
771 structure
772 - Information about executing the BL3-3 image in the `bl33_ep_info` field
773 - Information about executing the BL3-2 image in the `bl32_ep_info` field
774 - Information about the type and extents of BL3-1 image in the
775 `bl31_image_info` field
776 - Information about the type and extents of BL3-2 image in the
777 `bl32_image_info` field
778 - Information about the type and extents of BL3-3 image in the
779 `bl33_image_info` field
780
781The memory pointed by this structure and its sub-structures should be
782accessible from BL3-1 initialisation code. BL3-1 might choose to copy the
783necessary content, or maintain the structures until BL3-3 is initialised.
Harry Liebeld265bd72014-01-31 19:04:10 +0000784
785
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100786### Funtion : bl2_plat_get_bl31_ep_info() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100787
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100788 Argument : void
789 Return : entry_point_info *
790
791BL2 platform code returns a pointer which is used to populate the entry point
792information for BL3-1 entry point. The location pointed by it should be
793accessible from BL1 while processing the synchronous exception to run to BL3-1.
794
795On FVP this is allocated inside an bl2_to_bl31_params_mem structure which
796is allocated at an address pointed by PARAMS_BASE.
797
798
799### Function : bl2_plat_set_bl31_ep_info() [mandatory]
800
801 Argument : image_info *, entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100802 Return : void
803
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100804This function is called after loading BL3-1 image and it can be used to
805overwrite the entry point set by loader and also set the security state
806and SPSR which represents the entry point system state for BL3-1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100807
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100808On FVP, we are setting the security state and the SPSR for the BL3-1
809entrypoint.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100810
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100811### Function : bl2_plat_set_bl32_ep_info() [mandatory]
812
813 Argument : image_info *, entry_point_info *
814 Return : void
815
816This function is called after loading BL3-2 image and it can be used to
817overwrite the entry point set by loader and also set the security state
818and SPSR which represents the entry point system state for BL3-2.
819
820On FVP, we are setting the security state and the SPSR for the BL3-2
821entrypoint
822
823### Function : bl2_plat_set_bl33_ep_info() [mandatory]
824
825 Argument : image_info *, entry_point_info *
826 Return : void
827
828This function is called after loading BL3-3 image and it can be used to
829overwrite the entry point set by loader and also set the security state
830and SPSR which represents the entry point system state for BL3-3.
831
832On FVP, we are setting the security state and the SPSR for the BL3-3
833entrypoint
834
835### Function : bl2_plat_get_bl32_meminfo() [mandatory]
836
837 Argument : meminfo *
838 Return : void
839
840This function is used to get the memory limits where BL2 can load the
841BL3-2 image. The meminfo provided by this is used by load_image() to
842validate whether the BL3-2 image can be loaded with in the given
843memory from the given base.
844
845### Function : bl2_plat_get_bl33_meminfo() [mandatory]
846
847 Argument : meminfo *
848 Return : void
849
850This function is used to get the memory limits where BL2 can load the
851BL3-3 image. The meminfo provided by this is used by load_image() to
852validate whether the BL3-3 image can be loaded with in the given
853memory from the given base.
854
855### Function : bl2_plat_flush_bl31_params() [mandatory]
856
857 Argument : void
858 Return : void
859
860Once BL2 has populated all the structures that needs to be read by BL1
861and BL3-1 including the bl31_params structures and its sub-structures,
862the bl31_ep_info structure and any platform specific data. It flushes
863all these data to the main memory so that it is available when we jump to
864later Bootloader stages with MMU off
Achin Gupta4f6ad662013-10-25 09:08:21 +0100865
866### Function : plat_get_ns_image_entrypoint() [mandatory]
867
868 Argument : void
869 Return : unsigned long
870
871As previously described, BL2 is responsible for arranging for control to be
872passed to a normal world BL image through BL3-1. This function returns the
873entrypoint of that image, which BL3-1 uses to jump to it.
874
Harry Liebeld265bd72014-01-31 19:04:10 +0000875BL2 is responsible for loading the normal world BL3-3 image (e.g. UEFI).
Achin Gupta4f6ad662013-10-25 09:08:21 +0100876
877
8783.2 Boot Loader Stage 3-1 (BL3-1)
879---------------------------------
880
881During cold boot, the BL3-1 stage is executed only by the primary CPU. This is
882determined in BL1 using the `platform_is_primary_cpu()` function. BL1 passes
883control to BL3-1 at `BL31_BASE`. During warm boot, BL3-1 is executed by all
884CPUs. BL3-1 executes at EL3 and is responsible for:
885
8861. Re-initializing all architectural and platform state. Although BL1 performs
887 some of this initialization, BL3-1 remains resident in EL3 and must ensure
888 that EL3 architectural and platform state is completely initialized. It
889 should make no assumptions about the system state when it receives control.
890
8912. Passing control to a normal world BL image, pre-loaded at a platform-
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100892 specific address by BL2. BL3-1 uses the `entry_point_info` structure that BL2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100893 populated in memory to do this.
894
8953. Providing runtime firmware services. Currently, BL3-1 only implements a
896 subset of the Power State Coordination Interface (PSCI) API as a runtime
897 service. See Section 3.3 below for details of porting the PSCI
898 implementation.
899
Achin Gupta35ca3512014-02-19 17:58:33 +00009004. Optionally passing control to the BL3-2 image, pre-loaded at a platform-
901 specific address by BL2. BL3-1 exports a set of apis that allow runtime
902 services to specify the security state in which the next image should be
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100903 executed and run the corresponding image. BL3-1 uses the `entry_point_info`
904 structure populated by BL2 to do this.
905
906If BL3-1 is a reset vector, It also needs to handle the reset as specified in
907section 2.2 before the tasks described above.
Achin Gupta35ca3512014-02-19 17:58:33 +0000908
Achin Gupta4f6ad662013-10-25 09:08:21 +0100909The following functions must be implemented by the platform port to enable BL3-1
910to perform the above tasks.
911
912
913### Function : bl31_early_platform_setup() [mandatory]
914
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100915 Argument : bl31_params *, void *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100916 Return : void
917
918This function executes with the MMU and data caches disabled. It is only called
919by the primary CPU. The arguments to this function are:
920
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100921* The address of the `bl31_params` structure populated by BL2.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100922* An opaque pointer that the platform may use as needed.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100923
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100924The platform can copy the contents of the `bl31_params` structure and its
925sub-structures into private variables if the original memory may be
926subsequently overwritten by BL3-1 and similarly the `void *` pointing
927to the platform data also needs to be saved.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100928
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100929On the ARM FVP port, BL2 passes a pointer to a `bl31_params` structure populated
930in the secure DRAM at address `0x6000000` in the bl31_params * argument and it
931does not use opaque pointer mentioned earlier. BL3-1 does not copy this
932information to internal data structures as it guarantees that the secure
933DRAM memory will not be overwritten. It maintains an internal reference to this
934information in the `bl2_to_bl31_params` variable.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100935
936### Function : bl31_plat_arch_setup() [mandatory]
937
938 Argument : void
939 Return : void
940
941This function executes with the MMU and data caches disabled. It is only called
942by the primary CPU.
943
944The purpose of this function is to perform any architectural initialization
945that varies across platforms, for example enabling the MMU (since the memory
946map differs across platforms).
947
948
949### Function : bl31_platform_setup() [mandatory]
950
951 Argument : void
952 Return : void
953
954This function may execute with the MMU and data caches enabled if the platform
955port does the necessary initialization in `bl31_plat_arch_setup()`. It is only
956called by the primary CPU.
957
958The purpose of this function is to complete platform initialization so that both
959BL3-1 runtime services and normal world software can function correctly.
960
961The ARM FVP port does the following:
962* Initializes the generic interrupt controller.
963* Configures the CLCD controller.
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100964* Enables system-level implementation of the generic timer counter.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100965* Grants access to the system counter timer module
966* Initializes the FVP power controller device
967* Detects the system topology.
968
969
970### Function : bl31_get_next_image_info() [mandatory]
971
Achin Gupta35ca3512014-02-19 17:58:33 +0000972 Argument : unsigned int
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100973 Return : entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100974
975This function may execute with the MMU and data caches enabled if the platform
976port does the necessary initializations in `bl31_plat_arch_setup()`.
977
978This function is called by `bl31_main()` to retrieve information provided by
Achin Gupta35ca3512014-02-19 17:58:33 +0000979BL2 for the next image in the security state specified by the argument. BL3-1
980uses this information to pass control to that image in the specified security
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100981state. This function must return a pointer to the `entry_point_info` structure
Achin Gupta35ca3512014-02-19 17:58:33 +0000982(that was copied during `bl31_early_platform_setup()`) if the image exists. It
983should return NULL otherwise.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100984
985
Achin Gupta4f6ad662013-10-25 09:08:21 +01009863.3 Power State Coordination Interface (in BL3-1)
987------------------------------------------------
988
989The ARM Trusted Firmware's implementation of the PSCI API is based around the
990concept of an _affinity instance_. Each _affinity instance_ can be uniquely
991identified in a system by a CPU ID (the processor `MPIDR` is used in the PSCI
992interface) and an _affinity level_. A processing element (for example, a
993CPU) is at level 0. If the CPUs in the system are described in a tree where the
994node above a CPU is a logical grouping of CPUs that share some state, then
995affinity level 1 is that group of CPUs (for example, a cluster), and affinity
996level 2 is a group of clusters (for example, the system). The implementation
997assumes that the affinity level 1 ID can be computed from the affinity level 0
998ID (for example, a unique cluster ID can be computed from the CPU ID). The
999current implementation computes this on the basis of the recommended use of
1000`MPIDR` affinity fields in the ARM Architecture Reference Manual.
1001
1002BL3-1's platform initialization code exports a pointer to the platform-specific
1003power management operations required for the PSCI implementation to function
1004correctly. This information is populated in the `plat_pm_ops` structure. The
1005PSCI implementation calls members of the `plat_pm_ops` structure for performing
1006power management operations for each affinity instance. For example, the target
1007CPU is specified by its `MPIDR` in a PSCI `CPU_ON` call. The `affinst_on()`
1008handler (if present) is called for each affinity instance as the PSCI
1009implementation powers up each affinity level implemented in the `MPIDR` (for
1010example, CPU, cluster and system).
1011
1012The following functions must be implemented to initialize PSCI functionality in
1013the ARM Trusted Firmware.
1014
1015
1016### Function : plat_get_aff_count() [mandatory]
1017
1018 Argument : unsigned int, unsigned long
1019 Return : unsigned int
1020
1021This function may execute with the MMU and data caches enabled if the platform
1022port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1023called by the primary CPU.
1024
1025This function is called by the PSCI initialization code to detect the system
1026topology. Its purpose is to return the number of affinity instances implemented
1027at a given `affinity level` (specified by the first argument) and a given
1028`MPIDR` (specified by the second argument). For example, on a dual-cluster
1029system where first cluster implements 2 CPUs and the second cluster implements 4
1030CPUs, a call to this function with an `MPIDR` corresponding to the first cluster
1031(`0x0`) and affinity level 0, would return 2. A call to this function with an
1032`MPIDR` corresponding to the second cluster (`0x100`) and affinity level 0,
1033would return 4.
1034
1035
1036### Function : plat_get_aff_state() [mandatory]
1037
1038 Argument : unsigned int, unsigned long
1039 Return : unsigned int
1040
1041This function may execute with the MMU and data caches enabled if the platform
1042port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1043called by the primary CPU.
1044
1045This function is called by the PSCI initialization code. Its purpose is to
1046return the state of an affinity instance. The affinity instance is determined by
1047the affinity ID at a given `affinity level` (specified by the first argument)
1048and an `MPIDR` (specified by the second argument). The state can be one of
1049`PSCI_AFF_PRESENT` or `PSCI_AFF_ABSENT`. The latter state is used to cater for
1050system topologies where certain affinity instances are unimplemented. For
1051example, consider a platform that implements a single cluster with 4 CPUs and
1052another CPU implemented directly on the interconnect with the cluster. The
1053`MPIDR`s of the cluster would range from `0x0-0x3`. The `MPIDR` of the single
1054CPU would be 0x100 to indicate that it does not belong to cluster 0. Cluster 1
1055is missing but needs to be accounted for to reach this single CPU in the
1056topology tree. Hence it is marked as `PSCI_AFF_ABSENT`.
1057
1058
1059### Function : plat_get_max_afflvl() [mandatory]
1060
1061 Argument : void
1062 Return : int
1063
1064This function may execute with the MMU and data caches enabled if the platform
1065port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1066called by the primary CPU.
1067
1068This function is called by the PSCI implementation both during cold and warm
1069boot, to determine the maximum affinity level that the power management
James Morrisseyba3155b2013-10-29 10:56:46 +00001070operations should apply to. ARMv8-A has support for 4 affinity levels. It is
Achin Gupta4f6ad662013-10-25 09:08:21 +01001071likely that hardware will implement fewer affinity levels. This function allows
1072the PSCI implementation to consider only those affinity levels in the system
1073that the platform implements. For example, the Base AEM FVP implements two
1074clusters with a configurable number of CPUs. It reports the maximum affinity
1075level as 1, resulting in PSCI power control up to the cluster level.
1076
1077
1078### Function : platform_setup_pm() [mandatory]
1079
1080 Argument : plat_pm_ops **
1081 Return : int
1082
1083This function may execute with the MMU and data caches enabled if the platform
1084port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1085called by the primary CPU.
1086
1087This function is called by PSCI initialization code. Its purpose is to export
1088handler routines for platform-specific power management actions by populating
1089the passed pointer with a pointer to BL3-1's private `plat_pm_ops` structure.
1090
1091A description of each member of this structure is given below. Please refer to
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001092the ARM FVP specific implementation of these handlers in [plat/fvp/plat_pm.c]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001093as an example. A platform port may choose not implement some of the power
1094management operations. For example, the ARM FVP port does not implement the
1095`affinst_standby()` function.
1096
1097#### plat_pm_ops.affinst_standby()
1098
1099Perform the platform-specific setup to enter the standby state indicated by the
1100passed argument.
1101
1102#### plat_pm_ops.affinst_on()
1103
1104Perform the platform specific setup to power on an affinity instance, specified
1105by the `MPIDR` (first argument) and `affinity level` (fourth argument). The
1106`state` (fifth argument) contains the current state of that affinity instance
1107(ON or OFF). This is useful to determine whether any action must be taken. For
1108example, while powering on a CPU, the cluster that contains this CPU might
1109already be in the ON state. The platform decides what actions must be taken to
1110transition from the current state to the target state (indicated by the power
1111management operation).
1112
1113#### plat_pm_ops.affinst_off()
1114
1115Perform the platform specific setup to power off an affinity instance in the
1116`MPIDR` of the calling CPU. It is called by the PSCI `CPU_OFF` API
1117implementation.
1118
1119The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1120(third argument) have a similar meaning as described in the `affinst_on()`
1121operation. They are used to identify the affinity instance on which the call
1122is made and its current state. This gives the platform port an indication of the
1123state transition it must make to perform the requested action. For example, if
1124the calling CPU is the last powered on CPU in the cluster, after powering down
1125affinity level 0 (CPU), the platform port should power down affinity level 1
1126(the cluster) as well.
1127
1128This function is called with coherent stacks. This allows the PSCI
1129implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001130stale stack state after turning off the caches. On ARMv8-A cache hits do not
1131occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001132
1133#### plat_pm_ops.affinst_suspend()
1134
1135Perform the platform specific setup to power off an affinity instance in the
1136`MPIDR` of the calling CPU. It is called by the PSCI `CPU_SUSPEND` API
1137implementation.
1138
1139The `MPIDR` (first argument), `affinity level` (third argument) and `state`
1140(fifth argument) have a similar meaning as described in the `affinst_on()`
1141operation. They are used to identify the affinity instance on which the call
1142is made and its current state. This gives the platform port an indication of the
1143state transition it must make to perform the requested action. For example, if
1144the calling CPU is the last powered on CPU in the cluster, after powering down
1145affinity level 0 (CPU), the platform port should power down affinity level 1
1146(the cluster) as well.
1147
1148The difference between turning an affinity instance off versus suspending it
1149is that in the former case, the affinity instance is expected to re-initialize
1150its state when its next powered on (see `affinst_on_finish()`). In the latter
1151case, the affinity instance is expected to save enough state so that it can
1152resume execution by restoring this state when its powered on (see
1153`affinst_suspend_finish()`).
1154
1155This function is called with coherent stacks. This allows the PSCI
1156implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001157stale stack state after turning off the caches. On ARMv8-A cache hits do not
1158occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001159
1160#### plat_pm_ops.affinst_on_finish()
1161
1162This function is called by the PSCI implementation after the calling CPU is
1163powered on and released from reset in response to an earlier PSCI `CPU_ON` call.
1164It performs the platform-specific setup required to initialize enough state for
1165this CPU to enter the normal world and also provide secure runtime firmware
1166services.
1167
1168The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1169(third argument) have a similar meaning as described in the previous operations.
1170
1171This function is called with coherent stacks. This allows the PSCI
1172implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001173stale stack state after turning off the caches. On ARMv8-A cache hits do not
1174occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001175
1176#### plat_pm_ops.affinst_on_suspend()
1177
1178This function is called by the PSCI implementation after the calling CPU is
1179powered on and released from reset in response to an asynchronous wakeup
1180event, for example a timer interrupt that was programmed by the CPU during the
1181`CPU_SUSPEND` call. It performs the platform-specific setup required to
1182restore the saved state for this CPU to resume execution in the normal world
1183and also provide secure runtime firmware services.
1184
1185The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1186(third argument) have a similar meaning as described in the previous operations.
1187
1188This function is called with coherent stacks. This allows the PSCI
1189implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001190stale stack state after turning off the caches. On ARMv8-A cache hits do not
1191occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001192
1193BL3-1 platform initialization code must also detect the system topology and
1194the state of each affinity instance in the topology. This information is
1195critical for the PSCI runtime service to function correctly. More details are
1196provided in the description of the `plat_get_aff_count()` and
1197`plat_get_aff_state()` functions above.
1198
Achin Guptaa4fa3cb2014-06-02 22:27:36 +010011993.4 Interrupt Management framework (in BL3-1)
1200----------------------------------------------
1201BL3-1 implements an Interrupt Management Framework (IMF) to manage interrupts
1202generated in either security state and targeted to EL1 or EL2 in the non-secure
1203state or EL3/S-EL1 in the secure state. The design of this framework is
1204described in the [IMF Design Guide]
1205
1206A platform should export the following APIs to support the IMF. The following
1207text briefly describes each api and its implementation on the FVP port. The API
1208implementation depends upon the type of interrupt controller present in the
1209platform. The FVP implements an ARM Generic Interrupt Controller (ARM GIC) as
1210per the version 2.0 of the [ARM GIC Architecture Specification]
1211
1212### Function : plat_interrupt_type_to_line() [mandatory]
1213
1214 Argument : uint32_t, uint32_t
1215 Return : uint32_t
1216
1217The ARM processor signals an interrupt exception either through the IRQ or FIQ
1218interrupt line. The specific line that is signaled depends on how the interrupt
1219controller (IC) reports different interrupt types from an execution context in
1220either security state. The IMF uses this API to determine which interrupt line
1221the platform IC uses to signal each type of interrupt supported by the framework
1222from a given security state.
1223
1224The first parameter will be one of the `INTR_TYPE_*` values (see [IMF Design
1225Guide]) indicating the target type of the interrupt, the second parameter is the
1226security state of the originating execution context. The return result is the
1227bit position in the `SCR_EL3` register of the respective interrupt trap: IRQ=1,
1228FIQ=2.
1229
1230The FVP port configures the ARM GIC to signal S-EL1 interrupts as FIQs and
1231Non-secure interrupts as IRQs from either security state.
1232
1233
1234### Function : plat_ic_get_pending_interrupt_type() [mandatory]
1235
1236 Argument : void
1237 Return : uint32_t
1238
1239This API returns the type of the highest priority pending interrupt at the
1240platform IC. The IMF uses the interrupt type to retrieve the corresponding
1241handler function. `INTR_TYPE_INVAL` is returned when there is no interrupt
1242pending. The valid interrupt types that can be returned are `INTR_TYPE_EL3`,
1243`INTR_TYPE_S_EL1` and `INTR_TYPE_NS`.
1244
1245The FVP port reads the _Highest Priority Pending Interrupt Register_
1246(`GICC_HPPIR`) to determine the id of the pending interrupt. The type of interrupt
1247depends upon the id value as follows.
1248
12491. id < 1022 is reported as a S-EL1 interrupt
12502. id = 1022 is reported as a Non-secure interrupt.
12513. id = 1023 is reported as an invalid interrupt type.
1252
1253
1254### Function : plat_ic_get_pending_interrupt_id() [mandatory]
1255
1256 Argument : void
1257 Return : uint32_t
1258
1259This API returns the id of the highest priority pending interrupt at the
1260platform IC. The IMF passes the id returned by this API to the registered
1261handler for the pending interrupt if the `IMF_READ_INTERRUPT_ID` build time flag
1262is set. INTR_ID_UNAVAILABLE is returned when there is no interrupt pending.
1263
1264The FVP port reads the _Highest Priority Pending Interrupt Register_
1265(`GICC_HPPIR`) to determine the id of the pending interrupt. The id that is
1266returned by API depends upon the value of the id read from the interrupt
1267controller as follows.
1268
12691. id < 1022. id is returned as is.
12702. id = 1022. The _Aliased Highest Priority Pending Interrupt Register_
1271 (`GICC_AHPPIR`) is read to determine the id of the non-secure interrupt. This
1272 id is returned by the API.
12733. id = 1023. `INTR_ID_UNAVAILABLE` is returned.
1274
1275
1276### Function : plat_ic_acknowledge_interrupt() [mandatory]
1277
1278 Argument : void
1279 Return : uint32_t
1280
1281This API is used by the CPU to indicate to the platform IC that processing of
1282the highest pending interrupt has begun. It should return the id of the
1283interrupt which is being processed.
1284
1285The FVP port reads the _Interrupt Acknowledge Register_ (`GICC_IAR`). This
1286changes the state of the highest priority pending interrupt from pending to
1287active in the interrupt controller. It returns the value read from the
1288`GICC_IAR`. This value is the id of the interrupt whose state has been changed.
1289
1290The TSP uses this API to start processing of the secure physical timer
1291interrupt.
1292
1293
1294### Function : plat_ic_end_of_interrupt() [mandatory]
1295
1296 Argument : uint32_t
1297 Return : void
1298
1299This API is used by the CPU to indicate to the platform IC that processing of
1300the interrupt corresponding to the id (passed as the parameter) has
1301finished. The id should be the same as the id returned by the
1302`plat_ic_acknowledge_interrupt()` API.
1303
1304The FVP port writes the id to the _End of Interrupt Register_
1305(`GICC_EOIR`). This deactivates the corresponding interrupt in the interrupt
1306controller.
1307
1308The TSP uses this API to finish processing of the secure physical timer
1309interrupt.
1310
1311
1312### Function : plat_ic_get_interrupt_type() [mandatory]
1313
1314 Argument : uint32_t
1315 Return : uint32_t
1316
1317This API returns the type of the interrupt id passed as the parameter.
1318`INTR_TYPE_INVAL` is returned if the id is invalid. If the id is valid, a valid
1319interrupt type (one of `INTR_TYPE_EL3`, `INTR_TYPE_S_EL1` and `INTR_TYPE_NS`) is
1320returned depending upon how the interrupt has been configured by the platform
1321IC.
1322
1323The FVP port configures S-EL1 interrupts as Group0 interrupts and Non-secure
1324interrupts as Group1 interrupts. It reads the group value corresponding to the
1325interrupt id from the relevant _Interrupt Group Register_ (`GICD_IGROUPRn`). It
1326uses the group value to determine the type of interrupt.
1327
Soby Mathewc67b09b2014-07-14 16:57:23 +010013283.5 Crash Reporting mechanism (in BL3-1)
1329----------------------------------------------
1330BL3-1 implements a crash reporting mechanism which prints the various registers
1331of the CPU to enable quick crash analysis and debugging. It requires that a console
1332is designated as the crash console by the platform which will used to print the
1333register dump.
1334
1335The following functions must be implemented by the platform if it wants crash reporting
1336mechanism in BL3-1. The functions are implemented in assembly so that they can be
1337invoked without a C Runtime stack.
1338
1339### Function : plat_crash_console_init
1340
1341 Argument : void
1342 Return : int
1343
1344This API is used by the crash reporting mechanism to intialize the crash console.
1345It should only use the general purpose registers x0 to x2 to do the initiaization
1346and returns 1 on success.
1347
1348The FVP port designates the PL011_UART0 as the crash console and calls the
1349console_core_init() to initialize the console.
1350
1351### Function : plat_crash_console_putc
1352
1353 Argument : int
1354 Return : int
1355
1356This API is used by the crash reporting mechanism to print a character on the
1357designated crash console. It should only use general purpose registers x1 and
1358x2 to do its work. The parameter and the return value are in general purpose
1359register x0.
1360
1361The FVP port designates the PL011_UART0 as the crash console and calls the
1362console_core_putc() to print the character on the console.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001363
Harry Liebela960f282013-12-12 16:03:44 +000013644. C Library
1365-------------
1366
1367To avoid subtle toolchain behavioral dependencies, the header files provided
1368by the compiler are not used. The software is built with the `-nostdinc` flag
1369to ensure no headers are included from the toolchain inadvertently. Instead the
1370required headers are included in the ARM Trusted Firmware source tree. The
1371library only contains those C library definitions required by the local
1372implementation. If more functionality is required, the needed library functions
1373will need to be added to the local implementation.
1374
1375Versions of [FreeBSD] headers can be found in `include/stdlib`. Some of these
1376headers have been cut down in order to simplify the implementation. In order to
1377minimize changes to the header files, the [FreeBSD] layout has been maintained.
1378The generic C library definitions can be found in `include/stdlib` with more
1379system and machine specific declarations in `include/stdlib/sys` and
1380`include/stdlib/machine`.
1381
1382The local C library implementations can be found in `lib/stdlib`. In order to
1383extend the C library these files may need to be modified. It is recommended to
1384use a release version of [FreeBSD] as a starting point.
1385
1386The C library header files in the [FreeBSD] source tree are located in the
1387`include` and `sys/sys` directories. [FreeBSD] machine specific definitions
1388can be found in the `sys/<machine-type>` directories. These files define things
1389like 'the size of a pointer' and 'the range of an integer'. Since an AArch64
1390port for [FreeBSD] does not yet exist, the machine specific definitions are
1391based on existing machine types with similar properties (for example SPARC64).
1392
1393Where possible, C library function implementations were taken from [FreeBSD]
1394as found in the `lib/libc` directory.
1395
1396A copy of the [FreeBSD] sources can be downloaded with `git`.
1397
1398 git clone git://github.com/freebsd/freebsd.git -b origin/release/9.2.0
1399
1400
Harry Liebeld265bd72014-01-31 19:04:10 +000014015. Storage abstraction layer
1402-----------------------------
1403
1404In order to improve platform independence and portability an storage abstraction
1405layer is used to load data from non-volatile platform storage.
1406
1407Each platform should register devices and their drivers via the Storage layer.
1408These drivers then need to be initialized by bootloader phases as
1409required in their respective `blx_platform_setup()` functions. Currently
1410storage access is only required by BL1 and BL2 phases. The `load_image()`
1411function uses the storage layer to access non-volatile platform storage.
1412
1413It is mandatory to implement at least one storage driver. For the FVP the
1414Firmware Image Package(FIP) driver is provided as the default means to load data
1415from storage (see the "Firmware Image Package" section in the [User Guide]).
1416The storage layer is described in the header file `include/io_storage.h`. The
1417implementation of the common library is in `lib/io_storage.c` and the driver
1418files are located in `drivers/io/`.
1419
1420Each IO driver must provide `io_dev_*` structures, as described in
1421`drivers/io/io_driver.h`. These are returned via a mandatory registration
1422function that is called on platform initialization. The semi-hosting driver
1423implementation in `io_semihosting.c` can be used as an example.
1424
1425The Storage layer provides mechanisms to initialize storage devices before
1426IO operations are called. The basic operations supported by the layer
1427include `open()`, `close()`, `read()`, `write()`, `size()` and `seek()`.
1428Drivers do not have to implement all operations, but each platform must
1429provide at least one driver for a device capable of supporting generic
1430operations such as loading a bootloader image.
1431
1432The current implementation only allows for known images to be loaded by the
Dan Handleyb68954c2014-05-29 12:30:24 +01001433firmware. These images are specified by using their names, as defined in
1434[include/plat/common/platform.h]. The platform layer (`plat_get_image_source()`)
1435then returns a reference to a device and a driver-specific `spec` which will be
1436understood by the driver to allow access to the image data.
Harry Liebeld265bd72014-01-31 19:04:10 +00001437
1438The layer is designed in such a way that is it possible to chain drivers with
1439other drivers. For example, file-system drivers may be implemented on top of
1440physical block devices, both represented by IO devices with corresponding
1441drivers. In such a case, the file-system "binding" with the block device may
1442be deferred until the file-system device is initialised.
1443
1444The abstraction currently depends on structures being statically allocated
1445by the drivers and callers, as the system does not yet provide a means of
1446dynamically allocating memory. This may also have the affect of limiting the
1447amount of open resources per driver.
1448
1449
Achin Gupta4f6ad662013-10-25 09:08:21 +01001450- - - - - - - - - - - - - - - - - - - - - - - - - -
1451
Dan Handleye83b0ca2014-01-14 18:17:09 +00001452_Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved._
Achin Gupta4f6ad662013-10-25 09:08:21 +01001453
1454
Achin Guptaa4fa3cb2014-06-02 22:27:36 +01001455[ARM GIC Architecture Specification]: http://arminfo.emea.arm.com/help/topic/com.arm.doc.ihi0048b/IHI0048B_gic_architecture_specification.pdf
1456[IMF Design Guide]: interrupt-framework-design.md
1457[User Guide]: user-guide.md
1458[FreeBSD]: http://www.freebsd.org
Achin Gupta4f6ad662013-10-25 09:08:21 +01001459
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001460[plat/common/aarch64/platform_mp_stack.S]: ../plat/common/aarch64/platform_mp_stack.S
1461[plat/common/aarch64/platform_up_stack.S]: ../plat/common/aarch64/platform_up_stack.S
Dan Handleyb68954c2014-05-29 12:30:24 +01001462[plat/fvp/include/platform_def.h]: ../plat/fvp/include/platform_def.h
1463[plat/fvp/include/plat_macros.S]: ../plat/fvp/include/plat_macros.S
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001464[plat/fvp/aarch64/plat_common.c]: ../plat/fvp/aarch64/plat_common.c
1465[plat/fvp/plat_pm.c]: ../plat/fvp/plat_pm.c
1466[include/runtime_svc.h]: ../include/runtime_svc.h
Dan Handleyb68954c2014-05-29 12:30:24 +01001467[include/plat/common/platform.h]: ../include/plat/common/platform.h