]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
OvmfPkg: Modify QemuFwCfgLib to use new IoLib class library
[mirror_edk2.git] / OvmfPkg / Library / QemuFwCfgLib / QemuFwCfgLib.c
... / ...
CommitLineData
1/** @file\r
2\r
3 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>\r
4 Copyright (C) 2013, Red Hat, Inc.\r
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "Uefi.h"\r
18#include <Library/BaseLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/IoLib.h>\r
22#include <Library/QemuFwCfgLib.h>\r
23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/UefiBootServicesTableLib.h>\r
25\r
26#include "QemuFwCfgLibInternal.h"\r
27\r
28\r
29/**\r
30 Selects a firmware configuration item for reading.\r
31 \r
32 Following this call, any data read from this item will start from\r
33 the beginning of the configuration item's data.\r
34\r
35 @param[in] QemuFwCfgItem - Firmware Configuration item to read\r
36\r
37**/\r
38VOID\r
39EFIAPI\r
40QemuFwCfgSelectItem (\r
41 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem\r
42 )\r
43{\r
44 DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
45 IoWrite16 (0x510, (UINT16)(UINTN) QemuFwCfgItem);\r
46}\r
47\r
48\r
49/**\r
50 Transfer an array of bytes using the DMA interface.\r
51\r
52 @param[in] Size Size in bytes to transfer.\r
53 @param[in,out] Buffer Buffer to read data into or write data from. May be\r
54 NULL if Size is zero.\r
55 @param[in] Write TRUE if writing to fw_cfg from Buffer, FALSE if\r
56 reading from fw_cfg into Buffer.\r
57**/\r
58VOID\r
59InternalQemuFwCfgDmaBytes (\r
60 IN UINT32 Size,\r
61 IN OUT VOID *Buffer OPTIONAL,\r
62 IN BOOLEAN Write\r
63 )\r
64{\r
65 volatile FW_CFG_DMA_ACCESS Access;\r
66 UINT32 AccessHigh, AccessLow;\r
67 UINT32 Status;\r
68\r
69 if (Size == 0) {\r
70 return;\r
71 }\r
72\r
73 Access.Control = SwapBytes32 (\r
74 Write ? FW_CFG_DMA_CTL_WRITE : FW_CFG_DMA_CTL_READ\r
75 );\r
76 Access.Length = SwapBytes32 (Size);\r
77 Access.Address = SwapBytes64 ((UINTN)Buffer);\r
78\r
79 //\r
80 // Delimit the transfer from (a) modifications to Access, (b) in case of a\r
81 // write, from writes to Buffer by the caller.\r
82 //\r
83 MemoryFence ();\r
84\r
85 //\r
86 // Start the transfer.\r
87 //\r
88 AccessHigh = (UINT32)RShiftU64 ((UINTN)&Access, 32);\r
89 AccessLow = (UINT32)(UINTN)&Access;\r
90 IoWrite32 (0x514, SwapBytes32 (AccessHigh));\r
91 IoWrite32 (0x518, SwapBytes32 (AccessLow));\r
92\r
93 //\r
94 // Don't look at Access.Control before starting the transfer.\r
95 //\r
96 MemoryFence ();\r
97\r
98 //\r
99 // Wait for the transfer to complete.\r
100 //\r
101 do {\r
102 Status = SwapBytes32 (Access.Control);\r
103 ASSERT ((Status & FW_CFG_DMA_CTL_ERROR) == 0);\r
104 } while (Status != 0);\r
105\r
106 //\r
107 // After a read, the caller will want to use Buffer.\r
108 //\r
109 MemoryFence ();\r
110}\r
111\r
112\r
113/**\r
114 Reads firmware configuration bytes into a buffer\r
115\r
116 @param[in] Size - Size in bytes to read\r
117 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)\r
118\r
119**/\r
120VOID\r
121EFIAPI\r
122InternalQemuFwCfgReadBytes (\r
123 IN UINTN Size,\r
124 IN VOID *Buffer OPTIONAL\r
125 )\r
126{\r
127 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
128 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FALSE);\r
129 return;\r
130 }\r
131 IoReadFifo8 (0x511, Size, Buffer);\r
132}\r
133\r
134\r
135/**\r
136 Reads firmware configuration bytes into a buffer\r
137\r
138 If called multiple times, then the data read will\r
139 continue at the offset of the firmware configuration\r
140 item where the previous read ended.\r
141\r
142 @param[in] Size - Size in bytes to read\r
143 @param[in] Buffer - Buffer to store data into\r
144\r
145**/\r
146VOID\r
147EFIAPI\r
148QemuFwCfgReadBytes (\r
149 IN UINTN Size,\r
150 IN VOID *Buffer\r
151 )\r
152{\r
153 if (InternalQemuFwCfgIsAvailable ()) {\r
154 InternalQemuFwCfgReadBytes (Size, Buffer);\r
155 } else {\r
156 ZeroMem (Buffer, Size);\r
157 }\r
158}\r
159\r
160/**\r
161 Write firmware configuration bytes from a buffer\r
162\r
163 If called multiple times, then the data written will\r
164 continue at the offset of the firmware configuration\r
165 item where the previous write ended.\r
166\r
167 @param[in] Size - Size in bytes to write\r
168 @param[in] Buffer - Buffer to read data from\r
169\r
170**/\r
171VOID\r
172EFIAPI\r
173QemuFwCfgWriteBytes (\r
174 IN UINTN Size,\r
175 IN VOID *Buffer\r
176 )\r
177{\r
178 if (InternalQemuFwCfgIsAvailable ()) {\r
179 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
180 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, TRUE);\r
181 return;\r
182 }\r
183 IoWriteFifo8 (0x511, Size, Buffer);\r
184 }\r
185}\r
186\r
187\r
188/**\r
189 Reads a UINT8 firmware configuration value\r
190\r
191 @return Value of Firmware Configuration item read\r
192\r
193**/\r
194UINT8\r
195EFIAPI\r
196QemuFwCfgRead8 (\r
197 VOID\r
198 )\r
199{\r
200 UINT8 Result;\r
201\r
202 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
203\r
204 return Result;\r
205}\r
206\r
207\r
208/**\r
209 Reads a UINT16 firmware configuration value\r
210\r
211 @return Value of Firmware Configuration item read\r
212\r
213**/\r
214UINT16\r
215EFIAPI\r
216QemuFwCfgRead16 (\r
217 VOID\r
218 )\r
219{\r
220 UINT16 Result;\r
221\r
222 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
223\r
224 return Result;\r
225}\r
226\r
227\r
228/**\r
229 Reads a UINT32 firmware configuration value\r
230\r
231 @return Value of Firmware Configuration item read\r
232\r
233**/\r
234UINT32\r
235EFIAPI\r
236QemuFwCfgRead32 (\r
237 VOID\r
238 )\r
239{\r
240 UINT32 Result;\r
241\r
242 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
243\r
244 return Result;\r
245}\r
246\r
247\r
248/**\r
249 Reads a UINT64 firmware configuration value\r
250\r
251 @return Value of Firmware Configuration item read\r
252\r
253**/\r
254UINT64\r
255EFIAPI\r
256QemuFwCfgRead64 (\r
257 VOID\r
258 )\r
259{\r
260 UINT64 Result;\r
261\r
262 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
263\r
264 return Result;\r
265}\r
266\r
267\r
268/**\r
269 Find the configuration item corresponding to the firmware configuration file.\r
270\r
271 @param[in] Name - Name of file to look up.\r
272 @param[out] Item - Configuration item corresponding to the file, to be passed\r
273 to QemuFwCfgSelectItem ().\r
274 @param[out] Size - Number of bytes in the file.\r
275\r
276 @return RETURN_SUCCESS If file is found.\r
277 RETURN_NOT_FOUND If file is not found.\r
278 RETURN_UNSUPPORTED If firmware configuration is unavailable.\r
279\r
280**/\r
281RETURN_STATUS\r
282EFIAPI\r
283QemuFwCfgFindFile (\r
284 IN CONST CHAR8 *Name,\r
285 OUT FIRMWARE_CONFIG_ITEM *Item,\r
286 OUT UINTN *Size\r
287 )\r
288{\r
289 UINT32 Count;\r
290 UINT32 Idx;\r
291\r
292 if (!InternalQemuFwCfgIsAvailable ()) {\r
293 return RETURN_UNSUPPORTED;\r
294 }\r
295\r
296 QemuFwCfgSelectItem (QemuFwCfgItemFileDir);\r
297 Count = SwapBytes32 (QemuFwCfgRead32 ());\r
298\r
299 for (Idx = 0; Idx < Count; ++Idx) {\r
300 UINT32 FileSize;\r
301 UINT16 FileSelect;\r
302 UINT16 FileReserved;\r
303 CHAR8 FName[QEMU_FW_CFG_FNAME_SIZE];\r
304\r
305 FileSize = QemuFwCfgRead32 ();\r
306 FileSelect = QemuFwCfgRead16 ();\r
307 FileReserved = QemuFwCfgRead16 ();\r
308 (VOID) FileReserved; /* Force a do-nothing reference. */\r
309 InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
310\r
311 if (AsciiStrCmp (Name, FName) == 0) {\r
312 *Item = SwapBytes16 (FileSelect);\r
313 *Size = SwapBytes32 (FileSize);\r
314 return RETURN_SUCCESS;\r
315 }\r
316 }\r
317\r
318 return RETURN_NOT_FOUND;\r
319}\r
320\r
321\r
322/**\r
323 Determine if S3 support is explicitly enabled.\r
324\r
325 @retval TRUE if S3 support is explicitly enabled.\r
326 FALSE otherwise. This includes unavailability of the firmware\r
327 configuration interface.\r
328**/\r
329BOOLEAN\r
330EFIAPI\r
331QemuFwCfgS3Enabled (\r
332 VOID\r
333 )\r
334{\r
335 RETURN_STATUS Status;\r
336 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
337 UINTN FwCfgSize;\r
338 UINT8 SystemStates[6];\r
339\r
340 Status = QemuFwCfgFindFile ("etc/system-states", &FwCfgItem, &FwCfgSize);\r
341 if (Status != RETURN_SUCCESS || FwCfgSize != sizeof SystemStates) {\r
342 return FALSE;\r
343 }\r
344 QemuFwCfgSelectItem (FwCfgItem);\r
345 QemuFwCfgReadBytes (sizeof SystemStates, SystemStates);\r
346 return (BOOLEAN) (SystemStates[3] & BIT7);\r
347}\r