SD card model save/load support
v5 cover SD card model save/load support
This was a part of a bigger series which also included SD state qomification, but I decided to split them.
PATCH1
Not actually related to vmstate, fixes a bug for high and extra capacity cards in erase function.
Changelog:v4->v5
- changed commit message to clearly state that it's a bugfix;
- use neat extract*() API to test a bit in OCR register;
- get rid of "?:" statements;v3->v4
new in seriesv1->v3
wasn't presented
PATCH2
Patch by Peter Maydell which adds support of bitmaps to vmstate, required for next patch.
Changelog:
It was originally a standalone patch, the only thing I changed is a comment
in vmstate.h:
"_field_size should be a uint32_t.." -> "_field_size should be a int32_t..".
PATCH3
Adds save/load support for SDState, intermediate variable introduced in SDState to hold size of wp_groups bitfield.
Changelog:v4->v5
- use new VMSTATE_BITMAP() macro for wpgroup bitmap;
- give a names to SD card mode and state enums and refer to them in comments to .mode and .state SDState members;v3->v4
no changev2->v3
- don't use BITS_TO_LONGS when operating with bitfields;v1->v2
- use bitmap.h heder for bit operations
Igor Mitsyanko (2):
hw/sd.c: Fix erase for high capacity cards
hw/sd.c: add SD card save/load support
Peter Maydell (1):
vmstate: Add support for saving/loading bitmaps
hw/sd.c | 106 +++++++++++++++++++++++++++++++++++++++++++++----------------- hw/sd.h | 1 + savevm.c | 41 ++++++++++++++++++++++++ vmstate.h | 13 ++++++++ 4 files changed, 132 insertions(+), 29 deletions(-)
Signature · 1 line
1.8.0.msysgit.0
PATCH v5 1/3 hw/sd.c: Fix erase for high capacity cards
Standard capacity cards SDSC use byte unit address while SDHC and SDXC cards use block unit address (512 bytes) when setting ERASE_START and ERASE_END with CMD32 and CMD33, we have to account for this.
--- hw/sd.c | 17 +++++++++++++---- hw/sd.h | 1 + 2 files changed, 14 insertions(+), 4 deletions(-)
hw/sd.c +13 -4
@@ -476,19 +476,28 @@ void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert)static void sd_erase (SDState *sd) { -int i, start, end; +int i; +uint64_t erase_start = sd->erase_start; +uint64_t erase_end = sd->erase_end; +if (!sd->erase_start || !sd->erase_end) { sd->card_status |=ERASE_SEQ_ERROR ;return ; } - start =sd_addr_to_wpnum (sd->erase_start); - end =sd_addr_to_wpnum (sd->erase_end); +if (extract32 (sd->ocr,OCR_CCS_BITN ,1 )) { +/* High capacity memory card: erase units are 512 byte blocks */ + erase_start *=512 ; + erase_end *=512 ; + } + + erase_start =sd_addr_to_wpnum (erase_start); + erase_end =sd_addr_to_wpnum (erase_end); sd->erase_start =0 ; sd->erase_end =0 ; sd->csd[14 ] |=0x40 ; -for (i = start; i <= end; i++) { +for (i = erase_start; i <= erase_end; i++) {if (test_bit (i, sd->wp_groups)) { sd->card_status |=WP_ERASE_SKIP ; }
hw/sd.h +1 -0
@@ -50,6 +50,7 @@#define READY_FOR_DATA (1 << 8)#define APP_CMD (1 << 5)#define AKE_SEQ_ERROR (1 << 3) +#define OCR_CCS_BITN 30 typedef enum { sd_none = -1,
Signature · 1 line
1.8.0.msysgit.0
Re: [Qemu-devel] [PATCH V5 1/3] hw/sd.c: Fix erase for high capacity cards
Quoted 5 lines · Igor Mitsyanko
Standard capacity cards SDSC use byte unit address while SDHC and SDXC cards use block unit address (512 bytes) when setting
ERASE_STARTandERASE_ENDwith CMD32 and CMD33, we have to account for this.Signed-off-by Igor Mitsyanko <[email protected]>
-- PMM
PATCH v5 2/3 vmstate: Add support for saving/loading bitmaps
In reply to Igor MitsyankoFrom: Peter Maydell <[email protected]>
Add support for saving/loading bitmap.h bitmaps in vmstate.
--- savevm.c | 41 +++++++++++++++++++++++++++++++++++++++++ vmstate.h | 13 +++++++++++++ 2 files changed, 54 insertions(+)
savevm.c +41 -0
@@ -86,6 +86,7 @@#include "memory.h" #include "qmp-commands.h" #include "trace.h" +#include "bitops.h" #define SELF_ANNOUNCE_ROUNDS 5
@@ -1132,6 +1133,46 @@ const VMStateInfo vmstate_info_unused_buffer = { .put = put_unused_buffer, }; +/* bitmaps (as defined by bitmap.h). Note that size here is the size +* of the bitmap in bits. The on-the-wire format of a bitmap is 64 +* bit words with the bits in big endian order. The in-memory format +* is an array of 'unsigned long', which may be either 32 or 64 bits. +*/ +/* This is the number of 64 bit words sent over the wire */ +#define BITS_TO_U64S (nr) DIV_ROUND_UP(nr, 64) +static int get_bitmap (QEMUFile *f,void *pv,size_t size) +{ +unsigned long *bmp = pv; +int i, idx =0 ; +for (i =0 ; i <BITS_TO_U64S (size); i++) { +uint64_t w =qemu_get_be64 (f); + bmp[idx++] = w; +if (sizeof (unsigned long ) ==4 && idx <BITS_TO_LONGS (size)) { + bmp[idx++] = w >>32 ; + } + } +return 0 ; +} + +static void put_bitmap (QEMUFile *f,void *pv,size_t size) +{ +unsigned long *bmp = pv; +int i, idx =0 ; +for (i =0 ; i <BITS_TO_U64S (size); i++) { +uint64_t w = bmp[idx++]; +if (sizeof (unsigned long ) ==4 && idx <BITS_TO_LONGS (size)) { + w |= ((uint64_t )bmp[idx++]) <<32 ; + } +qemu_put_be64 (f, w); + } +} + +const VMStateInfo vmstate_info_bitmap = { + .name ="bitmap" , + .get = get_bitmap, + .put = put_bitmap, +}; + typedef struct CompatEntry { char idstr[256]; int instance_id;
vmstate.h +13 -0
@@ -139,6 +139,7 @@ extern const VMStateInfo vmstate_info_uint64;extern const VMStateInfo vmstate_info_timer;extern const VMStateInfo vmstate_info_buffer;extern const VMStateInfo vmstate_info_unused_buffer; +extern const VMStateInfo vmstate_info_bitmap;#define type_check_array (t1,t2,n) ((t1(*)[n])0 - (t2*)0)#define type_check_pointer (t1,t2) ((t1**)0 - (t2*)0)
@@ -411,6 +412,18 @@ extern const VMStateInfo vmstate_info_unused_buffer; .flags = VMS_BUFFER, \ } +/* _field_size should be a int32_t field in the _state struct giving the +* size of the bitmap _field in bits. +*/ +#define VMSTATE_BITMAP (_field, _state, _version, _field_size) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .size_offset = vmstate_offset_value(_state, _field_size, int32_t),\ + .info = &vmstate_info_bitmap, \ + .flags = VMS_VBUFFER|VMS_POINTER, \ + .offset = offsetof(_state, _field), \ +} +/* _f : field name _f_n : num of elements field_name _n : num of elements
Signature · 1 line
1.8.0.msysgit.0
PATCH v5 3/3 hw/sd.c: add SD card save/load support
In reply to Igor MitsyankoThis patch updates SD card model to support save/load of card's state.
--- hw/sd.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 25 deletions(-)
hw/sd.c +64 -25
@@ -55,24 +55,28 @@ typedef enum { sd_illegal =-2 , } sd_rsp_type_t; +enum SDCardModes { + sd_inactive, + sd_card_identification_mode, + sd_data_transfer_mode, +}; + +enum SDCardStates { + sd_inactive_state =-1 , + sd_idle_state =0 , + sd_ready_state, + sd_identification_state, + sd_standby_state, + sd_transfer_state, + sd_sendingdata_state, + sd_receivingdata_state, + sd_programming_state, + sd_disconnect_state, +}; +struct SDState { -enum { - sd_inactive, - sd_card_identification_mode, - sd_data_transfer_mode, - } mode; -enum { - sd_inactive_state =-1 , - sd_idle_state =0 , - sd_ready_state, - sd_identification_state, - sd_standby_state, - sd_transfer_state, - sd_sendingdata_state, - sd_receivingdata_state, - sd_programming_state, - sd_disconnect_state, - } state; +uint32_t mode;/* current card mode, one of SDCardModes */ +int32_t state;/* current card state, one of SDCardStates */ uint32_t ocr;uint8_t scr[8 ];uint8_t cid[16 ];
@@ -83,21 +87,22 @@ struct SDState {uint32_t vhs;bool wp_switch;unsigned long *wp_groups; +int32_t wpgrps_size;uint64_t size; -int blk_len; +uint32_t blk_len;uint32_t erase_start;uint32_t erase_end;uint8_t pwd[16 ]; -int pwd_len; -int function_group[6 ]; +uint32_t pwd_len; +uint8_t function_group[6 ];bool spi; -int current_cmd; +uint8_t current_cmd;/* True if we will handle the next command as an ACMD. Note that this does * *not* track the APP_CMD status bit! */ bool expecting_acmd; -int blk_written; +uint32_t blk_written;uint64_t data_start;uint32_t data_offset;uint8_t data[512 ];
@@ -421,8 +426,9 @@ static void sd_reset(SDState *sd, BlockDriverState *bdrv)if (sd->wp_groups)g_free (sd->wp_groups); sd->wp_switch = bdrv ?bdrv_is_read_only (bdrv) : false; - sd->wp_groups =bitmap_new (sect); -memset (sd->function_group,0 ,sizeof (int ) *6 ); + sd->wpgrps_size = sect; + sd->wp_groups =bitmap_new (sd->wpgrps_size); +memset (sd->function_group,0 ,sizeof (sd->function_group)); sd->erase_start =0 ; sd->erase_end =0 ; sd->size = size;
@@ -446,6 +452,38 @@ static const BlockDevOps sd_block_ops = { .change_media_cb = sd_cardchange, }; +static const VMStateDescription sd_vmstate = { + .name ="sd-card" , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT32 (mode, SDState), +VMSTATE_INT32 (state, SDState), +VMSTATE_UINT8_ARRAY (cid, SDState,16 ), +VMSTATE_UINT8_ARRAY (csd, SDState,16 ), +VMSTATE_UINT16 (rca, SDState), +VMSTATE_UINT32 (card_status, SDState), +VMSTATE_PARTIAL_BUFFER (sd_status, SDState,1 ), +VMSTATE_UINT32 (vhs, SDState), +VMSTATE_BITMAP (wp_groups, SDState,0 , wpgrps_size), +VMSTATE_UINT32 (blk_len, SDState), +VMSTATE_UINT32 (erase_start, SDState), +VMSTATE_UINT32 (erase_end, SDState), +VMSTATE_UINT8_ARRAY (pwd, SDState,16 ), +VMSTATE_UINT32 (pwd_len, SDState), +VMSTATE_UINT8_ARRAY (function_group, SDState,6 ), +VMSTATE_UINT8 (current_cmd, SDState), +VMSTATE_BOOL (expecting_acmd, SDState), +VMSTATE_UINT32 (blk_written, SDState), +VMSTATE_UINT64 (data_start, SDState), +VMSTATE_UINT32 (data_offset, SDState), +VMSTATE_UINT8_ARRAY (data, SDState,512 ), +VMSTATE_BUFFER_UNSAFE (buf, SDState,1 ,512 ), +VMSTATE_BOOL (enable, SDState), +VMSTATE_END_OF_LIST () + } +}; +/* We do not model the chip select pin, so allow the board to select whether card should be in SSI or MMC/SD mode. It is also up to the board to ensure that ssi transfers only occur when the chip select
@@ -463,6 +501,7 @@ SDState *sd_init(BlockDriverState *bs, bool is_spi)bdrv_attach_dev_nofail (sd->bdrv, sd);bdrv_set_dev_ops (sd->bdrv, &sd_block_ops, sd); } +vmstate_register (NULL ,-1 , &sd_vmstate, sd);return sd; }
@@ -576,7 +615,7 @@ static void sd_lock_command(SDState *sd) sd->card_status |=LOCK_UNLOCK_FAILED ;return ; } -bitmap_zero (sd->wp_groups,sd_addr_to_wpnum (sd->size) +1 ); +bitmap_zero (sd->wp_groups, sd->wpgrps_size); sd->csd[14 ] &= ~0x10 ; sd->card_status &= ~CARD_IS_LOCKED ; sd->pwd_len =0 ;
Signature · 1 line
1.8.0.msysgit.0
Re: [Qemu-devel] [PATCH V5 3/3] hw/sd.c: add SD card save/load support
Quoted 3 lines · Igor Mitsyanko
This patch updates SD card model to support save/load of card's state.
Signed-off-by Igor Mitsyanko <[email protected]>
-- PMM
Quoted 2 lines · Igor Mitsyanko
This was a part of a bigger series which also included SD state qomification, but I decided to split them.
I intend to add these patches to the arm-devs.next queue which I will send a pullreq for tomorrow.
-- PMM