Skip to contentSkip to conversation

Split TYPE_GPIOPWR and reuse in E500 machines

PATCH Superseded Bernhard Beschow 2 participants 6 messages Sep 23, 2024 – Oct 5, 2024 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Split TYPE_GPIOPWR and reuse in E500 machines

This series is part of a bigger series exploring data-driven machine creation using device tree blobs on top of the e500 machines [1]. The idea is to instantiate a QEMU device model for each device tree node containing a compatible property. [1] achieves feature-parity with the hardcoded machines when supplied the same device tree blob that the hardcoded machine would generate.

Just like the ARM virt machine, the ppce500 machine implements a "gpio-poweroff"-compatible device tree node. Unfortunately, the implementation isn't shared which this series fixes. In order to reflect device tree which has separate bindings for gpio-poweroff and gpio-reset, and to prepare for the above, the gpio-pwr device model is split.

Note: If the split seems too fine-grained, the existing gpio-pwr device model could probably be reused in ppce500, too.

Testing done:
* Build qemu_ppc64_e5500_defconfig in Buildroot, run it in the ppce500 machine and issue the poweroff command. Observe that QEMU is shut down cleanly.
* ARM virt: How to test the secure path?

[1] https://github.com/shentok/qemu/tree/e500-fdt

Bernhard Beschow (3):
MAINTAINERS: Add hw/gpio/gpio_pwr.c
hw/gpio/gpio_pwr: Split into separate gpio_poweroff and gpio_restart
devices
hw/ppc/e500: Reuse TYPE_GPIO_POWEROFF

 MAINTAINERS             |  2 ++
 hw/arm/virt.c           | 32 +++++++++++++------
 hw/gpio/gpio_poweroff.c | 51 ++++++++++++++++++++++++++++++
 hw/gpio/gpio_pwr.c      | 70 -----------------------------------------
 hw/gpio/gpio_restart.c  | 51 ++++++++++++++++++++++++++++++
 hw/ppc/e500.c           | 15 ++-------
 hw/arm/Kconfig          |  3 +-
 hw/gpio/Kconfig         |  5 ++-
 hw/gpio/meson.build     |  3 +-
 hw/ppc/Kconfig          |  1 +
 10 files changed, 138 insertions(+), 95 deletions(-)
 create mode 100644 hw/gpio/gpio_poweroff.c
 delete mode 100644 hw/gpio/gpio_pwr.c
 create mode 100644 hw/gpio/gpio_restart.c
Signature · 1 line
2.46.1
#1 lore raw

PATCH 1/3 MAINTAINERS: Add hw/gpio/gpio_pwr.c

The device is only used in the ARM virt machine and designed to be used on top of pl061 for use cases such as ARM Trusted Firmware. Add it to the same section as hw/gpio/pl061.c.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index ffacd60f40..7e0d3509e8 100644
@@ -656,6 +656,7 @@ F: hw/display/pl110*
 F: hw/dma/pl080.c
 F: include/hw/dma/pl080.h
 F: hw/dma/pl330.c
+F: hw/gpio/gpio_pwr.c
 F: hw/gpio/pl061.c
 F: hw/input/pl050.c
 F: include/hw/input/pl050.h
Signature · 1 line
2.46.1
#2 lore raw

PATCH 2/3 hw/gpio/gpio_pwr: Split into separate gpio_poweroff and gpio_restart devices

In reply to Bernhard Beschow

Take inspiration from Linux which has separate device tree bindings for the two GPIO lines. The naming of the two device models matches Linux' compatible properties.

---
 MAINTAINERS             |  3 +-
 hw/arm/virt.c           | 32 +++++++++++++------
 hw/gpio/gpio_poweroff.c | 51 ++++++++++++++++++++++++++++++
 hw/gpio/gpio_pwr.c      | 70 -----------------------------------------
 hw/gpio/gpio_restart.c  | 51 ++++++++++++++++++++++++++++++
 hw/arm/Kconfig          |  3 +-
 hw/gpio/Kconfig         |  5 ++-
 hw/gpio/meson.build     |  3 +-
 8 files changed, 134 insertions(+), 84 deletions(-)
 create mode 100644 hw/gpio/gpio_poweroff.c
 delete mode 100644 hw/gpio/gpio_pwr.c
 create mode 100644 hw/gpio/gpio_restart.c
