diff mbox series

[v2,01/13] hw/sd/ssi-sd: Do not create SD card within controller's realize

Message ID 20211117163409.3587705-2-armbru@redhat.com
State New
Headers show
Series Eliminate drive_get_next() | expand

Commit Message

Markus Armbruster Nov. 17, 2021, 4:33 p.m. UTC
ssi_sd_realize() creates an "sd-card" device.  This is inappropriate,
and marked FIXME.

Move it to the boards that create these devices.  Prior art: commit
eb4f566bbb for device "generic-sdhci", and commit 26c607b86b for
device "pl181".

The device remains not user-creatable, because its users should (and
do) wire up its GPIO chip-select line.

Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: Alistair Francis <Alistair.Francis@wdc.com>
Cc: Bin Meng <bin.meng@windriver.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
Cc: qemu-arm@nongnu.org
Cc: qemu-riscv@nongnu.org
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/arm/stellaris.c  | 15 ++++++++++++++-
 hw/riscv/sifive_u.c | 13 ++++++++++++-
 hw/sd/ssi-sd.c      | 29 +----------------------------
 3 files changed, 27 insertions(+), 30 deletions(-)

Comments

Philippe Mathieu-Daudé Nov. 17, 2021, 7:45 p.m. UTC | #1
Hi Markus, Peter,

On 11/17/21 17:33, Markus Armbruster wrote:
> ssi_sd_realize() creates an "sd-card" device.  This is inappropriate,
> and marked FIXME.
> 
> Move it to the boards that create these devices.  Prior art: commit
> eb4f566bbb for device "generic-sdhci", and commit 26c607b86b for
> device "pl181".
> 
> The device remains not user-creatable, because its users should (and
> do) wire up its GPIO chip-select line.
> 
> Cc: Peter Maydell <peter.maydell@linaro.org>
> Cc: Alistair Francis <Alistair.Francis@wdc.com>
> Cc: Bin Meng <bin.meng@windriver.com>
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
> Cc: qemu-arm@nongnu.org
> Cc: qemu-riscv@nongnu.org
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
>  hw/arm/stellaris.c  | 15 ++++++++++++++-
>  hw/riscv/sifive_u.c | 13 ++++++++++++-
>  hw/sd/ssi-sd.c      | 29 +----------------------------
>  3 files changed, 27 insertions(+), 30 deletions(-)
> 
> diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c
> index 78827ace6b..b6c8a5d609 100644
> --- a/hw/arm/stellaris.c
> +++ b/hw/arm/stellaris.c
> @@ -10,6 +10,7 @@
>  #include "qemu/osdep.h"
>  #include "qapi/error.h"
>  #include "hw/sysbus.h"
> +#include "hw/sd/sd.h"
>  #include "hw/ssi/ssi.h"
>  #include "hw/arm/boot.h"
>  #include "qemu/timer.h"
> @@ -1157,6 +1158,9 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
>              void *bus;
>              DeviceState *sddev;
>              DeviceState *ssddev;
> +            DriveInfo *dinfo;
> +            DeviceState *carddev;
> +            BlockBackend *blk;
>  
>              /*
>               * Some boards have both an OLED controller and SD card connected to
> @@ -1221,8 +1225,17 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
>               *  - Make the ssd0323 OLED controller chipselect active-low
>               */
>              bus = qdev_get_child_bus(dev, "ssi");
> -
>              sddev = ssi_create_peripheral(bus, "ssi-sd");
> +
> +            dinfo = drive_get(IF_SD, 0, 0);
> +            blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
> +            carddev = qdev_new(TYPE_SD_CARD);
> +            qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);

I guess a already asked this once but don't remember now... What is the
point of creating a SD card without drive? Is this due to the old design
issue we hotplug the drive to the SD card and not the SD card on the SD
bus?

