blob: 2ff818c2ae6bab2c18072a22a42984f76fa51ddc [file] [log] [blame]
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001/*
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 Uytterhoeven93af5e92015-06-26 11:14:14 +02009#include <linux/delay.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020010#include <linux/kernel.h>
11#include <linux/io.h>
Tomasz Figaaa422402014-09-19 20:27:36 +020012#include <linux/platform_device.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020013#include <linux/pm_runtime.h>
14#include <linux/pm_domain.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020015#include <linux/pm_qos.h>
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010016#include <linux/pm_clock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020017#include <linux/slab.h>
18#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020019#include <linux/sched.h>
20#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010021#include <linux/export.h>
22
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +020023#define GENPD_RETRY_MAX_MS 250 /* Approximate */
24
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010025#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
26({ \
27 type (*__routine)(struct device *__d); \
28 type __ret = (type)0; \
29 \
30 __routine = genpd->dev_ops.callback; \
31 if (__routine) { \
32 __ret = __routine(dev); \
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010033 } \
34 __ret; \
35})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020036
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020037static LIST_HEAD(gpd_list);
38static DEFINE_MUTEX(gpd_list_lock);
39
Russell King446d999c2015-03-20 17:20:33 +000040/*
41 * Get the generic PM domain for a particular struct device.
42 * This validates the struct device pointer, the PM domain pointer,
43 * and checks that the PM domain pointer is a real generic PM domain.
44 * Any failure results in NULL being returned.
45 */
46struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
47{
48 struct generic_pm_domain *genpd = NULL, *gpd;
49
50 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
51 return NULL;
52
53 mutex_lock(&gpd_list_lock);
54 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
55 if (&gpd->domain == dev->pm_domain) {
56 genpd = gpd;
57 break;
58 }
59 }
60 mutex_unlock(&gpd_list_lock);
61
62 return genpd;
63}
64
65/*
66 * This should only be used where we are certain that the pm_domain
67 * attached to the device is a genpd domain.
68 */
69static struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020070{
71 if (IS_ERR_OR_NULL(dev->pm_domain))
72 return ERR_PTR(-EINVAL);
73
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020074 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020075}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020076
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020077static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010078{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020079 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010080}
81
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020082static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010083{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020084 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010085}
86
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020087static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020088{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020089 bool ret = false;
90
91 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
92 ret = !!atomic_dec_and_test(&genpd->sd_count);
93
94 return ret;
95}
96
97static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
98{
99 atomic_inc(&genpd->sd_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100100 smp_mb__after_atomic();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200101}
102
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200103static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100104{
105 ktime_t time_start;
106 s64 elapsed_ns;
107 int ret;
108
109 if (!genpd->power_on)
110 return 0;
111
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200112 if (!timed)
113 return genpd->power_on(genpd);
114
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100115 time_start = ktime_get();
116 ret = genpd->power_on(genpd);
117 if (ret)
118 return ret;
119
120 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
121 if (elapsed_ns <= genpd->power_on_latency_ns)
122 return ret;
123
124 genpd->power_on_latency_ns = elapsed_ns;
125 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000126 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
127 genpd->name, "on", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100128
129 return ret;
130}
131
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200132static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100133{
134 ktime_t time_start;
135 s64 elapsed_ns;
136 int ret;
137
138 if (!genpd->power_off)
139 return 0;
140
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200141 if (!timed)
142 return genpd->power_off(genpd);
143
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100144 time_start = ktime_get();
145 ret = genpd->power_off(genpd);
146 if (ret == -EBUSY)
147 return ret;
148
149 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
150 if (elapsed_ns <= genpd->power_off_latency_ns)
151 return ret;
152
153 genpd->power_off_latency_ns = elapsed_ns;
154 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000155 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
156 genpd->name, "off", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100157
158 return ret;
159}
160
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200161/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200162 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
Ulf Hansson29e47e22015-09-02 10:16:13 +0200163 * @genpd: PM domait to power off.
164 *
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200165 * Queue up the execution of genpd_poweroff() unless it's already been done
Ulf Hansson29e47e22015-09-02 10:16:13 +0200166 * before.
167 */
168static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
169{
170 queue_work(pm_wq, &genpd->power_off_work);
171}
172
173/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200174 * __genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200175 * @genpd: PM domain to power up.
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100176 * @depth: nesting count for lockdep.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200177 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200178 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200179 * resume a device belonging to it.
180 */
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100181static int __genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200182{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200183 struct gpd_link *link;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200184 int ret = 0;
185
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200186 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200187 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200188 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200189
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200190 /*
191 * The list is guaranteed not to change while the loop below is being
192 * executed, unless one of the masters' .power_on() callbacks fiddles
193 * with it.
194 */
195 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100196 struct generic_pm_domain *master = link->master;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200197
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100198 genpd_sd_counter_inc(master);
199
200 mutex_lock_nested(&master->lock, depth + 1);
201 ret = __genpd_poweron(master, depth + 1);
202 mutex_unlock(&master->lock);
203
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200204 if (ret) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100205 genpd_sd_counter_dec(master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200206 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200207 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200208 }
209
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200210 ret = genpd_power_on(genpd, true);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100211 if (ret)
212 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200213
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200214 genpd->status = GPD_STATE_ACTIVE;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200215 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200216
217 err:
Ulf Hansson29e47e22015-09-02 10:16:13 +0200218 list_for_each_entry_continue_reverse(link,
219 &genpd->slave_links,
220 slave_node) {
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200221 genpd_sd_counter_dec(link->master);
Ulf Hansson29e47e22015-09-02 10:16:13 +0200222 genpd_queue_power_off_work(link->master);
223 }
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200224
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200225 return ret;
226}
227
228/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200229 * genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200230 * @genpd: PM domain to power up.
231 */
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200232static int genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200233{
234 int ret;
235
236 mutex_lock(&genpd->lock);
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100237 ret = __genpd_poweron(genpd, 0);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200238 mutex_unlock(&genpd->lock);
239 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200240}
241
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100242
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200243static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
244{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200245 return GENPD_DEV_CALLBACK(genpd, int, save_state, dev);
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200246}
247
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200248static int genpd_restore_dev(struct generic_pm_domain *genpd,
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200249 struct device *dev)
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200250{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200251 return GENPD_DEV_CALLBACK(genpd, int, restore_state, dev);
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200252}
253
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200254static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
255 unsigned long val, void *ptr)
256{
257 struct generic_pm_domain_data *gpd_data;
258 struct device *dev;
259
260 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200261 dev = gpd_data->base.dev;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200262
263 for (;;) {
264 struct generic_pm_domain *genpd;
265 struct pm_domain_data *pdd;
266
267 spin_lock_irq(&dev->power.lock);
268
269 pdd = dev->power.subsys_data ?
270 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200271 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200272 to_gpd_data(pdd)->td.constraint_changed = true;
273 genpd = dev_to_genpd(dev);
274 } else {
275 genpd = ERR_PTR(-ENODATA);
276 }
277
278 spin_unlock_irq(&dev->power.lock);
279
280 if (!IS_ERR(genpd)) {
281 mutex_lock(&genpd->lock);
282 genpd->max_off_time_changed = true;
283 mutex_unlock(&genpd->lock);
284 }
285
286 dev = dev->parent;
287 if (!dev || dev->power.ignore_children)
288 break;
289 }
290
291 return NOTIFY_DONE;
292}
293
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200294/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200295 * genpd_poweroff - Remove power from a given PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200296 * @genpd: PM domain to power down.
Ulf Hanssonf96b3c42015-09-29 15:12:37 +0200297 * @is_async: PM domain is powered down from a scheduled work
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200298 *
299 * If all of the @genpd's devices have been suspended and all of its subdomains
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200300 * have been powered down, remove power from @genpd.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200301 */
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200302static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200303{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200304 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200305 struct gpd_link *link;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200306 unsigned int not_suspended = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200307
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200308 /*
309 * Do not try to power off the domain in the following situations:
310 * (1) The domain is already in the "power off" state.
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200311 * (2) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200312 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200313 if (genpd->status == GPD_STATE_POWER_OFF
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200314 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200315 return 0;
316
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200317 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200318 return -EBUSY;
319
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200320 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
321 enum pm_qos_flags_status stat;
322
323 stat = dev_pm_qos_flags(pdd->dev,
324 PM_QOS_FLAG_NO_POWER_OFF
325 | PM_QOS_FLAG_REMOTE_WAKEUP);
326 if (stat > PM_QOS_FLAGS_NONE)
327 return -EBUSY;
328
Lina Iyer298cd0f2015-10-28 15:18:59 -0600329 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200330 not_suspended++;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200331 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200332
Ulf Hanssonf96b3c42015-09-29 15:12:37 +0200333 if (not_suspended > 1 || (not_suspended == 1 && is_async))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200334 return -EBUSY;
335
336 if (genpd->gov && genpd->gov->power_down_ok) {
337 if (!genpd->gov->power_down_ok(&genpd->domain))
338 return -EAGAIN;
339 }
340
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200341 if (genpd->power_off) {
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200342 int ret;
343
344 if (atomic_read(&genpd->sd_count) > 0)
345 return -EBUSY;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200346
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200347 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200348 * If sd_count > 0 at this point, one of the subdomains hasn't
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200349 * managed to call genpd_poweron() for the master yet after
350 * incrementing it. In that case genpd_poweron() will wait
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200351 * for us to drop the lock, so we can call .power_off() and let
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200352 * the genpd_poweron() restore power for us (this shouldn't
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200353 * happen very often).
354 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200355 ret = genpd_power_off(genpd, true);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200356 if (ret)
357 return ret;
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200358 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200359
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200360 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100361
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200362 list_for_each_entry(link, &genpd->slave_links, slave_node) {
363 genpd_sd_counter_dec(link->master);
364 genpd_queue_power_off_work(link->master);
365 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200366
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200367 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200368}
369
370/**
371 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
372 * @work: Work structure used for scheduling the execution of this function.
373 */
374static void genpd_power_off_work_fn(struct work_struct *work)
375{
376 struct generic_pm_domain *genpd;
377
378 genpd = container_of(work, struct generic_pm_domain, power_off_work);
379
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200380 mutex_lock(&genpd->lock);
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200381 genpd_poweroff(genpd, true);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200382 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200383}
384
385/**
386 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
387 * @dev: Device to suspend.
388 *
389 * Carry out a runtime suspend of a device under the assumption that its
390 * pm_domain field points to the domain member of an object of type
391 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
392 */
393static int pm_genpd_runtime_suspend(struct device *dev)
394{
395 struct generic_pm_domain *genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100396 bool (*stop_ok)(struct device *__dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200397 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100398 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200399 ktime_t time_start;
400 s64 elapsed_ns;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100401 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200402
403 dev_dbg(dev, "%s()\n", __func__);
404
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200405 genpd = dev_to_genpd(dev);
406 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200407 return -EINVAL;
408
Ulf Hanssonffe12852015-11-30 16:21:38 +0100409 /*
410 * A runtime PM centric subsystem/driver may re-use the runtime PM
411 * callbacks for other purposes than runtime PM. In those scenarios
412 * runtime PM is disabled. Under these circumstances, we shall skip
413 * validating/measuring the PM QoS latency.
414 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100415 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100416 if (runtime_pm && stop_ok && !stop_ok(dev))
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100417 return -EBUSY;
418
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200419 /* Measure suspend latency. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100420 if (runtime_pm)
421 time_start = ktime_get();
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200422
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200423 ret = genpd_save_dev(genpd, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100424 if (ret)
425 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200426
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200427 ret = genpd_stop_dev(genpd, dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200428 if (ret) {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200429 genpd_restore_dev(genpd, dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200430 return ret;
431 }
432
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200433 /* Update suspend latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100434 if (runtime_pm) {
435 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
436 if (elapsed_ns > td->suspend_latency_ns) {
437 td->suspend_latency_ns = elapsed_ns;
438 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
439 elapsed_ns);
440 genpd->max_off_time_changed = true;
441 td->constraint_changed = true;
442 }
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200443 }
444
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200445 /*
446 * If power.irq_safe is set, this routine will be run with interrupts
447 * off, so it can't use mutexes.
448 */
449 if (dev->power.irq_safe)
450 return 0;
451
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200452 mutex_lock(&genpd->lock);
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200453 genpd_poweroff(genpd, false);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200454 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200455
456 return 0;
457}
458
459/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200460 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
461 * @dev: Device to resume.
462 *
463 * Carry out a runtime resume of a device under the assumption that its
464 * pm_domain field points to the domain member of an object of type
465 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
466 */
467static int pm_genpd_runtime_resume(struct device *dev)
468{
469 struct generic_pm_domain *genpd;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200470 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100471 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200472 ktime_t time_start;
473 s64 elapsed_ns;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200474 int ret;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200475 bool timed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200476
477 dev_dbg(dev, "%s()\n", __func__);
478
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200479 genpd = dev_to_genpd(dev);
480 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200481 return -EINVAL;
482
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200483 /* If power.irq_safe, the PM domain is never powered off. */
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200484 if (dev->power.irq_safe) {
485 timed = false;
486 goto out;
487 }
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200488
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200489 mutex_lock(&genpd->lock);
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100490 ret = __genpd_poweron(genpd, 0);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200491 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200492
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200493 if (ret)
494 return ret;
495
496 out:
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200497 /* Measure resume latency. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100498 if (timed && runtime_pm)
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200499 time_start = ktime_get();
500
501 genpd_start_dev(genpd, dev);
502 genpd_restore_dev(genpd, dev);
503
504 /* Update resume latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100505 if (timed && runtime_pm) {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200506 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
507 if (elapsed_ns > td->resume_latency_ns) {
508 td->resume_latency_ns = elapsed_ns;
509 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
510 elapsed_ns);
511 genpd->max_off_time_changed = true;
512 td->constraint_changed = true;
513 }
514 }
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200515
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200516 return 0;
517}
518
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530519static bool pd_ignore_unused;
520static int __init pd_ignore_unused_setup(char *__unused)
521{
522 pd_ignore_unused = true;
523 return 1;
524}
525__setup("pd_ignore_unused", pd_ignore_unused_setup);
526
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200527/**
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200528 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200529 */
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200530static int __init genpd_poweroff_unused(void)
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200531{
532 struct generic_pm_domain *genpd;
533
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530534 if (pd_ignore_unused) {
535 pr_warn("genpd: Not disabling unused power domains\n");
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200536 return 0;
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530537 }
538
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200539 mutex_lock(&gpd_list_lock);
540
541 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
542 genpd_queue_power_off_work(genpd);
543
544 mutex_unlock(&gpd_list_lock);
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200545
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200546 return 0;
547}
548late_initcall(genpd_poweroff_unused);
549
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200550#ifdef CONFIG_PM_SLEEP
551
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200552/**
553 * pm_genpd_present - Check if the given PM domain has been initialized.
554 * @genpd: PM domain to check.
555 */
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100556static bool pm_genpd_present(const struct generic_pm_domain *genpd)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200557{
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100558 const struct generic_pm_domain *gpd;
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200559
560 if (IS_ERR_OR_NULL(genpd))
561 return false;
562
563 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
564 if (gpd == genpd)
565 return true;
566
567 return false;
568}
569
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100570static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
571 struct device *dev)
572{
573 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
574}
575
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200576/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200577 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200578 * @genpd: PM domain to power off, if possible.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200579 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200580 *
581 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200582 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200583 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200584 * This function is only called in "noirq" and "syscore" stages of system power
585 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
586 * executed sequentially, so it is guaranteed that it will never run twice in
587 * parallel).
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200588 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200589static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
590 bool timed)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200591{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200592 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200593
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200594 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200595 return;
596
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200597 if (genpd->suspended_count != genpd->device_count
598 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200599 return;
600
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200601 genpd_power_off(genpd, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200602
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200603 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200604
605 list_for_each_entry(link, &genpd->slave_links, slave_node) {
606 genpd_sd_counter_dec(link->master);
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200607 pm_genpd_sync_poweroff(link->master, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200608 }
609}
610
611/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200612 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
613 * @genpd: PM domain to power on.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200614 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200615 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200616 * This function is only called in "noirq" and "syscore" stages of system power
617 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
618 * executed sequentially, so it is guaranteed that it will never run twice in
619 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200620 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200621static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
622 bool timed)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200623{
624 struct gpd_link *link;
625
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200626 if (genpd->status == GPD_STATE_ACTIVE)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200627 return;
628
629 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200630 pm_genpd_sync_poweron(link->master, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200631 genpd_sd_counter_inc(link->master);
632 }
633
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200634 genpd_power_on(genpd, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200635
636 genpd->status = GPD_STATE_ACTIVE;
637}
638
639/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200640 * resume_needed - Check whether to resume a device before system suspend.
641 * @dev: Device to check.
642 * @genpd: PM domain the device belongs to.
643 *
644 * There are two cases in which a device that can wake up the system from sleep
645 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
646 * to wake up the system and it has to remain active for this purpose while the
647 * system is in the sleep state and (2) if the device is not enabled to wake up
648 * the system from sleep states and it generally doesn't generate wakeup signals
649 * by itself (those signals are generated on its behalf by other parts of the
650 * system). In the latter case it may be necessary to reconfigure the device's
651 * wakeup settings during system suspend, because it may have been set up to
652 * signal remote wakeup from the system's working state as needed by runtime PM.
653 * Return 'true' in either of the above cases.
654 */
655static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
656{
657 bool active_wakeup;
658
659 if (!device_can_wakeup(dev))
660 return false;
661
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100662 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200663 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
664}
665
666/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200667 * pm_genpd_prepare - Start power transition of a device in a PM domain.
668 * @dev: Device to start the transition of.
669 *
670 * Start a power transition of a device (during a system-wide power transition)
671 * under the assumption that its pm_domain field points to the domain member of
672 * an object of type struct generic_pm_domain representing a PM domain
673 * consisting of I/O devices.
674 */
675static int pm_genpd_prepare(struct device *dev)
676{
677 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200678 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200679
680 dev_dbg(dev, "%s()\n", __func__);
681
682 genpd = dev_to_genpd(dev);
683 if (IS_ERR(genpd))
684 return -EINVAL;
685
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200686 /*
687 * If a wakeup request is pending for the device, it should be woken up
688 * at this point and a system wakeup event should be reported if it's
689 * set up to wake up the system from sleep states.
690 */
691 pm_runtime_get_noresume(dev);
692 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
693 pm_wakeup_event(dev, 0);
694
695 if (pm_wakeup_pending()) {
Ulf Hansson84167032013-04-12 09:41:44 +0000696 pm_runtime_put(dev);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200697 return -EBUSY;
698 }
699
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200700 if (resume_needed(dev, genpd))
701 pm_runtime_resume(dev);
702
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200703 mutex_lock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200704
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100705 if (genpd->prepared_count++ == 0) {
706 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200707 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100708 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200709
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200710 mutex_unlock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200711
712 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200713 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200714 return 0;
715 }
716
717 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200718 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200719 * so genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100720 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200721 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200722 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200723 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200724 __pm_runtime_disable(dev, false);
725
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200726 ret = pm_generic_prepare(dev);
727 if (ret) {
728 mutex_lock(&genpd->lock);
729
730 if (--genpd->prepared_count == 0)
731 genpd->suspend_power_off = false;
732
733 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200734 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200735 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200736
Ulf Hansson84167032013-04-12 09:41:44 +0000737 pm_runtime_put(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200738 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200739}
740
741/**
742 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
743 * @dev: Device to suspend.
744 *
745 * Suspend a device under the assumption that its pm_domain field points to the
746 * domain member of an object of type struct generic_pm_domain representing
747 * a PM domain consisting of I/O devices.
748 */
749static int pm_genpd_suspend(struct device *dev)
750{
751 struct generic_pm_domain *genpd;
752
753 dev_dbg(dev, "%s()\n", __func__);
754
755 genpd = dev_to_genpd(dev);
756 if (IS_ERR(genpd))
757 return -EINVAL;
758
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200759 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200760}
761
762/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100763 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200764 * @dev: Device to suspend.
765 *
766 * Carry out a late suspend of a device under the assumption that its
767 * pm_domain field points to the domain member of an object of type
768 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
769 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100770static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200771{
772 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200773
774 dev_dbg(dev, "%s()\n", __func__);
775
776 genpd = dev_to_genpd(dev);
777 if (IS_ERR(genpd))
778 return -EINVAL;
779
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200780 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100781}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200782
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100783/**
784 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
785 * @dev: Device to suspend.
786 *
787 * Stop the device and remove power from the domain if all devices in it have
788 * been stopped.
789 */
790static int pm_genpd_suspend_noirq(struct device *dev)
791{
792 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200793
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100794 dev_dbg(dev, "%s()\n", __func__);
795
796 genpd = dev_to_genpd(dev);
797 if (IS_ERR(genpd))
798 return -EINVAL;
799
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200800 if (genpd->suspend_power_off
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100801 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200802 return 0;
803
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200804 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200805
806 /*
807 * Since all of the "noirq" callbacks are executed sequentially, it is
808 * guaranteed that this function will never run twice in parallel for
809 * the same PM domain, so it is not necessary to use locking here.
810 */
811 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200812 pm_genpd_sync_poweroff(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200813
814 return 0;
815}
816
817/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100818 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200819 * @dev: Device to resume.
820 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100821 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200822 */
823static int pm_genpd_resume_noirq(struct device *dev)
824{
825 struct generic_pm_domain *genpd;
826
827 dev_dbg(dev, "%s()\n", __func__);
828
829 genpd = dev_to_genpd(dev);
830 if (IS_ERR(genpd))
831 return -EINVAL;
832
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200833 if (genpd->suspend_power_off
Rafael J. Wysockicc85b202012-03-13 22:39:31 +0100834 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200835 return 0;
836
837 /*
838 * Since all of the "noirq" callbacks are executed sequentially, it is
839 * guaranteed that this function will never run twice in parallel for
840 * the same PM domain, so it is not necessary to use locking here.
841 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200842 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200843 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200844
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200845 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200846}
847
848/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100849 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
850 * @dev: Device to resume.
851 *
852 * Carry out an early resume of a device under the assumption that its
853 * pm_domain field points to the domain member of an object of type
854 * struct generic_pm_domain representing a power domain consisting of I/O
855 * devices.
856 */
857static int pm_genpd_resume_early(struct device *dev)
858{
859 struct generic_pm_domain *genpd;
860
861 dev_dbg(dev, "%s()\n", __func__);
862
863 genpd = dev_to_genpd(dev);
864 if (IS_ERR(genpd))
865 return -EINVAL;
866
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200867 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100868}
869
870/**
871 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200872 * @dev: Device to resume.
873 *
874 * Resume a device under the assumption that its pm_domain field points to the
875 * domain member of an object of type struct generic_pm_domain representing
876 * a power domain consisting of I/O devices.
877 */
878static int pm_genpd_resume(struct device *dev)
879{
880 struct generic_pm_domain *genpd;
881
882 dev_dbg(dev, "%s()\n", __func__);
883
884 genpd = dev_to_genpd(dev);
885 if (IS_ERR(genpd))
886 return -EINVAL;
887
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200888 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200889}
890
891/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100892 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200893 * @dev: Device to freeze.
894 *
895 * Freeze a device under the assumption that its pm_domain field points to the
896 * domain member of an object of type struct generic_pm_domain representing
897 * a power domain consisting of I/O devices.
898 */
899static int pm_genpd_freeze(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 Hansson1e0407c2014-09-03 12:52:19 +0200909 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200910}
911
912/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100913 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
914 * @dev: Device to freeze.
915 *
916 * Carry out a late freeze of a device under the assumption that its
917 * pm_domain field points to the domain member of an object of type
918 * struct generic_pm_domain representing a power domain consisting of I/O
919 * devices.
920 */
921static int pm_genpd_freeze_late(struct device *dev)
922{
923 struct generic_pm_domain *genpd;
924
925 dev_dbg(dev, "%s()\n", __func__);
926
927 genpd = dev_to_genpd(dev);
928 if (IS_ERR(genpd))
929 return -EINVAL;
930
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200931 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100932}
933
934/**
935 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200936 * @dev: Device to freeze.
937 *
938 * Carry out a late freeze of a device under the assumption that its
939 * pm_domain field points to the domain member of an object of type
940 * struct generic_pm_domain representing a power domain consisting of I/O
941 * devices.
942 */
943static int pm_genpd_freeze_noirq(struct device *dev)
944{
945 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200946
947 dev_dbg(dev, "%s()\n", __func__);
948
949 genpd = dev_to_genpd(dev);
950 if (IS_ERR(genpd))
951 return -EINVAL;
952
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200953 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200954}
955
956/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100957 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200958 * @dev: Device to thaw.
959 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100960 * Start the device, unless power has been removed from the domain already
961 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200962 */
963static int pm_genpd_thaw_noirq(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 Hansson51cda842015-10-15 17:02:06 +0200973 return genpd->suspend_power_off ?
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200974 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100975}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200976
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100977/**
978 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
979 * @dev: Device to thaw.
980 *
981 * Carry out an early thaw of a device under the assumption that its
982 * pm_domain field points to the domain member of an object of type
983 * struct generic_pm_domain representing a power domain consisting of I/O
984 * devices.
985 */
986static int pm_genpd_thaw_early(struct device *dev)
987{
988 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200989
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100990 dev_dbg(dev, "%s()\n", __func__);
991
992 genpd = dev_to_genpd(dev);
993 if (IS_ERR(genpd))
994 return -EINVAL;
995
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200996 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200997}
998
999/**
1000 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1001 * @dev: Device to thaw.
1002 *
1003 * Thaw a device under the assumption that its pm_domain field points to the
1004 * domain member of an object of type struct generic_pm_domain representing
1005 * a power domain consisting of I/O devices.
1006 */
1007static int pm_genpd_thaw(struct device *dev)
1008{
1009 struct generic_pm_domain *genpd;
1010
1011 dev_dbg(dev, "%s()\n", __func__);
1012
1013 genpd = dev_to_genpd(dev);
1014 if (IS_ERR(genpd))
1015 return -EINVAL;
1016
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001017 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001018}
1019
1020/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001021 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001022 * @dev: Device to resume.
1023 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001024 * Make sure the domain will be in the same power state as before the
1025 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001026 */
1027static int pm_genpd_restore_noirq(struct device *dev)
1028{
1029 struct generic_pm_domain *genpd;
1030
1031 dev_dbg(dev, "%s()\n", __func__);
1032
1033 genpd = dev_to_genpd(dev);
1034 if (IS_ERR(genpd))
1035 return -EINVAL;
1036
1037 /*
1038 * Since all of the "noirq" callbacks are executed sequentially, it is
1039 * guaranteed that this function will never run twice in parallel for
1040 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001041 *
1042 * At this point suspended_count == 0 means we are being run for the
1043 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001044 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001045 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001046 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001047 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001048 * so make it appear as powered off to pm_genpd_sync_poweron(),
1049 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001050 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001051 genpd->status = GPD_STATE_POWER_OFF;
1052 if (genpd->suspend_power_off) {
1053 /*
1054 * If the domain was off before the hibernation, make
1055 * sure it will be off going forward.
1056 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001057 genpd_power_off(genpd, true);
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001058
1059 return 0;
1060 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001061 }
1062
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001063 if (genpd->suspend_power_off)
1064 return 0;
1065
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001066 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001067
Ulf Hansson2b1d88c2015-10-15 17:02:19 +02001068 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001069}
1070
1071/**
1072 * pm_genpd_complete - Complete power transition of a device in a power domain.
1073 * @dev: Device to complete the transition of.
1074 *
1075 * Complete a power transition of a device (during a system-wide power
1076 * transition) under the assumption that its pm_domain field points to the
1077 * domain member of an object of type struct generic_pm_domain representing
1078 * a power domain consisting of I/O devices.
1079 */
1080static void pm_genpd_complete(struct device *dev)
1081{
1082 struct generic_pm_domain *genpd;
1083 bool run_complete;
1084
1085 dev_dbg(dev, "%s()\n", __func__);
1086
1087 genpd = dev_to_genpd(dev);
1088 if (IS_ERR(genpd))
1089 return;
1090
1091 mutex_lock(&genpd->lock);
1092
1093 run_complete = !genpd->suspend_power_off;
1094 if (--genpd->prepared_count == 0)
1095 genpd->suspend_power_off = false;
1096
1097 mutex_unlock(&genpd->lock);
1098
1099 if (run_complete) {
1100 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001101 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001102 pm_runtime_enable(dev);
Ulf Hanssonaf939332013-04-12 09:41:06 +00001103 pm_request_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001104 }
1105}
1106
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001107/**
Ulf Hanssond47e6462014-09-03 12:52:24 +02001108 * genpd_syscore_switch - Switch power during system core suspend or resume.
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001109 * @dev: Device that normally is marked as "always on" to switch power for.
1110 *
1111 * This routine may only be called during the system core (syscore) suspend or
1112 * resume phase for devices whose "always on" flags are set.
1113 */
Ulf Hanssond47e6462014-09-03 12:52:24 +02001114static void genpd_syscore_switch(struct device *dev, bool suspend)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001115{
1116 struct generic_pm_domain *genpd;
1117
1118 genpd = dev_to_genpd(dev);
1119 if (!pm_genpd_present(genpd))
1120 return;
1121
1122 if (suspend) {
1123 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001124 pm_genpd_sync_poweroff(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001125 } else {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001126 pm_genpd_sync_poweron(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001127 genpd->suspended_count--;
1128 }
1129}
Ulf Hanssond47e6462014-09-03 12:52:24 +02001130
1131void pm_genpd_syscore_poweroff(struct device *dev)
1132{
1133 genpd_syscore_switch(dev, true);
1134}
1135EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1136
1137void pm_genpd_syscore_poweron(struct device *dev)
1138{
1139 genpd_syscore_switch(dev, false);
1140}
1141EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001142
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001143#else /* !CONFIG_PM_SLEEP */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001144
1145#define pm_genpd_prepare NULL
1146#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001147#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001148#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001149#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001150#define pm_genpd_resume_noirq NULL
1151#define pm_genpd_resume NULL
1152#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001153#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001154#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001155#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001156#define pm_genpd_thaw_noirq NULL
1157#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001158#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001159#define pm_genpd_complete NULL
1160
1161#endif /* CONFIG_PM_SLEEP */
1162
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001163static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1164 struct generic_pm_domain *genpd,
1165 struct gpd_timing_data *td)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001166{
1167 struct generic_pm_domain_data *gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001168 int ret;
1169
1170 ret = dev_pm_get_subsys_data(dev);
1171 if (ret)
1172 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001173
1174 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
Ulf Hansson3e235682015-01-27 21:13:43 +01001175 if (!gpd_data) {
1176 ret = -ENOMEM;
1177 goto err_put;
1178 }
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001179
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001180 if (td)
1181 gpd_data->td = *td;
1182
1183 gpd_data->base.dev = dev;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001184 gpd_data->td.constraint_changed = true;
1185 gpd_data->td.effective_constraint_ns = -1;
1186 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1187
1188 spin_lock_irq(&dev->power.lock);
1189
1190 if (dev->power.subsys_data->domain_data) {
1191 ret = -EINVAL;
1192 goto err_free;
1193 }
1194
1195 dev->power.subsys_data->domain_data = &gpd_data->base;
1196 dev->pm_domain = &genpd->domain;
1197
1198 spin_unlock_irq(&dev->power.lock);
1199
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001200 return gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001201
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001202 err_free:
1203 spin_unlock_irq(&dev->power.lock);
1204 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001205 err_put:
1206 dev_pm_put_subsys_data(dev);
1207 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001208}
1209
Ulf Hansson49d400c2015-01-27 21:13:38 +01001210static void genpd_free_dev_data(struct device *dev,
1211 struct generic_pm_domain_data *gpd_data)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001212{
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001213 spin_lock_irq(&dev->power.lock);
1214
1215 dev->pm_domain = NULL;
1216 dev->power.subsys_data->domain_data = NULL;
1217
1218 spin_unlock_irq(&dev->power.lock);
1219
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001220 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001221 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001222}
1223
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001224/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001225 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001226 * @genpd: PM domain to add the device to.
1227 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001228 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001229 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001230int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1231 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001232{
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001233 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001234 int ret = 0;
1235
1236 dev_dbg(dev, "%s()\n", __func__);
1237
1238 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1239 return -EINVAL;
1240
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001241 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
Ulf Hansson3e235682015-01-27 21:13:43 +01001242 if (IS_ERR(gpd_data))
1243 return PTR_ERR(gpd_data);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001244
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001245 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001246
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001247 if (genpd->prepared_count > 0) {
1248 ret = -EAGAIN;
1249 goto out;
1250 }
1251
Ulf Hanssonb472c2f2015-01-27 21:13:45 +01001252 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1253 if (ret)
1254 goto out;
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001255
Ulf Hansson14b53062015-01-27 21:13:40 +01001256 genpd->device_count++;
1257 genpd->max_off_time_changed = true;
1258
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001259 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001260
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001261 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001262 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001263
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001264 if (ret)
1265 genpd_free_dev_data(dev, gpd_data);
1266 else
1267 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001268
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001269 return ret;
1270}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301271EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001272
1273/**
1274 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1275 * @genpd: PM domain to remove the device from.
1276 * @dev: Device to be removed.
1277 */
1278int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1279 struct device *dev)
1280{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001281 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001282 struct pm_domain_data *pdd;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001283 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001284
1285 dev_dbg(dev, "%s()\n", __func__);
1286
Russell Kingdf6a0d62015-03-20 17:20:38 +00001287 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001288 return -EINVAL;
1289
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001290 /* The above validation also means we have existing domain_data. */
1291 pdd = dev->power.subsys_data->domain_data;
1292 gpd_data = to_gpd_data(pdd);
1293 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1294
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001295 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001296
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001297 if (genpd->prepared_count > 0) {
1298 ret = -EAGAIN;
1299 goto out;
1300 }
1301
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001302 genpd->device_count--;
1303 genpd->max_off_time_changed = true;
1304
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001305 if (genpd->detach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001306 genpd->detach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001307
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001308 list_del_init(&pdd->list_node);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001309
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001310 mutex_unlock(&genpd->lock);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001311
Ulf Hanssonc1dbe2f2015-01-27 21:13:39 +01001312 genpd_free_dev_data(dev, gpd_data);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001313
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001314 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001315
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001316 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001317 mutex_unlock(&genpd->lock);
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001318 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001319
1320 return ret;
1321}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301322EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001323
1324/**
1325 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1326 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001327 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001328 */
1329int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001330 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001331{
Lina Iyer25479232015-10-28 15:19:50 -06001332 struct gpd_link *link, *itr;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001333 int ret = 0;
1334
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001335 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1336 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001337 return -EINVAL;
1338
Lina Iyer25479232015-10-28 15:19:50 -06001339 link = kzalloc(sizeof(*link), GFP_KERNEL);
1340 if (!link)
1341 return -ENOMEM;
1342
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001343 mutex_lock(&subdomain->lock);
1344 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001345
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001346 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001347 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001348 ret = -EINVAL;
1349 goto out;
1350 }
1351
Lina Iyer25479232015-10-28 15:19:50 -06001352 list_for_each_entry(itr, &genpd->master_links, master_node) {
1353 if (itr->slave == subdomain && itr->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001354 ret = -EINVAL;
1355 goto out;
1356 }
1357 }
1358
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001359 link->master = genpd;
1360 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001361 link->slave = subdomain;
1362 list_add_tail(&link->slave_node, &subdomain->slave_links);
1363 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001364 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001365
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001366 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001367 mutex_unlock(&genpd->lock);
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001368 mutex_unlock(&subdomain->lock);
Lina Iyer25479232015-10-28 15:19:50 -06001369 if (ret)
1370 kfree(link);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001371 return ret;
1372}
Stephen Boydd60ee962015-10-01 12:22:53 -07001373EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001374
1375/**
1376 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1377 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001378 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001379 */
1380int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001381 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001382{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001383 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001384 int ret = -EINVAL;
1385
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001386 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001387 return -EINVAL;
1388
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001389 mutex_lock(&subdomain->lock);
1390 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001391
Jon Hunter30e7a652015-09-03 09:10:37 +01001392 if (!list_empty(&subdomain->slave_links) || subdomain->device_count) {
1393 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1394 subdomain->name);
1395 ret = -EBUSY;
1396 goto out;
1397 }
1398
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001399 list_for_each_entry(link, &genpd->master_links, master_node) {
1400 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001401 continue;
1402
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001403 list_del(&link->master_node);
1404 list_del(&link->slave_node);
1405 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001406 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001407 genpd_sd_counter_dec(genpd);
1408
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001409 ret = 0;
1410 break;
1411 }
1412
Jon Hunter30e7a652015-09-03 09:10:37 +01001413out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001414 mutex_unlock(&genpd->lock);
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001415 mutex_unlock(&subdomain->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001416
1417 return ret;
1418}
Stephen Boydd60ee962015-10-01 12:22:53 -07001419EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001420
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001421/* Default device callbacks for generic PM domains. */
1422
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001423/**
Geert Uytterhoeven12e10bb2014-09-16 21:59:39 +02001424 * pm_genpd_default_save_state - Default "save device state" for PM domains.
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001425 * @dev: Device to handle.
1426 */
1427static int pm_genpd_default_save_state(struct device *dev)
1428{
1429 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001430
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001431 if (dev->type && dev->type->pm)
1432 cb = dev->type->pm->runtime_suspend;
1433 else if (dev->class && dev->class->pm)
1434 cb = dev->class->pm->runtime_suspend;
1435 else if (dev->bus && dev->bus->pm)
1436 cb = dev->bus->pm->runtime_suspend;
1437 else
1438 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001439
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001440 if (!cb && dev->driver && dev->driver->pm)
1441 cb = dev->driver->pm->runtime_suspend;
1442
1443 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001444}
1445
1446/**
Geert Uytterhoeven12e10bb2014-09-16 21:59:39 +02001447 * pm_genpd_default_restore_state - Default PM domains "restore device state".
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001448 * @dev: Device to handle.
1449 */
1450static int pm_genpd_default_restore_state(struct device *dev)
1451{
1452 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001453
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001454 if (dev->type && dev->type->pm)
1455 cb = dev->type->pm->runtime_resume;
1456 else if (dev->class && dev->class->pm)
1457 cb = dev->class->pm->runtime_resume;
1458 else if (dev->bus && dev->bus->pm)
1459 cb = dev->bus->pm->runtime_resume;
1460 else
1461 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001462
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001463 if (!cb && dev->driver && dev->driver->pm)
1464 cb = dev->driver->pm->runtime_resume;
1465
1466 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001467}
1468
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001469/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001470 * pm_genpd_init - Initialize a generic I/O PM domain object.
1471 * @genpd: PM domain object to initialize.
1472 * @gov: PM domain governor to associate with the domain (may be NULL).
1473 * @is_off: Initial value of the domain's power_is_off field.
1474 */
1475void pm_genpd_init(struct generic_pm_domain *genpd,
1476 struct dev_power_governor *gov, bool is_off)
1477{
1478 if (IS_ERR_OR_NULL(genpd))
1479 return;
1480
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001481 INIT_LIST_HEAD(&genpd->master_links);
1482 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001483 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001484 mutex_init(&genpd->lock);
1485 genpd->gov = gov;
1486 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001487 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001488 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001489 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001490 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001491 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001492 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1493 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001494 genpd->domain.ops.prepare = pm_genpd_prepare;
1495 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001496 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001497 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1498 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001499 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001500 genpd->domain.ops.resume = pm_genpd_resume;
1501 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001502 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001503 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1504 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001505 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001506 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001507 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001508 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001509 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001510 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001511 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001512 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001513 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001514 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1515 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Ulf Hanssonc11f6f52014-12-01 12:50:21 +01001516
1517 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1518 genpd->dev_ops.stop = pm_clk_suspend;
1519 genpd->dev_ops.start = pm_clk_resume;
1520 }
1521
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +02001522 mutex_lock(&gpd_list_lock);
1523 list_add(&genpd->gpd_list_node, &gpd_list);
1524 mutex_unlock(&gpd_list_lock);
1525}
Rajendra Nayakbe5ed552015-08-13 11:51:57 +05301526EXPORT_SYMBOL_GPL(pm_genpd_init);
Tomasz Figaaa422402014-09-19 20:27:36 +02001527
1528#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1529/*
1530 * Device Tree based PM domain providers.
1531 *
1532 * The code below implements generic device tree based PM domain providers that
1533 * bind device tree nodes with generic PM domains registered in the system.
1534 *
1535 * Any driver that registers generic PM domains and needs to support binding of
1536 * devices to these domains is supposed to register a PM domain provider, which
1537 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1538 *
1539 * Two simple mapping functions have been provided for convenience:
1540 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1541 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1542 * index.
1543 */
1544
1545/**
1546 * struct of_genpd_provider - PM domain provider registration structure
1547 * @link: Entry in global list of PM domain providers
1548 * @node: Pointer to device tree node of PM domain provider
1549 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1550 * into a PM domain.
1551 * @data: context pointer to be passed into @xlate callback
1552 */
1553struct of_genpd_provider {
1554 struct list_head link;
1555 struct device_node *node;
1556 genpd_xlate_t xlate;
1557 void *data;
1558};
1559
1560/* List of registered PM domain providers. */
1561static LIST_HEAD(of_genpd_providers);
1562/* Mutex to protect the list above. */
1563static DEFINE_MUTEX(of_genpd_mutex);
1564
1565/**
1566 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1567 * @genpdspec: OF phandle args to map into a PM domain
1568 * @data: xlate function private data - pointer to struct generic_pm_domain
1569 *
1570 * This is a generic xlate function that can be used to model PM domains that
1571 * have their own device tree nodes. The private data of xlate function needs
1572 * to be a valid pointer to struct generic_pm_domain.
1573 */
1574struct generic_pm_domain *__of_genpd_xlate_simple(
1575 struct of_phandle_args *genpdspec,
1576 void *data)
1577{
1578 if (genpdspec->args_count != 0)
1579 return ERR_PTR(-EINVAL);
1580 return data;
1581}
1582EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1583
1584/**
1585 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1586 * @genpdspec: OF phandle args to map into a PM domain
1587 * @data: xlate function private data - pointer to struct genpd_onecell_data
1588 *
1589 * This is a generic xlate function that can be used to model simple PM domain
1590 * controllers that have one device tree node and provide multiple PM domains.
1591 * A single cell is used as an index into an array of PM domains specified in
1592 * the genpd_onecell_data struct when registering the provider.
1593 */
1594struct generic_pm_domain *__of_genpd_xlate_onecell(
1595 struct of_phandle_args *genpdspec,
1596 void *data)
1597{
1598 struct genpd_onecell_data *genpd_data = data;
1599 unsigned int idx = genpdspec->args[0];
1600
1601 if (genpdspec->args_count != 1)
1602 return ERR_PTR(-EINVAL);
1603
1604 if (idx >= genpd_data->num_domains) {
1605 pr_err("%s: invalid domain index %u\n", __func__, idx);
1606 return ERR_PTR(-EINVAL);
1607 }
1608
1609 if (!genpd_data->domains[idx])
1610 return ERR_PTR(-ENOENT);
1611
1612 return genpd_data->domains[idx];
1613}
1614EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1615
1616/**
1617 * __of_genpd_add_provider() - Register a PM domain provider for a node
1618 * @np: Device node pointer associated with the PM domain provider.
1619 * @xlate: Callback for decoding PM domain from phandle arguments.
1620 * @data: Context pointer for @xlate callback.
1621 */
1622int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1623 void *data)
1624{
1625 struct of_genpd_provider *cp;
1626
1627 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1628 if (!cp)
1629 return -ENOMEM;
1630
1631 cp->node = of_node_get(np);
1632 cp->data = data;
1633 cp->xlate = xlate;
1634
1635 mutex_lock(&of_genpd_mutex);
1636 list_add(&cp->link, &of_genpd_providers);
1637 mutex_unlock(&of_genpd_mutex);
1638 pr_debug("Added domain provider from %s\n", np->full_name);
1639
1640 return 0;
1641}
1642EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1643
1644/**
1645 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1646 * @np: Device node pointer associated with the PM domain provider
1647 */
1648void of_genpd_del_provider(struct device_node *np)
1649{
1650 struct of_genpd_provider *cp;
1651
1652 mutex_lock(&of_genpd_mutex);
1653 list_for_each_entry(cp, &of_genpd_providers, link) {
1654 if (cp->node == np) {
1655 list_del(&cp->link);
1656 of_node_put(cp->node);
1657 kfree(cp);
1658 break;
1659 }
1660 }
1661 mutex_unlock(&of_genpd_mutex);
1662}
1663EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1664
1665/**
1666 * of_genpd_get_from_provider() - Look-up PM domain
1667 * @genpdspec: OF phandle args to use for look-up
1668 *
1669 * Looks for a PM domain provider under the node specified by @genpdspec and if
1670 * found, uses xlate function of the provider to map phandle args to a PM
1671 * domain.
1672 *
1673 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1674 * on failure.
1675 */
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05301676struct generic_pm_domain *of_genpd_get_from_provider(
Tomasz Figaaa422402014-09-19 20:27:36 +02001677 struct of_phandle_args *genpdspec)
1678{
1679 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1680 struct of_genpd_provider *provider;
1681
1682 mutex_lock(&of_genpd_mutex);
1683
1684 /* Check if we have such a provider in our array */
1685 list_for_each_entry(provider, &of_genpd_providers, link) {
1686 if (provider->node == genpdspec->np)
1687 genpd = provider->xlate(genpdspec, provider->data);
1688 if (!IS_ERR(genpd))
1689 break;
1690 }
1691
1692 mutex_unlock(&of_genpd_mutex);
1693
1694 return genpd;
1695}
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05301696EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
Tomasz Figaaa422402014-09-19 20:27:36 +02001697
1698/**
1699 * genpd_dev_pm_detach - Detach a device from its PM domain.
Jon Hunter8bb69442015-08-27 10:17:00 +01001700 * @dev: Device to detach.
Tomasz Figaaa422402014-09-19 20:27:36 +02001701 * @power_off: Currently not used
1702 *
1703 * Try to locate a corresponding generic PM domain, which the device was
1704 * attached to previously. If such is found, the device is detached from it.
1705 */
1706static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1707{
Russell King446d999c2015-03-20 17:20:33 +00001708 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001709 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02001710 int ret = 0;
1711
Russell King446d999c2015-03-20 17:20:33 +00001712 pd = pm_genpd_lookup_dev(dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02001713 if (!pd)
1714 return;
1715
1716 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1717
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001718 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02001719 ret = pm_genpd_remove_device(pd, dev);
1720 if (ret != -EAGAIN)
1721 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001722
1723 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02001724 cond_resched();
1725 }
1726
1727 if (ret < 0) {
1728 dev_err(dev, "failed to remove from PM domain %s: %d",
1729 pd->name, ret);
1730 return;
1731 }
1732
1733 /* Check if PM domain can be powered off after removing this device. */
1734 genpd_queue_power_off_work(pd);
1735}
1736
Russell King632f7ce2015-03-20 15:55:12 +01001737static void genpd_dev_pm_sync(struct device *dev)
1738{
1739 struct generic_pm_domain *pd;
1740
1741 pd = dev_to_genpd(dev);
1742 if (IS_ERR(pd))
1743 return;
1744
1745 genpd_queue_power_off_work(pd);
1746}
1747
Tomasz Figaaa422402014-09-19 20:27:36 +02001748/**
1749 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1750 * @dev: Device to attach.
1751 *
1752 * Parse device's OF node to find a PM domain specifier. If such is found,
1753 * attaches the device to retrieved pm_domain ops.
1754 *
1755 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1756 * backwards compatibility with existing DTBs.
1757 *
Jon Hunter311fa6a2015-07-31 10:20:00 +01001758 * Returns 0 on successfully attached PM domain or negative error code. Note
1759 * that if a power-domain exists for the device, but it cannot be found or
1760 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1761 * probed and to re-try again later.
Tomasz Figaaa422402014-09-19 20:27:36 +02001762 */
1763int genpd_dev_pm_attach(struct device *dev)
1764{
1765 struct of_phandle_args pd_args;
1766 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001767 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02001768 int ret;
1769
1770 if (!dev->of_node)
1771 return -ENODEV;
1772
1773 if (dev->pm_domain)
1774 return -EEXIST;
1775
1776 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1777 "#power-domain-cells", 0, &pd_args);
1778 if (ret < 0) {
1779 if (ret != -ENOENT)
1780 return ret;
1781
1782 /*
1783 * Try legacy Samsung-specific bindings
1784 * (for backwards compatibility of DT ABI)
1785 */
1786 pd_args.args_count = 0;
1787 pd_args.np = of_parse_phandle(dev->of_node,
1788 "samsung,power-domain", 0);
1789 if (!pd_args.np)
1790 return -ENOENT;
1791 }
1792
1793 pd = of_genpd_get_from_provider(&pd_args);
Eric Anholt265e2cf2015-12-01 09:39:31 -08001794 of_node_put(pd_args.np);
Tomasz Figaaa422402014-09-19 20:27:36 +02001795 if (IS_ERR(pd)) {
1796 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1797 __func__, PTR_ERR(pd));
Jon Hunter311fa6a2015-07-31 10:20:00 +01001798 return -EPROBE_DEFER;
Tomasz Figaaa422402014-09-19 20:27:36 +02001799 }
1800
1801 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1802
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001803 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02001804 ret = pm_genpd_add_device(pd, dev);
1805 if (ret != -EAGAIN)
1806 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001807
1808 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02001809 cond_resched();
1810 }
1811
1812 if (ret < 0) {
1813 dev_err(dev, "failed to add to PM domain %s: %d",
1814 pd->name, ret);
Jon Hunter311fa6a2015-07-31 10:20:00 +01001815 goto out;
Tomasz Figaaa422402014-09-19 20:27:36 +02001816 }
1817
1818 dev->pm_domain->detach = genpd_dev_pm_detach;
Russell King632f7ce2015-03-20 15:55:12 +01001819 dev->pm_domain->sync = genpd_dev_pm_sync;
Ulf Hansson7420aa4f2015-10-13 09:58:02 +02001820 ret = genpd_poweron(pd);
Tomasz Figaaa422402014-09-19 20:27:36 +02001821
Jon Hunter311fa6a2015-07-31 10:20:00 +01001822out:
1823 return ret ? -EPROBE_DEFER : 0;
Tomasz Figaaa422402014-09-19 20:27:36 +02001824}
1825EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001826#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001827
1828
1829/*** debugfs support ***/
1830
1831#ifdef CONFIG_PM_ADVANCED_DEBUG
1832#include <linux/pm.h>
1833#include <linux/device.h>
1834#include <linux/debugfs.h>
1835#include <linux/seq_file.h>
1836#include <linux/init.h>
1837#include <linux/kobject.h>
1838static struct dentry *pm_genpd_debugfs_dir;
1839
1840/*
1841 * TODO: This function is a slightly modified version of rtpm_status_show
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001842 * from sysfs.c, so generalize it.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001843 */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001844static void rtpm_status_str(struct seq_file *s, struct device *dev)
1845{
1846 static const char * const status_lookup[] = {
1847 [RPM_ACTIVE] = "active",
1848 [RPM_RESUMING] = "resuming",
1849 [RPM_SUSPENDED] = "suspended",
1850 [RPM_SUSPENDING] = "suspending"
1851 };
1852 const char *p = "";
1853
1854 if (dev->power.runtime_error)
1855 p = "error";
1856 else if (dev->power.disable_depth)
1857 p = "unsupported";
1858 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1859 p = status_lookup[dev->power.runtime_status];
1860 else
1861 WARN_ON(1);
1862
1863 seq_puts(s, p);
1864}
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001865
1866static int pm_genpd_summary_one(struct seq_file *s,
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001867 struct generic_pm_domain *genpd)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001868{
1869 static const char * const status_lookup[] = {
1870 [GPD_STATE_ACTIVE] = "on",
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001871 [GPD_STATE_POWER_OFF] = "off"
1872 };
1873 struct pm_domain_data *pm_data;
1874 const char *kobj_path;
1875 struct gpd_link *link;
1876 int ret;
1877
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001878 ret = mutex_lock_interruptible(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001879 if (ret)
1880 return -ERESTARTSYS;
1881
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001882 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001883 goto exit;
Geert Uytterhoeven15dec672015-08-11 14:50:49 +02001884 seq_printf(s, "%-30s %-15s ", genpd->name, status_lookup[genpd->status]);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001885
1886 /*
1887 * Modifications on the list require holding locks on both
1888 * master and slave, so we are safe.
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001889 * Also genpd->name is immutable.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001890 */
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001891 list_for_each_entry(link, &genpd->master_links, master_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001892 seq_printf(s, "%s", link->slave->name);
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001893 if (!list_is_last(&link->master_node, &genpd->master_links))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001894 seq_puts(s, ", ");
1895 }
1896
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001897 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001898 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1899 if (kobj_path == NULL)
1900 continue;
1901
1902 seq_printf(s, "\n %-50s ", kobj_path);
1903 rtpm_status_str(s, pm_data->dev);
1904 kfree(kobj_path);
1905 }
1906
1907 seq_puts(s, "\n");
1908exit:
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001909 mutex_unlock(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001910
1911 return 0;
1912}
1913
1914static int pm_genpd_summary_show(struct seq_file *s, void *data)
1915{
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001916 struct generic_pm_domain *genpd;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001917 int ret = 0;
1918
Geert Uytterhoeven15dec672015-08-11 14:50:49 +02001919 seq_puts(s, "domain status slaves\n");
1920 seq_puts(s, " /device runtime status\n");
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001921 seq_puts(s, "----------------------------------------------------------------------\n");
1922
1923 ret = mutex_lock_interruptible(&gpd_list_lock);
1924 if (ret)
1925 return -ERESTARTSYS;
1926
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001927 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1928 ret = pm_genpd_summary_one(s, genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001929 if (ret)
1930 break;
1931 }
1932 mutex_unlock(&gpd_list_lock);
1933
1934 return ret;
1935}
1936
1937static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1938{
1939 return single_open(file, pm_genpd_summary_show, NULL);
1940}
1941
1942static const struct file_operations pm_genpd_summary_fops = {
1943 .open = pm_genpd_summary_open,
1944 .read = seq_read,
1945 .llseek = seq_lseek,
1946 .release = single_release,
1947};
1948
1949static int __init pm_genpd_debug_init(void)
1950{
1951 struct dentry *d;
1952
1953 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1954
1955 if (!pm_genpd_debugfs_dir)
1956 return -ENOMEM;
1957
1958 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1959 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1960 if (!d)
1961 return -ENOMEM;
1962
1963 return 0;
1964}
1965late_initcall(pm_genpd_debug_init);
1966
1967static void __exit pm_genpd_debug_exit(void)
1968{
1969 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1970}
1971__exitcall(pm_genpd_debug_exit);
1972#endif /* CONFIG_PM_ADVANCED_DEBUG */