From nobody Mon Sep 16 19:09:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+114559+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+114559+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1706250669; cv=none; d=zohomail.com; s=zohoarc; b=iOKhuIgN3xQD8+vvLqFt7y8Kff9sXdfB8U4SK9QOC+3h6EAWQNzhiF3Vgi3Q7z903LAPtF5DSXIM/p8AncwHZBLgdSGnmY0gsWPT2799Lc0mluzXVdEv10xXH0ujLupDKrKQ5d4zbsjekM1w+vP/HORNrfCn5nykq1RaLJIXRRQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706250669; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=t0H3zO9xxPtItZMcakP5oPqshFiCrp4J3z0IbT3l/GQ=; b=b7OO+hbZNlTAFtQ8Bj6isLXjvdmduE3W7UllX5RNeJxlErHDtvPGivRft2cNDCcVHKoDKDTa2zuCrqJ0M+YIgBvFqgDW3rlQZfTgYcE2oIrDSMPLrbBOyfyyvnuymAx6y9LuDltsJGpY0Q/QOkQ5YMJKstoe+0O+yB2xd7gYNzA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+114559+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1706250669786508.47959086952415; Thu, 25 Jan 2024 22:31:09 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=5ccBFfTtJM/zj6wXb9ZxCTQki+IYtCz6LILWfnqF/0A=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1706250669; v=1; b=Ne7WAMw9YZVtPjz79BKVJvxiwqUHYQFW3eLzAtZAT04u3kmYX/4yE8CIU0gN2Ih5aGXIluav QSUIQaSMEarRoUiX1ER0JPk6pvu0bIX9fsD+NuAWvb9veR2KIkKO9gy6QECaN78jR9lSfFA4IEC R8lzBLHN8/DicaRXjsNoWH58= X-Received: by 127.0.0.2 with SMTP id NLyVYY1788612xtS3bPRs9kz; Thu, 25 Jan 2024 22:31:09 -0800 X-Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web10.10172.1706250668045398151 for ; Thu, 25 Jan 2024 22:31:08 -0800 X-Received: from loongson.cn (unknown [10.2.9.245]) by gateway (Coremail) with SMTP id _____8BxieinUbNl+h4GAA--.2171S3; Fri, 26 Jan 2024 14:31:03 +0800 (CST) X-Received: from code-server.gen (unknown [10.2.9.245]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxTs2lUbNliXMbAA--.51221S2; Fri, 26 Jan 2024 14:31:01 +0800 (CST) From: "Chao Li" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Laszlo Ersek , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Bibo Mao , Dongyan Qian , Xianglai Li Subject: [edk2-devel] [PATCH v8 33/37] OvmfPkg/LoongArchVirt: Add reset system library Date: Fri, 26 Jan 2024 14:31:00 +0800 Message-Id: <20240126063100.3102914-1-lichao@loongson.cn> In-Reply-To: <20240126062715.3099433-1-lichao@loongson.cn> References: <20240126062715.3099433-1-lichao@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8AxTs2lUbNliXMbAA--.51221S2 X-CM-SenderInfo: xolfxt3r6o00pqjv00gofq/1tbiAQAICGWyG+AOvAAFsa X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,lichao@loongson.cn List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 7FqLfZmQ3E2Sv3DufzeS8SBXx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1706250670614100001 Content-Type: text/plain; charset="utf-8" This library provides interface related to restart and shudown the LoongArch64 virtual machine. BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4584 Cc: Ard Biesheuvel Cc: Laszlo Ersek Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Bibo Mao Cc: Dongyan Qian Signed-off-by: Chao Li Co-authored-by: Xianglai Li Co-authored-by: Bibo Mao Reviewed-by: Bibo Mao --- .../BaseResetSystemAcpiGed.c | 148 ++++++++++ .../BaseResetSystemAcpiGedLib.inf | 36 +++ .../DxeResetSystemAcpiGed.c | 259 ++++++++++++++++++ .../DxeResetSystemAcpiGedLib.inf | 41 +++ .../ResetSystemAcpiLib/ResetSystemAcpiGed.c | 125 +++++++++ .../ResetSystemAcpiLib/ResetSystemAcpiGed.h | 23 ++ 6 files changed, 632 insertions(+) create mode 100644 OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseRe= setSystemAcpiGed.c create mode 100644 OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseRe= setSystemAcpiGedLib.inf create mode 100644 OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeRes= etSystemAcpiGed.c create mode 100644 OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeRes= etSystemAcpiGedLib.inf create mode 100644 OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetS= ystemAcpiGed.c create mode 100644 OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetS= ystemAcpiGed.h diff --git a/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseResetSyst= emAcpiGed.c b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseResetSys= temAcpiGed.c new file mode 100644 index 0000000000..0d94a62a38 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseResetSystemAcpiG= ed.c @@ -0,0 +1,148 @@ +/** @file + Base ResetSystem library implementation. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include "ResetSystemAcpiGed.h" + +/** + Get configuration item data by the firmware configuration file name. + + @param[in] Name - Name of file to look up. + + @return VOID* The Pointer of Value of Firmware Configuration it= em read. +**/ +VOID * +GetFwCfgData ( + CONST CHAR8 *Name + ) +{ + FIRMWARE_CONFIG_ITEM FwCfgItem; + EFI_STATUS Status; + UINTN FwCfgSize; + VOID *Data; + + Status =3D QemuFwCfgFindFile (Name, &FwCfgItem, &FwCfgSize); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a %d read %s error Status %d \n", __func__, __= LINE__, Name, Status)); + return NULL; + } + + Data =3D AllocatePool (FwCfgSize); + if (Data =3D=3D NULL) { + return NULL; + } + + QemuFwCfgSelectItem (FwCfgItem); + QemuFwCfgReadBytes (FwCfgSize, Data); + + return Data; +} + +/** + Find the power manager related info from ACPI table + + @retval RETURN_SUCCESS Successfully find out all the required inform= ation. + @retval RETURN_NOT_FOUND Failed to find the required info. +**/ +STATIC EFI_STATUS +GetPowerManagerByParseAcpiInfo ( + VOID + ) +{ + EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *Fadt =3D NULL; + EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp =3D NULL; + EFI_ACPI_DESCRIPTION_HEADER *Xsdt =3D NULL; + EFI_ACPI_DESCRIPTION_HEADER *Rsdt =3D NULL; + VOID *AcpiTables =3D NULL; + UINT32 *Entry32 =3D NULL; + UINTN Entry32Num; + UINT32 *Signature =3D NULL; + UINTN Idx; + + Rsdp =3D GetFwCfgData ("etc/acpi/rsdp"); + if (Rsdp =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "%a %d read etc/acpi/rsdp error \n", __func__, __= LINE__)); + return RETURN_NOT_FOUND; + } + + AcpiTables =3D GetFwCfgData ("etc/acpi/tables"); + if (AcpiTables =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "%a %d read etc/acpi/tables error \n", __func__, = __LINE__)); + FreePool (Rsdp); + return RETURN_NOT_FOUND; + } + + Rsdt =3D (EFI_ACPI_DESCRIPTION_HEADER *)((UINTN)AcpiTables + Rsdp= ->RsdtAddress); + Entry32 =3D (UINT32 *)(Rsdt + 1); + Entry32Num =3D (Rsdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) >> = 2; + for (Idx =3D 0; Idx < Entry32Num; Idx++) { + Signature =3D (UINT32 *)((UINTN)Entry32[Idx] + (UINTN)AcpiTables); + if (*Signature =3D=3D EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNAT= URE) { + Fadt =3D (EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *)Signature; + DEBUG ((DEBUG_INFO, "Found Fadt in Rsdt\n")); + goto Done; + } + } + + Xsdt =3D (EFI_ACPI_DESCRIPTION_HEADER *)((UINTN)AcpiTables + Rsdp= ->XsdtAddress); + Entry32 =3D (UINT32 *)(Xsdt + 1); + Entry32Num =3D (Xsdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) >> = 2; + for (Idx =3D 0; Idx < Entry32Num; Idx++) { + Signature =3D (UINT32 *)((UINTN)Entry32[Idx] + (UINTN)AcpiTables); + if (*Signature =3D=3D EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNAT= URE) { + Fadt =3D (EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *)Signature; + DEBUG ((DEBUG_INFO, "Found Fadt in Xsdt\n")); + goto Done; + } + } + + FreePool (Rsdp); + FreePool (AcpiTables); + DEBUG ((DEBUG_ERROR, " Fadt Not Found\n")); + return RETURN_NOT_FOUND; + +Done: + mPowerManager.ResetRegAddr =3D Fadt->ResetReg.Address; + mPowerManager.ResetValue =3D Fadt->ResetValue; + mPowerManager.SleepControlRegAddr =3D Fadt->SleepControlReg.Address; + mPowerManager.SleepStatusRegAddr =3D Fadt->SleepStatusReg.Address; + + FreePool (Rsdp); + FreePool (AcpiTables); + return RETURN_SUCCESS; +} + +/** + The constructor function to initialize mPowerManager. + + @retval EFI_SUCCESS initialize mPowerManager success. + @retval RETURN_NOT_FOUND Failed to initialize mPowerManager. +**/ +EFI_STATUS +ResetSystemLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status =3D GetPowerManagerByParseAcpiInfo (); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d\n", __func__, __LINE__)); + } + + ASSERT (mPowerManager.SleepControlRegAddr); + ASSERT (mPowerManager.SleepStatusRegAddr); + ASSERT (mPowerManager.ResetRegAddr); + return Status; +} diff --git a/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseResetSyst= emAcpiGedLib.inf b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseRes= etSystemAcpiGedLib.inf new file mode 100644 index 0000000000..e163b09284 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/BaseResetSystemAcpiG= edLib.inf @@ -0,0 +1,36 @@ +## @file +# Base library instance for ResetSystem library class for LoongArch +# +# Copyright (c) 2024 Loongson Technology Corporation Limited. All rights = reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 1.29 + BASE_NAME =3D ResetSystemLib + FILE_GUID =3D BA521997-9016-32B5-65DF-EA5F560A3837 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D ResetSystemLib|SEC PEI_CORE PEIM DXE_= CORE + CONSTRUCTOR =3D ResetSystemLibConstructor + +# +# VALID_ARCHITECTURES =3D LOONGARCH64 +# + +[Sources] + BaseResetSystemAcpiGed.c + ResetSystemAcpiGed.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + DebugLib + IoLib + MemoryAllocationLib + QemuFwCfgLib diff --git a/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeResetSyste= mAcpiGed.c b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeResetSyste= mAcpiGed.c new file mode 100644 index 0000000000..c3256ee4ec --- /dev/null +++ b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeResetSystemAcpiGe= d.c @@ -0,0 +1,259 @@ +/** @file + Dxe ResetSystem library implementation. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include // EfiConvertPointer() +#include "ResetSystemAcpiGed.h" + +/** + Modifies the attributes to Runtime type for a page size memory region. + + @param BaseAddress Specified start address + + @retval EFI_SUCCESS The attributes were set for the memory reg= ion. + @retval EFI_INVALID_PARAMETER Length is zero. + @retval EFI_UNSUPPORTED The processor does not support one or more= bytes of the memory + resource range specified by BaseAddress an= d Length. + @retval EFI_UNSUPPORTED The bit mask of attributes is not support = for the memory resource + range specified by BaseAddress and Length. + @retval EFI_ACCESS_DEFINED The attributes for the memory resource ran= ge specified by + BaseAddress and Length cannot be modified. + @retval EFI_OUT_OF_RESOURCES There are not enough system resources to m= odify the attributes of + the memory resource range. + @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU a= rchitectural protocol is + not available yet. +**/ +EFI_STATUS +SetMemoryAttributesRunTime ( + UINTN Address + ) +{ + EFI_STATUS Status; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor; + + Address &=3D ~EFI_PAGE_MASK; + + Status =3D gDS->GetMemorySpaceDescriptor (Address, &Descriptor); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a: GetMemorySpaceDescriptor failed\n", __func__)= ); + return Status; + } + + if (Descriptor.GcdMemoryType =3D=3D EfiGcdMemoryTypeNonExistent) { + Status =3D gDS->AddMemorySpace ( + EfiGcdMemoryTypeMemoryMappedIo, + Address, + EFI_PAGE_SIZE, + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a: AddMemorySpace failed\n", __func__)); + return Status; + } + + Status =3D gDS->SetMemorySpaceAttributes ( + Address, + EFI_PAGE_SIZE, + EFI_MEMORY_RUNTIME + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d SetMemorySpaceAttributes failed\n", __fun= c__, __LINE__)); + return Status; + } + } else if (!(Descriptor.Attributes & EFI_MEMORY_RUNTIME)) { + Status =3D gDS->SetMemorySpaceAttributes ( + Address, + EFI_PAGE_SIZE, + Descriptor.Attributes | EFI_MEMORY_RUNTIME + ); + + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d SetMemorySpaceAttributes failed\n", __fun= c__, __LINE__)); + return Status; + } + } + + return EFI_SUCCESS; +} + +/** + Find the power manager related info from ACPI table + + @retval RETURN_SUCCESS Successfully find out all the required inform= ation. + @retval RETURN_NOT_FOUND Failed to find the required info. +**/ +STATIC EFI_STATUS +GetPowerManagerByParseAcpiInfo ( + VOID + ) +{ + EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *Fadt =3D NULL; + EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER *Rsdp =3D NULL; + EFI_ACPI_DESCRIPTION_HEADER *Xsdt =3D NULL; + EFI_ACPI_DESCRIPTION_HEADER *Rsdt =3D NULL; + UINT32 *Entry32 =3D NULL; + UINTN Entry32Num; + UINT32 *Signature =3D NULL; + UINTN Idx; + EFI_STATUS Status; + + Status =3D EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid, (VOID **)= &Rsdp); + if (EFI_ERROR (Status)) { + Status =3D EfiGetSystemConfigurationTable (&gEfiAcpi10TableGuid, (VOID= **)&Rsdp); + } + + if (EFI_ERROR (Status) || (Rsdp =3D=3D NULL)) { + DEBUG ((DEBUG_ERROR, "EFI_ERROR or Rsdp =3D=3D NULL\n")); + return RETURN_NOT_FOUND; + } + + Rsdt =3D (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Rsdp->RsdtAddress; + Entry32 =3D (UINT32 *)(UINTN)(Rsdt + 1); + Entry32Num =3D (Rsdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) >> = 2; + for (Idx =3D 0; Idx < Entry32Num; Idx++) { + Signature =3D (UINT32 *)(UINTN)Entry32[Idx]; + if (*Signature =3D=3D EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNAT= URE) { + Fadt =3D (EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *)Signature; + DEBUG ((DEBUG_INFO, "Found Fadt in Rsdt\n")); + goto Done; + } + } + + Xsdt =3D (EFI_ACPI_DESCRIPTION_HEADER *)Rsdp->XsdtAddress; + Entry32 =3D (UINT32 *)(Xsdt + 1); + Entry32Num =3D (Xsdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) >> = 2; + for (Idx =3D 0; Idx < Entry32Num; Idx++) { + Signature =3D (UINT32 *)(UINTN)Entry32[Idx]; + if (*Signature =3D=3D EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNAT= URE) { + Fadt =3D (EFI_ACPI_5_0_FIXED_ACPI_DESCRIPTION_TABLE *)Signature; + DEBUG ((DEBUG_INFO, "Found Fadt in Xsdt\n")); + goto Done; + } + } + + DEBUG ((DEBUG_ERROR, " Fadt Not Found\n")); + return RETURN_NOT_FOUND; + +Done: + mPowerManager.ResetRegAddr =3D Fadt->ResetReg.Address; + mPowerManager.ResetValue =3D Fadt->ResetValue; + mPowerManager.SleepControlRegAddr =3D Fadt->SleepControlReg.Address; + mPowerManager.SleepStatusRegAddr =3D Fadt->SleepStatusReg.Address; + return RETURN_SUCCESS; +} + +/** + This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS= _CHANGE + event. It converts a pointer to a new virtual address. + + @param[in] Event Event whose notification function is being invok= ed. + @param[in] Context Pointer to the notification function's context +**/ +VOID +EFIAPI +ResetSystemLibAddressChangeEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0, (VOID **)&mPowerManager.SleepControlRegAddr); + EfiConvertPointer (0, (VOID **)&mPowerManager.SleepStatusRegAddr); + EfiConvertPointer (0, (VOID **)&mPowerManager.ResetRegAddr); +} + +/** + Notification function of ACPI Table change. + + This is a notification function registered on ACPI Table change event. + It saves the Century address stored in ACPI FADT table. + + @param Event Event whose notification function is being invoked. + @param Context Pointer to the notification function's context. +**/ +STATIC VOID +AcpiNotificationEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + + Status =3D GetPowerManagerByParseAcpiInfo (); + if (EFI_ERROR (Status)) { + return; + } + + DEBUG ((DEBUG_INFO, "%a: sleepControl %llx\n", __func__, mPowerManager.S= leepControlRegAddr)); + ASSERT (mPowerManager.SleepControlRegAddr); + Status =3D SetMemoryAttributesRunTime (mPowerManager.SleepControlRegAdd= r); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d\n", __func__, __LINE__)); + return; + } + + DEBUG ((DEBUG_INFO, "%a: sleepStatus %llx\n", __func__, mPowerManager.Sl= eepStatusRegAddr)); + ASSERT (mPowerManager.SleepStatusRegAddr); + Status =3D SetMemoryAttributesRunTime (mPowerManager.SleepStatusRegAddr= ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d\n", __func__, __LINE__)); + return; + } + + DEBUG ((DEBUG_INFO, "%a: ResetReg %llx\n", __func__, mPowerManager.Reset= RegAddr)); + ASSERT (mPowerManager.ResetRegAddr); + Status =3D SetMemoryAttributesRunTime (mPowerManager.ResetRegAddr); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d\n", __func__, __LINE__)); + } + + return; +} + +/** + The constructor function to Register ACPI Table change event and Address= Change Event. + + @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. +**/ +EFI_STATUS +EFIAPI +ResetSystemLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_EVENT Event; + EFI_EVENT ResetSystemVirtualNotifyEvent; + + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + AcpiNotificationEvent, + NULL, + &gEfiAcpiTableGuid, + &Event + ); + + // + // Register SetVirtualAddressMap () notify function + // + Status =3D gBS->CreateEvent ( + EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, + TPL_NOTIFY, + ResetSystemLibAddressChangeEvent, + NULL, + &ResetSystemVirtualNotifyEvent + ); + ASSERT_EFI_ERROR (Status); + return Status; +} diff --git a/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeResetSyste= mAcpiGedLib.inf b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeReset= SystemAcpiGedLib.inf new file mode 100644 index 0000000000..9815e2f6e7 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/DxeResetSystemAcpiGe= dLib.inf @@ -0,0 +1,41 @@ +## @file +# DXE library instance for ResetSystem library class for LoongArch. +# +# Copyright (c) 2024 Loongson Technology Corporation Limited. All rights = reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 1.29 + BASE_NAME =3D ResetSystemLib + FILE_GUID =3D F05197D5-5827-AA61-FB2D-BC69259F17A9 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D ResetSystemLib|DXE_DRIVER DXE_RUNTIME= _DRIVER SMM_CORE DXE_SMM_DRIVER UEFI_DRIVER UEFI_APPLICATION + CONSTRUCTOR =3D ResetSystemLibConstructor + +# +# VALID_ARCHITECTURES =3D LOONGARCH64 +# + +[Sources] + DxeResetSystemAcpiGed.c + ResetSystemAcpiGed.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + DebugLib + DxeServicesTableLib + UefiBootServicesTableLib + UefiLib + UefiRuntimeLib + +[Guids] + gEfiAcpi10TableGuid ## PRODUCES ## S= ystemTable + gEfiAcpiTableGuid ## PRODUCES ## S= ystemTable diff --git a/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetSystemAc= piGed.c b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetSystemAcpiG= ed.c new file mode 100644 index 0000000000..105382da35 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetSystemAcpiGed.c @@ -0,0 +1,125 @@ +/** @file + ResetSystem library implementation. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include // CpuDeadLoop() +#include +#include +#include // ResetCold() +#include "ResetSystemAcpiGed.h" + +POWER_MANAGER mPowerManager; + +/** + Calling this function causes a system-wide reset. This sets + all circuitry within the system to its initial state. This type of reset + is asynchronous to system operation and operates without regard to + cycle boundaries. + + System reset should not return, if it returns, it means the system does + not support cold reset. +**/ +STATIC VOID +AcpiGedReset ( + VOID + ) +{ + MmioWrite8 ( + (UINTN)mPowerManager.ResetRegAddr, + mPowerManager.ResetValue + ); + + CpuDeadLoop (); +} + +/** + This function causes the system to enter a power state equivalent + to the ACPI S5 states. + + * */ +STATIC VOID +AcpiGedShutdown ( + VOID + ) +{ + MmioWrite8 ( + (UINTN)mPowerManager.SleepControlRegAddr, + (1 << 5) /* enable bit */ | + (5 << 2) /* typ =3D=3D S5 */ + ); + + CpuDeadLoop (); +} + +/** + This function causes a system-wide reset (cold reset), in which + all circuitry within the system returns to its initial state. This type = of + reset is asynchronous to system operation and operates without regard to + cycle boundaries. + + If this function returns, it means that the system does not support cold + reset. +**/ +VOID EFIAPI +ResetCold ( + VOID + ) +{ + AcpiGedReset (); +} + +/** + This function causes a system-wide initialization (warm reset), in which= all + processors are set to their initial state. Pending cycles are not corrup= ted. + + If this function returns, it means that the system does not support warm + reset. +**/ +VOID EFIAPI +ResetWarm ( + VOID + ) +{ + AcpiGedReset (); +} + +/** + This function causes a systemwide reset. The exact type of the reset is + defined by the EFI_GUID that follows the Null-terminated Unicode string = passed + into ResetData. If the platform does not recognize the EFI_GUID in Reset= Data + the platform must pick a supported reset type to perform.The platform may + optionally log the parameters from any non-normal reset that occurs. + + @param[in] DataSize The size, in bytes, of ResetData. + @param[in] ResetData The data buffer starts with a Null-terminated str= ing, + followed by the EFI_GUID. +**/ +VOID +EFIAPI +ResetPlatformSpecific ( + IN UINTN DataSize, + IN VOID *ResetData + ) +{ + AcpiGedReset (); +} + +/** + This function causes the system to enter a power state equivalent + to the ACPI G2/S5 or G3 states. + + If this function returns, it means that the system does not support shut= down + reset. +**/ +VOID EFIAPI +ResetShutdown ( + VOID + ) +{ + AcpiGedShutdown (); +} diff --git a/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetSystemAc= piGed.h b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetSystemAcpiG= ed.h new file mode 100644 index 0000000000..06e2572db0 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/Library/ResetSystemAcpiLib/ResetSystemAcpiGed.h @@ -0,0 +1,23 @@ +/** @file + ResetSystem lib head file. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef RESET_SYSTEM_ACPI_GED_H_ +#define RESET_SYSTEM_ACPI_GED_H_ + +#include + +typedef struct { + UINT64 SleepControlRegAddr; + UINT64 SleepStatusRegAddr; + UINT64 ResetRegAddr; + UINT8 ResetValue; +} POWER_MANAGER; + +extern POWER_MANAGER mPowerManager; +#endif // RESET_SYSTEM_ACPI_GED_H_ --=20 2.27.0 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#114559): https://edk2.groups.io/g/devel/message/114559 Mute This Topic: https://groups.io/mt/103971684/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-