blob: 4d6f4b1e3b414d393bdfd0d3a4b8b222e412cd30 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/proc_fs.h>
19#include <linux/delay.h>
20#include <linux/list.h>
21#include <linux/io.h>
22#include <linux/kthread.h>
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070023#include <linux/time.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024
25#include <asm/current.h>
26
27#include <mach/peripheral-loader.h>
28#include <mach/scm.h>
29#include <mach/socinfo.h>
30#include <mach/subsystem_notif.h>
31#include <mach/subsystem_restart.h>
32
33#include "smd_private.h"
34
35#if defined(SUBSYS_RESTART_DEBUG)
36#define dprintk(msg...) printk(msg)
37#else
38#define dprintk(msg...)
39#endif
40
41struct subsys_soc_restart_order {
42 const char * const *subsystem_list;
43 int count;
44
45 struct mutex shutdown_lock;
46 struct mutex powerup_lock;
47 struct subsys_data *subsys_ptrs[];
48};
49
50struct restart_thread_data {
51 struct subsys_data *subsys;
52 int coupled;
53};
54
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070055struct restart_log {
56 struct timeval time;
57 struct subsys_data *subsys;
58 struct list_head list;
59};
60
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061static int restart_level;
62static int enable_ramdumps;
63
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070064static LIST_HEAD(restart_log_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065static LIST_HEAD(subsystem_list);
66static DEFINE_MUTEX(subsystem_list_lock);
67static DEFINE_MUTEX(soc_order_reg_lock);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070068static DEFINE_MUTEX(restart_log_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069
70/* SOC specific restart orders go here */
71
72#define DEFINE_SINGLE_RESTART_ORDER(name, order) \
73 static struct subsys_soc_restart_order __##name = { \
74 .subsystem_list = order, \
75 .count = ARRAY_SIZE(order), \
76 .subsys_ptrs = {[ARRAY_SIZE(order)] = NULL} \
77 }; \
78 static struct subsys_soc_restart_order *name[] = { \
79 &__##name, \
80 }
81
82/* MSM 8x60 restart ordering info */
83static const char * const _order_8x60_all[] = {
84 "external_modem", "modem", "lpass"
85};
86DEFINE_SINGLE_RESTART_ORDER(orders_8x60_all, _order_8x60_all);
87
88static const char * const _order_8x60_modems[] = {"external_modem", "modem"};
89DEFINE_SINGLE_RESTART_ORDER(orders_8x60_modems, _order_8x60_modems);
90
91/* MSM 8960 restart ordering info */
92static const char * const order_8960[] = {"modem", "lpass"};
93
94static struct subsys_soc_restart_order restart_orders_8960_one = {
95 .subsystem_list = order_8960,
96 .count = ARRAY_SIZE(order_8960),
97 .subsys_ptrs = {[ARRAY_SIZE(order_8960)] = NULL}
98 };
99
100static struct subsys_soc_restart_order *restart_orders_8960[] = {
101 &restart_orders_8960_one,
102};
103
104/* These will be assigned to one of the sets above after
105 * runtime SoC identification.
106 */
107static struct subsys_soc_restart_order **restart_orders;
108static int n_restart_orders;
109
110module_param(enable_ramdumps, int, S_IRUGO | S_IWUSR);
111
112static struct subsys_soc_restart_order *_update_restart_order(
113 struct subsys_data *subsys);
114
115int get_restart_level()
116{
117 return restart_level;
118}
119EXPORT_SYMBOL(get_restart_level);
120
121static void restart_level_changed(void)
122{
123 struct subsys_data *subsys;
124
125 if (cpu_is_msm8x60() && restart_level == RESET_SUBSYS_COUPLED) {
126 restart_orders = orders_8x60_all;
127 n_restart_orders = ARRAY_SIZE(orders_8x60_all);
128 }
129
130 if (cpu_is_msm8x60() && restart_level == RESET_SUBSYS_MIXED) {
131 restart_orders = orders_8x60_modems;
132 n_restart_orders = ARRAY_SIZE(orders_8x60_modems);
133 }
134
135 mutex_lock(&subsystem_list_lock);
136 list_for_each_entry(subsys, &subsystem_list, list)
137 subsys->restart_order = _update_restart_order(subsys);
138 mutex_unlock(&subsystem_list_lock);
139}
140
141static int restart_level_set(const char *val, struct kernel_param *kp)
142{
143 int ret;
144 int old_val = restart_level;
145
146 ret = param_set_int(val, kp);
147 if (ret)
148 return ret;
149
150 switch (restart_level) {
151
152 case RESET_SOC:
153 case RESET_SUBSYS_COUPLED:
154 case RESET_SUBSYS_INDEPENDENT:
155 pr_info("Subsystem Restart: Phase %d behavior activated.\n",
156 restart_level);
157 break;
158
159 case RESET_SUBSYS_MIXED:
160 pr_info("Subsystem Restart: Phase 2+ behavior activated.\n");
161 break;
162
163 default:
164 restart_level = old_val;
165 return -EINVAL;
166 break;
167
168 }
169
170 if (restart_level != old_val)
171 restart_level_changed();
172
173 return 0;
174}
175
176module_param_call(restart_level, restart_level_set, param_get_int,
177 &restart_level, 0644);
178
179static struct subsys_data *_find_subsystem(const char *subsys_name)
180{
181 struct subsys_data *subsys;
182
183 mutex_lock(&subsystem_list_lock);
184 list_for_each_entry(subsys, &subsystem_list, list)
185 if (!strncmp(subsys->name, subsys_name,
186 SUBSYS_NAME_MAX_LENGTH)) {
187 mutex_unlock(&subsystem_list_lock);
188 return subsys;
189 }
190 mutex_unlock(&subsystem_list_lock);
191
192 return NULL;
193}
194
195static struct subsys_soc_restart_order *_update_restart_order(
196 struct subsys_data *subsys)
197{
198 int i, j;
199
200 if (!subsys)
201 return NULL;
202
203 if (!subsys->name)
204 return NULL;
205
206 mutex_lock(&soc_order_reg_lock);
207 for (j = 0; j < n_restart_orders; j++) {
208 for (i = 0; i < restart_orders[j]->count; i++)
209 if (!strncmp(restart_orders[j]->subsystem_list[i],
210 subsys->name, SUBSYS_NAME_MAX_LENGTH)) {
211
212 restart_orders[j]->subsys_ptrs[i] =
213 subsys;
214 mutex_unlock(&soc_order_reg_lock);
215 return restart_orders[j];
216 }
217 }
218
219 mutex_unlock(&soc_order_reg_lock);
220
221 return NULL;
222}
223
224static void _send_notification_to_order(struct subsys_data
225 **restart_list, int count,
226 enum subsys_notif_type notif_type)
227{
228 int i;
229
230 for (i = 0; i < count; i++)
231 if (restart_list[i])
232 subsys_notif_queue_notification(
233 restart_list[i]->notif_handle, notif_type);
234}
235
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700236static int max_restarts;
237module_param(max_restarts, int, 0644);
238
239static long max_history_time = 3600;
240module_param(max_history_time, long, 0644);
241
242static void do_epoch_check(struct subsys_data *subsys)
243{
244 int n = 0;
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600245 struct timeval *time_first = NULL, *curr_time;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700246 struct restart_log *r_log, *temp;
247 static int max_restarts_check;
248 static long max_history_time_check;
249
250 mutex_lock(&restart_log_mutex);
251
252 max_restarts_check = max_restarts;
253 max_history_time_check = max_history_time;
254
255 /* Check if epoch checking is enabled */
256 if (!max_restarts_check)
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700257 goto out;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700258
259 r_log = kmalloc(sizeof(struct restart_log), GFP_KERNEL);
260 r_log->subsys = subsys;
261 do_gettimeofday(&r_log->time);
262 curr_time = &r_log->time;
263 INIT_LIST_HEAD(&r_log->list);
264
265 list_add_tail(&r_log->list, &restart_log_list);
266
267 list_for_each_entry_safe(r_log, temp, &restart_log_list, list) {
268
269 if ((curr_time->tv_sec - r_log->time.tv_sec) >
270 max_history_time_check) {
271
272 pr_debug("Deleted node with restart_time = %ld\n",
273 r_log->time.tv_sec);
274 list_del(&r_log->list);
275 kfree(r_log);
276 continue;
277 }
278 if (!n) {
279 time_first = &r_log->time;
280 pr_debug("time_first: %ld", time_first->tv_sec);
281 }
282 n++;
283 pr_debug("restart_time: %ld\n", r_log->time.tv_sec);
284 }
285
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600286 if (time_first && n >= max_restarts_check) {
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700287 if ((curr_time->tv_sec - time_first->tv_sec) <
288 max_history_time_check)
289 panic("Subsystems have crashed %d times in less than "
290 "%ld seconds!", max_restarts_check,
291 max_history_time_check);
292 }
293
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700294out:
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700295 mutex_unlock(&restart_log_mutex);
296}
297
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298static int subsystem_restart_thread(void *data)
299{
300 struct restart_thread_data *r_work = data;
301 struct subsys_data **restart_list;
302 struct subsys_data *subsys = r_work->subsys;
303 struct subsys_soc_restart_order *soc_restart_order = NULL;
304
305 struct mutex *powerup_lock;
306 struct mutex *shutdown_lock;
307
308 int i;
309 int restart_list_count = 0;
310
311 if (r_work->coupled)
312 soc_restart_order = subsys->restart_order;
313
314 /* It's OK to not take the registration lock at this point.
315 * This is because the subsystem list inside the relevant
316 * restart order is not being traversed.
317 */
318 if (!soc_restart_order) {
319 restart_list = subsys->single_restart_list;
320 restart_list_count = 1;
321 powerup_lock = &subsys->powerup_lock;
322 shutdown_lock = &subsys->shutdown_lock;
323 } else {
324 restart_list = soc_restart_order->subsys_ptrs;
325 restart_list_count = soc_restart_order->count;
326 powerup_lock = &soc_restart_order->powerup_lock;
327 shutdown_lock = &soc_restart_order->shutdown_lock;
328 }
329
330 dprintk("%s[%p]: Attempting to get shutdown lock!\n", __func__,
331 current);
332
333 /* Try to acquire shutdown_lock. If this fails, these subsystems are
334 * already being restarted - return.
335 */
336 if (!mutex_trylock(shutdown_lock)) {
337 kfree(data);
338 do_exit(0);
339 }
340
341 dprintk("%s[%p]: Attempting to get powerup lock!\n", __func__,
342 current);
343
344 /* Now that we've acquired the shutdown lock, either we're the first to
345 * restart these subsystems or some other thread is doing the powerup
346 * sequence for these subsystems. In the latter case, panic and bail
347 * out, since a subsystem died in its powerup sequence.
348 */
349 if (!mutex_trylock(powerup_lock))
350 panic("%s: Subsystem died during powerup!", __func__);
351
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700352 do_epoch_check(subsys);
353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 /* Now it is necessary to take the registration lock. This is because
355 * the subsystem list in the SoC restart order will be traversed
356 * and it shouldn't be changed until _this_ restart sequence completes.
357 */
358 mutex_lock(&soc_order_reg_lock);
359
360 dprintk("%s: Starting restart sequence for %s\n", __func__,
361 r_work->subsys->name);
362
363 _send_notification_to_order(restart_list,
364 restart_list_count,
365 SUBSYS_BEFORE_SHUTDOWN);
366
367 for (i = 0; i < restart_list_count; i++) {
368
369 if (!restart_list[i])
370 continue;
371
372 pr_info("subsys-restart: Shutting down %s\n",
373 restart_list[i]->name);
374
375 if (restart_list[i]->shutdown(subsys) < 0)
376 panic("%s: Failed to shutdown %s!\n", __func__,
377 restart_list[i]->name);
378 }
379
380 _send_notification_to_order(restart_list, restart_list_count,
381 SUBSYS_AFTER_SHUTDOWN);
382
383 /* Now that we've finished shutting down these subsystems, release the
384 * shutdown lock. If a subsystem restart request comes in for a
385 * subsystem in _this_ restart order after the unlock below, and
386 * before the powerup lock is released, panic and bail out.
387 */
388 mutex_unlock(shutdown_lock);
389
390 /* Collect ram dumps for all subsystems in order here */
391 for (i = 0; i < restart_list_count; i++) {
392 if (!restart_list[i])
393 continue;
394
395 if (restart_list[i]->ramdump)
396 if (restart_list[i]->ramdump(enable_ramdumps,
397 subsys) < 0)
398 pr_warn("%s(%s): Ramdump failed.", __func__,
399 restart_list[i]->name);
400 }
401
402 _send_notification_to_order(restart_list,
403 restart_list_count,
404 SUBSYS_BEFORE_POWERUP);
405
406 for (i = restart_list_count - 1; i >= 0; i--) {
407
408 if (!restart_list[i])
409 continue;
410
411 pr_info("subsys-restart: Powering up %s\n",
412 restart_list[i]->name);
413
414 if (restart_list[i]->powerup(subsys) < 0)
415 panic("%s: Failed to powerup %s!", __func__,
416 restart_list[i]->name);
417 }
418
419 _send_notification_to_order(restart_list,
420 restart_list_count,
421 SUBSYS_AFTER_POWERUP);
422
423 pr_info("%s: Restart sequence for %s completed.", __func__,
424 r_work->subsys->name);
425
426 mutex_unlock(powerup_lock);
427
428 mutex_unlock(&soc_order_reg_lock);
429
430 dprintk("%s: Released powerup lock!\n", __func__);
431
432 kfree(data);
433 do_exit(0);
434}
435
436int subsystem_restart(const char *subsys_name)
437{
438 struct subsys_data *subsys;
439 struct task_struct *tsk;
440 struct restart_thread_data *data = NULL;
441
442 if (!subsys_name) {
443 pr_err("%s: Invalid subsystem name.", __func__);
444 return -EINVAL;
445 }
446
447 pr_info("Subsystem Restart: Restart sequence requested for %s\n",
448 subsys_name);
449
450 /* List of subsystems is protected by a lock. New subsystems can
451 * still come in.
452 */
453 subsys = _find_subsystem(subsys_name);
454
455 if (!subsys) {
456 pr_warn("%s: Unregistered subsystem %s!", __func__,
457 subsys_name);
458 return -EINVAL;
459 }
460
461 if (restart_level != RESET_SOC) {
462 data = kzalloc(sizeof(struct restart_thread_data), GFP_KERNEL);
463 if (!data) {
464 restart_level = RESET_SOC;
465 pr_warn("%s: Failed to alloc restart data. Resetting.",
466 __func__);
467 } else {
468 if (restart_level == RESET_SUBSYS_COUPLED ||
469 restart_level == RESET_SUBSYS_MIXED)
470 data->coupled = 1;
471 else
472 data->coupled = 0;
473
474 data->subsys = subsys;
475 }
476 }
477
478 switch (restart_level) {
479
480 case RESET_SUBSYS_COUPLED:
481 case RESET_SUBSYS_MIXED:
482 case RESET_SUBSYS_INDEPENDENT:
483 dprintk("%s: Restarting %s [level=%d]!\n", __func__,
484 subsys_name, restart_level);
485
486 /* Let the kthread handle the actual restarting. Using a
487 * workqueue will not work since all restart requests are
488 * serialized and it prevents the short circuiting of
489 * restart requests for subsystems already in a restart
490 * sequence.
491 */
492 tsk = kthread_run(subsystem_restart_thread, data,
493 "subsystem_subsystem_restart_thread");
494 if (IS_ERR(tsk))
495 panic("%s: Unable to create thread to restart %s",
496 __func__, subsys->name);
497
498 break;
499
500 case RESET_SOC:
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700501 panic("subsys-restart: Resetting the SoC");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 break;
503
504 default:
505 panic("subsys-restart: Unknown restart level!\n");
506 break;
507
508 }
509
510 return 0;
511}
512EXPORT_SYMBOL(subsystem_restart);
513
514int ssr_register_subsystem(struct subsys_data *subsys)
515{
516 if (!subsys)
517 goto err;
518
519 if (!subsys->name)
520 goto err;
521
522 if (!subsys->powerup || !subsys->shutdown)
523 goto err;
524
525 subsys->notif_handle = subsys_notif_add_subsys(subsys->name);
526 subsys->restart_order = _update_restart_order(subsys);
527 subsys->single_restart_list[0] = subsys;
528
529 mutex_init(&subsys->shutdown_lock);
530 mutex_init(&subsys->powerup_lock);
531
532 mutex_lock(&subsystem_list_lock);
533 list_add(&subsys->list, &subsystem_list);
534 mutex_unlock(&subsystem_list_lock);
535
536 return 0;
537
538err:
539 return -EINVAL;
540}
541EXPORT_SYMBOL(ssr_register_subsystem);
542
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700543static int ssr_panic_handler(struct notifier_block *this,
544 unsigned long event, void *ptr)
545{
546 struct subsys_data *subsys;
547
548 list_for_each_entry(subsys, &subsystem_list, list)
549 if (subsys->crash_shutdown)
550 subsys->crash_shutdown(subsys);
551 return NOTIFY_DONE;
552}
553
554static struct notifier_block panic_nb = {
555 .notifier_call = ssr_panic_handler,
556};
557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558static int __init ssr_init_soc_restart_orders(void)
559{
560 int i;
561
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700562 atomic_notifier_chain_register(&panic_notifier_list,
563 &panic_nb);
564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 if (cpu_is_msm8x60()) {
566 for (i = 0; i < ARRAY_SIZE(orders_8x60_all); i++) {
567 mutex_init(&orders_8x60_all[i]->powerup_lock);
568 mutex_init(&orders_8x60_all[i]->shutdown_lock);
569 }
570
571 for (i = 0; i < ARRAY_SIZE(orders_8x60_modems); i++) {
572 mutex_init(&orders_8x60_modems[i]->powerup_lock);
573 mutex_init(&orders_8x60_modems[i]->shutdown_lock);
574 }
575
576 restart_orders = orders_8x60_all;
577 n_restart_orders = ARRAY_SIZE(orders_8x60_all);
578 }
579
580 if (cpu_is_msm8960()) {
581 restart_orders = restart_orders_8960;
582 n_restart_orders = ARRAY_SIZE(restart_orders_8960);
583 }
584
585 if (restart_orders == NULL || n_restart_orders < 1) {
586 WARN_ON(1);
587 return -EINVAL;
588 }
589
590 return 0;
591}
592
593static int __init subsys_restart_init(void)
594{
595 int ret = 0;
596
597 restart_level = RESET_SOC;
598
599 ret = ssr_init_soc_restart_orders();
600
601 return ret;
602}
603
604arch_initcall(subsys_restart_init);
605
606MODULE_DESCRIPTION("Subsystem Restart Driver");
607MODULE_LICENSE("GPL v2");