public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] Add QEMU virt-ctrl driver and update m68k virt
@ 2026-02-22 17:32 Kuan-Wei Chiu
  2026-02-22 17:32 ` [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver Kuan-Wei Chiu
  2026-02-22 17:32 ` [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver Kuan-Wei Chiu
  0 siblings, 2 replies; 8+ messages in thread
From: Kuan-Wei Chiu @ 2026-02-22 17:32 UTC (permalink / raw)
  To: geert, sre
  Cc: jserv, eleanor15x, daniel, laurent, linux-kernel, linux-m68k,
	linux-pm, Kuan-Wei Chiu

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)',
'reboot(LINUX_REBOOT_CMD_POWER_OFF)', and
'reboot(LINUX_REBOOT_CMD_HALT)' from userspace.

Link: https://gitlab.com/qemu-project/qemu/-/blob/v10.2.0/hw/misc/virt_ctrl.c [1]
---
Changes in v3:
- Add a reboot notifier in the driver to handle LINUX_REBOOT_CMD_HALT.
- Handle native endianness in the driver instead of hardcoding
  big-endian I/O writes.
- Select POWER_RESET and POWER_RESET_QEMU_VIRT_CTRL in m68k
  Kconfig.machine.

Changes in v2:
- Use devm_register_sys_off_handler() instead of register_restart_handler()
  and global pm_power_off.
- Switch Kconfig to tristate to support modular build.
- Add .id_table to platform_driver and use MODULE_DEVICE_TABLE() to correct
  module auto-loading.

v2: https://lore.kernel.org/lkml/20260203170824.2968045-1-visitorckw@gmail.com/
v1: https://lore.kernel.org/lkml/20260112182258.1851769-1-visitorckw@gmail.com/

Kuan-Wei Chiu (2):
  power: reset: Add QEMU virt-ctrl driver
  m68k: virt: Switch to qemu-virt-ctrl driver

 MAINTAINERS                          |   6 ++
 arch/m68k/Kconfig.machine            |   2 +
 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 | 122 +++++++++++++++++++++++++++
 7 files changed, 159 insertions(+), 44 deletions(-)
 create mode 100644 drivers/power/reset/qemu-virt-ctrl.c

-- 
2.53.0.345.g96ddfc5eaa-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver
  2026-02-22 17:32 [PATCH v3 0/2] Add QEMU virt-ctrl driver and update m68k virt Kuan-Wei Chiu
