1 // SPDX-License-Identifier: GPL-2.0+
3 * Pvpanic Device Support
5 * Copyright (C) 2013 Fujitsu.
6 * Copyright (C) 2018 ZTE.
7 * Copyright (C) 2021 Oracle.
10 #include <linux/device.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/panic_notifier.h>
18 #include <linux/types.h>
19 #include <linux/cdev.h>
20 #include <linux/list.h>
22 #include <uapi/misc/pvpanic.h>
26 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
27 MODULE_DESCRIPTION("pvpanic device driver");
28 MODULE_LICENSE("GPL");
30 struct pvpanic_instance {
32 unsigned int capability;
34 struct list_head list;
37 static struct list_head pvpanic_list;
38 static spinlock_t pvpanic_lock;
41 pvpanic_send_event(unsigned int event)
43 struct pvpanic_instance *pi_cur;
45 if (!spin_trylock(&pvpanic_lock))
48 list_for_each_entry(pi_cur, &pvpanic_list, list) {
49 if (event & pi_cur->capability & pi_cur->events)
50 iowrite8(event, pi_cur->base);
52 spin_unlock(&pvpanic_lock);
56 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code, void *unused)
58 unsigned int event = PVPANIC_PANICKED;
60 if (kexec_crash_loaded())
61 event = PVPANIC_CRASH_LOADED;
63 pvpanic_send_event(event);
69 * Call our notifier very early on panic, deferring the
70 * action taken to the hypervisor.
72 static struct notifier_block pvpanic_panic_nb = {
73 .notifier_call = pvpanic_panic_notify,
77 static void pvpanic_remove(void *param)
79 struct pvpanic_instance *pi_cur, *pi_next;
80 struct pvpanic_instance *pi = param;
82 spin_lock(&pvpanic_lock);
83 list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
85 list_del(&pi_cur->list);
89 spin_unlock(&pvpanic_lock);
92 static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
94 struct pvpanic_instance *pi = dev_get_drvdata(dev);
96 return sysfs_emit(buf, "%x\n", pi->capability);
98 static DEVICE_ATTR_RO(capability);
100 static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
102 struct pvpanic_instance *pi = dev_get_drvdata(dev);
104 return sysfs_emit(buf, "%x\n", pi->events);
107 static ssize_t events_store(struct device *dev, struct device_attribute *attr,
108 const char *buf, size_t count)
110 struct pvpanic_instance *pi = dev_get_drvdata(dev);
114 err = kstrtouint(buf, 16, &tmp);
118 if ((tmp & pi->capability) != tmp)
125 static DEVICE_ATTR_RW(events);
127 static struct attribute *pvpanic_dev_attrs[] = {
128 &dev_attr_capability.attr,
129 &dev_attr_events.attr,
133 static const struct attribute_group pvpanic_dev_group = {
134 .attrs = pvpanic_dev_attrs,
137 const struct attribute_group *pvpanic_dev_groups[] = {
141 EXPORT_SYMBOL_GPL(pvpanic_dev_groups);
143 int devm_pvpanic_probe(struct device *dev, void __iomem *base)
145 struct pvpanic_instance *pi;
150 pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
155 pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
157 /* initlize capability by RDPT */
158 pi->capability &= ioread8(base);
159 pi->events = pi->capability;
161 spin_lock(&pvpanic_lock);
162 list_add(&pi->list, &pvpanic_list);
163 spin_unlock(&pvpanic_lock);
165 dev_set_drvdata(dev, pi);
167 return devm_add_action_or_reset(dev, pvpanic_remove, pi);
169 EXPORT_SYMBOL_GPL(devm_pvpanic_probe);
171 static int pvpanic_init(void)
173 INIT_LIST_HEAD(&pvpanic_list);
174 spin_lock_init(&pvpanic_lock);
176 atomic_notifier_chain_register(&panic_notifier_list, &pvpanic_panic_nb);
180 module_init(pvpanic_init);
182 static void pvpanic_exit(void)
184 atomic_notifier_chain_unregister(&panic_notifier_list, &pvpanic_panic_nb);
187 module_exit(pvpanic_exit);