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> |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 14 | #include <linux/pm_qos.h> |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/err.h> |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 17 | #include <linux/sched.h> |
| 18 | #include <linux/suspend.h> |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 19 | #include <linux/export.h> |
| 20 | |
| 21 | #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \ |
| 22 | ({ \ |
| 23 | type (*__routine)(struct device *__d); \ |
| 24 | type __ret = (type)0; \ |
| 25 | \ |
| 26 | __routine = genpd->dev_ops.callback; \ |
| 27 | if (__routine) { \ |
| 28 | __ret = __routine(dev); \ |
| 29 | } else { \ |
| 30 | __routine = dev_gpd_data(dev)->ops.callback; \ |
| 31 | if (__routine) \ |
| 32 | __ret = __routine(dev); \ |
| 33 | } \ |
| 34 | __ret; \ |
| 35 | }) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 36 | |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 37 | #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \ |
| 38 | ({ \ |
| 39 | ktime_t __start = ktime_get(); \ |
| 40 | type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \ |
| 41 | s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \ |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 42 | struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \ |
| 43 | if (!__retval && __elapsed > __td->field) { \ |
| 44 | __td->field = __elapsed; \ |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 45 | dev_warn(dev, name " latency exceeded, new value %lld ns\n", \ |
| 46 | __elapsed); \ |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 47 | genpd->max_off_time_changed = true; \ |
| 48 | __td->constraint_changed = true; \ |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 49 | } \ |
| 50 | __retval; \ |
| 51 | }) |
| 52 | |
Rafael J. Wysocki | 5125bbf | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 53 | static LIST_HEAD(gpd_list); |
| 54 | static DEFINE_MUTEX(gpd_list_lock); |
| 55 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 56 | #ifdef CONFIG_PM |
| 57 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 58 | struct generic_pm_domain *dev_to_genpd(struct device *dev) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 59 | { |
| 60 | if (IS_ERR_OR_NULL(dev->pm_domain)) |
| 61 | return ERR_PTR(-EINVAL); |
| 62 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 63 | return pd_to_genpd(dev->pm_domain); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 64 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 65 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 66 | static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 67 | { |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 68 | return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev, |
| 69 | stop_latency_ns, "stop"); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 73 | { |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 74 | return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev, |
| 75 | start_latency_ns, "start"); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 78 | static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 79 | { |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 80 | bool ret = false; |
| 81 | |
| 82 | if (!WARN_ON(atomic_read(&genpd->sd_count) == 0)) |
| 83 | ret = !!atomic_dec_and_test(&genpd->sd_count); |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static void genpd_sd_counter_inc(struct generic_pm_domain *genpd) |
| 89 | { |
| 90 | atomic_inc(&genpd->sd_count); |
| 91 | smp_mb__after_atomic_inc(); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 94 | static void genpd_acquire_lock(struct generic_pm_domain *genpd) |
| 95 | { |
| 96 | DEFINE_WAIT(wait); |
| 97 | |
| 98 | mutex_lock(&genpd->lock); |
| 99 | /* |
| 100 | * Wait for the domain to transition into either the active, |
| 101 | * or the power off state. |
| 102 | */ |
| 103 | for (;;) { |
| 104 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 105 | TASK_UNINTERRUPTIBLE); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 106 | if (genpd->status == GPD_STATE_ACTIVE |
| 107 | || genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 108 | break; |
| 109 | mutex_unlock(&genpd->lock); |
| 110 | |
| 111 | schedule(); |
| 112 | |
| 113 | mutex_lock(&genpd->lock); |
| 114 | } |
| 115 | finish_wait(&genpd->status_wait_queue, &wait); |
| 116 | } |
| 117 | |
| 118 | static void genpd_release_lock(struct generic_pm_domain *genpd) |
| 119 | { |
| 120 | mutex_unlock(&genpd->lock); |
| 121 | } |
| 122 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 123 | static void genpd_set_active(struct generic_pm_domain *genpd) |
| 124 | { |
| 125 | if (genpd->resume_count == 0) |
| 126 | genpd->status = GPD_STATE_ACTIVE; |
| 127 | } |
| 128 | |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 129 | static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd) |
| 130 | { |
| 131 | s64 usecs64; |
| 132 | |
| 133 | if (!genpd->cpu_data) |
| 134 | return; |
| 135 | |
| 136 | usecs64 = genpd->power_on_latency_ns; |
| 137 | do_div(usecs64, NSEC_PER_USEC); |
| 138 | usecs64 += genpd->cpu_data->saved_exit_latency; |
| 139 | genpd->cpu_data->idle_state->exit_latency = usecs64; |
| 140 | } |
| 141 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 142 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 143 | * __pm_genpd_poweron - Restore power to a given PM domain and its masters. |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 144 | * @genpd: PM domain to power up. |
| 145 | * |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 146 | * Restore power to @genpd and all of its masters so that it is possible to |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 147 | * resume a device belonging to it. |
| 148 | */ |
Sachin Kamat | 8951ef0 | 2012-07-10 21:47:07 +0200 | [diff] [blame] | 149 | static int __pm_genpd_poweron(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 150 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 151 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 152 | struct gpd_link *link; |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 153 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 154 | int ret = 0; |
| 155 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 156 | /* If the domain's master is being waited for, we have to wait too. */ |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 157 | for (;;) { |
| 158 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 159 | TASK_UNINTERRUPTIBLE); |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 160 | if (genpd->status != GPD_STATE_WAIT_MASTER) |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 161 | break; |
| 162 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 163 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 164 | schedule(); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 165 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 166 | mutex_lock(&genpd->lock); |
| 167 | } |
| 168 | finish_wait(&genpd->status_wait_queue, &wait); |
| 169 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 170 | if (genpd->status == GPD_STATE_ACTIVE |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 171 | || (genpd->prepared_count > 0 && genpd->suspend_power_off)) |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 172 | return 0; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 173 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 174 | if (genpd->status != GPD_STATE_POWER_OFF) { |
| 175 | genpd_set_active(genpd); |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 176 | return 0; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 179 | if (genpd->cpu_data) { |
| 180 | cpuidle_pause_and_lock(); |
| 181 | genpd->cpu_data->idle_state->disabled = true; |
| 182 | cpuidle_resume_and_unlock(); |
| 183 | goto out; |
| 184 | } |
| 185 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 186 | /* |
| 187 | * The list is guaranteed not to change while the loop below is being |
| 188 | * executed, unless one of the masters' .power_on() callbacks fiddles |
| 189 | * with it. |
| 190 | */ |
| 191 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 192 | genpd_sd_counter_inc(link->master); |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 193 | genpd->status = GPD_STATE_WAIT_MASTER; |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 194 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 195 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 196 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 197 | ret = pm_genpd_poweron(link->master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 198 | |
| 199 | mutex_lock(&genpd->lock); |
| 200 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 201 | /* |
| 202 | * The "wait for parent" status is guaranteed not to change |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 203 | * while the master is powering on. |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 204 | */ |
| 205 | genpd->status = GPD_STATE_POWER_OFF; |
| 206 | wake_up_all(&genpd->status_wait_queue); |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 207 | if (ret) { |
| 208 | genpd_sd_counter_dec(link->master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 209 | goto err; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 210 | } |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 213 | if (genpd->power_on) { |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 214 | ktime_t time_start = ktime_get(); |
| 215 | s64 elapsed_ns; |
| 216 | |
Rafael J. Wysocki | fe202fd | 2011-08-05 21:45:11 +0200 | [diff] [blame] | 217 | ret = genpd->power_on(genpd); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 218 | if (ret) |
| 219 | goto err; |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 220 | |
| 221 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
Rafael J. Wysocki | e84b2c2 | 2011-12-06 22:19:54 +0100 | [diff] [blame] | 222 | if (elapsed_ns > genpd->power_on_latency_ns) { |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 223 | genpd->power_on_latency_ns = elapsed_ns; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 224 | genpd->max_off_time_changed = true; |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 225 | genpd_recalc_cpu_exit_latency(genpd); |
Rafael J. Wysocki | e84b2c2 | 2011-12-06 22:19:54 +0100 | [diff] [blame] | 226 | if (genpd->name) |
| 227 | pr_warning("%s: Power-on latency exceeded, " |
| 228 | "new value %lld ns\n", genpd->name, |
| 229 | elapsed_ns); |
| 230 | } |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 231 | } |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 232 | |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 233 | out: |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 234 | genpd_set_active(genpd); |
| 235 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 236 | return 0; |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 237 | |
| 238 | err: |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 239 | list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node) |
| 240 | genpd_sd_counter_dec(link->master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 241 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 246 | * pm_genpd_poweron - Restore power to a given PM domain and its masters. |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 247 | * @genpd: PM domain to power up. |
| 248 | */ |
| 249 | int pm_genpd_poweron(struct generic_pm_domain *genpd) |
| 250 | { |
| 251 | int ret; |
| 252 | |
| 253 | mutex_lock(&genpd->lock); |
| 254 | ret = __pm_genpd_poweron(genpd); |
| 255 | mutex_unlock(&genpd->lock); |
| 256 | return ret; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | #endif /* CONFIG_PM */ |
| 260 | |
| 261 | #ifdef CONFIG_PM_RUNTIME |
| 262 | |
Rafael J. Wysocki | 8e9afaf | 2012-07-11 22:42:52 +0200 | [diff] [blame] | 263 | static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 264 | { |
| 265 | return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev, |
| 266 | save_state_latency_ns, "state save"); |
| 267 | } |
| 268 | |
| 269 | static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 270 | { |
| 271 | return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev, |
| 272 | restore_state_latency_ns, |
| 273 | "state restore"); |
| 274 | } |
| 275 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 276 | static int genpd_dev_pm_qos_notifier(struct notifier_block *nb, |
| 277 | unsigned long val, void *ptr) |
| 278 | { |
| 279 | struct generic_pm_domain_data *gpd_data; |
| 280 | struct device *dev; |
| 281 | |
| 282 | gpd_data = container_of(nb, struct generic_pm_domain_data, nb); |
| 283 | |
| 284 | mutex_lock(&gpd_data->lock); |
| 285 | dev = gpd_data->base.dev; |
| 286 | if (!dev) { |
| 287 | mutex_unlock(&gpd_data->lock); |
| 288 | return NOTIFY_DONE; |
| 289 | } |
| 290 | mutex_unlock(&gpd_data->lock); |
| 291 | |
| 292 | for (;;) { |
| 293 | struct generic_pm_domain *genpd; |
| 294 | struct pm_domain_data *pdd; |
| 295 | |
| 296 | spin_lock_irq(&dev->power.lock); |
| 297 | |
| 298 | pdd = dev->power.subsys_data ? |
| 299 | dev->power.subsys_data->domain_data : NULL; |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 300 | if (pdd && pdd->dev) { |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 301 | to_gpd_data(pdd)->td.constraint_changed = true; |
| 302 | genpd = dev_to_genpd(dev); |
| 303 | } else { |
| 304 | genpd = ERR_PTR(-ENODATA); |
| 305 | } |
| 306 | |
| 307 | spin_unlock_irq(&dev->power.lock); |
| 308 | |
| 309 | if (!IS_ERR(genpd)) { |
| 310 | mutex_lock(&genpd->lock); |
| 311 | genpd->max_off_time_changed = true; |
| 312 | mutex_unlock(&genpd->lock); |
| 313 | } |
| 314 | |
| 315 | dev = dev->parent; |
| 316 | if (!dev || dev->power.ignore_children) |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | return NOTIFY_DONE; |
| 321 | } |
| 322 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 323 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 324 | * __pm_genpd_save_device - Save the pre-suspend state of a device. |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 325 | * @pdd: Domain data of the device to save the state of. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 326 | * @genpd: PM domain the device belongs to. |
| 327 | */ |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 328 | static int __pm_genpd_save_device(struct pm_domain_data *pdd, |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 329 | struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 330 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 331 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 332 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 333 | struct device *dev = pdd->dev; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 334 | int ret = 0; |
| 335 | |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 336 | if (gpd_data->need_restore) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 337 | return 0; |
| 338 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 339 | mutex_unlock(&genpd->lock); |
| 340 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 341 | genpd_start_dev(genpd, dev); |
| 342 | ret = genpd_save_dev(genpd, dev); |
| 343 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 344 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 345 | mutex_lock(&genpd->lock); |
| 346 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 347 | if (!ret) |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 348 | gpd_data->need_restore = true; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * __pm_genpd_restore_device - Restore the pre-suspend state of a device. |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 355 | * @pdd: Domain data of the device to restore the state of. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 356 | * @genpd: PM domain the device belongs to. |
| 357 | */ |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 358 | static void __pm_genpd_restore_device(struct pm_domain_data *pdd, |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 359 | struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 360 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 361 | { |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 362 | struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd); |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 363 | struct device *dev = pdd->dev; |
Rafael J. Wysocki | 80de3d7 | 2012-06-16 00:02:34 +0200 | [diff] [blame] | 364 | bool need_restore = gpd_data->need_restore; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 365 | |
Rafael J. Wysocki | 80de3d7 | 2012-06-16 00:02:34 +0200 | [diff] [blame] | 366 | gpd_data->need_restore = false; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 367 | mutex_unlock(&genpd->lock); |
| 368 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 369 | genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 80de3d7 | 2012-06-16 00:02:34 +0200 | [diff] [blame] | 370 | if (need_restore) |
| 371 | genpd_restore_dev(genpd, dev); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 372 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 373 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 377 | * genpd_abort_poweroff - Check if a PM domain power off should be aborted. |
| 378 | * @genpd: PM domain to check. |
| 379 | * |
| 380 | * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during |
| 381 | * a "power off" operation, which means that a "power on" has occured in the |
| 382 | * meantime, or if its resume_count field is different from zero, which means |
| 383 | * that one of its devices has been resumed in the meantime. |
| 384 | */ |
| 385 | static bool genpd_abort_poweroff(struct generic_pm_domain *genpd) |
| 386 | { |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 387 | return genpd->status == GPD_STATE_WAIT_MASTER |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 388 | || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Rafael J. Wysocki | 56375fd | 2011-07-12 00:40:03 +0200 | [diff] [blame] | 392 | * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff(). |
| 393 | * @genpd: PM domait to power off. |
| 394 | * |
| 395 | * Queue up the execution of pm_genpd_poweroff() unless it's already been done |
| 396 | * before. |
| 397 | */ |
Rafael J. Wysocki | 0bc5b2d | 2011-07-14 20:59:07 +0200 | [diff] [blame] | 398 | void genpd_queue_power_off_work(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 56375fd | 2011-07-12 00:40:03 +0200 | [diff] [blame] | 399 | { |
| 400 | if (!work_pending(&genpd->power_off_work)) |
| 401 | queue_work(pm_wq, &genpd->power_off_work); |
| 402 | } |
| 403 | |
| 404 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 405 | * pm_genpd_poweroff - Remove power from a given PM domain. |
| 406 | * @genpd: PM domain to power down. |
| 407 | * |
| 408 | * If all of the @genpd's devices have been suspended and all of its subdomains |
| 409 | * have been powered down, run the runtime suspend callbacks provided by all of |
| 410 | * the @genpd's devices' drivers and remove power from @genpd. |
| 411 | */ |
| 412 | static int pm_genpd_poweroff(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 413 | __releases(&genpd->lock) __acquires(&genpd->lock) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 414 | { |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 415 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 416 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 417 | unsigned int not_suspended; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 418 | int ret = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 419 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 420 | start: |
| 421 | /* |
| 422 | * Do not try to power off the domain in the following situations: |
| 423 | * (1) The domain is already in the "power off" state. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 424 | * (2) The domain is waiting for its master to power up. |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 425 | * (3) One of the domain's devices is being resumed right now. |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 426 | * (4) System suspend is in progress. |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 427 | */ |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 428 | if (genpd->status == GPD_STATE_POWER_OFF |
Rafael J. Wysocki | 17877eb | 2011-08-08 23:43:50 +0200 | [diff] [blame] | 429 | || genpd->status == GPD_STATE_WAIT_MASTER |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 430 | || genpd->resume_count > 0 || genpd->prepared_count > 0) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 433 | if (atomic_read(&genpd->sd_count) > 0) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 434 | return -EBUSY; |
| 435 | |
| 436 | not_suspended = 0; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 437 | list_for_each_entry(pdd, &genpd->dev_list, list_node) |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 438 | if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev) |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame^] | 439 | || pdd->dev->power.irq_safe || pdd->dev->power.syscore)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 440 | not_suspended++; |
| 441 | |
| 442 | if (not_suspended > genpd->in_progress) |
| 443 | return -EBUSY; |
| 444 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 445 | if (genpd->poweroff_task) { |
| 446 | /* |
| 447 | * Another instance of pm_genpd_poweroff() is executing |
| 448 | * callbacks, so tell it to start over and return. |
| 449 | */ |
| 450 | genpd->status = GPD_STATE_REPEAT; |
| 451 | return 0; |
| 452 | } |
| 453 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 454 | if (genpd->gov && genpd->gov->power_down_ok) { |
| 455 | if (!genpd->gov->power_down_ok(&genpd->domain)) |
| 456 | return -EAGAIN; |
| 457 | } |
| 458 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 459 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 460 | genpd->poweroff_task = current; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 461 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 462 | list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) { |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 463 | ret = atomic_read(&genpd->sd_count) == 0 ? |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 464 | __pm_genpd_save_device(pdd, genpd) : -EBUSY; |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 465 | |
| 466 | if (genpd_abort_poweroff(genpd)) |
| 467 | goto out; |
| 468 | |
Rafael J. Wysocki | 697a7f3 | 2011-07-12 00:39:48 +0200 | [diff] [blame] | 469 | if (ret) { |
| 470 | genpd_set_active(genpd); |
| 471 | goto out; |
| 472 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 473 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 474 | if (genpd->status == GPD_STATE_REPEAT) { |
| 475 | genpd->poweroff_task = NULL; |
| 476 | goto start; |
| 477 | } |
| 478 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 479 | |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 480 | if (genpd->cpu_data) { |
| 481 | /* |
| 482 | * If cpu_data is set, cpuidle should turn the domain off when |
| 483 | * the CPU in it is idle. In that case we don't decrement the |
| 484 | * subdomain counts of the master domains, so that power is not |
| 485 | * removed from the current domain prematurely as a result of |
| 486 | * cutting off the masters' power. |
| 487 | */ |
| 488 | genpd->status = GPD_STATE_POWER_OFF; |
| 489 | cpuidle_pause_and_lock(); |
| 490 | genpd->cpu_data->idle_state->disabled = false; |
| 491 | cpuidle_resume_and_unlock(); |
| 492 | goto out; |
| 493 | } |
| 494 | |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 495 | if (genpd->power_off) { |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 496 | ktime_t time_start; |
| 497 | s64 elapsed_ns; |
| 498 | |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 499 | if (atomic_read(&genpd->sd_count) > 0) { |
| 500 | ret = -EBUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 501 | goto out; |
| 502 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 503 | |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 504 | time_start = ktime_get(); |
| 505 | |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 506 | /* |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 507 | * If sd_count > 0 at this point, one of the subdomains hasn't |
| 508 | * managed to call pm_genpd_poweron() for the master yet after |
Rafael J. Wysocki | 3c07cbc | 2011-08-08 23:43:14 +0200 | [diff] [blame] | 509 | * incrementing it. In that case pm_genpd_poweron() will wait |
| 510 | * for us to drop the lock, so we can call .power_off() and let |
| 511 | * the pm_genpd_poweron() restore power for us (this shouldn't |
| 512 | * happen very often). |
| 513 | */ |
Rafael J. Wysocki | d280540 | 2011-07-14 20:59:20 +0200 | [diff] [blame] | 514 | ret = genpd->power_off(genpd); |
| 515 | if (ret == -EBUSY) { |
| 516 | genpd_set_active(genpd); |
Rafael J. Wysocki | d280540 | 2011-07-14 20:59:20 +0200 | [diff] [blame] | 517 | goto out; |
| 518 | } |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 519 | |
| 520 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
Rafael J. Wysocki | e84b2c2 | 2011-12-06 22:19:54 +0100 | [diff] [blame] | 521 | if (elapsed_ns > genpd->power_off_latency_ns) { |
Rafael J. Wysocki | 0140d8b | 2011-12-01 00:02:17 +0100 | [diff] [blame] | 522 | genpd->power_off_latency_ns = elapsed_ns; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 523 | genpd->max_off_time_changed = true; |
Rafael J. Wysocki | e84b2c2 | 2011-12-06 22:19:54 +0100 | [diff] [blame] | 524 | if (genpd->name) |
| 525 | pr_warning("%s: Power-off latency exceeded, " |
| 526 | "new value %lld ns\n", genpd->name, |
| 527 | elapsed_ns); |
| 528 | } |
Rafael J. Wysocki | d280540 | 2011-07-14 20:59:20 +0200 | [diff] [blame] | 529 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 530 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 531 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 532 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 533 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 534 | genpd_sd_counter_dec(link->master); |
| 535 | genpd_queue_power_off_work(link->master); |
| 536 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 537 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 538 | out: |
| 539 | genpd->poweroff_task = NULL; |
| 540 | wake_up_all(&genpd->status_wait_queue); |
| 541 | return ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0. |
| 546 | * @work: Work structure used for scheduling the execution of this function. |
| 547 | */ |
| 548 | static void genpd_power_off_work_fn(struct work_struct *work) |
| 549 | { |
| 550 | struct generic_pm_domain *genpd; |
| 551 | |
| 552 | genpd = container_of(work, struct generic_pm_domain, power_off_work); |
| 553 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 554 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 555 | pm_genpd_poweroff(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 556 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /** |
| 560 | * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain. |
| 561 | * @dev: Device to suspend. |
| 562 | * |
| 563 | * Carry out a runtime suspend of a device under the assumption that its |
| 564 | * pm_domain field points to the domain member of an object of type |
| 565 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 566 | */ |
| 567 | static int pm_genpd_runtime_suspend(struct device *dev) |
| 568 | { |
| 569 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 570 | bool (*stop_ok)(struct device *__dev); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 571 | int ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 572 | |
| 573 | dev_dbg(dev, "%s()\n", __func__); |
| 574 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 575 | genpd = dev_to_genpd(dev); |
| 576 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 577 | return -EINVAL; |
| 578 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 579 | might_sleep_if(!genpd->dev_irq_safe); |
| 580 | |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 581 | stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL; |
| 582 | if (stop_ok && !stop_ok(dev)) |
| 583 | return -EBUSY; |
| 584 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 585 | ret = genpd_stop_dev(genpd, dev); |
| 586 | if (ret) |
| 587 | return ret; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 588 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 589 | /* |
| 590 | * If power.irq_safe is set, this routine will be run with interrupts |
| 591 | * off, so it can't use mutexes. |
| 592 | */ |
| 593 | if (dev->power.irq_safe) |
| 594 | return 0; |
| 595 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 596 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 597 | genpd->in_progress++; |
| 598 | pm_genpd_poweroff(genpd); |
| 599 | genpd->in_progress--; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 600 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 606 | * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
| 607 | * @dev: Device to resume. |
| 608 | * |
| 609 | * Carry out a runtime resume of a device under the assumption that its |
| 610 | * pm_domain field points to the domain member of an object of type |
| 611 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 612 | */ |
| 613 | static int pm_genpd_runtime_resume(struct device *dev) |
| 614 | { |
| 615 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 616 | DEFINE_WAIT(wait); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 617 | int ret; |
| 618 | |
| 619 | dev_dbg(dev, "%s()\n", __func__); |
| 620 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 621 | genpd = dev_to_genpd(dev); |
| 622 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 623 | return -EINVAL; |
| 624 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 625 | might_sleep_if(!genpd->dev_irq_safe); |
| 626 | |
| 627 | /* If power.irq_safe, the PM domain is never powered off. */ |
| 628 | if (dev->power.irq_safe) |
Rafael J. Wysocki | 80de3d7 | 2012-06-16 00:02:34 +0200 | [diff] [blame] | 629 | return genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 630 | |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 631 | mutex_lock(&genpd->lock); |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 632 | ret = __pm_genpd_poweron(genpd); |
| 633 | if (ret) { |
| 634 | mutex_unlock(&genpd->lock); |
| 635 | return ret; |
| 636 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 637 | genpd->status = GPD_STATE_BUSY; |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 638 | genpd->resume_count++; |
| 639 | for (;;) { |
| 640 | prepare_to_wait(&genpd->status_wait_queue, &wait, |
| 641 | TASK_UNINTERRUPTIBLE); |
| 642 | /* |
| 643 | * If current is the powering off task, we have been called |
| 644 | * reentrantly from one of the device callbacks, so we should |
| 645 | * not wait. |
| 646 | */ |
| 647 | if (!genpd->poweroff_task || genpd->poweroff_task == current) |
| 648 | break; |
| 649 | mutex_unlock(&genpd->lock); |
| 650 | |
| 651 | schedule(); |
| 652 | |
| 653 | mutex_lock(&genpd->lock); |
| 654 | } |
| 655 | finish_wait(&genpd->status_wait_queue, &wait); |
Rafael J. Wysocki | cd0ea67 | 2011-09-26 20:22:02 +0200 | [diff] [blame] | 656 | __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 657 | genpd->resume_count--; |
| 658 | genpd_set_active(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 659 | wake_up_all(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 660 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 661 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 665 | /** |
| 666 | * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use. |
| 667 | */ |
| 668 | void pm_genpd_poweroff_unused(void) |
| 669 | { |
| 670 | struct generic_pm_domain *genpd; |
| 671 | |
| 672 | mutex_lock(&gpd_list_lock); |
| 673 | |
| 674 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) |
| 675 | genpd_queue_power_off_work(genpd); |
| 676 | |
| 677 | mutex_unlock(&gpd_list_lock); |
| 678 | } |
| 679 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 680 | #else |
| 681 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 682 | static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb, |
| 683 | unsigned long val, void *ptr) |
| 684 | { |
| 685 | return NOTIFY_DONE; |
| 686 | } |
| 687 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 688 | static inline void genpd_power_off_work_fn(struct work_struct *work) {} |
| 689 | |
| 690 | #define pm_genpd_runtime_suspend NULL |
| 691 | #define pm_genpd_runtime_resume NULL |
| 692 | |
| 693 | #endif /* CONFIG_PM_RUNTIME */ |
| 694 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 695 | #ifdef CONFIG_PM_SLEEP |
| 696 | |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 697 | /** |
| 698 | * pm_genpd_present - Check if the given PM domain has been initialized. |
| 699 | * @genpd: PM domain to check. |
| 700 | */ |
| 701 | static bool pm_genpd_present(struct generic_pm_domain *genpd) |
| 702 | { |
| 703 | struct generic_pm_domain *gpd; |
| 704 | |
| 705 | if (IS_ERR_OR_NULL(genpd)) |
| 706 | return false; |
| 707 | |
| 708 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) |
| 709 | if (gpd == genpd) |
| 710 | return true; |
| 711 | |
| 712 | return false; |
| 713 | } |
| 714 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 715 | static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd, |
| 716 | struct device *dev) |
| 717 | { |
| 718 | return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev); |
| 719 | } |
| 720 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 721 | static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 722 | { |
| 723 | return GENPD_DEV_CALLBACK(genpd, int, suspend, dev); |
| 724 | } |
| 725 | |
| 726 | static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev) |
| 727 | { |
| 728 | return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev); |
| 729 | } |
| 730 | |
| 731 | static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev) |
| 732 | { |
| 733 | return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev); |
| 734 | } |
| 735 | |
| 736 | static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 737 | { |
| 738 | return GENPD_DEV_CALLBACK(genpd, int, resume, dev); |
| 739 | } |
| 740 | |
| 741 | static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 742 | { |
| 743 | return GENPD_DEV_CALLBACK(genpd, int, freeze, dev); |
| 744 | } |
| 745 | |
| 746 | static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev) |
| 747 | { |
| 748 | return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev); |
| 749 | } |
| 750 | |
| 751 | static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev) |
| 752 | { |
| 753 | return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev); |
| 754 | } |
| 755 | |
| 756 | static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev) |
| 757 | { |
| 758 | return GENPD_DEV_CALLBACK(genpd, int, thaw, dev); |
| 759 | } |
| 760 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 761 | /** |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 762 | * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 763 | * @genpd: PM domain to power off, if possible. |
| 764 | * |
| 765 | * Check if the given PM domain can be powered off (during system suspend or |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 766 | * hibernation) and do that if so. Also, in that case propagate to its masters. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 767 | * |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 768 | * This function is only called in "noirq" and "syscore" stages of system power |
| 769 | * transitions, so it need not acquire locks (all of the "noirq" callbacks are |
| 770 | * executed sequentially, so it is guaranteed that it will never run twice in |
| 771 | * parallel). |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 772 | */ |
| 773 | static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd) |
| 774 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 775 | struct gpd_link *link; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 776 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 777 | if (genpd->status == GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 778 | return; |
| 779 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 780 | if (genpd->suspended_count != genpd->device_count |
| 781 | || atomic_read(&genpd->sd_count) > 0) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 782 | return; |
| 783 | |
| 784 | if (genpd->power_off) |
| 785 | genpd->power_off(genpd); |
| 786 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 787 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 788 | |
| 789 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 790 | genpd_sd_counter_dec(link->master); |
| 791 | pm_genpd_sync_poweroff(link->master); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | /** |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 796 | * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters. |
| 797 | * @genpd: PM domain to power on. |
| 798 | * |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 799 | * This function is only called in "noirq" and "syscore" stages of system power |
| 800 | * transitions, so it need not acquire locks (all of the "noirq" callbacks are |
| 801 | * executed sequentially, so it is guaranteed that it will never run twice in |
| 802 | * parallel). |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 803 | */ |
| 804 | static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd) |
| 805 | { |
| 806 | struct gpd_link *link; |
| 807 | |
| 808 | if (genpd->status != GPD_STATE_POWER_OFF) |
| 809 | return; |
| 810 | |
| 811 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 812 | pm_genpd_sync_poweron(link->master); |
| 813 | genpd_sd_counter_inc(link->master); |
| 814 | } |
| 815 | |
| 816 | if (genpd->power_on) |
| 817 | genpd->power_on(genpd); |
| 818 | |
| 819 | genpd->status = GPD_STATE_ACTIVE; |
| 820 | } |
| 821 | |
| 822 | /** |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 823 | * resume_needed - Check whether to resume a device before system suspend. |
| 824 | * @dev: Device to check. |
| 825 | * @genpd: PM domain the device belongs to. |
| 826 | * |
| 827 | * There are two cases in which a device that can wake up the system from sleep |
| 828 | * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled |
| 829 | * to wake up the system and it has to remain active for this purpose while the |
| 830 | * system is in the sleep state and (2) if the device is not enabled to wake up |
| 831 | * the system from sleep states and it generally doesn't generate wakeup signals |
| 832 | * by itself (those signals are generated on its behalf by other parts of the |
| 833 | * system). In the latter case it may be necessary to reconfigure the device's |
| 834 | * wakeup settings during system suspend, because it may have been set up to |
| 835 | * signal remote wakeup from the system's working state as needed by runtime PM. |
| 836 | * Return 'true' in either of the above cases. |
| 837 | */ |
| 838 | static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd) |
| 839 | { |
| 840 | bool active_wakeup; |
| 841 | |
| 842 | if (!device_can_wakeup(dev)) |
| 843 | return false; |
| 844 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 845 | active_wakeup = genpd_dev_active_wakeup(genpd, dev); |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 846 | return device_may_wakeup(dev) ? active_wakeup : !active_wakeup; |
| 847 | } |
| 848 | |
| 849 | /** |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 850 | * pm_genpd_prepare - Start power transition of a device in a PM domain. |
| 851 | * @dev: Device to start the transition of. |
| 852 | * |
| 853 | * Start a power transition of a device (during a system-wide power transition) |
| 854 | * under the assumption that its pm_domain field points to the domain member of |
| 855 | * an object of type struct generic_pm_domain representing a PM domain |
| 856 | * consisting of I/O devices. |
| 857 | */ |
| 858 | static int pm_genpd_prepare(struct device *dev) |
| 859 | { |
| 860 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 861 | int ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 862 | |
| 863 | dev_dbg(dev, "%s()\n", __func__); |
| 864 | |
| 865 | genpd = dev_to_genpd(dev); |
| 866 | if (IS_ERR(genpd)) |
| 867 | return -EINVAL; |
| 868 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 869 | /* |
| 870 | * If a wakeup request is pending for the device, it should be woken up |
| 871 | * at this point and a system wakeup event should be reported if it's |
| 872 | * set up to wake up the system from sleep states. |
| 873 | */ |
| 874 | pm_runtime_get_noresume(dev); |
| 875 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 876 | pm_wakeup_event(dev, 0); |
| 877 | |
| 878 | if (pm_wakeup_pending()) { |
| 879 | pm_runtime_put_sync(dev); |
| 880 | return -EBUSY; |
| 881 | } |
| 882 | |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 883 | if (resume_needed(dev, genpd)) |
| 884 | pm_runtime_resume(dev); |
| 885 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 886 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 887 | |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 888 | if (genpd->prepared_count++ == 0) { |
| 889 | genpd->suspended_count = 0; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 890 | genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 891 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 892 | |
| 893 | genpd_release_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 894 | |
| 895 | if (genpd->suspend_power_off) { |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 896 | pm_runtime_put_noidle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | /* |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 901 | * The PM domain must be in the GPD_STATE_ACTIVE state at this point, |
| 902 | * so pm_genpd_poweron() will return immediately, but if the device |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 903 | * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 904 | * to make it operational. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 905 | */ |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 906 | pm_runtime_resume(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 907 | __pm_runtime_disable(dev, false); |
| 908 | |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 909 | ret = pm_generic_prepare(dev); |
| 910 | if (ret) { |
| 911 | mutex_lock(&genpd->lock); |
| 912 | |
| 913 | if (--genpd->prepared_count == 0) |
| 914 | genpd->suspend_power_off = false; |
| 915 | |
| 916 | mutex_unlock(&genpd->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 917 | pm_runtime_enable(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 918 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 919 | |
| 920 | pm_runtime_put_sync(dev); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 921 | return ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /** |
| 925 | * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain. |
| 926 | * @dev: Device to suspend. |
| 927 | * |
| 928 | * Suspend a device under the assumption that its pm_domain field points to the |
| 929 | * domain member of an object of type struct generic_pm_domain representing |
| 930 | * a PM domain consisting of I/O devices. |
| 931 | */ |
| 932 | static int pm_genpd_suspend(struct device *dev) |
| 933 | { |
| 934 | struct generic_pm_domain *genpd; |
| 935 | |
| 936 | dev_dbg(dev, "%s()\n", __func__); |
| 937 | |
| 938 | genpd = dev_to_genpd(dev); |
| 939 | if (IS_ERR(genpd)) |
| 940 | return -EINVAL; |
| 941 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 942 | return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 946 | * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 947 | * @dev: Device to suspend. |
| 948 | * |
| 949 | * Carry out a late suspend of a device under the assumption that its |
| 950 | * pm_domain field points to the domain member of an object of type |
| 951 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 952 | */ |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 953 | static int pm_genpd_suspend_late(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 954 | { |
| 955 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 956 | |
| 957 | dev_dbg(dev, "%s()\n", __func__); |
| 958 | |
| 959 | genpd = dev_to_genpd(dev); |
| 960 | if (IS_ERR(genpd)) |
| 961 | return -EINVAL; |
| 962 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 963 | return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev); |
| 964 | } |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 965 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 966 | /** |
| 967 | * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain. |
| 968 | * @dev: Device to suspend. |
| 969 | * |
| 970 | * Stop the device and remove power from the domain if all devices in it have |
| 971 | * been stopped. |
| 972 | */ |
| 973 | static int pm_genpd_suspend_noirq(struct device *dev) |
| 974 | { |
| 975 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 976 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 977 | dev_dbg(dev, "%s()\n", __func__); |
| 978 | |
| 979 | genpd = dev_to_genpd(dev); |
| 980 | if (IS_ERR(genpd)) |
| 981 | return -EINVAL; |
| 982 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame^] | 983 | if (genpd->suspend_power_off |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 984 | || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))) |
Rafael J. Wysocki | d4f2d87 | 2011-07-01 22:13:29 +0200 | [diff] [blame] | 985 | return 0; |
| 986 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 987 | genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 988 | |
| 989 | /* |
| 990 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 991 | * guaranteed that this function will never run twice in parallel for |
| 992 | * the same PM domain, so it is not necessary to use locking here. |
| 993 | */ |
| 994 | genpd->suspended_count++; |
| 995 | pm_genpd_sync_poweroff(genpd); |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1001 | * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1002 | * @dev: Device to resume. |
| 1003 | * |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1004 | * Restore power to the device's PM domain, if necessary, and start the device. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1005 | */ |
| 1006 | static int pm_genpd_resume_noirq(struct device *dev) |
| 1007 | { |
| 1008 | struct generic_pm_domain *genpd; |
| 1009 | |
| 1010 | dev_dbg(dev, "%s()\n", __func__); |
| 1011 | |
| 1012 | genpd = dev_to_genpd(dev); |
| 1013 | if (IS_ERR(genpd)) |
| 1014 | return -EINVAL; |
| 1015 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame^] | 1016 | if (genpd->suspend_power_off |
Rafael J. Wysocki | cc85b20 | 2012-03-13 22:39:31 +0100 | [diff] [blame] | 1017 | || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev))) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1018 | return 0; |
| 1019 | |
| 1020 | /* |
| 1021 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 1022 | * guaranteed that this function will never run twice in parallel for |
| 1023 | * the same PM domain, so it is not necessary to use locking here. |
| 1024 | */ |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 1025 | pm_genpd_sync_poweron(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1026 | genpd->suspended_count--; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1027 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1028 | return genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1032 | * pm_genpd_resume_early - Early resume of a device in an I/O PM domain. |
| 1033 | * @dev: Device to resume. |
| 1034 | * |
| 1035 | * Carry out an early resume of a device under the assumption that its |
| 1036 | * pm_domain field points to the domain member of an object of type |
| 1037 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 1038 | * devices. |
| 1039 | */ |
| 1040 | static int pm_genpd_resume_early(struct device *dev) |
| 1041 | { |
| 1042 | struct generic_pm_domain *genpd; |
| 1043 | |
| 1044 | dev_dbg(dev, "%s()\n", __func__); |
| 1045 | |
| 1046 | genpd = dev_to_genpd(dev); |
| 1047 | if (IS_ERR(genpd)) |
| 1048 | return -EINVAL; |
| 1049 | |
| 1050 | return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev); |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * pm_genpd_resume - Resume of device in an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1055 | * @dev: Device to resume. |
| 1056 | * |
| 1057 | * Resume a device under the assumption that its pm_domain field points to the |
| 1058 | * domain member of an object of type struct generic_pm_domain representing |
| 1059 | * a power domain consisting of I/O devices. |
| 1060 | */ |
| 1061 | static int pm_genpd_resume(struct device *dev) |
| 1062 | { |
| 1063 | struct generic_pm_domain *genpd; |
| 1064 | |
| 1065 | dev_dbg(dev, "%s()\n", __func__); |
| 1066 | |
| 1067 | genpd = dev_to_genpd(dev); |
| 1068 | if (IS_ERR(genpd)) |
| 1069 | return -EINVAL; |
| 1070 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1071 | return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1075 | * pm_genpd_freeze - Freezing a device in an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1076 | * @dev: Device to freeze. |
| 1077 | * |
| 1078 | * Freeze a device under the assumption that its pm_domain field points to the |
| 1079 | * domain member of an object of type struct generic_pm_domain representing |
| 1080 | * a power domain consisting of I/O devices. |
| 1081 | */ |
| 1082 | static int pm_genpd_freeze(struct device *dev) |
| 1083 | { |
| 1084 | struct generic_pm_domain *genpd; |
| 1085 | |
| 1086 | dev_dbg(dev, "%s()\n", __func__); |
| 1087 | |
| 1088 | genpd = dev_to_genpd(dev); |
| 1089 | if (IS_ERR(genpd)) |
| 1090 | return -EINVAL; |
| 1091 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1092 | return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1096 | * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain. |
| 1097 | * @dev: Device to freeze. |
| 1098 | * |
| 1099 | * Carry out a late freeze of a device under the assumption that its |
| 1100 | * pm_domain field points to the domain member of an object of type |
| 1101 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 1102 | * devices. |
| 1103 | */ |
| 1104 | static int pm_genpd_freeze_late(struct device *dev) |
| 1105 | { |
| 1106 | struct generic_pm_domain *genpd; |
| 1107 | |
| 1108 | dev_dbg(dev, "%s()\n", __func__); |
| 1109 | |
| 1110 | genpd = dev_to_genpd(dev); |
| 1111 | if (IS_ERR(genpd)) |
| 1112 | return -EINVAL; |
| 1113 | |
| 1114 | return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev); |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1119 | * @dev: Device to freeze. |
| 1120 | * |
| 1121 | * Carry out a late freeze of a device under the assumption that its |
| 1122 | * pm_domain field points to the domain member of an object of type |
| 1123 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 1124 | * devices. |
| 1125 | */ |
| 1126 | static int pm_genpd_freeze_noirq(struct device *dev) |
| 1127 | { |
| 1128 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1129 | |
| 1130 | dev_dbg(dev, "%s()\n", __func__); |
| 1131 | |
| 1132 | genpd = dev_to_genpd(dev); |
| 1133 | if (IS_ERR(genpd)) |
| 1134 | return -EINVAL; |
| 1135 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame^] | 1136 | return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1140 | * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1141 | * @dev: Device to thaw. |
| 1142 | * |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1143 | * Start the device, unless power has been removed from the domain already |
| 1144 | * before the system transition. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1145 | */ |
| 1146 | static int pm_genpd_thaw_noirq(struct device *dev) |
| 1147 | { |
| 1148 | struct generic_pm_domain *genpd; |
| 1149 | |
| 1150 | dev_dbg(dev, "%s()\n", __func__); |
| 1151 | |
| 1152 | genpd = dev_to_genpd(dev); |
| 1153 | if (IS_ERR(genpd)) |
| 1154 | return -EINVAL; |
| 1155 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame^] | 1156 | return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1157 | } |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1158 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1159 | /** |
| 1160 | * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain. |
| 1161 | * @dev: Device to thaw. |
| 1162 | * |
| 1163 | * Carry out an early thaw of a device under the assumption that its |
| 1164 | * pm_domain field points to the domain member of an object of type |
| 1165 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 1166 | * devices. |
| 1167 | */ |
| 1168 | static int pm_genpd_thaw_early(struct device *dev) |
| 1169 | { |
| 1170 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1171 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1172 | dev_dbg(dev, "%s()\n", __func__); |
| 1173 | |
| 1174 | genpd = dev_to_genpd(dev); |
| 1175 | if (IS_ERR(genpd)) |
| 1176 | return -EINVAL; |
| 1177 | |
| 1178 | return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * pm_genpd_thaw - Thaw a device belonging to an I/O power domain. |
| 1183 | * @dev: Device to thaw. |
| 1184 | * |
| 1185 | * Thaw a device under the assumption that its pm_domain field points to the |
| 1186 | * domain member of an object of type struct generic_pm_domain representing |
| 1187 | * a power domain consisting of I/O devices. |
| 1188 | */ |
| 1189 | static int pm_genpd_thaw(struct device *dev) |
| 1190 | { |
| 1191 | struct generic_pm_domain *genpd; |
| 1192 | |
| 1193 | dev_dbg(dev, "%s()\n", __func__); |
| 1194 | |
| 1195 | genpd = dev_to_genpd(dev); |
| 1196 | if (IS_ERR(genpd)) |
| 1197 | return -EINVAL; |
| 1198 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1199 | return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /** |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1203 | * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1204 | * @dev: Device to resume. |
| 1205 | * |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1206 | * Make sure the domain will be in the same power state as before the |
| 1207 | * hibernation the system is resuming from and start the device if necessary. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1208 | */ |
| 1209 | static int pm_genpd_restore_noirq(struct device *dev) |
| 1210 | { |
| 1211 | struct generic_pm_domain *genpd; |
| 1212 | |
| 1213 | dev_dbg(dev, "%s()\n", __func__); |
| 1214 | |
| 1215 | genpd = dev_to_genpd(dev); |
| 1216 | if (IS_ERR(genpd)) |
| 1217 | return -EINVAL; |
| 1218 | |
| 1219 | /* |
| 1220 | * Since all of the "noirq" callbacks are executed sequentially, it is |
| 1221 | * guaranteed that this function will never run twice in parallel for |
| 1222 | * the same PM domain, so it is not necessary to use locking here. |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1223 | * |
| 1224 | * At this point suspended_count == 0 means we are being run for the |
| 1225 | * first time for the given domain in the present cycle. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1226 | */ |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1227 | if (genpd->suspended_count++ == 0) { |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1228 | /* |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1229 | * The boot kernel might put the domain into arbitrary state, |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 1230 | * so make it appear as powered off to pm_genpd_sync_poweron(), |
| 1231 | * so that it tries to power it on in case it was really off. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1232 | */ |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1233 | genpd->status = GPD_STATE_POWER_OFF; |
| 1234 | if (genpd->suspend_power_off) { |
| 1235 | /* |
| 1236 | * If the domain was off before the hibernation, make |
| 1237 | * sure it will be off going forward. |
| 1238 | */ |
| 1239 | if (genpd->power_off) |
| 1240 | genpd->power_off(genpd); |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Rafael J. Wysocki | 18dd2ec | 2012-03-19 10:38:14 +0100 | [diff] [blame] | 1246 | if (genpd->suspend_power_off) |
| 1247 | return 0; |
| 1248 | |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 1249 | pm_genpd_sync_poweron(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1250 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame^] | 1251 | return genpd_start_dev(genpd, dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * pm_genpd_complete - Complete power transition of a device in a power domain. |
| 1256 | * @dev: Device to complete the transition of. |
| 1257 | * |
| 1258 | * Complete a power transition of a device (during a system-wide power |
| 1259 | * transition) under the assumption that its pm_domain field points to the |
| 1260 | * domain member of an object of type struct generic_pm_domain representing |
| 1261 | * a power domain consisting of I/O devices. |
| 1262 | */ |
| 1263 | static void pm_genpd_complete(struct device *dev) |
| 1264 | { |
| 1265 | struct generic_pm_domain *genpd; |
| 1266 | bool run_complete; |
| 1267 | |
| 1268 | dev_dbg(dev, "%s()\n", __func__); |
| 1269 | |
| 1270 | genpd = dev_to_genpd(dev); |
| 1271 | if (IS_ERR(genpd)) |
| 1272 | return; |
| 1273 | |
| 1274 | mutex_lock(&genpd->lock); |
| 1275 | |
| 1276 | run_complete = !genpd->suspend_power_off; |
| 1277 | if (--genpd->prepared_count == 0) |
| 1278 | genpd->suspend_power_off = false; |
| 1279 | |
| 1280 | mutex_unlock(&genpd->lock); |
| 1281 | |
| 1282 | if (run_complete) { |
| 1283 | pm_generic_complete(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1284 | pm_runtime_set_active(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1285 | pm_runtime_enable(dev); |
Rafael J. Wysocki | 6f00ff7 | 2011-07-12 00:39:10 +0200 | [diff] [blame] | 1286 | pm_runtime_idle(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1290 | /** |
| 1291 | * pm_genpd_syscore_switch - Switch power during system core suspend or resume. |
| 1292 | * @dev: Device that normally is marked as "always on" to switch power for. |
| 1293 | * |
| 1294 | * This routine may only be called during the system core (syscore) suspend or |
| 1295 | * resume phase for devices whose "always on" flags are set. |
| 1296 | */ |
| 1297 | void pm_genpd_syscore_switch(struct device *dev, bool suspend) |
| 1298 | { |
| 1299 | struct generic_pm_domain *genpd; |
| 1300 | |
| 1301 | genpd = dev_to_genpd(dev); |
| 1302 | if (!pm_genpd_present(genpd)) |
| 1303 | return; |
| 1304 | |
| 1305 | if (suspend) { |
| 1306 | genpd->suspended_count++; |
| 1307 | pm_genpd_sync_poweroff(genpd); |
| 1308 | } else { |
| 1309 | pm_genpd_sync_poweron(genpd); |
| 1310 | genpd->suspended_count--; |
| 1311 | } |
| 1312 | } |
| 1313 | EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch); |
| 1314 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1315 | #else |
| 1316 | |
| 1317 | #define pm_genpd_prepare NULL |
| 1318 | #define pm_genpd_suspend NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1319 | #define pm_genpd_suspend_late NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1320 | #define pm_genpd_suspend_noirq NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1321 | #define pm_genpd_resume_early NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1322 | #define pm_genpd_resume_noirq NULL |
| 1323 | #define pm_genpd_resume NULL |
| 1324 | #define pm_genpd_freeze NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1325 | #define pm_genpd_freeze_late NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1326 | #define pm_genpd_freeze_noirq NULL |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1327 | #define pm_genpd_thaw_early NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1328 | #define pm_genpd_thaw_noirq NULL |
| 1329 | #define pm_genpd_thaw NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1330 | #define pm_genpd_restore_noirq NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1331 | #define pm_genpd_complete NULL |
| 1332 | |
| 1333 | #endif /* CONFIG_PM_SLEEP */ |
| 1334 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1335 | static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev) |
| 1336 | { |
| 1337 | struct generic_pm_domain_data *gpd_data; |
| 1338 | |
| 1339 | gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL); |
| 1340 | if (!gpd_data) |
| 1341 | return NULL; |
| 1342 | |
| 1343 | mutex_init(&gpd_data->lock); |
| 1344 | gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier; |
| 1345 | dev_pm_qos_add_notifier(dev, &gpd_data->nb); |
| 1346 | return gpd_data; |
| 1347 | } |
| 1348 | |
| 1349 | static void __pm_genpd_free_dev_data(struct device *dev, |
| 1350 | struct generic_pm_domain_data *gpd_data) |
| 1351 | { |
| 1352 | dev_pm_qos_remove_notifier(dev, &gpd_data->nb); |
| 1353 | kfree(gpd_data); |
| 1354 | } |
| 1355 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1356 | /** |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1357 | * __pm_genpd_add_device - Add a device to an I/O PM domain. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1358 | * @genpd: PM domain to add the device to. |
| 1359 | * @dev: Device to be added. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1360 | * @td: Set of PM QoS timing parameters to attach to the device. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1361 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1362 | int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev, |
| 1363 | struct gpd_timing_data *td) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1364 | { |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1365 | struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1366 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1367 | int ret = 0; |
| 1368 | |
| 1369 | dev_dbg(dev, "%s()\n", __func__); |
| 1370 | |
| 1371 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1372 | return -EINVAL; |
| 1373 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1374 | gpd_data_new = __pm_genpd_alloc_dev_data(dev); |
| 1375 | if (!gpd_data_new) |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1376 | return -ENOMEM; |
| 1377 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1378 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1379 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1380 | if (genpd->prepared_count > 0) { |
| 1381 | ret = -EAGAIN; |
| 1382 | goto out; |
| 1383 | } |
| 1384 | |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1385 | list_for_each_entry(pdd, &genpd->dev_list, list_node) |
| 1386 | if (pdd->dev == dev) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1387 | ret = -EINVAL; |
| 1388 | goto out; |
| 1389 | } |
| 1390 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1391 | ret = dev_pm_get_subsys_data(dev); |
| 1392 | if (ret) |
| 1393 | goto out; |
| 1394 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1395 | genpd->device_count++; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1396 | genpd->max_off_time_changed = true; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1397 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1398 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1399 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1400 | dev->pm_domain = &genpd->domain; |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1401 | if (dev->power.subsys_data->domain_data) { |
| 1402 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data); |
| 1403 | } else { |
| 1404 | gpd_data = gpd_data_new; |
| 1405 | dev->power.subsys_data->domain_data = &gpd_data->base; |
| 1406 | } |
| 1407 | gpd_data->refcount++; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1408 | if (td) |
| 1409 | gpd_data->td = *td; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1410 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1411 | spin_unlock_irq(&dev->power.lock); |
| 1412 | |
| 1413 | mutex_lock(&gpd_data->lock); |
| 1414 | gpd_data->base.dev = dev; |
| 1415 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); |
| 1416 | gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1417 | gpd_data->td.constraint_changed = true; |
| 1418 | gpd_data->td.effective_constraint_ns = -1; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1419 | mutex_unlock(&gpd_data->lock); |
| 1420 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1421 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1422 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1423 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1424 | if (gpd_data != gpd_data_new) |
| 1425 | __pm_genpd_free_dev_data(dev, gpd_data_new); |
| 1426 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1427 | return ret; |
| 1428 | } |
| 1429 | |
| 1430 | /** |
Thomas Abraham | c8aa130 | 2012-01-27 15:22:07 +0900 | [diff] [blame] | 1431 | * __pm_genpd_of_add_device - Add a device to an I/O PM domain. |
| 1432 | * @genpd_node: Device tree node pointer representing a PM domain to which the |
| 1433 | * the device is added to. |
| 1434 | * @dev: Device to be added. |
| 1435 | * @td: Set of PM QoS timing parameters to attach to the device. |
| 1436 | */ |
| 1437 | int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev, |
| 1438 | struct gpd_timing_data *td) |
| 1439 | { |
| 1440 | struct generic_pm_domain *genpd = NULL, *gpd; |
| 1441 | |
| 1442 | dev_dbg(dev, "%s()\n", __func__); |
| 1443 | |
| 1444 | if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev)) |
| 1445 | return -EINVAL; |
| 1446 | |
| 1447 | mutex_lock(&gpd_list_lock); |
| 1448 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) { |
| 1449 | if (gpd->of_node == genpd_node) { |
| 1450 | genpd = gpd; |
| 1451 | break; |
| 1452 | } |
| 1453 | } |
| 1454 | mutex_unlock(&gpd_list_lock); |
| 1455 | |
| 1456 | if (!genpd) |
| 1457 | return -EINVAL; |
| 1458 | |
| 1459 | return __pm_genpd_add_device(genpd, dev, td); |
| 1460 | } |
| 1461 | |
| 1462 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1463 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. |
| 1464 | * @genpd: PM domain to remove the device from. |
| 1465 | * @dev: Device to be removed. |
| 1466 | */ |
| 1467 | int pm_genpd_remove_device(struct generic_pm_domain *genpd, |
| 1468 | struct device *dev) |
| 1469 | { |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1470 | struct generic_pm_domain_data *gpd_data; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1471 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1472 | bool remove = false; |
Rafael J. Wysocki | efa6902 | 2012-05-01 21:33:53 +0200 | [diff] [blame] | 1473 | int ret = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1474 | |
| 1475 | dev_dbg(dev, "%s()\n", __func__); |
| 1476 | |
Rafael J. Wysocki | efa6902 | 2012-05-01 21:33:53 +0200 | [diff] [blame] | 1477 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev) |
| 1478 | || IS_ERR_OR_NULL(dev->pm_domain) |
| 1479 | || pd_to_genpd(dev->pm_domain) != genpd) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1480 | return -EINVAL; |
| 1481 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1482 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1483 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1484 | if (genpd->prepared_count > 0) { |
| 1485 | ret = -EAGAIN; |
| 1486 | goto out; |
| 1487 | } |
| 1488 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1489 | genpd->device_count--; |
| 1490 | genpd->max_off_time_changed = true; |
| 1491 | |
| 1492 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1493 | |
Rafael J. Wysocki | efa6902 | 2012-05-01 21:33:53 +0200 | [diff] [blame] | 1494 | dev->pm_domain = NULL; |
| 1495 | pdd = dev->power.subsys_data->domain_data; |
| 1496 | list_del_init(&pdd->list_node); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1497 | gpd_data = to_gpd_data(pdd); |
| 1498 | if (--gpd_data->refcount == 0) { |
| 1499 | dev->power.subsys_data->domain_data = NULL; |
| 1500 | remove = true; |
| 1501 | } |
| 1502 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1503 | spin_unlock_irq(&dev->power.lock); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1504 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1505 | mutex_lock(&gpd_data->lock); |
| 1506 | pdd->dev = NULL; |
| 1507 | mutex_unlock(&gpd_data->lock); |
| 1508 | |
| 1509 | genpd_release_lock(genpd); |
| 1510 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1511 | dev_pm_put_subsys_data(dev); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1512 | if (remove) |
| 1513 | __pm_genpd_free_dev_data(dev, gpd_data); |
| 1514 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1515 | return 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1516 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1517 | out: |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1518 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1519 | |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
| 1523 | /** |
Rafael J. Wysocki | ca1d72f | 2012-05-14 21:45:52 +0200 | [diff] [blame] | 1524 | * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag. |
| 1525 | * @dev: Device to set/unset the flag for. |
| 1526 | * @val: The new value of the device's "need restore" flag. |
| 1527 | */ |
| 1528 | void pm_genpd_dev_need_restore(struct device *dev, bool val) |
| 1529 | { |
| 1530 | struct pm_subsys_data *psd; |
| 1531 | unsigned long flags; |
| 1532 | |
| 1533 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1534 | |
| 1535 | psd = dev_to_psd(dev); |
| 1536 | if (psd && psd->domain_data) |
| 1537 | to_gpd_data(psd->domain_data)->need_restore = val; |
| 1538 | |
| 1539 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1540 | } |
| 1541 | EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore); |
| 1542 | |
| 1543 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1544 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 1545 | * @genpd: Master PM domain to add the subdomain to. |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1546 | * @subdomain: Subdomain to be added. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1547 | */ |
| 1548 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1549 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1550 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1551 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1552 | int ret = 0; |
| 1553 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1554 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1555 | return -EINVAL; |
| 1556 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1557 | start: |
| 1558 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1559 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1560 | |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1561 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1562 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1563 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1564 | genpd_release_lock(genpd); |
| 1565 | goto start; |
| 1566 | } |
| 1567 | |
| 1568 | if (genpd->status == GPD_STATE_POWER_OFF |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1569 | && subdomain->status != GPD_STATE_POWER_OFF) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1570 | ret = -EINVAL; |
| 1571 | goto out; |
| 1572 | } |
| 1573 | |
Huang Ying | 4fcac10 | 2012-05-07 21:35:45 +0200 | [diff] [blame] | 1574 | list_for_each_entry(link, &genpd->master_links, master_node) { |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1575 | if (link->slave == subdomain && link->master == genpd) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1576 | ret = -EINVAL; |
| 1577 | goto out; |
| 1578 | } |
| 1579 | } |
| 1580 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1581 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 1582 | if (!link) { |
| 1583 | ret = -ENOMEM; |
| 1584 | goto out; |
| 1585 | } |
| 1586 | link->master = genpd; |
| 1587 | list_add_tail(&link->master_node, &genpd->master_links); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1588 | link->slave = subdomain; |
| 1589 | list_add_tail(&link->slave_node, &subdomain->slave_links); |
| 1590 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1591 | genpd_sd_counter_inc(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1592 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1593 | out: |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1594 | mutex_unlock(&subdomain->lock); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1595 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1596 | |
| 1597 | return ret; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. |
| 1602 | * @genpd: Master PM domain to remove the subdomain from. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1603 | * @subdomain: Subdomain to be removed. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1604 | */ |
| 1605 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1606 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1607 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1608 | struct gpd_link *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1609 | int ret = -EINVAL; |
| 1610 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1611 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1612 | return -EINVAL; |
| 1613 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1614 | start: |
| 1615 | genpd_acquire_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1616 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1617 | list_for_each_entry(link, &genpd->master_links, master_node) { |
| 1618 | if (link->slave != subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1619 | continue; |
| 1620 | |
| 1621 | mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING); |
| 1622 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1623 | if (subdomain->status != GPD_STATE_POWER_OFF |
| 1624 | && subdomain->status != GPD_STATE_ACTIVE) { |
| 1625 | mutex_unlock(&subdomain->lock); |
| 1626 | genpd_release_lock(genpd); |
| 1627 | goto start; |
| 1628 | } |
| 1629 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1630 | list_del(&link->master_node); |
| 1631 | list_del(&link->slave_node); |
| 1632 | kfree(link); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1633 | if (subdomain->status != GPD_STATE_POWER_OFF) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1634 | genpd_sd_counter_dec(genpd); |
| 1635 | |
| 1636 | mutex_unlock(&subdomain->lock); |
| 1637 | |
| 1638 | ret = 0; |
| 1639 | break; |
| 1640 | } |
| 1641 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1642 | genpd_release_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1643 | |
| 1644 | return ret; |
| 1645 | } |
| 1646 | |
| 1647 | /** |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1648 | * pm_genpd_add_callbacks - Add PM domain callbacks to a given device. |
| 1649 | * @dev: Device to add the callbacks to. |
| 1650 | * @ops: Set of callbacks to add. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1651 | * @td: Timing data to add to the device along with the callbacks (optional). |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1652 | * |
| 1653 | * Every call to this routine should be balanced with a call to |
| 1654 | * __pm_genpd_remove_callbacks() and they must not be nested. |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1655 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1656 | int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops, |
| 1657 | struct gpd_timing_data *td) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1658 | { |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1659 | struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1660 | int ret = 0; |
| 1661 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1662 | if (!(dev && ops)) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1663 | return -EINVAL; |
| 1664 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1665 | gpd_data_new = __pm_genpd_alloc_dev_data(dev); |
| 1666 | if (!gpd_data_new) |
| 1667 | return -ENOMEM; |
| 1668 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1669 | pm_runtime_disable(dev); |
| 1670 | device_pm_lock(); |
| 1671 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1672 | ret = dev_pm_get_subsys_data(dev); |
| 1673 | if (ret) |
| 1674 | goto out; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1675 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1676 | spin_lock_irq(&dev->power.lock); |
| 1677 | |
| 1678 | if (dev->power.subsys_data->domain_data) { |
| 1679 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1680 | } else { |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1681 | gpd_data = gpd_data_new; |
| 1682 | dev->power.subsys_data->domain_data = &gpd_data->base; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1683 | } |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1684 | gpd_data->refcount++; |
| 1685 | gpd_data->ops = *ops; |
| 1686 | if (td) |
| 1687 | gpd_data->td = *td; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1688 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1689 | spin_unlock_irq(&dev->power.lock); |
| 1690 | |
| 1691 | out: |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1692 | device_pm_unlock(); |
| 1693 | pm_runtime_enable(dev); |
| 1694 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1695 | if (gpd_data != gpd_data_new) |
| 1696 | __pm_genpd_free_dev_data(dev, gpd_data_new); |
| 1697 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1698 | return ret; |
| 1699 | } |
| 1700 | EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks); |
| 1701 | |
| 1702 | /** |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1703 | * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device. |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1704 | * @dev: Device to remove the callbacks from. |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1705 | * @clear_td: If set, clear the device's timing data too. |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1706 | * |
| 1707 | * This routine can only be called after pm_genpd_add_callbacks(). |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1708 | */ |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1709 | int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1710 | { |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1711 | struct generic_pm_domain_data *gpd_data = NULL; |
| 1712 | bool remove = false; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1713 | int ret = 0; |
| 1714 | |
| 1715 | if (!(dev && dev->power.subsys_data)) |
| 1716 | return -EINVAL; |
| 1717 | |
| 1718 | pm_runtime_disable(dev); |
| 1719 | device_pm_lock(); |
| 1720 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1721 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1722 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1723 | if (dev->power.subsys_data->domain_data) { |
| 1724 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data); |
Sachin Kamat | db79e53 | 2012-07-11 12:25:49 +0200 | [diff] [blame] | 1725 | gpd_data->ops = (struct gpd_dev_ops){ NULL }; |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1726 | if (clear_td) |
| 1727 | gpd_data->td = (struct gpd_timing_data){ 0 }; |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1728 | |
| 1729 | if (--gpd_data->refcount == 0) { |
| 1730 | dev->power.subsys_data->domain_data = NULL; |
| 1731 | remove = true; |
| 1732 | } |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1733 | } else { |
| 1734 | ret = -EINVAL; |
| 1735 | } |
| 1736 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1737 | spin_unlock_irq(&dev->power.lock); |
| 1738 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1739 | device_pm_unlock(); |
| 1740 | pm_runtime_enable(dev); |
| 1741 | |
Rafael J. Wysocki | 62d4490 | 2012-07-05 22:12:54 +0200 | [diff] [blame] | 1742 | if (ret) |
| 1743 | return ret; |
| 1744 | |
| 1745 | dev_pm_put_subsys_data(dev); |
| 1746 | if (remove) |
| 1747 | __pm_genpd_free_dev_data(dev, gpd_data); |
| 1748 | |
| 1749 | return 0; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1750 | } |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 1751 | EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1752 | |
Rafael J. Wysocki | cbc9ef0 | 2012-07-03 19:07:42 +0200 | [diff] [blame] | 1753 | int genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state) |
| 1754 | { |
| 1755 | struct cpuidle_driver *cpuidle_drv; |
| 1756 | struct gpd_cpu_data *cpu_data; |
| 1757 | struct cpuidle_state *idle_state; |
| 1758 | int ret = 0; |
| 1759 | |
| 1760 | if (IS_ERR_OR_NULL(genpd) || state < 0) |
| 1761 | return -EINVAL; |
| 1762 | |
| 1763 | genpd_acquire_lock(genpd); |
| 1764 | |
| 1765 | if (genpd->cpu_data) { |
| 1766 | ret = -EEXIST; |
| 1767 | goto out; |
| 1768 | } |
| 1769 | cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL); |
| 1770 | if (!cpu_data) { |
| 1771 | ret = -ENOMEM; |
| 1772 | goto out; |
| 1773 | } |
| 1774 | cpuidle_drv = cpuidle_driver_ref(); |
| 1775 | if (!cpuidle_drv) { |
| 1776 | ret = -ENODEV; |
| 1777 | goto out; |
| 1778 | } |
| 1779 | if (cpuidle_drv->state_count <= state) { |
| 1780 | ret = -EINVAL; |
| 1781 | goto err; |
| 1782 | } |
| 1783 | idle_state = &cpuidle_drv->states[state]; |
| 1784 | if (!idle_state->disabled) { |
| 1785 | ret = -EAGAIN; |
| 1786 | goto err; |
| 1787 | } |
| 1788 | cpu_data->idle_state = idle_state; |
| 1789 | cpu_data->saved_exit_latency = idle_state->exit_latency; |
| 1790 | genpd->cpu_data = cpu_data; |
| 1791 | genpd_recalc_cpu_exit_latency(genpd); |
| 1792 | |
| 1793 | out: |
| 1794 | genpd_release_lock(genpd); |
| 1795 | return ret; |
| 1796 | |
| 1797 | err: |
| 1798 | cpuidle_driver_unref(); |
| 1799 | goto out; |
| 1800 | } |
| 1801 | |
| 1802 | int genpd_detach_cpuidle(struct generic_pm_domain *genpd) |
| 1803 | { |
| 1804 | struct gpd_cpu_data *cpu_data; |
| 1805 | struct cpuidle_state *idle_state; |
| 1806 | int ret = 0; |
| 1807 | |
| 1808 | if (IS_ERR_OR_NULL(genpd)) |
| 1809 | return -EINVAL; |
| 1810 | |
| 1811 | genpd_acquire_lock(genpd); |
| 1812 | |
| 1813 | cpu_data = genpd->cpu_data; |
| 1814 | if (!cpu_data) { |
| 1815 | ret = -ENODEV; |
| 1816 | goto out; |
| 1817 | } |
| 1818 | idle_state = cpu_data->idle_state; |
| 1819 | if (!idle_state->disabled) { |
| 1820 | ret = -EAGAIN; |
| 1821 | goto out; |
| 1822 | } |
| 1823 | idle_state->exit_latency = cpu_data->saved_exit_latency; |
| 1824 | cpuidle_driver_unref(); |
| 1825 | genpd->cpu_data = NULL; |
| 1826 | kfree(cpu_data); |
| 1827 | |
| 1828 | out: |
| 1829 | genpd_release_lock(genpd); |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1833 | /* Default device callbacks for generic PM domains. */ |
| 1834 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 1835 | /** |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1836 | * pm_genpd_default_save_state - Default "save device state" for PM domians. |
| 1837 | * @dev: Device to handle. |
| 1838 | */ |
| 1839 | static int pm_genpd_default_save_state(struct device *dev) |
| 1840 | { |
| 1841 | int (*cb)(struct device *__dev); |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1842 | |
| 1843 | cb = dev_gpd_data(dev)->ops.save_state; |
| 1844 | if (cb) |
| 1845 | return cb(dev); |
| 1846 | |
Rafael J. Wysocki | 0b589741 | 2012-06-16 00:02:22 +0200 | [diff] [blame] | 1847 | if (dev->type && dev->type->pm) |
| 1848 | cb = dev->type->pm->runtime_suspend; |
| 1849 | else if (dev->class && dev->class->pm) |
| 1850 | cb = dev->class->pm->runtime_suspend; |
| 1851 | else if (dev->bus && dev->bus->pm) |
| 1852 | cb = dev->bus->pm->runtime_suspend; |
| 1853 | else |
| 1854 | cb = NULL; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1855 | |
Rafael J. Wysocki | 0b589741 | 2012-06-16 00:02:22 +0200 | [diff] [blame] | 1856 | if (!cb && dev->driver && dev->driver->pm) |
| 1857 | cb = dev->driver->pm->runtime_suspend; |
| 1858 | |
| 1859 | return cb ? cb(dev) : 0; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * pm_genpd_default_restore_state - Default PM domians "restore device state". |
| 1864 | * @dev: Device to handle. |
| 1865 | */ |
| 1866 | static int pm_genpd_default_restore_state(struct device *dev) |
| 1867 | { |
| 1868 | int (*cb)(struct device *__dev); |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1869 | |
| 1870 | cb = dev_gpd_data(dev)->ops.restore_state; |
| 1871 | if (cb) |
| 1872 | return cb(dev); |
| 1873 | |
Rafael J. Wysocki | 0b589741 | 2012-06-16 00:02:22 +0200 | [diff] [blame] | 1874 | if (dev->type && dev->type->pm) |
| 1875 | cb = dev->type->pm->runtime_resume; |
| 1876 | else if (dev->class && dev->class->pm) |
| 1877 | cb = dev->class->pm->runtime_resume; |
| 1878 | else if (dev->bus && dev->bus->pm) |
| 1879 | cb = dev->bus->pm->runtime_resume; |
| 1880 | else |
| 1881 | cb = NULL; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1882 | |
Rafael J. Wysocki | 0b589741 | 2012-06-16 00:02:22 +0200 | [diff] [blame] | 1883 | if (!cb && dev->driver && dev->driver->pm) |
| 1884 | cb = dev->driver->pm->runtime_resume; |
| 1885 | |
| 1886 | return cb ? cb(dev) : 0; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1887 | } |
| 1888 | |
Rafael J. Wysocki | 0f1d698 | 2012-01-14 00:39:25 +0100 | [diff] [blame] | 1889 | #ifdef CONFIG_PM_SLEEP |
| 1890 | |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 1891 | /** |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1892 | * pm_genpd_default_suspend - Default "device suspend" for PM domians. |
| 1893 | * @dev: Device to handle. |
| 1894 | */ |
| 1895 | static int pm_genpd_default_suspend(struct device *dev) |
| 1896 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1897 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1898 | |
| 1899 | return cb ? cb(dev) : pm_generic_suspend(dev); |
| 1900 | } |
| 1901 | |
| 1902 | /** |
| 1903 | * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians. |
| 1904 | * @dev: Device to handle. |
| 1905 | */ |
| 1906 | static int pm_genpd_default_suspend_late(struct device *dev) |
| 1907 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1908 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1909 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1910 | return cb ? cb(dev) : pm_generic_suspend_late(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * pm_genpd_default_resume_early - Default "early device resume" for PM domians. |
| 1915 | * @dev: Device to handle. |
| 1916 | */ |
| 1917 | static int pm_genpd_default_resume_early(struct device *dev) |
| 1918 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1919 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1920 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1921 | return cb ? cb(dev) : pm_generic_resume_early(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | * pm_genpd_default_resume - Default "device resume" for PM domians. |
| 1926 | * @dev: Device to handle. |
| 1927 | */ |
| 1928 | static int pm_genpd_default_resume(struct device *dev) |
| 1929 | { |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 1930 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1931 | |
| 1932 | return cb ? cb(dev) : pm_generic_resume(dev); |
| 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * pm_genpd_default_freeze - Default "device freeze" for PM domians. |
| 1937 | * @dev: Device to handle. |
| 1938 | */ |
| 1939 | static int pm_genpd_default_freeze(struct device *dev) |
| 1940 | { |
| 1941 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze; |
| 1942 | |
| 1943 | return cb ? cb(dev) : pm_generic_freeze(dev); |
| 1944 | } |
| 1945 | |
| 1946 | /** |
| 1947 | * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians. |
| 1948 | * @dev: Device to handle. |
| 1949 | */ |
| 1950 | static int pm_genpd_default_freeze_late(struct device *dev) |
| 1951 | { |
| 1952 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late; |
| 1953 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1954 | return cb ? cb(dev) : pm_generic_freeze_late(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians. |
| 1959 | * @dev: Device to handle. |
| 1960 | */ |
| 1961 | static int pm_genpd_default_thaw_early(struct device *dev) |
| 1962 | { |
| 1963 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early; |
| 1964 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1965 | return cb ? cb(dev) : pm_generic_thaw_early(dev); |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * pm_genpd_default_thaw - Default "device thaw" for PM domians. |
| 1970 | * @dev: Device to handle. |
| 1971 | */ |
| 1972 | static int pm_genpd_default_thaw(struct device *dev) |
| 1973 | { |
| 1974 | int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw; |
| 1975 | |
| 1976 | return cb ? cb(dev) : pm_generic_thaw(dev); |
| 1977 | } |
| 1978 | |
Rafael J. Wysocki | 0f1d698 | 2012-01-14 00:39:25 +0100 | [diff] [blame] | 1979 | #else /* !CONFIG_PM_SLEEP */ |
| 1980 | |
| 1981 | #define pm_genpd_default_suspend NULL |
| 1982 | #define pm_genpd_default_suspend_late NULL |
| 1983 | #define pm_genpd_default_resume_early NULL |
| 1984 | #define pm_genpd_default_resume NULL |
| 1985 | #define pm_genpd_default_freeze NULL |
| 1986 | #define pm_genpd_default_freeze_late NULL |
| 1987 | #define pm_genpd_default_thaw_early NULL |
| 1988 | #define pm_genpd_default_thaw NULL |
| 1989 | |
| 1990 | #endif /* !CONFIG_PM_SLEEP */ |
| 1991 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1992 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1993 | * pm_genpd_init - Initialize a generic I/O PM domain object. |
| 1994 | * @genpd: PM domain object to initialize. |
| 1995 | * @gov: PM domain governor to associate with the domain (may be NULL). |
| 1996 | * @is_off: Initial value of the domain's power_is_off field. |
| 1997 | */ |
| 1998 | void pm_genpd_init(struct generic_pm_domain *genpd, |
| 1999 | struct dev_power_governor *gov, bool is_off) |
| 2000 | { |
| 2001 | if (IS_ERR_OR_NULL(genpd)) |
| 2002 | return; |
| 2003 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 2004 | INIT_LIST_HEAD(&genpd->master_links); |
| 2005 | INIT_LIST_HEAD(&genpd->slave_links); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 2006 | INIT_LIST_HEAD(&genpd->dev_list); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 2007 | mutex_init(&genpd->lock); |
| 2008 | genpd->gov = gov; |
| 2009 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); |
| 2010 | genpd->in_progress = 0; |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 2011 | atomic_set(&genpd->sd_count, 0); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 2012 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
| 2013 | init_waitqueue_head(&genpd->status_wait_queue); |
Rafael J. Wysocki | c6d22b3 | 2011-07-12 00:39:36 +0200 | [diff] [blame] | 2014 | genpd->poweroff_task = NULL; |
| 2015 | genpd->resume_count = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2016 | genpd->device_count = 0; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 2017 | genpd->max_off_time_ns = -1; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 2018 | genpd->max_off_time_changed = true; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 2019 | genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend; |
| 2020 | genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume; |
| 2021 | genpd->domain.ops.runtime_idle = pm_generic_runtime_idle; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2022 | genpd->domain.ops.prepare = pm_genpd_prepare; |
| 2023 | genpd->domain.ops.suspend = pm_genpd_suspend; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 2024 | genpd->domain.ops.suspend_late = pm_genpd_suspend_late; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2025 | genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq; |
| 2026 | genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 2027 | genpd->domain.ops.resume_early = pm_genpd_resume_early; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2028 | genpd->domain.ops.resume = pm_genpd_resume; |
| 2029 | genpd->domain.ops.freeze = pm_genpd_freeze; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 2030 | genpd->domain.ops.freeze_late = pm_genpd_freeze_late; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2031 | genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq; |
| 2032 | genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 2033 | genpd->domain.ops.thaw_early = pm_genpd_thaw_early; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2034 | genpd->domain.ops.thaw = pm_genpd_thaw; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 2035 | genpd->domain.ops.poweroff = pm_genpd_suspend; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 2036 | genpd->domain.ops.poweroff_late = pm_genpd_suspend_late; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 2037 | genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2038 | genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq; |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 2039 | genpd->domain.ops.restore_early = pm_genpd_resume_early; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 2040 | genpd->domain.ops.restore = pm_genpd_resume; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 2041 | genpd->domain.ops.complete = pm_genpd_complete; |
Rafael J. Wysocki | ecf0047 | 2011-11-27 13:11:44 +0100 | [diff] [blame] | 2042 | genpd->dev_ops.save_state = pm_genpd_default_save_state; |
| 2043 | genpd->dev_ops.restore_state = pm_genpd_default_restore_state; |
Rafael J. Wysocki | c991485 | 2011-12-06 23:16:47 +0100 | [diff] [blame] | 2044 | genpd->dev_ops.suspend = pm_genpd_default_suspend; |
| 2045 | genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late; |
| 2046 | genpd->dev_ops.resume_early = pm_genpd_default_resume_early; |
| 2047 | genpd->dev_ops.resume = pm_genpd_default_resume; |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 2048 | genpd->dev_ops.freeze = pm_genpd_default_freeze; |
| 2049 | genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late; |
| 2050 | genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early; |
| 2051 | genpd->dev_ops.thaw = pm_genpd_default_thaw; |
Rafael J. Wysocki | 5125bbf | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 2052 | mutex_lock(&gpd_list_lock); |
| 2053 | list_add(&genpd->gpd_list_node, &gpd_list); |
| 2054 | mutex_unlock(&gpd_list_lock); |
| 2055 | } |