blob: 1f4b1326c6a9e78c4723d85d7a31251169065c49 [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. Wysockif7218892011-07-01 22:12:45 +020018
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020019static LIST_HEAD(gpd_list);
20static DEFINE_MUTEX(gpd_list_lock);
21
Rafael J. Wysocki52480512011-07-01 22:13:10 +020022#ifdef CONFIG_PM
23
24static struct generic_pm_domain *dev_to_genpd(struct device *dev)
25{
26 if (IS_ERR_OR_NULL(dev->pm_domain))
27 return ERR_PTR(-EINVAL);
28
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020029 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020030}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020031
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020032static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020033{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020034 bool ret = false;
35
36 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
37 ret = !!atomic_dec_and_test(&genpd->sd_count);
38
39 return ret;
40}
41
42static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
43{
44 atomic_inc(&genpd->sd_count);
45 smp_mb__after_atomic_inc();
Rafael J. Wysockif7218892011-07-01 22:12:45 +020046}
47
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020048static void genpd_acquire_lock(struct generic_pm_domain *genpd)
49{
50 DEFINE_WAIT(wait);
51
52 mutex_lock(&genpd->lock);
53 /*
54 * Wait for the domain to transition into either the active,
55 * or the power off state.
56 */
57 for (;;) {
58 prepare_to_wait(&genpd->status_wait_queue, &wait,
59 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020060 if (genpd->status == GPD_STATE_ACTIVE
61 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020062 break;
63 mutex_unlock(&genpd->lock);
64
65 schedule();
66
67 mutex_lock(&genpd->lock);
68 }
69 finish_wait(&genpd->status_wait_queue, &wait);
70}
71
72static void genpd_release_lock(struct generic_pm_domain *genpd)
73{
74 mutex_unlock(&genpd->lock);
75}
76
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020077static void genpd_set_active(struct generic_pm_domain *genpd)
78{
79 if (genpd->resume_count == 0)
80 genpd->status = GPD_STATE_ACTIVE;
81}
82
Rafael J. Wysockif7218892011-07-01 22:12:45 +020083/**
Rafael J. Wysocki3f241772011-08-08 23:43:29 +020084 * __pm_genpd_poweron - Restore power to a given PM domain and its parents.
Rafael J. Wysocki52480512011-07-01 22:13:10 +020085 * @genpd: PM domain to power up.
86 *
87 * Restore power to @genpd and all of its parents so that it is possible to
88 * resume a device belonging to it.
89 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +020090int __pm_genpd_poweron(struct generic_pm_domain *genpd)
91 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020092{
Rafael J. Wysocki3f241772011-08-08 23:43:29 +020093 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020094 int ret = 0;
95
Rafael J. Wysocki3f241772011-08-08 23:43:29 +020096 /* If the domain's parent is being waited for, we have to wait too. */
97 for (;;) {
98 prepare_to_wait(&genpd->status_wait_queue, &wait,
99 TASK_UNINTERRUPTIBLE);
100 if (genpd->status != GPD_STATE_WAIT_PARENT)
101 break;
102 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200103
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200104 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200105
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200106 mutex_lock(&genpd->lock);
107 }
108 finish_wait(&genpd->status_wait_queue, &wait);
109
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200110 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200111 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200112 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200113
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200114 if (genpd->status != GPD_STATE_POWER_OFF) {
115 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200116 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200117 }
118
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200119 if (genpd->parent) {
120 genpd_sd_counter_inc(genpd->parent);
121 genpd->status = GPD_STATE_WAIT_PARENT;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200122
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200123 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200124
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200125 ret = pm_genpd_poweron(genpd->parent);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200126
127 mutex_lock(&genpd->lock);
128
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200129 /*
130 * The "wait for parent" status is guaranteed not to change
131 * while the parent is powering on.
132 */
133 genpd->status = GPD_STATE_POWER_OFF;
134 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200135 if (ret)
136 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200137 }
138
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200139 if (genpd->power_on) {
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200140 ret = genpd->power_on(genpd);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200141 if (ret)
142 goto err;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200143 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200144
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200145 genpd_set_active(genpd);
146
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200147 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200148
149 err:
150 if (genpd->parent)
151 genpd_sd_counter_dec(genpd->parent);
152
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200153 return ret;
154}
155
156/**
157 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
158 * @genpd: PM domain to power up.
159 */
160int pm_genpd_poweron(struct generic_pm_domain *genpd)
161{
162 int ret;
163
164 mutex_lock(&genpd->lock);
165 ret = __pm_genpd_poweron(genpd);
166 mutex_unlock(&genpd->lock);
167 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200168}
169
170#endif /* CONFIG_PM */
171
172#ifdef CONFIG_PM_RUNTIME
173
174/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200175 * __pm_genpd_save_device - Save the pre-suspend state of a device.
176 * @dle: Device list entry of the device to save the state of.
177 * @genpd: PM domain the device belongs to.
178 */
179static int __pm_genpd_save_device(struct dev_list_entry *dle,
180 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200181 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200182{
183 struct device *dev = dle->dev;
184 struct device_driver *drv = dev->driver;
185 int ret = 0;
186
187 if (dle->need_restore)
188 return 0;
189
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200190 mutex_unlock(&genpd->lock);
191
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200192 if (drv && drv->pm && drv->pm->runtime_suspend) {
193 if (genpd->start_device)
194 genpd->start_device(dev);
195
196 ret = drv->pm->runtime_suspend(dev);
197
198 if (genpd->stop_device)
199 genpd->stop_device(dev);
200 }
201
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200202 mutex_lock(&genpd->lock);
203
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200204 if (!ret)
205 dle->need_restore = true;
206
207 return ret;
208}
209
210/**
211 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
212 * @dle: Device list entry of the device to restore the state of.
213 * @genpd: PM domain the device belongs to.
214 */
215static void __pm_genpd_restore_device(struct dev_list_entry *dle,
216 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200217 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200218{
219 struct device *dev = dle->dev;
220 struct device_driver *drv = dev->driver;
221
222 if (!dle->need_restore)
223 return;
224
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200225 mutex_unlock(&genpd->lock);
226
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200227 if (drv && drv->pm && drv->pm->runtime_resume) {
228 if (genpd->start_device)
229 genpd->start_device(dev);
230
231 drv->pm->runtime_resume(dev);
232
233 if (genpd->stop_device)
234 genpd->stop_device(dev);
235 }
236
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200237 mutex_lock(&genpd->lock);
238
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200239 dle->need_restore = false;
240}
241
242/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200243 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
244 * @genpd: PM domain to check.
245 *
246 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
247 * a "power off" operation, which means that a "power on" has occured in the
248 * meantime, or if its resume_count field is different from zero, which means
249 * that one of its devices has been resumed in the meantime.
250 */
251static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
252{
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200253 return genpd->status == GPD_STATE_WAIT_PARENT
254 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200255}
256
257/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200258 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
259 * @genpd: PM domait to power off.
260 *
261 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
262 * before.
263 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200264void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200265{
266 if (!work_pending(&genpd->power_off_work))
267 queue_work(pm_wq, &genpd->power_off_work);
268}
269
270/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200271 * pm_genpd_poweroff - Remove power from a given PM domain.
272 * @genpd: PM domain to power down.
273 *
274 * If all of the @genpd's devices have been suspended and all of its subdomains
275 * have been powered down, run the runtime suspend callbacks provided by all of
276 * the @genpd's devices' drivers and remove power from @genpd.
277 */
278static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200279 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200280{
281 struct generic_pm_domain *parent;
282 struct dev_list_entry *dle;
283 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200284 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200285
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200286 start:
287 /*
288 * Do not try to power off the domain in the following situations:
289 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200290 * (2) The domain is waiting for its parent to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200291 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200292 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200293 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200294 if (genpd->status == GPD_STATE_POWER_OFF
295 || genpd->status == GPD_STATE_WAIT_PARENT
296 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200297 return 0;
298
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200299 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200300 return -EBUSY;
301
302 not_suspended = 0;
303 list_for_each_entry(dle, &genpd->dev_list, node)
304 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
305 not_suspended++;
306
307 if (not_suspended > genpd->in_progress)
308 return -EBUSY;
309
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200310 if (genpd->poweroff_task) {
311 /*
312 * Another instance of pm_genpd_poweroff() is executing
313 * callbacks, so tell it to start over and return.
314 */
315 genpd->status = GPD_STATE_REPEAT;
316 return 0;
317 }
318
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200319 if (genpd->gov && genpd->gov->power_down_ok) {
320 if (!genpd->gov->power_down_ok(&genpd->domain))
321 return -EAGAIN;
322 }
323
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200324 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200325 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200326
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200327 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200328 ret = atomic_read(&genpd->sd_count) == 0 ?
329 __pm_genpd_save_device(dle, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200330
331 if (genpd_abort_poweroff(genpd))
332 goto out;
333
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200334 if (ret) {
335 genpd_set_active(genpd);
336 goto out;
337 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200338
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200339 if (genpd->status == GPD_STATE_REPEAT) {
340 genpd->poweroff_task = NULL;
341 goto start;
342 }
343 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200344
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200345 if (genpd->power_off) {
346 if (atomic_read(&genpd->sd_count) > 0) {
347 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200348 goto out;
349 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200350
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200351 /*
352 * If sd_count > 0 at this point, one of the children hasn't
353 * managed to call pm_genpd_poweron() for the parent yet after
354 * incrementing it. In that case pm_genpd_poweron() will wait
355 * for us to drop the lock, so we can call .power_off() and let
356 * the pm_genpd_poweron() restore power for us (this shouldn't
357 * happen very often).
358 */
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200359 ret = genpd->power_off(genpd);
360 if (ret == -EBUSY) {
361 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200362 goto out;
363 }
364 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200365
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200366 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200367
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200368 parent = genpd->parent;
369 if (parent && genpd_sd_counter_dec(parent))
370 genpd_queue_power_off_work(parent);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200371
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200372 out:
373 genpd->poweroff_task = NULL;
374 wake_up_all(&genpd->status_wait_queue);
375 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200376}
377
378/**
379 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
380 * @work: Work structure used for scheduling the execution of this function.
381 */
382static void genpd_power_off_work_fn(struct work_struct *work)
383{
384 struct generic_pm_domain *genpd;
385
386 genpd = container_of(work, struct generic_pm_domain, power_off_work);
387
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200388 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200389 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200390 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200391}
392
393/**
394 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
395 * @dev: Device to suspend.
396 *
397 * Carry out a runtime suspend of a device under the assumption that its
398 * pm_domain field points to the domain member of an object of type
399 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
400 */
401static int pm_genpd_runtime_suspend(struct device *dev)
402{
403 struct generic_pm_domain *genpd;
404
405 dev_dbg(dev, "%s()\n", __func__);
406
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200407 genpd = dev_to_genpd(dev);
408 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200409 return -EINVAL;
410
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200411 if (genpd->stop_device) {
412 int ret = genpd->stop_device(dev);
413 if (ret)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200414 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200415 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200416
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200417 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200418 genpd->in_progress++;
419 pm_genpd_poweroff(genpd);
420 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200421 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200422
423 return 0;
424}
425
426/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200427 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
428 * @dev: Device to resume.
429 * @genpd: PM domain the device belongs to.
430 */
431static void __pm_genpd_runtime_resume(struct device *dev,
432 struct generic_pm_domain *genpd)
433{
434 struct dev_list_entry *dle;
435
436 list_for_each_entry(dle, &genpd->dev_list, node) {
437 if (dle->dev == dev) {
438 __pm_genpd_restore_device(dle, genpd);
439 break;
440 }
441 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200442}
443
444/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200445 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
446 * @dev: Device to resume.
447 *
448 * Carry out a runtime resume of a device under the assumption that its
449 * pm_domain field points to the domain member of an object of type
450 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
451 */
452static int pm_genpd_runtime_resume(struct device *dev)
453{
454 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200455 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200456 int ret;
457
458 dev_dbg(dev, "%s()\n", __func__);
459
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200460 genpd = dev_to_genpd(dev);
461 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200462 return -EINVAL;
463
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200464 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200465 ret = __pm_genpd_poweron(genpd);
466 if (ret) {
467 mutex_unlock(&genpd->lock);
468 return ret;
469 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200470 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200471 genpd->resume_count++;
472 for (;;) {
473 prepare_to_wait(&genpd->status_wait_queue, &wait,
474 TASK_UNINTERRUPTIBLE);
475 /*
476 * If current is the powering off task, we have been called
477 * reentrantly from one of the device callbacks, so we should
478 * not wait.
479 */
480 if (!genpd->poweroff_task || genpd->poweroff_task == current)
481 break;
482 mutex_unlock(&genpd->lock);
483
484 schedule();
485
486 mutex_lock(&genpd->lock);
487 }
488 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200489 __pm_genpd_runtime_resume(dev, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200490 genpd->resume_count--;
491 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200492 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200493 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200494
495 if (genpd->start_device)
496 genpd->start_device(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200497
498 return 0;
499}
500
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200501/**
502 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
503 */
504void pm_genpd_poweroff_unused(void)
505{
506 struct generic_pm_domain *genpd;
507
508 mutex_lock(&gpd_list_lock);
509
510 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
511 genpd_queue_power_off_work(genpd);
512
513 mutex_unlock(&gpd_list_lock);
514}
515
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200516#else
517
518static inline void genpd_power_off_work_fn(struct work_struct *work) {}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200519static inline void __pm_genpd_runtime_resume(struct device *dev,
520 struct generic_pm_domain *genpd) {}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200521
522#define pm_genpd_runtime_suspend NULL
523#define pm_genpd_runtime_resume NULL
524
525#endif /* CONFIG_PM_RUNTIME */
526
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200527#ifdef CONFIG_PM_SLEEP
528
529/**
530 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
531 * @genpd: PM domain to power off, if possible.
532 *
533 * Check if the given PM domain can be powered off (during system suspend or
534 * hibernation) and do that if so. Also, in that case propagate to its parent.
535 *
536 * This function is only called in "noirq" stages of system power transitions,
537 * so it need not acquire locks (all of the "noirq" callbacks are executed
538 * sequentially, so it is guaranteed that it will never run twice in parallel).
539 */
540static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
541{
542 struct generic_pm_domain *parent = genpd->parent;
543
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200544 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200545 return;
546
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200547 if (genpd->suspended_count != genpd->device_count
548 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200549 return;
550
551 if (genpd->power_off)
552 genpd->power_off(genpd);
553
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200554 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200555 if (parent) {
556 genpd_sd_counter_dec(parent);
557 pm_genpd_sync_poweroff(parent);
558 }
559}
560
561/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200562 * resume_needed - Check whether to resume a device before system suspend.
563 * @dev: Device to check.
564 * @genpd: PM domain the device belongs to.
565 *
566 * There are two cases in which a device that can wake up the system from sleep
567 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
568 * to wake up the system and it has to remain active for this purpose while the
569 * system is in the sleep state and (2) if the device is not enabled to wake up
570 * the system from sleep states and it generally doesn't generate wakeup signals
571 * by itself (those signals are generated on its behalf by other parts of the
572 * system). In the latter case it may be necessary to reconfigure the device's
573 * wakeup settings during system suspend, because it may have been set up to
574 * signal remote wakeup from the system's working state as needed by runtime PM.
575 * Return 'true' in either of the above cases.
576 */
577static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
578{
579 bool active_wakeup;
580
581 if (!device_can_wakeup(dev))
582 return false;
583
584 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
585 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
586}
587
588/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200589 * pm_genpd_prepare - Start power transition of a device in a PM domain.
590 * @dev: Device to start the transition of.
591 *
592 * Start a power transition of a device (during a system-wide power transition)
593 * under the assumption that its pm_domain field points to the domain member of
594 * an object of type struct generic_pm_domain representing a PM domain
595 * consisting of I/O devices.
596 */
597static int pm_genpd_prepare(struct device *dev)
598{
599 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200600 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200601
602 dev_dbg(dev, "%s()\n", __func__);
603
604 genpd = dev_to_genpd(dev);
605 if (IS_ERR(genpd))
606 return -EINVAL;
607
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200608 /*
609 * If a wakeup request is pending for the device, it should be woken up
610 * at this point and a system wakeup event should be reported if it's
611 * set up to wake up the system from sleep states.
612 */
613 pm_runtime_get_noresume(dev);
614 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
615 pm_wakeup_event(dev, 0);
616
617 if (pm_wakeup_pending()) {
618 pm_runtime_put_sync(dev);
619 return -EBUSY;
620 }
621
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200622 if (resume_needed(dev, genpd))
623 pm_runtime_resume(dev);
624
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200625 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200626
627 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200628 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
629
630 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200631
632 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200633 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200634 return 0;
635 }
636
637 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200638 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
639 * so pm_genpd_poweron() will return immediately, but if the device
640 * is suspended (e.g. it's been stopped by .stop_device()), we need
641 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200642 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200643 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200644 __pm_runtime_disable(dev, false);
645
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200646 ret = pm_generic_prepare(dev);
647 if (ret) {
648 mutex_lock(&genpd->lock);
649
650 if (--genpd->prepared_count == 0)
651 genpd->suspend_power_off = false;
652
653 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200654 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200655 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200656
657 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200658 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200659}
660
661/**
662 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
663 * @dev: Device to suspend.
664 *
665 * Suspend a device under the assumption that its pm_domain field points to the
666 * domain member of an object of type struct generic_pm_domain representing
667 * a PM domain consisting of I/O devices.
668 */
669static int pm_genpd_suspend(struct device *dev)
670{
671 struct generic_pm_domain *genpd;
672
673 dev_dbg(dev, "%s()\n", __func__);
674
675 genpd = dev_to_genpd(dev);
676 if (IS_ERR(genpd))
677 return -EINVAL;
678
679 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
680}
681
682/**
683 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
684 * @dev: Device to suspend.
685 *
686 * Carry out a late suspend of a device under the assumption that its
687 * pm_domain field points to the domain member of an object of type
688 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
689 */
690static int pm_genpd_suspend_noirq(struct device *dev)
691{
692 struct generic_pm_domain *genpd;
693 int ret;
694
695 dev_dbg(dev, "%s()\n", __func__);
696
697 genpd = dev_to_genpd(dev);
698 if (IS_ERR(genpd))
699 return -EINVAL;
700
701 if (genpd->suspend_power_off)
702 return 0;
703
704 ret = pm_generic_suspend_noirq(dev);
705 if (ret)
706 return ret;
707
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200708 if (device_may_wakeup(dev)
709 && genpd->active_wakeup && genpd->active_wakeup(dev))
710 return 0;
711
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200712 if (genpd->stop_device)
713 genpd->stop_device(dev);
714
715 /*
716 * Since all of the "noirq" callbacks are executed sequentially, it is
717 * guaranteed that this function will never run twice in parallel for
718 * the same PM domain, so it is not necessary to use locking here.
719 */
720 genpd->suspended_count++;
721 pm_genpd_sync_poweroff(genpd);
722
723 return 0;
724}
725
726/**
727 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
728 * @dev: Device to resume.
729 *
730 * Carry out an early resume of a device under the assumption that its
731 * pm_domain field points to the domain member of an object of type
732 * struct generic_pm_domain representing a power domain consisting of I/O
733 * devices.
734 */
735static int pm_genpd_resume_noirq(struct device *dev)
736{
737 struct generic_pm_domain *genpd;
738
739 dev_dbg(dev, "%s()\n", __func__);
740
741 genpd = dev_to_genpd(dev);
742 if (IS_ERR(genpd))
743 return -EINVAL;
744
745 if (genpd->suspend_power_off)
746 return 0;
747
748 /*
749 * Since all of the "noirq" callbacks are executed sequentially, it is
750 * guaranteed that this function will never run twice in parallel for
751 * the same PM domain, so it is not necessary to use locking here.
752 */
753 pm_genpd_poweron(genpd);
754 genpd->suspended_count--;
755 if (genpd->start_device)
756 genpd->start_device(dev);
757
758 return pm_generic_resume_noirq(dev);
759}
760
761/**
762 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
763 * @dev: Device to resume.
764 *
765 * Resume a device under the assumption that its pm_domain field points to the
766 * domain member of an object of type struct generic_pm_domain representing
767 * a power domain consisting of I/O devices.
768 */
769static int pm_genpd_resume(struct device *dev)
770{
771 struct generic_pm_domain *genpd;
772
773 dev_dbg(dev, "%s()\n", __func__);
774
775 genpd = dev_to_genpd(dev);
776 if (IS_ERR(genpd))
777 return -EINVAL;
778
779 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
780}
781
782/**
783 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
784 * @dev: Device to freeze.
785 *
786 * Freeze a device under the assumption that its pm_domain field points to the
787 * domain member of an object of type struct generic_pm_domain representing
788 * a power domain consisting of I/O devices.
789 */
790static int pm_genpd_freeze(struct device *dev)
791{
792 struct generic_pm_domain *genpd;
793
794 dev_dbg(dev, "%s()\n", __func__);
795
796 genpd = dev_to_genpd(dev);
797 if (IS_ERR(genpd))
798 return -EINVAL;
799
800 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
801}
802
803/**
804 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
805 * @dev: Device to freeze.
806 *
807 * Carry out a late freeze of a device under the assumption that its
808 * pm_domain field points to the domain member of an object of type
809 * struct generic_pm_domain representing a power domain consisting of I/O
810 * devices.
811 */
812static int pm_genpd_freeze_noirq(struct device *dev)
813{
814 struct generic_pm_domain *genpd;
815 int ret;
816
817 dev_dbg(dev, "%s()\n", __func__);
818
819 genpd = dev_to_genpd(dev);
820 if (IS_ERR(genpd))
821 return -EINVAL;
822
823 if (genpd->suspend_power_off)
824 return 0;
825
826 ret = pm_generic_freeze_noirq(dev);
827 if (ret)
828 return ret;
829
830 if (genpd->stop_device)
831 genpd->stop_device(dev);
832
833 return 0;
834}
835
836/**
837 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
838 * @dev: Device to thaw.
839 *
840 * Carry out an early thaw of a device under the assumption that its
841 * pm_domain field points to the domain member of an object of type
842 * struct generic_pm_domain representing a power domain consisting of I/O
843 * devices.
844 */
845static int pm_genpd_thaw_noirq(struct device *dev)
846{
847 struct generic_pm_domain *genpd;
848
849 dev_dbg(dev, "%s()\n", __func__);
850
851 genpd = dev_to_genpd(dev);
852 if (IS_ERR(genpd))
853 return -EINVAL;
854
855 if (genpd->suspend_power_off)
856 return 0;
857
858 if (genpd->start_device)
859 genpd->start_device(dev);
860
861 return pm_generic_thaw_noirq(dev);
862}
863
864/**
865 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
866 * @dev: Device to thaw.
867 *
868 * Thaw a device under the assumption that its pm_domain field points to the
869 * domain member of an object of type struct generic_pm_domain representing
870 * a power domain consisting of I/O devices.
871 */
872static int pm_genpd_thaw(struct device *dev)
873{
874 struct generic_pm_domain *genpd;
875
876 dev_dbg(dev, "%s()\n", __func__);
877
878 genpd = dev_to_genpd(dev);
879 if (IS_ERR(genpd))
880 return -EINVAL;
881
882 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
883}
884
885/**
886 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
887 * @dev: Device to suspend.
888 *
889 * Power off a device under the assumption that its pm_domain field points to
890 * the domain member of an object of type struct generic_pm_domain representing
891 * a PM domain consisting of I/O devices.
892 */
893static int pm_genpd_dev_poweroff(struct device *dev)
894{
895 struct generic_pm_domain *genpd;
896
897 dev_dbg(dev, "%s()\n", __func__);
898
899 genpd = dev_to_genpd(dev);
900 if (IS_ERR(genpd))
901 return -EINVAL;
902
903 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
904}
905
906/**
907 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
908 * @dev: Device to suspend.
909 *
910 * Carry out a late powering off of a device under the assumption that its
911 * pm_domain field points to the domain member of an object of type
912 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
913 */
914static int pm_genpd_dev_poweroff_noirq(struct device *dev)
915{
916 struct generic_pm_domain *genpd;
917 int ret;
918
919 dev_dbg(dev, "%s()\n", __func__);
920
921 genpd = dev_to_genpd(dev);
922 if (IS_ERR(genpd))
923 return -EINVAL;
924
925 if (genpd->suspend_power_off)
926 return 0;
927
928 ret = pm_generic_poweroff_noirq(dev);
929 if (ret)
930 return ret;
931
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200932 if (device_may_wakeup(dev)
933 && genpd->active_wakeup && genpd->active_wakeup(dev))
934 return 0;
935
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200936 if (genpd->stop_device)
937 genpd->stop_device(dev);
938
939 /*
940 * Since all of the "noirq" callbacks are executed sequentially, it is
941 * guaranteed that this function will never run twice in parallel for
942 * the same PM domain, so it is not necessary to use locking here.
943 */
944 genpd->suspended_count++;
945 pm_genpd_sync_poweroff(genpd);
946
947 return 0;
948}
949
950/**
951 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
952 * @dev: Device to resume.
953 *
954 * Carry out an early restore of a device under the assumption that its
955 * pm_domain field points to the domain member of an object of type
956 * struct generic_pm_domain representing a power domain consisting of I/O
957 * devices.
958 */
959static int pm_genpd_restore_noirq(struct device *dev)
960{
961 struct generic_pm_domain *genpd;
962
963 dev_dbg(dev, "%s()\n", __func__);
964
965 genpd = dev_to_genpd(dev);
966 if (IS_ERR(genpd))
967 return -EINVAL;
968
969 /*
970 * Since all of the "noirq" callbacks are executed sequentially, it is
971 * guaranteed that this function will never run twice in parallel for
972 * the same PM domain, so it is not necessary to use locking here.
973 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200974 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200975 if (genpd->suspend_power_off) {
976 /*
977 * The boot kernel might put the domain into the power on state,
978 * so make sure it really is powered off.
979 */
980 if (genpd->power_off)
981 genpd->power_off(genpd);
982 return 0;
983 }
984
985 pm_genpd_poweron(genpd);
986 genpd->suspended_count--;
987 if (genpd->start_device)
988 genpd->start_device(dev);
989
990 return pm_generic_restore_noirq(dev);
991}
992
993/**
994 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
995 * @dev: Device to resume.
996 *
997 * Restore a device under the assumption that its pm_domain field points to the
998 * domain member of an object of type struct generic_pm_domain representing
999 * a power domain consisting of I/O devices.
1000 */
1001static int pm_genpd_restore(struct device *dev)
1002{
1003 struct generic_pm_domain *genpd;
1004
1005 dev_dbg(dev, "%s()\n", __func__);
1006
1007 genpd = dev_to_genpd(dev);
1008 if (IS_ERR(genpd))
1009 return -EINVAL;
1010
1011 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
1012}
1013
1014/**
1015 * pm_genpd_complete - Complete power transition of a device in a power domain.
1016 * @dev: Device to complete the transition of.
1017 *
1018 * Complete a power transition of a device (during a system-wide power
1019 * transition) under the assumption that its pm_domain field points to the
1020 * domain member of an object of type struct generic_pm_domain representing
1021 * a power domain consisting of I/O devices.
1022 */
1023static void pm_genpd_complete(struct device *dev)
1024{
1025 struct generic_pm_domain *genpd;
1026 bool run_complete;
1027
1028 dev_dbg(dev, "%s()\n", __func__);
1029
1030 genpd = dev_to_genpd(dev);
1031 if (IS_ERR(genpd))
1032 return;
1033
1034 mutex_lock(&genpd->lock);
1035
1036 run_complete = !genpd->suspend_power_off;
1037 if (--genpd->prepared_count == 0)
1038 genpd->suspend_power_off = false;
1039
1040 mutex_unlock(&genpd->lock);
1041
1042 if (run_complete) {
1043 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001044 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001045 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001046 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001047 }
1048}
1049
1050#else
1051
1052#define pm_genpd_prepare NULL
1053#define pm_genpd_suspend NULL
1054#define pm_genpd_suspend_noirq NULL
1055#define pm_genpd_resume_noirq NULL
1056#define pm_genpd_resume NULL
1057#define pm_genpd_freeze NULL
1058#define pm_genpd_freeze_noirq NULL
1059#define pm_genpd_thaw_noirq NULL
1060#define pm_genpd_thaw NULL
1061#define pm_genpd_dev_poweroff_noirq NULL
1062#define pm_genpd_dev_poweroff NULL
1063#define pm_genpd_restore_noirq NULL
1064#define pm_genpd_restore NULL
1065#define pm_genpd_complete NULL
1066
1067#endif /* CONFIG_PM_SLEEP */
1068
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001069/**
1070 * pm_genpd_add_device - Add a device to an I/O PM domain.
1071 * @genpd: PM domain to add the device to.
1072 * @dev: Device to be added.
1073 */
1074int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1075{
1076 struct dev_list_entry *dle;
1077 int ret = 0;
1078
1079 dev_dbg(dev, "%s()\n", __func__);
1080
1081 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1082 return -EINVAL;
1083
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001084 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001085
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001086 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001087 ret = -EINVAL;
1088 goto out;
1089 }
1090
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001091 if (genpd->prepared_count > 0) {
1092 ret = -EAGAIN;
1093 goto out;
1094 }
1095
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001096 list_for_each_entry(dle, &genpd->dev_list, node)
1097 if (dle->dev == dev) {
1098 ret = -EINVAL;
1099 goto out;
1100 }
1101
1102 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1103 if (!dle) {
1104 ret = -ENOMEM;
1105 goto out;
1106 }
1107
1108 dle->dev = dev;
1109 dle->need_restore = false;
1110 list_add_tail(&dle->node, &genpd->dev_list);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001111 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001112
1113 spin_lock_irq(&dev->power.lock);
1114 dev->pm_domain = &genpd->domain;
1115 spin_unlock_irq(&dev->power.lock);
1116
1117 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001118 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001119
1120 return ret;
1121}
1122
1123/**
1124 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1125 * @genpd: PM domain to remove the device from.
1126 * @dev: Device to be removed.
1127 */
1128int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1129 struct device *dev)
1130{
1131 struct dev_list_entry *dle;
1132 int ret = -EINVAL;
1133
1134 dev_dbg(dev, "%s()\n", __func__);
1135
1136 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1137 return -EINVAL;
1138
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001139 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001140
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001141 if (genpd->prepared_count > 0) {
1142 ret = -EAGAIN;
1143 goto out;
1144 }
1145
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001146 list_for_each_entry(dle, &genpd->dev_list, node) {
1147 if (dle->dev != dev)
1148 continue;
1149
1150 spin_lock_irq(&dev->power.lock);
1151 dev->pm_domain = NULL;
1152 spin_unlock_irq(&dev->power.lock);
1153
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001154 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001155 list_del(&dle->node);
1156 kfree(dle);
1157
1158 ret = 0;
1159 break;
1160 }
1161
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001162 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001163 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001164
1165 return ret;
1166}
1167
1168/**
1169 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1170 * @genpd: Master PM domain to add the subdomain to.
1171 * @new_subdomain: Subdomain to be added.
1172 */
1173int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1174 struct generic_pm_domain *new_subdomain)
1175{
1176 struct generic_pm_domain *subdomain;
1177 int ret = 0;
1178
1179 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1180 return -EINVAL;
1181
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001182 start:
1183 genpd_acquire_lock(genpd);
1184 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001185
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001186 if (new_subdomain->status != GPD_STATE_POWER_OFF
1187 && new_subdomain->status != GPD_STATE_ACTIVE) {
1188 mutex_unlock(&new_subdomain->lock);
1189 genpd_release_lock(genpd);
1190 goto start;
1191 }
1192
1193 if (genpd->status == GPD_STATE_POWER_OFF
1194 && new_subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001195 ret = -EINVAL;
1196 goto out;
1197 }
1198
1199 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1200 if (subdomain == new_subdomain) {
1201 ret = -EINVAL;
1202 goto out;
1203 }
1204 }
1205
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001206 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1207 new_subdomain->parent = genpd;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001208 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001209 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001210
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001211 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001212 mutex_unlock(&new_subdomain->lock);
1213 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001214
1215 return ret;
1216}
1217
1218/**
1219 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1220 * @genpd: Master PM domain to remove the subdomain from.
1221 * @target: Subdomain to be removed.
1222 */
1223int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1224 struct generic_pm_domain *target)
1225{
1226 struct generic_pm_domain *subdomain;
1227 int ret = -EINVAL;
1228
1229 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1230 return -EINVAL;
1231
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001232 start:
1233 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001234
1235 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1236 if (subdomain != target)
1237 continue;
1238
1239 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1240
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001241 if (subdomain->status != GPD_STATE_POWER_OFF
1242 && subdomain->status != GPD_STATE_ACTIVE) {
1243 mutex_unlock(&subdomain->lock);
1244 genpd_release_lock(genpd);
1245 goto start;
1246 }
1247
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001248 list_del(&subdomain->sd_node);
1249 subdomain->parent = NULL;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001250 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001251 genpd_sd_counter_dec(genpd);
1252
1253 mutex_unlock(&subdomain->lock);
1254
1255 ret = 0;
1256 break;
1257 }
1258
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001259 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001260
1261 return ret;
1262}
1263
1264/**
1265 * pm_genpd_init - Initialize a generic I/O PM domain object.
1266 * @genpd: PM domain object to initialize.
1267 * @gov: PM domain governor to associate with the domain (may be NULL).
1268 * @is_off: Initial value of the domain's power_is_off field.
1269 */
1270void pm_genpd_init(struct generic_pm_domain *genpd,
1271 struct dev_power_governor *gov, bool is_off)
1272{
1273 if (IS_ERR_OR_NULL(genpd))
1274 return;
1275
1276 INIT_LIST_HEAD(&genpd->sd_node);
1277 genpd->parent = NULL;
1278 INIT_LIST_HEAD(&genpd->dev_list);
1279 INIT_LIST_HEAD(&genpd->sd_list);
1280 mutex_init(&genpd->lock);
1281 genpd->gov = gov;
1282 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1283 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001284 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001285 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1286 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001287 genpd->poweroff_task = NULL;
1288 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001289 genpd->device_count = 0;
1290 genpd->suspended_count = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001291 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1292 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1293 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001294 genpd->domain.ops.prepare = pm_genpd_prepare;
1295 genpd->domain.ops.suspend = pm_genpd_suspend;
1296 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1297 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1298 genpd->domain.ops.resume = pm_genpd_resume;
1299 genpd->domain.ops.freeze = pm_genpd_freeze;
1300 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1301 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1302 genpd->domain.ops.thaw = pm_genpd_thaw;
1303 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1304 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1305 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1306 genpd->domain.ops.restore = pm_genpd_restore;
1307 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001308 mutex_lock(&gpd_list_lock);
1309 list_add(&genpd->gpd_list_node, &gpd_list);
1310 mutex_unlock(&gpd_list_lock);
1311}