xref: /openbmc/linux/drivers/misc/pvpanic/pvpanic.c (revision 31eeb6b0)
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/io.h>
11 #include <linux/kernel.h>
12 #include <linux/kexec.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/panic_notifier.h>
17 #include <linux/types.h>
18 #include <linux/cdev.h>
19 #include <linux/list.h>
20 
21 #include <uapi/misc/pvpanic.h>
22 
23 #include "pvpanic.h"
24 
25 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
26 MODULE_DESCRIPTION("pvpanic device driver");
27 MODULE_LICENSE("GPL");
28 
29 static struct list_head pvpanic_list;
30 static spinlock_t pvpanic_lock;
31 
32 static void
33 pvpanic_send_event(unsigned int event)
34 {
35 	struct pvpanic_instance *pi_cur;
36 
37 	spin_lock(&pvpanic_lock);
38 	list_for_each_entry(pi_cur, &pvpanic_list, list) {
39 		if (event & pi_cur->capability & pi_cur->events)
40 			iowrite8(event, pi_cur->base);
41 	}
42 	spin_unlock(&pvpanic_lock);
43 }
44 
45 static int
46 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code, void *unused)
47 {
48 	unsigned int event = PVPANIC_PANICKED;
49 
50 	if (kexec_crash_loaded())
51 		event = PVPANIC_CRASH_LOADED;
52 
53 	pvpanic_send_event(event);
54 
55 	return NOTIFY_DONE;
56 }
57 
58 static struct notifier_block pvpanic_panic_nb = {
59 	.notifier_call = pvpanic_panic_notify,
60 	.priority = 1, /* let this called before broken drm_fb_helper() */
61 };
62 
63 static void pvpanic_remove(void *param)
64 {
65 	struct pvpanic_instance *pi_cur, *pi_next;
66 	struct pvpanic_instance *pi = param;
67 
68 	spin_lock(&pvpanic_lock);
69 	list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
70 		if (pi_cur == pi) {
71 			list_del(&pi_cur->list);
72 			break;
73 		}
74 	}
75 	spin_unlock(&pvpanic_lock);
76 }
77 
78 int devm_pvpanic_probe(struct device *dev, struct pvpanic_instance *pi)
79 {
80 	if (!pi || !pi->base)
81 		return -EINVAL;
82 
83 	spin_lock(&pvpanic_lock);
84 	list_add(&pi->list, &pvpanic_list);
85 	spin_unlock(&pvpanic_lock);
86 
87 	dev_set_drvdata(dev, pi);
88 
89 	return devm_add_action_or_reset(dev, pvpanic_remove, pi);
90 }
91 EXPORT_SYMBOL_GPL(devm_pvpanic_probe);
92 
93 static int pvpanic_init(void)
94 {
95 	INIT_LIST_HEAD(&pvpanic_list);
96 	spin_lock_init(&pvpanic_lock);
97 
98 	atomic_notifier_chain_register(&panic_notifier_list, &pvpanic_panic_nb);
99 
100 	return 0;
101 }
102 module_init(pvpanic_init);
103 
104 static void pvpanic_exit(void)
105 {
106 	atomic_notifier_chain_unregister(&panic_notifier_list, &pvpanic_panic_nb);
107 
108 }
109 module_exit(pvpanic_exit);
110