aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/power
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/power')
-rw-r--r--drivers/base/power/domain.c402
-rw-r--r--drivers/base/power/opp.c17
-rw-r--r--drivers/base/power/trace.c2
3 files changed, 339 insertions, 82 deletions
diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 1aed94c73cfc..be8714aa9dd6 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -13,6 +13,11 @@
13#include <linux/pm_domain.h> 13#include <linux/pm_domain.h>
14#include <linux/slab.h> 14#include <linux/slab.h>
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/sched.h>
17#include <linux/suspend.h>
18
19static LIST_HEAD(gpd_list);
20static DEFINE_MUTEX(gpd_list_lock);
16 21
17#ifdef CONFIG_PM 22#ifdef CONFIG_PM
18 23
@@ -30,6 +35,41 @@ static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
30 genpd->sd_count--; 35 genpd->sd_count--;
31} 36}
32 37
38static void genpd_acquire_lock(struct generic_pm_domain *genpd)
39{
40 DEFINE_WAIT(wait);
41
42 mutex_lock(&genpd->lock);
43 /*
44 * Wait for the domain to transition into either the active,
45 * or the power off state.
46 */
47 for (;;) {
48 prepare_to_wait(&genpd->status_wait_queue, &wait,
49 TASK_UNINTERRUPTIBLE);
50 if (genpd->status == GPD_STATE_ACTIVE
51 || genpd->status == GPD_STATE_POWER_OFF)
52 break;
53 mutex_unlock(&genpd->lock);
54
55 schedule();
56
57 mutex_lock(&genpd->lock);
58 }
59 finish_wait(&genpd->status_wait_queue, &wait);
60}
61
62static void genpd_release_lock(struct generic_pm_domain *genpd)
63{
64 mutex_unlock(&genpd->lock);
65}
66
67static void genpd_set_active(struct generic_pm_domain *genpd)
68{
69 if (genpd->resume_count == 0)
70 genpd->status = GPD_STATE_ACTIVE;
71}
72
33/** 73/**
34 * pm_genpd_poweron - Restore power to a given PM domain and its parents. 74 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
35 * @genpd: PM domain to power up. 75 * @genpd: PM domain to power up.
@@ -37,24 +77,34 @@ static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
37 * Restore power to @genpd and all of its parents so that it is possible to 77 * Restore power to @genpd and all of its parents so that it is possible to
38 * resume a device belonging to it. 78 * resume a device belonging to it.
39 */ 79 */
40static int pm_genpd_poweron(struct generic_pm_domain *genpd) 80int pm_genpd_poweron(struct generic_pm_domain *genpd)
41{ 81{
82 struct generic_pm_domain *parent = genpd->parent;
83 DEFINE_WAIT(wait);
42 int ret = 0; 84 int ret = 0;
43 85
44 start: 86 start:
45 if (genpd->parent) 87 if (parent) {
46 mutex_lock(&genpd->parent->lock); 88 genpd_acquire_lock(parent);
47 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING); 89 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
90 } else {
91 mutex_lock(&genpd->lock);
92 }
48 93
49 if (!genpd->power_is_off 94 if (genpd->status == GPD_STATE_ACTIVE
50 || (genpd->prepared_count > 0 && genpd->suspend_power_off)) 95 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
51 goto out; 96 goto out;
52 97
53 if (genpd->parent && genpd->parent->power_is_off) { 98 if (genpd->status != GPD_STATE_POWER_OFF) {
99 genpd_set_active(genpd);
100 goto out;
101 }
102
103 if (parent && parent->status != GPD_STATE_ACTIVE) {
54 mutex_unlock(&genpd->lock); 104 mutex_unlock(&genpd->lock);
55 mutex_unlock(&genpd->parent->lock); 105 genpd_release_lock(parent);
56 106
57 ret = pm_genpd_poweron(genpd->parent); 107 ret = pm_genpd_poweron(parent);
58 if (ret) 108 if (ret)
59 return ret; 109 return ret;
60 110
@@ -67,14 +117,14 @@ static int pm_genpd_poweron(struct generic_pm_domain *genpd)
67 goto out; 117 goto out;
68 } 118 }
69 119
70 genpd->power_is_off = false; 120 genpd_set_active(genpd);
71 if (genpd->parent) 121 if (parent)
72 genpd->parent->sd_count++; 122 parent->sd_count++;
73 123
74 out: 124 out:
75 mutex_unlock(&genpd->lock); 125 mutex_unlock(&genpd->lock);
76 if (genpd->parent) 126 if (parent)
77 mutex_unlock(&genpd->parent->lock); 127 genpd_release_lock(parent);
78 128
79 return ret; 129 return ret;
80} 130}
@@ -90,6 +140,7 @@ static int pm_genpd_poweron(struct generic_pm_domain *genpd)
90 */ 140 */
91static int __pm_genpd_save_device(struct dev_list_entry *dle, 141static int __pm_genpd_save_device(struct dev_list_entry *dle,
92 struct generic_pm_domain *genpd) 142 struct generic_pm_domain *genpd)
143 __releases(&genpd->lock) __acquires(&genpd->lock)
93{ 144{
94 struct device *dev = dle->dev; 145 struct device *dev = dle->dev;
95 struct device_driver *drv = dev->driver; 146 struct device_driver *drv = dev->driver;
@@ -98,6 +149,8 @@ static int __pm_genpd_save_device(struct dev_list_entry *dle,
98 if (dle->need_restore) 149 if (dle->need_restore)
99 return 0; 150 return 0;
100 151
152 mutex_unlock(&genpd->lock);
153
101 if (drv && drv->pm && drv->pm->runtime_suspend) { 154 if (drv && drv->pm && drv->pm->runtime_suspend) {
102 if (genpd->start_device) 155 if (genpd->start_device)
103 genpd->start_device(dev); 156 genpd->start_device(dev);
@@ -108,6 +161,8 @@ static int __pm_genpd_save_device(struct dev_list_entry *dle,
108 genpd->stop_device(dev); 161 genpd->stop_device(dev);
109 } 162 }
110 163
164 mutex_lock(&genpd->lock);
165
111 if (!ret) 166 if (!ret)
112 dle->need_restore = true; 167 dle->need_restore = true;
113 168
@@ -121,6 +176,7 @@ static int __pm_genpd_save_device(struct dev_list_entry *dle,
121 */ 176 */
122static void __pm_genpd_restore_device(struct dev_list_entry *dle, 177static void __pm_genpd_restore_device(struct dev_list_entry *dle,
123 struct generic_pm_domain *genpd) 178 struct generic_pm_domain *genpd)
179 __releases(&genpd->lock) __acquires(&genpd->lock)
124{ 180{
125 struct device *dev = dle->dev; 181 struct device *dev = dle->dev;
126 struct device_driver *drv = dev->driver; 182 struct device_driver *drv = dev->driver;
@@ -128,6 +184,8 @@ static void __pm_genpd_restore_device(struct dev_list_entry *dle,
128 if (!dle->need_restore) 184 if (!dle->need_restore)
129 return; 185 return;
130 186
187 mutex_unlock(&genpd->lock);
188
131 if (drv && drv->pm && drv->pm->runtime_resume) { 189 if (drv && drv->pm && drv->pm->runtime_resume) {
132 if (genpd->start_device) 190 if (genpd->start_device)
133 genpd->start_device(dev); 191 genpd->start_device(dev);
@@ -138,10 +196,39 @@ static void __pm_genpd_restore_device(struct dev_list_entry *dle,
138 genpd->stop_device(dev); 196 genpd->stop_device(dev);
139 } 197 }
140 198
199 mutex_lock(&genpd->lock);
200
141 dle->need_restore = false; 201 dle->need_restore = false;
142} 202}
143 203
144/** 204/**
205 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
206 * @genpd: PM domain to check.
207 *
208 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
209 * a "power off" operation, which means that a "power on" has occured in the
210 * meantime, or if its resume_count field is different from zero, which means
211 * that one of its devices has been resumed in the meantime.
212 */
213static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
214{
215 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
216}
217
218/**
219 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
220 * @genpd: PM domait to power off.
221 *
222 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
223 * before.
224 */
225void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
226{
227 if (!work_pending(&genpd->power_off_work))
228 queue_work(pm_wq, &genpd->power_off_work);
229}
230
231/**
145 * pm_genpd_poweroff - Remove power from a given PM domain. 232 * pm_genpd_poweroff - Remove power from a given PM domain.
146 * @genpd: PM domain to power down. 233 * @genpd: PM domain to power down.
147 * 234 *
@@ -150,13 +237,22 @@ static void __pm_genpd_restore_device(struct dev_list_entry *dle,
150 * the @genpd's devices' drivers and remove power from @genpd. 237 * the @genpd's devices' drivers and remove power from @genpd.
151 */ 238 */
152static int pm_genpd_poweroff(struct generic_pm_domain *genpd) 239static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
240 __releases(&genpd->lock) __acquires(&genpd->lock)
153{ 241{
154 struct generic_pm_domain *parent; 242 struct generic_pm_domain *parent;
155 struct dev_list_entry *dle; 243 struct dev_list_entry *dle;
156 unsigned int not_suspended; 244 unsigned int not_suspended;
157 int ret; 245 int ret = 0;
158 246
159 if (genpd->power_is_off || genpd->prepared_count > 0) 247 start:
248 /*
249 * Do not try to power off the domain in the following situations:
250 * (1) The domain is already in the "power off" state.
251 * (2) System suspend is in progress.
252 * (3) One of the domain's devices is being resumed right now.
253 */
254 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
255 || genpd->resume_count > 0)
160 return 0; 256 return 0;
161 257
162 if (genpd->sd_count > 0) 258 if (genpd->sd_count > 0)
@@ -170,35 +266,76 @@ static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
170 if (not_suspended > genpd->in_progress) 266 if (not_suspended > genpd->in_progress)
171 return -EBUSY; 267 return -EBUSY;
172 268
269 if (genpd->poweroff_task) {
270 /*
271 * Another instance of pm_genpd_poweroff() is executing
272 * callbacks, so tell it to start over and return.
273 */
274 genpd->status = GPD_STATE_REPEAT;
275 return 0;
276 }
277
173 if (genpd->gov && genpd->gov->power_down_ok) { 278 if (genpd->gov && genpd->gov->power_down_ok) {
174 if (!genpd->gov->power_down_ok(&genpd->domain)) 279 if (!genpd->gov->power_down_ok(&genpd->domain))
175 return -EAGAIN; 280 return -EAGAIN;
176 } 281 }
177 282
283 genpd->status = GPD_STATE_BUSY;
284 genpd->poweroff_task = current;
285
178 list_for_each_entry_reverse(dle, &genpd->dev_list, node) { 286 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
179 ret = __pm_genpd_save_device(dle, genpd); 287 ret = __pm_genpd_save_device(dle, genpd);
180 if (ret) 288 if (ret) {
181 goto err_dev; 289 genpd_set_active(genpd);
182 } 290 goto out;
291 }
183 292
184 if (genpd->power_off) 293 if (genpd_abort_poweroff(genpd))
185 genpd->power_off(genpd); 294 goto out;
186 295
187 genpd->power_is_off = true; 296 if (genpd->status == GPD_STATE_REPEAT) {
297 genpd->poweroff_task = NULL;
298 goto start;
299 }
300 }
188 301
189 parent = genpd->parent; 302 parent = genpd->parent;
190 if (parent) { 303 if (parent) {
191 genpd_sd_counter_dec(parent); 304 mutex_unlock(&genpd->lock);
192 if (parent->sd_count == 0) 305
193 queue_work(pm_wq, &parent->power_off_work); 306 genpd_acquire_lock(parent);
307 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
308
309 if (genpd_abort_poweroff(genpd)) {
310 genpd_release_lock(parent);
311 goto out;
312 }
194 } 313 }
195 314
196 return 0; 315 if (genpd->power_off) {
316 ret = genpd->power_off(genpd);
317 if (ret == -EBUSY) {
318 genpd_set_active(genpd);
319 if (parent)
320 genpd_release_lock(parent);
321
322 goto out;
323 }
324 }
325
326 genpd->status = GPD_STATE_POWER_OFF;
327
328 if (parent) {
329 genpd_sd_counter_dec(parent);
330 if (parent->sd_count == 0)
331 genpd_queue_power_off_work(parent);
197 332
198 err_dev: 333 genpd_release_lock(parent);
199 list_for_each_entry_continue(dle, &genpd->dev_list, node) 334 }
200 __pm_genpd_restore_device(dle, genpd);
201 335
336 out:
337 genpd->poweroff_task = NULL;
338 wake_up_all(&genpd->status_wait_queue);
202 return ret; 339 return ret;
203} 340}
204 341
@@ -212,13 +349,9 @@ static void genpd_power_off_work_fn(struct work_struct *work)
212 349
213 genpd = container_of(work, struct generic_pm_domain, power_off_work); 350 genpd = container_of(work, struct generic_pm_domain, power_off_work);
214 351
215 if (genpd->parent) 352 genpd_acquire_lock(genpd);
216 mutex_lock(&genpd->parent->lock);
217 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
218 pm_genpd_poweroff(genpd); 353 pm_genpd_poweroff(genpd);
219 mutex_unlock(&genpd->lock); 354 genpd_release_lock(genpd);
220 if (genpd->parent)
221 mutex_unlock(&genpd->parent->lock);
222} 355}
223 356
224/** 357/**
@@ -239,23 +372,17 @@ static int pm_genpd_runtime_suspend(struct device *dev)
239 if (IS_ERR(genpd)) 372 if (IS_ERR(genpd))
240 return -EINVAL; 373 return -EINVAL;
241 374
242 if (genpd->parent)
243 mutex_lock(&genpd->parent->lock);
244 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
245
246 if (genpd->stop_device) { 375 if (genpd->stop_device) {
247 int ret = genpd->stop_device(dev); 376 int ret = genpd->stop_device(dev);
248 if (ret) 377 if (ret)
249 goto out; 378 return ret;
250 } 379 }
380
381 mutex_lock(&genpd->lock);
251 genpd->in_progress++; 382 genpd->in_progress++;
252 pm_genpd_poweroff(genpd); 383 pm_genpd_poweroff(genpd);
253 genpd->in_progress--; 384 genpd->in_progress--;
254
255 out:
256 mutex_unlock(&genpd->lock); 385 mutex_unlock(&genpd->lock);
257 if (genpd->parent)
258 mutex_unlock(&genpd->parent->lock);
259 386
260 return 0; 387 return 0;
261} 388}
@@ -276,9 +403,6 @@ static void __pm_genpd_runtime_resume(struct device *dev,
276 break; 403 break;
277 } 404 }
278 } 405 }
279
280 if (genpd->start_device)
281 genpd->start_device(dev);
282} 406}
283 407
284/** 408/**
@@ -292,6 +416,7 @@ static void __pm_genpd_runtime_resume(struct device *dev,
292static int pm_genpd_runtime_resume(struct device *dev) 416static int pm_genpd_runtime_resume(struct device *dev)
293{ 417{
294 struct generic_pm_domain *genpd; 418 struct generic_pm_domain *genpd;
419 DEFINE_WAIT(wait);
295 int ret; 420 int ret;
296 421
297 dev_dbg(dev, "%s()\n", __func__); 422 dev_dbg(dev, "%s()\n", __func__);
@@ -305,9 +430,34 @@ static int pm_genpd_runtime_resume(struct device *dev)
305 return ret; 430 return ret;
306 431
307 mutex_lock(&genpd->lock); 432 mutex_lock(&genpd->lock);
433 genpd->status = GPD_STATE_BUSY;
434 genpd->resume_count++;
435 for (;;) {
436 prepare_to_wait(&genpd->status_wait_queue, &wait,
437 TASK_UNINTERRUPTIBLE);
438 /*
439 * If current is the powering off task, we have been called
440 * reentrantly from one of the device callbacks, so we should
441 * not wait.
442 */
443 if (!genpd->poweroff_task || genpd->poweroff_task == current)
444 break;
445 mutex_unlock(&genpd->lock);
446
447 schedule();
448
449 mutex_lock(&genpd->lock);
450 }
451 finish_wait(&genpd->status_wait_queue, &wait);
308 __pm_genpd_runtime_resume(dev, genpd); 452 __pm_genpd_runtime_resume(dev, genpd);
453 genpd->resume_count--;
454 genpd_set_active(genpd);
455 wake_up_all(&genpd->status_wait_queue);
309 mutex_unlock(&genpd->lock); 456 mutex_unlock(&genpd->lock);
310 457
458 if (genpd->start_device)
459 genpd->start_device(dev);
460
311 return 0; 461 return 0;
312} 462}
313 463
@@ -339,7 +489,7 @@ static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
339{ 489{
340 struct generic_pm_domain *parent = genpd->parent; 490 struct generic_pm_domain *parent = genpd->parent;
341 491
342 if (genpd->power_is_off) 492 if (genpd->status == GPD_STATE_POWER_OFF)
343 return; 493 return;
344 494
345 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0) 495 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
@@ -348,7 +498,7 @@ static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
348 if (genpd->power_off) 498 if (genpd->power_off)
349 genpd->power_off(genpd); 499 genpd->power_off(genpd);
350 500
351 genpd->power_is_off = true; 501 genpd->status = GPD_STATE_POWER_OFF;
352 if (parent) { 502 if (parent) {
353 genpd_sd_counter_dec(parent); 503 genpd_sd_counter_dec(parent);
354 pm_genpd_sync_poweroff(parent); 504 pm_genpd_sync_poweroff(parent);
@@ -356,6 +506,33 @@ static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
356} 506}
357 507
358/** 508/**
509 * resume_needed - Check whether to resume a device before system suspend.
510 * @dev: Device to check.
511 * @genpd: PM domain the device belongs to.
512 *
513 * There are two cases in which a device that can wake up the system from sleep
514 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
515 * to wake up the system and it has to remain active for this purpose while the
516 * system is in the sleep state and (2) if the device is not enabled to wake up
517 * the system from sleep states and it generally doesn't generate wakeup signals
518 * by itself (those signals are generated on its behalf by other parts of the
519 * system). In the latter case it may be necessary to reconfigure the device's
520 * wakeup settings during system suspend, because it may have been set up to
521 * signal remote wakeup from the system's working state as needed by runtime PM.
522 * Return 'true' in either of the above cases.
523 */
524static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
525{
526 bool active_wakeup;
527
528 if (!device_can_wakeup(dev))
529 return false;
530
531 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
532 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
533}
534
535/**
359 * pm_genpd_prepare - Start power transition of a device in a PM domain. 536 * pm_genpd_prepare - Start power transition of a device in a PM domain.
360 * @dev: Device to start the transition of. 537 * @dev: Device to start the transition of.
361 * 538 *
@@ -367,6 +544,7 @@ static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
367static int pm_genpd_prepare(struct device *dev) 544static int pm_genpd_prepare(struct device *dev)
368{ 545{
369 struct generic_pm_domain *genpd; 546 struct generic_pm_domain *genpd;
547 int ret;
370 548
371 dev_dbg(dev, "%s()\n", __func__); 549 dev_dbg(dev, "%s()\n", __func__);
372 550
@@ -374,33 +552,57 @@ static int pm_genpd_prepare(struct device *dev)
374 if (IS_ERR(genpd)) 552 if (IS_ERR(genpd))
375 return -EINVAL; 553 return -EINVAL;
376 554
377 mutex_lock(&genpd->lock); 555 /*
556 * If a wakeup request is pending for the device, it should be woken up
557 * at this point and a system wakeup event should be reported if it's
558 * set up to wake up the system from sleep states.
559 */
560 pm_runtime_get_noresume(dev);
561 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
562 pm_wakeup_event(dev, 0);
563
564 if (pm_wakeup_pending()) {
565 pm_runtime_put_sync(dev);
566 return -EBUSY;
567 }
568
569 if (resume_needed(dev, genpd))
570 pm_runtime_resume(dev);
571
572 genpd_acquire_lock(genpd);
378 573
379 if (genpd->prepared_count++ == 0) 574 if (genpd->prepared_count++ == 0)
380 genpd->suspend_power_off = genpd->power_is_off; 575 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
576
577 genpd_release_lock(genpd);
381 578
382 if (genpd->suspend_power_off) { 579 if (genpd->suspend_power_off) {
383 mutex_unlock(&genpd->lock); 580 pm_runtime_put_noidle(dev);
384 return 0; 581 return 0;
385 } 582 }
386 583
387 /* 584 /*
388 * If the device is in the (runtime) "suspended" state, call 585 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
389 * .start_device() for it, if defined. 586 * so pm_genpd_poweron() will return immediately, but if the device
390 */ 587 * is suspended (e.g. it's been stopped by .stop_device()), we need
391 if (pm_runtime_suspended(dev)) 588 * to make it operational.
392 __pm_genpd_runtime_resume(dev, genpd);
393
394 /*
395 * Do not check if runtime resume is pending at this point, because it
396 * has been taken care of already and if pm_genpd_poweron() ran at this
397 * point as a result of the check, it would deadlock.
398 */ 589 */
590 pm_runtime_resume(dev);
399 __pm_runtime_disable(dev, false); 591 __pm_runtime_disable(dev, false);
400 592
401 mutex_unlock(&genpd->lock); 593 ret = pm_generic_prepare(dev);
594 if (ret) {
595 mutex_lock(&genpd->lock);
596
597 if (--genpd->prepared_count == 0)
598 genpd->suspend_power_off = false;
402 599
403 return pm_generic_prepare(dev); 600 mutex_unlock(&genpd->lock);
601 pm_runtime_enable(dev);
602 }
603
604 pm_runtime_put_sync(dev);
605 return ret;
404} 606}
405 607
406/** 608/**
@@ -716,7 +918,7 @@ static int pm_genpd_restore_noirq(struct device *dev)
716 * guaranteed that this function will never run twice in parallel for 918 * guaranteed that this function will never run twice in parallel for
717 * the same PM domain, so it is not necessary to use locking here. 919 * the same PM domain, so it is not necessary to use locking here.
718 */ 920 */
719 genpd->power_is_off = true; 921 genpd->status = GPD_STATE_POWER_OFF;
720 if (genpd->suspend_power_off) { 922 if (genpd->suspend_power_off) {
721 /* 923 /*
722 * The boot kernel might put the domain into the power on state, 924 * The boot kernel might put the domain into the power on state,
@@ -786,7 +988,9 @@ static void pm_genpd_complete(struct device *dev)
786 988
787 if (run_complete) { 989 if (run_complete) {
788 pm_generic_complete(dev); 990 pm_generic_complete(dev);
991 pm_runtime_set_active(dev);
789 pm_runtime_enable(dev); 992 pm_runtime_enable(dev);
993 pm_runtime_idle(dev);
790 } 994 }
791} 995}
792 996
@@ -824,9 +1028,9 @@ int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
824 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) 1028 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
825 return -EINVAL; 1029 return -EINVAL;
826 1030
827 mutex_lock(&genpd->lock); 1031 genpd_acquire_lock(genpd);
828 1032
829 if (genpd->power_is_off) { 1033 if (genpd->status == GPD_STATE_POWER_OFF) {
830 ret = -EINVAL; 1034 ret = -EINVAL;
831 goto out; 1035 goto out;
832 } 1036 }
@@ -858,7 +1062,7 @@ int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
858 spin_unlock_irq(&dev->power.lock); 1062 spin_unlock_irq(&dev->power.lock);
859 1063
860 out: 1064 out:
861 mutex_unlock(&genpd->lock); 1065 genpd_release_lock(genpd);
862 1066
863 return ret; 1067 return ret;
864} 1068}
@@ -879,7 +1083,7 @@ int pm_genpd_remove_device(struct generic_pm_domain *genpd,
879 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) 1083 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
880 return -EINVAL; 1084 return -EINVAL;
881 1085
882 mutex_lock(&genpd->lock); 1086 genpd_acquire_lock(genpd);
883 1087
884 if (genpd->prepared_count > 0) { 1088 if (genpd->prepared_count > 0) {
885 ret = -EAGAIN; 1089 ret = -EAGAIN;
@@ -903,7 +1107,7 @@ int pm_genpd_remove_device(struct generic_pm_domain *genpd,
903 } 1107 }
904 1108
905 out: 1109 out:
906 mutex_unlock(&genpd->lock); 1110 genpd_release_lock(genpd);
907 1111
908 return ret; 1112 return ret;
909} 1113}
@@ -922,9 +1126,19 @@ int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
922 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain)) 1126 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
923 return -EINVAL; 1127 return -EINVAL;
924 1128
925 mutex_lock(&genpd->lock); 1129 start:
1130 genpd_acquire_lock(genpd);
1131 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
1132
1133 if (new_subdomain->status != GPD_STATE_POWER_OFF
1134 && new_subdomain->status != GPD_STATE_ACTIVE) {
1135 mutex_unlock(&new_subdomain->lock);
1136 genpd_release_lock(genpd);
1137 goto start;
1138 }
926 1139
927 if (genpd->power_is_off && !new_subdomain->power_is_off) { 1140 if (genpd->status == GPD_STATE_POWER_OFF
1141 && new_subdomain->status != GPD_STATE_POWER_OFF) {
928 ret = -EINVAL; 1142 ret = -EINVAL;
929 goto out; 1143 goto out;
930 } 1144 }
@@ -936,17 +1150,14 @@ int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
936 } 1150 }
937 } 1151 }
938 1152
939 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
940
941 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list); 1153 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
942 new_subdomain->parent = genpd; 1154 new_subdomain->parent = genpd;
943 if (!subdomain->power_is_off) 1155 if (subdomain->status != GPD_STATE_POWER_OFF)
944 genpd->sd_count++; 1156 genpd->sd_count++;
945 1157
946 mutex_unlock(&new_subdomain->lock);
947
948 out: 1158 out:
949 mutex_unlock(&genpd->lock); 1159 mutex_unlock(&new_subdomain->lock);
1160 genpd_release_lock(genpd);
950 1161
951 return ret; 1162 return ret;
952} 1163}
@@ -965,7 +1176,8 @@ int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
965 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target)) 1176 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
966 return -EINVAL; 1177 return -EINVAL;
967 1178
968 mutex_lock(&genpd->lock); 1179 start:
1180 genpd_acquire_lock(genpd);
969 1181
970 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) { 1182 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
971 if (subdomain != target) 1183 if (subdomain != target)
@@ -973,9 +1185,16 @@ int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
973 1185
974 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); 1186 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
975 1187
1188 if (subdomain->status != GPD_STATE_POWER_OFF
1189 && subdomain->status != GPD_STATE_ACTIVE) {
1190 mutex_unlock(&subdomain->lock);
1191 genpd_release_lock(genpd);
1192 goto start;
1193 }
1194
976 list_del(&subdomain->sd_node); 1195 list_del(&subdomain->sd_node);
977 subdomain->parent = NULL; 1196 subdomain->parent = NULL;
978 if (!subdomain->power_is_off) 1197 if (subdomain->status != GPD_STATE_POWER_OFF)
979 genpd_sd_counter_dec(genpd); 1198 genpd_sd_counter_dec(genpd);
980 1199
981 mutex_unlock(&subdomain->lock); 1200 mutex_unlock(&subdomain->lock);
@@ -984,7 +1203,7 @@ int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
984 break; 1203 break;
985 } 1204 }
986 1205
987 mutex_unlock(&genpd->lock); 1206 genpd_release_lock(genpd);
988 1207
989 return ret; 1208 return ret;
990} 1209}
@@ -1010,7 +1229,10 @@ void pm_genpd_init(struct generic_pm_domain *genpd,
1010 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); 1229 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1011 genpd->in_progress = 0; 1230 genpd->in_progress = 0;
1012 genpd->sd_count = 0; 1231 genpd->sd_count = 0;
1013 genpd->power_is_off = is_off; 1232 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1233 init_waitqueue_head(&genpd->status_wait_queue);
1234 genpd->poweroff_task = NULL;
1235 genpd->resume_count = 0;
1014 genpd->device_count = 0; 1236 genpd->device_count = 0;
1015 genpd->suspended_count = 0; 1237 genpd->suspended_count = 0;
1016 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; 1238 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
@@ -1030,4 +1252,22 @@ void pm_genpd_init(struct generic_pm_domain *genpd,
1030 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; 1252 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1031 genpd->domain.ops.restore = pm_genpd_restore; 1253 genpd->domain.ops.restore = pm_genpd_restore;
1032 genpd->domain.ops.complete = pm_genpd_complete; 1254 genpd->domain.ops.complete = pm_genpd_complete;
1255 mutex_lock(&gpd_list_lock);
1256 list_add(&genpd->gpd_list_node, &gpd_list);
1257 mutex_unlock(&gpd_list_lock);
1258}
1259
1260/**
1261 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
1262 */
1263void pm_genpd_poweroff_unused(void)
1264{
1265 struct generic_pm_domain *genpd;
1266
1267 mutex_lock(&gpd_list_lock);
1268
1269 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
1270 genpd_queue_power_off_work(genpd);
1271
1272 mutex_unlock(&gpd_list_lock);
1033} 1273}
diff --git a/drivers/base/power/opp.c b/drivers/base/power/opp.c
index 56a6899f5e9e..5cc12322ef32 100644
--- a/drivers/base/power/opp.c
+++ b/drivers/base/power/opp.c
@@ -625,4 +625,21 @@ int opp_init_cpufreq_table(struct device *dev,
625 625
626 return 0; 626 return 0;
627} 627}
628
629/**
630 * opp_free_cpufreq_table() - free the cpufreq table
631 * @dev: device for which we do this operation
632 * @table: table to free
633 *
634 * Free up the table allocated by opp_init_cpufreq_table
635 */
636void opp_free_cpufreq_table(struct device *dev,
637 struct cpufreq_frequency_table **table)
638{
639 if (!table)
640 return;
641
642 kfree(*table);
643 *table = NULL;
644}
628#endif /* CONFIG_CPU_FREQ */ 645#endif /* CONFIG_CPU_FREQ */
diff --git a/drivers/base/power/trace.c b/drivers/base/power/trace.c
index c80e138b62fe..af10abecb99b 100644
--- a/drivers/base/power/trace.c
+++ b/drivers/base/power/trace.c
@@ -112,7 +112,7 @@ static unsigned int read_magic_time(void)
112 unsigned int val; 112 unsigned int val;
113 113
114 get_rtc_time(&time); 114 get_rtc_time(&time);
115 pr_info("Time: %2d:%02d:%02d Date: %02d/%02d/%02d\n", 115 pr_info("RTC time: %2d:%02d:%02d, date: %02d/%02d/%02d\n",
116 time.tm_hour, time.tm_min, time.tm_sec, 116 time.tm_hour, time.tm_min, time.tm_sec,
117 time.tm_mon + 1, time.tm_mday, time.tm_year % 100); 117 time.tm_mon + 1, time.tm_mday, time.tm_year % 100);
118 val = time.tm_year; /* 100 years */ 118 val = time.tm_year; /* 100 years */