]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
OvmfPkg/QemuFwCfgLib: generalize InternalQemuFwCfgDmaBytes() to SKIP op
[mirror_edk2.git] / OvmfPkg / Library / QemuFwCfgLib / QemuFwCfgLib.c
CommitLineData
f1ec65ba 1/** @file\r
2\r
29874a8c 3 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>\r
0dc231c9 4 Copyright (C) 2013, Red Hat, Inc.\r
2b631390 5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
f1ec65ba 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
5297c0bf
LE
26#include "QemuFwCfgLibInternal.h"\r
27\r
f1ec65ba 28\r
f1ec65ba 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
2c8dcbc6 49/**\r
d055601e
LE
50 Transfer an array of bytes, or skip a number of bytes, using the DMA\r
51 interface.\r
2c8dcbc6 52\r
d055601e
LE
53 @param[in] Size Size in bytes to transfer or skip.\r
54\r
55 @param[in,out] Buffer Buffer to read data into or write data from. Ignored,\r
56 and may be NULL, if Size is zero, or Control is\r
57 FW_CFG_DMA_CTL_SKIP.\r
58\r
59 @param[in] Control One of the following:\r
60 FW_CFG_DMA_CTL_WRITE - write to fw_cfg from Buffer.\r
61 FW_CFG_DMA_CTL_READ - read from fw_cfg into Buffer.\r
62 FW_CFG_DMA_CTL_SKIP - skip bytes in fw_cfg.\r
2c8dcbc6
LE
63**/\r
64VOID\r
65InternalQemuFwCfgDmaBytes (\r
66 IN UINT32 Size,\r
67 IN OUT VOID *Buffer OPTIONAL,\r
d055601e 68 IN UINT32 Control\r
2c8dcbc6
LE
69 )\r
70{\r
71 volatile FW_CFG_DMA_ACCESS Access;\r
72 UINT32 AccessHigh, AccessLow;\r
73 UINT32 Status;\r
74\r
d055601e
LE
75 ASSERT (Control == FW_CFG_DMA_CTL_WRITE || Control == FW_CFG_DMA_CTL_READ ||\r
76 Control == FW_CFG_DMA_CTL_SKIP);\r
77\r
2c8dcbc6
LE
78 if (Size == 0) {\r
79 return;\r
80 }\r
81\r
d055601e 82 Access.Control = SwapBytes32 (Control);\r
2c8dcbc6
LE
83 Access.Length = SwapBytes32 (Size);\r
84 Access.Address = SwapBytes64 ((UINTN)Buffer);\r
85\r
86 //\r
87 // Delimit the transfer from (a) modifications to Access, (b) in case of a\r
88 // write, from writes to Buffer by the caller.\r
89 //\r
90 MemoryFence ();\r
91\r
92 //\r
93 // Start the transfer.\r
94 //\r
95 AccessHigh = (UINT32)RShiftU64 ((UINTN)&Access, 32);\r
96 AccessLow = (UINT32)(UINTN)&Access;\r
97 IoWrite32 (0x514, SwapBytes32 (AccessHigh));\r
98 IoWrite32 (0x518, SwapBytes32 (AccessLow));\r
99\r
100 //\r
101 // Don't look at Access.Control before starting the transfer.\r
102 //\r
103 MemoryFence ();\r
104\r
105 //\r
106 // Wait for the transfer to complete.\r
107 //\r
108 do {\r
109 Status = SwapBytes32 (Access.Control);\r
110 ASSERT ((Status & FW_CFG_DMA_CTL_ERROR) == 0);\r
111 } while (Status != 0);\r
112\r
113 //\r
114 // After a read, the caller will want to use Buffer.\r
115 //\r
116 MemoryFence ();\r
117}\r
118\r
119\r
f1ec65ba 120/**\r
121 Reads firmware configuration bytes into a buffer\r
122\r
123 @param[in] Size - Size in bytes to read\r
124 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)\r
125\r
126**/\r
127VOID\r
128EFIAPI\r
129InternalQemuFwCfgReadBytes (\r
130 IN UINTN Size,\r
131 IN VOID *Buffer OPTIONAL\r
132 )\r
133{\r
2c8dcbc6 134 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 135 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_READ);\r
2c8dcbc6
LE
136 return;\r
137 }\r
f1ec65ba 138 IoReadFifo8 (0x511, Size, Buffer);\r
139}\r
140\r
141\r
142/**\r
143 Reads firmware configuration bytes into a buffer\r
144\r
145 If called multiple times, then the data read will\r
146 continue at the offset of the firmware configuration\r
147 item where the previous read ended.\r
148\r
149 @param[in] Size - Size in bytes to read\r
150 @param[in] Buffer - Buffer to store data into\r
151\r
152**/\r
153VOID\r
154EFIAPI\r
155QemuFwCfgReadBytes (\r
156 IN UINTN Size,\r
157 IN VOID *Buffer\r
158 )\r
159{\r
0dc231c9 160 if (InternalQemuFwCfgIsAvailable ()) {\r
f1ec65ba 161 InternalQemuFwCfgReadBytes (Size, Buffer);\r
162 } else {\r
163 ZeroMem (Buffer, Size);\r
164 }\r
165}\r
166\r
29874a8c 167/**\r
168 Write firmware configuration bytes from a buffer\r
169\r
170 If called multiple times, then the data written will\r
171 continue at the offset of the firmware configuration\r
172 item where the previous write ended.\r
173\r
174 @param[in] Size - Size in bytes to write\r
175 @param[in] Buffer - Buffer to read data from\r
176\r
177**/\r
178VOID\r
179EFIAPI\r
180QemuFwCfgWriteBytes (\r
181 IN UINTN Size,\r
182 IN VOID *Buffer\r
183 )\r
184{\r
0dc231c9 185 if (InternalQemuFwCfgIsAvailable ()) {\r
2c8dcbc6 186 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 187 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_WRITE);\r
2c8dcbc6
LE
188 return;\r
189 }\r
29874a8c 190 IoWriteFifo8 (0x511, Size, Buffer);\r
191 }\r
192}\r
193\r
f1ec65ba 194\r
195/**\r
196 Reads a UINT8 firmware configuration value\r
197\r
198 @return Value of Firmware Configuration item read\r
199\r
200**/\r
201UINT8\r
202EFIAPI\r
203QemuFwCfgRead8 (\r
204 VOID\r
205 )\r
206{\r
207 UINT8 Result;\r
208\r
209 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
210\r
211 return Result;\r
212}\r
213\r
214\r
215/**\r
216 Reads a UINT16 firmware configuration value\r
217\r
218 @return Value of Firmware Configuration item read\r
219\r
220**/\r
221UINT16\r
222EFIAPI\r
223QemuFwCfgRead16 (\r
224 VOID\r
225 )\r
226{\r
227 UINT16 Result;\r
228\r
229 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
230\r
231 return Result;\r
232}\r
233\r
234\r
235/**\r
236 Reads a UINT32 firmware configuration value\r
237\r
238 @return Value of Firmware Configuration item read\r
239\r
240**/\r
241UINT32\r
242EFIAPI\r
243QemuFwCfgRead32 (\r
244 VOID\r
245 )\r
246{\r
247 UINT32 Result;\r
248\r
249 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
250\r
251 return Result;\r
252}\r
253\r
254\r
255/**\r
256 Reads a UINT64 firmware configuration value\r
257\r
258 @return Value of Firmware Configuration item read\r
259\r
260**/\r
261UINT64\r
262EFIAPI\r
263QemuFwCfgRead64 (\r
264 VOID\r
265 )\r
266{\r
267 UINT64 Result;\r
268\r
269 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
270\r
271 return Result;\r
272}\r
273\r
274\r
0ac9bc9b 275/**\r
276 Find the configuration item corresponding to the firmware configuration file.\r
277\r
278 @param[in] Name - Name of file to look up.\r
279 @param[out] Item - Configuration item corresponding to the file, to be passed\r
280 to QemuFwCfgSelectItem ().\r
281 @param[out] Size - Number of bytes in the file.\r
282\r
283 @return RETURN_SUCCESS If file is found.\r
284 RETURN_NOT_FOUND If file is not found.\r
285 RETURN_UNSUPPORTED If firmware configuration is unavailable.\r
286\r
287**/\r
288RETURN_STATUS\r
289EFIAPI\r
290QemuFwCfgFindFile (\r
291 IN CONST CHAR8 *Name,\r
292 OUT FIRMWARE_CONFIG_ITEM *Item,\r
293 OUT UINTN *Size\r
294 )\r
295{\r
296 UINT32 Count;\r
297 UINT32 Idx;\r
298\r
0dc231c9 299 if (!InternalQemuFwCfgIsAvailable ()) {\r
0ac9bc9b 300 return RETURN_UNSUPPORTED;\r
301 }\r
302\r
303 QemuFwCfgSelectItem (QemuFwCfgItemFileDir);\r
304 Count = SwapBytes32 (QemuFwCfgRead32 ());\r
305\r
306 for (Idx = 0; Idx < Count; ++Idx) {\r
307 UINT32 FileSize;\r
308 UINT16 FileSelect;\r
309 UINT16 FileReserved;\r
6a904296 310 CHAR8 FName[QEMU_FW_CFG_FNAME_SIZE];\r
0ac9bc9b 311\r
312 FileSize = QemuFwCfgRead32 ();\r
313 FileSelect = QemuFwCfgRead16 ();\r
314 FileReserved = QemuFwCfgRead16 ();\r
c6910aed 315 (VOID) FileReserved; /* Force a do-nothing reference. */\r
0ac9bc9b 316 InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
317\r
318 if (AsciiStrCmp (Name, FName) == 0) {\r
319 *Item = SwapBytes16 (FileSelect);\r
320 *Size = SwapBytes32 (FileSize);\r
321 return RETURN_SUCCESS;\r
322 }\r
323 }\r
324\r
325 return RETURN_NOT_FOUND;\r
326}\r
14eb7a5b
LE
327\r
328\r
329/**\r
330 Determine if S3 support is explicitly enabled.\r
331\r
332 @retval TRUE if S3 support is explicitly enabled.\r
333 FALSE otherwise. This includes unavailability of the firmware\r
334 configuration interface.\r
335**/\r
336BOOLEAN\r
337EFIAPI\r
338QemuFwCfgS3Enabled (\r
339 VOID\r
340 )\r
341{\r
342 RETURN_STATUS Status;\r
343 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
344 UINTN FwCfgSize;\r
345 UINT8 SystemStates[6];\r
346\r
347 Status = QemuFwCfgFindFile ("etc/system-states", &FwCfgItem, &FwCfgSize);\r
348 if (Status != RETURN_SUCCESS || FwCfgSize != sizeof SystemStates) {\r
349 return FALSE;\r
350 }\r
351 QemuFwCfgSelectItem (FwCfgItem);\r
352 QemuFwCfgReadBytes (sizeof SystemStates, SystemStates);\r
353 return (BOOLEAN) (SystemStates[3] & BIT7);\r
354}\r