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