On Thu, 07 Nov 2019 13:24:08 PST (-0800), Christoph Hellwig wrote:
Add a trivial poweroff driver for the qemu-virt test device that allows an oderly shutdown of the VM. Signed-off-by: Christoph Hellwig <hch@xxxxxx> --- arch/riscv/configs/defconfig | 2 + drivers/power/reset/Kconfig | 8 ++++ drivers/power/reset/Makefile | 1 + .../power/reset/qemu-riscv-virt-poweroff.c | 47 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 drivers/power/reset/qemu-riscv-virt-poweroff.c diff --git a/arch/riscv/configs/defconfig b/arch/riscv/configs/defconfig index 420a0dbef386..47da87725b5e 100644 --- a/arch/riscv/configs/defconfig +++ b/arch/riscv/configs/defconfig @@ -63,6 +63,8 @@ CONFIG_HW_RANDOM_VIRTIO=y CONFIG_SPI=y CONFIG_SPI_SIFIVE=y # CONFIG_PTP_1588_CLOCK is not set +CONFIG_POWER_RESET=y +CONFIG_QEMU_RISCV_VIRT_POWEROFF=y CONFIG_DRM=y CONFIG_DRM_RADEON=y CONFIG_DRM_VIRTIO_GPU=y diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig index a564237278ff..56cb18520850 100644 --- a/drivers/power/reset/Kconfig +++ b/drivers/power/reset/Kconfig @@ -256,5 +256,13 @@ config NVMEM_REBOOT_MODE then the bootloader can read it and take different action according to the mode. +config QEMU_RISCV_VIRT_POWEROFF + tristate "QEMU RISC-V virt machine poweroff driver" + depends on OF + depends on RISCV || COMPILE_TEST + help + Say y here to allow RISC-V Qemu VMs to be shut down by + the kernel. + endif diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile index 85da3198e4e0..b3094016b634 100644 --- a/drivers/power/reset/Makefile +++ b/drivers/power/reset/Makefile @@ -30,3 +30,4 @@ obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o obj-$(CONFIG_POWER_RESET_SC27XX) += sc27xx-poweroff.o obj-$(CONFIG_NVMEM_REBOOT_MODE) += nvmem-reboot-mode.o +obj-$(CONFIG_QEMU_RISCV_VIRT_POWEROFF) += qemu-riscv-virt-poweroff.o diff --git a/drivers/power/reset/qemu-riscv-virt-poweroff.c b/drivers/power/reset/qemu-riscv-virt-poweroff.c new file mode 100644 index 000000000000..5b9a12dd853b --- /dev/null +++ b/drivers/power/reset/qemu-riscv-virt-poweroff.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2019 Christoph Hellwig. + */ + +#include <linux/reboot.h> +#include <linux/init.h> +#include <linux/io.h> +#include <linux/platform_device.h> +#include <linux/of_address.h> +#include <linux/module.h> + +#define VIRT_TEST_FINISHER_PASS 0x5555 + +static u16 __iomem *test_addr; + +static void qemu_virt_power_off(void) +{ + writew(VIRT_TEST_FINISHER_PASS, test_addr); +} + +static int sifive_test_probe(struct platform_device *pdev) +{ + /* there can only be a single instance */ + if (WARN_ON_ONCE(test_addr)) + return -EINVAL; + + test_addr = of_iomap(pdev->dev.of_node, 0); + if (!test_addr) + return -EINVAL; + pm_power_off = qemu_virt_power_off; + return 0; +} + +static const struct of_device_id sifive_test_of_match[] = { + { .compatible = "sifive,test0", }, + {}, +}; + +static struct platform_driver sifive_test_driver = { + .probe = sifive_test_probe, + .driver = { + .name = "sifive_test", + .of_match_table = sifive_test_of_match, + }, +}; +module_platform_driver(sifive_test_driver);
Reviewed-by: Palmer Dabbelt <palmer@xxxxxxxxxxx>