Skip to contentSkip to conversation

sdhci: Implement device reset

PATCH In review Peter Maydell 3 participants 5 messages Feb 26, 2016

lore
Share & tools
lore
#0 lore raw

cover sdhci: Implement device reset

This patchset implements the DeviceClass reset method for the sdhci devices, so that on QEMU system reset they are reset. (Mostly guests didn't notice this was missing I think because they tend to do a commanded reset via the device register as part of driver initialization.)

For the reset patch to work and not crash QEMU on startup, we need to fix a missing NULL pointer check in sd.c's sd_get_inserted() function.

These patchsets apply on top of Andrew's rpi quirk patchset which is in target-arm.next.

Review/testing appreciated.

Peter Maydell (2):
sd.c: Handle NULL block backend in sd_get_inserted()
sdhci: Implement DeviceClass reset

 hw/sd/sd.c    |  2 +-
 hw/sd/sdhci.c | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 5 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 sd.c: Handle NULL block backend in sd_get_inserted()

The sd.c SD card emulation code can be in a state where the SDState BlockBackend pointer is NULL; this is treated as "card not present". Add a missing check to sd_get_inserted() so that we don't segfault in this situation.

(This could be provoked by the guest writing to the SDHCI register to do a reset on a xilinx-zynq-a9 board; it will also happen at startup when sdhci implements its DeviceClass reset method.)

---
 hw/sd/sd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sd.c +1 -1
index edb6b32..00c320d 100644
@@ -449,7 +449,7 @@ static void sd_reset(DeviceState *dev)
 
 static bool sd_get_inserted(SDState *sd)
 {
-    return blk_is_inserted(sd->blk);
+    return sd->blk && blk_is_inserted(sd->blk);
 }
 
 static bool sd_get_readonly(SDState *sd)
Signature · 1 line
1.9.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/2] sd.c: Handle NULL block backend in sd_get_inserted()

Quoted 11 lines · Peter Maydell

The sd.c SD card emulation code can be in a state where the SDState BlockBackend pointer is NULL; this is treated as "card not present". Add a missing check to sd_get_inserted() so that we don't segfault in this situation.

(This could be provoked by the guest writing to the SDHCI register to do a reset on a xilinx-zynq-a9 board; it will also happen at startup when sdhci implements its DeviceClass reset method.)

Thanks,

Alistair

Quoted 21 lines
---
 hw/sd/sd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sd.c +1 -2
index edb6b32..00c320d 100644
@@ -449,7 +449,7 @@ static void sd_reset(DeviceState *dev)
 
 static bool sd_get_inserted(SDState *sd)
 {
-    return blk_is_inserted(sd->blk);
+    return sd->blk && blk_is_inserted(sd->blk);
 }
 
 static bool sd_get_readonly(SDState *sd)
--

1.9.1

#3 lore raw

PATCH 2/2 sdhci: Implement DeviceClass reset

In reply to Peter Maydell

The sdhci device was missing a DeviceClass reset method; implement it. Poweron reset looks the same as reset commanded by the guest via the device registers, apart from modelling of the rpi 'pending insert interrupt on powerup' quirk.

---
 hw/sd/sdhci.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
hw/sd/sdhci.c +17 -4
index e087c17..d28b587 100644
@@ -207,6 +207,21 @@ static void sdhci_reset(SDHCIState *s)
     s->pending_insert_state = false;
 }
 
+static void sdhci_poweron_reset(DeviceState *dev)
+{
+    /* QOM (ie power-on) reset. This is identical to reset
+     * commanded via device register apart from handling of the
+     * 'pending insert on powerup' quirk.
+     */
+    SDHCIState *s = (SDHCIState *)dev;
+
+    sdhci_reset(s);
+
+    if (s->pending_insert_quirk) {
+        s->pending_insert_state = true;
+    }
+}
+
 static void sdhci_data_transfer(void *opaque);
 
 static void sdhci_send_command(SDHCIState *s)
