1*54fd6939SJiyong ParkInterrupt Management Framework 2*54fd6939SJiyong Park============================== 3*54fd6939SJiyong Park 4*54fd6939SJiyong ParkThis framework is responsible for managing interrupts routed to EL3. It also 5*54fd6939SJiyong Parkallows EL3 software to configure the interrupt routing behavior. Its main 6*54fd6939SJiyong Parkobjective is to implement the following two requirements. 7*54fd6939SJiyong Park 8*54fd6939SJiyong Park#. It should be possible to route interrupts meant to be handled by secure 9*54fd6939SJiyong Park software (Secure interrupts) to EL3, when execution is in non-secure state 10*54fd6939SJiyong Park (normal world). The framework should then take care of handing control of 11*54fd6939SJiyong Park the interrupt to either software in EL3 or Secure-EL1 depending upon the 12*54fd6939SJiyong Park software configuration and the GIC implementation. This requirement ensures 13*54fd6939SJiyong Park that secure interrupts are under the control of the secure software with 14*54fd6939SJiyong Park respect to their delivery and handling without the possibility of 15*54fd6939SJiyong Park intervention from non-secure software. 16*54fd6939SJiyong Park 17*54fd6939SJiyong Park#. It should be possible to route interrupts meant to be handled by 18*54fd6939SJiyong Park non-secure software (Non-secure interrupts) to the last executed exception 19*54fd6939SJiyong Park level in the normal world when the execution is in secure world at 20*54fd6939SJiyong Park exception levels lower than EL3. This could be done with or without the 21*54fd6939SJiyong Park knowledge of software executing in Secure-EL1/Secure-EL0. The choice of 22*54fd6939SJiyong Park approach should be governed by the secure software. This requirement 23*54fd6939SJiyong Park ensures that non-secure software is able to execute in tandem with the 24*54fd6939SJiyong Park secure software without overriding it. 25*54fd6939SJiyong Park 26*54fd6939SJiyong ParkConcepts 27*54fd6939SJiyong Park-------- 28*54fd6939SJiyong Park 29*54fd6939SJiyong ParkInterrupt types 30*54fd6939SJiyong Park~~~~~~~~~~~~~~~ 31*54fd6939SJiyong Park 32*54fd6939SJiyong ParkThe framework categorises an interrupt to be one of the following depending upon 33*54fd6939SJiyong Parkthe exception level(s) it is handled in. 34*54fd6939SJiyong Park 35*54fd6939SJiyong Park#. Secure EL1 interrupt. This type of interrupt can be routed to EL3 or 36*54fd6939SJiyong Park Secure-EL1 depending upon the security state of the current execution 37*54fd6939SJiyong Park context. It is always handled in Secure-EL1. 38*54fd6939SJiyong Park 39*54fd6939SJiyong Park#. Non-secure interrupt. This type of interrupt can be routed to EL3, 40*54fd6939SJiyong Park Secure-EL1, Non-secure EL1 or EL2 depending upon the security state of the 41*54fd6939SJiyong Park current execution context. It is always handled in either Non-secure EL1 42*54fd6939SJiyong Park or EL2. 43*54fd6939SJiyong Park 44*54fd6939SJiyong Park#. EL3 interrupt. This type of interrupt can be routed to EL3 or Secure-EL1 45*54fd6939SJiyong Park depending upon the security state of the current execution context. It is 46*54fd6939SJiyong Park always handled in EL3. 47*54fd6939SJiyong Park 48*54fd6939SJiyong ParkThe following constants define the various interrupt types in the framework 49*54fd6939SJiyong Parkimplementation. 50*54fd6939SJiyong Park 51*54fd6939SJiyong Park.. code:: c 52*54fd6939SJiyong Park 53*54fd6939SJiyong Park #define INTR_TYPE_S_EL1 0 54*54fd6939SJiyong Park #define INTR_TYPE_EL3 1 55*54fd6939SJiyong Park #define INTR_TYPE_NS 2 56*54fd6939SJiyong Park 57*54fd6939SJiyong ParkRouting model 58*54fd6939SJiyong Park~~~~~~~~~~~~~ 59*54fd6939SJiyong Park 60*54fd6939SJiyong ParkA type of interrupt can be either generated as an FIQ or an IRQ. The target 61*54fd6939SJiyong Parkexception level of an interrupt type is configured through the FIQ and IRQ bits 62*54fd6939SJiyong Parkin the Secure Configuration Register at EL3 (``SCR_EL3.FIQ`` and ``SCR_EL3.IRQ`` 63*54fd6939SJiyong Parkbits). When ``SCR_EL3.FIQ``\ =1, FIQs are routed to EL3. Otherwise they are routed 64*54fd6939SJiyong Parkto the First Exception Level (FEL) capable of handling interrupts. When 65*54fd6939SJiyong Park``SCR_EL3.IRQ``\ =1, IRQs are routed to EL3. Otherwise they are routed to the 66*54fd6939SJiyong ParkFEL. This register is configured independently by EL3 software for each security 67*54fd6939SJiyong Parkstate prior to entry into a lower exception level in that security state. 68*54fd6939SJiyong Park 69*54fd6939SJiyong ParkA routing model for a type of interrupt (generated as FIQ or IRQ) is defined as 70*54fd6939SJiyong Parkits target exception level for each security state. It is represented by a 71*54fd6939SJiyong Parksingle bit for each security state. A value of ``0`` means that the interrupt 72*54fd6939SJiyong Parkshould be routed to the FEL. A value of ``1`` means that the interrupt should be 73*54fd6939SJiyong Parkrouted to EL3. A routing model is applicable only when execution is not in EL3. 74*54fd6939SJiyong Park 75*54fd6939SJiyong ParkThe default routing model for an interrupt type is to route it to the FEL in 76*54fd6939SJiyong Parkeither security state. 77*54fd6939SJiyong Park 78*54fd6939SJiyong ParkValid routing models 79*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~ 80*54fd6939SJiyong Park 81*54fd6939SJiyong ParkThe framework considers certain routing models for each type of interrupt to be 82*54fd6939SJiyong Parkincorrect as they conflict with the requirements mentioned in Section 1. The 83*54fd6939SJiyong Parkfollowing sub-sections describe all the possible routing models and specify 84*54fd6939SJiyong Parkwhich ones are valid or invalid. EL3 interrupts are currently supported only 85*54fd6939SJiyong Parkfor GIC version 3.0 (Arm GICv3) and only the Secure-EL1 and Non-secure interrupt 86*54fd6939SJiyong Parktypes are supported for GIC version 2.0 (Arm GICv2) (see `Assumptions in 87*54fd6939SJiyong ParkInterrupt Management Framework`_). The terminology used in the following 88*54fd6939SJiyong Parksub-sections is explained below. 89*54fd6939SJiyong Park 90*54fd6939SJiyong Park#. **CSS**. Current Security State. ``0`` when secure and ``1`` when non-secure 91*54fd6939SJiyong Park 92*54fd6939SJiyong Park#. **TEL3**. Target Exception Level 3. ``0`` when targeted to the FEL. ``1`` when 93*54fd6939SJiyong Park targeted to EL3. 94*54fd6939SJiyong Park 95*54fd6939SJiyong ParkSecure-EL1 interrupts 96*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^ 97*54fd6939SJiyong Park 98*54fd6939SJiyong Park#. **CSS=0, TEL3=0**. Interrupt is routed to the FEL when execution is in 99*54fd6939SJiyong Park secure state. This is a valid routing model as secure software is in 100*54fd6939SJiyong Park control of handling secure interrupts. 101*54fd6939SJiyong Park 102*54fd6939SJiyong Park#. **CSS=0, TEL3=1**. Interrupt is routed to EL3 when execution is in secure 103*54fd6939SJiyong Park state. This is a valid routing model as secure software in EL3 can 104*54fd6939SJiyong Park handover the interrupt to Secure-EL1 for handling. 105*54fd6939SJiyong Park 106*54fd6939SJiyong Park#. **CSS=1, TEL3=0**. Interrupt is routed to the FEL when execution is in 107*54fd6939SJiyong Park non-secure state. This is an invalid routing model as a secure interrupt 108*54fd6939SJiyong Park is not visible to the secure software which violates the motivation behind 109*54fd6939SJiyong Park the Arm Security Extensions. 110*54fd6939SJiyong Park 111*54fd6939SJiyong Park#. **CSS=1, TEL3=1**. Interrupt is routed to EL3 when execution is in 112*54fd6939SJiyong Park non-secure state. This is a valid routing model as secure software in EL3 113*54fd6939SJiyong Park can handover the interrupt to Secure-EL1 for handling. 114*54fd6939SJiyong Park 115*54fd6939SJiyong ParkNon-secure interrupts 116*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^ 117*54fd6939SJiyong Park 118*54fd6939SJiyong Park#. **CSS=0, TEL3=0**. Interrupt is routed to the FEL when execution is in 119*54fd6939SJiyong Park secure state. This allows the secure software to trap non-secure 120*54fd6939SJiyong Park interrupts, perform its book-keeping and hand the interrupt to the 121*54fd6939SJiyong Park non-secure software through EL3. This is a valid routing model as secure 122*54fd6939SJiyong Park software is in control of how its execution is preempted by non-secure 123*54fd6939SJiyong Park interrupts. 124*54fd6939SJiyong Park 125*54fd6939SJiyong Park#. **CSS=0, TEL3=1**. Interrupt is routed to EL3 when execution is in secure 126*54fd6939SJiyong Park state. This is a valid routing model as secure software in EL3 can save 127*54fd6939SJiyong Park the state of software in Secure-EL1/Secure-EL0 before handing the 128*54fd6939SJiyong Park interrupt to non-secure software. This model requires additional 129*54fd6939SJiyong Park coordination between Secure-EL1 and EL3 software to ensure that the 130*54fd6939SJiyong Park former's state is correctly saved by the latter. 131*54fd6939SJiyong Park 132*54fd6939SJiyong Park#. **CSS=1, TEL3=0**. Interrupt is routed to FEL when execution is in 133*54fd6939SJiyong Park non-secure state. This is a valid routing model as a non-secure interrupt 134*54fd6939SJiyong Park is handled by non-secure software. 135*54fd6939SJiyong Park 136*54fd6939SJiyong Park#. **CSS=1, TEL3=1**. Interrupt is routed to EL3 when execution is in 137*54fd6939SJiyong Park non-secure state. This is an invalid routing model as there is no valid 138*54fd6939SJiyong Park reason to route the interrupt to EL3 software and then hand it back to 139*54fd6939SJiyong Park non-secure software for handling. 140*54fd6939SJiyong Park 141*54fd6939SJiyong Park.. _EL3 interrupts: 142*54fd6939SJiyong Park 143*54fd6939SJiyong ParkEL3 interrupts 144*54fd6939SJiyong Park^^^^^^^^^^^^^^ 145*54fd6939SJiyong Park 146*54fd6939SJiyong Park#. **CSS=0, TEL3=0**. Interrupt is routed to the FEL when execution is in 147*54fd6939SJiyong Park Secure-EL1/Secure-EL0. This is a valid routing model as secure software 148*54fd6939SJiyong Park in Secure-EL1/Secure-EL0 is in control of how its execution is preempted 149*54fd6939SJiyong Park by EL3 interrupt and can handover the interrupt to EL3 for handling. 150*54fd6939SJiyong Park 151*54fd6939SJiyong Park However, when ``EL3_EXCEPTION_HANDLING`` is ``1``, this routing model is 152*54fd6939SJiyong Park invalid as EL3 interrupts are unconditionally routed to EL3, and EL3 153*54fd6939SJiyong Park interrupts will always preempt Secure EL1/EL0 execution. See :ref:`exception 154*54fd6939SJiyong Park handling<interrupt-handling>` documentation. 155*54fd6939SJiyong Park 156*54fd6939SJiyong Park#. **CSS=0, TEL3=1**. Interrupt is routed to EL3 when execution is in 157*54fd6939SJiyong Park Secure-EL1/Secure-EL0. This is a valid routing model as secure software 158*54fd6939SJiyong Park in EL3 can handle the interrupt. 159*54fd6939SJiyong Park 160*54fd6939SJiyong Park#. **CSS=1, TEL3=0**. Interrupt is routed to the FEL when execution is in 161*54fd6939SJiyong Park non-secure state. This is an invalid routing model as a secure interrupt 162*54fd6939SJiyong Park is not visible to the secure software which violates the motivation behind 163*54fd6939SJiyong Park the Arm Security Extensions. 164*54fd6939SJiyong Park 165*54fd6939SJiyong Park#. **CSS=1, TEL3=1**. Interrupt is routed to EL3 when execution is in 166*54fd6939SJiyong Park non-secure state. This is a valid routing model as secure software in EL3 167*54fd6939SJiyong Park can handle the interrupt. 168*54fd6939SJiyong Park 169*54fd6939SJiyong ParkMapping of interrupt type to signal 170*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 171*54fd6939SJiyong Park 172*54fd6939SJiyong ParkThe framework is meant to work with any interrupt controller implemented by a 173*54fd6939SJiyong Parkplatform. A interrupt controller could generate a type of interrupt as either an 174*54fd6939SJiyong ParkFIQ or IRQ signal to the CPU depending upon the current security state. The 175*54fd6939SJiyong Parkmapping between the type and signal is known only to the platform. The framework 176*54fd6939SJiyong Parkuses this information to determine whether the IRQ or the FIQ bit should be 177*54fd6939SJiyong Parkprogrammed in ``SCR_EL3`` while applying the routing model for a type of 178*54fd6939SJiyong Parkinterrupt. The platform provides this information through the 179*54fd6939SJiyong Park``plat_interrupt_type_to_line()`` API (described in the 180*54fd6939SJiyong Park:ref:`Porting Guide`). For example, on the FVP port when the platform uses an 181*54fd6939SJiyong ParkArm GICv2 interrupt controller, Secure-EL1 interrupts are signaled through the 182*54fd6939SJiyong ParkFIQ signal while Non-secure interrupts are signaled through the IRQ signal. 183*54fd6939SJiyong ParkThis applies when execution is in either security state. 184*54fd6939SJiyong Park 185*54fd6939SJiyong ParkEffect of mapping of several interrupt types to one signal 186*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 187*54fd6939SJiyong Park 188*54fd6939SJiyong ParkIt should be noted that if more than one interrupt type maps to a single 189*54fd6939SJiyong Parkinterrupt signal, and if any one of the interrupt type sets **TEL3=1** for a 190*54fd6939SJiyong Parkparticular security state, then interrupt signal will be routed to EL3 when in 191*54fd6939SJiyong Parkthat security state. This means that all the other interrupt types using the 192*54fd6939SJiyong Parksame interrupt signal will be forced to the same routing model. This should be 193*54fd6939SJiyong Parkborne in mind when choosing the routing model for an interrupt type. 194*54fd6939SJiyong Park 195*54fd6939SJiyong ParkFor example, in Arm GICv3, when the execution context is Secure-EL1/ 196*54fd6939SJiyong ParkSecure-EL0, both the EL3 and the non secure interrupt types map to the FIQ 197*54fd6939SJiyong Parksignal. So if either one of the interrupt type sets the routing model so 198*54fd6939SJiyong Parkthat **TEL3=1** when **CSS=0**, the FIQ bit in ``SCR_EL3`` will be programmed to 199*54fd6939SJiyong Parkroute the FIQ signal to EL3 when executing in Secure-EL1/Secure-EL0, thereby 200*54fd6939SJiyong Parkeffectively routing the other interrupt type also to EL3. 201*54fd6939SJiyong Park 202*54fd6939SJiyong ParkAssumptions in Interrupt Management Framework 203*54fd6939SJiyong Park--------------------------------------------- 204*54fd6939SJiyong Park 205*54fd6939SJiyong ParkThe framework makes the following assumptions to simplify its implementation. 206*54fd6939SJiyong Park 207*54fd6939SJiyong Park#. Although the framework has support for 2 types of secure interrupts (EL3 208*54fd6939SJiyong Park and Secure-EL1 interrupt), only interrupt controller architectures 209*54fd6939SJiyong Park like Arm GICv3 has architectural support for EL3 interrupts in the form of 210*54fd6939SJiyong Park Group 0 interrupts. In Arm GICv2, all secure interrupts are assumed to be 211*54fd6939SJiyong Park handled in Secure-EL1. They can be delivered to Secure-EL1 via EL3 but they 212*54fd6939SJiyong Park cannot be handled in EL3. 213*54fd6939SJiyong Park 214*54fd6939SJiyong Park#. Interrupt exceptions (``PSTATE.I`` and ``F`` bits) are masked during execution 215*54fd6939SJiyong Park in EL3. 216*54fd6939SJiyong Park 217*54fd6939SJiyong Park#. Interrupt management: the following sections describe how interrupts are 218*54fd6939SJiyong Park managed by the interrupt handling framework. This entails: 219*54fd6939SJiyong Park 220*54fd6939SJiyong Park #. Providing an interface to allow registration of a handler and 221*54fd6939SJiyong Park specification of the routing model for a type of interrupt. 222*54fd6939SJiyong Park 223*54fd6939SJiyong Park #. Implementing support to hand control of an interrupt type to its 224*54fd6939SJiyong Park registered handler when the interrupt is generated. 225*54fd6939SJiyong Park 226*54fd6939SJiyong ParkBoth aspects of interrupt management involve various components in the secure 227*54fd6939SJiyong Parksoftware stack spanning from EL3 to Secure-EL1. These components are described 228*54fd6939SJiyong Parkin the section `Software components`_. The framework stores information 229*54fd6939SJiyong Parkassociated with each type of interrupt in the following data structure. 230*54fd6939SJiyong Park 231*54fd6939SJiyong Park.. code:: c 232*54fd6939SJiyong Park 233*54fd6939SJiyong Park typedef struct intr_type_desc { 234*54fd6939SJiyong Park interrupt_type_handler_t handler; 235*54fd6939SJiyong Park uint32_t flags; 236*54fd6939SJiyong Park uint32_t scr_el3[2]; 237*54fd6939SJiyong Park } intr_type_desc_t; 238*54fd6939SJiyong Park 239*54fd6939SJiyong ParkThe ``flags`` field stores the routing model for the interrupt type in 240*54fd6939SJiyong Parkbits[1:0]. Bit[0] stores the routing model when execution is in the secure 241*54fd6939SJiyong Parkstate. Bit[1] stores the routing model when execution is in the non-secure 242*54fd6939SJiyong Parkstate. As mentioned in Section `Routing model`_, a value of ``0`` implies that 243*54fd6939SJiyong Parkthe interrupt should be targeted to the FEL. A value of ``1`` implies that it 244*54fd6939SJiyong Parkshould be targeted to EL3. The remaining bits are reserved and SBZ. The helper 245*54fd6939SJiyong Parkmacro ``set_interrupt_rm_flag()`` should be used to set the bits in the 246*54fd6939SJiyong Park``flags`` parameter. 247*54fd6939SJiyong Park 248*54fd6939SJiyong ParkThe ``scr_el3[2]`` field also stores the routing model but as a mapping of the 249*54fd6939SJiyong Parkmodel in the ``flags`` field to the corresponding bit in the ``SCR_EL3`` for each 250*54fd6939SJiyong Parksecurity state. 251*54fd6939SJiyong Park 252*54fd6939SJiyong ParkThe framework also depends upon the platform port to configure the interrupt 253*54fd6939SJiyong Parkcontroller to distinguish between secure and non-secure interrupts. The platform 254*54fd6939SJiyong Parkis expected to be aware of the secure devices present in the system and their 255*54fd6939SJiyong Parkassociated interrupt numbers. It should configure the interrupt controller to 256*54fd6939SJiyong Parkenable the secure interrupts, ensure that their priority is always higher than 257*54fd6939SJiyong Parkthe non-secure interrupts and target them to the primary CPU. It should also 258*54fd6939SJiyong Parkexport the interface described in the :ref:`Porting Guide` to enable 259*54fd6939SJiyong Parkhandling of interrupts. 260*54fd6939SJiyong Park 261*54fd6939SJiyong ParkIn the remainder of this document, for the sake of simplicity a Arm GICv2 system 262*54fd6939SJiyong Parkis considered and it is assumed that the FIQ signal is used to generate Secure-EL1 263*54fd6939SJiyong Parkinterrupts and the IRQ signal is used to generate non-secure interrupts in either 264*54fd6939SJiyong Parksecurity state. EL3 interrupts are not considered. 265*54fd6939SJiyong Park 266*54fd6939SJiyong ParkSoftware components 267*54fd6939SJiyong Park------------------- 268*54fd6939SJiyong Park 269*54fd6939SJiyong ParkRoles and responsibilities for interrupt management are sub-divided between the 270*54fd6939SJiyong Parkfollowing components of software running in EL3 and Secure-EL1. Each component is 271*54fd6939SJiyong Parkbriefly described below. 272*54fd6939SJiyong Park 273*54fd6939SJiyong Park#. EL3 Runtime Firmware. This component is common to all ports of TF-A. 274*54fd6939SJiyong Park 275*54fd6939SJiyong Park#. Secure Payload Dispatcher (SPD) service. This service interfaces with the 276*54fd6939SJiyong Park Secure Payload (SP) software which runs in Secure-EL1/Secure-EL0 and is 277*54fd6939SJiyong Park responsible for switching execution between secure and non-secure states. 278*54fd6939SJiyong Park A switch is triggered by a Secure Monitor Call and it uses the APIs 279*54fd6939SJiyong Park exported by the Context management library to implement this functionality. 280*54fd6939SJiyong Park Switching execution between the two security states is a requirement for 281*54fd6939SJiyong Park interrupt management as well. This results in a significant dependency on 282*54fd6939SJiyong Park the SPD service. TF-A implements an example Test Secure Payload Dispatcher 283*54fd6939SJiyong Park (TSPD) service. 284*54fd6939SJiyong Park 285*54fd6939SJiyong Park An SPD service plugs into the EL3 runtime firmware and could be common to 286*54fd6939SJiyong Park some ports of TF-A. 287*54fd6939SJiyong Park 288*54fd6939SJiyong Park#. Secure Payload (SP). On a production system, the Secure Payload corresponds 289*54fd6939SJiyong Park to a Secure OS which runs in Secure-EL1/Secure-EL0. It interfaces with the 290*54fd6939SJiyong Park SPD service to manage communication with non-secure software. TF-A 291*54fd6939SJiyong Park implements an example secure payload called Test Secure Payload (TSP) 292*54fd6939SJiyong Park which runs only in Secure-EL1. 293*54fd6939SJiyong Park 294*54fd6939SJiyong Park A Secure payload implementation could be common to some ports of TF-A, 295*54fd6939SJiyong Park just like the SPD service. 296*54fd6939SJiyong Park 297*54fd6939SJiyong ParkInterrupt registration 298*54fd6939SJiyong Park---------------------- 299*54fd6939SJiyong Park 300*54fd6939SJiyong ParkThis section describes in detail the role of each software component (see 301*54fd6939SJiyong Park`Software components`_) during the registration of a handler for an interrupt 302*54fd6939SJiyong Parktype. 303*54fd6939SJiyong Park 304*54fd6939SJiyong Park.. _el3-runtime-firmware: 305*54fd6939SJiyong Park 306*54fd6939SJiyong ParkEL3 runtime firmware 307*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~ 308*54fd6939SJiyong Park 309*54fd6939SJiyong ParkThis component declares the following prototype for a handler of an interrupt type. 310*54fd6939SJiyong Park 311*54fd6939SJiyong Park.. code:: c 312*54fd6939SJiyong Park 313*54fd6939SJiyong Park typedef uint64_t (*interrupt_type_handler_t)(uint32_t id, 314*54fd6939SJiyong Park uint32_t flags, 315*54fd6939SJiyong Park void *handle, 316*54fd6939SJiyong Park void *cookie); 317*54fd6939SJiyong Park 318*54fd6939SJiyong ParkThe ``id`` is parameter is reserved and could be used in the future for passing 319*54fd6939SJiyong Parkthe interrupt id of the highest pending interrupt only if there is a foolproof 320*54fd6939SJiyong Parkway of determining the id. Currently it contains ``INTR_ID_UNAVAILABLE``. 321*54fd6939SJiyong Park 322*54fd6939SJiyong ParkThe ``flags`` parameter contains miscellaneous information as follows. 323*54fd6939SJiyong Park 324*54fd6939SJiyong Park#. Security state, bit[0]. This bit indicates the security state of the lower 325*54fd6939SJiyong Park exception level when the interrupt was generated. A value of ``1`` means 326*54fd6939SJiyong Park that it was in the non-secure state. A value of ``0`` indicates that it was 327*54fd6939SJiyong Park in the secure state. This bit can be used by the handler to ensure that 328*54fd6939SJiyong Park interrupt was generated and routed as per the routing model specified 329*54fd6939SJiyong Park during registration. 330*54fd6939SJiyong Park 331*54fd6939SJiyong Park#. Reserved, bits[31:1]. The remaining bits are reserved for future use. 332*54fd6939SJiyong Park 333*54fd6939SJiyong ParkThe ``handle`` parameter points to the ``cpu_context`` structure of the current CPU 334*54fd6939SJiyong Parkfor the security state specified in the ``flags`` parameter. 335*54fd6939SJiyong Park 336*54fd6939SJiyong ParkOnce the handler routine completes, execution will return to either the secure 337*54fd6939SJiyong Parkor non-secure state. The handler routine must return a pointer to 338*54fd6939SJiyong Park``cpu_context`` structure of the current CPU for the target security state. On 339*54fd6939SJiyong ParkAArch64, this return value is currently ignored by the caller as the 340*54fd6939SJiyong Parkappropriate ``cpu_context`` to be used is expected to be set by the handler 341*54fd6939SJiyong Parkvia the context management library APIs. 342*54fd6939SJiyong ParkA portable interrupt handler implementation must set the target context both in 343*54fd6939SJiyong Parkthe structure pointed to by the returned pointer and via the context management 344*54fd6939SJiyong Parklibrary APIs. The handler should treat all error conditions as critical errors 345*54fd6939SJiyong Parkand take appropriate action within its implementation e.g. use assertion 346*54fd6939SJiyong Parkfailures. 347*54fd6939SJiyong Park 348*54fd6939SJiyong ParkThe runtime firmware provides the following API for registering a handler for a 349*54fd6939SJiyong Parkparticular type of interrupt. A Secure Payload Dispatcher service should use 350*54fd6939SJiyong Parkthis API to register a handler for Secure-EL1 and optionally for non-secure 351*54fd6939SJiyong Parkinterrupts. This API also requires the caller to specify the routing model for 352*54fd6939SJiyong Parkthe type of interrupt. 353*54fd6939SJiyong Park 354*54fd6939SJiyong Park.. code:: c 355*54fd6939SJiyong Park 356*54fd6939SJiyong Park int32_t register_interrupt_type_handler(uint32_t type, 357*54fd6939SJiyong Park interrupt_type_handler handler, 358*54fd6939SJiyong Park uint64_t flags); 359*54fd6939SJiyong Park 360*54fd6939SJiyong ParkThe ``type`` parameter can be one of the three interrupt types listed above i.e. 361*54fd6939SJiyong Park``INTR_TYPE_S_EL1``, ``INTR_TYPE_NS`` & ``INTR_TYPE_EL3``. The ``flags`` parameter 362*54fd6939SJiyong Parkis as described in Section 2. 363*54fd6939SJiyong Park 364*54fd6939SJiyong ParkThe function will return ``0`` upon a successful registration. It will return 365*54fd6939SJiyong Park``-EALREADY`` in case a handler for the interrupt type has already been 366*54fd6939SJiyong Parkregistered. If the ``type`` is unrecognised or the ``flags`` or the ``handler`` are 367*54fd6939SJiyong Parkinvalid it will return ``-EINVAL``. 368*54fd6939SJiyong Park 369*54fd6939SJiyong ParkInterrupt routing is governed by the configuration of the ``SCR_EL3.FIQ/IRQ`` bits 370*54fd6939SJiyong Parkprior to entry into a lower exception level in either security state. The 371*54fd6939SJiyong Parkcontext management library maintains a copy of the ``SCR_EL3`` system register for 372*54fd6939SJiyong Parkeach security state in the ``cpu_context`` structure of each CPU. It exports the 373*54fd6939SJiyong Parkfollowing APIs to let EL3 Runtime Firmware program and retrieve the routing 374*54fd6939SJiyong Parkmodel for each security state for the current CPU. The value of ``SCR_EL3`` stored 375*54fd6939SJiyong Parkin the ``cpu_context`` is used by the ``el3_exit()`` function to program the 376*54fd6939SJiyong Park``SCR_EL3`` register prior to returning from the EL3 exception level. 377*54fd6939SJiyong Park 378*54fd6939SJiyong Park.. code:: c 379*54fd6939SJiyong Park 380*54fd6939SJiyong Park uint32_t cm_get_scr_el3(uint32_t security_state); 381*54fd6939SJiyong Park void cm_write_scr_el3_bit(uint32_t security_state, 382*54fd6939SJiyong Park uint32_t bit_pos, 383*54fd6939SJiyong Park uint32_t value); 384*54fd6939SJiyong Park 385*54fd6939SJiyong Park``cm_get_scr_el3()`` returns the value of the ``SCR_EL3`` register for the specified 386*54fd6939SJiyong Parksecurity state of the current CPU. ``cm_write_scr_el3_bit()`` writes a ``0`` or ``1`` 387*54fd6939SJiyong Parkto the bit specified by ``bit_pos``. ``register_interrupt_type_handler()`` invokes 388*54fd6939SJiyong Park``set_routing_model()`` API which programs the ``SCR_EL3`` according to the routing 389*54fd6939SJiyong Parkmodel using the ``cm_get_scr_el3()`` and ``cm_write_scr_el3_bit()`` APIs. 390*54fd6939SJiyong Park 391*54fd6939SJiyong ParkIt is worth noting that in the current implementation of the framework, the EL3 392*54fd6939SJiyong Parkruntime firmware is responsible for programming the routing model. The SPD is 393*54fd6939SJiyong Parkresponsible for ensuring that the routing model has been adhered to upon 394*54fd6939SJiyong Parkreceiving an interrupt. 395*54fd6939SJiyong Park 396*54fd6939SJiyong Park.. _spd-int-registration: 397*54fd6939SJiyong Park 398*54fd6939SJiyong ParkSecure payload dispatcher 399*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~ 400*54fd6939SJiyong Park 401*54fd6939SJiyong ParkA SPD service is responsible for determining and maintaining the interrupt 402*54fd6939SJiyong Parkrouting model supported by itself and the Secure Payload. It is also responsible 403*54fd6939SJiyong Parkfor ferrying interrupts between secure and non-secure software depending upon 404*54fd6939SJiyong Parkthe routing model. It could determine the routing model at build time or at 405*54fd6939SJiyong Parkruntime. It must use this information to register a handler for each interrupt 406*54fd6939SJiyong Parktype using the ``register_interrupt_type_handler()`` API in EL3 runtime firmware. 407*54fd6939SJiyong Park 408*54fd6939SJiyong ParkIf the routing model is not known to the SPD service at build time, then it must 409*54fd6939SJiyong Parkbe provided by the SP as the result of its initialisation. The SPD should 410*54fd6939SJiyong Parkprogram the routing model only after SP initialisation has completed e.g. in the 411*54fd6939SJiyong ParkSPD initialisation function pointed to by the ``bl32_init`` variable. 412*54fd6939SJiyong Park 413*54fd6939SJiyong ParkThe SPD should determine the mechanism to pass control to the Secure Payload 414*54fd6939SJiyong Parkafter receiving an interrupt from the EL3 runtime firmware. This information 415*54fd6939SJiyong Parkcould either be provided to the SPD service at build time or by the SP at 416*54fd6939SJiyong Parkruntime. 417*54fd6939SJiyong Park 418*54fd6939SJiyong ParkTest secure payload dispatcher behavior 419*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 420*54fd6939SJiyong Park 421*54fd6939SJiyong Park.. note:: 422*54fd6939SJiyong Park Where this document discusses ``TSP_NS_INTR_ASYNC_PREEMPT`` as being 423*54fd6939SJiyong Park ``1``, the same results also apply when ``EL3_EXCEPTION_HANDLING`` is ``1``. 424*54fd6939SJiyong Park 425*54fd6939SJiyong ParkThe TSPD only handles Secure-EL1 interrupts and is provided with the following 426*54fd6939SJiyong Parkrouting model at build time. 427*54fd6939SJiyong Park 428*54fd6939SJiyong Park- Secure-EL1 interrupts are routed to EL3 when execution is in non-secure 429*54fd6939SJiyong Park state and are routed to the FEL when execution is in the secure state 430*54fd6939SJiyong Park i.e **CSS=0, TEL3=0** & **CSS=1, TEL3=1** for Secure-EL1 interrupts 431*54fd6939SJiyong Park 432*54fd6939SJiyong Park- When the build flag ``TSP_NS_INTR_ASYNC_PREEMPT`` is zero, the default routing 433*54fd6939SJiyong Park model is used for non-secure interrupts. They are routed to the FEL in 434*54fd6939SJiyong Park either security state i.e **CSS=0, TEL3=0** & **CSS=1, TEL3=0** for 435*54fd6939SJiyong Park Non-secure interrupts. 436*54fd6939SJiyong Park 437*54fd6939SJiyong Park- When the build flag ``TSP_NS_INTR_ASYNC_PREEMPT`` is defined to 1, then the 438*54fd6939SJiyong Park non secure interrupts are routed to EL3 when execution is in secure state 439*54fd6939SJiyong Park i.e **CSS=0, TEL3=1** for non-secure interrupts. This effectively preempts 440*54fd6939SJiyong Park Secure-EL1. The default routing model is used for non secure interrupts in 441*54fd6939SJiyong Park non-secure state. i.e **CSS=1, TEL3=0**. 442*54fd6939SJiyong Park 443*54fd6939SJiyong ParkIt performs the following actions in the ``tspd_init()`` function to fulfill the 444*54fd6939SJiyong Parkrequirements mentioned earlier. 445*54fd6939SJiyong Park 446*54fd6939SJiyong Park#. It passes control to the Test Secure Payload to perform its 447*54fd6939SJiyong Park initialisation. The TSP provides the address of the vector table 448*54fd6939SJiyong Park ``tsp_vectors`` in the SP which also includes the handler for Secure-EL1 449*54fd6939SJiyong Park interrupts in the ``sel1_intr_entry`` field. The TSPD passes control to the TSP at 450*54fd6939SJiyong Park this address when it receives a Secure-EL1 interrupt. 451*54fd6939SJiyong Park 452*54fd6939SJiyong Park The handover agreement between the TSP and the TSPD requires that the TSPD 453*54fd6939SJiyong Park masks all interrupts (``PSTATE.DAIF`` bits) when it calls 454*54fd6939SJiyong Park ``tsp_sel1_intr_entry()``. The TSP has to preserve the callee saved general 455*54fd6939SJiyong Park purpose, SP_EL1/Secure-EL0, LR, VFP and system registers. It can use 456*54fd6939SJiyong Park ``x0-x18`` to enable its C runtime. 457*54fd6939SJiyong Park 458*54fd6939SJiyong Park#. The TSPD implements a handler function for Secure-EL1 interrupts. This 459*54fd6939SJiyong Park function is registered with the EL3 runtime firmware using the 460*54fd6939SJiyong Park ``register_interrupt_type_handler()`` API as follows 461*54fd6939SJiyong Park 462*54fd6939SJiyong Park .. code:: c 463*54fd6939SJiyong Park 464*54fd6939SJiyong Park /* Forward declaration */ 465*54fd6939SJiyong Park interrupt_type_handler tspd_secure_el1_interrupt_handler; 466*54fd6939SJiyong Park int32_t rc, flags = 0; 467*54fd6939SJiyong Park set_interrupt_rm_flag(flags, NON_SECURE); 468*54fd6939SJiyong Park rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 469*54fd6939SJiyong Park tspd_secure_el1_interrupt_handler, 470*54fd6939SJiyong Park flags); 471*54fd6939SJiyong Park if (rc) 472*54fd6939SJiyong Park panic(); 473*54fd6939SJiyong Park 474*54fd6939SJiyong Park#. When the build flag ``TSP_NS_INTR_ASYNC_PREEMPT`` is defined to 1, the TSPD 475*54fd6939SJiyong Park implements a handler function for non-secure interrupts. This function is 476*54fd6939SJiyong Park registered with the EL3 runtime firmware using the 477*54fd6939SJiyong Park ``register_interrupt_type_handler()`` API as follows 478*54fd6939SJiyong Park 479*54fd6939SJiyong Park .. code:: c 480*54fd6939SJiyong Park 481*54fd6939SJiyong Park /* Forward declaration */ 482*54fd6939SJiyong Park interrupt_type_handler tspd_ns_interrupt_handler; 483*54fd6939SJiyong Park int32_t rc, flags = 0; 484*54fd6939SJiyong Park set_interrupt_rm_flag(flags, SECURE); 485*54fd6939SJiyong Park rc = register_interrupt_type_handler(INTR_TYPE_NS, 486*54fd6939SJiyong Park tspd_ns_interrupt_handler, 487*54fd6939SJiyong Park flags); 488*54fd6939SJiyong Park if (rc) 489*54fd6939SJiyong Park panic(); 490*54fd6939SJiyong Park 491*54fd6939SJiyong Park.. _sp-int-registration: 492*54fd6939SJiyong Park 493*54fd6939SJiyong ParkSecure payload 494*54fd6939SJiyong Park~~~~~~~~~~~~~~ 495*54fd6939SJiyong Park 496*54fd6939SJiyong ParkA Secure Payload must implement an interrupt handling framework at Secure-EL1 497*54fd6939SJiyong Park(Secure-EL1 IHF) to support its chosen interrupt routing model. Secure payload 498*54fd6939SJiyong Parkexecution will alternate between the below cases. 499*54fd6939SJiyong Park 500*54fd6939SJiyong Park#. In the code where IRQ, FIQ or both interrupts are enabled, if an interrupt 501*54fd6939SJiyong Park type is targeted to the FEL, then it will be routed to the Secure-EL1 502*54fd6939SJiyong Park exception vector table. This is defined as the **asynchronous mode** of 503*54fd6939SJiyong Park handling interrupts. This mode applies to both Secure-EL1 and non-secure 504*54fd6939SJiyong Park interrupts. 505*54fd6939SJiyong Park 506*54fd6939SJiyong Park#. In the code where both interrupts are disabled, if an interrupt type is 507*54fd6939SJiyong Park targeted to the FEL, then execution will eventually migrate to the 508*54fd6939SJiyong Park non-secure state. Any non-secure interrupts will be handled as described 509*54fd6939SJiyong Park in the routing model where **CSS=1 and TEL3=0**. Secure-EL1 interrupts 510*54fd6939SJiyong Park will be routed to EL3 (as per the routing model where **CSS=1 and 511*54fd6939SJiyong Park TEL3=1**) where the SPD service will hand them to the SP. This is defined 512*54fd6939SJiyong Park as the **synchronous mode** of handling interrupts. 513*54fd6939SJiyong Park 514*54fd6939SJiyong ParkThe interrupt handling framework implemented by the SP should support one or 515*54fd6939SJiyong Parkboth these interrupt handling models depending upon the chosen routing model. 516*54fd6939SJiyong Park 517*54fd6939SJiyong ParkThe following list briefly describes how the choice of a valid routing model 518*54fd6939SJiyong Park(see `Valid routing models`_) effects the implementation of the Secure-EL1 519*54fd6939SJiyong ParkIHF. If the choice of the interrupt routing model is not known to the SPD 520*54fd6939SJiyong Parkservice at compile time, then the SP should pass this information to the SPD 521*54fd6939SJiyong Parkservice at runtime during its initialisation phase. 522*54fd6939SJiyong Park 523*54fd6939SJiyong ParkAs mentioned earlier, an Arm GICv2 system is considered and it is assumed that 524*54fd6939SJiyong Parkthe FIQ signal is used to generate Secure-EL1 interrupts and the IRQ signal 525*54fd6939SJiyong Parkis used to generate non-secure interrupts in either security state. 526*54fd6939SJiyong Park 527*54fd6939SJiyong ParkSecure payload IHF design w.r.t secure-EL1 interrupts 528*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 529*54fd6939SJiyong Park 530*54fd6939SJiyong Park#. **CSS=0, TEL3=0**. If ``PSTATE.F=0``, Secure-EL1 interrupts will be 531*54fd6939SJiyong Park triggered at one of the Secure-EL1 FIQ exception vectors. The Secure-EL1 532*54fd6939SJiyong Park IHF should implement support for handling FIQ interrupts asynchronously. 533*54fd6939SJiyong Park 534*54fd6939SJiyong Park If ``PSTATE.F=1`` then Secure-EL1 interrupts will be handled as per the 535*54fd6939SJiyong Park synchronous interrupt handling model. The SP could implement this scenario 536*54fd6939SJiyong Park by exporting a separate entrypoint for Secure-EL1 interrupts to the SPD 537*54fd6939SJiyong Park service during the registration phase. The SPD service would also need to 538*54fd6939SJiyong Park know the state of the system, general purpose and the ``PSTATE`` registers 539*54fd6939SJiyong Park in which it should arrange to return execution to the SP. The SP should 540*54fd6939SJiyong Park provide this information in an implementation defined way during the 541*54fd6939SJiyong Park registration phase if it is not known to the SPD service at build time. 542*54fd6939SJiyong Park 543*54fd6939SJiyong Park#. **CSS=1, TEL3=1**. Interrupts are routed to EL3 when execution is in 544*54fd6939SJiyong Park non-secure state. They should be handled through the synchronous interrupt 545*54fd6939SJiyong Park handling model as described in 1. above. 546*54fd6939SJiyong Park 547*54fd6939SJiyong Park#. **CSS=0, TEL3=1**. Secure-EL1 interrupts are routed to EL3 when execution 548*54fd6939SJiyong Park is in secure state. They will not be visible to the SP. The ``PSTATE.F`` bit 549*54fd6939SJiyong Park in Secure-EL1/Secure-EL0 will not mask FIQs. The EL3 runtime firmware will 550*54fd6939SJiyong Park call the handler registered by the SPD service for Secure-EL1 interrupts. 551*54fd6939SJiyong Park Secure-EL1 IHF should then handle all Secure-EL1 interrupt through the 552*54fd6939SJiyong Park synchronous interrupt handling model described in 1. above. 553*54fd6939SJiyong Park 554*54fd6939SJiyong ParkSecure payload IHF design w.r.t non-secure interrupts 555*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 556*54fd6939SJiyong Park 557*54fd6939SJiyong Park#. **CSS=0, TEL3=0**. If ``PSTATE.I=0``, non-secure interrupts will be 558*54fd6939SJiyong Park triggered at one of the Secure-EL1 IRQ exception vectors . The Secure-EL1 559*54fd6939SJiyong Park IHF should co-ordinate with the SPD service to transfer execution to the 560*54fd6939SJiyong Park non-secure state where the interrupt should be handled e.g the SP could 561*54fd6939SJiyong Park allocate a function identifier to issue a SMC64 or SMC32 to the SPD 562*54fd6939SJiyong Park service which indicates that the SP execution has been preempted by a 563*54fd6939SJiyong Park non-secure interrupt. If this function identifier is not known to the SPD 564*54fd6939SJiyong Park service at compile time then the SP could provide it during the 565*54fd6939SJiyong Park registration phase. 566*54fd6939SJiyong Park 567*54fd6939SJiyong Park If ``PSTATE.I=1`` then the non-secure interrupt will pend until execution 568*54fd6939SJiyong Park resumes in the non-secure state. 569*54fd6939SJiyong Park 570*54fd6939SJiyong Park#. **CSS=0, TEL3=1**. Non-secure interrupts are routed to EL3. They will not 571*54fd6939SJiyong Park be visible to the SP. The ``PSTATE.I`` bit in Secure-EL1/Secure-EL0 will 572*54fd6939SJiyong Park have not effect. The SPD service should register a non-secure interrupt 573*54fd6939SJiyong Park handler which should save the SP state correctly and resume execution in 574*54fd6939SJiyong Park the non-secure state where the interrupt will be handled. The Secure-EL1 575*54fd6939SJiyong Park IHF does not need to take any action. 576*54fd6939SJiyong Park 577*54fd6939SJiyong Park#. **CSS=1, TEL3=0**. Non-secure interrupts are handled in the FEL in 578*54fd6939SJiyong Park non-secure state (EL1/EL2) and are not visible to the SP. This routing 579*54fd6939SJiyong Park model does not affect the SP behavior. 580*54fd6939SJiyong Park 581*54fd6939SJiyong ParkA Secure Payload must also ensure that all Secure-EL1 interrupts are correctly 582*54fd6939SJiyong Parkconfigured at the interrupt controller by the platform port of the EL3 runtime 583*54fd6939SJiyong Parkfirmware. It should configure any additional Secure-EL1 interrupts which the EL3 584*54fd6939SJiyong Parkruntime firmware is not aware of through its platform port. 585*54fd6939SJiyong Park 586*54fd6939SJiyong ParkTest secure payload behavior 587*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 588*54fd6939SJiyong Park 589*54fd6939SJiyong ParkThe routing model for Secure-EL1 and non-secure interrupts chosen by the TSP is 590*54fd6939SJiyong Parkdescribed in Section `Secure Payload Dispatcher`__. It is known to the TSPD 591*54fd6939SJiyong Parkservice at build time. 592*54fd6939SJiyong Park 593*54fd6939SJiyong Park.. __: #spd-int-registration 594*54fd6939SJiyong Park 595*54fd6939SJiyong ParkThe TSP implements an entrypoint (``tsp_sel1_intr_entry()``) for handling Secure-EL1 596*54fd6939SJiyong Parkinterrupts taken in non-secure state and routed through the TSPD service 597*54fd6939SJiyong Park(synchronous handling model). It passes the reference to this entrypoint via 598*54fd6939SJiyong Park``tsp_vectors`` to the TSPD service. 599*54fd6939SJiyong Park 600*54fd6939SJiyong ParkThe TSP also replaces the default exception vector table referenced through the 601*54fd6939SJiyong Park``early_exceptions`` variable, with a vector table capable of handling FIQ and IRQ 602*54fd6939SJiyong Parkexceptions taken at the same (Secure-EL1) exception level. This table is 603*54fd6939SJiyong Parkreferenced through the ``tsp_exceptions`` variable and programmed into the 604*54fd6939SJiyong ParkVBAR_EL1. It caters for the asynchronous handling model. 605*54fd6939SJiyong Park 606*54fd6939SJiyong ParkThe TSP also programs the Secure Physical Timer in the Arm Generic Timer block 607*54fd6939SJiyong Parkto raise a periodic interrupt (every half a second) for the purpose of testing 608*54fd6939SJiyong Parkinterrupt management across all the software components listed in `Software 609*54fd6939SJiyong Parkcomponents`_. 610*54fd6939SJiyong Park 611*54fd6939SJiyong ParkInterrupt handling 612*54fd6939SJiyong Park------------------ 613*54fd6939SJiyong Park 614*54fd6939SJiyong ParkThis section describes in detail the role of each software component (see 615*54fd6939SJiyong ParkSection `Software components`_) in handling an interrupt of a particular type. 616*54fd6939SJiyong Park 617*54fd6939SJiyong ParkEL3 runtime firmware 618*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~ 619*54fd6939SJiyong Park 620*54fd6939SJiyong ParkThe EL3 runtime firmware populates the IRQ and FIQ exception vectors referenced 621*54fd6939SJiyong Parkby the ``runtime_exceptions`` variable as follows. 622*54fd6939SJiyong Park 623*54fd6939SJiyong Park#. IRQ and FIQ exceptions taken from the current exception level with 624*54fd6939SJiyong Park ``SP_EL0`` or ``SP_EL3`` are reported as irrecoverable error conditions. As 625*54fd6939SJiyong Park mentioned earlier, EL3 runtime firmware always executes with the 626*54fd6939SJiyong Park ``PSTATE.I`` and ``PSTATE.F`` bits set. 627*54fd6939SJiyong Park 628*54fd6939SJiyong Park#. The following text describes how the IRQ and FIQ exceptions taken from a 629*54fd6939SJiyong Park lower exception level using AArch64 or AArch32 are handled. 630*54fd6939SJiyong Park 631*54fd6939SJiyong ParkWhen an interrupt is generated, the vector for each interrupt type is 632*54fd6939SJiyong Parkresponsible for: 633*54fd6939SJiyong Park 634*54fd6939SJiyong Park#. Saving the entire general purpose register context (x0-x30) immediately 635*54fd6939SJiyong Park upon exception entry. The registers are saved in the per-cpu ``cpu_context`` 636*54fd6939SJiyong Park data structure referenced by the ``SP_EL3``\ register. 637*54fd6939SJiyong Park 638*54fd6939SJiyong Park#. Saving the ``ELR_EL3``, ``SP_EL0`` and ``SPSR_EL3`` system registers in the 639*54fd6939SJiyong Park per-cpu ``cpu_context`` data structure referenced by the ``SP_EL3`` register. 640*54fd6939SJiyong Park 641*54fd6939SJiyong Park#. Switching to the C runtime stack by restoring the ``CTX_RUNTIME_SP`` value 642*54fd6939SJiyong Park from the per-cpu ``cpu_context`` data structure in ``SP_EL0`` and 643*54fd6939SJiyong Park executing the ``msr spsel, #0`` instruction. 644*54fd6939SJiyong Park 645*54fd6939SJiyong Park#. Determining the type of interrupt. Secure-EL1 interrupts will be signaled 646*54fd6939SJiyong Park at the FIQ vector. Non-secure interrupts will be signaled at the IRQ 647*54fd6939SJiyong Park vector. The platform should implement the following API to determine the 648*54fd6939SJiyong Park type of the pending interrupt. 649*54fd6939SJiyong Park 650*54fd6939SJiyong Park .. code:: c 651*54fd6939SJiyong Park 652*54fd6939SJiyong Park uint32_t plat_ic_get_interrupt_type(void); 653*54fd6939SJiyong Park 654*54fd6939SJiyong Park It should return either ``INTR_TYPE_S_EL1`` or ``INTR_TYPE_NS``. 655*54fd6939SJiyong Park 656*54fd6939SJiyong Park#. Determining the handler for the type of interrupt that has been generated. 657*54fd6939SJiyong Park The following API has been added for this purpose. 658*54fd6939SJiyong Park 659*54fd6939SJiyong Park .. code:: c 660*54fd6939SJiyong Park 661*54fd6939SJiyong Park interrupt_type_handler get_interrupt_type_handler(uint32_t interrupt_type); 662*54fd6939SJiyong Park 663*54fd6939SJiyong Park It returns the reference to the registered handler for this interrupt 664*54fd6939SJiyong Park type. The ``handler`` is retrieved from the ``intr_type_desc_t`` structure as 665*54fd6939SJiyong Park described in Section 2. ``NULL`` is returned if no handler has been 666*54fd6939SJiyong Park registered for this type of interrupt. This scenario is reported as an 667*54fd6939SJiyong Park irrecoverable error condition. 668*54fd6939SJiyong Park 669*54fd6939SJiyong Park#. Calling the registered handler function for the interrupt type generated. 670*54fd6939SJiyong Park The ``id`` parameter is set to ``INTR_ID_UNAVAILABLE`` currently. The id along 671*54fd6939SJiyong Park with the current security state and a reference to the ``cpu_context_t`` 672*54fd6939SJiyong Park structure for the current security state are passed to the handler function 673*54fd6939SJiyong Park as its arguments. 674*54fd6939SJiyong Park 675*54fd6939SJiyong Park The handler function returns a reference to the per-cpu ``cpu_context_t`` 676*54fd6939SJiyong Park structure for the target security state. 677*54fd6939SJiyong Park 678*54fd6939SJiyong Park#. Calling ``el3_exit()`` to return from EL3 into a lower exception level in 679*54fd6939SJiyong Park the security state determined by the handler routine. The ``el3_exit()`` 680*54fd6939SJiyong Park function is responsible for restoring the register context from the 681*54fd6939SJiyong Park ``cpu_context_t`` data structure for the target security state. 682*54fd6939SJiyong Park 683*54fd6939SJiyong ParkSecure payload dispatcher 684*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~ 685*54fd6939SJiyong Park 686*54fd6939SJiyong ParkInterrupt entry 687*54fd6939SJiyong Park^^^^^^^^^^^^^^^ 688*54fd6939SJiyong Park 689*54fd6939SJiyong ParkThe SPD service begins handling an interrupt when the EL3 runtime firmware calls 690*54fd6939SJiyong Parkthe handler function for that type of interrupt. The SPD service is responsible 691*54fd6939SJiyong Parkfor the following: 692*54fd6939SJiyong Park 693*54fd6939SJiyong Park#. Validating the interrupt. This involves ensuring that the interrupt was 694*54fd6939SJiyong Park generated according to the interrupt routing model specified by the SPD 695*54fd6939SJiyong Park service during registration. It should use the security state of the 696*54fd6939SJiyong Park exception level (passed in the ``flags`` parameter of the handler) where 697*54fd6939SJiyong Park the interrupt was taken from to determine this. If the interrupt is not 698*54fd6939SJiyong Park recognised then the handler should treat it as an irrecoverable error 699*54fd6939SJiyong Park condition. 700*54fd6939SJiyong Park 701*54fd6939SJiyong Park An SPD service can register a handler for Secure-EL1 and/or Non-secure 702*54fd6939SJiyong Park interrupts. A non-secure interrupt should never be routed to EL3 from 703*54fd6939SJiyong Park from non-secure state. Also if a routing model is chosen where Secure-EL1 704*54fd6939SJiyong Park interrupts are routed to S-EL1 when execution is in Secure state, then a 705*54fd6939SJiyong Park S-EL1 interrupt should never be routed to EL3 from secure state. The handler 706*54fd6939SJiyong Park could use the security state flag to check this. 707*54fd6939SJiyong Park 708*54fd6939SJiyong Park#. Determining whether a context switch is required. This depends upon the 709*54fd6939SJiyong Park routing model and interrupt type. For non secure and S-EL1 interrupt, 710*54fd6939SJiyong Park if the security state of the execution context where the interrupt was 711*54fd6939SJiyong Park generated is not the same as the security state required for handling 712*54fd6939SJiyong Park the interrupt, a context switch is required. The following 2 cases 713*54fd6939SJiyong Park require a context switch from secure to non-secure or vice-versa: 714*54fd6939SJiyong Park 715*54fd6939SJiyong Park #. A Secure-EL1 interrupt taken from the non-secure state should be 716*54fd6939SJiyong Park routed to the Secure Payload. 717*54fd6939SJiyong Park 718*54fd6939SJiyong Park #. A non-secure interrupt taken from the secure state should be routed 719*54fd6939SJiyong Park to the last known non-secure exception level. 720*54fd6939SJiyong Park 721*54fd6939SJiyong Park The SPD service must save the system register context of the current 722*54fd6939SJiyong Park security state. It must then restore the system register context of the 723*54fd6939SJiyong Park target security state. It should use the ``cm_set_next_eret_context()`` API 724*54fd6939SJiyong Park to ensure that the next ``cpu_context`` to be restored is of the target 725*54fd6939SJiyong Park security state. 726*54fd6939SJiyong Park 727*54fd6939SJiyong Park If the target state is secure then execution should be handed to the SP as 728*54fd6939SJiyong Park per the synchronous interrupt handling model it implements. A Secure-EL1 729*54fd6939SJiyong Park interrupt can be routed to EL3 while execution is in the SP. This implies 730*54fd6939SJiyong Park that SP execution can be preempted while handling an interrupt by a 731*54fd6939SJiyong Park another higher priority Secure-EL1 interrupt or a EL3 interrupt. The SPD 732*54fd6939SJiyong Park service should be able to handle this preemption or manage secure interrupt 733*54fd6939SJiyong Park priorities before handing control to the SP. 734*54fd6939SJiyong Park 735*54fd6939SJiyong Park#. Setting the return value of the handler to the per-cpu ``cpu_context`` if 736*54fd6939SJiyong Park the interrupt has been successfully validated and ready to be handled at a 737*54fd6939SJiyong Park lower exception level. 738*54fd6939SJiyong Park 739*54fd6939SJiyong ParkThe routing model allows non-secure interrupts to interrupt Secure-EL1 when in 740*54fd6939SJiyong Parksecure state if it has been configured to do so. The SPD service and the SP 741*54fd6939SJiyong Parkshould implement a mechanism for routing these interrupts to the last known 742*54fd6939SJiyong Parkexception level in the non-secure state. The former should save the SP context, 743*54fd6939SJiyong Parkrestore the non-secure context and arrange for entry into the non-secure state 744*54fd6939SJiyong Parkso that the interrupt can be handled. 745*54fd6939SJiyong Park 746*54fd6939SJiyong ParkInterrupt exit 747*54fd6939SJiyong Park^^^^^^^^^^^^^^ 748*54fd6939SJiyong Park 749*54fd6939SJiyong ParkWhen the Secure Payload has finished handling a Secure-EL1 interrupt, it could 750*54fd6939SJiyong Parkreturn control back to the SPD service through a SMC32 or SMC64. The SPD service 751*54fd6939SJiyong Parkshould handle this secure monitor call so that execution resumes in the 752*54fd6939SJiyong Parkexception level and the security state from where the Secure-EL1 interrupt was 753*54fd6939SJiyong Parkoriginally taken. 754*54fd6939SJiyong Park 755*54fd6939SJiyong ParkTest secure payload dispatcher Secure-EL1 interrupt handling 756*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 757*54fd6939SJiyong Park 758*54fd6939SJiyong ParkThe example TSPD service registers a handler for Secure-EL1 interrupts taken 759*54fd6939SJiyong Parkfrom the non-secure state. During execution in S-EL1, the TSPD expects that the 760*54fd6939SJiyong ParkSecure-EL1 interrupts are handled in S-EL1 by TSP. Its handler 761*54fd6939SJiyong Park``tspd_secure_el1_interrupt_handler()`` expects only to be invoked for Secure-EL1 762*54fd6939SJiyong Parkoriginating from the non-secure state. It takes the following actions upon being 763*54fd6939SJiyong Parkinvoked. 764*54fd6939SJiyong Park 765*54fd6939SJiyong Park#. It uses the security state provided in the ``flags`` parameter to ensure 766*54fd6939SJiyong Park that the secure interrupt originated from the non-secure state. It asserts 767*54fd6939SJiyong Park if this is not the case. 768*54fd6939SJiyong Park 769*54fd6939SJiyong Park#. It saves the system register context for the non-secure state by calling 770*54fd6939SJiyong Park ``cm_el1_sysregs_context_save(NON_SECURE);``. 771*54fd6939SJiyong Park 772*54fd6939SJiyong Park#. It sets the ``ELR_EL3`` system register to ``tsp_sel1_intr_entry`` and sets the 773*54fd6939SJiyong Park ``SPSR_EL3.DAIF`` bits in the secure CPU context. It sets ``x0`` to 774*54fd6939SJiyong Park ``TSP_HANDLE_SEL1_INTR_AND_RETURN``. If the TSP was preempted earlier by a non 775*54fd6939SJiyong Park secure interrupt during ``yielding`` SMC processing, save the registers that 776*54fd6939SJiyong Park will be trashed, which is the ``ELR_EL3`` and ``SPSR_EL3``, in order to be able 777*54fd6939SJiyong Park to re-enter TSP for Secure-EL1 interrupt processing. It does not need to 778*54fd6939SJiyong Park save any other secure context since the TSP is expected to preserve it 779*54fd6939SJiyong Park (see section `Test secure payload dispatcher behavior`_). 780*54fd6939SJiyong Park 781*54fd6939SJiyong Park#. It restores the system register context for the secure state by calling 782*54fd6939SJiyong Park ``cm_el1_sysregs_context_restore(SECURE);``. 783*54fd6939SJiyong Park 784*54fd6939SJiyong Park#. It ensures that the secure CPU context is used to program the next 785*54fd6939SJiyong Park exception return from EL3 by calling ``cm_set_next_eret_context(SECURE);``. 786*54fd6939SJiyong Park 787*54fd6939SJiyong Park#. It returns the per-cpu ``cpu_context`` to indicate that the interrupt can 788*54fd6939SJiyong Park now be handled by the SP. ``x1`` is written with the value of ``elr_el3`` 789*54fd6939SJiyong Park register for the non-secure state. This information is used by the SP for 790*54fd6939SJiyong Park debugging purposes. 791*54fd6939SJiyong Park 792*54fd6939SJiyong ParkThe figure below describes how the interrupt handling is implemented by the TSPD 793*54fd6939SJiyong Parkwhen a Secure-EL1 interrupt is generated when execution is in the non-secure 794*54fd6939SJiyong Parkstate. 795*54fd6939SJiyong Park 796*54fd6939SJiyong Park|Image 1| 797*54fd6939SJiyong Park 798*54fd6939SJiyong ParkThe TSP issues an SMC with ``TSP_HANDLED_S_EL1_INTR`` as the function identifier to 799*54fd6939SJiyong Parksignal completion of interrupt handling. 800*54fd6939SJiyong Park 801*54fd6939SJiyong ParkThe TSPD service takes the following actions in ``tspd_smc_handler()`` function 802*54fd6939SJiyong Parkupon receiving an SMC with ``TSP_HANDLED_S_EL1_INTR`` as the function identifier: 803*54fd6939SJiyong Park 804*54fd6939SJiyong Park#. It ensures that the call originated from the secure state otherwise 805*54fd6939SJiyong Park execution returns to the non-secure state with ``SMC_UNK`` in ``x0``. 806*54fd6939SJiyong Park 807*54fd6939SJiyong Park#. It restores the saved ``ELR_EL3`` and ``SPSR_EL3`` system registers back to 808*54fd6939SJiyong Park the secure CPU context (see step 3 above) in case the TSP had been preempted 809*54fd6939SJiyong Park by a non secure interrupt earlier. 810*54fd6939SJiyong Park 811*54fd6939SJiyong Park#. It restores the system register context for the non-secure state by 812*54fd6939SJiyong Park calling ``cm_el1_sysregs_context_restore(NON_SECURE)``. 813*54fd6939SJiyong Park 814*54fd6939SJiyong Park#. It ensures that the non-secure CPU context is used to program the next 815*54fd6939SJiyong Park exception return from EL3 by calling ``cm_set_next_eret_context(NON_SECURE)``. 816*54fd6939SJiyong Park 817*54fd6939SJiyong Park#. ``tspd_smc_handler()`` returns a reference to the non-secure ``cpu_context`` 818*54fd6939SJiyong Park as the return value. 819*54fd6939SJiyong Park 820*54fd6939SJiyong ParkTest secure payload dispatcher non-secure interrupt handling 821*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 822*54fd6939SJiyong Park 823*54fd6939SJiyong ParkThe TSP in Secure-EL1 can be preempted by a non-secure interrupt during 824*54fd6939SJiyong Park``yielding`` SMC processing or by a higher priority EL3 interrupt during 825*54fd6939SJiyong ParkSecure-EL1 interrupt processing. When ``EL3_EXCEPTION_HANDLING`` is ``0``, only 826*54fd6939SJiyong Parknon-secure interrupts can cause preemption of TSP since there are no EL3 827*54fd6939SJiyong Parkinterrupts in the system. With ``EL3_EXCEPTION_HANDLING=1`` however, any EL3 828*54fd6939SJiyong Parkinterrupt may preempt Secure execution. 829*54fd6939SJiyong Park 830*54fd6939SJiyong ParkIt should be noted that while TSP is preempted, the TSPD only allows entry into 831*54fd6939SJiyong Parkthe TSP either for Secure-EL1 interrupt handling or for resuming the preempted 832*54fd6939SJiyong Park``yielding`` SMC in response to the ``TSP_FID_RESUME`` SMC from the normal world. 833*54fd6939SJiyong Park(See Section `Implication of preempted SMC on Non-Secure Software`_). 834*54fd6939SJiyong Park 835*54fd6939SJiyong ParkThe non-secure interrupt triggered in Secure-EL1 during ``yielding`` SMC 836*54fd6939SJiyong Parkprocessing can be routed to either EL3 or Secure-EL1 and is controlled by build 837*54fd6939SJiyong Parkoption ``TSP_NS_INTR_ASYNC_PREEMPT`` (see Section `Test secure payload 838*54fd6939SJiyong Parkdispatcher behavior`_). If the build option is set, the TSPD will set the 839*54fd6939SJiyong Parkrouting model for the non-secure interrupt to be routed to EL3 from secure state 840*54fd6939SJiyong Parki.e. **TEL3=1, CSS=0** and registers ``tspd_ns_interrupt_handler()`` as the 841*54fd6939SJiyong Parknon-secure interrupt handler. The ``tspd_ns_interrupt_handler()`` on being 842*54fd6939SJiyong Parkinvoked ensures that the interrupt originated from the secure state and disables 843*54fd6939SJiyong Parkrouting of non-secure interrupts from secure state to EL3. This is to prevent 844*54fd6939SJiyong Parkfurther preemption (by a non-secure interrupt) when TSP is reentered for 845*54fd6939SJiyong Parkhandling Secure-EL1 interrupts that triggered while execution was in the normal 846*54fd6939SJiyong Parkworld. The ``tspd_ns_interrupt_handler()`` then invokes 847*54fd6939SJiyong Park``tspd_handle_sp_preemption()`` for further handling. 848*54fd6939SJiyong Park 849*54fd6939SJiyong ParkIf the ``TSP_NS_INTR_ASYNC_PREEMPT`` build option is zero (default), the default 850*54fd6939SJiyong Parkrouting model for non-secure interrupt in secure state is in effect 851*54fd6939SJiyong Parki.e. **TEL3=0, CSS=0**. During ``yielding`` SMC processing, the IRQ 852*54fd6939SJiyong Parkexceptions are unmasked i.e. ``PSTATE.I=0``, and a non-secure interrupt will 853*54fd6939SJiyong Parktrigger at Secure-EL1 IRQ exception vector. The TSP saves the general purpose 854*54fd6939SJiyong Parkregister context and issues an SMC with ``TSP_PREEMPTED`` as the function 855*54fd6939SJiyong Parkidentifier to signal preemption of TSP. The TSPD SMC handler, 856*54fd6939SJiyong Park``tspd_smc_handler()``, ensures that the SMC call originated from the 857*54fd6939SJiyong Parksecure state otherwise execution returns to the non-secure state with 858*54fd6939SJiyong Park``SMC_UNK`` in ``x0``. It then invokes ``tspd_handle_sp_preemption()`` for 859*54fd6939SJiyong Parkfurther handling. 860*54fd6939SJiyong Park 861*54fd6939SJiyong ParkThe ``tspd_handle_sp_preemption()`` takes the following actions upon being 862*54fd6939SJiyong Parkinvoked: 863*54fd6939SJiyong Park 864*54fd6939SJiyong Park#. It saves the system register context for the secure state by calling 865*54fd6939SJiyong Park ``cm_el1_sysregs_context_save(SECURE)``. 866*54fd6939SJiyong Park 867*54fd6939SJiyong Park#. It restores the system register context for the non-secure state by 868*54fd6939SJiyong Park calling ``cm_el1_sysregs_context_restore(NON_SECURE)``. 869*54fd6939SJiyong Park 870*54fd6939SJiyong Park#. It ensures that the non-secure CPU context is used to program the next 871*54fd6939SJiyong Park exception return from EL3 by calling ``cm_set_next_eret_context(NON_SECURE)``. 872*54fd6939SJiyong Park 873*54fd6939SJiyong Park#. ``SMC_PREEMPTED`` is set in x0 and return to non secure state after 874*54fd6939SJiyong Park restoring non secure context. 875*54fd6939SJiyong Park 876*54fd6939SJiyong ParkThe Normal World is expected to resume the TSP after the ``yielding`` SMC 877*54fd6939SJiyong Parkpreemption by issuing an SMC with ``TSP_FID_RESUME`` as the function identifier 878*54fd6939SJiyong Park(see section `Implication of preempted SMC on Non-Secure Software`_). The TSPD 879*54fd6939SJiyong Parkservice takes the following actions in ``tspd_smc_handler()`` function upon 880*54fd6939SJiyong Parkreceiving this SMC: 881*54fd6939SJiyong Park 882*54fd6939SJiyong Park#. It ensures that the call originated from the non secure state. An 883*54fd6939SJiyong Park assertion is raised otherwise. 884*54fd6939SJiyong Park 885*54fd6939SJiyong Park#. Checks whether the TSP needs a resume i.e check if it was preempted. It 886*54fd6939SJiyong Park then saves the system register context for the non-secure state by calling 887*54fd6939SJiyong Park ``cm_el1_sysregs_context_save(NON_SECURE)``. 888*54fd6939SJiyong Park 889*54fd6939SJiyong Park#. Restores the secure context by calling 890*54fd6939SJiyong Park ``cm_el1_sysregs_context_restore(SECURE)`` 891*54fd6939SJiyong Park 892*54fd6939SJiyong Park#. It ensures that the secure CPU context is used to program the next 893*54fd6939SJiyong Park exception return from EL3 by calling ``cm_set_next_eret_context(SECURE)``. 894*54fd6939SJiyong Park 895*54fd6939SJiyong Park#. ``tspd_smc_handler()`` returns a reference to the secure ``cpu_context`` as the 896*54fd6939SJiyong Park return value. 897*54fd6939SJiyong Park 898*54fd6939SJiyong ParkThe figure below describes how the TSP/TSPD handle a non-secure interrupt when 899*54fd6939SJiyong Parkit is generated during execution in the TSP with ``PSTATE.I`` = 0 when the 900*54fd6939SJiyong Park``TSP_NS_INTR_ASYNC_PREEMPT`` build flag is 0. 901*54fd6939SJiyong Park 902*54fd6939SJiyong Park|Image 2| 903*54fd6939SJiyong Park 904*54fd6939SJiyong Park.. _sp-synchronous-int: 905*54fd6939SJiyong Park 906*54fd6939SJiyong ParkSecure payload interrupt handling 907*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 908*54fd6939SJiyong Park 909*54fd6939SJiyong ParkThe SP should implement one or both of the synchronous and asynchronous 910*54fd6939SJiyong Parkinterrupt handling models depending upon the interrupt routing model it has 911*54fd6939SJiyong Parkchosen (as described in section :ref:`Secure Payload <sp-int-registration>`). 912*54fd6939SJiyong Park 913*54fd6939SJiyong ParkIn the synchronous model, it should begin handling a Secure-EL1 interrupt after 914*54fd6939SJiyong Parkreceiving control from the SPD service at an entrypoint agreed upon during build 915*54fd6939SJiyong Parktime or during the registration phase. Before handling the interrupt, the SP 916*54fd6939SJiyong Parkshould save any Secure-EL1 system register context which is needed for resuming 917*54fd6939SJiyong Parknormal execution in the SP later e.g. ``SPSR_EL1``, ``ELR_EL1``. After handling 918*54fd6939SJiyong Parkthe interrupt, the SP could return control back to the exception level and 919*54fd6939SJiyong Parksecurity state where the interrupt was originally taken from. The SP should use 920*54fd6939SJiyong Parkan SMC32 or SMC64 to ask the SPD service to do this. 921*54fd6939SJiyong Park 922*54fd6939SJiyong ParkIn the asynchronous model, the Secure Payload is responsible for handling 923*54fd6939SJiyong Parknon-secure and Secure-EL1 interrupts at the IRQ and FIQ vectors in its exception 924*54fd6939SJiyong Parkvector table when ``PSTATE.I`` and ``PSTATE.F`` bits are 0. As described earlier, 925*54fd6939SJiyong Parkwhen a non-secure interrupt is generated, the SP should coordinate with the SPD 926*54fd6939SJiyong Parkservice to pass control back to the non-secure state in the last known exception 927*54fd6939SJiyong Parklevel. This will allow the non-secure interrupt to be handled in the non-secure 928*54fd6939SJiyong Parkstate. 929*54fd6939SJiyong Park 930*54fd6939SJiyong ParkTest secure payload behavior 931*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 932*54fd6939SJiyong Park 933*54fd6939SJiyong ParkThe TSPD hands control of a Secure-EL1 interrupt to the TSP at the 934*54fd6939SJiyong Park``tsp_sel1_intr_entry()``. The TSP handles the interrupt while ensuring that the 935*54fd6939SJiyong Parkhandover agreement described in Section `Test secure payload dispatcher 936*54fd6939SJiyong Parkbehavior`_ is maintained. It updates some statistics by calling 937*54fd6939SJiyong Park``tsp_update_sync_sel1_intr_stats()``. It then calls 938*54fd6939SJiyong Park``tsp_common_int_handler()`` which. 939*54fd6939SJiyong Park 940*54fd6939SJiyong Park#. Checks whether the interrupt is the secure physical timer interrupt. It 941*54fd6939SJiyong Park uses the platform API ``plat_ic_get_pending_interrupt_id()`` to get the 942*54fd6939SJiyong Park interrupt number. If it is not the secure physical timer interrupt, then 943*54fd6939SJiyong Park that means that a higher priority interrupt has preempted it. Invoke 944*54fd6939SJiyong Park ``tsp_handle_preemption()`` to handover control back to EL3 by issuing 945*54fd6939SJiyong Park an SMC with ``TSP_PREEMPTED`` as the function identifier. 946*54fd6939SJiyong Park 947*54fd6939SJiyong Park#. Handles the secure timer interrupt interrupt by acknowledging it using the 948*54fd6939SJiyong Park ``plat_ic_acknowledge_interrupt()`` platform API, calling 949*54fd6939SJiyong Park ``tsp_generic_timer_handler()`` to reprogram the secure physical generic 950*54fd6939SJiyong Park timer and calling the ``plat_ic_end_of_interrupt()`` platform API to signal 951*54fd6939SJiyong Park end of interrupt processing. 952*54fd6939SJiyong Park 953*54fd6939SJiyong ParkThe TSP passes control back to the TSPD by issuing an SMC64 with 954*54fd6939SJiyong Park``TSP_HANDLED_S_EL1_INTR`` as the function identifier. 955*54fd6939SJiyong Park 956*54fd6939SJiyong ParkThe TSP handles interrupts under the asynchronous model as follows. 957*54fd6939SJiyong Park 958*54fd6939SJiyong Park#. Secure-EL1 interrupts are handled by calling the ``tsp_common_int_handler()`` 959*54fd6939SJiyong Park function. The function has been described above. 960*54fd6939SJiyong Park 961*54fd6939SJiyong Park#. Non-secure interrupts are handled by calling the ``tsp_common_int_handler()`` 962*54fd6939SJiyong Park function which ends up invoking ``tsp_handle_preemption()`` and issuing an 963*54fd6939SJiyong Park SMC64 with ``TSP_PREEMPTED`` as the function identifier. Execution resumes at 964*54fd6939SJiyong Park the instruction that follows this SMC instruction when the TSPD hands control 965*54fd6939SJiyong Park to the TSP in response to an SMC with ``TSP_FID_RESUME`` as the function 966*54fd6939SJiyong Park identifier from the non-secure state (see section `Test secure payload 967*54fd6939SJiyong Park dispatcher non-secure interrupt handling`_). 968*54fd6939SJiyong Park 969*54fd6939SJiyong ParkOther considerations 970*54fd6939SJiyong Park-------------------- 971*54fd6939SJiyong Park 972*54fd6939SJiyong ParkImplication of preempted SMC on Non-Secure Software 973*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 974*54fd6939SJiyong Park 975*54fd6939SJiyong ParkA ``yielding`` SMC call to Secure payload can be preempted by a non-secure 976*54fd6939SJiyong Parkinterrupt and the execution can return to the non-secure world for handling 977*54fd6939SJiyong Parkthe interrupt (For details on ``yielding`` SMC refer `SMC calling convention`_). 978*54fd6939SJiyong ParkIn this case, the SMC call has not completed its execution and the execution 979*54fd6939SJiyong Parkmust return back to the secure payload to resume the preempted SMC call. 980*54fd6939SJiyong ParkThis can be achieved by issuing an SMC call which instructs to resume the 981*54fd6939SJiyong Parkpreempted SMC. 982*54fd6939SJiyong Park 983*54fd6939SJiyong ParkA ``fast`` SMC cannot be preempted and hence this case will not happen for 984*54fd6939SJiyong Parka fast SMC call. 985*54fd6939SJiyong Park 986*54fd6939SJiyong ParkIn the Test Secure Payload implementation, ``TSP_FID_RESUME`` is designated 987*54fd6939SJiyong Parkas the resume SMC FID. It is important to note that ``TSP_FID_RESUME`` is a 988*54fd6939SJiyong Park``yielding`` SMC which means it too can be be preempted. The typical non 989*54fd6939SJiyong Parksecure software sequence for issuing a ``yielding`` SMC would look like this, 990*54fd6939SJiyong Parkassuming ``P.STATE.I=0`` in the non secure state : 991*54fd6939SJiyong Park 992*54fd6939SJiyong Park.. code:: c 993*54fd6939SJiyong Park 994*54fd6939SJiyong Park int rc; 995*54fd6939SJiyong Park rc = smc(TSP_YIELD_SMC_FID, ...); /* Issue a Yielding SMC call */ 996*54fd6939SJiyong Park /* The pending non-secure interrupt is handled by the interrupt handler 997*54fd6939SJiyong Park and returns back here. */ 998*54fd6939SJiyong Park while (rc == SMC_PREEMPTED) { /* Check if the SMC call is preempted */ 999*54fd6939SJiyong Park rc = smc(TSP_FID_RESUME); /* Issue resume SMC call */ 1000*54fd6939SJiyong Park } 1001*54fd6939SJiyong Park 1002*54fd6939SJiyong ParkThe ``TSP_YIELD_SMC_FID`` is any ``yielding`` SMC function identifier and the smc() 1003*54fd6939SJiyong Parkfunction invokes a SMC call with the required arguments. The pending non-secure 1004*54fd6939SJiyong Parkinterrupt causes an IRQ exception and the IRQ handler registered at the 1005*54fd6939SJiyong Parkexception vector handles the non-secure interrupt and returns. The return value 1006*54fd6939SJiyong Parkfrom the SMC call is tested for ``SMC_PREEMPTED`` to check whether it is 1007*54fd6939SJiyong Parkpreempted. If it is, then the resume SMC call ``TSP_FID_RESUME`` is issued. The 1008*54fd6939SJiyong Parkreturn value of the SMC call is tested again to check if it is preempted. 1009*54fd6939SJiyong ParkThis is done in a loop till the SMC call succeeds or fails. If a ``yielding`` 1010*54fd6939SJiyong ParkSMC is preempted, until it is resumed using ``TSP_FID_RESUME`` SMC and 1011*54fd6939SJiyong Parkcompleted, the current TSPD prevents any other SMC call from re-entering 1012*54fd6939SJiyong ParkTSP by returning ``SMC_UNK`` error. 1013*54fd6939SJiyong Park 1014*54fd6939SJiyong Park-------------- 1015*54fd6939SJiyong Park 1016*54fd6939SJiyong Park*Copyright (c) 2014-2020, Arm Limited and Contributors. All rights reserved.* 1017*54fd6939SJiyong Park 1018*54fd6939SJiyong Park.. _SMC calling convention: https://developer.arm.com/docs/den0028/latest 1019*54fd6939SJiyong Park 1020*54fd6939SJiyong Park.. |Image 1| image:: ../resources/diagrams/sec-int-handling.png 1021*54fd6939SJiyong Park.. |Image 2| image:: ../resources/diagrams/non-sec-int-handling.png 1022