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