blob: cc76fb42aca3c93d8bb1b428beab2be43a912acf [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>
20#include <linux/proc_fs.h>
21#include <linux/delay.h>
22#include <linux/list.h>
23#include <linux/io.h>
24#include <linux/kthread.h>
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070025#include <linux/time.h>
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -080026#include <linux/wakelock.h>
27#include <linux/suspend.h>
Stephen Boyd0ebf7212012-04-30 20:42:35 -070028#include <linux/mutex.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
30#include <asm/current.h>
31
32#include <mach/peripheral-loader.h>
33#include <mach/scm.h>
34#include <mach/socinfo.h>
35#include <mach/subsystem_notif.h>
36#include <mach/subsystem_restart.h>
37
38#include "smd_private.h"
39
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040struct subsys_soc_restart_order {
41 const char * const *subsystem_list;
42 int count;
43
44 struct mutex shutdown_lock;
45 struct mutex powerup_lock;
Stephen Boyd0ebf7212012-04-30 20:42:35 -070046 struct subsys_device *subsys_ptrs[];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047};
48
Vikram Mulukutla69177e12012-03-21 20:51:43 -070049struct restart_wq_data {
Stephen Boyd0ebf7212012-04-30 20:42:35 -070050 struct subsys_device *dev;
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -080051 struct wake_lock ssr_wake_lock;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -070052 char wlname[64];
53 int use_restart_order;
Vikram Mulukutla69177e12012-03-21 20:51:43 -070054 struct work_struct work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055};
56
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070057struct restart_log {
58 struct timeval time;
Stephen Boyd0ebf7212012-04-30 20:42:35 -070059 struct subsys_device *dev;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070060 struct list_head list;
61};
62
Stephen Boyd0ebf7212012-04-30 20:42:35 -070063struct subsys_device {
64 struct subsys_desc *desc;
65 struct list_head list;
66
67 void *notify;
68
69 struct mutex shutdown_lock;
70 struct mutex powerup_lock;
71
72 void *restart_order;
73};
74
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075static int restart_level;
76static int enable_ramdumps;
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
138module_param(enable_ramdumps, int, S_IRUGO | S_IWUSR);
139
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) {
161
162 case RESET_SOC:
163 case RESET_SUBSYS_COUPLED:
164 case RESET_SUBSYS_INDEPENDENT:
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700165 pr_info("Phase %d behavior activated.\n", restart_level);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 break;
167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 default:
169 restart_level = old_val;
170 return -EINVAL;
171 break;
172
173 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174 return 0;
175}
176
177module_param_call(restart_level, restart_level_set, param_get_int,
178 &restart_level, 0644);
179
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700180static struct subsys_soc_restart_order *
181update_restart_order(struct subsys_device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182{
183 int i, j;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700184 struct subsys_soc_restart_order *order;
185 const char *name = dev->desc->name;
186 int len = SUBSYS_NAME_MAX_LENGTH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
188 mutex_lock(&soc_order_reg_lock);
189 for (j = 0; j < n_restart_orders; j++) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700190 order = restart_orders[j];
191 for (i = 0; i < order->count; i++) {
192 if (!strncmp(order->subsystem_list[i], name, len)) {
193 order->subsys_ptrs[i] = dev;
194 goto found;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 }
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700196 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 }
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700198 order = NULL;
199found:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 mutex_unlock(&soc_order_reg_lock);
201
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700202 return order;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203}
204
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700205static void _send_notification_to_order(struct subsys_device **restart_list,
206 int count, enum subsys_notif_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207{
208 int i;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700209 struct subsys_device **dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700211 for (i = 0, dev = restart_list; i < count; i++, dev++)
212 if (*dev)
213 subsys_notif_queue_notification((*dev)->notify, type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214}
215
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700216static int max_restarts;
217module_param(max_restarts, int, 0644);
218
219static long max_history_time = 3600;
220module_param(max_history_time, long, 0644);
221
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700222static void do_epoch_check(struct subsys_device *dev)
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700223{
224 int n = 0;
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600225 struct timeval *time_first = NULL, *curr_time;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700226 struct restart_log *r_log, *temp;
227 static int max_restarts_check;
228 static long max_history_time_check;
229
230 mutex_lock(&restart_log_mutex);
231
232 max_restarts_check = max_restarts;
233 max_history_time_check = max_history_time;
234
235 /* Check if epoch checking is enabled */
236 if (!max_restarts_check)
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700237 goto out;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700238
239 r_log = kmalloc(sizeof(struct restart_log), GFP_KERNEL);
Matt Wagantalleecca342011-11-10 20:12:05 -0800240 if (!r_log)
241 goto out;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700242 r_log->dev = dev;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700243 do_gettimeofday(&r_log->time);
244 curr_time = &r_log->time;
245 INIT_LIST_HEAD(&r_log->list);
246
247 list_add_tail(&r_log->list, &restart_log_list);
248
249 list_for_each_entry_safe(r_log, temp, &restart_log_list, list) {
250
251 if ((curr_time->tv_sec - r_log->time.tv_sec) >
252 max_history_time_check) {
253
254 pr_debug("Deleted node with restart_time = %ld\n",
255 r_log->time.tv_sec);
256 list_del(&r_log->list);
257 kfree(r_log);
258 continue;
259 }
260 if (!n) {
261 time_first = &r_log->time;
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700262 pr_debug("Time_first: %ld\n", time_first->tv_sec);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700263 }
264 n++;
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700265 pr_debug("Restart_time: %ld\n", r_log->time.tv_sec);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700266 }
267
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600268 if (time_first && n >= max_restarts_check) {
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700269 if ((curr_time->tv_sec - time_first->tv_sec) <
270 max_history_time_check)
271 panic("Subsystems have crashed %d times in less than "
272 "%ld seconds!", max_restarts_check,
273 max_history_time_check);
274 }
275
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700276out:
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700277 mutex_unlock(&restart_log_mutex);
278}
279
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700280static void subsystem_restart_wq_func(struct work_struct *work)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281{
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700282 struct restart_wq_data *r_work = container_of(work,
283 struct restart_wq_data, work);
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700284 struct subsys_device **restart_list;
285 struct subsys_device *dev = r_work->dev;
286 struct subsys_desc *desc = dev->desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 struct subsys_soc_restart_order *soc_restart_order = NULL;
288
289 struct mutex *powerup_lock;
290 struct mutex *shutdown_lock;
291
292 int i;
293 int restart_list_count = 0;
294
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700295 if (r_work->use_restart_order)
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700296 soc_restart_order = dev->restart_order;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297
298 /* It's OK to not take the registration lock at this point.
299 * This is because the subsystem list inside the relevant
300 * restart order is not being traversed.
301 */
302 if (!soc_restart_order) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700303 restart_list = &dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 restart_list_count = 1;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700305 powerup_lock = &dev->powerup_lock;
306 shutdown_lock = &dev->shutdown_lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 } else {
308 restart_list = soc_restart_order->subsys_ptrs;
309 restart_list_count = soc_restart_order->count;
310 powerup_lock = &soc_restart_order->powerup_lock;
311 shutdown_lock = &soc_restart_order->shutdown_lock;
312 }
313
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700314 pr_debug("[%p]: Attempting to get shutdown lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315
316 /* Try to acquire shutdown_lock. If this fails, these subsystems are
317 * already being restarted - return.
318 */
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -0800319 if (!mutex_trylock(shutdown_lock))
320 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700322 pr_debug("[%p]: Attempting to get powerup lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
324 /* Now that we've acquired the shutdown lock, either we're the first to
325 * restart these subsystems or some other thread is doing the powerup
326 * sequence for these subsystems. In the latter case, panic and bail
327 * out, since a subsystem died in its powerup sequence.
328 */
329 if (!mutex_trylock(powerup_lock))
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700330 panic("%s[%p]: Subsystem died during powerup!",
331 __func__, current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700333 do_epoch_check(dev);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 /* Now it is necessary to take the registration lock. This is because
336 * the subsystem list in the SoC restart order will be traversed
337 * and it shouldn't be changed until _this_ restart sequence completes.
338 */
339 mutex_lock(&soc_order_reg_lock);
340
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700341 pr_debug("[%p]: Starting restart sequence for %s\n", current,
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700342 desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343
344 _send_notification_to_order(restart_list,
345 restart_list_count,
346 SUBSYS_BEFORE_SHUTDOWN);
347
348 for (i = 0; i < restart_list_count; i++) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700349 const struct subsys_device *dev = restart_list[i];
350 const char *name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700352 if (!dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 continue;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700354 name = dev->desc->name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700356 pr_info("[%p]: Shutting down %s\n", current, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700358 if (dev->desc->shutdown(dev->desc) < 0)
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700359 panic("subsys-restart: %s[%p]: Failed to shutdown %s!",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700360 __func__, current, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 }
362
363 _send_notification_to_order(restart_list, restart_list_count,
364 SUBSYS_AFTER_SHUTDOWN);
365
366 /* Now that we've finished shutting down these subsystems, release the
367 * shutdown lock. If a subsystem restart request comes in for a
368 * subsystem in _this_ restart order after the unlock below, and
369 * before the powerup lock is released, panic and bail out.
370 */
371 mutex_unlock(shutdown_lock);
372
373 /* Collect ram dumps for all subsystems in order here */
374 for (i = 0; i < restart_list_count; i++) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700375 const struct subsys_device *dev = restart_list[i];
376 const char *name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700378 if (!dev)
379 continue;
380 name = dev->desc->name;
381
382 if (dev->desc->ramdump)
383 if (dev->desc->ramdump(enable_ramdumps, dev->desc) < 0)
384 pr_warn("%s[%p]: Ramdump failed.\n", name,
385 current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 }
387
388 _send_notification_to_order(restart_list,
389 restart_list_count,
390 SUBSYS_BEFORE_POWERUP);
391
392 for (i = restart_list_count - 1; i >= 0; i--) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700393 const struct subsys_device *dev = restart_list[i];
394 const char *name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700396 if (!dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 continue;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700398 name = dev->desc->name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700400 pr_info("[%p]: Powering up %s\n", current, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700402 if (dev->desc->powerup(dev->desc) < 0)
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700403 panic("%s[%p]: Failed to powerup %s!", __func__,
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700404 current, name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405 }
406
407 _send_notification_to_order(restart_list,
408 restart_list_count,
409 SUBSYS_AFTER_POWERUP);
410
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700411 pr_info("[%p]: Restart sequence for %s completed.\n",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700412 current, desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413
414 mutex_unlock(powerup_lock);
415
416 mutex_unlock(&soc_order_reg_lock);
417
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700418 pr_debug("[%p]: Released powerup lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -0800420out:
421 wake_unlock(&r_work->ssr_wake_lock);
422 wake_lock_destroy(&r_work->ssr_wake_lock);
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700423 kfree(r_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424}
425
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700426static void __subsystem_restart_dev(struct subsys_device *dev)
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700427{
428 struct restart_wq_data *data = NULL;
429 int rc;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700430 struct subsys_desc *desc = dev->desc;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700431
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700432 pr_debug("Restarting %s [level=%d]!\n", desc->name, restart_level);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700433
434 data = kzalloc(sizeof(struct restart_wq_data), GFP_ATOMIC);
435 if (!data)
436 panic("%s: Unable to allocate memory to restart %s.",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700437 __func__, desc->name);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700438
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700439 data->dev = dev;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700440
441 if (restart_level != RESET_SUBSYS_INDEPENDENT)
442 data->use_restart_order = 1;
443
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700444 snprintf(data->wlname, sizeof(data->wlname), "ssr(%s)", desc->name);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700445 wake_lock_init(&data->ssr_wake_lock, WAKE_LOCK_SUSPEND, data->wlname);
446 wake_lock(&data->ssr_wake_lock);
447
448 INIT_WORK(&data->work, subsystem_restart_wq_func);
449 rc = queue_work(ssr_wq, &data->work);
450 if (rc < 0)
451 panic("%s: Unable to schedule work to restart %s (%d).",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700452 __func__, desc->name, rc);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700453}
454
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700455int subsystem_restart_dev(struct subsys_device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700457 const char *name = dev->desc->name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
Vikram Mulukutla3b24c8c2011-12-02 15:12:53 -0800459 pr_info("Restart sequence requested for %s, restart_level = %d.\n",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700460 name, restart_level);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462 switch (restart_level) {
463
464 case RESET_SUBSYS_COUPLED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 case RESET_SUBSYS_INDEPENDENT:
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700466 __subsystem_restart_dev(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 break;
468
469 case RESET_SOC:
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700470 panic("subsys-restart: Resetting the SoC - %s crashed.", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 break;
472
473 default:
474 panic("subsys-restart: Unknown restart level!\n");
475 break;
476
477 }
478
479 return 0;
480}
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700481EXPORT_SYMBOL(subsystem_restart_dev);
482
483int subsystem_restart(const char *name)
484{
485 struct subsys_device *dev;
486
487 mutex_lock(&subsystem_list_lock);
488 list_for_each_entry(dev, &subsystem_list, list)
489 if (!strncmp(dev->desc->name, name, SUBSYS_NAME_MAX_LENGTH))
490 goto found;
491 dev = NULL;
492found:
493 mutex_unlock(&subsystem_list_lock);
494 if (dev)
495 return subsystem_restart_dev(dev);
496 return -ENODEV;
497}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498EXPORT_SYMBOL(subsystem_restart);
499
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700500struct subsys_device *subsys_register(struct subsys_desc *desc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700502 struct subsys_device *dev;
Vikram Mulukutla705e6892012-06-08 17:57:31 -0700503
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700504 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
505 if (!dev)
506 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700508 dev->desc = desc;
509 dev->notify = subsys_notif_add_subsys(desc->name);
510 dev->restart_order = update_restart_order(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700512 mutex_init(&dev->shutdown_lock);
513 mutex_init(&dev->powerup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700515 mutex_lock(&subsystem_list_lock);
516 list_add(&dev->list, &subsystem_list);
517 mutex_unlock(&subsystem_list_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700519 return dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520}
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700521EXPORT_SYMBOL(subsys_register);
522
523void subsys_unregister(struct subsys_device *dev)
524{
525 if (IS_ERR_OR_NULL(dev))
526 return;
527 mutex_lock(&subsystem_list_lock);
528 list_del(&dev->list);
529 mutex_unlock(&subsystem_list_lock);
530 kfree(dev);
531}
532EXPORT_SYMBOL(subsys_unregister);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700534static int ssr_panic_handler(struct notifier_block *this,
535 unsigned long event, void *ptr)
536{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700537 struct subsys_device *dev;
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700538
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700539 list_for_each_entry(dev, &subsystem_list, list)
540 if (dev->desc->crash_shutdown)
541 dev->desc->crash_shutdown(dev->desc);
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700542 return NOTIFY_DONE;
543}
544
545static struct notifier_block panic_nb = {
546 .notifier_call = ssr_panic_handler,
547};
548
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549static int __init ssr_init_soc_restart_orders(void)
550{
551 int i;
552
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700553 atomic_notifier_chain_register(&panic_notifier_list,
554 &panic_nb);
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 if (cpu_is_msm8x60()) {
557 for (i = 0; i < ARRAY_SIZE(orders_8x60_all); i++) {
558 mutex_init(&orders_8x60_all[i]->powerup_lock);
559 mutex_init(&orders_8x60_all[i]->shutdown_lock);
560 }
561
562 for (i = 0; i < ARRAY_SIZE(orders_8x60_modems); i++) {
563 mutex_init(&orders_8x60_modems[i]->powerup_lock);
564 mutex_init(&orders_8x60_modems[i]->shutdown_lock);
565 }
566
567 restart_orders = orders_8x60_all;
568 n_restart_orders = ARRAY_SIZE(orders_8x60_all);
569 }
570
Stepan Moskovchenko0df9bb22012-07-06 18:19:15 -0700571 if (cpu_is_msm8960() || cpu_is_msm8930() || cpu_is_msm8930aa() ||
Stepan Moskovchenko9c749262012-07-09 19:30:44 -0700572 cpu_is_msm9615() || cpu_is_apq8064() || cpu_is_msm8627() ||
573 cpu_is_msm8960ab()) {
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700574 if (socinfo_get_platform_subtype() == PLATFORM_SUBTYPE_SGLTE) {
575 restart_orders = restart_orders_8960_sglte;
576 n_restart_orders =
577 ARRAY_SIZE(restart_orders_8960_sglte);
578 } else {
579 restart_orders = restart_orders_8960;
580 n_restart_orders = ARRAY_SIZE(restart_orders_8960);
581 }
582 for (i = 0; i < n_restart_orders; i++) {
583 mutex_init(&restart_orders[i]->powerup_lock);
584 mutex_init(&restart_orders[i]->shutdown_lock);
585 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 }
587
588 if (restart_orders == NULL || n_restart_orders < 1) {
589 WARN_ON(1);
590 return -EINVAL;
591 }
592
593 return 0;
594}
595
596static int __init subsys_restart_init(void)
597{
598 int ret = 0;
599
600 restart_level = RESET_SOC;
601
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700602 ssr_wq = alloc_workqueue("ssr_wq", 0, 0);
603
604 if (!ssr_wq)
605 panic("Couldn't allocate workqueue for subsystem restart.\n");
606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 ret = ssr_init_soc_restart_orders();
608
609 return ret;
610}
611
612arch_initcall(subsys_restart_init);
613
614MODULE_DESCRIPTION("Subsystem Restart Driver");
615MODULE_LICENSE("GPL v2");