@ 2026-02-22 17:32 ` Kuan-Wei Chiu
  2026-04-02 21:52   ` Sebastian Reichel
  2026-02-22 17:32 ` [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver Kuan-Wei Chiu
  1 sibling, 1 reply; 8+ messages in thread
From: Kuan-Wei Chiu @ 2026-02-22 17:32 UTC (permalink / raw)
  To: geert, sre
  Cc: jserv, eleanor15x, daniel, laurent, linux-kernel, linux-m68k,
	linux-pm, 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 utilizes the modern system-off API to register callbacks
for both system restart and power off. It also registers a reboot
notifier to catch SYS_HALT events, ensuring that LINUX_REBOOT_CMD_HALT
is properly handled. It is designed to be generic and can be reused by
other architectures utilizing this QEMU device.

Link: https://gitlab.com/qemu-project/qemu/-/blob/v10.2.0/hw/misc/virt_ctrl.c [1]
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
Changes in v3:
- Add a reboot notifier to handle SYS_HALT.
- Implement virt_ctrl_write32() to handle native endianness.

I noticed devm_register_sys_off_handler() currently lacks a
SYS_OFF_MODE_HALT. Therefore, I registered a standard reboot notifier
for SYS_HALT, while keeping restart and power-off on the sys-off API.

 MAINTAINERS                          |   6 ++
 drivers/power/reset/Kconfig          |  10 +++
 drivers/power/reset/Makefile         |   1 +
 drivers/power/reset/qemu-virt-ctrl.c | 122 +++++++++++++++++++++++++++
 4 files changed, 139 insertions(+)
 create mode 100644 drivers/power/reset/qemu-virt-ctrl.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 55af015174a5..aa9eb8540637 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -21441,6 +21441,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 <visitorckw@gmail.com>
+L:	linux-pm@vger.kernel.org
+S:	Maintained
+F:	drivers/power/reset/qemu-virt-ctrl.c
+
 QLOGIC QL41xxx FCOE DRIVER
 M:	Saurav Kashyap <skashyap@marvell.com>
 M:	Javed Hasan <jhasan@marvell.com>
diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index f6c1bcbb57de..99e3334726a5 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -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
+	tristate "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
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index 0e4ae6f6b5c5..d7ae97241a83 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -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
diff --git a/drivers/power/reset/qemu-virt-ctrl.c b/drivers/power/reset/qemu-virt-ctrl.c
new file mode 100644
index 000000000000..f40d04afd4e3
--- /dev/null
+++ b/drivers/power/reset/qemu-virt-ctrl.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * QEMU Virt Machine System Controller Driver
+ *
+ * Copyright (C) 2026 Kuan-Wei Chiu <visitorckw@gmail.com>
+ */
+
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.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 reboot_nb;
+};
+
+static inline void virt_ctrl_write32(u32 val, void __iomem *addr)
+{
+	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
+		iowrite32be(val, addr);
+	else
+		iowrite32(val, addr);
+}
+
+static int qemu_virt_ctrl_power_off(struct sys_off_data *data)
+{
+	void __iomem *base = data->cb_data;
+
+	virt_ctrl_write32(CMD_HALT, base + VIRT_CTRL_REG_CMD);
+
+	return NOTIFY_DONE;
+}
+
+static int qemu_virt_ctrl_restart(struct sys_off_data *data)
+{
+	void __iomem *base = data->cb_data;
+
+	virt_ctrl_write32(CMD_RESET, base + VIRT_CTRL_REG_CMD);
+
+	return NOTIFY_DONE;
+}
+
+static int qemu_virt_ctrl_reboot_notify(struct notifier_block *nb,
+					unsigned long action, void *data)
+{
+	struct qemu_virt_ctrl *ctrl = container_of(nb, struct qemu_virt_ctrl, reboot_nb);
+
+	if (action == SYS_HALT)
+		virt_ctrl_write32(CMD_HALT, ctrl->base + VIRT_CTRL_REG_CMD);
+
+	return NOTIFY_DONE;
+}
+
+static int qemu_virt_ctrl_probe(struct platform_device *pdev)
+{
+	struct qemu_virt_ctrl *ctrl;
+	int ret;
+
+	ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
+	if (!ctrl)
+		return -ENOMEM;
+
+	ctrl->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(ctrl->base))
+		return PTR_ERR(ctrl->base);
+
+	ret = devm_register_sys_off_handler(&pdev->dev,
+					    SYS_OFF_MODE_RESTART,
+					    SYS_OFF_PRIO_DEFAULT,
+					    qemu_virt_ctrl_restart,
+					    ctrl->base);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret,
+				     "cannot register restart handler\n");
+
+	ret = devm_register_sys_off_handler(&pdev->dev,
+					    SYS_OFF_MODE_POWER_OFF,
+					    SYS_OFF_PRIO_DEFAULT,
+					    qemu_virt_ctrl_power_off,
+					    ctrl->base);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret,
+				     "cannot register power-off handler\n");
+
+	ctrl->reboot_nb.notifier_call = qemu_virt_ctrl_reboot_notify;
+	ret = devm_register_reboot_notifier(&pdev->dev, &ctrl->reboot_nb);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "cannot register reboot notifier\n");
+
+	return 0;
+}
+
+static const struct platform_device_id qemu_virt_ctrl_id[] = {
+	{ "qemu-virt-ctrl", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(platform, qemu_virt_ctrl_id);
+
+static struct platform_driver qemu_virt_ctrl_driver = {
+	.probe = qemu_virt_ctrl_probe,
+	.driver = {
+		.name = "qemu-virt-ctrl",
+	},
+	.id_table = qemu_virt_ctrl_id,
+};
+module_platform_driver(qemu_virt_ctrl_driver);
+
+MODULE_AUTHOR("Kuan-Wei Chiu <visitorckw@gmail.com>");
+MODULE_DESCRIPTION("QEMU Virt Machine System Controller Driver");
+MODULE_LICENSE("GPL");
-- 
2.53.0.345.g96ddfc5eaa-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver
  2026-02-22 17:32 [PATCH v3 0/2] Add QEMU virt-ctrl driver and update m68k virt Kuan-Wei Chiu
  2026-02-22 17:32 ` [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver Kuan-Wei Chiu
@ 2026-02-22 17:32 ` Kuan-Wei Chiu
  2026-04-08  1:17   ` Kuan-Wei Chiu
  2026-04-08  9:12   ` Geert Uytterhoeven
  1 sibling, 2 replies; 8+ messages in thread
From: Kuan-Wei Chiu @ 2026-02-22 17:32 UTC (permalink / raw)
  To: geert, sre
  Cc: jserv, eleanor15x, daniel, laurent, linux-kernel, linux-m68k,
	linux-pm, Kuan-Wei Chiu

Register 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.

To prevent any regressions in reboot or power-off functionality when
the driver is not built-in, explicitly select POWER_RESET and
POWER_RESET_QEMU_VIRT_CTRL for the VIRT machine in Kconfig.machine.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
Changes in v3:
- Add 'select POWER_RESET' and 'select POWER_RESET_QEMU_VIRT_CTRL' in
  Kconfig.machine to avoid restart/power-off regressions.

 arch/m68k/Kconfig.machine |  2 ++
 arch/m68k/virt/config.c   | 42 +--------------------------------------
 arch/m68k/virt/platform.c | 20 ++++++++++++++++---
 3 files changed, 20 insertions(+), 44 deletions(-)

diff --git a/arch/m68k/Kconfig.machine b/arch/m68k/Kconfig.machine
index de39f23b180e..624e6b27f394 100644
--- a/arch/m68k/Kconfig.machine
+++ b/arch/m68k/Kconfig.machine
@@ -133,6 +133,8 @@ config VIRT
 	select GOLDFISH_TIMER
 	select GOLDFISH_TTY
 	select M68040
+	select POWER_RESET
+	select POWER_RESET_QEMU_VIRT_CTRL
 	select RTC_CLASS
 	select RTC_DRV_GOLDFISH
 	select TTY
diff --git a/arch/m68k/virt/config.c b/arch/m68k/virt/config.c
index 632ba200ad42..b338e2a8da6a 100644
--- a/arch/m68k/virt/config.c
+++ b/arch/m68k/virt/config.c
@@ -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);
 }
diff --git a/arch/m68k/virt/platform.c b/arch/m68k/virt/platform.c
index 1560c4140ab9..764f556b4b32 100644
--- a/arch/m68k/virt/platform.c
+++ b/arch/m68k/virt/platform.c
@@ -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);
-- 
2.53.0.345.g96ddfc5eaa-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver
  2026-02-22 17:32 ` [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver Kuan-Wei Chiu
@ 2026-04-02 21:52   ` Sebastian Reichel
  2026-04-08  9:10     ` Geert Uytterhoeven
  0 siblings, 1 reply; 8+ messages in thread
From: Sebastian Reichel @ 2026-04-02 21:52 UTC (permalink / raw)
  To: Kuan-Wei Chiu
  Cc: geert, jserv, eleanor15x, daniel, laurent, linux-kernel,
	linux-m68k, linux-pm

[-- Attachment #1: Type: text/plain, Size: 7126 bytes --]

Hi,

On Sun, Feb 22, 2026 at 05:32:24PM +0000, Kuan-Wei Chiu wrote:
> 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 utilizes the modern system-off API to register callbacks
> for both system restart and power off. It also registers a reboot
> notifier to catch SYS_HALT events, ensuring that LINUX_REBOOT_CMD_HALT
> is properly handled. It is designed to be generic and can be reused by
> other architectures utilizing this QEMU device.
> 
> Link: https://gitlab.com/qemu-project/qemu/-/blob/v10.2.0/hw/misc/virt_ctrl.c [1]
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---

I think this should be merged with the second patch via the m68k
tree:

Acked-by: Sebastian Reichel <sebastian.reichel@collabora.com>

Greetings,

-- Sebastian

> Changes in v3:
> - Add a reboot notifier to handle SYS_HALT.
> - Implement virt_ctrl_write32() to handle native endianness.
> 
> I noticed devm_register_sys_off_handler() currently lacks a
> SYS_OFF_MODE_HALT. Therefore, I registered a standard reboot notifier
> for SYS_HALT, while keeping restart and power-off on the sys-off API.
> 
>  MAINTAINERS                          |   6 ++
>  drivers/power/reset/Kconfig          |  10 +++
>  drivers/power/reset/Makefile         |   1 +
>  drivers/power/reset/qemu-virt-ctrl.c | 122 +++++++++++++++++++++++++++
>  4 files changed, 139 insertions(+)
>  create mode 100644 drivers/power/reset/qemu-virt-ctrl.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 55af015174a5..aa9eb8540637 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -21441,6 +21441,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 <visitorckw@gmail.com>
> +L:	linux-pm@vger.kernel.org
> +S:	Maintained
> +F:	drivers/power/reset/qemu-virt-ctrl.c
> +
>  QLOGIC QL41xxx FCOE DRIVER
>  M:	Saurav Kashyap <skashyap@marvell.com>
>  M:	Javed Hasan <jhasan@marvell.com>
> diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
> index f6c1bcbb57de..99e3334726a5 100644
> --- a/drivers/power/reset/Kconfig
> +++ b/drivers/power/reset/Kconfig
> @@ -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
> +	tristate "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
> diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
> index 0e4ae6f6b5c5..d7ae97241a83 100644
> --- a/drivers/power/reset/Makefile
> +++ b/drivers/power/reset/Makefile
> @@ -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
> diff --git a/drivers/power/reset/qemu-virt-ctrl.c b/drivers/power/reset/qemu-virt-ctrl.c
> new file mode 100644
> index 000000000000..f40d04afd4e3
> --- /dev/null
> +++ b/drivers/power/reset/qemu-virt-ctrl.c
> @@ -0,0 +1,122 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * QEMU Virt Machine System Controller Driver
> + *
> + * Copyright (C) 2026 Kuan-Wei Chiu <visitorckw@gmail.com>
> + */
> +
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/mod_devicetable.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 reboot_nb;
> +};
> +
> +static inline void virt_ctrl_write32(u32 val, void __iomem *addr)
> +{
> +	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
> +		iowrite32be(val, addr);
> +	else
> +		iowrite32(val, addr);
> +}
> +
> +static int qemu_virt_ctrl_power_off(struct sys_off_data *data)
> +{
> +	void __iomem *base = data->cb_data;
> +
> +	virt_ctrl_write32(CMD_HALT, base + VIRT_CTRL_REG_CMD);
> +
> +	return NOTIFY_DONE;
> +}
> +
> +static int qemu_virt_ctrl_restart(struct sys_off_data *data)
> +{
> +	void __iomem *base = data->cb_data;
> +
> +	virt_ctrl_write32(CMD_RESET, base + VIRT_CTRL_REG_CMD);
> +
> +	return NOTIFY_DONE;
> +}
> +
> +static int qemu_virt_ctrl_reboot_notify(struct notifier_block *nb,
> +					unsigned long action, void *data)
> +{
> +	struct qemu_virt_ctrl *ctrl = container_of(nb, struct qemu_virt_ctrl, reboot_nb);
> +
> +	if (action == SYS_HALT)
> +		virt_ctrl_write32(CMD_HALT, ctrl->base + VIRT_CTRL_REG_CMD);
> +
> +	return NOTIFY_DONE;
> +}
> +
> +static int qemu_virt_ctrl_probe(struct platform_device *pdev)
> +{
> +	struct qemu_virt_ctrl *ctrl;
> +	int ret;
> +
> +	ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
> +	if (!ctrl)
> +		return -ENOMEM;
> +
> +	ctrl->base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(ctrl->base))
> +		return PTR_ERR(ctrl->base);
> +
> +	ret = devm_register_sys_off_handler(&pdev->dev,
> +					    SYS_OFF_MODE_RESTART,
> +					    SYS_OFF_PRIO_DEFAULT,
> +					    qemu_virt_ctrl_restart,
> +					    ctrl->base);
> +	if (ret)
> +		return dev_err_probe(&pdev->dev, ret,
> +				     "cannot register restart handler\n");
> +
> +	ret = devm_register_sys_off_handler(&pdev->dev,
> +					    SYS_OFF_MODE_POWER_OFF,
> +					    SYS_OFF_PRIO_DEFAULT,
> +					    qemu_virt_ctrl_power_off,
> +					    ctrl->base);
> +	if (ret)
> +		return dev_err_probe(&pdev->dev, ret,
> +				     "cannot register power-off handler\n");
> +
> +	ctrl->reboot_nb.notifier_call = qemu_virt_ctrl_reboot_notify;
> +	ret = devm_register_reboot_notifier(&pdev->dev, &ctrl->reboot_nb);
> +	if (ret)
> +		return dev_err_probe(&pdev->dev, ret, "cannot register reboot notifier\n");
> +
> +	return 0;
> +}
> +
> +static const struct platform_device_id qemu_virt_ctrl_id[] = {
> +	{ "qemu-virt-ctrl", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(platform, qemu_virt_ctrl_id);
> +
> +static struct platform_driver qemu_virt_ctrl_driver = {
> +	.probe = qemu_virt_ctrl_probe,
> +	.driver = {
> +		.name = "qemu-virt-ctrl",
> +	},
> +	.id_table = qemu_virt_ctrl_id,
> +};
> +module_platform_driver(qemu_virt_ctrl_driver);
> +
> +MODULE_AUTHOR("Kuan-Wei Chiu <visitorckw@gmail.com>");
> +MODULE_DESCRIPTION("QEMU Virt Machine System Controller Driver");
> +MODULE_LICENSE("GPL");
> -- 
> 2.53.0.345.g96ddfc5eaa-goog
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver
  2026-02-22 17:32 ` [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver Kuan-Wei Chiu
@ 2026-04-08  1:17   ` Kuan-Wei Chiu
  2026-04-08  9:12   ` Geert Uytterhoeven
  1 sibling, 0 replies; 8+ messages in thread
From: Kuan-Wei Chiu @ 2026-04-08  1:17 UTC (permalink / raw)
  To: geert
  Cc: jserv, eleanor15x, daniel, laurent, linux-kernel, linux-m68k,
	linux-pm, sre

Hi Geert,

On Sun, Feb 22, 2026 at 05:32:25PM +0000, Kuan-Wei Chiu wrote:
> Register 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.
> 
> To prevent any regressions in reboot or power-off functionality when
> the driver is not built-in, explicitly select POWER_RESET and
> POWER_RESET_QEMU_VIRT_CTRL for the VIRT machine in Kconfig.machine.
> 
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>

Since the next merge window is approaching, is there anything else
needed for this to be picked up via the m68k tree?

Regards,
Kuan-Wei

> ---
> Changes in v3:
> - Add 'select POWER_RESET' and 'select POWER_RESET_QEMU_VIRT_CTRL' in
>   Kconfig.machine to avoid restart/power-off regressions.
> 
>  arch/m68k/Kconfig.machine |  2 ++
>  arch/m68k/virt/config.c   | 42 +--------------------------------------
>  arch/m68k/virt/platform.c | 20 ++++++++++++++++---
>  3 files changed, 20 insertions(+), 44 deletions(-)
> 
> diff --git a/arch/m68k/Kconfig.machine b/arch/m68k/Kconfig.machine
> index de39f23b180e..624e6b27f394 100644
> --- a/arch/m68k/Kconfig.machine
> +++ b/arch/m68k/Kconfig.machine
> @@ -133,6 +133,8 @@ config VIRT
>  	select GOLDFISH_TIMER
>  	select GOLDFISH_TTY
>  	select M68040
> +	select POWER_RESET
> +	select POWER_RESET_QEMU_VIRT_CTRL
>  	select RTC_CLASS
>  	select RTC_DRV_GOLDFISH
>  	select TTY
> diff --git a/arch/m68k/virt/config.c b/arch/m68k/virt/config.c
> index 632ba200ad42..b338e2a8da6a 100644
> --- a/arch/m68k/virt/config.c
> +++ b/arch/m68k/virt/config.c
> @@ -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);
>  }
> diff --git a/arch/m68k/virt/platform.c b/arch/m68k/virt/platform.c
> index 1560c4140ab9..764f556b4b32 100644
> --- a/arch/m68k/virt/platform.c
> +++ b/arch/m68k/virt/platform.c
> @@ -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);
> -- 
> 2.53.0.345.g96ddfc5eaa-goog
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver
  2026-04-02 21:52   ` Sebastian Reichel
@ 2026-04-08  9:10     ` Geert Uytterhoeven
  2026-04-08  9:10       ` Geert Uytterhoeven
  0 siblings, 1 reply; 8+ messages in thread
From: Geert Uytterhoeven @ 2026-04-08  9:10 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: Kuan-Wei Chiu, jserv, eleanor15x, daniel, laurent, linux-kernel,
	linux-m68k, linux-pm

On Thu, 2 Apr 2026 at 23:52, Sebastian Reichel
<sebastian.reichel@collabora.com> wrote:
> On Sun, Feb 22, 2026 at 05:32:24PM +0000, Kuan-Wei Chiu wrote:
> > 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 utilizes the modern system-off API to register callbacks
> > for both system restart and power off. It also registers a reboot
> > notifier to catch SYS_HALT events, ensuring that LINUX_REBOOT_CMD_HALT
> > is properly handled. It is designed to be generic and can be reused by
> > other architectures utilizing this QEMU device.
> >
> > Link: https://gitlab.com/qemu-project/qemu/-/blob/v10.2.0/hw/misc/virt_ctrl.c [1]
> > Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> > ---
>
> I think this should be merged with the second patch via the m68k
> tree:
>
> Acked-by: Sebastian Reichel <sebastian.reichel@collabora.com>

Thanks, will queue in the m68k tree for v7.2.

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver
  2026-04-08  9:10     ` Geert Uytterhoeven
@ 2026-04-08  9:10       ` Geert Uytterhoeven
  0 siblings, 0 replies; 8+ messages in thread
From: Geert Uytterhoeven @ 2026-04-08  9:10 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: Kuan-Wei Chiu, jserv, eleanor15x, daniel, laurent, linux-kernel,
	linux-m68k, linux-pm

On Wed, 8 Apr 2026 at 11:10, Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> On Thu, 2 Apr 2026 at 23:52, Sebastian Reichel
> <sebastian.reichel@collabora.com> wrote:
> > On Sun, Feb 22, 2026 at 05:32:24PM +0000, Kuan-Wei Chiu wrote:
> > > 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 utilizes the modern system-off API to register callbacks
> > > for both system restart and power off. It also registers a reboot
> > > notifier to catch SYS_HALT events, ensuring that LINUX_REBOOT_CMD_HALT
> > > is properly handled. It is designed to be generic and can be reused by
> > > other architectures utilizing this QEMU device.
> > >
> > > Link: https://gitlab.com/qemu-project/qemu/-/blob/v10.2.0/hw/misc/virt_ctrl.c [1]
> > > Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> > > ---
> >
> > I think this should be merged with the second patch via the m68k
> > tree:
> >
> > Acked-by: Sebastian Reichel <sebastian.reichel@collabora.com>
>
> Thanks, will queue in the m68k tree for v7.2.

Oops, v7.1.

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver
  2026-02-22 17:32 ` [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver Kuan-Wei Chiu
  2026-04-08  1:17   ` Kuan-Wei Chiu
@ 2026-04-08  9:12   ` Geert Uytterhoeven
  1 sibling, 0 replies; 8+ messages in thread
From: Geert Uytterhoeven @ 2026-04-08  9:12 UTC (permalink / raw)
  To: Kuan-Wei Chiu
  Cc: sre, jserv, eleanor15x, daniel, laurent, linux-kernel, linux-m68k,
	linux-pm

On Sun, 22 Feb 2026 at 18:32, Kuan-Wei Chiu <visitorckw@gmail.com> wrote:
> Register 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.
>
> To prevent any regressions in reboot or power-off functionality when
> the driver is not built-in, explicitly select POWER_RESET and
> POWER_RESET_QEMU_VIRT_CTRL for the VIRT machine in Kconfig.machine.
>
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---
> Changes in v3:
> - Add 'select POWER_RESET' and 'select POWER_RESET_QEMU_VIRT_CTRL' in
>   Kconfig.machine to avoid restart/power-off regressions.

Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org>
i.e. will queue in the m68k tree for v7.1.

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-04-08  9:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-22 17:32 [PATCH v3 0/2] Add QEMU virt-ctrl driver and update m68k virt Kuan-Wei Chiu
2026-02-22 17:32 ` [PATCH v3 1/2] power: reset: Add QEMU virt-ctrl driver Kuan-Wei Chiu
2026-04-02 21:52   ` Sebastian Reichel
2026-04-08  9:10     ` Geert Uytterhoeven
2026-04-08  9:10       ` Geert Uytterhoeven
2026-02-22 17:32 ` [PATCH v3 2/2] m68k: virt: Switch to qemu-virt-ctrl driver Kuan-Wei Chiu
2026-04-08  1:17   ` Kuan-Wei Chiu
2026-04-08  9:12   ` Geert Uytterhoeven

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox