]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/Library/KvmtoolRtcFdtClientLib/KvmtoolRtcFdtClientLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmVirtPkg / Library / KvmtoolRtcFdtClientLib / KvmtoolRtcFdtClientLib.c
CommitLineData
cd09c384
SM
1/** @file\r
2 FDT client library for motorola,mc146818 RTC driver\r
3\r
4 Copyright (c) 2020, ARM Limited. All rights reserved.<BR>\r
5\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include <Library/BaseLib.h>\r
11#include <Library/DebugLib.h>\r
12#include <Library/DxeServicesTableLib.h>\r
13#include <Library/PcdLib.h>\r
14#include <Library/UefiBootServicesTableLib.h>\r
15#include <Protocol/FdtClient.h>\r
16\r
17/** RTC Index register is at offset 0x0\r
18*/\r
2b16a4fb 19#define RTC_INDEX_REG_OFFSET 0x0ULL\r
cd09c384
SM
20\r
21/** RTC Target register is at offset 0x1\r
22*/\r
2b16a4fb 23#define RTC_TARGET_REG_OFFSET 0x1ULL\r
cd09c384
SM
24\r
25/** Add the RTC controller address range to the memory map.\r
26\r
27 @param [in] ImageHandle The handle to the image.\r
28 @param [in] RtcPageBase Base address of the RTC controller.\r
29\r
30 @retval EFI_SUCCESS Success.\r
31 @retval EFI_INVALID_PARAMETER A parameter is invalid.\r
32 @retval EFI_NOT_FOUND Flash device not found.\r
33**/\r
34STATIC\r
35EFI_STATUS\r
36KvmtoolRtcMapMemory (\r
2b16a4fb
MK
37 IN EFI_HANDLE ImageHandle,\r
38 IN EFI_PHYSICAL_ADDRESS RtcPageBase\r
cd09c384
SM
39 )\r
40{\r
41 EFI_STATUS Status;\r
42\r
43 Status = gDS->AddMemorySpace (\r
44 EfiGcdMemoryTypeMemoryMappedIo,\r
45 RtcPageBase,\r
46 EFI_PAGE_SIZE,\r
47 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
48 );\r
49 if (EFI_ERROR (Status)) {\r
50 DEBUG ((\r
2b16a4fb
MK
51 DEBUG_ERROR,\r
52 "Failed to add memory space. Status = %r\n",\r
cd09c384
SM
53 Status\r
54 ));\r
55 return Status;\r
56 }\r
57\r
58 Status = gDS->AllocateMemorySpace (\r
59 EfiGcdAllocateAddress,\r
60 EfiGcdMemoryTypeMemoryMappedIo,\r
61 0,\r
62 EFI_PAGE_SIZE,\r
63 &RtcPageBase,\r
64 ImageHandle,\r
65 NULL\r
66 );\r
67 if (EFI_ERROR (Status)) {\r
68 DEBUG ((\r
69 DEBUG_ERROR,\r
70 "Failed to allocate memory space. Status = %r\n",\r
71 Status\r
72 ));\r
73 gDS->RemoveMemorySpace (\r
74 RtcPageBase,\r
75 EFI_PAGE_SIZE\r
76 );\r
77 return Status;\r
78 }\r
79\r
80 Status = gDS->SetMemorySpaceAttributes (\r
81 RtcPageBase,\r
82 EFI_PAGE_SIZE,\r
83 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
84 );\r
85 if (EFI_ERROR (Status)) {\r
86 DEBUG ((\r
87 DEBUG_ERROR,\r
88 "Failed to set memory attributes. Status = %r\n",\r
89 Status\r
90 ));\r
91 gDS->FreeMemorySpace (\r
92 RtcPageBase,\r
93 EFI_PAGE_SIZE\r
94 );\r
95 gDS->RemoveMemorySpace (\r
96 RtcPageBase,\r
97 EFI_PAGE_SIZE\r
98 );\r
99 }\r
100\r
101 return Status;\r
102}\r
103\r
104/** Entrypoint for KvmtoolRtcFdtClientLib.\r
105\r
106 Locate the RTC node in the DT and update the Index and\r
107 Target register base addresses in the respective PCDs.\r
108 Add the RTC memory region to the memory map.\r
109 Disable the RTC node as the RTC is owned by UEFI.\r
110\r
111 @param [in] ImageHandle The handle to the image.\r
112 @param [in] SystemTable Pointer to the System Table.\r
113\r
114 @retval EFI_SUCCESS Success.\r
115 @retval EFI_INVALID_PARAMETER A parameter is invalid.\r
116 @retval EFI_NOT_FOUND Flash device not found.\r
117**/\r
118EFI_STATUS\r
119EFIAPI\r
120KvmtoolRtcFdtClientLibConstructor (\r
2b16a4fb
MK
121 IN EFI_HANDLE ImageHandle,\r
122 IN EFI_SYSTEM_TABLE *SystemTable\r
cd09c384
SM
123 )\r
124{\r
2b16a4fb
MK
125 EFI_STATUS Status;\r
126 FDT_CLIENT_PROTOCOL *FdtClient;\r
127 INT32 Node;\r
128 CONST UINT32 *Reg;\r
129 UINT32 RegSize;\r
130 UINT64 RegBase;\r
131 UINT64 Range;\r
132 RETURN_STATUS PcdStatus;\r
cd09c384
SM
133\r
134 Status = gBS->LocateProtocol (\r
135 &gFdtClientProtocolGuid,\r
136 NULL,\r
137 (VOID **)&FdtClient\r
138 );\r
139 ASSERT_EFI_ERROR (Status);\r
140\r
141 Status = FdtClient->FindCompatibleNode (\r
142 FdtClient,\r
143 "motorola,mc146818",\r
144 &Node\r
145 );\r
146 if (EFI_ERROR (Status)) {\r
147 DEBUG ((\r
148 DEBUG_ERROR,\r
149 "%a: No 'motorola,mc146818' compatible DT node found\n",\r
150 __FUNCTION__\r
151 ));\r
152 return Status;\r
153 }\r
154\r
155 Status = FdtClient->GetNodeProperty (\r
156 FdtClient,\r
157 Node,\r
158 "reg",\r
159 (CONST VOID **)&Reg,\r
160 &RegSize\r
161 );\r
162 if (EFI_ERROR (Status)) {\r
163 DEBUG ((\r
164 DEBUG_ERROR,\r
165 "%a: No 'reg' property found in 'motorola,mc146818' compatible DT node\n",\r
166 __FUNCTION__\r
167 ));\r
168 return Status;\r
169 }\r
170\r
171 ASSERT (RegSize == 16);\r
172\r
173 RegBase = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));\r
2b16a4fb 174 Range = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));\r
cd09c384
SM
175 DEBUG ((\r
176 DEBUG_INFO,\r
177 "Found motorola,mc146818 RTC @ 0x%Lx Range = 0x%x\n",\r
178 RegBase,\r
179 Range\r
180 ));\r
181\r
182 // The address range must cover the RTC Index and the Target registers.\r
183 ASSERT (Range >= 0x2);\r
184\r
185 // RTC Index register is at offset 0x0\r
186 PcdStatus = PcdSet64S (\r
187 PcdRtcIndexRegister64,\r
188 (RegBase + RTC_INDEX_REG_OFFSET)\r
189 );\r
190 ASSERT_RETURN_ERROR (PcdStatus);\r
191\r
192 // RTC Target register is at offset 0x1\r
193 PcdStatus = PcdSet64S (\r
194 PcdRtcTargetRegister64,\r
195 (RegBase + RTC_TARGET_REG_OFFSET)\r
196 );\r
197 ASSERT_RETURN_ERROR (PcdStatus);\r
198\r
199 Status = KvmtoolRtcMapMemory (ImageHandle, (RegBase & ~EFI_PAGE_MASK));\r
200 if (EFI_ERROR (Status)) {\r
201 DEBUG ((\r
202 DEBUG_ERROR,\r
203 "Failed to map memory for motorola,mc146818. Status = %r\n",\r
204 Status\r
205 ));\r
206 return Status;\r
207 }\r
208\r
209 //\r
210 // UEFI takes ownership of the RTC hardware, and exposes its functionality\r
211 // through the UEFI Runtime Services GetTime, SetTime, etc. This means we\r
212 // need to disable it in the device tree to prevent the OS from attaching\r
213 // its device driver as well.\r
214 //\r
215 Status = FdtClient->SetNodeProperty (\r
216 FdtClient,\r
217 Node,\r
218 "status",\r
219 "disabled",\r
220 sizeof ("disabled")\r
221 );\r
222 if (EFI_ERROR (Status)) {\r
223 DEBUG ((\r
224 DEBUG_WARN,\r
225 "Failed to set motorola,mc146818 status to 'disabled', Status = %r\n",\r
226 Status\r
227 ));\r
228 }\r
229\r
230 return EFI_SUCCESS;\r
231}\r