Merge pull request #677 from hzhuang1/gpt
[project/bcm63xx/atf.git] / docs / porting-guide.md
1 ARM Trusted Firmware Porting Guide
2 ==================================
3
4 Contents
5 --------
6
7 1. [Introduction](#1--introduction)
8 2. [Common Modifications](#2--common-modifications)
9 * [Common mandatory modifications](#21-common-mandatory-modifications)
10 * [Handling reset](#22-handling-reset)
11 * [Common mandatory function modifications](#23-common-mandatory-function-modifications)
12 * [Common optional modifications](#24-common-optional-modifications)
13 3. [Boot Loader stage specific modifications](#3--modifications-specific-to-a-boot-loader-stage)
14 * [Boot Loader stage 1 (BL1)](#31-boot-loader-stage-1-bl1)
15 * [Boot Loader stage 2 (BL2)](#32-boot-loader-stage-2-bl2)
16 * [FWU Boot Loader stage 2 (BL2U)](#33-fwu-boot-loader-stage-2-bl2u)
17 * [Boot Loader stage 3-1 (BL31)](#34-boot-loader-stage-3-1-bl31)
18 * [PSCI implementation (in BL31)](#35-power-state-coordination-interface-in-bl31)
19 * [Interrupt Management framework (in BL31)](#36--interrupt-management-framework-in-bl31)
20 * [Crash Reporting mechanism (in BL31)](#37--crash-reporting-mechanism-in-bl31)
21 4. [Build flags](#4--build-flags)
22 5. [C Library](#5--c-library)
23 6. [Storage abstraction layer](#6--storage-abstraction-layer)
24
25 - - - - - - - - - - - - - - - - - -
26
27 1. Introduction
28 ----------------
29
30 Please note that this document has been updated for the new platform API
31 as required by the PSCI v1.0 implementation. Please refer to the
32 [Migration Guide] for the previous platform API.
33
34 Porting the ARM Trusted Firmware to a new platform involves making some
35 mandatory and optional modifications for both the cold and warm boot paths.
36 Modifications consist of:
37
38 * Implementing a platform-specific function or variable,
39 * Setting up the execution context in a certain way, or
40 * Defining certain constants (for example #defines).
41
42 The platform-specific functions and variables are declared in
43 [include/plat/common/platform.h]. The firmware provides a default implementation
44 of variables and functions to fulfill the optional requirements. These
45 implementations are all weakly defined; they are provided to ease the porting
46 effort. Each platform port can override them with its own implementation if the
47 default implementation is inadequate.
48
49 Platform ports that want to be aligned with standard ARM platforms (for example
50 FVP and Juno) may also use [include/plat/arm/common/plat_arm.h] and the
51 corresponding source files in `plat/arm/common/`. These provide standard
52 implementations for some of the required platform porting functions. However,
53 using these functions requires the platform port to implement additional
54 ARM standard platform porting functions. These additional functions are not
55 documented here.
56
57 Some modifications are common to all Boot Loader (BL) stages. Section 2
58 discusses these in detail. The subsequent sections discuss the remaining
59 modifications for each BL stage in detail.
60
61 This document should be read in conjunction with the ARM Trusted Firmware
62 [User Guide].
63
64
65 2. Common modifications
66 ------------------------
67
68 This section covers the modifications that should be made by the platform for
69 each BL stage to correctly port the firmware stack. They are categorized as
70 either mandatory or optional.
71
72
73 2.1 Common mandatory modifications
74 ----------------------------------
75
76 A platform port must enable the Memory Management Unit (MMU) as well as the
77 instruction and data caches for each BL stage. Setting up the translation
78 tables is the responsibility of the platform port because memory maps differ
79 across platforms. A memory translation library (see `lib/xlat_tables/`) is
80 provided to help in this setup. Note that although this library supports
81 non-identity mappings, this is intended only for re-mapping peripheral physical
82 addresses and allows platforms with high I/O addresses to reduce their virtual
83 address space. All other addresses corresponding to code and data must currently
84 use an identity mapping.
85
86 In ARM standard platforms, each BL stage configures the MMU in the
87 platform-specific architecture setup function, `blX_plat_arch_setup()`, and uses
88 an identity mapping for all addresses.
89
90 If the build option `USE_COHERENT_MEM` is enabled, each platform can allocate a
91 block of identity mapped secure memory with Device-nGnRE attributes aligned to
92 page boundary (4K) for each BL stage. All sections which allocate coherent
93 memory are grouped under `coherent_ram`. For ex: Bakery locks are placed in a
94 section identified by name `bakery_lock` inside `coherent_ram` so that its
95 possible for the firmware to place variables in it using the following C code
96 directive:
97
98 __section("bakery_lock")
99
100 Or alternatively the following assembler code directive:
101
102 .section bakery_lock
103
104 The `coherent_ram` section is a sum of all sections like `bakery_lock` which are
105 used to allocate any data structures that are accessed both when a CPU is
106 executing with its MMU and caches enabled, and when it's running with its MMU
107 and caches disabled. Examples are given below.
108
109 The following variables, functions and constants must be defined by the platform
110 for the firmware to work correctly.
111
112
113 ### File : platform_def.h [mandatory]
114
115 Each platform must ensure that a header file of this name is in the system
116 include path with the following constants defined. This may require updating the
117 list of `PLAT_INCLUDES` in the `platform.mk` file. In the ARM development
118 platforms, this file is found in `plat/arm/board/<plat_name>/include/`.
119
120 Platform ports may optionally use the file [include/plat/common/common_def.h],
121 which provides typical values for some of the constants below. These values are
122 likely to be suitable for all platform ports.
123
124 Platform ports that want to be aligned with standard ARM platforms (for example
125 FVP and Juno) may also use [include/plat/arm/common/arm_def.h], which provides
126 standard values for some of the constants below. However, this requires the
127 platform port to define additional platform porting constants in
128 `platform_def.h`. These additional constants are not documented here.
129
130 * **#define : PLATFORM_LINKER_FORMAT**
131
132 Defines the linker format used by the platform, for example
133 `elf64-littleaarch64`.
134
135 * **#define : PLATFORM_LINKER_ARCH**
136
137 Defines the processor architecture for the linker by the platform, for
138 example `aarch64`.
139
140 * **#define : PLATFORM_STACK_SIZE**
141
142 Defines the normal stack memory available to each CPU. This constant is used
143 by [plat/common/aarch64/platform_mp_stack.S] and
144 [plat/common/aarch64/platform_up_stack.S].
145
146 * **define : CACHE_WRITEBACK_GRANULE**
147
148 Defines the size in bits of the largest cache line across all the cache
149 levels in the platform.
150
151 * **#define : FIRMWARE_WELCOME_STR**
152
153 Defines the character string printed by BL1 upon entry into the `bl1_main()`
154 function.
155
156 * **#define : PLATFORM_CORE_COUNT**
157
158 Defines the total number of CPUs implemented by the platform across all
159 clusters in the system.
160
161 * **#define : PLAT_NUM_PWR_DOMAINS**
162
163 Defines the total number of nodes in the power domain topology
164 tree at all the power domain levels used by the platform.
165 This macro is used by the PSCI implementation to allocate
166 data structures to represent power domain topology.
167
168 * **#define : PLAT_MAX_PWR_LVL**
169
170 Defines the maximum power domain level that the power management operations
171 should apply to. More often, but not always, the power domain level
172 corresponds to affinity level. This macro allows the PSCI implementation
173 to know the highest power domain level that it should consider for power
174 management operations in the system that the platform implements. For
175 example, the Base AEM FVP implements two clusters with a configurable
176 number of CPUs and it reports the maximum power domain level as 1.
177
178 * **#define : PLAT_MAX_OFF_STATE**
179
180 Defines the local power state corresponding to the deepest power down
181 possible at every power domain level in the platform. The local power
182 states for each level may be sparsely allocated between 0 and this value
183 with 0 being reserved for the RUN state. The PSCI implementation uses this
184 value to initialize the local power states of the power domain nodes and
185 to specify the requested power state for a PSCI_CPU_OFF call.
186
187 * **#define : PLAT_MAX_RET_STATE**
188
189 Defines the local power state corresponding to the deepest retention state
190 possible at every power domain level in the platform. This macro should be
191 a value less than PLAT_MAX_OFF_STATE and greater than 0. It is used by the
192 PSCI implementation to distinguish between retention and power down local
193 power states within PSCI_CPU_SUSPEND call.
194
195 * **#define : PLAT_MAX_PWR_LVL_STATES**
196
197 Defines the maximum number of local power states per power domain level
198 that the platform supports. The default value of this macro is 2 since
199 most platforms just support a maximum of two local power states at each
200 power domain level (power-down and retention). If the platform needs to
201 account for more local power states, then it must redefine this macro.
202
203 Currently, this macro is used by the Generic PSCI implementation to size
204 the array used for PSCI_STAT_COUNT/RESIDENCY accounting.
205
206 * **#define : BL1_RO_BASE**
207
208 Defines the base address in secure ROM where BL1 originally lives. Must be
209 aligned on a page-size boundary.
210
211 * **#define : BL1_RO_LIMIT**
212
213 Defines the maximum address in secure ROM that BL1's actual content (i.e.
214 excluding any data section allocated at runtime) can occupy.
215
216 * **#define : BL1_RW_BASE**
217
218 Defines the base address in secure RAM where BL1's read-write data will live
219 at runtime. Must be aligned on a page-size boundary.
220
221 * **#define : BL1_RW_LIMIT**
222
223 Defines the maximum address in secure RAM that BL1's read-write data can
224 occupy at runtime.
225
226 * **#define : BL2_BASE**
227
228 Defines the base address in secure RAM where BL1 loads the BL2 binary image.
229 Must be aligned on a page-size boundary.
230
231 * **#define : BL2_LIMIT**
232
233 Defines the maximum address in secure RAM that the BL2 image can occupy.
234
235 * **#define : BL31_BASE**
236
237 Defines the base address in secure RAM where BL2 loads the BL31 binary
238 image. Must be aligned on a page-size boundary.
239
240 * **#define : BL31_LIMIT**
241
242 Defines the maximum address in secure RAM that the BL31 image can occupy.
243
244 For every image, the platform must define individual identifiers that will be
245 used by BL1 or BL2 to load the corresponding image into memory from non-volatile
246 storage. For the sake of performance, integer numbers will be used as
247 identifiers. The platform will use those identifiers to return the relevant
248 information about the image to be loaded (file handler, load address,
249 authentication information, etc.). The following image identifiers are
250 mandatory:
251
252 * **#define : BL2_IMAGE_ID**
253
254 BL2 image identifier, used by BL1 to load BL2.
255
256 * **#define : BL31_IMAGE_ID**
257
258 BL31 image identifier, used by BL2 to load BL31.
259
260 * **#define : BL33_IMAGE_ID**
261
262 BL33 image identifier, used by BL2 to load BL33.
263
264 If Trusted Board Boot is enabled, the following certificate identifiers must
265 also be defined:
266
267 * **#define : TRUSTED_BOOT_FW_CERT_ID**
268
269 BL2 content certificate identifier, used by BL1 to load the BL2 content
270 certificate.
271
272 * **#define : TRUSTED_KEY_CERT_ID**
273
274 Trusted key certificate identifier, used by BL2 to load the trusted key
275 certificate.
276
277 * **#define : SOC_FW_KEY_CERT_ID**
278
279 BL31 key certificate identifier, used by BL2 to load the BL31 key
280 certificate.
281
282 * **#define : SOC_FW_CONTENT_CERT_ID**
283
284 BL31 content certificate identifier, used by BL2 to load the BL31 content
285 certificate.
286
287 * **#define : NON_TRUSTED_FW_KEY_CERT_ID**
288
289 BL33 key certificate identifier, used by BL2 to load the BL33 key
290 certificate.
291
292 * **#define : NON_TRUSTED_FW_CONTENT_CERT_ID**
293
294 BL33 content certificate identifier, used by BL2 to load the BL33 content
295 certificate.
296
297 * **#define : FWU_CERT_ID**
298
299 Firmware Update (FWU) certificate identifier, used by NS_BL1U to load the
300 FWU content certificate.
301
302
303 If the AP Firmware Updater Configuration image, BL2U is used, the following
304 must also be defined:
305
306 * **#define : BL2U_BASE**
307
308 Defines the base address in secure memory where BL1 copies the BL2U binary
309 image. Must be aligned on a page-size boundary.
310
311 * **#define : BL2U_LIMIT**
312
313 Defines the maximum address in secure memory that the BL2U image can occupy.
314
315 * **#define : BL2U_IMAGE_ID**
316
317 BL2U image identifier, used by BL1 to fetch an image descriptor
318 corresponding to BL2U.
319
320 If the SCP Firmware Update Configuration Image, SCP_BL2U is used, the following
321 must also be defined:
322
323 * **#define : SCP_BL2U_IMAGE_ID**
324
325 SCP_BL2U image identifier, used by BL1 to fetch an image descriptor
326 corresponding to SCP_BL2U.
327 NOTE: TF does not provide source code for this image.
328
329 If the Non-Secure Firmware Updater ROM, NS_BL1U is used, the following must
330 also be defined:
331
332 * **#define : NS_BL1U_BASE**
333
334 Defines the base address in non-secure ROM where NS_BL1U executes.
335 Must be aligned on a page-size boundary.
336 NOTE: TF does not provide source code for this image.
337
338 * **#define : NS_BL1U_IMAGE_ID**
339
340 NS_BL1U image identifier, used by BL1 to fetch an image descriptor
341 corresponding to NS_BL1U.
342
343 If the Non-Secure Firmware Updater, NS_BL2U is used, the following must also
344 be defined:
345
346 * **#define : NS_BL2U_BASE**
347
348 Defines the base address in non-secure memory where NS_BL2U executes.
349 Must be aligned on a page-size boundary.
350 NOTE: TF does not provide source code for this image.
351
352 * **#define : NS_BL2U_IMAGE_ID**
353
354 NS_BL2U image identifier, used by BL1 to fetch an image descriptor
355 corresponding to NS_BL2U.
356
357
358 If a SCP_BL2 image is supported by the platform, the following constants must
359 also be defined:
360
361 * **#define : SCP_BL2_IMAGE_ID**
362
363 SCP_BL2 image identifier, used by BL2 to load SCP_BL2 into secure memory
364 from platform storage before being transfered to the SCP.
365
366 * **#define : SCP_FW_KEY_CERT_ID**
367
368 SCP_BL2 key certificate identifier, used by BL2 to load the SCP_BL2 key
369 certificate (mandatory when Trusted Board Boot is enabled).
370
371 * **#define : SCP_FW_CONTENT_CERT_ID**
372
373 SCP_BL2 content certificate identifier, used by BL2 to load the SCP_BL2
374 content certificate (mandatory when Trusted Board Boot is enabled).
375
376 If a BL32 image is supported by the platform, the following constants must
377 also be defined:
378
379 * **#define : BL32_IMAGE_ID**
380
381 BL32 image identifier, used by BL2 to load BL32.
382
383 * **#define : TRUSTED_OS_FW_KEY_CERT_ID**
384
385 BL32 key certificate identifier, used by BL2 to load the BL32 key
386 certificate (mandatory when Trusted Board Boot is enabled).
387
388 * **#define : TRUSTED_OS_FW_CONTENT_CERT_ID**
389
390 BL32 content certificate identifier, used by BL2 to load the BL32 content
391 certificate (mandatory when Trusted Board Boot is enabled).
392
393 * **#define : BL32_BASE**
394
395 Defines the base address in secure memory where BL2 loads the BL32 binary
396 image. Must be aligned on a page-size boundary.
397
398 * **#define : BL32_LIMIT**
399
400 Defines the maximum address that the BL32 image can occupy.
401
402 If the Test Secure-EL1 Payload (TSP) instantiation of BL32 is supported by the
403 platform, the following constants must also be defined:
404
405 * **#define : TSP_SEC_MEM_BASE**
406
407 Defines the base address of the secure memory used by the TSP image on the
408 platform. This must be at the same address or below `BL32_BASE`.
409
410 * **#define : TSP_SEC_MEM_SIZE**
411
412 Defines the size of the secure memory used by the BL32 image on the
413 platform. `TSP_SEC_MEM_BASE` and `TSP_SEC_MEM_SIZE` must fully accomodate
414 the memory required by the BL32 image, defined by `BL32_BASE` and
415 `BL32_LIMIT`.
416
417 * **#define : TSP_IRQ_SEC_PHY_TIMER**
418
419 Defines the ID of the secure physical generic timer interrupt used by the
420 TSP's interrupt handling code.
421
422 If the platform port uses the translation table library code, the following
423 constant must also be defined:
424
425 * **#define : MAX_XLAT_TABLES**
426
427 Defines the maximum number of translation tables that are allocated by the
428 translation table library code. To minimize the amount of runtime memory
429 used, choose the smallest value needed to map the required virtual addresses
430 for each BL stage.
431
432 * **#define : MAX_MMAP_REGIONS**
433
434 Defines the maximum number of regions that are allocated by the translation
435 table library code. A region consists of physical base address, virtual base
436 address, size and attributes (Device/Memory, RO/RW, Secure/Non-Secure), as
437 defined in the `mmap_region_t` structure. The platform defines the regions
438 that should be mapped. Then, the translation table library will create the
439 corresponding tables and descriptors at runtime. To minimize the amount of
440 runtime memory used, choose the smallest value needed to register the
441 required regions for each BL stage.
442
443 * **#define : ADDR_SPACE_SIZE**
444
445 Defines the total size of the address space in bytes. For example, for a 32
446 bit address space, this value should be `(1ull << 32)`.
447
448 If the platform port uses the IO storage framework, the following constants
449 must also be defined:
450
451 * **#define : MAX_IO_DEVICES**
452
453 Defines the maximum number of registered IO devices. Attempting to register
454 more devices than this value using `io_register_device()` will fail with
455 -ENOMEM.
456
457 * **#define : MAX_IO_HANDLES**
458
459 Defines the maximum number of open IO handles. Attempting to open more IO
460 entities than this value using `io_open()` will fail with -ENOMEM.
461
462 * **#define : MAX_IO_BLOCK_DEVICES**
463
464 Defines the maximum number of registered IO block devices. Attempting to
465 register more devices this value using `io_dev_open()` will fail
466 with -ENOMEM. MAX_IO_BLOCK_DEVICES should be less than MAX_IO_DEVICES.
467 With this macro, multiple block devices could be supported at the same
468 time.
469
470 If the platform needs to allocate data within the per-cpu data framework in
471 BL31, it should define the following macro. Currently this is only required if
472 the platform decides not to use the coherent memory section by undefining the
473 `USE_COHERENT_MEM` build flag. In this case, the framework allocates the
474 required memory within the the per-cpu data to minimize wastage.
475
476 * **#define : PLAT_PCPU_DATA_SIZE**
477
478 Defines the memory (in bytes) to be reserved within the per-cpu data
479 structure for use by the platform layer.
480
481 The following constants are optional. They should be defined when the platform
482 memory layout implies some image overlaying like in ARM standard platforms.
483
484 * **#define : BL31_PROGBITS_LIMIT**
485
486 Defines the maximum address in secure RAM that the BL31's progbits sections
487 can occupy.
488
489 * **#define : TSP_PROGBITS_LIMIT**
490
491 Defines the maximum address that the TSP's progbits sections can occupy.
492
493 If the platform port uses the PL061 GPIO driver, the following constant may
494 optionally be defined:
495
496 * **PLAT_PL061_MAX_GPIOS**
497 Maximum number of GPIOs required by the platform. This allows control how
498 much memory is allocated for PL061 GPIO controllers. The default value is
499 32.
500 [For example, define the build flag in platform.mk]:
501 PLAT_PL061_MAX_GPIOS := 160
502 $(eval $(call add_define,PLAT_PL061_MAX_GPIOS))
503
504 If the platform port uses the partition driver, the following constant may
505 optionally be defined:
506
507 * **PLAT_PARTITION_MAX_ENTRIES**
508 Maximum number of partition entries required by the platform. This allows
509 control how much memory is allocated for partition entries. The default
510 value is 128.
511 [For example, define the build flag in platform.mk]:
512 PLAT_PARTITION_MAX_ENTRIES := 12
513 $(eval $(call add_define,PLAT_PARTITION_MAX_ENTRIES))
514
515
516 ### File : plat_macros.S [mandatory]
517
518 Each platform must ensure a file of this name is in the system include path with
519 the following macro defined. In the ARM development platforms, this file is
520 found in `plat/arm/board/<plat_name>/include/plat_macros.S`.
521
522 * **Macro : plat_crash_print_regs**
523
524 This macro allows the crash reporting routine to print relevant platform
525 registers in case of an unhandled exception in BL31. This aids in debugging
526 and this macro can be defined to be empty in case register reporting is not
527 desired.
528
529 For instance, GIC or interconnect registers may be helpful for
530 troubleshooting.
531
532
533 2.2 Handling Reset
534 ------------------
535
536 BL1 by default implements the reset vector where execution starts from a cold
537 or warm boot. BL31 can be optionally set as a reset vector using the
538 `RESET_TO_BL31` make variable.
539
540 For each CPU, the reset vector code is responsible for the following tasks:
541
542 1. Distinguishing between a cold boot and a warm boot.
543
544 2. In the case of a cold boot and the CPU being a secondary CPU, ensuring that
545 the CPU is placed in a platform-specific state until the primary CPU
546 performs the necessary steps to remove it from this state.
547
548 3. In the case of a warm boot, ensuring that the CPU jumps to a platform-
549 specific address in the BL31 image in the same processor mode as it was
550 when released from reset.
551
552 The following functions need to be implemented by the platform port to enable
553 reset vector code to perform the above tasks.
554
555
556 ### Function : plat_get_my_entrypoint() [mandatory when PROGRAMMABLE_RESET_ADDRESS == 0]
557
558 Argument : void
559 Return : uintptr_t
560
561 This function is called with the called with the MMU and caches disabled
562 (`SCTLR_EL3.M` = 0 and `SCTLR_EL3.C` = 0). The function is responsible for
563 distinguishing between a warm and cold reset for the current CPU using
564 platform-specific means. If it's a warm reset, then it returns the warm
565 reset entrypoint point provided to `plat_setup_psci_ops()` during
566 BL31 initialization. If it's a cold reset then this function must return zero.
567
568 This function does not follow the Procedure Call Standard used by the
569 Application Binary Interface for the ARM 64-bit architecture. The caller should
570 not assume that callee saved registers are preserved across a call to this
571 function.
572
573 This function fulfills requirement 1 and 3 listed above.
574
575 Note that for platforms that support programming the reset address, it is
576 expected that a CPU will start executing code directly at the right address,
577 both on a cold and warm reset. In this case, there is no need to identify the
578 type of reset nor to query the warm reset entrypoint. Therefore, implementing
579 this function is not required on such platforms.
580
581
582 ### Function : plat_secondary_cold_boot_setup() [mandatory when COLD_BOOT_SINGLE_CPU == 0]
583
584 Argument : void
585
586 This function is called with the MMU and data caches disabled. It is responsible
587 for placing the executing secondary CPU in a platform-specific state until the
588 primary CPU performs the necessary actions to bring it out of that state and
589 allow entry into the OS. This function must not return.
590
591 In the ARM FVP port, when using the normal boot flow, each secondary CPU powers
592 itself off. The primary CPU is responsible for powering up the secondary CPUs
593 when normal world software requires them. When booting an EL3 payload instead,
594 they stay powered on and are put in a holding pen until their mailbox gets
595 populated.
596
597 This function fulfills requirement 2 above.
598
599 Note that for platforms that can't release secondary CPUs out of reset, only the
600 primary CPU will execute the cold boot code. Therefore, implementing this
601 function is not required on such platforms.
602
603
604 ### Function : plat_is_my_cpu_primary() [mandatory when COLD_BOOT_SINGLE_CPU == 0]
605
606 Argument : void
607 Return : unsigned int
608
609 This function identifies whether the current CPU is the primary CPU or a
610 secondary CPU. A return value of zero indicates that the CPU is not the
611 primary CPU, while a non-zero return value indicates that the CPU is the
612 primary CPU.
613
614 Note that for platforms that can't release secondary CPUs out of reset, only the
615 primary CPU will execute the cold boot code. Therefore, there is no need to
616 distinguish between primary and secondary CPUs and implementing this function is
617 not required.
618
619
620 ### Function : platform_mem_init() [mandatory]
621
622 Argument : void
623 Return : void
624
625 This function is called before any access to data is made by the firmware, in
626 order to carry out any essential memory initialization.
627
628
629 ### Function: plat_get_rotpk_info()
630
631 Argument : void *, void **, unsigned int *, unsigned int *
632 Return : int
633
634 This function is mandatory when Trusted Board Boot is enabled. It returns a
635 pointer to the ROTPK stored in the platform (or a hash of it) and its length.
636 The ROTPK must be encoded in DER format according to the following ASN.1
637 structure:
638
639 AlgorithmIdentifier ::= SEQUENCE {
640 algorithm OBJECT IDENTIFIER,
641 parameters ANY DEFINED BY algorithm OPTIONAL
642 }
643
644 SubjectPublicKeyInfo ::= SEQUENCE {
645 algorithm AlgorithmIdentifier,
646 subjectPublicKey BIT STRING
647 }
648
649 In case the function returns a hash of the key:
650
651 DigestInfo ::= SEQUENCE {
652 digestAlgorithm AlgorithmIdentifier,
653 digest OCTET STRING
654 }
655
656 The function returns 0 on success. Any other value is treated as error by the
657 Trusted Board Boot. The function also reports extra information related
658 to the ROTPK in the flags parameter:
659
660 ROTPK_IS_HASH : Indicates that the ROTPK returned by the platform is a
661 hash.
662 ROTPK_NOT_DEPLOYED : This allows the platform to skip certificate ROTPK
663 verification while the platform ROTPK is not deployed.
664 When this flag is set, the function does not need to
665 return a platform ROTPK, and the authentication
666 framework uses the ROTPK in the certificate without
667 verifying it against the platform value. This flag
668 must not be used in a deployed production environment.
669
670 ### Function: plat_get_nv_ctr()
671
672 Argument : void *, unsigned int *
673 Return : int
674
675 This function is mandatory when Trusted Board Boot is enabled. It returns the
676 non-volatile counter value stored in the platform in the second argument. The
677 cookie in the first argument may be used to select the counter in case the
678 platform provides more than one (for example, on platforms that use the default
679 TBBR CoT, the cookie will correspond to the OID values defined in
680 TRUSTED_FW_NVCOUNTER_OID or NON_TRUSTED_FW_NVCOUNTER_OID).
681
682 The function returns 0 on success. Any other value means the counter value could
683 not be retrieved from the platform.
684
685
686 ### Function: plat_set_nv_ctr()
687
688 Argument : void *, unsigned int
689 Return : int
690
691 This function is mandatory when Trusted Board Boot is enabled. It sets a new
692 counter value in the platform. The cookie in the first argument may be used to
693 select the counter (as explained in plat_get_nv_ctr()).
694
695 The function returns 0 on success. Any other value means the counter value could
696 not be updated.
697
698
699 2.3 Common mandatory function modifications
700 ---------------------------------
701
702 The following functions are mandatory functions which need to be implemented
703 by the platform port.
704
705 ### Function : plat_my_core_pos()
706
707 Argument : void
708 Return : unsigned int
709
710 This funtion returns the index of the calling CPU which is used as a
711 CPU-specific linear index into blocks of memory (for example while allocating
712 per-CPU stacks). This function will be invoked very early in the
713 initialization sequence which mandates that this function should be
714 implemented in assembly and should not rely on the avalability of a C
715 runtime environment. This function can clobber x0 - x8 and must preserve
716 x9 - x29.
717
718 This function plays a crucial role in the power domain topology framework in
719 PSCI and details of this can be found in [Power Domain Topology Design].
720
721 ### Function : plat_core_pos_by_mpidr()
722
723 Argument : u_register_t
724 Return : int
725
726 This function validates the `MPIDR` of a CPU and converts it to an index,
727 which can be used as a CPU-specific linear index into blocks of memory. In
728 case the `MPIDR` is invalid, this function returns -1. This function will only
729 be invoked by BL31 after the power domain topology is initialized and can
730 utilize the C runtime environment. For further details about how ARM Trusted
731 Firmware represents the power domain topology and how this relates to the
732 linear CPU index, please refer [Power Domain Topology Design].
733
734
735 2.4 Common optional modifications
736 ---------------------------------
737
738 The following are helper functions implemented by the firmware that perform
739 common platform-specific tasks. A platform may choose to override these
740 definitions.
741
742 ### Function : plat_set_my_stack()
743
744 Argument : void
745 Return : void
746
747 This function sets the current stack pointer to the normal memory stack that
748 has been allocated for the current CPU. For BL images that only require a
749 stack for the primary CPU, the UP version of the function is used. The size
750 of the stack allocated to each CPU is specified by the platform defined
751 constant `PLATFORM_STACK_SIZE`.
752
753 Common implementations of this function for the UP and MP BL images are
754 provided in [plat/common/aarch64/platform_up_stack.S] and
755 [plat/common/aarch64/platform_mp_stack.S]
756
757
758 ### Function : plat_get_my_stack()
759
760 Argument : void
761 Return : uintptr_t
762
763 This function returns the base address of the normal memory stack that
764 has been allocated for the current CPU. For BL images that only require a
765 stack for the primary CPU, the UP version of the function is used. The size
766 of the stack allocated to each CPU is specified by the platform defined
767 constant `PLATFORM_STACK_SIZE`.
768
769 Common implementations of this function for the UP and MP BL images are
770 provided in [plat/common/aarch64/platform_up_stack.S] and
771 [plat/common/aarch64/platform_mp_stack.S]
772
773
774 ### Function : plat_report_exception()
775
776 Argument : unsigned int
777 Return : void
778
779 A platform may need to report various information about its status when an
780 exception is taken, for example the current exception level, the CPU security
781 state (secure/non-secure), the exception type, and so on. This function is
782 called in the following circumstances:
783
784 * In BL1, whenever an exception is taken.
785 * In BL2, whenever an exception is taken.
786
787 The default implementation doesn't do anything, to avoid making assumptions
788 about the way the platform displays its status information.
789
790 For AArch64, this function receives the exception type as its argument.
791 Possible values for exceptions types are listed in the
792 [include/common/bl_common.h] header file. Note that these constants are not
793 related to any architectural exception code; they are just an ARM Trusted
794 Firmware convention.
795
796 For AArch32, this function receives the exception mode as its argument.
797 Possible values for exception modes are listed in the
798 [include/lib/aarch32/arch.h] header file.
799
800 ### Function : plat_reset_handler()
801
802 Argument : void
803 Return : void
804
805 A platform may need to do additional initialization after reset. This function
806 allows the platform to do the platform specific intializations. Platform
807 specific errata workarounds could also be implemented here. The api should
808 preserve the values of callee saved registers x19 to x29.
809
810 The default implementation doesn't do anything. If a platform needs to override
811 the default implementation, refer to the [Firmware Design] for general
812 guidelines.
813
814 ### Function : plat_disable_acp()
815
816 Argument : void
817 Return : void
818
819 This api allows a platform to disable the Accelerator Coherency Port (if
820 present) during a cluster power down sequence. The default weak implementation
821 doesn't do anything. Since this api is called during the power down sequence,
822 it has restrictions for stack usage and it can use the registers x0 - x17 as
823 scratch registers. It should preserve the value in x18 register as it is used
824 by the caller to store the return address.
825
826 ### Function : plat_error_handler()
827
828 Argument : int
829 Return : void
830
831 This API is called when the generic code encounters an error situation from
832 which it cannot continue. It allows the platform to perform error reporting or
833 recovery actions (for example, reset the system). This function must not return.
834
835 The parameter indicates the type of error using standard codes from `errno.h`.
836 Possible errors reported by the generic code are:
837
838 * `-EAUTH`: a certificate or image could not be authenticated (when Trusted
839 Board Boot is enabled)
840 * `-ENOENT`: the requested image or certificate could not be found or an IO
841 error was detected
842 * `-ENOMEM`: resources exhausted. Trusted Firmware does not use dynamic
843 memory, so this error is usually an indication of an incorrect array size
844
845 The default implementation simply spins.
846
847 ### Function : plat_panic_handler()
848
849 Argument : void
850 Return : void
851
852 This API is called when the generic code encounters an unexpected error
853 situation from which it cannot recover. This function must not return,
854 and must be implemented in assembly because it may be called before the C
855 environment is initialized.
856
857 Note: The address from where it was called is stored in x30 (Link Register).
858 The default implementation simply spins.
859
860
861 ### Function : plat_get_bl_image_load_info()
862
863 Argument : void
864 Return : bl_load_info_t *
865
866 This function returns pointer to the list of images that the platform has
867 populated to load. This function is currently invoked in BL2 to load the
868 BL3xx images, when LOAD_IMAGE_V2 is enabled.
869
870 ### Function : plat_get_next_bl_params()
871
872 Argument : void
873 Return : bl_params_t *
874
875 This function returns a pointer to the shared memory that the platform has
876 kept aside to pass trusted firmware related information that next BL image
877 needs. This function is currently invoked in BL2 to pass this information to
878 the next BL image, when LOAD_IMAGE_V2 is enabled.
879
880 ### Function : plat_flush_next_bl_params()
881
882 Argument : void
883 Return : void
884
885 This function flushes to main memory all the image params that are passed to
886 next image. This function is currently invoked in BL2 to flush this information
887 to the next BL image, when LOAD_IMAGE_V2 is enabled.
888
889 3. Modifications specific to a Boot Loader stage
890 -------------------------------------------------
891
892 3.1 Boot Loader Stage 1 (BL1)
893 -----------------------------
894
895 BL1 implements the reset vector where execution starts from after a cold or
896 warm boot. For each CPU, BL1 is responsible for the following tasks:
897
898 1. Handling the reset as described in section 2.2
899
900 2. In the case of a cold boot and the CPU being the primary CPU, ensuring that
901 only this CPU executes the remaining BL1 code, including loading and passing
902 control to the BL2 stage.
903
904 3. Identifying and starting the Firmware Update process (if required).
905
906 4. Loading the BL2 image from non-volatile storage into secure memory at the
907 address specified by the platform defined constant `BL2_BASE`.
908
909 5. Populating a `meminfo` structure with the following information in memory,
910 accessible by BL2 immediately upon entry.
911
912 meminfo.total_base = Base address of secure RAM visible to BL2
913 meminfo.total_size = Size of secure RAM visible to BL2
914 meminfo.free_base = Base address of secure RAM available for
915 allocation to BL2
916 meminfo.free_size = Size of secure RAM available for allocation to BL2
917
918 BL1 places this `meminfo` structure at the beginning of the free memory
919 available for its use. Since BL1 cannot allocate memory dynamically at the
920 moment, its free memory will be available for BL2's use as-is. However, this
921 means that BL2 must read the `meminfo` structure before it starts using its
922 free memory (this is discussed in Section 3.2).
923
924 In future releases of the ARM Trusted Firmware it will be possible for
925 the platform to decide where it wants to place the `meminfo` structure for
926 BL2.
927
928 BL1 implements the `bl1_init_bl2_mem_layout()` function to populate the
929 BL2 `meminfo` structure. The platform may override this implementation, for
930 example if the platform wants to restrict the amount of memory visible to
931 BL2. Details of how to do this are given below.
932
933 The following functions need to be implemented by the platform port to enable
934 BL1 to perform the above tasks.
935
936
937 ### Function : bl1_early_platform_setup() [mandatory]
938
939 Argument : void
940 Return : void
941
942 This function executes with the MMU and data caches disabled. It is only called
943 by the primary CPU.
944
945 On ARM standard platforms, this function:
946
947 * Enables a secure instance of SP805 to act as the Trusted Watchdog.
948
949 * Initializes a UART (PL011 console), which enables access to the `printf`
950 family of functions in BL1.
951
952 * Enables issuing of snoop and DVM (Distributed Virtual Memory) requests to
953 the CCI slave interface corresponding to the cluster that includes the
954 primary CPU.
955
956 ### Function : bl1_plat_arch_setup() [mandatory]
957
958 Argument : void
959 Return : void
960
961 This function performs any platform-specific and architectural setup that the
962 platform requires. Platform-specific setup might include configuration of
963 memory controllers and the interconnect.
964
965 In ARM standard platforms, this function enables the MMU.
966
967 This function helps fulfill requirement 2 above.
968
969
970 ### Function : bl1_platform_setup() [mandatory]
971
972 Argument : void
973 Return : void
974
975 This function executes with the MMU and data caches enabled. It is responsible
976 for performing any remaining platform-specific setup that can occur after the
977 MMU and data cache have been enabled.
978
979 In ARM standard platforms, this function initializes the storage abstraction
980 layer used to load the next bootloader image.
981
982 This function helps fulfill requirement 4 above.
983
984
985 ### Function : bl1_plat_sec_mem_layout() [mandatory]
986
987 Argument : void
988 Return : meminfo *
989
990 This function should only be called on the cold boot path. It executes with the
991 MMU and data caches enabled. The pointer returned by this function must point to
992 a `meminfo` structure containing the extents and availability of secure RAM for
993 the BL1 stage.
994
995 meminfo.total_base = Base address of secure RAM visible to BL1
996 meminfo.total_size = Size of secure RAM visible to BL1
997 meminfo.free_base = Base address of secure RAM available for allocation
998 to BL1
999 meminfo.free_size = Size of secure RAM available for allocation to BL1
1000
1001 This information is used by BL1 to load the BL2 image in secure RAM. BL1 also
1002 populates a similar structure to tell BL2 the extents of memory available for
1003 its own use.
1004
1005 This function helps fulfill requirements 4 and 5 above.
1006
1007
1008 ### Function : bl1_init_bl2_mem_layout() [optional]
1009
1010 Argument : meminfo *, meminfo *
1011 Return : void
1012
1013 BL1 needs to tell the next stage the amount of secure RAM available
1014 for it to use. This information is populated in a `meminfo`
1015 structure.
1016
1017 Depending upon where BL2 has been loaded in secure RAM (determined by
1018 `BL2_BASE`), BL1 calculates the amount of free memory available for BL2 to use.
1019 BL1 also ensures that its data sections resident in secure RAM are not visible
1020 to BL2. An illustration of how this is done in ARM standard platforms is given
1021 in the **Memory layout on ARM development platforms** section in the
1022 [Firmware Design].
1023
1024
1025 ### Function : bl1_plat_prepare_exit() [optional]
1026
1027 Argument : entry_point_info_t *
1028 Return : void
1029
1030 This function is called prior to exiting BL1 in response to the
1031 `BL1_SMC_RUN_IMAGE` SMC request raised by BL2. It should be used to perform
1032 platform specific clean up or bookkeeping operations before transferring
1033 control to the next image. It receives the address of the `entry_point_info_t`
1034 structure passed from BL2. This function runs with MMU disabled.
1035
1036 ### Function : bl1_plat_set_ep_info() [optional]
1037
1038 Argument : unsigned int image_id, entry_point_info_t *ep_info
1039 Return : void
1040
1041 This function allows platforms to override `ep_info` for the given `image_id`.
1042
1043 The default implementation just returns.
1044
1045 ### Function : bl1_plat_get_next_image_id() [optional]
1046
1047 Argument : void
1048 Return : unsigned int
1049
1050 This and the following function must be overridden to enable the FWU feature.
1051
1052 BL1 calls this function after platform setup to identify the next image to be
1053 loaded and executed. If the platform returns `BL2_IMAGE_ID` then BL1 proceeds
1054 with the normal boot sequence, which loads and executes BL2. If the platform
1055 returns a different image id, BL1 assumes that Firmware Update is required.
1056
1057 The default implementation always returns `BL2_IMAGE_ID`. The ARM development
1058 platforms override this function to detect if firmware update is required, and
1059 if so, return the first image in the firmware update process.
1060
1061 ### Function : bl1_plat_get_image_desc() [optional]
1062
1063 Argument : unsigned int image_id
1064 Return : image_desc_t *
1065
1066 BL1 calls this function to get the image descriptor information `image_desc_t`
1067 for the provided `image_id` from the platform.
1068
1069 The default implementation always returns a common BL2 image descriptor. ARM
1070 standard platforms return an image descriptor corresponding to BL2 or one of
1071 the firmware update images defined in the Trusted Board Boot Requirements
1072 specification.
1073
1074 ### Function : bl1_plat_fwu_done() [optional]
1075
1076 Argument : unsigned int image_id, uintptr_t image_src,
1077 unsigned int image_size
1078 Return : void
1079
1080 BL1 calls this function when the FWU process is complete. It must not return.
1081 The platform may override this function to take platform specific action, for
1082 example to initiate the normal boot flow.
1083
1084 The default implementation spins forever.
1085
1086 ### Function : bl1_plat_mem_check() [mandatory]
1087
1088 Argument : uintptr_t mem_base, unsigned int mem_size,
1089 unsigned int flags
1090 Return : void
1091
1092 BL1 calls this function while handling FWU copy and authenticate SMCs. The
1093 platform must ensure that the provided `mem_base` and `mem_size` are mapped into
1094 BL1, and that this memory corresponds to either a secure or non-secure memory
1095 region as indicated by the security state of the `flags` argument.
1096
1097 The default implementation of this function asserts therefore platforms must
1098 override it when using the FWU feature.
1099
1100
1101 3.2 Boot Loader Stage 2 (BL2)
1102 -----------------------------
1103
1104 The BL2 stage is executed only by the primary CPU, which is determined in BL1
1105 using the `platform_is_primary_cpu()` function. BL1 passed control to BL2 at
1106 `BL2_BASE`. BL2 executes in Secure EL1 and is responsible for:
1107
1108 1. (Optional) Loading the SCP_BL2 binary image (if present) from platform
1109 provided non-volatile storage. To load the SCP_BL2 image, BL2 makes use of
1110 the `meminfo` returned by the `bl2_plat_get_scp_bl2_meminfo()` function.
1111 The platform also defines the address in memory where SCP_BL2 is loaded
1112 through the optional constant `SCP_BL2_BASE`. BL2 uses this information
1113 to determine if there is enough memory to load the SCP_BL2 image.
1114 Subsequent handling of the SCP_BL2 image is platform-specific and is
1115 implemented in the `bl2_plat_handle_scp_bl2()` function.
1116 If `SCP_BL2_BASE` is not defined then this step is not performed.
1117
1118 2. Loading the BL31 binary image into secure RAM from non-volatile storage. To
1119 load the BL31 image, BL2 makes use of the `meminfo` structure passed to it
1120 by BL1. This structure allows BL2 to calculate how much secure RAM is
1121 available for its use. The platform also defines the address in secure RAM
1122 where BL31 is loaded through the constant `BL31_BASE`. BL2 uses this
1123 information to determine if there is enough memory to load the BL31 image.
1124
1125 3. (Optional) Loading the BL32 binary image (if present) from platform
1126 provided non-volatile storage. To load the BL32 image, BL2 makes use of
1127 the `meminfo` returned by the `bl2_plat_get_bl32_meminfo()` function.
1128 The platform also defines the address in memory where BL32 is loaded
1129 through the optional constant `BL32_BASE`. BL2 uses this information
1130 to determine if there is enough memory to load the BL32 image.
1131 If `BL32_BASE` is not defined then this and the next step is not performed.
1132
1133 4. (Optional) Arranging to pass control to the BL32 image (if present) that
1134 has been pre-loaded at `BL32_BASE`. BL2 populates an `entry_point_info`
1135 structure in memory provided by the platform with information about how
1136 BL31 should pass control to the BL32 image.
1137
1138 5. (Optional) Loading the normal world BL33 binary image (if not loaded by
1139 other means) into non-secure DRAM from platform storage and arranging for
1140 BL31 to pass control to this image. This address is determined using the
1141 `plat_get_ns_image_entrypoint()` function described below.
1142
1143 6. BL2 populates an `entry_point_info` structure in memory provided by the
1144 platform with information about how BL31 should pass control to the
1145 other BL images.
1146
1147 The following functions must be implemented by the platform port to enable BL2
1148 to perform the above tasks.
1149
1150
1151 ### Function : bl2_early_platform_setup() [mandatory]
1152
1153 Argument : meminfo *
1154 Return : void
1155
1156 This function executes with the MMU and data caches disabled. It is only called
1157 by the primary CPU. The arguments to this function is the address of the
1158 `meminfo` structure populated by BL1.
1159
1160 The platform may copy the contents of the `meminfo` structure into a private
1161 variable as the original memory may be subsequently overwritten by BL2. The
1162 copied structure is made available to all BL2 code through the
1163 `bl2_plat_sec_mem_layout()` function.
1164
1165 On ARM standard platforms, this function also:
1166
1167 * Initializes a UART (PL011 console), which enables access to the `printf`
1168 family of functions in BL2.
1169
1170 * Initializes the storage abstraction layer used to load further bootloader
1171 images. It is necessary to do this early on platforms with a SCP_BL2 image,
1172 since the later `bl2_platform_setup` must be done after SCP_BL2 is loaded.
1173
1174
1175 ### Function : bl2_plat_arch_setup() [mandatory]
1176
1177 Argument : void
1178 Return : void
1179
1180 This function executes with the MMU and data caches disabled. It is only called
1181 by the primary CPU.
1182
1183 The purpose of this function is to perform any architectural initialization
1184 that varies across platforms.
1185
1186 On ARM standard platforms, this function enables the MMU.
1187
1188 ### Function : bl2_platform_setup() [mandatory]
1189
1190 Argument : void
1191 Return : void
1192
1193 This function may execute with the MMU and data caches enabled if the platform
1194 port does the necessary initialization in `bl2_plat_arch_setup()`. It is only
1195 called by the primary CPU.
1196
1197 The purpose of this function is to perform any platform initialization
1198 specific to BL2.
1199
1200 In ARM standard platforms, this function performs security setup, including
1201 configuration of the TrustZone controller to allow non-secure masters access
1202 to most of DRAM. Part of DRAM is reserved for secure world use.
1203
1204
1205 ### Function : bl2_plat_sec_mem_layout() [mandatory]
1206
1207 Argument : void
1208 Return : meminfo *
1209
1210 This function should only be called on the cold boot path. It may execute with
1211 the MMU and data caches enabled if the platform port does the necessary
1212 initialization in `bl2_plat_arch_setup()`. It is only called by the primary CPU.
1213
1214 The purpose of this function is to return a pointer to a `meminfo` structure
1215 populated with the extents of secure RAM available for BL2 to use. See
1216 `bl2_early_platform_setup()` above.
1217
1218
1219 Following function is required only when LOAD_IMAGE_V2 is enabled.
1220
1221 ### Function : bl2_plat_handle_post_image_load() [mandatory]
1222
1223 Argument : unsigned int
1224 Return : int
1225
1226 This function can be used by the platforms to update/use image information
1227 for given `image_id`. This function is currently invoked in BL2 to handle
1228 BL image specific information based on the `image_id` passed, when
1229 LOAD_IMAGE_V2 is enabled.
1230
1231 Following functions are required only when LOAD_IMAGE_V2 is disabled.
1232
1233 ### Function : bl2_plat_get_scp_bl2_meminfo() [mandatory]
1234
1235 Argument : meminfo *
1236 Return : void
1237
1238 This function is used to get the memory limits where BL2 can load the
1239 SCP_BL2 image. The meminfo provided by this is used by load_image() to
1240 validate whether the SCP_BL2 image can be loaded within the given
1241 memory from the given base.
1242
1243
1244 ### Function : bl2_plat_handle_scp_bl2() [mandatory]
1245
1246 Argument : image_info *
1247 Return : int
1248
1249 This function is called after loading SCP_BL2 image and it is used to perform
1250 any platform-specific actions required to handle the SCP firmware. Typically it
1251 transfers the image into SCP memory using a platform-specific protocol and waits
1252 until SCP executes it and signals to the Application Processor (AP) for BL2
1253 execution to continue.
1254
1255 This function returns 0 on success, a negative error code otherwise.
1256
1257
1258 ### Function : bl2_plat_get_bl31_params() [mandatory]
1259
1260 Argument : void
1261 Return : bl31_params *
1262
1263 BL2 platform code needs to return a pointer to a `bl31_params` structure it
1264 will use for passing information to BL31. The `bl31_params` structure carries
1265 the following information.
1266 - Header describing the version information for interpreting the bl31_param
1267 structure
1268 - Information about executing the BL33 image in the `bl33_ep_info` field
1269 - Information about executing the BL32 image in the `bl32_ep_info` field
1270 - Information about the type and extents of BL31 image in the
1271 `bl31_image_info` field
1272 - Information about the type and extents of BL32 image in the
1273 `bl32_image_info` field
1274 - Information about the type and extents of BL33 image in the
1275 `bl33_image_info` field
1276
1277 The memory pointed by this structure and its sub-structures should be
1278 accessible from BL31 initialisation code. BL31 might choose to copy the
1279 necessary content, or maintain the structures until BL33 is initialised.
1280
1281
1282 ### Funtion : bl2_plat_get_bl31_ep_info() [mandatory]
1283
1284 Argument : void
1285 Return : entry_point_info *
1286
1287 BL2 platform code returns a pointer which is used to populate the entry point
1288 information for BL31 entry point. The location pointed by it should be
1289 accessible from BL1 while processing the synchronous exception to run to BL31.
1290
1291 In ARM standard platforms this is allocated inside a bl2_to_bl31_params_mem
1292 structure in BL2 memory.
1293
1294
1295 ### Function : bl2_plat_set_bl31_ep_info() [mandatory]
1296
1297 Argument : image_info *, entry_point_info *
1298 Return : void
1299
1300 In the normal boot flow, this function is called after loading BL31 image and
1301 it can be used to overwrite the entry point set by loader and also set the
1302 security state and SPSR which represents the entry point system state for BL31.
1303
1304 When booting an EL3 payload instead, this function is called after populating
1305 its entry point address and can be used for the same purpose for the payload
1306 image. It receives a null pointer as its first argument in this case.
1307
1308 ### Function : bl2_plat_set_bl32_ep_info() [mandatory]
1309
1310 Argument : image_info *, entry_point_info *
1311 Return : void
1312
1313 This function is called after loading BL32 image and it can be used to
1314 overwrite the entry point set by loader and also set the security state
1315 and SPSR which represents the entry point system state for BL32.
1316
1317
1318 ### Function : bl2_plat_set_bl33_ep_info() [mandatory]
1319
1320 Argument : image_info *, entry_point_info *
1321 Return : void
1322
1323 This function is called after loading BL33 image and it can be used to
1324 overwrite the entry point set by loader and also set the security state
1325 and SPSR which represents the entry point system state for BL33.
1326
1327 In the preloaded BL33 alternative boot flow, this function is called after
1328 populating its entry point address. It is passed a null pointer as its first
1329 argument in this case.
1330
1331
1332 ### Function : bl2_plat_get_bl32_meminfo() [mandatory]
1333
1334 Argument : meminfo *
1335 Return : void
1336
1337 This function is used to get the memory limits where BL2 can load the
1338 BL32 image. The meminfo provided by this is used by load_image() to
1339 validate whether the BL32 image can be loaded with in the given
1340 memory from the given base.
1341
1342 ### Function : bl2_plat_get_bl33_meminfo() [mandatory]
1343
1344 Argument : meminfo *
1345 Return : void
1346
1347 This function is used to get the memory limits where BL2 can load the
1348 BL33 image. The meminfo provided by this is used by load_image() to
1349 validate whether the BL33 image can be loaded with in the given
1350 memory from the given base.
1351
1352 This function isn't needed if either `PRELOADED_BL33_BASE` or `EL3_PAYLOAD_BASE`
1353 build options are used.
1354
1355 ### Function : bl2_plat_flush_bl31_params() [mandatory]
1356
1357 Argument : void
1358 Return : void
1359
1360 Once BL2 has populated all the structures that needs to be read by BL1
1361 and BL31 including the bl31_params structures and its sub-structures,
1362 the bl31_ep_info structure and any platform specific data. It flushes
1363 all these data to the main memory so that it is available when we jump to
1364 later Bootloader stages with MMU off
1365
1366 ### Function : plat_get_ns_image_entrypoint() [mandatory]
1367
1368 Argument : void
1369 Return : uintptr_t
1370
1371 As previously described, BL2 is responsible for arranging for control to be
1372 passed to a normal world BL image through BL31. This function returns the
1373 entrypoint of that image, which BL31 uses to jump to it.
1374
1375 BL2 is responsible for loading the normal world BL33 image (e.g. UEFI).
1376
1377 This function isn't needed if either `PRELOADED_BL33_BASE` or `EL3_PAYLOAD_BASE`
1378 build options are used.
1379
1380
1381 3.3 FWU Boot Loader Stage 2 (BL2U)
1382 ----------------------------------
1383
1384 The AP Firmware Updater Configuration, BL2U, is an optional part of the FWU
1385 process and is executed only by the primary CPU. BL1 passes control to BL2U at
1386 `BL2U_BASE`. BL2U executes in Secure-EL1 and is responsible for:
1387
1388 1. (Optional) Transfering the optional SCP_BL2U binary image from AP secure
1389 memory to SCP RAM. BL2U uses the SCP_BL2U `image_info` passed by BL1.
1390 `SCP_BL2U_BASE` defines the address in AP secure memory where SCP_BL2U
1391 should be copied from. Subsequent handling of the SCP_BL2U image is
1392 implemented by the platform specific `bl2u_plat_handle_scp_bl2u()` function.
1393 If `SCP_BL2U_BASE` is not defined then this step is not performed.
1394
1395 2. Any platform specific setup required to perform the FWU process. For
1396 example, ARM standard platforms initialize the TZC controller so that the
1397 normal world can access DDR memory.
1398
1399 The following functions must be implemented by the platform port to enable
1400 BL2U to perform the tasks mentioned above.
1401
1402 ### Function : bl2u_early_platform_setup() [mandatory]
1403
1404 Argument : meminfo *mem_info, void *plat_info
1405 Return : void
1406
1407 This function executes with the MMU and data caches disabled. It is only
1408 called by the primary CPU. The arguments to this function is the address
1409 of the `meminfo` structure and platform specific info provided by BL1.
1410
1411 The platform may copy the contents of the `mem_info` and `plat_info` into
1412 private storage as the original memory may be subsequently overwritten by BL2U.
1413
1414 On ARM CSS platforms `plat_info` is interpreted as an `image_info_t` structure,
1415 to extract SCP_BL2U image information, which is then copied into a private
1416 variable.
1417
1418 ### Function : bl2u_plat_arch_setup() [mandatory]
1419
1420 Argument : void
1421 Return : void
1422
1423 This function executes with the MMU and data caches disabled. It is only
1424 called by the primary CPU.
1425
1426 The purpose of this function is to perform any architectural initialization
1427 that varies across platforms, for example enabling the MMU (since the memory
1428 map differs across platforms).
1429
1430 ### Function : bl2u_platform_setup() [mandatory]
1431
1432 Argument : void
1433 Return : void
1434
1435 This function may execute with the MMU and data caches enabled if the platform
1436 port does the necessary initialization in `bl2u_plat_arch_setup()`. It is only
1437 called by the primary CPU.
1438
1439 The purpose of this function is to perform any platform initialization
1440 specific to BL2U.
1441
1442 In ARM standard platforms, this function performs security setup, including
1443 configuration of the TrustZone controller to allow non-secure masters access
1444 to most of DRAM. Part of DRAM is reserved for secure world use.
1445
1446 ### Function : bl2u_plat_handle_scp_bl2u() [optional]
1447
1448 Argument : void
1449 Return : int
1450
1451 This function is used to perform any platform-specific actions required to
1452 handle the SCP firmware. Typically it transfers the image into SCP memory using
1453 a platform-specific protocol and waits until SCP executes it and signals to the
1454 Application Processor (AP) for BL2U execution to continue.
1455
1456 This function returns 0 on success, a negative error code otherwise.
1457 This function is included if SCP_BL2U_BASE is defined.
1458
1459
1460 3.4 Boot Loader Stage 3-1 (BL31)
1461 ---------------------------------
1462
1463 During cold boot, the BL31 stage is executed only by the primary CPU. This is
1464 determined in BL1 using the `platform_is_primary_cpu()` function. BL1 passes
1465 control to BL31 at `BL31_BASE`. During warm boot, BL31 is executed by all
1466 CPUs. BL31 executes at EL3 and is responsible for:
1467
1468 1. Re-initializing all architectural and platform state. Although BL1 performs
1469 some of this initialization, BL31 remains resident in EL3 and must ensure
1470 that EL3 architectural and platform state is completely initialized. It
1471 should make no assumptions about the system state when it receives control.
1472
1473 2. Passing control to a normal world BL image, pre-loaded at a platform-
1474 specific address by BL2. BL31 uses the `entry_point_info` structure that BL2
1475 populated in memory to do this.
1476
1477 3. Providing runtime firmware services. Currently, BL31 only implements a
1478 subset of the Power State Coordination Interface (PSCI) API as a runtime
1479 service. See Section 3.3 below for details of porting the PSCI
1480 implementation.
1481
1482 4. Optionally passing control to the BL32 image, pre-loaded at a platform-
1483 specific address by BL2. BL31 exports a set of apis that allow runtime
1484 services to specify the security state in which the next image should be
1485 executed and run the corresponding image. BL31 uses the `entry_point_info`
1486 structure populated by BL2 to do this.
1487
1488 If BL31 is a reset vector, It also needs to handle the reset as specified in
1489 section 2.2 before the tasks described above.
1490
1491 The following functions must be implemented by the platform port to enable BL31
1492 to perform the above tasks.
1493
1494
1495 ### Function : bl31_early_platform_setup() [mandatory]
1496
1497 Argument : bl31_params *, void *
1498 Return : void
1499
1500 This function executes with the MMU and data caches disabled. It is only called
1501 by the primary CPU. The arguments to this function are:
1502
1503 * The address of the `bl31_params` structure populated by BL2.
1504 * An opaque pointer that the platform may use as needed.
1505
1506 The platform can copy the contents of the `bl31_params` structure and its
1507 sub-structures into private variables if the original memory may be
1508 subsequently overwritten by BL31 and similarly the `void *` pointing
1509 to the platform data also needs to be saved.
1510
1511 In ARM standard platforms, BL2 passes a pointer to a `bl31_params` structure
1512 in BL2 memory. BL31 copies the information in this pointer to internal data
1513 structures. It also performs the following:
1514
1515 * Initialize a UART (PL011 console), which enables access to the `printf`
1516 family of functions in BL31.
1517
1518 * Enable issuing of snoop and DVM (Distributed Virtual Memory) requests to the
1519 CCI slave interface corresponding to the cluster that includes the primary
1520 CPU.
1521
1522
1523 ### Function : bl31_plat_arch_setup() [mandatory]
1524
1525 Argument : void
1526 Return : void
1527
1528 This function executes with the MMU and data caches disabled. It is only called
1529 by the primary CPU.
1530
1531 The purpose of this function is to perform any architectural initialization
1532 that varies across platforms.
1533
1534 On ARM standard platforms, this function enables the MMU.
1535
1536
1537 ### Function : bl31_platform_setup() [mandatory]
1538
1539 Argument : void
1540 Return : void
1541
1542 This function may execute with the MMU and data caches enabled if the platform
1543 port does the necessary initialization in `bl31_plat_arch_setup()`. It is only
1544 called by the primary CPU.
1545
1546 The purpose of this function is to complete platform initialization so that both
1547 BL31 runtime services and normal world software can function correctly.
1548
1549 On ARM standard platforms, this function does the following:
1550
1551 * Initialize the generic interrupt controller.
1552
1553 Depending on the GIC driver selected by the platform, the appropriate GICv2
1554 or GICv3 initialization will be done, which mainly consists of:
1555
1556 - Enable secure interrupts in the GIC CPU interface.
1557 - Disable the legacy interrupt bypass mechanism.
1558 - Configure the priority mask register to allow interrupts of all priorities
1559 to be signaled to the CPU interface.
1560 - Mark SGIs 8-15 and the other secure interrupts on the platform as secure.
1561 - Target all secure SPIs to CPU0.
1562 - Enable these secure interrupts in the GIC distributor.
1563 - Configure all other interrupts as non-secure.
1564 - Enable signaling of secure interrupts in the GIC distributor.
1565
1566 * Enable system-level implementation of the generic timer counter through the
1567 memory mapped interface.
1568
1569 * Grant access to the system counter timer module
1570
1571 * Initialize the power controller device.
1572
1573 In particular, initialise the locks that prevent concurrent accesses to the
1574 power controller device.
1575
1576
1577 ### Function : bl31_plat_runtime_setup() [optional]
1578
1579 Argument : void
1580 Return : void
1581
1582 The purpose of this function is allow the platform to perform any BL31 runtime
1583 setup just prior to BL31 exit during cold boot. The default weak
1584 implementation of this function will invoke `console_uninit()` which will
1585 suppress any BL31 runtime logs.
1586
1587 In ARM Standard platforms, this function will initialize the BL31 runtime
1588 console which will cause all further BL31 logs to be output to the
1589 runtime console.
1590
1591
1592 ### Function : bl31_get_next_image_info() [mandatory]
1593
1594 Argument : unsigned int
1595 Return : entry_point_info *
1596
1597 This function may execute with the MMU and data caches enabled if the platform
1598 port does the necessary initializations in `bl31_plat_arch_setup()`.
1599
1600 This function is called by `bl31_main()` to retrieve information provided by
1601 BL2 for the next image in the security state specified by the argument. BL31
1602 uses this information to pass control to that image in the specified security
1603 state. This function must return a pointer to the `entry_point_info` structure
1604 (that was copied during `bl31_early_platform_setup()`) if the image exists. It
1605 should return NULL otherwise.
1606
1607 ### Function : plat_get_syscnt_freq2() [mandatory]
1608
1609 Argument : void
1610 Return : unsigned int
1611
1612 This function is used by the architecture setup code to retrieve the counter
1613 frequency for the CPU's generic timer. This value will be programmed into the
1614 `CNTFRQ_EL0` register. In ARM standard platforms, it returns the base frequency
1615 of the system counter, which is retrieved from the first entry in the frequency
1616 modes table.
1617
1618
1619 ### #define : PLAT_PERCPU_BAKERY_LOCK_SIZE [optional]
1620
1621 When `USE_COHERENT_MEM = 0`, this constant defines the total memory (in
1622 bytes) aligned to the cache line boundary that should be allocated per-cpu to
1623 accommodate all the bakery locks.
1624
1625 If this constant is not defined when `USE_COHERENT_MEM = 0`, the linker
1626 calculates the size of the `bakery_lock` input section, aligns it to the
1627 nearest `CACHE_WRITEBACK_GRANULE`, multiplies it with `PLATFORM_CORE_COUNT`
1628 and stores the result in a linker symbol. This constant prevents a platform
1629 from relying on the linker and provide a more efficient mechanism for
1630 accessing per-cpu bakery lock information.
1631
1632 If this constant is defined and its value is not equal to the value
1633 calculated by the linker then a link time assertion is raised. A compile time
1634 assertion is raised if the value of the constant is not aligned to the cache
1635 line boundary.
1636
1637 3.5 Power State Coordination Interface (in BL31)
1638 ------------------------------------------------
1639
1640 The ARM Trusted Firmware's implementation of the PSCI API is based around the
1641 concept of a _power domain_. A _power domain_ is a CPU or a logical group of
1642 CPUs which share some state on which power management operations can be
1643 performed as specified by [PSCI]. Each CPU in the system is assigned a cpu
1644 index which is a unique number between `0` and `PLATFORM_CORE_COUNT - 1`.
1645 The _power domains_ are arranged in a hierarchical tree structure and
1646 each _power domain_ can be identified in a system by the cpu index of any CPU
1647 that is part of that domain and a _power domain level_. A processing element
1648 (for example, a CPU) is at level 0. If the _power domain_ node above a CPU is
1649 a logical grouping of CPUs that share some state, then level 1 is that group
1650 of CPUs (for example, a cluster), and level 2 is a group of clusters
1651 (for example, the system). More details on the power domain topology and its
1652 organization can be found in [Power Domain Topology Design].
1653
1654 BL31's platform initialization code exports a pointer to the platform-specific
1655 power management operations required for the PSCI implementation to function
1656 correctly. This information is populated in the `plat_psci_ops` structure. The
1657 PSCI implementation calls members of the `plat_psci_ops` structure for performing
1658 power management operations on the power domains. For example, the target
1659 CPU is specified by its `MPIDR` in a PSCI `CPU_ON` call. The `pwr_domain_on()`
1660 handler (if present) is called for the CPU power domain.
1661
1662 The `power-state` parameter of a PSCI `CPU_SUSPEND` call can be used to
1663 describe composite power states specific to a platform. The PSCI implementation
1664 defines a generic representation of the power-state parameter viz which is an
1665 array of local power states where each index corresponds to a power domain
1666 level. Each entry contains the local power state the power domain at that power
1667 level could enter. It depends on the `validate_power_state()` handler to
1668 convert the power-state parameter (possibly encoding a composite power state)
1669 passed in a PSCI `CPU_SUSPEND` call to this representation.
1670
1671 The following functions must be implemented to initialize PSCI functionality in
1672 the ARM Trusted Firmware.
1673
1674
1675 ### Function : plat_get_target_pwr_state() [optional]
1676
1677 Argument : unsigned int, const plat_local_state_t *, unsigned int
1678 Return : plat_local_state_t
1679
1680 The PSCI generic code uses this function to let the platform participate in
1681 state coordination during a power management operation. The function is passed
1682 a pointer to an array of platform specific local power state `states` (second
1683 argument) which contains the requested power state for each CPU at a particular
1684 power domain level `lvl` (first argument) within the power domain. The function
1685 is expected to traverse this array of upto `ncpus` (third argument) and return
1686 a coordinated target power state by the comparing all the requested power
1687 states. The target power state should not be deeper than any of the requested
1688 power states.
1689
1690 A weak definition of this API is provided by default wherein it assumes
1691 that the platform assigns a local state value in order of increasing depth
1692 of the power state i.e. for two power states X & Y, if X < Y
1693 then X represents a shallower power state than Y. As a result, the
1694 coordinated target local power state for a power domain will be the minimum
1695 of the requested local power state values.
1696
1697
1698 ### Function : plat_get_power_domain_tree_desc() [mandatory]
1699
1700 Argument : void
1701 Return : const unsigned char *
1702
1703 This function returns a pointer to the byte array containing the power domain
1704 topology tree description. The format and method to construct this array are
1705 described in [Power Domain Topology Design]. The BL31 PSCI initilization code
1706 requires this array to be described by the platform, either statically or
1707 dynamically, to initialize the power domain topology tree. In case the array
1708 is populated dynamically, then plat_core_pos_by_mpidr() and
1709 plat_my_core_pos() should also be implemented suitably so that the topology
1710 tree description matches the CPU indices returned by these APIs. These APIs
1711 together form the platform interface for the PSCI topology framework.
1712
1713
1714 ## Function : plat_setup_psci_ops() [mandatory]
1715
1716 Argument : uintptr_t, const plat_psci_ops **
1717 Return : int
1718
1719 This function may execute with the MMU and data caches enabled if the platform
1720 port does the necessary initializations in `bl31_plat_arch_setup()`. It is only
1721 called by the primary CPU.
1722
1723 This function is called by PSCI initialization code. Its purpose is to let
1724 the platform layer know about the warm boot entrypoint through the
1725 `sec_entrypoint` (first argument) and to export handler routines for
1726 platform-specific psci power management actions by populating the passed
1727 pointer with a pointer to BL31's private `plat_psci_ops` structure.
1728
1729 A description of each member of this structure is given below. Please refer to
1730 the ARM FVP specific implementation of these handlers in
1731 [plat/arm/board/fvp/fvp_pm.c] as an example. For each PSCI function that the
1732 platform wants to support, the associated operation or operations in this
1733 structure must be provided and implemented (Refer section 4 of
1734 [Firmware Design] for the PSCI API supported in Trusted Firmware). To disable
1735 a PSCI function in a platform port, the operation should be removed from this
1736 structure instead of providing an empty implementation.
1737
1738 #### plat_psci_ops.cpu_standby()
1739
1740 Perform the platform-specific actions to enter the standby state for a cpu
1741 indicated by the passed argument. This provides a fast path for CPU standby
1742 wherein overheads of PSCI state management and lock acquistion is avoided.
1743 For this handler to be invoked by the PSCI `CPU_SUSPEND` API implementation,
1744 the suspend state type specified in the `power-state` parameter should be
1745 STANDBY and the target power domain level specified should be the CPU. The
1746 handler should put the CPU into a low power retention state (usually by
1747 issuing a wfi instruction) and ensure that it can be woken up from that
1748 state by a normal interrupt. The generic code expects the handler to succeed.
1749
1750 #### plat_psci_ops.pwr_domain_on()
1751
1752 Perform the platform specific actions to power on a CPU, specified
1753 by the `MPIDR` (first argument). The generic code expects the platform to
1754 return PSCI_E_SUCCESS on success or PSCI_E_INTERN_FAIL for any failure.
1755
1756 #### plat_psci_ops.pwr_domain_off()
1757
1758 Perform the platform specific actions to prepare to power off the calling CPU
1759 and its higher parent power domain levels as indicated by the `target_state`
1760 (first argument). It is called by the PSCI `CPU_OFF` API implementation.
1761
1762 The `target_state` encodes the platform coordinated target local power states
1763 for the CPU power domain and its parent power domain levels. The handler
1764 needs to perform power management operation corresponding to the local state
1765 at each power level.
1766
1767 For this handler, the local power state for the CPU power domain will be a
1768 power down state where as it could be either power down, retention or run state
1769 for the higher power domain levels depending on the result of state
1770 coordination. The generic code expects the handler to succeed.
1771
1772 #### plat_psci_ops.pwr_domain_suspend()
1773
1774 Perform the platform specific actions to prepare to suspend the calling
1775 CPU and its higher parent power domain levels as indicated by the
1776 `target_state` (first argument). It is called by the PSCI `CPU_SUSPEND`
1777 API implementation.
1778
1779 The `target_state` has a similar meaning as described in
1780 the `pwr_domain_off()` operation. It encodes the platform coordinated
1781 target local power states for the CPU power domain and its parent
1782 power domain levels. The handler needs to perform power management operation
1783 corresponding to the local state at each power level. The generic code
1784 expects the handler to succeed.
1785
1786 The difference between turning a power domain off versus suspending it
1787 is that in the former case, the power domain is expected to re-initialize
1788 its state when it is next powered on (see `pwr_domain_on_finish()`). In the
1789 latter case, the power domain is expected to save enough state so that it can
1790 resume execution by restoring this state when its powered on (see
1791 `pwr_domain_suspend_finish()`).
1792
1793 #### plat_psci_ops.pwr_domain_pwr_down_wfi()
1794
1795 This is an optional function and, if implemented, is expected to perform
1796 platform specific actions including the `wfi` invocation which allows the
1797 CPU to powerdown. Since this function is invoked outside the PSCI locks,
1798 the actions performed in this hook must be local to the CPU or the platform
1799 must ensure that races between multiple CPUs cannot occur.
1800
1801 The `target_state` has a similar meaning as described in the `pwr_domain_off()`
1802 operation and it encodes the platform coordinated target local power states for
1803 the CPU power domain and its parent power domain levels. This function must
1804 not return back to the caller.
1805
1806 If this function is not implemented by the platform, PSCI generic
1807 implementation invokes `psci_power_down_wfi()` for power down.
1808
1809 #### plat_psci_ops.pwr_domain_on_finish()
1810
1811 This function is called by the PSCI implementation after the calling CPU is
1812 powered on and released from reset in response to an earlier PSCI `CPU_ON` call.
1813 It performs the platform-specific setup required to initialize enough state for
1814 this CPU to enter the normal world and also provide secure runtime firmware
1815 services.
1816
1817 The `target_state` (first argument) is the prior state of the power domains
1818 immediately before the CPU was turned on. It indicates which power domains
1819 above the CPU might require initialization due to having previously been in
1820 low power states. The generic code expects the handler to succeed.
1821
1822 #### plat_psci_ops.pwr_domain_suspend_finish()
1823
1824 This function is called by the PSCI implementation after the calling CPU is
1825 powered on and released from reset in response to an asynchronous wakeup
1826 event, for example a timer interrupt that was programmed by the CPU during the
1827 `CPU_SUSPEND` call or `SYSTEM_SUSPEND` call. It performs the platform-specific
1828 setup required to restore the saved state for this CPU to resume execution
1829 in the normal world and also provide secure runtime firmware services.
1830
1831 The `target_state` (first argument) has a similar meaning as described in
1832 the `pwr_domain_on_finish()` operation. The generic code expects the platform
1833 to succeed.
1834
1835 #### plat_psci_ops.validate_power_state()
1836
1837 This function is called by the PSCI implementation during the `CPU_SUSPEND`
1838 call to validate the `power_state` parameter of the PSCI API and if valid,
1839 populate it in `req_state` (second argument) array as power domain level
1840 specific local states. If the `power_state` is invalid, the platform must
1841 return PSCI_E_INVALID_PARAMS as error, which is propagated back to the
1842 normal world PSCI client.
1843
1844 #### plat_psci_ops.validate_ns_entrypoint()
1845
1846 This function is called by the PSCI implementation during the `CPU_SUSPEND`,
1847 `SYSTEM_SUSPEND` and `CPU_ON` calls to validate the non-secure `entry_point`
1848 parameter passed by the normal world. If the `entry_point` is invalid,
1849 the platform must return PSCI_E_INVALID_ADDRESS as error, which is
1850 propagated back to the normal world PSCI client.
1851
1852 #### plat_psci_ops.get_sys_suspend_power_state()
1853
1854 This function is called by the PSCI implementation during the `SYSTEM_SUSPEND`
1855 call to get the `req_state` parameter from platform which encodes the power
1856 domain level specific local states to suspend to system affinity level. The
1857 `req_state` will be utilized to do the PSCI state coordination and
1858 `pwr_domain_suspend()` will be invoked with the coordinated target state to
1859 enter system suspend.
1860
1861 #### plat_psci_ops.get_pwr_lvl_state_idx()
1862
1863 This is an optional function and, if implemented, is invoked by the PSCI
1864 implementation to convert the `local_state` (first argument) at a specified
1865 `pwr_lvl` (second argument) to an index between 0 and
1866 `PLAT_MAX_PWR_LVL_STATES` - 1. This function is only needed if the platform
1867 supports more than two local power states at each power domain level, that is
1868 `PLAT_MAX_PWR_LVL_STATES` is greater than 2, and needs to account for these
1869 local power states.
1870
1871 #### plat_psci_ops.translate_power_state_by_mpidr()
1872
1873 This is an optional function and, if implemented, verifies the `power_state`
1874 (second argument) parameter of the PSCI API corresponding to a target power
1875 domain. The target power domain is identified by using both `MPIDR` (first
1876 argument) and the power domain level encoded in `power_state`. The power domain
1877 level specific local states are to be extracted from `power_state` and be
1878 populated in the `output_state` (third argument) array. The functionality
1879 is similar to the `validate_power_state` function described above and is
1880 envisaged to be used in case the validity of `power_state` depend on the
1881 targeted power domain. If the `power_state` is invalid for the targeted power
1882 domain, the platform must return PSCI_E_INVALID_PARAMS as error. If this
1883 function is not implemented, then the generic implementation relies on
1884 `validate_power_state` function to translate the `power_state`.
1885
1886 This function can also be used in case the platform wants to support local
1887 power state encoding for `power_state` parameter of PSCI_STAT_COUNT/RESIDENCY
1888 APIs as described in Section 5.18 of [PSCI].
1889
1890 #### plat_psci_ops.get_node_hw_state()
1891
1892 This is an optional function. If implemented this function is intended to return
1893 the power state of a node (identified by the first parameter, the `MPIDR`) in
1894 the power domain topology (identified by the second parameter, `power_level`),
1895 as retrieved from a power controller or equivalent component on the platform.
1896 Upon successful completion, the implementation must map and return the final
1897 status among `HW_ON`, `HW_OFF` or `HW_STANDBY`. Upon encountering failures, it
1898 must return either `PSCI_E_INVALID_PARAMS` or `PSCI_E_NOT_SUPPORTED` as
1899 appropriate.
1900
1901 Implementations are not expected to handle `power_levels` greater than
1902 `PLAT_MAX_PWR_LVL`.
1903
1904 3.6 Interrupt Management framework (in BL31)
1905 ----------------------------------------------
1906 BL31 implements an Interrupt Management Framework (IMF) to manage interrupts
1907 generated in either security state and targeted to EL1 or EL2 in the non-secure
1908 state or EL3/S-EL1 in the secure state. The design of this framework is
1909 described in the [IMF Design Guide]
1910
1911 A platform should export the following APIs to support the IMF. The following
1912 text briefly describes each api and its implementation in ARM standard
1913 platforms. The API implementation depends upon the type of interrupt controller
1914 present in the platform. ARM standard platform layer supports both [ARM Generic
1915 Interrupt Controller version 2.0 (GICv2)][ARM GIC Architecture Specification 2.0]
1916 and [3.0 (GICv3)][ARM GIC Architecture Specification 3.0]. Juno builds the ARM
1917 Standard layer to use GICv2 and the FVP can be configured to use either GICv2 or
1918 GICv3 depending on the build flag `FVP_USE_GIC_DRIVER` (See FVP platform
1919 specific build options in [User Guide] for more details).
1920
1921 ### Function : plat_interrupt_type_to_line() [mandatory]
1922
1923 Argument : uint32_t, uint32_t
1924 Return : uint32_t
1925
1926 The ARM processor signals an interrupt exception either through the IRQ or FIQ
1927 interrupt line. The specific line that is signaled depends on how the interrupt
1928 controller (IC) reports different interrupt types from an execution context in
1929 either security state. The IMF uses this API to determine which interrupt line
1930 the platform IC uses to signal each type of interrupt supported by the framework
1931 from a given security state. This API must be invoked at EL3.
1932
1933 The first parameter will be one of the `INTR_TYPE_*` values (see [IMF Design
1934 Guide]) indicating the target type of the interrupt, the second parameter is the
1935 security state of the originating execution context. The return result is the
1936 bit position in the `SCR_EL3` register of the respective interrupt trap: IRQ=1,
1937 FIQ=2.
1938
1939 In the case of ARM standard platforms using GICv2, S-EL1 interrupts are
1940 configured as FIQs and Non-secure interrupts as IRQs from either security
1941 state.
1942
1943 In the case of ARM standard platforms using GICv3, the interrupt line to be
1944 configured depends on the security state of the execution context when the
1945 interrupt is signalled and are as follows:
1946 * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 context and as FIQ in
1947 NS-EL0/1/2 context.
1948 * The Non secure interrupts are signaled as FIQ in S-EL0/1 context and as IRQ
1949 in the NS-EL0/1/2 context.
1950 * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and NS-EL0/1/2
1951 context.
1952
1953
1954 ### Function : plat_ic_get_pending_interrupt_type() [mandatory]
1955
1956 Argument : void
1957 Return : uint32_t
1958
1959 This API returns the type of the highest priority pending interrupt at the
1960 platform IC. The IMF uses the interrupt type to retrieve the corresponding
1961 handler function. `INTR_TYPE_INVAL` is returned when there is no interrupt
1962 pending. The valid interrupt types that can be returned are `INTR_TYPE_EL3`,
1963 `INTR_TYPE_S_EL1` and `INTR_TYPE_NS`. This API must be invoked at EL3.
1964
1965 In the case of ARM standard platforms using GICv2, the _Highest Priority
1966 Pending Interrupt Register_ (`GICC_HPPIR`) is read to determine the id of
1967 the pending interrupt. The type of interrupt depends upon the id value as
1968 follows.
1969
1970 1. id < 1022 is reported as a S-EL1 interrupt
1971 2. id = 1022 is reported as a Non-secure interrupt.
1972 3. id = 1023 is reported as an invalid interrupt type.
1973
1974 In the case of ARM standard platforms using GICv3, the system register
1975 `ICC_HPPIR0_EL1`, _Highest Priority Pending group 0 Interrupt Register_,
1976 is read to determine the id of the pending interrupt. The type of interrupt
1977 depends upon the id value as follows.
1978
1979 1. id = `PENDING_G1S_INTID` (1020) is reported as a S-EL1 interrupt
1980 2. id = `PENDING_G1NS_INTID` (1021) is reported as a Non-secure interrupt.
1981 3. id = `GIC_SPURIOUS_INTERRUPT` (1023) is reported as an invalid interrupt type.
1982 4. All other interrupt id's are reported as EL3 interrupt.
1983
1984
1985 ### Function : plat_ic_get_pending_interrupt_id() [mandatory]
1986
1987 Argument : void
1988 Return : uint32_t
1989
1990 This API returns the id of the highest priority pending interrupt at the
1991 platform IC. `INTR_ID_UNAVAILABLE` is returned when there is no interrupt
1992 pending.
1993
1994 In the case of ARM standard platforms using GICv2, the _Highest Priority
1995 Pending Interrupt Register_ (`GICC_HPPIR`) is read to determine the id of the
1996 pending interrupt. The id that is returned by API depends upon the value of
1997 the id read from the interrupt controller as follows.
1998
1999 1. id < 1022. id is returned as is.
2000 2. id = 1022. The _Aliased Highest Priority Pending Interrupt Register_
2001 (`GICC_AHPPIR`) is read to determine the id of the non-secure interrupt.
2002 This id is returned by the API.
2003 3. id = 1023. `INTR_ID_UNAVAILABLE` is returned.
2004
2005 In the case of ARM standard platforms using GICv3, if the API is invoked from
2006 EL3, the system register `ICC_HPPIR0_EL1`, _Highest Priority Pending Interrupt
2007 group 0 Register_, is read to determine the id of the pending interrupt. The id
2008 that is returned by API depends upon the value of the id read from the
2009 interrupt controller as follows.
2010
2011 1. id < `PENDING_G1S_INTID` (1020). id is returned as is.
2012 2. id = `PENDING_G1S_INTID` (1020) or `PENDING_G1NS_INTID` (1021). The system
2013 register `ICC_HPPIR1_EL1`, _Highest Priority Pending Interrupt group 1
2014 Register_ is read to determine the id of the group 1 interrupt. This id
2015 is returned by the API as long as it is a valid interrupt id
2016 3. If the id is any of the special interrupt identifiers,
2017 `INTR_ID_UNAVAILABLE` is returned.
2018
2019 When the API invoked from S-EL1 for GICv3 systems, the id read from system
2020 register `ICC_HPPIR1_EL1`, _Highest Priority Pending group 1 Interrupt
2021 Register_, is returned if is not equal to GIC_SPURIOUS_INTERRUPT (1023) else
2022 `INTR_ID_UNAVAILABLE` is returned.
2023
2024 ### Function : plat_ic_acknowledge_interrupt() [mandatory]
2025
2026 Argument : void
2027 Return : uint32_t
2028
2029 This API is used by the CPU to indicate to the platform IC that processing of
2030 the highest pending interrupt has begun. It should return the id of the
2031 interrupt which is being processed.
2032
2033 This function in ARM standard platforms using GICv2, reads the _Interrupt
2034 Acknowledge Register_ (`GICC_IAR`). This changes the state of the highest
2035 priority pending interrupt from pending to active in the interrupt controller.
2036 It returns the value read from the `GICC_IAR`. This value is the id of the
2037 interrupt whose state has been changed.
2038
2039 In the case of ARM standard platforms using GICv3, if the API is invoked
2040 from EL3, the function reads the system register `ICC_IAR0_EL1`, _Interrupt
2041 Acknowledge Register group 0_. If the API is invoked from S-EL1, the function
2042 reads the system register `ICC_IAR1_EL1`, _Interrupt Acknowledge Register
2043 group 1_. The read changes the state of the highest pending interrupt from
2044 pending to active in the interrupt controller. The value read is returned
2045 and is the id of the interrupt whose state has been changed.
2046
2047 The TSP uses this API to start processing of the secure physical timer
2048 interrupt.
2049
2050
2051 ### Function : plat_ic_end_of_interrupt() [mandatory]
2052
2053 Argument : uint32_t
2054 Return : void
2055
2056 This API is used by the CPU to indicate to the platform IC that processing of
2057 the interrupt corresponding to the id (passed as the parameter) has
2058 finished. The id should be the same as the id returned by the
2059 `plat_ic_acknowledge_interrupt()` API.
2060
2061 ARM standard platforms write the id to the _End of Interrupt Register_
2062 (`GICC_EOIR`) in case of GICv2, and to `ICC_EOIR0_EL1` or `ICC_EOIR1_EL1`
2063 system register in case of GICv3 depending on where the API is invoked from,
2064 EL3 or S-EL1. This deactivates the corresponding interrupt in the interrupt
2065 controller.
2066
2067 The TSP uses this API to finish processing of the secure physical timer
2068 interrupt.
2069
2070
2071 ### Function : plat_ic_get_interrupt_type() [mandatory]
2072
2073 Argument : uint32_t
2074 Return : uint32_t
2075
2076 This API returns the type of the interrupt id passed as the parameter.
2077 `INTR_TYPE_INVAL` is returned if the id is invalid. If the id is valid, a valid
2078 interrupt type (one of `INTR_TYPE_EL3`, `INTR_TYPE_S_EL1` and `INTR_TYPE_NS`) is
2079 returned depending upon how the interrupt has been configured by the platform
2080 IC. This API must be invoked at EL3.
2081
2082 ARM standard platforms using GICv2 configures S-EL1 interrupts as Group0 interrupts
2083 and Non-secure interrupts as Group1 interrupts. It reads the group value
2084 corresponding to the interrupt id from the relevant _Interrupt Group Register_
2085 (`GICD_IGROUPRn`). It uses the group value to determine the type of interrupt.
2086
2087 In the case of ARM standard platforms using GICv3, both the _Interrupt Group
2088 Register_ (`GICD_IGROUPRn`) and _Interrupt Group Modifier Register_
2089 (`GICD_IGRPMODRn`) is read to figure out whether the interrupt is configured
2090 as Group 0 secure interrupt, Group 1 secure interrupt or Group 1 NS interrupt.
2091
2092
2093 3.7 Crash Reporting mechanism (in BL31)
2094 ----------------------------------------------
2095 BL31 implements a crash reporting mechanism which prints the various registers
2096 of the CPU to enable quick crash analysis and debugging. It requires that a
2097 console is designated as the crash console by the platform which will be used to
2098 print the register dump.
2099
2100 The following functions must be implemented by the platform if it wants crash
2101 reporting mechanism in BL31. The functions are implemented in assembly so that
2102 they can be invoked without a C Runtime stack.
2103
2104 ### Function : plat_crash_console_init
2105
2106 Argument : void
2107 Return : int
2108
2109 This API is used by the crash reporting mechanism to initialize the crash
2110 console. It must only use the general purpose registers x0 to x4 to do the
2111 initialization and returns 1 on success.
2112
2113 ### Function : plat_crash_console_putc
2114
2115 Argument : int
2116 Return : int
2117
2118 This API is used by the crash reporting mechanism to print a character on the
2119 designated crash console. It must only use general purpose registers x1 and
2120 x2 to do its work. The parameter and the return value are in general purpose
2121 register x0.
2122
2123 4. Build flags
2124 ---------------
2125
2126 * **ENABLE_PLAT_COMPAT**
2127 All the platforms ports conforming to this API specification should define
2128 the build flag `ENABLE_PLAT_COMPAT` to 0 as the compatibility layer should
2129 be disabled. For more details on compatibility layer, refer
2130 [Migration Guide].
2131
2132 There are some build flags which can be defined by the platform to control
2133 inclusion or exclusion of certain BL stages from the FIP image. These flags
2134 need to be defined in the platform makefile which will get included by the
2135 build system.
2136
2137 * **NEED_BL33**
2138 By default, this flag is defined `yes` by the build system and `BL33`
2139 build option should be supplied as a build option. The platform has the
2140 option of excluding the BL33 image in the `fip` image by defining this flag
2141 to `no`. If any of the options `EL3_PAYLOAD_BASE` or `PRELOADED_BL33_BASE`
2142 are used, this flag will be set to `no` automatically.
2143
2144 5. C Library
2145 -------------
2146
2147 To avoid subtle toolchain behavioral dependencies, the header files provided
2148 by the compiler are not used. The software is built with the `-nostdinc` flag
2149 to ensure no headers are included from the toolchain inadvertently. Instead the
2150 required headers are included in the ARM Trusted Firmware source tree. The
2151 library only contains those C library definitions required by the local
2152 implementation. If more functionality is required, the needed library functions
2153 will need to be added to the local implementation.
2154
2155 Versions of [FreeBSD] headers can be found in `include/lib/stdlib`. Some of
2156 these headers have been cut down in order to simplify the implementation. In
2157 order to minimize changes to the header files, the [FreeBSD] layout has been
2158 maintained. The generic C library definitions can be found in
2159 `include/lib/stdlib` with more system and machine specific declarations in
2160 `include/lib/stdlib/sys` and `include/lib/stdlib/machine`.
2161
2162 The local C library implementations can be found in `lib/stdlib`. In order to
2163 extend the C library these files may need to be modified. It is recommended to
2164 use a release version of [FreeBSD] as a starting point.
2165
2166 The C library header files in the [FreeBSD] source tree are located in the
2167 `include` and `sys/sys` directories. [FreeBSD] machine specific definitions
2168 can be found in the `sys/<machine-type>` directories. These files define things
2169 like 'the size of a pointer' and 'the range of an integer'. Since an AArch64
2170 port for [FreeBSD] does not yet exist, the machine specific definitions are
2171 based on existing machine types with similar properties (for example SPARC64).
2172
2173 Where possible, C library function implementations were taken from [FreeBSD]
2174 as found in the `lib/libc` directory.
2175
2176 A copy of the [FreeBSD] sources can be downloaded with `git`.
2177
2178 git clone git://github.com/freebsd/freebsd.git -b origin/release/9.2.0
2179
2180
2181 6. Storage abstraction layer
2182 -----------------------------
2183
2184 In order to improve platform independence and portability an storage abstraction
2185 layer is used to load data from non-volatile platform storage.
2186
2187 Each platform should register devices and their drivers via the Storage layer.
2188 These drivers then need to be initialized by bootloader phases as
2189 required in their respective `blx_platform_setup()` functions. Currently
2190 storage access is only required by BL1 and BL2 phases. The `load_image()`
2191 function uses the storage layer to access non-volatile platform storage.
2192
2193 It is mandatory to implement at least one storage driver. For the ARM
2194 development platforms the Firmware Image Package (FIP) driver is provided as
2195 the default means to load data from storage (see the "Firmware Image Package"
2196 section in the [User Guide]). The storage layer is described in the header file
2197 `include/drivers/io/io_storage.h`. The implementation of the common library
2198 is in `drivers/io/io_storage.c` and the driver files are located in
2199 `drivers/io/`.
2200
2201 Each IO driver must provide `io_dev_*` structures, as described in
2202 `drivers/io/io_driver.h`. These are returned via a mandatory registration
2203 function that is called on platform initialization. The semi-hosting driver
2204 implementation in `io_semihosting.c` can be used as an example.
2205
2206 The Storage layer provides mechanisms to initialize storage devices before
2207 IO operations are called. The basic operations supported by the layer
2208 include `open()`, `close()`, `read()`, `write()`, `size()` and `seek()`.
2209 Drivers do not have to implement all operations, but each platform must
2210 provide at least one driver for a device capable of supporting generic
2211 operations such as loading a bootloader image.
2212
2213 The current implementation only allows for known images to be loaded by the
2214 firmware. These images are specified by using their identifiers, as defined in
2215 [include/plat/common/platform_def.h] (or a separate header file included from
2216 there). The platform layer (`plat_get_image_source()`) then returns a reference
2217 to a device and a driver-specific `spec` which will be understood by the driver
2218 to allow access to the image data.
2219
2220 The layer is designed in such a way that is it possible to chain drivers with
2221 other drivers. For example, file-system drivers may be implemented on top of
2222 physical block devices, both represented by IO devices with corresponding
2223 drivers. In such a case, the file-system "binding" with the block device may
2224 be deferred until the file-system device is initialised.
2225
2226 The abstraction currently depends on structures being statically allocated
2227 by the drivers and callers, as the system does not yet provide a means of
2228 dynamically allocating memory. This may also have the affect of limiting the
2229 amount of open resources per driver.
2230
2231
2232 - - - - - - - - - - - - - - - - - - - - - - - - - -
2233
2234 _Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved._
2235
2236
2237 [ARM GIC Architecture Specification 2.0]: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0048b/index.html
2238 [ARM GIC Architecture Specification 3.0]: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0069b/index.html
2239 [IMF Design Guide]: interrupt-framework-design.md
2240 [User Guide]: user-guide.md
2241 [FreeBSD]: http://www.freebsd.org
2242 [Firmware Design]: firmware-design.md
2243 [Power Domain Topology Design]: psci-pd-tree.md
2244 [PSCI]: http://infocenter.arm.com/help/topic/com.arm.doc.den0022c/DEN0022C_Power_State_Coordination_Interface.pdf
2245 [Migration Guide]: platform-migration-guide.md
2246 [Firmware Update]: firmware-update.md
2247
2248 [plat/common/aarch64/platform_mp_stack.S]: ../plat/common/aarch64/platform_mp_stack.S
2249 [plat/common/aarch64/platform_up_stack.S]: ../plat/common/aarch64/platform_up_stack.S
2250 [plat/arm/board/fvp/fvp_pm.c]: ../plat/arm/board/fvp/fvp_pm.c
2251 [include/common/bl_common.h]: ../include/common/bl_common.h
2252 [include/lib/aarch32/arch.h]: ../include/lib/aarch32/arch.h
2253 [include/plat/arm/common/arm_def.h]: ../include/plat/arm/common/arm_def.h
2254 [include/plat/common/common_def.h]: ../include/plat/common/common_def.h
2255 [include/plat/common/platform.h]: ../include/plat/common/platform.h
2256 [include/plat/arm/common/plat_arm.h]: ../include/plat/arm/common/plat_arm.h]