Skip to contentSkip to conversation

handle reparenting of SD cards for rpi2

PATCH In review Peter Maydell 3 participants 7 messages Feb 28, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover handle reparenting of SD cards for rpi2

These patches are a respin of Clement's work on the rpi2 sd controller/gpio. They fix the valgrind issue that I was seeing because we got reparenting of the card wrong.

This patchset adds a new qdev patch which makes the qdev_set_parent_bus() function do most of the work of unplugging and replugging if the device was already on a bus. In particular it calls bus_remove_child() which does a lot of things that the previous "just call object_unparent" attempt didn't do.

Comments on whether this makes sense to people welcome. I'm still kind of hoping we can squeeze this into 2.9; comments on whether that would be unwise also welcome.

thanks
-- PMM

Clement Deschamps (3):
hw/sd: add card-reparenting function
bcm2835_gpio: add bcm2835 gpio controller
bcm2835: add sdhost and gpio controllers

Peter Maydell (1):
qdev: Have qdev_set_parent_bus() handle devices already on a bus

 hw/gpio/Makefile.objs                |   1 +
 include/hw/arm/bcm2835_peripherals.h |   4 +
 include/hw/gpio/bcm2835_gpio.h       |  39 ++++
 include/hw/sd/sd.h                   |  11 ++
 hw/arm/bcm2835_peripherals.c         |  43 ++++-
 hw/core/qdev.c                       |  14 ++
 hw/gpio/bcm2835_gpio.c               | 353 +++++++++++++++++++++++++++++++++++
 hw/sd/core.c                         |  27 +++
 8 files changed, 490 insertions(+), 2 deletions(-)
 create mode 100644 include/hw/gpio/bcm2835_gpio.h
 create mode 100644 hw/gpio/bcm2835_gpio.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/4 qdev: Have qdev_set_parent_bus() handle devices already on a bus

Instead of qdev_set_parent_bus() silently doing the wrong thing if it's handed a device that's already on a bus, have it remove the device from the old bus and add it to the new one. This is useful for the raspi2 sdcard.

---
 hw/core/qdev.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/core/qdev.c +14 -0
index 06ba02e..923e626 100644
@@ -102,9 +102,23 @@ static void bus_add_child(BusState *bus, DeviceState *child)
 
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
+    bool replugging = dev->parent_bus != NULL;
+
+    if (replugging) {
+        /* Keep a reference to the device while it's not plugged into
+         * any bus, to avoid it potentially evaporating when it is
+         * dereffed in bus_remove_child().
+         */
+        object_ref(OBJECT(dev));
+        bus_remove_child(dev->parent_bus, dev);
+        object_unref(OBJECT(dev->parent_bus));
+    }
     dev->parent_bus = bus;
     object_ref(OBJECT(bus));
     bus_add_child(bus, dev);
+    if (replugging) {
+        object_unref(OBJECT(dev));
+    }
 }
 
 /* Create a new device.  This only initializes the device state
Signature · 1 line
2.7.4
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 1/4] qdev: Have qdev_set_parent_bus() handle devices already on a bus

Quoted 6 lines · On Tue, 28 Feb 2017 14:55:08 +0000 Peter Maydell <[email protected]>…

Instead of qdev_set_parent_bus() silently doing the wrong thing if it's handed a device that's already on a bus, have it remove the device from the old bus and add it to the new one. This is useful for the raspi2 sdcard.

Quoted 32 lines
---
 hw/core/qdev.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/core/qdev.c +14 -0
index 06ba02e..923e626 100644
@@ -102,9 +102,23 @@ static void bus_add_child(BusState *bus, DeviceState *child)
 
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
+    bool replugging = dev->parent_bus != NULL;
+
+    if (replugging) {
+        /* Keep a reference to the device while it's not plugged into
+         * any bus, to avoid it potentially evaporating when it is
+         * dereffed in bus_remove_child().
+         */
+        object_ref(OBJECT(dev));
+        bus_remove_child(dev->parent_bus, dev);
+        object_unref(OBJECT(dev->parent_bus));
+    }
     dev->parent_bus = bus;
     object_ref(OBJECT(bus));
     bus_add_child(bus, dev);
