gpio_pwr.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * GPIO qemu power controller
  3. *
  4. * Copyright (c) 2020 Linaro Limited
  5. *
  6. * Author: Maxim Uvarov <maxim.uvarov@linaro.org>
  7. *
  8. * Virtual gpio driver which can be used on top of pl061
  9. * to reboot and shutdown qemu virtual machine. One of use
  10. * case is gpio driver for secure world application (ARM
  11. * Trusted Firmware.).
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  14. * See the COPYING file in the top-level directory.
  15. * SPDX-License-Identifier: GPL-2.0-or-later
  16. */
  17. /*
  18. * QEMU interface:
  19. * two named input GPIO lines:
  20. * 'reset' : when asserted, trigger system reset
  21. * 'shutdown' : when asserted, trigger system shutdown
  22. */
  23. #include "qemu/osdep.h"
  24. #include "hw/sysbus.h"
  25. #include "sysemu/runstate.h"
  26. #define TYPE_GPIOPWR "gpio-pwr"
  27. OBJECT_DECLARE_SIMPLE_TYPE(GPIO_PWR_State, GPIOPWR)
  28. struct GPIO_PWR_State {
  29. SysBusDevice parent_obj;
  30. };
  31. static void gpio_pwr_reset(void *opaque, int n, int level)
  32. {
  33. if (level) {
  34. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  35. }
  36. }
  37. static void gpio_pwr_shutdown(void *opaque, int n, int level)
  38. {
  39. if (level) {
  40. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  41. }
  42. }
  43. static void gpio_pwr_init(Object *obj)
  44. {
  45. DeviceState *dev = DEVICE(obj);
  46. qdev_init_gpio_in_named(dev, gpio_pwr_reset, "reset", 1);
  47. qdev_init_gpio_in_named(dev, gpio_pwr_shutdown, "shutdown", 1);
  48. }
  49. static const TypeInfo gpio_pwr_info = {
  50. .name = TYPE_GPIOPWR,
  51. .parent = TYPE_SYS_BUS_DEVICE,
  52. .instance_size = sizeof(GPIO_PWR_State),
  53. .instance_init = gpio_pwr_init,
  54. };
  55. static void gpio_pwr_register_types(void)
  56. {
  57. type_register_static(&gpio_pwr_info);
  58. }
  59. type_init(gpio_pwr_register_types)