blob: ae5e72032a7cb36ccc41e02b123a0694bb2cacb4 [file] [log] [blame] [view]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001ARM Trusted Firmware Porting Guide
2==================================
3
4Contents
5--------
6
71. Introduction
82. Common Modifications
9 * Common mandatory modifications
Vikram Kanigirie452cd82014-05-23 15:56:12 +010010 * Handling reset
Achin Gupta4f6ad662013-10-25 09:08:21 +010011 * Common optional modifications
123. Boot Loader stage specific modifications
13 * Boot Loader stage 1 (BL1)
14 * Boot Loader stage 2 (BL2)
15 * Boot Loader stage 3-1 (BL3-1)
16 * PSCI implementation (in BL3-1)
Achin Guptaa4fa3cb2014-06-02 22:27:36 +010017 * Interrupt Management framework (in BL3-1)
Soby Mathewc67b09b2014-07-14 16:57:23 +010018 * Crash Reporting mechanism (in BL3-1)
Harry Liebeld265bd72014-01-31 19:04:10 +0000194. C Library
205. Storage abstraction layer
Achin Gupta4f6ad662013-10-25 09:08:21 +010021
22- - - - - - - - - - - - - - - - - -
23
241. Introduction
25----------------
26
27Porting the ARM Trusted Firmware to a new platform involves making some
28mandatory and optional modifications for both the cold and warm boot paths.
29Modifications consist of:
30
31* Implementing a platform-specific function or variable,
32* Setting up the execution context in a certain way, or
33* Defining certain constants (for example #defines).
34
Dan Handleyb68954c2014-05-29 12:30:24 +010035The platform-specific functions and variables are all declared in
36[include/plat/common/platform.h]. The firmware provides a default implementation
37of variables and functions to fulfill the optional requirements. These
38implementations are all weakly defined; they are provided to ease the porting
39effort. Each platform port can override them with its own implementation if the
40default implementation is inadequate.
Achin Gupta4f6ad662013-10-25 09:08:21 +010041
42Some modifications are common to all Boot Loader (BL) stages. Section 2
43discusses these in detail. The subsequent sections discuss the remaining
44modifications for each BL stage in detail.
45
46This document should be read in conjunction with the ARM Trusted Firmware
47[User Guide].
48
49
502. Common modifications
51------------------------
52
53This section covers the modifications that should be made by the platform for
54each BL stage to correctly port the firmware stack. They are categorized as
55either mandatory or optional.
56
57
582.1 Common mandatory modifications
59----------------------------------
60A platform port must enable the Memory Management Unit (MMU) with identity
61mapped page tables, and enable both the instruction and data caches for each BL
62stage. In the ARM FVP port, each BL stage configures the MMU in its platform-
63specific architecture setup function, for example `blX_plat_arch_setup()`.
64
65Each platform must allocate a block of identity mapped secure memory with
66Device-nGnRE attributes aligned to page boundary (4K) for each BL stage. This
67memory is identified by the section name `tzfw_coherent_mem` so that its
68possible for the firmware to place variables in it using the following C code
69directive:
70
71 __attribute__ ((section("tzfw_coherent_mem")))
72
73Or alternatively the following assembler code directive:
74
75 .section tzfw_coherent_mem
76
77The `tzfw_coherent_mem` section is used to allocate any data structures that are
78accessed both when a CPU is executing with its MMU and caches enabled, and when
79it's running with its MMU and caches disabled. Examples are given below.
80
81The following variables, functions and constants must be defined by the platform
82for the firmware to work correctly.
83
84
Dan Handleyb68954c2014-05-29 12:30:24 +010085### File : platform_def.h [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +010086
Dan Handleyb68954c2014-05-29 12:30:24 +010087Each platform must ensure that a header file of this name is in the system
88include path with the following constants defined. This may require updating the
89list of `PLAT_INCLUDES` in the `platform.mk` file. In the ARM FVP port, this
90file is found in [plat/fvp/include/platform_def.h].
Achin Gupta4f6ad662013-10-25 09:08:21 +010091
James Morrisseyba3155b2013-10-29 10:56:46 +000092* **#define : PLATFORM_LINKER_FORMAT**
Achin Gupta4f6ad662013-10-25 09:08:21 +010093
94 Defines the linker format used by the platform, for example
95 `elf64-littleaarch64` used by the FVP.
96
James Morrisseyba3155b2013-10-29 10:56:46 +000097* **#define : PLATFORM_LINKER_ARCH**
Achin Gupta4f6ad662013-10-25 09:08:21 +010098
99 Defines the processor architecture for the linker by the platform, for
100 example `aarch64` used by the FVP.
101
James Morrisseyba3155b2013-10-29 10:56:46 +0000102* **#define : PLATFORM_STACK_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103
104 Defines the normal stack memory available to each CPU. This constant is used
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000105 by [plat/common/aarch64/platform_mp_stack.S] and
106 [plat/common/aarch64/platform_up_stack.S].
107
James Morrisseyba3155b2013-10-29 10:56:46 +0000108* **#define : FIRMWARE_WELCOME_STR**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100109
110 Defines the character string printed by BL1 upon entry into the `bl1_main()`
111 function.
112
James Morrisseyba3155b2013-10-29 10:56:46 +0000113* **#define : BL2_IMAGE_NAME**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100114
115 Name of the BL2 binary image on the host file-system. This name is used by
Harry Liebeld265bd72014-01-31 19:04:10 +0000116 BL1 to load BL2 into secure memory from non-volatile storage.
117
118* **#define : BL31_IMAGE_NAME**
119
120 Name of the BL3-1 binary image on the host file-system. This name is used by
121 BL2 to load BL3-1 into secure memory from platform storage.
122
123* **#define : BL33_IMAGE_NAME**
124
125 Name of the BL3-3 binary image on the host file-system. This name is used by
126 BL2 to load BL3-3 into non-secure memory from platform storage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100127
James Morrisseyba3155b2013-10-29 10:56:46 +0000128* **#define : PLATFORM_CACHE_LINE_SIZE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129
130 Defines the size (in bytes) of the largest cache line across all the cache
131 levels in the platform.
132
James Morrisseyba3155b2013-10-29 10:56:46 +0000133* **#define : PLATFORM_CLUSTER_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100134
135 Defines the total number of clusters implemented by the platform in the
136 system.
137
James Morrisseyba3155b2013-10-29 10:56:46 +0000138* **#define : PLATFORM_CORE_COUNT**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100139
140 Defines the total number of CPUs implemented by the platform across all
141 clusters in the system.
142
James Morrisseyba3155b2013-10-29 10:56:46 +0000143* **#define : PLATFORM_MAX_CPUS_PER_CLUSTER**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100144
145 Defines the maximum number of CPUs that can be implemented within a cluster
146 on the platform.
147
Andrew Thoelke6c0b45d2014-06-20 00:36:14 +0100148* **#define : PLATFORM_NUM_AFFS**
149
150 Defines the total number of nodes in the affinity heirarchy at all affinity
151 levels used by the platform.
152
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100153* **#define : BL1_RO_BASE**
154
155 Defines the base address in secure ROM where BL1 originally lives. Must be
156 aligned on a page-size boundary.
157
158* **#define : BL1_RO_LIMIT**
159
160 Defines the maximum address in secure ROM that BL1's actual content (i.e.
161 excluding any data section allocated at runtime) can occupy.
162
163* **#define : BL1_RW_BASE**
164
165 Defines the base address in secure RAM where BL1's read-write data will live
166 at runtime. Must be aligned on a page-size boundary.
167
168* **#define : BL1_RW_LIMIT**
169
170 Defines the maximum address in secure RAM that BL1's read-write data can
171 occupy at runtime.
172
James Morrisseyba3155b2013-10-29 10:56:46 +0000173* **#define : BL2_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100174
175 Defines the base address in secure RAM where BL1 loads the BL2 binary image.
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000176 Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100177
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100178* **#define : BL2_LIMIT**
179
180 Defines the maximum address in secure RAM that the BL2 image can occupy.
181
James Morrisseyba3155b2013-10-29 10:56:46 +0000182* **#define : BL31_BASE**
Achin Gupta4f6ad662013-10-25 09:08:21 +0100183
184 Defines the base address in secure RAM where BL2 loads the BL3-1 binary
Sandrine Bailleuxcd29b0a2013-11-27 10:32:17 +0000185 image. Must be aligned on a page-size boundary.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100186
Sandrine Bailleux638363e2014-05-21 17:08:26 +0100187* **#define : BL31_LIMIT**
188
189 Defines the maximum address in secure RAM that the BL3-1 image can occupy.
190
Harry Liebeld265bd72014-01-31 19:04:10 +0000191* **#define : NS_IMAGE_OFFSET**
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100192
Harry Liebeld265bd72014-01-31 19:04:10 +0000193 Defines the base address in non-secure DRAM where BL2 loads the BL3-3 binary
194 image. Must be aligned on a page-size boundary.
195
Dan Handley5a06bb72014-08-04 11:41:20 +0100196If a BL3-2 image is supported by the platform, the following constants must
197also be defined:
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100198
Dan Handley5a06bb72014-08-04 11:41:20 +0100199* **#define : BL32_IMAGE_NAME**
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100200
Dan Handley5a06bb72014-08-04 11:41:20 +0100201 Name of the BL3-2 binary image on the host file-system. This name is used by
202 BL2 to load BL3-2 into secure memory from platform storage.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100203
204* **#define : BL32_BASE**
205
206 Defines the base address in secure memory where BL2 loads the BL3-2 binary
Dan Handley5a06bb72014-08-04 11:41:20 +0100207 image. Must be aligned on a page-size boundary.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100208
209* **#define : BL32_LIMIT**
210
Dan Handley5a06bb72014-08-04 11:41:20 +0100211 Defines the maximum address that the BL3-2 image can occupy.
212
213If the Test Secure-EL1 Payload (TSP) instantiation of BL3-2 is supported by the
214platform, the following constants must also be defined:
215
216* **#define : TSP_SEC_MEM_BASE**
217
218 Defines the base address of the secure memory used by the TSP image on the
219 platform. This must be at the same address or below `BL32_BASE`.
220
221* **#define : TSP_SEC_MEM_SIZE**
222
223 Defines the size of the secure memory used by the BL3-2 image on the
224 platform. `TSP_SEC_MEM_BASE` and `TSP_SEC_MEM_SIZE` must fully accomodate
225 the memory required by the BL3-2 image, defined by `BL32_BASE` and
226 `BL32_LIMIT`.
227
228* **#define : TSP_IRQ_SEC_PHY_TIMER**
229
230 Defines the ID of the secure physical generic timer interrupt used by the
231 TSP's interrupt handling code.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100232
Dan Handley6d16ce02014-08-04 18:31:43 +0100233If the platform port uses the IO storage framework, the following constants
234must also be defined:
235
236* **#define : MAX_IO_DEVICES**
237
238 Defines the maximum number of registered IO devices. Attempting to register
239 more devices than this value using `io_register_device()` will fail with
240 IO_RESOURCES_EXHAUSTED.
241
242* **#define : MAX_IO_HANDLES**
243
244 Defines the maximum number of open IO handles. Attempting to open more IO
245 entities than this value using `io_open()` will fail with
246 IO_RESOURCES_EXHAUSTED.
247
Sandrine Bailleux46d49f632014-06-23 17:00:23 +0100248The following constants are optional. They should be defined when the platform
249memory layout implies some image overlaying like on FVP.
250
251* **#define : BL31_PROGBITS_LIMIT**
252
253 Defines the maximum address in secure RAM that the BL3-1's progbits sections
254 can occupy.
255
Dan Handley5a06bb72014-08-04 11:41:20 +0100256* **#define : TSP_PROGBITS_LIMIT**
Sandrine Bailleux46d49f632014-06-23 17:00:23 +0100257
258 Defines the maximum address that the TSP's progbits sections can occupy.
Sandrine Bailleux2467f702014-05-20 17:22:24 +0100259
Dan Handleyb68954c2014-05-29 12:30:24 +0100260### File : plat_macros.S [mandatory]
Soby Mathewa43d4312014-04-07 15:28:55 +0100261
Dan Handleyb68954c2014-05-29 12:30:24 +0100262Each platform must ensure a file of this name is in the system include path with
263the following macro defined. In the ARM FVP port, this file is found in
264[plat/fvp/include/plat_macros.S].
Soby Mathewa43d4312014-04-07 15:28:55 +0100265
266* **Macro : plat_print_gic_regs**
267
268 This macro allows the crash reporting routine to print GIC registers
Soby Mathew8c106902014-07-16 09:23:52 +0100269 in case of an unhandled exception in BL3-1. This aids in debugging and
Soby Mathewa43d4312014-04-07 15:28:55 +0100270 this macro can be defined to be empty in case GIC register reporting is
271 not desired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100272
Soby Mathew8c106902014-07-16 09:23:52 +0100273* **Macro : plat_print_interconnect_regs**
274
275 This macro allows the crash reporting routine to print interconnect registers
276 in case of an unhandled exception in BL3-1. This aids in debugging and
277 this macro can be defined to be empty in case interconnect register reporting
278 is not desired. In the ARM FVP port, the CCI snoop control registers are
279 reported.
280
Achin Gupta4f6ad662013-10-25 09:08:21 +0100281### 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
Juan Castillo53fdceb2014-07-16 15:53:43 +0100360### Function : platform_is_primary_cpu() [mandatory]
361
362 Argument : unsigned long
363 Return : unsigned int
364
365This function identifies a CPU by its `MPIDR`, which is passed as the argument,
366to determine whether this CPU is the primary CPU or a secondary CPU. A return
367value of zero indicates that the CPU is not the primary CPU, while a non-zero
368return value indicates that the CPU is the primary CPU.
369
370
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100371### Function : platform_mem_init() [mandatory]
372
373 Argument : void
374 Return : void
375
376This function is called before any access to data is made by the firmware, in
377order to carry out any essential memory initialization.
378
379The ARM FVP port uses this function to initialize the mailbox memory used for
380providing the warm-boot entry-point addresses.
381
382
383
3842.3 Common optional modifications
Achin Gupta4f6ad662013-10-25 09:08:21 +0100385---------------------------------
386
387The following are helper functions implemented by the firmware that perform
388common platform-specific tasks. A platform may choose to override these
389definitions.
390
391
392### Function : platform_get_core_pos()
393
394 Argument : unsigned long
395 Return : int
396
397A platform may need to convert the `MPIDR` of a CPU to an absolute number, which
398can be used as a CPU-specific linear index into blocks of memory (for example
399while allocating per-CPU stacks). This routine contains a simple mechanism
400to perform this conversion, using the assumption that each cluster contains a
401maximum of 4 CPUs:
402
403 linear index = cpu_id + (cluster_id * 4)
404
405 cpu_id = 8-bit value in MPIDR at affinity level 0
406 cluster_id = 8-bit value in MPIDR at affinity level 1
407
408
Achin Gupta4f6ad662013-10-25 09:08:21 +0100409### Function : platform_set_stack()
410
411 Argument : unsigned long
412 Return : void
413
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000414This function sets the current stack pointer to the normal memory stack that
415has been allocated for the CPU specificed by MPIDR. For BL images that only
416require a stack for the primary CPU the parameter is ignored. The size of
417the stack allocated to each CPU is specified by the platform defined constant
418`PLATFORM_STACK_SIZE`.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100419
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000420Common implementations of this function for the UP and MP BL images are
421provided in [plat/common/aarch64/platform_up_stack.S] and
422[plat/common/aarch64/platform_mp_stack.S]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100423
424
Achin Guptac8afc782013-11-25 18:45:02 +0000425### Function : platform_get_stack()
426
427 Argument : unsigned long
428 Return : unsigned long
429
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000430This function returns the base address of the normal memory stack that
431has been allocated for the CPU specificed by MPIDR. For BL images that only
432require a stack for the primary CPU the parameter is ignored. The size of
433the stack allocated to each CPU is specified by the platform defined constant
434`PLATFORM_STACK_SIZE`.
Achin Guptac8afc782013-11-25 18:45:02 +0000435
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000436Common implementations of this function for the UP and MP BL images are
437provided in [plat/common/aarch64/platform_up_stack.S] and
438[plat/common/aarch64/platform_mp_stack.S]
Achin Guptac8afc782013-11-25 18:45:02 +0000439
440
Achin Gupta4f6ad662013-10-25 09:08:21 +0100441### Function : plat_report_exception()
442
443 Argument : unsigned int
444 Return : void
445
446A platform may need to report various information about its status when an
447exception is taken, for example the current exception level, the CPU security
448state (secure/non-secure), the exception type, and so on. This function is
449called in the following circumstances:
450
451* In BL1, whenever an exception is taken.
452* In BL2, whenever an exception is taken.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100453
454The default implementation doesn't do anything, to avoid making assumptions
455about the way the platform displays its status information.
456
457This function receives the exception type as its argument. Possible values for
Andrew Thoelke2bf28e62014-03-20 10:48:23 +0000458exceptions types are listed in the [include/runtime_svc.h] header file. Note
Achin Gupta4f6ad662013-10-25 09:08:21 +0100459that these constants are not related to any architectural exception code; they
460are just an ARM Trusted Firmware convention.
461
462
Soby Mathew24fb8382014-08-14 12:22:32 +0100463### Function : plat_reset_handler()
464
465 Argument : void
466 Return : void
467
468A platform may need to do additional initialization after reset. This function
469allows the platform to do the platform specific intializations. Platform
470specific errata workarounds could also be implemented here. The api should
471preserve the value in x10 register as it is used by the caller to store the
472return address.
473
474The default implementation doesn't do anything.
475
Soby Mathewadd40352014-08-14 12:49:05 +0100476### Function : plat_disable_acp()
477
478 Argument : void
479 Return : void
480
481This api allows a platform to disable the Accelerator Coherency Port (if
482present) during a cluster power down sequence. The default weak implementation
483doesn't do anything. Since this api is called during the power down sequence,
484it has restrictions for stack usage and it can use the registers x0 - x17 as
485scratch registers. It should preserve the value in x18 register as it is used
486by the caller to store the return address.
487
Soby Mathew24fb8382014-08-14 12:22:32 +0100488
Achin Gupta4f6ad662013-10-25 09:08:21 +01004893. Modifications specific to a Boot Loader stage
490-------------------------------------------------
491
4923.1 Boot Loader Stage 1 (BL1)
493-----------------------------
494
495BL1 implements the reset vector where execution starts from after a cold or
496warm boot. For each CPU, BL1 is responsible for the following tasks:
497
Vikram Kanigirie452cd82014-05-23 15:56:12 +01004981. Handling the reset as described in section 2.2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100499
5002. In the case of a cold boot and the CPU being the primary CPU, ensuring that
501 only this CPU executes the remaining BL1 code, including loading and passing
502 control to the BL2 stage.
503
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005043. Loading the BL2 image from non-volatile storage into secure memory at the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100505 address specified by the platform defined constant `BL2_BASE`.
506
Vikram Kanigirie452cd82014-05-23 15:56:12 +01005074. Populating a `meminfo` structure with the following information in memory,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100508 accessible by BL2 immediately upon entry.
509
510 meminfo.total_base = Base address of secure RAM visible to BL2
511 meminfo.total_size = Size of secure RAM visible to BL2
512 meminfo.free_base = Base address of secure RAM available for
513 allocation to BL2
514 meminfo.free_size = Size of secure RAM available for allocation to BL2
515
516 BL1 places this `meminfo` structure at the beginning of the free memory
517 available for its use. Since BL1 cannot allocate memory dynamically at the
518 moment, its free memory will be available for BL2's use as-is. However, this
519 means that BL2 must read the `meminfo` structure before it starts using its
520 free memory (this is discussed in Section 3.2).
521
522 In future releases of the ARM Trusted Firmware it will be possible for
523 the platform to decide where it wants to place the `meminfo` structure for
524 BL2.
525
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100526 BL1 implements the `bl1_init_bl2_mem_layout()` function to populate the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100527 BL2 `meminfo` structure. The platform may override this implementation, for
528 example if the platform wants to restrict the amount of memory visible to
529 BL2. Details of how to do this are given below.
530
531The following functions need to be implemented by the platform port to enable
532BL1 to perform the above tasks.
533
534
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100535### Function : bl1_plat_arch_setup() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100536
537 Argument : void
538 Return : void
539
Achin Gupta4f6ad662013-10-25 09:08:21 +0100540This function performs any platform-specific and architectural setup that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100541platform requires. Platform-specific setup might include configuration of
542memory controllers, configuration of the interconnect to allow the cluster
543to service cache snoop requests from another cluster, and so on.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100544
545In the ARM FVP port, this function enables CCI snoops into the cluster that the
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100546primary CPU is part of. It also enables the MMU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100547
548This function helps fulfill requirement 2 above.
549
550
551### Function : bl1_platform_setup() [mandatory]
552
553 Argument : void
554 Return : void
555
556This function executes with the MMU and data caches enabled. It is responsible
557for performing any remaining platform-specific setup that can occur after the
558MMU and data cache have been enabled.
559
Harry Liebeld265bd72014-01-31 19:04:10 +0000560This function is also responsible for initializing the storage abstraction layer
561which is used to load further bootloader images.
562
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100563This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100564
565
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000566### Function : bl1_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100567
568 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000569 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100570
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000571This function should only be called on the cold boot path. It executes with the
572MMU and data caches enabled. The pointer returned by this function must point to
573a `meminfo` structure containing the extents and availability of secure RAM for
574the BL1 stage.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100575
576 meminfo.total_base = Base address of secure RAM visible to BL1
577 meminfo.total_size = Size of secure RAM visible to BL1
578 meminfo.free_base = Base address of secure RAM available for allocation
579 to BL1
580 meminfo.free_size = Size of secure RAM available for allocation to BL1
581
582This information is used by BL1 to load the BL2 image in secure RAM. BL1 also
583populates a similar structure to tell BL2 the extents of memory available for
584its own use.
585
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100586This function helps fulfill requirement 3 above.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100587
588
Sandrine Bailleux8f55dfb2014-06-24 14:02:34 +0100589### Function : bl1_init_bl2_mem_layout() [optional]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100590
591 Argument : meminfo *, meminfo *, unsigned int, unsigned long
592 Return : void
593
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100594BL1 needs to tell the next stage the amount of secure RAM available
595for it to use. This information is populated in a `meminfo`
Achin Gupta4f6ad662013-10-25 09:08:21 +0100596structure.
597
598Depending upon where BL2 has been loaded in secure RAM (determined by
599`BL2_BASE`), BL1 calculates the amount of free memory available for BL2 to use.
600BL1 also ensures that its data sections resident in secure RAM are not visible
601to BL2. An illustration of how this is done in the ARM FVP port is given in the
602[User Guide], in the Section "Memory layout on Base FVP".
603
604
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100605### Function : bl1_plat_set_bl2_ep_info() [mandatory]
606
607 Argument : image_info *, entry_point_info *
608 Return : void
609
610This function is called after loading BL2 image and it can be used to overwrite
611the entry point set by loader and also set the security state and SPSR which
612represents the entry point system state for BL2.
613
614On FVP, we are setting the security state and the SPSR for the BL2 entrypoint
615
616
Achin Gupta4f6ad662013-10-25 09:08:21 +01006173.2 Boot Loader Stage 2 (BL2)
618-----------------------------
619
620The BL2 stage is executed only by the primary CPU, which is determined in BL1
621using the `platform_is_primary_cpu()` function. BL1 passed control to BL2 at
622`BL2_BASE`. BL2 executes in Secure EL1 and is responsible for:
623
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006241. (Optional) Loading the BL3-0 binary image (if present) from platform
625 provided non-volatile storage. To load the BL3-0 image, BL2 makes use of
626 the `meminfo` returned by the `bl2_plat_get_bl30_meminfo()` function.
627 The platform also defines the address in memory where BL3-0 is loaded
628 through the optional constant `BL30_BASE`. BL2 uses this information
629 to determine if there is enough memory to load the BL3-0 image.
630 Subsequent handling of the BL3-0 image is platform-specific and is
631 implemented in the `bl2_plat_handle_bl30()` function.
632 If `BL30_BASE` is not defined then this step is not performed.
633
6342. Loading the BL3-1 binary image into secure RAM from non-volatile storage. To
Harry Liebeld265bd72014-01-31 19:04:10 +0000635 load the BL3-1 image, BL2 makes use of the `meminfo` structure passed to it
636 by BL1. This structure allows BL2 to calculate how much secure RAM is
637 available for its use. The platform also defines the address in secure RAM
638 where BL3-1 is loaded through the constant `BL31_BASE`. BL2 uses this
639 information to determine if there is enough memory to load the BL3-1 image.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100640
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006413. (Optional) Loading the BL3-2 binary image (if present) from platform
Dan Handley1151c822014-04-15 11:38:38 +0100642 provided non-volatile storage. To load the BL3-2 image, BL2 makes use of
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100643 the `meminfo` returned by the `bl2_plat_get_bl32_meminfo()` function.
644 The platform also defines the address in memory where BL3-2 is loaded
645 through the optional constant `BL32_BASE`. BL2 uses this information
646 to determine if there is enough memory to load the BL3-2 image.
647 If `BL32_BASE` is not defined then this and the next step is not performed.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000648
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006494. (Optional) Arranging to pass control to the BL3-2 image (if present) that
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100650 has been pre-loaded at `BL32_BASE`. BL2 populates an `entry_point_info`
Dan Handley1151c822014-04-15 11:38:38 +0100651 structure in memory provided by the platform with information about how
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100652 BL3-1 should pass control to the BL3-2 image.
Achin Guptaa3050ed2014-02-19 17:52:35 +0000653
Sandrine Bailleux93d81d62014-06-24 14:19:36 +01006545. Loading the normal world BL3-3 binary image into non-secure DRAM from
655 platform storage and arranging for BL3-1 to pass control to this image. This
656 address is determined using the `plat_get_ns_image_entrypoint()` function
657 described below.
658
6596. BL2 populates an `entry_point_info` structure in memory provided by the
660 platform with information about how BL3-1 should pass control to the
661 other BL images.
662
Achin Gupta4f6ad662013-10-25 09:08:21 +0100663The following functions must be implemented by the platform port to enable BL2
664to perform the above tasks.
665
666
667### Function : bl2_early_platform_setup() [mandatory]
668
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100669 Argument : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100670 Return : void
671
672This function executes with the MMU and data caches disabled. It is only called
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100673by the primary CPU. The arguments to this function is the address of the
674`meminfo` structure populated by BL1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100675
676The platform must copy the contents of the `meminfo` structure into a private
677variable as the original memory may be subsequently overwritten by BL2. The
678copied structure is made available to all BL2 code through the
Achin Guptae4d084e2014-02-19 17:18:23 +0000679`bl2_plat_sec_mem_layout()` function.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100680
681
682### Function : bl2_plat_arch_setup() [mandatory]
683
684 Argument : void
685 Return : void
686
687This function executes with the MMU and data caches disabled. It is only called
688by the primary CPU.
689
690The purpose of this function is to perform any architectural initialization
691that varies across platforms, for example enabling the MMU (since the memory
692map differs across platforms).
693
694
695### Function : bl2_platform_setup() [mandatory]
696
697 Argument : void
698 Return : void
699
700This function may execute with the MMU and data caches enabled if the platform
701port does the necessary initialization in `bl2_plat_arch_setup()`. It is only
702called by the primary CPU.
703
Achin Guptae4d084e2014-02-19 17:18:23 +0000704The purpose of this function is to perform any platform initialization
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100705specific to BL2. Platform security components are configured if required.
706For the Base FVP the TZC-400 TrustZone controller is configured to only
707grant non-secure access to DRAM. This avoids aliasing between secure and
708non-secure accesses in the TLB and cache - secure execution states can use
709the NS attributes in the MMU translation tables to access the DRAM.
Harry Liebelce19cf12014-04-01 19:28:07 +0100710
Harry Liebeld265bd72014-01-31 19:04:10 +0000711This function is also responsible for initializing the storage abstraction layer
712which is used to load further bootloader images.
713
Achin Gupta4f6ad662013-10-25 09:08:21 +0100714
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000715### Function : bl2_plat_sec_mem_layout() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100716
717 Argument : void
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000718 Return : meminfo *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100719
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000720This function should only be called on the cold boot path. It may execute with
721the MMU and data caches enabled if the platform port does the necessary
722initialization in `bl2_plat_arch_setup()`. It is only called by the primary CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100723
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000724The purpose of this function is to return a pointer to a `meminfo` structure
725populated with the extents of secure RAM available for BL2 to use. See
Achin Gupta4f6ad662013-10-25 09:08:21 +0100726`bl2_early_platform_setup()` above.
727
728
Sandrine Bailleux93d81d62014-06-24 14:19:36 +0100729### Function : bl2_plat_get_bl30_meminfo() [mandatory]
730
731 Argument : meminfo *
732 Return : void
733
734This function is used to get the memory limits where BL2 can load the
735BL3-0 image. The meminfo provided by this is used by load_image() to
736validate whether the BL3-0 image can be loaded within the given
737memory from the given base.
738
739
740### Function : bl2_plat_handle_bl30() [mandatory]
741
742 Argument : image_info *
743 Return : int
744
745This function is called after loading BL3-0 image and it is used to perform any
746platform-specific actions required to handle the SCP firmware. Typically it
747transfers the image into SCP memory using a platform-specific protocol and waits
748until SCP executes it and signals to the Application Processor (AP) for BL2
749execution to continue.
750
751This function returns 0 on success, a negative error code otherwise.
752
753
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100754### Function : bl2_plat_get_bl31_params() [mandatory]
Harry Liebeld265bd72014-01-31 19:04:10 +0000755
756 Argument : void
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100757 Return : bl31_params *
Harry Liebeld265bd72014-01-31 19:04:10 +0000758
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100759BL2 platform code needs to return a pointer to a `bl31_params` structure it
760will use for passing information to BL3-1. The `bl31_params` structure carries
761the following information.
762 - Header describing the version information for interpreting the bl31_param
763 structure
764 - Information about executing the BL3-3 image in the `bl33_ep_info` field
765 - Information about executing the BL3-2 image in the `bl32_ep_info` field
766 - Information about the type and extents of BL3-1 image in the
767 `bl31_image_info` field
768 - Information about the type and extents of BL3-2 image in the
769 `bl32_image_info` field
770 - Information about the type and extents of BL3-3 image in the
771 `bl33_image_info` field
772
773The memory pointed by this structure and its sub-structures should be
774accessible from BL3-1 initialisation code. BL3-1 might choose to copy the
775necessary content, or maintain the structures until BL3-3 is initialised.
Harry Liebeld265bd72014-01-31 19:04:10 +0000776
777
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100778### Funtion : bl2_plat_get_bl31_ep_info() [mandatory]
Achin Gupta4f6ad662013-10-25 09:08:21 +0100779
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100780 Argument : void
781 Return : entry_point_info *
782
783BL2 platform code returns a pointer which is used to populate the entry point
784information for BL3-1 entry point. The location pointed by it should be
785accessible from BL1 while processing the synchronous exception to run to BL3-1.
786
787On FVP this is allocated inside an bl2_to_bl31_params_mem structure which
788is allocated at an address pointed by PARAMS_BASE.
789
790
791### Function : bl2_plat_set_bl31_ep_info() [mandatory]
792
793 Argument : image_info *, entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100794 Return : void
795
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100796This function is called after loading BL3-1 image and it can be used to
797overwrite the entry point set by loader and also set the security state
798and SPSR which represents the entry point system state for BL3-1.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100799
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100800On FVP, we are setting the security state and the SPSR for the BL3-1
801entrypoint.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100802
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100803### Function : bl2_plat_set_bl32_ep_info() [mandatory]
804
805 Argument : image_info *, entry_point_info *
806 Return : void
807
808This function is called after loading BL3-2 image and it can be used to
809overwrite the entry point set by loader and also set the security state
810and SPSR which represents the entry point system state for BL3-2.
811
812On FVP, we are setting the security state and the SPSR for the BL3-2
813entrypoint
814
815### Function : bl2_plat_set_bl33_ep_info() [mandatory]
816
817 Argument : image_info *, entry_point_info *
818 Return : void
819
820This function is called after loading BL3-3 image and it can be used to
821overwrite the entry point set by loader and also set the security state
822and SPSR which represents the entry point system state for BL3-3.
823
824On FVP, we are setting the security state and the SPSR for the BL3-3
825entrypoint
826
827### Function : bl2_plat_get_bl32_meminfo() [mandatory]
828
829 Argument : meminfo *
830 Return : void
831
832This function is used to get the memory limits where BL2 can load the
833BL3-2 image. The meminfo provided by this is used by load_image() to
834validate whether the BL3-2 image can be loaded with in the given
835memory from the given base.
836
837### Function : bl2_plat_get_bl33_meminfo() [mandatory]
838
839 Argument : meminfo *
840 Return : void
841
842This function is used to get the memory limits where BL2 can load the
843BL3-3 image. The meminfo provided by this is used by load_image() to
844validate whether the BL3-3 image can be loaded with in the given
845memory from the given base.
846
847### Function : bl2_plat_flush_bl31_params() [mandatory]
848
849 Argument : void
850 Return : void
851
852Once BL2 has populated all the structures that needs to be read by BL1
853and BL3-1 including the bl31_params structures and its sub-structures,
854the bl31_ep_info structure and any platform specific data. It flushes
855all these data to the main memory so that it is available when we jump to
856later Bootloader stages with MMU off
Achin Gupta4f6ad662013-10-25 09:08:21 +0100857
858### Function : plat_get_ns_image_entrypoint() [mandatory]
859
860 Argument : void
861 Return : unsigned long
862
863As previously described, BL2 is responsible for arranging for control to be
864passed to a normal world BL image through BL3-1. This function returns the
865entrypoint of that image, which BL3-1 uses to jump to it.
866
Harry Liebeld265bd72014-01-31 19:04:10 +0000867BL2 is responsible for loading the normal world BL3-3 image (e.g. UEFI).
Achin Gupta4f6ad662013-10-25 09:08:21 +0100868
869
8703.2 Boot Loader Stage 3-1 (BL3-1)
871---------------------------------
872
873During cold boot, the BL3-1 stage is executed only by the primary CPU. This is
874determined in BL1 using the `platform_is_primary_cpu()` function. BL1 passes
875control to BL3-1 at `BL31_BASE`. During warm boot, BL3-1 is executed by all
876CPUs. BL3-1 executes at EL3 and is responsible for:
877
8781. Re-initializing all architectural and platform state. Although BL1 performs
879 some of this initialization, BL3-1 remains resident in EL3 and must ensure
880 that EL3 architectural and platform state is completely initialized. It
881 should make no assumptions about the system state when it receives control.
882
8832. Passing control to a normal world BL image, pre-loaded at a platform-
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100884 specific address by BL2. BL3-1 uses the `entry_point_info` structure that BL2
Achin Gupta4f6ad662013-10-25 09:08:21 +0100885 populated in memory to do this.
886
8873. Providing runtime firmware services. Currently, BL3-1 only implements a
888 subset of the Power State Coordination Interface (PSCI) API as a runtime
889 service. See Section 3.3 below for details of porting the PSCI
890 implementation.
891
Achin Gupta35ca3512014-02-19 17:58:33 +00008924. Optionally passing control to the BL3-2 image, pre-loaded at a platform-
893 specific address by BL2. BL3-1 exports a set of apis that allow runtime
894 services to specify the security state in which the next image should be
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100895 executed and run the corresponding image. BL3-1 uses the `entry_point_info`
896 structure populated by BL2 to do this.
897
898If BL3-1 is a reset vector, It also needs to handle the reset as specified in
899section 2.2 before the tasks described above.
Achin Gupta35ca3512014-02-19 17:58:33 +0000900
Achin Gupta4f6ad662013-10-25 09:08:21 +0100901The following functions must be implemented by the platform port to enable BL3-1
902to perform the above tasks.
903
904
905### Function : bl31_early_platform_setup() [mandatory]
906
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100907 Argument : bl31_params *, void *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100908 Return : void
909
910This function executes with the MMU and data caches disabled. It is only called
911by the primary CPU. The arguments to this function are:
912
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100913* The address of the `bl31_params` structure populated by BL2.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100914* An opaque pointer that the platform may use as needed.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100915
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100916The platform can copy the contents of the `bl31_params` structure and its
917sub-structures into private variables if the original memory may be
918subsequently overwritten by BL3-1 and similarly the `void *` pointing
919to the platform data also needs to be saved.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100920
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100921On the ARM FVP port, BL2 passes a pointer to a `bl31_params` structure populated
922in the secure DRAM at address `0x6000000` in the bl31_params * argument and it
923does not use opaque pointer mentioned earlier. BL3-1 does not copy this
924information to internal data structures as it guarantees that the secure
925DRAM memory will not be overwritten. It maintains an internal reference to this
926information in the `bl2_to_bl31_params` variable.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100927
928### Function : bl31_plat_arch_setup() [mandatory]
929
930 Argument : void
931 Return : void
932
933This function executes with the MMU and data caches disabled. It is only called
934by the primary CPU.
935
936The purpose of this function is to perform any architectural initialization
937that varies across platforms, for example enabling the MMU (since the memory
938map differs across platforms).
939
940
941### Function : bl31_platform_setup() [mandatory]
942
943 Argument : void
944 Return : void
945
946This function may execute with the MMU and data caches enabled if the platform
947port does the necessary initialization in `bl31_plat_arch_setup()`. It is only
948called by the primary CPU.
949
950The purpose of this function is to complete platform initialization so that both
951BL3-1 runtime services and normal world software can function correctly.
952
953The ARM FVP port does the following:
954* Initializes the generic interrupt controller.
955* Configures the CLCD controller.
Sandrine Bailleux9e864902014-03-31 11:25:18 +0100956* Enables system-level implementation of the generic timer counter.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100957* Grants access to the system counter timer module
958* Initializes the FVP power controller device
959* Detects the system topology.
960
961
962### Function : bl31_get_next_image_info() [mandatory]
963
Achin Gupta35ca3512014-02-19 17:58:33 +0000964 Argument : unsigned int
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100965 Return : entry_point_info *
Achin Gupta4f6ad662013-10-25 09:08:21 +0100966
967This function may execute with the MMU and data caches enabled if the platform
968port does the necessary initializations in `bl31_plat_arch_setup()`.
969
970This function is called by `bl31_main()` to retrieve information provided by
Achin Gupta35ca3512014-02-19 17:58:33 +0000971BL2 for the next image in the security state specified by the argument. BL3-1
972uses this information to pass control to that image in the specified security
Vikram Kanigirie452cd82014-05-23 15:56:12 +0100973state. This function must return a pointer to the `entry_point_info` structure
Achin Gupta35ca3512014-02-19 17:58:33 +0000974(that was copied during `bl31_early_platform_setup()`) if the image exists. It
975should return NULL otherwise.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100976
977
Achin Gupta4f6ad662013-10-25 09:08:21 +01009783.3 Power State Coordination Interface (in BL3-1)
979------------------------------------------------
980
981The ARM Trusted Firmware's implementation of the PSCI API is based around the
982concept of an _affinity instance_. Each _affinity instance_ can be uniquely
983identified in a system by a CPU ID (the processor `MPIDR` is used in the PSCI
984interface) and an _affinity level_. A processing element (for example, a
985CPU) is at level 0. If the CPUs in the system are described in a tree where the
986node above a CPU is a logical grouping of CPUs that share some state, then
987affinity level 1 is that group of CPUs (for example, a cluster), and affinity
988level 2 is a group of clusters (for example, the system). The implementation
989assumes that the affinity level 1 ID can be computed from the affinity level 0
990ID (for example, a unique cluster ID can be computed from the CPU ID). The
991current implementation computes this on the basis of the recommended use of
992`MPIDR` affinity fields in the ARM Architecture Reference Manual.
993
994BL3-1's platform initialization code exports a pointer to the platform-specific
995power management operations required for the PSCI implementation to function
996correctly. This information is populated in the `plat_pm_ops` structure. The
997PSCI implementation calls members of the `plat_pm_ops` structure for performing
998power management operations for each affinity instance. For example, the target
999CPU is specified by its `MPIDR` in a PSCI `CPU_ON` call. The `affinst_on()`
1000handler (if present) is called for each affinity instance as the PSCI
1001implementation powers up each affinity level implemented in the `MPIDR` (for
1002example, CPU, cluster and system).
1003
1004The following functions must be implemented to initialize PSCI functionality in
1005the ARM Trusted Firmware.
1006
1007
1008### Function : plat_get_aff_count() [mandatory]
1009
1010 Argument : unsigned int, unsigned long
1011 Return : unsigned int
1012
1013This function may execute with the MMU and data caches enabled if the platform
1014port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1015called by the primary CPU.
1016
1017This function is called by the PSCI initialization code to detect the system
1018topology. Its purpose is to return the number of affinity instances implemented
1019at a given `affinity level` (specified by the first argument) and a given
1020`MPIDR` (specified by the second argument). For example, on a dual-cluster
1021system where first cluster implements 2 CPUs and the second cluster implements 4
1022CPUs, a call to this function with an `MPIDR` corresponding to the first cluster
1023(`0x0`) and affinity level 0, would return 2. A call to this function with an
1024`MPIDR` corresponding to the second cluster (`0x100`) and affinity level 0,
1025would return 4.
1026
1027
1028### Function : plat_get_aff_state() [mandatory]
1029
1030 Argument : unsigned int, unsigned long
1031 Return : unsigned int
1032
1033This function may execute with the MMU and data caches enabled if the platform
1034port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1035called by the primary CPU.
1036
1037This function is called by the PSCI initialization code. Its purpose is to
1038return the state of an affinity instance. The affinity instance is determined by
1039the affinity ID at a given `affinity level` (specified by the first argument)
1040and an `MPIDR` (specified by the second argument). The state can be one of
1041`PSCI_AFF_PRESENT` or `PSCI_AFF_ABSENT`. The latter state is used to cater for
1042system topologies where certain affinity instances are unimplemented. For
1043example, consider a platform that implements a single cluster with 4 CPUs and
1044another CPU implemented directly on the interconnect with the cluster. The
1045`MPIDR`s of the cluster would range from `0x0-0x3`. The `MPIDR` of the single
1046CPU would be 0x100 to indicate that it does not belong to cluster 0. Cluster 1
1047is missing but needs to be accounted for to reach this single CPU in the
1048topology tree. Hence it is marked as `PSCI_AFF_ABSENT`.
1049
1050
1051### Function : plat_get_max_afflvl() [mandatory]
1052
1053 Argument : void
1054 Return : int
1055
1056This function may execute with the MMU and data caches enabled if the platform
1057port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1058called by the primary CPU.
1059
1060This function is called by the PSCI implementation both during cold and warm
1061boot, to determine the maximum affinity level that the power management
James Morrisseyba3155b2013-10-29 10:56:46 +00001062operations should apply to. ARMv8-A has support for 4 affinity levels. It is
Achin Gupta4f6ad662013-10-25 09:08:21 +01001063likely that hardware will implement fewer affinity levels. This function allows
1064the PSCI implementation to consider only those affinity levels in the system
1065that the platform implements. For example, the Base AEM FVP implements two
1066clusters with a configurable number of CPUs. It reports the maximum affinity
1067level as 1, resulting in PSCI power control up to the cluster level.
1068
1069
1070### Function : platform_setup_pm() [mandatory]
1071
Sandrine Bailleux44804252014-08-06 11:27:23 +01001072 Argument : const plat_pm_ops **
Achin Gupta4f6ad662013-10-25 09:08:21 +01001073 Return : int
1074
1075This function may execute with the MMU and data caches enabled if the platform
1076port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1077called by the primary CPU.
1078
1079This function is called by PSCI initialization code. Its purpose is to export
1080handler routines for platform-specific power management actions by populating
1081the passed pointer with a pointer to BL3-1's private `plat_pm_ops` structure.
1082
1083A description of each member of this structure is given below. Please refer to
Sandrine Bailleux44804252014-08-06 11:27:23 +01001084the ARM FVP specific implementation of these handlers in [plat/fvp/fvp_pm.c]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001085as an example. A platform port may choose not implement some of the power
Sandrine Bailleux44804252014-08-06 11:27:23 +01001086management operations.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001087
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
Achin Gupta4f6ad662013-10-25 09:08:21 +01001119#### plat_pm_ops.affinst_suspend()
1120
1121Perform the platform specific setup to power off an affinity instance in the
1122`MPIDR` of the calling CPU. It is called by the PSCI `CPU_SUSPEND` API
1123implementation.
1124
1125The `MPIDR` (first argument), `affinity level` (third argument) and `state`
1126(fifth argument) have a similar meaning as described in the `affinst_on()`
1127operation. They are used to identify the affinity instance on which the call
1128is made and its current state. This gives the platform port an indication of the
1129state transition it must make to perform the requested action. For example, if
1130the calling CPU is the last powered on CPU in the cluster, after powering down
1131affinity level 0 (CPU), the platform port should power down affinity level 1
1132(the cluster) as well.
1133
1134The difference between turning an affinity instance off versus suspending it
1135is that in the former case, the affinity instance is expected to re-initialize
1136its state when its next powered on (see `affinst_on_finish()`). In the latter
1137case, the affinity instance is expected to save enough state so that it can
1138resume execution by restoring this state when its powered on (see
1139`affinst_suspend_finish()`).
1140
Achin Gupta4f6ad662013-10-25 09:08:21 +01001141#### plat_pm_ops.affinst_on_finish()
1142
1143This function is called by the PSCI implementation after the calling CPU is
1144powered on and released from reset in response to an earlier PSCI `CPU_ON` call.
1145It performs the platform-specific setup required to initialize enough state for
1146this CPU to enter the normal world and also provide secure runtime firmware
1147services.
1148
1149The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1150(third argument) have a similar meaning as described in the previous operations.
1151
Achin Gupta4f6ad662013-10-25 09:08:21 +01001152#### plat_pm_ops.affinst_on_suspend()
1153
1154This function is called by the PSCI implementation after the calling CPU is
1155powered on and released from reset in response to an asynchronous wakeup
1156event, for example a timer interrupt that was programmed by the CPU during the
1157`CPU_SUSPEND` call. It performs the platform-specific setup required to
1158restore the saved state for this CPU to resume execution in the normal world
1159and also provide secure runtime firmware services.
1160
1161The `MPIDR` (first argument), `affinity level` (second argument) and `state`
1162(third argument) have a similar meaning as described in the previous operations.
1163
Achin Gupta4f6ad662013-10-25 09:08:21 +01001164BL3-1 platform initialization code must also detect the system topology and
1165the state of each affinity instance in the topology. This information is
1166critical for the PSCI runtime service to function correctly. More details are
1167provided in the description of the `plat_get_aff_count()` and
1168`plat_get_aff_state()` functions above.
1169
Achin Guptaa4fa3cb2014-06-02 22:27:36 +010011703.4 Interrupt Management framework (in BL3-1)
1171----------------------------------------------
1172BL3-1 implements an Interrupt Management Framework (IMF) to manage interrupts
1173generated in either security state and targeted to EL1 or EL2 in the non-secure
1174state or EL3/S-EL1 in the secure state. The design of this framework is
1175described in the [IMF Design Guide]
1176
1177A platform should export the following APIs to support the IMF. The following
1178text briefly describes each api and its implementation on the FVP port. The API
1179implementation depends upon the type of interrupt controller present in the
1180platform. The FVP implements an ARM Generic Interrupt Controller (ARM GIC) as
1181per the version 2.0 of the [ARM GIC Architecture Specification]
1182
1183### Function : plat_interrupt_type_to_line() [mandatory]
1184
1185 Argument : uint32_t, uint32_t
1186 Return : uint32_t
1187
1188The ARM processor signals an interrupt exception either through the IRQ or FIQ
1189interrupt line. The specific line that is signaled depends on how the interrupt
1190controller (IC) reports different interrupt types from an execution context in
1191either security state. The IMF uses this API to determine which interrupt line
1192the platform IC uses to signal each type of interrupt supported by the framework
1193from a given security state.
1194
1195The first parameter will be one of the `INTR_TYPE_*` values (see [IMF Design
1196Guide]) indicating the target type of the interrupt, the second parameter is the
1197security state of the originating execution context. The return result is the
1198bit position in the `SCR_EL3` register of the respective interrupt trap: IRQ=1,
1199FIQ=2.
1200
1201The FVP port configures the ARM GIC to signal S-EL1 interrupts as FIQs and
1202Non-secure interrupts as IRQs from either security state.
1203
1204
1205### Function : plat_ic_get_pending_interrupt_type() [mandatory]
1206
1207 Argument : void
1208 Return : uint32_t
1209
1210This API returns the type of the highest priority pending interrupt at the
1211platform IC. The IMF uses the interrupt type to retrieve the corresponding
1212handler function. `INTR_TYPE_INVAL` is returned when there is no interrupt
1213pending. The valid interrupt types that can be returned are `INTR_TYPE_EL3`,
1214`INTR_TYPE_S_EL1` and `INTR_TYPE_NS`.
1215
1216The FVP port reads the _Highest Priority Pending Interrupt Register_
1217(`GICC_HPPIR`) to determine the id of the pending interrupt. The type of interrupt
1218depends upon the id value as follows.
1219
12201. id < 1022 is reported as a S-EL1 interrupt
12212. id = 1022 is reported as a Non-secure interrupt.
12223. id = 1023 is reported as an invalid interrupt type.
1223
1224
1225### Function : plat_ic_get_pending_interrupt_id() [mandatory]
1226
1227 Argument : void
1228 Return : uint32_t
1229
1230This API returns the id of the highest priority pending interrupt at the
1231platform IC. The IMF passes the id returned by this API to the registered
1232handler for the pending interrupt if the `IMF_READ_INTERRUPT_ID` build time flag
1233is set. INTR_ID_UNAVAILABLE is returned when there is no interrupt pending.
1234
1235The FVP port reads the _Highest Priority Pending Interrupt Register_
1236(`GICC_HPPIR`) to determine the id of the pending interrupt. The id that is
1237returned by API depends upon the value of the id read from the interrupt
1238controller as follows.
1239
12401. id < 1022. id is returned as is.
12412. id = 1022. The _Aliased Highest Priority Pending Interrupt Register_
1242 (`GICC_AHPPIR`) is read to determine the id of the non-secure interrupt. This
1243 id is returned by the API.
12443. id = 1023. `INTR_ID_UNAVAILABLE` is returned.
1245
1246
1247### Function : plat_ic_acknowledge_interrupt() [mandatory]
1248
1249 Argument : void
1250 Return : uint32_t
1251
1252This API is used by the CPU to indicate to the platform IC that processing of
1253the highest pending interrupt has begun. It should return the id of the
1254interrupt which is being processed.
1255
1256The FVP port reads the _Interrupt Acknowledge Register_ (`GICC_IAR`). This
1257changes the state of the highest priority pending interrupt from pending to
1258active in the interrupt controller. It returns the value read from the
1259`GICC_IAR`. This value is the id of the interrupt whose state has been changed.
1260
1261The TSP uses this API to start processing of the secure physical timer
1262interrupt.
1263
1264
1265### Function : plat_ic_end_of_interrupt() [mandatory]
1266
1267 Argument : uint32_t
1268 Return : void
1269
1270This API is used by the CPU to indicate to the platform IC that processing of
1271the interrupt corresponding to the id (passed as the parameter) has
1272finished. The id should be the same as the id returned by the
1273`plat_ic_acknowledge_interrupt()` API.
1274
1275The FVP port writes the id to the _End of Interrupt Register_
1276(`GICC_EOIR`). This deactivates the corresponding interrupt in the interrupt
1277controller.
1278
1279The TSP uses this API to finish processing of the secure physical timer
1280interrupt.
1281
1282
1283### Function : plat_ic_get_interrupt_type() [mandatory]
1284
1285 Argument : uint32_t
1286 Return : uint32_t
1287
1288This API returns the type of the interrupt id passed as the parameter.
1289`INTR_TYPE_INVAL` is returned if the id is invalid. If the id is valid, a valid
1290interrupt type (one of `INTR_TYPE_EL3`, `INTR_TYPE_S_EL1` and `INTR_TYPE_NS`) is
1291returned depending upon how the interrupt has been configured by the platform
1292IC.
1293
1294The FVP port configures S-EL1 interrupts as Group0 interrupts and Non-secure
1295interrupts as Group1 interrupts. It reads the group value corresponding to the
1296interrupt id from the relevant _Interrupt Group Register_ (`GICD_IGROUPRn`). It
1297uses the group value to determine the type of interrupt.
1298
Soby Mathewc67b09b2014-07-14 16:57:23 +010012993.5 Crash Reporting mechanism (in BL3-1)
1300----------------------------------------------
1301BL3-1 implements a crash reporting mechanism which prints the various registers
Sandrine Bailleux44804252014-08-06 11:27:23 +01001302of the CPU to enable quick crash analysis and debugging. It requires that a
1303console is designated as the crash console by the platform which will be used to
1304print the register dump.
Soby Mathewc67b09b2014-07-14 16:57:23 +01001305
Sandrine Bailleux44804252014-08-06 11:27:23 +01001306The following functions must be implemented by the platform if it wants crash
1307reporting mechanism in BL3-1. The functions are implemented in assembly so that
1308they can be invoked without a C Runtime stack.
Soby Mathewc67b09b2014-07-14 16:57:23 +01001309
1310### Function : plat_crash_console_init
1311
1312 Argument : void
1313 Return : int
1314
Sandrine Bailleux44804252014-08-06 11:27:23 +01001315This API is used by the crash reporting mechanism to initialize the crash
1316console. It should only use the general purpose registers x0 to x2 to do the
1317initialization and returns 1 on success.
Soby Mathewc67b09b2014-07-14 16:57:23 +01001318
Sandrine Bailleux44804252014-08-06 11:27:23 +01001319The FVP port designates the `PL011_UART0` as the crash console and calls the
Soby Mathewc67b09b2014-07-14 16:57:23 +01001320console_core_init() to initialize the console.
1321
1322### Function : plat_crash_console_putc
1323
1324 Argument : int
1325 Return : int
1326
1327This API is used by the crash reporting mechanism to print a character on the
1328designated crash console. It should only use general purpose registers x1 and
1329x2 to do its work. The parameter and the return value are in general purpose
1330register x0.
1331
Sandrine Bailleux44804252014-08-06 11:27:23 +01001332The FVP port designates the `PL011_UART0` as the crash console and calls the
Soby Mathewc67b09b2014-07-14 16:57:23 +01001333console_core_putc() to print the character on the console.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001334
Harry Liebela960f282013-12-12 16:03:44 +000013354. C Library
1336-------------
1337
1338To avoid subtle toolchain behavioral dependencies, the header files provided
1339by the compiler are not used. The software is built with the `-nostdinc` flag
1340to ensure no headers are included from the toolchain inadvertently. Instead the
1341required headers are included in the ARM Trusted Firmware source tree. The
1342library only contains those C library definitions required by the local
1343implementation. If more functionality is required, the needed library functions
1344will need to be added to the local implementation.
1345
1346Versions of [FreeBSD] headers can be found in `include/stdlib`. Some of these
1347headers have been cut down in order to simplify the implementation. In order to
1348minimize changes to the header files, the [FreeBSD] layout has been maintained.
1349The generic C library definitions can be found in `include/stdlib` with more
1350system and machine specific declarations in `include/stdlib/sys` and
1351`include/stdlib/machine`.
1352
1353The local C library implementations can be found in `lib/stdlib`. In order to
1354extend the C library these files may need to be modified. It is recommended to
1355use a release version of [FreeBSD] as a starting point.
1356
1357The C library header files in the [FreeBSD] source tree are located in the
1358`include` and `sys/sys` directories. [FreeBSD] machine specific definitions
1359can be found in the `sys/<machine-type>` directories. These files define things
1360like 'the size of a pointer' and 'the range of an integer'. Since an AArch64
1361port for [FreeBSD] does not yet exist, the machine specific definitions are
1362based on existing machine types with similar properties (for example SPARC64).
1363
1364Where possible, C library function implementations were taken from [FreeBSD]
1365as found in the `lib/libc` directory.
1366
1367A copy of the [FreeBSD] sources can be downloaded with `git`.
1368
1369 git clone git://github.com/freebsd/freebsd.git -b origin/release/9.2.0
1370
1371
Harry Liebeld265bd72014-01-31 19:04:10 +000013725. Storage abstraction layer
1373-----------------------------
1374
1375In order to improve platform independence and portability an storage abstraction
1376layer is used to load data from non-volatile platform storage.
1377
1378Each platform should register devices and their drivers via the Storage layer.
1379These drivers then need to be initialized by bootloader phases as
1380required in their respective `blx_platform_setup()` functions. Currently
1381storage access is only required by BL1 and BL2 phases. The `load_image()`
1382function uses the storage layer to access non-volatile platform storage.
1383
1384It is mandatory to implement at least one storage driver. For the FVP the
1385Firmware Image Package(FIP) driver is provided as the default means to load data
1386from storage (see the "Firmware Image Package" section in the [User Guide]).
1387The storage layer is described in the header file `include/io_storage.h`. The
1388implementation of the common library is in `lib/io_storage.c` and the driver
1389files are located in `drivers/io/`.
1390
1391Each IO driver must provide `io_dev_*` structures, as described in
1392`drivers/io/io_driver.h`. These are returned via a mandatory registration
1393function that is called on platform initialization. The semi-hosting driver
1394implementation in `io_semihosting.c` can be used as an example.
1395
1396The Storage layer provides mechanisms to initialize storage devices before
1397IO operations are called. The basic operations supported by the layer
1398include `open()`, `close()`, `read()`, `write()`, `size()` and `seek()`.
1399Drivers do not have to implement all operations, but each platform must
1400provide at least one driver for a device capable of supporting generic
1401operations such as loading a bootloader image.
1402
1403The current implementation only allows for known images to be loaded by the
Dan Handleyb68954c2014-05-29 12:30:24 +01001404firmware. These images are specified by using their names, as defined in
1405[include/plat/common/platform.h]. The platform layer (`plat_get_image_source()`)
1406then returns a reference to a device and a driver-specific `spec` which will be
1407understood by the driver to allow access to the image data.
Harry Liebeld265bd72014-01-31 19:04:10 +00001408
1409The layer is designed in such a way that is it possible to chain drivers with
1410other drivers. For example, file-system drivers may be implemented on top of
1411physical block devices, both represented by IO devices with corresponding
1412drivers. In such a case, the file-system "binding" with the block device may
1413be deferred until the file-system device is initialised.
1414
1415The abstraction currently depends on structures being statically allocated
1416by the drivers and callers, as the system does not yet provide a means of
1417dynamically allocating memory. This may also have the affect of limiting the
1418amount of open resources per driver.
1419
1420
Achin Gupta4f6ad662013-10-25 09:08:21 +01001421- - - - - - - - - - - - - - - - - - - - - - - - - -
1422
Dan Handleye83b0ca2014-01-14 18:17:09 +00001423_Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved._
Achin Gupta4f6ad662013-10-25 09:08:21 +01001424
1425
Achin Guptaa4fa3cb2014-06-02 22:27:36 +01001426[ARM GIC Architecture Specification]: http://arminfo.emea.arm.com/help/topic/com.arm.doc.ihi0048b/IHI0048B_gic_architecture_specification.pdf
1427[IMF Design Guide]: interrupt-framework-design.md
1428[User Guide]: user-guide.md
1429[FreeBSD]: http://www.freebsd.org
Achin Gupta4f6ad662013-10-25 09:08:21 +01001430
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001431[plat/common/aarch64/platform_mp_stack.S]: ../plat/common/aarch64/platform_mp_stack.S
1432[plat/common/aarch64/platform_up_stack.S]: ../plat/common/aarch64/platform_up_stack.S
Dan Handleyb68954c2014-05-29 12:30:24 +01001433[plat/fvp/include/platform_def.h]: ../plat/fvp/include/platform_def.h
1434[plat/fvp/include/plat_macros.S]: ../plat/fvp/include/plat_macros.S
Andrew Thoelke2bf28e62014-03-20 10:48:23 +00001435[plat/fvp/aarch64/plat_common.c]: ../plat/fvp/aarch64/plat_common.c
1436[plat/fvp/plat_pm.c]: ../plat/fvp/plat_pm.c
1437[include/runtime_svc.h]: ../include/runtime_svc.h
Dan Handleyb68954c2014-05-29 12:30:24 +01001438[include/plat/common/platform.h]: ../include/plat/common/platform.h