blob: 747276c15b639ff7a7e295f3f14eb30aaeb4236b [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2011-2012, The Linux Foundation. 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>
Stephen Boyd497ef402012-06-21 12:54:40 -070029#include <linux/spinlock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include <asm/current.h>
32
33#include <mach/peripheral-loader.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#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 Mulukutla96aecfc2011-07-28 18:18:42 -070049struct restart_log {
50 struct timeval time;
Stephen Boyd0ebf7212012-04-30 20:42:35 -070051 struct subsys_device *dev;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070052 struct list_head list;
53};
54
Stephen Boyd8f57c0f2012-09-18 11:31:02 -070055enum subsys_state {
56 SUBSYS_OFFLINE,
57 SUBSYS_ONLINE,
58 SUBSYS_CRASHED,
59};
60
61static const char * const subsys_states[] = {
62 [SUBSYS_OFFLINE] = "OFFLINE",
63 [SUBSYS_ONLINE] = "ONLINE",
64 [SUBSYS_CRASHED] = "CRASHED",
65};
66
Stephen Boyd0ebf7212012-04-30 20:42:35 -070067struct subsys_device {
68 struct subsys_desc *desc;
69 struct list_head list;
Stephen Boyd497ef402012-06-21 12:54:40 -070070 struct wake_lock wake_lock;
71 char wlname[64];
72 struct work_struct work;
73 spinlock_t restart_lock;
Stephen Boyd8f57c0f2012-09-18 11:31:02 -070074 bool restarting;
75 enum subsys_state state;
Stephen Boyd0ebf7212012-04-30 20:42:35 -070076
77 void *notify;
78
79 struct mutex shutdown_lock;
80 struct mutex powerup_lock;
81
82 void *restart_order;
83};
84
Jaeseong GIMcbea9362012-10-10 15:42:17 +090085static int enable_ramdumps;
Stephen Boydc68ee642012-05-01 17:02:45 -070086module_param(enable_ramdumps, int, S_IRUGO | S_IWUSR);
87
Vikram Mulukutla69177e12012-03-21 20:51:43 -070088struct workqueue_struct *ssr_wq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070090static LIST_HEAD(restart_log_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091static LIST_HEAD(subsystem_list);
Stephen Boyd0ebf7212012-04-30 20:42:35 -070092static DEFINE_MUTEX(subsystem_list_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093static DEFINE_MUTEX(soc_order_reg_lock);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -070094static DEFINE_MUTEX(restart_log_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095
96/* SOC specific restart orders go here */
97
98#define DEFINE_SINGLE_RESTART_ORDER(name, order) \
99 static struct subsys_soc_restart_order __##name = { \
100 .subsystem_list = order, \
101 .count = ARRAY_SIZE(order), \
102 .subsys_ptrs = {[ARRAY_SIZE(order)] = NULL} \
103 }; \
104 static struct subsys_soc_restart_order *name[] = { \
105 &__##name, \
106 }
107
108/* MSM 8x60 restart ordering info */
109static const char * const _order_8x60_all[] = {
110 "external_modem", "modem", "lpass"
111};
112DEFINE_SINGLE_RESTART_ORDER(orders_8x60_all, _order_8x60_all);
113
114static const char * const _order_8x60_modems[] = {"external_modem", "modem"};
115DEFINE_SINGLE_RESTART_ORDER(orders_8x60_modems, _order_8x60_modems);
116
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700117/*SGLTE restart ordering info*/
118static const char * const order_8960_sglte[] = {"external_modem",
119 "modem"};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700121static struct subsys_soc_restart_order restart_orders_8960_fusion_sglte = {
122 .subsystem_list = order_8960_sglte,
123 .count = ARRAY_SIZE(order_8960_sglte),
124 .subsys_ptrs = {[ARRAY_SIZE(order_8960_sglte)] = NULL}
125 };
126
Ameya Thakur2c0d2f22012-06-15 15:48:20 -0700127static struct subsys_soc_restart_order *restart_orders_8960_sglte[] = {
128 &restart_orders_8960_fusion_sglte,
129 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
Joel Kinge4f98902013-03-25 13:12:27 -0700131/* SGLTE2 restart ordering info*/
132static const char * const order_8064_sglte2[] = {"external_modem",
133 "external_modem_mdm"};
134
135static struct subsys_soc_restart_order restart_orders_8064_fusion_sglte2 = {
136 .subsystem_list = order_8064_sglte2,
137 .count = ARRAY_SIZE(order_8064_sglte2),
138 .subsys_ptrs = {[ARRAY_SIZE(order_8064_sglte2)] = NULL}
139 };
140
141static struct subsys_soc_restart_order *restart_orders_8064_sglte2[] = {
142 &restart_orders_8064_fusion_sglte2,
143 };
144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145/* These will be assigned to one of the sets above after
146 * runtime SoC identification.
147 */
148static struct subsys_soc_restart_order **restart_orders;
149static int n_restart_orders;
150
Brian Muramatsu0f0df2f2012-08-01 23:30:40 -0700151static int restart_level = RESET_SUBSYS_INDEPENDENT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152
153int get_restart_level()
154{
155 return restart_level;
156}
157EXPORT_SYMBOL(get_restart_level);
158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159static int restart_level_set(const char *val, struct kernel_param *kp)
160{
161 int ret;
162 int old_val = restart_level;
Joel King4e204292013-04-16 12:48:51 -0700163 int subtype;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164
Rohit Vaswani56dd22a2011-11-11 16:21:28 -0800165 if (cpu_is_msm9615()) {
166 pr_err("Only Phase 1 subsystem restart is supported\n");
167 return -EINVAL;
168 }
169
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170 ret = param_set_int(val, kp);
171 if (ret)
172 return ret;
173
174 switch (restart_level) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175 case RESET_SUBSYS_INDEPENDENT:
Joel King4e204292013-04-16 12:48:51 -0700176 subtype = socinfo_get_platform_subtype();
177 if ((subtype == PLATFORM_SUBTYPE_SGLTE) ||
178 (subtype == PLATFORM_SUBTYPE_SGLTE2)) {
Vikram Mulukutlaf68456d2012-07-27 15:14:10 -0700179 pr_info("Phase 3 is currently unsupported. Using phase 2 instead.\n");
180 restart_level = RESET_SUBSYS_COUPLED;
181 }
182 case RESET_SUBSYS_COUPLED:
183 case RESET_SOC:
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700184 pr_info("Phase %d behavior activated.\n", restart_level);
Stephen Boydc68ee642012-05-01 17:02:45 -0700185 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 default:
187 restart_level = old_val;
188 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 return 0;
191}
192
193module_param_call(restart_level, restart_level_set, param_get_int,
194 &restart_level, 0644);
195
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700196static void subsys_set_state(struct subsys_device *subsys,
197 enum subsys_state state)
198{
199 unsigned long flags;
200
201 spin_lock_irqsave(&subsys->restart_lock, flags);
202 if (subsys->state != state) {
203 subsys->state = state;
204 spin_unlock_irqrestore(&subsys->restart_lock, flags);
205 return;
206 }
207 spin_unlock_irqrestore(&subsys->restart_lock, flags);
208}
209
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700210static struct subsys_soc_restart_order *
211update_restart_order(struct subsys_device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212{
213 int i, j;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700214 struct subsys_soc_restart_order *order;
215 const char *name = dev->desc->name;
216 int len = SUBSYS_NAME_MAX_LENGTH;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217
218 mutex_lock(&soc_order_reg_lock);
219 for (j = 0; j < n_restart_orders; j++) {
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700220 order = restart_orders[j];
221 for (i = 0; i < order->count; i++) {
222 if (!strncmp(order->subsystem_list[i], name, len)) {
223 order->subsys_ptrs[i] = dev;
224 goto found;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 }
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700226 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 }
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700228 order = NULL;
229found:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 mutex_unlock(&soc_order_reg_lock);
231
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700232 return order;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233}
234
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700235static int max_restarts;
236module_param(max_restarts, int, 0644);
237
238static long max_history_time = 3600;
239module_param(max_history_time, long, 0644);
240
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700241static void do_epoch_check(struct subsys_device *dev)
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700242{
243 int n = 0;
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600244 struct timeval *time_first = NULL, *curr_time;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700245 struct restart_log *r_log, *temp;
246 static int max_restarts_check;
247 static long max_history_time_check;
248
249 mutex_lock(&restart_log_mutex);
250
251 max_restarts_check = max_restarts;
252 max_history_time_check = max_history_time;
253
254 /* Check if epoch checking is enabled */
255 if (!max_restarts_check)
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700256 goto out;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700257
258 r_log = kmalloc(sizeof(struct restart_log), GFP_KERNEL);
Matt Wagantalleecca342011-11-10 20:12:05 -0800259 if (!r_log)
260 goto out;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700261 r_log->dev = dev;
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700262 do_gettimeofday(&r_log->time);
263 curr_time = &r_log->time;
264 INIT_LIST_HEAD(&r_log->list);
265
266 list_add_tail(&r_log->list, &restart_log_list);
267
268 list_for_each_entry_safe(r_log, temp, &restart_log_list, list) {
269
270 if ((curr_time->tv_sec - r_log->time.tv_sec) >
271 max_history_time_check) {
272
273 pr_debug("Deleted node with restart_time = %ld\n",
274 r_log->time.tv_sec);
275 list_del(&r_log->list);
276 kfree(r_log);
277 continue;
278 }
279 if (!n) {
280 time_first = &r_log->time;
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700281 pr_debug("Time_first: %ld\n", time_first->tv_sec);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700282 }
283 n++;
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700284 pr_debug("Restart_time: %ld\n", r_log->time.tv_sec);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700285 }
286
Jordan Crouse75a25ca2011-09-09 12:49:57 -0600287 if (time_first && n >= max_restarts_check) {
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700288 if ((curr_time->tv_sec - time_first->tv_sec) <
Jaeseong GIMb8499412012-06-26 10:36:57 -0700289 max_history_time_check) {
Vikram Mulukutla0ad34f62013-01-22 11:01:13 -0800290 panic("Subsystems have crashed %d times in less than "\
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700291 "%ld seconds!", max_restarts_check,
292 max_history_time_check);
Jaeseong GIMb8499412012-06-26 10:36:57 -0700293 }
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700294 }
295
Vikram Mulukutla7a289092011-09-14 10:08:36 -0700296out:
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700297 mutex_unlock(&restart_log_mutex);
298}
299
Stephen Boyd0daae152012-05-01 17:36:15 -0700300static void for_each_subsys_device(struct subsys_device **list, unsigned count,
301 void *data, void (*fn)(struct subsys_device *, void *))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302{
Stephen Boyd0daae152012-05-01 17:36:15 -0700303 while (count--) {
304 struct subsys_device *dev = *list++;
305 if (!dev)
306 continue;
307 fn(dev, data);
308 }
309}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310
Stephen Boyd0daae152012-05-01 17:36:15 -0700311static void __send_notification_to_order(struct subsys_device *dev, void *data)
312{
313 enum subsys_notif_type type = (enum subsys_notif_type)data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314
Stephen Boyd0daae152012-05-01 17:36:15 -0700315 subsys_notif_queue_notification(dev->notify, type);
316}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317
Stephen Boyd0daae152012-05-01 17:36:15 -0700318static void send_notification_to_order(struct subsys_device **l, unsigned n,
319 enum subsys_notif_type t)
320{
321 for_each_subsys_device(l, n, (void *)t, __send_notification_to_order);
322}
323
324static void subsystem_shutdown(struct subsys_device *dev, void *data)
325{
326 const char *name = dev->desc->name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
Stephen Boyd0daae152012-05-01 17:36:15 -0700328 pr_info("[%p]: Shutting down %s\n", current, name);
Brian Muramatsu0f0df2f2012-08-01 23:30:40 -0700329 if (dev->desc->shutdown(dev->desc) < 0) {
Vikram Mulukutla0ad34f62013-01-22 11:01:13 -0800330 panic("subsys-restart: [%p]: Failed to shutdown %s!",
Stephen Boyd0daae152012-05-01 17:36:15 -0700331 current, name);
Brian Muramatsu0f0df2f2012-08-01 23:30:40 -0700332 }
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700333 subsys_set_state(dev, SUBSYS_OFFLINE);
Stephen Boyd0daae152012-05-01 17:36:15 -0700334}
335
336static void subsystem_ramdump(struct subsys_device *dev, void *data)
337{
338 const char *name = dev->desc->name;
339
340 if (dev->desc->ramdump)
341 if (dev->desc->ramdump(enable_ramdumps, dev->desc) < 0)
342 pr_warn("%s[%p]: Ramdump failed.\n", name, current);
343}
344
345static void subsystem_powerup(struct subsys_device *dev, void *data)
346{
347 const char *name = dev->desc->name;
348
349 pr_info("[%p]: Powering up %s\n", current, name);
Brian Muramatsu0f0df2f2012-08-01 23:30:40 -0700350 if (dev->desc->powerup(dev->desc) < 0) {
Vikram Mulukutla0ad34f62013-01-22 11:01:13 -0800351 panic("[%p]: Failed to powerup %s!", current, name);
Brian Muramatsu0f0df2f2012-08-01 23:30:40 -0700352 }
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700353 subsys_set_state(dev, SUBSYS_ONLINE);
Stephen Boyd0daae152012-05-01 17:36:15 -0700354}
355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356static void subsystem_restart_wq_func(struct work_struct *work)
357{
Stephen Boyd497ef402012-06-21 12:54:40 -0700358 struct subsys_device *dev = container_of(work,
359 struct subsys_device, work);
Stephen Boyd0daae152012-05-01 17:36:15 -0700360 struct subsys_device **list;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700361 struct subsys_desc *desc = dev->desc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 struct subsys_soc_restart_order *soc_restart_order = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 struct mutex *powerup_lock;
364 struct mutex *shutdown_lock;
Stephen Boyd0daae152012-05-01 17:36:15 -0700365 unsigned count;
Stephen Boyd497ef402012-06-21 12:54:40 -0700366 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367
Stephen Boyda82ee082012-06-21 12:35:49 -0700368 if (restart_level != RESET_SUBSYS_INDEPENDENT)
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700369 soc_restart_order = dev->restart_order;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370
Stephen Boydc68ee642012-05-01 17:02:45 -0700371 /*
372 * It's OK to not take the registration lock at this point.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 * This is because the subsystem list inside the relevant
374 * restart order is not being traversed.
375 */
376 if (!soc_restart_order) {
Stephen Boyd0daae152012-05-01 17:36:15 -0700377 list = &dev;
378 count = 1;
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700379 powerup_lock = &dev->powerup_lock;
380 shutdown_lock = &dev->shutdown_lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 } else {
Stephen Boyd0daae152012-05-01 17:36:15 -0700382 list = soc_restart_order->subsys_ptrs;
383 count = soc_restart_order->count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 powerup_lock = &soc_restart_order->powerup_lock;
385 shutdown_lock = &soc_restart_order->shutdown_lock;
386 }
387
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700388 pr_debug("[%p]: Attempting to get shutdown lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389
Stephen Boydc68ee642012-05-01 17:02:45 -0700390 /*
391 * Try to acquire shutdown_lock. If this fails, these subsystems are
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392 * already being restarted - return.
393 */
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -0800394 if (!mutex_trylock(shutdown_lock))
395 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700397 pr_debug("[%p]: Attempting to get powerup lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398
Stephen Boydc68ee642012-05-01 17:02:45 -0700399 /*
400 * Now that we've acquired the shutdown lock, either we're the first to
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 * restart these subsystems or some other thread is doing the powerup
402 * sequence for these subsystems. In the latter case, panic and bail
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700403 * out, since a subsystem died in its powerup sequence. This catches
404 * the case where a subsystem in a restart order isn't the one
405 * who initiated the original restart but has crashed while the restart
406 * order is being rebooted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 */
Jaeseong GIMb8499412012-06-26 10:36:57 -0700408 if (!mutex_trylock(powerup_lock)) {
Vikram Mulukutla0ad34f62013-01-22 11:01:13 -0800409 panic("%s[%p]: Subsystem died during powerup!",
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700410 __func__, current);
Jaeseong GIMb8499412012-06-26 10:36:57 -0700411 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700413 do_epoch_check(dev);
Vikram Mulukutla96aecfc2011-07-28 18:18:42 -0700414
Stephen Boydc68ee642012-05-01 17:02:45 -0700415 /*
416 * It's necessary to take the registration lock because the subsystem
417 * list in the SoC restart order will be traversed and it shouldn't be
418 * changed until _this_ restart sequence completes.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 */
420 mutex_lock(&soc_order_reg_lock);
421
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700422 pr_debug("[%p]: Starting restart sequence for %s\n", current,
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700423 desc->name);
Stephen Boyd0daae152012-05-01 17:36:15 -0700424 send_notification_to_order(list, count, SUBSYS_BEFORE_SHUTDOWN);
425 for_each_subsys_device(list, count, NULL, subsystem_shutdown);
426 send_notification_to_order(list, count, SUBSYS_AFTER_SHUTDOWN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427
Stephen Boydc68ee642012-05-01 17:02:45 -0700428 /*
429 * Now that we've finished shutting down these subsystems, release the
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430 * shutdown lock. If a subsystem restart request comes in for a
431 * subsystem in _this_ restart order after the unlock below, and
432 * before the powerup lock is released, panic and bail out.
433 */
434 mutex_unlock(shutdown_lock);
435
436 /* Collect ram dumps for all subsystems in order here */
Stephen Boyd0daae152012-05-01 17:36:15 -0700437 for_each_subsys_device(list, count, NULL, subsystem_ramdump);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438
Stephen Boyd0daae152012-05-01 17:36:15 -0700439 send_notification_to_order(list, count, SUBSYS_BEFORE_POWERUP);
440 for_each_subsys_device(list, count, NULL, subsystem_powerup);
441 send_notification_to_order(list, count, SUBSYS_AFTER_POWERUP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700443 pr_info("[%p]: Restart sequence for %s completed.\n",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700444 current, desc->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445
446 mutex_unlock(powerup_lock);
447
448 mutex_unlock(&soc_order_reg_lock);
449
Vikram Mulukutlaa5dd6112011-09-14 14:12:33 -0700450 pr_debug("[%p]: Released powerup lock!\n", current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451
Vikram Mulukutlaf6349ae2012-02-24 12:21:15 -0800452out:
Stephen Boyd497ef402012-06-21 12:54:40 -0700453 spin_lock_irqsave(&dev->restart_lock, flags);
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700454 dev->restarting = false;
455 wake_unlock(&dev->wake_lock);
Stephen Boyd497ef402012-06-21 12:54:40 -0700456 spin_unlock_irqrestore(&dev->restart_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457}
458
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700459static void __subsystem_restart_dev(struct subsys_device *dev)
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700460{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700461 struct subsys_desc *desc = dev->desc;
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700462 const char *name = dev->desc->name;
Stephen Boyd497ef402012-06-21 12:54:40 -0700463 unsigned long flags;
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700464
Stephen Boyd77c22742012-07-24 18:46:45 -0700465 pr_debug("Restarting %s [level=%d]!\n", desc->name, restart_level);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700466
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700467 /*
468 * We want to allow drivers to call subsystem_restart{_dev}() as many
469 * times as they want up until the point where the subsystem is
470 * shutdown.
471 */
Stephen Boyd77c22742012-07-24 18:46:45 -0700472 spin_lock_irqsave(&dev->restart_lock, flags);
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700473 if (dev->state != SUBSYS_CRASHED) {
474 if (dev->state == SUBSYS_ONLINE && !dev->restarting) {
475 dev->restarting = true;
476 dev->state = SUBSYS_CRASHED;
477 wake_lock(&dev->wake_lock);
478 queue_work(ssr_wq, &dev->work);
479 } else {
480 panic("Subsystem %s crashed during SSR!", name);
481 }
Stephen Boyd77c22742012-07-24 18:46:45 -0700482 }
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700483 spin_unlock_irqrestore(&dev->restart_lock, flags);
Vikram Mulukutla922a4f12012-04-13 14:36:50 -0700484}
485
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700486int subsystem_restart_dev(struct subsys_device *dev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700488 const char *name = dev->desc->name;
Vikram Mulukutla3944eff2012-10-29 18:43:02 -0700489
490 /*
491 * If a system reboot/shutdown is underway, ignore subsystem errors.
492 * However, print a message so that we know that a subsystem behaved
493 * unexpectedly here.
494 */
495 if (system_state == SYSTEM_RESTART
496 || system_state == SYSTEM_POWER_OFF) {
497 pr_err("%s crashed during a system poweroff/shutdown.\n", name);
498 return -EBUSY;
499 }
500
Vikram Mulukutla3b24c8c2011-12-02 15:12:53 -0800501 pr_info("Restart sequence requested for %s, restart_level = %d.\n",
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700502 name, restart_level);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 switch (restart_level) {
505
506 case RESET_SUBSYS_COUPLED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 case RESET_SUBSYS_INDEPENDENT:
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700508 __subsystem_restart_dev(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 case RESET_SOC:
Ameya Thakur97d41142012-12-05 17:04:52 -0800511 panic("subsys-restart: Resetting the SoC - %s crashed.", name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 default:
Ameya Thakur97d41142012-12-05 17:04:52 -0800514 panic("subsys-restart: Unknown restart level!\n");
Stephen Boydc68ee642012-05-01 17:02:45 -0700515 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 }
517
518 return 0;
519}
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700520EXPORT_SYMBOL(subsystem_restart_dev);
521
522int subsystem_restart(const char *name)
523{
524 struct subsys_device *dev;
525
526 mutex_lock(&subsystem_list_lock);
527 list_for_each_entry(dev, &subsystem_list, list)
528 if (!strncmp(dev->desc->name, name, SUBSYS_NAME_MAX_LENGTH))
529 goto found;
530 dev = NULL;
531found:
532 mutex_unlock(&subsystem_list_lock);
533 if (dev)
534 return subsystem_restart_dev(dev);
535 return -ENODEV;
536}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537EXPORT_SYMBOL(subsystem_restart);
538
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700539struct subsys_device *subsys_register(struct subsys_desc *desc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700541 struct subsys_device *dev;
Vikram Mulukutla705e6892012-06-08 17:57:31 -0700542
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700543 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
544 if (!dev)
545 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700547 dev->desc = desc;
548 dev->notify = subsys_notif_add_subsys(desc->name);
549 dev->restart_order = update_restart_order(dev);
Stephen Boyd8f57c0f2012-09-18 11:31:02 -0700550 dev->state = SUBSYS_ONLINE; /* Until proper refcounting appears */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
Stephen Boyd497ef402012-06-21 12:54:40 -0700552 snprintf(dev->wlname, sizeof(dev->wlname), "ssr(%s)", desc->name);
553 wake_lock_init(&dev->wake_lock, WAKE_LOCK_SUSPEND, dev->wlname);
554 INIT_WORK(&dev->work, subsystem_restart_wq_func);
555 spin_lock_init(&dev->restart_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700557 mutex_init(&dev->shutdown_lock);
558 mutex_init(&dev->powerup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700560 mutex_lock(&subsystem_list_lock);
561 list_add(&dev->list, &subsystem_list);
562 mutex_unlock(&subsystem_list_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700564 return dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565}
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700566EXPORT_SYMBOL(subsys_register);
567
568void subsys_unregister(struct subsys_device *dev)
569{
570 if (IS_ERR_OR_NULL(dev))
571 return;
572 mutex_lock(&subsystem_list_lock);
573 list_del(&dev->list);
574 mutex_unlock(&subsystem_list_lock);
Stephen Boyd497ef402012-06-21 12:54:40 -0700575 wake_lock_destroy(&dev->wake_lock);
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700576 kfree(dev);
577}
578EXPORT_SYMBOL(subsys_unregister);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700580static int ssr_panic_handler(struct notifier_block *this,
581 unsigned long event, void *ptr)
582{
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700583 struct subsys_device *dev;
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700584
Stephen Boyd0ebf7212012-04-30 20:42:35 -0700585 list_for_each_entry(dev, &subsystem_list, list)
586 if (dev->desc->crash_shutdown)
587 dev->desc->crash_shutdown(dev->desc);
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700588 return NOTIFY_DONE;
589}
590
591static struct notifier_block panic_nb = {
592 .notifier_call = ssr_panic_handler,
593};
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595static int __init ssr_init_soc_restart_orders(void)
596{
597 int i;
598
Vikram Mulukutla2a7ea7e2011-09-06 11:38:47 -0700599 atomic_notifier_chain_register(&panic_notifier_list,
600 &panic_nb);
601
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 if (cpu_is_msm8x60()) {
603 for (i = 0; i < ARRAY_SIZE(orders_8x60_all); i++) {
604 mutex_init(&orders_8x60_all[i]->powerup_lock);
605 mutex_init(&orders_8x60_all[i]->shutdown_lock);
606 }
607
608 for (i = 0; i < ARRAY_SIZE(orders_8x60_modems); i++) {
609 mutex_init(&orders_8x60_modems[i]->powerup_lock);
610 mutex_init(&orders_8x60_modems[i]->shutdown_lock);
611 }
612
613 restart_orders = orders_8x60_all;
614 n_restart_orders = ARRAY_SIZE(orders_8x60_all);
615 }
616
Vikram Mulukutlaf68456d2012-07-27 15:14:10 -0700617 if (socinfo_get_platform_subtype() == PLATFORM_SUBTYPE_SGLTE) {
618 restart_orders = restart_orders_8960_sglte;
619 n_restart_orders = ARRAY_SIZE(restart_orders_8960_sglte);
620 }
621
Joel Kinge4f98902013-03-25 13:12:27 -0700622 if (socinfo_get_platform_subtype() == PLATFORM_SUBTYPE_SGLTE2) {
623 restart_orders = restart_orders_8064_sglte2;
624 n_restart_orders = ARRAY_SIZE(restart_orders_8064_sglte2);
625 }
626
Vikram Mulukutlaf68456d2012-07-27 15:14:10 -0700627 for (i = 0; i < n_restart_orders; i++) {
628 mutex_init(&restart_orders[i]->powerup_lock);
629 mutex_init(&restart_orders[i]->shutdown_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 }
631
632 if (restart_orders == NULL || n_restart_orders < 1) {
633 WARN_ON(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634 }
635
636 return 0;
637}
638
639static int __init subsys_restart_init(void)
640{
Vikram Mulukutla372e15a2013-01-22 10:51:17 -0800641 restart_level = RESET_SOC;
642
Vikram Mulukutla99f84eff2012-09-10 19:25:32 -0700643 ssr_wq = alloc_workqueue("ssr_wq", WQ_CPU_INTENSIVE, 0);
Vikram Mulukutla0ad34f62013-01-22 11:01:13 -0800644 if (!ssr_wq)
645 panic("%s: out of memory\n", __func__);
Vikram Mulukutla69177e12012-03-21 20:51:43 -0700646
Iliyan Malcheva0899ce2012-06-29 15:28:25 -0700647 return ssr_init_soc_restart_orders();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649arch_initcall(subsys_restart_init);
650
651MODULE_DESCRIPTION("Subsystem Restart Driver");
652MODULE_LICENSE("GPL v2");