qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Christian Speich <c.speich@avm.de>
To: qemu-devel@nongnu.org
Cc: "Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Bin Meng" <bmeng.cn@gmail.com>,
	qemu-block@nongnu.org, "Christian Speich" <c.speich@avm.de>
Subject: [PATCH 4/4] hw/sd/sdcard: Erase in large blocks
Date: Fri, 19 Sep 2025 14:34:43 +0200	[thread overview]
Message-ID: <20250919-sdcard-performance-b4-v1-4-e1037e481a19@avm.de> (raw)
In-Reply-To: <20250919-sdcard-performance-b4-v1-0-e1037e481a19@avm.de>

Erasing each block individually is slow, so this patch reworks the logic
to erase as much as possible in one go.

Signed-off-by: Christian Speich <c.speich@avm.de>
---
 hw/sd/sd.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 94ef3cc62582717ee044c4b114b7f22bd1b4a256..42870fa19414be61e43d2e07619ed193cc514319 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1086,7 +1086,6 @@ static void sd_erase(SDState *sd)
     bool sdsc = true;
     uint64_t wpnum;
     uint64_t erase_addr;
-    int erase_len = 1 << HWBLOCK_SHIFT;
 
     trace_sdcard_erase(sd->erase_start, sd->erase_end);
     if (sd->erase_start == INVALID_ADDRESS
@@ -1115,19 +1114,26 @@ static void sd_erase(SDState *sd)
     sd->erase_end = INVALID_ADDRESS;
     sd->csd[14] |= 0x40;
 
-    for (erase_addr = erase_start; erase_addr <= erase_end;
-         erase_addr += erase_len) {
-        if (sdsc) {
-            /* Only SDSC cards support write protect groups */
+    /* Only SDSC cards support write protect groups */
+    if (sdsc) {
+        for (erase_addr = erase_start; erase_addr <= erase_end;
+             erase_addr = ROUND_UP(erase_addr + 1, WPGROUP_SIZE)) {
+            uint64_t wp_group_end = ROUND_UP(erase_addr + 1, WPGROUP_SIZE) - 1;
+            size_t to_erase = MIN(erase_end, wp_group_end) - erase_addr;
+
             wpnum = sd_addr_to_wpnum(erase_addr);
             assert(wpnum < sd->wp_group_bits);
             if (test_bit(wpnum, sd->wp_group_bmap)) {
                 sd->card_status |= WP_ERASE_SKIP;
                 continue;
             }
+
+            blk_pwrite_zeroes(sd->blk, erase_addr + sd_part_offset(sd),
+                              to_erase, 0);
         }
-        blk_pwrite_zeroes(sd->blk, erase_addr + sd_part_offset(sd),
-                          erase_len, 0);
+    } else {
+        blk_pwrite_zeroes(sd->blk, erase_start + sd_part_offset(sd),
+                          erase_end - erase_start, 0);
     }
 }
 

-- 
2.43.0



  parent reply	other threads:[~2025-09-19 13:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-19 12:34 [PATCH 0/4] hw/sd: Improve performance of read/write/erase Christian Speich
2025-09-19 12:34 ` [PATCH 1/4] hw/sd: Switch from byte-wise to buf+len read/writes Christian Speich
2025-09-19 12:34 ` [PATCH 2/4] hw/sd/sdhci: Don't use bounce buffer for ADMA Christian Speich
2025-09-19 12:34 ` [PATCH 3/4] hw/sd/sdcard: Erase blocks to zero Christian Speich
2025-11-24  4:09   ` Philippe Mathieu-Daudé
2025-11-25  9:47     ` Christian Speich
2025-09-19 12:34 ` Christian Speich [this message]
2025-11-07  9:08 ` [PATCH 0/4] hw/sd: Improve performance of read/write/erase Christian Speich
2025-11-24  4:05   ` Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250919-sdcard-performance-b4-v1-4-e1037e481a19@avm.de \
    --to=c.speich@avm.de \
    --cc=bmeng.cn@gmail.com \
    --cc=philmd@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).