PM / domains: Keep declaration of dev_power_governors together
[deliverable/linux.git] / drivers / base / power / domain.c
CommitLineData
f721889f
RW
1/*
2 * drivers/base/power/domain.c - Common code related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
f721889f
RW
9#include <linux/kernel.h>
10#include <linux/io.h>
11#include <linux/pm_runtime.h>
12#include <linux/pm_domain.h>
6ff7bb0d 13#include <linux/pm_qos.h>
f721889f
RW
14#include <linux/slab.h>
15#include <linux/err.h>
17b75eca
RW
16#include <linux/sched.h>
17#include <linux/suspend.h>
d5e4cbfe
RW
18#include <linux/export.h>
19
20#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
21({ \
22 type (*__routine)(struct device *__d); \
23 type __ret = (type)0; \
24 \
25 __routine = genpd->dev_ops.callback; \
26 if (__routine) { \
27 __ret = __routine(dev); \
d5e4cbfe
RW
28 } \
29 __ret; \
30})
f721889f 31
0140d8bd
RW
32#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
33({ \
34 ktime_t __start = ktime_get(); \
35 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
36 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
6ff7bb0d
RW
37 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
38 if (!__retval && __elapsed > __td->field) { \
39 __td->field = __elapsed; \
7d1af287 40 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
0140d8bd 41 __elapsed); \
6ff7bb0d
RW
42 genpd->max_off_time_changed = true; \
43 __td->constraint_changed = true; \
0140d8bd
RW
44 } \
45 __retval; \
46})
47
5125bbf3
RW
48static LIST_HEAD(gpd_list);
49static DEFINE_MUTEX(gpd_list_lock);
50
8bc0251d
RW
51static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
52{
53 struct generic_pm_domain *genpd = NULL, *gpd;
54
55 if (IS_ERR_OR_NULL(domain_name))
56 return NULL;
57
58 mutex_lock(&gpd_list_lock);
59 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
60 if (!strcmp(gpd->name, domain_name)) {
61 genpd = gpd;
62 break;
63 }
64 }
65 mutex_unlock(&gpd_list_lock);
66 return genpd;
67}
68
b02c999a 69struct generic_pm_domain *dev_to_genpd(struct device *dev)
5248051b
RW
70{
71 if (IS_ERR_OR_NULL(dev->pm_domain))
72 return ERR_PTR(-EINVAL);
73
596ba34b 74 return pd_to_genpd(dev->pm_domain);
5248051b 75}
f721889f 76
d5e4cbfe
RW
77static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
78{
0140d8bd
RW
79 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
80 stop_latency_ns, "stop");
d5e4cbfe
RW
81}
82
83static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
84{
0140d8bd
RW
85 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
86 start_latency_ns, "start");
d5e4cbfe
RW
87}
88
c4bb3160 89static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
f721889f 90{
c4bb3160
RW
91 bool ret = false;
92
93 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
94 ret = !!atomic_dec_and_test(&genpd->sd_count);
95
96 return ret;
97}
98
99static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
100{
101 atomic_inc(&genpd->sd_count);
4e857c58 102 smp_mb__after_atomic();
f721889f
RW
103}
104
17b75eca
RW
105static void genpd_acquire_lock(struct generic_pm_domain *genpd)
106{
107 DEFINE_WAIT(wait);
108
109 mutex_lock(&genpd->lock);
110 /*
111 * Wait for the domain to transition into either the active,
112 * or the power off state.
113 */
114 for (;;) {
115 prepare_to_wait(&genpd->status_wait_queue, &wait,
116 TASK_UNINTERRUPTIBLE);
c6d22b37
RW
117 if (genpd->status == GPD_STATE_ACTIVE
118 || genpd->status == GPD_STATE_POWER_OFF)
17b75eca
RW
119 break;
120 mutex_unlock(&genpd->lock);
121
122 schedule();
123
124 mutex_lock(&genpd->lock);
125 }
126 finish_wait(&genpd->status_wait_queue, &wait);
127}
128
129static void genpd_release_lock(struct generic_pm_domain *genpd)
130{
131 mutex_unlock(&genpd->lock);
132}
133
c6d22b37
RW
134static void genpd_set_active(struct generic_pm_domain *genpd)
135{
136 if (genpd->resume_count == 0)
137 genpd->status = GPD_STATE_ACTIVE;
138}
139
cbc9ef02
RW
140static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
141{
142 s64 usecs64;
143
144 if (!genpd->cpu_data)
145 return;
146
147 usecs64 = genpd->power_on_latency_ns;
148 do_div(usecs64, NSEC_PER_USEC);
149 usecs64 += genpd->cpu_data->saved_exit_latency;
150 genpd->cpu_data->idle_state->exit_latency = usecs64;
151}
152
5248051b 153/**
5063ce15 154 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
5248051b
RW
155 * @genpd: PM domain to power up.
156 *
5063ce15 157 * Restore power to @genpd and all of its masters so that it is possible to
5248051b
RW
158 * resume a device belonging to it.
159 */
8951ef02 160static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
3f241775 161 __releases(&genpd->lock) __acquires(&genpd->lock)
5248051b 162{
5063ce15 163 struct gpd_link *link;
3f241775 164 DEFINE_WAIT(wait);
5248051b
RW
165 int ret = 0;
166
5063ce15 167 /* If the domain's master is being waited for, we have to wait too. */
3f241775
RW
168 for (;;) {
169 prepare_to_wait(&genpd->status_wait_queue, &wait,
170 TASK_UNINTERRUPTIBLE);
17877eb5 171 if (genpd->status != GPD_STATE_WAIT_MASTER)
3f241775
RW
172 break;
173 mutex_unlock(&genpd->lock);
17b75eca 174
3f241775
RW
175 schedule();
176
177 mutex_lock(&genpd->lock);
178 }
179 finish_wait(&genpd->status_wait_queue, &wait);
9e08cf42 180
17b75eca 181 if (genpd->status == GPD_STATE_ACTIVE
596ba34b 182 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
3f241775 183 return 0;
5248051b 184
c6d22b37
RW
185 if (genpd->status != GPD_STATE_POWER_OFF) {
186 genpd_set_active(genpd);
3f241775 187 return 0;
c6d22b37
RW
188 }
189
cbc9ef02
RW
190 if (genpd->cpu_data) {
191 cpuidle_pause_and_lock();
192 genpd->cpu_data->idle_state->disabled = true;
193 cpuidle_resume_and_unlock();
194 goto out;
195 }
196
5063ce15
RW
197 /*
198 * The list is guaranteed not to change while the loop below is being
199 * executed, unless one of the masters' .power_on() callbacks fiddles
200 * with it.
201 */
202 list_for_each_entry(link, &genpd->slave_links, slave_node) {
203 genpd_sd_counter_inc(link->master);
17877eb5 204 genpd->status = GPD_STATE_WAIT_MASTER;
3c07cbc4 205
5248051b 206 mutex_unlock(&genpd->lock);
5248051b 207
5063ce15 208 ret = pm_genpd_poweron(link->master);
9e08cf42
RW
209
210 mutex_lock(&genpd->lock);
211
3f241775
RW
212 /*
213 * The "wait for parent" status is guaranteed not to change
5063ce15 214 * while the master is powering on.
3f241775
RW
215 */
216 genpd->status = GPD_STATE_POWER_OFF;
217 wake_up_all(&genpd->status_wait_queue);
5063ce15
RW
218 if (ret) {
219 genpd_sd_counter_dec(link->master);
9e08cf42 220 goto err;
5063ce15 221 }
5248051b
RW
222 }
223
9e08cf42 224 if (genpd->power_on) {
0140d8bd
RW
225 ktime_t time_start = ktime_get();
226 s64 elapsed_ns;
227
fe202fde 228 ret = genpd->power_on(genpd);
9e08cf42
RW
229 if (ret)
230 goto err;
0140d8bd
RW
231
232 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
e84b2c20 233 if (elapsed_ns > genpd->power_on_latency_ns) {
0140d8bd 234 genpd->power_on_latency_ns = elapsed_ns;
6ff7bb0d 235 genpd->max_off_time_changed = true;
cbc9ef02 236 genpd_recalc_cpu_exit_latency(genpd);
e84b2c20
RW
237 if (genpd->name)
238 pr_warning("%s: Power-on latency exceeded, "
239 "new value %lld ns\n", genpd->name,
240 elapsed_ns);
241 }
3c07cbc4 242 }
5248051b 243
cbc9ef02 244 out:
9e08cf42
RW
245 genpd_set_active(genpd);
246
3f241775 247 return 0;
9e08cf42
RW
248
249 err:
5063ce15
RW
250 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
251 genpd_sd_counter_dec(link->master);
9e08cf42 252
3f241775
RW
253 return ret;
254}
255
256/**
5063ce15 257 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
3f241775
RW
258 * @genpd: PM domain to power up.
259 */
260int pm_genpd_poweron(struct generic_pm_domain *genpd)
261{
262 int ret;
263
264 mutex_lock(&genpd->lock);
265 ret = __pm_genpd_poweron(genpd);
266 mutex_unlock(&genpd->lock);
267 return ret;
5248051b
RW
268}
269
8bc0251d
RW
270/**
271 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
272 * @domain_name: Name of the PM domain to power up.
273 */
274int pm_genpd_name_poweron(const char *domain_name)
275{
276 struct generic_pm_domain *genpd;
277
278 genpd = pm_genpd_lookup_name(domain_name);
279 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
280}
281
5248051b
RW
282#ifdef CONFIG_PM_RUNTIME
283
b3d3b9fb
SK
284static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
285 struct device *dev)
286{
287 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
288}
289
8e9afafd
RW
290static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
291{
292 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
293 save_state_latency_ns, "state save");
294}
295
296static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
297{
298 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
299 restore_state_latency_ns,
300 "state restore");
301}
302
6ff7bb0d
RW
303static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
304 unsigned long val, void *ptr)
305{
306 struct generic_pm_domain_data *gpd_data;
307 struct device *dev;
308
309 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
310
311 mutex_lock(&gpd_data->lock);
312 dev = gpd_data->base.dev;
313 if (!dev) {
314 mutex_unlock(&gpd_data->lock);
315 return NOTIFY_DONE;
316 }
317 mutex_unlock(&gpd_data->lock);
318
319 for (;;) {
320 struct generic_pm_domain *genpd;
321 struct pm_domain_data *pdd;
322
323 spin_lock_irq(&dev->power.lock);
324
325 pdd = dev->power.subsys_data ?
326 dev->power.subsys_data->domain_data : NULL;
1d5fcfec 327 if (pdd && pdd->dev) {
6ff7bb0d
RW
328 to_gpd_data(pdd)->td.constraint_changed = true;
329 genpd = dev_to_genpd(dev);
330 } else {
331 genpd = ERR_PTR(-ENODATA);
332 }
333
334 spin_unlock_irq(&dev->power.lock);
335
336 if (!IS_ERR(genpd)) {
337 mutex_lock(&genpd->lock);
338 genpd->max_off_time_changed = true;
339 mutex_unlock(&genpd->lock);
340 }
341
342 dev = dev->parent;
343 if (!dev || dev->power.ignore_children)
344 break;
345 }
346
347 return NOTIFY_DONE;
348}
349
f721889f
RW
350/**
351 * __pm_genpd_save_device - Save the pre-suspend state of a device.
4605ab65 352 * @pdd: Domain data of the device to save the state of.
f721889f
RW
353 * @genpd: PM domain the device belongs to.
354 */
4605ab65 355static int __pm_genpd_save_device(struct pm_domain_data *pdd,
f721889f 356 struct generic_pm_domain *genpd)
17b75eca 357 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f 358{
cd0ea672 359 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
4605ab65 360 struct device *dev = pdd->dev;
f721889f
RW
361 int ret = 0;
362
cd0ea672 363 if (gpd_data->need_restore)
f721889f
RW
364 return 0;
365
17b75eca
RW
366 mutex_unlock(&genpd->lock);
367
ecf00475
RW
368 genpd_start_dev(genpd, dev);
369 ret = genpd_save_dev(genpd, dev);
370 genpd_stop_dev(genpd, dev);
f721889f 371
17b75eca
RW
372 mutex_lock(&genpd->lock);
373
f721889f 374 if (!ret)
cd0ea672 375 gpd_data->need_restore = true;
f721889f
RW
376
377 return ret;
378}
379
380/**
381 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
4605ab65 382 * @pdd: Domain data of the device to restore the state of.
f721889f
RW
383 * @genpd: PM domain the device belongs to.
384 */
4605ab65 385static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
f721889f 386 struct generic_pm_domain *genpd)
17b75eca 387 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f 388{
cd0ea672 389 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
4605ab65 390 struct device *dev = pdd->dev;
80de3d7f 391 bool need_restore = gpd_data->need_restore;
f721889f 392
80de3d7f 393 gpd_data->need_restore = false;
17b75eca
RW
394 mutex_unlock(&genpd->lock);
395
ecf00475 396 genpd_start_dev(genpd, dev);
80de3d7f
RW
397 if (need_restore)
398 genpd_restore_dev(genpd, dev);
f721889f 399
17b75eca 400 mutex_lock(&genpd->lock);
f721889f
RW
401}
402
c6d22b37
RW
403/**
404 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
405 * @genpd: PM domain to check.
406 *
407 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
408 * a "power off" operation, which means that a "power on" has occured in the
409 * meantime, or if its resume_count field is different from zero, which means
410 * that one of its devices has been resumed in the meantime.
411 */
412static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
413{
17877eb5 414 return genpd->status == GPD_STATE_WAIT_MASTER
3f241775 415 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
c6d22b37
RW
416}
417
56375fd4
RW
418/**
419 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
420 * @genpd: PM domait to power off.
421 *
422 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
423 * before.
424 */
d971f0b0 425static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
56375fd4 426{
a4ca26a4 427 queue_work(pm_wq, &genpd->power_off_work);
56375fd4
RW
428}
429
f721889f
RW
430/**
431 * pm_genpd_poweroff - Remove power from a given PM domain.
432 * @genpd: PM domain to power down.
433 *
434 * If all of the @genpd's devices have been suspended and all of its subdomains
435 * have been powered down, run the runtime suspend callbacks provided by all of
436 * the @genpd's devices' drivers and remove power from @genpd.
437 */
438static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
17b75eca 439 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f 440{
4605ab65 441 struct pm_domain_data *pdd;
5063ce15 442 struct gpd_link *link;
f721889f 443 unsigned int not_suspended;
c6d22b37 444 int ret = 0;
f721889f 445
c6d22b37
RW
446 start:
447 /*
448 * Do not try to power off the domain in the following situations:
449 * (1) The domain is already in the "power off" state.
5063ce15 450 * (2) The domain is waiting for its master to power up.
c6d22b37 451 * (3) One of the domain's devices is being resumed right now.
3f241775 452 * (4) System suspend is in progress.
c6d22b37 453 */
3f241775 454 if (genpd->status == GPD_STATE_POWER_OFF
17877eb5 455 || genpd->status == GPD_STATE_WAIT_MASTER
3f241775 456 || genpd->resume_count > 0 || genpd->prepared_count > 0)
f721889f
RW
457 return 0;
458
c4bb3160 459 if (atomic_read(&genpd->sd_count) > 0)
f721889f
RW
460 return -EBUSY;
461
462 not_suspended = 0;
34b1f762
RW
463 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
464 enum pm_qos_flags_status stat;
465
466 stat = dev_pm_qos_flags(pdd->dev,
467 PM_QOS_FLAG_NO_POWER_OFF
468 | PM_QOS_FLAG_REMOTE_WAKEUP);
469 if (stat > PM_QOS_FLAGS_NONE)
470 return -EBUSY;
471
0aa2a221 472 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
feb70af0 473 || pdd->dev->power.irq_safe))
f721889f 474 not_suspended++;
34b1f762 475 }
f721889f
RW
476
477 if (not_suspended > genpd->in_progress)
478 return -EBUSY;
479
c6d22b37
RW
480 if (genpd->poweroff_task) {
481 /*
482 * Another instance of pm_genpd_poweroff() is executing
483 * callbacks, so tell it to start over and return.
484 */
485 genpd->status = GPD_STATE_REPEAT;
486 return 0;
487 }
488
f721889f
RW
489 if (genpd->gov && genpd->gov->power_down_ok) {
490 if (!genpd->gov->power_down_ok(&genpd->domain))
491 return -EAGAIN;
492 }
493
17b75eca 494 genpd->status = GPD_STATE_BUSY;
c6d22b37 495 genpd->poweroff_task = current;
17b75eca 496
4605ab65 497 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
3c07cbc4 498 ret = atomic_read(&genpd->sd_count) == 0 ?
4605ab65 499 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
3f241775
RW
500
501 if (genpd_abort_poweroff(genpd))
502 goto out;
503
697a7f37
RW
504 if (ret) {
505 genpd_set_active(genpd);
506 goto out;
507 }
f721889f 508
c6d22b37
RW
509 if (genpd->status == GPD_STATE_REPEAT) {
510 genpd->poweroff_task = NULL;
511 goto start;
512 }
513 }
17b75eca 514
cbc9ef02
RW
515 if (genpd->cpu_data) {
516 /*
517 * If cpu_data is set, cpuidle should turn the domain off when
518 * the CPU in it is idle. In that case we don't decrement the
519 * subdomain counts of the master domains, so that power is not
520 * removed from the current domain prematurely as a result of
521 * cutting off the masters' power.
522 */
523 genpd->status = GPD_STATE_POWER_OFF;
524 cpuidle_pause_and_lock();
525 genpd->cpu_data->idle_state->disabled = false;
526 cpuidle_resume_and_unlock();
527 goto out;
528 }
529
3c07cbc4 530 if (genpd->power_off) {
0140d8bd
RW
531 ktime_t time_start;
532 s64 elapsed_ns;
533
3c07cbc4
RW
534 if (atomic_read(&genpd->sd_count) > 0) {
535 ret = -EBUSY;
c6d22b37
RW
536 goto out;
537 }
17b75eca 538
0140d8bd
RW
539 time_start = ktime_get();
540
3c07cbc4 541 /*
5063ce15
RW
542 * If sd_count > 0 at this point, one of the subdomains hasn't
543 * managed to call pm_genpd_poweron() for the master yet after
3c07cbc4
RW
544 * incrementing it. In that case pm_genpd_poweron() will wait
545 * for us to drop the lock, so we can call .power_off() and let
546 * the pm_genpd_poweron() restore power for us (this shouldn't
547 * happen very often).
548 */
d2805402
RW
549 ret = genpd->power_off(genpd);
550 if (ret == -EBUSY) {
551 genpd_set_active(genpd);
d2805402
RW
552 goto out;
553 }
0140d8bd
RW
554
555 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
e84b2c20 556 if (elapsed_ns > genpd->power_off_latency_ns) {
0140d8bd 557 genpd->power_off_latency_ns = elapsed_ns;
6ff7bb0d 558 genpd->max_off_time_changed = true;
e84b2c20
RW
559 if (genpd->name)
560 pr_warning("%s: Power-off latency exceeded, "
561 "new value %lld ns\n", genpd->name,
562 elapsed_ns);
563 }
d2805402 564 }
f721889f 565
17b75eca 566 genpd->status = GPD_STATE_POWER_OFF;
221e9b58 567
5063ce15
RW
568 list_for_each_entry(link, &genpd->slave_links, slave_node) {
569 genpd_sd_counter_dec(link->master);
570 genpd_queue_power_off_work(link->master);
571 }
f721889f 572
c6d22b37
RW
573 out:
574 genpd->poweroff_task = NULL;
575 wake_up_all(&genpd->status_wait_queue);
576 return ret;
f721889f
RW
577}
578
579/**
580 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
581 * @work: Work structure used for scheduling the execution of this function.
582 */
583static void genpd_power_off_work_fn(struct work_struct *work)
584{
585 struct generic_pm_domain *genpd;
586
587 genpd = container_of(work, struct generic_pm_domain, power_off_work);
588
17b75eca 589 genpd_acquire_lock(genpd);
f721889f 590 pm_genpd_poweroff(genpd);
17b75eca 591 genpd_release_lock(genpd);
f721889f
RW
592}
593
594/**
595 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
596 * @dev: Device to suspend.
597 *
598 * Carry out a runtime suspend of a device under the assumption that its
599 * pm_domain field points to the domain member of an object of type
600 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
601 */
602static int pm_genpd_runtime_suspend(struct device *dev)
603{
604 struct generic_pm_domain *genpd;
b02c999a 605 bool (*stop_ok)(struct device *__dev);
d5e4cbfe 606 int ret;
f721889f
RW
607
608 dev_dbg(dev, "%s()\n", __func__);
609
5248051b
RW
610 genpd = dev_to_genpd(dev);
611 if (IS_ERR(genpd))
f721889f
RW
612 return -EINVAL;
613
b02c999a
RW
614 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
615 if (stop_ok && !stop_ok(dev))
616 return -EBUSY;
617
d5e4cbfe
RW
618 ret = genpd_stop_dev(genpd, dev);
619 if (ret)
620 return ret;
17b75eca 621
0aa2a221
RW
622 /*
623 * If power.irq_safe is set, this routine will be run with interrupts
624 * off, so it can't use mutexes.
625 */
626 if (dev->power.irq_safe)
627 return 0;
628
c6d22b37 629 mutex_lock(&genpd->lock);
f721889f
RW
630 genpd->in_progress++;
631 pm_genpd_poweroff(genpd);
632 genpd->in_progress--;
c6d22b37 633 mutex_unlock(&genpd->lock);
f721889f
RW
634
635 return 0;
636}
637
f721889f
RW
638/**
639 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
640 * @dev: Device to resume.
641 *
642 * Carry out a runtime resume of a device under the assumption that its
643 * pm_domain field points to the domain member of an object of type
644 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
645 */
646static int pm_genpd_runtime_resume(struct device *dev)
647{
648 struct generic_pm_domain *genpd;
c6d22b37 649 DEFINE_WAIT(wait);
f721889f
RW
650 int ret;
651
652 dev_dbg(dev, "%s()\n", __func__);
653
5248051b
RW
654 genpd = dev_to_genpd(dev);
655 if (IS_ERR(genpd))
f721889f
RW
656 return -EINVAL;
657
0aa2a221
RW
658 /* If power.irq_safe, the PM domain is never powered off. */
659 if (dev->power.irq_safe)
e2e3e4e5 660 return genpd_start_dev_no_timing(genpd, dev);
0aa2a221 661
c6d22b37 662 mutex_lock(&genpd->lock);
3f241775
RW
663 ret = __pm_genpd_poweron(genpd);
664 if (ret) {
665 mutex_unlock(&genpd->lock);
666 return ret;
667 }
17b75eca 668 genpd->status = GPD_STATE_BUSY;
c6d22b37
RW
669 genpd->resume_count++;
670 for (;;) {
671 prepare_to_wait(&genpd->status_wait_queue, &wait,
672 TASK_UNINTERRUPTIBLE);
673 /*
674 * If current is the powering off task, we have been called
675 * reentrantly from one of the device callbacks, so we should
676 * not wait.
677 */
678 if (!genpd->poweroff_task || genpd->poweroff_task == current)
679 break;
680 mutex_unlock(&genpd->lock);
681
682 schedule();
683
684 mutex_lock(&genpd->lock);
685 }
686 finish_wait(&genpd->status_wait_queue, &wait);
cd0ea672 687 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
c6d22b37
RW
688 genpd->resume_count--;
689 genpd_set_active(genpd);
17b75eca 690 wake_up_all(&genpd->status_wait_queue);
c6d22b37 691 mutex_unlock(&genpd->lock);
17b75eca 692
f721889f
RW
693 return 0;
694}
695
39ac5ba5
TB
696static bool pd_ignore_unused;
697static int __init pd_ignore_unused_setup(char *__unused)
698{
699 pd_ignore_unused = true;
700 return 1;
701}
702__setup("pd_ignore_unused", pd_ignore_unused_setup);
703
17f2ae7f
RW
704/**
705 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
706 */
707void pm_genpd_poweroff_unused(void)
708{
709 struct generic_pm_domain *genpd;
710
39ac5ba5
TB
711 if (pd_ignore_unused) {
712 pr_warn("genpd: Not disabling unused power domains\n");
713 return;
714 }
715
17f2ae7f
RW
716 mutex_lock(&gpd_list_lock);
717
718 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
719 genpd_queue_power_off_work(genpd);
720
721 mutex_unlock(&gpd_list_lock);
722}
723
2fe71dcd
UH
724static int __init genpd_poweroff_unused(void)
725{
726 pm_genpd_poweroff_unused();
727 return 0;
728}
729late_initcall(genpd_poweroff_unused);
730
f721889f
RW
731#else
732
6ff7bb0d
RW
733static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
734 unsigned long val, void *ptr)
735{
736 return NOTIFY_DONE;
737}
738
d971f0b0
UH
739static inline void
740genpd_queue_power_off_work(struct generic_pm_domain *genpd) {}
741
f721889f
RW
742static inline void genpd_power_off_work_fn(struct work_struct *work) {}
743
744#define pm_genpd_runtime_suspend NULL
745#define pm_genpd_runtime_resume NULL
746
747#endif /* CONFIG_PM_RUNTIME */
748
596ba34b
RW
749#ifdef CONFIG_PM_SLEEP
750
77f827de
RW
751/**
752 * pm_genpd_present - Check if the given PM domain has been initialized.
753 * @genpd: PM domain to check.
754 */
755static bool pm_genpd_present(struct generic_pm_domain *genpd)
756{
757 struct generic_pm_domain *gpd;
758
759 if (IS_ERR_OR_NULL(genpd))
760 return false;
761
762 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
763 if (gpd == genpd)
764 return true;
765
766 return false;
767}
768
d5e4cbfe
RW
769static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
770 struct device *dev)
771{
772 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
773}
774
596ba34b 775/**
5063ce15 776 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
596ba34b
RW
777 * @genpd: PM domain to power off, if possible.
778 *
779 * Check if the given PM domain can be powered off (during system suspend or
5063ce15 780 * hibernation) and do that if so. Also, in that case propagate to its masters.
596ba34b 781 *
77f827de
RW
782 * This function is only called in "noirq" and "syscore" stages of system power
783 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
784 * executed sequentially, so it is guaranteed that it will never run twice in
785 * parallel).
596ba34b
RW
786 */
787static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
788{
5063ce15 789 struct gpd_link *link;
596ba34b 790
17b75eca 791 if (genpd->status == GPD_STATE_POWER_OFF)
596ba34b
RW
792 return;
793
c4bb3160
RW
794 if (genpd->suspended_count != genpd->device_count
795 || atomic_read(&genpd->sd_count) > 0)
596ba34b
RW
796 return;
797
798 if (genpd->power_off)
799 genpd->power_off(genpd);
800
17b75eca 801 genpd->status = GPD_STATE_POWER_OFF;
5063ce15
RW
802
803 list_for_each_entry(link, &genpd->slave_links, slave_node) {
804 genpd_sd_counter_dec(link->master);
805 pm_genpd_sync_poweroff(link->master);
596ba34b
RW
806 }
807}
808
802d8b49
RW
809/**
810 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
811 * @genpd: PM domain to power on.
812 *
77f827de
RW
813 * This function is only called in "noirq" and "syscore" stages of system power
814 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
815 * executed sequentially, so it is guaranteed that it will never run twice in
816 * parallel).
802d8b49
RW
817 */
818static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
819{
820 struct gpd_link *link;
821
822 if (genpd->status != GPD_STATE_POWER_OFF)
823 return;
824
825 list_for_each_entry(link, &genpd->slave_links, slave_node) {
826 pm_genpd_sync_poweron(link->master);
827 genpd_sd_counter_inc(link->master);
828 }
829
830 if (genpd->power_on)
831 genpd->power_on(genpd);
832
833 genpd->status = GPD_STATE_ACTIVE;
834}
835
4ecd6e65
RW
836/**
837 * resume_needed - Check whether to resume a device before system suspend.
838 * @dev: Device to check.
839 * @genpd: PM domain the device belongs to.
840 *
841 * There are two cases in which a device that can wake up the system from sleep
842 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
843 * to wake up the system and it has to remain active for this purpose while the
844 * system is in the sleep state and (2) if the device is not enabled to wake up
845 * the system from sleep states and it generally doesn't generate wakeup signals
846 * by itself (those signals are generated on its behalf by other parts of the
847 * system). In the latter case it may be necessary to reconfigure the device's
848 * wakeup settings during system suspend, because it may have been set up to
849 * signal remote wakeup from the system's working state as needed by runtime PM.
850 * Return 'true' in either of the above cases.
851 */
852static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
853{
854 bool active_wakeup;
855
856 if (!device_can_wakeup(dev))
857 return false;
858
d5e4cbfe 859 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
4ecd6e65
RW
860 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
861}
862
596ba34b
RW
863/**
864 * pm_genpd_prepare - Start power transition of a device in a PM domain.
865 * @dev: Device to start the transition of.
866 *
867 * Start a power transition of a device (during a system-wide power transition)
868 * under the assumption that its pm_domain field points to the domain member of
869 * an object of type struct generic_pm_domain representing a PM domain
870 * consisting of I/O devices.
871 */
872static int pm_genpd_prepare(struct device *dev)
873{
874 struct generic_pm_domain *genpd;
b6c10c84 875 int ret;
596ba34b
RW
876
877 dev_dbg(dev, "%s()\n", __func__);
878
879 genpd = dev_to_genpd(dev);
880 if (IS_ERR(genpd))
881 return -EINVAL;
882
17b75eca
RW
883 /*
884 * If a wakeup request is pending for the device, it should be woken up
885 * at this point and a system wakeup event should be reported if it's
886 * set up to wake up the system from sleep states.
887 */
888 pm_runtime_get_noresume(dev);
889 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
890 pm_wakeup_event(dev, 0);
891
892 if (pm_wakeup_pending()) {
84167035 893 pm_runtime_put(dev);
17b75eca
RW
894 return -EBUSY;
895 }
896
4ecd6e65
RW
897 if (resume_needed(dev, genpd))
898 pm_runtime_resume(dev);
899
17b75eca 900 genpd_acquire_lock(genpd);
596ba34b 901
65533bbf
RW
902 if (genpd->prepared_count++ == 0) {
903 genpd->suspended_count = 0;
17b75eca 904 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
65533bbf 905 }
17b75eca
RW
906
907 genpd_release_lock(genpd);
596ba34b
RW
908
909 if (genpd->suspend_power_off) {
17b75eca 910 pm_runtime_put_noidle(dev);
596ba34b
RW
911 return 0;
912 }
913
914 /*
17b75eca
RW
915 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
916 * so pm_genpd_poweron() will return immediately, but if the device
d5e4cbfe 917 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
17b75eca 918 * to make it operational.
596ba34b 919 */
17b75eca 920 pm_runtime_resume(dev);
596ba34b
RW
921 __pm_runtime_disable(dev, false);
922
b6c10c84
RW
923 ret = pm_generic_prepare(dev);
924 if (ret) {
925 mutex_lock(&genpd->lock);
926
927 if (--genpd->prepared_count == 0)
928 genpd->suspend_power_off = false;
929
930 mutex_unlock(&genpd->lock);
17b75eca 931 pm_runtime_enable(dev);
b6c10c84 932 }
17b75eca 933
84167035 934 pm_runtime_put(dev);
b6c10c84 935 return ret;
596ba34b
RW
936}
937
938/**
939 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
940 * @dev: Device to suspend.
941 *
942 * Suspend a device under the assumption that its pm_domain field points to the
943 * domain member of an object of type struct generic_pm_domain representing
944 * a PM domain consisting of I/O devices.
945 */
946static int pm_genpd_suspend(struct device *dev)
947{
948 struct generic_pm_domain *genpd;
949
950 dev_dbg(dev, "%s()\n", __func__);
951
952 genpd = dev_to_genpd(dev);
953 if (IS_ERR(genpd))
954 return -EINVAL;
955
1e0407ca 956 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
596ba34b
RW
957}
958
959/**
0496c8ae 960 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
596ba34b
RW
961 * @dev: Device to suspend.
962 *
963 * Carry out a late suspend of a device under the assumption that its
964 * pm_domain field points to the domain member of an object of type
965 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
966 */
0496c8ae 967static int pm_genpd_suspend_late(struct device *dev)
596ba34b
RW
968{
969 struct generic_pm_domain *genpd;
596ba34b
RW
970
971 dev_dbg(dev, "%s()\n", __func__);
972
973 genpd = dev_to_genpd(dev);
974 if (IS_ERR(genpd))
975 return -EINVAL;
976
1e0407ca 977 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
0496c8ae 978}
596ba34b 979
0496c8ae
RW
980/**
981 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
982 * @dev: Device to suspend.
983 *
984 * Stop the device and remove power from the domain if all devices in it have
985 * been stopped.
986 */
987static int pm_genpd_suspend_noirq(struct device *dev)
988{
989 struct generic_pm_domain *genpd;
990
991 dev_dbg(dev, "%s()\n", __func__);
992
993 genpd = dev_to_genpd(dev);
994 if (IS_ERR(genpd))
995 return -EINVAL;
596ba34b 996
dbf37414 997 if (genpd->suspend_power_off
0496c8ae 998 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
d4f2d87a
RW
999 return 0;
1000
d5e4cbfe 1001 genpd_stop_dev(genpd, dev);
596ba34b
RW
1002
1003 /*
1004 * Since all of the "noirq" callbacks are executed sequentially, it is
1005 * guaranteed that this function will never run twice in parallel for
1006 * the same PM domain, so it is not necessary to use locking here.
1007 */
1008 genpd->suspended_count++;
1009 pm_genpd_sync_poweroff(genpd);
1010
1011 return 0;
1012}
1013
1014/**
0496c8ae 1015 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
596ba34b
RW
1016 * @dev: Device to resume.
1017 *
0496c8ae 1018 * Restore power to the device's PM domain, if necessary, and start the device.
596ba34b
RW
1019 */
1020static int pm_genpd_resume_noirq(struct device *dev)
1021{
1022 struct generic_pm_domain *genpd;
1023
1024 dev_dbg(dev, "%s()\n", __func__);
1025
1026 genpd = dev_to_genpd(dev);
1027 if (IS_ERR(genpd))
1028 return -EINVAL;
1029
dbf37414 1030 if (genpd->suspend_power_off
cc85b207 1031 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
596ba34b
RW
1032 return 0;
1033
1034 /*
1035 * Since all of the "noirq" callbacks are executed sequentially, it is
1036 * guaranteed that this function will never run twice in parallel for
1037 * the same PM domain, so it is not necessary to use locking here.
1038 */
802d8b49 1039 pm_genpd_sync_poweron(genpd);
596ba34b 1040 genpd->suspended_count--;
596ba34b 1041
0496c8ae 1042 return genpd_start_dev(genpd, dev);
596ba34b
RW
1043}
1044
1045/**
0496c8ae
RW
1046 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1047 * @dev: Device to resume.
1048 *
1049 * Carry out an early resume of a device under the assumption that its
1050 * pm_domain field points to the domain member of an object of type
1051 * struct generic_pm_domain representing a power domain consisting of I/O
1052 * devices.
1053 */
1054static int pm_genpd_resume_early(struct device *dev)
1055{
1056 struct generic_pm_domain *genpd;
1057
1058 dev_dbg(dev, "%s()\n", __func__);
1059
1060 genpd = dev_to_genpd(dev);
1061 if (IS_ERR(genpd))
1062 return -EINVAL;
1063
1e0407ca 1064 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
0496c8ae
RW
1065}
1066
1067/**
1068 * pm_genpd_resume - Resume of device in an I/O PM domain.
596ba34b
RW
1069 * @dev: Device to resume.
1070 *
1071 * Resume a device under the assumption that its pm_domain field points to the
1072 * domain member of an object of type struct generic_pm_domain representing
1073 * a power domain consisting of I/O devices.
1074 */
1075static int pm_genpd_resume(struct device *dev)
1076{
1077 struct generic_pm_domain *genpd;
1078
1079 dev_dbg(dev, "%s()\n", __func__);
1080
1081 genpd = dev_to_genpd(dev);
1082 if (IS_ERR(genpd))
1083 return -EINVAL;
1084
1e0407ca 1085 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
596ba34b
RW
1086}
1087
1088/**
0496c8ae 1089 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
596ba34b
RW
1090 * @dev: Device to freeze.
1091 *
1092 * Freeze a device under the assumption that its pm_domain field points to the
1093 * domain member of an object of type struct generic_pm_domain representing
1094 * a power domain consisting of I/O devices.
1095 */
1096static int pm_genpd_freeze(struct device *dev)
1097{
1098 struct generic_pm_domain *genpd;
1099
1100 dev_dbg(dev, "%s()\n", __func__);
1101
1102 genpd = dev_to_genpd(dev);
1103 if (IS_ERR(genpd))
1104 return -EINVAL;
1105
1e0407ca 1106 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
596ba34b
RW
1107}
1108
1109/**
0496c8ae
RW
1110 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1111 * @dev: Device to freeze.
1112 *
1113 * Carry out a late freeze of a device under the assumption that its
1114 * pm_domain field points to the domain member of an object of type
1115 * struct generic_pm_domain representing a power domain consisting of I/O
1116 * devices.
1117 */
1118static int pm_genpd_freeze_late(struct device *dev)
1119{
1120 struct generic_pm_domain *genpd;
1121
1122 dev_dbg(dev, "%s()\n", __func__);
1123
1124 genpd = dev_to_genpd(dev);
1125 if (IS_ERR(genpd))
1126 return -EINVAL;
1127
1e0407ca 1128 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
0496c8ae
RW
1129}
1130
1131/**
1132 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
596ba34b
RW
1133 * @dev: Device to freeze.
1134 *
1135 * Carry out a late freeze of a device under the assumption that its
1136 * pm_domain field points to the domain member of an object of type
1137 * struct generic_pm_domain representing a power domain consisting of I/O
1138 * devices.
1139 */
1140static int pm_genpd_freeze_noirq(struct device *dev)
1141{
1142 struct generic_pm_domain *genpd;
596ba34b
RW
1143
1144 dev_dbg(dev, "%s()\n", __func__);
1145
1146 genpd = dev_to_genpd(dev);
1147 if (IS_ERR(genpd))
1148 return -EINVAL;
1149
dbf37414 1150 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
0496c8ae 1151}
596ba34b 1152
0496c8ae
RW
1153/**
1154 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1155 * @dev: Device to thaw.
1156 *
1157 * Start the device, unless power has been removed from the domain already
1158 * before the system transition.
1159 */
1160static int pm_genpd_thaw_noirq(struct device *dev)
1161{
1162 struct generic_pm_domain *genpd;
596ba34b 1163
0496c8ae 1164 dev_dbg(dev, "%s()\n", __func__);
596ba34b 1165
0496c8ae
RW
1166 genpd = dev_to_genpd(dev);
1167 if (IS_ERR(genpd))
1168 return -EINVAL;
1169
dbf37414 1170 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
596ba34b
RW
1171}
1172
1173/**
0496c8ae 1174 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
596ba34b
RW
1175 * @dev: Device to thaw.
1176 *
1177 * Carry out an early thaw of a device under the assumption that its
1178 * pm_domain field points to the domain member of an object of type
1179 * struct generic_pm_domain representing a power domain consisting of I/O
1180 * devices.
1181 */
0496c8ae 1182static int pm_genpd_thaw_early(struct device *dev)
596ba34b
RW
1183{
1184 struct generic_pm_domain *genpd;
1185
1186 dev_dbg(dev, "%s()\n", __func__);
1187
1188 genpd = dev_to_genpd(dev);
1189 if (IS_ERR(genpd))
1190 return -EINVAL;
1191
1e0407ca 1192 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
596ba34b
RW
1193}
1194
1195/**
1196 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1197 * @dev: Device to thaw.
1198 *
1199 * Thaw a device under the assumption that its pm_domain field points to the
1200 * domain member of an object of type struct generic_pm_domain representing
1201 * a power domain consisting of I/O devices.
1202 */
1203static int pm_genpd_thaw(struct device *dev)
1204{
1205 struct generic_pm_domain *genpd;
1206
1207 dev_dbg(dev, "%s()\n", __func__);
1208
1209 genpd = dev_to_genpd(dev);
1210 if (IS_ERR(genpd))
1211 return -EINVAL;
1212
1e0407ca 1213 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
596ba34b
RW
1214}
1215
1216/**
0496c8ae 1217 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
596ba34b
RW
1218 * @dev: Device to resume.
1219 *
0496c8ae
RW
1220 * Make sure the domain will be in the same power state as before the
1221 * hibernation the system is resuming from and start the device if necessary.
596ba34b
RW
1222 */
1223static int pm_genpd_restore_noirq(struct device *dev)
1224{
1225 struct generic_pm_domain *genpd;
1226
1227 dev_dbg(dev, "%s()\n", __func__);
1228
1229 genpd = dev_to_genpd(dev);
1230 if (IS_ERR(genpd))
1231 return -EINVAL;
1232
1233 /*
1234 * Since all of the "noirq" callbacks are executed sequentially, it is
1235 * guaranteed that this function will never run twice in parallel for
1236 * the same PM domain, so it is not necessary to use locking here.
65533bbf
RW
1237 *
1238 * At this point suspended_count == 0 means we are being run for the
1239 * first time for the given domain in the present cycle.
596ba34b 1240 */
65533bbf 1241 if (genpd->suspended_count++ == 0) {
596ba34b 1242 /*
65533bbf 1243 * The boot kernel might put the domain into arbitrary state,
802d8b49
RW
1244 * so make it appear as powered off to pm_genpd_sync_poweron(),
1245 * so that it tries to power it on in case it was really off.
596ba34b 1246 */
65533bbf
RW
1247 genpd->status = GPD_STATE_POWER_OFF;
1248 if (genpd->suspend_power_off) {
1249 /*
1250 * If the domain was off before the hibernation, make
1251 * sure it will be off going forward.
1252 */
1253 if (genpd->power_off)
1254 genpd->power_off(genpd);
1255
1256 return 0;
1257 }
596ba34b
RW
1258 }
1259
18dd2ece
RW
1260 if (genpd->suspend_power_off)
1261 return 0;
1262
802d8b49 1263 pm_genpd_sync_poweron(genpd);
596ba34b 1264
dbf37414 1265 return genpd_start_dev(genpd, dev);
596ba34b
RW
1266}
1267
1268/**
1269 * pm_genpd_complete - Complete power transition of a device in a power domain.
1270 * @dev: Device to complete the transition of.
1271 *
1272 * Complete a power transition of a device (during a system-wide power
1273 * transition) under the assumption that its pm_domain field points to the
1274 * domain member of an object of type struct generic_pm_domain representing
1275 * a power domain consisting of I/O devices.
1276 */
1277static void pm_genpd_complete(struct device *dev)
1278{
1279 struct generic_pm_domain *genpd;
1280 bool run_complete;
1281
1282 dev_dbg(dev, "%s()\n", __func__);
1283
1284 genpd = dev_to_genpd(dev);
1285 if (IS_ERR(genpd))
1286 return;
1287
1288 mutex_lock(&genpd->lock);
1289
1290 run_complete = !genpd->suspend_power_off;
1291 if (--genpd->prepared_count == 0)
1292 genpd->suspend_power_off = false;
1293
1294 mutex_unlock(&genpd->lock);
1295
1296 if (run_complete) {
1297 pm_generic_complete(dev);
6f00ff78 1298 pm_runtime_set_active(dev);
596ba34b 1299 pm_runtime_enable(dev);
af939339 1300 pm_request_idle(dev);
596ba34b
RW
1301 }
1302}
1303
77f827de 1304/**
d47e6464 1305 * genpd_syscore_switch - Switch power during system core suspend or resume.
77f827de
RW
1306 * @dev: Device that normally is marked as "always on" to switch power for.
1307 *
1308 * This routine may only be called during the system core (syscore) suspend or
1309 * resume phase for devices whose "always on" flags are set.
1310 */
d47e6464 1311static void genpd_syscore_switch(struct device *dev, bool suspend)
77f827de
RW
1312{
1313 struct generic_pm_domain *genpd;
1314
1315 genpd = dev_to_genpd(dev);
1316 if (!pm_genpd_present(genpd))
1317 return;
1318
1319 if (suspend) {
1320 genpd->suspended_count++;
1321 pm_genpd_sync_poweroff(genpd);
1322 } else {
1323 pm_genpd_sync_poweron(genpd);
1324 genpd->suspended_count--;
1325 }
1326}
d47e6464
UH
1327
1328void pm_genpd_syscore_poweroff(struct device *dev)
1329{
1330 genpd_syscore_switch(dev, true);
1331}
1332EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1333
1334void pm_genpd_syscore_poweron(struct device *dev)
1335{
1336 genpd_syscore_switch(dev, false);
1337}
1338EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
77f827de 1339
596ba34b
RW
1340#else
1341
1342#define pm_genpd_prepare NULL
1343#define pm_genpd_suspend NULL
0496c8ae 1344#define pm_genpd_suspend_late NULL
596ba34b 1345#define pm_genpd_suspend_noirq NULL
0496c8ae 1346#define pm_genpd_resume_early NULL
596ba34b
RW
1347#define pm_genpd_resume_noirq NULL
1348#define pm_genpd_resume NULL
1349#define pm_genpd_freeze NULL
0496c8ae 1350#define pm_genpd_freeze_late NULL
596ba34b 1351#define pm_genpd_freeze_noirq NULL
0496c8ae 1352#define pm_genpd_thaw_early NULL
596ba34b
RW
1353#define pm_genpd_thaw_noirq NULL
1354#define pm_genpd_thaw NULL
596ba34b 1355#define pm_genpd_restore_noirq NULL
596ba34b
RW
1356#define pm_genpd_complete NULL
1357
1358#endif /* CONFIG_PM_SLEEP */
1359
1d5fcfec
RW
1360static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1361{
1362 struct generic_pm_domain_data *gpd_data;
1363
1364 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1365 if (!gpd_data)
1366 return NULL;
1367
1368 mutex_init(&gpd_data->lock);
1369 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1370 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1371 return gpd_data;
1372}
1373
1374static void __pm_genpd_free_dev_data(struct device *dev,
1375 struct generic_pm_domain_data *gpd_data)
1376{
1377 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1378 kfree(gpd_data);
1379}
1380
f721889f 1381/**
b02c999a 1382 * __pm_genpd_add_device - Add a device to an I/O PM domain.
f721889f
RW
1383 * @genpd: PM domain to add the device to.
1384 * @dev: Device to be added.
b02c999a 1385 * @td: Set of PM QoS timing parameters to attach to the device.
f721889f 1386 */
b02c999a
RW
1387int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1388 struct gpd_timing_data *td)
f721889f 1389{
1d5fcfec 1390 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
4605ab65 1391 struct pm_domain_data *pdd;
f721889f
RW
1392 int ret = 0;
1393
1394 dev_dbg(dev, "%s()\n", __func__);
1395
1396 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1397 return -EINVAL;
1398
1d5fcfec
RW
1399 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1400 if (!gpd_data_new)
6ff7bb0d
RW
1401 return -ENOMEM;
1402
17b75eca 1403 genpd_acquire_lock(genpd);
f721889f 1404
596ba34b
RW
1405 if (genpd->prepared_count > 0) {
1406 ret = -EAGAIN;
1407 goto out;
1408 }
1409
4605ab65
RW
1410 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1411 if (pdd->dev == dev) {
f721889f
RW
1412 ret = -EINVAL;
1413 goto out;
1414 }
1415
1d5fcfec
RW
1416 ret = dev_pm_get_subsys_data(dev);
1417 if (ret)
1418 goto out;
1419
596ba34b 1420 genpd->device_count++;
6ff7bb0d 1421 genpd->max_off_time_changed = true;
f721889f 1422
6ff7bb0d 1423 spin_lock_irq(&dev->power.lock);
1d5fcfec 1424
6ff7bb0d 1425 dev->pm_domain = &genpd->domain;
1d5fcfec
RW
1426 if (dev->power.subsys_data->domain_data) {
1427 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1428 } else {
1429 gpd_data = gpd_data_new;
1430 dev->power.subsys_data->domain_data = &gpd_data->base;
1431 }
1432 gpd_data->refcount++;
b02c999a
RW
1433 if (td)
1434 gpd_data->td = *td;
f721889f 1435
1d5fcfec
RW
1436 spin_unlock_irq(&dev->power.lock);
1437
1438 mutex_lock(&gpd_data->lock);
1439 gpd_data->base.dev = dev;
1440 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1441 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
6ff7bb0d
RW
1442 gpd_data->td.constraint_changed = true;
1443 gpd_data->td.effective_constraint_ns = -1;
6ff7bb0d
RW
1444 mutex_unlock(&gpd_data->lock);
1445
f721889f 1446 out:
17b75eca 1447 genpd_release_lock(genpd);
f721889f 1448
1d5fcfec
RW
1449 if (gpd_data != gpd_data_new)
1450 __pm_genpd_free_dev_data(dev, gpd_data_new);
1451
f721889f
RW
1452 return ret;
1453}
1454
c8aa130b
TA
1455/**
1456 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1457 * @genpd_node: Device tree node pointer representing a PM domain to which the
1458 * the device is added to.
1459 * @dev: Device to be added.
1460 * @td: Set of PM QoS timing parameters to attach to the device.
1461 */
1462int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1463 struct gpd_timing_data *td)
1464{
1465 struct generic_pm_domain *genpd = NULL, *gpd;
1466
1467 dev_dbg(dev, "%s()\n", __func__);
1468
1469 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1470 return -EINVAL;
1471
1472 mutex_lock(&gpd_list_lock);
1473 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1474 if (gpd->of_node == genpd_node) {
1475 genpd = gpd;
1476 break;
1477 }
1478 }
1479 mutex_unlock(&gpd_list_lock);
1480
1481 if (!genpd)
1482 return -EINVAL;
1483
1484 return __pm_genpd_add_device(genpd, dev, td);
1485}
1486
b5abb085
RW
1487
1488/**
1489 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1490 * @domain_name: Name of the PM domain to add the device to.
1491 * @dev: Device to be added.
1492 * @td: Set of PM QoS timing parameters to attach to the device.
1493 */
1494int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1495 struct gpd_timing_data *td)
1496{
8bc0251d 1497 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
b5abb085
RW
1498}
1499
f721889f
RW
1500/**
1501 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1502 * @genpd: PM domain to remove the device from.
1503 * @dev: Device to be removed.
1504 */
1505int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1506 struct device *dev)
1507{
6ff7bb0d 1508 struct generic_pm_domain_data *gpd_data;
4605ab65 1509 struct pm_domain_data *pdd;
1d5fcfec 1510 bool remove = false;
efa69025 1511 int ret = 0;
f721889f
RW
1512
1513 dev_dbg(dev, "%s()\n", __func__);
1514
efa69025
RW
1515 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1516 || IS_ERR_OR_NULL(dev->pm_domain)
1517 || pd_to_genpd(dev->pm_domain) != genpd)
f721889f
RW
1518 return -EINVAL;
1519
17b75eca 1520 genpd_acquire_lock(genpd);
f721889f 1521
596ba34b
RW
1522 if (genpd->prepared_count > 0) {
1523 ret = -EAGAIN;
1524 goto out;
1525 }
1526
6ff7bb0d
RW
1527 genpd->device_count--;
1528 genpd->max_off_time_changed = true;
1529
1530 spin_lock_irq(&dev->power.lock);
1d5fcfec 1531
efa69025
RW
1532 dev->pm_domain = NULL;
1533 pdd = dev->power.subsys_data->domain_data;
1534 list_del_init(&pdd->list_node);
1d5fcfec
RW
1535 gpd_data = to_gpd_data(pdd);
1536 if (--gpd_data->refcount == 0) {
1537 dev->power.subsys_data->domain_data = NULL;
1538 remove = true;
1539 }
1540
6ff7bb0d 1541 spin_unlock_irq(&dev->power.lock);
f721889f 1542
6ff7bb0d
RW
1543 mutex_lock(&gpd_data->lock);
1544 pdd->dev = NULL;
1545 mutex_unlock(&gpd_data->lock);
1546
1547 genpd_release_lock(genpd);
1548
6ff7bb0d 1549 dev_pm_put_subsys_data(dev);
1d5fcfec
RW
1550 if (remove)
1551 __pm_genpd_free_dev_data(dev, gpd_data);
1552
6ff7bb0d 1553 return 0;
f721889f 1554
596ba34b 1555 out:
17b75eca 1556 genpd_release_lock(genpd);
f721889f
RW
1557
1558 return ret;
1559}
1560
ca1d72f0
RW
1561/**
1562 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1563 * @dev: Device to set/unset the flag for.
1564 * @val: The new value of the device's "need restore" flag.
1565 */
1566void pm_genpd_dev_need_restore(struct device *dev, bool val)
1567{
1568 struct pm_subsys_data *psd;
1569 unsigned long flags;
1570
1571 spin_lock_irqsave(&dev->power.lock, flags);
1572
1573 psd = dev_to_psd(dev);
1574 if (psd && psd->domain_data)
1575 to_gpd_data(psd->domain_data)->need_restore = val;
1576
1577 spin_unlock_irqrestore(&dev->power.lock, flags);
1578}
1579EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1580
f721889f
RW
1581/**
1582 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1583 * @genpd: Master PM domain to add the subdomain to.
bc0403ff 1584 * @subdomain: Subdomain to be added.
f721889f
RW
1585 */
1586int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
bc0403ff 1587 struct generic_pm_domain *subdomain)
f721889f 1588{
5063ce15 1589 struct gpd_link *link;
f721889f
RW
1590 int ret = 0;
1591
fb7268be
RW
1592 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1593 || genpd == subdomain)
f721889f
RW
1594 return -EINVAL;
1595
17b75eca
RW
1596 start:
1597 genpd_acquire_lock(genpd);
bc0403ff 1598 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
f721889f 1599
bc0403ff
RW
1600 if (subdomain->status != GPD_STATE_POWER_OFF
1601 && subdomain->status != GPD_STATE_ACTIVE) {
1602 mutex_unlock(&subdomain->lock);
17b75eca
RW
1603 genpd_release_lock(genpd);
1604 goto start;
1605 }
1606
1607 if (genpd->status == GPD_STATE_POWER_OFF
bc0403ff 1608 && subdomain->status != GPD_STATE_POWER_OFF) {
f721889f
RW
1609 ret = -EINVAL;
1610 goto out;
1611 }
1612
4fcac10d 1613 list_for_each_entry(link, &genpd->master_links, master_node) {
bc0403ff 1614 if (link->slave == subdomain && link->master == genpd) {
f721889f
RW
1615 ret = -EINVAL;
1616 goto out;
1617 }
1618 }
1619
5063ce15
RW
1620 link = kzalloc(sizeof(*link), GFP_KERNEL);
1621 if (!link) {
1622 ret = -ENOMEM;
1623 goto out;
1624 }
1625 link->master = genpd;
1626 list_add_tail(&link->master_node, &genpd->master_links);
bc0403ff
RW
1627 link->slave = subdomain;
1628 list_add_tail(&link->slave_node, &subdomain->slave_links);
1629 if (subdomain->status != GPD_STATE_POWER_OFF)
c4bb3160 1630 genpd_sd_counter_inc(genpd);
f721889f 1631
f721889f 1632 out:
bc0403ff 1633 mutex_unlock(&subdomain->lock);
17b75eca 1634 genpd_release_lock(genpd);
f721889f
RW
1635
1636 return ret;
1637}
1638
fb7268be
RW
1639/**
1640 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1641 * @master_name: Name of the master PM domain to add the subdomain to.
1642 * @subdomain_name: Name of the subdomain to be added.
1643 */
1644int pm_genpd_add_subdomain_names(const char *master_name,
1645 const char *subdomain_name)
1646{
1647 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1648
1649 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1650 return -EINVAL;
1651
1652 mutex_lock(&gpd_list_lock);
1653 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1654 if (!master && !strcmp(gpd->name, master_name))
1655 master = gpd;
1656
1657 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1658 subdomain = gpd;
1659
1660 if (master && subdomain)
1661 break;
1662 }
1663 mutex_unlock(&gpd_list_lock);
1664
1665 return pm_genpd_add_subdomain(master, subdomain);
1666}
1667
f721889f
RW
1668/**
1669 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1670 * @genpd: Master PM domain to remove the subdomain from.
5063ce15 1671 * @subdomain: Subdomain to be removed.
f721889f
RW
1672 */
1673int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
5063ce15 1674 struct generic_pm_domain *subdomain)
f721889f 1675{
5063ce15 1676 struct gpd_link *link;
f721889f
RW
1677 int ret = -EINVAL;
1678
5063ce15 1679 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
f721889f
RW
1680 return -EINVAL;
1681
17b75eca
RW
1682 start:
1683 genpd_acquire_lock(genpd);
f721889f 1684
5063ce15
RW
1685 list_for_each_entry(link, &genpd->master_links, master_node) {
1686 if (link->slave != subdomain)
f721889f
RW
1687 continue;
1688
1689 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1690
17b75eca
RW
1691 if (subdomain->status != GPD_STATE_POWER_OFF
1692 && subdomain->status != GPD_STATE_ACTIVE) {
1693 mutex_unlock(&subdomain->lock);
1694 genpd_release_lock(genpd);
1695 goto start;
1696 }
1697
5063ce15
RW
1698 list_del(&link->master_node);
1699 list_del(&link->slave_node);
1700 kfree(link);
17b75eca 1701 if (subdomain->status != GPD_STATE_POWER_OFF)
f721889f
RW
1702 genpd_sd_counter_dec(genpd);
1703
1704 mutex_unlock(&subdomain->lock);
1705
1706 ret = 0;
1707 break;
1708 }
1709
17b75eca 1710 genpd_release_lock(genpd);
f721889f
RW
1711
1712 return ret;
1713}
1714
40114447
RW
1715/**
1716 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1717 * @genpd: PM domain to be connected with cpuidle.
1718 * @state: cpuidle state this domain can disable/enable.
1719 *
1720 * Make a PM domain behave as though it contained a CPU core, that is, instead
1721 * of calling its power down routine it will enable the given cpuidle state so
1722 * that the cpuidle subsystem can power it down (if possible and desirable).
1723 */
1724int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
cbc9ef02
RW
1725{
1726 struct cpuidle_driver *cpuidle_drv;
1727 struct gpd_cpu_data *cpu_data;
1728 struct cpuidle_state *idle_state;
1729 int ret = 0;
1730
1731 if (IS_ERR_OR_NULL(genpd) || state < 0)
1732 return -EINVAL;
1733
1734 genpd_acquire_lock(genpd);
1735
1736 if (genpd->cpu_data) {
1737 ret = -EEXIST;
1738 goto out;
1739 }
1740 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1741 if (!cpu_data) {
1742 ret = -ENOMEM;
1743 goto out;
1744 }
1745 cpuidle_drv = cpuidle_driver_ref();
1746 if (!cpuidle_drv) {
1747 ret = -ENODEV;
debe081a 1748 goto err_drv;
cbc9ef02
RW
1749 }
1750 if (cpuidle_drv->state_count <= state) {
1751 ret = -EINVAL;
1752 goto err;
1753 }
1754 idle_state = &cpuidle_drv->states[state];
1755 if (!idle_state->disabled) {
1756 ret = -EAGAIN;
1757 goto err;
1758 }
1759 cpu_data->idle_state = idle_state;
1760 cpu_data->saved_exit_latency = idle_state->exit_latency;
1761 genpd->cpu_data = cpu_data;
1762 genpd_recalc_cpu_exit_latency(genpd);
1763
1764 out:
1765 genpd_release_lock(genpd);
1766 return ret;
1767
1768 err:
1769 cpuidle_driver_unref();
debe081a 1770
1771 err_drv:
1772 kfree(cpu_data);
cbc9ef02
RW
1773 goto out;
1774}
1775
74a2799a
RW
1776/**
1777 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1778 * @name: Name of the domain to connect to cpuidle.
1779 * @state: cpuidle state this domain can manipulate.
1780 */
1781int pm_genpd_name_attach_cpuidle(const char *name, int state)
1782{
1783 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1784}
1785
40114447
RW
1786/**
1787 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1788 * @genpd: PM domain to remove the cpuidle connection from.
1789 *
1790 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1791 * given PM domain.
1792 */
1793int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
cbc9ef02
RW
1794{
1795 struct gpd_cpu_data *cpu_data;
1796 struct cpuidle_state *idle_state;
1797 int ret = 0;
1798
1799 if (IS_ERR_OR_NULL(genpd))
1800 return -EINVAL;
1801
1802 genpd_acquire_lock(genpd);
1803
1804 cpu_data = genpd->cpu_data;
1805 if (!cpu_data) {
1806 ret = -ENODEV;
1807 goto out;
1808 }
1809 idle_state = cpu_data->idle_state;
1810 if (!idle_state->disabled) {
1811 ret = -EAGAIN;
1812 goto out;
1813 }
1814 idle_state->exit_latency = cpu_data->saved_exit_latency;
1815 cpuidle_driver_unref();
1816 genpd->cpu_data = NULL;
1817 kfree(cpu_data);
1818
1819 out:
1820 genpd_release_lock(genpd);
1821 return ret;
1822}
1823
74a2799a
RW
1824/**
1825 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1826 * @name: Name of the domain to disconnect cpuidle from.
1827 */
1828int pm_genpd_name_detach_cpuidle(const char *name)
1829{
1830 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1831}
1832
d23b9b00
RW
1833/* Default device callbacks for generic PM domains. */
1834
ecf00475
RW
1835/**
1836 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1837 * @dev: Device to handle.
1838 */
1839static int pm_genpd_default_save_state(struct device *dev)
1840{
1841 int (*cb)(struct device *__dev);
ecf00475 1842
0b589741
RW
1843 if (dev->type && dev->type->pm)
1844 cb = dev->type->pm->runtime_suspend;
1845 else if (dev->class && dev->class->pm)
1846 cb = dev->class->pm->runtime_suspend;
1847 else if (dev->bus && dev->bus->pm)
1848 cb = dev->bus->pm->runtime_suspend;
1849 else
1850 cb = NULL;
ecf00475 1851
0b589741
RW
1852 if (!cb && dev->driver && dev->driver->pm)
1853 cb = dev->driver->pm->runtime_suspend;
1854
1855 return cb ? cb(dev) : 0;
ecf00475
RW
1856}
1857
1858/**
1859 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1860 * @dev: Device to handle.
1861 */
1862static int pm_genpd_default_restore_state(struct device *dev)
1863{
1864 int (*cb)(struct device *__dev);
ecf00475 1865
0b589741
RW
1866 if (dev->type && dev->type->pm)
1867 cb = dev->type->pm->runtime_resume;
1868 else if (dev->class && dev->class->pm)
1869 cb = dev->class->pm->runtime_resume;
1870 else if (dev->bus && dev->bus->pm)
1871 cb = dev->bus->pm->runtime_resume;
1872 else
1873 cb = NULL;
ecf00475 1874
0b589741
RW
1875 if (!cb && dev->driver && dev->driver->pm)
1876 cb = dev->driver->pm->runtime_resume;
1877
1878 return cb ? cb(dev) : 0;
ecf00475
RW
1879}
1880
f721889f
RW
1881/**
1882 * pm_genpd_init - Initialize a generic I/O PM domain object.
1883 * @genpd: PM domain object to initialize.
1884 * @gov: PM domain governor to associate with the domain (may be NULL).
1885 * @is_off: Initial value of the domain's power_is_off field.
1886 */
1887void pm_genpd_init(struct generic_pm_domain *genpd,
1888 struct dev_power_governor *gov, bool is_off)
1889{
1890 if (IS_ERR_OR_NULL(genpd))
1891 return;
1892
5063ce15
RW
1893 INIT_LIST_HEAD(&genpd->master_links);
1894 INIT_LIST_HEAD(&genpd->slave_links);
f721889f 1895 INIT_LIST_HEAD(&genpd->dev_list);
f721889f
RW
1896 mutex_init(&genpd->lock);
1897 genpd->gov = gov;
1898 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1899 genpd->in_progress = 0;
c4bb3160 1900 atomic_set(&genpd->sd_count, 0);
17b75eca
RW
1901 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1902 init_waitqueue_head(&genpd->status_wait_queue);
c6d22b37
RW
1903 genpd->poweroff_task = NULL;
1904 genpd->resume_count = 0;
596ba34b 1905 genpd->device_count = 0;
221e9b58 1906 genpd->max_off_time_ns = -1;
6ff7bb0d 1907 genpd->max_off_time_changed = true;
f721889f
RW
1908 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1909 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
596ba34b
RW
1910 genpd->domain.ops.prepare = pm_genpd_prepare;
1911 genpd->domain.ops.suspend = pm_genpd_suspend;
0496c8ae 1912 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
596ba34b
RW
1913 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1914 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
0496c8ae 1915 genpd->domain.ops.resume_early = pm_genpd_resume_early;
596ba34b
RW
1916 genpd->domain.ops.resume = pm_genpd_resume;
1917 genpd->domain.ops.freeze = pm_genpd_freeze;
0496c8ae 1918 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
596ba34b
RW
1919 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1920 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
0496c8ae 1921 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
596ba34b 1922 genpd->domain.ops.thaw = pm_genpd_thaw;
d23b9b00 1923 genpd->domain.ops.poweroff = pm_genpd_suspend;
0496c8ae 1924 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
d23b9b00 1925 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
596ba34b 1926 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
0496c8ae 1927 genpd->domain.ops.restore_early = pm_genpd_resume_early;
d23b9b00 1928 genpd->domain.ops.restore = pm_genpd_resume;
596ba34b 1929 genpd->domain.ops.complete = pm_genpd_complete;
ecf00475
RW
1930 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1931 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
5125bbf3
RW
1932 mutex_lock(&gpd_list_lock);
1933 list_add(&genpd->gpd_list_node, &gpd_list);
1934 mutex_unlock(&gpd_list_lock);
1935}
This page took 0.398773 seconds and 5 git commands to generate.