1 /** @file
2   Reset System Library functions for bootloader
3 
4   Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <PiDxe.h>
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/IoLib.h>
13 #include <Library/HobLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Guid/AcpiBoardInfoGuid.h>
16 
17 ACPI_BOARD_INFO    mAcpiBoardInfo;
18 
19 /**
20   The constructor function to initialize mAcpiBoardInfo.
21 
22   @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
23 
24 **/
25 RETURN_STATUS
26 EFIAPI
ResetSystemLibConstructor(VOID)27 ResetSystemLibConstructor (
28   VOID
29   )
30 {
31   EFI_HOB_GUID_TYPE  *GuidHob;
32   ACPI_BOARD_INFO    *AcpiBoardInfoPtr;
33 
34   //
35   // Find the acpi board information guid hob
36   //
37   GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);
38   ASSERT (GuidHob != NULL);
39 
40   AcpiBoardInfoPtr = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob);
41   CopyMem (&mAcpiBoardInfo, AcpiBoardInfoPtr, sizeof (ACPI_BOARD_INFO));
42 
43   return EFI_SUCCESS;
44 }
45 
46 
47 VOID
AcpiPmControl(UINTN SuspendType)48 AcpiPmControl (
49   UINTN   SuspendType
50   )
51 {
52   UINTN              PmCtrlReg;
53 
54   ASSERT (SuspendType <= 7);
55 
56   PmCtrlReg = (UINTN)mAcpiBoardInfo.PmCtrlRegBase;
57   IoAndThenOr16 (PmCtrlReg, (UINT16) ~0x3c00, (UINT16) (SuspendType << 10));
58   IoOr16 (PmCtrlReg, BIT13);
59   CpuDeadLoop ();
60 }
61 
62 /**
63   Calling this function causes a system-wide reset. This sets
64   all circuitry within the system to its initial state. This type of reset
65   is asynchronous to system operation and operates without regard to
66   cycle boundaries.
67 
68   System reset should not return, if it returns, it means the system does
69   not support cold reset.
70 **/
71 VOID
72 EFIAPI
ResetCold(VOID)73 ResetCold (
74   VOID
75   )
76 {
77   IoWrite8 ((UINTN)mAcpiBoardInfo.ResetRegAddress, mAcpiBoardInfo.ResetValue);
78   CpuDeadLoop ();
79 }
80 
81 /**
82   Calling this function causes a system-wide initialization. The processors
83   are set to their initial state, and pending cycles are not corrupted.
84 
85   System reset should not return, if it returns, it means the system does
86   not support warm reset.
87 **/
88 VOID
89 EFIAPI
ResetWarm(VOID)90 ResetWarm (
91   VOID
92   )
93 {
94   IoWrite8 ((UINTN)mAcpiBoardInfo.ResetRegAddress, mAcpiBoardInfo.ResetValue);
95   CpuDeadLoop ();
96 }
97 
98 /**
99   Calling this function causes the system to enter a power state equivalent
100   to the ACPI G2/S5 or G3 states.
101 
102   System shutdown should not return, if it returns, it means the system does
103   not support shut down reset.
104 **/
105 VOID
106 EFIAPI
ResetShutdown(VOID)107 ResetShutdown (
108   VOID
109   )
110 {
111   UINTN              PmCtrlReg;
112 
113   //
114   // GPE0_EN should be disabled to avoid any GPI waking up the system from S5
115   //
116   IoWrite16 ((UINTN)mAcpiBoardInfo.PmGpeEnBase,  0);
117 
118   //
119   // Clear Power Button Status
120   //
121   IoWrite16((UINTN) mAcpiBoardInfo.PmEvtBase, BIT8);
122 
123   //
124   // Transform system into S5 sleep state
125   //
126   PmCtrlReg = (UINTN)mAcpiBoardInfo.PmCtrlRegBase;
127   IoAndThenOr16 (PmCtrlReg, (UINT16) ~0x3c00, (UINT16) (7 << 10));
128   IoOr16 (PmCtrlReg, BIT13);
129   CpuDeadLoop ();
130 
131   ASSERT (FALSE);
132 }
133 
134 /**
135   Calling this function causes the system to enter a power state for capsule
136   update.
137 
138   Reset update should not return, if it returns, it means the system does
139   not support capsule update.
140 
141 **/
142 VOID
143 EFIAPI
EnterS3WithImmediateWake(VOID)144 EnterS3WithImmediateWake (
145   VOID
146   )
147 {
148   AcpiPmControl (5);
149   ASSERT (FALSE);
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
ResetPlatformSpecific(IN UINTN DataSize,IN VOID * ResetData)165 ResetPlatformSpecific (
166   IN UINTN   DataSize,
167   IN VOID    *ResetData
168   )
169 {
170   ResetCold ();
171 }
172