diff mbox series

[PATCHv4,1/2] hw: gpio: implement gpio-pwr driver for qemu reset/poweroff

Message ID 20210112143058.12159-2-maxim.uvarov@linaro.org
State Superseded
Headers show
Series arm-virt: add secure pl061 for reset/power down | expand

Commit Message

Maxim Uvarov Jan. 12, 2021, 2:30 p.m. UTC
Implement gpio-pwr driver to allow reboot and poweroff machine.
This is simple driver with just 2 gpios lines. Current use case
is to reboot and poweroff virt machine in secure mode. Secure
pl066 gpio chip is needed for that.

Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>

---
 hw/gpio/Kconfig     |  3 ++
 hw/gpio/gpio_pwr.c  | 70 +++++++++++++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build |  1 +
 3 files changed, 74 insertions(+)
 create mode 100644 hw/gpio/gpio_pwr.c

-- 
2.17.1

Comments

Xingtao Yao (Fujitsu)" via Jan. 12, 2021, 5:56 p.m. UTC | #1
On Tue, Jan 12, 2021 at 6:36 AM Maxim Uvarov <maxim.uvarov@linaro.org>
wrote:

> Implement gpio-pwr driver to allow reboot and poweroff machine.

> This is simple driver with just 2 gpios lines. Current use case

> is to reboot and poweroff virt machine in secure mode. Secure

> pl066 gpio chip is needed for that.

>

> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>

>

Reviewed-by: Hao Wu <wuhaotsh@google.com>


> ---

>  hw/gpio/Kconfig     |  3 ++

>  hw/gpio/gpio_pwr.c  | 70 +++++++++++++++++++++++++++++++++++++++++++++

>  hw/gpio/meson.build |  1 +

>  3 files changed, 74 insertions(+)

>  create mode 100644 hw/gpio/gpio_pwr.c

>

> diff --git a/hw/gpio/Kconfig b/hw/gpio/Kconfig

> index b6fdaa2586..f0e7405f6e 100644

> --- a/hw/gpio/Kconfig

> +++ b/hw/gpio/Kconfig

> @@ -8,5 +8,8 @@ config PL061

>  config GPIO_KEY

>      bool

>

> +config GPIO_PWR

> +    bool

> +

>  config SIFIVE_GPIO

>      bool

> diff --git a/hw/gpio/gpio_pwr.c b/hw/gpio/gpio_pwr.c

> new file mode 100644

> index 0000000000..8ed8d5d24f

> --- /dev/null

> +++ b/hw/gpio/gpio_pwr.c

> @@ -0,0 +1,70 @@

> +/*

> + * GPIO qemu power controller

> + *

> + * Copyright (c) 2020 Linaro Limited

> + *

> + * Author: Maxim Uvarov <maxim.uvarov@linaro.org>

> + *

> + * Virtual gpio driver which can be used on top of pl061

> + * to reboot and shutdown qemu virtual machine. One of use

> + * case is gpio driver for secure world application (ARM

> + * Trusted Firmware.).

> + *

> + * This work is licensed under the terms of the GNU GPL, version 2 or

> later.

> + * See the COPYING file in the top-level directory.

> + * SPDX-License-Identifier: GPL-2.0-or-later

> + */

> +

> +/*

> + * QEMU interface:

> + * two named input GPIO lines:

> + *   'reset' : when asserted, trigger system reset

> + *   'shutdown' : when asserted, trigger system shutdown

> + */

> +

> +#include "qemu/osdep.h"

> +#include "hw/sysbus.h"

> +#include "sysemu/runstate.h"

> +

> +#define TYPE_GPIOPWR "gpio-pwr"

> +OBJECT_DECLARE_SIMPLE_TYPE(GPIO_PWR_State, GPIOPWR)

> +

> +struct GPIO_PWR_State {

> +    SysBusDevice parent_obj;

> +};

> +

> +static void gpio_pwr_reset(void *opaque, int n, int level)

> +{

> +    if (!level) {

> +        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);

> +    }

