1 // SPDX-License-Identifier: GPL-2.0+
3 * Pvpanic Device Support
5 * Copyright (C) 2013 Fujitsu.
6 * Copyright (C) 2018 ZTE.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/platform_device.h>
17 #include <linux/types.h>
19 static void __iomem *base;
21 #define PVPANIC_PANICKED (1 << 0)
23 MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
24 MODULE_DESCRIPTION("pvpanic device driver");
25 MODULE_LICENSE("GPL");
28 pvpanic_send_event(unsigned int event)
30 iowrite8(event, base);
34 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
37 pvpanic_send_event(PVPANIC_PANICKED);
41 static struct notifier_block pvpanic_panic_nb = {
42 .notifier_call = pvpanic_panic_notify,
43 .priority = 1, /* let this called before broken drm_fb_helper */
47 static int pvpanic_add(struct acpi_device *device);
48 static int pvpanic_remove(struct acpi_device *device);
50 static const struct acpi_device_id pvpanic_device_ids[] = {
54 MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
56 static struct acpi_driver pvpanic_driver = {
59 .ids = pvpanic_device_ids,
62 .remove = pvpanic_remove,
68 pvpanic_walk_resources(struct acpi_resource *res, void *context)
72 if (acpi_dev_resource_io(res, &r)) {
73 base = ioport_map(r.start, resource_size(&r));
75 } else if (acpi_dev_resource_memory(res, &r)) {
76 base = ioremap(r.start, resource_size(&r));
83 static int pvpanic_add(struct acpi_device *device)
87 ret = acpi_bus_get_status(device);
91 if (!device->status.enabled || !device->status.functional)
94 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
95 pvpanic_walk_resources, NULL);
100 atomic_notifier_chain_register(&panic_notifier_list,
106 static int pvpanic_remove(struct acpi_device *device)
109 atomic_notifier_chain_unregister(&panic_notifier_list,
116 static int pvpanic_register_acpi_driver(void)
118 return acpi_bus_register_driver(&pvpanic_driver);
121 static void pvpanic_unregister_acpi_driver(void)
123 acpi_bus_unregister_driver(&pvpanic_driver);
126 static int pvpanic_register_acpi_driver(void)
131 static void pvpanic_unregister_acpi_driver(void) {}
134 static int pvpanic_mmio_probe(struct platform_device *pdev)
136 struct resource *mem;
138 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 base = devm_ioremap_resource(&pdev->dev, mem);
144 return PTR_ERR(base);
146 atomic_notifier_chain_register(&panic_notifier_list,
152 static int pvpanic_mmio_remove(struct platform_device *pdev)
155 atomic_notifier_chain_unregister(&panic_notifier_list,
161 static const struct of_device_id pvpanic_mmio_match[] = {
162 { .compatible = "qemu,pvpanic-mmio", },
166 static struct platform_driver pvpanic_mmio_driver = {
168 .name = "pvpanic-mmio",
169 .of_match_table = pvpanic_mmio_match,
171 .probe = pvpanic_mmio_probe,
172 .remove = pvpanic_mmio_remove,
175 static int __init pvpanic_mmio_init(void)
178 return platform_driver_register(&pvpanic_mmio_driver);
180 return pvpanic_register_acpi_driver();
183 static void __exit pvpanic_mmio_exit(void)
186 platform_driver_unregister(&pvpanic_mmio_driver);
188 pvpanic_unregister_acpi_driver();
191 module_init(pvpanic_mmio_init);
192 module_exit(pvpanic_mmio_exit);