firmware-design.rst 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  1. Firmware Design
  2. ===============
  3. Trusted Firmware-A (TF-A) implements a subset of the Trusted Board Boot
  4. Requirements (TBBR) Platform Design Document (PDD) for Arm reference
  5. platforms.
  6. The TBB sequence starts when the platform is powered on and runs up
  7. to the stage where it hands-off control to firmware running in the normal
  8. world in DRAM. This is the cold boot path.
  9. TF-A also implements the `Power State Coordination Interface PDD`_ as a
  10. runtime service. PSCI is the interface from normal world software to firmware
  11. implementing power management use-cases (for example, secondary CPU boot,
  12. hotplug and idle). Normal world software can access TF-A runtime services via
  13. the Arm SMC (Secure Monitor Call) instruction. The SMC instruction must be
  14. used as mandated by the SMC Calling Convention (`SMCCC`_).
  15. TF-A implements a framework for configuring and managing interrupts generated
  16. in either security state. The details of the interrupt management framework
  17. and its design can be found in :ref:`Interrupt Management Framework`.
  18. TF-A also implements a library for setting up and managing the translation
  19. tables. The details of this library can be found in
  20. :ref:`Translation (XLAT) Tables Library`.
  21. TF-A can be built to support either AArch64 or AArch32 execution state.
  22. .. note::
  23. The descriptions in this chapter are for the Arm TrustZone architecture.
  24. For changes to the firmware design for the
  25. `Arm Confidential Compute Architecture (Arm CCA)`_ please refer to the
  26. chapter :ref:`Realm Management Extension (RME)`.
  27. Cold boot
  28. ---------
  29. The cold boot path starts when the platform is physically turned on. If
  30. ``COLD_BOOT_SINGLE_CPU=0``, one of the CPUs released from reset is chosen as the
  31. primary CPU, and the remaining CPUs are considered secondary CPUs. The primary
  32. CPU is chosen through platform-specific means. The cold boot path is mainly
  33. executed by the primary CPU, other than essential CPU initialization executed by
  34. all CPUs. The secondary CPUs are kept in a safe platform-specific state until
  35. the primary CPU has performed enough initialization to boot them.
  36. Refer to the :ref:`CPU Reset` for more information on the effect of the
  37. ``COLD_BOOT_SINGLE_CPU`` platform build option.
  38. The cold boot path in this implementation of TF-A depends on the execution
  39. state. For AArch64, it is divided into five steps (in order of execution):
  40. - Boot Loader stage 1 (BL1) *AP Trusted ROM*
  41. - Boot Loader stage 2 (BL2) *Trusted Boot Firmware*
  42. - Boot Loader stage 3-1 (BL31) *EL3 Runtime Software*
  43. - Boot Loader stage 3-2 (BL32) *Secure-EL1 Payload* (optional)
  44. - Boot Loader stage 3-3 (BL33) *Non-trusted Firmware*
  45. For AArch32, it is divided into four steps (in order of execution):
  46. - Boot Loader stage 1 (BL1) *AP Trusted ROM*
  47. - Boot Loader stage 2 (BL2) *Trusted Boot Firmware*
  48. - Boot Loader stage 3-2 (BL32) *EL3 Runtime Software*
  49. - Boot Loader stage 3-3 (BL33) *Non-trusted Firmware*
  50. Arm development platforms (Fixed Virtual Platforms (FVPs) and Juno) implement a
  51. combination of the following types of memory regions. Each bootloader stage uses
  52. one or more of these memory regions.
  53. - Regions accessible from both non-secure and secure states. For example,
  54. non-trusted SRAM, ROM and DRAM.
  55. - Regions accessible from only the secure state. For example, trusted SRAM and
  56. ROM. The FVPs also implement the trusted DRAM which is statically
  57. configured. Additionally, the Base FVPs and Juno development platform
  58. configure the TrustZone Controller (TZC) to create a region in the DRAM
  59. which is accessible only from the secure state.
  60. The sections below provide the following details:
  61. - dynamic configuration of Boot Loader stages
  62. - initialization and execution of the first three stages during cold boot
  63. - specification of the EL3 Runtime Software (BL31 for AArch64 and BL32 for
  64. AArch32) entrypoint requirements for use by alternative Trusted Boot
  65. Firmware in place of the provided BL1 and BL2
  66. Dynamic Configuration during cold boot
  67. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. Each of the Boot Loader stages may be dynamically configured if required by the
  69. platform. The Boot Loader stage may optionally specify a firmware
  70. configuration file and/or hardware configuration file as listed below:
  71. - FW_CONFIG - The firmware configuration file. Holds properties shared across
  72. all BLx images.
  73. An example is the "dtb-registry" node, which contains the information about
  74. the other device tree configurations (load-address, size, image_id).
  75. - HW_CONFIG - The hardware configuration file. Can be shared by all Boot Loader
  76. stages and also by the Normal World Rich OS.
  77. - TB_FW_CONFIG - Trusted Boot Firmware configuration file. Shared between BL1
  78. and BL2.
  79. - SOC_FW_CONFIG - SoC Firmware configuration file. Used by BL31.
  80. - TOS_FW_CONFIG - Trusted OS Firmware configuration file. Used by Trusted OS
  81. (BL32).
  82. - NT_FW_CONFIG - Non Trusted Firmware configuration file. Used by Non-trusted
  83. firmware (BL33).
  84. The Arm development platforms use the Flattened Device Tree format for the
  85. dynamic configuration files.
  86. Each Boot Loader stage can pass up to 4 arguments via registers to the next
  87. stage. BL2 passes the list of the next images to execute to the *EL3 Runtime
  88. Software* (BL31 for AArch64 and BL32 for AArch32) via `arg0`. All the other
  89. arguments are platform defined. The Arm development platforms use the following
  90. convention:
  91. - BL1 passes the address of a meminfo_t structure to BL2 via ``arg1``. This
  92. structure contains the memory layout available to BL2.
  93. - When dynamic configuration files are present, the firmware configuration for
  94. the next Boot Loader stage is populated in the first available argument and
  95. the generic hardware configuration is passed the next available argument.
  96. For example,
  97. - FW_CONFIG is loaded by BL1, then its address is passed in ``arg0`` to BL2.
  98. - TB_FW_CONFIG address is retrieved by BL2 from FW_CONFIG device tree.
  99. - If HW_CONFIG is loaded by BL1, then its address is passed in ``arg2`` to
  100. BL2. Note, ``arg1`` is already used for meminfo_t.
  101. - If SOC_FW_CONFIG is loaded by BL2, then its address is passed in ``arg1``
  102. to BL31. Note, ``arg0`` is used to pass the list of executable images.
  103. - Similarly, if HW_CONFIG is loaded by BL1 or BL2, then its address is
  104. passed in ``arg2`` to BL31.
  105. - For other BL3x images, if the firmware configuration file is loaded by
  106. BL2, then its address is passed in ``arg0`` and if HW_CONFIG is loaded
  107. then its address is passed in ``arg1``.
  108. - In case of the Arm FVP platform, FW_CONFIG address passed in ``arg1`` to
  109. BL31/SP_MIN, and the SOC_FW_CONFIG and HW_CONFIG details are retrieved
  110. from FW_CONFIG device tree.
  111. BL1
  112. ~~~
  113. This stage begins execution from the platform's reset vector at EL3. The reset
  114. address is platform dependent but it is usually located in a Trusted ROM area.
  115. The BL1 data section is copied to trusted SRAM at runtime.
  116. On the Arm development platforms, BL1 code starts execution from the reset
  117. vector defined by the constant ``BL1_RO_BASE``. The BL1 data section is copied
  118. to the top of trusted SRAM as defined by the constant ``BL1_RW_BASE``.
  119. The functionality implemented by this stage is as follows.
  120. Determination of boot path
  121. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  122. Whenever a CPU is released from reset, BL1 needs to distinguish between a warm
  123. boot and a cold boot. This is done using platform-specific mechanisms (see the
  124. ``plat_get_my_entrypoint()`` function in the :ref:`Porting Guide`). In the case
  125. of a warm boot, a CPU is expected to continue execution from a separate
  126. entrypoint. In the case of a cold boot, the secondary CPUs are placed in a safe
  127. platform-specific state (see the ``plat_secondary_cold_boot_setup()`` function in
  128. the :ref:`Porting Guide`) while the primary CPU executes the remaining cold boot
  129. path as described in the following sections.
  130. This step only applies when ``PROGRAMMABLE_RESET_ADDRESS=0``. Refer to the
  131. :ref:`CPU Reset` for more information on the effect of the
  132. ``PROGRAMMABLE_RESET_ADDRESS`` platform build option.
  133. Architectural initialization
  134. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  135. BL1 performs minimal architectural initialization as follows.
  136. - Exception vectors
  137. BL1 sets up simple exception vectors for both synchronous and asynchronous
  138. exceptions. The default behavior upon receiving an exception is to populate
  139. a status code in the general purpose register ``X0/R0`` and call the
  140. ``plat_report_exception()`` function (see the :ref:`Porting Guide`). The
  141. status code is one of:
  142. For AArch64:
  143. ::
  144. 0x0 : Synchronous exception from Current EL with SP_EL0
  145. 0x1 : IRQ exception from Current EL with SP_EL0
  146. 0x2 : FIQ exception from Current EL with SP_EL0
  147. 0x3 : System Error exception from Current EL with SP_EL0
  148. 0x4 : Synchronous exception from Current EL with SP_ELx
  149. 0x5 : IRQ exception from Current EL with SP_ELx
  150. 0x6 : FIQ exception from Current EL with SP_ELx
  151. 0x7 : System Error exception from Current EL with SP_ELx
  152. 0x8 : Synchronous exception from Lower EL using aarch64
  153. 0x9 : IRQ exception from Lower EL using aarch64
  154. 0xa : FIQ exception from Lower EL using aarch64
  155. 0xb : System Error exception from Lower EL using aarch64
  156. 0xc : Synchronous exception from Lower EL using aarch32
  157. 0xd : IRQ exception from Lower EL using aarch32
  158. 0xe : FIQ exception from Lower EL using aarch32
  159. 0xf : System Error exception from Lower EL using aarch32
  160. For AArch32:
  161. ::
  162. 0x10 : User mode
  163. 0x11 : FIQ mode
  164. 0x12 : IRQ mode
  165. 0x13 : SVC mode
  166. 0x16 : Monitor mode
  167. 0x17 : Abort mode
  168. 0x1a : Hypervisor mode
  169. 0x1b : Undefined mode
  170. 0x1f : System mode
  171. The ``plat_report_exception()`` implementation on the Arm FVP port programs
  172. the Versatile Express System LED register in the following format to
  173. indicate the occurrence of an unexpected exception:
  174. ::
  175. SYS_LED[0] - Security state (Secure=0/Non-Secure=1)
  176. SYS_LED[2:1] - Exception Level (EL3=0x3, EL2=0x2, EL1=0x1, EL0=0x0)
  177. For AArch32 it is always 0x0
  178. SYS_LED[7:3] - Exception Class (Sync/Async & origin). This is the value
  179. of the status code
  180. A write to the LED register reflects in the System LEDs (S6LED0..7) in the
  181. CLCD window of the FVP.
  182. BL1 does not expect to receive any exceptions other than the SMC exception.
  183. For the latter, BL1 installs a simple stub. The stub expects to receive a
  184. limited set of SMC types (determined by their function IDs in the general
  185. purpose register ``X0/R0``):
  186. - ``BL1_SMC_RUN_IMAGE``: This SMC is raised by BL2 to make BL1 pass control
  187. to EL3 Runtime Software.
  188. - All SMCs listed in section "BL1 SMC Interface" in the :ref:`Firmware Update (FWU)`
  189. Design Guide are supported for AArch64 only. These SMCs are currently
  190. not supported when BL1 is built for AArch32.
  191. Any other SMC leads to an assertion failure.
  192. - CPU initialization
  193. BL1 calls the ``reset_handler()`` function which in turn calls the CPU
  194. specific reset handler function (see the section: "CPU specific operations
  195. framework").
  196. - Control register setup (for AArch64)
  197. - ``SCTLR_EL3``. Instruction cache is enabled by setting the ``SCTLR_EL3.I``
  198. bit. Alignment and stack alignment checking is enabled by setting the
  199. ``SCTLR_EL3.A`` and ``SCTLR_EL3.SA`` bits. Exception endianness is set to
  200. little-endian by clearing the ``SCTLR_EL3.EE`` bit.
  201. - ``SCR_EL3``. The register width of the next lower exception level is set
  202. to AArch64 by setting the ``SCR.RW`` bit. The ``SCR.EA`` bit is set to trap
  203. both External Aborts and SError Interrupts in EL3. The ``SCR.SIF`` bit is
  204. also set to disable instruction fetches from Non-secure memory when in
  205. secure state.
  206. - ``CPTR_EL3``. Accesses to the ``CPACR_EL1`` register from EL1 or EL2, or the
  207. ``CPTR_EL2`` register from EL2 are configured to not trap to EL3 by
  208. clearing the ``CPTR_EL3.TCPAC`` bit. Access to the trace functionality is
  209. configured not to trap to EL3 by clearing the ``CPTR_EL3.TTA`` bit.
  210. Instructions that access the registers associated with Floating Point
  211. and Advanced SIMD execution are configured to not trap to EL3 by
  212. clearing the ``CPTR_EL3.TFP`` bit.
  213. - ``DAIF``. The SError interrupt is enabled by clearing the SError interrupt
  214. mask bit.
  215. - ``MDCR_EL3``. The trap controls, ``MDCR_EL3.TDOSA``, ``MDCR_EL3.TDA`` and
  216. ``MDCR_EL3.TPM``, are set so that accesses to the registers they control
  217. do not trap to EL3. AArch64 Secure self-hosted debug is disabled by
  218. setting the ``MDCR_EL3.SDD`` bit. Also ``MDCR_EL3.SPD32`` is set to
  219. disable AArch32 Secure self-hosted privileged debug from S-EL1.
  220. - Control register setup (for AArch32)
  221. - ``SCTLR``. Instruction cache is enabled by setting the ``SCTLR.I`` bit.
  222. Alignment checking is enabled by setting the ``SCTLR.A`` bit.
  223. Exception endianness is set to little-endian by clearing the
  224. ``SCTLR.EE`` bit.
  225. - ``SCR``. The ``SCR.SIF`` bit is set to disable instruction fetches from
  226. Non-secure memory when in secure state.
  227. - ``CPACR``. Allow execution of Advanced SIMD instructions at PL0 and PL1,
  228. by clearing the ``CPACR.ASEDIS`` bit. Access to the trace functionality
  229. is configured not to trap to undefined mode by clearing the
  230. ``CPACR.TRCDIS`` bit.
  231. - ``NSACR``. Enable non-secure access to Advanced SIMD functionality and
  232. system register access to implemented trace registers.
  233. - ``FPEXC``. Enable access to the Advanced SIMD and floating-point
  234. functionality from all Exception levels.
  235. - ``CPSR.A``. The Asynchronous data abort interrupt is enabled by clearing
  236. the Asynchronous data abort interrupt mask bit.
  237. - ``SDCR``. The ``SDCR.SPD`` field is set to disable AArch32 Secure
  238. self-hosted privileged debug.
  239. Platform initialization
  240. ^^^^^^^^^^^^^^^^^^^^^^^
  241. On Arm platforms, BL1 performs the following platform initializations:
  242. - Enable the Trusted Watchdog.
  243. - Initialize the console.
  244. - Configure the Interconnect to enable hardware coherency.
  245. - Enable the MMU and map the memory it needs to access.
  246. - Configure any required platform storage to load the next bootloader image
  247. (BL2).
  248. - If the BL1 dynamic configuration file, ``TB_FW_CONFIG``, is available, then
  249. load it to the platform defined address and make it available to BL2 via
  250. ``arg0``.
  251. - Configure the system timer and program the `CNTFRQ_EL0` for use by NS-BL1U
  252. and NS-BL2U firmware update images.
  253. Firmware Update detection and execution
  254. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  255. After performing platform setup, BL1 common code calls
  256. ``bl1_plat_get_next_image_id()`` to determine if :ref:`Firmware Update (FWU)` is
  257. required or to proceed with the normal boot process. If the platform code
  258. returns ``BL2_IMAGE_ID`` then the normal boot sequence is executed as described
  259. in the next section, else BL1 assumes that :ref:`Firmware Update (FWU)` is
  260. required and execution passes to the first image in the
  261. :ref:`Firmware Update (FWU)` process. In either case, BL1 retrieves a descriptor
  262. of the next image by calling ``bl1_plat_get_image_desc()``. The image descriptor
  263. contains an ``entry_point_info_t`` structure, which BL1 uses to initialize the
  264. execution state of the next image.
  265. BL2 image load and execution
  266. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  267. In the normal boot flow, BL1 execution continues as follows:
  268. #. BL1 prints the following string from the primary CPU to indicate successful
  269. execution of the BL1 stage:
  270. ::
  271. "Booting Trusted Firmware"
  272. #. BL1 loads a BL2 raw binary image from platform storage, at a
  273. platform-specific base address. Prior to the load, BL1 invokes
  274. ``bl1_plat_handle_pre_image_load()`` which allows the platform to update or
  275. use the image information. If the BL2 image file is not present or if
  276. there is not enough free trusted SRAM the following error message is
  277. printed:
  278. ::
  279. "Failed to load BL2 firmware."
  280. #. BL1 invokes ``bl1_plat_handle_post_image_load()`` which again is intended
  281. for platforms to take further action after image load. This function must
  282. populate the necessary arguments for BL2, which may also include the memory
  283. layout. Further description of the memory layout can be found later
  284. in this document.
  285. #. BL1 passes control to the BL2 image at Secure EL1 (for AArch64) or at
  286. Secure SVC mode (for AArch32), starting from its load address.
  287. BL2
  288. ~~~
  289. BL1 loads and passes control to BL2 at Secure-EL1 (for AArch64) or at Secure
  290. SVC mode (for AArch32) . BL2 is linked against and loaded at a platform-specific
  291. base address (more information can be found later in this document).
  292. The functionality implemented by BL2 is as follows.
  293. Architectural initialization
  294. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  295. For AArch64, BL2 performs the minimal architectural initialization required
  296. for subsequent stages of TF-A and normal world software. EL1 and EL0 are given
  297. access to Floating Point and Advanced SIMD registers by setting the
  298. ``CPACR.FPEN`` bits.
  299. For AArch32, the minimal architectural initialization required for subsequent
  300. stages of TF-A and normal world software is taken care of in BL1 as both BL1
  301. and BL2 execute at PL1.
  302. Platform initialization
  303. ^^^^^^^^^^^^^^^^^^^^^^^
  304. On Arm platforms, BL2 performs the following platform initializations:
  305. - Initialize the console.
  306. - Configure any required platform storage to allow loading further bootloader
  307. images.
  308. - Enable the MMU and map the memory it needs to access.
  309. - Perform platform security setup to allow access to controlled components.
  310. - Reserve some memory for passing information to the next bootloader image
  311. EL3 Runtime Software and populate it.
  312. - Define the extents of memory available for loading each subsequent
  313. bootloader image.
  314. - If BL1 has passed TB_FW_CONFIG dynamic configuration file in ``arg0``,
  315. then parse it.
  316. Image loading in BL2
  317. ^^^^^^^^^^^^^^^^^^^^
  318. BL2 generic code loads the images based on the list of loadable images
  319. provided by the platform. BL2 passes the list of executable images
  320. provided by the platform to the next handover BL image.
  321. The list of loadable images provided by the platform may also contain
  322. dynamic configuration files. The files are loaded and can be parsed as
  323. needed in the ``bl2_plat_handle_post_image_load()`` function. These
  324. configuration files can be passed to next Boot Loader stages as arguments
  325. by updating the corresponding entrypoint information in this function.
  326. SCP_BL2 (System Control Processor Firmware) image load
  327. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  328. Some systems have a separate System Control Processor (SCP) for power, clock,
  329. reset and system control. BL2 loads the optional SCP_BL2 image from platform
  330. storage into a platform-specific region of secure memory. The subsequent
  331. handling of SCP_BL2 is platform specific. For example, on the Juno Arm
  332. development platform port the image is transferred into SCP's internal memory
  333. using the Boot Over MHU (BOM) protocol after being loaded in the trusted SRAM
  334. memory. The SCP executes SCP_BL2 and signals to the Application Processor (AP)
  335. for BL2 execution to continue.
  336. EL3 Runtime Software image load
  337. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  338. BL2 loads the EL3 Runtime Software image from platform storage into a platform-
  339. specific address in trusted SRAM. If there is not enough memory to load the
  340. image or image is missing it leads to an assertion failure.
  341. AArch64 BL32 (Secure-EL1 Payload) image load
  342. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  343. BL2 loads the optional BL32 image from platform storage into a platform-
  344. specific region of secure memory. The image executes in the secure world. BL2
  345. relies on BL31 to pass control to the BL32 image, if present. Hence, BL2
  346. populates a platform-specific area of memory with the entrypoint/load-address
  347. of the BL32 image. The value of the Saved Processor Status Register (``SPSR``)
  348. for entry into BL32 is not determined by BL2, it is initialized by the
  349. Secure-EL1 Payload Dispatcher (see later) within BL31, which is responsible for
  350. managing interaction with BL32. This information is passed to BL31.
  351. BL33 (Non-trusted Firmware) image load
  352. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  353. BL2 loads the BL33 image (e.g. UEFI or other test or boot software) from
  354. platform storage into non-secure memory as defined by the platform.
  355. BL2 relies on EL3 Runtime Software to pass control to BL33 once secure state
  356. initialization is complete. Hence, BL2 populates a platform-specific area of
  357. memory with the entrypoint and Saved Program Status Register (``SPSR``) of the
  358. normal world software image. The entrypoint is the load address of the BL33
  359. image. The ``SPSR`` is determined as specified in Section 5.13 of the
  360. `Power State Coordination Interface PDD`_. This information is passed to the
  361. EL3 Runtime Software.
  362. AArch64 BL31 (EL3 Runtime Software) execution
  363. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  364. BL2 execution continues as follows:
  365. #. BL2 passes control back to BL1 by raising an SMC, providing BL1 with the
  366. BL31 entrypoint. The exception is handled by the SMC exception handler
  367. installed by BL1.
  368. #. BL1 turns off the MMU and flushes the caches. It clears the
  369. ``SCTLR_EL3.M/I/C`` bits, flushes the data cache to the point of coherency
  370. and invalidates the TLBs.
  371. #. BL1 passes control to BL31 at the specified entrypoint at EL3.
  372. Running BL2 at EL3 execution level
  373. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  374. Some platforms have a non-TF-A Boot ROM that expects the next boot stage
  375. to execute at EL3. On these platforms, TF-A BL1 is a waste of memory
  376. as its only purpose is to ensure TF-A BL2 is entered at S-EL1. To avoid
  377. this waste, a special mode enables BL2 to execute at EL3, which allows
  378. a non-TF-A Boot ROM to load and jump directly to BL2. This mode is selected
  379. when the build flag BL2_AT_EL3 is enabled. The main differences in this
  380. mode are:
  381. #. BL2 includes the reset code and the mailbox mechanism to differentiate
  382. cold boot and warm boot. It runs at EL3 doing the arch
  383. initialization required for EL3.
  384. #. BL2 does not receive the meminfo information from BL1 anymore. This
  385. information can be passed by the Boot ROM or be internal to the
  386. BL2 image.
  387. #. Since BL2 executes at EL3, BL2 jumps directly to the next image,
  388. instead of invoking the RUN_IMAGE SMC call.
  389. We assume 3 different types of BootROM support on the platform:
  390. #. The Boot ROM always jumps to the same address, for both cold
  391. and warm boot. In this case, we will need to keep a resident part
  392. of BL2 whose memory cannot be reclaimed by any other image. The
  393. linker script defines the symbols __TEXT_RESIDENT_START__ and
  394. __TEXT_RESIDENT_END__ that allows the platform to configure
  395. correctly the memory map.
  396. #. The platform has some mechanism to indicate the jump address to the
  397. Boot ROM. Platform code can then program the jump address with
  398. psci_warmboot_entrypoint during cold boot.
  399. #. The platform has some mechanism to program the reset address using
  400. the PROGRAMMABLE_RESET_ADDRESS feature. Platform code can then
  401. program the reset address with psci_warmboot_entrypoint during
  402. cold boot, bypassing the boot ROM for warm boot.
  403. In the last 2 cases, no part of BL2 needs to remain resident at
  404. runtime. In the first 2 cases, we expect the Boot ROM to be able to
  405. differentiate between warm and cold boot, to avoid loading BL2 again
  406. during warm boot.
  407. This functionality can be tested with FVP loading the image directly
  408. in memory and changing the address where the system jumps at reset.
  409. For example:
  410. -C cluster0.cpu0.RVBAR=0x4022000
  411. --data cluster0.cpu0=bl2.bin@0x4022000
  412. With this configuration, FVP is like a platform of the first case,
  413. where the Boot ROM jumps always to the same address. For simplification,
  414. BL32 is loaded in DRAM in this case, to avoid other images reclaiming
  415. BL2 memory.
  416. AArch64 BL31
  417. ~~~~~~~~~~~~
  418. The image for this stage is loaded by BL2 and BL1 passes control to BL31 at
  419. EL3. BL31 executes solely in trusted SRAM. BL31 is linked against and
  420. loaded at a platform-specific base address (more information can be found later
  421. in this document). The functionality implemented by BL31 is as follows.
  422. Architectural initialization
  423. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  424. Currently, BL31 performs a similar architectural initialization to BL1 as
  425. far as system register settings are concerned. Since BL1 code resides in ROM,
  426. architectural initialization in BL31 allows override of any previous
  427. initialization done by BL1.
  428. BL31 initializes the per-CPU data framework, which provides a cache of
  429. frequently accessed per-CPU data optimised for fast, concurrent manipulation
  430. on different CPUs. This buffer includes pointers to per-CPU contexts, crash
  431. buffer, CPU reset and power down operations, PSCI data, platform data and so on.
  432. It then replaces the exception vectors populated by BL1 with its own. BL31
  433. exception vectors implement more elaborate support for handling SMCs since this
  434. is the only mechanism to access the runtime services implemented by BL31 (PSCI
  435. for example). BL31 checks each SMC for validity as specified by the
  436. `SMC Calling Convention`_ before passing control to the required SMC
  437. handler routine.
  438. BL31 programs the ``CNTFRQ_EL0`` register with the clock frequency of the system
  439. counter, which is provided by the platform.
  440. Platform initialization
  441. ^^^^^^^^^^^^^^^^^^^^^^^
  442. BL31 performs detailed platform initialization, which enables normal world
  443. software to function correctly.
  444. On Arm platforms, this consists of the following:
  445. - Initialize the console.
  446. - Configure the Interconnect to enable hardware coherency.
  447. - Enable the MMU and map the memory it needs to access.
  448. - Initialize the generic interrupt controller.
  449. - Initialize the power controller device.
  450. - Detect the system topology.
  451. Runtime services initialization
  452. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  453. BL31 is responsible for initializing the runtime services. One of them is PSCI.
  454. As part of the PSCI initializations, BL31 detects the system topology. It also
  455. initializes the data structures that implement the state machine used to track
  456. the state of power domain nodes. The state can be one of ``OFF``, ``RUN`` or
  457. ``RETENTION``. All secondary CPUs are initially in the ``OFF`` state. The cluster
  458. that the primary CPU belongs to is ``ON``; any other cluster is ``OFF``. It also
  459. initializes the locks that protect them. BL31 accesses the state of a CPU or
  460. cluster immediately after reset and before the data cache is enabled in the
  461. warm boot path. It is not currently possible to use 'exclusive' based spinlocks,
  462. therefore BL31 uses locks based on Lamport's Bakery algorithm instead.
  463. The runtime service framework and its initialization is described in more
  464. detail in the "EL3 runtime services framework" section below.
  465. Details about the status of the PSCI implementation are provided in the
  466. "Power State Coordination Interface" section below.
  467. AArch64 BL32 (Secure-EL1 Payload) image initialization
  468. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  469. If a BL32 image is present then there must be a matching Secure-EL1 Payload
  470. Dispatcher (SPD) service (see later for details). During initialization
  471. that service must register a function to carry out initialization of BL32
  472. once the runtime services are fully initialized. BL31 invokes such a
  473. registered function to initialize BL32 before running BL33. This initialization
  474. is not necessary for AArch32 SPs.
  475. Details on BL32 initialization and the SPD's role are described in the
  476. :ref:`firmware_design_sel1_spd` section below.
  477. BL33 (Non-trusted Firmware) execution
  478. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  479. EL3 Runtime Software initializes the EL2 or EL1 processor context for normal-
  480. world cold boot, ensuring that no secure state information finds its way into
  481. the non-secure execution state. EL3 Runtime Software uses the entrypoint
  482. information provided by BL2 to jump to the Non-trusted firmware image (BL33)
  483. at the highest available Exception Level (EL2 if available, otherwise EL1).
  484. Using alternative Trusted Boot Firmware in place of BL1 & BL2 (AArch64 only)
  485. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  486. Some platforms have existing implementations of Trusted Boot Firmware that
  487. would like to use TF-A BL31 for the EL3 Runtime Software. To enable this
  488. firmware architecture it is important to provide a fully documented and stable
  489. interface between the Trusted Boot Firmware and BL31.
  490. Future changes to the BL31 interface will be done in a backwards compatible
  491. way, and this enables these firmware components to be independently enhanced/
  492. updated to develop and exploit new functionality.
  493. Required CPU state when calling ``bl31_entrypoint()`` during cold boot
  494. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  495. This function must only be called by the primary CPU.
  496. On entry to this function the calling primary CPU must be executing in AArch64
  497. EL3, little-endian data access, and all interrupt sources masked:
  498. ::
  499. PSTATE.EL = 3
  500. PSTATE.RW = 1
  501. PSTATE.DAIF = 0xf
  502. SCTLR_EL3.EE = 0
  503. X0 and X1 can be used to pass information from the Trusted Boot Firmware to the
  504. platform code in BL31:
  505. ::
  506. X0 : Reserved for common TF-A information
  507. X1 : Platform specific information
  508. BL31 zero-init sections (e.g. ``.bss``) should not contain valid data on entry,
  509. these will be zero filled prior to invoking platform setup code.
  510. Use of the X0 and X1 parameters
  511. '''''''''''''''''''''''''''''''
  512. The parameters are platform specific and passed from ``bl31_entrypoint()`` to
  513. ``bl31_early_platform_setup()``. The value of these parameters is never directly
  514. used by the common BL31 code.
  515. The convention is that ``X0`` conveys information regarding the BL31, BL32 and
  516. BL33 images from the Trusted Boot firmware and ``X1`` can be used for other
  517. platform specific purpose. This convention allows platforms which use TF-A's
  518. BL1 and BL2 images to transfer additional platform specific information from
  519. Secure Boot without conflicting with future evolution of TF-A using ``X0`` to
  520. pass a ``bl31_params`` structure.
  521. BL31 common and SPD initialization code depends on image and entrypoint
  522. information about BL33 and BL32, which is provided via BL31 platform APIs.
  523. This information is required until the start of execution of BL33. This
  524. information can be provided in a platform defined manner, e.g. compiled into
  525. the platform code in BL31, or provided in a platform defined memory location
  526. by the Trusted Boot firmware, or passed from the Trusted Boot Firmware via the
  527. Cold boot Initialization parameters. This data may need to be cleaned out of
  528. the CPU caches if it is provided by an earlier boot stage and then accessed by
  529. BL31 platform code before the caches are enabled.
  530. TF-A's BL2 implementation passes a ``bl31_params`` structure in
  531. ``X0`` and the Arm development platforms interpret this in the BL31 platform
  532. code.
  533. MMU, Data caches & Coherency
  534. ''''''''''''''''''''''''''''
  535. BL31 does not depend on the enabled state of the MMU, data caches or
  536. interconnect coherency on entry to ``bl31_entrypoint()``. If these are disabled
  537. on entry, these should be enabled during ``bl31_plat_arch_setup()``.
  538. Data structures used in the BL31 cold boot interface
  539. ''''''''''''''''''''''''''''''''''''''''''''''''''''
  540. These structures are designed to support compatibility and independent
  541. evolution of the structures and the firmware images. For example, a version of
  542. BL31 that can interpret the BL3x image information from different versions of
  543. BL2, a platform that uses an extended entry_point_info structure to convey
  544. additional register information to BL31, or a ELF image loader that can convey
  545. more details about the firmware images.
  546. To support these scenarios the structures are versioned and sized, which enables
  547. BL31 to detect which information is present and respond appropriately. The
  548. ``param_header`` is defined to capture this information:
  549. .. code:: c
  550. typedef struct param_header {
  551. uint8_t type; /* type of the structure */
  552. uint8_t version; /* version of this structure */
  553. uint16_t size; /* size of this structure in bytes */
  554. uint32_t attr; /* attributes: unused bits SBZ */
  555. } param_header_t;
  556. The structures using this format are ``entry_point_info``, ``image_info`` and
  557. ``bl31_params``. The code that allocates and populates these structures must set
  558. the header fields appropriately, and the ``SET_PARAM_HEAD()`` a macro is defined
  559. to simplify this action.
  560. Required CPU state for BL31 Warm boot initialization
  561. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  562. When requesting a CPU power-on, or suspending a running CPU, TF-A provides
  563. the platform power management code with a Warm boot initialization
  564. entry-point, to be invoked by the CPU immediately after the reset handler.
  565. On entry to the Warm boot initialization function the calling CPU must be in
  566. AArch64 EL3, little-endian data access and all interrupt sources masked:
  567. ::
  568. PSTATE.EL = 3
  569. PSTATE.RW = 1
  570. PSTATE.DAIF = 0xf
  571. SCTLR_EL3.EE = 0
  572. The PSCI implementation will initialize the processor state and ensure that the
  573. platform power management code is then invoked as required to initialize all
  574. necessary system, cluster and CPU resources.
  575. AArch32 EL3 Runtime Software entrypoint interface
  576. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  577. To enable this firmware architecture it is important to provide a fully
  578. documented and stable interface between the Trusted Boot Firmware and the
  579. AArch32 EL3 Runtime Software.
  580. Future changes to the entrypoint interface will be done in a backwards
  581. compatible way, and this enables these firmware components to be independently
  582. enhanced/updated to develop and exploit new functionality.
  583. Required CPU state when entering during cold boot
  584. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  585. This function must only be called by the primary CPU.
  586. On entry to this function the calling primary CPU must be executing in AArch32
  587. EL3, little-endian data access, and all interrupt sources masked:
  588. ::
  589. PSTATE.AIF = 0x7
  590. SCTLR.EE = 0
  591. R0 and R1 are used to pass information from the Trusted Boot Firmware to the
  592. platform code in AArch32 EL3 Runtime Software:
  593. ::
  594. R0 : Reserved for common TF-A information
  595. R1 : Platform specific information
  596. Use of the R0 and R1 parameters
  597. '''''''''''''''''''''''''''''''
  598. The parameters are platform specific and the convention is that ``R0`` conveys
  599. information regarding the BL3x images from the Trusted Boot firmware and ``R1``
  600. can be used for other platform specific purpose. This convention allows
  601. platforms which use TF-A's BL1 and BL2 images to transfer additional platform
  602. specific information from Secure Boot without conflicting with future
  603. evolution of TF-A using ``R0`` to pass a ``bl_params`` structure.
  604. The AArch32 EL3 Runtime Software is responsible for entry into BL33. This
  605. information can be obtained in a platform defined manner, e.g. compiled into
  606. the AArch32 EL3 Runtime Software, or provided in a platform defined memory
  607. location by the Trusted Boot firmware, or passed from the Trusted Boot Firmware
  608. via the Cold boot Initialization parameters. This data may need to be cleaned
  609. out of the CPU caches if it is provided by an earlier boot stage and then
  610. accessed by AArch32 EL3 Runtime Software before the caches are enabled.
  611. When using AArch32 EL3 Runtime Software, the Arm development platforms pass a
  612. ``bl_params`` structure in ``R0`` from BL2 to be interpreted by AArch32 EL3 Runtime
  613. Software platform code.
  614. MMU, Data caches & Coherency
  615. ''''''''''''''''''''''''''''
  616. AArch32 EL3 Runtime Software must not depend on the enabled state of the MMU,
  617. data caches or interconnect coherency in its entrypoint. They must be explicitly
  618. enabled if required.
  619. Data structures used in cold boot interface
  620. '''''''''''''''''''''''''''''''''''''''''''
  621. The AArch32 EL3 Runtime Software cold boot interface uses ``bl_params`` instead
  622. of ``bl31_params``. The ``bl_params`` structure is based on the convention
  623. described in AArch64 BL31 cold boot interface section.
  624. Required CPU state for warm boot initialization
  625. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  626. When requesting a CPU power-on, or suspending a running CPU, AArch32 EL3
  627. Runtime Software must ensure execution of a warm boot initialization entrypoint.
  628. If TF-A BL1 is used and the PROGRAMMABLE_RESET_ADDRESS build flag is false,
  629. then AArch32 EL3 Runtime Software must ensure that BL1 branches to the warm
  630. boot entrypoint by arranging for the BL1 platform function,
  631. plat_get_my_entrypoint(), to return a non-zero value.
  632. In this case, the warm boot entrypoint must be in AArch32 EL3, little-endian
  633. data access and all interrupt sources masked:
  634. ::
  635. PSTATE.AIF = 0x7
  636. SCTLR.EE = 0
  637. The warm boot entrypoint may be implemented by using TF-A
  638. ``psci_warmboot_entrypoint()`` function. In that case, the platform must fulfil
  639. the pre-requisites mentioned in the
  640. :ref:`PSCI Library Integration guide for Armv8-A AArch32 systems`.
  641. EL3 runtime services framework
  642. ------------------------------
  643. Software executing in the non-secure state and in the secure state at exception
  644. levels lower than EL3 will request runtime services using the Secure Monitor
  645. Call (SMC) instruction. These requests will follow the convention described in
  646. the SMC Calling Convention PDD (`SMCCC`_). The `SMCCC`_ assigns function
  647. identifiers to each SMC request and describes how arguments are passed and
  648. returned.
  649. The EL3 runtime services framework enables the development of services by
  650. different providers that can be easily integrated into final product firmware.
  651. The following sections describe the framework which facilitates the
  652. registration, initialization and use of runtime services in EL3 Runtime
  653. Software (BL31).
  654. The design of the runtime services depends heavily on the concepts and
  655. definitions described in the `SMCCC`_, in particular SMC Function IDs, Owning
  656. Entity Numbers (OEN), Fast and Yielding calls, and the SMC32 and SMC64 calling
  657. conventions. Please refer to that document for more detailed explanation of
  658. these terms.
  659. The following runtime services are expected to be implemented first. They have
  660. not all been instantiated in the current implementation.
  661. #. Standard service calls
  662. This service is for management of the entire system. The Power State
  663. Coordination Interface (`PSCI`_) is the first set of standard service calls
  664. defined by Arm (see PSCI section later).
  665. #. Secure-EL1 Payload Dispatcher service
  666. If a system runs a Trusted OS or other Secure-EL1 Payload (SP) then
  667. it also requires a *Secure Monitor* at EL3 to switch the EL1 processor
  668. context between the normal world (EL1/EL2) and trusted world (Secure-EL1).
  669. The Secure Monitor will make these world switches in response to SMCs. The
  670. `SMCCC`_ provides for such SMCs with the Trusted OS Call and Trusted
  671. Application Call OEN ranges.
  672. The interface between the EL3 Runtime Software and the Secure-EL1 Payload is
  673. not defined by the `SMCCC`_ or any other standard. As a result, each
  674. Secure-EL1 Payload requires a specific Secure Monitor that runs as a runtime
  675. service - within TF-A this service is referred to as the Secure-EL1 Payload
  676. Dispatcher (SPD).
  677. TF-A provides a Test Secure-EL1 Payload (TSP) and its associated Dispatcher
  678. (TSPD). Details of SPD design and TSP/TSPD operation are described in the
  679. :ref:`firmware_design_sel1_spd` section below.
  680. #. CPU implementation service
  681. This service will provide an interface to CPU implementation specific
  682. services for a given platform e.g. access to processor errata workarounds.
  683. This service is currently unimplemented.
  684. Additional services for Arm Architecture, SiP and OEM calls can be implemented.
  685. Each implemented service handles a range of SMC function identifiers as
  686. described in the `SMCCC`_.
  687. Registration
  688. ~~~~~~~~~~~~
  689. A runtime service is registered using the ``DECLARE_RT_SVC()`` macro, specifying
  690. the name of the service, the range of OENs covered, the type of service and
  691. initialization and call handler functions. This macro instantiates a ``const struct rt_svc_desc`` for the service with these details (see ``runtime_svc.h``).
  692. This structure is allocated in a special ELF section ``.rt_svc_descs``, enabling
  693. the framework to find all service descriptors included into BL31.
  694. The specific service for a SMC Function is selected based on the OEN and call
  695. type of the Function ID, and the framework uses that information in the service
  696. descriptor to identify the handler for the SMC Call.
  697. The service descriptors do not include information to identify the precise set
  698. of SMC function identifiers supported by this service implementation, the
  699. security state from which such calls are valid nor the capability to support
  700. 64-bit and/or 32-bit callers (using SMC32 or SMC64). Responding appropriately
  701. to these aspects of a SMC call is the responsibility of the service
  702. implementation, the framework is focused on integration of services from
  703. different providers and minimizing the time taken by the framework before the
  704. service handler is invoked.
  705. Details of the parameters, requirements and behavior of the initialization and
  706. call handling functions are provided in the following sections.
  707. Initialization
  708. ~~~~~~~~~~~~~~
  709. ``runtime_svc_init()`` in ``runtime_svc.c`` initializes the runtime services
  710. framework running on the primary CPU during cold boot as part of the BL31
  711. initialization. This happens prior to initializing a Trusted OS and running
  712. Normal world boot firmware that might in turn use these services.
  713. Initialization involves validating each of the declared runtime service
  714. descriptors, calling the service initialization function and populating the
  715. index used for runtime lookup of the service.
  716. The BL31 linker script collects all of the declared service descriptors into a
  717. single array and defines symbols that allow the framework to locate and traverse
  718. the array, and determine its size.
  719. The framework does basic validation of each descriptor to halt firmware
  720. initialization if service declaration errors are detected. The framework does
  721. not check descriptors for the following error conditions, and may behave in an
  722. unpredictable manner under such scenarios:
  723. #. Overlapping OEN ranges
  724. #. Multiple descriptors for the same range of OENs and ``call_type``
  725. #. Incorrect range of owning entity numbers for a given ``call_type``
  726. Once validated, the service ``init()`` callback is invoked. This function carries
  727. out any essential EL3 initialization before servicing requests. The ``init()``
  728. function is only invoked on the primary CPU during cold boot. If the service
  729. uses per-CPU data this must either be initialized for all CPUs during this call,
  730. or be done lazily when a CPU first issues an SMC call to that service. If
  731. ``init()`` returns anything other than ``0``, this is treated as an initialization
  732. error and the service is ignored: this does not cause the firmware to halt.
  733. The OEN and call type fields present in the SMC Function ID cover a total of
  734. 128 distinct services, but in practice a single descriptor can cover a range of
  735. OENs, e.g. SMCs to call a Trusted OS function. To optimize the lookup of a
  736. service handler, the framework uses an array of 128 indices that map every
  737. distinct OEN/call-type combination either to one of the declared services or to
  738. indicate the service is not handled. This ``rt_svc_descs_indices[]`` array is
  739. populated for all of the OENs covered by a service after the service ``init()``
  740. function has reported success. So a service that fails to initialize will never
  741. have it's ``handle()`` function invoked.
  742. The following figure shows how the ``rt_svc_descs_indices[]`` index maps the SMC
  743. Function ID call type and OEN onto a specific service handler in the
  744. ``rt_svc_descs[]`` array.
  745. |Image 1|
  746. .. _handling-an-smc:
  747. Handling an SMC
  748. ~~~~~~~~~~~~~~~
  749. When the EL3 runtime services framework receives a Secure Monitor Call, the SMC
  750. Function ID is passed in W0 from the lower exception level (as per the
  751. `SMCCC`_). If the calling register width is AArch32, it is invalid to invoke an
  752. SMC Function which indicates the SMC64 calling convention: such calls are
  753. ignored and return the Unknown SMC Function Identifier result code ``0xFFFFFFFF``
  754. in R0/X0.
  755. Bit[31] (fast/yielding call) and bits[29:24] (owning entity number) of the SMC
  756. Function ID are combined to index into the ``rt_svc_descs_indices[]`` array. The
  757. resulting value might indicate a service that has no handler, in this case the
  758. framework will also report an Unknown SMC Function ID. Otherwise, the value is
  759. used as a further index into the ``rt_svc_descs[]`` array to locate the required
  760. service and handler.
  761. The service's ``handle()`` callback is provided with five of the SMC parameters
  762. directly, the others are saved into memory for retrieval (if needed) by the
  763. handler. The handler is also provided with an opaque ``handle`` for use with the
  764. supporting library for parameter retrieval, setting return values and context
  765. manipulation. The ``flags`` parameter indicates the security state of the caller
  766. and the state of the SVE hint bit per the SMCCCv1.3. The framework finally sets
  767. up the execution stack for the handler, and invokes the services ``handle()``
  768. function.
  769. On return from the handler the result registers are populated in X0-X7 as needed
  770. before restoring the stack and CPU state and returning from the original SMC.
  771. Exception Handling Framework
  772. ----------------------------
  773. Please refer to the :ref:`Exception Handling Framework` document.
  774. Power State Coordination Interface
  775. ----------------------------------
  776. TODO: Provide design walkthrough of PSCI implementation.
  777. The PSCI v1.1 specification categorizes APIs as optional and mandatory. All the
  778. mandatory APIs in PSCI v1.1, PSCI v1.0 and in PSCI v0.2 draft specification
  779. `Power State Coordination Interface PDD`_ are implemented. The table lists
  780. the PSCI v1.1 APIs and their support in generic code.
  781. An API implementation might have a dependency on platform code e.g. CPU_SUSPEND
  782. requires the platform to export a part of the implementation. Hence the level
  783. of support of the mandatory APIs depends upon the support exported by the
  784. platform port as well. The Juno and FVP (all variants) platforms export all the
  785. required support.
  786. +-----------------------------+-------------+-------------------------------+
  787. | PSCI v1.1 API | Supported | Comments |
  788. +=============================+=============+===============================+
  789. | ``PSCI_VERSION`` | Yes | The version returned is 1.1 |
  790. +-----------------------------+-------------+-------------------------------+
  791. | ``CPU_SUSPEND`` | Yes\* | |
  792. +-----------------------------+-------------+-------------------------------+
  793. | ``CPU_OFF`` | Yes\* | |
  794. +-----------------------------+-------------+-------------------------------+
  795. | ``CPU_ON`` | Yes\* | |
  796. +-----------------------------+-------------+-------------------------------+
  797. | ``AFFINITY_INFO`` | Yes | |
  798. +-----------------------------+-------------+-------------------------------+
  799. | ``MIGRATE`` | Yes\*\* | |
  800. +-----------------------------+-------------+-------------------------------+
  801. | ``MIGRATE_INFO_TYPE`` | Yes\*\* | |
  802. +-----------------------------+-------------+-------------------------------+
  803. | ``MIGRATE_INFO_CPU`` | Yes\*\* | |
  804. +-----------------------------+-------------+-------------------------------+
  805. | ``SYSTEM_OFF`` | Yes\* | |
  806. +-----------------------------+-------------+-------------------------------+
  807. | ``SYSTEM_RESET`` | Yes\* | |
  808. +-----------------------------+-------------+-------------------------------+
  809. | ``PSCI_FEATURES`` | Yes | |
  810. +-----------------------------+-------------+-------------------------------+
  811. | ``CPU_FREEZE`` | No | |
  812. +-----------------------------+-------------+-------------------------------+
  813. | ``CPU_DEFAULT_SUSPEND`` | No | |
  814. +-----------------------------+-------------+-------------------------------+
  815. | ``NODE_HW_STATE`` | Yes\* | |
  816. +-----------------------------+-------------+-------------------------------+
  817. | ``SYSTEM_SUSPEND`` | Yes\* | |
  818. +-----------------------------+-------------+-------------------------------+
  819. | ``PSCI_SET_SUSPEND_MODE`` | No | |
  820. +-----------------------------+-------------+-------------------------------+
  821. | ``PSCI_STAT_RESIDENCY`` | Yes\* | |
  822. +-----------------------------+-------------+-------------------------------+
  823. | ``PSCI_STAT_COUNT`` | Yes\* | |
  824. +-----------------------------+-------------+-------------------------------+
  825. | ``SYSTEM_RESET2`` | Yes\* | |
  826. +-----------------------------+-------------+-------------------------------+
  827. | ``MEM_PROTECT`` | Yes\* | |
  828. +-----------------------------+-------------+-------------------------------+
  829. | ``MEM_PROTECT_CHECK_RANGE`` | Yes\* | |
  830. +-----------------------------+-------------+-------------------------------+
  831. \*Note : These PSCI APIs require platform power management hooks to be
  832. registered with the generic PSCI code to be supported.
  833. \*\*Note : These PSCI APIs require appropriate Secure Payload Dispatcher
  834. hooks to be registered with the generic PSCI code to be supported.
  835. The PSCI implementation in TF-A is a library which can be integrated with
  836. AArch64 or AArch32 EL3 Runtime Software for Armv8-A systems. A guide to
  837. integrating PSCI library with AArch32 EL3 Runtime Software can be found
  838. at :ref:`PSCI Library Integration guide for Armv8-A AArch32 systems`.
  839. .. _firmware_design_sel1_spd:
  840. Secure-EL1 Payloads and Dispatchers
  841. -----------------------------------
  842. On a production system that includes a Trusted OS running in Secure-EL1/EL0,
  843. the Trusted OS is coupled with a companion runtime service in the BL31
  844. firmware. This service is responsible for the initialisation of the Trusted
  845. OS and all communications with it. The Trusted OS is the BL32 stage of the
  846. boot flow in TF-A. The firmware will attempt to locate, load and execute a
  847. BL32 image.
  848. TF-A uses a more general term for the BL32 software that runs at Secure-EL1 -
  849. the *Secure-EL1 Payload* - as it is not always a Trusted OS.
  850. TF-A provides a Test Secure-EL1 Payload (TSP) and a Test Secure-EL1 Payload
  851. Dispatcher (TSPD) service as an example of how a Trusted OS is supported on a
  852. production system using the Runtime Services Framework. On such a system, the
  853. Test BL32 image and service are replaced by the Trusted OS and its dispatcher
  854. service. The TF-A build system expects that the dispatcher will define the
  855. build flag ``NEED_BL32`` to enable it to include the BL32 in the build either
  856. as a binary or to compile from source depending on whether the ``BL32`` build
  857. option is specified or not.
  858. The TSP runs in Secure-EL1. It is designed to demonstrate synchronous
  859. communication with the normal-world software running in EL1/EL2. Communication
  860. is initiated by the normal-world software
  861. - either directly through a Fast SMC (as defined in the `SMCCC`_)
  862. - or indirectly through a `PSCI`_ SMC. The `PSCI`_ implementation in turn
  863. informs the TSPD about the requested power management operation. This allows
  864. the TSP to prepare for or respond to the power state change
  865. The TSPD service is responsible for.
  866. - Initializing the TSP
  867. - Routing requests and responses between the secure and the non-secure
  868. states during the two types of communications just described
  869. Initializing a BL32 Image
  870. ~~~~~~~~~~~~~~~~~~~~~~~~~
  871. The Secure-EL1 Payload Dispatcher (SPD) service is responsible for initializing
  872. the BL32 image. It needs access to the information passed by BL2 to BL31 to do
  873. so. This is provided by:
  874. .. code:: c
  875. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t);
  876. which returns a reference to the ``entry_point_info`` structure corresponding to
  877. the image which will be run in the specified security state. The SPD uses this
  878. API to get entry point information for the SECURE image, BL32.
  879. In the absence of a BL32 image, BL31 passes control to the normal world
  880. bootloader image (BL33). When the BL32 image is present, it is typical
  881. that the SPD wants control to be passed to BL32 first and then later to BL33.
  882. To do this the SPD has to register a BL32 initialization function during
  883. initialization of the SPD service. The BL32 initialization function has this
  884. prototype:
  885. .. code:: c
  886. int32_t init(void);
  887. and is registered using the ``bl31_register_bl32_init()`` function.
  888. TF-A supports two approaches for the SPD to pass control to BL32 before
  889. returning through EL3 and running the non-trusted firmware (BL33):
  890. #. In the BL32 setup function, use ``bl31_set_next_image_type()`` to
  891. request that the exit from ``bl31_main()`` is to the BL32 entrypoint in
  892. Secure-EL1. BL31 will exit to BL32 using the asynchronous method by
  893. calling ``bl31_prepare_next_image_entry()`` and ``el3_exit()``.
  894. When the BL32 has completed initialization at Secure-EL1, it returns to
  895. BL31 by issuing an SMC, using a Function ID allocated to the SPD. On
  896. receipt of this SMC, the SPD service handler should switch the CPU context
  897. from trusted to normal world and use the ``bl31_set_next_image_type()`` and
  898. ``bl31_prepare_next_image_entry()`` functions to set up the initial return to
  899. the normal world firmware BL33. On return from the handler the framework
  900. will exit to EL2 and run BL33.
  901. #. The BL32 setup function registers an initialization function using
  902. ``bl31_register_bl32_init()`` which provides a SPD-defined mechanism to
  903. invoke a 'world-switch synchronous call' to Secure-EL1 to run the BL32
  904. entrypoint.
  905. .. note::
  906. The Test SPD service included with TF-A provides one implementation
  907. of such a mechanism.
  908. On completion BL32 returns control to BL31 via a SMC, and on receipt the
  909. SPD service handler invokes the synchronous call return mechanism to return
  910. to the BL32 initialization function. On return from this function,
  911. ``bl31_main()`` will set up the return to the normal world firmware BL33 and
  912. continue the boot process in the normal world.
  913. Crash Reporting in BL31
  914. -----------------------
  915. BL31 implements a scheme for reporting the processor state when an unhandled
  916. exception is encountered. The reporting mechanism attempts to preserve all the
  917. register contents and report it via a dedicated UART (PL011 console). BL31
  918. reports the general purpose, EL3, Secure EL1 and some EL2 state registers.
  919. A dedicated per-CPU crash stack is maintained by BL31 and this is retrieved via
  920. the per-CPU pointer cache. The implementation attempts to minimise the memory
  921. required for this feature. The file ``crash_reporting.S`` contains the
  922. implementation for crash reporting.
  923. The sample crash output is shown below.
  924. ::
  925. x0 = 0x000000002a4a0000
  926. x1 = 0x0000000000000001
  927. x2 = 0x0000000000000002
  928. x3 = 0x0000000000000003
  929. x4 = 0x0000000000000004
  930. x5 = 0x0000000000000005
  931. x6 = 0x0000000000000006
  932. x7 = 0x0000000000000007
  933. x8 = 0x0000000000000008
  934. x9 = 0x0000000000000009
  935. x10 = 0x0000000000000010
  936. x11 = 0x0000000000000011
  937. x12 = 0x0000000000000012
  938. x13 = 0x0000000000000013
  939. x14 = 0x0000000000000014
  940. x15 = 0x0000000000000015
  941. x16 = 0x0000000000000016
  942. x17 = 0x0000000000000017
  943. x18 = 0x0000000000000018
  944. x19 = 0x0000000000000019
  945. x20 = 0x0000000000000020
  946. x21 = 0x0000000000000021
  947. x22 = 0x0000000000000022
  948. x23 = 0x0000000000000023
  949. x24 = 0x0000000000000024
  950. x25 = 0x0000000000000025
  951. x26 = 0x0000000000000026
  952. x27 = 0x0000000000000027
  953. x28 = 0x0000000000000028
  954. x29 = 0x0000000000000029
  955. x30 = 0x0000000088000b78
  956. scr_el3 = 0x000000000003073d
  957. sctlr_el3 = 0x00000000b0cd183f
  958. cptr_el3 = 0x0000000000000000
  959. tcr_el3 = 0x000000008080351c
  960. daif = 0x00000000000002c0
  961. mair_el3 = 0x00000000004404ff
  962. spsr_el3 = 0x0000000060000349
  963. elr_el3 = 0x0000000088000114
  964. ttbr0_el3 = 0x0000000004018201
  965. esr_el3 = 0x00000000be000000
  966. far_el3 = 0x0000000000000000
  967. spsr_el1 = 0x0000000000000000
  968. elr_el1 = 0x0000000000000000
  969. spsr_abt = 0x0000000000000000
  970. spsr_und = 0x0000000000000000
  971. spsr_irq = 0x0000000000000000
  972. spsr_fiq = 0x0000000000000000
  973. sctlr_el1 = 0x0000000030d00800
  974. actlr_el1 = 0x0000000000000000
  975. cpacr_el1 = 0x0000000000000000
  976. csselr_el1 = 0x0000000000000000
  977. sp_el1 = 0x0000000000000000
  978. esr_el1 = 0x0000000000000000
  979. ttbr0_el1 = 0x0000000000000000
  980. ttbr1_el1 = 0x0000000000000000
  981. mair_el1 = 0x0000000000000000
  982. amair_el1 = 0x0000000000000000
  983. tcr_el1 = 0x0000000000000000
  984. tpidr_el1 = 0x0000000000000000
  985. tpidr_el0 = 0x0000000000000000
  986. tpidrro_el0 = 0x0000000000000000
  987. par_el1 = 0x0000000000000000
  988. mpidr_el1 = 0x0000000080000000
  989. afsr0_el1 = 0x0000000000000000
  990. afsr1_el1 = 0x0000000000000000
  991. contextidr_el1 = 0x0000000000000000
  992. vbar_el1 = 0x0000000000000000
  993. cntp_ctl_el0 = 0x0000000000000000
  994. cntp_cval_el0 = 0x0000000000000000
  995. cntv_ctl_el0 = 0x0000000000000000
  996. cntv_cval_el0 = 0x0000000000000000
  997. cntkctl_el1 = 0x0000000000000000
  998. sp_el0 = 0x0000000004014940
  999. isr_el1 = 0x0000000000000000
  1000. dacr32_el2 = 0x0000000000000000
  1001. ifsr32_el2 = 0x0000000000000000
  1002. icc_hppir0_el1 = 0x00000000000003ff
  1003. icc_hppir1_el1 = 0x00000000000003ff
  1004. icc_ctlr_el3 = 0x0000000000080400
  1005. gicd_ispendr regs (Offsets 0x200-0x278)
  1006. Offset Value
  1007. 0x200: 0x0000000000000000
  1008. 0x208: 0x0000000000000000
  1009. 0x210: 0x0000000000000000
  1010. 0x218: 0x0000000000000000
  1011. 0x220: 0x0000000000000000
  1012. 0x228: 0x0000000000000000
  1013. 0x230: 0x0000000000000000
  1014. 0x238: 0x0000000000000000
  1015. 0x240: 0x0000000000000000
  1016. 0x248: 0x0000000000000000
  1017. 0x250: 0x0000000000000000
  1018. 0x258: 0x0000000000000000
  1019. 0x260: 0x0000000000000000
  1020. 0x268: 0x0000000000000000
  1021. 0x270: 0x0000000000000000
  1022. 0x278: 0x0000000000000000
  1023. Guidelines for Reset Handlers
  1024. -----------------------------
  1025. TF-A implements a framework that allows CPU and platform ports to perform
  1026. actions very early after a CPU is released from reset in both the cold and warm
  1027. boot paths. This is done by calling the ``reset_handler()`` function in both
  1028. the BL1 and BL31 images. It in turn calls the platform and CPU specific reset
  1029. handling functions.
  1030. Details for implementing a CPU specific reset handler can be found in
  1031. Section 8. Details for implementing a platform specific reset handler can be
  1032. found in the :ref:`Porting Guide` (see the ``plat_reset_handler()`` function).
  1033. When adding functionality to a reset handler, keep in mind that if a different
  1034. reset handling behavior is required between the first and the subsequent
  1035. invocations of the reset handling code, this should be detected at runtime.
  1036. In other words, the reset handler should be able to detect whether an action has
  1037. already been performed and act as appropriate. Possible courses of actions are,
  1038. e.g. skip the action the second time, or undo/redo it.
  1039. .. _configuring-secure-interrupts:
  1040. Configuring secure interrupts
  1041. -----------------------------
  1042. The GIC driver is responsible for performing initial configuration of secure
  1043. interrupts on the platform. To this end, the platform is expected to provide the
  1044. GIC driver (either GICv2 or GICv3, as selected by the platform) with the
  1045. interrupt configuration during the driver initialisation.
  1046. Secure interrupt configuration are specified in an array of secure interrupt
  1047. properties. In this scheme, in both GICv2 and GICv3 driver data structures, the
  1048. ``interrupt_props`` member points to an array of interrupt properties. Each
  1049. element of the array specifies the interrupt number and its attributes
  1050. (priority, group, configuration). Each element of the array shall be populated
  1051. by the macro ``INTR_PROP_DESC()``. The macro takes the following arguments:
  1052. - 10-bit interrupt number,
  1053. - 8-bit interrupt priority,
  1054. - Interrupt type (one of ``INTR_TYPE_EL3``, ``INTR_TYPE_S_EL1``,
  1055. ``INTR_TYPE_NS``),
  1056. - Interrupt configuration (either ``GIC_INTR_CFG_LEVEL`` or
  1057. ``GIC_INTR_CFG_EDGE``).
  1058. .. _firmware_design_cpu_ops_fwk:
  1059. CPU specific operations framework
  1060. ---------------------------------
  1061. Certain aspects of the Armv8-A architecture are implementation defined,
  1062. that is, certain behaviours are not architecturally defined, but must be
  1063. defined and documented by individual processor implementations. TF-A
  1064. implements a framework which categorises the common implementation defined
  1065. behaviours and allows a processor to export its implementation of that
  1066. behaviour. The categories are:
  1067. #. Processor specific reset sequence.
  1068. #. Processor specific power down sequences.
  1069. #. Processor specific register dumping as a part of crash reporting.
  1070. #. Errata status reporting.
  1071. Each of the above categories fulfils a different requirement.
  1072. #. allows any processor specific initialization before the caches and MMU
  1073. are turned on, like implementation of errata workarounds, entry into
  1074. the intra-cluster coherency domain etc.
  1075. #. allows each processor to implement the power down sequence mandated in
  1076. its Technical Reference Manual (TRM).
  1077. #. allows a processor to provide additional information to the developer
  1078. in the event of a crash, for example Cortex-A53 has registers which
  1079. can expose the data cache contents.
  1080. #. allows a processor to define a function that inspects and reports the status
  1081. of all errata workarounds on that processor.
  1082. Please note that only 2. is mandated by the TRM.
  1083. The CPU specific operations framework scales to accommodate a large number of
  1084. different CPUs during power down and reset handling. The platform can specify
  1085. any CPU optimization it wants to enable for each CPU. It can also specify
  1086. the CPU errata workarounds to be applied for each CPU type during reset
  1087. handling by defining CPU errata compile time macros. Details on these macros
  1088. can be found in the :ref:`Arm CPU Specific Build Macros` document.
  1089. The CPU specific operations framework depends on the ``cpu_ops`` structure which
  1090. needs to be exported for each type of CPU in the platform. It is defined in
  1091. ``include/lib/cpus/aarch64/cpu_macros.S`` and has the following fields : ``midr``,
  1092. ``reset_func()``, ``cpu_pwr_down_ops`` (array of power down functions) and
  1093. ``cpu_reg_dump()``.
  1094. The CPU specific files in ``lib/cpus`` export a ``cpu_ops`` data structure with
  1095. suitable handlers for that CPU. For example, ``lib/cpus/aarch64/cortex_a53.S``
  1096. exports the ``cpu_ops`` for Cortex-A53 CPU. According to the platform
  1097. configuration, these CPU specific files must be included in the build by
  1098. the platform makefile. The generic CPU specific operations framework code exists
  1099. in ``lib/cpus/aarch64/cpu_helpers.S``.
  1100. CPU specific Reset Handling
  1101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1102. After a reset, the state of the CPU when it calls generic reset handler is:
  1103. MMU turned off, both instruction and data caches turned off and not part
  1104. of any coherency domain.
  1105. The BL entrypoint code first invokes the ``plat_reset_handler()`` to allow
  1106. the platform to perform any system initialization required and any system
  1107. errata workarounds that needs to be applied. The ``get_cpu_ops_ptr()`` reads
  1108. the current CPU midr, finds the matching ``cpu_ops`` entry in the ``cpu_ops``
  1109. array and returns it. Note that only the part number and implementer fields
  1110. in midr are used to find the matching ``cpu_ops`` entry. The ``reset_func()`` in
  1111. the returned ``cpu_ops`` is then invoked which executes the required reset
  1112. handling for that CPU and also any errata workarounds enabled by the platform.
  1113. This function must preserve the values of general purpose registers x20 to x29.
  1114. Refer to Section "Guidelines for Reset Handlers" for general guidelines
  1115. regarding placement of code in a reset handler.
  1116. CPU specific power down sequence
  1117. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1118. During the BL31 initialization sequence, the pointer to the matching ``cpu_ops``
  1119. entry is stored in per-CPU data by ``init_cpu_ops()`` so that it can be quickly
  1120. retrieved during power down sequences.
  1121. Various CPU drivers register handlers to perform power down at certain power
  1122. levels for that specific CPU. The PSCI service, upon receiving a power down
  1123. request, determines the highest power level at which to execute power down
  1124. sequence for a particular CPU. It uses the ``prepare_cpu_pwr_dwn()`` function to
  1125. pick the right power down handler for the requested level. The function
  1126. retrieves ``cpu_ops`` pointer member of per-CPU data, and from that, further
  1127. retrieves ``cpu_pwr_down_ops`` array, and indexes into the required level. If the
  1128. requested power level is higher than what a CPU driver supports, the handler
  1129. registered for highest level is invoked.
  1130. At runtime the platform hooks for power down are invoked by the PSCI service to
  1131. perform platform specific operations during a power down sequence, for example
  1132. turning off CCI coherency during a cluster power down.
  1133. CPU specific register reporting during crash
  1134. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1135. If the crash reporting is enabled in BL31, when a crash occurs, the crash
  1136. reporting framework calls ``do_cpu_reg_dump`` which retrieves the matching
  1137. ``cpu_ops`` using ``get_cpu_ops_ptr()`` function. The ``cpu_reg_dump()`` in
  1138. ``cpu_ops`` is invoked, which then returns the CPU specific register values to
  1139. be reported and a pointer to the ASCII list of register names in a format
  1140. expected by the crash reporting framework.
  1141. .. _firmware_design_cpu_errata_reporting:
  1142. CPU errata status reporting
  1143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1144. Errata workarounds for CPUs supported in TF-A are applied during both cold and
  1145. warm boots, shortly after reset. Individual Errata workarounds are enabled as
  1146. build options. Some errata workarounds have potential run-time implications;
  1147. therefore some are enabled by default, others not. Platform ports shall
  1148. override build options to enable or disable errata as appropriate. The CPU
  1149. drivers take care of applying errata workarounds that are enabled and applicable
  1150. to a given CPU. Refer to :ref:`arm_cpu_macros_errata_workarounds` for more
  1151. information.
  1152. Functions in CPU drivers that apply errata workaround must follow the
  1153. conventions listed below.
  1154. The errata workaround must be authored as two separate functions:
  1155. - One that checks for errata. This function must determine whether that errata
  1156. applies to the current CPU. Typically this involves matching the current
  1157. CPUs revision and variant against a value that's known to be affected by the
  1158. errata. If the function determines that the errata applies to this CPU, it
  1159. must return ``ERRATA_APPLIES``; otherwise, it must return
  1160. ``ERRATA_NOT_APPLIES``. The utility functions ``cpu_get_rev_var`` and
  1161. ``cpu_rev_var_ls`` functions may come in handy for this purpose.
  1162. For an errata identified as ``E``, the check function must be named
  1163. ``check_errata_E``.
  1164. This function will be invoked at different times, both from assembly and from
  1165. C run time. Therefore it must follow AAPCS, and must not use stack.
  1166. - Another one that applies the errata workaround. This function would call the
  1167. check function described above, and applies errata workaround if required.
  1168. CPU drivers that apply errata workaround can optionally implement an assembly
  1169. function that report the status of errata workarounds pertaining to that CPU.
  1170. For a driver that registers the CPU, for example, ``cpux`` via ``declare_cpu_ops``
  1171. macro, the errata reporting function, if it exists, must be named
  1172. ``cpux_errata_report``. This function will always be called with MMU enabled; it
  1173. must follow AAPCS and may use stack.
  1174. In a debug build of TF-A, on a CPU that comes out of reset, both BL1 and the
  1175. runtime firmware (BL31 in AArch64, and BL32 in AArch32) will invoke errata
  1176. status reporting function, if one exists, for that type of CPU.
  1177. To report the status of each errata workaround, the function shall use the
  1178. assembler macro ``report_errata``, passing it:
  1179. - The build option that enables the errata;
  1180. - The name of the CPU: this must be the same identifier that CPU driver
  1181. registered itself with, using ``declare_cpu_ops``;
  1182. - And the errata identifier: the identifier must match what's used in the
  1183. errata's check function described above.
  1184. The errata status reporting function will be called once per CPU type/errata
  1185. combination during the software's active life time.
  1186. It's expected that whenever an errata workaround is submitted to TF-A, the
  1187. errata reporting function is appropriately extended to report its status as
  1188. well.
  1189. Reporting the status of errata workaround is for informational purpose only; it
  1190. has no functional significance.
  1191. Memory layout of BL images
  1192. --------------------------
  1193. Each bootloader image can be divided in 2 parts:
  1194. - the static contents of the image. These are data actually stored in the
  1195. binary on the disk. In the ELF terminology, they are called ``PROGBITS``
  1196. sections;
  1197. - the run-time contents of the image. These are data that don't occupy any
  1198. space in the binary on the disk. The ELF binary just contains some
  1199. metadata indicating where these data will be stored at run-time and the
  1200. corresponding sections need to be allocated and initialized at run-time.
  1201. In the ELF terminology, they are called ``NOBITS`` sections.
  1202. All PROGBITS sections are grouped together at the beginning of the image,
  1203. followed by all NOBITS sections. This is true for all TF-A images and it is
  1204. governed by the linker scripts. This ensures that the raw binary images are
  1205. as small as possible. If a NOBITS section was inserted in between PROGBITS
  1206. sections then the resulting binary file would contain zero bytes in place of
  1207. this NOBITS section, making the image unnecessarily bigger. Smaller images
  1208. allow faster loading from the FIP to the main memory.
  1209. For BL31, a platform can specify an alternate location for NOBITS sections
  1210. (other than immediately following PROGBITS sections) by setting
  1211. ``SEPARATE_NOBITS_REGION`` to 1 and defining ``BL31_NOBITS_BASE`` and
  1212. ``BL31_NOBITS_LIMIT``.
  1213. Linker scripts and symbols
  1214. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1215. Each bootloader stage image layout is described by its own linker script. The
  1216. linker scripts export some symbols into the program symbol table. Their values
  1217. correspond to particular addresses. TF-A code can refer to these symbols to
  1218. figure out the image memory layout.
  1219. Linker symbols follow the following naming convention in TF-A.
  1220. - ``__<SECTION>_START__``
  1221. Start address of a given section named ``<SECTION>``.
  1222. - ``__<SECTION>_END__``
  1223. End address of a given section named ``<SECTION>``. If there is an alignment
  1224. constraint on the section's end address then ``__<SECTION>_END__`` corresponds
  1225. to the end address of the section's actual contents, rounded up to the right
  1226. boundary. Refer to the value of ``__<SECTION>_UNALIGNED_END__`` to know the
  1227. actual end address of the section's contents.
  1228. - ``__<SECTION>_UNALIGNED_END__``
  1229. End address of a given section named ``<SECTION>`` without any padding or
  1230. rounding up due to some alignment constraint.
  1231. - ``__<SECTION>_SIZE__``
  1232. Size (in bytes) of a given section named ``<SECTION>``. If there is an
  1233. alignment constraint on the section's end address then ``__<SECTION>_SIZE__``
  1234. corresponds to the size of the section's actual contents, rounded up to the
  1235. right boundary. In other words, ``__<SECTION>_SIZE__ = __<SECTION>_END__ - _<SECTION>_START__``. Refer to the value of ``__<SECTION>_UNALIGNED_SIZE__``
  1236. to know the actual size of the section's contents.
  1237. - ``__<SECTION>_UNALIGNED_SIZE__``
  1238. Size (in bytes) of a given section named ``<SECTION>`` without any padding or
  1239. rounding up due to some alignment constraint. In other words,
  1240. ``__<SECTION>_UNALIGNED_SIZE__ = __<SECTION>_UNALIGNED_END__ - __<SECTION>_START__``.
  1241. Some of the linker symbols are mandatory as TF-A code relies on them to be
  1242. defined. They are listed in the following subsections. Some of them must be
  1243. provided for each bootloader stage and some are specific to a given bootloader
  1244. stage.
  1245. The linker scripts define some extra, optional symbols. They are not actually
  1246. used by any code but they help in understanding the bootloader images' memory
  1247. layout as they are easy to spot in the link map files.
  1248. Common linker symbols
  1249. ^^^^^^^^^^^^^^^^^^^^^
  1250. All BL images share the following requirements:
  1251. - The BSS section must be zero-initialised before executing any C code.
  1252. - The coherent memory section (if enabled) must be zero-initialised as well.
  1253. - The MMU setup code needs to know the extents of the coherent and read-only
  1254. memory regions to set the right memory attributes. When
  1255. ``SEPARATE_CODE_AND_RODATA=1``, it needs to know more specifically how the
  1256. read-only memory region is divided between code and data.
  1257. The following linker symbols are defined for this purpose:
  1258. - ``__BSS_START__``
  1259. - ``__BSS_SIZE__``
  1260. - ``__COHERENT_RAM_START__`` Must be aligned on a page-size boundary.
  1261. - ``__COHERENT_RAM_END__`` Must be aligned on a page-size boundary.
  1262. - ``__COHERENT_RAM_UNALIGNED_SIZE__``
  1263. - ``__RO_START__``
  1264. - ``__RO_END__``
  1265. - ``__TEXT_START__``
  1266. - ``__TEXT_END__``
  1267. - ``__RODATA_START__``
  1268. - ``__RODATA_END__``
  1269. BL1's linker symbols
  1270. ^^^^^^^^^^^^^^^^^^^^
  1271. BL1 being the ROM image, it has additional requirements. BL1 resides in ROM and
  1272. it is entirely executed in place but it needs some read-write memory for its
  1273. mutable data. Its ``.data`` section (i.e. its allocated read-write data) must be
  1274. relocated from ROM to RAM before executing any C code.
  1275. The following additional linker symbols are defined for BL1:
  1276. - ``__BL1_ROM_END__`` End address of BL1's ROM contents, covering its code
  1277. and ``.data`` section in ROM.
  1278. - ``__DATA_ROM_START__`` Start address of the ``.data`` section in ROM. Must be
  1279. aligned on a 16-byte boundary.
  1280. - ``__DATA_RAM_START__`` Address in RAM where the ``.data`` section should be
  1281. copied over. Must be aligned on a 16-byte boundary.
  1282. - ``__DATA_SIZE__`` Size of the ``.data`` section (in ROM or RAM).
  1283. - ``__BL1_RAM_START__`` Start address of BL1 read-write data.
  1284. - ``__BL1_RAM_END__`` End address of BL1 read-write data.
  1285. How to choose the right base addresses for each bootloader stage image
  1286. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1287. There is currently no support for dynamic image loading in TF-A. This means
  1288. that all bootloader images need to be linked against their ultimate runtime
  1289. locations and the base addresses of each image must be chosen carefully such
  1290. that images don't overlap each other in an undesired way. As the code grows,
  1291. the base addresses might need adjustments to cope with the new memory layout.
  1292. The memory layout is completely specific to the platform and so there is no
  1293. general recipe for choosing the right base addresses for each bootloader image.
  1294. However, there are tools to aid in understanding the memory layout. These are
  1295. the link map files: ``build/<platform>/<build-type>/bl<x>/bl<x>.map``, with ``<x>``
  1296. being the stage bootloader. They provide a detailed view of the memory usage of
  1297. each image. Among other useful information, they provide the end address of
  1298. each image.
  1299. - ``bl1.map`` link map file provides ``__BL1_RAM_END__`` address.
  1300. - ``bl2.map`` link map file provides ``__BL2_END__`` address.
  1301. - ``bl31.map`` link map file provides ``__BL31_END__`` address.
  1302. - ``bl32.map`` link map file provides ``__BL32_END__`` address.
  1303. For each bootloader image, the platform code must provide its start address
  1304. as well as a limit address that it must not overstep. The latter is used in the
  1305. linker scripts to check that the image doesn't grow past that address. If that
  1306. happens, the linker will issue a message similar to the following:
  1307. ::
  1308. aarch64-none-elf-ld: BLx has exceeded its limit.
  1309. Additionally, if the platform memory layout implies some image overlaying like
  1310. on FVP, BL31 and TSP need to know the limit address that their PROGBITS
  1311. sections must not overstep. The platform code must provide those.
  1312. TF-A does not provide any mechanism to verify at boot time that the memory
  1313. to load a new image is free to prevent overwriting a previously loaded image.
  1314. The platform must specify the memory available in the system for all the
  1315. relevant BL images to be loaded.
  1316. For example, in the case of BL1 loading BL2, ``bl1_plat_sec_mem_layout()`` will
  1317. return the region defined by the platform where BL1 intends to load BL2. The
  1318. ``load_image()`` function performs bounds check for the image size based on the
  1319. base and maximum image size provided by the platforms. Platforms must take
  1320. this behaviour into account when defining the base/size for each of the images.
  1321. Memory layout on Arm development platforms
  1322. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1323. The following list describes the memory layout on the Arm development platforms:
  1324. - A 4KB page of shared memory is used for communication between Trusted
  1325. Firmware and the platform's power controller. This is located at the base of
  1326. Trusted SRAM. The amount of Trusted SRAM available to load the bootloader
  1327. images is reduced by the size of the shared memory.
  1328. The shared memory is used to store the CPUs' entrypoint mailbox. On Juno,
  1329. this is also used for the MHU payload when passing messages to and from the
  1330. SCP.
  1331. - Another 4 KB page is reserved for passing memory layout between BL1 and BL2
  1332. and also the dynamic firmware configurations.
  1333. - On FVP, BL1 is originally sitting in the Trusted ROM at address ``0x0``. On
  1334. Juno, BL1 resides in flash memory at address ``0x0BEC0000``. BL1 read-write
  1335. data are relocated to the top of Trusted SRAM at runtime.
  1336. - BL2 is loaded below BL1 RW
  1337. - EL3 Runtime Software, BL31 for AArch64 and BL32 for AArch32 (e.g. SP_MIN),
  1338. is loaded at the top of the Trusted SRAM, such that its NOBITS sections will
  1339. overwrite BL1 R/W data and BL2. This implies that BL1 global variables
  1340. remain valid only until execution reaches the EL3 Runtime Software entry
  1341. point during a cold boot.
  1342. - On Juno, SCP_BL2 is loaded temporarily into the EL3 Runtime Software memory
  1343. region and transferred to the SCP before being overwritten by EL3 Runtime
  1344. Software.
  1345. - BL32 (for AArch64) can be loaded in one of the following locations:
  1346. - Trusted SRAM
  1347. - Trusted DRAM (FVP only)
  1348. - Secure region of DRAM (top 16MB of DRAM configured by the TrustZone
  1349. controller)
  1350. When BL32 (for AArch64) is loaded into Trusted SRAM, it is loaded below
  1351. BL31.
  1352. The location of the BL32 image will result in different memory maps. This is
  1353. illustrated for both FVP and Juno in the following diagrams, using the TSP as
  1354. an example.
  1355. .. note::
  1356. Loading the BL32 image in TZC secured DRAM doesn't change the memory
  1357. layout of the other images in Trusted SRAM.
  1358. CONFIG section in memory layouts shown below contains:
  1359. ::
  1360. +--------------------+
  1361. |bl2_mem_params_descs|
  1362. |--------------------|
  1363. | fw_configs |
  1364. +--------------------+
  1365. ``bl2_mem_params_descs`` contains parameters passed from BL2 to next the
  1366. BL image during boot.
  1367. ``fw_configs`` includes soc_fw_config, tos_fw_config, tb_fw_config and fw_config.
  1368. **FVP with TSP in Trusted SRAM with firmware configs :**
  1369. (These diagrams only cover the AArch64 case)
  1370. ::
  1371. DRAM
  1372. 0xffffffff +----------+
  1373. : :
  1374. 0x82100000 |----------|
  1375. |HW_CONFIG |
  1376. 0x82000000 |----------| (non-secure)
  1377. | |
  1378. 0x80000000 +----------+
  1379. Trusted DRAM
  1380. 0x08000000 +----------+
  1381. |HW_CONFIG |
  1382. 0x07f00000 |----------|
  1383. : :
  1384. | |
  1385. 0x06000000 +----------+
  1386. Trusted SRAM
  1387. 0x04040000 +----------+ loaded by BL2 +----------------+
  1388. | BL1 (rw) | <<<<<<<<<<<<< | |
  1389. |----------| <<<<<<<<<<<<< | BL31 NOBITS |
  1390. | BL2 | <<<<<<<<<<<<< | |
  1391. |----------| <<<<<<<<<<<<< |----------------|
  1392. | | <<<<<<<<<<<<< | BL31 PROGBITS |
  1393. | | <<<<<<<<<<<<< |----------------|
  1394. | | <<<<<<<<<<<<< | BL32 |
  1395. 0x04003000 +----------+ +----------------+
  1396. | CONFIG |
  1397. 0x04001000 +----------+
  1398. | Shared |
  1399. 0x04000000 +----------+
  1400. Trusted ROM
  1401. 0x04000000 +----------+
  1402. | BL1 (ro) |
  1403. 0x00000000 +----------+
  1404. **FVP with TSP in Trusted DRAM with firmware configs (default option):**
  1405. ::
  1406. DRAM
  1407. 0xffffffff +--------------+
  1408. : :
  1409. 0x82100000 |--------------|
  1410. | HW_CONFIG |
  1411. 0x82000000 |--------------| (non-secure)
  1412. | |
  1413. 0x80000000 +--------------+
  1414. Trusted DRAM
  1415. 0x08000000 +--------------+
  1416. | HW_CONFIG |
  1417. 0x07f00000 |--------------|
  1418. : :
  1419. | BL32 |
  1420. 0x06000000 +--------------+
  1421. Trusted SRAM
  1422. 0x04040000 +--------------+ loaded by BL2 +----------------+
  1423. | BL1 (rw) | <<<<<<<<<<<<< | |
  1424. |--------------| <<<<<<<<<<<<< | BL31 NOBITS |
  1425. | BL2 | <<<<<<<<<<<<< | |
  1426. |--------------| <<<<<<<<<<<<< |----------------|
  1427. | | <<<<<<<<<<<<< | BL31 PROGBITS |
  1428. | | +----------------+
  1429. 0x04003000 +--------------+
  1430. | CONFIG |
  1431. 0x04001000 +--------------+
  1432. | Shared |
  1433. 0x04000000 +--------------+
  1434. Trusted ROM
  1435. 0x04000000 +--------------+
  1436. | BL1 (ro) |
  1437. 0x00000000 +--------------+
  1438. **FVP with TSP in TZC-Secured DRAM with firmware configs :**
  1439. ::
  1440. DRAM
  1441. 0xffffffff +----------+
  1442. | BL32 | (secure)
  1443. 0xff000000 +----------+
  1444. | |
  1445. 0x82100000 |----------|
  1446. |HW_CONFIG |
  1447. 0x82000000 |----------| (non-secure)
  1448. | |
  1449. 0x80000000 +----------+
  1450. Trusted DRAM
  1451. 0x08000000 +----------+
  1452. |HW_CONFIG |
  1453. 0x7f000000 |----------|
  1454. : :
  1455. | |
  1456. 0x06000000 +----------+
  1457. Trusted SRAM
  1458. 0x04040000 +----------+ loaded by BL2 +----------------+
  1459. | BL1 (rw) | <<<<<<<<<<<<< | |
  1460. |----------| <<<<<<<<<<<<< | BL31 NOBITS |
  1461. | BL2 | <<<<<<<<<<<<< | |
  1462. |----------| <<<<<<<<<<<<< |----------------|
  1463. | | <<<<<<<<<<<<< | BL31 PROGBITS |
  1464. | | +----------------+
  1465. 0x04003000 +----------+
  1466. | CONFIG |
  1467. 0x04001000 +----------+
  1468. | Shared |
  1469. 0x04000000 +----------+
  1470. Trusted ROM
  1471. 0x04000000 +----------+
  1472. | BL1 (ro) |
  1473. 0x00000000 +----------+
  1474. **Juno with BL32 in Trusted SRAM :**
  1475. ::
  1476. Flash0
  1477. 0x0C000000 +----------+
  1478. : :
  1479. 0x0BED0000 |----------|
  1480. | BL1 (ro) |
  1481. 0x0BEC0000 |----------|
  1482. : :
  1483. 0x08000000 +----------+ BL31 is loaded
  1484. after SCP_BL2 has
  1485. Trusted SRAM been sent to SCP
  1486. 0x04040000 +----------+ loaded by BL2 +----------------+
  1487. | BL1 (rw) | <<<<<<<<<<<<< | |
  1488. |----------| <<<<<<<<<<<<< | BL31 NOBITS |
  1489. | BL2 | <<<<<<<<<<<<< | |
  1490. |----------| <<<<<<<<<<<<< |----------------|
  1491. | SCP_BL2 | <<<<<<<<<<<<< | BL31 PROGBITS |
  1492. | | <<<<<<<<<<<<< |----------------|
  1493. | | <<<<<<<<<<<<< | BL32 |
  1494. | | +----------------+
  1495. | |
  1496. 0x04001000 +----------+
  1497. | MHU |
  1498. 0x04000000 +----------+
  1499. **Juno with BL32 in TZC-secured DRAM :**
  1500. ::
  1501. DRAM
  1502. 0xFFE00000 +----------+
  1503. | BL32 | (secure)
  1504. 0xFF000000 |----------|
  1505. | |
  1506. : : (non-secure)
  1507. | |
  1508. 0x80000000 +----------+
  1509. Flash0
  1510. 0x0C000000 +----------+
  1511. : :
  1512. 0x0BED0000 |----------|
  1513. | BL1 (ro) |
  1514. 0x0BEC0000 |----------|
  1515. : :
  1516. 0x08000000 +----------+ BL31 is loaded
  1517. after SCP_BL2 has
  1518. Trusted SRAM been sent to SCP
  1519. 0x04040000 +----------+ loaded by BL2 +----------------+
  1520. | BL1 (rw) | <<<<<<<<<<<<< | |
  1521. |----------| <<<<<<<<<<<<< | BL31 NOBITS |
  1522. | BL2 | <<<<<<<<<<<<< | |
  1523. |----------| <<<<<<<<<<<<< |----------------|
  1524. | SCP_BL2 | <<<<<<<<<<<<< | BL31 PROGBITS |
  1525. | | +----------------+
  1526. 0x04001000 +----------+
  1527. | MHU |
  1528. 0x04000000 +----------+
  1529. .. _firmware_design_fip:
  1530. Firmware Image Package (FIP)
  1531. ----------------------------
  1532. Using a Firmware Image Package (FIP) allows for packing bootloader images (and
  1533. potentially other payloads) into a single archive that can be loaded by TF-A
  1534. from non-volatile platform storage. A driver to load images from a FIP has
  1535. been added to the storage layer and allows a package to be read from supported
  1536. platform storage. A tool to create Firmware Image Packages is also provided
  1537. and described below.
  1538. Firmware Image Package layout
  1539. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1540. The FIP layout consists of a table of contents (ToC) followed by payload data.
  1541. The ToC itself has a header followed by one or more table entries. The ToC is
  1542. terminated by an end marker entry, and since the size of the ToC is 0 bytes,
  1543. the offset equals the total size of the FIP file. All ToC entries describe some
  1544. payload data that has been appended to the end of the binary package. With the
  1545. information provided in the ToC entry the corresponding payload data can be
  1546. retrieved.
  1547. ::
  1548. ------------------
  1549. | ToC Header |
  1550. |----------------|
  1551. | ToC Entry 0 |
  1552. |----------------|
  1553. | ToC Entry 1 |
  1554. |----------------|
  1555. | ToC End Marker |
  1556. |----------------|
  1557. | |
  1558. | Data 0 |
  1559. | |
  1560. |----------------|
  1561. | |
  1562. | Data 1 |
  1563. | |
  1564. ------------------
  1565. The ToC header and entry formats are described in the header file
  1566. ``include/tools_share/firmware_image_package.h``. This file is used by both the
  1567. tool and TF-A.
  1568. The ToC header has the following fields:
  1569. ::
  1570. `name`: The name of the ToC. This is currently used to validate the header.
  1571. `serial_number`: A non-zero number provided by the creation tool
  1572. `flags`: Flags associated with this data.
  1573. Bits 0-31: Reserved
  1574. Bits 32-47: Platform defined
  1575. Bits 48-63: Reserved
  1576. A ToC entry has the following fields:
  1577. ::
  1578. `uuid`: All files are referred to by a pre-defined Universally Unique
  1579. IDentifier [UUID] . The UUIDs are defined in
  1580. `include/tools_share/firmware_image_package.h`. The platform translates
  1581. the requested image name into the corresponding UUID when accessing the
  1582. package.
  1583. `offset_address`: The offset address at which the corresponding payload data
  1584. can be found. The offset is calculated from the ToC base address.
  1585. `size`: The size of the corresponding payload data in bytes.
  1586. `flags`: Flags associated with this entry. None are yet defined.
  1587. Firmware Image Package creation tool
  1588. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1589. The FIP creation tool can be used to pack specified images into a binary
  1590. package that can be loaded by TF-A from platform storage. The tool currently
  1591. only supports packing bootloader images. Additional image definitions can be
  1592. added to the tool as required.
  1593. The tool can be found in ``tools/fiptool``.
  1594. Loading from a Firmware Image Package (FIP)
  1595. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1596. The Firmware Image Package (FIP) driver can load images from a binary package on
  1597. non-volatile platform storage. For the Arm development platforms, this is
  1598. currently NOR FLASH.
  1599. Bootloader images are loaded according to the platform policy as specified by
  1600. the function ``plat_get_image_source()``. For the Arm development platforms, this
  1601. means the platform will attempt to load images from a Firmware Image Package
  1602. located at the start of NOR FLASH0.
  1603. The Arm development platforms' policy is to only allow loading of a known set of
  1604. images. The platform policy can be modified to allow additional images.
  1605. Use of coherent memory in TF-A
  1606. ------------------------------
  1607. There might be loss of coherency when physical memory with mismatched
  1608. shareability, cacheability and memory attributes is accessed by multiple CPUs
  1609. (refer to section B2.9 of `Arm ARM`_ for more details). This possibility occurs
  1610. in TF-A during power up/down sequences when coherency, MMU and caches are
  1611. turned on/off incrementally.
  1612. TF-A defines coherent memory as a region of memory with Device nGnRE attributes
  1613. in the translation tables. The translation granule size in TF-A is 4KB. This
  1614. is the smallest possible size of the coherent memory region.
  1615. By default, all data structures which are susceptible to accesses with
  1616. mismatched attributes from various CPUs are allocated in a coherent memory
  1617. region (refer to section 2.1 of :ref:`Porting Guide`). The coherent memory
  1618. region accesses are Outer Shareable, non-cacheable and they can be accessed with
  1619. the Device nGnRE attributes when the MMU is turned on. Hence, at the expense of
  1620. at least an extra page of memory, TF-A is able to work around coherency issues
  1621. due to mismatched memory attributes.
  1622. The alternative to the above approach is to allocate the susceptible data
  1623. structures in Normal WriteBack WriteAllocate Inner shareable memory. This
  1624. approach requires the data structures to be designed so that it is possible to
  1625. work around the issue of mismatched memory attributes by performing software
  1626. cache maintenance on them.
  1627. Disabling the use of coherent memory in TF-A
  1628. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1629. It might be desirable to avoid the cost of allocating coherent memory on
  1630. platforms which are memory constrained. TF-A enables inclusion of coherent
  1631. memory in firmware images through the build flag ``USE_COHERENT_MEM``.
  1632. This flag is enabled by default. It can be disabled to choose the second
  1633. approach described above.
  1634. The below sections analyze the data structures allocated in the coherent memory
  1635. region and the changes required to allocate them in normal memory.
  1636. Coherent memory usage in PSCI implementation
  1637. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1638. The ``psci_non_cpu_pd_nodes`` data structure stores the platform's power domain
  1639. tree information for state management of power domains. By default, this data
  1640. structure is allocated in the coherent memory region in TF-A because it can be
  1641. accessed by multiple CPUs, either with caches enabled or disabled.
  1642. .. code:: c
  1643. typedef struct non_cpu_pwr_domain_node {
  1644. /*
  1645. * Index of the first CPU power domain node level 0 which has this node
  1646. * as its parent.
  1647. */
  1648. unsigned int cpu_start_idx;
  1649. /*
  1650. * Number of CPU power domains which are siblings of the domain indexed
  1651. * by 'cpu_start_idx' i.e. all the domains in the range 'cpu_start_idx
  1652. * -> cpu_start_idx + ncpus' have this node as their parent.
  1653. */
  1654. unsigned int ncpus;
  1655. /*
  1656. * Index of the parent power domain node.
  1657. */
  1658. unsigned int parent_node;
  1659. plat_local_state_t local_state;
  1660. unsigned char level;
  1661. /* For indexing the psci_lock array*/
  1662. unsigned char lock_index;
  1663. } non_cpu_pd_node_t;
  1664. In order to move this data structure to normal memory, the use of each of its
  1665. fields must be analyzed. Fields like ``cpu_start_idx``, ``ncpus``, ``parent_node``
  1666. ``level`` and ``lock_index`` are only written once during cold boot. Hence removing
  1667. them from coherent memory involves only doing a clean and invalidate of the
  1668. cache lines after these fields are written.
  1669. The field ``local_state`` can be concurrently accessed by multiple CPUs in
  1670. different cache states. A Lamport's Bakery lock ``psci_locks`` is used to ensure
  1671. mutual exclusion to this field and a clean and invalidate is needed after it
  1672. is written.
  1673. Bakery lock data
  1674. ~~~~~~~~~~~~~~~~
  1675. The bakery lock data structure ``bakery_lock_t`` is allocated in coherent memory
  1676. and is accessed by multiple CPUs with mismatched attributes. ``bakery_lock_t`` is
  1677. defined as follows:
  1678. .. code:: c
  1679. typedef struct bakery_lock {
  1680. /*
  1681. * The lock_data is a bit-field of 2 members:
  1682. * Bit[0] : choosing. This field is set when the CPU is
  1683. * choosing its bakery number.
  1684. * Bits[1 - 15] : number. This is the bakery number allocated.
  1685. */
  1686. volatile uint16_t lock_data[BAKERY_LOCK_MAX_CPUS];
  1687. } bakery_lock_t;
  1688. It is a characteristic of Lamport's Bakery algorithm that the volatile per-CPU
  1689. fields can be read by all CPUs but only written to by the owning CPU.
  1690. Depending upon the data cache line size, the per-CPU fields of the
  1691. ``bakery_lock_t`` structure for multiple CPUs may exist on a single cache line.
  1692. These per-CPU fields can be read and written during lock contention by multiple
  1693. CPUs with mismatched memory attributes. Since these fields are a part of the
  1694. lock implementation, they do not have access to any other locking primitive to
  1695. safeguard against the resulting coherency issues. As a result, simple software
  1696. cache maintenance is not enough to allocate them in coherent memory. Consider
  1697. the following example.
  1698. CPU0 updates its per-CPU field with data cache enabled. This write updates a
  1699. local cache line which contains a copy of the fields for other CPUs as well. Now
  1700. CPU1 updates its per-CPU field of the ``bakery_lock_t`` structure with data cache
  1701. disabled. CPU1 then issues a DCIVAC operation to invalidate any stale copies of
  1702. its field in any other cache line in the system. This operation will invalidate
  1703. the update made by CPU0 as well.
  1704. To use bakery locks when ``USE_COHERENT_MEM`` is disabled, the lock data structure
  1705. has been redesigned. The changes utilise the characteristic of Lamport's Bakery
  1706. algorithm mentioned earlier. The bakery_lock structure only allocates the memory
  1707. for a single CPU. The macro ``DEFINE_BAKERY_LOCK`` allocates all the bakery locks
  1708. needed for a CPU into a section ``.bakery_lock``. The linker allocates the memory
  1709. for other cores by using the total size allocated for the bakery_lock section
  1710. and multiplying it with (PLATFORM_CORE_COUNT - 1). This enables software to
  1711. perform software cache maintenance on the lock data structure without running
  1712. into coherency issues associated with mismatched attributes.
  1713. The bakery lock data structure ``bakery_info_t`` is defined for use when
  1714. ``USE_COHERENT_MEM`` is disabled as follows:
  1715. .. code:: c
  1716. typedef struct bakery_info {
  1717. /*
  1718. * The lock_data is a bit-field of 2 members:
  1719. * Bit[0] : choosing. This field is set when the CPU is
  1720. * choosing its bakery number.
  1721. * Bits[1 - 15] : number. This is the bakery number allocated.
  1722. */
  1723. volatile uint16_t lock_data;
  1724. } bakery_info_t;
  1725. The ``bakery_info_t`` represents a single per-CPU field of one lock and
  1726. the combination of corresponding ``bakery_info_t`` structures for all CPUs in the
  1727. system represents the complete bakery lock. The view in memory for a system
  1728. with n bakery locks are:
  1729. ::
  1730. .bakery_lock section start
  1731. |----------------|
  1732. | `bakery_info_t`| <-- Lock_0 per-CPU field
  1733. | Lock_0 | for CPU0
  1734. |----------------|
  1735. | `bakery_info_t`| <-- Lock_1 per-CPU field
  1736. | Lock_1 | for CPU0
  1737. |----------------|
  1738. | .... |
  1739. |----------------|
  1740. | `bakery_info_t`| <-- Lock_N per-CPU field
  1741. | Lock_N | for CPU0
  1742. ------------------
  1743. | XXXXX |
  1744. | Padding to |
  1745. | next Cache WB | <--- Calculate PERCPU_BAKERY_LOCK_SIZE, allocate
  1746. | Granule | continuous memory for remaining CPUs.
  1747. ------------------
  1748. | `bakery_info_t`| <-- Lock_0 per-CPU field
  1749. | Lock_0 | for CPU1
  1750. |----------------|
  1751. | `bakery_info_t`| <-- Lock_1 per-CPU field
  1752. | Lock_1 | for CPU1
  1753. |----------------|
  1754. | .... |
  1755. |----------------|
  1756. | `bakery_info_t`| <-- Lock_N per-CPU field
  1757. | Lock_N | for CPU1
  1758. ------------------
  1759. | XXXXX |
  1760. | Padding to |
  1761. | next Cache WB |
  1762. | Granule |
  1763. ------------------
  1764. Consider a system of 2 CPUs with 'N' bakery locks as shown above. For an
  1765. operation on Lock_N, the corresponding ``bakery_info_t`` in both CPU0 and CPU1
  1766. ``.bakery_lock`` section need to be fetched and appropriate cache operations need
  1767. to be performed for each access.
  1768. On Arm Platforms, bakery locks are used in psci (``psci_locks``) and power controller
  1769. driver (``arm_lock``).
  1770. Non Functional Impact of removing coherent memory
  1771. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1772. Removal of the coherent memory region leads to the additional software overhead
  1773. of performing cache maintenance for the affected data structures. However, since
  1774. the memory where the data structures are allocated is cacheable, the overhead is
  1775. mostly mitigated by an increase in performance.
  1776. There is however a performance impact for bakery locks, due to:
  1777. - Additional cache maintenance operations, and
  1778. - Multiple cache line reads for each lock operation, since the bakery locks
  1779. for each CPU are distributed across different cache lines.
  1780. The implementation has been optimized to minimize this additional overhead.
  1781. Measurements indicate that when bakery locks are allocated in Normal memory, the
  1782. minimum latency of acquiring a lock is on an average 3-4 micro seconds whereas
  1783. in Device memory the same is 2 micro seconds. The measurements were done on the
  1784. Juno Arm development platform.
  1785. As mentioned earlier, almost a page of memory can be saved by disabling
  1786. ``USE_COHERENT_MEM``. Each platform needs to consider these trade-offs to decide
  1787. whether coherent memory should be used. If a platform disables
  1788. ``USE_COHERENT_MEM`` and needs to use bakery locks in the porting layer, it can
  1789. optionally define macro ``PLAT_PERCPU_BAKERY_LOCK_SIZE`` (see the
  1790. :ref:`Porting Guide`). Refer to the reference platform code for examples.
  1791. Isolating code and read-only data on separate memory pages
  1792. ----------------------------------------------------------
  1793. In the Armv8-A VMSA, translation table entries include fields that define the
  1794. properties of the target memory region, such as its access permissions. The
  1795. smallest unit of memory that can be addressed by a translation table entry is
  1796. a memory page. Therefore, if software needs to set different permissions on two
  1797. memory regions then it needs to map them using different memory pages.
  1798. The default memory layout for each BL image is as follows:
  1799. ::
  1800. | ... |
  1801. +-------------------+
  1802. | Read-write data |
  1803. +-------------------+ Page boundary
  1804. | <Padding> |
  1805. +-------------------+
  1806. | Exception vectors |
  1807. +-------------------+ 2 KB boundary
  1808. | <Padding> |
  1809. +-------------------+
  1810. | Read-only data |
  1811. +-------------------+
  1812. | Code |
  1813. +-------------------+ BLx_BASE
  1814. .. note::
  1815. The 2KB alignment for the exception vectors is an architectural
  1816. requirement.
  1817. The read-write data start on a new memory page so that they can be mapped with
  1818. read-write permissions, whereas the code and read-only data below are configured
  1819. as read-only.
  1820. However, the read-only data are not aligned on a page boundary. They are
  1821. contiguous to the code. Therefore, the end of the code section and the beginning
  1822. of the read-only data one might share a memory page. This forces both to be
  1823. mapped with the same memory attributes. As the code needs to be executable, this
  1824. means that the read-only data stored on the same memory page as the code are
  1825. executable as well. This could potentially be exploited as part of a security
  1826. attack.
  1827. TF provides the build flag ``SEPARATE_CODE_AND_RODATA`` to isolate the code and
  1828. read-only data on separate memory pages. This in turn allows independent control
  1829. of the access permissions for the code and read-only data. In this case,
  1830. platform code gets a finer-grained view of the image layout and can
  1831. appropriately map the code region as executable and the read-only data as
  1832. execute-never.
  1833. This has an impact on memory footprint, as padding bytes need to be introduced
  1834. between the code and read-only data to ensure the segregation of the two. To
  1835. limit the memory cost, this flag also changes the memory layout such that the
  1836. code and exception vectors are now contiguous, like so:
  1837. ::
  1838. | ... |
  1839. +-------------------+
  1840. | Read-write data |
  1841. +-------------------+ Page boundary
  1842. | <Padding> |
  1843. +-------------------+
  1844. | Read-only data |
  1845. +-------------------+ Page boundary
  1846. | <Padding> |
  1847. +-------------------+
  1848. | Exception vectors |
  1849. +-------------------+ 2 KB boundary
  1850. | <Padding> |
  1851. +-------------------+
  1852. | Code |
  1853. +-------------------+ BLx_BASE
  1854. With this more condensed memory layout, the separation of read-only data will
  1855. add zero or one page to the memory footprint of each BL image. Each platform
  1856. should consider the trade-off between memory footprint and security.
  1857. This build flag is disabled by default, minimising memory footprint. On Arm
  1858. platforms, it is enabled.
  1859. Publish and Subscribe Framework
  1860. -------------------------------
  1861. The Publish and Subscribe Framework allows EL3 components to define and publish
  1862. events, to which other EL3 components can subscribe.
  1863. The following macros are provided by the framework:
  1864. - ``REGISTER_PUBSUB_EVENT(event)``: Defines an event, and takes one argument,
  1865. the event name, which must be a valid C identifier. All calls to
  1866. ``REGISTER_PUBSUB_EVENT`` macro must be placed in the file
  1867. ``pubsub_events.h``.
  1868. - ``PUBLISH_EVENT_ARG(event, arg)``: Publishes a defined event, by iterating
  1869. subscribed handlers and calling them in turn. The handlers will be passed the
  1870. parameter ``arg``. The expected use-case is to broadcast an event.
  1871. - ``PUBLISH_EVENT(event)``: Like ``PUBLISH_EVENT_ARG``, except that the value
  1872. ``NULL`` is passed to subscribed handlers.
  1873. - ``SUBSCRIBE_TO_EVENT(event, handler)``: Registers the ``handler`` to
  1874. subscribe to ``event``. The handler will be executed whenever the ``event``
  1875. is published.
  1876. - ``for_each_subscriber(event, subscriber)``: Iterates through all handlers
  1877. subscribed for ``event``. ``subscriber`` must be a local variable of type
  1878. ``pubsub_cb_t *``, and will point to each subscribed handler in turn during
  1879. iteration. This macro can be used for those patterns that none of the
  1880. ``PUBLISH_EVENT_*()`` macros cover.
  1881. Publishing an event that wasn't defined using ``REGISTER_PUBSUB_EVENT`` will
  1882. result in build error. Subscribing to an undefined event however won't.
  1883. Subscribed handlers must be of type ``pubsub_cb_t``, with following function
  1884. signature:
  1885. .. code:: c
  1886. typedef void* (*pubsub_cb_t)(const void *arg);
  1887. There may be arbitrary number of handlers registered to the same event. The
  1888. order in which subscribed handlers are notified when that event is published is
  1889. not defined. Subscribed handlers may be executed in any order; handlers should
  1890. not assume any relative ordering amongst them.
  1891. Publishing an event on a PE will result in subscribed handlers executing on that
  1892. PE only; it won't cause handlers to execute on a different PE.
  1893. Note that publishing an event on a PE blocks until all the subscribed handlers
  1894. finish executing on the PE.
  1895. TF-A generic code publishes and subscribes to some events within. Platform
  1896. ports are discouraged from subscribing to them. These events may be withdrawn,
  1897. renamed, or have their semantics altered in the future. Platforms may however
  1898. register, publish, and subscribe to platform-specific events.
  1899. Publish and Subscribe Example
  1900. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1901. A publisher that wants to publish event ``foo`` would:
  1902. - Define the event ``foo`` in the ``pubsub_events.h``.
  1903. .. code:: c
  1904. REGISTER_PUBSUB_EVENT(foo);
  1905. - Depending on the nature of event, use one of ``PUBLISH_EVENT_*()`` macros to
  1906. publish the event at the appropriate path and time of execution.
  1907. A subscriber that wants to subscribe to event ``foo`` published above would
  1908. implement:
  1909. .. code:: c
  1910. void *foo_handler(const void *arg)
  1911. {
  1912. void *result;
  1913. /* Do handling ... */
  1914. return result;
  1915. }
  1916. SUBSCRIBE_TO_EVENT(foo, foo_handler);
  1917. Reclaiming the BL31 initialization code
  1918. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1919. A significant amount of the code used for the initialization of BL31 is never
  1920. needed again after boot time. In order to reduce the runtime memory
  1921. footprint, the memory used for this code can be reclaimed after initialization
  1922. has finished and be used for runtime data.
  1923. The build option ``RECLAIM_INIT_CODE`` can be set to mark this boot time code
  1924. with a ``.text.init.*`` attribute which can be filtered and placed suitably
  1925. within the BL image for later reclamation by the platform. The platform can
  1926. specify the filter and the memory region for this init section in BL31 via the
  1927. plat.ld.S linker script. For example, on the FVP, this section is placed
  1928. overlapping the secondary CPU stacks so that after the cold boot is done, this
  1929. memory can be reclaimed for the stacks. The init memory section is initially
  1930. mapped with ``RO``, ``EXECUTE`` attributes. After BL31 initialization has
  1931. completed, the FVP changes the attributes of this section to ``RW``,
  1932. ``EXECUTE_NEVER`` allowing it to be used for runtime data. The memory attributes
  1933. are changed within the ``bl31_plat_runtime_setup`` platform hook. The init
  1934. section section can be reclaimed for any data which is accessed after cold
  1935. boot initialization and it is upto the platform to make the decision.
  1936. .. _firmware_design_pmf:
  1937. Performance Measurement Framework
  1938. ---------------------------------
  1939. The Performance Measurement Framework (PMF) facilitates collection of
  1940. timestamps by registered services and provides interfaces to retrieve them
  1941. from within TF-A. A platform can choose to expose appropriate SMCs to
  1942. retrieve these collected timestamps.
  1943. By default, the global physical counter is used for the timestamp
  1944. value and is read via ``CNTPCT_EL0``. The framework allows to retrieve
  1945. timestamps captured by other CPUs.
  1946. Timestamp identifier format
  1947. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1948. A PMF timestamp is uniquely identified across the system via the
  1949. timestamp ID or ``tid``. The ``tid`` is composed as follows:
  1950. ::
  1951. Bits 0-7: The local timestamp identifier.
  1952. Bits 8-9: Reserved.
  1953. Bits 10-15: The service identifier.
  1954. Bits 16-31: Reserved.
  1955. #. The service identifier. Each PMF service is identified by a
  1956. service name and a service identifier. Both the service name and
  1957. identifier are unique within the system as a whole.
  1958. #. The local timestamp identifier. This identifier is unique within a given
  1959. service.
  1960. Registering a PMF service
  1961. ~~~~~~~~~~~~~~~~~~~~~~~~~
  1962. To register a PMF service, the ``PMF_REGISTER_SERVICE()`` macro from ``pmf.h``
  1963. is used. The arguments required are the service name, the service ID,
  1964. the total number of local timestamps to be captured and a set of flags.
  1965. The ``flags`` field can be specified as a bitwise-OR of the following values:
  1966. ::
  1967. PMF_STORE_ENABLE: The timestamp is stored in memory for later retrieval.
  1968. PMF_DUMP_ENABLE: The timestamp is dumped on the serial console.
  1969. The ``PMF_REGISTER_SERVICE()`` reserves memory to store captured
  1970. timestamps in a PMF specific linker section at build time.
  1971. Additionally, it defines necessary functions to capture and
  1972. retrieve a particular timestamp for the given service at runtime.
  1973. The macro ``PMF_REGISTER_SERVICE()`` only enables capturing PMF timestamps
  1974. from within TF-A. In order to retrieve timestamps from outside of TF-A, the
  1975. ``PMF_REGISTER_SERVICE_SMC()`` macro must be used instead. This macro
  1976. accepts the same set of arguments as the ``PMF_REGISTER_SERVICE()``
  1977. macro but additionally supports retrieving timestamps using SMCs.
  1978. Capturing a timestamp
  1979. ~~~~~~~~~~~~~~~~~~~~~
  1980. PMF timestamps are stored in a per-service timestamp region. On a
  1981. system with multiple CPUs, each timestamp is captured and stored
  1982. in a per-CPU cache line aligned memory region.
  1983. Having registered the service, the ``PMF_CAPTURE_TIMESTAMP()`` macro can be
  1984. used to capture a timestamp at the location where it is used. The macro
  1985. takes the service name, a local timestamp identifier and a flag as arguments.
  1986. The ``flags`` field argument can be zero, or ``PMF_CACHE_MAINT`` which
  1987. instructs PMF to do cache maintenance following the capture. Cache
  1988. maintenance is required if any of the service's timestamps are captured
  1989. with data cache disabled.
  1990. To capture a timestamp in assembly code, the caller should use
  1991. ``pmf_calc_timestamp_addr`` macro (defined in ``pmf_asm_macros.S``) to
  1992. calculate the address of where the timestamp would be stored. The
  1993. caller should then read ``CNTPCT_EL0`` register to obtain the timestamp
  1994. and store it at the determined address for later retrieval.
  1995. Retrieving a timestamp
  1996. ~~~~~~~~~~~~~~~~~~~~~~
  1997. From within TF-A, timestamps for individual CPUs can be retrieved using either
  1998. ``PMF_GET_TIMESTAMP_BY_MPIDR()`` or ``PMF_GET_TIMESTAMP_BY_INDEX()`` macros.
  1999. These macros accept the CPU's MPIDR value, or its ordinal position
  2000. respectively.
  2001. From outside TF-A, timestamps for individual CPUs can be retrieved by calling
  2002. into ``pmf_smc_handler()``.
  2003. ::
  2004. Interface : pmf_smc_handler()
  2005. Argument : unsigned int smc_fid, u_register_t x1,
  2006. u_register_t x2, u_register_t x3,
  2007. u_register_t x4, void *cookie,
  2008. void *handle, u_register_t flags
  2009. Return : uintptr_t
  2010. smc_fid: Holds the SMC identifier which is either `PMF_SMC_GET_TIMESTAMP_32`
  2011. when the caller of the SMC is running in AArch32 mode
  2012. or `PMF_SMC_GET_TIMESTAMP_64` when the caller is running in AArch64 mode.
  2013. x1: Timestamp identifier.
  2014. x2: The `mpidr` of the CPU for which the timestamp has to be retrieved.
  2015. This can be the `mpidr` of a different core to the one initiating
  2016. the SMC. In that case, service specific cache maintenance may be
  2017. required to ensure the updated copy of the timestamp is returned.
  2018. x3: A flags value that is either 0 or `PMF_CACHE_MAINT`. If
  2019. `PMF_CACHE_MAINT` is passed, then the PMF code will perform a
  2020. cache invalidate before reading the timestamp. This ensures
  2021. an updated copy is returned.
  2022. The remaining arguments, ``x4``, ``cookie``, ``handle`` and ``flags`` are unused
  2023. in this implementation.
  2024. PMF code structure
  2025. ~~~~~~~~~~~~~~~~~~
  2026. #. ``pmf_main.c`` consists of core functions that implement service registration,
  2027. initialization, storing, dumping and retrieving timestamps.
  2028. #. ``pmf_smc.c`` contains the SMC handling for registered PMF services.
  2029. #. ``pmf.h`` contains the public interface to Performance Measurement Framework.
  2030. #. ``pmf_asm_macros.S`` consists of macros to facilitate capturing timestamps in
  2031. assembly code.
  2032. #. ``pmf_helpers.h`` is an internal header used by ``pmf.h``.
  2033. Armv8-A Architecture Extensions
  2034. -------------------------------
  2035. TF-A makes use of Armv8-A Architecture Extensions where applicable. This
  2036. section lists the usage of Architecture Extensions, and build flags
  2037. controlling them.
  2038. In general, and unless individually mentioned, the build options
  2039. ``ARM_ARCH_MAJOR`` and ``ARM_ARCH_MINOR`` select the Architecture Extension to
  2040. target when building TF-A. Subsequent Arm Architecture Extensions are backward
  2041. compatible with previous versions.
  2042. The build system only requires that ``ARM_ARCH_MAJOR`` and ``ARM_ARCH_MINOR`` have a
  2043. valid numeric value. These build options only control whether or not
  2044. Architecture Extension-specific code is included in the build. Otherwise, TF-A
  2045. targets the base Armv8.0-A architecture; i.e. as if ``ARM_ARCH_MAJOR`` == 8
  2046. and ``ARM_ARCH_MINOR`` == 0, which are also their respective default values.
  2047. .. seealso:: :ref:`Build Options`
  2048. For details on the Architecture Extension and available features, please refer
  2049. to the respective Architecture Extension Supplement.
  2050. Armv8.1-A
  2051. ~~~~~~~~~
  2052. This Architecture Extension is targeted when ``ARM_ARCH_MAJOR`` >= 8, or when
  2053. ``ARM_ARCH_MAJOR`` == 8 and ``ARM_ARCH_MINOR`` >= 1.
  2054. - By default, a load-/store-exclusive instruction pair is used to implement
  2055. spinlocks. The ``USE_SPINLOCK_CAS`` build option when set to 1 selects the
  2056. spinlock implementation using the ARMv8.1-LSE Compare and Swap instruction.
  2057. Notice this instruction is only available in AArch64 execution state, so
  2058. the option is only available to AArch64 builds.
  2059. Armv8.2-A
  2060. ~~~~~~~~~
  2061. - The presence of ARMv8.2-TTCNP is detected at runtime. When it is present, the
  2062. Common not Private (TTBRn_ELx.CnP) bit is enabled to indicate that multiple
  2063. Processing Elements in the same Inner Shareable domain use the same
  2064. translation table entries for a given stage of translation for a particular
  2065. translation regime.
  2066. Armv8.3-A
  2067. ~~~~~~~~~
  2068. - Pointer authentication features of Armv8.3-A are unconditionally enabled in
  2069. the Non-secure world so that lower ELs are allowed to use them without
  2070. causing a trap to EL3.
  2071. In order to enable the Secure world to use it, ``CTX_INCLUDE_PAUTH_REGS``
  2072. must be set to 1. This will add all pointer authentication system registers
  2073. to the context that is saved when doing a world switch.
  2074. The TF-A itself has support for pointer authentication at runtime
  2075. that can be enabled by setting ``BRANCH_PROTECTION`` option to non-zero and
  2076. ``CTX_INCLUDE_PAUTH_REGS`` to 1. This enables pointer authentication in BL1,
  2077. BL2, BL31, and the TSP if it is used.
  2078. Note that Pointer Authentication is enabled for Non-secure world irrespective
  2079. of the value of these build flags if the CPU supports it.
  2080. If ``ARM_ARCH_MAJOR == 8`` and ``ARM_ARCH_MINOR >= 3`` the code footprint of
  2081. enabling PAuth is lower because the compiler will use the optimized
  2082. PAuth instructions rather than the backwards-compatible ones.
  2083. Armv8.5-A
  2084. ~~~~~~~~~
  2085. - Branch Target Identification feature is selected by ``BRANCH_PROTECTION``
  2086. option set to 1. This option defaults to 0.
  2087. - Memory Tagging Extension feature is unconditionally enabled for both worlds
  2088. (at EL0 and S-EL0) if it is only supported at EL0. If instead it is
  2089. implemented at all ELs, it is unconditionally enabled for only the normal
  2090. world. To enable it for the secure world as well, the build option
  2091. ``CTX_INCLUDE_MTE_REGS`` is required. If the hardware does not implement
  2092. MTE support at all, it is always disabled, no matter what build options
  2093. are used.
  2094. Armv7-A
  2095. ~~~~~~~
  2096. This Architecture Extension is targeted when ``ARM_ARCH_MAJOR`` == 7.
  2097. There are several Armv7-A extensions available. Obviously the TrustZone
  2098. extension is mandatory to support the TF-A bootloader and runtime services.
  2099. Platform implementing an Armv7-A system can to define from its target
  2100. Cortex-A architecture through ``ARM_CORTEX_A<X> = yes`` in their
  2101. ``platform.mk`` script. For example ``ARM_CORTEX_A15=yes`` for a
  2102. Cortex-A15 target.
  2103. Platform can also set ``ARM_WITH_NEON=yes`` to enable neon support.
  2104. Note that using neon at runtime has constraints on non secure world context.
  2105. TF-A does not yet provide VFP context management.
  2106. Directive ``ARM_CORTEX_A<x>`` and ``ARM_WITH_NEON`` are used to set
  2107. the toolchain target architecture directive.
  2108. Platform may choose to not define straight the toolchain target architecture
  2109. directive by defining ``MARCH32_DIRECTIVE``.
  2110. I.e:
  2111. .. code:: make
  2112. MARCH32_DIRECTIVE := -mach=armv7-a
  2113. Code Structure
  2114. --------------
  2115. TF-A code is logically divided between the three boot loader stages mentioned
  2116. in the previous sections. The code is also divided into the following
  2117. categories (present as directories in the source code):
  2118. - **Platform specific.** Choice of architecture specific code depends upon
  2119. the platform.
  2120. - **Common code.** This is platform and architecture agnostic code.
  2121. - **Library code.** This code comprises of functionality commonly used by all
  2122. other code. The PSCI implementation and other EL3 runtime frameworks reside
  2123. as Library components.
  2124. - **Stage specific.** Code specific to a boot stage.
  2125. - **Drivers.**
  2126. - **Services.** EL3 runtime services (eg: SPD). Specific SPD services
  2127. reside in the ``services/spd`` directory (e.g. ``services/spd/tspd``).
  2128. Each boot loader stage uses code from one or more of the above mentioned
  2129. categories. Based upon the above, the code layout looks like this:
  2130. ::
  2131. Directory Used by BL1? Used by BL2? Used by BL31?
  2132. bl1 Yes No No
  2133. bl2 No Yes No
  2134. bl31 No No Yes
  2135. plat Yes Yes Yes
  2136. drivers Yes No Yes
  2137. common Yes Yes Yes
  2138. lib Yes Yes Yes
  2139. services No No Yes
  2140. The build system provides a non configurable build option IMAGE_BLx for each
  2141. boot loader stage (where x = BL stage). e.g. for BL1 , IMAGE_BL1 will be
  2142. defined by the build system. This enables TF-A to compile certain code only
  2143. for specific boot loader stages
  2144. All assembler files have the ``.S`` extension. The linker source files for each
  2145. boot stage have the extension ``.ld.S``. These are processed by GCC to create the
  2146. linker scripts which have the extension ``.ld``.
  2147. FDTs provide a description of the hardware platform and are used by the Linux
  2148. kernel at boot time. These can be found in the ``fdts`` directory.
  2149. .. rubric:: References
  2150. - `Trusted Board Boot Requirements CLIENT (TBBR-CLIENT) Armv8-A (ARM DEN0006D)`_
  2151. - `Power State Coordination Interface PDD`_
  2152. - `SMC Calling Convention`_
  2153. - :ref:`Interrupt Management Framework`
  2154. --------------
  2155. *Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.*
  2156. .. _Power State Coordination Interface PDD: http://infocenter.arm.com/help/topic/com.arm.doc.den0022d/Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
  2157. .. _SMCCC: https://developer.arm.com/docs/den0028/latest
  2158. .. _PSCI: http://infocenter.arm.com/help/topic/com.arm.doc.den0022d/Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
  2159. .. _Power State Coordination Interface PDD: http://infocenter.arm.com/help/topic/com.arm.doc.den0022d/Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
  2160. .. _Arm ARM: https://developer.arm.com/docs/ddi0487/latest
  2161. .. _SMC Calling Convention: https://developer.arm.com/docs/den0028/latest
  2162. .. _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
  2163. .. _Arm Confidential Compute Architecture (Arm CCA): https://www.arm.com/why-arm/architecture/security-features/arm-confidential-compute-architecture
  2164. .. |Image 1| image:: ../resources/diagrams/rt-svc-descs-layout.png