Merge tag 'memblock-v5.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rppt...
[linux-2.6-microblaze.git] / drivers / misc / pvpanic / pvpanic.c
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,
47                      void *unused)
48 {
49         unsigned int event = PVPANIC_PANICKED;
50
51         if (kexec_crash_loaded())
52                 event = PVPANIC_CRASH_LOADED;
53
54         pvpanic_send_event(event);
55
56         return NOTIFY_DONE;
57 }
58
59 static struct notifier_block pvpanic_panic_nb = {
60         .notifier_call = pvpanic_panic_notify,
61         .priority = 1, /* let this called before broken drm_fb_helper */
62 };
63
64 int pvpanic_probe(struct pvpanic_instance *pi)
65 {
66         if (!pi || !pi->base)
67                 return -EINVAL;
68
69         spin_lock(&pvpanic_lock);
70         list_add(&pi->list, &pvpanic_list);
71         spin_unlock(&pvpanic_lock);
72
73         return 0;
74 }
75 EXPORT_SYMBOL_GPL(pvpanic_probe);
76
77 void pvpanic_remove(struct pvpanic_instance *pi)
78 {
79         struct pvpanic_instance *pi_cur, *pi_next;
80
81         if (!pi)
82                 return;
83
84         spin_lock(&pvpanic_lock);
85         list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
86                 if (pi_cur == pi) {
87                         list_del(&pi_cur->list);
88                         break;
89                 }
90         }
91         spin_unlock(&pvpanic_lock);
92 }
93 EXPORT_SYMBOL_GPL(pvpanic_remove);
94
95 static int pvpanic_init(void)
96 {
97         INIT_LIST_HEAD(&pvpanic_list);
98         spin_lock_init(&pvpanic_lock);
99
100         atomic_notifier_chain_register(&panic_notifier_list,
101                                        &pvpanic_panic_nb);
102
103         return 0;
104 }
105
106 static void pvpanic_exit(void)
107 {
108         atomic_notifier_chain_unregister(&panic_notifier_list,
109                                          &pvpanic_panic_nb);
110
111 }
112
113 module_init(pvpanic_init);
114 module_exit(pvpanic_exit);