3 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
4 Copyright (C) 2013, Red Hat, Inc.
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/QemuFwCfgLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 #include "QemuFwCfgLibInternal.h"
29 Reads an 8-bit I/O port fifo into a block of memory.
31 Reads the 8-bit I/O fifo port specified by Port.
33 The port is read Count times, and the read data is
34 stored in the provided Buffer.
36 This function must guarantee that all I/O read and write operations are
39 If 8-bit I/O port operations are not supported, then ASSERT().
41 @param Port The I/O port to read.
42 @param Count The number of times to read I/O port.
43 @param Buffer The buffer to store the read data into.
55 Writes an 8-bit I/O port fifo from a block of memory.
57 Writes the 8-bit I/O fifo port specified by Port.
59 The port is written Count times, and the data are obtained
60 from the provided Buffer.
62 This function must guarantee that all I/O read and write operations are
65 If 8-bit I/O port operations are not supported, then ASSERT().
67 @param Port The I/O port to read.
68 @param Count The number of times to read I/O port.
69 @param Buffer The buffer to store the read data into.
82 Selects a firmware configuration item for reading.
84 Following this call, any data read from this item will start from
85 the beginning of the configuration item's data.
87 @param[in] QemuFwCfgItem - Firmware Configuration item to read
93 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
96 DEBUG ((EFI_D_INFO
, "Select Item: 0x%x\n", (UINT16
)(UINTN
) QemuFwCfgItem
));
97 IoWrite16 (0x510, (UINT16
)(UINTN
) QemuFwCfgItem
);
102 Transfer an array of bytes using the DMA interface.
104 @param[in] Size Size in bytes to transfer.
105 @param[in,out] Buffer Buffer to read data into or write data from. May be
106 NULL if Size is zero.
107 @param[in] Write TRUE if writing to fw_cfg from Buffer, FALSE if
108 reading from fw_cfg into Buffer.
111 InternalQemuFwCfgDmaBytes (
113 IN OUT VOID
*Buffer OPTIONAL
,
117 volatile FW_CFG_DMA_ACCESS Access
;
118 UINT32 AccessHigh
, AccessLow
;
125 Access
.Control
= SwapBytes32 (
126 Write
? FW_CFG_DMA_CTL_WRITE
: FW_CFG_DMA_CTL_READ
128 Access
.Length
= SwapBytes32 (Size
);
129 Access
.Address
= SwapBytes64 ((UINTN
)Buffer
);
132 // Delimit the transfer from (a) modifications to Access, (b) in case of a
133 // write, from writes to Buffer by the caller.
138 // Start the transfer.
140 AccessHigh
= (UINT32
)RShiftU64 ((UINTN
)&Access
, 32);
141 AccessLow
= (UINT32
)(UINTN
)&Access
;
142 IoWrite32 (0x514, SwapBytes32 (AccessHigh
));
143 IoWrite32 (0x518, SwapBytes32 (AccessLow
));
146 // Don't look at Access.Control before starting the transfer.
151 // Wait for the transfer to complete.
154 Status
= SwapBytes32 (Access
.Control
);
155 ASSERT ((Status
& FW_CFG_DMA_CTL_ERROR
) == 0);
156 } while (Status
!= 0);
159 // After a read, the caller will want to use Buffer.
166 Reads firmware configuration bytes into a buffer
168 @param[in] Size - Size in bytes to read
169 @param[in] Buffer - Buffer to store data into (OPTIONAL if Size is 0)
174 InternalQemuFwCfgReadBytes (
176 IN VOID
*Buffer OPTIONAL
179 if (InternalQemuFwCfgDmaIsAvailable () && Size
<= MAX_UINT32
) {
180 InternalQemuFwCfgDmaBytes ((UINT32
)Size
, Buffer
, FALSE
);
183 IoReadFifo8 (0x511, Size
, Buffer
);
188 Reads firmware configuration bytes into a buffer
190 If called multiple times, then the data read will
191 continue at the offset of the firmware configuration
192 item where the previous read ended.
194 @param[in] Size - Size in bytes to read
195 @param[in] Buffer - Buffer to store data into
205 if (InternalQemuFwCfgIsAvailable ()) {
206 InternalQemuFwCfgReadBytes (Size
, Buffer
);
208 ZeroMem (Buffer
, Size
);
213 Write firmware configuration bytes from a buffer
215 If called multiple times, then the data written will
216 continue at the offset of the firmware configuration
217 item where the previous write ended.
219 @param[in] Size - Size in bytes to write
220 @param[in] Buffer - Buffer to read data from
225 QemuFwCfgWriteBytes (
230 if (InternalQemuFwCfgIsAvailable ()) {
231 if (InternalQemuFwCfgDmaIsAvailable () && Size
<= MAX_UINT32
) {
232 InternalQemuFwCfgDmaBytes ((UINT32
)Size
, Buffer
, TRUE
);
235 IoWriteFifo8 (0x511, Size
, Buffer
);
241 Reads a UINT8 firmware configuration value
243 @return Value of Firmware Configuration item read
254 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
261 Reads a UINT16 firmware configuration value
263 @return Value of Firmware Configuration item read
274 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
281 Reads a UINT32 firmware configuration value
283 @return Value of Firmware Configuration item read
294 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
301 Reads a UINT64 firmware configuration value
303 @return Value of Firmware Configuration item read
314 QemuFwCfgReadBytes (sizeof (Result
), &Result
);
321 Find the configuration item corresponding to the firmware configuration file.
323 @param[in] Name - Name of file to look up.
324 @param[out] Item - Configuration item corresponding to the file, to be passed
325 to QemuFwCfgSelectItem ().
326 @param[out] Size - Number of bytes in the file.
328 @return RETURN_SUCCESS If file is found.
329 RETURN_NOT_FOUND If file is not found.
330 RETURN_UNSUPPORTED If firmware configuration is unavailable.
336 IN CONST CHAR8
*Name
,
337 OUT FIRMWARE_CONFIG_ITEM
*Item
,
344 if (!InternalQemuFwCfgIsAvailable ()) {
345 return RETURN_UNSUPPORTED
;
348 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
349 Count
= SwapBytes32 (QemuFwCfgRead32 ());
351 for (Idx
= 0; Idx
< Count
; ++Idx
) {
355 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
357 FileSize
= QemuFwCfgRead32 ();
358 FileSelect
= QemuFwCfgRead16 ();
359 FileReserved
= QemuFwCfgRead16 ();
360 (VOID
) FileReserved
; /* Force a do-nothing reference. */
361 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
363 if (AsciiStrCmp (Name
, FName
) == 0) {
364 *Item
= SwapBytes16 (FileSelect
);
365 *Size
= SwapBytes32 (FileSize
);
366 return RETURN_SUCCESS
;
370 return RETURN_NOT_FOUND
;
375 Determine if S3 support is explicitly enabled.
377 @retval TRUE if S3 support is explicitly enabled.
378 FALSE otherwise. This includes unavailability of the firmware
379 configuration interface.
387 RETURN_STATUS Status
;
388 FIRMWARE_CONFIG_ITEM FwCfgItem
;
390 UINT8 SystemStates
[6];
392 Status
= QemuFwCfgFindFile ("etc/system-states", &FwCfgItem
, &FwCfgSize
);
393 if (Status
!= RETURN_SUCCESS
|| FwCfgSize
!= sizeof SystemStates
) {
396 QemuFwCfgSelectItem (FwCfgItem
);
397 QemuFwCfgReadBytes (sizeof SystemStates
, SystemStates
);
398 return (BOOLEAN
) (SystemStates
[3] & BIT7
);