xref: /openbmc/linux/drivers/misc/pvpanic/pvpanic.c (revision 867e6d38)
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/types.h>
17 #include <linux/cdev.h>
18 #include <linux/list.h>
19 
20 #include <uapi/misc/pvpanic.h>
21 
22 #include "pvpanic.h"
23 
24 MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
25 MODULE_DESCRIPTION("pvpanic device driver ");
26 MODULE_LICENSE("GPL");
27 
28 static struct list_head pvpanic_list;
29 static spinlock_t pvpanic_lock;
30 
31 static void
32 pvpanic_send_event(unsigned int event)
33 {
34 	struct pvpanic_instance *pi_cur;
35 
36 	spin_lock(&pvpanic_lock);
37 	list_for_each_entry(pi_cur, &pvpanic_list, list) {
38 		if (event & pi_cur->capability & pi_cur->events)
39 			iowrite8(event, pi_cur->base);
40 	}
41 	spin_unlock(&pvpanic_lock);
42 }
43 
44 static int
45 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
46 		     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 int pvpanic_probe(struct pvpanic_instance *pi)
64 {
65 	if (!pi || !pi->base)
66 		return -EINVAL;
67 
68 	spin_lock(&pvpanic_lock);
69 	list_add(&pi->list, &pvpanic_list);
70 	spin_unlock(&pvpanic_lock);
71 
72 	return 0;
73 }
74 EXPORT_SYMBOL_GPL(pvpanic_probe);
75 
76 void pvpanic_remove(struct pvpanic_instance *pi)
77 {
78 	struct pvpanic_instance *pi_cur, *pi_next;
79 
80 	if (!pi)
81 		return;
82 
83 	spin_lock(&pvpanic_lock);
84 	list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
85 		if (pi_cur == pi) {
86 			list_del(&pi_cur->list);
87 			break;
88 		}
89 	}
90 	spin_unlock(&pvpanic_lock);
91 }
92 EXPORT_SYMBOL_GPL(pvpanic_remove);
93 
94 static int pvpanic_init(void)
95 {
96 	INIT_LIST_HEAD(&pvpanic_list);
97 	spin_lock_init(&pvpanic_lock);
98 
99 	atomic_notifier_chain_register(&panic_notifier_list,
100 				       &pvpanic_panic_nb);
101 
102 	return 0;
103 }
104 
105 static void pvpanic_exit(void)
106 {
107 	atomic_notifier_chain_unregister(&panic_notifier_list,
108 					 &pvpanic_panic_nb);
109 
110 }
111 
112 module_init(pvpanic_init);
113 module_exit(pvpanic_exit);
114