xref: /openbmc/linux/drivers/misc/pvpanic/pvpanic.c (revision e9194a95)
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/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/panic_notifier.h>
18 #include <linux/types.h>
19 #include <linux/cdev.h>
20 #include <linux/list.h>
21 
22 #include <uapi/misc/pvpanic.h>
23 
24 #include "pvpanic.h"
25 
26 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
27 MODULE_DESCRIPTION("pvpanic device driver");
28 MODULE_LICENSE("GPL");
29 
30 struct pvpanic_instance {
31 	void __iomem *base;
32 	unsigned int capability;
33 	unsigned int events;
34 	struct list_head list;
35 };
36 
37 static struct list_head pvpanic_list;
38 static spinlock_t pvpanic_lock;
39 
40 static void
pvpanic_send_event(unsigned int event)41 pvpanic_send_event(unsigned int event)
42 {
43 	struct pvpanic_instance *pi_cur;
44 
45 	if (!spin_trylock(&pvpanic_lock))
46 		return;
47 
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);
51 	}
52 	spin_unlock(&pvpanic_lock);
53 }
54 
55 static int
pvpanic_panic_notify(struct notifier_block * nb,unsigned long code,void * unused)56 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code, void *unused)
57 {
58 	unsigned int event = PVPANIC_PANICKED;
59 
60 	if (kexec_crash_loaded())
61 		event = PVPANIC_CRASH_LOADED;
62 
63 	pvpanic_send_event(event);
64 
65 	return NOTIFY_DONE;
66 }
67 
68 /*
69  * Call our notifier very early on panic, deferring the
70  * action taken to the hypervisor.
71  */
72 static struct notifier_block pvpanic_panic_nb = {
73 	.notifier_call = pvpanic_panic_notify,
74 	.priority = INT_MAX,
75 };
76 
pvpanic_remove(void * param)77 static void pvpanic_remove(void *param)
78 {
79 	struct pvpanic_instance *pi_cur, *pi_next;
80 	struct pvpanic_instance *pi = param;
81 
82 	spin_lock(&pvpanic_lock);
83 	list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
84 		if (pi_cur == pi) {
85 			list_del(&pi_cur->list);
86 			break;
87 		}
88 	}
89 	spin_unlock(&pvpanic_lock);
90 }
91 
capability_show(struct device * dev,struct device_attribute * attr,char * buf)92 static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
93 {
94 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
95 
96 	return sysfs_emit(buf, "%x\n", pi->capability);
97 }
98 static DEVICE_ATTR_RO(capability);
99 
events_show(struct device * dev,struct device_attribute * attr,char * buf)100 static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
101 {
102 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
103 
104 	return sysfs_emit(buf, "%x\n", pi->events);
105 }
106 
events_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)107 static ssize_t events_store(struct device *dev, struct device_attribute *attr,
108 			    const char *buf, size_t count)
109 {
110 	struct pvpanic_instance *pi = dev_get_drvdata(dev);
111 	unsigned int tmp;
112 	int err;
113 
114 	err = kstrtouint(buf, 16, &tmp);
115 	if (err)
116 		return err;
117 
118 	if ((tmp & pi->capability) != tmp)
119 		return -EINVAL;
120 
121 	pi->events = tmp;
122 
123 	return count;
124 }
125 static DEVICE_ATTR_RW(events);
126 
127 static struct attribute *pvpanic_dev_attrs[] = {
128 	&dev_attr_capability.attr,
129 	&dev_attr_events.attr,
130 	NULL
131 };
132 
133 static const struct attribute_group pvpanic_dev_group = {
134 	.attrs = pvpanic_dev_attrs,
135 };
136 
137 const struct attribute_group *pvpanic_dev_groups[] = {
138 	&pvpanic_dev_group,
139 	NULL
140 };
141 EXPORT_SYMBOL_GPL(pvpanic_dev_groups);
142 
devm_pvpanic_probe(struct device * dev,void __iomem * base)143 int devm_pvpanic_probe(struct device *dev, void __iomem *base)
144 {
145 	struct pvpanic_instance *pi;
146 
147 	if (!base)
148 		return -EINVAL;
149 
150 	pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
151 	if (!pi)
152 		return -ENOMEM;
153 
154 	pi->base = base;
155 	pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
156 
157 	/* initlize capability by RDPT */
158 	pi->capability &= ioread8(base);
159 	pi->events = pi->capability;
160 
161 	spin_lock(&pvpanic_lock);
162 	list_add(&pi->list, &pvpanic_list);
163 	spin_unlock(&pvpanic_lock);
164 
165 	dev_set_drvdata(dev, pi);
166 
167 	return devm_add_action_or_reset(dev, pvpanic_remove, pi);
168 }
169 EXPORT_SYMBOL_GPL(devm_pvpanic_probe);
170 
pvpanic_init(void)171 static int pvpanic_init(void)
172 {
173 	INIT_LIST_HEAD(&pvpanic_list);
174 	spin_lock_init(&pvpanic_lock);
175 
176 	atomic_notifier_chain_register(&panic_notifier_list, &pvpanic_panic_nb);
177 
178 	return 0;
179 }
180 module_init(pvpanic_init);
181 
pvpanic_exit(void)182 static void pvpanic_exit(void)
183 {
184 	atomic_notifier_chain_unregister(&panic_notifier_list, &pvpanic_panic_nb);
185 
186 }
187 module_exit(pvpanic_exit);
188