Introduce three new DebugLib instances, forked from
MdePkg/Library/BaseDebugLibSerialPort. All three instances rely on
PL011UartLib rather than SerialPortLib so that they can customize the
PL011 UART that the debug messages are written to. All three instances
direct the debug output to the first such PL011 UART that *differs* from
the one specified in the Device Tree's /chosen node's "stdout-path"
property.

From these, DebugLibFdtPL011UartFlash mirrors EarlyFdtPL011SerialPortLib:
it parses the initial Device Tree, and initializes the UART -- a UART
different from EarlyFdtPL011SerialPortLib's -- for every message written.
Suitable for SEC, PEI_CORE, PEIM.

(Note that OVMF uses a similar set of dedicated DebugLib instances
(PlatformDebugLibIoPort) for logging to the (x86-only) isa-debugcon device
from various firmware phases.)

The contexts in which these DebugLib instances run are identical to those
in which the corresponding SerialPortLib instances run. The particular
original dependency chain is

  BaseDebugLibSerialPort (SEC, PEI_CORE, PEIM)
    EarlyFdtPL011SerialPortLib
      PcdDeviceTreeInitialBaseAddress
      FdtSerialPortAddressLib
      PL011UartLib

and the new dependency chain is

  DebugLibFdtPL011UartFlash (SEC, PEI_CORE, PEIM)
    PcdDeviceTreeInitialBaseAddress
    FdtSerialPortAddressLib
    PL011UartLib

Note that EarlyFdtPL011SerialPortLib remains in use (just not via
BaseDebugLibSerialPort), namely for direct SerialPortLib calls from SEC,
PEI_CORE, PEIM. See for example commit 56035d1c8b25
("ArmPlatformPkg/PrePeiCore: Print the firmware version early in boot",
2022-10-25).

The ArmVirtPkg DSC files will be switched to the new library instances in
a separate patch.

This patch is worth viewing with "git show --find-copies-harder".

Cc: Ard Biesheuvel <ardb+tianoc...@kernel.org>
Cc: Gerd Hoffmann <kra...@redhat.com>
Cc: Leif Lindholm <quic_llind...@quicinc.com>
Cc: Sami Mujawar <sami.muja...@arm.com>
Signed-off-by: Laszlo Ersek <ler...@redhat.com>
---
 ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLibFdtPL011UartFlash.inf          
               |  54 ++++++++++
 ArmVirtPkg/Library/DebugLibFdtPL011Uart/Write.h                                
               |  39 +++++++
 {MdePkg/Library/BaseDebugLibSerialPort => 
ArmVirtPkg/Library/DebugLibFdtPL011Uart}/DebugLib.c |  41 +++-----
 ArmVirtPkg/Library/DebugLibFdtPL011Uart/Flash.c                                
               | 107 ++++++++++++++++++++
 4 files changed, 216 insertions(+), 25 deletions(-)

