blob: 0d8780c04a5e4d7c409b2ad1aa2d7e4c0c4f424b [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
Rafael J. Wysockif7218892011-07-01 22:12:45 +02009#include <linux/kernel.h>
10#include <linux/io.h>
Tomasz Figaaa422402014-09-19 20:27:36 +020011#include <linux/platform_device.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020012#include <linux/pm_runtime.h>
13#include <linux/pm_domain.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020014#include <linux/pm_qos.h>
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010015#include <linux/pm_clock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020016#include <linux/slab.h>
17#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020018#include <linux/sched.h>
19#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010020#include <linux/export.h>
21
22#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
23({ \
24 type (*__routine)(struct device *__d); \
25 type __ret = (type)0; \
26 \
27 __routine = genpd->dev_ops.callback; \
28 if (__routine) { \
29 __ret = __routine(dev); \
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010030 } \
31 __ret; \
32})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020033
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010034#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
35({ \
36 ktime_t __start = ktime_get(); \
37 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
38 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020039 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
40 if (!__retval && __elapsed > __td->field) { \
41 __td->field = __elapsed; \
Philipp Zabel7d1af282014-02-27 19:26:44 +010042 dev_dbg(dev, name " latency exceeded, new value %lld ns\n", \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010043 __elapsed); \
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020044 genpd->max_off_time_changed = true; \
45 __td->constraint_changed = true; \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010046 } \
47 __retval; \
48})
49
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020050static LIST_HEAD(gpd_list);
51static DEFINE_MUTEX(gpd_list_lock);
52
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +020053static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
54{
55 struct generic_pm_domain *genpd = NULL, *gpd;
56
57 if (IS_ERR_OR_NULL(domain_name))
58 return NULL;
59
60 mutex_lock(&gpd_list_lock);
61 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
62 if (!strcmp(gpd->name, domain_name)) {
63 genpd = gpd;
64 break;
65 }
66 }
67 mutex_unlock(&gpd_list_lock);
68 return genpd;
69}
70
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010071struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020072{
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
75
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020076 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020077}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020078
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010079static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
80{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010081 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
82 stop_latency_ns, "stop");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010083}
84
85static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
86{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010087 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
88 start_latency_ns, "start");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010089}
90
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020091static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020092{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020093 bool ret = false;
94
95 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
96 ret = !!atomic_dec_and_test(&genpd->sd_count);
97
98 return ret;
99}
100
101static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
102{
103 atomic_inc(&genpd->sd_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100104 smp_mb__after_atomic();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200105}
106
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200107static void genpd_acquire_lock(struct generic_pm_domain *genpd)
108{
109 DEFINE_WAIT(wait);
110
111 mutex_lock(&genpd->lock);
112 /*
113 * Wait for the domain to transition into either the active,
114 * or the power off state.
115 */
116 for (;;) {
117 prepare_to_wait(&genpd->status_wait_queue, &wait,
118 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200119 if (genpd->status == GPD_STATE_ACTIVE
120 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200121 break;
122 mutex_unlock(&genpd->lock);
123
124 schedule();
125
126 mutex_lock(&genpd->lock);
127 }
128 finish_wait(&genpd->status_wait_queue, &wait);
129}
130
131static void genpd_release_lock(struct generic_pm_domain *genpd)
132{
133 mutex_unlock(&genpd->lock);
134}
135
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200136static void genpd_set_active(struct generic_pm_domain *genpd)
137{
138 if (genpd->resume_count == 0)
139 genpd->status = GPD_STATE_ACTIVE;
140}
141
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200142static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
143{
144 s64 usecs64;
145
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200146 if (!genpd->cpuidle_data)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200147 return;
148
149 usecs64 = genpd->power_on_latency_ns;
150 do_div(usecs64, NSEC_PER_USEC);
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200151 usecs64 += genpd->cpuidle_data->saved_exit_latency;
152 genpd->cpuidle_data->idle_state->exit_latency = usecs64;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200153}
154
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100155static int genpd_power_on(struct generic_pm_domain *genpd)
156{
157 ktime_t time_start;
158 s64 elapsed_ns;
159 int ret;
160
161 if (!genpd->power_on)
162 return 0;
163
164 time_start = ktime_get();
165 ret = genpd->power_on(genpd);
166 if (ret)
167 return ret;
168
169 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
170 if (elapsed_ns <= genpd->power_on_latency_ns)
171 return ret;
172
173 genpd->power_on_latency_ns = elapsed_ns;
174 genpd->max_off_time_changed = true;
175 genpd_recalc_cpu_exit_latency(genpd);
176 pr_warn("%s: Power-%s latency exceeded, new value %lld ns\n",
177 genpd->name, "on", elapsed_ns);
178
179 return ret;
180}
181
182static int genpd_power_off(struct generic_pm_domain *genpd)
183{
184 ktime_t time_start;
185 s64 elapsed_ns;
186 int ret;
187
188 if (!genpd->power_off)
189 return 0;
190
191 time_start = ktime_get();
192 ret = genpd->power_off(genpd);
193 if (ret == -EBUSY)
194 return ret;
195
196 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
197 if (elapsed_ns <= genpd->power_off_latency_ns)
198 return ret;
199
200 genpd->power_off_latency_ns = elapsed_ns;
201 genpd->max_off_time_changed = true;
202 pr_warn("%s: Power-%s latency exceeded, new value %lld ns\n",
203 genpd->name, "off", elapsed_ns);
204
205 return ret;
206}
207
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200208/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200209 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200210 * @genpd: PM domain to power up.
211 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200212 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200213 * resume a device belonging to it.
214 */
Sachin Kamat8951ef02012-07-10 21:47:07 +0200215static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200216 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200217{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200218 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200219 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200220 int ret = 0;
221
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200222 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200223 for (;;) {
224 prepare_to_wait(&genpd->status_wait_queue, &wait,
225 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200226 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200227 break;
228 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200229
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200230 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200231
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200232 mutex_lock(&genpd->lock);
233 }
234 finish_wait(&genpd->status_wait_queue, &wait);
235
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200236 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200237 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200238 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200239
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200240 if (genpd->status != GPD_STATE_POWER_OFF) {
241 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200242 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200243 }
244
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200245 if (genpd->cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200246 cpuidle_pause_and_lock();
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200247 genpd->cpuidle_data->idle_state->disabled = true;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200248 cpuidle_resume_and_unlock();
249 goto out;
250 }
251
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200252 /*
253 * The list is guaranteed not to change while the loop below is being
254 * executed, unless one of the masters' .power_on() callbacks fiddles
255 * with it.
256 */
257 list_for_each_entry(link, &genpd->slave_links, slave_node) {
258 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200259 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200260
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200261 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200262
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200263 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200264
265 mutex_lock(&genpd->lock);
266
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200267 /*
268 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200269 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200270 */
271 genpd->status = GPD_STATE_POWER_OFF;
272 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200273 if (ret) {
274 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200275 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200276 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200277 }
278
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100279 ret = genpd_power_on(genpd);
280 if (ret)
281 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200282
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200283 out:
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200284 genpd_set_active(genpd);
285
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200286 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200287
288 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200289 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
290 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200291
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200292 return ret;
293}
294
295/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200296 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200297 * @genpd: PM domain to power up.
298 */
299int pm_genpd_poweron(struct generic_pm_domain *genpd)
300{
301 int ret;
302
303 mutex_lock(&genpd->lock);
304 ret = __pm_genpd_poweron(genpd);
305 mutex_unlock(&genpd->lock);
306 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200307}
308
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +0200309/**
310 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
311 * @domain_name: Name of the PM domain to power up.
312 */
313int pm_genpd_name_poweron(const char *domain_name)
314{
315 struct generic_pm_domain *genpd;
316
317 genpd = pm_genpd_lookup_name(domain_name);
318 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
319}
320
Sachin Kamatb3d3b9f2012-09-06 08:18:57 +0000321static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
322 struct device *dev)
323{
324 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
325}
326
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200327static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
328{
329 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
330 save_state_latency_ns, "state save");
331}
332
333static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
334{
335 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
336 restore_state_latency_ns,
337 "state restore");
338}
339
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200340static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
341 unsigned long val, void *ptr)
342{
343 struct generic_pm_domain_data *gpd_data;
344 struct device *dev;
345
346 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
347
348 mutex_lock(&gpd_data->lock);
349 dev = gpd_data->base.dev;
350 if (!dev) {
351 mutex_unlock(&gpd_data->lock);
352 return NOTIFY_DONE;
353 }
354 mutex_unlock(&gpd_data->lock);
355
356 for (;;) {
357 struct generic_pm_domain *genpd;
358 struct pm_domain_data *pdd;
359
360 spin_lock_irq(&dev->power.lock);
361
362 pdd = dev->power.subsys_data ?
363 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200364 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200365 to_gpd_data(pdd)->td.constraint_changed = true;
366 genpd = dev_to_genpd(dev);
367 } else {
368 genpd = ERR_PTR(-ENODATA);
369 }
370
371 spin_unlock_irq(&dev->power.lock);
372
373 if (!IS_ERR(genpd)) {
374 mutex_lock(&genpd->lock);
375 genpd->max_off_time_changed = true;
376 mutex_unlock(&genpd->lock);
377 }
378
379 dev = dev->parent;
380 if (!dev || dev->power.ignore_children)
381 break;
382 }
383
384 return NOTIFY_DONE;
385}
386
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200387/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200388 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200389 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200390 * @genpd: PM domain the device belongs to.
391 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200392static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200393 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200394 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200395{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200396 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200397 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200398 int ret = 0;
399
Ulf Hansson67732cd2014-11-11 11:07:08 +0100400 if (gpd_data->need_restore > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200401 return 0;
402
Ulf Hansson67732cd2014-11-11 11:07:08 +0100403 /*
404 * If the value of the need_restore flag is still unknown at this point,
405 * we trust that pm_genpd_poweroff() has verified that the device is
406 * already runtime PM suspended.
407 */
408 if (gpd_data->need_restore < 0) {
409 gpd_data->need_restore = 1;
410 return 0;
411 }
412
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200413 mutex_unlock(&genpd->lock);
414
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100415 genpd_start_dev(genpd, dev);
416 ret = genpd_save_dev(genpd, dev);
417 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200418
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200419 mutex_lock(&genpd->lock);
420
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200421 if (!ret)
Ulf Hansson67732cd2014-11-11 11:07:08 +0100422 gpd_data->need_restore = 1;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200423
424 return ret;
425}
426
427/**
428 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200429 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200430 * @genpd: PM domain the device belongs to.
431 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200432static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200433 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200434 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200435{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200436 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200437 struct device *dev = pdd->dev;
Ulf Hansson67732cd2014-11-11 11:07:08 +0100438 int need_restore = gpd_data->need_restore;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200439
Ulf Hansson67732cd2014-11-11 11:07:08 +0100440 gpd_data->need_restore = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200441 mutex_unlock(&genpd->lock);
442
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100443 genpd_start_dev(genpd, dev);
Ulf Hansson67732cd2014-11-11 11:07:08 +0100444
445 /*
446 * Call genpd_restore_dev() for recently added devices too (need_restore
447 * is negative then).
448 */
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200449 if (need_restore)
450 genpd_restore_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200451
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200452 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200453}
454
455/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200456 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
457 * @genpd: PM domain to check.
458 *
459 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
460 * a "power off" operation, which means that a "power on" has occured in the
461 * meantime, or if its resume_count field is different from zero, which means
462 * that one of its devices has been resumed in the meantime.
463 */
464static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
465{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200466 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200467 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200468}
469
470/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200471 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
472 * @genpd: PM domait to power off.
473 *
474 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
475 * before.
476 */
Ulf Hanssond971f0b2014-09-03 12:52:25 +0200477static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200478{
Tejun Heoa4ca26a2013-01-11 13:37:23 +0100479 queue_work(pm_wq, &genpd->power_off_work);
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200480}
481
482/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200483 * pm_genpd_poweroff - Remove power from a given PM domain.
484 * @genpd: PM domain to power down.
485 *
486 * If all of the @genpd's devices have been suspended and all of its subdomains
487 * have been powered down, run the runtime suspend callbacks provided by all of
488 * the @genpd's devices' drivers and remove power from @genpd.
489 */
490static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200491 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200492{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200493 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200494 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200495 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200496 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200497
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200498 start:
499 /*
500 * Do not try to power off the domain in the following situations:
501 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200502 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200503 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200504 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200505 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200506 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200507 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200508 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200509 return 0;
510
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200511 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200512 return -EBUSY;
513
514 not_suspended = 0;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200515 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
516 enum pm_qos_flags_status stat;
517
518 stat = dev_pm_qos_flags(pdd->dev,
519 PM_QOS_FLAG_NO_POWER_OFF
520 | PM_QOS_FLAG_REMOTE_WAKEUP);
521 if (stat > PM_QOS_FLAGS_NONE)
522 return -EBUSY;
523
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200524 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
Rafael J. Wysockifeb70af2012-08-13 14:00:25 +0200525 || pdd->dev->power.irq_safe))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200526 not_suspended++;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200527 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200528
529 if (not_suspended > genpd->in_progress)
530 return -EBUSY;
531
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200532 if (genpd->poweroff_task) {
533 /*
534 * Another instance of pm_genpd_poweroff() is executing
535 * callbacks, so tell it to start over and return.
536 */
537 genpd->status = GPD_STATE_REPEAT;
538 return 0;
539 }
540
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200541 if (genpd->gov && genpd->gov->power_down_ok) {
542 if (!genpd->gov->power_down_ok(&genpd->domain))
543 return -EAGAIN;
544 }
545
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200546 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200547 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200548
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200549 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200550 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200551 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200552
553 if (genpd_abort_poweroff(genpd))
554 goto out;
555
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200556 if (ret) {
557 genpd_set_active(genpd);
558 goto out;
559 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200560
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200561 if (genpd->status == GPD_STATE_REPEAT) {
562 genpd->poweroff_task = NULL;
563 goto start;
564 }
565 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200566
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200567 if (genpd->cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200568 /*
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200569 * If cpuidle_data is set, cpuidle should turn the domain off
570 * when the CPU in it is idle. In that case we don't decrement
571 * the subdomain counts of the master domains, so that power is
572 * not removed from the current domain prematurely as a result
573 * of cutting off the masters' power.
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200574 */
575 genpd->status = GPD_STATE_POWER_OFF;
576 cpuidle_pause_and_lock();
Ulf Hanssonf39cb172014-10-02 21:12:34 +0200577 genpd->cpuidle_data->idle_state->disabled = false;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200578 cpuidle_resume_and_unlock();
579 goto out;
580 }
581
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200582 if (genpd->power_off) {
583 if (atomic_read(&genpd->sd_count) > 0) {
584 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200585 goto out;
586 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200587
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200588 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200589 * If sd_count > 0 at this point, one of the subdomains hasn't
590 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200591 * incrementing it. In that case pm_genpd_poweron() will wait
592 * for us to drop the lock, so we can call .power_off() and let
593 * the pm_genpd_poweron() restore power for us (this shouldn't
594 * happen very often).
595 */
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100596 ret = genpd_power_off(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200597 if (ret == -EBUSY) {
598 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200599 goto out;
600 }
601 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200602
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200603 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100604
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200605 list_for_each_entry(link, &genpd->slave_links, slave_node) {
606 genpd_sd_counter_dec(link->master);
607 genpd_queue_power_off_work(link->master);
608 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200609
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200610 out:
611 genpd->poweroff_task = NULL;
612 wake_up_all(&genpd->status_wait_queue);
613 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200614}
615
616/**
617 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
618 * @work: Work structure used for scheduling the execution of this function.
619 */
620static void genpd_power_off_work_fn(struct work_struct *work)
621{
622 struct generic_pm_domain *genpd;
623
624 genpd = container_of(work, struct generic_pm_domain, power_off_work);
625
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200626 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200627 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200628 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200629}
630
631/**
632 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
633 * @dev: Device to suspend.
634 *
635 * Carry out a runtime suspend of a device under the assumption that its
636 * pm_domain field points to the domain member of an object of type
637 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
638 */
639static int pm_genpd_runtime_suspend(struct device *dev)
640{
641 struct generic_pm_domain *genpd;
Ulf Hansson67732cd2014-11-11 11:07:08 +0100642 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100643 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100644 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200645
646 dev_dbg(dev, "%s()\n", __func__);
647
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200648 genpd = dev_to_genpd(dev);
649 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200650 return -EINVAL;
651
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100652 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
653 if (stop_ok && !stop_ok(dev))
654 return -EBUSY;
655
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100656 ret = genpd_stop_dev(genpd, dev);
657 if (ret)
658 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200659
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200660 /*
661 * If power.irq_safe is set, this routine will be run with interrupts
662 * off, so it can't use mutexes.
663 */
664 if (dev->power.irq_safe)
665 return 0;
666
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200667 mutex_lock(&genpd->lock);
Ulf Hansson67732cd2014-11-11 11:07:08 +0100668
669 /*
670 * If we have an unknown state of the need_restore flag, it means none
671 * of the runtime PM callbacks has been invoked yet. Let's update the
672 * flag to reflect that the current state is active.
673 */
674 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
675 if (gpd_data->need_restore < 0)
676 gpd_data->need_restore = 0;
677
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200678 genpd->in_progress++;
679 pm_genpd_poweroff(genpd);
680 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200681 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200682
683 return 0;
684}
685
686/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200687 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
688 * @dev: Device to resume.
689 *
690 * Carry out a runtime resume of a device under the assumption that its
691 * pm_domain field points to the domain member of an object of type
692 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
693 */
694static int pm_genpd_runtime_resume(struct device *dev)
695{
696 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200697 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200698 int ret;
699
700 dev_dbg(dev, "%s()\n", __func__);
701
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200702 genpd = dev_to_genpd(dev);
703 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200704 return -EINVAL;
705
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200706 /* If power.irq_safe, the PM domain is never powered off. */
707 if (dev->power.irq_safe)
Rafael J. Wysockie2e3e4e52012-08-06 01:47:29 +0200708 return genpd_start_dev_no_timing(genpd, dev);
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200709
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200710 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200711 ret = __pm_genpd_poweron(genpd);
712 if (ret) {
713 mutex_unlock(&genpd->lock);
714 return ret;
715 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200716 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200717 genpd->resume_count++;
718 for (;;) {
719 prepare_to_wait(&genpd->status_wait_queue, &wait,
720 TASK_UNINTERRUPTIBLE);
721 /*
722 * If current is the powering off task, we have been called
723 * reentrantly from one of the device callbacks, so we should
724 * not wait.
725 */
726 if (!genpd->poweroff_task || genpd->poweroff_task == current)
727 break;
728 mutex_unlock(&genpd->lock);
729
730 schedule();
731
732 mutex_lock(&genpd->lock);
733 }
734 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200735 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200736 genpd->resume_count--;
737 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200738 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200739 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200740
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200741 return 0;
742}
743
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530744static bool pd_ignore_unused;
745static int __init pd_ignore_unused_setup(char *__unused)
746{
747 pd_ignore_unused = true;
748 return 1;
749}
750__setup("pd_ignore_unused", pd_ignore_unused_setup);
751
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200752/**
753 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
754 */
755void pm_genpd_poweroff_unused(void)
756{
757 struct generic_pm_domain *genpd;
758
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530759 if (pd_ignore_unused) {
760 pr_warn("genpd: Not disabling unused power domains\n");
761 return;
762 }
763
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200764 mutex_lock(&gpd_list_lock);
765
766 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
767 genpd_queue_power_off_work(genpd);
768
769 mutex_unlock(&gpd_list_lock);
770}
771
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200772static int __init genpd_poweroff_unused(void)
773{
774 pm_genpd_poweroff_unused();
775 return 0;
776}
777late_initcall(genpd_poweroff_unused);
778
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200779#ifdef CONFIG_PM_SLEEP
780
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200781/**
782 * pm_genpd_present - Check if the given PM domain has been initialized.
783 * @genpd: PM domain to check.
784 */
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100785static bool pm_genpd_present(const struct generic_pm_domain *genpd)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200786{
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100787 const struct generic_pm_domain *gpd;
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200788
789 if (IS_ERR_OR_NULL(genpd))
790 return false;
791
792 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
793 if (gpd == genpd)
794 return true;
795
796 return false;
797}
798
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100799static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
800 struct device *dev)
801{
802 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
803}
804
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200805/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200806 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200807 * @genpd: PM domain to power off, if possible.
808 *
809 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200810 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200811 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200812 * This function is only called in "noirq" and "syscore" stages of system power
813 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
814 * executed sequentially, so it is guaranteed that it will never run twice in
815 * parallel).
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200816 */
817static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
818{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200819 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200820
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200821 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200822 return;
823
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200824 if (genpd->suspended_count != genpd->device_count
825 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200826 return;
827
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100828 genpd_power_off(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200829
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200830 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200831
832 list_for_each_entry(link, &genpd->slave_links, slave_node) {
833 genpd_sd_counter_dec(link->master);
834 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200835 }
836}
837
838/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200839 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
840 * @genpd: PM domain to power on.
841 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200842 * This function is only called in "noirq" and "syscore" stages of system power
843 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
844 * executed sequentially, so it is guaranteed that it will never run twice in
845 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200846 */
847static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
848{
849 struct gpd_link *link;
850
851 if (genpd->status != GPD_STATE_POWER_OFF)
852 return;
853
854 list_for_each_entry(link, &genpd->slave_links, slave_node) {
855 pm_genpd_sync_poweron(link->master);
856 genpd_sd_counter_inc(link->master);
857 }
858
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100859 genpd_power_on(genpd);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200860
861 genpd->status = GPD_STATE_ACTIVE;
862}
863
864/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200865 * resume_needed - Check whether to resume a device before system suspend.
866 * @dev: Device to check.
867 * @genpd: PM domain the device belongs to.
868 *
869 * There are two cases in which a device that can wake up the system from sleep
870 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
871 * to wake up the system and it has to remain active for this purpose while the
872 * system is in the sleep state and (2) if the device is not enabled to wake up
873 * the system from sleep states and it generally doesn't generate wakeup signals
874 * by itself (those signals are generated on its behalf by other parts of the
875 * system). In the latter case it may be necessary to reconfigure the device's
876 * wakeup settings during system suspend, because it may have been set up to
877 * signal remote wakeup from the system's working state as needed by runtime PM.
878 * Return 'true' in either of the above cases.
879 */
880static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
881{
882 bool active_wakeup;
883
884 if (!device_can_wakeup(dev))
885 return false;
886
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100887 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200888 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
889}
890
891/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200892 * pm_genpd_prepare - Start power transition of a device in a PM domain.
893 * @dev: Device to start the transition of.
894 *
895 * Start a power transition of a device (during a system-wide power transition)
896 * under the assumption that its pm_domain field points to the domain member of
897 * an object of type struct generic_pm_domain representing a PM domain
898 * consisting of I/O devices.
899 */
900static int pm_genpd_prepare(struct device *dev)
901{
902 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200903 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200904
905 dev_dbg(dev, "%s()\n", __func__);
906
907 genpd = dev_to_genpd(dev);
908 if (IS_ERR(genpd))
909 return -EINVAL;
910
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200911 /*
912 * If a wakeup request is pending for the device, it should be woken up
913 * at this point and a system wakeup event should be reported if it's
914 * set up to wake up the system from sleep states.
915 */
916 pm_runtime_get_noresume(dev);
917 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
918 pm_wakeup_event(dev, 0);
919
920 if (pm_wakeup_pending()) {
Ulf Hansson84167032013-04-12 09:41:44 +0000921 pm_runtime_put(dev);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200922 return -EBUSY;
923 }
924
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200925 if (resume_needed(dev, genpd))
926 pm_runtime_resume(dev);
927
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200928 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200929
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100930 if (genpd->prepared_count++ == 0) {
931 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200932 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100933 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200934
935 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200936
937 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200938 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200939 return 0;
940 }
941
942 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200943 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
944 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100945 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200946 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200947 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200948 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200949 __pm_runtime_disable(dev, false);
950
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200951 ret = pm_generic_prepare(dev);
952 if (ret) {
953 mutex_lock(&genpd->lock);
954
955 if (--genpd->prepared_count == 0)
956 genpd->suspend_power_off = false;
957
958 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200959 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200960 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200961
Ulf Hansson84167032013-04-12 09:41:44 +0000962 pm_runtime_put(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200963 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200964}
965
966/**
967 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
968 * @dev: Device to suspend.
969 *
970 * Suspend a device under the assumption that its pm_domain field points to the
971 * domain member of an object of type struct generic_pm_domain representing
972 * a PM domain consisting of I/O devices.
973 */
974static int pm_genpd_suspend(struct device *dev)
975{
976 struct generic_pm_domain *genpd;
977
978 dev_dbg(dev, "%s()\n", __func__);
979
980 genpd = dev_to_genpd(dev);
981 if (IS_ERR(genpd))
982 return -EINVAL;
983
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200984 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200985}
986
987/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100988 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200989 * @dev: Device to suspend.
990 *
991 * Carry out a late suspend of a device under the assumption that its
992 * pm_domain field points to the domain member of an object of type
993 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
994 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100995static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200996{
997 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200998
999 dev_dbg(dev, "%s()\n", __func__);
1000
1001 genpd = dev_to_genpd(dev);
1002 if (IS_ERR(genpd))
1003 return -EINVAL;
1004
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001005 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001006}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001007
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001008/**
1009 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1010 * @dev: Device to suspend.
1011 *
1012 * Stop the device and remove power from the domain if all devices in it have
1013 * been stopped.
1014 */
1015static int pm_genpd_suspend_noirq(struct device *dev)
1016{
1017 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001018
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001019 dev_dbg(dev, "%s()\n", __func__);
1020
1021 genpd = dev_to_genpd(dev);
1022 if (IS_ERR(genpd))
1023 return -EINVAL;
1024
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001025 if (genpd->suspend_power_off
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001026 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +02001027 return 0;
1028
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001029 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001030
1031 /*
1032 * Since all of the "noirq" callbacks are executed sequentially, it is
1033 * guaranteed that this function will never run twice in parallel for
1034 * the same PM domain, so it is not necessary to use locking here.
1035 */
1036 genpd->suspended_count++;
1037 pm_genpd_sync_poweroff(genpd);
1038
1039 return 0;
1040}
1041
1042/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001043 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001044 * @dev: Device to resume.
1045 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001046 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001047 */
1048static int pm_genpd_resume_noirq(struct device *dev)
1049{
1050 struct generic_pm_domain *genpd;
1051
1052 dev_dbg(dev, "%s()\n", __func__);
1053
1054 genpd = dev_to_genpd(dev);
1055 if (IS_ERR(genpd))
1056 return -EINVAL;
1057
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001058 if (genpd->suspend_power_off
Rafael J. Wysockicc85b202012-03-13 22:39:31 +01001059 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001060 return 0;
1061
1062 /*
1063 * Since all of the "noirq" callbacks are executed sequentially, it is
1064 * guaranteed that this function will never run twice in parallel for
1065 * the same PM domain, so it is not necessary to use locking here.
1066 */
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001067 pm_genpd_sync_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001068 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001069
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001070 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001071}
1072
1073/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001074 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1075 * @dev: Device to resume.
1076 *
1077 * Carry out an early resume of a device under the assumption that its
1078 * pm_domain field points to the domain member of an object of type
1079 * struct generic_pm_domain representing a power domain consisting of I/O
1080 * devices.
1081 */
1082static int pm_genpd_resume_early(struct device *dev)
1083{
1084 struct generic_pm_domain *genpd;
1085
1086 dev_dbg(dev, "%s()\n", __func__);
1087
1088 genpd = dev_to_genpd(dev);
1089 if (IS_ERR(genpd))
1090 return -EINVAL;
1091
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001092 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001093}
1094
1095/**
1096 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001097 * @dev: Device to resume.
1098 *
1099 * Resume a device under the assumption that its pm_domain field points to the
1100 * domain member of an object of type struct generic_pm_domain representing
1101 * a power domain consisting of I/O devices.
1102 */
1103static int pm_genpd_resume(struct device *dev)
1104{
1105 struct generic_pm_domain *genpd;
1106
1107 dev_dbg(dev, "%s()\n", __func__);
1108
1109 genpd = dev_to_genpd(dev);
1110 if (IS_ERR(genpd))
1111 return -EINVAL;
1112
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001113 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001114}
1115
1116/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001117 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001118 * @dev: Device to freeze.
1119 *
1120 * Freeze a device under the assumption that its pm_domain field points to the
1121 * domain member of an object of type struct generic_pm_domain representing
1122 * a power domain consisting of I/O devices.
1123 */
1124static int pm_genpd_freeze(struct device *dev)
1125{
1126 struct generic_pm_domain *genpd;
1127
1128 dev_dbg(dev, "%s()\n", __func__);
1129
1130 genpd = dev_to_genpd(dev);
1131 if (IS_ERR(genpd))
1132 return -EINVAL;
1133
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001134 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001135}
1136
1137/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001138 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1139 * @dev: Device to freeze.
1140 *
1141 * Carry out a late freeze of a device under the assumption that its
1142 * pm_domain field points to the domain member of an object of type
1143 * struct generic_pm_domain representing a power domain consisting of I/O
1144 * devices.
1145 */
1146static int pm_genpd_freeze_late(struct device *dev)
1147{
1148 struct generic_pm_domain *genpd;
1149
1150 dev_dbg(dev, "%s()\n", __func__);
1151
1152 genpd = dev_to_genpd(dev);
1153 if (IS_ERR(genpd))
1154 return -EINVAL;
1155
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001156 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001157}
1158
1159/**
1160 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001161 * @dev: Device to freeze.
1162 *
1163 * Carry out a late freeze of a device under the assumption that its
1164 * pm_domain field points to the domain member of an object of type
1165 * struct generic_pm_domain representing a power domain consisting of I/O
1166 * devices.
1167 */
1168static int pm_genpd_freeze_noirq(struct device *dev)
1169{
1170 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001171
1172 dev_dbg(dev, "%s()\n", __func__);
1173
1174 genpd = dev_to_genpd(dev);
1175 if (IS_ERR(genpd))
1176 return -EINVAL;
1177
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001178 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001179}
1180
1181/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001182 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001183 * @dev: Device to thaw.
1184 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001185 * Start the device, unless power has been removed from the domain already
1186 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001187 */
1188static int pm_genpd_thaw_noirq(struct device *dev)
1189{
1190 struct generic_pm_domain *genpd;
1191
1192 dev_dbg(dev, "%s()\n", __func__);
1193
1194 genpd = dev_to_genpd(dev);
1195 if (IS_ERR(genpd))
1196 return -EINVAL;
1197
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001198 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001199}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001200
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001201/**
1202 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1203 * @dev: Device to thaw.
1204 *
1205 * Carry out an early thaw of a device under the assumption that its
1206 * pm_domain field points to the domain member of an object of type
1207 * struct generic_pm_domain representing a power domain consisting of I/O
1208 * devices.
1209 */
1210static int pm_genpd_thaw_early(struct device *dev)
1211{
1212 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001213
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001214 dev_dbg(dev, "%s()\n", __func__);
1215
1216 genpd = dev_to_genpd(dev);
1217 if (IS_ERR(genpd))
1218 return -EINVAL;
1219
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001220 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001221}
1222
1223/**
1224 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1225 * @dev: Device to thaw.
1226 *
1227 * Thaw a device under the assumption that its pm_domain field points to the
1228 * domain member of an object of type struct generic_pm_domain representing
1229 * a power domain consisting of I/O devices.
1230 */
1231static int pm_genpd_thaw(struct device *dev)
1232{
1233 struct generic_pm_domain *genpd;
1234
1235 dev_dbg(dev, "%s()\n", __func__);
1236
1237 genpd = dev_to_genpd(dev);
1238 if (IS_ERR(genpd))
1239 return -EINVAL;
1240
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001241 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001242}
1243
1244/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001245 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001246 * @dev: Device to resume.
1247 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001248 * Make sure the domain will be in the same power state as before the
1249 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001250 */
1251static int pm_genpd_restore_noirq(struct device *dev)
1252{
1253 struct generic_pm_domain *genpd;
1254
1255 dev_dbg(dev, "%s()\n", __func__);
1256
1257 genpd = dev_to_genpd(dev);
1258 if (IS_ERR(genpd))
1259 return -EINVAL;
1260
1261 /*
1262 * Since all of the "noirq" callbacks are executed sequentially, it is
1263 * guaranteed that this function will never run twice in parallel for
1264 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001265 *
1266 * At this point suspended_count == 0 means we are being run for the
1267 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001268 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001269 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001270 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001271 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001272 * so make it appear as powered off to pm_genpd_sync_poweron(),
1273 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001274 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001275 genpd->status = GPD_STATE_POWER_OFF;
1276 if (genpd->suspend_power_off) {
1277 /*
1278 * If the domain was off before the hibernation, make
1279 * sure it will be off going forward.
1280 */
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +01001281 genpd_power_off(genpd);
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001282
1283 return 0;
1284 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001285 }
1286
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001287 if (genpd->suspend_power_off)
1288 return 0;
1289
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001290 pm_genpd_sync_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001291
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001292 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001293}
1294
1295/**
1296 * pm_genpd_complete - Complete power transition of a device in a power domain.
1297 * @dev: Device to complete the transition of.
1298 *
1299 * Complete a power transition of a device (during a system-wide power
1300 * transition) under the assumption that its pm_domain field points to the
1301 * domain member of an object of type struct generic_pm_domain representing
1302 * a power domain consisting of I/O devices.
1303 */
1304static void pm_genpd_complete(struct device *dev)
1305{
1306 struct generic_pm_domain *genpd;
1307 bool run_complete;
1308
1309 dev_dbg(dev, "%s()\n", __func__);
1310
1311 genpd = dev_to_genpd(dev);
1312 if (IS_ERR(genpd))
1313 return;
1314
1315 mutex_lock(&genpd->lock);
1316
1317 run_complete = !genpd->suspend_power_off;
1318 if (--genpd->prepared_count == 0)
1319 genpd->suspend_power_off = false;
1320
1321 mutex_unlock(&genpd->lock);
1322
1323 if (run_complete) {
1324 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001325 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001326 pm_runtime_enable(dev);
Ulf Hanssonaf939332013-04-12 09:41:06 +00001327 pm_request_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001328 }
1329}
1330
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001331/**
Ulf Hanssond47e6462014-09-03 12:52:24 +02001332 * genpd_syscore_switch - Switch power during system core suspend or resume.
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001333 * @dev: Device that normally is marked as "always on" to switch power for.
1334 *
1335 * This routine may only be called during the system core (syscore) suspend or
1336 * resume phase for devices whose "always on" flags are set.
1337 */
Ulf Hanssond47e6462014-09-03 12:52:24 +02001338static void genpd_syscore_switch(struct device *dev, bool suspend)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001339{
1340 struct generic_pm_domain *genpd;
1341
1342 genpd = dev_to_genpd(dev);
1343 if (!pm_genpd_present(genpd))
1344 return;
1345
1346 if (suspend) {
1347 genpd->suspended_count++;
1348 pm_genpd_sync_poweroff(genpd);
1349 } else {
1350 pm_genpd_sync_poweron(genpd);
1351 genpd->suspended_count--;
1352 }
1353}
Ulf Hanssond47e6462014-09-03 12:52:24 +02001354
1355void pm_genpd_syscore_poweroff(struct device *dev)
1356{
1357 genpd_syscore_switch(dev, true);
1358}
1359EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1360
1361void pm_genpd_syscore_poweron(struct device *dev)
1362{
1363 genpd_syscore_switch(dev, false);
1364}
1365EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001366
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001367#else /* !CONFIG_PM_SLEEP */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001368
1369#define pm_genpd_prepare NULL
1370#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001371#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001372#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001373#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001374#define pm_genpd_resume_noirq NULL
1375#define pm_genpd_resume NULL
1376#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001377#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001378#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001379#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001380#define pm_genpd_thaw_noirq NULL
1381#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001382#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001383#define pm_genpd_complete NULL
1384
1385#endif /* CONFIG_PM_SLEEP */
1386
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001387static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1388{
1389 struct generic_pm_domain_data *gpd_data;
1390
1391 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1392 if (!gpd_data)
1393 return NULL;
1394
1395 mutex_init(&gpd_data->lock);
1396 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1397 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1398 return gpd_data;
1399}
1400
1401static void __pm_genpd_free_dev_data(struct device *dev,
1402 struct generic_pm_domain_data *gpd_data)
1403{
1404 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1405 kfree(gpd_data);
1406}
1407
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001408/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001409 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001410 * @genpd: PM domain to add the device to.
1411 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001412 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001413 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001414int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1415 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001416{
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001417 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001418 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001419 int ret = 0;
1420
1421 dev_dbg(dev, "%s()\n", __func__);
1422
1423 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1424 return -EINVAL;
1425
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001426 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1427 if (!gpd_data_new)
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001428 return -ENOMEM;
1429
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001430 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001431
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001432 if (genpd->prepared_count > 0) {
1433 ret = -EAGAIN;
1434 goto out;
1435 }
1436
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001437 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1438 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001439 ret = -EINVAL;
1440 goto out;
1441 }
1442
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001443 ret = dev_pm_get_subsys_data(dev);
1444 if (ret)
1445 goto out;
1446
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001447 genpd->device_count++;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001448 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001449
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001450 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001451
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001452 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001453 if (dev->power.subsys_data->domain_data) {
1454 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1455 } else {
1456 gpd_data = gpd_data_new;
1457 dev->power.subsys_data->domain_data = &gpd_data->base;
1458 }
1459 gpd_data->refcount++;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001460 if (td)
1461 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001462
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001463 spin_unlock_irq(&dev->power.lock);
1464
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001465 if (genpd->attach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001466 genpd->attach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001467
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001468 mutex_lock(&gpd_data->lock);
1469 gpd_data->base.dev = dev;
1470 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Ulf Hansson67732cd2014-11-11 11:07:08 +01001471 gpd_data->need_restore = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001472 gpd_data->td.constraint_changed = true;
1473 gpd_data->td.effective_constraint_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001474 mutex_unlock(&gpd_data->lock);
1475
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001476 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001477 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001478
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001479 if (gpd_data != gpd_data_new)
1480 __pm_genpd_free_dev_data(dev, gpd_data_new);
1481
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001482 return ret;
1483}
1484
1485/**
Rafael J. Wysockib5abb082012-08-07 01:06:11 +02001486 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1487 * @domain_name: Name of the PM domain to add the device to.
1488 * @dev: Device to be added.
1489 * @td: Set of PM QoS timing parameters to attach to the device.
1490 */
1491int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1492 struct gpd_timing_data *td)
1493{
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +02001494 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
Rafael J. Wysockib5abb082012-08-07 01:06:11 +02001495}
1496
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001497/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001498 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1499 * @genpd: PM domain to remove the device from.
1500 * @dev: Device to be removed.
1501 */
1502int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1503 struct device *dev)
1504{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001505 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001506 struct pm_domain_data *pdd;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001507 bool remove = false;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001508 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001509
1510 dev_dbg(dev, "%s()\n", __func__);
1511
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001512 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1513 || IS_ERR_OR_NULL(dev->pm_domain)
1514 || pd_to_genpd(dev->pm_domain) != genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001515 return -EINVAL;
1516
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001517 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001518
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001519 if (genpd->prepared_count > 0) {
1520 ret = -EAGAIN;
1521 goto out;
1522 }
1523
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001524 genpd->device_count--;
1525 genpd->max_off_time_changed = true;
1526
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001527 if (genpd->detach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001528 genpd->detach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001529
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001530 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001531
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001532 dev->pm_domain = NULL;
1533 pdd = dev->power.subsys_data->domain_data;
1534 list_del_init(&pdd->list_node);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001535 gpd_data = to_gpd_data(pdd);
1536 if (--gpd_data->refcount == 0) {
1537 dev->power.subsys_data->domain_data = NULL;
1538 remove = true;
1539 }
1540
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001541 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001542
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001543 mutex_lock(&gpd_data->lock);
1544 pdd->dev = NULL;
1545 mutex_unlock(&gpd_data->lock);
1546
1547 genpd_release_lock(genpd);
1548
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001549 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001550 if (remove)
1551 __pm_genpd_free_dev_data(dev, gpd_data);
1552
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001553 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001554
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001555 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001556 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001557
1558 return ret;
1559}
1560
1561/**
Rafael J. Wysockica1d72f2012-05-14 21:45:52 +02001562 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1563 * @dev: Device to set/unset the flag for.
1564 * @val: The new value of the device's "need restore" flag.
1565 */
1566void pm_genpd_dev_need_restore(struct device *dev, bool val)
1567{
1568 struct pm_subsys_data *psd;
1569 unsigned long flags;
1570
1571 spin_lock_irqsave(&dev->power.lock, flags);
1572
1573 psd = dev_to_psd(dev);
1574 if (psd && psd->domain_data)
Ulf Hansson67732cd2014-11-11 11:07:08 +01001575 to_gpd_data(psd->domain_data)->need_restore = val ? 1 : 0;
Rafael J. Wysockica1d72f2012-05-14 21:45:52 +02001576
1577 spin_unlock_irqrestore(&dev->power.lock, flags);
1578}
1579EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1580
1581/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001582 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1583 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001584 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001585 */
1586int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001587 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001588{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001589 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001590 int ret = 0;
1591
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001592 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1593 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001594 return -EINVAL;
1595
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001596 start:
1597 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001598 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001599
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001600 if (subdomain->status != GPD_STATE_POWER_OFF
1601 && subdomain->status != GPD_STATE_ACTIVE) {
1602 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001603 genpd_release_lock(genpd);
1604 goto start;
1605 }
1606
1607 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001608 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001609 ret = -EINVAL;
1610 goto out;
1611 }
1612
Huang Ying4fcac102012-05-07 21:35:45 +02001613 list_for_each_entry(link, &genpd->master_links, master_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001614 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001615 ret = -EINVAL;
1616 goto out;
1617 }
1618 }
1619
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001620 link = kzalloc(sizeof(*link), GFP_KERNEL);
1621 if (!link) {
1622 ret = -ENOMEM;
1623 goto out;
1624 }
1625 link->master = genpd;
1626 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001627 link->slave = subdomain;
1628 list_add_tail(&link->slave_node, &subdomain->slave_links);
1629 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001630 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001631
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001632 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001633 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001634 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001635
1636 return ret;
1637}
1638
1639/**
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001640 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1641 * @master_name: Name of the master PM domain to add the subdomain to.
1642 * @subdomain_name: Name of the subdomain to be added.
1643 */
1644int pm_genpd_add_subdomain_names(const char *master_name,
1645 const char *subdomain_name)
1646{
1647 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1648
1649 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1650 return -EINVAL;
1651
1652 mutex_lock(&gpd_list_lock);
1653 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1654 if (!master && !strcmp(gpd->name, master_name))
1655 master = gpd;
1656
1657 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1658 subdomain = gpd;
1659
1660 if (master && subdomain)
1661 break;
1662 }
1663 mutex_unlock(&gpd_list_lock);
1664
1665 return pm_genpd_add_subdomain(master, subdomain);
1666}
1667
1668/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001669 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1670 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001671 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001672 */
1673int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001674 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001675{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001676 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001677 int ret = -EINVAL;
1678
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001679 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001680 return -EINVAL;
1681
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001682 start:
1683 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001684
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001685 list_for_each_entry(link, &genpd->master_links, master_node) {
1686 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001687 continue;
1688
1689 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1690
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001691 if (subdomain->status != GPD_STATE_POWER_OFF
1692 && subdomain->status != GPD_STATE_ACTIVE) {
1693 mutex_unlock(&subdomain->lock);
1694 genpd_release_lock(genpd);
1695 goto start;
1696 }
1697
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001698 list_del(&link->master_node);
1699 list_del(&link->slave_node);
1700 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001701 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001702 genpd_sd_counter_dec(genpd);
1703
1704 mutex_unlock(&subdomain->lock);
1705
1706 ret = 0;
1707 break;
1708 }
1709
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001710 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001711
1712 return ret;
1713}
1714
1715/**
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001716 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1717 * @genpd: PM domain to be connected with cpuidle.
1718 * @state: cpuidle state this domain can disable/enable.
1719 *
1720 * Make a PM domain behave as though it contained a CPU core, that is, instead
1721 * of calling its power down routine it will enable the given cpuidle state so
1722 * that the cpuidle subsystem can power it down (if possible and desirable).
1723 */
1724int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001725{
1726 struct cpuidle_driver *cpuidle_drv;
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001727 struct gpd_cpuidle_data *cpuidle_data;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001728 struct cpuidle_state *idle_state;
1729 int ret = 0;
1730
1731 if (IS_ERR_OR_NULL(genpd) || state < 0)
1732 return -EINVAL;
1733
1734 genpd_acquire_lock(genpd);
1735
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001736 if (genpd->cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001737 ret = -EEXIST;
1738 goto out;
1739 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001740 cpuidle_data = kzalloc(sizeof(*cpuidle_data), GFP_KERNEL);
1741 if (!cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001742 ret = -ENOMEM;
1743 goto out;
1744 }
1745 cpuidle_drv = cpuidle_driver_ref();
1746 if (!cpuidle_drv) {
1747 ret = -ENODEV;
jhbird.choi@samsung.comdebe0812012-10-23 00:54:38 +02001748 goto err_drv;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001749 }
1750 if (cpuidle_drv->state_count <= state) {
1751 ret = -EINVAL;
1752 goto err;
1753 }
1754 idle_state = &cpuidle_drv->states[state];
1755 if (!idle_state->disabled) {
1756 ret = -EAGAIN;
1757 goto err;
1758 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001759 cpuidle_data->idle_state = idle_state;
1760 cpuidle_data->saved_exit_latency = idle_state->exit_latency;
1761 genpd->cpuidle_data = cpuidle_data;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001762 genpd_recalc_cpu_exit_latency(genpd);
1763
1764 out:
1765 genpd_release_lock(genpd);
1766 return ret;
1767
1768 err:
1769 cpuidle_driver_unref();
jhbird.choi@samsung.comdebe0812012-10-23 00:54:38 +02001770
1771 err_drv:
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001772 kfree(cpuidle_data);
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001773 goto out;
1774}
1775
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001776/**
Rafael J. Wysocki74a2799a2012-08-15 20:32:59 +02001777 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1778 * @name: Name of the domain to connect to cpuidle.
1779 * @state: cpuidle state this domain can manipulate.
1780 */
1781int pm_genpd_name_attach_cpuidle(const char *name, int state)
1782{
1783 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1784}
1785
1786/**
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001787 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1788 * @genpd: PM domain to remove the cpuidle connection from.
1789 *
1790 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1791 * given PM domain.
1792 */
1793int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001794{
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001795 struct gpd_cpuidle_data *cpuidle_data;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001796 struct cpuidle_state *idle_state;
1797 int ret = 0;
1798
1799 if (IS_ERR_OR_NULL(genpd))
1800 return -EINVAL;
1801
1802 genpd_acquire_lock(genpd);
1803
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001804 cpuidle_data = genpd->cpuidle_data;
1805 if (!cpuidle_data) {
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001806 ret = -ENODEV;
1807 goto out;
1808 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001809 idle_state = cpuidle_data->idle_state;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001810 if (!idle_state->disabled) {
1811 ret = -EAGAIN;
1812 goto out;
1813 }
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001814 idle_state->exit_latency = cpuidle_data->saved_exit_latency;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001815 cpuidle_driver_unref();
Ulf Hanssonf39cb172014-10-02 21:12:34 +02001816 genpd->cpuidle_data = NULL;
1817 kfree(cpuidle_data);
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001818
1819 out:
1820 genpd_release_lock(genpd);
1821 return ret;
1822}
1823
Rafael J. Wysocki74a2799a2012-08-15 20:32:59 +02001824/**
1825 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1826 * @name: Name of the domain to disconnect cpuidle from.
1827 */
1828int pm_genpd_name_detach_cpuidle(const char *name)
1829{
1830 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1831}
1832
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001833/* Default device callbacks for generic PM domains. */
1834
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001835/**
Geert Uytterhoeven12e10bb2014-09-16 21:59:39 +02001836 * pm_genpd_default_save_state - Default "save device state" for PM domains.
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001837 * @dev: Device to handle.
1838 */
1839static int pm_genpd_default_save_state(struct device *dev)
1840{
1841 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001842
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001843 if (dev->type && dev->type->pm)
1844 cb = dev->type->pm->runtime_suspend;
1845 else if (dev->class && dev->class->pm)
1846 cb = dev->class->pm->runtime_suspend;
1847 else if (dev->bus && dev->bus->pm)
1848 cb = dev->bus->pm->runtime_suspend;
1849 else
1850 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001851
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001852 if (!cb && dev->driver && dev->driver->pm)
1853 cb = dev->driver->pm->runtime_suspend;
1854
1855 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001856}
1857
1858/**
Geert Uytterhoeven12e10bb2014-09-16 21:59:39 +02001859 * pm_genpd_default_restore_state - Default PM domains "restore device state".
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001860 * @dev: Device to handle.
1861 */
1862static int pm_genpd_default_restore_state(struct device *dev)
1863{
1864 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001865
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001866 if (dev->type && dev->type->pm)
1867 cb = dev->type->pm->runtime_resume;
1868 else if (dev->class && dev->class->pm)
1869 cb = dev->class->pm->runtime_resume;
1870 else if (dev->bus && dev->bus->pm)
1871 cb = dev->bus->pm->runtime_resume;
1872 else
1873 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001874
Rafael J. Wysocki0b5897412012-06-16 00:02:22 +02001875 if (!cb && dev->driver && dev->driver->pm)
1876 cb = dev->driver->pm->runtime_resume;
1877
1878 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001879}
1880
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001881/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001882 * pm_genpd_init - Initialize a generic I/O PM domain object.
1883 * @genpd: PM domain object to initialize.
1884 * @gov: PM domain governor to associate with the domain (may be NULL).
1885 * @is_off: Initial value of the domain's power_is_off field.
1886 */
1887void pm_genpd_init(struct generic_pm_domain *genpd,
1888 struct dev_power_governor *gov, bool is_off)
1889{
1890 if (IS_ERR_OR_NULL(genpd))
1891 return;
1892
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001893 INIT_LIST_HEAD(&genpd->master_links);
1894 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001895 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001896 mutex_init(&genpd->lock);
1897 genpd->gov = gov;
1898 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1899 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001900 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001901 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1902 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02001903 genpd->poweroff_task = NULL;
1904 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001905 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001906 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001907 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001908 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1909 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001910 genpd->domain.ops.prepare = pm_genpd_prepare;
1911 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001912 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001913 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1914 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001915 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001916 genpd->domain.ops.resume = pm_genpd_resume;
1917 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001918 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001919 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1920 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001921 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001922 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001923 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001924 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001925 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001926 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001927 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001928 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001929 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001930 genpd->dev_ops.save_state = pm_genpd_default_save_state;
1931 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Ulf Hanssonc11f6f52014-12-01 12:50:21 +01001932
1933 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1934 genpd->dev_ops.stop = pm_clk_suspend;
1935 genpd->dev_ops.start = pm_clk_resume;
1936 }
1937
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02001938 mutex_lock(&gpd_list_lock);
1939 list_add(&genpd->gpd_list_node, &gpd_list);
1940 mutex_unlock(&gpd_list_lock);
1941}
Tomasz Figaaa422402014-09-19 20:27:36 +02001942
1943#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1944/*
1945 * Device Tree based PM domain providers.
1946 *
1947 * The code below implements generic device tree based PM domain providers that
1948 * bind device tree nodes with generic PM domains registered in the system.
1949 *
1950 * Any driver that registers generic PM domains and needs to support binding of
1951 * devices to these domains is supposed to register a PM domain provider, which
1952 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1953 *
1954 * Two simple mapping functions have been provided for convenience:
1955 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1956 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1957 * index.
1958 */
1959
1960/**
1961 * struct of_genpd_provider - PM domain provider registration structure
1962 * @link: Entry in global list of PM domain providers
1963 * @node: Pointer to device tree node of PM domain provider
1964 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1965 * into a PM domain.
1966 * @data: context pointer to be passed into @xlate callback
1967 */
1968struct of_genpd_provider {
1969 struct list_head link;
1970 struct device_node *node;
1971 genpd_xlate_t xlate;
1972 void *data;
1973};
1974
1975/* List of registered PM domain providers. */
1976static LIST_HEAD(of_genpd_providers);
1977/* Mutex to protect the list above. */
1978static DEFINE_MUTEX(of_genpd_mutex);
1979
1980/**
1981 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1982 * @genpdspec: OF phandle args to map into a PM domain
1983 * @data: xlate function private data - pointer to struct generic_pm_domain
1984 *
1985 * This is a generic xlate function that can be used to model PM domains that
1986 * have their own device tree nodes. The private data of xlate function needs
1987 * to be a valid pointer to struct generic_pm_domain.
1988 */
1989struct generic_pm_domain *__of_genpd_xlate_simple(
1990 struct of_phandle_args *genpdspec,
1991 void *data)
1992{
1993 if (genpdspec->args_count != 0)
1994 return ERR_PTR(-EINVAL);
1995 return data;
1996}
1997EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1998
1999/**
2000 * __of_genpd_xlate_onecell() - Xlate function using a single index.
2001 * @genpdspec: OF phandle args to map into a PM domain
2002 * @data: xlate function private data - pointer to struct genpd_onecell_data
2003 *
2004 * This is a generic xlate function that can be used to model simple PM domain
2005 * controllers that have one device tree node and provide multiple PM domains.
2006 * A single cell is used as an index into an array of PM domains specified in
2007 * the genpd_onecell_data struct when registering the provider.
2008 */
2009struct generic_pm_domain *__of_genpd_xlate_onecell(
2010 struct of_phandle_args *genpdspec,
2011 void *data)
2012{
2013 struct genpd_onecell_data *genpd_data = data;
2014 unsigned int idx = genpdspec->args[0];
2015
2016 if (genpdspec->args_count != 1)
2017 return ERR_PTR(-EINVAL);
2018
2019 if (idx >= genpd_data->num_domains) {
2020 pr_err("%s: invalid domain index %u\n", __func__, idx);
2021 return ERR_PTR(-EINVAL);
2022 }
2023
2024 if (!genpd_data->domains[idx])
2025 return ERR_PTR(-ENOENT);
2026
2027 return genpd_data->domains[idx];
2028}
2029EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
2030
2031/**
2032 * __of_genpd_add_provider() - Register a PM domain provider for a node
2033 * @np: Device node pointer associated with the PM domain provider.
2034 * @xlate: Callback for decoding PM domain from phandle arguments.
2035 * @data: Context pointer for @xlate callback.
2036 */
2037int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
2038 void *data)
2039{
2040 struct of_genpd_provider *cp;
2041
2042 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2043 if (!cp)
2044 return -ENOMEM;
2045
2046 cp->node = of_node_get(np);
2047 cp->data = data;
2048 cp->xlate = xlate;
2049
2050 mutex_lock(&of_genpd_mutex);
2051 list_add(&cp->link, &of_genpd_providers);
2052 mutex_unlock(&of_genpd_mutex);
2053 pr_debug("Added domain provider from %s\n", np->full_name);
2054
2055 return 0;
2056}
2057EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
2058
2059/**
2060 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2061 * @np: Device node pointer associated with the PM domain provider
2062 */
2063void of_genpd_del_provider(struct device_node *np)
2064{
2065 struct of_genpd_provider *cp;
2066
2067 mutex_lock(&of_genpd_mutex);
2068 list_for_each_entry(cp, &of_genpd_providers, link) {
2069 if (cp->node == np) {
2070 list_del(&cp->link);
2071 of_node_put(cp->node);
2072 kfree(cp);
2073 break;
2074 }
2075 }
2076 mutex_unlock(&of_genpd_mutex);
2077}
2078EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2079
2080/**
2081 * of_genpd_get_from_provider() - Look-up PM domain
2082 * @genpdspec: OF phandle args to use for look-up
2083 *
2084 * Looks for a PM domain provider under the node specified by @genpdspec and if
2085 * found, uses xlate function of the provider to map phandle args to a PM
2086 * domain.
2087 *
2088 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2089 * on failure.
2090 */
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05302091struct generic_pm_domain *of_genpd_get_from_provider(
Tomasz Figaaa422402014-09-19 20:27:36 +02002092 struct of_phandle_args *genpdspec)
2093{
2094 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2095 struct of_genpd_provider *provider;
2096
2097 mutex_lock(&of_genpd_mutex);
2098
2099 /* Check if we have such a provider in our array */
2100 list_for_each_entry(provider, &of_genpd_providers, link) {
2101 if (provider->node == genpdspec->np)
2102 genpd = provider->xlate(genpdspec, provider->data);
2103 if (!IS_ERR(genpd))
2104 break;
2105 }
2106
2107 mutex_unlock(&of_genpd_mutex);
2108
2109 return genpd;
2110}
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05302111EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
Tomasz Figaaa422402014-09-19 20:27:36 +02002112
2113/**
2114 * genpd_dev_pm_detach - Detach a device from its PM domain.
2115 * @dev: Device to attach.
2116 * @power_off: Currently not used
2117 *
2118 * Try to locate a corresponding generic PM domain, which the device was
2119 * attached to previously. If such is found, the device is detached from it.
2120 */
2121static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2122{
2123 struct generic_pm_domain *pd = NULL, *gpd;
2124 int ret = 0;
2125
2126 if (!dev->pm_domain)
2127 return;
2128
2129 mutex_lock(&gpd_list_lock);
2130 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2131 if (&gpd->domain == dev->pm_domain) {
2132 pd = gpd;
2133 break;
2134 }
2135 }
2136 mutex_unlock(&gpd_list_lock);
2137
2138 if (!pd)
2139 return;
2140
2141 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2142
2143 while (1) {
2144 ret = pm_genpd_remove_device(pd, dev);
2145 if (ret != -EAGAIN)
2146 break;
2147 cond_resched();
2148 }
2149
2150 if (ret < 0) {
2151 dev_err(dev, "failed to remove from PM domain %s: %d",
2152 pd->name, ret);
2153 return;
2154 }
2155
2156 /* Check if PM domain can be powered off after removing this device. */
2157 genpd_queue_power_off_work(pd);
2158}
2159
2160/**
2161 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2162 * @dev: Device to attach.
2163 *
2164 * Parse device's OF node to find a PM domain specifier. If such is found,
2165 * attaches the device to retrieved pm_domain ops.
2166 *
2167 * Both generic and legacy Samsung-specific DT bindings are supported to keep
2168 * backwards compatibility with existing DTBs.
2169 *
2170 * Returns 0 on successfully attached PM domain or negative error code.
2171 */
2172int genpd_dev_pm_attach(struct device *dev)
2173{
2174 struct of_phandle_args pd_args;
2175 struct generic_pm_domain *pd;
2176 int ret;
2177
2178 if (!dev->of_node)
2179 return -ENODEV;
2180
2181 if (dev->pm_domain)
2182 return -EEXIST;
2183
2184 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2185 "#power-domain-cells", 0, &pd_args);
2186 if (ret < 0) {
2187 if (ret != -ENOENT)
2188 return ret;
2189
2190 /*
2191 * Try legacy Samsung-specific bindings
2192 * (for backwards compatibility of DT ABI)
2193 */
2194 pd_args.args_count = 0;
2195 pd_args.np = of_parse_phandle(dev->of_node,
2196 "samsung,power-domain", 0);
2197 if (!pd_args.np)
2198 return -ENOENT;
2199 }
2200
2201 pd = of_genpd_get_from_provider(&pd_args);
2202 if (IS_ERR(pd)) {
2203 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2204 __func__, PTR_ERR(pd));
2205 of_node_put(dev->of_node);
2206 return PTR_ERR(pd);
2207 }
2208
2209 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2210
2211 while (1) {
2212 ret = pm_genpd_add_device(pd, dev);
2213 if (ret != -EAGAIN)
2214 break;
2215 cond_resched();
2216 }
2217
2218 if (ret < 0) {
2219 dev_err(dev, "failed to add to PM domain %s: %d",
2220 pd->name, ret);
2221 of_node_put(dev->of_node);
2222 return ret;
2223 }
2224
2225 dev->pm_domain->detach = genpd_dev_pm_detach;
Ulf Hansson2ed12762014-11-17 16:19:10 +01002226 pm_genpd_poweron(pd);
Tomasz Figaaa422402014-09-19 20:27:36 +02002227
2228 return 0;
2229}
2230EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01002231#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002232
2233
2234/*** debugfs support ***/
2235
2236#ifdef CONFIG_PM_ADVANCED_DEBUG
2237#include <linux/pm.h>
2238#include <linux/device.h>
2239#include <linux/debugfs.h>
2240#include <linux/seq_file.h>
2241#include <linux/init.h>
2242#include <linux/kobject.h>
2243static struct dentry *pm_genpd_debugfs_dir;
2244
2245/*
2246 * TODO: This function is a slightly modified version of rtpm_status_show
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01002247 * from sysfs.c, so generalize it.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002248 */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002249static void rtpm_status_str(struct seq_file *s, struct device *dev)
2250{
2251 static const char * const status_lookup[] = {
2252 [RPM_ACTIVE] = "active",
2253 [RPM_RESUMING] = "resuming",
2254 [RPM_SUSPENDED] = "suspended",
2255 [RPM_SUSPENDING] = "suspending"
2256 };
2257 const char *p = "";
2258
2259 if (dev->power.runtime_error)
2260 p = "error";
2261 else if (dev->power.disable_depth)
2262 p = "unsupported";
2263 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2264 p = status_lookup[dev->power.runtime_status];
2265 else
2266 WARN_ON(1);
2267
2268 seq_puts(s, p);
2269}
Maciej Matraszek2bd53062014-09-15 13:09:10 +02002270
2271static int pm_genpd_summary_one(struct seq_file *s,
2272 struct generic_pm_domain *gpd)
2273{
2274 static const char * const status_lookup[] = {
2275 [GPD_STATE_ACTIVE] = "on",
2276 [GPD_STATE_WAIT_MASTER] = "wait-master",
2277 [GPD_STATE_BUSY] = "busy",
2278 [GPD_STATE_REPEAT] = "off-in-progress",
2279 [GPD_STATE_POWER_OFF] = "off"
2280 };
2281 struct pm_domain_data *pm_data;
2282 const char *kobj_path;
2283 struct gpd_link *link;
2284 int ret;
2285
2286 ret = mutex_lock_interruptible(&gpd->lock);
2287 if (ret)
2288 return -ERESTARTSYS;
2289
2290 if (WARN_ON(gpd->status >= ARRAY_SIZE(status_lookup)))
2291 goto exit;
2292 seq_printf(s, "%-30s %-15s ", gpd->name, status_lookup[gpd->status]);
2293
2294 /*
2295 * Modifications on the list require holding locks on both
2296 * master and slave, so we are safe.
2297 * Also gpd->name is immutable.
2298 */
2299 list_for_each_entry(link, &gpd->master_links, master_node) {
2300 seq_printf(s, "%s", link->slave->name);
2301 if (!list_is_last(&link->master_node, &gpd->master_links))
2302 seq_puts(s, ", ");
2303 }
2304
2305 list_for_each_entry(pm_data, &gpd->dev_list, list_node) {
2306 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
2307 if (kobj_path == NULL)
2308 continue;
2309
2310 seq_printf(s, "\n %-50s ", kobj_path);
2311 rtpm_status_str(s, pm_data->dev);
2312 kfree(kobj_path);
2313 }
2314
2315 seq_puts(s, "\n");
2316exit:
2317 mutex_unlock(&gpd->lock);
2318
2319 return 0;
2320}
2321
2322static int pm_genpd_summary_show(struct seq_file *s, void *data)
2323{
2324 struct generic_pm_domain *gpd;
2325 int ret = 0;
2326
2327 seq_puts(s, " domain status slaves\n");
2328 seq_puts(s, " /device runtime status\n");
2329 seq_puts(s, "----------------------------------------------------------------------\n");
2330
2331 ret = mutex_lock_interruptible(&gpd_list_lock);
2332 if (ret)
2333 return -ERESTARTSYS;
2334
2335 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2336 ret = pm_genpd_summary_one(s, gpd);
2337 if (ret)
2338 break;
2339 }
2340 mutex_unlock(&gpd_list_lock);
2341
2342 return ret;
2343}
2344
2345static int pm_genpd_summary_open(struct inode *inode, struct file *file)
2346{
2347 return single_open(file, pm_genpd_summary_show, NULL);
2348}
2349
2350static const struct file_operations pm_genpd_summary_fops = {
2351 .open = pm_genpd_summary_open,
2352 .read = seq_read,
2353 .llseek = seq_lseek,
2354 .release = single_release,
2355};
2356
2357static int __init pm_genpd_debug_init(void)
2358{
2359 struct dentry *d;
2360
2361 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
2362
2363 if (!pm_genpd_debugfs_dir)
2364 return -ENOMEM;
2365
2366 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
2367 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
2368 if (!d)
2369 return -ENOMEM;
2370
2371 return 0;
2372}
2373late_initcall(pm_genpd_debug_init);
2374
2375static void __exit pm_genpd_debug_exit(void)
2376{
2377 debugfs_remove_recursive(pm_genpd_debugfs_dir);
2378}
2379__exitcall(pm_genpd_debug_exit);
2380#endif /* CONFIG_PM_ADVANCED_DEBUG */