diff options
| author | Yazen Ghannam <yazen.ghannam@amd.com> | 2026-09-03 10:43:25 -0500 |
|---|---|---|
| committer | Borislav Petkov (AMD) <bp@alien8.de> | 2026-09-10 18:05:43 -0700 |
| commit | 27600805e62f800bacf990354632eae4e487d34c (patch) | |
| tree | e8aec0ddfaa0bc264d79af632e3de3f1b75ae4c8 /drivers/power/reset/qemu-virt-ctrl.c | |
| download | linux-stable-27600805e62f800bacf990354632eae4e487d34c.tar.gz linux-stable-27600805e62f800bacf990354632eae4e487d34c.zip | |
x86/amd_node: Fix PCI device reference counting in amd_smn_init()grafted
The local "root" pointer is a temporary variable used during the device
search. Therefore, refcount related to the search iterators should be cleaned
up after the search is complete.
Use the __free() cleanup macro to ensure the refcount is decremented when the
temporary pointer goes out of scope.
Additionally, increment the refcount when caching a root pointer. This ensures
the in-use refcount is separate from the temporary search refcounting.
Finally, drop the redundant "root = NULL" before the second search loop. The
pci_get_class() iterator always decrements the refcount of its "from"
argument, so the first loop can only fall through with "root" already NULL.
Fixes: 0a4b61d9c2e4 ("x86/amd_node: Fix AMD root device caching")
Closes: https://sashiko.dev/#/patchset/20260806160159.230453-1-jason.andryuk%40amd.com
Reported-by: Sashiko <sashiko-bot@kernel.org>
Assisted-by: LLM
Signed-off-by: Yazen Ghannam <yazen.ghannam@amd.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Mario Limonciello (AMD) <superm1@kernel.org>
Cc: <stable@kernel.org>
Link: https://patch.msgid.link/20260903154325.74343-1-yazen.ghannam@amd.com
Diffstat (limited to 'drivers/power/reset/qemu-virt-ctrl.c')
| -rw-r--r-- | drivers/power/reset/qemu-virt-ctrl.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/drivers/power/reset/qemu-virt-ctrl.c b/drivers/power/reset/qemu-virt-ctrl.c new file mode 100644 index 000000000..97875007f --- /dev/null +++ b/drivers/power/reset/qemu-virt-ctrl.c @@ -0,0 +1,121 @@ +// 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/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) +{ + struct qemu_virt_ctrl *ctrl = data->cb_data; + + virt_ctrl_write32(CMD_HALT, ctrl->base + VIRT_CTRL_REG_CMD); + + return NOTIFY_DONE; +} + +static int qemu_virt_ctrl_restart(struct sys_off_data *data) +{ + struct qemu_virt_ctrl *ctrl = data->cb_data; + + virt_ctrl_write32(CMD_RESET, ctrl->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); + 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); + 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[] = { + { .name = "qemu-virt-ctrl" }, + { } +}; +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"); |
