blob: 813d0be6c83ff73f2edbd00e9740221e5a2f1c26 [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)
Harry Liebeld265bd72014-01-31 19:04:10 +0000184. C Library
195. Storage abstraction layer
Achin Gupta4f6ad662013-10-25 09:08:21 +010020
21- - - - - - - - - - - - - - - - - -
22
231. Introduction
24----------------
25
26Porting the ARM Trusted Firmware to a new platform involves making some
27mandatory and optional modifications for both the cold and warm boot paths.
28Modifications consist of:
29
30* Implementing a platform-specific function or variable,
31* Setting up the execution context in a certain way, or
32* Defining certain constants (for example #defines).
33
Dan Handleyb68954c2014-05-29 12:30:24 +010034The platform-specific functions and variables are all declared in
35[include/plat/common/platform.h]. The firmware provides a default implementation
36of variables and functions to fulfill the optional requirements. These
37implementations are all weakly defined; they are provided to ease the porting
38effort. Each platform port can override them with its own implementation if the
39default implementation is inadequate.
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
41Some modifications are common to all Boot Loader (BL) stages. Section 2
42discusses these in detail. The subsequent sections discuss the remaining
43modifications for each BL stage in detail.
44
45This document should be read in conjunction with the ARM Trusted Firmware
46[User Guide].
47
48
492. Common modifications
50------------------------
51
52This section covers the modifications that should be made by the platform for
53each BL stage to correctly port the firmware stack. They are categorized as
54either mandatory or optional.
55
56
572.1 Common mandatory modifications
58----------------------------------
59A platform port must enable the Memory Management Unit (MMU) with identity
60mapped page tables, and enable both the instruction and data caches for each BL
61stage. In the ARM FVP port, each BL stage configures the MMU in its platform-
62specific architecture setup function, for example `blX_plat_arch_setup()`.
63
64Each platform must allocate a block of identity mapped secure memory with
65Device-nGnRE attributes aligned to page boundary (4K) for each BL stage. This
66memory is identified by the section name `tzfw_coherent_mem` so that its
67possible for the firmware to place variables in it using the following C code
68directive:
69
70 __attribute__ ((section("tzfw_coherent_mem")))
71
72Or alternatively the following assembler code directive:
73
74 .section tzfw_coherent_mem
75
76The `tzfw_coherent_mem` section is used to allocate any data structures that are
77accessed both when a CPU is executing with its MMU and caches enabled, and when
78it's running with its MMU and caches disabled. Examples are given below.
79
80The following variables, functions and constants must be defined by the platform
81for the firmware to work correctly.
82
83
Dan Handleyb68954c2014-05-29 12:30:24 +010084### File : platform_def.h [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +010085
Dan Handleyb68954c2014-05-29 12:30:24 +010086Each platform must ensure that a header file of this name is in the system
87include path with the following constants defined. This may require updating the
88list of `PLAT_INCLUDES` in the `platform.mk` file. In the ARM FVP port, this
89file is found in [plat/fvp/include/platform_def.h].
Achin Gupta4f6ad662013-10-25 09:08:21 +010090
James Morrisseyba3155b2013-10-29 10:56:46 +000091* **#define : PLATFORM_LINKER_FORMAT**
Achin Gupta4f6ad662013-10-25 09:08:21 +010092
93 Defines the linker format used by the platform, for example
94 `elf64-littleaarch64` used by the FVP.
95
James Morrisseyba3155b2013-10-29 10:56:46 +000096* **#define : PLATFORM_LINKER_ARCH**
Achin Gupta4f6ad662013-10-25 09:08:21 +010097
98 Defines the processor architecture for the linker by the platform, for
99 example `aarch64` used by the FVP.
100
James Morrisseyba3155b2013-10-29 10:56:46 +0000101* **#define : PLATFORM_STACK_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100102
103 Defines the normal stack memory available to each CPU. This constant is used
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000104 by [plat/common/aarch64/platform_mp_stack.S] and
105 [plat/common/aarch64/platform_up_stack.S].
106
107* **#define : PCPU_DV_MEM_STACK_SIZE**
108
109 Defines the coherent stack memory available to each CPU. This constant is used
110 by [plat/common/aarch64/platform_mp_stack.S] and
111 [plat/common/aarch64/platform_up_stack.S].
Achin Gupta4f6ad662013-10-25 09:08:21 +0100112
James Morrisseyba3155b2013-10-29 10:56:46 +0000113* **#define : FIRMWARE_WELCOME_STR**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100114
115 Defines the character string printed by BL1 upon entry into the `bl1_main()`
116 function.
117
James Morrisseyba3155b2013-10-29 10:56:46 +0000118* **#define : BL2_IMAGE_NAME**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100119
120 Name of the BL2 binary image on the host file-system. This name is used by
Harry Liebeld265bd72014-01-31 19:04:10 +0000121 BL1 to load BL2 into secure memory from non-volatile storage.
122
123* **#define : BL31_IMAGE_NAME**
124
125 Name of the BL3-1 binary image on the host file-system. This name is used by
126 BL2 to load BL3-1 into secure memory from platform storage.
127
128* **#define : BL33_IMAGE_NAME**
129
130 Name of the BL3-3 binary image on the host file-system. This name is used by
131 BL2 to load BL3-3 into non-secure memory from platform storage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100132
James Morrisseyba3155b2013-10-29 10:56:46 +0000133* **#define : PLATFORM_CACHE_LINE_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100134
135 Defines the size (in bytes) of the largest cache line across all the cache
136 levels in the platform.
137
James Morrisseyba3155b2013-10-29 10:56:46 +0000138* **#define : PLATFORM_CLUSTER_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100139
140 Defines the total number of clusters implemented by the platform in the
141 system.
142
James Morrisseyba3155b2013-10-29 10:56:46 +0000143* **#define : PLATFORM_CORE_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100144
145 Defines the total number of CPUs implemented by the platform across all
146 clusters in the system.
147
James Morrisseyba3155b2013-10-29 10:56:46 +0000148* **#define : PLATFORM_MAX_CPUS_PER_CLUSTER**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149
150 Defines the maximum number of CPUs that can be implemented within a cluster
151 on the platform.
152
Andrew Thoelke6c0b45d2014-06-20 00:36:14 +0100153* **#define : PLATFORM_NUM_AFFS**
154
155 Defines the total number of nodes in the affinity heirarchy at all affinity
156 levels used by the platform.
157
James Morrisseyba3155b2013-10-29 10:56:46 +0000158* **#define : PRIMARY_CPU**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100159
160 Defines the `MPIDR` of the primary CPU on the platform. This value is used
161 after a cold boot to distinguish between primary and secondary CPUs.
162
James Morrisseyba3155b2013-10-29 10:56:46 +0000163* **#define : TZROM_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100164
165 Defines the base address of secure ROM on the platform, where the BL1 binary
166 is loaded. This constant is used by the linker scripts to ensure that the
167 BL1 image fits into the available memory.
168
James Morrisseyba3155b2013-10-29 10:56:46 +0000169* **#define : TZROM_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100170
171 Defines the size of secure ROM on the platform. This constant is used by the
172 linker scripts to ensure that the BL1 image fits into the available memory.
173
James Morrisseyba3155b2013-10-29 10:56:46 +0000174* **#define : TZRAM_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100175
176 Defines the base address of the secure RAM on platform, where the data
177 section of the BL1 binary is loaded. The BL2 and BL3-1 images are also
178 loaded in this secure RAM region. This constant is used by the linker
179 scripts to ensure that the BL1 data section and BL2/BL3-1 binary images fit
180 into the available memory.
181
James Morrisseyba3155b2013-10-29 10:56:46 +0000182* **#define : TZRAM_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100183
184 Defines the size of the secure RAM on the platform. This constant is used by
185 the linker scripts to ensure that the BL1 data section and BL2/BL3-1 binary
186 images fit into the available memory.
187
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100188* **#define : BL1_RO_BASE**
189
190 Defines the base address in secure ROM where BL1 originally lives. Must be
191 aligned on a page-size boundary.
192
193* **#define : BL1_RO_LIMIT**
194
195 Defines the maximum address in secure ROM that BL1's actual content (i.e.
196 excluding any data section allocated at runtime) can occupy.
197
198* **#define : BL1_RW_BASE**
199
200 Defines the base address in secure RAM where BL1's read-write data will live
201 at runtime. Must be aligned on a page-size boundary.
202
203* **#define : BL1_RW_LIMIT**
204
205 Defines the maximum address in secure RAM that BL1's read-write data can
206 occupy at runtime.
207
James Morrisseyba3155b2013-10-29 10:56:46 +0000208* **#define : BL2_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100209
210 Defines the base address in secure RAM where BL1 loads the BL2 binary image.
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000211 Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100212
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100213* **#define : BL2_LIMIT**
214
215 Defines the maximum address in secure RAM that the BL2 image can occupy.
216
James Morrisseyba3155b2013-10-29 10:56:46 +0000217* **#define : BL31_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100218
219 Defines the base address in secure RAM where BL2 loads the BL3-1 binary
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000220 image. Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100221
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100222* **#define : BL31_LIMIT**
223
224 Defines the maximum address in secure RAM that the BL3-1 image can occupy.
225
Harry Liebeld265bd72014-01-31 19:04:10 +0000226* **#define : NS_IMAGE_OFFSET**
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100227
Harry Liebeld265bd72014-01-31 19:04:10 +0000228 Defines the base address in non-secure DRAM where BL2 loads the BL3-3 binary
229 image. Must be aligned on a page-size boundary.
230
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100231If the BL3-2 image is supported by the platform, the following constants must
232be defined as well:
233
234* **#define : TSP_SEC_MEM_BASE**
235
236 Defines the base address of the secure memory used by the BL3-2 image on the
237 platform.
238
239* **#define : TSP_SEC_MEM_SIZE**
240
241 Defines the size of the secure memory used by the BL3-2 image on the
242 platform.
243
244* **#define : BL32_BASE**
245
246 Defines the base address in secure memory where BL2 loads the BL3-2 binary
247 image. Must be inside the secure memory identified by `TSP_SEC_MEM_BASE` and
248 `TSP_SEC_MEM_SIZE` constants. Must also be aligned on a page-size boundary.
249
250* **#define : BL32_LIMIT**
251
252 Defines the maximum address that the BL3-2 image can occupy. Must be inside
253 the secure memory identified by `TSP_SEC_MEM_BASE` and `TSP_SEC_MEM_SIZE`
254 constants.
255
Sandrine Bailleux46d49f632014-06-23 17:00:23 +0100256The following constants are optional. They should be defined when the platform
257memory layout implies some image overlaying like on FVP.
258
259* **#define : BL31_PROGBITS_LIMIT**
260
261 Defines the maximum address in secure RAM that the BL3-1's progbits sections
262 can occupy.
263
264* **#define : BL32_PROGBITS_LIMIT**
265
266 Defines the maximum address that the TSP's progbits sections can occupy.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100267
Dan Handleyb68954c2014-05-29 12:30:24 +0100268### File : plat_macros.S [mandatory]
Soby Mathewa43d4312014-04-07 15:28:55 +0100269
Dan Handleyb68954c2014-05-29 12:30:24 +0100270Each platform must ensure a file of this name is in the system include path with
271the following macro defined. In the ARM FVP port, this file is found in
272[plat/fvp/include/plat_macros.S].
Soby Mathewa43d4312014-04-07 15:28:55 +0100273
274* **Macro : plat_print_gic_regs**
275
276 This macro allows the crash reporting routine to print GIC registers
277 in case of an unhandled IRQ or FIQ in BL3-1. This aids in debugging and
278 this macro can be defined to be empty in case GIC register reporting is
279 not desired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100280
281### Other mandatory modifications
282
James Morrisseyba3155b2013-10-29 10:56:46 +0000283The following mandatory modifications may be implemented in any file
Achin Gupta4f6ad662013-10-25 09:08:21 +0100284the implementer chooses. In the ARM FVP port, they are implemented in
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000285[plat/fvp/aarch64/plat_common.c].
Achin Gupta4f6ad662013-10-25 09:08:21 +0100286
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100287* **Function : uint64_t plat_get_syscnt_freq(void)**
288
289 This function is used by the architecture setup code to retrieve the
290 counter frequency for the CPU's generic timer. This value will be
291 programmed into the `CNTFRQ_EL0` register.
292 In the ARM FVP port, it returns the base frequency of the system counter,
293 which is retrieved from the first entry in the frequency modes table.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100294
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000295
Vikram Kanigirie452cd82014-05-23 15:56:12 +01002962.2 Handling Reset
297------------------
298
299BL1 by default implements the reset vector where execution starts from a cold
300or warm boot. BL3-1 can be optionally set as a reset vector using the
301RESET_TO_BL31 make variable.
302
303For each CPU, the reset vector code is responsible for the following tasks:
304
3051. Distinguishing between a cold boot and a warm boot.
306
3072. In the case of a cold boot and the CPU being a secondary CPU, ensuring that
308 the CPU is placed in a platform-specific state until the primary CPU
309 performs the necessary steps to remove it from this state.
310
3113. In the case of a warm boot, ensuring that the CPU jumps to a platform-
312 specific address in the BL3-1 image in the same processor mode as it was
313 when released from reset.
314
315The following functions need to be implemented by the platform port to enable
316reset vector code to perform the above tasks.
317
318
319### Function : platform_get_entrypoint() [mandatory]
320
321 Argument : unsigned long
322 Return : unsigned int
323
324This function is called with the `SCTLR.M` and `SCTLR.C` bits disabled. The CPU
325is identified by its `MPIDR`, which is passed as the argument. The function is
326responsible for distinguishing between a warm and cold reset using platform-
327specific means. If it's a warm reset then it returns the entrypoint into the
328BL3-1 image that the CPU must jump to. If it's a cold reset then this function
329must return zero.
330
331This function is also responsible for implementing a platform-specific mechanism
332to handle the condition where the CPU has been warm reset but there is no
333entrypoint to jump to.
334
335This function does not follow the Procedure Call Standard used by the
336Application Binary Interface for the ARM 64-bit architecture. The caller should
337not assume that callee saved registers are preserved across a call to this
338function.
339
340This function fulfills requirement 1 and 3 listed above.
341
342
343### Function : plat_secondary_cold_boot_setup() [mandatory]
344
345 Argument : void
346 Return : void
347
348This function is called with the MMU and data caches disabled. It is responsible
349for placing the executing secondary CPU in a platform-specific state until the
350primary CPU performs the necessary actions to bring it out of that state and
351allow entry into the OS.
352
353In the ARM FVP port, each secondary CPU powers itself off. The primary CPU is
354responsible for powering up the secondary CPU when normal world software
355requires them.
356
357This function fulfills requirement 2 above.
358
359
360### Function : platform_mem_init() [mandatory]
361
362 Argument : void
363 Return : void
364
365This function is called before any access to data is made by the firmware, in
366order to carry out any essential memory initialization.
367
368The ARM FVP port uses this function to initialize the mailbox memory used for
369providing the warm-boot entry-point addresses.
370
371
372
3732.3 Common optional modifications
Achin Gupta4f6ad662013-10-25 09:08:21 +0100374---------------------------------
375
376The following are helper functions implemented by the firmware that perform
377common platform-specific tasks. A platform may choose to override these
378definitions.
379
380
381### Function : platform_get_core_pos()
382
383 Argument : unsigned long
384 Return : int
385
386A platform may need to convert the `MPIDR` of a CPU to an absolute number, which
387can be used as a CPU-specific linear index into blocks of memory (for example
388while allocating per-CPU stacks). This routine contains a simple mechanism
389to perform this conversion, using the assumption that each cluster contains a
390maximum of 4 CPUs:
391
392 linear index = cpu_id + (cluster_id * 4)
393
394 cpu_id = 8-bit value in MPIDR at affinity level 0
395 cluster_id = 8-bit value in MPIDR at affinity level 1
396
397
398### Function : platform_set_coherent_stack()
399
400 Argument : unsigned long
401 Return : void
402
403A platform may need stack memory that is coherent with main memory to perform
404certain operations like:
405
406* Turning the MMU on, or
407* Flushing caches prior to powering down a CPU or cluster.
408
409Each BL stage allocates this coherent stack memory for each CPU in the
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000410`tzfw_coherent_mem` section.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100411
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000412This function sets the current stack pointer to the coherent stack that
413has been allocated for the CPU specified 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
Achin Gupta4f6ad662013-10-25 09:08:21 +0100416`PCPU_DV_MEM_STACK_SIZE`.
417
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]
421
Achin Gupta4f6ad662013-10-25 09:08:21 +0100422
423### Function : platform_is_primary_cpu()
424
425 Argument : unsigned long
426 Return : unsigned int
427
428This function identifies a CPU by its `MPIDR`, which is passed as the argument,
429to determine whether this CPU is the primary CPU or a secondary CPU. A return
430value of zero indicates that the CPU is not the primary CPU, while a non-zero
431return value indicates that the CPU is the primary CPU.
432
433
434### Function : platform_set_stack()
435
436 Argument : unsigned long
437 Return : void
438
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000439This function sets the current stack pointer to the normal memory stack that
440has been allocated for the CPU specificed by MPIDR. For BL images that only
441require a stack for the primary CPU the parameter is ignored. The size of
442the stack allocated to each CPU is specified by the platform defined constant
443`PLATFORM_STACK_SIZE`.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100444
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000445Common implementations of this function for the UP and MP BL images are
446provided in [plat/common/aarch64/platform_up_stack.S] and
447[plat/common/aarch64/platform_mp_stack.S]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100448
449
Achin Guptac8afc782013-11-25 18:45:02 +0000450### Function : platform_get_stack()
451
452 Argument : unsigned long
453 Return : unsigned long
454
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000455This function returns the base address of the normal memory stack that
456has been allocated for the CPU specificed by MPIDR. For BL images that only
457require a stack for the primary CPU the parameter is ignored. The size of
458the stack allocated to each CPU is specified by the platform defined constant
459`PLATFORM_STACK_SIZE`.
Achin Guptac8afc782013-11-25 18:45:02 +0000460
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000461Common implementations of this function for the UP and MP BL images are
462provided in [plat/common/aarch64/platform_up_stack.S] and
463[plat/common/aarch64/platform_mp_stack.S]
Achin Guptac8afc782013-11-25 18:45:02 +0000464
465
Achin Gupta4f6ad662013-10-25 09:08:21 +0100466### Function : plat_report_exception()
467
468 Argument : unsigned int
469 Return : void
470
471A platform may need to report various information about its status when an
472exception is taken, for example the current exception level, the CPU security
473state (secure/non-secure), the exception type, and so on. This function is
474called in the following circumstances:
475
476* In BL1, whenever an exception is taken.
477* In BL2, whenever an exception is taken.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100478
479The default implementation doesn't do anything, to avoid making assumptions
480about the way the platform displays its status information.
481
482This function receives the exception type as its argument. Possible values for
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000483exceptions types are listed in the [include/runtime_svc.h] header file. Note
Achin Gupta4f6ad662013-10-25 09:08:21 +0100484that these constants are not related to any architectural exception code; they
485are just an ARM Trusted Firmware convention.
486
487
4883. Modifications specific to a Boot Loader stage
489-------------------------------------------------
490
4913.1 Boot Loader Stage 1 (BL1)
492-----------------------------
493
494BL1 implements the reset vector where execution starts from after a cold or
495warm boot. For each CPU, BL1 is responsible for the following tasks:
496
Vikram Kanigirie452cd82014-05-23 15:56:12 +01004971. Handling the reset as described in section 2.2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100498
4992. In the case of a cold boot and the CPU being the primary CPU, ensuring that
500 only this CPU executes the remaining BL1 code, including loading and passing
501 control to the BL2 stage.
502
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005033. Loading the BL2 image from non-volatile storage into secure memory at the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100504 address specified by the platform defined constant `BL2_BASE`.
505
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005064. Populating a `meminfo` structure with the following information in memory,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100507 accessible by BL2 immediately upon entry.
508
509 meminfo.total_base = Base address of secure RAM visible to BL2
510 meminfo.total_size = Size of secure RAM visible to BL2
511 meminfo.free_base = Base address of secure RAM available for
512 allocation to BL2
513 meminfo.free_size = Size of secure RAM available for allocation to BL2
514
515 BL1 places this `meminfo` structure at the beginning of the free memory
516 available for its use. Since BL1 cannot allocate memory dynamically at the
517 moment, its free memory will be available for BL2's use as-is. However, this
518 means that BL2 must read the `meminfo` structure before it starts using its
519 free memory (this is discussed in Section 3.2).
520
521 In future releases of the ARM Trusted Firmware it will be possible for
522 the platform to decide where it wants to place the `meminfo` structure for
523 BL2.
524
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100525 BL1 implements the `bl1_init_bl2_mem_layout()` function to populate the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100526 BL2 `meminfo` structure. The platform may override this implementation, for
527 example if the platform wants to restrict the amount of memory visible to
528 BL2. Details of how to do this are given below.
529
530The following functions need to be implemented by the platform port to enable
531BL1 to perform the above tasks.
532
533
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100534### Function : bl1_plat_arch_setup() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100535
536 Argument : void
537 Return : void
538
Achin Gupta4f6ad662013-10-25 09:08:21 +0100539This function performs any platform-specific and architectural setup that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100540platform requires. Platform-specific setup might include configuration of
541memory controllers, configuration of the interconnect to allow the cluster
542to service cache snoop requests from another cluster, and so on.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100543
544In the ARM FVP port, this function enables CCI snoops into the cluster that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100545primary CPU is part of. It also enables the MMU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100546
547This function helps fulfill requirement 2 above.
548
549
550### Function : bl1_platform_setup() [mandatory]
551
552 Argument : void
553 Return : void
554
555This function executes with the MMU and data caches enabled. It is responsible
556for performing any remaining platform-specific setup that can occur after the
557MMU and data cache have been enabled.
558
Harry Liebeld265bd72014-01-31 19:04:10 +0000559This function is also responsible for initializing the storage abstraction layer
560which is used to load further bootloader images.
561
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100562This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100563
564
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000565### Function : bl1_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100566
567 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000568 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100569
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000570This function should only be called on the cold boot path. It executes with the
571MMU and data caches enabled. The pointer returned by this function must point to
572a `meminfo` structure containing the extents and availability of secure RAM for
573the BL1 stage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100574
575 meminfo.total_base = Base address of secure RAM visible to BL1
576 meminfo.total_size = Size of secure RAM visible to BL1
577 meminfo.free_base = Base address of secure RAM available for allocation
578 to BL1
579 meminfo.free_size = Size of secure RAM available for allocation to BL1
580
581This information is used by BL1 to load the BL2 image in secure RAM. BL1 also
582populates a similar structure to tell BL2 the extents of memory available for
583its own use.
584
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100585This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100586
587
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100588### Function : bl1_init_bl2_mem_layout() [optional]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100589
590 Argument : meminfo *, meminfo *, unsigned int, unsigned long
591 Return : void
592
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100593BL1 needs to tell the next stage the amount of secure RAM available
594for it to use. This information is populated in a `meminfo`
Achin Gupta4f6ad662013-10-25 09:08:21 +0100595structure.
596
597Depending upon where BL2 has been loaded in secure RAM (determined by
598`BL2_BASE`), BL1 calculates the amount of free memory available for BL2 to use.
599BL1 also ensures that its data sections resident in secure RAM are not visible
600to BL2. An illustration of how this is done in the ARM FVP port is given in the
601[User Guide], in the Section "Memory layout on Base FVP".
602
603
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100604### Function : bl1_plat_set_bl2_ep_info() [mandatory]
605
606 Argument : image_info *, entry_point_info *
607 Return : void
608
609This function is called after loading BL2 image and it can be used to overwrite
610the entry point set by loader and also set the security state and SPSR which
611represents the entry point system state for BL2.
612
613On FVP, we are setting the security state and the SPSR for the BL2 entrypoint
614
615
Achin Gupta4f6ad662013-10-25 09:08:21 +01006163.2 Boot Loader Stage 2 (BL2)
617-----------------------------
618
619The BL2 stage is executed only by the primary CPU, which is determined in BL1
620using the `platform_is_primary_cpu()` function. BL1 passed control to BL2 at
621`BL2_BASE`. BL2 executes in Secure EL1 and is responsible for:
622
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006231. (Optional) Loading the BL3-0 binary image (if present) from platform
624 provided non-volatile storage. To load the BL3-0 image, BL2 makes use of
625 the `meminfo` returned by the `bl2_plat_get_bl30_meminfo()` function.
626 The platform also defines the address in memory where BL3-0 is loaded
627 through the optional constant `BL30_BASE`. BL2 uses this information
628 to determine if there is enough memory to load the BL3-0 image.
629 Subsequent handling of the BL3-0 image is platform-specific and is
630 implemented in the `bl2_plat_handle_bl30()` function.
631 If `BL30_BASE` is not defined then this step is not performed.
632
6332. Loading the BL3-1 binary image into secure RAM from non-volatile storage. To
Harry Liebeld265bd72014-01-31 19:04:10 +0000634 load the BL3-1 image, BL2 makes use of the `meminfo` structure passed to it
635 by BL1. This structure allows BL2 to calculate how much secure RAM is
636 available for its use. The platform also defines the address in secure RAM
637 where BL3-1 is loaded through the constant `BL31_BASE`. BL2 uses this
638 information to determine if there is enough memory to load the BL3-1 image.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100639
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006403. (Optional) Loading the BL3-2 binary image (if present) from platform
Dan Handley1151c822014-04-15 11:38:38 +0100641 provided non-volatile storage. To load the BL3-2 image, BL2 makes use of
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100642 the `meminfo` returned by the `bl2_plat_get_bl32_meminfo()` function.
643 The platform also defines the address in memory where BL3-2 is loaded
644 through the optional constant `BL32_BASE`. BL2 uses this information
645 to determine if there is enough memory to load the BL3-2 image.
646 If `BL32_BASE` is not defined then this and the next step is not performed.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000647
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006484. (Optional) Arranging to pass control to the BL3-2 image (if present) that
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100649 has been pre-loaded at `BL32_BASE`. BL2 populates an `entry_point_info`
Dan Handley1151c822014-04-15 11:38:38 +0100650 structure in memory provided by the platform with information about how
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100651 BL3-1 should pass control to the BL3-2 image.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000652
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006535. Loading the normal world BL3-3 binary image into non-secure DRAM from
654 platform storage and arranging for BL3-1 to pass control to this image. This
655 address is determined using the `plat_get_ns_image_entrypoint()` function
656 described below.
657
6586. BL2 populates an `entry_point_info` structure in memory provided by the
659 platform with information about how BL3-1 should pass control to the
660 other BL images.
661
Achin Gupta4f6ad662013-10-25 09:08:21 +0100662The following functions must be implemented by the platform port to enable BL2
663to perform the above tasks.
664
665
666### Function : bl2_early_platform_setup() [mandatory]
667
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100668 Argument : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100669 Return : void
670
671This function executes with the MMU and data caches disabled. It is only called
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100672by the primary CPU. The arguments to this function is the address of the
673`meminfo` structure populated by BL1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100674
675The platform must copy the contents of the `meminfo` structure into a private
676variable as the original memory may be subsequently overwritten by BL2. The
677copied structure is made available to all BL2 code through the
Achin Guptae4d084e2014-02-19 17:18:23 +0000678`bl2_plat_sec_mem_layout()` function.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100679
680
681### Function : bl2_plat_arch_setup() [mandatory]
682
683 Argument : void
684 Return : void
685
686This function executes with the MMU and data caches disabled. It is only called
687by the primary CPU.
688
689The purpose of this function is to perform any architectural initialization
690that varies across platforms, for example enabling the MMU (since the memory
691map differs across platforms).
692
693
694### Function : bl2_platform_setup() [mandatory]
695
696 Argument : void
697 Return : void
698
699This function may execute with the MMU and data caches enabled if the platform
700port does the necessary initialization in `bl2_plat_arch_setup()`. It is only
701called by the primary CPU.
702
Achin Guptae4d084e2014-02-19 17:18:23 +0000703The purpose of this function is to perform any platform initialization
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100704specific to BL2. Platform security components are configured if required.
705For the Base FVP the TZC-400 TrustZone controller is configured to only
706grant non-secure access to DRAM. This avoids aliasing between secure and
707non-secure accesses in the TLB and cache - secure execution states can use
708the NS attributes in the MMU translation tables to access the DRAM.
Harry Liebelce19cf12014-04-01 19:28:07 +0100709
Harry Liebeld265bd72014-01-31 19:04:10 +0000710This function is also responsible for initializing the storage abstraction layer
711which is used to load further bootloader images.
712
Achin Gupta4f6ad662013-10-25 09:08:21 +0100713
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000714### Function : bl2_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100715
716 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000717 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100718
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000719This function should only be called on the cold boot path. It may execute with
720the MMU and data caches enabled if the platform port does the necessary
721initialization in `bl2_plat_arch_setup()`. It is only called by the primary CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100722
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000723The purpose of this function is to return a pointer to a `meminfo` structure
724populated with the extents of secure RAM available for BL2 to use. See
Achin Gupta4f6ad662013-10-25 09:08:21 +0100725`bl2_early_platform_setup()` above.
726
727
Sandrine Bailleux93d81d62014-06-24 14:19:36 +0100728### Function : bl2_plat_get_bl30_meminfo() [mandatory]
729
730 Argument : meminfo *
731 Return : void
732
733This function is used to get the memory limits where BL2 can load the
734BL3-0 image. The meminfo provided by this is used by load_image() to
735validate whether the BL3-0 image can be loaded within the given
736memory from the given base.
737
738
739### Function : bl2_plat_handle_bl30() [mandatory]
740
741 Argument : image_info *
742 Return : int
743
744This function is called after loading BL3-0 image and it is used to perform any
745platform-specific actions required to handle the SCP firmware. Typically it
746transfers the image into SCP memory using a platform-specific protocol and waits
747until SCP executes it and signals to the Application Processor (AP) for BL2
748execution to continue.
749
750This function returns 0 on success, a negative error code otherwise.
751
752
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100753### Function : bl2_plat_get_bl31_params() [mandatory]
Harry Liebeld265bd72014-01-31 19:04:10 +0000754
755 Argument : void
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100756 Return : bl31_params *
Harry Liebeld265bd72014-01-31 19:04:10 +0000757
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100758BL2 platform code needs to return a pointer to a `bl31_params` structure it
759will use for passing information to BL3-1. The `bl31_params` structure carries
760the following information.
761 - Header describing the version information for interpreting the bl31_param
762 structure
763 - Information about executing the BL3-3 image in the `bl33_ep_info` field
764 - Information about executing the BL3-2 image in the `bl32_ep_info` field
765 - Information about the type and extents of BL3-1 image in the
766 `bl31_image_info` field
767 - Information about the type and extents of BL3-2 image in the
768 `bl32_image_info` field
769 - Information about the type and extents of BL3-3 image in the
770 `bl33_image_info` field
771
772The memory pointed by this structure and its sub-structures should be
773accessible from BL3-1 initialisation code. BL3-1 might choose to copy the
774necessary content, or maintain the structures until BL3-3 is initialised.
Harry Liebeld265bd72014-01-31 19:04:10 +0000775
776
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100777### Funtion : bl2_plat_get_bl31_ep_info() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100778
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100779 Argument : void
780 Return : entry_point_info *
781
782BL2 platform code returns a pointer which is used to populate the entry point
783information for BL3-1 entry point. The location pointed by it should be
784accessible from BL1 while processing the synchronous exception to run to BL3-1.
785
786On FVP this is allocated inside an bl2_to_bl31_params_mem structure which
787is allocated at an address pointed by PARAMS_BASE.
788
789
790### Function : bl2_plat_set_bl31_ep_info() [mandatory]
791
792 Argument : image_info *, entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100793 Return : void
794
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100795This function is called after loading BL3-1 image and it can be used to
796overwrite the entry point set by loader and also set the security state
797and SPSR which represents the entry point system state for BL3-1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100798
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100799On FVP, we are setting the security state and the SPSR for the BL3-1
800entrypoint.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100801
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100802### Function : bl2_plat_set_bl32_ep_info() [mandatory]
803
804 Argument : image_info *, entry_point_info *
805 Return : void
806
807This function is called after loading BL3-2 image and it can be used to
808overwrite the entry point set by loader and also set the security state
809and SPSR which represents the entry point system state for BL3-2.
810
811On FVP, we are setting the security state and the SPSR for the BL3-2
812entrypoint
813
814### Function : bl2_plat_set_bl33_ep_info() [mandatory]
815
816 Argument : image_info *, entry_point_info *
817 Return : void
818
819This function is called after loading BL3-3 image and it can be used to
820overwrite the entry point set by loader and also set the security state
821and SPSR which represents the entry point system state for BL3-3.
822
823On FVP, we are setting the security state and the SPSR for the BL3-3
824entrypoint
825
826### Function : bl2_plat_get_bl32_meminfo() [mandatory]
827
828 Argument : meminfo *
829 Return : void
830
831This function is used to get the memory limits where BL2 can load the
832BL3-2 image. The meminfo provided by this is used by load_image() to
833validate whether the BL3-2 image can be loaded with in the given
834memory from the given base.
835
836### Function : bl2_plat_get_bl33_meminfo() [mandatory]
837
838 Argument : meminfo *
839 Return : void
840
841This function is used to get the memory limits where BL2 can load the
842BL3-3 image. The meminfo provided by this is used by load_image() to
843validate whether the BL3-3 image can be loaded with in the given
844memory from the given base.
845
846### Function : bl2_plat_flush_bl31_params() [mandatory]
847
848 Argument : void
849 Return : void
850
851Once BL2 has populated all the structures that needs to be read by BL1
852and BL3-1 including the bl31_params structures and its sub-structures,
853the bl31_ep_info structure and any platform specific data. It flushes
854all these data to the main memory so that it is available when we jump to
855later Bootloader stages with MMU off
Achin Gupta4f6ad662013-10-25 09:08:21 +0100856
857### Function : plat_get_ns_image_entrypoint() [mandatory]
858
859 Argument : void
860 Return : unsigned long
861
862As previously described, BL2 is responsible for arranging for control to be
863passed to a normal world BL image through BL3-1. This function returns the
864entrypoint of that image, which BL3-1 uses to jump to it.
865
Harry Liebeld265bd72014-01-31 19:04:10 +0000866BL2 is responsible for loading the normal world BL3-3 image (e.g. UEFI).
Achin Gupta4f6ad662013-10-25 09:08:21 +0100867
868
8693.2 Boot Loader Stage 3-1 (BL3-1)
870---------------------------------
871
872During cold boot, the BL3-1 stage is executed only by the primary CPU. This is
873determined in BL1 using the `platform_is_primary_cpu()` function. BL1 passes
874control to BL3-1 at `BL31_BASE`. During warm boot, BL3-1 is executed by all
875CPUs. BL3-1 executes at EL3 and is responsible for:
876
8771. Re-initializing all architectural and platform state. Although BL1 performs
878 some of this initialization, BL3-1 remains resident in EL3 and must ensure
879 that EL3 architectural and platform state is completely initialized. It
880 should make no assumptions about the system state when it receives control.
881
8822. Passing control to a normal world BL image, pre-loaded at a platform-
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100883 specific address by BL2. BL3-1 uses the `entry_point_info` structure that BL2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100884 populated in memory to do this.
885
8863. Providing runtime firmware services. Currently, BL3-1 only implements a
887 subset of the Power State Coordination Interface (PSCI) API as a runtime
888 service. See Section 3.3 below for details of porting the PSCI
889 implementation.
890
Achin Gupta35ca3512014-02-19 17:58:33 +00008914. Optionally passing control to the BL3-2 image, pre-loaded at a platform-
892 specific address by BL2. BL3-1 exports a set of apis that allow runtime
893 services to specify the security state in which the next image should be
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100894 executed and run the corresponding image. BL3-1 uses the `entry_point_info`
895 structure populated by BL2 to do this.
896
897If BL3-1 is a reset vector, It also needs to handle the reset as specified in
898section 2.2 before the tasks described above.
Achin Gupta35ca3512014-02-19 17:58:33 +0000899
Achin Gupta4f6ad662013-10-25 09:08:21 +0100900The following functions must be implemented by the platform port to enable BL3-1
901to perform the above tasks.
902
903
904### Function : bl31_early_platform_setup() [mandatory]
905
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100906 Argument : bl31_params *, void *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100907 Return : void
908
909This function executes with the MMU and data caches disabled. It is only called
910by the primary CPU. The arguments to this function are:
911
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100912* The address of the `bl31_params` structure populated by BL2.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100913* An opaque pointer that the platform may use as needed.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100914
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100915The platform can copy the contents of the `bl31_params` structure and its
916sub-structures into private variables if the original memory may be
917subsequently overwritten by BL3-1 and similarly the `void *` pointing
918to the platform data also needs to be saved.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100919
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100920On the ARM FVP port, BL2 passes a pointer to a `bl31_params` structure populated
921in the secure DRAM at address `0x6000000` in the bl31_params * argument and it
922does not use opaque pointer mentioned earlier. BL3-1 does not copy this
923information to internal data structures as it guarantees that the secure
924DRAM memory will not be overwritten. It maintains an internal reference to this
925information in the `bl2_to_bl31_params` variable.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100926
927### Function : bl31_plat_arch_setup() [mandatory]
928
929 Argument : void
930 Return : void
931
932This function executes with the MMU and data caches disabled. It is only called
933by the primary CPU.
934
935The purpose of this function is to perform any architectural initialization
936that varies across platforms, for example enabling the MMU (since the memory
937map differs across platforms).
938
939
940### Function : bl31_platform_setup() [mandatory]
941
942 Argument : void
943 Return : void
944
945This function may execute with the MMU and data caches enabled if the platform
946port does the necessary initialization in `bl31_plat_arch_setup()`. It is only
947called by the primary CPU.
948
949The purpose of this function is to complete platform initialization so that both
950BL3-1 runtime services and normal world software can function correctly.
951
952The ARM FVP port does the following:
953* Initializes the generic interrupt controller.
954* Configures the CLCD controller.
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100955* Enables system-level implementation of the generic timer counter.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100956* Grants access to the system counter timer module
957* Initializes the FVP power controller device
958* Detects the system topology.
959
960
961### Function : bl31_get_next_image_info() [mandatory]
962
Achin Gupta35ca3512014-02-19 17:58:33 +0000963 Argument : unsigned int
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100964 Return : entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100965
966This function may execute with the MMU and data caches enabled if the platform
967port does the necessary initializations in `bl31_plat_arch_setup()`.
968
969This function is called by `bl31_main()` to retrieve information provided by
Achin Gupta35ca3512014-02-19 17:58:33 +0000970BL2 for the next image in the security state specified by the argument. BL3-1
971uses this information to pass control to that image in the specified security
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100972state. This function must return a pointer to the `entry_point_info` structure
Achin Gupta35ca3512014-02-19 17:58:33 +0000973(that was copied during `bl31_early_platform_setup()`) if the image exists. It
974should return NULL otherwise.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100975
976
Achin Gupta4f6ad662013-10-25 09:08:21 +01009773.3 Power State Coordination Interface (in BL3-1)
978------------------------------------------------
979
980The ARM Trusted Firmware's implementation of the PSCI API is based around the
981concept of an _affinity instance_. Each _affinity instance_ can be uniquely
982identified in a system by a CPU ID (the processor `MPIDR` is used in the PSCI
983interface) and an _affinity level_. A processing element (for example, a
984CPU) is at level 0. If the CPUs in the system are described in a tree where the
985node above a CPU is a logical grouping of CPUs that share some state, then
986affinity level 1 is that group of CPUs (for example, a cluster), and affinity
987level 2 is a group of clusters (for example, the system). The implementation
988assumes that the affinity level 1 ID can be computed from the affinity level 0
989ID (for example, a unique cluster ID can be computed from the CPU ID). The
990current implementation computes this on the basis of the recommended use of
991`MPIDR` affinity fields in the ARM Architecture Reference Manual.
992
993BL3-1's platform initialization code exports a pointer to the platform-specific
994power management operations required for the PSCI implementation to function
995correctly. This information is populated in the `plat_pm_ops` structure. The
996PSCI implementation calls members of the `plat_pm_ops` structure for performing
997power management operations for each affinity instance. For example, the target
998CPU is specified by its `MPIDR` in a PSCI `CPU_ON` call. The `affinst_on()`
999handler (if present) is called for each affinity instance as the PSCI
1000implementation powers up each affinity level implemented in the `MPIDR` (for
1001example, CPU, cluster and system).
1002
1003The following functions must be implemented to initialize PSCI functionality in
1004the ARM Trusted Firmware.
1005
1006
1007### Function : plat_get_aff_count() [mandatory]
1008
1009 Argument : unsigned int, unsigned long
1010 Return : unsigned int
1011
1012This function may execute with the MMU and data caches enabled if the platform
1013port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1014called by the primary CPU.
1015
1016This function is called by the PSCI initialization code to detect the system
1017topology. Its purpose is to return the number of affinity instances implemented
1018at a given `affinity level` (specified by the first argument) and a given
1019`MPIDR` (specified by the second argument). For example, on a dual-cluster
1020system where first cluster implements 2 CPUs and the second cluster implements 4
1021CPUs, a call to this function with an `MPIDR` corresponding to the first cluster
1022(`0x0`) and affinity level 0, would return 2. A call to this function with an
1023`MPIDR` corresponding to the second cluster (`0x100`) and affinity level 0,
1024would return 4.
1025
1026
1027### Function : plat_get_aff_state() [mandatory]
1028
1029 Argument : unsigned int, unsigned long
1030 Return : unsigned int
1031
1032This function may execute with the MMU and data caches enabled if the platform
1033port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1034called by the primary CPU.
1035
1036This function is called by the PSCI initialization code. Its purpose is to
1037return the state of an affinity instance. The affinity instance is determined by
1038the affinity ID at a given `affinity level` (specified by the first argument)
1039and an `MPIDR` (specified by the second argument). The state can be one of
1040`PSCI_AFF_PRESENT` or `PSCI_AFF_ABSENT`. The latter state is used to cater for
1041system topologies where certain affinity instances are unimplemented. For
1042example, consider a platform that implements a single cluster with 4 CPUs and
1043another CPU implemented directly on the interconnect with the cluster. The
1044`MPIDR`s of the cluster would range from `0x0-0x3`. The `MPIDR` of the single
1045CPU would be 0x100 to indicate that it does not belong to cluster 0. Cluster 1
1046is missing but needs to be accounted for to reach this single CPU in the
1047topology tree. Hence it is marked as `PSCI_AFF_ABSENT`.
1048
1049
1050### Function : plat_get_max_afflvl() [mandatory]
1051
1052 Argument : void
1053 Return : int
1054
1055This function may execute with the MMU and data caches enabled if the platform
1056port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1057called by the primary CPU.
1058
1059This function is called by the PSCI implementation both during cold and warm
1060boot, to determine the maximum affinity level that the power management
James Morrisseyba3155b2013-10-29 10:56:46 +00001061operations should apply to. ARMv8-A has support for 4 affinity levels. It is
Achin Gupta4f6ad662013-10-25 09:08:21 +01001062likely that hardware will implement fewer affinity levels. This function allows
1063the PSCI implementation to consider only those affinity levels in the system
1064that the platform implements. For example, the Base AEM FVP implements two
1065clusters with a configurable number of CPUs. It reports the maximum affinity
1066level as 1, resulting in PSCI power control up to the cluster level.
1067
1068
1069### Function : platform_setup_pm() [mandatory]
1070
1071 Argument : plat_pm_ops **
1072 Return : int
1073
1074This function may execute with the MMU and data caches enabled if the platform
1075port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1076called by the primary CPU.
1077
1078This function is called by PSCI initialization code. Its purpose is to export
1079handler routines for platform-specific power management actions by populating
1080the passed pointer with a pointer to BL3-1's private `plat_pm_ops` structure.
1081
1082A description of each member of this structure is given below. Please refer to
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001083the ARM FVP specific implementation of these handlers in [plat/fvp/plat_pm.c]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001084as an example. A platform port may choose not implement some of the power
1085management operations. For example, the ARM FVP port does not implement the
1086`affinst_standby()` function.
1087
1088#### plat_pm_ops.affinst_standby()
1089
1090Perform the platform-specific setup to enter the standby state indicated by the
1091passed argument.
1092
1093#### plat_pm_ops.affinst_on()
1094
1095Perform the platform specific setup to power on an affinity instance, specified
1096by the `MPIDR` (first argument) and `affinity level` (fourth argument). The
1097`state` (fifth argument) contains the current state of that affinity instance
1098(ON or OFF). This is useful to determine whether any action must be taken. For
1099example, while powering on a CPU, the cluster that contains this CPU might
1100already be in the ON state. The platform decides what actions must be taken to
1101transition from the current state to the target state (indicated by the power
1102management operation).
1103
1104#### plat_pm_ops.affinst_off()
1105
1106Perform the platform specific setup to power off an affinity instance in the
1107`MPIDR` of the calling CPU. It is called by the PSCI `CPU_OFF` API
1108implementation.
1109
1110The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1111(third argument) have a similar meaning as described in the `affinst_on()`
1112operation. They are used to identify the affinity instance on which the call
1113is made and its current state. This gives the platform port an indication of the
1114state transition it must make to perform the requested action. For example, if
1115the calling CPU is the last powered on CPU in the cluster, after powering down
1116affinity level 0 (CPU), the platform port should power down affinity level 1
1117(the cluster) as well.
1118
1119This function is called with coherent stacks. This allows the PSCI
1120implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001121stale stack state after turning off the caches. On ARMv8-A cache hits do not
1122occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001123
1124#### plat_pm_ops.affinst_suspend()
1125
1126Perform the platform specific setup to power off an affinity instance in the
1127`MPIDR` of the calling CPU. It is called by the PSCI `CPU_SUSPEND` API
1128implementation.
1129
1130The `MPIDR` (first argument), `affinity level` (third argument) and `state`
1131(fifth argument) have a similar meaning as described in the `affinst_on()`
1132operation. They are used to identify the affinity instance on which the call
1133is made and its current state. This gives the platform port an indication of the
1134state transition it must make to perform the requested action. For example, if
1135the calling CPU is the last powered on CPU in the cluster, after powering down
1136affinity level 0 (CPU), the platform port should power down affinity level 1
1137(the cluster) as well.
1138
1139The difference between turning an affinity instance off versus suspending it
1140is that in the former case, the affinity instance is expected to re-initialize
1141its state when its next powered on (see `affinst_on_finish()`). In the latter
1142case, the affinity instance is expected to save enough state so that it can
1143resume execution by restoring this state when its powered on (see
1144`affinst_suspend_finish()`).
1145
1146This function is called with coherent stacks. This allows the PSCI
1147implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001148stale stack state after turning off the caches. On ARMv8-A cache hits do not
1149occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001150
1151#### plat_pm_ops.affinst_on_finish()
1152
1153This function is called by the PSCI implementation after the calling CPU is
1154powered on and released from reset in response to an earlier PSCI `CPU_ON` call.
1155It performs the platform-specific setup required to initialize enough state for
1156this CPU to enter the normal world and also provide secure runtime firmware
1157services.
1158
1159The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1160(third argument) have a similar meaning as described in the previous operations.
1161
1162This function is called with coherent stacks. This allows the PSCI
1163implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001164stale stack state after turning off the caches. On ARMv8-A cache hits do not
1165occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001166
1167#### plat_pm_ops.affinst_on_suspend()
1168
1169This function is called by the PSCI implementation after the calling CPU is
1170powered on and released from reset in response to an asynchronous wakeup
1171event, for example a timer interrupt that was programmed by the CPU during the
1172`CPU_SUSPEND` call. It performs the platform-specific setup required to
1173restore the saved state for this CPU to resume execution in the normal world
1174and also provide secure runtime firmware services.
1175
1176The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1177(third argument) have a similar meaning as described in the previous operations.
1178
1179This function is called with coherent stacks. This allows the PSCI
1180implementation to flush caches at a given affinity level without running into
James Morrisseyba3155b2013-10-29 10:56:46 +00001181stale stack state after turning off the caches. On ARMv8-A cache hits do not
1182occur after the cache has been turned off.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001183
1184BL3-1 platform initialization code must also detect the system topology and
1185the state of each affinity instance in the topology. This information is
1186critical for the PSCI runtime service to function correctly. More details are
1187provided in the description of the `plat_get_aff_count()` and
1188`plat_get_aff_state()` functions above.
1189
Achin Guptaa4fa3cb2014-06-02 22:27:36 +010011903.4 Interrupt Management framework (in BL3-1)
1191----------------------------------------------
1192BL3-1 implements an Interrupt Management Framework (IMF) to manage interrupts
1193generated in either security state and targeted to EL1 or EL2 in the non-secure
1194state or EL3/S-EL1 in the secure state. The design of this framework is
1195described in the [IMF Design Guide]
1196
1197A platform should export the following APIs to support the IMF. The following
1198text briefly describes each api and its implementation on the FVP port. The API
1199implementation depends upon the type of interrupt controller present in the
1200platform. The FVP implements an ARM Generic Interrupt Controller (ARM GIC) as
1201per the version 2.0 of the [ARM GIC Architecture Specification]
1202
1203### Function : plat_interrupt_type_to_line() [mandatory]
1204
1205 Argument : uint32_t, uint32_t
1206 Return : uint32_t
1207
1208The ARM processor signals an interrupt exception either through the IRQ or FIQ
1209interrupt line. The specific line that is signaled depends on how the interrupt
1210controller (IC) reports different interrupt types from an execution context in
1211either security state. The IMF uses this API to determine which interrupt line
1212the platform IC uses to signal each type of interrupt supported by the framework
1213from a given security state.
1214
1215The first parameter will be one of the `INTR_TYPE_*` values (see [IMF Design
1216Guide]) indicating the target type of the interrupt, the second parameter is the
1217security state of the originating execution context. The return result is the
1218bit position in the `SCR_EL3` register of the respective interrupt trap: IRQ=1,
1219FIQ=2.
1220
1221The FVP port configures the ARM GIC to signal S-EL1 interrupts as FIQs and
1222Non-secure interrupts as IRQs from either security state.
1223
1224
1225### Function : plat_ic_get_pending_interrupt_type() [mandatory]
1226
1227 Argument : void
1228 Return : uint32_t
1229
1230This API returns the type of the highest priority pending interrupt at the
1231platform IC. The IMF uses the interrupt type to retrieve the corresponding
1232handler function. `INTR_TYPE_INVAL` is returned when there is no interrupt
1233pending. The valid interrupt types that can be returned are `INTR_TYPE_EL3`,
1234`INTR_TYPE_S_EL1` and `INTR_TYPE_NS`.
1235
1236The FVP port reads the _Highest Priority Pending Interrupt Register_
1237(`GICC_HPPIR`) to determine the id of the pending interrupt. The type of interrupt
1238depends upon the id value as follows.
1239
12401. id < 1022 is reported as a S-EL1 interrupt
12412. id = 1022 is reported as a Non-secure interrupt.
12423. id = 1023 is reported as an invalid interrupt type.
1243
1244
1245### Function : plat_ic_get_pending_interrupt_id() [mandatory]
1246
1247 Argument : void
1248 Return : uint32_t
1249
1250This API returns the id of the highest priority pending interrupt at the
1251platform IC. The IMF passes the id returned by this API to the registered
1252handler for the pending interrupt if the `IMF_READ_INTERRUPT_ID` build time flag
1253is set. INTR_ID_UNAVAILABLE is returned when there is no interrupt pending.
1254
1255The FVP port reads the _Highest Priority Pending Interrupt Register_
1256(`GICC_HPPIR`) to determine the id of the pending interrupt. The id that is
1257returned by API depends upon the value of the id read from the interrupt
1258controller as follows.
1259
12601. id < 1022. id is returned as is.
12612. id = 1022. The _Aliased Highest Priority Pending Interrupt Register_
1262 (`GICC_AHPPIR`) is read to determine the id of the non-secure interrupt. This
1263 id is returned by the API.
12643. id = 1023. `INTR_ID_UNAVAILABLE` is returned.
1265
1266
1267### Function : plat_ic_acknowledge_interrupt() [mandatory]
1268
1269 Argument : void
1270 Return : uint32_t
1271
1272This API is used by the CPU to indicate to the platform IC that processing of
1273the highest pending interrupt has begun. It should return the id of the
1274interrupt which is being processed.
1275
1276The FVP port reads the _Interrupt Acknowledge Register_ (`GICC_IAR`). This
1277changes the state of the highest priority pending interrupt from pending to
1278active in the interrupt controller. It returns the value read from the
1279`GICC_IAR`. This value is the id of the interrupt whose state has been changed.
1280
1281The TSP uses this API to start processing of the secure physical timer
1282interrupt.
1283
1284
1285### Function : plat_ic_end_of_interrupt() [mandatory]
1286
1287 Argument : uint32_t
1288 Return : void
1289
1290This API is used by the CPU to indicate to the platform IC that processing of
1291the interrupt corresponding to the id (passed as the parameter) has
1292finished. The id should be the same as the id returned by the
1293`plat_ic_acknowledge_interrupt()` API.
1294
1295The FVP port writes the id to the _End of Interrupt Register_
1296(`GICC_EOIR`). This deactivates the corresponding interrupt in the interrupt
1297controller.
1298
1299The TSP uses this API to finish processing of the secure physical timer
1300interrupt.
1301
1302
1303### Function : plat_ic_get_interrupt_type() [mandatory]
1304
1305 Argument : uint32_t
1306 Return : uint32_t
1307
1308This API returns the type of the interrupt id passed as the parameter.
1309`INTR_TYPE_INVAL` is returned if the id is invalid. If the id is valid, a valid
1310interrupt type (one of `INTR_TYPE_EL3`, `INTR_TYPE_S_EL1` and `INTR_TYPE_NS`) is
1311returned depending upon how the interrupt has been configured by the platform
1312IC.
1313
1314The FVP port configures S-EL1 interrupts as Group0 interrupts and Non-secure
1315interrupts as Group1 interrupts. It reads the group value corresponding to the
1316interrupt id from the relevant _Interrupt Group Register_ (`GICD_IGROUPRn`). It
1317uses the group value to determine the type of interrupt.
1318
Achin Gupta4f6ad662013-10-25 09:08:21 +01001319
Harry Liebela960f282013-12-12 16:03:44 +000013204. C Library
1321-------------
1322
1323To avoid subtle toolchain behavioral dependencies, the header files provided
1324by the compiler are not used. The software is built with the `-nostdinc` flag
1325to ensure no headers are included from the toolchain inadvertently. Instead the
1326required headers are included in the ARM Trusted Firmware source tree. The
1327library only contains those C library definitions required by the local
1328implementation. If more functionality is required, the needed library functions
1329will need to be added to the local implementation.
1330
1331Versions of [FreeBSD] headers can be found in `include/stdlib`. Some of these
1332headers have been cut down in order to simplify the implementation. In order to
1333minimize changes to the header files, the [FreeBSD] layout has been maintained.
1334The generic C library definitions can be found in `include/stdlib` with more
1335system and machine specific declarations in `include/stdlib/sys` and
1336`include/stdlib/machine`.
1337
1338The local C library implementations can be found in `lib/stdlib`. In order to
1339extend the C library these files may need to be modified. It is recommended to
1340use a release version of [FreeBSD] as a starting point.
1341
1342The C library header files in the [FreeBSD] source tree are located in the
1343`include` and `sys/sys` directories. [FreeBSD] machine specific definitions
1344can be found in the `sys/<machine-type>` directories. These files define things
1345like 'the size of a pointer' and 'the range of an integer'. Since an AArch64
1346port for [FreeBSD] does not yet exist, the machine specific definitions are
1347based on existing machine types with similar properties (for example SPARC64).
1348
1349Where possible, C library function implementations were taken from [FreeBSD]
1350as found in the `lib/libc` directory.
1351
1352A copy of the [FreeBSD] sources can be downloaded with `git`.
1353
1354 git clone git://github.com/freebsd/freebsd.git -b origin/release/9.2.0
1355
1356
Harry Liebeld265bd72014-01-31 19:04:10 +000013575. Storage abstraction layer
1358-----------------------------
1359
1360In order to improve platform independence and portability an storage abstraction
1361layer is used to load data from non-volatile platform storage.
1362
1363Each platform should register devices and their drivers via the Storage layer.
1364These drivers then need to be initialized by bootloader phases as
1365required in their respective `blx_platform_setup()` functions. Currently
1366storage access is only required by BL1 and BL2 phases. The `load_image()`
1367function uses the storage layer to access non-volatile platform storage.
1368
1369It is mandatory to implement at least one storage driver. For the FVP the
1370Firmware Image Package(FIP) driver is provided as the default means to load data
1371from storage (see the "Firmware Image Package" section in the [User Guide]).
1372The storage layer is described in the header file `include/io_storage.h`. The
1373implementation of the common library is in `lib/io_storage.c` and the driver
1374files are located in `drivers/io/`.
1375
1376Each IO driver must provide `io_dev_*` structures, as described in
1377`drivers/io/io_driver.h`. These are returned via a mandatory registration
1378function that is called on platform initialization. The semi-hosting driver
1379implementation in `io_semihosting.c` can be used as an example.
1380
1381The Storage layer provides mechanisms to initialize storage devices before
1382IO operations are called. The basic operations supported by the layer
1383include `open()`, `close()`, `read()`, `write()`, `size()` and `seek()`.
1384Drivers do not have to implement all operations, but each platform must
1385provide at least one driver for a device capable of supporting generic
1386operations such as loading a bootloader image.
1387
1388The current implementation only allows for known images to be loaded by the
Dan Handleyb68954c2014-05-29 12:30:24 +01001389firmware. These images are specified by using their names, as defined in
1390[include/plat/common/platform.h]. The platform layer (`plat_get_image_source()`)
1391then returns a reference to a device and a driver-specific `spec` which will be
1392understood by the driver to allow access to the image data.
Harry Liebeld265bd72014-01-31 19:04:10 +00001393
1394The layer is designed in such a way that is it possible to chain drivers with
1395other drivers. For example, file-system drivers may be implemented on top of
1396physical block devices, both represented by IO devices with corresponding
1397drivers. In such a case, the file-system "binding" with the block device may
1398be deferred until the file-system device is initialised.
1399
1400The abstraction currently depends on structures being statically allocated
1401by the drivers and callers, as the system does not yet provide a means of
1402dynamically allocating memory. This may also have the affect of limiting the
1403amount of open resources per driver.
1404
1405
Achin Gupta4f6ad662013-10-25 09:08:21 +01001406- - - - - - - - - - - - - - - - - - - - - - - - - -
1407
Dan Handleye83b0ca2014-01-14 18:17:09 +00001408_Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved._
Achin Gupta4f6ad662013-10-25 09:08:21 +01001409
1410
Achin Guptaa4fa3cb2014-06-02 22:27:36 +01001411[ARM GIC Architecture Specification]: http://arminfo.emea.arm.com/help/topic/com.arm.doc.ihi0048b/IHI0048B_gic_architecture_specification.pdf
1412[IMF Design Guide]: interrupt-framework-design.md
1413[User Guide]: user-guide.md
1414[FreeBSD]: http://www.freebsd.org
Achin Gupta4f6ad662013-10-25 09:08:21 +01001415
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001416[plat/common/aarch64/platform_mp_stack.S]: ../plat/common/aarch64/platform_mp_stack.S
1417[plat/common/aarch64/platform_up_stack.S]: ../plat/common/aarch64/platform_up_stack.S
Dan Handleyb68954c2014-05-29 12:30:24 +01001418[plat/fvp/include/platform_def.h]: ../plat/fvp/include/platform_def.h
1419[plat/fvp/include/plat_macros.S]: ../plat/fvp/include/plat_macros.S
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001420[plat/fvp/aarch64/plat_common.c]: ../plat/fvp/aarch64/plat_common.c
1421[plat/fvp/plat_pm.c]: ../plat/fvp/plat_pm.c
1422[include/runtime_svc.h]: ../include/runtime_svc.h
Dan Handleyb68954c2014-05-29 12:30:24 +01001423[include/plat/common/platform.h]: ../include/plat/common/platform.h