MAINTAINERS +2 -1
index 7e0d3509e8..8c17d68821 100644
@@ -656,7 +656,8 @@ F: hw/display/pl110*
 F: hw/dma/pl080.c
 F: include/hw/dma/pl080.h
 F: hw/dma/pl330.c
-F: hw/gpio/gpio_pwr.c
+F: hw/gpio/gpio_poweroff.c
+F: hw/gpio/gpio_restart.c
 F: hw/gpio/pl061.c
 F: hw/input/pl050.c
 F: include/hw/input/pl050.h
hw/arm/virt.c +22 -10
index 8b2b991d97..1ce1a1ebc1 100644
@@ -1032,19 +1032,17 @@ static void create_gpio_keys(char *fdt, DeviceState *pl061_dev,
 #define SECURE_GPIO_POWEROFF 0
 #define SECURE_GPIO_RESET    1
 
-static void create_secure_gpio_pwr(char *fdt, DeviceState *pl061_dev,
-                                   uint32_t phandle)
+static void create_secure_gpio_poweroff(char *fdt, DeviceState *pl061_dev,
+                                        uint32_t phandle)
 {
-    DeviceState *gpio_pwr_dev;
+    DeviceState *gpio_poweroff_dev;
 
-    /* gpio-pwr */
-    gpio_pwr_dev = sysbus_create_simple("gpio-pwr", -1, NULL);
+    /* gpio-poweroff */
+    gpio_poweroff_dev = sysbus_create_simple("gpio-poweroff", -1, NULL);
 
-    /* connect secure pl061 to gpio-pwr */
-    qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_RESET,
-                          qdev_get_gpio_in_named(gpio_pwr_dev, "reset", 0));
+    /* connect secure pl061 to gpio-poweroff */
     qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_POWEROFF,
-                          qdev_get_gpio_in_named(gpio_pwr_dev, "shutdown", 0));
+                          qdev_get_gpio_in(gpio_poweroff_dev, 0));
 
     qemu_fdt_add_subnode(fdt, "/gpio-poweroff");
     qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "compatible",
@@ -1054,6 +1052,19 @@ static void create_secure_gpio_pwr(char *fdt, DeviceState *pl061_dev,
     qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "status", "disabled");
     qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "secure-status",
                             "okay");
