From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web09.614.1666630907468544538 for ; Mon, 24 Oct 2022 10:01:47 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cu4IK4xX; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E676A614E0; Mon, 24 Oct 2022 17:01:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06F2DC433D6; Mon, 24 Oct 2022 17:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666630906; bh=/ILPAgrklGDgEG+YuJfZW8DD8J49DDc3acUtgfew4+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cu4IK4xXQjqMpZzRpa96WqO2ac3mJBHhR/ytiLibEoFyu2Cvww01cPkk6blVjEXJ5 fq6iuV7YeVqvNC54jxVBevT4i3qSGeXqmL/FpJhpJv995CLzVsD0Lt1Z7CnIICKjye l433Fw+sclCbqUq9Ho7i6XM2xQV/GozarZFSNAx7YRJDAWrtA3eA07uGTejc0T/uxx 0Z5Ysw6irfQTjwjY7v8S+9ogyVGUJkvl6Iz3UNtu7+hCgQyQ8SOsov5TeHNhjms30h cz6+MnqMaSIJVK1zKXveiJFaXO8RiAlbC+iGmqR6nw57dN70NdHyakLJnNnR/mSmwa ftoj5UejXBZfw== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Gerd Hoffmann , Sunil V L , Sami Mujawar , Leif Lindholm Subject: [PATCH 07/11] OvmfPkg/VirtNorFlashDxe: avoid switching between modes in a tight loop Date: Mon, 24 Oct 2022 19:01:18 +0200 Message-Id: <20221024170122.594577-8-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221024170122.594577-1-ardb@kernel.org> References: <20221024170122.594577-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Currently, when dealing with small updates that can be written out directly (i.e., if they only involve clearing bits and not setting bits, as the latter requires a block level erase), we iterate over the data one word at a time, read the old value, compare it, write the new value, and repeat, unless we encountered a value that we cannot write (0->1 transition), in which case we fall back to a block level operation. This is inefficient for two reasons: - reading and writing a word at a time involves switching between array and programming mode for every word of data, which is disproportionately costly when running under KVM; - we end up writing some data twice, as we may not notice that a block erase is needed until after some data has been written to flash. So replace this sequence with a single read of up to twice the buffered write maximum size, followed by one or two buffered writes if the data can be written directly. Otherwise, fall back to the existing block level sequence, but without writing out part of the data twice. Signed-off-by: Ard Biesheuvel --- OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c | 208 +++++++------------- 1 file changed, 71 insertions(+), 137 deletions(-) diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c b/OvmfPkg/VirtNorFlashD= xe/VirtNorFlash.c index 0a5c5d48c738..d87cdd49d6a6 100644 --- a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c @@ -576,23 +576,20 @@ NorFlashWriteSingleBlock ( IN UINT8 *Buffer=0D )=0D {=0D - EFI_STATUS TempStatus;=0D - UINT32 Tmp;=0D - UINT32 TmpBuf;=0D - UINT32 WordToWrite;=0D - UINT32 Mask;=0D - BOOLEAN DoErase;=0D - UINTN BytesToWrite;=0D + EFI_STATUS Status;=0D UINTN CurOffset;=0D - UINTN WordAddr;=0D UINTN BlockSize;=0D UINTN BlockAddress;=0D - UINTN PrevBlockAddress;=0D -=0D - PrevBlockAddress =3D 0;=0D + UINT8 *OrigData;=0D =0D DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=3D%ld, Of= fset=3D0x%x, *NumBytes=3D0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes,= Buffer));=0D =0D + // Check we did get some memory. Buffer is BlockSize.=0D + if (Instance->ShadowBuffer =3D=3D NULL) {=0D + DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D // Cache the block size to avoid de-referencing pointers all the time=0D BlockSize =3D Instance->BlockSize;=0D =0D @@ -612,148 +609,85 @@ NorFlashWriteSingleBlock ( return EFI_BAD_BUFFER_SIZE;=0D }=0D =0D - // Pick 128bytes as a good start for word operations as opposed to erasi= ng the=0D - // block and writing the data regardless if an erase is really needed.=0D - // It looks like most individual NV variable writes are smaller than 128= bytes.=0D - if (*NumBytes <=3D 128) {=0D + // Pick P30_MAX_BUFFER_SIZE_IN_BYTES (=3D=3D 128 bytes) as a good start = for word=0D + // operations as opposed to erasing the block and writing the data regar= dless=0D + // if an erase is really needed. It looks like most individual NV varia= ble=0D + // writes are smaller than 128 bytes.=0D + // To avoid pathological cases were a 2 byte write is disregarded becaus= e it=0D + // occurs right at a 128 byte buffered write alignment boundary, permit = up to=0D + // twice the max buffer size, and perform two writes if needed.=0D + if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) <=3D (2 * P30_MAX_BUFF= ER_SIZE_IN_BYTES)) {=0D // Check to see if we need to erase before programming the data into N= OR.=0D // If the destination bits are only changing from 1s to 0s we can just= write.=0D // After a block is erased all bits in the block is set to 1.=0D // If any byte requires us to erase we just give up and rewrite all of= it.=0D - DoErase =3D FALSE;=0D - BytesToWrite =3D *NumBytes;=0D - CurOffset =3D Offset;=0D =0D - while (BytesToWrite > 0) {=0D - // Read full word from NOR, splice as required. A word is the smalle= st=0D - // unit we can write.=0D - TempStatus =3D NorFlashRead (Instance, Lba, CurOffset & ~(0x3), size= of (Tmp), &Tmp);=0D - if (EFI_ERROR (TempStatus)) {=0D - return EFI_DEVICE_ERROR;=0D - }=0D -=0D - // Physical address of word in NOR to write.=0D - WordAddr =3D (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS (=0D - Instance->RegionBaseAddress,=0D - Lba,=0D - BlockSize=0D - );=0D - // The word of data that is to be written.=0D - TmpBuf =3D *((UINT32 *)(Buffer + (*NumBytes - BytesToWrite)));=0D -=0D - // First do word aligned chunks.=0D - if ((CurOffset & 0x3) =3D=3D 0) {=0D - if (BytesToWrite >=3D 4) {=0D - // Is the destination still in 'erased' state?=0D - if (~Tmp !=3D 0) {=0D - // Check to see if we are only changing bits to zero.=0D - if ((Tmp ^ TmpBuf) & TmpBuf) {=0D - DoErase =3D TRUE;=0D - break;=0D - }=0D - }=0D -=0D - // Write this word to NOR=0D - WordToWrite =3D TmpBuf;=0D - CurOffset +=3D sizeof (TmpBuf);=0D - BytesToWrite -=3D sizeof (TmpBuf);=0D - } else {=0D - // BytesToWrite < 4. Do small writes and left-overs=0D - Mask =3D ~((~0) << (BytesToWrite * 8));=0D - // Mask out the bytes we want.=0D - TmpBuf &=3D Mask;=0D - // Is the destination still in 'erased' state?=0D - if ((Tmp & Mask) !=3D Mask) {=0D - // Check to see if we are only changing bits to zero.=0D - if ((Tmp ^ TmpBuf) & TmpBuf) {=0D - DoErase =3D TRUE;=0D - break;=0D - }=0D - }=0D -=0D - // Merge old and new data. Write merged word to NOR=0D - WordToWrite =3D (Tmp & ~Mask) | TmpBuf;=0D - CurOffset +=3D BytesToWrite;=0D - BytesToWrite =3D 0;=0D - }=0D - } else {=0D - // Do multiple words, but starting unaligned.=0D - if (BytesToWrite > (4 - (CurOffset & 0x3))) {=0D - Mask =3D ((~0) << ((CurOffset & 0x3) * 8));=0D - // Mask out the bytes we want.=0D - TmpBuf &=3D Mask;=0D - // Is the destination still in 'erased' state?=0D - if ((Tmp & Mask) !=3D Mask) {=0D - // Check to see if we are only changing bits to zero.=0D - if ((Tmp ^ TmpBuf) & TmpBuf) {=0D - DoErase =3D TRUE;=0D - break;=0D - }=0D - }=0D + // Read the old version of the data into the shadow buffer=0D + Status =3D NorFlashRead (=0D + Instance,=0D + Lba,=0D + Offset & ~BOUNDARY_OF_32_WORDS,=0D + (*NumBytes | BOUNDARY_OF_32_WORDS) + 1,=0D + Instance->ShadowBuffer=0D + );=0D + if (EFI_ERROR (Status)) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D =0D - // Merge old and new data. Write merged word to NOR=0D - WordToWrite =3D (Tmp & ~Mask) | TmpBuf;=0D - BytesToWrite -=3D (4 - (CurOffset & 0x3));=0D - CurOffset +=3D (4 - (CurOffset & 0x3));=0D - } else {=0D - // Unaligned and fits in one word.=0D - Mask =3D (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) *= 8);=0D - // Mask out the bytes we want.=0D - TmpBuf =3D (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;=0D - // Is the destination still in 'erased' state?=0D - if ((Tmp & Mask) !=3D Mask) {=0D - // Check to see if we are only changing bits to zero.=0D - if ((Tmp ^ TmpBuf) & TmpBuf) {=0D - DoErase =3D TRUE;=0D - break;=0D - }=0D - }=0D + // Make OrigData point to the start of the old version of the data ins= ide=0D + // the word aligned buffer=0D + OrigData =3D Instance->ShadowBuffer + (Offset & BOUNDARY_OF_32_WORDS);= =0D =0D - // Merge old and new data. Write merged word to NOR=0D - WordToWrite =3D (Tmp & ~Mask) | TmpBuf;=0D - CurOffset +=3D BytesToWrite;=0D - BytesToWrite =3D 0;=0D - }=0D + // Update the buffer containing the old version of the data with the n= ew=0D + // contents, while checking whether the old version had any bits clear= ed=0D + // that we want to set. In that case, we will need to erase the block = first.=0D + for (CurOffset =3D 0; CurOffset < *NumBytes; CurOffset++) {=0D + if (~OrigData[CurOffset] & Buffer[CurOffset]) {=0D + goto DoErase;=0D }=0D + OrigData[CurOffset] =3D Buffer[CurOffset];=0D + }=0D =0D - //=0D - // Write the word to NOR.=0D - //=0D -=0D - BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,= Lba, BlockSize);=0D - if (BlockAddress !=3D PrevBlockAddress) {=0D - TempStatus =3D NorFlashUnlockSingleBlockIfNecessary (Instance, Blo= ckAddress);=0D - if (EFI_ERROR (TempStatus)) {=0D - return EFI_DEVICE_ERROR;=0D - }=0D + //=0D + // Write the updated buffer to NOR.=0D + //=0D + BlockAddress =3D GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, L= ba, BlockSize);=0D =0D - PrevBlockAddress =3D BlockAddress;=0D - }=0D + // Unlock the block if we have to=0D + Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddres= s);=0D + if (EFI_ERROR (Status)) {=0D + goto Exit;=0D + }=0D =0D - TempStatus =3D NorFlashWriteSingleWord (Instance, WordAddr, WordToWr= ite);=0D - // Put device back into Read Array mode=0D - SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY= );=0D - if (EFI_ERROR (TempStatus)) {=0D - return EFI_DEVICE_ERROR;=0D - }=0D + Status =3D NorFlashWriteBuffer (=0D + Instance,=0D + BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS),=0D + P30_MAX_BUFFER_SIZE_IN_BYTES,=0D + Instance->ShadowBuffer);=0D + if (EFI_ERROR (Status)) {=0D + goto Exit;=0D }=0D =0D - // Exit if we got here and could write all the data. Otherwise do the= =0D - // Erase-Write cycle.=0D - if (!DoErase) {=0D - return EFI_SUCCESS;=0D + if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) > P30_MAX_BUFFER_SIZ= E_IN_BYTES) {=0D + BlockAddress +=3D P30_MAX_BUFFER_SIZE_IN_BYTES;=0D + Status =3D NorFlashWriteBuffer (=0D + Instance,=0D + BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS),=0D + P30_MAX_BUFFER_SIZE_IN_BYTES,=0D + Instance->ShadowBuffer + P30_MAX_BUFFER_SIZE_IN_BYTES);=0D }=0D - }=0D =0D - // Check we did get some memory. Buffer is BlockSize.=0D - if (Instance->ShadowBuffer =3D=3D NULL) {=0D - DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));=0D - return EFI_DEVICE_ERROR;=0D +Exit:=0D + // Put device back into Read Array mode=0D + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);= =0D +=0D + return Status;=0D }=0D =0D +DoErase:=0D // Read NOR Flash data into shadow buffer=0D - TempStatus =3D NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->S= hadowBuffer);=0D - if (EFI_ERROR (TempStatus)) {=0D + Status =3D NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->Shado= wBuffer);=0D + if (EFI_ERROR (Status)) {=0D // Return one of the pre-approved error statuses=0D return EFI_DEVICE_ERROR;=0D }=0D @@ -762,8 +696,8 @@ NorFlashWriteSingleBlock ( CopyMem ((VOID *)((UINTN)Instance->ShadowBuffer + Offset), Buffer, *NumB= ytes);=0D =0D // Write the modified buffer back to the NorFlash=0D - TempStatus =3D NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->= ShadowBuffer);=0D - if (EFI_ERROR (TempStatus)) {=0D + Status =3D NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->Shad= owBuffer);=0D + if (EFI_ERROR (Status)) {=0D // Return one of the pre-approved error statuses=0D return EFI_DEVICE_ERROR;=0D }=0D --=20 2.35.1