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>
7 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/IoLib.h>
18 #include <Library/QemuFwCfgLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
21 #include <Protocol/FdtClient.h>
23 STATIC UINTN mFwCfgSelectorAddress
;
24 STATIC UINTN mFwCfgDataAddress
;
25 STATIC UINTN mFwCfgDmaAddress
;
28 Reads firmware configuration bytes into a buffer
30 @param[in] Size Size in bytes to read
31 @param[in] Buffer Buffer to store data into (OPTIONAL if Size is 0)
35 VOID(EFIAPI READ_BYTES_FUNCTION
)(
37 IN VOID
*Buffer OPTIONAL
41 Writes bytes from a buffer to firmware configuration
43 @param[in] Size Size in bytes to write
44 @param[in] Buffer Buffer to transfer data from (OPTIONAL if Size is 0)
48 VOID(EFIAPI WRITE_BYTES_FUNCTION
)(
50 IN VOID
*Buffer OPTIONAL
54 Skips bytes in firmware configuration
56 @param[in] Size Size in bytes to skip
60 VOID(EFIAPI SKIP_BYTES_FUNCTION
)(
65 // Forward declaration of the two implementations we have.
67 STATIC READ_BYTES_FUNCTION MmioReadBytes
;
68 STATIC WRITE_BYTES_FUNCTION MmioWriteBytes
;
69 STATIC SKIP_BYTES_FUNCTION MmioSkipBytes
;
70 STATIC READ_BYTES_FUNCTION DmaReadBytes
;
71 STATIC WRITE_BYTES_FUNCTION DmaWriteBytes
;
72 STATIC SKIP_BYTES_FUNCTION DmaSkipBytes
;
75 // These correspond to the implementation we detect at runtime.
77 STATIC READ_BYTES_FUNCTION
*InternalQemuFwCfgReadBytes
= MmioReadBytes
;
78 STATIC WRITE_BYTES_FUNCTION
*InternalQemuFwCfgWriteBytes
= MmioWriteBytes
;
79 STATIC SKIP_BYTES_FUNCTION
*InternalQemuFwCfgSkipBytes
= MmioSkipBytes
;
82 Returns a boolean indicating if the firmware configuration interface
85 This function may change fw_cfg state.
87 @retval TRUE The interface is available
88 @retval FALSE The interface is not available
93 QemuFwCfgIsAvailable (
97 return (BOOLEAN
)(mFwCfgSelectorAddress
!= 0 && mFwCfgDataAddress
!= 0);
102 QemuFwCfgInitialize (
107 FDT_CLIENT_PROTOCOL
*FdtClient
;
110 UINTN AddressCells
, SizeCells
;
111 UINT64 FwCfgSelectorAddress
;
112 UINT64 FwCfgSelectorSize
;
113 UINT64 FwCfgDataAddress
;
114 UINT64 FwCfgDataSize
;
115 UINT64 FwCfgDmaAddress
;
118 Status
= gBS
->LocateProtocol (
119 &gFdtClientProtocolGuid
,
123 ASSERT_EFI_ERROR (Status
);
125 Status
= FdtClient
->FindCompatibleNodeReg (
133 if (EFI_ERROR (Status
)) {
136 "%a: No 'qemu,fw-cfg-mmio' compatible DT node found (Status == %r)\n",
143 ASSERT (AddressCells
== 2);
144 ASSERT (SizeCells
== 2);
145 ASSERT (RegSize
== 2 * sizeof (UINT64
));
147 FwCfgDataAddress
= SwapBytes64 (Reg
[0]);
149 FwCfgSelectorAddress
= FwCfgDataAddress
+ FwCfgDataSize
;
150 FwCfgSelectorSize
= 2;
153 // The following ASSERT()s express
155 // Address + Size - 1 <= MAX_UINTN
157 // for both registers, that is, that the last byte in each MMIO range is
158 // expressible as a MAX_UINTN. The form below is mathematically
159 // equivalent, and it also prevents any unsigned overflow before the
162 ASSERT (FwCfgSelectorAddress
<= MAX_UINTN
- FwCfgSelectorSize
+ 1);
163 ASSERT (FwCfgDataAddress
<= MAX_UINTN
- FwCfgDataSize
+ 1);
165 mFwCfgSelectorAddress
= FwCfgSelectorAddress
;
166 mFwCfgDataAddress
= FwCfgDataAddress
;
170 "Found FwCfg @ 0x%Lx/0x%Lx\n",
171 FwCfgSelectorAddress
,
175 if (SwapBytes64 (Reg
[1]) >= 0x18) {
176 FwCfgDmaAddress
= FwCfgDataAddress
+ 0x10;
180 // See explanation above.
182 ASSERT (FwCfgDmaAddress
<= MAX_UINTN
- FwCfgDmaSize
+ 1);
184 DEBUG ((DEBUG_INFO
, "Found FwCfg DMA @ 0x%Lx\n", FwCfgDmaAddress
));
189 if (QemuFwCfgIsAvailable ()) {
192 QemuFwCfgSelectItem (QemuFwCfgItemSignature
);
193 Signature
= QemuFwCfgRead32 ();
194 if (Signature
== SIGNATURE_32 ('Q', 'E', 'M', 'U')) {
196 // For DMA support, we require the DTB to advertise the register, and the
197 // feature bitmap (which we read without DMA) to confirm the feature.
199 if (FwCfgDmaAddress
!= 0) {
202 QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion
);
203 Features
= QemuFwCfgRead32 ();
204 if ((Features
& FW_CFG_F_DMA
) != 0) {
205 mFwCfgDmaAddress
= FwCfgDmaAddress
;
206 InternalQemuFwCfgReadBytes
= DmaReadBytes
;
207 InternalQemuFwCfgWriteBytes
= DmaWriteBytes
;
208 InternalQemuFwCfgSkipBytes
= DmaSkipBytes
;
212 mFwCfgSelectorAddress
= 0;
213 mFwCfgDataAddress
= 0;
217 return RETURN_SUCCESS
;
221 Selects a firmware configuration item for reading.
223 Following this call, any data read from this item will start from the
224 beginning of the configuration item's data.
226 @param[in] QemuFwCfgItem Firmware Configuration item to read
231 QemuFwCfgSelectItem (
232 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
235 if (QemuFwCfgIsAvailable ()) {
236 MmioWrite16 (mFwCfgSelectorAddress
, SwapBytes16 ((UINT16
)QemuFwCfgItem
));
241 Slow READ_BYTES_FUNCTION.
248 IN VOID
*Buffer OPTIONAL
255 #if defined (MDE_CPU_AARCH64) || defined (MDE_CPU_RISCV64)
265 #if defined (MDE_CPU_AARCH64) || defined (MDE_CPU_RISCV64)
267 *(UINT64
*)Ptr
= MmioRead64 (mFwCfgDataAddress
);
272 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
278 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
285 *(UINT16
*)Ptr
= MmioRead16 (mFwCfgDataAddress
);
290 *Ptr
= MmioRead8 (mFwCfgDataAddress
);
295 Transfer an array of bytes, or skip a number of bytes, using the DMA
298 @param[in] Size Size in bytes to transfer or skip.
300 @param[in,out] Buffer Buffer to read data into or write data from. Ignored,
301 and may be NULL, if Size is zero, or Control is
304 @param[in] Control One of the following:
305 FW_CFG_DMA_CTL_WRITE - write to fw_cfg from Buffer.
306 FW_CFG_DMA_CTL_READ - read from fw_cfg into Buffer.
307 FW_CFG_DMA_CTL_SKIP - skip bytes in fw_cfg.
313 IN OUT VOID
*Buffer OPTIONAL
,
317 volatile FW_CFG_DMA_ACCESS Access
;
321 Control
== FW_CFG_DMA_CTL_WRITE
|| Control
== FW_CFG_DMA_CTL_READ
||
322 Control
== FW_CFG_DMA_CTL_SKIP
329 ASSERT (Size
<= MAX_UINT32
);
331 Access
.Control
= SwapBytes32 (Control
);
332 Access
.Length
= SwapBytes32 ((UINT32
)Size
);
333 Access
.Address
= SwapBytes64 ((UINT64
)(UINTN
)Buffer
);
336 // We shouldn't start the transfer before setting up Access.
341 // This will fire off the transfer.
343 #if defined (MDE_CPU_AARCH64) || defined (MDE_CPU_RISCV64)
344 MmioWrite64 (mFwCfgDmaAddress
, SwapBytes64 ((UINT64
)&Access
));
346 MmioWrite32 ((UINT32
)(mFwCfgDmaAddress
+ 4), SwapBytes32 ((UINT32
)&Access
));
350 // We shouldn't look at Access.Control before starting the transfer.
355 Status
= SwapBytes32 (Access
.Control
);
356 ASSERT ((Status
& FW_CFG_DMA_CTL_ERROR
) == 0);
357 } while (Status
!= 0);
360 // The caller will want to access the transferred data.
366 Fast READ_BYTES_FUNCTION.
373 IN VOID
*Buffer OPTIONAL
376 DmaTransferBytes (Size
, Buffer
, FW_CFG_DMA_CTL_READ
);
380 Reads firmware configuration bytes into a buffer
382 If called multiple times, then the data read will continue at the offset of
383 the firmware configuration item where the previous read ended.
385 @param[in] Size Size in bytes to read
386 @param[in] Buffer Buffer to store data into
396 if (QemuFwCfgIsAvailable ()) {
397 InternalQemuFwCfgReadBytes (Size
, Buffer
);
399 ZeroMem (Buffer
, Size
);
404 Slow WRITE_BYTES_FUNCTION.
411 IN VOID
*Buffer OPTIONAL
416 for (Idx
= 0; Idx
< Size
; ++Idx
) {
417 MmioWrite8 (mFwCfgDataAddress
, ((UINT8
*)Buffer
)[Idx
]);
422 Fast WRITE_BYTES_FUNCTION.
429 IN VOID
*Buffer OPTIONAL
432 DmaTransferBytes (Size
, Buffer
, FW_CFG_DMA_CTL_WRITE
);
436 Write firmware configuration bytes from a buffer
438 If called multiple times, then the data written will continue at the offset
439 of the firmware configuration item where the previous write ended.
441 @param[in] Size Size in bytes to write
442 @param[in] Buffer Buffer to read data from
447 QemuFwCfgWriteBytes (
452 if (QemuFwCfgIsAvailable ()) {
453 InternalQemuFwCfgWriteBytes (Size
, Buffer
);
458 Slow SKIP_BYTES_FUNCTION.
468 UINT8 SkipBuffer
[256];
471 // Emulate the skip by reading data in chunks, and throwing it away. The
472 // implementation below doesn't affect the static data footprint for client
473 // modules. Large skips are not expected, therefore this fallback is not
474 // performance critical. The size of SkipBuffer is thought not to exert a
475 // large pressure on the stack.
478 ChunkSize
= MIN (Size
, sizeof SkipBuffer
);
479 MmioReadBytes (ChunkSize
, SkipBuffer
);
485 Fast SKIP_BYTES_FUNCTION.
494 DmaTransferBytes (Size
, NULL
, FW_CFG_DMA_CTL_SKIP
);
498 Skip bytes in the firmware configuration item.
500 Increase the offset of the firmware configuration item without transferring
501 bytes between the item and a caller-provided buffer. Subsequent read, write
502 or skip operations will commence at the increased offset.
504 @param[in] Size Number of bytes to skip.
512 if (QemuFwCfgIsAvailable ()) {
513 InternalQemuFwCfgSkipBytes (Size
);
518 Reads a UINT8 firmware configuration value
520 @return Value of Firmware Configuration item read
531 QemuFwCfgReadBytes (sizeof Result
, &Result
);
536 Reads a UINT16 firmware configuration value
538 @return Value of Firmware Configuration item read
549 QemuFwCfgReadBytes (sizeof Result
, &Result
);
554 Reads a UINT32 firmware configuration value
556 @return Value of Firmware Configuration item read
567 QemuFwCfgReadBytes (sizeof Result
, &Result
);
572 Reads a UINT64 firmware configuration value
574 @return Value of Firmware Configuration item read
585 QemuFwCfgReadBytes (sizeof Result
, &Result
);
590 Find the configuration item corresponding to the firmware configuration file.
592 @param[in] Name Name of file to look up.
593 @param[out] Item Configuration item corresponding to the file, to be passed
594 to QemuFwCfgSelectItem ().
595 @param[out] Size Number of bytes in the file.
597 @retval RETURN_SUCCESS If file is found.
598 @retval RETURN_NOT_FOUND If file is not found.
599 @retval RETURN_UNSUPPORTED If firmware configuration is unavailable.
605 IN CONST CHAR8
*Name
,
606 OUT FIRMWARE_CONFIG_ITEM
*Item
,
613 if (!QemuFwCfgIsAvailable ()) {
614 return RETURN_UNSUPPORTED
;
617 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
618 Count
= SwapBytes32 (QemuFwCfgRead32 ());
620 for (Idx
= 0; Idx
< Count
; ++Idx
) {
623 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
625 FileSize
= QemuFwCfgRead32 ();
626 FileSelect
= QemuFwCfgRead16 ();
627 QemuFwCfgRead16 (); // skip the field called "reserved"
628 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
630 if (AsciiStrCmp (Name
, FName
) == 0) {
631 *Item
= (FIRMWARE_CONFIG_ITEM
)SwapBytes16 (FileSelect
);
632 *Size
= SwapBytes32 (FileSize
);
633 return RETURN_SUCCESS
;
637 return RETURN_NOT_FOUND
;