hw/sd: Improve performance of read/write/erase
cover hw/sd: Improve performance of read/write/erase
This patch series improves the performance of read/write/erase operations on sdcards.
This is done by increasing the maximum buffer size that is worked on. From 1 byte (master) to 512 bytes (first commit) to larger than 512 (adma commit).
Testing on my system with fio I see the following rough performance values in MiB/s.
read write readwrite
master: 6 6 3/ 3
first commit: 51 43 23/ 23
second commit: 392 180 144/143
Tested on a 2GiB raw image with:
fio --filename=/dev/mmcblk0 --direct=1 --runtime=60 --time_based --bs=128k --rw={mode}
The adma values are somewhat unstable but always >100MiB/s, I'm not sure why but I guess it has something to do with the host side caching.
For erasing the third commit changes the erase operation to write zeros, as indicated by DATA_STAT_AFTER_ERASE in SCR.
The fourth commit allows erasure in large blocks, to speed it up significantly. Erasing 2GiB now takes 0.1s instead of 26s.
---
Christian Speich (4):
hw/sd: Switch from byte-wise to buf+len read/writes hw/sd/sdhci: Don't use bounce buffer for ADMA hw/sd/sdcard: Erase blocks to zero hw/sd/sdcard: Erase in large blocks
hw/sd/core.c | 16 +--- hw/sd/sd.c | 277 ++++++++++++++++++++++++++++++++++++++++------------- hw/sd/sdhci.c | 102 +++++++++++--------- include/hw/sd/sd.h | 13 +-- 4 files changed, 277 insertions(+), 131 deletions(-)
---
base-commit: e7c1e8043a69c5a8efa39d4f9d111f7c72c076e6
change-id: 20250912-sdcard-performance-b4-d908bbb5a004
Best regards,
Signature · 1 line
Christian Speich <[email protected]>
PATCH 1/4 hw/sd: Switch from byte-wise to buf+len read/writes
Currently, each read/write is eventually split into individual bytes and then later reconstructed into blocks. Byte-wise operation is always bad for performance.
This patch switches from the read_byte/write_byte primitives to read_data/write_data which take a buf + length that should be filled. Byte-wise operation is still supported via sdbus_{read,write}_byte.
As before, arbitrarily sized and aligned data can be passed. The new primitives try to pass the largest possible buffer down to the block layer. This enhances speed and if possible avoids the local bounce buffer.
--- hw/sd/core.c | 16 +--- hw/sd/sd.c | 257 +++++++++++++++++++++++++++++++++++++++++------------ include/hw/sd/sd.h | 13 +-- 3 files changed, 209 insertions(+), 77 deletions(-)
hw/sd/core.c +4 -12
@@ -113,22 +113,18 @@ void sdbus_write_byte(SDBus *sdbus, uint8_t value)if (card) {SDCardClass *sc =SDMMC_COMMON_GET_CLASS (card); - sc->write_byte (card, value); + sc->write_data (card, &value,1 ); } }void sdbus_write_data (SDBus *sdbus,const void *buf,size_t length) {SDState *card =get_card (sdbus); -const uint8_t *data = buf;if (card) {SDCardClass *sc =SDMMC_COMMON_GET_CLASS (card); -for (size_t i =0 ; i < length; i++) { -trace_sdbus_write (sdbus_name (sdbus), data[i]); - sc->write_byte (card, data[i]); - } + sc->write_data (card, buf, length); } }
@@ -140,7 +136,7 @@ uint8_t sdbus_read_byte(SDBus *sdbus)if (card) {SDCardClass *sc =SDMMC_COMMON_GET_CLASS (card); - value = sc->read_byte (card); + sc->read_data (card, &value,1 ); }trace_sdbus_read (sdbus_name (sdbus), value);
@@ -150,15 +146,11 @@ uint8_t sdbus_read_byte(SDBus *sdbus)void sdbus_read_data (SDBus *sdbus,void *buf,size_t length) {SDState *card =get_card (sdbus); -uint8_t *data = buf;if (card) {SDCardClass *sc =SDMMC_COMMON_GET_CLASS (card); -for (size_t i =0 ; i < length; i++) { - data[i] = sc->read_byte (card); -trace_sdbus_read (sdbus_name (sdbus), data[i]); - } + sc->read_data (card, buf, length); } }
hw/sd/sd.c +198 -59
412 lines not on this page · raw message
include/hw/sd/sd.h +7 -6
@@ -107,22 +107,23 @@ struct SDCardClass {size_t (*do_command)(SDState *sd,SDRequest *req,uint8_t *resp,size_t respsz);/** -* Write a byte to a SD card. +* Write data to a SD card. * @sd: card -* @value: byte to write +* @value: data to write * * Write a byte on the data lines of a SD card. */ -void (*write_byte)(SDState *sd,uint8_t value); +void (*write_data)(SDState *sd,const void * buf,size_t len);/** -* Read a byte from a SD card. +* Read data from a SD card. * @sd: card * -* Read a byte from the data lines of a SD card. +* Read data from the data lines of a SD card. The requestes length is +* always filled even if an error occours. * * Return: byte value read */ -uint8_t (*read_byte)(SDState *sd); +void (*read_data)(SDState *sd,void * buf,size_t len);bool (*receive_ready)(SDState *sd);bool (*data_ready)(SDState *sd);void (*set_voltage)(SDState *sd,uint16_t millivolts);
Signature · 1 line
2.43.0
PATCH 2/4 hw/sd/sdhci: Don't use bounce buffer for ADMA
In reply to Christian SpeichCurrently, ADMA will temporarily store data into a local bounce buffer when transferring it. This will produce unneeded copies of the data and limit us to the bounce buffer size for each step.
This patch now maps the requested DMA address and passes this buffer directly to sdbus_{read,write}_data. This allows to pass much larger buffers down to increase the performance. sdbus_{read,write}_data is already able to handle arbitrary length and alignments, so we do not need to ensure this.
--- hw/sd/sdhci.c | 102 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 47 deletions(-)
hw/sd/sdhci.c +55 -47
@@ -774,7 +774,7 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr)static void sdhci_do_adma (SDHCIState *s) { -unsigned int begin, length; +unsigned int length;const uint16_t block_size = s->blksize &BLOCK_SIZE_MASK ;const MemTxAttrs attrs = { .memory = true };ADMADescr dscr = {};
@@ -816,66 +816,74 @@ static void sdhci_do_adma(SDHCIState *s)if (s->trnmod &SDHC_TRNS_READ ) { s->prnsts |=SDHC_DOING_READ ;while (length) { -if (s->data_count ==0 ) { -sdbus_read_data (&s->sdbus, s->fifo_buffer, block_size); - } - begin = s->data_count; -if ((length + begin) < block_size) { - s->data_count = length + begin; - length =0 ; - }else { - s->data_count = block_size; - length -= block_size - begin; - } - res =dma_memory_write (s->dma_as, dscr.addr, - &s->fifo_buffer[begin], - s->data_count - begin, - attrs); -if (res !=MEMTX_OK ) { +dma_addr_t dma_len = length; + +void *buf =dma_memory_map (s->dma_as, dscr.addr, &dma_len, +DMA_DIRECTION_FROM_DEVICE , + attrs); + +if (buf ==NULL ) { + res =MEMTX_ERROR ;break ; + }else { + res =MEMTX_OK ; } - dscr.addr += s->data_count - begin; -if (s->data_count == block_size) { - s->data_count =0 ; -if (s->trnmod &SDHC_TRNS_BLK_CNT_EN ) { - s->blkcnt--; -if (s->blkcnt ==0 ) { -break ; - } + +sdbus_read_data (&s->sdbus, buf, dma_len); + length -= dma_len; + dscr.addr += dma_len; + +dma_memory_unmap (s->dma_as, buf, dma_len, +DMA_DIRECTION_FROM_DEVICE , dma_len); + +if (s->trnmod &SDHC_TRNS_BLK_CNT_EN ) { +size_t transfered = s->data_count + dma_len; + + s->blkcnt -= transfered / block_size; + s->data_count = transfered % block_size; + +if (s->blkcnt ==0 ) { + s->data_count =0 ; +break ; } } } }else { s->prnsts |=SDHC_DOING_WRITE ;while (length) { - begin = s->data_count; -if ((length + begin) < block_size) { - s->data_count = length + begin; - length =0 ; - }else { - s->data_count = block_size; - length -= block_size - begin; - } - res =dma_memory_read (s->dma_as, dscr.addr, - &s->fifo_buffer[begin], - s->data_count - begin, - attrs); -if (res !=MEMTX_OK ) { +dma_addr_t dma_len = length; + +void *buf =dma_memory_map (s->dma_as, dscr.addr, &dma_len, +DMA_DIRECTION_TO_DEVICE , attrs); + +if (buf ==NULL ) { + res =MEMTX_ERROR ;break ; + }else { + res =MEMTX_OK ; } - dscr.addr += s->data_count - begin; -if (s->data_count == block_size) { -sdbus_write_data (&s->sdbus, s->fifo_buffer, block_size); - s->data_count =0 ; -if (s->trnmod &SDHC_TRNS_BLK_CNT_EN ) { - s->blkcnt--; -if (s->blkcnt ==0 ) { -break ; - } + +sdbus_write_data (&s->sdbus, buf, dma_len); + length -= dma_len; + dscr.addr += dma_len; + +dma_memory_unmap (s->dma_as, buf, dma_len, +DMA_DIRECTION_TO_DEVICE , dma_len); + +if (s->trnmod &SDHC_TRNS_BLK_CNT_EN ) { +size_t transfered = s->data_count + dma_len; + + s->blkcnt -= transfered / block_size; + s->data_count = transfered % block_size; + +if (s->blkcnt ==0 ) { + s->data_count =0 ; +break ; } } } } +if (res !=MEMTX_OK ) { s->data_count =0 ;if (s->errintstsen &SDHC_EISEN_ADMAERR ) {
Signature · 1 line
2.43.0
PATCH 3/4 hw/sd/sdcard: Erase blocks to zero
In reply to Christian SpeichCurrently, erased blocks are filled with 0xFF. However SCR Bit 55 (DATA_STAT_AFTER_ERASE) indicates that an erase produces zeros. One of them is wrong.
As erasing to zero is more performant and allows block devices to use optimizations, we the erase to produce 0x00.
--- hw/sd/sd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/sd/sd.c +2 -2
@@ -1115,7 +1115,6 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -memset (sd->data,0xff , erase_len);for (erase_addr = erase_start; erase_addr <= erase_end; erase_addr += erase_len) {if (sdsc) {
@@ -1127,7 +1126,8 @@ static void sd_erase(SDState *sd)continue ; } } -sd_blk_write (sd, erase_addr, erase_len); +blk_pwrite_zeroes (sd->blk, erase_addr +sd_part_offset (sd), + erase_len,0 ); } }
Signature · 1 line
2.43.0
Re: [PATCH 3/4] hw/sd/sdcard: Erase blocks to zero
Quoted 3 lines · Christian Speich
Currently, erased blocks are filled with
0xFF. However SCR Bit 55 (DATA_STAT_AFTER_ERASE) indicates that an erase produces zeros. One of them is wrong.
You are right, we don't set DATA_STAT_AFTER_ERASE correctly.
Quoted 21 lines
As erasing to zero is more performant and allows block devices to use optimizations, we the erase to produce
0x00.Signed-off-by Christian Speich <[email protected]>--- hw/sd/sd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/sd/sd.c +0
-1@@ -1115,7 +1115,6 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -memset (sd->data,0xff , erase_len);for (erase_addr = erase_start; erase_addr <= erase_end; erase_addr += erase_len) {if (sdsc) {
hw/sd/sd.c static void sd_erase(SDState *sd) · line 1132
Quoted 8 lines
hw/sd/sd.c +2
-1@@ -1127,7 +1126,8 @@ static void sd_erase(SDState *sd)continue ; } } -sd_blk_write (sd, erase_addr, erase_len); +blk_pwrite_zeroes (sd->blk, erase_addr +sd_part_offset (sd), + erase_len,0 ); } }
I'm OK with this change, but I'd rather having a device boolean property so we can keep the old behavior for backward compatibility. Maybe 'erase-block-as-zero'? Do you mind updating this patch?
Regards,
Phil.
Re: [PATCH 3/4] hw/sd/sdcard: Erase blocks to zero
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Christian Speich
Currently, erased blocks are filled with
0xFF. However SCR Bit 55 (DATA_STAT_AFTER_ERASE) indicates that an erase produces zeros. One of them is wrong.You are right, we don't set
DATA_STAT_AFTER_ERASEcorrectly.Quoted 24 lines
As erasing to zero is more performant and allows block devices to use optimizations, we the erase to produce
0x00.Signed-off-by Christian Speich <[email protected]>--- hw/sd/sd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/sd/sd.c +0
-2@@ -1115,7 +1115,6 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -memset (sd->data,0xff , erase_len);for (erase_addr = erase_start; erase_addr <= erase_end; erase_addr += erase_len) {if (sdsc) {@@ -1127,7 +1126,8 @@ static void sd_erase(SDState *sd)continue ; } } -sd_blk_write (sd, erase_addr, erase_len);
Quoted 8 lines
hw/sd/sd.c static void sd_erase(SDState *sd) · line 1132
Quoted 4 lines
hw/sd/sd.c +2
-0+blk_pwrite_zeroes (sd->blk, erase_addr +sd_part_offset (sd), + erase_len,0 ); } }I'm OK with this change, but I'd rather having a device boolean property so we can keep the old behavior for backward compatibility. Maybe 'erase-block-as-zero'? Do you mind updating this patch?
I've refrained from making this a user-configruable property to keep the code simple and as erasing to zero can be better optimized (performance and disk usage, e.g. the following commit). Additionally, it may be less confusing to users as new disks created by qemu-img will be filled with zeros.
I see that this is a breaking change though, so I'll look into adding a property.
I assume you want erase-block-as-zero to default to false? (I'd prefer true, but thats just my use-case).
Greetings,
Christian
Quoted 5 lines
Regards,
Phil.
PATCH 4/4 hw/sd/sdcard: Erase in large blocks
In reply to Christian SpeichErasing each block individually is slow, so this patch reworks the logic to erase as much as possible in one go.
--- hw/sd/sd.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
hw/sd/sd.c +13 -7
@@ -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 ); } }
Signature · 1 line
2.43.0
ping, I've not received any reaction on this series[1]. Is there anything I can do to move this forward?
Greetings,
Christian
[1]: https://patchew.org/QEMU/[email protected]/
Quoted 49 lines · Christian Speich
This patch series improves the performance of read/write/erase operations on sdcards.
This is done by increasing the maximum buffer size that is worked on.
Quoted 1 line
From 1 byte (master) to 512 bytes (first commit) to larger than 512
(adma commit).
Testing on my system with fio I see the following rough performance values in MiB/s.
read write readwrite
master: 6 6 3/ 3
first commit: 51 43 23/ 23
second commit: 392 180 144/143Tested on a 2GiB raw image with:
fio --filename=/dev/mmcblk0 --direct=1 --runtime=60 --time_based --bs=128k --rw={mode}The adma values are somewhat unstable but always >100MiB/s, I'm not sure why but I guess it has something to do with the host side caching.
For erasing the third commit changes the erase operation to write zeros, as indicated by
DATA_STAT_AFTER_ERASEin SCR.The fourth commit allows erasure in large blocks, to speed it up significantly. Erasing 2GiB now takes 0.1s instead of 26s.
Signed-off-by Christian Speich <[email protected]>---
Christian Speich (4):
hw/sd: Switch from byte-wise to buf+len read/writes hw/sd/sdhci: Don't use bounce buffer for ADMA hw/sd/sdcard: Erase blocks to zero hw/sd/sdcard: Erase in large blockshw/sd/core.c | 16 +--- hw/sd/sd.c | 277 ++++++++++++++++++++++++++++++++++++++++------------- hw/sd/sdhci.c | 102 +++++++++++--------- include/hw/sd/sd.h | 13 +-- 4 files changed, 277 insertions(+), 131 deletions(-)---
base-commit:e7c1e8043a69c5a8efa39d4f9d111f7c72c076e6
change-id: 20250912-sdcard-performance-b4-d908bbb5a004Best regards,
Signature · 1 line
Christian Speich <[email protected]>
Hi Christian,
Quoted 2 lines · Christian Speich
ping, I've not received any reaction on this series[1]. Is there anything I can do to move this forward?
My apologies for missing this... I was in PTO when you posted and then it felt into my INBOX cracks :/
Quoted 50 lines · Christian Speich
Quoted 49 lines
This patch series improves the performance of read/write/erase operations on sdcards.
This is done by increasing the maximum buffer size that is worked on.
Quoted 1 line
From 1 byte (master) to 512 bytes (first commit) to larger than 512
(adma commit).
Testing on my system with fio I see the following rough performance values in MiB/s.
read write readwrite
master: 6 6 3/ 3
first commit: 51 43 23/ 23
second commit: 392 180 144/143Tested on a 2GiB raw image with:
fio --filename=/dev/mmcblk0 --direct=1 --runtime=60 --time_based --bs=128k --rw={mode}The adma values are somewhat unstable but always >100MiB/s, I'm not sure why but I guess it has something to do with the host side caching.
For erasing the third commit changes the erase operation to write zeros, as indicated by
DATA_STAT_AFTER_ERASEin SCR.The fourth commit allows erasure in large blocks, to speed it up significantly. Erasing 2GiB now takes 0.1s instead of 26s.
Signed-off-by Christian Speich <[email protected]>---
Christian Speich (4):
hw/sd: Switch from byte-wise to buf+len read/writes hw/sd/sdhci: Don't use bounce buffer for ADMA hw/sd/sdcard: Erase blocks to zero hw/sd/sdcard: Erase in large blockshw/sd/core.c | 16 +--- hw/sd/sd.c | 277 ++++++++++++++++++++++++++++++++++++++++------------- hw/sd/sdhci.c | 102 +++++++++++--------- include/hw/sd/sd.h | 13 +-- 4 files changed, 277 insertions(+), 131 deletions(-)---
base-commit:e7c1e8043a69c5a8efa39d4f9d111f7c72c076e6
change-id: 20250912-sdcard-performance-b4-d908bbb5a004Best regards,
Signature · 1 line
Christian Speich <[email protected]>