PM / Domains: Sync runtime PM status with genpd after probe
[linux-2.6-block.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>
aa42240a 11#include <linux/platform_device.h>
f721889f
RW
12#include <linux/pm_runtime.h>
13#include <linux/pm_domain.h>
6ff7bb0d 14#include <linux/pm_qos.h>
c11f6f5b 15#include <linux/pm_clock.h>
f721889f
RW
16#include <linux/slab.h>
17#include <linux/err.h>
17b75eca
RW
18#include <linux/sched.h>
19#include <linux/suspend.h>
d5e4cbfe
RW
20#include <linux/export.h>
21
22#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
23({ \
24 type (*__routine)(struct device *__d); \
25 type __ret = (type)0; \
26 \
27 __routine = genpd->dev_ops.callback; \
28 if (__routine) { \
29 __ret = __routine(dev); \
d5e4cbfe
RW
30 } \
31 __ret; \
32})
f721889f 33
0140d8bd
RW
34#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
35({ \
36 ktime_t __start = ktime_get(); \
37 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
38 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
6ff7bb0d
RW
39 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
40 if (!__retval && __elapsed > __td->field) { \
41 __td->field = __elapsed; \
7d1af287 42 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
0140d8bd 43 __elapsed); \
6ff7bb0d
RW
44 genpd->max_off_time_changed = true; \
45 __td->constraint_changed = true; \
0140d8bd
RW
46 } \
47 __retval; \
48})
49
5125bbf3
RW
50static LIST_HEAD(gpd_list);
51static DEFINE_MUTEX(gpd_list_lock);
52
8bc0251d
RW
53static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
54{
55 struct generic_pm_domain *genpd = NULL, *gpd;
56
57 if (IS_ERR_OR_NULL(domain_name))
58 return NULL;
59
60 mutex_lock(&gpd_list_lock);
61 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
62 if (!strcmp(gpd->name, domain_name)) {
63 genpd = gpd;
64 break;
65 }
66 }
67 mutex_unlock(&gpd_list_lock);
68 return genpd;
69}
70
b02c999a 71struct generic_pm_domain *dev_to_genpd(struct device *dev)
5248051b
RW
72{
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
75
596ba34b 76 return pd_to_genpd(dev->pm_domain);
5248051b 77}
f721889f 78
d5e4cbfe
RW
79static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
80{
0140d8bd
RW
81 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
82 stop_latency_ns, "stop");
d5e4cbfe
RW
83}
84
85static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
86{
0140d8bd
RW
87 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
88 start_latency_ns, "start");
d5e4cbfe
RW
89}
90
c4bb3160 91static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
f721889f 92{
c4bb3160
RW
93 bool ret = false;
94
95 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
96 ret = !!atomic_dec_and_test(&genpd->sd_count);
97
98 return ret;
99}
100
101static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
102{
103 atomic_inc(&genpd->sd_count);
4e857c58 104 smp_mb__after_atomic();
f721889f
RW
105}
106
17b75eca
RW
107static void genpd_acquire_lock(struct generic_pm_domain *genpd)
108{
109 DEFINE_WAIT(wait);
110
111 mutex_lock(&genpd->lock);
112 /*
113 * Wait for the domain to transition into either the active,
114 * or the power off state.
115 */
116 for (;;) {
117 prepare_to_wait(&genpd->status_wait_queue, &wait,
118 TASK_UNINTERRUPTIBLE);
c6d22b37
RW
119 if (genpd->status == GPD_STATE_ACTIVE
120 || genpd->status == GPD_STATE_POWER_OFF)
17b75eca
RW
121 break;
122 mutex_unlock(&genpd->lock);
123
124 schedule();
125
126 mutex_lock(&genpd->lock);
127 }
128 finish_wait(&genpd->status_wait_queue, &wait);
129}
130
131static void genpd_release_lock(struct generic_pm_domain *genpd)
132{
133 mutex_unlock(&genpd->lock);
134}
135
c6d22b37
RW
136static void genpd_set_active(struct generic_pm_domain *genpd)
137{
138 if (genpd->resume_count == 0)
139 genpd->status = GPD_STATE_ACTIVE;
140}
141
cbc9ef02
RW
142static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
143{
144 s64 usecs64;
145
f39cb179 146 if (!genpd->cpuidle_data)
cbc9ef02
RW
147 return;
148
149 usecs64 = genpd->power_on_latency_ns;
150 do_div(usecs64, NSEC_PER_USEC);
f39cb179
UH
151 usecs64 += genpd->cpuidle_data->saved_exit_latency;
152 genpd->cpuidle_data->idle_state->exit_latency = usecs64;
cbc9ef02
RW
153}
154
c8f0ea45
GU
155static int genpd_power_on(struct generic_pm_domain *genpd)
156{
157 ktime_t time_start;
158 s64 elapsed_ns;
159 int ret;
160
161 if (!genpd->power_on)
162 return 0;
163
164 time_start = ktime_get();
165 ret = genpd->power_on(genpd);
166 if (ret)
167 return ret;
168
169 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
170 if (elapsed_ns <= genpd->power_on_latency_ns)
171 return ret;
172
173 genpd->power_on_latency_ns = elapsed_ns;
174 genpd->max_off_time_changed = true;
175 genpd_recalc_cpu_exit_latency(genpd);
176 pr_warn("%s: Power-%s latency exceeded, new value %lld ns\n",
177 genpd->name, "on", elapsed_ns);
178
179 return ret;
180}
181
182static int genpd_power_off(struct generic_pm_domain *genpd)
183{
184 ktime_t time_start;
185 s64 elapsed_ns;
186 int ret;
187
188 if (!genpd->power_off)
189 return 0;
190
191 time_start = ktime_get();
192 ret = genpd->power_off(genpd);
193 if (ret == -EBUSY)
194 return ret;
195
196 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
197 if (elapsed_ns <= genpd->power_off_latency_ns)
198 return ret;
199
200 genpd->power_off_latency_ns = elapsed_ns;
201 genpd->max_off_time_changed = true;
202 pr_warn("%s: Power-%s latency exceeded, new value %lld ns\n",
203 genpd->name, "off", elapsed_ns);
204
205 return ret;
206}
207
5248051b 208/**
5063ce15 209 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
5248051b
RW
210 * @genpd: PM domain to power up.
211 *
5063ce15 212 * Restore power to @genpd and all of its masters so that it is possible to
5248051b
RW
213 * resume a device belonging to it.
214 */
8951ef02 215static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
3f241775 216 __releases(&genpd->lock) __acquires(&genpd->lock)
5248051b 217{
5063ce15 218 struct gpd_link *link;
3f241775 219 DEFINE_WAIT(wait);
5248051b
RW
220 int ret = 0;
221
5063ce15 222 /* If the domain's master is being waited for, we have to wait too. */
3f241775
RW
223 for (;;) {
224 prepare_to_wait(&genpd->status_wait_queue, &wait,
225 TASK_UNINTERRUPTIBLE);
17877eb5 226 if (genpd->status != GPD_STATE_WAIT_MASTER)
3f241775
RW
227 break;
228 mutex_unlock(&genpd->lock);
17b75eca 229
3f241775
RW
230 schedule();
231
232 mutex_lock(&genpd->lock);
233 }
234 finish_wait(&genpd->status_wait_queue, &wait);
9e08cf42 235
17b75eca 236 if (genpd->status == GPD_STATE_ACTIVE
596ba34b 237 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
3f241775 238 return 0;
5248051b 239
c6d22b37
RW
240 if (genpd->status != GPD_STATE_POWER_OFF) {
241 genpd_set_active(genpd);
3f241775 242 return 0;
c6d22b37
RW
243 }
244
f39cb179 245 if (genpd->cpuidle_data) {
cbc9ef02 246 cpuidle_pause_and_lock();
f39cb179 247 genpd->cpuidle_data->idle_state->disabled = true;
cbc9ef02
RW
248 cpuidle_resume_and_unlock();
249 goto out;
250 }
251
5063ce15
RW
252 /*
253 * The list is guaranteed not to change while the loop below is being
254 * executed, unless one of the masters' .power_on() callbacks fiddles
255 * with it.
256 */
257 list_for_each_entry(link, &genpd->slave_links, slave_node) {
258 genpd_sd_counter_inc(link->master);
17877eb5 259 genpd->status = GPD_STATE_WAIT_MASTER;
3c07cbc4 260
5248051b 261 mutex_unlock(&genpd->lock);
5248051b 262
5063ce15 263 ret = pm_genpd_poweron(link->master);
9e08cf42
RW
264
265 mutex_lock(&genpd->lock);
266
3f241775
RW
267 /*
268 * The "wait for parent" status is guaranteed not to change
5063ce15 269 * while the master is powering on.
3f241775
RW
270 */
271 genpd->status = GPD_STATE_POWER_OFF;
272 wake_up_all(&genpd->status_wait_queue);
5063ce15
RW
273 if (ret) {
274 genpd_sd_counter_dec(link->master);
9e08cf42 275 goto err;
5063ce15 276 }
5248051b
RW
277 }
278
c8f0ea45
GU
279 ret = genpd_power_on(genpd);
280 if (ret)
281 goto err;
5248051b 282
cbc9ef02 283 out:
9e08cf42
RW
284 genpd_set_active(genpd);
285
3f241775 286 return 0;
9e08cf42
RW
287
288 err:
5063ce15
RW
289 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
290 genpd_sd_counter_dec(link->master);
9e08cf42 291
3f241775
RW
292 return ret;
293}
294
295/**
5063ce15 296 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
3f241775
RW
297 * @genpd: PM domain to power up.
298 */
299int pm_genpd_poweron(struct generic_pm_domain *genpd)
300{
301 int ret;
302
303 mutex_lock(&genpd->lock);
304 ret = __pm_genpd_poweron(genpd);
305 mutex_unlock(&genpd->lock);
306 return ret;
5248051b
RW
307}
308
8bc0251d
RW
309/**
310 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
311 * @domain_name: Name of the PM domain to power up.
312 */
313int pm_genpd_name_poweron(const char *domain_name)
314{
315 struct generic_pm_domain *genpd;
316
317 genpd = pm_genpd_lookup_name(domain_name);
318 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
319}
320
b3d3b9fb
SK
321static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
322 struct device *dev)
323{
324 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
325}
326
8e9afafd
RW
327static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
328{
329 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
330 save_state_latency_ns, "state save");
331}
332
333static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
334{
335 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
336 restore_state_latency_ns,
337 "state restore");
338}
339
6ff7bb0d
RW
340static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
341 unsigned long val, void *ptr)
342{
343 struct generic_pm_domain_data *gpd_data;
344 struct device *dev;
345
346 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
6ff7bb0d 347 dev = gpd_data->base.dev;
6ff7bb0d
RW
348
349 for (;;) {
350 struct generic_pm_domain *genpd;
351 struct pm_domain_data *pdd;
352
353 spin_lock_irq(&dev->power.lock);
354
355 pdd = dev->power.subsys_data ?
356 dev->power.subsys_data->domain_data : NULL;
1d5fcfec 357 if (pdd && pdd->dev) {
6ff7bb0d
RW
358 to_gpd_data(pdd)->td.constraint_changed = true;
359 genpd = dev_to_genpd(dev);
360 } else {
361 genpd = ERR_PTR(-ENODATA);
362 }
363
364 spin_unlock_irq(&dev->power.lock);
365
366 if (!IS_ERR(genpd)) {
367 mutex_lock(&genpd->lock);
368 genpd->max_off_time_changed = true;
369 mutex_unlock(&genpd->lock);
370 }
371
372 dev = dev->parent;
373 if (!dev || dev->power.ignore_children)
374 break;
375 }
376
377 return NOTIFY_DONE;
378}
379
f721889f
RW
380/**
381 * __pm_genpd_save_device - Save the pre-suspend state of a device.
4605ab65 382 * @pdd: Domain data of the device to save the state of.
f721889f
RW
383 * @genpd: PM domain the device belongs to.
384 */
4605ab65 385static int __pm_genpd_save_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;
f721889f
RW
391 int ret = 0;
392
67732cd3 393 if (gpd_data->need_restore > 0)
f721889f
RW
394 return 0;
395
67732cd3
UH
396 /*
397 * If the value of the need_restore flag is still unknown at this point,
398 * we trust that pm_genpd_poweroff() has verified that the device is
399 * already runtime PM suspended.
400 */
401 if (gpd_data->need_restore < 0) {
402 gpd_data->need_restore = 1;
403 return 0;
404 }
405
17b75eca
RW
406 mutex_unlock(&genpd->lock);
407
ecf00475
RW
408 genpd_start_dev(genpd, dev);
409 ret = genpd_save_dev(genpd, dev);
410 genpd_stop_dev(genpd, dev);
f721889f 411
17b75eca
RW
412 mutex_lock(&genpd->lock);
413
f721889f 414 if (!ret)
67732cd3 415 gpd_data->need_restore = 1;
f721889f
RW
416
417 return ret;
418}
419
420/**
421 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
4605ab65 422 * @pdd: Domain data of the device to restore the state of.
f721889f
RW
423 * @genpd: PM domain the device belongs to.
424 */
4605ab65 425static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
f721889f 426 struct generic_pm_domain *genpd)
17b75eca 427 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f 428{
cd0ea672 429 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
4605ab65 430 struct device *dev = pdd->dev;
67732cd3 431 int need_restore = gpd_data->need_restore;
f721889f 432
67732cd3 433 gpd_data->need_restore = 0;
17b75eca
RW
434 mutex_unlock(&genpd->lock);
435
ecf00475 436 genpd_start_dev(genpd, dev);
67732cd3
UH
437
438 /*
439 * Call genpd_restore_dev() for recently added devices too (need_restore
440 * is negative then).
441 */
80de3d7f
RW
442 if (need_restore)
443 genpd_restore_dev(genpd, dev);
f721889f 444
17b75eca 445 mutex_lock(&genpd->lock);
f721889f
RW
446}
447
c6d22b37
RW
448/**
449 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
450 * @genpd: PM domain to check.
451 *
452 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
453 * a "power off" operation, which means that a "power on" has occured in the
454 * meantime, or if its resume_count field is different from zero, which means
455 * that one of its devices has been resumed in the meantime.
456 */
457static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
458{
17877eb5 459 return genpd->status == GPD_STATE_WAIT_MASTER
3f241775 460 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
c6d22b37
RW
461}
462
56375fd4
RW
463/**
464 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
465 * @genpd: PM domait to power off.
466 *
467 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
468 * before.
469 */
d971f0b0 470static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
56375fd4 471{
a4ca26a4 472 queue_work(pm_wq, &genpd->power_off_work);
56375fd4
RW
473}
474
f721889f
RW
475/**
476 * pm_genpd_poweroff - Remove power from a given PM domain.
477 * @genpd: PM domain to power down.
478 *
479 * If all of the @genpd's devices have been suspended and all of its subdomains
480 * have been powered down, run the runtime suspend callbacks provided by all of
481 * the @genpd's devices' drivers and remove power from @genpd.
482 */
483static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
17b75eca 484 __releases(&genpd->lock) __acquires(&genpd->lock)
f721889f 485{
4605ab65 486 struct pm_domain_data *pdd;
5063ce15 487 struct gpd_link *link;
f721889f 488 unsigned int not_suspended;
c6d22b37 489 int ret = 0;
f721889f 490
c6d22b37
RW
491 start:
492 /*
493 * Do not try to power off the domain in the following situations:
494 * (1) The domain is already in the "power off" state.
5063ce15 495 * (2) The domain is waiting for its master to power up.
c6d22b37 496 * (3) One of the domain's devices is being resumed right now.
3f241775 497 * (4) System suspend is in progress.
c6d22b37 498 */
3f241775 499 if (genpd->status == GPD_STATE_POWER_OFF
17877eb5 500 || genpd->status == GPD_STATE_WAIT_MASTER
3f241775 501 || genpd->resume_count > 0 || genpd->prepared_count > 0)
f721889f
RW
502 return 0;
503
c4bb3160 504 if (atomic_read(&genpd->sd_count) > 0)
f721889f
RW
505 return -EBUSY;
506
507 not_suspended = 0;
34b1f762
RW
508 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
509 enum pm_qos_flags_status stat;
510
511 stat = dev_pm_qos_flags(pdd->dev,
512 PM_QOS_FLAG_NO_POWER_OFF
513 | PM_QOS_FLAG_REMOTE_WAKEUP);
514 if (stat > PM_QOS_FLAGS_NONE)
515 return -EBUSY;
516
0aa2a221 517 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
feb70af0 518 || pdd->dev->power.irq_safe))
f721889f 519 not_suspended++;
34b1f762 520 }
f721889f
RW
521
522 if (not_suspended > genpd->in_progress)
523 return -EBUSY;
524
c6d22b37
RW
525 if (genpd->poweroff_task) {
526 /*
527 * Another instance of pm_genpd_poweroff() is executing
528 * callbacks, so tell it to start over and return.
529 */
530 genpd->status = GPD_STATE_REPEAT;
531 return 0;
532 }
533
f721889f
RW
534 if (genpd->gov && genpd->gov->power_down_ok) {
535 if (!genpd->gov->power_down_ok(&genpd->domain))
536 return -EAGAIN;
537 }
538
17b75eca 539 genpd->status = GPD_STATE_BUSY;
c6d22b37 540 genpd->poweroff_task = current;
17b75eca 541
4605ab65 542 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
3c07cbc4 543 ret = atomic_read(&genpd->sd_count) == 0 ?
4605ab65 544 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
3f241775
RW
545
546 if (genpd_abort_poweroff(genpd))
547 goto out;
548
697a7f37
RW
549 if (ret) {
550 genpd_set_active(genpd);
551 goto out;
552 }
f721889f 553
c6d22b37
RW
554 if (genpd->status == GPD_STATE_REPEAT) {
555 genpd->poweroff_task = NULL;
556 goto start;
557 }
558 }
17b75eca 559
f39cb179 560 if (genpd->cpuidle_data) {
cbc9ef02 561 /*
f39cb179
UH
562 * If cpuidle_data is set, cpuidle should turn the domain off
563 * when the CPU in it is idle. In that case we don't decrement
564 * the subdomain counts of the master domains, so that power is
565 * not removed from the current domain prematurely as a result
566 * of cutting off the masters' power.
cbc9ef02
RW
567 */
568 genpd->status = GPD_STATE_POWER_OFF;
569 cpuidle_pause_and_lock();
f39cb179 570 genpd->cpuidle_data->idle_state->disabled = false;
cbc9ef02
RW
571 cpuidle_resume_and_unlock();
572 goto out;
573 }
574
3c07cbc4
RW
575 if (genpd->power_off) {
576 if (atomic_read(&genpd->sd_count) > 0) {
577 ret = -EBUSY;
c6d22b37
RW
578 goto out;
579 }
17b75eca 580
3c07cbc4 581 /*
5063ce15
RW
582 * If sd_count > 0 at this point, one of the subdomains hasn't
583 * managed to call pm_genpd_poweron() for the master yet after
3c07cbc4
RW
584 * incrementing it. In that case pm_genpd_poweron() will wait
585 * for us to drop the lock, so we can call .power_off() and let
586 * the pm_genpd_poweron() restore power for us (this shouldn't
587 * happen very often).
588 */
c8f0ea45 589 ret = genpd_power_off(genpd);
d2805402
RW
590 if (ret == -EBUSY) {
591 genpd_set_active(genpd);
d2805402
RW
592 goto out;
593 }
594 }
f721889f 595
17b75eca 596 genpd->status = GPD_STATE_POWER_OFF;
221e9b58 597
5063ce15
RW
598 list_for_each_entry(link, &genpd->slave_links, slave_node) {
599 genpd_sd_counter_dec(link->master);
600 genpd_queue_power_off_work(link->master);
601 }
f721889f 602
c6d22b37
RW
603 out:
604 genpd->poweroff_task = NULL;
605 wake_up_all(&genpd->status_wait_queue);
606 return ret;
f721889f
RW
607}
608
609/**
610 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
611 * @work: Work structure used for scheduling the execution of this function.
612 */
613static void genpd_power_off_work_fn(struct work_struct *work)
614{
615 struct generic_pm_domain *genpd;
616
617 genpd = container_of(work, struct generic_pm_domain, power_off_work);
618
17b75eca 619 genpd_acquire_lock(genpd);
f721889f 620 pm_genpd_poweroff(genpd);
17b75eca 621 genpd_release_lock(genpd);
f721889f
RW
622}
623
624/**
625 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
626 * @dev: Device to suspend.
627 *
628 * Carry out a runtime suspend of a device under the assumption that its
629 * pm_domain field points to the domain member of an object of type
630 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
631 */
632static int pm_genpd_runtime_suspend(struct device *dev)
633{
634 struct generic_pm_domain *genpd;
67732cd3 635 struct generic_pm_domain_data *gpd_data;
b02c999a 636 bool (*stop_ok)(struct device *__dev);
d5e4cbfe 637 int ret;
f721889f
RW
638
639 dev_dbg(dev, "%s()\n", __func__);
640
5248051b
RW
641 genpd = dev_to_genpd(dev);
642 if (IS_ERR(genpd))
f721889f
RW
643 return -EINVAL;
644
b02c999a
RW
645 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
646 if (stop_ok && !stop_ok(dev))
647 return -EBUSY;
648
d5e4cbfe
RW
649 ret = genpd_stop_dev(genpd, dev);
650 if (ret)
651 return ret;
17b75eca 652
0aa2a221
RW
653 /*
654 * If power.irq_safe is set, this routine will be run with interrupts
655 * off, so it can't use mutexes.
656 */
657 if (dev->power.irq_safe)
658 return 0;
659
c6d22b37 660 mutex_lock(&genpd->lock);
67732cd3
UH
661
662 /*
663 * If we have an unknown state of the need_restore flag, it means none
664 * of the runtime PM callbacks has been invoked yet. Let's update the
665 * flag to reflect that the current state is active.
666 */
667 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
668 if (gpd_data->need_restore < 0)
669 gpd_data->need_restore = 0;
670
f721889f
RW
671 genpd->in_progress++;
672 pm_genpd_poweroff(genpd);
673 genpd->in_progress--;
c6d22b37 674 mutex_unlock(&genpd->lock);
f721889f
RW
675
676 return 0;
677}
678
f721889f
RW
679/**
680 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
681 * @dev: Device to resume.
682 *
683 * Carry out a runtime resume of a device under the assumption that its
684 * pm_domain field points to the domain member of an object of type
685 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
686 */
687static int pm_genpd_runtime_resume(struct device *dev)
688{
689 struct generic_pm_domain *genpd;
c6d22b37 690 DEFINE_WAIT(wait);
f721889f
RW
691 int ret;
692
693 dev_dbg(dev, "%s()\n", __func__);
694
5248051b
RW
695 genpd = dev_to_genpd(dev);
696 if (IS_ERR(genpd))
f721889f
RW
697 return -EINVAL;
698
0aa2a221
RW
699 /* If power.irq_safe, the PM domain is never powered off. */
700 if (dev->power.irq_safe)
e2e3e4e5 701 return genpd_start_dev_no_timing(genpd, dev);
0aa2a221 702
c6d22b37 703 mutex_lock(&genpd->lock);
3f241775
RW
704 ret = __pm_genpd_poweron(genpd);
705 if (ret) {
706 mutex_unlock(&genpd->lock);
707 return ret;
708 }
17b75eca 709 genpd->status = GPD_STATE_BUSY;
c6d22b37
RW
710 genpd->resume_count++;
711 for (;;) {
712 prepare_to_wait(&genpd->status_wait_queue, &wait,
713 TASK_UNINTERRUPTIBLE);
714 /*
715 * If current is the powering off task, we have been called
716 * reentrantly from one of the device callbacks, so we should
717 * not wait.
718 */
719 if (!genpd->poweroff_task || genpd->poweroff_task == current)
720 break;
721 mutex_unlock(&genpd->lock);
722
723 schedule();
724
725 mutex_lock(&genpd->lock);
726 }
727 finish_wait(&genpd->status_wait_queue, &wait);
cd0ea672 728 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
c6d22b37
RW
729 genpd->resume_count--;
730 genpd_set_active(genpd);
17b75eca 731 wake_up_all(&genpd->status_wait_queue);
c6d22b37 732 mutex_unlock(&genpd->lock);
17b75eca 733
f721889f
RW
734 return 0;
735}
736
39ac5ba5
TB
737static bool pd_ignore_unused;
738static int __init pd_ignore_unused_setup(char *__unused)
739{
740 pd_ignore_unused = true;
741 return 1;
742}
743__setup("pd_ignore_unused", pd_ignore_unused_setup);
744
17f2ae7f
RW
745/**
746 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
747 */
748void pm_genpd_poweroff_unused(void)
749{
750 struct generic_pm_domain *genpd;
751
39ac5ba5
TB
752 if (pd_ignore_unused) {
753 pr_warn("genpd: Not disabling unused power domains\n");
754 return;
755 }
756
17f2ae7f
RW
757 mutex_lock(&gpd_list_lock);
758
759 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
760 genpd_queue_power_off_work(genpd);
761
762 mutex_unlock(&gpd_list_lock);
763}
764
2fe71dcd
UH
765static int __init genpd_poweroff_unused(void)
766{
767 pm_genpd_poweroff_unused();
768 return 0;
769}
770late_initcall(genpd_poweroff_unused);
771
596ba34b
RW
772#ifdef CONFIG_PM_SLEEP
773
77f827de
RW
774/**
775 * pm_genpd_present - Check if the given PM domain has been initialized.
776 * @genpd: PM domain to check.
777 */
895b31f3 778static bool pm_genpd_present(const struct generic_pm_domain *genpd)
77f827de 779{
895b31f3 780 const struct generic_pm_domain *gpd;
77f827de
RW
781
782 if (IS_ERR_OR_NULL(genpd))
783 return false;
784
785 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
786 if (gpd == genpd)
787 return true;
788
789 return false;
790}
791
d5e4cbfe
RW
792static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
793 struct device *dev)
794{
795 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
796}
797
596ba34b 798/**
5063ce15 799 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
596ba34b
RW
800 * @genpd: PM domain to power off, if possible.
801 *
802 * Check if the given PM domain can be powered off (during system suspend or
5063ce15 803 * hibernation) and do that if so. Also, in that case propagate to its masters.
596ba34b 804 *
77f827de
RW
805 * This function is only called in "noirq" and "syscore" stages of system power
806 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
807 * executed sequentially, so it is guaranteed that it will never run twice in
808 * parallel).
596ba34b
RW
809 */
810static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
811{
5063ce15 812 struct gpd_link *link;
596ba34b 813
17b75eca 814 if (genpd->status == GPD_STATE_POWER_OFF)
596ba34b
RW
815 return;
816
c4bb3160
RW
817 if (genpd->suspended_count != genpd->device_count
818 || atomic_read(&genpd->sd_count) > 0)
596ba34b
RW
819 return;
820
c8f0ea45 821 genpd_power_off(genpd);
596ba34b 822
17b75eca 823 genpd->status = GPD_STATE_POWER_OFF;
5063ce15
RW
824
825 list_for_each_entry(link, &genpd->slave_links, slave_node) {
826 genpd_sd_counter_dec(link->master);
827 pm_genpd_sync_poweroff(link->master);
596ba34b
RW
828 }
829}
830
802d8b49
RW
831/**
832 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
833 * @genpd: PM domain to power on.
834 *
77f827de
RW
835 * This function is only called in "noirq" and "syscore" stages of system power
836 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
837 * executed sequentially, so it is guaranteed that it will never run twice in
838 * parallel).
802d8b49
RW
839 */
840static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
841{
842 struct gpd_link *link;
843
844 if (genpd->status != GPD_STATE_POWER_OFF)
845 return;
846
847 list_for_each_entry(link, &genpd->slave_links, slave_node) {
848 pm_genpd_sync_poweron(link->master);
849 genpd_sd_counter_inc(link->master);
850 }
851
c8f0ea45 852 genpd_power_on(genpd);
802d8b49
RW
853
854 genpd->status = GPD_STATE_ACTIVE;
855}
856
4ecd6e65
RW
857/**
858 * resume_needed - Check whether to resume a device before system suspend.
859 * @dev: Device to check.
860 * @genpd: PM domain the device belongs to.
861 *
862 * There are two cases in which a device that can wake up the system from sleep
863 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
864 * to wake up the system and it has to remain active for this purpose while the
865 * system is in the sleep state and (2) if the device is not enabled to wake up
866 * the system from sleep states and it generally doesn't generate wakeup signals
867 * by itself (those signals are generated on its behalf by other parts of the
868 * system). In the latter case it may be necessary to reconfigure the device's
869 * wakeup settings during system suspend, because it may have been set up to
870 * signal remote wakeup from the system's working state as needed by runtime PM.
871 * Return 'true' in either of the above cases.
872 */
873static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
874{
875 bool active_wakeup;
876
877 if (!device_can_wakeup(dev))
878 return false;
879
d5e4cbfe 880 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
4ecd6e65
RW
881 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
882}
883
596ba34b
RW
884/**
885 * pm_genpd_prepare - Start power transition of a device in a PM domain.
886 * @dev: Device to start the transition of.
887 *
888 * Start a power transition of a device (during a system-wide power transition)
889 * under the assumption that its pm_domain field points to the domain member of
890 * an object of type struct generic_pm_domain representing a PM domain
891 * consisting of I/O devices.
892 */
893static int pm_genpd_prepare(struct device *dev)
894{
895 struct generic_pm_domain *genpd;
b6c10c84 896 int ret;
596ba34b
RW
897
898 dev_dbg(dev, "%s()\n", __func__);
899
900 genpd = dev_to_genpd(dev);
901 if (IS_ERR(genpd))
902 return -EINVAL;
903
17b75eca
RW
904 /*
905 * If a wakeup request is pending for the device, it should be woken up
906 * at this point and a system wakeup event should be reported if it's
907 * set up to wake up the system from sleep states.
908 */
909 pm_runtime_get_noresume(dev);
910 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
911 pm_wakeup_event(dev, 0);
912
913 if (pm_wakeup_pending()) {
84167035 914 pm_runtime_put(dev);
17b75eca
RW
915 return -EBUSY;
916 }
917
4ecd6e65
RW
918 if (resume_needed(dev, genpd))
919 pm_runtime_resume(dev);
920
17b75eca 921 genpd_acquire_lock(genpd);
596ba34b 922
65533bbf
RW
923 if (genpd->prepared_count++ == 0) {
924 genpd->suspended_count = 0;
17b75eca 925 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
65533bbf 926 }
17b75eca
RW
927
928 genpd_release_lock(genpd);
596ba34b
RW
929
930 if (genpd->suspend_power_off) {
17b75eca 931 pm_runtime_put_noidle(dev);
596ba34b
RW
932 return 0;
933 }
934
935 /*
17b75eca
RW
936 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
937 * so pm_genpd_poweron() will return immediately, but if the device
d5e4cbfe 938 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
17b75eca 939 * to make it operational.
596ba34b 940 */
17b75eca 941 pm_runtime_resume(dev);
596ba34b
RW
942 __pm_runtime_disable(dev, false);
943
b6c10c84
RW
944 ret = pm_generic_prepare(dev);
945 if (ret) {
946 mutex_lock(&genpd->lock);
947
948 if (--genpd->prepared_count == 0)
949 genpd->suspend_power_off = false;
950
951 mutex_unlock(&genpd->lock);
17b75eca 952 pm_runtime_enable(dev);
b6c10c84 953 }
17b75eca 954
84167035 955 pm_runtime_put(dev);
b6c10c84 956 return ret;
596ba34b
RW
957}
958
959/**
960 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
961 * @dev: Device to suspend.
962 *
963 * Suspend a device under the assumption that its pm_domain field points to the
964 * domain member of an object of type struct generic_pm_domain representing
965 * a PM domain consisting of I/O devices.
966 */
967static int pm_genpd_suspend(struct device *dev)
968{
969 struct generic_pm_domain *genpd;
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(dev);
596ba34b
RW
978}
979
980/**
0496c8ae 981 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
596ba34b
RW
982 * @dev: Device to suspend.
983 *
984 * Carry out a late suspend of a device under the assumption that its
985 * pm_domain field points to the domain member of an object of type
986 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
987 */
0496c8ae 988static int pm_genpd_suspend_late(struct device *dev)
596ba34b
RW
989{
990 struct generic_pm_domain *genpd;
596ba34b
RW
991
992 dev_dbg(dev, "%s()\n", __func__);
993
994 genpd = dev_to_genpd(dev);
995 if (IS_ERR(genpd))
996 return -EINVAL;
997
1e0407ca 998 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
0496c8ae 999}
596ba34b 1000
0496c8ae
RW
1001/**
1002 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1003 * @dev: Device to suspend.
1004 *
1005 * Stop the device and remove power from the domain if all devices in it have
1006 * been stopped.
1007 */
1008static int pm_genpd_suspend_noirq(struct device *dev)
1009{
1010 struct generic_pm_domain *genpd;
1011
1012 dev_dbg(dev, "%s()\n", __func__);
1013
1014 genpd = dev_to_genpd(dev);
1015 if (IS_ERR(genpd))
1016 return -EINVAL;
596ba34b 1017
dbf37414 1018 if (genpd->suspend_power_off
0496c8ae 1019 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
d4f2d87a
RW
1020 return 0;
1021
d5e4cbfe 1022 genpd_stop_dev(genpd, dev);
596ba34b
RW
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 */
1029 genpd->suspended_count++;
1030 pm_genpd_sync_poweroff(genpd);
1031
1032 return 0;
1033}
1034
1035/**
0496c8ae 1036 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
596ba34b
RW
1037 * @dev: Device to resume.
1038 *
0496c8ae 1039 * Restore power to the device's PM domain, if necessary, and start the device.
596ba34b
RW
1040 */
1041static int pm_genpd_resume_noirq(struct device *dev)
1042{
1043 struct generic_pm_domain *genpd;
1044
1045 dev_dbg(dev, "%s()\n", __func__);
1046
1047 genpd = dev_to_genpd(dev);
1048 if (IS_ERR(genpd))
1049 return -EINVAL;
1050
dbf37414 1051 if (genpd->suspend_power_off
cc85b207 1052 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
596ba34b
RW
1053 return 0;
1054
1055 /*
1056 * Since all of the "noirq" callbacks are executed sequentially, it is
1057 * guaranteed that this function will never run twice in parallel for
1058 * the same PM domain, so it is not necessary to use locking here.
1059 */
802d8b49 1060 pm_genpd_sync_poweron(genpd);
596ba34b 1061 genpd->suspended_count--;
596ba34b 1062
0496c8ae 1063 return genpd_start_dev(genpd, dev);
596ba34b
RW
1064}
1065
1066/**
0496c8ae
RW
1067 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1068 * @dev: Device to resume.
1069 *
1070 * Carry out an early resume of a device under the assumption that its
1071 * pm_domain field points to the domain member of an object of type
1072 * struct generic_pm_domain representing a power domain consisting of I/O
1073 * devices.
1074 */
1075static int pm_genpd_resume_early(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_early(dev);
0496c8ae
RW
1086}
1087
1088/**
1089 * pm_genpd_resume - Resume of device in an I/O PM domain.
596ba34b
RW
1090 * @dev: Device to resume.
1091 *
1092 * Resume 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_resume(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_resume(dev);
596ba34b
RW
1107}
1108
1109/**
0496c8ae 1110 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
596ba34b
RW
1111 * @dev: Device to freeze.
1112 *
1113 * Freeze a device under the assumption that its pm_domain field points to the
1114 * domain member of an object of type struct generic_pm_domain representing
1115 * a power domain consisting of I/O devices.
1116 */
1117static int pm_genpd_freeze(struct device *dev)
1118{
1119 struct generic_pm_domain *genpd;
1120
1121 dev_dbg(dev, "%s()\n", __func__);
1122
1123 genpd = dev_to_genpd(dev);
1124 if (IS_ERR(genpd))
1125 return -EINVAL;
1126
1e0407ca 1127 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
596ba34b
RW
1128}
1129
1130/**
0496c8ae
RW
1131 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1132 * @dev: Device to freeze.
1133 *
1134 * Carry out a late freeze of a device under the assumption that its
1135 * pm_domain field points to the domain member of an object of type
1136 * struct generic_pm_domain representing a power domain consisting of I/O
1137 * devices.
1138 */
1139static int pm_genpd_freeze_late(struct device *dev)
1140{
1141 struct generic_pm_domain *genpd;
1142
1143 dev_dbg(dev, "%s()\n", __func__);
1144
1145 genpd = dev_to_genpd(dev);
1146 if (IS_ERR(genpd))
1147 return -EINVAL;
1148
1e0407ca 1149 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
0496c8ae
RW
1150}
1151
1152/**
1153 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
596ba34b
RW
1154 * @dev: Device to freeze.
1155 *
1156 * Carry out a late freeze of a device under the assumption that its
1157 * pm_domain field points to the domain member of an object of type
1158 * struct generic_pm_domain representing a power domain consisting of I/O
1159 * devices.
1160 */
1161static int pm_genpd_freeze_noirq(struct device *dev)
1162{
1163 struct generic_pm_domain *genpd;
596ba34b
RW
1164
1165 dev_dbg(dev, "%s()\n", __func__);
1166
1167 genpd = dev_to_genpd(dev);
1168 if (IS_ERR(genpd))
1169 return -EINVAL;
1170
dbf37414 1171 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
0496c8ae 1172}
596ba34b 1173
0496c8ae
RW
1174/**
1175 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1176 * @dev: Device to thaw.
1177 *
1178 * Start the device, unless power has been removed from the domain already
1179 * before the system transition.
1180 */
1181static int pm_genpd_thaw_noirq(struct device *dev)
1182{
1183 struct generic_pm_domain *genpd;
596ba34b 1184
0496c8ae 1185 dev_dbg(dev, "%s()\n", __func__);
596ba34b 1186
0496c8ae
RW
1187 genpd = dev_to_genpd(dev);
1188 if (IS_ERR(genpd))
1189 return -EINVAL;
1190
dbf37414 1191 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
596ba34b
RW
1192}
1193
1194/**
0496c8ae 1195 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
596ba34b
RW
1196 * @dev: Device to thaw.
1197 *
1198 * Carry out an early thaw of a device under the assumption that its
1199 * pm_domain field points to the domain member of an object of type
1200 * struct generic_pm_domain representing a power domain consisting of I/O
1201 * devices.
1202 */
0496c8ae 1203static int pm_genpd_thaw_early(struct device *dev)
596ba34b
RW
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_early(dev);
596ba34b
RW
1214}
1215
1216/**
1217 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1218 * @dev: Device to thaw.
1219 *
1220 * Thaw a device under the assumption that its pm_domain field points to the
1221 * domain member of an object of type struct generic_pm_domain representing
1222 * a power domain consisting of I/O devices.
1223 */
1224static int pm_genpd_thaw(struct device *dev)
1225{
1226 struct generic_pm_domain *genpd;
1227
1228 dev_dbg(dev, "%s()\n", __func__);
1229
1230 genpd = dev_to_genpd(dev);
1231 if (IS_ERR(genpd))
1232 return -EINVAL;
1233
1e0407ca 1234 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
596ba34b
RW
1235}
1236
1237/**
0496c8ae 1238 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
596ba34b
RW
1239 * @dev: Device to resume.
1240 *
0496c8ae
RW
1241 * Make sure the domain will be in the same power state as before the
1242 * hibernation the system is resuming from and start the device if necessary.
596ba34b
RW
1243 */
1244static int pm_genpd_restore_noirq(struct device *dev)
1245{
1246 struct generic_pm_domain *genpd;
1247
1248 dev_dbg(dev, "%s()\n", __func__);
1249
1250 genpd = dev_to_genpd(dev);
1251 if (IS_ERR(genpd))
1252 return -EINVAL;
1253
1254 /*
1255 * Since all of the "noirq" callbacks are executed sequentially, it is
1256 * guaranteed that this function will never run twice in parallel for
1257 * the same PM domain, so it is not necessary to use locking here.
65533bbf
RW
1258 *
1259 * At this point suspended_count == 0 means we are being run for the
1260 * first time for the given domain in the present cycle.
596ba34b 1261 */
65533bbf 1262 if (genpd->suspended_count++ == 0) {
596ba34b 1263 /*
65533bbf 1264 * The boot kernel might put the domain into arbitrary state,
802d8b49
RW
1265 * so make it appear as powered off to pm_genpd_sync_poweron(),
1266 * so that it tries to power it on in case it was really off.
596ba34b 1267 */
65533bbf
RW
1268 genpd->status = GPD_STATE_POWER_OFF;
1269 if (genpd->suspend_power_off) {
1270 /*
1271 * If the domain was off before the hibernation, make
1272 * sure it will be off going forward.
1273 */
c8f0ea45 1274 genpd_power_off(genpd);
65533bbf
RW
1275
1276 return 0;
1277 }
596ba34b
RW
1278 }
1279
18dd2ece
RW
1280 if (genpd->suspend_power_off)
1281 return 0;
1282
802d8b49 1283 pm_genpd_sync_poweron(genpd);
596ba34b 1284
dbf37414 1285 return genpd_start_dev(genpd, dev);
596ba34b
RW
1286}
1287
1288/**
1289 * pm_genpd_complete - Complete power transition of a device in a power domain.
1290 * @dev: Device to complete the transition of.
1291 *
1292 * Complete a power transition of a device (during a system-wide power
1293 * transition) under the assumption that its pm_domain field points to the
1294 * domain member of an object of type struct generic_pm_domain representing
1295 * a power domain consisting of I/O devices.
1296 */
1297static void pm_genpd_complete(struct device *dev)
1298{
1299 struct generic_pm_domain *genpd;
1300 bool run_complete;
1301
1302 dev_dbg(dev, "%s()\n", __func__);
1303
1304 genpd = dev_to_genpd(dev);
1305 if (IS_ERR(genpd))
1306 return;
1307
1308 mutex_lock(&genpd->lock);
1309
1310 run_complete = !genpd->suspend_power_off;
1311 if (--genpd->prepared_count == 0)
1312 genpd->suspend_power_off = false;
1313
1314 mutex_unlock(&genpd->lock);
1315
1316 if (run_complete) {
1317 pm_generic_complete(dev);
6f00ff78 1318 pm_runtime_set_active(dev);
596ba34b 1319 pm_runtime_enable(dev);
af939339 1320 pm_request_idle(dev);
596ba34b
RW
1321 }
1322}
1323
77f827de 1324/**
d47e6464 1325 * genpd_syscore_switch - Switch power during system core suspend or resume.
77f827de
RW
1326 * @dev: Device that normally is marked as "always on" to switch power for.
1327 *
1328 * This routine may only be called during the system core (syscore) suspend or
1329 * resume phase for devices whose "always on" flags are set.
1330 */
d47e6464 1331static void genpd_syscore_switch(struct device *dev, bool suspend)
77f827de
RW
1332{
1333 struct generic_pm_domain *genpd;
1334
1335 genpd = dev_to_genpd(dev);
1336 if (!pm_genpd_present(genpd))
1337 return;
1338
1339 if (suspend) {
1340 genpd->suspended_count++;
1341 pm_genpd_sync_poweroff(genpd);
1342 } else {
1343 pm_genpd_sync_poweron(genpd);
1344 genpd->suspended_count--;
1345 }
1346}
d47e6464
UH
1347
1348void pm_genpd_syscore_poweroff(struct device *dev)
1349{
1350 genpd_syscore_switch(dev, true);
1351}
1352EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1353
1354void pm_genpd_syscore_poweron(struct device *dev)
1355{
1356 genpd_syscore_switch(dev, false);
1357}
1358EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
77f827de 1359
d30d819d 1360#else /* !CONFIG_PM_SLEEP */
596ba34b
RW
1361
1362#define pm_genpd_prepare NULL
1363#define pm_genpd_suspend NULL
0496c8ae 1364#define pm_genpd_suspend_late NULL
596ba34b 1365#define pm_genpd_suspend_noirq NULL
0496c8ae 1366#define pm_genpd_resume_early NULL
596ba34b
RW
1367#define pm_genpd_resume_noirq NULL
1368#define pm_genpd_resume NULL
1369#define pm_genpd_freeze NULL
0496c8ae 1370#define pm_genpd_freeze_late NULL
596ba34b 1371#define pm_genpd_freeze_noirq NULL
0496c8ae 1372#define pm_genpd_thaw_early NULL
596ba34b
RW
1373#define pm_genpd_thaw_noirq NULL
1374#define pm_genpd_thaw NULL
596ba34b 1375#define pm_genpd_restore_noirq NULL
596ba34b
RW
1376#define pm_genpd_complete NULL
1377
1378#endif /* CONFIG_PM_SLEEP */
1379
f104e1e5
UH
1380static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1381 struct generic_pm_domain *genpd,
1382 struct gpd_timing_data *td)
1d5fcfec
RW
1383{
1384 struct generic_pm_domain_data *gpd_data;
3e235685
UH
1385 int ret;
1386
1387 ret = dev_pm_get_subsys_data(dev);
1388 if (ret)
1389 return ERR_PTR(ret);
1d5fcfec
RW
1390
1391 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
3e235685
UH
1392 if (!gpd_data) {
1393 ret = -ENOMEM;
1394 goto err_put;
1395 }
1d5fcfec 1396
f104e1e5
UH
1397 if (td)
1398 gpd_data->td = *td;
1399
1400 gpd_data->base.dev = dev;
1401 gpd_data->need_restore = -1;
1402 gpd_data->td.constraint_changed = true;
1403 gpd_data->td.effective_constraint_ns = -1;
1404 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1405
1406 spin_lock_irq(&dev->power.lock);
1407
1408 if (dev->power.subsys_data->domain_data) {
1409 ret = -EINVAL;
1410 goto err_free;
1411 }
1412
1413 dev->power.subsys_data->domain_data = &gpd_data->base;
1414 dev->pm_domain = &genpd->domain;
1415
1416 spin_unlock_irq(&dev->power.lock);
1417
1d5fcfec 1418 return gpd_data;
3e235685 1419
f104e1e5
UH
1420 err_free:
1421 spin_unlock_irq(&dev->power.lock);
1422 kfree(gpd_data);
3e235685
UH
1423 err_put:
1424 dev_pm_put_subsys_data(dev);
1425 return ERR_PTR(ret);
1d5fcfec
RW
1426}
1427
49d400c7
UH
1428static void genpd_free_dev_data(struct device *dev,
1429 struct generic_pm_domain_data *gpd_data)
1d5fcfec 1430{
f104e1e5
UH
1431 spin_lock_irq(&dev->power.lock);
1432
1433 dev->pm_domain = NULL;
1434 dev->power.subsys_data->domain_data = NULL;
1435
1436 spin_unlock_irq(&dev->power.lock);
1437
1d5fcfec 1438 kfree(gpd_data);
3e235685 1439 dev_pm_put_subsys_data(dev);
1d5fcfec
RW
1440}
1441
f721889f 1442/**
b02c999a 1443 * __pm_genpd_add_device - Add a device to an I/O PM domain.
f721889f
RW
1444 * @genpd: PM domain to add the device to.
1445 * @dev: Device to be added.
b02c999a 1446 * @td: Set of PM QoS timing parameters to attach to the device.
f721889f 1447 */
b02c999a
RW
1448int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1449 struct gpd_timing_data *td)
f721889f 1450{
c0356db7 1451 struct generic_pm_domain_data *gpd_data;
f721889f
RW
1452 int ret = 0;
1453
1454 dev_dbg(dev, "%s()\n", __func__);
1455
1456 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1457 return -EINVAL;
1458
f104e1e5 1459 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
3e235685
UH
1460 if (IS_ERR(gpd_data))
1461 return PTR_ERR(gpd_data);
6ff7bb0d 1462
17b75eca 1463 genpd_acquire_lock(genpd);
f721889f 1464
596ba34b
RW
1465 if (genpd->prepared_count > 0) {
1466 ret = -EAGAIN;
1467 goto out;
1468 }
1469
b472c2fa
UH
1470 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1471 if (ret)
1472 goto out;
d79b6fe1 1473
14b53064
UH
1474 genpd->device_count++;
1475 genpd->max_off_time_changed = true;
1476
1d5fcfec 1477 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
6ff7bb0d 1478
f721889f 1479 out:
17b75eca 1480 genpd_release_lock(genpd);
f721889f 1481
c0356db7
UH
1482 if (ret)
1483 genpd_free_dev_data(dev, gpd_data);
1484 else
1485 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1d5fcfec 1486
f721889f
RW
1487 return ret;
1488}
1489
b5abb085
RW
1490/**
1491 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1492 * @domain_name: Name of the PM domain to add the device to.
1493 * @dev: Device to be added.
1494 * @td: Set of PM QoS timing parameters to attach to the device.
1495 */
1496int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1497 struct gpd_timing_data *td)
1498{
8bc0251d 1499 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
b5abb085
RW
1500}
1501
f721889f
RW
1502/**
1503 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1504 * @genpd: PM domain to remove the device from.
1505 * @dev: Device to be removed.
1506 */
1507int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1508 struct device *dev)
1509{
6ff7bb0d 1510 struct generic_pm_domain_data *gpd_data;
4605ab65 1511 struct pm_domain_data *pdd;
efa69025 1512 int ret = 0;
f721889f
RW
1513
1514 dev_dbg(dev, "%s()\n", __func__);
1515
efa69025
RW
1516 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1517 || IS_ERR_OR_NULL(dev->pm_domain)
1518 || pd_to_genpd(dev->pm_domain) != genpd)
f721889f
RW
1519 return -EINVAL;
1520
c0356db7
UH
1521 /* The above validation also means we have existing domain_data. */
1522 pdd = dev->power.subsys_data->domain_data;
1523 gpd_data = to_gpd_data(pdd);
1524 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1525
17b75eca 1526 genpd_acquire_lock(genpd);
f721889f 1527
596ba34b
RW
1528 if (genpd->prepared_count > 0) {
1529 ret = -EAGAIN;
1530 goto out;
1531 }
1532
6ff7bb0d
RW
1533 genpd->device_count--;
1534 genpd->max_off_time_changed = true;
1535
d79b6fe1 1536 if (genpd->detach_dev)
c16561e8 1537 genpd->detach_dev(genpd, dev);
d79b6fe1 1538
efa69025 1539 list_del_init(&pdd->list_node);
6ff7bb0d
RW
1540
1541 genpd_release_lock(genpd);
1542
c1dbe2fb 1543 genpd_free_dev_data(dev, gpd_data);
1d5fcfec 1544
6ff7bb0d 1545 return 0;
f721889f 1546
596ba34b 1547 out:
17b75eca 1548 genpd_release_lock(genpd);
c0356db7 1549 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
f721889f
RW
1550
1551 return ret;
1552}
1553
1554/**
1555 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1556 * @genpd: Master PM domain to add the subdomain to.
bc0403ff 1557 * @subdomain: Subdomain to be added.
f721889f
RW
1558 */
1559int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
bc0403ff 1560 struct generic_pm_domain *subdomain)
f721889f 1561{
5063ce15 1562 struct gpd_link *link;
f721889f
RW
1563 int ret = 0;
1564
fb7268be
RW
1565 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1566 || genpd == subdomain)
f721889f
RW
1567 return -EINVAL;
1568
17b75eca
RW
1569 start:
1570 genpd_acquire_lock(genpd);
bc0403ff 1571 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
f721889f 1572
bc0403ff
RW
1573 if (subdomain->status != GPD_STATE_POWER_OFF
1574 && subdomain->status != GPD_STATE_ACTIVE) {
1575 mutex_unlock(&subdomain->lock);
17b75eca
RW
1576 genpd_release_lock(genpd);
1577 goto start;
1578 }
1579
1580 if (genpd->status == GPD_STATE_POWER_OFF
bc0403ff 1581 && subdomain->status != GPD_STATE_POWER_OFF) {
f721889f
RW
1582 ret = -EINVAL;
1583 goto out;
1584 }
1585
4fcac10d 1586 list_for_each_entry(link, &genpd->master_links, master_node) {
bc0403ff 1587 if (link->slave == subdomain && link->master == genpd) {
f721889f
RW
1588 ret = -EINVAL;
1589 goto out;
1590 }
1591 }
1592
5063ce15
RW
1593 link = kzalloc(sizeof(*link), GFP_KERNEL);
1594 if (!link) {
1595 ret = -ENOMEM;
1596 goto out;
1597 }
1598 link->master = genpd;
1599 list_add_tail(&link->master_node, &genpd->master_links);
bc0403ff
RW
1600 link->slave = subdomain;
1601 list_add_tail(&link->slave_node, &subdomain->slave_links);
1602 if (subdomain->status != GPD_STATE_POWER_OFF)
c4bb3160 1603 genpd_sd_counter_inc(genpd);
f721889f 1604
f721889f 1605 out:
bc0403ff 1606 mutex_unlock(&subdomain->lock);
17b75eca 1607 genpd_release_lock(genpd);
f721889f
RW
1608
1609 return ret;
1610}
1611
fb7268be
RW
1612/**
1613 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1614 * @master_name: Name of the master PM domain to add the subdomain to.
1615 * @subdomain_name: Name of the subdomain to be added.
1616 */
1617int pm_genpd_add_subdomain_names(const char *master_name,
1618 const char *subdomain_name)
1619{
1620 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1621
1622 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1623 return -EINVAL;
1624
1625 mutex_lock(&gpd_list_lock);
1626 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1627 if (!master && !strcmp(gpd->name, master_name))
1628 master = gpd;
1629
1630 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1631 subdomain = gpd;
1632
1633 if (master && subdomain)
1634 break;
1635 }
1636 mutex_unlock(&gpd_list_lock);
1637
1638 return pm_genpd_add_subdomain(master, subdomain);
1639}
1640
f721889f
RW
1641/**
1642 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1643 * @genpd: Master PM domain to remove the subdomain from.
5063ce15 1644 * @subdomain: Subdomain to be removed.
f721889f
RW
1645 */
1646int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
5063ce15 1647 struct generic_pm_domain *subdomain)
f721889f 1648{
5063ce15 1649 struct gpd_link *link;
f721889f
RW
1650 int ret = -EINVAL;
1651
5063ce15 1652 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
f721889f
RW
1653 return -EINVAL;
1654
17b75eca
RW
1655 start:
1656 genpd_acquire_lock(genpd);
f721889f 1657
5063ce15
RW
1658 list_for_each_entry(link, &genpd->master_links, master_node) {
1659 if (link->slave != subdomain)
f721889f
RW
1660 continue;
1661
1662 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1663
17b75eca
RW
1664 if (subdomain->status != GPD_STATE_POWER_OFF
1665 && subdomain->status != GPD_STATE_ACTIVE) {
1666 mutex_unlock(&subdomain->lock);
1667 genpd_release_lock(genpd);
1668 goto start;
1669 }
1670
5063ce15
RW
1671 list_del(&link->master_node);
1672 list_del(&link->slave_node);
1673 kfree(link);
17b75eca 1674 if (subdomain->status != GPD_STATE_POWER_OFF)
f721889f
RW
1675 genpd_sd_counter_dec(genpd);
1676
1677 mutex_unlock(&subdomain->lock);
1678
1679 ret = 0;
1680 break;
1681 }
1682
17b75eca 1683 genpd_release_lock(genpd);
f721889f
RW
1684
1685 return ret;
1686}
1687
40114447
RW
1688/**
1689 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1690 * @genpd: PM domain to be connected with cpuidle.
1691 * @state: cpuidle state this domain can disable/enable.
1692 *
1693 * Make a PM domain behave as though it contained a CPU core, that is, instead
1694 * of calling its power down routine it will enable the given cpuidle state so
1695 * that the cpuidle subsystem can power it down (if possible and desirable).
1696 */
1697int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
cbc9ef02
RW
1698{
1699 struct cpuidle_driver *cpuidle_drv;
f39cb179 1700 struct gpd_cpuidle_data *cpuidle_data;
cbc9ef02
RW
1701 struct cpuidle_state *idle_state;
1702 int ret = 0;
1703
1704 if (IS_ERR_OR_NULL(genpd) || state < 0)
1705 return -EINVAL;
1706
1707 genpd_acquire_lock(genpd);
1708
f39cb179 1709 if (genpd->cpuidle_data) {
cbc9ef02
RW
1710 ret = -EEXIST;
1711 goto out;
1712 }
f39cb179
UH
1713 cpuidle_data = kzalloc(sizeof(*cpuidle_data), GFP_KERNEL);
1714 if (!cpuidle_data) {
cbc9ef02
RW
1715 ret = -ENOMEM;
1716 goto out;
1717 }
1718 cpuidle_drv = cpuidle_driver_ref();
1719 if (!cpuidle_drv) {
1720 ret = -ENODEV;
debe081a 1721 goto err_drv;
cbc9ef02
RW
1722 }
1723 if (cpuidle_drv->state_count <= state) {
1724 ret = -EINVAL;
1725 goto err;
1726 }
1727 idle_state = &cpuidle_drv->states[state];
1728 if (!idle_state->disabled) {
1729 ret = -EAGAIN;
1730 goto err;
1731 }
f39cb179
UH
1732 cpuidle_data->idle_state = idle_state;
1733 cpuidle_data->saved_exit_latency = idle_state->exit_latency;
1734 genpd->cpuidle_data = cpuidle_data;
cbc9ef02
RW
1735 genpd_recalc_cpu_exit_latency(genpd);
1736
1737 out:
1738 genpd_release_lock(genpd);
1739 return ret;
1740
1741 err:
1742 cpuidle_driver_unref();
debe081a 1743
1744 err_drv:
f39cb179 1745 kfree(cpuidle_data);
cbc9ef02
RW
1746 goto out;
1747}
1748
74a2799a
RW
1749/**
1750 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1751 * @name: Name of the domain to connect to cpuidle.
1752 * @state: cpuidle state this domain can manipulate.
1753 */
1754int pm_genpd_name_attach_cpuidle(const char *name, int state)
1755{
1756 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1757}
1758
40114447
RW
1759/**
1760 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1761 * @genpd: PM domain to remove the cpuidle connection from.
1762 *
1763 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1764 * given PM domain.
1765 */
1766int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
cbc9ef02 1767{
f39cb179 1768 struct gpd_cpuidle_data *cpuidle_data;
cbc9ef02
RW
1769 struct cpuidle_state *idle_state;
1770 int ret = 0;
1771
1772 if (IS_ERR_OR_NULL(genpd))
1773 return -EINVAL;
1774
1775 genpd_acquire_lock(genpd);
1776
f39cb179
UH
1777 cpuidle_data = genpd->cpuidle_data;
1778 if (!cpuidle_data) {
cbc9ef02
RW
1779 ret = -ENODEV;
1780 goto out;
1781 }
f39cb179 1782 idle_state = cpuidle_data->idle_state;
cbc9ef02
RW
1783 if (!idle_state->disabled) {
1784 ret = -EAGAIN;
1785 goto out;
1786 }
f39cb179 1787 idle_state->exit_latency = cpuidle_data->saved_exit_latency;
cbc9ef02 1788 cpuidle_driver_unref();
f39cb179
UH
1789 genpd->cpuidle_data = NULL;
1790 kfree(cpuidle_data);
cbc9ef02
RW
1791
1792 out:
1793 genpd_release_lock(genpd);
1794 return ret;
1795}
1796
74a2799a
RW
1797/**
1798 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1799 * @name: Name of the domain to disconnect cpuidle from.
1800 */
1801int pm_genpd_name_detach_cpuidle(const char *name)
1802{
1803 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1804}
1805
d23b9b00
RW
1806/* Default device callbacks for generic PM domains. */
1807
ecf00475 1808/**
12e10bb6 1809 * pm_genpd_default_save_state - Default "save device state" for PM domains.
ecf00475
RW
1810 * @dev: Device to handle.
1811 */
1812static int pm_genpd_default_save_state(struct device *dev)
1813{
1814 int (*cb)(struct device *__dev);
ecf00475 1815
0b589741
RW
1816 if (dev->type && dev->type->pm)
1817 cb = dev->type->pm->runtime_suspend;
1818 else if (dev->class && dev->class->pm)
1819 cb = dev->class->pm->runtime_suspend;
1820 else if (dev->bus && dev->bus->pm)
1821 cb = dev->bus->pm->runtime_suspend;
1822 else
1823 cb = NULL;
ecf00475 1824
0b589741
RW
1825 if (!cb && dev->driver && dev->driver->pm)
1826 cb = dev->driver->pm->runtime_suspend;
1827
1828 return cb ? cb(dev) : 0;
ecf00475
RW
1829}
1830
1831/**
12e10bb6 1832 * pm_genpd_default_restore_state - Default PM domains "restore device state".
ecf00475
RW
1833 * @dev: Device to handle.
1834 */
1835static int pm_genpd_default_restore_state(struct device *dev)
1836{
1837 int (*cb)(struct device *__dev);
ecf00475 1838
0b589741
RW
1839 if (dev->type && dev->type->pm)
1840 cb = dev->type->pm->runtime_resume;
1841 else if (dev->class && dev->class->pm)
1842 cb = dev->class->pm->runtime_resume;
1843 else if (dev->bus && dev->bus->pm)
1844 cb = dev->bus->pm->runtime_resume;
1845 else
1846 cb = NULL;
ecf00475 1847
0b589741
RW
1848 if (!cb && dev->driver && dev->driver->pm)
1849 cb = dev->driver->pm->runtime_resume;
1850
1851 return cb ? cb(dev) : 0;
ecf00475
RW
1852}
1853
f721889f
RW
1854/**
1855 * pm_genpd_init - Initialize a generic I/O PM domain object.
1856 * @genpd: PM domain object to initialize.
1857 * @gov: PM domain governor to associate with the domain (may be NULL).
1858 * @is_off: Initial value of the domain's power_is_off field.
1859 */
1860void pm_genpd_init(struct generic_pm_domain *genpd,
1861 struct dev_power_governor *gov, bool is_off)
1862{
1863 if (IS_ERR_OR_NULL(genpd))
1864 return;
1865
5063ce15
RW
1866 INIT_LIST_HEAD(&genpd->master_links);
1867 INIT_LIST_HEAD(&genpd->slave_links);
f721889f 1868 INIT_LIST_HEAD(&genpd->dev_list);
f721889f
RW
1869 mutex_init(&genpd->lock);
1870 genpd->gov = gov;
1871 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1872 genpd->in_progress = 0;
c4bb3160 1873 atomic_set(&genpd->sd_count, 0);
17b75eca
RW
1874 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1875 init_waitqueue_head(&genpd->status_wait_queue);
c6d22b37
RW
1876 genpd->poweroff_task = NULL;
1877 genpd->resume_count = 0;
596ba34b 1878 genpd->device_count = 0;
221e9b58 1879 genpd->max_off_time_ns = -1;
6ff7bb0d 1880 genpd->max_off_time_changed = true;
f721889f
RW
1881 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1882 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
596ba34b
RW
1883 genpd->domain.ops.prepare = pm_genpd_prepare;
1884 genpd->domain.ops.suspend = pm_genpd_suspend;
0496c8ae 1885 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
596ba34b
RW
1886 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1887 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
0496c8ae 1888 genpd->domain.ops.resume_early = pm_genpd_resume_early;
596ba34b
RW
1889 genpd->domain.ops.resume = pm_genpd_resume;
1890 genpd->domain.ops.freeze = pm_genpd_freeze;
0496c8ae 1891 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
596ba34b
RW
1892 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1893 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
0496c8ae 1894 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
596ba34b 1895 genpd->domain.ops.thaw = pm_genpd_thaw;
d23b9b00 1896 genpd->domain.ops.poweroff = pm_genpd_suspend;
0496c8ae 1897 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
d23b9b00 1898 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
596ba34b 1899 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
0496c8ae 1900 genpd->domain.ops.restore_early = pm_genpd_resume_early;
d23b9b00 1901 genpd->domain.ops.restore = pm_genpd_resume;
596ba34b 1902 genpd->domain.ops.complete = pm_genpd_complete;
ecf00475
RW
1903 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1904 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
c11f6f5b
UH
1905
1906 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1907 genpd->dev_ops.stop = pm_clk_suspend;
1908 genpd->dev_ops.start = pm_clk_resume;
1909 }
1910
5125bbf3
RW
1911 mutex_lock(&gpd_list_lock);
1912 list_add(&genpd->gpd_list_node, &gpd_list);
1913 mutex_unlock(&gpd_list_lock);
1914}
aa42240a
TF
1915
1916#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1917/*
1918 * Device Tree based PM domain providers.
1919 *
1920 * The code below implements generic device tree based PM domain providers that
1921 * bind device tree nodes with generic PM domains registered in the system.
1922 *
1923 * Any driver that registers generic PM domains and needs to support binding of
1924 * devices to these domains is supposed to register a PM domain provider, which
1925 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1926 *
1927 * Two simple mapping functions have been provided for convenience:
1928 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1929 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1930 * index.
1931 */
1932
1933/**
1934 * struct of_genpd_provider - PM domain provider registration structure
1935 * @link: Entry in global list of PM domain providers
1936 * @node: Pointer to device tree node of PM domain provider
1937 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1938 * into a PM domain.
1939 * @data: context pointer to be passed into @xlate callback
1940 */
1941struct of_genpd_provider {
1942 struct list_head link;
1943 struct device_node *node;
1944 genpd_xlate_t xlate;
1945 void *data;
1946};
1947
1948/* List of registered PM domain providers. */
1949static LIST_HEAD(of_genpd_providers);
1950/* Mutex to protect the list above. */
1951static DEFINE_MUTEX(of_genpd_mutex);
1952
1953/**
1954 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1955 * @genpdspec: OF phandle args to map into a PM domain
1956 * @data: xlate function private data - pointer to struct generic_pm_domain
1957 *
1958 * This is a generic xlate function that can be used to model PM domains that
1959 * have their own device tree nodes. The private data of xlate function needs
1960 * to be a valid pointer to struct generic_pm_domain.
1961 */
1962struct generic_pm_domain *__of_genpd_xlate_simple(
1963 struct of_phandle_args *genpdspec,
1964 void *data)
1965{
1966 if (genpdspec->args_count != 0)
1967 return ERR_PTR(-EINVAL);
1968 return data;
1969}
1970EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1971
1972/**
1973 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1974 * @genpdspec: OF phandle args to map into a PM domain
1975 * @data: xlate function private data - pointer to struct genpd_onecell_data
1976 *
1977 * This is a generic xlate function that can be used to model simple PM domain
1978 * controllers that have one device tree node and provide multiple PM domains.
1979 * A single cell is used as an index into an array of PM domains specified in
1980 * the genpd_onecell_data struct when registering the provider.
1981 */
1982struct generic_pm_domain *__of_genpd_xlate_onecell(
1983 struct of_phandle_args *genpdspec,
1984 void *data)
1985{
1986 struct genpd_onecell_data *genpd_data = data;
1987 unsigned int idx = genpdspec->args[0];
1988
1989 if (genpdspec->args_count != 1)
1990 return ERR_PTR(-EINVAL);
1991
1992 if (idx >= genpd_data->num_domains) {
1993 pr_err("%s: invalid domain index %u\n", __func__, idx);
1994 return ERR_PTR(-EINVAL);
1995 }
1996
1997 if (!genpd_data->domains[idx])
1998 return ERR_PTR(-ENOENT);
1999
2000 return genpd_data->domains[idx];
2001}
2002EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
2003
2004/**
2005 * __of_genpd_add_provider() - Register a PM domain provider for a node
2006 * @np: Device node pointer associated with the PM domain provider.
2007 * @xlate: Callback for decoding PM domain from phandle arguments.
2008 * @data: Context pointer for @xlate callback.
2009 */
2010int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2011 void *data)
2012{
2013 struct of_genpd_provider *cp;
2014
2015 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2016 if (!cp)
2017 return -ENOMEM;
2018
2019 cp->node = of_node_get(np);
2020 cp->data = data;
2021 cp->xlate = xlate;
2022
2023 mutex_lock(&of_genpd_mutex);
2024 list_add(&cp->link, &of_genpd_providers);
2025 mutex_unlock(&of_genpd_mutex);
2026 pr_debug("Added domain provider from %s\n", np->full_name);
2027
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
2031
2032/**
2033 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2034 * @np: Device node pointer associated with the PM domain provider
2035 */
2036void of_genpd_del_provider(struct device_node *np)
2037{
2038 struct of_genpd_provider *cp;
2039
2040 mutex_lock(&of_genpd_mutex);
2041 list_for_each_entry(cp, &of_genpd_providers, link) {
2042 if (cp->node == np) {
2043 list_del(&cp->link);
2044 of_node_put(cp->node);
2045 kfree(cp);
2046 break;
2047 }
2048 }
2049 mutex_unlock(&of_genpd_mutex);
2050}
2051EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2052
2053/**
2054 * of_genpd_get_from_provider() - Look-up PM domain
2055 * @genpdspec: OF phandle args to use for look-up
2056 *
2057 * Looks for a PM domain provider under the node specified by @genpdspec and if
2058 * found, uses xlate function of the provider to map phandle args to a PM
2059 * domain.
2060 *
2061 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2062 * on failure.
2063 */
7496fcbe 2064struct generic_pm_domain *of_genpd_get_from_provider(
aa42240a
TF
2065 struct of_phandle_args *genpdspec)
2066{
2067 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2068 struct of_genpd_provider *provider;
2069
2070 mutex_lock(&of_genpd_mutex);
2071
2072 /* Check if we have such a provider in our array */
2073 list_for_each_entry(provider, &of_genpd_providers, link) {
2074 if (provider->node == genpdspec->np)
2075 genpd = provider->xlate(genpdspec, provider->data);
2076 if (!IS_ERR(genpd))
2077 break;
2078 }
2079
2080 mutex_unlock(&of_genpd_mutex);
2081
2082 return genpd;
2083}
7496fcbe 2084EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
aa42240a
TF
2085
2086/**
2087 * genpd_dev_pm_detach - Detach a device from its PM domain.
2088 * @dev: Device to attach.
2089 * @power_off: Currently not used
2090 *
2091 * Try to locate a corresponding generic PM domain, which the device was
2092 * attached to previously. If such is found, the device is detached from it.
2093 */
2094static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2095{
2096 struct generic_pm_domain *pd = NULL, *gpd;
2097 int ret = 0;
2098
2099 if (!dev->pm_domain)
2100 return;
2101
2102 mutex_lock(&gpd_list_lock);
2103 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2104 if (&gpd->domain == dev->pm_domain) {
2105 pd = gpd;
2106 break;
2107 }
2108 }
2109 mutex_unlock(&gpd_list_lock);
2110
2111 if (!pd)
2112 return;
2113
2114 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2115
2116 while (1) {
2117 ret = pm_genpd_remove_device(pd, dev);
2118 if (ret != -EAGAIN)
2119 break;
2120 cond_resched();
2121 }
2122
2123 if (ret < 0) {
2124 dev_err(dev, "failed to remove from PM domain %s: %d",
2125 pd->name, ret);
2126 return;
2127 }
2128
2129 /* Check if PM domain can be powered off after removing this device. */
2130 genpd_queue_power_off_work(pd);
2131}
2132
632f7ce3
RK
2133static void genpd_dev_pm_sync(struct device *dev)
2134{
2135 struct generic_pm_domain *pd;
2136
2137 pd = dev_to_genpd(dev);
2138 if (IS_ERR(pd))
2139 return;
2140
2141 genpd_queue_power_off_work(pd);
2142}
2143
aa42240a
TF
2144/**
2145 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2146 * @dev: Device to attach.
2147 *
2148 * Parse device's OF node to find a PM domain specifier. If such is found,
2149 * attaches the device to retrieved pm_domain ops.
2150 *
2151 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2152 * backwards compatibility with existing DTBs.
2153 *
2154 * Returns 0 on successfully attached PM domain or negative error code.
2155 */
2156int genpd_dev_pm_attach(struct device *dev)
2157{
2158 struct of_phandle_args pd_args;
2159 struct generic_pm_domain *pd;
2160 int ret;
2161
2162 if (!dev->of_node)
2163 return -ENODEV;
2164
2165 if (dev->pm_domain)
2166 return -EEXIST;
2167
2168 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2169 "#power-domain-cells", 0, &pd_args);
2170 if (ret < 0) {
2171 if (ret != -ENOENT)
2172 return ret;
2173
2174 /*
2175 * Try legacy Samsung-specific bindings
2176 * (for backwards compatibility of DT ABI)
2177 */
2178 pd_args.args_count = 0;
2179 pd_args.np = of_parse_phandle(dev->of_node,
2180 "samsung,power-domain", 0);
2181 if (!pd_args.np)
2182 return -ENOENT;
2183 }
2184
2185 pd = of_genpd_get_from_provider(&pd_args);
2186 if (IS_ERR(pd)) {
2187 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2188 __func__, PTR_ERR(pd));
2189 of_node_put(dev->of_node);
2190 return PTR_ERR(pd);
2191 }
2192
2193 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2194
2195 while (1) {
2196 ret = pm_genpd_add_device(pd, dev);
2197 if (ret != -EAGAIN)
2198 break;
2199 cond_resched();
2200 }
2201
2202 if (ret < 0) {
2203 dev_err(dev, "failed to add to PM domain %s: %d",
2204 pd->name, ret);
2205 of_node_put(dev->of_node);
2206 return ret;
2207 }
2208
2209 dev->pm_domain->detach = genpd_dev_pm_detach;
632f7ce3 2210 dev->pm_domain->sync = genpd_dev_pm_sync;
2ed12769 2211 pm_genpd_poweron(pd);
aa42240a
TF
2212
2213 return 0;
2214}
2215EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
d30d819d 2216#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2bd5306a
MM
2217
2218
2219/*** debugfs support ***/
2220
2221#ifdef CONFIG_PM_ADVANCED_DEBUG
2222#include <linux/pm.h>
2223#include <linux/device.h>
2224#include <linux/debugfs.h>
2225#include <linux/seq_file.h>
2226#include <linux/init.h>
2227#include <linux/kobject.h>
2228static struct dentry *pm_genpd_debugfs_dir;
2229
2230/*
2231 * TODO: This function is a slightly modified version of rtpm_status_show
d30d819d 2232 * from sysfs.c, so generalize it.
2bd5306a 2233 */
2bd5306a
MM
2234static void rtpm_status_str(struct seq_file *s, struct device *dev)
2235{
2236 static const char * const status_lookup[] = {
2237 [RPM_ACTIVE] = "active",
2238 [RPM_RESUMING] = "resuming",
2239 [RPM_SUSPENDED] = "suspended",
2240 [RPM_SUSPENDING] = "suspending"
2241 };
2242 const char *p = "";
2243
2244 if (dev->power.runtime_error)
2245 p = "error";
2246 else if (dev->power.disable_depth)
2247 p = "unsupported";
2248 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2249 p = status_lookup[dev->power.runtime_status];
2250 else
2251 WARN_ON(1);
2252
2253 seq_puts(s, p);
2254}
2bd5306a
MM
2255
2256static int pm_genpd_summary_one(struct seq_file *s,
66a5ca4b 2257 struct generic_pm_domain *genpd)
2bd5306a
MM
2258{
2259 static const char * const status_lookup[] = {
2260 [GPD_STATE_ACTIVE] = "on",
2261 [GPD_STATE_WAIT_MASTER] = "wait-master",
2262 [GPD_STATE_BUSY] = "busy",
2263 [GPD_STATE_REPEAT] = "off-in-progress",
2264 [GPD_STATE_POWER_OFF] = "off"
2265 };
2266 struct pm_domain_data *pm_data;
2267 const char *kobj_path;
2268 struct gpd_link *link;
2269 int ret;
2270
66a5ca4b 2271 ret = mutex_lock_interruptible(&genpd->lock);
2bd5306a
MM
2272 if (ret)
2273 return -ERESTARTSYS;
2274
66a5ca4b 2275 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2bd5306a 2276 goto exit;
66a5ca4b 2277 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
2bd5306a
MM
2278
2279 /*
2280 * Modifications on the list require holding locks on both
2281 * master and slave, so we are safe.
66a5ca4b 2282 * Also genpd->name is immutable.
2bd5306a 2283 */
66a5ca4b 2284 list_for_each_entry(link, &genpd->master_links, master_node) {
2bd5306a 2285 seq_printf(s, "%s", link->slave->name);
66a5ca4b 2286 if (!list_is_last(&link->master_node, &genpd->master_links))
2bd5306a
MM
2287 seq_puts(s, ", ");
2288 }
2289
66a5ca4b 2290 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2bd5306a
MM
2291 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
2292 if (kobj_path == NULL)
2293 continue;
2294
2295 seq_printf(s, "\n %-50s ", kobj_path);
2296 rtpm_status_str(s, pm_data->dev);
2297 kfree(kobj_path);
2298 }
2299
2300 seq_puts(s, "\n");
2301exit:
66a5ca4b 2302 mutex_unlock(&genpd->lock);
2bd5306a
MM
2303
2304 return 0;
2305}
2306
2307static int pm_genpd_summary_show(struct seq_file *s, void *data)
2308{
66a5ca4b 2309 struct generic_pm_domain *genpd;
2bd5306a
MM
2310 int ret = 0;
2311
2312 seq_puts(s, " domain status slaves\n");
2313 seq_puts(s, " /device runtime status\n");
2314 seq_puts(s, "----------------------------------------------------------------------\n");
2315
2316 ret = mutex_lock_interruptible(&gpd_list_lock);
2317 if (ret)
2318 return -ERESTARTSYS;
2319
66a5ca4b
KH
2320 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2321 ret = pm_genpd_summary_one(s, genpd);
2bd5306a
MM
2322 if (ret)
2323 break;
2324 }
2325 mutex_unlock(&gpd_list_lock);
2326
2327 return ret;
2328}
2329
2330static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2331{
2332 return single_open(file, pm_genpd_summary_show, NULL);
2333}
2334
2335static const struct file_operations pm_genpd_summary_fops = {
2336 .open = pm_genpd_summary_open,
2337 .read = seq_read,
2338 .llseek = seq_lseek,
2339 .release = single_release,
2340};
2341
2342static int __init pm_genpd_debug_init(void)
2343{
2344 struct dentry *d;
2345
2346 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2347
2348 if (!pm_genpd_debugfs_dir)
2349 return -ENOMEM;
2350
2351 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2352 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2353 if (!d)
2354 return -ENOMEM;
2355
2356 return 0;
2357}
2358late_initcall(pm_genpd_debug_init);
2359
2360static void __exit pm_genpd_debug_exit(void)
2361{
2362 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2363}
2364__exitcall(pm_genpd_debug_exit);
2365#endif /* CONFIG_PM_ADVANCED_DEBUG */