1f4b1326c6a9e78c4723d85d7a31251169065c49
[linux-2.6-block.git] / drivers / base / power / domain.c
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 #include <linux/sched.h>
17 #include <linux/suspend.h>
18
19 static LIST_HEAD(gpd_list);
20 static DEFINE_MUTEX(gpd_list_lock);
21
22 #ifdef CONFIG_PM
23
24 static struct generic_pm_domain *dev_to_genpd(struct device *dev)
25 {
26         if (IS_ERR_OR_NULL(dev->pm_domain))
27                 return ERR_PTR(-EINVAL);
28
29         return pd_to_genpd(dev->pm_domain);
30 }
31
32 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
33 {
34         bool ret = false;
35
36         if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
37                 ret = !!atomic_dec_and_test(&genpd->sd_count);
38
39         return ret;
40 }
41
42 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
43 {
44         atomic_inc(&genpd->sd_count);
45         smp_mb__after_atomic_inc();
46 }
47
48 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
49 {
50         DEFINE_WAIT(wait);
51
52         mutex_lock(&genpd->lock);
53         /*
54          * Wait for the domain to transition into either the active,
55          * or the power off state.
56          */
57         for (;;) {
58                 prepare_to_wait(&genpd->status_wait_queue, &wait,
59                                 TASK_UNINTERRUPTIBLE);
60                 if (genpd->status == GPD_STATE_ACTIVE
61                     || genpd->status == GPD_STATE_POWER_OFF)
62                         break;
63                 mutex_unlock(&genpd->lock);
64
65                 schedule();
66
67                 mutex_lock(&genpd->lock);
68         }
69         finish_wait(&genpd->status_wait_queue, &wait);
70 }
71
72 static void genpd_release_lock(struct generic_pm_domain *genpd)
73 {
74         mutex_unlock(&genpd->lock);
75 }
76
77 static void genpd_set_active(struct generic_pm_domain *genpd)
78 {
79         if (genpd->resume_count == 0)
80                 genpd->status = GPD_STATE_ACTIVE;
81 }
82
83 /**
84  * __pm_genpd_poweron - Restore power to a given PM domain and its parents.
85  * @genpd: PM domain to power up.
86  *
87  * Restore power to @genpd and all of its parents so that it is possible to
88  * resume a device belonging to it.
89  */
90 int __pm_genpd_poweron(struct generic_pm_domain *genpd)
91         __releases(&genpd->lock) __acquires(&genpd->lock)
92 {
93         DEFINE_WAIT(wait);
94         int ret = 0;
95
96         /* If the domain's parent is being waited for, we have to wait too. */
97         for (;;) {
98                 prepare_to_wait(&genpd->status_wait_queue, &wait,
99                                 TASK_UNINTERRUPTIBLE);
100                 if (genpd->status != GPD_STATE_WAIT_PARENT)
101                         break;
102                 mutex_unlock(&genpd->lock);
103
104                 schedule();
105
106                 mutex_lock(&genpd->lock);
107         }
108         finish_wait(&genpd->status_wait_queue, &wait);
109
110         if (genpd->status == GPD_STATE_ACTIVE
111             || (genpd->prepared_count > 0 && genpd->suspend_power_off))
112                 return 0;
113
114         if (genpd->status != GPD_STATE_POWER_OFF) {
115                 genpd_set_active(genpd);
116                 return 0;
117         }
118
119         if (genpd->parent) {
120                 genpd_sd_counter_inc(genpd->parent);
121                 genpd->status = GPD_STATE_WAIT_PARENT;
122
123                 mutex_unlock(&genpd->lock);
124
125                 ret = pm_genpd_poweron(genpd->parent);
126
127                 mutex_lock(&genpd->lock);
128
129                 /*
130                  * The "wait for parent" status is guaranteed not to change
131                  * while the parent is powering on.
132                  */
133                 genpd->status = GPD_STATE_POWER_OFF;
134                 wake_up_all(&genpd->status_wait_queue);
135                 if (ret)
136                         goto err;
137         }
138
139         if (genpd->power_on) {
140                 ret = genpd->power_on(genpd);
141                 if (ret)
142                         goto err;
143         }
144
145         genpd_set_active(genpd);
146
147         return 0;
148
149  err:
150         if (genpd->parent)
151                 genpd_sd_counter_dec(genpd->parent);
152
153         return ret;
154 }
155
156 /**
157  * pm_genpd_poweron - Restore power to a given PM domain and its parents.
158  * @genpd: PM domain to power up.
159  */
160 int pm_genpd_poweron(struct generic_pm_domain *genpd)
161 {
162         int ret;
163
164         mutex_lock(&genpd->lock);
165         ret = __pm_genpd_poweron(genpd);
166         mutex_unlock(&genpd->lock);
167         return ret;
168 }
169
170 #endif /* CONFIG_PM */
171
172 #ifdef CONFIG_PM_RUNTIME
173
174 /**
175  * __pm_genpd_save_device - Save the pre-suspend state of a device.
176  * @dle: Device list entry of the device to save the state of.
177  * @genpd: PM domain the device belongs to.
178  */
179 static int __pm_genpd_save_device(struct dev_list_entry *dle,
180                                   struct generic_pm_domain *genpd)
181         __releases(&genpd->lock) __acquires(&genpd->lock)
182 {
183         struct device *dev = dle->dev;
184         struct device_driver *drv = dev->driver;
185         int ret = 0;
186
187         if (dle->need_restore)
188                 return 0;
189
190         mutex_unlock(&genpd->lock);
191
192         if (drv && drv->pm && drv->pm->runtime_suspend) {
193                 if (genpd->start_device)
194                         genpd->start_device(dev);
195
196                 ret = drv->pm->runtime_suspend(dev);
197
198                 if (genpd->stop_device)
199                         genpd->stop_device(dev);
200         }
201
202         mutex_lock(&genpd->lock);
203
204         if (!ret)
205                 dle->need_restore = true;
206
207         return ret;
208 }
209
210 /**
211  * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
212  * @dle: Device list entry of the device to restore the state of.
213  * @genpd: PM domain the device belongs to.
214  */
215 static void __pm_genpd_restore_device(struct dev_list_entry *dle,
216                                       struct generic_pm_domain *genpd)
217         __releases(&genpd->lock) __acquires(&genpd->lock)
218 {
219         struct device *dev = dle->dev;
220         struct device_driver *drv = dev->driver;
221
222         if (!dle->need_restore)
223                 return;
224
225         mutex_unlock(&genpd->lock);
226
227         if (drv && drv->pm && drv->pm->runtime_resume) {
228                 if (genpd->start_device)
229                         genpd->start_device(dev);
230
231                 drv->pm->runtime_resume(dev);
232
233                 if (genpd->stop_device)
234                         genpd->stop_device(dev);
235         }
236
237         mutex_lock(&genpd->lock);
238
239         dle->need_restore = false;
240 }
241
242 /**
243  * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
244  * @genpd: PM domain to check.
245  *
246  * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
247  * a "power off" operation, which means that a "power on" has occured in the
248  * meantime, or if its resume_count field is different from zero, which means
249  * that one of its devices has been resumed in the meantime.
250  */
251 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
252 {
253         return genpd->status == GPD_STATE_WAIT_PARENT
254                 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
255 }
256
257 /**
258  * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
259  * @genpd: PM domait to power off.
260  *
261  * Queue up the execution of pm_genpd_poweroff() unless it's already been done
262  * before.
263  */
264 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
265 {
266         if (!work_pending(&genpd->power_off_work))
267                 queue_work(pm_wq, &genpd->power_off_work);
268 }
269
270 /**
271  * pm_genpd_poweroff - Remove power from a given PM domain.
272  * @genpd: PM domain to power down.
273  *
274  * If all of the @genpd's devices have been suspended and all of its subdomains
275  * have been powered down, run the runtime suspend callbacks provided by all of
276  * the @genpd's devices' drivers and remove power from @genpd.
277  */
278 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
279         __releases(&genpd->lock) __acquires(&genpd->lock)
280 {
281         struct generic_pm_domain *parent;
282         struct dev_list_entry *dle;
283         unsigned int not_suspended;
284         int ret = 0;
285
286  start:
287         /*
288          * Do not try to power off the domain in the following situations:
289          * (1) The domain is already in the "power off" state.
290          * (2) The domain is waiting for its parent to power up.
291          * (3) One of the domain's devices is being resumed right now.
292          * (4) System suspend is in progress.
293          */
294         if (genpd->status == GPD_STATE_POWER_OFF
295             || genpd->status == GPD_STATE_WAIT_PARENT
296             || genpd->resume_count > 0 || genpd->prepared_count > 0)
297                 return 0;
298
299         if (atomic_read(&genpd->sd_count) > 0)
300                 return -EBUSY;
301
302         not_suspended = 0;
303         list_for_each_entry(dle, &genpd->dev_list, node)
304                 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
305                         not_suspended++;
306
307         if (not_suspended > genpd->in_progress)
308                 return -EBUSY;
309
310         if (genpd->poweroff_task) {
311                 /*
312                  * Another instance of pm_genpd_poweroff() is executing
313                  * callbacks, so tell it to start over and return.
314                  */
315                 genpd->status = GPD_STATE_REPEAT;
316                 return 0;
317         }
318
319         if (genpd->gov && genpd->gov->power_down_ok) {
320                 if (!genpd->gov->power_down_ok(&genpd->domain))
321                         return -EAGAIN;
322         }
323
324         genpd->status = GPD_STATE_BUSY;
325         genpd->poweroff_task = current;
326
327         list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
328                 ret = atomic_read(&genpd->sd_count) == 0 ?
329                         __pm_genpd_save_device(dle, genpd) : -EBUSY;
330
331                 if (genpd_abort_poweroff(genpd))
332                         goto out;
333
334                 if (ret) {
335                         genpd_set_active(genpd);
336                         goto out;
337                 }
338
339                 if (genpd->status == GPD_STATE_REPEAT) {
340                         genpd->poweroff_task = NULL;
341                         goto start;
342                 }
343         }
344
345         if (genpd->power_off) {
346                 if (atomic_read(&genpd->sd_count) > 0) {
347                         ret = -EBUSY;
348                         goto out;
349                 }
350
351                 /*
352                  * If sd_count > 0 at this point, one of the children hasn't
353                  * managed to call pm_genpd_poweron() for the parent yet after
354                  * incrementing it.  In that case pm_genpd_poweron() will wait
355                  * for us to drop the lock, so we can call .power_off() and let
356                  * the pm_genpd_poweron() restore power for us (this shouldn't
357                  * happen very often).
358                  */
359                 ret = genpd->power_off(genpd);
360                 if (ret == -EBUSY) {
361                         genpd_set_active(genpd);
362                         goto out;
363                 }
364         }
365
366         genpd->status = GPD_STATE_POWER_OFF;
367
368         parent = genpd->parent;
369         if (parent && genpd_sd_counter_dec(parent))
370                 genpd_queue_power_off_work(parent);
371
372  out:
373         genpd->poweroff_task = NULL;
374         wake_up_all(&genpd->status_wait_queue);
375         return ret;
376 }
377
378 /**
379  * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
380  * @work: Work structure used for scheduling the execution of this function.
381  */
382 static void genpd_power_off_work_fn(struct work_struct *work)
383 {
384         struct generic_pm_domain *genpd;
385
386         genpd = container_of(work, struct generic_pm_domain, power_off_work);
387
388         genpd_acquire_lock(genpd);
389         pm_genpd_poweroff(genpd);
390         genpd_release_lock(genpd);
391 }
392
393 /**
394  * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
395  * @dev: Device to suspend.
396  *
397  * Carry out a runtime suspend of a device under the assumption that its
398  * pm_domain field points to the domain member of an object of type
399  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
400  */
401 static int pm_genpd_runtime_suspend(struct device *dev)
402 {
403         struct generic_pm_domain *genpd;
404
405         dev_dbg(dev, "%s()\n", __func__);
406
407         genpd = dev_to_genpd(dev);
408         if (IS_ERR(genpd))
409                 return -EINVAL;
410
411         if (genpd->stop_device) {
412                 int ret = genpd->stop_device(dev);
413                 if (ret)
414                         return ret;
415         }
416
417         mutex_lock(&genpd->lock);
418         genpd->in_progress++;
419         pm_genpd_poweroff(genpd);
420         genpd->in_progress--;
421         mutex_unlock(&genpd->lock);
422
423         return 0;
424 }
425
426 /**
427  * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
428  * @dev: Device to resume.
429  * @genpd: PM domain the device belongs to.
430  */
431 static void __pm_genpd_runtime_resume(struct device *dev,
432                                       struct generic_pm_domain *genpd)
433 {
434         struct dev_list_entry *dle;
435
436         list_for_each_entry(dle, &genpd->dev_list, node) {
437                 if (dle->dev == dev) {
438                         __pm_genpd_restore_device(dle, genpd);
439                         break;
440                 }
441         }
442 }
443
444 /**
445  * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
446  * @dev: Device to resume.
447  *
448  * Carry out a runtime resume of a device under the assumption that its
449  * pm_domain field points to the domain member of an object of type
450  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
451  */
452 static int pm_genpd_runtime_resume(struct device *dev)
453 {
454         struct generic_pm_domain *genpd;
455         DEFINE_WAIT(wait);
456         int ret;
457
458         dev_dbg(dev, "%s()\n", __func__);
459
460         genpd = dev_to_genpd(dev);
461         if (IS_ERR(genpd))
462                 return -EINVAL;
463
464         mutex_lock(&genpd->lock);
465         ret = __pm_genpd_poweron(genpd);
466         if (ret) {
467                 mutex_unlock(&genpd->lock);
468                 return ret;
469         }
470         genpd->status = GPD_STATE_BUSY;
471         genpd->resume_count++;
472         for (;;) {
473                 prepare_to_wait(&genpd->status_wait_queue, &wait,
474                                 TASK_UNINTERRUPTIBLE);
475                 /*
476                  * If current is the powering off task, we have been called
477                  * reentrantly from one of the device callbacks, so we should
478                  * not wait.
479                  */
480                 if (!genpd->poweroff_task || genpd->poweroff_task == current)
481                         break;
482                 mutex_unlock(&genpd->lock);
483
484                 schedule();
485
486                 mutex_lock(&genpd->lock);
487         }
488         finish_wait(&genpd->status_wait_queue, &wait);
489         __pm_genpd_runtime_resume(dev, genpd);
490         genpd->resume_count--;
491         genpd_set_active(genpd);
492         wake_up_all(&genpd->status_wait_queue);
493         mutex_unlock(&genpd->lock);
494
495         if (genpd->start_device)
496                 genpd->start_device(dev);
497
498         return 0;
499 }
500
501 /**
502  * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
503  */
504 void pm_genpd_poweroff_unused(void)
505 {
506         struct generic_pm_domain *genpd;
507
508         mutex_lock(&gpd_list_lock);
509
510         list_for_each_entry(genpd, &gpd_list, gpd_list_node)
511                 genpd_queue_power_off_work(genpd);
512
513         mutex_unlock(&gpd_list_lock);
514 }
515
516 #else
517
518 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
519 static inline void __pm_genpd_runtime_resume(struct device *dev,
520                                              struct generic_pm_domain *genpd) {}
521
522 #define pm_genpd_runtime_suspend        NULL
523 #define pm_genpd_runtime_resume         NULL
524
525 #endif /* CONFIG_PM_RUNTIME */
526
527 #ifdef CONFIG_PM_SLEEP
528
529 /**
530  * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
531  * @genpd: PM domain to power off, if possible.
532  *
533  * Check if the given PM domain can be powered off (during system suspend or
534  * hibernation) and do that if so.  Also, in that case propagate to its parent.
535  *
536  * This function is only called in "noirq" stages of system power transitions,
537  * so it need not acquire locks (all of the "noirq" callbacks are executed
538  * sequentially, so it is guaranteed that it will never run twice in parallel).
539  */
540 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
541 {
542         struct generic_pm_domain *parent = genpd->parent;
543
544         if (genpd->status == GPD_STATE_POWER_OFF)
545                 return;
546
547         if (genpd->suspended_count != genpd->device_count
548             || atomic_read(&genpd->sd_count) > 0)
549                 return;
550
551         if (genpd->power_off)
552                 genpd->power_off(genpd);
553
554         genpd->status = GPD_STATE_POWER_OFF;
555         if (parent) {
556                 genpd_sd_counter_dec(parent);
557                 pm_genpd_sync_poweroff(parent);
558         }
559 }
560
561 /**
562  * resume_needed - Check whether to resume a device before system suspend.
563  * @dev: Device to check.
564  * @genpd: PM domain the device belongs to.
565  *
566  * There are two cases in which a device that can wake up the system from sleep
567  * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
568  * to wake up the system and it has to remain active for this purpose while the
569  * system is in the sleep state and (2) if the device is not enabled to wake up
570  * the system from sleep states and it generally doesn't generate wakeup signals
571  * by itself (those signals are generated on its behalf by other parts of the
572  * system).  In the latter case it may be necessary to reconfigure the device's
573  * wakeup settings during system suspend, because it may have been set up to
574  * signal remote wakeup from the system's working state as needed by runtime PM.
575  * Return 'true' in either of the above cases.
576  */
577 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
578 {
579         bool active_wakeup;
580
581         if (!device_can_wakeup(dev))
582                 return false;
583
584         active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
585         return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
586 }
587
588 /**
589  * pm_genpd_prepare - Start power transition of a device in a PM domain.
590  * @dev: Device to start the transition of.
591  *
592  * Start a power transition of a device (during a system-wide power transition)
593  * under the assumption that its pm_domain field points to the domain member of
594  * an object of type struct generic_pm_domain representing a PM domain
595  * consisting of I/O devices.
596  */
597 static int pm_genpd_prepare(struct device *dev)
598 {
599         struct generic_pm_domain *genpd;
600         int ret;
601
602         dev_dbg(dev, "%s()\n", __func__);
603
604         genpd = dev_to_genpd(dev);
605         if (IS_ERR(genpd))
606                 return -EINVAL;
607
608         /*
609          * If a wakeup request is pending for the device, it should be woken up
610          * at this point and a system wakeup event should be reported if it's
611          * set up to wake up the system from sleep states.
612          */
613         pm_runtime_get_noresume(dev);
614         if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
615                 pm_wakeup_event(dev, 0);
616
617         if (pm_wakeup_pending()) {
618                 pm_runtime_put_sync(dev);
619                 return -EBUSY;
620         }
621
622         if (resume_needed(dev, genpd))
623                 pm_runtime_resume(dev);
624
625         genpd_acquire_lock(genpd);
626
627         if (genpd->prepared_count++ == 0)
628                 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
629
630         genpd_release_lock(genpd);
631
632         if (genpd->suspend_power_off) {
633                 pm_runtime_put_noidle(dev);
634                 return 0;
635         }
636
637         /*
638          * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
639          * so pm_genpd_poweron() will return immediately, but if the device
640          * is suspended (e.g. it's been stopped by .stop_device()), we need
641          * to make it operational.
642          */
643         pm_runtime_resume(dev);
644         __pm_runtime_disable(dev, false);
645
646         ret = pm_generic_prepare(dev);
647         if (ret) {
648                 mutex_lock(&genpd->lock);
649
650                 if (--genpd->prepared_count == 0)
651                         genpd->suspend_power_off = false;
652
653                 mutex_unlock(&genpd->lock);
654                 pm_runtime_enable(dev);
655         }
656
657         pm_runtime_put_sync(dev);
658         return ret;
659 }
660
661 /**
662  * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
663  * @dev: Device to suspend.
664  *
665  * Suspend a device under the assumption that its pm_domain field points to the
666  * domain member of an object of type struct generic_pm_domain representing
667  * a PM domain consisting of I/O devices.
668  */
669 static int pm_genpd_suspend(struct device *dev)
670 {
671         struct generic_pm_domain *genpd;
672
673         dev_dbg(dev, "%s()\n", __func__);
674
675         genpd = dev_to_genpd(dev);
676         if (IS_ERR(genpd))
677                 return -EINVAL;
678
679         return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
680 }
681
682 /**
683  * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
684  * @dev: Device to suspend.
685  *
686  * Carry out a late suspend of a device under the assumption that its
687  * pm_domain field points to the domain member of an object of type
688  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
689  */
690 static int pm_genpd_suspend_noirq(struct device *dev)
691 {
692         struct generic_pm_domain *genpd;
693         int ret;
694
695         dev_dbg(dev, "%s()\n", __func__);
696
697         genpd = dev_to_genpd(dev);
698         if (IS_ERR(genpd))
699                 return -EINVAL;
700
701         if (genpd->suspend_power_off)
702                 return 0;
703
704         ret = pm_generic_suspend_noirq(dev);
705         if (ret)
706                 return ret;
707
708         if (device_may_wakeup(dev)
709             && genpd->active_wakeup && genpd->active_wakeup(dev))
710                 return 0;
711
712         if (genpd->stop_device)
713                 genpd->stop_device(dev);
714
715         /*
716          * Since all of the "noirq" callbacks are executed sequentially, it is
717          * guaranteed that this function will never run twice in parallel for
718          * the same PM domain, so it is not necessary to use locking here.
719          */
720         genpd->suspended_count++;
721         pm_genpd_sync_poweroff(genpd);
722
723         return 0;
724 }
725
726 /**
727  * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
728  * @dev: Device to resume.
729  *
730  * Carry out an early resume of a device under the assumption that its
731  * pm_domain field points to the domain member of an object of type
732  * struct generic_pm_domain representing a power domain consisting of I/O
733  * devices.
734  */
735 static int pm_genpd_resume_noirq(struct device *dev)
736 {
737         struct generic_pm_domain *genpd;
738
739         dev_dbg(dev, "%s()\n", __func__);
740
741         genpd = dev_to_genpd(dev);
742         if (IS_ERR(genpd))
743                 return -EINVAL;
744
745         if (genpd->suspend_power_off)
746                 return 0;
747
748         /*
749          * Since all of the "noirq" callbacks are executed sequentially, it is
750          * guaranteed that this function will never run twice in parallel for
751          * the same PM domain, so it is not necessary to use locking here.
752          */
753         pm_genpd_poweron(genpd);
754         genpd->suspended_count--;
755         if (genpd->start_device)
756                 genpd->start_device(dev);
757
758         return pm_generic_resume_noirq(dev);
759 }
760
761 /**
762  * pm_genpd_resume - Resume a device belonging to an I/O power domain.
763  * @dev: Device to resume.
764  *
765  * Resume a device under the assumption that its pm_domain field points to the
766  * domain member of an object of type struct generic_pm_domain representing
767  * a power domain consisting of I/O devices.
768  */
769 static int pm_genpd_resume(struct device *dev)
770 {
771         struct generic_pm_domain *genpd;
772
773         dev_dbg(dev, "%s()\n", __func__);
774
775         genpd = dev_to_genpd(dev);
776         if (IS_ERR(genpd))
777                 return -EINVAL;
778
779         return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
780 }
781
782 /**
783  * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
784  * @dev: Device to freeze.
785  *
786  * Freeze a device under the assumption that its pm_domain field points to the
787  * domain member of an object of type struct generic_pm_domain representing
788  * a power domain consisting of I/O devices.
789  */
790 static int pm_genpd_freeze(struct device *dev)
791 {
792         struct generic_pm_domain *genpd;
793
794         dev_dbg(dev, "%s()\n", __func__);
795
796         genpd = dev_to_genpd(dev);
797         if (IS_ERR(genpd))
798                 return -EINVAL;
799
800         return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
801 }
802
803 /**
804  * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
805  * @dev: Device to freeze.
806  *
807  * Carry out a late freeze of a device under the assumption that its
808  * pm_domain field points to the domain member of an object of type
809  * struct generic_pm_domain representing a power domain consisting of I/O
810  * devices.
811  */
812 static int pm_genpd_freeze_noirq(struct device *dev)
813 {
814         struct generic_pm_domain *genpd;
815         int ret;
816
817         dev_dbg(dev, "%s()\n", __func__);
818
819         genpd = dev_to_genpd(dev);
820         if (IS_ERR(genpd))
821                 return -EINVAL;
822
823         if (genpd->suspend_power_off)
824                 return 0;
825
826         ret = pm_generic_freeze_noirq(dev);
827         if (ret)
828                 return ret;
829
830         if (genpd->stop_device)
831                 genpd->stop_device(dev);
832
833         return 0;
834 }
835
836 /**
837  * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
838  * @dev: Device to thaw.
839  *
840  * Carry out an early thaw of a device under the assumption that its
841  * pm_domain field points to the domain member of an object of type
842  * struct generic_pm_domain representing a power domain consisting of I/O
843  * devices.
844  */
845 static int pm_genpd_thaw_noirq(struct device *dev)
846 {
847         struct generic_pm_domain *genpd;
848
849         dev_dbg(dev, "%s()\n", __func__);
850
851         genpd = dev_to_genpd(dev);
852         if (IS_ERR(genpd))
853                 return -EINVAL;
854
855         if (genpd->suspend_power_off)
856                 return 0;
857
858         if (genpd->start_device)
859                 genpd->start_device(dev);
860
861         return pm_generic_thaw_noirq(dev);
862 }
863
864 /**
865  * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
866  * @dev: Device to thaw.
867  *
868  * Thaw a device under the assumption that its pm_domain field points to the
869  * domain member of an object of type struct generic_pm_domain representing
870  * a power domain consisting of I/O devices.
871  */
872 static int pm_genpd_thaw(struct device *dev)
873 {
874         struct generic_pm_domain *genpd;
875
876         dev_dbg(dev, "%s()\n", __func__);
877
878         genpd = dev_to_genpd(dev);
879         if (IS_ERR(genpd))
880                 return -EINVAL;
881
882         return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
883 }
884
885 /**
886  * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
887  * @dev: Device to suspend.
888  *
889  * Power off a device under the assumption that its pm_domain field points to
890  * the domain member of an object of type struct generic_pm_domain representing
891  * a PM domain consisting of I/O devices.
892  */
893 static int pm_genpd_dev_poweroff(struct device *dev)
894 {
895         struct generic_pm_domain *genpd;
896
897         dev_dbg(dev, "%s()\n", __func__);
898
899         genpd = dev_to_genpd(dev);
900         if (IS_ERR(genpd))
901                 return -EINVAL;
902
903         return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
904 }
905
906 /**
907  * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
908  * @dev: Device to suspend.
909  *
910  * Carry out a late powering off of a device under the assumption that its
911  * pm_domain field points to the domain member of an object of type
912  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
913  */
914 static int pm_genpd_dev_poweroff_noirq(struct device *dev)
915 {
916         struct generic_pm_domain *genpd;
917         int ret;
918
919         dev_dbg(dev, "%s()\n", __func__);
920
921         genpd = dev_to_genpd(dev);
922         if (IS_ERR(genpd))
923                 return -EINVAL;
924
925         if (genpd->suspend_power_off)
926                 return 0;
927
928         ret = pm_generic_poweroff_noirq(dev);
929         if (ret)
930                 return ret;
931
932         if (device_may_wakeup(dev)
933             && genpd->active_wakeup && genpd->active_wakeup(dev))
934                 return 0;
935
936         if (genpd->stop_device)
937                 genpd->stop_device(dev);
938
939         /*
940          * Since all of the "noirq" callbacks are executed sequentially, it is
941          * guaranteed that this function will never run twice in parallel for
942          * the same PM domain, so it is not necessary to use locking here.
943          */
944         genpd->suspended_count++;
945         pm_genpd_sync_poweroff(genpd);
946
947         return 0;
948 }
949
950 /**
951  * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
952  * @dev: Device to resume.
953  *
954  * Carry out an early restore of a device under the assumption that its
955  * pm_domain field points to the domain member of an object of type
956  * struct generic_pm_domain representing a power domain consisting of I/O
957  * devices.
958  */
959 static int pm_genpd_restore_noirq(struct device *dev)
960 {
961         struct generic_pm_domain *genpd;
962
963         dev_dbg(dev, "%s()\n", __func__);
964
965         genpd = dev_to_genpd(dev);
966         if (IS_ERR(genpd))
967                 return -EINVAL;
968
969         /*
970          * Since all of the "noirq" callbacks are executed sequentially, it is
971          * guaranteed that this function will never run twice in parallel for
972          * the same PM domain, so it is not necessary to use locking here.
973          */
974         genpd->status = GPD_STATE_POWER_OFF;
975         if (genpd->suspend_power_off) {
976                 /*
977                  * The boot kernel might put the domain into the power on state,
978                  * so make sure it really is powered off.
979                  */
980                 if (genpd->power_off)
981                         genpd->power_off(genpd);
982                 return 0;
983         }
984
985         pm_genpd_poweron(genpd);
986         genpd->suspended_count--;
987         if (genpd->start_device)
988                 genpd->start_device(dev);
989
990         return pm_generic_restore_noirq(dev);
991 }
992
993 /**
994  * pm_genpd_restore - Restore a device belonging to an I/O power domain.
995  * @dev: Device to resume.
996  *
997  * Restore a device under the assumption that its pm_domain field points to the
998  * domain member of an object of type struct generic_pm_domain representing
999  * a power domain consisting of I/O devices.
1000  */
1001 static int pm_genpd_restore(struct device *dev)
1002 {
1003         struct generic_pm_domain *genpd;
1004
1005         dev_dbg(dev, "%s()\n", __func__);
1006
1007         genpd = dev_to_genpd(dev);
1008         if (IS_ERR(genpd))
1009                 return -EINVAL;
1010
1011         return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
1012 }
1013
1014 /**
1015  * pm_genpd_complete - Complete power transition of a device in a power domain.
1016  * @dev: Device to complete the transition of.
1017  *
1018  * Complete a power transition of a device (during a system-wide power
1019  * transition) under the assumption that its pm_domain field points to the
1020  * domain member of an object of type struct generic_pm_domain representing
1021  * a power domain consisting of I/O devices.
1022  */
1023 static void pm_genpd_complete(struct device *dev)
1024 {
1025         struct generic_pm_domain *genpd;
1026         bool run_complete;
1027
1028         dev_dbg(dev, "%s()\n", __func__);
1029
1030         genpd = dev_to_genpd(dev);
1031         if (IS_ERR(genpd))
1032                 return;
1033
1034         mutex_lock(&genpd->lock);
1035
1036         run_complete = !genpd->suspend_power_off;
1037         if (--genpd->prepared_count == 0)
1038                 genpd->suspend_power_off = false;
1039
1040         mutex_unlock(&genpd->lock);
1041
1042         if (run_complete) {
1043                 pm_generic_complete(dev);
1044                 pm_runtime_set_active(dev);
1045                 pm_runtime_enable(dev);
1046                 pm_runtime_idle(dev);
1047         }
1048 }
1049
1050 #else
1051
1052 #define pm_genpd_prepare                NULL
1053 #define pm_genpd_suspend                NULL
1054 #define pm_genpd_suspend_noirq          NULL
1055 #define pm_genpd_resume_noirq           NULL
1056 #define pm_genpd_resume                 NULL
1057 #define pm_genpd_freeze                 NULL
1058 #define pm_genpd_freeze_noirq           NULL
1059 #define pm_genpd_thaw_noirq             NULL
1060 #define pm_genpd_thaw                   NULL
1061 #define pm_genpd_dev_poweroff_noirq     NULL
1062 #define pm_genpd_dev_poweroff           NULL
1063 #define pm_genpd_restore_noirq          NULL
1064 #define pm_genpd_restore                NULL
1065 #define pm_genpd_complete               NULL
1066
1067 #endif /* CONFIG_PM_SLEEP */
1068
1069 /**
1070  * pm_genpd_add_device - Add a device to an I/O PM domain.
1071  * @genpd: PM domain to add the device to.
1072  * @dev: Device to be added.
1073  */
1074 int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1075 {
1076         struct dev_list_entry *dle;
1077         int ret = 0;
1078
1079         dev_dbg(dev, "%s()\n", __func__);
1080
1081         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1082                 return -EINVAL;
1083
1084         genpd_acquire_lock(genpd);
1085
1086         if (genpd->status == GPD_STATE_POWER_OFF) {
1087                 ret = -EINVAL;
1088                 goto out;
1089         }
1090
1091         if (genpd->prepared_count > 0) {
1092                 ret = -EAGAIN;
1093                 goto out;
1094         }
1095
1096         list_for_each_entry(dle, &genpd->dev_list, node)
1097                 if (dle->dev == dev) {
1098                         ret = -EINVAL;
1099                         goto out;
1100                 }
1101
1102         dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1103         if (!dle) {
1104                 ret = -ENOMEM;
1105                 goto out;
1106         }
1107
1108         dle->dev = dev;
1109         dle->need_restore = false;
1110         list_add_tail(&dle->node, &genpd->dev_list);
1111         genpd->device_count++;
1112
1113         spin_lock_irq(&dev->power.lock);
1114         dev->pm_domain = &genpd->domain;
1115         spin_unlock_irq(&dev->power.lock);
1116
1117  out:
1118         genpd_release_lock(genpd);
1119
1120         return ret;
1121 }
1122
1123 /**
1124  * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1125  * @genpd: PM domain to remove the device from.
1126  * @dev: Device to be removed.
1127  */
1128 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1129                            struct device *dev)
1130 {
1131         struct dev_list_entry *dle;
1132         int ret = -EINVAL;
1133
1134         dev_dbg(dev, "%s()\n", __func__);
1135
1136         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1137                 return -EINVAL;
1138
1139         genpd_acquire_lock(genpd);
1140
1141         if (genpd->prepared_count > 0) {
1142                 ret = -EAGAIN;
1143                 goto out;
1144         }
1145
1146         list_for_each_entry(dle, &genpd->dev_list, node) {
1147                 if (dle->dev != dev)
1148                         continue;
1149
1150                 spin_lock_irq(&dev->power.lock);
1151                 dev->pm_domain = NULL;
1152                 spin_unlock_irq(&dev->power.lock);
1153
1154                 genpd->device_count--;
1155                 list_del(&dle->node);
1156                 kfree(dle);
1157
1158                 ret = 0;
1159                 break;
1160         }
1161
1162  out:
1163         genpd_release_lock(genpd);
1164
1165         return ret;
1166 }
1167
1168 /**
1169  * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1170  * @genpd: Master PM domain to add the subdomain to.
1171  * @new_subdomain: Subdomain to be added.
1172  */
1173 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1174                            struct generic_pm_domain *new_subdomain)
1175 {
1176         struct generic_pm_domain *subdomain;
1177         int ret = 0;
1178
1179         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1180                 return -EINVAL;
1181
1182  start:
1183         genpd_acquire_lock(genpd);
1184         mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
1185
1186         if (new_subdomain->status != GPD_STATE_POWER_OFF
1187             && new_subdomain->status != GPD_STATE_ACTIVE) {
1188                 mutex_unlock(&new_subdomain->lock);
1189                 genpd_release_lock(genpd);
1190                 goto start;
1191         }
1192
1193         if (genpd->status == GPD_STATE_POWER_OFF
1194             &&  new_subdomain->status != GPD_STATE_POWER_OFF) {
1195                 ret = -EINVAL;
1196                 goto out;
1197         }
1198
1199         list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1200                 if (subdomain == new_subdomain) {
1201                         ret = -EINVAL;
1202                         goto out;
1203                 }
1204         }
1205
1206         list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1207         new_subdomain->parent = genpd;
1208         if (subdomain->status != GPD_STATE_POWER_OFF)
1209                 genpd_sd_counter_inc(genpd);
1210
1211  out:
1212         mutex_unlock(&new_subdomain->lock);
1213         genpd_release_lock(genpd);
1214
1215         return ret;
1216 }
1217
1218 /**
1219  * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1220  * @genpd: Master PM domain to remove the subdomain from.
1221  * @target: Subdomain to be removed.
1222  */
1223 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1224                               struct generic_pm_domain *target)
1225 {
1226         struct generic_pm_domain *subdomain;
1227         int ret = -EINVAL;
1228
1229         if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1230                 return -EINVAL;
1231
1232  start:
1233         genpd_acquire_lock(genpd);
1234
1235         list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1236                 if (subdomain != target)
1237                         continue;
1238
1239                 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1240
1241                 if (subdomain->status != GPD_STATE_POWER_OFF
1242                     && subdomain->status != GPD_STATE_ACTIVE) {
1243                         mutex_unlock(&subdomain->lock);
1244                         genpd_release_lock(genpd);
1245                         goto start;
1246                 }
1247
1248                 list_del(&subdomain->sd_node);
1249                 subdomain->parent = NULL;
1250                 if (subdomain->status != GPD_STATE_POWER_OFF)
1251                         genpd_sd_counter_dec(genpd);
1252
1253                 mutex_unlock(&subdomain->lock);
1254
1255                 ret = 0;
1256                 break;
1257         }
1258
1259         genpd_release_lock(genpd);
1260
1261         return ret;
1262 }
1263
1264 /**
1265  * pm_genpd_init - Initialize a generic I/O PM domain object.
1266  * @genpd: PM domain object to initialize.
1267  * @gov: PM domain governor to associate with the domain (may be NULL).
1268  * @is_off: Initial value of the domain's power_is_off field.
1269  */
1270 void pm_genpd_init(struct generic_pm_domain *genpd,
1271                    struct dev_power_governor *gov, bool is_off)
1272 {
1273         if (IS_ERR_OR_NULL(genpd))
1274                 return;
1275
1276         INIT_LIST_HEAD(&genpd->sd_node);
1277         genpd->parent = NULL;
1278         INIT_LIST_HEAD(&genpd->dev_list);
1279         INIT_LIST_HEAD(&genpd->sd_list);
1280         mutex_init(&genpd->lock);
1281         genpd->gov = gov;
1282         INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1283         genpd->in_progress = 0;
1284         atomic_set(&genpd->sd_count, 0);
1285         genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1286         init_waitqueue_head(&genpd->status_wait_queue);
1287         genpd->poweroff_task = NULL;
1288         genpd->resume_count = 0;
1289         genpd->device_count = 0;
1290         genpd->suspended_count = 0;
1291         genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1292         genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1293         genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1294         genpd->domain.ops.prepare = pm_genpd_prepare;
1295         genpd->domain.ops.suspend = pm_genpd_suspend;
1296         genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1297         genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1298         genpd->domain.ops.resume = pm_genpd_resume;
1299         genpd->domain.ops.freeze = pm_genpd_freeze;
1300         genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1301         genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1302         genpd->domain.ops.thaw = pm_genpd_thaw;
1303         genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1304         genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1305         genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1306         genpd->domain.ops.restore = pm_genpd_restore;
1307         genpd->domain.ops.complete = pm_genpd_complete;
1308         mutex_lock(&gpd_list_lock);
1309         list_add(&genpd->gpd_list_node, &gpd_list);
1310         mutex_unlock(&gpd_list_lock);
1311 }