Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1 | /* |
| 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. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | #include <linux/suspend.h> |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 18 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_PM |
| 20 | |
| 21 | static struct generic_pm_domain *dev_to_genpd(struct device *dev) |
| 22 | { |
| 23 | if (IS_ERR_OR_NULL(dev->pm_domain)) |
| 24 | return ERR_PTR(-EINVAL); |
| 25 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 26 | return pd_to_genpd(dev->pm_domain); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 27 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 28 | |
| 29 | static void genpd_sd_counter_dec(struct generic_pm_domain *genpd) |
| 30 | { |
| 31 | if (!WARN_ON(genpd->sd_count == 0)) |
| 32 | genpd->sd_count--; |
| 33 | } |
| 34 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 35 | static void genpd_acquire_lock(struct generic_pm_domain *genpd) |
| 36 | { |
| 37 | DEFINE_WAIT(wait); |
| 38 | |
| 39 | mutex_lock(&genpd->lock); |
| 40 | /* |
| 41 | * Wait for the domain to transition into either the active, |
| 42 | * or the power off state. |
| 43 | */ |
| 44 | for (;;) { |
| 45 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 46 | TASK_UNINTERRUPTIBLE); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 47 | if (genpd->status == GPD_STATE_ACTIVE |
| 48 | || genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 49 | break; |
| 50 | mutex_unlock(&genpd->lock); |
| 51 | |
| 52 | schedule(); |
| 53 | |
| 54 | mutex_lock(&genpd->lock); |
| 55 | } |
| 56 | finish_wait(&genpd->status_wait_queue, &wait); |
| 57 | } |
| 58 | |
| 59 | static void genpd_release_lock(struct generic_pm_domain *genpd) |
| 60 | { |
| 61 | mutex_unlock(&genpd->lock); |
| 62 | } |
| 63 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 64 | static void genpd_set_active(struct generic_pm_domain *genpd) |
| 65 | { |
| 66 | if (genpd->resume_count == 0) |
| 67 | genpd->status = GPD_STATE_ACTIVE; |
| 68 | } |
| 69 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 70 | /** |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 71 | * pm_genpd_poweron - Restore power to a given PM domain and its parents. |
| 72 | * @genpd: PM domain to power up. |
| 73 | * |
| 74 | * Restore power to @genpd and all of its parents so that it is possible to |
| 75 | * resume a device belonging to it. |
| 76 | */ |
Magnus Damm | 18b4f3f | 2011-07-10 10:39:14 +0200 | [diff] [blame] | 77 | int pm_genpd_poweron(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 78 | { |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 79 | struct generic_pm_domain *parent = genpd->parent; |
| 80 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 81 | int ret = 0; |
| 82 | |
| 83 | start: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 84 | if (parent) { |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 85 | genpd_acquire_lock(parent); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 86 | mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING); |
| 87 | } else { |
| 88 | mutex_lock(&genpd->lock); |
| 89 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 90 | |
| 91 | if (genpd->status == GPD_STATE_ACTIVE |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 92 | || (genpd->prepared_count > 0 && genpd->suspend_power_off)) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 93 | goto out; |
| 94 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 95 | if (genpd->status != GPD_STATE_POWER_OFF) { |
| 96 | genpd_set_active(genpd); |
| 97 | goto out; |
| 98 | } |
| 99 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 100 | if (parent && parent->status != GPD_STATE_ACTIVE) { |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 101 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 102 | genpd_release_lock(parent); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 103 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 104 | ret = pm_genpd_poweron(parent); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | goto start; |
| 109 | } |
| 110 | |
| 111 | if (genpd->power_on) { |
| 112 | int ret = genpd->power_on(genpd); |
| 113 | if (ret) |
| 114 | goto out; |
| 115 | } |
| 116 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 117 | genpd_set_active(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 118 | if (parent) |
| 119 | parent->sd_count++; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 120 | |
| 121 | out: |
| 122 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 123 | if (parent) |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 124 | genpd_release_lock(parent); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | #endif /* CONFIG_PM */ |
| 130 | |
| 131 | #ifdef CONFIG_PM_RUNTIME |
| 132 | |
| 133 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 134 | * __pm_genpd_save_device - Save the pre-suspend state of a device. |
| 135 | * @dle: Device list entry of the device to save the state of. |
| 136 | * @genpd: PM domain the device belongs to. |
| 137 | */ |
| 138 | static int __pm_genpd_save_device(struct dev_list_entry *dle, |
| 139 | struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 140 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 141 | { |
| 142 | struct device *dev = dle->dev; |
| 143 | struct device_driver *drv = dev->driver; |
| 144 | int ret = 0; |
| 145 | |
| 146 | if (dle->need_restore) |
| 147 | return 0; |
| 148 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 149 | mutex_unlock(&genpd->lock); |
| 150 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 151 | if (drv && drv->pm && drv->pm->runtime_suspend) { |
| 152 | if (genpd->start_device) |
| 153 | genpd->start_device(dev); |
| 154 | |
| 155 | ret = drv->pm->runtime_suspend(dev); |
| 156 | |
| 157 | if (genpd->stop_device) |
| 158 | genpd->stop_device(dev); |
| 159 | } |
| 160 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 161 | mutex_lock(&genpd->lock); |
| 162 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 163 | if (!ret) |
| 164 | dle->need_restore = true; |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * __pm_genpd_restore_device - Restore the pre-suspend state of a device. |
| 171 | * @dle: Device list entry of the device to restore the state of. |
| 172 | * @genpd: PM domain the device belongs to. |
| 173 | */ |
| 174 | static void __pm_genpd_restore_device(struct dev_list_entry *dle, |
| 175 | struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 176 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 177 | { |
| 178 | struct device *dev = dle->dev; |
| 179 | struct device_driver *drv = dev->driver; |
| 180 | |
| 181 | if (!dle->need_restore) |
| 182 | return; |
| 183 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 184 | mutex_unlock(&genpd->lock); |
| 185 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 186 | if (drv && drv->pm && drv->pm->runtime_resume) { |
| 187 | if (genpd->start_device) |
| 188 | genpd->start_device(dev); |
| 189 | |
| 190 | drv->pm->runtime_resume(dev); |
| 191 | |
| 192 | if (genpd->stop_device) |
| 193 | genpd->stop_device(dev); |
| 194 | } |
| 195 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 196 | mutex_lock(&genpd->lock); |
| 197 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 198 | dle->need_restore = false; |
| 199 | } |
| 200 | |
| 201 | /** |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 202 | * genpd_abort_poweroff - Check if a PM domain power off should be aborted. |
| 203 | * @genpd: PM domain to check. |
| 204 | * |
| 205 | * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during |
| 206 | * a "power off" operation, which means that a "power on" has occured in the |
| 207 | * meantime, or if its resume_count field is different from zero, which means |
| 208 | * that one of its devices has been resumed in the meantime. |
| 209 | */ |
| 210 | static bool genpd_abort_poweroff(struct generic_pm_domain *genpd) |
| 211 | { |
| 212 | return genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0; |
| 213 | } |
| 214 | |
| 215 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 216 | * pm_genpd_poweroff - Remove power from a given PM domain. |
| 217 | * @genpd: PM domain to power down. |
| 218 | * |
| 219 | * If all of the @genpd's devices have been suspended and all of its subdomains |
| 220 | * have been powered down, run the runtime suspend callbacks provided by all of |
| 221 | * the @genpd's devices' drivers and remove power from @genpd. |
| 222 | */ |
| 223 | static int pm_genpd_poweroff(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 224 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 225 | { |
| 226 | struct generic_pm_domain *parent; |
| 227 | struct dev_list_entry *dle; |
| 228 | unsigned int not_suspended; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 229 | int ret = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 230 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 231 | start: |
| 232 | /* |
| 233 | * Do not try to power off the domain in the following situations: |
| 234 | * (1) The domain is already in the "power off" state. |
| 235 | * (2) System suspend is in progress. |
| 236 | * (3) One of the domain's devices is being resumed right now. |
| 237 | */ |
| 238 | if (genpd->status == GPD_STATE_POWER_OFF || genpd->prepared_count > 0 |
| 239 | || genpd->resume_count > 0) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 240 | return 0; |
| 241 | |
| 242 | if (genpd->sd_count > 0) |
| 243 | return -EBUSY; |
| 244 | |
| 245 | not_suspended = 0; |
| 246 | list_for_each_entry(dle, &genpd->dev_list, node) |
| 247 | if (dle->dev->driver && !pm_runtime_suspended(dle->dev)) |
| 248 | not_suspended++; |
| 249 | |
| 250 | if (not_suspended > genpd->in_progress) |
| 251 | return -EBUSY; |
| 252 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 253 | if (genpd->poweroff_task) { |
| 254 | /* |
| 255 | * Another instance of pm_genpd_poweroff() is executing |
| 256 | * callbacks, so tell it to start over and return. |
| 257 | */ |
| 258 | genpd->status = GPD_STATE_REPEAT; |
| 259 | return 0; |
| 260 | } |
| 261 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 262 | if (genpd->gov && genpd->gov->power_down_ok) { |
| 263 | if (!genpd->gov->power_down_ok(&genpd->domain)) |
| 264 | return -EAGAIN; |
| 265 | } |
| 266 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 267 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 268 | genpd->poweroff_task = current; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 269 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 270 | list_for_each_entry_reverse(dle, &genpd->dev_list, node) { |
| 271 | ret = __pm_genpd_save_device(dle, genpd); |
Rafael J. Wysocki | 697a7f3 | 2011-07-12 00:39:48 +0200 | [diff] [blame^] | 272 | if (ret) { |
| 273 | genpd_set_active(genpd); |
| 274 | goto out; |
| 275 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 276 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 277 | if (genpd_abort_poweroff(genpd)) |
| 278 | goto out; |
| 279 | |
| 280 | if (genpd->status == GPD_STATE_REPEAT) { |
| 281 | genpd->poweroff_task = NULL; |
| 282 | goto start; |
| 283 | } |
| 284 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 285 | |
| 286 | parent = genpd->parent; |
| 287 | if (parent) { |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 288 | mutex_unlock(&genpd->lock); |
| 289 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 290 | genpd_acquire_lock(parent); |
| 291 | mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 292 | |
| 293 | if (genpd_abort_poweroff(genpd)) { |
| 294 | genpd_release_lock(parent); |
| 295 | goto out; |
| 296 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 299 | if (genpd->power_off) |
| 300 | genpd->power_off(genpd); |
| 301 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 302 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 303 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 304 | if (parent) { |
| 305 | genpd_sd_counter_dec(parent); |
| 306 | if (parent->sd_count == 0) |
| 307 | queue_work(pm_wq, &parent->power_off_work); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 308 | |
| 309 | genpd_release_lock(parent); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 312 | out: |
| 313 | genpd->poweroff_task = NULL; |
| 314 | wake_up_all(&genpd->status_wait_queue); |
| 315 | return ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0. |
| 320 | * @work: Work structure used for scheduling the execution of this function. |
| 321 | */ |
| 322 | static void genpd_power_off_work_fn(struct work_struct *work) |
| 323 | { |
| 324 | struct generic_pm_domain *genpd; |
| 325 | |
| 326 | genpd = container_of(work, struct generic_pm_domain, power_off_work); |
| 327 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 328 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 329 | pm_genpd_poweroff(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 330 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain. |
| 335 | * @dev: Device to suspend. |
| 336 | * |
| 337 | * Carry out a runtime suspend of a device under the assumption that its |
| 338 | * pm_domain field points to the domain member of an object of type |
| 339 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 340 | */ |
| 341 | static int pm_genpd_runtime_suspend(struct device *dev) |
| 342 | { |
| 343 | struct generic_pm_domain *genpd; |
| 344 | |
| 345 | dev_dbg(dev, "%s()\n", __func__); |
| 346 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 347 | genpd = dev_to_genpd(dev); |
| 348 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 349 | return -EINVAL; |
| 350 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 351 | if (genpd->stop_device) { |
| 352 | int ret = genpd->stop_device(dev); |
| 353 | if (ret) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 354 | return ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 355 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 356 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 357 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 358 | genpd->in_progress++; |
| 359 | pm_genpd_poweroff(genpd); |
| 360 | genpd->in_progress--; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 361 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /** |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 367 | * __pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
| 368 | * @dev: Device to resume. |
| 369 | * @genpd: PM domain the device belongs to. |
| 370 | */ |
| 371 | static void __pm_genpd_runtime_resume(struct device *dev, |
| 372 | struct generic_pm_domain *genpd) |
| 373 | { |
| 374 | struct dev_list_entry *dle; |
| 375 | |
| 376 | list_for_each_entry(dle, &genpd->dev_list, node) { |
| 377 | if (dle->dev == dev) { |
| 378 | __pm_genpd_restore_device(dle, genpd); |
| 379 | break; |
| 380 | } |
| 381 | } |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 385 | * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
| 386 | * @dev: Device to resume. |
| 387 | * |
| 388 | * Carry out a runtime resume of a device under the assumption that its |
| 389 | * pm_domain field points to the domain member of an object of type |
| 390 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 391 | */ |
| 392 | static int pm_genpd_runtime_resume(struct device *dev) |
| 393 | { |
| 394 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 395 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 396 | int ret; |
| 397 | |
| 398 | dev_dbg(dev, "%s()\n", __func__); |
| 399 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 400 | genpd = dev_to_genpd(dev); |
| 401 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 402 | return -EINVAL; |
| 403 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 404 | ret = pm_genpd_poweron(genpd); |
| 405 | if (ret) |
| 406 | return ret; |
| 407 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 408 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 409 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 410 | genpd->resume_count++; |
| 411 | for (;;) { |
| 412 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 413 | TASK_UNINTERRUPTIBLE); |
| 414 | /* |
| 415 | * If current is the powering off task, we have been called |
| 416 | * reentrantly from one of the device callbacks, so we should |
| 417 | * not wait. |
| 418 | */ |
| 419 | if (!genpd->poweroff_task || genpd->poweroff_task == current) |
| 420 | break; |
| 421 | mutex_unlock(&genpd->lock); |
| 422 | |
| 423 | schedule(); |
| 424 | |
| 425 | mutex_lock(&genpd->lock); |
| 426 | } |
| 427 | finish_wait(&genpd->status_wait_queue, &wait); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 428 | __pm_genpd_runtime_resume(dev, genpd); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 429 | genpd->resume_count--; |
| 430 | genpd_set_active(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 431 | wake_up_all(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 432 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 433 | |
| 434 | if (genpd->start_device) |
| 435 | genpd->start_device(dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | #else |
| 441 | |
| 442 | static inline void genpd_power_off_work_fn(struct work_struct *work) {} |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 443 | static inline void __pm_genpd_runtime_resume(struct device *dev, |
| 444 | struct generic_pm_domain *genpd) {} |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 445 | |
| 446 | #define pm_genpd_runtime_suspend NULL |
| 447 | #define pm_genpd_runtime_resume NULL |
| 448 | |
| 449 | #endif /* CONFIG_PM_RUNTIME */ |
| 450 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 451 | #ifdef CONFIG_PM_SLEEP |
| 452 | |
| 453 | /** |
| 454 | * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its parents. |
| 455 | * @genpd: PM domain to power off, if possible. |
| 456 | * |
| 457 | * Check if the given PM domain can be powered off (during system suspend or |
| 458 | * hibernation) and do that if so. Also, in that case propagate to its parent. |
| 459 | * |
| 460 | * This function is only called in "noirq" stages of system power transitions, |
| 461 | * so it need not acquire locks (all of the "noirq" callbacks are executed |
| 462 | * sequentially, so it is guaranteed that it will never run twice in parallel). |
| 463 | */ |
| 464 | static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd) |
| 465 | { |
| 466 | struct generic_pm_domain *parent = genpd->parent; |
| 467 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 468 | if (genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 469 | return; |
| 470 | |
| 471 | if (genpd->suspended_count != genpd->device_count || genpd->sd_count > 0) |
| 472 | return; |
| 473 | |
| 474 | if (genpd->power_off) |
| 475 | genpd->power_off(genpd); |
| 476 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 477 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 478 | if (parent) { |
| 479 | genpd_sd_counter_dec(parent); |
| 480 | pm_genpd_sync_poweroff(parent); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * pm_genpd_prepare - Start power transition of a device in a PM domain. |
| 486 | * @dev: Device to start the transition of. |
| 487 | * |
| 488 | * Start a power transition of a device (during a system-wide power transition) |
| 489 | * under the assumption that its pm_domain field points to the domain member of |
| 490 | * an object of type struct generic_pm_domain representing a PM domain |
| 491 | * consisting of I/O devices. |
| 492 | */ |
| 493 | static int pm_genpd_prepare(struct device *dev) |
| 494 | { |
| 495 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 496 | int ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 497 | |
| 498 | dev_dbg(dev, "%s()\n", __func__); |
| 499 | |
| 500 | genpd = dev_to_genpd(dev); |
| 501 | if (IS_ERR(genpd)) |
| 502 | return -EINVAL; |
| 503 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 504 | /* |
| 505 | * If a wakeup request is pending for the device, it should be woken up |
| 506 | * at this point and a system wakeup event should be reported if it's |
| 507 | * set up to wake up the system from sleep states. |
| 508 | */ |
| 509 | pm_runtime_get_noresume(dev); |
| 510 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 511 | pm_wakeup_event(dev, 0); |
| 512 | |
| 513 | if (pm_wakeup_pending()) { |
| 514 | pm_runtime_put_sync(dev); |
| 515 | return -EBUSY; |
| 516 | } |
| 517 | |
| 518 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 519 | |
| 520 | if (genpd->prepared_count++ == 0) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 521 | genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF; |
| 522 | |
| 523 | genpd_release_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 524 | |
| 525 | if (genpd->suspend_power_off) { |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 526 | pm_runtime_put_noidle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /* |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 531 | * The PM domain must be in the GPD_STATE_ACTIVE state at this point, |
| 532 | * so pm_genpd_poweron() will return immediately, but if the device |
| 533 | * is suspended (e.g. it's been stopped by .stop_device()), we need |
| 534 | * to make it operational. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 535 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 536 | pm_runtime_resume(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 537 | __pm_runtime_disable(dev, false); |
| 538 | |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 539 | ret = pm_generic_prepare(dev); |
| 540 | if (ret) { |
| 541 | mutex_lock(&genpd->lock); |
| 542 | |
| 543 | if (--genpd->prepared_count == 0) |
| 544 | genpd->suspend_power_off = false; |
| 545 | |
| 546 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 547 | pm_runtime_enable(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 548 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 549 | |
| 550 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 551 | return ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
| 555 | * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain. |
| 556 | * @dev: Device to suspend. |
| 557 | * |
| 558 | * Suspend a device under the assumption that its pm_domain field points to the |
| 559 | * domain member of an object of type struct generic_pm_domain representing |
| 560 | * a PM domain consisting of I/O devices. |
| 561 | */ |
| 562 | static int pm_genpd_suspend(struct device *dev) |
| 563 | { |
| 564 | struct generic_pm_domain *genpd; |
| 565 | |
| 566 | dev_dbg(dev, "%s()\n", __func__); |
| 567 | |
| 568 | genpd = dev_to_genpd(dev); |
| 569 | if (IS_ERR(genpd)) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * pm_genpd_suspend_noirq - Late suspend of a device from an I/O PM domain. |
| 577 | * @dev: Device to suspend. |
| 578 | * |
| 579 | * Carry out a late suspend of a device under the assumption that its |
| 580 | * pm_domain field points to the domain member of an object of type |
| 581 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 582 | */ |
| 583 | static int pm_genpd_suspend_noirq(struct device *dev) |
| 584 | { |
| 585 | struct generic_pm_domain *genpd; |
| 586 | int ret; |
| 587 | |
| 588 | dev_dbg(dev, "%s()\n", __func__); |
| 589 | |
| 590 | genpd = dev_to_genpd(dev); |
| 591 | if (IS_ERR(genpd)) |
| 592 | return -EINVAL; |
| 593 | |
| 594 | if (genpd->suspend_power_off) |
| 595 | return 0; |
| 596 | |
| 597 | ret = pm_generic_suspend_noirq(dev); |
| 598 | if (ret) |
| 599 | return ret; |
| 600 | |
Rafael J. Wysocki | d4f2d87 | 2011-07-01 22:13:29 +0200 | [diff] [blame] | 601 | if (device_may_wakeup(dev) |
| 602 | && genpd->active_wakeup && genpd->active_wakeup(dev)) |
| 603 | return 0; |
| 604 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 605 | if (genpd->stop_device) |
| 606 | genpd->stop_device(dev); |
| 607 | |
| 608 | /* |
| 609 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 610 | * guaranteed that this function will never run twice in parallel for |
| 611 | * the same PM domain, so it is not necessary to use locking here. |
| 612 | */ |
| 613 | genpd->suspended_count++; |
| 614 | pm_genpd_sync_poweroff(genpd); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * pm_genpd_resume_noirq - Early resume of a device from an I/O power domain. |
| 621 | * @dev: Device to resume. |
| 622 | * |
| 623 | * Carry out an early resume of a device under the assumption that its |
| 624 | * pm_domain field points to the domain member of an object of type |
| 625 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 626 | * devices. |
| 627 | */ |
| 628 | static int pm_genpd_resume_noirq(struct device *dev) |
| 629 | { |
| 630 | struct generic_pm_domain *genpd; |
| 631 | |
| 632 | dev_dbg(dev, "%s()\n", __func__); |
| 633 | |
| 634 | genpd = dev_to_genpd(dev); |
| 635 | if (IS_ERR(genpd)) |
| 636 | return -EINVAL; |
| 637 | |
| 638 | if (genpd->suspend_power_off) |
| 639 | return 0; |
| 640 | |
| 641 | /* |
| 642 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 643 | * guaranteed that this function will never run twice in parallel for |
| 644 | * the same PM domain, so it is not necessary to use locking here. |
| 645 | */ |
| 646 | pm_genpd_poweron(genpd); |
| 647 | genpd->suspended_count--; |
| 648 | if (genpd->start_device) |
| 649 | genpd->start_device(dev); |
| 650 | |
| 651 | return pm_generic_resume_noirq(dev); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * pm_genpd_resume - Resume a device belonging to an I/O power domain. |
| 656 | * @dev: Device to resume. |
| 657 | * |
| 658 | * Resume a device under the assumption that its pm_domain field points to the |
| 659 | * domain member of an object of type struct generic_pm_domain representing |
| 660 | * a power domain consisting of I/O devices. |
| 661 | */ |
| 662 | static int pm_genpd_resume(struct device *dev) |
| 663 | { |
| 664 | struct generic_pm_domain *genpd; |
| 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 | return genpd->suspend_power_off ? 0 : pm_generic_resume(dev); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * pm_genpd_freeze - Freeze a device belonging to an I/O power domain. |
| 677 | * @dev: Device to freeze. |
| 678 | * |
| 679 | * Freeze a device under the assumption that its pm_domain field points to the |
| 680 | * domain member of an object of type struct generic_pm_domain representing |
| 681 | * a power domain consisting of I/O devices. |
| 682 | */ |
| 683 | static int pm_genpd_freeze(struct device *dev) |
| 684 | { |
| 685 | struct generic_pm_domain *genpd; |
| 686 | |
| 687 | dev_dbg(dev, "%s()\n", __func__); |
| 688 | |
| 689 | genpd = dev_to_genpd(dev); |
| 690 | if (IS_ERR(genpd)) |
| 691 | return -EINVAL; |
| 692 | |
| 693 | return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * pm_genpd_freeze_noirq - Late freeze of a device from an I/O power domain. |
| 698 | * @dev: Device to freeze. |
| 699 | * |
| 700 | * Carry out a late freeze of a device under the assumption that its |
| 701 | * pm_domain field points to the domain member of an object of type |
| 702 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 703 | * devices. |
| 704 | */ |
| 705 | static int pm_genpd_freeze_noirq(struct device *dev) |
| 706 | { |
| 707 | struct generic_pm_domain *genpd; |
| 708 | int ret; |
| 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 | ret = pm_generic_freeze_noirq(dev); |
| 720 | if (ret) |
| 721 | return ret; |
| 722 | |
| 723 | if (genpd->stop_device) |
| 724 | genpd->stop_device(dev); |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * pm_genpd_thaw_noirq - Early thaw of a device from an I/O power domain. |
| 731 | * @dev: Device to thaw. |
| 732 | * |
| 733 | * Carry out an early thaw of a device under the assumption that its |
| 734 | * pm_domain field points to the domain member of an object of type |
| 735 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 736 | * devices. |
| 737 | */ |
| 738 | static int pm_genpd_thaw_noirq(struct device *dev) |
| 739 | { |
| 740 | struct generic_pm_domain *genpd; |
| 741 | |
| 742 | dev_dbg(dev, "%s()\n", __func__); |
| 743 | |
| 744 | genpd = dev_to_genpd(dev); |
| 745 | if (IS_ERR(genpd)) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | if (genpd->suspend_power_off) |
| 749 | return 0; |
| 750 | |
| 751 | if (genpd->start_device) |
| 752 | genpd->start_device(dev); |
| 753 | |
| 754 | return pm_generic_thaw_noirq(dev); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * pm_genpd_thaw - Thaw a device belonging to an I/O power domain. |
| 759 | * @dev: Device to thaw. |
| 760 | * |
| 761 | * Thaw a device under the assumption that its pm_domain field points to the |
| 762 | * domain member of an object of type struct generic_pm_domain representing |
| 763 | * a power domain consisting of I/O devices. |
| 764 | */ |
| 765 | static int pm_genpd_thaw(struct device *dev) |
| 766 | { |
| 767 | struct generic_pm_domain *genpd; |
| 768 | |
| 769 | dev_dbg(dev, "%s()\n", __func__); |
| 770 | |
| 771 | genpd = dev_to_genpd(dev); |
| 772 | if (IS_ERR(genpd)) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * pm_genpd_dev_poweroff - Power off a device belonging to an I/O PM domain. |
| 780 | * @dev: Device to suspend. |
| 781 | * |
| 782 | * Power off a device under the assumption that its pm_domain field points to |
| 783 | * the domain member of an object of type struct generic_pm_domain representing |
| 784 | * a PM domain consisting of I/O devices. |
| 785 | */ |
| 786 | static int pm_genpd_dev_poweroff(struct device *dev) |
| 787 | { |
| 788 | struct generic_pm_domain *genpd; |
| 789 | |
| 790 | dev_dbg(dev, "%s()\n", __func__); |
| 791 | |
| 792 | genpd = dev_to_genpd(dev); |
| 793 | if (IS_ERR(genpd)) |
| 794 | return -EINVAL; |
| 795 | |
| 796 | return genpd->suspend_power_off ? 0 : pm_generic_poweroff(dev); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * pm_genpd_dev_poweroff_noirq - Late power off of a device from a PM domain. |
| 801 | * @dev: Device to suspend. |
| 802 | * |
| 803 | * Carry out a late powering off of a device under the assumption that its |
| 804 | * pm_domain field points to the domain member of an object of type |
| 805 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 806 | */ |
| 807 | static int pm_genpd_dev_poweroff_noirq(struct device *dev) |
| 808 | { |
| 809 | struct generic_pm_domain *genpd; |
| 810 | int ret; |
| 811 | |
| 812 | dev_dbg(dev, "%s()\n", __func__); |
| 813 | |
| 814 | genpd = dev_to_genpd(dev); |
| 815 | if (IS_ERR(genpd)) |
| 816 | return -EINVAL; |
| 817 | |
| 818 | if (genpd->suspend_power_off) |
| 819 | return 0; |
| 820 | |
| 821 | ret = pm_generic_poweroff_noirq(dev); |
| 822 | if (ret) |
| 823 | return ret; |
| 824 | |
Rafael J. Wysocki | d4f2d87 | 2011-07-01 22:13:29 +0200 | [diff] [blame] | 825 | if (device_may_wakeup(dev) |
| 826 | && genpd->active_wakeup && genpd->active_wakeup(dev)) |
| 827 | return 0; |
| 828 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 829 | if (genpd->stop_device) |
| 830 | genpd->stop_device(dev); |
| 831 | |
| 832 | /* |
| 833 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 834 | * guaranteed that this function will never run twice in parallel for |
| 835 | * the same PM domain, so it is not necessary to use locking here. |
| 836 | */ |
| 837 | genpd->suspended_count++; |
| 838 | pm_genpd_sync_poweroff(genpd); |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * pm_genpd_restore_noirq - Early restore of a device from an I/O power domain. |
| 845 | * @dev: Device to resume. |
| 846 | * |
| 847 | * Carry out an early restore of a device under the assumption that its |
| 848 | * pm_domain field points to the domain member of an object of type |
| 849 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 850 | * devices. |
| 851 | */ |
| 852 | static int pm_genpd_restore_noirq(struct device *dev) |
| 853 | { |
| 854 | struct generic_pm_domain *genpd; |
| 855 | |
| 856 | dev_dbg(dev, "%s()\n", __func__); |
| 857 | |
| 858 | genpd = dev_to_genpd(dev); |
| 859 | if (IS_ERR(genpd)) |
| 860 | return -EINVAL; |
| 861 | |
| 862 | /* |
| 863 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 864 | * guaranteed that this function will never run twice in parallel for |
| 865 | * the same PM domain, so it is not necessary to use locking here. |
| 866 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 867 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 868 | if (genpd->suspend_power_off) { |
| 869 | /* |
| 870 | * The boot kernel might put the domain into the power on state, |
| 871 | * so make sure it really is powered off. |
| 872 | */ |
| 873 | if (genpd->power_off) |
| 874 | genpd->power_off(genpd); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | pm_genpd_poweron(genpd); |
| 879 | genpd->suspended_count--; |
| 880 | if (genpd->start_device) |
| 881 | genpd->start_device(dev); |
| 882 | |
| 883 | return pm_generic_restore_noirq(dev); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * pm_genpd_restore - Restore a device belonging to an I/O power domain. |
| 888 | * @dev: Device to resume. |
| 889 | * |
| 890 | * Restore a device under the assumption that its pm_domain field points to the |
| 891 | * domain member of an object of type struct generic_pm_domain representing |
| 892 | * a power domain consisting of I/O devices. |
| 893 | */ |
| 894 | static int pm_genpd_restore(struct device *dev) |
| 895 | { |
| 896 | struct generic_pm_domain *genpd; |
| 897 | |
| 898 | dev_dbg(dev, "%s()\n", __func__); |
| 899 | |
| 900 | genpd = dev_to_genpd(dev); |
| 901 | if (IS_ERR(genpd)) |
| 902 | return -EINVAL; |
| 903 | |
| 904 | return genpd->suspend_power_off ? 0 : pm_generic_restore(dev); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * pm_genpd_complete - Complete power transition of a device in a power domain. |
| 909 | * @dev: Device to complete the transition of. |
| 910 | * |
| 911 | * Complete a power transition of a device (during a system-wide power |
| 912 | * transition) under the assumption that its pm_domain field points to the |
| 913 | * domain member of an object of type struct generic_pm_domain representing |
| 914 | * a power domain consisting of I/O devices. |
| 915 | */ |
| 916 | static void pm_genpd_complete(struct device *dev) |
| 917 | { |
| 918 | struct generic_pm_domain *genpd; |
| 919 | bool run_complete; |
| 920 | |
| 921 | dev_dbg(dev, "%s()\n", __func__); |
| 922 | |
| 923 | genpd = dev_to_genpd(dev); |
| 924 | if (IS_ERR(genpd)) |
| 925 | return; |
| 926 | |
| 927 | mutex_lock(&genpd->lock); |
| 928 | |
| 929 | run_complete = !genpd->suspend_power_off; |
| 930 | if (--genpd->prepared_count == 0) |
| 931 | genpd->suspend_power_off = false; |
| 932 | |
| 933 | mutex_unlock(&genpd->lock); |
| 934 | |
| 935 | if (run_complete) { |
| 936 | pm_generic_complete(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 937 | pm_runtime_set_active(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 938 | pm_runtime_enable(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 939 | pm_runtime_idle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
| 943 | #else |
| 944 | |
| 945 | #define pm_genpd_prepare NULL |
| 946 | #define pm_genpd_suspend NULL |
| 947 | #define pm_genpd_suspend_noirq NULL |
| 948 | #define pm_genpd_resume_noirq NULL |
| 949 | #define pm_genpd_resume NULL |
| 950 | #define pm_genpd_freeze NULL |
| 951 | #define pm_genpd_freeze_noirq NULL |
| 952 | #define pm_genpd_thaw_noirq NULL |
| 953 | #define pm_genpd_thaw NULL |
| 954 | #define pm_genpd_dev_poweroff_noirq NULL |
| 955 | #define pm_genpd_dev_poweroff NULL |
| 956 | #define pm_genpd_restore_noirq NULL |
| 957 | #define pm_genpd_restore NULL |
| 958 | #define pm_genpd_complete NULL |
| 959 | |
| 960 | #endif /* CONFIG_PM_SLEEP */ |
| 961 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 962 | /** |
| 963 | * pm_genpd_add_device - Add a device to an I/O PM domain. |
| 964 | * @genpd: PM domain to add the device to. |
| 965 | * @dev: Device to be added. |
| 966 | */ |
| 967 | int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev) |
| 968 | { |
| 969 | struct dev_list_entry *dle; |
| 970 | int ret = 0; |
| 971 | |
| 972 | dev_dbg(dev, "%s()\n", __func__); |
| 973 | |
| 974 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 975 | return -EINVAL; |
| 976 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 977 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 978 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 979 | if (genpd->status == GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 980 | ret = -EINVAL; |
| 981 | goto out; |
| 982 | } |
| 983 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 984 | if (genpd->prepared_count > 0) { |
| 985 | ret = -EAGAIN; |
| 986 | goto out; |
| 987 | } |
| 988 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 989 | list_for_each_entry(dle, &genpd->dev_list, node) |
| 990 | if (dle->dev == dev) { |
| 991 | ret = -EINVAL; |
| 992 | goto out; |
| 993 | } |
| 994 | |
| 995 | dle = kzalloc(sizeof(*dle), GFP_KERNEL); |
| 996 | if (!dle) { |
| 997 | ret = -ENOMEM; |
| 998 | goto out; |
| 999 | } |
| 1000 | |
| 1001 | dle->dev = dev; |
| 1002 | dle->need_restore = false; |
| 1003 | list_add_tail(&dle->node, &genpd->dev_list); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1004 | genpd->device_count++; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1005 | |
| 1006 | spin_lock_irq(&dev->power.lock); |
| 1007 | dev->pm_domain = &genpd->domain; |
| 1008 | spin_unlock_irq(&dev->power.lock); |
| 1009 | |
| 1010 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1011 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1012 | |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. |
| 1018 | * @genpd: PM domain to remove the device from. |
| 1019 | * @dev: Device to be removed. |
| 1020 | */ |
| 1021 | int pm_genpd_remove_device(struct generic_pm_domain *genpd, |
| 1022 | struct device *dev) |
| 1023 | { |
| 1024 | struct dev_list_entry *dle; |
| 1025 | int ret = -EINVAL; |
| 1026 | |
| 1027 | dev_dbg(dev, "%s()\n", __func__); |
| 1028 | |
| 1029 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1030 | return -EINVAL; |
| 1031 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1032 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1033 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1034 | if (genpd->prepared_count > 0) { |
| 1035 | ret = -EAGAIN; |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1039 | list_for_each_entry(dle, &genpd->dev_list, node) { |
| 1040 | if (dle->dev != dev) |
| 1041 | continue; |
| 1042 | |
| 1043 | spin_lock_irq(&dev->power.lock); |
| 1044 | dev->pm_domain = NULL; |
| 1045 | spin_unlock_irq(&dev->power.lock); |
| 1046 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1047 | genpd->device_count--; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1048 | list_del(&dle->node); |
| 1049 | kfree(dle); |
| 1050 | |
| 1051 | ret = 0; |
| 1052 | break; |
| 1053 | } |
| 1054 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1055 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1056 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1057 | |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 1063 | * @genpd: Master PM domain to add the subdomain to. |
| 1064 | * @new_subdomain: Subdomain to be added. |
| 1065 | */ |
| 1066 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, |
| 1067 | struct generic_pm_domain *new_subdomain) |
| 1068 | { |
| 1069 | struct generic_pm_domain *subdomain; |
| 1070 | int ret = 0; |
| 1071 | |
| 1072 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(new_subdomain)) |
| 1073 | return -EINVAL; |
| 1074 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1075 | start: |
| 1076 | genpd_acquire_lock(genpd); |
| 1077 | mutex_lock_nested(&new_subdomain->lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1078 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1079 | if (new_subdomain->status != GPD_STATE_POWER_OFF |
| 1080 | && new_subdomain->status != GPD_STATE_ACTIVE) { |
| 1081 | mutex_unlock(&new_subdomain->lock); |
| 1082 | genpd_release_lock(genpd); |
| 1083 | goto start; |
| 1084 | } |
| 1085 | |
| 1086 | if (genpd->status == GPD_STATE_POWER_OFF |
| 1087 | && new_subdomain->status != GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1088 | ret = -EINVAL; |
| 1089 | goto out; |
| 1090 | } |
| 1091 | |
| 1092 | list_for_each_entry(subdomain, &genpd->sd_list, sd_node) { |
| 1093 | if (subdomain == new_subdomain) { |
| 1094 | ret = -EINVAL; |
| 1095 | goto out; |
| 1096 | } |
| 1097 | } |
| 1098 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1099 | list_add_tail(&new_subdomain->sd_node, &genpd->sd_list); |
| 1100 | new_subdomain->parent = genpd; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1101 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1102 | genpd->sd_count++; |
| 1103 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1104 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1105 | mutex_unlock(&new_subdomain->lock); |
| 1106 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1107 | |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | /** |
| 1112 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. |
| 1113 | * @genpd: Master PM domain to remove the subdomain from. |
| 1114 | * @target: Subdomain to be removed. |
| 1115 | */ |
| 1116 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, |
| 1117 | struct generic_pm_domain *target) |
| 1118 | { |
| 1119 | struct generic_pm_domain *subdomain; |
| 1120 | int ret = -EINVAL; |
| 1121 | |
| 1122 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(target)) |
| 1123 | return -EINVAL; |
| 1124 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1125 | start: |
| 1126 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1127 | |
| 1128 | list_for_each_entry(subdomain, &genpd->sd_list, sd_node) { |
| 1129 | if (subdomain != target) |
| 1130 | continue; |
| 1131 | |
| 1132 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
| 1133 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1134 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1135 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1136 | mutex_unlock(&subdomain->lock); |
| 1137 | genpd_release_lock(genpd); |
| 1138 | goto start; |
| 1139 | } |
| 1140 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1141 | list_del(&subdomain->sd_node); |
| 1142 | subdomain->parent = NULL; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1143 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1144 | genpd_sd_counter_dec(genpd); |
| 1145 | |
| 1146 | mutex_unlock(&subdomain->lock); |
| 1147 | |
| 1148 | ret = 0; |
| 1149 | break; |
| 1150 | } |
| 1151 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1152 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1153 | |
| 1154 | return ret; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * pm_genpd_init - Initialize a generic I/O PM domain object. |
| 1159 | * @genpd: PM domain object to initialize. |
| 1160 | * @gov: PM domain governor to associate with the domain (may be NULL). |
| 1161 | * @is_off: Initial value of the domain's power_is_off field. |
| 1162 | */ |
| 1163 | void pm_genpd_init(struct generic_pm_domain *genpd, |
| 1164 | struct dev_power_governor *gov, bool is_off) |
| 1165 | { |
| 1166 | if (IS_ERR_OR_NULL(genpd)) |
| 1167 | return; |
| 1168 | |
| 1169 | INIT_LIST_HEAD(&genpd->sd_node); |
| 1170 | genpd->parent = NULL; |
| 1171 | INIT_LIST_HEAD(&genpd->dev_list); |
| 1172 | INIT_LIST_HEAD(&genpd->sd_list); |
| 1173 | mutex_init(&genpd->lock); |
| 1174 | genpd->gov = gov; |
| 1175 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); |
| 1176 | genpd->in_progress = 0; |
| 1177 | genpd->sd_count = 0; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1178 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
| 1179 | init_waitqueue_head(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 1180 | genpd->poweroff_task = NULL; |
| 1181 | genpd->resume_count = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1182 | genpd->device_count = 0; |
| 1183 | genpd->suspended_count = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1184 | genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; |
| 1185 | genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume; |
| 1186 | genpd->domain.ops.runtime_idle = pm_generic_runtime_idle; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1187 | genpd->domain.ops.prepare = pm_genpd_prepare; |
| 1188 | genpd->domain.ops.suspend = pm_genpd_suspend; |
| 1189 | genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq; |
| 1190 | genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq; |
| 1191 | genpd->domain.ops.resume = pm_genpd_resume; |
| 1192 | genpd->domain.ops.freeze = pm_genpd_freeze; |
| 1193 | genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq; |
| 1194 | genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq; |
| 1195 | genpd->domain.ops.thaw = pm_genpd_thaw; |
| 1196 | genpd->domain.ops.poweroff = pm_genpd_dev_poweroff; |
| 1197 | genpd->domain.ops.poweroff_noirq = pm_genpd_dev_poweroff_noirq; |
| 1198 | genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; |
| 1199 | genpd->domain.ops.restore = pm_genpd_restore; |
| 1200 | genpd->domain.ops.complete = pm_genpd_complete; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1201 | } |