blob: 52172754ff786c9bfd17958231dcf9d623536f43 [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
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/io.h>
12#include <linux/pm_runtime.h>
13#include <linux/pm_domain.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020014#include <linux/pm_qos.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020015#include <linux/slab.h>
16#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020017#include <linux/sched.h>
18#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010019#include <linux/export.h>
20
21#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
22({ \
23 type (*__routine)(struct device *__d); \
24 type __ret = (type)0; \
25 \
26 __routine = genpd->dev_ops.callback; \
27 if (__routine) { \
28 __ret = __routine(dev); \
29 } else { \
30 __routine = dev_gpd_data(dev)->ops.callback; \
31 if (__routine) \
32 __ret = __routine(dev); \
33 } \
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; \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010045 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
46 __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. Wysocki52480512011-07-01 22:13:10 +020056#ifdef CONFIG_PM
57
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010058struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020059{
60 if (IS_ERR_OR_NULL(dev->pm_domain))
61 return ERR_PTR(-EINVAL);
62
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020063 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020064}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020065
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010066static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
67{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010068 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
69 stop_latency_ns, "stop");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010070}
71
72static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
73{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010074 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
75 start_latency_ns, "start");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010076}
77
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020078static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020079{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020080 bool ret = false;
81
82 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
83 ret = !!atomic_dec_and_test(&genpd->sd_count);
84
85 return ret;
86}
87
88static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
89{
90 atomic_inc(&genpd->sd_count);
91 smp_mb__after_atomic_inc();
Rafael J. Wysockif7218892011-07-01 22:12:45 +020092}
93
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020094static void genpd_acquire_lock(struct generic_pm_domain *genpd)
95{
96 DEFINE_WAIT(wait);
97
98 mutex_lock(&genpd->lock);
99 /*
100 * Wait for the domain to transition into either the active,
101 * or the power off state.
102 */
103 for (;;) {
104 prepare_to_wait(&genpd->status_wait_queue, &wait,
105 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200106 if (genpd->status == GPD_STATE_ACTIVE
107 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200108 break;
109 mutex_unlock(&genpd->lock);
110
111 schedule();
112
113 mutex_lock(&genpd->lock);
114 }
115 finish_wait(&genpd->status_wait_queue, &wait);
116}
117
118static void genpd_release_lock(struct generic_pm_domain *genpd)
119{
120 mutex_unlock(&genpd->lock);
121}
122
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200123static void genpd_set_active(struct generic_pm_domain *genpd)
124{
125 if (genpd->resume_count == 0)
126 genpd->status = GPD_STATE_ACTIVE;
127}
128
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200129static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
130{
131 s64 usecs64;
132
133 if (!genpd->cpu_data)
134 return;
135
136 usecs64 = genpd->power_on_latency_ns;
137 do_div(usecs64, NSEC_PER_USEC);
138 usecs64 += genpd->cpu_data->saved_exit_latency;
139 genpd->cpu_data->idle_state->exit_latency = usecs64;
140}
141
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200142/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200143 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200144 * @genpd: PM domain to power up.
145 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200146 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200147 * resume a device belonging to it.
148 */
Sachin Kamat8951ef02012-07-10 21:47:07 +0200149static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200150 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200151{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200152 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200153 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200154 int ret = 0;
155
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200156 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200157 for (;;) {
158 prepare_to_wait(&genpd->status_wait_queue, &wait,
159 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200160 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200161 break;
162 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200163
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200164 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200165
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200166 mutex_lock(&genpd->lock);
167 }
168 finish_wait(&genpd->status_wait_queue, &wait);
169
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200170 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200171 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200172 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200173
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200174 if (genpd->status != GPD_STATE_POWER_OFF) {
175 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200176 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200177 }
178
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200179 if (genpd->cpu_data) {
180 cpuidle_pause_and_lock();
181 genpd->cpu_data->idle_state->disabled = true;
182 cpuidle_resume_and_unlock();
183 goto out;
184 }
185
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200186 /*
187 * The list is guaranteed not to change while the loop below is being
188 * executed, unless one of the masters' .power_on() callbacks fiddles
189 * with it.
190 */
191 list_for_each_entry(link, &genpd->slave_links, slave_node) {
192 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200193 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200194
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200195 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200196
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200197 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200198
199 mutex_lock(&genpd->lock);
200
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200201 /*
202 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200203 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200204 */
205 genpd->status = GPD_STATE_POWER_OFF;
206 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200207 if (ret) {
208 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200209 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200210 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200211 }
212
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200213 if (genpd->power_on) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100214 ktime_t time_start = ktime_get();
215 s64 elapsed_ns;
216
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200217 ret = genpd->power_on(genpd);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200218 if (ret)
219 goto err;
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100220
221 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100222 if (elapsed_ns > genpd->power_on_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100223 genpd->power_on_latency_ns = elapsed_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200224 genpd->max_off_time_changed = true;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200225 genpd_recalc_cpu_exit_latency(genpd);
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100226 if (genpd->name)
227 pr_warning("%s: Power-on latency exceeded, "
228 "new value %lld ns\n", genpd->name,
229 elapsed_ns);
230 }
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200231 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200232
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200233 out:
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200234 genpd_set_active(genpd);
235
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200236 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200237
238 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200239 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
240 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200241
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200242 return ret;
243}
244
245/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200246 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200247 * @genpd: PM domain to power up.
248 */
249int pm_genpd_poweron(struct generic_pm_domain *genpd)
250{
251 int ret;
252
253 mutex_lock(&genpd->lock);
254 ret = __pm_genpd_poweron(genpd);
255 mutex_unlock(&genpd->lock);
256 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200257}
258
259#endif /* CONFIG_PM */
260
261#ifdef CONFIG_PM_RUNTIME
262
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200263static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
264{
265 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
266 save_state_latency_ns, "state save");
267}
268
269static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
270{
271 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
272 restore_state_latency_ns,
273 "state restore");
274}
275
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200276static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
277 unsigned long val, void *ptr)
278{
279 struct generic_pm_domain_data *gpd_data;
280 struct device *dev;
281
282 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
283
284 mutex_lock(&gpd_data->lock);
285 dev = gpd_data->base.dev;
286 if (!dev) {
287 mutex_unlock(&gpd_data->lock);
288 return NOTIFY_DONE;
289 }
290 mutex_unlock(&gpd_data->lock);
291
292 for (;;) {
293 struct generic_pm_domain *genpd;
294 struct pm_domain_data *pdd;
295
296 spin_lock_irq(&dev->power.lock);
297
298 pdd = dev->power.subsys_data ?
299 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200300 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200301 to_gpd_data(pdd)->td.constraint_changed = true;
302 genpd = dev_to_genpd(dev);
303 } else {
304 genpd = ERR_PTR(-ENODATA);
305 }
306
307 spin_unlock_irq(&dev->power.lock);
308
309 if (!IS_ERR(genpd)) {
310 mutex_lock(&genpd->lock);
311 genpd->max_off_time_changed = true;
312 mutex_unlock(&genpd->lock);
313 }
314
315 dev = dev->parent;
316 if (!dev || dev->power.ignore_children)
317 break;
318 }
319
320 return NOTIFY_DONE;
321}
322
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200323/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200324 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200325 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200326 * @genpd: PM domain the device belongs to.
327 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200328static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200329 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200330 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200331{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200332 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200333 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200334 int ret = 0;
335
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200336 if (gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200337 return 0;
338
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200339 mutex_unlock(&genpd->lock);
340
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100341 genpd_start_dev(genpd, dev);
342 ret = genpd_save_dev(genpd, dev);
343 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200344
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200345 mutex_lock(&genpd->lock);
346
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200347 if (!ret)
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200348 gpd_data->need_restore = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200349
350 return ret;
351}
352
353/**
354 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200355 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200356 * @genpd: PM domain the device belongs to.
357 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200358static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200359 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200360 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200361{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200362 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200363 struct device *dev = pdd->dev;
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200364 bool need_restore = gpd_data->need_restore;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200365
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200366 gpd_data->need_restore = false;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200367 mutex_unlock(&genpd->lock);
368
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100369 genpd_start_dev(genpd, dev);
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200370 if (need_restore)
371 genpd_restore_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200372
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200373 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200374}
375
376/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200377 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
378 * @genpd: PM domain to check.
379 *
380 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
381 * a "power off" operation, which means that a "power on" has occured in the
382 * meantime, or if its resume_count field is different from zero, which means
383 * that one of its devices has been resumed in the meantime.
384 */
385static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
386{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200387 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200388 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200389}
390
391/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200392 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
393 * @genpd: PM domait to power off.
394 *
395 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
396 * before.
397 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200398void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200399{
400 if (!work_pending(&genpd->power_off_work))
401 queue_work(pm_wq, &genpd->power_off_work);
402}
403
404/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200405 * pm_genpd_poweroff - Remove power from a given PM domain.
406 * @genpd: PM domain to power down.
407 *
408 * If all of the @genpd's devices have been suspended and all of its subdomains
409 * have been powered down, run the runtime suspend callbacks provided by all of
410 * the @genpd's devices' drivers and remove power from @genpd.
411 */
412static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200413 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200414{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200415 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200416 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200417 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200418 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200419
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200420 start:
421 /*
422 * Do not try to power off the domain in the following situations:
423 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200424 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200425 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200426 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200427 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200428 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200429 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200430 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200431 return 0;
432
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200433 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200434 return -EBUSY;
435
436 not_suspended = 0;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200437 list_for_each_entry(pdd, &genpd->dev_list, list_node)
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200438 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200439 || pdd->dev->power.irq_safe || pdd->dev->power.syscore))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200440 not_suspended++;
441
442 if (not_suspended > genpd->in_progress)
443 return -EBUSY;
444
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200445 if (genpd->poweroff_task) {
446 /*
447 * Another instance of pm_genpd_poweroff() is executing
448 * callbacks, so tell it to start over and return.
449 */
450 genpd->status = GPD_STATE_REPEAT;
451 return 0;
452 }
453
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200454 if (genpd->gov && genpd->gov->power_down_ok) {
455 if (!genpd->gov->power_down_ok(&genpd->domain))
456 return -EAGAIN;
457 }
458
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200459 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200460 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200461
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200462 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200463 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200464 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200465
466 if (genpd_abort_poweroff(genpd))
467 goto out;
468
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200469 if (ret) {
470 genpd_set_active(genpd);
471 goto out;
472 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200473
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200474 if (genpd->status == GPD_STATE_REPEAT) {
475 genpd->poweroff_task = NULL;
476 goto start;
477 }
478 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200479
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200480 if (genpd->cpu_data) {
481 /*
482 * If cpu_data is set, cpuidle should turn the domain off when
483 * the CPU in it is idle. In that case we don't decrement the
484 * subdomain counts of the master domains, so that power is not
485 * removed from the current domain prematurely as a result of
486 * cutting off the masters' power.
487 */
488 genpd->status = GPD_STATE_POWER_OFF;
489 cpuidle_pause_and_lock();
490 genpd->cpu_data->idle_state->disabled = false;
491 cpuidle_resume_and_unlock();
492 goto out;
493 }
494
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200495 if (genpd->power_off) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100496 ktime_t time_start;
497 s64 elapsed_ns;
498
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200499 if (atomic_read(&genpd->sd_count) > 0) {
500 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200501 goto out;
502 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200503
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100504 time_start = ktime_get();
505
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200506 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200507 * If sd_count > 0 at this point, one of the subdomains hasn't
508 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200509 * incrementing it. In that case pm_genpd_poweron() will wait
510 * for us to drop the lock, so we can call .power_off() and let
511 * the pm_genpd_poweron() restore power for us (this shouldn't
512 * happen very often).
513 */
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200514 ret = genpd->power_off(genpd);
515 if (ret == -EBUSY) {
516 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200517 goto out;
518 }
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100519
520 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100521 if (elapsed_ns > genpd->power_off_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100522 genpd->power_off_latency_ns = elapsed_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200523 genpd->max_off_time_changed = true;
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100524 if (genpd->name)
525 pr_warning("%s: Power-off latency exceeded, "
526 "new value %lld ns\n", genpd->name,
527 elapsed_ns);
528 }
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200529 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200530
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200531 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100532
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200533 list_for_each_entry(link, &genpd->slave_links, slave_node) {
534 genpd_sd_counter_dec(link->master);
535 genpd_queue_power_off_work(link->master);
536 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200537
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200538 out:
539 genpd->poweroff_task = NULL;
540 wake_up_all(&genpd->status_wait_queue);
541 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200542}
543
544/**
545 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
546 * @work: Work structure used for scheduling the execution of this function.
547 */
548static void genpd_power_off_work_fn(struct work_struct *work)
549{
550 struct generic_pm_domain *genpd;
551
552 genpd = container_of(work, struct generic_pm_domain, power_off_work);
553
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200554 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200555 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200556 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200557}
558
559/**
560 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
561 * @dev: Device to suspend.
562 *
563 * Carry out a runtime suspend of a device under the assumption that its
564 * pm_domain field points to the domain member of an object of type
565 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
566 */
567static int pm_genpd_runtime_suspend(struct device *dev)
568{
569 struct generic_pm_domain *genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100570 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100571 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200572
573 dev_dbg(dev, "%s()\n", __func__);
574
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200575 genpd = dev_to_genpd(dev);
576 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200577 return -EINVAL;
578
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200579 might_sleep_if(!genpd->dev_irq_safe);
580
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100581 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
582 if (stop_ok && !stop_ok(dev))
583 return -EBUSY;
584
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100585 ret = genpd_stop_dev(genpd, dev);
586 if (ret)
587 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200588
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200589 /*
590 * If power.irq_safe is set, this routine will be run with interrupts
591 * off, so it can't use mutexes.
592 */
593 if (dev->power.irq_safe)
594 return 0;
595
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200596 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200597 genpd->in_progress++;
598 pm_genpd_poweroff(genpd);
599 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200600 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200601
602 return 0;
603}
604
605/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200606 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
607 * @dev: Device to resume.
608 *
609 * Carry out a runtime resume of a device under the assumption that its
610 * pm_domain field points to the domain member of an object of type
611 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
612 */
613static int pm_genpd_runtime_resume(struct device *dev)
614{
615 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200616 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200617 int ret;
618
619 dev_dbg(dev, "%s()\n", __func__);
620
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200621 genpd = dev_to_genpd(dev);
622 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200623 return -EINVAL;
624
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200625 might_sleep_if(!genpd->dev_irq_safe);
626
627 /* If power.irq_safe, the PM domain is never powered off. */
628 if (dev->power.irq_safe)
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200629 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200630
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200631 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200632 ret = __pm_genpd_poweron(genpd);
633 if (ret) {
634 mutex_unlock(&genpd->lock);
635 return ret;
636 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200637 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200638 genpd->resume_count++;
639 for (;;) {
640 prepare_to_wait(&genpd->status_wait_queue, &wait,
641 TASK_UNINTERRUPTIBLE);
642 /*
643 * If current is the powering off task, we have been called
644 * reentrantly from one of the device callbacks, so we should
645 * not wait.
646 */
647 if (!genpd->poweroff_task || genpd->poweroff_task == current)
648 break;
649 mutex_unlock(&genpd->lock);
650
651 schedule();
652
653 mutex_lock(&genpd->lock);
654 }
655 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200656 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200657 genpd->resume_count--;
658 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200659 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200660 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200661
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200662 return 0;
663}
664
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200665/**
666 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
667 */
668void pm_genpd_poweroff_unused(void)
669{
670 struct generic_pm_domain *genpd;
671
672 mutex_lock(&gpd_list_lock);
673
674 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
675 genpd_queue_power_off_work(genpd);
676
677 mutex_unlock(&gpd_list_lock);
678}
679
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200680#else
681
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200682static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
683 unsigned long val, void *ptr)
684{
685 return NOTIFY_DONE;
686}
687
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200688static inline void genpd_power_off_work_fn(struct work_struct *work) {}
689
690#define pm_genpd_runtime_suspend NULL
691#define pm_genpd_runtime_resume NULL
692
693#endif /* CONFIG_PM_RUNTIME */
694
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200695#ifdef CONFIG_PM_SLEEP
696
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200697/**
698 * pm_genpd_present - Check if the given PM domain has been initialized.
699 * @genpd: PM domain to check.
700 */
701static bool pm_genpd_present(struct generic_pm_domain *genpd)
702{
703 struct generic_pm_domain *gpd;
704
705 if (IS_ERR_OR_NULL(genpd))
706 return false;
707
708 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
709 if (gpd == genpd)
710 return true;
711
712 return false;
713}
714
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100715static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
716 struct device *dev)
717{
718 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
719}
720
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100721static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
722{
723 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
724}
725
726static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
727{
728 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
729}
730
731static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
732{
733 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
734}
735
736static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
737{
738 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
739}
740
741static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
742{
743 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
744}
745
746static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
747{
748 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
749}
750
751static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
752{
753 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
754}
755
756static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
757{
758 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
759}
760
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200761/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200762 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200763 * @genpd: PM domain to power off, if possible.
764 *
765 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200766 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200767 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200768 * This function is only called in "noirq" and "syscore" stages of system power
769 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
770 * executed sequentially, so it is guaranteed that it will never run twice in
771 * parallel).
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200772 */
773static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
774{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200775 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200776
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200777 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200778 return;
779
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200780 if (genpd->suspended_count != genpd->device_count
781 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200782 return;
783
784 if (genpd->power_off)
785 genpd->power_off(genpd);
786
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200787 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200788
789 list_for_each_entry(link, &genpd->slave_links, slave_node) {
790 genpd_sd_counter_dec(link->master);
791 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200792 }
793}
794
795/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200796 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
797 * @genpd: PM domain to power on.
798 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200799 * This function is only called in "noirq" and "syscore" stages of system power
800 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
801 * executed sequentially, so it is guaranteed that it will never run twice in
802 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200803 */
804static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
805{
806 struct gpd_link *link;
807
808 if (genpd->status != GPD_STATE_POWER_OFF)
809 return;
810
811 list_for_each_entry(link, &genpd->slave_links, slave_node) {
812 pm_genpd_sync_poweron(link->master);
813 genpd_sd_counter_inc(link->master);
814 }
815
816 if (genpd->power_on)
817 genpd->power_on(genpd);
818
819 genpd->status = GPD_STATE_ACTIVE;
820}
821
822/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200823 * resume_needed - Check whether to resume a device before system suspend.
824 * @dev: Device to check.
825 * @genpd: PM domain the device belongs to.
826 *
827 * There are two cases in which a device that can wake up the system from sleep
828 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
829 * to wake up the system and it has to remain active for this purpose while the
830 * system is in the sleep state and (2) if the device is not enabled to wake up
831 * the system from sleep states and it generally doesn't generate wakeup signals
832 * by itself (those signals are generated on its behalf by other parts of the
833 * system). In the latter case it may be necessary to reconfigure the device's
834 * wakeup settings during system suspend, because it may have been set up to
835 * signal remote wakeup from the system's working state as needed by runtime PM.
836 * Return 'true' in either of the above cases.
837 */
838static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
839{
840 bool active_wakeup;
841
842 if (!device_can_wakeup(dev))
843 return false;
844
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100845 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200846 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
847}
848
849/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200850 * pm_genpd_prepare - Start power transition of a device in a PM domain.
851 * @dev: Device to start the transition of.
852 *
853 * Start a power transition of a device (during a system-wide power transition)
854 * under the assumption that its pm_domain field points to the domain member of
855 * an object of type struct generic_pm_domain representing a PM domain
856 * consisting of I/O devices.
857 */
858static int pm_genpd_prepare(struct device *dev)
859{
860 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200861 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200862
863 dev_dbg(dev, "%s()\n", __func__);
864
865 genpd = dev_to_genpd(dev);
866 if (IS_ERR(genpd))
867 return -EINVAL;
868
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200869 /*
870 * If a wakeup request is pending for the device, it should be woken up
871 * at this point and a system wakeup event should be reported if it's
872 * set up to wake up the system from sleep states.
873 */
874 pm_runtime_get_noresume(dev);
875 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
876 pm_wakeup_event(dev, 0);
877
878 if (pm_wakeup_pending()) {
879 pm_runtime_put_sync(dev);
880 return -EBUSY;
881 }
882
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200883 if (resume_needed(dev, genpd))
884 pm_runtime_resume(dev);
885
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200886 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200887
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100888 if (genpd->prepared_count++ == 0) {
889 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200890 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100891 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200892
893 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200894
895 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200896 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200897 return 0;
898 }
899
900 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200901 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
902 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100903 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200904 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200905 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200906 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200907 __pm_runtime_disable(dev, false);
908
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200909 ret = pm_generic_prepare(dev);
910 if (ret) {
911 mutex_lock(&genpd->lock);
912
913 if (--genpd->prepared_count == 0)
914 genpd->suspend_power_off = false;
915
916 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200917 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200918 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200919
920 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200921 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200922}
923
924/**
925 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
926 * @dev: Device to suspend.
927 *
928 * Suspend a device under the assumption that its pm_domain field points to the
929 * domain member of an object of type struct generic_pm_domain representing
930 * a PM domain consisting of I/O devices.
931 */
932static int pm_genpd_suspend(struct device *dev)
933{
934 struct generic_pm_domain *genpd;
935
936 dev_dbg(dev, "%s()\n", __func__);
937
938 genpd = dev_to_genpd(dev);
939 if (IS_ERR(genpd))
940 return -EINVAL;
941
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100942 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200943}
944
945/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100946 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200947 * @dev: Device to suspend.
948 *
949 * Carry out a late suspend of a device under the assumption that its
950 * pm_domain field points to the domain member of an object of type
951 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
952 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100953static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200954{
955 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200956
957 dev_dbg(dev, "%s()\n", __func__);
958
959 genpd = dev_to_genpd(dev);
960 if (IS_ERR(genpd))
961 return -EINVAL;
962
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100963 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
964}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200965
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100966/**
967 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
968 * @dev: Device to suspend.
969 *
970 * Stop the device and remove power from the domain if all devices in it have
971 * been stopped.
972 */
973static int pm_genpd_suspend_noirq(struct device *dev)
974{
975 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200976
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100977 dev_dbg(dev, "%s()\n", __func__);
978
979 genpd = dev_to_genpd(dev);
980 if (IS_ERR(genpd))
981 return -EINVAL;
982
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200983 if (genpd->suspend_power_off
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100984 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200985 return 0;
986
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100987 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200988
989 /*
990 * Since all of the "noirq" callbacks are executed sequentially, it is
991 * guaranteed that this function will never run twice in parallel for
992 * the same PM domain, so it is not necessary to use locking here.
993 */
994 genpd->suspended_count++;
995 pm_genpd_sync_poweroff(genpd);
996
997 return 0;
998}
999
1000/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001001 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001002 * @dev: Device to resume.
1003 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001004 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001005 */
1006static int pm_genpd_resume_noirq(struct device *dev)
1007{
1008 struct generic_pm_domain *genpd;
1009
1010 dev_dbg(dev, "%s()\n", __func__);
1011
1012 genpd = dev_to_genpd(dev);
1013 if (IS_ERR(genpd))
1014 return -EINVAL;
1015
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001016 if (genpd->suspend_power_off
Rafael J. Wysockicc85b202012-03-13 22:39:31 +01001017 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001018 return 0;
1019
1020 /*
1021 * Since all of the "noirq" callbacks are executed sequentially, it is
1022 * guaranteed that this function will never run twice in parallel for
1023 * the same PM domain, so it is not necessary to use locking here.
1024 */
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001025 pm_genpd_sync_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001026 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001027
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001028 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001029}
1030
1031/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001032 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1033 * @dev: Device to resume.
1034 *
1035 * Carry out an early resume of a device under the assumption that its
1036 * pm_domain field points to the domain member of an object of type
1037 * struct generic_pm_domain representing a power domain consisting of I/O
1038 * devices.
1039 */
1040static int pm_genpd_resume_early(struct device *dev)
1041{
1042 struct generic_pm_domain *genpd;
1043
1044 dev_dbg(dev, "%s()\n", __func__);
1045
1046 genpd = dev_to_genpd(dev);
1047 if (IS_ERR(genpd))
1048 return -EINVAL;
1049
1050 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1051}
1052
1053/**
1054 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001055 * @dev: Device to resume.
1056 *
1057 * Resume a device under the assumption that its pm_domain field points to the
1058 * domain member of an object of type struct generic_pm_domain representing
1059 * a power domain consisting of I/O devices.
1060 */
1061static int pm_genpd_resume(struct device *dev)
1062{
1063 struct generic_pm_domain *genpd;
1064
1065 dev_dbg(dev, "%s()\n", __func__);
1066
1067 genpd = dev_to_genpd(dev);
1068 if (IS_ERR(genpd))
1069 return -EINVAL;
1070
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001071 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001072}
1073
1074/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001075 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001076 * @dev: Device to freeze.
1077 *
1078 * Freeze a device under the assumption that its pm_domain field points to the
1079 * domain member of an object of type struct generic_pm_domain representing
1080 * a power domain consisting of I/O devices.
1081 */
1082static int pm_genpd_freeze(struct device *dev)
1083{
1084 struct generic_pm_domain *genpd;
1085
1086 dev_dbg(dev, "%s()\n", __func__);
1087
1088 genpd = dev_to_genpd(dev);
1089 if (IS_ERR(genpd))
1090 return -EINVAL;
1091
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001092 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001093}
1094
1095/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001096 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1097 * @dev: Device to freeze.
1098 *
1099 * Carry out a late freeze of a device under the assumption that its
1100 * pm_domain field points to the domain member of an object of type
1101 * struct generic_pm_domain representing a power domain consisting of I/O
1102 * devices.
1103 */
1104static int pm_genpd_freeze_late(struct device *dev)
1105{
1106 struct generic_pm_domain *genpd;
1107
1108 dev_dbg(dev, "%s()\n", __func__);
1109
1110 genpd = dev_to_genpd(dev);
1111 if (IS_ERR(genpd))
1112 return -EINVAL;
1113
1114 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1115}
1116
1117/**
1118 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001119 * @dev: Device to freeze.
1120 *
1121 * Carry out a late freeze of a device under the assumption that its
1122 * pm_domain field points to the domain member of an object of type
1123 * struct generic_pm_domain representing a power domain consisting of I/O
1124 * devices.
1125 */
1126static int pm_genpd_freeze_noirq(struct device *dev)
1127{
1128 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001129
1130 dev_dbg(dev, "%s()\n", __func__);
1131
1132 genpd = dev_to_genpd(dev);
1133 if (IS_ERR(genpd))
1134 return -EINVAL;
1135
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001136 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001137}
1138
1139/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001140 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001141 * @dev: Device to thaw.
1142 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001143 * Start the device, unless power has been removed from the domain already
1144 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001145 */
1146static int pm_genpd_thaw_noirq(struct device *dev)
1147{
1148 struct generic_pm_domain *genpd;
1149
1150 dev_dbg(dev, "%s()\n", __func__);
1151
1152 genpd = dev_to_genpd(dev);
1153 if (IS_ERR(genpd))
1154 return -EINVAL;
1155
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001156 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001157}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001158
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001159/**
1160 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1161 * @dev: Device to thaw.
1162 *
1163 * Carry out an early thaw of a device under the assumption that its
1164 * pm_domain field points to the domain member of an object of type
1165 * struct generic_pm_domain representing a power domain consisting of I/O
1166 * devices.
1167 */
1168static int pm_genpd_thaw_early(struct device *dev)
1169{
1170 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001171
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001172 dev_dbg(dev, "%s()\n", __func__);
1173
1174 genpd = dev_to_genpd(dev);
1175 if (IS_ERR(genpd))
1176 return -EINVAL;
1177
1178 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001179}
1180
1181/**
1182 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1183 * @dev: Device to thaw.
1184 *
1185 * Thaw a device under the assumption that its pm_domain field points to the
1186 * domain member of an object of type struct generic_pm_domain representing
1187 * a power domain consisting of I/O devices.
1188 */
1189static int pm_genpd_thaw(struct device *dev)
1190{
1191 struct generic_pm_domain *genpd;
1192
1193 dev_dbg(dev, "%s()\n", __func__);
1194
1195 genpd = dev_to_genpd(dev);
1196 if (IS_ERR(genpd))
1197 return -EINVAL;
1198
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001199 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001200}
1201
1202/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001203 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001204 * @dev: Device to resume.
1205 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001206 * Make sure the domain will be in the same power state as before the
1207 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001208 */
1209static int pm_genpd_restore_noirq(struct device *dev)
1210{
1211 struct generic_pm_domain *genpd;
1212
1213 dev_dbg(dev, "%s()\n", __func__);
1214
1215 genpd = dev_to_genpd(dev);
1216 if (IS_ERR(genpd))
1217 return -EINVAL;
1218
1219 /*
1220 * Since all of the "noirq" callbacks are executed sequentially, it is
1221 * guaranteed that this function will never run twice in parallel for
1222 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001223 *
1224 * At this point suspended_count == 0 means we are being run for the
1225 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001226 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001227 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001228 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001229 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001230 * so make it appear as powered off to pm_genpd_sync_poweron(),
1231 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001232 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001233 genpd->status = GPD_STATE_POWER_OFF;
1234 if (genpd->suspend_power_off) {
1235 /*
1236 * If the domain was off before the hibernation, make
1237 * sure it will be off going forward.
1238 */
1239 if (genpd->power_off)
1240 genpd->power_off(genpd);
1241
1242 return 0;
1243 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001244 }
1245
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001246 if (genpd->suspend_power_off)
1247 return 0;
1248
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001249 pm_genpd_sync_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001250
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001251 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001252}
1253
1254/**
1255 * pm_genpd_complete - Complete power transition of a device in a power domain.
1256 * @dev: Device to complete the transition of.
1257 *
1258 * Complete a power transition of a device (during a system-wide power
1259 * transition) under the assumption that its pm_domain field points to the
1260 * domain member of an object of type struct generic_pm_domain representing
1261 * a power domain consisting of I/O devices.
1262 */
1263static void pm_genpd_complete(struct device *dev)
1264{
1265 struct generic_pm_domain *genpd;
1266 bool run_complete;
1267
1268 dev_dbg(dev, "%s()\n", __func__);
1269
1270 genpd = dev_to_genpd(dev);
1271 if (IS_ERR(genpd))
1272 return;
1273
1274 mutex_lock(&genpd->lock);
1275
1276 run_complete = !genpd->suspend_power_off;
1277 if (--genpd->prepared_count == 0)
1278 genpd->suspend_power_off = false;
1279
1280 mutex_unlock(&genpd->lock);
1281
1282 if (run_complete) {
1283 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001284 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001285 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001286 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001287 }
1288}
1289
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001290/**
1291 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1292 * @dev: Device that normally is marked as "always on" to switch power for.
1293 *
1294 * This routine may only be called during the system core (syscore) suspend or
1295 * resume phase for devices whose "always on" flags are set.
1296 */
1297void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1298{
1299 struct generic_pm_domain *genpd;
1300
1301 genpd = dev_to_genpd(dev);
1302 if (!pm_genpd_present(genpd))
1303 return;
1304
1305 if (suspend) {
1306 genpd->suspended_count++;
1307 pm_genpd_sync_poweroff(genpd);
1308 } else {
1309 pm_genpd_sync_poweron(genpd);
1310 genpd->suspended_count--;
1311 }
1312}
1313EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1314
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001315#else
1316
1317#define pm_genpd_prepare NULL
1318#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001319#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001320#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001321#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001322#define pm_genpd_resume_noirq NULL
1323#define pm_genpd_resume NULL
1324#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001325#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001326#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001327#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001328#define pm_genpd_thaw_noirq NULL
1329#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001330#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001331#define pm_genpd_complete NULL
1332
1333#endif /* CONFIG_PM_SLEEP */
1334
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001335static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1336{
1337 struct generic_pm_domain_data *gpd_data;
1338
1339 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1340 if (!gpd_data)
1341 return NULL;
1342
1343 mutex_init(&gpd_data->lock);
1344 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1345 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1346 return gpd_data;
1347}
1348
1349static void __pm_genpd_free_dev_data(struct device *dev,
1350 struct generic_pm_domain_data *gpd_data)
1351{
1352 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1353 kfree(gpd_data);
1354}
1355
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001356/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001357 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001358 * @genpd: PM domain to add the device to.
1359 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001360 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001361 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001362int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1363 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001364{
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001365 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001366 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001367 int ret = 0;
1368
1369 dev_dbg(dev, "%s()\n", __func__);
1370
1371 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1372 return -EINVAL;
1373
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001374 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1375 if (!gpd_data_new)
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001376 return -ENOMEM;
1377
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001378 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001379
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001380 if (genpd->prepared_count > 0) {
1381 ret = -EAGAIN;
1382 goto out;
1383 }
1384
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001385 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1386 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001387 ret = -EINVAL;
1388 goto out;
1389 }
1390
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001391 ret = dev_pm_get_subsys_data(dev);
1392 if (ret)
1393 goto out;
1394
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001395 genpd->device_count++;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001396 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001397
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001398 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001399
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001400 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001401 if (dev->power.subsys_data->domain_data) {
1402 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1403 } else {
1404 gpd_data = gpd_data_new;
1405 dev->power.subsys_data->domain_data = &gpd_data->base;
1406 }
1407 gpd_data->refcount++;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001408 if (td)
1409 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001410
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001411 spin_unlock_irq(&dev->power.lock);
1412
1413 mutex_lock(&gpd_data->lock);
1414 gpd_data->base.dev = dev;
1415 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1416 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001417 gpd_data->td.constraint_changed = true;
1418 gpd_data->td.effective_constraint_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001419 mutex_unlock(&gpd_data->lock);
1420
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001421 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001422 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001423
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001424 if (gpd_data != gpd_data_new)
1425 __pm_genpd_free_dev_data(dev, gpd_data_new);
1426
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001427 return ret;
1428}
1429
1430/**
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001431 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1432 * @genpd_node: Device tree node pointer representing a PM domain to which the
1433 * the device is added to.
1434 * @dev: Device to be added.
1435 * @td: Set of PM QoS timing parameters to attach to the device.
1436 */
1437int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1438 struct gpd_timing_data *td)
1439{
1440 struct generic_pm_domain *genpd = NULL, *gpd;
1441
1442 dev_dbg(dev, "%s()\n", __func__);
1443
1444 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1445 return -EINVAL;
1446
1447 mutex_lock(&gpd_list_lock);
1448 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1449 if (gpd->of_node == genpd_node) {
1450 genpd = gpd;
1451 break;
1452 }
1453 }
1454 mutex_unlock(&gpd_list_lock);
1455
1456 if (!genpd)
1457 return -EINVAL;
1458
1459 return __pm_genpd_add_device(genpd, dev, td);
1460}
1461
1462/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001463 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1464 * @genpd: PM domain to remove the device from.
1465 * @dev: Device to be removed.
1466 */
1467int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1468 struct device *dev)
1469{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001470 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001471 struct pm_domain_data *pdd;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001472 bool remove = false;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001473 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001474
1475 dev_dbg(dev, "%s()\n", __func__);
1476
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001477 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1478 || IS_ERR_OR_NULL(dev->pm_domain)
1479 || pd_to_genpd(dev->pm_domain) != genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001480 return -EINVAL;
1481
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001482 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001483
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001484 if (genpd->prepared_count > 0) {
1485 ret = -EAGAIN;
1486 goto out;
1487 }
1488
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001489 genpd->device_count--;
1490 genpd->max_off_time_changed = true;
1491
1492 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001493
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001494 dev->pm_domain = NULL;
1495 pdd = dev->power.subsys_data->domain_data;
1496 list_del_init(&pdd->list_node);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001497 gpd_data = to_gpd_data(pdd);
1498 if (--gpd_data->refcount == 0) {
1499 dev->power.subsys_data->domain_data = NULL;
1500 remove = true;
1501 }
1502
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001503 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001504
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001505 mutex_lock(&gpd_data->lock);
1506 pdd->dev = NULL;
1507 mutex_unlock(&gpd_data->lock);
1508
1509 genpd_release_lock(genpd);
1510
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001511 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001512 if (remove)
1513 __pm_genpd_free_dev_data(dev, gpd_data);
1514
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001515 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001516
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001517 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001518 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001519
1520 return ret;
1521}
1522
1523/**
Rafael J. Wysockica1d72f2012-05-14 21:45:52 +02001524 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1525 * @dev: Device to set/unset the flag for.
1526 * @val: The new value of the device's "need restore" flag.
1527 */
1528void pm_genpd_dev_need_restore(struct device *dev, bool val)
1529{
1530 struct pm_subsys_data *psd;
1531 unsigned long flags;
1532
1533 spin_lock_irqsave(&dev->power.lock, flags);
1534
1535 psd = dev_to_psd(dev);
1536 if (psd && psd->domain_data)
1537 to_gpd_data(psd->domain_data)->need_restore = val;
1538
1539 spin_unlock_irqrestore(&dev->power.lock, flags);
1540}
1541EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1542
1543/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001544 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1545 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001546 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001547 */
1548int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001549 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001550{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001551 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001552 int ret = 0;
1553
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001554 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001555 return -EINVAL;
1556
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001557 start:
1558 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001559 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001560
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001561 if (subdomain->status != GPD_STATE_POWER_OFF
1562 && subdomain->status != GPD_STATE_ACTIVE) {
1563 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001564 genpd_release_lock(genpd);
1565 goto start;
1566 }
1567
1568 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001569 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001570 ret = -EINVAL;
1571 goto out;
1572 }
1573
Huang Ying4fcac102012-05-07 21:35:45 +02001574 list_for_each_entry(link, &genpd->master_links, master_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001575 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001576 ret = -EINVAL;
1577 goto out;
1578 }
1579 }
1580
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001581 link = kzalloc(sizeof(*link), GFP_KERNEL);
1582 if (!link) {
1583 ret = -ENOMEM;
1584 goto out;
1585 }
1586 link->master = genpd;
1587 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001588 link->slave = subdomain;
1589 list_add_tail(&link->slave_node, &subdomain->slave_links);
1590 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001591 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001592
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001593 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001594 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001595 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001596
1597 return ret;
1598}
1599
1600/**
1601 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1602 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001603 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001604 */
1605int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001606 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001607{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001608 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001609 int ret = -EINVAL;
1610
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001611 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001612 return -EINVAL;
1613
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001614 start:
1615 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001616
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001617 list_for_each_entry(link, &genpd->master_links, master_node) {
1618 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001619 continue;
1620
1621 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1622
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001623 if (subdomain->status != GPD_STATE_POWER_OFF
1624 && subdomain->status != GPD_STATE_ACTIVE) {
1625 mutex_unlock(&subdomain->lock);
1626 genpd_release_lock(genpd);
1627 goto start;
1628 }
1629
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001630 list_del(&link->master_node);
1631 list_del(&link->slave_node);
1632 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001633 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001634 genpd_sd_counter_dec(genpd);
1635
1636 mutex_unlock(&subdomain->lock);
1637
1638 ret = 0;
1639 break;
1640 }
1641
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001642 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001643
1644 return ret;
1645}
1646
1647/**
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001648 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1649 * @dev: Device to add the callbacks to.
1650 * @ops: Set of callbacks to add.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001651 * @td: Timing data to add to the device along with the callbacks (optional).
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001652 *
1653 * Every call to this routine should be balanced with a call to
1654 * __pm_genpd_remove_callbacks() and they must not be nested.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001655 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001656int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1657 struct gpd_timing_data *td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001658{
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001659 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001660 int ret = 0;
1661
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001662 if (!(dev && ops))
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001663 return -EINVAL;
1664
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001665 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1666 if (!gpd_data_new)
1667 return -ENOMEM;
1668
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001669 pm_runtime_disable(dev);
1670 device_pm_lock();
1671
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001672 ret = dev_pm_get_subsys_data(dev);
1673 if (ret)
1674 goto out;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001675
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001676 spin_lock_irq(&dev->power.lock);
1677
1678 if (dev->power.subsys_data->domain_data) {
1679 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001680 } else {
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001681 gpd_data = gpd_data_new;
1682 dev->power.subsys_data->domain_data = &gpd_data->base;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001683 }
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001684 gpd_data->refcount++;
1685 gpd_data->ops = *ops;
1686 if (td)
1687 gpd_data->td = *td;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001688
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001689 spin_unlock_irq(&dev->power.lock);
1690
1691 out:
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001692 device_pm_unlock();
1693 pm_runtime_enable(dev);
1694
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001695 if (gpd_data != gpd_data_new)
1696 __pm_genpd_free_dev_data(dev, gpd_data_new);
1697
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001698 return ret;
1699}
1700EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1701
1702/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001703 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001704 * @dev: Device to remove the callbacks from.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001705 * @clear_td: If set, clear the device's timing data too.
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001706 *
1707 * This routine can only be called after pm_genpd_add_callbacks().
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001708 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001709int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001710{
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001711 struct generic_pm_domain_data *gpd_data = NULL;
1712 bool remove = false;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001713 int ret = 0;
1714
1715 if (!(dev && dev->power.subsys_data))
1716 return -EINVAL;
1717
1718 pm_runtime_disable(dev);
1719 device_pm_lock();
1720
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001721 spin_lock_irq(&dev->power.lock);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001722
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001723 if (dev->power.subsys_data->domain_data) {
1724 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
Sachin Kamatdb79e532012-07-11 12:25:49 +02001725 gpd_data->ops = (struct gpd_dev_ops){ NULL };
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001726 if (clear_td)
1727 gpd_data->td = (struct gpd_timing_data){ 0 };
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001728
1729 if (--gpd_data->refcount == 0) {
1730 dev->power.subsys_data->domain_data = NULL;
1731 remove = true;
1732 }
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001733 } else {
1734 ret = -EINVAL;
1735 }
1736
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001737 spin_unlock_irq(&dev->power.lock);
1738
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001739 device_pm_unlock();
1740 pm_runtime_enable(dev);
1741
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001742 if (ret)
1743 return ret;
1744
1745 dev_pm_put_subsys_data(dev);
1746 if (remove)
1747 __pm_genpd_free_dev_data(dev, gpd_data);
1748
1749 return 0;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001750}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001751EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001752
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001753int genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1754{
1755 struct cpuidle_driver *cpuidle_drv;
1756 struct gpd_cpu_data *cpu_data;
1757 struct cpuidle_state *idle_state;
1758 int ret = 0;
1759
1760 if (IS_ERR_OR_NULL(genpd) || state < 0)
1761 return -EINVAL;
1762
1763 genpd_acquire_lock(genpd);
1764
1765 if (genpd->cpu_data) {
1766 ret = -EEXIST;
1767 goto out;
1768 }
1769 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1770 if (!cpu_data) {
1771 ret = -ENOMEM;
1772 goto out;
1773 }
1774 cpuidle_drv = cpuidle_driver_ref();
1775 if (!cpuidle_drv) {
1776 ret = -ENODEV;
1777 goto out;
1778 }
1779 if (cpuidle_drv->state_count <= state) {
1780 ret = -EINVAL;
1781 goto err;
1782 }
1783 idle_state = &cpuidle_drv->states[state];
1784 if (!idle_state->disabled) {
1785 ret = -EAGAIN;
1786 goto err;
1787 }
1788 cpu_data->idle_state = idle_state;
1789 cpu_data->saved_exit_latency = idle_state->exit_latency;
1790 genpd->cpu_data = cpu_data;
1791 genpd_recalc_cpu_exit_latency(genpd);
1792
1793 out:
1794 genpd_release_lock(genpd);
1795 return ret;
1796
1797 err:
1798 cpuidle_driver_unref();
1799 goto out;
1800}
1801
1802int genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1803{
1804 struct gpd_cpu_data *cpu_data;
1805 struct cpuidle_state *idle_state;
1806 int ret = 0;
1807
1808 if (IS_ERR_OR_NULL(genpd))
1809 return -EINVAL;
1810
1811 genpd_acquire_lock(genpd);
1812
1813 cpu_data = genpd->cpu_data;
1814 if (!cpu_data) {
1815 ret = -ENODEV;
1816 goto out;
1817 }
1818 idle_state = cpu_data->idle_state;
1819 if (!idle_state->disabled) {
1820 ret = -EAGAIN;
1821 goto out;
1822 }
1823 idle_state->exit_latency = cpu_data->saved_exit_latency;
1824 cpuidle_driver_unref();
1825 genpd->cpu_data = NULL;
1826 kfree(cpu_data);
1827
1828 out:
1829 genpd_release_lock(genpd);
1830 return ret;
1831}
1832
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001833/* Default device callbacks for generic PM domains. */
1834
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001835/**
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001836 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1837 * @dev: Device to handle.
1838 */
1839static int pm_genpd_default_save_state(struct device *dev)
1840{
1841 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001842
1843 cb = dev_gpd_data(dev)->ops.save_state;
1844 if (cb)
1845 return cb(dev);
1846
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001847 if (dev->type && dev->type->pm)
1848 cb = dev->type->pm->runtime_suspend;
1849 else if (dev->class && dev->class->pm)
1850 cb = dev->class->pm->runtime_suspend;
1851 else if (dev->bus && dev->bus->pm)
1852 cb = dev->bus->pm->runtime_suspend;
1853 else
1854 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001855
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001856 if (!cb && dev->driver && dev->driver->pm)
1857 cb = dev->driver->pm->runtime_suspend;
1858
1859 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001860}
1861
1862/**
1863 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1864 * @dev: Device to handle.
1865 */
1866static int pm_genpd_default_restore_state(struct device *dev)
1867{
1868 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001869
1870 cb = dev_gpd_data(dev)->ops.restore_state;
1871 if (cb)
1872 return cb(dev);
1873
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001874 if (dev->type && dev->type->pm)
1875 cb = dev->type->pm->runtime_resume;
1876 else if (dev->class && dev->class->pm)
1877 cb = dev->class->pm->runtime_resume;
1878 else if (dev->bus && dev->bus->pm)
1879 cb = dev->bus->pm->runtime_resume;
1880 else
1881 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001882
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001883 if (!cb && dev->driver && dev->driver->pm)
1884 cb = dev->driver->pm->runtime_resume;
1885
1886 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001887}
1888
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001889#ifdef CONFIG_PM_SLEEP
1890
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001891/**
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001892 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1893 * @dev: Device to handle.
1894 */
1895static int pm_genpd_default_suspend(struct device *dev)
1896{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001897 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001898
1899 return cb ? cb(dev) : pm_generic_suspend(dev);
1900}
1901
1902/**
1903 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1904 * @dev: Device to handle.
1905 */
1906static int pm_genpd_default_suspend_late(struct device *dev)
1907{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001908 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001909
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001910 return cb ? cb(dev) : pm_generic_suspend_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001911}
1912
1913/**
1914 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1915 * @dev: Device to handle.
1916 */
1917static int pm_genpd_default_resume_early(struct device *dev)
1918{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001919 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001920
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001921 return cb ? cb(dev) : pm_generic_resume_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001922}
1923
1924/**
1925 * pm_genpd_default_resume - Default "device resume" for PM domians.
1926 * @dev: Device to handle.
1927 */
1928static int pm_genpd_default_resume(struct device *dev)
1929{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001930 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001931
1932 return cb ? cb(dev) : pm_generic_resume(dev);
1933}
1934
1935/**
1936 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1937 * @dev: Device to handle.
1938 */
1939static int pm_genpd_default_freeze(struct device *dev)
1940{
1941 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1942
1943 return cb ? cb(dev) : pm_generic_freeze(dev);
1944}
1945
1946/**
1947 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1948 * @dev: Device to handle.
1949 */
1950static int pm_genpd_default_freeze_late(struct device *dev)
1951{
1952 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1953
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001954 return cb ? cb(dev) : pm_generic_freeze_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001955}
1956
1957/**
1958 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1959 * @dev: Device to handle.
1960 */
1961static int pm_genpd_default_thaw_early(struct device *dev)
1962{
1963 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1964
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001965 return cb ? cb(dev) : pm_generic_thaw_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001966}
1967
1968/**
1969 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1970 * @dev: Device to handle.
1971 */
1972static int pm_genpd_default_thaw(struct device *dev)
1973{
1974 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1975
1976 return cb ? cb(dev) : pm_generic_thaw(dev);
1977}
1978
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001979#else /* !CONFIG_PM_SLEEP */
1980
1981#define pm_genpd_default_suspend NULL
1982#define pm_genpd_default_suspend_late NULL
1983#define pm_genpd_default_resume_early NULL
1984#define pm_genpd_default_resume NULL
1985#define pm_genpd_default_freeze NULL
1986#define pm_genpd_default_freeze_late NULL
1987#define pm_genpd_default_thaw_early NULL
1988#define pm_genpd_default_thaw NULL
1989
1990#endif /* !CONFIG_PM_SLEEP */
1991
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001992/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001993 * pm_genpd_init - Initialize a generic I/O PM domain object.
1994 * @genpd: PM domain object to initialize.
1995 * @gov: PM domain governor to associate with the domain (may be NULL).
1996 * @is_off: Initial value of the domain's power_is_off field.
1997 */
1998void pm_genpd_init(struct generic_pm_domain *genpd,
1999 struct dev_power_governor *gov, bool is_off)
2000{
2001 if (IS_ERR_OR_NULL(genpd))
2002 return;
2003
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02002004 INIT_LIST_HEAD(&genpd->master_links);
2005 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002006 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002007 mutex_init(&genpd->lock);
2008 genpd->gov = gov;
2009 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2010 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02002011 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02002012 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2013 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02002014 genpd->poweroff_task = NULL;
2015 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002016 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01002017 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02002018 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002019 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2020 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2021 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002022 genpd->domain.ops.prepare = pm_genpd_prepare;
2023 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002024 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002025 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2026 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002027 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002028 genpd->domain.ops.resume = pm_genpd_resume;
2029 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002030 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002031 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2032 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002033 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002034 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002035 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002036 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002037 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002038 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002039 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002040 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002041 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01002042 genpd->dev_ops.save_state = pm_genpd_default_save_state;
2043 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Rafael J. Wysockic9914852011-12-06 23:16:47 +01002044 genpd->dev_ops.suspend = pm_genpd_default_suspend;
2045 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2046 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2047 genpd->dev_ops.resume = pm_genpd_default_resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002048 genpd->dev_ops.freeze = pm_genpd_default_freeze;
2049 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2050 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2051 genpd->dev_ops.thaw = pm_genpd_default_thaw;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02002052 mutex_lock(&gpd_list_lock);
2053 list_add(&genpd->gpd_list_node, &gpd_list);
2054 mutex_unlock(&gpd_list_lock);
2055}