blob: 0ee43c1056e045bd6a402908e8a11cf9dac8f34e [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
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +020023#define GENPD_RETRY_MAX_MS 250 /* Approximate */
24
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010025#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
26({ \
27 type (*__routine)(struct device *__d); \
28 type __ret = (type)0; \
29 \
30 __routine = genpd->dev_ops.callback; \
31 if (__routine) { \
32 __ret = __routine(dev); \
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010033 } \
34 __ret; \
35})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020036
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010037#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
38({ \
39 ktime_t __start = ktime_get(); \
40 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
41 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020042 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
43 if (!__retval && __elapsed > __td->field) { \
44 __td->field = __elapsed; \
Philipp Zabel7d1af282014-02-27 19:26:44 +010045 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010046 __elapsed); \
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020047 genpd->max_off_time_changed = true; \
48 __td->constraint_changed = true; \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010049 } \
50 __retval; \
51})
52
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020053static LIST_HEAD(gpd_list);
54static DEFINE_MUTEX(gpd_list_lock);
55
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +020056static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
57{
58 struct generic_pm_domain *genpd = NULL, *gpd;
59
60 if (IS_ERR_OR_NULL(domain_name))
61 return NULL;
62
63 mutex_lock(&gpd_list_lock);
64 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
65 if (!strcmp(gpd->name, domain_name)) {
66 genpd = gpd;
67 break;
68 }
69 }
70 mutex_unlock(&gpd_list_lock);
71 return genpd;
72}
73
Russell King446d9992015-03-20 17:20:33 +000074/*
75 * Get the generic PM domain for a particular struct device.
76 * This validates the struct device pointer, the PM domain pointer,
77 * and checks that the PM domain pointer is a real generic PM domain.
78 * Any failure results in NULL being returned.
79 */
80struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
81{
82 struct generic_pm_domain *genpd = NULL, *gpd;
83
84 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
85 return NULL;
86
87 mutex_lock(&gpd_list_lock);
88 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
89 if (&gpd->domain == dev->pm_domain) {
90 genpd = gpd;
91 break;
92 }
93 }
94 mutex_unlock(&gpd_list_lock);
95
96 return genpd;
97}
98
99/*
100 * This should only be used where we are certain that the pm_domain
101 * attached to the device is a genpd domain.
102 */
103static struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200104{
105 if (IS_ERR_OR_NULL(dev->pm_domain))
106 return ERR_PTR(-EINVAL);
107
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200108 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200109}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200110
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100111static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
112{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100113 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
114 stop_latency_ns, "stop");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100115}
116
117static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
118{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100119 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
120 start_latency_ns, "start");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100121}
122
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200123static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200124{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200125 bool ret = false;
126
127 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
128 ret = !!atomic_dec_and_test(&genpd->sd_count);
129
130 return ret;
131}
132
133static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
134{
135 atomic_inc(&genpd->sd_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100136 smp_mb__after_atomic();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200137}
138
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200139static void genpd_acquire_lock(struct generic_pm_domain *genpd)
140{
141 DEFINE_WAIT(wait);
142
143 mutex_lock(&genpd->lock);
144 /*
145 * Wait for the domain to transition into either the active,
146 * or the power off state.
147 */
148 for (;;) {
149 prepare_to_wait(&genpd->status_wait_queue, &wait,
150 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200151 if (genpd->status == GPD_STATE_ACTIVE
152 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200153 break;
154 mutex_unlock(&genpd->lock);
155
156 schedule();
157
158 mutex_lock(&genpd->lock);
159 }
160 finish_wait(&genpd->status_wait_queue, &wait);
161}
162
163static void genpd_release_lock(struct generic_pm_domain *genpd)
164{
165 mutex_unlock(&genpd->lock);
166}
167
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200168static void genpd_set_active(struct generic_pm_domain *genpd)
169{
170 if (genpd->resume_count == 0)
171 genpd->status = GPD_STATE_ACTIVE;
172}
173
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200174static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
175{
176 s64 usecs64;
177
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200178 if (!genpd->cpuidle_data)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200179 return;
180
181 usecs64 = genpd->power_on_latency_ns;
182 do_div(usecs64, NSEC_PER_USEC);
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200183 usecs64 += genpd->cpuidle_data->saved_exit_latency;
184 genpd->cpuidle_data->idle_state->exit_latency = usecs64;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200185}
186
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200187static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100188{
189 ktime_t time_start;
190 s64 elapsed_ns;
191 int ret;
192
193 if (!genpd->power_on)
194 return 0;
195
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200196 if (!timed)
197 return genpd->power_on(genpd);
198
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100199 time_start = ktime_get();
200 ret = genpd->power_on(genpd);
201 if (ret)
202 return ret;
203
204 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
205 if (elapsed_ns <= genpd->power_on_latency_ns)
206 return ret;
207
208 genpd->power_on_latency_ns = elapsed_ns;
209 genpd->max_off_time_changed = true;
210 genpd_recalc_cpu_exit_latency(genpd);
Russell King6d7d5c32015-03-20 17:20:28 +0000211 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
212 genpd->name, "on", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100213
214 return ret;
215}
216
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200217static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100218{
219 ktime_t time_start;
220 s64 elapsed_ns;
221 int ret;
222
223 if (!genpd->power_off)
224 return 0;
225
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200226 if (!timed)
227 return genpd->power_off(genpd);
228
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100229 time_start = ktime_get();
230 ret = genpd->power_off(genpd);
231 if (ret == -EBUSY)
232 return ret;
233
234 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
235 if (elapsed_ns <= genpd->power_off_latency_ns)
236 return ret;
237
238 genpd->power_off_latency_ns = elapsed_ns;
239 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000240 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
241 genpd->name, "off", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100242
243 return ret;
244}
245
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200246/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200247 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200248 * @genpd: PM domain to power up.
249 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200250 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200251 * resume a device belonging to it.
252 */
Sachin Kamat8951ef02012-07-10 21:47:07 +0200253static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200254 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200255{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200256 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200257 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200258 int ret = 0;
259
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200260 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200261 for (;;) {
262 prepare_to_wait(&genpd->status_wait_queue, &wait,
263 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200264 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200265 break;
266 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200267
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200268 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200269
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200270 mutex_lock(&genpd->lock);
271 }
272 finish_wait(&genpd->status_wait_queue, &wait);
273
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200274 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200275 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200276 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200277
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200278 if (genpd->status != GPD_STATE_POWER_OFF) {
279 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200280 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200281 }
282
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200283 if (genpd->cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200284 cpuidle_pause_and_lock();
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200285 genpd->cpuidle_data->idle_state->disabled = true;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200286 cpuidle_resume_and_unlock();
287 goto out;
288 }
289
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200290 /*
291 * The list is guaranteed not to change while the loop below is being
292 * executed, unless one of the masters' .power_on() callbacks fiddles
293 * with it.
294 */
295 list_for_each_entry(link, &genpd->slave_links, slave_node) {
296 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200297 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200298
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200299 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200300
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200301 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200302
303 mutex_lock(&genpd->lock);
304
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200305 /*
306 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200307 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200308 */
309 genpd->status = GPD_STATE_POWER_OFF;
310 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200311 if (ret) {
312 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200313 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200314 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200315 }
316
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200317 ret = genpd_power_on(genpd, true);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100318 if (ret)
319 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200320
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200321 out:
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200322 genpd_set_active(genpd);
323
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200324 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200325
326 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200327 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
328 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200329
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200330 return ret;
331}
332
333/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200334 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200335 * @genpd: PM domain to power up.
336 */
337int pm_genpd_poweron(struct generic_pm_domain *genpd)
338{
339 int ret;
340
341 mutex_lock(&genpd->lock);
342 ret = __pm_genpd_poweron(genpd);
343 mutex_unlock(&genpd->lock);
344 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200345}
346
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +0200347/**
348 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
349 * @domain_name: Name of the PM domain to power up.
350 */
351int pm_genpd_name_poweron(const char *domain_name)
352{
353 struct generic_pm_domain *genpd;
354
355 genpd = pm_genpd_lookup_name(domain_name);
356 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
357}
358
Sachin Kamatb3d3b9f2012-09-06 08:18:57 +0000359static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
360 struct device *dev)
361{
362 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
363}
364
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200365static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
366{
367 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
368 save_state_latency_ns, "state save");
369}
370
371static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
372{
373 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
374 restore_state_latency_ns,
375 "state restore");
376}
377
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200378static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
379 unsigned long val, void *ptr)
380{
381 struct generic_pm_domain_data *gpd_data;
382 struct device *dev;
383
384 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200385 dev = gpd_data->base.dev;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200386
387 for (;;) {
388 struct generic_pm_domain *genpd;
389 struct pm_domain_data *pdd;
390
391 spin_lock_irq(&dev->power.lock);
392
393 pdd = dev->power.subsys_data ?
394 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200395 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200396 to_gpd_data(pdd)->td.constraint_changed = true;
397 genpd = dev_to_genpd(dev);
398 } else {
399 genpd = ERR_PTR(-ENODATA);
400 }
401
402 spin_unlock_irq(&dev->power.lock);
403
404 if (!IS_ERR(genpd)) {
405 mutex_lock(&genpd->lock);
406 genpd->max_off_time_changed = true;
407 mutex_unlock(&genpd->lock);
408 }
409
410 dev = dev->parent;
411 if (!dev || dev->power.ignore_children)
412 break;
413 }
414
415 return NOTIFY_DONE;
416}
417
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200418/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200419 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200420 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200421 * @genpd: PM domain the device belongs to.
422 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200423static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200424 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200425 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200426{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200427 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200428 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200429 int ret = 0;
430
Ulf Hansson67732cd2014-11-11 11:07:08 +0100431 if (gpd_data->need_restore > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200432 return 0;
433
Ulf Hansson67732cd2014-11-11 11:07:08 +0100434 /*
435 * If the value of the need_restore flag is still unknown at this point,
436 * we trust that pm_genpd_poweroff() has verified that the device is
437 * already runtime PM suspended.
438 */
439 if (gpd_data->need_restore < 0) {
440 gpd_data->need_restore = 1;
441 return 0;
442 }
443
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200444 mutex_unlock(&genpd->lock);
445
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100446 genpd_start_dev(genpd, dev);
447 ret = genpd_save_dev(genpd, dev);
448 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200449
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200450 mutex_lock(&genpd->lock);
451
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200452 if (!ret)
Ulf Hansson67732cd2014-11-11 11:07:08 +0100453 gpd_data->need_restore = 1;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200454
455 return ret;
456}
457
458/**
459 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200460 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200461 * @genpd: PM domain the device belongs to.
462 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200463static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200464 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200465 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200466{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200467 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200468 struct device *dev = pdd->dev;
Ulf Hansson67732cd2014-11-11 11:07:08 +0100469 int need_restore = gpd_data->need_restore;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200470
Ulf Hansson67732cd2014-11-11 11:07:08 +0100471 gpd_data->need_restore = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200472 mutex_unlock(&genpd->lock);
473
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100474 genpd_start_dev(genpd, dev);
Ulf Hansson67732cd2014-11-11 11:07:08 +0100475
476 /*
477 * Call genpd_restore_dev() for recently added devices too (need_restore
478 * is negative then).
479 */
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200480 if (need_restore)
481 genpd_restore_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200482
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200483 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200484}
485
486/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200487 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
488 * @genpd: PM domain to check.
489 *
490 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
491 * a "power off" operation, which means that a "power on" has occured in the
492 * meantime, or if its resume_count field is different from zero, which means
493 * that one of its devices has been resumed in the meantime.
494 */
495static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
496{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200497 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200498 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200499}
500
501/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200502 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
503 * @genpd: PM domait to power off.
504 *
505 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
506 * before.
507 */
Ulf Hanssond971f0b2014-09-03 12:52:25 +0200508static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200509{
Tejun Heoa4ca26a2013-01-11 13:37:23 +0100510 queue_work(pm_wq, &genpd->power_off_work);
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200511}
512
513/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200514 * pm_genpd_poweroff - Remove power from a given PM domain.
515 * @genpd: PM domain to power down.
516 *
517 * If all of the @genpd's devices have been suspended and all of its subdomains
518 * have been powered down, run the runtime suspend callbacks provided by all of
519 * the @genpd's devices' drivers and remove power from @genpd.
520 */
521static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200522 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200523{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200524 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200525 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200526 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200527 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200528
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200529 start:
530 /*
531 * Do not try to power off the domain in the following situations:
532 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200533 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200534 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200535 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200536 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200537 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200538 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200539 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200540 return 0;
541
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200542 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200543 return -EBUSY;
544
545 not_suspended = 0;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200546 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
547 enum pm_qos_flags_status stat;
548
549 stat = dev_pm_qos_flags(pdd->dev,
550 PM_QOS_FLAG_NO_POWER_OFF
551 | PM_QOS_FLAG_REMOTE_WAKEUP);
552 if (stat > PM_QOS_FLAGS_NONE)
553 return -EBUSY;
554
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200555 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
Rafael J. Wysockifeb70af2012-08-13 14:00:25 +0200556 || pdd->dev->power.irq_safe))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200557 not_suspended++;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200558 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200559
560 if (not_suspended > genpd->in_progress)
561 return -EBUSY;
562
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200563 if (genpd->poweroff_task) {
564 /*
565 * Another instance of pm_genpd_poweroff() is executing
566 * callbacks, so tell it to start over and return.
567 */
568 genpd->status = GPD_STATE_REPEAT;
569 return 0;
570 }
571
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200572 if (genpd->gov && genpd->gov->power_down_ok) {
573 if (!genpd->gov->power_down_ok(&genpd->domain))
574 return -EAGAIN;
575 }
576
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200577 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200578 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200579
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200580 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200581 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200582 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200583
584 if (genpd_abort_poweroff(genpd))
585 goto out;
586
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200587 if (ret) {
588 genpd_set_active(genpd);
589 goto out;
590 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200591
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200592 if (genpd->status == GPD_STATE_REPEAT) {
593 genpd->poweroff_task = NULL;
594 goto start;
595 }
596 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200597
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200598 if (genpd->cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200599 /*
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200600 * If cpuidle_data is set, cpuidle should turn the domain off
601 * when the CPU in it is idle. In that case we don't decrement
602 * the subdomain counts of the master domains, so that power is
603 * not removed from the current domain prematurely as a result
604 * of cutting off the masters' power.
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200605 */
606 genpd->status = GPD_STATE_POWER_OFF;
607 cpuidle_pause_and_lock();
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200608 genpd->cpuidle_data->idle_state->disabled = false;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200609 cpuidle_resume_and_unlock();
610 goto out;
611 }
612
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200613 if (genpd->power_off) {
614 if (atomic_read(&genpd->sd_count) > 0) {
615 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200616 goto out;
617 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200618
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200619 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200620 * If sd_count > 0 at this point, one of the subdomains hasn't
621 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200622 * incrementing it. In that case pm_genpd_poweron() will wait
623 * for us to drop the lock, so we can call .power_off() and let
624 * the pm_genpd_poweron() restore power for us (this shouldn't
625 * happen very often).
626 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200627 ret = genpd_power_off(genpd, true);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200628 if (ret == -EBUSY) {
629 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200630 goto out;
631 }
632 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200633
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200634 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100635
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200636 list_for_each_entry(link, &genpd->slave_links, slave_node) {
637 genpd_sd_counter_dec(link->master);
638 genpd_queue_power_off_work(link->master);
639 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200640
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200641 out:
642 genpd->poweroff_task = NULL;
643 wake_up_all(&genpd->status_wait_queue);
644 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200645}
646
647/**
648 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
649 * @work: Work structure used for scheduling the execution of this function.
650 */
651static void genpd_power_off_work_fn(struct work_struct *work)
652{
653 struct generic_pm_domain *genpd;
654
655 genpd = container_of(work, struct generic_pm_domain, power_off_work);
656
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200657 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200658 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200659 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200660}
661
662/**
663 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
664 * @dev: Device to suspend.
665 *
666 * Carry out a runtime suspend of a device under the assumption that its
667 * pm_domain field points to the domain member of an object of type
668 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
669 */
670static int pm_genpd_runtime_suspend(struct device *dev)
671{
672 struct generic_pm_domain *genpd;
Ulf Hansson67732cd2014-11-11 11:07:08 +0100673 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100674 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100675 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200676
677 dev_dbg(dev, "%s()\n", __func__);
678
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200679 genpd = dev_to_genpd(dev);
680 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200681 return -EINVAL;
682
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100683 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
684 if (stop_ok && !stop_ok(dev))
685 return -EBUSY;
686
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100687 ret = genpd_stop_dev(genpd, dev);
688 if (ret)
689 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200690
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200691 /*
692 * If power.irq_safe is set, this routine will be run with interrupts
693 * off, so it can't use mutexes.
694 */
695 if (dev->power.irq_safe)
696 return 0;
697
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200698 mutex_lock(&genpd->lock);
Ulf Hansson67732cd2014-11-11 11:07:08 +0100699
700 /*
701 * If we have an unknown state of the need_restore flag, it means none
702 * of the runtime PM callbacks has been invoked yet. Let's update the
703 * flag to reflect that the current state is active.
704 */
705 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
706 if (gpd_data->need_restore < 0)
707 gpd_data->need_restore = 0;
708
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200709 genpd->in_progress++;
710 pm_genpd_poweroff(genpd);
711 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200712 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200713
714 return 0;
715}
716
717/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200718 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
719 * @dev: Device to resume.
720 *
721 * Carry out a runtime resume of a device under the assumption that its
722 * pm_domain field points to the domain member of an object of type
723 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
724 */
725static int pm_genpd_runtime_resume(struct device *dev)
726{
727 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200728 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200729 int ret;
730
731 dev_dbg(dev, "%s()\n", __func__);
732
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200733 genpd = dev_to_genpd(dev);
734 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200735 return -EINVAL;
736
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200737 /* If power.irq_safe, the PM domain is never powered off. */
738 if (dev->power.irq_safe)
Rafael J. Wysockie2e3e4e52012-08-06 01:47:29 +0200739 return genpd_start_dev_no_timing(genpd, dev);
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200740
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200741 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200742 ret = __pm_genpd_poweron(genpd);
743 if (ret) {
744 mutex_unlock(&genpd->lock);
745 return ret;
746 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200747 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200748 genpd->resume_count++;
749 for (;;) {
750 prepare_to_wait(&genpd->status_wait_queue, &wait,
751 TASK_UNINTERRUPTIBLE);
752 /*
753 * If current is the powering off task, we have been called
754 * reentrantly from one of the device callbacks, so we should
755 * not wait.
756 */
757 if (!genpd->poweroff_task || genpd->poweroff_task == current)
758 break;
759 mutex_unlock(&genpd->lock);
760
761 schedule();
762
763 mutex_lock(&genpd->lock);
764 }
765 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200766 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200767 genpd->resume_count--;
768 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200769 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200770 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200771
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200772 return 0;
773}
774
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530775static bool pd_ignore_unused;
776static int __init pd_ignore_unused_setup(char *__unused)
777{
778 pd_ignore_unused = true;
779 return 1;
780}
781__setup("pd_ignore_unused", pd_ignore_unused_setup);
782
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200783/**
784 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
785 */
786void pm_genpd_poweroff_unused(void)
787{
788 struct generic_pm_domain *genpd;
789
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530790 if (pd_ignore_unused) {
791 pr_warn("genpd: Not disabling unused power domains\n");
792 return;
793 }
794
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200795 mutex_lock(&gpd_list_lock);
796
797 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
798 genpd_queue_power_off_work(genpd);
799
800 mutex_unlock(&gpd_list_lock);
801}
802
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200803static int __init genpd_poweroff_unused(void)
804{
805 pm_genpd_poweroff_unused();
806 return 0;
807}
808late_initcall(genpd_poweroff_unused);
809
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200810#ifdef CONFIG_PM_SLEEP
811
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200812/**
813 * pm_genpd_present - Check if the given PM domain has been initialized.
814 * @genpd: PM domain to check.
815 */
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100816static bool pm_genpd_present(const struct generic_pm_domain *genpd)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200817{
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100818 const struct generic_pm_domain *gpd;
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200819
820 if (IS_ERR_OR_NULL(genpd))
821 return false;
822
823 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
824 if (gpd == genpd)
825 return true;
826
827 return false;
828}
829
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100830static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
831 struct device *dev)
832{
833 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
834}
835
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200836/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200837 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200838 * @genpd: PM domain to power off, if possible.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200839 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200840 *
841 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200842 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200843 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200844 * This function is only called in "noirq" and "syscore" stages of system power
845 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
846 * executed sequentially, so it is guaranteed that it will never run twice in
847 * parallel).
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200848 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200849static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
850 bool timed)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200851{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200852 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200853
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200854 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200855 return;
856
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200857 if (genpd->suspended_count != genpd->device_count
858 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200859 return;
860
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200861 genpd_power_off(genpd, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200862
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200863 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200864
865 list_for_each_entry(link, &genpd->slave_links, slave_node) {
866 genpd_sd_counter_dec(link->master);
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200867 pm_genpd_sync_poweroff(link->master, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200868 }
869}
870
871/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200872 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
873 * @genpd: PM domain to power on.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200874 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200875 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200876 * This function is only called in "noirq" and "syscore" stages of system power
877 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
878 * executed sequentially, so it is guaranteed that it will never run twice in
879 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200880 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200881static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
882 bool timed)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200883{
884 struct gpd_link *link;
885
886 if (genpd->status != GPD_STATE_POWER_OFF)
887 return;
888
889 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200890 pm_genpd_sync_poweron(link->master, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200891 genpd_sd_counter_inc(link->master);
892 }
893
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200894 genpd_power_on(genpd, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200895
896 genpd->status = GPD_STATE_ACTIVE;
897}
898
899/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200900 * resume_needed - Check whether to resume a device before system suspend.
901 * @dev: Device to check.
902 * @genpd: PM domain the device belongs to.
903 *
904 * There are two cases in which a device that can wake up the system from sleep
905 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
906 * to wake up the system and it has to remain active for this purpose while the
907 * system is in the sleep state and (2) if the device is not enabled to wake up
908 * the system from sleep states and it generally doesn't generate wakeup signals
909 * by itself (those signals are generated on its behalf by other parts of the
910 * system). In the latter case it may be necessary to reconfigure the device's
911 * wakeup settings during system suspend, because it may have been set up to
912 * signal remote wakeup from the system's working state as needed by runtime PM.
913 * Return 'true' in either of the above cases.
914 */
915static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
916{
917 bool active_wakeup;
918
919 if (!device_can_wakeup(dev))
920 return false;
921
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100922 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200923 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
924}
925
926/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200927 * pm_genpd_prepare - Start power transition of a device in a PM domain.
928 * @dev: Device to start the transition of.
929 *
930 * Start a power transition of a device (during a system-wide power transition)
931 * under the assumption that its pm_domain field points to the domain member of
932 * an object of type struct generic_pm_domain representing a PM domain
933 * consisting of I/O devices.
934 */
935static int pm_genpd_prepare(struct device *dev)
936{
937 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200938 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200939
940 dev_dbg(dev, "%s()\n", __func__);
941
942 genpd = dev_to_genpd(dev);
943 if (IS_ERR(genpd))
944 return -EINVAL;
945
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200946 /*
947 * If a wakeup request is pending for the device, it should be woken up
948 * at this point and a system wakeup event should be reported if it's
949 * set up to wake up the system from sleep states.
950 */
951 pm_runtime_get_noresume(dev);
952 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
953 pm_wakeup_event(dev, 0);
954
955 if (pm_wakeup_pending()) {
Ulf Hansson84167032013-04-12 09:41:44 +0000956 pm_runtime_put(dev);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200957 return -EBUSY;
958 }
959
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200960 if (resume_needed(dev, genpd))
961 pm_runtime_resume(dev);
962
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200963 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200964
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100965 if (genpd->prepared_count++ == 0) {
966 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200967 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100968 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200969
970 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200971
972 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200973 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200974 return 0;
975 }
976
977 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200978 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
979 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100980 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200981 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200982 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200983 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200984 __pm_runtime_disable(dev, false);
985
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200986 ret = pm_generic_prepare(dev);
987 if (ret) {
988 mutex_lock(&genpd->lock);
989
990 if (--genpd->prepared_count == 0)
991 genpd->suspend_power_off = false;
992
993 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200994 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200995 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200996
Ulf Hansson84167032013-04-12 09:41:44 +0000997 pm_runtime_put(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200998 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200999}
1000
1001/**
1002 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
1003 * @dev: Device to suspend.
1004 *
1005 * Suspend a device under the assumption that its pm_domain field points to the
1006 * domain member of an object of type struct generic_pm_domain representing
1007 * a PM domain consisting of I/O devices.
1008 */
1009static int pm_genpd_suspend(struct device *dev)
1010{
1011 struct generic_pm_domain *genpd;
1012
1013 dev_dbg(dev, "%s()\n", __func__);
1014
1015 genpd = dev_to_genpd(dev);
1016 if (IS_ERR(genpd))
1017 return -EINVAL;
1018
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001019 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001020}
1021
1022/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001023 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001024 * @dev: Device to suspend.
1025 *
1026 * Carry out a late suspend of a device under the assumption that its
1027 * pm_domain field points to the domain member of an object of type
1028 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1029 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001030static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001031{
1032 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001033
1034 dev_dbg(dev, "%s()\n", __func__);
1035
1036 genpd = dev_to_genpd(dev);
1037 if (IS_ERR(genpd))
1038 return -EINVAL;
1039
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001040 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001041}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001042
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001043/**
1044 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1045 * @dev: Device to suspend.
1046 *
1047 * Stop the device and remove power from the domain if all devices in it have
1048 * been stopped.
1049 */
1050static int pm_genpd_suspend_noirq(struct device *dev)
1051{
1052 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001053
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001054 dev_dbg(dev, "%s()\n", __func__);
1055
1056 genpd = dev_to_genpd(dev);
1057 if (IS_ERR(genpd))
1058 return -EINVAL;
1059
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001060 if (genpd->suspend_power_off
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001061 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +02001062 return 0;
1063
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001064 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001065
1066 /*
1067 * Since all of the "noirq" callbacks are executed sequentially, it is
1068 * guaranteed that this function will never run twice in parallel for
1069 * the same PM domain, so it is not necessary to use locking here.
1070 */
1071 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001072 pm_genpd_sync_poweroff(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001073
1074 return 0;
1075}
1076
1077/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001078 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001079 * @dev: Device to resume.
1080 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001081 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001082 */
1083static int pm_genpd_resume_noirq(struct device *dev)
1084{
1085 struct generic_pm_domain *genpd;
1086
1087 dev_dbg(dev, "%s()\n", __func__);
1088
1089 genpd = dev_to_genpd(dev);
1090 if (IS_ERR(genpd))
1091 return -EINVAL;
1092
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001093 if (genpd->suspend_power_off
Rafael J. Wysockicc85b202012-03-13 22:39:31 +01001094 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001095 return 0;
1096
1097 /*
1098 * Since all of the "noirq" callbacks are executed sequentially, it is
1099 * guaranteed that this function will never run twice in parallel for
1100 * the same PM domain, so it is not necessary to use locking here.
1101 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001102 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001103 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001104
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001105 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001106}
1107
1108/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001109 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1110 * @dev: Device to resume.
1111 *
1112 * Carry out an early resume of a device under the assumption that its
1113 * pm_domain field points to the domain member of an object of type
1114 * struct generic_pm_domain representing a power domain consisting of I/O
1115 * devices.
1116 */
1117static int pm_genpd_resume_early(struct device *dev)
1118{
1119 struct generic_pm_domain *genpd;
1120
1121 dev_dbg(dev, "%s()\n", __func__);
1122
1123 genpd = dev_to_genpd(dev);
1124 if (IS_ERR(genpd))
1125 return -EINVAL;
1126
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001127 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001128}
1129
1130/**
1131 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001132 * @dev: Device to resume.
1133 *
1134 * Resume a device under the assumption that its pm_domain field points to the
1135 * domain member of an object of type struct generic_pm_domain representing
1136 * a power domain consisting of I/O devices.
1137 */
1138static int pm_genpd_resume(struct device *dev)
1139{
1140 struct generic_pm_domain *genpd;
1141
1142 dev_dbg(dev, "%s()\n", __func__);
1143
1144 genpd = dev_to_genpd(dev);
1145 if (IS_ERR(genpd))
1146 return -EINVAL;
1147
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001148 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001149}
1150
1151/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001152 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001153 * @dev: Device to freeze.
1154 *
1155 * Freeze a device under the assumption that its pm_domain field points to the
1156 * domain member of an object of type struct generic_pm_domain representing
1157 * a power domain consisting of I/O devices.
1158 */
1159static int pm_genpd_freeze(struct device *dev)
1160{
1161 struct generic_pm_domain *genpd;
1162
1163 dev_dbg(dev, "%s()\n", __func__);
1164
1165 genpd = dev_to_genpd(dev);
1166 if (IS_ERR(genpd))
1167 return -EINVAL;
1168
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001169 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001170}
1171
1172/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001173 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1174 * @dev: Device to freeze.
1175 *
1176 * Carry out a late freeze of a device under the assumption that its
1177 * pm_domain field points to the domain member of an object of type
1178 * struct generic_pm_domain representing a power domain consisting of I/O
1179 * devices.
1180 */
1181static int pm_genpd_freeze_late(struct device *dev)
1182{
1183 struct generic_pm_domain *genpd;
1184
1185 dev_dbg(dev, "%s()\n", __func__);
1186
1187 genpd = dev_to_genpd(dev);
1188 if (IS_ERR(genpd))
1189 return -EINVAL;
1190
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001191 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001192}
1193
1194/**
1195 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001196 * @dev: Device to freeze.
1197 *
1198 * Carry out a late freeze of a device under the assumption that its
1199 * pm_domain field points to the domain member of an object of type
1200 * struct generic_pm_domain representing a power domain consisting of I/O
1201 * devices.
1202 */
1203static int pm_genpd_freeze_noirq(struct device *dev)
1204{
1205 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001206
1207 dev_dbg(dev, "%s()\n", __func__);
1208
1209 genpd = dev_to_genpd(dev);
1210 if (IS_ERR(genpd))
1211 return -EINVAL;
1212
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001213 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001214}
1215
1216/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001217 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001218 * @dev: Device to thaw.
1219 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001220 * Start the device, unless power has been removed from the domain already
1221 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001222 */
1223static int pm_genpd_thaw_noirq(struct device *dev)
1224{
1225 struct generic_pm_domain *genpd;
1226
1227 dev_dbg(dev, "%s()\n", __func__);
1228
1229 genpd = dev_to_genpd(dev);
1230 if (IS_ERR(genpd))
1231 return -EINVAL;
1232
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001233 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001234}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001235
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001236/**
1237 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1238 * @dev: Device to thaw.
1239 *
1240 * Carry out an early thaw of a device under the assumption that its
1241 * pm_domain field points to the domain member of an object of type
1242 * struct generic_pm_domain representing a power domain consisting of I/O
1243 * devices.
1244 */
1245static int pm_genpd_thaw_early(struct device *dev)
1246{
1247 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001248
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001249 dev_dbg(dev, "%s()\n", __func__);
1250
1251 genpd = dev_to_genpd(dev);
1252 if (IS_ERR(genpd))
1253 return -EINVAL;
1254
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001255 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001256}
1257
1258/**
1259 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1260 * @dev: Device to thaw.
1261 *
1262 * Thaw a device under the assumption that its pm_domain field points to the
1263 * domain member of an object of type struct generic_pm_domain representing
1264 * a power domain consisting of I/O devices.
1265 */
1266static int pm_genpd_thaw(struct device *dev)
1267{
1268 struct generic_pm_domain *genpd;
1269
1270 dev_dbg(dev, "%s()\n", __func__);
1271
1272 genpd = dev_to_genpd(dev);
1273 if (IS_ERR(genpd))
1274 return -EINVAL;
1275
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001276 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001277}
1278
1279/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001280 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001281 * @dev: Device to resume.
1282 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001283 * Make sure the domain will be in the same power state as before the
1284 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001285 */
1286static int pm_genpd_restore_noirq(struct device *dev)
1287{
1288 struct generic_pm_domain *genpd;
1289
1290 dev_dbg(dev, "%s()\n", __func__);
1291
1292 genpd = dev_to_genpd(dev);
1293 if (IS_ERR(genpd))
1294 return -EINVAL;
1295
1296 /*
1297 * Since all of the "noirq" callbacks are executed sequentially, it is
1298 * guaranteed that this function will never run twice in parallel for
1299 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001300 *
1301 * At this point suspended_count == 0 means we are being run for the
1302 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001303 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001304 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001305 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001306 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001307 * so make it appear as powered off to pm_genpd_sync_poweron(),
1308 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001309 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001310 genpd->status = GPD_STATE_POWER_OFF;
1311 if (genpd->suspend_power_off) {
1312 /*
1313 * If the domain was off before the hibernation, make
1314 * sure it will be off going forward.
1315 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001316 genpd_power_off(genpd, true);
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001317
1318 return 0;
1319 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001320 }
1321
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001322 if (genpd->suspend_power_off)
1323 return 0;
1324
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001325 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001326
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001327 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001328}
1329
1330/**
1331 * pm_genpd_complete - Complete power transition of a device in a power domain.
1332 * @dev: Device to complete the transition of.
1333 *
1334 * Complete a power transition of a device (during a system-wide power
1335 * transition) under the assumption that its pm_domain field points to the
1336 * domain member of an object of type struct generic_pm_domain representing
1337 * a power domain consisting of I/O devices.
1338 */
1339static void pm_genpd_complete(struct device *dev)
1340{
1341 struct generic_pm_domain *genpd;
1342 bool run_complete;
1343
1344 dev_dbg(dev, "%s()\n", __func__);
1345
1346 genpd = dev_to_genpd(dev);
1347 if (IS_ERR(genpd))
1348 return;
1349
1350 mutex_lock(&genpd->lock);
1351
1352 run_complete = !genpd->suspend_power_off;
1353 if (--genpd->prepared_count == 0)
1354 genpd->suspend_power_off = false;
1355
1356 mutex_unlock(&genpd->lock);
1357
1358 if (run_complete) {
1359 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001360 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001361 pm_runtime_enable(dev);
Ulf Hanssonaf939332013-04-12 09:41:06 +00001362 pm_request_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001363 }
1364}
1365
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001366/**
Ulf Hanssond47e6462014-09-03 12:52:24 +02001367 * genpd_syscore_switch - Switch power during system core suspend or resume.
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001368 * @dev: Device that normally is marked as "always on" to switch power for.
1369 *
1370 * This routine may only be called during the system core (syscore) suspend or
1371 * resume phase for devices whose "always on" flags are set.
1372 */
Ulf Hanssond47e6462014-09-03 12:52:24 +02001373static void genpd_syscore_switch(struct device *dev, bool suspend)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001374{
1375 struct generic_pm_domain *genpd;
1376
1377 genpd = dev_to_genpd(dev);
1378 if (!pm_genpd_present(genpd))
1379 return;
1380
1381 if (suspend) {
1382 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001383 pm_genpd_sync_poweroff(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001384 } else {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001385 pm_genpd_sync_poweron(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001386 genpd->suspended_count--;
1387 }
1388}
Ulf Hanssond47e6462014-09-03 12:52:24 +02001389
1390void pm_genpd_syscore_poweroff(struct device *dev)
1391{
1392 genpd_syscore_switch(dev, true);
1393}
1394EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1395
1396void pm_genpd_syscore_poweron(struct device *dev)
1397{
1398 genpd_syscore_switch(dev, false);
1399}
1400EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001401
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001402#else /* !CONFIG_PM_SLEEP */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001403
1404#define pm_genpd_prepare NULL
1405#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001406#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001407#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001408#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001409#define pm_genpd_resume_noirq NULL
1410#define pm_genpd_resume NULL
1411#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001412#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001413#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001414#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001415#define pm_genpd_thaw_noirq NULL
1416#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001417#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001418#define pm_genpd_complete NULL
1419
1420#endif /* CONFIG_PM_SLEEP */
1421
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001422static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1423 struct generic_pm_domain *genpd,
1424 struct gpd_timing_data *td)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001425{
1426 struct generic_pm_domain_data *gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001427 int ret;
1428
1429 ret = dev_pm_get_subsys_data(dev);
1430 if (ret)
1431 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001432
1433 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
Ulf Hansson3e235682015-01-27 21:13:43 +01001434 if (!gpd_data) {
1435 ret = -ENOMEM;
1436 goto err_put;
1437 }
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001438
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001439 if (td)
1440 gpd_data->td = *td;
1441
1442 gpd_data->base.dev = dev;
1443 gpd_data->need_restore = -1;
1444 gpd_data->td.constraint_changed = true;
1445 gpd_data->td.effective_constraint_ns = -1;
1446 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1447
1448 spin_lock_irq(&dev->power.lock);
1449
1450 if (dev->power.subsys_data->domain_data) {
1451 ret = -EINVAL;
1452 goto err_free;
1453 }
1454
1455 dev->power.subsys_data->domain_data = &gpd_data->base;
1456 dev->pm_domain = &genpd->domain;
1457
1458 spin_unlock_irq(&dev->power.lock);
1459
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001460 return gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001461
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001462 err_free:
1463 spin_unlock_irq(&dev->power.lock);
1464 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001465 err_put:
1466 dev_pm_put_subsys_data(dev);
1467 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001468}
1469
Ulf Hansson49d400c2015-01-27 21:13:38 +01001470static void genpd_free_dev_data(struct device *dev,
1471 struct generic_pm_domain_data *gpd_data)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001472{
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001473 spin_lock_irq(&dev->power.lock);
1474
1475 dev->pm_domain = NULL;
1476 dev->power.subsys_data->domain_data = NULL;
1477
1478 spin_unlock_irq(&dev->power.lock);
1479
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001480 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001481 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001482}
1483
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001484/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001485 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001486 * @genpd: PM domain to add the device to.
1487 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001488 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001489 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001490int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1491 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001492{
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001493 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001494 int ret = 0;
1495
1496 dev_dbg(dev, "%s()\n", __func__);
1497
1498 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1499 return -EINVAL;
1500
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001501 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
Ulf Hansson3e235682015-01-27 21:13:43 +01001502 if (IS_ERR(gpd_data))
1503 return PTR_ERR(gpd_data);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001504
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001505 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001506
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001507 if (genpd->prepared_count > 0) {
1508 ret = -EAGAIN;
1509 goto out;
1510 }
1511
Ulf Hanssonb472c2f2015-01-27 21:13:45 +01001512 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1513 if (ret)
1514 goto out;
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001515
Ulf Hansson14b53062015-01-27 21:13:40 +01001516 genpd->device_count++;
1517 genpd->max_off_time_changed = true;
1518
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001519 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001520
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001521 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001522 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001523
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001524 if (ret)
1525 genpd_free_dev_data(dev, gpd_data);
1526 else
1527 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001528
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001529 return ret;
1530}
1531
1532/**
Rafael J. Wysockib5abb082012-08-07 01:06:11 +02001533 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1534 * @domain_name: Name of the PM domain to add the device to.
1535 * @dev: Device to be added.
1536 * @td: Set of PM QoS timing parameters to attach to the device.
1537 */
1538int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1539 struct gpd_timing_data *td)
1540{
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +02001541 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
Rafael J. Wysockib5abb082012-08-07 01:06:11 +02001542}
1543
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001544/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001545 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1546 * @genpd: PM domain to remove the device from.
1547 * @dev: Device to be removed.
1548 */
1549int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1550 struct device *dev)
1551{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001552 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001553 struct pm_domain_data *pdd;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001554 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001555
1556 dev_dbg(dev, "%s()\n", __func__);
1557
Russell Kingdf6a0d62015-03-20 17:20:38 +00001558 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001559 return -EINVAL;
1560
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001561 /* The above validation also means we have existing domain_data. */
1562 pdd = dev->power.subsys_data->domain_data;
1563 gpd_data = to_gpd_data(pdd);
1564 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1565
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001566 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001567
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001568 if (genpd->prepared_count > 0) {
1569 ret = -EAGAIN;
1570 goto out;
1571 }
1572
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001573 genpd->device_count--;
1574 genpd->max_off_time_changed = true;
1575
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001576 if (genpd->detach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001577 genpd->detach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001578
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001579 list_del_init(&pdd->list_node);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001580
1581 genpd_release_lock(genpd);
1582
Ulf Hanssonc1dbe2f2015-01-27 21:13:39 +01001583 genpd_free_dev_data(dev, gpd_data);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001584
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001585 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001586
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001587 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001588 genpd_release_lock(genpd);
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001589 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001590
1591 return ret;
1592}
1593
1594/**
1595 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1596 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001597 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001598 */
1599int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001600 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001601{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001602 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001603 int ret = 0;
1604
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001605 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1606 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001607 return -EINVAL;
1608
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001609 start:
1610 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001611 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001612
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001613 if (subdomain->status != GPD_STATE_POWER_OFF
1614 && subdomain->status != GPD_STATE_ACTIVE) {
1615 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001616 genpd_release_lock(genpd);
1617 goto start;
1618 }
1619
1620 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001621 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001622 ret = -EINVAL;
1623 goto out;
1624 }
1625
Huang Ying4fcac102012-05-07 21:35:45 +02001626 list_for_each_entry(link, &genpd->master_links, master_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001627 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001628 ret = -EINVAL;
1629 goto out;
1630 }
1631 }
1632
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001633 link = kzalloc(sizeof(*link), GFP_KERNEL);
1634 if (!link) {
1635 ret = -ENOMEM;
1636 goto out;
1637 }
1638 link->master = genpd;
1639 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001640 link->slave = subdomain;
1641 list_add_tail(&link->slave_node, &subdomain->slave_links);
1642 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001643 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001644
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001645 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001646 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001647 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001648
1649 return ret;
1650}
1651
1652/**
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001653 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1654 * @master_name: Name of the master PM domain to add the subdomain to.
1655 * @subdomain_name: Name of the subdomain to be added.
1656 */
1657int pm_genpd_add_subdomain_names(const char *master_name,
1658 const char *subdomain_name)
1659{
1660 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1661
1662 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1663 return -EINVAL;
1664
1665 mutex_lock(&gpd_list_lock);
1666 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1667 if (!master && !strcmp(gpd->name, master_name))
1668 master = gpd;
1669
1670 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1671 subdomain = gpd;
1672
1673 if (master && subdomain)
1674 break;
1675 }
1676 mutex_unlock(&gpd_list_lock);
1677
1678 return pm_genpd_add_subdomain(master, subdomain);
1679}
1680
1681/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001682 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1683 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001684 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001685 */
1686int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001687 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001688{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001689 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001690 int ret = -EINVAL;
1691
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001692 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001693 return -EINVAL;
1694
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001695 start:
1696 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001697
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001698 list_for_each_entry(link, &genpd->master_links, master_node) {
1699 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001700 continue;
1701
1702 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1703
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001704 if (subdomain->status != GPD_STATE_POWER_OFF
1705 && subdomain->status != GPD_STATE_ACTIVE) {
1706 mutex_unlock(&subdomain->lock);
1707 genpd_release_lock(genpd);
1708 goto start;
1709 }
1710
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001711 list_del(&link->master_node);
1712 list_del(&link->slave_node);
1713 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001714 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001715 genpd_sd_counter_dec(genpd);
1716
1717 mutex_unlock(&subdomain->lock);
1718
1719 ret = 0;
1720 break;
1721 }
1722
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001723 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001724
1725 return ret;
1726}
1727
1728/**
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001729 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1730 * @genpd: PM domain to be connected with cpuidle.
1731 * @state: cpuidle state this domain can disable/enable.
1732 *
1733 * Make a PM domain behave as though it contained a CPU core, that is, instead
1734 * of calling its power down routine it will enable the given cpuidle state so
1735 * that the cpuidle subsystem can power it down (if possible and desirable).
1736 */
1737int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001738{
1739 struct cpuidle_driver *cpuidle_drv;
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001740 struct gpd_cpuidle_data *cpuidle_data;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001741 struct cpuidle_state *idle_state;
1742 int ret = 0;
1743
1744 if (IS_ERR_OR_NULL(genpd) || state < 0)
1745 return -EINVAL;
1746
1747 genpd_acquire_lock(genpd);
1748
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001749 if (genpd->cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001750 ret = -EEXIST;
1751 goto out;
1752 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001753 cpuidle_data = kzalloc(sizeof(*cpuidle_data), GFP_KERNEL);
1754 if (!cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001755 ret = -ENOMEM;
1756 goto out;
1757 }
1758 cpuidle_drv = cpuidle_driver_ref();
1759 if (!cpuidle_drv) {
1760 ret = -ENODEV;
jhbird.choi@samsung.comdebe0812012-10-23 00:54:38 +02001761 goto err_drv;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001762 }
1763 if (cpuidle_drv->state_count <= state) {
1764 ret = -EINVAL;
1765 goto err;
1766 }
1767 idle_state = &cpuidle_drv->states[state];
1768 if (!idle_state->disabled) {
1769 ret = -EAGAIN;
1770 goto err;
1771 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001772 cpuidle_data->idle_state = idle_state;
1773 cpuidle_data->saved_exit_latency = idle_state->exit_latency;
1774 genpd->cpuidle_data = cpuidle_data;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001775 genpd_recalc_cpu_exit_latency(genpd);
1776
1777 out:
1778 genpd_release_lock(genpd);
1779 return ret;
1780
1781 err:
1782 cpuidle_driver_unref();
jhbird.choi@samsung.comdebe0812012-10-23 00:54:38 +02001783
1784 err_drv:
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001785 kfree(cpuidle_data);
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001786 goto out;
1787}
1788
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001789/**
Rafael J. Wysocki74a2799a2012-08-15 20:32:59 +02001790 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1791 * @name: Name of the domain to connect to cpuidle.
1792 * @state: cpuidle state this domain can manipulate.
1793 */
1794int pm_genpd_name_attach_cpuidle(const char *name, int state)
1795{
1796 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1797}
1798
1799/**
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001800 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1801 * @genpd: PM domain to remove the cpuidle connection from.
1802 *
1803 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1804 * given PM domain.
1805 */
1806int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001807{
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001808 struct gpd_cpuidle_data *cpuidle_data;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001809 struct cpuidle_state *idle_state;
1810 int ret = 0;
1811
1812 if (IS_ERR_OR_NULL(genpd))
1813 return -EINVAL;
1814
1815 genpd_acquire_lock(genpd);
1816
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001817 cpuidle_data = genpd->cpuidle_data;
1818 if (!cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001819 ret = -ENODEV;
1820 goto out;
1821 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001822 idle_state = cpuidle_data->idle_state;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001823 if (!idle_state->disabled) {
1824 ret = -EAGAIN;
1825 goto out;
1826 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001827 idle_state->exit_latency = cpuidle_data->saved_exit_latency;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001828 cpuidle_driver_unref();
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001829 genpd->cpuidle_data = NULL;
1830 kfree(cpuidle_data);
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001831
1832 out:
1833 genpd_release_lock(genpd);
1834 return ret;
1835}
1836
Rafael J. Wysocki74a2799a2012-08-15 20:32:59 +02001837/**
1838 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1839 * @name: Name of the domain to disconnect cpuidle from.
1840 */
1841int pm_genpd_name_detach_cpuidle(const char *name)
1842{
1843 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1844}
1845
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001846/* Default device callbacks for generic PM domains. */
1847
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001848/**
Geert Uytterhoeven12e10bb2014-09-16 21:59:39 +02001849 * pm_genpd_default_save_state - Default "save device state" for PM domains.
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001850 * @dev: Device to handle.
1851 */
1852static int pm_genpd_default_save_state(struct device *dev)
1853{
1854 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001855
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001856 if (dev->type && dev->type->pm)
1857 cb = dev->type->pm->runtime_suspend;
1858 else if (dev->class && dev->class->pm)
1859 cb = dev->class->pm->runtime_suspend;
1860 else if (dev->bus && dev->bus->pm)
1861 cb = dev->bus->pm->runtime_suspend;
1862 else
1863 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001864
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001865 if (!cb && dev->driver && dev->driver->pm)
1866 cb = dev->driver->pm->runtime_suspend;
1867
1868 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001869}
1870
1871/**
Geert Uytterhoeven12e10bb2014-09-16 21:59:39 +02001872 * pm_genpd_default_restore_state - Default PM domains "restore device state".
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001873 * @dev: Device to handle.
1874 */
1875static int pm_genpd_default_restore_state(struct device *dev)
1876{
1877 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001878
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001879 if (dev->type && dev->type->pm)
1880 cb = dev->type->pm->runtime_resume;
1881 else if (dev->class && dev->class->pm)
1882 cb = dev->class->pm->runtime_resume;
1883 else if (dev->bus && dev->bus->pm)
1884 cb = dev->bus->pm->runtime_resume;
1885 else
1886 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001887
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001888 if (!cb && dev->driver && dev->driver->pm)
1889 cb = dev->driver->pm->runtime_resume;
1890
1891 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001892}
1893
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001894/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001895 * pm_genpd_init - Initialize a generic I/O PM domain object.
1896 * @genpd: PM domain object to initialize.
1897 * @gov: PM domain governor to associate with the domain (may be NULL).
1898 * @is_off: Initial value of the domain's power_is_off field.
1899 */
1900void pm_genpd_init(struct generic_pm_domain *genpd,
1901 struct dev_power_governor *gov, bool is_off)
1902{
1903 if (IS_ERR_OR_NULL(genpd))
1904 return;
1905
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001906 INIT_LIST_HEAD(&genpd->master_links);
1907 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001908 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001909 mutex_init(&genpd->lock);
1910 genpd->gov = gov;
1911 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1912 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001913 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001914 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1915 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001916 genpd->poweroff_task = NULL;
1917 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001918 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001919 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001920 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001921 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1922 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001923 genpd->domain.ops.prepare = pm_genpd_prepare;
1924 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001925 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001926 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1927 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001928 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001929 genpd->domain.ops.resume = pm_genpd_resume;
1930 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001931 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001932 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1933 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001934 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001935 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001936 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001937 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001938 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001939 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001940 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001941 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001942 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001943 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1944 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Ulf Hanssonc11f6f52014-12-01 12:50:21 +01001945
1946 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1947 genpd->dev_ops.stop = pm_clk_suspend;
1948 genpd->dev_ops.start = pm_clk_resume;
1949 }
1950
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001951 mutex_lock(&gpd_list_lock);
1952 list_add(&genpd->gpd_list_node, &gpd_list);
1953 mutex_unlock(&gpd_list_lock);
1954}
Tomasz Figaaa422402014-09-19 20:27:36 +02001955
1956#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1957/*
1958 * Device Tree based PM domain providers.
1959 *
1960 * The code below implements generic device tree based PM domain providers that
1961 * bind device tree nodes with generic PM domains registered in the system.
1962 *
1963 * Any driver that registers generic PM domains and needs to support binding of
1964 * devices to these domains is supposed to register a PM domain provider, which
1965 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1966 *
1967 * Two simple mapping functions have been provided for convenience:
1968 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1969 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1970 * index.
1971 */
1972
1973/**
1974 * struct of_genpd_provider - PM domain provider registration structure
1975 * @link: Entry in global list of PM domain providers
1976 * @node: Pointer to device tree node of PM domain provider
1977 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1978 * into a PM domain.
1979 * @data: context pointer to be passed into @xlate callback
1980 */
1981struct of_genpd_provider {
1982 struct list_head link;
1983 struct device_node *node;
1984 genpd_xlate_t xlate;
1985 void *data;
1986};
1987
1988/* List of registered PM domain providers. */
1989static LIST_HEAD(of_genpd_providers);
1990/* Mutex to protect the list above. */
1991static DEFINE_MUTEX(of_genpd_mutex);
1992
1993/**
1994 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1995 * @genpdspec: OF phandle args to map into a PM domain
1996 * @data: xlate function private data - pointer to struct generic_pm_domain
1997 *
1998 * This is a generic xlate function that can be used to model PM domains that
1999 * have their own device tree nodes. The private data of xlate function needs
2000 * to be a valid pointer to struct generic_pm_domain.
2001 */
2002struct generic_pm_domain *__of_genpd_xlate_simple(
2003 struct of_phandle_args *genpdspec,
2004 void *data)
2005{
2006 if (genpdspec->args_count != 0)
2007 return ERR_PTR(-EINVAL);
2008 return data;
2009}
2010EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
2011
2012/**
2013 * __of_genpd_xlate_onecell() - Xlate function using a single index.
2014 * @genpdspec: OF phandle args to map into a PM domain
2015 * @data: xlate function private data - pointer to struct genpd_onecell_data
2016 *
2017 * This is a generic xlate function that can be used to model simple PM domain
2018 * controllers that have one device tree node and provide multiple PM domains.
2019 * A single cell is used as an index into an array of PM domains specified in
2020 * the genpd_onecell_data struct when registering the provider.
2021 */
2022struct generic_pm_domain *__of_genpd_xlate_onecell(
2023 struct of_phandle_args *genpdspec,
2024 void *data)
2025{
2026 struct genpd_onecell_data *genpd_data = data;
2027 unsigned int idx = genpdspec->args[0];
2028
2029 if (genpdspec->args_count != 1)
2030 return ERR_PTR(-EINVAL);
2031
2032 if (idx >= genpd_data->num_domains) {
2033 pr_err("%s: invalid domain index %u\n", __func__, idx);
2034 return ERR_PTR(-EINVAL);
2035 }
2036
2037 if (!genpd_data->domains[idx])
2038 return ERR_PTR(-ENOENT);
2039
2040 return genpd_data->domains[idx];
2041}
2042EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
2043
2044/**
2045 * __of_genpd_add_provider() - Register a PM domain provider for a node
2046 * @np: Device node pointer associated with the PM domain provider.
2047 * @xlate: Callback for decoding PM domain from phandle arguments.
2048 * @data: Context pointer for @xlate callback.
2049 */
2050int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2051 void *data)
2052{
2053 struct of_genpd_provider *cp;
2054
2055 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2056 if (!cp)
2057 return -ENOMEM;
2058
2059 cp->node = of_node_get(np);
2060 cp->data = data;
2061 cp->xlate = xlate;
2062
2063 mutex_lock(&of_genpd_mutex);
2064 list_add(&cp->link, &of_genpd_providers);
2065 mutex_unlock(&of_genpd_mutex);
2066 pr_debug("Added domain provider from %s\n", np->full_name);
2067
2068 return 0;
2069}
2070EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
2071
2072/**
2073 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2074 * @np: Device node pointer associated with the PM domain provider
2075 */
2076void of_genpd_del_provider(struct device_node *np)
2077{
2078 struct of_genpd_provider *cp;
2079
2080 mutex_lock(&of_genpd_mutex);
2081 list_for_each_entry(cp, &of_genpd_providers, link) {
2082 if (cp->node == np) {
2083 list_del(&cp->link);
2084 of_node_put(cp->node);
2085 kfree(cp);
2086 break;
2087 }
2088 }
2089 mutex_unlock(&of_genpd_mutex);
2090}
2091EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2092
2093/**
2094 * of_genpd_get_from_provider() - Look-up PM domain
2095 * @genpdspec: OF phandle args to use for look-up
2096 *
2097 * Looks for a PM domain provider under the node specified by @genpdspec and if
2098 * found, uses xlate function of the provider to map phandle args to a PM
2099 * domain.
2100 *
2101 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2102 * on failure.
2103 */
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05302104struct generic_pm_domain *of_genpd_get_from_provider(
Tomasz Figaaa422402014-09-19 20:27:36 +02002105 struct of_phandle_args *genpdspec)
2106{
2107 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2108 struct of_genpd_provider *provider;
2109
2110 mutex_lock(&of_genpd_mutex);
2111
2112 /* Check if we have such a provider in our array */
2113 list_for_each_entry(provider, &of_genpd_providers, link) {
2114 if (provider->node == genpdspec->np)
2115 genpd = provider->xlate(genpdspec, provider->data);
2116 if (!IS_ERR(genpd))
2117 break;
2118 }
2119
2120 mutex_unlock(&of_genpd_mutex);
2121
2122 return genpd;
2123}
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05302124EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
Tomasz Figaaa422402014-09-19 20:27:36 +02002125
2126/**
2127 * genpd_dev_pm_detach - Detach a device from its PM domain.
2128 * @dev: Device to attach.
2129 * @power_off: Currently not used
2130 *
2131 * Try to locate a corresponding generic PM domain, which the device was
2132 * attached to previously. If such is found, the device is detached from it.
2133 */
2134static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2135{
Russell King446d9992015-03-20 17:20:33 +00002136 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002137 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02002138 int ret = 0;
2139
Russell King446d9992015-03-20 17:20:33 +00002140 pd = pm_genpd_lookup_dev(dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02002141 if (!pd)
2142 return;
2143
2144 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2145
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002146 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02002147 ret = pm_genpd_remove_device(pd, dev);
2148 if (ret != -EAGAIN)
2149 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002150
2151 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02002152 cond_resched();
2153 }
2154
2155 if (ret < 0) {
2156 dev_err(dev, "failed to remove from PM domain %s: %d",
2157 pd->name, ret);
2158 return;
2159 }
2160
2161 /* Check if PM domain can be powered off after removing this device. */
2162 genpd_queue_power_off_work(pd);
2163}
2164
Russell King632f7ce2015-03-20 15:55:12 +01002165static void genpd_dev_pm_sync(struct device *dev)
2166{
2167 struct generic_pm_domain *pd;
2168
2169 pd = dev_to_genpd(dev);
2170 if (IS_ERR(pd))
2171 return;
2172
2173 genpd_queue_power_off_work(pd);
2174}
2175
Tomasz Figaaa422402014-09-19 20:27:36 +02002176/**
2177 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2178 * @dev: Device to attach.
2179 *
2180 * Parse device's OF node to find a PM domain specifier. If such is found,
2181 * attaches the device to retrieved pm_domain ops.
2182 *
2183 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2184 * backwards compatibility with existing DTBs.
2185 *
2186 * Returns 0 on successfully attached PM domain or negative error code.
2187 */
2188int genpd_dev_pm_attach(struct device *dev)
2189{
2190 struct of_phandle_args pd_args;
2191 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002192 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02002193 int ret;
2194
2195 if (!dev->of_node)
2196 return -ENODEV;
2197
2198 if (dev->pm_domain)
2199 return -EEXIST;
2200
2201 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2202 "#power-domain-cells", 0, &pd_args);
2203 if (ret < 0) {
2204 if (ret != -ENOENT)
2205 return ret;
2206
2207 /*
2208 * Try legacy Samsung-specific bindings
2209 * (for backwards compatibility of DT ABI)
2210 */
2211 pd_args.args_count = 0;
2212 pd_args.np = of_parse_phandle(dev->of_node,
2213 "samsung,power-domain", 0);
2214 if (!pd_args.np)
2215 return -ENOENT;
2216 }
2217
2218 pd = of_genpd_get_from_provider(&pd_args);
2219 if (IS_ERR(pd)) {
2220 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2221 __func__, PTR_ERR(pd));
2222 of_node_put(dev->of_node);
2223 return PTR_ERR(pd);
2224 }
2225
2226 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2227
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002228 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02002229 ret = pm_genpd_add_device(pd, dev);
2230 if (ret != -EAGAIN)
2231 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02002232
2233 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02002234 cond_resched();
2235 }
2236
2237 if (ret < 0) {
2238 dev_err(dev, "failed to add to PM domain %s: %d",
2239 pd->name, ret);
2240 of_node_put(dev->of_node);
2241 return ret;
2242 }
2243
2244 dev->pm_domain->detach = genpd_dev_pm_detach;
Russell King632f7ce2015-03-20 15:55:12 +01002245 dev->pm_domain->sync = genpd_dev_pm_sync;
Ulf Hansson2ed12762014-11-17 16:19:10 +01002246 pm_genpd_poweron(pd);
Tomasz Figaaa422402014-09-19 20:27:36 +02002247
2248 return 0;
2249}
2250EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01002251#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002252
2253
2254/*** debugfs support ***/
2255
2256#ifdef CONFIG_PM_ADVANCED_DEBUG
2257#include <linux/pm.h>
2258#include <linux/device.h>
2259#include <linux/debugfs.h>
2260#include <linux/seq_file.h>
2261#include <linux/init.h>
2262#include <linux/kobject.h>
2263static struct dentry *pm_genpd_debugfs_dir;
2264
2265/*
2266 * TODO: This function is a slightly modified version of rtpm_status_show
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01002267 * from sysfs.c, so generalize it.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002268 */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002269static void rtpm_status_str(struct seq_file *s, struct device *dev)
2270{
2271 static const char * const status_lookup[] = {
2272 [RPM_ACTIVE] = "active",
2273 [RPM_RESUMING] = "resuming",
2274 [RPM_SUSPENDED] = "suspended",
2275 [RPM_SUSPENDING] = "suspending"
2276 };
2277 const char *p = "";
2278
2279 if (dev->power.runtime_error)
2280 p = "error";
2281 else if (dev->power.disable_depth)
2282 p = "unsupported";
2283 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2284 p = status_lookup[dev->power.runtime_status];
2285 else
2286 WARN_ON(1);
2287
2288 seq_puts(s, p);
2289}
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002290
2291static int pm_genpd_summary_one(struct seq_file *s,
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002292 struct generic_pm_domain *genpd)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002293{
2294 static const char * const status_lookup[] = {
2295 [GPD_STATE_ACTIVE] = "on",
2296 [GPD_STATE_WAIT_MASTER] = "wait-master",
2297 [GPD_STATE_BUSY] = "busy",
2298 [GPD_STATE_REPEAT] = "off-in-progress",
2299 [GPD_STATE_POWER_OFF] = "off"
2300 };
2301 struct pm_domain_data *pm_data;
2302 const char *kobj_path;
2303 struct gpd_link *link;
2304 int ret;
2305
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002306 ret = mutex_lock_interruptible(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002307 if (ret)
2308 return -ERESTARTSYS;
2309
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002310 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002311 goto exit;
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002312 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002313
2314 /*
2315 * Modifications on the list require holding locks on both
2316 * master and slave, so we are safe.
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002317 * Also genpd->name is immutable.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002318 */
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002319 list_for_each_entry(link, &genpd->master_links, master_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002320 seq_printf(s, "%s", link->slave->name);
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002321 if (!list_is_last(&link->master_node, &genpd->master_links))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002322 seq_puts(s, ", ");
2323 }
2324
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002325 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002326 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
2327 if (kobj_path == NULL)
2328 continue;
2329
2330 seq_printf(s, "\n %-50s ", kobj_path);
2331 rtpm_status_str(s, pm_data->dev);
2332 kfree(kobj_path);
2333 }
2334
2335 seq_puts(s, "\n");
2336exit:
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002337 mutex_unlock(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002338
2339 return 0;
2340}
2341
2342static int pm_genpd_summary_show(struct seq_file *s, void *data)
2343{
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002344 struct generic_pm_domain *genpd;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002345 int ret = 0;
2346
2347 seq_puts(s, " domain status slaves\n");
2348 seq_puts(s, " /device runtime status\n");
2349 seq_puts(s, "----------------------------------------------------------------------\n");
2350
2351 ret = mutex_lock_interruptible(&gpd_list_lock);
2352 if (ret)
2353 return -ERESTARTSYS;
2354
Kevin Hilman66a5ca42015-03-02 11:24:28 -08002355 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2356 ret = pm_genpd_summary_one(s, genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002357 if (ret)
2358 break;
2359 }
2360 mutex_unlock(&gpd_list_lock);
2361
2362 return ret;
2363}
2364
2365static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2366{
2367 return single_open(file, pm_genpd_summary_show, NULL);
2368}
2369
2370static const struct file_operations pm_genpd_summary_fops = {
2371 .open = pm_genpd_summary_open,
2372 .read = seq_read,
2373 .llseek = seq_lseek,
2374 .release = single_release,
2375};
2376
2377static int __init pm_genpd_debug_init(void)
2378{
2379 struct dentry *d;
2380
2381 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2382
2383 if (!pm_genpd_debugfs_dir)
2384 return -ENOMEM;
2385
2386 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2387 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2388 if (!d)
2389 return -ENOMEM;
2390
2391 return 0;
2392}
2393late_initcall(pm_genpd_debug_init);
2394
2395static void __exit pm_genpd_debug_exit(void)
2396{
2397 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2398}
2399__exitcall(pm_genpd_debug_exit);
2400#endif /* CONFIG_PM_ADVANCED_DEBUG */