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 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/IoLib.h>
17 #include <Library/QemuFwCfgLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
20 #include <Protocol/FdtClient.h>
22 STATIC UINTN mFwCfgSelectorAddress
;
23 STATIC UINTN mFwCfgDataAddress
;
24 STATIC UINTN mFwCfgDmaAddress
;
27 Reads firmware configuration bytes into a buffer
29 @param[in] Size Size in bytes to read
30 @param[in] Buffer Buffer to store data into (OPTIONAL if Size is 0)
34 VOID (EFIAPI READ_BYTES_FUNCTION
) (
36 IN VOID
*Buffer OPTIONAL
40 Writes bytes from a buffer to firmware configuration
42 @param[in] Size Size in bytes to write
43 @param[in] Buffer Buffer to transfer data from (OPTIONAL if Size is 0)
47 VOID (EFIAPI WRITE_BYTES_FUNCTION
) (
49 IN VOID
*Buffer OPTIONAL
53 Skips bytes in firmware configuration
55 @param[in] Size Size in bytes to skip
59 VOID (EFIAPI SKIP_BYTES_FUNCTION
) (
64 // Forward declaration of the two implementations we have.
66 STATIC READ_BYTES_FUNCTION MmioReadBytes
;
67 STATIC WRITE_BYTES_FUNCTION MmioWriteBytes
;
68 STATIC SKIP_BYTES_FUNCTION MmioSkipBytes
;
69 STATIC READ_BYTES_FUNCTION DmaReadBytes
;
70 STATIC WRITE_BYTES_FUNCTION DmaWriteBytes
;
71 STATIC SKIP_BYTES_FUNCTION DmaSkipBytes
;
74 // These correspond to the implementation we detect at runtime.
76 STATIC READ_BYTES_FUNCTION
*InternalQemuFwCfgReadBytes
= MmioReadBytes
;
77 STATIC WRITE_BYTES_FUNCTION
*InternalQemuFwCfgWriteBytes
= MmioWriteBytes
;
78 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);
103 QemuFwCfgInitialize (
108 FDT_CLIENT_PROTOCOL
*FdtClient
;
111 UINTN AddressCells
, SizeCells
;
112 UINT64 FwCfgSelectorAddress
;
113 UINT64 FwCfgSelectorSize
;
114 UINT64 FwCfgDataAddress
;
115 UINT64 FwCfgDataSize
;
116 UINT64 FwCfgDmaAddress
;
119 Status
= gBS
->LocateProtocol (&gFdtClientProtocolGuid
, NULL
,
120 (VOID
**)&FdtClient
);
121 ASSERT_EFI_ERROR (Status
);
123 Status
= FdtClient
->FindCompatibleNodeReg (FdtClient
, "qemu,fw-cfg-mmio",
124 (CONST VOID
**)&Reg
, &AddressCells
, &SizeCells
,
126 if (EFI_ERROR (Status
)) {
128 "%a: No 'qemu,fw-cfg-mmio' compatible DT node found (Status == %r)\n",
129 __FUNCTION__
, Status
));
133 ASSERT (AddressCells
== 2);
134 ASSERT (SizeCells
== 2);
135 ASSERT (RegSize
== 2 * sizeof (UINT64
));
137 FwCfgDataAddress
= SwapBytes64 (Reg
[0]);
139 FwCfgSelectorAddress
= FwCfgDataAddress
+ FwCfgDataSize
;
140 FwCfgSelectorSize
= 2;
143 // The following ASSERT()s express
145 // Address + Size - 1 <= MAX_UINTN
147 // for both registers, that is, that the last byte in each MMIO range is
148 // expressible as a MAX_UINTN. The form below is mathematically
149 // equivalent, and it also prevents any unsigned overflow before the
152 ASSERT (FwCfgSelectorAddress
<= MAX_UINTN
- FwCfgSelectorSize
+ 1);
153 ASSERT (FwCfgDataAddress
<= MAX_UINTN
- FwCfgDataSize
+ 1);
155 mFwCfgSelectorAddress
= FwCfgSelectorAddress
;
156 mFwCfgDataAddress
= FwCfgDataAddress
;
158 DEBUG ((EFI_D_INFO
, "Found FwCfg @ 0x%Lx/0x%Lx\n", FwCfgSelectorAddress
,
161 if (SwapBytes64 (Reg
[1]) >= 0x18) {
162 FwCfgDmaAddress
= FwCfgDataAddress
+ 0x10;
166 // See explanation above.
168 ASSERT (FwCfgDmaAddress
<= MAX_UINTN
- FwCfgDmaSize
+ 1);
170 DEBUG ((EFI_D_INFO
, "Found FwCfg DMA @ 0x%Lx\n", FwCfgDmaAddress
));
175 if (QemuFwCfgIsAvailable ()) {
178 QemuFwCfgSelectItem (QemuFwCfgItemSignature
);
179 Signature
= QemuFwCfgRead32 ();
180 if (Signature
== SIGNATURE_32 ('Q', 'E', 'M', 'U')) {
182 // For DMA support, we require the DTB to advertise the register, and the
183 // feature bitmap (which we read without DMA) to confirm the feature.
185 if (FwCfgDmaAddress
!= 0) {
188 QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion
);
189 Features
= QemuFwCfgRead32 ();
190 if ((Features
& FW_CFG_F_DMA
) != 0) {
191 mFwCfgDmaAddress
= FwCfgDmaAddress
;
192 InternalQemuFwCfgReadBytes
= DmaReadBytes
;
193 InternalQemuFwCfgWriteBytes
= DmaWriteBytes
;
194 InternalQemuFwCfgSkipBytes
= DmaSkipBytes
;
198 mFwCfgSelectorAddress
= 0;
199 mFwCfgDataAddress
= 0;
202 return RETURN_SUCCESS
;
207 Selects a firmware configuration item for reading.
209 Following this call, any data read from this item will start from the
210 beginning of the configuration item's data.
212 @param[in] QemuFwCfgItem Firmware Configuration item to read
217 QemuFwCfgSelectItem (
218 IN FIRMWARE_CONFIG_ITEM QemuFwCfgItem
221 if (QemuFwCfgIsAvailable ()) {
222 MmioWrite16 (mFwCfgSelectorAddress
, SwapBytes16 ((UINT16
)QemuFwCfgItem
));
228 Slow READ_BYTES_FUNCTION.
235 IN VOID
*Buffer OPTIONAL
242 #ifdef MDE_CPU_AARCH64
252 #ifdef MDE_CPU_AARCH64
254 *(UINT64
*)Ptr
= MmioRead64 (mFwCfgDataAddress
);
258 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
263 *(UINT32
*)Ptr
= MmioRead32 (mFwCfgDataAddress
);
269 *(UINT16
*)Ptr
= MmioRead16 (mFwCfgDataAddress
);
273 *Ptr
= MmioRead8 (mFwCfgDataAddress
);
279 Transfer an array of bytes, or skip a number of bytes, using the DMA
282 @param[in] Size Size in bytes to transfer or skip.
284 @param[in,out] Buffer Buffer to read data into or write data from. Ignored,
285 and may be NULL, if Size is zero, or Control is
288 @param[in] Control One of the following:
289 FW_CFG_DMA_CTL_WRITE - write to fw_cfg from Buffer.
290 FW_CFG_DMA_CTL_READ - read from fw_cfg into Buffer.
291 FW_CFG_DMA_CTL_SKIP - skip bytes in fw_cfg.
297 IN OUT VOID
*Buffer OPTIONAL
,
301 volatile FW_CFG_DMA_ACCESS Access
;
304 ASSERT (Control
== FW_CFG_DMA_CTL_WRITE
|| Control
== FW_CFG_DMA_CTL_READ
||
305 Control
== FW_CFG_DMA_CTL_SKIP
);
311 ASSERT (Size
<= MAX_UINT32
);
313 Access
.Control
= SwapBytes32 (Control
);
314 Access
.Length
= SwapBytes32 ((UINT32
)Size
);
315 Access
.Address
= SwapBytes64 ((UINT64
)(UINTN
)Buffer
);
318 // We shouldn't start the transfer before setting up Access.
323 // This will fire off the transfer.
325 #ifdef MDE_CPU_AARCH64
326 MmioWrite64 (mFwCfgDmaAddress
, SwapBytes64 ((UINT64
)&Access
));
328 MmioWrite32 ((UINT32
)(mFwCfgDmaAddress
+ 4), SwapBytes32 ((UINT32
)&Access
));
332 // We shouldn't look at Access.Control before starting the transfer.
337 Status
= SwapBytes32 (Access
.Control
);
338 ASSERT ((Status
& FW_CFG_DMA_CTL_ERROR
) == 0);
339 } while (Status
!= 0);
342 // The caller will want to access the transferred data.
349 Fast READ_BYTES_FUNCTION.
356 IN VOID
*Buffer OPTIONAL
359 DmaTransferBytes (Size
, Buffer
, FW_CFG_DMA_CTL_READ
);
364 Reads firmware configuration bytes into a buffer
366 If called multiple times, then the data read will continue at the offset of
367 the firmware configuration item where the previous read ended.
369 @param[in] Size Size in bytes to read
370 @param[in] Buffer Buffer to store data into
380 if (QemuFwCfgIsAvailable ()) {
381 InternalQemuFwCfgReadBytes (Size
, Buffer
);
383 ZeroMem (Buffer
, Size
);
389 Slow WRITE_BYTES_FUNCTION.
396 IN VOID
*Buffer OPTIONAL
401 for (Idx
= 0; Idx
< Size
; ++Idx
) {
402 MmioWrite8 (mFwCfgDataAddress
, ((UINT8
*)Buffer
)[Idx
]);
408 Fast WRITE_BYTES_FUNCTION.
415 IN VOID
*Buffer OPTIONAL
418 DmaTransferBytes (Size
, Buffer
, FW_CFG_DMA_CTL_WRITE
);
423 Write firmware configuration bytes from a buffer
425 If called multiple times, then the data written will continue at the offset
426 of the firmware configuration item where the previous write ended.
428 @param[in] Size Size in bytes to write
429 @param[in] Buffer Buffer to read data from
434 QemuFwCfgWriteBytes (
439 if (QemuFwCfgIsAvailable ()) {
440 InternalQemuFwCfgWriteBytes (Size
, Buffer
);
446 Slow SKIP_BYTES_FUNCTION.
456 UINT8 SkipBuffer
[256];
459 // Emulate the skip by reading data in chunks, and throwing it away. The
460 // implementation below doesn't affect the static data footprint for client
461 // modules. Large skips are not expected, therefore this fallback is not
462 // performance critical. The size of SkipBuffer is thought not to exert a
463 // large pressure on the stack.
466 ChunkSize
= MIN (Size
, sizeof SkipBuffer
);
467 MmioReadBytes (ChunkSize
, SkipBuffer
);
474 Fast SKIP_BYTES_FUNCTION.
483 DmaTransferBytes (Size
, NULL
, FW_CFG_DMA_CTL_SKIP
);
488 Skip bytes in the firmware configuration item.
490 Increase the offset of the firmware configuration item without transferring
491 bytes between the item and a caller-provided buffer. Subsequent read, write
492 or skip operations will commence at the increased offset.
494 @param[in] Size Number of bytes to skip.
502 if (QemuFwCfgIsAvailable ()) {
503 InternalQemuFwCfgSkipBytes (Size
);
509 Reads a UINT8 firmware configuration value
511 @return Value of Firmware Configuration item read
522 QemuFwCfgReadBytes (sizeof Result
, &Result
);
528 Reads a UINT16 firmware configuration value
530 @return Value of Firmware Configuration item read
541 QemuFwCfgReadBytes (sizeof Result
, &Result
);
547 Reads a UINT32 firmware configuration value
549 @return Value of Firmware Configuration item read
560 QemuFwCfgReadBytes (sizeof Result
, &Result
);
566 Reads a UINT64 firmware configuration value
568 @return Value of Firmware Configuration item read
579 QemuFwCfgReadBytes (sizeof Result
, &Result
);
585 Find the configuration item corresponding to the firmware configuration file.
587 @param[in] Name Name of file to look up.
588 @param[out] Item Configuration item corresponding to the file, to be passed
589 to QemuFwCfgSelectItem ().
590 @param[out] Size Number of bytes in the file.
592 @retval RETURN_SUCCESS If file is found.
593 @retval RETURN_NOT_FOUND If file is not found.
594 @retval RETURN_UNSUPPORTED If firmware configuration is unavailable.
600 IN CONST CHAR8
*Name
,
601 OUT FIRMWARE_CONFIG_ITEM
*Item
,
608 if (!QemuFwCfgIsAvailable ()) {
609 return RETURN_UNSUPPORTED
;
612 QemuFwCfgSelectItem (QemuFwCfgItemFileDir
);
613 Count
= SwapBytes32 (QemuFwCfgRead32 ());
615 for (Idx
= 0; Idx
< Count
; ++Idx
) {
618 CHAR8 FName
[QEMU_FW_CFG_FNAME_SIZE
];
620 FileSize
= QemuFwCfgRead32 ();
621 FileSelect
= QemuFwCfgRead16 ();
622 QemuFwCfgRead16 (); // skip the field called "reserved"
623 InternalQemuFwCfgReadBytes (sizeof (FName
), FName
);
625 if (AsciiStrCmp (Name
, FName
) == 0) {
626 *Item
= (FIRMWARE_CONFIG_ITEM
) SwapBytes16 (FileSelect
);
627 *Size
= SwapBytes32 (FileSize
);
628 return RETURN_SUCCESS
;
632 return RETURN_NOT_FOUND
;