Add QEMU virt-ctrl driver and update m68k virt
Subsystems (3)
Kuan-Wei Chiu
maintainercover Add QEMU virt-ctrl driver and update m68k virt
Introduce a generic platform driver for the QEMU 'virt-ctrl' device [1] and transitions the m68k 'virt' machine to use it, replacing architecture-specific hooks.
The new driver ('qemu-virt-ctrl') registers a restart handler and populates the global 'pm_power_off' callback.
On the m68k side, the platform initialization is updated to register the 'qemu-virt-ctrl' platform device. Additionally, the 'mach_reset' hook is bridged to 'do_kernel_restart()' to ensure the kernel's restart handler chain is correctly invoked.
Verified on QEMU m68k virt. Both system reset and power-off were confirmed functional by invoking 'reboot(LINUX_REBOOT_CMD_RESTART)' and 'reboot(LINUX_REBOOT_CMD_POWER_OFF)' from userspace.
Kuan-Wei Chiu (2):
power: reset: Add QEMU virt-ctrl driver
m68k: virt: Switch to qemu-virt-ctrl driver
MAINTAINERS | 6 ++ arch/m68k/virt/config.c | 42 +----------- arch/m68k/virt/platform.c | 20 +++++- drivers/power/reset/Kconfig | 10 +++ drivers/power/reset/Makefile | 1 + drivers/power/reset/qemu-virt-ctrl.c | 98 ++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 44 deletions(-) create mode 100644 drivers/power/reset/qemu-virt-ctrl.c
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Kuan-Wei Chiu
maintainerPATCH 1/2 power: reset: Add QEMU virt-ctrl driver
Add a new driver for the 'virt-ctrl' device found on QEMU virt machines (e.g. m68k). This device provides a simple interface for system reset and power off [1].
This driver registers a restart handler for system reboot and sets the global pm_power_off callback for system shutdown. It is designed to be generic and can be reused by other architectures utilizing this QEMU device.
--- MAINTAINERS | 6 ++ drivers/power/reset/Kconfig | 10 +++ drivers/power/reset/Makefile | 1 + drivers/power/reset/qemu-virt-ctrl.c | 98 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 drivers/power/reset/qemu-virt-ctrl.c
MAINTAINERS +6 -0
@@ -21179,6 +21179,12 @@ S: Maintained F: drivers/firmware/qemu_fw_cfg.c F: include/uapi/linux/qemu_fw_cfg.h +QEMU VIRT MACHINE SYSTEM CONTROLLER DRIVER +M: Kuan-Wei Chiu <[email protected]> +L: [email protected] +S: Maintained +F: drivers/power/reset/qemu-virt-ctrl.c + QLOGIC QL41xxx FCOE DRIVER M: Saurav Kashyap <[email protected]> M: Javed Hasan <[email protected]>
drivers/power/reset/Kconfig +10 -0
@@ -354,4 +354,14 @@ config POWER_MLXBF help This driver supports reset or low power mode handling for Mellanox BlueField. +config POWER_RESET_QEMU_VIRT_CTRL + bool "QEMU Virt Machine System Controller" + depends on HAS_IOMEM + help + This driver supports the system reset and power off functionality + provided by the QEMU 'virt-ctrl' device. + + Say Y here if you are running Linux on a QEMU virtual machine that + provides this controller, such as the m68k virt machine. + endif
drivers/power/reset/Makefile +1 -0
@@ -41,3 +41,4 @@ 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_POWER_MLXBF) += pwr-mlxbf.o +obj-$(CONFIG_POWER_RESET_QEMU_VIRT_CTRL) += qemu-virt-ctrl.o
drivers/power/reset/qemu-virt-ctrl.c new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH 1/2] power: reset: Add QEMU virt-ctrl driver
Hi Kuan-Wei,
Quoted 5 lines · Kuan-Wei Chiu
Add a new driver for the 'virt-ctrl' device found on QEMU virt machines (e.g. m68k). This device provides a simple interface for system reset and power off [1].
Quoted 7 lines
This driver registers a restart handler for system reboot and sets the global
pm_power_offcallback for system shutdown. It is designed to be generic and can be reused by other architectures utilizing this QEMU device.
FWIW: I have a driver for this in my "m68k with devicetree" tree. As far as I could tell the virt_ctrl thing in QEMU might get more features aside from power control.
So I made it a misc device instead.
Cheers,
Daniel
Kuan-Wei Chiu
maintainerRe: [PATCH 1/2] power: reset: Add QEMU virt-ctrl driver
Hi Daniel,
Quoted 13 lines · Daniel Palmer
Hi Kuan-Wei,
Quoted 10 lines · Kuan-Wei Chiu
Add a new driver for the 'virt-ctrl' device found on QEMU virt machines (e.g. m68k). This device provides a simple interface for system reset and power off [1].
This driver registers a restart handler for system reboot and sets the global
pm_power_offcallback for system shutdown. It is designed to be generic and can be reused by other architectures utilizing this QEMU device.
Quoted 7 lines
Quoted 2 lines
FWIW: I have a driver for this in my "m68k with devicetree" tree. As far as I could tell the
virt_ctrlthing in QEMU might get more features aside from power control.
So I made it a misc device instead.
Thanks for the note.
Just out of curious, are there currently specific plans to add non-power features to virt_ctrl?
If new features are added, shouldn't they be exposed via separate drivers in their respective subsystems, rather than consolidating everything into a misc driver?
Regards,
Kuan-Wei
Re: [PATCH 1/2] power: reset: Add QEMU virt-ctrl driver
Hi Kuan-Wei,
Quoted 8 lines · Kuan-Wei Chiu
Quoted 4 lines
FWIW: I have a driver for this in my "m68k with devicetree" tree. As far as I could tell the
virt_ctrlthing in QEMU might get more features aside from power control.
So I made it a misc device instead.Thanks for the note.
Just out of curious, are there currently specific plans to add non-power features tovirt_ctrl?
The docs here suggest that power control is the only currently implemented feature but doesn't have to be the only feature:
https://github.com/qemu/qemu/blob/master/docs/specs/virt-ctlr.rst
Quoted 3 lines
If new features are added, shouldn't they be exposed via separate drivers in their respective subsystems, rather than consolidating everything into a misc driver?
I guess if it did get new features maybe it'd be a multifunction device? Since nothing except power control has ever actually been implemented, maybe the way you have it right now makes the most sense.
Thanks,
Daniel
Kuan-Wei Chiu
maintainerRe: [PATCH 1/2] power: reset: Add QEMU virt-ctrl driver
Hi Daniel,
Quoted 17 lines · Daniel Palmer
Hi Kuan-Wei,
Quoted 8 lines · Kuan-Wei Chiu
Quoted 4 lines
FWIW: I have a driver for this in my "m68k with devicetree" tree. As far as I could tell the
virt_ctrlthing in QEMU might get more features aside from power control.
So I made it a misc device instead.Thanks for the note.
Just out of curious, are there currently specific plans to add non-power features tovirt_ctrl?The docs here suggest that power control is the only currently implemented feature but doesn't have to be the only feature:
https://github.com/qemu/qemu/blob/master/docs/specs/virt-ctlr.rst
Quoted 8 lines
Quoted 3 lines
If new features are added, shouldn't they be exposed via separate drivers in their respective subsystems, rather than consolidating everything into a misc driver?
I guess if it did get new features maybe it'd be a multifunction device? Since nothing except power control has ever actually been implemented, maybe the way you have it right now makes the most sense.
I agree that migrating to an MFD architecture would be the right move if/when qemu implements additional features. For now, I will stick to the current approach in drivers/power/reset as it fits the existing functionality best.
Thanks again for your feedback.
Regards,
Kuan-Wei
Re: [PATCH 1/2] power: reset: Add QEMU virt-ctrl driver
In reply to Kuan-Wei ChiuHi,
Quoted 127 lines · Kuan-Wei Chiu
Add a new driver for the 'virt-ctrl' device found on QEMU virt machines (e.g. m68k). This device provides a simple interface for system reset and power off [1].
This driver registers a restart handler for system reboot and sets the global
pm_power_offcallback for system shutdown. It is designed to be generic and can be reused by other architectures utilizing this QEMU device.Signed-off-by Kuan-Wei Chiu <[email protected]>--- MAINTAINERS | 6 ++ drivers/power/reset/Kconfig | 10 +++ drivers/power/reset/Makefile | 1 + drivers/power/reset/qemu-virt-ctrl.c | 98 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 drivers/power/reset/qemu-virt-ctrl.cMAINTAINERS +6
-0@@ -21179,6 +21179,12 @@ S: Maintained F: drivers/firmware/qemu_fw_cfg.c F: include/uapi/linux/qemu_fw_cfg.h +QEMU VIRT MACHINE SYSTEM CONTROLLER DRIVER +M: Kuan-Wei Chiu <[email protected]> +L: [email protected] +S: Maintained +F: drivers/power/reset/qemu-virt-ctrl.c + QLOGIC QL41xxx FCOE DRIVER M: Saurav Kashyap <[email protected]> M: Javed Hasan <[email protected]>drivers/power/reset/Kconfig +10
-0@@ -354,4 +354,14 @@ config POWER_MLXBF help This driver supports reset or low power mode handling for Mellanox BlueField. +config POWER_RESET_QEMU_VIRT_CTRL + bool "QEMU Virt Machine System Controller" + depends on HAS_IOMEM + help + This driver supports the system reset and power off functionality + provided by the QEMU 'virt-ctrl' device. + + Say Y here if you are running Linux on a QEMU virtual machine that + provides this controller, such as the m68k virt machine. + endifdrivers/power/reset/Makefile +1
-0@@ -41,3 +41,4 @@ 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_POWER_MLXBF) += pwr-mlxbf.o +obj-$(CONFIG_POWER_RESET_QEMU_VIRT_CTRL) += qemu-virt-ctrl.odrivers/power/reset/qemu-virt-ctrl.c new file +57
-0@@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* QEMU Virt Machine System Controller Driver +* +* Copyright (C) 2026 Kuan-Wei Chiu <[email protected]> +*/ + +#include <linux/io.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/reboot.h> + +/* Registers */ +#define VIRT_CTRL_REG_FEATURES 0x00 +#define VIRT_CTRL_REG_CMD 0x04 + +/* Commands */ +#define CMD_NOOP 0 +#define CMD_RESET 1 +#define CMD_HALT 2 +#define CMD_PANIC 3 + +struct qemu_virt_ctrl { +void __iomem *base; +struct notifier_block restart_nb; +}; + +static void __iomem *qemu_virt_ctrl_base; + +static void qemu_virt_ctrl_power_off (void ) +{ +if (qemu_virt_ctrl_base) +iowrite32be (CMD_HALT , qemu_virt_ctrl_base +VIRT_CTRL_REG_CMD ); +} + +static int qemu_virt_ctrl_restart (struct notifier_block *nb,unsigned long action, +void *data) +{ + struct qemu_virt_ctrl *vc = container_of(nb, struct qemu_virt_ctrl, restart_nb); + +iowrite32be (CMD_RESET , vc->base +VIRT_CTRL_REG_CMD ); + +return NOTIFY_DONE ; +} + +static int qemu_virt_ctrl_probe (struct platform_device *pdev) +{ +struct qemu_virt_ctrl *vc; +int ret; + + vc =devm_kzalloc (&pdev->dev,sizeof (*vc),GFP_KERNEL ); +if (!vc) +return -ENOMEM ; + + vc->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (vc->base)) +return PTR_ERR (vc->base);
drivers/power/reset/qemu-virt-ctrl.c line 67
Quoted 10 lines
drivers/power/reset/qemu-virt-ctrl.c new file +10
-0+ + qemu_virt_ctrl_base = vc->base; + + vc->restart_nb.notifier_call = qemu_virt_ctrl_restart; + vc->restart_nb.priority =128 ; + + ret =register_restart_handler (&vc->restart_nb); +if (ret) +return dev_err_probe (&pdev->dev, ret, +"cannot register restart handler\n" );
Please use devm_register_sys_off_handler().
drivers/power/reset/qemu-virt-ctrl.c line 70
Quoted 2 lines
drivers/power/reset/qemu-virt-ctrl.c new file +2
-0+if (!pm_power_off) + pm_power_off = qemu_virt_ctrl_power_off;
same.
Quoted 17 lines
drivers/power/reset/qemu-virt-ctrl.c new file +17
-0+platform_set_drvdata (pdev, vc); + +return 0 ; +} + +static void qemu_virt_ctrl_remove (struct platform_device *pdev) +{ +struct qemu_virt_ctrl *vc =platform_get_drvdata (pdev); + +unregister_restart_handler (&vc->restart_nb); + +if (pm_power_off == qemu_virt_ctrl_power_off) + pm_power_off =NULL ; +} + +static struct platform_driver qemu_virt_ctrl_driver = { + .probe = qemu_virt_ctrl_probe,
drivers/power/reset/qemu-virt-ctrl.c line 98
Quoted 10 lines
drivers/power/reset/qemu-virt-ctrl.c new file +10
-0+ .remove = qemu_virt_ctrl_remove, + .driver = { + .name = "qemu-virt-ctrl", + }, +}; +module_platform_driver (qemu_virt_ctrl_driver); + +MODULE_AUTHOR ("Kuan-Wei Chiu <[email protected]>" ); +MODULE_DESCRIPTION ("QEMU Virt Machine System Controller Driver" ); +MODULE_LICENSE ("GPL" );
You mark this as module_platform_driver() and add MODULE_*, but the Kconfig option is bool. To be usable as a module it is missing setup of .id_table in the platform_driver struct for auto-loading.
Greetings,
-- Sebastian
Kuan-Wei Chiu
maintainerPATCH 2/2 m68k: virt: Switch to qemu-virt-ctrl driver
In reply to Kuan-Wei ChiuRegister the "qemu-virt-ctrl" platform device during board initialization to utilize the new generic power/reset driver.
Consequently, remove the legacy reset and power-off implementations specific to the virt machine. The platform's mach_reset callback is updated to call do_kernel_restart(), bridging the legacy m68k reboot path to the generic kernel restart handler framework for this machine.
--- arch/m68k/virt/config.c | 42 +-------------------------------------- arch/m68k/virt/platform.c | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 44 deletions(-)
arch/m68k/virt/config.c +1 -41
@@ -13,18 +13,6 @@struct virt_booter_data virt_bi_data; -#define VIRT_CTRL_REG_FEATURES 0x00 -#define VIRT_CTRL_REG_CMD 0x04 - -static struct resource ctrlres; - -enum { -CMD_NOOP , -CMD_RESET , -CMD_HALT , -CMD_PANIC , -}; -static void virt_get_model (char *str) {/* str is 80 characters long */
@@ -33,25 +21,9 @@ static void virt_get_model(char *str) (u8)(virt_bi_data.qemu_version >>16 ), (u8)(virt_bi_data.qemu_version >> 8)); } - -static void virt_halt (void ) -{ -void __iomem *base = (void __iomem *)virt_bi_data.ctrl.mmio; - -iowrite32be (CMD_HALT , base +VIRT_CTRL_REG_CMD ); -local_irq_disable (); -while (1 ) - ; -} -static void virt_reset (void ) { -void __iomem *base = (void __iomem *)virt_bi_data.ctrl.mmio; - -iowrite32be (CMD_RESET , base +VIRT_CTRL_REG_CMD ); -local_irq_disable (); -while (1 ) - ; +do_kernel_restart (NULL ); }/*
@@ -113,20 +85,8 @@ void __init config_virt(void) virt_bi_data.tty.mmio);setup_earlycon (earlycon); - ctrlres = (struct resource ) -DEFINE_RES_MEM_NAMED (virt_bi_data.ctrl.mmio,0x100 , -"virtctrl" ); - -if (request_resource (&iomem_resource, &ctrlres)) { -pr_err ("Cannot allocate virt controller resource\n" ); -return ; - } - mach_init_IRQ = virt_init_IRQ; mach_sched_init = virt_sched_init; mach_get_model = virt_get_model; mach_reset = virt_reset; - mach_halt = virt_halt; - -register_platform_power_off (virt_halt); }
arch/m68k/virt/platform.c +17 -3
@@ -30,7 +30,10 @@ static int __init virt_platform_init(void)DEFINE_RES_MEM (virt_bi_data.rtc.mmio +0x1000 ,0x1000 ), DEFINE_RES_IRQ(virt_bi_data.rtc.irq + 1), }; -struct platform_device *pdev1, *pdev2; +const struct resource virt_ctrl_res[] = { +DEFINE_RES_MEM (virt_bi_data.ctrl.mmio,0x100 ), + }; +struct platform_device *pdev1, *pdev2, *pdev3;struct platform_device *pdevs[VIRTIO_BUS_NB ];unsigned int i;int ret =0 ;
@@ -57,19 +60,30 @@ static int __init virt_platform_init(void) goto err_unregister_tty; } + pdev3 =platform_device_register_simple ("qemu-virt-ctrl" , +PLATFORM_DEVID_NONE , + virt_ctrl_res, +ARRAY_SIZE (virt_ctrl_res)); +if (IS_ERR (pdev3)) { + ret =PTR_ERR (pdev3); + goto err_unregister_rtc; + } +for (i =0 ; i <VIRTIO_BUS_NB ; i++) { pdevs[i] =virt_virtio_init (i);if (IS_ERR (pdevs[i])) { ret =PTR_ERR (pdevs[i]); - goto err_unregister_rtc_virtio; + goto err_unregister_virtio; } }return 0 ; -err_unregister_rtc_virtio: +err_unregister_virtio:while (i >0 )platform_device_unregister (pdevs[--i]); +platform_device_unregister (pdev3); +err_unregister_rtc:platform_device_unregister (pdev2); err_unregister_tty:platform_device_unregister (pdev1);
Signature · 1 line
2.52.0.457.g6b5491de43-goog