PM / Domains: Make failing pm_genpd_prepare() clean up properly
[GitHub/mt8127/android_kernel_alcatel_ttab.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
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/io.h>
12#include <linux/pm_runtime.h>
13#include <linux/pm_domain.h>
14#include <linux/slab.h>
15#include <linux/err.h>
16
5248051b
RW
17#ifdef CONFIG_PM
18
19static struct generic_pm_domain *dev_to_genpd(struct device *dev)
20{
21 if (IS_ERR_OR_NULL(dev->pm_domain))
22 return ERR_PTR(-EINVAL);
23
596ba34b 24 return pd_to_genpd(dev->pm_domain);
5248051b 25}
f721889f
RW
26
27static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
28{
29 if (!WARN_ON(genpd->sd_count == 0))
30 genpd->sd_count--;
31}
32
5248051b
RW
33/**
34 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
35 * @genpd: PM domain to power up.
36 *
37 * Restore power to @genpd and all of its parents so that it is possible to
38 * resume a device belonging to it.
39 */
18b4f3f5 40int pm_genpd_poweron(struct generic_pm_domain *genpd)
5248051b
RW
41{
42 int ret = 0;
43
44 start:
45 if (genpd->parent)
46 mutex_lock(&genpd->parent->lock);
47 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
48
596ba34b
RW
49 if (!genpd->power_is_off
50 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
5248051b
RW
51 goto out;
52
53 if (genpd->parent && genpd->parent->power_is_off) {
54 mutex_unlock(&genpd->lock);
55 mutex_unlock(&genpd->parent->lock);
56
57 ret = pm_genpd_poweron(genpd->parent);
58 if (ret)
59 return ret;
60
61 goto start;
62 }
63
64 if (genpd->power_on) {
65 int ret = genpd->power_on(genpd);
66 if (ret)
67 goto out;
68 }
69
70 genpd->power_is_off = false;
71 if (genpd->parent)
72 genpd->parent->sd_count++;
73
74 out:
75 mutex_unlock(&genpd->lock);
76 if (genpd->parent)
77 mutex_unlock(&genpd->parent->lock);
78
79 return ret;
80}
81
82#endif /* CONFIG_PM */
83
84#ifdef CONFIG_PM_RUNTIME
85
f721889f
RW
86/**
87 * __pm_genpd_save_device - Save the pre-suspend state of a device.
88 * @dle: Device list entry of the device to save the state of.
89 * @genpd: PM domain the device belongs to.
90 */
91static int __pm_genpd_save_device(struct dev_list_entry *dle,
92 struct generic_pm_domain *genpd)
93{
94 struct device *dev = dle->dev;
95 struct device_driver *drv = dev->driver;
96 int ret = 0;
97
98 if (dle->need_restore)
99 return 0;
100
101 if (drv && drv->pm && drv->pm->runtime_suspend) {
102 if (genpd->start_device)
103 genpd->start_device(dev);
104
105 ret = drv->pm->runtime_suspend(dev);
106
107 if (genpd->stop_device)
108 genpd->stop_device(dev);
109 }
110
111 if (!ret)
112 dle->need_restore = true;
113
114 return ret;
115}
116
117/**
118 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
119 * @dle: Device list entry of the device to restore the state of.
120 * @genpd: PM domain the device belongs to.
121 */
122static void __pm_genpd_restore_device(struct dev_list_entry *dle,
123 struct generic_pm_domain *genpd)
124{
125 struct device *dev = dle->dev;
126 struct device_driver *drv = dev->driver;
127
128 if (!dle->need_restore)
129 return;
130
131 if (drv && drv->pm && drv->pm->runtime_resume) {
132 if (genpd->start_device)
133 genpd->start_device(dev);
134
135 drv->pm->runtime_resume(dev);
136
137 if (genpd->stop_device)
138 genpd->stop_device(dev);
139 }
140
141 dle->need_restore = false;
142}
143
144/**
145 * pm_genpd_poweroff - Remove power from a given PM domain.
146 * @genpd: PM domain to power down.
147 *
148 * If all of the @genpd's devices have been suspended and all of its subdomains
149 * have been powered down, run the runtime suspend callbacks provided by all of
150 * the @genpd's devices' drivers and remove power from @genpd.
151 */
152static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
153{
154 struct generic_pm_domain *parent;
155 struct dev_list_entry *dle;
156 unsigned int not_suspended;
157 int ret;
158
596ba34b 159 if (genpd->power_is_off || genpd->prepared_count > 0)
f721889f
RW
160 return 0;
161
162 if (genpd->sd_count > 0)
163 return -EBUSY;
164
165 not_suspended = 0;
166 list_for_each_entry(dle, &genpd->dev_list, node)
167 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
168 not_suspended++;
169
170 if (not_suspended > genpd->in_progress)
171 return -EBUSY;
172
173 if (genpd->gov && genpd->gov->power_down_ok) {
174 if (!genpd->gov->power_down_ok(&genpd->domain))
175 return -EAGAIN;
176 }
177
178 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
179 ret = __pm_genpd_save_device(dle, genpd);
180 if (ret)
181 goto err_dev;
182 }
183
184 if (genpd->power_off)
185 genpd->power_off(genpd);
186
187 genpd->power_is_off = true;
188
189 parent = genpd->parent;
190 if (parent) {
191 genpd_sd_counter_dec(parent);
192 if (parent->sd_count == 0)
193 queue_work(pm_wq, &parent->power_off_work);
194 }
195
196 return 0;
197
198 err_dev:
199 list_for_each_entry_continue(dle, &genpd->dev_list, node)
200 __pm_genpd_restore_device(dle, genpd);
201
202 return ret;
203}
204
205/**
206 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
207 * @work: Work structure used for scheduling the execution of this function.
208 */
209static void genpd_power_off_work_fn(struct work_struct *work)
210{
211 struct generic_pm_domain *genpd;
212
213 genpd = container_of(work, struct generic_pm_domain, power_off_work);
214
215 if (genpd->parent)
216 mutex_lock(&genpd->parent->lock);
217 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
218 pm_genpd_poweroff(genpd);
219 mutex_unlock(&genpd->lock);
220 if (genpd->parent)
221 mutex_unlock(&genpd->parent->lock);
222}
223
224/**
225 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
226 * @dev: Device to suspend.
227 *
228 * Carry out a runtime suspend of a device under the assumption that its
229 * pm_domain field points to the domain member of an object of type
230 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
231 */
232static int pm_genpd_runtime_suspend(struct device *dev)
233{
234 struct generic_pm_domain *genpd;
235
236 dev_dbg(dev, "%s()\n", __func__);
237
5248051b
RW
238 genpd = dev_to_genpd(dev);
239 if (IS_ERR(genpd))
f721889f
RW
240 return -EINVAL;
241
f721889f
RW
242 if (genpd->parent)
243 mutex_lock(&genpd->parent->lock);
244 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
245
246 if (genpd->stop_device) {
247 int ret = genpd->stop_device(dev);
248 if (ret)
249 goto out;
250 }
251 genpd->in_progress++;
252 pm_genpd_poweroff(genpd);
253 genpd->in_progress--;
254
255 out:
256 mutex_unlock(&genpd->lock);
257 if (genpd->parent)
258 mutex_unlock(&genpd->parent->lock);
259
260 return 0;
261}
262
596ba34b
RW
263/**
264 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
265 * @dev: Device to resume.
266 * @genpd: PM domain the device belongs to.
267 */
268static void __pm_genpd_runtime_resume(struct device *dev,
269 struct generic_pm_domain *genpd)
270{
271 struct dev_list_entry *dle;
272
273 list_for_each_entry(dle, &genpd->dev_list, node) {
274 if (dle->dev == dev) {
275 __pm_genpd_restore_device(dle, genpd);
276 break;
277 }
278 }
279
280 if (genpd->start_device)
281 genpd->start_device(dev);
282}
283
f721889f
RW
284/**
285 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
286 * @dev: Device to resume.
287 *
288 * Carry out a runtime resume of a device under the assumption that its
289 * pm_domain field points to the domain member of an object of type
290 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
291 */
292static int pm_genpd_runtime_resume(struct device *dev)
293{
294 struct generic_pm_domain *genpd;
f721889f
RW
295 int ret;
296
297 dev_dbg(dev, "%s()\n", __func__);
298
5248051b
RW
299 genpd = dev_to_genpd(dev);
300 if (IS_ERR(genpd))
f721889f
RW
301 return -EINVAL;
302
f721889f
RW
303 ret = pm_genpd_poweron(genpd);
304 if (ret)
305 return ret;
306
307 mutex_lock(&genpd->lock);
596ba34b 308 __pm_genpd_runtime_resume(dev, genpd);
f721889f
RW
309 mutex_unlock(&genpd->lock);
310
311 return 0;
312}
313
314#else
315
316static inline void genpd_power_off_work_fn(struct work_struct *work) {}
596ba34b
RW
317static inline void __pm_genpd_runtime_resume(struct device *dev,
318 struct generic_pm_domain *genpd) {}
f721889f
RW
319
320#define pm_genpd_runtime_suspend NULL
321#define pm_genpd_runtime_resume NULL
322
323#endif /* CONFIG_PM_RUNTIME */
324
596ba34b
RW
325#ifdef CONFIG_PM_SLEEP
326
327/**
328 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
329 * @genpd: PM domain to power off, if possible.
330 *
331 * Check if the given PM domain can be powered off (during system suspend or
332 * hibernation) and do that if so. Also, in that case propagate to its parent.
333 *
334 * This function is only called in "noirq" stages of system power transitions,
335 * so it need not acquire locks (all of the "noirq" callbacks are executed
336 * sequentially, so it is guaranteed that it will never run twice in parallel).
337 */
338static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
339{
340 struct generic_pm_domain *parent = genpd->parent;
341
342 if (genpd->power_is_off)
343 return;
344
345 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
346 return;
347
348 if (genpd->power_off)
349 genpd->power_off(genpd);
350
351 genpd->power_is_off = true;
352 if (parent) {
353 genpd_sd_counter_dec(parent);
354 pm_genpd_sync_poweroff(parent);
355 }
356}
357
358/**
359 * pm_genpd_prepare - Start power transition of a device in a PM domain.
360 * @dev: Device to start the transition of.
361 *
362 * Start a power transition of a device (during a system-wide power transition)
363 * under the assumption that its pm_domain field points to the domain member of
364 * an object of type struct generic_pm_domain representing a PM domain
365 * consisting of I/O devices.
366 */
367static int pm_genpd_prepare(struct device *dev)
368{
369 struct generic_pm_domain *genpd;
b6c10c84 370 int ret;
596ba34b
RW
371
372 dev_dbg(dev, "%s()\n", __func__);
373
374 genpd = dev_to_genpd(dev);
375 if (IS_ERR(genpd))
376 return -EINVAL;
377
378 mutex_lock(&genpd->lock);
379
380 if (genpd->prepared_count++ == 0)
381 genpd->suspend_power_off = genpd->power_is_off;
382
383 if (genpd->suspend_power_off) {
384 mutex_unlock(&genpd->lock);
385 return 0;
386 }
387
388 /*
389 * If the device is in the (runtime) "suspended" state, call
390 * .start_device() for it, if defined.
391 */
392 if (pm_runtime_suspended(dev))
393 __pm_genpd_runtime_resume(dev, genpd);
394
395 /*
396 * Do not check if runtime resume is pending at this point, because it
397 * has been taken care of already and if pm_genpd_poweron() ran at this
398 * point as a result of the check, it would deadlock.
399 */
400 __pm_runtime_disable(dev, false);
401
402 mutex_unlock(&genpd->lock);
403
b6c10c84
RW
404 ret = pm_generic_prepare(dev);
405 if (ret) {
406 mutex_lock(&genpd->lock);
407
408 if (--genpd->prepared_count == 0)
409 genpd->suspend_power_off = false;
410
411 mutex_unlock(&genpd->lock);
412 }
413 return ret;
596ba34b
RW
414}
415
416/**
417 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
418 * @dev: Device to suspend.
419 *
420 * Suspend a device under the assumption that its pm_domain field points to the
421 * domain member of an object of type struct generic_pm_domain representing
422 * a PM domain consisting of I/O devices.
423 */
424static int pm_genpd_suspend(struct device *dev)
425{
426 struct generic_pm_domain *genpd;
427
428 dev_dbg(dev, "%s()\n", __func__);
429
430 genpd = dev_to_genpd(dev);
431 if (IS_ERR(genpd))
432 return -EINVAL;
433
434 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
435}
436
437/**
438 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
439 * @dev: Device to suspend.
440 *
441 * Carry out a late suspend of a device under the assumption that its
442 * pm_domain field points to the domain member of an object of type
443 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
444 */
445static int pm_genpd_suspend_noirq(struct device *dev)
446{
447 struct generic_pm_domain *genpd;
448 int ret;
449
450 dev_dbg(dev, "%s()\n", __func__);
451
452 genpd = dev_to_genpd(dev);
453 if (IS_ERR(genpd))
454 return -EINVAL;
455
456 if (genpd->suspend_power_off)
457 return 0;
458
459 ret = pm_generic_suspend_noirq(dev);
460 if (ret)
461 return ret;
462
d4f2d87a
RW
463 if (device_may_wakeup(dev)
464 && genpd->active_wakeup && genpd->active_wakeup(dev))
465 return 0;
466
596ba34b
RW
467 if (genpd->stop_device)
468 genpd->stop_device(dev);
469
470 /*
471 * Since all of the "noirq" callbacks are executed sequentially, it is
472 * guaranteed that this function will never run twice in parallel for
473 * the same PM domain, so it is not necessary to use locking here.
474 */
475 genpd->suspended_count++;
476 pm_genpd_sync_poweroff(genpd);
477
478 return 0;
479}
480
481/**
482 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
483 * @dev: Device to resume.
484 *
485 * Carry out an early resume of a device under the assumption that its
486 * pm_domain field points to the domain member of an object of type
487 * struct generic_pm_domain representing a power domain consisting of I/O
488 * devices.
489 */
490static int pm_genpd_resume_noirq(struct device *dev)
491{
492 struct generic_pm_domain *genpd;
493
494 dev_dbg(dev, "%s()\n", __func__);
495
496 genpd = dev_to_genpd(dev);
497 if (IS_ERR(genpd))
498 return -EINVAL;
499
500 if (genpd->suspend_power_off)
501 return 0;
502
503 /*
504 * Since all of the "noirq" callbacks are executed sequentially, it is
505 * guaranteed that this function will never run twice in parallel for
506 * the same PM domain, so it is not necessary to use locking here.
507 */
508 pm_genpd_poweron(genpd);
509 genpd->suspended_count--;
510 if (genpd->start_device)
511 genpd->start_device(dev);
512
513 return pm_generic_resume_noirq(dev);
514}
515
516/**
517 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
518 * @dev: Device to resume.
519 *
520 * Resume a device under the assumption that its pm_domain field points to the
521 * domain member of an object of type struct generic_pm_domain representing
522 * a power domain consisting of I/O devices.
523 */
524static int pm_genpd_resume(struct device *dev)
525{
526 struct generic_pm_domain *genpd;
527
528 dev_dbg(dev, "%s()\n", __func__);
529
530 genpd = dev_to_genpd(dev);
531 if (IS_ERR(genpd))
532 return -EINVAL;
533
534 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
535}
536
537/**
538 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
539 * @dev: Device to freeze.
540 *
541 * Freeze a device under the assumption that its pm_domain field points to the
542 * domain member of an object of type struct generic_pm_domain representing
543 * a power domain consisting of I/O devices.
544 */
545static int pm_genpd_freeze(struct device *dev)
546{
547 struct generic_pm_domain *genpd;
548
549 dev_dbg(dev, "%s()\n", __func__);
550
551 genpd = dev_to_genpd(dev);
552 if (IS_ERR(genpd))
553 return -EINVAL;
554
555 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
556}
557
558/**
559 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
560 * @dev: Device to freeze.
561 *
562 * Carry out a late freeze of a device under the assumption that its
563 * pm_domain field points to the domain member of an object of type
564 * struct generic_pm_domain representing a power domain consisting of I/O
565 * devices.
566 */
567static int pm_genpd_freeze_noirq(struct device *dev)
568{
569 struct generic_pm_domain *genpd;
570 int ret;
571
572 dev_dbg(dev, "%s()\n", __func__);
573
574 genpd = dev_to_genpd(dev);
575 if (IS_ERR(genpd))
576 return -EINVAL;
577
578 if (genpd->suspend_power_off)
579 return 0;
580
581 ret = pm_generic_freeze_noirq(dev);
582 if (ret)
583 return ret;
584
585 if (genpd->stop_device)
586 genpd->stop_device(dev);
587
588 return 0;
589}
590
591/**
592 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
593 * @dev: Device to thaw.
594 *
595 * Carry out an early thaw of a device under the assumption that its
596 * pm_domain field points to the domain member of an object of type
597 * struct generic_pm_domain representing a power domain consisting of I/O
598 * devices.
599 */
600static int pm_genpd_thaw_noirq(struct device *dev)
601{
602 struct generic_pm_domain *genpd;
603
604 dev_dbg(dev, "%s()\n", __func__);
605
606 genpd = dev_to_genpd(dev);
607 if (IS_ERR(genpd))
608 return -EINVAL;
609
610 if (genpd->suspend_power_off)
611 return 0;
612
613 if (genpd->start_device)
614 genpd->start_device(dev);
615
616 return pm_generic_thaw_noirq(dev);
617}
618
619/**
620 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
621 * @dev: Device to thaw.
622 *
623 * Thaw a device under the assumption that its pm_domain field points to the
624 * domain member of an object of type struct generic_pm_domain representing
625 * a power domain consisting of I/O devices.
626 */
627static int pm_genpd_thaw(struct device *dev)
628{
629 struct generic_pm_domain *genpd;
630
631 dev_dbg(dev, "%s()\n", __func__);
632
633 genpd = dev_to_genpd(dev);
634 if (IS_ERR(genpd))
635 return -EINVAL;
636
637 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
638}
639
640/**
641 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
642 * @dev: Device to suspend.
643 *
644 * Power off a device under the assumption that its pm_domain field points to
645 * the domain member of an object of type struct generic_pm_domain representing
646 * a PM domain consisting of I/O devices.
647 */
648static int pm_genpd_dev_poweroff(struct device *dev)
649{
650 struct generic_pm_domain *genpd;
651
652 dev_dbg(dev, "%s()\n", __func__);
653
654 genpd = dev_to_genpd(dev);
655 if (IS_ERR(genpd))
656 return -EINVAL;
657
658 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
659}
660
661/**
662 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
663 * @dev: Device to suspend.
664 *
665 * Carry out a late powering off of a device under the assumption that its
666 * pm_domain field points to the domain member of an object of type
667 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
668 */
669static int pm_genpd_dev_poweroff_noirq(struct device *dev)
670{
671 struct generic_pm_domain *genpd;
672 int ret;
673
674 dev_dbg(dev, "%s()\n", __func__);
675
676 genpd = dev_to_genpd(dev);
677 if (IS_ERR(genpd))
678 return -EINVAL;
679
680 if (genpd->suspend_power_off)
681 return 0;
682
683 ret = pm_generic_poweroff_noirq(dev);
684 if (ret)
685 return ret;
686
d4f2d87a
RW
687 if (device_may_wakeup(dev)
688 && genpd->active_wakeup && genpd->active_wakeup(dev))
689 return 0;
690
596ba34b
RW
691 if (genpd->stop_device)
692 genpd->stop_device(dev);
693
694 /*
695 * Since all of the "noirq" callbacks are executed sequentially, it is
696 * guaranteed that this function will never run twice in parallel for
697 * the same PM domain, so it is not necessary to use locking here.
698 */
699 genpd->suspended_count++;
700 pm_genpd_sync_poweroff(genpd);
701
702 return 0;
703}
704
705/**
706 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
707 * @dev: Device to resume.
708 *
709 * Carry out an early restore of a device under the assumption that its
710 * pm_domain field points to the domain member of an object of type
711 * struct generic_pm_domain representing a power domain consisting of I/O
712 * devices.
713 */
714static int pm_genpd_restore_noirq(struct device *dev)
715{
716 struct generic_pm_domain *genpd;
717
718 dev_dbg(dev, "%s()\n", __func__);
719
720 genpd = dev_to_genpd(dev);
721 if (IS_ERR(genpd))
722 return -EINVAL;
723
724 /*
725 * Since all of the "noirq" callbacks are executed sequentially, it is
726 * guaranteed that this function will never run twice in parallel for
727 * the same PM domain, so it is not necessary to use locking here.
728 */
729 genpd->power_is_off = true;
730 if (genpd->suspend_power_off) {
731 /*
732 * The boot kernel might put the domain into the power on state,
733 * so make sure it really is powered off.
734 */
735 if (genpd->power_off)
736 genpd->power_off(genpd);
737 return 0;
738 }
739
740 pm_genpd_poweron(genpd);
741 genpd->suspended_count--;
742 if (genpd->start_device)
743 genpd->start_device(dev);
744
745 return pm_generic_restore_noirq(dev);
746}
747
748/**
749 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
750 * @dev: Device to resume.
751 *
752 * Restore a device under the assumption that its pm_domain field points to the
753 * domain member of an object of type struct generic_pm_domain representing
754 * a power domain consisting of I/O devices.
755 */
756static int pm_genpd_restore(struct device *dev)
757{
758 struct generic_pm_domain *genpd;
759
760 dev_dbg(dev, "%s()\n", __func__);
761
762 genpd = dev_to_genpd(dev);
763 if (IS_ERR(genpd))
764 return -EINVAL;
765
766 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
767}
768
769/**
770 * pm_genpd_complete - Complete power transition of a device in a power domain.
771 * @dev: Device to complete the transition of.
772 *
773 * Complete a power transition of a device (during a system-wide power
774 * transition) under the assumption that its pm_domain field points to the
775 * domain member of an object of type struct generic_pm_domain representing
776 * a power domain consisting of I/O devices.
777 */
778static void pm_genpd_complete(struct device *dev)
779{
780 struct generic_pm_domain *genpd;
781 bool run_complete;
782
783 dev_dbg(dev, "%s()\n", __func__);
784
785 genpd = dev_to_genpd(dev);
786 if (IS_ERR(genpd))
787 return;
788
789 mutex_lock(&genpd->lock);
790
791 run_complete = !genpd->suspend_power_off;
792 if (--genpd->prepared_count == 0)
793 genpd->suspend_power_off = false;
794
795 mutex_unlock(&genpd->lock);
796
797 if (run_complete) {
798 pm_generic_complete(dev);
6f00ff78 799 pm_runtime_set_active(dev);
596ba34b 800 pm_runtime_enable(dev);
6f00ff78 801 pm_runtime_idle(dev);
596ba34b
RW
802 }
803}
804
805#else
806
807#define pm_genpd_prepare NULL
808#define pm_genpd_suspend NULL
809#define pm_genpd_suspend_noirq NULL
810#define pm_genpd_resume_noirq NULL
811#define pm_genpd_resume NULL
812#define pm_genpd_freeze NULL
813#define pm_genpd_freeze_noirq NULL
814#define pm_genpd_thaw_noirq NULL
815#define pm_genpd_thaw NULL
816#define pm_genpd_dev_poweroff_noirq NULL
817#define pm_genpd_dev_poweroff NULL
818#define pm_genpd_restore_noirq NULL
819#define pm_genpd_restore NULL
820#define pm_genpd_complete NULL
821
822#endif /* CONFIG_PM_SLEEP */
823
f721889f
RW
824/**
825 * pm_genpd_add_device - Add a device to an I/O PM domain.
826 * @genpd: PM domain to add the device to.
827 * @dev: Device to be added.
828 */
829int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
830{
831 struct dev_list_entry *dle;
832 int ret = 0;
833
834 dev_dbg(dev, "%s()\n", __func__);
835
836 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
837 return -EINVAL;
838
839 mutex_lock(&genpd->lock);
840
841 if (genpd->power_is_off) {
842 ret = -EINVAL;
843 goto out;
844 }
845
596ba34b
RW
846 if (genpd->prepared_count > 0) {
847 ret = -EAGAIN;
848 goto out;
849 }
850
f721889f
RW
851 list_for_each_entry(dle, &genpd->dev_list, node)
852 if (dle->dev == dev) {
853 ret = -EINVAL;
854 goto out;
855 }
856
857 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
858 if (!dle) {
859 ret = -ENOMEM;
860 goto out;
861 }
862
863 dle->dev = dev;
864 dle->need_restore = false;
865 list_add_tail(&dle->node, &genpd->dev_list);
596ba34b 866 genpd->device_count++;
f721889f
RW
867
868 spin_lock_irq(&dev->power.lock);
869 dev->pm_domain = &genpd->domain;
870 spin_unlock_irq(&dev->power.lock);
871
872 out:
873 mutex_unlock(&genpd->lock);
874
875 return ret;
876}
877
878/**
879 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
880 * @genpd: PM domain to remove the device from.
881 * @dev: Device to be removed.
882 */
883int pm_genpd_remove_device(struct generic_pm_domain *genpd,
884 struct device *dev)
885{
886 struct dev_list_entry *dle;
887 int ret = -EINVAL;
888
889 dev_dbg(dev, "%s()\n", __func__);
890
891 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
892 return -EINVAL;
893
894 mutex_lock(&genpd->lock);
895
596ba34b
RW
896 if (genpd->prepared_count > 0) {
897 ret = -EAGAIN;
898 goto out;
899 }
900
f721889f
RW
901 list_for_each_entry(dle, &genpd->dev_list, node) {
902 if (dle->dev != dev)
903 continue;
904
905 spin_lock_irq(&dev->power.lock);
906 dev->pm_domain = NULL;
907 spin_unlock_irq(&dev->power.lock);
908
596ba34b 909 genpd->device_count--;
f721889f
RW
910 list_del(&dle->node);
911 kfree(dle);
912
913 ret = 0;
914 break;
915 }
916
596ba34b 917 out:
f721889f
RW
918 mutex_unlock(&genpd->lock);
919
920 return ret;
921}
922
923/**
924 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
925 * @genpd: Master PM domain to add the subdomain to.
926 * @new_subdomain: Subdomain to be added.
927 */
928int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
929 struct generic_pm_domain *new_subdomain)
930{
931 struct generic_pm_domain *subdomain;
932 int ret = 0;
933
934 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
935 return -EINVAL;
936
937 mutex_lock(&genpd->lock);
938
939 if (genpd->power_is_off && !new_subdomain->power_is_off) {
940 ret = -EINVAL;
941 goto out;
942 }
943
944 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
945 if (subdomain == new_subdomain) {
946 ret = -EINVAL;
947 goto out;
948 }
949 }
950
951 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
952
953 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
954 new_subdomain->parent = genpd;
955 if (!subdomain->power_is_off)
956 genpd->sd_count++;
957
958 mutex_unlock(&new_subdomain->lock);
959
960 out:
961 mutex_unlock(&genpd->lock);
962
963 return ret;
964}
965
966/**
967 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
968 * @genpd: Master PM domain to remove the subdomain from.
969 * @target: Subdomain to be removed.
970 */
971int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
972 struct generic_pm_domain *target)
973{
974 struct generic_pm_domain *subdomain;
975 int ret = -EINVAL;
976
977 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
978 return -EINVAL;
979
980 mutex_lock(&genpd->lock);
981
982 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
983 if (subdomain != target)
984 continue;
985
986 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
987
988 list_del(&subdomain->sd_node);
989 subdomain->parent = NULL;
990 if (!subdomain->power_is_off)
991 genpd_sd_counter_dec(genpd);
992
993 mutex_unlock(&subdomain->lock);
994
995 ret = 0;
996 break;
997 }
998
999 mutex_unlock(&genpd->lock);
1000
1001 return ret;
1002}
1003
1004/**
1005 * pm_genpd_init - Initialize a generic I/O PM domain object.
1006 * @genpd: PM domain object to initialize.
1007 * @gov: PM domain governor to associate with the domain (may be NULL).
1008 * @is_off: Initial value of the domain's power_is_off field.
1009 */
1010void pm_genpd_init(struct generic_pm_domain *genpd,
1011 struct dev_power_governor *gov, bool is_off)
1012{
1013 if (IS_ERR_OR_NULL(genpd))
1014 return;
1015
1016 INIT_LIST_HEAD(&genpd->sd_node);
1017 genpd->parent = NULL;
1018 INIT_LIST_HEAD(&genpd->dev_list);
1019 INIT_LIST_HEAD(&genpd->sd_list);
1020 mutex_init(&genpd->lock);
1021 genpd->gov = gov;
1022 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1023 genpd->in_progress = 0;
1024 genpd->sd_count = 0;
1025 genpd->power_is_off = is_off;
596ba34b
RW
1026 genpd->device_count = 0;
1027 genpd->suspended_count = 0;
f721889f
RW
1028 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1029 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1030 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
596ba34b
RW
1031 genpd->domain.ops.prepare = pm_genpd_prepare;
1032 genpd->domain.ops.suspend = pm_genpd_suspend;
1033 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1034 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1035 genpd->domain.ops.resume = pm_genpd_resume;
1036 genpd->domain.ops.freeze = pm_genpd_freeze;
1037 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1038 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1039 genpd->domain.ops.thaw = pm_genpd_thaw;
1040 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1041 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1042 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1043 genpd->domain.ops.restore = pm_genpd_restore;
1044 genpd->domain.ops.complete = pm_genpd_complete;
f721889f 1045}