> +            qdev_prop_set_bit(carddev, "spi", true);
> +            qdev_realize_and_unref(carddev,
> +                                   qdev_get_child_bus(sddev, "sd-bus"),
> +                                   &error_fatal);
> +
Alistair Francis Nov. 19, 2021, 1:04 p.m. UTC | #2
On Thu, Nov 18, 2021 at 2:35 AM Markus Armbruster <armbru@redhat.com> wrote:
>
> ssi_sd_realize() creates an "sd-card" device.  This is inappropriate,
> and marked FIXME.
>
> Move it to the boards that create these devices.  Prior art: commit
> eb4f566bbb for device "generic-sdhci", and commit 26c607b86b for
> device "pl181".
>
> The device remains not user-creatable, because its users should (and
> do) wire up its GPIO chip-select line.
>
> Cc: Peter Maydell <peter.maydell@linaro.org>
> Cc: Alistair Francis <Alistair.Francis@wdc.com>
> Cc: Bin Meng <bin.meng@windriver.com>
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
> Cc: qemu-arm@nongnu.org
> Cc: qemu-riscv@nongnu.org
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  hw/arm/stellaris.c  | 15 ++++++++++++++-
>  hw/riscv/sifive_u.c | 13 ++++++++++++-
>  hw/sd/ssi-sd.c      | 29 +----------------------------
>  3 files changed, 27 insertions(+), 30 deletions(-)
>
> diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c
> index 78827ace6b..b6c8a5d609 100644
> --- a/hw/arm/stellaris.c
> +++ b/hw/arm/stellaris.c
> @@ -10,6 +10,7 @@
>  #include "qemu/osdep.h"
>  #include "qapi/error.h"
>  #include "hw/sysbus.h"
> +#include "hw/sd/sd.h"
>  #include "hw/ssi/ssi.h"
>  #include "hw/arm/boot.h"
>  #include "qemu/timer.h"
> @@ -1157,6 +1158,9 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
>              void *bus;
>              DeviceState *sddev;
>              DeviceState *ssddev;
> +            DriveInfo *dinfo;
> +            DeviceState *carddev;
> +            BlockBackend *blk;
>
>              /*
>               * Some boards have both an OLED controller and SD card connected to
> @@ -1221,8 +1225,17 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
>               *  - Make the ssd0323 OLED controller chipselect active-low
>               */
>              bus = qdev_get_child_bus(dev, "ssi");
> -
>              sddev = ssi_create_peripheral(bus, "ssi-sd");
> +
> +            dinfo = drive_get(IF_SD, 0, 0);
> +            blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
> +            carddev = qdev_new(TYPE_SD_CARD);
> +            qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
> +            qdev_prop_set_bit(carddev, "spi", true);
> +            qdev_realize_and_unref(carddev,
> +                                   qdev_get_child_bus(sddev, "sd-bus"),
> +                                   &error_fatal);
> +
>              ssddev = ssi_create_peripheral(bus, "ssd0323");
>              gpio_out[GPIO_D][0] = qemu_irq_split(
>                      qdev_get_gpio_in_named(sddev, SSI_GPIO_CS, 0),
> diff --git a/hw/riscv/sifive_u.c b/hw/riscv/sifive_u.c
> index 589ae72a59..a4ecadaf12 100644
> --- a/hw/riscv/sifive_u.c
> +++ b/hw/riscv/sifive_u.c
> @@ -46,6 +46,7 @@
>  #include "hw/char/serial.h"
>  #include "hw/cpu/cluster.h"
>  #include "hw/misc/unimp.h"
> +#include "hw/sd/sd.h"
>  #include "hw/ssi/ssi.h"
>  #include "target/riscv/cpu.h"
>  #include "hw/riscv/riscv_hart.h"
> @@ -536,7 +537,8 @@ static void sifive_u_machine_init(MachineState *machine)
>      uint32_t fdt_load_addr;
>      uint64_t kernel_entry;
>      DriveInfo *dinfo;
> -    DeviceState *flash_dev, *sd_dev;
> +    BlockBackend *blk;
> +    DeviceState *flash_dev, *sd_dev, *card_dev;
>      qemu_irq flash_cs, sd_cs;
>
>      /* Initialize SoC */
> @@ -686,6 +688,15 @@ static void sifive_u_machine_init(MachineState *machine)
>
>      sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
>      sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
> +
> +    dinfo = drive_get(IF_SD, 0, 0);
> +    blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
> +    card_dev = qdev_new(TYPE_SD_CARD);
> +    qdev_prop_set_drive_err(card_dev, "drive", blk, &error_fatal);
> +    qdev_prop_set_bit(card_dev, "spi", true);
> +    qdev_realize_and_unref(card_dev,
> +                           qdev_get_child_bus(sd_dev, "sd-bus"),
> +                           &error_fatal);
>  }
>
>  static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
> diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
> index e60854eeef..167c03b780 100644
> --- a/hw/sd/ssi-sd.c
> +++ b/hw/sd/ssi-sd.c
> @@ -368,36 +368,9 @@ static const VMStateDescription vmstate_ssi_sd = {
>
>  static void ssi_sd_realize(SSIPeripheral *d, Error **errp)
>  {
> -    ERRP_GUARD();
>      ssi_sd_state *s = SSI_SD(d);
> -    DeviceState *carddev;
> -    DriveInfo *dinfo;
>
>      qbus_init(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS, DEVICE(d), "sd-bus");
> -
> -    /* Create and plug in the sd card */
> -    /* FIXME use a qdev drive property instead of drive_get_next() */
> -    dinfo = drive_get_next(IF_SD);
> -    carddev = qdev_new(TYPE_SD_CARD);
> -    if (dinfo) {
> -        if (!qdev_prop_set_drive_err(carddev, "drive",
> -                                     blk_by_legacy_dinfo(dinfo), errp)) {
> -            goto fail;
> -        }
> -    }
> -
> -    if (!object_property_set_bool(OBJECT(carddev), "spi", true, errp)) {
> -        goto fail;
> -    }
> -
> -    if (!qdev_realize_and_unref(carddev, BUS(&s->sdbus), errp)) {
> -        goto fail;
> -    }
> -
> -    return;
> -
> -fail:
> -    error_prepend(errp, "failed to init SD card: ");
>  }
>
>  static void ssi_sd_reset(DeviceState *dev)
> @@ -426,7 +399,7 @@ static void ssi_sd_class_init(ObjectClass *klass, void *data)
>      k->cs_polarity = SSI_CS_LOW;
>      dc->vmsd = &vmstate_ssi_sd;
>      dc->reset = ssi_sd_reset;
> -    /* Reason: init() method uses drive_get_next() */
> +    /* Reason: GPIO chip-select line should be wired up */
>      dc->user_creatable = false;
>  }
>
> --
> 2.31.1
>
>
Markus Armbruster Dec. 6, 2021, 12:34 p.m. UTC | #3
Philippe Mathieu-Daudé <f4bug@amsat.org> writes:

