blob: 20e2b52d9c9ca874f328f24a753799f1bc9add40 [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. Wysocki52480512011-07-01 22:13:10 +020084 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
85 * @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 */
Magnus Damm18b4f3f2011-07-10 10:39:14 +020090int pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020091{
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020092 struct generic_pm_domain *parent = genpd->parent;
Rafael J. Wysocki52480512011-07-01 22:13:10 +020093 int ret = 0;
94
95 start:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020096 if (parent) {
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020097 genpd_acquire_lock(parent);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020098 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
99 } else {
100 mutex_lock(&genpd->lock);
101 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200102
103 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200104 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200105 goto out;
106
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200107 if (genpd->status != GPD_STATE_POWER_OFF) {
108 genpd_set_active(genpd);
109 goto out;
110 }
111
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200112 if (parent && parent->status != GPD_STATE_ACTIVE) {
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200113 mutex_unlock(&genpd->lock);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200114 genpd_release_lock(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200115
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200116 ret = pm_genpd_poweron(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200117 if (ret)
118 return ret;
119
120 goto start;
121 }
122
123 if (genpd->power_on) {
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200124 ret = genpd->power_on(genpd);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200125 if (ret)
126 goto out;
127 }
128
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200129 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200130 if (parent)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200131 genpd_sd_counter_inc(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200132
133 out:
134 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200135 if (parent)
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200136 genpd_release_lock(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200137
138 return ret;
139}
140
141#endif /* CONFIG_PM */
142
143#ifdef CONFIG_PM_RUNTIME
144
145/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200146 * __pm_genpd_save_device - Save the pre-suspend state of a device.
147 * @dle: Device list entry of the device to save the state of.
148 * @genpd: PM domain the device belongs to.
149 */
150static int __pm_genpd_save_device(struct dev_list_entry *dle,
151 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200152 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200153{
154 struct device *dev = dle->dev;
155 struct device_driver *drv = dev->driver;
156 int ret = 0;
157
158 if (dle->need_restore)
159 return 0;
160
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200161 mutex_unlock(&genpd->lock);
162
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200163 if (drv && drv->pm && drv->pm->runtime_suspend) {
164 if (genpd->start_device)
165 genpd->start_device(dev);
166
167 ret = drv->pm->runtime_suspend(dev);
168
169 if (genpd->stop_device)
170 genpd->stop_device(dev);
171 }
172
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200173 mutex_lock(&genpd->lock);
174
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200175 if (!ret)
176 dle->need_restore = true;
177
178 return ret;
179}
180
181/**
182 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
183 * @dle: Device list entry of the device to restore the state of.
184 * @genpd: PM domain the device belongs to.
185 */
186static void __pm_genpd_restore_device(struct dev_list_entry *dle,
187 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200188 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200189{
190 struct device *dev = dle->dev;
191 struct device_driver *drv = dev->driver;
192
193 if (!dle->need_restore)
194 return;
195
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200196 mutex_unlock(&genpd->lock);
197
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200198 if (drv && drv->pm && drv->pm->runtime_resume) {
199 if (genpd->start_device)
200 genpd->start_device(dev);
201
202 drv->pm->runtime_resume(dev);
203
204 if (genpd->stop_device)
205 genpd->stop_device(dev);
206 }
207
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200208 mutex_lock(&genpd->lock);
209
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200210 dle->need_restore = false;
211}
212
213/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200214 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
215 * @genpd: PM domain to check.
216 *
217 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
218 * a "power off" operation, which means that a "power on" has occured in the
219 * meantime, or if its resume_count field is different from zero, which means
220 * that one of its devices has been resumed in the meantime.
221 */
222static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
223{
224 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
225}
226
227/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200228 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
229 * @genpd: PM domait to power off.
230 *
231 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
232 * before.
233 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200234void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200235{
236 if (!work_pending(&genpd->power_off_work))
237 queue_work(pm_wq, &genpd->power_off_work);
238}
239
240/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200241 * pm_genpd_poweroff - Remove power from a given PM domain.
242 * @genpd: PM domain to power down.
243 *
244 * If all of the @genpd's devices have been suspended and all of its subdomains
245 * have been powered down, run the runtime suspend callbacks provided by all of
246 * the @genpd's devices' drivers and remove power from @genpd.
247 */
248static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200249 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200250{
251 struct generic_pm_domain *parent;
252 struct dev_list_entry *dle;
253 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200254 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200255
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200256 start:
257 /*
258 * Do not try to power off the domain in the following situations:
259 * (1) The domain is already in the "power off" state.
260 * (2) System suspend is in progress.
261 * (3) One of the domain's devices is being resumed right now.
262 */
263 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
264 || genpd->resume_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200265 return 0;
266
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200267 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200268 return -EBUSY;
269
270 not_suspended = 0;
271 list_for_each_entry(dle, &genpd->dev_list, node)
272 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
273 not_suspended++;
274
275 if (not_suspended > genpd->in_progress)
276 return -EBUSY;
277
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200278 if (genpd->poweroff_task) {
279 /*
280 * Another instance of pm_genpd_poweroff() is executing
281 * callbacks, so tell it to start over and return.
282 */
283 genpd->status = GPD_STATE_REPEAT;
284 return 0;
285 }
286
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200287 if (genpd->gov && genpd->gov->power_down_ok) {
288 if (!genpd->gov->power_down_ok(&genpd->domain))
289 return -EAGAIN;
290 }
291
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200292 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200293 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200294
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200295 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
296 ret = __pm_genpd_save_device(dle, genpd);
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200297 if (ret) {
298 genpd_set_active(genpd);
299 goto out;
300 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200301
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200302 if (genpd_abort_poweroff(genpd))
303 goto out;
304
305 if (genpd->status == GPD_STATE_REPEAT) {
306 genpd->poweroff_task = NULL;
307 goto start;
308 }
309 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200310
311 parent = genpd->parent;
312 if (parent) {
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200313 mutex_unlock(&genpd->lock);
314
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200315 genpd_acquire_lock(parent);
316 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200317
318 if (genpd_abort_poweroff(genpd)) {
319 genpd_release_lock(parent);
320 goto out;
321 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200322 }
323
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200324 if (genpd->power_off) {
325 ret = genpd->power_off(genpd);
326 if (ret == -EBUSY) {
327 genpd_set_active(genpd);
328 if (parent)
329 genpd_release_lock(parent);
330
331 goto out;
332 }
333 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200334
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200335 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200336
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200337 if (parent) {
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200338 if (genpd_sd_counter_dec(parent))
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200339 genpd_queue_power_off_work(parent);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200340
341 genpd_release_lock(parent);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200342 }
343
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200344 out:
345 genpd->poweroff_task = NULL;
346 wake_up_all(&genpd->status_wait_queue);
347 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200348}
349
350/**
351 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
352 * @work: Work structure used for scheduling the execution of this function.
353 */
354static void genpd_power_off_work_fn(struct work_struct *work)
355{
356 struct generic_pm_domain *genpd;
357
358 genpd = container_of(work, struct generic_pm_domain, power_off_work);
359
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200360 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200361 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200362 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200363}
364
365/**
366 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
367 * @dev: Device to suspend.
368 *
369 * Carry out a runtime suspend of a device under the assumption that its
370 * pm_domain field points to the domain member of an object of type
371 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
372 */
373static int pm_genpd_runtime_suspend(struct device *dev)
374{
375 struct generic_pm_domain *genpd;
376
377 dev_dbg(dev, "%s()\n", __func__);
378
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200379 genpd = dev_to_genpd(dev);
380 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200381 return -EINVAL;
382
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200383 if (genpd->stop_device) {
384 int ret = genpd->stop_device(dev);
385 if (ret)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200386 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200387 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200388
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200389 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200390 genpd->in_progress++;
391 pm_genpd_poweroff(genpd);
392 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200393 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200394
395 return 0;
396}
397
398/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200399 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
400 * @dev: Device to resume.
401 * @genpd: PM domain the device belongs to.
402 */
403static void __pm_genpd_runtime_resume(struct device *dev,
404 struct generic_pm_domain *genpd)
405{
406 struct dev_list_entry *dle;
407
408 list_for_each_entry(dle, &genpd->dev_list, node) {
409 if (dle->dev == dev) {
410 __pm_genpd_restore_device(dle, genpd);
411 break;
412 }
413 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200414}
415
416/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200417 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
418 * @dev: Device to resume.
419 *
420 * Carry out a runtime resume of a device under the assumption that its
421 * pm_domain field points to the domain member of an object of type
422 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
423 */
424static int pm_genpd_runtime_resume(struct device *dev)
425{
426 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200427 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200428 int ret;
429
430 dev_dbg(dev, "%s()\n", __func__);
431
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200432 genpd = dev_to_genpd(dev);
433 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200434 return -EINVAL;
435
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200436 ret = pm_genpd_poweron(genpd);
437 if (ret)
438 return ret;
439
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200440 mutex_lock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200441 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200442 genpd->resume_count++;
443 for (;;) {
444 prepare_to_wait(&genpd->status_wait_queue, &wait,
445 TASK_UNINTERRUPTIBLE);
446 /*
447 * If current is the powering off task, we have been called
448 * reentrantly from one of the device callbacks, so we should
449 * not wait.
450 */
451 if (!genpd->poweroff_task || genpd->poweroff_task == current)
452 break;
453 mutex_unlock(&genpd->lock);
454
455 schedule();
456
457 mutex_lock(&genpd->lock);
458 }
459 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200460 __pm_genpd_runtime_resume(dev, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200461 genpd->resume_count--;
462 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200463 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200464 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200465
466 if (genpd->start_device)
467 genpd->start_device(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200468
469 return 0;
470}
471
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200472/**
473 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
474 */
475void pm_genpd_poweroff_unused(void)
476{
477 struct generic_pm_domain *genpd;
478
479 mutex_lock(&gpd_list_lock);
480
481 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
482 genpd_queue_power_off_work(genpd);
483
484 mutex_unlock(&gpd_list_lock);
485}
486
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200487#else
488
489static inline void genpd_power_off_work_fn(struct work_struct *work) {}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200490static inline void __pm_genpd_runtime_resume(struct device *dev,
491 struct generic_pm_domain *genpd) {}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200492
493#define pm_genpd_runtime_suspend NULL
494#define pm_genpd_runtime_resume NULL
495
496#endif /* CONFIG_PM_RUNTIME */
497
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200498#ifdef CONFIG_PM_SLEEP
499
500/**
501 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
502 * @genpd: PM domain to power off, if possible.
503 *
504 * Check if the given PM domain can be powered off (during system suspend or
505 * hibernation) and do that if so. Also, in that case propagate to its parent.
506 *
507 * This function is only called in "noirq" stages of system power transitions,
508 * so it need not acquire locks (all of the "noirq" callbacks are executed
509 * sequentially, so it is guaranteed that it will never run twice in parallel).
510 */
511static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
512{
513 struct generic_pm_domain *parent = genpd->parent;
514
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200515 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200516 return;
517
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200518 if (genpd->suspended_count != genpd->device_count
519 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200520 return;
521
522 if (genpd->power_off)
523 genpd->power_off(genpd);
524
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200525 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200526 if (parent) {
527 genpd_sd_counter_dec(parent);
528 pm_genpd_sync_poweroff(parent);
529 }
530}
531
532/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200533 * resume_needed - Check whether to resume a device before system suspend.
534 * @dev: Device to check.
535 * @genpd: PM domain the device belongs to.
536 *
537 * There are two cases in which a device that can wake up the system from sleep
538 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
539 * to wake up the system and it has to remain active for this purpose while the
540 * system is in the sleep state and (2) if the device is not enabled to wake up
541 * the system from sleep states and it generally doesn't generate wakeup signals
542 * by itself (those signals are generated on its behalf by other parts of the
543 * system). In the latter case it may be necessary to reconfigure the device's
544 * wakeup settings during system suspend, because it may have been set up to
545 * signal remote wakeup from the system's working state as needed by runtime PM.
546 * Return 'true' in either of the above cases.
547 */
548static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
549{
550 bool active_wakeup;
551
552 if (!device_can_wakeup(dev))
553 return false;
554
555 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
556 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
557}
558
559/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200560 * pm_genpd_prepare - Start power transition of a device in a PM domain.
561 * @dev: Device to start the transition of.
562 *
563 * Start a power transition of a device (during a system-wide power transition)
564 * under the assumption that its pm_domain field points to the domain member of
565 * an object of type struct generic_pm_domain representing a PM domain
566 * consisting of I/O devices.
567 */
568static int pm_genpd_prepare(struct device *dev)
569{
570 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200571 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200572
573 dev_dbg(dev, "%s()\n", __func__);
574
575 genpd = dev_to_genpd(dev);
576 if (IS_ERR(genpd))
577 return -EINVAL;
578
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200579 /*
580 * If a wakeup request is pending for the device, it should be woken up
581 * at this point and a system wakeup event should be reported if it's
582 * set up to wake up the system from sleep states.
583 */
584 pm_runtime_get_noresume(dev);
585 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
586 pm_wakeup_event(dev, 0);
587
588 if (pm_wakeup_pending()) {
589 pm_runtime_put_sync(dev);
590 return -EBUSY;
591 }
592
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200593 if (resume_needed(dev, genpd))
594 pm_runtime_resume(dev);
595
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200596 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200597
598 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200599 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
600
601 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200602
603 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200604 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200605 return 0;
606 }
607
608 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200609 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
610 * so pm_genpd_poweron() will return immediately, but if the device
611 * is suspended (e.g. it's been stopped by .stop_device()), we need
612 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200613 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200614 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200615 __pm_runtime_disable(dev, false);
616
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200617 ret = pm_generic_prepare(dev);
618 if (ret) {
619 mutex_lock(&genpd->lock);
620
621 if (--genpd->prepared_count == 0)
622 genpd->suspend_power_off = false;
623
624 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200625 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200626 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200627
628 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200629 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200630}
631
632/**
633 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
634 * @dev: Device to suspend.
635 *
636 * Suspend a device under the assumption that its pm_domain field points to the
637 * domain member of an object of type struct generic_pm_domain representing
638 * a PM domain consisting of I/O devices.
639 */
640static int pm_genpd_suspend(struct device *dev)
641{
642 struct generic_pm_domain *genpd;
643
644 dev_dbg(dev, "%s()\n", __func__);
645
646 genpd = dev_to_genpd(dev);
647 if (IS_ERR(genpd))
648 return -EINVAL;
649
650 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
651}
652
653/**
654 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
655 * @dev: Device to suspend.
656 *
657 * Carry out a late suspend of a device under the assumption that its
658 * pm_domain field points to the domain member of an object of type
659 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
660 */
661static int pm_genpd_suspend_noirq(struct device *dev)
662{
663 struct generic_pm_domain *genpd;
664 int ret;
665
666 dev_dbg(dev, "%s()\n", __func__);
667
668 genpd = dev_to_genpd(dev);
669 if (IS_ERR(genpd))
670 return -EINVAL;
671
672 if (genpd->suspend_power_off)
673 return 0;
674
675 ret = pm_generic_suspend_noirq(dev);
676 if (ret)
677 return ret;
678
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200679 if (device_may_wakeup(dev)
680 && genpd->active_wakeup && genpd->active_wakeup(dev))
681 return 0;
682
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200683 if (genpd->stop_device)
684 genpd->stop_device(dev);
685
686 /*
687 * Since all of the "noirq" callbacks are executed sequentially, it is
688 * guaranteed that this function will never run twice in parallel for
689 * the same PM domain, so it is not necessary to use locking here.
690 */
691 genpd->suspended_count++;
692 pm_genpd_sync_poweroff(genpd);
693
694 return 0;
695}
696
697/**
698 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
699 * @dev: Device to resume.
700 *
701 * Carry out an early resume of a device under the assumption that its
702 * pm_domain field points to the domain member of an object of type
703 * struct generic_pm_domain representing a power domain consisting of I/O
704 * devices.
705 */
706static int pm_genpd_resume_noirq(struct device *dev)
707{
708 struct generic_pm_domain *genpd;
709
710 dev_dbg(dev, "%s()\n", __func__);
711
712 genpd = dev_to_genpd(dev);
713 if (IS_ERR(genpd))
714 return -EINVAL;
715
716 if (genpd->suspend_power_off)
717 return 0;
718
719 /*
720 * Since all of the "noirq" callbacks are executed sequentially, it is
721 * guaranteed that this function will never run twice in parallel for
722 * the same PM domain, so it is not necessary to use locking here.
723 */
724 pm_genpd_poweron(genpd);
725 genpd->suspended_count--;
726 if (genpd->start_device)
727 genpd->start_device(dev);
728
729 return pm_generic_resume_noirq(dev);
730}
731
732/**
733 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
734 * @dev: Device to resume.
735 *
736 * Resume a device under the assumption that its pm_domain field points to the
737 * domain member of an object of type struct generic_pm_domain representing
738 * a power domain consisting of I/O devices.
739 */
740static int pm_genpd_resume(struct device *dev)
741{
742 struct generic_pm_domain *genpd;
743
744 dev_dbg(dev, "%s()\n", __func__);
745
746 genpd = dev_to_genpd(dev);
747 if (IS_ERR(genpd))
748 return -EINVAL;
749
750 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
751}
752
753/**
754 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
755 * @dev: Device to freeze.
756 *
757 * Freeze a device under the assumption that its pm_domain field points to the
758 * domain member of an object of type struct generic_pm_domain representing
759 * a power domain consisting of I/O devices.
760 */
761static int pm_genpd_freeze(struct device *dev)
762{
763 struct generic_pm_domain *genpd;
764
765 dev_dbg(dev, "%s()\n", __func__);
766
767 genpd = dev_to_genpd(dev);
768 if (IS_ERR(genpd))
769 return -EINVAL;
770
771 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
772}
773
774/**
775 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
776 * @dev: Device to freeze.
777 *
778 * Carry out a late freeze of a device under the assumption that its
779 * pm_domain field points to the domain member of an object of type
780 * struct generic_pm_domain representing a power domain consisting of I/O
781 * devices.
782 */
783static int pm_genpd_freeze_noirq(struct device *dev)
784{
785 struct generic_pm_domain *genpd;
786 int ret;
787
788 dev_dbg(dev, "%s()\n", __func__);
789
790 genpd = dev_to_genpd(dev);
791 if (IS_ERR(genpd))
792 return -EINVAL;
793
794 if (genpd->suspend_power_off)
795 return 0;
796
797 ret = pm_generic_freeze_noirq(dev);
798 if (ret)
799 return ret;
800
801 if (genpd->stop_device)
802 genpd->stop_device(dev);
803
804 return 0;
805}
806
807/**
808 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
809 * @dev: Device to thaw.
810 *
811 * Carry out an early thaw of a device under the assumption that its
812 * pm_domain field points to the domain member of an object of type
813 * struct generic_pm_domain representing a power domain consisting of I/O
814 * devices.
815 */
816static int pm_genpd_thaw_noirq(struct device *dev)
817{
818 struct generic_pm_domain *genpd;
819
820 dev_dbg(dev, "%s()\n", __func__);
821
822 genpd = dev_to_genpd(dev);
823 if (IS_ERR(genpd))
824 return -EINVAL;
825
826 if (genpd->suspend_power_off)
827 return 0;
828
829 if (genpd->start_device)
830 genpd->start_device(dev);
831
832 return pm_generic_thaw_noirq(dev);
833}
834
835/**
836 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
837 * @dev: Device to thaw.
838 *
839 * Thaw a device under the assumption that its pm_domain field points to the
840 * domain member of an object of type struct generic_pm_domain representing
841 * a power domain consisting of I/O devices.
842 */
843static int pm_genpd_thaw(struct device *dev)
844{
845 struct generic_pm_domain *genpd;
846
847 dev_dbg(dev, "%s()\n", __func__);
848
849 genpd = dev_to_genpd(dev);
850 if (IS_ERR(genpd))
851 return -EINVAL;
852
853 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
854}
855
856/**
857 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
858 * @dev: Device to suspend.
859 *
860 * Power off a device under the assumption that its pm_domain field points to
861 * the domain member of an object of type struct generic_pm_domain representing
862 * a PM domain consisting of I/O devices.
863 */
864static int pm_genpd_dev_poweroff(struct device *dev)
865{
866 struct generic_pm_domain *genpd;
867
868 dev_dbg(dev, "%s()\n", __func__);
869
870 genpd = dev_to_genpd(dev);
871 if (IS_ERR(genpd))
872 return -EINVAL;
873
874 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
875}
876
877/**
878 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
879 * @dev: Device to suspend.
880 *
881 * Carry out a late powering off of a device under the assumption that its
882 * pm_domain field points to the domain member of an object of type
883 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
884 */
885static int pm_genpd_dev_poweroff_noirq(struct device *dev)
886{
887 struct generic_pm_domain *genpd;
888 int ret;
889
890 dev_dbg(dev, "%s()\n", __func__);
891
892 genpd = dev_to_genpd(dev);
893 if (IS_ERR(genpd))
894 return -EINVAL;
895
896 if (genpd->suspend_power_off)
897 return 0;
898
899 ret = pm_generic_poweroff_noirq(dev);
900 if (ret)
901 return ret;
902
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200903 if (device_may_wakeup(dev)
904 && genpd->active_wakeup && genpd->active_wakeup(dev))
905 return 0;
906
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200907 if (genpd->stop_device)
908 genpd->stop_device(dev);
909
910 /*
911 * Since all of the "noirq" callbacks are executed sequentially, it is
912 * guaranteed that this function will never run twice in parallel for
913 * the same PM domain, so it is not necessary to use locking here.
914 */
915 genpd->suspended_count++;
916 pm_genpd_sync_poweroff(genpd);
917
918 return 0;
919}
920
921/**
922 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
923 * @dev: Device to resume.
924 *
925 * Carry out an early restore of a device under the assumption that its
926 * pm_domain field points to the domain member of an object of type
927 * struct generic_pm_domain representing a power domain consisting of I/O
928 * devices.
929 */
930static int pm_genpd_restore_noirq(struct device *dev)
931{
932 struct generic_pm_domain *genpd;
933
934 dev_dbg(dev, "%s()\n", __func__);
935
936 genpd = dev_to_genpd(dev);
937 if (IS_ERR(genpd))
938 return -EINVAL;
939
940 /*
941 * Since all of the "noirq" callbacks are executed sequentially, it is
942 * guaranteed that this function will never run twice in parallel for
943 * the same PM domain, so it is not necessary to use locking here.
944 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200945 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200946 if (genpd->suspend_power_off) {
947 /*
948 * The boot kernel might put the domain into the power on state,
949 * so make sure it really is powered off.
950 */
951 if (genpd->power_off)
952 genpd->power_off(genpd);
953 return 0;
954 }
955
956 pm_genpd_poweron(genpd);
957 genpd->suspended_count--;
958 if (genpd->start_device)
959 genpd->start_device(dev);
960
961 return pm_generic_restore_noirq(dev);
962}
963
964/**
965 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
966 * @dev: Device to resume.
967 *
968 * Restore a device under the assumption that its pm_domain field points to the
969 * domain member of an object of type struct generic_pm_domain representing
970 * a power domain consisting of I/O devices.
971 */
972static int pm_genpd_restore(struct device *dev)
973{
974 struct generic_pm_domain *genpd;
975
976 dev_dbg(dev, "%s()\n", __func__);
977
978 genpd = dev_to_genpd(dev);
979 if (IS_ERR(genpd))
980 return -EINVAL;
981
982 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
983}
984
985/**
986 * pm_genpd_complete - Complete power transition of a device in a power domain.
987 * @dev: Device to complete the transition of.
988 *
989 * Complete a power transition of a device (during a system-wide power
990 * transition) under the assumption that its pm_domain field points to the
991 * domain member of an object of type struct generic_pm_domain representing
992 * a power domain consisting of I/O devices.
993 */
994static void pm_genpd_complete(struct device *dev)
995{
996 struct generic_pm_domain *genpd;
997 bool run_complete;
998
999 dev_dbg(dev, "%s()\n", __func__);
1000
1001 genpd = dev_to_genpd(dev);
1002 if (IS_ERR(genpd))
1003 return;
1004
1005 mutex_lock(&genpd->lock);
1006
1007 run_complete = !genpd->suspend_power_off;
1008 if (--genpd->prepared_count == 0)
1009 genpd->suspend_power_off = false;
1010
1011 mutex_unlock(&genpd->lock);
1012
1013 if (run_complete) {
1014 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001015 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001016 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001017 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001018 }
1019}
1020
1021#else
1022
1023#define pm_genpd_prepare NULL
1024#define pm_genpd_suspend NULL
1025#define pm_genpd_suspend_noirq NULL
1026#define pm_genpd_resume_noirq NULL
1027#define pm_genpd_resume NULL
1028#define pm_genpd_freeze NULL
1029#define pm_genpd_freeze_noirq NULL
1030#define pm_genpd_thaw_noirq NULL
1031#define pm_genpd_thaw NULL
1032#define pm_genpd_dev_poweroff_noirq NULL
1033#define pm_genpd_dev_poweroff NULL
1034#define pm_genpd_restore_noirq NULL
1035#define pm_genpd_restore NULL
1036#define pm_genpd_complete NULL
1037
1038#endif /* CONFIG_PM_SLEEP */
1039
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001040/**
1041 * pm_genpd_add_device - Add a device to an I/O PM domain.
1042 * @genpd: PM domain to add the device to.
1043 * @dev: Device to be added.
1044 */
1045int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1046{
1047 struct dev_list_entry *dle;
1048 int ret = 0;
1049
1050 dev_dbg(dev, "%s()\n", __func__);
1051
1052 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1053 return -EINVAL;
1054
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001055 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001056
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001057 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001058 ret = -EINVAL;
1059 goto out;
1060 }
1061
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001062 if (genpd->prepared_count > 0) {
1063 ret = -EAGAIN;
1064 goto out;
1065 }
1066
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001067 list_for_each_entry(dle, &genpd->dev_list, node)
1068 if (dle->dev == dev) {
1069 ret = -EINVAL;
1070 goto out;
1071 }
1072
1073 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1074 if (!dle) {
1075 ret = -ENOMEM;
1076 goto out;
1077 }
1078
1079 dle->dev = dev;
1080 dle->need_restore = false;
1081 list_add_tail(&dle->node, &genpd->dev_list);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001082 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001083
1084 spin_lock_irq(&dev->power.lock);
1085 dev->pm_domain = &genpd->domain;
1086 spin_unlock_irq(&dev->power.lock);
1087
1088 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001089 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001090
1091 return ret;
1092}
1093
1094/**
1095 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1096 * @genpd: PM domain to remove the device from.
1097 * @dev: Device to be removed.
1098 */
1099int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1100 struct device *dev)
1101{
1102 struct dev_list_entry *dle;
1103 int ret = -EINVAL;
1104
1105 dev_dbg(dev, "%s()\n", __func__);
1106
1107 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1108 return -EINVAL;
1109
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001110 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001111
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001112 if (genpd->prepared_count > 0) {
1113 ret = -EAGAIN;
1114 goto out;
1115 }
1116
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001117 list_for_each_entry(dle, &genpd->dev_list, node) {
1118 if (dle->dev != dev)
1119 continue;
1120
1121 spin_lock_irq(&dev->power.lock);
1122 dev->pm_domain = NULL;
1123 spin_unlock_irq(&dev->power.lock);
1124
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001125 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001126 list_del(&dle->node);
1127 kfree(dle);
1128
1129 ret = 0;
1130 break;
1131 }
1132
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001133 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001134 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001135
1136 return ret;
1137}
1138
1139/**
1140 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1141 * @genpd: Master PM domain to add the subdomain to.
1142 * @new_subdomain: Subdomain to be added.
1143 */
1144int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1145 struct generic_pm_domain *new_subdomain)
1146{
1147 struct generic_pm_domain *subdomain;
1148 int ret = 0;
1149
1150 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1151 return -EINVAL;
1152
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001153 start:
1154 genpd_acquire_lock(genpd);
1155 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001156
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001157 if (new_subdomain->status != GPD_STATE_POWER_OFF
1158 && new_subdomain->status != GPD_STATE_ACTIVE) {
1159 mutex_unlock(&new_subdomain->lock);
1160 genpd_release_lock(genpd);
1161 goto start;
1162 }
1163
1164 if (genpd->status == GPD_STATE_POWER_OFF
1165 && new_subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001166 ret = -EINVAL;
1167 goto out;
1168 }
1169
1170 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1171 if (subdomain == new_subdomain) {
1172 ret = -EINVAL;
1173 goto out;
1174 }
1175 }
1176
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001177 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1178 new_subdomain->parent = genpd;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001179 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001180 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001181
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001182 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001183 mutex_unlock(&new_subdomain->lock);
1184 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001185
1186 return ret;
1187}
1188
1189/**
1190 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1191 * @genpd: Master PM domain to remove the subdomain from.
1192 * @target: Subdomain to be removed.
1193 */
1194int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1195 struct generic_pm_domain *target)
1196{
1197 struct generic_pm_domain *subdomain;
1198 int ret = -EINVAL;
1199
1200 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1201 return -EINVAL;
1202
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001203 start:
1204 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001205
1206 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1207 if (subdomain != target)
1208 continue;
1209
1210 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1211
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001212 if (subdomain->status != GPD_STATE_POWER_OFF
1213 && subdomain->status != GPD_STATE_ACTIVE) {
1214 mutex_unlock(&subdomain->lock);
1215 genpd_release_lock(genpd);
1216 goto start;
1217 }
1218
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001219 list_del(&subdomain->sd_node);
1220 subdomain->parent = NULL;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001221 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001222 genpd_sd_counter_dec(genpd);
1223
1224 mutex_unlock(&subdomain->lock);
1225
1226 ret = 0;
1227 break;
1228 }
1229
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001230 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001231
1232 return ret;
1233}
1234
1235/**
1236 * pm_genpd_init - Initialize a generic I/O PM domain object.
1237 * @genpd: PM domain object to initialize.
1238 * @gov: PM domain governor to associate with the domain (may be NULL).
1239 * @is_off: Initial value of the domain's power_is_off field.
1240 */
1241void pm_genpd_init(struct generic_pm_domain *genpd,
1242 struct dev_power_governor *gov, bool is_off)
1243{
1244 if (IS_ERR_OR_NULL(genpd))
1245 return;
1246
1247 INIT_LIST_HEAD(&genpd->sd_node);
1248 genpd->parent = NULL;
1249 INIT_LIST_HEAD(&genpd->dev_list);
1250 INIT_LIST_HEAD(&genpd->sd_list);
1251 mutex_init(&genpd->lock);
1252 genpd->gov = gov;
1253 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1254 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001255 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001256 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1257 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001258 genpd->poweroff_task = NULL;
1259 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001260 genpd->device_count = 0;
1261 genpd->suspended_count = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001262 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1263 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1264 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001265 genpd->domain.ops.prepare = pm_genpd_prepare;
1266 genpd->domain.ops.suspend = pm_genpd_suspend;
1267 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1268 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1269 genpd->domain.ops.resume = pm_genpd_resume;
1270 genpd->domain.ops.freeze = pm_genpd_freeze;
1271 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1272 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1273 genpd->domain.ops.thaw = pm_genpd_thaw;
1274 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1275 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1276 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1277 genpd->domain.ops.restore = pm_genpd_restore;
1278 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001279 mutex_lock(&gpd_list_lock);
1280 list_add(&genpd->gpd_list_node, &gpd_list);
1281 mutex_unlock(&gpd_list_lock);
1282}