diff --git 
a/ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLibFdtPL011UartFlash.inf 
b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLibFdtPL011UartFlash.inf
new file mode 100644
index 000000000000..7870ca2ae47f
--- /dev/null
+++ b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLibFdtPL011UartFlash.inf
@@ -0,0 +1,54 @@
+## @file
+# DebugLib instance that produces debug output directly via PL011UartLib.
+#
+# If there are at least two PL011 UARTs in the device tree, and the /chosen
+# node's "stdout-path" property references one PL011 UART, then both raw
+# SerialPortLib IO, and -- via SerialDxe -- UEFI console IO, will occur on that
+# UART; and this DebugLib instance will produce output on a *different* UART.
+#
+# This instance is suitable for modules that may run from flash or RAM.
+#
+# Copyright (C) Red Hat
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+  INF_VERSION    = 1.27
+  BASE_NAME      = DebugLibFdtPL011UartFlash
+  FILE_GUID      = 43A4C56B-D071-4CE0-A157-9D59E6161DEC
+  MODULE_TYPE    = BASE
+  VERSION_STRING = 1.0
+  LIBRARY_CLASS  = DebugLib|SEC PEI_CORE PEIM
+
+[Sources]
+  DebugLib.c
+  Flash.c
+  Write.h
+
+[Packages]
+  ArmPlatformPkg/ArmPlatformPkg.dec
+  ArmVirtPkg/ArmVirtPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  BaseLib
+  BaseMemoryLib
+  DebugPrintErrorLevelLib
+  FdtSerialPortAddressLib # Flash.c
+  PL011UartLib
+  PcdLib
+  PrintLib
+
+[Pcd]
+  gArmVirtTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress # Flash.c
+  gEfiMdePkgTokenSpaceGuid.PcdDebugClearMemoryValue
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask
+  gEfiMdePkgTokenSpaceGuid.PcdFixedDebugPrintErrorLevel
+
+[FixedPcd]
+  gArmPlatformTokenSpaceGuid.PL011UartClkInHz
+  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate
+  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultDataBits
+  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultParity
+  gEfiMdePkgTokenSpaceGuid.PcdUartDefaultStopBits
diff --git a/ArmVirtPkg/Library/DebugLibFdtPL011Uart/Write.h 
b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/Write.h
new file mode 100644
index 000000000000..2cf610676423
--- /dev/null
+++ b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/Write.h
@@ -0,0 +1,39 @@
+/** @file
+  Declare DebugLibFdtPL011UartWrite(), for abstracting PL011 UART 
initialization
+  differences between flash- vs. RAM-based modules.
+
+  Copyright (C) Red Hat
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2012 - 2014, ARM Ltd. All rights reserved.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#ifndef DEBUG_LIB_FDT_PL011_UART_WRITE_H_
+#define DEBUG_LIB_FDT_PL011_UART_WRITE_H_
+
+/**
+  (Copied from SerialPortWrite() in "MdePkg/Include/Library/SerialPortLib.h" at
+  commit c4547aefb3d0, with the Buffer non-nullity assertion removed:)
+
+  Write data from buffer to serial device.
+
+  Writes NumberOfBytes data bytes from Buffer to the serial device.
+  The number of bytes actually written to the serial device is returned.
+  If the return value is less than NumberOfBytes, then the write operation 
failed.
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           Pointer to the data buffer to be written.
+  @param  NumberOfBytes    Number of bytes to written to the serial device.
+
+  @retval 0                NumberOfBytes is 0.
+  @retval >0               The number of bytes written to the serial device.
+                           If this value is less than NumberOfBytes, then the 
write operation failed.
+**/
+UINTN
+DebugLibFdtPL011UartWrite (
+  IN UINT8  *Buffer,
+  IN UINTN  NumberOfBytes
+  );
+
+#endif
diff --git a/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c 
b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLib.c
similarity index 89%
copy from MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
copy to ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLib.c
index bd5686947712..0da84ba8d263 100644
--- a/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
+++ b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/DebugLib.c
@@ -1,15 +1,20 @@
 /** @file
-  Base Debug library instance base on Serial Port library.
-  It uses PrintLib to send debug messages to serial port device.
+  Originally copied from "MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c" at
+  commit f36e1ec1f0a5, and customized for:
 
-  NOTE: If the Serial Port library enables hardware flow control, then a call
-  to DebugPrint() or DebugAssert() may hang if writes to the serial port are
-  being blocked.  This may occur if a key(s) are pressed in a terminal emulator
-  used to monitor the DEBUG() and ASSERT() messages.
+  - RAM vs. flash dependent PL011 UART initialization,
 
+  - direct PL011 UART access, with the base address taken from the device tree
+    such that the debug output be separate from the SerialPortLib / UEFI 
console
+    traffic.
+
+  Both of these customizations are hidden behind DebugLibFdtPL011UartWrite(),
+  which replaces SerialPortWrite().
+
+  Copyright (C) Red Hat
   Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
+
   SPDX-License-Identifier: BSD-2-Clause-Patent
-
 **/
 
 #include <Base.h>
@@ -18,9 +23,10 @@
 #include <Library/PrintLib.h>
 #include <Library/PcdLib.h>
 #include <Library/BaseMemoryLib.h>
-#include <Library/SerialPortLib.h>
 #include <Library/DebugPrintErrorLevelLib.h>
 
+#include "Write.h"
+
 //
 // Define the maximum debug and assert message length that this library 
supports
 //
@@ -32,21 +38,6 @@
 //
 VA_LIST  mVaListNull;
 