> Hi Markus, Peter,
>
> On 11/17/21 17:33, Markus Armbruster wrote:
>> ssi_sd_realize() creates an "sd-card" device.  This is inappropriate,
>> and marked FIXME.
>> 
>> Move it to the boards that create these devices.  Prior art: commit
>> eb4f566bbb for device "generic-sdhci", and commit 26c607b86b for
>> device "pl181".
>> 
>> The device remains not user-creatable, because its users should (and
>> do) wire up its GPIO chip-select line.
>> 
>> Cc: Peter Maydell <peter.maydell@linaro.org>
>> Cc: Alistair Francis <Alistair.Francis@wdc.com>
>> Cc: Bin Meng <bin.meng@windriver.com>
>> Cc: Palmer Dabbelt <palmer@dabbelt.com>
>> Cc: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
>> Cc: qemu-arm@nongnu.org
>> Cc: qemu-riscv@nongnu.org
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>
> Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
>
>> ---
>>  hw/arm/stellaris.c  | 15 ++++++++++++++-
>>  hw/riscv/sifive_u.c | 13 ++++++++++++-
>>  hw/sd/ssi-sd.c      | 29 +----------------------------
>>  3 files changed, 27 insertions(+), 30 deletions(-)
>> 
>> diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c
>> index 78827ace6b..b6c8a5d609 100644
>> --- a/hw/arm/stellaris.c
>> +++ b/hw/arm/stellaris.c
>> @@ -10,6 +10,7 @@
>>  #include "qemu/osdep.h"
>>  #include "qapi/error.h"
>>  #include "hw/sysbus.h"
>> +#include "hw/sd/sd.h"
>>  #include "hw/ssi/ssi.h"
>>  #include "hw/arm/boot.h"
>>  #include "qemu/timer.h"
>> @@ -1157,6 +1158,9 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
>>              void *bus;
>>              DeviceState *sddev;
>>              DeviceState *ssddev;
>> +            DriveInfo *dinfo;
>> +            DeviceState *carddev;
>> +            BlockBackend *blk;
>>  
>>              /*
>>               * Some boards have both an OLED controller and SD card connected to
>> @@ -1221,8 +1225,17 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
>>               *  - Make the ssd0323 OLED controller chipselect active-low
>>               */
>>              bus = qdev_get_child_bus(dev, "ssi");
>> -
>>              sddev = ssi_create_peripheral(bus, "ssi-sd");
>> +
>> +            dinfo = drive_get(IF_SD, 0, 0);
>> +            blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
>> +            carddev = qdev_new(TYPE_SD_CARD);
>> +            qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
>
> I guess a already asked this once but don't remember now... What is the
> point of creating a SD card without drive? Is this due to the old design
> issue we hotplug the drive to the SD card and not the SD card on the SD
> bus?

