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