+    if (replugging) {
+        object_unref(OBJECT(dev));
+    }
 }
 
 /* Create a new device.  This only initializes the device state
#3 lore raw

PATCH 2/4 hw/sd: add card-reparenting function

In reply to Peter Maydell

From: Clement Deschamps <[email protected]>

Provide a new function sdbus_reparent_card() in sd core for reparenting a card from a SDBus to another one.

This function is required by the raspi platform, where the two SD controllers can be dynamically switched.

Message-id: [email protected]

[PMM: added a doc comment to the header file; changed to
use new behaviour of qdev_set_parent_bus()]

---
 include/hw/sd/sd.h | 11 +++++++++++
 hw/sd/core.c       | 27 +++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
include/hw/sd/sd.h +11 -0
index 79909b2..96caefe 100644
@@ -140,6 +140,17 @@ uint8_t sdbus_read_data(SDBus *sd);
 bool sdbus_data_ready(SDBus *sd);
 bool sdbus_get_inserted(SDBus *sd);
 bool sdbus_get_readonly(SDBus *sd);
+/**
+ * sdbus_reparent_card: Reparent an SD card from one controller to another
+ * @from: controller bus to remove card from
+ * @to: controller bus to move card to
+ *
+ * Reparent an SD card, effectively unplugging it from one controller
+ * and inserting it into another. This is useful for SoCs like the
+ * bcm2835 which have two SD controllers and connect a single SD card
+ * to them, selected by the guest reprogramming GPIO line routing.
+ */
+void sdbus_reparent_card(SDBus *from, SDBus *to);
 
 /* Functions to be used by SD devices to report back to qdevified controllers */
 void sdbus_set_inserted(SDBus *sd, bool inserted);
hw/sd/core.c +27 -0
index 14c2bdf..295dc44 100644
@@ -131,6 +131,33 @@ void sdbus_set_readonly(SDBus *sdbus, bool readonly)
     }
 }
 
+void sdbus_reparent_card(SDBus *from, SDBus *to)
+{
+    SDState *card = get_card(from);
+    SDCardClass *sc;
+    bool readonly;
+
+    /* We directly reparent the card object rather than implementing this
+     * as a hotpluggable connection because we don't want to expose SD cards
+     * to users as being hotpluggable, and we can get away with it in this
+     * limited use case. This could perhaps be implemented more cleanly in
+     * future by adding support to the hotplug infrastructure for "device
+     * can be hotplugged only via code, not by user".
+     */
+
+    if (!card) {
+        return;
+    }
+
+    sc = SD_CARD_GET_CLASS(card);
+    readonly = sc->get_readonly(card);
+
+    sdbus_set_inserted(from, false);
+    qdev_set_parent_bus(DEVICE(card), &to->qbus);
+    sdbus_set_inserted(to, true);
+    sdbus_set_readonly(to, readonly);
+}
+
 static const TypeInfo sd_bus_info = {
     .name = TYPE_SD_BUS,
     .parent = TYPE_BUS,
Signature · 1 line
2.7.4
#4 lore raw

PATCH 3/4 bcm2835_gpio: add bcm2835 gpio controller

In reply to Peter Maydell

From: Clement Deschamps <[email protected]>

This adds the BCM2835 GPIO controller.

It currently implements:
- The 54 GPIOs as outputs (qemu_irq)
- The SD controller selection via alternate function of GPIOs 48-53

Message-id: [email protected]

---
 hw/gpio/Makefile.objs          |   1 +
 include/hw/gpio/bcm2835_gpio.h |  39 +++++
 hw/gpio/bcm2835_gpio.c         | 353 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 393 insertions(+)
 create mode 100644 include/hw/gpio/bcm2835_gpio.h
 create mode 100644 hw/gpio/bcm2835_gpio.c
hw/gpio/Makefile.objs +1 -0
index a43c7cf..fa0a72e 100644
@@ -7,3 +7,4 @@ common-obj-$(CONFIG_GPIO_KEY) += gpio_key.o
 
 obj-$(CONFIG_OMAP) += omap_gpio.o
 obj-$(CONFIG_IMX) += imx_gpio.o
+obj-$(CONFIG_RASPI) += bcm2835_gpio.o
include/hw/gpio/bcm2835_gpio.h new file +39 -0
new file mode 100644
index 0000000..9f8e0c7
@@ -0,0 +1,39 @@
+/*
+ * Raspberry Pi (BCM2835) GPIO Controller
+ *
+ * Copyright (c) 2017 Antfield SAS
+ *
+ * Authors:
+ *  Clement Deschamps <[email protected]>
+ *  Luc Michel <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef BCM2835_GPIO_H
+#define BCM2835_GPIO_H
+
+#include "hw/sd/sd.h"
+
+typedef struct BCM2835GpioState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion iomem;
+
+    /* SDBus selector */
+    SDBus sdbus;
+    SDBus *sdbus_sdhci;
+    SDBus *sdbus_sdhost;
+
+    uint8_t fsel[54];
+    uint32_t lev0, lev1;
+    uint8_t sd_fsel;
+    qemu_irq out[54];
+} BCM2835GpioState;
+
+#define TYPE_BCM2835_GPIO "bcm2835_gpio"
+#define BCM2835_GPIO(obj) \
+    OBJECT_CHECK(BCM2835GpioState, (obj), TYPE_BCM2835_GPIO)
+
+#endif
hw/gpio/bcm2835_gpio.c new file +353 -0