Device model "sd-card" implements BlockdevOps method change_media_cb().
This menas it models a device with a removable medium.  No drive behaves
like no medium.  I guess it's an SD card reader.

>
>> +            qdev_prop_set_bit(carddev, "spi", true);
>> +            qdev_realize_and_unref(carddev,
>> +                                   qdev_get_child_bus(sddev, "sd-bus"),
>> +                                   &error_fatal);
>> +
Peter Maydell Dec. 6, 2021, 1:05 p.m. UTC | #4
On Mon, 6 Dec 2021 at 12:35, Markus Armbruster <armbru@redhat.com> wrote:
> Philippe Mathieu-Daudé <f4bug@amsat.org> writes:
> > I guess a already asked this once but don't remember now... What is the
> > point of creating a SD card without drive? Is this due to the old design
> > issue we hotplug the drive to the SD card and not the SD card on the SD
> > bus?
>
> Device model "sd-card" implements BlockdevOps method change_media_cb().
> This menas it models a device with a removable medium.  No drive behaves
> like no medium.  I guess it's an SD card reader.

No, device sd-card really is the SD card -- the protocol between
the SD controller device and the sd-card device is (a slightly
abstracted version of) the protocol that goes between the card
and the controller in hardware. I think the reason it's implemented
as a "device with a removable medium" is historical -- back in
2007 that was the only way to support runtime ejecting and
insertion. We'd implement it differently today, but we've wanted
to preserve the user-facing compatibility of how the monitor
commands for ejecting and inserting an sd card work.

