xref: /arm-trusted-firmware/docs/design/firmware-design.rst (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700SchasingluluFirmware Design
2*91f16700Schasinglulu===============
3*91f16700Schasinglulu
4*91f16700SchasingluluTrusted Firmware-A (TF-A) implements a subset of the Trusted Board Boot
5*91f16700SchasingluluRequirements (TBBR) Platform Design Document (PDD) for Arm reference
6*91f16700Schasingluluplatforms.
7*91f16700Schasinglulu
8*91f16700SchasingluluThe TBB sequence starts when the platform is powered on and runs up
9*91f16700Schasingluluto the stage where it hands-off control to firmware running in the normal
10*91f16700Schasingluluworld in DRAM. This is the cold boot path.
11*91f16700Schasinglulu
12*91f16700SchasingluluTF-A also implements the `PSCI`_ as a runtime service. PSCI is the interface
13*91f16700Schasinglulufrom normal world software to firmware implementing power management use-cases
14*91f16700Schasinglulu(for example, secondary CPU boot, hotplug and idle). Normal world software can
15*91f16700Schasingluluaccess TF-A runtime services via the Arm SMC (Secure Monitor Call) instruction.
16*91f16700SchasingluluThe SMC instruction must be used as mandated by the SMC Calling Convention
17*91f16700Schasinglulu(`SMCCC`_).
18*91f16700Schasinglulu
19*91f16700SchasingluluTF-A implements a framework for configuring and managing interrupts generated
20*91f16700Schasingluluin either security state. The details of the interrupt management framework
21*91f16700Schasingluluand its design can be found in :ref:`Interrupt Management Framework`.
22*91f16700Schasinglulu
23*91f16700SchasingluluTF-A also implements a library for setting up and managing the translation
24*91f16700Schasinglulutables. The details of this library can be found in
25*91f16700Schasinglulu:ref:`Translation (XLAT) Tables Library`.
26*91f16700Schasinglulu
27*91f16700SchasingluluTF-A can be built to support either AArch64 or AArch32 execution state.
28*91f16700Schasinglulu
29*91f16700Schasinglulu.. note::
30*91f16700Schasinglulu    The descriptions in this chapter are for the Arm TrustZone architecture.
31*91f16700Schasinglulu    For changes to the firmware design for the `Arm Confidential Compute
32*91f16700Schasinglulu    Architecture (Arm CCA)`_ please refer to the chapter :ref:`Realm Management
33*91f16700Schasinglulu    Extension (RME)`.
34*91f16700Schasinglulu
35*91f16700SchasingluluCold boot
36*91f16700Schasinglulu---------
37*91f16700Schasinglulu
38*91f16700SchasingluluThe cold boot path starts when the platform is physically turned on. If
39*91f16700Schasinglulu``COLD_BOOT_SINGLE_CPU=0``, one of the CPUs released from reset is chosen as the
40*91f16700Schasingluluprimary CPU, and the remaining CPUs are considered secondary CPUs. The primary
41*91f16700SchasingluluCPU is chosen through platform-specific means. The cold boot path is mainly
42*91f16700Schasingluluexecuted by the primary CPU, other than essential CPU initialization executed by
43*91f16700Schasingluluall CPUs. The secondary CPUs are kept in a safe platform-specific state until
44*91f16700Schasingluluthe primary CPU has performed enough initialization to boot them.
45*91f16700Schasinglulu
46*91f16700SchasingluluRefer to the :ref:`CPU Reset` for more information on the effect of the
47*91f16700Schasinglulu``COLD_BOOT_SINGLE_CPU`` platform build option.
48*91f16700Schasinglulu
49*91f16700SchasingluluThe cold boot path in this implementation of TF-A depends on the execution
50*91f16700Schasinglulustate. For AArch64, it is divided into five steps (in order of execution):
51*91f16700Schasinglulu
52*91f16700Schasinglulu-  Boot Loader stage 1 (BL1) *AP Trusted ROM*
53*91f16700Schasinglulu-  Boot Loader stage 2 (BL2) *Trusted Boot Firmware*
54*91f16700Schasinglulu-  Boot Loader stage 3-1 (BL31) *EL3 Runtime Software*
55*91f16700Schasinglulu-  Boot Loader stage 3-2 (BL32) *Secure-EL1 Payload* (optional)
56*91f16700Schasinglulu-  Boot Loader stage 3-3 (BL33) *Non-trusted Firmware*
57*91f16700Schasinglulu
58*91f16700SchasingluluFor AArch32, it is divided into four steps (in order of execution):
59*91f16700Schasinglulu
60*91f16700Schasinglulu-  Boot Loader stage 1 (BL1) *AP Trusted ROM*
61*91f16700Schasinglulu-  Boot Loader stage 2 (BL2) *Trusted Boot Firmware*
62*91f16700Schasinglulu-  Boot Loader stage 3-2 (BL32) *EL3 Runtime Software*
63*91f16700Schasinglulu-  Boot Loader stage 3-3 (BL33) *Non-trusted Firmware*
64*91f16700Schasinglulu
65*91f16700SchasingluluArm development platforms (Fixed Virtual Platforms (FVPs) and Juno) implement a
66*91f16700Schasinglulucombination of the following types of memory regions. Each bootloader stage uses
67*91f16700Schasingluluone or more of these memory regions.
68*91f16700Schasinglulu
69*91f16700Schasinglulu-  Regions accessible from both non-secure and secure states. For example,
70*91f16700Schasinglulu   non-trusted SRAM, ROM and DRAM.
71*91f16700Schasinglulu-  Regions accessible from only the secure state. For example, trusted SRAM and
72*91f16700Schasinglulu   ROM. The FVPs also implement the trusted DRAM which is statically
73*91f16700Schasinglulu   configured. Additionally, the Base FVPs and Juno development platform
74*91f16700Schasinglulu   configure the TrustZone Controller (TZC) to create a region in the DRAM
75*91f16700Schasinglulu   which is accessible only from the secure state.
76*91f16700Schasinglulu
77*91f16700SchasingluluThe sections below provide the following details:
78*91f16700Schasinglulu
79*91f16700Schasinglulu-  dynamic configuration of Boot Loader stages
80*91f16700Schasinglulu-  initialization and execution of the first three stages during cold boot
81*91f16700Schasinglulu-  specification of the EL3 Runtime Software (BL31 for AArch64 and BL32 for
82*91f16700Schasinglulu   AArch32) entrypoint requirements for use by alternative Trusted Boot
83*91f16700Schasinglulu   Firmware in place of the provided BL1 and BL2
84*91f16700Schasinglulu
85*91f16700SchasingluluDynamic Configuration during cold boot
86*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87*91f16700Schasinglulu
88*91f16700SchasingluluEach of the Boot Loader stages may be dynamically configured if required by the
89*91f16700Schasingluluplatform. The Boot Loader stage may optionally specify a firmware
90*91f16700Schasingluluconfiguration file and/or hardware configuration file as listed below:
91*91f16700Schasinglulu
92*91f16700Schasinglulu-  FW_CONFIG - The firmware configuration file. Holds properties shared across
93*91f16700Schasinglulu   all BLx images.
94*91f16700Schasinglulu   An example is the "dtb-registry" node, which contains the information about
95*91f16700Schasinglulu   the other device tree configurations (load-address, size, image_id).
96*91f16700Schasinglulu-  HW_CONFIG - The hardware configuration file. Can be shared by all Boot Loader
97*91f16700Schasinglulu   stages and also by the Normal World Rich OS.
98*91f16700Schasinglulu-  TB_FW_CONFIG - Trusted Boot Firmware configuration file. Shared between BL1
99*91f16700Schasinglulu   and BL2.
100*91f16700Schasinglulu-  SOC_FW_CONFIG - SoC Firmware configuration file. Used by BL31.
101*91f16700Schasinglulu-  TOS_FW_CONFIG - Trusted OS Firmware configuration file. Used by Trusted OS
102*91f16700Schasinglulu   (BL32).
103*91f16700Schasinglulu-  NT_FW_CONFIG - Non Trusted Firmware configuration file. Used by Non-trusted
104*91f16700Schasinglulu   firmware (BL33).
105*91f16700Schasinglulu
106*91f16700SchasingluluThe Arm development platforms use the Flattened Device Tree format for the
107*91f16700Schasingluludynamic configuration files.
108*91f16700Schasinglulu
109*91f16700SchasingluluEach Boot Loader stage can pass up to 4 arguments via registers to the next
110*91f16700Schasinglulustage.  BL2 passes the list of the next images to execute to the *EL3 Runtime
111*91f16700SchasingluluSoftware* (BL31 for AArch64 and BL32 for AArch32) via `arg0`. All the other
112*91f16700Schasingluluarguments are platform defined. The Arm development platforms use the following
113*91f16700Schasingluluconvention:
114*91f16700Schasinglulu
115*91f16700Schasinglulu-  BL1 passes the address of a meminfo_t structure to BL2 via ``arg1``. This
116*91f16700Schasinglulu   structure contains the memory layout available to BL2.
117*91f16700Schasinglulu-  When dynamic configuration files are present, the firmware configuration for
118*91f16700Schasinglulu   the next Boot Loader stage is populated in the first available argument and
119*91f16700Schasinglulu   the generic hardware configuration is passed the next available argument.
120*91f16700Schasinglulu   For example,
121*91f16700Schasinglulu
122*91f16700Schasinglulu   -  FW_CONFIG is loaded by BL1, then its address is passed in ``arg0`` to BL2.
123*91f16700Schasinglulu   -  TB_FW_CONFIG address is retrieved by BL2 from FW_CONFIG device tree.
124*91f16700Schasinglulu   -  If HW_CONFIG is loaded by BL1, then its address is passed in ``arg2`` to
125*91f16700Schasinglulu      BL2. Note, ``arg1`` is already used for meminfo_t.
126*91f16700Schasinglulu   -  If SOC_FW_CONFIG is loaded by BL2, then its address is passed in ``arg1``
127*91f16700Schasinglulu      to BL31. Note, ``arg0`` is used to pass the list of executable images.
128*91f16700Schasinglulu   -  Similarly, if HW_CONFIG is loaded by BL1 or BL2, then its address is
129*91f16700Schasinglulu      passed in ``arg2`` to BL31.
130*91f16700Schasinglulu   -  For other BL3x images, if the firmware configuration file is loaded by
131*91f16700Schasinglulu      BL2, then its address is passed in ``arg0`` and if HW_CONFIG is loaded
132*91f16700Schasinglulu      then its address is passed in ``arg1``.
133*91f16700Schasinglulu   -  In case SPMC_AT_EL3 is enabled, populate the BL32 image base, size and max
134*91f16700Schasinglulu      limit in the entry point information, since there is no platform function
135*91f16700Schasinglulu      to retrieve these in generic code. We choose ``arg2``, ``arg3`` and
136*91f16700Schasinglulu      ``arg4`` since the generic code uses ``arg1`` for stashing the SP manifest
137*91f16700Schasinglulu      size. The SPMC setup uses these arguments to update SP manifest with
138*91f16700Schasinglulu      actual SP's base address and it size.
139*91f16700Schasinglulu   -  In case of the Arm FVP platform, FW_CONFIG address passed in ``arg1`` to
140*91f16700Schasinglulu      BL31/SP_MIN, and the SOC_FW_CONFIG and HW_CONFIG details are retrieved
141*91f16700Schasinglulu      from FW_CONFIG device tree.
142*91f16700Schasinglulu
143*91f16700SchasingluluBL1
144*91f16700Schasinglulu~~~
145*91f16700Schasinglulu
146*91f16700SchasingluluThis stage begins execution from the platform's reset vector at EL3. The reset
147*91f16700Schasingluluaddress is platform dependent but it is usually located in a Trusted ROM area.
148*91f16700SchasingluluThe BL1 data section is copied to trusted SRAM at runtime.
149*91f16700Schasinglulu
150*91f16700SchasingluluOn the Arm development platforms, BL1 code starts execution from the reset
151*91f16700Schasingluluvector defined by the constant ``BL1_RO_BASE``. The BL1 data section is copied
152*91f16700Schasingluluto the top of trusted SRAM as defined by the constant ``BL1_RW_BASE``.
153*91f16700Schasinglulu
154*91f16700SchasingluluThe functionality implemented by this stage is as follows.
155*91f16700Schasinglulu
156*91f16700SchasingluluDetermination of boot path
157*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^
158*91f16700Schasinglulu
159*91f16700SchasingluluWhenever a CPU is released from reset, BL1 needs to distinguish between a warm
160*91f16700Schasingluluboot and a cold boot. This is done using platform-specific mechanisms (see the
161*91f16700Schasinglulu``plat_get_my_entrypoint()`` function in the :ref:`Porting Guide`). In the case
162*91f16700Schasingluluof a warm boot, a CPU is expected to continue execution from a separate
163*91f16700Schasingluluentrypoint. In the case of a cold boot, the secondary CPUs are placed in a safe
164*91f16700Schasingluluplatform-specific state (see the ``plat_secondary_cold_boot_setup()`` function in
165*91f16700Schasingluluthe :ref:`Porting Guide`) while the primary CPU executes the remaining cold boot
166*91f16700Schasinglulupath as described in the following sections.
167*91f16700Schasinglulu
168*91f16700SchasingluluThis step only applies when ``PROGRAMMABLE_RESET_ADDRESS=0``. Refer to the
169*91f16700Schasinglulu:ref:`CPU Reset` for more information on the effect of the
170*91f16700Schasinglulu``PROGRAMMABLE_RESET_ADDRESS`` platform build option.
171*91f16700Schasinglulu
172*91f16700SchasingluluArchitectural initialization
173*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^
174*91f16700Schasinglulu
175*91f16700SchasingluluBL1 performs minimal architectural initialization as follows.
176*91f16700Schasinglulu
177*91f16700Schasinglulu-  Exception vectors
178*91f16700Schasinglulu
179*91f16700Schasinglulu   BL1 sets up simple exception vectors for both synchronous and asynchronous
180*91f16700Schasinglulu   exceptions. The default behavior upon receiving an exception is to populate
181*91f16700Schasinglulu   a status code in the general purpose register ``X0/R0`` and call the
182*91f16700Schasinglulu   ``plat_report_exception()`` function (see the :ref:`Porting Guide`). The
183*91f16700Schasinglulu   status code is one of:
184*91f16700Schasinglulu
185*91f16700Schasinglulu   For AArch64:
186*91f16700Schasinglulu
187*91f16700Schasinglulu   ::
188*91f16700Schasinglulu
189*91f16700Schasinglulu       0x0 : Synchronous exception from Current EL with SP_EL0
190*91f16700Schasinglulu       0x1 : IRQ exception from Current EL with SP_EL0
191*91f16700Schasinglulu       0x2 : FIQ exception from Current EL with SP_EL0
192*91f16700Schasinglulu       0x3 : System Error exception from Current EL with SP_EL0
193*91f16700Schasinglulu       0x4 : Synchronous exception from Current EL with SP_ELx
194*91f16700Schasinglulu       0x5 : IRQ exception from Current EL with SP_ELx
195*91f16700Schasinglulu       0x6 : FIQ exception from Current EL with SP_ELx
196*91f16700Schasinglulu       0x7 : System Error exception from Current EL with SP_ELx
197*91f16700Schasinglulu       0x8 : Synchronous exception from Lower EL using aarch64
198*91f16700Schasinglulu       0x9 : IRQ exception from Lower EL using aarch64
199*91f16700Schasinglulu       0xa : FIQ exception from Lower EL using aarch64
200*91f16700Schasinglulu       0xb : System Error exception from Lower EL using aarch64
201*91f16700Schasinglulu       0xc : Synchronous exception from Lower EL using aarch32
202*91f16700Schasinglulu       0xd : IRQ exception from Lower EL using aarch32
203*91f16700Schasinglulu       0xe : FIQ exception from Lower EL using aarch32
204*91f16700Schasinglulu       0xf : System Error exception from Lower EL using aarch32
205*91f16700Schasinglulu
206*91f16700Schasinglulu   For AArch32:
207*91f16700Schasinglulu
208*91f16700Schasinglulu   ::
209*91f16700Schasinglulu
210*91f16700Schasinglulu       0x10 : User mode
211*91f16700Schasinglulu       0x11 : FIQ mode
212*91f16700Schasinglulu       0x12 : IRQ mode
213*91f16700Schasinglulu       0x13 : SVC mode
214*91f16700Schasinglulu       0x16 : Monitor mode
215*91f16700Schasinglulu       0x17 : Abort mode
216*91f16700Schasinglulu       0x1a : Hypervisor mode
217*91f16700Schasinglulu       0x1b : Undefined mode
218*91f16700Schasinglulu       0x1f : System mode
219*91f16700Schasinglulu
220*91f16700Schasinglulu   The ``plat_report_exception()`` implementation on the Arm FVP port programs
221*91f16700Schasinglulu   the Versatile Express System LED register in the following format to
222*91f16700Schasinglulu   indicate the occurrence of an unexpected exception:
223*91f16700Schasinglulu
224*91f16700Schasinglulu   ::
225*91f16700Schasinglulu
226*91f16700Schasinglulu       SYS_LED[0]   - Security state (Secure=0/Non-Secure=1)
227*91f16700Schasinglulu       SYS_LED[2:1] - Exception Level (EL3=0x3, EL2=0x2, EL1=0x1, EL0=0x0)
228*91f16700Schasinglulu                      For AArch32 it is always 0x0
229*91f16700Schasinglulu       SYS_LED[7:3] - Exception Class (Sync/Async & origin). This is the value
230*91f16700Schasinglulu                      of the status code
231*91f16700Schasinglulu
232*91f16700Schasinglulu   A write to the LED register reflects in the System LEDs (S6LED0..7) in the
233*91f16700Schasinglulu   CLCD window of the FVP.
234*91f16700Schasinglulu
235*91f16700Schasinglulu   BL1 does not expect to receive any exceptions other than the SMC exception.
236*91f16700Schasinglulu   For the latter, BL1 installs a simple stub. The stub expects to receive a
237*91f16700Schasinglulu   limited set of SMC types (determined by their function IDs in the general
238*91f16700Schasinglulu   purpose register ``X0/R0``):
239*91f16700Schasinglulu
240*91f16700Schasinglulu   -  ``BL1_SMC_RUN_IMAGE``: This SMC is raised by BL2 to make BL1 pass control
241*91f16700Schasinglulu      to EL3 Runtime Software.
242*91f16700Schasinglulu   -  All SMCs listed in section "BL1 SMC Interface" in the :ref:`Firmware Update (FWU)`
243*91f16700Schasinglulu      Design Guide are supported for AArch64 only. These SMCs are currently
244*91f16700Schasinglulu      not supported when BL1 is built for AArch32.
245*91f16700Schasinglulu
246*91f16700Schasinglulu   Any other SMC leads to an assertion failure.
247*91f16700Schasinglulu
248*91f16700Schasinglulu-  CPU initialization
249*91f16700Schasinglulu
250*91f16700Schasinglulu   BL1 calls the ``reset_handler()`` function which in turn calls the CPU
251*91f16700Schasinglulu   specific reset handler function (see the section: "CPU specific operations
252*91f16700Schasinglulu   framework").
253*91f16700Schasinglulu
254*91f16700SchasingluluPlatform initialization
255*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^
256*91f16700Schasinglulu
257*91f16700SchasingluluOn Arm platforms, BL1 performs the following platform initializations:
258*91f16700Schasinglulu
259*91f16700Schasinglulu-  Enable the Trusted Watchdog.
260*91f16700Schasinglulu-  Initialize the console.
261*91f16700Schasinglulu-  Configure the Interconnect to enable hardware coherency.
262*91f16700Schasinglulu-  Enable the MMU and map the memory it needs to access.
263*91f16700Schasinglulu-  Configure any required platform storage to load the next bootloader image
264*91f16700Schasinglulu   (BL2).
265*91f16700Schasinglulu-  If the BL1 dynamic configuration file, ``TB_FW_CONFIG``, is available, then
266*91f16700Schasinglulu   load it to the platform defined address and make it available to BL2 via
267*91f16700Schasinglulu   ``arg0``.
268*91f16700Schasinglulu-  Configure the system timer and program the `CNTFRQ_EL0` for use by NS-BL1U
269*91f16700Schasinglulu   and NS-BL2U firmware update images.
270*91f16700Schasinglulu
271*91f16700SchasingluluFirmware Update detection and execution
272*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
273*91f16700Schasinglulu
274*91f16700SchasingluluAfter performing platform setup, BL1 common code calls
275*91f16700Schasinglulu``bl1_plat_get_next_image_id()`` to determine if :ref:`Firmware Update (FWU)` is
276*91f16700Schasinglulurequired or to proceed with the normal boot process. If the platform code
277*91f16700Schasinglulureturns ``BL2_IMAGE_ID`` then the normal boot sequence is executed as described
278*91f16700Schasingluluin the next section, else BL1 assumes that :ref:`Firmware Update (FWU)` is
279*91f16700Schasinglulurequired and execution passes to the first image in the
280*91f16700Schasinglulu:ref:`Firmware Update (FWU)` process. In either case, BL1 retrieves a descriptor
281*91f16700Schasingluluof the next image by calling ``bl1_plat_get_image_desc()``. The image descriptor
282*91f16700Schasinglulucontains an ``entry_point_info_t`` structure, which BL1 uses to initialize the
283*91f16700Schasingluluexecution state of the next image.
284*91f16700Schasinglulu
285*91f16700SchasingluluBL2 image load and execution
286*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287*91f16700Schasinglulu
288*91f16700SchasingluluIn the normal boot flow, BL1 execution continues as follows:
289*91f16700Schasinglulu
290*91f16700Schasinglulu#. BL1 prints the following string from the primary CPU to indicate successful
291*91f16700Schasinglulu   execution of the BL1 stage:
292*91f16700Schasinglulu
293*91f16700Schasinglulu   ::
294*91f16700Schasinglulu
295*91f16700Schasinglulu       "Booting Trusted Firmware"
296*91f16700Schasinglulu
297*91f16700Schasinglulu#. BL1 loads a BL2 raw binary image from platform storage, at a
298*91f16700Schasinglulu   platform-specific base address. Prior to the load, BL1 invokes
299*91f16700Schasinglulu   ``bl1_plat_handle_pre_image_load()`` which allows the platform to update or
300*91f16700Schasinglulu   use the image information. If the BL2 image file is not present or if
301*91f16700Schasinglulu   there is not enough free trusted SRAM the following error message is
302*91f16700Schasinglulu   printed:
303*91f16700Schasinglulu
304*91f16700Schasinglulu   ::
305*91f16700Schasinglulu
306*91f16700Schasinglulu       "Failed to load BL2 firmware."
307*91f16700Schasinglulu
308*91f16700Schasinglulu#. BL1 invokes ``bl1_plat_handle_post_image_load()`` which again is intended
309*91f16700Schasinglulu   for platforms to take further action after image load. This function must
310*91f16700Schasinglulu   populate the necessary arguments for BL2, which may also include the memory
311*91f16700Schasinglulu   layout. Further description of the memory layout can be found later
312*91f16700Schasinglulu   in this document.
313*91f16700Schasinglulu
314*91f16700Schasinglulu#. BL1 passes control to the BL2 image at Secure EL1 (for AArch64) or at
315*91f16700Schasinglulu   Secure SVC mode (for AArch32), starting from its load address.
316*91f16700Schasinglulu
317*91f16700SchasingluluBL2
318*91f16700Schasinglulu~~~
319*91f16700Schasinglulu
320*91f16700SchasingluluBL1 loads and passes control to BL2 at Secure-EL1 (for AArch64) or at Secure
321*91f16700SchasingluluSVC mode (for AArch32) . BL2 is linked against and loaded at a platform-specific
322*91f16700Schasinglulubase address (more information can be found later in this document).
323*91f16700SchasingluluThe functionality implemented by BL2 is as follows.
324*91f16700Schasinglulu
325*91f16700SchasingluluArchitectural initialization
326*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^
327*91f16700Schasinglulu
328*91f16700SchasingluluFor AArch64, BL2 performs the minimal architectural initialization required
329*91f16700Schasinglulufor subsequent stages of TF-A and normal world software. EL1 and EL0 are given
330*91f16700Schasingluluaccess to Floating Point and Advanced SIMD registers by setting the
331*91f16700Schasinglulu``CPACR.FPEN`` bits.
332*91f16700Schasinglulu
333*91f16700SchasingluluFor AArch32, the minimal architectural initialization required for subsequent
334*91f16700Schasinglulustages of TF-A and normal world software is taken care of in BL1 as both BL1
335*91f16700Schasingluluand BL2 execute at PL1.
336*91f16700Schasinglulu
337*91f16700SchasingluluPlatform initialization
338*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^
339*91f16700Schasinglulu
340*91f16700SchasingluluOn Arm platforms, BL2 performs the following platform initializations:
341*91f16700Schasinglulu
342*91f16700Schasinglulu-  Initialize the console.
343*91f16700Schasinglulu-  Configure any required platform storage to allow loading further bootloader
344*91f16700Schasinglulu   images.
345*91f16700Schasinglulu-  Enable the MMU and map the memory it needs to access.
346*91f16700Schasinglulu-  Perform platform security setup to allow access to controlled components.
347*91f16700Schasinglulu-  Reserve some memory for passing information to the next bootloader image
348*91f16700Schasinglulu   EL3 Runtime Software and populate it.
349*91f16700Schasinglulu-  Define the extents of memory available for loading each subsequent
350*91f16700Schasinglulu   bootloader image.
351*91f16700Schasinglulu-  If BL1 has passed TB_FW_CONFIG dynamic configuration file in ``arg0``,
352*91f16700Schasinglulu   then parse it.
353*91f16700Schasinglulu
354*91f16700SchasingluluImage loading in BL2
355*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^
356*91f16700Schasinglulu
357*91f16700SchasingluluBL2 generic code loads the images based on the list of loadable images
358*91f16700Schasingluluprovided by the platform. BL2 passes the list of executable images
359*91f16700Schasingluluprovided by the platform to the next handover BL image.
360*91f16700Schasinglulu
361*91f16700SchasingluluThe list of loadable images provided by the platform may also contain
362*91f16700Schasingluludynamic configuration files. The files are loaded and can be parsed as
363*91f16700Schasingluluneeded in the ``bl2_plat_handle_post_image_load()`` function. These
364*91f16700Schasingluluconfiguration files can be passed to next Boot Loader stages as arguments
365*91f16700Schasingluluby updating the corresponding entrypoint information in this function.
366*91f16700Schasinglulu
367*91f16700SchasingluluSCP_BL2 (System Control Processor Firmware) image load
368*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
369*91f16700Schasinglulu
370*91f16700SchasingluluSome systems have a separate System Control Processor (SCP) for power, clock,
371*91f16700Schasinglulureset and system control. BL2 loads the optional SCP_BL2 image from platform
372*91f16700Schasinglulustorage into a platform-specific region of secure memory. The subsequent
373*91f16700Schasingluluhandling of SCP_BL2 is platform specific. For example, on the Juno Arm
374*91f16700Schasingluludevelopment platform port the image is transferred into SCP's internal memory
375*91f16700Schasingluluusing the Boot Over MHU (BOM) protocol after being loaded in the trusted SRAM
376*91f16700Schasinglulumemory. The SCP executes SCP_BL2 and signals to the Application Processor (AP)
377*91f16700Schasinglulufor BL2 execution to continue.
378*91f16700Schasinglulu
379*91f16700SchasingluluEL3 Runtime Software image load
380*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
381*91f16700Schasinglulu
382*91f16700SchasingluluBL2 loads the EL3 Runtime Software image from platform storage into a platform-
383*91f16700Schasingluluspecific address in trusted SRAM. If there is not enough memory to load the
384*91f16700Schasingluluimage or image is missing it leads to an assertion failure.
385*91f16700Schasinglulu
386*91f16700SchasingluluAArch64 BL32 (Secure-EL1 Payload) image load
387*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388*91f16700Schasinglulu
389*91f16700SchasingluluBL2 loads the optional BL32 image from platform storage into a platform-
390*91f16700Schasingluluspecific region of secure memory. The image executes in the secure world. BL2
391*91f16700Schasinglulurelies on BL31 to pass control to the BL32 image, if present. Hence, BL2
392*91f16700Schasinglulupopulates a platform-specific area of memory with the entrypoint/load-address
393*91f16700Schasingluluof the BL32 image. The value of the Saved Processor Status Register (``SPSR``)
394*91f16700Schasinglulufor entry into BL32 is not determined by BL2, it is initialized by the
395*91f16700SchasingluluSecure-EL1 Payload Dispatcher (see later) within BL31, which is responsible for
396*91f16700Schasinglulumanaging interaction with BL32. This information is passed to BL31.
397*91f16700Schasinglulu
398*91f16700SchasingluluBL33 (Non-trusted Firmware) image load
399*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
400*91f16700Schasinglulu
401*91f16700SchasingluluBL2 loads the BL33 image (e.g. UEFI or other test or boot software) from
402*91f16700Schasingluluplatform storage into non-secure memory as defined by the platform.
403*91f16700Schasinglulu
404*91f16700SchasingluluBL2 relies on EL3 Runtime Software to pass control to BL33 once secure state
405*91f16700Schasingluluinitialization is complete. Hence, BL2 populates a platform-specific area of
406*91f16700Schasinglulumemory with the entrypoint and Saved Program Status Register (``SPSR``) of the
407*91f16700Schasinglulunormal world software image. The entrypoint is the load address of the BL33
408*91f16700Schasingluluimage. The ``SPSR`` is determined as specified in Section 5.13 of the
409*91f16700Schasinglulu`PSCI`_. This information is passed to the EL3 Runtime Software.
410*91f16700Schasinglulu
411*91f16700SchasingluluAArch64 BL31 (EL3 Runtime Software) execution
412*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
413*91f16700Schasinglulu
414*91f16700SchasingluluBL2 execution continues as follows:
415*91f16700Schasinglulu
416*91f16700Schasinglulu#. BL2 passes control back to BL1 by raising an SMC, providing BL1 with the
417*91f16700Schasinglulu   BL31 entrypoint. The exception is handled by the SMC exception handler
418*91f16700Schasinglulu   installed by BL1.
419*91f16700Schasinglulu
420*91f16700Schasinglulu#. BL1 turns off the MMU and flushes the caches. It clears the
421*91f16700Schasinglulu   ``SCTLR_EL3.M/I/C`` bits, flushes the data cache to the point of coherency
422*91f16700Schasinglulu   and invalidates the TLBs.
423*91f16700Schasinglulu
424*91f16700Schasinglulu#. BL1 passes control to BL31 at the specified entrypoint at EL3.
425*91f16700Schasinglulu
426*91f16700SchasingluluRunning BL2 at EL3 execution level
427*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
428*91f16700Schasinglulu
429*91f16700SchasingluluSome platforms have a non-TF-A Boot ROM that expects the next boot stage
430*91f16700Schasingluluto execute at EL3. On these platforms, TF-A BL1 is a waste of memory
431*91f16700Schasingluluas its only purpose is to ensure TF-A BL2 is entered at S-EL1. To avoid
432*91f16700Schasingluluthis waste, a special mode enables BL2 to execute at EL3, which allows
433*91f16700Schasinglulua non-TF-A Boot ROM to load and jump directly to BL2. This mode is selected
434*91f16700Schasingluluwhen the build flag RESET_TO_BL2 is enabled.
435*91f16700SchasingluluThe main differences in this mode are:
436*91f16700Schasinglulu
437*91f16700Schasinglulu#. BL2 includes the reset code and the mailbox mechanism to differentiate
438*91f16700Schasinglulu   cold boot and warm boot. It runs at EL3 doing the arch
439*91f16700Schasinglulu   initialization required for EL3.
440*91f16700Schasinglulu
441*91f16700Schasinglulu#. BL2 does not receive the meminfo information from BL1 anymore. This
442*91f16700Schasinglulu   information can be passed by the Boot ROM or be internal to the
443*91f16700Schasinglulu   BL2 image.
444*91f16700Schasinglulu
445*91f16700Schasinglulu#. Since BL2 executes at EL3, BL2 jumps directly to the next image,
446*91f16700Schasinglulu   instead of invoking the RUN_IMAGE SMC call.
447*91f16700Schasinglulu
448*91f16700Schasinglulu
449*91f16700SchasingluluWe assume 3 different types of BootROM support on the platform:
450*91f16700Schasinglulu
451*91f16700Schasinglulu#. The Boot ROM always jumps to the same address, for both cold
452*91f16700Schasinglulu   and warm boot. In this case, we will need to keep a resident part
453*91f16700Schasinglulu   of BL2 whose memory cannot be reclaimed by any other image. The
454*91f16700Schasinglulu   linker script defines the symbols __TEXT_RESIDENT_START__ and
455*91f16700Schasinglulu   __TEXT_RESIDENT_END__ that allows the platform to configure
456*91f16700Schasinglulu   correctly the memory map.
457*91f16700Schasinglulu#. The platform has some mechanism to indicate the jump address to the
458*91f16700Schasinglulu   Boot ROM. Platform code can then program the jump address with
459*91f16700Schasinglulu   psci_warmboot_entrypoint during cold boot.
460*91f16700Schasinglulu#. The platform has some mechanism to program the reset address using
461*91f16700Schasinglulu   the PROGRAMMABLE_RESET_ADDRESS feature. Platform code can then
462*91f16700Schasinglulu   program the reset address with psci_warmboot_entrypoint during
463*91f16700Schasinglulu   cold boot, bypassing the boot ROM for warm boot.
464*91f16700Schasinglulu
465*91f16700SchasingluluIn the last 2 cases, no part of BL2 needs to remain resident at
466*91f16700Schasingluluruntime. In the first 2 cases, we expect the Boot ROM to be able to
467*91f16700Schasingluludifferentiate between warm and cold boot, to avoid loading BL2 again
468*91f16700Schasingluluduring warm boot.
469*91f16700Schasinglulu
470*91f16700SchasingluluThis functionality can be tested with FVP loading the image directly
471*91f16700Schasingluluin memory and changing the address where the system jumps at reset.
472*91f16700SchasingluluFor example:
473*91f16700Schasinglulu
474*91f16700Schasinglulu	-C cluster0.cpu0.RVBAR=0x4022000
475*91f16700Schasinglulu	--data cluster0.cpu0=bl2.bin@0x4022000
476*91f16700Schasinglulu
477*91f16700SchasingluluWith this configuration, FVP is like a platform of the first case,
478*91f16700Schasingluluwhere the Boot ROM jumps always to the same address. For simplification,
479*91f16700SchasingluluBL32 is loaded in DRAM in this case, to avoid other images reclaiming
480*91f16700SchasingluluBL2 memory.
481*91f16700Schasinglulu
482*91f16700Schasinglulu
483*91f16700SchasingluluAArch64 BL31
484*91f16700Schasinglulu~~~~~~~~~~~~
485*91f16700Schasinglulu
486*91f16700SchasingluluThe image for this stage is loaded by BL2 and BL1 passes control to BL31 at
487*91f16700SchasingluluEL3. BL31 executes solely in trusted SRAM. BL31 is linked against and
488*91f16700Schasinglululoaded at a platform-specific base address (more information can be found later
489*91f16700Schasingluluin this document). The functionality implemented by BL31 is as follows.
490*91f16700Schasinglulu
491*91f16700SchasingluluArchitectural initialization
492*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^
493*91f16700Schasinglulu
494*91f16700SchasingluluCurrently, BL31 performs a similar architectural initialization to BL1 as
495*91f16700Schasinglulufar as system register settings are concerned. Since BL1 code resides in ROM,
496*91f16700Schasingluluarchitectural initialization in BL31 allows override of any previous
497*91f16700Schasingluluinitialization done by BL1.
498*91f16700Schasinglulu
499*91f16700SchasingluluBL31 initializes the per-CPU data framework, which provides a cache of
500*91f16700Schasinglulufrequently accessed per-CPU data optimised for fast, concurrent manipulation
501*91f16700Schasingluluon different CPUs. This buffer includes pointers to per-CPU contexts, crash
502*91f16700Schasinglulubuffer, CPU reset and power down operations, PSCI data, platform data and so on.
503*91f16700Schasinglulu
504*91f16700SchasingluluIt then replaces the exception vectors populated by BL1 with its own. BL31
505*91f16700Schasingluluexception vectors implement more elaborate support for handling SMCs since this
506*91f16700Schasingluluis the only mechanism to access the runtime services implemented by BL31 (PSCI
507*91f16700Schasinglulufor example). BL31 checks each SMC for validity as specified by the
508*91f16700Schasinglulu`SMC Calling Convention`_ before passing control to the required SMC
509*91f16700Schasingluluhandler routine.
510*91f16700Schasinglulu
511*91f16700SchasingluluBL31 programs the ``CNTFRQ_EL0`` register with the clock frequency of the system
512*91f16700Schasinglulucounter, which is provided by the platform.
513*91f16700Schasinglulu
514*91f16700SchasingluluPlatform initialization
515*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^
516*91f16700Schasinglulu
517*91f16700SchasingluluBL31 performs detailed platform initialization, which enables normal world
518*91f16700Schasinglulusoftware to function correctly.
519*91f16700Schasinglulu
520*91f16700SchasingluluOn Arm platforms, this consists of the following:
521*91f16700Schasinglulu
522*91f16700Schasinglulu-  Initialize the console.
523*91f16700Schasinglulu-  Configure the Interconnect to enable hardware coherency.
524*91f16700Schasinglulu-  Enable the MMU and map the memory it needs to access.
525*91f16700Schasinglulu-  Initialize the generic interrupt controller.
526*91f16700Schasinglulu-  Initialize the power controller device.
527*91f16700Schasinglulu-  Detect the system topology.
528*91f16700Schasinglulu
529*91f16700SchasingluluRuntime services initialization
530*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
531*91f16700Schasinglulu
532*91f16700SchasingluluBL31 is responsible for initializing the runtime services. One of them is PSCI.
533*91f16700Schasinglulu
534*91f16700SchasingluluAs part of the PSCI initializations, BL31 detects the system topology. It also
535*91f16700Schasingluluinitializes the data structures that implement the state machine used to track
536*91f16700Schasingluluthe state of power domain nodes. The state can be one of ``OFF``, ``RUN`` or
537*91f16700Schasinglulu``RETENTION``. All secondary CPUs are initially in the ``OFF`` state. The cluster
538*91f16700Schasingluluthat the primary CPU belongs to is ``ON``; any other cluster is ``OFF``. It also
539*91f16700Schasingluluinitializes the locks that protect them. BL31 accesses the state of a CPU or
540*91f16700Schasinglulucluster immediately after reset and before the data cache is enabled in the
541*91f16700Schasingluluwarm boot path. It is not currently possible to use 'exclusive' based spinlocks,
542*91f16700Schasinglulutherefore BL31 uses locks based on Lamport's Bakery algorithm instead.
543*91f16700Schasinglulu
544*91f16700SchasingluluThe runtime service framework and its initialization is described in more
545*91f16700Schasingluludetail in the "EL3 runtime services framework" section below.
546*91f16700Schasinglulu
547*91f16700SchasingluluDetails about the status of the PSCI implementation are provided in the
548*91f16700Schasinglulu"Power State Coordination Interface" section below.
549*91f16700Schasinglulu
550*91f16700SchasingluluAArch64 BL32 (Secure-EL1 Payload) image initialization
551*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
552*91f16700Schasinglulu
553*91f16700SchasingluluIf a BL32 image is present then there must be a matching Secure-EL1 Payload
554*91f16700SchasingluluDispatcher (SPD) service (see later for details). During initialization
555*91f16700Schasingluluthat service must register a function to carry out initialization of BL32
556*91f16700Schasingluluonce the runtime services are fully initialized. BL31 invokes such a
557*91f16700Schasingluluregistered function to initialize BL32 before running BL33. This initialization
558*91f16700Schasingluluis not necessary for AArch32 SPs.
559*91f16700Schasinglulu
560*91f16700SchasingluluDetails on BL32 initialization and the SPD's role are described in the
561*91f16700Schasinglulu:ref:`firmware_design_sel1_spd` section below.
562*91f16700Schasinglulu
563*91f16700SchasingluluBL33 (Non-trusted Firmware) execution
564*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
565*91f16700Schasinglulu
566*91f16700SchasingluluEL3 Runtime Software initializes the EL2 or EL1 processor context for normal-
567*91f16700Schasingluluworld cold boot, ensuring that no secure state information finds its way into
568*91f16700Schasingluluthe non-secure execution state. EL3 Runtime Software uses the entrypoint
569*91f16700Schasingluluinformation provided by BL2 to jump to the Non-trusted firmware image (BL33)
570*91f16700Schasingluluat the highest available Exception Level (EL2 if available, otherwise EL1).
571*91f16700Schasinglulu
572*91f16700SchasingluluUsing alternative Trusted Boot Firmware in place of BL1 & BL2 (AArch64 only)
573*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
574*91f16700Schasinglulu
575*91f16700SchasingluluSome platforms have existing implementations of Trusted Boot Firmware that
576*91f16700Schasingluluwould like to use TF-A BL31 for the EL3 Runtime Software. To enable this
577*91f16700Schasinglulufirmware architecture it is important to provide a fully documented and stable
578*91f16700Schasingluluinterface between the Trusted Boot Firmware and BL31.
579*91f16700Schasinglulu
580*91f16700SchasingluluFuture changes to the BL31 interface will be done in a backwards compatible
581*91f16700Schasingluluway, and this enables these firmware components to be independently enhanced/
582*91f16700Schasingluluupdated to develop and exploit new functionality.
583*91f16700Schasinglulu
584*91f16700SchasingluluRequired CPU state when calling ``bl31_entrypoint()`` during cold boot
585*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
586*91f16700Schasinglulu
587*91f16700SchasingluluThis function must only be called by the primary CPU.
588*91f16700Schasinglulu
589*91f16700SchasingluluOn entry to this function the calling primary CPU must be executing in AArch64
590*91f16700SchasingluluEL3, little-endian data access, and all interrupt sources masked:
591*91f16700Schasinglulu
592*91f16700Schasinglulu::
593*91f16700Schasinglulu
594*91f16700Schasinglulu    PSTATE.EL = 3
595*91f16700Schasinglulu    PSTATE.RW = 1
596*91f16700Schasinglulu    PSTATE.DAIF = 0xf
597*91f16700Schasinglulu    SCTLR_EL3.EE = 0
598*91f16700Schasinglulu
599*91f16700SchasingluluX0 and X1 can be used to pass information from the Trusted Boot Firmware to the
600*91f16700Schasingluluplatform code in BL31:
601*91f16700Schasinglulu
602*91f16700Schasinglulu::
603*91f16700Schasinglulu
604*91f16700Schasinglulu    X0 : Reserved for common TF-A information
605*91f16700Schasinglulu    X1 : Platform specific information
606*91f16700Schasinglulu
607*91f16700SchasingluluBL31 zero-init sections (e.g. ``.bss``) should not contain valid data on entry,
608*91f16700Schasingluluthese will be zero filled prior to invoking platform setup code.
609*91f16700Schasinglulu
610*91f16700SchasingluluUse of the X0 and X1 parameters
611*91f16700Schasinglulu'''''''''''''''''''''''''''''''
612*91f16700Schasinglulu
613*91f16700SchasingluluThe parameters are platform specific and passed from ``bl31_entrypoint()`` to
614*91f16700Schasinglulu``bl31_early_platform_setup()``. The value of these parameters is never directly
615*91f16700Schasingluluused by the common BL31 code.
616*91f16700Schasinglulu
617*91f16700SchasingluluThe convention is that ``X0`` conveys information regarding the BL31, BL32 and
618*91f16700SchasingluluBL33 images from the Trusted Boot firmware and ``X1`` can be used for other
619*91f16700Schasingluluplatform specific purpose. This convention allows platforms which use TF-A's
620*91f16700SchasingluluBL1 and BL2 images to transfer additional platform specific information from
621*91f16700SchasingluluSecure Boot without conflicting with future evolution of TF-A using ``X0`` to
622*91f16700Schasinglulupass a ``bl31_params`` structure.
623*91f16700Schasinglulu
624*91f16700SchasingluluBL31 common and SPD initialization code depends on image and entrypoint
625*91f16700Schasingluluinformation about BL33 and BL32, which is provided via BL31 platform APIs.
626*91f16700SchasingluluThis information is required until the start of execution of BL33. This
627*91f16700Schasingluluinformation can be provided in a platform defined manner, e.g. compiled into
628*91f16700Schasingluluthe platform code in BL31, or provided in a platform defined memory location
629*91f16700Schasingluluby the Trusted Boot firmware, or passed from the Trusted Boot Firmware via the
630*91f16700SchasingluluCold boot Initialization parameters. This data may need to be cleaned out of
631*91f16700Schasingluluthe CPU caches if it is provided by an earlier boot stage and then accessed by
632*91f16700SchasingluluBL31 platform code before the caches are enabled.
633*91f16700Schasinglulu
634*91f16700SchasingluluTF-A's BL2 implementation passes a ``bl31_params`` structure in
635*91f16700Schasinglulu``X0`` and the Arm development platforms interpret this in the BL31 platform
636*91f16700Schasinglulucode.
637*91f16700Schasinglulu
638*91f16700SchasingluluMMU, Data caches & Coherency
639*91f16700Schasinglulu''''''''''''''''''''''''''''
640*91f16700Schasinglulu
641*91f16700SchasingluluBL31 does not depend on the enabled state of the MMU, data caches or
642*91f16700Schasingluluinterconnect coherency on entry to ``bl31_entrypoint()``. If these are disabled
643*91f16700Schasingluluon entry, these should be enabled during ``bl31_plat_arch_setup()``.
644*91f16700Schasinglulu
645*91f16700SchasingluluData structures used in the BL31 cold boot interface
646*91f16700Schasinglulu''''''''''''''''''''''''''''''''''''''''''''''''''''
647*91f16700Schasinglulu
648*91f16700SchasingluluThese structures are designed to support compatibility and independent
649*91f16700Schasingluluevolution of the structures and the firmware images. For example, a version of
650*91f16700SchasingluluBL31 that can interpret the BL3x image information from different versions of
651*91f16700SchasingluluBL2, a platform that uses an extended entry_point_info structure to convey
652*91f16700Schasingluluadditional register information to BL31, or a ELF image loader that can convey
653*91f16700Schasinglulumore details about the firmware images.
654*91f16700Schasinglulu
655*91f16700SchasingluluTo support these scenarios the structures are versioned and sized, which enables
656*91f16700SchasingluluBL31 to detect which information is present and respond appropriately. The
657*91f16700Schasinglulu``param_header`` is defined to capture this information:
658*91f16700Schasinglulu
659*91f16700Schasinglulu.. code:: c
660*91f16700Schasinglulu
661*91f16700Schasinglulu    typedef struct param_header {
662*91f16700Schasinglulu        uint8_t type;       /* type of the structure */
663*91f16700Schasinglulu        uint8_t version;    /* version of this structure */
664*91f16700Schasinglulu        uint16_t size;      /* size of this structure in bytes */
665*91f16700Schasinglulu        uint32_t attr;      /* attributes: unused bits SBZ */
666*91f16700Schasinglulu    } param_header_t;
667*91f16700Schasinglulu
668*91f16700SchasingluluThe structures using this format are ``entry_point_info``, ``image_info`` and
669*91f16700Schasinglulu``bl31_params``. The code that allocates and populates these structures must set
670*91f16700Schasingluluthe header fields appropriately, and the ``SET_PARAM_HEAD()`` a macro is defined
671*91f16700Schasingluluto simplify this action.
672*91f16700Schasinglulu
673*91f16700SchasingluluRequired CPU state for BL31 Warm boot initialization
674*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
675*91f16700Schasinglulu
676*91f16700SchasingluluWhen requesting a CPU power-on, or suspending a running CPU, TF-A provides
677*91f16700Schasingluluthe platform power management code with a Warm boot initialization
678*91f16700Schasingluluentry-point, to be invoked by the CPU immediately after the reset handler.
679*91f16700SchasingluluOn entry to the Warm boot initialization function the calling CPU must be in
680*91f16700SchasingluluAArch64 EL3, little-endian data access and all interrupt sources masked:
681*91f16700Schasinglulu
682*91f16700Schasinglulu::
683*91f16700Schasinglulu
684*91f16700Schasinglulu    PSTATE.EL = 3
685*91f16700Schasinglulu    PSTATE.RW = 1
686*91f16700Schasinglulu    PSTATE.DAIF = 0xf
687*91f16700Schasinglulu    SCTLR_EL3.EE = 0
688*91f16700Schasinglulu
689*91f16700SchasingluluThe PSCI implementation will initialize the processor state and ensure that the
690*91f16700Schasingluluplatform power management code is then invoked as required to initialize all
691*91f16700Schasinglulunecessary system, cluster and CPU resources.
692*91f16700Schasinglulu
693*91f16700SchasingluluAArch32 EL3 Runtime Software entrypoint interface
694*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
695*91f16700Schasinglulu
696*91f16700SchasingluluTo enable this firmware architecture it is important to provide a fully
697*91f16700Schasingluludocumented and stable interface between the Trusted Boot Firmware and the
698*91f16700SchasingluluAArch32 EL3 Runtime Software.
699*91f16700Schasinglulu
700*91f16700SchasingluluFuture changes to the entrypoint interface will be done in a backwards
701*91f16700Schasinglulucompatible way, and this enables these firmware components to be independently
702*91f16700Schasingluluenhanced/updated to develop and exploit new functionality.
703*91f16700Schasinglulu
704*91f16700SchasingluluRequired CPU state when entering during cold boot
705*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
706*91f16700Schasinglulu
707*91f16700SchasingluluThis function must only be called by the primary CPU.
708*91f16700Schasinglulu
709*91f16700SchasingluluOn entry to this function the calling primary CPU must be executing in AArch32
710*91f16700SchasingluluEL3, little-endian data access, and all interrupt sources masked:
711*91f16700Schasinglulu
712*91f16700Schasinglulu::
713*91f16700Schasinglulu
714*91f16700Schasinglulu    PSTATE.AIF = 0x7
715*91f16700Schasinglulu    SCTLR.EE = 0
716*91f16700Schasinglulu
717*91f16700SchasingluluR0 and R1 are used to pass information from the Trusted Boot Firmware to the
718*91f16700Schasingluluplatform code in AArch32 EL3 Runtime Software:
719*91f16700Schasinglulu
720*91f16700Schasinglulu::
721*91f16700Schasinglulu
722*91f16700Schasinglulu    R0 : Reserved for common TF-A information
723*91f16700Schasinglulu    R1 : Platform specific information
724*91f16700Schasinglulu
725*91f16700SchasingluluUse of the R0 and R1 parameters
726*91f16700Schasinglulu'''''''''''''''''''''''''''''''
727*91f16700Schasinglulu
728*91f16700SchasingluluThe parameters are platform specific and the convention is that ``R0`` conveys
729*91f16700Schasingluluinformation regarding the BL3x images from the Trusted Boot firmware and ``R1``
730*91f16700Schasinglulucan be used for other platform specific purpose. This convention allows
731*91f16700Schasingluluplatforms which use TF-A's BL1 and BL2 images to transfer additional platform
732*91f16700Schasingluluspecific information from Secure Boot without conflicting with future
733*91f16700Schasingluluevolution of TF-A using ``R0`` to pass a ``bl_params`` structure.
734*91f16700Schasinglulu
735*91f16700SchasingluluThe AArch32 EL3 Runtime Software is responsible for entry into BL33. This
736*91f16700Schasingluluinformation can be obtained in a platform defined manner, e.g. compiled into
737*91f16700Schasingluluthe AArch32 EL3 Runtime Software, or provided in a platform defined memory
738*91f16700Schasinglululocation by the Trusted Boot firmware, or passed from the Trusted Boot Firmware
739*91f16700Schasingluluvia the Cold boot Initialization parameters. This data may need to be cleaned
740*91f16700Schasingluluout of the CPU caches if it is provided by an earlier boot stage and then
741*91f16700Schasingluluaccessed by AArch32 EL3 Runtime Software before the caches are enabled.
742*91f16700Schasinglulu
743*91f16700SchasingluluWhen using AArch32 EL3 Runtime Software, the Arm development platforms pass a
744*91f16700Schasinglulu``bl_params`` structure in ``R0`` from BL2 to be interpreted by AArch32 EL3 Runtime
745*91f16700SchasingluluSoftware platform code.
746*91f16700Schasinglulu
747*91f16700SchasingluluMMU, Data caches & Coherency
748*91f16700Schasinglulu''''''''''''''''''''''''''''
749*91f16700Schasinglulu
750*91f16700SchasingluluAArch32 EL3 Runtime Software must not depend on the enabled state of the MMU,
751*91f16700Schasingluludata caches or interconnect coherency in its entrypoint. They must be explicitly
752*91f16700Schasingluluenabled if required.
753*91f16700Schasinglulu
754*91f16700SchasingluluData structures used in cold boot interface
755*91f16700Schasinglulu'''''''''''''''''''''''''''''''''''''''''''
756*91f16700Schasinglulu
757*91f16700SchasingluluThe AArch32 EL3 Runtime Software cold boot interface uses ``bl_params`` instead
758*91f16700Schasingluluof ``bl31_params``. The ``bl_params`` structure is based on the convention
759*91f16700Schasingluludescribed in AArch64 BL31 cold boot interface section.
760*91f16700Schasinglulu
761*91f16700SchasingluluRequired CPU state for warm boot initialization
762*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
763*91f16700Schasinglulu
764*91f16700SchasingluluWhen requesting a CPU power-on, or suspending a running CPU, AArch32 EL3
765*91f16700SchasingluluRuntime Software must ensure execution of a warm boot initialization entrypoint.
766*91f16700SchasingluluIf TF-A BL1 is used and the PROGRAMMABLE_RESET_ADDRESS build flag is false,
767*91f16700Schasingluluthen AArch32 EL3 Runtime Software must ensure that BL1 branches to the warm
768*91f16700Schasingluluboot entrypoint by arranging for the BL1 platform function,
769*91f16700Schasingluluplat_get_my_entrypoint(), to return a non-zero value.
770*91f16700Schasinglulu
771*91f16700SchasingluluIn this case, the warm boot entrypoint must be in AArch32 EL3, little-endian
772*91f16700Schasingluludata access and all interrupt sources masked:
773*91f16700Schasinglulu
774*91f16700Schasinglulu::
775*91f16700Schasinglulu
776*91f16700Schasinglulu    PSTATE.AIF = 0x7
777*91f16700Schasinglulu    SCTLR.EE = 0
778*91f16700Schasinglulu
779*91f16700SchasingluluThe warm boot entrypoint may be implemented by using TF-A
780*91f16700Schasinglulu``psci_warmboot_entrypoint()`` function. In that case, the platform must fulfil
781*91f16700Schasingluluthe pre-requisites mentioned in the
782*91f16700Schasinglulu:ref:`PSCI Library Integration guide for Armv8-A AArch32 systems`.
783*91f16700Schasinglulu
784*91f16700SchasingluluEL3 runtime services framework
785*91f16700Schasinglulu------------------------------
786*91f16700Schasinglulu
787*91f16700SchasingluluSoftware executing in the non-secure state and in the secure state at exception
788*91f16700Schasinglululevels lower than EL3 will request runtime services using the Secure Monitor
789*91f16700SchasingluluCall (SMC) instruction. These requests will follow the convention described in
790*91f16700Schasingluluthe SMC Calling Convention PDD (`SMCCC`_). The `SMCCC`_ assigns function
791*91f16700Schasingluluidentifiers to each SMC request and describes how arguments are passed and
792*91f16700Schasinglulureturned.
793*91f16700Schasinglulu
794*91f16700SchasingluluThe EL3 runtime services framework enables the development of services by
795*91f16700Schasingluludifferent providers that can be easily integrated into final product firmware.
796*91f16700SchasingluluThe following sections describe the framework which facilitates the
797*91f16700Schasingluluregistration, initialization and use of runtime services in EL3 Runtime
798*91f16700SchasingluluSoftware (BL31).
799*91f16700Schasinglulu
800*91f16700SchasingluluThe design of the runtime services depends heavily on the concepts and
801*91f16700Schasingluludefinitions described in the `SMCCC`_, in particular SMC Function IDs, Owning
802*91f16700SchasingluluEntity Numbers (OEN), Fast and Yielding calls, and the SMC32 and SMC64 calling
803*91f16700Schasingluluconventions. Please refer to that document for more detailed explanation of
804*91f16700Schasingluluthese terms.
805*91f16700Schasinglulu
806*91f16700SchasingluluThe following runtime services are expected to be implemented first. They have
807*91f16700Schasinglulunot all been instantiated in the current implementation.
808*91f16700Schasinglulu
809*91f16700Schasinglulu#. Standard service calls
810*91f16700Schasinglulu
811*91f16700Schasinglulu   This service is for management of the entire system. The Power State
812*91f16700Schasinglulu   Coordination Interface (`PSCI`_) is the first set of standard service calls
813*91f16700Schasinglulu   defined by Arm (see PSCI section later).
814*91f16700Schasinglulu
815*91f16700Schasinglulu#. Secure-EL1 Payload Dispatcher service
816*91f16700Schasinglulu
817*91f16700Schasinglulu   If a system runs a Trusted OS or other Secure-EL1 Payload (SP) then
818*91f16700Schasinglulu   it also requires a *Secure Monitor* at EL3 to switch the EL1 processor
819*91f16700Schasinglulu   context between the normal world (EL1/EL2) and trusted world (Secure-EL1).
820*91f16700Schasinglulu   The Secure Monitor will make these world switches in response to SMCs. The
821*91f16700Schasinglulu   `SMCCC`_ provides for such SMCs with the Trusted OS Call and Trusted
822*91f16700Schasinglulu   Application Call OEN ranges.
823*91f16700Schasinglulu
824*91f16700Schasinglulu   The interface between the EL3 Runtime Software and the Secure-EL1 Payload is
825*91f16700Schasinglulu   not defined by the `SMCCC`_ or any other standard. As a result, each
826*91f16700Schasinglulu   Secure-EL1 Payload requires a specific Secure Monitor that runs as a runtime
827*91f16700Schasinglulu   service - within TF-A this service is referred to as the Secure-EL1 Payload
828*91f16700Schasinglulu   Dispatcher (SPD).
829*91f16700Schasinglulu
830*91f16700Schasinglulu   TF-A provides a Test Secure-EL1 Payload (TSP) and its associated Dispatcher
831*91f16700Schasinglulu   (TSPD). Details of SPD design and TSP/TSPD operation are described in the
832*91f16700Schasinglulu   :ref:`firmware_design_sel1_spd` section below.
833*91f16700Schasinglulu
834*91f16700Schasinglulu#. CPU implementation service
835*91f16700Schasinglulu
836*91f16700Schasinglulu   This service will provide an interface to CPU implementation specific
837*91f16700Schasinglulu   services for a given platform e.g. access to processor errata workarounds.
838*91f16700Schasinglulu   This service is currently unimplemented.
839*91f16700Schasinglulu
840*91f16700SchasingluluAdditional services for Arm Architecture, SiP and OEM calls can be implemented.
841*91f16700SchasingluluEach implemented service handles a range of SMC function identifiers as
842*91f16700Schasingluludescribed in the `SMCCC`_.
843*91f16700Schasinglulu
844*91f16700SchasingluluRegistration
845*91f16700Schasinglulu~~~~~~~~~~~~
846*91f16700Schasinglulu
847*91f16700SchasingluluA runtime service is registered using the ``DECLARE_RT_SVC()`` macro, specifying
848*91f16700Schasingluluthe name of the service, the range of OENs covered, the type of service and
849*91f16700Schasingluluinitialization and call handler functions. This macro instantiates a ``const struct rt_svc_desc`` for the service with these details (see ``runtime_svc.h``).
850*91f16700SchasingluluThis structure is allocated in a special ELF section ``.rt_svc_descs``, enabling
851*91f16700Schasingluluthe framework to find all service descriptors included into BL31.
852*91f16700Schasinglulu
853*91f16700SchasingluluThe specific service for a SMC Function is selected based on the OEN and call
854*91f16700Schasinglulutype of the Function ID, and the framework uses that information in the service
855*91f16700Schasingluludescriptor to identify the handler for the SMC Call.
856*91f16700Schasinglulu
857*91f16700SchasingluluThe service descriptors do not include information to identify the precise set
858*91f16700Schasingluluof SMC function identifiers supported by this service implementation, the
859*91f16700Schasinglulusecurity state from which such calls are valid nor the capability to support
860*91f16700Schasinglulu64-bit and/or 32-bit callers (using SMC32 or SMC64). Responding appropriately
861*91f16700Schasingluluto these aspects of a SMC call is the responsibility of the service
862*91f16700Schasingluluimplementation, the framework is focused on integration of services from
863*91f16700Schasingluludifferent providers and minimizing the time taken by the framework before the
864*91f16700Schasingluluservice handler is invoked.
865*91f16700Schasinglulu
866*91f16700SchasingluluDetails of the parameters, requirements and behavior of the initialization and
867*91f16700Schasinglulucall handling functions are provided in the following sections.
868*91f16700Schasinglulu
869*91f16700SchasingluluInitialization
870*91f16700Schasinglulu~~~~~~~~~~~~~~
871*91f16700Schasinglulu
872*91f16700Schasinglulu``runtime_svc_init()`` in ``runtime_svc.c`` initializes the runtime services
873*91f16700Schasingluluframework running on the primary CPU during cold boot as part of the BL31
874*91f16700Schasingluluinitialization. This happens prior to initializing a Trusted OS and running
875*91f16700SchasingluluNormal world boot firmware that might in turn use these services.
876*91f16700SchasingluluInitialization involves validating each of the declared runtime service
877*91f16700Schasingluludescriptors, calling the service initialization function and populating the
878*91f16700Schasingluluindex used for runtime lookup of the service.
879*91f16700Schasinglulu
880*91f16700SchasingluluThe BL31 linker script collects all of the declared service descriptors into a
881*91f16700Schasinglulusingle array and defines symbols that allow the framework to locate and traverse
882*91f16700Schasingluluthe array, and determine its size.
883*91f16700Schasinglulu
884*91f16700SchasingluluThe framework does basic validation of each descriptor to halt firmware
885*91f16700Schasingluluinitialization if service declaration errors are detected. The framework does
886*91f16700Schasinglulunot check descriptors for the following error conditions, and may behave in an
887*91f16700Schasingluluunpredictable manner under such scenarios:
888*91f16700Schasinglulu
889*91f16700Schasinglulu#. Overlapping OEN ranges
890*91f16700Schasinglulu#. Multiple descriptors for the same range of OENs and ``call_type``
891*91f16700Schasinglulu#. Incorrect range of owning entity numbers for a given ``call_type``
892*91f16700Schasinglulu
893*91f16700SchasingluluOnce validated, the service ``init()`` callback is invoked. This function carries
894*91f16700Schasingluluout any essential EL3 initialization before servicing requests. The ``init()``
895*91f16700Schasinglulufunction is only invoked on the primary CPU during cold boot. If the service
896*91f16700Schasingluluuses per-CPU data this must either be initialized for all CPUs during this call,
897*91f16700Schasingluluor be done lazily when a CPU first issues an SMC call to that service. If
898*91f16700Schasinglulu``init()`` returns anything other than ``0``, this is treated as an initialization
899*91f16700Schasingluluerror and the service is ignored: this does not cause the firmware to halt.
900*91f16700Schasinglulu
901*91f16700SchasingluluThe OEN and call type fields present in the SMC Function ID cover a total of
902*91f16700Schasinglulu128 distinct services, but in practice a single descriptor can cover a range of
903*91f16700SchasingluluOENs, e.g. SMCs to call a Trusted OS function. To optimize the lookup of a
904*91f16700Schasingluluservice handler, the framework uses an array of 128 indices that map every
905*91f16700Schasingluludistinct OEN/call-type combination either to one of the declared services or to
906*91f16700Schasingluluindicate the service is not handled. This ``rt_svc_descs_indices[]`` array is
907*91f16700Schasinglulupopulated for all of the OENs covered by a service after the service ``init()``
908*91f16700Schasinglulufunction has reported success. So a service that fails to initialize will never
909*91f16700Schasingluluhave it's ``handle()`` function invoked.
910*91f16700Schasinglulu
911*91f16700SchasingluluThe following figure shows how the ``rt_svc_descs_indices[]`` index maps the SMC
912*91f16700SchasingluluFunction ID call type and OEN onto a specific service handler in the
913*91f16700Schasinglulu``rt_svc_descs[]`` array.
914*91f16700Schasinglulu
915*91f16700Schasinglulu|Image 1|
916*91f16700Schasinglulu
917*91f16700Schasinglulu.. _handling-an-smc:
918*91f16700Schasinglulu
919*91f16700SchasingluluHandling an SMC
920*91f16700Schasinglulu~~~~~~~~~~~~~~~
921*91f16700Schasinglulu
922*91f16700SchasingluluWhen the EL3 runtime services framework receives a Secure Monitor Call, the SMC
923*91f16700SchasingluluFunction ID is passed in W0 from the lower exception level (as per the
924*91f16700Schasinglulu`SMCCC`_). If the calling register width is AArch32, it is invalid to invoke an
925*91f16700SchasingluluSMC Function which indicates the SMC64 calling convention: such calls are
926*91f16700Schasingluluignored and return the Unknown SMC Function Identifier result code ``0xFFFFFFFF``
927*91f16700Schasingluluin R0/X0.
928*91f16700Schasinglulu
929*91f16700SchasingluluBit[31] (fast/yielding call) and bits[29:24] (owning entity number) of the SMC
930*91f16700SchasingluluFunction ID are combined to index into the ``rt_svc_descs_indices[]`` array. The
931*91f16700Schasingluluresulting value might indicate a service that has no handler, in this case the
932*91f16700Schasingluluframework will also report an Unknown SMC Function ID. Otherwise, the value is
933*91f16700Schasingluluused as a further index into the ``rt_svc_descs[]`` array to locate the required
934*91f16700Schasingluluservice and handler.
935*91f16700Schasinglulu
936*91f16700SchasingluluThe service's ``handle()`` callback is provided with five of the SMC parameters
937*91f16700Schasingluludirectly, the others are saved into memory for retrieval (if needed) by the
938*91f16700Schasingluluhandler. The handler is also provided with an opaque ``handle`` for use with the
939*91f16700Schasinglulusupporting library for parameter retrieval, setting return values and context
940*91f16700Schasinglulumanipulation. The ``flags`` parameter indicates the security state of the caller
941*91f16700Schasingluluand the state of the SVE hint bit per the SMCCCv1.3. The framework finally sets
942*91f16700Schasingluluup the execution stack for the handler, and invokes the services ``handle()``
943*91f16700Schasinglulufunction.
944*91f16700Schasinglulu
945*91f16700SchasingluluOn return from the handler the result registers are populated in X0-X7 as needed
946*91f16700Schasinglulubefore restoring the stack and CPU state and returning from the original SMC.
947*91f16700Schasinglulu
948*91f16700SchasingluluException Handling Framework
949*91f16700Schasinglulu----------------------------
950*91f16700Schasinglulu
951*91f16700SchasingluluPlease refer to the :ref:`Exception Handling Framework` document.
952*91f16700Schasinglulu
953*91f16700SchasingluluPower State Coordination Interface
954*91f16700Schasinglulu----------------------------------
955*91f16700Schasinglulu
956*91f16700SchasingluluTODO: Provide design walkthrough of PSCI implementation.
957*91f16700Schasinglulu
958*91f16700SchasingluluThe PSCI v1.1 specification categorizes APIs as optional and mandatory. All the
959*91f16700Schasinglulumandatory APIs in PSCI v1.1, PSCI v1.0 and in PSCI v0.2 draft specification
960*91f16700Schasinglulu`PSCI`_ are implemented. The table lists the PSCI v1.1 APIs and their support
961*91f16700Schasingluluin generic code.
962*91f16700Schasinglulu
963*91f16700SchasingluluAn API implementation might have a dependency on platform code e.g. CPU_SUSPEND
964*91f16700Schasinglulurequires the platform to export a part of the implementation. Hence the level
965*91f16700Schasingluluof support of the mandatory APIs depends upon the support exported by the
966*91f16700Schasingluluplatform port as well. The Juno and FVP (all variants) platforms export all the
967*91f16700Schasinglulurequired support.
968*91f16700Schasinglulu
969*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
970*91f16700Schasinglulu| PSCI v1.1 API               | Supported   | Comments                      |
971*91f16700Schasinglulu+=============================+=============+===============================+
972*91f16700Schasinglulu| ``PSCI_VERSION``            | Yes         | The version returned is 1.1   |
973*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
974*91f16700Schasinglulu| ``CPU_SUSPEND``             | Yes\*       |                               |
975*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
976*91f16700Schasinglulu| ``CPU_OFF``                 | Yes\*       |                               |
977*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
978*91f16700Schasinglulu| ``CPU_ON``                  | Yes\*       |                               |
979*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
980*91f16700Schasinglulu| ``AFFINITY_INFO``           | Yes         |                               |
981*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
982*91f16700Schasinglulu| ``MIGRATE``                 | Yes\*\*     |                               |
983*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
984*91f16700Schasinglulu| ``MIGRATE_INFO_TYPE``       | Yes\*\*     |                               |
985*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
986*91f16700Schasinglulu| ``MIGRATE_INFO_CPU``        | Yes\*\*     |                               |
987*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
988*91f16700Schasinglulu| ``SYSTEM_OFF``              | Yes\*       |                               |
989*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
990*91f16700Schasinglulu| ``SYSTEM_RESET``            | Yes\*       |                               |
991*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
992*91f16700Schasinglulu| ``PSCI_FEATURES``           | Yes         |                               |
993*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
994*91f16700Schasinglulu| ``CPU_FREEZE``              | No          |                               |
995*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
996*91f16700Schasinglulu| ``CPU_DEFAULT_SUSPEND``     | No          |                               |
997*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
998*91f16700Schasinglulu| ``NODE_HW_STATE``           | Yes\*       |                               |
999*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1000*91f16700Schasinglulu| ``SYSTEM_SUSPEND``          | Yes\*       |                               |
1001*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1002*91f16700Schasinglulu| ``PSCI_SET_SUSPEND_MODE``   | No          |                               |
1003*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1004*91f16700Schasinglulu| ``PSCI_STAT_RESIDENCY``     | Yes\*       |                               |
1005*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1006*91f16700Schasinglulu| ``PSCI_STAT_COUNT``         | Yes\*       |                               |
1007*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1008*91f16700Schasinglulu| ``SYSTEM_RESET2``           | Yes\*       |                               |
1009*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1010*91f16700Schasinglulu| ``MEM_PROTECT``             | Yes\*       |                               |
1011*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1012*91f16700Schasinglulu| ``MEM_PROTECT_CHECK_RANGE`` | Yes\*       |                               |
1013*91f16700Schasinglulu+-----------------------------+-------------+-------------------------------+
1014*91f16700Schasinglulu
1015*91f16700Schasinglulu\*Note : These PSCI APIs require platform power management hooks to be
1016*91f16700Schasingluluregistered with the generic PSCI code to be supported.
1017*91f16700Schasinglulu
1018*91f16700Schasinglulu\*\*Note : These PSCI APIs require appropriate Secure Payload Dispatcher
1019*91f16700Schasingluluhooks to be registered with the generic PSCI code to be supported.
1020*91f16700Schasinglulu
1021*91f16700SchasingluluThe PSCI implementation in TF-A is a library which can be integrated with
1022*91f16700SchasingluluAArch64 or AArch32 EL3 Runtime Software for Armv8-A systems. A guide to
1023*91f16700Schasingluluintegrating PSCI library with AArch32 EL3 Runtime Software can be found
1024*91f16700Schasingluluat :ref:`PSCI Library Integration guide for Armv8-A AArch32 systems`.
1025*91f16700Schasinglulu
1026*91f16700Schasinglulu.. _firmware_design_sel1_spd:
1027*91f16700Schasinglulu
1028*91f16700SchasingluluSecure-EL1 Payloads and Dispatchers
1029*91f16700Schasinglulu-----------------------------------
1030*91f16700Schasinglulu
1031*91f16700SchasingluluOn a production system that includes a Trusted OS running in Secure-EL1/EL0,
1032*91f16700Schasingluluthe Trusted OS is coupled with a companion runtime service in the BL31
1033*91f16700Schasinglulufirmware. This service is responsible for the initialisation of the Trusted
1034*91f16700SchasingluluOS and all communications with it. The Trusted OS is the BL32 stage of the
1035*91f16700Schasingluluboot flow in TF-A. The firmware will attempt to locate, load and execute a
1036*91f16700SchasingluluBL32 image.
1037*91f16700Schasinglulu
1038*91f16700SchasingluluTF-A uses a more general term for the BL32 software that runs at Secure-EL1 -
1039*91f16700Schasingluluthe *Secure-EL1 Payload* - as it is not always a Trusted OS.
1040*91f16700Schasinglulu
1041*91f16700SchasingluluTF-A provides a Test Secure-EL1 Payload (TSP) and a Test Secure-EL1 Payload
1042*91f16700SchasingluluDispatcher (TSPD) service as an example of how a Trusted OS is supported on a
1043*91f16700Schasingluluproduction system using the Runtime Services Framework. On such a system, the
1044*91f16700SchasingluluTest BL32 image and service are replaced by the Trusted OS and its dispatcher
1045*91f16700Schasingluluservice. The TF-A build system expects that the dispatcher will define the
1046*91f16700Schasinglulubuild flag ``NEED_BL32`` to enable it to include the BL32 in the build either
1047*91f16700Schasingluluas a binary or to compile from source depending on whether the ``BL32`` build
1048*91f16700Schasingluluoption is specified or not.
1049*91f16700Schasinglulu
1050*91f16700SchasingluluThe TSP runs in Secure-EL1. It is designed to demonstrate synchronous
1051*91f16700Schasinglulucommunication with the normal-world software running in EL1/EL2. Communication
1052*91f16700Schasingluluis initiated by the normal-world software
1053*91f16700Schasinglulu
1054*91f16700Schasinglulu-  either directly through a Fast SMC (as defined in the `SMCCC`_)
1055*91f16700Schasinglulu
1056*91f16700Schasinglulu-  or indirectly through a `PSCI`_ SMC. The `PSCI`_ implementation in turn
1057*91f16700Schasinglulu   informs the TSPD about the requested power management operation. This allows
1058*91f16700Schasinglulu   the TSP to prepare for or respond to the power state change
1059*91f16700Schasinglulu
1060*91f16700SchasingluluThe TSPD service is responsible for.
1061*91f16700Schasinglulu
1062*91f16700Schasinglulu-  Initializing the TSP
1063*91f16700Schasinglulu
1064*91f16700Schasinglulu-  Routing requests and responses between the secure and the non-secure
1065*91f16700Schasinglulu   states during the two types of communications just described
1066*91f16700Schasinglulu
1067*91f16700SchasingluluInitializing a BL32 Image
1068*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~
1069*91f16700Schasinglulu
1070*91f16700SchasingluluThe Secure-EL1 Payload Dispatcher (SPD) service is responsible for initializing
1071*91f16700Schasingluluthe BL32 image. It needs access to the information passed by BL2 to BL31 to do
1072*91f16700Schasingluluso. This is provided by:
1073*91f16700Schasinglulu
1074*91f16700Schasinglulu.. code:: c
1075*91f16700Schasinglulu
1076*91f16700Schasinglulu    entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t);
1077*91f16700Schasinglulu
1078*91f16700Schasingluluwhich returns a reference to the ``entry_point_info`` structure corresponding to
1079*91f16700Schasingluluthe image which will be run in the specified security state. The SPD uses this
1080*91f16700SchasingluluAPI to get entry point information for the SECURE image, BL32.
1081*91f16700Schasinglulu
1082*91f16700SchasingluluIn the absence of a BL32 image, BL31 passes control to the normal world
1083*91f16700Schasinglulubootloader image (BL33). When the BL32 image is present, it is typical
1084*91f16700Schasingluluthat the SPD wants control to be passed to BL32 first and then later to BL33.
1085*91f16700Schasinglulu
1086*91f16700SchasingluluTo do this the SPD has to register a BL32 initialization function during
1087*91f16700Schasingluluinitialization of the SPD service. The BL32 initialization function has this
1088*91f16700Schasingluluprototype:
1089*91f16700Schasinglulu
1090*91f16700Schasinglulu.. code:: c
1091*91f16700Schasinglulu
1092*91f16700Schasinglulu    int32_t init(void);
1093*91f16700Schasinglulu
1094*91f16700Schasingluluand is registered using the ``bl31_register_bl32_init()`` function.
1095*91f16700Schasinglulu
1096*91f16700SchasingluluTF-A supports two approaches for the SPD to pass control to BL32 before
1097*91f16700Schasinglulureturning through EL3 and running the non-trusted firmware (BL33):
1098*91f16700Schasinglulu
1099*91f16700Schasinglulu#. In the BL32 setup function, use ``bl31_set_next_image_type()`` to
1100*91f16700Schasinglulu   request that the exit from ``bl31_main()`` is to the BL32 entrypoint in
1101*91f16700Schasinglulu   Secure-EL1. BL31 will exit to BL32 using the asynchronous method by
1102*91f16700Schasinglulu   calling ``bl31_prepare_next_image_entry()`` and ``el3_exit()``.
1103*91f16700Schasinglulu
1104*91f16700Schasinglulu   When the BL32 has completed initialization at Secure-EL1, it returns to
1105*91f16700Schasinglulu   BL31 by issuing an SMC, using a Function ID allocated to the SPD. On
1106*91f16700Schasinglulu   receipt of this SMC, the SPD service handler should switch the CPU context
1107*91f16700Schasinglulu   from trusted to normal world and use the ``bl31_set_next_image_type()`` and
1108*91f16700Schasinglulu   ``bl31_prepare_next_image_entry()`` functions to set up the initial return to
1109*91f16700Schasinglulu   the normal world firmware BL33. On return from the handler the framework
1110*91f16700Schasinglulu   will exit to EL2 and run BL33.
1111*91f16700Schasinglulu
1112*91f16700Schasinglulu#. The BL32 setup function registers an initialization function using
1113*91f16700Schasinglulu   ``bl31_register_bl32_init()`` which provides a SPD-defined mechanism to
1114*91f16700Schasinglulu   invoke a 'world-switch synchronous call' to Secure-EL1 to run the BL32
1115*91f16700Schasinglulu   entrypoint.
1116*91f16700Schasinglulu
1117*91f16700Schasinglulu   .. note::
1118*91f16700Schasinglulu      The Test SPD service included with TF-A provides one implementation
1119*91f16700Schasinglulu      of such a mechanism.
1120*91f16700Schasinglulu
1121*91f16700Schasinglulu   On completion BL32 returns control to BL31 via a SMC, and on receipt the
1122*91f16700Schasinglulu   SPD service handler invokes the synchronous call return mechanism to return
1123*91f16700Schasinglulu   to the BL32 initialization function. On return from this function,
1124*91f16700Schasinglulu   ``bl31_main()`` will set up the return to the normal world firmware BL33 and
1125*91f16700Schasinglulu   continue the boot process in the normal world.
1126*91f16700Schasinglulu
1127*91f16700SchasingluluException handling in BL31
1128*91f16700Schasinglulu--------------------------
1129*91f16700Schasinglulu
1130*91f16700SchasingluluWhen exception occurs, PE must execute handler corresponding to exception. The
1131*91f16700Schasinglululocation in memory where the handler is stored is called the exception vector.
1132*91f16700SchasingluluFor ARM architecture, exception vectors are stored in a table, called the exception
1133*91f16700Schasingluluvector table.
1134*91f16700Schasinglulu
1135*91f16700SchasingluluEach EL (except EL0) has its own vector table, VBAR_ELn register stores the base
1136*91f16700Schasingluluof vector table. Refer to `AArch64 exception vector table`_
1137*91f16700Schasinglulu
1138*91f16700SchasingluluCurrent EL with SP_EL0
1139*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~
1140*91f16700Schasinglulu
1141*91f16700Schasinglulu-  Sync exception : Not expected except for BRK instruction, its debugging tool which
1142*91f16700Schasinglulu   a programmer may place at specific points in a program, to check the state of
1143*91f16700Schasinglulu   processor flags at these points in the code.
1144*91f16700Schasinglulu
1145*91f16700Schasinglulu-  IRQ/FIQ : Unexpected exception, panic
1146*91f16700Schasinglulu
1147*91f16700Schasinglulu-  SError : "plat_handle_el3_ea", defaults to panic
1148*91f16700Schasinglulu
1149*91f16700SchasingluluCurrent EL with SP_ELx
1150*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~
1151*91f16700Schasinglulu
1152*91f16700Schasinglulu-  Sync exception : Unexpected exception, panic
1153*91f16700Schasinglulu
1154*91f16700Schasinglulu-  IRQ/FIQ : Unexpected exception, panic
1155*91f16700Schasinglulu
1156*91f16700Schasinglulu-  SError : "plat_handle_el3_ea" Except for special handling of lower EL's SError exception
1157*91f16700Schasinglulu   which gets triggered in EL3 when PSTATE.A is unmasked. Its only applicable when lower
1158*91f16700Schasinglulu   EL's EA is routed to EL3 (FFH_SUPPORT=1).
1159*91f16700Schasinglulu
1160*91f16700SchasingluluLower EL Exceptions
1161*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~
1162*91f16700Schasinglulu
1163*91f16700SchasingluluApplies to all the exceptions in both AArch64/AArch32 mode of lower EL.
1164*91f16700Schasinglulu
1165*91f16700SchasingluluBefore handling any lower EL exception, we synchronize the errors at EL3 entry to ensure
1166*91f16700Schasingluluthat any errors pertaining to lower EL is isolated/identified. If we continue without
1167*91f16700Schasingluluidentifying these errors early on then these errors will trigger in EL3 (as SError from
1168*91f16700Schasinglulucurrent EL) any time after PSTATE.A is unmasked. This is wrong because the error originated
1169*91f16700Schasingluluin lower EL but exception happened in EL3.
1170*91f16700Schasinglulu
1171*91f16700SchasingluluTo solve this problem, synchronize the errors at EL3 entry and check for any pending
1172*91f16700Schasingluluerrors (async EA). If there is no pending error then continue with original exception.
1173*91f16700SchasingluluIf there is a pending error then, handle them based on routing model of EA's. Refer to
1174*91f16700Schasinglulu:ref:`Reliability, Availability, and Serviceability (RAS) Extensions` for details about
1175*91f16700Schasinglulurouting models.
1176*91f16700Schasinglulu
1177*91f16700Schasinglulu-  KFH : Reflect it back to lower EL using **reflect_pending_async_ea_to_lower_el()**
1178*91f16700Schasinglulu
1179*91f16700Schasinglulu-  FFH : Handle the synchronized error first using **handle_pending_async_ea()** after
1180*91f16700Schasinglulu   that continue with original exception. It is the only scenario where EL3 is capable
1181*91f16700Schasinglulu   of doing nested exception handling.
1182*91f16700Schasinglulu
1183*91f16700SchasingluluAfter synchronizing and handling lower EL SErrors, unmask EA (PSTATE.A) to ensure
1184*91f16700Schasingluluthat any further EA's caused by EL3 are caught.
1185*91f16700Schasinglulu
1186*91f16700SchasingluluCrash Reporting in BL31
1187*91f16700Schasinglulu-----------------------
1188*91f16700Schasinglulu
1189*91f16700SchasingluluBL31 implements a scheme for reporting the processor state when an unhandled
1190*91f16700Schasingluluexception is encountered. The reporting mechanism attempts to preserve all the
1191*91f16700Schasingluluregister contents and report it via a dedicated UART (PL011 console). BL31
1192*91f16700Schasinglulureports the general purpose, EL3, Secure EL1 and some EL2 state registers.
1193*91f16700Schasinglulu
1194*91f16700SchasingluluA dedicated per-CPU crash stack is maintained by BL31 and this is retrieved via
1195*91f16700Schasingluluthe per-CPU pointer cache. The implementation attempts to minimise the memory
1196*91f16700Schasinglulurequired for this feature. The file ``crash_reporting.S`` contains the
1197*91f16700Schasingluluimplementation for crash reporting.
1198*91f16700Schasinglulu
1199*91f16700SchasingluluThe sample crash output is shown below.
1200*91f16700Schasinglulu
1201*91f16700Schasinglulu::
1202*91f16700Schasinglulu
1203*91f16700Schasinglulu    x0             = 0x000000002a4a0000
1204*91f16700Schasinglulu    x1             = 0x0000000000000001
1205*91f16700Schasinglulu    x2             = 0x0000000000000002
1206*91f16700Schasinglulu    x3             = 0x0000000000000003
1207*91f16700Schasinglulu    x4             = 0x0000000000000004
1208*91f16700Schasinglulu    x5             = 0x0000000000000005
1209*91f16700Schasinglulu    x6             = 0x0000000000000006
1210*91f16700Schasinglulu    x7             = 0x0000000000000007
1211*91f16700Schasinglulu    x8             = 0x0000000000000008
1212*91f16700Schasinglulu    x9             = 0x0000000000000009
1213*91f16700Schasinglulu    x10            = 0x0000000000000010
1214*91f16700Schasinglulu    x11            = 0x0000000000000011
1215*91f16700Schasinglulu    x12            = 0x0000000000000012
1216*91f16700Schasinglulu    x13            = 0x0000000000000013
1217*91f16700Schasinglulu    x14            = 0x0000000000000014
1218*91f16700Schasinglulu    x15            = 0x0000000000000015
1219*91f16700Schasinglulu    x16            = 0x0000000000000016
1220*91f16700Schasinglulu    x17            = 0x0000000000000017
1221*91f16700Schasinglulu    x18            = 0x0000000000000018
1222*91f16700Schasinglulu    x19            = 0x0000000000000019
1223*91f16700Schasinglulu    x20            = 0x0000000000000020
1224*91f16700Schasinglulu    x21            = 0x0000000000000021
1225*91f16700Schasinglulu    x22            = 0x0000000000000022
1226*91f16700Schasinglulu    x23            = 0x0000000000000023
1227*91f16700Schasinglulu    x24            = 0x0000000000000024
1228*91f16700Schasinglulu    x25            = 0x0000000000000025
1229*91f16700Schasinglulu    x26            = 0x0000000000000026
1230*91f16700Schasinglulu    x27            = 0x0000000000000027
1231*91f16700Schasinglulu    x28            = 0x0000000000000028
1232*91f16700Schasinglulu    x29            = 0x0000000000000029
1233*91f16700Schasinglulu    x30            = 0x0000000088000b78
1234*91f16700Schasinglulu    scr_el3        = 0x000000000003073d
1235*91f16700Schasinglulu    sctlr_el3      = 0x00000000b0cd183f
1236*91f16700Schasinglulu    cptr_el3       = 0x0000000000000000
1237*91f16700Schasinglulu    tcr_el3        = 0x000000008080351c
1238*91f16700Schasinglulu    daif           = 0x00000000000002c0
1239*91f16700Schasinglulu    mair_el3       = 0x00000000004404ff
1240*91f16700Schasinglulu    spsr_el3       = 0x0000000060000349
1241*91f16700Schasinglulu    elr_el3        = 0x0000000088000114
1242*91f16700Schasinglulu    ttbr0_el3      = 0x0000000004018201
1243*91f16700Schasinglulu    esr_el3        = 0x00000000be000000
1244*91f16700Schasinglulu    far_el3        = 0x0000000000000000
1245*91f16700Schasinglulu    spsr_el1       = 0x0000000000000000
1246*91f16700Schasinglulu    elr_el1        = 0x0000000000000000
1247*91f16700Schasinglulu    spsr_abt       = 0x0000000000000000
1248*91f16700Schasinglulu    spsr_und       = 0x0000000000000000
1249*91f16700Schasinglulu    spsr_irq       = 0x0000000000000000
1250*91f16700Schasinglulu    spsr_fiq       = 0x0000000000000000
1251*91f16700Schasinglulu    sctlr_el1      = 0x0000000030d00800
1252*91f16700Schasinglulu    actlr_el1      = 0x0000000000000000
1253*91f16700Schasinglulu    cpacr_el1      = 0x0000000000000000
1254*91f16700Schasinglulu    csselr_el1     = 0x0000000000000000
1255*91f16700Schasinglulu    sp_el1         = 0x0000000000000000
1256*91f16700Schasinglulu    esr_el1        = 0x0000000000000000
1257*91f16700Schasinglulu    ttbr0_el1      = 0x0000000000000000
1258*91f16700Schasinglulu    ttbr1_el1      = 0x0000000000000000
1259*91f16700Schasinglulu    mair_el1       = 0x0000000000000000
1260*91f16700Schasinglulu    amair_el1      = 0x0000000000000000
1261*91f16700Schasinglulu    tcr_el1        = 0x0000000000000000
1262*91f16700Schasinglulu    tpidr_el1      = 0x0000000000000000
1263*91f16700Schasinglulu    tpidr_el0      = 0x0000000000000000
1264*91f16700Schasinglulu    tpidrro_el0    = 0x0000000000000000
1265*91f16700Schasinglulu    par_el1        = 0x0000000000000000
1266*91f16700Schasinglulu    mpidr_el1      = 0x0000000080000000
1267*91f16700Schasinglulu    afsr0_el1      = 0x0000000000000000
1268*91f16700Schasinglulu    afsr1_el1      = 0x0000000000000000
1269*91f16700Schasinglulu    contextidr_el1 = 0x0000000000000000
1270*91f16700Schasinglulu    vbar_el1       = 0x0000000000000000
1271*91f16700Schasinglulu    cntp_ctl_el0   = 0x0000000000000000
1272*91f16700Schasinglulu    cntp_cval_el0  = 0x0000000000000000
1273*91f16700Schasinglulu    cntv_ctl_el0   = 0x0000000000000000
1274*91f16700Schasinglulu    cntv_cval_el0  = 0x0000000000000000
1275*91f16700Schasinglulu    cntkctl_el1    = 0x0000000000000000
1276*91f16700Schasinglulu    sp_el0         = 0x0000000004014940
1277*91f16700Schasinglulu    isr_el1        = 0x0000000000000000
1278*91f16700Schasinglulu    dacr32_el2     = 0x0000000000000000
1279*91f16700Schasinglulu    ifsr32_el2     = 0x0000000000000000
1280*91f16700Schasinglulu    icc_hppir0_el1 = 0x00000000000003ff
1281*91f16700Schasinglulu    icc_hppir1_el1 = 0x00000000000003ff
1282*91f16700Schasinglulu    icc_ctlr_el3   = 0x0000000000080400
1283*91f16700Schasinglulu    gicd_ispendr regs (Offsets 0x200-0x278)
1284*91f16700Schasinglulu    Offset		    Value
1285*91f16700Schasinglulu    0x200:	     0x0000000000000000
1286*91f16700Schasinglulu    0x208:	     0x0000000000000000
1287*91f16700Schasinglulu    0x210:	     0x0000000000000000
1288*91f16700Schasinglulu    0x218:	     0x0000000000000000
1289*91f16700Schasinglulu    0x220:	     0x0000000000000000
1290*91f16700Schasinglulu    0x228:	     0x0000000000000000
1291*91f16700Schasinglulu    0x230:	     0x0000000000000000
1292*91f16700Schasinglulu    0x238:	     0x0000000000000000
1293*91f16700Schasinglulu    0x240:	     0x0000000000000000
1294*91f16700Schasinglulu    0x248:	     0x0000000000000000
1295*91f16700Schasinglulu    0x250:	     0x0000000000000000
1296*91f16700Schasinglulu    0x258:	     0x0000000000000000
1297*91f16700Schasinglulu    0x260:	     0x0000000000000000
1298*91f16700Schasinglulu    0x268:	     0x0000000000000000
1299*91f16700Schasinglulu    0x270:	     0x0000000000000000
1300*91f16700Schasinglulu    0x278:	     0x0000000000000000
1301*91f16700Schasinglulu
1302*91f16700SchasingluluGuidelines for Reset Handlers
1303*91f16700Schasinglulu-----------------------------
1304*91f16700Schasinglulu
1305*91f16700SchasingluluTF-A implements a framework that allows CPU and platform ports to perform
1306*91f16700Schasingluluactions very early after a CPU is released from reset in both the cold and warm
1307*91f16700Schasingluluboot paths. This is done by calling the ``reset_handler()`` function in both
1308*91f16700Schasingluluthe BL1 and BL31 images. It in turn calls the platform and CPU specific reset
1309*91f16700Schasingluluhandling functions.
1310*91f16700Schasinglulu
1311*91f16700SchasingluluDetails for implementing a CPU specific reset handler can be found in
1312*91f16700Schasinglulu:ref:`firmware_design_cpu_specific_reset_handling`. Details for implementing a
1313*91f16700Schasingluluplatform specific reset handler can be found in the :ref:`Porting Guide` (see
1314*91f16700Schasingluluthe``plat_reset_handler()`` function).
1315*91f16700Schasinglulu
1316*91f16700SchasingluluWhen adding functionality to a reset handler, keep in mind that if a different
1317*91f16700Schasinglulureset handling behavior is required between the first and the subsequent
1318*91f16700Schasingluluinvocations of the reset handling code, this should be detected at runtime.
1319*91f16700SchasingluluIn other words, the reset handler should be able to detect whether an action has
1320*91f16700Schasinglulualready been performed and act as appropriate. Possible courses of actions are,
1321*91f16700Schasinglulue.g. skip the action the second time, or undo/redo it.
1322*91f16700Schasinglulu
1323*91f16700Schasinglulu.. _configuring-secure-interrupts:
1324*91f16700Schasinglulu
1325*91f16700SchasingluluConfiguring secure interrupts
1326*91f16700Schasinglulu-----------------------------
1327*91f16700Schasinglulu
1328*91f16700SchasingluluThe GIC driver is responsible for performing initial configuration of secure
1329*91f16700Schasingluluinterrupts on the platform. To this end, the platform is expected to provide the
1330*91f16700SchasingluluGIC driver (either GICv2 or GICv3, as selected by the platform) with the
1331*91f16700Schasingluluinterrupt configuration during the driver initialisation.
1332*91f16700Schasinglulu
1333*91f16700SchasingluluSecure interrupt configuration are specified in an array of secure interrupt
1334*91f16700Schasingluluproperties. In this scheme, in both GICv2 and GICv3 driver data structures, the
1335*91f16700Schasinglulu``interrupt_props`` member points to an array of interrupt properties. Each
1336*91f16700Schasingluluelement of the array specifies the interrupt number and its attributes
1337*91f16700Schasinglulu(priority, group, configuration). Each element of the array shall be populated
1338*91f16700Schasingluluby the macro ``INTR_PROP_DESC()``. The macro takes the following arguments:
1339*91f16700Schasinglulu
1340*91f16700Schasinglulu- 13-bit interrupt number,
1341*91f16700Schasinglulu
1342*91f16700Schasinglulu- 8-bit interrupt priority,
1343*91f16700Schasinglulu
1344*91f16700Schasinglulu- Interrupt type (one of ``INTR_TYPE_EL3``, ``INTR_TYPE_S_EL1``,
1345*91f16700Schasinglulu  ``INTR_TYPE_NS``),
1346*91f16700Schasinglulu
1347*91f16700Schasinglulu- Interrupt configuration (either ``GIC_INTR_CFG_LEVEL`` or
1348*91f16700Schasinglulu  ``GIC_INTR_CFG_EDGE``).
1349*91f16700Schasinglulu
1350*91f16700Schasinglulu.. _firmware_design_cpu_ops_fwk:
1351*91f16700Schasinglulu
1352*91f16700SchasingluluCPU specific operations framework
1353*91f16700Schasinglulu---------------------------------
1354*91f16700Schasinglulu
1355*91f16700SchasingluluCertain aspects of the Armv8-A architecture are implementation defined,
1356*91f16700Schasingluluthat is, certain behaviours are not architecturally defined, but must be
1357*91f16700Schasingluludefined and documented by individual processor implementations. TF-A
1358*91f16700Schasingluluimplements a framework which categorises the common implementation defined
1359*91f16700Schasinglulubehaviours and allows a processor to export its implementation of that
1360*91f16700Schasinglulubehaviour. The categories are:
1361*91f16700Schasinglulu
1362*91f16700Schasinglulu#. Processor specific reset sequence.
1363*91f16700Schasinglulu
1364*91f16700Schasinglulu#. Processor specific power down sequences.
1365*91f16700Schasinglulu
1366*91f16700Schasinglulu#. Processor specific register dumping as a part of crash reporting.
1367*91f16700Schasinglulu
1368*91f16700Schasinglulu#. Errata status reporting.
1369*91f16700Schasinglulu
1370*91f16700SchasingluluEach of the above categories fulfils a different requirement.
1371*91f16700Schasinglulu
1372*91f16700Schasinglulu#. allows any processor specific initialization before the caches and MMU
1373*91f16700Schasinglulu   are turned on, like implementation of errata workarounds, entry into
1374*91f16700Schasinglulu   the intra-cluster coherency domain etc.
1375*91f16700Schasinglulu
1376*91f16700Schasinglulu#. allows each processor to implement the power down sequence mandated in
1377*91f16700Schasinglulu   its Technical Reference Manual (TRM).
1378*91f16700Schasinglulu
1379*91f16700Schasinglulu#. allows a processor to provide additional information to the developer
1380*91f16700Schasinglulu   in the event of a crash, for example Cortex-A53 has registers which
1381*91f16700Schasinglulu   can expose the data cache contents.
1382*91f16700Schasinglulu
1383*91f16700Schasinglulu#. allows a processor to define a function that inspects and reports the status
1384*91f16700Schasinglulu   of all errata workarounds on that processor.
1385*91f16700Schasinglulu
1386*91f16700SchasingluluPlease note that only 2. is mandated by the TRM.
1387*91f16700Schasinglulu
1388*91f16700SchasingluluThe CPU specific operations framework scales to accommodate a large number of
1389*91f16700Schasingluludifferent CPUs during power down and reset handling. The platform can specify
1390*91f16700Schasingluluany CPU optimization it wants to enable for each CPU. It can also specify
1391*91f16700Schasingluluthe CPU errata workarounds to be applied for each CPU type during reset
1392*91f16700Schasingluluhandling by defining CPU errata compile time macros. Details on these macros
1393*91f16700Schasinglulucan be found in the :ref:`Arm CPU Specific Build Macros` document.
1394*91f16700Schasinglulu
1395*91f16700SchasingluluThe CPU specific operations framework depends on the ``cpu_ops`` structure which
1396*91f16700Schasingluluneeds to be exported for each type of CPU in the platform. It is defined in
1397*91f16700Schasinglulu``include/lib/cpus/aarch64/cpu_macros.S`` and has the following fields : ``midr``,
1398*91f16700Schasinglulu``reset_func()``, ``cpu_pwr_down_ops`` (array of power down functions) and
1399*91f16700Schasinglulu``cpu_reg_dump()``.
1400*91f16700Schasinglulu
1401*91f16700SchasingluluThe CPU specific files in ``lib/cpus`` export a ``cpu_ops`` data structure with
1402*91f16700Schasinglulusuitable handlers for that CPU. For example, ``lib/cpus/aarch64/cortex_a53.S``
1403*91f16700Schasingluluexports the ``cpu_ops`` for Cortex-A53 CPU. According to the platform
1404*91f16700Schasingluluconfiguration, these CPU specific files must be included in the build by
1405*91f16700Schasingluluthe platform makefile. The generic CPU specific operations framework code exists
1406*91f16700Schasingluluin ``lib/cpus/aarch64/cpu_helpers.S``.
1407*91f16700Schasinglulu
1408*91f16700SchasingluluCPU PCS
1409*91f16700Schasinglulu~~~~~~~
1410*91f16700Schasinglulu
1411*91f16700SchasingluluAll assembly functions in CPU files are asked to follow a modified version of
1412*91f16700Schasingluluthe Procedure Call Standard (PCS) in their internals. This is done to ensure
1413*91f16700Schasinglulucalling these functions from outside the file doesn't unexpectedly corrupt
1414*91f16700Schasingluluregisters in the very early environment and to help the internals to be easier
1415*91f16700Schasingluluto understand. Please see the :ref:`firmware_design_cpu_errata_implementation`
1416*91f16700Schasinglulufor any function specific restrictions.
1417*91f16700Schasinglulu
1418*91f16700Schasinglulu+--------------+---------------------------------+
1419*91f16700Schasinglulu|   register   | use                             |
1420*91f16700Schasinglulu+==============+=================================+
1421*91f16700Schasinglulu|   x0 - x15   | scratch                         |
1422*91f16700Schasinglulu+--------------+---------------------------------+
1423*91f16700Schasinglulu|   x16, x17   | do not use (used by the linker) |
1424*91f16700Schasinglulu+--------------+---------------------------------+
1425*91f16700Schasinglulu|     x18      | do not use (platform register)  |
1426*91f16700Schasinglulu+--------------+---------------------------------+
1427*91f16700Schasinglulu|   x19 - x28  | callee saved                    |
1428*91f16700Schasinglulu+--------------+---------------------------------+
1429*91f16700Schasinglulu|   x29, x30   | FP, LR                          |
1430*91f16700Schasinglulu+--------------+---------------------------------+
1431*91f16700Schasinglulu
1432*91f16700Schasinglulu.. _firmware_design_cpu_specific_reset_handling:
1433*91f16700Schasinglulu
1434*91f16700SchasingluluCPU specific Reset Handling
1435*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~
1436*91f16700Schasinglulu
1437*91f16700SchasingluluAfter a reset, the state of the CPU when it calls generic reset handler is:
1438*91f16700SchasingluluMMU turned off, both instruction and data caches turned off, not part
1439*91f16700Schasingluluof any coherency domain and no stack.
1440*91f16700Schasinglulu
1441*91f16700SchasingluluThe BL entrypoint code first invokes the ``plat_reset_handler()`` to allow
1442*91f16700Schasingluluthe platform to perform any system initialization required and any system
1443*91f16700Schasingluluerrata workarounds that needs to be applied. The ``get_cpu_ops_ptr()`` reads
1444*91f16700Schasingluluthe current CPU midr, finds the matching ``cpu_ops`` entry in the ``cpu_ops``
1445*91f16700Schasingluluarray and returns it. Note that only the part number and implementer fields
1446*91f16700Schasingluluin midr are used to find the matching ``cpu_ops`` entry. The ``reset_func()`` in
1447*91f16700Schasingluluthe returned ``cpu_ops`` is then invoked which executes the required reset
1448*91f16700Schasingluluhandling for that CPU and also any errata workarounds enabled by the platform.
1449*91f16700Schasinglulu
1450*91f16700SchasingluluIt should be defined using the ``cpu_reset_func_{start,end}`` macros and its
1451*91f16700Schasinglulubody may only clobber x0 to x14 with x14 being the cpu_rev parameter.
1452*91f16700Schasinglulu
1453*91f16700SchasingluluCPU specific power down sequence
1454*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1455*91f16700Schasinglulu
1456*91f16700SchasingluluDuring the BL31 initialization sequence, the pointer to the matching ``cpu_ops``
1457*91f16700Schasingluluentry is stored in per-CPU data by ``init_cpu_ops()`` so that it can be quickly
1458*91f16700Schasingluluretrieved during power down sequences.
1459*91f16700Schasinglulu
1460*91f16700SchasingluluVarious CPU drivers register handlers to perform power down at certain power
1461*91f16700Schasinglululevels for that specific CPU. The PSCI service, upon receiving a power down
1462*91f16700Schasinglulurequest, determines the highest power level at which to execute power down
1463*91f16700Schasinglulusequence for a particular CPU. It uses the ``prepare_cpu_pwr_dwn()`` function to
1464*91f16700Schasinglulupick the right power down handler for the requested level. The function
1465*91f16700Schasingluluretrieves ``cpu_ops`` pointer member of per-CPU data, and from that, further
1466*91f16700Schasingluluretrieves ``cpu_pwr_down_ops`` array, and indexes into the required level. If the
1467*91f16700Schasinglulurequested power level is higher than what a CPU driver supports, the handler
1468*91f16700Schasingluluregistered for highest level is invoked.
1469*91f16700Schasinglulu
1470*91f16700SchasingluluAt runtime the platform hooks for power down are invoked by the PSCI service to
1471*91f16700Schasingluluperform platform specific operations during a power down sequence, for example
1472*91f16700Schasingluluturning off CCI coherency during a cluster power down.
1473*91f16700Schasinglulu
1474*91f16700SchasingluluCPU specific register reporting during crash
1475*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1476*91f16700Schasinglulu
1477*91f16700SchasingluluIf the crash reporting is enabled in BL31, when a crash occurs, the crash
1478*91f16700Schasinglulureporting framework calls ``do_cpu_reg_dump`` which retrieves the matching
1479*91f16700Schasinglulu``cpu_ops`` using ``get_cpu_ops_ptr()`` function. The ``cpu_reg_dump()`` in
1480*91f16700Schasinglulu``cpu_ops`` is invoked, which then returns the CPU specific register values to
1481*91f16700Schasinglulube reported and a pointer to the ASCII list of register names in a format
1482*91f16700Schasingluluexpected by the crash reporting framework.
1483*91f16700Schasinglulu
1484*91f16700Schasinglulu.. _firmware_design_cpu_errata_implementation:
1485*91f16700Schasinglulu
1486*91f16700SchasingluluCPU errata implementation
1487*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~
1488*91f16700Schasinglulu
1489*91f16700SchasingluluErrata workarounds for CPUs supported in TF-A are applied during both cold and
1490*91f16700Schasingluluwarm boots, shortly after reset. Individual Errata workarounds are enabled as
1491*91f16700Schasinglulubuild options. Some errata workarounds have potential run-time implications;
1492*91f16700Schasinglulutherefore some are enabled by default, others not. Platform ports shall
1493*91f16700Schasingluluoverride build options to enable or disable errata as appropriate. The CPU
1494*91f16700Schasingluludrivers take care of applying errata workarounds that are enabled and applicable
1495*91f16700Schasingluluto a given CPU.
1496*91f16700Schasinglulu
1497*91f16700SchasingluluEach erratum has a build flag in ``lib/cpus/cpu-ops.mk`` of the form:
1498*91f16700Schasinglulu``ERRATA_<cpu_num>_<erratum_id>``. It also has a short description in
1499*91f16700Schasinglulu:ref:`arm_cpu_macros_errata_workarounds` on when it should apply.
1500*91f16700Schasinglulu
1501*91f16700SchasingluluErrata framework
1502*91f16700Schasinglulu^^^^^^^^^^^^^^^^
1503*91f16700Schasinglulu
1504*91f16700SchasingluluThe errata framework is a convention and a small library to allow errata to be
1505*91f16700Schasingluluautomatically discovered. It enables compliant errata to be automatically
1506*91f16700Schasingluluapplied and reported at runtime (either by status reporting or the errata ABI).
1507*91f16700Schasinglulu
1508*91f16700SchasingluluTo write a compliant mitigation for erratum number ``erratum_id`` on a cpu that
1509*91f16700Schasingluludeclared itself (with ``declare_cpu_ops``) as ``cpu_name`` one needs 3 things:
1510*91f16700Schasinglulu
1511*91f16700Schasinglulu#. A CPU revision checker function: ``check_erratum_<cpu_name>_<erratum_id>``
1512*91f16700Schasinglulu
1513*91f16700Schasinglulu   It should check whether this erratum applies on this revision of this CPU.
1514*91f16700Schasinglulu   It will be called with the CPU revision as its first parameter (x0) and
1515*91f16700Schasinglulu   should return one of ``ERRATA_APPLIES`` or ``ERRATA_NOT_APPLIES``.
1516*91f16700Schasinglulu
1517*91f16700Schasinglulu   It may only clobber x0 to x4. The rest should be treated as callee-saved.
1518*91f16700Schasinglulu
1519*91f16700Schasinglulu#. A workaround function: ``erratum_<cpu_name>_<erratum_id>_wa``
1520*91f16700Schasinglulu
1521*91f16700Schasinglulu   It should obtain the cpu revision (with ``cpu_get_rev_var``), call its
1522*91f16700Schasinglulu   revision checker, and perform the mitigation, should the erratum apply.
1523*91f16700Schasinglulu
1524*91f16700Schasinglulu   It may only clobber x0 to x8. The rest should be treated as callee-saved.
1525*91f16700Schasinglulu
1526*91f16700Schasinglulu#. Register itself to the framework
1527*91f16700Schasinglulu
1528*91f16700Schasinglulu   Do this with
1529*91f16700Schasinglulu   ``add_erratum_entry <cpu_name>, ERRATUM(<erratum_id>), <errata_flag>``
1530*91f16700Schasinglulu   where the ``errata_flag`` is the enable flag in ``cpu-ops.mk`` described
1531*91f16700Schasinglulu   above.
1532*91f16700Schasinglulu
1533*91f16700SchasingluluSee the next section on how to do this easily.
1534*91f16700Schasinglulu
1535*91f16700Schasinglulu.. note::
1536*91f16700Schasinglulu
1537*91f16700Schasinglulu CVEs have the format ``CVE_<year>_<number>``. To fit them in the framework, the
1538*91f16700Schasinglulu ``erratum_id`` for the checker and the workaround functions become the
1539*91f16700Schasinglulu ``number`` part of its name and the ``ERRATUM(<number>)`` part of the
1540*91f16700Schasinglulu registration should instead be ``CVE(<year>, <number>)``. In the extremely
1541*91f16700Schasinglulu unlikely scenario where a CVE and an erratum numbers clash, the CVE number
1542*91f16700Schasinglulu should be prefixed with a zero.
1543*91f16700Schasinglulu
1544*91f16700Schasinglulu Also, their build flag should be ``WORKAROUND_CVE_<year>_<number>``.
1545*91f16700Schasinglulu
1546*91f16700Schasinglulu.. note::
1547*91f16700Schasinglulu
1548*91f16700Schasinglulu AArch32 uses the legacy convention. The checker function has the format
1549*91f16700Schasinglulu ``check_errata_<erratum_id>`` and the workaround has the format
1550*91f16700Schasinglulu ``errata_<cpu_number>_<erratum_id>_wa`` where ``cpu_number`` is the shortform
1551*91f16700Schasinglulu letter and number name of the CPU.
1552*91f16700Schasinglulu
1553*91f16700Schasinglulu For CVEs the ``erratum_id`` also becomes ``cve_<year>_<number>``.
1554*91f16700Schasinglulu
1555*91f16700SchasingluluErrata framework helpers
1556*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^
1557*91f16700Schasinglulu
1558*91f16700SchasingluluWriting these errata involves lots of boilerplate and repetitive code. On
1559*91f16700SchasingluluAArch64 there are helpers to omit most of this. They are located in
1560*91f16700Schasinglulu``include/lib/cpus/aarch64/cpu_macros.S`` and the preferred way to implement
1561*91f16700Schasingluluerrata. Please see their comments on how to use them.
1562*91f16700Schasinglulu
1563*91f16700SchasingluluThe most common type of erratum workaround, one that just sets a "chicken" bit
1564*91f16700Schasingluluin some arbitrary register, would have an implementation for the Cortex-A77,
1565*91f16700Schasingluluerratum #1925769 like::
1566*91f16700Schasinglulu
1567*91f16700Schasinglulu    workaround_reset_start cortex_a77, ERRATUM(1925769), ERRATA_A77_1925769
1568*91f16700Schasinglulu        sysreg_bit_set CORTEX_A77_CPUECTLR_EL1, CORTEX_A77_CPUECTLR_EL1_BIT_8
1569*91f16700Schasinglulu    workaround_reset_end cortex_a77, ERRATUM(1925769)
1570*91f16700Schasinglulu
1571*91f16700Schasinglulu    check_erratum_ls cortex_a77, ERRATUM(1925769), CPU_REV(1, 1)
1572*91f16700Schasinglulu
1573*91f16700SchasingluluStatus reporting
1574*91f16700Schasinglulu^^^^^^^^^^^^^^^^
1575*91f16700Schasinglulu
1576*91f16700SchasingluluIn a debug build of TF-A, on a CPU that comes out of reset, both BL1 and the
1577*91f16700Schasingluluruntime firmware (BL31 in AArch64, and BL32 in AArch32) will invoke a generic
1578*91f16700Schasingluluerrata status reporting function. It will read the ``errata_entries`` list of
1579*91f16700Schasingluluthat cpu and will report whether each known erratum was applied and, if not,
1580*91f16700Schasingluluwhether it should have been.
1581*91f16700Schasinglulu
1582*91f16700SchasingluluReporting the status of errata workaround is for informational purpose only; it
1583*91f16700Schasingluluhas no functional significance.
1584*91f16700Schasinglulu
1585*91f16700SchasingluluMemory layout of BL images
1586*91f16700Schasinglulu--------------------------
1587*91f16700Schasinglulu
1588*91f16700SchasingluluEach bootloader image can be divided in 2 parts:
1589*91f16700Schasinglulu
1590*91f16700Schasinglulu-  the static contents of the image. These are data actually stored in the
1591*91f16700Schasinglulu   binary on the disk. In the ELF terminology, they are called ``PROGBITS``
1592*91f16700Schasinglulu   sections;
1593*91f16700Schasinglulu
1594*91f16700Schasinglulu-  the run-time contents of the image. These are data that don't occupy any
1595*91f16700Schasinglulu   space in the binary on the disk. The ELF binary just contains some
1596*91f16700Schasinglulu   metadata indicating where these data will be stored at run-time and the
1597*91f16700Schasinglulu   corresponding sections need to be allocated and initialized at run-time.
1598*91f16700Schasinglulu   In the ELF terminology, they are called ``NOBITS`` sections.
1599*91f16700Schasinglulu
1600*91f16700SchasingluluAll PROGBITS sections are grouped together at the beginning of the image,
1601*91f16700Schasinglulufollowed by all NOBITS sections. This is true for all TF-A images and it is
1602*91f16700Schasinglulugoverned by the linker scripts. This ensures that the raw binary images are
1603*91f16700Schasingluluas small as possible. If a NOBITS section was inserted in between PROGBITS
1604*91f16700Schasinglulusections then the resulting binary file would contain zero bytes in place of
1605*91f16700Schasingluluthis NOBITS section, making the image unnecessarily bigger. Smaller images
1606*91f16700Schasingluluallow faster loading from the FIP to the main memory.
1607*91f16700Schasinglulu
1608*91f16700SchasingluluFor BL31, a platform can specify an alternate location for NOBITS sections
1609*91f16700Schasinglulu(other than immediately following PROGBITS sections) by setting
1610*91f16700Schasinglulu``SEPARATE_NOBITS_REGION`` to 1 and defining ``BL31_NOBITS_BASE`` and
1611*91f16700Schasinglulu``BL31_NOBITS_LIMIT``.
1612*91f16700Schasinglulu
1613*91f16700SchasingluluLinker scripts and symbols
1614*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~
1615*91f16700Schasinglulu
1616*91f16700SchasingluluEach bootloader stage image layout is described by its own linker script. The
1617*91f16700Schasinglululinker scripts export some symbols into the program symbol table. Their values
1618*91f16700Schasinglulucorrespond to particular addresses. TF-A code can refer to these symbols to
1619*91f16700Schasinglulufigure out the image memory layout.
1620*91f16700Schasinglulu
1621*91f16700SchasingluluLinker symbols follow the following naming convention in TF-A.
1622*91f16700Schasinglulu
1623*91f16700Schasinglulu-  ``__<SECTION>_START__``
1624*91f16700Schasinglulu
1625*91f16700Schasinglulu   Start address of a given section named ``<SECTION>``.
1626*91f16700Schasinglulu
1627*91f16700Schasinglulu-  ``__<SECTION>_END__``
1628*91f16700Schasinglulu
1629*91f16700Schasinglulu   End address of a given section named ``<SECTION>``. If there is an alignment
1630*91f16700Schasinglulu   constraint on the section's end address then ``__<SECTION>_END__`` corresponds
1631*91f16700Schasinglulu   to the end address of the section's actual contents, rounded up to the right
1632*91f16700Schasinglulu   boundary. Refer to the value of ``__<SECTION>_UNALIGNED_END__`` to know the
1633*91f16700Schasinglulu   actual end address of the section's contents.
1634*91f16700Schasinglulu
1635*91f16700Schasinglulu-  ``__<SECTION>_UNALIGNED_END__``
1636*91f16700Schasinglulu
1637*91f16700Schasinglulu   End address of a given section named ``<SECTION>`` without any padding or
1638*91f16700Schasinglulu   rounding up due to some alignment constraint.
1639*91f16700Schasinglulu
1640*91f16700Schasinglulu-  ``__<SECTION>_SIZE__``
1641*91f16700Schasinglulu
1642*91f16700Schasinglulu   Size (in bytes) of a given section named ``<SECTION>``. If there is an
1643*91f16700Schasinglulu   alignment constraint on the section's end address then ``__<SECTION>_SIZE__``
1644*91f16700Schasinglulu   corresponds to the size of the section's actual contents, rounded up to the
1645*91f16700Schasinglulu   right boundary. In other words, ``__<SECTION>_SIZE__ = __<SECTION>_END__ - _<SECTION>_START__``. Refer to the value of ``__<SECTION>_UNALIGNED_SIZE__``
1646*91f16700Schasinglulu   to know the actual size of the section's contents.
1647*91f16700Schasinglulu
1648*91f16700Schasinglulu-  ``__<SECTION>_UNALIGNED_SIZE__``
1649*91f16700Schasinglulu
1650*91f16700Schasinglulu   Size (in bytes) of a given section named ``<SECTION>`` without any padding or
1651*91f16700Schasinglulu   rounding up due to some alignment constraint. In other words,
1652*91f16700Schasinglulu   ``__<SECTION>_UNALIGNED_SIZE__ = __<SECTION>_UNALIGNED_END__ - __<SECTION>_START__``.
1653*91f16700Schasinglulu
1654*91f16700SchasingluluSome of the linker symbols are mandatory as TF-A code relies on them to be
1655*91f16700Schasingluludefined. They are listed in the following subsections. Some of them must be
1656*91f16700Schasingluluprovided for each bootloader stage and some are specific to a given bootloader
1657*91f16700Schasinglulustage.
1658*91f16700Schasinglulu
1659*91f16700SchasingluluThe linker scripts define some extra, optional symbols. They are not actually
1660*91f16700Schasingluluused by any code but they help in understanding the bootloader images' memory
1661*91f16700Schasinglululayout as they are easy to spot in the link map files.
1662*91f16700Schasinglulu
1663*91f16700SchasingluluCommon linker symbols
1664*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^
1665*91f16700Schasinglulu
1666*91f16700SchasingluluAll BL images share the following requirements:
1667*91f16700Schasinglulu
1668*91f16700Schasinglulu-  The BSS section must be zero-initialised before executing any C code.
1669*91f16700Schasinglulu-  The coherent memory section (if enabled) must be zero-initialised as well.
1670*91f16700Schasinglulu-  The MMU setup code needs to know the extents of the coherent and read-only
1671*91f16700Schasinglulu   memory regions to set the right memory attributes. When
1672*91f16700Schasinglulu   ``SEPARATE_CODE_AND_RODATA=1``, it needs to know more specifically how the
1673*91f16700Schasinglulu   read-only memory region is divided between code and data.
1674*91f16700Schasinglulu
1675*91f16700SchasingluluThe following linker symbols are defined for this purpose:
1676*91f16700Schasinglulu
1677*91f16700Schasinglulu-  ``__BSS_START__``
1678*91f16700Schasinglulu-  ``__BSS_SIZE__``
1679*91f16700Schasinglulu-  ``__COHERENT_RAM_START__`` Must be aligned on a page-size boundary.
1680*91f16700Schasinglulu-  ``__COHERENT_RAM_END__`` Must be aligned on a page-size boundary.
1681*91f16700Schasinglulu-  ``__COHERENT_RAM_UNALIGNED_SIZE__``
1682*91f16700Schasinglulu-  ``__RO_START__``
1683*91f16700Schasinglulu-  ``__RO_END__``
1684*91f16700Schasinglulu-  ``__TEXT_START__``
1685*91f16700Schasinglulu-  ``__TEXT_END_UNALIGNED__``
1686*91f16700Schasinglulu-  ``__TEXT_END__``
1687*91f16700Schasinglulu-  ``__RODATA_START__``
1688*91f16700Schasinglulu-  ``__RODATA_END_UNALIGNED__``
1689*91f16700Schasinglulu-  ``__RODATA_END__``
1690*91f16700Schasinglulu
1691*91f16700SchasingluluBL1's linker symbols
1692*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^
1693*91f16700Schasinglulu
1694*91f16700SchasingluluBL1 being the ROM image, it has additional requirements. BL1 resides in ROM and
1695*91f16700Schasingluluit is entirely executed in place but it needs some read-write memory for its
1696*91f16700Schasinglulumutable data. Its ``.data`` section (i.e. its allocated read-write data) must be
1697*91f16700Schasinglulurelocated from ROM to RAM before executing any C code.
1698*91f16700Schasinglulu
1699*91f16700SchasingluluThe following additional linker symbols are defined for BL1:
1700*91f16700Schasinglulu
1701*91f16700Schasinglulu-  ``__BL1_ROM_END__`` End address of BL1's ROM contents, covering its code
1702*91f16700Schasinglulu   and ``.data`` section in ROM.
1703*91f16700Schasinglulu-  ``__DATA_ROM_START__`` Start address of the ``.data`` section in ROM. Must be
1704*91f16700Schasinglulu   aligned on a 16-byte boundary.
1705*91f16700Schasinglulu-  ``__DATA_RAM_START__`` Address in RAM where the ``.data`` section should be
1706*91f16700Schasinglulu   copied over. Must be aligned on a 16-byte boundary.
1707*91f16700Schasinglulu-  ``__DATA_SIZE__`` Size of the ``.data`` section (in ROM or RAM).
1708*91f16700Schasinglulu-  ``__BL1_RAM_START__`` Start address of BL1 read-write data.
1709*91f16700Schasinglulu-  ``__BL1_RAM_END__`` End address of BL1 read-write data.
1710*91f16700Schasinglulu
1711*91f16700SchasingluluHow to choose the right base addresses for each bootloader stage image
1712*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1713*91f16700Schasinglulu
1714*91f16700SchasingluluThere is currently no support for dynamic image loading in TF-A. This means
1715*91f16700Schasingluluthat all bootloader images need to be linked against their ultimate runtime
1716*91f16700Schasinglululocations and the base addresses of each image must be chosen carefully such
1717*91f16700Schasingluluthat images don't overlap each other in an undesired way. As the code grows,
1718*91f16700Schasingluluthe base addresses might need adjustments to cope with the new memory layout.
1719*91f16700Schasinglulu
1720*91f16700SchasingluluThe memory layout is completely specific to the platform and so there is no
1721*91f16700Schasinglulugeneral recipe for choosing the right base addresses for each bootloader image.
1722*91f16700SchasingluluHowever, there are tools to aid in understanding the memory layout. These are
1723*91f16700Schasingluluthe link map files: ``build/<platform>/<build-type>/bl<x>/bl<x>.map``, with ``<x>``
1724*91f16700Schasinglulubeing the stage bootloader. They provide a detailed view of the memory usage of
1725*91f16700Schasinglulueach image. Among other useful information, they provide the end address of
1726*91f16700Schasinglulueach image.
1727*91f16700Schasinglulu
1728*91f16700Schasinglulu-  ``bl1.map`` link map file provides ``__BL1_RAM_END__`` address.
1729*91f16700Schasinglulu-  ``bl2.map`` link map file provides ``__BL2_END__`` address.
1730*91f16700Schasinglulu-  ``bl31.map`` link map file provides ``__BL31_END__`` address.
1731*91f16700Schasinglulu-  ``bl32.map`` link map file provides ``__BL32_END__`` address.
1732*91f16700Schasinglulu
1733*91f16700SchasingluluFor each bootloader image, the platform code must provide its start address
1734*91f16700Schasingluluas well as a limit address that it must not overstep. The latter is used in the
1735*91f16700Schasinglululinker scripts to check that the image doesn't grow past that address. If that
1736*91f16700Schasingluluhappens, the linker will issue a message similar to the following:
1737*91f16700Schasinglulu
1738*91f16700Schasinglulu::
1739*91f16700Schasinglulu
1740*91f16700Schasinglulu    aarch64-none-elf-ld: BLx has exceeded its limit.
1741*91f16700Schasinglulu
1742*91f16700SchasingluluAdditionally, if the platform memory layout implies some image overlaying like
1743*91f16700Schasingluluon FVP, BL31 and TSP need to know the limit address that their PROGBITS
1744*91f16700Schasinglulusections must not overstep. The platform code must provide those.
1745*91f16700Schasinglulu
1746*91f16700SchasingluluTF-A does not provide any mechanism to verify at boot time that the memory
1747*91f16700Schasingluluto load a new image is free to prevent overwriting a previously loaded image.
1748*91f16700SchasingluluThe platform must specify the memory available in the system for all the
1749*91f16700Schasinglulurelevant BL images to be loaded.
1750*91f16700Schasinglulu
1751*91f16700SchasingluluFor example, in the case of BL1 loading BL2, ``bl1_plat_sec_mem_layout()`` will
1752*91f16700Schasinglulureturn the region defined by the platform where BL1 intends to load BL2. The
1753*91f16700Schasinglulu``load_image()`` function performs bounds check for the image size based on the
1754*91f16700Schasinglulubase and maximum image size provided by the platforms. Platforms must take
1755*91f16700Schasingluluthis behaviour into account when defining the base/size for each of the images.
1756*91f16700Schasinglulu
1757*91f16700SchasingluluMemory layout on Arm development platforms
1758*91f16700Schasinglulu^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1759*91f16700Schasinglulu
1760*91f16700SchasingluluThe following list describes the memory layout on the Arm development platforms:
1761*91f16700Schasinglulu
1762*91f16700Schasinglulu-  A 4KB page of shared memory is used for communication between Trusted
1763*91f16700Schasinglulu   Firmware and the platform's power controller. This is located at the base of
1764*91f16700Schasinglulu   Trusted SRAM. The amount of Trusted SRAM available to load the bootloader
1765*91f16700Schasinglulu   images is reduced by the size of the shared memory.
1766*91f16700Schasinglulu
1767*91f16700Schasinglulu   The shared memory is used to store the CPUs' entrypoint mailbox. On Juno,
1768*91f16700Schasinglulu   this is also used for the MHU payload when passing messages to and from the
1769*91f16700Schasinglulu   SCP.
1770*91f16700Schasinglulu
1771*91f16700Schasinglulu-  Another 4 KB page is reserved for passing memory layout between BL1 and BL2
1772*91f16700Schasinglulu   and also the dynamic firmware configurations.
1773*91f16700Schasinglulu
1774*91f16700Schasinglulu-  On FVP, BL1 is originally sitting in the Trusted ROM at address ``0x0``. On
1775*91f16700Schasinglulu   Juno, BL1 resides in flash memory at address ``0x0BEC0000``. BL1 read-write
1776*91f16700Schasinglulu   data are relocated to the top of Trusted SRAM at runtime.
1777*91f16700Schasinglulu
1778*91f16700Schasinglulu-  BL2 is loaded below BL1 RW
1779*91f16700Schasinglulu
1780*91f16700Schasinglulu-  EL3 Runtime Software, BL31 for AArch64 and BL32 for AArch32 (e.g. SP_MIN),
1781*91f16700Schasinglulu   is loaded at the top of the Trusted SRAM, such that its NOBITS sections will
1782*91f16700Schasinglulu   overwrite BL1 R/W data and BL2. This implies that BL1 global variables
1783*91f16700Schasinglulu   remain valid only until execution reaches the EL3 Runtime Software entry
1784*91f16700Schasinglulu   point during a cold boot.
1785*91f16700Schasinglulu
1786*91f16700Schasinglulu-  On Juno, SCP_BL2 is loaded temporarily into the EL3 Runtime Software memory
1787*91f16700Schasinglulu   region and transferred to the SCP before being overwritten by EL3 Runtime
1788*91f16700Schasinglulu   Software.
1789*91f16700Schasinglulu
1790*91f16700Schasinglulu-  BL32 (for AArch64) can be loaded in one of the following locations:
1791*91f16700Schasinglulu
1792*91f16700Schasinglulu   -  Trusted SRAM
1793*91f16700Schasinglulu   -  Trusted DRAM (FVP only)
1794*91f16700Schasinglulu   -  Secure region of DRAM (top 16MB of DRAM configured by the TrustZone
1795*91f16700Schasinglulu      controller)
1796*91f16700Schasinglulu
1797*91f16700Schasinglulu   When BL32 (for AArch64) is loaded into Trusted SRAM, it is loaded below
1798*91f16700Schasinglulu   BL31.
1799*91f16700Schasinglulu
1800*91f16700SchasingluluThe location of the BL32 image will result in different memory maps. This is
1801*91f16700Schasingluluillustrated for both FVP and Juno in the following diagrams, using the TSP as
1802*91f16700Schasingluluan example.
1803*91f16700Schasinglulu
1804*91f16700Schasinglulu.. note::
1805*91f16700Schasinglulu   Loading the BL32 image in TZC secured DRAM doesn't change the memory
1806*91f16700Schasinglulu   layout of the other images in Trusted SRAM.
1807*91f16700Schasinglulu
1808*91f16700SchasingluluCONFIG section in memory layouts shown below contains:
1809*91f16700Schasinglulu
1810*91f16700Schasinglulu::
1811*91f16700Schasinglulu
1812*91f16700Schasinglulu    +--------------------+
1813*91f16700Schasinglulu    |bl2_mem_params_descs|
1814*91f16700Schasinglulu    |--------------------|
1815*91f16700Schasinglulu    |     fw_configs     |
1816*91f16700Schasinglulu    +--------------------+
1817*91f16700Schasinglulu
1818*91f16700Schasinglulu``bl2_mem_params_descs`` contains parameters passed from BL2 to next the
1819*91f16700SchasingluluBL image during boot.
1820*91f16700Schasinglulu
1821*91f16700Schasinglulu``fw_configs`` includes soc_fw_config, tos_fw_config, tb_fw_config and fw_config.
1822*91f16700Schasinglulu
1823*91f16700Schasinglulu**FVP with TSP in Trusted SRAM with firmware configs :**
1824*91f16700Schasinglulu(These diagrams only cover the AArch64 case)
1825*91f16700Schasinglulu
1826*91f16700Schasinglulu::
1827*91f16700Schasinglulu
1828*91f16700Schasinglulu                   DRAM
1829*91f16700Schasinglulu    0xffffffff +----------+
1830*91f16700Schasinglulu               | EL3 TZC  |
1831*91f16700Schasinglulu    0xffe00000 |----------| (secure)
1832*91f16700Schasinglulu               | AP TZC   |
1833*91f16700Schasinglulu    0xff000000 +----------+
1834*91f16700Schasinglulu               :          :
1835*91f16700Schasinglulu    0x82100000 |----------|
1836*91f16700Schasinglulu               |HW_CONFIG |
1837*91f16700Schasinglulu    0x82000000 |----------|  (non-secure)
1838*91f16700Schasinglulu               |          |
1839*91f16700Schasinglulu    0x80000000 +----------+
1840*91f16700Schasinglulu
1841*91f16700Schasinglulu               Trusted DRAM
1842*91f16700Schasinglulu    0x08000000 +----------+
1843*91f16700Schasinglulu               |HW_CONFIG |
1844*91f16700Schasinglulu    0x07f00000 |----------|
1845*91f16700Schasinglulu               :          :
1846*91f16700Schasinglulu               |          |
1847*91f16700Schasinglulu    0x06000000 +----------+
1848*91f16700Schasinglulu
1849*91f16700Schasinglulu               Trusted SRAM
1850*91f16700Schasinglulu    0x04040000 +----------+  loaded by BL2  +----------------+
1851*91f16700Schasinglulu               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1852*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1853*91f16700Schasinglulu               |   BL2    |  <<<<<<<<<<<<<  |                |
1854*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |----------------|
1855*91f16700Schasinglulu               |          |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1856*91f16700Schasinglulu               |          |  <<<<<<<<<<<<<  |----------------|
1857*91f16700Schasinglulu               |          |  <<<<<<<<<<<<<  |     BL32       |
1858*91f16700Schasinglulu    0x04003000 +----------+                 +----------------+
1859*91f16700Schasinglulu               |  CONFIG  |
1860*91f16700Schasinglulu    0x04001000 +----------+
1861*91f16700Schasinglulu               |  Shared  |
1862*91f16700Schasinglulu    0x04000000 +----------+
1863*91f16700Schasinglulu
1864*91f16700Schasinglulu               Trusted ROM
1865*91f16700Schasinglulu    0x04000000 +----------+
1866*91f16700Schasinglulu               | BL1 (ro) |
1867*91f16700Schasinglulu    0x00000000 +----------+
1868*91f16700Schasinglulu
1869*91f16700Schasinglulu**FVP with TSP in Trusted DRAM with firmware configs (default option):**
1870*91f16700Schasinglulu
1871*91f16700Schasinglulu::
1872*91f16700Schasinglulu
1873*91f16700Schasinglulu                     DRAM
1874*91f16700Schasinglulu    0xffffffff +--------------+
1875*91f16700Schasinglulu               |   EL3 TZC    |
1876*91f16700Schasinglulu    0xffe00000 |--------------|  (secure)
1877*91f16700Schasinglulu               |   AP TZC     |
1878*91f16700Schasinglulu    0xff000000 +--------------+
1879*91f16700Schasinglulu               :              :
1880*91f16700Schasinglulu    0x82100000 |--------------|
1881*91f16700Schasinglulu               |  HW_CONFIG   |
1882*91f16700Schasinglulu    0x82000000 |--------------|  (non-secure)
1883*91f16700Schasinglulu               |              |
1884*91f16700Schasinglulu    0x80000000 +--------------+
1885*91f16700Schasinglulu
1886*91f16700Schasinglulu                 Trusted DRAM
1887*91f16700Schasinglulu    0x08000000 +--------------+
1888*91f16700Schasinglulu               |  HW_CONFIG   |
1889*91f16700Schasinglulu    0x07f00000 |--------------|
1890*91f16700Schasinglulu               :              :
1891*91f16700Schasinglulu               |    BL32      |
1892*91f16700Schasinglulu    0x06000000 +--------------+
1893*91f16700Schasinglulu
1894*91f16700Schasinglulu                 Trusted SRAM
1895*91f16700Schasinglulu    0x04040000 +--------------+  loaded by BL2  +----------------+
1896*91f16700Schasinglulu               |   BL1 (rw)   |  <<<<<<<<<<<<<  |                |
1897*91f16700Schasinglulu               |--------------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1898*91f16700Schasinglulu               |     BL2      |  <<<<<<<<<<<<<  |                |
1899*91f16700Schasinglulu               |--------------|  <<<<<<<<<<<<<  |----------------|
1900*91f16700Schasinglulu               |              |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1901*91f16700Schasinglulu               |              |                 +----------------+
1902*91f16700Schasinglulu    0x04003000 +--------------+
1903*91f16700Schasinglulu               |    CONFIG    |
1904*91f16700Schasinglulu    0x04001000 +--------------+
1905*91f16700Schasinglulu               |    Shared    |
1906*91f16700Schasinglulu    0x04000000 +--------------+
1907*91f16700Schasinglulu
1908*91f16700Schasinglulu                 Trusted ROM
1909*91f16700Schasinglulu    0x04000000 +--------------+
1910*91f16700Schasinglulu               |   BL1 (ro)   |
1911*91f16700Schasinglulu    0x00000000 +--------------+
1912*91f16700Schasinglulu
1913*91f16700Schasinglulu**FVP with TSP in TZC-Secured DRAM with firmware configs :**
1914*91f16700Schasinglulu
1915*91f16700Schasinglulu::
1916*91f16700Schasinglulu
1917*91f16700Schasinglulu                   DRAM
1918*91f16700Schasinglulu    0xffffffff +----------+
1919*91f16700Schasinglulu               |  EL3 TZC |
1920*91f16700Schasinglulu    0xffe00000 |----------|  (secure)
1921*91f16700Schasinglulu               |  AP TZC  |
1922*91f16700Schasinglulu               |  (BL32)  |
1923*91f16700Schasinglulu    0xff000000 +----------+
1924*91f16700Schasinglulu               |          |
1925*91f16700Schasinglulu    0x82100000 |----------|
1926*91f16700Schasinglulu               |HW_CONFIG |
1927*91f16700Schasinglulu    0x82000000 |----------|  (non-secure)
1928*91f16700Schasinglulu               |          |
1929*91f16700Schasinglulu    0x80000000 +----------+
1930*91f16700Schasinglulu
1931*91f16700Schasinglulu               Trusted DRAM
1932*91f16700Schasinglulu    0x08000000 +----------+
1933*91f16700Schasinglulu               |HW_CONFIG |
1934*91f16700Schasinglulu    0x7f000000 |----------|
1935*91f16700Schasinglulu               :          :
1936*91f16700Schasinglulu               |          |
1937*91f16700Schasinglulu    0x06000000 +----------+
1938*91f16700Schasinglulu
1939*91f16700Schasinglulu               Trusted SRAM
1940*91f16700Schasinglulu    0x04040000 +----------+  loaded by BL2  +----------------+
1941*91f16700Schasinglulu               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1942*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1943*91f16700Schasinglulu               |   BL2    |  <<<<<<<<<<<<<  |                |
1944*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |----------------|
1945*91f16700Schasinglulu               |          |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1946*91f16700Schasinglulu               |          |                 +----------------+
1947*91f16700Schasinglulu    0x04003000 +----------+
1948*91f16700Schasinglulu               |  CONFIG  |
1949*91f16700Schasinglulu    0x04001000 +----------+
1950*91f16700Schasinglulu               |  Shared  |
1951*91f16700Schasinglulu    0x04000000 +----------+
1952*91f16700Schasinglulu
1953*91f16700Schasinglulu               Trusted ROM
1954*91f16700Schasinglulu    0x04000000 +----------+
1955*91f16700Schasinglulu               | BL1 (ro) |
1956*91f16700Schasinglulu    0x00000000 +----------+
1957*91f16700Schasinglulu
1958*91f16700Schasinglulu**Juno with BL32 in Trusted SRAM :**
1959*91f16700Schasinglulu
1960*91f16700Schasinglulu::
1961*91f16700Schasinglulu
1962*91f16700Schasinglulu                  DRAM
1963*91f16700Schasinglulu    0xFFFFFFFF +----------+
1964*91f16700Schasinglulu               |  SCP TZC |
1965*91f16700Schasinglulu    0xFFE00000 |----------|
1966*91f16700Schasinglulu               |  EL3 TZC |
1967*91f16700Schasinglulu    0xFFC00000 |----------|  (secure)
1968*91f16700Schasinglulu               |  AP TZC  |
1969*91f16700Schasinglulu    0xFF000000 +----------+
1970*91f16700Schasinglulu               |          |
1971*91f16700Schasinglulu               :          :  (non-secure)
1972*91f16700Schasinglulu               |          |
1973*91f16700Schasinglulu    0x80000000 +----------+
1974*91f16700Schasinglulu
1975*91f16700Schasinglulu
1976*91f16700Schasinglulu                  Flash0
1977*91f16700Schasinglulu    0x0C000000 +----------+
1978*91f16700Schasinglulu               :          :
1979*91f16700Schasinglulu    0x0BED0000 |----------|
1980*91f16700Schasinglulu               | BL1 (ro) |
1981*91f16700Schasinglulu    0x0BEC0000 |----------|
1982*91f16700Schasinglulu               :          :
1983*91f16700Schasinglulu    0x08000000 +----------+                  BL31 is loaded
1984*91f16700Schasinglulu                                             after SCP_BL2 has
1985*91f16700Schasinglulu               Trusted SRAM                  been sent to SCP
1986*91f16700Schasinglulu    0x04040000 +----------+  loaded by BL2  +----------------+
1987*91f16700Schasinglulu               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
1988*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
1989*91f16700Schasinglulu               |   BL2    |  <<<<<<<<<<<<<  |                |
1990*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |----------------|
1991*91f16700Schasinglulu               | SCP_BL2  |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
1992*91f16700Schasinglulu               |          |  <<<<<<<<<<<<<  |----------------|
1993*91f16700Schasinglulu               |          |  <<<<<<<<<<<<<  |     BL32       |
1994*91f16700Schasinglulu               |          |                 +----------------+
1995*91f16700Schasinglulu               |          |
1996*91f16700Schasinglulu    0x04001000 +----------+
1997*91f16700Schasinglulu               |   MHU    |
1998*91f16700Schasinglulu    0x04000000 +----------+
1999*91f16700Schasinglulu
2000*91f16700Schasinglulu**Juno with BL32 in TZC-secured DRAM :**
2001*91f16700Schasinglulu
2002*91f16700Schasinglulu::
2003*91f16700Schasinglulu
2004*91f16700Schasinglulu                   DRAM
2005*91f16700Schasinglulu    0xFFFFFFFF +----------+
2006*91f16700Schasinglulu               |  SCP TZC |
2007*91f16700Schasinglulu    0xFFE00000 |----------|
2008*91f16700Schasinglulu               |  EL3 TZC |
2009*91f16700Schasinglulu    0xFFC00000 |----------|  (secure)
2010*91f16700Schasinglulu               |  AP TZC  |
2011*91f16700Schasinglulu               |  (BL32)  |
2012*91f16700Schasinglulu    0xFF000000 +----------+
2013*91f16700Schasinglulu               |          |
2014*91f16700Schasinglulu               :          :  (non-secure)
2015*91f16700Schasinglulu               |          |
2016*91f16700Schasinglulu    0x80000000 +----------+
2017*91f16700Schasinglulu
2018*91f16700Schasinglulu                  Flash0
2019*91f16700Schasinglulu    0x0C000000 +----------+
2020*91f16700Schasinglulu               :          :
2021*91f16700Schasinglulu    0x0BED0000 |----------|
2022*91f16700Schasinglulu               | BL1 (ro) |
2023*91f16700Schasinglulu    0x0BEC0000 |----------|
2024*91f16700Schasinglulu               :          :
2025*91f16700Schasinglulu    0x08000000 +----------+                  BL31 is loaded
2026*91f16700Schasinglulu                                             after SCP_BL2 has
2027*91f16700Schasinglulu               Trusted SRAM                  been sent to SCP
2028*91f16700Schasinglulu    0x04040000 +----------+  loaded by BL2  +----------------+
2029*91f16700Schasinglulu               | BL1 (rw) |  <<<<<<<<<<<<<  |                |
2030*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |  BL31 NOBITS   |
2031*91f16700Schasinglulu               |   BL2    |  <<<<<<<<<<<<<  |                |
2032*91f16700Schasinglulu               |----------|  <<<<<<<<<<<<<  |----------------|
2033*91f16700Schasinglulu               | SCP_BL2  |  <<<<<<<<<<<<<  | BL31 PROGBITS  |
2034*91f16700Schasinglulu               |          |                 +----------------+
2035*91f16700Schasinglulu    0x04001000 +----------+
2036*91f16700Schasinglulu               |   MHU    |
2037*91f16700Schasinglulu    0x04000000 +----------+
2038*91f16700Schasinglulu
2039*91f16700Schasinglulu.. _firmware_design_fip:
2040*91f16700Schasinglulu
2041*91f16700SchasingluluFirmware Image Package (FIP)
2042*91f16700Schasinglulu----------------------------
2043*91f16700Schasinglulu
2044*91f16700SchasingluluUsing a Firmware Image Package (FIP) allows for packing bootloader images (and
2045*91f16700Schasinglulupotentially other payloads) into a single archive that can be loaded by TF-A
2046*91f16700Schasinglulufrom non-volatile platform storage. A driver to load images from a FIP has
2047*91f16700Schasinglulubeen added to the storage layer and allows a package to be read from supported
2048*91f16700Schasingluluplatform storage. A tool to create Firmware Image Packages is also provided
2049*91f16700Schasingluluand described below.
2050*91f16700Schasinglulu
2051*91f16700SchasingluluFirmware Image Package layout
2052*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2053*91f16700Schasinglulu
2054*91f16700SchasingluluThe FIP layout consists of a table of contents (ToC) followed by payload data.
2055*91f16700SchasingluluThe ToC itself has a header followed by one or more table entries. The ToC is
2056*91f16700Schasingluluterminated by an end marker entry, and since the size of the ToC is 0 bytes,
2057*91f16700Schasingluluthe offset equals the total size of the FIP file. All ToC entries describe some
2058*91f16700Schasinglulupayload data that has been appended to the end of the binary package. With the
2059*91f16700Schasingluluinformation provided in the ToC entry the corresponding payload data can be
2060*91f16700Schasingluluretrieved.
2061*91f16700Schasinglulu
2062*91f16700Schasinglulu::
2063*91f16700Schasinglulu
2064*91f16700Schasinglulu    ------------------
2065*91f16700Schasinglulu    | ToC Header     |
2066*91f16700Schasinglulu    |----------------|
2067*91f16700Schasinglulu    | ToC Entry 0    |
2068*91f16700Schasinglulu    |----------------|
2069*91f16700Schasinglulu    | ToC Entry 1    |
2070*91f16700Schasinglulu    |----------------|
2071*91f16700Schasinglulu    | ToC End Marker |
2072*91f16700Schasinglulu    |----------------|
2073*91f16700Schasinglulu    |                |
2074*91f16700Schasinglulu    |     Data 0     |
2075*91f16700Schasinglulu    |                |
2076*91f16700Schasinglulu    |----------------|
2077*91f16700Schasinglulu    |                |
2078*91f16700Schasinglulu    |     Data 1     |
2079*91f16700Schasinglulu    |                |
2080*91f16700Schasinglulu    ------------------
2081*91f16700Schasinglulu
2082*91f16700SchasingluluThe ToC header and entry formats are described in the header file
2083*91f16700Schasinglulu``include/tools_share/firmware_image_package.h``. This file is used by both the
2084*91f16700Schasinglulutool and TF-A.
2085*91f16700Schasinglulu
2086*91f16700SchasingluluThe ToC header has the following fields:
2087*91f16700Schasinglulu
2088*91f16700Schasinglulu::
2089*91f16700Schasinglulu
2090*91f16700Schasinglulu    `name`: The name of the ToC. This is currently used to validate the header.
2091*91f16700Schasinglulu    `serial_number`: A non-zero number provided by the creation tool
2092*91f16700Schasinglulu    `flags`: Flags associated with this data.
2093*91f16700Schasinglulu        Bits 0-31: Reserved
2094*91f16700Schasinglulu        Bits 32-47: Platform defined
2095*91f16700Schasinglulu        Bits 48-63: Reserved
2096*91f16700Schasinglulu
2097*91f16700SchasingluluA ToC entry has the following fields:
2098*91f16700Schasinglulu
2099*91f16700Schasinglulu::
2100*91f16700Schasinglulu
2101*91f16700Schasinglulu    `uuid`: All files are referred to by a pre-defined Universally Unique
2102*91f16700Schasinglulu        IDentifier [UUID] . The UUIDs are defined in
2103*91f16700Schasinglulu        `include/tools_share/firmware_image_package.h`. The platform translates
2104*91f16700Schasinglulu        the requested image name into the corresponding UUID when accessing the
2105*91f16700Schasinglulu        package.
2106*91f16700Schasinglulu    `offset_address`: The offset address at which the corresponding payload data
2107*91f16700Schasinglulu        can be found. The offset is calculated from the ToC base address.
2108*91f16700Schasinglulu    `size`: The size of the corresponding payload data in bytes.
2109*91f16700Schasinglulu    `flags`: Flags associated with this entry. None are yet defined.
2110*91f16700Schasinglulu
2111*91f16700SchasingluluFirmware Image Package creation tool
2112*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2113*91f16700Schasinglulu
2114*91f16700SchasingluluThe FIP creation tool can be used to pack specified images into a binary
2115*91f16700Schasinglulupackage that can be loaded by TF-A from platform storage. The tool currently
2116*91f16700Schasingluluonly supports packing bootloader images. Additional image definitions can be
2117*91f16700Schasingluluadded to the tool as required.
2118*91f16700Schasinglulu
2119*91f16700SchasingluluThe tool can be found in ``tools/fiptool``.
2120*91f16700Schasinglulu
2121*91f16700SchasingluluLoading from a Firmware Image Package (FIP)
2122*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2123*91f16700Schasinglulu
2124*91f16700SchasingluluThe Firmware Image Package (FIP) driver can load images from a binary package on
2125*91f16700Schasinglulunon-volatile platform storage. For the Arm development platforms, this is
2126*91f16700Schasinglulucurrently NOR FLASH.
2127*91f16700Schasinglulu
2128*91f16700SchasingluluBootloader images are loaded according to the platform policy as specified by
2129*91f16700Schasingluluthe function ``plat_get_image_source()``. For the Arm development platforms, this
2130*91f16700Schasinglulumeans the platform will attempt to load images from a Firmware Image Package
2131*91f16700Schasinglululocated at the start of NOR FLASH0.
2132*91f16700Schasinglulu
2133*91f16700SchasingluluThe Arm development platforms' policy is to only allow loading of a known set of
2134*91f16700Schasingluluimages. The platform policy can be modified to allow additional images.
2135*91f16700Schasinglulu
2136*91f16700SchasingluluUse of coherent memory in TF-A
2137*91f16700Schasinglulu------------------------------
2138*91f16700Schasinglulu
2139*91f16700SchasingluluThere might be loss of coherency when physical memory with mismatched
2140*91f16700Schasinglulushareability, cacheability and memory attributes is accessed by multiple CPUs
2141*91f16700Schasinglulu(refer to section B2.9 of `Arm ARM`_ for more details). This possibility occurs
2142*91f16700Schasingluluin TF-A during power up/down sequences when coherency, MMU and caches are
2143*91f16700Schasingluluturned on/off incrementally.
2144*91f16700Schasinglulu
2145*91f16700SchasingluluTF-A defines coherent memory as a region of memory with Device nGnRE attributes
2146*91f16700Schasingluluin the translation tables. The translation granule size in TF-A is 4KB. This
2147*91f16700Schasingluluis the smallest possible size of the coherent memory region.
2148*91f16700Schasinglulu
2149*91f16700SchasingluluBy default, all data structures which are susceptible to accesses with
2150*91f16700Schasinglulumismatched attributes from various CPUs are allocated in a coherent memory
2151*91f16700Schasingluluregion (refer to section 2.1 of :ref:`Porting Guide`). The coherent memory
2152*91f16700Schasingluluregion accesses are Outer Shareable, non-cacheable and they can be accessed with
2153*91f16700Schasingluluthe Device nGnRE attributes when the MMU is turned on. Hence, at the expense of
2154*91f16700Schasingluluat least an extra page of memory, TF-A is able to work around coherency issues
2155*91f16700Schasingluludue to mismatched memory attributes.
2156*91f16700Schasinglulu
2157*91f16700SchasingluluThe alternative to the above approach is to allocate the susceptible data
2158*91f16700Schasinglulustructures in Normal WriteBack WriteAllocate Inner shareable memory. This
2159*91f16700Schasingluluapproach requires the data structures to be designed so that it is possible to
2160*91f16700Schasingluluwork around the issue of mismatched memory attributes by performing software
2161*91f16700Schasinglulucache maintenance on them.
2162*91f16700Schasinglulu
2163*91f16700SchasingluluDisabling the use of coherent memory in TF-A
2164*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2165*91f16700Schasinglulu
2166*91f16700SchasingluluIt might be desirable to avoid the cost of allocating coherent memory on
2167*91f16700Schasingluluplatforms which are memory constrained. TF-A enables inclusion of coherent
2168*91f16700Schasinglulumemory in firmware images through the build flag ``USE_COHERENT_MEM``.
2169*91f16700SchasingluluThis flag is enabled by default. It can be disabled to choose the second
2170*91f16700Schasingluluapproach described above.
2171*91f16700Schasinglulu
2172*91f16700SchasingluluThe below sections analyze the data structures allocated in the coherent memory
2173*91f16700Schasingluluregion and the changes required to allocate them in normal memory.
2174*91f16700Schasinglulu
2175*91f16700SchasingluluCoherent memory usage in PSCI implementation
2176*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2177*91f16700Schasinglulu
2178*91f16700SchasingluluThe ``psci_non_cpu_pd_nodes`` data structure stores the platform's power domain
2179*91f16700Schasinglulutree information for state management of power domains. By default, this data
2180*91f16700Schasinglulustructure is allocated in the coherent memory region in TF-A because it can be
2181*91f16700Schasingluluaccessed by multiple CPUs, either with caches enabled or disabled.
2182*91f16700Schasinglulu
2183*91f16700Schasinglulu.. code:: c
2184*91f16700Schasinglulu
2185*91f16700Schasinglulu    typedef struct non_cpu_pwr_domain_node {
2186*91f16700Schasinglulu        /*
2187*91f16700Schasinglulu         * Index of the first CPU power domain node level 0 which has this node
2188*91f16700Schasinglulu         * as its parent.
2189*91f16700Schasinglulu         */
2190*91f16700Schasinglulu        unsigned int cpu_start_idx;
2191*91f16700Schasinglulu
2192*91f16700Schasinglulu        /*
2193*91f16700Schasinglulu         * Number of CPU power domains which are siblings of the domain indexed
2194*91f16700Schasinglulu         * by 'cpu_start_idx' i.e. all the domains in the range 'cpu_start_idx
2195*91f16700Schasinglulu         * -> cpu_start_idx + ncpus' have this node as their parent.
2196*91f16700Schasinglulu         */
2197*91f16700Schasinglulu        unsigned int ncpus;
2198*91f16700Schasinglulu
2199*91f16700Schasinglulu        /*
2200*91f16700Schasinglulu         * Index of the parent power domain node.
2201*91f16700Schasinglulu         */
2202*91f16700Schasinglulu        unsigned int parent_node;
2203*91f16700Schasinglulu
2204*91f16700Schasinglulu        plat_local_state_t local_state;
2205*91f16700Schasinglulu
2206*91f16700Schasinglulu        unsigned char level;
2207*91f16700Schasinglulu
2208*91f16700Schasinglulu        /* For indexing the psci_lock array*/
2209*91f16700Schasinglulu        unsigned char lock_index;
2210*91f16700Schasinglulu    } non_cpu_pd_node_t;
2211*91f16700Schasinglulu
2212*91f16700SchasingluluIn order to move this data structure to normal memory, the use of each of its
2213*91f16700Schasinglulufields must be analyzed. Fields like ``cpu_start_idx``, ``ncpus``, ``parent_node``
2214*91f16700Schasinglulu``level`` and ``lock_index`` are only written once during cold boot. Hence removing
2215*91f16700Schasingluluthem from coherent memory involves only doing a clean and invalidate of the
2216*91f16700Schasinglulucache lines after these fields are written.
2217*91f16700Schasinglulu
2218*91f16700SchasingluluThe field ``local_state`` can be concurrently accessed by multiple CPUs in
2219*91f16700Schasingluludifferent cache states. A Lamport's Bakery lock ``psci_locks`` is used to ensure
2220*91f16700Schasinglulumutual exclusion to this field and a clean and invalidate is needed after it
2221*91f16700Schasingluluis written.
2222*91f16700Schasinglulu
2223*91f16700SchasingluluBakery lock data
2224*91f16700Schasinglulu~~~~~~~~~~~~~~~~
2225*91f16700Schasinglulu
2226*91f16700SchasingluluThe bakery lock data structure ``bakery_lock_t`` is allocated in coherent memory
2227*91f16700Schasingluluand is accessed by multiple CPUs with mismatched attributes. ``bakery_lock_t`` is
2228*91f16700Schasingluludefined as follows:
2229*91f16700Schasinglulu
2230*91f16700Schasinglulu.. code:: c
2231*91f16700Schasinglulu
2232*91f16700Schasinglulu    typedef struct bakery_lock {
2233*91f16700Schasinglulu        /*
2234*91f16700Schasinglulu         * The lock_data is a bit-field of 2 members:
2235*91f16700Schasinglulu         * Bit[0]       : choosing. This field is set when the CPU is
2236*91f16700Schasinglulu         *                choosing its bakery number.
2237*91f16700Schasinglulu         * Bits[1 - 15] : number. This is the bakery number allocated.
2238*91f16700Schasinglulu         */
2239*91f16700Schasinglulu        volatile uint16_t lock_data[BAKERY_LOCK_MAX_CPUS];
2240*91f16700Schasinglulu    } bakery_lock_t;
2241*91f16700Schasinglulu
2242*91f16700SchasingluluIt is a characteristic of Lamport's Bakery algorithm that the volatile per-CPU
2243*91f16700Schasinglulufields can be read by all CPUs but only written to by the owning CPU.
2244*91f16700Schasinglulu
2245*91f16700SchasingluluDepending upon the data cache line size, the per-CPU fields of the
2246*91f16700Schasinglulu``bakery_lock_t`` structure for multiple CPUs may exist on a single cache line.
2247*91f16700SchasingluluThese per-CPU fields can be read and written during lock contention by multiple
2248*91f16700SchasingluluCPUs with mismatched memory attributes. Since these fields are a part of the
2249*91f16700Schasinglululock implementation, they do not have access to any other locking primitive to
2250*91f16700Schasinglulusafeguard against the resulting coherency issues. As a result, simple software
2251*91f16700Schasinglulucache maintenance is not enough to allocate them in coherent memory. Consider
2252*91f16700Schasingluluthe following example.
2253*91f16700Schasinglulu
2254*91f16700SchasingluluCPU0 updates its per-CPU field with data cache enabled. This write updates a
2255*91f16700Schasinglululocal cache line which contains a copy of the fields for other CPUs as well. Now
2256*91f16700SchasingluluCPU1 updates its per-CPU field of the ``bakery_lock_t`` structure with data cache
2257*91f16700Schasingluludisabled. CPU1 then issues a DCIVAC operation to invalidate any stale copies of
2258*91f16700Schasingluluits field in any other cache line in the system. This operation will invalidate
2259*91f16700Schasingluluthe update made by CPU0 as well.
2260*91f16700Schasinglulu
2261*91f16700SchasingluluTo use bakery locks when ``USE_COHERENT_MEM`` is disabled, the lock data structure
2262*91f16700Schasingluluhas been redesigned. The changes utilise the characteristic of Lamport's Bakery
2263*91f16700Schasinglulualgorithm mentioned earlier. The bakery_lock structure only allocates the memory
2264*91f16700Schasinglulufor a single CPU. The macro ``DEFINE_BAKERY_LOCK`` allocates all the bakery locks
2265*91f16700Schasingluluneeded for a CPU into a section ``.bakery_lock``. The linker allocates the memory
2266*91f16700Schasinglulufor other cores by using the total size allocated for the bakery_lock section
2267*91f16700Schasingluluand multiplying it with (PLATFORM_CORE_COUNT - 1). This enables software to
2268*91f16700Schasingluluperform software cache maintenance on the lock data structure without running
2269*91f16700Schasingluluinto coherency issues associated with mismatched attributes.
2270*91f16700Schasinglulu
2271*91f16700SchasingluluThe bakery lock data structure ``bakery_info_t`` is defined for use when
2272*91f16700Schasinglulu``USE_COHERENT_MEM`` is disabled as follows:
2273*91f16700Schasinglulu
2274*91f16700Schasinglulu.. code:: c
2275*91f16700Schasinglulu
2276*91f16700Schasinglulu    typedef struct bakery_info {
2277*91f16700Schasinglulu        /*
2278*91f16700Schasinglulu         * The lock_data is a bit-field of 2 members:
2279*91f16700Schasinglulu         * Bit[0]       : choosing. This field is set when the CPU is
2280*91f16700Schasinglulu         *                choosing its bakery number.
2281*91f16700Schasinglulu         * Bits[1 - 15] : number. This is the bakery number allocated.
2282*91f16700Schasinglulu         */
2283*91f16700Schasinglulu         volatile uint16_t lock_data;
2284*91f16700Schasinglulu    } bakery_info_t;
2285*91f16700Schasinglulu
2286*91f16700SchasingluluThe ``bakery_info_t`` represents a single per-CPU field of one lock and
2287*91f16700Schasingluluthe combination of corresponding ``bakery_info_t`` structures for all CPUs in the
2288*91f16700Schasinglulusystem represents the complete bakery lock. The view in memory for a system
2289*91f16700Schasingluluwith n bakery locks are:
2290*91f16700Schasinglulu
2291*91f16700Schasinglulu::
2292*91f16700Schasinglulu
2293*91f16700Schasinglulu    .bakery_lock section start
2294*91f16700Schasinglulu    |----------------|
2295*91f16700Schasinglulu    | `bakery_info_t`| <-- Lock_0 per-CPU field
2296*91f16700Schasinglulu    |    Lock_0      |     for CPU0
2297*91f16700Schasinglulu    |----------------|
2298*91f16700Schasinglulu    | `bakery_info_t`| <-- Lock_1 per-CPU field
2299*91f16700Schasinglulu    |    Lock_1      |     for CPU0
2300*91f16700Schasinglulu    |----------------|
2301*91f16700Schasinglulu    | ....           |
2302*91f16700Schasinglulu    |----------------|
2303*91f16700Schasinglulu    | `bakery_info_t`| <-- Lock_N per-CPU field
2304*91f16700Schasinglulu    |    Lock_N      |     for CPU0
2305*91f16700Schasinglulu    ------------------
2306*91f16700Schasinglulu    |    XXXXX       |
2307*91f16700Schasinglulu    | Padding to     |
2308*91f16700Schasinglulu    | next Cache WB  | <--- Calculate PERCPU_BAKERY_LOCK_SIZE, allocate
2309*91f16700Schasinglulu    |  Granule       |       continuous memory for remaining CPUs.
2310*91f16700Schasinglulu    ------------------
2311*91f16700Schasinglulu    | `bakery_info_t`| <-- Lock_0 per-CPU field
2312*91f16700Schasinglulu    |    Lock_0      |     for CPU1
2313*91f16700Schasinglulu    |----------------|
2314*91f16700Schasinglulu    | `bakery_info_t`| <-- Lock_1 per-CPU field
2315*91f16700Schasinglulu    |    Lock_1      |     for CPU1
2316*91f16700Schasinglulu    |----------------|
2317*91f16700Schasinglulu    | ....           |
2318*91f16700Schasinglulu    |----------------|
2319*91f16700Schasinglulu    | `bakery_info_t`| <-- Lock_N per-CPU field
2320*91f16700Schasinglulu    |    Lock_N      |     for CPU1
2321*91f16700Schasinglulu    ------------------
2322*91f16700Schasinglulu    |    XXXXX       |
2323*91f16700Schasinglulu    | Padding to     |
2324*91f16700Schasinglulu    | next Cache WB  |
2325*91f16700Schasinglulu    |  Granule       |
2326*91f16700Schasinglulu    ------------------
2327*91f16700Schasinglulu
2328*91f16700SchasingluluConsider a system of 2 CPUs with 'N' bakery locks as shown above. For an
2329*91f16700Schasingluluoperation on Lock_N, the corresponding ``bakery_info_t`` in both CPU0 and CPU1
2330*91f16700Schasinglulu``.bakery_lock`` section need to be fetched and appropriate cache operations need
2331*91f16700Schasingluluto be performed for each access.
2332*91f16700Schasinglulu
2333*91f16700SchasingluluOn Arm Platforms, bakery locks are used in psci (``psci_locks``) and power controller
2334*91f16700Schasingluludriver (``arm_lock``).
2335*91f16700Schasinglulu
2336*91f16700SchasingluluNon Functional Impact of removing coherent memory
2337*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2338*91f16700Schasinglulu
2339*91f16700SchasingluluRemoval of the coherent memory region leads to the additional software overhead
2340*91f16700Schasingluluof performing cache maintenance for the affected data structures. However, since
2341*91f16700Schasingluluthe memory where the data structures are allocated is cacheable, the overhead is
2342*91f16700Schasinglulumostly mitigated by an increase in performance.
2343*91f16700Schasinglulu
2344*91f16700SchasingluluThere is however a performance impact for bakery locks, due to:
2345*91f16700Schasinglulu
2346*91f16700Schasinglulu-  Additional cache maintenance operations, and
2347*91f16700Schasinglulu-  Multiple cache line reads for each lock operation, since the bakery locks
2348*91f16700Schasinglulu   for each CPU are distributed across different cache lines.
2349*91f16700Schasinglulu
2350*91f16700SchasingluluThe implementation has been optimized to minimize this additional overhead.
2351*91f16700SchasingluluMeasurements indicate that when bakery locks are allocated in Normal memory, the
2352*91f16700Schasingluluminimum latency of acquiring a lock is on an average 3-4 micro seconds whereas
2353*91f16700Schasingluluin Device memory the same is 2 micro seconds. The measurements were done on the
2354*91f16700SchasingluluJuno Arm development platform.
2355*91f16700Schasinglulu
2356*91f16700SchasingluluAs mentioned earlier, almost a page of memory can be saved by disabling
2357*91f16700Schasinglulu``USE_COHERENT_MEM``. Each platform needs to consider these trade-offs to decide
2358*91f16700Schasingluluwhether coherent memory should be used. If a platform disables
2359*91f16700Schasinglulu``USE_COHERENT_MEM`` and needs to use bakery locks in the porting layer, it can
2360*91f16700Schasingluluoptionally define macro ``PLAT_PERCPU_BAKERY_LOCK_SIZE`` (see the
2361*91f16700Schasinglulu:ref:`Porting Guide`). Refer to the reference platform code for examples.
2362*91f16700Schasinglulu
2363*91f16700SchasingluluIsolating code and read-only data on separate memory pages
2364*91f16700Schasinglulu----------------------------------------------------------
2365*91f16700Schasinglulu
2366*91f16700SchasingluluIn the Armv8-A VMSA, translation table entries include fields that define the
2367*91f16700Schasingluluproperties of the target memory region, such as its access permissions. The
2368*91f16700Schasinglulusmallest unit of memory that can be addressed by a translation table entry is
2369*91f16700Schasinglulua memory page. Therefore, if software needs to set different permissions on two
2370*91f16700Schasinglulumemory regions then it needs to map them using different memory pages.
2371*91f16700Schasinglulu
2372*91f16700SchasingluluThe default memory layout for each BL image is as follows:
2373*91f16700Schasinglulu
2374*91f16700Schasinglulu::
2375*91f16700Schasinglulu
2376*91f16700Schasinglulu       |        ...        |
2377*91f16700Schasinglulu       +-------------------+
2378*91f16700Schasinglulu       |  Read-write data  |
2379*91f16700Schasinglulu       +-------------------+ Page boundary
2380*91f16700Schasinglulu       |     <Padding>     |
2381*91f16700Schasinglulu       +-------------------+
2382*91f16700Schasinglulu       | Exception vectors |
2383*91f16700Schasinglulu       +-------------------+ 2 KB boundary
2384*91f16700Schasinglulu       |     <Padding>     |
2385*91f16700Schasinglulu       +-------------------+
2386*91f16700Schasinglulu       |  Read-only data   |
2387*91f16700Schasinglulu       +-------------------+
2388*91f16700Schasinglulu       |       Code        |
2389*91f16700Schasinglulu       +-------------------+ BLx_BASE
2390*91f16700Schasinglulu
2391*91f16700Schasinglulu.. note::
2392*91f16700Schasinglulu   The 2KB alignment for the exception vectors is an architectural
2393*91f16700Schasinglulu   requirement.
2394*91f16700Schasinglulu
2395*91f16700SchasingluluThe read-write data start on a new memory page so that they can be mapped with
2396*91f16700Schasingluluread-write permissions, whereas the code and read-only data below are configured
2397*91f16700Schasingluluas read-only.
2398*91f16700Schasinglulu
2399*91f16700SchasingluluHowever, the read-only data are not aligned on a page boundary. They are
2400*91f16700Schasinglulucontiguous to the code. Therefore, the end of the code section and the beginning
2401*91f16700Schasingluluof the read-only data one might share a memory page. This forces both to be
2402*91f16700Schasinglulumapped with the same memory attributes. As the code needs to be executable, this
2403*91f16700Schasinglulumeans that the read-only data stored on the same memory page as the code are
2404*91f16700Schasingluluexecutable as well. This could potentially be exploited as part of a security
2405*91f16700Schasingluluattack.
2406*91f16700Schasinglulu
2407*91f16700SchasingluluTF provides the build flag ``SEPARATE_CODE_AND_RODATA`` to isolate the code and
2408*91f16700Schasingluluread-only data on separate memory pages. This in turn allows independent control
2409*91f16700Schasingluluof the access permissions for the code and read-only data. In this case,
2410*91f16700Schasingluluplatform code gets a finer-grained view of the image layout and can
2411*91f16700Schasingluluappropriately map the code region as executable and the read-only data as
2412*91f16700Schasingluluexecute-never.
2413*91f16700Schasinglulu
2414*91f16700SchasingluluThis has an impact on memory footprint, as padding bytes need to be introduced
2415*91f16700Schasinglulubetween the code and read-only data to ensure the segregation of the two. To
2416*91f16700Schasinglululimit the memory cost, this flag also changes the memory layout such that the
2417*91f16700Schasinglulucode and exception vectors are now contiguous, like so:
2418*91f16700Schasinglulu
2419*91f16700Schasinglulu::
2420*91f16700Schasinglulu
2421*91f16700Schasinglulu       |        ...        |
2422*91f16700Schasinglulu       +-------------------+
2423*91f16700Schasinglulu       |  Read-write data  |
2424*91f16700Schasinglulu       +-------------------+ Page boundary
2425*91f16700Schasinglulu       |     <Padding>     |
2426*91f16700Schasinglulu       +-------------------+
2427*91f16700Schasinglulu       |  Read-only data   |
2428*91f16700Schasinglulu       +-------------------+ Page boundary
2429*91f16700Schasinglulu       |     <Padding>     |
2430*91f16700Schasinglulu       +-------------------+
2431*91f16700Schasinglulu       | Exception vectors |
2432*91f16700Schasinglulu       +-------------------+ 2 KB boundary
2433*91f16700Schasinglulu       |     <Padding>     |
2434*91f16700Schasinglulu       +-------------------+
2435*91f16700Schasinglulu       |       Code        |
2436*91f16700Schasinglulu       +-------------------+ BLx_BASE
2437*91f16700Schasinglulu
2438*91f16700SchasingluluWith this more condensed memory layout, the separation of read-only data will
2439*91f16700Schasingluluadd zero or one page to the memory footprint of each BL image. Each platform
2440*91f16700Schasinglulushould consider the trade-off between memory footprint and security.
2441*91f16700Schasinglulu
2442*91f16700SchasingluluThis build flag is disabled by default, minimising memory footprint. On Arm
2443*91f16700Schasingluluplatforms, it is enabled.
2444*91f16700Schasinglulu
2445*91f16700SchasingluluPublish and Subscribe Framework
2446*91f16700Schasinglulu-------------------------------
2447*91f16700Schasinglulu
2448*91f16700SchasingluluThe Publish and Subscribe Framework allows EL3 components to define and publish
2449*91f16700Schasingluluevents, to which other EL3 components can subscribe.
2450*91f16700Schasinglulu
2451*91f16700SchasingluluThe following macros are provided by the framework:
2452*91f16700Schasinglulu
2453*91f16700Schasinglulu-  ``REGISTER_PUBSUB_EVENT(event)``: Defines an event, and takes one argument,
2454*91f16700Schasinglulu   the event name, which must be a valid C identifier. All calls to
2455*91f16700Schasinglulu   ``REGISTER_PUBSUB_EVENT`` macro must be placed in the file
2456*91f16700Schasinglulu   ``pubsub_events.h``.
2457*91f16700Schasinglulu
2458*91f16700Schasinglulu-  ``PUBLISH_EVENT_ARG(event, arg)``: Publishes a defined event, by iterating
2459*91f16700Schasinglulu   subscribed handlers and calling them in turn. The handlers will be passed the
2460*91f16700Schasinglulu   parameter ``arg``. The expected use-case is to broadcast an event.
2461*91f16700Schasinglulu
2462*91f16700Schasinglulu-  ``PUBLISH_EVENT(event)``: Like ``PUBLISH_EVENT_ARG``, except that the value
2463*91f16700Schasinglulu   ``NULL`` is passed to subscribed handlers.
2464*91f16700Schasinglulu
2465*91f16700Schasinglulu-  ``SUBSCRIBE_TO_EVENT(event, handler)``: Registers the ``handler`` to
2466*91f16700Schasinglulu   subscribe to ``event``. The handler will be executed whenever the ``event``
2467*91f16700Schasinglulu   is published.
2468*91f16700Schasinglulu
2469*91f16700Schasinglulu-  ``for_each_subscriber(event, subscriber)``: Iterates through all handlers
2470*91f16700Schasinglulu   subscribed for ``event``. ``subscriber`` must be a local variable of type
2471*91f16700Schasinglulu   ``pubsub_cb_t *``, and will point to each subscribed handler in turn during
2472*91f16700Schasinglulu   iteration. This macro can be used for those patterns that none of the
2473*91f16700Schasinglulu   ``PUBLISH_EVENT_*()`` macros cover.
2474*91f16700Schasinglulu
2475*91f16700SchasingluluPublishing an event that wasn't defined using ``REGISTER_PUBSUB_EVENT`` will
2476*91f16700Schasingluluresult in build error. Subscribing to an undefined event however won't.
2477*91f16700Schasinglulu
2478*91f16700SchasingluluSubscribed handlers must be of type ``pubsub_cb_t``, with following function
2479*91f16700Schasinglulusignature:
2480*91f16700Schasinglulu
2481*91f16700Schasinglulu.. code:: c
2482*91f16700Schasinglulu
2483*91f16700Schasinglulu   typedef void* (*pubsub_cb_t)(const void *arg);
2484*91f16700Schasinglulu
2485*91f16700SchasingluluThere may be arbitrary number of handlers registered to the same event. The
2486*91f16700Schasingluluorder in which subscribed handlers are notified when that event is published is
2487*91f16700Schasinglulunot defined. Subscribed handlers may be executed in any order; handlers should
2488*91f16700Schasinglulunot assume any relative ordering amongst them.
2489*91f16700Schasinglulu
2490*91f16700SchasingluluPublishing an event on a PE will result in subscribed handlers executing on that
2491*91f16700SchasingluluPE only; it won't cause handlers to execute on a different PE.
2492*91f16700Schasinglulu
2493*91f16700SchasingluluNote that publishing an event on a PE blocks until all the subscribed handlers
2494*91f16700Schasinglulufinish executing on the PE.
2495*91f16700Schasinglulu
2496*91f16700SchasingluluTF-A generic code publishes and subscribes to some events within. Platform
2497*91f16700Schasingluluports are discouraged from subscribing to them. These events may be withdrawn,
2498*91f16700Schasinglulurenamed, or have their semantics altered in the future. Platforms may however
2499*91f16700Schasingluluregister, publish, and subscribe to platform-specific events.
2500*91f16700Schasinglulu
2501*91f16700SchasingluluPublish and Subscribe Example
2502*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2503*91f16700Schasinglulu
2504*91f16700SchasingluluA publisher that wants to publish event ``foo`` would:
2505*91f16700Schasinglulu
2506*91f16700Schasinglulu-  Define the event ``foo`` in the ``pubsub_events.h``.
2507*91f16700Schasinglulu
2508*91f16700Schasinglulu   .. code:: c
2509*91f16700Schasinglulu
2510*91f16700Schasinglulu      REGISTER_PUBSUB_EVENT(foo);
2511*91f16700Schasinglulu
2512*91f16700Schasinglulu-  Depending on the nature of event, use one of ``PUBLISH_EVENT_*()`` macros to
2513*91f16700Schasinglulu   publish the event at the appropriate path and time of execution.
2514*91f16700Schasinglulu
2515*91f16700SchasingluluA subscriber that wants to subscribe to event ``foo`` published above would
2516*91f16700Schasingluluimplement:
2517*91f16700Schasinglulu
2518*91f16700Schasinglulu.. code:: c
2519*91f16700Schasinglulu
2520*91f16700Schasinglulu    void *foo_handler(const void *arg)
2521*91f16700Schasinglulu    {
2522*91f16700Schasinglulu         void *result;
2523*91f16700Schasinglulu
2524*91f16700Schasinglulu         /* Do handling ... */
2525*91f16700Schasinglulu
2526*91f16700Schasinglulu         return result;
2527*91f16700Schasinglulu    }
2528*91f16700Schasinglulu
2529*91f16700Schasinglulu    SUBSCRIBE_TO_EVENT(foo, foo_handler);
2530*91f16700Schasinglulu
2531*91f16700Schasinglulu
2532*91f16700SchasingluluReclaiming the BL31 initialization code
2533*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2534*91f16700Schasinglulu
2535*91f16700SchasingluluA significant amount of the code used for the initialization of BL31 is never
2536*91f16700Schasingluluneeded again after boot time. In order to reduce the runtime memory
2537*91f16700Schasinglulufootprint, the memory used for this code can be reclaimed after initialization
2538*91f16700Schasingluluhas finished and be used for runtime data.
2539*91f16700Schasinglulu
2540*91f16700SchasingluluThe build option ``RECLAIM_INIT_CODE`` can be set to mark this boot time code
2541*91f16700Schasingluluwith a ``.text.init.*`` attribute which can be filtered and placed suitably
2542*91f16700Schasingluluwithin the BL image for later reclamation by the platform. The platform can
2543*91f16700Schasingluluspecify the filter and the memory region for this init section in BL31 via the
2544*91f16700Schasingluluplat.ld.S linker script. For example, on the FVP, this section is placed
2545*91f16700Schasingluluoverlapping the secondary CPU stacks so that after the cold boot is done, this
2546*91f16700Schasinglulumemory can be reclaimed for the stacks. The init memory section is initially
2547*91f16700Schasinglulumapped with ``RO``, ``EXECUTE`` attributes. After BL31 initialization has
2548*91f16700Schasinglulucompleted, the FVP changes the attributes of this section to ``RW``,
2549*91f16700Schasinglulu``EXECUTE_NEVER`` allowing it to be used for runtime data. The memory attributes
2550*91f16700Schasingluluare changed within the ``bl31_plat_runtime_setup`` platform hook. The init
2551*91f16700Schasinglulusection section can be reclaimed for any data which is accessed after cold
2552*91f16700Schasingluluboot initialization and it is upto the platform to make the decision.
2553*91f16700Schasinglulu
2554*91f16700Schasinglulu.. _firmware_design_pmf:
2555*91f16700Schasinglulu
2556*91f16700SchasingluluPerformance Measurement Framework
2557*91f16700Schasinglulu---------------------------------
2558*91f16700Schasinglulu
2559*91f16700SchasingluluThe Performance Measurement Framework (PMF) facilitates collection of
2560*91f16700Schasinglulutimestamps by registered services and provides interfaces to retrieve them
2561*91f16700Schasinglulufrom within TF-A. A platform can choose to expose appropriate SMCs to
2562*91f16700Schasingluluretrieve these collected timestamps.
2563*91f16700Schasinglulu
2564*91f16700SchasingluluBy default, the global physical counter is used for the timestamp
2565*91f16700Schasingluluvalue and is read via ``CNTPCT_EL0``. The framework allows to retrieve
2566*91f16700Schasinglulutimestamps captured by other CPUs.
2567*91f16700Schasinglulu
2568*91f16700SchasingluluTimestamp identifier format
2569*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~
2570*91f16700Schasinglulu
2571*91f16700SchasingluluA PMF timestamp is uniquely identified across the system via the
2572*91f16700Schasinglulutimestamp ID or ``tid``. The ``tid`` is composed as follows:
2573*91f16700Schasinglulu
2574*91f16700Schasinglulu::
2575*91f16700Schasinglulu
2576*91f16700Schasinglulu    Bits 0-7: The local timestamp identifier.
2577*91f16700Schasinglulu    Bits 8-9: Reserved.
2578*91f16700Schasinglulu    Bits 10-15: The service identifier.
2579*91f16700Schasinglulu    Bits 16-31: Reserved.
2580*91f16700Schasinglulu
2581*91f16700Schasinglulu#. The service identifier. Each PMF service is identified by a
2582*91f16700Schasinglulu   service name and a service identifier. Both the service name and
2583*91f16700Schasinglulu   identifier are unique within the system as a whole.
2584*91f16700Schasinglulu
2585*91f16700Schasinglulu#. The local timestamp identifier. This identifier is unique within a given
2586*91f16700Schasinglulu   service.
2587*91f16700Schasinglulu
2588*91f16700SchasingluluRegistering a PMF service
2589*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~
2590*91f16700Schasinglulu
2591*91f16700SchasingluluTo register a PMF service, the ``PMF_REGISTER_SERVICE()`` macro from ``pmf.h``
2592*91f16700Schasingluluis used. The arguments required are the service name, the service ID,
2593*91f16700Schasingluluthe total number of local timestamps to be captured and a set of flags.
2594*91f16700Schasinglulu
2595*91f16700SchasingluluThe ``flags`` field can be specified as a bitwise-OR of the following values:
2596*91f16700Schasinglulu
2597*91f16700Schasinglulu::
2598*91f16700Schasinglulu
2599*91f16700Schasinglulu    PMF_STORE_ENABLE: The timestamp is stored in memory for later retrieval.
2600*91f16700Schasinglulu    PMF_DUMP_ENABLE: The timestamp is dumped on the serial console.
2601*91f16700Schasinglulu
2602*91f16700SchasingluluThe ``PMF_REGISTER_SERVICE()`` reserves memory to store captured
2603*91f16700Schasinglulutimestamps in a PMF specific linker section at build time.
2604*91f16700SchasingluluAdditionally, it defines necessary functions to capture and
2605*91f16700Schasingluluretrieve a particular timestamp for the given service at runtime.
2606*91f16700Schasinglulu
2607*91f16700SchasingluluThe macro ``PMF_REGISTER_SERVICE()`` only enables capturing PMF timestamps
2608*91f16700Schasinglulufrom within TF-A. In order to retrieve timestamps from outside of TF-A, the
2609*91f16700Schasinglulu``PMF_REGISTER_SERVICE_SMC()`` macro must be used instead. This macro
2610*91f16700Schasingluluaccepts the same set of arguments as the ``PMF_REGISTER_SERVICE()``
2611*91f16700Schasinglulumacro but additionally supports retrieving timestamps using SMCs.
2612*91f16700Schasinglulu
2613*91f16700SchasingluluCapturing a timestamp
2614*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~
2615*91f16700Schasinglulu
2616*91f16700SchasingluluPMF timestamps are stored in a per-service timestamp region. On a
2617*91f16700Schasinglulusystem with multiple CPUs, each timestamp is captured and stored
2618*91f16700Schasingluluin a per-CPU cache line aligned memory region.
2619*91f16700Schasinglulu
2620*91f16700SchasingluluHaving registered the service, the ``PMF_CAPTURE_TIMESTAMP()`` macro can be
2621*91f16700Schasingluluused to capture a timestamp at the location where it is used. The macro
2622*91f16700Schasinglulutakes the service name, a local timestamp identifier and a flag as arguments.
2623*91f16700Schasinglulu
2624*91f16700SchasingluluThe ``flags`` field argument can be zero, or ``PMF_CACHE_MAINT`` which
2625*91f16700Schasingluluinstructs PMF to do cache maintenance following the capture. Cache
2626*91f16700Schasinglulumaintenance is required if any of the service's timestamps are captured
2627*91f16700Schasingluluwith data cache disabled.
2628*91f16700Schasinglulu
2629*91f16700SchasingluluTo capture a timestamp in assembly code, the caller should use
2630*91f16700Schasinglulu``pmf_calc_timestamp_addr`` macro (defined in ``pmf_asm_macros.S``) to
2631*91f16700Schasinglulucalculate the address of where the timestamp would be stored. The
2632*91f16700Schasinglulucaller should then read ``CNTPCT_EL0`` register to obtain the timestamp
2633*91f16700Schasingluluand store it at the determined address for later retrieval.
2634*91f16700Schasinglulu
2635*91f16700SchasingluluRetrieving a timestamp
2636*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~
2637*91f16700Schasinglulu
2638*91f16700SchasingluluFrom within TF-A, timestamps for individual CPUs can be retrieved using either
2639*91f16700Schasinglulu``PMF_GET_TIMESTAMP_BY_MPIDR()`` or ``PMF_GET_TIMESTAMP_BY_INDEX()`` macros.
2640*91f16700SchasingluluThese macros accept the CPU's MPIDR value, or its ordinal position
2641*91f16700Schasinglulurespectively.
2642*91f16700Schasinglulu
2643*91f16700SchasingluluFrom outside TF-A, timestamps for individual CPUs can be retrieved by calling
2644*91f16700Schasingluluinto ``pmf_smc_handler()``.
2645*91f16700Schasinglulu
2646*91f16700Schasinglulu::
2647*91f16700Schasinglulu
2648*91f16700Schasinglulu    Interface : pmf_smc_handler()
2649*91f16700Schasinglulu    Argument  : unsigned int smc_fid, u_register_t x1,
2650*91f16700Schasinglulu                u_register_t x2, u_register_t x3,
2651*91f16700Schasinglulu                u_register_t x4, void *cookie,
2652*91f16700Schasinglulu                void *handle, u_register_t flags
2653*91f16700Schasinglulu    Return    : uintptr_t
2654*91f16700Schasinglulu
2655*91f16700Schasinglulu    smc_fid: Holds the SMC identifier which is either `PMF_SMC_GET_TIMESTAMP_32`
2656*91f16700Schasinglulu        when the caller of the SMC is running in AArch32 mode
2657*91f16700Schasinglulu        or `PMF_SMC_GET_TIMESTAMP_64` when the caller is running in AArch64 mode.
2658*91f16700Schasinglulu    x1: Timestamp identifier.
2659*91f16700Schasinglulu    x2: The `mpidr` of the CPU for which the timestamp has to be retrieved.
2660*91f16700Schasinglulu        This can be the `mpidr` of a different core to the one initiating
2661*91f16700Schasinglulu        the SMC.  In that case, service specific cache maintenance may be
2662*91f16700Schasinglulu        required to ensure the updated copy of the timestamp is returned.
2663*91f16700Schasinglulu    x3: A flags value that is either 0 or `PMF_CACHE_MAINT`.  If
2664*91f16700Schasinglulu        `PMF_CACHE_MAINT` is passed, then the PMF code will perform a
2665*91f16700Schasinglulu        cache invalidate before reading the timestamp.  This ensures
2666*91f16700Schasinglulu        an updated copy is returned.
2667*91f16700Schasinglulu
2668*91f16700SchasingluluThe remaining arguments, ``x4``, ``cookie``, ``handle`` and ``flags`` are unused
2669*91f16700Schasingluluin this implementation.
2670*91f16700Schasinglulu
2671*91f16700SchasingluluPMF code structure
2672*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~
2673*91f16700Schasinglulu
2674*91f16700Schasinglulu#. ``pmf_main.c`` consists of core functions that implement service registration,
2675*91f16700Schasinglulu   initialization, storing, dumping and retrieving timestamps.
2676*91f16700Schasinglulu
2677*91f16700Schasinglulu#. ``pmf_smc.c`` contains the SMC handling for registered PMF services.
2678*91f16700Schasinglulu
2679*91f16700Schasinglulu#. ``pmf.h`` contains the public interface to Performance Measurement Framework.
2680*91f16700Schasinglulu
2681*91f16700Schasinglulu#. ``pmf_asm_macros.S`` consists of macros to facilitate capturing timestamps in
2682*91f16700Schasinglulu   assembly code.
2683*91f16700Schasinglulu
2684*91f16700Schasinglulu#. ``pmf_helpers.h`` is an internal header used by ``pmf.h``.
2685*91f16700Schasinglulu
2686*91f16700SchasingluluArmv8-A Architecture Extensions
2687*91f16700Schasinglulu-------------------------------
2688*91f16700Schasinglulu
2689*91f16700SchasingluluTF-A makes use of Armv8-A Architecture Extensions where applicable. This
2690*91f16700Schasinglulusection lists the usage of Architecture Extensions, and build flags
2691*91f16700Schasinglulucontrolling them.
2692*91f16700Schasinglulu
2693*91f16700SchasingluluBuild options
2694*91f16700Schasinglulu~~~~~~~~~~~~~
2695*91f16700Schasinglulu
2696*91f16700Schasinglulu``ARM_ARCH_MAJOR`` and ``ARM_ARCH_MINOR``
2697*91f16700Schasinglulu
2698*91f16700SchasingluluThese build options serve dual purpose
2699*91f16700Schasinglulu
2700*91f16700Schasinglulu- Determine the architecture extension support in TF-A build: All the mandatory
2701*91f16700Schasinglulu  architectural features up to ``ARM_ARCH_MAJOR.ARM_ARCH_MINOR`` are included
2702*91f16700Schasinglulu  and unconditionally enabled by TF-A build system.
2703*91f16700Schasinglulu
2704*91f16700Schasinglulu- ``ARM_ARCH_MAJOR`` and ``ARM_ARCH_MINOR`` are passed to a march.mk build utility
2705*91f16700Schasinglulu  this will try to come up with an appropriate -march value to be passed to compiler
2706*91f16700Schasinglulu  by probing the compiler and checking what's supported by the compiler and what's best
2707*91f16700Schasinglulu  that can be used. But if platform provides a ``MARCH_DIRECTIVE`` then it will used
2708*91f16700Schasinglulu  directly and compiler probing will be skipped.
2709*91f16700Schasinglulu
2710*91f16700SchasingluluThe build system requires that the platform provides a valid numeric value based on
2711*91f16700SchasingluluCPU architecture extension, otherwise it defaults to base Armv8.0-A architecture.
2712*91f16700SchasingluluSubsequent Arm Architecture versions also support extensions which were introduced
2713*91f16700Schasingluluin previous versions.
2714*91f16700Schasinglulu
2715*91f16700Schasinglulu.. seealso:: :ref:`Build Options`
2716*91f16700Schasinglulu
2717*91f16700SchasingluluFor details on the Architecture Extension and available features, please refer
2718*91f16700Schasingluluto the respective Architecture Extension Supplement.
2719*91f16700Schasinglulu
2720*91f16700SchasingluluArmv8.1-A
2721*91f16700Schasinglulu~~~~~~~~~
2722*91f16700Schasinglulu
2723*91f16700SchasingluluThis Architecture Extension is targeted when ``ARM_ARCH_MAJOR`` >= 8, or when
2724*91f16700Schasinglulu``ARM_ARCH_MAJOR`` == 8 and ``ARM_ARCH_MINOR`` >= 1.
2725*91f16700Schasinglulu
2726*91f16700Schasinglulu-  By default, a load-/store-exclusive instruction pair is used to implement
2727*91f16700Schasinglulu   spinlocks. The ``USE_SPINLOCK_CAS`` build option when set to 1 selects the
2728*91f16700Schasinglulu   spinlock implementation using the ARMv8.1-LSE Compare and Swap instruction.
2729*91f16700Schasinglulu   Notice this instruction is only available in AArch64 execution state, so
2730*91f16700Schasinglulu   the option is only available to AArch64 builds.
2731*91f16700Schasinglulu
2732*91f16700SchasingluluArmv8.2-A
2733*91f16700Schasinglulu~~~~~~~~~
2734*91f16700Schasinglulu
2735*91f16700Schasinglulu-  The presence of ARMv8.2-TTCNP is detected at runtime. When it is present, the
2736*91f16700Schasinglulu   Common not Private (TTBRn_ELx.CnP) bit is enabled to indicate that multiple
2737*91f16700Schasinglulu   Processing Elements in the same Inner Shareable domain use the same
2738*91f16700Schasinglulu   translation table entries for a given stage of translation for a particular
2739*91f16700Schasinglulu   translation regime.
2740*91f16700Schasinglulu
2741*91f16700SchasingluluArmv8.3-A
2742*91f16700Schasinglulu~~~~~~~~~
2743*91f16700Schasinglulu
2744*91f16700Schasinglulu-  Pointer authentication features of Armv8.3-A are unconditionally enabled in
2745*91f16700Schasinglulu   the Non-secure world so that lower ELs are allowed to use them without
2746*91f16700Schasinglulu   causing a trap to EL3.
2747*91f16700Schasinglulu
2748*91f16700Schasinglulu   In order to enable the Secure world to use it, ``CTX_INCLUDE_PAUTH_REGS``
2749*91f16700Schasinglulu   must be set to 1. This will add all pointer authentication system registers
2750*91f16700Schasinglulu   to the context that is saved when doing a world switch.
2751*91f16700Schasinglulu
2752*91f16700Schasinglulu   The TF-A itself has support for pointer authentication at runtime
2753*91f16700Schasinglulu   that can be enabled by setting ``BRANCH_PROTECTION`` option to non-zero and
2754*91f16700Schasinglulu   ``CTX_INCLUDE_PAUTH_REGS`` to 1. This enables pointer authentication in BL1,
2755*91f16700Schasinglulu   BL2, BL31, and the TSP if it is used.
2756*91f16700Schasinglulu
2757*91f16700Schasinglulu   Note that Pointer Authentication is enabled for Non-secure world irrespective
2758*91f16700Schasinglulu   of the value of these build flags if the CPU supports it.
2759*91f16700Schasinglulu
2760*91f16700Schasinglulu   If ``ARM_ARCH_MAJOR == 8`` and ``ARM_ARCH_MINOR >= 3`` the code footprint of
2761*91f16700Schasinglulu   enabling PAuth is lower because the compiler will use the optimized
2762*91f16700Schasinglulu   PAuth instructions rather than the backwards-compatible ones.
2763*91f16700Schasinglulu
2764*91f16700SchasingluluArmv8.5-A
2765*91f16700Schasinglulu~~~~~~~~~
2766*91f16700Schasinglulu
2767*91f16700Schasinglulu-  Branch Target Identification feature is selected by ``BRANCH_PROTECTION``
2768*91f16700Schasinglulu   option set to 1. This option defaults to 0.
2769*91f16700Schasinglulu
2770*91f16700Schasinglulu-  Memory Tagging Extension feature is unconditionally enabled for both worlds
2771*91f16700Schasinglulu   (at EL0 and S-EL0) if it is only supported at EL0. If instead it is
2772*91f16700Schasinglulu   implemented at all ELs, it is unconditionally enabled for only the normal
2773*91f16700Schasinglulu   world. To enable it for the secure world as well, the build option
2774*91f16700Schasinglulu   ``CTX_INCLUDE_MTE_REGS`` is required. If the hardware does not implement
2775*91f16700Schasinglulu   MTE support at all, it is always disabled, no matter what build options
2776*91f16700Schasinglulu   are used.
2777*91f16700Schasinglulu
2778*91f16700SchasingluluArmv7-A
2779*91f16700Schasinglulu~~~~~~~
2780*91f16700Schasinglulu
2781*91f16700SchasingluluThis Architecture Extension is targeted when ``ARM_ARCH_MAJOR`` == 7.
2782*91f16700Schasinglulu
2783*91f16700SchasingluluThere are several Armv7-A extensions available. Obviously the TrustZone
2784*91f16700Schasingluluextension is mandatory to support the TF-A bootloader and runtime services.
2785*91f16700Schasinglulu
2786*91f16700SchasingluluPlatform implementing an Armv7-A system can to define from its target
2787*91f16700SchasingluluCortex-A architecture through ``ARM_CORTEX_A<X> = yes`` in their
2788*91f16700Schasinglulu``platform.mk`` script. For example ``ARM_CORTEX_A15=yes`` for a
2789*91f16700SchasingluluCortex-A15 target.
2790*91f16700Schasinglulu
2791*91f16700SchasingluluPlatform can also set ``ARM_WITH_NEON=yes`` to enable neon support.
2792*91f16700SchasingluluNote that using neon at runtime has constraints on non secure world context.
2793*91f16700SchasingluluTF-A does not yet provide VFP context management.
2794*91f16700Schasinglulu
2795*91f16700SchasingluluDirective ``ARM_CORTEX_A<x>`` and ``ARM_WITH_NEON`` are used to set
2796*91f16700Schasingluluthe toolchain  target architecture directive.
2797*91f16700Schasinglulu
2798*91f16700SchasingluluPlatform may choose to not define straight the toolchain target architecture
2799*91f16700Schasingluludirective by defining ``MARCH_DIRECTIVE``.
2800*91f16700SchasingluluI.e:
2801*91f16700Schasinglulu
2802*91f16700Schasinglulu.. code:: make
2803*91f16700Schasinglulu
2804*91f16700Schasinglulu   MARCH_DIRECTIVE := -march=armv7-a
2805*91f16700Schasinglulu
2806*91f16700SchasingluluCode Structure
2807*91f16700Schasinglulu--------------
2808*91f16700Schasinglulu
2809*91f16700SchasingluluTF-A code is logically divided between the three boot loader stages mentioned
2810*91f16700Schasingluluin the previous sections. The code is also divided into the following
2811*91f16700Schasinglulucategories (present as directories in the source code):
2812*91f16700Schasinglulu
2813*91f16700Schasinglulu-  **Platform specific.** Choice of architecture specific code depends upon
2814*91f16700Schasinglulu   the platform.
2815*91f16700Schasinglulu-  **Common code.** This is platform and architecture agnostic code.
2816*91f16700Schasinglulu-  **Library code.** This code comprises of functionality commonly used by all
2817*91f16700Schasinglulu   other code. The PSCI implementation and other EL3 runtime frameworks reside
2818*91f16700Schasinglulu   as Library components.
2819*91f16700Schasinglulu-  **Stage specific.** Code specific to a boot stage.
2820*91f16700Schasinglulu-  **Drivers.**
2821*91f16700Schasinglulu-  **Services.** EL3 runtime services (eg: SPD). Specific SPD services
2822*91f16700Schasinglulu   reside in the ``services/spd`` directory (e.g. ``services/spd/tspd``).
2823*91f16700Schasinglulu
2824*91f16700SchasingluluEach boot loader stage uses code from one or more of the above mentioned
2825*91f16700Schasinglulucategories. Based upon the above, the code layout looks like this:
2826*91f16700Schasinglulu
2827*91f16700Schasinglulu::
2828*91f16700Schasinglulu
2829*91f16700Schasinglulu    Directory    Used by BL1?    Used by BL2?    Used by BL31?
2830*91f16700Schasinglulu    bl1          Yes             No              No
2831*91f16700Schasinglulu    bl2          No              Yes             No
2832*91f16700Schasinglulu    bl31         No              No              Yes
2833*91f16700Schasinglulu    plat         Yes             Yes             Yes
2834*91f16700Schasinglulu    drivers      Yes             No              Yes
2835*91f16700Schasinglulu    common       Yes             Yes             Yes
2836*91f16700Schasinglulu    lib          Yes             Yes             Yes
2837*91f16700Schasinglulu    services     No              No              Yes
2838*91f16700Schasinglulu
2839*91f16700SchasingluluThe build system provides a non configurable build option IMAGE_BLx for each
2840*91f16700Schasingluluboot loader stage (where x = BL stage). e.g. for BL1 , IMAGE_BL1 will be
2841*91f16700Schasingluludefined by the build system. This enables TF-A to compile certain code only
2842*91f16700Schasinglulufor specific boot loader stages
2843*91f16700Schasinglulu
2844*91f16700SchasingluluAll assembler files have the ``.S`` extension. The linker source files for each
2845*91f16700Schasingluluboot stage have the extension ``.ld.S``. These are processed by GCC to create the
2846*91f16700Schasinglululinker scripts which have the extension ``.ld``.
2847*91f16700Schasinglulu
2848*91f16700SchasingluluFDTs provide a description of the hardware platform and are used by the Linux
2849*91f16700Schasinglulukernel at boot time. These can be found in the ``fdts`` directory.
2850*91f16700Schasinglulu
2851*91f16700Schasinglulu.. rubric:: References
2852*91f16700Schasinglulu
2853*91f16700Schasinglulu-  `Trusted Board Boot Requirements CLIENT (TBBR-CLIENT) Armv8-A (ARM DEN0006D)`_
2854*91f16700Schasinglulu
2855*91f16700Schasinglulu-  `PSCI`_
2856*91f16700Schasinglulu
2857*91f16700Schasinglulu-  `SMC Calling Convention`_
2858*91f16700Schasinglulu
2859*91f16700Schasinglulu-  :ref:`Interrupt Management Framework`
2860*91f16700Schasinglulu
2861*91f16700Schasinglulu--------------
2862*91f16700Schasinglulu
2863*91f16700Schasinglulu*Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.*
2864*91f16700Schasinglulu
2865*91f16700Schasinglulu.. _SMCCC: https://developer.arm.com/docs/den0028/latest
2866*91f16700Schasinglulu.. _PSCI: https://developer.arm.com/documentation/den0022/latest/
2867*91f16700Schasinglulu.. _Arm ARM: https://developer.arm.com/docs/ddi0487/latest
2868*91f16700Schasinglulu.. _SMC Calling Convention: https://developer.arm.com/docs/den0028/latest
2869*91f16700Schasinglulu.. _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
2870*91f16700Schasinglulu.. _Arm Confidential Compute Architecture (Arm CCA): https://www.arm.com/why-arm/architecture/security-features/arm-confidential-compute-architecture
2871*91f16700Schasinglulu.. _AArch64 exception vector table: https://developer.arm.com/documentation/100933/0100/AArch64-exception-vector-table
2872*91f16700Schasinglulu
2873*91f16700Schasinglulu.. |Image 1| image:: ../resources/diagrams/rt-svc-descs-layout.png
2874