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