]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/base/power/domain.c
PM / Runtime: Avoid resuming devices again in pm_runtime_force_resume()
[mirror_ubuntu-artful-kernel.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
93af5e93 9#include <linux/delay.h>
f721889f
RW
10#include <linux/kernel.h>
11#include <linux/io.h>
aa42240a 12#include <linux/platform_device.h>
f721889f
RW
13#include <linux/pm_runtime.h>
14#include <linux/pm_domain.h>
6ff7bb0d 15#include <linux/pm_qos.h>
c11f6f5b 16#include <linux/pm_clock.h>
f721889f
RW
17#include <linux/slab.h>
18#include <linux/err.h>
17b75eca
RW
19#include <linux/sched.h>
20#include <linux/suspend.h>
d5e4cbfe
RW
21#include <linux/export.h>
22
aa8e54b5
TV
23#include "power.h"
24
93af5e93
GU
25#define GENPD_RETRY_MAX_MS 250 /* Approximate */
26
d5e4cbfe
RW
27#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
28({ \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
31 \
32 __routine = genpd->dev_ops.callback; \
33 if (__routine) { \
34 __ret = __routine(dev); \
d5e4cbfe
RW
35 } \
36 __ret; \
37})
f721889f 38
5125bbf3
RW
39static LIST_HEAD(gpd_list);
40static DEFINE_MUTEX(gpd_list_lock);
41
446d999c
RK
42/*
43 * Get the generic PM domain for a particular struct device.
44 * This validates the struct device pointer, the PM domain pointer,
45 * and checks that the PM domain pointer is a real generic PM domain.
46 * Any failure results in NULL being returned.
47 */
48struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
49{
50 struct generic_pm_domain *genpd = NULL, *gpd;
51
52 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
53 return NULL;
54
55 mutex_lock(&gpd_list_lock);
56 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
57 if (&gpd->domain == dev->pm_domain) {
58 genpd = gpd;
59 break;
60 }
61 }
62 mutex_unlock(&gpd_list_lock);
63
64 return genpd;
65}
66
67/*
68 * This should only be used where we are certain that the pm_domain
69 * attached to the device is a genpd domain.
70 */
71static struct 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
2b1d88cd 79static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
d5e4cbfe 80{
2b1d88cd 81 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
d5e4cbfe
RW
82}
83
2b1d88cd 84static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
d5e4cbfe 85{
2b1d88cd 86 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
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
a4630c61 105static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
c8f0ea45 106{
fc5cbf0c 107 unsigned int state_idx = genpd->state_idx;
c8f0ea45
GU
108 ktime_t time_start;
109 s64 elapsed_ns;
110 int ret;
111
112 if (!genpd->power_on)
113 return 0;
114
a4630c61
GU
115 if (!timed)
116 return genpd->power_on(genpd);
117
c8f0ea45
GU
118 time_start = ktime_get();
119 ret = genpd->power_on(genpd);
120 if (ret)
121 return ret;
122
123 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
fc5cbf0c 124 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
c8f0ea45
GU
125 return ret;
126
fc5cbf0c 127 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
c8f0ea45 128 genpd->max_off_time_changed = true;
6d7d5c32
RK
129 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
130 genpd->name, "on", elapsed_ns);
c8f0ea45
GU
131
132 return ret;
133}
134
a4630c61 135static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
c8f0ea45 136{
fc5cbf0c 137 unsigned int state_idx = genpd->state_idx;
c8f0ea45
GU
138 ktime_t time_start;
139 s64 elapsed_ns;
140 int ret;
141
142 if (!genpd->power_off)
143 return 0;
144
a4630c61
GU
145 if (!timed)
146 return genpd->power_off(genpd);
147
c8f0ea45
GU
148 time_start = ktime_get();
149 ret = genpd->power_off(genpd);
150 if (ret == -EBUSY)
151 return ret;
152
153 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
fc5cbf0c 154 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
c8f0ea45
GU
155 return ret;
156
fc5cbf0c 157 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
c8f0ea45 158 genpd->max_off_time_changed = true;
6d7d5c32
RK
159 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
160 genpd->name, "off", elapsed_ns);
c8f0ea45
GU
161
162 return ret;
163}
164
29e47e21 165/**
7420aa4f 166 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
a3d09c73 167 * @genpd: PM domain to power off.
29e47e21 168 *
7420aa4f 169 * Queue up the execution of genpd_poweroff() unless it's already been done
29e47e21
UH
170 * before.
171 */
172static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
173{
174 queue_work(pm_wq, &genpd->power_off_work);
175}
176
5248051b 177/**
53af16f7 178 * genpd_poweron - Restore power to a given PM domain and its masters.
5248051b 179 * @genpd: PM domain to power up.
0106ef51 180 * @depth: nesting count for lockdep.
5248051b 181 *
5063ce15 182 * Restore power to @genpd and all of its masters so that it is possible to
5248051b
RW
183 * resume a device belonging to it.
184 */
53af16f7 185static int genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
5248051b 186{
5063ce15 187 struct gpd_link *link;
5248051b
RW
188 int ret = 0;
189
39dd0f23 190 if (genpd->status == GPD_STATE_ACTIVE)
3f241775 191 return 0;
5248051b 192
5063ce15
RW
193 /*
194 * The list is guaranteed not to change while the loop below is being
195 * executed, unless one of the masters' .power_on() callbacks fiddles
196 * with it.
197 */
198 list_for_each_entry(link, &genpd->slave_links, slave_node) {
0106ef51
MS
199 struct generic_pm_domain *master = link->master;
200
201 genpd_sd_counter_inc(master);
202
203 mutex_lock_nested(&master->lock, depth + 1);
53af16f7 204 ret = genpd_poweron(master, depth + 1);
0106ef51 205 mutex_unlock(&master->lock);
5248051b 206
5063ce15 207 if (ret) {
0106ef51 208 genpd_sd_counter_dec(master);
9e08cf42 209 goto err;
5063ce15 210 }
5248051b
RW
211 }
212
a4630c61 213 ret = genpd_power_on(genpd, true);
c8f0ea45
GU
214 if (ret)
215 goto err;
5248051b 216
ba2bbfbf 217 genpd->status = GPD_STATE_ACTIVE;
3f241775 218 return 0;
9e08cf42
RW
219
220 err:
29e47e21
UH
221 list_for_each_entry_continue_reverse(link,
222 &genpd->slave_links,
223 slave_node) {
5063ce15 224 genpd_sd_counter_dec(link->master);
29e47e21
UH
225 genpd_queue_power_off_work(link->master);
226 }
9e08cf42 227
3f241775
RW
228 return ret;
229}
230
6ff7bb0d
RW
231static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
232 unsigned long val, void *ptr)
233{
234 struct generic_pm_domain_data *gpd_data;
235 struct device *dev;
236
237 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
6ff7bb0d 238 dev = gpd_data->base.dev;
6ff7bb0d
RW
239
240 for (;;) {
241 struct generic_pm_domain *genpd;
242 struct pm_domain_data *pdd;
243
244 spin_lock_irq(&dev->power.lock);
245
246 pdd = dev->power.subsys_data ?
247 dev->power.subsys_data->domain_data : NULL;
1d5fcfec 248 if (pdd && pdd->dev) {
6ff7bb0d
RW
249 to_gpd_data(pdd)->td.constraint_changed = true;
250 genpd = dev_to_genpd(dev);
251 } else {
252 genpd = ERR_PTR(-ENODATA);
253 }
254
255 spin_unlock_irq(&dev->power.lock);
256
257 if (!IS_ERR(genpd)) {
258 mutex_lock(&genpd->lock);
259 genpd->max_off_time_changed = true;
260 mutex_unlock(&genpd->lock);
261 }
262
263 dev = dev->parent;
264 if (!dev || dev->power.ignore_children)
265 break;
266 }
267
268 return NOTIFY_DONE;
269}
270
f721889f 271/**
7420aa4f 272 * genpd_poweroff - Remove power from a given PM domain.
f721889f 273 * @genpd: PM domain to power down.
f96b3c4f 274 * @is_async: PM domain is powered down from a scheduled work
f721889f
RW
275 *
276 * If all of the @genpd's devices have been suspended and all of its subdomains
ba2bbfbf 277 * have been powered down, remove power from @genpd.
f721889f 278 */
7420aa4f 279static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
f721889f 280{
4605ab65 281 struct pm_domain_data *pdd;
5063ce15 282 struct gpd_link *link;
ba2bbfbf 283 unsigned int not_suspended = 0;
f721889f 284
c6d22b37
RW
285 /*
286 * Do not try to power off the domain in the following situations:
287 * (1) The domain is already in the "power off" state.
ba2bbfbf 288 * (2) System suspend is in progress.
c6d22b37 289 */
3f241775 290 if (genpd->status == GPD_STATE_POWER_OFF
ba2bbfbf 291 || genpd->prepared_count > 0)
f721889f
RW
292 return 0;
293
c4bb3160 294 if (atomic_read(&genpd->sd_count) > 0)
f721889f
RW
295 return -EBUSY;
296
34b1f762
RW
297 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
298 enum pm_qos_flags_status stat;
299
300 stat = dev_pm_qos_flags(pdd->dev,
301 PM_QOS_FLAG_NO_POWER_OFF
302 | PM_QOS_FLAG_REMOTE_WAKEUP);
303 if (stat > PM_QOS_FLAGS_NONE)
304 return -EBUSY;
305
298cd0f0 306 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
f721889f 307 not_suspended++;
34b1f762 308 }
f721889f 309
f96b3c4f 310 if (not_suspended > 1 || (not_suspended == 1 && is_async))
f721889f
RW
311 return -EBUSY;
312
313 if (genpd->gov && genpd->gov->power_down_ok) {
314 if (!genpd->gov->power_down_ok(&genpd->domain))
315 return -EAGAIN;
316 }
317
3c07cbc4 318 if (genpd->power_off) {
ba2bbfbf
UH
319 int ret;
320
321 if (atomic_read(&genpd->sd_count) > 0)
322 return -EBUSY;
17b75eca 323
3c07cbc4 324 /*
5063ce15 325 * If sd_count > 0 at this point, one of the subdomains hasn't
7420aa4f
UH
326 * managed to call genpd_poweron() for the master yet after
327 * incrementing it. In that case genpd_poweron() will wait
3c07cbc4 328 * for us to drop the lock, so we can call .power_off() and let
7420aa4f 329 * the genpd_poweron() restore power for us (this shouldn't
3c07cbc4
RW
330 * happen very often).
331 */
a4630c61 332 ret = genpd_power_off(genpd, true);
ba2bbfbf
UH
333 if (ret)
334 return ret;
d2805402 335 }
f721889f 336
17b75eca 337 genpd->status = GPD_STATE_POWER_OFF;
221e9b58 338
5063ce15
RW
339 list_for_each_entry(link, &genpd->slave_links, slave_node) {
340 genpd_sd_counter_dec(link->master);
341 genpd_queue_power_off_work(link->master);
342 }
f721889f 343
ba2bbfbf 344 return 0;
f721889f
RW
345}
346
347/**
348 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
349 * @work: Work structure used for scheduling the execution of this function.
350 */
351static void genpd_power_off_work_fn(struct work_struct *work)
352{
353 struct generic_pm_domain *genpd;
354
355 genpd = container_of(work, struct generic_pm_domain, power_off_work);
356
ba2bbfbf 357 mutex_lock(&genpd->lock);
7420aa4f 358 genpd_poweroff(genpd, true);
ba2bbfbf 359 mutex_unlock(&genpd->lock);
f721889f
RW
360}
361
54eeddbf
UH
362/**
363 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
364 * @dev: Device to handle.
365 */
366static int __genpd_runtime_suspend(struct device *dev)
367{
368 int (*cb)(struct device *__dev);
369
370 if (dev->type && dev->type->pm)
371 cb = dev->type->pm->runtime_suspend;
372 else if (dev->class && dev->class->pm)
373 cb = dev->class->pm->runtime_suspend;
374 else if (dev->bus && dev->bus->pm)
375 cb = dev->bus->pm->runtime_suspend;
376 else
377 cb = NULL;
378
379 if (!cb && dev->driver && dev->driver->pm)
380 cb = dev->driver->pm->runtime_suspend;
381
382 return cb ? cb(dev) : 0;
383}
384
385/**
386 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
387 * @dev: Device to handle.
388 */
389static int __genpd_runtime_resume(struct device *dev)
390{
391 int (*cb)(struct device *__dev);
392
393 if (dev->type && dev->type->pm)
394 cb = dev->type->pm->runtime_resume;
395 else if (dev->class && dev->class->pm)
396 cb = dev->class->pm->runtime_resume;
397 else if (dev->bus && dev->bus->pm)
398 cb = dev->bus->pm->runtime_resume;
399 else
400 cb = NULL;
401
402 if (!cb && dev->driver && dev->driver->pm)
403 cb = dev->driver->pm->runtime_resume;
404
405 return cb ? cb(dev) : 0;
406}
407
f721889f 408/**
795bd2e7 409 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
f721889f
RW
410 * @dev: Device to suspend.
411 *
412 * Carry out a runtime suspend of a device under the assumption that its
413 * pm_domain field points to the domain member of an object of type
414 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
415 */
795bd2e7 416static int genpd_runtime_suspend(struct device *dev)
f721889f
RW
417{
418 struct generic_pm_domain *genpd;
9df3921e 419 bool (*suspend_ok)(struct device *__dev);
2b1d88cd 420 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
ffe12855 421 bool runtime_pm = pm_runtime_enabled(dev);
2b1d88cd
UH
422 ktime_t time_start;
423 s64 elapsed_ns;
d5e4cbfe 424 int ret;
f721889f
RW
425
426 dev_dbg(dev, "%s()\n", __func__);
427
5248051b
RW
428 genpd = dev_to_genpd(dev);
429 if (IS_ERR(genpd))
f721889f
RW
430 return -EINVAL;
431
ffe12855
UH
432 /*
433 * A runtime PM centric subsystem/driver may re-use the runtime PM
434 * callbacks for other purposes than runtime PM. In those scenarios
435 * runtime PM is disabled. Under these circumstances, we shall skip
436 * validating/measuring the PM QoS latency.
437 */
9df3921e
UH
438 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
439 if (runtime_pm && suspend_ok && !suspend_ok(dev))
b02c999a
RW
440 return -EBUSY;
441
2b1d88cd 442 /* Measure suspend latency. */
ffe12855
UH
443 if (runtime_pm)
444 time_start = ktime_get();
2b1d88cd 445
54eeddbf 446 ret = __genpd_runtime_suspend(dev);
d5e4cbfe
RW
447 if (ret)
448 return ret;
17b75eca 449
2b1d88cd 450 ret = genpd_stop_dev(genpd, dev);
ba2bbfbf 451 if (ret) {
54eeddbf 452 __genpd_runtime_resume(dev);
ba2bbfbf
UH
453 return ret;
454 }
455
2b1d88cd 456 /* Update suspend latency value if the measured time exceeds it. */
ffe12855
UH
457 if (runtime_pm) {
458 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
459 if (elapsed_ns > td->suspend_latency_ns) {
460 td->suspend_latency_ns = elapsed_ns;
461 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
462 elapsed_ns);
463 genpd->max_off_time_changed = true;
464 td->constraint_changed = true;
465 }
2b1d88cd
UH
466 }
467
0aa2a221
RW
468 /*
469 * If power.irq_safe is set, this routine will be run with interrupts
470 * off, so it can't use mutexes.
471 */
472 if (dev->power.irq_safe)
473 return 0;
474
c6d22b37 475 mutex_lock(&genpd->lock);
7420aa4f 476 genpd_poweroff(genpd, false);
c6d22b37 477 mutex_unlock(&genpd->lock);
f721889f
RW
478
479 return 0;
480}
481
f721889f 482/**
795bd2e7 483 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
f721889f
RW
484 * @dev: Device to resume.
485 *
486 * Carry out a runtime resume of a device under the assumption that its
487 * pm_domain field points to the domain member of an object of type
488 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
489 */
795bd2e7 490static int genpd_runtime_resume(struct device *dev)
f721889f
RW
491{
492 struct generic_pm_domain *genpd;
2b1d88cd 493 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
ffe12855 494 bool runtime_pm = pm_runtime_enabled(dev);
2b1d88cd
UH
495 ktime_t time_start;
496 s64 elapsed_ns;
f721889f 497 int ret;
ba2bbfbf 498 bool timed = true;
f721889f
RW
499
500 dev_dbg(dev, "%s()\n", __func__);
501
5248051b
RW
502 genpd = dev_to_genpd(dev);
503 if (IS_ERR(genpd))
f721889f
RW
504 return -EINVAL;
505
0aa2a221 506 /* If power.irq_safe, the PM domain is never powered off. */
ba2bbfbf
UH
507 if (dev->power.irq_safe) {
508 timed = false;
509 goto out;
510 }
0aa2a221 511
c6d22b37 512 mutex_lock(&genpd->lock);
53af16f7 513 ret = genpd_poweron(genpd, 0);
ba2bbfbf 514 mutex_unlock(&genpd->lock);
c6d22b37 515
ba2bbfbf
UH
516 if (ret)
517 return ret;
c6d22b37 518
ba2bbfbf 519 out:
2b1d88cd 520 /* Measure resume latency. */
ffe12855 521 if (timed && runtime_pm)
2b1d88cd
UH
522 time_start = ktime_get();
523
076395ca
LP
524 ret = genpd_start_dev(genpd, dev);
525 if (ret)
526 goto err_poweroff;
527
54eeddbf 528 ret = __genpd_runtime_resume(dev);
076395ca
LP
529 if (ret)
530 goto err_stop;
2b1d88cd
UH
531
532 /* Update resume latency value if the measured time exceeds it. */
ffe12855 533 if (timed && runtime_pm) {
2b1d88cd
UH
534 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
535 if (elapsed_ns > td->resume_latency_ns) {
536 td->resume_latency_ns = elapsed_ns;
537 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
538 elapsed_ns);
539 genpd->max_off_time_changed = true;
540 td->constraint_changed = true;
541 }
542 }
17b75eca 543
f721889f 544 return 0;
076395ca
LP
545
546err_stop:
547 genpd_stop_dev(genpd, dev);
548err_poweroff:
549 if (!dev->power.irq_safe) {
550 mutex_lock(&genpd->lock);
551 genpd_poweroff(genpd, 0);
552 mutex_unlock(&genpd->lock);
553 }
554
555 return ret;
f721889f
RW
556}
557
39ac5ba5
TB
558static bool pd_ignore_unused;
559static int __init pd_ignore_unused_setup(char *__unused)
560{
561 pd_ignore_unused = true;
562 return 1;
563}
564__setup("pd_ignore_unused", pd_ignore_unused_setup);
565
17f2ae7f 566/**
bb4b72fc 567 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
17f2ae7f 568 */
bb4b72fc 569static int __init genpd_poweroff_unused(void)
17f2ae7f
RW
570{
571 struct generic_pm_domain *genpd;
572
39ac5ba5
TB
573 if (pd_ignore_unused) {
574 pr_warn("genpd: Not disabling unused power domains\n");
bb4b72fc 575 return 0;
39ac5ba5
TB
576 }
577
17f2ae7f
RW
578 mutex_lock(&gpd_list_lock);
579
580 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
581 genpd_queue_power_off_work(genpd);
582
583 mutex_unlock(&gpd_list_lock);
17f2ae7f 584
2fe71dcd
UH
585 return 0;
586}
587late_initcall(genpd_poweroff_unused);
588
596ba34b
RW
589#ifdef CONFIG_PM_SLEEP
590
77f827de
RW
591/**
592 * pm_genpd_present - Check if the given PM domain has been initialized.
593 * @genpd: PM domain to check.
594 */
895b31f3 595static bool pm_genpd_present(const struct generic_pm_domain *genpd)
77f827de 596{
895b31f3 597 const struct generic_pm_domain *gpd;
77f827de
RW
598
599 if (IS_ERR_OR_NULL(genpd))
600 return false;
601
602 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
603 if (gpd == genpd)
604 return true;
605
606 return false;
607}
608
d5e4cbfe
RW
609static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
610 struct device *dev)
611{
612 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
613}
614
596ba34b 615/**
5063ce15 616 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
596ba34b 617 * @genpd: PM domain to power off, if possible.
a4630c61 618 * @timed: True if latency measurements are allowed.
596ba34b
RW
619 *
620 * Check if the given PM domain can be powered off (during system suspend or
5063ce15 621 * hibernation) and do that if so. Also, in that case propagate to its masters.
596ba34b 622 *
77f827de
RW
623 * This function is only called in "noirq" and "syscore" stages of system power
624 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
625 * executed sequentially, so it is guaranteed that it will never run twice in
626 * parallel).
596ba34b 627 */
a4630c61
GU
628static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
629 bool timed)
596ba34b 630{
5063ce15 631 struct gpd_link *link;
596ba34b 632
17b75eca 633 if (genpd->status == GPD_STATE_POWER_OFF)
596ba34b
RW
634 return;
635
c4bb3160
RW
636 if (genpd->suspended_count != genpd->device_count
637 || atomic_read(&genpd->sd_count) > 0)
596ba34b
RW
638 return;
639
fc5cbf0c
AH
640 /* Choose the deepest state when suspending */
641 genpd->state_idx = genpd->state_count - 1;
a4630c61 642 genpd_power_off(genpd, timed);
596ba34b 643
17b75eca 644 genpd->status = GPD_STATE_POWER_OFF;
5063ce15
RW
645
646 list_for_each_entry(link, &genpd->slave_links, slave_node) {
647 genpd_sd_counter_dec(link->master);
a4630c61 648 pm_genpd_sync_poweroff(link->master, timed);
596ba34b
RW
649 }
650}
651
802d8b49
RW
652/**
653 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
654 * @genpd: PM domain to power on.
a4630c61 655 * @timed: True if latency measurements are allowed.
802d8b49 656 *
77f827de
RW
657 * This function is only called in "noirq" and "syscore" stages of system power
658 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
659 * executed sequentially, so it is guaranteed that it will never run twice in
660 * parallel).
802d8b49 661 */
a4630c61
GU
662static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
663 bool timed)
802d8b49
RW
664{
665 struct gpd_link *link;
666
ba2bbfbf 667 if (genpd->status == GPD_STATE_ACTIVE)
802d8b49
RW
668 return;
669
670 list_for_each_entry(link, &genpd->slave_links, slave_node) {
a4630c61 671 pm_genpd_sync_poweron(link->master, timed);
802d8b49
RW
672 genpd_sd_counter_inc(link->master);
673 }
674
a4630c61 675 genpd_power_on(genpd, timed);
802d8b49
RW
676
677 genpd->status = GPD_STATE_ACTIVE;
678}
679
4ecd6e65
RW
680/**
681 * resume_needed - Check whether to resume a device before system suspend.
682 * @dev: Device to check.
683 * @genpd: PM domain the device belongs to.
684 *
685 * There are two cases in which a device that can wake up the system from sleep
686 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
687 * to wake up the system and it has to remain active for this purpose while the
688 * system is in the sleep state and (2) if the device is not enabled to wake up
689 * the system from sleep states and it generally doesn't generate wakeup signals
690 * by itself (those signals are generated on its behalf by other parts of the
691 * system). In the latter case it may be necessary to reconfigure the device's
692 * wakeup settings during system suspend, because it may have been set up to
693 * signal remote wakeup from the system's working state as needed by runtime PM.
694 * Return 'true' in either of the above cases.
695 */
696static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
697{
698 bool active_wakeup;
699
700 if (!device_can_wakeup(dev))
701 return false;
702
d5e4cbfe 703 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
4ecd6e65
RW
704 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
705}
706
596ba34b
RW
707/**
708 * pm_genpd_prepare - Start power transition of a device in a PM domain.
709 * @dev: Device to start the transition of.
710 *
711 * Start a power transition of a device (during a system-wide power transition)
712 * under the assumption that its pm_domain field points to the domain member of
713 * an object of type struct generic_pm_domain representing a PM domain
714 * consisting of I/O devices.
715 */
716static int pm_genpd_prepare(struct device *dev)
717{
718 struct generic_pm_domain *genpd;
b6c10c84 719 int ret;
596ba34b
RW
720
721 dev_dbg(dev, "%s()\n", __func__);
722
723 genpd = dev_to_genpd(dev);
724 if (IS_ERR(genpd))
725 return -EINVAL;
726
17b75eca
RW
727 /*
728 * If a wakeup request is pending for the device, it should be woken up
729 * at this point and a system wakeup event should be reported if it's
730 * set up to wake up the system from sleep states.
731 */
4ecd6e65
RW
732 if (resume_needed(dev, genpd))
733 pm_runtime_resume(dev);
734
ba2bbfbf 735 mutex_lock(&genpd->lock);
596ba34b 736
39dd0f23 737 if (genpd->prepared_count++ == 0)
65533bbf 738 genpd->suspended_count = 0;
17b75eca 739
ba2bbfbf 740 mutex_unlock(&genpd->lock);
596ba34b 741
596ba34b 742 /*
39dd0f23
UH
743 * Even if the PM domain is powered off at this point, we can't expect
744 * it to remain in that state during the entire system PM suspend
745 * phase. Any subsystem/driver for a device in the PM domain, may still
746 * need to serve a request which may require the device to be runtime
747 * resumed and its PM domain to be powered.
748 *
749 * As we are disabling runtime PM at this point, we are preventing the
750 * subsystem/driver to decide themselves. For that reason, we need to
751 * make sure the device is operational as it may be required in some
752 * cases.
596ba34b 753 */
17b75eca 754 pm_runtime_resume(dev);
596ba34b
RW
755 __pm_runtime_disable(dev, false);
756
b6c10c84
RW
757 ret = pm_generic_prepare(dev);
758 if (ret) {
759 mutex_lock(&genpd->lock);
760
39dd0f23 761 genpd->prepared_count--;
b6c10c84
RW
762
763 mutex_unlock(&genpd->lock);
17b75eca 764 pm_runtime_enable(dev);
b6c10c84 765 }
17b75eca 766
b6c10c84 767 return ret;
596ba34b
RW
768}
769
0496c8ae
RW
770/**
771 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
772 * @dev: Device to suspend.
773 *
774 * Stop the device and remove power from the domain if all devices in it have
775 * been stopped.
776 */
777static int pm_genpd_suspend_noirq(struct device *dev)
778{
779 struct generic_pm_domain *genpd;
780
781 dev_dbg(dev, "%s()\n", __func__);
782
783 genpd = dev_to_genpd(dev);
784 if (IS_ERR(genpd))
785 return -EINVAL;
596ba34b 786
39dd0f23 787 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
d4f2d87a
RW
788 return 0;
789
2b1d88cd 790 genpd_stop_dev(genpd, dev);
596ba34b
RW
791
792 /*
793 * Since all of the "noirq" callbacks are executed sequentially, it is
794 * guaranteed that this function will never run twice in parallel for
795 * the same PM domain, so it is not necessary to use locking here.
796 */
797 genpd->suspended_count++;
a4630c61 798 pm_genpd_sync_poweroff(genpd, true);
596ba34b
RW
799
800 return 0;
801}
802
803/**
0496c8ae 804 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
596ba34b
RW
805 * @dev: Device to resume.
806 *
0496c8ae 807 * Restore power to the device's PM domain, if necessary, and start the device.
596ba34b
RW
808 */
809static int pm_genpd_resume_noirq(struct device *dev)
810{
811 struct generic_pm_domain *genpd;
812
813 dev_dbg(dev, "%s()\n", __func__);
814
815 genpd = dev_to_genpd(dev);
816 if (IS_ERR(genpd))
817 return -EINVAL;
818
39dd0f23 819 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
596ba34b
RW
820 return 0;
821
822 /*
823 * Since all of the "noirq" callbacks are executed sequentially, it is
824 * guaranteed that this function will never run twice in parallel for
825 * the same PM domain, so it is not necessary to use locking here.
826 */
a4630c61 827 pm_genpd_sync_poweron(genpd, true);
596ba34b 828 genpd->suspended_count--;
596ba34b 829
2b1d88cd 830 return genpd_start_dev(genpd, dev);
596ba34b
RW
831}
832
0496c8ae
RW
833/**
834 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
596ba34b
RW
835 * @dev: Device to freeze.
836 *
837 * Carry out a late freeze of a device under the assumption that its
838 * pm_domain field points to the domain member of an object of type
839 * struct generic_pm_domain representing a power domain consisting of I/O
840 * devices.
841 */
842static int pm_genpd_freeze_noirq(struct device *dev)
843{
844 struct generic_pm_domain *genpd;
596ba34b
RW
845
846 dev_dbg(dev, "%s()\n", __func__);
847
848 genpd = dev_to_genpd(dev);
849 if (IS_ERR(genpd))
850 return -EINVAL;
851
39dd0f23 852 return genpd_stop_dev(genpd, dev);
0496c8ae 853}
596ba34b 854
0496c8ae
RW
855/**
856 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
857 * @dev: Device to thaw.
858 *
859 * Start the device, unless power has been removed from the domain already
860 * before the system transition.
861 */
862static int pm_genpd_thaw_noirq(struct device *dev)
863{
864 struct generic_pm_domain *genpd;
596ba34b 865
0496c8ae 866 dev_dbg(dev, "%s()\n", __func__);
596ba34b 867
0496c8ae
RW
868 genpd = dev_to_genpd(dev);
869 if (IS_ERR(genpd))
870 return -EINVAL;
871
39dd0f23 872 return genpd_start_dev(genpd, dev);
596ba34b
RW
873}
874
596ba34b 875/**
0496c8ae 876 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
596ba34b
RW
877 * @dev: Device to resume.
878 *
0496c8ae
RW
879 * Make sure the domain will be in the same power state as before the
880 * hibernation the system is resuming from and start the device if necessary.
596ba34b
RW
881 */
882static int pm_genpd_restore_noirq(struct device *dev)
883{
884 struct generic_pm_domain *genpd;
885
886 dev_dbg(dev, "%s()\n", __func__);
887
888 genpd = dev_to_genpd(dev);
889 if (IS_ERR(genpd))
890 return -EINVAL;
891
892 /*
893 * Since all of the "noirq" callbacks are executed sequentially, it is
894 * guaranteed that this function will never run twice in parallel for
895 * the same PM domain, so it is not necessary to use locking here.
65533bbf
RW
896 *
897 * At this point suspended_count == 0 means we are being run for the
898 * first time for the given domain in the present cycle.
596ba34b 899 */
39dd0f23 900 if (genpd->suspended_count++ == 0)
596ba34b 901 /*
65533bbf 902 * The boot kernel might put the domain into arbitrary state,
802d8b49
RW
903 * so make it appear as powered off to pm_genpd_sync_poweron(),
904 * so that it tries to power it on in case it was really off.
596ba34b 905 */
65533bbf 906 genpd->status = GPD_STATE_POWER_OFF;
18dd2ece 907
a4630c61 908 pm_genpd_sync_poweron(genpd, true);
596ba34b 909
2b1d88cd 910 return genpd_start_dev(genpd, dev);
596ba34b
RW
911}
912
913/**
914 * pm_genpd_complete - Complete power transition of a device in a power domain.
915 * @dev: Device to complete the transition of.
916 *
917 * Complete a power transition of a device (during a system-wide power
918 * transition) under the assumption that its pm_domain field points to the
919 * domain member of an object of type struct generic_pm_domain representing
920 * a power domain consisting of I/O devices.
921 */
922static void pm_genpd_complete(struct device *dev)
923{
924 struct generic_pm_domain *genpd;
596ba34b
RW
925
926 dev_dbg(dev, "%s()\n", __func__);
927
928 genpd = dev_to_genpd(dev);
929 if (IS_ERR(genpd))
930 return;
931
932 mutex_lock(&genpd->lock);
933
39dd0f23 934 genpd->prepared_count--;
596ba34b
RW
935
936 mutex_unlock(&genpd->lock);
937
39dd0f23
UH
938 pm_generic_complete(dev);
939 pm_runtime_set_active(dev);
940 pm_runtime_enable(dev);
596ba34b
RW
941}
942
77f827de 943/**
d47e6464 944 * genpd_syscore_switch - Switch power during system core suspend or resume.
77f827de
RW
945 * @dev: Device that normally is marked as "always on" to switch power for.
946 *
947 * This routine may only be called during the system core (syscore) suspend or
948 * resume phase for devices whose "always on" flags are set.
949 */
d47e6464 950static void genpd_syscore_switch(struct device *dev, bool suspend)
77f827de
RW
951{
952 struct generic_pm_domain *genpd;
953
954 genpd = dev_to_genpd(dev);
955 if (!pm_genpd_present(genpd))
956 return;
957
958 if (suspend) {
959 genpd->suspended_count++;
a4630c61 960 pm_genpd_sync_poweroff(genpd, false);
77f827de 961 } else {
a4630c61 962 pm_genpd_sync_poweron(genpd, false);
77f827de
RW
963 genpd->suspended_count--;
964 }
965}
d47e6464
UH
966
967void pm_genpd_syscore_poweroff(struct device *dev)
968{
969 genpd_syscore_switch(dev, true);
970}
971EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
972
973void pm_genpd_syscore_poweron(struct device *dev)
974{
975 genpd_syscore_switch(dev, false);
976}
977EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
77f827de 978
d30d819d 979#else /* !CONFIG_PM_SLEEP */
596ba34b
RW
980
981#define pm_genpd_prepare NULL
596ba34b
RW
982#define pm_genpd_suspend_noirq NULL
983#define pm_genpd_resume_noirq NULL
596ba34b
RW
984#define pm_genpd_freeze_noirq NULL
985#define pm_genpd_thaw_noirq NULL
596ba34b 986#define pm_genpd_restore_noirq NULL
596ba34b
RW
987#define pm_genpd_complete NULL
988
989#endif /* CONFIG_PM_SLEEP */
990
f104e1e5
UH
991static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
992 struct generic_pm_domain *genpd,
993 struct gpd_timing_data *td)
1d5fcfec
RW
994{
995 struct generic_pm_domain_data *gpd_data;
3e235685
UH
996 int ret;
997
998 ret = dev_pm_get_subsys_data(dev);
999 if (ret)
1000 return ERR_PTR(ret);
1d5fcfec
RW
1001
1002 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
3e235685
UH
1003 if (!gpd_data) {
1004 ret = -ENOMEM;
1005 goto err_put;
1006 }
1d5fcfec 1007
f104e1e5
UH
1008 if (td)
1009 gpd_data->td = *td;
1010
1011 gpd_data->base.dev = dev;
f104e1e5
UH
1012 gpd_data->td.constraint_changed = true;
1013 gpd_data->td.effective_constraint_ns = -1;
1014 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1015
1016 spin_lock_irq(&dev->power.lock);
1017
1018 if (dev->power.subsys_data->domain_data) {
1019 ret = -EINVAL;
1020 goto err_free;
1021 }
1022
1023 dev->power.subsys_data->domain_data = &gpd_data->base;
f104e1e5
UH
1024
1025 spin_unlock_irq(&dev->power.lock);
1026
989561de
TV
1027 dev_pm_domain_set(dev, &genpd->domain);
1028
1d5fcfec 1029 return gpd_data;
3e235685 1030
f104e1e5
UH
1031 err_free:
1032 spin_unlock_irq(&dev->power.lock);
1033 kfree(gpd_data);
3e235685
UH
1034 err_put:
1035 dev_pm_put_subsys_data(dev);
1036 return ERR_PTR(ret);
1d5fcfec
RW
1037}
1038
49d400c7
UH
1039static void genpd_free_dev_data(struct device *dev,
1040 struct generic_pm_domain_data *gpd_data)
1d5fcfec 1041{
989561de
TV
1042 dev_pm_domain_set(dev, NULL);
1043
f104e1e5
UH
1044 spin_lock_irq(&dev->power.lock);
1045
f104e1e5
UH
1046 dev->power.subsys_data->domain_data = NULL;
1047
1048 spin_unlock_irq(&dev->power.lock);
1049
1d5fcfec 1050 kfree(gpd_data);
3e235685 1051 dev_pm_put_subsys_data(dev);
1d5fcfec
RW
1052}
1053
f721889f 1054/**
b02c999a 1055 * __pm_genpd_add_device - Add a device to an I/O PM domain.
f721889f
RW
1056 * @genpd: PM domain to add the device to.
1057 * @dev: Device to be added.
b02c999a 1058 * @td: Set of PM QoS timing parameters to attach to the device.
f721889f 1059 */
b02c999a
RW
1060int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1061 struct gpd_timing_data *td)
f721889f 1062{
c0356db7 1063 struct generic_pm_domain_data *gpd_data;
f721889f
RW
1064 int ret = 0;
1065
1066 dev_dbg(dev, "%s()\n", __func__);
1067
1068 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1069 return -EINVAL;
1070
f104e1e5 1071 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
3e235685
UH
1072 if (IS_ERR(gpd_data))
1073 return PTR_ERR(gpd_data);
6ff7bb0d 1074
ba2bbfbf 1075 mutex_lock(&genpd->lock);
f721889f 1076
596ba34b
RW
1077 if (genpd->prepared_count > 0) {
1078 ret = -EAGAIN;
1079 goto out;
1080 }
1081
b472c2fa
UH
1082 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1083 if (ret)
1084 goto out;
d79b6fe1 1085
14b53064
UH
1086 genpd->device_count++;
1087 genpd->max_off_time_changed = true;
1088
1d5fcfec 1089 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
6ff7bb0d 1090
f721889f 1091 out:
ba2bbfbf 1092 mutex_unlock(&genpd->lock);
f721889f 1093
c0356db7
UH
1094 if (ret)
1095 genpd_free_dev_data(dev, gpd_data);
1096 else
1097 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1d5fcfec 1098
f721889f
RW
1099 return ret;
1100}
24c96dc7 1101EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
f721889f
RW
1102
1103/**
1104 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1105 * @genpd: PM domain to remove the device from.
1106 * @dev: Device to be removed.
1107 */
1108int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1109 struct device *dev)
1110{
6ff7bb0d 1111 struct generic_pm_domain_data *gpd_data;
4605ab65 1112 struct pm_domain_data *pdd;
efa69025 1113 int ret = 0;
f721889f
RW
1114
1115 dev_dbg(dev, "%s()\n", __func__);
1116
df6a0d6f 1117 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
f721889f
RW
1118 return -EINVAL;
1119
c0356db7
UH
1120 /* The above validation also means we have existing domain_data. */
1121 pdd = dev->power.subsys_data->domain_data;
1122 gpd_data = to_gpd_data(pdd);
1123 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1124
ba2bbfbf 1125 mutex_lock(&genpd->lock);
f721889f 1126
596ba34b
RW
1127 if (genpd->prepared_count > 0) {
1128 ret = -EAGAIN;
1129 goto out;
1130 }
1131
6ff7bb0d
RW
1132 genpd->device_count--;
1133 genpd->max_off_time_changed = true;
1134
d79b6fe1 1135 if (genpd->detach_dev)
c16561e8 1136 genpd->detach_dev(genpd, dev);
d79b6fe1 1137
efa69025 1138 list_del_init(&pdd->list_node);
6ff7bb0d 1139
ba2bbfbf 1140 mutex_unlock(&genpd->lock);
6ff7bb0d 1141
c1dbe2fb 1142 genpd_free_dev_data(dev, gpd_data);
1d5fcfec 1143
6ff7bb0d 1144 return 0;
f721889f 1145
596ba34b 1146 out:
ba2bbfbf 1147 mutex_unlock(&genpd->lock);
c0356db7 1148 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
f721889f
RW
1149
1150 return ret;
1151}
24c96dc7 1152EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
f721889f
RW
1153
1154/**
1155 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1156 * @genpd: Master PM domain to add the subdomain to.
bc0403ff 1157 * @subdomain: Subdomain to be added.
f721889f
RW
1158 */
1159int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
bc0403ff 1160 struct generic_pm_domain *subdomain)
f721889f 1161{
2547923d 1162 struct gpd_link *link, *itr;
f721889f
RW
1163 int ret = 0;
1164
fb7268be
RW
1165 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1166 || genpd == subdomain)
f721889f
RW
1167 return -EINVAL;
1168
2547923d
LI
1169 link = kzalloc(sizeof(*link), GFP_KERNEL);
1170 if (!link)
1171 return -ENOMEM;
1172
cdb300a0
UH
1173 mutex_lock(&subdomain->lock);
1174 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
f721889f 1175
17b75eca 1176 if (genpd->status == GPD_STATE_POWER_OFF
bc0403ff 1177 && subdomain->status != GPD_STATE_POWER_OFF) {
f721889f
RW
1178 ret = -EINVAL;
1179 goto out;
1180 }
1181
2547923d
LI
1182 list_for_each_entry(itr, &genpd->master_links, master_node) {
1183 if (itr->slave == subdomain && itr->master == genpd) {
f721889f
RW
1184 ret = -EINVAL;
1185 goto out;
1186 }
1187 }
1188
5063ce15
RW
1189 link->master = genpd;
1190 list_add_tail(&link->master_node, &genpd->master_links);
bc0403ff
RW
1191 link->slave = subdomain;
1192 list_add_tail(&link->slave_node, &subdomain->slave_links);
1193 if (subdomain->status != GPD_STATE_POWER_OFF)
c4bb3160 1194 genpd_sd_counter_inc(genpd);
f721889f 1195
f721889f 1196 out:
ba2bbfbf 1197 mutex_unlock(&genpd->lock);
cdb300a0 1198 mutex_unlock(&subdomain->lock);
2547923d
LI
1199 if (ret)
1200 kfree(link);
f721889f
RW
1201 return ret;
1202}
d60ee966 1203EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
f721889f
RW
1204
1205/**
1206 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1207 * @genpd: Master PM domain to remove the subdomain from.
5063ce15 1208 * @subdomain: Subdomain to be removed.
f721889f
RW
1209 */
1210int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
5063ce15 1211 struct generic_pm_domain *subdomain)
f721889f 1212{
5063ce15 1213 struct gpd_link *link;
f721889f
RW
1214 int ret = -EINVAL;
1215
5063ce15 1216 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
f721889f
RW
1217 return -EINVAL;
1218
cdb300a0
UH
1219 mutex_lock(&subdomain->lock);
1220 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
f721889f 1221
beda5fc1 1222 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
30e7a65b
JH
1223 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1224 subdomain->name);
1225 ret = -EBUSY;
1226 goto out;
1227 }
1228
5063ce15
RW
1229 list_for_each_entry(link, &genpd->master_links, master_node) {
1230 if (link->slave != subdomain)
f721889f
RW
1231 continue;
1232
5063ce15
RW
1233 list_del(&link->master_node);
1234 list_del(&link->slave_node);
1235 kfree(link);
17b75eca 1236 if (subdomain->status != GPD_STATE_POWER_OFF)
f721889f
RW
1237 genpd_sd_counter_dec(genpd);
1238
f721889f
RW
1239 ret = 0;
1240 break;
1241 }
1242
30e7a65b 1243out:
ba2bbfbf 1244 mutex_unlock(&genpd->lock);
cdb300a0 1245 mutex_unlock(&subdomain->lock);
f721889f
RW
1246
1247 return ret;
1248}
d60ee966 1249EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
f721889f
RW
1250
1251/**
1252 * pm_genpd_init - Initialize a generic I/O PM domain object.
1253 * @genpd: PM domain object to initialize.
1254 * @gov: PM domain governor to associate with the domain (may be NULL).
1255 * @is_off: Initial value of the domain's power_is_off field.
1256 */
1257void pm_genpd_init(struct generic_pm_domain *genpd,
1258 struct dev_power_governor *gov, bool is_off)
1259{
1260 if (IS_ERR_OR_NULL(genpd))
1261 return;
1262
5063ce15
RW
1263 INIT_LIST_HEAD(&genpd->master_links);
1264 INIT_LIST_HEAD(&genpd->slave_links);
f721889f 1265 INIT_LIST_HEAD(&genpd->dev_list);
f721889f
RW
1266 mutex_init(&genpd->lock);
1267 genpd->gov = gov;
1268 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
c4bb3160 1269 atomic_set(&genpd->sd_count, 0);
17b75eca 1270 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
596ba34b 1271 genpd->device_count = 0;
221e9b58 1272 genpd->max_off_time_ns = -1;
6ff7bb0d 1273 genpd->max_off_time_changed = true;
795bd2e7
UH
1274 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1275 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
596ba34b 1276 genpd->domain.ops.prepare = pm_genpd_prepare;
80018853
UH
1277 genpd->domain.ops.suspend = pm_generic_suspend;
1278 genpd->domain.ops.suspend_late = pm_generic_suspend_late;
596ba34b
RW
1279 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1280 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
80018853
UH
1281 genpd->domain.ops.resume_early = pm_generic_resume_early;
1282 genpd->domain.ops.resume = pm_generic_resume;
1283 genpd->domain.ops.freeze = pm_generic_freeze;
1284 genpd->domain.ops.freeze_late = pm_generic_freeze_late;
596ba34b
RW
1285 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1286 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
80018853
UH
1287 genpd->domain.ops.thaw_early = pm_generic_thaw_early;
1288 genpd->domain.ops.thaw = pm_generic_thaw;
1289 genpd->domain.ops.poweroff = pm_generic_poweroff;
1290 genpd->domain.ops.poweroff_late = pm_generic_poweroff_late;
d23b9b00 1291 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
596ba34b 1292 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
80018853
UH
1293 genpd->domain.ops.restore_early = pm_generic_restore_early;
1294 genpd->domain.ops.restore = pm_generic_restore;
596ba34b 1295 genpd->domain.ops.complete = pm_genpd_complete;
c11f6f5b
UH
1296
1297 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1298 genpd->dev_ops.stop = pm_clk_suspend;
1299 genpd->dev_ops.start = pm_clk_resume;
1300 }
1301
fc5cbf0c
AH
1302 if (genpd->state_idx >= GENPD_MAX_NUM_STATES) {
1303 pr_warn("Initial state index out of bounds.\n");
1304 genpd->state_idx = GENPD_MAX_NUM_STATES - 1;
1305 }
1306
1307 if (genpd->state_count > GENPD_MAX_NUM_STATES) {
1308 pr_warn("Limiting states to %d\n", GENPD_MAX_NUM_STATES);
1309 genpd->state_count = GENPD_MAX_NUM_STATES;
1310 }
1311
1312 /* Use only one "off" state if there were no states declared */
90e63452 1313 if (genpd->state_count == 0)
fc5cbf0c 1314 genpd->state_count = 1;
fc5cbf0c 1315
5125bbf3
RW
1316 mutex_lock(&gpd_list_lock);
1317 list_add(&genpd->gpd_list_node, &gpd_list);
1318 mutex_unlock(&gpd_list_lock);
1319}
be5ed55d 1320EXPORT_SYMBOL_GPL(pm_genpd_init);
aa42240a
TF
1321
1322#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1323/*
1324 * Device Tree based PM domain providers.
1325 *
1326 * The code below implements generic device tree based PM domain providers that
1327 * bind device tree nodes with generic PM domains registered in the system.
1328 *
1329 * Any driver that registers generic PM domains and needs to support binding of
1330 * devices to these domains is supposed to register a PM domain provider, which
1331 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1332 *
1333 * Two simple mapping functions have been provided for convenience:
1334 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1335 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1336 * index.
1337 */
1338
1339/**
1340 * struct of_genpd_provider - PM domain provider registration structure
1341 * @link: Entry in global list of PM domain providers
1342 * @node: Pointer to device tree node of PM domain provider
1343 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1344 * into a PM domain.
1345 * @data: context pointer to be passed into @xlate callback
1346 */
1347struct of_genpd_provider {
1348 struct list_head link;
1349 struct device_node *node;
1350 genpd_xlate_t xlate;
1351 void *data;
1352};
1353
1354/* List of registered PM domain providers. */
1355static LIST_HEAD(of_genpd_providers);
1356/* Mutex to protect the list above. */
1357static DEFINE_MUTEX(of_genpd_mutex);
1358
1359/**
1360 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1361 * @genpdspec: OF phandle args to map into a PM domain
1362 * @data: xlate function private data - pointer to struct generic_pm_domain
1363 *
1364 * This is a generic xlate function that can be used to model PM domains that
1365 * have their own device tree nodes. The private data of xlate function needs
1366 * to be a valid pointer to struct generic_pm_domain.
1367 */
1368struct generic_pm_domain *__of_genpd_xlate_simple(
1369 struct of_phandle_args *genpdspec,
1370 void *data)
1371{
1372 if (genpdspec->args_count != 0)
1373 return ERR_PTR(-EINVAL);
1374 return data;
1375}
1376EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1377
1378/**
1379 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1380 * @genpdspec: OF phandle args to map into a PM domain
1381 * @data: xlate function private data - pointer to struct genpd_onecell_data
1382 *
1383 * This is a generic xlate function that can be used to model simple PM domain
1384 * controllers that have one device tree node and provide multiple PM domains.
1385 * A single cell is used as an index into an array of PM domains specified in
1386 * the genpd_onecell_data struct when registering the provider.
1387 */
1388struct generic_pm_domain *__of_genpd_xlate_onecell(
1389 struct of_phandle_args *genpdspec,
1390 void *data)
1391{
1392 struct genpd_onecell_data *genpd_data = data;
1393 unsigned int idx = genpdspec->args[0];
1394
1395 if (genpdspec->args_count != 1)
1396 return ERR_PTR(-EINVAL);
1397
1398 if (idx >= genpd_data->num_domains) {
1399 pr_err("%s: invalid domain index %u\n", __func__, idx);
1400 return ERR_PTR(-EINVAL);
1401 }
1402
1403 if (!genpd_data->domains[idx])
1404 return ERR_PTR(-ENOENT);
1405
1406 return genpd_data->domains[idx];
1407}
1408EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1409
1410/**
1411 * __of_genpd_add_provider() - Register a PM domain provider for a node
1412 * @np: Device node pointer associated with the PM domain provider.
1413 * @xlate: Callback for decoding PM domain from phandle arguments.
1414 * @data: Context pointer for @xlate callback.
1415 */
1416int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1417 void *data)
1418{
1419 struct of_genpd_provider *cp;
1420
1421 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1422 if (!cp)
1423 return -ENOMEM;
1424
1425 cp->node = of_node_get(np);
1426 cp->data = data;
1427 cp->xlate = xlate;
1428
1429 mutex_lock(&of_genpd_mutex);
1430 list_add(&cp->link, &of_genpd_providers);
1431 mutex_unlock(&of_genpd_mutex);
1432 pr_debug("Added domain provider from %s\n", np->full_name);
1433
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1437
1438/**
1439 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1440 * @np: Device node pointer associated with the PM domain provider
1441 */
1442void of_genpd_del_provider(struct device_node *np)
1443{
1444 struct of_genpd_provider *cp;
1445
1446 mutex_lock(&of_genpd_mutex);
1447 list_for_each_entry(cp, &of_genpd_providers, link) {
1448 if (cp->node == np) {
1449 list_del(&cp->link);
1450 of_node_put(cp->node);
1451 kfree(cp);
1452 break;
1453 }
1454 }
1455 mutex_unlock(&of_genpd_mutex);
1456}
1457EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1458
1459/**
1460 * of_genpd_get_from_provider() - Look-up PM domain
1461 * @genpdspec: OF phandle args to use for look-up
1462 *
1463 * Looks for a PM domain provider under the node specified by @genpdspec and if
1464 * found, uses xlate function of the provider to map phandle args to a PM
1465 * domain.
1466 *
1467 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1468 * on failure.
1469 */
7496fcbe 1470struct generic_pm_domain *of_genpd_get_from_provider(
aa42240a
TF
1471 struct of_phandle_args *genpdspec)
1472{
1473 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1474 struct of_genpd_provider *provider;
1475
41795a8a
JH
1476 if (!genpdspec)
1477 return ERR_PTR(-EINVAL);
1478
aa42240a
TF
1479 mutex_lock(&of_genpd_mutex);
1480
1481 /* Check if we have such a provider in our array */
1482 list_for_each_entry(provider, &of_genpd_providers, link) {
1483 if (provider->node == genpdspec->np)
1484 genpd = provider->xlate(genpdspec, provider->data);
1485 if (!IS_ERR(genpd))
1486 break;
1487 }
1488
1489 mutex_unlock(&of_genpd_mutex);
1490
1491 return genpd;
1492}
7496fcbe 1493EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
aa42240a
TF
1494
1495/**
1496 * genpd_dev_pm_detach - Detach a device from its PM domain.
8bb6944e 1497 * @dev: Device to detach.
aa42240a
TF
1498 * @power_off: Currently not used
1499 *
1500 * Try to locate a corresponding generic PM domain, which the device was
1501 * attached to previously. If such is found, the device is detached from it.
1502 */
1503static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1504{
446d999c 1505 struct generic_pm_domain *pd;
93af5e93 1506 unsigned int i;
aa42240a
TF
1507 int ret = 0;
1508
446d999c 1509 pd = pm_genpd_lookup_dev(dev);
aa42240a
TF
1510 if (!pd)
1511 return;
1512
1513 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1514
93af5e93 1515 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
aa42240a
TF
1516 ret = pm_genpd_remove_device(pd, dev);
1517 if (ret != -EAGAIN)
1518 break;
93af5e93
GU
1519
1520 mdelay(i);
aa42240a
TF
1521 cond_resched();
1522 }
1523
1524 if (ret < 0) {
1525 dev_err(dev, "failed to remove from PM domain %s: %d",
1526 pd->name, ret);
1527 return;
1528 }
1529
1530 /* Check if PM domain can be powered off after removing this device. */
1531 genpd_queue_power_off_work(pd);
1532}
1533
632f7ce3
RK
1534static void genpd_dev_pm_sync(struct device *dev)
1535{
1536 struct generic_pm_domain *pd;
1537
1538 pd = dev_to_genpd(dev);
1539 if (IS_ERR(pd))
1540 return;
1541
1542 genpd_queue_power_off_work(pd);
1543}
1544
aa42240a
TF
1545/**
1546 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1547 * @dev: Device to attach.
1548 *
1549 * Parse device's OF node to find a PM domain specifier. If such is found,
1550 * attaches the device to retrieved pm_domain ops.
1551 *
1552 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1553 * backwards compatibility with existing DTBs.
1554 *
311fa6ad
JH
1555 * Returns 0 on successfully attached PM domain or negative error code. Note
1556 * that if a power-domain exists for the device, but it cannot be found or
1557 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1558 * probed and to re-try again later.
aa42240a
TF
1559 */
1560int genpd_dev_pm_attach(struct device *dev)
1561{
1562 struct of_phandle_args pd_args;
1563 struct generic_pm_domain *pd;
93af5e93 1564 unsigned int i;
aa42240a
TF
1565 int ret;
1566
1567 if (!dev->of_node)
1568 return -ENODEV;
1569
1570 if (dev->pm_domain)
1571 return -EEXIST;
1572
1573 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1574 "#power-domain-cells", 0, &pd_args);
1575 if (ret < 0) {
1576 if (ret != -ENOENT)
1577 return ret;
1578
1579 /*
1580 * Try legacy Samsung-specific bindings
1581 * (for backwards compatibility of DT ABI)
1582 */
1583 pd_args.args_count = 0;
1584 pd_args.np = of_parse_phandle(dev->of_node,
1585 "samsung,power-domain", 0);
1586 if (!pd_args.np)
1587 return -ENOENT;
1588 }
1589
1590 pd = of_genpd_get_from_provider(&pd_args);
265e2cf6 1591 of_node_put(pd_args.np);
aa42240a
TF
1592 if (IS_ERR(pd)) {
1593 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1594 __func__, PTR_ERR(pd));
311fa6ad 1595 return -EPROBE_DEFER;
aa42240a
TF
1596 }
1597
1598 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1599
93af5e93 1600 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
aa42240a
TF
1601 ret = pm_genpd_add_device(pd, dev);
1602 if (ret != -EAGAIN)
1603 break;
93af5e93
GU
1604
1605 mdelay(i);
aa42240a
TF
1606 cond_resched();
1607 }
1608
1609 if (ret < 0) {
1610 dev_err(dev, "failed to add to PM domain %s: %d",
1611 pd->name, ret);
311fa6ad 1612 goto out;
aa42240a
TF
1613 }
1614
1615 dev->pm_domain->detach = genpd_dev_pm_detach;
632f7ce3 1616 dev->pm_domain->sync = genpd_dev_pm_sync;
aa42240a 1617
53af16f7
UH
1618 mutex_lock(&pd->lock);
1619 ret = genpd_poweron(pd, 0);
1620 mutex_unlock(&pd->lock);
311fa6ad
JH
1621out:
1622 return ret ? -EPROBE_DEFER : 0;
aa42240a
TF
1623}
1624EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
d30d819d 1625#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2bd5306a
MM
1626
1627
1628/*** debugfs support ***/
1629
1630#ifdef CONFIG_PM_ADVANCED_DEBUG
1631#include <linux/pm.h>
1632#include <linux/device.h>
1633#include <linux/debugfs.h>
1634#include <linux/seq_file.h>
1635#include <linux/init.h>
1636#include <linux/kobject.h>
1637static struct dentry *pm_genpd_debugfs_dir;
1638
1639/*
1640 * TODO: This function is a slightly modified version of rtpm_status_show
d30d819d 1641 * from sysfs.c, so generalize it.
2bd5306a 1642 */
2bd5306a
MM
1643static void rtpm_status_str(struct seq_file *s, struct device *dev)
1644{
1645 static const char * const status_lookup[] = {
1646 [RPM_ACTIVE] = "active",
1647 [RPM_RESUMING] = "resuming",
1648 [RPM_SUSPENDED] = "suspended",
1649 [RPM_SUSPENDING] = "suspending"
1650 };
1651 const char *p = "";
1652
1653 if (dev->power.runtime_error)
1654 p = "error";
1655 else if (dev->power.disable_depth)
1656 p = "unsupported";
1657 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1658 p = status_lookup[dev->power.runtime_status];
1659 else
1660 WARN_ON(1);
1661
1662 seq_puts(s, p);
1663}
2bd5306a
MM
1664
1665static int pm_genpd_summary_one(struct seq_file *s,
66a5ca4b 1666 struct generic_pm_domain *genpd)
2bd5306a
MM
1667{
1668 static const char * const status_lookup[] = {
1669 [GPD_STATE_ACTIVE] = "on",
2bd5306a
MM
1670 [GPD_STATE_POWER_OFF] = "off"
1671 };
1672 struct pm_domain_data *pm_data;
1673 const char *kobj_path;
1674 struct gpd_link *link;
6954d432 1675 char state[16];
2bd5306a
MM
1676 int ret;
1677
66a5ca4b 1678 ret = mutex_lock_interruptible(&genpd->lock);
2bd5306a
MM
1679 if (ret)
1680 return -ERESTARTSYS;
1681
66a5ca4b 1682 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2bd5306a 1683 goto exit;
fc5cbf0c 1684 if (genpd->status == GPD_STATE_POWER_OFF)
0ba554e4 1685 snprintf(state, sizeof(state), "%s-%u",
6954d432 1686 status_lookup[genpd->status], genpd->state_idx);
fc5cbf0c 1687 else
6954d432
GU
1688 snprintf(state, sizeof(state), "%s",
1689 status_lookup[genpd->status]);
1690 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2bd5306a
MM
1691
1692 /*
1693 * Modifications on the list require holding locks on both
1694 * master and slave, so we are safe.
66a5ca4b 1695 * Also genpd->name is immutable.
2bd5306a 1696 */
66a5ca4b 1697 list_for_each_entry(link, &genpd->master_links, master_node) {
2bd5306a 1698 seq_printf(s, "%s", link->slave->name);
66a5ca4b 1699 if (!list_is_last(&link->master_node, &genpd->master_links))
2bd5306a
MM
1700 seq_puts(s, ", ");
1701 }
1702
66a5ca4b 1703 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2bd5306a
MM
1704 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1705 if (kobj_path == NULL)
1706 continue;
1707
1708 seq_printf(s, "\n %-50s ", kobj_path);
1709 rtpm_status_str(s, pm_data->dev);
1710 kfree(kobj_path);
1711 }
1712
1713 seq_puts(s, "\n");
1714exit:
66a5ca4b 1715 mutex_unlock(&genpd->lock);
2bd5306a
MM
1716
1717 return 0;
1718}
1719
1720static int pm_genpd_summary_show(struct seq_file *s, void *data)
1721{
66a5ca4b 1722 struct generic_pm_domain *genpd;
2bd5306a
MM
1723 int ret = 0;
1724
15dec67a
GU
1725 seq_puts(s, "domain status slaves\n");
1726 seq_puts(s, " /device runtime status\n");
2bd5306a
MM
1727 seq_puts(s, "----------------------------------------------------------------------\n");
1728
1729 ret = mutex_lock_interruptible(&gpd_list_lock);
1730 if (ret)
1731 return -ERESTARTSYS;
1732
66a5ca4b
KH
1733 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1734 ret = pm_genpd_summary_one(s, genpd);
2bd5306a
MM
1735 if (ret)
1736 break;
1737 }
1738 mutex_unlock(&gpd_list_lock);
1739
1740 return ret;
1741}
1742
1743static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1744{
1745 return single_open(file, pm_genpd_summary_show, NULL);
1746}
1747
1748static const struct file_operations pm_genpd_summary_fops = {
1749 .open = pm_genpd_summary_open,
1750 .read = seq_read,
1751 .llseek = seq_lseek,
1752 .release = single_release,
1753};
1754
1755static int __init pm_genpd_debug_init(void)
1756{
1757 struct dentry *d;
1758
1759 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1760
1761 if (!pm_genpd_debugfs_dir)
1762 return -ENOMEM;
1763
1764 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1765 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1766 if (!d)
1767 return -ENOMEM;
1768
1769 return 0;
1770}
1771late_initcall(pm_genpd_debug_init);
1772
1773static void __exit pm_genpd_debug_exit(void)
1774{
1775 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1776}
1777__exitcall(pm_genpd_debug_exit);
1778#endif /* CONFIG_PM_ADVANCED_DEBUG */