]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c
ArmPkg/ArmSmcPsciResetSystemLib: Add a new API ResetSystem
[mirror_edk2.git] / ArmPkg / Library / ArmSmcPsciResetSystemLib / ArmSmcPsciResetSystemLib.c
CommitLineData
b2c55e73
AB
1/** @file\r
2 ResetSystemLib implementation using PSCI calls\r
3\r
6556224e 4 Copyright (c) 2017 - 2018, Linaro Ltd. All rights reserved.<BR>\r
bda4d5be 5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
b2c55e73 6\r
4059386c 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
b2c55e73
AB
8\r
9**/\r
10\r
11#include <PiDxe.h>\r
12\r
dde2dd64
AB
13#include <Library/ArmMmuLib.h>\r
14#include <Library/ArmSmcLib.h>\r
b2c55e73
AB
15#include <Library/BaseLib.h>\r
16#include <Library/DebugLib.h>\r
17#include <Library/ResetSystemLib.h>\r
dde2dd64
AB
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/UefiRuntimeLib.h>\r
b2c55e73
AB
20\r
21#include <IndustryStandard/ArmStdSmc.h>\r
22\r
23/**\r
24 This function causes a system-wide reset (cold reset), in which\r
25 all circuitry within the system returns to its initial state. This type of reset\r
26 is asynchronous to system operation and operates without regard to\r
27 cycle boundaries.\r
28\r
29 If this function returns, it means that the system does not support cold reset.\r
30**/\r
31VOID\r
32EFIAPI\r
33ResetCold (\r
34 VOID\r
35 )\r
36{\r
37 ARM_SMC_ARGS ArmSmcArgs;\r
38\r
39 // Send a PSCI 0.2 SYSTEM_RESET command\r
40 ArmSmcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_RESET;\r
41 ArmCallSmc (&ArmSmcArgs);\r
42}\r
43\r
44/**\r
45 This function causes a system-wide initialization (warm reset), in which all processors\r
46 are set to their initial state. Pending cycles are not corrupted.\r
47\r
48 If this function returns, it means that the system does not support warm reset.\r
49**/\r
50VOID\r
51EFIAPI\r
52ResetWarm (\r
53 VOID\r
54 )\r
55{\r
56 // Map a warm reset into a cold reset\r
57 ResetCold ();\r
58}\r
59\r
60/**\r
61 This function causes the system to enter a power state equivalent\r
62 to the ACPI G2/S5 or G3 states.\r
63\r
64 If this function returns, it means that the system does not support shutdown reset.\r
65**/\r
66VOID\r
67EFIAPI\r
68ResetShutdown (\r
69 VOID\r
70 )\r
71{\r
72 ARM_SMC_ARGS ArmSmcArgs;\r
73\r
74 // Send a PSCI 0.2 SYSTEM_OFF command\r
75 ArmSmcArgs.Arg0 = ARM_SMC_ID_PSCI_SYSTEM_OFF;\r
76 ArmCallSmc (&ArmSmcArgs);\r
77}\r
78\r
6556224e
AB
79VOID DisableMmuAndReenterPei (VOID);\r
80\r
b2c55e73
AB
81/**\r
82 This function causes the system to enter S3 and then wake up immediately.\r
83\r
84 If this function returns, it means that the system does not support S3 feature.\r
85**/\r
86VOID\r
87EFIAPI\r
88EnterS3WithImmediateWake (\r
89 VOID\r
90 )\r
91{\r
6556224e
AB
92 EFI_PHYSICAL_ADDRESS Alloc;\r
93 EFI_MEMORY_DESCRIPTOR *MemMap;\r
94 UINTN MemMapSize;\r
95 UINTN MapKey, DescriptorSize;\r
96 UINT32 DescriptorVersion;\r
97 EFI_STATUS Status;\r
dde2dd64
AB
98\r
99 if (FeaturePcdGet (PcdArmReenterPeiForCapsuleWarmReboot) &&\r
100 !EfiAtRuntime ()) {\r
101 //\r
102 // At boot time, we are the only core running, so we can implement the\r
103 // immediate wake (which is used by capsule update) by disabling the MMU\r
104 // and interrupts, and jumping to the PEI entry point.\r
105 //\r
dde2dd64 106\r
6556224e
AB
107 //\r
108 // Obtain the size of the memory map\r
109 //\r
110 MemMapSize = 0;\r
111 MemMap = NULL;\r
112 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,\r
113 &DescriptorVersion);\r
114 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
115\r
116 //\r
117 // Add some slack to the allocation to cater for changes in the memory\r
118 // map if ExitBootServices () fails the first time around.\r
119 //\r
120 MemMapSize += SIZE_4KB;\r
121 Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,\r
122 EFI_SIZE_TO_PAGES (MemMapSize), &Alloc);\r
123 ASSERT_EFI_ERROR (Status);\r
124\r
125 MemMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)Alloc;\r
126\r
127 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,\r
128 &DescriptorVersion);\r
129 ASSERT_EFI_ERROR (Status);\r
130\r
131 Status = gBS->ExitBootServices (gImageHandle, MapKey);\r
132 if (EFI_ERROR (Status)) {\r
133 //\r
134 // ExitBootServices () may fail the first time around if an event fired\r
135 // right after the call to GetMemoryMap() which allocated or freed memory.\r
136 // Since that first call to ExitBootServices () will disarm the timer,\r
137 // this is guaranteed not to happen again, so one additional attempt\r
138 // should suffice.\r
139 //\r
140 Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,\r
141 &DescriptorVersion);\r
142 ASSERT_EFI_ERROR (Status);\r
143\r
144 Status = gBS->ExitBootServices (gImageHandle, MapKey);\r
145 ASSERT_EFI_ERROR (Status);\r
146 }\r
147\r
148 DisableMmuAndReenterPei ();\r
dde2dd64 149 }\r
b2c55e73
AB
150}\r
151\r
152/**\r
153 This function causes a systemwide reset. The exact type of the reset is\r
154 defined by the EFI_GUID that follows the Null-terminated Unicode string passed\r
155 into ResetData. If the platform does not recognize the EFI_GUID in ResetData\r
156 the platform must pick a supported reset type to perform.The platform may\r
157 optionally log the parameters from any non-normal reset that occurs.\r
158\r
159 @param[in] DataSize The size, in bytes, of ResetData.\r
160 @param[in] ResetData The data buffer starts with a Null-terminated string,\r
161 followed by the EFI_GUID.\r
162**/\r
163VOID\r
164EFIAPI\r
165ResetPlatformSpecific (\r
166 IN UINTN DataSize,\r
167 IN VOID *ResetData\r
168 )\r
169{\r
170 // Map the platform specific reset as reboot\r
171 ResetCold ();\r
172}\r
bda4d5be
ZG
173\r
174/**\r
175 The ResetSystem function resets the entire platform.\r
176\r
177 @param[in] ResetType The type of reset to perform.\r
178 @param[in] ResetStatus The status code for the reset.\r
179 @param[in] DataSize The size, in bytes, of ResetData.\r
180 @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or EfiResetShutdown\r
181 the data buffer starts with a Null-terminated string, optionally\r
182 followed by additional binary data. The string is a description\r
183 that the caller may use to further indicate the reason for the\r
184 system reset.\r
185**/\r
186VOID\r
187EFIAPI\r
188ResetSystem (\r
189 IN EFI_RESET_TYPE ResetType,\r
190 IN EFI_STATUS ResetStatus,\r
191 IN UINTN DataSize,\r
192 IN VOID *ResetData OPTIONAL\r
193 )\r
194{\r
195 switch (ResetType) {\r
196 case EfiResetWarm:\r
197 ResetWarm ();\r
198 break;\r
199\r
200 case EfiResetCold:\r
201 ResetCold ();\r
202 break;\r
203\r
204 case EfiResetShutdown:\r
205 ResetShutdown ();\r
206 return;\r
207\r
208 case EfiResetPlatformSpecific:\r
209 ResetPlatformSpecific (DataSize, ResetData);\r
210 return;\r
211\r
212 default:\r
213 return;\r
214 }\r
215}\r