1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2011, 2013, 2016-2019, The Linux Foundation. All rights reserved.
*/
/*
* Subsystem Notifier -- Provides notifications
* of subsys events.
*
* Use subsys_notif_register_notifier to register for notifications
* and subsys_notif_queue_notification to send notifications.
*
*/
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <linux/stringify.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <soc/qcom/subsystem_notif.h>
/**
* The callbacks that are registered in this data structure as early
* notification callbacks will be called as soon as the SSR framework is
* informed that the subsystem has crashed. This means that these functions will
* be invoked as part of an IRQ handler, and thus, will be called in an atomic
* context. Therefore, functions that are registered as early notification
* callback must obey to the same constraints as interrupt handlers
* (i.e. these functions must not sleep or block, etc).
*/
struct subsys_early_notif_info {
spinlock_t cb_lock;
void (*early_notif_cb[NUM_EARLY_NOTIFS])(void *);
void *data[NUM_EARLY_NOTIFS];
};
struct subsys_notif_info {
char name[50];
struct srcu_notifier_head subsys_notif_rcvr_list;
struct subsys_early_notif_info early_notif_info;
struct list_head list;
};
static LIST_HEAD(subsystem_list);
static DEFINE_MUTEX(notif_lock);
static DEFINE_MUTEX(notif_add_lock);
#if defined(SUBSYS_RESTART_DEBUG)
static void subsys_notif_reg_test_notifier(const char *);
#endif
static struct subsys_notif_info *_notif_find_subsys(const char *subsys_name)
{
struct subsys_notif_info *subsys;
mutex_lock(¬if_lock);
list_for_each_entry(subsys, &subsystem_list, list)
if (!strcmp(subsys->name, subsys_name)) {
mutex_unlock(¬if_lock);
return subsys;
}
mutex_unlock(¬if_lock);
return NULL;
}
void *subsys_notif_register_notifier(
const char *subsys_name, struct notifier_block *nb)
{
int ret;
struct subsys_notif_info *subsys = _notif_find_subsys(subsys_name);
if (!subsys) {
/* Possible first time reference to this subsystem. Add it. */
subsys = (struct subsys_notif_info *)
subsys_notif_add_subsys(subsys_name);
if (!subsys)
return ERR_PTR(-EINVAL);
}
ret = srcu_notifier_chain_register(
&subsys->subsys_notif_rcvr_list, nb);
if (ret < 0)
return ERR_PTR(ret);
return subsys;
}
EXPORT_SYMBOL(subsys_notif_register_notifier);
int subsys_notif_unregister_notifier(void *subsys_handle,
struct notifier_block *nb)
{
int ret;
struct subsys_notif_info *subsys =
(struct subsys_notif_info *)subsys_handle;
if (!subsys)
return -EINVAL;
ret = srcu_notifier_chain_unregister(
&subsys->subsys_notif_rcvr_list, nb);
return ret;
}
EXPORT_SYMBOL(subsys_notif_unregister_notifier);
void subsys_send_early_notifications(void *early_notif_handle)
{
struct subsys_early_notif_info *early_info = early_notif_handle;
unsigned long flags;
unsigned int i;
void (*notif_cb)(void *data);
if (!early_notif_handle)
return;
spin_lock_irqsave(&early_info->cb_lock, flags);
for (i = 0; i < NUM_EARLY_NOTIFS; i++) {
notif_cb = early_info->early_notif_cb[i];
if (notif_cb)
notif_cb(early_info->data[i]);
}
spin_unlock_irqrestore(&early_info->cb_lock, flags);
}
EXPORT_SYMBOL(subsys_send_early_notifications);
static bool valid_early_notif(enum early_subsys_notif_type notif_type)
{
return notif_type >= 0 && notif_type < NUM_EARLY_NOTIFS;
}
/**
* The early_notif_cb parameter must point to a function that conforms to the
* same constraints placed upon interrupt handlers, as the function will be
* called in an atomic context (i.e. these functions must not sleep or block).
*/
int subsys_register_early_notifier(const char *subsys_name,
enum early_subsys_notif_type notif_type,
void (*early_notif_cb)(void *), void *data)
{
struct subsys_notif_info *subsys;
struct subsys_early_notif_info *early_notif_info;
unsigned long flags;
int rc = 0;
if (!subsys_name || !early_notif_cb || !valid_early_notif(notif_type))
return -EINVAL;
subsys = _notif_find_subsys(subsys_name);
if (!subsys)
return -EINVAL;
early_notif_info = &subsys->early_notif_info;
spin_lock_irqsave(&early_notif_info->cb_lock, flags);
if (early_notif_info->early_notif_cb[notif_type]) {
rc = -EEXIST;
goto out;
}
early_notif_info->early_notif_cb[notif_type] = early_notif_cb;
early_notif_info->data[notif_type] = data;
out:
spin_unlock_irqrestore(&early_notif_info->cb_lock, flags);
return rc;
}
EXPORT_SYMBOL(subsys_register_early_notifier);
int subsys_unregister_early_notifier(const char *subsys_name, enum
early_subsys_notif_type notif_type)
{
struct subsys_notif_info *subsys;
struct subsys_early_notif_info *early_notif_info;
unsigned long flags;
if (!subsys_name || !valid_early_notif(notif_type))
return -EINVAL;
subsys = _notif_find_subsys(subsys_name);
if (!subsys)
return -EINVAL;
early_notif_info = &subsys->early_notif_info;
spin_lock_irqsave(&early_notif_info->cb_lock, flags);
early_notif_info->early_notif_cb[notif_type] = NULL;
early_notif_info->data[notif_type] = NULL;
spin_unlock_irqrestore(&early_notif_info->cb_lock, flags);
return 0;
}
EXPORT_SYMBOL(subsys_unregister_early_notifier);
void *subsys_get_early_notif_info(const char *subsys_name)
{
struct subsys_notif_info *subsys;
if (!subsys_name)
return ERR_PTR(-EINVAL);
subsys = _notif_find_subsys(subsys_name);
if (!subsys)
return ERR_PTR(-EINVAL);
return &subsys->early_notif_info;
}
EXPORT_SYMBOL(subsys_get_early_notif_info);
void *subsys_notif_add_subsys(const char *subsys_name)
{
struct subsys_notif_info *subsys = NULL;
if (!subsys_name)
goto done;
mutex_lock(¬if_add_lock);
subsys = _notif_find_subsys(subsys_name);
if (subsys) {
mutex_unlock(¬if_add_lock);
goto done;
}
subsys = kmalloc(sizeof(struct subsys_notif_info), GFP_KERNEL);
if (!subsys) {
mutex_unlock(¬if_add_lock);
return ERR_PTR(-EINVAL);
}
strlcpy(subsys->name, subsys_name, ARRAY_SIZE(subsys->name));
srcu_init_notifier_head(&subsys->subsys_notif_rcvr_list);
memset(&subsys->early_notif_info, 0, sizeof(struct
subsys_early_notif_info));
spin_lock_init(&subsys->early_notif_info.cb_lock);
INIT_LIST_HEAD(&subsys->list);
mutex_lock(¬if_lock);
list_add_tail(&subsys->list, &subsystem_list);
mutex_unlock(¬if_lock);
#if defined(SUBSYS_RESTART_DEBUG)
subsys_notif_reg_test_notifier(subsys->name);
#endif
mutex_unlock(¬if_add_lock);
done:
return subsys;
}
EXPORT_SYMBOL(subsys_notif_add_subsys);
int subsys_notif_queue_notification(void *subsys_handle,
enum subsys_notif_type notif_type,
void *data)
{
struct subsys_notif_info *subsys = subsys_handle;
if (!subsys)
return -EINVAL;
if (notif_type < 0 || notif_type >= SUBSYS_NOTIF_TYPE_COUNT)
return -EINVAL;
return srcu_notifier_call_chain(&subsys->subsys_notif_rcvr_list,
notif_type, data);
}
EXPORT_SYMBOL(subsys_notif_queue_notification);
#if defined(SUBSYS_RESTART_DEBUG)
static const char *notif_to_string(enum subsys_notif_type notif_type)
{
switch (notif_type) {
case SUBSYS_BEFORE_SHUTDOWN:
return __stringify(SUBSYS_BEFORE_SHUTDOWN);
case SUBSYS_AFTER_SHUTDOWN:
return __stringify(SUBSYS_AFTER_SHUTDOWN);
case SUBSYS_BEFORE_POWERUP:
return __stringify(SUBSYS_BEFORE_POWERUP);
case SUBSYS_AFTER_POWERUP:
return __stringify(SUBSYS_AFTER_POWERUP);
default:
return "unknown";
}
}
static int subsys_notifier_test_call(struct notifier_block *this,
unsigned long code,
void *data)
{
switch (code) {
default:
pr_warn("%s: Notification %s from subsystem %pK\n",
__func__, notif_to_string(code), data);
break;
}
return NOTIFY_DONE;
}
static struct notifier_block nb = {
.notifier_call = subsys_notifier_test_call,
};
static void subsys_notif_reg_test_notifier(const char *subsys_name)
{
void *handle = subsys_notif_register_notifier(subsys_name, &nb);
pr_warn("%s: Registered test notifier, handle=%pK\n",
__func__, handle);
}
#endif
MODULE_DESCRIPTION("Subsystem Restart Notifier");
MODULE_LICENSE("GPL v2");
|