hw/sd: Improve performance of read/write/erase
v4 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 (commit 1-3) 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.
The fifth commit fixes the DATA_STAT_AFTER_ERASE bit in SCR and introduces an option to allow to erase blocks to 0x00.
The sixth commit optimizes block erase when erase-blocks-as-zero=true is used, by passing the zeroing request down the to the block device. Erasing 2GiB now takes 0.1s instead of 26s.
---
Changes in v4:
- Rebase onto master, no changes needed.
- Renames:
- sd_blk_{read,write} -> sd_blk_{read,write}_bounce_buffer
- sd_blk_{read,write}_direct -> sd_blk_{read,write}
- to_erase -> erase_len
- Extract erase logic into sd_blk_erase helper
- Enable erase-blocks-as-zero by default. Add compat for 11.0 and below
to keep it disabled.
- Add cleanup commit to update trace events.
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v3:
- Rebase onto master, updating read/write path for newly added RBMP
- Split up commit 1 into multiple commits
- change interface to allow "short" read/writes that are continued
by the core later by calling again
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v2:
- Properly set DATA_STAT_AFTER_ERASE in SCR
- Add erase-blocks-as-zero option to allow the user to switch between
0x00 and 0xFF for erased blocks.
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---
Christian Speich (7):
hw/sd: Switch read/write primitive to buf+len hw/sd/sd: Allow multi-byte read/write for generic paths hw/sd/sd: Use multi-byte/block writes for block path hw/sd/sdhci: Don't use bounce buffer for ADMA hw/sd/sdcard: Add erase-blocks-as-zero option. hw/sd/sdcard: Optimize erase blocks as zero. hw/sd: Update trace events for buf+len data
hw/core/machine.c | 5 + hw/sd/core.c | 53 ++++++-- hw/sd/sd.c | 353 ++++++++++++++++++++++++++++++++++++++++------------- hw/sd/sdhci.c | 102 +++++++++------- hw/sd/trace-events | 6 +- include/hw/sd/sd.h | 22 ++-- 6 files changed, 389 insertions(+), 152 deletions(-)
---
base-commit: da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20250912-sdcard-performance-b4-d908bbb5a004
Best regards,
Signature · 1 line
Christian Speich <[email protected]>
PATCH v4 1/7 hw/sd: Switch read/write primitive to buf+len
Currently, read/writes are broken down into individual bytes which result in many function calls. This is quite bad for performance and since both the layer below and above work with larger buffers, it should be corrected.
This patch is the first that switches the corresponding interface over to use a buf+len instead of a single byte. However, for most cases the implementation still only reads one byte and is then called again with the remaining buffer.
Optimizations taking advantage of this new interface are to follow in the next commits.
--- hw/sd/core.c | 26 ++++++++++++++--------- hw/sd/sd.c | 62 +++++++++++++++++++++++++++++++++++------------------- include/hw/sd/sd.h | 22 +++++++++++++------ 3 files changed, 71 insertions(+), 39 deletions(-)
hw/sd/core.c +16 -10
@@ -113,21 +113,24 @@ 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]); +while (length >0 ) { +size_t written = sc->write_data (card, buf, length); + +g_assert (written >=1 ); + + buf += written; + length -= written; } } }
@@ -140,7 +143,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,14 +153,17 @@ 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]); +while (length >0 ) { +size_t read = sc->read_data (card, buf, length); + +g_assert (read >=1 ); + + buf += read; + length -= read; } } }
hw/sd/sd.c +40 -22
166 lines not on this page · raw message
include/hw/sd/sd.h +15 -7
@@ -107,22 +107,30 @@ 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 +* @len: length of data * -* Write a byte on the data lines of a SD card. +* Write data on the data lines of a SD card. May write not all data, in +* which case it should be called again. At least one byte must be consumed. +* +* Return: number of bytes actually written. >= 1 */ -void (*write_byte)(SDState *sd,uint8_t value); +size_t (*write_data)(SDState *sd,const void *buf,size_t len);/** * Read a byte from a SD card. * @sd: card +* @buf: buffer to receive the data +* @len: size of data to read * -* Read a byte from the data lines of a SD card. +* Read data from the data lines of a SD card. This may not read all +* requested data, in this case it should be called again with the remaining +* buffer. At least one byte must be read. * -* Return: byte value read +* Return: number of bytes actually read. >= 1 */ -uint8_t (*read_byte)(SDState *sd); +size_t (*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 v4 2/7 hw/sd/sd: Allow multi-byte read/write for generic paths
In reply to Christian SpeichPaths that use sd_generic_write/read_data can now write/read multiple bytes with one call.
--- hw/sd/sd.c | 62 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 27 deletions(-)
hw/sd/sd.c +35 -27
@@ -1609,7 +1609,7 @@ static sd_rsp_type_t sd_cmd_optional(SDState *sd, SDRequest req)return sd_illegal; } -/* Configure fields for following sd_generic_write_byte() calls */ +/* Configure fields for following sd_generic_write_data() calls */ static sd_rsp_type_t sd_cmd_to_receivingdata (SDState *sd,SDRequest req,uint64_t start,size_t size) {
@@ -1624,7 +1624,7 @@ static sd_rsp_type_t sd_cmd_to_receivingdata(SDState *sd, SDRequest req,return sd_r1; } -/* Configure fields for following sd_generic_read_byte() calls */ +/* Configure fields for following sd_generic_read_data() calls */ static sd_rsp_type_t sd_cmd_to_sendingdata (SDState *sd,SDRequest req,uint64_t start,const void *data,size_t size)
@@ -2614,11 +2614,15 @@ send_response: }/* Return true if buffer is consumed. Configured by sd_cmd_to_receivingdata() */ -static bool sd_generic_write_byte (SDState *sd,uint8_t value) +static bool sd_generic_write_data (SDState *sd,const void *buf,size_t *len) { - sd->data[sd->data_offset] = value; +size_t to_write =MIN (sd->data_size - sd->data_offset, *len); -if (++sd->data_offset >= sd->data_size) { +memcpy (sd->data, buf, to_write); + sd->data_offset += to_write; + *len = to_write; + +if (sd->data_offset >= sd->data_size) { sd->state = sd_transfer_state;return true; }
@@ -2626,11 +2630,15 @@ static bool sd_generic_write_byte(SDState *sd, uint8_t value) }/* Return true when buffer is consumed. Configured by sd_cmd_to_sendingdata() */ -static bool sd_generic_read_byte (SDState *sd,uint8_t *value) +static bool sd_generic_read_data (SDState *sd,void *buf,size_t *len) { - *value = sd->data[sd->data_offset]; +size_t to_read =MIN (sd->data_size - sd->data_offset, *len); + +memcpy (buf, sd->data, to_read); + sd->data_offset += to_read; + *len = to_read; -if (++sd->data_offset >= sd->data_size) { +if (sd->data_offset >= sd->data_size) { sd->state = sd_transfer_state;return true; }
@@ -2657,18 +2665,12 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)if (sd->card_status & (ADDRESS_ERROR |WP_VIOLATION ))return length; -/* -* Only read one byte at a time. We will be called again with the -* remaining. -*/ - length =1 ; -trace_sdcard_write_data (sd->proto->name, sd->last_cmd_name, sd->current_cmd, sd->data_offset, value[0 ]);switch (sd->current_cmd) {case 24 :/* CMD24: WRITE_SINGLE_BLOCK */ -if (sd_generic_write_byte (sd, value[0 ])) { +if (sd_generic_write_data (sd, buf, &length)) {/* TODO: Check CRC before committing */ sd->state = sd_programming_state;sd_blk_write (sd, sd->data_start, sd->data_offset);
@@ -2680,6 +2682,12 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ +/* +* Only read one byte at a time. We will be called again with the +* remaining. +*/ + length =1 ; + if (sd->data_offset == 0) { /* Start of the block - let's check the address is valid */ if (!address_in_range(sd, "WRITE_MULTIPLE_BLOCK",
@@ -2723,7 +2731,7 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 26 :/* CMD26: PROGRAM_CID */ -if (sd_generic_write_byte (sd, value[0 ])) { +if (sd_generic_write_data (sd, buf, &length)) {/* TODO: Check CRC before committing */ sd->state = sd_programming_state;for (i =0 ; i <sizeof (sd->cid); i ++)
@@ -2741,7 +2749,7 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 27 :/* CMD27: PROGRAM_CSD */ -if (sd_generic_write_byte (sd, value[0 ])) { +if (sd_generic_write_data (sd, buf, &length)) {/* TODO: Check CRC before committing */ sd->state = sd_programming_state;for (i =0 ; i <sizeof (sd->csd); i ++)
@@ -2764,7 +2772,7 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 42 :/* CMD42: LOCK_UNLOCK */ -if (sd_generic_write_byte (sd, value[0 ])) { +if (sd_generic_write_data (sd, buf, &length)) {/* TODO: Check CRC before committing */ sd->state = sd_programming_state;sd_lock_command (sd);
@@ -2774,7 +2782,7 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 56 :/* CMD56: GEN_CMD */ -sd_generic_write_byte (sd, value[0 ]); +sd_generic_write_data (sd, buf, &length);break ;default :
@@ -2809,12 +2817,6 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)return length; } -/* -* We will only read one byte at a time. We will be called again with the -* remaining buffer. -*/ - length =1 ; - io_len =sd_blk_len (sd); trace_sdcard_read_data(sd->proto->name,
@@ -2832,10 +2834,16 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)case 30 :/* CMD30: SEND_WRITE_PROT */ case 51 :/* ACMD51: SEND_SCR */ case 56 :/* CMD56: GEN_CMD */ -sd_generic_read_byte (sd, value); +sd_generic_read_data (sd, buf, &length);break ;case 18 :/* CMD18: READ_MULTIPLE_BLOCK */ +/* +* We will only read one byte at a time. We will be called again with +* the remaining buffer. +*/ + length =1 ; +if (sd->data_offset ==0 ) {if (!address_in_range (sd,"READ_MULTIPLE_BLOCK" , sd->data_start, io_len)) {
@@ -2868,7 +2876,7 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)default :qemu_log_mask (LOG_GUEST_ERROR ,"%s: DAT read illegal for command %s\n" , __func__, sd->last_cmd_name); - *value = dummy_byte; +memset (buf, dummy_byte, length); }return length;
Signature · 1 line
2.43.0
PATCH v4 3/7 hw/sd/sd: Use multi-byte/block writes for block path
In reply to Christian SpeichWhen writing/reading blocks via WRITE/READ_MULTIPLE_BLOCK we try to directly pass this request down to the block layer. This can only be done for properly sized and aligned accesses, other access still use a bounce buffer but still benefit from copying as much data in one memcpy as possible.
RPMB is limited to the slow path using a bounce buffer.
--- hw/sd/sd.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 172 insertions(+), 56 deletions(-)
hw/sd/sd.c +172 -56
@@ -1112,24 +1112,36 @@ static const VMStateDescription sd_vmstate = { }, }; -static void sd_blk_read (SDState *sd,uint64_t addr,uint32_t len) +static void sd_blk_read (SDState *sd,void * buf,uint64_t addr, +uint32_t len) {trace_sdcard_read_block (addr, len); addr +=sd_part_offset (sd); -if (!sd->blk ||blk_pread (sd->blk, addr, len, sd->data,0 ) <0 ) { +if (!sd->blk ||blk_pread (sd->blk, addr, len, buf,0 ) <0 ) {fprintf (stderr,"sd_blk_read: read error on host side\n" ); } } -static void sd_blk_write (SDState *sd,uint64_t addr,uint32_t len) +static void sd_blk_read_bounce_buffer (SDState *sd,uint64_t addr,uint32_t len) +{ +sd_blk_read (sd, sd->data, addr, len); +} + +static void sd_blk_write (SDState *sd,const void *buf,uint64_t addr, +uint32_t len) {trace_sdcard_write_block (addr, len); addr +=sd_part_offset (sd); -if (!sd->blk ||blk_pwrite (sd->blk, addr, len, sd->data,0 ) <0 ) { +if (!sd->blk ||blk_pwrite (sd->blk, addr, len, buf,0 ) <0 ) {fprintf (stderr,"sd_blk_write: write error on host side\n" ); } } +static void sd_blk_write_bounce_buffer (SDState *sd,uint64_t addr,uint32_t len) +{ +sd_blk_write (sd, sd->data, addr, len); +} +static bool rpmb_calc_hmac (SDState *sd,const RPMBDataFrame *frame,unsigned int num_blocks,uint8_t *mac) {
@@ -1363,7 +1375,7 @@ static void sd_erase(SDState *sd)continue ; } } -sd_blk_write (sd, erase_addr, erase_len); +sd_blk_write_bounce_buffer (sd, erase_addr, erase_len); } }
@@ -1984,7 +1996,7 @@ static sd_rsp_type_t sd_cmd_READ_SINGLE_BLOCK(SDState *sd, SDRequest req)return sd_r1; } -sd_blk_read (sd, addr, sd->blk_len); +sd_blk_read_bounce_buffer (sd, addr, sd->blk_len);return sd_cmd_to_sendingdata (sd, req, addr,NULL , sd->blk_len); }
@@ -2673,7 +2685,7 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)if (sd_generic_write_data (sd, buf, &length)) {/* TODO: Check CRC before committing */ sd->state = sd_programming_state; -sd_blk_write (sd, sd->data_start, sd->data_offset); +sd_blk_write_bounce_buffer (sd, sd->data_start, sd->data_offset); sd->blk_written ++; sd->csd[14 ] |=0x40 ;/* Bzzzzzzztt .... Operation complete. */
@@ -2682,44 +2694,85 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ -/* -* Only read one byte at a time. We will be called again with the -* remaining. -*/ - length =1 ; - -if (sd->data_offset ==0 ) { -/* Start of the block - let's check the address is valid */ -if (!address_in_range (sd,"WRITE_MULTIPLE_BLOCK" , - sd->data_start, sd->blk_len)) { -break ; +if (!address_in_range (sd,"WRITE_MULTIPLE_BLOCK" , + sd->data_start + sd->data_offset, length)) { +/* Limit writing data to our device size */ + length = sd->size - sd->data_start - sd->data_offset; + +/* We've read past the end, return a dummy write. */ +if (length ==0 ) { +return 1 ; } -if (sd->size <=SDSC_MAX_CAPACITY ) { -if (sd_wp_addr (sd, sd->data_start)) { + } + +if (sd->size <=SDSC_MAX_CAPACITY ) { +uint64_t start = sd->data_start + sd->data_offset; + +/* +* Check if any covered address violates WP. If so, limit our write +* up to the allowed address. +*/ +for (uint64_t addr = start; addr < start + length; + addr =ROUND_UP (addr +1 ,WPGROUP_SIZE )) { +if (sd_wp_addr (sd, addr)) { sd->card_status |=WP_VIOLATION ; + + length = addr - start -1 ;break ; } } } - sd->data[sd->data_offset++] = value[0 ]; -if (sd->data_offset >= sd->blk_len) { -/* TODO: Check CRC before committing */ - sd->state = sd_programming_state; - partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] - &EXT_CSD_PART_CONFIG_ACC_MASK ; -if (partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) { -emmc_rpmb_blk_write (sd, sd->data_start, sd->data_offset); - }else { -sd_blk_write (sd, sd->data_start, sd->data_offset); + + partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] + &EXT_CSD_PART_CONFIG_ACC_MASK ; + +/* Partial write or RPMB (single block only for now) */ +if (sd->data_offset >0 + || partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) { + length =MIN (sd->blk_len - sd->data_offset, length); + +memcpy (sd->data + sd->data_offset, buf, length); + sd->data_offset += length; + +if (sd->data_offset >= sd->blk_len) { + sd->state = sd_programming_state; +if (partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) { +emmc_rpmb_blk_write (sd, sd->data_start, sd->data_offset); + }else { +sd_blk_write_bounce_buffer (sd, sd->data_start, + sd->data_offset); + } + sd->blk_written++; + sd->data_start += sd->blk_len; + sd->data_offset =0 ; + sd->csd[14 ] |=0x40 ; + +/* Bzzzzzzztt .... Operation complete. */ +if (sd->multi_blk_cnt !=0 ) { +if (--sd->multi_blk_cnt ==0 ) { +/* Stop! */ + sd->state = sd_transfer_state; +break ; + } + } + + sd->state = sd_receivingdata_state; } - sd->blk_written++; - sd->data_start += sd->blk_len; - sd->data_offset =0 ; + } +/* Try to write multiple of block sizes */ +else if (length >= sd->blk_len) { + length =QEMU_ALIGN_DOWN (length, sd->blk_len); + + sd->state = sd_programming_state; +sd_blk_write (sd, buf, sd->data_start, length); + sd->blk_written += length / sd->blk_len; + sd->data_start += length; sd->csd[14 ] |=0x40 ; -/* Bzzzzzzztt .... Operation complete. */ if (sd->multi_blk_cnt !=0 ) { -if (--sd->multi_blk_cnt ==0 ) { + sd->multi_blk_cnt -= length / sd->blk_len; + +if (sd->multi_blk_cnt ==0 ) {/* Stop! */ sd->state = sd_transfer_state;break ;
@@ -2728,6 +2781,12 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length) sd->state = sd_receivingdata_state; } +/* Partial write */ +else if (length >0 ) { +memcpy (sd->data, buf, length); + sd->data_offset = length; + } +break ;case 26 :/* CMD26: PROGRAM_CID */
@@ -2798,7 +2857,6 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)const uint8_t dummy_byte =0x00 ;unsigned int partition_access;uint32_t io_len; -uint8_t *value = buf;if (!sd->blk || !blk_is_inserted (sd->blk)) {memset (buf, dummy_byte, length);
@@ -2838,36 +2896,94 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)break ;case 18 :/* CMD18: READ_MULTIPLE_BLOCK */ +if (!address_in_range (sd,"READ_MULTIPLE_BLOCK" , + sd->data_start + sd->data_offset, length)) { +/* Limit reading data to our device size */ + length = sd->size - sd->data_start - sd->data_offset; + +/* We read past the end, return a dummy read. */ +if (length ==0 ) { +memset (buf, dummy_byte,1 ); +return 1 ; + } + } + + partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] + &EXT_CSD_PART_CONFIG_ACC_MASK ; + +/* We have a partially read block. */ +if (sd->data_offset >0 ) { + length =MIN (sd->data_size - sd->data_offset, length); + +memcpy (buf, sd->data + sd->data_offset, length); + + sd->data_offset += length; + +/* Partial read is complete, clear state. */ +if (sd->data_offset >= sd->data_size) { + sd->data_start += io_len; + sd->data_size =0 ; + sd->data_offset =0 ; + +if (sd->multi_blk_cnt !=0 ) { +if (--sd->multi_blk_cnt ==0 ) { + sd->state = sd_transfer_state; + } + } + } + }/* -* We will only read one byte at a time. We will be called again with -* the remaining buffer. +* Try to read multiples of the block size directly bypassing the local +* bounce buffer. +* Not for RPMB. */ - length =1 ; +else if (length >= io_len + && partition_access !=EXT_CSD_PART_CONFIG_ACC_RPMB ) { + length =QEMU_ALIGN_DOWN (length, io_len); -if (sd->data_offset ==0 ) { -if (!address_in_range (sd,"READ_MULTIPLE_BLOCK" , - sd->data_start, io_len)) { -return dummy_byte; +/* For limited reads, only read the requested block count. */ +if (sd->multi_blk_cnt !=0 ) { + length =MIN (length, sd->multi_blk_cnt * io_len); } - partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] - &EXT_CSD_PART_CONFIG_ACC_MASK ; + +sd_blk_read (sd, buf, sd->data_start, length); + + sd->data_start += length; + +if (sd->multi_blk_cnt !=0 ) { + sd->multi_blk_cnt -= length / io_len; + +if (sd->multi_blk_cnt ==0 ) { + sd->state = sd_transfer_state; + } + } + } +/* Read partial at the end or sinlge-block RPMB */ +else if (length >0 ) { + length =MIN (length, io_len); + +/* Fill the buffer */ if (partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) {emmc_rpmb_blk_read (sd, sd->data_start, io_len); }else { -sd_blk_read (sd, sd->data_start, io_len); +sd_blk_read_bounce_buffer (sd, sd->data_start, io_len); } - } - *value = sd->data[sd->data_offset++]; -if (sd->data_offset >= io_len) { - sd->data_start += io_len; - sd->data_offset =0 ; +memcpy (buf, sd->data, length); -if (sd->multi_blk_cnt !=0 ) { -if (--sd->multi_blk_cnt ==0 ) { -/* Stop! */ - sd->state = sd_transfer_state; -break ; + sd->data_size = io_len; + sd->data_offset = length; + +if (sd->data_offset >= io_len) { + sd->data_start += io_len; + sd->data_offset =0 ; + +if (sd->multi_blk_cnt !=0 ) { +if (--sd->multi_blk_cnt ==0 ) { +/* Stop! */ + sd->state = sd_transfer_state; +break ; + } } } }
Signature · 1 line
2.43.0
Re: [PATCH v4 3/7] hw/sd/sd: Use multi-byte/block writes for block path
Am 17. April 2026 09:51:36 UTC schrieb Christian Speich <[email protected]>:
Quoted 5 lines
When writing/reading blocks via WRITE/READ_MULTIPLE_BLOCK we try to directly pass this request down to the block layer. This can only be done for properly sized and aligned accesses, other access still use a bounce buffer but still benefit from copying as much data in one memcpy as possible.
I really like the whole series since it provides a huge performance boost for the SDHCI subsystem. When performing a package upgrade inside a Linux guest the stalls when Linux flushes its buffers are essentially gone. It would be really nice if the series made it into the upcoming QEMU release. Some comments below. I could send an updated version of this series if needed.
Quoted 23 lines
RPMB is limited to the slow path using a bounce buffer.
Signed-off-by Christian Speich <[email protected]>---
hw/sd/sd.c| 228 ++++++++++++++++++++++++++++++++++++++++++++++---------------1 file changed, 172 insertions(+), 56 deletions(-)hw/sd/sd.c +2
-2@@ -1112,24 +1112,36 @@ static const VMStateDescription sd_vmstate = { }, }; -static void sd_blk_read (SDState *sd,uint64_t addr,uint32_t len) +static void sd_blk_read (SDState *sd,void * buf,uint64_t addr, +uint32_t len) {trace_sdcard_read_block (addr, len); addr +=sd_part_offset (sd); -if (!sd->blk ||blk_pread (sd->blk, addr, len, sd->data,0 ) <0 ) {
hw/sd/sd.c static const VMStateDescription sd_vmstate = { · line 1128
Quoted 10 lines
hw/sd/sd.c +5
-1+if (!sd->blk ||blk_pread (sd->blk, addr, len, buf,0 ) <0 ) {fprintf (stderr,"sd_blk_read: read error on host side\n" ); } } -static void sd_blk_write (SDState *sd,uint64_t addr,uint32_t len) +static void sd_blk_read_bounce_buffer (SDState *sd,uint64_t addr,uint32_t len) +{ +sd_blk_read (sd, sd->data, addr, len); +}
For simplicity and since there are already cases where we pass multiple sd->foo to sd_blk_* I wouldn't introduce the *_bounce_buffer wrappers and instead use sd_blk_* directly with the additional argument. For easier review this should be a separate patch.
Quoted 100 lines
hw/sd/sd.c +34
-25+ +static void sd_blk_write (SDState *sd,const void *buf,uint64_t addr, +uint32_t len) {trace_sdcard_write_block (addr, len); addr +=sd_part_offset (sd); -if (!sd->blk ||blk_pwrite (sd->blk, addr, len, sd->data,0 ) <0 ) { +if (!sd->blk ||blk_pwrite (sd->blk, addr, len, buf,0 ) <0 ) {fprintf (stderr,"sd_blk_write: write error on host side\n" ); } } +static void sd_blk_write_bounce_buffer (SDState *sd,uint64_t addr,uint32_t len) +{ +sd_blk_write (sd, sd->data, addr, len); +} +static bool rpmb_calc_hmac (SDState *sd,const RPMBDataFrame *frame,unsigned int num_blocks,uint8_t *mac) {@@ -1363,7 +1375,7 @@ static void sd_erase(SDState *sd)continue ; } } -sd_blk_write (sd, erase_addr, erase_len); +sd_blk_write_bounce_buffer (sd, erase_addr, erase_len); } }@@ -1984,7 +1996,7 @@ static sd_rsp_type_t sd_cmd_READ_SINGLE_BLOCK(SDState *sd, SDRequest req)return sd_r1; } -sd_blk_read (sd, addr, sd->blk_len); +sd_blk_read_bounce_buffer (sd, addr, sd->blk_len);return sd_cmd_to_sendingdata (sd, req, addr,NULL , sd->blk_len); }@@ -2673,7 +2685,7 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)if (sd_generic_write_data (sd, buf, &length)) {/* TODO: Check CRC before committing */ sd->state = sd_programming_state; -sd_blk_write (sd, sd->data_start, sd->data_offset); +sd_blk_write_bounce_buffer (sd, sd->data_start, sd->data_offset); sd->blk_written ++; sd->csd[14 ] |=0x40 ;/* Bzzzzzzztt .... Operation complete. */ @@ -2682,44 +2694,85 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)break ;case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ -/* -* Only read one byte at a time. We will be called again with the -* remaining. -*/ - length =1 ; - -if (sd->data_offset ==0 ) { -/* Start of the block - let's check the address is valid */ -if (!address_in_range (sd,"WRITE_MULTIPLE_BLOCK" , - sd->data_start, sd->blk_len)) { -break ; +if (!address_in_range (sd,"WRITE_MULTIPLE_BLOCK" , + sd->data_start + sd->data_offset, length)) { +/* Limit writing data to our device size */ + length = sd->size - sd->data_start - sd->data_offset; + +/* We've read past the end, return a dummy write. */ +if (length ==0 ) { +return 1 ; } -if (sd->size <=SDSC_MAX_CAPACITY ) { -if (sd_wp_addr (sd, sd->data_start)) { + } + +if (sd->size <=SDSC_MAX_CAPACITY ) { +uint64_t start = sd->data_start + sd->data_offset; + +/* +* Check if any covered address violates WP. If so, limit our write +* up to the allowed address. +*/ +for (uint64_t addr = start; addr < start + length; + addr =ROUND_UP (addr +1 ,WPGROUP_SIZE )) { +if (sd_wp_addr (sd, addr)) { sd->card_status |=WP_VIOLATION ; + + length = addr - start -1 ;break ; } } } - sd->data[sd->data_offset++] = value[0 ]; -if (sd->data_offset >= sd->blk_len) { -/* TODO: Check CRC before committing */ - sd->state = sd_programming_state; - partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] - &EXT_CSD_PART_CONFIG_ACC_MASK ; -if (partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) { -emmc_rpmb_blk_write (sd, sd->data_start, sd->data_offset);
hw/sd/sd.c static size_t sd_write_data(SDState *sd, const void *buf, size_t length) · line 2732
Quoted 10 lines
hw/sd/sd.c +8
-2- }else { -sd_blk_write (sd, sd->data_start, sd->data_offset); + + partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] + &EXT_CSD_PART_CONFIG_ACC_MASK ; + +/* Partial write or RPMB (single block only for now) */ +if (sd->data_offset >0 + || partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) { + length =MIN (sd->blk_len - sd->data_offset, length);
Similar to the read case I'd add a
if (sd->multi_blk_cnt > 0) {
length = MIN(length, sd->multi_blk_cnt * sd->blk_len);
}here to prevent sd->multi_blk_cnt -= length / sd->blk_len; down below from underflowing.
Quoted 53 lines
hw/sd/sd.c +39
-5+ +memcpy (sd->data + sd->data_offset, buf, length); + sd->data_offset += length; + +if (sd->data_offset >= sd->blk_len) { + sd->state = sd_programming_state; +if (partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) { +emmc_rpmb_blk_write (sd, sd->data_start, sd->data_offset); + }else { +sd_blk_write_bounce_buffer (sd, sd->data_start, + sd->data_offset); + } + sd->blk_written++; + sd->data_start += sd->blk_len; + sd->data_offset =0 ; + sd->csd[14 ] |=0x40 ; + +/* Bzzzzzzztt .... Operation complete. */ +if (sd->multi_blk_cnt !=0 ) { +if (--sd->multi_blk_cnt ==0 ) { +/* Stop! */ + sd->state = sd_transfer_state; +break ; + } + } + + sd->state = sd_receivingdata_state; } - sd->blk_written++; - sd->data_start += sd->blk_len; - sd->data_offset =0 ; + } +/* Try to write multiple of block sizes */ +else if (length >= sd->blk_len) { + length =QEMU_ALIGN_DOWN (length, sd->blk_len); + + sd->state = sd_programming_state; +sd_blk_write (sd, buf, sd->data_start, length); + sd->blk_written += length / sd->blk_len; + sd->data_start += length; sd->csd[14 ] |=0x40 ; -/* Bzzzzzzztt .... Operation complete. */ if (sd->multi_blk_cnt !=0 ) { -if (--sd->multi_blk_cnt ==0 ) { + sd->multi_blk_cnt -= length / sd->blk_len; + +if (sd->multi_blk_cnt ==0 ) {/* Stop! */ sd->state = sd_transfer_state;break ;@@ -2728,6 +2781,12 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)
hw/sd/sd.c static size_t sd_write_data(SDState *sd, const void *buf, size_t length) · line 2791
Quoted 10 lines
hw/sd/sd.c +6
-0sd->state = sd_receivingdata_state; } +/* Partial write */ +else if (length >0 ) { +memcpy (sd->data, buf, length); + sd->data_offset = length; + } +
It should be possible to fold this case into the if statement. Same for the read operation below.
Best regards,
Bernhard
Quoted 127 lines
hw/sd/sd.c +0
-0break ;case 26: /* CMD26: PROGRAM_CID */hw/sd/sd.c +78
-21@@ -2798,7 +2857,6 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)const uint8_t dummy_byte =0x00 ;unsigned int partition_access;uint32_t io_len; -uint8_t *value = buf;if (!sd->blk || !blk_is_inserted (sd->blk)) {memset (buf, dummy_byte, length);@@ -2838,36 +2896,94 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)break ;case 18 :/* CMD18: READ_MULTIPLE_BLOCK */ +if (!address_in_range (sd,"READ_MULTIPLE_BLOCK" , + sd->data_start + sd->data_offset, length)) { +/* Limit reading data to our device size */ + length = sd->size - sd->data_start - sd->data_offset; + +/* We read past the end, return a dummy read. */ +if (length ==0 ) { +memset (buf, dummy_byte,1 ); +return 1 ; + } + } + + partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] + &EXT_CSD_PART_CONFIG_ACC_MASK ; + +/* We have a partially read block. */ +if (sd->data_offset >0 ) { + length =MIN (sd->data_size - sd->data_offset, length); + +memcpy (buf, sd->data + sd->data_offset, length); + + sd->data_offset += length; + +/* Partial read is complete, clear state. */ +if (sd->data_offset >= sd->data_size) { + sd->data_start += io_len; + sd->data_size =0 ; + sd->data_offset =0 ; + +if (sd->multi_blk_cnt !=0 ) { +if (--sd->multi_blk_cnt ==0 ) { + sd->state = sd_transfer_state; + } + } + } + }/* -* We will only read one byte at a time. We will be called again with -* the remaining buffer. +* Try to read multiples of the block size directly bypassing the local +* bounce buffer. +* Not for RPMB. */ - length =1 ; +else if (length >= io_len + && partition_access !=EXT_CSD_PART_CONFIG_ACC_RPMB ) { + length =QEMU_ALIGN_DOWN (length, io_len); -if (sd->data_offset ==0 ) { -if (!address_in_range (sd,"READ_MULTIPLE_BLOCK" , - sd->data_start, io_len)) { -return dummy_byte; +/* For limited reads, only read the requested block count. */ +if (sd->multi_blk_cnt !=0 ) { + length =MIN (length, sd->multi_blk_cnt * io_len); } - partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG ] - &EXT_CSD_PART_CONFIG_ACC_MASK ; + +sd_blk_read (sd, buf, sd->data_start, length); + + sd->data_start += length; + +if (sd->multi_blk_cnt !=0 ) { + sd->multi_blk_cnt -= length / io_len; + +if (sd->multi_blk_cnt ==0 ) { + sd->state = sd_transfer_state; + } + } + } +/* Read partial at the end or sinlge-block RPMB */ +else if (length >0 ) { + length =MIN (length, io_len); + +/* Fill the buffer */ if (partition_access ==EXT_CSD_PART_CONFIG_ACC_RPMB ) {emmc_rpmb_blk_read (sd, sd->data_start, io_len); }else { -sd_blk_read (sd, sd->data_start, io_len); +sd_blk_read_bounce_buffer (sd, sd->data_start, io_len); } - } - *value = sd->data[sd->data_offset++]; -if (sd->data_offset >= io_len) { - sd->data_start += io_len; - sd->data_offset =0 ; +memcpy (buf, sd->data, length); -if (sd->multi_blk_cnt !=0 ) { -if (--sd->multi_blk_cnt ==0 ) { -/* Stop! */ - sd->state = sd_transfer_state; -break ; + sd->data_size = io_len; + sd->data_offset = length; + +if (sd->data_offset >= io_len) { + sd->data_start += io_len; + sd->data_offset =0 ; + +if (sd->multi_blk_cnt !=0 ) { +if (--sd->multi_blk_cnt ==0 ) { +/* Stop! */ + sd->state = sd_transfer_state; +break ; + } } } }
PATCH v4 4/7 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
@@ -775,7 +775,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 = {};
@@ -817,66 +817,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
Re: [PATCH v4 4/7] hw/sd/sdhci: Don't use bounce buffer for ADMA
Quoted 10 lines · Christian Speich
Currently, 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.
Quoted 4 lines
Signed-off-by Christian Speich <[email protected]>--- hw/sd/sdhci.c | 102 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 47 deletions(-)
PATCH v4 5/7 hw/sd/sdcard: Add erase-blocks-as-zero option.
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.
This patch does two things.
First it fixes the reporting of DATA_STAT_AFTER_ERASE in SCR to correctly reflect the content of erased blocks. We also increase the Product Revision (REV in CID) to indicate to the guest that DATA_STAT_AFTER_ERASE is now reliable.
Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain 0xFF or 0x00. The default is still 0xFF to remain compatible with current users.
--- hw/core/machine.c | 5 +++++ hw/sd/sd.c | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-)
hw/core/machine.c +5 -0
@@ -38,6 +38,11 @@#include "hw/acpi/generic_event_device.h" #include "qemu/audio.h" +GlobalProperty hw_compat_11_0[] = { + {"sdmmc-common" ,"erase-blocks-as-zero" ,"false" }, +}; +const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0); +GlobalProperty hw_compat_10_2[] = { {"scsi-block" ,"migrate-pr" ,"off" }, {"isa-cirrus-vga" ,"global-vmstate" ,"true" },
hw/sd/sd.c +13 -2
@@ -162,6 +162,7 @@ struct SDState {/* Static properties */ uint8_t spec_version; +bool erase_blocks_as_zero;uint64_t boot_part_size;uint64_t rpmb_part_size;BlockBackend *blk;
@@ -439,6 +440,9 @@ static void sd_set_scr(SDState *sd) sd->scr[0 ] |=2 ;/* Spec Version 2.00 or Version 3.0X */ sd->scr[1 ] = (2 <<4 )/* SDSC Card (Security Version 1.01) */ |0b0101 ;/* 1-bit or 4-bit width bus modes */ +if (!sd->erase_blocks_as_zero) { + sd->scr[1 ] |= (1 <<7 );/* DATA_STAT_AFTER_ERASE: Erase produces 0xFF */ + } sd->scr[2 ] =0x00 ;/* Extended Security is not supported. */ if (sd->spec_version >= SD_PHY_SPECv3_01_VERS) { sd->scr[2 ] |=1 <<7 ;/* Spec Version 3.0X */
@@ -456,7 +460,7 @@ static void sd_set_scr(SDState *sd)#define MID 0xaa#define OID "XY"#define PNM "QEMU!" -#define PRV 0x01 +#define PRV 0x02#define MDT_YR 2006#define MDT_MON 2
@@ -1363,7 +1367,12 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -memset (sd->data,0xff , erase_len); +if (sd->erase_blocks_as_zero) { +memset (sd->data,0x0 , erase_len); + }else { +memset (sd->data,0xFF , erase_len); + } +for (erase_addr = erase_start; erase_addr <= erase_end; erase_addr += erase_len) {if (sdsc) {
@@ -3287,6 +3296,8 @@ static void emmc_realize(DeviceState *dev, Error **errp)static const Property sdmmc_common_properties[] = {DEFINE_PROP_DRIVE ("drive" , SDState, blk), +DEFINE_PROP_BOOL ("erase-blocks-as-zero" , SDState, erase_blocks_as_zero, + true), };static const Property sd_properties[] = {
Signature · 1 line
2.43.0
Re: [PATCH v4 5/7] hw/sd/sdcard: Add erase-blocks-as-zero option.
Quoted 6 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.This patch does two things.
Quoted 8 lines
First it fixes the reporting of
DATA_STAT_AFTER_ERASEin SCR to correctly reflect the content of erased blocks. We also increase the Product Revision (REV in CID) to indicate to the guest thatDATA_STAT_AFTER_ERASEis now reliable.Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. The default is still0xFFto remain compatible with current users.
I'll update as:
Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain 0xFF or 0x00. While previous QEMU versions keep using the 'erase as 0xFF' pattern, the new default becomes the more efficient 'erase as 0x00'.
Does it look OK to you?
Quoted 28 lines
Signed-off-by Christian Speich <[email protected]>--- hw/core/machine.c | 5 +++++ hw/sd/sd.c | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-)hw/core/machine.c +5
-0@@ -38,6 +38,11 @@#include "hw/acpi/generic_event_device.h" #include "qemu/audio.h" +GlobalProperty hw_compat_11_0[] = { + {"sdmmc-common" ,"erase-blocks-as-zero" ,"false" }, +}; +const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0); +GlobalProperty hw_compat_10_2[] = { {"scsi-block" ,"migrate-pr" ,"off" }, {"isa-cirrus-vga" ,"global-vmstate" ,"true" },hw/sd/sd.c +0
-0
hw/sd/sd.c static void emmc_realize(DeviceState *dev, Error **errp) · line 3301
Quoted 6 lines
hw/sd/sd.c +2
-0@@ -3287,6 +3296,8 @@ static void emmc_realize(DeviceState *dev, Error **errp)static const Property sdmmc_common_properties[] = {DEFINE_PROP_DRIVE ("drive" , SDState, blk), +DEFINE_PROP_BOOL ("erase-blocks-as-zero" , SDState, erase_blocks_as_zero, + true), };
Re: [PATCH v4 5/7] hw/sd/sdcard: Add erase-blocks-as-zero option.
Quoted 51 lines · Philippe Mathieu-Daudé
Quoted 6 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.This patch does two things.
Quoted 8 lines
First it fixes the reporting of
DATA_STAT_AFTER_ERASEin SCR to correctly reflect the content of erased blocks. We also increase the Product Revision (REV in CID) to indicate to the guest thatDATA_STAT_AFTER_ERASEis now reliable.Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. The default is still0xFFto remain compatible with current users.I'll update as:
Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. While previous QEMU versions keep using the 'erase as0xFF' pattern, the new default becomes the more efficient 'erase as0x00'.Does it look OK to you?
Quoted 25 lines
Signed-off-by Christian Speich <[email protected]>---
hw/core/machine.c| 5 +++++
hw/sd/sd.c | 15 +++++++++++++--2 files changed, 18 insertions(+), 2 deletions(-)hw/core/machine.c +5
-0@@ -38,6 +38,11 @@ #include "hw/acpi/generic_event_device.h" #include "qemu/audio.h" +GlobalProperty hw_compat_11_0[] = { + { "sdmmc-common", "erase-blocks-as-zero", "false" }, +}; +const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0); + GlobalProperty hw_compat_10_2[] = { { "scsi-block", "migrate-pr", "off" }, { "isa-cirrus-vga", "global-vmstate", "true" },hw/sd/sd.c +0
-0
Quoted 7 lines
hw/sd/sd.c static void emmc_realize(DeviceState *dev, Error · line 3301
Quoted 7 lines
hw/sd/sd.c +2
-0@@ -3287,6 +3296,8 @@ static void emmc_realize(DeviceState *dev, Error **errp) static const Property sdmmc_common_properties[] = { DEFINE_PROP_DRIVE("drive", SDState, blk), + DEFINE_PROP_BOOL("erase-blocks-as-zero", SDState, erase_blocks_as_zero, + true),
Although to maintain bisectability we should expose the property after the next patch -- the 0x00 implementation --.
Quoted 4 lines
hw/sd/sd.c static void emmc_realize(DeviceState *dev, Error · line 3302
Quoted 1 line
hw/sd/sd.c +0
-0};Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Re: [PATCH v4 5/7] hw/sd/sdcard: Add erase-blocks-as-zero option.
Quoted 52 lines · Philippe Mathieu-Daudé
Quoted 51 lines · Philippe Mathieu-Daudé
Quoted 6 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.This patch does two things.
Quoted 8 lines
First it fixes the reporting of
DATA_STAT_AFTER_ERASEin SCR to correctly reflect the content of erased blocks. We also increase the Product Revision (REV in CID) to indicate to the guest thatDATA_STAT_AFTER_ERASEis now reliable.Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. The default is still0xFFto remain compatible with current users.I'll update as:
Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. While previous QEMU versions keep using the 'erase as0xFF' pattern, the new default becomes the more efficient 'erase as0x00'.Does it look OK to you?
Quoted 25 lines
Signed-off-by Christian Speich <[email protected]>---
hw/core/machine.c| 5 +++++
hw/sd/sd.c | 15 +++++++++++++--2 files changed, 18 insertions(+), 2 deletions(-)hw/core/machine.c +5
-0@@ -38,6 +38,11 @@ #include "hw/acpi/generic_event_device.h" #include "qemu/audio.h" +GlobalProperty hw_compat_11_0[] = { + { "sdmmc-common", "erase-blocks-as-zero", "false" }, +}; +const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0); + GlobalProperty hw_compat_10_2[] = { { "scsi-block", "migrate-pr", "off" }, { "isa-cirrus-vga", "global-vmstate", "true" },hw/sd/sd.c +0
-0
Quoted 10 lines
Quoted 7 lines
hw/sd/sd.c static void emmc_realize(DeviceState *dev, · line 3301
Quoted 7 lines
hw/sd/sd.c +2
-0@@ -3287,6 +3296,8 @@ static void emmc_realize(DeviceState *dev, Error **errp) static const Property sdmmc_common_properties[] = { DEFINE_PROP_DRIVE("drive", SDState, blk), + DEFINE_PROP_BOOL("erase-blocks-as-zero", SDState, erase_blocks_as_zero, + true),Although to maintain bisectability we should expose the property after the next patch -- the
0x00implementation --.
Ah no, this is correct, this patch contains the memset() implementation, and the following patch *optimize* :)
Quoted 6 lines
Quoted 5 lines
hw/sd/sd.c static void emmc_realize(DeviceState *dev, · line 3302
Quoted 2 lines
hw/sd/sd.c +0
-0};Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Re: [PATCH v4 5/7] hw/sd/sdcard: Add erase-blocks-as-zero option.
In reply to Philippe Mathieu-DaudéQuoted 16 lines · Philippe Mathieu-Daudé
Quoted 6 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.This patch does two things.
Quoted 8 lines
First it fixes the reporting of
DATA_STAT_AFTER_ERASEin SCR to correctly reflect the content of erased blocks. We also increase the Product Revision (REV in CID) to indicate to the guest thatDATA_STAT_AFTER_ERASEis now reliable.Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. The default is still0xFFto remain compatible with current users.
Quoted 8 lines
I'll update as:
Secondly, we introduce a erase-blocks-as-zero option, which allows the user to choose if erased blocks should contain
0xFFor0x00. While previous QEMU versions keep using the 'erase as0xFF' pattern, the new default becomes the more efficient 'erase as0x00'.Does it look OK to you?
Yes, looks good, thanks.
Kind Regards,
Christian
Quoted 37 lines
Quoted 28 lines
Signed-off-by Christian Speich <[email protected]>--- hw/core/machine.c | 5 +++++ hw/sd/sd.c | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-)hw/core/machine.c +5
-0@@ -38,6 +38,11 @@#include "hw/acpi/generic_event_device.h" #include "qemu/audio.h" +GlobalProperty hw_compat_11_0[] = { + {"sdmmc-common" ,"erase-blocks-as-zero" ,"false" }, +}; +const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0); +GlobalProperty hw_compat_10_2[] = { {"scsi-block" ,"migrate-pr" ,"off" }, {"isa-cirrus-vga" ,"global-vmstate" ,"true" },hw/sd/sd.c +0
-0hw/sd/sd.c static void emmc_realize(DeviceState *dev, Error **errp) · line 3300
Quoted 5 lines
hw/sd/sd.c +2
-0@@ -3287,6 +3296,8 @@ static void emmc_realize(DeviceState *dev, Error **errp)static const Property sdmmc_common_properties[] = {DEFINE_PROP_DRIVE ("drive" , SDState, blk), +DEFINE_PROP_BOOL ("erase-blocks-as-zero" , SDState, erase_blocks_as_zero, + true), };Reviewed-by Philippe Mathieu-Daudé <[email protected]>
PATCH v4 6/7 hw/sd/sdcard: Optimize erase blocks as zero.
In reply to Christian SpeichWhen erasing blocks as zero, we can use optimized block functions to achieve this.
These allow us to request a large rage to be zeroed, possible optimizing this operation and freeing disk space for sparsesly stored images.
This only is possible when erase-blocks-as-zero=true is used and can provide a significant performance boost. The case where 0xFF is used during erase is as slow as before.
--- hw/sd/sd.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-)
hw/sd/sd.c +33 -10
@@ -1331,6 +1331,26 @@ exit:trace_sdcard_rpmb_write_block (req,lduw_be_p (&sd->rpmb.result.result)); } +/* Requires sd->buf to be filled with 0xFF */ +static void sd_erase_ff (SDState *sd,uint64_t addr,size_t len) +{ +int erase_len =1 <<HWBLOCK_SHIFT ; +uint64_t erase_addr; + +for (erase_addr = addr; erase_addr < addr + len; erase_addr += erase_len) { +sd_blk_write_bounce_buffer (sd, erase_addr, erase_len); + } +} + +static void sd_blk_erase (SDState *sd,uint64_t addr,size_t len) +{ +if (sd->erase_blocks_as_zero) { +blk_pwrite_zeroes (sd->blk, addr +sd_part_offset (sd), len,0 ); + }else { +sd_erase_ff (sd, addr, len); + } +} +static void sd_erase (SDState *sd) {uint64_t erase_start = sd->erase_start;
@@ -1338,7 +1358,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
@@ -1367,24 +1386,28 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -if (sd->erase_blocks_as_zero) { -memset (sd->data,0x0 , erase_len); - }else { -memset (sd->data,0xFF , erase_len); +if (!sd->erase_blocks_as_zero) { +memset (sd->data,0xFF ,1 <<HWBLOCK_SHIFT ); } -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 erase_len =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 ; } + +sd_blk_erase (sd, erase_addr, erase_len); } -sd_blk_write_bounce_buffer (sd, erase_addr, erase_len); + }else { +sd_blk_erase (sd, erase_start, erase_end - erase_start); } }
Signature · 1 line
2.43.0
PATCH v4 7/7 hw/sd: Update trace events for buf+len data
In reply to Christian SpeichAfter switching sdbuf and sdcard over to use buf+len instead of single byte operation, the trace events need to be updated.
This patch updates sdbus_{read,write} and sdcard_write_data to output the buffer that is worked on.
sdcard_read_data is left unchanged, as it did not print the read byte before anyways.
--- hw/sd/core.c | 27 +++++++++++++++++++++++++-- hw/sd/sd.c | 19 +++++++++++++++---- hw/sd/trace-events | 6 +++--- 3 files changed, 43 insertions(+), 9 deletions(-)
hw/sd/core.c +25 -2
@@ -24,6 +24,7 @@#include "hw/sd/sd.h" #include "qemu/module.h" #include "qapi/error.h" +#include "qemu/cutils.h" #include "sdmmc-internal.h" #include "trace.h"
@@ -43,6 +44,26 @@ static SDState *get_card(SDBus *sdbus)return SDMMC_COMMON (kid->child); } +static void sdbus_write_dump (const char *bus_name,const void *buf,size_t len) +{ +g_autoptr (GString) str =NULL ; + +if (trace_event_get_state_backends (TRACE_SDBUS_WRITE )) { + str =qemu_hexdump_line (NULL , buf, len,8 ,0 ); +trace_sdbus_write (bus_name, str->str); + } +} + +static void sdbus_read_dump (const char *bus_name,const void *buf,size_t len) +{ +g_autoptr (GString) str =NULL ; + +if (trace_event_get_state_backends (TRACE_SDBUS_READ )) { + str =qemu_hexdump_line (NULL , buf, len,8 ,0 ); +trace_sdbus_read (bus_name, str->str); + } +} +uint8_t sdbus_get_dat_lines (SDBus *sdbus) {SDState *slave =get_card (sdbus);
@@ -109,7 +130,7 @@ void sdbus_write_byte(SDBus *sdbus, uint8_t value) {SDState *card =get_card (sdbus); -trace_sdbus_write (sdbus_name (sdbus), value); +sdbus_write_dump (sdbus_name (sdbus), &value,1 );if (card) {SDCardClass *sc =SDMMC_COMMON_GET_CLASS (card);
@@ -121,6 +142,7 @@ void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length) {SDState *card =get_card (sdbus); +sdbus_write_dump (sdbus_name (sdbus), buf, length);if (card) {SDCardClass *sc =SDMMC_COMMON_GET_CLASS (card);
@@ -145,7 +167,7 @@ uint8_t sdbus_read_byte(SDBus *sdbus) sc->read_data (card, &value,1 ); } -trace_sdbus_read (sdbus_name (sdbus), value); +sdbus_read_dump (sdbus_name (sdbus), &value,1 );return value; }
@@ -166,6 +188,7 @@ void sdbus_read_data(SDBus *sdbus, void *buf, size_t length) length -= read; } } +sdbus_read_dump (sdbus_name (sdbus), buf, length); }bool sdbus_receive_ready (SDBus *sdbus)
hw/sd/sd.c +15 -4
@@ -2690,11 +2690,22 @@ static bool sd_generic_read_data(SDState *sd, void *buf, size_t *len)return false; } +static void sdcard_write_data_dump (const char *proto,const char *cmd_desc, +uint8_t cmd,uint32_t offset, +const void *buf,size_t len) +{ +g_autoptr (GString) str =NULL ; + +if (trace_event_get_state_backends (TRACE_SDCARD_WRITE_DATA )) { + str =qemu_hexdump_line (NULL , buf, len,8 ,0 ); +trace_sdcard_write_data (proto, cmd_desc, cmd, offset, str->str); + } +} +static size_t sd_write_data (SDState *sd,const void *buf,size_t length) {unsigned int partition_access;int i; -const uint8_t *value = buf;if (!sd->blk || !blk_is_inserted (sd->blk)) {return length;
@@ -2709,9 +2720,9 @@ static size_t sd_write_data(SDState *sd, const void *buf, size_t length)if (sd->card_status & (ADDRESS_ERROR |WP_VIOLATION ))return length; -trace_sdcard_write_data (sd->proto->name, - sd->last_cmd_name, - sd->current_cmd, sd->data_offset, value[0 ]); +sdcard_write_data_dump (sd->proto->name, + sd->last_cmd_name, + sd->current_cmd, sd->data_offset, buf, length);switch (sd->current_cmd) {case 24 :/* CMD24: WRITE_SINGLE_BLOCK */ if (sd_generic_write_data (sd, buf, &length)) {
hw/sd/trace-events +3 -3
@@ -15,8 +15,8 @@ bcm2835_sdhost_update_irq(uint32_t irq) "IRQ bits 0x%x" # core.c sdbus_command(const char *bus_name, uint8_t cmd, uint32_t arg) "@%s CMD%02d arg 0x%08x" -sdbus_read(const char *bus_name, uint8_t value) "@%s value 0x%02x" -sdbus_write(const char *bus_name, uint8_t value) "@%s value 0x%02x" +sdbus_read(const char *bus_name, const char *hexdump) "@%s data %s" +sdbus_write(const char *bus_name, const char *hexdump) "@%s data %s" sdbus_set_voltage(const char *bus_name, uint16_t millivolts) "@%s %u (mV)" sdbus_get_dat_lines(const char *bus_name, uint8_t dat_lines) "@%s dat_lines: %u" sdbus_get_cmd_line(const char *bus_name, bool cmd_line) "@%s cmd_line: %u"
@@ -54,7 +54,7 @@ sdcard_unlock(void) "" sdcard_req_addr(uint32_t req_arg, uint64_t addr) "req 0x%" PRIx32 " addr 0x%" PRIx64 sdcard_read_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x" sdcard_write_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x" -sdcard_write_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t offset, uint8_t value) "%s %20s/ CMD%02d ofs %"PRIu32" value 0x%02x" +sdcard_write_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t offset, const char *hexdump) "%s %20s/ CMD%02d ofs %"PRIu32" data %s" sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t offset, uint64_t size, uint32_t blklen) "%s %20s/ CMD%02d ofs %"PRIu32" size %"PRIu64" blklen %" PRIu32 sdcard_set_voltage(uint16_t millivolts) "%u mV" sdcard_ext_csd_update(unsigned index, uint8_t oval, uint8_t nval) "index %u: 0x%02x -> 0x%02x"
Signature · 1 line
2.43.0
Re: [PATCH v4 7/7] hw/sd: Update trace events for buf+len data
Quoted 9 lines · Christian Speich
After switching sdbuf and sdcard over to use buf+len instead of single byte operation, the trace events need to be updated.
This patch updates sdbus_{read,write} and
sdcard_write_datato output the buffer that is worked on.
sdcard_read_datais left unchanged, as it did not print the read byte before anyways.
Quoted 6 lines
Signed-off-by Christian Speich <[email protected]>--- hw/sd/core.c | 27 +++++++++++++++++++++++++-- hw/sd/sd.c | 19 +++++++++++++++---- hw/sd/trace-events | 6 +++--- 3 files changed, 43 insertions(+), 9 deletions(-)
Thanks!
Quoted 6 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.
From 1 byte (master) to 512 bytes (commit 1-3) to larger than 512
(adma commit).
...
Quoted 8 lines
Christian Speich (7):
hw/sd: Switch read/write primitive to buf+len hw/sd/sd: Allow multi-byte read/write for generic paths hw/sd/sd: Use multi-byte/block writes for block path hw/sd/sdhci: Don't use bounce buffer for ADMA hw/sd/sdcard: Add erase-blocks-as-zero option. hw/sd/sdcard: Optimize erase blocks as zero. hw/sd: Update trace events for buf+len data
3 changes from this series landed in the master branch for v11.1.0 release:
468fa450a7e0c hw/sd: Switch read/write primitive to buf+len eff58d3d9681a hw/sd/sd: Allow multi-byte read/write for generic paths d7f3ab9d5a186 hw/sd: Update trace events for buf+len data
I don't see discussion about the other 4 changes, why these weren't applied at the time, but ok.
Currently I'm picking up another change:
b5424abaf8e9a hw/sd/sdcard: Keep high-capacity memory blocks at 512 bytes
to the current stable qemu series, and it touches the same area. But the mentioned-above 3 changes are not included in 10.0 release, while 10.0.x is an LTS stable series. And without the first change (switch to buf+len), things in 10.0.x are quite different.
So I can either pick the above 3 changes for 10.0.x, or I can omit b5424abaf8e9a for 10.0.x.
To me it feels like picking up the above 3 optimization changes is the way to go for 10.0.x.
What do you think?
BTW, doesn't b5424abaf8e9a actually fix 468fa450a7e0c, not 2d7adea4fe57?
Thanks,
/mjt
Quoted 20 lines · Michael Tokarev
Quoted 6 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. From 1 byte (master) to 512 bytes (commit 1-3) to larger than 512 (adma commit).
...
Quoted 8 lines
Christian Speich (7):
hw/sd: Switch read/write primitive to buf+len hw/sd/sd: Allow multi-byte read/write for generic paths hw/sd/sd: Use multi-byte/block writes for block path hw/sd/sdhci: Don't use bounce buffer for ADMA hw/sd/sdcard: Add erase-blocks-as-zero option. hw/sd/sdcard: Optimize erase blocks as zero. hw/sd: Update trace events for buf+len data3 changes from this series landed in the master branch for v11.1.0 release:
Quoted 7 lines
468fa450a7e0c hw/sd: Switch read/write primitive to buf+len
eff58d3d9681ahw/sd/sd: Allow multi-byte read/write for generic pathsd7f3ab9d5a186hw/sd: Update trace events for buf+len dataI don't see discussion about the other 4 changes, why these weren't applied at the time, but ok.
The rest failed harness test suite.
Quoted 10 lines
Currently I'm picking up another change:
b5424abaf8e9a hw/sd/sdcard: Keep high-capacity memory blocks at 512 bytes
to the current stable qemu series, and it touches the same area. But the mentioned-above 3 changes are not included in 10.0 release, while 10.0.x is an LTS stable series. And without the first change (switch to buf+len), things in 10.0.x are quite different.
Quoted 7 lines
So I can either pick the above 3 changes for 10.0.x, or I can omit
b5424abaf8e9afor 10.0.x.To me it feels like picking up the above 3 optimization changes is the way to go for 10.0.x.
What do you think?
I'll defer that to Bin, no strong opinion.
Quoted 2 lines
BTW, doesn't
b5424abaf8e9aactually fix468fa450a7e0c, not2d7adea4fe57?
Likely.
Quoted 5 lines
Thanks,
/mjt