Skip to contentSkip to conversation

PPC: E500: Add power off GPIO

PATCH New Alexander Graf 1 participant 4 messages Oct 1, 2014

lore
Share & tools
lore
#0 lore raw

cover PPC: E500: Add power off GPIO

When running a virtual machine, you eventually want to shut it down and see it properly end the QEMU process. The magic that makes this work is the "power off" code path that a guest OS uses to indicate that it wants to power off the host machine.

This logic was missing from our e500 virt machine. The easiest way to implement this is to expose a GPIO to the guest that allows the virtual machine to indicate to QEMU that it wants to power down now.

To expose this GPIO to the guest, we need to add a GPIO controller, the GPIO hookup and some description in device tree. This is what this patch set does.

We only support this for the e500 virt machine though, as the mpc8544ds does not support power off from an OS.

To make use of this logic, you will also need patches to Linux that enable it to register the gpio-poweroff driver:

https://lists.ozlabs.org/pipermail/linuxppc-dev/2014-October/121532.html

And you need to make sure that the following options are enabled in your guest kernel:

CONFIG_POWER_SUPPLY=y
CONFIG_POWER_RESET=y
CONFIG_POWER_RESET_GPIO=y

Alexander Graf (3):
PPC: Add MPC8XXX gpio controller
PPC: E500: Instantiate MPC8XXX gpio controller on virt machine
PPC: E500: Hook up power off GPIO to GPIO controller

 hw/gpio/Makefile.objs |   1 +
 hw/gpio/mpc8xxx.c     | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/e500.c         |  56 +++++++++++++
 hw/ppc/e500.h         |   1 +
 hw/ppc/e500plat.c     |   1 +
 5 files changed, 276 insertions(+)
 create mode 100644 hw/gpio/mpc8xxx.c
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/3 PPC: Add MPC8XXX gpio controller

On e500 systems most SoCs implement a common GPIO controller that Linux calls the "mpc8xxx" gpio controller. This patch adds an emulation model for this device.

---
 hw/gpio/Makefile.objs |   1 +
 hw/gpio/mpc8xxx.c     | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 218 insertions(+)
 create mode 100644 hw/gpio/mpc8xxx.c
hw/gpio/Makefile.objs +1 -0
index 2c8b51f..1abcf17 100644
@@ -2,5 +2,6 @@ common-obj-$(CONFIG_MAX7310) += max7310.o
 common-obj-$(CONFIG_PL061) += pl061.o
 common-obj-$(CONFIG_PUV3) += puv3_gpio.o
 common-obj-$(CONFIG_ZAURUS) += zaurus.o
+common-obj-$(CONFIG_E500) += mpc8xxx.o
 
 obj-$(CONFIG_OMAP) += omap_gpio.o
hw/gpio/mpc8xxx.c new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/3 PPC: E500: Instantiate MPC8XXX gpio controller on virt machine

In reply to Alexander Graf

With the e500 virt machine, we don't have to adhere to the exact hardware layout of an mpc8544ds board. So there we can just add a qoriq compatible GPIO controller into the system that we can add a power off hook to.

---
 hw/ppc/e500.c     | 32 ++++++++++++++++++++++++++++++++
 hw/ppc/e500.h     |  1 +
 hw/ppc/e500plat.c |  1 +
 3 files changed, 34 insertions(+)
hw/ppc/e500.c +32 -0
index 22cfeac..db82f72 100644
@@ -64,6 +64,8 @@
 #define MPC8544_PCI_IO             0xE1000000ULL
 #define MPC8544_UTIL_OFFSET        0xe0000ULL
 #define MPC8544_SPIN_BASE          0xEF000000ULL
+#define MPC8XXX_GPIO_OFFSET        0x000FF000ULL
+#define MPC8XXX_GPIO_IRQ           43
 
 struct boot_info
 {
@@ -170,6 +172,23 @@ static int create_devtree_etsec(SysBusDevice *sbdev, PlatformDevtreeData *data)
     return 0;
 }
 
+static void create_dt_mpc8xxx_gpio(void *fdt, const char *soc, const char *mpic)
+{
+    hwaddr mmio0 = MPC8XXX_GPIO_OFFSET;
+    int irq0 = MPC8XXX_GPIO_IRQ;
+    gchar *node = g_strdup_printf("%s/gpio@%"PRIx64, soc, mmio0);
+
+    qemu_fdt_add_subnode(fdt, node);
+    qemu_fdt_setprop_string(fdt, node, "compatible", "fsl,qoriq-gpio");
+    qemu_fdt_setprop_cells(fdt, node, "reg", mmio0, 0x1000);
+    qemu_fdt_setprop_cells(fdt, node, "interrupts", irq0, 0x2);
+    qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", mpic);
+    qemu_fdt_setprop_cells(fdt, node, "#gpio-cells", 2);
+    qemu_fdt_setprop(fdt, node, "gpio-controller", NULL, 0);
+
+    g_free(node);
+}
+
 static int sysbus_device_create_devtree(SysBusDevice *sbdev, void *opaque)
 {
     PlatformDevtreeData *data = opaque;
@@ -490,6 +509,10 @@ static int ppce500_load_device_tree(MachineState *machine,
     qemu_fdt_setprop_cell(fdt, pci, "#address-cells", 3);
     qemu_fdt_setprop_string(fdt, "/aliases", "pci0", pci);
 
+    if (params->has_mpc8xxx_gpio) {
+        create_dt_mpc8xxx_gpio(fdt, soc, mpic);
+    }
+
     if (params->has_platform_bus) {
         platform_bus_create_devtree(params, fdt, mpic);
     }
@@ -912,6 +935,15 @@ void ppce500_init(MachineState *machine, PPCE500Params *params)
                                     sysbus_mmio_get_region(s, 0));
     }
 
