blob: 808ba41b6580aaba919f228b648053fae465893d [file] [log] [blame]
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001/*
2 * drivers/base/power/domain.c - Common code related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02009#include <linux/delay.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020010#include <linux/kernel.h>
11#include <linux/io.h>
Tomasz Figaaa422402014-09-19 20:27:36 +020012#include <linux/platform_device.h>
Viresh Kumar6a0ae732018-04-05 15:53:34 +053013#include <linux/pm_opp.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020014#include <linux/pm_runtime.h>
15#include <linux/pm_domain.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020016#include <linux/pm_qos.h>
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010017#include <linux/pm_clock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020018#include <linux/slab.h>
19#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020020#include <linux/sched.h>
21#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010022#include <linux/export.h>
23
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +010024#include "power.h"
25
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +020026#define GENPD_RETRY_MAX_MS 250 /* Approximate */
27
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010028#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
29({ \
30 type (*__routine)(struct device *__d); \
31 type __ret = (type)0; \
32 \
33 __routine = genpd->dev_ops.callback; \
34 if (__routine) { \
35 __ret = __routine(dev); \
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010036 } \
37 __ret; \
38})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020039
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020040static LIST_HEAD(gpd_list);
41static DEFINE_MUTEX(gpd_list_lock);
42
Lina Iyer35241d12016-10-14 10:47:54 -070043struct genpd_lock_ops {
44 void (*lock)(struct generic_pm_domain *genpd);
45 void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
46 int (*lock_interruptible)(struct generic_pm_domain *genpd);
47 void (*unlock)(struct generic_pm_domain *genpd);
48};
49
50static void genpd_lock_mtx(struct generic_pm_domain *genpd)
51{
52 mutex_lock(&genpd->mlock);
53}
54
55static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
56 int depth)
57{
58 mutex_lock_nested(&genpd->mlock, depth);
59}
60
61static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
62{
63 return mutex_lock_interruptible(&genpd->mlock);
64}
65
66static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
67{
68 return mutex_unlock(&genpd->mlock);
69}
70
71static const struct genpd_lock_ops genpd_mtx_ops = {
72 .lock = genpd_lock_mtx,
73 .lock_nested = genpd_lock_nested_mtx,
74 .lock_interruptible = genpd_lock_interruptible_mtx,
75 .unlock = genpd_unlock_mtx,
76};
77
Lina Iyerd716f472016-10-14 10:47:55 -070078static void genpd_lock_spin(struct generic_pm_domain *genpd)
79 __acquires(&genpd->slock)
80{
81 unsigned long flags;
82
83 spin_lock_irqsave(&genpd->slock, flags);
84 genpd->lock_flags = flags;
85}
86
87static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
88 int depth)
89 __acquires(&genpd->slock)
90{
91 unsigned long flags;
92
93 spin_lock_irqsave_nested(&genpd->slock, flags, depth);
94 genpd->lock_flags = flags;
95}
96
97static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
98 __acquires(&genpd->slock)
99{
100 unsigned long flags;
101
102 spin_lock_irqsave(&genpd->slock, flags);
103 genpd->lock_flags = flags;
104 return 0;
105}
106
107static void genpd_unlock_spin(struct generic_pm_domain *genpd)
108 __releases(&genpd->slock)
109{
110 spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
111}
112
113static const struct genpd_lock_ops genpd_spin_ops = {
114 .lock = genpd_lock_spin,
115 .lock_nested = genpd_lock_nested_spin,
116 .lock_interruptible = genpd_lock_interruptible_spin,
117 .unlock = genpd_unlock_spin,
118};
119
Lina Iyer35241d12016-10-14 10:47:54 -0700120#define genpd_lock(p) p->lock_ops->lock(p)
121#define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d)
122#define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p)
123#define genpd_unlock(p) p->lock_ops->unlock(p)
124
Ulf Hansson41e2c8e2017-03-20 11:19:20 +0100125#define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE)
Lina Iyerd716f472016-10-14 10:47:55 -0700126#define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE)
Ulf Hanssonffaa42e2017-03-20 11:19:21 +0100127#define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON)
Geert Uytterhoeven95a20ef2017-11-07 13:48:11 +0100128#define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
Lina Iyerd716f472016-10-14 10:47:55 -0700129
130static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
Krzysztof Kozlowskid8600c82017-06-12 17:17:41 +0200131 const struct generic_pm_domain *genpd)
Lina Iyerd716f472016-10-14 10:47:55 -0700132{
133 bool ret;
134
135 ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
136
Ulf Hansson075c37d2017-03-20 11:19:23 +0100137 /*
138 * Warn once if an IRQ safe device is attached to a no sleep domain, as
139 * to indicate a suboptimal configuration for PM. For an always on
140 * domain this isn't case, thus don't warn.
141 */
142 if (ret && !genpd_is_always_on(genpd))
Lina Iyerd716f472016-10-14 10:47:55 -0700143 dev_warn_once(dev, "PM domain %s will not be powered off\n",
144 genpd->name);
145
146 return ret;
147}
148
Russell King446d999c2015-03-20 17:20:33 +0000149/*
150 * Get the generic PM domain for a particular struct device.
151 * This validates the struct device pointer, the PM domain pointer,
152 * and checks that the PM domain pointer is a real generic PM domain.
153 * Any failure results in NULL being returned.
154 */
Jon Hunterf58d4e52016-09-12 12:01:08 +0100155static struct generic_pm_domain *genpd_lookup_dev(struct device *dev)
Russell King446d999c2015-03-20 17:20:33 +0000156{
157 struct generic_pm_domain *genpd = NULL, *gpd;
158
159 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
160 return NULL;
161
162 mutex_lock(&gpd_list_lock);
163 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
164 if (&gpd->domain == dev->pm_domain) {
165 genpd = gpd;
166 break;
167 }
168 }
169 mutex_unlock(&gpd_list_lock);
170
171 return genpd;
172}
173
174/*
175 * This should only be used where we are certain that the pm_domain
176 * attached to the device is a genpd domain.
177 */
178static struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200179{
180 if (IS_ERR_OR_NULL(dev->pm_domain))
181 return ERR_PTR(-EINVAL);
182
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200183 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200184}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200185
Krzysztof Kozlowskid8600c82017-06-12 17:17:41 +0200186static int genpd_stop_dev(const struct generic_pm_domain *genpd,
187 struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100188{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200189 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100190}
191
Krzysztof Kozlowskid8600c82017-06-12 17:17:41 +0200192static int genpd_start_dev(const struct generic_pm_domain *genpd,
193 struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100194{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200195 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100196}
197
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200198static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200199{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200200 bool ret = false;
201
202 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
203 ret = !!atomic_dec_and_test(&genpd->sd_count);
204
205 return ret;
206}
207
208static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
209{
210 atomic_inc(&genpd->sd_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100211 smp_mb__after_atomic();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200212}
213
Thara Gopinathafece3a2017-07-14 13:10:15 -0400214#ifdef CONFIG_DEBUG_FS
215static void genpd_update_accounting(struct generic_pm_domain *genpd)
216{
217 ktime_t delta, now;
218
219 now = ktime_get();
220 delta = ktime_sub(now, genpd->accounting_time);
221
222 /*
223 * If genpd->status is active, it means we are just
224 * out of off and so update the idle time and vice
225 * versa.
226 */
227 if (genpd->status == GPD_STATE_ACTIVE) {
228 int state_idx = genpd->state_idx;
229
230 genpd->states[state_idx].idle_time =
231 ktime_add(genpd->states[state_idx].idle_time, delta);
232 } else {
233 genpd->on_time = ktime_add(genpd->on_time, delta);
234 }
235
236 genpd->accounting_time = now;
237}
238#else
239static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
240#endif
241
Viresh Kumarcd50c6d2018-10-31 14:56:54 +0530242static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
243 unsigned int state)
244{
245 struct generic_pm_domain_data *pd_data;
246 struct pm_domain_data *pdd;
247
248 /* New requested state is same as Max requested state */
249 if (state == genpd->performance_state)
250 return state;
251
252 /* New requested state is higher than Max requested state */
253 if (state > genpd->performance_state)
254 return state;
255
256 /* Traverse all devices within the domain */
257 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
258 pd_data = to_gpd_data(pdd);
259
260 if (pd_data->performance_state > state)
261 state = pd_data->performance_state;
262 }
263
264 /*
265 * We aren't propagating performance state changes of a subdomain to its
266 * masters as we don't have hardware that needs it. Over that, the
267 * performance states of subdomain and its masters may not have
268 * one-to-one mapping and would require additional information. We can
269 * get back to this once we have hardware that needs it. For that
270 * reason, we don't have to consider performance state of the subdomains
271 * of genpd here.
272 */
273 return state;
274}
275
276static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
277 unsigned int state)
278{
279 int ret;
280
281 if (state == genpd->performance_state)
282 return 0;
283
284 ret = genpd->set_performance_state(genpd, state);
285 if (ret)
286 return ret;
287
288 genpd->performance_state = state;
289 return 0;
290}
291
Viresh Kumar42f62842017-10-12 15:07:23 +0530292/**
293 * dev_pm_genpd_set_performance_state- Set performance state of device's power
294 * domain.
295 *
296 * @dev: Device for which the performance-state needs to be set.
297 * @state: Target performance state of the device. This can be set as 0 when the
298 * device doesn't have any performance state constraints left (And so
299 * the device wouldn't participate anymore to find the target
300 * performance state of the genpd).
301 *
302 * It is assumed that the users guarantee that the genpd wouldn't be detached
303 * while this routine is getting called.
304 *
305 * Returns 0 on success and negative error values on failures.
306 */
307int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
308{
309 struct generic_pm_domain *genpd;
Viresh Kumarcd50c6d2018-10-31 14:56:54 +0530310 struct generic_pm_domain_data *gpd_data;
Viresh Kumar42f62842017-10-12 15:07:23 +0530311 unsigned int prev;
Viresh Kumarcd50c6d2018-10-31 14:56:54 +0530312 int ret;
Viresh Kumar42f62842017-10-12 15:07:23 +0530313
314 genpd = dev_to_genpd(dev);
315 if (IS_ERR(genpd))
316 return -ENODEV;
317
318 if (unlikely(!genpd->set_performance_state))
319 return -EINVAL;
320
321 if (unlikely(!dev->power.subsys_data ||
322 !dev->power.subsys_data->domain_data)) {
323 WARN_ON(1);
324 return -EINVAL;
325 }
326
327 genpd_lock(genpd);
328
329 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
330 prev = gpd_data->performance_state;
331 gpd_data->performance_state = state;
332
Viresh Kumarcd50c6d2018-10-31 14:56:54 +0530333 state = _genpd_reeval_performance_state(genpd, state);
334 ret = _genpd_set_performance_state(genpd, state);
335 if (ret)
Ulf Hansson68de2fe2018-12-11 11:04:55 +0100336 gpd_data->performance_state = prev;
Viresh Kumar42f62842017-10-12 15:07:23 +0530337
Viresh Kumar42f62842017-10-12 15:07:23 +0530338 genpd_unlock(genpd);
339
340 return ret;
341}
342EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
343
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100344static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100345{
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100346 unsigned int state_idx = genpd->state_idx;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100347 ktime_t time_start;
348 s64 elapsed_ns;
349 int ret;
350
351 if (!genpd->power_on)
352 return 0;
353
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200354 if (!timed)
355 return genpd->power_on(genpd);
356
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100357 time_start = ktime_get();
358 ret = genpd->power_on(genpd);
359 if (ret)
360 return ret;
361
362 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100363 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100364 return ret;
365
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100366 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100367 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000368 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
369 genpd->name, "on", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100370
371 return ret;
372}
373
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100374static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100375{
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100376 unsigned int state_idx = genpd->state_idx;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100377 ktime_t time_start;
378 s64 elapsed_ns;
379 int ret;
380
381 if (!genpd->power_off)
382 return 0;
383
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200384 if (!timed)
385 return genpd->power_off(genpd);
386
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100387 time_start = ktime_get();
388 ret = genpd->power_off(genpd);
389 if (ret == -EBUSY)
390 return ret;
391
392 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100393 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100394 return ret;
395
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100396 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100397 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000398 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
399 genpd->name, "off", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100400
401 return ret;
402}
403
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200404/**
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100405 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
Moritz Fischera3d09c72016-01-27 08:29:27 +0100406 * @genpd: PM domain to power off.
Ulf Hansson29e47e22015-09-02 10:16:13 +0200407 *
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100408 * Queue up the execution of genpd_power_off() unless it's already been done
Ulf Hansson29e47e22015-09-02 10:16:13 +0200409 * before.
410 */
411static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
412{
413 queue_work(pm_wq, &genpd->power_off_work);
414}
415
416/**
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100417 * genpd_power_off - Remove power from a given PM domain.
418 * @genpd: PM domain to power down.
Ulf Hansson3c646492017-02-17 10:55:24 +0100419 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
420 * RPM status of the releated device is in an intermediate state, not yet turned
421 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
422 * be RPM_SUSPENDED, while it tries to power off the PM domain.
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100423 *
424 * If all of the @genpd's devices have been suspended and all of its subdomains
425 * have been powered down, remove power from @genpd.
426 */
Ulf Hansson2da83542017-02-17 10:55:25 +0100427static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
428 unsigned int depth)
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100429{
430 struct pm_domain_data *pdd;
431 struct gpd_link *link;
432 unsigned int not_suspended = 0;
433
434 /*
435 * Do not try to power off the domain in the following situations:
436 * (1) The domain is already in the "power off" state.
437 * (2) System suspend is in progress.
438 */
Ulf Hansson41e2c8e2017-03-20 11:19:20 +0100439 if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100440 return 0;
441
Ulf Hanssonffaa42e2017-03-20 11:19:21 +0100442 /*
443 * Abort power off for the PM domain in the following situations:
444 * (1) The domain is configured as always on.
445 * (2) When the domain has a subdomain being powered on.
446 */
447 if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0)
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100448 return -EBUSY;
449
450 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
451 enum pm_qos_flags_status stat;
452
Rafael J. Wysocki20f97ca2017-10-13 15:27:24 +0200453 stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100454 if (stat > PM_QOS_FLAGS_NONE)
455 return -EBUSY;
456
457 /*
458 * Do not allow PM domain to be powered off, when an IRQ safe
459 * device is part of a non-IRQ safe domain.
460 */
461 if (!pm_runtime_suspended(pdd->dev) ||
462 irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
463 not_suspended++;
464 }
465
Ulf Hansson3c646492017-02-17 10:55:24 +0100466 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100467 return -EBUSY;
468
469 if (genpd->gov && genpd->gov->power_down_ok) {
470 if (!genpd->gov->power_down_ok(&genpd->domain))
471 return -EAGAIN;
472 }
473
Ulf Hansson2c9b7f82018-10-03 16:38:15 +0200474 /* Default to shallowest state. */
475 if (!genpd->gov)
476 genpd->state_idx = 0;
477
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100478 if (genpd->power_off) {
479 int ret;
480
481 if (atomic_read(&genpd->sd_count) > 0)
482 return -EBUSY;
483
484 /*
485 * If sd_count > 0 at this point, one of the subdomains hasn't
486 * managed to call genpd_power_on() for the master yet after
487 * incrementing it. In that case genpd_power_on() will wait
488 * for us to drop the lock, so we can call .power_off() and let
489 * the genpd_power_on() restore power for us (this shouldn't
490 * happen very often).
491 */
492 ret = _genpd_power_off(genpd, true);
493 if (ret)
494 return ret;
495 }
496
497 genpd->status = GPD_STATE_POWER_OFF;
Thara Gopinathafece3a2017-07-14 13:10:15 -0400498 genpd_update_accounting(genpd);
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100499
500 list_for_each_entry(link, &genpd->slave_links, slave_node) {
501 genpd_sd_counter_dec(link->master);
Ulf Hansson2da83542017-02-17 10:55:25 +0100502 genpd_lock_nested(link->master, depth + 1);
503 genpd_power_off(link->master, false, depth + 1);
504 genpd_unlock(link->master);
Ulf Hansson1f8728b2017-02-17 10:55:23 +0100505 }
506
507 return 0;
508}
509
510/**
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100511 * genpd_power_on - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200512 * @genpd: PM domain to power up.
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100513 * @depth: nesting count for lockdep.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200514 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200515 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200516 * resume a device belonging to it.
517 */
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100518static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200519{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200520 struct gpd_link *link;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200521 int ret = 0;
522
Ulf Hansson41e2c8e2017-03-20 11:19:20 +0100523 if (genpd_status_on(genpd))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200524 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200525
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200526 /*
527 * The list is guaranteed not to change while the loop below is being
528 * executed, unless one of the masters' .power_on() callbacks fiddles
529 * with it.
530 */
531 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100532 struct generic_pm_domain *master = link->master;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200533
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100534 genpd_sd_counter_inc(master);
535
Lina Iyer35241d12016-10-14 10:47:54 -0700536 genpd_lock_nested(master, depth + 1);
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100537 ret = genpd_power_on(master, depth + 1);
Lina Iyer35241d12016-10-14 10:47:54 -0700538 genpd_unlock(master);
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100539
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200540 if (ret) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100541 genpd_sd_counter_dec(master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200542 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200543 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200544 }
545
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100546 ret = _genpd_power_on(genpd, true);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100547 if (ret)
548 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200549
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200550 genpd->status = GPD_STATE_ACTIVE;
Thara Gopinathafece3a2017-07-14 13:10:15 -0400551 genpd_update_accounting(genpd);
552
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200553 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200554
555 err:
Ulf Hansson29e47e22015-09-02 10:16:13 +0200556 list_for_each_entry_continue_reverse(link,
557 &genpd->slave_links,
558 slave_node) {
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200559 genpd_sd_counter_dec(link->master);
Ulf Hansson2da83542017-02-17 10:55:25 +0100560 genpd_lock_nested(link->master, depth + 1);
561 genpd_power_off(link->master, false, depth + 1);
562 genpd_unlock(link->master);
Ulf Hansson29e47e22015-09-02 10:16:13 +0200563 }
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200564
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200565 return ret;
566}
567
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200568static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
569 unsigned long val, void *ptr)
570{
571 struct generic_pm_domain_data *gpd_data;
572 struct device *dev;
573
574 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200575 dev = gpd_data->base.dev;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200576
577 for (;;) {
578 struct generic_pm_domain *genpd;
579 struct pm_domain_data *pdd;
580
581 spin_lock_irq(&dev->power.lock);
582
583 pdd = dev->power.subsys_data ?
584 dev->power.subsys_data->domain_data : NULL;
Viresh Kumarb4883ca2017-05-16 10:52:43 +0530585 if (pdd) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200586 to_gpd_data(pdd)->td.constraint_changed = true;
587 genpd = dev_to_genpd(dev);
588 } else {
589 genpd = ERR_PTR(-ENODATA);
590 }
591
592 spin_unlock_irq(&dev->power.lock);
593
594 if (!IS_ERR(genpd)) {
Lina Iyer35241d12016-10-14 10:47:54 -0700595 genpd_lock(genpd);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200596 genpd->max_off_time_changed = true;
Lina Iyer35241d12016-10-14 10:47:54 -0700597 genpd_unlock(genpd);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200598 }
599
600 dev = dev->parent;
601 if (!dev || dev->power.ignore_children)
602 break;
603 }
604
605 return NOTIFY_DONE;
606}
607
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200608/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200609 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
610 * @work: Work structure used for scheduling the execution of this function.
611 */
612static void genpd_power_off_work_fn(struct work_struct *work)
613{
614 struct generic_pm_domain *genpd;
615
616 genpd = container_of(work, struct generic_pm_domain, power_off_work);
617
Lina Iyer35241d12016-10-14 10:47:54 -0700618 genpd_lock(genpd);
Ulf Hansson2da83542017-02-17 10:55:25 +0100619 genpd_power_off(genpd, false, 0);
Lina Iyer35241d12016-10-14 10:47:54 -0700620 genpd_unlock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200621}
622
623/**
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200624 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
625 * @dev: Device to handle.
626 */
627static int __genpd_runtime_suspend(struct device *dev)
628{
629 int (*cb)(struct device *__dev);
630
631 if (dev->type && dev->type->pm)
632 cb = dev->type->pm->runtime_suspend;
633 else if (dev->class && dev->class->pm)
634 cb = dev->class->pm->runtime_suspend;
635 else if (dev->bus && dev->bus->pm)
636 cb = dev->bus->pm->runtime_suspend;
637 else
638 cb = NULL;
639
640 if (!cb && dev->driver && dev->driver->pm)
641 cb = dev->driver->pm->runtime_suspend;
642
643 return cb ? cb(dev) : 0;
644}
645
646/**
647 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
648 * @dev: Device to handle.
649 */
650static int __genpd_runtime_resume(struct device *dev)
651{
652 int (*cb)(struct device *__dev);
653
654 if (dev->type && dev->type->pm)
655 cb = dev->type->pm->runtime_resume;
656 else if (dev->class && dev->class->pm)
657 cb = dev->class->pm->runtime_resume;
658 else if (dev->bus && dev->bus->pm)
659 cb = dev->bus->pm->runtime_resume;
660 else
661 cb = NULL;
662
663 if (!cb && dev->driver && dev->driver->pm)
664 cb = dev->driver->pm->runtime_resume;
665
666 return cb ? cb(dev) : 0;
667}
668
669/**
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200670 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200671 * @dev: Device to suspend.
672 *
673 * Carry out a runtime suspend of a device under the assumption that its
674 * pm_domain field points to the domain member of an object of type
675 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
676 */
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200677static int genpd_runtime_suspend(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200678{
679 struct generic_pm_domain *genpd;
Ulf Hansson9df39212016-03-31 11:21:25 +0200680 bool (*suspend_ok)(struct device *__dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200681 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100682 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200683 ktime_t time_start;
684 s64 elapsed_ns;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100685 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200686
687 dev_dbg(dev, "%s()\n", __func__);
688
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200689 genpd = dev_to_genpd(dev);
690 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200691 return -EINVAL;
692
Ulf Hanssonffe12852015-11-30 16:21:38 +0100693 /*
694 * A runtime PM centric subsystem/driver may re-use the runtime PM
695 * callbacks for other purposes than runtime PM. In those scenarios
696 * runtime PM is disabled. Under these circumstances, we shall skip
697 * validating/measuring the PM QoS latency.
698 */
Ulf Hansson9df39212016-03-31 11:21:25 +0200699 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
700 if (runtime_pm && suspend_ok && !suspend_ok(dev))
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100701 return -EBUSY;
702
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200703 /* Measure suspend latency. */
Linus Torvaldsd33d5a6c2016-12-25 14:56:58 -0800704 time_start = 0;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100705 if (runtime_pm)
706 time_start = ktime_get();
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200707
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200708 ret = __genpd_runtime_suspend(dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100709 if (ret)
710 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200711
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200712 ret = genpd_stop_dev(genpd, dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200713 if (ret) {
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200714 __genpd_runtime_resume(dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200715 return ret;
716 }
717
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200718 /* Update suspend latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100719 if (runtime_pm) {
720 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
721 if (elapsed_ns > td->suspend_latency_ns) {
722 td->suspend_latency_ns = elapsed_ns;
723 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
724 elapsed_ns);
725 genpd->max_off_time_changed = true;
726 td->constraint_changed = true;
727 }
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200728 }
729
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200730 /*
Lina Iyerd716f472016-10-14 10:47:55 -0700731 * If power.irq_safe is set, this routine may be run with
732 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200733 */
Lina Iyerd716f472016-10-14 10:47:55 -0700734 if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200735 return 0;
736
Lina Iyer35241d12016-10-14 10:47:54 -0700737 genpd_lock(genpd);
Ulf Hansson2da83542017-02-17 10:55:25 +0100738 genpd_power_off(genpd, true, 0);
Lina Iyer35241d12016-10-14 10:47:54 -0700739 genpd_unlock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200740
741 return 0;
742}
743
744/**
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200745 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200746 * @dev: Device to resume.
747 *
748 * Carry out a runtime resume of a device under the assumption that its
749 * pm_domain field points to the domain member of an object of type
750 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
751 */
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200752static int genpd_runtime_resume(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200753{
754 struct generic_pm_domain *genpd;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200755 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100756 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200757 ktime_t time_start;
758 s64 elapsed_ns;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200759 int ret;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200760 bool timed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200761
762 dev_dbg(dev, "%s()\n", __func__);
763
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200764 genpd = dev_to_genpd(dev);
765 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200766 return -EINVAL;
767
Lina Iyerd716f472016-10-14 10:47:55 -0700768 /*
769 * As we don't power off a non IRQ safe domain, which holds
770 * an IRQ safe device, we don't need to restore power to it.
771 */
772 if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200773 timed = false;
774 goto out;
775 }
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200776
Lina Iyer35241d12016-10-14 10:47:54 -0700777 genpd_lock(genpd);
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100778 ret = genpd_power_on(genpd, 0);
Lina Iyer35241d12016-10-14 10:47:54 -0700779 genpd_unlock(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200780
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200781 if (ret)
782 return ret;
783
784 out:
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200785 /* Measure resume latency. */
Augusto Mecking Caringiab51e6b2016-12-30 11:34:08 +0000786 time_start = 0;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100787 if (timed && runtime_pm)
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200788 time_start = ktime_get();
789
Laurent Pinchart076395c2016-03-02 01:20:38 +0200790 ret = genpd_start_dev(genpd, dev);
791 if (ret)
792 goto err_poweroff;
793
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200794 ret = __genpd_runtime_resume(dev);
Laurent Pinchart076395c2016-03-02 01:20:38 +0200795 if (ret)
796 goto err_stop;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200797
798 /* Update resume latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100799 if (timed && runtime_pm) {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200800 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
801 if (elapsed_ns > td->resume_latency_ns) {
802 td->resume_latency_ns = elapsed_ns;
803 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
804 elapsed_ns);
805 genpd->max_off_time_changed = true;
806 td->constraint_changed = true;
807 }
808 }
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200809
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200810 return 0;
Laurent Pinchart076395c2016-03-02 01:20:38 +0200811
812err_stop:
813 genpd_stop_dev(genpd, dev);
814err_poweroff:
Lina Iyerd716f472016-10-14 10:47:55 -0700815 if (!pm_runtime_is_irq_safe(dev) ||
816 (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
Lina Iyer35241d12016-10-14 10:47:54 -0700817 genpd_lock(genpd);
Ulf Hansson2da83542017-02-17 10:55:25 +0100818 genpd_power_off(genpd, true, 0);
Lina Iyer35241d12016-10-14 10:47:54 -0700819 genpd_unlock(genpd);
Laurent Pinchart076395c2016-03-02 01:20:38 +0200820 }
821
822 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200823}
824
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530825static bool pd_ignore_unused;
826static int __init pd_ignore_unused_setup(char *__unused)
827{
828 pd_ignore_unused = true;
829 return 1;
830}
831__setup("pd_ignore_unused", pd_ignore_unused_setup);
832
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200833/**
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100834 * genpd_power_off_unused - Power off all PM domains with no devices in use.
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200835 */
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100836static int __init genpd_power_off_unused(void)
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200837{
838 struct generic_pm_domain *genpd;
839
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530840 if (pd_ignore_unused) {
841 pr_warn("genpd: Not disabling unused power domains\n");
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200842 return 0;
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530843 }
844
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200845 mutex_lock(&gpd_list_lock);
846
847 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
848 genpd_queue_power_off_work(genpd);
849
850 mutex_unlock(&gpd_list_lock);
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200851
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200852 return 0;
853}
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100854late_initcall(genpd_power_off_unused);
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200855
Jon Hunter0159ec62016-09-12 12:01:10 +0100856#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200857
Ulf Hansson9e9704e2017-10-06 09:02:06 +0200858static bool genpd_present(const struct generic_pm_domain *genpd)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200859{
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100860 const struct generic_pm_domain *gpd;
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200861
862 if (IS_ERR_OR_NULL(genpd))
863 return false;
864
865 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
866 if (gpd == genpd)
867 return true;
868
869 return false;
870}
871
Jon Hunter0159ec62016-09-12 12:01:10 +0100872#endif
873
874#ifdef CONFIG_PM_SLEEP
875
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200876/**
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100877 * genpd_sync_power_off - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200878 * @genpd: PM domain to power off, if possible.
Ulf Hansson0883ac02017-02-08 13:39:00 +0100879 * @use_lock: use the lock.
880 * @depth: nesting count for lockdep.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200881 *
882 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200883 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200884 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200885 * This function is only called in "noirq" and "syscore" stages of system power
Ulf Hansson0883ac02017-02-08 13:39:00 +0100886 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
887 * these cases the lock must be held.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200888 */
Ulf Hansson0883ac02017-02-08 13:39:00 +0100889static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
890 unsigned int depth)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200891{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200892 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200893
Ulf Hanssonffaa42e2017-03-20 11:19:21 +0100894 if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200895 return;
896
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200897 if (genpd->suspended_count != genpd->device_count
898 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200899 return;
900
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100901 /* Choose the deepest state when suspending */
902 genpd->state_idx = genpd->state_count - 1;
Ulf Hansson1c14967c2017-03-20 11:19:22 +0100903 if (_genpd_power_off(genpd, false))
904 return;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200905
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200906 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200907
908 list_for_each_entry(link, &genpd->slave_links, slave_node) {
909 genpd_sd_counter_dec(link->master);
Ulf Hansson0883ac02017-02-08 13:39:00 +0100910
911 if (use_lock)
912 genpd_lock_nested(link->master, depth + 1);
913
914 genpd_sync_power_off(link->master, use_lock, depth + 1);
915
916 if (use_lock)
917 genpd_unlock(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200918 }
919}
920
921/**
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100922 * genpd_sync_power_on - Synchronously power on a PM domain and its masters.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200923 * @genpd: PM domain to power on.
Ulf Hansson0883ac02017-02-08 13:39:00 +0100924 * @use_lock: use the lock.
925 * @depth: nesting count for lockdep.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200926 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200927 * This function is only called in "noirq" and "syscore" stages of system power
Ulf Hansson0883ac02017-02-08 13:39:00 +0100928 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
929 * these cases the lock must be held.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200930 */
Ulf Hansson0883ac02017-02-08 13:39:00 +0100931static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
932 unsigned int depth)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200933{
934 struct gpd_link *link;
935
Ulf Hansson41e2c8e2017-03-20 11:19:20 +0100936 if (genpd_status_on(genpd))
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200937 return;
938
939 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200940 genpd_sd_counter_inc(link->master);
Ulf Hansson0883ac02017-02-08 13:39:00 +0100941
942 if (use_lock)
943 genpd_lock_nested(link->master, depth + 1);
944
945 genpd_sync_power_on(link->master, use_lock, depth + 1);
946
947 if (use_lock)
948 genpd_unlock(link->master);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200949 }
950
Ulf Hansson86e12ea2016-12-08 14:45:20 +0100951 _genpd_power_on(genpd, false);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200952
953 genpd->status = GPD_STATE_ACTIVE;
954}
955
956/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200957 * resume_needed - Check whether to resume a device before system suspend.
958 * @dev: Device to check.
959 * @genpd: PM domain the device belongs to.
960 *
961 * There are two cases in which a device that can wake up the system from sleep
Ulf Hansson9e9704e2017-10-06 09:02:06 +0200962 * states should be resumed by genpd_prepare(): (1) if the device is enabled
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200963 * to wake up the system and it has to remain active for this purpose while the
964 * system is in the sleep state and (2) if the device is not enabled to wake up
965 * the system from sleep states and it generally doesn't generate wakeup signals
966 * by itself (those signals are generated on its behalf by other parts of the
967 * system). In the latter case it may be necessary to reconfigure the device's
968 * wakeup settings during system suspend, because it may have been set up to
969 * signal remote wakeup from the system's working state as needed by runtime PM.
970 * Return 'true' in either of the above cases.
971 */
Krzysztof Kozlowskid8600c82017-06-12 17:17:41 +0200972static bool resume_needed(struct device *dev,
973 const struct generic_pm_domain *genpd)
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200974{
975 bool active_wakeup;
976
977 if (!device_can_wakeup(dev))
978 return false;
979
Geert Uytterhoevend0af45f2017-11-07 13:48:15 +0100980 active_wakeup = genpd_is_active_wakeup(genpd);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200981 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
982}
983
984/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +0200985 * genpd_prepare - Start power transition of a device in a PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200986 * @dev: Device to start the transition of.
987 *
988 * Start a power transition of a device (during a system-wide power transition)
989 * under the assumption that its pm_domain field points to the domain member of
990 * an object of type struct generic_pm_domain representing a PM domain
991 * consisting of I/O devices.
992 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +0200993static int genpd_prepare(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200994{
995 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200996 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200997
998 dev_dbg(dev, "%s()\n", __func__);
999
1000 genpd = dev_to_genpd(dev);
1001 if (IS_ERR(genpd))
1002 return -EINVAL;
1003
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001004 /*
1005 * If a wakeup request is pending for the device, it should be woken up
1006 * at this point and a system wakeup event should be reported if it's
1007 * set up to wake up the system from sleep states.
1008 */
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +02001009 if (resume_needed(dev, genpd))
1010 pm_runtime_resume(dev);
1011
Lina Iyer35241d12016-10-14 10:47:54 -07001012 genpd_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001013
Ulf Hansson39dd0f22016-05-30 11:43:07 +02001014 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001015 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001016
Lina Iyer35241d12016-10-14 10:47:54 -07001017 genpd_unlock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001018
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +02001019 ret = pm_generic_prepare(dev);
Ulf Hansson5241ab402017-11-08 10:11:02 +01001020 if (ret < 0) {
Lina Iyer35241d12016-10-14 10:47:54 -07001021 genpd_lock(genpd);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +02001022
Ulf Hansson39dd0f22016-05-30 11:43:07 +02001023 genpd->prepared_count--;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +02001024
Lina Iyer35241d12016-10-14 10:47:54 -07001025 genpd_unlock(genpd);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +02001026 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001027
Ulf Hansson5241ab402017-11-08 10:11:02 +01001028 /* Never return 1, as genpd don't cope with the direct_complete path. */
1029 return ret >= 0 ? 0 : ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001030}
1031
1032/**
Mikko Perttunen10da6542017-06-22 10:18:33 +03001033 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1034 * I/O pm domain.
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001035 * @dev: Device to suspend.
Mikko Perttunen10da6542017-06-22 10:18:33 +03001036 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001037 *
1038 * Stop the device and remove power from the domain if all devices in it have
1039 * been stopped.
1040 */
Mikko Perttunen10da6542017-06-22 10:18:33 +03001041static int genpd_finish_suspend(struct device *dev, bool poweroff)
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001042{
1043 struct generic_pm_domain *genpd;
Ulf Hanssona9354242018-01-10 21:31:56 +01001044 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001045
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001046 genpd = dev_to_genpd(dev);
1047 if (IS_ERR(genpd))
1048 return -EINVAL;
1049
Mikko Perttunen10da6542017-06-22 10:18:33 +03001050 if (poweroff)
1051 ret = pm_generic_poweroff_noirq(dev);
1052 else
1053 ret = pm_generic_suspend_noirq(dev);
1054 if (ret)
1055 return ret;
1056
Ulf Hanssona9354242018-01-10 21:31:56 +01001057 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1058 return 0;
1059
Rafael J. Wysocki17218e02018-01-12 14:10:38 +01001060 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1061 !pm_runtime_status_suspended(dev)) {
1062 ret = genpd_stop_dev(genpd, dev);
Ulf Hanssona9354242018-01-10 21:31:56 +01001063 if (ret) {
1064 if (poweroff)
1065 pm_generic_restore_noirq(dev);
1066 else
1067 pm_generic_resume_noirq(dev);
Ulf Hansson122a2232016-05-30 11:33:14 +02001068 return ret;
Ulf Hanssona9354242018-01-10 21:31:56 +01001069 }
Ulf Hansson122a2232016-05-30 11:33:14 +02001070 }
1071
Ulf Hansson0883ac02017-02-08 13:39:00 +01001072 genpd_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001073 genpd->suspended_count++;
Ulf Hansson0883ac02017-02-08 13:39:00 +01001074 genpd_sync_power_off(genpd, true, 0);
1075 genpd_unlock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001076
1077 return 0;
1078}
1079
1080/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001081 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
Mikko Perttunen10da6542017-06-22 10:18:33 +03001082 * @dev: Device to suspend.
1083 *
1084 * Stop the device and remove power from the domain if all devices in it have
1085 * been stopped.
1086 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001087static int genpd_suspend_noirq(struct device *dev)
Mikko Perttunen10da6542017-06-22 10:18:33 +03001088{
1089 dev_dbg(dev, "%s()\n", __func__);
1090
1091 return genpd_finish_suspend(dev, false);
1092}
1093
1094/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001095 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001096 * @dev: Device to resume.
1097 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001098 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001099 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001100static int genpd_resume_noirq(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001101{
1102 struct generic_pm_domain *genpd;
Ulf Hanssona9354242018-01-10 21:31:56 +01001103 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001104
1105 dev_dbg(dev, "%s()\n", __func__);
1106
1107 genpd = dev_to_genpd(dev);
1108 if (IS_ERR(genpd))
1109 return -EINVAL;
1110
Geert Uytterhoevend0af45f2017-11-07 13:48:15 +01001111 if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
Ulf Hanssona9354242018-01-10 21:31:56 +01001112 return pm_generic_resume_noirq(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001113
Ulf Hansson0883ac02017-02-08 13:39:00 +01001114 genpd_lock(genpd);
1115 genpd_sync_power_on(genpd, true, 0);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001116 genpd->suspended_count--;
Ulf Hansson0883ac02017-02-08 13:39:00 +01001117 genpd_unlock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001118
Rafael J. Wysocki17218e02018-01-12 14:10:38 +01001119 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1120 !pm_runtime_status_suspended(dev)) {
1121 ret = genpd_start_dev(genpd, dev);
Ulf Hanssona9354242018-01-10 21:31:56 +01001122 if (ret)
1123 return ret;
1124 }
Ulf Hansson122a2232016-05-30 11:33:14 +02001125
Ulf Hanssona9354242018-01-10 21:31:56 +01001126 return pm_generic_resume_noirq(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001127}
1128
1129/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001130 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001131 * @dev: Device to freeze.
1132 *
1133 * Carry out a late freeze of a device under the assumption that its
1134 * pm_domain field points to the domain member of an object of type
1135 * struct generic_pm_domain representing a power domain consisting of I/O
1136 * devices.
1137 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001138static int genpd_freeze_noirq(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001139{
Krzysztof Kozlowskid8600c82017-06-12 17:17:41 +02001140 const struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +02001141 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001142
1143 dev_dbg(dev, "%s()\n", __func__);
1144
1145 genpd = dev_to_genpd(dev);
1146 if (IS_ERR(genpd))
1147 return -EINVAL;
1148
Mikko Perttunen10da6542017-06-22 10:18:33 +03001149 ret = pm_generic_freeze_noirq(dev);
1150 if (ret)
1151 return ret;
1152
Rafael J. Wysocki17218e02018-01-12 14:10:38 +01001153 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1154 !pm_runtime_status_suspended(dev))
1155 ret = genpd_stop_dev(genpd, dev);
Ulf Hansson122a2232016-05-30 11:33:14 +02001156
1157 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001158}
1159
1160/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001161 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001162 * @dev: Device to thaw.
1163 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001164 * Start the device, unless power has been removed from the domain already
1165 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001166 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001167static int genpd_thaw_noirq(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001168{
Krzysztof Kozlowskid8600c82017-06-12 17:17:41 +02001169 const struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +02001170 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001171
1172 dev_dbg(dev, "%s()\n", __func__);
1173
1174 genpd = dev_to_genpd(dev);
1175 if (IS_ERR(genpd))
1176 return -EINVAL;
1177
Rafael J. Wysocki17218e02018-01-12 14:10:38 +01001178 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1179 !pm_runtime_status_suspended(dev)) {
1180 ret = genpd_start_dev(genpd, dev);
Mikko Perttunen10da6542017-06-22 10:18:33 +03001181 if (ret)
1182 return ret;
1183 }
Ulf Hansson122a2232016-05-30 11:33:14 +02001184
Mikko Perttunen10da6542017-06-22 10:18:33 +03001185 return pm_generic_thaw_noirq(dev);
1186}
1187
1188/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001189 * genpd_poweroff_noirq - Completion of hibernation of device in an
Mikko Perttunen10da6542017-06-22 10:18:33 +03001190 * I/O PM domain.
1191 * @dev: Device to poweroff.
1192 *
1193 * Stop the device and remove power from the domain if all devices in it have
1194 * been stopped.
1195 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001196static int genpd_poweroff_noirq(struct device *dev)
Mikko Perttunen10da6542017-06-22 10:18:33 +03001197{
1198 dev_dbg(dev, "%s()\n", __func__);
1199
1200 return genpd_finish_suspend(dev, true);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001201}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001202
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001203/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001204 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001205 * @dev: Device to resume.
1206 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001207 * Make sure the domain will be in the same power state as before the
1208 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001209 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001210static int genpd_restore_noirq(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001211{
1212 struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +02001213 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001214
1215 dev_dbg(dev, "%s()\n", __func__);
1216
1217 genpd = dev_to_genpd(dev);
1218 if (IS_ERR(genpd))
1219 return -EINVAL;
1220
1221 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001222 * At this point suspended_count == 0 means we are being run for the
1223 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001224 */
Ulf Hansson0883ac02017-02-08 13:39:00 +01001225 genpd_lock(genpd);
Ulf Hansson39dd0f22016-05-30 11:43:07 +02001226 if (genpd->suspended_count++ == 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001227 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001228 * The boot kernel might put the domain into arbitrary state,
Ulf Hansson86e12ea2016-12-08 14:45:20 +01001229 * so make it appear as powered off to genpd_sync_power_on(),
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001230 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001231 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001232 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001233
Ulf Hansson0883ac02017-02-08 13:39:00 +01001234 genpd_sync_power_on(genpd, true, 0);
1235 genpd_unlock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001236
Rafael J. Wysocki17218e02018-01-12 14:10:38 +01001237 if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1238 !pm_runtime_status_suspended(dev)) {
1239 ret = genpd_start_dev(genpd, dev);
Mikko Perttunen10da6542017-06-22 10:18:33 +03001240 if (ret)
1241 return ret;
1242 }
Ulf Hansson122a2232016-05-30 11:33:14 +02001243
Mikko Perttunen10da6542017-06-22 10:18:33 +03001244 return pm_generic_restore_noirq(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001245}
1246
1247/**
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001248 * genpd_complete - Complete power transition of a device in a power domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001249 * @dev: Device to complete the transition of.
1250 *
1251 * Complete a power transition of a device (during a system-wide power
1252 * transition) under the assumption that its pm_domain field points to the
1253 * domain member of an object of type struct generic_pm_domain representing
1254 * a power domain consisting of I/O devices.
1255 */
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001256static void genpd_complete(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001257{
1258 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001259
1260 dev_dbg(dev, "%s()\n", __func__);
1261
1262 genpd = dev_to_genpd(dev);
1263 if (IS_ERR(genpd))
1264 return;
1265
Ulf Hansson4d23a5e2016-05-30 11:33:13 +02001266 pm_generic_complete(dev);
1267
Lina Iyer35241d12016-10-14 10:47:54 -07001268 genpd_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001269
Ulf Hansson39dd0f22016-05-30 11:43:07 +02001270 genpd->prepared_count--;
Ulf Hansson4d23a5e2016-05-30 11:33:13 +02001271 if (!genpd->prepared_count)
1272 genpd_queue_power_off_work(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001273
Lina Iyer35241d12016-10-14 10:47:54 -07001274 genpd_unlock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001275}
1276
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001277/**
Ulf Hanssond47e6462014-09-03 12:52:24 +02001278 * genpd_syscore_switch - Switch power during system core suspend or resume.
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001279 * @dev: Device that normally is marked as "always on" to switch power for.
1280 *
1281 * This routine may only be called during the system core (syscore) suspend or
1282 * resume phase for devices whose "always on" flags are set.
1283 */
Ulf Hanssond47e6462014-09-03 12:52:24 +02001284static void genpd_syscore_switch(struct device *dev, bool suspend)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001285{
1286 struct generic_pm_domain *genpd;
1287
Rafael J. Wysocki23c6d2c2017-07-04 21:55:13 +02001288 genpd = dev_to_genpd(dev);
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001289 if (!genpd_present(genpd))
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001290 return;
1291
1292 if (suspend) {
1293 genpd->suspended_count++;
Ulf Hansson0883ac02017-02-08 13:39:00 +01001294 genpd_sync_power_off(genpd, false, 0);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001295 } else {
Ulf Hansson0883ac02017-02-08 13:39:00 +01001296 genpd_sync_power_on(genpd, false, 0);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001297 genpd->suspended_count--;
1298 }
1299}
Ulf Hanssond47e6462014-09-03 12:52:24 +02001300
1301void pm_genpd_syscore_poweroff(struct device *dev)
1302{
1303 genpd_syscore_switch(dev, true);
1304}
1305EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1306
1307void pm_genpd_syscore_poweron(struct device *dev)
1308{
1309 genpd_syscore_switch(dev, false);
1310}
1311EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001312
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001313#else /* !CONFIG_PM_SLEEP */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001314
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001315#define genpd_prepare NULL
1316#define genpd_suspend_noirq NULL
1317#define genpd_resume_noirq NULL
1318#define genpd_freeze_noirq NULL
1319#define genpd_thaw_noirq NULL
1320#define genpd_poweroff_noirq NULL
1321#define genpd_restore_noirq NULL
1322#define genpd_complete NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001323
1324#endif /* CONFIG_PM_SLEEP */
1325
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001326static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001327 struct gpd_timing_data *td)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001328{
1329 struct generic_pm_domain_data *gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001330 int ret;
1331
1332 ret = dev_pm_get_subsys_data(dev);
1333 if (ret)
1334 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001335
1336 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
Ulf Hansson3e235682015-01-27 21:13:43 +01001337 if (!gpd_data) {
1338 ret = -ENOMEM;
1339 goto err_put;
1340 }
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001341
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001342 if (td)
1343 gpd_data->td = *td;
1344
1345 gpd_data->base.dev = dev;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001346 gpd_data->td.constraint_changed = true;
Rafael J. Wysocki0759e802017-11-07 11:33:49 +01001347 gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001348 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1349
1350 spin_lock_irq(&dev->power.lock);
1351
1352 if (dev->power.subsys_data->domain_data) {
1353 ret = -EINVAL;
1354 goto err_free;
1355 }
1356
1357 dev->power.subsys_data->domain_data = &gpd_data->base;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001358
1359 spin_unlock_irq(&dev->power.lock);
1360
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001361 return gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001362
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001363 err_free:
1364 spin_unlock_irq(&dev->power.lock);
1365 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001366 err_put:
1367 dev_pm_put_subsys_data(dev);
1368 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001369}
1370
Ulf Hansson49d400c2015-01-27 21:13:38 +01001371static void genpd_free_dev_data(struct device *dev,
1372 struct generic_pm_domain_data *gpd_data)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001373{
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001374 spin_lock_irq(&dev->power.lock);
1375
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001376 dev->power.subsys_data->domain_data = NULL;
1377
1378 spin_unlock_irq(&dev->power.lock);
1379
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001380 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001381 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001382}
1383
Jon Hunter19efa5f2016-09-12 12:01:11 +01001384static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1385 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001386{
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001387 struct generic_pm_domain_data *gpd_data;
Ulf Hanssonb56d9c92018-04-26 10:53:02 +02001388 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001389
1390 dev_dbg(dev, "%s()\n", __func__);
1391
1392 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1393 return -EINVAL;
1394
Ulf Hansson96c1bf62018-05-29 12:04:16 +02001395 gpd_data = genpd_alloc_dev_data(dev, td);
Ulf Hansson3e235682015-01-27 21:13:43 +01001396 if (IS_ERR(gpd_data))
1397 return PTR_ERR(gpd_data);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001398
Lina Iyer35241d12016-10-14 10:47:54 -07001399 genpd_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001400
Ulf Hanssonb472c2f2015-01-27 21:13:45 +01001401 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1402 if (ret)
1403 goto out;
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001404
Sudeep Holla975e83c2017-07-14 11:51:48 +01001405 dev_pm_domain_set(dev, &genpd->domain);
1406
Ulf Hansson14b53062015-01-27 21:13:40 +01001407 genpd->device_count++;
1408 genpd->max_off_time_changed = true;
1409
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001410 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001411
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001412 out:
Lina Iyer35241d12016-10-14 10:47:54 -07001413 genpd_unlock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001414
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001415 if (ret)
1416 genpd_free_dev_data(dev, gpd_data);
1417 else
1418 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001419
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001420 return ret;
1421}
Jon Hunter19efa5f2016-09-12 12:01:11 +01001422
1423/**
Ulf Hansson1a7a6702018-05-29 12:04:14 +02001424 * pm_genpd_add_device - Add a device to an I/O PM domain.
Jon Hunter19efa5f2016-09-12 12:01:11 +01001425 * @genpd: PM domain to add the device to.
1426 * @dev: Device to be added.
Jon Hunter19efa5f2016-09-12 12:01:11 +01001427 */
Ulf Hansson1a7a6702018-05-29 12:04:14 +02001428int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
Jon Hunter19efa5f2016-09-12 12:01:11 +01001429{
1430 int ret;
1431
1432 mutex_lock(&gpd_list_lock);
Ulf Hansson1a7a6702018-05-29 12:04:14 +02001433 ret = genpd_add_device(genpd, dev, NULL);
Jon Hunter19efa5f2016-09-12 12:01:11 +01001434 mutex_unlock(&gpd_list_lock);
1435
1436 return ret;
1437}
Ulf Hansson1a7a6702018-05-29 12:04:14 +02001438EXPORT_SYMBOL_GPL(pm_genpd_add_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001439
Ulf Hansson85168d52016-09-21 15:38:50 +02001440static int genpd_remove_device(struct generic_pm_domain *genpd,
1441 struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001442{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001443 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001444 struct pm_domain_data *pdd;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001445 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001446
1447 dev_dbg(dev, "%s()\n", __func__);
1448
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001449 pdd = dev->power.subsys_data->domain_data;
1450 gpd_data = to_gpd_data(pdd);
1451 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1452
Lina Iyer35241d12016-10-14 10:47:54 -07001453 genpd_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001454
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001455 if (genpd->prepared_count > 0) {
1456 ret = -EAGAIN;
1457 goto out;
1458 }
1459
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001460 genpd->device_count--;
1461 genpd->max_off_time_changed = true;
1462
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001463 if (genpd->detach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001464 genpd->detach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001465
Sudeep Holla975e83c2017-07-14 11:51:48 +01001466 dev_pm_domain_set(dev, NULL);
1467
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001468 list_del_init(&pdd->list_node);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001469
Lina Iyer35241d12016-10-14 10:47:54 -07001470 genpd_unlock(genpd);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001471
Ulf Hanssonc1dbe2f2015-01-27 21:13:39 +01001472 genpd_free_dev_data(dev, gpd_data);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001473
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001474 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001475
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001476 out:
Lina Iyer35241d12016-10-14 10:47:54 -07001477 genpd_unlock(genpd);
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001478 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001479
1480 return ret;
1481}
Ulf Hansson85168d52016-09-21 15:38:50 +02001482
1483/**
1484 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
Ulf Hansson85168d52016-09-21 15:38:50 +02001485 * @dev: Device to be removed.
1486 */
Ulf Hansson924f4482018-05-29 12:04:15 +02001487int pm_genpd_remove_device(struct device *dev)
Ulf Hansson85168d52016-09-21 15:38:50 +02001488{
Ulf Hansson924f4482018-05-29 12:04:15 +02001489 struct generic_pm_domain *genpd = genpd_lookup_dev(dev);
1490
1491 if (!genpd)
Ulf Hansson85168d52016-09-21 15:38:50 +02001492 return -EINVAL;
1493
1494 return genpd_remove_device(genpd, dev);
1495}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301496EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001497
Jon Hunter19efa5f2016-09-12 12:01:11 +01001498static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1499 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001500{
Lina Iyer25479232015-10-28 15:19:50 -06001501 struct gpd_link *link, *itr;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001502 int ret = 0;
1503
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001504 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1505 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001506 return -EINVAL;
1507
Lina Iyerd716f472016-10-14 10:47:55 -07001508 /*
1509 * If the domain can be powered on/off in an IRQ safe
1510 * context, ensure that the subdomain can also be
1511 * powered on/off in that context.
1512 */
1513 if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
Dan Carpenter44cae7d2016-11-10 15:52:15 +03001514 WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
Lina Iyerd716f472016-10-14 10:47:55 -07001515 genpd->name, subdomain->name);
1516 return -EINVAL;
1517 }
1518
Lina Iyer25479232015-10-28 15:19:50 -06001519 link = kzalloc(sizeof(*link), GFP_KERNEL);
1520 if (!link)
1521 return -ENOMEM;
1522
Lina Iyer35241d12016-10-14 10:47:54 -07001523 genpd_lock(subdomain);
1524 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001525
Ulf Hansson41e2c8e2017-03-20 11:19:20 +01001526 if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001527 ret = -EINVAL;
1528 goto out;
1529 }
1530
Lina Iyer25479232015-10-28 15:19:50 -06001531 list_for_each_entry(itr, &genpd->master_links, master_node) {
1532 if (itr->slave == subdomain && itr->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001533 ret = -EINVAL;
1534 goto out;
1535 }
1536 }
1537
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001538 link->master = genpd;
1539 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001540 link->slave = subdomain;
1541 list_add_tail(&link->slave_node, &subdomain->slave_links);
Ulf Hansson41e2c8e2017-03-20 11:19:20 +01001542 if (genpd_status_on(subdomain))
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001543 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001544
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001545 out:
Lina Iyer35241d12016-10-14 10:47:54 -07001546 genpd_unlock(genpd);
1547 genpd_unlock(subdomain);
Lina Iyer25479232015-10-28 15:19:50 -06001548 if (ret)
1549 kfree(link);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001550 return ret;
1551}
Jon Hunter19efa5f2016-09-12 12:01:11 +01001552
1553/**
1554 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1555 * @genpd: Master PM domain to add the subdomain to.
1556 * @subdomain: Subdomain to be added.
1557 */
1558int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1559 struct generic_pm_domain *subdomain)
1560{
1561 int ret;
1562
1563 mutex_lock(&gpd_list_lock);
1564 ret = genpd_add_subdomain(genpd, subdomain);
1565 mutex_unlock(&gpd_list_lock);
1566
1567 return ret;
1568}
Stephen Boydd60ee962015-10-01 12:22:53 -07001569EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001570
1571/**
1572 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1573 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001574 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001575 */
1576int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001577 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001578{
Krzysztof Kozlowskic6e83ca2017-06-28 16:56:18 +02001579 struct gpd_link *l, *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001580 int ret = -EINVAL;
1581
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001582 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001583 return -EINVAL;
1584
Lina Iyer35241d12016-10-14 10:47:54 -07001585 genpd_lock(subdomain);
1586 genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001587
Jon Hunterbeda5fc2016-03-04 10:55:14 +00001588 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
Jon Hunter30e7a652015-09-03 09:10:37 +01001589 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1590 subdomain->name);
1591 ret = -EBUSY;
1592 goto out;
1593 }
1594
Krzysztof Kozlowskic6e83ca2017-06-28 16:56:18 +02001595 list_for_each_entry_safe(link, l, &genpd->master_links, master_node) {
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001596 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001597 continue;
1598
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001599 list_del(&link->master_node);
1600 list_del(&link->slave_node);
1601 kfree(link);
Ulf Hansson41e2c8e2017-03-20 11:19:20 +01001602 if (genpd_status_on(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001603 genpd_sd_counter_dec(genpd);
1604
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001605 ret = 0;
1606 break;
1607 }
1608
Jon Hunter30e7a652015-09-03 09:10:37 +01001609out:
Lina Iyer35241d12016-10-14 10:47:54 -07001610 genpd_unlock(genpd);
1611 genpd_unlock(subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001612
1613 return ret;
1614}
Stephen Boydd60ee962015-10-01 12:22:53 -07001615EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001616
Lina Iyer59d65b72016-10-14 10:47:49 -07001617static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1618{
1619 struct genpd_power_state *state;
1620
1621 state = kzalloc(sizeof(*state), GFP_KERNEL);
1622 if (!state)
1623 return -ENOMEM;
1624
1625 genpd->states = state;
1626 genpd->state_count = 1;
1627 genpd->free = state;
1628
1629 return 0;
1630}
1631
Lina Iyerd716f472016-10-14 10:47:55 -07001632static void genpd_lock_init(struct generic_pm_domain *genpd)
1633{
1634 if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1635 spin_lock_init(&genpd->slock);
1636 genpd->lock_ops = &genpd_spin_ops;
1637 } else {
1638 mutex_init(&genpd->mlock);
1639 genpd->lock_ops = &genpd_mtx_ops;
1640 }
1641}
1642
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001643/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001644 * pm_genpd_init - Initialize a generic I/O PM domain object.
1645 * @genpd: PM domain object to initialize.
1646 * @gov: PM domain governor to associate with the domain (may be NULL).
1647 * @is_off: Initial value of the domain's power_is_off field.
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001648 *
1649 * Returns 0 on successful initialization, else a negative error code.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001650 */
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001651int pm_genpd_init(struct generic_pm_domain *genpd,
1652 struct dev_power_governor *gov, bool is_off)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001653{
Lina Iyer59d65b72016-10-14 10:47:49 -07001654 int ret;
1655
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001656 if (IS_ERR_OR_NULL(genpd))
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001657 return -EINVAL;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001658
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001659 INIT_LIST_HEAD(&genpd->master_links);
1660 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001661 INIT_LIST_HEAD(&genpd->dev_list);
Lina Iyerd716f472016-10-14 10:47:55 -07001662 genpd_lock_init(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001663 genpd->gov = gov;
1664 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001665 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001666 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001667 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001668 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001669 genpd->max_off_time_changed = true;
Jon Hunterde0aa06d2016-09-12 12:01:12 +01001670 genpd->provider = NULL;
1671 genpd->has_provider = false;
Thara Gopinathafece3a2017-07-14 13:10:15 -04001672 genpd->accounting_time = ktime_get();
Ulf Hansson795bd2e2016-03-31 11:21:26 +02001673 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1674 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
Ulf Hansson9e9704e2017-10-06 09:02:06 +02001675 genpd->domain.ops.prepare = genpd_prepare;
1676 genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1677 genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1678 genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1679 genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1680 genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1681 genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1682 genpd->domain.ops.complete = genpd_complete;
Ulf Hanssonc11f6f52014-12-01 12:50:21 +01001683
1684 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1685 genpd->dev_ops.stop = pm_clk_suspend;
1686 genpd->dev_ops.start = pm_clk_resume;
1687 }
1688
Ulf Hanssonffaa42e2017-03-20 11:19:21 +01001689 /* Always-on domains must be powered on at initialization. */
1690 if (genpd_is_always_on(genpd) && !genpd_status_on(genpd))
1691 return -EINVAL;
1692
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001693 /* Use only one "off" state if there were no states declared */
Lina Iyer59d65b72016-10-14 10:47:49 -07001694 if (genpd->state_count == 0) {
1695 ret = genpd_set_default_power_state(genpd);
1696 if (ret)
1697 return ret;
Ulf Hansson2c9b7f82018-10-03 16:38:15 +02001698 } else if (!gov) {
1699 pr_warn("%s : no governor for states\n", genpd->name);
Lina Iyer59d65b72016-10-14 10:47:49 -07001700 }
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001701
Viresh Kumar401ea152017-03-17 11:26:19 +05301702 device_initialize(&genpd->dev);
1703 dev_set_name(&genpd->dev, "%s", genpd->name);
1704
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +02001705 mutex_lock(&gpd_list_lock);
1706 list_add(&genpd->gpd_list_node, &gpd_list);
1707 mutex_unlock(&gpd_list_lock);
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001708
1709 return 0;
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +02001710}
Rajendra Nayakbe5ed552015-08-13 11:51:57 +05301711EXPORT_SYMBOL_GPL(pm_genpd_init);
Tomasz Figaaa422402014-09-19 20:27:36 +02001712
Jon Hunter3fe57712016-09-12 12:01:13 +01001713static int genpd_remove(struct generic_pm_domain *genpd)
1714{
1715 struct gpd_link *l, *link;
1716
1717 if (IS_ERR_OR_NULL(genpd))
1718 return -EINVAL;
1719
Lina Iyer35241d12016-10-14 10:47:54 -07001720 genpd_lock(genpd);
Jon Hunter3fe57712016-09-12 12:01:13 +01001721
1722 if (genpd->has_provider) {
Lina Iyer35241d12016-10-14 10:47:54 -07001723 genpd_unlock(genpd);
Jon Hunter3fe57712016-09-12 12:01:13 +01001724 pr_err("Provider present, unable to remove %s\n", genpd->name);
1725 return -EBUSY;
1726 }
1727
1728 if (!list_empty(&genpd->master_links) || genpd->device_count) {
Lina Iyer35241d12016-10-14 10:47:54 -07001729 genpd_unlock(genpd);
Jon Hunter3fe57712016-09-12 12:01:13 +01001730 pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1731 return -EBUSY;
1732 }
1733
1734 list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) {
1735 list_del(&link->master_node);
1736 list_del(&link->slave_node);
1737 kfree(link);
1738 }
1739
1740 list_del(&genpd->gpd_list_node);
Lina Iyer35241d12016-10-14 10:47:54 -07001741 genpd_unlock(genpd);
Jon Hunter3fe57712016-09-12 12:01:13 +01001742 cancel_work_sync(&genpd->power_off_work);
Lina Iyer59d65b72016-10-14 10:47:49 -07001743 kfree(genpd->free);
Jon Hunter3fe57712016-09-12 12:01:13 +01001744 pr_debug("%s: removed %s\n", __func__, genpd->name);
1745
1746 return 0;
1747}
1748
1749/**
1750 * pm_genpd_remove - Remove a generic I/O PM domain
1751 * @genpd: Pointer to PM domain that is to be removed.
1752 *
1753 * To remove the PM domain, this function:
1754 * - Removes the PM domain as a subdomain to any parent domains,
1755 * if it was added.
1756 * - Removes the PM domain from the list of registered PM domains.
1757 *
1758 * The PM domain will only be removed, if the associated provider has
1759 * been removed, it is not a parent to any other PM domain and has no
1760 * devices associated with it.
1761 */
1762int pm_genpd_remove(struct generic_pm_domain *genpd)
1763{
1764 int ret;
1765
1766 mutex_lock(&gpd_list_lock);
1767 ret = genpd_remove(genpd);
1768 mutex_unlock(&gpd_list_lock);
1769
1770 return ret;
1771}
1772EXPORT_SYMBOL_GPL(pm_genpd_remove);
1773
Tomasz Figaaa422402014-09-19 20:27:36 +02001774#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001775
Tomasz Figaaa422402014-09-19 20:27:36 +02001776/*
1777 * Device Tree based PM domain providers.
1778 *
1779 * The code below implements generic device tree based PM domain providers that
1780 * bind device tree nodes with generic PM domains registered in the system.
1781 *
1782 * Any driver that registers generic PM domains and needs to support binding of
1783 * devices to these domains is supposed to register a PM domain provider, which
1784 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1785 *
1786 * Two simple mapping functions have been provided for convenience:
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001787 * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1788 * - genpd_xlate_onecell() for mapping of multiple PM domains per node by
Tomasz Figaaa422402014-09-19 20:27:36 +02001789 * index.
1790 */
1791
1792/**
1793 * struct of_genpd_provider - PM domain provider registration structure
1794 * @link: Entry in global list of PM domain providers
1795 * @node: Pointer to device tree node of PM domain provider
1796 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1797 * into a PM domain.
1798 * @data: context pointer to be passed into @xlate callback
1799 */
1800struct of_genpd_provider {
1801 struct list_head link;
1802 struct device_node *node;
1803 genpd_xlate_t xlate;
1804 void *data;
1805};
1806
1807/* List of registered PM domain providers. */
1808static LIST_HEAD(of_genpd_providers);
1809/* Mutex to protect the list above. */
1810static DEFINE_MUTEX(of_genpd_mutex);
1811
1812/**
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001813 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
Tomasz Figaaa422402014-09-19 20:27:36 +02001814 * @genpdspec: OF phandle args to map into a PM domain
1815 * @data: xlate function private data - pointer to struct generic_pm_domain
1816 *
1817 * This is a generic xlate function that can be used to model PM domains that
1818 * have their own device tree nodes. The private data of xlate function needs
1819 * to be a valid pointer to struct generic_pm_domain.
1820 */
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001821static struct generic_pm_domain *genpd_xlate_simple(
Tomasz Figaaa422402014-09-19 20:27:36 +02001822 struct of_phandle_args *genpdspec,
1823 void *data)
1824{
Tomasz Figaaa422402014-09-19 20:27:36 +02001825 return data;
1826}
Tomasz Figaaa422402014-09-19 20:27:36 +02001827
1828/**
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001829 * genpd_xlate_onecell() - Xlate function using a single index.
Tomasz Figaaa422402014-09-19 20:27:36 +02001830 * @genpdspec: OF phandle args to map into a PM domain
1831 * @data: xlate function private data - pointer to struct genpd_onecell_data
1832 *
1833 * This is a generic xlate function that can be used to model simple PM domain
1834 * controllers that have one device tree node and provide multiple PM domains.
1835 * A single cell is used as an index into an array of PM domains specified in
1836 * the genpd_onecell_data struct when registering the provider.
1837 */
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001838static struct generic_pm_domain *genpd_xlate_onecell(
Tomasz Figaaa422402014-09-19 20:27:36 +02001839 struct of_phandle_args *genpdspec,
1840 void *data)
1841{
1842 struct genpd_onecell_data *genpd_data = data;
1843 unsigned int idx = genpdspec->args[0];
1844
1845 if (genpdspec->args_count != 1)
1846 return ERR_PTR(-EINVAL);
1847
1848 if (idx >= genpd_data->num_domains) {
1849 pr_err("%s: invalid domain index %u\n", __func__, idx);
1850 return ERR_PTR(-EINVAL);
1851 }
1852
1853 if (!genpd_data->domains[idx])
1854 return ERR_PTR(-ENOENT);
1855
1856 return genpd_data->domains[idx];
1857}
Tomasz Figaaa422402014-09-19 20:27:36 +02001858
1859/**
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001860 * genpd_add_provider() - Register a PM domain provider for a node
Tomasz Figaaa422402014-09-19 20:27:36 +02001861 * @np: Device node pointer associated with the PM domain provider.
1862 * @xlate: Callback for decoding PM domain from phandle arguments.
1863 * @data: Context pointer for @xlate callback.
1864 */
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001865static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1866 void *data)
Tomasz Figaaa422402014-09-19 20:27:36 +02001867{
1868 struct of_genpd_provider *cp;
1869
1870 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1871 if (!cp)
1872 return -ENOMEM;
1873
1874 cp->node = of_node_get(np);
1875 cp->data = data;
1876 cp->xlate = xlate;
1877
1878 mutex_lock(&of_genpd_mutex);
1879 list_add(&cp->link, &of_genpd_providers);
1880 mutex_unlock(&of_genpd_mutex);
Rob Herringea11e942017-07-18 16:42:50 -05001881 pr_debug("Added domain provider from %pOF\n", np);
Tomasz Figaaa422402014-09-19 20:27:36 +02001882
1883 return 0;
1884}
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001885
1886/**
1887 * of_genpd_add_provider_simple() - Register a simple PM domain provider
1888 * @np: Device node pointer associated with the PM domain provider.
1889 * @genpd: Pointer to PM domain associated with the PM domain provider.
1890 */
1891int of_genpd_add_provider_simple(struct device_node *np,
1892 struct generic_pm_domain *genpd)
1893{
Jon Hunter0159ec62016-09-12 12:01:10 +01001894 int ret = -EINVAL;
1895
1896 if (!np || !genpd)
1897 return -EINVAL;
1898
1899 mutex_lock(&gpd_list_lock);
1900
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301901 if (!genpd_present(genpd))
1902 goto unlock;
1903
1904 genpd->dev.of_node = np;
1905
1906 /* Parse genpd OPP table */
1907 if (genpd->set_performance_state) {
1908 ret = dev_pm_opp_of_add_table(&genpd->dev);
1909 if (ret) {
1910 dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
1911 ret);
1912 goto unlock;
Viresh Kumar8ce95842017-03-17 11:19:21 +05301913 }
Viresh Kumar1067ae32018-11-02 11:18:08 +05301914
1915 /*
1916 * Save table for faster processing while setting performance
1917 * state.
1918 */
1919 genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
1920 WARN_ON(!genpd->opp_table);
Jon Hunterde0aa06d2016-09-12 12:01:12 +01001921 }
1922
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301923 ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
1924 if (ret) {
Viresh Kumar1067ae32018-11-02 11:18:08 +05301925 if (genpd->set_performance_state) {
1926 dev_pm_opp_put_opp_table(genpd->opp_table);
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301927 dev_pm_opp_of_remove_table(&genpd->dev);
Viresh Kumar1067ae32018-11-02 11:18:08 +05301928 }
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301929
1930 goto unlock;
1931 }
1932
1933 genpd->provider = &np->fwnode;
1934 genpd->has_provider = true;
1935
1936unlock:
Jon Hunter0159ec62016-09-12 12:01:10 +01001937 mutex_unlock(&gpd_list_lock);
1938
1939 return ret;
Jon Hunter892ebdcc2016-09-12 12:01:09 +01001940}
1941EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
1942
1943/**
1944 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
1945 * @np: Device node pointer associated with the PM domain provider.
1946 * @data: Pointer to the data associated with the PM domain provider.
1947 */
1948int of_genpd_add_provider_onecell(struct device_node *np,
1949 struct genpd_onecell_data *data)
1950{
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301951 struct generic_pm_domain *genpd;
Jon Hunter0159ec62016-09-12 12:01:10 +01001952 unsigned int i;
Jon Hunterde0aa06d2016-09-12 12:01:12 +01001953 int ret = -EINVAL;
Jon Hunter0159ec62016-09-12 12:01:10 +01001954
1955 if (!np || !data)
1956 return -EINVAL;
1957
1958 mutex_lock(&gpd_list_lock);
1959
Thierry Reding40845522017-03-29 18:34:50 +02001960 if (!data->xlate)
1961 data->xlate = genpd_xlate_onecell;
1962
Jon Hunter0159ec62016-09-12 12:01:10 +01001963 for (i = 0; i < data->num_domains; i++) {
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301964 genpd = data->domains[i];
1965
1966 if (!genpd)
Tomeu Vizoso609bed62016-09-15 14:05:23 +02001967 continue;
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301968 if (!genpd_present(genpd))
Jon Hunterde0aa06d2016-09-12 12:01:12 +01001969 goto error;
1970
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301971 genpd->dev.of_node = np;
1972
1973 /* Parse genpd OPP table */
1974 if (genpd->set_performance_state) {
1975 ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
1976 if (ret) {
1977 dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
1978 i, ret);
1979 goto error;
1980 }
Viresh Kumar1067ae32018-11-02 11:18:08 +05301981
1982 /*
1983 * Save table for faster processing while setting
1984 * performance state.
1985 */
1986 genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i);
1987 WARN_ON(!genpd->opp_table);
Viresh Kumar6a0ae732018-04-05 15:53:34 +05301988 }
1989
1990 genpd->provider = &np->fwnode;
1991 genpd->has_provider = true;
Jon Hunter0159ec62016-09-12 12:01:10 +01001992 }
1993
Thierry Reding40845522017-03-29 18:34:50 +02001994 ret = genpd_add_provider(np, data->xlate, data);
Jon Hunterde0aa06d2016-09-12 12:01:12 +01001995 if (ret < 0)
1996 goto error;
1997
1998 mutex_unlock(&gpd_list_lock);
1999
2000 return 0;
2001
2002error:
2003 while (i--) {
Viresh Kumar6a0ae732018-04-05 15:53:34 +05302004 genpd = data->domains[i];
2005
2006 if (!genpd)
Tomeu Vizoso609bed62016-09-15 14:05:23 +02002007 continue;
Viresh Kumar6a0ae732018-04-05 15:53:34 +05302008
2009 genpd->provider = NULL;
2010 genpd->has_provider = false;
2011
Viresh Kumar1067ae32018-11-02 11:18:08 +05302012 if (genpd->set_performance_state) {
2013 dev_pm_opp_put_opp_table(genpd->opp_table);
Viresh Kumar6a0ae732018-04-05 15:53:34 +05302014 dev_pm_opp_of_remove_table(&genpd->dev);
Viresh Kumar1067ae32018-11-02 11:18:08 +05302015 }
Jon Hunterde0aa06d2016-09-12 12:01:12 +01002016 }
Jon Hunter0159ec62016-09-12 12:01:10 +01002017
2018 mutex_unlock(&gpd_list_lock);
2019
2020 return ret;
Jon Hunter892ebdcc2016-09-12 12:01:09 +01002021}
2022EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
Tomasz Figaaa422402014-09-19 20:27:36 +02002023
2024/**
2025 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2026 * @np: Device node pointer associated with the PM domain provider
2027 */
2028void of_genpd_del_provider(struct device_node *np)
2029{
Krzysztof Kozlowskib556b152017-06-28 16:56:19 +02002030 struct of_genpd_provider *cp, *tmp;
Jon Hunterde0aa06d2016-09-12 12:01:12 +01002031 struct generic_pm_domain *gpd;
Tomasz Figaaa422402014-09-19 20:27:36 +02002032
Jon Hunterde0aa06d2016-09-12 12:01:12 +01002033 mutex_lock(&gpd_list_lock);
Tomasz Figaaa422402014-09-19 20:27:36 +02002034 mutex_lock(&of_genpd_mutex);
Krzysztof Kozlowskib556b152017-06-28 16:56:19 +02002035 list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
Tomasz Figaaa422402014-09-19 20:27:36 +02002036 if (cp->node == np) {
Jon Hunterde0aa06d2016-09-12 12:01:12 +01002037 /*
2038 * For each PM domain associated with the
2039 * provider, set the 'has_provider' to false
2040 * so that the PM domain can be safely removed.
2041 */
Viresh Kumar6a0ae732018-04-05 15:53:34 +05302042 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2043 if (gpd->provider == &np->fwnode) {
Jon Hunterde0aa06d2016-09-12 12:01:12 +01002044 gpd->has_provider = false;
2045
Viresh Kumar6a0ae732018-04-05 15:53:34 +05302046 if (!gpd->set_performance_state)
2047 continue;
2048
Viresh Kumar1067ae32018-11-02 11:18:08 +05302049 dev_pm_opp_put_opp_table(gpd->opp_table);
Viresh Kumar6a0ae732018-04-05 15:53:34 +05302050 dev_pm_opp_of_remove_table(&gpd->dev);
2051 }
2052 }
2053
Tomasz Figaaa422402014-09-19 20:27:36 +02002054 list_del(&cp->link);
2055 of_node_put(cp->node);
2056 kfree(cp);
2057 break;
2058 }
2059 }
2060 mutex_unlock(&of_genpd_mutex);
Jon Hunterde0aa06d2016-09-12 12:01:12 +01002061 mutex_unlock(&gpd_list_lock);
Tomasz Figaaa422402014-09-19 20:27:36 +02002062}
2063EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2064
2065/**
Jon Hunterf58d4e52016-09-12 12:01:08 +01002066 * genpd_get_from_provider() - Look-up PM domain
Tomasz Figaaa422402014-09-19 20:27:36 +02002067 * @genpdspec: OF phandle args to use for look-up
2068 *
2069 * Looks for a PM domain provider under the node specified by @genpdspec and if
2070 * found, uses xlate function of the provider to map phandle args to a PM
2071 * domain.
2072 *
2073 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2074 * on failure.
2075 */
Jon Hunterf58d4e52016-09-12 12:01:08 +01002076static struct generic_pm_domain *genpd_get_from_provider(
Tomasz Figaaa422402014-09-19 20:27:36 +02002077 struct of_phandle_args *genpdspec)
2078{
2079 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2080 struct of_genpd_provider *provider;
2081
Jon Hunter41795a82016-03-04 10:55:15 +00002082 if (!genpdspec)
2083 return ERR_PTR(-EINVAL);
2084
Tomasz Figaaa422402014-09-19 20:27:36 +02002085 mutex_lock(&of_genpd_mutex);
2086
2087 /* Check if we have such a provider in our array */
2088 list_for_each_entry(provider, &of_genpd_providers, link) {
2089 if (provider->node == genpdspec->np)
2090 genpd = provider->xlate(genpdspec, provider->data);
2091 if (!IS_ERR(genpd))
2092 break;
2093 }
2094
2095 mutex_unlock(&of_genpd_mutex);
2096
2097 return genpd;
2098}
2099
2100/**
Jon Hunterec695722016-09-12 12:01:05 +01002101 * of_genpd_add_device() - Add a device to an I/O PM domain
2102 * @genpdspec: OF phandle args to use for look-up PM domain
2103 * @dev: Device to be added.
2104 *
2105 * Looks-up an I/O PM domain based upon phandle args provided and adds
2106 * the device to the PM domain. Returns a negative error code on failure.
2107 */
2108int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2109{
2110 struct generic_pm_domain *genpd;
Jon Hunter19efa5f2016-09-12 12:01:11 +01002111 int ret;
2112
2113 mutex_lock(&gpd_list_lock);
Jon Hunterec695722016-09-12 12:01:05 +01002114
Jon Hunterf58d4e52016-09-12 12:01:08 +01002115 genpd = genpd_get_from_provider(genpdspec);
Jon Hunter19efa5f2016-09-12 12:01:11 +01002116 if (IS_ERR(genpd)) {
2117 ret = PTR_ERR(genpd);
2118 goto out;
2119 }
Jon Hunterec695722016-09-12 12:01:05 +01002120
Jon Hunter19efa5f2016-09-12 12:01:11 +01002121 ret = genpd_add_device(genpd, dev, NULL);
2122
2123out:
2124 mutex_unlock(&gpd_list_lock);
2125
2126 return ret;
Jon Hunterec695722016-09-12 12:01:05 +01002127}
2128EXPORT_SYMBOL_GPL(of_genpd_add_device);
2129
2130/**
2131 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2132 * @parent_spec: OF phandle args to use for parent PM domain look-up
2133 * @subdomain_spec: OF phandle args to use for subdomain look-up
2134 *
2135 * Looks-up a parent PM domain and subdomain based upon phandle args
2136 * provided and adds the subdomain to the parent PM domain. Returns a
2137 * negative error code on failure.
2138 */
2139int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2140 struct of_phandle_args *subdomain_spec)
2141{
2142 struct generic_pm_domain *parent, *subdomain;
Jon Hunter19efa5f2016-09-12 12:01:11 +01002143 int ret;
2144
2145 mutex_lock(&gpd_list_lock);
Jon Hunterec695722016-09-12 12:01:05 +01002146
Jon Hunterf58d4e52016-09-12 12:01:08 +01002147 parent = genpd_get_from_provider(parent_spec);
Jon Hunter19efa5f2016-09-12 12:01:11 +01002148 if (IS_ERR(parent)) {
2149 ret = PTR_ERR(parent);
2150 goto out;
2151 }
Jon Hunterec695722016-09-12 12:01:05 +01002152
Jon Hunterf58d4e52016-09-12 12:01:08 +01002153 subdomain = genpd_get_from_provider(subdomain_spec);
Jon Hunter19efa5f2016-09-12 12:01:11 +01002154 if (IS_ERR(subdomain)) {
2155 ret = PTR_ERR(subdomain);
2156 goto out;
2157 }
Jon Hunterec695722016-09-12 12:01:05 +01002158
Jon Hunter19efa5f2016-09-12 12:01:11 +01002159 ret = genpd_add_subdomain(parent, subdomain);
2160
2161out:
2162 mutex_unlock(&gpd_list_lock);
2163
2164 return ret;
Jon Hunterec695722016-09-12 12:01:05 +01002165}
2166EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2167
2168/**
Jon Hunter17926552016-09-12 12:01:14 +01002169 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2170 * @provider: Pointer to device structure associated with provider
2171 *
2172 * Find the last PM domain that was added by a particular provider and
2173 * remove this PM domain from the list of PM domains. The provider is
2174 * identified by the 'provider' device structure that is passed. The PM
2175 * domain will only be removed, if the provider associated with domain
2176 * has been removed.
2177 *
2178 * Returns a valid pointer to struct generic_pm_domain on success or
2179 * ERR_PTR() on failure.
2180 */
2181struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2182{
Krzysztof Kozlowskia7e2d1b2017-06-28 16:56:20 +02002183 struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
Jon Hunter17926552016-09-12 12:01:14 +01002184 int ret;
2185
2186 if (IS_ERR_OR_NULL(np))
2187 return ERR_PTR(-EINVAL);
2188
2189 mutex_lock(&gpd_list_lock);
Krzysztof Kozlowskia7e2d1b2017-06-28 16:56:20 +02002190 list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
Jon Hunter17926552016-09-12 12:01:14 +01002191 if (gpd->provider == &np->fwnode) {
2192 ret = genpd_remove(gpd);
2193 genpd = ret ? ERR_PTR(ret) : gpd;
2194 break;
2195 }
2196 }
2197 mutex_unlock(&gpd_list_lock);
2198
2199 return genpd;
2200}
2201EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2202
Ulf Hansson3c095f32018-05-31 12:59:58 +02002203static void genpd_release_dev(struct device *dev)
2204{
2205 kfree(dev);
2206}
2207
2208static struct bus_type genpd_bus_type = {
2209 .name = "genpd",
2210};
2211
Jon Hunter17926552016-09-12 12:01:14 +01002212/**
Tomasz Figaaa422402014-09-19 20:27:36 +02002213 * genpd_dev_pm_detach - Detach a device from its PM domain.
Jon Hunter8bb69442015-08-27 10:17:00 +01002214 * @dev: Device to detach.
Tomasz Figaaa422402014-09-19 20:27:36 +02002215 * @power_off: Currently not used
2216 *
2217 * Try to locate a corresponding generic PM domain, which the device was
2218 * attached to previously. If such is found, the device is detached from it.
2219 */
2220static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2221{
Russell King446d999c2015-03-20 17:20:33 +00002222 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002223 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02002224 int ret = 0;
2225
Ulf Hansson85168d52016-09-21 15:38:50 +02002226 pd = dev_to_genpd(dev);
2227 if (IS_ERR(pd))
Tomasz Figaaa422402014-09-19 20:27:36 +02002228 return;
2229
2230 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2231
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002232 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Ulf Hansson85168d52016-09-21 15:38:50 +02002233 ret = genpd_remove_device(pd, dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02002234 if (ret != -EAGAIN)
2235 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002236
2237 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02002238 cond_resched();
2239 }
2240
2241 if (ret < 0) {
2242 dev_err(dev, "failed to remove from PM domain %s: %d",
2243 pd->name, ret);
2244 return;
2245 }
2246
2247 /* Check if PM domain can be powered off after removing this device. */
2248 genpd_queue_power_off_work(pd);
Ulf Hansson3c095f32018-05-31 12:59:58 +02002249
2250 /* Unregister the device if it was created by genpd. */
2251 if (dev->bus == &genpd_bus_type)
2252 device_unregister(dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02002253}
2254
Russell King632f7ce2015-03-20 15:55:12 +01002255static void genpd_dev_pm_sync(struct device *dev)
2256{
2257 struct generic_pm_domain *pd;
2258
2259 pd = dev_to_genpd(dev);
2260 if (IS_ERR(pd))
2261 return;
2262
2263 genpd_queue_power_off_work(pd);
2264}
2265
Ulf Hansson8cb1cbd62018-05-31 12:59:57 +02002266static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np,
Ulf Hansson895b6612018-06-29 11:15:37 +02002267 unsigned int index, bool power_on)
Tomasz Figaaa422402014-09-19 20:27:36 +02002268{
2269 struct of_phandle_args pd_args;
2270 struct generic_pm_domain *pd;
2271 int ret;
2272
Ulf Hansson8cb1cbd62018-05-31 12:59:57 +02002273 ret = of_parse_phandle_with_args(np, "power-domains",
2274 "#power-domain-cells", index, &pd_args);
Geert Uytterhoeven001d50c2017-11-30 12:54:28 +01002275 if (ret < 0)
Ulf Hanssonbcd931f2018-05-31 12:59:56 +02002276 return ret;
Tomasz Figaaa422402014-09-19 20:27:36 +02002277
Jon Hunter19efa5f2016-09-12 12:01:11 +01002278 mutex_lock(&gpd_list_lock);
Jon Hunterf58d4e52016-09-12 12:01:08 +01002279 pd = genpd_get_from_provider(&pd_args);
Eric Anholt265e2cf2015-12-01 09:39:31 -08002280 of_node_put(pd_args.np);
Tomasz Figaaa422402014-09-19 20:27:36 +02002281 if (IS_ERR(pd)) {
Jon Hunter19efa5f2016-09-12 12:01:11 +01002282 mutex_unlock(&gpd_list_lock);
Tomasz Figaaa422402014-09-19 20:27:36 +02002283 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2284 __func__, PTR_ERR(pd));
Rob Herringe01afc32018-07-09 09:41:53 -06002285 return driver_deferred_probe_check_state(dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02002286 }
2287
2288 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2289
Ulf Hanssonb56d9c92018-04-26 10:53:02 +02002290 ret = genpd_add_device(pd, dev, NULL);
Jon Hunter19efa5f2016-09-12 12:01:11 +01002291 mutex_unlock(&gpd_list_lock);
Tomasz Figaaa422402014-09-19 20:27:36 +02002292
2293 if (ret < 0) {
Geert Uytterhoeven34994692016-11-30 13:24:56 +01002294 if (ret != -EPROBE_DEFER)
2295 dev_err(dev, "failed to add to PM domain %s: %d",
2296 pd->name, ret);
Ulf Hansson919b7302018-05-09 12:17:52 +02002297 return ret;
Tomasz Figaaa422402014-09-19 20:27:36 +02002298 }
2299
2300 dev->pm_domain->detach = genpd_dev_pm_detach;
Russell King632f7ce2015-03-20 15:55:12 +01002301 dev->pm_domain->sync = genpd_dev_pm_sync;
Tomasz Figaaa422402014-09-19 20:27:36 +02002302
Ulf Hansson895b6612018-06-29 11:15:37 +02002303 if (power_on) {
2304 genpd_lock(pd);
2305 ret = genpd_power_on(pd, 0);
2306 genpd_unlock(pd);
2307 }
Ulf Hansson72038df2018-04-26 10:53:00 +02002308
2309 if (ret)
2310 genpd_remove_device(pd, dev);
Ulf Hansson919b7302018-05-09 12:17:52 +02002311
2312 return ret ? -EPROBE_DEFER : 1;
Tomasz Figaaa422402014-09-19 20:27:36 +02002313}
Ulf Hansson8cb1cbd62018-05-31 12:59:57 +02002314
2315/**
2316 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2317 * @dev: Device to attach.
2318 *
2319 * Parse device's OF node to find a PM domain specifier. If such is found,
2320 * attaches the device to retrieved pm_domain ops.
2321 *
2322 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2323 * PM domain or when multiple power-domains exists for it, else a negative error
2324 * code. Note that if a power-domain exists for the device, but it cannot be
2325 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2326 * not probed and to re-try again later.
2327 */
2328int genpd_dev_pm_attach(struct device *dev)
2329{
2330 if (!dev->of_node)
2331 return 0;
2332
2333 /*
2334 * Devices with multiple PM domains must be attached separately, as we
2335 * can only attach one PM domain per device.
2336 */
2337 if (of_count_phandle_with_args(dev->of_node, "power-domains",
2338 "#power-domain-cells") != 1)
2339 return 0;
2340
Ulf Hansson895b6612018-06-29 11:15:37 +02002341 return __genpd_dev_pm_attach(dev, dev->of_node, 0, true);
Ulf Hansson8cb1cbd62018-05-31 12:59:57 +02002342}
Tomasz Figaaa422402014-09-19 20:27:36 +02002343EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
Lina Iyer30f604282016-10-14 10:47:51 -07002344
Ulf Hansson3c095f32018-05-31 12:59:58 +02002345/**
2346 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2347 * @dev: The device used to lookup the PM domain.
2348 * @index: The index of the PM domain.
2349 *
2350 * Parse device's OF node to find a PM domain specifier at the provided @index.
2351 * If such is found, creates a virtual device and attaches it to the retrieved
2352 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2353 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2354 *
2355 * Returns the created virtual device if successfully attached PM domain, NULL
2356 * when the device don't need a PM domain, else an ERR_PTR() in case of
2357 * failures. If a power-domain exists for the device, but cannot be found or
2358 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2359 * is not probed and to re-try again later.
2360 */
2361struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2362 unsigned int index)
2363{
Viresh Kumar560928b2018-10-25 09:07:38 +05302364 struct device *virt_dev;
Ulf Hansson3c095f32018-05-31 12:59:58 +02002365 int num_domains;
2366 int ret;
2367
2368 if (!dev->of_node)
2369 return NULL;
2370
2371 /* Deal only with devices using multiple PM domains. */
2372 num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2373 "#power-domain-cells");
2374 if (num_domains < 2 || index >= num_domains)
2375 return NULL;
2376
2377 /* Allocate and register device on the genpd bus. */
Viresh Kumar560928b2018-10-25 09:07:38 +05302378 virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2379 if (!virt_dev)
Ulf Hansson3c095f32018-05-31 12:59:58 +02002380 return ERR_PTR(-ENOMEM);
2381
Viresh Kumar560928b2018-10-25 09:07:38 +05302382 dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2383 virt_dev->bus = &genpd_bus_type;
2384 virt_dev->release = genpd_release_dev;
Ulf Hansson3c095f32018-05-31 12:59:58 +02002385
Viresh Kumar560928b2018-10-25 09:07:38 +05302386 ret = device_register(virt_dev);
Ulf Hansson3c095f32018-05-31 12:59:58 +02002387 if (ret) {
Viresh Kumar560928b2018-10-25 09:07:38 +05302388 kfree(virt_dev);
Ulf Hansson3c095f32018-05-31 12:59:58 +02002389 return ERR_PTR(ret);
2390 }
2391
2392 /* Try to attach the device to the PM domain at the specified index. */
Viresh Kumar560928b2018-10-25 09:07:38 +05302393 ret = __genpd_dev_pm_attach(virt_dev, dev->of_node, index, false);
Ulf Hansson3c095f32018-05-31 12:59:58 +02002394 if (ret < 1) {
Viresh Kumar560928b2018-10-25 09:07:38 +05302395 device_unregister(virt_dev);
Ulf Hansson3c095f32018-05-31 12:59:58 +02002396 return ret ? ERR_PTR(ret) : NULL;
2397 }
2398
Viresh Kumar560928b2018-10-25 09:07:38 +05302399 pm_runtime_enable(virt_dev);
2400 genpd_queue_power_off_work(dev_to_genpd(virt_dev));
Ulf Hansson3c095f32018-05-31 12:59:58 +02002401
Viresh Kumar560928b2018-10-25 09:07:38 +05302402 return virt_dev;
Ulf Hansson3c095f32018-05-31 12:59:58 +02002403}
2404EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2405
Ulf Hansson5d6be702018-06-29 13:04:31 +02002406/**
2407 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2408 * @dev: The device used to lookup the PM domain.
2409 * @name: The name of the PM domain.
2410 *
2411 * Parse device's OF node to find a PM domain specifier using the
2412 * power-domain-names DT property. For further description see
2413 * genpd_dev_pm_attach_by_id().
2414 */
2415struct device *genpd_dev_pm_attach_by_name(struct device *dev, char *name)
2416{
2417 int index;
2418
2419 if (!dev->of_node)
2420 return NULL;
2421
2422 index = of_property_match_string(dev->of_node, "power-domain-names",
2423 name);
2424 if (index < 0)
2425 return NULL;
2426
2427 return genpd_dev_pm_attach_by_id(dev, index);
2428}
2429
Lina Iyer30f604282016-10-14 10:47:51 -07002430static const struct of_device_id idle_state_match[] = {
Lina Iyer598da542016-11-03 14:54:35 -07002431 { .compatible = "domain-idle-state", },
Lina Iyer30f604282016-10-14 10:47:51 -07002432 { }
2433};
2434
2435static int genpd_parse_state(struct genpd_power_state *genpd_state,
2436 struct device_node *state_node)
2437{
2438 int err;
2439 u32 residency;
2440 u32 entry_latency, exit_latency;
Lina Iyer30f604282016-10-14 10:47:51 -07002441
2442 err = of_property_read_u32(state_node, "entry-latency-us",
2443 &entry_latency);
2444 if (err) {
Rob Herringea11e942017-07-18 16:42:50 -05002445 pr_debug(" * %pOF missing entry-latency-us property\n",
2446 state_node);
Lina Iyer30f604282016-10-14 10:47:51 -07002447 return -EINVAL;
2448 }
2449
2450 err = of_property_read_u32(state_node, "exit-latency-us",
2451 &exit_latency);
2452 if (err) {
Rob Herringea11e942017-07-18 16:42:50 -05002453 pr_debug(" * %pOF missing exit-latency-us property\n",
2454 state_node);
Lina Iyer30f604282016-10-14 10:47:51 -07002455 return -EINVAL;
2456 }
2457
2458 err = of_property_read_u32(state_node, "min-residency-us", &residency);
2459 if (!err)
2460 genpd_state->residency_ns = 1000 * residency;
2461
2462 genpd_state->power_on_latency_ns = 1000 * exit_latency;
2463 genpd_state->power_off_latency_ns = 1000 * entry_latency;
Lina Iyer0c9b6942016-10-14 10:47:52 -07002464 genpd_state->fwnode = &state_node->fwnode;
Lina Iyer30f604282016-10-14 10:47:51 -07002465
2466 return 0;
2467}
2468
Ulf Hanssona3381e32018-01-23 21:43:08 +01002469static int genpd_iterate_idle_states(struct device_node *dn,
2470 struct genpd_power_state *states)
2471{
2472 int ret;
2473 struct of_phandle_iterator it;
2474 struct device_node *np;
2475 int i = 0;
2476
2477 ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2478 if (ret <= 0)
2479 return ret;
2480
2481 /* Loop over the phandles until all the requested entry is found */
2482 of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2483 np = it.node;
2484 if (!of_match_node(idle_state_match, np))
2485 continue;
2486 if (states) {
2487 ret = genpd_parse_state(&states[i], np);
2488 if (ret) {
2489 pr_err("Parsing idle state node %pOF failed with err %d\n",
2490 np, ret);
2491 of_node_put(np);
2492 return ret;
2493 }
2494 }
2495 i++;
2496 }
2497
2498 return i;
2499}
2500
Lina Iyer30f604282016-10-14 10:47:51 -07002501/**
2502 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2503 *
2504 * @dn: The genpd device node
2505 * @states: The pointer to which the state array will be saved.
2506 * @n: The count of elements in the array returned from this function.
2507 *
2508 * Returns the device states parsed from the OF node. The memory for the states
2509 * is allocated by this function and is the responsibility of the caller to
Ulf Hansson2c361682018-10-03 16:38:14 +02002510 * free the memory after use. If any or zero compatible domain idle states is
2511 * found it returns 0 and in case of errors, a negative error code is returned.
Lina Iyer30f604282016-10-14 10:47:51 -07002512 */
2513int of_genpd_parse_idle_states(struct device_node *dn,
2514 struct genpd_power_state **states, int *n)
2515{
2516 struct genpd_power_state *st;
Ulf Hanssona3381e32018-01-23 21:43:08 +01002517 int ret;
Lina Iyer30f604282016-10-14 10:47:51 -07002518
Ulf Hanssona3381e32018-01-23 21:43:08 +01002519 ret = genpd_iterate_idle_states(dn, NULL);
Ulf Hansson2c361682018-10-03 16:38:14 +02002520 if (ret < 0)
2521 return ret;
2522
2523 if (!ret) {
2524 *states = NULL;
2525 *n = 0;
2526 return 0;
2527 }
Lina Iyer30f604282016-10-14 10:47:51 -07002528
Ulf Hanssona3381e32018-01-23 21:43:08 +01002529 st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
Lina Iyer30f604282016-10-14 10:47:51 -07002530 if (!st)
2531 return -ENOMEM;
2532
Ulf Hanssona3381e32018-01-23 21:43:08 +01002533 ret = genpd_iterate_idle_states(dn, st);
2534 if (ret <= 0) {
2535 kfree(st);
2536 return ret < 0 ? ret : -EINVAL;
Lina Iyer30f604282016-10-14 10:47:51 -07002537 }
2538
Ulf Hanssona3381e32018-01-23 21:43:08 +01002539 *states = st;
2540 *n = ret;
Lina Iyer30f604282016-10-14 10:47:51 -07002541
2542 return 0;
2543}
2544EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2545
Viresh Kumar6e417662017-11-29 15:21:51 +05302546/**
Viresh Kumare38f89d2018-06-13 20:22:04 +05302547 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2548 *
2549 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2550 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2551 * state.
2552 *
2553 * Returns performance state encoded in the OPP of the genpd. This calls
2554 * platform specific genpd->opp_to_performance_state() callback to translate
2555 * power domain OPP to performance state.
2556 *
2557 * Returns performance state on success and 0 on failure.
2558 */
2559unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2560 struct dev_pm_opp *opp)
2561{
2562 struct generic_pm_domain *genpd = NULL;
2563 int state;
2564
2565 genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2566
2567 if (unlikely(!genpd->opp_to_performance_state))
2568 return 0;
2569
2570 genpd_lock(genpd);
2571 state = genpd->opp_to_performance_state(genpd, opp);
2572 genpd_unlock(genpd);
2573
2574 return state;
2575}
2576EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2577
Ulf Hansson3c095f32018-05-31 12:59:58 +02002578static int __init genpd_bus_init(void)
2579{
2580 return bus_register(&genpd_bus_type);
2581}
2582core_initcall(genpd_bus_init);
2583
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01002584#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002585
2586
2587/*** debugfs support ***/
2588
Jon Hunter8b0510b2016-08-11 11:40:05 +01002589#ifdef CONFIG_DEBUG_FS
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002590#include <linux/pm.h>
2591#include <linux/device.h>
2592#include <linux/debugfs.h>
2593#include <linux/seq_file.h>
2594#include <linux/init.h>
2595#include <linux/kobject.h>
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002596static struct dentry *genpd_debugfs_dir;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002597
2598/*
2599 * TODO: This function is a slightly modified version of rtpm_status_show
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01002600 * from sysfs.c, so generalize it.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002601 */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002602static void rtpm_status_str(struct seq_file *s, struct device *dev)
2603{
2604 static const char * const status_lookup[] = {
2605 [RPM_ACTIVE] = "active",
2606 [RPM_RESUMING] = "resuming",
2607 [RPM_SUSPENDED] = "suspended",
2608 [RPM_SUSPENDING] = "suspending"
2609 };
2610 const char *p = "";
2611
2612 if (dev->power.runtime_error)
2613 p = "error";
2614 else if (dev->power.disable_depth)
2615 p = "unsupported";
2616 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2617 p = status_lookup[dev->power.runtime_status];
2618 else
2619 WARN_ON(1);
2620
2621 seq_puts(s, p);
2622}
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002623
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002624static int genpd_summary_one(struct seq_file *s,
2625 struct generic_pm_domain *genpd)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002626{
2627 static const char * const status_lookup[] = {
2628 [GPD_STATE_ACTIVE] = "on",
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002629 [GPD_STATE_POWER_OFF] = "off"
2630 };
2631 struct pm_domain_data *pm_data;
2632 const char *kobj_path;
2633 struct gpd_link *link;
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01002634 char state[16];
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002635 int ret;
2636
Lina Iyer35241d12016-10-14 10:47:54 -07002637 ret = genpd_lock_interruptible(genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002638 if (ret)
2639 return -ERESTARTSYS;
2640
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002641 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002642 goto exit;
Ulf Hansson41e2c8e2017-03-20 11:19:20 +01002643 if (!genpd_status_on(genpd))
Geert Uytterhoeven0ba554e2016-02-23 17:49:18 +01002644 snprintf(state, sizeof(state), "%s-%u",
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01002645 status_lookup[genpd->status], genpd->state_idx);
Axel Haslamfc5cbf02016-02-15 11:10:51 +01002646 else
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01002647 snprintf(state, sizeof(state), "%s",
2648 status_lookup[genpd->status]);
2649 seq_printf(s, "%-30s %-15s ", genpd->name, state);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002650
2651 /*
2652 * Modifications on the list require holding locks on both
2653 * master and slave, so we are safe.
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002654 * Also genpd->name is immutable.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002655 */
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002656 list_for_each_entry(link, &genpd->master_links, master_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002657 seq_printf(s, "%s", link->slave->name);
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002658 if (!list_is_last(&link->master_node, &genpd->master_links))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002659 seq_puts(s, ", ");
2660 }
2661
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002662 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
Lina Iyerd716f472016-10-14 10:47:55 -07002663 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2664 genpd_is_irq_safe(genpd) ?
2665 GFP_ATOMIC : GFP_KERNEL);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002666 if (kobj_path == NULL)
2667 continue;
2668
2669 seq_printf(s, "\n %-50s ", kobj_path);
2670 rtpm_status_str(s, pm_data->dev);
2671 kfree(kobj_path);
2672 }
2673
2674 seq_puts(s, "\n");
2675exit:
Lina Iyer35241d12016-10-14 10:47:54 -07002676 genpd_unlock(genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002677
2678 return 0;
2679}
2680
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002681static int genpd_summary_show(struct seq_file *s, void *data)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002682{
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002683 struct generic_pm_domain *genpd;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002684 int ret = 0;
2685
Geert Uytterhoeven15dec672015-08-11 14:50:49 +02002686 seq_puts(s, "domain status slaves\n");
2687 seq_puts(s, " /device runtime status\n");
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002688 seq_puts(s, "----------------------------------------------------------------------\n");
2689
2690 ret = mutex_lock_interruptible(&gpd_list_lock);
2691 if (ret)
2692 return -ERESTARTSYS;
2693
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002694 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002695 ret = genpd_summary_one(s, genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002696 if (ret)
2697 break;
2698 }
2699 mutex_unlock(&gpd_list_lock);
2700
2701 return ret;
2702}
2703
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002704static int genpd_status_show(struct seq_file *s, void *data)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002705{
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002706 static const char * const status_lookup[] = {
2707 [GPD_STATE_ACTIVE] = "on",
2708 [GPD_STATE_POWER_OFF] = "off"
2709 };
2710
2711 struct generic_pm_domain *genpd = s->private;
2712 int ret = 0;
2713
2714 ret = genpd_lock_interruptible(genpd);
2715 if (ret)
2716 return -ERESTARTSYS;
2717
2718 if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2719 goto exit;
2720
2721 if (genpd->status == GPD_STATE_POWER_OFF)
2722 seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2723 genpd->state_idx);
2724 else
2725 seq_printf(s, "%s\n", status_lookup[genpd->status]);
2726exit:
2727 genpd_unlock(genpd);
2728 return ret;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002729}
2730
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002731static int genpd_sub_domains_show(struct seq_file *s, void *data)
2732{
2733 struct generic_pm_domain *genpd = s->private;
2734 struct gpd_link *link;
2735 int ret = 0;
2736
2737 ret = genpd_lock_interruptible(genpd);
2738 if (ret)
2739 return -ERESTARTSYS;
2740
2741 list_for_each_entry(link, &genpd->master_links, master_node)
2742 seq_printf(s, "%s\n", link->slave->name);
2743
2744 genpd_unlock(genpd);
2745 return ret;
2746}
2747
2748static int genpd_idle_states_show(struct seq_file *s, void *data)
2749{
2750 struct generic_pm_domain *genpd = s->private;
2751 unsigned int i;
2752 int ret = 0;
2753
2754 ret = genpd_lock_interruptible(genpd);
2755 if (ret)
2756 return -ERESTARTSYS;
2757
2758 seq_puts(s, "State Time Spent(ms)\n");
2759
2760 for (i = 0; i < genpd->state_count; i++) {
2761 ktime_t delta = 0;
2762 s64 msecs;
2763
2764 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2765 (genpd->state_idx == i))
2766 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2767
2768 msecs = ktime_to_ms(
2769 ktime_add(genpd->states[i].idle_time, delta));
2770 seq_printf(s, "S%-13i %lld\n", i, msecs);
2771 }
2772
2773 genpd_unlock(genpd);
2774 return ret;
2775}
2776
2777static int genpd_active_time_show(struct seq_file *s, void *data)
2778{
2779 struct generic_pm_domain *genpd = s->private;
2780 ktime_t delta = 0;
2781 int ret = 0;
2782
2783 ret = genpd_lock_interruptible(genpd);
2784 if (ret)
2785 return -ERESTARTSYS;
2786
2787 if (genpd->status == GPD_STATE_ACTIVE)
2788 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2789
2790 seq_printf(s, "%lld ms\n", ktime_to_ms(
2791 ktime_add(genpd->on_time, delta)));
2792
2793 genpd_unlock(genpd);
2794 return ret;
2795}
2796
2797static int genpd_total_idle_time_show(struct seq_file *s, void *data)
2798{
2799 struct generic_pm_domain *genpd = s->private;
2800 ktime_t delta = 0, total = 0;
2801 unsigned int i;
2802 int ret = 0;
2803
2804 ret = genpd_lock_interruptible(genpd);
2805 if (ret)
2806 return -ERESTARTSYS;
2807
2808 for (i = 0; i < genpd->state_count; i++) {
2809
2810 if ((genpd->status == GPD_STATE_POWER_OFF) &&
2811 (genpd->state_idx == i))
2812 delta = ktime_sub(ktime_get(), genpd->accounting_time);
2813
2814 total = ktime_add(total, genpd->states[i].idle_time);
2815 }
2816 total = ktime_add(total, delta);
2817
2818 seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2819
2820 genpd_unlock(genpd);
2821 return ret;
2822}
2823
2824
2825static int genpd_devices_show(struct seq_file *s, void *data)
2826{
2827 struct generic_pm_domain *genpd = s->private;
2828 struct pm_domain_data *pm_data;
2829 const char *kobj_path;
2830 int ret = 0;
2831
2832 ret = genpd_lock_interruptible(genpd);
2833 if (ret)
2834 return -ERESTARTSYS;
2835
2836 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2837 kobj_path = kobject_get_path(&pm_data->dev->kobj,
2838 genpd_is_irq_safe(genpd) ?
2839 GFP_ATOMIC : GFP_KERNEL);
2840 if (kobj_path == NULL)
2841 continue;
2842
2843 seq_printf(s, "%s\n", kobj_path);
2844 kfree(kobj_path);
2845 }
2846
2847 genpd_unlock(genpd);
2848 return ret;
2849}
2850
Rajendra Nayake8912812018-05-30 15:15:17 +05302851static int genpd_perf_state_show(struct seq_file *s, void *data)
2852{
2853 struct generic_pm_domain *genpd = s->private;
2854
2855 if (genpd_lock_interruptible(genpd))
2856 return -ERESTARTSYS;
2857
2858 seq_printf(s, "%u\n", genpd->performance_state);
2859
2860 genpd_unlock(genpd);
2861 return 0;
2862}
2863
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002864#define define_genpd_open_function(name) \
2865static int genpd_##name##_open(struct inode *inode, struct file *file) \
2866{ \
2867 return single_open(file, genpd_##name##_show, inode->i_private); \
2868}
2869
2870define_genpd_open_function(summary);
2871define_genpd_open_function(status);
2872define_genpd_open_function(sub_domains);
2873define_genpd_open_function(idle_states);
2874define_genpd_open_function(active_time);
2875define_genpd_open_function(total_idle_time);
2876define_genpd_open_function(devices);
Rajendra Nayake8912812018-05-30 15:15:17 +05302877define_genpd_open_function(perf_state);
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002878
2879#define define_genpd_debugfs_fops(name) \
2880static const struct file_operations genpd_##name##_fops = { \
2881 .open = genpd_##name##_open, \
2882 .read = seq_read, \
2883 .llseek = seq_lseek, \
2884 .release = single_release, \
2885}
2886
2887define_genpd_debugfs_fops(summary);
2888define_genpd_debugfs_fops(status);
2889define_genpd_debugfs_fops(sub_domains);
2890define_genpd_debugfs_fops(idle_states);
2891define_genpd_debugfs_fops(active_time);
2892define_genpd_debugfs_fops(total_idle_time);
2893define_genpd_debugfs_fops(devices);
Rajendra Nayake8912812018-05-30 15:15:17 +05302894define_genpd_debugfs_fops(perf_state);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002895
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002896static int __init genpd_debug_init(void)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002897{
2898 struct dentry *d;
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002899 struct generic_pm_domain *genpd;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002900
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002901 genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002902
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002903 if (!genpd_debugfs_dir)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002904 return -ENOMEM;
2905
2906 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002907 genpd_debugfs_dir, NULL, &genpd_summary_fops);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002908 if (!d)
2909 return -ENOMEM;
2910
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002911 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002912 d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002913 if (!d)
2914 return -ENOMEM;
2915
2916 debugfs_create_file("current_state", 0444,
2917 d, genpd, &genpd_status_fops);
2918 debugfs_create_file("sub_domains", 0444,
2919 d, genpd, &genpd_sub_domains_fops);
2920 debugfs_create_file("idle_states", 0444,
2921 d, genpd, &genpd_idle_states_fops);
2922 debugfs_create_file("active_time", 0444,
2923 d, genpd, &genpd_active_time_fops);
2924 debugfs_create_file("total_idle_time", 0444,
2925 d, genpd, &genpd_total_idle_time_fops);
2926 debugfs_create_file("devices", 0444,
2927 d, genpd, &genpd_devices_fops);
Rajendra Nayake8912812018-05-30 15:15:17 +05302928 if (genpd->set_performance_state)
2929 debugfs_create_file("perf_state", 0444,
2930 d, genpd, &genpd_perf_state_fops);
Thara Gopinathb6a1d092017-07-14 13:10:16 -04002931 }
2932
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002933 return 0;
2934}
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002935late_initcall(genpd_debug_init);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002936
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002937static void __exit genpd_debug_exit(void)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002938{
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002939 debugfs_remove_recursive(genpd_debugfs_dir);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002940}
Ulf Hansson9e9704e2017-10-06 09:02:06 +02002941__exitcall(genpd_debug_exit);
Jon Hunter8b0510b2016-08-11 11:40:05 +01002942#endif /* CONFIG_DEBUG_FS */