xref: /aosp_15_r20/external/arm-trusted-firmware/docs/getting_started/psci-lib-integration-guide.rst (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong ParkPSCI Library Integration guide for Armv8-A AArch32 systems
2*54fd6939SJiyong Park==========================================================
3*54fd6939SJiyong Park
4*54fd6939SJiyong ParkThis document describes the PSCI library interface with a focus on how to
5*54fd6939SJiyong Parkintegrate with a suitable Trusted OS for an Armv8-A AArch32 system. The PSCI
6*54fd6939SJiyong ParkLibrary implements the PSCI Standard as described in `PSCI spec`_ and is meant
7*54fd6939SJiyong Parkto be integrated with EL3 Runtime Software which invokes the PSCI Library
8*54fd6939SJiyong Parkinterface appropriately. **EL3 Runtime Software** refers to software executing
9*54fd6939SJiyong Parkat the highest secure privileged mode, which is EL3 in AArch64 or Secure SVC/
10*54fd6939SJiyong ParkMonitor mode in AArch32, and provides runtime services to the non-secure world.
11*54fd6939SJiyong ParkThe runtime service request is made via SMC (Secure Monitor Call) and the call
12*54fd6939SJiyong Parkmust adhere to `SMCCC`_. In AArch32, EL3 Runtime Software may additionally
13*54fd6939SJiyong Parkinclude Trusted OS functionality. A minimal AArch32 Secure Payload, SP-MIN, is
14*54fd6939SJiyong Parkprovided in Trusted Firmware-A (TF-A) to illustrate the usage and integration
15*54fd6939SJiyong Parkof the PSCI library. The description of PSCI library interface and its
16*54fd6939SJiyong Parkintegration with EL3 Runtime Software in this document is targeted towards
17*54fd6939SJiyong ParkAArch32 systems.
18*54fd6939SJiyong Park
19*54fd6939SJiyong ParkGeneric call sequence for PSCI Library interface (AArch32)
20*54fd6939SJiyong Park----------------------------------------------------------
21*54fd6939SJiyong Park
22*54fd6939SJiyong ParkThe generic call sequence of PSCI Library interfaces (see
23*54fd6939SJiyong Park`PSCI Library Interface`_) during cold boot in AArch32
24*54fd6939SJiyong Parksystem is described below:
25*54fd6939SJiyong Park
26*54fd6939SJiyong Park#. After cold reset, the EL3 Runtime Software performs its cold boot
27*54fd6939SJiyong Park   initialization including the PSCI library pre-requisites mentioned in
28*54fd6939SJiyong Park   `PSCI Library Interface`_, and also the necessary platform
29*54fd6939SJiyong Park   setup.
30*54fd6939SJiyong Park
31*54fd6939SJiyong Park#. Call ``psci_setup()`` in Monitor mode.
32*54fd6939SJiyong Park
33*54fd6939SJiyong Park#. Optionally call ``psci_register_spd_pm_hook()`` to register callbacks to
34*54fd6939SJiyong Park   do bookkeeping for the EL3 Runtime Software during power management.
35*54fd6939SJiyong Park
36*54fd6939SJiyong Park#. Call ``psci_prepare_next_non_secure_ctx()`` to initialize the non-secure CPU
37*54fd6939SJiyong Park   context.
38*54fd6939SJiyong Park
39*54fd6939SJiyong Park#. Get the non-secure ``cpu_context_t`` for the current CPU by calling
40*54fd6939SJiyong Park   ``cm_get_context()`` , then programming the registers in the non-secure
41*54fd6939SJiyong Park   context and exiting to non-secure world. If the EL3 Runtime Software needs
42*54fd6939SJiyong Park   additional configuration to be set for non-secure context, like routing
43*54fd6939SJiyong Park   FIQs to the secure world, the values of the registers can be modified prior
44*54fd6939SJiyong Park   to programming. See `PSCI CPU context management`_ for more
45*54fd6939SJiyong Park   details on CPU context management.
46*54fd6939SJiyong Park
47*54fd6939SJiyong ParkThe generic call sequence of PSCI library interfaces during warm boot in
48*54fd6939SJiyong ParkAArch32 systems is described below:
49*54fd6939SJiyong Park
50*54fd6939SJiyong Park#. After warm reset, the EL3 Runtime Software performs the necessary warm
51*54fd6939SJiyong Park   boot initialization including the PSCI library pre-requisites mentioned in
52*54fd6939SJiyong Park   `PSCI Library Interface`_ (Note that the Data cache
53*54fd6939SJiyong Park   **must not** be enabled).
54*54fd6939SJiyong Park
55*54fd6939SJiyong Park#. Call ``psci_warmboot_entrypoint()`` in Monitor mode. This interface
56*54fd6939SJiyong Park   initializes/restores the non-secure CPU context as well.
57*54fd6939SJiyong Park
58*54fd6939SJiyong Park#. Do step 5 of the cold boot call sequence described above.
59*54fd6939SJiyong Park
60*54fd6939SJiyong ParkThe generic call sequence of PSCI library interfaces on receipt of a PSCI SMC
61*54fd6939SJiyong Parkon an AArch32 system is described below:
62*54fd6939SJiyong Park
63*54fd6939SJiyong Park#. On receipt of an SMC, save the register context as per `SMCCC`_.
64*54fd6939SJiyong Park
65*54fd6939SJiyong Park#. If the SMC function identifier corresponds to a SMC32 PSCI API, construct
66*54fd6939SJiyong Park   the appropriate arguments and call the ``psci_smc_handler()`` interface.
67*54fd6939SJiyong Park   The invocation may or may not return back to the caller depending on
68*54fd6939SJiyong Park   whether the PSCI API resulted in power down of the CPU.
69*54fd6939SJiyong Park
70*54fd6939SJiyong Park#. If ``psci_smc_handler()`` returns, populate the return value in R0 (AArch32)/
71*54fd6939SJiyong Park   X0 (AArch64) and restore other registers as per `SMCCC`_.
72*54fd6939SJiyong Park
73*54fd6939SJiyong ParkPSCI CPU context management
74*54fd6939SJiyong Park---------------------------
75*54fd6939SJiyong Park
76*54fd6939SJiyong ParkPSCI library is in charge of initializing/restoring the non-secure CPU system
77*54fd6939SJiyong Parkregisters according to `PSCI specification`_ during cold/warm boot.
78*54fd6939SJiyong ParkThis is referred to as ``PSCI CPU Context Management``. Registers that need to
79*54fd6939SJiyong Parkbe preserved across CPU power down/power up cycles are maintained in
80*54fd6939SJiyong Park``cpu_context_t`` data structure. The initialization of other non-secure CPU
81*54fd6939SJiyong Parksystem registers which do not require coordination with the EL3 Runtime
82*54fd6939SJiyong ParkSoftware is done directly by the PSCI library (see ``cm_prepare_el3_exit()``).
83*54fd6939SJiyong Park
84*54fd6939SJiyong ParkThe EL3 Runtime Software is responsible for managing register context
85*54fd6939SJiyong Parkduring switch between Normal and Secure worlds. The register context to be
86*54fd6939SJiyong Parksaved and restored depends on the mechanism used to trigger the world switch.
87*54fd6939SJiyong ParkFor example, if the world switch was triggered by an SMC call, then the
88*54fd6939SJiyong Parkregisters need to be saved and restored according to `SMCCC`_. In AArch64,
89*54fd6939SJiyong Parkdue to the tight integration with BL31, both BL31 and PSCI library
90*54fd6939SJiyong Parkuse the same ``cpu_context_t`` data structure for PSCI CPU context management
91*54fd6939SJiyong Parkand register context management during world switch. This cannot be assumed
92*54fd6939SJiyong Parkfor AArch32 EL3 Runtime Software since most AArch32 Trusted OSes already implement
93*54fd6939SJiyong Parka mechanism for register context management during world switch. Hence, when
94*54fd6939SJiyong Parkthe PSCI library is integrated with a AArch32 EL3 Runtime Software, the
95*54fd6939SJiyong Park``cpu_context_t`` is stripped down for just PSCI CPU context management.
96*54fd6939SJiyong Park
97*54fd6939SJiyong ParkDuring cold/warm boot, after invoking appropriate PSCI library interfaces, it
98*54fd6939SJiyong Parkis expected that the EL3 Runtime Software will query the ``cpu_context_t`` and
99*54fd6939SJiyong Parkwrite appropriate values to the corresponding system registers. This mechanism
100*54fd6939SJiyong Parkresolves 2 additional problems for AArch32 EL3 Runtime Software:
101*54fd6939SJiyong Park
102*54fd6939SJiyong Park#. Values for certain system registers like SCR and SCTLR cannot be
103*54fd6939SJiyong Park   unilaterally determined by PSCI library and need inputs from the EL3
104*54fd6939SJiyong Park   Runtime Software. Using ``cpu_context_t`` as an intermediary data store
105*54fd6939SJiyong Park   allows EL3 Runtime Software to modify the register values appropriately
106*54fd6939SJiyong Park   before programming them.
107*54fd6939SJiyong Park
108*54fd6939SJiyong Park#. The PSCI library provides appropriate LR and SPSR values (entrypoint
109*54fd6939SJiyong Park   information) for exit into non-secure world. Using ``cpu_context_t`` as an
110*54fd6939SJiyong Park   intermediary data store allows the EL3 Runtime Software to store these
111*54fd6939SJiyong Park   values safely until it is ready for exit to non-secure world.
112*54fd6939SJiyong Park
113*54fd6939SJiyong ParkCurrently the ``cpu_context_t`` data structure for AArch32 stores the following
114*54fd6939SJiyong Parkregisters: R0 - R3, LR (R14), SCR, SPSR, SCTLR.
115*54fd6939SJiyong Park
116*54fd6939SJiyong ParkThe EL3 Runtime Software must implement accessors to get/set pointers
117*54fd6939SJiyong Parkto CPU context ``cpu_context_t`` data and these are described in
118*54fd6939SJiyong Park`CPU Context management API`_.
119*54fd6939SJiyong Park
120*54fd6939SJiyong ParkPSCI Library Interface
121*54fd6939SJiyong Park----------------------
122*54fd6939SJiyong Park
123*54fd6939SJiyong ParkThe PSCI library implements the `PSCI Specification`_. The interfaces
124*54fd6939SJiyong Parkto this library are declared in ``psci_lib.h`` and are as listed below:
125*54fd6939SJiyong Park
126*54fd6939SJiyong Park.. code:: c
127*54fd6939SJiyong Park
128*54fd6939SJiyong Park        u_register_t psci_smc_handler(uint32_t smc_fid, u_register_t x1,
129*54fd6939SJiyong Park                                      u_register_t x2, u_register_t x3,
130*54fd6939SJiyong Park                                      u_register_t x4, void *cookie,
131*54fd6939SJiyong Park                                      void *handle, u_register_t flags);
132*54fd6939SJiyong Park        int psci_setup(const psci_lib_args_t *lib_args);
133*54fd6939SJiyong Park        void psci_warmboot_entrypoint(void);
134*54fd6939SJiyong Park        void psci_register_spd_pm_hook(const spd_pm_ops_t *pm);
135*54fd6939SJiyong Park        void psci_prepare_next_non_secure_ctx(entry_point_info_t *next_image_info);
136*54fd6939SJiyong Park
137*54fd6939SJiyong ParkThe CPU context data 'cpu_context_t' is programmed to the registers differently
138*54fd6939SJiyong Parkwhen PSCI is integrated with an AArch32 EL3 Runtime Software compared to
139*54fd6939SJiyong Parkwhen the PSCI is integrated with an AArch64 EL3 Runtime Software (BL31). For
140*54fd6939SJiyong Parkexample, in the case of AArch64, there is no need to retrieve ``cpu_context_t``
141*54fd6939SJiyong Parkdata and program the registers as it will done implicitly as part of
142*54fd6939SJiyong Park``el3_exit``. The description below of the PSCI interfaces is targeted at
143*54fd6939SJiyong Parkintegration with an AArch32 EL3 Runtime Software.
144*54fd6939SJiyong Park
145*54fd6939SJiyong ParkThe PSCI library is responsible for initializing/restoring the non-secure world
146*54fd6939SJiyong Parkto an appropriate state after boot and may choose to directly program the
147*54fd6939SJiyong Parknon-secure system registers. The PSCI generic code takes care not to directly
148*54fd6939SJiyong Parkmodify any of the system registers affecting the secure world and instead
149*54fd6939SJiyong Parkreturns the values to be programmed to these registers via ``cpu_context_t``.
150*54fd6939SJiyong ParkThe EL3 Runtime Software is responsible for programming those registers and
151*54fd6939SJiyong Parkcan use the proposed values provided in the ``cpu_context_t``, modifying the
152*54fd6939SJiyong Parkvalues if required.
153*54fd6939SJiyong Park
154*54fd6939SJiyong ParkPSCI library needs the flexibility to access both secure and non-secure
155*54fd6939SJiyong Parkcopies of banked registers. Hence it needs to be invoked in Monitor mode
156*54fd6939SJiyong Parkfor AArch32 and in EL3 for AArch64. The NS bit in SCR (in AArch32) or SCR_EL3
157*54fd6939SJiyong Park(in AArch64) must be set to 0. Additional requirements for the PSCI library
158*54fd6939SJiyong Parkinterfaces are:
159*54fd6939SJiyong Park
160*54fd6939SJiyong Park-  Instruction cache must be enabled
161*54fd6939SJiyong Park-  Both IRQ and FIQ must be masked for the current CPU
162*54fd6939SJiyong Park-  The page tables must be setup and the MMU enabled
163*54fd6939SJiyong Park-  The C runtime environment must be setup and stack initialized
164*54fd6939SJiyong Park-  The Data cache must be enabled prior to invoking any of the PSCI library
165*54fd6939SJiyong Park   interfaces except for ``psci_warmboot_entrypoint()``. For
166*54fd6939SJiyong Park   ``psci_warmboot_entrypoint()``, if the build option ``HW_ASSISTED_COHERENCY``
167*54fd6939SJiyong Park   is enabled however, data caches are expected to be enabled.
168*54fd6939SJiyong Park
169*54fd6939SJiyong ParkFurther requirements for each interface can be found in the interface
170*54fd6939SJiyong Parkdescription.
171*54fd6939SJiyong Park
172*54fd6939SJiyong ParkInterface : psci_setup()
173*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~
174*54fd6939SJiyong Park
175*54fd6939SJiyong Park::
176*54fd6939SJiyong Park
177*54fd6939SJiyong Park    Argument : const psci_lib_args_t *lib_args
178*54fd6939SJiyong Park    Return   : void
179*54fd6939SJiyong Park
180*54fd6939SJiyong ParkThis function is to be called by the primary CPU during cold boot before
181*54fd6939SJiyong Parkany other interface to the PSCI library. It takes ``lib_args``, a const pointer
182*54fd6939SJiyong Parkto ``psci_lib_args_t``, as the argument. The ``psci_lib_args_t`` is a versioned
183*54fd6939SJiyong Parkstructure and is declared in ``psci_lib.h`` header as follows:
184*54fd6939SJiyong Park
185*54fd6939SJiyong Park.. code:: c
186*54fd6939SJiyong Park
187*54fd6939SJiyong Park        typedef struct psci_lib_args {
188*54fd6939SJiyong Park            /* The version information of PSCI Library Interface */
189*54fd6939SJiyong Park            param_header_t        h;
190*54fd6939SJiyong Park            /* The warm boot entrypoint function */
191*54fd6939SJiyong Park            mailbox_entrypoint_t  mailbox_ep;
192*54fd6939SJiyong Park        } psci_lib_args_t;
193*54fd6939SJiyong Park
194*54fd6939SJiyong ParkThe first field ``h``, of ``param_header_t`` type, provides the version
195*54fd6939SJiyong Parkinformation. The second field ``mailbox_ep`` is the warm boot entrypoint address
196*54fd6939SJiyong Parkand is used to configure the platform mailbox. Helper macros are provided in
197*54fd6939SJiyong Park``psci_lib.h`` to construct the ``lib_args`` argument statically or during
198*54fd6939SJiyong Parkruntime. Prior to calling the ``psci_setup()`` interface, the platform setup for
199*54fd6939SJiyong Parkcold boot must have completed. Major actions performed by this interface are:
200*54fd6939SJiyong Park
201*54fd6939SJiyong Park-  Initializes architecture.
202*54fd6939SJiyong Park-  Initializes PSCI power domain and state coordination data structures.
203*54fd6939SJiyong Park-  Calls ``plat_setup_psci_ops()`` with warm boot entrypoint ``mailbox_ep`` as
204*54fd6939SJiyong Park   argument.
205*54fd6939SJiyong Park-  Calls ``cm_set_context_by_index()`` (see
206*54fd6939SJiyong Park   `CPU Context management API`_) for all the CPUs in the
207*54fd6939SJiyong Park   platform
208*54fd6939SJiyong Park
209*54fd6939SJiyong ParkInterface : psci_prepare_next_non_secure_ctx()
210*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211*54fd6939SJiyong Park
212*54fd6939SJiyong Park::
213*54fd6939SJiyong Park
214*54fd6939SJiyong Park    Argument : entry_point_info_t *next_image_info
215*54fd6939SJiyong Park    Return   : void
216*54fd6939SJiyong Park
217*54fd6939SJiyong ParkAfter ``psci_setup()`` and prior to exit to the non-secure world, this function
218*54fd6939SJiyong Parkmust be called by the EL3 Runtime Software to initialize the non-secure world
219*54fd6939SJiyong Parkcontext. The non-secure world entrypoint information ``next_image_info`` (first
220*54fd6939SJiyong Parkargument) will be used to determine the non-secure context. After this function
221*54fd6939SJiyong Parkreturns, the EL3 Runtime Software must retrieve the ``cpu_context_t`` (using
222*54fd6939SJiyong Parkcm_get_context()) for the current CPU and program the registers prior to exit
223*54fd6939SJiyong Parkto the non-secure world.
224*54fd6939SJiyong Park
225*54fd6939SJiyong ParkInterface : psci_register_spd_pm_hook()
226*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227*54fd6939SJiyong Park
228*54fd6939SJiyong Park::
229*54fd6939SJiyong Park
230*54fd6939SJiyong Park    Argument : const spd_pm_ops_t *
231*54fd6939SJiyong Park    Return   : void
232*54fd6939SJiyong Park
233*54fd6939SJiyong ParkAs explained in `Secure payload power management callback`_,
234*54fd6939SJiyong Parkthe EL3 Runtime Software may want to perform some bookkeeping during power
235*54fd6939SJiyong Parkmanagement operations. This function is used to register the ``spd_pm_ops_t``
236*54fd6939SJiyong Park(first argument) callbacks with the PSCI library which will be called
237*54fd6939SJiyong Parkappropriately during power management. Calling this function is optional and
238*54fd6939SJiyong Parkneed to be called by the primary CPU during the cold boot sequence after
239*54fd6939SJiyong Park``psci_setup()`` has completed.
240*54fd6939SJiyong Park
241*54fd6939SJiyong ParkInterface : psci_smc_handler()
242*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243*54fd6939SJiyong Park
244*54fd6939SJiyong Park::
245*54fd6939SJiyong Park
246*54fd6939SJiyong Park    Argument : uint32_t smc_fid, u_register_t x1,
247*54fd6939SJiyong Park               u_register_t x2, u_register_t x3,
248*54fd6939SJiyong Park               u_register_t x4, void *cookie,
249*54fd6939SJiyong Park               void *handle, u_register_t flags
250*54fd6939SJiyong Park    Return   : u_register_t
251*54fd6939SJiyong Park
252*54fd6939SJiyong ParkThis function is the top level handler for SMCs which fall within the
253*54fd6939SJiyong ParkPSCI service range specified in `SMCCC`_. The function ID ``smc_fid`` (first
254*54fd6939SJiyong Parkargument) determines the PSCI API to be called. The ``x1`` to ``x4`` (2nd to 5th
255*54fd6939SJiyong Parkarguments), are the values of the registers r1 - r4 (in AArch32) or x1 - x4
256*54fd6939SJiyong Park(in AArch64) when the SMC is received. These are the arguments to PSCI API as
257*54fd6939SJiyong Parkdescribed in `PSCI spec`_. The 'flags' (8th argument) is a bit field parameter
258*54fd6939SJiyong Parkand is detailed in 'smccc.h' header. It includes whether the call is from the
259*54fd6939SJiyong Parksecure or non-secure world. The ``cookie`` (6th argument) and the ``handle``
260*54fd6939SJiyong Park(7th argument) are not used and are reserved for future use.
261*54fd6939SJiyong Park
262*54fd6939SJiyong ParkThe return value from this interface is the return value from the underlying
263*54fd6939SJiyong ParkPSCI API corresponding to ``smc_fid``. This function may not return back to the
264*54fd6939SJiyong Parkcaller if PSCI API causes power down of the CPU. In this case, when the CPU
265*54fd6939SJiyong Parkwakes up, it will start execution from the warm reset address.
266*54fd6939SJiyong Park
267*54fd6939SJiyong ParkInterface : psci_warmboot_entrypoint()
268*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269*54fd6939SJiyong Park
270*54fd6939SJiyong Park::
271*54fd6939SJiyong Park
272*54fd6939SJiyong Park    Argument : void
273*54fd6939SJiyong Park    Return   : void
274*54fd6939SJiyong Park
275*54fd6939SJiyong ParkThis function performs the warm boot initialization/restoration as mandated by
276*54fd6939SJiyong Park`PSCI spec`_. For AArch32, on wakeup from power down the CPU resets to secure SVC
277*54fd6939SJiyong Parkmode and the EL3 Runtime Software must perform the prerequisite initializations
278*54fd6939SJiyong Parkmentioned at top of this section. This function must be called with Data cache
279*54fd6939SJiyong Parkdisabled (unless build option ``HW_ASSISTED_COHERENCY`` is enabled) but with MMU
280*54fd6939SJiyong Parkinitialized and enabled. The major actions performed by this function are:
281*54fd6939SJiyong Park
282*54fd6939SJiyong Park-  Invalidates the stack and enables the data cache.
283*54fd6939SJiyong Park-  Initializes architecture and PSCI state coordination.
284*54fd6939SJiyong Park-  Restores/Initializes the peripheral drivers to the required state via
285*54fd6939SJiyong Park   appropriate ``plat_psci_ops_t`` hooks
286*54fd6939SJiyong Park-  Restores the EL3 Runtime Software context via appropriate ``spd_pm_ops_t``
287*54fd6939SJiyong Park   callbacks.
288*54fd6939SJiyong Park-  Restores/Initializes the non-secure context and populates the
289*54fd6939SJiyong Park   ``cpu_context_t`` for the current CPU.
290*54fd6939SJiyong Park
291*54fd6939SJiyong ParkUpon the return of this function, the EL3 Runtime Software must retrieve the
292*54fd6939SJiyong Parknon-secure ``cpu_context_t`` using ``cm_get_context()`` and program the registers
293*54fd6939SJiyong Parkprior to exit to the non-secure world.
294*54fd6939SJiyong Park
295*54fd6939SJiyong ParkEL3 Runtime Software dependencies
296*54fd6939SJiyong Park---------------------------------
297*54fd6939SJiyong Park
298*54fd6939SJiyong ParkThe PSCI Library includes supporting frameworks like context management,
299*54fd6939SJiyong Parkcpu operations (cpu_ops) and per-cpu data framework. Other helper library
300*54fd6939SJiyong Parkfunctions like bakery locks and spin locks are also included in the library.
301*54fd6939SJiyong ParkThe dependencies which must be fulfilled by the EL3 Runtime Software
302*54fd6939SJiyong Parkfor integration with PSCI library are described below.
303*54fd6939SJiyong Park
304*54fd6939SJiyong ParkGeneral dependencies
305*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~
306*54fd6939SJiyong Park
307*54fd6939SJiyong ParkThe PSCI library being a Multiprocessor (MP) implementation, EL3 Runtime
308*54fd6939SJiyong ParkSoftware must provide an SMC handling framework capable of MP adhering to
309*54fd6939SJiyong Park`SMCCC`_ specification.
310*54fd6939SJiyong Park
311*54fd6939SJiyong ParkThe EL3 Runtime Software must also export cache maintenance primitives
312*54fd6939SJiyong Parkand some helper utilities for assert, print and memory operations as listed
313*54fd6939SJiyong Parkbelow. The TF-A source tree provides implementations for all
314*54fd6939SJiyong Parkthese functions but the EL3 Runtime Software may use its own implementation.
315*54fd6939SJiyong Park
316*54fd6939SJiyong Park**Functions : assert(), memcpy(), memset(), printf()**
317*54fd6939SJiyong Park
318*54fd6939SJiyong ParkThese must be implemented as described in ISO C Standard.
319*54fd6939SJiyong Park
320*54fd6939SJiyong Park**Function : flush_dcache_range()**
321*54fd6939SJiyong Park
322*54fd6939SJiyong Park::
323*54fd6939SJiyong Park
324*54fd6939SJiyong Park    Argument : uintptr_t addr, size_t size
325*54fd6939SJiyong Park    Return   : void
326*54fd6939SJiyong Park
327*54fd6939SJiyong ParkThis function cleans and invalidates (flushes) the data cache for memory
328*54fd6939SJiyong Parkat address ``addr`` (first argument) address and of size ``size`` (second argument).
329*54fd6939SJiyong Park
330*54fd6939SJiyong Park**Function : inv_dcache_range()**
331*54fd6939SJiyong Park
332*54fd6939SJiyong Park::
333*54fd6939SJiyong Park
334*54fd6939SJiyong Park    Argument : uintptr_t addr, size_t size
335*54fd6939SJiyong Park    Return   : void
336*54fd6939SJiyong Park
337*54fd6939SJiyong ParkThis function invalidates (flushes) the data cache for memory at address
338*54fd6939SJiyong Park``addr`` (first argument) address and of size ``size`` (second argument).
339*54fd6939SJiyong Park
340*54fd6939SJiyong Park**Function : do_panic()**
341*54fd6939SJiyong Park
342*54fd6939SJiyong Park::
343*54fd6939SJiyong Park
344*54fd6939SJiyong Park    Argument : void
345*54fd6939SJiyong Park    Return   : void
346*54fd6939SJiyong Park
347*54fd6939SJiyong ParkThis function will be called by the PSCI library on encountering a critical
348*54fd6939SJiyong Parkfailure that cannot be recovered from. This function **must not** return.
349*54fd6939SJiyong Park
350*54fd6939SJiyong ParkCPU Context management API
351*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~
352*54fd6939SJiyong Park
353*54fd6939SJiyong ParkThe CPU context management data memory is statically allocated by PSCI library
354*54fd6939SJiyong Parkin BSS section. The PSCI library requires the EL3 Runtime Software to implement
355*54fd6939SJiyong ParkAPIs to store and retrieve pointers to this CPU context data. SP-MIN
356*54fd6939SJiyong Parkdemonstrates how these APIs can be implemented but the EL3 Runtime Software can
357*54fd6939SJiyong Parkchoose a more optimal implementation (like dedicating the secure TPIDRPRW
358*54fd6939SJiyong Parksystem register (in AArch32) for storing these pointers).
359*54fd6939SJiyong Park
360*54fd6939SJiyong Park**Function : cm_set_context_by_index()**
361*54fd6939SJiyong Park
362*54fd6939SJiyong Park::
363*54fd6939SJiyong Park
364*54fd6939SJiyong Park    Argument : unsigned int cpu_idx, void *context, unsigned int security_state
365*54fd6939SJiyong Park    Return   : void
366*54fd6939SJiyong Park
367*54fd6939SJiyong ParkThis function is called during cold boot when the ``psci_setup()`` PSCI library
368*54fd6939SJiyong Parkinterface is called.
369*54fd6939SJiyong Park
370*54fd6939SJiyong ParkThis function must store the pointer to the CPU context data, ``context`` (2nd
371*54fd6939SJiyong Parkargument), for the specified ``security_state`` (3rd argument) and CPU identified
372*54fd6939SJiyong Parkby ``cpu_idx`` (first argument). The ``security_state`` will always be non-secure
373*54fd6939SJiyong Parkwhen called by PSCI library and this argument is retained for compatibility
374*54fd6939SJiyong Parkwith BL31. The ``cpu_idx`` will correspond to the index returned by the
375*54fd6939SJiyong Park``plat_core_pos_by_mpidr()`` for ``mpidr`` of the CPU.
376*54fd6939SJiyong Park
377*54fd6939SJiyong ParkThe actual method of storing the ``context`` pointers is implementation specific.
378*54fd6939SJiyong ParkFor example, SP-MIN stores the pointers in the array ``sp_min_cpu_ctx_ptr``
379*54fd6939SJiyong Parkdeclared in ``sp_min_main.c``.
380*54fd6939SJiyong Park
381*54fd6939SJiyong Park**Function : cm_get_context()**
382*54fd6939SJiyong Park
383*54fd6939SJiyong Park::
384*54fd6939SJiyong Park
385*54fd6939SJiyong Park    Argument : uint32_t security_state
386*54fd6939SJiyong Park    Return   : void *
387*54fd6939SJiyong Park
388*54fd6939SJiyong ParkThis function must return the pointer to the ``cpu_context_t`` structure for
389*54fd6939SJiyong Parkthe specified ``security_state`` (first argument) for the current CPU. The caller
390*54fd6939SJiyong Parkmust ensure that ``cm_set_context_by_index`` is called first and the appropriate
391*54fd6939SJiyong Parkcontext pointers are stored prior to invoking this API. The ``security_state``
392*54fd6939SJiyong Parkwill always be non-secure when called by PSCI library and this argument
393*54fd6939SJiyong Parkis retained for compatibility with BL31.
394*54fd6939SJiyong Park
395*54fd6939SJiyong Park**Function : cm_get_context_by_index()**
396*54fd6939SJiyong Park
397*54fd6939SJiyong Park::
398*54fd6939SJiyong Park
399*54fd6939SJiyong Park    Argument : unsigned int cpu_idx, unsigned int security_state
400*54fd6939SJiyong Park    Return   : void *
401*54fd6939SJiyong Park
402*54fd6939SJiyong ParkThis function must return the pointer to the ``cpu_context_t`` structure for
403*54fd6939SJiyong Parkthe specified ``security_state`` (second argument) for the CPU identified by
404*54fd6939SJiyong Park``cpu_idx`` (first argument). The caller must ensure that
405*54fd6939SJiyong Park``cm_set_context_by_index`` is called first and the appropriate context
406*54fd6939SJiyong Parkpointers are stored prior to invoking this API. The ``security_state`` will
407*54fd6939SJiyong Parkalways be non-secure when called by PSCI library and this argument is
408*54fd6939SJiyong Parkretained for compatibility with BL31. The ``cpu_idx`` will correspond to the
409*54fd6939SJiyong Parkindex returned by the ``plat_core_pos_by_mpidr()`` for ``mpidr`` of the CPU.
410*54fd6939SJiyong Park
411*54fd6939SJiyong ParkPlatform API
412*54fd6939SJiyong Park~~~~~~~~~~~~
413*54fd6939SJiyong Park
414*54fd6939SJiyong ParkThe platform layer abstracts the platform-specific details from the generic
415*54fd6939SJiyong ParkPSCI library. The following platform APIs/macros must be defined by the EL3
416*54fd6939SJiyong ParkRuntime Software for integration with the PSCI library.
417*54fd6939SJiyong Park
418*54fd6939SJiyong ParkThe mandatory platform APIs are:
419*54fd6939SJiyong Park
420*54fd6939SJiyong Park-  plat_my_core_pos
421*54fd6939SJiyong Park-  plat_core_pos_by_mpidr
422*54fd6939SJiyong Park-  plat_get_syscnt_freq2
423*54fd6939SJiyong Park-  plat_get_power_domain_tree_desc
424*54fd6939SJiyong Park-  plat_setup_psci_ops
425*54fd6939SJiyong Park-  plat_reset_handler
426*54fd6939SJiyong Park-  plat_panic_handler
427*54fd6939SJiyong Park-  plat_get_my_stack
428*54fd6939SJiyong Park
429*54fd6939SJiyong ParkThe mandatory platform macros are:
430*54fd6939SJiyong Park
431*54fd6939SJiyong Park-  PLATFORM_CORE_COUNT
432*54fd6939SJiyong Park-  PLAT_MAX_PWR_LVL
433*54fd6939SJiyong Park-  PLAT_NUM_PWR_DOMAINS
434*54fd6939SJiyong Park-  CACHE_WRITEBACK_GRANULE
435*54fd6939SJiyong Park-  PLAT_MAX_OFF_STATE
436*54fd6939SJiyong Park-  PLAT_MAX_RET_STATE
437*54fd6939SJiyong Park-  PLAT_MAX_PWR_LVL_STATES (optional)
438*54fd6939SJiyong Park-  PLAT_PCPU_DATA_SIZE (optional)
439*54fd6939SJiyong Park
440*54fd6939SJiyong ParkThe details of these APIs/macros can be found in the :ref:`Porting Guide`.
441*54fd6939SJiyong Park
442*54fd6939SJiyong ParkAll platform specific operations for power management are done via
443*54fd6939SJiyong Park``plat_psci_ops_t`` callbacks registered by the platform when
444*54fd6939SJiyong Park``plat_setup_psci_ops()`` API is called. The description of each of
445*54fd6939SJiyong Parkthe callbacks in ``plat_psci_ops_t`` can be found in PSCI section of the
446*54fd6939SJiyong Park:ref:`Porting Guide`. If any these callbacks are not registered, then the
447*54fd6939SJiyong ParkPSCI API associated with that callback will not be supported by PSCI
448*54fd6939SJiyong Parklibrary.
449*54fd6939SJiyong Park
450*54fd6939SJiyong ParkSecure payload power management callback
451*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
452*54fd6939SJiyong Park
453*54fd6939SJiyong ParkDuring PSCI power management operations, the EL3 Runtime Software may
454*54fd6939SJiyong Parkneed to perform some bookkeeping, and PSCI library provides
455*54fd6939SJiyong Park``spd_pm_ops_t`` callbacks for this purpose. These hooks must be
456*54fd6939SJiyong Parkpopulated and registered by using ``psci_register_spd_pm_hook()`` PSCI
457*54fd6939SJiyong Parklibrary interface.
458*54fd6939SJiyong Park
459*54fd6939SJiyong ParkTypical bookkeeping during PSCI power management calls include save/restore
460*54fd6939SJiyong Parkof the EL3 Runtime Software context. Also if the EL3 Runtime Software makes
461*54fd6939SJiyong Parkuse of secure interrupts, then these interrupts must also be managed
462*54fd6939SJiyong Parkappropriately during CPU power down/power up. Any secure interrupt targeted
463*54fd6939SJiyong Parkto the current CPU must be disabled or re-targeted to other running CPU prior
464*54fd6939SJiyong Parkto power down of the current CPU. During power up, these interrupt can be
465*54fd6939SJiyong Parkenabled/re-targeted back to the current CPU.
466*54fd6939SJiyong Park
467*54fd6939SJiyong Park.. code:: c
468*54fd6939SJiyong Park
469*54fd6939SJiyong Park        typedef struct spd_pm_ops {
470*54fd6939SJiyong Park                void (*svc_on)(u_register_t target_cpu);
471*54fd6939SJiyong Park                int32_t (*svc_off)(u_register_t __unused);
472*54fd6939SJiyong Park                void (*svc_suspend)(u_register_t max_off_pwrlvl);
473*54fd6939SJiyong Park                void (*svc_on_finish)(u_register_t __unused);
474*54fd6939SJiyong Park                void (*svc_suspend_finish)(u_register_t max_off_pwrlvl);
475*54fd6939SJiyong Park                int32_t (*svc_migrate)(u_register_t from_cpu, u_register_t to_cpu);
476*54fd6939SJiyong Park                int32_t (*svc_migrate_info)(u_register_t *resident_cpu);
477*54fd6939SJiyong Park                void (*svc_system_off)(void);
478*54fd6939SJiyong Park                void (*svc_system_reset)(void);
479*54fd6939SJiyong Park        } spd_pm_ops_t;
480*54fd6939SJiyong Park
481*54fd6939SJiyong ParkA brief description of each callback is given below:
482*54fd6939SJiyong Park
483*54fd6939SJiyong Park-  svc_on, svc_off, svc_on_finish
484*54fd6939SJiyong Park
485*54fd6939SJiyong Park   The ``svc_on``, ``svc_off`` callbacks are called during PSCI_CPU_ON,
486*54fd6939SJiyong Park   PSCI_CPU_OFF APIs respectively. The ``svc_on_finish`` is called when the
487*54fd6939SJiyong Park   target CPU of PSCI_CPU_ON API powers up and executes the
488*54fd6939SJiyong Park   ``psci_warmboot_entrypoint()`` PSCI library interface.
489*54fd6939SJiyong Park
490*54fd6939SJiyong Park-  svc_suspend, svc_suspend_finish
491*54fd6939SJiyong Park
492*54fd6939SJiyong Park   The ``svc_suspend`` callback is called during power down bu either
493*54fd6939SJiyong Park   PSCI_SUSPEND or PSCI_SYSTEM_SUSPEND APIs. The ``svc_suspend_finish`` is
494*54fd6939SJiyong Park   called when the CPU wakes up from suspend and executes the
495*54fd6939SJiyong Park   ``psci_warmboot_entrypoint()`` PSCI library interface. The ``max_off_pwrlvl``
496*54fd6939SJiyong Park   (first parameter) denotes the highest power domain level being powered down
497*54fd6939SJiyong Park   to or woken up from suspend.
498*54fd6939SJiyong Park
499*54fd6939SJiyong Park-  svc_system_off, svc_system_reset
500*54fd6939SJiyong Park
501*54fd6939SJiyong Park   These callbacks are called during PSCI_SYSTEM_OFF and PSCI_SYSTEM_RESET
502*54fd6939SJiyong Park   PSCI APIs respectively.
503*54fd6939SJiyong Park
504*54fd6939SJiyong Park-  svc_migrate_info
505*54fd6939SJiyong Park
506*54fd6939SJiyong Park   This callback is called in response to PSCI_MIGRATE_INFO_TYPE or
507*54fd6939SJiyong Park   PSCI_MIGRATE_INFO_UP_CPU APIs. The return value of this callback must
508*54fd6939SJiyong Park   correspond to the return value of PSCI_MIGRATE_INFO_TYPE API as described
509*54fd6939SJiyong Park   in `PSCI spec`_. If the secure payload is a Uniprocessor (UP)
510*54fd6939SJiyong Park   implementation, then it must update the mpidr of the CPU it is resident in
511*54fd6939SJiyong Park   via ``resident_cpu`` (first argument). The updates to ``resident_cpu`` is
512*54fd6939SJiyong Park   ignored if the secure payload is a multiprocessor (MP) implementation.
513*54fd6939SJiyong Park
514*54fd6939SJiyong Park-  svc_migrate
515*54fd6939SJiyong Park
516*54fd6939SJiyong Park   This callback is only relevant if the secure payload in EL3 Runtime
517*54fd6939SJiyong Park   Software is a Uniprocessor (UP) implementation and supports migration from
518*54fd6939SJiyong Park   the current CPU ``from_cpu`` (first argument) to another CPU ``to_cpu``
519*54fd6939SJiyong Park   (second argument). This callback is called in response to PSCI_MIGRATE
520*54fd6939SJiyong Park   API. This callback is never called if the secure payload is a
521*54fd6939SJiyong Park   Multiprocessor (MP) implementation.
522*54fd6939SJiyong Park
523*54fd6939SJiyong ParkCPU operations
524*54fd6939SJiyong Park~~~~~~~~~~~~~~
525*54fd6939SJiyong Park
526*54fd6939SJiyong ParkThe CPU operations (cpu_ops) framework implement power down sequence specific
527*54fd6939SJiyong Parkto the CPU and the details of which can be found at
528*54fd6939SJiyong Park:ref:`firmware_design_cpu_ops_fwk`. The TF-A tree implements the ``cpu_ops``
529*54fd6939SJiyong Parkfor various supported CPUs and the EL3 Runtime Software needs to include the
530*54fd6939SJiyong Parkrequired ``cpu_ops`` in its build. The start and end of the ``cpu_ops``
531*54fd6939SJiyong Parkdescriptors must be exported by the EL3 Runtime Software via the
532*54fd6939SJiyong Park``__CPU_OPS_START__`` and ``__CPU_OPS_END__`` linker symbols.
533*54fd6939SJiyong Park
534*54fd6939SJiyong ParkThe ``cpu_ops`` descriptors also include reset sequences and may include errata
535*54fd6939SJiyong Parkworkarounds for the CPU. The EL3 Runtime Software can choose to call this
536*54fd6939SJiyong Parkduring cold/warm reset if it does not implement its own reset sequence/errata
537*54fd6939SJiyong Parkworkarounds.
538*54fd6939SJiyong Park
539*54fd6939SJiyong Park--------------
540*54fd6939SJiyong Park
541*54fd6939SJiyong Park*Copyright (c) 2016-2020, Arm Limited and Contributors. All rights reserved.*
542*54fd6939SJiyong Park
543*54fd6939SJiyong Park.. _PSCI spec: http://infocenter.arm.com/help/topic/com.arm.doc.den0022c/DEN0022C_Power_State_Coordination_Interface.pdf
544*54fd6939SJiyong Park.. _SMCCC: https://developer.arm.com/docs/den0028/latest
545*54fd6939SJiyong Park.. _PSCI specification: http://infocenter.arm.com/help/topic/com.arm.doc.den0022c/DEN0022C_Power_State_Coordination_Interface.pdf
546*54fd6939SJiyong Park.. _PSCI Specification: http://infocenter.arm.com/help/topic/com.arm.doc.den0022c/DEN0022C_Power_State_Coordination_Interface.pdf
547