+    if (params->has_mpc8xxx_gpio) {
+        dev = qdev_create(NULL, "mpc8xxx_gpio");
+        s = SYS_BUS_DEVICE(dev);
+        qdev_init_nofail(dev);
+        sysbus_connect_irq(s, 0, mpic[MPC8XXX_GPIO_IRQ]);
+        memory_region_add_subregion(ccsr_addr_space, MPC8XXX_GPIO_OFFSET,
+                                    sysbus_mmio_get_region(s, 0));
+    }
+
     /* Load kernel. */
     if (machine->kernel_filename) {
         kernel_base = cur_base;
hw/ppc/e500.h +1 -0
index f1b2766..08a4c3d 100644
@@ -16,6 +16,7 @@ typedef struct PPCE500Params {
     hwaddr platform_bus_size;
     int platform_bus_first_irq;
     int platform_bus_num_irqs;
+    bool has_mpc8xxx_gpio;
 } PPCE500Params;
 
 void ppce500_init(MachineState *machine, PPCE500Params *params);
hw/ppc/e500plat.c +1 -0
index befe1d1..2d14d58 100644
@@ -40,6 +40,7 @@ static void e500plat_init(MachineState *machine)
         .platform_bus_size = (128ULL * 1024 * 1024),
         .platform_bus_first_irq = 5,
         .platform_bus_num_irqs = 10,
+        .has_mpc8xxx_gpio = true,
     };
 
     /* Older KVM versions don't support EPR which breaks guests when we announce
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH 3/3 PPC: E500: Hook up power off GPIO to GPIO controller

In reply to Alexander Graf

Now that we have a working GPIO controller on the virt machine, we can use one pin to notify QEMU that the guests wants to power off the system.

---
 hw/ppc/e500.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
hw/ppc/e500.c +24 -0
index db82f72..cc12f90 100644
@@ -177,6 +177,8 @@ static void create_dt_mpc8xxx_gpio(void *fdt, const char *soc, const char *mpic)
     hwaddr mmio0 = MPC8XXX_GPIO_OFFSET;
     int irq0 = MPC8XXX_GPIO_IRQ;
     gchar *node = g_strdup_printf("%s/gpio@%"PRIx64, soc, mmio0);
+    gchar *poweroff = g_strdup_printf("%s/power-off", soc);
+    int gpio_ph;
 
     qemu_fdt_add_subnode(fdt, node);
     qemu_fdt_setprop_string(fdt, node, "compatible", "fsl,qoriq-gpio");
@@ -185,8 +187,17 @@ static void create_dt_mpc8xxx_gpio(void *fdt, const char *soc, const char *mpic)
     qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", mpic);
     qemu_fdt_setprop_cells(fdt, node, "#gpio-cells", 2);
     qemu_fdt_setprop(fdt, node, "gpio-controller", NULL, 0);
+    gpio_ph = qemu_fdt_alloc_phandle(fdt);
+    qemu_fdt_setprop_cell(fdt, node, "phandle", gpio_ph);
+    qemu_fdt_setprop_cell(fdt, node, "linux,phandle", gpio_ph);
+
+    /* Power Off Pin */
+    qemu_fdt_add_subnode(fdt, poweroff);
+    qemu_fdt_setprop_string(fdt, poweroff, "compatible", "gpio-poweroff");
+    qemu_fdt_setprop_cells(fdt, poweroff, "gpios", gpio_ph, 0, 0);
 
     g_free(node);
+    g_free(poweroff);
 }
 
 static int sysbus_device_create_devtree(SysBusDevice *sbdev, void *opaque)
@@ -765,6 +776,13 @@ static qemu_irq *ppce500_init_mpic(PPCE500Params *params, MemoryRegion *ccsr,
     return mpic;
 }
 
+static void ppce500_power_off(void *opaque, int line, int on)
+{
+    if (on) {
+        qemu_system_shutdown_request();
+    }
+}
+
 void ppce500_init(MachineState *machine, PPCE500Params *params)
 {
     MemoryRegion *address_space_mem = get_system_memory();
@@ -936,12 +954,18 @@ void ppce500_init(MachineState *machine, PPCE500Params *params)
     }
 
     if (params->has_mpc8xxx_gpio) {
+        qemu_irq poweroff_irq;
+
         dev = qdev_create(NULL, "mpc8xxx_gpio");
         s = SYS_BUS_DEVICE(dev);
         qdev_init_nofail(dev);
         sysbus_connect_irq(s, 0, mpic[MPC8XXX_GPIO_IRQ]);
         memory_region_add_subregion(ccsr_addr_space, MPC8XXX_GPIO_OFFSET,
                                     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);
     }
 
     /* Load kernel. */
Signature · 1 line
1.8.1.4