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