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