]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
OvmfPkg/QemuFwCfgLib: Add option to dynamic alloc FW_CFG_DMA Access
[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
21ca2f28 45 IoWrite16 (FW_CFG_IO_SELECTOR, (UINT16)(UINTN) QemuFwCfgItem);\r
f1ec65ba 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
7cfe445d
BS
71 volatile FW_CFG_DMA_ACCESS LocalAccess;\r
72 volatile FW_CFG_DMA_ACCESS *Access;\r
2c8dcbc6
LE
73 UINT32 AccessHigh, AccessLow;\r
74 UINT32 Status;\r
75\r
d055601e
LE
76 ASSERT (Control == FW_CFG_DMA_CTL_WRITE || Control == FW_CFG_DMA_CTL_READ ||\r
77 Control == FW_CFG_DMA_CTL_SKIP);\r
78\r
2c8dcbc6
LE
79 if (Size == 0) {\r
80 return;\r
81 }\r
82\r
7cfe445d
BS
83 Access = &LocalAccess;\r
84\r
85 Access->Control = SwapBytes32 (Control);\r
86 Access->Length = SwapBytes32 (Size);\r
87 Access->Address = SwapBytes64 ((UINTN)Buffer);\r
2c8dcbc6
LE
88\r
89 //\r
90 // Delimit the transfer from (a) modifications to Access, (b) in case of a\r
91 // write, from writes to Buffer by the caller.\r
92 //\r
93 MemoryFence ();\r
94\r
95 //\r
96 // Start the transfer.\r
97 //\r
7cfe445d
BS
98 AccessHigh = (UINT32)RShiftU64 ((UINTN)Access, 32);\r
99 AccessLow = (UINT32)(UINTN)Access;\r
ed1a2d42
LE
100 IoWrite32 (FW_CFG_IO_DMA_ADDRESS, SwapBytes32 (AccessHigh));\r
101 IoWrite32 (FW_CFG_IO_DMA_ADDRESS + 4, SwapBytes32 (AccessLow));\r
2c8dcbc6
LE
102\r
103 //\r
104 // Don't look at Access.Control before starting the transfer.\r
105 //\r
106 MemoryFence ();\r
107\r
108 //\r
109 // Wait for the transfer to complete.\r
110 //\r
111 do {\r
7cfe445d 112 Status = SwapBytes32 (Access->Control);\r
2c8dcbc6
LE
113 ASSERT ((Status & FW_CFG_DMA_CTL_ERROR) == 0);\r
114 } while (Status != 0);\r
115\r
116 //\r
117 // After a read, the caller will want to use Buffer.\r
118 //\r
119 MemoryFence ();\r
120}\r
121\r
122\r
f1ec65ba 123/**\r
124 Reads firmware configuration bytes into a buffer\r
125\r
126 @param[in] Size - Size in bytes to read\r
127 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)\r
128\r
129**/\r
130VOID\r
131EFIAPI\r
132InternalQemuFwCfgReadBytes (\r
133 IN UINTN Size,\r
134 IN VOID *Buffer OPTIONAL\r
135 )\r
136{\r
2c8dcbc6 137 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 138 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_READ);\r
2c8dcbc6
LE
139 return;\r
140 }\r
509e6b5a 141 IoReadFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
f1ec65ba 142}\r
143\r
144\r
145/**\r
146 Reads firmware configuration bytes into a buffer\r
147\r
148 If called multiple times, then the data read will\r
149 continue at the offset of the firmware configuration\r
150 item where the previous read ended.\r
151\r
152 @param[in] Size - Size in bytes to read\r
153 @param[in] Buffer - Buffer to store data into\r
154\r
155**/\r
156VOID\r
157EFIAPI\r
158QemuFwCfgReadBytes (\r
159 IN UINTN Size,\r
160 IN VOID *Buffer\r
161 )\r
162{\r
0dc231c9 163 if (InternalQemuFwCfgIsAvailable ()) {\r
f1ec65ba 164 InternalQemuFwCfgReadBytes (Size, Buffer);\r
165 } else {\r
166 ZeroMem (Buffer, Size);\r
167 }\r
168}\r
169\r
29874a8c 170/**\r
171 Write firmware configuration bytes from a buffer\r
172\r
173 If called multiple times, then the data written will\r
174 continue at the offset of the firmware configuration\r
175 item where the previous write ended.\r
176\r
177 @param[in] Size - Size in bytes to write\r
178 @param[in] Buffer - Buffer to read data from\r
179\r
180**/\r
181VOID\r
182EFIAPI\r
183QemuFwCfgWriteBytes (\r
184 IN UINTN Size,\r
185 IN VOID *Buffer\r
186 )\r
187{\r
0dc231c9 188 if (InternalQemuFwCfgIsAvailable ()) {\r
2c8dcbc6 189 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
d055601e 190 InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_WRITE);\r
2c8dcbc6
LE
191 return;\r
192 }\r
509e6b5a 193 IoWriteFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
29874a8c 194 }\r
195}\r
196\r
f1ec65ba 197\r
fcca9f67
LE
198/**\r
199 Skip bytes in the firmware configuration item.\r
200\r
201 Increase the offset of the firmware configuration item without transferring\r
202 bytes between the item and a caller-provided buffer. Subsequent read, write\r
203 or skip operations will commence at the increased offset.\r
204\r
205 @param[in] Size Number of bytes to skip.\r
206**/\r
207VOID\r
208EFIAPI\r
209QemuFwCfgSkipBytes (\r
210 IN UINTN Size\r
211 )\r
212{\r
213 UINTN ChunkSize;\r
214 UINT8 SkipBuffer[256];\r
215\r
216 if (!InternalQemuFwCfgIsAvailable ()) {\r
217 return;\r
218 }\r
219\r
220 if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
221 InternalQemuFwCfgDmaBytes ((UINT32)Size, NULL, FW_CFG_DMA_CTL_SKIP);\r
222 return;\r
223 }\r
224\r
225 //\r
226 // Emulate the skip by reading data in chunks, and throwing it away. The\r
227 // implementation below is suitable even for phases where RAM or dynamic\r
228 // allocation is not available or appropriate. It also doesn't affect the\r
229 // static data footprint for client modules. Large skips are not expected,\r
230 // therefore this fallback is not performance critical. The size of\r
231 // SkipBuffer is thought not to exert a large pressure on the stack in any\r
232 // phase.\r
233 //\r
234 while (Size > 0) {\r
235 ChunkSize = MIN (Size, sizeof SkipBuffer);\r
509e6b5a 236 IoReadFifo8 (FW_CFG_IO_DATA, ChunkSize, SkipBuffer);\r
fcca9f67
LE
237 Size -= ChunkSize;\r
238 }\r
239}\r
240\r
241\r
f1ec65ba 242/**\r
243 Reads a UINT8 firmware configuration value\r
244\r
245 @return Value of Firmware Configuration item read\r
246\r
247**/\r
248UINT8\r
249EFIAPI\r
250QemuFwCfgRead8 (\r
251 VOID\r
252 )\r
253{\r
254 UINT8 Result;\r
255\r
256 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
257\r
258 return Result;\r
259}\r
260\r
261\r
262/**\r
263 Reads a UINT16 firmware configuration value\r
264\r
265 @return Value of Firmware Configuration item read\r
266\r
267**/\r
268UINT16\r
269EFIAPI\r
270QemuFwCfgRead16 (\r
271 VOID\r
272 )\r
273{\r
274 UINT16 Result;\r
275\r
276 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
277\r
278 return Result;\r
279}\r
280\r
281\r
282/**\r
283 Reads a UINT32 firmware configuration value\r
284\r
285 @return Value of Firmware Configuration item read\r
286\r
287**/\r
288UINT32\r
289EFIAPI\r
290QemuFwCfgRead32 (\r
291 VOID\r
292 )\r
293{\r
294 UINT32 Result;\r
295\r
296 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
297\r
298 return Result;\r
299}\r
300\r
301\r
302/**\r
303 Reads a UINT64 firmware configuration value\r
304\r
305 @return Value of Firmware Configuration item read\r
306\r
307**/\r
308UINT64\r
309EFIAPI\r
310QemuFwCfgRead64 (\r
311 VOID\r
312 )\r
313{\r
314 UINT64 Result;\r
315\r
316 QemuFwCfgReadBytes (sizeof (Result), &Result);\r
317\r
318 return Result;\r
319}\r
320\r
321\r
0ac9bc9b 322/**\r
323 Find the configuration item corresponding to the firmware configuration file.\r
324\r
325 @param[in] Name - Name of file to look up.\r
326 @param[out] Item - Configuration item corresponding to the file, to be passed\r
327 to QemuFwCfgSelectItem ().\r
328 @param[out] Size - Number of bytes in the file.\r
329\r
330 @return RETURN_SUCCESS If file is found.\r
331 RETURN_NOT_FOUND If file is not found.\r
332 RETURN_UNSUPPORTED If firmware configuration is unavailable.\r
333\r
334**/\r
335RETURN_STATUS\r
336EFIAPI\r
337QemuFwCfgFindFile (\r
338 IN CONST CHAR8 *Name,\r
339 OUT FIRMWARE_CONFIG_ITEM *Item,\r
340 OUT UINTN *Size\r
341 )\r
342{\r
343 UINT32 Count;\r
344 UINT32 Idx;\r
345\r
0dc231c9 346 if (!InternalQemuFwCfgIsAvailable ()) {\r
0ac9bc9b 347 return RETURN_UNSUPPORTED;\r
348 }\r
349\r
350 QemuFwCfgSelectItem (QemuFwCfgItemFileDir);\r
351 Count = SwapBytes32 (QemuFwCfgRead32 ());\r
352\r
353 for (Idx = 0; Idx < Count; ++Idx) {\r
354 UINT32 FileSize;\r
355 UINT16 FileSelect;\r
356 UINT16 FileReserved;\r
6a904296 357 CHAR8 FName[QEMU_FW_CFG_FNAME_SIZE];\r
0ac9bc9b 358\r
359 FileSize = QemuFwCfgRead32 ();\r
360 FileSelect = QemuFwCfgRead16 ();\r
361 FileReserved = QemuFwCfgRead16 ();\r
c6910aed 362 (VOID) FileReserved; /* Force a do-nothing reference. */\r
0ac9bc9b 363 InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
364\r
365 if (AsciiStrCmp (Name, FName) == 0) {\r
366 *Item = SwapBytes16 (FileSelect);\r
367 *Size = SwapBytes32 (FileSize);\r
368 return RETURN_SUCCESS;\r
369 }\r
370 }\r
371\r
372 return RETURN_NOT_FOUND;\r
373}\r