PM / domains: Remove pm_genpd_syscore_switch() API
[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 */
0bc5b2de 425void 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
f721889f
RW
724#else
725
6ff7bb0d
RW
726static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
727 unsigned long val, void *ptr)
728{
729 return NOTIFY_DONE;
730}
731
f721889f
RW
732static inline void genpd_power_off_work_fn(struct work_struct *work) {}
733
734#define pm_genpd_runtime_suspend NULL
735#define pm_genpd_runtime_resume NULL
736
737#endif /* CONFIG_PM_RUNTIME */
738
596ba34b
RW
739#ifdef CONFIG_PM_SLEEP
740
77f827de
RW
741/**
742 * pm_genpd_present - Check if the given PM domain has been initialized.
743 * @genpd: PM domain to check.
744 */
745static bool pm_genpd_present(struct generic_pm_domain *genpd)
746{
747 struct generic_pm_domain *gpd;
748
749 if (IS_ERR_OR_NULL(genpd))
750 return false;
751
752 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
753 if (gpd == genpd)
754 return true;
755
756 return false;
757}
758
d5e4cbfe
RW
759static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
760 struct device *dev)
761{
762 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
763}
764
596ba34b 765/**
5063ce15 766 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
596ba34b
RW
767 * @genpd: PM domain to power off, if possible.
768 *
769 * Check if the given PM domain can be powered off (during system suspend or
5063ce15 770 * hibernation) and do that if so. Also, in that case propagate to its masters.
596ba34b 771 *
77f827de
RW
772 * This function is only called in "noirq" and "syscore" stages of system power
773 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
774 * executed sequentially, so it is guaranteed that it will never run twice in
775 * parallel).
596ba34b
RW
776 */
777static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
778{
5063ce15 779 struct gpd_link *link;
596ba34b 780
17b75eca 781 if (genpd->status == GPD_STATE_POWER_OFF)
596ba34b
RW
782 return;
783
c4bb3160
RW
784 if (genpd->suspended_count != genpd->device_count
785 || atomic_read(&genpd->sd_count) > 0)
596ba34b
RW
786 return;
787
788 if (genpd->power_off)
789 genpd->power_off(genpd);
790
17b75eca 791 genpd->status = GPD_STATE_POWER_OFF;
5063ce15
RW
792
793 list_for_each_entry(link, &genpd->slave_links, slave_node) {
794 genpd_sd_counter_dec(link->master);
795 pm_genpd_sync_poweroff(link->master);
596ba34b
RW
796 }
797}
798
802d8b49
RW
799/**
800 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
801 * @genpd: PM domain to power on.
802 *
77f827de
RW
803 * This function is only called in "noirq" and "syscore" stages of system power
804 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
805 * executed sequentially, so it is guaranteed that it will never run twice in
806 * parallel).
802d8b49
RW
807 */
808static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
809{
810 struct gpd_link *link;
811
812 if (genpd->status != GPD_STATE_POWER_OFF)
813 return;
814
815 list_for_each_entry(link, &genpd->slave_links, slave_node) {
816 pm_genpd_sync_poweron(link->master);
817 genpd_sd_counter_inc(link->master);
818 }
819
820 if (genpd->power_on)
821 genpd->power_on(genpd);
822
823 genpd->status = GPD_STATE_ACTIVE;
824}
825
4ecd6e65
RW
826/**
827 * resume_needed - Check whether to resume a device before system suspend.
828 * @dev: Device to check.
829 * @genpd: PM domain the device belongs to.
830 *
831 * There are two cases in which a device that can wake up the system from sleep
832 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
833 * to wake up the system and it has to remain active for this purpose while the
834 * system is in the sleep state and (2) if the device is not enabled to wake up
835 * the system from sleep states and it generally doesn't generate wakeup signals
836 * by itself (those signals are generated on its behalf by other parts of the
837 * system). In the latter case it may be necessary to reconfigure the device's
838 * wakeup settings during system suspend, because it may have been set up to
839 * signal remote wakeup from the system's working state as needed by runtime PM.
840 * Return 'true' in either of the above cases.
841 */
842static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
843{
844 bool active_wakeup;
845
846 if (!device_can_wakeup(dev))
847 return false;
848
d5e4cbfe 849 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
4ecd6e65
RW
850 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
851}
852
596ba34b
RW
853/**
854 * pm_genpd_prepare - Start power transition of a device in a PM domain.
855 * @dev: Device to start the transition of.
856 *
857 * Start a power transition of a device (during a system-wide power transition)
858 * under the assumption that its pm_domain field points to the domain member of
859 * an object of type struct generic_pm_domain representing a PM domain
860 * consisting of I/O devices.
861 */
862static int pm_genpd_prepare(struct device *dev)
863{
864 struct generic_pm_domain *genpd;
b6c10c84 865 int ret;
596ba34b
RW
866
867 dev_dbg(dev, "%s()\n", __func__);
868
869 genpd = dev_to_genpd(dev);
870 if (IS_ERR(genpd))
871 return -EINVAL;
872
17b75eca
RW
873 /*
874 * If a wakeup request is pending for the device, it should be woken up
875 * at this point and a system wakeup event should be reported if it's
876 * set up to wake up the system from sleep states.
877 */
878 pm_runtime_get_noresume(dev);
879 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
880 pm_wakeup_event(dev, 0);
881
882 if (pm_wakeup_pending()) {
84167035 883 pm_runtime_put(dev);
17b75eca
RW
884 return -EBUSY;
885 }
886
4ecd6e65
RW
887 if (resume_needed(dev, genpd))
888 pm_runtime_resume(dev);
889
17b75eca 890 genpd_acquire_lock(genpd);
596ba34b 891
65533bbf
RW
892 if (genpd->prepared_count++ == 0) {
893 genpd->suspended_count = 0;
17b75eca 894 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
65533bbf 895 }
17b75eca
RW
896
897 genpd_release_lock(genpd);
596ba34b
RW
898
899 if (genpd->suspend_power_off) {
17b75eca 900 pm_runtime_put_noidle(dev);
596ba34b
RW
901 return 0;
902 }
903
904 /*
17b75eca
RW
905 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
906 * so pm_genpd_poweron() will return immediately, but if the device
d5e4cbfe 907 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
17b75eca 908 * to make it operational.
596ba34b 909 */
17b75eca 910 pm_runtime_resume(dev);
596ba34b
RW
911 __pm_runtime_disable(dev, false);
912
b6c10c84
RW
913 ret = pm_generic_prepare(dev);
914 if (ret) {
915 mutex_lock(&genpd->lock);
916
917 if (--genpd->prepared_count == 0)
918 genpd->suspend_power_off = false;
919
920 mutex_unlock(&genpd->lock);
17b75eca 921 pm_runtime_enable(dev);
b6c10c84 922 }
17b75eca 923
84167035 924 pm_runtime_put(dev);
b6c10c84 925 return ret;
596ba34b
RW
926}
927
928/**
929 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
930 * @dev: Device to suspend.
931 *
932 * Suspend a device under the assumption that its pm_domain field points to the
933 * domain member of an object of type struct generic_pm_domain representing
934 * a PM domain consisting of I/O devices.
935 */
936static int pm_genpd_suspend(struct device *dev)
937{
938 struct generic_pm_domain *genpd;
939
940 dev_dbg(dev, "%s()\n", __func__);
941
942 genpd = dev_to_genpd(dev);
943 if (IS_ERR(genpd))
944 return -EINVAL;
945
1e0407ca 946 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
596ba34b
RW
947}
948
949/**
0496c8ae 950 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
596ba34b
RW
951 * @dev: Device to suspend.
952 *
953 * Carry out a late suspend of a device under the assumption that its
954 * pm_domain field points to the domain member of an object of type
955 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
956 */
0496c8ae 957static int pm_genpd_suspend_late(struct device *dev)
596ba34b
RW
958{
959 struct generic_pm_domain *genpd;
596ba34b
RW
960
961 dev_dbg(dev, "%s()\n", __func__);
962
963 genpd = dev_to_genpd(dev);
964 if (IS_ERR(genpd))
965 return -EINVAL;
966
1e0407ca 967 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
0496c8ae 968}
596ba34b 969
0496c8ae
RW
970/**
971 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
972 * @dev: Device to suspend.
973 *
974 * Stop the device and remove power from the domain if all devices in it have
975 * been stopped.
976 */
977static int pm_genpd_suspend_noirq(struct device *dev)
978{
979 struct generic_pm_domain *genpd;
980
981 dev_dbg(dev, "%s()\n", __func__);
982
983 genpd = dev_to_genpd(dev);
984 if (IS_ERR(genpd))
985 return -EINVAL;
596ba34b 986
dbf37414 987 if (genpd->suspend_power_off
0496c8ae 988 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
d4f2d87a
RW
989 return 0;
990
d5e4cbfe 991 genpd_stop_dev(genpd, dev);
596ba34b
RW
992
993 /*
994 * Since all of the "noirq" callbacks are executed sequentially, it is
995 * guaranteed that this function will never run twice in parallel for
996 * the same PM domain, so it is not necessary to use locking here.
997 */
998 genpd->suspended_count++;
999 pm_genpd_sync_poweroff(genpd);
1000
1001 return 0;
1002}
1003
1004/**
0496c8ae 1005 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
596ba34b
RW
1006 * @dev: Device to resume.
1007 *
0496c8ae 1008 * Restore power to the device's PM domain, if necessary, and start the device.
596ba34b
RW
1009 */
1010static int pm_genpd_resume_noirq(struct device *dev)
1011{
1012 struct generic_pm_domain *genpd;
1013
1014 dev_dbg(dev, "%s()\n", __func__);
1015
1016 genpd = dev_to_genpd(dev);
1017 if (IS_ERR(genpd))
1018 return -EINVAL;
1019
dbf37414 1020 if (genpd->suspend_power_off
cc85b207 1021 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
596ba34b
RW
1022 return 0;
1023
1024 /*
1025 * Since all of the "noirq" callbacks are executed sequentially, it is
1026 * guaranteed that this function will never run twice in parallel for
1027 * the same PM domain, so it is not necessary to use locking here.
1028 */
802d8b49 1029 pm_genpd_sync_poweron(genpd);
596ba34b 1030 genpd->suspended_count--;
596ba34b 1031
0496c8ae 1032 return genpd_start_dev(genpd, dev);
596ba34b
RW
1033}
1034
1035/**
0496c8ae
RW
1036 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1037 * @dev: Device to resume.
1038 *
1039 * Carry out an early resume of a device under the assumption that its
1040 * pm_domain field points to the domain member of an object of type
1041 * struct generic_pm_domain representing a power domain consisting of I/O
1042 * devices.
1043 */
1044static int pm_genpd_resume_early(struct device *dev)
1045{
1046 struct generic_pm_domain *genpd;
1047
1048 dev_dbg(dev, "%s()\n", __func__);
1049
1050 genpd = dev_to_genpd(dev);
1051 if (IS_ERR(genpd))
1052 return -EINVAL;
1053
1e0407ca 1054 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
0496c8ae
RW
1055}
1056
1057/**
1058 * pm_genpd_resume - Resume of device in an I/O PM domain.
596ba34b
RW
1059 * @dev: Device to resume.
1060 *
1061 * Resume a device under the assumption that its pm_domain field points to the
1062 * domain member of an object of type struct generic_pm_domain representing
1063 * a power domain consisting of I/O devices.
1064 */
1065static int pm_genpd_resume(struct device *dev)
1066{
1067 struct generic_pm_domain *genpd;
1068
1069 dev_dbg(dev, "%s()\n", __func__);
1070
1071 genpd = dev_to_genpd(dev);
1072 if (IS_ERR(genpd))
1073 return -EINVAL;
1074
1e0407ca 1075 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
596ba34b
RW
1076}
1077
1078/**
0496c8ae 1079 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
596ba34b
RW
1080 * @dev: Device to freeze.
1081 *
1082 * Freeze a device under the assumption that its pm_domain field points to the
1083 * domain member of an object of type struct generic_pm_domain representing
1084 * a power domain consisting of I/O devices.
1085 */
1086static int pm_genpd_freeze(struct device *dev)
1087{
1088 struct generic_pm_domain *genpd;
1089
1090 dev_dbg(dev, "%s()\n", __func__);
1091
1092 genpd = dev_to_genpd(dev);
1093 if (IS_ERR(genpd))
1094 return -EINVAL;
1095
1e0407ca 1096 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
596ba34b
RW
1097}
1098
1099/**
0496c8ae
RW
1100 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1101 * @dev: Device to freeze.
1102 *
1103 * Carry out a late freeze of a device under the assumption that its
1104 * pm_domain field points to the domain member of an object of type
1105 * struct generic_pm_domain representing a power domain consisting of I/O
1106 * devices.
1107 */
1108static int pm_genpd_freeze_late(struct device *dev)
1109{
1110 struct generic_pm_domain *genpd;
1111
1112 dev_dbg(dev, "%s()\n", __func__);
1113
1114 genpd = dev_to_genpd(dev);
1115 if (IS_ERR(genpd))
1116 return -EINVAL;
1117
1e0407ca 1118 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
0496c8ae
RW
1119}
1120
1121/**
1122 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
596ba34b
RW
1123 * @dev: Device to freeze.
1124 *
1125 * Carry out a late freeze of a device under the assumption that its
1126 * pm_domain field points to the domain member of an object of type
1127 * struct generic_pm_domain representing a power domain consisting of I/O
1128 * devices.
1129 */
1130static int pm_genpd_freeze_noirq(struct device *dev)
1131{
1132 struct generic_pm_domain *genpd;
596ba34b
RW
1133
1134 dev_dbg(dev, "%s()\n", __func__);
1135
1136 genpd = dev_to_genpd(dev);
1137 if (IS_ERR(genpd))
1138 return -EINVAL;
1139
dbf37414 1140 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
0496c8ae 1141}
596ba34b 1142
0496c8ae
RW
1143/**
1144 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1145 * @dev: Device to thaw.
1146 *
1147 * Start the device, unless power has been removed from the domain already
1148 * before the system transition.
1149 */
1150static int pm_genpd_thaw_noirq(struct device *dev)
1151{
1152 struct generic_pm_domain *genpd;
596ba34b 1153
0496c8ae 1154 dev_dbg(dev, "%s()\n", __func__);
596ba34b 1155
0496c8ae
RW
1156 genpd = dev_to_genpd(dev);
1157 if (IS_ERR(genpd))
1158 return -EINVAL;
1159
dbf37414 1160 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
596ba34b
RW
1161}
1162
1163/**
0496c8ae 1164 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
596ba34b
RW
1165 * @dev: Device to thaw.
1166 *
1167 * Carry out an early thaw of a device under the assumption that its
1168 * pm_domain field points to the domain member of an object of type
1169 * struct generic_pm_domain representing a power domain consisting of I/O
1170 * devices.
1171 */
0496c8ae 1172static int pm_genpd_thaw_early(struct device *dev)
596ba34b
RW
1173{
1174 struct generic_pm_domain *genpd;
1175
1176 dev_dbg(dev, "%s()\n", __func__);
1177
1178 genpd = dev_to_genpd(dev);
1179 if (IS_ERR(genpd))
1180 return -EINVAL;
1181
1e0407ca 1182 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
596ba34b
RW
1183}
1184
1185/**
1186 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1187 * @dev: Device to thaw.
1188 *
1189 * Thaw a device under the assumption that its pm_domain field points to the
1190 * domain member of an object of type struct generic_pm_domain representing
1191 * a power domain consisting of I/O devices.
1192 */
1193static int pm_genpd_thaw(struct device *dev)
1194{
1195 struct generic_pm_domain *genpd;
1196
1197 dev_dbg(dev, "%s()\n", __func__);
1198
1199 genpd = dev_to_genpd(dev);
1200 if (IS_ERR(genpd))
1201 return -EINVAL;
1202
1e0407ca 1203 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
596ba34b
RW
1204}
1205
1206/**
0496c8ae 1207 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
596ba34b
RW
1208 * @dev: Device to resume.
1209 *
0496c8ae
RW
1210 * Make sure the domain will be in the same power state as before the
1211 * hibernation the system is resuming from and start the device if necessary.
596ba34b
RW
1212 */
1213static int pm_genpd_restore_noirq(struct device *dev)
1214{
1215 struct generic_pm_domain *genpd;
1216
1217 dev_dbg(dev, "%s()\n", __func__);
1218
1219 genpd = dev_to_genpd(dev);
1220 if (IS_ERR(genpd))
1221 return -EINVAL;
1222
1223 /*
1224 * Since all of the "noirq" callbacks are executed sequentially, it is
1225 * guaranteed that this function will never run twice in parallel for
1226 * the same PM domain, so it is not necessary to use locking here.
65533bbf
RW
1227 *
1228 * At this point suspended_count == 0 means we are being run for the
1229 * first time for the given domain in the present cycle.
596ba34b 1230 */
65533bbf 1231 if (genpd->suspended_count++ == 0) {
596ba34b 1232 /*
65533bbf 1233 * The boot kernel might put the domain into arbitrary state,
802d8b49
RW
1234 * so make it appear as powered off to pm_genpd_sync_poweron(),
1235 * so that it tries to power it on in case it was really off.
596ba34b 1236 */
65533bbf
RW
1237 genpd->status = GPD_STATE_POWER_OFF;
1238 if (genpd->suspend_power_off) {
1239 /*
1240 * If the domain was off before the hibernation, make
1241 * sure it will be off going forward.
1242 */
1243 if (genpd->power_off)
1244 genpd->power_off(genpd);
1245
1246 return 0;
1247 }
596ba34b
RW
1248 }
1249
18dd2ece
RW
1250 if (genpd->suspend_power_off)
1251 return 0;
1252
802d8b49 1253 pm_genpd_sync_poweron(genpd);
596ba34b 1254
dbf37414 1255 return genpd_start_dev(genpd, dev);
596ba34b
RW
1256}
1257
1258/**
1259 * pm_genpd_complete - Complete power transition of a device in a power domain.
1260 * @dev: Device to complete the transition of.
1261 *
1262 * Complete a power transition of a device (during a system-wide power
1263 * transition) under the assumption that its pm_domain field points to the
1264 * domain member of an object of type struct generic_pm_domain representing
1265 * a power domain consisting of I/O devices.
1266 */
1267static void pm_genpd_complete(struct device *dev)
1268{
1269 struct generic_pm_domain *genpd;
1270 bool run_complete;
1271
1272 dev_dbg(dev, "%s()\n", __func__);
1273
1274 genpd = dev_to_genpd(dev);
1275 if (IS_ERR(genpd))
1276 return;
1277
1278 mutex_lock(&genpd->lock);
1279
1280 run_complete = !genpd->suspend_power_off;
1281 if (--genpd->prepared_count == 0)
1282 genpd->suspend_power_off = false;
1283
1284 mutex_unlock(&genpd->lock);
1285
1286 if (run_complete) {
1287 pm_generic_complete(dev);
6f00ff78 1288 pm_runtime_set_active(dev);
596ba34b 1289 pm_runtime_enable(dev);
af939339 1290 pm_request_idle(dev);
596ba34b
RW
1291 }
1292}
1293
77f827de 1294/**
d47e6464 1295 * genpd_syscore_switch - Switch power during system core suspend or resume.
77f827de
RW
1296 * @dev: Device that normally is marked as "always on" to switch power for.
1297 *
1298 * This routine may only be called during the system core (syscore) suspend or
1299 * resume phase for devices whose "always on" flags are set.
1300 */
d47e6464 1301static void genpd_syscore_switch(struct device *dev, bool suspend)
77f827de
RW
1302{
1303 struct generic_pm_domain *genpd;
1304
1305 genpd = dev_to_genpd(dev);
1306 if (!pm_genpd_present(genpd))
1307 return;
1308
1309 if (suspend) {
1310 genpd->suspended_count++;
1311 pm_genpd_sync_poweroff(genpd);
1312 } else {
1313 pm_genpd_sync_poweron(genpd);
1314 genpd->suspended_count--;
1315 }
1316}
d47e6464
UH
1317
1318void pm_genpd_syscore_poweroff(struct device *dev)
1319{
1320 genpd_syscore_switch(dev, true);
1321}
1322EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1323
1324void pm_genpd_syscore_poweron(struct device *dev)
1325{
1326 genpd_syscore_switch(dev, false);
1327}
1328EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
77f827de 1329
596ba34b
RW
1330#else
1331
1332#define pm_genpd_prepare NULL
1333#define pm_genpd_suspend NULL
0496c8ae 1334#define pm_genpd_suspend_late NULL
596ba34b 1335#define pm_genpd_suspend_noirq NULL
0496c8ae 1336#define pm_genpd_resume_early NULL
596ba34b
RW
1337#define pm_genpd_resume_noirq NULL
1338#define pm_genpd_resume NULL
1339#define pm_genpd_freeze NULL
0496c8ae 1340#define pm_genpd_freeze_late NULL
596ba34b 1341#define pm_genpd_freeze_noirq NULL
0496c8ae 1342#define pm_genpd_thaw_early NULL
596ba34b
RW
1343#define pm_genpd_thaw_noirq NULL
1344#define pm_genpd_thaw NULL
596ba34b 1345#define pm_genpd_restore_noirq NULL
596ba34b
RW
1346#define pm_genpd_complete NULL
1347
1348#endif /* CONFIG_PM_SLEEP */
1349
1d5fcfec
RW
1350static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1351{
1352 struct generic_pm_domain_data *gpd_data;
1353
1354 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1355 if (!gpd_data)
1356 return NULL;
1357
1358 mutex_init(&gpd_data->lock);
1359 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1360 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1361 return gpd_data;
1362}
1363
1364static void __pm_genpd_free_dev_data(struct device *dev,
1365 struct generic_pm_domain_data *gpd_data)
1366{
1367 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1368 kfree(gpd_data);
1369}
1370
f721889f 1371/**
b02c999a 1372 * __pm_genpd_add_device - Add a device to an I/O PM domain.
f721889f
RW
1373 * @genpd: PM domain to add the device to.
1374 * @dev: Device to be added.
b02c999a 1375 * @td: Set of PM QoS timing parameters to attach to the device.
f721889f 1376 */
b02c999a
RW
1377int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1378 struct gpd_timing_data *td)
f721889f 1379{
1d5fcfec 1380 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
4605ab65 1381 struct pm_domain_data *pdd;
f721889f
RW
1382 int ret = 0;
1383
1384 dev_dbg(dev, "%s()\n", __func__);
1385
1386 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1387 return -EINVAL;
1388
1d5fcfec
RW
1389 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1390 if (!gpd_data_new)
6ff7bb0d
RW
1391 return -ENOMEM;
1392
17b75eca 1393 genpd_acquire_lock(genpd);
f721889f 1394
596ba34b
RW
1395 if (genpd->prepared_count > 0) {
1396 ret = -EAGAIN;
1397 goto out;
1398 }
1399
4605ab65
RW
1400 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1401 if (pdd->dev == dev) {
f721889f
RW
1402 ret = -EINVAL;
1403 goto out;
1404 }
1405
1d5fcfec
RW
1406 ret = dev_pm_get_subsys_data(dev);
1407 if (ret)
1408 goto out;
1409
596ba34b 1410 genpd->device_count++;
6ff7bb0d 1411 genpd->max_off_time_changed = true;
f721889f 1412
6ff7bb0d 1413 spin_lock_irq(&dev->power.lock);
1d5fcfec 1414
6ff7bb0d 1415 dev->pm_domain = &genpd->domain;
1d5fcfec
RW
1416 if (dev->power.subsys_data->domain_data) {
1417 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1418 } else {
1419 gpd_data = gpd_data_new;
1420 dev->power.subsys_data->domain_data = &gpd_data->base;
1421 }
1422 gpd_data->refcount++;
b02c999a
RW
1423 if (td)
1424 gpd_data->td = *td;
f721889f 1425
1d5fcfec
RW
1426 spin_unlock_irq(&dev->power.lock);
1427
1428 mutex_lock(&gpd_data->lock);
1429 gpd_data->base.dev = dev;
1430 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1431 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
6ff7bb0d
RW
1432 gpd_data->td.constraint_changed = true;
1433 gpd_data->td.effective_constraint_ns = -1;
6ff7bb0d
RW
1434 mutex_unlock(&gpd_data->lock);
1435
f721889f 1436 out:
17b75eca 1437 genpd_release_lock(genpd);
f721889f 1438
1d5fcfec
RW
1439 if (gpd_data != gpd_data_new)
1440 __pm_genpd_free_dev_data(dev, gpd_data_new);
1441
f721889f
RW
1442 return ret;
1443}
1444
c8aa130b
TA
1445/**
1446 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1447 * @genpd_node: Device tree node pointer representing a PM domain to which the
1448 * the device is added to.
1449 * @dev: Device to be added.
1450 * @td: Set of PM QoS timing parameters to attach to the device.
1451 */
1452int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1453 struct gpd_timing_data *td)
1454{
1455 struct generic_pm_domain *genpd = NULL, *gpd;
1456
1457 dev_dbg(dev, "%s()\n", __func__);
1458
1459 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1460 return -EINVAL;
1461
1462 mutex_lock(&gpd_list_lock);
1463 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1464 if (gpd->of_node == genpd_node) {
1465 genpd = gpd;
1466 break;
1467 }
1468 }
1469 mutex_unlock(&gpd_list_lock);
1470
1471 if (!genpd)
1472 return -EINVAL;
1473
1474 return __pm_genpd_add_device(genpd, dev, td);
1475}
1476
b5abb085
RW
1477
1478/**
1479 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1480 * @domain_name: Name of the PM domain to add the device to.
1481 * @dev: Device to be added.
1482 * @td: Set of PM QoS timing parameters to attach to the device.
1483 */
1484int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1485 struct gpd_timing_data *td)
1486{
8bc0251d 1487 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
b5abb085
RW
1488}
1489
f721889f
RW
1490/**
1491 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1492 * @genpd: PM domain to remove the device from.
1493 * @dev: Device to be removed.
1494 */
1495int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1496 struct device *dev)
1497{
6ff7bb0d 1498 struct generic_pm_domain_data *gpd_data;
4605ab65 1499 struct pm_domain_data *pdd;
1d5fcfec 1500 bool remove = false;
efa69025 1501 int ret = 0;
f721889f
RW
1502
1503 dev_dbg(dev, "%s()\n", __func__);
1504
efa69025
RW
1505 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1506 || IS_ERR_OR_NULL(dev->pm_domain)
1507 || pd_to_genpd(dev->pm_domain) != genpd)
f721889f
RW
1508 return -EINVAL;
1509
17b75eca 1510 genpd_acquire_lock(genpd);
f721889f 1511
596ba34b
RW
1512 if (genpd->prepared_count > 0) {
1513 ret = -EAGAIN;
1514 goto out;
1515 }
1516
6ff7bb0d
RW
1517 genpd->device_count--;
1518 genpd->max_off_time_changed = true;
1519
1520 spin_lock_irq(&dev->power.lock);
1d5fcfec 1521
efa69025
RW
1522 dev->pm_domain = NULL;
1523 pdd = dev->power.subsys_data->domain_data;
1524 list_del_init(&pdd->list_node);
1d5fcfec
RW
1525 gpd_data = to_gpd_data(pdd);
1526 if (--gpd_data->refcount == 0) {
1527 dev->power.subsys_data->domain_data = NULL;
1528 remove = true;
1529 }
1530
6ff7bb0d 1531 spin_unlock_irq(&dev->power.lock);
f721889f 1532
6ff7bb0d
RW
1533 mutex_lock(&gpd_data->lock);
1534 pdd->dev = NULL;
1535 mutex_unlock(&gpd_data->lock);
1536
1537 genpd_release_lock(genpd);
1538
6ff7bb0d 1539 dev_pm_put_subsys_data(dev);
1d5fcfec
RW
1540 if (remove)
1541 __pm_genpd_free_dev_data(dev, gpd_data);
1542
6ff7bb0d 1543 return 0;
f721889f 1544
596ba34b 1545 out:
17b75eca 1546 genpd_release_lock(genpd);
f721889f
RW
1547
1548 return ret;
1549}
1550
ca1d72f0
RW
1551/**
1552 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1553 * @dev: Device to set/unset the flag for.
1554 * @val: The new value of the device's "need restore" flag.
1555 */
1556void pm_genpd_dev_need_restore(struct device *dev, bool val)
1557{
1558 struct pm_subsys_data *psd;
1559 unsigned long flags;
1560
1561 spin_lock_irqsave(&dev->power.lock, flags);
1562
1563 psd = dev_to_psd(dev);
1564 if (psd && psd->domain_data)
1565 to_gpd_data(psd->domain_data)->need_restore = val;
1566
1567 spin_unlock_irqrestore(&dev->power.lock, flags);
1568}
1569EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1570
f721889f
RW
1571/**
1572 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1573 * @genpd: Master PM domain to add the subdomain to.
bc0403ff 1574 * @subdomain: Subdomain to be added.
f721889f
RW
1575 */
1576int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
bc0403ff 1577 struct generic_pm_domain *subdomain)
f721889f 1578{
5063ce15 1579 struct gpd_link *link;
f721889f
RW
1580 int ret = 0;
1581
fb7268be
RW
1582 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1583 || genpd == subdomain)
f721889f
RW
1584 return -EINVAL;
1585
17b75eca
RW
1586 start:
1587 genpd_acquire_lock(genpd);
bc0403ff 1588 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
f721889f 1589
bc0403ff
RW
1590 if (subdomain->status != GPD_STATE_POWER_OFF
1591 && subdomain->status != GPD_STATE_ACTIVE) {
1592 mutex_unlock(&subdomain->lock);
17b75eca
RW
1593 genpd_release_lock(genpd);
1594 goto start;
1595 }
1596
1597 if (genpd->status == GPD_STATE_POWER_OFF
bc0403ff 1598 && subdomain->status != GPD_STATE_POWER_OFF) {
f721889f
RW
1599 ret = -EINVAL;
1600 goto out;
1601 }
1602
4fcac10d 1603 list_for_each_entry(link, &genpd->master_links, master_node) {
bc0403ff 1604 if (link->slave == subdomain && link->master == genpd) {
f721889f
RW
1605 ret = -EINVAL;
1606 goto out;
1607 }
1608 }
1609
5063ce15
RW
1610 link = kzalloc(sizeof(*link), GFP_KERNEL);
1611 if (!link) {
1612 ret = -ENOMEM;
1613 goto out;
1614 }
1615 link->master = genpd;
1616 list_add_tail(&link->master_node, &genpd->master_links);
bc0403ff
RW
1617 link->slave = subdomain;
1618 list_add_tail(&link->slave_node, &subdomain->slave_links);
1619 if (subdomain->status != GPD_STATE_POWER_OFF)
c4bb3160 1620 genpd_sd_counter_inc(genpd);
f721889f 1621
f721889f 1622 out:
bc0403ff 1623 mutex_unlock(&subdomain->lock);
17b75eca 1624 genpd_release_lock(genpd);
f721889f
RW
1625
1626 return ret;
1627}
1628
fb7268be
RW
1629/**
1630 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1631 * @master_name: Name of the master PM domain to add the subdomain to.
1632 * @subdomain_name: Name of the subdomain to be added.
1633 */
1634int pm_genpd_add_subdomain_names(const char *master_name,
1635 const char *subdomain_name)
1636{
1637 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1638
1639 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1640 return -EINVAL;
1641
1642 mutex_lock(&gpd_list_lock);
1643 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1644 if (!master && !strcmp(gpd->name, master_name))
1645 master = gpd;
1646
1647 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1648 subdomain = gpd;
1649
1650 if (master && subdomain)
1651 break;
1652 }
1653 mutex_unlock(&gpd_list_lock);
1654
1655 return pm_genpd_add_subdomain(master, subdomain);
1656}
1657
f721889f
RW
1658/**
1659 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1660 * @genpd: Master PM domain to remove the subdomain from.
5063ce15 1661 * @subdomain: Subdomain to be removed.
f721889f
RW
1662 */
1663int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
5063ce15 1664 struct generic_pm_domain *subdomain)
f721889f 1665{
5063ce15 1666 struct gpd_link *link;
f721889f
RW
1667 int ret = -EINVAL;
1668
5063ce15 1669 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
f721889f
RW
1670 return -EINVAL;
1671
17b75eca
RW
1672 start:
1673 genpd_acquire_lock(genpd);
f721889f 1674
5063ce15
RW
1675 list_for_each_entry(link, &genpd->master_links, master_node) {
1676 if (link->slave != subdomain)
f721889f
RW
1677 continue;
1678
1679 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1680
17b75eca
RW
1681 if (subdomain->status != GPD_STATE_POWER_OFF
1682 && subdomain->status != GPD_STATE_ACTIVE) {
1683 mutex_unlock(&subdomain->lock);
1684 genpd_release_lock(genpd);
1685 goto start;
1686 }
1687
5063ce15
RW
1688 list_del(&link->master_node);
1689 list_del(&link->slave_node);
1690 kfree(link);
17b75eca 1691 if (subdomain->status != GPD_STATE_POWER_OFF)
f721889f
RW
1692 genpd_sd_counter_dec(genpd);
1693
1694 mutex_unlock(&subdomain->lock);
1695
1696 ret = 0;
1697 break;
1698 }
1699
17b75eca 1700 genpd_release_lock(genpd);
f721889f
RW
1701
1702 return ret;
1703}
1704
40114447
RW
1705/**
1706 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1707 * @genpd: PM domain to be connected with cpuidle.
1708 * @state: cpuidle state this domain can disable/enable.
1709 *
1710 * Make a PM domain behave as though it contained a CPU core, that is, instead
1711 * of calling its power down routine it will enable the given cpuidle state so
1712 * that the cpuidle subsystem can power it down (if possible and desirable).
1713 */
1714int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
cbc9ef02
RW
1715{
1716 struct cpuidle_driver *cpuidle_drv;
1717 struct gpd_cpu_data *cpu_data;
1718 struct cpuidle_state *idle_state;
1719 int ret = 0;
1720
1721 if (IS_ERR_OR_NULL(genpd) || state < 0)
1722 return -EINVAL;
1723
1724 genpd_acquire_lock(genpd);
1725
1726 if (genpd->cpu_data) {
1727 ret = -EEXIST;
1728 goto out;
1729 }
1730 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1731 if (!cpu_data) {
1732 ret = -ENOMEM;
1733 goto out;
1734 }
1735 cpuidle_drv = cpuidle_driver_ref();
1736 if (!cpuidle_drv) {
1737 ret = -ENODEV;
debe081a 1738 goto err_drv;
cbc9ef02
RW
1739 }
1740 if (cpuidle_drv->state_count <= state) {
1741 ret = -EINVAL;
1742 goto err;
1743 }
1744 idle_state = &cpuidle_drv->states[state];
1745 if (!idle_state->disabled) {
1746 ret = -EAGAIN;
1747 goto err;
1748 }
1749 cpu_data->idle_state = idle_state;
1750 cpu_data->saved_exit_latency = idle_state->exit_latency;
1751 genpd->cpu_data = cpu_data;
1752 genpd_recalc_cpu_exit_latency(genpd);
1753
1754 out:
1755 genpd_release_lock(genpd);
1756 return ret;
1757
1758 err:
1759 cpuidle_driver_unref();
debe081a 1760
1761 err_drv:
1762 kfree(cpu_data);
cbc9ef02
RW
1763 goto out;
1764}
1765
74a2799a
RW
1766/**
1767 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1768 * @name: Name of the domain to connect to cpuidle.
1769 * @state: cpuidle state this domain can manipulate.
1770 */
1771int pm_genpd_name_attach_cpuidle(const char *name, int state)
1772{
1773 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1774}
1775
40114447
RW
1776/**
1777 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1778 * @genpd: PM domain to remove the cpuidle connection from.
1779 *
1780 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1781 * given PM domain.
1782 */
1783int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
cbc9ef02
RW
1784{
1785 struct gpd_cpu_data *cpu_data;
1786 struct cpuidle_state *idle_state;
1787 int ret = 0;
1788
1789 if (IS_ERR_OR_NULL(genpd))
1790 return -EINVAL;
1791
1792 genpd_acquire_lock(genpd);
1793
1794 cpu_data = genpd->cpu_data;
1795 if (!cpu_data) {
1796 ret = -ENODEV;
1797 goto out;
1798 }
1799 idle_state = cpu_data->idle_state;
1800 if (!idle_state->disabled) {
1801 ret = -EAGAIN;
1802 goto out;
1803 }
1804 idle_state->exit_latency = cpu_data->saved_exit_latency;
1805 cpuidle_driver_unref();
1806 genpd->cpu_data = NULL;
1807 kfree(cpu_data);
1808
1809 out:
1810 genpd_release_lock(genpd);
1811 return ret;
1812}
1813
74a2799a
RW
1814/**
1815 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1816 * @name: Name of the domain to disconnect cpuidle from.
1817 */
1818int pm_genpd_name_detach_cpuidle(const char *name)
1819{
1820 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1821}
1822
d23b9b00
RW
1823/* Default device callbacks for generic PM domains. */
1824
ecf00475
RW
1825/**
1826 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1827 * @dev: Device to handle.
1828 */
1829static int pm_genpd_default_save_state(struct device *dev)
1830{
1831 int (*cb)(struct device *__dev);
ecf00475 1832
0b589741
RW
1833 if (dev->type && dev->type->pm)
1834 cb = dev->type->pm->runtime_suspend;
1835 else if (dev->class && dev->class->pm)
1836 cb = dev->class->pm->runtime_suspend;
1837 else if (dev->bus && dev->bus->pm)
1838 cb = dev->bus->pm->runtime_suspend;
1839 else
1840 cb = NULL;
ecf00475 1841
0b589741
RW
1842 if (!cb && dev->driver && dev->driver->pm)
1843 cb = dev->driver->pm->runtime_suspend;
1844
1845 return cb ? cb(dev) : 0;
ecf00475
RW
1846}
1847
1848/**
1849 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1850 * @dev: Device to handle.
1851 */
1852static int pm_genpd_default_restore_state(struct device *dev)
1853{
1854 int (*cb)(struct device *__dev);
ecf00475 1855
0b589741
RW
1856 if (dev->type && dev->type->pm)
1857 cb = dev->type->pm->runtime_resume;
1858 else if (dev->class && dev->class->pm)
1859 cb = dev->class->pm->runtime_resume;
1860 else if (dev->bus && dev->bus->pm)
1861 cb = dev->bus->pm->runtime_resume;
1862 else
1863 cb = NULL;
ecf00475 1864
0b589741
RW
1865 if (!cb && dev->driver && dev->driver->pm)
1866 cb = dev->driver->pm->runtime_resume;
1867
1868 return cb ? cb(dev) : 0;
ecf00475
RW
1869}
1870
f721889f
RW
1871/**
1872 * pm_genpd_init - Initialize a generic I/O PM domain object.
1873 * @genpd: PM domain object to initialize.
1874 * @gov: PM domain governor to associate with the domain (may be NULL).
1875 * @is_off: Initial value of the domain's power_is_off field.
1876 */
1877void pm_genpd_init(struct generic_pm_domain *genpd,
1878 struct dev_power_governor *gov, bool is_off)
1879{
1880 if (IS_ERR_OR_NULL(genpd))
1881 return;
1882
5063ce15
RW
1883 INIT_LIST_HEAD(&genpd->master_links);
1884 INIT_LIST_HEAD(&genpd->slave_links);
f721889f 1885 INIT_LIST_HEAD(&genpd->dev_list);
f721889f
RW
1886 mutex_init(&genpd->lock);
1887 genpd->gov = gov;
1888 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1889 genpd->in_progress = 0;
c4bb3160 1890 atomic_set(&genpd->sd_count, 0);
17b75eca
RW
1891 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1892 init_waitqueue_head(&genpd->status_wait_queue);
c6d22b37
RW
1893 genpd->poweroff_task = NULL;
1894 genpd->resume_count = 0;
596ba34b 1895 genpd->device_count = 0;
221e9b58 1896 genpd->max_off_time_ns = -1;
6ff7bb0d 1897 genpd->max_off_time_changed = true;
f721889f
RW
1898 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1899 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
596ba34b
RW
1900 genpd->domain.ops.prepare = pm_genpd_prepare;
1901 genpd->domain.ops.suspend = pm_genpd_suspend;
0496c8ae 1902 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
596ba34b
RW
1903 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1904 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
0496c8ae 1905 genpd->domain.ops.resume_early = pm_genpd_resume_early;
596ba34b
RW
1906 genpd->domain.ops.resume = pm_genpd_resume;
1907 genpd->domain.ops.freeze = pm_genpd_freeze;
0496c8ae 1908 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
596ba34b
RW
1909 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1910 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
0496c8ae 1911 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
596ba34b 1912 genpd->domain.ops.thaw = pm_genpd_thaw;
d23b9b00 1913 genpd->domain.ops.poweroff = pm_genpd_suspend;
0496c8ae 1914 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
d23b9b00 1915 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
596ba34b 1916 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
0496c8ae 1917 genpd->domain.ops.restore_early = pm_genpd_resume_early;
d23b9b00 1918 genpd->domain.ops.restore = pm_genpd_resume;
596ba34b 1919 genpd->domain.ops.complete = pm_genpd_complete;
ecf00475
RW
1920 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1921 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
5125bbf3
RW
1922 mutex_lock(&gpd_list_lock);
1923 list_add(&genpd->gpd_list_node, &gpd_list);
1924 mutex_unlock(&gpd_list_lock);
1925}
This page took 0.363243 seconds and 5 git commands to generate.