blob: ba3487c9835b67fa64daad5f6e23016f23a7f569 [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. Wysocki1e78a0c2012-03-13 22:39:48 +0100439 || pdd->dev->power.irq_safe || to_gpd_data(pdd)->always_on))
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. Wysocki1e78a0c2012-03-13 22:39:48 +0100581 if (dev_gpd_data(dev)->always_on)
582 return -EBUSY;
583
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100584 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
585 if (stop_ok && !stop_ok(dev))
586 return -EBUSY;
587
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100588 ret = genpd_stop_dev(genpd, dev);
589 if (ret)
590 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200591
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200592 /*
593 * If power.irq_safe is set, this routine will be run with interrupts
594 * off, so it can't use mutexes.
595 */
596 if (dev->power.irq_safe)
597 return 0;
598
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200599 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200600 genpd->in_progress++;
601 pm_genpd_poweroff(genpd);
602 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200603 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200604
605 return 0;
606}
607
608/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200609 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
610 * @dev: Device to resume.
611 *
612 * Carry out a runtime resume of a device under the assumption that its
613 * pm_domain field points to the domain member of an object of type
614 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
615 */
616static int pm_genpd_runtime_resume(struct device *dev)
617{
618 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200619 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200620 int ret;
621
622 dev_dbg(dev, "%s()\n", __func__);
623
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200624 genpd = dev_to_genpd(dev);
625 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200626 return -EINVAL;
627
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200628 might_sleep_if(!genpd->dev_irq_safe);
629
630 /* If power.irq_safe, the PM domain is never powered off. */
631 if (dev->power.irq_safe)
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200632 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200633
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200634 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200635 ret = __pm_genpd_poweron(genpd);
636 if (ret) {
637 mutex_unlock(&genpd->lock);
638 return ret;
639 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200640 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200641 genpd->resume_count++;
642 for (;;) {
643 prepare_to_wait(&genpd->status_wait_queue, &wait,
644 TASK_UNINTERRUPTIBLE);
645 /*
646 * If current is the powering off task, we have been called
647 * reentrantly from one of the device callbacks, so we should
648 * not wait.
649 */
650 if (!genpd->poweroff_task || genpd->poweroff_task == current)
651 break;
652 mutex_unlock(&genpd->lock);
653
654 schedule();
655
656 mutex_lock(&genpd->lock);
657 }
658 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200659 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200660 genpd->resume_count--;
661 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200662 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200663 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200664
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200665 return 0;
666}
667
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200668/**
669 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
670 */
671void pm_genpd_poweroff_unused(void)
672{
673 struct generic_pm_domain *genpd;
674
675 mutex_lock(&gpd_list_lock);
676
677 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
678 genpd_queue_power_off_work(genpd);
679
680 mutex_unlock(&gpd_list_lock);
681}
682
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200683#else
684
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200685static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
686 unsigned long val, void *ptr)
687{
688 return NOTIFY_DONE;
689}
690
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200691static inline void genpd_power_off_work_fn(struct work_struct *work) {}
692
693#define pm_genpd_runtime_suspend NULL
694#define pm_genpd_runtime_resume NULL
695
696#endif /* CONFIG_PM_RUNTIME */
697
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200698#ifdef CONFIG_PM_SLEEP
699
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100700static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
701 struct device *dev)
702{
703 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
704}
705
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100706static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
707{
708 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
709}
710
711static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
712{
713 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
714}
715
716static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
717{
718 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
719}
720
721static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
722{
723 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
724}
725
726static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
727{
728 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
729}
730
731static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
732{
733 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
734}
735
736static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
737{
738 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
739}
740
741static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
742{
743 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
744}
745
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200746/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200747 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200748 * @genpd: PM domain to power off, if possible.
749 *
750 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200751 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200752 *
753 * This function is only called in "noirq" stages of system power transitions,
754 * so it need not acquire locks (all of the "noirq" callbacks are executed
755 * sequentially, so it is guaranteed that it will never run twice in parallel).
756 */
757static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
758{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200759 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200760
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200761 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200762 return;
763
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200764 if (genpd->suspended_count != genpd->device_count
765 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200766 return;
767
768 if (genpd->power_off)
769 genpd->power_off(genpd);
770
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200771 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200772
773 list_for_each_entry(link, &genpd->slave_links, slave_node) {
774 genpd_sd_counter_dec(link->master);
775 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200776 }
777}
778
779/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200780 * resume_needed - Check whether to resume a device before system suspend.
781 * @dev: Device to check.
782 * @genpd: PM domain the device belongs to.
783 *
784 * There are two cases in which a device that can wake up the system from sleep
785 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
786 * to wake up the system and it has to remain active for this purpose while the
787 * system is in the sleep state and (2) if the device is not enabled to wake up
788 * the system from sleep states and it generally doesn't generate wakeup signals
789 * by itself (those signals are generated on its behalf by other parts of the
790 * system). In the latter case it may be necessary to reconfigure the device's
791 * wakeup settings during system suspend, because it may have been set up to
792 * signal remote wakeup from the system's working state as needed by runtime PM.
793 * Return 'true' in either of the above cases.
794 */
795static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
796{
797 bool active_wakeup;
798
799 if (!device_can_wakeup(dev))
800 return false;
801
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100802 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200803 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
804}
805
806/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200807 * pm_genpd_prepare - Start power transition of a device in a PM domain.
808 * @dev: Device to start the transition of.
809 *
810 * Start a power transition of a device (during a system-wide power transition)
811 * under the assumption that its pm_domain field points to the domain member of
812 * an object of type struct generic_pm_domain representing a PM domain
813 * consisting of I/O devices.
814 */
815static int pm_genpd_prepare(struct device *dev)
816{
817 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200818 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200819
820 dev_dbg(dev, "%s()\n", __func__);
821
822 genpd = dev_to_genpd(dev);
823 if (IS_ERR(genpd))
824 return -EINVAL;
825
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200826 /*
827 * If a wakeup request is pending for the device, it should be woken up
828 * at this point and a system wakeup event should be reported if it's
829 * set up to wake up the system from sleep states.
830 */
831 pm_runtime_get_noresume(dev);
832 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
833 pm_wakeup_event(dev, 0);
834
835 if (pm_wakeup_pending()) {
836 pm_runtime_put_sync(dev);
837 return -EBUSY;
838 }
839
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200840 if (resume_needed(dev, genpd))
841 pm_runtime_resume(dev);
842
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200843 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200844
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100845 if (genpd->prepared_count++ == 0) {
846 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200847 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100848 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200849
850 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200851
852 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200853 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200854 return 0;
855 }
856
857 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200858 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
859 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100860 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200861 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200862 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200863 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200864 __pm_runtime_disable(dev, false);
865
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200866 ret = pm_generic_prepare(dev);
867 if (ret) {
868 mutex_lock(&genpd->lock);
869
870 if (--genpd->prepared_count == 0)
871 genpd->suspend_power_off = false;
872
873 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200874 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200875 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200876
877 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200878 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200879}
880
881/**
882 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
883 * @dev: Device to suspend.
884 *
885 * Suspend a device under the assumption that its pm_domain field points to the
886 * domain member of an object of type struct generic_pm_domain representing
887 * a PM domain consisting of I/O devices.
888 */
889static int pm_genpd_suspend(struct device *dev)
890{
891 struct generic_pm_domain *genpd;
892
893 dev_dbg(dev, "%s()\n", __func__);
894
895 genpd = dev_to_genpd(dev);
896 if (IS_ERR(genpd))
897 return -EINVAL;
898
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100899 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200900}
901
902/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100903 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200904 * @dev: Device to suspend.
905 *
906 * Carry out a late suspend of a device under the assumption that its
907 * pm_domain field points to the domain member of an object of type
908 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
909 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100910static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200911{
912 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200913
914 dev_dbg(dev, "%s()\n", __func__);
915
916 genpd = dev_to_genpd(dev);
917 if (IS_ERR(genpd))
918 return -EINVAL;
919
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100920 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
921}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200922
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100923/**
924 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
925 * @dev: Device to suspend.
926 *
927 * Stop the device and remove power from the domain if all devices in it have
928 * been stopped.
929 */
930static int pm_genpd_suspend_noirq(struct device *dev)
931{
932 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200933
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100934 dev_dbg(dev, "%s()\n", __func__);
935
936 genpd = dev_to_genpd(dev);
937 if (IS_ERR(genpd))
938 return -EINVAL;
939
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100940 if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100941 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200942 return 0;
943
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100944 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200945
946 /*
947 * Since all of the "noirq" callbacks are executed sequentially, it is
948 * guaranteed that this function will never run twice in parallel for
949 * the same PM domain, so it is not necessary to use locking here.
950 */
951 genpd->suspended_count++;
952 pm_genpd_sync_poweroff(genpd);
953
954 return 0;
955}
956
957/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100958 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200959 * @dev: Device to resume.
960 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100961 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200962 */
963static int pm_genpd_resume_noirq(struct device *dev)
964{
965 struct generic_pm_domain *genpd;
966
967 dev_dbg(dev, "%s()\n", __func__);
968
969 genpd = dev_to_genpd(dev);
970 if (IS_ERR(genpd))
971 return -EINVAL;
972
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100973 if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
Rafael J. Wysockicc85b202012-03-13 22:39:31 +0100974 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200975 return 0;
976
977 /*
978 * Since all of the "noirq" callbacks are executed sequentially, it is
979 * guaranteed that this function will never run twice in parallel for
980 * the same PM domain, so it is not necessary to use locking here.
981 */
982 pm_genpd_poweron(genpd);
983 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200984
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100985 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200986}
987
988/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100989 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
990 * @dev: Device to resume.
991 *
992 * Carry out an early resume of a device under the assumption that its
993 * pm_domain field points to the domain member of an object of type
994 * struct generic_pm_domain representing a power domain consisting of I/O
995 * devices.
996 */
997static int pm_genpd_resume_early(struct device *dev)
998{
999 struct generic_pm_domain *genpd;
1000
1001 dev_dbg(dev, "%s()\n", __func__);
1002
1003 genpd = dev_to_genpd(dev);
1004 if (IS_ERR(genpd))
1005 return -EINVAL;
1006
1007 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1008}
1009
1010/**
1011 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001012 * @dev: Device to resume.
1013 *
1014 * Resume a device under the assumption that its pm_domain field points to the
1015 * domain member of an object of type struct generic_pm_domain representing
1016 * a power domain consisting of I/O devices.
1017 */
1018static int pm_genpd_resume(struct device *dev)
1019{
1020 struct generic_pm_domain *genpd;
1021
1022 dev_dbg(dev, "%s()\n", __func__);
1023
1024 genpd = dev_to_genpd(dev);
1025 if (IS_ERR(genpd))
1026 return -EINVAL;
1027
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001028 return genpd->suspend_power_off ? 0 : genpd_resume_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_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001033 * @dev: Device to freeze.
1034 *
1035 * Freeze a device under the assumption that its pm_domain field points to the
1036 * domain member of an object of type struct generic_pm_domain representing
1037 * a power domain consisting of I/O devices.
1038 */
1039static int pm_genpd_freeze(struct device *dev)
1040{
1041 struct generic_pm_domain *genpd;
1042
1043 dev_dbg(dev, "%s()\n", __func__);
1044
1045 genpd = dev_to_genpd(dev);
1046 if (IS_ERR(genpd))
1047 return -EINVAL;
1048
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001049 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001050}
1051
1052/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001053 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1054 * @dev: Device to freeze.
1055 *
1056 * Carry out a late freeze of a device under the assumption that its
1057 * pm_domain field points to the domain member of an object of type
1058 * struct generic_pm_domain representing a power domain consisting of I/O
1059 * devices.
1060 */
1061static int pm_genpd_freeze_late(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
1071 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1072}
1073
1074/**
1075 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001076 * @dev: Device to freeze.
1077 *
1078 * Carry out a late freeze of a device under the assumption that its
1079 * pm_domain field points to the domain member of an object of type
1080 * struct generic_pm_domain representing a power domain consisting of I/O
1081 * devices.
1082 */
1083static int pm_genpd_freeze_noirq(struct device *dev)
1084{
1085 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001086
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. Wysocki1e78a0c2012-03-13 22:39:48 +01001093 return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1094 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001095}
1096
1097/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001098 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001099 * @dev: Device to thaw.
1100 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001101 * Start the device, unless power has been removed from the domain already
1102 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001103 */
1104static int pm_genpd_thaw_noirq(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
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001114 return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1115 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001116}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001117
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001118/**
1119 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1120 * @dev: Device to thaw.
1121 *
1122 * Carry out an early thaw of a device under the assumption that its
1123 * pm_domain field points to the domain member of an object of type
1124 * struct generic_pm_domain representing a power domain consisting of I/O
1125 * devices.
1126 */
1127static int pm_genpd_thaw_early(struct device *dev)
1128{
1129 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001130
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001131 dev_dbg(dev, "%s()\n", __func__);
1132
1133 genpd = dev_to_genpd(dev);
1134 if (IS_ERR(genpd))
1135 return -EINVAL;
1136
1137 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001138}
1139
1140/**
1141 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1142 * @dev: Device to thaw.
1143 *
1144 * Thaw a device under the assumption that its pm_domain field points to the
1145 * domain member of an object of type struct generic_pm_domain representing
1146 * a power domain consisting of I/O devices.
1147 */
1148static int pm_genpd_thaw(struct device *dev)
1149{
1150 struct generic_pm_domain *genpd;
1151
1152 dev_dbg(dev, "%s()\n", __func__);
1153
1154 genpd = dev_to_genpd(dev);
1155 if (IS_ERR(genpd))
1156 return -EINVAL;
1157
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001158 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001159}
1160
1161/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001162 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001163 * @dev: Device to resume.
1164 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001165 * Make sure the domain will be in the same power state as before the
1166 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001167 */
1168static int pm_genpd_restore_noirq(struct device *dev)
1169{
1170 struct generic_pm_domain *genpd;
1171
1172 dev_dbg(dev, "%s()\n", __func__);
1173
1174 genpd = dev_to_genpd(dev);
1175 if (IS_ERR(genpd))
1176 return -EINVAL;
1177
1178 /*
1179 * Since all of the "noirq" callbacks are executed sequentially, it is
1180 * guaranteed that this function will never run twice in parallel for
1181 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001182 *
1183 * At this point suspended_count == 0 means we are being run for the
1184 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001185 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001186 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001187 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001188 * The boot kernel might put the domain into arbitrary state,
1189 * so make it appear as powered off to pm_genpd_poweron(), so
1190 * that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001191 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001192 genpd->status = GPD_STATE_POWER_OFF;
1193 if (genpd->suspend_power_off) {
1194 /*
1195 * If the domain was off before the hibernation, make
1196 * sure it will be off going forward.
1197 */
1198 if (genpd->power_off)
1199 genpd->power_off(genpd);
1200
1201 return 0;
1202 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001203 }
1204
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001205 if (genpd->suspend_power_off)
1206 return 0;
1207
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001208 pm_genpd_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001209
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001210 return dev_gpd_data(dev)->always_on ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001211}
1212
1213/**
1214 * pm_genpd_complete - Complete power transition of a device in a power domain.
1215 * @dev: Device to complete the transition of.
1216 *
1217 * Complete a power transition of a device (during a system-wide power
1218 * transition) under the assumption that its pm_domain field points to the
1219 * domain member of an object of type struct generic_pm_domain representing
1220 * a power domain consisting of I/O devices.
1221 */
1222static void pm_genpd_complete(struct device *dev)
1223{
1224 struct generic_pm_domain *genpd;
1225 bool run_complete;
1226
1227 dev_dbg(dev, "%s()\n", __func__);
1228
1229 genpd = dev_to_genpd(dev);
1230 if (IS_ERR(genpd))
1231 return;
1232
1233 mutex_lock(&genpd->lock);
1234
1235 run_complete = !genpd->suspend_power_off;
1236 if (--genpd->prepared_count == 0)
1237 genpd->suspend_power_off = false;
1238
1239 mutex_unlock(&genpd->lock);
1240
1241 if (run_complete) {
1242 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001243 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001244 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001245 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001246 }
1247}
1248
1249#else
1250
1251#define pm_genpd_prepare NULL
1252#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001253#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001254#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001255#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001256#define pm_genpd_resume_noirq NULL
1257#define pm_genpd_resume NULL
1258#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001259#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001260#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001261#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001262#define pm_genpd_thaw_noirq NULL
1263#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001264#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001265#define pm_genpd_complete NULL
1266
1267#endif /* CONFIG_PM_SLEEP */
1268
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001269static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1270{
1271 struct generic_pm_domain_data *gpd_data;
1272
1273 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1274 if (!gpd_data)
1275 return NULL;
1276
1277 mutex_init(&gpd_data->lock);
1278 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1279 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1280 return gpd_data;
1281}
1282
1283static void __pm_genpd_free_dev_data(struct device *dev,
1284 struct generic_pm_domain_data *gpd_data)
1285{
1286 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1287 kfree(gpd_data);
1288}
1289
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001290/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001291 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001292 * @genpd: PM domain to add the device to.
1293 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001294 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001295 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001296int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1297 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001298{
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001299 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001300 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001301 int ret = 0;
1302
1303 dev_dbg(dev, "%s()\n", __func__);
1304
1305 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1306 return -EINVAL;
1307
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001308 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1309 if (!gpd_data_new)
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001310 return -ENOMEM;
1311
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001312 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001313
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001314 if (genpd->prepared_count > 0) {
1315 ret = -EAGAIN;
1316 goto out;
1317 }
1318
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001319 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1320 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001321 ret = -EINVAL;
1322 goto out;
1323 }
1324
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001325 ret = dev_pm_get_subsys_data(dev);
1326 if (ret)
1327 goto out;
1328
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001329 genpd->device_count++;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001330 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001331
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001332 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001333
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001334 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001335 if (dev->power.subsys_data->domain_data) {
1336 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1337 } else {
1338 gpd_data = gpd_data_new;
1339 dev->power.subsys_data->domain_data = &gpd_data->base;
1340 }
1341 gpd_data->refcount++;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001342 if (td)
1343 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001344
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001345 spin_unlock_irq(&dev->power.lock);
1346
1347 mutex_lock(&gpd_data->lock);
1348 gpd_data->base.dev = dev;
1349 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1350 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001351 gpd_data->td.constraint_changed = true;
1352 gpd_data->td.effective_constraint_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001353 mutex_unlock(&gpd_data->lock);
1354
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001355 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001356 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001357
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001358 if (gpd_data != gpd_data_new)
1359 __pm_genpd_free_dev_data(dev, gpd_data_new);
1360
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001361 return ret;
1362}
1363
1364/**
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001365 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1366 * @genpd_node: Device tree node pointer representing a PM domain to which the
1367 * the device is added to.
1368 * @dev: Device to be added.
1369 * @td: Set of PM QoS timing parameters to attach to the device.
1370 */
1371int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1372 struct gpd_timing_data *td)
1373{
1374 struct generic_pm_domain *genpd = NULL, *gpd;
1375
1376 dev_dbg(dev, "%s()\n", __func__);
1377
1378 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1379 return -EINVAL;
1380
1381 mutex_lock(&gpd_list_lock);
1382 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1383 if (gpd->of_node == genpd_node) {
1384 genpd = gpd;
1385 break;
1386 }
1387 }
1388 mutex_unlock(&gpd_list_lock);
1389
1390 if (!genpd)
1391 return -EINVAL;
1392
1393 return __pm_genpd_add_device(genpd, dev, td);
1394}
1395
1396/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001397 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1398 * @genpd: PM domain to remove the device from.
1399 * @dev: Device to be removed.
1400 */
1401int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1402 struct device *dev)
1403{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001404 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001405 struct pm_domain_data *pdd;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001406 bool remove = false;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001407 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001408
1409 dev_dbg(dev, "%s()\n", __func__);
1410
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001411 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1412 || IS_ERR_OR_NULL(dev->pm_domain)
1413 || pd_to_genpd(dev->pm_domain) != genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001414 return -EINVAL;
1415
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001416 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001417
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001418 if (genpd->prepared_count > 0) {
1419 ret = -EAGAIN;
1420 goto out;
1421 }
1422
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001423 genpd->device_count--;
1424 genpd->max_off_time_changed = true;
1425
1426 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001427
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001428 dev->pm_domain = NULL;
1429 pdd = dev->power.subsys_data->domain_data;
1430 list_del_init(&pdd->list_node);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001431 gpd_data = to_gpd_data(pdd);
1432 if (--gpd_data->refcount == 0) {
1433 dev->power.subsys_data->domain_data = NULL;
1434 remove = true;
1435 }
1436
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001437 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001438
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001439 mutex_lock(&gpd_data->lock);
1440 pdd->dev = NULL;
1441 mutex_unlock(&gpd_data->lock);
1442
1443 genpd_release_lock(genpd);
1444
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001445 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001446 if (remove)
1447 __pm_genpd_free_dev_data(dev, gpd_data);
1448
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001449 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001450
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001451 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001452 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001453
1454 return ret;
1455}
1456
1457/**
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001458 * pm_genpd_dev_always_on - Set/unset the "always on" flag for a given device.
1459 * @dev: Device to set/unset the flag for.
1460 * @val: The new value of the device's "always on" flag.
1461 */
1462void pm_genpd_dev_always_on(struct device *dev, bool val)
1463{
1464 struct pm_subsys_data *psd;
1465 unsigned long flags;
1466
1467 spin_lock_irqsave(&dev->power.lock, flags);
1468
1469 psd = dev_to_psd(dev);
1470 if (psd && psd->domain_data)
1471 to_gpd_data(psd->domain_data)->always_on = val;
1472
1473 spin_unlock_irqrestore(&dev->power.lock, flags);
1474}
1475EXPORT_SYMBOL_GPL(pm_genpd_dev_always_on);
1476
1477/**
Rafael J. Wysockica1d72f2012-05-14 21:45:52 +02001478 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1479 * @dev: Device to set/unset the flag for.
1480 * @val: The new value of the device's "need restore" flag.
1481 */
1482void pm_genpd_dev_need_restore(struct device *dev, bool val)
1483{
1484 struct pm_subsys_data *psd;
1485 unsigned long flags;
1486
1487 spin_lock_irqsave(&dev->power.lock, flags);
1488
1489 psd = dev_to_psd(dev);
1490 if (psd && psd->domain_data)
1491 to_gpd_data(psd->domain_data)->need_restore = val;
1492
1493 spin_unlock_irqrestore(&dev->power.lock, flags);
1494}
1495EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1496
1497/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001498 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1499 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001500 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001501 */
1502int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001503 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001504{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001505 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001506 int ret = 0;
1507
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001508 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001509 return -EINVAL;
1510
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001511 start:
1512 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001513 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001514
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001515 if (subdomain->status != GPD_STATE_POWER_OFF
1516 && subdomain->status != GPD_STATE_ACTIVE) {
1517 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001518 genpd_release_lock(genpd);
1519 goto start;
1520 }
1521
1522 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001523 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001524 ret = -EINVAL;
1525 goto out;
1526 }
1527
Huang Ying4fcac102012-05-07 21:35:45 +02001528 list_for_each_entry(link, &genpd->master_links, master_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001529 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001530 ret = -EINVAL;
1531 goto out;
1532 }
1533 }
1534
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001535 link = kzalloc(sizeof(*link), GFP_KERNEL);
1536 if (!link) {
1537 ret = -ENOMEM;
1538 goto out;
1539 }
1540 link->master = genpd;
1541 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001542 link->slave = subdomain;
1543 list_add_tail(&link->slave_node, &subdomain->slave_links);
1544 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001545 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001546
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001547 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001548 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001549 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001550
1551 return ret;
1552}
1553
1554/**
1555 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1556 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001557 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001558 */
1559int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001560 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001561{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001562 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001563 int ret = -EINVAL;
1564
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001565 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001566 return -EINVAL;
1567
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001568 start:
1569 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001570
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001571 list_for_each_entry(link, &genpd->master_links, master_node) {
1572 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001573 continue;
1574
1575 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1576
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001577 if (subdomain->status != GPD_STATE_POWER_OFF
1578 && subdomain->status != GPD_STATE_ACTIVE) {
1579 mutex_unlock(&subdomain->lock);
1580 genpd_release_lock(genpd);
1581 goto start;
1582 }
1583
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001584 list_del(&link->master_node);
1585 list_del(&link->slave_node);
1586 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001587 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001588 genpd_sd_counter_dec(genpd);
1589
1590 mutex_unlock(&subdomain->lock);
1591
1592 ret = 0;
1593 break;
1594 }
1595
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001596 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001597
1598 return ret;
1599}
1600
1601/**
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001602 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1603 * @dev: Device to add the callbacks to.
1604 * @ops: Set of callbacks to add.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001605 * @td: Timing data to add to the device along with the callbacks (optional).
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001606 *
1607 * Every call to this routine should be balanced with a call to
1608 * __pm_genpd_remove_callbacks() and they must not be nested.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001609 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001610int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1611 struct gpd_timing_data *td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001612{
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001613 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001614 int ret = 0;
1615
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001616 if (!(dev && ops))
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001617 return -EINVAL;
1618
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001619 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1620 if (!gpd_data_new)
1621 return -ENOMEM;
1622
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001623 pm_runtime_disable(dev);
1624 device_pm_lock();
1625
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001626 ret = dev_pm_get_subsys_data(dev);
1627 if (ret)
1628 goto out;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001629
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001630 spin_lock_irq(&dev->power.lock);
1631
1632 if (dev->power.subsys_data->domain_data) {
1633 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001634 } else {
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001635 gpd_data = gpd_data_new;
1636 dev->power.subsys_data->domain_data = &gpd_data->base;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001637 }
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001638 gpd_data->refcount++;
1639 gpd_data->ops = *ops;
1640 if (td)
1641 gpd_data->td = *td;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001642
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001643 spin_unlock_irq(&dev->power.lock);
1644
1645 out:
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001646 device_pm_unlock();
1647 pm_runtime_enable(dev);
1648
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001649 if (gpd_data != gpd_data_new)
1650 __pm_genpd_free_dev_data(dev, gpd_data_new);
1651
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001652 return ret;
1653}
1654EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1655
1656/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001657 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001658 * @dev: Device to remove the callbacks from.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001659 * @clear_td: If set, clear the device's timing data too.
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001660 *
1661 * This routine can only be called after pm_genpd_add_callbacks().
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001662 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001663int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001664{
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001665 struct generic_pm_domain_data *gpd_data = NULL;
1666 bool remove = false;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001667 int ret = 0;
1668
1669 if (!(dev && dev->power.subsys_data))
1670 return -EINVAL;
1671
1672 pm_runtime_disable(dev);
1673 device_pm_lock();
1674
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001675 spin_lock_irq(&dev->power.lock);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001676
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001677 if (dev->power.subsys_data->domain_data) {
1678 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
Sachin Kamatdb79e532012-07-11 12:25:49 +02001679 gpd_data->ops = (struct gpd_dev_ops){ NULL };
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001680 if (clear_td)
1681 gpd_data->td = (struct gpd_timing_data){ 0 };
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001682
1683 if (--gpd_data->refcount == 0) {
1684 dev->power.subsys_data->domain_data = NULL;
1685 remove = true;
1686 }
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001687 } else {
1688 ret = -EINVAL;
1689 }
1690
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001691 spin_unlock_irq(&dev->power.lock);
1692
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001693 device_pm_unlock();
1694 pm_runtime_enable(dev);
1695
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001696 if (ret)
1697 return ret;
1698
1699 dev_pm_put_subsys_data(dev);
1700 if (remove)
1701 __pm_genpd_free_dev_data(dev, gpd_data);
1702
1703 return 0;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001704}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001705EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001706
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001707int genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
1708{
1709 struct cpuidle_driver *cpuidle_drv;
1710 struct gpd_cpu_data *cpu_data;
1711 struct cpuidle_state *idle_state;
1712 int ret = 0;
1713
1714 if (IS_ERR_OR_NULL(genpd) || state < 0)
1715 return -EINVAL;
1716
1717 genpd_acquire_lock(genpd);
1718
1719 if (genpd->cpu_data) {
1720 ret = -EEXIST;
1721 goto out;
1722 }
1723 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1724 if (!cpu_data) {
1725 ret = -ENOMEM;
1726 goto out;
1727 }
1728 cpuidle_drv = cpuidle_driver_ref();
1729 if (!cpuidle_drv) {
1730 ret = -ENODEV;
1731 goto out;
1732 }
1733 if (cpuidle_drv->state_count <= state) {
1734 ret = -EINVAL;
1735 goto err;
1736 }
1737 idle_state = &cpuidle_drv->states[state];
1738 if (!idle_state->disabled) {
1739 ret = -EAGAIN;
1740 goto err;
1741 }
1742 cpu_data->idle_state = idle_state;
1743 cpu_data->saved_exit_latency = idle_state->exit_latency;
1744 genpd->cpu_data = cpu_data;
1745 genpd_recalc_cpu_exit_latency(genpd);
1746
1747 out:
1748 genpd_release_lock(genpd);
1749 return ret;
1750
1751 err:
1752 cpuidle_driver_unref();
1753 goto out;
1754}
1755
1756int genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1757{
1758 struct gpd_cpu_data *cpu_data;
1759 struct cpuidle_state *idle_state;
1760 int ret = 0;
1761
1762 if (IS_ERR_OR_NULL(genpd))
1763 return -EINVAL;
1764
1765 genpd_acquire_lock(genpd);
1766
1767 cpu_data = genpd->cpu_data;
1768 if (!cpu_data) {
1769 ret = -ENODEV;
1770 goto out;
1771 }
1772 idle_state = cpu_data->idle_state;
1773 if (!idle_state->disabled) {
1774 ret = -EAGAIN;
1775 goto out;
1776 }
1777 idle_state->exit_latency = cpu_data->saved_exit_latency;
1778 cpuidle_driver_unref();
1779 genpd->cpu_data = NULL;
1780 kfree(cpu_data);
1781
1782 out:
1783 genpd_release_lock(genpd);
1784 return ret;
1785}
1786
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001787/* Default device callbacks for generic PM domains. */
1788
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001789/**
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001790 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1791 * @dev: Device to handle.
1792 */
1793static int pm_genpd_default_save_state(struct device *dev)
1794{
1795 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001796
1797 cb = dev_gpd_data(dev)->ops.save_state;
1798 if (cb)
1799 return cb(dev);
1800
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001801 if (dev->type && dev->type->pm)
1802 cb = dev->type->pm->runtime_suspend;
1803 else if (dev->class && dev->class->pm)
1804 cb = dev->class->pm->runtime_suspend;
1805 else if (dev->bus && dev->bus->pm)
1806 cb = dev->bus->pm->runtime_suspend;
1807 else
1808 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001809
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001810 if (!cb && dev->driver && dev->driver->pm)
1811 cb = dev->driver->pm->runtime_suspend;
1812
1813 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001814}
1815
1816/**
1817 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1818 * @dev: Device to handle.
1819 */
1820static int pm_genpd_default_restore_state(struct device *dev)
1821{
1822 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001823
1824 cb = dev_gpd_data(dev)->ops.restore_state;
1825 if (cb)
1826 return cb(dev);
1827
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001828 if (dev->type && dev->type->pm)
1829 cb = dev->type->pm->runtime_resume;
1830 else if (dev->class && dev->class->pm)
1831 cb = dev->class->pm->runtime_resume;
1832 else if (dev->bus && dev->bus->pm)
1833 cb = dev->bus->pm->runtime_resume;
1834 else
1835 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001836
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001837 if (!cb && dev->driver && dev->driver->pm)
1838 cb = dev->driver->pm->runtime_resume;
1839
1840 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001841}
1842
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001843#ifdef CONFIG_PM_SLEEP
1844
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001845/**
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001846 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1847 * @dev: Device to handle.
1848 */
1849static int pm_genpd_default_suspend(struct device *dev)
1850{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001851 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001852
1853 return cb ? cb(dev) : pm_generic_suspend(dev);
1854}
1855
1856/**
1857 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1858 * @dev: Device to handle.
1859 */
1860static int pm_genpd_default_suspend_late(struct device *dev)
1861{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001862 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001863
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001864 return cb ? cb(dev) : pm_generic_suspend_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001865}
1866
1867/**
1868 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1869 * @dev: Device to handle.
1870 */
1871static int pm_genpd_default_resume_early(struct device *dev)
1872{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001873 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001874
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001875 return cb ? cb(dev) : pm_generic_resume_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001876}
1877
1878/**
1879 * pm_genpd_default_resume - Default "device resume" for PM domians.
1880 * @dev: Device to handle.
1881 */
1882static int pm_genpd_default_resume(struct device *dev)
1883{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001884 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001885
1886 return cb ? cb(dev) : pm_generic_resume(dev);
1887}
1888
1889/**
1890 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1891 * @dev: Device to handle.
1892 */
1893static int pm_genpd_default_freeze(struct device *dev)
1894{
1895 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1896
1897 return cb ? cb(dev) : pm_generic_freeze(dev);
1898}
1899
1900/**
1901 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1902 * @dev: Device to handle.
1903 */
1904static int pm_genpd_default_freeze_late(struct device *dev)
1905{
1906 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1907
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001908 return cb ? cb(dev) : pm_generic_freeze_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001909}
1910
1911/**
1912 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1913 * @dev: Device to handle.
1914 */
1915static int pm_genpd_default_thaw_early(struct device *dev)
1916{
1917 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1918
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001919 return cb ? cb(dev) : pm_generic_thaw_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001920}
1921
1922/**
1923 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1924 * @dev: Device to handle.
1925 */
1926static int pm_genpd_default_thaw(struct device *dev)
1927{
1928 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1929
1930 return cb ? cb(dev) : pm_generic_thaw(dev);
1931}
1932
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001933#else /* !CONFIG_PM_SLEEP */
1934
1935#define pm_genpd_default_suspend NULL
1936#define pm_genpd_default_suspend_late NULL
1937#define pm_genpd_default_resume_early NULL
1938#define pm_genpd_default_resume NULL
1939#define pm_genpd_default_freeze NULL
1940#define pm_genpd_default_freeze_late NULL
1941#define pm_genpd_default_thaw_early NULL
1942#define pm_genpd_default_thaw NULL
1943
1944#endif /* !CONFIG_PM_SLEEP */
1945
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001946/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001947 * pm_genpd_init - Initialize a generic I/O PM domain object.
1948 * @genpd: PM domain object to initialize.
1949 * @gov: PM domain governor to associate with the domain (may be NULL).
1950 * @is_off: Initial value of the domain's power_is_off field.
1951 */
1952void pm_genpd_init(struct generic_pm_domain *genpd,
1953 struct dev_power_governor *gov, bool is_off)
1954{
1955 if (IS_ERR_OR_NULL(genpd))
1956 return;
1957
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001958 INIT_LIST_HEAD(&genpd->master_links);
1959 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001960 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001961 mutex_init(&genpd->lock);
1962 genpd->gov = gov;
1963 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1964 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001965 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001966 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1967 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001968 genpd->poweroff_task = NULL;
1969 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001970 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001971 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001972 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001973 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1974 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1975 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001976 genpd->domain.ops.prepare = pm_genpd_prepare;
1977 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001978 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001979 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1980 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001981 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001982 genpd->domain.ops.resume = pm_genpd_resume;
1983 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001984 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001985 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1986 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001987 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001988 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001989 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001990 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001991 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001992 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001993 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001994 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001995 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001996 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1997 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001998 genpd->dev_ops.suspend = pm_genpd_default_suspend;
1999 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2000 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2001 genpd->dev_ops.resume = pm_genpd_default_resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002002 genpd->dev_ops.freeze = pm_genpd_default_freeze;
2003 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2004 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2005 genpd->dev_ops.thaw = pm_genpd_default_thaw;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02002006 mutex_lock(&gpd_list_lock);
2007 list_add(&genpd->gpd_list_node, &gpd_list);
2008 mutex_unlock(&gpd_list_lock);
2009}