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
12 #include <linux/kernel.h>
13 #include <linux/kexec.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/types.h>
19 #include <uapi/misc/pvpanic.h>
21 static void __iomem *base;
22 static unsigned int capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
23 static unsigned int events;
25 static ssize_t capability_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
28 return sysfs_emit(buf, "%x\n", capability);
30 static DEVICE_ATTR_RO(capability);
32 static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
34 return sysfs_emit(buf, "%x\n", events);
37 static ssize_t events_store(struct device *dev, struct device_attribute *attr,
38 const char *buf, size_t count)
43 err = kstrtouint(buf, 16, &tmp);
47 if ((tmp & capability) != tmp)
55 static DEVICE_ATTR_RW(events);
57 static struct attribute *pvpanic_dev_attrs[] = {
58 &dev_attr_capability.attr,
59 &dev_attr_events.attr,
62 ATTRIBUTE_GROUPS(pvpanic_dev);
65 MODULE_DESCRIPTION("pvpanic device driver");
66 MODULE_LICENSE("GPL");
69 pvpanic_send_event(unsigned int event)
71 if (event & capability & events)
72 iowrite8(event, base);
76 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
79 unsigned int event = PVPANIC_PANICKED;
81 if (kexec_crash_loaded())
82 event = PVPANIC_CRASH_LOADED;
84 pvpanic_send_event(event);
89 static struct notifier_block pvpanic_panic_nb = {
90 .notifier_call = pvpanic_panic_notify,
91 .priority = 1, /* let this called before broken drm_fb_helper */
94 static int pvpanic_mmio_probe(struct platform_device *pdev)
96 struct device *dev = &pdev->dev;
99 res = platform_get_mem_or_io(pdev, 0);
103 switch (resource_type(res)) {
105 base = devm_ioport_map(dev, res->start, resource_size(res));
110 base = devm_ioremap_resource(dev, res);
112 return PTR_ERR(base);
118 /* initlize capability by RDPT */
119 capability &= ioread8(base);
123 atomic_notifier_chain_register(&panic_notifier_list,
129 static int pvpanic_mmio_remove(struct platform_device *pdev)
133 atomic_notifier_chain_unregister(&panic_notifier_list,
139 static const struct of_device_id pvpanic_mmio_match[] = {
140 { .compatible = "qemu,pvpanic-mmio", },
143 MODULE_DEVICE_TABLE(of, pvpanic_mmio_match);
145 static const struct acpi_device_id pvpanic_device_ids[] = {
149 MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
151 static struct platform_driver pvpanic_mmio_driver = {
153 .name = "pvpanic-mmio",
154 .of_match_table = pvpanic_mmio_match,
155 .acpi_match_table = pvpanic_device_ids,
156 .dev_groups = pvpanic_dev_groups,
158 .probe = pvpanic_mmio_probe,
159 .remove = pvpanic_mmio_remove,
161 module_platform_driver(pvpanic_mmio_driver);