1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Pvpanic Device Support
4 *
5 * Copyright (C) 2013 Fujitsu.
6 * Copyright (C) 2018 ZTE.
7 * Copyright (C) 2021 Oracle.
8 */
9
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/gfp_types.h>
13 #include <linux/io.h>
14 #include <linux/kexec.h>
15 #include <linux/kstrtox.h>
16 #include <linux/limits.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/panic_notifier.h>
20 #include <linux/platform_device.h>
21 #include <linux/reboot.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysfs.h>
24 #include <linux/types.h>
25
26 #include <uapi/misc/pvpanic.h>
27
28 #include "pvpanic.h"
29
30 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
31 MODULE_DESCRIPTION("pvpanic device driver");
32 MODULE_LICENSE("GPL");
33
34 struct pvpanic_instance {
35 void __iomem *base;
36 unsigned int capability;
37 unsigned int events;
38 struct sys_off_handler *sys_off;
39 struct list_head list;
40 };
41
42 static struct list_head pvpanic_list;
43 static spinlock_t pvpanic_lock;
44
45 static void
pvpanic_send_event(unsigned int event)46 pvpanic_send_event(unsigned int event)
47 {
48 struct pvpanic_instance *pi_cur;
49
50 if (!spin_trylock(&pvpanic_lock))
51 return;
52
53 list_for_each_entry(pi_cur, &pvpanic_list, list) {
54 if (event & pi_cur->capability & pi_cur->events)
55 iowrite8(event, pi_cur->base);
56 }
57 spin_unlock(&pvpanic_lock);
58 }
59
60 static int
pvpanic_panic_notify(struct notifier_block * nb,unsigned long code,void * unused)61 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code, void *unused)
62 {
63 unsigned int event = PVPANIC_PANICKED;
64
65 if (kexec_crash_loaded())
66 event = PVPANIC_CRASH_LOADED;
67
68 pvpanic_send_event(event);
69
70 return NOTIFY_DONE;
71 }
72
73 /*
74 * Call our notifier very early on panic, deferring the
75 * action taken to the hypervisor.
76 */
77 static struct notifier_block pvpanic_panic_nb = {
78 .notifier_call = pvpanic_panic_notify,
79 .priority = INT_MAX,
80 };
81
pvpanic_sys_off(struct sys_off_data * data)82 static int pvpanic_sys_off(struct sys_off_data *data)
83 {
84 pvpanic_send_event(PVPANIC_SHUTDOWN);
85
86 return NOTIFY_DONE;
87 }
88
pvpanic_synchronize_sys_off_handler(struct device * dev,struct pvpanic_instance * pi)89 static void pvpanic_synchronize_sys_off_handler(struct device *dev, struct pvpanic_instance *pi)
90 {
91 /* The kernel core has logic to fall back to system halt if no
92 * sys_off_handler is registered.
93 * When the pvpanic sys_off_handler is disabled via sysfs the kernel
94 * should use that fallback logic, so the handler needs to be unregistered.
95 */
96
97 struct sys_off_handler *sys_off;
98
99 if (!(pi->events & PVPANIC_SHUTDOWN) == !pi->sys_off)
100 return;
101
102 if (!pi->sys_off) {
103 sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_LOW,
104 pvpanic_sys_off, NULL);
105 if (IS_ERR(sys_off))
106 dev_warn(dev, "Could not register sys_off_handler: %pe\n", sys_off);
107 else
108 pi->sys_off = sys_off;
109 } else {
110 unregister_sys_off_handler(pi->sys_off);
111 pi->sys_off = NULL;
112 }
113 }
114
pvpanic_remove(void * param)115 static void pvpanic_remove(void *param)
116 {
117 struct pvpanic_instance *pi_cur, *pi_next;
118 struct pvpanic_instance *pi = param;
119
120 spin_lock(&pvpanic_lock);
121 list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
122 if (pi_cur == pi) {
123 list_del(&pi_cur->list);
124 break;
125 }
126 }
127 spin_unlock(&pvpanic_lock);
128
129 unregister_sys_off_handler(pi->sys_off);
130 }
131
capability_show(struct device * dev,struct device_attribute * attr,char * buf)132 static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
133 {
134 struct pvpanic_instance *pi = dev_get_drvdata(dev);
135
136 return sysfs_emit(buf, "%x\n", pi->capability);
137 }
138 static DEVICE_ATTR_RO(capability);
139
events_show(struct device * dev,struct device_attribute * attr,char * buf)140 static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
141 {
142 struct pvpanic_instance *pi = dev_get_drvdata(dev);
143
144 return sysfs_emit(buf, "%x\n", pi->events);
145 }
146
events_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)147 static ssize_t events_store(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t count)
149 {
150 struct pvpanic_instance *pi = dev_get_drvdata(dev);
151 unsigned int tmp;
152 int err;
153
154 err = kstrtouint(buf, 16, &tmp);
155 if (err)
156 return err;
157
158 if ((tmp & pi->capability) != tmp)
159 return -EINVAL;
160
161 pi->events = tmp;
162 pvpanic_synchronize_sys_off_handler(dev, pi);
163
164 return count;
165 }
166 static DEVICE_ATTR_RW(events);
167
168 static struct attribute *pvpanic_dev_attrs[] = {
169 &dev_attr_capability.attr,
170 &dev_attr_events.attr,
171 NULL
172 };
173
174 static const struct attribute_group pvpanic_dev_group = {
175 .attrs = pvpanic_dev_attrs,
176 };
177
178 const struct attribute_group *pvpanic_dev_groups[] = {
179 &pvpanic_dev_group,
180 NULL
181 };
182 EXPORT_SYMBOL_GPL(pvpanic_dev_groups);
183
devm_pvpanic_probe(struct device * dev,void __iomem * base)184 int devm_pvpanic_probe(struct device *dev, void __iomem *base)
185 {
186 struct pvpanic_instance *pi;
187
188 if (!base)
189 return -EINVAL;
190
191 pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
192 if (!pi)
193 return -ENOMEM;
194
195 pi->base = base;
196 pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | PVPANIC_SHUTDOWN;
197
198 /* initlize capability by RDPT */
199 pi->capability &= ioread8(base);
200 pi->events = pi->capability;
201
202 pi->sys_off = NULL;
203 pvpanic_synchronize_sys_off_handler(dev, pi);
204
205 spin_lock(&pvpanic_lock);
206 list_add(&pi->list, &pvpanic_list);
207 spin_unlock(&pvpanic_lock);
208
209 dev_set_drvdata(dev, pi);
210
211 return devm_add_action_or_reset(dev, pvpanic_remove, pi);
212 }
213 EXPORT_SYMBOL_GPL(devm_pvpanic_probe);
214
pvpanic_init(void)215 static int pvpanic_init(void)
216 {
217 INIT_LIST_HEAD(&pvpanic_list);
218 spin_lock_init(&pvpanic_lock);
219
220 atomic_notifier_chain_register(&panic_notifier_list, &pvpanic_panic_nb);
221
222 return 0;
223 }
224 module_init(pvpanic_init);
225
pvpanic_exit(void)226 static void pvpanic_exit(void)
227 {
228 atomic_notifier_chain_unregister(&panic_notifier_list, &pvpanic_panic_nb);
229
230 }
231 module_exit(pvpanic_exit);
232