blob: 058c8b6d2871ca6687148b84b7fa924377362f3d [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>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020013#include <linux/pm_runtime.h>
14#include <linux/pm_domain.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020015#include <linux/pm_qos.h>
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010016#include <linux/pm_clock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020017#include <linux/slab.h>
18#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020019#include <linux/sched.h>
20#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010021#include <linux/export.h>
22
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +010023#include "power.h"
24
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +020025#define GENPD_RETRY_MAX_MS 250 /* Approximate */
26
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010027#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
28({ \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
31 \
32 __routine = genpd->dev_ops.callback; \
33 if (__routine) { \
34 __ret = __routine(dev); \
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010035 } \
36 __ret; \
37})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020038
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020039static LIST_HEAD(gpd_list);
40static DEFINE_MUTEX(gpd_list_lock);
41
Russell King446d999c2015-03-20 17:20:33 +000042/*
43 * Get the generic PM domain for a particular struct device.
44 * This validates the struct device pointer, the PM domain pointer,
45 * and checks that the PM domain pointer is a real generic PM domain.
46 * Any failure results in NULL being returned.
47 */
48struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
49{
50 struct generic_pm_domain *genpd = NULL, *gpd;
51
52 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
53 return NULL;
54
55 mutex_lock(&gpd_list_lock);
56 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
57 if (&gpd->domain == dev->pm_domain) {
58 genpd = gpd;
59 break;
60 }
61 }
62 mutex_unlock(&gpd_list_lock);
63
64 return genpd;
65}
66
67/*
68 * This should only be used where we are certain that the pm_domain
69 * attached to the device is a genpd domain.
70 */
71static struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020072{
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
75
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020076 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020077}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020078
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020079static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010080{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020081 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010082}
83
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020084static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010085{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020086 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010087}
88
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020089static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020090{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020091 bool ret = false;
92
93 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
94 ret = !!atomic_dec_and_test(&genpd->sd_count);
95
96 return ret;
97}
98
99static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
100{
101 atomic_inc(&genpd->sd_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100102 smp_mb__after_atomic();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200103}
104
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200105static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100106{
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100107 unsigned int state_idx = genpd->state_idx;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100108 ktime_t time_start;
109 s64 elapsed_ns;
110 int ret;
111
112 if (!genpd->power_on)
113 return 0;
114
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200115 if (!timed)
116 return genpd->power_on(genpd);
117
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100118 time_start = ktime_get();
119 ret = genpd->power_on(genpd);
120 if (ret)
121 return ret;
122
123 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100124 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100125 return ret;
126
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100127 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100128 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000129 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
130 genpd->name, "on", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100131
132 return ret;
133}
134
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200135static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100136{
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100137 unsigned int state_idx = genpd->state_idx;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100138 ktime_t time_start;
139 s64 elapsed_ns;
140 int ret;
141
142 if (!genpd->power_off)
143 return 0;
144
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200145 if (!timed)
146 return genpd->power_off(genpd);
147
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100148 time_start = ktime_get();
149 ret = genpd->power_off(genpd);
150 if (ret == -EBUSY)
151 return ret;
152
153 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100154 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100155 return ret;
156
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100157 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100158 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000159 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
160 genpd->name, "off", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100161
162 return ret;
163}
164
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200165/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200166 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
Moritz Fischera3d09c72016-01-27 08:29:27 +0100167 * @genpd: PM domain to power off.
Ulf Hansson29e47e22015-09-02 10:16:13 +0200168 *
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200169 * Queue up the execution of genpd_poweroff() unless it's already been done
Ulf Hansson29e47e22015-09-02 10:16:13 +0200170 * before.
171 */
172static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
173{
174 queue_work(pm_wq, &genpd->power_off_work);
175}
176
177/**
Ulf Hansson53af16f2016-02-01 14:52:41 +0100178 * genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200179 * @genpd: PM domain to power up.
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100180 * @depth: nesting count for lockdep.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200181 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200182 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200183 * resume a device belonging to it.
184 */
Ulf Hansson53af16f2016-02-01 14:52:41 +0100185static int genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200186{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200187 struct gpd_link *link;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200188 int ret = 0;
189
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200190 if (genpd->status == GPD_STATE_ACTIVE)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200191 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200192
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200193 /*
194 * The list is guaranteed not to change while the loop below is being
195 * executed, unless one of the masters' .power_on() callbacks fiddles
196 * with it.
197 */
198 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100199 struct generic_pm_domain *master = link->master;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200200
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100201 genpd_sd_counter_inc(master);
202
203 mutex_lock_nested(&master->lock, depth + 1);
Ulf Hansson53af16f2016-02-01 14:52:41 +0100204 ret = genpd_poweron(master, depth + 1);
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100205 mutex_unlock(&master->lock);
206
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200207 if (ret) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100208 genpd_sd_counter_dec(master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200209 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200210 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200211 }
212
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200213 ret = genpd_power_on(genpd, true);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100214 if (ret)
215 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200216
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200217 genpd->status = GPD_STATE_ACTIVE;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200218 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200219
220 err:
Ulf Hansson29e47e22015-09-02 10:16:13 +0200221 list_for_each_entry_continue_reverse(link,
222 &genpd->slave_links,
223 slave_node) {
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200224 genpd_sd_counter_dec(link->master);
Ulf Hansson29e47e22015-09-02 10:16:13 +0200225 genpd_queue_power_off_work(link->master);
226 }
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200227
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200228 return ret;
229}
230
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200231static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
232 unsigned long val, void *ptr)
233{
234 struct generic_pm_domain_data *gpd_data;
235 struct device *dev;
236
237 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200238 dev = gpd_data->base.dev;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200239
240 for (;;) {
241 struct generic_pm_domain *genpd;
242 struct pm_domain_data *pdd;
243
244 spin_lock_irq(&dev->power.lock);
245
246 pdd = dev->power.subsys_data ?
247 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200248 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200249 to_gpd_data(pdd)->td.constraint_changed = true;
250 genpd = dev_to_genpd(dev);
251 } else {
252 genpd = ERR_PTR(-ENODATA);
253 }
254
255 spin_unlock_irq(&dev->power.lock);
256
257 if (!IS_ERR(genpd)) {
258 mutex_lock(&genpd->lock);
259 genpd->max_off_time_changed = true;
260 mutex_unlock(&genpd->lock);
261 }
262
263 dev = dev->parent;
264 if (!dev || dev->power.ignore_children)
265 break;
266 }
267
268 return NOTIFY_DONE;
269}
270
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200271/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200272 * genpd_poweroff - Remove power from a given PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200273 * @genpd: PM domain to power down.
Ulf Hanssonf96b3c42015-09-29 15:12:37 +0200274 * @is_async: PM domain is powered down from a scheduled work
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200275 *
276 * If all of the @genpd's devices have been suspended and all of its subdomains
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200277 * have been powered down, remove power from @genpd.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200278 */
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200279static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200280{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200281 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200282 struct gpd_link *link;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200283 unsigned int not_suspended = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200284
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200285 /*
286 * Do not try to power off the domain in the following situations:
287 * (1) The domain is already in the "power off" state.
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200288 * (2) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200289 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200290 if (genpd->status == GPD_STATE_POWER_OFF
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200291 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200292 return 0;
293
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200294 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200295 return -EBUSY;
296
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200297 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
298 enum pm_qos_flags_status stat;
299
300 stat = dev_pm_qos_flags(pdd->dev,
301 PM_QOS_FLAG_NO_POWER_OFF
302 | PM_QOS_FLAG_REMOTE_WAKEUP);
303 if (stat > PM_QOS_FLAGS_NONE)
304 return -EBUSY;
305
Lina Iyer298cd0f2015-10-28 15:18:59 -0600306 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200307 not_suspended++;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200308 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200309
Ulf Hanssonf96b3c42015-09-29 15:12:37 +0200310 if (not_suspended > 1 || (not_suspended == 1 && is_async))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200311 return -EBUSY;
312
313 if (genpd->gov && genpd->gov->power_down_ok) {
314 if (!genpd->gov->power_down_ok(&genpd->domain))
315 return -EAGAIN;
316 }
317
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200318 if (genpd->power_off) {
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200319 int ret;
320
321 if (atomic_read(&genpd->sd_count) > 0)
322 return -EBUSY;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200323
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200324 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200325 * If sd_count > 0 at this point, one of the subdomains hasn't
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200326 * managed to call genpd_poweron() for the master yet after
327 * incrementing it. In that case genpd_poweron() will wait
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200328 * for us to drop the lock, so we can call .power_off() and let
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200329 * the genpd_poweron() restore power for us (this shouldn't
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200330 * happen very often).
331 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200332 ret = genpd_power_off(genpd, true);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200333 if (ret)
334 return ret;
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200335 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200336
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200337 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100338
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200339 list_for_each_entry(link, &genpd->slave_links, slave_node) {
340 genpd_sd_counter_dec(link->master);
341 genpd_queue_power_off_work(link->master);
342 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200343
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200344 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200345}
346
347/**
348 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
349 * @work: Work structure used for scheduling the execution of this function.
350 */
351static void genpd_power_off_work_fn(struct work_struct *work)
352{
353 struct generic_pm_domain *genpd;
354
355 genpd = container_of(work, struct generic_pm_domain, power_off_work);
356
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200357 mutex_lock(&genpd->lock);
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200358 genpd_poweroff(genpd, true);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200359 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200360}
361
362/**
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200363 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
364 * @dev: Device to handle.
365 */
366static int __genpd_runtime_suspend(struct device *dev)
367{
368 int (*cb)(struct device *__dev);
369
370 if (dev->type && dev->type->pm)
371 cb = dev->type->pm->runtime_suspend;
372 else if (dev->class && dev->class->pm)
373 cb = dev->class->pm->runtime_suspend;
374 else if (dev->bus && dev->bus->pm)
375 cb = dev->bus->pm->runtime_suspend;
376 else
377 cb = NULL;
378
379 if (!cb && dev->driver && dev->driver->pm)
380 cb = dev->driver->pm->runtime_suspend;
381
382 return cb ? cb(dev) : 0;
383}
384
385/**
386 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
387 * @dev: Device to handle.
388 */
389static int __genpd_runtime_resume(struct device *dev)
390{
391 int (*cb)(struct device *__dev);
392
393 if (dev->type && dev->type->pm)
394 cb = dev->type->pm->runtime_resume;
395 else if (dev->class && dev->class->pm)
396 cb = dev->class->pm->runtime_resume;
397 else if (dev->bus && dev->bus->pm)
398 cb = dev->bus->pm->runtime_resume;
399 else
400 cb = NULL;
401
402 if (!cb && dev->driver && dev->driver->pm)
403 cb = dev->driver->pm->runtime_resume;
404
405 return cb ? cb(dev) : 0;
406}
407
408/**
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200409 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200410 * @dev: Device to suspend.
411 *
412 * Carry out a runtime suspend of a device under the assumption that its
413 * pm_domain field points to the domain member of an object of type
414 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
415 */
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200416static int genpd_runtime_suspend(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200417{
418 struct generic_pm_domain *genpd;
Ulf Hansson9df39212016-03-31 11:21:25 +0200419 bool (*suspend_ok)(struct device *__dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200420 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100421 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200422 ktime_t time_start;
423 s64 elapsed_ns;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100424 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200425
426 dev_dbg(dev, "%s()\n", __func__);
427
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200428 genpd = dev_to_genpd(dev);
429 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200430 return -EINVAL;
431
Ulf Hanssonffe12852015-11-30 16:21:38 +0100432 /*
433 * A runtime PM centric subsystem/driver may re-use the runtime PM
434 * callbacks for other purposes than runtime PM. In those scenarios
435 * runtime PM is disabled. Under these circumstances, we shall skip
436 * validating/measuring the PM QoS latency.
437 */
Ulf Hansson9df39212016-03-31 11:21:25 +0200438 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
439 if (runtime_pm && suspend_ok && !suspend_ok(dev))
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100440 return -EBUSY;
441
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200442 /* Measure suspend latency. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100443 if (runtime_pm)
444 time_start = ktime_get();
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200445
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200446 ret = __genpd_runtime_suspend(dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100447 if (ret)
448 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200449
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200450 ret = genpd_stop_dev(genpd, dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200451 if (ret) {
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200452 __genpd_runtime_resume(dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200453 return ret;
454 }
455
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200456 /* Update suspend latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100457 if (runtime_pm) {
458 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
459 if (elapsed_ns > td->suspend_latency_ns) {
460 td->suspend_latency_ns = elapsed_ns;
461 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
462 elapsed_ns);
463 genpd->max_off_time_changed = true;
464 td->constraint_changed = true;
465 }
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200466 }
467
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200468 /*
469 * If power.irq_safe is set, this routine will be run with interrupts
470 * off, so it can't use mutexes.
471 */
472 if (dev->power.irq_safe)
473 return 0;
474
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200475 mutex_lock(&genpd->lock);
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200476 genpd_poweroff(genpd, false);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200477 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200478
479 return 0;
480}
481
482/**
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200483 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200484 * @dev: Device to resume.
485 *
486 * Carry out a runtime resume of a device under the assumption that its
487 * pm_domain field points to the domain member of an object of type
488 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
489 */
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200490static int genpd_runtime_resume(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200491{
492 struct generic_pm_domain *genpd;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200493 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100494 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200495 ktime_t time_start;
496 s64 elapsed_ns;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200497 int ret;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200498 bool timed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200499
500 dev_dbg(dev, "%s()\n", __func__);
501
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200502 genpd = dev_to_genpd(dev);
503 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200504 return -EINVAL;
505
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200506 /* If power.irq_safe, the PM domain is never powered off. */
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200507 if (dev->power.irq_safe) {
508 timed = false;
509 goto out;
510 }
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200511
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200512 mutex_lock(&genpd->lock);
Ulf Hansson53af16f2016-02-01 14:52:41 +0100513 ret = genpd_poweron(genpd, 0);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200514 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200515
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200516 if (ret)
517 return ret;
518
519 out:
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200520 /* Measure resume latency. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100521 if (timed && runtime_pm)
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200522 time_start = ktime_get();
523
Laurent Pinchart076395c2016-03-02 01:20:38 +0200524 ret = genpd_start_dev(genpd, dev);
525 if (ret)
526 goto err_poweroff;
527
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200528 ret = __genpd_runtime_resume(dev);
Laurent Pinchart076395c2016-03-02 01:20:38 +0200529 if (ret)
530 goto err_stop;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200531
532 /* Update resume latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100533 if (timed && runtime_pm) {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200534 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
535 if (elapsed_ns > td->resume_latency_ns) {
536 td->resume_latency_ns = elapsed_ns;
537 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
538 elapsed_ns);
539 genpd->max_off_time_changed = true;
540 td->constraint_changed = true;
541 }
542 }
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200543
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200544 return 0;
Laurent Pinchart076395c2016-03-02 01:20:38 +0200545
546err_stop:
547 genpd_stop_dev(genpd, dev);
548err_poweroff:
549 if (!dev->power.irq_safe) {
550 mutex_lock(&genpd->lock);
551 genpd_poweroff(genpd, 0);
552 mutex_unlock(&genpd->lock);
553 }
554
555 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200556}
557
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530558static bool pd_ignore_unused;
559static int __init pd_ignore_unused_setup(char *__unused)
560{
561 pd_ignore_unused = true;
562 return 1;
563}
564__setup("pd_ignore_unused", pd_ignore_unused_setup);
565
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200566/**
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200567 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200568 */
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200569static int __init genpd_poweroff_unused(void)
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200570{
571 struct generic_pm_domain *genpd;
572
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530573 if (pd_ignore_unused) {
574 pr_warn("genpd: Not disabling unused power domains\n");
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200575 return 0;
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530576 }
577
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200578 mutex_lock(&gpd_list_lock);
579
580 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
581 genpd_queue_power_off_work(genpd);
582
583 mutex_unlock(&gpd_list_lock);
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200584
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200585 return 0;
586}
587late_initcall(genpd_poweroff_unused);
588
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200589#ifdef CONFIG_PM_SLEEP
590
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200591/**
592 * pm_genpd_present - Check if the given PM domain has been initialized.
593 * @genpd: PM domain to check.
594 */
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100595static bool pm_genpd_present(const struct generic_pm_domain *genpd)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200596{
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100597 const struct generic_pm_domain *gpd;
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200598
599 if (IS_ERR_OR_NULL(genpd))
600 return false;
601
602 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
603 if (gpd == genpd)
604 return true;
605
606 return false;
607}
608
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100609static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
610 struct device *dev)
611{
612 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
613}
614
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200615/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200616 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200617 * @genpd: PM domain to power off, if possible.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200618 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200619 *
620 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200621 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200622 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200623 * This function is only called in "noirq" and "syscore" stages of system power
624 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
625 * executed sequentially, so it is guaranteed that it will never run twice in
626 * parallel).
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200627 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200628static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
629 bool timed)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200630{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200631 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200632
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200633 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200634 return;
635
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200636 if (genpd->suspended_count != genpd->device_count
637 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200638 return;
639
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100640 /* Choose the deepest state when suspending */
641 genpd->state_idx = genpd->state_count - 1;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200642 genpd_power_off(genpd, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200643
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200644 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200645
646 list_for_each_entry(link, &genpd->slave_links, slave_node) {
647 genpd_sd_counter_dec(link->master);
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200648 pm_genpd_sync_poweroff(link->master, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200649 }
650}
651
652/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200653 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
654 * @genpd: PM domain to power on.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200655 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200656 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200657 * This function is only called in "noirq" and "syscore" stages of system power
658 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
659 * executed sequentially, so it is guaranteed that it will never run twice in
660 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200661 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200662static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
663 bool timed)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200664{
665 struct gpd_link *link;
666
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200667 if (genpd->status == GPD_STATE_ACTIVE)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200668 return;
669
670 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200671 pm_genpd_sync_poweron(link->master, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200672 genpd_sd_counter_inc(link->master);
673 }
674
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200675 genpd_power_on(genpd, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200676
677 genpd->status = GPD_STATE_ACTIVE;
678}
679
680/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200681 * resume_needed - Check whether to resume a device before system suspend.
682 * @dev: Device to check.
683 * @genpd: PM domain the device belongs to.
684 *
685 * There are two cases in which a device that can wake up the system from sleep
686 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
687 * to wake up the system and it has to remain active for this purpose while the
688 * system is in the sleep state and (2) if the device is not enabled to wake up
689 * the system from sleep states and it generally doesn't generate wakeup signals
690 * by itself (those signals are generated on its behalf by other parts of the
691 * system). In the latter case it may be necessary to reconfigure the device's
692 * wakeup settings during system suspend, because it may have been set up to
693 * signal remote wakeup from the system's working state as needed by runtime PM.
694 * Return 'true' in either of the above cases.
695 */
696static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
697{
698 bool active_wakeup;
699
700 if (!device_can_wakeup(dev))
701 return false;
702
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100703 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200704 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
705}
706
707/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200708 * pm_genpd_prepare - Start power transition of a device in a PM domain.
709 * @dev: Device to start the transition of.
710 *
711 * Start a power transition of a device (during a system-wide power transition)
712 * under the assumption that its pm_domain field points to the domain member of
713 * an object of type struct generic_pm_domain representing a PM domain
714 * consisting of I/O devices.
715 */
716static int pm_genpd_prepare(struct device *dev)
717{
718 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200719 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200720
721 dev_dbg(dev, "%s()\n", __func__);
722
723 genpd = dev_to_genpd(dev);
724 if (IS_ERR(genpd))
725 return -EINVAL;
726
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200727 /*
728 * If a wakeup request is pending for the device, it should be woken up
729 * at this point and a system wakeup event should be reported if it's
730 * set up to wake up the system from sleep states.
731 */
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200732 if (resume_needed(dev, genpd))
733 pm_runtime_resume(dev);
734
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200735 mutex_lock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200736
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200737 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100738 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200739
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200740 mutex_unlock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200741
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200742 ret = pm_generic_prepare(dev);
743 if (ret) {
744 mutex_lock(&genpd->lock);
745
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200746 genpd->prepared_count--;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200747
748 mutex_unlock(&genpd->lock);
749 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200750
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200751 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200752}
753
754/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100755 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
756 * @dev: Device to suspend.
757 *
758 * Stop the device and remove power from the domain if all devices in it have
759 * been stopped.
760 */
761static int pm_genpd_suspend_noirq(struct device *dev)
762{
763 struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +0200764 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200765
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100766 dev_dbg(dev, "%s()\n", __func__);
767
768 genpd = dev_to_genpd(dev);
769 if (IS_ERR(genpd))
770 return -EINVAL;
771
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200772 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200773 return 0;
774
Ulf Hansson122a2232016-05-30 11:33:14 +0200775 if (genpd->dev_ops.stop && genpd->dev_ops.start) {
776 ret = pm_runtime_force_suspend(dev);
777 if (ret)
778 return ret;
779 }
780
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200781 /*
782 * Since all of the "noirq" callbacks are executed sequentially, it is
783 * guaranteed that this function will never run twice in parallel for
784 * the same PM domain, so it is not necessary to use locking here.
785 */
786 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200787 pm_genpd_sync_poweroff(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200788
789 return 0;
790}
791
792/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100793 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200794 * @dev: Device to resume.
795 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100796 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200797 */
798static int pm_genpd_resume_noirq(struct device *dev)
799{
800 struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +0200801 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200802
803 dev_dbg(dev, "%s()\n", __func__);
804
805 genpd = dev_to_genpd(dev);
806 if (IS_ERR(genpd))
807 return -EINVAL;
808
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200809 if (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200810 return 0;
811
812 /*
813 * Since all of the "noirq" callbacks are executed sequentially, it is
814 * guaranteed that this function will never run twice in parallel for
815 * the same PM domain, so it is not necessary to use locking here.
816 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200817 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200818 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200819
Ulf Hansson122a2232016-05-30 11:33:14 +0200820 if (genpd->dev_ops.stop && genpd->dev_ops.start)
821 ret = pm_runtime_force_resume(dev);
822
823 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200824}
825
826/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100827 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200828 * @dev: Device to freeze.
829 *
830 * Carry out a late freeze of a device under the assumption that its
831 * pm_domain field points to the domain member of an object of type
832 * struct generic_pm_domain representing a power domain consisting of I/O
833 * devices.
834 */
835static int pm_genpd_freeze_noirq(struct device *dev)
836{
837 struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +0200838 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200839
840 dev_dbg(dev, "%s()\n", __func__);
841
842 genpd = dev_to_genpd(dev);
843 if (IS_ERR(genpd))
844 return -EINVAL;
845
Ulf Hansson122a2232016-05-30 11:33:14 +0200846 if (genpd->dev_ops.stop && genpd->dev_ops.start)
847 ret = pm_runtime_force_suspend(dev);
848
849 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200850}
851
852/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100853 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200854 * @dev: Device to thaw.
855 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100856 * Start the device, unless power has been removed from the domain already
857 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200858 */
859static int pm_genpd_thaw_noirq(struct device *dev)
860{
861 struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +0200862 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200863
864 dev_dbg(dev, "%s()\n", __func__);
865
866 genpd = dev_to_genpd(dev);
867 if (IS_ERR(genpd))
868 return -EINVAL;
869
Ulf Hansson122a2232016-05-30 11:33:14 +0200870 if (genpd->dev_ops.stop && genpd->dev_ops.start)
871 ret = pm_runtime_force_resume(dev);
872
873 return ret;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100874}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200875
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100876/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100877 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200878 * @dev: Device to resume.
879 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100880 * Make sure the domain will be in the same power state as before the
881 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200882 */
883static int pm_genpd_restore_noirq(struct device *dev)
884{
885 struct generic_pm_domain *genpd;
Ulf Hansson122a2232016-05-30 11:33:14 +0200886 int ret = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200887
888 dev_dbg(dev, "%s()\n", __func__);
889
890 genpd = dev_to_genpd(dev);
891 if (IS_ERR(genpd))
892 return -EINVAL;
893
894 /*
895 * Since all of the "noirq" callbacks are executed sequentially, it is
896 * guaranteed that this function will never run twice in parallel for
897 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100898 *
899 * At this point suspended_count == 0 means we are being run for the
900 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200901 */
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200902 if (genpd->suspended_count++ == 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200903 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100904 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200905 * so make it appear as powered off to pm_genpd_sync_poweron(),
906 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200907 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100908 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +0100909
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200910 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200911
Ulf Hansson122a2232016-05-30 11:33:14 +0200912 if (genpd->dev_ops.stop && genpd->dev_ops.start)
913 ret = pm_runtime_force_resume(dev);
914
915 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200916}
917
918/**
919 * pm_genpd_complete - Complete power transition of a device in a power domain.
920 * @dev: Device to complete the transition of.
921 *
922 * Complete a power transition of a device (during a system-wide power
923 * transition) under the assumption that its pm_domain field points to the
924 * domain member of an object of type struct generic_pm_domain representing
925 * a power domain consisting of I/O devices.
926 */
927static void pm_genpd_complete(struct device *dev)
928{
929 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200930
931 dev_dbg(dev, "%s()\n", __func__);
932
933 genpd = dev_to_genpd(dev);
934 if (IS_ERR(genpd))
935 return;
936
Ulf Hansson4d23a5e2016-05-30 11:33:13 +0200937 pm_generic_complete(dev);
938
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200939 mutex_lock(&genpd->lock);
940
Ulf Hansson39dd0f22016-05-30 11:43:07 +0200941 genpd->prepared_count--;
Ulf Hansson4d23a5e2016-05-30 11:33:13 +0200942 if (!genpd->prepared_count)
943 genpd_queue_power_off_work(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200944
945 mutex_unlock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200946}
947
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200948/**
Ulf Hanssond47e6462014-09-03 12:52:24 +0200949 * genpd_syscore_switch - Switch power during system core suspend or resume.
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200950 * @dev: Device that normally is marked as "always on" to switch power for.
951 *
952 * This routine may only be called during the system core (syscore) suspend or
953 * resume phase for devices whose "always on" flags are set.
954 */
Ulf Hanssond47e6462014-09-03 12:52:24 +0200955static void genpd_syscore_switch(struct device *dev, bool suspend)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200956{
957 struct generic_pm_domain *genpd;
958
959 genpd = dev_to_genpd(dev);
960 if (!pm_genpd_present(genpd))
961 return;
962
963 if (suspend) {
964 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200965 pm_genpd_sync_poweroff(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200966 } else {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200967 pm_genpd_sync_poweron(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200968 genpd->suspended_count--;
969 }
970}
Ulf Hanssond47e6462014-09-03 12:52:24 +0200971
972void pm_genpd_syscore_poweroff(struct device *dev)
973{
974 genpd_syscore_switch(dev, true);
975}
976EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
977
978void pm_genpd_syscore_poweron(struct device *dev)
979{
980 genpd_syscore_switch(dev, false);
981}
982EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200983
Rafael J. Wysockid30d8192014-11-27 22:38:05 +0100984#else /* !CONFIG_PM_SLEEP */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200985
986#define pm_genpd_prepare NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200987#define pm_genpd_suspend_noirq NULL
988#define pm_genpd_resume_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200989#define pm_genpd_freeze_noirq NULL
990#define pm_genpd_thaw_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200991#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200992#define pm_genpd_complete NULL
993
994#endif /* CONFIG_PM_SLEEP */
995
Ulf Hanssonf104e1e2015-01-27 21:13:44 +0100996static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
997 struct generic_pm_domain *genpd,
998 struct gpd_timing_data *td)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200999{
1000 struct generic_pm_domain_data *gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001001 int ret;
1002
1003 ret = dev_pm_get_subsys_data(dev);
1004 if (ret)
1005 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001006
1007 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
Ulf Hansson3e235682015-01-27 21:13:43 +01001008 if (!gpd_data) {
1009 ret = -ENOMEM;
1010 goto err_put;
1011 }
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001012
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001013 if (td)
1014 gpd_data->td = *td;
1015
1016 gpd_data->base.dev = dev;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001017 gpd_data->td.constraint_changed = true;
1018 gpd_data->td.effective_constraint_ns = -1;
1019 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1020
1021 spin_lock_irq(&dev->power.lock);
1022
1023 if (dev->power.subsys_data->domain_data) {
1024 ret = -EINVAL;
1025 goto err_free;
1026 }
1027
1028 dev->power.subsys_data->domain_data = &gpd_data->base;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001029
1030 spin_unlock_irq(&dev->power.lock);
1031
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001032 dev_pm_domain_set(dev, &genpd->domain);
1033
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001034 return gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001035
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001036 err_free:
1037 spin_unlock_irq(&dev->power.lock);
1038 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001039 err_put:
1040 dev_pm_put_subsys_data(dev);
1041 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001042}
1043
Ulf Hansson49d400c2015-01-27 21:13:38 +01001044static void genpd_free_dev_data(struct device *dev,
1045 struct generic_pm_domain_data *gpd_data)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001046{
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001047 dev_pm_domain_set(dev, NULL);
1048
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001049 spin_lock_irq(&dev->power.lock);
1050
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001051 dev->power.subsys_data->domain_data = NULL;
1052
1053 spin_unlock_irq(&dev->power.lock);
1054
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001055 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001056 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001057}
1058
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001059/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001060 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001061 * @genpd: PM domain to add the device to.
1062 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001063 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001064 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001065int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1066 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001067{
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001068 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001069 int ret = 0;
1070
1071 dev_dbg(dev, "%s()\n", __func__);
1072
1073 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1074 return -EINVAL;
1075
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001076 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
Ulf Hansson3e235682015-01-27 21:13:43 +01001077 if (IS_ERR(gpd_data))
1078 return PTR_ERR(gpd_data);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001079
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001080 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001081
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001082 if (genpd->prepared_count > 0) {
1083 ret = -EAGAIN;
1084 goto out;
1085 }
1086
Ulf Hanssonb472c2f2015-01-27 21:13:45 +01001087 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1088 if (ret)
1089 goto out;
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001090
Ulf Hansson14b53062015-01-27 21:13:40 +01001091 genpd->device_count++;
1092 genpd->max_off_time_changed = true;
1093
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001094 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001095
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001096 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001097 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001098
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001099 if (ret)
1100 genpd_free_dev_data(dev, gpd_data);
1101 else
1102 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001103
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001104 return ret;
1105}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301106EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001107
1108/**
1109 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1110 * @genpd: PM domain to remove the device from.
1111 * @dev: Device to be removed.
1112 */
1113int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1114 struct device *dev)
1115{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001116 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001117 struct pm_domain_data *pdd;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001118 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001119
1120 dev_dbg(dev, "%s()\n", __func__);
1121
Russell Kingdf6a0d62015-03-20 17:20:38 +00001122 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001123 return -EINVAL;
1124
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001125 /* The above validation also means we have existing domain_data. */
1126 pdd = dev->power.subsys_data->domain_data;
1127 gpd_data = to_gpd_data(pdd);
1128 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1129
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001130 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001131
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001132 if (genpd->prepared_count > 0) {
1133 ret = -EAGAIN;
1134 goto out;
1135 }
1136
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001137 genpd->device_count--;
1138 genpd->max_off_time_changed = true;
1139
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001140 if (genpd->detach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001141 genpd->detach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001142
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001143 list_del_init(&pdd->list_node);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001144
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001145 mutex_unlock(&genpd->lock);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001146
Ulf Hanssonc1dbe2f2015-01-27 21:13:39 +01001147 genpd_free_dev_data(dev, gpd_data);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001148
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001149 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001150
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001151 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001152 mutex_unlock(&genpd->lock);
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001153 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001154
1155 return ret;
1156}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301157EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001158
1159/**
1160 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1161 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001162 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001163 */
1164int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001165 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001166{
Lina Iyer25479232015-10-28 15:19:50 -06001167 struct gpd_link *link, *itr;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001168 int ret = 0;
1169
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001170 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1171 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001172 return -EINVAL;
1173
Lina Iyer25479232015-10-28 15:19:50 -06001174 link = kzalloc(sizeof(*link), GFP_KERNEL);
1175 if (!link)
1176 return -ENOMEM;
1177
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001178 mutex_lock(&subdomain->lock);
1179 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001180
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001181 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001182 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001183 ret = -EINVAL;
1184 goto out;
1185 }
1186
Lina Iyer25479232015-10-28 15:19:50 -06001187 list_for_each_entry(itr, &genpd->master_links, master_node) {
1188 if (itr->slave == subdomain && itr->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001189 ret = -EINVAL;
1190 goto out;
1191 }
1192 }
1193
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001194 link->master = genpd;
1195 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001196 link->slave = subdomain;
1197 list_add_tail(&link->slave_node, &subdomain->slave_links);
1198 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001199 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001200
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001201 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001202 mutex_unlock(&genpd->lock);
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001203 mutex_unlock(&subdomain->lock);
Lina Iyer25479232015-10-28 15:19:50 -06001204 if (ret)
1205 kfree(link);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001206 return ret;
1207}
Stephen Boydd60ee962015-10-01 12:22:53 -07001208EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001209
1210/**
1211 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1212 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001213 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001214 */
1215int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001216 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001217{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001218 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001219 int ret = -EINVAL;
1220
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001221 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001222 return -EINVAL;
1223
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001224 mutex_lock(&subdomain->lock);
1225 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001226
Jon Hunterbeda5fc2016-03-04 10:55:14 +00001227 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
Jon Hunter30e7a652015-09-03 09:10:37 +01001228 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1229 subdomain->name);
1230 ret = -EBUSY;
1231 goto out;
1232 }
1233
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001234 list_for_each_entry(link, &genpd->master_links, master_node) {
1235 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001236 continue;
1237
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001238 list_del(&link->master_node);
1239 list_del(&link->slave_node);
1240 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001241 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001242 genpd_sd_counter_dec(genpd);
1243
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001244 ret = 0;
1245 break;
1246 }
1247
Jon Hunter30e7a652015-09-03 09:10:37 +01001248out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001249 mutex_unlock(&genpd->lock);
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001250 mutex_unlock(&subdomain->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001251
1252 return ret;
1253}
Stephen Boydd60ee962015-10-01 12:22:53 -07001254EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001255
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001256/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001257 * pm_genpd_init - Initialize a generic I/O PM domain object.
1258 * @genpd: PM domain object to initialize.
1259 * @gov: PM domain governor to associate with the domain (may be NULL).
1260 * @is_off: Initial value of the domain's power_is_off field.
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001261 *
1262 * Returns 0 on successful initialization, else a negative error code.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001263 */
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001264int pm_genpd_init(struct generic_pm_domain *genpd,
1265 struct dev_power_governor *gov, bool is_off)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001266{
1267 if (IS_ERR_OR_NULL(genpd))
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001268 return -EINVAL;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001269
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001270 INIT_LIST_HEAD(&genpd->master_links);
1271 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001272 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001273 mutex_init(&genpd->lock);
1274 genpd->gov = gov;
1275 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001276 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001277 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001278 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001279 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001280 genpd->max_off_time_changed = true;
Ulf Hansson795bd2e2016-03-31 11:21:26 +02001281 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1282 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001283 genpd->domain.ops.prepare = pm_genpd_prepare;
Ulf Hansson80018852016-05-30 11:43:08 +02001284 genpd->domain.ops.suspend = pm_generic_suspend;
1285 genpd->domain.ops.suspend_late = pm_generic_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001286 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1287 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Ulf Hansson80018852016-05-30 11:43:08 +02001288 genpd->domain.ops.resume_early = pm_generic_resume_early;
1289 genpd->domain.ops.resume = pm_generic_resume;
1290 genpd->domain.ops.freeze = pm_generic_freeze;
1291 genpd->domain.ops.freeze_late = pm_generic_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001292 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1293 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Ulf Hansson80018852016-05-30 11:43:08 +02001294 genpd->domain.ops.thaw_early = pm_generic_thaw_early;
1295 genpd->domain.ops.thaw = pm_generic_thaw;
1296 genpd->domain.ops.poweroff = pm_generic_poweroff;
1297 genpd->domain.ops.poweroff_late = pm_generic_poweroff_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001298 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001299 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Ulf Hansson80018852016-05-30 11:43:08 +02001300 genpd->domain.ops.restore_early = pm_generic_restore_early;
1301 genpd->domain.ops.restore = pm_generic_restore;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001302 genpd->domain.ops.complete = pm_genpd_complete;
Ulf Hanssonc11f6f52014-12-01 12:50:21 +01001303
1304 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1305 genpd->dev_ops.stop = pm_clk_suspend;
1306 genpd->dev_ops.start = pm_clk_resume;
1307 }
1308
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001309 if (genpd->state_idx >= GENPD_MAX_NUM_STATES) {
1310 pr_warn("Initial state index out of bounds.\n");
1311 genpd->state_idx = GENPD_MAX_NUM_STATES - 1;
1312 }
1313
1314 if (genpd->state_count > GENPD_MAX_NUM_STATES) {
1315 pr_warn("Limiting states to %d\n", GENPD_MAX_NUM_STATES);
1316 genpd->state_count = GENPD_MAX_NUM_STATES;
1317 }
1318
1319 /* Use only one "off" state if there were no states declared */
Axel Haslam90e63452016-02-15 11:10:53 +01001320 if (genpd->state_count == 0)
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001321 genpd->state_count = 1;
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001322
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +02001323 mutex_lock(&gpd_list_lock);
1324 list_add(&genpd->gpd_list_node, &gpd_list);
1325 mutex_unlock(&gpd_list_lock);
Ulf Hansson7eb231c2016-06-17 12:27:52 +02001326
1327 return 0;
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +02001328}
Rajendra Nayakbe5ed552015-08-13 11:51:57 +05301329EXPORT_SYMBOL_GPL(pm_genpd_init);
Tomasz Figaaa422402014-09-19 20:27:36 +02001330
1331#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1332/*
1333 * Device Tree based PM domain providers.
1334 *
1335 * The code below implements generic device tree based PM domain providers that
1336 * bind device tree nodes with generic PM domains registered in the system.
1337 *
1338 * Any driver that registers generic PM domains and needs to support binding of
1339 * devices to these domains is supposed to register a PM domain provider, which
1340 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1341 *
1342 * Two simple mapping functions have been provided for convenience:
1343 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1344 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1345 * index.
1346 */
1347
1348/**
1349 * struct of_genpd_provider - PM domain provider registration structure
1350 * @link: Entry in global list of PM domain providers
1351 * @node: Pointer to device tree node of PM domain provider
1352 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1353 * into a PM domain.
1354 * @data: context pointer to be passed into @xlate callback
1355 */
1356struct of_genpd_provider {
1357 struct list_head link;
1358 struct device_node *node;
1359 genpd_xlate_t xlate;
1360 void *data;
1361};
1362
1363/* List of registered PM domain providers. */
1364static LIST_HEAD(of_genpd_providers);
1365/* Mutex to protect the list above. */
1366static DEFINE_MUTEX(of_genpd_mutex);
1367
1368/**
1369 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1370 * @genpdspec: OF phandle args to map into a PM domain
1371 * @data: xlate function private data - pointer to struct generic_pm_domain
1372 *
1373 * This is a generic xlate function that can be used to model PM domains that
1374 * have their own device tree nodes. The private data of xlate function needs
1375 * to be a valid pointer to struct generic_pm_domain.
1376 */
1377struct generic_pm_domain *__of_genpd_xlate_simple(
1378 struct of_phandle_args *genpdspec,
1379 void *data)
1380{
1381 if (genpdspec->args_count != 0)
1382 return ERR_PTR(-EINVAL);
1383 return data;
1384}
1385EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1386
1387/**
1388 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1389 * @genpdspec: OF phandle args to map into a PM domain
1390 * @data: xlate function private data - pointer to struct genpd_onecell_data
1391 *
1392 * This is a generic xlate function that can be used to model simple PM domain
1393 * controllers that have one device tree node and provide multiple PM domains.
1394 * A single cell is used as an index into an array of PM domains specified in
1395 * the genpd_onecell_data struct when registering the provider.
1396 */
1397struct generic_pm_domain *__of_genpd_xlate_onecell(
1398 struct of_phandle_args *genpdspec,
1399 void *data)
1400{
1401 struct genpd_onecell_data *genpd_data = data;
1402 unsigned int idx = genpdspec->args[0];
1403
1404 if (genpdspec->args_count != 1)
1405 return ERR_PTR(-EINVAL);
1406
1407 if (idx >= genpd_data->num_domains) {
1408 pr_err("%s: invalid domain index %u\n", __func__, idx);
1409 return ERR_PTR(-EINVAL);
1410 }
1411
1412 if (!genpd_data->domains[idx])
1413 return ERR_PTR(-ENOENT);
1414
1415 return genpd_data->domains[idx];
1416}
1417EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1418
1419/**
1420 * __of_genpd_add_provider() - Register a PM domain provider for a node
1421 * @np: Device node pointer associated with the PM domain provider.
1422 * @xlate: Callback for decoding PM domain from phandle arguments.
1423 * @data: Context pointer for @xlate callback.
1424 */
1425int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1426 void *data)
1427{
1428 struct of_genpd_provider *cp;
1429
1430 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1431 if (!cp)
1432 return -ENOMEM;
1433
1434 cp->node = of_node_get(np);
1435 cp->data = data;
1436 cp->xlate = xlate;
1437
1438 mutex_lock(&of_genpd_mutex);
1439 list_add(&cp->link, &of_genpd_providers);
1440 mutex_unlock(&of_genpd_mutex);
1441 pr_debug("Added domain provider from %s\n", np->full_name);
1442
1443 return 0;
1444}
1445EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1446
1447/**
1448 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1449 * @np: Device node pointer associated with the PM domain provider
1450 */
1451void of_genpd_del_provider(struct device_node *np)
1452{
1453 struct of_genpd_provider *cp;
1454
1455 mutex_lock(&of_genpd_mutex);
1456 list_for_each_entry(cp, &of_genpd_providers, link) {
1457 if (cp->node == np) {
1458 list_del(&cp->link);
1459 of_node_put(cp->node);
1460 kfree(cp);
1461 break;
1462 }
1463 }
1464 mutex_unlock(&of_genpd_mutex);
1465}
1466EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1467
1468/**
1469 * of_genpd_get_from_provider() - Look-up PM domain
1470 * @genpdspec: OF phandle args to use for look-up
1471 *
1472 * Looks for a PM domain provider under the node specified by @genpdspec and if
1473 * found, uses xlate function of the provider to map phandle args to a PM
1474 * domain.
1475 *
1476 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1477 * on failure.
1478 */
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05301479struct generic_pm_domain *of_genpd_get_from_provider(
Tomasz Figaaa422402014-09-19 20:27:36 +02001480 struct of_phandle_args *genpdspec)
1481{
1482 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1483 struct of_genpd_provider *provider;
1484
Jon Hunter41795a82016-03-04 10:55:15 +00001485 if (!genpdspec)
1486 return ERR_PTR(-EINVAL);
1487
Tomasz Figaaa422402014-09-19 20:27:36 +02001488 mutex_lock(&of_genpd_mutex);
1489
1490 /* Check if we have such a provider in our array */
1491 list_for_each_entry(provider, &of_genpd_providers, link) {
1492 if (provider->node == genpdspec->np)
1493 genpd = provider->xlate(genpdspec, provider->data);
1494 if (!IS_ERR(genpd))
1495 break;
1496 }
1497
1498 mutex_unlock(&of_genpd_mutex);
1499
1500 return genpd;
1501}
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05301502EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
Tomasz Figaaa422402014-09-19 20:27:36 +02001503
1504/**
1505 * genpd_dev_pm_detach - Detach a device from its PM domain.
Jon Hunter8bb69442015-08-27 10:17:00 +01001506 * @dev: Device to detach.
Tomasz Figaaa422402014-09-19 20:27:36 +02001507 * @power_off: Currently not used
1508 *
1509 * Try to locate a corresponding generic PM domain, which the device was
1510 * attached to previously. If such is found, the device is detached from it.
1511 */
1512static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1513{
Russell King446d999c2015-03-20 17:20:33 +00001514 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001515 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02001516 int ret = 0;
1517
Russell King446d999c2015-03-20 17:20:33 +00001518 pd = pm_genpd_lookup_dev(dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02001519 if (!pd)
1520 return;
1521
1522 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1523
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001524 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02001525 ret = pm_genpd_remove_device(pd, dev);
1526 if (ret != -EAGAIN)
1527 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001528
1529 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02001530 cond_resched();
1531 }
1532
1533 if (ret < 0) {
1534 dev_err(dev, "failed to remove from PM domain %s: %d",
1535 pd->name, ret);
1536 return;
1537 }
1538
1539 /* Check if PM domain can be powered off after removing this device. */
1540 genpd_queue_power_off_work(pd);
1541}
1542
Russell King632f7ce2015-03-20 15:55:12 +01001543static void genpd_dev_pm_sync(struct device *dev)
1544{
1545 struct generic_pm_domain *pd;
1546
1547 pd = dev_to_genpd(dev);
1548 if (IS_ERR(pd))
1549 return;
1550
1551 genpd_queue_power_off_work(pd);
1552}
1553
Tomasz Figaaa422402014-09-19 20:27:36 +02001554/**
1555 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1556 * @dev: Device to attach.
1557 *
1558 * Parse device's OF node to find a PM domain specifier. If such is found,
1559 * attaches the device to retrieved pm_domain ops.
1560 *
1561 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1562 * backwards compatibility with existing DTBs.
1563 *
Jon Hunter311fa6a2015-07-31 10:20:00 +01001564 * Returns 0 on successfully attached PM domain or negative error code. Note
1565 * that if a power-domain exists for the device, but it cannot be found or
1566 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1567 * probed and to re-try again later.
Tomasz Figaaa422402014-09-19 20:27:36 +02001568 */
1569int genpd_dev_pm_attach(struct device *dev)
1570{
1571 struct of_phandle_args pd_args;
1572 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001573 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02001574 int ret;
1575
1576 if (!dev->of_node)
1577 return -ENODEV;
1578
1579 if (dev->pm_domain)
1580 return -EEXIST;
1581
1582 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1583 "#power-domain-cells", 0, &pd_args);
1584 if (ret < 0) {
1585 if (ret != -ENOENT)
1586 return ret;
1587
1588 /*
1589 * Try legacy Samsung-specific bindings
1590 * (for backwards compatibility of DT ABI)
1591 */
1592 pd_args.args_count = 0;
1593 pd_args.np = of_parse_phandle(dev->of_node,
1594 "samsung,power-domain", 0);
1595 if (!pd_args.np)
1596 return -ENOENT;
1597 }
1598
1599 pd = of_genpd_get_from_provider(&pd_args);
Eric Anholt265e2cf2015-12-01 09:39:31 -08001600 of_node_put(pd_args.np);
Tomasz Figaaa422402014-09-19 20:27:36 +02001601 if (IS_ERR(pd)) {
1602 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1603 __func__, PTR_ERR(pd));
Jon Hunter311fa6a2015-07-31 10:20:00 +01001604 return -EPROBE_DEFER;
Tomasz Figaaa422402014-09-19 20:27:36 +02001605 }
1606
1607 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1608
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001609 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02001610 ret = pm_genpd_add_device(pd, dev);
1611 if (ret != -EAGAIN)
1612 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001613
1614 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02001615 cond_resched();
1616 }
1617
1618 if (ret < 0) {
1619 dev_err(dev, "failed to add to PM domain %s: %d",
1620 pd->name, ret);
Jon Hunter311fa6a2015-07-31 10:20:00 +01001621 goto out;
Tomasz Figaaa422402014-09-19 20:27:36 +02001622 }
1623
1624 dev->pm_domain->detach = genpd_dev_pm_detach;
Russell King632f7ce2015-03-20 15:55:12 +01001625 dev->pm_domain->sync = genpd_dev_pm_sync;
Tomasz Figaaa422402014-09-19 20:27:36 +02001626
Ulf Hansson53af16f2016-02-01 14:52:41 +01001627 mutex_lock(&pd->lock);
1628 ret = genpd_poweron(pd, 0);
1629 mutex_unlock(&pd->lock);
Jon Hunter311fa6a2015-07-31 10:20:00 +01001630out:
1631 return ret ? -EPROBE_DEFER : 0;
Tomasz Figaaa422402014-09-19 20:27:36 +02001632}
1633EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001634#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001635
1636
1637/*** debugfs support ***/
1638
Jon Hunter8b0510b2016-08-11 11:40:05 +01001639#ifdef CONFIG_DEBUG_FS
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001640#include <linux/pm.h>
1641#include <linux/device.h>
1642#include <linux/debugfs.h>
1643#include <linux/seq_file.h>
1644#include <linux/init.h>
1645#include <linux/kobject.h>
1646static struct dentry *pm_genpd_debugfs_dir;
1647
1648/*
1649 * TODO: This function is a slightly modified version of rtpm_status_show
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001650 * from sysfs.c, so generalize it.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001651 */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001652static void rtpm_status_str(struct seq_file *s, struct device *dev)
1653{
1654 static const char * const status_lookup[] = {
1655 [RPM_ACTIVE] = "active",
1656 [RPM_RESUMING] = "resuming",
1657 [RPM_SUSPENDED] = "suspended",
1658 [RPM_SUSPENDING] = "suspending"
1659 };
1660 const char *p = "";
1661
1662 if (dev->power.runtime_error)
1663 p = "error";
1664 else if (dev->power.disable_depth)
1665 p = "unsupported";
1666 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1667 p = status_lookup[dev->power.runtime_status];
1668 else
1669 WARN_ON(1);
1670
1671 seq_puts(s, p);
1672}
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001673
1674static int pm_genpd_summary_one(struct seq_file *s,
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001675 struct generic_pm_domain *genpd)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001676{
1677 static const char * const status_lookup[] = {
1678 [GPD_STATE_ACTIVE] = "on",
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001679 [GPD_STATE_POWER_OFF] = "off"
1680 };
1681 struct pm_domain_data *pm_data;
1682 const char *kobj_path;
1683 struct gpd_link *link;
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01001684 char state[16];
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001685 int ret;
1686
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001687 ret = mutex_lock_interruptible(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001688 if (ret)
1689 return -ERESTARTSYS;
1690
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001691 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001692 goto exit;
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001693 if (genpd->status == GPD_STATE_POWER_OFF)
Geert Uytterhoeven0ba554e2016-02-23 17:49:18 +01001694 snprintf(state, sizeof(state), "%s-%u",
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01001695 status_lookup[genpd->status], genpd->state_idx);
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001696 else
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01001697 snprintf(state, sizeof(state), "%s",
1698 status_lookup[genpd->status]);
1699 seq_printf(s, "%-30s %-15s ", genpd->name, state);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001700
1701 /*
1702 * Modifications on the list require holding locks on both
1703 * master and slave, so we are safe.
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001704 * Also genpd->name is immutable.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001705 */
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001706 list_for_each_entry(link, &genpd->master_links, master_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001707 seq_printf(s, "%s", link->slave->name);
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001708 if (!list_is_last(&link->master_node, &genpd->master_links))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001709 seq_puts(s, ", ");
1710 }
1711
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001712 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001713 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1714 if (kobj_path == NULL)
1715 continue;
1716
1717 seq_printf(s, "\n %-50s ", kobj_path);
1718 rtpm_status_str(s, pm_data->dev);
1719 kfree(kobj_path);
1720 }
1721
1722 seq_puts(s, "\n");
1723exit:
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001724 mutex_unlock(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001725
1726 return 0;
1727}
1728
1729static int pm_genpd_summary_show(struct seq_file *s, void *data)
1730{
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001731 struct generic_pm_domain *genpd;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001732 int ret = 0;
1733
Geert Uytterhoeven15dec672015-08-11 14:50:49 +02001734 seq_puts(s, "domain status slaves\n");
1735 seq_puts(s, " /device runtime status\n");
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001736 seq_puts(s, "----------------------------------------------------------------------\n");
1737
1738 ret = mutex_lock_interruptible(&gpd_list_lock);
1739 if (ret)
1740 return -ERESTARTSYS;
1741
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001742 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1743 ret = pm_genpd_summary_one(s, genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001744 if (ret)
1745 break;
1746 }
1747 mutex_unlock(&gpd_list_lock);
1748
1749 return ret;
1750}
1751
1752static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1753{
1754 return single_open(file, pm_genpd_summary_show, NULL);
1755}
1756
1757static const struct file_operations pm_genpd_summary_fops = {
1758 .open = pm_genpd_summary_open,
1759 .read = seq_read,
1760 .llseek = seq_lseek,
1761 .release = single_release,
1762};
1763
1764static int __init pm_genpd_debug_init(void)
1765{
1766 struct dentry *d;
1767
1768 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1769
1770 if (!pm_genpd_debugfs_dir)
1771 return -ENOMEM;
1772
1773 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1774 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1775 if (!d)
1776 return -ENOMEM;
1777
1778 return 0;
1779}
1780late_initcall(pm_genpd_debug_init);
1781
1782static void __exit pm_genpd_debug_exit(void)
1783{
1784 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1785}
1786__exitcall(pm_genpd_debug_exit);
Jon Hunter8b0510b2016-08-11 11:40:05 +01001787#endif /* CONFIG_DEBUG_FS */