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 is
79 available for library-internal purposes.
81 This function never changes fw_cfg state.
83 @retval TRUE The interface is available internally.
84 @retval FALSE The interface is not available internally.
88 InternalQemuFwCfgIsAvailable (
92 return (BOOLEAN
)(mFwCfgSelectorAddress
!= 0 && mFwCfgDataAddress
!= 0);
97 Returns a boolean indicating if the firmware configuration interface
100 This function may change fw_cfg state.
102 @retval TRUE The interface is available
103 @retval FALSE The interface is not available
108 QemuFwCfgIsAvailable (
112 return InternalQemuFwCfgIsAvailable ();
118 QemuFwCfgInitialize (
123 FDT_CLIENT_PROTOCOL
*FdtClient
;
126 UINTN AddressCells
, SizeCells
;
127 UINT64 FwCfgSelectorAddress
;
128 UINT64 FwCfgSelectorSize
;
129 UINT64 FwCfgDataAddress
;
130 UINT64 FwCfgDataSize
;
131 UINT64 FwCfgDmaAddress
;
134 Status
= gBS
->LocateProtocol (&gFdtClientProtocolGuid
, NULL
,
135 (VOID
**)&FdtClient
);
136 ASSERT_EFI_ERROR (Status
);
138 Status
= FdtClient
->FindCompatibleNodeReg (FdtClient
, "qemu,fw-cfg-mmio",
139 (CONST VOID
**)&Reg
, &AddressCells
, &SizeCells
,
141 if (EFI_ERROR (Status
)) {
143 "%a: No 'qemu,fw-cfg-mmio' compatible DT node found (Status == %r)\n",
144 __FUNCTION__
, Status
));
148 ASSERT (AddressCells
== 2);
149 ASSERT (SizeCells
== 2);
150 ASSERT (RegSize
== 2 * sizeof (UINT64
));
152 FwCfgDataAddress
= SwapBytes64 (Reg
[0]);
154 FwCfgSelectorAddress
= FwCfgDataAddress
+ FwCfgDataSize
;
155 FwCfgSelectorSize
= 2;
158 // The following ASSERT()s express
160 // Address + Size - 1 <= MAX_UINTN
162 // for both registers, that is, that the last byte in each MMIO range is
163 // expressible as a MAX_UINTN. The form below is mathematically
164 // equivalent, and it also prevents any unsigned overflow before the
167 ASSERT (FwCfgSelectorAddress
<= MAX_UINTN
- FwCfgSelectorSize
+ 1);
168 ASSERT (FwCfgDataAddress
<= MAX_UINTN
- FwCfgDataSize
+ 1);
170 mFwCfgSelectorAddress
= FwCfgSelectorAddress
;
171 mFwCfgDataAddress
= FwCfgDataAddress
;
173 DEBUG ((EFI_D_INFO
, "Found FwCfg @ 0x%Lx/0x%Lx\n", FwCfgSelectorAddress
,
176 if (SwapBytes64 (Reg
[1]) >= 0x18) {
177 FwCfgDmaAddress
= FwCfgDataAddress
+ 0x10;
181 // See explanation above.
183 ASSERT (FwCfgDmaAddress
<= MAX_UINTN
- FwCfgDmaSize
+ 1);
185 DEBUG ((EFI_D_INFO
, "Found FwCfg DMA @ 0x%Lx\n", FwCfgDmaAddress
));
190 if (InternalQemuFwCfgIsAvailable ()) {
193 QemuFwCfgSelectItem (QemuFwCfgItemSignature
);
194 Signature
= QemuFwCfgRead32 ();
195 if (Signature
== SIGNATURE_32 ('Q', 'E', 'M', 'U')) {
197 // For DMA support, we require the DTB to advertise the register, and the
198 // feature bitmap (which we read without DMA) to confirm the feature.
200 if (FwCfgDmaAddress
!= 0) {
203 QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion
);
204 Features
= QemuFwCfgRead32 ();
205 if ((Features
& BIT1
) != 0) {
206 mFwCfgDmaAddress
= FwCfgDmaAddress
;
207 InternalQemuFwCfgReadBytes
= DmaReadBytes
;
211 mFwCfgSelectorAddress
= 0;
212 mFwCfgDataAddress
= 0;
215 return RETURN_SUCCESS
;
220 Selects a firmware configuration item for reading.
222 Following this call, any data read from this item will start from the
223 beginning of the configuration item's data.
225 @param[in] QemuFwCfgItem Firmware Configuration item to read
230 QemuFwCfgSelectItem (
231 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
234 if (InternalQemuFwCfgIsAvailable ()) {
235 MmioWrite16 (mFwCfgSelectorAddress
, SwapBytes16 ((UINT16
)QemuFwCfgItem
));
241 Slow READ_BYTES_FUNCTION.
248 IN VOID
*Buffer OPTIONAL
255 #ifdef MDE_CPU_AARCH64
265 #ifdef MDE_CPU_AARCH64
267 *(UINT64
*)Ptr
= MmioRead64 (mFwCfgDataAddress
);
271 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
276 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
282 *(UINT16
*)Ptr
= MmioRead16 (mFwCfgDataAddress
);
286 *Ptr
= MmioRead8 (mFwCfgDataAddress
);
292 Fast READ_BYTES_FUNCTION.
299 IN VOID
*Buffer OPTIONAL
302 volatile FW_CFG_DMA_ACCESS Access
;
309 ASSERT (Size
<= MAX_UINT32
);
311 Access
.Control
= SwapBytes32 (FW_CFG_DMA_CTL_READ
);
312 Access
.Length
= SwapBytes32 ((UINT32
)Size
);
313 Access
.Address
= SwapBytes64 ((UINT64
)(UINTN
)Buffer
);
316 // We shouldn't start the transfer before setting up Access.
321 // This will fire off the transfer.
323 #ifdef MDE_CPU_AARCH64
324 MmioWrite64 (mFwCfgDmaAddress
, SwapBytes64 ((UINT64
)&Access
));
326 MmioWrite32 ((UINT32
)(mFwCfgDmaAddress
+ 4), SwapBytes32 ((UINT32
)&Access
));
330 // We shouldn't look at Access.Control before starting the transfer.
335 Status
= SwapBytes32 (Access
.Control
);
336 ASSERT ((Status
& FW_CFG_DMA_CTL_ERROR
) == 0);
337 } while (Status
!= 0);
340 // The caller will want to access the transferred data.
347 Reads firmware configuration bytes into a buffer
349 If called multiple times, then the data read will continue at the offset of
350 the firmware configuration item where the previous read ended.
352 @param[in] Size Size in bytes to read
353 @param[in] Buffer Buffer to store data into
363 if (InternalQemuFwCfgIsAvailable ()) {
364 InternalQemuFwCfgReadBytes (Size
, Buffer
);
366 ZeroMem (Buffer
, Size
);
371 Write firmware configuration bytes from a buffer
373 If called multiple times, then the data written will continue at the offset
374 of the firmware configuration item where the previous write ended.
376 @param[in] Size Size in bytes to write
377 @param[in] Buffer Buffer to read data from
382 QemuFwCfgWriteBytes (
387 if (InternalQemuFwCfgIsAvailable ()) {
390 for (Idx
= 0; Idx
< Size
; ++Idx
) {
391 MmioWrite8 (mFwCfgDataAddress
, ((UINT8
*)Buffer
)[Idx
]);
398 Reads a UINT8 firmware configuration value
400 @return Value of Firmware Configuration item read
411 QemuFwCfgReadBytes (sizeof Result
, &Result
);
417 Reads a UINT16 firmware configuration value
419 @return Value of Firmware Configuration item read
430 QemuFwCfgReadBytes (sizeof Result
, &Result
);
436 Reads a UINT32 firmware configuration value
438 @return Value of Firmware Configuration item read
449 QemuFwCfgReadBytes (sizeof Result
, &Result
);
455 Reads a UINT64 firmware configuration value
457 @return Value of Firmware Configuration item read
468 QemuFwCfgReadBytes (sizeof Result
, &Result
);
474 Find the configuration item corresponding to the firmware configuration file.
476 @param[in] Name Name of file to look up.
477 @param[out] Item Configuration item corresponding to the file, to be passed
478 to QemuFwCfgSelectItem ().
479 @param[out] Size Number of bytes in the file.
481 @retval RETURN_SUCCESS If file is found.
482 @retval RETURN_NOT_FOUND If file is not found.
483 @retval RETURN_UNSUPPORTED If firmware configuration is unavailable.
489 IN CONST CHAR8
*Name
,
490 OUT FIRMWARE_CONFIG_ITEM
*Item
,
497 if (!InternalQemuFwCfgIsAvailable ()) {
498 return RETURN_UNSUPPORTED
;
501 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
502 Count
= SwapBytes32 (QemuFwCfgRead32 ());
504 for (Idx
= 0; Idx
< Count
; ++Idx
) {
507 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
509 FileSize
= QemuFwCfgRead32 ();
510 FileSelect
= QemuFwCfgRead16 ();
511 QemuFwCfgRead16 (); // skip the field called "reserved"
512 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
514 if (AsciiStrCmp (Name
, FName
) == 0) {
515 *Item
= (FIRMWARE_CONFIG_ITEM
) SwapBytes16 (FileSelect
);
516 *Size
= SwapBytes32 (FileSize
);
517 return RETURN_SUCCESS
;
521 return RETURN_NOT_FOUND
;
526 Determine if S3 support is explicitly enabled.
528 @retval TRUE if S3 support is explicitly enabled.
529 FALSE otherwise. This includes unavailability of the firmware
530 configuration interface.