From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by mx.groups.io with SMTP id smtpd.web10.18675.1665571598677851748 for ; Wed, 12 Oct 2022 03:46:38 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ventanamicro.com header.s=google header.b=PsnR8qgK; spf=pass (domain: ventanamicro.com, ip: 209.85.216.51, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pj1-f51.google.com with SMTP id o17-20020a17090aac1100b0020d98b0c0f4so251202pjq.4 for ; Wed, 12 Oct 2022 03:46:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=v9X/I40svFNLdc8O50eNXWTQ7+K5gA+d+UnT770ivxQ=; b=PsnR8qgKWwUlyxc7jY4/OotiDqICvZcpJo/Yj2NB61Nwg5zhkG23+kWFv72UySWl5n cFRsTqxt/ZgZVkz/pGvGBSnEaL4T02TJ/sPdDWLYpj/gPcsa98HmRZBTVzfgpZdJzopD 9iL5hWTO0C1wEPmpaXP6aqNYETbweAx/nL3NDyKZcVs1MhofsY37+YQNd+B7omQ/QqOV G/z2m0mBYwz4O72zyEQqBES/5qMWbzvG29YikOzI38+FeHemM8nNN7dc4EYggxsMWwZo XQ5NArBKR+d2/Xag4iWFeFBNNTDr+zSyQYOo3cfEVHD9S38ciqouFl/KGhuCjNiUXzzj NYaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v9X/I40svFNLdc8O50eNXWTQ7+K5gA+d+UnT770ivxQ=; b=IffCjQrG1f4SgDnW5Fb8F8tAF5EQSwX6Xrlv2pl0wd31lmg5SDjBr56m71BVqM360y nog6tTe5/wSO3+FQnkZrRr/cFNH/7Je0PpF+ClFubQZ45y3fS/2HJeT18TmOi8li9ttk zIwsUjQ8cHkyNLcc0fRml7Le/O9LecyVdbnk54TR7OeNdJSnbsatUD3hAg7WbYaFVZzg UPZqQ+xuDdZJzu+vB4u+LsEVS7RQU4XgFtbn9duWXIeXm4vuOZGT9Rn4tq6aDqngSk0g uq8lhfmjljq1ga2oW9P55k0DIyHD3K3cGhuh66vXeAIlkGuJDghkWmkueDZjo1PWiHp6 8aXA== X-Gm-Message-State: ACrzQf0YQZ7zyr8YBXQmBz6Fst4tnKvIhNOXVt0cvkBP5U37y0O08yhL pyQDZh+E0E2YgvaXsq2RDw24SOsT1fhaBA== X-Google-Smtp-Source: AMsMyM6UF1IYsbThREHtE5oQhglQgiq1rtzgumA1vZFggoeBXn7PG07PO3V83Aa20z1DBl75W/8K+Q== X-Received: by 2002:a17:902:d34a:b0:17f:9be0:cdf9 with SMTP id l10-20020a170902d34a00b0017f9be0cdf9mr28747308plk.132.1665571597905; Wed, 12 Oct 2022 03:46:37 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([49.206.13.138]) by smtp.gmail.com with ESMTPSA id z4-20020aa79e44000000b0054223a0185asm10812221pfq.161.2022.10.12.03.46.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 03:46:37 -0700 (PDT) From: "Sunil V L" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Leif Lindholm , Jiewen Yao , Jordan Justen , Gerd Hoffmann Subject: [edk2-staging/RiscV64QemuVirt PATCH V2 30/33] OvmfPkg/NorFlashDxe: Avoid switching between modes in a tight loop Date: Wed, 12 Oct 2022 16:14:53 +0530 Message-Id: <20221012104456.1393376-31-sunilvl@ventanamicro.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221012104456.1393376-1-sunilvl@ventanamicro.com> References: <20221012104456.1393376-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Signed-off-by: Ard Biesheuvel --- OvmfPkg/Drivers/NorFlashDxe/NorFlash.c | 211 +++++++++---------------- 1 file changed, 75 insertions(+), 136 deletions(-) diff --git a/OvmfPkg/Drivers/NorFlashDxe/NorFlash.c b/OvmfPkg/Drivers/NorFlashDxe/NorFlash.c index cdc6b5da8bfb..649e0789db3c 100644 --- a/OvmfPkg/Drivers/NorFlashDxe/NorFlash.c +++ b/OvmfPkg/Drivers/NorFlashDxe/NorFlash.c @@ -582,20 +582,11 @@ NorFlashWriteSingleBlock ( IN UINT8 *Buffer ) { - EFI_STATUS TempStatus; - UINT32 Tmp; - UINT32 TmpBuf; - UINT32 WordToWrite; - UINT32 Mask; - BOOLEAN DoErase; - UINTN BytesToWrite; + EFI_STATUS Status; UINTN CurOffset; - UINTN WordAddr; UINTN BlockSize; UINTN BlockAddress; - UINTN PrevBlockAddress; - - PrevBlockAddress = 0; + UINT8 *OrigData; DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer)); @@ -606,6 +597,12 @@ NorFlashWriteSingleBlock ( return EFI_ACCESS_DENIED; } + // Check we did get some memory. Buffer is BlockSize. + if (Instance->ShadowBuffer == NULL) { + DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n")); + return EFI_DEVICE_ERROR; + } + // Cache the block size to avoid de-referencing pointers all the time BlockSize = Instance->Media.BlockSize; @@ -625,149 +622,91 @@ NorFlashWriteSingleBlock ( return EFI_BAD_BUFFER_SIZE; } - // Pick 128bytes as a good start for word operations as opposed to erasing the - // block and writing the data regardless if an erase is really needed. - // It looks like most individual NV variable writes are smaller than 128bytes. - if (*NumBytes <= 128) { + // Pick P30_MAX_BUFFER_SIZE_IN_BYTES (== 128 bytes) as a good start for word + // operations as opposed to erasing the block and writing the data regardless + // if an erase is really needed. It looks like most individual NV variable + // writes are smaller than 128 bytes. + // To avoid pathological cases were a 2 byte write is disregarded because it + // occurs right at a 128 byte buffered write alignment boundary, permit up to + // twice the max buffer size, and perform two writes if needed. + if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) <= (2 * P30_MAX_BUFFER_SIZE_IN_BYTES)) { // Check to see if we need to erase before programming the data into NOR. // If the destination bits are only changing from 1s to 0s we can just write. // After a block is erased all bits in the block is set to 1. // If any byte requires us to erase we just give up and rewrite all of it. - DoErase = FALSE; - BytesToWrite = *NumBytes; - CurOffset = Offset; - while (BytesToWrite > 0) { - // Read full word from NOR, splice as required. A word is the smallest - // unit we can write. - TempStatus = NorFlashRead (Instance, Lba, CurOffset & ~(0x3), sizeof (Tmp), &Tmp); - if (EFI_ERROR (TempStatus)) { - return EFI_DEVICE_ERROR; - } - - // Physical address of word in NOR to write. - WordAddr = (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS ( - Instance->RegionBaseAddress, - Lba, - BlockSize - ); - // The word of data that is to be written. - TmpBuf = *((UINT32 *)(Buffer + (*NumBytes - BytesToWrite))); - - // First do word aligned chunks. - if ((CurOffset & 0x3) == 0) { - if (BytesToWrite >= 4) { - // Is the destination still in 'erased' state? - if (~Tmp != 0) { - // Check to see if we are only changing bits to zero. - if ((Tmp ^ TmpBuf) & TmpBuf) { - DoErase = TRUE; - break; - } - } - - // Write this word to NOR - WordToWrite = TmpBuf; - CurOffset += sizeof (TmpBuf); - BytesToWrite -= sizeof (TmpBuf); - } else { - // BytesToWrite < 4. Do small writes and left-overs - Mask = ~((~0) << (BytesToWrite * 8)); - // Mask out the bytes we want. - TmpBuf &= Mask; - // Is the destination still in 'erased' state? - if ((Tmp & Mask) != Mask) { - // Check to see if we are only changing bits to zero. - if ((Tmp ^ TmpBuf) & TmpBuf) { - DoErase = TRUE; - break; - } - } - - // Merge old and new data. Write merged word to NOR - WordToWrite = (Tmp & ~Mask) | TmpBuf; - CurOffset += BytesToWrite; - BytesToWrite = 0; - } - } else { - // Do multiple words, but starting unaligned. - if (BytesToWrite > (4 - (CurOffset & 0x3))) { - Mask = ((~0) << ((CurOffset & 0x3) * 8)); - // Mask out the bytes we want. - TmpBuf &= Mask; - // Is the destination still in 'erased' state? - if ((Tmp & Mask) != Mask) { - // Check to see if we are only changing bits to zero. - if ((Tmp ^ TmpBuf) & TmpBuf) { - DoErase = TRUE; - break; - } - } + // Read the old version of the data into the shadow buffer + Status = NorFlashRead ( + Instance, + Lba, + Offset & ~BOUNDARY_OF_32_WORDS, + (*NumBytes | BOUNDARY_OF_32_WORDS) + 1, + Instance->ShadowBuffer + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } - // Merge old and new data. Write merged word to NOR - WordToWrite = (Tmp & ~Mask) | TmpBuf; - BytesToWrite -= (4 - (CurOffset & 0x3)); - CurOffset += (4 - (CurOffset & 0x3)); - } else { - // Unaligned and fits in one word. - Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8); - // Mask out the bytes we want. - TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask; - // Is the destination still in 'erased' state? - if ((Tmp & Mask) != Mask) { - // Check to see if we are only changing bits to zero. - if ((Tmp ^ TmpBuf) & TmpBuf) { - DoErase = TRUE; - break; - } - } + // Make OrigData point to the start of the old version of the data inside + // the word aligned buffer + OrigData = Instance->ShadowBuffer + (Offset & BOUNDARY_OF_32_WORDS); - // Merge old and new data. Write merged word to NOR - WordToWrite = (Tmp & ~Mask) | TmpBuf; - CurOffset += BytesToWrite; - BytesToWrite = 0; - } + // Update the buffer containing the old version of the data with the new + // contents, while checking whether the old version had any bits cleared + // that we want to set. In that case, we will need to erase the block first. + for (CurOffset = 0; CurOffset < *NumBytes; CurOffset++) { + if (~OrigData[CurOffset] & Buffer[CurOffset]) { + goto DoErase; } - // - // Write the word to NOR. - // + OrigData[CurOffset] = Buffer[CurOffset]; + } - BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize); - if (BlockAddress != PrevBlockAddress) { - TempStatus = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress); - if (EFI_ERROR (TempStatus)) { - return EFI_DEVICE_ERROR; - } + // + // Write the updated buffer to NOR. + // + BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize); - PrevBlockAddress = BlockAddress; - } + // Unlock the block if we have to + Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress); + if (EFI_ERROR (Status)) { + goto Exit; + } - TempStatus = NorFlashWriteSingleWord (Instance, WordAddr, WordToWrite); - // Put device back into Read Array mode - SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY); + Status = NorFlashWriteBuffer ( + Instance, + BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS), + P30_MAX_BUFFER_SIZE_IN_BYTES, + Instance->ShadowBuffer + ); + if (EFI_ERROR (Status)) { + goto Exit; + } - if (EFI_ERROR (TempStatus)) { - return EFI_DEVICE_ERROR; + if ((*NumBytes + (Offset & BOUNDARY_OF_32_WORDS)) > P30_MAX_BUFFER_SIZE_IN_BYTES) { + BlockAddress += P30_MAX_BUFFER_SIZE_IN_BYTES; + Status = NorFlashWriteBuffer ( + Instance, + BlockAddress + (Offset & ~BOUNDARY_OF_32_WORDS), + P30_MAX_BUFFER_SIZE_IN_BYTES, + Instance->ShadowBuffer + P30_MAX_BUFFER_SIZE_IN_BYTES + ); + if (EFI_ERROR (Status)) { + goto Exit; } } - // Exit if we got here and could write all the data. Otherwise do the - // Erase-Write cycle. - if (!DoErase) { - return EFI_SUCCESS; - } - } +Exit: + // Put device back into Read Array mode + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY); - // Check we did get some memory. Buffer is BlockSize. - if (Instance->ShadowBuffer == NULL) { - DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n")); - return EFI_DEVICE_ERROR; + return Status; } +DoErase: // Read NOR Flash data into shadow buffer - TempStatus = NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer); - if (EFI_ERROR (TempStatus)) { + Status = NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer); + if (EFI_ERROR (Status)) { // Return one of the pre-approved error statuses return EFI_DEVICE_ERROR; } @@ -776,8 +715,8 @@ NorFlashWriteSingleBlock ( CopyMem ((VOID *)((UINTN)Instance->ShadowBuffer + Offset), Buffer, *NumBytes); // Write the modified buffer back to the NorFlash - TempStatus = NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer); - if (EFI_ERROR (TempStatus)) { + Status = NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer); + if (EFI_ERROR (Status)) { // Return one of the pre-approved error statuses return EFI_DEVICE_ERROR; } -- 2.25.1