(Side note, the initial sd implementation actually tells the block
layer that it's BDRV_TYPE_FLOPPY as a "reasonable approximation"...)

-- PMM
diff mbox series

Patch

diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c
index 78827ace6b..b6c8a5d609 100644
--- a/hw/arm/stellaris.c
+++ b/hw/arm/stellaris.c
@@ -10,6 +10,7 @@ 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "hw/sysbus.h"
+#include "hw/sd/sd.h"
 #include "hw/ssi/ssi.h"
 #include "hw/arm/boot.h"
 #include "qemu/timer.h"
@@ -1157,6 +1158,9 @@  static void stellaris_init(MachineState *ms, stellaris_board_info *board)
             void *bus;
             DeviceState *sddev;
             DeviceState *ssddev;
+            DriveInfo *dinfo;
+            DeviceState *carddev;
+            BlockBackend *blk;
 
             /*
              * Some boards have both an OLED controller and SD card connected to
@@ -1221,8 +1225,17 @@  static void stellaris_init(MachineState *ms, stellaris_board_info *board)
              *  - Make the ssd0323 OLED controller chipselect active-low
              */
             bus = qdev_get_child_bus(dev, "ssi");
-
             sddev = ssi_create_peripheral(bus, "ssi-sd");
+
+            dinfo = drive_get(IF_SD, 0, 0);
+            blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
+            carddev = qdev_new(TYPE_SD_CARD);
+            qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
+            qdev_prop_set_bit(carddev, "spi", true);
+            qdev_realize_and_unref(carddev,
+                                   qdev_get_child_bus(sddev, "sd-bus"),
+                                   &error_fatal);
+
             ssddev = ssi_create_peripheral(bus, "ssd0323");
             gpio_out[GPIO_D][0] = qemu_irq_split(
                     qdev_get_gpio_in_named(sddev, SSI_GPIO_CS, 0),
diff --git a/hw/riscv/sifive_u.c b/hw/riscv/sifive_u.c
index 589ae72a59..a4ecadaf12 100644
--- a/hw/riscv/sifive_u.c
+++ b/hw/riscv/sifive_u.c
@@ -46,6 +46,7 @@ 
 #include "hw/char/serial.h"
 #include "hw/cpu/cluster.h"
 #include "hw/misc/unimp.h"
+#include "hw/sd/sd.h"
 #include "hw/ssi/ssi.h"
 #include "target/riscv/cpu.h"
 #include "hw/riscv/riscv_hart.h"
@@ -536,7 +537,8 @@  static void sifive_u_machine_init(MachineState *machine)
     uint32_t fdt_load_addr;
     uint64_t kernel_entry;
     DriveInfo *dinfo;
-    DeviceState *flash_dev, *sd_dev;
+    BlockBackend *blk;
+    DeviceState *flash_dev, *sd_dev, *card_dev;
     qemu_irq flash_cs, sd_cs;
 
     /* Initialize SoC */
@@ -686,6 +688,15 @@  static void sifive_u_machine_init(MachineState *machine)
 
     sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs);
+
+    dinfo = drive_get(IF_SD, 0, 0);
+    blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
+    card_dev = qdev_new(TYPE_SD_CARD);
+    qdev_prop_set_drive_err(card_dev, "drive", blk, &error_fatal);
+    qdev_prop_set_bit(card_dev, "spi", true);
+    qdev_realize_and_unref(card_dev,
+                           qdev_get_child_bus(sd_dev, "sd-bus"),
+                           &error_fatal);
 }
 
 static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp)
diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
index e60854eeef..167c03b780 100644
--- a/hw/sd/ssi-sd.c
+++ b/hw/sd/ssi-sd.c
@@ -368,36 +368,9 @@  static const VMStateDescription vmstate_ssi_sd = {
 
 static void ssi_sd_realize(SSIPeripheral *d, Error **errp)
 {
-    ERRP_GUARD();
     ssi_sd_state *s = SSI_SD(d);
-    DeviceState *carddev;
-    DriveInfo *dinfo;
 
     qbus_init(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS, DEVICE(d), "sd-bus");
-
-    /* Create and plug in the sd card */
-    /* FIXME use a qdev drive property instead of drive_get_next() */
-    dinfo = drive_get_next(IF_SD);
-    carddev = qdev_new(TYPE_SD_CARD);
-    if (dinfo) {
-        if (!qdev_prop_set_drive_err(carddev, "drive",
-                                     blk_by_legacy_dinfo(dinfo), errp)) {
-            goto fail;
-        }
-    }
-
-    if (!object_property_set_bool(OBJECT(carddev), "spi", true, errp)) {
-        goto fail;
-    }
-
-    if (!qdev_realize_and_unref(carddev, BUS(&s->sdbus), errp)) {
-        goto fail;
-    }
-
-    return;
-
-fail:
-    error_prepend(errp, "failed to init SD card: ");
 }
 
 static void ssi_sd_reset(DeviceState *dev)
@@ -426,7 +399,7 @@  static void ssi_sd_class_init(ObjectClass *klass, void *data)
     k->cs_polarity = SSI_CS_LOW;
     dc->vmsd = &vmstate_ssi_sd;
     dc->reset = ssi_sd_reset;
-    /* Reason: init() method uses drive_get_next() */
+    /* Reason: GPIO chip-select line should be wired up */
     dc->user_creatable = false;
 }