> +}

> +

> +static void gpio_pwr_shutdown(void *opaque, int n, int level)

> +{

> +    if (!level) {

> +        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);

> +    }

> +}

> +

> +static void gpio_pwr_init(Object *obj)

> +{

> +    DeviceState *dev = DEVICE(obj);

> +

> +    qdev_init_gpio_in_named(dev, gpio_pwr_reset, "reset", 1);

> +    qdev_init_gpio_in_named(dev, gpio_pwr_shutdown, "shutdown", 1);

> +}

> +

> +static const TypeInfo gpio_pwr_info = {

> +    .name          = TYPE_GPIOPWR,

> +    .parent        = TYPE_SYS_BUS_DEVICE,

> +    .instance_size = sizeof(GPIO_PWR_State),

> +    .instance_init = gpio_pwr_init,

> +};

> +

> +static void gpio_pwr_register_types(void)

> +{

> +    type_register_static(&gpio_pwr_info);

> +}

> +

> +type_init(gpio_pwr_register_types)

> diff --git a/hw/gpio/meson.build b/hw/gpio/meson.build

> index 5c0a7d7b95..79568f00ce 100644

> --- a/hw/gpio/meson.build

> +++ b/hw/gpio/meson.build

> @@ -1,5 +1,6 @@

>  softmmu_ss.add(when: 'CONFIG_E500', if_true: files('mpc8xxx.c'))

>  softmmu_ss.add(when: 'CONFIG_GPIO_KEY', if_true: files('gpio_key.c'))

> +softmmu_ss.add(when: 'CONFIG_GPIO_PWR', if_true: files('gpio_pwr.c'))

>  softmmu_ss.add(when: 'CONFIG_MAX7310', if_true: files('max7310.c'))

>  softmmu_ss.add(when: 'CONFIG_PL061', if_true: files('pl061.c'))

>  softmmu_ss.add(when: 'CONFIG_PUV3', if_true: files('puv3_gpio.c'))

> --

> 2.17.1

>

>

>
<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 12, 2021 at 6:36 AM Maxim Uvarov &lt;<a href="mailto:maxim.uvarov@linaro.org">maxim.uvarov@linaro.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Implement gpio-pwr driver to allow reboot and poweroff machine.<br>
This is simple driver with just 2 gpios lines. Current use case<br>
is to reboot and poweroff virt machine in secure mode. Secure<br>
pl066 gpio chip is needed for that.<br>
<br>
Signed-off-by: Maxim Uvarov &lt;<a href="mailto:maxim.uvarov@linaro.org" target="_blank">maxim.uvarov@linaro.org</a>&gt;<br></blockquote><div>Reviewed-by: Hao Wu &lt;<a href="mailto:wuhaotsh@google.com">wuhaotsh@google.com</a>&gt; </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

---<br>
 hw/gpio/Kconfig     |  3 ++<br>
 hw/gpio/gpio_pwr.c  | 70 +++++++++++++++++++++++++++++++++++++++++++++<br>
 hw/gpio/meson.build |  1 +<br>
 3 files changed, 74 insertions(+)<br>
 create mode 100644 hw/gpio/gpio_pwr.c<br>
<br>
diff --git a/hw/gpio/Kconfig b/hw/gpio/Kconfig<br>
index b6fdaa2586..f0e7405f6e 100644<br>
--- a/hw/gpio/Kconfig<br>
+++ b/hw/gpio/Kconfig<br>
@@ -8,5 +8,8 @@ config PL061<br>
 config GPIO_KEY<br>
     bool<br>
<br>
+config GPIO_PWR<br>
+    bool<br>
+<br>
 config SIFIVE_GPIO<br>
     bool<br>
