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> |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 13 | #include <linux/pm_opp.h> |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 14 | #include <linux/pm_runtime.h> |
| 15 | #include <linux/pm_domain.h> |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 16 | #include <linux/pm_qos.h> |
Ulf Hansson | c11f6f5 | 2014-12-01 12:50:21 +0100 | [diff] [blame] | 17 | #include <linux/pm_clock.h> |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/err.h> |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 20 | #include <linux/sched.h> |
| 21 | #include <linux/suspend.h> |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 22 | #include <linux/export.h> |
| 23 | |
Tomeu Vizoso | aa8e54b5 | 2016-01-07 16:46:14 +0100 | [diff] [blame] | 24 | #include "power.h" |
| 25 | |
Geert Uytterhoeven | 93af5e9 | 2015-06-26 11:14:14 +0200 | [diff] [blame] | 26 | #define GENPD_RETRY_MAX_MS 250 /* Approximate */ |
| 27 | |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 28 | #define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \ |
| 29 | ({ \ |
| 30 | type (*__routine)(struct device *__d); \ |
| 31 | type __ret = (type)0; \ |
| 32 | \ |
| 33 | __routine = genpd->dev_ops.callback; \ |
| 34 | if (__routine) { \ |
| 35 | __ret = __routine(dev); \ |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 36 | } \ |
| 37 | __ret; \ |
| 38 | }) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 39 | |
Rafael J. Wysocki | 5125bbf38 | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 40 | static LIST_HEAD(gpd_list); |
| 41 | static DEFINE_MUTEX(gpd_list_lock); |
| 42 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 43 | struct genpd_lock_ops { |
| 44 | void (*lock)(struct generic_pm_domain *genpd); |
| 45 | void (*lock_nested)(struct generic_pm_domain *genpd, int depth); |
| 46 | int (*lock_interruptible)(struct generic_pm_domain *genpd); |
| 47 | void (*unlock)(struct generic_pm_domain *genpd); |
| 48 | }; |
| 49 | |
| 50 | static void genpd_lock_mtx(struct generic_pm_domain *genpd) |
| 51 | { |
| 52 | mutex_lock(&genpd->mlock); |
| 53 | } |
| 54 | |
| 55 | static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd, |
| 56 | int depth) |
| 57 | { |
| 58 | mutex_lock_nested(&genpd->mlock, depth); |
| 59 | } |
| 60 | |
| 61 | static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd) |
| 62 | { |
| 63 | return mutex_lock_interruptible(&genpd->mlock); |
| 64 | } |
| 65 | |
| 66 | static void genpd_unlock_mtx(struct generic_pm_domain *genpd) |
| 67 | { |
| 68 | return mutex_unlock(&genpd->mlock); |
| 69 | } |
| 70 | |
| 71 | static const struct genpd_lock_ops genpd_mtx_ops = { |
| 72 | .lock = genpd_lock_mtx, |
| 73 | .lock_nested = genpd_lock_nested_mtx, |
| 74 | .lock_interruptible = genpd_lock_interruptible_mtx, |
| 75 | .unlock = genpd_unlock_mtx, |
| 76 | }; |
| 77 | |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 78 | static void genpd_lock_spin(struct generic_pm_domain *genpd) |
| 79 | __acquires(&genpd->slock) |
| 80 | { |
| 81 | unsigned long flags; |
| 82 | |
| 83 | spin_lock_irqsave(&genpd->slock, flags); |
| 84 | genpd->lock_flags = flags; |
| 85 | } |
| 86 | |
| 87 | static void genpd_lock_nested_spin(struct generic_pm_domain *genpd, |
| 88 | int depth) |
| 89 | __acquires(&genpd->slock) |
| 90 | { |
| 91 | unsigned long flags; |
| 92 | |
| 93 | spin_lock_irqsave_nested(&genpd->slock, flags, depth); |
| 94 | genpd->lock_flags = flags; |
| 95 | } |
| 96 | |
| 97 | static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd) |
| 98 | __acquires(&genpd->slock) |
| 99 | { |
| 100 | unsigned long flags; |
| 101 | |
| 102 | spin_lock_irqsave(&genpd->slock, flags); |
| 103 | genpd->lock_flags = flags; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void genpd_unlock_spin(struct generic_pm_domain *genpd) |
| 108 | __releases(&genpd->slock) |
| 109 | { |
| 110 | spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags); |
| 111 | } |
| 112 | |
| 113 | static const struct genpd_lock_ops genpd_spin_ops = { |
| 114 | .lock = genpd_lock_spin, |
| 115 | .lock_nested = genpd_lock_nested_spin, |
| 116 | .lock_interruptible = genpd_lock_interruptible_spin, |
| 117 | .unlock = genpd_unlock_spin, |
| 118 | }; |
| 119 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 120 | #define genpd_lock(p) p->lock_ops->lock(p) |
| 121 | #define genpd_lock_nested(p, d) p->lock_ops->lock_nested(p, d) |
| 122 | #define genpd_lock_interruptible(p) p->lock_ops->lock_interruptible(p) |
| 123 | #define genpd_unlock(p) p->lock_ops->unlock(p) |
| 124 | |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 125 | #define genpd_status_on(genpd) (genpd->status == GPD_STATE_ACTIVE) |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 126 | #define genpd_is_irq_safe(genpd) (genpd->flags & GENPD_FLAG_IRQ_SAFE) |
Ulf Hansson | ffaa42e | 2017-03-20 11:19:21 +0100 | [diff] [blame] | 127 | #define genpd_is_always_on(genpd) (genpd->flags & GENPD_FLAG_ALWAYS_ON) |
Geert Uytterhoeven | 95a20ef | 2017-11-07 13:48:11 +0100 | [diff] [blame] | 128 | #define genpd_is_active_wakeup(genpd) (genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP) |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 129 | |
| 130 | static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev, |
Krzysztof Kozlowski | d8600c8 | 2017-06-12 17:17:41 +0200 | [diff] [blame] | 131 | const struct generic_pm_domain *genpd) |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 132 | { |
| 133 | bool ret; |
| 134 | |
| 135 | ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd); |
| 136 | |
Ulf Hansson | 075c37d | 2017-03-20 11:19:23 +0100 | [diff] [blame] | 137 | /* |
| 138 | * Warn once if an IRQ safe device is attached to a no sleep domain, as |
| 139 | * to indicate a suboptimal configuration for PM. For an always on |
| 140 | * domain this isn't case, thus don't warn. |
| 141 | */ |
| 142 | if (ret && !genpd_is_always_on(genpd)) |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 143 | dev_warn_once(dev, "PM domain %s will not be powered off\n", |
| 144 | genpd->name); |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
Russell King | 446d999c | 2015-03-20 17:20:33 +0000 | [diff] [blame] | 149 | /* |
| 150 | * Get the generic PM domain for a particular struct device. |
| 151 | * This validates the struct device pointer, the PM domain pointer, |
| 152 | * and checks that the PM domain pointer is a real generic PM domain. |
| 153 | * Any failure results in NULL being returned. |
| 154 | */ |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 155 | static struct generic_pm_domain *genpd_lookup_dev(struct device *dev) |
Russell King | 446d999c | 2015-03-20 17:20:33 +0000 | [diff] [blame] | 156 | { |
| 157 | struct generic_pm_domain *genpd = NULL, *gpd; |
| 158 | |
| 159 | if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain)) |
| 160 | return NULL; |
| 161 | |
| 162 | mutex_lock(&gpd_list_lock); |
| 163 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) { |
| 164 | if (&gpd->domain == dev->pm_domain) { |
| 165 | genpd = gpd; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | mutex_unlock(&gpd_list_lock); |
| 170 | |
| 171 | return genpd; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * This should only be used where we are certain that the pm_domain |
| 176 | * attached to the device is a genpd domain. |
| 177 | */ |
| 178 | static struct generic_pm_domain *dev_to_genpd(struct device *dev) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 179 | { |
| 180 | if (IS_ERR_OR_NULL(dev->pm_domain)) |
| 181 | return ERR_PTR(-EINVAL); |
| 182 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 183 | return pd_to_genpd(dev->pm_domain); |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 184 | } |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 185 | |
Krzysztof Kozlowski | d8600c8 | 2017-06-12 17:17:41 +0200 | [diff] [blame] | 186 | static int genpd_stop_dev(const struct generic_pm_domain *genpd, |
| 187 | struct device *dev) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 188 | { |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 189 | return GENPD_DEV_CALLBACK(genpd, int, stop, dev); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Krzysztof Kozlowski | d8600c8 | 2017-06-12 17:17:41 +0200 | [diff] [blame] | 192 | static int genpd_start_dev(const struct generic_pm_domain *genpd, |
| 193 | struct device *dev) |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 194 | { |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 195 | return GENPD_DEV_CALLBACK(genpd, int, start, dev); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 198 | static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 199 | { |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 200 | bool ret = false; |
| 201 | |
| 202 | if (!WARN_ON(atomic_read(&genpd->sd_count) == 0)) |
| 203 | ret = !!atomic_dec_and_test(&genpd->sd_count); |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static void genpd_sd_counter_inc(struct generic_pm_domain *genpd) |
| 209 | { |
| 210 | atomic_inc(&genpd->sd_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 211 | smp_mb__after_atomic(); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Thara Gopinath | afece3a | 2017-07-14 13:10:15 -0400 | [diff] [blame] | 214 | #ifdef CONFIG_DEBUG_FS |
| 215 | static void genpd_update_accounting(struct generic_pm_domain *genpd) |
| 216 | { |
| 217 | ktime_t delta, now; |
| 218 | |
| 219 | now = ktime_get(); |
| 220 | delta = ktime_sub(now, genpd->accounting_time); |
| 221 | |
| 222 | /* |
| 223 | * If genpd->status is active, it means we are just |
| 224 | * out of off and so update the idle time and vice |
| 225 | * versa. |
| 226 | */ |
| 227 | if (genpd->status == GPD_STATE_ACTIVE) { |
| 228 | int state_idx = genpd->state_idx; |
| 229 | |
| 230 | genpd->states[state_idx].idle_time = |
| 231 | ktime_add(genpd->states[state_idx].idle_time, delta); |
| 232 | } else { |
| 233 | genpd->on_time = ktime_add(genpd->on_time, delta); |
| 234 | } |
| 235 | |
| 236 | genpd->accounting_time = now; |
| 237 | } |
| 238 | #else |
| 239 | static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {} |
| 240 | #endif |
| 241 | |
Viresh Kumar | cd50c6d | 2018-10-31 14:56:54 +0530 | [diff] [blame^] | 242 | static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd, |
| 243 | unsigned int state) |
| 244 | { |
| 245 | struct generic_pm_domain_data *pd_data; |
| 246 | struct pm_domain_data *pdd; |
| 247 | |
| 248 | /* New requested state is same as Max requested state */ |
| 249 | if (state == genpd->performance_state) |
| 250 | return state; |
| 251 | |
| 252 | /* New requested state is higher than Max requested state */ |
| 253 | if (state > genpd->performance_state) |
| 254 | return state; |
| 255 | |
| 256 | /* Traverse all devices within the domain */ |
| 257 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 258 | pd_data = to_gpd_data(pdd); |
| 259 | |
| 260 | if (pd_data->performance_state > state) |
| 261 | state = pd_data->performance_state; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * We aren't propagating performance state changes of a subdomain to its |
| 266 | * masters as we don't have hardware that needs it. Over that, the |
| 267 | * performance states of subdomain and its masters may not have |
| 268 | * one-to-one mapping and would require additional information. We can |
| 269 | * get back to this once we have hardware that needs it. For that |
| 270 | * reason, we don't have to consider performance state of the subdomains |
| 271 | * of genpd here. |
| 272 | */ |
| 273 | return state; |
| 274 | } |
| 275 | |
| 276 | static int _genpd_set_performance_state(struct generic_pm_domain *genpd, |
| 277 | unsigned int state) |
| 278 | { |
| 279 | int ret; |
| 280 | |
| 281 | if (state == genpd->performance_state) |
| 282 | return 0; |
| 283 | |
| 284 | ret = genpd->set_performance_state(genpd, state); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | genpd->performance_state = state; |
| 289 | return 0; |
| 290 | } |
| 291 | |
Viresh Kumar | 42f6284 | 2017-10-12 15:07:23 +0530 | [diff] [blame] | 292 | /** |
| 293 | * dev_pm_genpd_set_performance_state- Set performance state of device's power |
| 294 | * domain. |
| 295 | * |
| 296 | * @dev: Device for which the performance-state needs to be set. |
| 297 | * @state: Target performance state of the device. This can be set as 0 when the |
| 298 | * device doesn't have any performance state constraints left (And so |
| 299 | * the device wouldn't participate anymore to find the target |
| 300 | * performance state of the genpd). |
| 301 | * |
| 302 | * It is assumed that the users guarantee that the genpd wouldn't be detached |
| 303 | * while this routine is getting called. |
| 304 | * |
| 305 | * Returns 0 on success and negative error values on failures. |
| 306 | */ |
| 307 | int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state) |
| 308 | { |
| 309 | struct generic_pm_domain *genpd; |
Viresh Kumar | cd50c6d | 2018-10-31 14:56:54 +0530 | [diff] [blame^] | 310 | struct generic_pm_domain_data *gpd_data; |
Viresh Kumar | 42f6284 | 2017-10-12 15:07:23 +0530 | [diff] [blame] | 311 | unsigned int prev; |
Viresh Kumar | cd50c6d | 2018-10-31 14:56:54 +0530 | [diff] [blame^] | 312 | int ret; |
Viresh Kumar | 42f6284 | 2017-10-12 15:07:23 +0530 | [diff] [blame] | 313 | |
| 314 | genpd = dev_to_genpd(dev); |
| 315 | if (IS_ERR(genpd)) |
| 316 | return -ENODEV; |
| 317 | |
| 318 | if (unlikely(!genpd->set_performance_state)) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | if (unlikely(!dev->power.subsys_data || |
| 322 | !dev->power.subsys_data->domain_data)) { |
| 323 | WARN_ON(1); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | genpd_lock(genpd); |
| 328 | |
| 329 | gpd_data = to_gpd_data(dev->power.subsys_data->domain_data); |
| 330 | prev = gpd_data->performance_state; |
| 331 | gpd_data->performance_state = state; |
| 332 | |
Viresh Kumar | cd50c6d | 2018-10-31 14:56:54 +0530 | [diff] [blame^] | 333 | state = _genpd_reeval_performance_state(genpd, state); |
| 334 | ret = _genpd_set_performance_state(genpd, state); |
| 335 | if (ret) |
Ulf Hansson | 68de2fe | 2018-12-11 11:04:55 +0100 | [diff] [blame] | 336 | gpd_data->performance_state = prev; |
Viresh Kumar | 42f6284 | 2017-10-12 15:07:23 +0530 | [diff] [blame] | 337 | |
Viresh Kumar | 42f6284 | 2017-10-12 15:07:23 +0530 | [diff] [blame] | 338 | genpd_unlock(genpd); |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state); |
| 343 | |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 344 | static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed) |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 345 | { |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 346 | unsigned int state_idx = genpd->state_idx; |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 347 | ktime_t time_start; |
| 348 | s64 elapsed_ns; |
| 349 | int ret; |
| 350 | |
| 351 | if (!genpd->power_on) |
| 352 | return 0; |
| 353 | |
Geert Uytterhoeven | a4630c6 | 2015-05-29 17:24:23 +0200 | [diff] [blame] | 354 | if (!timed) |
| 355 | return genpd->power_on(genpd); |
| 356 | |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 357 | time_start = ktime_get(); |
| 358 | ret = genpd->power_on(genpd); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | |
| 362 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 363 | if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns) |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 364 | return ret; |
| 365 | |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 366 | genpd->states[state_idx].power_on_latency_ns = elapsed_ns; |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 367 | genpd->max_off_time_changed = true; |
Russell King | 6d7d5c3 | 2015-03-20 17:20:28 +0000 | [diff] [blame] | 368 | pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n", |
| 369 | genpd->name, "on", elapsed_ns); |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 374 | static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed) |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 375 | { |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 376 | unsigned int state_idx = genpd->state_idx; |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 377 | ktime_t time_start; |
| 378 | s64 elapsed_ns; |
| 379 | int ret; |
| 380 | |
| 381 | if (!genpd->power_off) |
| 382 | return 0; |
| 383 | |
Geert Uytterhoeven | a4630c6 | 2015-05-29 17:24:23 +0200 | [diff] [blame] | 384 | if (!timed) |
| 385 | return genpd->power_off(genpd); |
| 386 | |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 387 | time_start = ktime_get(); |
| 388 | ret = genpd->power_off(genpd); |
| 389 | if (ret == -EBUSY) |
| 390 | return ret; |
| 391 | |
| 392 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 393 | if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns) |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 394 | return ret; |
| 395 | |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 396 | genpd->states[state_idx].power_off_latency_ns = elapsed_ns; |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 397 | genpd->max_off_time_changed = true; |
Russell King | 6d7d5c3 | 2015-03-20 17:20:28 +0000 | [diff] [blame] | 398 | pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n", |
| 399 | genpd->name, "off", elapsed_ns); |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 400 | |
| 401 | return ret; |
| 402 | } |
| 403 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 404 | /** |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 405 | * genpd_queue_power_off_work - Queue up the execution of genpd_power_off(). |
Moritz Fischer | a3d09c7 | 2016-01-27 08:29:27 +0100 | [diff] [blame] | 406 | * @genpd: PM domain to power off. |
Ulf Hansson | 29e47e2 | 2015-09-02 10:16:13 +0200 | [diff] [blame] | 407 | * |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 408 | * Queue up the execution of genpd_power_off() unless it's already been done |
Ulf Hansson | 29e47e2 | 2015-09-02 10:16:13 +0200 | [diff] [blame] | 409 | * before. |
| 410 | */ |
| 411 | static void genpd_queue_power_off_work(struct generic_pm_domain *genpd) |
| 412 | { |
| 413 | queue_work(pm_wq, &genpd->power_off_work); |
| 414 | } |
| 415 | |
| 416 | /** |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 417 | * genpd_power_off - Remove power from a given PM domain. |
| 418 | * @genpd: PM domain to power down. |
Ulf Hansson | 3c64649 | 2017-02-17 10:55:24 +0100 | [diff] [blame] | 419 | * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the |
| 420 | * RPM status of the releated device is in an intermediate state, not yet turned |
| 421 | * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not |
| 422 | * be RPM_SUSPENDED, while it tries to power off the PM domain. |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 423 | * |
| 424 | * If all of the @genpd's devices have been suspended and all of its subdomains |
| 425 | * have been powered down, remove power from @genpd. |
| 426 | */ |
Ulf Hansson | 2da8354 | 2017-02-17 10:55:25 +0100 | [diff] [blame] | 427 | static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on, |
| 428 | unsigned int depth) |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 429 | { |
| 430 | struct pm_domain_data *pdd; |
| 431 | struct gpd_link *link; |
| 432 | unsigned int not_suspended = 0; |
| 433 | |
| 434 | /* |
| 435 | * Do not try to power off the domain in the following situations: |
| 436 | * (1) The domain is already in the "power off" state. |
| 437 | * (2) System suspend is in progress. |
| 438 | */ |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 439 | if (!genpd_status_on(genpd) || genpd->prepared_count > 0) |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 440 | return 0; |
| 441 | |
Ulf Hansson | ffaa42e | 2017-03-20 11:19:21 +0100 | [diff] [blame] | 442 | /* |
| 443 | * Abort power off for the PM domain in the following situations: |
| 444 | * (1) The domain is configured as always on. |
| 445 | * (2) When the domain has a subdomain being powered on. |
| 446 | */ |
| 447 | if (genpd_is_always_on(genpd) || atomic_read(&genpd->sd_count) > 0) |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 448 | return -EBUSY; |
| 449 | |
| 450 | list_for_each_entry(pdd, &genpd->dev_list, list_node) { |
| 451 | enum pm_qos_flags_status stat; |
| 452 | |
Rafael J. Wysocki | 20f97ca | 2017-10-13 15:27:24 +0200 | [diff] [blame] | 453 | stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF); |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 454 | if (stat > PM_QOS_FLAGS_NONE) |
| 455 | return -EBUSY; |
| 456 | |
| 457 | /* |
| 458 | * Do not allow PM domain to be powered off, when an IRQ safe |
| 459 | * device is part of a non-IRQ safe domain. |
| 460 | */ |
| 461 | if (!pm_runtime_suspended(pdd->dev) || |
| 462 | irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd)) |
| 463 | not_suspended++; |
| 464 | } |
| 465 | |
Ulf Hansson | 3c64649 | 2017-02-17 10:55:24 +0100 | [diff] [blame] | 466 | if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on)) |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 467 | return -EBUSY; |
| 468 | |
| 469 | if (genpd->gov && genpd->gov->power_down_ok) { |
| 470 | if (!genpd->gov->power_down_ok(&genpd->domain)) |
| 471 | return -EAGAIN; |
| 472 | } |
| 473 | |
Ulf Hansson | 2c9b7f8 | 2018-10-03 16:38:15 +0200 | [diff] [blame] | 474 | /* Default to shallowest state. */ |
| 475 | if (!genpd->gov) |
| 476 | genpd->state_idx = 0; |
| 477 | |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 478 | if (genpd->power_off) { |
| 479 | int ret; |
| 480 | |
| 481 | if (atomic_read(&genpd->sd_count) > 0) |
| 482 | return -EBUSY; |
| 483 | |
| 484 | /* |
| 485 | * If sd_count > 0 at this point, one of the subdomains hasn't |
| 486 | * managed to call genpd_power_on() for the master yet after |
| 487 | * incrementing it. In that case genpd_power_on() will wait |
| 488 | * for us to drop the lock, so we can call .power_off() and let |
| 489 | * the genpd_power_on() restore power for us (this shouldn't |
| 490 | * happen very often). |
| 491 | */ |
| 492 | ret = _genpd_power_off(genpd, true); |
| 493 | if (ret) |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | genpd->status = GPD_STATE_POWER_OFF; |
Thara Gopinath | afece3a | 2017-07-14 13:10:15 -0400 | [diff] [blame] | 498 | genpd_update_accounting(genpd); |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 499 | |
| 500 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 501 | genpd_sd_counter_dec(link->master); |
Ulf Hansson | 2da8354 | 2017-02-17 10:55:25 +0100 | [diff] [blame] | 502 | genpd_lock_nested(link->master, depth + 1); |
| 503 | genpd_power_off(link->master, false, depth + 1); |
| 504 | genpd_unlock(link->master); |
Ulf Hansson | 1f8728b | 2017-02-17 10:55:23 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /** |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 511 | * genpd_power_on - Restore power to a given PM domain and its masters. |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 512 | * @genpd: PM domain to power up. |
Marek Szyprowski | 0106ef5 | 2016-01-20 10:13:42 +0100 | [diff] [blame] | 513 | * @depth: nesting count for lockdep. |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 514 | * |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 515 | * 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] | 516 | * resume a device belonging to it. |
| 517 | */ |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 518 | static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth) |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 519 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 520 | struct gpd_link *link; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 521 | int ret = 0; |
| 522 | |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 523 | if (genpd_status_on(genpd)) |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 524 | return 0; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 525 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 526 | /* |
| 527 | * The list is guaranteed not to change while the loop below is being |
| 528 | * executed, unless one of the masters' .power_on() callbacks fiddles |
| 529 | * with it. |
| 530 | */ |
| 531 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
Marek Szyprowski | 0106ef5 | 2016-01-20 10:13:42 +0100 | [diff] [blame] | 532 | struct generic_pm_domain *master = link->master; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 533 | |
Marek Szyprowski | 0106ef5 | 2016-01-20 10:13:42 +0100 | [diff] [blame] | 534 | genpd_sd_counter_inc(master); |
| 535 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 536 | genpd_lock_nested(master, depth + 1); |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 537 | ret = genpd_power_on(master, depth + 1); |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 538 | genpd_unlock(master); |
Marek Szyprowski | 0106ef5 | 2016-01-20 10:13:42 +0100 | [diff] [blame] | 539 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 540 | if (ret) { |
Marek Szyprowski | 0106ef5 | 2016-01-20 10:13:42 +0100 | [diff] [blame] | 541 | genpd_sd_counter_dec(master); |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 542 | goto err; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 543 | } |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 546 | ret = _genpd_power_on(genpd, true); |
Geert Uytterhoeven | c8f0ea4 | 2014-11-10 19:39:19 +0100 | [diff] [blame] | 547 | if (ret) |
| 548 | goto err; |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 549 | |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 550 | genpd->status = GPD_STATE_ACTIVE; |
Thara Gopinath | afece3a | 2017-07-14 13:10:15 -0400 | [diff] [blame] | 551 | genpd_update_accounting(genpd); |
| 552 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 553 | return 0; |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 554 | |
| 555 | err: |
Ulf Hansson | 29e47e2 | 2015-09-02 10:16:13 +0200 | [diff] [blame] | 556 | list_for_each_entry_continue_reverse(link, |
| 557 | &genpd->slave_links, |
| 558 | slave_node) { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 559 | genpd_sd_counter_dec(link->master); |
Ulf Hansson | 2da8354 | 2017-02-17 10:55:25 +0100 | [diff] [blame] | 560 | genpd_lock_nested(link->master, depth + 1); |
| 561 | genpd_power_off(link->master, false, depth + 1); |
| 562 | genpd_unlock(link->master); |
Ulf Hansson | 29e47e2 | 2015-09-02 10:16:13 +0200 | [diff] [blame] | 563 | } |
Rafael J. Wysocki | 9e08cf4 | 2011-08-08 23:43:22 +0200 | [diff] [blame] | 564 | |
Rafael J. Wysocki | 3f24177 | 2011-08-08 23:43:29 +0200 | [diff] [blame] | 565 | return ret; |
| 566 | } |
| 567 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 568 | static int genpd_dev_pm_qos_notifier(struct notifier_block *nb, |
| 569 | unsigned long val, void *ptr) |
| 570 | { |
| 571 | struct generic_pm_domain_data *gpd_data; |
| 572 | struct device *dev; |
| 573 | |
| 574 | gpd_data = container_of(nb, struct generic_pm_domain_data, nb); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 575 | dev = gpd_data->base.dev; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 576 | |
| 577 | for (;;) { |
| 578 | struct generic_pm_domain *genpd; |
| 579 | struct pm_domain_data *pdd; |
| 580 | |
| 581 | spin_lock_irq(&dev->power.lock); |
| 582 | |
| 583 | pdd = dev->power.subsys_data ? |
| 584 | dev->power.subsys_data->domain_data : NULL; |
Viresh Kumar | b4883ca | 2017-05-16 10:52:43 +0530 | [diff] [blame] | 585 | if (pdd) { |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 586 | to_gpd_data(pdd)->td.constraint_changed = true; |
| 587 | genpd = dev_to_genpd(dev); |
| 588 | } else { |
| 589 | genpd = ERR_PTR(-ENODATA); |
| 590 | } |
| 591 | |
| 592 | spin_unlock_irq(&dev->power.lock); |
| 593 | |
| 594 | if (!IS_ERR(genpd)) { |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 595 | genpd_lock(genpd); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 596 | genpd->max_off_time_changed = true; |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 597 | genpd_unlock(genpd); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | dev = dev->parent; |
| 601 | if (!dev || dev->power.ignore_children) |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | return NOTIFY_DONE; |
| 606 | } |
| 607 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 608 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 609 | * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0. |
| 610 | * @work: Work structure used for scheduling the execution of this function. |
| 611 | */ |
| 612 | static void genpd_power_off_work_fn(struct work_struct *work) |
| 613 | { |
| 614 | struct generic_pm_domain *genpd; |
| 615 | |
| 616 | genpd = container_of(work, struct generic_pm_domain, power_off_work); |
| 617 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 618 | genpd_lock(genpd); |
Ulf Hansson | 2da8354 | 2017-02-17 10:55:25 +0100 | [diff] [blame] | 619 | genpd_power_off(genpd, false, 0); |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 620 | genpd_unlock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
Ulf Hansson | 54eeddb | 2016-03-31 11:21:27 +0200 | [diff] [blame] | 624 | * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks |
| 625 | * @dev: Device to handle. |
| 626 | */ |
| 627 | static int __genpd_runtime_suspend(struct device *dev) |
| 628 | { |
| 629 | int (*cb)(struct device *__dev); |
| 630 | |
| 631 | if (dev->type && dev->type->pm) |
| 632 | cb = dev->type->pm->runtime_suspend; |
| 633 | else if (dev->class && dev->class->pm) |
| 634 | cb = dev->class->pm->runtime_suspend; |
| 635 | else if (dev->bus && dev->bus->pm) |
| 636 | cb = dev->bus->pm->runtime_suspend; |
| 637 | else |
| 638 | cb = NULL; |
| 639 | |
| 640 | if (!cb && dev->driver && dev->driver->pm) |
| 641 | cb = dev->driver->pm->runtime_suspend; |
| 642 | |
| 643 | return cb ? cb(dev) : 0; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks |
| 648 | * @dev: Device to handle. |
| 649 | */ |
| 650 | static int __genpd_runtime_resume(struct device *dev) |
| 651 | { |
| 652 | int (*cb)(struct device *__dev); |
| 653 | |
| 654 | if (dev->type && dev->type->pm) |
| 655 | cb = dev->type->pm->runtime_resume; |
| 656 | else if (dev->class && dev->class->pm) |
| 657 | cb = dev->class->pm->runtime_resume; |
| 658 | else if (dev->bus && dev->bus->pm) |
| 659 | cb = dev->bus->pm->runtime_resume; |
| 660 | else |
| 661 | cb = NULL; |
| 662 | |
| 663 | if (!cb && dev->driver && dev->driver->pm) |
| 664 | cb = dev->driver->pm->runtime_resume; |
| 665 | |
| 666 | return cb ? cb(dev) : 0; |
| 667 | } |
| 668 | |
| 669 | /** |
Ulf Hansson | 795bd2e | 2016-03-31 11:21:26 +0200 | [diff] [blame] | 670 | * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 671 | * @dev: Device to suspend. |
| 672 | * |
| 673 | * Carry out a runtime suspend of a device under the assumption that its |
| 674 | * pm_domain field points to the domain member of an object of type |
| 675 | * struct generic_pm_domain representing a PM domain consisting of I/O devices. |
| 676 | */ |
Ulf Hansson | 795bd2e | 2016-03-31 11:21:26 +0200 | [diff] [blame] | 677 | static int genpd_runtime_suspend(struct device *dev) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 678 | { |
| 679 | struct generic_pm_domain *genpd; |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 680 | bool (*suspend_ok)(struct device *__dev); |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 681 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 682 | bool runtime_pm = pm_runtime_enabled(dev); |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 683 | ktime_t time_start; |
| 684 | s64 elapsed_ns; |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 685 | int ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 686 | |
| 687 | dev_dbg(dev, "%s()\n", __func__); |
| 688 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 689 | genpd = dev_to_genpd(dev); |
| 690 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 691 | return -EINVAL; |
| 692 | |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 693 | /* |
| 694 | * A runtime PM centric subsystem/driver may re-use the runtime PM |
| 695 | * callbacks for other purposes than runtime PM. In those scenarios |
| 696 | * runtime PM is disabled. Under these circumstances, we shall skip |
| 697 | * validating/measuring the PM QoS latency. |
| 698 | */ |
Ulf Hansson | 9df3921 | 2016-03-31 11:21:25 +0200 | [diff] [blame] | 699 | suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL; |
| 700 | if (runtime_pm && suspend_ok && !suspend_ok(dev)) |
Rafael J. Wysocki | b02c999 | 2011-12-01 00:02:05 +0100 | [diff] [blame] | 701 | return -EBUSY; |
| 702 | |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 703 | /* Measure suspend latency. */ |
Linus Torvalds | d33d5a6c | 2016-12-25 14:56:58 -0800 | [diff] [blame] | 704 | time_start = 0; |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 705 | if (runtime_pm) |
| 706 | time_start = ktime_get(); |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 707 | |
Ulf Hansson | 54eeddb | 2016-03-31 11:21:27 +0200 | [diff] [blame] | 708 | ret = __genpd_runtime_suspend(dev); |
Rafael J. Wysocki | d5e4cbf | 2011-11-27 13:11:36 +0100 | [diff] [blame] | 709 | if (ret) |
| 710 | return ret; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 711 | |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 712 | ret = genpd_stop_dev(genpd, dev); |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 713 | if (ret) { |
Ulf Hansson | 54eeddb | 2016-03-31 11:21:27 +0200 | [diff] [blame] | 714 | __genpd_runtime_resume(dev); |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 715 | return ret; |
| 716 | } |
| 717 | |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 718 | /* Update suspend latency value if the measured time exceeds it. */ |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 719 | if (runtime_pm) { |
| 720 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
| 721 | if (elapsed_ns > td->suspend_latency_ns) { |
| 722 | td->suspend_latency_ns = elapsed_ns; |
| 723 | dev_dbg(dev, "suspend latency exceeded, %lld ns\n", |
| 724 | elapsed_ns); |
| 725 | genpd->max_off_time_changed = true; |
| 726 | td->constraint_changed = true; |
| 727 | } |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 730 | /* |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 731 | * If power.irq_safe is set, this routine may be run with |
| 732 | * IRQs disabled, so suspend only if the PM domain also is irq_safe. |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 733 | */ |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 734 | if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 735 | return 0; |
| 736 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 737 | genpd_lock(genpd); |
Ulf Hansson | 2da8354 | 2017-02-17 10:55:25 +0100 | [diff] [blame] | 738 | genpd_power_off(genpd, true, 0); |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 739 | genpd_unlock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | /** |
Ulf Hansson | 795bd2e | 2016-03-31 11:21:26 +0200 | [diff] [blame] | 745 | * genpd_runtime_resume - Resume a device belonging to I/O PM domain. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 746 | * @dev: Device to resume. |
| 747 | * |
| 748 | * Carry out a runtime resume 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 | */ |
Ulf Hansson | 795bd2e | 2016-03-31 11:21:26 +0200 | [diff] [blame] | 752 | static int genpd_runtime_resume(struct device *dev) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 753 | { |
| 754 | struct generic_pm_domain *genpd; |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 755 | struct gpd_timing_data *td = &dev_gpd_data(dev)->td; |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 756 | bool runtime_pm = pm_runtime_enabled(dev); |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 757 | ktime_t time_start; |
| 758 | s64 elapsed_ns; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 759 | int ret; |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 760 | bool timed = true; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 761 | |
| 762 | dev_dbg(dev, "%s()\n", __func__); |
| 763 | |
Rafael J. Wysocki | 5248051 | 2011-07-01 22:13:10 +0200 | [diff] [blame] | 764 | genpd = dev_to_genpd(dev); |
| 765 | if (IS_ERR(genpd)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 766 | return -EINVAL; |
| 767 | |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 768 | /* |
| 769 | * As we don't power off a non IRQ safe domain, which holds |
| 770 | * an IRQ safe device, we don't need to restore power to it. |
| 771 | */ |
| 772 | if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) { |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 773 | timed = false; |
| 774 | goto out; |
| 775 | } |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 776 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 777 | genpd_lock(genpd); |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 778 | ret = genpd_power_on(genpd, 0); |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 779 | genpd_unlock(genpd); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 780 | |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 781 | if (ret) |
| 782 | return ret; |
| 783 | |
| 784 | out: |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 785 | /* Measure resume latency. */ |
Augusto Mecking Caringi | ab51e6b | 2016-12-30 11:34:08 +0000 | [diff] [blame] | 786 | time_start = 0; |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 787 | if (timed && runtime_pm) |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 788 | time_start = ktime_get(); |
| 789 | |
Laurent Pinchart | 076395c | 2016-03-02 01:20:38 +0200 | [diff] [blame] | 790 | ret = genpd_start_dev(genpd, dev); |
| 791 | if (ret) |
| 792 | goto err_poweroff; |
| 793 | |
Ulf Hansson | 54eeddb | 2016-03-31 11:21:27 +0200 | [diff] [blame] | 794 | ret = __genpd_runtime_resume(dev); |
Laurent Pinchart | 076395c | 2016-03-02 01:20:38 +0200 | [diff] [blame] | 795 | if (ret) |
| 796 | goto err_stop; |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 797 | |
| 798 | /* Update resume latency value if the measured time exceeds it. */ |
Ulf Hansson | ffe1285 | 2015-11-30 16:21:38 +0100 | [diff] [blame] | 799 | if (timed && runtime_pm) { |
Ulf Hansson | 2b1d88c | 2015-10-15 17:02:19 +0200 | [diff] [blame] | 800 | elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start)); |
| 801 | if (elapsed_ns > td->resume_latency_ns) { |
| 802 | td->resume_latency_ns = elapsed_ns; |
| 803 | dev_dbg(dev, "resume latency exceeded, %lld ns\n", |
| 804 | elapsed_ns); |
| 805 | genpd->max_off_time_changed = true; |
| 806 | td->constraint_changed = true; |
| 807 | } |
| 808 | } |
Ulf Hansson | ba2bbfb | 2015-06-18 15:17:53 +0200 | [diff] [blame] | 809 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 810 | return 0; |
Laurent Pinchart | 076395c | 2016-03-02 01:20:38 +0200 | [diff] [blame] | 811 | |
| 812 | err_stop: |
| 813 | genpd_stop_dev(genpd, dev); |
| 814 | err_poweroff: |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 815 | if (!pm_runtime_is_irq_safe(dev) || |
| 816 | (pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) { |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 817 | genpd_lock(genpd); |
Ulf Hansson | 2da8354 | 2017-02-17 10:55:25 +0100 | [diff] [blame] | 818 | genpd_power_off(genpd, true, 0); |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 819 | genpd_unlock(genpd); |
Laurent Pinchart | 076395c | 2016-03-02 01:20:38 +0200 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | return ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Tushar Behera | 39ac5ba | 2014-03-28 10:50:21 +0530 | [diff] [blame] | 825 | static bool pd_ignore_unused; |
| 826 | static int __init pd_ignore_unused_setup(char *__unused) |
| 827 | { |
| 828 | pd_ignore_unused = true; |
| 829 | return 1; |
| 830 | } |
| 831 | __setup("pd_ignore_unused", pd_ignore_unused_setup); |
| 832 | |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 833 | /** |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 834 | * genpd_power_off_unused - Power off all PM domains with no devices in use. |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 835 | */ |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 836 | static int __init genpd_power_off_unused(void) |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 837 | { |
| 838 | struct generic_pm_domain *genpd; |
| 839 | |
Tushar Behera | 39ac5ba | 2014-03-28 10:50:21 +0530 | [diff] [blame] | 840 | if (pd_ignore_unused) { |
| 841 | pr_warn("genpd: Not disabling unused power domains\n"); |
Ulf Hansson | bb4b72f | 2015-10-06 14:27:42 +0200 | [diff] [blame] | 842 | return 0; |
Tushar Behera | 39ac5ba | 2014-03-28 10:50:21 +0530 | [diff] [blame] | 843 | } |
| 844 | |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 845 | mutex_lock(&gpd_list_lock); |
| 846 | |
| 847 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) |
| 848 | genpd_queue_power_off_work(genpd); |
| 849 | |
| 850 | mutex_unlock(&gpd_list_lock); |
Rafael J. Wysocki | 17f2ae7 | 2011-08-14 13:34:31 +0200 | [diff] [blame] | 851 | |
Ulf Hansson | 2fe71dc | 2014-09-03 12:52:26 +0200 | [diff] [blame] | 852 | return 0; |
| 853 | } |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 854 | late_initcall(genpd_power_off_unused); |
Ulf Hansson | 2fe71dc | 2014-09-03 12:52:26 +0200 | [diff] [blame] | 855 | |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 856 | #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_GENERIC_DOMAINS_OF) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 857 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 858 | static bool genpd_present(const struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 859 | { |
Geert Uytterhoeven | 895b31f | 2014-11-10 19:37:39 +0100 | [diff] [blame] | 860 | const struct generic_pm_domain *gpd; |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 861 | |
| 862 | if (IS_ERR_OR_NULL(genpd)) |
| 863 | return false; |
| 864 | |
| 865 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) |
| 866 | if (gpd == genpd) |
| 867 | return true; |
| 868 | |
| 869 | return false; |
| 870 | } |
| 871 | |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 872 | #endif |
| 873 | |
| 874 | #ifdef CONFIG_PM_SLEEP |
| 875 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 876 | /** |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 877 | * genpd_sync_power_off - Synchronously power off a PM domain and its masters. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 878 | * @genpd: PM domain to power off, if possible. |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 879 | * @use_lock: use the lock. |
| 880 | * @depth: nesting count for lockdep. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 881 | * |
| 882 | * 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] | 883 | * 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] | 884 | * |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 885 | * This function is only called in "noirq" and "syscore" stages of system power |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 886 | * transitions. The "noirq" callbacks may be executed asynchronously, thus in |
| 887 | * these cases the lock must be held. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 888 | */ |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 889 | static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock, |
| 890 | unsigned int depth) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 891 | { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 892 | struct gpd_link *link; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 893 | |
Ulf Hansson | ffaa42e | 2017-03-20 11:19:21 +0100 | [diff] [blame] | 894 | if (!genpd_status_on(genpd) || genpd_is_always_on(genpd)) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 895 | return; |
| 896 | |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 897 | if (genpd->suspended_count != genpd->device_count |
| 898 | || atomic_read(&genpd->sd_count) > 0) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 899 | return; |
| 900 | |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 901 | /* Choose the deepest state when suspending */ |
| 902 | genpd->state_idx = genpd->state_count - 1; |
Ulf Hansson | 1c14967c | 2017-03-20 11:19:22 +0100 | [diff] [blame] | 903 | if (_genpd_power_off(genpd, false)) |
| 904 | return; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 905 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 906 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 907 | |
| 908 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
| 909 | genpd_sd_counter_dec(link->master); |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 910 | |
| 911 | if (use_lock) |
| 912 | genpd_lock_nested(link->master, depth + 1); |
| 913 | |
| 914 | genpd_sync_power_off(link->master, use_lock, depth + 1); |
| 915 | |
| 916 | if (use_lock) |
| 917 | genpd_unlock(link->master); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | /** |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 922 | * genpd_sync_power_on - Synchronously power on a PM domain and its masters. |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 923 | * @genpd: PM domain to power on. |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 924 | * @use_lock: use the lock. |
| 925 | * @depth: nesting count for lockdep. |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 926 | * |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 927 | * This function is only called in "noirq" and "syscore" stages of system power |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 928 | * transitions. The "noirq" callbacks may be executed asynchronously, thus in |
| 929 | * these cases the lock must be held. |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 930 | */ |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 931 | static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock, |
| 932 | unsigned int depth) |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 933 | { |
| 934 | struct gpd_link *link; |
| 935 | |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 936 | if (genpd_status_on(genpd)) |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 937 | return; |
| 938 | |
| 939 | list_for_each_entry(link, &genpd->slave_links, slave_node) { |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 940 | genpd_sd_counter_inc(link->master); |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 941 | |
| 942 | if (use_lock) |
| 943 | genpd_lock_nested(link->master, depth + 1); |
| 944 | |
| 945 | genpd_sync_power_on(link->master, use_lock, depth + 1); |
| 946 | |
| 947 | if (use_lock) |
| 948 | genpd_unlock(link->master); |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 951 | _genpd_power_on(genpd, false); |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 952 | |
| 953 | genpd->status = GPD_STATE_ACTIVE; |
| 954 | } |
| 955 | |
| 956 | /** |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 957 | * resume_needed - Check whether to resume a device before system suspend. |
| 958 | * @dev: Device to check. |
| 959 | * @genpd: PM domain the device belongs to. |
| 960 | * |
| 961 | * There are two cases in which a device that can wake up the system from sleep |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 962 | * states should be resumed by genpd_prepare(): (1) if the device is enabled |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 963 | * to wake up the system and it has to remain active for this purpose while the |
| 964 | * system is in the sleep state and (2) if the device is not enabled to wake up |
| 965 | * the system from sleep states and it generally doesn't generate wakeup signals |
| 966 | * by itself (those signals are generated on its behalf by other parts of the |
| 967 | * system). In the latter case it may be necessary to reconfigure the device's |
| 968 | * wakeup settings during system suspend, because it may have been set up to |
| 969 | * signal remote wakeup from the system's working state as needed by runtime PM. |
| 970 | * Return 'true' in either of the above cases. |
| 971 | */ |
Krzysztof Kozlowski | d8600c8 | 2017-06-12 17:17:41 +0200 | [diff] [blame] | 972 | static bool resume_needed(struct device *dev, |
| 973 | const struct generic_pm_domain *genpd) |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 974 | { |
| 975 | bool active_wakeup; |
| 976 | |
| 977 | if (!device_can_wakeup(dev)) |
| 978 | return false; |
| 979 | |
Geert Uytterhoeven | d0af45f | 2017-11-07 13:48:15 +0100 | [diff] [blame] | 980 | active_wakeup = genpd_is_active_wakeup(genpd); |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 981 | return device_may_wakeup(dev) ? active_wakeup : !active_wakeup; |
| 982 | } |
| 983 | |
| 984 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 985 | * genpd_prepare - Start power transition of a device in a PM domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 986 | * @dev: Device to start the transition of. |
| 987 | * |
| 988 | * Start a power transition of a device (during a system-wide power transition) |
| 989 | * under the assumption that its pm_domain field points to the domain member of |
| 990 | * an object of type struct generic_pm_domain representing a PM domain |
| 991 | * consisting of I/O devices. |
| 992 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 993 | static int genpd_prepare(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 994 | { |
| 995 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 996 | int ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 997 | |
| 998 | dev_dbg(dev, "%s()\n", __func__); |
| 999 | |
| 1000 | genpd = dev_to_genpd(dev); |
| 1001 | if (IS_ERR(genpd)) |
| 1002 | return -EINVAL; |
| 1003 | |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1004 | /* |
| 1005 | * If a wakeup request is pending for the device, it should be woken up |
| 1006 | * at this point and a system wakeup event should be reported if it's |
| 1007 | * set up to wake up the system from sleep states. |
| 1008 | */ |
Rafael J. Wysocki | 4ecd6e6 | 2011-07-12 00:39:57 +0200 | [diff] [blame] | 1009 | if (resume_needed(dev, genpd)) |
| 1010 | pm_runtime_resume(dev); |
| 1011 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1012 | genpd_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1013 | |
Ulf Hansson | 39dd0f2 | 2016-05-30 11:43:07 +0200 | [diff] [blame] | 1014 | if (genpd->prepared_count++ == 0) |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1015 | genpd->suspended_count = 0; |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1016 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1017 | genpd_unlock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1018 | |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 1019 | ret = pm_generic_prepare(dev); |
Ulf Hansson | 5241ab40 | 2017-11-08 10:11:02 +0100 | [diff] [blame] | 1020 | if (ret < 0) { |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1021 | genpd_lock(genpd); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 1022 | |
Ulf Hansson | 39dd0f2 | 2016-05-30 11:43:07 +0200 | [diff] [blame] | 1023 | genpd->prepared_count--; |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 1024 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1025 | genpd_unlock(genpd); |
Rafael J. Wysocki | b6c10c8 | 2011-07-12 00:39:21 +0200 | [diff] [blame] | 1026 | } |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1027 | |
Ulf Hansson | 5241ab40 | 2017-11-08 10:11:02 +0100 | [diff] [blame] | 1028 | /* Never return 1, as genpd don't cope with the direct_complete path. */ |
| 1029 | return ret >= 0 ? 0 : ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /** |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1033 | * genpd_finish_suspend - Completion of suspend or hibernation of device in an |
| 1034 | * I/O pm domain. |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1035 | * @dev: Device to suspend. |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1036 | * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback. |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1037 | * |
| 1038 | * Stop the device and remove power from the domain if all devices in it have |
| 1039 | * been stopped. |
| 1040 | */ |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1041 | static int genpd_finish_suspend(struct device *dev, bool poweroff) |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1042 | { |
| 1043 | struct generic_pm_domain *genpd; |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1044 | int ret = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1045 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1046 | genpd = dev_to_genpd(dev); |
| 1047 | if (IS_ERR(genpd)) |
| 1048 | return -EINVAL; |
| 1049 | |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1050 | if (poweroff) |
| 1051 | ret = pm_generic_poweroff_noirq(dev); |
| 1052 | else |
| 1053 | ret = pm_generic_suspend_noirq(dev); |
| 1054 | if (ret) |
| 1055 | return ret; |
| 1056 | |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1057 | if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd)) |
| 1058 | return 0; |
| 1059 | |
Rafael J. Wysocki | 17218e0 | 2018-01-12 14:10:38 +0100 | [diff] [blame] | 1060 | if (genpd->dev_ops.stop && genpd->dev_ops.start && |
| 1061 | !pm_runtime_status_suspended(dev)) { |
| 1062 | ret = genpd_stop_dev(genpd, dev); |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1063 | if (ret) { |
| 1064 | if (poweroff) |
| 1065 | pm_generic_restore_noirq(dev); |
| 1066 | else |
| 1067 | pm_generic_resume_noirq(dev); |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1068 | return ret; |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1069 | } |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1072 | genpd_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1073 | genpd->suspended_count++; |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1074 | genpd_sync_power_off(genpd, true, 0); |
| 1075 | genpd_unlock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1081 | * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain. |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1082 | * @dev: Device to suspend. |
| 1083 | * |
| 1084 | * Stop the device and remove power from the domain if all devices in it have |
| 1085 | * been stopped. |
| 1086 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1087 | static int genpd_suspend_noirq(struct device *dev) |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1088 | { |
| 1089 | dev_dbg(dev, "%s()\n", __func__); |
| 1090 | |
| 1091 | return genpd_finish_suspend(dev, false); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1095 | * 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] | 1096 | * @dev: Device to resume. |
| 1097 | * |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1098 | * 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] | 1099 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1100 | static int genpd_resume_noirq(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1101 | { |
| 1102 | struct generic_pm_domain *genpd; |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1103 | int ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1104 | |
| 1105 | dev_dbg(dev, "%s()\n", __func__); |
| 1106 | |
| 1107 | genpd = dev_to_genpd(dev); |
| 1108 | if (IS_ERR(genpd)) |
| 1109 | return -EINVAL; |
| 1110 | |
Geert Uytterhoeven | d0af45f | 2017-11-07 13:48:15 +0100 | [diff] [blame] | 1111 | if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd)) |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1112 | return pm_generic_resume_noirq(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1113 | |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1114 | genpd_lock(genpd); |
| 1115 | genpd_sync_power_on(genpd, true, 0); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1116 | genpd->suspended_count--; |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1117 | genpd_unlock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1118 | |
Rafael J. Wysocki | 17218e0 | 2018-01-12 14:10:38 +0100 | [diff] [blame] | 1119 | if (genpd->dev_ops.stop && genpd->dev_ops.start && |
| 1120 | !pm_runtime_status_suspended(dev)) { |
| 1121 | ret = genpd_start_dev(genpd, dev); |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1122 | if (ret) |
| 1123 | return ret; |
| 1124 | } |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1125 | |
Ulf Hansson | a935424 | 2018-01-10 21:31:56 +0100 | [diff] [blame] | 1126 | return pm_generic_resume_noirq(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1130 | * 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] | 1131 | * @dev: Device to freeze. |
| 1132 | * |
| 1133 | * Carry out a late freeze of a device under the assumption that its |
| 1134 | * pm_domain field points to the domain member of an object of type |
| 1135 | * struct generic_pm_domain representing a power domain consisting of I/O |
| 1136 | * devices. |
| 1137 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1138 | static int genpd_freeze_noirq(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1139 | { |
Krzysztof Kozlowski | d8600c8 | 2017-06-12 17:17:41 +0200 | [diff] [blame] | 1140 | const struct generic_pm_domain *genpd; |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1141 | int ret = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1142 | |
| 1143 | dev_dbg(dev, "%s()\n", __func__); |
| 1144 | |
| 1145 | genpd = dev_to_genpd(dev); |
| 1146 | if (IS_ERR(genpd)) |
| 1147 | return -EINVAL; |
| 1148 | |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1149 | ret = pm_generic_freeze_noirq(dev); |
| 1150 | if (ret) |
| 1151 | return ret; |
| 1152 | |
Rafael J. Wysocki | 17218e0 | 2018-01-12 14:10:38 +0100 | [diff] [blame] | 1153 | if (genpd->dev_ops.stop && genpd->dev_ops.start && |
| 1154 | !pm_runtime_status_suspended(dev)) |
| 1155 | ret = genpd_stop_dev(genpd, dev); |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1156 | |
| 1157 | return ret; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1161 | * 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] | 1162 | * @dev: Device to thaw. |
| 1163 | * |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1164 | * Start the device, unless power has been removed from the domain already |
| 1165 | * before the system transition. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1166 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1167 | static int genpd_thaw_noirq(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1168 | { |
Krzysztof Kozlowski | d8600c8 | 2017-06-12 17:17:41 +0200 | [diff] [blame] | 1169 | const struct generic_pm_domain *genpd; |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1170 | int ret = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1171 | |
| 1172 | dev_dbg(dev, "%s()\n", __func__); |
| 1173 | |
| 1174 | genpd = dev_to_genpd(dev); |
| 1175 | if (IS_ERR(genpd)) |
| 1176 | return -EINVAL; |
| 1177 | |
Rafael J. Wysocki | 17218e0 | 2018-01-12 14:10:38 +0100 | [diff] [blame] | 1178 | if (genpd->dev_ops.stop && genpd->dev_ops.start && |
| 1179 | !pm_runtime_status_suspended(dev)) { |
| 1180 | ret = genpd_start_dev(genpd, dev); |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1181 | if (ret) |
| 1182 | return ret; |
| 1183 | } |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1184 | |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1185 | return pm_generic_thaw_noirq(dev); |
| 1186 | } |
| 1187 | |
| 1188 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1189 | * genpd_poweroff_noirq - Completion of hibernation of device in an |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1190 | * I/O PM domain. |
| 1191 | * @dev: Device to poweroff. |
| 1192 | * |
| 1193 | * Stop the device and remove power from the domain if all devices in it have |
| 1194 | * been stopped. |
| 1195 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1196 | static int genpd_poweroff_noirq(struct device *dev) |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1197 | { |
| 1198 | dev_dbg(dev, "%s()\n", __func__); |
| 1199 | |
| 1200 | return genpd_finish_suspend(dev, true); |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1201 | } |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1202 | |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1203 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1204 | * 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] | 1205 | * @dev: Device to resume. |
| 1206 | * |
Rafael J. Wysocki | 0496c8a | 2012-01-29 20:39:02 +0100 | [diff] [blame] | 1207 | * Make sure the domain will be in the same power state as before the |
| 1208 | * 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] | 1209 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1210 | static int genpd_restore_noirq(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1211 | { |
| 1212 | struct generic_pm_domain *genpd; |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1213 | int ret = 0; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1214 | |
| 1215 | dev_dbg(dev, "%s()\n", __func__); |
| 1216 | |
| 1217 | genpd = dev_to_genpd(dev); |
| 1218 | if (IS_ERR(genpd)) |
| 1219 | return -EINVAL; |
| 1220 | |
| 1221 | /* |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1222 | * At this point suspended_count == 0 means we are being run for the |
| 1223 | * first time for the given domain in the present cycle. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1224 | */ |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1225 | genpd_lock(genpd); |
Ulf Hansson | 39dd0f2 | 2016-05-30 11:43:07 +0200 | [diff] [blame] | 1226 | if (genpd->suspended_count++ == 0) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1227 | /* |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1228 | * The boot kernel might put the domain into arbitrary state, |
Ulf Hansson | 86e12ea | 2016-12-08 14:45:20 +0100 | [diff] [blame] | 1229 | * so make it appear as powered off to genpd_sync_power_on(), |
Rafael J. Wysocki | 802d8b4 | 2012-08-06 01:39:16 +0200 | [diff] [blame] | 1230 | * 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] | 1231 | */ |
Rafael J. Wysocki | 65533bb | 2012-03-13 22:39:37 +0100 | [diff] [blame] | 1232 | genpd->status = GPD_STATE_POWER_OFF; |
Rafael J. Wysocki | 18dd2ec | 2012-03-19 10:38:14 +0100 | [diff] [blame] | 1233 | |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1234 | genpd_sync_power_on(genpd, true, 0); |
| 1235 | genpd_unlock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1236 | |
Rafael J. Wysocki | 17218e0 | 2018-01-12 14:10:38 +0100 | [diff] [blame] | 1237 | if (genpd->dev_ops.stop && genpd->dev_ops.start && |
| 1238 | !pm_runtime_status_suspended(dev)) { |
| 1239 | ret = genpd_start_dev(genpd, dev); |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1240 | if (ret) |
| 1241 | return ret; |
| 1242 | } |
Ulf Hansson | 122a223 | 2016-05-30 11:33:14 +0200 | [diff] [blame] | 1243 | |
Mikko Perttunen | 10da654 | 2017-06-22 10:18:33 +0300 | [diff] [blame] | 1244 | return pm_generic_restore_noirq(dev); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /** |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1248 | * genpd_complete - Complete power transition of a device in a power domain. |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1249 | * @dev: Device to complete the transition of. |
| 1250 | * |
| 1251 | * Complete a power transition of a device (during a system-wide power |
| 1252 | * transition) under the assumption that its pm_domain field points to the |
| 1253 | * domain member of an object of type struct generic_pm_domain representing |
| 1254 | * a power domain consisting of I/O devices. |
| 1255 | */ |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1256 | static void genpd_complete(struct device *dev) |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1257 | { |
| 1258 | struct generic_pm_domain *genpd; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1259 | |
| 1260 | dev_dbg(dev, "%s()\n", __func__); |
| 1261 | |
| 1262 | genpd = dev_to_genpd(dev); |
| 1263 | if (IS_ERR(genpd)) |
| 1264 | return; |
| 1265 | |
Ulf Hansson | 4d23a5e | 2016-05-30 11:33:13 +0200 | [diff] [blame] | 1266 | pm_generic_complete(dev); |
| 1267 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1268 | genpd_lock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1269 | |
Ulf Hansson | 39dd0f2 | 2016-05-30 11:43:07 +0200 | [diff] [blame] | 1270 | genpd->prepared_count--; |
Ulf Hansson | 4d23a5e | 2016-05-30 11:33:13 +0200 | [diff] [blame] | 1271 | if (!genpd->prepared_count) |
| 1272 | genpd_queue_power_off_work(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1273 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1274 | genpd_unlock(genpd); |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1275 | } |
| 1276 | |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1277 | /** |
Ulf Hansson | d47e646 | 2014-09-03 12:52:24 +0200 | [diff] [blame] | 1278 | * genpd_syscore_switch - Switch power during system core suspend or resume. |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1279 | * @dev: Device that normally is marked as "always on" to switch power for. |
| 1280 | * |
| 1281 | * This routine may only be called during the system core (syscore) suspend or |
| 1282 | * resume phase for devices whose "always on" flags are set. |
| 1283 | */ |
Ulf Hansson | d47e646 | 2014-09-03 12:52:24 +0200 | [diff] [blame] | 1284 | static void genpd_syscore_switch(struct device *dev, bool suspend) |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1285 | { |
| 1286 | struct generic_pm_domain *genpd; |
| 1287 | |
Rafael J. Wysocki | 23c6d2c | 2017-07-04 21:55:13 +0200 | [diff] [blame] | 1288 | genpd = dev_to_genpd(dev); |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1289 | if (!genpd_present(genpd)) |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1290 | return; |
| 1291 | |
| 1292 | if (suspend) { |
| 1293 | genpd->suspended_count++; |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1294 | genpd_sync_power_off(genpd, false, 0); |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1295 | } else { |
Ulf Hansson | 0883ac0 | 2017-02-08 13:39:00 +0100 | [diff] [blame] | 1296 | genpd_sync_power_on(genpd, false, 0); |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1297 | genpd->suspended_count--; |
| 1298 | } |
| 1299 | } |
Ulf Hansson | d47e646 | 2014-09-03 12:52:24 +0200 | [diff] [blame] | 1300 | |
| 1301 | void pm_genpd_syscore_poweroff(struct device *dev) |
| 1302 | { |
| 1303 | genpd_syscore_switch(dev, true); |
| 1304 | } |
| 1305 | EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff); |
| 1306 | |
| 1307 | void pm_genpd_syscore_poweron(struct device *dev) |
| 1308 | { |
| 1309 | genpd_syscore_switch(dev, false); |
| 1310 | } |
| 1311 | EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron); |
Rafael J. Wysocki | 77f827d | 2012-08-06 01:39:57 +0200 | [diff] [blame] | 1312 | |
Rafael J. Wysocki | d30d819 | 2014-11-27 22:38:05 +0100 | [diff] [blame] | 1313 | #else /* !CONFIG_PM_SLEEP */ |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1314 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1315 | #define genpd_prepare NULL |
| 1316 | #define genpd_suspend_noirq NULL |
| 1317 | #define genpd_resume_noirq NULL |
| 1318 | #define genpd_freeze_noirq NULL |
| 1319 | #define genpd_thaw_noirq NULL |
| 1320 | #define genpd_poweroff_noirq NULL |
| 1321 | #define genpd_restore_noirq NULL |
| 1322 | #define genpd_complete NULL |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1323 | |
| 1324 | #endif /* CONFIG_PM_SLEEP */ |
| 1325 | |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1326 | static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev, |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1327 | struct gpd_timing_data *td) |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1328 | { |
| 1329 | struct generic_pm_domain_data *gpd_data; |
Ulf Hansson | 3e23568 | 2015-01-27 21:13:43 +0100 | [diff] [blame] | 1330 | int ret; |
| 1331 | |
| 1332 | ret = dev_pm_get_subsys_data(dev); |
| 1333 | if (ret) |
| 1334 | return ERR_PTR(ret); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1335 | |
| 1336 | gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL); |
Ulf Hansson | 3e23568 | 2015-01-27 21:13:43 +0100 | [diff] [blame] | 1337 | if (!gpd_data) { |
| 1338 | ret = -ENOMEM; |
| 1339 | goto err_put; |
| 1340 | } |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1341 | |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1342 | if (td) |
| 1343 | gpd_data->td = *td; |
| 1344 | |
| 1345 | gpd_data->base.dev = dev; |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1346 | gpd_data->td.constraint_changed = true; |
Rafael J. Wysocki | 0759e80 | 2017-11-07 11:33:49 +0100 | [diff] [blame] | 1347 | gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS; |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1348 | gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier; |
| 1349 | |
| 1350 | spin_lock_irq(&dev->power.lock); |
| 1351 | |
| 1352 | if (dev->power.subsys_data->domain_data) { |
| 1353 | ret = -EINVAL; |
| 1354 | goto err_free; |
| 1355 | } |
| 1356 | |
| 1357 | dev->power.subsys_data->domain_data = &gpd_data->base; |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1358 | |
| 1359 | spin_unlock_irq(&dev->power.lock); |
| 1360 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1361 | return gpd_data; |
Ulf Hansson | 3e23568 | 2015-01-27 21:13:43 +0100 | [diff] [blame] | 1362 | |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1363 | err_free: |
| 1364 | spin_unlock_irq(&dev->power.lock); |
| 1365 | kfree(gpd_data); |
Ulf Hansson | 3e23568 | 2015-01-27 21:13:43 +0100 | [diff] [blame] | 1366 | err_put: |
| 1367 | dev_pm_put_subsys_data(dev); |
| 1368 | return ERR_PTR(ret); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
Ulf Hansson | 49d400c | 2015-01-27 21:13:38 +0100 | [diff] [blame] | 1371 | static void genpd_free_dev_data(struct device *dev, |
| 1372 | struct generic_pm_domain_data *gpd_data) |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1373 | { |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1374 | spin_lock_irq(&dev->power.lock); |
| 1375 | |
Ulf Hansson | f104e1e | 2015-01-27 21:13:44 +0100 | [diff] [blame] | 1376 | dev->power.subsys_data->domain_data = NULL; |
| 1377 | |
| 1378 | spin_unlock_irq(&dev->power.lock); |
| 1379 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1380 | kfree(gpd_data); |
Ulf Hansson | 3e23568 | 2015-01-27 21:13:43 +0100 | [diff] [blame] | 1381 | dev_pm_put_subsys_data(dev); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1384 | static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev, |
| 1385 | struct gpd_timing_data *td) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1386 | { |
Ulf Hansson | c0356db | 2015-01-27 21:13:42 +0100 | [diff] [blame] | 1387 | struct generic_pm_domain_data *gpd_data; |
Ulf Hansson | b56d9c9 | 2018-04-26 10:53:02 +0200 | [diff] [blame] | 1388 | int ret; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1389 | |
| 1390 | dev_dbg(dev, "%s()\n", __func__); |
| 1391 | |
| 1392 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)) |
| 1393 | return -EINVAL; |
| 1394 | |
Ulf Hansson | 96c1bf6 | 2018-05-29 12:04:16 +0200 | [diff] [blame] | 1395 | gpd_data = genpd_alloc_dev_data(dev, td); |
Ulf Hansson | 3e23568 | 2015-01-27 21:13:43 +0100 | [diff] [blame] | 1396 | if (IS_ERR(gpd_data)) |
| 1397 | return PTR_ERR(gpd_data); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1398 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1399 | genpd_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1400 | |
Ulf Hansson | b472c2f | 2015-01-27 21:13:45 +0100 | [diff] [blame] | 1401 | ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0; |
| 1402 | if (ret) |
| 1403 | goto out; |
Geert Uytterhoeven | d79b6fe | 2014-09-25 18:28:28 +0200 | [diff] [blame] | 1404 | |
Sudeep Holla | 975e83c | 2017-07-14 11:51:48 +0100 | [diff] [blame] | 1405 | dev_pm_domain_set(dev, &genpd->domain); |
| 1406 | |
Ulf Hansson | 14b5306 | 2015-01-27 21:13:40 +0100 | [diff] [blame] | 1407 | genpd->device_count++; |
| 1408 | genpd->max_off_time_changed = true; |
| 1409 | |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1410 | list_add_tail(&gpd_data->base.list_node, &genpd->dev_list); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1411 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1412 | out: |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1413 | genpd_unlock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1414 | |
Ulf Hansson | c0356db | 2015-01-27 21:13:42 +0100 | [diff] [blame] | 1415 | if (ret) |
| 1416 | genpd_free_dev_data(dev, gpd_data); |
| 1417 | else |
| 1418 | dev_pm_qos_add_notifier(dev, &gpd_data->nb); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1419 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1420 | return ret; |
| 1421 | } |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1422 | |
| 1423 | /** |
Ulf Hansson | 1a7a670 | 2018-05-29 12:04:14 +0200 | [diff] [blame] | 1424 | * pm_genpd_add_device - Add a device to an I/O PM domain. |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1425 | * @genpd: PM domain to add the device to. |
| 1426 | * @dev: Device to be added. |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1427 | */ |
Ulf Hansson | 1a7a670 | 2018-05-29 12:04:14 +0200 | [diff] [blame] | 1428 | int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev) |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1429 | { |
| 1430 | int ret; |
| 1431 | |
| 1432 | mutex_lock(&gpd_list_lock); |
Ulf Hansson | 1a7a670 | 2018-05-29 12:04:14 +0200 | [diff] [blame] | 1433 | ret = genpd_add_device(genpd, dev, NULL); |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1434 | mutex_unlock(&gpd_list_lock); |
| 1435 | |
| 1436 | return ret; |
| 1437 | } |
Ulf Hansson | 1a7a670 | 2018-05-29 12:04:14 +0200 | [diff] [blame] | 1438 | EXPORT_SYMBOL_GPL(pm_genpd_add_device); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1439 | |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 1440 | static int genpd_remove_device(struct generic_pm_domain *genpd, |
| 1441 | struct device *dev) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1442 | { |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1443 | struct generic_pm_domain_data *gpd_data; |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 1444 | struct pm_domain_data *pdd; |
Rafael J. Wysocki | efa6902 | 2012-05-01 21:33:53 +0200 | [diff] [blame] | 1445 | int ret = 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1446 | |
| 1447 | dev_dbg(dev, "%s()\n", __func__); |
| 1448 | |
Ulf Hansson | c0356db | 2015-01-27 21:13:42 +0100 | [diff] [blame] | 1449 | pdd = dev->power.subsys_data->domain_data; |
| 1450 | gpd_data = to_gpd_data(pdd); |
| 1451 | dev_pm_qos_remove_notifier(dev, &gpd_data->nb); |
| 1452 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1453 | genpd_lock(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1454 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1455 | if (genpd->prepared_count > 0) { |
| 1456 | ret = -EAGAIN; |
| 1457 | goto out; |
| 1458 | } |
| 1459 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1460 | genpd->device_count--; |
| 1461 | genpd->max_off_time_changed = true; |
| 1462 | |
Geert Uytterhoeven | d79b6fe | 2014-09-25 18:28:28 +0200 | [diff] [blame] | 1463 | if (genpd->detach_dev) |
Ulf Hansson | c16561e | 2014-11-06 00:37:08 +0100 | [diff] [blame] | 1464 | genpd->detach_dev(genpd, dev); |
Geert Uytterhoeven | d79b6fe | 2014-09-25 18:28:28 +0200 | [diff] [blame] | 1465 | |
Sudeep Holla | 975e83c | 2017-07-14 11:51:48 +0100 | [diff] [blame] | 1466 | dev_pm_domain_set(dev, NULL); |
| 1467 | |
Rafael J. Wysocki | efa6902 | 2012-05-01 21:33:53 +0200 | [diff] [blame] | 1468 | list_del_init(&pdd->list_node); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1469 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1470 | genpd_unlock(genpd); |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1471 | |
Ulf Hansson | c1dbe2f | 2015-01-27 21:13:39 +0100 | [diff] [blame] | 1472 | genpd_free_dev_data(dev, gpd_data); |
Rafael J. Wysocki | 1d5fcfe | 2012-07-05 22:12:32 +0200 | [diff] [blame] | 1473 | |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1474 | return 0; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1475 | |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1476 | out: |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1477 | genpd_unlock(genpd); |
Ulf Hansson | c0356db | 2015-01-27 21:13:42 +0100 | [diff] [blame] | 1478 | dev_pm_qos_add_notifier(dev, &gpd_data->nb); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1479 | |
| 1480 | return ret; |
| 1481 | } |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 1482 | |
| 1483 | /** |
| 1484 | * pm_genpd_remove_device - Remove a device from an I/O PM domain. |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 1485 | * @dev: Device to be removed. |
| 1486 | */ |
Ulf Hansson | 924f448 | 2018-05-29 12:04:15 +0200 | [diff] [blame] | 1487 | int pm_genpd_remove_device(struct device *dev) |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 1488 | { |
Ulf Hansson | 924f448 | 2018-05-29 12:04:15 +0200 | [diff] [blame] | 1489 | struct generic_pm_domain *genpd = genpd_lookup_dev(dev); |
| 1490 | |
| 1491 | if (!genpd) |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 1492 | return -EINVAL; |
| 1493 | |
| 1494 | return genpd_remove_device(genpd, dev); |
| 1495 | } |
Maruthi Bayyavarapu | 24c96dc | 2015-11-18 01:12:00 +0530 | [diff] [blame] | 1496 | EXPORT_SYMBOL_GPL(pm_genpd_remove_device); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1497 | |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1498 | static int genpd_add_subdomain(struct generic_pm_domain *genpd, |
| 1499 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1500 | { |
Lina Iyer | 2547923 | 2015-10-28 15:19:50 -0600 | [diff] [blame] | 1501 | struct gpd_link *link, *itr; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1502 | int ret = 0; |
| 1503 | |
Rafael J. Wysocki | fb7268b | 2012-08-07 01:08:37 +0200 | [diff] [blame] | 1504 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain) |
| 1505 | || genpd == subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1506 | return -EINVAL; |
| 1507 | |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * If the domain can be powered on/off in an IRQ safe |
| 1510 | * context, ensure that the subdomain can also be |
| 1511 | * powered on/off in that context. |
| 1512 | */ |
| 1513 | if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) { |
Dan Carpenter | 44cae7d | 2016-11-10 15:52:15 +0300 | [diff] [blame] | 1514 | WARN(1, "Parent %s of subdomain %s must be IRQ safe\n", |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 1515 | genpd->name, subdomain->name); |
| 1516 | return -EINVAL; |
| 1517 | } |
| 1518 | |
Lina Iyer | 2547923 | 2015-10-28 15:19:50 -0600 | [diff] [blame] | 1519 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 1520 | if (!link) |
| 1521 | return -ENOMEM; |
| 1522 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1523 | genpd_lock(subdomain); |
| 1524 | genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1525 | |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 1526 | if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1527 | ret = -EINVAL; |
| 1528 | goto out; |
| 1529 | } |
| 1530 | |
Lina Iyer | 2547923 | 2015-10-28 15:19:50 -0600 | [diff] [blame] | 1531 | list_for_each_entry(itr, &genpd->master_links, master_node) { |
| 1532 | if (itr->slave == subdomain && itr->master == genpd) { |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1533 | ret = -EINVAL; |
| 1534 | goto out; |
| 1535 | } |
| 1536 | } |
| 1537 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1538 | link->master = genpd; |
| 1539 | list_add_tail(&link->master_node, &genpd->master_links); |
Rafael J. Wysocki | bc0403f | 2011-08-08 23:43:59 +0200 | [diff] [blame] | 1540 | link->slave = subdomain; |
| 1541 | list_add_tail(&link->slave_node, &subdomain->slave_links); |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 1542 | if (genpd_status_on(subdomain)) |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1543 | genpd_sd_counter_inc(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1544 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1545 | out: |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1546 | genpd_unlock(genpd); |
| 1547 | genpd_unlock(subdomain); |
Lina Iyer | 2547923 | 2015-10-28 15:19:50 -0600 | [diff] [blame] | 1548 | if (ret) |
| 1549 | kfree(link); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1550 | return ret; |
| 1551 | } |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 1552 | |
| 1553 | /** |
| 1554 | * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 1555 | * @genpd: Master PM domain to add the subdomain to. |
| 1556 | * @subdomain: Subdomain to be added. |
| 1557 | */ |
| 1558 | int pm_genpd_add_subdomain(struct generic_pm_domain *genpd, |
| 1559 | struct generic_pm_domain *subdomain) |
| 1560 | { |
| 1561 | int ret; |
| 1562 | |
| 1563 | mutex_lock(&gpd_list_lock); |
| 1564 | ret = genpd_add_subdomain(genpd, subdomain); |
| 1565 | mutex_unlock(&gpd_list_lock); |
| 1566 | |
| 1567 | return ret; |
| 1568 | } |
Stephen Boyd | d60ee96 | 2015-10-01 12:22:53 -0700 | [diff] [blame] | 1569 | EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1570 | |
| 1571 | /** |
| 1572 | * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain. |
| 1573 | * @genpd: Master PM domain to remove the subdomain from. |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1574 | * @subdomain: Subdomain to be removed. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1575 | */ |
| 1576 | int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd, |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1577 | struct generic_pm_domain *subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1578 | { |
Krzysztof Kozlowski | c6e83ca | 2017-06-28 16:56:18 +0200 | [diff] [blame] | 1579 | struct gpd_link *l, *link; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1580 | int ret = -EINVAL; |
| 1581 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1582 | if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1583 | return -EINVAL; |
| 1584 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1585 | genpd_lock(subdomain); |
| 1586 | genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1587 | |
Jon Hunter | beda5fc | 2016-03-04 10:55:14 +0000 | [diff] [blame] | 1588 | if (!list_empty(&subdomain->master_links) || subdomain->device_count) { |
Jon Hunter | 30e7a65 | 2015-09-03 09:10:37 +0100 | [diff] [blame] | 1589 | pr_warn("%s: unable to remove subdomain %s\n", genpd->name, |
| 1590 | subdomain->name); |
| 1591 | ret = -EBUSY; |
| 1592 | goto out; |
| 1593 | } |
| 1594 | |
Krzysztof Kozlowski | c6e83ca | 2017-06-28 16:56:18 +0200 | [diff] [blame] | 1595 | list_for_each_entry_safe(link, l, &genpd->master_links, master_node) { |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1596 | if (link->slave != subdomain) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1597 | continue; |
| 1598 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1599 | list_del(&link->master_node); |
| 1600 | list_del(&link->slave_node); |
| 1601 | kfree(link); |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 1602 | if (genpd_status_on(subdomain)) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1603 | genpd_sd_counter_dec(genpd); |
| 1604 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1605 | ret = 0; |
| 1606 | break; |
| 1607 | } |
| 1608 | |
Jon Hunter | 30e7a65 | 2015-09-03 09:10:37 +0100 | [diff] [blame] | 1609 | out: |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1610 | genpd_unlock(genpd); |
| 1611 | genpd_unlock(subdomain); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1612 | |
| 1613 | return ret; |
| 1614 | } |
Stephen Boyd | d60ee96 | 2015-10-01 12:22:53 -0700 | [diff] [blame] | 1615 | EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1616 | |
Lina Iyer | 59d65b7 | 2016-10-14 10:47:49 -0700 | [diff] [blame] | 1617 | static int genpd_set_default_power_state(struct generic_pm_domain *genpd) |
| 1618 | { |
| 1619 | struct genpd_power_state *state; |
| 1620 | |
| 1621 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 1622 | if (!state) |
| 1623 | return -ENOMEM; |
| 1624 | |
| 1625 | genpd->states = state; |
| 1626 | genpd->state_count = 1; |
| 1627 | genpd->free = state; |
| 1628 | |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 1632 | static void genpd_lock_init(struct generic_pm_domain *genpd) |
| 1633 | { |
| 1634 | if (genpd->flags & GENPD_FLAG_IRQ_SAFE) { |
| 1635 | spin_lock_init(&genpd->slock); |
| 1636 | genpd->lock_ops = &genpd_spin_ops; |
| 1637 | } else { |
| 1638 | mutex_init(&genpd->mlock); |
| 1639 | genpd->lock_ops = &genpd_mtx_ops; |
| 1640 | } |
| 1641 | } |
| 1642 | |
Rafael J. Wysocki | d23b9b0 | 2011-11-27 13:11:51 +0100 | [diff] [blame] | 1643 | /** |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1644 | * pm_genpd_init - Initialize a generic I/O PM domain object. |
| 1645 | * @genpd: PM domain object to initialize. |
| 1646 | * @gov: PM domain governor to associate with the domain (may be NULL). |
| 1647 | * @is_off: Initial value of the domain's power_is_off field. |
Ulf Hansson | 7eb231c | 2016-06-17 12:27:52 +0200 | [diff] [blame] | 1648 | * |
| 1649 | * Returns 0 on successful initialization, else a negative error code. |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1650 | */ |
Ulf Hansson | 7eb231c | 2016-06-17 12:27:52 +0200 | [diff] [blame] | 1651 | int pm_genpd_init(struct generic_pm_domain *genpd, |
| 1652 | struct dev_power_governor *gov, bool is_off) |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1653 | { |
Lina Iyer | 59d65b7 | 2016-10-14 10:47:49 -0700 | [diff] [blame] | 1654 | int ret; |
| 1655 | |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1656 | if (IS_ERR_OR_NULL(genpd)) |
Ulf Hansson | 7eb231c | 2016-06-17 12:27:52 +0200 | [diff] [blame] | 1657 | return -EINVAL; |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1658 | |
Rafael J. Wysocki | 5063ce1 | 2011-08-08 23:43:40 +0200 | [diff] [blame] | 1659 | INIT_LIST_HEAD(&genpd->master_links); |
| 1660 | INIT_LIST_HEAD(&genpd->slave_links); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1661 | INIT_LIST_HEAD(&genpd->dev_list); |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 1662 | genpd_lock_init(genpd); |
Rafael J. Wysocki | f721889 | 2011-07-01 22:12:45 +0200 | [diff] [blame] | 1663 | genpd->gov = gov; |
| 1664 | INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn); |
Rafael J. Wysocki | c4bb316 | 2011-08-08 23:43:04 +0200 | [diff] [blame] | 1665 | atomic_set(&genpd->sd_count, 0); |
Rafael J. Wysocki | 17b75ec | 2011-07-12 00:39:29 +0200 | [diff] [blame] | 1666 | genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE; |
Rafael J. Wysocki | 596ba34 | 2011-07-01 22:13:19 +0200 | [diff] [blame] | 1667 | genpd->device_count = 0; |
Rafael J. Wysocki | 221e9b5 | 2011-12-01 00:02:10 +0100 | [diff] [blame] | 1668 | genpd->max_off_time_ns = -1; |
Rafael J. Wysocki | 6ff7bb0d0 | 2012-05-01 21:34:07 +0200 | [diff] [blame] | 1669 | genpd->max_off_time_changed = true; |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 1670 | genpd->provider = NULL; |
| 1671 | genpd->has_provider = false; |
Thara Gopinath | afece3a | 2017-07-14 13:10:15 -0400 | [diff] [blame] | 1672 | genpd->accounting_time = ktime_get(); |
Ulf Hansson | 795bd2e | 2016-03-31 11:21:26 +0200 | [diff] [blame] | 1673 | genpd->domain.ops.runtime_suspend = genpd_runtime_suspend; |
| 1674 | genpd->domain.ops.runtime_resume = genpd_runtime_resume; |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 1675 | genpd->domain.ops.prepare = genpd_prepare; |
| 1676 | genpd->domain.ops.suspend_noirq = genpd_suspend_noirq; |
| 1677 | genpd->domain.ops.resume_noirq = genpd_resume_noirq; |
| 1678 | genpd->domain.ops.freeze_noirq = genpd_freeze_noirq; |
| 1679 | genpd->domain.ops.thaw_noirq = genpd_thaw_noirq; |
| 1680 | genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq; |
| 1681 | genpd->domain.ops.restore_noirq = genpd_restore_noirq; |
| 1682 | genpd->domain.ops.complete = genpd_complete; |
Ulf Hansson | c11f6f5 | 2014-12-01 12:50:21 +0100 | [diff] [blame] | 1683 | |
| 1684 | if (genpd->flags & GENPD_FLAG_PM_CLK) { |
| 1685 | genpd->dev_ops.stop = pm_clk_suspend; |
| 1686 | genpd->dev_ops.start = pm_clk_resume; |
| 1687 | } |
| 1688 | |
Ulf Hansson | ffaa42e | 2017-03-20 11:19:21 +0100 | [diff] [blame] | 1689 | /* Always-on domains must be powered on at initialization. */ |
| 1690 | if (genpd_is_always_on(genpd) && !genpd_status_on(genpd)) |
| 1691 | return -EINVAL; |
| 1692 | |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 1693 | /* Use only one "off" state if there were no states declared */ |
Lina Iyer | 59d65b7 | 2016-10-14 10:47:49 -0700 | [diff] [blame] | 1694 | if (genpd->state_count == 0) { |
| 1695 | ret = genpd_set_default_power_state(genpd); |
| 1696 | if (ret) |
| 1697 | return ret; |
Ulf Hansson | 2c9b7f8 | 2018-10-03 16:38:15 +0200 | [diff] [blame] | 1698 | } else if (!gov) { |
| 1699 | pr_warn("%s : no governor for states\n", genpd->name); |
Lina Iyer | 59d65b7 | 2016-10-14 10:47:49 -0700 | [diff] [blame] | 1700 | } |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 1701 | |
Viresh Kumar | 401ea15 | 2017-03-17 11:26:19 +0530 | [diff] [blame] | 1702 | device_initialize(&genpd->dev); |
| 1703 | dev_set_name(&genpd->dev, "%s", genpd->name); |
| 1704 | |
Rafael J. Wysocki | 5125bbf38 | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 1705 | mutex_lock(&gpd_list_lock); |
| 1706 | list_add(&genpd->gpd_list_node, &gpd_list); |
| 1707 | mutex_unlock(&gpd_list_lock); |
Ulf Hansson | 7eb231c | 2016-06-17 12:27:52 +0200 | [diff] [blame] | 1708 | |
| 1709 | return 0; |
Rafael J. Wysocki | 5125bbf38 | 2011-07-13 12:31:52 +0200 | [diff] [blame] | 1710 | } |
Rajendra Nayak | be5ed55 | 2015-08-13 11:51:57 +0530 | [diff] [blame] | 1711 | EXPORT_SYMBOL_GPL(pm_genpd_init); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1712 | |
Jon Hunter | 3fe5771 | 2016-09-12 12:01:13 +0100 | [diff] [blame] | 1713 | static int genpd_remove(struct generic_pm_domain *genpd) |
| 1714 | { |
| 1715 | struct gpd_link *l, *link; |
| 1716 | |
| 1717 | if (IS_ERR_OR_NULL(genpd)) |
| 1718 | return -EINVAL; |
| 1719 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1720 | genpd_lock(genpd); |
Jon Hunter | 3fe5771 | 2016-09-12 12:01:13 +0100 | [diff] [blame] | 1721 | |
| 1722 | if (genpd->has_provider) { |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1723 | genpd_unlock(genpd); |
Jon Hunter | 3fe5771 | 2016-09-12 12:01:13 +0100 | [diff] [blame] | 1724 | pr_err("Provider present, unable to remove %s\n", genpd->name); |
| 1725 | return -EBUSY; |
| 1726 | } |
| 1727 | |
| 1728 | if (!list_empty(&genpd->master_links) || genpd->device_count) { |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1729 | genpd_unlock(genpd); |
Jon Hunter | 3fe5771 | 2016-09-12 12:01:13 +0100 | [diff] [blame] | 1730 | pr_err("%s: unable to remove %s\n", __func__, genpd->name); |
| 1731 | return -EBUSY; |
| 1732 | } |
| 1733 | |
| 1734 | list_for_each_entry_safe(link, l, &genpd->slave_links, slave_node) { |
| 1735 | list_del(&link->master_node); |
| 1736 | list_del(&link->slave_node); |
| 1737 | kfree(link); |
| 1738 | } |
| 1739 | |
| 1740 | list_del(&genpd->gpd_list_node); |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 1741 | genpd_unlock(genpd); |
Jon Hunter | 3fe5771 | 2016-09-12 12:01:13 +0100 | [diff] [blame] | 1742 | cancel_work_sync(&genpd->power_off_work); |
Lina Iyer | 59d65b7 | 2016-10-14 10:47:49 -0700 | [diff] [blame] | 1743 | kfree(genpd->free); |
Jon Hunter | 3fe5771 | 2016-09-12 12:01:13 +0100 | [diff] [blame] | 1744 | pr_debug("%s: removed %s\n", __func__, genpd->name); |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | /** |
| 1750 | * pm_genpd_remove - Remove a generic I/O PM domain |
| 1751 | * @genpd: Pointer to PM domain that is to be removed. |
| 1752 | * |
| 1753 | * To remove the PM domain, this function: |
| 1754 | * - Removes the PM domain as a subdomain to any parent domains, |
| 1755 | * if it was added. |
| 1756 | * - Removes the PM domain from the list of registered PM domains. |
| 1757 | * |
| 1758 | * The PM domain will only be removed, if the associated provider has |
| 1759 | * been removed, it is not a parent to any other PM domain and has no |
| 1760 | * devices associated with it. |
| 1761 | */ |
| 1762 | int pm_genpd_remove(struct generic_pm_domain *genpd) |
| 1763 | { |
| 1764 | int ret; |
| 1765 | |
| 1766 | mutex_lock(&gpd_list_lock); |
| 1767 | ret = genpd_remove(genpd); |
| 1768 | mutex_unlock(&gpd_list_lock); |
| 1769 | |
| 1770 | return ret; |
| 1771 | } |
| 1772 | EXPORT_SYMBOL_GPL(pm_genpd_remove); |
| 1773 | |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1774 | #ifdef CONFIG_PM_GENERIC_DOMAINS_OF |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1775 | |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1776 | /* |
| 1777 | * Device Tree based PM domain providers. |
| 1778 | * |
| 1779 | * The code below implements generic device tree based PM domain providers that |
| 1780 | * bind device tree nodes with generic PM domains registered in the system. |
| 1781 | * |
| 1782 | * Any driver that registers generic PM domains and needs to support binding of |
| 1783 | * devices to these domains is supposed to register a PM domain provider, which |
| 1784 | * maps a PM domain specifier retrieved from the device tree to a PM domain. |
| 1785 | * |
| 1786 | * Two simple mapping functions have been provided for convenience: |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1787 | * - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping. |
| 1788 | * - genpd_xlate_onecell() for mapping of multiple PM domains per node by |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1789 | * index. |
| 1790 | */ |
| 1791 | |
| 1792 | /** |
| 1793 | * struct of_genpd_provider - PM domain provider registration structure |
| 1794 | * @link: Entry in global list of PM domain providers |
| 1795 | * @node: Pointer to device tree node of PM domain provider |
| 1796 | * @xlate: Provider-specific xlate callback mapping a set of specifier cells |
| 1797 | * into a PM domain. |
| 1798 | * @data: context pointer to be passed into @xlate callback |
| 1799 | */ |
| 1800 | struct of_genpd_provider { |
| 1801 | struct list_head link; |
| 1802 | struct device_node *node; |
| 1803 | genpd_xlate_t xlate; |
| 1804 | void *data; |
| 1805 | }; |
| 1806 | |
| 1807 | /* List of registered PM domain providers. */ |
| 1808 | static LIST_HEAD(of_genpd_providers); |
| 1809 | /* Mutex to protect the list above. */ |
| 1810 | static DEFINE_MUTEX(of_genpd_mutex); |
| 1811 | |
| 1812 | /** |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1813 | * genpd_xlate_simple() - Xlate function for direct node-domain mapping |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1814 | * @genpdspec: OF phandle args to map into a PM domain |
| 1815 | * @data: xlate function private data - pointer to struct generic_pm_domain |
| 1816 | * |
| 1817 | * This is a generic xlate function that can be used to model PM domains that |
| 1818 | * have their own device tree nodes. The private data of xlate function needs |
| 1819 | * to be a valid pointer to struct generic_pm_domain. |
| 1820 | */ |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1821 | static struct generic_pm_domain *genpd_xlate_simple( |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1822 | struct of_phandle_args *genpdspec, |
| 1823 | void *data) |
| 1824 | { |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1825 | return data; |
| 1826 | } |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1827 | |
| 1828 | /** |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1829 | * genpd_xlate_onecell() - Xlate function using a single index. |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1830 | * @genpdspec: OF phandle args to map into a PM domain |
| 1831 | * @data: xlate function private data - pointer to struct genpd_onecell_data |
| 1832 | * |
| 1833 | * This is a generic xlate function that can be used to model simple PM domain |
| 1834 | * controllers that have one device tree node and provide multiple PM domains. |
| 1835 | * A single cell is used as an index into an array of PM domains specified in |
| 1836 | * the genpd_onecell_data struct when registering the provider. |
| 1837 | */ |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1838 | static struct generic_pm_domain *genpd_xlate_onecell( |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1839 | struct of_phandle_args *genpdspec, |
| 1840 | void *data) |
| 1841 | { |
| 1842 | struct genpd_onecell_data *genpd_data = data; |
| 1843 | unsigned int idx = genpdspec->args[0]; |
| 1844 | |
| 1845 | if (genpdspec->args_count != 1) |
| 1846 | return ERR_PTR(-EINVAL); |
| 1847 | |
| 1848 | if (idx >= genpd_data->num_domains) { |
| 1849 | pr_err("%s: invalid domain index %u\n", __func__, idx); |
| 1850 | return ERR_PTR(-EINVAL); |
| 1851 | } |
| 1852 | |
| 1853 | if (!genpd_data->domains[idx]) |
| 1854 | return ERR_PTR(-ENOENT); |
| 1855 | |
| 1856 | return genpd_data->domains[idx]; |
| 1857 | } |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1858 | |
| 1859 | /** |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1860 | * genpd_add_provider() - Register a PM domain provider for a node |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1861 | * @np: Device node pointer associated with the PM domain provider. |
| 1862 | * @xlate: Callback for decoding PM domain from phandle arguments. |
| 1863 | * @data: Context pointer for @xlate callback. |
| 1864 | */ |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1865 | static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate, |
| 1866 | void *data) |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1867 | { |
| 1868 | struct of_genpd_provider *cp; |
| 1869 | |
| 1870 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
| 1871 | if (!cp) |
| 1872 | return -ENOMEM; |
| 1873 | |
| 1874 | cp->node = of_node_get(np); |
| 1875 | cp->data = data; |
| 1876 | cp->xlate = xlate; |
| 1877 | |
| 1878 | mutex_lock(&of_genpd_mutex); |
| 1879 | list_add(&cp->link, &of_genpd_providers); |
| 1880 | mutex_unlock(&of_genpd_mutex); |
Rob Herring | ea11e94 | 2017-07-18 16:42:50 -0500 | [diff] [blame] | 1881 | pr_debug("Added domain provider from %pOF\n", np); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 1882 | |
| 1883 | return 0; |
| 1884 | } |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1885 | |
| 1886 | /** |
| 1887 | * of_genpd_add_provider_simple() - Register a simple PM domain provider |
| 1888 | * @np: Device node pointer associated with the PM domain provider. |
| 1889 | * @genpd: Pointer to PM domain associated with the PM domain provider. |
| 1890 | */ |
| 1891 | int of_genpd_add_provider_simple(struct device_node *np, |
| 1892 | struct generic_pm_domain *genpd) |
| 1893 | { |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 1894 | int ret = -EINVAL; |
| 1895 | |
| 1896 | if (!np || !genpd) |
| 1897 | return -EINVAL; |
| 1898 | |
| 1899 | mutex_lock(&gpd_list_lock); |
| 1900 | |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1901 | if (!genpd_present(genpd)) |
| 1902 | goto unlock; |
| 1903 | |
| 1904 | genpd->dev.of_node = np; |
| 1905 | |
| 1906 | /* Parse genpd OPP table */ |
| 1907 | if (genpd->set_performance_state) { |
| 1908 | ret = dev_pm_opp_of_add_table(&genpd->dev); |
| 1909 | if (ret) { |
| 1910 | dev_err(&genpd->dev, "Failed to add OPP table: %d\n", |
| 1911 | ret); |
| 1912 | goto unlock; |
Viresh Kumar | 8ce9584 | 2017-03-17 11:19:21 +0530 | [diff] [blame] | 1913 | } |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 1914 | |
| 1915 | /* |
| 1916 | * Save table for faster processing while setting performance |
| 1917 | * state. |
| 1918 | */ |
| 1919 | genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev); |
| 1920 | WARN_ON(!genpd->opp_table); |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 1921 | } |
| 1922 | |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1923 | ret = genpd_add_provider(np, genpd_xlate_simple, genpd); |
| 1924 | if (ret) { |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 1925 | if (genpd->set_performance_state) { |
| 1926 | dev_pm_opp_put_opp_table(genpd->opp_table); |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1927 | dev_pm_opp_of_remove_table(&genpd->dev); |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 1928 | } |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1929 | |
| 1930 | goto unlock; |
| 1931 | } |
| 1932 | |
| 1933 | genpd->provider = &np->fwnode; |
| 1934 | genpd->has_provider = true; |
| 1935 | |
| 1936 | unlock: |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 1937 | mutex_unlock(&gpd_list_lock); |
| 1938 | |
| 1939 | return ret; |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 1940 | } |
| 1941 | EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple); |
| 1942 | |
| 1943 | /** |
| 1944 | * of_genpd_add_provider_onecell() - Register a onecell PM domain provider |
| 1945 | * @np: Device node pointer associated with the PM domain provider. |
| 1946 | * @data: Pointer to the data associated with the PM domain provider. |
| 1947 | */ |
| 1948 | int of_genpd_add_provider_onecell(struct device_node *np, |
| 1949 | struct genpd_onecell_data *data) |
| 1950 | { |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1951 | struct generic_pm_domain *genpd; |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 1952 | unsigned int i; |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 1953 | int ret = -EINVAL; |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 1954 | |
| 1955 | if (!np || !data) |
| 1956 | return -EINVAL; |
| 1957 | |
| 1958 | mutex_lock(&gpd_list_lock); |
| 1959 | |
Thierry Reding | 4084552 | 2017-03-29 18:34:50 +0200 | [diff] [blame] | 1960 | if (!data->xlate) |
| 1961 | data->xlate = genpd_xlate_onecell; |
| 1962 | |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 1963 | for (i = 0; i < data->num_domains; i++) { |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1964 | genpd = data->domains[i]; |
| 1965 | |
| 1966 | if (!genpd) |
Tomeu Vizoso | 609bed6 | 2016-09-15 14:05:23 +0200 | [diff] [blame] | 1967 | continue; |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1968 | if (!genpd_present(genpd)) |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 1969 | goto error; |
| 1970 | |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1971 | genpd->dev.of_node = np; |
| 1972 | |
| 1973 | /* Parse genpd OPP table */ |
| 1974 | if (genpd->set_performance_state) { |
| 1975 | ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i); |
| 1976 | if (ret) { |
| 1977 | dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n", |
| 1978 | i, ret); |
| 1979 | goto error; |
| 1980 | } |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 1981 | |
| 1982 | /* |
| 1983 | * Save table for faster processing while setting |
| 1984 | * performance state. |
| 1985 | */ |
| 1986 | genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i); |
| 1987 | WARN_ON(!genpd->opp_table); |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | genpd->provider = &np->fwnode; |
| 1991 | genpd->has_provider = true; |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 1992 | } |
| 1993 | |
Thierry Reding | 4084552 | 2017-03-29 18:34:50 +0200 | [diff] [blame] | 1994 | ret = genpd_add_provider(np, data->xlate, data); |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 1995 | if (ret < 0) |
| 1996 | goto error; |
| 1997 | |
| 1998 | mutex_unlock(&gpd_list_lock); |
| 1999 | |
| 2000 | return 0; |
| 2001 | |
| 2002 | error: |
| 2003 | while (i--) { |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 2004 | genpd = data->domains[i]; |
| 2005 | |
| 2006 | if (!genpd) |
Tomeu Vizoso | 609bed6 | 2016-09-15 14:05:23 +0200 | [diff] [blame] | 2007 | continue; |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 2008 | |
| 2009 | genpd->provider = NULL; |
| 2010 | genpd->has_provider = false; |
| 2011 | |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 2012 | if (genpd->set_performance_state) { |
| 2013 | dev_pm_opp_put_opp_table(genpd->opp_table); |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 2014 | dev_pm_opp_of_remove_table(&genpd->dev); |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 2015 | } |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 2016 | } |
Jon Hunter | 0159ec6 | 2016-09-12 12:01:10 +0100 | [diff] [blame] | 2017 | |
| 2018 | mutex_unlock(&gpd_list_lock); |
| 2019 | |
| 2020 | return ret; |
Jon Hunter | 892ebdcc | 2016-09-12 12:01:09 +0100 | [diff] [blame] | 2021 | } |
| 2022 | EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2023 | |
| 2024 | /** |
| 2025 | * of_genpd_del_provider() - Remove a previously registered PM domain provider |
| 2026 | * @np: Device node pointer associated with the PM domain provider |
| 2027 | */ |
| 2028 | void of_genpd_del_provider(struct device_node *np) |
| 2029 | { |
Krzysztof Kozlowski | b556b15 | 2017-06-28 16:56:19 +0200 | [diff] [blame] | 2030 | struct of_genpd_provider *cp, *tmp; |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 2031 | struct generic_pm_domain *gpd; |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2032 | |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 2033 | mutex_lock(&gpd_list_lock); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2034 | mutex_lock(&of_genpd_mutex); |
Krzysztof Kozlowski | b556b15 | 2017-06-28 16:56:19 +0200 | [diff] [blame] | 2035 | list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) { |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2036 | if (cp->node == np) { |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 2037 | /* |
| 2038 | * For each PM domain associated with the |
| 2039 | * provider, set the 'has_provider' to false |
| 2040 | * so that the PM domain can be safely removed. |
| 2041 | */ |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 2042 | list_for_each_entry(gpd, &gpd_list, gpd_list_node) { |
| 2043 | if (gpd->provider == &np->fwnode) { |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 2044 | gpd->has_provider = false; |
| 2045 | |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 2046 | if (!gpd->set_performance_state) |
| 2047 | continue; |
| 2048 | |
Viresh Kumar | 1067ae3 | 2018-11-02 11:18:08 +0530 | [diff] [blame] | 2049 | dev_pm_opp_put_opp_table(gpd->opp_table); |
Viresh Kumar | 6a0ae73 | 2018-04-05 15:53:34 +0530 | [diff] [blame] | 2050 | dev_pm_opp_of_remove_table(&gpd->dev); |
| 2051 | } |
| 2052 | } |
| 2053 | |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2054 | list_del(&cp->link); |
| 2055 | of_node_put(cp->node); |
| 2056 | kfree(cp); |
| 2057 | break; |
| 2058 | } |
| 2059 | } |
| 2060 | mutex_unlock(&of_genpd_mutex); |
Jon Hunter | de0aa06d | 2016-09-12 12:01:12 +0100 | [diff] [blame] | 2061 | mutex_unlock(&gpd_list_lock); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2062 | } |
| 2063 | EXPORT_SYMBOL_GPL(of_genpd_del_provider); |
| 2064 | |
| 2065 | /** |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 2066 | * genpd_get_from_provider() - Look-up PM domain |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2067 | * @genpdspec: OF phandle args to use for look-up |
| 2068 | * |
| 2069 | * Looks for a PM domain provider under the node specified by @genpdspec and if |
| 2070 | * found, uses xlate function of the provider to map phandle args to a PM |
| 2071 | * domain. |
| 2072 | * |
| 2073 | * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR() |
| 2074 | * on failure. |
| 2075 | */ |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 2076 | static struct generic_pm_domain *genpd_get_from_provider( |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2077 | struct of_phandle_args *genpdspec) |
| 2078 | { |
| 2079 | struct generic_pm_domain *genpd = ERR_PTR(-ENOENT); |
| 2080 | struct of_genpd_provider *provider; |
| 2081 | |
Jon Hunter | 41795a8 | 2016-03-04 10:55:15 +0000 | [diff] [blame] | 2082 | if (!genpdspec) |
| 2083 | return ERR_PTR(-EINVAL); |
| 2084 | |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2085 | mutex_lock(&of_genpd_mutex); |
| 2086 | |
| 2087 | /* Check if we have such a provider in our array */ |
| 2088 | list_for_each_entry(provider, &of_genpd_providers, link) { |
| 2089 | if (provider->node == genpdspec->np) |
| 2090 | genpd = provider->xlate(genpdspec, provider->data); |
| 2091 | if (!IS_ERR(genpd)) |
| 2092 | break; |
| 2093 | } |
| 2094 | |
| 2095 | mutex_unlock(&of_genpd_mutex); |
| 2096 | |
| 2097 | return genpd; |
| 2098 | } |
| 2099 | |
| 2100 | /** |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2101 | * of_genpd_add_device() - Add a device to an I/O PM domain |
| 2102 | * @genpdspec: OF phandle args to use for look-up PM domain |
| 2103 | * @dev: Device to be added. |
| 2104 | * |
| 2105 | * Looks-up an I/O PM domain based upon phandle args provided and adds |
| 2106 | * the device to the PM domain. Returns a negative error code on failure. |
| 2107 | */ |
| 2108 | int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev) |
| 2109 | { |
| 2110 | struct generic_pm_domain *genpd; |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2111 | int ret; |
| 2112 | |
| 2113 | mutex_lock(&gpd_list_lock); |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2114 | |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 2115 | genpd = genpd_get_from_provider(genpdspec); |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2116 | if (IS_ERR(genpd)) { |
| 2117 | ret = PTR_ERR(genpd); |
| 2118 | goto out; |
| 2119 | } |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2120 | |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2121 | ret = genpd_add_device(genpd, dev, NULL); |
| 2122 | |
| 2123 | out: |
| 2124 | mutex_unlock(&gpd_list_lock); |
| 2125 | |
| 2126 | return ret; |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2127 | } |
| 2128 | EXPORT_SYMBOL_GPL(of_genpd_add_device); |
| 2129 | |
| 2130 | /** |
| 2131 | * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain. |
| 2132 | * @parent_spec: OF phandle args to use for parent PM domain look-up |
| 2133 | * @subdomain_spec: OF phandle args to use for subdomain look-up |
| 2134 | * |
| 2135 | * Looks-up a parent PM domain and subdomain based upon phandle args |
| 2136 | * provided and adds the subdomain to the parent PM domain. Returns a |
| 2137 | * negative error code on failure. |
| 2138 | */ |
| 2139 | int of_genpd_add_subdomain(struct of_phandle_args *parent_spec, |
| 2140 | struct of_phandle_args *subdomain_spec) |
| 2141 | { |
| 2142 | struct generic_pm_domain *parent, *subdomain; |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2143 | int ret; |
| 2144 | |
| 2145 | mutex_lock(&gpd_list_lock); |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2146 | |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 2147 | parent = genpd_get_from_provider(parent_spec); |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2148 | if (IS_ERR(parent)) { |
| 2149 | ret = PTR_ERR(parent); |
| 2150 | goto out; |
| 2151 | } |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2152 | |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 2153 | subdomain = genpd_get_from_provider(subdomain_spec); |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2154 | if (IS_ERR(subdomain)) { |
| 2155 | ret = PTR_ERR(subdomain); |
| 2156 | goto out; |
| 2157 | } |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2158 | |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2159 | ret = genpd_add_subdomain(parent, subdomain); |
| 2160 | |
| 2161 | out: |
| 2162 | mutex_unlock(&gpd_list_lock); |
| 2163 | |
| 2164 | return ret; |
Jon Hunter | ec69572 | 2016-09-12 12:01:05 +0100 | [diff] [blame] | 2165 | } |
| 2166 | EXPORT_SYMBOL_GPL(of_genpd_add_subdomain); |
| 2167 | |
| 2168 | /** |
Jon Hunter | 1792655 | 2016-09-12 12:01:14 +0100 | [diff] [blame] | 2169 | * of_genpd_remove_last - Remove the last PM domain registered for a provider |
| 2170 | * @provider: Pointer to device structure associated with provider |
| 2171 | * |
| 2172 | * Find the last PM domain that was added by a particular provider and |
| 2173 | * remove this PM domain from the list of PM domains. The provider is |
| 2174 | * identified by the 'provider' device structure that is passed. The PM |
| 2175 | * domain will only be removed, if the provider associated with domain |
| 2176 | * has been removed. |
| 2177 | * |
| 2178 | * Returns a valid pointer to struct generic_pm_domain on success or |
| 2179 | * ERR_PTR() on failure. |
| 2180 | */ |
| 2181 | struct generic_pm_domain *of_genpd_remove_last(struct device_node *np) |
| 2182 | { |
Krzysztof Kozlowski | a7e2d1b | 2017-06-28 16:56:20 +0200 | [diff] [blame] | 2183 | struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT); |
Jon Hunter | 1792655 | 2016-09-12 12:01:14 +0100 | [diff] [blame] | 2184 | int ret; |
| 2185 | |
| 2186 | if (IS_ERR_OR_NULL(np)) |
| 2187 | return ERR_PTR(-EINVAL); |
| 2188 | |
| 2189 | mutex_lock(&gpd_list_lock); |
Krzysztof Kozlowski | a7e2d1b | 2017-06-28 16:56:20 +0200 | [diff] [blame] | 2190 | list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) { |
Jon Hunter | 1792655 | 2016-09-12 12:01:14 +0100 | [diff] [blame] | 2191 | if (gpd->provider == &np->fwnode) { |
| 2192 | ret = genpd_remove(gpd); |
| 2193 | genpd = ret ? ERR_PTR(ret) : gpd; |
| 2194 | break; |
| 2195 | } |
| 2196 | } |
| 2197 | mutex_unlock(&gpd_list_lock); |
| 2198 | |
| 2199 | return genpd; |
| 2200 | } |
| 2201 | EXPORT_SYMBOL_GPL(of_genpd_remove_last); |
| 2202 | |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2203 | static void genpd_release_dev(struct device *dev) |
| 2204 | { |
| 2205 | kfree(dev); |
| 2206 | } |
| 2207 | |
| 2208 | static struct bus_type genpd_bus_type = { |
| 2209 | .name = "genpd", |
| 2210 | }; |
| 2211 | |
Jon Hunter | 1792655 | 2016-09-12 12:01:14 +0100 | [diff] [blame] | 2212 | /** |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2213 | * genpd_dev_pm_detach - Detach a device from its PM domain. |
Jon Hunter | 8bb6944 | 2015-08-27 10:17:00 +0100 | [diff] [blame] | 2214 | * @dev: Device to detach. |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2215 | * @power_off: Currently not used |
| 2216 | * |
| 2217 | * Try to locate a corresponding generic PM domain, which the device was |
| 2218 | * attached to previously. If such is found, the device is detached from it. |
| 2219 | */ |
| 2220 | static void genpd_dev_pm_detach(struct device *dev, bool power_off) |
| 2221 | { |
Russell King | 446d999c | 2015-03-20 17:20:33 +0000 | [diff] [blame] | 2222 | struct generic_pm_domain *pd; |
Geert Uytterhoeven | 93af5e9 | 2015-06-26 11:14:14 +0200 | [diff] [blame] | 2223 | unsigned int i; |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2224 | int ret = 0; |
| 2225 | |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 2226 | pd = dev_to_genpd(dev); |
| 2227 | if (IS_ERR(pd)) |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2228 | return; |
| 2229 | |
| 2230 | dev_dbg(dev, "removing from PM domain %s\n", pd->name); |
| 2231 | |
Geert Uytterhoeven | 93af5e9 | 2015-06-26 11:14:14 +0200 | [diff] [blame] | 2232 | for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) { |
Ulf Hansson | 85168d5 | 2016-09-21 15:38:50 +0200 | [diff] [blame] | 2233 | ret = genpd_remove_device(pd, dev); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2234 | if (ret != -EAGAIN) |
| 2235 | break; |
Geert Uytterhoeven | 93af5e9 | 2015-06-26 11:14:14 +0200 | [diff] [blame] | 2236 | |
| 2237 | mdelay(i); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2238 | cond_resched(); |
| 2239 | } |
| 2240 | |
| 2241 | if (ret < 0) { |
| 2242 | dev_err(dev, "failed to remove from PM domain %s: %d", |
| 2243 | pd->name, ret); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
| 2247 | /* Check if PM domain can be powered off after removing this device. */ |
| 2248 | genpd_queue_power_off_work(pd); |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2249 | |
| 2250 | /* Unregister the device if it was created by genpd. */ |
| 2251 | if (dev->bus == &genpd_bus_type) |
| 2252 | device_unregister(dev); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2253 | } |
| 2254 | |
Russell King | 632f7ce | 2015-03-20 15:55:12 +0100 | [diff] [blame] | 2255 | static void genpd_dev_pm_sync(struct device *dev) |
| 2256 | { |
| 2257 | struct generic_pm_domain *pd; |
| 2258 | |
| 2259 | pd = dev_to_genpd(dev); |
| 2260 | if (IS_ERR(pd)) |
| 2261 | return; |
| 2262 | |
| 2263 | genpd_queue_power_off_work(pd); |
| 2264 | } |
| 2265 | |
Ulf Hansson | 8cb1cbd6 | 2018-05-31 12:59:57 +0200 | [diff] [blame] | 2266 | static int __genpd_dev_pm_attach(struct device *dev, struct device_node *np, |
Ulf Hansson | 895b661 | 2018-06-29 11:15:37 +0200 | [diff] [blame] | 2267 | unsigned int index, bool power_on) |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2268 | { |
| 2269 | struct of_phandle_args pd_args; |
| 2270 | struct generic_pm_domain *pd; |
| 2271 | int ret; |
| 2272 | |
Ulf Hansson | 8cb1cbd6 | 2018-05-31 12:59:57 +0200 | [diff] [blame] | 2273 | ret = of_parse_phandle_with_args(np, "power-domains", |
| 2274 | "#power-domain-cells", index, &pd_args); |
Geert Uytterhoeven | 001d50c | 2017-11-30 12:54:28 +0100 | [diff] [blame] | 2275 | if (ret < 0) |
Ulf Hansson | bcd931f | 2018-05-31 12:59:56 +0200 | [diff] [blame] | 2276 | return ret; |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2277 | |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2278 | mutex_lock(&gpd_list_lock); |
Jon Hunter | f58d4e5 | 2016-09-12 12:01:08 +0100 | [diff] [blame] | 2279 | pd = genpd_get_from_provider(&pd_args); |
Eric Anholt | 265e2cf | 2015-12-01 09:39:31 -0800 | [diff] [blame] | 2280 | of_node_put(pd_args.np); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2281 | if (IS_ERR(pd)) { |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2282 | mutex_unlock(&gpd_list_lock); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2283 | dev_dbg(dev, "%s() failed to find PM domain: %ld\n", |
| 2284 | __func__, PTR_ERR(pd)); |
Rob Herring | e01afc3 | 2018-07-09 09:41:53 -0600 | [diff] [blame] | 2285 | return driver_deferred_probe_check_state(dev); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | dev_dbg(dev, "adding to PM domain %s\n", pd->name); |
| 2289 | |
Ulf Hansson | b56d9c9 | 2018-04-26 10:53:02 +0200 | [diff] [blame] | 2290 | ret = genpd_add_device(pd, dev, NULL); |
Jon Hunter | 19efa5f | 2016-09-12 12:01:11 +0100 | [diff] [blame] | 2291 | mutex_unlock(&gpd_list_lock); |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2292 | |
| 2293 | if (ret < 0) { |
Geert Uytterhoeven | 3499469 | 2016-11-30 13:24:56 +0100 | [diff] [blame] | 2294 | if (ret != -EPROBE_DEFER) |
| 2295 | dev_err(dev, "failed to add to PM domain %s: %d", |
| 2296 | pd->name, ret); |
Ulf Hansson | 919b730 | 2018-05-09 12:17:52 +0200 | [diff] [blame] | 2297 | return ret; |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | dev->pm_domain->detach = genpd_dev_pm_detach; |
Russell King | 632f7ce | 2015-03-20 15:55:12 +0100 | [diff] [blame] | 2301 | dev->pm_domain->sync = genpd_dev_pm_sync; |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2302 | |
Ulf Hansson | 895b661 | 2018-06-29 11:15:37 +0200 | [diff] [blame] | 2303 | if (power_on) { |
| 2304 | genpd_lock(pd); |
| 2305 | ret = genpd_power_on(pd, 0); |
| 2306 | genpd_unlock(pd); |
| 2307 | } |
Ulf Hansson | 72038df | 2018-04-26 10:53:00 +0200 | [diff] [blame] | 2308 | |
| 2309 | if (ret) |
| 2310 | genpd_remove_device(pd, dev); |
Ulf Hansson | 919b730 | 2018-05-09 12:17:52 +0200 | [diff] [blame] | 2311 | |
| 2312 | return ret ? -EPROBE_DEFER : 1; |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2313 | } |
Ulf Hansson | 8cb1cbd6 | 2018-05-31 12:59:57 +0200 | [diff] [blame] | 2314 | |
| 2315 | /** |
| 2316 | * genpd_dev_pm_attach - Attach a device to its PM domain using DT. |
| 2317 | * @dev: Device to attach. |
| 2318 | * |
| 2319 | * Parse device's OF node to find a PM domain specifier. If such is found, |
| 2320 | * attaches the device to retrieved pm_domain ops. |
| 2321 | * |
| 2322 | * Returns 1 on successfully attached PM domain, 0 when the device don't need a |
| 2323 | * PM domain or when multiple power-domains exists for it, else a negative error |
| 2324 | * code. Note that if a power-domain exists for the device, but it cannot be |
| 2325 | * found or turned on, then return -EPROBE_DEFER to ensure that the device is |
| 2326 | * not probed and to re-try again later. |
| 2327 | */ |
| 2328 | int genpd_dev_pm_attach(struct device *dev) |
| 2329 | { |
| 2330 | if (!dev->of_node) |
| 2331 | return 0; |
| 2332 | |
| 2333 | /* |
| 2334 | * Devices with multiple PM domains must be attached separately, as we |
| 2335 | * can only attach one PM domain per device. |
| 2336 | */ |
| 2337 | if (of_count_phandle_with_args(dev->of_node, "power-domains", |
| 2338 | "#power-domain-cells") != 1) |
| 2339 | return 0; |
| 2340 | |
Ulf Hansson | 895b661 | 2018-06-29 11:15:37 +0200 | [diff] [blame] | 2341 | return __genpd_dev_pm_attach(dev, dev->of_node, 0, true); |
Ulf Hansson | 8cb1cbd6 | 2018-05-31 12:59:57 +0200 | [diff] [blame] | 2342 | } |
Tomasz Figa | aa42240 | 2014-09-19 20:27:36 +0200 | [diff] [blame] | 2343 | EXPORT_SYMBOL_GPL(genpd_dev_pm_attach); |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2344 | |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2345 | /** |
| 2346 | * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains. |
| 2347 | * @dev: The device used to lookup the PM domain. |
| 2348 | * @index: The index of the PM domain. |
| 2349 | * |
| 2350 | * Parse device's OF node to find a PM domain specifier at the provided @index. |
| 2351 | * If such is found, creates a virtual device and attaches it to the retrieved |
| 2352 | * pm_domain ops. To deal with detaching of the virtual device, the ->detach() |
| 2353 | * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach(). |
| 2354 | * |
| 2355 | * Returns the created virtual device if successfully attached PM domain, NULL |
| 2356 | * when the device don't need a PM domain, else an ERR_PTR() in case of |
| 2357 | * failures. If a power-domain exists for the device, but cannot be found or |
| 2358 | * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device |
| 2359 | * is not probed and to re-try again later. |
| 2360 | */ |
| 2361 | struct device *genpd_dev_pm_attach_by_id(struct device *dev, |
| 2362 | unsigned int index) |
| 2363 | { |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2364 | struct device *virt_dev; |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2365 | int num_domains; |
| 2366 | int ret; |
| 2367 | |
| 2368 | if (!dev->of_node) |
| 2369 | return NULL; |
| 2370 | |
| 2371 | /* Deal only with devices using multiple PM domains. */ |
| 2372 | num_domains = of_count_phandle_with_args(dev->of_node, "power-domains", |
| 2373 | "#power-domain-cells"); |
| 2374 | if (num_domains < 2 || index >= num_domains) |
| 2375 | return NULL; |
| 2376 | |
| 2377 | /* Allocate and register device on the genpd bus. */ |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2378 | virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL); |
| 2379 | if (!virt_dev) |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2380 | return ERR_PTR(-ENOMEM); |
| 2381 | |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2382 | dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev)); |
| 2383 | virt_dev->bus = &genpd_bus_type; |
| 2384 | virt_dev->release = genpd_release_dev; |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2385 | |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2386 | ret = device_register(virt_dev); |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2387 | if (ret) { |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2388 | kfree(virt_dev); |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2389 | return ERR_PTR(ret); |
| 2390 | } |
| 2391 | |
| 2392 | /* Try to attach the device to the PM domain at the specified index. */ |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2393 | ret = __genpd_dev_pm_attach(virt_dev, dev->of_node, index, false); |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2394 | if (ret < 1) { |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2395 | device_unregister(virt_dev); |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2396 | return ret ? ERR_PTR(ret) : NULL; |
| 2397 | } |
| 2398 | |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2399 | pm_runtime_enable(virt_dev); |
| 2400 | genpd_queue_power_off_work(dev_to_genpd(virt_dev)); |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2401 | |
Viresh Kumar | 560928b | 2018-10-25 09:07:38 +0530 | [diff] [blame] | 2402 | return virt_dev; |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2403 | } |
| 2404 | EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id); |
| 2405 | |
Ulf Hansson | 5d6be70 | 2018-06-29 13:04:31 +0200 | [diff] [blame] | 2406 | /** |
| 2407 | * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains. |
| 2408 | * @dev: The device used to lookup the PM domain. |
| 2409 | * @name: The name of the PM domain. |
| 2410 | * |
| 2411 | * Parse device's OF node to find a PM domain specifier using the |
| 2412 | * power-domain-names DT property. For further description see |
| 2413 | * genpd_dev_pm_attach_by_id(). |
| 2414 | */ |
| 2415 | struct device *genpd_dev_pm_attach_by_name(struct device *dev, char *name) |
| 2416 | { |
| 2417 | int index; |
| 2418 | |
| 2419 | if (!dev->of_node) |
| 2420 | return NULL; |
| 2421 | |
| 2422 | index = of_property_match_string(dev->of_node, "power-domain-names", |
| 2423 | name); |
| 2424 | if (index < 0) |
| 2425 | return NULL; |
| 2426 | |
| 2427 | return genpd_dev_pm_attach_by_id(dev, index); |
| 2428 | } |
| 2429 | |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2430 | static const struct of_device_id idle_state_match[] = { |
Lina Iyer | 598da54 | 2016-11-03 14:54:35 -0700 | [diff] [blame] | 2431 | { .compatible = "domain-idle-state", }, |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2432 | { } |
| 2433 | }; |
| 2434 | |
| 2435 | static int genpd_parse_state(struct genpd_power_state *genpd_state, |
| 2436 | struct device_node *state_node) |
| 2437 | { |
| 2438 | int err; |
| 2439 | u32 residency; |
| 2440 | u32 entry_latency, exit_latency; |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2441 | |
| 2442 | err = of_property_read_u32(state_node, "entry-latency-us", |
| 2443 | &entry_latency); |
| 2444 | if (err) { |
Rob Herring | ea11e94 | 2017-07-18 16:42:50 -0500 | [diff] [blame] | 2445 | pr_debug(" * %pOF missing entry-latency-us property\n", |
| 2446 | state_node); |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2447 | return -EINVAL; |
| 2448 | } |
| 2449 | |
| 2450 | err = of_property_read_u32(state_node, "exit-latency-us", |
| 2451 | &exit_latency); |
| 2452 | if (err) { |
Rob Herring | ea11e94 | 2017-07-18 16:42:50 -0500 | [diff] [blame] | 2453 | pr_debug(" * %pOF missing exit-latency-us property\n", |
| 2454 | state_node); |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2455 | return -EINVAL; |
| 2456 | } |
| 2457 | |
| 2458 | err = of_property_read_u32(state_node, "min-residency-us", &residency); |
| 2459 | if (!err) |
| 2460 | genpd_state->residency_ns = 1000 * residency; |
| 2461 | |
| 2462 | genpd_state->power_on_latency_ns = 1000 * exit_latency; |
| 2463 | genpd_state->power_off_latency_ns = 1000 * entry_latency; |
Lina Iyer | 0c9b694 | 2016-10-14 10:47:52 -0700 | [diff] [blame] | 2464 | genpd_state->fwnode = &state_node->fwnode; |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2465 | |
| 2466 | return 0; |
| 2467 | } |
| 2468 | |
Ulf Hansson | a3381e3 | 2018-01-23 21:43:08 +0100 | [diff] [blame] | 2469 | static int genpd_iterate_idle_states(struct device_node *dn, |
| 2470 | struct genpd_power_state *states) |
| 2471 | { |
| 2472 | int ret; |
| 2473 | struct of_phandle_iterator it; |
| 2474 | struct device_node *np; |
| 2475 | int i = 0; |
| 2476 | |
| 2477 | ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL); |
| 2478 | if (ret <= 0) |
| 2479 | return ret; |
| 2480 | |
| 2481 | /* Loop over the phandles until all the requested entry is found */ |
| 2482 | of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) { |
| 2483 | np = it.node; |
| 2484 | if (!of_match_node(idle_state_match, np)) |
| 2485 | continue; |
| 2486 | if (states) { |
| 2487 | ret = genpd_parse_state(&states[i], np); |
| 2488 | if (ret) { |
| 2489 | pr_err("Parsing idle state node %pOF failed with err %d\n", |
| 2490 | np, ret); |
| 2491 | of_node_put(np); |
| 2492 | return ret; |
| 2493 | } |
| 2494 | } |
| 2495 | i++; |
| 2496 | } |
| 2497 | |
| 2498 | return i; |
| 2499 | } |
| 2500 | |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2501 | /** |
| 2502 | * of_genpd_parse_idle_states: Return array of idle states for the genpd. |
| 2503 | * |
| 2504 | * @dn: The genpd device node |
| 2505 | * @states: The pointer to which the state array will be saved. |
| 2506 | * @n: The count of elements in the array returned from this function. |
| 2507 | * |
| 2508 | * Returns the device states parsed from the OF node. The memory for the states |
| 2509 | * is allocated by this function and is the responsibility of the caller to |
Ulf Hansson | 2c36168 | 2018-10-03 16:38:14 +0200 | [diff] [blame] | 2510 | * free the memory after use. If any or zero compatible domain idle states is |
| 2511 | * found it returns 0 and in case of errors, a negative error code is returned. |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2512 | */ |
| 2513 | int of_genpd_parse_idle_states(struct device_node *dn, |
| 2514 | struct genpd_power_state **states, int *n) |
| 2515 | { |
| 2516 | struct genpd_power_state *st; |
Ulf Hansson | a3381e3 | 2018-01-23 21:43:08 +0100 | [diff] [blame] | 2517 | int ret; |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2518 | |
Ulf Hansson | a3381e3 | 2018-01-23 21:43:08 +0100 | [diff] [blame] | 2519 | ret = genpd_iterate_idle_states(dn, NULL); |
Ulf Hansson | 2c36168 | 2018-10-03 16:38:14 +0200 | [diff] [blame] | 2520 | if (ret < 0) |
| 2521 | return ret; |
| 2522 | |
| 2523 | if (!ret) { |
| 2524 | *states = NULL; |
| 2525 | *n = 0; |
| 2526 | return 0; |
| 2527 | } |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2528 | |
Ulf Hansson | a3381e3 | 2018-01-23 21:43:08 +0100 | [diff] [blame] | 2529 | st = kcalloc(ret, sizeof(*st), GFP_KERNEL); |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2530 | if (!st) |
| 2531 | return -ENOMEM; |
| 2532 | |
Ulf Hansson | a3381e3 | 2018-01-23 21:43:08 +0100 | [diff] [blame] | 2533 | ret = genpd_iterate_idle_states(dn, st); |
| 2534 | if (ret <= 0) { |
| 2535 | kfree(st); |
| 2536 | return ret < 0 ? ret : -EINVAL; |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Ulf Hansson | a3381e3 | 2018-01-23 21:43:08 +0100 | [diff] [blame] | 2539 | *states = st; |
| 2540 | *n = ret; |
Lina Iyer | 30f60428 | 2016-10-14 10:47:51 -0700 | [diff] [blame] | 2541 | |
| 2542 | return 0; |
| 2543 | } |
| 2544 | EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states); |
| 2545 | |
Viresh Kumar | 6e41766 | 2017-11-29 15:21:51 +0530 | [diff] [blame] | 2546 | /** |
Viresh Kumar | e38f89d | 2018-06-13 20:22:04 +0530 | [diff] [blame] | 2547 | * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node. |
| 2548 | * |
| 2549 | * @genpd_dev: Genpd's device for which the performance-state needs to be found. |
| 2550 | * @opp: struct dev_pm_opp of the OPP for which we need to find performance |
| 2551 | * state. |
| 2552 | * |
| 2553 | * Returns performance state encoded in the OPP of the genpd. This calls |
| 2554 | * platform specific genpd->opp_to_performance_state() callback to translate |
| 2555 | * power domain OPP to performance state. |
| 2556 | * |
| 2557 | * Returns performance state on success and 0 on failure. |
| 2558 | */ |
| 2559 | unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev, |
| 2560 | struct dev_pm_opp *opp) |
| 2561 | { |
| 2562 | struct generic_pm_domain *genpd = NULL; |
| 2563 | int state; |
| 2564 | |
| 2565 | genpd = container_of(genpd_dev, struct generic_pm_domain, dev); |
| 2566 | |
| 2567 | if (unlikely(!genpd->opp_to_performance_state)) |
| 2568 | return 0; |
| 2569 | |
| 2570 | genpd_lock(genpd); |
| 2571 | state = genpd->opp_to_performance_state(genpd, opp); |
| 2572 | genpd_unlock(genpd); |
| 2573 | |
| 2574 | return state; |
| 2575 | } |
| 2576 | EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state); |
| 2577 | |
Ulf Hansson | 3c095f3 | 2018-05-31 12:59:58 +0200 | [diff] [blame] | 2578 | static int __init genpd_bus_init(void) |
| 2579 | { |
| 2580 | return bus_register(&genpd_bus_type); |
| 2581 | } |
| 2582 | core_initcall(genpd_bus_init); |
| 2583 | |
Rafael J. Wysocki | d30d819 | 2014-11-27 22:38:05 +0100 | [diff] [blame] | 2584 | #endif /* CONFIG_PM_GENERIC_DOMAINS_OF */ |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2585 | |
| 2586 | |
| 2587 | /*** debugfs support ***/ |
| 2588 | |
Jon Hunter | 8b0510b | 2016-08-11 11:40:05 +0100 | [diff] [blame] | 2589 | #ifdef CONFIG_DEBUG_FS |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2590 | #include <linux/pm.h> |
| 2591 | #include <linux/device.h> |
| 2592 | #include <linux/debugfs.h> |
| 2593 | #include <linux/seq_file.h> |
| 2594 | #include <linux/init.h> |
| 2595 | #include <linux/kobject.h> |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2596 | static struct dentry *genpd_debugfs_dir; |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2597 | |
| 2598 | /* |
| 2599 | * 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] | 2600 | * from sysfs.c, so generalize it. |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2601 | */ |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2602 | static void rtpm_status_str(struct seq_file *s, struct device *dev) |
| 2603 | { |
| 2604 | static const char * const status_lookup[] = { |
| 2605 | [RPM_ACTIVE] = "active", |
| 2606 | [RPM_RESUMING] = "resuming", |
| 2607 | [RPM_SUSPENDED] = "suspended", |
| 2608 | [RPM_SUSPENDING] = "suspending" |
| 2609 | }; |
| 2610 | const char *p = ""; |
| 2611 | |
| 2612 | if (dev->power.runtime_error) |
| 2613 | p = "error"; |
| 2614 | else if (dev->power.disable_depth) |
| 2615 | p = "unsupported"; |
| 2616 | else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup)) |
| 2617 | p = status_lookup[dev->power.runtime_status]; |
| 2618 | else |
| 2619 | WARN_ON(1); |
| 2620 | |
| 2621 | seq_puts(s, p); |
| 2622 | } |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2623 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2624 | static int genpd_summary_one(struct seq_file *s, |
| 2625 | struct generic_pm_domain *genpd) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2626 | { |
| 2627 | static const char * const status_lookup[] = { |
| 2628 | [GPD_STATE_ACTIVE] = "on", |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2629 | [GPD_STATE_POWER_OFF] = "off" |
| 2630 | }; |
| 2631 | struct pm_domain_data *pm_data; |
| 2632 | const char *kobj_path; |
| 2633 | struct gpd_link *link; |
Geert Uytterhoeven | 6954d43 | 2016-02-23 17:49:17 +0100 | [diff] [blame] | 2634 | char state[16]; |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2635 | int ret; |
| 2636 | |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 2637 | ret = genpd_lock_interruptible(genpd); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2638 | if (ret) |
| 2639 | return -ERESTARTSYS; |
| 2640 | |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2641 | if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup))) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2642 | goto exit; |
Ulf Hansson | 41e2c8e | 2017-03-20 11:19:20 +0100 | [diff] [blame] | 2643 | if (!genpd_status_on(genpd)) |
Geert Uytterhoeven | 0ba554e | 2016-02-23 17:49:18 +0100 | [diff] [blame] | 2644 | snprintf(state, sizeof(state), "%s-%u", |
Geert Uytterhoeven | 6954d43 | 2016-02-23 17:49:17 +0100 | [diff] [blame] | 2645 | status_lookup[genpd->status], genpd->state_idx); |
Axel Haslam | fc5cbf0 | 2016-02-15 11:10:51 +0100 | [diff] [blame] | 2646 | else |
Geert Uytterhoeven | 6954d43 | 2016-02-23 17:49:17 +0100 | [diff] [blame] | 2647 | snprintf(state, sizeof(state), "%s", |
| 2648 | status_lookup[genpd->status]); |
| 2649 | seq_printf(s, "%-30s %-15s ", genpd->name, state); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2650 | |
| 2651 | /* |
| 2652 | * Modifications on the list require holding locks on both |
| 2653 | * master and slave, so we are safe. |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2654 | * Also genpd->name is immutable. |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2655 | */ |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2656 | list_for_each_entry(link, &genpd->master_links, master_node) { |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2657 | seq_printf(s, "%s", link->slave->name); |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2658 | if (!list_is_last(&link->master_node, &genpd->master_links)) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2659 | seq_puts(s, ", "); |
| 2660 | } |
| 2661 | |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2662 | list_for_each_entry(pm_data, &genpd->dev_list, list_node) { |
Lina Iyer | d716f47 | 2016-10-14 10:47:55 -0700 | [diff] [blame] | 2663 | kobj_path = kobject_get_path(&pm_data->dev->kobj, |
| 2664 | genpd_is_irq_safe(genpd) ? |
| 2665 | GFP_ATOMIC : GFP_KERNEL); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2666 | if (kobj_path == NULL) |
| 2667 | continue; |
| 2668 | |
| 2669 | seq_printf(s, "\n %-50s ", kobj_path); |
| 2670 | rtpm_status_str(s, pm_data->dev); |
| 2671 | kfree(kobj_path); |
| 2672 | } |
| 2673 | |
| 2674 | seq_puts(s, "\n"); |
| 2675 | exit: |
Lina Iyer | 35241d1 | 2016-10-14 10:47:54 -0700 | [diff] [blame] | 2676 | genpd_unlock(genpd); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2677 | |
| 2678 | return 0; |
| 2679 | } |
| 2680 | |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2681 | static int genpd_summary_show(struct seq_file *s, void *data) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2682 | { |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2683 | struct generic_pm_domain *genpd; |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2684 | int ret = 0; |
| 2685 | |
Geert Uytterhoeven | 15dec67 | 2015-08-11 14:50:49 +0200 | [diff] [blame] | 2686 | seq_puts(s, "domain status slaves\n"); |
| 2687 | seq_puts(s, " /device runtime status\n"); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2688 | seq_puts(s, "----------------------------------------------------------------------\n"); |
| 2689 | |
| 2690 | ret = mutex_lock_interruptible(&gpd_list_lock); |
| 2691 | if (ret) |
| 2692 | return -ERESTARTSYS; |
| 2693 | |
Kevin Hilman | 66a5ca4 | 2015-03-02 11:24:28 -0800 | [diff] [blame] | 2694 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) { |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2695 | ret = genpd_summary_one(s, genpd); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2696 | if (ret) |
| 2697 | break; |
| 2698 | } |
| 2699 | mutex_unlock(&gpd_list_lock); |
| 2700 | |
| 2701 | return ret; |
| 2702 | } |
| 2703 | |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2704 | static int genpd_status_show(struct seq_file *s, void *data) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2705 | { |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2706 | static const char * const status_lookup[] = { |
| 2707 | [GPD_STATE_ACTIVE] = "on", |
| 2708 | [GPD_STATE_POWER_OFF] = "off" |
| 2709 | }; |
| 2710 | |
| 2711 | struct generic_pm_domain *genpd = s->private; |
| 2712 | int ret = 0; |
| 2713 | |
| 2714 | ret = genpd_lock_interruptible(genpd); |
| 2715 | if (ret) |
| 2716 | return -ERESTARTSYS; |
| 2717 | |
| 2718 | if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup))) |
| 2719 | goto exit; |
| 2720 | |
| 2721 | if (genpd->status == GPD_STATE_POWER_OFF) |
| 2722 | seq_printf(s, "%s-%u\n", status_lookup[genpd->status], |
| 2723 | genpd->state_idx); |
| 2724 | else |
| 2725 | seq_printf(s, "%s\n", status_lookup[genpd->status]); |
| 2726 | exit: |
| 2727 | genpd_unlock(genpd); |
| 2728 | return ret; |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2729 | } |
| 2730 | |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2731 | static int genpd_sub_domains_show(struct seq_file *s, void *data) |
| 2732 | { |
| 2733 | struct generic_pm_domain *genpd = s->private; |
| 2734 | struct gpd_link *link; |
| 2735 | int ret = 0; |
| 2736 | |
| 2737 | ret = genpd_lock_interruptible(genpd); |
| 2738 | if (ret) |
| 2739 | return -ERESTARTSYS; |
| 2740 | |
| 2741 | list_for_each_entry(link, &genpd->master_links, master_node) |
| 2742 | seq_printf(s, "%s\n", link->slave->name); |
| 2743 | |
| 2744 | genpd_unlock(genpd); |
| 2745 | return ret; |
| 2746 | } |
| 2747 | |
| 2748 | static int genpd_idle_states_show(struct seq_file *s, void *data) |
| 2749 | { |
| 2750 | struct generic_pm_domain *genpd = s->private; |
| 2751 | unsigned int i; |
| 2752 | int ret = 0; |
| 2753 | |
| 2754 | ret = genpd_lock_interruptible(genpd); |
| 2755 | if (ret) |
| 2756 | return -ERESTARTSYS; |
| 2757 | |
| 2758 | seq_puts(s, "State Time Spent(ms)\n"); |
| 2759 | |
| 2760 | for (i = 0; i < genpd->state_count; i++) { |
| 2761 | ktime_t delta = 0; |
| 2762 | s64 msecs; |
| 2763 | |
| 2764 | if ((genpd->status == GPD_STATE_POWER_OFF) && |
| 2765 | (genpd->state_idx == i)) |
| 2766 | delta = ktime_sub(ktime_get(), genpd->accounting_time); |
| 2767 | |
| 2768 | msecs = ktime_to_ms( |
| 2769 | ktime_add(genpd->states[i].idle_time, delta)); |
| 2770 | seq_printf(s, "S%-13i %lld\n", i, msecs); |
| 2771 | } |
| 2772 | |
| 2773 | genpd_unlock(genpd); |
| 2774 | return ret; |
| 2775 | } |
| 2776 | |
| 2777 | static int genpd_active_time_show(struct seq_file *s, void *data) |
| 2778 | { |
| 2779 | struct generic_pm_domain *genpd = s->private; |
| 2780 | ktime_t delta = 0; |
| 2781 | int ret = 0; |
| 2782 | |
| 2783 | ret = genpd_lock_interruptible(genpd); |
| 2784 | if (ret) |
| 2785 | return -ERESTARTSYS; |
| 2786 | |
| 2787 | if (genpd->status == GPD_STATE_ACTIVE) |
| 2788 | delta = ktime_sub(ktime_get(), genpd->accounting_time); |
| 2789 | |
| 2790 | seq_printf(s, "%lld ms\n", ktime_to_ms( |
| 2791 | ktime_add(genpd->on_time, delta))); |
| 2792 | |
| 2793 | genpd_unlock(genpd); |
| 2794 | return ret; |
| 2795 | } |
| 2796 | |
| 2797 | static int genpd_total_idle_time_show(struct seq_file *s, void *data) |
| 2798 | { |
| 2799 | struct generic_pm_domain *genpd = s->private; |
| 2800 | ktime_t delta = 0, total = 0; |
| 2801 | unsigned int i; |
| 2802 | int ret = 0; |
| 2803 | |
| 2804 | ret = genpd_lock_interruptible(genpd); |
| 2805 | if (ret) |
| 2806 | return -ERESTARTSYS; |
| 2807 | |
| 2808 | for (i = 0; i < genpd->state_count; i++) { |
| 2809 | |
| 2810 | if ((genpd->status == GPD_STATE_POWER_OFF) && |
| 2811 | (genpd->state_idx == i)) |
| 2812 | delta = ktime_sub(ktime_get(), genpd->accounting_time); |
| 2813 | |
| 2814 | total = ktime_add(total, genpd->states[i].idle_time); |
| 2815 | } |
| 2816 | total = ktime_add(total, delta); |
| 2817 | |
| 2818 | seq_printf(s, "%lld ms\n", ktime_to_ms(total)); |
| 2819 | |
| 2820 | genpd_unlock(genpd); |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | |
| 2825 | static int genpd_devices_show(struct seq_file *s, void *data) |
| 2826 | { |
| 2827 | struct generic_pm_domain *genpd = s->private; |
| 2828 | struct pm_domain_data *pm_data; |
| 2829 | const char *kobj_path; |
| 2830 | int ret = 0; |
| 2831 | |
| 2832 | ret = genpd_lock_interruptible(genpd); |
| 2833 | if (ret) |
| 2834 | return -ERESTARTSYS; |
| 2835 | |
| 2836 | list_for_each_entry(pm_data, &genpd->dev_list, list_node) { |
| 2837 | kobj_path = kobject_get_path(&pm_data->dev->kobj, |
| 2838 | genpd_is_irq_safe(genpd) ? |
| 2839 | GFP_ATOMIC : GFP_KERNEL); |
| 2840 | if (kobj_path == NULL) |
| 2841 | continue; |
| 2842 | |
| 2843 | seq_printf(s, "%s\n", kobj_path); |
| 2844 | kfree(kobj_path); |
| 2845 | } |
| 2846 | |
| 2847 | genpd_unlock(genpd); |
| 2848 | return ret; |
| 2849 | } |
| 2850 | |
Rajendra Nayak | e891281 | 2018-05-30 15:15:17 +0530 | [diff] [blame] | 2851 | static int genpd_perf_state_show(struct seq_file *s, void *data) |
| 2852 | { |
| 2853 | struct generic_pm_domain *genpd = s->private; |
| 2854 | |
| 2855 | if (genpd_lock_interruptible(genpd)) |
| 2856 | return -ERESTARTSYS; |
| 2857 | |
| 2858 | seq_printf(s, "%u\n", genpd->performance_state); |
| 2859 | |
| 2860 | genpd_unlock(genpd); |
| 2861 | return 0; |
| 2862 | } |
| 2863 | |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2864 | #define define_genpd_open_function(name) \ |
| 2865 | static int genpd_##name##_open(struct inode *inode, struct file *file) \ |
| 2866 | { \ |
| 2867 | return single_open(file, genpd_##name##_show, inode->i_private); \ |
| 2868 | } |
| 2869 | |
| 2870 | define_genpd_open_function(summary); |
| 2871 | define_genpd_open_function(status); |
| 2872 | define_genpd_open_function(sub_domains); |
| 2873 | define_genpd_open_function(idle_states); |
| 2874 | define_genpd_open_function(active_time); |
| 2875 | define_genpd_open_function(total_idle_time); |
| 2876 | define_genpd_open_function(devices); |
Rajendra Nayak | e891281 | 2018-05-30 15:15:17 +0530 | [diff] [blame] | 2877 | define_genpd_open_function(perf_state); |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2878 | |
| 2879 | #define define_genpd_debugfs_fops(name) \ |
| 2880 | static const struct file_operations genpd_##name##_fops = { \ |
| 2881 | .open = genpd_##name##_open, \ |
| 2882 | .read = seq_read, \ |
| 2883 | .llseek = seq_lseek, \ |
| 2884 | .release = single_release, \ |
| 2885 | } |
| 2886 | |
| 2887 | define_genpd_debugfs_fops(summary); |
| 2888 | define_genpd_debugfs_fops(status); |
| 2889 | define_genpd_debugfs_fops(sub_domains); |
| 2890 | define_genpd_debugfs_fops(idle_states); |
| 2891 | define_genpd_debugfs_fops(active_time); |
| 2892 | define_genpd_debugfs_fops(total_idle_time); |
| 2893 | define_genpd_debugfs_fops(devices); |
Rajendra Nayak | e891281 | 2018-05-30 15:15:17 +0530 | [diff] [blame] | 2894 | define_genpd_debugfs_fops(perf_state); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2895 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2896 | static int __init genpd_debug_init(void) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2897 | { |
| 2898 | struct dentry *d; |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2899 | struct generic_pm_domain *genpd; |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2900 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2901 | genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2902 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2903 | if (!genpd_debugfs_dir) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2904 | return -ENOMEM; |
| 2905 | |
| 2906 | d = debugfs_create_file("pm_genpd_summary", S_IRUGO, |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2907 | genpd_debugfs_dir, NULL, &genpd_summary_fops); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2908 | if (!d) |
| 2909 | return -ENOMEM; |
| 2910 | |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2911 | list_for_each_entry(genpd, &gpd_list, gpd_list_node) { |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2912 | d = debugfs_create_dir(genpd->name, genpd_debugfs_dir); |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2913 | if (!d) |
| 2914 | return -ENOMEM; |
| 2915 | |
| 2916 | debugfs_create_file("current_state", 0444, |
| 2917 | d, genpd, &genpd_status_fops); |
| 2918 | debugfs_create_file("sub_domains", 0444, |
| 2919 | d, genpd, &genpd_sub_domains_fops); |
| 2920 | debugfs_create_file("idle_states", 0444, |
| 2921 | d, genpd, &genpd_idle_states_fops); |
| 2922 | debugfs_create_file("active_time", 0444, |
| 2923 | d, genpd, &genpd_active_time_fops); |
| 2924 | debugfs_create_file("total_idle_time", 0444, |
| 2925 | d, genpd, &genpd_total_idle_time_fops); |
| 2926 | debugfs_create_file("devices", 0444, |
| 2927 | d, genpd, &genpd_devices_fops); |
Rajendra Nayak | e891281 | 2018-05-30 15:15:17 +0530 | [diff] [blame] | 2928 | if (genpd->set_performance_state) |
| 2929 | debugfs_create_file("perf_state", 0444, |
| 2930 | d, genpd, &genpd_perf_state_fops); |
Thara Gopinath | b6a1d09 | 2017-07-14 13:10:16 -0400 | [diff] [blame] | 2931 | } |
| 2932 | |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2933 | return 0; |
| 2934 | } |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2935 | late_initcall(genpd_debug_init); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2936 | |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2937 | static void __exit genpd_debug_exit(void) |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2938 | { |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2939 | debugfs_remove_recursive(genpd_debugfs_dir); |
Maciej Matraszek | 2bd5306 | 2014-09-15 13:09:10 +0200 | [diff] [blame] | 2940 | } |
Ulf Hansson | 9e9704e | 2017-10-06 09:02:06 +0200 | [diff] [blame] | 2941 | __exitcall(genpd_debug_exit); |
Jon Hunter | 8b0510b | 2016-08-11 11:40:05 +0100 | [diff] [blame] | 2942 | #endif /* CONFIG_DEBUG_FS */ |