Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * drivers/base/power/domain.c - Common code related to device power domains.
4 *
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
6 */
7#define pr_fmt(fmt) "PM: " fmt
8
9#include <linux/delay.h>
10#include <linux/kernel.h>
11#include <linux/io.h>
12#include <linux/platform_device.h>
13#include <linux/pm_opp.h>
14#include <linux/pm_runtime.h>
15#include <linux/pm_domain.h>
16#include <linux/pm_qos.h>
17#include <linux/pm_clock.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20#include <linux/sched.h>
21#include <linux/suspend.h>
22#include <linux/export.h>
23#include <linux/cpu.h>
24
25#include "power.h"
26
27#define GENPD_RETRY_MAX_MS 250 /* Approximate */
28
29#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
30({ \
31 type (*__routine)(struct device *__d); \
32 type __ret = (type)0; \
33 \
34 __routine = genpd->dev_ops.callback; \
35 if (__routine) { \
36 __ret = __routine(dev); \
37 } \
38 __ret; \
39})
40
41static LIST_HEAD(gpd_list);
42static DEFINE_MUTEX(gpd_list_lock);
43
44struct genpd_lock_ops {
45 void (*lock)(struct generic_pm_domain *genpd);
46 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
47 int (*lock_interruptible)(struct generic_pm_domain *genpd);
48 void (*unlock)(struct generic_pm_domain *genpd);
49};
50
51static void genpd_lock_mtx(struct generic_pm_domain *genpd)
52{
53 mutex_lock(&genpd->mlock);
54}
55
56static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
57 int depth)
58{
59 mutex_lock_nested(&genpd->mlock, depth);
60}
61
62static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
63{
64 return mutex_lock_interruptible(&genpd->mlock);
65}
66
67static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
68{
69 return mutex_unlock(&genpd->mlock);
70}
71
72static const struct genpd_lock_ops genpd_mtx_ops = {
73 .lock = genpd_lock_mtx,
74 .lock_nested = genpd_lock_nested_mtx,
75 .lock_interruptible = genpd_lock_interruptible_mtx,
76 .unlock = genpd_unlock_mtx,
77};
78
79static void genpd_lock_spin(struct generic_pm_domain *genpd)
80 __acquires(&genpd->slock)
81{
82 unsigned long flags;
83
84 spin_lock_irqsave(&genpd->slock, flags);
85 genpd->lock_flags = flags;
86}
87
88static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
89 int depth)
90 __acquires(&genpd->slock)
91{
92 unsigned long flags;
93
94 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
95 genpd->lock_flags = flags;
96}
97
98static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
99 __acquires(&genpd->slock)
100{
101 unsigned long flags;
102
103 spin_lock_irqsave(&genpd->slock, flags);
104 genpd->lock_flags = flags;
105 return 0;
106}
107
108static void genpd_unlock_spin(struct generic_pm_domain *genpd)
109 __releases(&genpd->slock)
110{
111 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
112}
113
114static const struct genpd_lock_ops genpd_spin_ops = {
115 .lock = genpd_lock_spin,
116 .lock_nested = genpd_lock_nested_spin,
117 .lock_interruptible = genpd_lock_interruptible_spin,
118 .unlock = genpd_unlock_spin,
119};
120
121#define genpd_lock(p) p->lock_ops->lock(p)
122#define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
123#define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
124#define genpd_unlock(p) p->lock_ops->unlock(p)
125
126#define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
127#define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
128#define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
129#define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
130#define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
131#define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
132
133static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
134 const struct generic_pm_domain *genpd)
135{
136 bool ret;
137
138 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
139
140 /*
141 * Warn once if an IRQ safe device is attached to a no sleep domain, as
142 * to indicate a suboptimal configuration for PM. For an always on
143 * domain this isn't case, thus don't warn.
144 */
145 if (ret && !genpd_is_always_on(genpd))
146 dev_warn_once(dev, "PM domain %s will not be powered off\n",
147 genpd->name);
148
149 return ret;
150}
151
152static int genpd_runtime_suspend(struct device *dev);
153
154/*
155 * Get the generic PM domain for a particular struct device.
156 * This validates the struct device pointer, the PM domain pointer,
157 * and checks that the PM domain pointer is a real generic PM domain.
158 * Any failure results in NULL being returned.
159 */
160static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
161{
162 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
163 return NULL;
164
165 /* A genpd's always have its ->runtime_suspend() callback assigned. */
166 if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
167 return pd_to_genpd(dev->pm_domain);
168
169 return NULL;
170}
171
172/*
173 * This should only be used where we are certain that the pm_domain
174 * attached to the device is a genpd domain.
175 */
176static struct generic_pm_domain *dev_to_genpd(struct device *dev)
177{
178 if (IS_ERR_OR_NULL(dev->pm_domain))
179 return ERR_PTR(-EINVAL);
180
181 return pd_to_genpd(dev->pm_domain);
182}
183
184static int genpd_stop_dev(const struct generic_pm_domain *genpd,
185 struct device *dev)
186{
187 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
188}
189
190static int genpd_start_dev(const struct generic_pm_domain *genpd,
191 struct device *dev)
192{
193 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
194}
195
196static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
197{
198 bool ret = false;
199
200 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
201 ret = !!atomic_dec_and_test(&genpd->sd_count);
202
203 return ret;
204}
205
206static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
207{
208 atomic_inc(&genpd->sd_count);
209 smp_mb__after_atomic();
210}
211
212#ifdef CONFIG_DEBUG_FS
213static void genpd_update_accounting(struct generic_pm_domain *genpd)
214{
215 ktime_t delta, now;
216
217 now = ktime_get();
218 delta = ktime_sub(now, genpd->accounting_time);
219
220 /*
221 * If genpd->status is active, it means we are just
222 * out of off and so update the idle time and vice
223 * versa.
224 */
225 if (genpd->status == GPD_STATE_ACTIVE) {
226 int state_idx = genpd->state_idx;
227
228 genpd->states[state_idx].idle_time =
229 ktime_add(genpd->states[state_idx].idle_time, delta);
230 } else {
231 genpd->on_time = ktime_add(genpd->on_time, delta);
232 }
233
234 genpd->accounting_time = now;
235}
236#else
237static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
238#endif
239
240static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
241 unsigned int state)
242{
243 struct generic_pm_domain_data *pd_data;
244 struct pm_domain_data *pdd;
245 struct gpd_link *link;
246
247 /* New requested state is same as Max requested state */
248 if (state == genpd->performance_state)
249 return state;
250
251 /* New requested state is higher than Max requested state */
252 if (state > genpd->performance_state)
253 return state;
254
255 /* Traverse all devices within the domain */
256 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
257 pd_data = to_gpd_data(pdd);
258
259 if (pd_data->performance_state > state)
260 state = pd_data->performance_state;
261 }
262
263 /*
264 * Traverse all sub-domains within the domain. This can be
265 * done without any additional locking as the link->performance_state
266 * field is protected by the master genpd->lock, which is already taken.
267 *
268 * Also note that link->performance_state (subdomain's performance state
269 * requirement to master domain) is different from
270 * link->slave->performance_state (current performance state requirement
271 * of the devices/sub-domains of the subdomain) and so can have a
272 * different value.
273 *
274 * Note that we also take vote from powered-off sub-domains into account
275 * as the same is done for devices right now.
276 */
277 list_for_each_entry(link, &genpd->master_links, master_node) {
278 if (link->performance_state > state)
279 state = link->performance_state;
280 }
281
282 return state;
283}
284
285static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
286 unsigned int state, int depth)
287{
288 struct generic_pm_domain *master;
289 struct gpd_link *link;
290 int master_state, ret;
291
292 if (state == genpd->performance_state)
293 return 0;
294
295 /* Propagate to masters of genpd */
296 list_for_each_entry(link, &genpd->slave_links, slave_node) {
297 master = link->master;
298
299 if (!master->set_performance_state)
300 continue;
301
302 /* Find master's performance state */
303 ret = dev_pm_opp_xlate_performance_state(genpd->opp_table,
304 master->opp_table,
305 state);
306 if (unlikely(ret < 0))
307 goto err;
308
309 master_state = ret;
310
311 genpd_lock_nested(master, depth + 1);
312
313 link->prev_performance_state = link->performance_state;
314 link->performance_state = master_state;
315 master_state = _genpd_reeval_performance_state(master,
316 master_state);
317 ret = _genpd_set_performance_state(master, master_state, depth + 1);
318 if (ret)
319 link->performance_state = link->prev_performance_state;
320
321 genpd_unlock(master);
322
323 if (ret)
324 goto err;
325 }
326
327 ret = genpd->set_performance_state(genpd, state);
328 if (ret)
329 goto err;
330
331 genpd->performance_state = state;
332 return 0;
333
334err:
335 /* Encountered an error, lets rollback */
336 list_for_each_entry_continue_reverse(link, &genpd->slave_links,
337 slave_node) {
338 master = link->master;
339
340 if (!master->set_performance_state)
341 continue;
342
343 genpd_lock_nested(master, depth + 1);
344
345 master_state = link->prev_performance_state;
346 link->performance_state = master_state;
347
348 master_state = _genpd_reeval_performance_state(master,
349 master_state);
350 if (_genpd_set_performance_state(master, master_state, depth + 1)) {
351 pr_err("%s: Failed to roll back to %d performance state\n",
352 master->name, master_state);
353 }
354
355 genpd_unlock(master);
356 }
357
358 return ret;
359}
360
361/**
362 * dev_pm_genpd_set_performance_state- Set performance state of device's power
363 * domain.
364 *
365 * @dev: Device for which the performance-state needs to be set.
366 * @state: Target performance state of the device. This can be set as 0 when the
367 * device doesn't have any performance state constraints left (And so
368 * the device wouldn't participate anymore to find the target
369 * performance state of the genpd).
370 *
371 * It is assumed that the users guarantee that the genpd wouldn't be detached
372 * while this routine is getting called.
373 *
374 * Returns 0 on success and negative error values on failures.
375 */
376int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
377{
378 struct generic_pm_domain *genpd;
379 struct generic_pm_domain_data *gpd_data;
380 unsigned int prev;
381 int ret;
382
383 genpd = dev_to_genpd_safe(dev);
384 if (!genpd)
385 return -ENODEV;
386
387 if (unlikely(!genpd->set_performance_state))
388 return -EINVAL;
389
390 if (WARN_ON(!dev->power.subsys_data ||
391 !dev->power.subsys_data->domain_data))
392 return -EINVAL;
393
394 genpd_lock(genpd);
395
396 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
397 prev = gpd_data->performance_state;
398 gpd_data->performance_state = state;
399
400 state = _genpd_reeval_performance_state(genpd, state);
401 ret = _genpd_set_performance_state(genpd, state, 0);
402 if (ret)
403 gpd_data->performance_state = prev;
404
405 genpd_unlock(genpd);
406
407 return ret;
408}
409EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
410
411static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
412{
413 unsigned int state_idx = genpd->state_idx;
414 ktime_t time_start;
415 s64 elapsed_ns;
416 int ret;
417
418 if (!genpd->power_on)
419 return 0;
420
421 if (!timed)
422 return genpd->power_on(genpd);
423
424 time_start = ktime_get();
425 ret = genpd->power_on(genpd);
426 if (ret)
427 return ret;
428
429 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
430 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
431 return ret;
432
433 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
434 genpd->max_off_time_changed = true;
435 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
436 genpd->name, "on", elapsed_ns);
437
438 return ret;
439}
440
441static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
442{
443 unsigned int state_idx = genpd->state_idx;
444 ktime_t time_start;
445 s64 elapsed_ns;
446 int ret;
447
448 if (!genpd->power_off)
449 return 0;
450
451 if (!timed)
452 return genpd->power_off(genpd);
453
454 time_start = ktime_get();
455 ret = genpd->power_off(genpd);
456 if (ret)
457 return ret;
458
459 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
460 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
461 return 0;
462
463 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
464 genpd->max_off_time_changed = true;
465 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
466 genpd->name, "off", elapsed_ns);
467
468 return 0;
469}
470
471/**
472 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
473 * @genpd: PM domain to power off.
474 *
475 * Queue up the execution of genpd_power_off() unless it's already been done
476 * before.
477 */
478static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
479{
480 queue_work(pm_wq, &genpd->power_off_work);
481}
482
483/**
484 * genpd_power_off - Remove power from a given PM domain.
485 * @genpd: PM domain to power down.
486 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
487 * RPM status of the releated device is in an intermediate state, not yet turned
488 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
489 * be RPM_SUSPENDED, while it tries to power off the PM domain.
490 *
491 * If all of the @genpd's devices have been suspended and all of its subdomains
492 * have been powered down, remove power from @genpd.
493 */
494static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
495 unsigned int depth)
496{
497 struct pm_domain_data *pdd;
498 struct gpd_link *link;
499 unsigned int not_suspended = 0;
500
501 /*
502 * Do not try to power off the domain in the following situations:
503 * (1) The domain is already in the "power off" state.
504 * (2) System suspend is in progress.
505 */
506 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
507 return 0;
508
509 /*
510 * Abort power off for the PM domain in the following situations:
511 * (1) The domain is configured as always on.
512 * (2) When the domain has a subdomain being powered on.
513 */
514 if (genpd_is_always_on(genpd) ||
515 genpd_is_rpm_always_on(genpd) ||
516 atomic_read(&genpd->sd_count) > 0)
517 return -EBUSY;
518
519 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
520 enum pm_qos_flags_status stat;
521
522 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
523 if (stat > PM_QOS_FLAGS_NONE)
524 return -EBUSY;
525
526 /*
527 * Do not allow PM domain to be powered off, when an IRQ safe
528 * device is part of a non-IRQ safe domain.
529 */
530 if (!pm_runtime_suspended(pdd->dev) ||
531 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
532 not_suspended++;
533 }
534
535 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
536 return -EBUSY;
537
538 if (genpd->gov && genpd->gov->power_down_ok) {
539 if (!genpd->gov->power_down_ok(&genpd->domain))
540 return -EAGAIN;
541 }
542
543 /* Default to shallowest state. */
544 if (!genpd->gov)
545 genpd->state_idx = 0;
546
547 if (genpd->power_off) {
548 int ret;
549
550 if (atomic_read(&genpd->sd_count) > 0)
551 return -EBUSY;
552
553 /*
554 * If sd_count > 0 at this point, one of the subdomains hasn't
555 * managed to call genpd_power_on() for the master yet after
556 * incrementing it. In that case genpd_power_on() will wait
557 * for us to drop the lock, so we can call .power_off() and let
558 * the genpd_power_on() restore power for us (this shouldn't
559 * happen very often).
560 */
561 ret = _genpd_power_off(genpd, true);
562 if (ret)
563 return ret;
564 }
565
566 genpd->status = GPD_STATE_POWER_OFF;
567 genpd_update_accounting(genpd);
568
569 list_for_each_entry(link, &genpd->slave_links, slave_node) {
570 genpd_sd_counter_dec(link->master);
571 genpd_lock_nested(link->master, depth + 1);
572 genpd_power_off(link->master, false, depth + 1);
573 genpd_unlock(link->master);
574 }
575
576 return 0;
577}
578
579/**
580 * genpd_power_on - Restore power to a given PM domain and its masters.
581 * @genpd: PM domain to power up.
582 * @depth: nesting count for lockdep.
583 *
584 * Restore power to @genpd and all of its masters so that it is possible to
585 * resume a device belonging to it.
586 */
587static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
588{
589 struct gpd_link *link;
590 int ret = 0;
591
592 if (genpd_status_on(genpd))
593 return 0;
594
595 /*
596 * The list is guaranteed not to change while the loop below is being
597 * executed, unless one of the masters' .power_on() callbacks fiddles
598 * with it.
599 */
600 list_for_each_entry(link, &genpd->slave_links, slave_node) {
601 struct generic_pm_domain *master = link->master;
602
603 genpd_sd_counter_inc(master);
604
605 genpd_lock_nested(master, depth + 1);
606 ret = genpd_power_on(master, depth + 1);
607 genpd_unlock(master);
608
609 if (ret) {
610 genpd_sd_counter_dec(master);
611 goto err;
612 }
613 }
614
615 ret = _genpd_power_on(genpd, true);
616 if (ret)
617 goto err;
618
619 genpd->status = GPD_STATE_ACTIVE;
620 genpd_update_accounting(genpd);
621
622 return 0;
623
624 err:
625 list_for_each_entry_continue_reverse(link,
626 &genpd->slave_links,
627 slave_node) {
628 genpd_sd_counter_dec(link->master);
629 genpd_lock_nested(link->master, depth + 1);
630 genpd_power_off(link->master, false, depth + 1);
631 genpd_unlock(link->master);
632 }
633
634 return ret;
635}
636
637static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
638 unsigned long val, void *ptr)
639{
640 struct generic_pm_domain_data *gpd_data;
641 struct device *dev;
642
643 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
644 dev = gpd_data->base.dev;
645
646 for (;;) {
647 struct generic_pm_domain *genpd;
648 struct pm_domain_data *pdd;
649
650 spin_lock_irq(&dev->power.lock);
651
652 pdd = dev->power.subsys_data ?
653 dev->power.subsys_data->domain_data : NULL;
654 if (pdd) {
655 to_gpd_data(pdd)->td.constraint_changed = true;
656 genpd = dev_to_genpd(dev);
657 } else {
658 genpd = ERR_PTR(-ENODATA);
659 }
660
661 spin_unlock_irq(&dev->power.lock);
662
663 if (!IS_ERR(genpd)) {
664 genpd_lock(genpd);
665 genpd->max_off_time_changed = true;
666 genpd_unlock(genpd);
667 }
668
669 dev = dev->parent;
670 if (!dev || dev->power.ignore_children)
671 break;
672 }
673
674 return NOTIFY_DONE;
675}
676
677/**
678 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
679 * @work: Work structure used for scheduling the execution of this function.
680 */
681static void genpd_power_off_work_fn(struct work_struct *work)
682{
683 struct generic_pm_domain *genpd;
684
685 genpd = container_of(work, struct generic_pm_domain, power_off_work);
686
687 genpd_lock(genpd);
688 genpd_power_off(genpd, false, 0);
689 genpd_unlock(genpd);
690}
691
692/**
693 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
694 * @dev: Device to handle.
695 */
696static int __genpd_runtime_suspend(struct device *dev)
697{
698 int (*cb)(struct device *__dev);
699
700 if (dev->type && dev->type->pm)
701 cb = dev->type->pm->runtime_suspend;
702 else if (dev->class && dev->class->pm)
703 cb = dev->class->pm->runtime_suspend;
704 else if (dev->bus && dev->bus->pm)
705 cb = dev->bus->pm->runtime_suspend;
706 else
707 cb = NULL;
708
709 if (!cb && dev->driver && dev->driver->pm)
710 cb = dev->driver->pm->runtime_suspend;
711
712 return cb ? cb(dev) : 0;
713}
714
715/**
716 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
717 * @dev: Device to handle.
718 */
719static int __genpd_runtime_resume(struct device *dev)
720{
721 int (*cb)(struct device *__dev);
722
723 if (dev->type && dev->type->pm)
724 cb = dev->type->pm->runtime_resume;
725 else if (dev->class && dev->class->pm)
726 cb = dev->class->pm->runtime_resume;
727 else if (dev->bus && dev->bus->pm)
728 cb = dev->bus->pm->runtime_resume;
729 else
730 cb = NULL;
731
732 if (!cb && dev->driver && dev->driver->pm)
733 cb = dev->driver->pm->runtime_resume;
734
735 return cb ? cb(dev) : 0;
736}
737
738/**
739 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
740 * @dev: Device to suspend.
741 *
742 * Carry out a runtime suspend of a device under the assumption that its
743 * pm_domain field points to the domain member of an object of type
744 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
745 */
746static int genpd_runtime_suspend(struct device *dev)
747{
748 struct generic_pm_domain *genpd;
749 bool (*suspend_ok)(struct device *__dev);
750 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
751 bool runtime_pm = pm_runtime_enabled(dev);
752 ktime_t time_start;
753 s64 elapsed_ns;
754 int ret;
755
756 dev_dbg(dev, "%s()\n", __func__);
757
758 genpd = dev_to_genpd(dev);
759 if (IS_ERR(genpd))
760 return -EINVAL;
761
762 /*
763 * A runtime PM centric subsystem/driver may re-use the runtime PM
764 * callbacks for other purposes than runtime PM. In those scenarios
765 * runtime PM is disabled. Under these circumstances, we shall skip
766 * validating/measuring the PM QoS latency.
767 */
768 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
769 if (runtime_pm && suspend_ok && !suspend_ok(dev))
770 return -EBUSY;
771
772 /* Measure suspend latency. */
773 time_start = 0;
774 if (runtime_pm)
775 time_start = ktime_get();
776
777 ret = __genpd_runtime_suspend(dev);
778 if (ret)
779 return ret;
780
781 ret = genpd_stop_dev(genpd, dev);
782 if (ret) {
783 __genpd_runtime_resume(dev);
784 return ret;
785 }
786
787 /* Update suspend latency value if the measured time exceeds it. */
788 if (runtime_pm) {
789 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
790 if (elapsed_ns > td->suspend_latency_ns) {
791 td->suspend_latency_ns = elapsed_ns;
792 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
793 elapsed_ns);
794 genpd->max_off_time_changed = true;
795 td->constraint_changed = true;
796 }
797 }
798
799 /*
800 * If power.irq_safe is set, this routine may be run with
801 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
802 */
803 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
804 return 0;
805
806 genpd_lock(genpd);
807 genpd_power_off(genpd, true, 0);
808 genpd_unlock(genpd);
809
810 return 0;
811}
812
813/**
814 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
815 * @dev: Device to resume.
816 *
817 * Carry out a runtime resume of a device under the assumption that its
818 * pm_domain field points to the domain member of an object of type
819 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
820 */
821static int genpd_runtime_resume(struct device *dev)
822{
823 struct generic_pm_domain *genpd;
824 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
825 bool runtime_pm = pm_runtime_enabled(dev);
826 ktime_t time_start;
827 s64 elapsed_ns;
828 int ret;
829 bool timed = true;
830
831 dev_dbg(dev, "%s()\n", __func__);
832
833 genpd = dev_to_genpd(dev);
834 if (IS_ERR(genpd))
835 return -EINVAL;
836
837 /*
838 * As we don't power off a non IRQ safe domain, which holds
839 * an IRQ safe device, we don't need to restore power to it.
840 */
841 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
842 timed = false;
843 goto out;
844 }
845
846 genpd_lock(genpd);
847 ret = genpd_power_on(genpd, 0);
848 genpd_unlock(genpd);
849
850 if (ret)
851 return ret;
852
853 out:
854 /* Measure resume latency. */
855 time_start = 0;
856 if (timed && runtime_pm)
857 time_start = ktime_get();
858
859 ret = genpd_start_dev(genpd, dev);
860 if (ret)
861 goto err_poweroff;
862
863 ret = __genpd_runtime_resume(dev);
864 if (ret)
865 goto err_stop;
866
867 /* Update resume latency value if the measured time exceeds it. */
868 if (timed && runtime_pm) {
869 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
870 if (elapsed_ns > td->resume_latency_ns) {
871 td->resume_latency_ns = elapsed_ns;
872 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
873 elapsed_ns);
874 genpd->max_off_time_changed = true;
875 td->constraint_changed = true;
876 }
877 }
878
879 return 0;
880
881err_stop:
882 genpd_stop_dev(genpd, dev);
883err_poweroff:
884 if (!pm_runtime_is_irq_safe(dev) ||
885 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
886 genpd_lock(genpd);
887 genpd_power_off(genpd, true, 0);
888 genpd_unlock(genpd);
889 }
890
891 return ret;
892}
893
894static bool pd_ignore_unused;
895static int __init pd_ignore_unused_setup(char *__unused)
896{
897 pd_ignore_unused = true;
898 return 1;
899}
900__setup("pd_ignore_unused", pd_ignore_unused_setup);
901
902/**
903 * genpd_power_off_unused - Power off all PM domains with no devices in use.
904 */
905static int __init genpd_power_off_unused(void)
906{
907 struct generic_pm_domain *genpd;
908
909 if (pd_ignore_unused) {
910 pr_warn("genpd: Not disabling unused power domains\n");
911 return 0;
912 }
913
914 mutex_lock(&gpd_list_lock);
915
916 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
917 genpd_queue_power_off_work(genpd);
918
919 mutex_unlock(&gpd_list_lock);
920
921 return 0;
922}
923late_initcall(genpd_power_off_unused);
924
925#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
926
927static bool genpd_present(const struct generic_pm_domain *genpd)
928{
929 const struct generic_pm_domain *gpd;
930
931 if (IS_ERR_OR_NULL(genpd))
932 return false;
933
934 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
935 if (gpd == genpd)
936 return true;
937
938 return false;
939}
940
941#endif
942
943#ifdef CONFIG_PM_SLEEP
944
945/**
946 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
947 * @genpd: PM domain to power off, if possible.
948 * @use_lock: use the lock.
949 * @depth: nesting count for lockdep.
950 *
951 * Check if the given PM domain can be powered off (during system suspend or
952 * hibernation) and do that if so. Also, in that case propagate to its masters.
953 *
954 * This function is only called in "noirq" and "syscore" stages of system power
955 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
956 * these cases the lock must be held.
957 */
958static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
959 unsigned int depth)
960{
961 struct gpd_link *link;
962
963 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
964 return;
965
966 if (genpd->suspended_count != genpd->device_count
967 || atomic_read(&genpd->sd_count) > 0)
968 return;
969
970 /* Choose the deepest state when suspending */
971 genpd->state_idx = genpd->state_count - 1;
972 if (_genpd_power_off(genpd, false))
973 return;
974
975 genpd->status = GPD_STATE_POWER_OFF;
976
977 list_for_each_entry(link, &genpd->slave_links, slave_node) {
978 genpd_sd_counter_dec(link->master);
979
980 if (use_lock)
981 genpd_lock_nested(link->master, depth + 1);
982
983 genpd_sync_power_off(link->master, use_lock, depth + 1);
984
985 if (use_lock)
986 genpd_unlock(link->master);
987 }
988}
989
990/**
991 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
992 * @genpd: PM domain to power on.
993 * @use_lock: use the lock.
994 * @depth: nesting count for lockdep.
995 *
996 * This function is only called in "noirq" and "syscore" stages of system power
997 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
998 * these cases the lock must be held.
999 */
1000static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
1001 unsigned int depth)
1002{
1003 struct gpd_link *link;
1004
1005 if (genpd_status_on(genpd))
1006 return;
1007
1008 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1009 genpd_sd_counter_inc(link->master);
1010
1011 if (use_lock)
1012 genpd_lock_nested(link->master, depth + 1);
1013
1014 genpd_sync_power_on(link->master, use_lock, depth + 1);
1015
1016 if (use_lock)
1017 genpd_unlock(link->master);
1018 }
1019
1020 _genpd_power_on(genpd, false);
1021
1022 genpd->status = GPD_STATE_ACTIVE;
1023}
1024
1025/**
1026 * resume_needed - Check whether to resume a device before system suspend.
1027 * @dev: Device to check.
1028 * @genpd: PM domain the device belongs to.
1029 *
1030 * There are two cases in which a device that can wake up the system from sleep
1031 * states should be resumed by genpd_prepare(): (1) if the device is enabled
1032 * to wake up the system and it has to remain active for this purpose while the
1033 * system is in the sleep state and (2) if the device is not enabled to wake up
1034 * the system from sleep states and it generally doesn't generate wakeup signals
1035 * by itself (those signals are generated on its behalf by other parts of the
1036 * system). In the latter case it may be necessary to reconfigure the device's
1037 * wakeup settings during system suspend, because it may have been set up to
1038 * signal remote wakeup from the system's working state as needed by runtime PM.
1039 * Return 'true' in either of the above cases.
1040 */
1041static bool resume_needed(struct device *dev,
1042 const struct generic_pm_domain *genpd)
1043{
1044 bool active_wakeup;
1045
1046 if (!device_can_wakeup(dev))
1047 return false;
1048
1049 active_wakeup = genpd_is_active_wakeup(genpd);
1050 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
1051}
1052
1053/**
1054 * genpd_prepare - Start power transition of a device in a PM domain.
1055 * @dev: Device to start the transition of.
1056 *
1057 * Start a power transition of a device (during a system-wide power transition)
1058 * under the assumption that its pm_domain field points to the domain member of
1059 * an object of type struct generic_pm_domain representing a PM domain
1060 * consisting of I/O devices.
1061 */
1062static int genpd_prepare(struct device *dev)
1063{
1064 struct generic_pm_domain *genpd;
1065 int ret;
1066
1067 dev_dbg(dev, "%s()\n", __func__);
1068
1069 genpd = dev_to_genpd(dev);
1070 if (IS_ERR(genpd))
1071 return -EINVAL;
1072
1073 /*
1074 * If a wakeup request is pending for the device, it should be woken up
1075 * at this point and a system wakeup event should be reported if it's
1076 * set up to wake up the system from sleep states.
1077 */
1078 if (resume_needed(dev, genpd))
1079 pm_runtime_resume(dev);
1080
1081 genpd_lock(genpd);
1082
1083 if (genpd->prepared_count++ == 0)
1084 genpd->suspended_count = 0;
1085
1086 genpd_unlock(genpd);
1087
1088 ret = pm_generic_prepare(dev);
1089 if (ret < 0) {
1090 genpd_lock(genpd);
1091
1092 genpd->prepared_count--;
1093
1094 genpd_unlock(genpd);
1095 }
1096
1097 /* Never return 1, as genpd don't cope with the direct_complete path. */
1098 return ret >= 0 ? 0 : ret;
1099}
1100
1101/**
1102 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1103 * I/O pm domain.
1104 * @dev: Device to suspend.
1105 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1106 *
1107 * Stop the device and remove power from the domain if all devices in it have
1108 * been stopped.
1109 */
1110static int genpd_finish_suspend(struct device *dev, bool poweroff)
1111{
1112 struct generic_pm_domain *genpd;
1113 int ret = 0;
1114
1115 genpd = dev_to_genpd(dev);
1116 if (IS_ERR(genpd))
1117 return -EINVAL;
1118
1119 if (poweroff)
1120 ret = pm_generic_poweroff_noirq(dev);
1121 else
1122 ret = pm_generic_suspend_noirq(dev);
1123 if (ret)
1124 return ret;
1125
1126 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1127 return 0;
1128
1129 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1130 !pm_runtime_status_suspended(dev)) {
1131 ret = genpd_stop_dev(genpd, dev);
1132 if (ret) {
1133 if (poweroff)
1134 pm_generic_restore_noirq(dev);
1135 else
1136 pm_generic_resume_noirq(dev);
1137 return ret;
1138 }
1139 }
1140
1141 genpd_lock(genpd);
1142 genpd->suspended_count++;
1143 genpd_sync_power_off(genpd, true, 0);
1144 genpd_unlock(genpd);
1145
1146 return 0;
1147}
1148
1149/**
1150 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1151 * @dev: Device to suspend.
1152 *
1153 * Stop the device and remove power from the domain if all devices in it have
1154 * been stopped.
1155 */
1156static int genpd_suspend_noirq(struct device *dev)
1157{
1158 dev_dbg(dev, "%s()\n", __func__);
1159
1160 return genpd_finish_suspend(dev, false);
1161}
1162
1163/**
1164 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1165 * @dev: Device to resume.
1166 *
1167 * Restore power to the device's PM domain, if necessary, and start the device.
1168 */
1169static int genpd_resume_noirq(struct device *dev)
1170{
1171 struct generic_pm_domain *genpd;
1172 int ret;
1173
1174 dev_dbg(dev, "%s()\n", __func__);
1175
1176 genpd = dev_to_genpd(dev);
1177 if (IS_ERR(genpd))
1178 return -EINVAL;
1179
1180 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1181 return pm_generic_resume_noirq(dev);
1182
1183 genpd_lock(genpd);
1184 genpd_sync_power_on(genpd, true, 0);
1185 genpd->suspended_count--;
1186 genpd_unlock(genpd);
1187
1188 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1189 !pm_runtime_status_suspended(dev)) {
1190 ret = genpd_start_dev(genpd, dev);
1191 if (ret)
1192 return ret;
1193 }
1194
1195 return pm_generic_resume_noirq(dev);
1196}
1197
1198/**
1199 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1200 * @dev: Device to freeze.
1201 *
1202 * Carry out a late freeze of a device under the assumption that its
1203 * pm_domain field points to the domain member of an object of type
1204 * struct generic_pm_domain representing a power domain consisting of I/O
1205 * devices.
1206 */
1207static int genpd_freeze_noirq(struct device *dev)
1208{
1209 const struct generic_pm_domain *genpd;
1210 int ret = 0;
1211
1212 dev_dbg(dev, "%s()\n", __func__);
1213
1214 genpd = dev_to_genpd(dev);
1215 if (IS_ERR(genpd))
1216 return -EINVAL;
1217
1218 ret = pm_generic_freeze_noirq(dev);
1219 if (ret)
1220 return ret;
1221
1222 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1223 !pm_runtime_status_suspended(dev))
1224 ret = genpd_stop_dev(genpd, dev);
1225
1226 return ret;
1227}
1228
1229/**
1230 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1231 * @dev: Device to thaw.
1232 *
1233 * Start the device, unless power has been removed from the domain already
1234 * before the system transition.
1235 */
1236static int genpd_thaw_noirq(struct device *dev)
1237{
1238 const struct generic_pm_domain *genpd;
1239 int ret = 0;
1240
1241 dev_dbg(dev, "%s()\n", __func__);
1242
1243 genpd = dev_to_genpd(dev);
1244 if (IS_ERR(genpd))
1245 return -EINVAL;
1246
1247 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1248 !pm_runtime_status_suspended(dev)) {
1249 ret = genpd_start_dev(genpd, dev);
1250 if (ret)
1251 return ret;
1252 }
1253
1254 return pm_generic_thaw_noirq(dev);
1255}
1256
1257/**
1258 * genpd_poweroff_noirq - Completion of hibernation of device in an
1259 * I/O PM domain.
1260 * @dev: Device to poweroff.
1261 *
1262 * Stop the device and remove power from the domain if all devices in it have
1263 * been stopped.
1264 */
1265static int genpd_poweroff_noirq(struct device *dev)
1266{
1267 dev_dbg(dev, "%s()\n", __func__);
1268
1269 return genpd_finish_suspend(dev, true);
1270}
1271
1272/**
1273 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1274 * @dev: Device to resume.
1275 *
1276 * Make sure the domain will be in the same power state as before the
1277 * hibernation the system is resuming from and start the device if necessary.
1278 */
1279static int genpd_restore_noirq(struct device *dev)
1280{
1281 struct generic_pm_domain *genpd;
1282 int ret = 0;
1283
1284 dev_dbg(dev, "%s()\n", __func__);
1285
1286 genpd = dev_to_genpd(dev);
1287 if (IS_ERR(genpd))
1288 return -EINVAL;
1289
1290 /*
1291 * At this point suspended_count == 0 means we are being run for the
1292 * first time for the given domain in the present cycle.
1293 */
1294 genpd_lock(genpd);
1295 if (genpd->suspended_count++ == 0)
1296 /*
1297 * The boot kernel might put the domain into arbitrary state,
1298 * so make it appear as powered off to genpd_sync_power_on(),
1299 * so that it tries to power it on in case it was really off.
1300 */
1301 genpd->status = GPD_STATE_POWER_OFF;
1302
1303 genpd_sync_power_on(genpd, true, 0);
1304 genpd_unlock(genpd);
1305
1306 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1307 !pm_runtime_status_suspended(dev)) {
1308 ret = genpd_start_dev(genpd, dev);
1309 if (ret)
1310 return ret;
1311 }
1312
1313 return pm_generic_restore_noirq(dev);
1314}
1315
1316/**
1317 * genpd_complete - Complete power transition of a device in a power domain.
1318 * @dev: Device to complete the transition of.
1319 *
1320 * Complete a power transition of a device (during a system-wide power
1321 * transition) under the assumption that its pm_domain field points to the
1322 * domain member of an object of type struct generic_pm_domain representing
1323 * a power domain consisting of I/O devices.
1324 */
1325static void genpd_complete(struct device *dev)
1326{
1327 struct generic_pm_domain *genpd;
1328
1329 dev_dbg(dev, "%s()\n", __func__);
1330
1331 genpd = dev_to_genpd(dev);
1332 if (IS_ERR(genpd))
1333 return;
1334
1335 pm_generic_complete(dev);
1336
1337 genpd_lock(genpd);
1338
1339 genpd->prepared_count--;
1340 if (!genpd->prepared_count)
1341 genpd_queue_power_off_work(genpd);
1342
1343 genpd_unlock(genpd);
1344}
1345
1346/**
1347 * genpd_syscore_switch - Switch power during system core suspend or resume.
1348 * @dev: Device that normally is marked as "always on" to switch power for.
1349 *
1350 * This routine may only be called during the system core (syscore) suspend or
1351 * resume phase for devices whose "always on" flags are set.
1352 */
1353static void genpd_syscore_switch(struct device *dev, bool suspend)
1354{
1355 struct generic_pm_domain *genpd;
1356
1357 genpd = dev_to_genpd(dev);
1358 if (!genpd_present(genpd))
1359 return;
1360
1361 if (suspend) {
1362 genpd->suspended_count++;
1363 genpd_sync_power_off(genpd, false, 0);
1364 } else {
1365 genpd_sync_power_on(genpd, false, 0);
1366 genpd->suspended_count--;
1367 }
1368}
1369
1370void pm_genpd_syscore_poweroff(struct device *dev)
1371{
1372 genpd_syscore_switch(dev, true);
1373}
1374EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1375
1376void pm_genpd_syscore_poweron(struct device *dev)
1377{
1378 genpd_syscore_switch(dev, false);
1379}
1380EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1381
1382#else /* !CONFIG_PM_SLEEP */
1383
1384#define genpd_prepare NULL
1385#define genpd_suspend_noirq NULL
1386#define genpd_resume_noirq NULL
1387#define genpd_freeze_noirq NULL
1388#define genpd_thaw_noirq NULL
1389#define genpd_poweroff_noirq NULL
1390#define genpd_restore_noirq NULL
1391#define genpd_complete NULL
1392
1393#endif /* CONFIG_PM_SLEEP */
1394
1395static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev)
1396{
1397 struct generic_pm_domain_data *gpd_data;
1398 int ret;
1399
1400 ret = dev_pm_get_subsys_data(dev);
1401 if (ret)
1402 return ERR_PTR(ret);
1403
1404 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1405 if (!gpd_data) {
1406 ret = -ENOMEM;
1407 goto err_put;
1408 }
1409
1410 gpd_data->base.dev = dev;
1411 gpd_data->td.constraint_changed = true;
1412 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1413 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1414
1415 spin_lock_irq(&dev->power.lock);
1416
1417 if (dev->power.subsys_data->domain_data) {
1418 ret = -EINVAL;
1419 goto err_free;
1420 }
1421
1422 dev->power.subsys_data->domain_data = &gpd_data->base;
1423
1424 spin_unlock_irq(&dev->power.lock);
1425
1426 return gpd_data;
1427
1428 err_free:
1429 spin_unlock_irq(&dev->power.lock);
1430 kfree(gpd_data);
1431 err_put:
1432 dev_pm_put_subsys_data(dev);
1433 return ERR_PTR(ret);
1434}
1435
1436static void genpd_free_dev_data(struct device *dev,
1437 struct generic_pm_domain_data *gpd_data)
1438{
1439 spin_lock_irq(&dev->power.lock);
1440
1441 dev->power.subsys_data->domain_data = NULL;
1442
1443 spin_unlock_irq(&dev->power.lock);
1444
1445 kfree(gpd_data);
1446 dev_pm_put_subsys_data(dev);
1447}
1448
1449static void genpd_update_cpumask(struct generic_pm_domain *genpd,
1450 int cpu, bool set, unsigned int depth)
1451{
1452 struct gpd_link *link;
1453
1454 if (!genpd_is_cpu_domain(genpd))
1455 return;
1456
1457 list_for_each_entry(link, &genpd->slave_links, slave_node) {
1458 struct generic_pm_domain *master = link->master;
1459
1460 genpd_lock_nested(master, depth + 1);
1461 genpd_update_cpumask(master, cpu, set, depth + 1);
1462 genpd_unlock(master);
1463 }
1464
1465 if (set)
1466 cpumask_set_cpu(cpu, genpd->cpus);
1467 else
1468 cpumask_clear_cpu(cpu, genpd->cpus);
1469}
1470
1471static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
1472{
1473 if (cpu >= 0)
1474 genpd_update_cpumask(genpd, cpu, true, 0);
1475}
1476
1477static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
1478{
1479 if (cpu >= 0)
1480 genpd_update_cpumask(genpd, cpu, false, 0);
1481}
1482
1483static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
1484{
1485 int cpu;
1486
1487 if (!genpd_is_cpu_domain(genpd))
1488 return -1;
1489
1490 for_each_possible_cpu(cpu) {
1491 if (get_cpu_device(cpu) == dev)
1492 return cpu;
1493 }
1494
1495 return -1;
1496}
1497
1498static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1499 struct device *base_dev)
1500{
1501 struct generic_pm_domain_data *gpd_data;
1502 int ret;
1503
1504 dev_dbg(dev, "%s()\n", __func__);
1505
1506 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1507 return -EINVAL;
1508
1509 gpd_data = genpd_alloc_dev_data(dev);
1510 if (IS_ERR(gpd_data))
1511 return PTR_ERR(gpd_data);
1512
1513 gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
1514
1515 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1516 if (ret)
1517 goto out;
1518
1519 genpd_lock(genpd);
1520
1521 genpd_set_cpumask(genpd, gpd_data->cpu);
1522 dev_pm_domain_set(dev, &genpd->domain);
1523
1524 genpd->device_count++;
1525 genpd->max_off_time_changed = true;
1526
1527 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1528
1529 genpd_unlock(genpd);
1530 out:
1531 if (ret)
1532 genpd_free_dev_data(dev, gpd_data);
1533 else
1534 dev_pm_qos_add_notifier(dev, &gpd_data->nb,
1535 DEV_PM_QOS_RESUME_LATENCY);
1536
1537 return ret;
1538}
1539
1540/**
1541 * pm_genpd_add_device - Add a device to an I/O PM domain.
1542 * @genpd: PM domain to add the device to.
1543 * @dev: Device to be added.
1544 */
1545int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1546{
1547 int ret;
1548
1549 mutex_lock(&gpd_list_lock);
1550 ret = genpd_add_device(genpd, dev, dev);
1551 mutex_unlock(&gpd_list_lock);
1552
1553 return ret;
1554}
1555EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1556
1557static int genpd_remove_device(struct generic_pm_domain *genpd,
1558 struct device *dev)
1559{
1560 struct generic_pm_domain_data *gpd_data;
1561 struct pm_domain_data *pdd;
1562 int ret = 0;
1563
1564 dev_dbg(dev, "%s()\n", __func__);
1565
1566 pdd = dev->power.subsys_data->domain_data;
1567 gpd_data = to_gpd_data(pdd);
1568 dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
1569 DEV_PM_QOS_RESUME_LATENCY);
1570
1571 genpd_lock(genpd);
1572
1573 if (genpd->prepared_count > 0) {
1574 ret = -EAGAIN;
1575 goto out;
1576 }
1577
1578 genpd->device_count--;
1579 genpd->max_off_time_changed = true;
1580
1581 genpd_clear_cpumask(genpd, gpd_data->cpu);
1582 dev_pm_domain_set(dev, NULL);
1583
1584 list_del_init(&pdd->list_node);
1585
1586 genpd_unlock(genpd);
1587
1588 if (genpd->detach_dev)
1589 genpd->detach_dev(genpd, dev);
1590
1591 genpd_free_dev_data(dev, gpd_data);
1592
1593 return 0;
1594
1595 out:
1596 genpd_unlock(genpd);
1597 dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);
1598
1599 return ret;
1600}
1601
1602/**
1603 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1604 * @dev: Device to be removed.
1605 */
1606int pm_genpd_remove_device(struct device *dev)
1607{
1608 struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);
1609
1610 if (!genpd)
1611 return -EINVAL;
1612
1613 return genpd_remove_device(genpd, dev);
1614}
1615EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1616
1617static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1618 struct generic_pm_domain *subdomain)
1619{
1620 struct gpd_link *link, *itr;
1621 int ret = 0;
1622
1623 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1624 || genpd == subdomain)
1625 return -EINVAL;
1626
1627 /*
1628 * If the domain can be powered on/off in an IRQ safe
1629 * context, ensure that the subdomain can also be
1630 * powered on/off in that context.
1631 */
1632 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1633 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1634 genpd->name, subdomain->name);
1635 return -EINVAL;
1636 }
1637
1638 link = kzalloc(sizeof(*link), GFP_KERNEL);
1639 if (!link)
1640 return -ENOMEM;
1641
1642 genpd_lock(subdomain);
1643 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1644
1645 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1646 ret = -EINVAL;
1647 goto out;
1648 }
1649
1650 list_for_each_entry(itr, &genpd->master_links, master_node) {
1651 if (itr->slave == subdomain && itr->master == genpd) {
1652 ret = -EINVAL;
1653 goto out;
1654 }
1655 }
1656
1657 link->master = genpd;
1658 list_add_tail(&link->master_node, &genpd->master_links);
1659 link->slave = subdomain;
1660 list_add_tail(&link->slave_node, &subdomain->slave_links);
1661 if (genpd_status_on(subdomain))
1662 genpd_sd_counter_inc(genpd);
1663
1664 out:
1665 genpd_unlock(genpd);
1666 genpd_unlock(subdomain);
1667 if (ret)
1668 kfree(link);
1669 return ret;
1670}
1671
1672/**
1673 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1674 * @genpd: Master PM domain to add the subdomain to.
1675 * @subdomain: Subdomain to be added.
1676 */
1677int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1678 struct generic_pm_domain *subdomain)
1679{
1680 int ret;
1681
1682 mutex_lock(&gpd_list_lock);
1683 ret = genpd_add_subdomain(genpd, subdomain);
1684 mutex_unlock(&gpd_list_lock);
1685
1686 return ret;
1687}
1688EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1689
1690/**
1691 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1692 * @genpd: Master PM domain to remove the subdomain from.
1693 * @subdomain: Subdomain to be removed.
1694 */
1695int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1696 struct generic_pm_domain *subdomain)
1697{
1698 struct gpd_link *l, *link;
1699 int ret = -EINVAL;
1700
1701 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1702 return -EINVAL;
1703
1704 genpd_lock(subdomain);
1705 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1706
1707 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1708 pr_warn("%s: unable to remove subdomain %s\n",
1709 genpd->name, subdomain->name);
1710 ret = -EBUSY;
1711 goto out;
1712 }
1713
1714 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
1715 if (link->slave != subdomain)
1716 continue;
1717
1718 list_del(&link->master_node);
1719 list_del(&link->slave_node);
1720 kfree(link);
1721 if (genpd_status_on(subdomain))
1722 genpd_sd_counter_dec(genpd);
1723
1724 ret = 0;
1725 break;
1726 }
1727
1728out:
1729 genpd_unlock(genpd);
1730 genpd_unlock(subdomain);
1731
1732 return ret;
1733}
1734EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1735
1736static void genpd_free_default_power_state(struct genpd_power_state *states,
1737 unsigned int state_count)
1738{
1739 kfree(states);
1740}
1741
1742static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1743{
1744 struct genpd_power_state *state;
1745
1746 state = kzalloc(sizeof(*state), GFP_KERNEL);
1747 if (!state)
1748 return -ENOMEM;
1749
1750 genpd->states = state;
1751 genpd->state_count = 1;
1752 genpd->free_states = genpd_free_default_power_state;
1753
1754 return 0;
1755}
1756
1757static void genpd_lock_init(struct generic_pm_domain *genpd)
1758{
1759 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1760 spin_lock_init(&genpd->slock);
1761 genpd->lock_ops = &genpd_spin_ops;
1762 } else {
1763 mutex_init(&genpd->mlock);
1764 genpd->lock_ops = &genpd_mtx_ops;
1765 }
1766}
1767
1768/**
1769 * pm_genpd_init - Initialize a generic I/O PM domain object.
1770 * @genpd: PM domain object to initialize.
1771 * @gov: PM domain governor to associate with the domain (may be NULL).
1772 * @is_off: Initial value of the domain's power_is_off field.
1773 *
1774 * Returns 0 on successful initialization, else a negative error code.
1775 */
1776int pm_genpd_init(struct generic_pm_domain *genpd,
1777 struct dev_power_governor *gov, bool is_off)
1778{
1779 int ret;
1780
1781 if (IS_ERR_OR_NULL(genpd))
1782 return -EINVAL;
1783
1784 INIT_LIST_HEAD(&genpd->master_links);
1785 INIT_LIST_HEAD(&genpd->slave_links);
1786 INIT_LIST_HEAD(&genpd->dev_list);
1787 genpd_lock_init(genpd);
1788 genpd->gov = gov;
1789 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1790 atomic_set(&genpd->sd_count, 0);
1791 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1792 genpd->device_count = 0;
1793 genpd->max_off_time_ns = -1;
1794 genpd->max_off_time_changed = true;
1795 genpd->provider = NULL;
1796 genpd->has_provider = false;
1797 genpd->accounting_time = ktime_get();
1798 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1799 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1800 genpd->domain.ops.prepare = genpd_prepare;
1801 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1802 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1803 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1804 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1805 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1806 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1807 genpd->domain.ops.complete = genpd_complete;
1808
1809 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1810 genpd->dev_ops.stop = pm_clk_suspend;
1811 genpd->dev_ops.start = pm_clk_resume;
1812 }
1813
1814 /* Always-on domains must be powered on at initialization. */
1815 if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
1816 !genpd_status_on(genpd))
1817 return -EINVAL;
1818
1819 if (genpd_is_cpu_domain(genpd) &&
1820 !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
1821 return -ENOMEM;
1822
1823 /* Use only one "off" state if there were no states declared */
1824 if (genpd->state_count == 0) {
1825 ret = genpd_set_default_power_state(genpd);
1826 if (ret) {
1827 if (genpd_is_cpu_domain(genpd))
1828 free_cpumask_var(genpd->cpus);
1829 return ret;
1830 }
1831 } else if (!gov && genpd->state_count > 1) {
1832 pr_warn("%s: no governor for states\n", genpd->name);
1833 }
1834
1835 device_initialize(&genpd->dev);
1836 dev_set_name(&genpd->dev, "%s", genpd->name);
1837
1838 mutex_lock(&gpd_list_lock);
1839 list_add(&genpd->gpd_list_node, &gpd_list);
1840 mutex_unlock(&gpd_list_lock);
1841
1842 return 0;
1843}
1844EXPORT_SYMBOL_GPL(pm_genpd_init);
1845
1846static int genpd_remove(struct generic_pm_domain *genpd)
1847{
1848 struct gpd_link *l, *link;
1849
1850 if (IS_ERR_OR_NULL(genpd))
1851 return -EINVAL;
1852
1853 genpd_lock(genpd);
1854
1855 if (genpd->has_provider) {
1856 genpd_unlock(genpd);
1857 pr_err("Provider present, unable to remove %s\n", genpd->name);
1858 return -EBUSY;
1859 }
1860
1861 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1862 genpd_unlock(genpd);
1863 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1864 return -EBUSY;
1865 }
1866
1867 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1868 list_del(&link->master_node);
1869 list_del(&link->slave_node);
1870 kfree(link);
1871 }
1872
1873 list_del(&genpd->gpd_list_node);
1874 genpd_unlock(genpd);
1875 cancel_work_sync(&genpd->power_off_work);
1876 if (genpd_is_cpu_domain(genpd))
1877 free_cpumask_var(genpd->cpus);
1878 if (genpd->free_states)
1879 genpd->free_states(genpd->states, genpd->state_count);
1880
1881 pr_debug("%s: removed %s\n", __func__, genpd->name);
1882
1883 return 0;
1884}
1885
1886/**
1887 * pm_genpd_remove - Remove a generic I/O PM domain
1888 * @genpd: Pointer to PM domain that is to be removed.
1889 *
1890 * To remove the PM domain, this function:
1891 * - Removes the PM domain as a subdomain to any parent domains,
1892 * if it was added.
1893 * - Removes the PM domain from the list of registered PM domains.
1894 *
1895 * The PM domain will only be removed, if the associated provider has
1896 * been removed, it is not a parent to any other PM domain and has no
1897 * devices associated with it.
1898 */
1899int pm_genpd_remove(struct generic_pm_domain *genpd)
1900{
1901 int ret;
1902
1903 mutex_lock(&gpd_list_lock);
1904 ret = genpd_remove(genpd);
1905 mutex_unlock(&gpd_list_lock);
1906
1907 return ret;
1908}
1909EXPORT_SYMBOL_GPL(pm_genpd_remove);
1910
1911#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1912
1913/*
1914 * Device Tree based PM domain providers.
1915 *
1916 * The code below implements generic device tree based PM domain providers that
1917 * bind device tree nodes with generic PM domains registered in the system.
1918 *
1919 * Any driver that registers generic PM domains and needs to support binding of
1920 * devices to these domains is supposed to register a PM domain provider, which
1921 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1922 *
1923 * Two simple mapping functions have been provided for convenience:
1924 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1925 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1926 * index.
1927 */
1928
1929/**
1930 * struct of_genpd_provider - PM domain provider registration structure
1931 * @link: Entry in global list of PM domain providers
1932 * @node: Pointer to device tree node of PM domain provider
1933 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1934 * into a PM domain.
1935 * @data: context pointer to be passed into @xlate callback
1936 */
1937struct of_genpd_provider {
1938 struct list_head link;
1939 struct device_node *node;
1940 genpd_xlate_t xlate;
1941 void *data;
1942};
1943
1944/* List of registered PM domain providers. */
1945static LIST_HEAD(of_genpd_providers);
1946/* Mutex to protect the list above. */
1947static DEFINE_MUTEX(of_genpd_mutex);
1948
1949/**
1950 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1951 * @genpdspec: OF phandle args to map into a PM domain
1952 * @data: xlate function private data - pointer to struct generic_pm_domain
1953 *
1954 * This is a generic xlate function that can be used to model PM domains that
1955 * have their own device tree nodes. The private data of xlate function needs
1956 * to be a valid pointer to struct generic_pm_domain.
1957 */
1958static struct generic_pm_domain *genpd_xlate_simple(
1959 struct of_phandle_args *genpdspec,
1960 void *data)
1961{
1962 return data;
1963}
1964
1965/**
1966 * genpd_xlate_onecell() - Xlate function using a single index.
1967 * @genpdspec: OF phandle args to map into a PM domain
1968 * @data: xlate function private data - pointer to struct genpd_onecell_data
1969 *
1970 * This is a generic xlate function that can be used to model simple PM domain
1971 * controllers that have one device tree node and provide multiple PM domains.
1972 * A single cell is used as an index into an array of PM domains specified in
1973 * the genpd_onecell_data struct when registering the provider.
1974 */
1975static struct generic_pm_domain *genpd_xlate_onecell(
1976 struct of_phandle_args *genpdspec,
1977 void *data)
1978{
1979 struct genpd_onecell_data *genpd_data = data;
1980 unsigned int idx = genpdspec->args[0];
1981
1982 if (genpdspec->args_count != 1)
1983 return ERR_PTR(-EINVAL);
1984
1985 if (idx >= genpd_data->num_domains) {
1986 pr_err("%s: invalid domain index %u\n", __func__, idx);
1987 return ERR_PTR(-EINVAL);
1988 }
1989
1990 if (!genpd_data->domains[idx])
1991 return ERR_PTR(-ENOENT);
1992
1993 return genpd_data->domains[idx];
1994}
1995
1996/**
1997 * genpd_add_provider() - Register a PM domain provider for a node
1998 * @np: Device node pointer associated with the PM domain provider.
1999 * @xlate: Callback for decoding PM domain from phandle arguments.
2000 * @data: Context pointer for @xlate callback.
2001 */
2002static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2003 void *data)
2004{
2005 struct of_genpd_provider *cp;
2006
2007 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2008 if (!cp)
2009 return -ENOMEM;
2010
2011 cp->node = of_node_get(np);
2012 cp->data = data;
2013 cp->xlate = xlate;
2014
2015 mutex_lock(&of_genpd_mutex);
2016 list_add(&cp->link, &of_genpd_providers);
2017 mutex_unlock(&of_genpd_mutex);
2018 pr_debug("Added domain provider from %pOF\n", np);
2019
2020 return 0;
2021}
2022
2023/**
2024 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2025 * @np: Device node pointer associated with the PM domain provider.
2026 * @genpd: Pointer to PM domain associated with the PM domain provider.
2027 */
2028int of_genpd_add_provider_simple(struct device_node *np,
2029 struct generic_pm_domain *genpd)
2030{
2031 int ret = -EINVAL;
2032
2033 if (!np || !genpd)
2034 return -EINVAL;
2035
2036 mutex_lock(&gpd_list_lock);
2037
2038 if (!genpd_present(genpd))
2039 goto unlock;
2040
2041 genpd->dev.of_node = np;
2042
2043 /* Parse genpd OPP table */
2044 if (genpd->set_performance_state) {
2045 ret = dev_pm_opp_of_add_table(&genpd->dev);
2046 if (ret) {
2047 dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
2048 ret);
2049 goto unlock;
2050 }
2051
2052 /*
2053 * Save table for faster processing while setting performance
2054 * state.
2055 */
2056 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2057 WARN_ON(!genpd->opp_table);
2058 }
2059
2060 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
2061 if (ret) {
2062 if (genpd->set_performance_state) {
2063 dev_pm_opp_put_opp_table(genpd->opp_table);
2064 dev_pm_opp_of_remove_table(&genpd->dev);
2065 }
2066
2067 goto unlock;
2068 }
2069
2070 genpd->provider = &np->fwnode;
2071 genpd->has_provider = true;
2072
2073unlock:
2074 mutex_unlock(&gpd_list_lock);
2075
2076 return ret;
2077}
2078EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2079
2080/**
2081 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2082 * @np: Device node pointer associated with the PM domain provider.
2083 * @data: Pointer to the data associated with the PM domain provider.
2084 */
2085int of_genpd_add_provider_onecell(struct device_node *np,
2086 struct genpd_onecell_data *data)
2087{
2088 struct generic_pm_domain *genpd;
2089 unsigned int i;
2090 int ret = -EINVAL;
2091
2092 if (!np || !data)
2093 return -EINVAL;
2094
2095 mutex_lock(&gpd_list_lock);
2096
2097 if (!data->xlate)
2098 data->xlate = genpd_xlate_onecell;
2099
2100 for (i = 0; i < data->num_domains; i++) {
2101 genpd = data->domains[i];
2102
2103 if (!genpd)
2104 continue;
2105 if (!genpd_present(genpd))
2106 goto error;
2107
2108 genpd->dev.of_node = np;
2109
2110 /* Parse genpd OPP table */
2111 if (genpd->set_performance_state) {
2112 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2113 if (ret) {
2114 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
2115 i, ret);
2116 goto error;
2117 }
2118
2119 /*
2120 * Save table for faster processing while setting
2121 * performance state.
2122 */
2123 genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i);
2124 WARN_ON(!genpd->opp_table);
2125 }
2126
2127 genpd->provider = &np->fwnode;
2128 genpd->has_provider = true;
2129 }
2130
2131 ret = genpd_add_provider(np, data->xlate, data);
2132 if (ret < 0)
2133 goto error;
2134
2135 mutex_unlock(&gpd_list_lock);
2136
2137 return 0;
2138
2139error:
2140 while (i--) {
2141 genpd = data->domains[i];
2142
2143 if (!genpd)
2144 continue;
2145
2146 genpd->provider = NULL;
2147 genpd->has_provider = false;
2148
2149 if (genpd->set_performance_state) {
2150 dev_pm_opp_put_opp_table(genpd->opp_table);
2151 dev_pm_opp_of_remove_table(&genpd->dev);
2152 }
2153 }
2154
2155 mutex_unlock(&gpd_list_lock);
2156
2157 return ret;
2158}
2159EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2160
2161/**
2162 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2163 * @np: Device node pointer associated with the PM domain provider
2164 */
2165void of_genpd_del_provider(struct device_node *np)
2166{
2167 struct of_genpd_provider *cp, *tmp;
2168 struct generic_pm_domain *gpd;
2169
2170 mutex_lock(&gpd_list_lock);
2171 mutex_lock(&of_genpd_mutex);
2172 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2173 if (cp->node == np) {
2174 /*
2175 * For each PM domain associated with the
2176 * provider, set the 'has_provider' to false
2177 * so that the PM domain can be safely removed.
2178 */
2179 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2180 if (gpd->provider == &np->fwnode) {
2181 gpd->has_provider = false;
2182
2183 if (!gpd->set_performance_state)
2184 continue;
2185
2186 dev_pm_opp_put_opp_table(gpd->opp_table);
2187 dev_pm_opp_of_remove_table(&gpd->dev);
2188 }
2189 }
2190
2191 list_del(&cp->link);
2192 of_node_put(cp->node);
2193 kfree(cp);
2194 break;
2195 }
2196 }
2197 mutex_unlock(&of_genpd_mutex);
2198 mutex_unlock(&gpd_list_lock);
2199}
2200EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2201
2202/**
2203 * genpd_get_from_provider() - Look-up PM domain
2204 * @genpdspec: OF phandle args to use for look-up
2205 *
2206 * Looks for a PM domain provider under the node specified by @genpdspec and if
2207 * found, uses xlate function of the provider to map phandle args to a PM
2208 * domain.
2209 *
2210 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2211 * on failure.
2212 */
2213static struct generic_pm_domain *genpd_get_from_provider(
2214 struct of_phandle_args *genpdspec)
2215{
2216 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2217 struct of_genpd_provider *provider;
2218
2219 if (!genpdspec)
2220 return ERR_PTR(-EINVAL);
2221
2222 mutex_lock(&of_genpd_mutex);
2223
2224 /* Check if we have such a provider in our array */
2225 list_for_each_entry(provider, &of_genpd_providers, link) {
2226 if (provider->node == genpdspec->np)
2227 genpd = provider->xlate(genpdspec, provider->data);
2228 if (!IS_ERR(genpd))
2229 break;
2230 }
2231
2232 mutex_unlock(&of_genpd_mutex);
2233
2234 return genpd;
2235}
2236
2237/**
2238 * of_genpd_add_device() - Add a device to an I/O PM domain
2239 * @genpdspec: OF phandle args to use for look-up PM domain
2240 * @dev: Device to be added.
2241 *
2242 * Looks-up an I/O PM domain based upon phandle args provided and adds
2243 * the device to the PM domain. Returns a negative error code on failure.
2244 */
2245int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2246{
2247 struct generic_pm_domain *genpd;
2248 int ret;
2249
2250 mutex_lock(&gpd_list_lock);
2251
2252 genpd = genpd_get_from_provider(genpdspec);
2253 if (IS_ERR(genpd)) {
2254 ret = PTR_ERR(genpd);
2255 goto out;
2256 }
2257
2258 ret = genpd_add_device(genpd, dev, dev);
2259
2260out:
2261 mutex_unlock(&gpd_list_lock);
2262
2263 return ret;
2264}
2265EXPORT_SYMBOL_GPL(of_genpd_add_device);
2266
2267/**
2268 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2269 * @parent_spec: OF phandle args to use for parent PM domain look-up
2270 * @subdomain_spec: OF phandle args to use for subdomain look-up
2271 *
2272 * Looks-up a parent PM domain and subdomain based upon phandle args
2273 * provided and adds the subdomain to the parent PM domain. Returns a
2274 * negative error code on failure.
2275 */
2276int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2277 struct of_phandle_args *subdomain_spec)
2278{
2279 struct generic_pm_domain *parent, *subdomain;
2280 int ret;
2281
2282 mutex_lock(&gpd_list_lock);
2283
2284 parent = genpd_get_from_provider(parent_spec);
2285 if (IS_ERR(parent)) {
2286 ret = PTR_ERR(parent);
2287 goto out;
2288 }
2289
2290 subdomain = genpd_get_from_provider(subdomain_spec);
2291 if (IS_ERR(subdomain)) {
2292 ret = PTR_ERR(subdomain);
2293 goto out;
2294 }
2295
2296 ret = genpd_add_subdomain(parent, subdomain);
2297
2298out:
2299 mutex_unlock(&gpd_list_lock);
2300
2301 return ret;
2302}
2303EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2304
2305/**
2306 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2307 * @provider: Pointer to device structure associated with provider
2308 *
2309 * Find the last PM domain that was added by a particular provider and
2310 * remove this PM domain from the list of PM domains. The provider is
2311 * identified by the 'provider' device structure that is passed. The PM
2312 * domain will only be removed, if the provider associated with domain
2313 * has been removed.
2314 *
2315 * Returns a valid pointer to struct generic_pm_domain on success or
2316 * ERR_PTR() on failure.
2317 */
2318struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2319{
2320 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2321 int ret;
2322
2323 if (IS_ERR_OR_NULL(np))
2324 return ERR_PTR(-EINVAL);
2325
2326 mutex_lock(&gpd_list_lock);
2327 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2328 if (gpd->provider == &np->fwnode) {
2329 ret = genpd_remove(gpd);
2330 genpd = ret ? ERR_PTR(ret) : gpd;
2331 break;
2332 }
2333 }
2334 mutex_unlock(&gpd_list_lock);
2335
2336 return genpd;
2337}
2338EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2339
2340static void genpd_release_dev(struct device *dev)
2341{
2342 of_node_put(dev->of_node);
2343 kfree(dev);
2344}
2345
2346static struct bus_type genpd_bus_type = {
2347 .name = "genpd",
2348};
2349
2350/**
2351 * genpd_dev_pm_detach - Detach a device from its PM domain.
2352 * @dev: Device to detach.
2353 * @power_off: Currently not used
2354 *
2355 * Try to locate a corresponding generic PM domain, which the device was
2356 * attached to previously. If such is found, the device is detached from it.
2357 */
2358static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2359{
2360 struct generic_pm_domain *pd;
2361 unsigned int i;
2362 int ret = 0;
2363
2364 pd = dev_to_genpd(dev);
2365 if (IS_ERR(pd))
2366 return;
2367
2368 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2369
2370 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2371 ret = genpd_remove_device(pd, dev);
2372 if (ret != -EAGAIN)
2373 break;
2374
2375 mdelay(i);
2376 cond_resched();
2377 }
2378
2379 if (ret < 0) {
2380 dev_err(dev, "failed to remove from PM domain %s: %d",
2381 pd->name, ret);
2382 return;
2383 }
2384
2385 /* Check if PM domain can be powered off after removing this device. */
2386 genpd_queue_power_off_work(pd);
2387
2388 /* Unregister the device if it was created by genpd. */
2389 if (dev->bus == &genpd_bus_type)
2390 device_unregister(dev);
2391}
2392
2393static void genpd_dev_pm_sync(struct device *dev)
2394{
2395 struct generic_pm_domain *pd;
2396
2397 pd = dev_to_genpd(dev);
2398 if (IS_ERR(pd))
2399 return;
2400
2401 genpd_queue_power_off_work(pd);
2402}
2403
2404static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
2405 unsigned int index, bool power_on)
2406{
2407 struct of_phandle_args pd_args;
2408 struct generic_pm_domain *pd;
2409 int ret;
2410
2411 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2412 "#power-domain-cells", index, &pd_args);
2413 if (ret < 0)
2414 return ret;
2415
2416 mutex_lock(&gpd_list_lock);
2417 pd = genpd_get_from_provider(&pd_args);
2418 of_node_put(pd_args.np);
2419 if (IS_ERR(pd)) {
2420 mutex_unlock(&gpd_list_lock);
2421 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2422 __func__, PTR_ERR(pd));
2423 return driver_deferred_probe_check_state(base_dev);
2424 }
2425
2426 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2427
2428 ret = genpd_add_device(pd, dev, base_dev);
2429 mutex_unlock(&gpd_list_lock);
2430
2431 if (ret < 0) {
2432 if (ret != -EPROBE_DEFER)
2433 dev_err(dev, "failed to add to PM domain %s: %d",
2434 pd->name, ret);
2435 return ret;
2436 }
2437
2438 dev->pm_domain->detach = genpd_dev_pm_detach;
2439 dev->pm_domain->sync = genpd_dev_pm_sync;
2440
2441 if (power_on) {
2442 genpd_lock(pd);
2443 ret = genpd_power_on(pd, 0);
2444 genpd_unlock(pd);
2445 }
2446
2447 if (ret)
2448 genpd_remove_device(pd, dev);
2449
2450 return ret ? -EPROBE_DEFER : 1;
2451}
2452
2453/**
2454 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2455 * @dev: Device to attach.
2456 *
2457 * Parse device's OF node to find a PM domain specifier. If such is found,
2458 * attaches the device to retrieved pm_domain ops.
2459 *
2460 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2461 * PM domain or when multiple power-domains exists for it, else a negative error
2462 * code. Note that if a power-domain exists for the device, but it cannot be
2463 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2464 * not probed and to re-try again later.
2465 */
2466int genpd_dev_pm_attach(struct device *dev)
2467{
2468 if (!dev->of_node)
2469 return 0;
2470
2471 /*
2472 * Devices with multiple PM domains must be attached separately, as we
2473 * can only attach one PM domain per device.
2474 */
2475 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2476 "#power-domain-cells") != 1)
2477 return 0;
2478
2479 return __genpd_dev_pm_attach(dev, dev, 0, true);
2480}
2481EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2482
2483/**
2484 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2485 * @dev: The device used to lookup the PM domain.
2486 * @index: The index of the PM domain.
2487 *
2488 * Parse device's OF node to find a PM domain specifier at the provided @index.
2489 * If such is found, creates a virtual device and attaches it to the retrieved
2490 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2491 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2492 *
2493 * Returns the created virtual device if successfully attached PM domain, NULL
2494 * when the device don't need a PM domain, else an ERR_PTR() in case of
2495 * failures. If a power-domain exists for the device, but cannot be found or
2496 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2497 * is not probed and to re-try again later.
2498 */
2499struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2500 unsigned int index)
2501{
2502 struct device *virt_dev;
2503 int num_domains;
2504 int ret;
2505
2506 if (!dev->of_node)
2507 return NULL;
2508
2509 /* Verify that the index is within a valid range. */
2510 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2511 "#power-domain-cells");
2512 if (index >= num_domains)
2513 return NULL;
2514
2515 /* Allocate and register device on the genpd bus. */
2516 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2517 if (!virt_dev)
2518 return ERR_PTR(-ENOMEM);
2519
2520 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2521 virt_dev->bus = &genpd_bus_type;
2522 virt_dev->release = genpd_release_dev;
2523 virt_dev->of_node = of_node_get(dev->of_node);
2524
2525 ret = device_register(virt_dev);
2526 if (ret) {
2527 put_device(virt_dev);
2528 return ERR_PTR(ret);
2529 }
2530
2531 /* Try to attach the device to the PM domain at the specified index. */
2532 ret = __genpd_dev_pm_attach(virt_dev, dev, index, false);
2533 if (ret < 1) {
2534 device_unregister(virt_dev);
2535 return ret ? ERR_PTR(ret) : NULL;
2536 }
2537
2538 pm_runtime_enable(virt_dev);
2539 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2540
2541 return virt_dev;
2542}
2543EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2544
2545/**
2546 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2547 * @dev: The device used to lookup the PM domain.
2548 * @name: The name of the PM domain.
2549 *
2550 * Parse device's OF node to find a PM domain specifier using the
2551 * power-domain-names DT property. For further description see
2552 * genpd_dev_pm_attach_by_id().
2553 */
2554struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
2555{
2556 int index;
2557
2558 if (!dev->of_node)
2559 return NULL;
2560
2561 index = of_property_match_string(dev->of_node, "power-domain-names",
2562 name);
2563 if (index < 0)
2564 return NULL;
2565
2566 return genpd_dev_pm_attach_by_id(dev, index);
2567}
2568
2569static const struct of_device_id idle_state_match[] = {
2570 { .compatible = "domain-idle-state", },
2571 { }
2572};
2573
2574static int genpd_parse_state(struct genpd_power_state *genpd_state,
2575 struct device_node *state_node)
2576{
2577 int err;
2578 u32 residency;
2579 u32 entry_latency, exit_latency;
2580
2581 err = of_property_read_u32(state_node, "entry-latency-us",
2582 &entry_latency);
2583 if (err) {
2584 pr_debug(" * %pOF missing entry-latency-us property\n",
2585 state_node);
2586 return -EINVAL;
2587 }
2588
2589 err = of_property_read_u32(state_node, "exit-latency-us",
2590 &exit_latency);
2591 if (err) {
2592 pr_debug(" * %pOF missing exit-latency-us property\n",
2593 state_node);
2594 return -EINVAL;
2595 }
2596
2597 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2598 if (!err)
2599 genpd_state->residency_ns = 1000 * residency;
2600
2601 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2602 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2603 genpd_state->fwnode = &state_node->fwnode;
2604
2605 return 0;
2606}
2607
2608static int genpd_iterate_idle_states(struct device_node *dn,
2609 struct genpd_power_state *states)
2610{
2611 int ret;
2612 struct of_phandle_iterator it;
2613 struct device_node *np;
2614 int i = 0;
2615
2616 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2617 if (ret <= 0)
2618 return ret;
2619
2620 /* Loop over the phandles until all the requested entry is found */
2621 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2622 np = it.node;
2623 if (!of_match_node(idle_state_match, np))
2624 continue;
2625 if (states) {
2626 ret = genpd_parse_state(&states[i], np);
2627 if (ret) {
2628 pr_err("Parsing idle state node %pOF failed with err %d\n",
2629 np, ret);
2630 of_node_put(np);
2631 return ret;
2632 }
2633 }
2634 i++;
2635 }
2636
2637 return i;
2638}
2639
2640/**
2641 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2642 *
2643 * @dn: The genpd device node
2644 * @states: The pointer to which the state array will be saved.
2645 * @n: The count of elements in the array returned from this function.
2646 *
2647 * Returns the device states parsed from the OF node. The memory for the states
2648 * is allocated by this function and is the responsibility of the caller to
2649 * free the memory after use. If any or zero compatible domain idle states is
2650 * found it returns 0 and in case of errors, a negative error code is returned.
2651 */
2652int of_genpd_parse_idle_states(struct device_node *dn,
2653 struct genpd_power_state **states, int *n)
2654{
2655 struct genpd_power_state *st;
2656 int ret;
2657
2658 ret = genpd_iterate_idle_states(dn, NULL);
2659 if (ret < 0)
2660 return ret;
2661
2662 if (!ret) {
2663 *states = NULL;
2664 *n = 0;
2665 return 0;
2666 }
2667
2668 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2669 if (!st)
2670 return -ENOMEM;
2671
2672 ret = genpd_iterate_idle_states(dn, st);
2673 if (ret <= 0) {
2674 kfree(st);
2675 return ret < 0 ? ret : -EINVAL;
2676 }
2677
2678 *states = st;
2679 *n = ret;
2680
2681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2684
2685/**
2686 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2687 *
2688 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2689 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2690 * state.
2691 *
2692 * Returns performance state encoded in the OPP of the genpd. This calls
2693 * platform specific genpd->opp_to_performance_state() callback to translate
2694 * power domain OPP to performance state.
2695 *
2696 * Returns performance state on success and 0 on failure.
2697 */
2698unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2699 struct dev_pm_opp *opp)
2700{
2701 struct generic_pm_domain *genpd = NULL;
2702 int state;
2703
2704 genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2705
2706 if (unlikely(!genpd->opp_to_performance_state))
2707 return 0;
2708
2709 genpd_lock(genpd);
2710 state = genpd->opp_to_performance_state(genpd, opp);
2711 genpd_unlock(genpd);
2712
2713 return state;
2714}
2715EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2716
2717static int __init genpd_bus_init(void)
2718{
2719 return bus_register(&genpd_bus_type);
2720}
2721core_initcall(genpd_bus_init);
2722
2723#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2724
2725
2726/*** debugfs support ***/
2727
2728#ifdef CONFIG_DEBUG_FS
2729#include <linux/pm.h>
2730#include <linux/device.h>
2731#include <linux/debugfs.h>
2732#include <linux/seq_file.h>
2733#include <linux/init.h>
2734#include <linux/kobject.h>
2735static struct dentry *genpd_debugfs_dir;
2736
2737/*
2738 * TODO: This function is a slightly modified version of rtpm_status_show
2739 * from sysfs.c, so generalize it.
2740 */
2741static void rtpm_status_str(struct seq_file *s, struct device *dev)
2742{
2743 static const char * const status_lookup[] = {
2744 [RPM_ACTIVE] = "active",
2745 [RPM_RESUMING] = "resuming",
2746 [RPM_SUSPENDED] = "suspended",
2747 [RPM_SUSPENDING] = "suspending"
2748 };
2749 const char *p = "";
2750
2751 if (dev->power.runtime_error)
2752 p = "error";
2753 else if (dev->power.disable_depth)
2754 p = "unsupported";
2755 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2756 p = status_lookup[dev->power.runtime_status];
2757 else
2758 WARN_ON(1);
2759
2760 seq_puts(s, p);
2761}
2762
2763static int genpd_summary_one(struct seq_file *s,
2764 struct generic_pm_domain *genpd)
2765{
2766 static const char * const status_lookup[] = {
2767 [GPD_STATE_ACTIVE] = "on",
2768 [GPD_STATE_POWER_OFF] = "off"
2769 };
2770 struct pm_domain_data *pm_data;
2771 const char *kobj_path;
2772 struct gpd_link *link;
2773 char state[16];
2774 int ret;
2775
2776 ret = genpd_lock_interruptible(genpd);
2777 if (ret)
2778 return -ERESTARTSYS;
2779
2780 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2781 goto exit;
2782 if (!genpd_status_on(genpd))
2783 snprintf(state, sizeof(state), "%s-%u",
2784 status_lookup[genpd->status], genpd->state_idx);
2785 else
2786 snprintf(state, sizeof(state), "%s",
2787 status_lookup[genpd->status]);
2788 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2789
2790 /*
2791 * Modifications on the list require holding locks on both
2792 * master and slave, so we are safe.
2793 * Also genpd->name is immutable.
2794 */
2795 list_for_each_entry(link, &genpd->master_links, master_node) {
2796 seq_printf(s, "%s", link->slave->name);
2797 if (!list_is_last(&link->master_node, &genpd->master_links))
2798 seq_puts(s, ", ");
2799 }
2800
2801 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2802 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2803 genpd_is_irq_safe(genpd) ?
2804 GFP_ATOMIC : GFP_KERNEL);
2805 if (kobj_path == NULL)
2806 continue;
2807
2808 seq_printf(s, "\n %-50s ", kobj_path);
2809 rtpm_status_str(s, pm_data->dev);
2810 kfree(kobj_path);
2811 }
2812
2813 seq_puts(s, "\n");
2814exit:
2815 genpd_unlock(genpd);
2816
2817 return 0;
2818}
2819
2820static int summary_show(struct seq_file *s, void *data)
2821{
2822 struct generic_pm_domain *genpd;
2823 int ret = 0;
2824
2825 seq_puts(s, "domain status slaves\n");
2826 seq_puts(s, " /device runtime status\n");
2827 seq_puts(s, "----------------------------------------------------------------------\n");
2828
2829 ret = mutex_lock_interruptible(&gpd_list_lock);
2830 if (ret)
2831 return -ERESTARTSYS;
2832
2833 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2834 ret = genpd_summary_one(s, genpd);
2835 if (ret)
2836 break;
2837 }
2838 mutex_unlock(&gpd_list_lock);
2839
2840 return ret;
2841}
2842
2843static int status_show(struct seq_file *s, void *data)
2844{
2845 static const char * const status_lookup[] = {
2846 [GPD_STATE_ACTIVE] = "on",
2847 [GPD_STATE_POWER_OFF] = "off"
2848 };
2849
2850 struct generic_pm_domain *genpd = s->private;
2851 int ret = 0;
2852
2853 ret = genpd_lock_interruptible(genpd);
2854 if (ret)
2855 return -ERESTARTSYS;
2856
2857 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2858 goto exit;
2859
2860 if (genpd->status == GPD_STATE_POWER_OFF)
2861 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2862 genpd->state_idx);
2863 else
2864 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2865exit:
2866 genpd_unlock(genpd);
2867 return ret;
2868}
2869
2870static int sub_domains_show(struct seq_file *s, void *data)
2871{
2872 struct generic_pm_domain *genpd = s->private;
2873 struct gpd_link *link;
2874 int ret = 0;
2875
2876 ret = genpd_lock_interruptible(genpd);
2877 if (ret)
2878 return -ERESTARTSYS;
2879
2880 list_for_each_entry(link, &genpd->master_links, master_node)
2881 seq_printf(s, "%s\n", link->slave->name);
2882
2883 genpd_unlock(genpd);
2884 return ret;
2885}
2886
2887static int idle_states_show(struct seq_file *s, void *data)
2888{
2889 struct generic_pm_domain *genpd = s->private;
2890 unsigned int i;
2891 int ret = 0;
2892
2893 ret = genpd_lock_interruptible(genpd);
2894 if (ret)
2895 return -ERESTARTSYS;
2896
2897 seq_puts(s, "State Time Spent(ms)\n");
2898
2899 for (i = 0; i < genpd->state_count; i++) {
2900 ktime_t delta = 0;
2901 s64 msecs;
2902
2903 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2904 (genpd->state_idx == i))
2905 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2906
2907 msecs = ktime_to_ms(
2908 ktime_add(genpd->states[i].idle_time, delta));
2909 seq_printf(s, "S%-13i %lld\n", i, msecs);
2910 }
2911
2912 genpd_unlock(genpd);
2913 return ret;
2914}
2915
2916static int active_time_show(struct seq_file *s, void *data)
2917{
2918 struct generic_pm_domain *genpd = s->private;
2919 ktime_t delta = 0;
2920 int ret = 0;
2921
2922 ret = genpd_lock_interruptible(genpd);
2923 if (ret)
2924 return -ERESTARTSYS;
2925
2926 if (genpd->status == GPD_STATE_ACTIVE)
2927 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2928
2929 seq_printf(s, "%lld ms\n", ktime_to_ms(
2930 ktime_add(genpd->on_time, delta)));
2931
2932 genpd_unlock(genpd);
2933 return ret;
2934}
2935
2936static int total_idle_time_show(struct seq_file *s, void *data)
2937{
2938 struct generic_pm_domain *genpd = s->private;
2939 ktime_t delta = 0, total = 0;
2940 unsigned int i;
2941 int ret = 0;
2942
2943 ret = genpd_lock_interruptible(genpd);
2944 if (ret)
2945 return -ERESTARTSYS;
2946
2947 for (i = 0; i < genpd->state_count; i++) {
2948
2949 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2950 (genpd->state_idx == i))
2951 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2952
2953 total = ktime_add(total, genpd->states[i].idle_time);
2954 }
2955 total = ktime_add(total, delta);
2956
2957 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2958
2959 genpd_unlock(genpd);
2960 return ret;
2961}
2962
2963
2964static int devices_show(struct seq_file *s, void *data)
2965{
2966 struct generic_pm_domain *genpd = s->private;
2967 struct pm_domain_data *pm_data;
2968 const char *kobj_path;
2969 int ret = 0;
2970
2971 ret = genpd_lock_interruptible(genpd);
2972 if (ret)
2973 return -ERESTARTSYS;
2974
2975 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2976 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2977 genpd_is_irq_safe(genpd) ?
2978 GFP_ATOMIC : GFP_KERNEL);
2979 if (kobj_path == NULL)
2980 continue;
2981
2982 seq_printf(s, "%s\n", kobj_path);
2983 kfree(kobj_path);
2984 }
2985
2986 genpd_unlock(genpd);
2987 return ret;
2988}
2989
2990static int perf_state_show(struct seq_file *s, void *data)
2991{
2992 struct generic_pm_domain *genpd = s->private;
2993
2994 if (genpd_lock_interruptible(genpd))
2995 return -ERESTARTSYS;
2996
2997 seq_printf(s, "%u\n", genpd->performance_state);
2998
2999 genpd_unlock(genpd);
3000 return 0;
3001}
3002
3003DEFINE_SHOW_ATTRIBUTE(summary);
3004DEFINE_SHOW_ATTRIBUTE(status);
3005DEFINE_SHOW_ATTRIBUTE(sub_domains);
3006DEFINE_SHOW_ATTRIBUTE(idle_states);
3007DEFINE_SHOW_ATTRIBUTE(active_time);
3008DEFINE_SHOW_ATTRIBUTE(total_idle_time);
3009DEFINE_SHOW_ATTRIBUTE(devices);
3010DEFINE_SHOW_ATTRIBUTE(perf_state);
3011
3012static int __init genpd_debug_init(void)
3013{
3014 struct dentry *d;
3015 struct generic_pm_domain *genpd;
3016
3017 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
3018
3019 debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
3020 NULL, &summary_fops);
3021
3022 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
3023 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
3024
3025 debugfs_create_file("current_state", 0444,
3026 d, genpd, &status_fops);
3027 debugfs_create_file("sub_domains", 0444,
3028 d, genpd, &sub_domains_fops);
3029 debugfs_create_file("idle_states", 0444,
3030 d, genpd, &idle_states_fops);
3031 debugfs_create_file("active_time", 0444,
3032 d, genpd, &active_time_fops);
3033 debugfs_create_file("total_idle_time", 0444,
3034 d, genpd, &total_idle_time_fops);
3035 debugfs_create_file("devices", 0444,
3036 d, genpd, &devices_fops);
3037 if (genpd->set_performance_state)
3038 debugfs_create_file("perf_state", 0444,
3039 d, genpd, &perf_state_fops);
3040 }
3041
3042 return 0;
3043}
3044late_initcall(genpd_debug_init);
3045
3046static void __exit genpd_debug_exit(void)
3047{
3048 debugfs_remove_recursive(genpd_debugfs_dir);
3049}
3050__exitcall(genpd_debug_exit);
3051#endif /* CONFIG_DEBUG_FS */
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/delay.h>
10#include <linux/kernel.h>
11#include <linux/io.h>
12#include <linux/platform_device.h>
13#include <linux/pm_runtime.h>
14#include <linux/pm_domain.h>
15#include <linux/pm_qos.h>
16#include <linux/pm_clock.h>
17#include <linux/slab.h>
18#include <linux/err.h>
19#include <linux/sched.h>
20#include <linux/suspend.h>
21#include <linux/export.h>
22
23#include "power.h"
24
25#define GENPD_RETRY_MAX_MS 250 /* Approximate */
26
27#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
28({ \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
31 \
32 __routine = genpd->dev_ops.callback; \
33 if (__routine) { \
34 __ret = __routine(dev); \
35 } \
36 __ret; \
37})
38
39static LIST_HEAD(gpd_list);
40static DEFINE_MUTEX(gpd_list_lock);
41
42struct genpd_lock_ops {
43 void (*lock)(struct generic_pm_domain *genpd);
44 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
45 int (*lock_interruptible)(struct generic_pm_domain *genpd);
46 void (*unlock)(struct generic_pm_domain *genpd);
47};
48
49static void genpd_lock_mtx(struct generic_pm_domain *genpd)
50{
51 mutex_lock(&genpd->mlock);
52}
53
54static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
55 int depth)
56{
57 mutex_lock_nested(&genpd->mlock, depth);
58}
59
60static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
61{
62 return mutex_lock_interruptible(&genpd->mlock);
63}
64
65static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
66{
67 return mutex_unlock(&genpd->mlock);
68}
69
70static const struct genpd_lock_ops genpd_mtx_ops = {
71 .lock = genpd_lock_mtx,
72 .lock_nested = genpd_lock_nested_mtx,
73 .lock_interruptible = genpd_lock_interruptible_mtx,
74 .unlock = genpd_unlock_mtx,
75};
76
77static void genpd_lock_spin(struct generic_pm_domain *genpd)
78 __acquires(&genpd->slock)
79{
80 unsigned long flags;
81
82 spin_lock_irqsave(&genpd->slock, flags);
83 genpd->lock_flags = flags;
84}
85
86static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
87 int depth)
88 __acquires(&genpd->slock)
89{
90 unsigned long flags;
91
92 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
93 genpd->lock_flags = flags;
94}
95
96static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
97 __acquires(&genpd->slock)
98{
99 unsigned long flags;
100
101 spin_lock_irqsave(&genpd->slock, flags);
102 genpd->lock_flags = flags;
103 return 0;
104}
105
106static void genpd_unlock_spin(struct generic_pm_domain *genpd)
107 __releases(&genpd->slock)
108{
109 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
110}
111
112static const struct genpd_lock_ops genpd_spin_ops = {
113 .lock = genpd_lock_spin,
114 .lock_nested = genpd_lock_nested_spin,
115 .lock_interruptible = genpd_lock_interruptible_spin,
116 .unlock = genpd_unlock_spin,
117};
118
119#define genpd_lock(p) p->lock_ops->lock(p)
120#define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
121#define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
122#define genpd_unlock(p) p->lock_ops->unlock(p)
123
124#define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
125
126static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
127 struct generic_pm_domain *genpd)
128{
129 bool ret;
130
131 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
132
133 /* Warn once for each IRQ safe dev in no sleep domain */
134 if (ret)
135 dev_warn_once(dev, "PM domain %s will not be powered off\n",
136 genpd->name);
137
138 return ret;
139}
140
141/*
142 * Get the generic PM domain for a particular struct device.
143 * This validates the struct device pointer, the PM domain pointer,
144 * and checks that the PM domain pointer is a real generic PM domain.
145 * Any failure results in NULL being returned.
146 */
147static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
148{
149 struct generic_pm_domain *genpd = NULL, *gpd;
150
151 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
152 return NULL;
153
154 mutex_lock(&gpd_list_lock);
155 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
156 if (&gpd->domain == dev->pm_domain) {
157 genpd = gpd;
158 break;
159 }
160 }
161 mutex_unlock(&gpd_list_lock);
162
163 return genpd;
164}
165
166/*
167 * This should only be used where we are certain that the pm_domain
168 * attached to the device is a genpd domain.
169 */
170static struct generic_pm_domain *dev_to_genpd(struct device *dev)
171{
172 if (IS_ERR_OR_NULL(dev->pm_domain))
173 return ERR_PTR(-EINVAL);
174
175 return pd_to_genpd(dev->pm_domain);
176}
177
178static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
179{
180 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
181}
182
183static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
184{
185 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
186}
187
188static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
189{
190 bool ret = false;
191
192 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
193 ret = !!atomic_dec_and_test(&genpd->sd_count);
194
195 return ret;
196}
197
198static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
199{
200 atomic_inc(&genpd->sd_count);
201 smp_mb__after_atomic();
202}
203
204static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
205{
206 unsigned int state_idx = genpd->state_idx;
207 ktime_t time_start;
208 s64 elapsed_ns;
209 int ret;
210
211 if (!genpd->power_on)
212 return 0;
213
214 if (!timed)
215 return genpd->power_on(genpd);
216
217 time_start = ktime_get();
218 ret = genpd->power_on(genpd);
219 if (ret)
220 return ret;
221
222 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
223 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
224 return ret;
225
226 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
227 genpd->max_off_time_changed = true;
228 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
229 genpd->name, "on", elapsed_ns);
230
231 return ret;
232}
233
234static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
235{
236 unsigned int state_idx = genpd->state_idx;
237 ktime_t time_start;
238 s64 elapsed_ns;
239 int ret;
240
241 if (!genpd->power_off)
242 return 0;
243
244 if (!timed)
245 return genpd->power_off(genpd);
246
247 time_start = ktime_get();
248 ret = genpd->power_off(genpd);
249 if (ret == -EBUSY)
250 return ret;
251
252 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
253 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
254 return ret;
255
256 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
257 genpd->max_off_time_changed = true;
258 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
259 genpd->name, "off", elapsed_ns);
260
261 return ret;
262}
263
264/**
265 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
266 * @genpd: PM domain to power off.
267 *
268 * Queue up the execution of genpd_poweroff() unless it's already been done
269 * before.
270 */
271static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
272{
273 queue_work(pm_wq, &genpd->power_off_work);
274}
275
276/**
277 * genpd_poweron - Restore power to a given PM domain and its masters.
278 * @genpd: PM domain to power up.
279 * @depth: nesting count for lockdep.
280 *
281 * Restore power to @genpd and all of its masters so that it is possible to
282 * resume a device belonging to it.
283 */
284static int genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
285{
286 struct gpd_link *link;
287 int ret = 0;
288
289 if (genpd->status == GPD_STATE_ACTIVE)
290 return 0;
291
292 /*
293 * The list is guaranteed not to change while the loop below is being
294 * executed, unless one of the masters' .power_on() callbacks fiddles
295 * with it.
296 */
297 list_for_each_entry(link, &genpd->slave_links, slave_node) {
298 struct generic_pm_domain *master = link->master;
299
300 genpd_sd_counter_inc(master);
301
302 genpd_lock_nested(master, depth + 1);
303 ret = genpd_poweron(master, depth + 1);
304 genpd_unlock(master);
305
306 if (ret) {
307 genpd_sd_counter_dec(master);
308 goto err;
309 }
310 }
311
312 ret = genpd_power_on(genpd, true);
313 if (ret)
314 goto err;
315
316 genpd->status = GPD_STATE_ACTIVE;
317 return 0;
318
319 err:
320 list_for_each_entry_continue_reverse(link,
321 &genpd->slave_links,
322 slave_node) {
323 genpd_sd_counter_dec(link->master);
324 genpd_queue_power_off_work(link->master);
325 }
326
327 return ret;
328}
329
330static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
331 unsigned long val, void *ptr)
332{
333 struct generic_pm_domain_data *gpd_data;
334 struct device *dev;
335
336 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
337 dev = gpd_data->base.dev;
338
339 for (;;) {
340 struct generic_pm_domain *genpd;
341 struct pm_domain_data *pdd;
342
343 spin_lock_irq(&dev->power.lock);
344
345 pdd = dev->power.subsys_data ?
346 dev->power.subsys_data->domain_data : NULL;
347 if (pdd && pdd->dev) {
348 to_gpd_data(pdd)->td.constraint_changed = true;
349 genpd = dev_to_genpd(dev);
350 } else {
351 genpd = ERR_PTR(-ENODATA);
352 }
353
354 spin_unlock_irq(&dev->power.lock);
355
356 if (!IS_ERR(genpd)) {
357 genpd_lock(genpd);
358 genpd->max_off_time_changed = true;
359 genpd_unlock(genpd);
360 }
361
362 dev = dev->parent;
363 if (!dev || dev->power.ignore_children)
364 break;
365 }
366
367 return NOTIFY_DONE;
368}
369
370/**
371 * genpd_poweroff - Remove power from a given PM domain.
372 * @genpd: PM domain to power down.
373 * @is_async: PM domain is powered down from a scheduled work
374 *
375 * If all of the @genpd's devices have been suspended and all of its subdomains
376 * have been powered down, remove power from @genpd.
377 */
378static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
379{
380 struct pm_domain_data *pdd;
381 struct gpd_link *link;
382 unsigned int not_suspended = 0;
383
384 /*
385 * Do not try to power off the domain in the following situations:
386 * (1) The domain is already in the "power off" state.
387 * (2) System suspend is in progress.
388 */
389 if (genpd->status == GPD_STATE_POWER_OFF
390 || genpd->prepared_count > 0)
391 return 0;
392
393 if (atomic_read(&genpd->sd_count) > 0)
394 return -EBUSY;
395
396 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
397 enum pm_qos_flags_status stat;
398
399 stat = dev_pm_qos_flags(pdd->dev,
400 PM_QOS_FLAG_NO_POWER_OFF
401 | PM_QOS_FLAG_REMOTE_WAKEUP);
402 if (stat > PM_QOS_FLAGS_NONE)
403 return -EBUSY;
404
405 /*
406 * Do not allow PM domain to be powered off, when an IRQ safe
407 * device is part of a non-IRQ safe domain.
408 */
409 if (!pm_runtime_suspended(pdd->dev) ||
410 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
411 not_suspended++;
412 }
413
414 if (not_suspended > 1 || (not_suspended == 1 && is_async))
415 return -EBUSY;
416
417 if (genpd->gov && genpd->gov->power_down_ok) {
418 if (!genpd->gov->power_down_ok(&genpd->domain))
419 return -EAGAIN;
420 }
421
422 if (genpd->power_off) {
423 int ret;
424
425 if (atomic_read(&genpd->sd_count) > 0)
426 return -EBUSY;
427
428 /*
429 * If sd_count > 0 at this point, one of the subdomains hasn't
430 * managed to call genpd_poweron() for the master yet after
431 * incrementing it. In that case genpd_poweron() will wait
432 * for us to drop the lock, so we can call .power_off() and let
433 * the genpd_poweron() restore power for us (this shouldn't
434 * happen very often).
435 */
436 ret = genpd_power_off(genpd, true);
437 if (ret)
438 return ret;
439 }
440
441 genpd->status = GPD_STATE_POWER_OFF;
442
443 list_for_each_entry(link, &genpd->slave_links, slave_node) {
444 genpd_sd_counter_dec(link->master);
445 genpd_queue_power_off_work(link->master);
446 }
447
448 return 0;
449}
450
451/**
452 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
453 * @work: Work structure used for scheduling the execution of this function.
454 */
455static void genpd_power_off_work_fn(struct work_struct *work)
456{
457 struct generic_pm_domain *genpd;
458
459 genpd = container_of(work, struct generic_pm_domain, power_off_work);
460
461 genpd_lock(genpd);
462 genpd_poweroff(genpd, true);
463 genpd_unlock(genpd);
464}
465
466/**
467 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
468 * @dev: Device to handle.
469 */
470static int __genpd_runtime_suspend(struct device *dev)
471{
472 int (*cb)(struct device *__dev);
473
474 if (dev->type && dev->type->pm)
475 cb = dev->type->pm->runtime_suspend;
476 else if (dev->class && dev->class->pm)
477 cb = dev->class->pm->runtime_suspend;
478 else if (dev->bus && dev->bus->pm)
479 cb = dev->bus->pm->runtime_suspend;
480 else
481 cb = NULL;
482
483 if (!cb && dev->driver && dev->driver->pm)
484 cb = dev->driver->pm->runtime_suspend;
485
486 return cb ? cb(dev) : 0;
487}
488
489/**
490 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
491 * @dev: Device to handle.
492 */
493static int __genpd_runtime_resume(struct device *dev)
494{
495 int (*cb)(struct device *__dev);
496
497 if (dev->type && dev->type->pm)
498 cb = dev->type->pm->runtime_resume;
499 else if (dev->class && dev->class->pm)
500 cb = dev->class->pm->runtime_resume;
501 else if (dev->bus && dev->bus->pm)
502 cb = dev->bus->pm->runtime_resume;
503 else
504 cb = NULL;
505
506 if (!cb && dev->driver && dev->driver->pm)
507 cb = dev->driver->pm->runtime_resume;
508
509 return cb ? cb(dev) : 0;
510}
511
512/**
513 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
514 * @dev: Device to suspend.
515 *
516 * Carry out a runtime suspend of a device under the assumption that its
517 * pm_domain field points to the domain member of an object of type
518 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
519 */
520static int genpd_runtime_suspend(struct device *dev)
521{
522 struct generic_pm_domain *genpd;
523 bool (*suspend_ok)(struct device *__dev);
524 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
525 bool runtime_pm = pm_runtime_enabled(dev);
526 ktime_t time_start;
527 s64 elapsed_ns;
528 int ret;
529
530 dev_dbg(dev, "%s()\n", __func__);
531
532 genpd = dev_to_genpd(dev);
533 if (IS_ERR(genpd))
534 return -EINVAL;
535
536 /*
537 * A runtime PM centric subsystem/driver may re-use the runtime PM
538 * callbacks for other purposes than runtime PM. In those scenarios
539 * runtime PM is disabled. Under these circumstances, we shall skip
540 * validating/measuring the PM QoS latency.
541 */
542 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
543 if (runtime_pm && suspend_ok && !suspend_ok(dev))
544 return -EBUSY;
545
546 /* Measure suspend latency. */
547 time_start = 0;
548 if (runtime_pm)
549 time_start = ktime_get();
550
551 ret = __genpd_runtime_suspend(dev);
552 if (ret)
553 return ret;
554
555 ret = genpd_stop_dev(genpd, dev);
556 if (ret) {
557 __genpd_runtime_resume(dev);
558 return ret;
559 }
560
561 /* Update suspend latency value if the measured time exceeds it. */
562 if (runtime_pm) {
563 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
564 if (elapsed_ns > td->suspend_latency_ns) {
565 td->suspend_latency_ns = elapsed_ns;
566 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
567 elapsed_ns);
568 genpd->max_off_time_changed = true;
569 td->constraint_changed = true;
570 }
571 }
572
573 /*
574 * If power.irq_safe is set, this routine may be run with
575 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
576 */
577 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
578 return 0;
579
580 genpd_lock(genpd);
581 genpd_poweroff(genpd, false);
582 genpd_unlock(genpd);
583
584 return 0;
585}
586
587/**
588 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
589 * @dev: Device to resume.
590 *
591 * Carry out a runtime resume of a device under the assumption that its
592 * pm_domain field points to the domain member of an object of type
593 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
594 */
595static int genpd_runtime_resume(struct device *dev)
596{
597 struct generic_pm_domain *genpd;
598 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
599 bool runtime_pm = pm_runtime_enabled(dev);
600 ktime_t time_start;
601 s64 elapsed_ns;
602 int ret;
603 bool timed = true;
604
605 dev_dbg(dev, "%s()\n", __func__);
606
607 genpd = dev_to_genpd(dev);
608 if (IS_ERR(genpd))
609 return -EINVAL;
610
611 /*
612 * As we don't power off a non IRQ safe domain, which holds
613 * an IRQ safe device, we don't need to restore power to it.
614 */
615 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
616 timed = false;
617 goto out;
618 }
619
620 genpd_lock(genpd);
621 ret = genpd_poweron(genpd, 0);
622 genpd_unlock(genpd);
623
624 if (ret)
625 return ret;
626
627 out:
628 /* Measure resume latency. */
629 time_start = 0;
630 if (timed && runtime_pm)
631 time_start = ktime_get();
632
633 ret = genpd_start_dev(genpd, dev);
634 if (ret)
635 goto err_poweroff;
636
637 ret = __genpd_runtime_resume(dev);
638 if (ret)
639 goto err_stop;
640
641 /* Update resume latency value if the measured time exceeds it. */
642 if (timed && runtime_pm) {
643 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
644 if (elapsed_ns > td->resume_latency_ns) {
645 td->resume_latency_ns = elapsed_ns;
646 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
647 elapsed_ns);
648 genpd->max_off_time_changed = true;
649 td->constraint_changed = true;
650 }
651 }
652
653 return 0;
654
655err_stop:
656 genpd_stop_dev(genpd, dev);
657err_poweroff:
658 if (!pm_runtime_is_irq_safe(dev) ||
659 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
660 genpd_lock(genpd);
661 genpd_poweroff(genpd, 0);
662 genpd_unlock(genpd);
663 }
664
665 return ret;
666}
667
668static bool pd_ignore_unused;
669static int __init pd_ignore_unused_setup(char *__unused)
670{
671 pd_ignore_unused = true;
672 return 1;
673}
674__setup("pd_ignore_unused", pd_ignore_unused_setup);
675
676/**
677 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
678 */
679static int __init genpd_poweroff_unused(void)
680{
681 struct generic_pm_domain *genpd;
682
683 if (pd_ignore_unused) {
684 pr_warn("genpd: Not disabling unused power domains\n");
685 return 0;
686 }
687
688 mutex_lock(&gpd_list_lock);
689
690 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
691 genpd_queue_power_off_work(genpd);
692
693 mutex_unlock(&gpd_list_lock);
694
695 return 0;
696}
697late_initcall(genpd_poweroff_unused);
698
699#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
700
701/**
702 * pm_genpd_present - Check if the given PM domain has been initialized.
703 * @genpd: PM domain to check.
704 */
705static bool pm_genpd_present(const struct generic_pm_domain *genpd)
706{
707 const struct generic_pm_domain *gpd;
708
709 if (IS_ERR_OR_NULL(genpd))
710 return false;
711
712 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
713 if (gpd == genpd)
714 return true;
715
716 return false;
717}
718
719#endif
720
721#ifdef CONFIG_PM_SLEEP
722
723static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
724 struct device *dev)
725{
726 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
727}
728
729/**
730 * genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
731 * @genpd: PM domain to power off, if possible.
732 *
733 * Check if the given PM domain can be powered off (during system suspend or
734 * hibernation) and do that if so. Also, in that case propagate to its masters.
735 *
736 * This function is only called in "noirq" and "syscore" stages of system power
737 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
738 * executed sequentially, so it is guaranteed that it will never run twice in
739 * parallel).
740 */
741static void genpd_sync_poweroff(struct generic_pm_domain *genpd)
742{
743 struct gpd_link *link;
744
745 if (genpd->status == GPD_STATE_POWER_OFF)
746 return;
747
748 if (genpd->suspended_count != genpd->device_count
749 || atomic_read(&genpd->sd_count) > 0)
750 return;
751
752 /* Choose the deepest state when suspending */
753 genpd->state_idx = genpd->state_count - 1;
754 genpd_power_off(genpd, false);
755
756 genpd->status = GPD_STATE_POWER_OFF;
757
758 list_for_each_entry(link, &genpd->slave_links, slave_node) {
759 genpd_sd_counter_dec(link->master);
760 genpd_sync_poweroff(link->master);
761 }
762}
763
764/**
765 * genpd_sync_poweron - Synchronously power on a PM domain and its masters.
766 * @genpd: PM domain to power on.
767 *
768 * This function is only called in "noirq" and "syscore" stages of system power
769 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
770 * executed sequentially, so it is guaranteed that it will never run twice in
771 * parallel).
772 */
773static void genpd_sync_poweron(struct generic_pm_domain *genpd)
774{
775 struct gpd_link *link;
776
777 if (genpd->status == GPD_STATE_ACTIVE)
778 return;
779
780 list_for_each_entry(link, &genpd->slave_links, slave_node) {
781 genpd_sync_poweron(link->master);
782 genpd_sd_counter_inc(link->master);
783 }
784
785 genpd_power_on(genpd, false);
786
787 genpd->status = GPD_STATE_ACTIVE;
788}
789
790/**
791 * resume_needed - Check whether to resume a device before system suspend.
792 * @dev: Device to check.
793 * @genpd: PM domain the device belongs to.
794 *
795 * There are two cases in which a device that can wake up the system from sleep
796 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
797 * to wake up the system and it has to remain active for this purpose while the
798 * system is in the sleep state and (2) if the device is not enabled to wake up
799 * the system from sleep states and it generally doesn't generate wakeup signals
800 * by itself (those signals are generated on its behalf by other parts of the
801 * system). In the latter case it may be necessary to reconfigure the device's
802 * wakeup settings during system suspend, because it may have been set up to
803 * signal remote wakeup from the system's working state as needed by runtime PM.
804 * Return 'true' in either of the above cases.
805 */
806static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
807{
808 bool active_wakeup;
809
810 if (!device_can_wakeup(dev))
811 return false;
812
813 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
814 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
815}
816
817/**
818 * pm_genpd_prepare - Start power transition of a device in a PM domain.
819 * @dev: Device to start the transition of.
820 *
821 * Start a power transition of a device (during a system-wide power transition)
822 * under the assumption that its pm_domain field points to the domain member of
823 * an object of type struct generic_pm_domain representing a PM domain
824 * consisting of I/O devices.
825 */
826static int pm_genpd_prepare(struct device *dev)
827{
828 struct generic_pm_domain *genpd;
829 int ret;
830
831 dev_dbg(dev, "%s()\n", __func__);
832
833 genpd = dev_to_genpd(dev);
834 if (IS_ERR(genpd))
835 return -EINVAL;
836
837 /*
838 * If a wakeup request is pending for the device, it should be woken up
839 * at this point and a system wakeup event should be reported if it's
840 * set up to wake up the system from sleep states.
841 */
842 if (resume_needed(dev, genpd))
843 pm_runtime_resume(dev);
844
845 genpd_lock(genpd);
846
847 if (genpd->prepared_count++ == 0)
848 genpd->suspended_count = 0;
849
850 genpd_unlock(genpd);
851
852 ret = pm_generic_prepare(dev);
853 if (ret) {
854 genpd_lock(genpd);
855
856 genpd->prepared_count--;
857
858 genpd_unlock(genpd);
859 }
860
861 return ret;
862}
863
864/**
865 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
866 * @dev: Device to suspend.
867 *
868 * Stop the device and remove power from the domain if all devices in it have
869 * been stopped.
870 */
871static int pm_genpd_suspend_noirq(struct device *dev)
872{
873 struct generic_pm_domain *genpd;
874 int ret;
875
876 dev_dbg(dev, "%s()\n", __func__);
877
878 genpd = dev_to_genpd(dev);
879 if (IS_ERR(genpd))
880 return -EINVAL;
881
882 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
883 return 0;
884
885 if (genpd->dev_ops.stop && genpd->dev_ops.start) {
886 ret = pm_runtime_force_suspend(dev);
887 if (ret)
888 return ret;
889 }
890
891 /*
892 * Since all of the "noirq" callbacks are executed sequentially, it is
893 * guaranteed that this function will never run twice in parallel for
894 * the same PM domain, so it is not necessary to use locking here.
895 */
896 genpd->suspended_count++;
897 genpd_sync_poweroff(genpd);
898
899 return 0;
900}
901
902/**
903 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
904 * @dev: Device to resume.
905 *
906 * Restore power to the device's PM domain, if necessary, and start the device.
907 */
908static int pm_genpd_resume_noirq(struct device *dev)
909{
910 struct generic_pm_domain *genpd;
911 int ret = 0;
912
913 dev_dbg(dev, "%s()\n", __func__);
914
915 genpd = dev_to_genpd(dev);
916 if (IS_ERR(genpd))
917 return -EINVAL;
918
919 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
920 return 0;
921
922 /*
923 * Since all of the "noirq" callbacks are executed sequentially, it is
924 * guaranteed that this function will never run twice in parallel for
925 * the same PM domain, so it is not necessary to use locking here.
926 */
927 genpd_sync_poweron(genpd);
928 genpd->suspended_count--;
929
930 if (genpd->dev_ops.stop && genpd->dev_ops.start)
931 ret = pm_runtime_force_resume(dev);
932
933 return ret;
934}
935
936/**
937 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
938 * @dev: Device to freeze.
939 *
940 * Carry out a late freeze of a device under the assumption that its
941 * pm_domain field points to the domain member of an object of type
942 * struct generic_pm_domain representing a power domain consisting of I/O
943 * devices.
944 */
945static int pm_genpd_freeze_noirq(struct device *dev)
946{
947 struct generic_pm_domain *genpd;
948 int ret = 0;
949
950 dev_dbg(dev, "%s()\n", __func__);
951
952 genpd = dev_to_genpd(dev);
953 if (IS_ERR(genpd))
954 return -EINVAL;
955
956 if (genpd->dev_ops.stop && genpd->dev_ops.start)
957 ret = pm_runtime_force_suspend(dev);
958
959 return ret;
960}
961
962/**
963 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
964 * @dev: Device to thaw.
965 *
966 * Start the device, unless power has been removed from the domain already
967 * before the system transition.
968 */
969static int pm_genpd_thaw_noirq(struct device *dev)
970{
971 struct generic_pm_domain *genpd;
972 int ret = 0;
973
974 dev_dbg(dev, "%s()\n", __func__);
975
976 genpd = dev_to_genpd(dev);
977 if (IS_ERR(genpd))
978 return -EINVAL;
979
980 if (genpd->dev_ops.stop && genpd->dev_ops.start)
981 ret = pm_runtime_force_resume(dev);
982
983 return ret;
984}
985
986/**
987 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
988 * @dev: Device to resume.
989 *
990 * Make sure the domain will be in the same power state as before the
991 * hibernation the system is resuming from and start the device if necessary.
992 */
993static int pm_genpd_restore_noirq(struct device *dev)
994{
995 struct generic_pm_domain *genpd;
996 int ret = 0;
997
998 dev_dbg(dev, "%s()\n", __func__);
999
1000 genpd = dev_to_genpd(dev);
1001 if (IS_ERR(genpd))
1002 return -EINVAL;
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 * At this point suspended_count == 0 means we are being run for the
1010 * first time for the given domain in the present cycle.
1011 */
1012 if (genpd->suspended_count++ == 0)
1013 /*
1014 * The boot kernel might put the domain into arbitrary state,
1015 * so make it appear as powered off to genpd_sync_poweron(),
1016 * so that it tries to power it on in case it was really off.
1017 */
1018 genpd->status = GPD_STATE_POWER_OFF;
1019
1020 genpd_sync_poweron(genpd);
1021
1022 if (genpd->dev_ops.stop && genpd->dev_ops.start)
1023 ret = pm_runtime_force_resume(dev);
1024
1025 return ret;
1026}
1027
1028/**
1029 * pm_genpd_complete - Complete power transition of a device in a power domain.
1030 * @dev: Device to complete the transition of.
1031 *
1032 * Complete a power transition of a device (during a system-wide power
1033 * transition) under the assumption that its pm_domain field points to the
1034 * domain member of an object of type struct generic_pm_domain representing
1035 * a power domain consisting of I/O devices.
1036 */
1037static void pm_genpd_complete(struct device *dev)
1038{
1039 struct generic_pm_domain *genpd;
1040
1041 dev_dbg(dev, "%s()\n", __func__);
1042
1043 genpd = dev_to_genpd(dev);
1044 if (IS_ERR(genpd))
1045 return;
1046
1047 pm_generic_complete(dev);
1048
1049 genpd_lock(genpd);
1050
1051 genpd->prepared_count--;
1052 if (!genpd->prepared_count)
1053 genpd_queue_power_off_work(genpd);
1054
1055 genpd_unlock(genpd);
1056}
1057
1058/**
1059 * genpd_syscore_switch - Switch power during system core suspend or resume.
1060 * @dev: Device that normally is marked as "always on" to switch power for.
1061 *
1062 * This routine may only be called during the system core (syscore) suspend or
1063 * resume phase for devices whose "always on" flags are set.
1064 */
1065static void genpd_syscore_switch(struct device *dev, bool suspend)
1066{
1067 struct generic_pm_domain *genpd;
1068
1069 genpd = dev_to_genpd(dev);
1070 if (!pm_genpd_present(genpd))
1071 return;
1072
1073 if (suspend) {
1074 genpd->suspended_count++;
1075 genpd_sync_poweroff(genpd);
1076 } else {
1077 genpd_sync_poweron(genpd);
1078 genpd->suspended_count--;
1079 }
1080}
1081
1082void pm_genpd_syscore_poweroff(struct device *dev)
1083{
1084 genpd_syscore_switch(dev, true);
1085}
1086EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1087
1088void pm_genpd_syscore_poweron(struct device *dev)
1089{
1090 genpd_syscore_switch(dev, false);
1091}
1092EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1093
1094#else /* !CONFIG_PM_SLEEP */
1095
1096#define pm_genpd_prepare NULL
1097#define pm_genpd_suspend_noirq NULL
1098#define pm_genpd_resume_noirq NULL
1099#define pm_genpd_freeze_noirq NULL
1100#define pm_genpd_thaw_noirq NULL
1101#define pm_genpd_restore_noirq NULL
1102#define pm_genpd_complete NULL
1103
1104#endif /* CONFIG_PM_SLEEP */
1105
1106static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1107 struct generic_pm_domain *genpd,
1108 struct gpd_timing_data *td)
1109{
1110 struct generic_pm_domain_data *gpd_data;
1111 int ret;
1112
1113 ret = dev_pm_get_subsys_data(dev);
1114 if (ret)
1115 return ERR_PTR(ret);
1116
1117 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1118 if (!gpd_data) {
1119 ret = -ENOMEM;
1120 goto err_put;
1121 }
1122
1123 if (td)
1124 gpd_data->td = *td;
1125
1126 gpd_data->base.dev = dev;
1127 gpd_data->td.constraint_changed = true;
1128 gpd_data->td.effective_constraint_ns = -1;
1129 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1130
1131 spin_lock_irq(&dev->power.lock);
1132
1133 if (dev->power.subsys_data->domain_data) {
1134 ret = -EINVAL;
1135 goto err_free;
1136 }
1137
1138 dev->power.subsys_data->domain_data = &gpd_data->base;
1139
1140 spin_unlock_irq(&dev->power.lock);
1141
1142 dev_pm_domain_set(dev, &genpd->domain);
1143
1144 return gpd_data;
1145
1146 err_free:
1147 spin_unlock_irq(&dev->power.lock);
1148 kfree(gpd_data);
1149 err_put:
1150 dev_pm_put_subsys_data(dev);
1151 return ERR_PTR(ret);
1152}
1153
1154static void genpd_free_dev_data(struct device *dev,
1155 struct generic_pm_domain_data *gpd_data)
1156{
1157 dev_pm_domain_set(dev, NULL);
1158
1159 spin_lock_irq(&dev->power.lock);
1160
1161 dev->power.subsys_data->domain_data = NULL;
1162
1163 spin_unlock_irq(&dev->power.lock);
1164
1165 kfree(gpd_data);
1166 dev_pm_put_subsys_data(dev);
1167}
1168
1169static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1170 struct gpd_timing_data *td)
1171{
1172 struct generic_pm_domain_data *gpd_data;
1173 int ret = 0;
1174
1175 dev_dbg(dev, "%s()\n", __func__);
1176
1177 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1178 return -EINVAL;
1179
1180 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
1181 if (IS_ERR(gpd_data))
1182 return PTR_ERR(gpd_data);
1183
1184 genpd_lock(genpd);
1185
1186 if (genpd->prepared_count > 0) {
1187 ret = -EAGAIN;
1188 goto out;
1189 }
1190
1191 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1192 if (ret)
1193 goto out;
1194
1195 genpd->device_count++;
1196 genpd->max_off_time_changed = true;
1197
1198 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1199
1200 out:
1201 genpd_unlock(genpd);
1202
1203 if (ret)
1204 genpd_free_dev_data(dev, gpd_data);
1205 else
1206 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1207
1208 return ret;
1209}
1210
1211/**
1212 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1213 * @genpd: PM domain to add the device to.
1214 * @dev: Device to be added.
1215 * @td: Set of PM QoS timing parameters to attach to the device.
1216 */
1217int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1218 struct gpd_timing_data *td)
1219{
1220 int ret;
1221
1222 mutex_lock(&gpd_list_lock);
1223 ret = genpd_add_device(genpd, dev, td);
1224 mutex_unlock(&gpd_list_lock);
1225
1226 return ret;
1227}
1228EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
1229
1230static int genpd_remove_device(struct generic_pm_domain *genpd,
1231 struct device *dev)
1232{
1233 struct generic_pm_domain_data *gpd_data;
1234 struct pm_domain_data *pdd;
1235 int ret = 0;
1236
1237 dev_dbg(dev, "%s()\n", __func__);
1238
1239 pdd = dev->power.subsys_data->domain_data;
1240 gpd_data = to_gpd_data(pdd);
1241 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1242
1243 genpd_lock(genpd);
1244
1245 if (genpd->prepared_count > 0) {
1246 ret = -EAGAIN;
1247 goto out;
1248 }
1249
1250 genpd->device_count--;
1251 genpd->max_off_time_changed = true;
1252
1253 if (genpd->detach_dev)
1254 genpd->detach_dev(genpd, dev);
1255
1256 list_del_init(&pdd->list_node);
1257
1258 genpd_unlock(genpd);
1259
1260 genpd_free_dev_data(dev, gpd_data);
1261
1262 return 0;
1263
1264 out:
1265 genpd_unlock(genpd);
1266 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1267
1268 return ret;
1269}
1270
1271/**
1272 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1273 * @genpd: PM domain to remove the device from.
1274 * @dev: Device to be removed.
1275 */
1276int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1277 struct device *dev)
1278{
1279 if (!genpd || genpd != genpd_lookup_dev(dev))
1280 return -EINVAL;
1281
1282 return genpd_remove_device(genpd, dev);
1283}
1284EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1285
1286static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1287 struct generic_pm_domain *subdomain)
1288{
1289 struct gpd_link *link, *itr;
1290 int ret = 0;
1291
1292 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1293 || genpd == subdomain)
1294 return -EINVAL;
1295
1296 /*
1297 * If the domain can be powered on/off in an IRQ safe
1298 * context, ensure that the subdomain can also be
1299 * powered on/off in that context.
1300 */
1301 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1302 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1303 genpd->name, subdomain->name);
1304 return -EINVAL;
1305 }
1306
1307 link = kzalloc(sizeof(*link), GFP_KERNEL);
1308 if (!link)
1309 return -ENOMEM;
1310
1311 genpd_lock(subdomain);
1312 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1313
1314 if (genpd->status == GPD_STATE_POWER_OFF
1315 && subdomain->status != GPD_STATE_POWER_OFF) {
1316 ret = -EINVAL;
1317 goto out;
1318 }
1319
1320 list_for_each_entry(itr, &genpd->master_links, master_node) {
1321 if (itr->slave == subdomain && itr->master == genpd) {
1322 ret = -EINVAL;
1323 goto out;
1324 }
1325 }
1326
1327 link->master = genpd;
1328 list_add_tail(&link->master_node, &genpd->master_links);
1329 link->slave = subdomain;
1330 list_add_tail(&link->slave_node, &subdomain->slave_links);
1331 if (subdomain->status != GPD_STATE_POWER_OFF)
1332 genpd_sd_counter_inc(genpd);
1333
1334 out:
1335 genpd_unlock(genpd);
1336 genpd_unlock(subdomain);
1337 if (ret)
1338 kfree(link);
1339 return ret;
1340}
1341
1342/**
1343 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1344 * @genpd: Master PM domain to add the subdomain to.
1345 * @subdomain: Subdomain to be added.
1346 */
1347int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1348 struct generic_pm_domain *subdomain)
1349{
1350 int ret;
1351
1352 mutex_lock(&gpd_list_lock);
1353 ret = genpd_add_subdomain(genpd, subdomain);
1354 mutex_unlock(&gpd_list_lock);
1355
1356 return ret;
1357}
1358EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1359
1360/**
1361 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1362 * @genpd: Master PM domain to remove the subdomain from.
1363 * @subdomain: Subdomain to be removed.
1364 */
1365int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1366 struct generic_pm_domain *subdomain)
1367{
1368 struct gpd_link *link;
1369 int ret = -EINVAL;
1370
1371 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1372 return -EINVAL;
1373
1374 genpd_lock(subdomain);
1375 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1376
1377 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
1378 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1379 subdomain->name);
1380 ret = -EBUSY;
1381 goto out;
1382 }
1383
1384 list_for_each_entry(link, &genpd->master_links, master_node) {
1385 if (link->slave != subdomain)
1386 continue;
1387
1388 list_del(&link->master_node);
1389 list_del(&link->slave_node);
1390 kfree(link);
1391 if (subdomain->status != GPD_STATE_POWER_OFF)
1392 genpd_sd_counter_dec(genpd);
1393
1394 ret = 0;
1395 break;
1396 }
1397
1398out:
1399 genpd_unlock(genpd);
1400 genpd_unlock(subdomain);
1401
1402 return ret;
1403}
1404EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1405
1406static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1407{
1408 struct genpd_power_state *state;
1409
1410 state = kzalloc(sizeof(*state), GFP_KERNEL);
1411 if (!state)
1412 return -ENOMEM;
1413
1414 genpd->states = state;
1415 genpd->state_count = 1;
1416 genpd->free = state;
1417
1418 return 0;
1419}
1420
1421static void genpd_lock_init(struct generic_pm_domain *genpd)
1422{
1423 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1424 spin_lock_init(&genpd->slock);
1425 genpd->lock_ops = &genpd_spin_ops;
1426 } else {
1427 mutex_init(&genpd->mlock);
1428 genpd->lock_ops = &genpd_mtx_ops;
1429 }
1430}
1431
1432/**
1433 * pm_genpd_init - Initialize a generic I/O PM domain object.
1434 * @genpd: PM domain object to initialize.
1435 * @gov: PM domain governor to associate with the domain (may be NULL).
1436 * @is_off: Initial value of the domain's power_is_off field.
1437 *
1438 * Returns 0 on successful initialization, else a negative error code.
1439 */
1440int pm_genpd_init(struct generic_pm_domain *genpd,
1441 struct dev_power_governor *gov, bool is_off)
1442{
1443 int ret;
1444
1445 if (IS_ERR_OR_NULL(genpd))
1446 return -EINVAL;
1447
1448 INIT_LIST_HEAD(&genpd->master_links);
1449 INIT_LIST_HEAD(&genpd->slave_links);
1450 INIT_LIST_HEAD(&genpd->dev_list);
1451 genpd_lock_init(genpd);
1452 genpd->gov = gov;
1453 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1454 atomic_set(&genpd->sd_count, 0);
1455 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1456 genpd->device_count = 0;
1457 genpd->max_off_time_ns = -1;
1458 genpd->max_off_time_changed = true;
1459 genpd->provider = NULL;
1460 genpd->has_provider = false;
1461 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1462 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1463 genpd->domain.ops.prepare = pm_genpd_prepare;
1464 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1465 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1466 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1467 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1468 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1469 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1470 genpd->domain.ops.complete = pm_genpd_complete;
1471
1472 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1473 genpd->dev_ops.stop = pm_clk_suspend;
1474 genpd->dev_ops.start = pm_clk_resume;
1475 }
1476
1477 /* Use only one "off" state if there were no states declared */
1478 if (genpd->state_count == 0) {
1479 ret = genpd_set_default_power_state(genpd);
1480 if (ret)
1481 return ret;
1482 }
1483
1484 mutex_lock(&gpd_list_lock);
1485 list_add(&genpd->gpd_list_node, &gpd_list);
1486 mutex_unlock(&gpd_list_lock);
1487
1488 return 0;
1489}
1490EXPORT_SYMBOL_GPL(pm_genpd_init);
1491
1492static int genpd_remove(struct generic_pm_domain *genpd)
1493{
1494 struct gpd_link *l, *link;
1495
1496 if (IS_ERR_OR_NULL(genpd))
1497 return -EINVAL;
1498
1499 genpd_lock(genpd);
1500
1501 if (genpd->has_provider) {
1502 genpd_unlock(genpd);
1503 pr_err("Provider present, unable to remove %s\n", genpd->name);
1504 return -EBUSY;
1505 }
1506
1507 if (!list_empty(&genpd->master_links) || genpd->device_count) {
1508 genpd_unlock(genpd);
1509 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1510 return -EBUSY;
1511 }
1512
1513 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1514 list_del(&link->master_node);
1515 list_del(&link->slave_node);
1516 kfree(link);
1517 }
1518
1519 list_del(&genpd->gpd_list_node);
1520 genpd_unlock(genpd);
1521 cancel_work_sync(&genpd->power_off_work);
1522 kfree(genpd->free);
1523 pr_debug("%s: removed %s\n", __func__, genpd->name);
1524
1525 return 0;
1526}
1527
1528/**
1529 * pm_genpd_remove - Remove a generic I/O PM domain
1530 * @genpd: Pointer to PM domain that is to be removed.
1531 *
1532 * To remove the PM domain, this function:
1533 * - Removes the PM domain as a subdomain to any parent domains,
1534 * if it was added.
1535 * - Removes the PM domain from the list of registered PM domains.
1536 *
1537 * The PM domain will only be removed, if the associated provider has
1538 * been removed, it is not a parent to any other PM domain and has no
1539 * devices associated with it.
1540 */
1541int pm_genpd_remove(struct generic_pm_domain *genpd)
1542{
1543 int ret;
1544
1545 mutex_lock(&gpd_list_lock);
1546 ret = genpd_remove(genpd);
1547 mutex_unlock(&gpd_list_lock);
1548
1549 return ret;
1550}
1551EXPORT_SYMBOL_GPL(pm_genpd_remove);
1552
1553#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1554
1555typedef struct generic_pm_domain *(*genpd_xlate_t)(struct of_phandle_args *args,
1556 void *data);
1557
1558/*
1559 * Device Tree based PM domain providers.
1560 *
1561 * The code below implements generic device tree based PM domain providers that
1562 * bind device tree nodes with generic PM domains registered in the system.
1563 *
1564 * Any driver that registers generic PM domains and needs to support binding of
1565 * devices to these domains is supposed to register a PM domain provider, which
1566 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1567 *
1568 * Two simple mapping functions have been provided for convenience:
1569 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1570 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1571 * index.
1572 */
1573
1574/**
1575 * struct of_genpd_provider - PM domain provider registration structure
1576 * @link: Entry in global list of PM domain providers
1577 * @node: Pointer to device tree node of PM domain provider
1578 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1579 * into a PM domain.
1580 * @data: context pointer to be passed into @xlate callback
1581 */
1582struct of_genpd_provider {
1583 struct list_head link;
1584 struct device_node *node;
1585 genpd_xlate_t xlate;
1586 void *data;
1587};
1588
1589/* List of registered PM domain providers. */
1590static LIST_HEAD(of_genpd_providers);
1591/* Mutex to protect the list above. */
1592static DEFINE_MUTEX(of_genpd_mutex);
1593
1594/**
1595 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1596 * @genpdspec: OF phandle args to map into a PM domain
1597 * @data: xlate function private data - pointer to struct generic_pm_domain
1598 *
1599 * This is a generic xlate function that can be used to model PM domains that
1600 * have their own device tree nodes. The private data of xlate function needs
1601 * to be a valid pointer to struct generic_pm_domain.
1602 */
1603static struct generic_pm_domain *genpd_xlate_simple(
1604 struct of_phandle_args *genpdspec,
1605 void *data)
1606{
1607 if (genpdspec->args_count != 0)
1608 return ERR_PTR(-EINVAL);
1609 return data;
1610}
1611
1612/**
1613 * genpd_xlate_onecell() - Xlate function using a single index.
1614 * @genpdspec: OF phandle args to map into a PM domain
1615 * @data: xlate function private data - pointer to struct genpd_onecell_data
1616 *
1617 * This is a generic xlate function that can be used to model simple PM domain
1618 * controllers that have one device tree node and provide multiple PM domains.
1619 * A single cell is used as an index into an array of PM domains specified in
1620 * the genpd_onecell_data struct when registering the provider.
1621 */
1622static struct generic_pm_domain *genpd_xlate_onecell(
1623 struct of_phandle_args *genpdspec,
1624 void *data)
1625{
1626 struct genpd_onecell_data *genpd_data = data;
1627 unsigned int idx = genpdspec->args[0];
1628
1629 if (genpdspec->args_count != 1)
1630 return ERR_PTR(-EINVAL);
1631
1632 if (idx >= genpd_data->num_domains) {
1633 pr_err("%s: invalid domain index %u\n", __func__, idx);
1634 return ERR_PTR(-EINVAL);
1635 }
1636
1637 if (!genpd_data->domains[idx])
1638 return ERR_PTR(-ENOENT);
1639
1640 return genpd_data->domains[idx];
1641}
1642
1643/**
1644 * genpd_add_provider() - Register a PM domain provider for a node
1645 * @np: Device node pointer associated with the PM domain provider.
1646 * @xlate: Callback for decoding PM domain from phandle arguments.
1647 * @data: Context pointer for @xlate callback.
1648 */
1649static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1650 void *data)
1651{
1652 struct of_genpd_provider *cp;
1653
1654 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1655 if (!cp)
1656 return -ENOMEM;
1657
1658 cp->node = of_node_get(np);
1659 cp->data = data;
1660 cp->xlate = xlate;
1661
1662 mutex_lock(&of_genpd_mutex);
1663 list_add(&cp->link, &of_genpd_providers);
1664 mutex_unlock(&of_genpd_mutex);
1665 pr_debug("Added domain provider from %s\n", np->full_name);
1666
1667 return 0;
1668}
1669
1670/**
1671 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1672 * @np: Device node pointer associated with the PM domain provider.
1673 * @genpd: Pointer to PM domain associated with the PM domain provider.
1674 */
1675int of_genpd_add_provider_simple(struct device_node *np,
1676 struct generic_pm_domain *genpd)
1677{
1678 int ret = -EINVAL;
1679
1680 if (!np || !genpd)
1681 return -EINVAL;
1682
1683 mutex_lock(&gpd_list_lock);
1684
1685 if (pm_genpd_present(genpd))
1686 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1687
1688 if (!ret) {
1689 genpd->provider = &np->fwnode;
1690 genpd->has_provider = true;
1691 }
1692
1693 mutex_unlock(&gpd_list_lock);
1694
1695 return ret;
1696}
1697EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1698
1699/**
1700 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1701 * @np: Device node pointer associated with the PM domain provider.
1702 * @data: Pointer to the data associated with the PM domain provider.
1703 */
1704int of_genpd_add_provider_onecell(struct device_node *np,
1705 struct genpd_onecell_data *data)
1706{
1707 unsigned int i;
1708 int ret = -EINVAL;
1709
1710 if (!np || !data)
1711 return -EINVAL;
1712
1713 mutex_lock(&gpd_list_lock);
1714
1715 for (i = 0; i < data->num_domains; i++) {
1716 if (!data->domains[i])
1717 continue;
1718 if (!pm_genpd_present(data->domains[i]))
1719 goto error;
1720
1721 data->domains[i]->provider = &np->fwnode;
1722 data->domains[i]->has_provider = true;
1723 }
1724
1725 ret = genpd_add_provider(np, genpd_xlate_onecell, data);
1726 if (ret < 0)
1727 goto error;
1728
1729 mutex_unlock(&gpd_list_lock);
1730
1731 return 0;
1732
1733error:
1734 while (i--) {
1735 if (!data->domains[i])
1736 continue;
1737 data->domains[i]->provider = NULL;
1738 data->domains[i]->has_provider = false;
1739 }
1740
1741 mutex_unlock(&gpd_list_lock);
1742
1743 return ret;
1744}
1745EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
1746
1747/**
1748 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1749 * @np: Device node pointer associated with the PM domain provider
1750 */
1751void of_genpd_del_provider(struct device_node *np)
1752{
1753 struct of_genpd_provider *cp;
1754 struct generic_pm_domain *gpd;
1755
1756 mutex_lock(&gpd_list_lock);
1757 mutex_lock(&of_genpd_mutex);
1758 list_for_each_entry(cp, &of_genpd_providers, link) {
1759 if (cp->node == np) {
1760 /*
1761 * For each PM domain associated with the
1762 * provider, set the 'has_provider' to false
1763 * so that the PM domain can be safely removed.
1764 */
1765 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
1766 if (gpd->provider == &np->fwnode)
1767 gpd->has_provider = false;
1768
1769 list_del(&cp->link);
1770 of_node_put(cp->node);
1771 kfree(cp);
1772 break;
1773 }
1774 }
1775 mutex_unlock(&of_genpd_mutex);
1776 mutex_unlock(&gpd_list_lock);
1777}
1778EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1779
1780/**
1781 * genpd_get_from_provider() - Look-up PM domain
1782 * @genpdspec: OF phandle args to use for look-up
1783 *
1784 * Looks for a PM domain provider under the node specified by @genpdspec and if
1785 * found, uses xlate function of the provider to map phandle args to a PM
1786 * domain.
1787 *
1788 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1789 * on failure.
1790 */
1791static struct generic_pm_domain *genpd_get_from_provider(
1792 struct of_phandle_args *genpdspec)
1793{
1794 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1795 struct of_genpd_provider *provider;
1796
1797 if (!genpdspec)
1798 return ERR_PTR(-EINVAL);
1799
1800 mutex_lock(&of_genpd_mutex);
1801
1802 /* Check if we have such a provider in our array */
1803 list_for_each_entry(provider, &of_genpd_providers, link) {
1804 if (provider->node == genpdspec->np)
1805 genpd = provider->xlate(genpdspec, provider->data);
1806 if (!IS_ERR(genpd))
1807 break;
1808 }
1809
1810 mutex_unlock(&of_genpd_mutex);
1811
1812 return genpd;
1813}
1814
1815/**
1816 * of_genpd_add_device() - Add a device to an I/O PM domain
1817 * @genpdspec: OF phandle args to use for look-up PM domain
1818 * @dev: Device to be added.
1819 *
1820 * Looks-up an I/O PM domain based upon phandle args provided and adds
1821 * the device to the PM domain. Returns a negative error code on failure.
1822 */
1823int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
1824{
1825 struct generic_pm_domain *genpd;
1826 int ret;
1827
1828 mutex_lock(&gpd_list_lock);
1829
1830 genpd = genpd_get_from_provider(genpdspec);
1831 if (IS_ERR(genpd)) {
1832 ret = PTR_ERR(genpd);
1833 goto out;
1834 }
1835
1836 ret = genpd_add_device(genpd, dev, NULL);
1837
1838out:
1839 mutex_unlock(&gpd_list_lock);
1840
1841 return ret;
1842}
1843EXPORT_SYMBOL_GPL(of_genpd_add_device);
1844
1845/**
1846 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1847 * @parent_spec: OF phandle args to use for parent PM domain look-up
1848 * @subdomain_spec: OF phandle args to use for subdomain look-up
1849 *
1850 * Looks-up a parent PM domain and subdomain based upon phandle args
1851 * provided and adds the subdomain to the parent PM domain. Returns a
1852 * negative error code on failure.
1853 */
1854int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
1855 struct of_phandle_args *subdomain_spec)
1856{
1857 struct generic_pm_domain *parent, *subdomain;
1858 int ret;
1859
1860 mutex_lock(&gpd_list_lock);
1861
1862 parent = genpd_get_from_provider(parent_spec);
1863 if (IS_ERR(parent)) {
1864 ret = PTR_ERR(parent);
1865 goto out;
1866 }
1867
1868 subdomain = genpd_get_from_provider(subdomain_spec);
1869 if (IS_ERR(subdomain)) {
1870 ret = PTR_ERR(subdomain);
1871 goto out;
1872 }
1873
1874 ret = genpd_add_subdomain(parent, subdomain);
1875
1876out:
1877 mutex_unlock(&gpd_list_lock);
1878
1879 return ret;
1880}
1881EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
1882
1883/**
1884 * of_genpd_remove_last - Remove the last PM domain registered for a provider
1885 * @provider: Pointer to device structure associated with provider
1886 *
1887 * Find the last PM domain that was added by a particular provider and
1888 * remove this PM domain from the list of PM domains. The provider is
1889 * identified by the 'provider' device structure that is passed. The PM
1890 * domain will only be removed, if the provider associated with domain
1891 * has been removed.
1892 *
1893 * Returns a valid pointer to struct generic_pm_domain on success or
1894 * ERR_PTR() on failure.
1895 */
1896struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
1897{
1898 struct generic_pm_domain *gpd, *genpd = ERR_PTR(-ENOENT);
1899 int ret;
1900
1901 if (IS_ERR_OR_NULL(np))
1902 return ERR_PTR(-EINVAL);
1903
1904 mutex_lock(&gpd_list_lock);
1905 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1906 if (gpd->provider == &np->fwnode) {
1907 ret = genpd_remove(gpd);
1908 genpd = ret ? ERR_PTR(ret) : gpd;
1909 break;
1910 }
1911 }
1912 mutex_unlock(&gpd_list_lock);
1913
1914 return genpd;
1915}
1916EXPORT_SYMBOL_GPL(of_genpd_remove_last);
1917
1918/**
1919 * genpd_dev_pm_detach - Detach a device from its PM domain.
1920 * @dev: Device to detach.
1921 * @power_off: Currently not used
1922 *
1923 * Try to locate a corresponding generic PM domain, which the device was
1924 * attached to previously. If such is found, the device is detached from it.
1925 */
1926static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1927{
1928 struct generic_pm_domain *pd;
1929 unsigned int i;
1930 int ret = 0;
1931
1932 pd = dev_to_genpd(dev);
1933 if (IS_ERR(pd))
1934 return;
1935
1936 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1937
1938 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
1939 ret = genpd_remove_device(pd, dev);
1940 if (ret != -EAGAIN)
1941 break;
1942
1943 mdelay(i);
1944 cond_resched();
1945 }
1946
1947 if (ret < 0) {
1948 dev_err(dev, "failed to remove from PM domain %s: %d",
1949 pd->name, ret);
1950 return;
1951 }
1952
1953 /* Check if PM domain can be powered off after removing this device. */
1954 genpd_queue_power_off_work(pd);
1955}
1956
1957static void genpd_dev_pm_sync(struct device *dev)
1958{
1959 struct generic_pm_domain *pd;
1960
1961 pd = dev_to_genpd(dev);
1962 if (IS_ERR(pd))
1963 return;
1964
1965 genpd_queue_power_off_work(pd);
1966}
1967
1968/**
1969 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1970 * @dev: Device to attach.
1971 *
1972 * Parse device's OF node to find a PM domain specifier. If such is found,
1973 * attaches the device to retrieved pm_domain ops.
1974 *
1975 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1976 * backwards compatibility with existing DTBs.
1977 *
1978 * Returns 0 on successfully attached PM domain or negative error code. Note
1979 * that if a power-domain exists for the device, but it cannot be found or
1980 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1981 * probed and to re-try again later.
1982 */
1983int genpd_dev_pm_attach(struct device *dev)
1984{
1985 struct of_phandle_args pd_args;
1986 struct generic_pm_domain *pd;
1987 unsigned int i;
1988 int ret;
1989
1990 if (!dev->of_node)
1991 return -ENODEV;
1992
1993 if (dev->pm_domain)
1994 return -EEXIST;
1995
1996 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1997 "#power-domain-cells", 0, &pd_args);
1998 if (ret < 0) {
1999 if (ret != -ENOENT)
2000 return ret;
2001
2002 /*
2003 * Try legacy Samsung-specific bindings
2004 * (for backwards compatibility of DT ABI)
2005 */
2006 pd_args.args_count = 0;
2007 pd_args.np = of_parse_phandle(dev->of_node,
2008 "samsung,power-domain", 0);
2009 if (!pd_args.np)
2010 return -ENOENT;
2011 }
2012
2013 mutex_lock(&gpd_list_lock);
2014 pd = genpd_get_from_provider(&pd_args);
2015 of_node_put(pd_args.np);
2016 if (IS_ERR(pd)) {
2017 mutex_unlock(&gpd_list_lock);
2018 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2019 __func__, PTR_ERR(pd));
2020 return -EPROBE_DEFER;
2021 }
2022
2023 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2024
2025 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2026 ret = genpd_add_device(pd, dev, NULL);
2027 if (ret != -EAGAIN)
2028 break;
2029
2030 mdelay(i);
2031 cond_resched();
2032 }
2033 mutex_unlock(&gpd_list_lock);
2034
2035 if (ret < 0) {
2036 if (ret != -EPROBE_DEFER)
2037 dev_err(dev, "failed to add to PM domain %s: %d",
2038 pd->name, ret);
2039 goto out;
2040 }
2041
2042 dev->pm_domain->detach = genpd_dev_pm_detach;
2043 dev->pm_domain->sync = genpd_dev_pm_sync;
2044
2045 genpd_lock(pd);
2046 ret = genpd_poweron(pd, 0);
2047 genpd_unlock(pd);
2048out:
2049 return ret ? -EPROBE_DEFER : 0;
2050}
2051EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2052
2053static const struct of_device_id idle_state_match[] = {
2054 { .compatible = "domain-idle-state", },
2055 { }
2056};
2057
2058static int genpd_parse_state(struct genpd_power_state *genpd_state,
2059 struct device_node *state_node)
2060{
2061 int err;
2062 u32 residency;
2063 u32 entry_latency, exit_latency;
2064 const struct of_device_id *match_id;
2065
2066 match_id = of_match_node(idle_state_match, state_node);
2067 if (!match_id)
2068 return -EINVAL;
2069
2070 err = of_property_read_u32(state_node, "entry-latency-us",
2071 &entry_latency);
2072 if (err) {
2073 pr_debug(" * %s missing entry-latency-us property\n",
2074 state_node->full_name);
2075 return -EINVAL;
2076 }
2077
2078 err = of_property_read_u32(state_node, "exit-latency-us",
2079 &exit_latency);
2080 if (err) {
2081 pr_debug(" * %s missing exit-latency-us property\n",
2082 state_node->full_name);
2083 return -EINVAL;
2084 }
2085
2086 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2087 if (!err)
2088 genpd_state->residency_ns = 1000 * residency;
2089
2090 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2091 genpd_state->power_off_latency_ns = 1000 * entry_latency;
2092 genpd_state->fwnode = &state_node->fwnode;
2093
2094 return 0;
2095}
2096
2097/**
2098 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2099 *
2100 * @dn: The genpd device node
2101 * @states: The pointer to which the state array will be saved.
2102 * @n: The count of elements in the array returned from this function.
2103 *
2104 * Returns the device states parsed from the OF node. The memory for the states
2105 * is allocated by this function and is the responsibility of the caller to
2106 * free the memory after use.
2107 */
2108int of_genpd_parse_idle_states(struct device_node *dn,
2109 struct genpd_power_state **states, int *n)
2110{
2111 struct genpd_power_state *st;
2112 struct device_node *np;
2113 int i = 0;
2114 int err, ret;
2115 int count;
2116 struct of_phandle_iterator it;
2117
2118 count = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2119 if (count <= 0)
2120 return -EINVAL;
2121
2122 st = kcalloc(count, sizeof(*st), GFP_KERNEL);
2123 if (!st)
2124 return -ENOMEM;
2125
2126 /* Loop over the phandles until all the requested entry is found */
2127 of_for_each_phandle(&it, err, dn, "domain-idle-states", NULL, 0) {
2128 np = it.node;
2129 ret = genpd_parse_state(&st[i++], np);
2130 if (ret) {
2131 pr_err
2132 ("Parsing idle state node %s failed with err %d\n",
2133 np->full_name, ret);
2134 of_node_put(np);
2135 kfree(st);
2136 return ret;
2137 }
2138 }
2139
2140 *n = count;
2141 *states = st;
2142
2143 return 0;
2144}
2145EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2146
2147#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2148
2149
2150/*** debugfs support ***/
2151
2152#ifdef CONFIG_DEBUG_FS
2153#include <linux/pm.h>
2154#include <linux/device.h>
2155#include <linux/debugfs.h>
2156#include <linux/seq_file.h>
2157#include <linux/init.h>
2158#include <linux/kobject.h>
2159static struct dentry *pm_genpd_debugfs_dir;
2160
2161/*
2162 * TODO: This function is a slightly modified version of rtpm_status_show
2163 * from sysfs.c, so generalize it.
2164 */
2165static void rtpm_status_str(struct seq_file *s, struct device *dev)
2166{
2167 static const char * const status_lookup[] = {
2168 [RPM_ACTIVE] = "active",
2169 [RPM_RESUMING] = "resuming",
2170 [RPM_SUSPENDED] = "suspended",
2171 [RPM_SUSPENDING] = "suspending"
2172 };
2173 const char *p = "";
2174
2175 if (dev->power.runtime_error)
2176 p = "error";
2177 else if (dev->power.disable_depth)
2178 p = "unsupported";
2179 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2180 p = status_lookup[dev->power.runtime_status];
2181 else
2182 WARN_ON(1);
2183
2184 seq_puts(s, p);
2185}
2186
2187static int pm_genpd_summary_one(struct seq_file *s,
2188 struct generic_pm_domain *genpd)
2189{
2190 static const char * const status_lookup[] = {
2191 [GPD_STATE_ACTIVE] = "on",
2192 [GPD_STATE_POWER_OFF] = "off"
2193 };
2194 struct pm_domain_data *pm_data;
2195 const char *kobj_path;
2196 struct gpd_link *link;
2197 char state[16];
2198 int ret;
2199
2200 ret = genpd_lock_interruptible(genpd);
2201 if (ret)
2202 return -ERESTARTSYS;
2203
2204 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2205 goto exit;
2206 if (genpd->status == GPD_STATE_POWER_OFF)
2207 snprintf(state, sizeof(state), "%s-%u",
2208 status_lookup[genpd->status], genpd->state_idx);
2209 else
2210 snprintf(state, sizeof(state), "%s",
2211 status_lookup[genpd->status]);
2212 seq_printf(s, "%-30s %-15s ", genpd->name, state);
2213
2214 /*
2215 * Modifications on the list require holding locks on both
2216 * master and slave, so we are safe.
2217 * Also genpd->name is immutable.
2218 */
2219 list_for_each_entry(link, &genpd->master_links, master_node) {
2220 seq_printf(s, "%s", link->slave->name);
2221 if (!list_is_last(&link->master_node, &genpd->master_links))
2222 seq_puts(s, ", ");
2223 }
2224
2225 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2226 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2227 genpd_is_irq_safe(genpd) ?
2228 GFP_ATOMIC : GFP_KERNEL);
2229 if (kobj_path == NULL)
2230 continue;
2231
2232 seq_printf(s, "\n %-50s ", kobj_path);
2233 rtpm_status_str(s, pm_data->dev);
2234 kfree(kobj_path);
2235 }
2236
2237 seq_puts(s, "\n");
2238exit:
2239 genpd_unlock(genpd);
2240
2241 return 0;
2242}
2243
2244static int pm_genpd_summary_show(struct seq_file *s, void *data)
2245{
2246 struct generic_pm_domain *genpd;
2247 int ret = 0;
2248
2249 seq_puts(s, "domain status slaves\n");
2250 seq_puts(s, " /device runtime status\n");
2251 seq_puts(s, "----------------------------------------------------------------------\n");
2252
2253 ret = mutex_lock_interruptible(&gpd_list_lock);
2254 if (ret)
2255 return -ERESTARTSYS;
2256
2257 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2258 ret = pm_genpd_summary_one(s, genpd);
2259 if (ret)
2260 break;
2261 }
2262 mutex_unlock(&gpd_list_lock);
2263
2264 return ret;
2265}
2266
2267static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2268{
2269 return single_open(file, pm_genpd_summary_show, NULL);
2270}
2271
2272static const struct file_operations pm_genpd_summary_fops = {
2273 .open = pm_genpd_summary_open,
2274 .read = seq_read,
2275 .llseek = seq_lseek,
2276 .release = single_release,
2277};
2278
2279static int __init pm_genpd_debug_init(void)
2280{
2281 struct dentry *d;
2282
2283 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2284
2285 if (!pm_genpd_debugfs_dir)
2286 return -ENOMEM;
2287
2288 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2289 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2290 if (!d)
2291 return -ENOMEM;
2292
2293 return 0;
2294}
2295late_initcall(pm_genpd_debug_init);
2296
2297static void __exit pm_genpd_debug_exit(void)
2298{
2299 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2300}
2301__exitcall(pm_genpd_debug_exit);
2302#endif /* CONFIG_DEBUG_FS */