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/kexec.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
19 #include <uapi/misc/pvpanic.h>
21 static void __iomem
*base
;
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 unsigned int event
= PVPANIC_PANICKED
;
39 if (kexec_crash_loaded())
40 event
= PVPANIC_CRASH_LOADED
;
42 pvpanic_send_event(event
);
47 static struct notifier_block pvpanic_panic_nb
= {
48 .notifier_call
= pvpanic_panic_notify
,
49 .priority
= 1, /* let this called before broken drm_fb_helper */
53 static int pvpanic_add(struct acpi_device
*device
);
54 static int pvpanic_remove(struct acpi_device
*device
);
56 static const struct acpi_device_id pvpanic_device_ids
[] = {
60 MODULE_DEVICE_TABLE(acpi
, pvpanic_device_ids
);
62 static struct acpi_driver pvpanic_driver
= {
65 .ids
= pvpanic_device_ids
,
68 .remove
= pvpanic_remove
,
74 pvpanic_walk_resources(struct acpi_resource
*res
, void *context
)
78 if (acpi_dev_resource_io(res
, &r
)) {
79 #ifdef CONFIG_HAS_IOPORT_MAP
80 base
= ioport_map(r
.start
, resource_size(&r
));
85 } else if (acpi_dev_resource_memory(res
, &r
)) {
86 base
= ioremap(r
.start
, resource_size(&r
));
93 static int pvpanic_add(struct acpi_device
*device
)
97 ret
= acpi_bus_get_status(device
);
101 if (!device
->status
.enabled
|| !device
->status
.functional
)
104 acpi_walk_resources(device
->handle
, METHOD_NAME__CRS
,
105 pvpanic_walk_resources
, NULL
);
110 atomic_notifier_chain_register(&panic_notifier_list
,
116 static int pvpanic_remove(struct acpi_device
*device
)
119 atomic_notifier_chain_unregister(&panic_notifier_list
,
126 static int pvpanic_register_acpi_driver(void)
128 return acpi_bus_register_driver(&pvpanic_driver
);
131 static void pvpanic_unregister_acpi_driver(void)
133 acpi_bus_unregister_driver(&pvpanic_driver
);
136 static int pvpanic_register_acpi_driver(void)
141 static void pvpanic_unregister_acpi_driver(void) {}
144 static int pvpanic_mmio_probe(struct platform_device
*pdev
)
146 struct resource
*mem
;
148 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
152 base
= devm_ioremap_resource(&pdev
->dev
, mem
);
154 return PTR_ERR(base
);
156 atomic_notifier_chain_register(&panic_notifier_list
,
162 static int pvpanic_mmio_remove(struct platform_device
*pdev
)
165 atomic_notifier_chain_unregister(&panic_notifier_list
,
171 static const struct of_device_id pvpanic_mmio_match
[] = {
172 { .compatible
= "qemu,pvpanic-mmio", },
176 static struct platform_driver pvpanic_mmio_driver
= {
178 .name
= "pvpanic-mmio",
179 .of_match_table
= pvpanic_mmio_match
,
181 .probe
= pvpanic_mmio_probe
,
182 .remove
= pvpanic_mmio_remove
,
185 static int __init
pvpanic_mmio_init(void)
188 return platform_driver_register(&pvpanic_mmio_driver
);
190 return pvpanic_register_acpi_driver();
193 static void __exit
pvpanic_mmio_exit(void)
196 platform_driver_unregister(&pvpanic_mmio_driver
);
198 pvpanic_unregister_acpi_driver();
201 module_init(pvpanic_mmio_init
);
202 module_exit(pvpanic_mmio_exit
);