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