Skip to content

Commit

Permalink
Rk356x: Add option to disable EFI_MEMORY_ATTRIBUTE_PROTOCOL.
Browse files Browse the repository at this point in the history
Fix for #73
  • Loading branch information
jaredmcneill committed Feb 23, 2024
1 parent 37d9861 commit 43b1cfe
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@
gRk356xTokenSpaceGuid.PcdSystemTableMode|L"SystemTableMode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdCpuClock|L"CpuClock"|gConfigDxeFormSetGuid|0x0|2
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@
gRk356xTokenSpaceGuid.PcdSystemTableMode|L"SystemTableMode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdCpuClock|L"CpuClock"|gConfigDxeFormSetGuid|0x0|2
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
1 change: 1 addition & 0 deletions edk2-rockchip/Platform/OrangePi/OrangePi3B/OrangePi3B.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@
gRk356xTokenSpaceGuid.PcdSystemTableMode|L"SystemTableMode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdCpuClock|L"CpuClock"|gConfigDxeFormSetGuid|0x0|2
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
1 change: 1 addition & 0 deletions edk2-rockchip/Platform/Pine64/PineTab2/PineTab2.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@
gRk356xTokenSpaceGuid.PcdCpuClock|L"CpuClock"|gConfigDxeFormSetGuid|0x0|2
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMultiPhy1Mode|L"MultiPhy1Mode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
1 change: 1 addition & 0 deletions edk2-rockchip/Platform/Pine64/Quartz64/Quartz64.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMultiPhy1Mode|L"MultiPhy1Mode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdFanMode|L"FanMode"|gConfigDxeFormSetGuid|0x0|1
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
1 change: 1 addition & 0 deletions edk2-rockchip/Platform/Pine64/SOQuartz/SOQuartz.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@
gRk356xTokenSpaceGuid.PcdSystemTableMode|L"SystemTableMode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdCpuClock|L"CpuClock"|gConfigDxeFormSetGuid|0x0|2
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
1 change: 1 addition & 0 deletions edk2-rockchip/Platform/Radxa/CM3/CM3.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ gRk356xTokenSpaceGuid.PcdSystemTableMode|L"CpuClock"|gConfigDxeFormSetGuid|0x0|1
gRk356xTokenSpaceGuid.PcdSystemTableMode|L"SystemTableMode"|gConfigDxeFormSetGuid|0x0|0
gRk356xTokenSpaceGuid.PcdCpuClock|L"CpuClock"|gConfigDxeFormSetGuid|0x0|2
gRk356xTokenSpaceGuid.PcdCustomCpuClock|L"CustomCpuClock"|gConfigDxeFormSetGuid|0x0|816
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|L"MemAttrProtocolEnable"|gConfigDxeFormSetGuid|1

#
# Common UEFI ones.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @file
*
* Copyright (c) 2023, Jared McNeill <jmcneill@invisible.ca>
* Copyright (c) 2023, Mario Bălănică <mariobalanica02@gmail.com>
* Copyright (c) 2019 - 2020, ARM Limited. All rights reserved.
* Copyright (c) 2018 - 2020, Andrei Warkentin <andrey.warkentin@gmail.com>
*
Expand All @@ -25,6 +26,7 @@
#include <Library/GpioLib.h>
#include <Protocol/ArmScmi.h>
#include <Protocol/ArmScmiClockProtocol.h>
#include <Protocol/MemoryAttribute.h>
#include <ConfigVars.h>
#include "ConfigDxeFormSetGuid.h"
#include "ConfigDxe.h"
Expand Down Expand Up @@ -261,9 +263,7 @@ SetupVariables (
{
UINTN Size;
UINT32 Var32;
#if FAN_GPIO_BANK != 0xFF
BOOLEAN VarBool;
#endif
EFI_STATUS Status;

/*
Expand All @@ -280,6 +280,15 @@ SetupVariables (
ASSERT_EFI_ERROR (Status);
}

Size = sizeof (BOOLEAN);
Status = gRT->GetVariable (L"MemAttrProtocolEnable",
&gConfigDxeFormSetGuid,
NULL, &Size, &VarBool);
if (EFI_ERROR (Status)) {
Status = PcdSetBoolS (PcdMemAttrProtocolEnable, PcdGetBool (PcdMemAttrProtocolEnable));
ASSERT_EFI_ERROR (Status);
}

Size = sizeof (UINT32);
Status = gRT->GetVariable (L"CpuClock",
&gConfigDxeFormSetGuid,
Expand Down Expand Up @@ -403,6 +412,77 @@ RemoveTables (
}


/**
This function uninstalls the recently added EFI_MEMORY_ATTRIBUTE_PROTOCOL
to workaround older versions of OS loaders/shims using it incorrectly and
throwing a Synchronous Exception.
Source: https://github.com/edk2-porting/edk2-rk3588/commit/842db13abdcf46a2cc45570806dd7c0e0bc00db8
See:
- https://github.com/microsoft/mu_silicon_arm_tiano/issues/124
- https://edk2.groups.io/g/devel/topic/99631663
**/
STATIC
VOID
EFIAPI
UninstallMemoryAttributeProtocol (
VOID
)
{
EFI_STATUS Status;
EFI_HANDLE *Handles;
UINTN HandleCount;
EFI_MEMORY_ATTRIBUTE_PROTOCOL *MemoryAttributeProtocol;

Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiMemoryAttributeProtocolGuid,
NULL,
&HandleCount,
&Handles
);
ASSERT_EFI_ERROR (Status);
ASSERT (HandleCount == 1);

Status = gBS->HandleProtocol (
Handles[0],
&gEfiMemoryAttributeProtocolGuid,
(VOID **)&MemoryAttributeProtocol
);
ASSERT_EFI_ERROR (Status);

Status = gBS->UninstallMultipleProtocolInterfaces (
Handles[0],
&gEfiMemoryAttributeProtocolGuid,
MemoryAttributeProtocol,
NULL
);
ASSERT_EFI_ERROR (Status);

gBS->FreePool (Handles);
}


STATIC
VOID
EFIAPI
RemoveMemAttrProtocol (
EFI_EVENT Event,
VOID *Context
)
{
gBS->CloseEvent (Event);

if (PcdGetBool (PcdMemAttrProtocolEnable) == TRUE) {
return;
}

DEBUG ((DEBUG_INFO, "Removing EFI_MEMORY_ATTRIBUTE_PROTOCOL\n"));
UninstallMemoryAttributeProtocol ();
}


EFI_STATUS
EFIAPI
ConfigInitialize (
Expand All @@ -411,7 +491,7 @@ ConfigInitialize (
)
{
EFI_STATUS Status;
EFI_EVENT EndOfDxeEvent;
EFI_EVENT Event;

Status = SetupVariables ();
if (Status != EFI_SUCCESS) {
Expand All @@ -426,15 +506,19 @@ ConfigInitialize (
}

Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, RemoveTables,
NULL, &gEfiEndOfDxeEventGroupGuid, &EndOfDxeEvent);
NULL, &gEfiEndOfDxeEventGroupGuid, &Event);
ASSERT_EFI_ERROR (Status);

Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, RemoveTables,
NULL, &gEfiAcpiTableGuid, &EndOfDxeEvent);
NULL, &gEfiAcpiTableGuid, &Event);
ASSERT_EFI_ERROR (Status);

Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, RemoveTables,
NULL, &gEfiAcpi10TableGuid, &EndOfDxeEvent);
NULL, &gEfiAcpi10TableGuid, &Event);
ASSERT_EFI_ERROR (Status);

Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, RemoveMemAttrProtocol,
NULL, &gEfiEndOfDxeEventGroupGuid, &Event);
ASSERT_EFI_ERROR (Status);

