blob: 563313b90f3d9f5a17eb14b727d5bf80bee6d3ef [file] [log] [blame]
Rohit Vaswanid0fb4182012-03-19 18:07:59 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -070013#define pr_fmt(fmt) "subsys-restart: %s(): " fmt, __func__
14
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/uaccess.h>
18#include <linux/module.h>
19#include <linux/fs.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020#include <linux/delay.h>
21#include <linux/list.h>
22#include <linux/io.h>
23#include <linux/kthread.h>
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070024#include <linux/time.h>
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -080025#include <linux/wakelock.h>
26#include <linux/suspend.h>
Stephen Boyd0ebf7212012-04-30 20:42:35 -070027#include <linux/mutex.h>
Stephen Boydc68ee642012-05-01 17:02:45 -070028#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
30#include <asm/current.h>
31
32#include <mach/peripheral-loader.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <mach/socinfo.h>
34#include <mach/subsystem_notif.h>
35#include <mach/subsystem_restart.h>
36
37#include "smd_private.h"
38
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039struct subsys_soc_restart_order {
40 const char * const *subsystem_list;
41 int count;
42
43 struct mutex shutdown_lock;
44 struct mutex powerup_lock;
Stephen Boyd0ebf7212012-04-30 20:42:35 -070045 struct subsys_device *subsys_ptrs[];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046};
47
Vikram Mulukutla69177e12012-03-21 20:51:43 -070048struct restart_wq_data {
Stephen Boyd0ebf7212012-04-30 20:42:35 -070049 struct subsys_device *dev;
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -080050 struct wake_lock ssr_wake_lock;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -070051 char wlname[64];
52 int use_restart_order;
Vikram Mulukutla69177e12012-03-21 20:51:43 -070053 struct work_struct work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054};
55
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070056struct restart_log {
57 struct timeval time;
Stephen Boyd0ebf7212012-04-30 20:42:35 -070058 struct subsys_device *dev;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070059 struct list_head list;
60};
61
Stephen Boyd0ebf7212012-04-30 20:42:35 -070062struct subsys_device {
63 struct subsys_desc *desc;
64 struct list_head list;
65
66 void *notify;
67
68 struct mutex shutdown_lock;
69 struct mutex powerup_lock;
70
71 void *restart_order;
72};
73
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static int enable_ramdumps;
Stephen Boydc68ee642012-05-01 17:02:45 -070075module_param(enable_ramdumps, int, S_IRUGO | S_IWUSR);
76
Vikram Mulukutla69177e12012-03-21 20:51:43 -070077struct workqueue_struct *ssr_wq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070079static LIST_HEAD(restart_log_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080static LIST_HEAD(subsystem_list);
Stephen Boyd0ebf7212012-04-30 20:42:35 -070081static DEFINE_MUTEX(subsystem_list_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082static DEFINE_MUTEX(soc_order_reg_lock);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070083static DEFINE_MUTEX(restart_log_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084
85/* SOC specific restart orders go here */
86
87#define DEFINE_SINGLE_RESTART_ORDER(name, order) \
88 static struct subsys_soc_restart_order __##name = { \
89 .subsystem_list = order, \
90 .count = ARRAY_SIZE(order), \
91 .subsys_ptrs = {[ARRAY_SIZE(order)] = NULL} \
92 }; \
93 static struct subsys_soc_restart_order *name[] = { \
94 &__##name, \
95 }
96
97/* MSM 8x60 restart ordering info */
98static const char * const _order_8x60_all[] = {
99 "external_modem", "modem", "lpass"
100};
101DEFINE_SINGLE_RESTART_ORDER(orders_8x60_all, _order_8x60_all);
102
103static const char * const _order_8x60_modems[] = {"external_modem", "modem"};
104DEFINE_SINGLE_RESTART_ORDER(orders_8x60_modems, _order_8x60_modems);
105
106/* MSM 8960 restart ordering info */
107static const char * const order_8960[] = {"modem", "lpass"};
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700108/*SGLTE restart ordering info*/
109static const char * const order_8960_sglte[] = {"external_modem",
110 "modem"};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111
112static struct subsys_soc_restart_order restart_orders_8960_one = {
113 .subsystem_list = order_8960,
114 .count = ARRAY_SIZE(order_8960),
115 .subsys_ptrs = {[ARRAY_SIZE(order_8960)] = NULL}
116 };
117
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700118static struct subsys_soc_restart_order restart_orders_8960_fusion_sglte = {
119 .subsystem_list = order_8960_sglte,
120 .count = ARRAY_SIZE(order_8960_sglte),
121 .subsys_ptrs = {[ARRAY_SIZE(order_8960_sglte)] = NULL}
122 };
123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124static struct subsys_soc_restart_order *restart_orders_8960[] = {
125 &restart_orders_8960_one,
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700126 };
127
128static struct subsys_soc_restart_order *restart_orders_8960_sglte[] = {
129 &restart_orders_8960_fusion_sglte,
130 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131
132/* These will be assigned to one of the sets above after
133 * runtime SoC identification.
134 */
135static struct subsys_soc_restart_order **restart_orders;
136static int n_restart_orders;
137
Stephen Boydc68ee642012-05-01 17:02:45 -0700138static int restart_level = RESET_SOC;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140int get_restart_level()
141{
142 return restart_level;
143}
144EXPORT_SYMBOL(get_restart_level);
145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146static int restart_level_set(const char *val, struct kernel_param *kp)
147{
148 int ret;
149 int old_val = restart_level;
150
Rohit Vaswani56dd22a2011-11-11 16:21:28 -0800151 if (cpu_is_msm9615()) {
152 pr_err("Only Phase 1 subsystem restart is supported\n");
153 return -EINVAL;
154 }
155
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 ret = param_set_int(val, kp);
157 if (ret)
158 return ret;
159
160 switch (restart_level) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 case RESET_SOC:
162 case RESET_SUBSYS_COUPLED:
163 case RESET_SUBSYS_INDEPENDENT:
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700164 pr_info("Phase %d behavior activated.\n", restart_level);
Stephen Boydc68ee642012-05-01 17:02:45 -0700165 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 default:
167 restart_level = old_val;
168 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170 return 0;
171}
172
173module_param_call(restart_level, restart_level_set, param_get_int,
174 &restart_level, 0644);
175
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700176static struct subsys_soc_restart_order *
177update_restart_order(struct subsys_device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178{
179 int i, j;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700180 struct subsys_soc_restart_order *order;
181 const char *name = dev->desc->name;
182 int len = SUBSYS_NAME_MAX_LENGTH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183
184 mutex_lock(&soc_order_reg_lock);
185 for (j = 0; j < n_restart_orders; j++) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700186 order = restart_orders[j];
187 for (i = 0; i < order->count; i++) {
188 if (!strncmp(order->subsystem_list[i], name, len)) {
189 order->subsys_ptrs[i] = dev;
190 goto found;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 }
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700192 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 }
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700194 order = NULL;
195found:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 mutex_unlock(&soc_order_reg_lock);
197
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700198 return order;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199}
200
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700201static int max_restarts;
202module_param(max_restarts, int, 0644);
203
204static long max_history_time = 3600;
205module_param(max_history_time, long, 0644);
206
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700207static void do_epoch_check(struct subsys_device *dev)
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700208{
209 int n = 0;
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600210 struct timeval *time_first = NULL, *curr_time;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700211 struct restart_log *r_log, *temp;
212 static int max_restarts_check;
213 static long max_history_time_check;
214
215 mutex_lock(&restart_log_mutex);
216
217 max_restarts_check = max_restarts;
218 max_history_time_check = max_history_time;
219
220 /* Check if epoch checking is enabled */
221 if (!max_restarts_check)
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700222 goto out;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700223
224 r_log = kmalloc(sizeof(struct restart_log), GFP_KERNEL);
Matt Wagantalleecca342011-11-10 20:12:05 -0800225 if (!r_log)
226 goto out;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700227 r_log->dev = dev;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700228 do_gettimeofday(&r_log->time);
229 curr_time = &r_log->time;
230 INIT_LIST_HEAD(&r_log->list);
231
232 list_add_tail(&r_log->list, &restart_log_list);
233
234 list_for_each_entry_safe(r_log, temp, &restart_log_list, list) {
235
236 if ((curr_time->tv_sec - r_log->time.tv_sec) >
237 max_history_time_check) {
238
239 pr_debug("Deleted node with restart_time = %ld\n",
240 r_log->time.tv_sec);
241 list_del(&r_log->list);
242 kfree(r_log);
243 continue;
244 }
245 if (!n) {
246 time_first = &r_log->time;
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700247 pr_debug("Time_first: %ld\n", time_first->tv_sec);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700248 }
249 n++;
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700250 pr_debug("Restart_time: %ld\n", r_log->time.tv_sec);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700251 }
252
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600253 if (time_first && n >= max_restarts_check) {
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700254 if ((curr_time->tv_sec - time_first->tv_sec) <
255 max_history_time_check)
256 panic("Subsystems have crashed %d times in less than "
257 "%ld seconds!", max_restarts_check,
258 max_history_time_check);
259 }
260
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700261out:
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700262 mutex_unlock(&restart_log_mutex);
263}
264
Stephen Boyd0daae152012-05-01 17:36:15 -0700265static void for_each_subsys_device(struct subsys_device **list, unsigned count,
266 void *data, void (*fn)(struct subsys_device *, void *))
267{
268 while (count--) {
269 struct subsys_device *dev = *list++;
270 if (!dev)
271 continue;
272 fn(dev, data);
273 }
274}
275
276static void __send_notification_to_order(struct subsys_device *dev, void *data)
277{
278 enum subsys_notif_type type = (enum subsys_notif_type)data;
279
280 subsys_notif_queue_notification(dev->notify, type);
281}
282
283static void send_notification_to_order(struct subsys_device **l, unsigned n,
284 enum subsys_notif_type t)
285{
286 for_each_subsys_device(l, n, (void *)t, __send_notification_to_order);
287}
288
289static void subsystem_shutdown(struct subsys_device *dev, void *data)
290{
291 const char *name = dev->desc->name;
292
293 pr_info("[%p]: Shutting down %s\n", current, name);
294 if (dev->desc->shutdown(dev->desc) < 0)
295 panic("subsys-restart: [%p]: Failed to shutdown %s!",
296 current, name);
297}
298
299static void subsystem_ramdump(struct subsys_device *dev, void *data)
300{
301 const char *name = dev->desc->name;
302
303 if (dev->desc->ramdump)
304 if (dev->desc->ramdump(enable_ramdumps, dev->desc) < 0)
305 pr_warn("%s[%p]: Ramdump failed.\n", name, current);
306}
307
308static void subsystem_powerup(struct subsys_device *dev, void *data)
309{
310 const char *name = dev->desc->name;
311
312 pr_info("[%p]: Powering up %s\n", current, name);
313 if (dev->desc->powerup(dev->desc) < 0)
314 panic("[%p]: Failed to powerup %s!", current, name);
315}
316
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700317static void subsystem_restart_wq_func(struct work_struct *work)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318{
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700319 struct restart_wq_data *r_work = container_of(work,
320 struct restart_wq_data, work);
Stephen Boyd0daae152012-05-01 17:36:15 -0700321 struct subsys_device **list;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700322 struct subsys_device *dev = r_work->dev;
323 struct subsys_desc *desc = dev->desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 struct subsys_soc_restart_order *soc_restart_order = NULL;
325
326 struct mutex *powerup_lock;
327 struct mutex *shutdown_lock;
328
Stephen Boyd0daae152012-05-01 17:36:15 -0700329 unsigned count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700331 if (r_work->use_restart_order)
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700332 soc_restart_order = dev->restart_order;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333
Stephen Boydc68ee642012-05-01 17:02:45 -0700334 /*
335 * It's OK to not take the registration lock at this point.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 * This is because the subsystem list inside the relevant
337 * restart order is not being traversed.
338 */
339 if (!soc_restart_order) {
Stephen Boyd0daae152012-05-01 17:36:15 -0700340 list = &dev;
341 count = 1;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700342 powerup_lock = &dev->powerup_lock;
343 shutdown_lock = &dev->shutdown_lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 } else {
Stephen Boyd0daae152012-05-01 17:36:15 -0700345 list = soc_restart_order->subsys_ptrs;
346 count = soc_restart_order->count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 powerup_lock = &soc_restart_order->powerup_lock;
348 shutdown_lock = &soc_restart_order->shutdown_lock;
349 }
350
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700351 pr_debug("[%p]: Attempting to get shutdown lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352
Stephen Boydc68ee642012-05-01 17:02:45 -0700353 /*
354 * Try to acquire shutdown_lock. If this fails, these subsystems are
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355 * already being restarted - return.
356 */
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -0800357 if (!mutex_trylock(shutdown_lock))
358 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700360 pr_debug("[%p]: Attempting to get powerup lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361
Stephen Boydc68ee642012-05-01 17:02:45 -0700362 /*
363 * Now that we've acquired the shutdown lock, either we're the first to
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 * restart these subsystems or some other thread is doing the powerup
365 * sequence for these subsystems. In the latter case, panic and bail
366 * out, since a subsystem died in its powerup sequence.
367 */
368 if (!mutex_trylock(powerup_lock))
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700369 panic("%s[%p]: Subsystem died during powerup!",
370 __func__, current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700372 do_epoch_check(dev);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700373
Stephen Boydc68ee642012-05-01 17:02:45 -0700374 /*
375 * It's necessary to take the registration lock because the subsystem
376 * list in the SoC restart order will be traversed and it shouldn't be
377 * changed until _this_ restart sequence completes.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 */
379 mutex_lock(&soc_order_reg_lock);
380
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700381 pr_debug("[%p]: Starting restart sequence for %s\n", current,
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700382 desc->name);
Stephen Boyd0daae152012-05-01 17:36:15 -0700383 send_notification_to_order(list, count, SUBSYS_BEFORE_SHUTDOWN);
384 for_each_subsys_device(list, count, NULL, subsystem_shutdown);
385 send_notification_to_order(list, count, SUBSYS_AFTER_SHUTDOWN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386
Stephen Boydc68ee642012-05-01 17:02:45 -0700387 /*
388 * Now that we've finished shutting down these subsystems, release the
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 * shutdown lock. If a subsystem restart request comes in for a
390 * subsystem in _this_ restart order after the unlock below, and
391 * before the powerup lock is released, panic and bail out.
392 */
393 mutex_unlock(shutdown_lock);
394
395 /* Collect ram dumps for all subsystems in order here */
Stephen Boyd0daae152012-05-01 17:36:15 -0700396 for_each_subsys_device(list, count, NULL, subsystem_ramdump);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397
Stephen Boyd0daae152012-05-01 17:36:15 -0700398 send_notification_to_order(list, count, SUBSYS_BEFORE_POWERUP);
399 for_each_subsys_device(list, count, NULL, subsystem_powerup);
400 send_notification_to_order(list, count, SUBSYS_AFTER_POWERUP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700402 pr_info("[%p]: Restart sequence for %s completed.\n",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700403 current, desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404
405 mutex_unlock(powerup_lock);
406
407 mutex_unlock(&soc_order_reg_lock);
408
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700409 pr_debug("[%p]: Released powerup lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -0800411out:
412 wake_unlock(&r_work->ssr_wake_lock);
413 wake_lock_destroy(&r_work->ssr_wake_lock);
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700414 kfree(r_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415}
416
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700417static void __subsystem_restart_dev(struct subsys_device *dev)
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700418{
419 struct restart_wq_data *data = NULL;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700420 struct subsys_desc *desc = dev->desc;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700421
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700422 pr_debug("Restarting %s [level=%d]!\n", desc->name, restart_level);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700423
424 data = kzalloc(sizeof(struct restart_wq_data), GFP_ATOMIC);
425 if (!data)
426 panic("%s: Unable to allocate memory to restart %s.",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700427 __func__, desc->name);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700428
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700429 data->dev = dev;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700430
431 if (restart_level != RESET_SUBSYS_INDEPENDENT)
432 data->use_restart_order = 1;
433
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700434 snprintf(data->wlname, sizeof(data->wlname), "ssr(%s)", desc->name);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700435 wake_lock_init(&data->ssr_wake_lock, WAKE_LOCK_SUSPEND, data->wlname);
436 wake_lock(&data->ssr_wake_lock);
437
438 INIT_WORK(&data->work, subsystem_restart_wq_func);
Stephen Boydc68ee642012-05-01 17:02:45 -0700439 queue_work(ssr_wq, &data->work);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700440}
441
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700442int subsystem_restart_dev(struct subsys_device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700444 const char *name = dev->desc->name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445
Vikram Mulukutla3b24c8c2011-12-02 15:12:53 -0800446 pr_info("Restart sequence requested for %s, restart_level = %d.\n",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700447 name, restart_level);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 switch (restart_level) {
450
451 case RESET_SUBSYS_COUPLED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 case RESET_SUBSYS_INDEPENDENT:
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700453 __subsystem_restart_dev(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 case RESET_SOC:
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700456 panic("subsys-restart: Resetting the SoC - %s crashed.", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 default:
459 panic("subsys-restart: Unknown restart level!\n");
Stephen Boydc68ee642012-05-01 17:02:45 -0700460 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 }
462
463 return 0;
464}
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700465EXPORT_SYMBOL(subsystem_restart_dev);
466
467int subsystem_restart(const char *name)
468{
469 struct subsys_device *dev;
470
471 mutex_lock(&subsystem_list_lock);
472 list_for_each_entry(dev, &subsystem_list, list)
473 if (!strncmp(dev->desc->name, name, SUBSYS_NAME_MAX_LENGTH))
474 goto found;
475 dev = NULL;
476found:
477 mutex_unlock(&subsystem_list_lock);
478 if (dev)
479 return subsystem_restart_dev(dev);
480 return -ENODEV;
481}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482EXPORT_SYMBOL(subsystem_restart);
483
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700484struct subsys_device *subsys_register(struct subsys_desc *desc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700486 struct subsys_device *dev;
Vikram Mulukutla705e6892012-06-08 17:57:31 -0700487
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700488 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
489 if (!dev)
490 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700492 dev->desc = desc;
493 dev->notify = subsys_notif_add_subsys(desc->name);
494 dev->restart_order = update_restart_order(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700496 mutex_init(&dev->shutdown_lock);
497 mutex_init(&dev->powerup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700499 mutex_lock(&subsystem_list_lock);
500 list_add(&dev->list, &subsystem_list);
501 mutex_unlock(&subsystem_list_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700503 return dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504}
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700505EXPORT_SYMBOL(subsys_register);
506
507void subsys_unregister(struct subsys_device *dev)
508{
509 if (IS_ERR_OR_NULL(dev))
510 return;
511 mutex_lock(&subsystem_list_lock);
512 list_del(&dev->list);
513 mutex_unlock(&subsystem_list_lock);
514 kfree(dev);
515}
516EXPORT_SYMBOL(subsys_unregister);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700518static int ssr_panic_handler(struct notifier_block *this,
519 unsigned long event, void *ptr)
520{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700521 struct subsys_device *dev;
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700522
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700523 list_for_each_entry(dev, &subsystem_list, list)
524 if (dev->desc->crash_shutdown)
525 dev->desc->crash_shutdown(dev->desc);
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700526 return NOTIFY_DONE;
527}
528
529static struct notifier_block panic_nb = {
530 .notifier_call = ssr_panic_handler,
531};
532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533static int __init ssr_init_soc_restart_orders(void)
534{
535 int i;
536
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700537 atomic_notifier_chain_register(&panic_notifier_list,
538 &panic_nb);
539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 if (cpu_is_msm8x60()) {
541 for (i = 0; i < ARRAY_SIZE(orders_8x60_all); i++) {
542 mutex_init(&orders_8x60_all[i]->powerup_lock);
543 mutex_init(&orders_8x60_all[i]->shutdown_lock);
544 }
545
546 for (i = 0; i < ARRAY_SIZE(orders_8x60_modems); i++) {
547 mutex_init(&orders_8x60_modems[i]->powerup_lock);
548 mutex_init(&orders_8x60_modems[i]->shutdown_lock);
549 }
550
551 restart_orders = orders_8x60_all;
552 n_restart_orders = ARRAY_SIZE(orders_8x60_all);
553 }
554
Stepan Moskovchenko0df9bb22012-07-06 18:19:15 -0700555 if (cpu_is_msm8960() || cpu_is_msm8930() || cpu_is_msm8930aa() ||
Stepan Moskovchenko9c749262012-07-09 19:30:44 -0700556 cpu_is_msm9615() || cpu_is_apq8064() || cpu_is_msm8627() ||
557 cpu_is_msm8960ab()) {
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700558 if (socinfo_get_platform_subtype() == PLATFORM_SUBTYPE_SGLTE) {
559 restart_orders = restart_orders_8960_sglte;
560 n_restart_orders =
561 ARRAY_SIZE(restart_orders_8960_sglte);
562 } else {
563 restart_orders = restart_orders_8960;
564 n_restart_orders = ARRAY_SIZE(restart_orders_8960);
565 }
566 for (i = 0; i < n_restart_orders; i++) {
567 mutex_init(&restart_orders[i]->powerup_lock);
568 mutex_init(&restart_orders[i]->shutdown_lock);
569 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 }
571
572 if (restart_orders == NULL || n_restart_orders < 1) {
573 WARN_ON(1);
574 return -EINVAL;
575 }
576
577 return 0;
578}
579
580static int __init subsys_restart_init(void)
581{
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700582 ssr_wq = alloc_workqueue("ssr_wq", 0, 0);
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700583 if (!ssr_wq)
584 panic("Couldn't allocate workqueue for subsystem restart.\n");
585
Stephen Boydc68ee642012-05-01 17:02:45 -0700586 return ssr_init_soc_restart_orders();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588arch_initcall(subsys_restart_init);
589
590MODULE_DESCRIPTION("Subsystem Restart Driver");
591MODULE_LICENSE("GPL v2");