From: "Nhi Pham" <nhi@os.amperecomputing.com>
To: devel@edk2.groups.io
Cc: Nhi Pham <nhi@os.amperecomputing.com>
Subject: [edk2-platforms][PATCH 07/34] Platform/Ampere: Add AcpiHelperLib to update ACPI DSDT table
Date: Wed, 9 Dec 2020 16:25:04 +0700 [thread overview]
Message-ID: <20201209092531.30867-8-nhi@os.amperecomputing.com> (raw)
In-Reply-To: <20201209092531.30867-1-nhi@os.amperecomputing.com>
The AcpiHelperLib provides functions to update the ACPI DSDT table after
the table is installed.
Signed-off-by: Nhi Pham <nhi@os.amperecomputing.com>
---
Silicon/Ampere/AmperePkg.dec | 2 +
Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.inf | 33 +++
Silicon/Ampere/Include/Library/AcpiHelperLib.h | 109 +++++++++
Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.c | 247 ++++++++++++++++++++
4 files changed, 391 insertions(+)
diff --git a/Silicon/Ampere/AmperePkg.dec b/Silicon/Ampere/AmperePkg.dec
index fa5b7074f749..3d6ddfb39b97 100755
--- a/Silicon/Ampere/AmperePkg.dec
+++ b/Silicon/Ampere/AmperePkg.dec
@@ -28,6 +28,8 @@ [LibraryClasses]
## @libraryclass Provides functions to create the ACPI PCCT Table which which advertises PCC mailbox channel information.
AcpiPccLib|Silicon/Ampere/Include/Library/AcpiPccLib.h
+ ## @libraryclass Provides helper functions to update ACPI DSDT Table.
+ AcpiHelperLib|Silicon/Ampere/Include/Library/AcpiHelperLib.h
[Guids]
gAmpereTokenSpaceGuid = { 0xdbd4436e, 0x89cb, 0x44dc, { 0xb5, 0xc0, 0x49, 0xc3, 0x91, 0x35, 0xbf, 0xdf } }
diff --git a/Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.inf b/Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.inf
new file mode 100755
index 000000000000..acf14623d87c
--- /dev/null
+++ b/Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.inf
@@ -0,0 +1,33 @@
+## @file
+#
+# Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION = 0x0001001B
+ BASE_NAME = AcpiHelperLib
+ FILE_GUID = E4F89216-E722-11E6-BF01-FE55135034F3
+ MODULE_TYPE = BASE
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = AcpiHelperLib
+
+[Sources]
+ AcpiHelperLib.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+ MdeModulePkg/MdeModulePkg.dec
+ Silicon/Ampere/AmperePkg.dec
+
+[LibraryClasses]
+ BaseLib
+ BaseMemoryLib
+ UefiBootServicesTableLib
+ UefiLib
+ DebugLib
+
+[Protocols]
+ gEfiAcpiSdtProtocolGuid ## COMSUMED
diff --git a/Silicon/Ampere/Include/Library/AcpiHelperLib.h b/Silicon/Ampere/Include/Library/AcpiHelperLib.h
new file mode 100644
index 000000000000..9ab13c4b0714
--- /dev/null
+++ b/Silicon/Ampere/Include/Library/AcpiHelperLib.h
@@ -0,0 +1,109 @@
+/** @file
+
+ Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _ACPIHELPERLIB_H_
+#define _ACPIHELPERLIB_H_
+
+#include <Uefi.h>
+#include <Uefi/UefiSpec.h>
+#include <Protocol/AcpiSystemDescriptionTable.h>
+
+#define MAX_ACPI_NODE_PATH 256
+
+
+typedef struct {
+ EFI_ACPI_SDT_HEADER *Table;
+ EFI_ACPI_TABLE_VERSION TableVersion;
+ UINTN TableKey;
+} ACPI_TABLE_DESCRIPTOR;
+
+/**
+ This function calculates and updates an UINT8 checksum.
+
+ @param[in] Buffer Pointer to buffer to checksum
+ @param[in] Size Number of bytes to checksum
+
+**/
+VOID
+EFIAPI
+AcpiTableChecksum (
+ IN UINT8 *Buffer,
+ IN UINTN Size
+ );
+
+/**
+ This function calculates and updates the ACPI DSDT checksum.
+
+ @param[in] AcpiTableProtocol Pointer to ACPI table protocol
+
+**/
+VOID
+EFIAPI
+AcpiDSDTUpdateChecksum (
+ IN EFI_ACPI_SDT_PROTOCOL *AcpiTableProtocol
+ );
+
+/**
+ This function update the _STA value of a ACPI DSDT node.
+
+ @param[in] AsciiNodePath Pointer to the path of the node.
+ @param[in] NodeStatus The status value needed to be updated.
+
+**/
+EFI_STATUS
+EFIAPI
+AcpiDSDTSetNodeStatusValue (
+ IN CHAR8 *AsciiNodePath,
+ IN CHAR8 NodeStatus
+ );
+
+/**
+ This function return the handle of the ACPI DSDT table.
+
+ @param[in] AcpiTableProtocol Pointer to ACPI table protocol.
+ @param[out] TableHandle Pointer to table handle.
+
+**/
+EFI_STATUS
+EFIAPI
+AcpiOpenDSDT (
+ IN EFI_ACPI_SDT_PROTOCOL *AcpiTableProtocol,
+ OUT EFI_ACPI_HANDLE *TableHandle
+ );
+
+/**
+ This function return the ACPI table matching a signature.
+
+ @param[in] TableDescriptor Pointer to ACPI table descriptor.
+ @param[in] TableSignature ACPI table signature.
+
+**/
+EFI_STATUS
+EFIAPI
+AcpiGetTable (
+ IN EFI_ACPI_SDT_PROTOCOL *AcpiTableSdtProtocol,
+ IN UINT32 TableSignature,
+ OUT ACPI_TABLE_DESCRIPTOR *TableDescriptor
+ );
+
+/**
+ Check whether the ACPI table is installed or not.
+
+ @param[in] AcpiTableSignature ACPI table signature.
+
+ @retval TRUE Already installed.
+ @retval FALSE Not installed.
+
+**/
+BOOLEAN
+EFIAPI
+IsAcpiInstalled (
+ IN UINT32 AcpiTableSignature
+ );
+
+#endif /* _ACPIHELPERLIB_H_ */
diff --git a/Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.c b/Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.c
new file mode 100755
index 000000000000..9baf44272a30
--- /dev/null
+++ b/Platform/Ampere/Library/AcpiHelperLib/AcpiHelperLib.c
@@ -0,0 +1,247 @@
+/** @file
+
+ Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiSpec.h>
+#include <IndustryStandard/Acpi63.h>
+#include <IndustryStandard/AcpiAml.h>
+#include <IndustryStandard/Acpi.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <Protocol/AcpiTable.h>
+#include <Protocol/AcpiSystemDescriptionTable.h>
+
+#include <Library/AcpiHelperLib.h>
+
+#define DSDT_SIGNATURE 0x54445344
+#define FADT_SIGNATURE 0x50434146
+
+/**
+ This function calculates and updates an UINT8 checksum.
+
+ @param[in] Buffer Pointer to buffer to checksum
+ @param[in] Size Number of bytes to checksum
+
+**/
+VOID
+EFIAPI
+AcpiTableChecksum (
+ IN UINT8 *Buffer,
+ IN UINTN Size
+ )
+{
+ UINTN ChecksumOffset;
+
+ ASSERT (Buffer != NULL);
+
+ ChecksumOffset = OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER, Checksum);
+
+ /*
+ * Set checksum to 0 first.
+ */
+ Buffer[ChecksumOffset] = 0;
+
+ /*
+ * Update checksum value.
+ */
+ Buffer[ChecksumOffset] = 0 - CalculateSum8 (Buffer, Size);
+}
+
+/**
+ This function calculates and updates the ACPI DSDT checksum.
+
+ @param[in] AcpiTableProtocol Pointer to ACPI table protocol
+
+**/
+VOID
+EFIAPI
+AcpiDSDTUpdateChecksum (
+ IN EFI_ACPI_SDT_PROTOCOL *AcpiTableProtocol
+ )
+{
+ EFI_STATUS Status = EFI_SUCCESS;
+ EFI_ACPI_SDT_HEADER *DsdtHdr = NULL;
+ ACPI_TABLE_DESCRIPTOR TableDescriptor;
+ EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *FadtPtr = NULL;
+
+ ASSERT (AcpiTableProtocol != NULL);
+
+ Status = AcpiGetTable (AcpiTableProtocol, FADT_SIGNATURE, &TableDescriptor);
+ if (EFI_ERROR (Status) || TableDescriptor.Table == NULL) {
+ return;
+ }
+
+ FadtPtr = (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *) TableDescriptor.Table;
+
+ if (FadtPtr->Dsdt) {
+ DsdtHdr = (EFI_ACPI_SDT_HEADER *)(UINT64) FadtPtr->Dsdt;
+ } else if (FadtPtr->XDsdt) {
+ DsdtHdr = (EFI_ACPI_SDT_HEADER *) FadtPtr->XDsdt;
+ }
+
+ if (DsdtHdr != NULL) {
+ AcpiTableChecksum ((UINT8 *) DsdtHdr, DsdtHdr->Length);
+ }
+}
+
+/**
+ This function return the handle of the ACPI DSDT table.
+
+ @param[in] AcpiTableProtocol Pointer to ACPI table protocol.
+ @param[out] TableHandle Pointer to table handle.
+
+**/
+EFI_STATUS
+EFIAPI
+AcpiOpenDSDT (
+ IN EFI_ACPI_SDT_PROTOCOL *AcpiTableProtocol,
+ OUT EFI_ACPI_HANDLE *TableHandle
+ )
+{
+ EFI_STATUS Status = EFI_SUCCESS;
+ ACPI_TABLE_DESCRIPTOR TableDescriptor;
+
+ Status = AcpiGetTable (AcpiTableProtocol, DSDT_SIGNATURE, &TableDescriptor);
+ if (!EFI_ERROR (Status) && (TableDescriptor.Table != NULL)) {
+ return AcpiTableProtocol->OpenSdt (TableDescriptor.TableKey, TableHandle);
+ }
+
+ return Status;
+}
+
+EFI_STATUS
+EFIAPI
+AcpiDSDTSetNodeStatusValue (
+ IN CHAR8 *AsciiNodePath,
+ IN CHAR8 NodeStatus
+ )
+{
+ EFI_STATUS Status = EFI_SUCCESS;
+ EFI_ACPI_SDT_PROTOCOL *AcpiTableProtocol;
+ EFI_ACPI_HANDLE TableHandle;
+ EFI_ACPI_HANDLE ChildHandle;
+ EFI_ACPI_DATA_TYPE DataType;
+ CHAR8 *Buffer;
+ UINTN DataSize;
+
+ Status = gBS->LocateProtocol (&gEfiAcpiSdtProtocolGuid, NULL, (VOID**) &AcpiTableProtocol);
+ if (EFI_ERROR(Status)) {
+ DEBUG ((DEBUG_ERROR, "Unable to locate ACPI table protocol\n"));
+ return Status;
+ }
+
+ /* Open DSDT Table */
+ Status = AcpiOpenDSDT (AcpiTableProtocol, &TableHandle);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ Status = AcpiTableProtocol->FindPath (TableHandle, AsciiNodePath, &ChildHandle);
+ if (EFI_ERROR (Status)) {
+ /* Close DSDT Table */
+ AcpiTableProtocol->Close (TableHandle);
+ return EFI_SUCCESS;
+ }
+
+ Status = AcpiTableProtocol->GetOption (ChildHandle, 2, &DataType, (VOID *) &Buffer, &DataSize);
+ if (Status == EFI_SUCCESS && Buffer[2] == AML_BYTE_PREFIX) {
+ /*
+ * Only patch when the initial value is byte object.
+ */
+ Buffer[3] = NodeStatus;
+ }
+
+ /* Close DSDT Table */
+ AcpiTableProtocol->Close (TableHandle);
+
+ /* Update DSDT Checksum */
+ AcpiDSDTUpdateChecksum (AcpiTableProtocol);
+
+ return EFI_SUCCESS;
+}
+
+/**
+ This function return the ACPI table matching a signature.
+
+ @param[in] AcpiTableSdtProtocol Pointer to ACPI SDT protocol.
+ @param[in] TableSignature ACPI table signature.
+ @param[out] TableDescriptor Pointer to ACPI table descriptor.
+
+**/
+EFI_STATUS
+EFIAPI
+AcpiGetTable (
+ IN EFI_ACPI_SDT_PROTOCOL *AcpiTableSdtProtocol,
+ IN UINT32 TableSignature,
+ OUT ACPI_TABLE_DESCRIPTOR *TableDescriptor
+ )
+{
+ EFI_STATUS Status = EFI_SUCCESS;
+ UINTN TableIndex = 0;
+
+ ASSERT (AcpiTableSdtProtocol != NULL);
+ ASSERT (TableDescriptor != NULL);
+
+ /*
+ * Search for ACPI Table Signature
+ */
+ while (!EFI_ERROR (Status)) {
+ Status = AcpiTableSdtProtocol->GetAcpiTable (
+ TableIndex,
+ &(TableDescriptor->Table),
+ &(TableDescriptor->TableVersion),
+ &(TableDescriptor->TableKey)
+ );
+ if (!EFI_ERROR (Status)) {
+ TableIndex++;
+
+ if (((EFI_ACPI_SDT_HEADER *)TableDescriptor->Table)->Signature == TableSignature) {
+ return EFI_SUCCESS;
+ }
+ }
+ }
+
+ /* Nothing was found. Clear the table descriptor. */
+ ZeroMem(&TableDescriptor, sizeof(TableDescriptor));
+
+ return EFI_NOT_FOUND;
+}
+
+/**
+ Check whether the ACPI table is installed or not.
+
+ @param[in] AcpiTableSignature ACPI table signature.
+
+ @retval TRUE Already installed.
+ @retval FALSE Not installed.
+
+**/
+BOOLEAN
+EFIAPI
+IsAcpiInstalled (
+ IN UINT32 AcpiTableSignature
+ )
+{
+ EFI_STATUS Status;
+ ACPI_TABLE_DESCRIPTOR TableDescriptor;
+ EFI_ACPI_SDT_PROTOCOL *AcpiTableSdtProtocol = NULL;
+
+ Status = gBS->LocateProtocol (&gEfiAcpiSdtProtocolGuid, NULL, (VOID**) &AcpiTableSdtProtocol);
+ if (EFI_ERROR (Status)) {
+ return Status;
+ }
+
+ Status = AcpiGetTable (AcpiTableSdtProtocol, AcpiTableSignature, &TableDescriptor);
+ if (!EFI_ERROR (Status) && (TableDescriptor.Table != NULL)) {
+ return TRUE;
+ }
+
+ return FALSE;
+}
--
2.17.1
next prev parent reply other threads:[~2020-12-09 9:24 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-09 9:24 [edk2-platforms][PATCH 00/34] Add new Ampere Mt. Jade platform Nhi Pham
2020-12-09 9:24 ` [edk2-platforms][PATCH 01/34] Initial support for Ampere Altra and " Nhi Pham
2021-01-07 23:57 ` [edk2-devel] " Leif Lindholm
2021-01-15 4:59 ` Vu Nguyen
2020-12-09 9:24 ` [edk2-platforms][PATCH 02/34] Platform/Ampere: Implement FailSafe library Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 03/34] Platform/Ampere: Add FailSafe and WDT support Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 04/34] AmpereAltraPkg: Implement GpioLib and I2cLib modules Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 05/34] JadePkg: Implement RealTimeClockLib for PCF85063 Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 06/34] Platform/Ampere: Add AcpiPccLib to support ACPI PCCT Table Nhi Pham
2020-12-09 9:25 ` Nhi Pham [this message]
2020-12-09 9:25 ` [edk2-platforms][PATCH 08/34] JadePkg: Initial support for static ACPI tables Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 09/34] JadePkg: Install some ACPI tables at runtime Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 10/34] Silicon/Ampere: Support Non Volatile storage for Variable service Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 11/34] Silicon/Ampere: Support PlatformManagerUiLib Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 12/34] AmpereAltraPkg: Add PcieCore Library Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 13/34] AmpereAltraPkg: Add PciHostBridge driver Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 14/34] JadePkg: Add implementation for PcieBoardLib Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 15/34] JadePkg: Enable PCIe support Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 16/34] JadePkg: Add ASpeed GOP driver Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 17/34] Silicon/Ampere: Add Random Number Generator Support Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 18/34] Silicon/Ampere: Fixup runtime memory attribute Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 19/34] JadePkg: Add SMBIOS tables support Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 20/34] AmpereAltraPkg: Add DebugInfoPei module Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 21/34] Silicon/Ampere: Add platform info screen Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 22/34] Silicon/Ampere: Add Memory " Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 23/34] AmpereAltraPkg: Add CPU Configuration for SubNUMA Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 24/34] AmpereAltraPkg: Add ACPI configuration screen Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 25/34] AmpereAltraPkg: Implement PlatformFlashAccessLib instance Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 26/34] JadePkg: Add implementation for UEFI Capsule Update Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 27/34] JadePkg: Add Capsule Update support Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 28/34] Silicon/Ampere: Implement PlatformBootManagerLib for LinuxBoot Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 29/34] Platform/Ampere: Add LinuxBoot image Nhi Pham
2020-12-10 12:40 ` [edk2-devel] " Leif Lindholm
2020-12-11 2:38 ` Nhi Pham
2020-12-11 11:15 ` Leif Lindholm
2020-12-09 9:25 ` [edk2-platforms][PATCH 30/34] JadePkg: Support LinuxBoot DSC/FDF build for Jade platform Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 31/34] AmpereAltraPkg: Add BootProgress support Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 32/34] JadePkg: Add ACPI/APEI tables Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 33/34] Platform/Ampere: Add AcpiApeiLib Nhi Pham
2020-12-09 9:25 ` [edk2-platforms][PATCH 34/34] AmpereAltraPkg, JadePkg: Add RAS setting screen Nhi Pham
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201209092531.30867-8-nhi@os.amperecomputing.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox