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