+}
+
+static void create_secure_gpio_restart(char *fdt, DeviceState *pl061_dev,
+                                       uint32_t phandle)
+{
+    DeviceState *gpio_restart_dev;
+
+    /* gpio-restart */
+    gpio_restart_dev = sysbus_create_simple("gpio-restart", -1, NULL);
+
+    /* connect secure pl061 to gpio-restart */
+    qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_RESET,
+                          qdev_get_gpio_in(gpio_restart_dev, 0));
 
     qemu_fdt_add_subnode(fdt, "/gpio-restart");
     qemu_fdt_setprop_string(fdt, "/gpio-restart", "compatible",
@@ -1112,7 +1123,8 @@ static void create_gpio_devices(const VirtMachineState *vms, int gpio,
     if (gpio == VIRT_GPIO) {
         create_gpio_keys(ms->fdt, pl061_dev, phandle);
     } else {
-        create_secure_gpio_pwr(ms->fdt, pl061_dev, phandle);
+        create_secure_gpio_poweroff(ms->fdt, pl061_dev, phandle);
+        create_secure_gpio_restart(ms->fdt, pl061_dev, phandle);
     }
 }
 
hw/gpio/gpio_poweroff.c new file +51 -0

51 lines not on this page · raw message

hw/gpio/gpio_pwr.c deleted +0 -70

70 lines not on this page · raw message

hw/gpio/gpio_restart.c new file +51 -0

51 lines not on this page · raw message

hw/arm/Kconfig +2 -1
index 1ad60da7aa..2592f8c60b 100644
@@ -23,7 +23,8 @@ config ARM_VIRT
     select PL011 # UART
     select PL031 # RTC
     select PL061 # GPIO
-    select GPIO_PWR
+    select GPIO_POWEROFF
+    select GPIO_RESTART
     select PLATFORM_BUS
     select SMBIOS
     select VIRTIO_MMIO
hw/gpio/Kconfig +4 -1
index 19c97cc823..49cb25400c 100644
@@ -11,7 +11,10 @@ config GPIO_KEY
 config GPIO_MPC8XXX
     bool
 
-config GPIO_PWR
+config GPIO_POWEROFF
+    bool
+
+config GPIO_RESTART
     bool
 
 config SIFIVE_GPIO
hw/gpio/meson.build +2 -1
index a7495d196a..0e12f47ce1 100644
@@ -1,6 +1,7 @@
 system_ss.add(when: 'CONFIG_GPIO_KEY', if_true: files('gpio_key.c'))
 system_ss.add(when: 'CONFIG_GPIO_MPC8XXX', if_true: files('mpc8xxx.c'))
-system_ss.add(when: 'CONFIG_GPIO_PWR', if_true: files('gpio_pwr.c'))
+system_ss.add(when: 'CONFIG_GPIO_POWEROFF', if_true: files('gpio_poweroff.c'))
+system_ss.add(when: 'CONFIG_GPIO_RESTART', if_true: files('gpio_restart.c'))
 system_ss.add(when: 'CONFIG_MAX7310', if_true: files('max7310.c'))
 system_ss.add(when: 'CONFIG_PCA9552', if_true: files('pca9552.c'))
 system_ss.add(when: 'CONFIG_PCA9554', if_true: files('pca9554.c'))
Signature · 1 line
2.46.1
#3 lore raw

PATCH 3/3 hw/ppc/e500: Reuse TYPE_GPIO_POWEROFF

In reply to Bernhard Beschow

The ppce500 machine provides a device tree node whose compatible property is "gpio-poweroff". This matches TYPE_GPIO_POWEROFF like used in the ARM virt machine, so reuse it.

---
 hw/ppc/e500.c  | 15 +++------------
 hw/ppc/Kconfig |  1 +
 2 files changed, 4 insertions(+), 12 deletions(-)
hw/ppc/e500.c +3 -12
index 3bd12b54ab..af74b32c13 100644
@@ -30,7 +30,6 @@
 #include "sysemu/sysemu.h"
 #include "sysemu/kvm.h"
 #include "sysemu/reset.h"
-#include "sysemu/runstate.h"
 #include "kvm_ppc.h"
 #include "sysemu/device_tree.h"
 #include "hw/ppc/openpic.h"
@@ -47,7 +46,6 @@
 #include "hw/platform-bus.h"
 #include "hw/net/fsl_etsec/etsec.h"
 #include "hw/i2c/i2c.h"
-#include "hw/irq.h"
 #include "hw/sd/sdhci.h"
 #include "hw/misc/unimp.h"
 
@@ -892,13 +890,6 @@ static DeviceState *ppce500_init_mpic(PPCE500MachineState *pms,
     return dev;
 }
 
-static void ppce500_power_off(void *opaque, int line, int on)
-{
-    if (on) {
-        qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
-    }
-}
-
 void ppce500_init(MachineState *machine)
 {
     MemoryRegion *address_space_mem = get_system_memory();
@@ -1086,7 +1077,7 @@ void ppce500_init(MachineState *machine)
     sysbus_create_simple("e500-spin", pmc->spin_base, NULL);
 
     if (pmc->has_mpc8xxx_gpio) {
-        qemu_irq poweroff_irq;
+        DeviceState *poweroff_dev;
 
         dev = qdev_new("mpc8xxx_gpio");
         s = SYS_BUS_DEVICE(dev);
@@ -1096,8 +1087,8 @@ void ppce500_init(MachineState *machine)
                                     sysbus_mmio_get_region(s, 0));
 
         /* Power Off GPIO at Pin 0 */
-        poweroff_irq = qemu_allocate_irq(ppce500_power_off, NULL, 0);
-        qdev_connect_gpio_out(dev, 0, poweroff_irq);
+        poweroff_dev = sysbus_create_simple("gpio-poweroff", -1, NULL);
+        qdev_connect_gpio_out(dev, 0, qdev_get_gpio_in(poweroff_dev, 0));
     }
 
     /* Platform Bus Device */
hw/ppc/Kconfig +1 -0
index 5addad1124..31764ef909 100644
@@ -158,6 +158,7 @@ config E500
     imply VIRTIO_PCI
     select ETSEC
     select GPIO_MPC8XXX
+    select GPIO_POWEROFF
     select OPENPIC
     select PFLASH_CFI01
     select PLATFORM_BUS
Signature · 1 line
2.46.1
#4 lore raw
In reply to Bernhard Beschow
Quoted 7 lines · Bernhard Beschow

This series is part of a bigger series exploring data-driven machine creation using device tree blobs on top of the e500 machines [1]. The idea is to instantiate a QEMU device model for each device tree node containing a compatible property. [1] achieves feature-parity with the hardcoded machines when supplied the same device tree blob that the hardcoded machine would generate.

FWIW, on Arm I have generally pushed back against the idea of "create a QEMU machine from a device tree", because the device tree does not in general contain sufficient information to create a QEMU machine. It only has the information that Linux needs to use the devices, which is a subset of the total "how do all these devices exist and get wired together". (There are some special cases like some Xilinx FPGA systems, where both the device tree and the hardware were autogenerated from a common source definition, and so there's enough of a constraint on what the hardware can be to make it workable.)

Quoted 8 lines

Just like the ARM virt machine, the ppce500 machine implements a "gpio-poweroff"-compatible device tree node. Unfortunately, the implementation isn't shared which this series fixes. In order to reflect device tree which has separate bindings for gpio-poweroff and gpio-reset, and to prepare for the above, the gpio-pwr device model is split.

Note: If the split seems too fine-grained, the existing gpio-pwr device model could probably be reused in ppce500, too.

I definitely like getting rid of the hand-coded qemu_allocate_irq() in e500.c. But I don't really see the benefit of splitting gpio_pwr into two devices. If you only need the power-off behaviour, you can leave the restart gpio input not connected to anything.

thanks
-- PMM

#5 lore raw

Am 30. September 2024 12:57:17 UTC schrieb Peter Maydell <[email protected]>:

Quoted 26 lines · Bernhard Beschow
Quoted 7 lines

This series is part of a bigger series exploring data-driven machine creation using device tree blobs on top of the e500 machines [1]. The idea is to instantiate a QEMU device model for each device tree node containing a compatible property. [1] achieves feature-parity with the hardcoded machines when supplied the same device tree blob that the hardcoded machine would generate.

FWIW, on Arm I have generally pushed back against the idea of "create a QEMU machine from a device tree", because the device tree does not in general contain sufficient information to create a QEMU machine. It only has the information that Linux needs to use the devices, which is a subset of the total "how do all these devices exist and get wired together". (There are some special cases like some Xilinx FPGA systems, where both the device tree and the hardware were autogenerated from a common source definition, and so there's enough of a constraint on what the hardware can be to make it workable.)

Quoted 6 lines

Just like the ARM virt machine, the ppce500 machine implements a "gpio-poweroff"-compatible device tree node. Unfortunately, the implementation isn't shared which this series fixes. In order to reflect device tree which has separate bindings for gpio-poweroff and gpio-reset, and to prepare for the above, the gpio-pwr device model is split.

Quoted 8 lines
Quoted 2 lines

Note: If the split seems too fine-grained, the existing gpio-pwr device model could probably be reused in ppce500, too.

I definitely like getting rid of the hand-coded qemu_allocate_irq() in e500.c. But I don't really see the benefit of splitting gpio_pwr into two devices. If you only need the power-off behaviour, you can leave the restart gpio input not connected to anything.

New series sent which removes qemu_allocate_irq() usage in e500.c: https://lore.kernel.org/qemu-devel/[email protected]/

Best regards,
Bernhard

Quoted 3 lines

thanks
-- PMM