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