blob: 1c374579407c14316b35e8b9d7faff71077b18f6 [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
32static void genpd_sd_counter_dec(struct generic_pm_domain *genpd)
33{
34 if (!WARN_ON(genpd->sd_count == 0))
35 genpd->sd_count--;
36}
37
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020038static void genpd_acquire_lock(struct generic_pm_domain *genpd)
39{
40 DEFINE_WAIT(wait);
41
42 mutex_lock(&genpd->lock);
43 /*
44 * Wait for the domain to transition into either the active,
45 * or the power off state.
46 */
47 for (;;) {
48 prepare_to_wait(&genpd->status_wait_queue, &wait,
49 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020050 if (genpd->status == GPD_STATE_ACTIVE
51 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020052 break;
53 mutex_unlock(&genpd->lock);
54
55 schedule();
56
57 mutex_lock(&genpd->lock);
58 }
59 finish_wait(&genpd->status_wait_queue, &wait);
60}
61
62static void genpd_release_lock(struct generic_pm_domain *genpd)
63{
64 mutex_unlock(&genpd->lock);
65}
66
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020067static void genpd_set_active(struct generic_pm_domain *genpd)
68{
69 if (genpd->resume_count == 0)
70 genpd->status = GPD_STATE_ACTIVE;
71}
72
Rafael J. Wysockif7218892011-07-01 22:12:45 +020073/**
Rafael J. Wysocki52480512011-07-01 22:13:10 +020074 * pm_genpd_poweron - Restore power to a given PM domain and its parents.
75 * @genpd: PM domain to power up.
76 *
77 * Restore power to @genpd and all of its parents so that it is possible to
78 * resume a device belonging to it.
79 */
Magnus Damm18b4f3f2011-07-10 10:39:14 +020080int pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020081{
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020082 struct generic_pm_domain *parent = genpd->parent;
Rafael J. Wysocki52480512011-07-01 22:13:10 +020083 int ret = 0;
84
85 start:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020086 if (parent) {
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020087 genpd_acquire_lock(parent);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020088 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
89 } else {
90 mutex_lock(&genpd->lock);
91 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020092
93 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020094 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki52480512011-07-01 22:13:10 +020095 goto out;
96
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +020097 if (genpd->status != GPD_STATE_POWER_OFF) {
98 genpd_set_active(genpd);
99 goto out;
100 }
101
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200102 if (parent && parent->status != GPD_STATE_ACTIVE) {
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200103 mutex_unlock(&genpd->lock);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200104 genpd_release_lock(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200105
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200106 ret = pm_genpd_poweron(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200107 if (ret)
108 return ret;
109
110 goto start;
111 }
112
113 if (genpd->power_on) {
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200114 ret = genpd->power_on(genpd);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200115 if (ret)
116 goto out;
117 }
118
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200119 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200120 if (parent)
121 parent->sd_count++;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200122
123 out:
124 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200125 if (parent)
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200126 genpd_release_lock(parent);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200127
128 return ret;
129}
130
131#endif /* CONFIG_PM */
132
133#ifdef CONFIG_PM_RUNTIME
134
135/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200136 * __pm_genpd_save_device - Save the pre-suspend state of a device.
137 * @dle: Device list entry of the device to save the state of.
138 * @genpd: PM domain the device belongs to.
139 */
140static int __pm_genpd_save_device(struct dev_list_entry *dle,
141 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200142 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200143{
144 struct device *dev = dle->dev;
145 struct device_driver *drv = dev->driver;
146 int ret = 0;
147
148 if (dle->need_restore)
149 return 0;
150
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200151 mutex_unlock(&genpd->lock);
152
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200153 if (drv && drv->pm && drv->pm->runtime_suspend) {
154 if (genpd->start_device)
155 genpd->start_device(dev);
156
157 ret = drv->pm->runtime_suspend(dev);
158
159 if (genpd->stop_device)
160 genpd->stop_device(dev);
161 }
162
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200163 mutex_lock(&genpd->lock);
164
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200165 if (!ret)
166 dle->need_restore = true;
167
168 return ret;
169}
170
171/**
172 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
173 * @dle: Device list entry of the device to restore the state of.
174 * @genpd: PM domain the device belongs to.
175 */
176static void __pm_genpd_restore_device(struct dev_list_entry *dle,
177 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200178 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200179{
180 struct device *dev = dle->dev;
181 struct device_driver *drv = dev->driver;
182
183 if (!dle->need_restore)
184 return;
185
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200186 mutex_unlock(&genpd->lock);
187
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200188 if (drv && drv->pm && drv->pm->runtime_resume) {
189 if (genpd->start_device)
190 genpd->start_device(dev);
191
192 drv->pm->runtime_resume(dev);
193
194 if (genpd->stop_device)
195 genpd->stop_device(dev);
196 }
197
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200198 mutex_lock(&genpd->lock);
199
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200200 dle->need_restore = false;
201}
202
203/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200204 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
205 * @genpd: PM domain to check.
206 *
207 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
208 * a "power off" operation, which means that a "power on" has occured in the
209 * meantime, or if its resume_count field is different from zero, which means
210 * that one of its devices has been resumed in the meantime.
211 */
212static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
213{
214 return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
215}
216
217/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200218 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
219 * @genpd: PM domait to power off.
220 *
221 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
222 * before.
223 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200224void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200225{
226 if (!work_pending(&genpd->power_off_work))
227 queue_work(pm_wq, &genpd->power_off_work);
228}
229
230/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200231 * pm_genpd_poweroff - Remove power from a given PM domain.
232 * @genpd: PM domain to power down.
233 *
234 * If all of the @genpd's devices have been suspended and all of its subdomains
235 * have been powered down, run the runtime suspend callbacks provided by all of
236 * the @genpd's devices' drivers and remove power from @genpd.
237 */
238static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200239 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200240{
241 struct generic_pm_domain *parent;
242 struct dev_list_entry *dle;
243 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200244 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200245
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200246 start:
247 /*
248 * Do not try to power off the domain in the following situations:
249 * (1) The domain is already in the "power off" state.
250 * (2) System suspend is in progress.
251 * (3) One of the domain's devices is being resumed right now.
252 */
253 if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0
254 || genpd->resume_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200255 return 0;
256
257 if (genpd->sd_count > 0)
258 return -EBUSY;
259
260 not_suspended = 0;
261 list_for_each_entry(dle, &genpd->dev_list, node)
262 if (dle->dev->driver && !pm_runtime_suspended(dle->dev))
263 not_suspended++;
264
265 if (not_suspended > genpd->in_progress)
266 return -EBUSY;
267
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200268 if (genpd->poweroff_task) {
269 /*
270 * Another instance of pm_genpd_poweroff() is executing
271 * callbacks, so tell it to start over and return.
272 */
273 genpd->status = GPD_STATE_REPEAT;
274 return 0;
275 }
276
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200277 if (genpd->gov && genpd->gov->power_down_ok) {
278 if (!genpd->gov->power_down_ok(&genpd->domain))
279 return -EAGAIN;
280 }
281
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200282 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200283 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200284
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200285 list_for_each_entry_reverse(dle, &genpd->dev_list, node) {
286 ret = __pm_genpd_save_device(dle, genpd);
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200287 if (ret) {
288 genpd_set_active(genpd);
289 goto out;
290 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200291
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200292 if (genpd_abort_poweroff(genpd))
293 goto out;
294
295 if (genpd->status == GPD_STATE_REPEAT) {
296 genpd->poweroff_task = NULL;
297 goto start;
298 }
299 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200300
301 parent = genpd->parent;
302 if (parent) {
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200303 mutex_unlock(&genpd->lock);
304
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200305 genpd_acquire_lock(parent);
306 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200307
308 if (genpd_abort_poweroff(genpd)) {
309 genpd_release_lock(parent);
310 goto out;
311 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200312 }
313
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200314 if (genpd->power_off) {
315 ret = genpd->power_off(genpd);
316 if (ret == -EBUSY) {
317 genpd_set_active(genpd);
318 if (parent)
319 genpd_release_lock(parent);
320
321 goto out;
322 }
323 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200324
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200325 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200326
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200327 if (parent) {
328 genpd_sd_counter_dec(parent);
329 if (parent->sd_count == 0)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200330 genpd_queue_power_off_work(parent);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200331
332 genpd_release_lock(parent);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200333 }
334
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200335 out:
336 genpd->poweroff_task = NULL;
337 wake_up_all(&genpd->status_wait_queue);
338 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200339}
340
341/**
342 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
343 * @work: Work structure used for scheduling the execution of this function.
344 */
345static void genpd_power_off_work_fn(struct work_struct *work)
346{
347 struct generic_pm_domain *genpd;
348
349 genpd = container_of(work, struct generic_pm_domain, power_off_work);
350
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200351 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200352 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200353 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200354}
355
356/**
357 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
358 * @dev: Device to suspend.
359 *
360 * Carry out a runtime suspend of a device under the assumption that its
361 * pm_domain field points to the domain member of an object of type
362 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
363 */
364static int pm_genpd_runtime_suspend(struct device *dev)
365{
366 struct generic_pm_domain *genpd;
367
368 dev_dbg(dev, "%s()\n", __func__);
369
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200370 genpd = dev_to_genpd(dev);
371 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200372 return -EINVAL;
373
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200374 if (genpd->stop_device) {
375 int ret = genpd->stop_device(dev);
376 if (ret)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200377 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200378 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200379
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200380 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200381 genpd->in_progress++;
382 pm_genpd_poweroff(genpd);
383 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200384 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200385
386 return 0;
387}
388
389/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200390 * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
391 * @dev: Device to resume.
392 * @genpd: PM domain the device belongs to.
393 */
394static void __pm_genpd_runtime_resume(struct device *dev,
395 struct generic_pm_domain *genpd)
396{
397 struct dev_list_entry *dle;
398
399 list_for_each_entry(dle, &genpd->dev_list, node) {
400 if (dle->dev == dev) {
401 __pm_genpd_restore_device(dle, genpd);
402 break;
403 }
404 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200405}
406
407/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200408 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
409 * @dev: Device to resume.
410 *
411 * Carry out a runtime resume of a device under the assumption that its
412 * pm_domain field points to the domain member of an object of type
413 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
414 */
415static int pm_genpd_runtime_resume(struct device *dev)
416{
417 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200418 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200419 int ret;
420
421 dev_dbg(dev, "%s()\n", __func__);
422
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200423 genpd = dev_to_genpd(dev);
424 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200425 return -EINVAL;
426
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200427 ret = pm_genpd_poweron(genpd);
428 if (ret)
429 return ret;
430
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200431 mutex_lock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200432 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200433 genpd->resume_count++;
434 for (;;) {
435 prepare_to_wait(&genpd->status_wait_queue, &wait,
436 TASK_UNINTERRUPTIBLE);
437 /*
438 * If current is the powering off task, we have been called
439 * reentrantly from one of the device callbacks, so we should
440 * not wait.
441 */
442 if (!genpd->poweroff_task || genpd->poweroff_task == current)
443 break;
444 mutex_unlock(&genpd->lock);
445
446 schedule();
447
448 mutex_lock(&genpd->lock);
449 }
450 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200451 __pm_genpd_runtime_resume(dev, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200452 genpd->resume_count--;
453 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200454 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200455 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200456
457 if (genpd->start_device)
458 genpd->start_device(dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200459
460 return 0;
461}
462
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200463/**
464 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
465 */
466void pm_genpd_poweroff_unused(void)
467{
468 struct generic_pm_domain *genpd;
469
470 mutex_lock(&gpd_list_lock);
471
472 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
473 genpd_queue_power_off_work(genpd);
474
475 mutex_unlock(&gpd_list_lock);
476}
477
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200478#else
479
480static inline void genpd_power_off_work_fn(struct work_struct *work) {}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200481static inline void __pm_genpd_runtime_resume(struct device *dev,
482 struct generic_pm_domain *genpd) {}
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200483
484#define pm_genpd_runtime_suspend NULL
485#define pm_genpd_runtime_resume NULL
486
487#endif /* CONFIG_PM_RUNTIME */
488
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200489#ifdef CONFIG_PM_SLEEP
490
491/**
492 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents.
493 * @genpd: PM domain to power off, if possible.
494 *
495 * Check if the given PM domain can be powered off (during system suspend or
496 * hibernation) and do that if so. Also, in that case propagate to its parent.
497 *
498 * This function is only called in "noirq" stages of system power transitions,
499 * so it need not acquire locks (all of the "noirq" callbacks are executed
500 * sequentially, so it is guaranteed that it will never run twice in parallel).
501 */
502static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
503{
504 struct generic_pm_domain *parent = genpd->parent;
505
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200506 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200507 return;
508
509 if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0)
510 return;
511
512 if (genpd->power_off)
513 genpd->power_off(genpd);
514
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200515 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200516 if (parent) {
517 genpd_sd_counter_dec(parent);
518 pm_genpd_sync_poweroff(parent);
519 }
520}
521
522/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200523 * resume_needed - Check whether to resume a device before system suspend.
524 * @dev: Device to check.
525 * @genpd: PM domain the device belongs to.
526 *
527 * There are two cases in which a device that can wake up the system from sleep
528 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
529 * to wake up the system and it has to remain active for this purpose while the
530 * system is in the sleep state and (2) if the device is not enabled to wake up
531 * the system from sleep states and it generally doesn't generate wakeup signals
532 * by itself (those signals are generated on its behalf by other parts of the
533 * system). In the latter case it may be necessary to reconfigure the device's
534 * wakeup settings during system suspend, because it may have been set up to
535 * signal remote wakeup from the system's working state as needed by runtime PM.
536 * Return 'true' in either of the above cases.
537 */
538static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
539{
540 bool active_wakeup;
541
542 if (!device_can_wakeup(dev))
543 return false;
544
545 active_wakeup = genpd->active_wakeup && genpd->active_wakeup(dev);
546 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
547}
548
549/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200550 * pm_genpd_prepare - Start power transition of a device in a PM domain.
551 * @dev: Device to start the transition of.
552 *
553 * Start a power transition of a device (during a system-wide power transition)
554 * under the assumption that its pm_domain field points to the domain member of
555 * an object of type struct generic_pm_domain representing a PM domain
556 * consisting of I/O devices.
557 */
558static int pm_genpd_prepare(struct device *dev)
559{
560 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200561 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200562
563 dev_dbg(dev, "%s()\n", __func__);
564
565 genpd = dev_to_genpd(dev);
566 if (IS_ERR(genpd))
567 return -EINVAL;
568
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200569 /*
570 * If a wakeup request is pending for the device, it should be woken up
571 * at this point and a system wakeup event should be reported if it's
572 * set up to wake up the system from sleep states.
573 */
574 pm_runtime_get_noresume(dev);
575 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
576 pm_wakeup_event(dev, 0);
577
578 if (pm_wakeup_pending()) {
579 pm_runtime_put_sync(dev);
580 return -EBUSY;
581 }
582
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200583 if (resume_needed(dev, genpd))
584 pm_runtime_resume(dev);
585
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200586 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200587
588 if (genpd->prepared_count++ == 0)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200589 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
590
591 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200592
593 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200594 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200595 return 0;
596 }
597
598 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200599 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
600 * so pm_genpd_poweron() will return immediately, but if the device
601 * is suspended (e.g. it's been stopped by .stop_device()), we need
602 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200603 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200604 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200605 __pm_runtime_disable(dev, false);
606
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200607 ret = pm_generic_prepare(dev);
608 if (ret) {
609 mutex_lock(&genpd->lock);
610
611 if (--genpd->prepared_count == 0)
612 genpd->suspend_power_off = false;
613
614 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200615 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200616 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200617
618 pm_runtime_put_sync(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200619 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200620}
621
622/**
623 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
624 * @dev: Device to suspend.
625 *
626 * Suspend a device under the assumption that its pm_domain field points to the
627 * domain member of an object of type struct generic_pm_domain representing
628 * a PM domain consisting of I/O devices.
629 */
630static int pm_genpd_suspend(struct device *dev)
631{
632 struct generic_pm_domain *genpd;
633
634 dev_dbg(dev, "%s()\n", __func__);
635
636 genpd = dev_to_genpd(dev);
637 if (IS_ERR(genpd))
638 return -EINVAL;
639
640 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
641}
642
643/**
644 * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain.
645 * @dev: Device to suspend.
646 *
647 * Carry out a late suspend of a device under the assumption that its
648 * pm_domain field points to the domain member of an object of type
649 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
650 */
651static int pm_genpd_suspend_noirq(struct device *dev)
652{
653 struct generic_pm_domain *genpd;
654 int ret;
655
656 dev_dbg(dev, "%s()\n", __func__);
657
658 genpd = dev_to_genpd(dev);
659 if (IS_ERR(genpd))
660 return -EINVAL;
661
662 if (genpd->suspend_power_off)
663 return 0;
664
665 ret = pm_generic_suspend_noirq(dev);
666 if (ret)
667 return ret;
668
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200669 if (device_may_wakeup(dev)
670 && genpd->active_wakeup && genpd->active_wakeup(dev))
671 return 0;
672
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200673 if (genpd->stop_device)
674 genpd->stop_device(dev);
675
676 /*
677 * Since all of the "noirq" callbacks are executed sequentially, it is
678 * guaranteed that this function will never run twice in parallel for
679 * the same PM domain, so it is not necessary to use locking here.
680 */
681 genpd->suspended_count++;
682 pm_genpd_sync_poweroff(genpd);
683
684 return 0;
685}
686
687/**
688 * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain.
689 * @dev: Device to resume.
690 *
691 * Carry out an early resume of a device under the assumption that its
692 * pm_domain field points to the domain member of an object of type
693 * struct generic_pm_domain representing a power domain consisting of I/O
694 * devices.
695 */
696static int pm_genpd_resume_noirq(struct device *dev)
697{
698 struct generic_pm_domain *genpd;
699
700 dev_dbg(dev, "%s()\n", __func__);
701
702 genpd = dev_to_genpd(dev);
703 if (IS_ERR(genpd))
704 return -EINVAL;
705
706 if (genpd->suspend_power_off)
707 return 0;
708
709 /*
710 * Since all of the "noirq" callbacks are executed sequentially, it is
711 * guaranteed that this function will never run twice in parallel for
712 * the same PM domain, so it is not necessary to use locking here.
713 */
714 pm_genpd_poweron(genpd);
715 genpd->suspended_count--;
716 if (genpd->start_device)
717 genpd->start_device(dev);
718
719 return pm_generic_resume_noirq(dev);
720}
721
722/**
723 * pm_genpd_resume - Resume a device belonging to an I/O power domain.
724 * @dev: Device to resume.
725 *
726 * Resume a device under the assumption that its pm_domain field points to the
727 * domain member of an object of type struct generic_pm_domain representing
728 * a power domain consisting of I/O devices.
729 */
730static int pm_genpd_resume(struct device *dev)
731{
732 struct generic_pm_domain *genpd;
733
734 dev_dbg(dev, "%s()\n", __func__);
735
736 genpd = dev_to_genpd(dev);
737 if (IS_ERR(genpd))
738 return -EINVAL;
739
740 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
741}
742
743/**
744 * pm_genpd_freeze - Freeze a device belonging to an I/O power domain.
745 * @dev: Device to freeze.
746 *
747 * Freeze a device under the assumption that its pm_domain field points to the
748 * domain member of an object of type struct generic_pm_domain representing
749 * a power domain consisting of I/O devices.
750 */
751static int pm_genpd_freeze(struct device *dev)
752{
753 struct generic_pm_domain *genpd;
754
755 dev_dbg(dev, "%s()\n", __func__);
756
757 genpd = dev_to_genpd(dev);
758 if (IS_ERR(genpd))
759 return -EINVAL;
760
761 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
762}
763
764/**
765 * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain.
766 * @dev: Device to freeze.
767 *
768 * Carry out a late freeze of a device under the assumption that its
769 * pm_domain field points to the domain member of an object of type
770 * struct generic_pm_domain representing a power domain consisting of I/O
771 * devices.
772 */
773static int pm_genpd_freeze_noirq(struct device *dev)
774{
775 struct generic_pm_domain *genpd;
776 int ret;
777
778 dev_dbg(dev, "%s()\n", __func__);
779
780 genpd = dev_to_genpd(dev);
781 if (IS_ERR(genpd))
782 return -EINVAL;
783
784 if (genpd->suspend_power_off)
785 return 0;
786
787 ret = pm_generic_freeze_noirq(dev);
788 if (ret)
789 return ret;
790
791 if (genpd->stop_device)
792 genpd->stop_device(dev);
793
794 return 0;
795}
796
797/**
798 * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain.
799 * @dev: Device to thaw.
800 *
801 * Carry out an early thaw of a device under the assumption that its
802 * pm_domain field points to the domain member of an object of type
803 * struct generic_pm_domain representing a power domain consisting of I/O
804 * devices.
805 */
806static int pm_genpd_thaw_noirq(struct device *dev)
807{
808 struct generic_pm_domain *genpd;
809
810 dev_dbg(dev, "%s()\n", __func__);
811
812 genpd = dev_to_genpd(dev);
813 if (IS_ERR(genpd))
814 return -EINVAL;
815
816 if (genpd->suspend_power_off)
817 return 0;
818
819 if (genpd->start_device)
820 genpd->start_device(dev);
821
822 return pm_generic_thaw_noirq(dev);
823}
824
825/**
826 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
827 * @dev: Device to thaw.
828 *
829 * Thaw a device under the assumption that its pm_domain field points to the
830 * domain member of an object of type struct generic_pm_domain representing
831 * a power domain consisting of I/O devices.
832 */
833static int pm_genpd_thaw(struct device *dev)
834{
835 struct generic_pm_domain *genpd;
836
837 dev_dbg(dev, "%s()\n", __func__);
838
839 genpd = dev_to_genpd(dev);
840 if (IS_ERR(genpd))
841 return -EINVAL;
842
843 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
844}
845
846/**
847 * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain.
848 * @dev: Device to suspend.
849 *
850 * Power off a device under the assumption that its pm_domain field points to
851 * the domain member of an object of type struct generic_pm_domain representing
852 * a PM domain consisting of I/O devices.
853 */
854static int pm_genpd_dev_poweroff(struct device *dev)
855{
856 struct generic_pm_domain *genpd;
857
858 dev_dbg(dev, "%s()\n", __func__);
859
860 genpd = dev_to_genpd(dev);
861 if (IS_ERR(genpd))
862 return -EINVAL;
863
864 return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev);
865}
866
867/**
868 * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain.
869 * @dev: Device to suspend.
870 *
871 * Carry out a late powering off of a device under the assumption that its
872 * pm_domain field points to the domain member of an object of type
873 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
874 */
875static int pm_genpd_dev_poweroff_noirq(struct device *dev)
876{
877 struct generic_pm_domain *genpd;
878 int ret;
879
880 dev_dbg(dev, "%s()\n", __func__);
881
882 genpd = dev_to_genpd(dev);
883 if (IS_ERR(genpd))
884 return -EINVAL;
885
886 if (genpd->suspend_power_off)
887 return 0;
888
889 ret = pm_generic_poweroff_noirq(dev);
890 if (ret)
891 return ret;
892
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200893 if (device_may_wakeup(dev)
894 && genpd->active_wakeup && genpd->active_wakeup(dev))
895 return 0;
896
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200897 if (genpd->stop_device)
898 genpd->stop_device(dev);
899
900 /*
901 * Since all of the "noirq" callbacks are executed sequentially, it is
902 * guaranteed that this function will never run twice in parallel for
903 * the same PM domain, so it is not necessary to use locking here.
904 */
905 genpd->suspended_count++;
906 pm_genpd_sync_poweroff(genpd);
907
908 return 0;
909}
910
911/**
912 * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain.
913 * @dev: Device to resume.
914 *
915 * Carry out an early restore of a device under the assumption that its
916 * pm_domain field points to the domain member of an object of type
917 * struct generic_pm_domain representing a power domain consisting of I/O
918 * devices.
919 */
920static int pm_genpd_restore_noirq(struct device *dev)
921{
922 struct generic_pm_domain *genpd;
923
924 dev_dbg(dev, "%s()\n", __func__);
925
926 genpd = dev_to_genpd(dev);
927 if (IS_ERR(genpd))
928 return -EINVAL;
929
930 /*
931 * Since all of the "noirq" callbacks are executed sequentially, it is
932 * guaranteed that this function will never run twice in parallel for
933 * the same PM domain, so it is not necessary to use locking here.
934 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200935 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200936 if (genpd->suspend_power_off) {
937 /*
938 * The boot kernel might put the domain into the power on state,
939 * so make sure it really is powered off.
940 */
941 if (genpd->power_off)
942 genpd->power_off(genpd);
943 return 0;
944 }
945
946 pm_genpd_poweron(genpd);
947 genpd->suspended_count--;
948 if (genpd->start_device)
949 genpd->start_device(dev);
950
951 return pm_generic_restore_noirq(dev);
952}
953
954/**
955 * pm_genpd_restore - Restore a device belonging to an I/O power domain.
956 * @dev: Device to resume.
957 *
958 * Restore a device under the assumption that its pm_domain field points to the
959 * domain member of an object of type struct generic_pm_domain representing
960 * a power domain consisting of I/O devices.
961 */
962static int pm_genpd_restore(struct device *dev)
963{
964 struct generic_pm_domain *genpd;
965
966 dev_dbg(dev, "%s()\n", __func__);
967
968 genpd = dev_to_genpd(dev);
969 if (IS_ERR(genpd))
970 return -EINVAL;
971
972 return genpd->suspend_power_off ? 0 : pm_generic_restore(dev);
973}
974
975/**
976 * pm_genpd_complete - Complete power transition of a device in a power domain.
977 * @dev: Device to complete the transition of.
978 *
979 * Complete a power transition of a device (during a system-wide power
980 * transition) under the assumption that its pm_domain field points to the
981 * domain member of an object of type struct generic_pm_domain representing
982 * a power domain consisting of I/O devices.
983 */
984static void pm_genpd_complete(struct device *dev)
985{
986 struct generic_pm_domain *genpd;
987 bool run_complete;
988
989 dev_dbg(dev, "%s()\n", __func__);
990
991 genpd = dev_to_genpd(dev);
992 if (IS_ERR(genpd))
993 return;
994
995 mutex_lock(&genpd->lock);
996
997 run_complete = !genpd->suspend_power_off;
998 if (--genpd->prepared_count == 0)
999 genpd->suspend_power_off = false;
1000
1001 mutex_unlock(&genpd->lock);
1002
1003 if (run_complete) {
1004 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001005 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001006 pm_runtime_enable(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001007 pm_runtime_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001008 }
1009}
1010
1011#else
1012
1013#define pm_genpd_prepare NULL
1014#define pm_genpd_suspend NULL
1015#define pm_genpd_suspend_noirq NULL
1016#define pm_genpd_resume_noirq NULL
1017#define pm_genpd_resume NULL
1018#define pm_genpd_freeze NULL
1019#define pm_genpd_freeze_noirq NULL
1020#define pm_genpd_thaw_noirq NULL
1021#define pm_genpd_thaw NULL
1022#define pm_genpd_dev_poweroff_noirq NULL
1023#define pm_genpd_dev_poweroff NULL
1024#define pm_genpd_restore_noirq NULL
1025#define pm_genpd_restore NULL
1026#define pm_genpd_complete NULL
1027
1028#endif /* CONFIG_PM_SLEEP */
1029
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001030/**
1031 * pm_genpd_add_device - Add a device to an I/O PM domain.
1032 * @genpd: PM domain to add the device to.
1033 * @dev: Device to be added.
1034 */
1035int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
1036{
1037 struct dev_list_entry *dle;
1038 int ret = 0;
1039
1040 dev_dbg(dev, "%s()\n", __func__);
1041
1042 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1043 return -EINVAL;
1044
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001045 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001046
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001047 if (genpd->status == GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001048 ret = -EINVAL;
1049 goto out;
1050 }
1051
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001052 if (genpd->prepared_count > 0) {
1053 ret = -EAGAIN;
1054 goto out;
1055 }
1056
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001057 list_for_each_entry(dle, &genpd->dev_list, node)
1058 if (dle->dev == dev) {
1059 ret = -EINVAL;
1060 goto out;
1061 }
1062
1063 dle = kzalloc(sizeof(*dle), GFP_KERNEL);
1064 if (!dle) {
1065 ret = -ENOMEM;
1066 goto out;
1067 }
1068
1069 dle->dev = dev;
1070 dle->need_restore = false;
1071 list_add_tail(&dle->node, &genpd->dev_list);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001072 genpd->device_count++;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001073
1074 spin_lock_irq(&dev->power.lock);
1075 dev->pm_domain = &genpd->domain;
1076 spin_unlock_irq(&dev->power.lock);
1077
1078 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001079 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001080
1081 return ret;
1082}
1083
1084/**
1085 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1086 * @genpd: PM domain to remove the device from.
1087 * @dev: Device to be removed.
1088 */
1089int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1090 struct device *dev)
1091{
1092 struct dev_list_entry *dle;
1093 int ret = -EINVAL;
1094
1095 dev_dbg(dev, "%s()\n", __func__);
1096
1097 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1098 return -EINVAL;
1099
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001100 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001101
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001102 if (genpd->prepared_count > 0) {
1103 ret = -EAGAIN;
1104 goto out;
1105 }
1106
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001107 list_for_each_entry(dle, &genpd->dev_list, node) {
1108 if (dle->dev != dev)
1109 continue;
1110
1111 spin_lock_irq(&dev->power.lock);
1112 dev->pm_domain = NULL;
1113 spin_unlock_irq(&dev->power.lock);
1114
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001115 genpd->device_count--;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001116 list_del(&dle->node);
1117 kfree(dle);
1118
1119 ret = 0;
1120 break;
1121 }
1122
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001123 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001124 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001125
1126 return ret;
1127}
1128
1129/**
1130 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1131 * @genpd: Master PM domain to add the subdomain to.
1132 * @new_subdomain: Subdomain to be added.
1133 */
1134int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1135 struct generic_pm_domain *new_subdomain)
1136{
1137 struct generic_pm_domain *subdomain;
1138 int ret = 0;
1139
1140 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain))
1141 return -EINVAL;
1142
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001143 start:
1144 genpd_acquire_lock(genpd);
1145 mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001146
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001147 if (new_subdomain->status != GPD_STATE_POWER_OFF
1148 && new_subdomain->status != GPD_STATE_ACTIVE) {
1149 mutex_unlock(&new_subdomain->lock);
1150 genpd_release_lock(genpd);
1151 goto start;
1152 }
1153
1154 if (genpd->status == GPD_STATE_POWER_OFF
1155 && new_subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001156 ret = -EINVAL;
1157 goto out;
1158 }
1159
1160 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1161 if (subdomain == new_subdomain) {
1162 ret = -EINVAL;
1163 goto out;
1164 }
1165 }
1166
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001167 list_add_tail(&new_subdomain->sd_node, &genpd->sd_list);
1168 new_subdomain->parent = genpd;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001169 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001170 genpd->sd_count++;
1171
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001172 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001173 mutex_unlock(&new_subdomain->lock);
1174 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001175
1176 return ret;
1177}
1178
1179/**
1180 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1181 * @genpd: Master PM domain to remove the subdomain from.
1182 * @target: Subdomain to be removed.
1183 */
1184int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1185 struct generic_pm_domain *target)
1186{
1187 struct generic_pm_domain *subdomain;
1188 int ret = -EINVAL;
1189
1190 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target))
1191 return -EINVAL;
1192
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001193 start:
1194 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001195
1196 list_for_each_entry(subdomain, &genpd->sd_list, sd_node) {
1197 if (subdomain != target)
1198 continue;
1199
1200 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1201
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001202 if (subdomain->status != GPD_STATE_POWER_OFF
1203 && subdomain->status != GPD_STATE_ACTIVE) {
1204 mutex_unlock(&subdomain->lock);
1205 genpd_release_lock(genpd);
1206 goto start;
1207 }
1208
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001209 list_del(&subdomain->sd_node);
1210 subdomain->parent = NULL;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001211 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001212 genpd_sd_counter_dec(genpd);
1213
1214 mutex_unlock(&subdomain->lock);
1215
1216 ret = 0;
1217 break;
1218 }
1219
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001220 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001221
1222 return ret;
1223}
1224
1225/**
1226 * pm_genpd_init - Initialize a generic I/O PM domain object.
1227 * @genpd: PM domain object to initialize.
1228 * @gov: PM domain governor to associate with the domain (may be NULL).
1229 * @is_off: Initial value of the domain's power_is_off field.
1230 */
1231void pm_genpd_init(struct generic_pm_domain *genpd,
1232 struct dev_power_governor *gov, bool is_off)
1233{
1234 if (IS_ERR_OR_NULL(genpd))
1235 return;
1236
1237 INIT_LIST_HEAD(&genpd->sd_node);
1238 genpd->parent = NULL;
1239 INIT_LIST_HEAD(&genpd->dev_list);
1240 INIT_LIST_HEAD(&genpd->sd_list);
1241 mutex_init(&genpd->lock);
1242 genpd->gov = gov;
1243 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1244 genpd->in_progress = 0;
1245 genpd->sd_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001246 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1247 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001248 genpd->poweroff_task = NULL;
1249 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001250 genpd->device_count = 0;
1251 genpd->suspended_count = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001252 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1253 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1254 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001255 genpd->domain.ops.prepare = pm_genpd_prepare;
1256 genpd->domain.ops.suspend = pm_genpd_suspend;
1257 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1258 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1259 genpd->domain.ops.resume = pm_genpd_resume;
1260 genpd->domain.ops.freeze = pm_genpd_freeze;
1261 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1262 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1263 genpd->domain.ops.thaw = pm_genpd_thaw;
1264 genpd->domain.ops.poweroff = pm_genpd_dev_poweroff;
1265 genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq;
1266 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1267 genpd->domain.ops.restore = pm_genpd_restore;
1268 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001269 mutex_lock(&gpd_list_lock);
1270 list_add(&genpd->gpd_list_node, &gpd_list);
1271 mutex_unlock(&gpd_list_lock);
1272}