@@ -1290,6 +1305,7 @@ static void sdhci_pci_class_init(ObjectClass *klass, void *data)
     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
     dc->vmsd = &sdhci_vmstate;
     dc->props = sdhci_pci_properties;
+    dc->reset = sdhci_poweron_reset;
 }
 
 static const TypeInfo sdhci_pci_info = {
@@ -1332,10 +1348,6 @@ static void sdhci_sysbus_realize(DeviceState *dev, Error ** errp)
     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s, "sdhci",
             SDHC_REGISTERS_MAP_SIZE);
     sysbus_init_mmio(sbd, &s->iomem);
-
-    if (s->pending_insert_quirk) {
-        s->pending_insert_state = true;
-    }
 }
 
 static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
@@ -1345,6 +1357,7 @@ static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)
     dc->vmsd = &sdhci_vmstate;
     dc->props = sdhci_sysbus_properties;
     dc->realize = sdhci_sysbus_realize;
+    dc->reset = sdhci_poweron_reset;
 }
 
 static const TypeInfo sdhci_sysbus_info = {
Signature · 1 line
1.9.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] sdhci: Implement DeviceClass reset

Quoted 69 lines

From: Peter Maydell [mailto:[email protected]]
Sent: Friday, 26 February 2016 5:24 AM

The sdhci device was missing a DeviceClass reset method; implement it. Poweron reset looks the same as reset commanded by the guest via the device registers, apart from modelling of the rpi 'pending insert interrupt on powerup' quirk.

---
 hw/sd/sdhci.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
hw/sd/sdhci.c +16 -0
index e087c17..d28b587 100644
@@ -207,6 +207,21 @@ static void sdhci_reset(SDHCIState *s)
     s->pending_insert_state = false;
 }
 
+static void sdhci_poweron_reset(DeviceState *dev)
+{
+    /* QOM (ie power-on) reset. This is identical to reset
+     * commanded via device register apart from handling of the
+     * 'pending insert on powerup' quirk.
+     */
+    SDHCIState *s = (SDHCIState *)dev;
+
+    sdhci_reset(s);
+
+    if (s->pending_insert_quirk) {
+        s->pending_insert_state = true;
+    }
+}
+
 static void sdhci_data_transfer(void *opaque);
 
 static void sdhci_send_command(SDHCIState *s)
@@ -1290,6 +1305,7 @@ static void sdhci_pci_class_init(ObjectClass *klass,
 void *data)
     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
     dc->vmsd = &sdhci_vmstate;
     dc->props = sdhci_pci_properties;
+    dc->reset = sdhci_poweron_reset;
 }
 

static const TypeInfo sdhci_pci_info = {

hw/sd/sdhci.c +0 -4
@@ -1332,10 +1348,6 @@ static void sdhci_sysbus_realize(DeviceState *dev,
 Error ** errp)
     memory_region_init_io(&s->iomem, OBJECT(s), &sdhci_mmio_ops, s,
 "sdhci",
             SDHC_REGISTERS_MAP_SIZE);
     sysbus_init_mmio(sbd, &s->iomem);
-
-    if (s->pending_insert_quirk) {
-        s->pending_insert_state = true;
-    }
 }

static void sdhci_sysbus_class_init(ObjectClass *klass, void *data)

hw/sd/sdhci.c +1 -0
@@ -1345,6 +1357,7 @@ static void sdhci_sysbus_class_init(ObjectClass
 *klass, void *data)
     dc->vmsd = &sdhci_vmstate;
     dc->props = sdhci_sysbus_properties;
     dc->realize = sdhci_sysbus_realize;
+    dc->reset = sdhci_poweron_reset;
 }
 
Quoted 1 line

static const TypeInfo sdhci_sysbus_info = {

Not tested, but the handling of the Pi quirk looks good to me, and it's trivial.

BTW, now that you've done this, we could unify sdhci_pci_properties and sdhci_sysbus_properties. Let me know if you'd like a patch for that.

Cheers,
Andrew