From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@semihalf-com.20150623.gappssmtp.com header.s=20150623 header.b=iTrk1LOV; spf=none, err=SPF record not found (domain: semihalf.com, ip: 209.85.167.66, mailfrom: mw@semihalf.com) Received: from mail-lf1-f66.google.com (mail-lf1-f66.google.com [209.85.167.66]) by groups.io with SMTP; Thu, 25 Apr 2019 04:29:39 -0700 Received: by mail-lf1-f66.google.com with SMTP id t30so17226721lfd.8 for ; Thu, 25 Apr 2019 04:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=semihalf-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=5dpRtz0LORTHgCajzjXfL5N4ASROyRZSt22VB0U1oZw=; b=iTrk1LOVo0IdxDzP6MWOgsez0N39RgzrrQwZQd6s368M2G3PJOtcYbsRw5E2D17QNN nPn+KcfkcXgerjL90Y/UoxJMqQPP2iytEgpxOvAtv3yyXWsj3zP6Atct5YCS9d7qtZRS AjTUTDRsnOCZfWDEDATml896ChjeAM4UO06sOZ7GyZA2+zxB9+LGWGw3AH6tdFvlkT4k X8rtA0Q1yYDKJiXWnuvrKjzF+ILm/LMU/zrbg1bHyucORDTsIddWeW7hFsGgg1bfqeAS XX0tHtpz/cwWWX7YSO/s9dVSPRHWGwx4CMndWWwgcfCdj/HX9dvNw2pCnfVzxzSHfE6g eQTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=5dpRtz0LORTHgCajzjXfL5N4ASROyRZSt22VB0U1oZw=; b=qVoPIwuEYaZV1URBOaP+OLHk+K828bV4KFSzh1Kv3h67UZC3TugcuoybzeKSywzU3c dAt8KllhmHK5HD8eOnc6c0nTIAtAHlgcX+yQsQEsc6XIfw9onI6l1U7BMiigyHrwiXRB /OXqGmuoU8Uwyg/niJS8m7pqSVixmba4MtoEgGoi617ZiS4C8biFIGQCfsSwZjyCq7ww yiYIK8zM7uy0x5xpfPBNsDc2REkP246IsALjf3BwB45jLVs/KaikvoX2r1FX0z/Lbshp jsUpumsat4w8uud0BgYbvkjZEWSL5Hwx9iuQGda2j9croPStV1/Et+h2oXuo2Hssj63O qsXg== X-Gm-Message-State: APjAAAUMebzSvTO6ftCEuX6gWKvDgaWG2xSKNDOhaQFZ7zaDpehFFdtP qacdovr9qgBb+xrcUiCUJ4P2XkR2TJM= X-Google-Smtp-Source: APXvYqxWtxsjP1bWt0W+giaHtEF3GFBaMEg6O1f/MC+mdJyCACyFUIBsxezO2/Y0BEaKyhSBffmcZA== X-Received: by 2002:ac2:48bb:: with SMTP id u27mr20542732lfg.17.1556191776730; Thu, 25 Apr 2019 04:29:36 -0700 (PDT) Return-Path: Received: from gilgamesh.semihalf.com (31-172-191-173.noc.fibertech.net.pl. [31.172.191.173]) by smtp.gmail.com with ESMTPSA id c7sm5448111lja.90.2019.04.25.04.29.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 25 Apr 2019 04:29:36 -0700 (PDT) From: "Marcin Wojtas" To: devel@edk2.groups.io Cc: leif.lindholm@linaro.org, ard.biesheuvel@linaro.org, mw@semihalf.com, jsd@semihalf.com, jaz@semihalf.com, kostap@marvell.com, Jici.Gao@arm.com, Kornel Duleba Subject: [edk2-platforms: PATCH v3 4/4] Marvell/Drivers: MvFvbDxe: Introduce non-mmio mode Date: Thu, 25 Apr 2019 13:28:24 +0200 Message-Id: <1556191704-28834-5-git-send-email-mw@semihalf.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1556191704-28834-1-git-send-email-mw@semihalf.com> References: <1556191704-28834-1-git-send-email-mw@semihalf.com> From: Kornel Duleba This path enables support for reading variables directly from flash without relying on it to be memory mapped. It adds PcdSpiMemoryMapped PCD that allows to switch between the modes. When in non-memory-mapped mode the driver will copy the variables from flash to previously allocated buffer and set PcdFlashNvStorageVariableBase64, PcdFlashNvStorageFtwWorkingBase64 and PcdFlashNvStorageFtwSpareBase64 accordingly. Because of that satisfy a depex dependency with the FTW driver by hooking NvVarStoreFormattedLib. Signed-off-by: Marcin Wojtas --- Silicon/Marvell/Marvell.dec | 2 + Silicon/Marvell/Armada7k8k/Armada7k8k.dsc.inc | 15 ++- Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.inf | 10 +- Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.h | 1 + Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.c | 135 +++++++++++++++----- 5 files changed, 127 insertions(+), 36 deletions(-) diff --git a/Silicon/Marvell/Marvell.dec b/Silicon/Marvell/Marvell.dec index 641c53b..2b25ae9 100644 --- a/Silicon/Marvell/Marvell.dec +++ b/Silicon/Marvell/Marvell.dec @@ -137,6 +137,8 @@ #SPI gMarvellTokenSpaceGuid.PcdSpiRegBase|0|UINT32|0x3000051 gMarvellTokenSpaceGuid.PcdSpiMemoryBase|0|UINT64|0x3000059 + gMarvellTokenSpaceGuid.PcdSpiMemoryMapped|TRUE|BOOLEAN|0x3000060 + gMarvellTokenSpaceGuid.PcdSpiVariableOffset|0|UINT32|0x3000061 gMarvellTokenSpaceGuid.PcdSpiMaxFrequency|0|UINT32|0x30000052 gMarvellTokenSpaceGuid.PcdSpiClockFrequency|0|UINT32|0x30000053 diff --git a/Silicon/Marvell/Armada7k8k/Armada7k8k.dsc.inc b/Silicon/Marvell/Armada7k8k/Armada7k8k.dsc.inc index ca3de2e..545b369 100644 --- a/Silicon/Marvell/Armada7k8k/Armada7k8k.dsc.inc +++ b/Silicon/Marvell/Armada7k8k/Armada7k8k.dsc.inc @@ -256,6 +256,11 @@ # USB support gEfiMdeModulePkgTokenSpaceGuid.PcdTurnOffUsbLegacySupport|TRUE +[PcdsDynamicDefault.common] + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0xF93C0000 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0xF93E0000 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0xF93D0000 + [PcdsFixedAtBuild.common] gEfiMdeModulePkgTokenSpaceGuid.PcdFirmwareVersionString|L"MARVELL_EFI" gArmPlatformTokenSpaceGuid.PcdCoreCount|4 @@ -396,11 +401,10 @@ # Variable store - default values # gMarvellTokenSpaceGuid.PcdSpiMemoryBase|0xF9000000 - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0xF93C0000 + gMarvellTokenSpaceGuid.PcdSpiMemoryMapped|TRUE + gMarvellTokenSpaceGuid.PcdSpiVariableOffset|0x3C0000 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize|0x00010000 - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64|0xF93D0000 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0x00010000 - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64|0xF93E0000 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize|0x00010000 !if $(CAPSULE_ENABLE) @@ -558,7 +562,10 @@ # Variable services # Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.inf - MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf + MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf { + + NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf + } MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf { AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf diff --git a/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.inf b/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.inf index e222075..128c538 100644 --- a/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.inf +++ b/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.inf @@ -76,13 +76,17 @@ gMarvellSpiMasterProtocolGuid [FixedPcd] - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize gMarvellTokenSpaceGuid.PcdSpiMemoryBase + gMarvellTokenSpaceGuid.PcdSpiMemoryMapped + gMarvellTokenSpaceGuid.PcdSpiVariableOffset + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 [Depex] gEfiCpuArchProtocolGuid AND diff --git a/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.h b/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.h index 31e6e44..e8df9a5 100644 --- a/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.h +++ b/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.h @@ -55,6 +55,7 @@ typedef struct { UINT32 Signature; + BOOLEAN IsMemoryMapped; UINTN DeviceBaseAddress; UINTN RegionBaseAddress; UINTN Size; diff --git a/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.c b/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.c index cb006cd..b4fd29c 100644 --- a/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.c +++ b/Silicon/Marvell/Drivers/Spi/MvFvbDxe/MvFvbDxe.c @@ -52,6 +52,7 @@ STATIC CONST FVB_DEVICE mMvFvbFlashInstanceTemplate = { FVB_FLASH_SIGNATURE, // Signature + FALSE, // IsMemoryMapped ... NEED TO BE FILLED 0, // DeviceBaseAddress ... NEED TO BE FILLED 0, // RegionBaseAddress ... NEED TO BE FILLED SIZE_256KB, // Size @@ -175,11 +176,14 @@ MvFvbInitFvAndVariableStoreHeaders ( FirmwareVolumeHeader->Attributes = EFI_FVB2_READ_ENABLED_CAP | EFI_FVB2_READ_STATUS | EFI_FVB2_STICKY_WRITE | - EFI_FVB2_MEMORY_MAPPED | EFI_FVB2_ERASE_POLARITY | EFI_FVB2_WRITE_STATUS | EFI_FVB2_WRITE_ENABLED_CAP; + if (FlashInstance->IsMemoryMapped) { + FirmwareVolumeHeader->Attributes |= EFI_FVB2_MEMORY_MAPPED; + } + FirmwareVolumeHeader->HeaderLength = sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY); FirmwareVolumeHeader->Revision = EFI_FVH_REVISION; @@ -349,10 +353,13 @@ MvFvbSetAttributes ( EFI_FVB_ATTRIBUTES_2 OldAttributes; EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes; EFI_FVB_ATTRIBUTES_2 UnchangedAttributes; + FVB_DEVICE *FlashInstance; UINT32 Capabilities; UINT32 OldStatus; UINT32 NewStatus; + FlashInstance = INSTANCE_FROM_FVB_THIS (This); + // // Obtain attributes from FVB header // @@ -369,12 +376,15 @@ MvFvbSetAttributes ( EFI_FVB2_WRITE_ENABLED_CAP | \ EFI_FVB2_LOCK_CAP | \ EFI_FVB2_STICKY_WRITE | \ - EFI_FVB2_MEMORY_MAPPED | \ EFI_FVB2_ERASE_POLARITY | \ EFI_FVB2_READ_LOCK_CAP | \ EFI_FVB2_WRITE_LOCK_CAP | \ EFI_FVB2_ALIGNMENT; + if (FlashInstance->IsMemoryMapped) { + UnchangedAttributes |= EFI_FVB2_MEMORY_MAPPED; + } + // // Some attributes of FV is read only can *not* be set // @@ -692,6 +702,7 @@ MvFvbWrite ( IN UINT8 *Buffer ) { + EFI_STATUS Status; FVB_DEVICE *FlashInstance; UINTN DataOffset; @@ -701,10 +712,27 @@ MvFvbWrite ( FlashInstance->StartLba + Lba, FlashInstance->Media.BlockSize); - return FlashInstance->SpiFlashProtocol->Write (&FlashInstance->SpiDevice, - DataOffset, - *NumBytes, - Buffer); + Status = FlashInstance->SpiFlashProtocol->Write (&FlashInstance->SpiDevice, + DataOffset, + *NumBytes, + Buffer); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "%a: Failed to write to Spi device\n", + __FUNCTION__)); + return Status; + } + + // Update shadow buffer + if (!FlashInstance->IsMemoryMapped) { + DataOffset = GET_DATA_OFFSET (FlashInstance->RegionBaseAddress + Offset, + FlashInstance->StartLba + Lba, + FlashInstance->Media.BlockSize); + + CopyMem ((UINTN *)DataOffset, Buffer, *NumBytes); + } + + return EFI_SUCCESS; } /** @@ -975,6 +1003,9 @@ MvFvbConfigureFlashInstance ( ) { EFI_STATUS Status; + UINTN *NumBytes; + UINTN DataOffset; + UINTN VariableSize, FtwWorkingSize, FtwSpareSize, MemorySize; // Locate SPI protocols @@ -1009,25 +1040,62 @@ MvFvbConfigureFlashInstance ( } // Fill remaining flash description - FlashInstance->DeviceBaseAddress = PcdGet64 (PcdSpiMemoryBase); - FlashInstance->RegionBaseAddress = FixedPcdGet64 (PcdFlashNvStorageVariableBase64); - FlashInstance->FvbOffset = FlashInstance->RegionBaseAddress - - FlashInstance->DeviceBaseAddress; - FlashInstance->FvbSize = PcdGet32(PcdFlashNvStorageVariableSize) + - PcdGet32(PcdFlashNvStorageFtwWorkingSize) + - PcdGet32(PcdFlashNvStorageFtwSpareSize); + VariableSize = PcdGet32 (PcdFlashNvStorageVariableSize); + FtwWorkingSize = PcdGet32 (PcdFlashNvStorageFtwWorkingSize); + FtwSpareSize = PcdGet32 (PcdFlashNvStorageFtwSpareSize); + + FlashInstance->IsMemoryMapped = PcdGetBool (PcdSpiMemoryMapped); + FlashInstance->FvbSize = VariableSize + FtwWorkingSize + FtwSpareSize; + FlashInstance->FvbOffset = PcdGet32 (PcdSpiVariableOffset); FlashInstance->Media.MediaId = 0; FlashInstance->Media.BlockSize = FlashInstance->SpiDevice.Info->SectorSize; FlashInstance->Media.LastBlock = FlashInstance->Size / FlashInstance->Media.BlockSize - 1; + if (FlashInstance->IsMemoryMapped) { + FlashInstance->DeviceBaseAddress = PcdGet64 (PcdSpiMemoryBase); + FlashInstance->RegionBaseAddress = PcdGet64 (PcdFlashNvStorageVariableBase64); + } else { + MemorySize = EFI_SIZE_TO_PAGES (FlashInstance->FvbSize); + + // FaultTolerantWriteDxe requires memory to be aligned to FtwWorkingSize + FlashInstance->RegionBaseAddress = (UINTN) AllocateAlignedRuntimePages (MemorySize, + SIZE_64KB); + if (FlashInstance->RegionBaseAddress == (UINTN) NULL) { + return EFI_OUT_OF_RESOURCES; + } + + PcdSet64 (PcdFlashNvStorageVariableBase64, + (UINT64) FlashInstance->RegionBaseAddress); + PcdSet64 (PcdFlashNvStorageFtwWorkingBase64, + (UINT64) FlashInstance->RegionBaseAddress + + VariableSize); + PcdSet64 (PcdFlashNvStorageFtwSpareBase64, + (UINT64) FlashInstance->RegionBaseAddress + + VariableSize + + FtwWorkingSize); + + // Fill the buffer with data from flash + DataOffset = GET_DATA_OFFSET (FlashInstance->FvbOffset, + FlashInstance->StartLba, + FlashInstance->Media.BlockSize); + *NumBytes = FlashInstance->FvbSize; + Status = FlashInstance->SpiFlashProtocol->Read (&FlashInstance->SpiDevice, + DataOffset, + *NumBytes, + (VOID *)FlashInstance->RegionBaseAddress); + if (EFI_ERROR (Status)) { + goto ErrorFreeAllocatedPages; + } + } + Status = gBS->InstallMultipleProtocolInterfaces (&FlashInstance->Handle, &gEfiDevicePathProtocolGuid, &FlashInstance->DevicePath, &gEfiFirmwareVolumeBlockProtocolGuid, &FlashInstance->FvbProtocol, NULL); if (EFI_ERROR (Status)) { - return Status; + goto ErrorFreeAllocatedPages; } Status = MvFvbPrepareFvHeader (FlashInstance); @@ -1043,6 +1111,12 @@ ErrorPrepareFvbHeader: &gEfiFirmwareVolumeBlockProtocolGuid, NULL); +ErrorFreeAllocatedPages: + if (!FlashInstance->IsMemoryMapped) { + FreeAlignedPages ((VOID *)FlashInstance->RegionBaseAddress, + MemorySize); + } + return Status; } @@ -1094,24 +1168,27 @@ MvFvbEntryPoint ( // // Declare the Non-Volatile storage as EFI_MEMORY_RUNTIME // - RuntimeMmioRegionSize = mFvbDevice->FvbSize; RegionBaseAddress = mFvbDevice->RegionBaseAddress; - Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo, - RegionBaseAddress, - RuntimeMmioRegionSize, - EFI_MEMORY_UC | EFI_MEMORY_RUNTIME); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a: Failed to add memory space\n", __FUNCTION__)); - goto ErrorAddSpace; - } + if (mFvbDevice->IsMemoryMapped) { + RuntimeMmioRegionSize = mFvbDevice->FvbSize; + Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo, + RegionBaseAddress, + RuntimeMmioRegionSize, + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Failed to add memory space\n", __FUNCTION__)); + goto ErrorAddSpace; + } - Status = gDS->SetMemorySpaceAttributes (RegionBaseAddress, - RuntimeMmioRegionSize, - EFI_MEMORY_UC | EFI_MEMORY_RUNTIME); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a: Failed to set memory attributes\n", __FUNCTION__)); - goto ErrorSetMemAttr; + + Status = gDS->SetMemorySpaceAttributes (RegionBaseAddress, + RuntimeMmioRegionSize, + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: Failed to set memory attributes\n", __FUNCTION__)); + goto ErrorSetMemAttr; + } } // -- 2.7.4