Merge tag 'amd-drm-fixes-5.12-2021-04-08' of https://gitlab.freedesktop.org/agd5f...
[linux-2.6-microblaze.git] / drivers / misc / pvpanic.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Pvpanic Device Support
4  *
5  *  Copyright (C) 2013 Fujitsu.
6  *  Copyright (C) 2018 ZTE.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/io.h>
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>
18
19 #include <uapi/misc/pvpanic.h>
20
21 static void __iomem *base;
22 static unsigned int capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
23 static unsigned int events;
24
25 static ssize_t capability_show(struct device *dev,
26                                struct device_attribute *attr, char *buf)
27 {
28         return sysfs_emit(buf, "%x\n", capability);
29 }
30 static DEVICE_ATTR_RO(capability);
31
32 static ssize_t events_show(struct device *dev,  struct device_attribute *attr, char *buf)
33 {
34         return sysfs_emit(buf, "%x\n", events);
35 }
36
37 static ssize_t events_store(struct device *dev,  struct device_attribute *attr,
38                             const char *buf, size_t count)
39 {
40         unsigned int tmp;
41         int err;
42
43         err = kstrtouint(buf, 16, &tmp);
44         if (err)
45                 return err;
46
47         if ((tmp & capability) != tmp)
48                 return -EINVAL;
49
50         events = tmp;
51
52         return count;
53
54 }
55 static DEVICE_ATTR_RW(events);
56
57 static struct attribute *pvpanic_dev_attrs[] = {
58         &dev_attr_capability.attr,
59         &dev_attr_events.attr,
60         NULL
61 };
62 ATTRIBUTE_GROUPS(pvpanic_dev);
63
64 MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
65 MODULE_DESCRIPTION("pvpanic device driver");
66 MODULE_LICENSE("GPL");
67
68 static void
69 pvpanic_send_event(unsigned int event)
70 {
71         if (event & capability & events)
72                 iowrite8(event, base);
73 }
74
75 static int
76 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
77                      void *unused)
78 {
79         unsigned int event = PVPANIC_PANICKED;
80
81         if (kexec_crash_loaded())
82                 event = PVPANIC_CRASH_LOADED;
83
84         pvpanic_send_event(event);
85
86         return NOTIFY_DONE;
87 }
88
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 */
92 };
93
94 static int pvpanic_mmio_probe(struct platform_device *pdev)
95 {
96         struct device *dev = &pdev->dev;
97         struct resource *res;
98
99         res = platform_get_mem_or_io(pdev, 0);
100         if (!res)
101                 return -EINVAL;
102
103         switch (resource_type(res)) {
104         case IORESOURCE_IO:
105                 base = devm_ioport_map(dev, res->start, resource_size(res));
106                 if (!base)
107                         return -ENOMEM;
108                 break;
109         case IORESOURCE_MEM:
110                 base = devm_ioremap_resource(dev, res);
111                 if (IS_ERR(base))
112                         return PTR_ERR(base);
113                 break;
114         default:
115                 return -EINVAL;
116         }
117
118         /* initlize capability by RDPT */
119         capability &= ioread8(base);
120         events = capability;
121
122         if (capability)
123                 atomic_notifier_chain_register(&panic_notifier_list,
124                                                &pvpanic_panic_nb);
125
126         return 0;
127 }
128
129 static int pvpanic_mmio_remove(struct platform_device *pdev)
130 {
131
132         if (capability)
133                 atomic_notifier_chain_unregister(&panic_notifier_list,
134                                                  &pvpanic_panic_nb);
135
136         return 0;
137 }
138
139 static const struct of_device_id pvpanic_mmio_match[] = {
140         { .compatible = "qemu,pvpanic-mmio", },
141         {}
142 };
143 MODULE_DEVICE_TABLE(of, pvpanic_mmio_match);
144
145 static const struct acpi_device_id pvpanic_device_ids[] = {
146         { "QEMU0001", 0 },
147         { "", 0 }
148 };
149 MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
150
151 static struct platform_driver pvpanic_mmio_driver = {
152         .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,
157         },
158         .probe = pvpanic_mmio_probe,
159         .remove = pvpanic_mmio_remove,
160 };
161 module_platform_driver(pvpanic_mmio_driver);