blob: 73ce9fbe983961598d53f3bbeef0057cda084d4b [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>
14#include <linux/slab.h>
15#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020016#include <linux/sched.h>
17#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010018#include <linux/export.h>
19
20#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
21({ \
22 type (*__routine)(struct device *__d); \
23 type __ret = (type)0; \
24 \
25 __routine = genpd->dev_ops.callback; \
26 if (__routine) { \
27 __ret = __routine(dev); \
28 } else { \
29 __routine = dev_gpd_data(dev)->ops.callback; \
30 if (__routine) \
31 __ret = __routine(dev); \
32 } \
33 __ret; \
34})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020035
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010036#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
37({ \
38 ktime_t __start = ktime_get(); \
39 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
40 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
41 struct generic_pm_domain_data *__gpd_data = dev_gpd_data(dev); \
42 if (__elapsed > __gpd_data->td.field) { \
43 __gpd_data->td.field = __elapsed; \
44 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
45 __elapsed); \
46 } \
47 __retval; \
48})
49
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020050static LIST_HEAD(gpd_list);
51static DEFINE_MUTEX(gpd_list_lock);
52
Rafael J. Wysocki52480512011-07-01 22:13:10 +020053#ifdef CONFIG_PM
54
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010055struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020056{
57 if (IS_ERR_OR_NULL(dev->pm_domain))
58 return ERR_PTR(-EINVAL);
59
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020060 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020061}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020062
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010063static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
64{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010065 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
66 stop_latency_ns, "stop");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010067}
68
69static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
70{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010071 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
72 start_latency_ns, "start");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010073}
74
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010075static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
76{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010077 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
78 save_state_latency_ns, "state save");
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010079}
80
81static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
82{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010083 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
84 restore_state_latency_ns,
85 "state restore");
Rafael J. Wysockiecf00472011-11-27 13:11:44 +010086}
87
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020088static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020089{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020090 bool ret = false;
91
92 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
93 ret = !!atomic_dec_and_test(&genpd->sd_count);
94
95 return ret;
96}
97
98static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
99{
100 atomic_inc(&genpd->sd_count);
101 smp_mb__after_atomic_inc();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200102}
103
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200104static void genpd_acquire_lock(struct generic_pm_domain *genpd)
105{
106 DEFINE_WAIT(wait);
107
108 mutex_lock(&genpd->lock);
109 /*
110 * Wait for the domain to transition into either the active,
111 * or the power off state.
112 */
113 for (;;) {
114 prepare_to_wait(&genpd->status_wait_queue, &wait,
115 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200116 if (genpd->status == GPD_STATE_ACTIVE
117 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200118 break;
119 mutex_unlock(&genpd->lock);
120
121 schedule();
122
123 mutex_lock(&genpd->lock);
124 }
125 finish_wait(&genpd->status_wait_queue, &wait);
126}
127
128static void genpd_release_lock(struct generic_pm_domain *genpd)
129{
130 mutex_unlock(&genpd->lock);
131}
132
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200133static void genpd_set_active(struct generic_pm_domain *genpd)
134{
135 if (genpd->resume_count == 0)
136 genpd->status = GPD_STATE_ACTIVE;
137}
138
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200139/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200140 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200141 * @genpd: PM domain to power up.
142 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200143 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200144 * resume a device belonging to it.
145 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200146int __pm_genpd_poweron(struct generic_pm_domain *genpd)
147 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200148{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200149 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200150 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200151 int ret = 0;
152
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200153 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200154 for (;;) {
155 prepare_to_wait(&genpd->status_wait_queue, &wait,
156 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200157 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200158 break;
159 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200160
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200161 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200162
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200163 mutex_lock(&genpd->lock);
164 }
165 finish_wait(&genpd->status_wait_queue, &wait);
166
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200167 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200168 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200169 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200170
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200171 if (genpd->status != GPD_STATE_POWER_OFF) {
172 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200173 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200174 }
175
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200176 /*
177 * The list is guaranteed not to change while the loop below is being
178 * executed, unless one of the masters' .power_on() callbacks fiddles
179 * with it.
180 */
181 list_for_each_entry(link, &genpd->slave_links, slave_node) {
182 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200183 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200184
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200185 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200186
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200187 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200188
189 mutex_lock(&genpd->lock);
190
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200191 /*
192 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200193 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200194 */
195 genpd->status = GPD_STATE_POWER_OFF;
196 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200197 if (ret) {
198 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200199 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200200 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200201 }
202
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200203 if (genpd->power_on) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100204 ktime_t time_start = ktime_get();
205 s64 elapsed_ns;
206
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200207 ret = genpd->power_on(genpd);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200208 if (ret)
209 goto err;
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100210
211 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100212 if (elapsed_ns > genpd->power_on_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100213 genpd->power_on_latency_ns = elapsed_ns;
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100214 if (genpd->name)
215 pr_warning("%s: Power-on latency exceeded, "
216 "new value %lld ns\n", genpd->name,
217 elapsed_ns);
218 }
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200219 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200220
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200221 genpd_set_active(genpd);
222
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200223 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200224
225 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200226 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
227 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200228
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200229 return ret;
230}
231
232/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200233 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200234 * @genpd: PM domain to power up.
235 */
236int pm_genpd_poweron(struct generic_pm_domain *genpd)
237{
238 int ret;
239
240 mutex_lock(&genpd->lock);
241 ret = __pm_genpd_poweron(genpd);
242 mutex_unlock(&genpd->lock);
243 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200244}
245
246#endif /* CONFIG_PM */
247
248#ifdef CONFIG_PM_RUNTIME
249
250/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200251 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200252 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200253 * @genpd: PM domain the device belongs to.
254 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200255static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200256 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200257 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200258{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200259 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200260 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200261 int ret = 0;
262
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200263 if (gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200264 return 0;
265
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200266 mutex_unlock(&genpd->lock);
267
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100268 genpd_start_dev(genpd, dev);
269 ret = genpd_save_dev(genpd, dev);
270 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200271
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200272 mutex_lock(&genpd->lock);
273
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200274 if (!ret)
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200275 gpd_data->need_restore = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200276
277 return ret;
278}
279
280/**
281 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200282 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200283 * @genpd: PM domain the device belongs to.
284 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200285static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200286 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200287 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200288{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200289 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200290 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200291
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200292 if (!gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200293 return;
294
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200295 mutex_unlock(&genpd->lock);
296
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100297 genpd_start_dev(genpd, dev);
298 genpd_restore_dev(genpd, dev);
299 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200300
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200301 mutex_lock(&genpd->lock);
302
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200303 gpd_data->need_restore = false;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200304}
305
306/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200307 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
308 * @genpd: PM domain to check.
309 *
310 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
311 * a "power off" operation, which means that a "power on" has occured in the
312 * meantime, or if its resume_count field is different from zero, which means
313 * that one of its devices has been resumed in the meantime.
314 */
315static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
316{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200317 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200318 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200319}
320
321/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200322 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
323 * @genpd: PM domait to power off.
324 *
325 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
326 * before.
327 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200328void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200329{
330 if (!work_pending(&genpd->power_off_work))
331 queue_work(pm_wq, &genpd->power_off_work);
332}
333
334/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200335 * pm_genpd_poweroff - Remove power from a given PM domain.
336 * @genpd: PM domain to power down.
337 *
338 * If all of the @genpd's devices have been suspended and all of its subdomains
339 * have been powered down, run the runtime suspend callbacks provided by all of
340 * the @genpd's devices' drivers and remove power from @genpd.
341 */
342static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200343 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200344{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200345 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200346 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200347 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200348 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200349
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200350 start:
351 /*
352 * Do not try to power off the domain in the following situations:
353 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200354 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200355 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200356 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200357 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200358 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200359 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200360 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200361 return 0;
362
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200363 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200364 return -EBUSY;
365
366 not_suspended = 0;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200367 list_for_each_entry(pdd, &genpd->dev_list, list_node)
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200368 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100369 || pdd->dev->power.irq_safe || to_gpd_data(pdd)->always_on))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200370 not_suspended++;
371
372 if (not_suspended > genpd->in_progress)
373 return -EBUSY;
374
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200375 if (genpd->poweroff_task) {
376 /*
377 * Another instance of pm_genpd_poweroff() is executing
378 * callbacks, so tell it to start over and return.
379 */
380 genpd->status = GPD_STATE_REPEAT;
381 return 0;
382 }
383
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200384 if (genpd->gov && genpd->gov->power_down_ok) {
385 if (!genpd->gov->power_down_ok(&genpd->domain))
386 return -EAGAIN;
387 }
388
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200389 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200390 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200391
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200392 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200393 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200394 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200395
396 if (genpd_abort_poweroff(genpd))
397 goto out;
398
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200399 if (ret) {
400 genpd_set_active(genpd);
401 goto out;
402 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200403
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200404 if (genpd->status == GPD_STATE_REPEAT) {
405 genpd->poweroff_task = NULL;
406 goto start;
407 }
408 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200409
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200410 if (genpd->power_off) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100411 ktime_t time_start;
412 s64 elapsed_ns;
413
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200414 if (atomic_read(&genpd->sd_count) > 0) {
415 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200416 goto out;
417 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200418
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100419 time_start = ktime_get();
420
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200421 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200422 * If sd_count > 0 at this point, one of the subdomains hasn't
423 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200424 * incrementing it. In that case pm_genpd_poweron() will wait
425 * for us to drop the lock, so we can call .power_off() and let
426 * the pm_genpd_poweron() restore power for us (this shouldn't
427 * happen very often).
428 */
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200429 ret = genpd->power_off(genpd);
430 if (ret == -EBUSY) {
431 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200432 goto out;
433 }
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100434
435 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100436 if (elapsed_ns > genpd->power_off_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100437 genpd->power_off_latency_ns = elapsed_ns;
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100438 if (genpd->name)
439 pr_warning("%s: Power-off latency exceeded, "
440 "new value %lld ns\n", genpd->name,
441 elapsed_ns);
442 }
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200443 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200444
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200445 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100446 genpd->power_off_time = ktime_get();
447
448 /* Update PM QoS information for devices in the domain. */
449 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
450 struct gpd_timing_data *td = &to_gpd_data(pdd)->td;
451
452 pm_runtime_update_max_time_suspended(pdd->dev,
453 td->start_latency_ns +
454 td->restore_state_latency_ns +
455 genpd->power_on_latency_ns);
456 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200457
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200458 list_for_each_entry(link, &genpd->slave_links, slave_node) {
459 genpd_sd_counter_dec(link->master);
460 genpd_queue_power_off_work(link->master);
461 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200462
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200463 out:
464 genpd->poweroff_task = NULL;
465 wake_up_all(&genpd->status_wait_queue);
466 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200467}
468
469/**
470 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
471 * @work: Work structure used for scheduling the execution of this function.
472 */
473static void genpd_power_off_work_fn(struct work_struct *work)
474{
475 struct generic_pm_domain *genpd;
476
477 genpd = container_of(work, struct generic_pm_domain, power_off_work);
478
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200479 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200480 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200481 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200482}
483
484/**
485 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
486 * @dev: Device to suspend.
487 *
488 * Carry out a runtime suspend of a device under the assumption that its
489 * pm_domain field points to the domain member of an object of type
490 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
491 */
492static int pm_genpd_runtime_suspend(struct device *dev)
493{
494 struct generic_pm_domain *genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100495 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100496 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200497
498 dev_dbg(dev, "%s()\n", __func__);
499
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200500 genpd = dev_to_genpd(dev);
501 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200502 return -EINVAL;
503
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200504 might_sleep_if(!genpd->dev_irq_safe);
505
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100506 if (dev_gpd_data(dev)->always_on)
507 return -EBUSY;
508
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100509 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
510 if (stop_ok && !stop_ok(dev))
511 return -EBUSY;
512
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100513 ret = genpd_stop_dev(genpd, dev);
514 if (ret)
515 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200516
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100517 pm_runtime_update_max_time_suspended(dev,
518 dev_gpd_data(dev)->td.start_latency_ns);
519
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200520 /*
521 * If power.irq_safe is set, this routine will be run with interrupts
522 * off, so it can't use mutexes.
523 */
524 if (dev->power.irq_safe)
525 return 0;
526
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200527 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200528 genpd->in_progress++;
529 pm_genpd_poweroff(genpd);
530 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200531 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200532
533 return 0;
534}
535
536/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200537 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
538 * @dev: Device to resume.
539 *
540 * Carry out a runtime resume of a device under the assumption that its
541 * pm_domain field points to the domain member of an object of type
542 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
543 */
544static int pm_genpd_runtime_resume(struct device *dev)
545{
546 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200547 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200548 int ret;
549
550 dev_dbg(dev, "%s()\n", __func__);
551
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200552 genpd = dev_to_genpd(dev);
553 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200554 return -EINVAL;
555
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200556 might_sleep_if(!genpd->dev_irq_safe);
557
558 /* If power.irq_safe, the PM domain is never powered off. */
559 if (dev->power.irq_safe)
560 goto out;
561
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200562 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200563 ret = __pm_genpd_poweron(genpd);
564 if (ret) {
565 mutex_unlock(&genpd->lock);
566 return ret;
567 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200568 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200569 genpd->resume_count++;
570 for (;;) {
571 prepare_to_wait(&genpd->status_wait_queue, &wait,
572 TASK_UNINTERRUPTIBLE);
573 /*
574 * If current is the powering off task, we have been called
575 * reentrantly from one of the device callbacks, so we should
576 * not wait.
577 */
578 if (!genpd->poweroff_task || genpd->poweroff_task == current)
579 break;
580 mutex_unlock(&genpd->lock);
581
582 schedule();
583
584 mutex_lock(&genpd->lock);
585 }
586 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200587 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200588 genpd->resume_count--;
589 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200590 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200591 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200592
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200593 out:
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100594 genpd_start_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200595
596 return 0;
597}
598
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200599/**
600 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
601 */
602void pm_genpd_poweroff_unused(void)
603{
604 struct generic_pm_domain *genpd;
605
606 mutex_lock(&gpd_list_lock);
607
608 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
609 genpd_queue_power_off_work(genpd);
610
611 mutex_unlock(&gpd_list_lock);
612}
613
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200614#else
615
616static inline void genpd_power_off_work_fn(struct work_struct *work) {}
617
618#define pm_genpd_runtime_suspend NULL
619#define pm_genpd_runtime_resume NULL
620
621#endif /* CONFIG_PM_RUNTIME */
622
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200623#ifdef CONFIG_PM_SLEEP
624
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100625static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
626 struct device *dev)
627{
628 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
629}
630
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100631static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
632{
633 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
634}
635
636static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
637{
638 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
639}
640
641static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
642{
643 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
644}
645
646static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
647{
648 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
649}
650
651static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
652{
653 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
654}
655
656static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
657{
658 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
659}
660
661static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
662{
663 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
664}
665
666static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
667{
668 return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
669}
670
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200671/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200672 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200673 * @genpd: PM domain to power off, if possible.
674 *
675 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200676 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200677 *
678 * This function is only called in "noirq" stages of system power transitions,
679 * so it need not acquire locks (all of the "noirq" callbacks are executed
680 * sequentially, so it is guaranteed that it will never run twice in parallel).
681 */
682static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
683{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200684 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200685
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200686 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200687 return;
688
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200689 if (genpd->suspended_count != genpd->device_count
690 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200691 return;
692
693 if (genpd->power_off)
694 genpd->power_off(genpd);
695
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200696 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200697
698 list_for_each_entry(link, &genpd->slave_links, slave_node) {
699 genpd_sd_counter_dec(link->master);
700 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200701 }
702}
703
704/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200705 * resume_needed - Check whether to resume a device before system suspend.
706 * @dev: Device to check.
707 * @genpd: PM domain the device belongs to.
708 *
709 * There are two cases in which a device that can wake up the system from sleep
710 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
711 * to wake up the system and it has to remain active for this purpose while the
712 * system is in the sleep state and (2) if the device is not enabled to wake up
713 * the system from sleep states and it generally doesn't generate wakeup signals
714 * by itself (those signals are generated on its behalf by other parts of the
715 * system). In the latter case it may be necessary to reconfigure the device's
716 * wakeup settings during system suspend, because it may have been set up to
717 * signal remote wakeup from the system's working state as needed by runtime PM.
718 * Return 'true' in either of the above cases.
719 */
720static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
721{
722 bool active_wakeup;
723
724 if (!device_can_wakeup(dev))
725 return false;
726
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100727 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200728 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
729}
730
731/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200732 * pm_genpd_prepare - Start power transition of a device in a PM domain.
733 * @dev: Device to start the transition of.
734 *
735 * Start a power transition of a device (during a system-wide power transition)
736 * under the assumption that its pm_domain field points to the domain member of
737 * an object of type struct generic_pm_domain representing a PM domain
738 * consisting of I/O devices.
739 */
740static int pm_genpd_prepare(struct device *dev)
741{
742 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200743 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200744
745 dev_dbg(dev, "%s()\n", __func__);
746
747 genpd = dev_to_genpd(dev);
748 if (IS_ERR(genpd))
749 return -EINVAL;
750
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200751 /*
752 * If a wakeup request is pending for the device, it should be woken up
753 * at this point and a system wakeup event should be reported if it's
754 * set up to wake up the system from sleep states.
755 */
756 pm_runtime_get_noresume(dev);
757 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
758 pm_wakeup_event(dev, 0);
759
760 if (pm_wakeup_pending()) {
761 pm_runtime_put_sync(dev);
762 return -EBUSY;
763 }
764
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200765 if (resume_needed(dev, genpd))
766 pm_runtime_resume(dev);
767
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200768 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200769
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100770 if (genpd->prepared_count++ == 0) {
771 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200772 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100773 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200774
775 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200776
777 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200778 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200779 return 0;
780 }
781
782 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200783 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
784 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100785 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200786 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200787 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200788 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200789 __pm_runtime_disable(dev, false);
790
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200791 ret = pm_generic_prepare(dev);
792 if (ret) {
793 mutex_lock(&genpd->lock);
794
795 if (--genpd->prepared_count == 0)
796 genpd->suspend_power_off = false;
797
798 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200799 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200800 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200801
802 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200803 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200804}
805
806/**
807 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
808 * @dev: Device to suspend.
809 *
810 * Suspend a device under the assumption that its pm_domain field points to the
811 * domain member of an object of type struct generic_pm_domain representing
812 * a PM domain consisting of I/O devices.
813 */
814static int pm_genpd_suspend(struct device *dev)
815{
816 struct generic_pm_domain *genpd;
817
818 dev_dbg(dev, "%s()\n", __func__);
819
820 genpd = dev_to_genpd(dev);
821 if (IS_ERR(genpd))
822 return -EINVAL;
823
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100824 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200825}
826
827/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100828 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200829 * @dev: Device to suspend.
830 *
831 * Carry out a late suspend of a device under the assumption that its
832 * pm_domain field points to the domain member of an object of type
833 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
834 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100835static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200836{
837 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200838
839 dev_dbg(dev, "%s()\n", __func__);
840
841 genpd = dev_to_genpd(dev);
842 if (IS_ERR(genpd))
843 return -EINVAL;
844
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100845 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
846}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200847
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100848/**
849 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
850 * @dev: Device to suspend.
851 *
852 * Stop the device and remove power from the domain if all devices in it have
853 * been stopped.
854 */
855static int pm_genpd_suspend_noirq(struct device *dev)
856{
857 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200858
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100859 dev_dbg(dev, "%s()\n", __func__);
860
861 genpd = dev_to_genpd(dev);
862 if (IS_ERR(genpd))
863 return -EINVAL;
864
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100865 if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100866 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200867 return 0;
868
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100869 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200870
871 /*
872 * Since all of the "noirq" callbacks are executed sequentially, it is
873 * guaranteed that this function will never run twice in parallel for
874 * the same PM domain, so it is not necessary to use locking here.
875 */
876 genpd->suspended_count++;
877 pm_genpd_sync_poweroff(genpd);
878
879 return 0;
880}
881
882/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100883 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200884 * @dev: Device to resume.
885 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100886 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200887 */
888static int pm_genpd_resume_noirq(struct device *dev)
889{
890 struct generic_pm_domain *genpd;
891
892 dev_dbg(dev, "%s()\n", __func__);
893
894 genpd = dev_to_genpd(dev);
895 if (IS_ERR(genpd))
896 return -EINVAL;
897
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +0100898 if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
Rafael J. Wysockicc85b202012-03-13 22:39:31 +0100899 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200900 return 0;
901
902 /*
903 * Since all of the "noirq" callbacks are executed sequentially, it is
904 * guaranteed that this function will never run twice in parallel for
905 * the same PM domain, so it is not necessary to use locking here.
906 */
907 pm_genpd_poweron(genpd);
908 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200909
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100910 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200911}
912
913/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100914 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
915 * @dev: Device to resume.
916 *
917 * Carry out an early resume of a device under the assumption that its
918 * pm_domain field points to the domain member of an object of type
919 * struct generic_pm_domain representing a power domain consisting of I/O
920 * devices.
921 */
922static int pm_genpd_resume_early(struct device *dev)
923{
924 struct generic_pm_domain *genpd;
925
926 dev_dbg(dev, "%s()\n", __func__);
927
928 genpd = dev_to_genpd(dev);
929 if (IS_ERR(genpd))
930 return -EINVAL;
931
932 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
933}
934
935/**
936 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200937 * @dev: Device to resume.
938 *
939 * Resume a device under the assumption that its pm_domain field points to the
940 * domain member of an object of type struct generic_pm_domain representing
941 * a power domain consisting of I/O devices.
942 */
943static int pm_genpd_resume(struct device *dev)
944{
945 struct generic_pm_domain *genpd;
946
947 dev_dbg(dev, "%s()\n", __func__);
948
949 genpd = dev_to_genpd(dev);
950 if (IS_ERR(genpd))
951 return -EINVAL;
952
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100953 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200954}
955
956/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100957 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200958 * @dev: Device to freeze.
959 *
960 * Freeze a device under the assumption that its pm_domain field points to the
961 * domain member of an object of type struct generic_pm_domain representing
962 * a power domain consisting of I/O devices.
963 */
964static int pm_genpd_freeze(struct device *dev)
965{
966 struct generic_pm_domain *genpd;
967
968 dev_dbg(dev, "%s()\n", __func__);
969
970 genpd = dev_to_genpd(dev);
971 if (IS_ERR(genpd))
972 return -EINVAL;
973
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100974 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200975}
976
977/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100978 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
979 * @dev: Device to freeze.
980 *
981 * Carry out a late freeze of a device under the assumption that its
982 * pm_domain field points to the domain member of an object of type
983 * struct generic_pm_domain representing a power domain consisting of I/O
984 * devices.
985 */
986static int pm_genpd_freeze_late(struct device *dev)
987{
988 struct generic_pm_domain *genpd;
989
990 dev_dbg(dev, "%s()\n", __func__);
991
992 genpd = dev_to_genpd(dev);
993 if (IS_ERR(genpd))
994 return -EINVAL;
995
996 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
997}
998
999/**
1000 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001001 * @dev: Device to freeze.
1002 *
1003 * Carry out a late freeze of a device under the assumption that its
1004 * pm_domain field points to the domain member of an object of type
1005 * struct generic_pm_domain representing a power domain consisting of I/O
1006 * devices.
1007 */
1008static int pm_genpd_freeze_noirq(struct device *dev)
1009{
1010 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001011
1012 dev_dbg(dev, "%s()\n", __func__);
1013
1014 genpd = dev_to_genpd(dev);
1015 if (IS_ERR(genpd))
1016 return -EINVAL;
1017
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001018 return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1019 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001020}
1021
1022/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001023 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001024 * @dev: Device to thaw.
1025 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001026 * Start the device, unless power has been removed from the domain already
1027 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001028 */
1029static int pm_genpd_thaw_noirq(struct device *dev)
1030{
1031 struct generic_pm_domain *genpd;
1032
1033 dev_dbg(dev, "%s()\n", __func__);
1034
1035 genpd = dev_to_genpd(dev);
1036 if (IS_ERR(genpd))
1037 return -EINVAL;
1038
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001039 return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1040 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001041}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001042
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001043/**
1044 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1045 * @dev: Device to thaw.
1046 *
1047 * Carry out an early thaw of a device under the assumption that its
1048 * pm_domain field points to the domain member of an object of type
1049 * struct generic_pm_domain representing a power domain consisting of I/O
1050 * devices.
1051 */
1052static int pm_genpd_thaw_early(struct device *dev)
1053{
1054 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001055
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001056 dev_dbg(dev, "%s()\n", __func__);
1057
1058 genpd = dev_to_genpd(dev);
1059 if (IS_ERR(genpd))
1060 return -EINVAL;
1061
1062 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001063}
1064
1065/**
1066 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1067 * @dev: Device to thaw.
1068 *
1069 * Thaw a device under the assumption that its pm_domain field points to the
1070 * domain member of an object of type struct generic_pm_domain representing
1071 * a power domain consisting of I/O devices.
1072 */
1073static int pm_genpd_thaw(struct device *dev)
1074{
1075 struct generic_pm_domain *genpd;
1076
1077 dev_dbg(dev, "%s()\n", __func__);
1078
1079 genpd = dev_to_genpd(dev);
1080 if (IS_ERR(genpd))
1081 return -EINVAL;
1082
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001083 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001084}
1085
1086/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001087 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001088 * @dev: Device to resume.
1089 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001090 * Make sure the domain will be in the same power state as before the
1091 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001092 */
1093static int pm_genpd_restore_noirq(struct device *dev)
1094{
1095 struct generic_pm_domain *genpd;
1096
1097 dev_dbg(dev, "%s()\n", __func__);
1098
1099 genpd = dev_to_genpd(dev);
1100 if (IS_ERR(genpd))
1101 return -EINVAL;
1102
1103 /*
1104 * Since all of the "noirq" callbacks are executed sequentially, it is
1105 * guaranteed that this function will never run twice in parallel for
1106 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001107 *
1108 * At this point suspended_count == 0 means we are being run for the
1109 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001110 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001111 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001112 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001113 * The boot kernel might put the domain into arbitrary state,
1114 * so make it appear as powered off to pm_genpd_poweron(), so
1115 * that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001116 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001117 genpd->status = GPD_STATE_POWER_OFF;
1118 if (genpd->suspend_power_off) {
1119 /*
1120 * If the domain was off before the hibernation, make
1121 * sure it will be off going forward.
1122 */
1123 if (genpd->power_off)
1124 genpd->power_off(genpd);
1125
1126 return 0;
1127 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001128 }
1129
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001130 if (genpd->suspend_power_off)
1131 return 0;
1132
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001133 pm_genpd_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001134
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001135 return dev_gpd_data(dev)->always_on ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001136}
1137
1138/**
1139 * pm_genpd_complete - Complete power transition of a device in a power domain.
1140 * @dev: Device to complete the transition of.
1141 *
1142 * Complete a power transition of a device (during a system-wide power
1143 * transition) under the assumption that its pm_domain field points to the
1144 * domain member of an object of type struct generic_pm_domain representing
1145 * a power domain consisting of I/O devices.
1146 */
1147static void pm_genpd_complete(struct device *dev)
1148{
1149 struct generic_pm_domain *genpd;
1150 bool run_complete;
1151
1152 dev_dbg(dev, "%s()\n", __func__);
1153
1154 genpd = dev_to_genpd(dev);
1155 if (IS_ERR(genpd))
1156 return;
1157
1158 mutex_lock(&genpd->lock);
1159
1160 run_complete = !genpd->suspend_power_off;
1161 if (--genpd->prepared_count == 0)
1162 genpd->suspend_power_off = false;
1163
1164 mutex_unlock(&genpd->lock);
1165
1166 if (run_complete) {
1167 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001168 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001169 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001170 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001171 }
1172}
1173
1174#else
1175
1176#define pm_genpd_prepare NULL
1177#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001178#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001179#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001180#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001181#define pm_genpd_resume_noirq NULL
1182#define pm_genpd_resume NULL
1183#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001184#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001185#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001186#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001187#define pm_genpd_thaw_noirq NULL
1188#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001189#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001190#define pm_genpd_complete NULL
1191
1192#endif /* CONFIG_PM_SLEEP */
1193
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001194/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001195 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001196 * @genpd: PM domain to add the device to.
1197 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001198 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001199 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001200int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1201 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001202{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001203 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001204 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001205 int ret = 0;
1206
1207 dev_dbg(dev, "%s()\n", __func__);
1208
1209 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1210 return -EINVAL;
1211
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001212 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001213
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001214 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001215 ret = -EINVAL;
1216 goto out;
1217 }
1218
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001219 if (genpd->prepared_count > 0) {
1220 ret = -EAGAIN;
1221 goto out;
1222 }
1223
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001224 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1225 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001226 ret = -EINVAL;
1227 goto out;
1228 }
1229
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001230 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1231 if (!gpd_data) {
1232 ret = -ENOMEM;
1233 goto out;
1234 }
1235
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001236 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001237
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001238 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001239 dev_pm_get_subsys_data(dev);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001240 dev->power.subsys_data->domain_data = &gpd_data->base;
1241 gpd_data->base.dev = dev;
1242 gpd_data->need_restore = false;
1243 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001244 if (td)
1245 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001246
1247 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001248 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001249
1250 return ret;
1251}
1252
1253/**
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001254 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1255 * @genpd_node: Device tree node pointer representing a PM domain to which the
1256 * the device is added to.
1257 * @dev: Device to be added.
1258 * @td: Set of PM QoS timing parameters to attach to the device.
1259 */
1260int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1261 struct gpd_timing_data *td)
1262{
1263 struct generic_pm_domain *genpd = NULL, *gpd;
1264
1265 dev_dbg(dev, "%s()\n", __func__);
1266
1267 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1268 return -EINVAL;
1269
1270 mutex_lock(&gpd_list_lock);
1271 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1272 if (gpd->of_node == genpd_node) {
1273 genpd = gpd;
1274 break;
1275 }
1276 }
1277 mutex_unlock(&gpd_list_lock);
1278
1279 if (!genpd)
1280 return -EINVAL;
1281
1282 return __pm_genpd_add_device(genpd, dev, td);
1283}
1284
1285/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001286 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1287 * @genpd: PM domain to remove the device from.
1288 * @dev: Device to be removed.
1289 */
1290int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1291 struct device *dev)
1292{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001293 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001294 int ret = -EINVAL;
1295
1296 dev_dbg(dev, "%s()\n", __func__);
1297
1298 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1299 return -EINVAL;
1300
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001301 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001302
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001303 if (genpd->prepared_count > 0) {
1304 ret = -EAGAIN;
1305 goto out;
1306 }
1307
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001308 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
1309 if (pdd->dev != dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001310 continue;
1311
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001312 list_del_init(&pdd->list_node);
1313 pdd->dev = NULL;
1314 dev_pm_put_subsys_data(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001315 dev->pm_domain = NULL;
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +02001316 kfree(to_gpd_data(pdd));
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001317
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001318 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001319
1320 ret = 0;
1321 break;
1322 }
1323
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001324 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001325 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001326
1327 return ret;
1328}
1329
1330/**
Rafael J. Wysocki1e78a0c2012-03-13 22:39:48 +01001331 * pm_genpd_dev_always_on - Set/unset the "always on" flag for a given device.
1332 * @dev: Device to set/unset the flag for.
1333 * @val: The new value of the device's "always on" flag.
1334 */
1335void pm_genpd_dev_always_on(struct device *dev, bool val)
1336{
1337 struct pm_subsys_data *psd;
1338 unsigned long flags;
1339
1340 spin_lock_irqsave(&dev->power.lock, flags);
1341
1342 psd = dev_to_psd(dev);
1343 if (psd && psd->domain_data)
1344 to_gpd_data(psd->domain_data)->always_on = val;
1345
1346 spin_unlock_irqrestore(&dev->power.lock, flags);
1347}
1348EXPORT_SYMBOL_GPL(pm_genpd_dev_always_on);
1349
1350/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001351 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1352 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001353 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001354 */
1355int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001356 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001357{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001358 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001359 int ret = 0;
1360
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001361 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001362 return -EINVAL;
1363
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001364 start:
1365 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001366 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001367
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001368 if (subdomain->status != GPD_STATE_POWER_OFF
1369 && subdomain->status != GPD_STATE_ACTIVE) {
1370 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001371 genpd_release_lock(genpd);
1372 goto start;
1373 }
1374
1375 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001376 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001377 ret = -EINVAL;
1378 goto out;
1379 }
1380
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001381 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001382 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001383 ret = -EINVAL;
1384 goto out;
1385 }
1386 }
1387
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001388 link = kzalloc(sizeof(*link), GFP_KERNEL);
1389 if (!link) {
1390 ret = -ENOMEM;
1391 goto out;
1392 }
1393 link->master = genpd;
1394 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001395 link->slave = subdomain;
1396 list_add_tail(&link->slave_node, &subdomain->slave_links);
1397 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001398 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001399
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001400 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001401 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001402 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001403
1404 return ret;
1405}
1406
1407/**
1408 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1409 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001410 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001411 */
1412int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001413 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001414{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001415 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001416 int ret = -EINVAL;
1417
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001418 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001419 return -EINVAL;
1420
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001421 start:
1422 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001423
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001424 list_for_each_entry(link, &genpd->master_links, master_node) {
1425 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001426 continue;
1427
1428 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1429
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001430 if (subdomain->status != GPD_STATE_POWER_OFF
1431 && subdomain->status != GPD_STATE_ACTIVE) {
1432 mutex_unlock(&subdomain->lock);
1433 genpd_release_lock(genpd);
1434 goto start;
1435 }
1436
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001437 list_del(&link->master_node);
1438 list_del(&link->slave_node);
1439 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001440 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001441 genpd_sd_counter_dec(genpd);
1442
1443 mutex_unlock(&subdomain->lock);
1444
1445 ret = 0;
1446 break;
1447 }
1448
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001449 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001450
1451 return ret;
1452}
1453
1454/**
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001455 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1456 * @dev: Device to add the callbacks to.
1457 * @ops: Set of callbacks to add.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001458 * @td: Timing data to add to the device along with the callbacks (optional).
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001459 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001460int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1461 struct gpd_timing_data *td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001462{
1463 struct pm_domain_data *pdd;
1464 int ret = 0;
1465
1466 if (!(dev && dev->power.subsys_data && ops))
1467 return -EINVAL;
1468
1469 pm_runtime_disable(dev);
1470 device_pm_lock();
1471
1472 pdd = dev->power.subsys_data->domain_data;
1473 if (pdd) {
1474 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1475
1476 gpd_data->ops = *ops;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001477 if (td)
1478 gpd_data->td = *td;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001479 } else {
1480 ret = -EINVAL;
1481 }
1482
1483 device_pm_unlock();
1484 pm_runtime_enable(dev);
1485
1486 return ret;
1487}
1488EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1489
1490/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001491 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001492 * @dev: Device to remove the callbacks from.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001493 * @clear_td: If set, clear the device's timing data too.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001494 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001495int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001496{
1497 struct pm_domain_data *pdd;
1498 int ret = 0;
1499
1500 if (!(dev && dev->power.subsys_data))
1501 return -EINVAL;
1502
1503 pm_runtime_disable(dev);
1504 device_pm_lock();
1505
1506 pdd = dev->power.subsys_data->domain_data;
1507 if (pdd) {
1508 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1509
1510 gpd_data->ops = (struct gpd_dev_ops){ 0 };
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001511 if (clear_td)
1512 gpd_data->td = (struct gpd_timing_data){ 0 };
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001513 } else {
1514 ret = -EINVAL;
1515 }
1516
1517 device_pm_unlock();
1518 pm_runtime_enable(dev);
1519
1520 return ret;
1521}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001522EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001523
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001524/* Default device callbacks for generic PM domains. */
1525
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001526/**
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001527 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1528 * @dev: Device to handle.
1529 */
1530static int pm_genpd_default_save_state(struct device *dev)
1531{
1532 int (*cb)(struct device *__dev);
1533 struct device_driver *drv = dev->driver;
1534
1535 cb = dev_gpd_data(dev)->ops.save_state;
1536 if (cb)
1537 return cb(dev);
1538
1539 if (drv && drv->pm && drv->pm->runtime_suspend)
1540 return drv->pm->runtime_suspend(dev);
1541
1542 return 0;
1543}
1544
1545/**
1546 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1547 * @dev: Device to handle.
1548 */
1549static int pm_genpd_default_restore_state(struct device *dev)
1550{
1551 int (*cb)(struct device *__dev);
1552 struct device_driver *drv = dev->driver;
1553
1554 cb = dev_gpd_data(dev)->ops.restore_state;
1555 if (cb)
1556 return cb(dev);
1557
1558 if (drv && drv->pm && drv->pm->runtime_resume)
1559 return drv->pm->runtime_resume(dev);
1560
1561 return 0;
1562}
1563
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001564#ifdef CONFIG_PM_SLEEP
1565
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001566/**
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001567 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1568 * @dev: Device to handle.
1569 */
1570static int pm_genpd_default_suspend(struct device *dev)
1571{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001572 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001573
1574 return cb ? cb(dev) : pm_generic_suspend(dev);
1575}
1576
1577/**
1578 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1579 * @dev: Device to handle.
1580 */
1581static int pm_genpd_default_suspend_late(struct device *dev)
1582{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001583 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001584
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001585 return cb ? cb(dev) : pm_generic_suspend_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001586}
1587
1588/**
1589 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1590 * @dev: Device to handle.
1591 */
1592static int pm_genpd_default_resume_early(struct device *dev)
1593{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001594 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001595
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001596 return cb ? cb(dev) : pm_generic_resume_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001597}
1598
1599/**
1600 * pm_genpd_default_resume - Default "device resume" for PM domians.
1601 * @dev: Device to handle.
1602 */
1603static int pm_genpd_default_resume(struct device *dev)
1604{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001605 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001606
1607 return cb ? cb(dev) : pm_generic_resume(dev);
1608}
1609
1610/**
1611 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1612 * @dev: Device to handle.
1613 */
1614static int pm_genpd_default_freeze(struct device *dev)
1615{
1616 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1617
1618 return cb ? cb(dev) : pm_generic_freeze(dev);
1619}
1620
1621/**
1622 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1623 * @dev: Device to handle.
1624 */
1625static int pm_genpd_default_freeze_late(struct device *dev)
1626{
1627 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1628
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001629 return cb ? cb(dev) : pm_generic_freeze_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001630}
1631
1632/**
1633 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1634 * @dev: Device to handle.
1635 */
1636static int pm_genpd_default_thaw_early(struct device *dev)
1637{
1638 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1639
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001640 return cb ? cb(dev) : pm_generic_thaw_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001641}
1642
1643/**
1644 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1645 * @dev: Device to handle.
1646 */
1647static int pm_genpd_default_thaw(struct device *dev)
1648{
1649 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1650
1651 return cb ? cb(dev) : pm_generic_thaw(dev);
1652}
1653
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01001654#else /* !CONFIG_PM_SLEEP */
1655
1656#define pm_genpd_default_suspend NULL
1657#define pm_genpd_default_suspend_late NULL
1658#define pm_genpd_default_resume_early NULL
1659#define pm_genpd_default_resume NULL
1660#define pm_genpd_default_freeze NULL
1661#define pm_genpd_default_freeze_late NULL
1662#define pm_genpd_default_thaw_early NULL
1663#define pm_genpd_default_thaw NULL
1664
1665#endif /* !CONFIG_PM_SLEEP */
1666
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001667/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001668 * pm_genpd_init - Initialize a generic I/O PM domain object.
1669 * @genpd: PM domain object to initialize.
1670 * @gov: PM domain governor to associate with the domain (may be NULL).
1671 * @is_off: Initial value of the domain's power_is_off field.
1672 */
1673void pm_genpd_init(struct generic_pm_domain *genpd,
1674 struct dev_power_governor *gov, bool is_off)
1675{
1676 if (IS_ERR_OR_NULL(genpd))
1677 return;
1678
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001679 INIT_LIST_HEAD(&genpd->master_links);
1680 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001681 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001682 mutex_init(&genpd->lock);
1683 genpd->gov = gov;
1684 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1685 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001686 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001687 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1688 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001689 genpd->poweroff_task = NULL;
1690 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001691 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001692 genpd->max_off_time_ns = -1;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001693 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1694 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1695 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001696 genpd->domain.ops.prepare = pm_genpd_prepare;
1697 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001698 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001699 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1700 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001701 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001702 genpd->domain.ops.resume = pm_genpd_resume;
1703 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001704 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001705 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1706 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001707 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001708 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001709 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001710 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001711 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001712 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001713 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001714 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001715 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001716 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1717 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Rafael J. Wysockic9914852011-12-06 23:16:47 +01001718 genpd->dev_ops.suspend = pm_genpd_default_suspend;
1719 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
1720 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
1721 genpd->dev_ops.resume = pm_genpd_default_resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001722 genpd->dev_ops.freeze = pm_genpd_default_freeze;
1723 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
1724 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
1725 genpd->dev_ops.thaw = pm_genpd_default_thaw;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001726 mutex_lock(&gpd_list_lock);
1727 list_add(&genpd->gpd_list_node, &gpd_list);
1728 mutex_unlock(&gpd_list_lock);
1729}