diff --git a/hw/gpio/gpio_pwr.c b/hw/gpio/gpio_pwr.c<br>
new file mode 100644<br>
index 0000000000..8ed8d5d24f<br>
--- /dev/null<br>
+++ b/hw/gpio/gpio_pwr.c<br>
@@ -0,0 +1,70 @@<br>
+/*<br>
+ * GPIO qemu power controller<br>
+ *<br>
+ * Copyright (c) 2020 Linaro Limited<br>
+ *<br>
+ * Author: Maxim Uvarov &lt;<a href="mailto:maxim.uvarov@linaro.org" target="_blank">maxim.uvarov@linaro.org</a>&gt;<br>
+ *<br>
+ * Virtual gpio driver which can be used on top of pl061<br>
+ * to reboot and shutdown qemu virtual machine. One of use<br>
+ * case is gpio driver for secure world application (ARM<br>
+ * Trusted Firmware.).<br>
+ *<br>
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.<br>
+ * See the COPYING file in the top-level directory.<br>
+ * SPDX-License-Identifier: GPL-2.0-or-later<br>
+ */<br>
+<br>
+/*<br>
+ * QEMU interface:<br>
+ * two named input GPIO lines:<br>
+ *   &#39;reset&#39; : when asserted, trigger system reset<br>
+ *   &#39;shutdown&#39; : when asserted, trigger system shutdown<br>
+ */<br>
+<br>
+#include &quot;qemu/osdep.h&quot;<br>
+#include &quot;hw/sysbus.h&quot;<br>
+#include &quot;sysemu/runstate.h&quot;<br>
+<br>
+#define TYPE_GPIOPWR &quot;gpio-pwr&quot;<br>
+OBJECT_DECLARE_SIMPLE_TYPE(GPIO_PWR_State, GPIOPWR)<br>
+<br>
+struct GPIO_PWR_State {<br>
+    SysBusDevice parent_obj;<br>
+};<br>
+<br>
+static void gpio_pwr_reset(void *opaque, int n, int level)<br>
+{<br>
+    if (!level) {<br>
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);<br>
+    }<br>
+}<br>
+<br>
+static void gpio_pwr_shutdown(void *opaque, int n, int level)<br>
+{<br>
+    if (!level) {<br>
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);<br>
+    }<br>
+}<br>
+<br>
+static void gpio_pwr_init(Object *obj)<br>
+{<br>
+    DeviceState *dev = DEVICE(obj);<br>
+<br>
+    qdev_init_gpio_in_named(dev, gpio_pwr_reset, &quot;reset&quot;, 1);<br>
+    qdev_init_gpio_in_named(dev, gpio_pwr_shutdown, &quot;shutdown&quot;, 1);<br>
+}<br>
+<br>
+static const TypeInfo gpio_pwr_info = {<br>
+    .name          = TYPE_GPIOPWR,<br>
+    .parent        = TYPE_SYS_BUS_DEVICE,<br>
+    .instance_size = sizeof(GPIO_PWR_State),<br>
+    .instance_init = gpio_pwr_init,<br>
+};<br>
+<br>
+static void gpio_pwr_register_types(void)<br>
+{<br>
+    type_register_static(&amp;gpio_pwr_info);<br>
+}<br>
+<br>
+type_init(gpio_pwr_register_types)<br>
diff --git a/hw/gpio/meson.build b/hw/gpio/meson.build<br>
index 5c0a7d7b95..79568f00ce 100644<br>
--- a/hw/gpio/meson.build<br>
+++ b/hw/gpio/meson.build<br>
@@ -1,5 +1,6 @@<br>
 softmmu_ss.add(when: &#39;CONFIG_E500&#39;, if_true: files(&#39;mpc8xxx.c&#39;))<br>
 softmmu_ss.add(when: &#39;CONFIG_GPIO_KEY&#39;, if_true: files(&#39;gpio_key.c&#39;))<br>
+softmmu_ss.add(when: &#39;CONFIG_GPIO_PWR&#39;, if_true: files(&#39;gpio_pwr.c&#39;))<br>
 softmmu_ss.add(when: &#39;CONFIG_MAX7310&#39;, if_true: files(&#39;max7310.c&#39;))<br>
 softmmu_ss.add(when: &#39;CONFIG_PL061&#39;, if_true: files(&#39;pl061.c&#39;))<br>
 softmmu_ss.add(when: &#39;CONFIG_PUV3&#39;, if_true: files(&#39;puv3_gpio.c&#39;))<br>
-- <br>
2.17.1<br>
<br>
<br>
</blockquote></div></div>
diff mbox series

Patch

diff --git a/hw/gpio/Kconfig b/hw/gpio/Kconfig
index b6fdaa2586..f0e7405f6e 100644
--- a/hw/gpio/Kconfig
+++ b/hw/gpio/Kconfig
@@ -8,5 +8,8 @@  config PL061
 config GPIO_KEY
     bool
 
+config GPIO_PWR
+    bool
+
 config SIFIVE_GPIO
     bool
diff --git a/hw/gpio/gpio_pwr.c b/hw/gpio/gpio_pwr.c
new file mode 100644
index 0000000000..8ed8d5d24f
--- /dev/null
+++ b/hw/gpio/gpio_pwr.c
@@ -0,0 +1,70 @@ 
+/*
+ * GPIO qemu power controller
+ *
+ * Copyright (c) 2020 Linaro Limited
+ *
+ * Author: Maxim Uvarov <maxim.uvarov@linaro.org>
+ *
+ * Virtual gpio driver which can be used on top of pl061
+ * to reboot and shutdown qemu virtual machine. One of use
+ * case is gpio driver for secure world application (ARM
+ * Trusted Firmware.).
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+/*
+ * QEMU interface:
+ * two named input GPIO lines:
+ *   'reset' : when asserted, trigger system reset
+ *   'shutdown' : when asserted, trigger system shutdown
+ */
+
+#include "qemu/osdep.h"
+#include "hw/sysbus.h"
+#include "sysemu/runstate.h"
+
+#define TYPE_GPIOPWR "gpio-pwr"
+OBJECT_DECLARE_SIMPLE_TYPE(GPIO_PWR_State, GPIOPWR)
+
+struct GPIO_PWR_State {
+    SysBusDevice parent_obj;
+};
+
+static void gpio_pwr_reset(void *opaque, int n, int level)
+{
+    if (!level) {
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
+    }
+}
+
+static void gpio_pwr_shutdown(void *opaque, int n, int level)
+{
+    if (!level) {
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+    }
+}
+
+static void gpio_pwr_init(Object *obj)
+{
+    DeviceState *dev = DEVICE(obj);
+
+    qdev_init_gpio_in_named(dev, gpio_pwr_reset, "reset", 1);
+    qdev_init_gpio_in_named(dev, gpio_pwr_shutdown, "shutdown", 1);
+}
+
+static const TypeInfo gpio_pwr_info = {
+    .name          = TYPE_GPIOPWR,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(GPIO_PWR_State),
+    .instance_init = gpio_pwr_init,
+};
+
+static void gpio_pwr_register_types(void)
+{
+    type_register_static(&gpio_pwr_info);
+}
+
+type_init(gpio_pwr_register_types)
diff --git a/hw/gpio/meson.build b/hw/gpio/meson.build
index 5c0a7d7b95..79568f00ce 100644
--- a/hw/gpio/meson.build
+++ b/hw/gpio/meson.build
@@ -1,5 +1,6 @@ 
 softmmu_ss.add(when: 'CONFIG_E500', if_true: files('mpc8xxx.c'))
 softmmu_ss.add(when: 'CONFIG_GPIO_KEY', if_true: files('gpio_key.c'))
+softmmu_ss.add(when: 'CONFIG_GPIO_PWR', if_true: files('gpio_pwr.c'))
 softmmu_ss.add(when: 'CONFIG_MAX7310', if_true: files('max7310.c'))
 softmmu_ss.add(when: 'CONFIG_PL061', if_true: files('pl061.c'))
 softmmu_ss.add(when: 'CONFIG_PUV3', if_true: files('puv3_gpio.c'))