-/**
-  The constructor function initialize the Serial Port Library
-
-  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
-
-**/
-RETURN_STATUS
-EFIAPI
-BaseDebugLibSerialPortConstructor (
-  VOID
-  )
-{
-  return SerialPortInitialize ();
-}
-
 /**
   Prints a debug message to the debug output device if the specified error 
level is enabled.
 
@@ -128,7 +119,7 @@ DebugPrintMarker (
   //
   // Send the print string to a Serial Port
   //
-  SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
+  DebugLibFdtPL011UartWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
 }
 
 /**
@@ -224,7 +215,7 @@ DebugAssert (
   //
   // Send the print string to the Console Output device
   //
-  SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
+  DebugLibFdtPL011UartWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
 
   //
   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
diff --git a/ArmVirtPkg/Library/DebugLibFdtPL011Uart/Flash.c 
b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/Flash.c
new file mode 100644
index 000000000000..a624e0860d10
--- /dev/null
+++ b/ArmVirtPkg/Library/DebugLibFdtPL011Uart/Flash.c
@@ -0,0 +1,107 @@
+/** @file
+  Define DebugLibFdtPL011UartWrite() for modules that may run from flash or 
RAM.
+
+  Copyright (C) Red Hat
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#include <Library/FdtSerialPortAddressLib.h>
+#include <Library/PL011UartLib.h>
+#include <Library/PcdLib.h>
+
+#include "Write.h"
+
+/**
+  (Copied from SerialPortWrite() in "MdePkg/Include/Library/SerialPortLib.h" at
+  commit c4547aefb3d0, with the Buffer non-nullity assertion removed:)
+
+  Write data from buffer to serial device.
+
+  Writes NumberOfBytes data bytes from Buffer to the serial device.
+  The number of bytes actually written to the serial device is returned.
+  If the return value is less than NumberOfBytes, then the write operation 
failed.
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           Pointer to the data buffer to be written.
+  @param  NumberOfBytes    Number of bytes to written to the serial device.
+
+  @retval 0                NumberOfBytes is 0.
+  @retval >0               The number of bytes written to the serial device.
+                           If this value is less than NumberOfBytes, then the 
write operation failed.
+**/
+UINTN
+DebugLibFdtPL011UartWrite (
+  IN UINT8  *Buffer,
+  IN UINTN  NumberOfBytes
+  )
+{
+  CONST VOID          *DeviceTree;
+  RETURN_STATUS       Status;
+  FDT_SERIAL_PORTS    Ports;
+  UINT64              DebugAddress;
+  UINT64              BaudRate;
+  UINT32              ReceiveFifoDepth;
+  EFI_PARITY_TYPE     Parity;
+  UINT8               DataBits;
+  EFI_STOP_BITS_TYPE  StopBits;
+
+  DeviceTree = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);
+  if (DeviceTree == NULL) {
+    return 0;
+  }
+
+  Status = FdtSerialGetPorts (DeviceTree, "arm,pl011", &Ports);
+  if (RETURN_ERROR (Status)) {
+    return 0;
+  }
+
+  if (Ports.NumberOfPorts == 1) {
+    //
+    // Just one UART; direct DebugLib to it.
+    //
+    DebugAddress = Ports.BaseAddress[0];
+  } else {
+    UINT64  ConsoleAddress;
+
+    Status = FdtSerialGetConsolePort (DeviceTree, &ConsoleAddress);
+    if (EFI_ERROR (Status)) {
+      //
+      // At least two UARTs; but failed to get the console preference. Use the
+      // second UART for DebugLib.
+      //
+      DebugAddress = Ports.BaseAddress[1];
+    } else {
+      //
+      // At least two UARTs; and console preference available. Use the first
+      // such UART for DebugLib that *differs* from ConsoleAddress.
+      //
+      if (ConsoleAddress == Ports.BaseAddress[0]) {
+        DebugAddress = Ports.BaseAddress[1];
+      } else {
+        DebugAddress = Ports.BaseAddress[0];
+      }
+    }
+  }
+
+  BaudRate         = (UINTN)FixedPcdGet64 (PcdUartDefaultBaudRate);
+  ReceiveFifoDepth = 0; // Use the default value for Fifo depth
+  Parity           = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);
+  DataBits         = FixedPcdGet8 (PcdUartDefaultDataBits);
+  StopBits         = (EFI_STOP_BITS_TYPE)FixedPcdGet8 (PcdUartDefaultStopBits);
+
+  Status = PL011UartInitializePort (
+             (UINTN)DebugAddress,
+             FixedPcdGet32 (PL011UartClkInHz),
+             &BaudRate,
+             &ReceiveFifoDepth,
+             &Parity,
+             &DataBits,
+             &StopBits
+             );
+  if (RETURN_ERROR (Status)) {
+    return 0;
+  }
+
+  return PL011UartWrite ((UINTN)DebugAddress, Buffer, NumberOfBytes);
+}



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109395): https://edk2.groups.io/g/devel/message/109395
Mute This Topic: https://groups.io/mt/101834881/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: 
https://edk2.groups.io/g/devel/leave/9847357/21656/1706620634/xyzzy 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to