353 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

PATCH 4/4 bcm2835: add sdhost and gpio controllers

In reply to Peter Maydell

From: Clement Deschamps <[email protected]>

This adds the bcm2835_sdhost and bcm2835_gpio to the BCM2835 platform.

For supporting the SD controller selection (alternate function of GPIOs 48-53), the bcm2835_gpio now exposes an sdbus.
It also has a link to both the sdbus of sdhci and sdhost controllers, and the card is reparented from one bus to another when the alternate function of GPIOs 48-53 is modified.

Message-id: [email protected]

---
 include/hw/arm/bcm2835_peripherals.h |  4 ++++
 hw/arm/bcm2835_peripherals.c         | 43 ++++++++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
include/hw/arm/bcm2835_peripherals.h +4 -0
index 31241c7..122b286 100644
@@ -22,6 +22,8 @@
 #include "hw/misc/bcm2835_rng.h"
 #include "hw/misc/bcm2835_mbox.h"
 #include "hw/sd/sdhci.h"
+#include "hw/sd/bcm2835_sdhost.h"
+#include "hw/gpio/bcm2835_gpio.h"
 
 #define TYPE_BCM2835_PERIPHERALS "bcm2835-peripherals"
 #define BCM2835_PERIPHERALS(obj) \
@@ -45,6 +47,8 @@ typedef struct BCM2835PeripheralState {
     BCM2835RngState rng;
     BCM2835MboxState mboxes;
     SDHCIState sdhci;
+    BCM2835SDHostState sdhost;
+    BCM2835GpioState gpio;
 } BCM2835PeripheralState;
 
 #endif /* BCM2835_PERIPHERALS_H */
hw/arm/bcm2835_peripherals.c +41 -2

72 lines not on this page · raw message

Signature · 1 line
2.7.4
#6 lore raw
In reply to Peter Maydell
Quoted 11 lines · Peter Maydell

These patches are a respin of Clement's work on the rpi2 sd controller/gpio. They fix the valgrind issue that I was seeing because we got reparenting of the card wrong.

This patchset adds a new qdev patch which makes the qdev_set_parent_bus() function do most of the work of unplugging and replugging if the device was already on a bus. In particular it calls bus_remove_child() which does a lot of things that the previous "just call object_unparent" attempt didn't do.

Quoted 3 lines

Comments on whether this makes sense to people welcome. I'm still kind of hoping we can squeeze this into 2.9; comments on whether that would be unwise also welcome.

Looks good. My first reaction was to add an assertion in qdev_set_parent_bus, modify it to handle qdev_set_parent_bus(dev, NULL), and then do a two-step unplug+replug in the raspi device models.

However, who knows what you could unleash when adding that assertion...

Paolo