3 Stateful and implicitly initialized fw_cfg library implementation.
5 Copyright (C) 2013 - 2014, Red Hat, Inc.
6 Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/QemuFwCfgLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
26 #include <Protocol/FdtClient.h>
28 STATIC UINTN mFwCfgSelectorAddress
;
29 STATIC UINTN mFwCfgDataAddress
;
30 STATIC UINTN mFwCfgDmaAddress
;
33 Reads firmware configuration bytes into a buffer
35 @param[in] Size Size in bytes to read
36 @param[in] Buffer Buffer to store data into (OPTIONAL if Size is 0)
40 VOID (EFIAPI READ_BYTES_FUNCTION
) (
42 IN VOID
*Buffer OPTIONAL
46 // Forward declaration of the two implementations we have.
48 STATIC READ_BYTES_FUNCTION MmioReadBytes
;
49 STATIC READ_BYTES_FUNCTION DmaReadBytes
;
52 // This points to the one we detect at runtime.
54 STATIC READ_BYTES_FUNCTION
*InternalQemuFwCfgReadBytes
= MmioReadBytes
;
57 // Communication structure for DmaReadBytes(). All fields are encoded in big
69 // Macros for the FW_CFG_DMA_ACCESS.Control bitmap (in native encoding).
71 #define FW_CFG_DMA_CTL_ERROR BIT0
72 #define FW_CFG_DMA_CTL_READ BIT1
73 #define FW_CFG_DMA_CTL_SKIP BIT2
74 #define FW_CFG_DMA_CTL_SELECT BIT3
78 Returns a boolean indicating if the firmware configuration interface
81 This function may change fw_cfg state.
83 @retval TRUE The interface is available
84 @retval FALSE The interface is not available
89 QemuFwCfgIsAvailable (
93 return (BOOLEAN
)(mFwCfgSelectorAddress
!= 0 && mFwCfgDataAddress
!= 0);
104 FDT_CLIENT_PROTOCOL
*FdtClient
;
107 UINTN AddressCells
, SizeCells
;
108 UINT64 FwCfgSelectorAddress
;
109 UINT64 FwCfgSelectorSize
;
110 UINT64 FwCfgDataAddress
;
111 UINT64 FwCfgDataSize
;
112 UINT64 FwCfgDmaAddress
;
115 Status
= gBS
->LocateProtocol (&gFdtClientProtocolGuid
, NULL
,
116 (VOID
**)&FdtClient
);
117 ASSERT_EFI_ERROR (Status
);
119 Status
= FdtClient
->FindCompatibleNodeReg (FdtClient
, "qemu,fw-cfg-mmio",
120 (CONST VOID
**)&Reg
, &AddressCells
, &SizeCells
,
122 if (EFI_ERROR (Status
)) {
124 "%a: No 'qemu,fw-cfg-mmio' compatible DT node found (Status == %r)\n",
125 __FUNCTION__
, Status
));
129 ASSERT (AddressCells
== 2);
130 ASSERT (SizeCells
== 2);
131 ASSERT (RegSize
== 2 * sizeof (UINT64
));
133 FwCfgDataAddress
= SwapBytes64 (Reg
[0]);
135 FwCfgSelectorAddress
= FwCfgDataAddress
+ FwCfgDataSize
;
136 FwCfgSelectorSize
= 2;
139 // The following ASSERT()s express
141 // Address + Size - 1 <= MAX_UINTN
143 // for both registers, that is, that the last byte in each MMIO range is
144 // expressible as a MAX_UINTN. The form below is mathematically
145 // equivalent, and it also prevents any unsigned overflow before the
148 ASSERT (FwCfgSelectorAddress
<= MAX_UINTN
- FwCfgSelectorSize
+ 1);
149 ASSERT (FwCfgDataAddress
<= MAX_UINTN
- FwCfgDataSize
+ 1);
151 mFwCfgSelectorAddress
= FwCfgSelectorAddress
;
152 mFwCfgDataAddress
= FwCfgDataAddress
;
154 DEBUG ((EFI_D_INFO
, "Found FwCfg @ 0x%Lx/0x%Lx\n", FwCfgSelectorAddress
,
157 if (SwapBytes64 (Reg
[1]) >= 0x18) {
158 FwCfgDmaAddress
= FwCfgDataAddress
+ 0x10;
162 // See explanation above.
164 ASSERT (FwCfgDmaAddress
<= MAX_UINTN
- FwCfgDmaSize
+ 1);
166 DEBUG ((EFI_D_INFO
, "Found FwCfg DMA @ 0x%Lx\n", FwCfgDmaAddress
));
171 if (QemuFwCfgIsAvailable ()) {
174 QemuFwCfgSelectItem (QemuFwCfgItemSignature
);
175 Signature
= QemuFwCfgRead32 ();
176 if (Signature
== SIGNATURE_32 ('Q', 'E', 'M', 'U')) {
178 // For DMA support, we require the DTB to advertise the register, and the
179 // feature bitmap (which we read without DMA) to confirm the feature.
181 if (FwCfgDmaAddress
!= 0) {
184 QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion
);
185 Features
= QemuFwCfgRead32 ();
186 if ((Features
& BIT1
) != 0) {
187 mFwCfgDmaAddress
= FwCfgDmaAddress
;
188 InternalQemuFwCfgReadBytes
= DmaReadBytes
;
192 mFwCfgSelectorAddress
= 0;
193 mFwCfgDataAddress
= 0;
196 return RETURN_SUCCESS
;
201 Selects a firmware configuration item for reading.
203 Following this call, any data read from this item will start from the
204 beginning of the configuration item's data.
206 @param[in] QemuFwCfgItem Firmware Configuration item to read
211 QemuFwCfgSelectItem (
212 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
215 if (QemuFwCfgIsAvailable ()) {
216 MmioWrite16 (mFwCfgSelectorAddress
, SwapBytes16 ((UINT16
)QemuFwCfgItem
));
222 Slow READ_BYTES_FUNCTION.
229 IN VOID
*Buffer OPTIONAL
236 #ifdef MDE_CPU_AARCH64
246 #ifdef MDE_CPU_AARCH64
248 *(UINT64
*)Ptr
= MmioRead64 (mFwCfgDataAddress
);
252 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
257 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
263 *(UINT16
*)Ptr
= MmioRead16 (mFwCfgDataAddress
);
267 *Ptr
= MmioRead8 (mFwCfgDataAddress
);
273 Fast READ_BYTES_FUNCTION.
280 IN VOID
*Buffer OPTIONAL
283 volatile FW_CFG_DMA_ACCESS Access
;
290 ASSERT (Size
<= MAX_UINT32
);
292 Access
.Control
= SwapBytes32 (FW_CFG_DMA_CTL_READ
);
293 Access
.Length
= SwapBytes32 ((UINT32
)Size
);
294 Access
.Address
= SwapBytes64 ((UINT64
)(UINTN
)Buffer
);
297 // We shouldn't start the transfer before setting up Access.
302 // This will fire off the transfer.
304 #ifdef MDE_CPU_AARCH64
305 MmioWrite64 (mFwCfgDmaAddress
, SwapBytes64 ((UINT64
)&Access
));
307 MmioWrite32 ((UINT32
)(mFwCfgDmaAddress
+ 4), SwapBytes32 ((UINT32
)&Access
));
311 // We shouldn't look at Access.Control before starting the transfer.
316 Status
= SwapBytes32 (Access
.Control
);
317 ASSERT ((Status
& FW_CFG_DMA_CTL_ERROR
) == 0);
318 } while (Status
!= 0);
321 // The caller will want to access the transferred data.
328 Reads firmware configuration bytes into a buffer
330 If called multiple times, then the data read will continue at the offset of
331 the firmware configuration item where the previous read ended.
333 @param[in] Size Size in bytes to read
334 @param[in] Buffer Buffer to store data into
344 if (QemuFwCfgIsAvailable ()) {
345 InternalQemuFwCfgReadBytes (Size
, Buffer
);
347 ZeroMem (Buffer
, Size
);
352 Write firmware configuration bytes from a buffer
354 If called multiple times, then the data written will continue at the offset
355 of the firmware configuration item where the previous write ended.
357 @param[in] Size Size in bytes to write
358 @param[in] Buffer Buffer to read data from
363 QemuFwCfgWriteBytes (
368 if (QemuFwCfgIsAvailable ()) {
371 for (Idx
= 0; Idx
< Size
; ++Idx
) {
372 MmioWrite8 (mFwCfgDataAddress
, ((UINT8
*)Buffer
)[Idx
]);
379 Reads a UINT8 firmware configuration value
381 @return Value of Firmware Configuration item read
392 QemuFwCfgReadBytes (sizeof Result
, &Result
);
398 Reads a UINT16 firmware configuration value
400 @return Value of Firmware Configuration item read
411 QemuFwCfgReadBytes (sizeof Result
, &Result
);
417 Reads a UINT32 firmware configuration value
419 @return Value of Firmware Configuration item read
430 QemuFwCfgReadBytes (sizeof Result
, &Result
);
436 Reads a UINT64 firmware configuration value
438 @return Value of Firmware Configuration item read
449 QemuFwCfgReadBytes (sizeof Result
, &Result
);
455 Find the configuration item corresponding to the firmware configuration file.
457 @param[in] Name Name of file to look up.
458 @param[out] Item Configuration item corresponding to the file, to be passed
459 to QemuFwCfgSelectItem ().
460 @param[out] Size Number of bytes in the file.
462 @retval RETURN_SUCCESS If file is found.
463 @retval RETURN_NOT_FOUND If file is not found.
464 @retval RETURN_UNSUPPORTED If firmware configuration is unavailable.
470 IN CONST CHAR8
*Name
,
471 OUT FIRMWARE_CONFIG_ITEM
*Item
,
478 if (!QemuFwCfgIsAvailable ()) {
479 return RETURN_UNSUPPORTED
;
482 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
483 Count
= SwapBytes32 (QemuFwCfgRead32 ());
485 for (Idx
= 0; Idx
< Count
; ++Idx
) {
488 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
490 FileSize
= QemuFwCfgRead32 ();
491 FileSelect
= QemuFwCfgRead16 ();
492 QemuFwCfgRead16 (); // skip the field called "reserved"
493 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
495 if (AsciiStrCmp (Name
, FName
) == 0) {
496 *Item
= (FIRMWARE_CONFIG_ITEM
) SwapBytes16 (FileSelect
);
497 *Size
= SwapBytes32 (FileSize
);
498 return RETURN_SUCCESS
;
502 return RETURN_NOT_FOUND
;
507 Determine if S3 support is explicitly enabled.
509 @retval TRUE if S3 support is explicitly enabled.
510 FALSE otherwise. This includes unavailability of the firmware
511 configuration interface.