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