1 /** @file
2   Support ResetSystem Runtime call using PSCI calls
3 
4   Note: A similar library is implemented in
5   ArmPkg/Library/ArmPsciResetSystemLib. Similar issues might
6   exist in this implementation too.
7 
8   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
9   Copyright (c) 2013, ARM Ltd. All rights reserved.<BR>
10   Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
11 
12   This program and the accompanying materials
13   are licensed and made available under the terms and conditions of the BSD License
14   which accompanies this distribution.  The full text of the license may be found at
15   http://opensource.org/licenses/bsd-license.php
16 
17   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 
20 **/
21 
22 #include <PiDxe.h>
23 
24 #include <Library/BaseLib.h>
25 #include <Library/DebugLib.h>
26 #include <Library/EfiResetSystemLib.h>
27 #include <Library/ArmSmcLib.h>
28 #include <Library/ArmHvcLib.h>
29 
30 #include <IndustryStandard/ArmStdSmc.h>
31 
32 STATIC UINT32 mArmPsciMethod;
33 
34 RETURN_STATUS
35 EFIAPI
ArmPsciResetSystemLibConstructor(VOID)36 ArmPsciResetSystemLibConstructor (
37   VOID
38   )
39 {
40   mArmPsciMethod = PcdGet32 (PcdArmPsciMethod);
41   return RETURN_SUCCESS;
42 }
43 
44 /**
45   Resets the entire platform.
46 
47   @param  ResetType             The type of reset to perform.
48   @param  ResetStatus           The status code for the reset.
49   @param  DataSize              The size, in bytes, of WatchdogData.
50   @param  ResetData             For a ResetType of EfiResetCold, EfiResetWarm, or
51                                 EfiResetShutdown the data buffer starts with a Null-terminated
52                                 Unicode string, optionally followed by additional binary data.
53 
54 **/
55 EFI_STATUS
56 EFIAPI
LibResetSystem(IN EFI_RESET_TYPE ResetType,IN EFI_STATUS ResetStatus,IN UINTN DataSize,IN CHAR16 * ResetData OPTIONAL)57 LibResetSystem (
58   IN EFI_RESET_TYPE   ResetType,
59   IN EFI_STATUS       ResetStatus,
60   IN UINTN            DataSize,
61   IN CHAR16           *ResetData OPTIONAL
62   )
63 {
64   ARM_SMC_ARGS ArmSmcArgs;
65   ARM_HVC_ARGS ArmHvcArgs;
66 
67   switch (ResetType) {
68 
69   case EfiResetPlatformSpecific:
70     // Map the platform specific reset as reboot
71   case EfiResetWarm:
72     // Map a warm reset into a cold reset
73   case EfiResetCold:
74     // Send a PSCI 0.2 SYSTEM_RESET command
75     ArmSmcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_RESET;
76     ArmHvcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_RESET;
77     break;
78   case EfiResetShutdown:
79     // Send a PSCI 0.2 SYSTEM_OFF command
80     ArmSmcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_OFF;
81     ArmHvcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_OFF;
82     break;
83   default:
84     ASSERT (FALSE);
85     return EFI_UNSUPPORTED;
86   }
87 
88   switch (mArmPsciMethod) {
89   case 1:
90     ArmCallHvc (&ArmHvcArgs);
91     break;
92 
93   case 2:
94     ArmCallSmc (&ArmSmcArgs);
95     break;
96 
97   default:
98     DEBUG ((EFI_D_ERROR, "%a: no PSCI method defined\n", __FUNCTION__));
99     return EFI_UNSUPPORTED;
100   }
101 
102   // We should never be here
103   DEBUG ((EFI_D_ERROR, "%a: PSCI Reset failed\n", __FUNCTION__));
104   CpuDeadLoop ();
105   return EFI_UNSUPPORTED;
106 }
107 
108 /**
109   Initialize any infrastructure required for LibResetSystem () to function.
110 
111   @param  ImageHandle   The firmware allocated handle for the EFI image.
112   @param  SystemTable   A pointer to the EFI System Table.
113 
114   @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
115 
116 **/
117 EFI_STATUS
118 EFIAPI
LibInitializeResetSystem(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)119 LibInitializeResetSystem (
120   IN EFI_HANDLE        ImageHandle,
121   IN EFI_SYSTEM_TABLE  *SystemTable
122   )
123 {
124   return EFI_SUCCESS;
125 }
126