return EFI_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
gEfiAcpi10TableGuid

[Protocols]
gEfiMemoryAttributeProtocolGuid ## CONSUMES

[FixedPcd]
gRk356xTokenSpaceGuid.PcdFanGpioBank
Expand All @@ -73,6 +74,7 @@
gRk356xTokenSpaceGuid.PcdCustomCpuClock
gRk356xTokenSpaceGuid.PcdMultiPhy1Mode
gRk356xTokenSpaceGuid.PcdFanMode
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable

[Depex]
gPcdProtocolGuid
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#string STR_SYSCONFIG_SYSTAB_BOTH #language en-US "ACPI + Devicetree"
#string STR_SYSCONFIG_SYSTAB_DT #language en-US "Devicetree"

#string STR_SYSCONFIG_MEMATTR_PROMPT #language en-US "EFI_MEMORY_ATTRIBUTE_PROTOCOL Support"
#string STR_SYSCONFIG_MEMATTR_HELP #language en-US "Disable EFI_MEMORY_ATTRIBUTE_PROTOCOL support to work around buggy Linux distributions."

#string STR_SYSCONFIG_CPUCLOCK_PROMPT #language en-US "CPU Clock"
#string STR_SYSCONFIG_CPUCLOCK_HELP #language en-US "CPU Speed"
#string STR_SYSCONFIG_CPUCLOCK_LOW #language en-US "Low"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ formset
name = SystemTableMode,
guid = CONFIGDXE_FORM_SET_GUID;

efivarstore MEMATTR_VARSTORE_DATA,
attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
name = MemAttrProtocolEnable,
guid = CONFIGDXE_FORM_SET_GUID;

efivarstore CPUCLOCK_VARSTORE_DATA,
attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
name = CpuClock,
Expand Down Expand Up @@ -68,6 +73,13 @@ formset
option text = STRING_TOKEN(STR_SYSCONFIG_SYSTAB_DT), value = SYSTEM_TABLE_MODE_DT, flags = 0;
endoneof;

checkbox varid = MemAttrProtocolEnable.Enable,
prompt = STRING_TOKEN(STR_SYSCONFIG_MEMATTR_PROMPT),
help = STRING_TOKEN(STR_SYSCONFIG_MEMATTR_HELP),
flags = CHECKBOX_DEFAULT | CHECKBOX_DEFAULT_MFG | RESET_REQUIRED,
default = 1,
endcheckbox;

oneof varid = CpuClock.Clock,
prompt = STRING_TOKEN(STR_SYSCONFIG_CPUCLOCK_PROMPT),
help = STRING_TOKEN(STR_SYSCONFIG_CPUCLOCK_HELP),
Expand Down
4 changes: 4 additions & 0 deletions edk2-rockchip/Platform/Rockchip/Rk356x/Include/ConfigVars.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ typedef struct {
UINT32 Mode;
} SYSTEM_TABLE_MODE_VARSTORE_DATA;

typedef struct {
BOOLEAN Enable;
} MEMATTR_VARSTORE_DATA;

typedef struct {
#define CPUCLOCK_LOW 0
#define CPUCLOCK_DEFAULT 1
Expand Down
1 change: 1 addition & 0 deletions edk2-rockchip/Platform/Rockchip/Rk356x/Rk356x.dec
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@
gRk356xTokenSpaceGuid.PcdCustomCpuClock|816|UINT32|0x00004003
gRk356xTokenSpaceGuid.PcdMultiPhy1Mode|0|UINT32|0x00004004
gRk356xTokenSpaceGuid.PcdFanMode|FALSE|BOOLEAN|0x00040005
gRk356xTokenSpaceGuid.PcdMemAttrProtocolEnable|TRUE|BOOLEAN|0x00040006

0 comments on commit 43b1cfe

Please sign in to comment.