Improve SD controllers emulation
v4 cover Improve SD controllers emulation
Changelogv3->v4
- PATCH 1/3 rebased to current master;
- Fixed .start field use during vmstate save/load;
- added get_bufsize() NULL pointer check;
- patches 2-4 merged into patches 2-3
v2->v3:
- PATCH 2/3 splitted into smaller patches 2-4/5.
- SDState structure rearrengment dropped.
v1->v2:
PATCH 1/3:
- .calc_size field replaced with .get_bufsize field in VMStateField;
- .size_offset removed completely, macros based on it rewritten to use new .get_bufsize field.
PATCH 2/3:
- all binary variables in SDState now have bool type;
- SDState structure rearranged to ensure alignement;
- sd_init(), sd_enable() now receive bool;
- new version of PATCH 1/3 now used to save wp_groups array; PATCH 3/3:
- DMA transfers modified and now operate with data blocks of BLKSIZE only, like real hardware does;
- reset procedure optimized;
- new version of PATCH 1/3 now used to save fifo_buffer.
First patch of this patch set modifies existing VMStateField interface to ease save/restore functionality implementation for device's dynamically allocated buffers. This modification is used later in second patch to implement SD card's VMStateDescription structure.
Fourth patch adds imlementation of new device: SD host controller fully compliant with "SD host controller specification version 2.00". It also uses vmstate field modification introduced in first patch.
Mitsyanko Igor (4):
vmstate: introduce get_bufsize entry in VMStateField hw/sd.c: add SD card save/load support hw/sd.c: convert SD state binary variables to bool
hw: Introduce spec. ver. 2.00 compliant SD host controller
Makefile.target | 1 + hw/g364fb.c | 7 +- hw/m48t59.c | 7 +- hw/mac_nvram.c | 8 +- hw/onenand.c | 7 +- hw/sd.c | 126 +++-- hw/sd.h | 4 +- hw/sdhc.c | 1570 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/sdhc.h | 327 ++++++++++++ savevm.c | 18 +- vmstate.h | 43 +-- 11 files changed, 2030 insertions(+), 88 deletions(-) create mode 100644 hw/sdhc.c create mode 100644 hw/sdhc.h
Signature · 1 line
1.7.4.1
PATCH v4 1/4 vmstate: introduce get_bufsize entry in VMStateField
New get_bufsize field in VMStateField is supposed to help us easily add save/restore support of dynamically allocated buffers in device's states.
There are some cases when information about size of dynamically allocated buffer is already presented in specific device's state structure, but in such a form that can not be used with existing VMStateField interface. Currently, we either can get size from another variable in device's state as it is with VMSTATE_VBUFFER_* macros, or we can also multiply value kept in a variable by a constant with VMSTATE_BUFFER_MULTIPLY macro. If we need to perform any other action, we're forced to add additional variable with size information to device state structure with the only intention to use it in VMStateDescription. This approach is not very pretty. Adding extra flags to VMStateFlags enum for every other possible operation with size field seems redundant, and still it would't cover cases when we need to perform a set of operations to get size value.
With get_bufsize callback we can calculate size of dynamic array in whichever way we need. We don't need .size_offset field anymore, so we can remove it from VMState Field structure to compensate for extra memory consuption because of get_bufsize addition. Macros VMSTATE_VBUFFER* are modified to use new callback instead of .size_offset. Macro VMSTATE_BUFFER_MULTIPLY and VMFlag VMS_MULTIPLY are removed completely as they are now redundant.
--- hw/g364fb.c | 7 ++++++- hw/m48t59.c | 7 ++++++- hw/mac_nvram.c | 8 +++++++- hw/onenand.c | 7 ++++++- savevm.c | 18 ++++++++++++------ vmstate.h | 43 ++++++++----------------------------------- 6 files changed, 45 insertions(+), 45 deletions(-)
hw/g364fb.c +6 -1
@@ -495,6 +495,11 @@ static int g364fb_post_load(void *opaque, int version_id)return 0 ; } +static size_t g364fb_get_vramsize (void *opaque,int version_id) +{ +return ((G364State *)opaque)->vram_size; +} +static const VMStateDescription vmstate_g364fb = { .name ="g364fb" , .version_id =1 ,
@@ -502,7 +507,7 @@ static const VMStateDescription vmstate_g364fb = { .minimum_version_id_old = 1, .post_load = g364fb_post_load, .fields = (VMStateField []) { -VMSTATE_VBUFFER_UINT32 (vram, G364State,1 ,NULL ,0 , vram_size), +VMSTATE_VBUFFER (vram, G364State,1 ,NULL ,0 , g364fb_get_vramsize),VMSTATE_BUFFER_UNSAFE (color_palette, G364State,0 ,256 *3 ),VMSTATE_BUFFER_UNSAFE (cursor_palette, G364State,0 ,9 ),VMSTATE_UINT16_ARRAY (cursor, G364State,512 ),
hw/m48t59.c +6 -1
@@ -582,6 +582,11 @@ static const MemoryRegionOps nvram_ops = { .endianness = DEVICE_NATIVE_ENDIAN, }; +static size_t m48t59_get_bufsize (void *opaque,int version_id) +{ +return ((M48t59State *)opaque)->size; +} +static const VMStateDescription vmstate_m48t59 = { .name ="m48t59" , .version_id =1 ,
@@ -590,7 +595,7 @@ static const VMStateDescription vmstate_m48t59 = { .fields = (VMStateField[]) {VMSTATE_UINT8 (lock, M48t59State),VMSTATE_UINT16 (addr, M48t59State), -VMSTATE_VBUFFER_UINT32 (buffer, M48t59State,0 ,NULL ,0 , size), +VMSTATE_VBUFFER (buffer, M48t59State,0 ,NULL ,0 , m48t59_get_bufsize),VMSTATE_END_OF_LIST () } };
hw/mac_nvram.c +7 -1
@@ -100,13 +100,19 @@ static const MemoryRegionOps macio_nvram_ops = { .endianness = DEVICE_NATIVE_ENDIAN, }; +static size_t macio_nvram_get_datasize (void *opaque,int version_id) +{ +return ((MacIONVRAMState *)opaque)->size; +} +static const VMStateDescription vmstate_macio_nvram = { .name ="macio_nvram" , .version_id =1 , .minimum_version_id =1 , .minimum_version_id_old =1 , .fields = (VMStateField []) { -VMSTATE_VBUFFER_UINT32 (data, MacIONVRAMState,0 ,NULL ,0 , size), +VMSTATE_VBUFFER (data, MacIONVRAMState,0 ,NULL ,0 , + macio_nvram_get_datasize),VMSTATE_END_OF_LIST () } };
hw/onenand.c +6 -1
@@ -160,6 +160,11 @@ static int onenand_post_load(void *opaque, int version_id)return 0 ; } +static size_t onenand_get_blockwpsize (void *opaque,int version_id) +{ +return ((OneNANDState *)opaque)->blocks; +} +static const VMStateDescription vmstate_onenand = { .name ="onenand" , .version_id =1 ,
@@ -181,7 +186,7 @@ static const VMStateDescription vmstate_onenand = {VMSTATE_UINT16 (intstatus, OneNANDState),VMSTATE_UINT16 (wpstatus, OneNANDState),VMSTATE_INT32 (secs_cur, OneNANDState), -VMSTATE_PARTIAL_VBUFFER (blockwp, OneNANDState, blocks), +VMSTATE_PARTIAL_VBUFFER (blockwp, OneNANDState, onenand_get_blockwpsize),VMSTATE_UINT8 (ecc.cp, OneNANDState),VMSTATE_UINT16_ARRAY (ecc.lp, OneNANDState,2 ), VMSTATE_UINT16(ecc.count, OneNANDState),
savevm.c +12 -6
@@ -1413,9 +1413,12 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,int size = field->size;if (field->flags &VMS_VBUFFER ) { - size = *(int32_t *)(opaque+field->size_offset); -if (field->flags &VMS_MULTIPLY ) { - size *= field->size; +if (field->get_bufsize) { + size = field->get_bufsize (opaque, version_id); + size > field->start ? (size -= field->start) : (size =0 ); + }else { +error_report ("%s vmstate load error: couldn't get " +"buffer %s size" , vmsd->name, field->name); } }if (field->flags &VMS_ARRAY ) {
@@ -1477,9 +1480,12 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,int size = field->size;if (field->flags &VMS_VBUFFER ) { - size = *(int32_t *)(opaque+field->size_offset); -if (field->flags &VMS_MULTIPLY ) { - size *= field->size; +if (field->get_bufsize) { + size = field->get_bufsize (opaque, vmsd->version_id); + size > field->start ? (size -= field->start) : (size =0 ); + }else { +error_report ("%s vmstate save error: couldn't get " +"buffer %s size" , vmsd->name, field->name); } }if (field->flags &VMS_ARRAY ) {
vmstate.h +8 -35
76 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v4 2/4 hw/sd.c: add SD card save/load support
In reply to Mitsyanko IgorThis patch updates SD card emulation to support save/load of card's state.
--- hw/sd.c | 102 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 73 insertions(+), 29 deletions(-)
hw/sd.c +73 -29
@@ -54,24 +54,28 @@ typedef enum { sd_illegal =-2 , } sd_rsp_type_t; +enum { + sd_inactive, + sd_card_identification_mode, + sd_data_transfer_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, +}; +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; +int32_t state;uint32_t ocr;uint8_t scr[8 ];uint8_t cid[16 ];
@@ -80,23 +84,23 @@ struct SDState {uint32_t card_status;uint8_t sd_status[64 ];uint32_t vhs; -int wp_switch; -int *wp_groups; +uint32_t wp_switch; +uint32_t *wp_groups;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 ];int 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! */ -int expecting_acmd; -int blk_written; +uint32_t expecting_acmd; +uint32_t blk_written;uint64_t data_start;uint32_t data_offset;uint8_t data[512 ];
@@ -105,7 +109,7 @@ struct SDState {BlockDriverState *bdrv;uint8_t *buf; -int enable; +uint32_t enable; };static void sd_set_mode (SDState *sd)
@@ -415,8 +419,8 @@ 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) :0 ; - sd->wp_groups = (int *)g_malloc0 (sizeof (int ) * sect); -memset (sd->function_group,0 ,sizeof (int ) *6 ); + sd->wp_groups =g_new0 (uint32_t, sect); +memset (sd->function_group,0 ,sizeof (sd->function_group)); sd->erase_start =0 ; sd->erase_end =0 ; sd->size = size;
@@ -440,6 +444,45 @@ static const BlockDevOps sd_block_ops = { .change_media_cb = sd_cardchange, }; +static size_t sd_get_wpgroups_size (void *opaque,int version_id) +{ +SDState *sd = (SDState *)opaque; +return sizeof (uint32_t ) * (sd->size >> (HWBLOCK_SHIFT +SECTOR_SHIFT + +WPGROUP_SHIFT )); +} + +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_VBUFFER (wp_groups, SDState,1 ,NULL ,0 , sd_get_wpgroups_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_UINT32 (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_UINT32 (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
@@ -457,6 +500,7 @@ SDState *sd_init(BlockDriverState *bs, int 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; }
@@ -560,7 +604,7 @@ static void sd_lock_command(SDState *sd) sd->card_status |=LOCK_UNLOCK_FAILED ;return ; } -memset (sd->wp_groups,0 ,sizeof (int ) * (sd->size >> +memset (sd->wp_groups,0 ,sizeof (uint32_t ) * (sd->size >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT ))); sd->csd[14 ] &= ~0x10 ; sd->card_status &= ~CARD_IS_LOCKED ;
Signature · 1 line
1.7.4.1
PATCH v4 3/4 hw/sd.c: convert SD state binary variables to bool
In reply to Mitsyanko IgorSDstate members wp_switch, wp_groups, spi, expecting_acmd and enable are in fact binary variables s converting them to type "bool" makes code a little bit more clear.
API modified to reflect new variables types.
--- hw/sd.c | 42 +++++++++++++++++++++--------------------- hw/sd.h | 4 ++-- 2 files changed, 23 insertions(+), 23 deletions(-)
hw/sd.c +21 -21
@@ -84,8 +84,8 @@ struct SDState {uint32_t card_status;uint8_t sd_status[64 ];uint32_t vhs; -uint32_t wp_switch; -uint32_t *wp_groups; +bool wp_switch; +bool *wp_groups;uint64_t size;uint32_t blk_len;uint32_t erase_start;
@@ -94,12 +94,12 @@ struct SDState {uint32_t pwd_len;uint8_t function_group[6 ]; -int spi; +bool spi;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! */ -uint32_t expecting_acmd; +bool expecting_acmd;uint32_t blk_written;uint64_t data_start;uint32_t data_offset;
@@ -109,7 +109,7 @@ struct SDState {BlockDriverState *bdrv;uint8_t *buf; -uint32_t enable; +bool enable; };static void sd_set_mode (SDState *sd)
@@ -418,15 +418,15 @@ 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) :0 ; - sd->wp_groups =g_new0 (uint32_t, sect); + sd->wp_switch = bdrv ?bdrv_is_read_only (bdrv) : false; + sd->wp_groups =g_new0 (bool, sect);memset (sd->function_group,0 ,sizeof (sd->function_group)); sd->erase_start =0 ; sd->erase_end =0 ; sd->size = size; sd->blk_len =0x200 ; sd->pwd_len =0 ; - sd->expecting_acmd =0 ; + sd->expecting_acmd = false; }static void sd_cardchange (void *opaque,bool load)
@@ -447,7 +447,7 @@ static const BlockDevOps sd_block_ops = {static size_t sd_get_wpgroups_size (void *opaque,int version_id) {SDState *sd = (SDState *)opaque; -return sizeof (uint32_t ) * (sd->size >> (HWBLOCK_SHIFT +SECTOR_SHIFT + +return sizeof (bool ) * (sd->size >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT )); }
@@ -472,13 +472,13 @@ static const VMStateDescription sd_vmstate = { VMSTATE_UINT32(pwd_len, SDState), VMSTATE_UINT8_ARRAY(function_group, SDState, 6), VMSTATE_UINT8(current_cmd, SDState), - VMSTATE_UINT32(expecting_acmd, 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_UINT32(enable, SDState), + VMSTATE_BOOL(enable, SDState),VMSTATE_END_OF_LIST () } };
@@ -487,14 +487,14 @@ static const VMStateDescription sd_vmstate = {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 is asserted. */ -SDState *sd_init (BlockDriverState *bs,int is_spi) +SDState *sd_init (BlockDriverState *bs,bool is_spi) {SDState *sd; sd = (SDState *)g_malloc0 (sizeof (SDState)); sd->buf =qemu_blockalign (bs,512 ); sd->spi = is_spi; - sd->enable =1 ; + sd->enable = true;sd_reset (sd, bs);if (sd->bdrv) {bdrv_attach_dev_nofail (sd->bdrv, sd);
@@ -578,7 +578,7 @@ static void sd_function_switch(SDState *sd, uint32_t arg) sd->data[66 ] = crc &0xff ; } -static inline int sd_wp_addr (SDState *sd,uint32_t addr) +static inline bool sd_wp_addr (SDState *sd,uint32_t addr) {return sd->wp_groups[addr >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT )];
@@ -604,7 +604,7 @@ static void sd_lock_command(SDState *sd) sd->card_status |=LOCK_UNLOCK_FAILED ;return ; } -memset (sd->wp_groups,0 ,sizeof (uint32_t ) * (sd->size >> +memset (sd->wp_groups,0 ,sizeof (bool ) * (sd->size >> (HWBLOCK_SHIFT +SECTOR_SHIFT +WPGROUP_SHIFT ))); sd->csd[14 ] &= ~0x10 ; sd->card_status &= ~CARD_IS_LOCKED ;
@@ -1052,7 +1052,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, sd->state = sd_programming_state; sd->wp_groups[addr >> (HWBLOCK_SHIFT + -SECTOR_SHIFT +WPGROUP_SHIFT )] =1 ; +SECTOR_SHIFT +WPGROUP_SHIFT )] = true;/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;
@@ -1072,7 +1072,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, sd->state = sd_programming_state; sd->wp_groups[addr >> (HWBLOCK_SHIFT + -SECTOR_SHIFT +WPGROUP_SHIFT )] =0 ; +SECTOR_SHIFT +WPGROUP_SHIFT )] = false;/* Bzzzzzzztt .... Operation complete. */ sd->state = sd_transfer_state;return sd_r1b;
@@ -1169,7 +1169,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd,if (sd->rca != rca)return sd_r0; - sd->expecting_acmd =1 ; + sd->expecting_acmd = true; sd->card_status |=APP_CMD ;return sd_r1;
@@ -1351,7 +1351,7 @@ int sd_do_command(SDState *sd, SDRequest *req,if (sd->card_status &CARD_IS_LOCKED ) {if (!cmd_valid_while_locked (sd, req)) { sd->card_status |=ILLEGAL_COMMAND ; - sd->expecting_acmd =0 ; + sd->expecting_acmd = false;fprintf (stderr,"SD: Card is locked\n" ); rtype = sd_illegal; goto send_response;
@@ -1362,7 +1362,7 @@ int sd_do_command(SDState *sd, SDRequest *req,sd_set_mode (sd);if (sd->expecting_acmd) { - sd->expecting_acmd =0 ; + sd->expecting_acmd = false; rtype =sd_app_command (sd, *req); }else { rtype =sd_normal_command (sd, *req);
@@ -1748,7 +1748,7 @@ int sd_data_ready(SDState *sd)return sd->state == sd_sendingdata_state; } -void sd_enable (SDState *sd,int enable) +void sd_enable (SDState *sd,bool enable) { sd->enable = enable; }
hw/sd.h +2 -2
@@ -67,13 +67,13 @@ typedef struct {typedef struct SDState SDState ; -SDState *sd_init (BlockDriverState *bs,int is_spi); +SDState *sd_init (BlockDriverState *bs,bool is_spi);int sd_do_command (SDState *sd,SDRequest *req,uint8_t *response);void sd_write_data (SDState *sd,uint8_t value);uint8_t sd_read_data (SDState *sd);void sd_set_cb (SDState *sd,qemu_irq readonly,qemu_irq insert);int sd_data_ready (SDState *sd); -void sd_enable (SDState *sd,int enable); +void sd_enable (SDState *sd,bool enable);#endif /* __hw_sd_h */
Signature · 1 line
1.7.4.1
PATCH v4 4/4 hw: Introduce spec. ver. 2.00 compliant SD host controller
In reply to Mitsyanko IgorThis patch adds implementation of "SD specification version 2.00" compliant SD host controller. Also it provides interface to implement SoC-specific controllers on top of this specification-compliant one.
--- Makefile.target | 1 + hw/sdhc.c | 1570 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/sdhc.h | 327 ++++++++++++ 3 files changed, 1898 insertions(+), 0 deletions(-) create mode 100644 hw/sdhc.c create mode 100644 hw/sdhc.h
Makefile.target +1 -0
@@ -360,6 +360,7 @@ obj-arm-y += vexpress.o obj-arm-y += strongarm.o obj-arm-y += collie.o obj-arm-y += pl041.o lm4549.o +obj-arm-y += sdhc.o obj-sh4-y = shix.o r2d.o sh7750.o sh7750_regnames.o tc58128.o obj-sh4-y += sh_timer.o sh_serial.o sh_intc.o sh_pci.o sm501.o
hw/sdhc.c new file +1570 -0
1,570 lines not on this page · raw message
hw/sdhc.h new file +327 -0
327 lines not on this page · raw message
Signature · 1 line
1.7.4.1