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