xref: /aosp_15_r20/external/arm-trusted-firmware/docs/design/firmware-design.rst (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong ParkFirmware Design
2*54fd6939SJiyong Park===============
3*54fd6939SJiyong Park
4*54fd6939SJiyong ParkTrusted Firmware-A (TF-A) implements a subset of the Trusted Board Boot
5*54fd6939SJiyong ParkRequirements (TBBR) Platform Design Document (PDD) for Arm reference
6*54fd6939SJiyong Parkplatforms.
7*54fd6939SJiyong Park
8*54fd6939SJiyong ParkThe TBB sequence starts when the platform is powered on and runs up
9*54fd6939SJiyong Parkto the stage where it hands-off control to firmware running in the normal
10*54fd6939SJiyong Parkworld in DRAM. This is the cold boot path.
11*54fd6939SJiyong Park
12*54fd6939SJiyong ParkTF-A also implements the `Power State Coordination Interface PDD`_ as a
13*54fd6939SJiyong Parkruntime service. PSCI is the interface from normal world software to firmware
14*54fd6939SJiyong Parkimplementing power management use-cases (for example, secondary CPU boot,
15*54fd6939SJiyong Parkhotplug and idle). Normal world software can access TF-A runtime services via
16*54fd6939SJiyong Parkthe Arm SMC (Secure Monitor Call) instruction. The SMC instruction must be
17*54fd6939SJiyong Parkused as mandated by the SMC Calling Convention (`SMCCC`_).
18*54fd6939SJiyong Park
19*54fd6939SJiyong ParkTF-A implements a framework for configuring and managing interrupts generated
20*54fd6939SJiyong Parkin either security state. The details of the interrupt management framework
21*54fd6939SJiyong Parkand its design can be found in :ref:`Interrupt Management Framework`.
22*54fd6939SJiyong Park
23*54fd6939SJiyong ParkTF-A also implements a library for setting up and managing the translation
24*54fd6939SJiyong Parktables. The details of this library can be found in
25*54fd6939SJiyong Park:ref:`Translation (XLAT) Tables Library`.
26*54fd6939SJiyong Park
27*54fd6939SJiyong ParkTF-A can be built to support either AArch64 or AArch32 execution state.
28*54fd6939SJiyong Park
29*54fd6939SJiyong Park.. note::
30*54fd6939SJiyong Park
31*54fd6939SJiyong Park The descriptions in this chapter are for the Arm TrustZone architecture.
32*54fd6939SJiyong Park For changes to the firmware design for the
33*54fd6939SJiyong Park `Arm Confidential Compute Architecture (Arm CCA)`_ please refer to the
34*54fd6939SJiyong Park chapter :ref:`Realm Management Extension (RME)`.
35*54fd6939SJiyong Park
36*54fd6939SJiyong ParkCold boot
37*54fd6939SJiyong Park---------
38*54fd6939SJiyong Park
39*54fd6939SJiyong ParkThe cold boot path starts when the platform is physically turned on. If
40*54fd6939SJiyong Park``COLD_BOOT_SINGLE_CPU=0``, one of the CPUs released from reset is chosen as the
41*54fd6939SJiyong Parkprimary CPU, and the remaining CPUs are considered secondary CPUs. The primary
42*54fd6939SJiyong ParkCPU is chosen through platform-specific means. The cold boot path is mainly
43*54fd6939SJiyong Parkexecuted by the primary CPU, other than essential CPU initialization executed by
44*54fd6939SJiyong Parkall CPUs. The secondary CPUs are kept in a safe platform-specific state until
45*54fd6939SJiyong Parkthe primary CPU has performed enough initialization to boot them.
46*54fd6939SJiyong Park
47*54fd6939SJiyong ParkRefer to the :ref:`CPU Reset` for more information on the effect of the
48*54fd6939SJiyong Park``COLD_BOOT_SINGLE_CPU`` platform build option.
49*54fd6939SJiyong Park
50*54fd6939SJiyong ParkThe cold boot path in this implementation of TF-A depends on the execution
51*54fd6939SJiyong Parkstate. For AArch64, it is divided into five steps (in order of execution):
52*54fd6939SJiyong Park
53*54fd6939SJiyong Park-  Boot Loader stage 1 (BL1) *AP Trusted ROM*
54*54fd6939SJiyong Park-  Boot Loader stage 2 (BL2) *Trusted Boot Firmware*
55*54fd6939SJiyong Park-  Boot Loader stage 3-1 (BL31) *EL3 Runtime Software*
56*54fd6939SJiyong Park-  Boot Loader stage 3-2 (BL32) *Secure-EL1 Payload* (optional)
57*54fd6939SJiyong Park-  Boot Loader stage 3-3 (BL33) *Non-trusted Firmware*
58*54fd6939SJiyong Park
59*54fd6939SJiyong ParkFor AArch32, it is divided into four steps (in order of execution):
60*54fd6939SJiyong Park
61*54fd6939SJiyong Park-  Boot Loader stage 1 (BL1) *AP Trusted ROM*
62*54fd6939SJiyong Park-  Boot Loader stage 2 (BL2) *Trusted Boot Firmware*
63*54fd6939SJiyong Park-  Boot Loader stage 3-2 (BL32) *EL3 Runtime Software*
64*54fd6939SJiyong Park-  Boot Loader stage 3-3 (BL33) *Non-trusted Firmware*
65*54fd6939SJiyong Park
66*54fd6939SJiyong ParkArm development platforms (Fixed Virtual Platforms (FVPs) and Juno) implement a
67*54fd6939SJiyong Parkcombination of the following types of memory regions. Each bootloader stage uses
68*54fd6939SJiyong Parkone or more of these memory regions.
69*54fd6939SJiyong Park
70*54fd6939SJiyong Park-  Regions accessible from both non-secure and secure states. For example,
71*54fd6939SJiyong Park   non-trusted SRAM, ROM and DRAM.
72*54fd6939SJiyong Park-  Regions accessible from only the secure state. For example, trusted SRAM and
73*54fd6939SJiyong Park   ROM. The FVPs also implement the trusted DRAM which is statically
74*54fd6939SJiyong Park   configured. Additionally, the Base FVPs and Juno development platform
75*54fd6939SJiyong Park   configure the TrustZone Controller (TZC) to create a region in the DRAM
76*54fd6939SJiyong Park   which is accessible only from the secure state.
77*54fd6939SJiyong Park
78*54fd6939SJiyong ParkThe sections below provide the following details:
79*54fd6939SJiyong Park
80*54fd6939SJiyong Park-  dynamic configuration of Boot Loader stages
81*54fd6939SJiyong Park-  initialization and execution of the first three stages during cold boot
82*54fd6939SJiyong Park-  specification of the EL3 Runtime Software (BL31 for AArch64 and BL32 for
83*54fd6939SJiyong Park   AArch32) entrypoint requirements for use by alternative Trusted Boot
84*54fd6939SJiyong Park   Firmware in place of the provided BL1 and BL2
85*54fd6939SJiyong Park
86*54fd6939SJiyong ParkDynamic Configuration during cold boot
87*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88*54fd6939SJiyong Park
89*54fd6939SJiyong ParkEach of the Boot Loader stages may be dynamically configured if required by the
90*54fd6939SJiyong Parkplatform. The Boot Loader stage may optionally specify a firmware
91*54fd6939SJiyong Parkconfiguration file and/or hardware configuration file as listed below:
92*54fd6939SJiyong Park
93*54fd6939SJiyong Park-  FW_CONFIG - The firmware configuration file. Holds properties shared across
94*54fd6939SJiyong Park   all BLx images.
95*54fd6939SJiyong Park   An example is the "dtb-registry" node, which contains the information about
96*54fd6939SJiyong Park   the other device tree configurations (load-address, size, image_id).
97*54fd6939SJiyong Park-  HW_CONFIG - The hardware configuration file. Can be shared by all Boot Loader
98*54fd6939SJiyong Park   stages and also by the Normal World Rich OS.
99*54fd6939SJiyong Park-  TB_FW_CONFIG - Trusted Boot Firmware configuration file. Shared between BL1
100*54fd6939SJiyong Park   and BL2.
101*54fd6939SJiyong Park-  SOC_FW_CONFIG - SoC Firmware configuration file. Used by BL31.
102*54fd6939SJiyong Park-  TOS_FW_CONFIG - Trusted OS Firmware configuration file. Used by Trusted OS
103*54fd6939SJiyong Park   (BL32).
104*54fd6939SJiyong Park-  NT_FW_CONFIG - Non Trusted Firmware configuration file. Used by Non-trusted
105*54fd6939SJiyong Park   firmware (BL33).
106*54fd6939SJiyong Park
107*54fd6939SJiyong ParkThe Arm development platforms use the Flattened Device Tree format for the
108*54fd6939SJiyong Parkdynamic configuration files.
109*54fd6939SJiyong Park
110*54fd6939SJiyong ParkEach Boot Loader stage can pass up to 4 arguments via registers to the next
111*54fd6939SJiyong Parkstage.  BL2 passes the list of the next images to execute to the *EL3 Runtime
112*54fd6939SJiyong ParkSoftware* (BL31 for AArch64 and BL32 for AArch32) via `arg0`. All the other
113*54fd6939SJiyong Parkarguments are platform defined. The Arm development platforms use the following
114*54fd6939SJiyong Parkconvention:
115*54fd6939SJiyong Park
116*54fd6939SJiyong Park-  BL1 passes the address of a meminfo_t structure to BL2 via ``arg1``. This
117*54fd6939SJiyong Park   structure contains the memory layout available to BL2.
118*54fd6939SJiyong Park-  When dynamic configuration files are present, the firmware configuration for
119*54fd6939SJiyong Park   the next Boot Loader stage is populated in the first available argument and
120*54fd6939SJiyong Park   the generic hardware configuration is passed the next available argument.
121*54fd6939SJiyong Park   For example,
122*54fd6939SJiyong Park
123*54fd6939SJiyong Park   -  FW_CONFIG is loaded by BL1, then its address is passed in ``arg0`` to BL2.
124*54fd6939SJiyong Park   -  TB_FW_CONFIG address is retrieved by BL2 from FW_CONFIG device tree.
125*54fd6939SJiyong Park   -  If HW_CONFIG is loaded by BL1, then its address is passed in ``arg2`` to
126*54fd6939SJiyong Park      BL2. Note, ``arg1`` is already used for meminfo_t.
127*54fd6939SJiyong Park   -  If SOC_FW_CONFIG is loaded by BL2, then its address is passed in ``arg1``
128*54fd6939SJiyong Park      to BL31. Note, ``arg0`` is used to pass the list of executable images.
129*54fd6939SJiyong Park   -  Similarly, if HW_CONFIG is loaded by BL1 or BL2, then its address is
130*54fd6939SJiyong Park      passed in ``arg2`` to BL31.
131*54fd6939SJiyong Park   -  For other BL3x images, if the firmware configuration file is loaded by
132*54fd6939SJiyong Park      BL2, then its address is passed in ``arg0`` and if HW_CONFIG is loaded
133*54fd6939SJiyong Park      then its address is passed in ``arg1``.
134*54fd6939SJiyong Park
135*54fd6939SJiyong ParkBL1
136*54fd6939SJiyong Park~~~
137*54fd6939SJiyong Park
138*54fd6939SJiyong ParkThis stage begins execution from the platform's reset vector at EL3. The reset
139*54fd6939SJiyong Parkaddress is platform dependent but it is usually located in a Trusted ROM area.
140*54fd6939SJiyong ParkThe BL1 data section is copied to trusted SRAM at runtime.
141*54fd6939SJiyong Park
142*54fd6939SJiyong ParkOn the Arm development platforms, BL1 code starts execution from the reset
143*54fd6939SJiyong Parkvector defined by the constant ``BL1_RO_BASE``. The BL1 data section is copied
144*54fd6939SJiyong Parkto the top of trusted SRAM as defined by the constant ``BL1_RW_BASE``.
145*54fd6939SJiyong Park
146*54fd6939SJiyong ParkThe functionality implemented by this stage is as follows.
147*54fd6939SJiyong Park
148*54fd6939SJiyong ParkDetermination of boot path
149*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^
150*54fd6939SJiyong Park
151*54fd6939SJiyong ParkWhenever a CPU is released from reset, BL1 needs to distinguish between a warm
152*54fd6939SJiyong Parkboot and a cold boot. This is done using platform-specific mechanisms (see the
153*54fd6939SJiyong Park``plat_get_my_entrypoint()`` function in the :ref:`Porting Guide`). In the case
154*54fd6939SJiyong Parkof a warm boot, a CPU is expected to continue execution from a separate
155*54fd6939SJiyong Parkentrypoint. In the case of a cold boot, the secondary CPUs are placed in a safe
156*54fd6939SJiyong Parkplatform-specific state (see the ``plat_secondary_cold_boot_setup()`` function in
157*54fd6939SJiyong Parkthe :ref:`Porting Guide`) while the primary CPU executes the remaining cold boot
158*54fd6939SJiyong Parkpath as described in the following sections.
159*54fd6939SJiyong Park
160*54fd6939SJiyong ParkThis step only applies when ``PROGRAMMABLE_RESET_ADDRESS=0``. Refer to the
161*54fd6939SJiyong Park:ref:`CPU Reset` for more information on the effect of the
162*54fd6939SJiyong Park``PROGRAMMABLE_RESET_ADDRESS`` platform build option.
163*54fd6939SJiyong Park
164*54fd6939SJiyong ParkArchitectural initialization
165*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^
166*54fd6939SJiyong Park
167*54fd6939SJiyong ParkBL1 performs minimal architectural initialization as follows.
168*54fd6939SJiyong Park
169*54fd6939SJiyong Park-  Exception vectors
170*54fd6939SJiyong Park
171*54fd6939SJiyong Park   BL1 sets up simple exception vectors for both synchronous and asynchronous
172*54fd6939SJiyong Park   exceptions. The default behavior upon receiving an exception is to populate
173*54fd6939SJiyong Park   a status code in the general purpose register ``X0/R0`` and call the
174*54fd6939SJiyong Park   ``plat_report_exception()`` function (see the :ref:`Porting Guide`). The
175*54fd6939SJiyong Park   status code is one of:
176*54fd6939SJiyong Park
177*54fd6939SJiyong Park   For AArch64:
178*54fd6939SJiyong Park
179*54fd6939SJiyong Park   ::
180*54fd6939SJiyong Park
181*54fd6939SJiyong Park       0x0 : Synchronous exception from Current EL with SP_EL0
182*54fd6939SJiyong Park       0x1 : IRQ exception from Current EL with SP_EL0
183*54fd6939SJiyong Park       0x2 : FIQ exception from Current EL with SP_EL0
184*54fd6939SJiyong Park       0x3 : System Error exception from Current EL with SP_EL0
185*54fd6939SJiyong Park       0x4 : Synchronous exception from Current EL with SP_ELx
186*54fd6939SJiyong Park       0x5 : IRQ exception from Current EL with SP_ELx
187*54fd6939SJiyong Park       0x6 : FIQ exception from Current EL with SP_ELx
188*54fd6939SJiyong Park       0x7 : System Error exception from Current EL with SP_ELx
189*54fd6939SJiyong Park       0x8 : Synchronous exception from Lower EL using aarch64
190*54fd6939SJiyong Park       0x9 : IRQ exception from Lower EL using aarch64
191*54fd6939SJiyong Park       0xa : FIQ exception from Lower EL using aarch64
192*54fd6939SJiyong Park       0xb : System Error exception from Lower EL using aarch64
193*54fd6939SJiyong Park       0xc : Synchronous exception from Lower EL using aarch32
194*54fd6939SJiyong Park       0xd : IRQ exception from Lower EL using aarch32
195*54fd6939SJiyong Park       0xe : FIQ exception from Lower EL using aarch32
196*54fd6939SJiyong Park       0xf : System Error exception from Lower EL using aarch32
197*54fd6939SJiyong Park
198*54fd6939SJiyong Park   For AArch32:
199*54fd6939SJiyong Park
200*54fd6939SJiyong Park   ::
201*54fd6939SJiyong Park
202*54fd6939SJiyong Park       0x10 : User mode
203*54fd6939SJiyong Park       0x11 : FIQ mode
204*54fd6939SJiyong Park       0x12 : IRQ mode
205*54fd6939SJiyong Park       0x13 : SVC mode
206*54fd6939SJiyong Park       0x16 : Monitor mode
207*54fd6939SJiyong Park       0x17 : Abort mode
208*54fd6939SJiyong Park       0x1a : Hypervisor mode
209*54fd6939SJiyong Park       0x1b : Undefined mode
210*54fd6939SJiyong Park       0x1f : System mode
211*54fd6939SJiyong Park
212*54fd6939SJiyong Park   The ``plat_report_exception()`` implementation on the Arm FVP port programs
213*54fd6939SJiyong Park   the Versatile Express System LED register in the following format to
214*54fd6939SJiyong Park   indicate the occurrence of an unexpected exception:
215*54fd6939SJiyong Park
216*54fd6939SJiyong Park   ::
217*54fd6939SJiyong Park
218*54fd6939SJiyong Park       SYS_LED[0]   - Security state (Secure=0/Non-Secure=1)
219*54fd6939SJiyong Park       SYS_LED[2:1] - Exception Level (EL3=0x3, EL2=0x2, EL1=0x1, EL0=0x0)
220*54fd6939SJiyong Park                      For AArch32 it is always 0x0
221*54fd6939SJiyong Park       SYS_LED[7:3] - Exception Class (Sync/Async & origin). This is the value
222*54fd6939SJiyong Park                      of the status code
223*54fd6939SJiyong Park
224*54fd6939SJiyong Park   A write to the LED register reflects in the System LEDs (S6LED0..7) in the
225*54fd6939SJiyong Park   CLCD window of the FVP.
226*54fd6939SJiyong Park
227*54fd6939SJiyong Park   BL1 does not expect to receive any exceptions other than the SMC exception.
228*54fd6939SJiyong Park   For the latter, BL1 installs a simple stub. The stub expects to receive a
229*54fd6939SJiyong Park   limited set of SMC types (determined by their function IDs in the general
230*54fd6939SJiyong Park   purpose register ``X0/R0``):
231*54fd6939SJiyong Park
232*54fd6939SJiyong Park   -  ``BL1_SMC_RUN_IMAGE``: This SMC is raised by BL2 to make BL1 pass control
233*54fd6939SJiyong Park      to EL3 Runtime Software.
234*54fd6939SJiyong Park   -  All SMCs listed in section "BL1 SMC Interface" in the :ref:`Firmware Update (FWU)`
235*54fd6939SJiyong Park      Design Guide are supported for AArch64 only. These SMCs are currently
236*54fd6939SJiyong Park      not supported when BL1 is built for AArch32.
237*54fd6939SJiyong Park
238*54fd6939SJiyong Park   Any other SMC leads to an assertion failure.
239*54fd6939SJiyong Park
240*54fd6939SJiyong Park-  CPU initialization
241*54fd6939SJiyong Park
242*54fd6939SJiyong Park   BL1 calls the ``reset_handler()`` function which in turn calls the CPU
243*54fd6939SJiyong Park   specific reset handler function (see the section: "CPU specific operations
244*54fd6939SJiyong Park   framework").
245*54fd6939SJiyong Park
246*54fd6939SJiyong Park-  Control register setup (for AArch64)
247*54fd6939SJiyong Park
248*54fd6939SJiyong Park   -  ``SCTLR_EL3``. Instruction cache is enabled by setting the ``SCTLR_EL3.I``
249*54fd6939SJiyong Park      bit. Alignment and stack alignment checking is enabled by setting the
250*54fd6939SJiyong Park      ``SCTLR_EL3.A`` and ``SCTLR_EL3.SA`` bits. Exception endianness is set to
251*54fd6939SJiyong Park      little-endian by clearing the ``SCTLR_EL3.EE`` bit.
252*54fd6939SJiyong Park
253*54fd6939SJiyong Park   -  ``SCR_EL3``. The register width of the next lower exception level is set
254*54fd6939SJiyong Park      to AArch64 by setting the ``SCR.RW`` bit. The ``SCR.EA`` bit is set to trap
255*54fd6939SJiyong Park      both External Aborts and SError Interrupts in EL3. The ``SCR.SIF`` bit is
256*54fd6939SJiyong Park      also set to disable instruction fetches from Non-secure memory when in
257*54fd6939SJiyong Park      secure state.
258*54fd6939SJiyong Park
259*54fd6939SJiyong Park   -  ``CPTR_EL3``. Accesses to the ``CPACR_EL1`` register from EL1 or EL2, or the
260*54fd6939SJiyong Park      ``CPTR_EL2`` register from EL2 are configured to not trap to EL3 by
261*54fd6939SJiyong Park      clearing the ``CPTR_EL3.TCPAC`` bit. Access to the trace functionality is
262*54fd6939SJiyong Park      configured not to trap to EL3 by clearing the ``CPTR_EL3.TTA`` bit.
263*54fd6939SJiyong Park      Instructions that access the registers associated with Floating Point
264*54fd6939SJiyong Park      and Advanced SIMD execution are configured to not trap to EL3 by
265*54fd6939SJiyong Park      clearing the ``CPTR_EL3.TFP`` bit.
266*54fd6939SJiyong Park
267*54fd6939SJiyong Park   -  ``DAIF``. The SError interrupt is enabled by clearing the SError interrupt
268*54fd6939SJiyong Park      mask bit.
269*54fd6939SJiyong Park
270*54fd6939SJiyong Park   -  ``MDCR_EL3``. The trap controls, ``MDCR_EL3.TDOSA``, ``MDCR_EL3.TDA`` and
271*54fd6939SJiyong Park      ``MDCR_EL3.TPM``, are set so that accesses to the registers they control
272*54fd6939SJiyong Park      do not trap to EL3. AArch64 Secure self-hosted debug is disabled by
273*54fd6939SJiyong Park      setting the ``MDCR_EL3.SDD`` bit. Also ``MDCR_EL3.SPD32`` is set to
274*54fd6939SJiyong Park      disable AArch32 Secure self-hosted privileged debug from S-EL1.
275*54fd6939SJiyong Park
276*54fd6939SJiyong Park-  Control register setup (for AArch32)
277*54fd6939SJiyong Park
278*54fd6939SJiyong Park   -  ``SCTLR``. Instruction cache is enabled by setting the ``SCTLR.I`` bit.
279*54fd6939SJiyong Park      Alignment checking is enabled by setting the ``SCTLR.A`` bit.
280*54fd6939SJiyong Park      Exception endianness is set to little-endian by clearing the
281*54fd6939SJiyong Park      ``SCTLR.EE`` bit.
282*54fd6939SJiyong Park
283*54fd6939SJiyong Park   -  ``SCR``. The ``SCR.SIF`` bit is set to disable instruction fetches from
284*54fd6939SJiyong Park      Non-secure memory when in secure state.
285*54fd6939SJiyong Park
286*54fd6939SJiyong Park   -  ``CPACR``. Allow execution of Advanced SIMD instructions at PL0 and PL1,
287*54fd6939SJiyong Park      by clearing the ``CPACR.ASEDIS`` bit. Access to the trace functionality
288*54fd6939SJiyong Park      is configured not to trap to undefined mode by clearing the
289*54fd6939SJiyong Park      ``CPACR.TRCDIS`` bit.
290*54fd6939SJiyong Park
291*54fd6939SJiyong Park   -  ``NSACR``. Enable non-secure access to Advanced SIMD functionality and
292*54fd6939SJiyong Park      system register access to implemented trace registers.
293*54fd6939SJiyong Park
294*54fd6939SJiyong Park   -  ``FPEXC``. Enable access to the Advanced SIMD and floating-point
295*54fd6939SJiyong Park      functionality from all Exception levels.
296*54fd6939SJiyong Park
297*54fd6939SJiyong Park   -  ``CPSR.A``. The Asynchronous data abort interrupt is enabled by clearing
298*54fd6939SJiyong Park      the Asynchronous data abort interrupt mask bit.
299*54fd6939SJiyong Park
300*54fd6939SJiyong Park   -  ``SDCR``. The ``SDCR.SPD`` field is set to disable AArch32 Secure
301*54fd6939SJiyong Park      self-hosted privileged debug.
302*54fd6939SJiyong Park
303*54fd6939SJiyong ParkPlatform initialization
304*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^
305*54fd6939SJiyong Park
306*54fd6939SJiyong ParkOn Arm platforms, BL1 performs the following platform initializations:
307*54fd6939SJiyong Park
308*54fd6939SJiyong Park-  Enable the Trusted Watchdog.
309*54fd6939SJiyong Park-  Initialize the console.
310*54fd6939SJiyong Park-  Configure the Interconnect to enable hardware coherency.
311*54fd6939SJiyong Park-  Enable the MMU and map the memory it needs to access.
312*54fd6939SJiyong Park-  Configure any required platform storage to load the next bootloader image
313*54fd6939SJiyong Park   (BL2).
314*54fd6939SJiyong Park-  If the BL1 dynamic configuration file, ``TB_FW_CONFIG``, is available, then
315*54fd6939SJiyong Park   load it to the platform defined address and make it available to BL2 via
316*54fd6939SJiyong Park   ``arg0``.
317*54fd6939SJiyong Park-  Configure the system timer and program the `CNTFRQ_EL0` for use by NS-BL1U
318*54fd6939SJiyong Park   and NS-BL2U firmware update images.
319*54fd6939SJiyong Park
320*54fd6939SJiyong ParkFirmware Update detection and execution
321*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322*54fd6939SJiyong Park
323*54fd6939SJiyong ParkAfter performing platform setup, BL1 common code calls
324*54fd6939SJiyong Park``bl1_plat_get_next_image_id()`` to determine if :ref:`Firmware Update (FWU)` is
325*54fd6939SJiyong Parkrequired or to proceed with the normal boot process. If the platform code
326*54fd6939SJiyong Parkreturns ``BL2_IMAGE_ID`` then the normal boot sequence is executed as described
327*54fd6939SJiyong Parkin the next section, else BL1 assumes that :ref:`Firmware Update (FWU)` is
328*54fd6939SJiyong Parkrequired and execution passes to the first image in the
329*54fd6939SJiyong Park:ref:`Firmware Update (FWU)` process. In either case, BL1 retrieves a descriptor
330*54fd6939SJiyong Parkof the next image by calling ``bl1_plat_get_image_desc()``. The image descriptor
331*54fd6939SJiyong Parkcontains an ``entry_point_info_t`` structure, which BL1 uses to initialize the
332*54fd6939SJiyong Parkexecution state of the next image.
333*54fd6939SJiyong Park
334*54fd6939SJiyong ParkBL2 image load and execution
335*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^
336*54fd6939SJiyong Park
337*54fd6939SJiyong ParkIn the normal boot flow, BL1 execution continues as follows:
338*54fd6939SJiyong Park
339*54fd6939SJiyong Park#. BL1 prints the following string from the primary CPU to indicate successful
340*54fd6939SJiyong Park   execution of the BL1 stage:
341*54fd6939SJiyong Park
342*54fd6939SJiyong Park   ::
343*54fd6939SJiyong Park
344*54fd6939SJiyong Park       "Booting Trusted Firmware"
345*54fd6939SJiyong Park
346*54fd6939SJiyong Park#. BL1 loads a BL2 raw binary image from platform storage, at a
347*54fd6939SJiyong Park   platform-specific base address. Prior to the load, BL1 invokes
348*54fd6939SJiyong Park   ``bl1_plat_handle_pre_image_load()`` which allows the platform to update or
349*54fd6939SJiyong Park   use the image information. If the BL2 image file is not present or if
350*54fd6939SJiyong Park   there is not enough free trusted SRAM the following error message is
351*54fd6939SJiyong Park   printed:
352*54fd6939SJiyong Park
353*54fd6939SJiyong Park   ::
354*54fd6939SJiyong Park
355*54fd6939SJiyong Park       "Failed to load BL2 firmware."
356*54fd6939SJiyong Park
357*54fd6939SJiyong Park#. BL1 invokes ``bl1_plat_handle_post_image_load()`` which again is intended
358*54fd6939SJiyong Park   for platforms to take further action after image load. This function must
359*54fd6939SJiyong Park   populate the necessary arguments for BL2, which may also include the memory
360*54fd6939SJiyong Park   layout. Further description of the memory layout can be found later
361*54fd6939SJiyong Park   in this document.
362*54fd6939SJiyong Park
363*54fd6939SJiyong Park#. BL1 passes control to the BL2 image at Secure EL1 (for AArch64) or at
364*54fd6939SJiyong Park   Secure SVC mode (for AArch32), starting from its load address.
365*54fd6939SJiyong Park
366*54fd6939SJiyong ParkBL2
367*54fd6939SJiyong Park~~~
368*54fd6939SJiyong Park
369*54fd6939SJiyong ParkBL1 loads and passes control to BL2 at Secure-EL1 (for AArch64) or at Secure
370*54fd6939SJiyong ParkSVC mode (for AArch32) . BL2 is linked against and loaded at a platform-specific
371*54fd6939SJiyong Parkbase address (more information can be found later in this document).
372*54fd6939SJiyong ParkThe functionality implemented by BL2 is as follows.
373*54fd6939SJiyong Park
374*54fd6939SJiyong ParkArchitectural initialization
375*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^
376*54fd6939SJiyong Park
377*54fd6939SJiyong ParkFor AArch64, BL2 performs the minimal architectural initialization required
378*54fd6939SJiyong Parkfor subsequent stages of TF-A and normal world software. EL1 and EL0 are given
379*54fd6939SJiyong Parkaccess to Floating Point and Advanced SIMD registers by setting the
380*54fd6939SJiyong Park``CPACR.FPEN`` bits.
381*54fd6939SJiyong Park
382*54fd6939SJiyong ParkFor AArch32, the minimal architectural initialization required for subsequent
383*54fd6939SJiyong Parkstages of TF-A and normal world software is taken care of in BL1 as both BL1
384*54fd6939SJiyong Parkand BL2 execute at PL1.
385*54fd6939SJiyong Park
386*54fd6939SJiyong ParkPlatform initialization
387*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^
388*54fd6939SJiyong Park
389*54fd6939SJiyong ParkOn Arm platforms, BL2 performs the following platform initializations:
390*54fd6939SJiyong Park
391*54fd6939SJiyong Park-  Initialize the console.
392*54fd6939SJiyong Park-  Configure any required platform storage to allow loading further bootloader
393*54fd6939SJiyong Park   images.
394*54fd6939SJiyong Park-  Enable the MMU and map the memory it needs to access.
395*54fd6939SJiyong Park-  Perform platform security setup to allow access to controlled components.
396*54fd6939SJiyong Park-  Reserve some memory for passing information to the next bootloader image
397*54fd6939SJiyong Park   EL3 Runtime Software and populate it.
398*54fd6939SJiyong Park-  Define the extents of memory available for loading each subsequent
399*54fd6939SJiyong Park   bootloader image.
400*54fd6939SJiyong Park-  If BL1 has passed TB_FW_CONFIG dynamic configuration file in ``arg0``,
401*54fd6939SJiyong Park   then parse it.
402*54fd6939SJiyong Park
403*54fd6939SJiyong ParkImage loading in BL2
404*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^
405*54fd6939SJiyong Park
406*54fd6939SJiyong ParkBL2 generic code loads the images based on the list of loadable images
407*54fd6939SJiyong Parkprovided by the platform. BL2 passes the list of executable images
408*54fd6939SJiyong Parkprovided by the platform to the next handover BL image.
409*54fd6939SJiyong Park
410*54fd6939SJiyong ParkThe list of loadable images provided by the platform may also contain
411*54fd6939SJiyong Parkdynamic configuration files. The files are loaded and can be parsed as
412*54fd6939SJiyong Parkneeded in the ``bl2_plat_handle_post_image_load()`` function. These
413*54fd6939SJiyong Parkconfiguration files can be passed to next Boot Loader stages as arguments
414*54fd6939SJiyong Parkby updating the corresponding entrypoint information in this function.
415*54fd6939SJiyong Park
416*54fd6939SJiyong ParkSCP_BL2 (System Control Processor Firmware) image load
417*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
418*54fd6939SJiyong Park
419*54fd6939SJiyong ParkSome systems have a separate System Control Processor (SCP) for power, clock,
420*54fd6939SJiyong Parkreset and system control. BL2 loads the optional SCP_BL2 image from platform
421*54fd6939SJiyong Parkstorage into a platform-specific region of secure memory. The subsequent
422*54fd6939SJiyong Parkhandling of SCP_BL2 is platform specific. For example, on the Juno Arm
423*54fd6939SJiyong Parkdevelopment platform port the image is transferred into SCP's internal memory
424*54fd6939SJiyong Parkusing the Boot Over MHU (BOM) protocol after being loaded in the trusted SRAM
425*54fd6939SJiyong Parkmemory. The SCP executes SCP_BL2 and signals to the Application Processor (AP)
426*54fd6939SJiyong Parkfor BL2 execution to continue.
427*54fd6939SJiyong Park
428*54fd6939SJiyong ParkEL3 Runtime Software image load
429*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
430*54fd6939SJiyong Park
431*54fd6939SJiyong ParkBL2 loads the EL3 Runtime Software image from platform storage into a platform-
432*54fd6939SJiyong Parkspecific address in trusted SRAM. If there is not enough memory to load the
433*54fd6939SJiyong Parkimage or image is missing it leads to an assertion failure.
434*54fd6939SJiyong Park
435*54fd6939SJiyong ParkAArch64 BL32 (Secure-EL1 Payload) image load
436*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
437*54fd6939SJiyong Park
438*54fd6939SJiyong ParkBL2 loads the optional BL32 image from platform storage into a platform-
439*54fd6939SJiyong Parkspecific region of secure memory. The image executes in the secure world. BL2
440*54fd6939SJiyong Parkrelies on BL31 to pass control to the BL32 image, if present. Hence, BL2
441*54fd6939SJiyong Parkpopulates a platform-specific area of memory with the entrypoint/load-address
442*54fd6939SJiyong Parkof the BL32 image. The value of the Saved Processor Status Register (``SPSR``)
443*54fd6939SJiyong Parkfor entry into BL32 is not determined by BL2, it is initialized by the
444*54fd6939SJiyong ParkSecure-EL1 Payload Dispatcher (see later) within BL31, which is responsible for
445*54fd6939SJiyong Parkmanaging interaction with BL32. This information is passed to BL31.
446*54fd6939SJiyong Park
447*54fd6939SJiyong ParkBL33 (Non-trusted Firmware) image load
448*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
449*54fd6939SJiyong Park
450*54fd6939SJiyong ParkBL2 loads the BL33 image (e.g. UEFI or other test or boot software) from
451*54fd6939SJiyong Parkplatform storage into non-secure memory as defined by the platform.
452*54fd6939SJiyong Park
453*54fd6939SJiyong ParkBL2 relies on EL3 Runtime Software to pass control to BL33 once secure state
454*54fd6939SJiyong Parkinitialization is complete. Hence, BL2 populates a platform-specific area of
455*54fd6939SJiyong Parkmemory with the entrypoint and Saved Program Status Register (``SPSR``) of the
456*54fd6939SJiyong Parknormal world software image. The entrypoint is the load address of the BL33
457*54fd6939SJiyong Parkimage. The ``SPSR`` is determined as specified in Section 5.13 of the
458*54fd6939SJiyong Park`Power State Coordination Interface PDD`_. This information is passed to the
459*54fd6939SJiyong ParkEL3 Runtime Software.
460*54fd6939SJiyong Park
461*54fd6939SJiyong ParkAArch64 BL31 (EL3 Runtime Software) execution
462*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
463*54fd6939SJiyong Park
464*54fd6939SJiyong ParkBL2 execution continues as follows:
465*54fd6939SJiyong Park
466*54fd6939SJiyong Park#. BL2 passes control back to BL1 by raising an SMC, providing BL1 with the
467*54fd6939SJiyong Park   BL31 entrypoint. The exception is handled by the SMC exception handler
468*54fd6939SJiyong Park   installed by BL1.
469*54fd6939SJiyong Park
470*54fd6939SJiyong Park#. BL1 turns off the MMU and flushes the caches. It clears the
471*54fd6939SJiyong Park   ``SCTLR_EL3.M/I/C`` bits, flushes the data cache to the point of coherency
472*54fd6939SJiyong Park   and invalidates the TLBs.
473*54fd6939SJiyong Park
474*54fd6939SJiyong Park#. BL1 passes control to BL31 at the specified entrypoint at EL3.
475*54fd6939SJiyong Park
476*54fd6939SJiyong ParkRunning BL2 at EL3 execution level
477*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
478*54fd6939SJiyong Park
479*54fd6939SJiyong ParkSome platforms have a non-TF-A Boot ROM that expects the next boot stage
480*54fd6939SJiyong Parkto execute at EL3. On these platforms, TF-A BL1 is a waste of memory
481*54fd6939SJiyong Parkas its only purpose is to ensure TF-A BL2 is entered at S-EL1. To avoid
482*54fd6939SJiyong Parkthis waste, a special mode enables BL2 to execute at EL3, which allows
483*54fd6939SJiyong Parka non-TF-A Boot ROM to load and jump directly to BL2. This mode is selected
484*54fd6939SJiyong Parkwhen the build flag BL2_AT_EL3 is enabled. The main differences in this
485*54fd6939SJiyong Parkmode are:
486*54fd6939SJiyong Park
487*54fd6939SJiyong Park#. BL2 includes the reset code and the mailbox mechanism to differentiate
488*54fd6939SJiyong Park   cold boot and warm boot. It runs at EL3 doing the arch
489*54fd6939SJiyong Park   initialization required for EL3.
490*54fd6939SJiyong Park
491*54fd6939SJiyong Park#. BL2 does not receive the meminfo information from BL1 anymore. This
492*54fd6939SJiyong Park   information can be passed by the Boot ROM or be internal to the
493*54fd6939SJiyong Park   BL2 image.
494*54fd6939SJiyong Park
495*54fd6939SJiyong Park#. Since BL2 executes at EL3, BL2 jumps directly to the next image,
496*54fd6939SJiyong Park   instead of invoking the RUN_IMAGE SMC call.
497*54fd6939SJiyong Park
498*54fd6939SJiyong Park
499*54fd6939SJiyong ParkWe assume 3 different types of BootROM support on the platform:
500*54fd6939SJiyong Park
501*54fd6939SJiyong Park#. The Boot ROM always jumps to the same address, for both cold
502*54fd6939SJiyong Park   and warm boot. In this case, we will need to keep a resident part
503*54fd6939SJiyong Park   of BL2 whose memory cannot be reclaimed by any other image. The
504*54fd6939SJiyong Park   linker script defines the symbols __TEXT_RESIDENT_START__ and
505*54fd6939SJiyong Park   __TEXT_RESIDENT_END__ that allows the platform to configure
506*54fd6939SJiyong Park   correctly the memory map.
507*54fd6939SJiyong Park#. The platform has some mechanism to indicate the jump address to the
508*54fd6939SJiyong Park   Boot ROM. Platform code can then program the jump address with
509*54fd6939SJiyong Park   psci_warmboot_entrypoint during cold boot.
510*54fd6939SJiyong Park#. The platform has some mechanism to program the reset address using
511*54fd6939SJiyong Park   the PROGRAMMABLE_RESET_ADDRESS feature. Platform code can then
512*54fd6939SJiyong Park   program the reset address with psci_warmboot_entrypoint during
513*54fd6939SJiyong Park   cold boot, bypassing the boot ROM for warm boot.
514*54fd6939SJiyong Park
515*54fd6939SJiyong ParkIn the last 2 cases, no part of BL2 needs to remain resident at
516*54fd6939SJiyong Parkruntime. In the first 2 cases, we expect the Boot ROM to be able to
517*54fd6939SJiyong Parkdifferentiate between warm and cold boot, to avoid loading BL2 again
518*54fd6939SJiyong Parkduring warm boot.
519*54fd6939SJiyong Park
520*54fd6939SJiyong ParkThis functionality can be tested with FVP loading the image directly
521*54fd6939SJiyong Parkin memory and changing the address where the system jumps at reset.
522*54fd6939SJiyong ParkFor example:
523*54fd6939SJiyong Park
524*54fd6939SJiyong Park	-C cluster0.cpu0.RVBAR=0x4022000
525*54fd6939SJiyong Park	--data cluster0.cpu0=bl2.bin@0x4022000
526*54fd6939SJiyong Park
527*54fd6939SJiyong ParkWith this configuration, FVP is like a platform of the first case,
528*54fd6939SJiyong Parkwhere the Boot ROM jumps always to the same address. For simplification,
529*54fd6939SJiyong ParkBL32 is loaded in DRAM in this case, to avoid other images reclaiming
530*54fd6939SJiyong ParkBL2 memory.
531*54fd6939SJiyong Park
532*54fd6939SJiyong Park
533*54fd6939SJiyong ParkAArch64 BL31
534*54fd6939SJiyong Park~~~~~~~~~~~~
535*54fd6939SJiyong Park
536*54fd6939SJiyong ParkThe image for this stage is loaded by BL2 and BL1 passes control to BL31 at
537*54fd6939SJiyong ParkEL3. BL31 executes solely in trusted SRAM. BL31 is linked against and
538*54fd6939SJiyong Parkloaded at a platform-specific base address (more information can be found later
539*54fd6939SJiyong Parkin this document). The functionality implemented by BL31 is as follows.
540*54fd6939SJiyong Park
541*54fd6939SJiyong ParkArchitectural initialization
542*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^
543*54fd6939SJiyong Park
544*54fd6939SJiyong ParkCurrently, BL31 performs a similar architectural initialization to BL1 as
545*54fd6939SJiyong Parkfar as system register settings are concerned. Since BL1 code resides in ROM,
546*54fd6939SJiyong Parkarchitectural initialization in BL31 allows override of any previous
547*54fd6939SJiyong Parkinitialization done by BL1.
548*54fd6939SJiyong Park
549*54fd6939SJiyong ParkBL31 initializes the per-CPU data framework, which provides a cache of
550*54fd6939SJiyong Parkfrequently accessed per-CPU data optimised for fast, concurrent manipulation
551*54fd6939SJiyong Parkon different CPUs. This buffer includes pointers to per-CPU contexts, crash
552*54fd6939SJiyong Parkbuffer, CPU reset and power down operations, PSCI data, platform data and so on.
553*54fd6939SJiyong Park
554*54fd6939SJiyong ParkIt then replaces the exception vectors populated by BL1 with its own. BL31
555*54fd6939SJiyong Parkexception vectors implement more elaborate support for handling SMCs since this
556*54fd6939SJiyong Parkis the only mechanism to access the runtime services implemented by BL31 (PSCI
557*54fd6939SJiyong Parkfor example). BL31 checks each SMC for validity as specified by the
558*54fd6939SJiyong Park`SMC Calling Convention`_ before passing control to the required SMC
559*54fd6939SJiyong Parkhandler routine.
560*54fd6939SJiyong Park
561*54fd6939SJiyong ParkBL31 programs the ``CNTFRQ_EL0`` register with the clock frequency of the system
562*54fd6939SJiyong Parkcounter, which is provided by the platform.
563*54fd6939SJiyong Park
564*54fd6939SJiyong ParkPlatform initialization
565*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^
566*54fd6939SJiyong Park
567*54fd6939SJiyong ParkBL31 performs detailed platform initialization, which enables normal world
568*54fd6939SJiyong Parksoftware to function correctly.
569*54fd6939SJiyong Park
570*54fd6939SJiyong ParkOn Arm platforms, this consists of the following:
571*54fd6939SJiyong Park
572*54fd6939SJiyong Park-  Initialize the console.
573*54fd6939SJiyong Park-  Configure the Interconnect to enable hardware coherency.
574*54fd6939SJiyong Park-  Enable the MMU and map the memory it needs to access.
575*54fd6939SJiyong Park-  Initialize the generic interrupt controller.
576*54fd6939SJiyong Park-  Initialize the power controller device.
577*54fd6939SJiyong Park-  Detect the system topology.
578*54fd6939SJiyong Park
579*54fd6939SJiyong ParkRuntime services initialization
580*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
581*54fd6939SJiyong Park
582*54fd6939SJiyong ParkBL31 is responsible for initializing the runtime services. One of them is PSCI.
583*54fd6939SJiyong Park
584*54fd6939SJiyong ParkAs part of the PSCI initializations, BL31 detects the system topology. It also
585*54fd6939SJiyong Parkinitializes the data structures that implement the state machine used to track
586*54fd6939SJiyong Parkthe state of power domain nodes. The state can be one of ``OFF``, ``RUN`` or
587*54fd6939SJiyong Park``RETENTION``. All secondary CPUs are initially in the ``OFF`` state. The cluster
588*54fd6939SJiyong Parkthat the primary CPU belongs to is ``ON``; any other cluster is ``OFF``. It also
589*54fd6939SJiyong Parkinitializes the locks that protect them. BL31 accesses the state of a CPU or
590*54fd6939SJiyong Parkcluster immediately after reset and before the data cache is enabled in the
591*54fd6939SJiyong Parkwarm boot path. It is not currently possible to use 'exclusive' based spinlocks,
592*54fd6939SJiyong Parktherefore BL31 uses locks based on Lamport's Bakery algorithm instead.
593*54fd6939SJiyong Park
594*54fd6939SJiyong ParkThe runtime service framework and its initialization is described in more
595*54fd6939SJiyong Parkdetail in the "EL3 runtime services framework" section below.
596*54fd6939SJiyong Park
597*54fd6939SJiyong ParkDetails about the status of the PSCI implementation are provided in the
598*54fd6939SJiyong Park"Power State Coordination Interface" section below.
599*54fd6939SJiyong Park
600*54fd6939SJiyong ParkAArch64 BL32 (Secure-EL1 Payload) image initialization
601*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
602*54fd6939SJiyong Park
603*54fd6939SJiyong ParkIf a BL32 image is present then there must be a matching Secure-EL1 Payload
604*54fd6939SJiyong ParkDispatcher (SPD) service (see later for details). During initialization
605*54fd6939SJiyong Parkthat service must register a function to carry out initialization of BL32
606*54fd6939SJiyong Parkonce the runtime services are fully initialized. BL31 invokes such a
607*54fd6939SJiyong Parkregistered function to initialize BL32 before running BL33. This initialization
608*54fd6939SJiyong Parkis not necessary for AArch32 SPs.
609*54fd6939SJiyong Park
610*54fd6939SJiyong ParkDetails on BL32 initialization and the SPD's role are described in the
611*54fd6939SJiyong Park:ref:`firmware_design_sel1_spd` section below.
612*54fd6939SJiyong Park
613*54fd6939SJiyong ParkBL33 (Non-trusted Firmware) execution
614*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
615*54fd6939SJiyong Park
616*54fd6939SJiyong ParkEL3 Runtime Software initializes the EL2 or EL1 processor context for normal-
617*54fd6939SJiyong Parkworld cold boot, ensuring that no secure state information finds its way into
618*54fd6939SJiyong Parkthe non-secure execution state. EL3 Runtime Software uses the entrypoint
619*54fd6939SJiyong Parkinformation provided by BL2 to jump to the Non-trusted firmware image (BL33)
620*54fd6939SJiyong Parkat the highest available Exception Level (EL2 if available, otherwise EL1).
621*54fd6939SJiyong Park
622*54fd6939SJiyong ParkUsing alternative Trusted Boot Firmware in place of BL1 & BL2 (AArch64 only)
623*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
624*54fd6939SJiyong Park
625*54fd6939SJiyong ParkSome platforms have existing implementations of Trusted Boot Firmware that
626*54fd6939SJiyong Parkwould like to use TF-A BL31 for the EL3 Runtime Software. To enable this
627*54fd6939SJiyong Parkfirmware architecture it is important to provide a fully documented and stable
628*54fd6939SJiyong Parkinterface between the Trusted Boot Firmware and BL31.
629*54fd6939SJiyong Park
630*54fd6939SJiyong ParkFuture changes to the BL31 interface will be done in a backwards compatible
631*54fd6939SJiyong Parkway, and this enables these firmware components to be independently enhanced/
632*54fd6939SJiyong Parkupdated to develop and exploit new functionality.
633*54fd6939SJiyong Park
634*54fd6939SJiyong ParkRequired CPU state when calling ``bl31_entrypoint()`` during cold boot
635*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
636*54fd6939SJiyong Park
637*54fd6939SJiyong ParkThis function must only be called by the primary CPU.
638*54fd6939SJiyong Park
639*54fd6939SJiyong ParkOn entry to this function the calling primary CPU must be executing in AArch64
640*54fd6939SJiyong ParkEL3, little-endian data access, and all interrupt sources masked:
641*54fd6939SJiyong Park
642*54fd6939SJiyong Park::
643*54fd6939SJiyong Park
644*54fd6939SJiyong Park    PSTATE.EL = 3
645*54fd6939SJiyong Park    PSTATE.RW = 1
646*54fd6939SJiyong Park    PSTATE.DAIF = 0xf
647*54fd6939SJiyong Park    SCTLR_EL3.EE = 0
648*54fd6939SJiyong Park
649*54fd6939SJiyong ParkX0 and X1 can be used to pass information from the Trusted Boot Firmware to the
650*54fd6939SJiyong Parkplatform code in BL31:
651*54fd6939SJiyong Park
652*54fd6939SJiyong Park::
653*54fd6939SJiyong Park
654*54fd6939SJiyong Park    X0 : Reserved for common TF-A information
655*54fd6939SJiyong Park    X1 : Platform specific information
656*54fd6939SJiyong Park
657*54fd6939SJiyong ParkBL31 zero-init sections (e.g. ``.bss``) should not contain valid data on entry,
658*54fd6939SJiyong Parkthese will be zero filled prior to invoking platform setup code.
659*54fd6939SJiyong Park
660*54fd6939SJiyong ParkUse of the X0 and X1 parameters
661*54fd6939SJiyong Park'''''''''''''''''''''''''''''''
662*54fd6939SJiyong Park
663*54fd6939SJiyong ParkThe parameters are platform specific and passed from ``bl31_entrypoint()`` to
664*54fd6939SJiyong Park``bl31_early_platform_setup()``. The value of these parameters is never directly
665*54fd6939SJiyong Parkused by the common BL31 code.
666*54fd6939SJiyong Park
667*54fd6939SJiyong ParkThe convention is that ``X0`` conveys information regarding the BL31, BL32 and
668*54fd6939SJiyong ParkBL33 images from the Trusted Boot firmware and ``X1`` can be used for other
669*54fd6939SJiyong Parkplatform specific purpose. This convention allows platforms which use TF-A's
670*54fd6939SJiyong ParkBL1 and BL2 images to transfer additional platform specific information from
671*54fd6939SJiyong ParkSecure Boot without conflicting with future evolution of TF-A using ``X0`` to
672*54fd6939SJiyong Parkpass a ``bl31_params`` structure.
673*54fd6939SJiyong Park
674*54fd6939SJiyong ParkBL31 common and SPD initialization code depends on image and entrypoint
675*54fd6939SJiyong Parkinformation about BL33 and BL32, which is provided via BL31 platform APIs.
676*54fd6939SJiyong ParkThis information is required until the start of execution of BL33. This
677*54fd6939SJiyong Parkinformation can be provided in a platform defined manner, e.g. compiled into
678*54fd6939SJiyong Parkthe platform code in BL31, or provided in a platform defined memory location
679*54fd6939SJiyong Parkby the Trusted Boot firmware, or passed from the Trusted Boot Firmware via the
680*54fd6939SJiyong ParkCold boot Initialization parameters. This data may need to be cleaned out of
681*54fd6939SJiyong Parkthe CPU caches if it is provided by an earlier boot stage and then accessed by
682*54fd6939SJiyong ParkBL31 platform code before the caches are enabled.
683*54fd6939SJiyong Park
684*54fd6939SJiyong ParkTF-A's BL2 implementation passes a ``bl31_params`` structure in
685*54fd6939SJiyong Park``X0`` and the Arm development platforms interpret this in the BL31 platform
686*54fd6939SJiyong Parkcode.
687*54fd6939SJiyong Park
688*54fd6939SJiyong ParkMMU, Data caches & Coherency
689*54fd6939SJiyong Park''''''''''''''''''''''''''''
690*54fd6939SJiyong Park
691*54fd6939SJiyong ParkBL31 does not depend on the enabled state of the MMU, data caches or
692*54fd6939SJiyong Parkinterconnect coherency on entry to ``bl31_entrypoint()``. If these are disabled
693*54fd6939SJiyong Parkon entry, these should be enabled during ``bl31_plat_arch_setup()``.
694*54fd6939SJiyong Park
695*54fd6939SJiyong ParkData structures used in the BL31 cold boot interface
696*54fd6939SJiyong Park''''''''''''''''''''''''''''''''''''''''''''''''''''
697*54fd6939SJiyong Park
698*54fd6939SJiyong ParkThese structures are designed to support compatibility and independent
699*54fd6939SJiyong Parkevolution of the structures and the firmware images. For example, a version of
700*54fd6939SJiyong ParkBL31 that can interpret the BL3x image information from different versions of
701*54fd6939SJiyong ParkBL2, a platform that uses an extended entry_point_info structure to convey
702*54fd6939SJiyong Parkadditional register information to BL31, or a ELF image loader that can convey
703*54fd6939SJiyong Parkmore details about the firmware images.
704*54fd6939SJiyong Park
705*54fd6939SJiyong ParkTo support these scenarios the structures are versioned and sized, which enables
706*54fd6939SJiyong ParkBL31 to detect which information is present and respond appropriately. The
707*54fd6939SJiyong Park``param_header`` is defined to capture this information:
708*54fd6939SJiyong Park
709*54fd6939SJiyong Park.. code:: c
710*54fd6939SJiyong Park
711*54fd6939SJiyong Park    typedef struct param_header {
712*54fd6939SJiyong Park        uint8_t type;       /* type of the structure */
713*54fd6939SJiyong Park        uint8_t version;    /* version of this structure */
714*54fd6939SJiyong Park        uint16_t size;      /* size of this structure in bytes */
715*54fd6939SJiyong Park        uint32_t attr;      /* attributes: unused bits SBZ */
716*54fd6939SJiyong Park    } param_header_t;
717*54fd6939SJiyong Park
718*54fd6939SJiyong ParkThe structures using this format are ``entry_point_info``, ``image_info`` and
719*54fd6939SJiyong Park``bl31_params``. The code that allocates and populates these structures must set
720*54fd6939SJiyong Parkthe header fields appropriately, and the ``SET_PARAM_HEAD()`` a macro is defined
721*54fd6939SJiyong Parkto simplify this action.
722*54fd6939SJiyong Park
723*54fd6939SJiyong ParkRequired CPU state for BL31 Warm boot initialization
724*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
725*54fd6939SJiyong Park
726*54fd6939SJiyong ParkWhen requesting a CPU power-on, or suspending a running CPU, TF-A provides
727*54fd6939SJiyong Parkthe platform power management code with a Warm boot initialization
728*54fd6939SJiyong Parkentry-point, to be invoked by the CPU immediately after the reset handler.
729*54fd6939SJiyong ParkOn entry to the Warm boot initialization function the calling CPU must be in
730*54fd6939SJiyong ParkAArch64 EL3, little-endian data access and all interrupt sources masked:
731*54fd6939SJiyong Park
732*54fd6939SJiyong Park::
733*54fd6939SJiyong Park
734*54fd6939SJiyong Park    PSTATE.EL = 3
735*54fd6939SJiyong Park    PSTATE.RW = 1
736*54fd6939SJiyong Park    PSTATE.DAIF = 0xf
737*54fd6939SJiyong Park    SCTLR_EL3.EE = 0
738*54fd6939SJiyong Park
739*54fd6939SJiyong ParkThe PSCI implementation will initialize the processor state and ensure that the
740*54fd6939SJiyong Parkplatform power management code is then invoked as required to initialize all
741*54fd6939SJiyong Parknecessary system, cluster and CPU resources.
742*54fd6939SJiyong Park
743*54fd6939SJiyong ParkAArch32 EL3 Runtime Software entrypoint interface
744*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
745*54fd6939SJiyong Park
746*54fd6939SJiyong ParkTo enable this firmware architecture it is important to provide a fully
747*54fd6939SJiyong Parkdocumented and stable interface between the Trusted Boot Firmware and the
748*54fd6939SJiyong ParkAArch32 EL3 Runtime Software.
749*54fd6939SJiyong Park
750*54fd6939SJiyong ParkFuture changes to the entrypoint interface will be done in a backwards
751*54fd6939SJiyong Parkcompatible way, and this enables these firmware components to be independently
752*54fd6939SJiyong Parkenhanced/updated to develop and exploit new functionality.
753*54fd6939SJiyong Park
754*54fd6939SJiyong ParkRequired CPU state when entering during cold boot
755*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
756*54fd6939SJiyong Park
757*54fd6939SJiyong ParkThis function must only be called by the primary CPU.
758*54fd6939SJiyong Park
759*54fd6939SJiyong ParkOn entry to this function the calling primary CPU must be executing in AArch32
760*54fd6939SJiyong ParkEL3, little-endian data access, and all interrupt sources masked:
761*54fd6939SJiyong Park
762*54fd6939SJiyong Park::
763*54fd6939SJiyong Park
764*54fd6939SJiyong Park    PSTATE.AIF = 0x7
765*54fd6939SJiyong Park    SCTLR.EE = 0
766*54fd6939SJiyong Park
767*54fd6939SJiyong ParkR0 and R1 are used to pass information from the Trusted Boot Firmware to the
768*54fd6939SJiyong Parkplatform code in AArch32 EL3 Runtime Software:
769*54fd6939SJiyong Park
770*54fd6939SJiyong Park::
771*54fd6939SJiyong Park
772*54fd6939SJiyong Park    R0 : Reserved for common TF-A information
773*54fd6939SJiyong Park    R1 : Platform specific information
774*54fd6939SJiyong Park
775*54fd6939SJiyong ParkUse of the R0 and R1 parameters
776*54fd6939SJiyong Park'''''''''''''''''''''''''''''''
777*54fd6939SJiyong Park
778*54fd6939SJiyong ParkThe parameters are platform specific and the convention is that ``R0`` conveys
779*54fd6939SJiyong Parkinformation regarding the BL3x images from the Trusted Boot firmware and ``R1``
780*54fd6939SJiyong Parkcan be used for other platform specific purpose. This convention allows
781*54fd6939SJiyong Parkplatforms which use TF-A's BL1 and BL2 images to transfer additional platform
782*54fd6939SJiyong Parkspecific information from Secure Boot without conflicting with future
783*54fd6939SJiyong Parkevolution of TF-A using ``R0`` to pass a ``bl_params`` structure.
784*54fd6939SJiyong Park
785*54fd6939SJiyong ParkThe AArch32 EL3 Runtime Software is responsible for entry into BL33. This
786*54fd6939SJiyong Parkinformation can be obtained in a platform defined manner, e.g. compiled into
787*54fd6939SJiyong Parkthe AArch32 EL3 Runtime Software, or provided in a platform defined memory
788*54fd6939SJiyong Parklocation by the Trusted Boot firmware, or passed from the Trusted Boot Firmware
789*54fd6939SJiyong Parkvia the Cold boot Initialization parameters. This data may need to be cleaned
790*54fd6939SJiyong Parkout of the CPU caches if it is provided by an earlier boot stage and then
791*54fd6939SJiyong Parkaccessed by AArch32 EL3 Runtime Software before the caches are enabled.
792*54fd6939SJiyong Park
793*54fd6939SJiyong ParkWhen using AArch32 EL3 Runtime Software, the Arm development platforms pass a
794*54fd6939SJiyong Park``bl_params`` structure in ``R0`` from BL2 to be interpreted by AArch32 EL3 Runtime
795*54fd6939SJiyong ParkSoftware platform code.
796*54fd6939SJiyong Park
797*54fd6939SJiyong ParkMMU, Data caches & Coherency
798*54fd6939SJiyong Park''''''''''''''''''''''''''''
799*54fd6939SJiyong Park
800*54fd6939SJiyong ParkAArch32 EL3 Runtime Software must not depend on the enabled state of the MMU,
801*54fd6939SJiyong Parkdata caches or interconnect coherency in its entrypoint. They must be explicitly
802*54fd6939SJiyong Parkenabled if required.
803*54fd6939SJiyong Park
804*54fd6939SJiyong ParkData structures used in cold boot interface
805*54fd6939SJiyong Park'''''''''''''''''''''''''''''''''''''''''''
806*54fd6939SJiyong Park
807*54fd6939SJiyong ParkThe AArch32 EL3 Runtime Software cold boot interface uses ``bl_params`` instead
808*54fd6939SJiyong Parkof ``bl31_params``. The ``bl_params`` structure is based on the convention
809*54fd6939SJiyong Parkdescribed in AArch64 BL31 cold boot interface section.
810*54fd6939SJiyong Park
811*54fd6939SJiyong ParkRequired CPU state for warm boot initialization
812*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
813*54fd6939SJiyong Park
814*54fd6939SJiyong ParkWhen requesting a CPU power-on, or suspending a running CPU, AArch32 EL3
815*54fd6939SJiyong ParkRuntime Software must ensure execution of a warm boot initialization entrypoint.
816*54fd6939SJiyong ParkIf TF-A BL1 is used and the PROGRAMMABLE_RESET_ADDRESS build flag is false,
817*54fd6939SJiyong Parkthen AArch32 EL3 Runtime Software must ensure that BL1 branches to the warm
818*54fd6939SJiyong Parkboot entrypoint by arranging for the BL1 platform function,
819*54fd6939SJiyong Parkplat_get_my_entrypoint(), to return a non-zero value.
820*54fd6939SJiyong Park
821*54fd6939SJiyong ParkIn this case, the warm boot entrypoint must be in AArch32 EL3, little-endian
822*54fd6939SJiyong Parkdata access and all interrupt sources masked:
823*54fd6939SJiyong Park
824*54fd6939SJiyong Park::
825*54fd6939SJiyong Park
826*54fd6939SJiyong Park    PSTATE.AIF = 0x7
827*54fd6939SJiyong Park    SCTLR.EE = 0
828*54fd6939SJiyong Park
829*54fd6939SJiyong ParkThe warm boot entrypoint may be implemented by using TF-A
830*54fd6939SJiyong Park``psci_warmboot_entrypoint()`` function. In that case, the platform must fulfil
831*54fd6939SJiyong Parkthe pre-requisites mentioned in the
832*54fd6939SJiyong Park:ref:`PSCI Library Integration guide for Armv8-A AArch32 systems`.
833*54fd6939SJiyong Park
834*54fd6939SJiyong ParkEL3 runtime services framework
835*54fd6939SJiyong Park------------------------------
836*54fd6939SJiyong Park
837*54fd6939SJiyong ParkSoftware executing in the non-secure state and in the secure state at exception
838*54fd6939SJiyong Parklevels lower than EL3 will request runtime services using the Secure Monitor
839*54fd6939SJiyong ParkCall (SMC) instruction. These requests will follow the convention described in
840*54fd6939SJiyong Parkthe SMC Calling Convention PDD (`SMCCC`_). The `SMCCC`_ assigns function
841*54fd6939SJiyong Parkidentifiers to each SMC request and describes how arguments are passed and
842*54fd6939SJiyong Parkreturned.
843*54fd6939SJiyong Park
844*54fd6939SJiyong ParkThe EL3 runtime services framework enables the development of services by
845*54fd6939SJiyong Parkdifferent providers that can be easily integrated into final product firmware.
846*54fd6939SJiyong ParkThe following sections describe the framework which facilitates the
847*54fd6939SJiyong Parkregistration, initialization and use of runtime services in EL3 Runtime
848*54fd6939SJiyong ParkSoftware (BL31).
849*54fd6939SJiyong Park
850*54fd6939SJiyong ParkThe design of the runtime services depends heavily on the concepts and
851*54fd6939SJiyong Parkdefinitions described in the `SMCCC`_, in particular SMC Function IDs, Owning
852*54fd6939SJiyong ParkEntity Numbers (OEN), Fast and Yielding calls, and the SMC32 and SMC64 calling
853*54fd6939SJiyong Parkconventions. Please refer to that document for more detailed explanation of
854*54fd6939SJiyong Parkthese terms.
855*54fd6939SJiyong Park
856*54fd6939SJiyong ParkThe following runtime services are expected to be implemented first. They have
857*54fd6939SJiyong Parknot all been instantiated in the current implementation.
858*54fd6939SJiyong Park
859*54fd6939SJiyong Park#. Standard service calls
860*54fd6939SJiyong Park
861*54fd6939SJiyong Park   This service is for management of the entire system. The Power State
862*54fd6939SJiyong Park   Coordination Interface (`PSCI`_) is the first set of standard service calls
863*54fd6939SJiyong Park   defined by Arm (see PSCI section later).
864*54fd6939SJiyong Park
865*54fd6939SJiyong Park#. Secure-EL1 Payload Dispatcher service
866*54fd6939SJiyong Park
867*54fd6939SJiyong Park   If a system runs a Trusted OS or other Secure-EL1 Payload (SP) then
868*54fd6939SJiyong Park   it also requires a *Secure Monitor* at EL3 to switch the EL1 processor
869*54fd6939SJiyong Park   context between the normal world (EL1/EL2) and trusted world (Secure-EL1).
870*54fd6939SJiyong Park   The Secure Monitor will make these world switches in response to SMCs. The
871*54fd6939SJiyong Park   `SMCCC`_ provides for such SMCs with the Trusted OS Call and Trusted
872*54fd6939SJiyong Park   Application Call OEN ranges.
873*54fd6939SJiyong Park
874*54fd6939SJiyong Park   The interface between the EL3 Runtime Software and the Secure-EL1 Payload is
875*54fd6939SJiyong Park   not defined by the `SMCCC`_ or any other standard. As a result, each
876*54fd6939SJiyong Park   Secure-EL1 Payload requires a specific Secure Monitor that runs as a runtime
877*54fd6939SJiyong Park   service - within TF-A this service is referred to as the Secure-EL1 Payload
878*54fd6939SJiyong Park   Dispatcher (SPD).
879*54fd6939SJiyong Park
880*54fd6939SJiyong Park   TF-A provides a Test Secure-EL1 Payload (TSP) and its associated Dispatcher
881*54fd6939SJiyong Park   (TSPD). Details of SPD design and TSP/TSPD operation are described in the
882*54fd6939SJiyong Park   :ref:`firmware_design_sel1_spd` section below.
883*54fd6939SJiyong Park
884*54fd6939SJiyong Park#. CPU implementation service
885*54fd6939SJiyong Park
886*54fd6939SJiyong Park   This service will provide an interface to CPU implementation specific
887*54fd6939SJiyong Park   services for a given platform e.g. access to processor errata workarounds.
888*54fd6939SJiyong Park   This service is currently unimplemented.
889*54fd6939SJiyong Park
890*54fd6939SJiyong ParkAdditional services for Arm Architecture, SiP and OEM calls can be implemented.
891*54fd6939SJiyong ParkEach implemented service handles a range of SMC function identifiers as
892*54fd6939SJiyong Parkdescribed in the `SMCCC`_.
893*54fd6939SJiyong Park
894*54fd6939SJiyong ParkRegistration
895*54fd6939SJiyong Park~~~~~~~~~~~~
896*54fd6939SJiyong Park
897*54fd6939SJiyong ParkA runtime service is registered using the ``DECLARE_RT_SVC()`` macro, specifying
898*54fd6939SJiyong Parkthe name of the service, the range of OENs covered, the type of service and
899*54fd6939SJiyong Parkinitialization and call handler functions. This macro instantiates a ``const struct rt_svc_desc`` for the service with these details (see ``runtime_svc.h``).
900*54fd6939SJiyong ParkThis structure is allocated in a special ELF section ``rt_svc_descs``, enabling
901*54fd6939SJiyong Parkthe framework to find all service descriptors included into BL31.
902*54fd6939SJiyong Park
903*54fd6939SJiyong ParkThe specific service for a SMC Function is selected based on the OEN and call
904*54fd6939SJiyong Parktype of the Function ID, and the framework uses that information in the service
905*54fd6939SJiyong Parkdescriptor to identify the handler for the SMC Call.
906*54fd6939SJiyong Park
907*54fd6939SJiyong ParkThe service descriptors do not include information to identify the precise set
908*54fd6939SJiyong Parkof SMC function identifiers supported by this service implementation, the
909*54fd6939SJiyong Parksecurity state from which such calls are valid nor the capability to support
910*54fd6939SJiyong Park64-bit and/or 32-bit callers (using SMC32 or SMC64). Responding appropriately
911*54fd6939SJiyong Parkto these aspects of a SMC call is the responsibility of the service
912*54fd6939SJiyong Parkimplementation, the framework is focused on integration of services from
913*54fd6939SJiyong Parkdifferent providers and minimizing the time taken by the framework before the
914*54fd6939SJiyong Parkservice handler is invoked.
915*54fd6939SJiyong Park
916*54fd6939SJiyong ParkDetails of the parameters, requirements and behavior of the initialization and
917*54fd6939SJiyong Parkcall handling functions are provided in the following sections.
918*54fd6939SJiyong Park
919*54fd6939SJiyong ParkInitialization
920*54fd6939SJiyong Park~~~~~~~~~~~~~~
921*54fd6939SJiyong Park
922*54fd6939SJiyong Park``runtime_svc_init()`` in ``runtime_svc.c`` initializes the runtime services
923*54fd6939SJiyong Parkframework running on the primary CPU during cold boot as part of the BL31
924*54fd6939SJiyong Parkinitialization. This happens prior to initializing a Trusted OS and running
925*54fd6939SJiyong ParkNormal world boot firmware that might in turn use these services.
926*54fd6939SJiyong ParkInitialization involves validating each of the declared runtime service
927*54fd6939SJiyong Parkdescriptors, calling the service initialization function and populating the
928*54fd6939SJiyong Parkindex used for runtime lookup of the service.
929*54fd6939SJiyong Park
930*54fd6939SJiyong ParkThe BL31 linker script collects all of the declared service descriptors into a
931*54fd6939SJiyong Parksingle array and defines symbols that allow the framework to locate and traverse
932*54fd6939SJiyong Parkthe array, and determine its size.
933*54fd6939SJiyong Park
934*54fd6939SJiyong ParkThe framework does basic validation of each descriptor to halt firmware
935*54fd6939SJiyong Parkinitialization if service declaration errors are detected. The framework does
936*54fd6939SJiyong Parknot check descriptors for the following error conditions, and may behave in an
937*54fd6939SJiyong Parkunpredictable manner under such scenarios:
938*54fd6939SJiyong Park
939*54fd6939SJiyong Park#. Overlapping OEN ranges
940*54fd6939SJiyong Park#. Multiple descriptors for the same range of OENs and ``call_type``
941*54fd6939SJiyong Park#. Incorrect range of owning entity numbers for a given ``call_type``
942*54fd6939SJiyong Park
943*54fd6939SJiyong ParkOnce validated, the service ``init()`` callback is invoked. This function carries
944*54fd6939SJiyong Parkout any essential EL3 initialization before servicing requests. The ``init()``
945*54fd6939SJiyong Parkfunction is only invoked on the primary CPU during cold boot. If the service
946*54fd6939SJiyong Parkuses per-CPU data this must either be initialized for all CPUs during this call,
947*54fd6939SJiyong Parkor be done lazily when a CPU first issues an SMC call to that service. If
948*54fd6939SJiyong Park``init()`` returns anything other than ``0``, this is treated as an initialization
949*54fd6939SJiyong Parkerror and the service is ignored: this does not cause the firmware to halt.
950*54fd6939SJiyong Park
951*54fd6939SJiyong ParkThe OEN and call type fields present in the SMC Function ID cover a total of
952*54fd6939SJiyong Park128 distinct services, but in practice a single descriptor can cover a range of
953*54fd6939SJiyong ParkOENs, e.g. SMCs to call a Trusted OS function. To optimize the lookup of a
954*54fd6939SJiyong Parkservice handler, the framework uses an array of 128 indices that map every
955*54fd6939SJiyong Parkdistinct OEN/call-type combination either to one of the declared services or to
956*54fd6939SJiyong Parkindicate the service is not handled. This ``rt_svc_descs_indices[]`` array is
957*54fd6939SJiyong Parkpopulated for all of the OENs covered by a service after the service ``init()``
958*54fd6939SJiyong Parkfunction has reported success. So a service that fails to initialize will never
959*54fd6939SJiyong Parkhave it's ``handle()`` function invoked.
960*54fd6939SJiyong Park
961*54fd6939SJiyong ParkThe following figure shows how the ``rt_svc_descs_indices[]`` index maps the SMC
962*54fd6939SJiyong ParkFunction ID call type and OEN onto a specific service handler in the
963*54fd6939SJiyong Park``rt_svc_descs[]`` array.
964*54fd6939SJiyong Park
965*54fd6939SJiyong Park|Image 1|
966*54fd6939SJiyong Park
967*54fd6939SJiyong Park.. _handling-an-smc:
968*54fd6939SJiyong Park
969*54fd6939SJiyong ParkHandling an SMC
970*54fd6939SJiyong Park~~~~~~~~~~~~~~~
971*54fd6939SJiyong Park
972*54fd6939SJiyong ParkWhen the EL3 runtime services framework receives a Secure Monitor Call, the SMC
973*54fd6939SJiyong ParkFunction ID is passed in W0 from the lower exception level (as per the
974*54fd6939SJiyong Park`SMCCC`_). If the calling register width is AArch32, it is invalid to invoke an
975*54fd6939SJiyong ParkSMC Function which indicates the SMC64 calling convention: such calls are
976*54fd6939SJiyong Parkignored and return the Unknown SMC Function Identifier result code ``0xFFFFFFFF``
977*54fd6939SJiyong Parkin R0/X0.
978*54fd6939SJiyong Park
979*54fd6939SJiyong ParkBit[31] (fast/yielding call) and bits[29:24] (owning entity number) of the SMC
980*54fd6939SJiyong ParkFunction ID are combined to index into the ``rt_svc_descs_indices[]`` array. The
981*54fd6939SJiyong Parkresulting value might indicate a service that has no handler, in this case the
982*54fd6939SJiyong Parkframework will also report an Unknown SMC Function ID. Otherwise, the value is
983*54fd6939SJiyong Parkused as a further index into the ``rt_svc_descs[]`` array to locate the required
984*54fd6939SJiyong Parkservice and handler.
985*54fd6939SJiyong Park
986*54fd6939SJiyong ParkThe service's ``handle()`` callback is provided with five of the SMC parameters
987*54fd6939SJiyong Parkdirectly, the others are saved into memory for retrieval (if needed) by the
988*54fd6939SJiyong Parkhandler. The handler is also provided with an opaque ``handle`` for use with the
989*54fd6939SJiyong Parksupporting library for parameter retrieval, setting return values and context
990*54fd6939SJiyong Parkmanipulation; and with ``flags`` indicating the security state of the caller. The
991*54fd6939SJiyong Parkframework finally sets up the execution stack for the handler, and invokes the
992*54fd6939SJiyong Parkservices ``handle()`` function.
993*54fd6939SJiyong Park
994*54fd6939SJiyong ParkOn return from the handler the result registers are populated in X0-X7 as needed
995*54fd6939SJiyong Parkbefore restoring the stack and CPU state and returning from the original SMC.
996*54fd6939SJiyong Park
997*54fd6939SJiyong ParkException Handling Framework
998*54fd6939SJiyong Park----------------------------
999*54fd6939SJiyong Park
1000*54fd6939SJiyong ParkPlease refer to the :ref:`Exception Handling Framework` document.
1001*54fd6939SJiyong Park
1002*54fd6939SJiyong ParkPower State Coordination Interface
1003*54fd6939SJiyong Park----------------------------------
1004*54fd6939SJiyong Park
1005*54fd6939SJiyong ParkTODO: Provide design walkthrough of PSCI implementation.
1006*54fd6939SJiyong Park
1007*54fd6939SJiyong ParkThe PSCI v1.1 specification categorizes APIs as optional and mandatory. All the
1008*54fd6939SJiyong Parkmandatory APIs in PSCI v1.1, PSCI v1.0 and in PSCI v0.2 draft specification
1009*54fd6939SJiyong Park`Power State Coordination Interface PDD`_ are implemented. The table lists
1010*54fd6939SJiyong Parkthe PSCI v1.1 APIs and their support in generic code.
1011*54fd6939SJiyong Park
1012*54fd6939SJiyong ParkAn API implementation might have a dependency on platform code e.g. CPU_SUSPEND
1013*54fd6939SJiyong Parkrequires the platform to export a part of the implementation. Hence the level
1014*54fd6939SJiyong Parkof support of the mandatory APIs depends upon the support exported by the
1015*54fd6939SJiyong Parkplatform port as well. The Juno and FVP (all variants) platforms export all the
1016*54fd6939SJiyong Parkrequired support.
1017*54fd6939SJiyong Park
1018*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1019*54fd6939SJiyong Park| PSCI v1.1 API               | Supported   | Comments                      |
1020*54fd6939SJiyong Park+=============================+=============+===============================+
1021*54fd6939SJiyong Park| ``PSCI_VERSION``            | Yes         | The version returned is 1.1   |
1022*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1023*54fd6939SJiyong Park| ``CPU_SUSPEND``             | Yes\*       |                               |
1024*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1025*54fd6939SJiyong Park| ``CPU_OFF``                 | Yes\*       |                               |
1026*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1027*54fd6939SJiyong Park| ``CPU_ON``                  | Yes\*       |                               |
1028*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1029*54fd6939SJiyong Park| ``AFFINITY_INFO``           | Yes         |                               |
1030*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1031*54fd6939SJiyong Park| ``MIGRATE``                 | Yes\*\*     |                               |
1032*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1033*54fd6939SJiyong Park| ``MIGRATE_INFO_TYPE``       | Yes\*\*     |                               |
1034*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1035*54fd6939SJiyong Park| ``MIGRATE_INFO_CPU``        | Yes\*\*     |                               |
1036*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1037*54fd6939SJiyong Park| ``SYSTEM_OFF``              | Yes\*       |                               |
1038*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1039*54fd6939SJiyong Park| ``SYSTEM_RESET``            | Yes\*       |                               |
1040*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1041*54fd6939SJiyong Park| ``PSCI_FEATURES``           | Yes         |                               |
1042*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1043*54fd6939SJiyong Park| ``CPU_FREEZE``              | No          |                               |
1044*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1045*54fd6939SJiyong Park| ``CPU_DEFAULT_SUSPEND``     | No          |                               |
1046*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1047*54fd6939SJiyong Park| ``NODE_HW_STATE``           | Yes\*       |                               |
1048*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1049*54fd6939SJiyong Park| ``SYSTEM_SUSPEND``          | Yes\*       |                               |
1050*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1051*54fd6939SJiyong Park| ``PSCI_SET_SUSPEND_MODE``   | No          |                               |
1052*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1053*54fd6939SJiyong Park| ``PSCI_STAT_RESIDENCY``     | Yes\*       |                               |
1054*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1055*54fd6939SJiyong Park| ``PSCI_STAT_COUNT``         | Yes\*       |                               |
1056*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1057*54fd6939SJiyong Park| ``SYSTEM_RESET2``           | Yes\*       |                               |
1058*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1059*54fd6939SJiyong Park| ``MEM_PROTECT``             | Yes\*       |                               |
1060*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1061*54fd6939SJiyong Park| ``MEM_PROTECT_CHECK_RANGE`` | Yes\*       |                               |
1062*54fd6939SJiyong Park+-----------------------------+-------------+-------------------------------+
1063*54fd6939SJiyong Park
1064*54fd6939SJiyong Park\*Note : These PSCI APIs require platform power management hooks to be
1065*54fd6939SJiyong Parkregistered with the generic PSCI code to be supported.
1066*54fd6939SJiyong Park
1067*54fd6939SJiyong Park\*\*Note : These PSCI APIs require appropriate Secure Payload Dispatcher
1068*54fd6939SJiyong Parkhooks to be registered with the generic PSCI code to be supported.
1069*54fd6939SJiyong Park
1070*54fd6939SJiyong ParkThe PSCI implementation in TF-A is a library which can be integrated with
1071*54fd6939SJiyong ParkAArch64 or AArch32 EL3 Runtime Software for Armv8-A systems. A guide to
1072*54fd6939SJiyong Parkintegrating PSCI library with AArch32 EL3 Runtime Software can be found
1073*54fd6939SJiyong Parkat :ref:`PSCI Library Integration guide for Armv8-A AArch32 systems`.
1074*54fd6939SJiyong Park
1075*54fd6939SJiyong Park.. _firmware_design_sel1_spd:
1076*54fd6939SJiyong Park
1077*54fd6939SJiyong ParkSecure-EL1 Payloads and Dispatchers
1078*54fd6939SJiyong Park-----------------------------------
1079*54fd6939SJiyong Park
1080*54fd6939SJiyong ParkOn a production system that includes a Trusted OS running in Secure-EL1/EL0,
1081*54fd6939SJiyong Parkthe Trusted OS is coupled with a companion runtime service in the BL31
1082*54fd6939SJiyong Parkfirmware. This service is responsible for the initialisation of the Trusted
1083*54fd6939SJiyong ParkOS and all communications with it. The Trusted OS is the BL32 stage of the
1084*54fd6939SJiyong Parkboot flow in TF-A. The firmware will attempt to locate, load and execute a
1085*54fd6939SJiyong ParkBL32 image.
1086*54fd6939SJiyong Park
1087*54fd6939SJiyong ParkTF-A uses a more general term for the BL32 software that runs at Secure-EL1 -
1088*54fd6939SJiyong Parkthe *Secure-EL1 Payload* - as it is not always a Trusted OS.
1089*54fd6939SJiyong Park
1090*54fd6939SJiyong ParkTF-A provides a Test Secure-EL1 Payload (TSP) and a Test Secure-EL1 Payload
1091*54fd6939SJiyong ParkDispatcher (TSPD) service as an example of how a Trusted OS is supported on a
1092*54fd6939SJiyong Parkproduction system using the Runtime Services Framework. On such a system, the
1093*54fd6939SJiyong ParkTest BL32 image and service are replaced by the Trusted OS and its dispatcher
1094*54fd6939SJiyong Parkservice. The TF-A build system expects that the dispatcher will define the
1095*54fd6939SJiyong Parkbuild flag ``NEED_BL32`` to enable it to include the BL32 in the build either
1096*54fd6939SJiyong Parkas a binary or to compile from source depending on whether the ``BL32`` build
1097*54fd6939SJiyong Parkoption is specified or not.
1098*54fd6939SJiyong Park
1099*54fd6939SJiyong ParkThe TSP runs in Secure-EL1. It is designed to demonstrate synchronous
1100*54fd6939SJiyong Parkcommunication with the normal-world software running in EL1/EL2. Communication
1101*54fd6939SJiyong Parkis initiated by the normal-world software
1102*54fd6939SJiyong Park
1103*54fd6939SJiyong Park-  either directly through a Fast SMC (as defined in the `SMCCC`_)
1104*54fd6939SJiyong Park
1105*54fd6939SJiyong Park-  or indirectly through a `PSCI`_ SMC. The `PSCI`_ implementation in turn
1106*54fd6939SJiyong Park   informs the TSPD about the requested power management operation. This allows
1107*54fd6939SJiyong Park   the TSP to prepare for or respond to the power state change
1108*54fd6939SJiyong Park
1109*54fd6939SJiyong ParkThe TSPD service is responsible for.
1110*54fd6939SJiyong Park
1111*54fd6939SJiyong Park-  Initializing the TSP
1112*54fd6939SJiyong Park
1113*54fd6939SJiyong Park-  Routing requests and responses between the secure and the non-secure
1114*54fd6939SJiyong Park   states during the two types of communications just described
1115*54fd6939SJiyong Park
1116*54fd6939SJiyong ParkInitializing a BL32 Image
1117*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~
1118*54fd6939SJiyong Park
1119*54fd6939SJiyong ParkThe Secure-EL1 Payload Dispatcher (SPD) service is responsible for initializing
1120*54fd6939SJiyong Parkthe BL32 image. It needs access to the information passed by BL2 to BL31 to do
1121*54fd6939SJiyong Parkso. This is provided by:
1122*54fd6939SJiyong Park
1123*54fd6939SJiyong Park.. code:: c
1124*54fd6939SJiyong Park
1125*54fd6939SJiyong Park    entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t);
1126*54fd6939SJiyong Park
1127*54fd6939SJiyong Parkwhich returns a reference to the ``entry_point_info`` structure corresponding to
1128*54fd6939SJiyong Parkthe image which will be run in the specified security state. The SPD uses this
1129*54fd6939SJiyong ParkAPI to get entry point information for the SECURE image, BL32.
1130*54fd6939SJiyong Park
1131*54fd6939SJiyong ParkIn the absence of a BL32 image, BL31 passes control to the normal world
1132*54fd6939SJiyong Parkbootloader image (BL33). When the BL32 image is present, it is typical
1133*54fd6939SJiyong Parkthat the SPD wants control to be passed to BL32 first and then later to BL33.
1134*54fd6939SJiyong Park
1135*54fd6939SJiyong ParkTo do this the SPD has to register a BL32 initialization function during
1136*54fd6939SJiyong Parkinitialization of the SPD service. The BL32 initialization function has this
1137*54fd6939SJiyong Parkprototype:
1138*54fd6939SJiyong Park
1139*54fd6939SJiyong Park.. code:: c
1140*54fd6939SJiyong Park
1141*54fd6939SJiyong Park    int32_t init(void);
1142*54fd6939SJiyong Park
1143*54fd6939SJiyong Parkand is registered using the ``bl31_register_bl32_init()`` function.
1144*54fd6939SJiyong Park
1145*54fd6939SJiyong ParkTF-A supports two approaches for the SPD to pass control to BL32 before
1146*54fd6939SJiyong Parkreturning through EL3 and running the non-trusted firmware (BL33):
1147*54fd6939SJiyong Park
1148*54fd6939SJiyong Park#. In the BL32 setup function, use ``bl31_set_next_image_type()`` to
1149*54fd6939SJiyong Park   request that the exit from ``bl31_main()`` is to the BL32 entrypoint in
1150*54fd6939SJiyong Park   Secure-EL1. BL31 will exit to BL32 using the asynchronous method by
1151*54fd6939SJiyong Park   calling ``bl31_prepare_next_image_entry()`` and ``el3_exit()``.
1152*54fd6939SJiyong Park
1153*54fd6939SJiyong Park   When the BL32 has completed initialization at Secure-EL1, it returns to
1154*54fd6939SJiyong Park   BL31 by issuing an SMC, using a Function ID allocated to the SPD. On
1155*54fd6939SJiyong Park   receipt of this SMC, the SPD service handler should switch the CPU context
1156*54fd6939SJiyong Park   from trusted to normal world and use the ``bl31_set_next_image_type()`` and
1157*54fd6939SJiyong Park   ``bl31_prepare_next_image_entry()`` functions to set up the initial return to
1158*54fd6939SJiyong Park   the normal world firmware BL33. On return from the handler the framework
1159*54fd6939SJiyong Park   will exit to EL2 and run BL33.
1160*54fd6939SJiyong Park
1161*54fd6939SJiyong Park#. The BL32 setup function registers an initialization function using
1162*54fd6939SJiyong Park   ``bl31_register_bl32_init()`` which provides a SPD-defined mechanism to
1163*54fd6939SJiyong Park   invoke a 'world-switch synchronous call' to Secure-EL1 to run the BL32
1164*54fd6939SJiyong Park   entrypoint.
1165*54fd6939SJiyong Park
1166*54fd6939SJiyong Park   .. note::
1167*54fd6939SJiyong Park      The Test SPD service included with TF-A provides one implementation
1168*54fd6939SJiyong Park      of such a mechanism.
1169*54fd6939SJiyong Park
1170*54fd6939SJiyong Park   On completion BL32 returns control to BL31 via a SMC, and on receipt the
1171*54fd6939SJiyong Park   SPD service handler invokes the synchronous call return mechanism to return
1172*54fd6939SJiyong Park   to the BL32 initialization function. On return from this function,
1173*54fd6939SJiyong Park   ``bl31_main()`` will set up the return to the normal world firmware BL33 and
1174*54fd6939SJiyong Park   continue the boot process in the normal world.
1175*54fd6939SJiyong Park
1176*54fd6939SJiyong ParkCrash Reporting in BL31
1177*54fd6939SJiyong Park-----------------------
1178*54fd6939SJiyong Park
1179*54fd6939SJiyong ParkBL31 implements a scheme for reporting the processor state when an unhandled
1180*54fd6939SJiyong Parkexception is encountered. The reporting mechanism attempts to preserve all the
1181*54fd6939SJiyong Parkregister contents and report it via a dedicated UART (PL011 console). BL31
1182*54fd6939SJiyong Parkreports the general purpose, EL3, Secure EL1 and some EL2 state registers.
1183*54fd6939SJiyong Park
1184*54fd6939SJiyong ParkA dedicated per-CPU crash stack is maintained by BL31 and this is retrieved via
1185*54fd6939SJiyong Parkthe per-CPU pointer cache. The implementation attempts to minimise the memory
1186*54fd6939SJiyong Parkrequired for this feature. The file ``crash_reporting.S`` contains the
1187*54fd6939SJiyong Parkimplementation for crash reporting.
1188*54fd6939SJiyong Park
1189*54fd6939SJiyong ParkThe sample crash output is shown below.
1190*54fd6939SJiyong Park
1191*54fd6939SJiyong Park::
1192*54fd6939SJiyong Park
1193*54fd6939SJiyong Park    x0             = 0x000000002a4a0000
1194*54fd6939SJiyong Park    x1             = 0x0000000000000001
1195*54fd6939SJiyong Park    x2             = 0x0000000000000002
1196*54fd6939SJiyong Park    x3             = 0x0000000000000003
1197*54fd6939SJiyong Park    x4             = 0x0000000000000004
1198*54fd6939SJiyong Park    x5             = 0x0000000000000005
1199*54fd6939SJiyong Park    x6             = 0x0000000000000006
1200*54fd6939SJiyong Park    x7             = 0x0000000000000007
1201*54fd6939SJiyong Park    x8             = 0x0000000000000008
1202*54fd6939SJiyong Park    x9             = 0x0000000000000009
1203*54fd6939SJiyong Park    x10            = 0x0000000000000010
1204*54fd6939SJiyong Park    x11            = 0x0000000000000011
1205*54fd6939SJiyong Park    x12            = 0x0000000000000012
1206*54fd6939SJiyong Park    x13            = 0x0000000000000013
1207*54fd6939SJiyong Park    x14            = 0x0000000000000014
1208*54fd6939SJiyong Park    x15            = 0x0000000000000015
1209*54fd6939SJiyong Park    x16            = 0x0000000000000016
1210*54fd6939SJiyong Park    x17            = 0x0000000000000017
1211*54fd6939SJiyong Park    x18            = 0x0000000000000018
1212*54fd6939SJiyong Park    x19            = 0x0000000000000019
1213*54fd6939SJiyong Park    x20            = 0x0000000000000020
1214*54fd6939SJiyong Park    x21            = 0x0000000000000021
1215*54fd6939SJiyong Park    x22            = 0x0000000000000022
1216*54fd6939SJiyong Park    x23            = 0x0000000000000023
1217*54fd6939SJiyong Park    x24            = 0x0000000000000024
1218*54fd6939SJiyong Park    x25            = 0x0000000000000025
1219*54fd6939SJiyong Park    x26            = 0x0000000000000026
1220*54fd6939SJiyong Park    x27            = 0x0000000000000027
1221*54fd6939SJiyong Park    x28            = 0x0000000000000028
1222*54fd6939SJiyong Park    x29            = 0x0000000000000029
1223*54fd6939SJiyong Park    x30            = 0x0000000088000b78
1224*54fd6939SJiyong Park    scr_el3        = 0x000000000003073d
1225*54fd6939SJiyong Park    sctlr_el3      = 0x00000000b0cd183f
1226*54fd6939SJiyong Park    cptr_el3       = 0x0000000000000000
1227*54fd6939SJiyong Park    tcr_el3        = 0x000000008080351c
1228*54fd6939SJiyong Park    daif           = 0x00000000000002c0
1229*54fd6939SJiyong Park    mair_el3       = 0x00000000004404ff
1230*54fd6939SJiyong Park    spsr_el3       = 0x0000000060000349
1231*54fd6939SJiyong Park    elr_el3        = 0x0000000088000114
1232*54fd6939SJiyong Park    ttbr0_el3      = 0x0000000004018201
1233*54fd6939SJiyong Park    esr_el3        = 0x00000000be000000
1234*54fd6939SJiyong Park    far_el3        = 0x0000000000000000
1235*54fd6939SJiyong Park    spsr_el1       = 0x0000000000000000
1236*54fd6939SJiyong Park    elr_el1        = 0x0000000000000000
1237*54fd6939SJiyong Park    spsr_abt       = 0x0000000000000000
1238*54fd6939SJiyong Park    spsr_und       = 0x0000000000000000
1239*54fd6939SJiyong Park    spsr_irq       = 0x0000000000000000
1240*54fd6939SJiyong Park    spsr_fiq       = 0x0000000000000000
1241*54fd6939SJiyong Park    sctlr_el1      = 0x0000000030d00800
1242*54fd6939SJiyong Park    actlr_el1      = 0x0000000000000000
1243*54fd6939SJiyong Park    cpacr_el1      = 0x0000000000000000
1244*54fd6939SJiyong Park    csselr_el1     = 0x0000000000000000
1245*54fd6939SJiyong Park    sp_el1         = 0x0000000000000000
1246*54fd6939SJiyong Park    esr_el1        = 0x0000000000000000
1247*54fd6939SJiyong Park    ttbr0_el1      = 0x0000000000000000
1248*54fd6939SJiyong Park    ttbr1_el1      = 0x0000000000000000
1249*54fd6939SJiyong Park    mair_el1       = 0x0000000000000000
1250*54fd6939SJiyong Park    amair_el1      = 0x0000000000000000
1251*54fd6939SJiyong Park    tcr_el1        = 0x0000000000000000
1252*54fd6939SJiyong Park    tpidr_el1      = 0x0000000000000000
1253*54fd6939SJiyong Park    tpidr_el0      = 0x0000000000000000
1254*54fd6939SJiyong Park    tpidrro_el0    = 0x0000000000000000
1255*54fd6939SJiyong Park    par_el1        = 0x0000000000000000
1256*54fd6939SJiyong Park    mpidr_el1      = 0x0000000080000000
1257*54fd6939SJiyong Park    afsr0_el1      = 0x0000000000000000
1258*54fd6939SJiyong Park    afsr1_el1      = 0x0000000000000000
1259*54fd6939SJiyong Park    contextidr_el1 = 0x0000000000000000
1260*54fd6939SJiyong Park    vbar_el1       = 0x0000000000000000
1261*54fd6939SJiyong Park    cntp_ctl_el0   = 0x0000000000000000
1262*54fd6939SJiyong Park    cntp_cval_el0  = 0x0000000000000000
1263*54fd6939SJiyong Park    cntv_ctl_el0   = 0x0000000000000000
1264*54fd6939SJiyong Park    cntv_cval_el0  = 0x0000000000000000
1265*54fd6939SJiyong Park    cntkctl_el1    = 0x0000000000000000
1266*54fd6939SJiyong Park    sp_el0         = 0x0000000004014940
1267*54fd6939SJiyong Park    isr_el1        = 0x0000000000000000
1268*54fd6939SJiyong Park    dacr32_el2     = 0x0000000000000000
1269*54fd6939SJiyong Park    ifsr32_el2     = 0x0000000000000000
1270*54fd6939SJiyong Park    icc_hppir0_el1 = 0x00000000000003ff
1271*54fd6939SJiyong Park    icc_hppir1_el1 = 0x00000000000003ff
1272*54fd6939SJiyong Park    icc_ctlr_el3   = 0x0000000000080400
1273*54fd6939SJiyong Park    gicd_ispendr regs (Offsets 0x200-0x278)
1274*54fd6939SJiyong Park    Offset		    Value
1275*54fd6939SJiyong Park    0x200:	     0x0000000000000000
1276*54fd6939SJiyong Park    0x208:	     0x0000000000000000
1277*54fd6939SJiyong Park    0x210:	     0x0000000000000000
1278*54fd6939SJiyong Park    0x218:	     0x0000000000000000
1279*54fd6939SJiyong Park    0x220:	     0x0000000000000000
1280*54fd6939SJiyong Park    0x228:	     0x0000000000000000
1281*54fd6939SJiyong Park    0x230:	     0x0000000000000000
1282*54fd6939SJiyong Park    0x238:	     0x0000000000000000
1283*54fd6939SJiyong Park    0x240:	     0x0000000000000000
1284*54fd6939SJiyong Park    0x248:	     0x0000000000000000
1285*54fd6939SJiyong Park    0x250:	     0x0000000000000000
1286*54fd6939SJiyong Park    0x258:	     0x0000000000000000
1287*54fd6939SJiyong Park    0x260:	     0x0000000000000000
1288*54fd6939SJiyong Park    0x268:	     0x0000000000000000
1289*54fd6939SJiyong Park    0x270:	     0x0000000000000000
1290*54fd6939SJiyong Park    0x278:	     0x0000000000000000
1291*54fd6939SJiyong Park
1292*54fd6939SJiyong ParkGuidelines for Reset Handlers
1293*54fd6939SJiyong Park-----------------------------
1294*54fd6939SJiyong Park
1295*54fd6939SJiyong ParkTF-A implements a framework that allows CPU and platform ports to perform
1296*54fd6939SJiyong Parkactions very early after a CPU is released from reset in both the cold and warm
1297*54fd6939SJiyong Parkboot paths. This is done by calling the ``reset_handler()`` function in both
1298*54fd6939SJiyong Parkthe BL1 and BL31 images. It in turn calls the platform and CPU specific reset
1299*54fd6939SJiyong Parkhandling functions.
1300*54fd6939SJiyong Park
1301*54fd6939SJiyong ParkDetails for implementing a CPU specific reset handler can be found in
1302*54fd6939SJiyong ParkSection 8. Details for implementing a platform specific reset handler can be
1303*54fd6939SJiyong Parkfound in the :ref:`Porting Guide` (see the ``plat_reset_handler()`` function).
1304*54fd6939SJiyong Park
1305*54fd6939SJiyong ParkWhen adding functionality to a reset handler, keep in mind that if a different
1306*54fd6939SJiyong Parkreset handling behavior is required between the first and the subsequent
1307*54fd6939SJiyong Parkinvocations of the reset handling code, this should be detected at runtime.
1308*54fd6939SJiyong ParkIn other words, the reset handler should be able to detect whether an action has
1309*54fd6939SJiyong Parkalready been performed and act as appropriate. Possible courses of actions are,
1310*54fd6939SJiyong Parke.g. skip the action the second time, or undo/redo it.
1311*54fd6939SJiyong Park
1312*54fd6939SJiyong Park.. _configuring-secure-interrupts:
1313*54fd6939SJiyong Park
1314*54fd6939SJiyong ParkConfiguring secure interrupts
1315*54fd6939SJiyong Park-----------------------------
1316*54fd6939SJiyong Park
1317*54fd6939SJiyong ParkThe GIC driver is responsible for performing initial configuration of secure
1318*54fd6939SJiyong Parkinterrupts on the platform. To this end, the platform is expected to provide the
1319*54fd6939SJiyong ParkGIC driver (either GICv2 or GICv3, as selected by the platform) with the
1320*54fd6939SJiyong Parkinterrupt configuration during the driver initialisation.
1321*54fd6939SJiyong Park
1322*54fd6939SJiyong ParkSecure interrupt configuration are specified in an array of secure interrupt
1323*54fd6939SJiyong Parkproperties. In this scheme, in both GICv2 and GICv3 driver data structures, the
1324*54fd6939SJiyong Park``interrupt_props`` member points to an array of interrupt properties. Each
1325*54fd6939SJiyong Parkelement of the array specifies the interrupt number and its attributes
1326*54fd6939SJiyong Park(priority, group, configuration). Each element of the array shall be populated
1327*54fd6939SJiyong Parkby the macro ``INTR_PROP_DESC()``. The macro takes the following arguments:
1328*54fd6939SJiyong Park
1329*54fd6939SJiyong Park- 10-bit interrupt number,
1330*54fd6939SJiyong Park
1331*54fd6939SJiyong Park- 8-bit interrupt priority,
1332*54fd6939SJiyong Park
1333*54fd6939SJiyong Park- Interrupt type (one of ``INTR_TYPE_EL3``, ``INTR_TYPE_S_EL1``,
1334*54fd6939SJiyong Park  ``INTR_TYPE_NS``),
1335*54fd6939SJiyong Park
1336*54fd6939SJiyong Park- Interrupt configuration (either ``GIC_INTR_CFG_LEVEL`` or
1337*54fd6939SJiyong Park  ``GIC_INTR_CFG_EDGE``).
1338*54fd6939SJiyong Park
1339*54fd6939SJiyong Park.. _firmware_design_cpu_ops_fwk:
1340*54fd6939SJiyong Park
1341*54fd6939SJiyong ParkCPU specific operations framework
1342*54fd6939SJiyong Park---------------------------------
1343*54fd6939SJiyong Park
1344*54fd6939SJiyong ParkCertain aspects of the Armv8-A architecture are implementation defined,
1345*54fd6939SJiyong Parkthat is, certain behaviours are not architecturally defined, but must be
1346*54fd6939SJiyong Parkdefined and documented by individual processor implementations. TF-A
1347*54fd6939SJiyong Parkimplements a framework which categorises the common implementation defined
1348*54fd6939SJiyong Parkbehaviours and allows a processor to export its implementation of that
1349*54fd6939SJiyong Parkbehaviour. The categories are:
1350*54fd6939SJiyong Park
1351*54fd6939SJiyong Park#. Processor specific reset sequence.
1352*54fd6939SJiyong Park
1353*54fd6939SJiyong Park#. Processor specific power down sequences.
1354*54fd6939SJiyong Park
1355*54fd6939SJiyong Park#. Processor specific register dumping as a part of crash reporting.
1356*54fd6939SJiyong Park
1357*54fd6939SJiyong Park#. Errata status reporting.
1358*54fd6939SJiyong Park
1359*54fd6939SJiyong ParkEach of the above categories fulfils a different requirement.
1360*54fd6939SJiyong Park
1361*54fd6939SJiyong Park#. allows any processor specific initialization before the caches and MMU
1362*54fd6939SJiyong Park   are turned on, like implementation of errata workarounds, entry into
1363*54fd6939SJiyong Park   the intra-cluster coherency domain etc.
1364*54fd6939SJiyong Park
1365*54fd6939SJiyong Park#. allows each processor to implement the power down sequence mandated in
1366*54fd6939SJiyong Park   its Technical Reference Manual (TRM).
1367*54fd6939SJiyong Park
1368*54fd6939SJiyong Park#. allows a processor to provide additional information to the developer
1369*54fd6939SJiyong Park   in the event of a crash, for example Cortex-A53 has registers which
1370*54fd6939SJiyong Park   can expose the data cache contents.
1371*54fd6939SJiyong Park
1372*54fd6939SJiyong Park#. allows a processor to define a function that inspects and reports the status
1373*54fd6939SJiyong Park   of all errata workarounds on that processor.
1374*54fd6939SJiyong Park
1375*54fd6939SJiyong ParkPlease note that only 2. is mandated by the TRM.
1376*54fd6939SJiyong Park
1377*54fd6939SJiyong ParkThe CPU specific operations framework scales to accommodate a large number of
1378*54fd6939SJiyong Parkdifferent CPUs during power down and reset handling. The platform can specify
1379*54fd6939SJiyong Parkany CPU optimization it wants to enable for each CPU. It can also specify
1380*54fd6939SJiyong Parkthe CPU errata workarounds to be applied for each CPU type during reset
1381*54fd6939SJiyong Parkhandling by defining CPU errata compile time macros. Details on these macros
1382*54fd6939SJiyong Parkcan be found in the :ref:`Arm CPU Specific Build Macros` document.
1383*54fd6939SJiyong Park
1384*54fd6939SJiyong ParkThe CPU specific operations framework depends on the ``cpu_ops`` structure which
1385*54fd6939SJiyong Parkneeds to be exported for each type of CPU in the platform. It is defined in
1386*54fd6939SJiyong Park``include/lib/cpus/aarch64/cpu_macros.S`` and has the following fields : ``midr``,
1387*54fd6939SJiyong Park``reset_func()``, ``cpu_pwr_down_ops`` (array of power down functions) and
1388*54fd6939SJiyong Park``cpu_reg_dump()``.
1389*54fd6939SJiyong Park
1390*54fd6939SJiyong ParkThe CPU specific files in ``lib/cpus`` export a ``cpu_ops`` data structure with
1391*54fd6939SJiyong Parksuitable handlers for that CPU. For example, ``lib/cpus/aarch64/cortex_a53.S``
1392*54fd6939SJiyong Parkexports the ``cpu_ops`` for Cortex-A53 CPU. According to the platform
1393*54fd6939SJiyong Parkconfiguration, these CPU specific files must be included in the build by
1394*54fd6939SJiyong Parkthe platform makefile. The generic CPU specific operations framework code exists
1395*54fd6939SJiyong Parkin ``lib/cpus/aarch64/cpu_helpers.S``.
1396*54fd6939SJiyong Park
1397*54fd6939SJiyong ParkCPU specific Reset Handling
1398*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~
1399*54fd6939SJiyong Park
1400*54fd6939SJiyong ParkAfter a reset, the state of the CPU when it calls generic reset handler is:
1401*54fd6939SJiyong ParkMMU turned off, both instruction and data caches turned off and not part
1402*54fd6939SJiyong Parkof any coherency domain.
1403*54fd6939SJiyong Park
1404*54fd6939SJiyong ParkThe BL entrypoint code first invokes the ``plat_reset_handler()`` to allow
1405*54fd6939SJiyong Parkthe platform to perform any system initialization required and any system
1406*54fd6939SJiyong Parkerrata workarounds that needs to be applied. The ``get_cpu_ops_ptr()`` reads
1407*54fd6939SJiyong Parkthe current CPU midr, finds the matching ``cpu_ops`` entry in the ``cpu_ops``
1408*54fd6939SJiyong Parkarray and returns it. Note that only the part number and implementer fields
1409*54fd6939SJiyong Parkin midr are used to find the matching ``cpu_ops`` entry. The ``reset_func()`` in
1410*54fd6939SJiyong Parkthe returned ``cpu_ops`` is then invoked which executes the required reset
1411*54fd6939SJiyong Parkhandling for that CPU and also any errata workarounds enabled by the platform.
1412*54fd6939SJiyong ParkThis function must preserve the values of general purpose registers x20 to x29.
1413*54fd6939SJiyong Park
1414*54fd6939SJiyong ParkRefer to Section "Guidelines for Reset Handlers" for general guidelines
1415*54fd6939SJiyong Parkregarding placement of code in a reset handler.
1416*54fd6939SJiyong Park
1417*54fd6939SJiyong ParkCPU specific power down sequence
1418*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1419*54fd6939SJiyong Park
1420*54fd6939SJiyong ParkDuring the BL31 initialization sequence, the pointer to the matching ``cpu_ops``
1421*54fd6939SJiyong Parkentry is stored in per-CPU data by ``init_cpu_ops()`` so that it can be quickly
1422*54fd6939SJiyong Parkretrieved during power down sequences.
1423*54fd6939SJiyong Park
1424*54fd6939SJiyong ParkVarious CPU drivers register handlers to perform power down at certain power
1425*54fd6939SJiyong Parklevels for that specific CPU. The PSCI service, upon receiving a power down
1426*54fd6939SJiyong Parkrequest, determines the highest power level at which to execute power down
1427*54fd6939SJiyong Parksequence for a particular CPU. It uses the ``prepare_cpu_pwr_dwn()`` function to
1428*54fd6939SJiyong Parkpick the right power down handler for the requested level. The function
1429*54fd6939SJiyong Parkretrieves ``cpu_ops`` pointer member of per-CPU data, and from that, further
1430*54fd6939SJiyong Parkretrieves ``cpu_pwr_down_ops`` array, and indexes into the required level. If the
1431*54fd6939SJiyong Parkrequested power level is higher than what a CPU driver supports, the handler
1432*54fd6939SJiyong Parkregistered for highest level is invoked.
1433*54fd6939SJiyong Park
1434*54fd6939SJiyong ParkAt runtime the platform hooks for power down are invoked by the PSCI service to
1435*54fd6939SJiyong Parkperform platform specific operations during a power down sequence, for example
1436*54fd6939SJiyong Parkturning off CCI coherency during a cluster power down.
1437*54fd6939SJiyong Park
1438*54fd6939SJiyong ParkCPU specific register reporting during crash
1439*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1440*54fd6939SJiyong Park
1441*54fd6939SJiyong ParkIf the crash reporting is enabled in BL31, when a crash occurs, the crash
1442*54fd6939SJiyong Parkreporting framework calls ``do_cpu_reg_dump`` which retrieves the matching
1443*54fd6939SJiyong Park``cpu_ops`` using ``get_cpu_ops_ptr()`` function. The ``cpu_reg_dump()`` in
1444*54fd6939SJiyong Park``cpu_ops`` is invoked, which then returns the CPU specific register values to
1445*54fd6939SJiyong Parkbe reported and a pointer to the ASCII list of register names in a format
1446*54fd6939SJiyong Parkexpected by the crash reporting framework.
1447*54fd6939SJiyong Park
1448*54fd6939SJiyong Park.. _firmware_design_cpu_errata_reporting:
1449*54fd6939SJiyong Park
1450*54fd6939SJiyong ParkCPU errata status reporting
1451*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~
1452*54fd6939SJiyong Park
1453*54fd6939SJiyong ParkErrata workarounds for CPUs supported in TF-A are applied during both cold and
1454*54fd6939SJiyong Parkwarm boots, shortly after reset. Individual Errata workarounds are enabled as
1455*54fd6939SJiyong Parkbuild options. Some errata workarounds have potential run-time implications;
1456*54fd6939SJiyong Parktherefore some are enabled by default, others not. Platform ports shall
1457*54fd6939SJiyong Parkoverride build options to enable or disable errata as appropriate. The CPU
1458*54fd6939SJiyong Parkdrivers take care of applying errata workarounds that are enabled and applicable
1459*54fd6939SJiyong Parkto a given CPU. Refer to :ref:`arm_cpu_macros_errata_workarounds` for more
1460*54fd6939SJiyong Parkinformation.
1461*54fd6939SJiyong Park
1462*54fd6939SJiyong ParkFunctions in CPU drivers that apply errata workaround must follow the
1463*54fd6939SJiyong Parkconventions listed below.
1464*54fd6939SJiyong Park
1465*54fd6939SJiyong ParkThe errata workaround must be authored as two separate functions:
1466*54fd6939SJiyong Park
1467*54fd6939SJiyong Park-  One that checks for errata. This function must determine whether that errata
1468*54fd6939SJiyong Park   applies to the current CPU. Typically this involves matching the current
1469*54fd6939SJiyong Park   CPUs revision and variant against a value that's known to be affected by the
1470*54fd6939SJiyong Park   errata. If the function determines that the errata applies to this CPU, it
1471*54fd6939SJiyong Park   must return ``ERRATA_APPLIES``; otherwise, it must return
1472*54fd6939SJiyong Park   ``ERRATA_NOT_APPLIES``. The utility functions ``cpu_get_rev_var`` and
1473*54fd6939SJiyong Park   ``cpu_rev_var_ls`` functions may come in handy for this purpose.
1474*54fd6939SJiyong Park
1475*54fd6939SJiyong ParkFor an errata identified as ``E``, the check function must be named
1476*54fd6939SJiyong Park``check_errata_E``.
1477*54fd6939SJiyong Park
1478*54fd6939SJiyong ParkThis function will be invoked at different times, both from assembly and from
1479*54fd6939SJiyong ParkC run time. Therefore it must follow AAPCS, and must not use stack.
1480*54fd6939SJiyong Park
1481*54fd6939SJiyong Park-  Another one that applies the errata workaround. This function would call the
1482*54fd6939SJiyong Park   check function described above, and applies errata workaround if required.
1483*54fd6939SJiyong Park
1484*54fd6939SJiyong ParkCPU drivers that apply errata workaround can optionally implement an assembly
1485*54fd6939SJiyong Parkfunction that report the status of errata workarounds pertaining to that CPU.
1486*54fd6939SJiyong ParkFor a driver that registers the CPU, for example, ``cpux`` via ``declare_cpu_ops``
1487*54fd6939SJiyong Parkmacro, the errata reporting function, if it exists, must be named
1488*54fd6939SJiyong Park``cpux_errata_report``. This function will always be called with MMU enabled; it
1489*54fd6939SJiyong Parkmust follow AAPCS and may use stack.
1490*54fd6939SJiyong Park
1491*54fd6939SJiyong ParkIn a debug build of TF-A, on a CPU that comes out of reset, both BL1 and the
1492*54fd6939SJiyong Parkruntime firmware (BL31 in AArch64, and BL32 in AArch32) will invoke errata
1493*54fd6939SJiyong Parkstatus reporting function, if one exists, for that type of CPU.
1494*54fd6939SJiyong Park
1495*54fd6939SJiyong ParkTo report the status of each errata workaround, the function shall use the
1496*54fd6939SJiyong Parkassembler macro ``report_errata``, passing it:
1497*54fd6939SJiyong Park
1498*54fd6939SJiyong Park-  The build option that enables the errata;
1499*54fd6939SJiyong Park
1500*54fd6939SJiyong Park-  The name of the CPU: this must be the same identifier that CPU driver
1501*54fd6939SJiyong Park   registered itself with, using ``declare_cpu_ops``;
1502*54fd6939SJiyong Park
1503*54fd6939SJiyong Park-  And the errata identifier: the identifier must match what's used in the
1504*54fd6939SJiyong Park   errata's check function described above.
1505*54fd6939SJiyong Park
1506*54fd6939SJiyong ParkThe errata status reporting function will be called once per CPU type/errata
1507*54fd6939SJiyong Parkcombination during the software's active life time.
1508*54fd6939SJiyong Park
1509*54fd6939SJiyong ParkIt's expected that whenever an errata workaround is submitted to TF-A, the
1510*54fd6939SJiyong Parkerrata reporting function is appropriately extended to report its status as
1511*54fd6939SJiyong Parkwell.
1512*54fd6939SJiyong Park
1513*54fd6939SJiyong ParkReporting the status of errata workaround is for informational purpose only; it
1514*54fd6939SJiyong Parkhas no functional significance.
1515*54fd6939SJiyong Park
1516*54fd6939SJiyong ParkMemory layout of BL images
1517*54fd6939SJiyong Park--------------------------
1518*54fd6939SJiyong Park
1519*54fd6939SJiyong ParkEach bootloader image can be divided in 2 parts:
1520*54fd6939SJiyong Park
1521*54fd6939SJiyong Park-  the static contents of the image. These are data actually stored in the
1522*54fd6939SJiyong Park   binary on the disk. In the ELF terminology, they are called ``PROGBITS``
1523*54fd6939SJiyong Park   sections;
1524*54fd6939SJiyong Park
1525*54fd6939SJiyong Park-  the run-time contents of the image. These are data that don't occupy any
1526*54fd6939SJiyong Park   space in the binary on the disk. The ELF binary just contains some
1527*54fd6939SJiyong Park   metadata indicating where these data will be stored at run-time and the
1528*54fd6939SJiyong Park   corresponding sections need to be allocated and initialized at run-time.
1529*54fd6939SJiyong Park   In the ELF terminology, they are called ``NOBITS`` sections.
1530*54fd6939SJiyong Park
1531*54fd6939SJiyong ParkAll PROGBITS sections are grouped together at the beginning of the image,
1532*54fd6939SJiyong Parkfollowed by all NOBITS sections. This is true for all TF-A images and it is
1533*54fd6939SJiyong Parkgoverned by the linker scripts. This ensures that the raw binary images are
1534*54fd6939SJiyong Parkas small as possible. If a NOBITS section was inserted in between PROGBITS
1535*54fd6939SJiyong Parksections then the resulting binary file would contain zero bytes in place of
1536*54fd6939SJiyong Parkthis NOBITS section, making the image unnecessarily bigger. Smaller images
1537*54fd6939SJiyong Parkallow faster loading from the FIP to the main memory.
1538*54fd6939SJiyong Park
1539*54fd6939SJiyong ParkFor BL31, a platform can specify an alternate location for NOBITS sections
1540*54fd6939SJiyong Park(other than immediately following PROGBITS sections) by setting
1541*54fd6939SJiyong Park``SEPARATE_NOBITS_REGION`` to 1 and defining ``BL31_NOBITS_BASE`` and
1542*54fd6939SJiyong Park``BL31_NOBITS_LIMIT``.
1543*54fd6939SJiyong Park
1544*54fd6939SJiyong ParkLinker scripts and symbols
1545*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~
1546*54fd6939SJiyong Park
1547*54fd6939SJiyong ParkEach bootloader stage image layout is described by its own linker script. The
1548*54fd6939SJiyong Parklinker scripts export some symbols into the program symbol table. Their values
1549*54fd6939SJiyong Parkcorrespond to particular addresses. TF-A code can refer to these symbols to
1550*54fd6939SJiyong Parkfigure out the image memory layout.
1551*54fd6939SJiyong Park
1552*54fd6939SJiyong ParkLinker symbols follow the following naming convention in TF-A.
1553*54fd6939SJiyong Park
1554*54fd6939SJiyong Park-  ``__<SECTION>_START__``
1555*54fd6939SJiyong Park
1556*54fd6939SJiyong Park   Start address of a given section named ``<SECTION>``.
1557*54fd6939SJiyong Park
1558*54fd6939SJiyong Park-  ``__<SECTION>_END__``
1559*54fd6939SJiyong Park
1560*54fd6939SJiyong Park   End address of a given section named ``<SECTION>``. If there is an alignment
1561*54fd6939SJiyong Park   constraint on the section's end address then ``__<SECTION>_END__`` corresponds
1562*54fd6939SJiyong Park   to the end address of the section's actual contents, rounded up to the right
1563*54fd6939SJiyong Park   boundary. Refer to the value of ``__<SECTION>_UNALIGNED_END__`` to know the
1564*54fd6939SJiyong Park   actual end address of the section's contents.
1565*54fd6939SJiyong Park
1566*54fd6939SJiyong Park-  ``__<SECTION>_UNALIGNED_END__``
1567*54fd6939SJiyong Park
1568*54fd6939SJiyong Park   End address of a given section named ``<SECTION>`` without any padding or
1569*54fd6939SJiyong Park   rounding up due to some alignment constraint.
1570*54fd6939SJiyong Park
1571*54fd6939SJiyong Park-  ``__<SECTION>_SIZE__``
1572*54fd6939SJiyong Park
1573*54fd6939SJiyong Park   Size (in bytes) of a given section named ``<SECTION>``. If there is an
1574*54fd6939SJiyong Park   alignment constraint on the section's end address then ``__<SECTION>_SIZE__``
1575*54fd6939SJiyong Park   corresponds to the size of the section's actual contents, rounded up to the
1576*54fd6939SJiyong Park   right boundary. In other words, ``__<SECTION>_SIZE__ = __<SECTION>_END__ - _<SECTION>_START__``. Refer to the value of ``__<SECTION>_UNALIGNED_SIZE__``
1577*54fd6939SJiyong Park   to know the actual size of the section's contents.
1578*54fd6939SJiyong Park
1579*54fd6939SJiyong Park-  ``__<SECTION>_UNALIGNED_SIZE__``
1580*54fd6939SJiyong Park
1581*54fd6939SJiyong Park   Size (in bytes) of a given section named ``<SECTION>`` without any padding or
1582*54fd6939SJiyong Park   rounding up due to some alignment constraint. In other words,
1583*54fd6939SJiyong Park   ``__<SECTION>_UNALIGNED_SIZE__ = __<SECTION>_UNALIGNED_END__ - __<SECTION>_START__``.
1584*54fd6939SJiyong Park
1585*54fd6939SJiyong ParkSome of the linker symbols are mandatory as TF-A code relies on them to be
1586*54fd6939SJiyong Parkdefined. They are listed in the following subsections. Some of them must be
1587*54fd6939SJiyong Parkprovided for each bootloader stage and some are specific to a given bootloader
1588*54fd6939SJiyong Parkstage.
1589*54fd6939SJiyong Park
1590*54fd6939SJiyong ParkThe linker scripts define some extra, optional symbols. They are not actually
1591*54fd6939SJiyong Parkused by any code but they help in understanding the bootloader images' memory
1592*54fd6939SJiyong Parklayout as they are easy to spot in the link map files.
1593*54fd6939SJiyong Park
1594*54fd6939SJiyong ParkCommon linker symbols
1595*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^
1596*54fd6939SJiyong Park
1597*54fd6939SJiyong ParkAll BL images share the following requirements:
1598*54fd6939SJiyong Park
1599*54fd6939SJiyong Park-  The BSS section must be zero-initialised before executing any C code.
1600*54fd6939SJiyong Park-  The coherent memory section (if enabled) must be zero-initialised as well.
1601*54fd6939SJiyong Park-  The MMU setup code needs to know the extents of the coherent and read-only
1602*54fd6939SJiyong Park   memory regions to set the right memory attributes. When
1603*54fd6939SJiyong Park   ``SEPARATE_CODE_AND_RODATA=1``, it needs to know more specifically how the
1604*54fd6939SJiyong Park   read-only memory region is divided between code and data.
1605*54fd6939SJiyong Park
1606*54fd6939SJiyong ParkThe following linker symbols are defined for this purpose:
1607*54fd6939SJiyong Park
1608*54fd6939SJiyong Park-  ``__BSS_START__``
1609*54fd6939SJiyong Park-  ``__BSS_SIZE__``
1610*54fd6939SJiyong Park-  ``__COHERENT_RAM_START__`` Must be aligned on a page-size boundary.
1611*54fd6939SJiyong Park-  ``__COHERENT_RAM_END__`` Must be aligned on a page-size boundary.
1612*54fd6939SJiyong Park-  ``__COHERENT_RAM_UNALIGNED_SIZE__``
1613*54fd6939SJiyong Park-  ``__RO_START__``
1614*54fd6939SJiyong Park-  ``__RO_END__``
1615*54fd6939SJiyong Park-  ``__TEXT_START__``
1616*54fd6939SJiyong Park-  ``__TEXT_END__``
1617*54fd6939SJiyong Park-  ``__RODATA_START__``
1618*54fd6939SJiyong Park-  ``__RODATA_END__``
1619*54fd6939SJiyong Park
1620*54fd6939SJiyong ParkBL1's linker symbols
1621*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^
1622*54fd6939SJiyong Park
1623*54fd6939SJiyong ParkBL1 being the ROM image, it has additional requirements. BL1 resides in ROM and
1624*54fd6939SJiyong Parkit is entirely executed in place but it needs some read-write memory for its
1625*54fd6939SJiyong Parkmutable data. Its ``.data`` section (i.e. its allocated read-write data) must be
1626*54fd6939SJiyong Parkrelocated from ROM to RAM before executing any C code.
1627*54fd6939SJiyong Park
1628*54fd6939SJiyong ParkThe following additional linker symbols are defined for BL1:
1629*54fd6939SJiyong Park
1630*54fd6939SJiyong Park-  ``__BL1_ROM_END__`` End address of BL1's ROM contents, covering its code
1631*54fd6939SJiyong Park   and ``.data`` section in ROM.
1632*54fd6939SJiyong Park-  ``__DATA_ROM_START__`` Start address of the ``.data`` section in ROM. Must be
1633*54fd6939SJiyong Park   aligned on a 16-byte boundary.
1634*54fd6939SJiyong Park-  ``__DATA_RAM_START__`` Address in RAM where the ``.data`` section should be
1635*54fd6939SJiyong Park   copied over. Must be aligned on a 16-byte boundary.
1636*54fd6939SJiyong Park-  ``__DATA_SIZE__`` Size of the ``.data`` section (in ROM or RAM).
1637*54fd6939SJiyong Park-  ``__BL1_RAM_START__`` Start address of BL1 read-write data.
1638*54fd6939SJiyong Park-  ``__BL1_RAM_END__`` End address of BL1 read-write data.
1639*54fd6939SJiyong Park
1640*54fd6939SJiyong ParkHow to choose the right base addresses for each bootloader stage image
1641*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1642*54fd6939SJiyong Park
1643*54fd6939SJiyong ParkThere is currently no support for dynamic image loading in TF-A. This means
1644*54fd6939SJiyong Parkthat all bootloader images need to be linked against their ultimate runtime
1645*54fd6939SJiyong Parklocations and the base addresses of each image must be chosen carefully such
1646*54fd6939SJiyong Parkthat images don't overlap each other in an undesired way. As the code grows,
1647*54fd6939SJiyong Parkthe base addresses might need adjustments to cope with the new memory layout.
1648*54fd6939SJiyong Park
1649*54fd6939SJiyong ParkThe memory layout is completely specific to the platform and so there is no
1650*54fd6939SJiyong Parkgeneral recipe for choosing the right base addresses for each bootloader image.
1651*54fd6939SJiyong ParkHowever, there are tools to aid in understanding the memory layout. These are
1652*54fd6939SJiyong Parkthe link map files: ``build/<platform>/<build-type>/bl<x>/bl<x>.map``, with ``<x>``
1653*54fd6939SJiyong Parkbeing the stage bootloader. They provide a detailed view of the memory usage of
1654*54fd6939SJiyong Parkeach image. Among other useful information, they provide the end address of
1655*54fd6939SJiyong Parkeach image.
1656*54fd6939SJiyong Park
1657*54fd6939SJiyong Park-  ``bl1.map`` link map file provides ``__BL1_RAM_END__`` address.
1658*54fd6939SJiyong Park-  ``bl2.map`` link map file provides ``__BL2_END__`` address.
1659*54fd6939SJiyong Park-  ``bl31.map`` link map file provides ``__BL31_END__`` address.
1660*54fd6939SJiyong Park-  ``bl32.map`` link map file provides ``__BL32_END__`` address.
1661*54fd6939SJiyong Park
1662*54fd6939SJiyong ParkFor each bootloader image, the platform code must provide its start address
1663*54fd6939SJiyong Parkas well as a limit address that it must not overstep. The latter is used in the
1664*54fd6939SJiyong Parklinker scripts to check that the image doesn't grow past that address. If that
1665*54fd6939SJiyong Parkhappens, the linker will issue a message similar to the following:
1666*54fd6939SJiyong Park
1667*54fd6939SJiyong Park::
1668*54fd6939SJiyong Park
1669*54fd6939SJiyong Park    aarch64-none-elf-ld: BLx has exceeded its limit.
1670*54fd6939SJiyong Park
1671*54fd6939SJiyong ParkAdditionally, if the platform memory layout implies some image overlaying like
1672*54fd6939SJiyong Parkon FVP, BL31 and TSP need to know the limit address that their PROGBITS
1673*54fd6939SJiyong Parksections must not overstep. The platform code must provide those.
1674*54fd6939SJiyong Park
1675*54fd6939SJiyong ParkTF-A does not provide any mechanism to verify at boot time that the memory
1676*54fd6939SJiyong Parkto load a new image is free to prevent overwriting a previously loaded image.
1677*54fd6939SJiyong ParkThe platform must specify the memory available in the system for all the
1678*54fd6939SJiyong Parkrelevant BL images to be loaded.
1679*54fd6939SJiyong Park
1680*54fd6939SJiyong ParkFor example, in the case of BL1 loading BL2, ``bl1_plat_sec_mem_layout()`` will
1681*54fd6939SJiyong Parkreturn the region defined by the platform where BL1 intends to load BL2. The
1682*54fd6939SJiyong Park``load_image()`` function performs bounds check for the image size based on the
1683*54fd6939SJiyong Parkbase and maximum image size provided by the platforms. Platforms must take
1684*54fd6939SJiyong Parkthis behaviour into account when defining the base/size for each of the images.
1685*54fd6939SJiyong Park
1686*54fd6939SJiyong ParkMemory layout on Arm development platforms
1687*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1688*54fd6939SJiyong Park
1689*54fd6939SJiyong ParkThe following list describes the memory layout on the Arm development platforms:
1690*54fd6939SJiyong Park
1691*54fd6939SJiyong Park-  A 4KB page of shared memory is used for communication between Trusted
1692*54fd6939SJiyong Park   Firmware and the platform's power controller. This is located at the base of
1693*54fd6939SJiyong Park   Trusted SRAM. The amount of Trusted SRAM available to load the bootloader
1694*54fd6939SJiyong Park   images is reduced by the size of the shared memory.
1695*54fd6939SJiyong Park
1696*54fd6939SJiyong Park   The shared memory is used to store the CPUs' entrypoint mailbox. On Juno,
1697*54fd6939SJiyong Park   this is also used for the MHU payload when passing messages to and from the
1698*54fd6939SJiyong Park   SCP.
1699*54fd6939SJiyong Park
1700*54fd6939SJiyong Park-  Another 4 KB page is reserved for passing memory layout between BL1 and BL2
1701*54fd6939SJiyong Park   and also the dynamic firmware configurations.
1702*54fd6939SJiyong Park
1703*54fd6939SJiyong Park-  On FVP, BL1 is originally sitting in the Trusted ROM at address ``0x0``. On
1704*54fd6939SJiyong Park   Juno, BL1 resides in flash memory at address ``0x0BEC0000``. BL1 read-write
1705*54fd6939SJiyong Park   data are relocated to the top of Trusted SRAM at runtime.
1706*54fd6939SJiyong Park
1707*54fd6939SJiyong Park-  BL2 is loaded below BL1 RW
1708*54fd6939SJiyong Park
1709*54fd6939SJiyong Park-  EL3 Runtime Software, BL31 for AArch64 and BL32 for AArch32 (e.g. SP_MIN),
1710*54fd6939SJiyong Park   is loaded at the top of the Trusted SRAM, such that its NOBITS sections will
1711*54fd6939SJiyong Park   overwrite BL1 R/W data and BL2. This implies that BL1 global variables
1712*54fd6939SJiyong Park   remain valid only until execution reaches the EL3 Runtime Software entry
1713*54fd6939SJiyong Park   point during a cold boot.
1714*54fd6939SJiyong Park
1715*54fd6939SJiyong Park-  On Juno, SCP_BL2 is loaded temporarily into the EL3 Runtime Software memory
1716*54fd6939SJiyong Park   region and transferred to the SCP before being overwritten by EL3 Runtime
1717*54fd6939SJiyong Park   Software.
1718*54fd6939SJiyong Park
1719*54fd6939SJiyong Park-  BL32 (for AArch64) can be loaded in one of the following locations:
1720*54fd6939SJiyong Park
1721*54fd6939SJiyong Park   -  Trusted SRAM
1722*54fd6939SJiyong Park   -  Trusted DRAM (FVP only)
1723*54fd6939SJiyong Park   -  Secure region of DRAM (top 16MB of DRAM configured by the TrustZone
1724*54fd6939SJiyong Park      controller)
1725*54fd6939SJiyong Park
1726*54fd6939SJiyong Park   When BL32 (for AArch64) is loaded into Trusted SRAM, it is loaded below
1727*54fd6939SJiyong Park   BL31.
1728*54fd6939SJiyong Park
1729*54fd6939SJiyong ParkThe location of the BL32 image will result in different memory maps. This is
1730*54fd6939SJiyong Parkillustrated for both FVP and Juno in the following diagrams, using the TSP as
1731*54fd6939SJiyong Parkan example.
1732*54fd6939SJiyong Park
1733*54fd6939SJiyong Park.. note::
1734*54fd6939SJiyong Park   Loading the BL32 image in TZC secured DRAM doesn't change the memory
1735*54fd6939SJiyong Park   layout of the other images in Trusted SRAM.
1736*54fd6939SJiyong Park
1737*54fd6939SJiyong ParkCONFIG section in memory layouts shown below contains:
1738*54fd6939SJiyong Park
1739*54fd6939SJiyong Park::
1740*54fd6939SJiyong Park
1741*54fd6939SJiyong Park    +--------------------+
1742*54fd6939SJiyong Park    |bl2_mem_params_descs|
1743*54fd6939SJiyong Park    |--------------------|
1744*54fd6939SJiyong Park    |     fw_configs     |
1745*54fd6939SJiyong Park    +--------------------+
1746*54fd6939SJiyong Park
1747*54fd6939SJiyong Park``bl2_mem_params_descs`` contains parameters passed from BL2 to next the
1748*54fd6939SJiyong ParkBL image during boot.
1749*54fd6939SJiyong Park
1750*54fd6939SJiyong Park``fw_configs`` includes soc_fw_config, tos_fw_config, tb_fw_config and fw_config.
1751*54fd6939SJiyong Park
1752*54fd6939SJiyong Park**FVP with TSP in Trusted SRAM with firmware configs :**
1753*54fd6939SJiyong Park(These diagrams only cover the AArch64 case)
1754*54fd6939SJiyong Park
1755*54fd6939SJiyong Park::
1756*54fd6939SJiyong Park
1757*54fd6939SJiyong Park                   DRAM
1758*54fd6939SJiyong Park    0xffffffff +----------+
1759*54fd6939SJiyong Park               :          :
1760*54fd6939SJiyong Park               |----------|
1761*54fd6939SJiyong Park               |HW_CONFIG |
1762*54fd6939SJiyong Park    0x83000000 |----------|  (non-secure)
1763*54fd6939SJiyong Park               |          |
1764*54fd6939SJiyong Park    0x80000000 +----------+
1765*54fd6939SJiyong Park
1766*54fd6939SJiyong Park               Trusted SRAM
1767*54fd6939SJiyong Park    0x04040000 +----------+  loaded by BL2  +----------------+
1768*54fd6939SJiyong Park               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1769*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1770*54fd6939SJiyong Park               |   BL2    |  <<<<<<<<<<<<<  |                |
1771*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |----------------|
1772*54fd6939SJiyong Park               |          |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1773*54fd6939SJiyong Park               |          |  <<<<<<<<<<<<<  |----------------|
1774*54fd6939SJiyong Park               |          |  <<<<<<<<<<<<<  |     BL32       |
1775*54fd6939SJiyong Park    0x04003000 +----------+                 +----------------+
1776*54fd6939SJiyong Park               |  CONFIG  |
1777*54fd6939SJiyong Park    0x04001000 +----------+
1778*54fd6939SJiyong Park               |  Shared  |
1779*54fd6939SJiyong Park    0x04000000 +----------+
1780*54fd6939SJiyong Park
1781*54fd6939SJiyong Park               Trusted ROM
1782*54fd6939SJiyong Park    0x04000000 +----------+
1783*54fd6939SJiyong Park               | BL1 (ro) |
1784*54fd6939SJiyong Park    0x00000000 +----------+
1785*54fd6939SJiyong Park
1786*54fd6939SJiyong Park**FVP with TSP in Trusted DRAM with firmware configs (default option):**
1787*54fd6939SJiyong Park
1788*54fd6939SJiyong Park::
1789*54fd6939SJiyong Park
1790*54fd6939SJiyong Park                     DRAM
1791*54fd6939SJiyong Park    0xffffffff +--------------+
1792*54fd6939SJiyong Park               :              :
1793*54fd6939SJiyong Park               |--------------|
1794*54fd6939SJiyong Park               |  HW_CONFIG   |
1795*54fd6939SJiyong Park    0x83000000 |--------------|  (non-secure)
1796*54fd6939SJiyong Park               |              |
1797*54fd6939SJiyong Park    0x80000000 +--------------+
1798*54fd6939SJiyong Park
1799*54fd6939SJiyong Park                Trusted DRAM
1800*54fd6939SJiyong Park    0x08000000 +--------------+
1801*54fd6939SJiyong Park               |     BL32     |
1802*54fd6939SJiyong Park    0x06000000 +--------------+
1803*54fd6939SJiyong Park
1804*54fd6939SJiyong Park                 Trusted SRAM
1805*54fd6939SJiyong Park    0x04040000 +--------------+  loaded by BL2  +----------------+
1806*54fd6939SJiyong Park               |   BL1 (rw)   |  <<<<<<<<<<<<<  |                |
1807*54fd6939SJiyong Park               |--------------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1808*54fd6939SJiyong Park               |     BL2      |  <<<<<<<<<<<<<  |                |
1809*54fd6939SJiyong Park               |--------------|  <<<<<<<<<<<<<  |----------------|
1810*54fd6939SJiyong Park               |              |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1811*54fd6939SJiyong Park               |              |                 +----------------+
1812*54fd6939SJiyong Park    0x04003000 +--------------+
1813*54fd6939SJiyong Park               |    CONFIG    |
1814*54fd6939SJiyong Park    0x04001000 +--------------+
1815*54fd6939SJiyong Park               |    Shared    |
1816*54fd6939SJiyong Park    0x04000000 +--------------+
1817*54fd6939SJiyong Park
1818*54fd6939SJiyong Park                 Trusted ROM
1819*54fd6939SJiyong Park    0x04000000 +--------------+
1820*54fd6939SJiyong Park               |   BL1 (ro)   |
1821*54fd6939SJiyong Park    0x00000000 +--------------+
1822*54fd6939SJiyong Park
1823*54fd6939SJiyong Park**FVP with TSP in TZC-Secured DRAM with firmware configs :**
1824*54fd6939SJiyong Park
1825*54fd6939SJiyong Park::
1826*54fd6939SJiyong Park
1827*54fd6939SJiyong Park                   DRAM
1828*54fd6939SJiyong Park    0xffffffff +----------+
1829*54fd6939SJiyong Park               |  BL32    |  (secure)
1830*54fd6939SJiyong Park    0xff000000 +----------+
1831*54fd6939SJiyong Park               |          |
1832*54fd6939SJiyong Park               |----------|
1833*54fd6939SJiyong Park               |HW_CONFIG |
1834*54fd6939SJiyong Park    0x83000000 |----------|  (non-secure)
1835*54fd6939SJiyong Park               |          |
1836*54fd6939SJiyong Park    0x80000000 +----------+
1837*54fd6939SJiyong Park
1838*54fd6939SJiyong Park               Trusted SRAM
1839*54fd6939SJiyong Park    0x04040000 +----------+  loaded by BL2  +----------------+
1840*54fd6939SJiyong Park               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1841*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1842*54fd6939SJiyong Park               |   BL2    |  <<<<<<<<<<<<<  |                |
1843*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |----------------|
1844*54fd6939SJiyong Park               |          |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1845*54fd6939SJiyong Park               |          |                 +----------------+
1846*54fd6939SJiyong Park    0x04003000 +----------+
1847*54fd6939SJiyong Park               |  CONFIG  |
1848*54fd6939SJiyong Park    0x04001000 +----------+
1849*54fd6939SJiyong Park               |  Shared  |
1850*54fd6939SJiyong Park    0x04000000 +----------+
1851*54fd6939SJiyong Park
1852*54fd6939SJiyong Park               Trusted ROM
1853*54fd6939SJiyong Park    0x04000000 +----------+
1854*54fd6939SJiyong Park               | BL1 (ro) |
1855*54fd6939SJiyong Park    0x00000000 +----------+
1856*54fd6939SJiyong Park
1857*54fd6939SJiyong Park**Juno with BL32 in Trusted SRAM :**
1858*54fd6939SJiyong Park
1859*54fd6939SJiyong Park::
1860*54fd6939SJiyong Park
1861*54fd6939SJiyong Park                  Flash0
1862*54fd6939SJiyong Park    0x0C000000 +----------+
1863*54fd6939SJiyong Park               :          :
1864*54fd6939SJiyong Park    0x0BED0000 |----------|
1865*54fd6939SJiyong Park               | BL1 (ro) |
1866*54fd6939SJiyong Park    0x0BEC0000 |----------|
1867*54fd6939SJiyong Park               :          :
1868*54fd6939SJiyong Park    0x08000000 +----------+                  BL31 is loaded
1869*54fd6939SJiyong Park                                             after SCP_BL2 has
1870*54fd6939SJiyong Park               Trusted SRAM                  been sent to SCP
1871*54fd6939SJiyong Park    0x04040000 +----------+  loaded by BL2  +----------------+
1872*54fd6939SJiyong Park               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1873*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1874*54fd6939SJiyong Park               |   BL2    |  <<<<<<<<<<<<<  |                |
1875*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |----------------|
1876*54fd6939SJiyong Park               | SCP_BL2  |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1877*54fd6939SJiyong Park               |          |  <<<<<<<<<<<<<  |----------------|
1878*54fd6939SJiyong Park               |          |  <<<<<<<<<<<<<  |     BL32       |
1879*54fd6939SJiyong Park               |          |                 +----------------+
1880*54fd6939SJiyong Park               |          |
1881*54fd6939SJiyong Park    0x04001000 +----------+
1882*54fd6939SJiyong Park               |   MHU    |
1883*54fd6939SJiyong Park    0x04000000 +----------+
1884*54fd6939SJiyong Park
1885*54fd6939SJiyong Park**Juno with BL32 in TZC-secured DRAM :**
1886*54fd6939SJiyong Park
1887*54fd6939SJiyong Park::
1888*54fd6939SJiyong Park
1889*54fd6939SJiyong Park                   DRAM
1890*54fd6939SJiyong Park    0xFFE00000 +----------+
1891*54fd6939SJiyong Park               |  BL32    |  (secure)
1892*54fd6939SJiyong Park    0xFF000000 |----------|
1893*54fd6939SJiyong Park               |          |
1894*54fd6939SJiyong Park               :          :  (non-secure)
1895*54fd6939SJiyong Park               |          |
1896*54fd6939SJiyong Park    0x80000000 +----------+
1897*54fd6939SJiyong Park
1898*54fd6939SJiyong Park                  Flash0
1899*54fd6939SJiyong Park    0x0C000000 +----------+
1900*54fd6939SJiyong Park               :          :
1901*54fd6939SJiyong Park    0x0BED0000 |----------|
1902*54fd6939SJiyong Park               | BL1 (ro) |
1903*54fd6939SJiyong Park    0x0BEC0000 |----------|
1904*54fd6939SJiyong Park               :          :
1905*54fd6939SJiyong Park    0x08000000 +----------+                  BL31 is loaded
1906*54fd6939SJiyong Park                                             after SCP_BL2 has
1907*54fd6939SJiyong Park               Trusted SRAM                  been sent to SCP
1908*54fd6939SJiyong Park    0x04040000 +----------+  loaded by BL2  +----------------+
1909*54fd6939SJiyong Park               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1910*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1911*54fd6939SJiyong Park               |   BL2    |  <<<<<<<<<<<<<  |                |
1912*54fd6939SJiyong Park               |----------|  <<<<<<<<<<<<<  |----------------|
1913*54fd6939SJiyong Park               | SCP_BL2  |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1914*54fd6939SJiyong Park               |          |                 +----------------+
1915*54fd6939SJiyong Park    0x04001000 +----------+
1916*54fd6939SJiyong Park               |   MHU    |
1917*54fd6939SJiyong Park    0x04000000 +----------+
1918*54fd6939SJiyong Park
1919*54fd6939SJiyong Park.. _firmware_design_fip:
1920*54fd6939SJiyong Park
1921*54fd6939SJiyong ParkFirmware Image Package (FIP)
1922*54fd6939SJiyong Park----------------------------
1923*54fd6939SJiyong Park
1924*54fd6939SJiyong ParkUsing a Firmware Image Package (FIP) allows for packing bootloader images (and
1925*54fd6939SJiyong Parkpotentially other payloads) into a single archive that can be loaded by TF-A
1926*54fd6939SJiyong Parkfrom non-volatile platform storage. A driver to load images from a FIP has
1927*54fd6939SJiyong Parkbeen added to the storage layer and allows a package to be read from supported
1928*54fd6939SJiyong Parkplatform storage. A tool to create Firmware Image Packages is also provided
1929*54fd6939SJiyong Parkand described below.
1930*54fd6939SJiyong Park
1931*54fd6939SJiyong ParkFirmware Image Package layout
1932*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1933*54fd6939SJiyong Park
1934*54fd6939SJiyong ParkThe FIP layout consists of a table of contents (ToC) followed by payload data.
1935*54fd6939SJiyong ParkThe ToC itself has a header followed by one or more table entries. The ToC is
1936*54fd6939SJiyong Parkterminated by an end marker entry, and since the size of the ToC is 0 bytes,
1937*54fd6939SJiyong Parkthe offset equals the total size of the FIP file. All ToC entries describe some
1938*54fd6939SJiyong Parkpayload data that has been appended to the end of the binary package. With the
1939*54fd6939SJiyong Parkinformation provided in the ToC entry the corresponding payload data can be
1940*54fd6939SJiyong Parkretrieved.
1941*54fd6939SJiyong Park
1942*54fd6939SJiyong Park::
1943*54fd6939SJiyong Park
1944*54fd6939SJiyong Park    ------------------
1945*54fd6939SJiyong Park    | ToC Header     |
1946*54fd6939SJiyong Park    |----------------|
1947*54fd6939SJiyong Park    | ToC Entry 0    |
1948*54fd6939SJiyong Park    |----------------|
1949*54fd6939SJiyong Park    | ToC Entry 1    |
1950*54fd6939SJiyong Park    |----------------|
1951*54fd6939SJiyong Park    | ToC End Marker |
1952*54fd6939SJiyong Park    |----------------|
1953*54fd6939SJiyong Park    |                |
1954*54fd6939SJiyong Park    |     Data 0     |
1955*54fd6939SJiyong Park    |                |
1956*54fd6939SJiyong Park    |----------------|
1957*54fd6939SJiyong Park    |                |
1958*54fd6939SJiyong Park    |     Data 1     |
1959*54fd6939SJiyong Park    |                |
1960*54fd6939SJiyong Park    ------------------
1961*54fd6939SJiyong Park
1962*54fd6939SJiyong ParkThe ToC header and entry formats are described in the header file
1963*54fd6939SJiyong Park``include/tools_share/firmware_image_package.h``. This file is used by both the
1964*54fd6939SJiyong Parktool and TF-A.
1965*54fd6939SJiyong Park
1966*54fd6939SJiyong ParkThe ToC header has the following fields:
1967*54fd6939SJiyong Park
1968*54fd6939SJiyong Park::
1969*54fd6939SJiyong Park
1970*54fd6939SJiyong Park    `name`: The name of the ToC. This is currently used to validate the header.
1971*54fd6939SJiyong Park    `serial_number`: A non-zero number provided by the creation tool
1972*54fd6939SJiyong Park    `flags`: Flags associated with this data.
1973*54fd6939SJiyong Park        Bits 0-31: Reserved
1974*54fd6939SJiyong Park        Bits 32-47: Platform defined
1975*54fd6939SJiyong Park        Bits 48-63: Reserved
1976*54fd6939SJiyong Park
1977*54fd6939SJiyong ParkA ToC entry has the following fields:
1978*54fd6939SJiyong Park
1979*54fd6939SJiyong Park::
1980*54fd6939SJiyong Park
1981*54fd6939SJiyong Park    `uuid`: All files are referred to by a pre-defined Universally Unique
1982*54fd6939SJiyong Park        IDentifier [UUID] . The UUIDs are defined in
1983*54fd6939SJiyong Park        `include/tools_share/firmware_image_package.h`. The platform translates
1984*54fd6939SJiyong Park        the requested image name into the corresponding UUID when accessing the
1985*54fd6939SJiyong Park        package.
1986*54fd6939SJiyong Park    `offset_address`: The offset address at which the corresponding payload data
1987*54fd6939SJiyong Park        can be found. The offset is calculated from the ToC base address.
1988*54fd6939SJiyong Park    `size`: The size of the corresponding payload data in bytes.
1989*54fd6939SJiyong Park    `flags`: Flags associated with this entry. None are yet defined.
1990*54fd6939SJiyong Park
1991*54fd6939SJiyong ParkFirmware Image Package creation tool
1992*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1993*54fd6939SJiyong Park
1994*54fd6939SJiyong ParkThe FIP creation tool can be used to pack specified images into a binary
1995*54fd6939SJiyong Parkpackage that can be loaded by TF-A from platform storage. The tool currently
1996*54fd6939SJiyong Parkonly supports packing bootloader images. Additional image definitions can be
1997*54fd6939SJiyong Parkadded to the tool as required.
1998*54fd6939SJiyong Park
1999*54fd6939SJiyong ParkThe tool can be found in ``tools/fiptool``.
2000*54fd6939SJiyong Park
2001*54fd6939SJiyong ParkLoading from a Firmware Image Package (FIP)
2002*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2003*54fd6939SJiyong Park
2004*54fd6939SJiyong ParkThe Firmware Image Package (FIP) driver can load images from a binary package on
2005*54fd6939SJiyong Parknon-volatile platform storage. For the Arm development platforms, this is
2006*54fd6939SJiyong Parkcurrently NOR FLASH.
2007*54fd6939SJiyong Park
2008*54fd6939SJiyong ParkBootloader images are loaded according to the platform policy as specified by
2009*54fd6939SJiyong Parkthe function ``plat_get_image_source()``. For the Arm development platforms, this
2010*54fd6939SJiyong Parkmeans the platform will attempt to load images from a Firmware Image Package
2011*54fd6939SJiyong Parklocated at the start of NOR FLASH0.
2012*54fd6939SJiyong Park
2013*54fd6939SJiyong ParkThe Arm development platforms' policy is to only allow loading of a known set of
2014*54fd6939SJiyong Parkimages. The platform policy can be modified to allow additional images.
2015*54fd6939SJiyong Park
2016*54fd6939SJiyong ParkUse of coherent memory in TF-A
2017*54fd6939SJiyong Park------------------------------
2018*54fd6939SJiyong Park
2019*54fd6939SJiyong ParkThere might be loss of coherency when physical memory with mismatched
2020*54fd6939SJiyong Parkshareability, cacheability and memory attributes is accessed by multiple CPUs
2021*54fd6939SJiyong Park(refer to section B2.9 of `Arm ARM`_ for more details). This possibility occurs
2022*54fd6939SJiyong Parkin TF-A during power up/down sequences when coherency, MMU and caches are
2023*54fd6939SJiyong Parkturned on/off incrementally.
2024*54fd6939SJiyong Park
2025*54fd6939SJiyong ParkTF-A defines coherent memory as a region of memory with Device nGnRE attributes
2026*54fd6939SJiyong Parkin the translation tables. The translation granule size in TF-A is 4KB. This
2027*54fd6939SJiyong Parkis the smallest possible size of the coherent memory region.
2028*54fd6939SJiyong Park
2029*54fd6939SJiyong ParkBy default, all data structures which are susceptible to accesses with
2030*54fd6939SJiyong Parkmismatched attributes from various CPUs are allocated in a coherent memory
2031*54fd6939SJiyong Parkregion (refer to section 2.1 of :ref:`Porting Guide`). The coherent memory
2032*54fd6939SJiyong Parkregion accesses are Outer Shareable, non-cacheable and they can be accessed with
2033*54fd6939SJiyong Parkthe Device nGnRE attributes when the MMU is turned on. Hence, at the expense of
2034*54fd6939SJiyong Parkat least an extra page of memory, TF-A is able to work around coherency issues
2035*54fd6939SJiyong Parkdue to mismatched memory attributes.
2036*54fd6939SJiyong Park
2037*54fd6939SJiyong ParkThe alternative to the above approach is to allocate the susceptible data
2038*54fd6939SJiyong Parkstructures in Normal WriteBack WriteAllocate Inner shareable memory. This
2039*54fd6939SJiyong Parkapproach requires the data structures to be designed so that it is possible to
2040*54fd6939SJiyong Parkwork around the issue of mismatched memory attributes by performing software
2041*54fd6939SJiyong Parkcache maintenance on them.
2042*54fd6939SJiyong Park
2043*54fd6939SJiyong ParkDisabling the use of coherent memory in TF-A
2044*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2045*54fd6939SJiyong Park
2046*54fd6939SJiyong ParkIt might be desirable to avoid the cost of allocating coherent memory on
2047*54fd6939SJiyong Parkplatforms which are memory constrained. TF-A enables inclusion of coherent
2048*54fd6939SJiyong Parkmemory in firmware images through the build flag ``USE_COHERENT_MEM``.
2049*54fd6939SJiyong ParkThis flag is enabled by default. It can be disabled to choose the second
2050*54fd6939SJiyong Parkapproach described above.
2051*54fd6939SJiyong Park
2052*54fd6939SJiyong ParkThe below sections analyze the data structures allocated in the coherent memory
2053*54fd6939SJiyong Parkregion and the changes required to allocate them in normal memory.
2054*54fd6939SJiyong Park
2055*54fd6939SJiyong ParkCoherent memory usage in PSCI implementation
2056*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2057*54fd6939SJiyong Park
2058*54fd6939SJiyong ParkThe ``psci_non_cpu_pd_nodes`` data structure stores the platform's power domain
2059*54fd6939SJiyong Parktree information for state management of power domains. By default, this data
2060*54fd6939SJiyong Parkstructure is allocated in the coherent memory region in TF-A because it can be
2061*54fd6939SJiyong Parkaccessed by multiple CPUs, either with caches enabled or disabled.
2062*54fd6939SJiyong Park
2063*54fd6939SJiyong Park.. code:: c
2064*54fd6939SJiyong Park
2065*54fd6939SJiyong Park    typedef struct non_cpu_pwr_domain_node {
2066*54fd6939SJiyong Park        /*
2067*54fd6939SJiyong Park         * Index of the first CPU power domain node level 0 which has this node
2068*54fd6939SJiyong Park         * as its parent.
2069*54fd6939SJiyong Park         */
2070*54fd6939SJiyong Park        unsigned int cpu_start_idx;
2071*54fd6939SJiyong Park
2072*54fd6939SJiyong Park        /*
2073*54fd6939SJiyong Park         * Number of CPU power domains which are siblings of the domain indexed
2074*54fd6939SJiyong Park         * by 'cpu_start_idx' i.e. all the domains in the range 'cpu_start_idx
2075*54fd6939SJiyong Park         * -> cpu_start_idx + ncpus' have this node as their parent.
2076*54fd6939SJiyong Park         */
2077*54fd6939SJiyong Park        unsigned int ncpus;
2078*54fd6939SJiyong Park
2079*54fd6939SJiyong Park        /*
2080*54fd6939SJiyong Park         * Index of the parent power domain node.
2081*54fd6939SJiyong Park         */
2082*54fd6939SJiyong Park        unsigned int parent_node;
2083*54fd6939SJiyong Park
2084*54fd6939SJiyong Park        plat_local_state_t local_state;
2085*54fd6939SJiyong Park
2086*54fd6939SJiyong Park        unsigned char level;
2087*54fd6939SJiyong Park
2088*54fd6939SJiyong Park        /* For indexing the psci_lock array*/
2089*54fd6939SJiyong Park        unsigned char lock_index;
2090*54fd6939SJiyong Park    } non_cpu_pd_node_t;
2091*54fd6939SJiyong Park
2092*54fd6939SJiyong ParkIn order to move this data structure to normal memory, the use of each of its
2093*54fd6939SJiyong Parkfields must be analyzed. Fields like ``cpu_start_idx``, ``ncpus``, ``parent_node``
2094*54fd6939SJiyong Park``level`` and ``lock_index`` are only written once during cold boot. Hence removing
2095*54fd6939SJiyong Parkthem from coherent memory involves only doing a clean and invalidate of the
2096*54fd6939SJiyong Parkcache lines after these fields are written.
2097*54fd6939SJiyong Park
2098*54fd6939SJiyong ParkThe field ``local_state`` can be concurrently accessed by multiple CPUs in
2099*54fd6939SJiyong Parkdifferent cache states. A Lamport's Bakery lock ``psci_locks`` is used to ensure
2100*54fd6939SJiyong Parkmutual exclusion to this field and a clean and invalidate is needed after it
2101*54fd6939SJiyong Parkis written.
2102*54fd6939SJiyong Park
2103*54fd6939SJiyong ParkBakery lock data
2104*54fd6939SJiyong Park~~~~~~~~~~~~~~~~
2105*54fd6939SJiyong Park
2106*54fd6939SJiyong ParkThe bakery lock data structure ``bakery_lock_t`` is allocated in coherent memory
2107*54fd6939SJiyong Parkand is accessed by multiple CPUs with mismatched attributes. ``bakery_lock_t`` is
2108*54fd6939SJiyong Parkdefined as follows:
2109*54fd6939SJiyong Park
2110*54fd6939SJiyong Park.. code:: c
2111*54fd6939SJiyong Park
2112*54fd6939SJiyong Park    typedef struct bakery_lock {
2113*54fd6939SJiyong Park        /*
2114*54fd6939SJiyong Park         * The lock_data is a bit-field of 2 members:
2115*54fd6939SJiyong Park         * Bit[0]       : choosing. This field is set when the CPU is
2116*54fd6939SJiyong Park         *                choosing its bakery number.
2117*54fd6939SJiyong Park         * Bits[1 - 15] : number. This is the bakery number allocated.
2118*54fd6939SJiyong Park         */
2119*54fd6939SJiyong Park        volatile uint16_t lock_data[BAKERY_LOCK_MAX_CPUS];
2120*54fd6939SJiyong Park    } bakery_lock_t;
2121*54fd6939SJiyong Park
2122*54fd6939SJiyong ParkIt is a characteristic of Lamport's Bakery algorithm that the volatile per-CPU
2123*54fd6939SJiyong Parkfields can be read by all CPUs but only written to by the owning CPU.
2124*54fd6939SJiyong Park
2125*54fd6939SJiyong ParkDepending upon the data cache line size, the per-CPU fields of the
2126*54fd6939SJiyong Park``bakery_lock_t`` structure for multiple CPUs may exist on a single cache line.
2127*54fd6939SJiyong ParkThese per-CPU fields can be read and written during lock contention by multiple
2128*54fd6939SJiyong ParkCPUs with mismatched memory attributes. Since these fields are a part of the
2129*54fd6939SJiyong Parklock implementation, they do not have access to any other locking primitive to
2130*54fd6939SJiyong Parksafeguard against the resulting coherency issues. As a result, simple software
2131*54fd6939SJiyong Parkcache maintenance is not enough to allocate them in coherent memory. Consider
2132*54fd6939SJiyong Parkthe following example.
2133*54fd6939SJiyong Park
2134*54fd6939SJiyong ParkCPU0 updates its per-CPU field with data cache enabled. This write updates a
2135*54fd6939SJiyong Parklocal cache line which contains a copy of the fields for other CPUs as well. Now
2136*54fd6939SJiyong ParkCPU1 updates its per-CPU field of the ``bakery_lock_t`` structure with data cache
2137*54fd6939SJiyong Parkdisabled. CPU1 then issues a DCIVAC operation to invalidate any stale copies of
2138*54fd6939SJiyong Parkits field in any other cache line in the system. This operation will invalidate
2139*54fd6939SJiyong Parkthe update made by CPU0 as well.
2140*54fd6939SJiyong Park
2141*54fd6939SJiyong ParkTo use bakery locks when ``USE_COHERENT_MEM`` is disabled, the lock data structure
2142*54fd6939SJiyong Parkhas been redesigned. The changes utilise the characteristic of Lamport's Bakery
2143*54fd6939SJiyong Parkalgorithm mentioned earlier. The bakery_lock structure only allocates the memory
2144*54fd6939SJiyong Parkfor a single CPU. The macro ``DEFINE_BAKERY_LOCK`` allocates all the bakery locks
2145*54fd6939SJiyong Parkneeded for a CPU into a section ``bakery_lock``. The linker allocates the memory
2146*54fd6939SJiyong Parkfor other cores by using the total size allocated for the bakery_lock section
2147*54fd6939SJiyong Parkand multiplying it with (PLATFORM_CORE_COUNT - 1). This enables software to
2148*54fd6939SJiyong Parkperform software cache maintenance on the lock data structure without running
2149*54fd6939SJiyong Parkinto coherency issues associated with mismatched attributes.
2150*54fd6939SJiyong Park
2151*54fd6939SJiyong ParkThe bakery lock data structure ``bakery_info_t`` is defined for use when
2152*54fd6939SJiyong Park``USE_COHERENT_MEM`` is disabled as follows:
2153*54fd6939SJiyong Park
2154*54fd6939SJiyong Park.. code:: c
2155*54fd6939SJiyong Park
2156*54fd6939SJiyong Park    typedef struct bakery_info {
2157*54fd6939SJiyong Park        /*
2158*54fd6939SJiyong Park         * The lock_data is a bit-field of 2 members:
2159*54fd6939SJiyong Park         * Bit[0]       : choosing. This field is set when the CPU is
2160*54fd6939SJiyong Park         *                choosing its bakery number.
2161*54fd6939SJiyong Park         * Bits[1 - 15] : number. This is the bakery number allocated.
2162*54fd6939SJiyong Park         */
2163*54fd6939SJiyong Park         volatile uint16_t lock_data;
2164*54fd6939SJiyong Park    } bakery_info_t;
2165*54fd6939SJiyong Park
2166*54fd6939SJiyong ParkThe ``bakery_info_t`` represents a single per-CPU field of one lock and
2167*54fd6939SJiyong Parkthe combination of corresponding ``bakery_info_t`` structures for all CPUs in the
2168*54fd6939SJiyong Parksystem represents the complete bakery lock. The view in memory for a system
2169*54fd6939SJiyong Parkwith n bakery locks are:
2170*54fd6939SJiyong Park
2171*54fd6939SJiyong Park::
2172*54fd6939SJiyong Park
2173*54fd6939SJiyong Park    bakery_lock section start
2174*54fd6939SJiyong Park    |----------------|
2175*54fd6939SJiyong Park    | `bakery_info_t`| <-- Lock_0 per-CPU field
2176*54fd6939SJiyong Park    |    Lock_0      |     for CPU0
2177*54fd6939SJiyong Park    |----------------|
2178*54fd6939SJiyong Park    | `bakery_info_t`| <-- Lock_1 per-CPU field
2179*54fd6939SJiyong Park    |    Lock_1      |     for CPU0
2180*54fd6939SJiyong Park    |----------------|
2181*54fd6939SJiyong Park    | ....           |
2182*54fd6939SJiyong Park    |----------------|
2183*54fd6939SJiyong Park    | `bakery_info_t`| <-- Lock_N per-CPU field
2184*54fd6939SJiyong Park    |    Lock_N      |     for CPU0
2185*54fd6939SJiyong Park    ------------------
2186*54fd6939SJiyong Park    |    XXXXX       |
2187*54fd6939SJiyong Park    | Padding to     |
2188*54fd6939SJiyong Park    | next Cache WB  | <--- Calculate PERCPU_BAKERY_LOCK_SIZE, allocate
2189*54fd6939SJiyong Park    |  Granule       |       continuous memory for remaining CPUs.
2190*54fd6939SJiyong Park    ------------------
2191*54fd6939SJiyong Park    | `bakery_info_t`| <-- Lock_0 per-CPU field
2192*54fd6939SJiyong Park    |    Lock_0      |     for CPU1
2193*54fd6939SJiyong Park    |----------------|
2194*54fd6939SJiyong Park    | `bakery_info_t`| <-- Lock_1 per-CPU field
2195*54fd6939SJiyong Park    |    Lock_1      |     for CPU1
2196*54fd6939SJiyong Park    |----------------|
2197*54fd6939SJiyong Park    | ....           |
2198*54fd6939SJiyong Park    |----------------|
2199*54fd6939SJiyong Park    | `bakery_info_t`| <-- Lock_N per-CPU field
2200*54fd6939SJiyong Park    |    Lock_N      |     for CPU1
2201*54fd6939SJiyong Park    ------------------
2202*54fd6939SJiyong Park    |    XXXXX       |
2203*54fd6939SJiyong Park    | Padding to     |
2204*54fd6939SJiyong Park    | next Cache WB  |
2205*54fd6939SJiyong Park    |  Granule       |
2206*54fd6939SJiyong Park    ------------------
2207*54fd6939SJiyong Park
2208*54fd6939SJiyong ParkConsider a system of 2 CPUs with 'N' bakery locks as shown above. For an
2209*54fd6939SJiyong Parkoperation on Lock_N, the corresponding ``bakery_info_t`` in both CPU0 and CPU1
2210*54fd6939SJiyong Park``bakery_lock`` section need to be fetched and appropriate cache operations need
2211*54fd6939SJiyong Parkto be performed for each access.
2212*54fd6939SJiyong Park
2213*54fd6939SJiyong ParkOn Arm Platforms, bakery locks are used in psci (``psci_locks``) and power controller
2214*54fd6939SJiyong Parkdriver (``arm_lock``).
2215*54fd6939SJiyong Park
2216*54fd6939SJiyong ParkNon Functional Impact of removing coherent memory
2217*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2218*54fd6939SJiyong Park
2219*54fd6939SJiyong ParkRemoval of the coherent memory region leads to the additional software overhead
2220*54fd6939SJiyong Parkof performing cache maintenance for the affected data structures. However, since
2221*54fd6939SJiyong Parkthe memory where the data structures are allocated is cacheable, the overhead is
2222*54fd6939SJiyong Parkmostly mitigated by an increase in performance.
2223*54fd6939SJiyong Park
2224*54fd6939SJiyong ParkThere is however a performance impact for bakery locks, due to:
2225*54fd6939SJiyong Park
2226*54fd6939SJiyong Park-  Additional cache maintenance operations, and
2227*54fd6939SJiyong Park-  Multiple cache line reads for each lock operation, since the bakery locks
2228*54fd6939SJiyong Park   for each CPU are distributed across different cache lines.
2229*54fd6939SJiyong Park
2230*54fd6939SJiyong ParkThe implementation has been optimized to minimize this additional overhead.
2231*54fd6939SJiyong ParkMeasurements indicate that when bakery locks are allocated in Normal memory, the
2232*54fd6939SJiyong Parkminimum latency of acquiring a lock is on an average 3-4 micro seconds whereas
2233*54fd6939SJiyong Parkin Device memory the same is 2 micro seconds. The measurements were done on the
2234*54fd6939SJiyong ParkJuno Arm development platform.
2235*54fd6939SJiyong Park
2236*54fd6939SJiyong ParkAs mentioned earlier, almost a page of memory can be saved by disabling
2237*54fd6939SJiyong Park``USE_COHERENT_MEM``. Each platform needs to consider these trade-offs to decide
2238*54fd6939SJiyong Parkwhether coherent memory should be used. If a platform disables
2239*54fd6939SJiyong Park``USE_COHERENT_MEM`` and needs to use bakery locks in the porting layer, it can
2240*54fd6939SJiyong Parkoptionally define macro ``PLAT_PERCPU_BAKERY_LOCK_SIZE`` (see the
2241*54fd6939SJiyong Park:ref:`Porting Guide`). Refer to the reference platform code for examples.
2242*54fd6939SJiyong Park
2243*54fd6939SJiyong ParkIsolating code and read-only data on separate memory pages
2244*54fd6939SJiyong Park----------------------------------------------------------
2245*54fd6939SJiyong Park
2246*54fd6939SJiyong ParkIn the Armv8-A VMSA, translation table entries include fields that define the
2247*54fd6939SJiyong Parkproperties of the target memory region, such as its access permissions. The
2248*54fd6939SJiyong Parksmallest unit of memory that can be addressed by a translation table entry is
2249*54fd6939SJiyong Parka memory page. Therefore, if software needs to set different permissions on two
2250*54fd6939SJiyong Parkmemory regions then it needs to map them using different memory pages.
2251*54fd6939SJiyong Park
2252*54fd6939SJiyong ParkThe default memory layout for each BL image is as follows:
2253*54fd6939SJiyong Park
2254*54fd6939SJiyong Park::
2255*54fd6939SJiyong Park
2256*54fd6939SJiyong Park       |        ...        |
2257*54fd6939SJiyong Park       +-------------------+
2258*54fd6939SJiyong Park       |  Read-write data  |
2259*54fd6939SJiyong Park       +-------------------+ Page boundary
2260*54fd6939SJiyong Park       |     <Padding>     |
2261*54fd6939SJiyong Park       +-------------------+
2262*54fd6939SJiyong Park       | Exception vectors |
2263*54fd6939SJiyong Park       +-------------------+ 2 KB boundary
2264*54fd6939SJiyong Park       |     <Padding>     |
2265*54fd6939SJiyong Park       +-------------------+
2266*54fd6939SJiyong Park       |  Read-only data   |
2267*54fd6939SJiyong Park       +-------------------+
2268*54fd6939SJiyong Park       |       Code        |
2269*54fd6939SJiyong Park       +-------------------+ BLx_BASE
2270*54fd6939SJiyong Park
2271*54fd6939SJiyong Park.. note::
2272*54fd6939SJiyong Park   The 2KB alignment for the exception vectors is an architectural
2273*54fd6939SJiyong Park   requirement.
2274*54fd6939SJiyong Park
2275*54fd6939SJiyong ParkThe read-write data start on a new memory page so that they can be mapped with
2276*54fd6939SJiyong Parkread-write permissions, whereas the code and read-only data below are configured
2277*54fd6939SJiyong Parkas read-only.
2278*54fd6939SJiyong Park
2279*54fd6939SJiyong ParkHowever, the read-only data are not aligned on a page boundary. They are
2280*54fd6939SJiyong Parkcontiguous to the code. Therefore, the end of the code section and the beginning
2281*54fd6939SJiyong Parkof the read-only data one might share a memory page. This forces both to be
2282*54fd6939SJiyong Parkmapped with the same memory attributes. As the code needs to be executable, this
2283*54fd6939SJiyong Parkmeans that the read-only data stored on the same memory page as the code are
2284*54fd6939SJiyong Parkexecutable as well. This could potentially be exploited as part of a security
2285*54fd6939SJiyong Parkattack.
2286*54fd6939SJiyong Park
2287*54fd6939SJiyong ParkTF provides the build flag ``SEPARATE_CODE_AND_RODATA`` to isolate the code and
2288*54fd6939SJiyong Parkread-only data on separate memory pages. This in turn allows independent control
2289*54fd6939SJiyong Parkof the access permissions for the code and read-only data. In this case,
2290*54fd6939SJiyong Parkplatform code gets a finer-grained view of the image layout and can
2291*54fd6939SJiyong Parkappropriately map the code region as executable and the read-only data as
2292*54fd6939SJiyong Parkexecute-never.
2293*54fd6939SJiyong Park
2294*54fd6939SJiyong ParkThis has an impact on memory footprint, as padding bytes need to be introduced
2295*54fd6939SJiyong Parkbetween the code and read-only data to ensure the segregation of the two. To
2296*54fd6939SJiyong Parklimit the memory cost, this flag also changes the memory layout such that the
2297*54fd6939SJiyong Parkcode and exception vectors are now contiguous, like so:
2298*54fd6939SJiyong Park
2299*54fd6939SJiyong Park::
2300*54fd6939SJiyong Park
2301*54fd6939SJiyong Park       |        ...        |
2302*54fd6939SJiyong Park       +-------------------+
2303*54fd6939SJiyong Park       |  Read-write data  |
2304*54fd6939SJiyong Park       +-------------------+ Page boundary
2305*54fd6939SJiyong Park       |     <Padding>     |
2306*54fd6939SJiyong Park       +-------------------+
2307*54fd6939SJiyong Park       |  Read-only data   |
2308*54fd6939SJiyong Park       +-------------------+ Page boundary
2309*54fd6939SJiyong Park       |     <Padding>     |
2310*54fd6939SJiyong Park       +-------------------+
2311*54fd6939SJiyong Park       | Exception vectors |
2312*54fd6939SJiyong Park       +-------------------+ 2 KB boundary
2313*54fd6939SJiyong Park       |     <Padding>     |
2314*54fd6939SJiyong Park       +-------------------+
2315*54fd6939SJiyong Park       |       Code        |
2316*54fd6939SJiyong Park       +-------------------+ BLx_BASE
2317*54fd6939SJiyong Park
2318*54fd6939SJiyong ParkWith this more condensed memory layout, the separation of read-only data will
2319*54fd6939SJiyong Parkadd zero or one page to the memory footprint of each BL image. Each platform
2320*54fd6939SJiyong Parkshould consider the trade-off between memory footprint and security.
2321*54fd6939SJiyong Park
2322*54fd6939SJiyong ParkThis build flag is disabled by default, minimising memory footprint. On Arm
2323*54fd6939SJiyong Parkplatforms, it is enabled.
2324*54fd6939SJiyong Park
2325*54fd6939SJiyong ParkPublish and Subscribe Framework
2326*54fd6939SJiyong Park-------------------------------
2327*54fd6939SJiyong Park
2328*54fd6939SJiyong ParkThe Publish and Subscribe Framework allows EL3 components to define and publish
2329*54fd6939SJiyong Parkevents, to which other EL3 components can subscribe.
2330*54fd6939SJiyong Park
2331*54fd6939SJiyong ParkThe following macros are provided by the framework:
2332*54fd6939SJiyong Park
2333*54fd6939SJiyong Park-  ``REGISTER_PUBSUB_EVENT(event)``: Defines an event, and takes one argument,
2334*54fd6939SJiyong Park   the event name, which must be a valid C identifier. All calls to
2335*54fd6939SJiyong Park   ``REGISTER_PUBSUB_EVENT`` macro must be placed in the file
2336*54fd6939SJiyong Park   ``pubsub_events.h``.
2337*54fd6939SJiyong Park
2338*54fd6939SJiyong Park-  ``PUBLISH_EVENT_ARG(event, arg)``: Publishes a defined event, by iterating
2339*54fd6939SJiyong Park   subscribed handlers and calling them in turn. The handlers will be passed the
2340*54fd6939SJiyong Park   parameter ``arg``. The expected use-case is to broadcast an event.
2341*54fd6939SJiyong Park
2342*54fd6939SJiyong Park-  ``PUBLISH_EVENT(event)``: Like ``PUBLISH_EVENT_ARG``, except that the value
2343*54fd6939SJiyong Park   ``NULL`` is passed to subscribed handlers.
2344*54fd6939SJiyong Park
2345*54fd6939SJiyong Park-  ``SUBSCRIBE_TO_EVENT(event, handler)``: Registers the ``handler`` to
2346*54fd6939SJiyong Park   subscribe to ``event``. The handler will be executed whenever the ``event``
2347*54fd6939SJiyong Park   is published.
2348*54fd6939SJiyong Park
2349*54fd6939SJiyong Park-  ``for_each_subscriber(event, subscriber)``: Iterates through all handlers
2350*54fd6939SJiyong Park   subscribed for ``event``. ``subscriber`` must be a local variable of type
2351*54fd6939SJiyong Park   ``pubsub_cb_t *``, and will point to each subscribed handler in turn during
2352*54fd6939SJiyong Park   iteration. This macro can be used for those patterns that none of the
2353*54fd6939SJiyong Park   ``PUBLISH_EVENT_*()`` macros cover.
2354*54fd6939SJiyong Park
2355*54fd6939SJiyong ParkPublishing an event that wasn't defined using ``REGISTER_PUBSUB_EVENT`` will
2356*54fd6939SJiyong Parkresult in build error. Subscribing to an undefined event however won't.
2357*54fd6939SJiyong Park
2358*54fd6939SJiyong ParkSubscribed handlers must be of type ``pubsub_cb_t``, with following function
2359*54fd6939SJiyong Parksignature:
2360*54fd6939SJiyong Park
2361*54fd6939SJiyong Park.. code:: c
2362*54fd6939SJiyong Park
2363*54fd6939SJiyong Park   typedef void* (*pubsub_cb_t)(const void *arg);
2364*54fd6939SJiyong Park
2365*54fd6939SJiyong ParkThere may be arbitrary number of handlers registered to the same event. The
2366*54fd6939SJiyong Parkorder in which subscribed handlers are notified when that event is published is
2367*54fd6939SJiyong Parknot defined. Subscribed handlers may be executed in any order; handlers should
2368*54fd6939SJiyong Parknot assume any relative ordering amongst them.
2369*54fd6939SJiyong Park
2370*54fd6939SJiyong ParkPublishing an event on a PE will result in subscribed handlers executing on that
2371*54fd6939SJiyong ParkPE only; it won't cause handlers to execute on a different PE.
2372*54fd6939SJiyong Park
2373*54fd6939SJiyong ParkNote that publishing an event on a PE blocks until all the subscribed handlers
2374*54fd6939SJiyong Parkfinish executing on the PE.
2375*54fd6939SJiyong Park
2376*54fd6939SJiyong ParkTF-A generic code publishes and subscribes to some events within. Platform
2377*54fd6939SJiyong Parkports are discouraged from subscribing to them. These events may be withdrawn,
2378*54fd6939SJiyong Parkrenamed, or have their semantics altered in the future. Platforms may however
2379*54fd6939SJiyong Parkregister, publish, and subscribe to platform-specific events.
2380*54fd6939SJiyong Park
2381*54fd6939SJiyong ParkPublish and Subscribe Example
2382*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2383*54fd6939SJiyong Park
2384*54fd6939SJiyong ParkA publisher that wants to publish event ``foo`` would:
2385*54fd6939SJiyong Park
2386*54fd6939SJiyong Park-  Define the event ``foo`` in the ``pubsub_events.h``.
2387*54fd6939SJiyong Park
2388*54fd6939SJiyong Park   .. code:: c
2389*54fd6939SJiyong Park
2390*54fd6939SJiyong Park      REGISTER_PUBSUB_EVENT(foo);
2391*54fd6939SJiyong Park
2392*54fd6939SJiyong Park-  Depending on the nature of event, use one of ``PUBLISH_EVENT_*()`` macros to
2393*54fd6939SJiyong Park   publish the event at the appropriate path and time of execution.
2394*54fd6939SJiyong Park
2395*54fd6939SJiyong ParkA subscriber that wants to subscribe to event ``foo`` published above would
2396*54fd6939SJiyong Parkimplement:
2397*54fd6939SJiyong Park
2398*54fd6939SJiyong Park.. code:: c
2399*54fd6939SJiyong Park
2400*54fd6939SJiyong Park    void *foo_handler(const void *arg)
2401*54fd6939SJiyong Park    {
2402*54fd6939SJiyong Park         void *result;
2403*54fd6939SJiyong Park
2404*54fd6939SJiyong Park         /* Do handling ... */
2405*54fd6939SJiyong Park
2406*54fd6939SJiyong Park         return result;
2407*54fd6939SJiyong Park    }
2408*54fd6939SJiyong Park
2409*54fd6939SJiyong Park    SUBSCRIBE_TO_EVENT(foo, foo_handler);
2410*54fd6939SJiyong Park
2411*54fd6939SJiyong Park
2412*54fd6939SJiyong ParkReclaiming the BL31 initialization code
2413*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2414*54fd6939SJiyong Park
2415*54fd6939SJiyong ParkA significant amount of the code used for the initialization of BL31 is never
2416*54fd6939SJiyong Parkneeded again after boot time. In order to reduce the runtime memory
2417*54fd6939SJiyong Parkfootprint, the memory used for this code can be reclaimed after initialization
2418*54fd6939SJiyong Parkhas finished and be used for runtime data.
2419*54fd6939SJiyong Park
2420*54fd6939SJiyong ParkThe build option ``RECLAIM_INIT_CODE`` can be set to mark this boot time code
2421*54fd6939SJiyong Parkwith a ``.text.init.*`` attribute which can be filtered and placed suitably
2422*54fd6939SJiyong Parkwithin the BL image for later reclamation by the platform. The platform can
2423*54fd6939SJiyong Parkspecify the filter and the memory region for this init section in BL31 via the
2424*54fd6939SJiyong Parkplat.ld.S linker script. For example, on the FVP, this section is placed
2425*54fd6939SJiyong Parkoverlapping the secondary CPU stacks so that after the cold boot is done, this
2426*54fd6939SJiyong Parkmemory can be reclaimed for the stacks. The init memory section is initially
2427*54fd6939SJiyong Parkmapped with ``RO``, ``EXECUTE`` attributes. After BL31 initialization has
2428*54fd6939SJiyong Parkcompleted, the FVP changes the attributes of this section to ``RW``,
2429*54fd6939SJiyong Park``EXECUTE_NEVER`` allowing it to be used for runtime data. The memory attributes
2430*54fd6939SJiyong Parkare changed within the ``bl31_plat_runtime_setup`` platform hook. The init
2431*54fd6939SJiyong Parksection section can be reclaimed for any data which is accessed after cold
2432*54fd6939SJiyong Parkboot initialization and it is upto the platform to make the decision.
2433*54fd6939SJiyong Park
2434*54fd6939SJiyong Park.. _firmware_design_pmf:
2435*54fd6939SJiyong Park
2436*54fd6939SJiyong ParkPerformance Measurement Framework
2437*54fd6939SJiyong Park---------------------------------
2438*54fd6939SJiyong Park
2439*54fd6939SJiyong ParkThe Performance Measurement Framework (PMF) facilitates collection of
2440*54fd6939SJiyong Parktimestamps by registered services and provides interfaces to retrieve them
2441*54fd6939SJiyong Parkfrom within TF-A. A platform can choose to expose appropriate SMCs to
2442*54fd6939SJiyong Parkretrieve these collected timestamps.
2443*54fd6939SJiyong Park
2444*54fd6939SJiyong ParkBy default, the global physical counter is used for the timestamp
2445*54fd6939SJiyong Parkvalue and is read via ``CNTPCT_EL0``. The framework allows to retrieve
2446*54fd6939SJiyong Parktimestamps captured by other CPUs.
2447*54fd6939SJiyong Park
2448*54fd6939SJiyong ParkTimestamp identifier format
2449*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~
2450*54fd6939SJiyong Park
2451*54fd6939SJiyong ParkA PMF timestamp is uniquely identified across the system via the
2452*54fd6939SJiyong Parktimestamp ID or ``tid``. The ``tid`` is composed as follows:
2453*54fd6939SJiyong Park
2454*54fd6939SJiyong Park::
2455*54fd6939SJiyong Park
2456*54fd6939SJiyong Park    Bits 0-7: The local timestamp identifier.
2457*54fd6939SJiyong Park    Bits 8-9: Reserved.
2458*54fd6939SJiyong Park    Bits 10-15: The service identifier.
2459*54fd6939SJiyong Park    Bits 16-31: Reserved.
2460*54fd6939SJiyong Park
2461*54fd6939SJiyong Park#. The service identifier. Each PMF service is identified by a
2462*54fd6939SJiyong Park   service name and a service identifier. Both the service name and
2463*54fd6939SJiyong Park   identifier are unique within the system as a whole.
2464*54fd6939SJiyong Park
2465*54fd6939SJiyong Park#. The local timestamp identifier. This identifier is unique within a given
2466*54fd6939SJiyong Park   service.
2467*54fd6939SJiyong Park
2468*54fd6939SJiyong ParkRegistering a PMF service
2469*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~
2470*54fd6939SJiyong Park
2471*54fd6939SJiyong ParkTo register a PMF service, the ``PMF_REGISTER_SERVICE()`` macro from ``pmf.h``
2472*54fd6939SJiyong Parkis used. The arguments required are the service name, the service ID,
2473*54fd6939SJiyong Parkthe total number of local timestamps to be captured and a set of flags.
2474*54fd6939SJiyong Park
2475*54fd6939SJiyong ParkThe ``flags`` field can be specified as a bitwise-OR of the following values:
2476*54fd6939SJiyong Park
2477*54fd6939SJiyong Park::
2478*54fd6939SJiyong Park
2479*54fd6939SJiyong Park    PMF_STORE_ENABLE: The timestamp is stored in memory for later retrieval.
2480*54fd6939SJiyong Park    PMF_DUMP_ENABLE: The timestamp is dumped on the serial console.
2481*54fd6939SJiyong Park
2482*54fd6939SJiyong ParkThe ``PMF_REGISTER_SERVICE()`` reserves memory to store captured
2483*54fd6939SJiyong Parktimestamps in a PMF specific linker section at build time.
2484*54fd6939SJiyong ParkAdditionally, it defines necessary functions to capture and
2485*54fd6939SJiyong Parkretrieve a particular timestamp for the given service at runtime.
2486*54fd6939SJiyong Park
2487*54fd6939SJiyong ParkThe macro ``PMF_REGISTER_SERVICE()`` only enables capturing PMF timestamps
2488*54fd6939SJiyong Parkfrom within TF-A. In order to retrieve timestamps from outside of TF-A, the
2489*54fd6939SJiyong Park``PMF_REGISTER_SERVICE_SMC()`` macro must be used instead. This macro
2490*54fd6939SJiyong Parkaccepts the same set of arguments as the ``PMF_REGISTER_SERVICE()``
2491*54fd6939SJiyong Parkmacro but additionally supports retrieving timestamps using SMCs.
2492*54fd6939SJiyong Park
2493*54fd6939SJiyong ParkCapturing a timestamp
2494*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~
2495*54fd6939SJiyong Park
2496*54fd6939SJiyong ParkPMF timestamps are stored in a per-service timestamp region. On a
2497*54fd6939SJiyong Parksystem with multiple CPUs, each timestamp is captured and stored
2498*54fd6939SJiyong Parkin a per-CPU cache line aligned memory region.
2499*54fd6939SJiyong Park
2500*54fd6939SJiyong ParkHaving registered the service, the ``PMF_CAPTURE_TIMESTAMP()`` macro can be
2501*54fd6939SJiyong Parkused to capture a timestamp at the location where it is used. The macro
2502*54fd6939SJiyong Parktakes the service name, a local timestamp identifier and a flag as arguments.
2503*54fd6939SJiyong Park
2504*54fd6939SJiyong ParkThe ``flags`` field argument can be zero, or ``PMF_CACHE_MAINT`` which
2505*54fd6939SJiyong Parkinstructs PMF to do cache maintenance following the capture. Cache
2506*54fd6939SJiyong Parkmaintenance is required if any of the service's timestamps are captured
2507*54fd6939SJiyong Parkwith data cache disabled.
2508*54fd6939SJiyong Park
2509*54fd6939SJiyong ParkTo capture a timestamp in assembly code, the caller should use
2510*54fd6939SJiyong Park``pmf_calc_timestamp_addr`` macro (defined in ``pmf_asm_macros.S``) to
2511*54fd6939SJiyong Parkcalculate the address of where the timestamp would be stored. The
2512*54fd6939SJiyong Parkcaller should then read ``CNTPCT_EL0`` register to obtain the timestamp
2513*54fd6939SJiyong Parkand store it at the determined address for later retrieval.
2514*54fd6939SJiyong Park
2515*54fd6939SJiyong ParkRetrieving a timestamp
2516*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~
2517*54fd6939SJiyong Park
2518*54fd6939SJiyong ParkFrom within TF-A, timestamps for individual CPUs can be retrieved using either
2519*54fd6939SJiyong Park``PMF_GET_TIMESTAMP_BY_MPIDR()`` or ``PMF_GET_TIMESTAMP_BY_INDEX()`` macros.
2520*54fd6939SJiyong ParkThese macros accept the CPU's MPIDR value, or its ordinal position
2521*54fd6939SJiyong Parkrespectively.
2522*54fd6939SJiyong Park
2523*54fd6939SJiyong ParkFrom outside TF-A, timestamps for individual CPUs can be retrieved by calling
2524*54fd6939SJiyong Parkinto ``pmf_smc_handler()``.
2525*54fd6939SJiyong Park
2526*54fd6939SJiyong Park::
2527*54fd6939SJiyong Park
2528*54fd6939SJiyong Park    Interface : pmf_smc_handler()
2529*54fd6939SJiyong Park    Argument  : unsigned int smc_fid, u_register_t x1,
2530*54fd6939SJiyong Park                u_register_t x2, u_register_t x3,
2531*54fd6939SJiyong Park                u_register_t x4, void *cookie,
2532*54fd6939SJiyong Park                void *handle, u_register_t flags
2533*54fd6939SJiyong Park    Return    : uintptr_t
2534*54fd6939SJiyong Park
2535*54fd6939SJiyong Park    smc_fid: Holds the SMC identifier which is either `PMF_SMC_GET_TIMESTAMP_32`
2536*54fd6939SJiyong Park        when the caller of the SMC is running in AArch32 mode
2537*54fd6939SJiyong Park        or `PMF_SMC_GET_TIMESTAMP_64` when the caller is running in AArch64 mode.
2538*54fd6939SJiyong Park    x1: Timestamp identifier.
2539*54fd6939SJiyong Park    x2: The `mpidr` of the CPU for which the timestamp has to be retrieved.
2540*54fd6939SJiyong Park        This can be the `mpidr` of a different core to the one initiating
2541*54fd6939SJiyong Park        the SMC.  In that case, service specific cache maintenance may be
2542*54fd6939SJiyong Park        required to ensure the updated copy of the timestamp is returned.
2543*54fd6939SJiyong Park    x3: A flags value that is either 0 or `PMF_CACHE_MAINT`.  If
2544*54fd6939SJiyong Park        `PMF_CACHE_MAINT` is passed, then the PMF code will perform a
2545*54fd6939SJiyong Park        cache invalidate before reading the timestamp.  This ensures
2546*54fd6939SJiyong Park        an updated copy is returned.
2547*54fd6939SJiyong Park
2548*54fd6939SJiyong ParkThe remaining arguments, ``x4``, ``cookie``, ``handle`` and ``flags`` are unused
2549*54fd6939SJiyong Parkin this implementation.
2550*54fd6939SJiyong Park
2551*54fd6939SJiyong ParkPMF code structure
2552*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~
2553*54fd6939SJiyong Park
2554*54fd6939SJiyong Park#. ``pmf_main.c`` consists of core functions that implement service registration,
2555*54fd6939SJiyong Park   initialization, storing, dumping and retrieving timestamps.
2556*54fd6939SJiyong Park
2557*54fd6939SJiyong Park#. ``pmf_smc.c`` contains the SMC handling for registered PMF services.
2558*54fd6939SJiyong Park
2559*54fd6939SJiyong Park#. ``pmf.h`` contains the public interface to Performance Measurement Framework.
2560*54fd6939SJiyong Park
2561*54fd6939SJiyong Park#. ``pmf_asm_macros.S`` consists of macros to facilitate capturing timestamps in
2562*54fd6939SJiyong Park   assembly code.
2563*54fd6939SJiyong Park
2564*54fd6939SJiyong Park#. ``pmf_helpers.h`` is an internal header used by ``pmf.h``.
2565*54fd6939SJiyong Park
2566*54fd6939SJiyong ParkArmv8-A Architecture Extensions
2567*54fd6939SJiyong Park-------------------------------
2568*54fd6939SJiyong Park
2569*54fd6939SJiyong ParkTF-A makes use of Armv8-A Architecture Extensions where applicable. This
2570*54fd6939SJiyong Parksection lists the usage of Architecture Extensions, and build flags
2571*54fd6939SJiyong Parkcontrolling them.
2572*54fd6939SJiyong Park
2573*54fd6939SJiyong ParkIn general, and unless individually mentioned, the build options
2574*54fd6939SJiyong Park``ARM_ARCH_MAJOR`` and ``ARM_ARCH_MINOR`` select the Architecture Extension to
2575*54fd6939SJiyong Parktarget when building TF-A. Subsequent Arm Architecture Extensions are backward
2576*54fd6939SJiyong Parkcompatible with previous versions.
2577*54fd6939SJiyong Park
2578*54fd6939SJiyong ParkThe build system only requires that ``ARM_ARCH_MAJOR`` and ``ARM_ARCH_MINOR`` have a
2579*54fd6939SJiyong Parkvalid numeric value. These build options only control whether or not
2580*54fd6939SJiyong ParkArchitecture Extension-specific code is included in the build. Otherwise, TF-A
2581*54fd6939SJiyong Parktargets the base Armv8.0-A architecture; i.e. as if ``ARM_ARCH_MAJOR`` == 8
2582*54fd6939SJiyong Parkand ``ARM_ARCH_MINOR`` == 0, which are also their respective default values.
2583*54fd6939SJiyong Park
2584*54fd6939SJiyong Park.. seealso:: :ref:`Build Options`
2585*54fd6939SJiyong Park
2586*54fd6939SJiyong ParkFor details on the Architecture Extension and available features, please refer
2587*54fd6939SJiyong Parkto the respective Architecture Extension Supplement.
2588*54fd6939SJiyong Park
2589*54fd6939SJiyong ParkArmv8.1-A
2590*54fd6939SJiyong Park~~~~~~~~~
2591*54fd6939SJiyong Park
2592*54fd6939SJiyong ParkThis Architecture Extension is targeted when ``ARM_ARCH_MAJOR`` >= 8, or when
2593*54fd6939SJiyong Park``ARM_ARCH_MAJOR`` == 8 and ``ARM_ARCH_MINOR`` >= 1.
2594*54fd6939SJiyong Park
2595*54fd6939SJiyong Park-  By default, a load-/store-exclusive instruction pair is used to implement
2596*54fd6939SJiyong Park   spinlocks. The ``USE_SPINLOCK_CAS`` build option when set to 1 selects the
2597*54fd6939SJiyong Park   spinlock implementation using the ARMv8.1-LSE Compare and Swap instruction.
2598*54fd6939SJiyong Park   Notice this instruction is only available in AArch64 execution state, so
2599*54fd6939SJiyong Park   the option is only available to AArch64 builds.
2600*54fd6939SJiyong Park
2601*54fd6939SJiyong ParkArmv8.2-A
2602*54fd6939SJiyong Park~~~~~~~~~
2603*54fd6939SJiyong Park
2604*54fd6939SJiyong Park-  The presence of ARMv8.2-TTCNP is detected at runtime. When it is present, the
2605*54fd6939SJiyong Park   Common not Private (TTBRn_ELx.CnP) bit is enabled to indicate that multiple
2606*54fd6939SJiyong Park   Processing Elements in the same Inner Shareable domain use the same
2607*54fd6939SJiyong Park   translation table entries for a given stage of translation for a particular
2608*54fd6939SJiyong Park   translation regime.
2609*54fd6939SJiyong Park
2610*54fd6939SJiyong ParkArmv8.3-A
2611*54fd6939SJiyong Park~~~~~~~~~
2612*54fd6939SJiyong Park
2613*54fd6939SJiyong Park-  Pointer authentication features of Armv8.3-A are unconditionally enabled in
2614*54fd6939SJiyong Park   the Non-secure world so that lower ELs are allowed to use them without
2615*54fd6939SJiyong Park   causing a trap to EL3.
2616*54fd6939SJiyong Park
2617*54fd6939SJiyong Park   In order to enable the Secure world to use it, ``CTX_INCLUDE_PAUTH_REGS``
2618*54fd6939SJiyong Park   must be set to 1. This will add all pointer authentication system registers
2619*54fd6939SJiyong Park   to the context that is saved when doing a world switch.
2620*54fd6939SJiyong Park
2621*54fd6939SJiyong Park   The TF-A itself has support for pointer authentication at runtime
2622*54fd6939SJiyong Park   that can be enabled by setting ``BRANCH_PROTECTION`` option to non-zero and
2623*54fd6939SJiyong Park   ``CTX_INCLUDE_PAUTH_REGS`` to 1. This enables pointer authentication in BL1,
2624*54fd6939SJiyong Park   BL2, BL31, and the TSP if it is used.
2625*54fd6939SJiyong Park
2626*54fd6939SJiyong Park   Note that Pointer Authentication is enabled for Non-secure world irrespective
2627*54fd6939SJiyong Park   of the value of these build flags if the CPU supports it.
2628*54fd6939SJiyong Park
2629*54fd6939SJiyong Park   If ``ARM_ARCH_MAJOR == 8`` and ``ARM_ARCH_MINOR >= 3`` the code footprint of
2630*54fd6939SJiyong Park   enabling PAuth is lower because the compiler will use the optimized
2631*54fd6939SJiyong Park   PAuth instructions rather than the backwards-compatible ones.
2632*54fd6939SJiyong Park
2633*54fd6939SJiyong ParkArmv8.5-A
2634*54fd6939SJiyong Park~~~~~~~~~
2635*54fd6939SJiyong Park
2636*54fd6939SJiyong Park-  Branch Target Identification feature is selected by ``BRANCH_PROTECTION``
2637*54fd6939SJiyong Park   option set to 1. This option defaults to 0.
2638*54fd6939SJiyong Park
2639*54fd6939SJiyong Park-  Memory Tagging Extension feature is unconditionally enabled for both worlds
2640*54fd6939SJiyong Park   (at EL0 and S-EL0) if it is only supported at EL0. If instead it is
2641*54fd6939SJiyong Park   implemented at all ELs, it is unconditionally enabled for only the normal
2642*54fd6939SJiyong Park   world. To enable it for the secure world as well, the build option
2643*54fd6939SJiyong Park   ``CTX_INCLUDE_MTE_REGS`` is required. If the hardware does not implement
2644*54fd6939SJiyong Park   MTE support at all, it is always disabled, no matter what build options
2645*54fd6939SJiyong Park   are used.
2646*54fd6939SJiyong Park
2647*54fd6939SJiyong ParkArmv7-A
2648*54fd6939SJiyong Park~~~~~~~
2649*54fd6939SJiyong Park
2650*54fd6939SJiyong ParkThis Architecture Extension is targeted when ``ARM_ARCH_MAJOR`` == 7.
2651*54fd6939SJiyong Park
2652*54fd6939SJiyong ParkThere are several Armv7-A extensions available. Obviously the TrustZone
2653*54fd6939SJiyong Parkextension is mandatory to support the TF-A bootloader and runtime services.
2654*54fd6939SJiyong Park
2655*54fd6939SJiyong ParkPlatform implementing an Armv7-A system can to define from its target
2656*54fd6939SJiyong ParkCortex-A architecture through ``ARM_CORTEX_A<X> = yes`` in their
2657*54fd6939SJiyong Park``platform.mk`` script. For example ``ARM_CORTEX_A15=yes`` for a
2658*54fd6939SJiyong ParkCortex-A15 target.
2659*54fd6939SJiyong Park
2660*54fd6939SJiyong ParkPlatform can also set ``ARM_WITH_NEON=yes`` to enable neon support.
2661*54fd6939SJiyong ParkNote that using neon at runtime has constraints on non secure world context.
2662*54fd6939SJiyong ParkTF-A does not yet provide VFP context management.
2663*54fd6939SJiyong Park
2664*54fd6939SJiyong ParkDirective ``ARM_CORTEX_A<x>`` and ``ARM_WITH_NEON`` are used to set
2665*54fd6939SJiyong Parkthe toolchain  target architecture directive.
2666*54fd6939SJiyong Park
2667*54fd6939SJiyong ParkPlatform may choose to not define straight the toolchain target architecture
2668*54fd6939SJiyong Parkdirective by defining ``MARCH32_DIRECTIVE``.
2669*54fd6939SJiyong ParkI.e:
2670*54fd6939SJiyong Park
2671*54fd6939SJiyong Park.. code:: make
2672*54fd6939SJiyong Park
2673*54fd6939SJiyong Park   MARCH32_DIRECTIVE := -mach=armv7-a
2674*54fd6939SJiyong Park
2675*54fd6939SJiyong ParkCode Structure
2676*54fd6939SJiyong Park--------------
2677*54fd6939SJiyong Park
2678*54fd6939SJiyong ParkTF-A code is logically divided between the three boot loader stages mentioned
2679*54fd6939SJiyong Parkin the previous sections. The code is also divided into the following
2680*54fd6939SJiyong Parkcategories (present as directories in the source code):
2681*54fd6939SJiyong Park
2682*54fd6939SJiyong Park-  **Platform specific.** Choice of architecture specific code depends upon
2683*54fd6939SJiyong Park   the platform.
2684*54fd6939SJiyong Park-  **Common code.** This is platform and architecture agnostic code.
2685*54fd6939SJiyong Park-  **Library code.** This code comprises of functionality commonly used by all
2686*54fd6939SJiyong Park   other code. The PSCI implementation and other EL3 runtime frameworks reside
2687*54fd6939SJiyong Park   as Library components.
2688*54fd6939SJiyong Park-  **Stage specific.** Code specific to a boot stage.
2689*54fd6939SJiyong Park-  **Drivers.**
2690*54fd6939SJiyong Park-  **Services.** EL3 runtime services (eg: SPD). Specific SPD services
2691*54fd6939SJiyong Park   reside in the ``services/spd`` directory (e.g. ``services/spd/tspd``).
2692*54fd6939SJiyong Park
2693*54fd6939SJiyong ParkEach boot loader stage uses code from one or more of the above mentioned
2694*54fd6939SJiyong Parkcategories. Based upon the above, the code layout looks like this:
2695*54fd6939SJiyong Park
2696*54fd6939SJiyong Park::
2697*54fd6939SJiyong Park
2698*54fd6939SJiyong Park    Directory    Used by BL1?    Used by BL2?    Used by BL31?
2699*54fd6939SJiyong Park    bl1          Yes             No              No
2700*54fd6939SJiyong Park    bl2          No              Yes             No
2701*54fd6939SJiyong Park    bl31         No              No              Yes
2702*54fd6939SJiyong Park    plat         Yes             Yes             Yes
2703*54fd6939SJiyong Park    drivers      Yes             No              Yes
2704*54fd6939SJiyong Park    common       Yes             Yes             Yes
2705*54fd6939SJiyong Park    lib          Yes             Yes             Yes
2706*54fd6939SJiyong Park    services     No              No              Yes
2707*54fd6939SJiyong Park
2708*54fd6939SJiyong ParkThe build system provides a non configurable build option IMAGE_BLx for each
2709*54fd6939SJiyong Parkboot loader stage (where x = BL stage). e.g. for BL1 , IMAGE_BL1 will be
2710*54fd6939SJiyong Parkdefined by the build system. This enables TF-A to compile certain code only
2711*54fd6939SJiyong Parkfor specific boot loader stages
2712*54fd6939SJiyong Park
2713*54fd6939SJiyong ParkAll assembler files have the ``.S`` extension. The linker source files for each
2714*54fd6939SJiyong Parkboot stage have the extension ``.ld.S``. These are processed by GCC to create the
2715*54fd6939SJiyong Parklinker scripts which have the extension ``.ld``.
2716*54fd6939SJiyong Park
2717*54fd6939SJiyong ParkFDTs provide a description of the hardware platform and are used by the Linux
2718*54fd6939SJiyong Parkkernel at boot time. These can be found in the ``fdts`` directory.
2719*54fd6939SJiyong Park
2720*54fd6939SJiyong Park.. rubric:: References
2721*54fd6939SJiyong Park
2722*54fd6939SJiyong Park-  `Trusted Board Boot Requirements CLIENT (TBBR-CLIENT) Armv8-A (ARM DEN0006D)`_
2723*54fd6939SJiyong Park
2724*54fd6939SJiyong Park-  `Power State Coordination Interface PDD`_
2725*54fd6939SJiyong Park
2726*54fd6939SJiyong Park-  `SMC Calling Convention`_
2727*54fd6939SJiyong Park
2728*54fd6939SJiyong Park-  :ref:`Interrupt Management Framework`
2729*54fd6939SJiyong Park
2730*54fd6939SJiyong Park--------------
2731*54fd6939SJiyong Park
2732*54fd6939SJiyong Park*Copyright (c) 2013-2021, Arm Limited and Contributors. All rights reserved.*
2733*54fd6939SJiyong Park
2734*54fd6939SJiyong Park.. _Power State Coordination Interface PDD: http://infocenter.arm.com/help/topic/com.arm.doc.den0022d/Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
2735*54fd6939SJiyong Park.. _SMCCC: https://developer.arm.com/docs/den0028/latest
2736*54fd6939SJiyong Park.. _PSCI: http://infocenter.arm.com/help/topic/com.arm.doc.den0022d/Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
2737*54fd6939SJiyong Park.. _Power State Coordination Interface PDD: http://infocenter.arm.com/help/topic/com.arm.doc.den0022d/Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
2738*54fd6939SJiyong Park.. _Arm ARM: https://developer.arm.com/docs/ddi0487/latest
2739*54fd6939SJiyong Park.. _SMC Calling Convention: https://developer.arm.com/docs/den0028/latest
2740*54fd6939SJiyong Park.. _Trusted Board Boot Requirements CLIENT (TBBR-CLIENT) Armv8-A (ARM DEN0006D): https://developer.arm.com/docs/den0006/latest/trusted-board-boot-requirements-client-tbbr-client-armv8-a
2741*54fd6939SJiyong Park.. _Arm Confidential Compute Architecture (Arm CCA): https://www.arm.com/why-arm/architecture/security-features/arm-confidential-compute-architecture
2742*54fd6939SJiyong Park
2743*54fd6939SJiyong Park.. |Image 1| image:: ../resources/diagrams/rt-svc-descs-layout.png
2744