blob: 7072404c8b6da6ddb7ba6633cab5da087e49868e [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
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/io.h>
12#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>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020015#include <linux/slab.h>
16#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020017#include <linux/sched.h>
18#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010019#include <linux/export.h>
20
21#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
22({ \
23 type (*__routine)(struct device *__d); \
24 type __ret = (type)0; \
25 \
26 __routine = genpd->dev_ops.callback; \
27 if (__routine) { \
28 __ret = __routine(dev); \
29 } else { \
30 __routine = dev_gpd_data(dev)->ops.callback; \
31 if (__routine) \
32 __ret = __routine(dev); \
33 } \
34 __ret; \
35})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020036
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010037#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name) \
38({ \
39 ktime_t __start = ktime_get(); \
40 type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev); \
41 s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start)); \
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020042 struct gpd_timing_data *__td = &dev_gpd_data(dev)->td; \
43 if (!__retval && __elapsed > __td->field) { \
44 __td->field = __elapsed; \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010045 dev_warn(dev, name " latency exceeded, new value %lld ns\n", \
46 __elapsed); \
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020047 genpd->max_off_time_changed = true; \
48 __td->constraint_changed = true; \
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010049 } \
50 __retval; \
51})
52
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +020053static LIST_HEAD(gpd_list);
54static DEFINE_MUTEX(gpd_list_lock);
55
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +020056static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
57{
58 struct generic_pm_domain *genpd = NULL, *gpd;
59
60 if (IS_ERR_OR_NULL(domain_name))
61 return NULL;
62
63 mutex_lock(&gpd_list_lock);
64 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
65 if (!strcmp(gpd->name, domain_name)) {
66 genpd = gpd;
67 break;
68 }
69 }
70 mutex_unlock(&gpd_list_lock);
71 return genpd;
72}
73
Rafael J. Wysocki52480512011-07-01 22:13:10 +020074#ifdef CONFIG_PM
75
Rafael J. Wysockib02c9992011-12-01 00:02:05 +010076struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020077{
78 if (IS_ERR_OR_NULL(dev->pm_domain))
79 return ERR_PTR(-EINVAL);
80
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020081 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020082}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020083
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010084static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
85{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010086 return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
87 stop_latency_ns, "stop");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010088}
89
90static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
91{
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +010092 return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
93 start_latency_ns, "start");
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010094}
95
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020096static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020097{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020098 bool ret = false;
99
100 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
101 ret = !!atomic_dec_and_test(&genpd->sd_count);
102
103 return ret;
104}
105
106static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
107{
108 atomic_inc(&genpd->sd_count);
109 smp_mb__after_atomic_inc();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200110}
111
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200112static void genpd_acquire_lock(struct generic_pm_domain *genpd)
113{
114 DEFINE_WAIT(wait);
115
116 mutex_lock(&genpd->lock);
117 /*
118 * Wait for the domain to transition into either the active,
119 * or the power off state.
120 */
121 for (;;) {
122 prepare_to_wait(&genpd->status_wait_queue, &wait,
123 TASK_UNINTERRUPTIBLE);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200124 if (genpd->status == GPD_STATE_ACTIVE
125 || genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200126 break;
127 mutex_unlock(&genpd->lock);
128
129 schedule();
130
131 mutex_lock(&genpd->lock);
132 }
133 finish_wait(&genpd->status_wait_queue, &wait);
134}
135
136static void genpd_release_lock(struct generic_pm_domain *genpd)
137{
138 mutex_unlock(&genpd->lock);
139}
140
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200141static void genpd_set_active(struct generic_pm_domain *genpd)
142{
143 if (genpd->resume_count == 0)
144 genpd->status = GPD_STATE_ACTIVE;
145}
146
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200147static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
148{
149 s64 usecs64;
150
151 if (!genpd->cpu_data)
152 return;
153
154 usecs64 = genpd->power_on_latency_ns;
155 do_div(usecs64, NSEC_PER_USEC);
156 usecs64 += genpd->cpu_data->saved_exit_latency;
157 genpd->cpu_data->idle_state->exit_latency = usecs64;
158}
159
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200160/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200161 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200162 * @genpd: PM domain to power up.
163 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200164 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200165 * resume a device belonging to it.
166 */
Sachin Kamat8951ef02012-07-10 21:47:07 +0200167static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200168 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200169{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200170 struct gpd_link *link;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200171 DEFINE_WAIT(wait);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200172 int ret = 0;
173
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200174 /* If the domain's master is being waited for, we have to wait too. */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200175 for (;;) {
176 prepare_to_wait(&genpd->status_wait_queue, &wait,
177 TASK_UNINTERRUPTIBLE);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200178 if (genpd->status != GPD_STATE_WAIT_MASTER)
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200179 break;
180 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200181
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200182 schedule();
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200183
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200184 mutex_lock(&genpd->lock);
185 }
186 finish_wait(&genpd->status_wait_queue, &wait);
187
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200188 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200189 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200190 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200191
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200192 if (genpd->status != GPD_STATE_POWER_OFF) {
193 genpd_set_active(genpd);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200194 return 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200195 }
196
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200197 if (genpd->cpu_data) {
198 cpuidle_pause_and_lock();
199 genpd->cpu_data->idle_state->disabled = true;
200 cpuidle_resume_and_unlock();
201 goto out;
202 }
203
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200204 /*
205 * The list is guaranteed not to change while the loop below is being
206 * executed, unless one of the masters' .power_on() callbacks fiddles
207 * with it.
208 */
209 list_for_each_entry(link, &genpd->slave_links, slave_node) {
210 genpd_sd_counter_inc(link->master);
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200211 genpd->status = GPD_STATE_WAIT_MASTER;
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200212
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200213 mutex_unlock(&genpd->lock);
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200214
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200215 ret = pm_genpd_poweron(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200216
217 mutex_lock(&genpd->lock);
218
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200219 /*
220 * The "wait for parent" status is guaranteed not to change
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200221 * while the master is powering on.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200222 */
223 genpd->status = GPD_STATE_POWER_OFF;
224 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200225 if (ret) {
226 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200227 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200228 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200229 }
230
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200231 if (genpd->power_on) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100232 ktime_t time_start = ktime_get();
233 s64 elapsed_ns;
234
Rafael J. Wysockife202fd2011-08-05 21:45:11 +0200235 ret = genpd->power_on(genpd);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200236 if (ret)
237 goto err;
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100238
239 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100240 if (elapsed_ns > genpd->power_on_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100241 genpd->power_on_latency_ns = elapsed_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200242 genpd->max_off_time_changed = true;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200243 genpd_recalc_cpu_exit_latency(genpd);
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100244 if (genpd->name)
245 pr_warning("%s: Power-on latency exceeded, "
246 "new value %lld ns\n", genpd->name,
247 elapsed_ns);
248 }
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200249 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200250
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200251 out:
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200252 genpd_set_active(genpd);
253
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200254 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200255
256 err:
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200257 list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
258 genpd_sd_counter_dec(link->master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200259
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200260 return ret;
261}
262
263/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200264 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200265 * @genpd: PM domain to power up.
266 */
267int pm_genpd_poweron(struct generic_pm_domain *genpd)
268{
269 int ret;
270
271 mutex_lock(&genpd->lock);
272 ret = __pm_genpd_poweron(genpd);
273 mutex_unlock(&genpd->lock);
274 return ret;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200275}
276
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +0200277/**
278 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
279 * @domain_name: Name of the PM domain to power up.
280 */
281int pm_genpd_name_poweron(const char *domain_name)
282{
283 struct generic_pm_domain *genpd;
284
285 genpd = pm_genpd_lookup_name(domain_name);
286 return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
287}
288
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200289#endif /* CONFIG_PM */
290
291#ifdef CONFIG_PM_RUNTIME
292
Sachin Kamatb3d3b9f2012-09-06 08:18:57 +0000293static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
294 struct device *dev)
295{
296 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
297}
298
Rafael J. Wysocki8e9afaf2012-07-11 22:42:52 +0200299static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
300{
301 return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
302 save_state_latency_ns, "state save");
303}
304
305static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
306{
307 return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
308 restore_state_latency_ns,
309 "state restore");
310}
311
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200312static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
313 unsigned long val, void *ptr)
314{
315 struct generic_pm_domain_data *gpd_data;
316 struct device *dev;
317
318 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
319
320 mutex_lock(&gpd_data->lock);
321 dev = gpd_data->base.dev;
322 if (!dev) {
323 mutex_unlock(&gpd_data->lock);
324 return NOTIFY_DONE;
325 }
326 mutex_unlock(&gpd_data->lock);
327
328 for (;;) {
329 struct generic_pm_domain *genpd;
330 struct pm_domain_data *pdd;
331
332 spin_lock_irq(&dev->power.lock);
333
334 pdd = dev->power.subsys_data ?
335 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200336 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200337 to_gpd_data(pdd)->td.constraint_changed = true;
338 genpd = dev_to_genpd(dev);
339 } else {
340 genpd = ERR_PTR(-ENODATA);
341 }
342
343 spin_unlock_irq(&dev->power.lock);
344
345 if (!IS_ERR(genpd)) {
346 mutex_lock(&genpd->lock);
347 genpd->max_off_time_changed = true;
348 mutex_unlock(&genpd->lock);
349 }
350
351 dev = dev->parent;
352 if (!dev || dev->power.ignore_children)
353 break;
354 }
355
356 return NOTIFY_DONE;
357}
358
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200359/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200360 * __pm_genpd_save_device - Save the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200361 * @pdd: Domain data of the device to save the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200362 * @genpd: PM domain the device belongs to.
363 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200364static int __pm_genpd_save_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200365 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200366 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200367{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200368 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200369 struct device *dev = pdd->dev;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200370 int ret = 0;
371
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200372 if (gpd_data->need_restore)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200373 return 0;
374
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200375 mutex_unlock(&genpd->lock);
376
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100377 genpd_start_dev(genpd, dev);
378 ret = genpd_save_dev(genpd, dev);
379 genpd_stop_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200380
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200381 mutex_lock(&genpd->lock);
382
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200383 if (!ret)
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200384 gpd_data->need_restore = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200385
386 return ret;
387}
388
389/**
390 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200391 * @pdd: Domain data of the device to restore the state of.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200392 * @genpd: PM domain the device belongs to.
393 */
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200394static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200395 struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200396 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200397{
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200398 struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200399 struct device *dev = pdd->dev;
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200400 bool need_restore = gpd_data->need_restore;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200401
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200402 gpd_data->need_restore = false;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200403 mutex_unlock(&genpd->lock);
404
Rafael J. Wysockiecf00472011-11-27 13:11:44 +0100405 genpd_start_dev(genpd, dev);
Rafael J. Wysocki80de3d72012-06-16 00:02:34 +0200406 if (need_restore)
407 genpd_restore_dev(genpd, dev);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200408
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200409 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200410}
411
412/**
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200413 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
414 * @genpd: PM domain to check.
415 *
416 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
417 * a "power off" operation, which means that a "power on" has occured in the
418 * meantime, or if its resume_count field is different from zero, which means
419 * that one of its devices has been resumed in the meantime.
420 */
421static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
422{
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200423 return genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200424 || genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200425}
426
427/**
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200428 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
429 * @genpd: PM domait to power off.
430 *
431 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
432 * before.
433 */
Rafael J. Wysocki0bc5b2d2011-07-14 20:59:07 +0200434void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200435{
Tejun Heoa4ca26a2013-01-11 13:37:23 +0100436 queue_work(pm_wq, &genpd->power_off_work);
Rafael J. Wysocki56375fd2011-07-12 00:40:03 +0200437}
438
439/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200440 * pm_genpd_poweroff - Remove power from a given PM domain.
441 * @genpd: PM domain to power down.
442 *
443 * If all of the @genpd's devices have been suspended and all of its subdomains
444 * have been powered down, run the runtime suspend callbacks provided by all of
445 * the @genpd's devices' drivers and remove power from @genpd.
446 */
447static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200448 __releases(&genpd->lock) __acquires(&genpd->lock)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200449{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200450 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200451 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200452 unsigned int not_suspended;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200453 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200454
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200455 start:
456 /*
457 * Do not try to power off the domain in the following situations:
458 * (1) The domain is already in the "power off" state.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200459 * (2) The domain is waiting for its master to power up.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200460 * (3) One of the domain's devices is being resumed right now.
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200461 * (4) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200462 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200463 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysocki17877eb2011-08-08 23:43:50 +0200464 || genpd->status == GPD_STATE_WAIT_MASTER
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200465 || genpd->resume_count > 0 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200466 return 0;
467
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200468 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200469 return -EBUSY;
470
471 not_suspended = 0;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200472 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
473 enum pm_qos_flags_status stat;
474
475 stat = dev_pm_qos_flags(pdd->dev,
476 PM_QOS_FLAG_NO_POWER_OFF
477 | PM_QOS_FLAG_REMOTE_WAKEUP);
478 if (stat > PM_QOS_FLAGS_NONE)
479 return -EBUSY;
480
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200481 if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
Rafael J. Wysockifeb70af2012-08-13 14:00:25 +0200482 || pdd->dev->power.irq_safe))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200483 not_suspended++;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200484 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200485
486 if (not_suspended > genpd->in_progress)
487 return -EBUSY;
488
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200489 if (genpd->poweroff_task) {
490 /*
491 * Another instance of pm_genpd_poweroff() is executing
492 * callbacks, so tell it to start over and return.
493 */
494 genpd->status = GPD_STATE_REPEAT;
495 return 0;
496 }
497
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200498 if (genpd->gov && genpd->gov->power_down_ok) {
499 if (!genpd->gov->power_down_ok(&genpd->domain))
500 return -EAGAIN;
501 }
502
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200503 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200504 genpd->poweroff_task = current;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200505
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200506 list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200507 ret = atomic_read(&genpd->sd_count) == 0 ?
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200508 __pm_genpd_save_device(pdd, genpd) : -EBUSY;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200509
510 if (genpd_abort_poweroff(genpd))
511 goto out;
512
Rafael J. Wysocki697a7f32011-07-12 00:39:48 +0200513 if (ret) {
514 genpd_set_active(genpd);
515 goto out;
516 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200517
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200518 if (genpd->status == GPD_STATE_REPEAT) {
519 genpd->poweroff_task = NULL;
520 goto start;
521 }
522 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200523
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200524 if (genpd->cpu_data) {
525 /*
526 * If cpu_data is set, cpuidle should turn the domain off when
527 * the CPU in it is idle. In that case we don't decrement the
528 * subdomain counts of the master domains, so that power is not
529 * removed from the current domain prematurely as a result of
530 * cutting off the masters' power.
531 */
532 genpd->status = GPD_STATE_POWER_OFF;
533 cpuidle_pause_and_lock();
534 genpd->cpu_data->idle_state->disabled = false;
535 cpuidle_resume_and_unlock();
536 goto out;
537 }
538
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200539 if (genpd->power_off) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100540 ktime_t time_start;
541 s64 elapsed_ns;
542
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200543 if (atomic_read(&genpd->sd_count) > 0) {
544 ret = -EBUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200545 goto out;
546 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200547
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100548 time_start = ktime_get();
549
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200550 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200551 * If sd_count > 0 at this point, one of the subdomains hasn't
552 * managed to call pm_genpd_poweron() for the master yet after
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200553 * incrementing it. In that case pm_genpd_poweron() will wait
554 * for us to drop the lock, so we can call .power_off() and let
555 * the pm_genpd_poweron() restore power for us (this shouldn't
556 * happen very often).
557 */
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200558 ret = genpd->power_off(genpd);
559 if (ret == -EBUSY) {
560 genpd_set_active(genpd);
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200561 goto out;
562 }
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100563
564 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100565 if (elapsed_ns > genpd->power_off_latency_ns) {
Rafael J. Wysocki0140d8b2011-12-01 00:02:17 +0100566 genpd->power_off_latency_ns = elapsed_ns;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200567 genpd->max_off_time_changed = true;
Rafael J. Wysockie84b2c22011-12-06 22:19:54 +0100568 if (genpd->name)
569 pr_warning("%s: Power-off latency exceeded, "
570 "new value %lld ns\n", genpd->name,
571 elapsed_ns);
572 }
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200573 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200574
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200575 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100576
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200577 list_for_each_entry(link, &genpd->slave_links, slave_node) {
578 genpd_sd_counter_dec(link->master);
579 genpd_queue_power_off_work(link->master);
580 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200581
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200582 out:
583 genpd->poweroff_task = NULL;
584 wake_up_all(&genpd->status_wait_queue);
585 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200586}
587
588/**
589 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
590 * @work: Work structure used for scheduling the execution of this function.
591 */
592static void genpd_power_off_work_fn(struct work_struct *work)
593{
594 struct generic_pm_domain *genpd;
595
596 genpd = container_of(work, struct generic_pm_domain, power_off_work);
597
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200598 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200599 pm_genpd_poweroff(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200600 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200601}
602
603/**
604 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
605 * @dev: Device to suspend.
606 *
607 * Carry out a runtime suspend of a device under the assumption that its
608 * pm_domain field points to the domain member of an object of type
609 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
610 */
611static int pm_genpd_runtime_suspend(struct device *dev)
612{
613 struct generic_pm_domain *genpd;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100614 bool (*stop_ok)(struct device *__dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100615 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200616
617 dev_dbg(dev, "%s()\n", __func__);
618
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200619 genpd = dev_to_genpd(dev);
620 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200621 return -EINVAL;
622
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200623 might_sleep_if(!genpd->dev_irq_safe);
624
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100625 stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
626 if (stop_ok && !stop_ok(dev))
627 return -EBUSY;
628
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100629 ret = genpd_stop_dev(genpd, dev);
630 if (ret)
631 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200632
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200633 /*
634 * If power.irq_safe is set, this routine will be run with interrupts
635 * off, so it can't use mutexes.
636 */
637 if (dev->power.irq_safe)
638 return 0;
639
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200640 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200641 genpd->in_progress++;
642 pm_genpd_poweroff(genpd);
643 genpd->in_progress--;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200644 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200645
646 return 0;
647}
648
649/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200650 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
651 * @dev: Device to resume.
652 *
653 * Carry out a runtime resume of a device under the assumption that its
654 * pm_domain field points to the domain member of an object of type
655 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
656 */
657static int pm_genpd_runtime_resume(struct device *dev)
658{
659 struct generic_pm_domain *genpd;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200660 DEFINE_WAIT(wait);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200661 int ret;
662
663 dev_dbg(dev, "%s()\n", __func__);
664
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200665 genpd = dev_to_genpd(dev);
666 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200667 return -EINVAL;
668
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200669 might_sleep_if(!genpd->dev_irq_safe);
670
671 /* If power.irq_safe, the PM domain is never powered off. */
672 if (dev->power.irq_safe)
Rafael J. Wysockie2e3e4e52012-08-06 01:47:29 +0200673 return genpd_start_dev_no_timing(genpd, dev);
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200674
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200675 mutex_lock(&genpd->lock);
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200676 ret = __pm_genpd_poweron(genpd);
677 if (ret) {
678 mutex_unlock(&genpd->lock);
679 return ret;
680 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200681 genpd->status = GPD_STATE_BUSY;
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200682 genpd->resume_count++;
683 for (;;) {
684 prepare_to_wait(&genpd->status_wait_queue, &wait,
685 TASK_UNINTERRUPTIBLE);
686 /*
687 * If current is the powering off task, we have been called
688 * reentrantly from one of the device callbacks, so we should
689 * not wait.
690 */
691 if (!genpd->poweroff_task || genpd->poweroff_task == current)
692 break;
693 mutex_unlock(&genpd->lock);
694
695 schedule();
696
697 mutex_lock(&genpd->lock);
698 }
699 finish_wait(&genpd->status_wait_queue, &wait);
Rafael J. Wysockicd0ea672011-09-26 20:22:02 +0200700 __pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200701 genpd->resume_count--;
702 genpd_set_active(genpd);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200703 wake_up_all(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200704 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200705
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200706 return 0;
707}
708
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200709/**
710 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
711 */
712void pm_genpd_poweroff_unused(void)
713{
714 struct generic_pm_domain *genpd;
715
716 mutex_lock(&gpd_list_lock);
717
718 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
719 genpd_queue_power_off_work(genpd);
720
721 mutex_unlock(&gpd_list_lock);
722}
723
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200724#else
725
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200726static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
727 unsigned long val, void *ptr)
728{
729 return NOTIFY_DONE;
730}
731
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200732static inline void genpd_power_off_work_fn(struct work_struct *work) {}
733
734#define pm_genpd_runtime_suspend NULL
735#define pm_genpd_runtime_resume NULL
736
737#endif /* CONFIG_PM_RUNTIME */
738
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200739#ifdef CONFIG_PM_SLEEP
740
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200741/**
742 * pm_genpd_present - Check if the given PM domain has been initialized.
743 * @genpd: PM domain to check.
744 */
745static bool pm_genpd_present(struct generic_pm_domain *genpd)
746{
747 struct generic_pm_domain *gpd;
748
749 if (IS_ERR_OR_NULL(genpd))
750 return false;
751
752 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
753 if (gpd == genpd)
754 return true;
755
756 return false;
757}
758
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100759static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
760 struct device *dev)
761{
762 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
763}
764
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100765static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
766{
767 return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
768}
769
770static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
771{
772 return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
773}
774
775static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
776{
777 return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
778}
779
780static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
781{
782 return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
783}
784
785static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
786{
787 return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
788}
789
790static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
791{
792 return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
793}
794
795static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
796{
797 return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
798}
799
800static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
801{
802 return GENPD_DEV_CALLBACK(genpd, int, thaw, 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
828 if (genpd->power_off)
829 genpd->power_off(genpd);
830
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200831 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200832
833 list_for_each_entry(link, &genpd->slave_links, slave_node) {
834 genpd_sd_counter_dec(link->master);
835 pm_genpd_sync_poweroff(link->master);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200836 }
837}
838
839/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200840 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
841 * @genpd: PM domain to power on.
842 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200843 * This function is only called in "noirq" and "syscore" stages of system power
844 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
845 * executed sequentially, so it is guaranteed that it will never run twice in
846 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200847 */
848static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
849{
850 struct gpd_link *link;
851
852 if (genpd->status != GPD_STATE_POWER_OFF)
853 return;
854
855 list_for_each_entry(link, &genpd->slave_links, slave_node) {
856 pm_genpd_sync_poweron(link->master);
857 genpd_sd_counter_inc(link->master);
858 }
859
860 if (genpd->power_on)
861 genpd->power_on(genpd);
862
863 genpd->status = GPD_STATE_ACTIVE;
864}
865
866/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200867 * resume_needed - Check whether to resume a device before system suspend.
868 * @dev: Device to check.
869 * @genpd: PM domain the device belongs to.
870 *
871 * There are two cases in which a device that can wake up the system from sleep
872 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
873 * to wake up the system and it has to remain active for this purpose while the
874 * system is in the sleep state and (2) if the device is not enabled to wake up
875 * the system from sleep states and it generally doesn't generate wakeup signals
876 * by itself (those signals are generated on its behalf by other parts of the
877 * system). In the latter case it may be necessary to reconfigure the device's
878 * wakeup settings during system suspend, because it may have been set up to
879 * signal remote wakeup from the system's working state as needed by runtime PM.
880 * Return 'true' in either of the above cases.
881 */
882static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
883{
884 bool active_wakeup;
885
886 if (!device_can_wakeup(dev))
887 return false;
888
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100889 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200890 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
891}
892
893/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200894 * pm_genpd_prepare - Start power transition of a device in a PM domain.
895 * @dev: Device to start the transition of.
896 *
897 * Start a power transition of a device (during a system-wide power transition)
898 * under the assumption that its pm_domain field points to the domain member of
899 * an object of type struct generic_pm_domain representing a PM domain
900 * consisting of I/O devices.
901 */
902static int pm_genpd_prepare(struct device *dev)
903{
904 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200905 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200906
907 dev_dbg(dev, "%s()\n", __func__);
908
909 genpd = dev_to_genpd(dev);
910 if (IS_ERR(genpd))
911 return -EINVAL;
912
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200913 /*
914 * If a wakeup request is pending for the device, it should be woken up
915 * at this point and a system wakeup event should be reported if it's
916 * set up to wake up the system from sleep states.
917 */
918 pm_runtime_get_noresume(dev);
919 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
920 pm_wakeup_event(dev, 0);
921
922 if (pm_wakeup_pending()) {
Ulf Hansson84167032013-04-12 09:41:44 +0000923 pm_runtime_put(dev);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200924 return -EBUSY;
925 }
926
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200927 if (resume_needed(dev, genpd))
928 pm_runtime_resume(dev);
929
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200930 genpd_acquire_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200931
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100932 if (genpd->prepared_count++ == 0) {
933 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200934 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100935 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200936
937 genpd_release_lock(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200938
939 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200940 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200941 return 0;
942 }
943
944 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200945 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
946 * so pm_genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100947 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200948 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200949 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200950 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200951 __pm_runtime_disable(dev, false);
952
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200953 ret = pm_generic_prepare(dev);
954 if (ret) {
955 mutex_lock(&genpd->lock);
956
957 if (--genpd->prepared_count == 0)
958 genpd->suspend_power_off = false;
959
960 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200961 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200962 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200963
Ulf Hansson84167032013-04-12 09:41:44 +0000964 pm_runtime_put(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200965 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200966}
967
968/**
969 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
970 * @dev: Device to suspend.
971 *
972 * Suspend a device under the assumption that its pm_domain field points to the
973 * domain member of an object of type struct generic_pm_domain representing
974 * a PM domain consisting of I/O devices.
975 */
976static int pm_genpd_suspend(struct device *dev)
977{
978 struct generic_pm_domain *genpd;
979
980 dev_dbg(dev, "%s()\n", __func__);
981
982 genpd = dev_to_genpd(dev);
983 if (IS_ERR(genpd))
984 return -EINVAL;
985
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +0100986 return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200987}
988
989/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100990 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200991 * @dev: Device to suspend.
992 *
993 * Carry out a late suspend of a device under the assumption that its
994 * pm_domain field points to the domain member of an object of type
995 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
996 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100997static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200998{
999 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001000
1001 dev_dbg(dev, "%s()\n", __func__);
1002
1003 genpd = dev_to_genpd(dev);
1004 if (IS_ERR(genpd))
1005 return -EINVAL;
1006
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001007 return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1008}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001009
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001010/**
1011 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1012 * @dev: Device to suspend.
1013 *
1014 * Stop the device and remove power from the domain if all devices in it have
1015 * been stopped.
1016 */
1017static int pm_genpd_suspend_noirq(struct device *dev)
1018{
1019 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001020
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001021 dev_dbg(dev, "%s()\n", __func__);
1022
1023 genpd = dev_to_genpd(dev);
1024 if (IS_ERR(genpd))
1025 return -EINVAL;
1026
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001027 if (genpd->suspend_power_off
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001028 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +02001029 return 0;
1030
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001031 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001032
1033 /*
1034 * Since all of the "noirq" callbacks are executed sequentially, it is
1035 * guaranteed that this function will never run twice in parallel for
1036 * the same PM domain, so it is not necessary to use locking here.
1037 */
1038 genpd->suspended_count++;
1039 pm_genpd_sync_poweroff(genpd);
1040
1041 return 0;
1042}
1043
1044/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001045 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001046 * @dev: Device to resume.
1047 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001048 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001049 */
1050static int pm_genpd_resume_noirq(struct device *dev)
1051{
1052 struct generic_pm_domain *genpd;
1053
1054 dev_dbg(dev, "%s()\n", __func__);
1055
1056 genpd = dev_to_genpd(dev);
1057 if (IS_ERR(genpd))
1058 return -EINVAL;
1059
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001060 if (genpd->suspend_power_off
Rafael J. Wysockicc85b202012-03-13 22:39:31 +01001061 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001062 return 0;
1063
1064 /*
1065 * Since all of the "noirq" callbacks are executed sequentially, it is
1066 * guaranteed that this function will never run twice in parallel for
1067 * the same PM domain, so it is not necessary to use locking here.
1068 */
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001069 pm_genpd_sync_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001070 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001071
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001072 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001073}
1074
1075/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001076 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1077 * @dev: Device to resume.
1078 *
1079 * Carry out an early resume of a device under the assumption that its
1080 * pm_domain field points to the domain member of an object of type
1081 * struct generic_pm_domain representing a power domain consisting of I/O
1082 * devices.
1083 */
1084static int pm_genpd_resume_early(struct device *dev)
1085{
1086 struct generic_pm_domain *genpd;
1087
1088 dev_dbg(dev, "%s()\n", __func__);
1089
1090 genpd = dev_to_genpd(dev);
1091 if (IS_ERR(genpd))
1092 return -EINVAL;
1093
1094 return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1095}
1096
1097/**
1098 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001099 * @dev: Device to resume.
1100 *
1101 * Resume a device under the assumption that its pm_domain field points to the
1102 * domain member of an object of type struct generic_pm_domain representing
1103 * a power domain consisting of I/O devices.
1104 */
1105static int pm_genpd_resume(struct device *dev)
1106{
1107 struct generic_pm_domain *genpd;
1108
1109 dev_dbg(dev, "%s()\n", __func__);
1110
1111 genpd = dev_to_genpd(dev);
1112 if (IS_ERR(genpd))
1113 return -EINVAL;
1114
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001115 return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001116}
1117
1118/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001119 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001120 * @dev: Device to freeze.
1121 *
1122 * Freeze a device under the assumption that its pm_domain field points to the
1123 * domain member of an object of type struct generic_pm_domain representing
1124 * a power domain consisting of I/O devices.
1125 */
1126static int pm_genpd_freeze(struct device *dev)
1127{
1128 struct generic_pm_domain *genpd;
1129
1130 dev_dbg(dev, "%s()\n", __func__);
1131
1132 genpd = dev_to_genpd(dev);
1133 if (IS_ERR(genpd))
1134 return -EINVAL;
1135
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001136 return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001137}
1138
1139/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001140 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1141 * @dev: Device to freeze.
1142 *
1143 * Carry out a late freeze of a device under the assumption that its
1144 * pm_domain field points to the domain member of an object of type
1145 * struct generic_pm_domain representing a power domain consisting of I/O
1146 * devices.
1147 */
1148static int pm_genpd_freeze_late(struct device *dev)
1149{
1150 struct generic_pm_domain *genpd;
1151
1152 dev_dbg(dev, "%s()\n", __func__);
1153
1154 genpd = dev_to_genpd(dev);
1155 if (IS_ERR(genpd))
1156 return -EINVAL;
1157
1158 return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1159}
1160
1161/**
1162 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001163 * @dev: Device to freeze.
1164 *
1165 * Carry out a late freeze of a device under the assumption that its
1166 * pm_domain field points to the domain member of an object of type
1167 * struct generic_pm_domain representing a power domain consisting of I/O
1168 * devices.
1169 */
1170static int pm_genpd_freeze_noirq(struct device *dev)
1171{
1172 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001173
1174 dev_dbg(dev, "%s()\n", __func__);
1175
1176 genpd = dev_to_genpd(dev);
1177 if (IS_ERR(genpd))
1178 return -EINVAL;
1179
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001180 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001181}
1182
1183/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001184 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001185 * @dev: Device to thaw.
1186 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001187 * Start the device, unless power has been removed from the domain already
1188 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001189 */
1190static int pm_genpd_thaw_noirq(struct device *dev)
1191{
1192 struct generic_pm_domain *genpd;
1193
1194 dev_dbg(dev, "%s()\n", __func__);
1195
1196 genpd = dev_to_genpd(dev);
1197 if (IS_ERR(genpd))
1198 return -EINVAL;
1199
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001200 return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001201}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001202
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001203/**
1204 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1205 * @dev: Device to thaw.
1206 *
1207 * Carry out an early thaw of a device under the assumption that its
1208 * pm_domain field points to the domain member of an object of type
1209 * struct generic_pm_domain representing a power domain consisting of I/O
1210 * devices.
1211 */
1212static int pm_genpd_thaw_early(struct device *dev)
1213{
1214 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001215
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001216 dev_dbg(dev, "%s()\n", __func__);
1217
1218 genpd = dev_to_genpd(dev);
1219 if (IS_ERR(genpd))
1220 return -EINVAL;
1221
1222 return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001223}
1224
1225/**
1226 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1227 * @dev: Device to thaw.
1228 *
1229 * Thaw a device under the assumption that its pm_domain field points to the
1230 * domain member of an object of type struct generic_pm_domain representing
1231 * a power domain consisting of I/O devices.
1232 */
1233static int pm_genpd_thaw(struct device *dev)
1234{
1235 struct generic_pm_domain *genpd;
1236
1237 dev_dbg(dev, "%s()\n", __func__);
1238
1239 genpd = dev_to_genpd(dev);
1240 if (IS_ERR(genpd))
1241 return -EINVAL;
1242
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001243 return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001244}
1245
1246/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001247 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001248 * @dev: Device to resume.
1249 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001250 * Make sure the domain will be in the same power state as before the
1251 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001252 */
1253static int pm_genpd_restore_noirq(struct device *dev)
1254{
1255 struct generic_pm_domain *genpd;
1256
1257 dev_dbg(dev, "%s()\n", __func__);
1258
1259 genpd = dev_to_genpd(dev);
1260 if (IS_ERR(genpd))
1261 return -EINVAL;
1262
1263 /*
1264 * Since all of the "noirq" callbacks are executed sequentially, it is
1265 * guaranteed that this function will never run twice in parallel for
1266 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001267 *
1268 * At this point suspended_count == 0 means we are being run for the
1269 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001270 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001271 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001272 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001273 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001274 * so make it appear as powered off to pm_genpd_sync_poweron(),
1275 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001276 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001277 genpd->status = GPD_STATE_POWER_OFF;
1278 if (genpd->suspend_power_off) {
1279 /*
1280 * If the domain was off before the hibernation, make
1281 * sure it will be off going forward.
1282 */
1283 if (genpd->power_off)
1284 genpd->power_off(genpd);
1285
1286 return 0;
1287 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001288 }
1289
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001290 if (genpd->suspend_power_off)
1291 return 0;
1292
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001293 pm_genpd_sync_poweron(genpd);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001294
Rafael J. Wysockidbf37412012-08-06 01:46:39 +02001295 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001296}
1297
1298/**
1299 * pm_genpd_complete - Complete power transition of a device in a power domain.
1300 * @dev: Device to complete the transition of.
1301 *
1302 * Complete a power transition of a device (during a system-wide power
1303 * transition) under the assumption that its pm_domain field points to the
1304 * domain member of an object of type struct generic_pm_domain representing
1305 * a power domain consisting of I/O devices.
1306 */
1307static void pm_genpd_complete(struct device *dev)
1308{
1309 struct generic_pm_domain *genpd;
1310 bool run_complete;
1311
1312 dev_dbg(dev, "%s()\n", __func__);
1313
1314 genpd = dev_to_genpd(dev);
1315 if (IS_ERR(genpd))
1316 return;
1317
1318 mutex_lock(&genpd->lock);
1319
1320 run_complete = !genpd->suspend_power_off;
1321 if (--genpd->prepared_count == 0)
1322 genpd->suspend_power_off = false;
1323
1324 mutex_unlock(&genpd->lock);
1325
1326 if (run_complete) {
1327 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001328 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001329 pm_runtime_enable(dev);
Ulf Hanssonaf939332013-04-12 09:41:06 +00001330 pm_request_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001331 }
1332}
1333
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001334/**
1335 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1336 * @dev: Device that normally is marked as "always on" to switch power for.
1337 *
1338 * This routine may only be called during the system core (syscore) suspend or
1339 * resume phase for devices whose "always on" flags are set.
1340 */
1341void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1342{
1343 struct generic_pm_domain *genpd;
1344
1345 genpd = dev_to_genpd(dev);
1346 if (!pm_genpd_present(genpd))
1347 return;
1348
1349 if (suspend) {
1350 genpd->suspended_count++;
1351 pm_genpd_sync_poweroff(genpd);
1352 } else {
1353 pm_genpd_sync_poweron(genpd);
1354 genpd->suspended_count--;
1355 }
1356}
1357EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1358
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001359#else
1360
1361#define pm_genpd_prepare NULL
1362#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001363#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001364#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001365#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001366#define pm_genpd_resume_noirq NULL
1367#define pm_genpd_resume NULL
1368#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001369#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001370#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001371#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001372#define pm_genpd_thaw_noirq NULL
1373#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001374#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001375#define pm_genpd_complete NULL
1376
1377#endif /* CONFIG_PM_SLEEP */
1378
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001379static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1380{
1381 struct generic_pm_domain_data *gpd_data;
1382
1383 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1384 if (!gpd_data)
1385 return NULL;
1386
1387 mutex_init(&gpd_data->lock);
1388 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1389 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
1390 return gpd_data;
1391}
1392
1393static void __pm_genpd_free_dev_data(struct device *dev,
1394 struct generic_pm_domain_data *gpd_data)
1395{
1396 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1397 kfree(gpd_data);
1398}
1399
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001400/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001401 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001402 * @genpd: PM domain to add the device to.
1403 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001404 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001405 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001406int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1407 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001408{
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001409 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001410 struct pm_domain_data *pdd;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001411 int ret = 0;
1412
1413 dev_dbg(dev, "%s()\n", __func__);
1414
1415 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1416 return -EINVAL;
1417
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001418 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1419 if (!gpd_data_new)
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001420 return -ENOMEM;
1421
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001422 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001423
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001424 if (genpd->prepared_count > 0) {
1425 ret = -EAGAIN;
1426 goto out;
1427 }
1428
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001429 list_for_each_entry(pdd, &genpd->dev_list, list_node)
1430 if (pdd->dev == dev) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001431 ret = -EINVAL;
1432 goto out;
1433 }
1434
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001435 ret = dev_pm_get_subsys_data(dev);
1436 if (ret)
1437 goto out;
1438
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001439 genpd->device_count++;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001440 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001441
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001442 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001443
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001444 dev->pm_domain = &genpd->domain;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001445 if (dev->power.subsys_data->domain_data) {
1446 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1447 } else {
1448 gpd_data = gpd_data_new;
1449 dev->power.subsys_data->domain_data = &gpd_data->base;
1450 }
1451 gpd_data->refcount++;
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001452 if (td)
1453 gpd_data->td = *td;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001454
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001455 spin_unlock_irq(&dev->power.lock);
1456
1457 mutex_lock(&gpd_data->lock);
1458 gpd_data->base.dev = dev;
1459 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1460 gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001461 gpd_data->td.constraint_changed = true;
1462 gpd_data->td.effective_constraint_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001463 mutex_unlock(&gpd_data->lock);
1464
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001465 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001466 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001467
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001468 if (gpd_data != gpd_data_new)
1469 __pm_genpd_free_dev_data(dev, gpd_data_new);
1470
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001471 return ret;
1472}
1473
1474/**
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001475 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1476 * @genpd_node: Device tree node pointer representing a PM domain to which the
1477 * the device is added to.
1478 * @dev: Device to be added.
1479 * @td: Set of PM QoS timing parameters to attach to the device.
1480 */
1481int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1482 struct gpd_timing_data *td)
1483{
1484 struct generic_pm_domain *genpd = NULL, *gpd;
1485
1486 dev_dbg(dev, "%s()\n", __func__);
1487
1488 if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1489 return -EINVAL;
1490
1491 mutex_lock(&gpd_list_lock);
1492 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1493 if (gpd->of_node == genpd_node) {
1494 genpd = gpd;
1495 break;
1496 }
1497 }
1498 mutex_unlock(&gpd_list_lock);
1499
1500 if (!genpd)
1501 return -EINVAL;
1502
1503 return __pm_genpd_add_device(genpd, dev, td);
1504}
1505
Rafael J. Wysockib5abb082012-08-07 01:06:11 +02001506
1507/**
1508 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1509 * @domain_name: Name of the PM domain to add the device to.
1510 * @dev: Device to be added.
1511 * @td: Set of PM QoS timing parameters to attach to the device.
1512 */
1513int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1514 struct gpd_timing_data *td)
1515{
Rafael J. Wysocki8bc02512012-08-07 01:11:14 +02001516 return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
Rafael J. Wysockib5abb082012-08-07 01:06:11 +02001517}
1518
Thomas Abrahamc8aa1302012-01-27 15:22:07 +09001519/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001520 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1521 * @genpd: PM domain to remove the device from.
1522 * @dev: Device to be removed.
1523 */
1524int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1525 struct device *dev)
1526{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001527 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001528 struct pm_domain_data *pdd;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001529 bool remove = false;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001530 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001531
1532 dev_dbg(dev, "%s()\n", __func__);
1533
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001534 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1535 || IS_ERR_OR_NULL(dev->pm_domain)
1536 || pd_to_genpd(dev->pm_domain) != genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001537 return -EINVAL;
1538
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001539 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001540
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001541 if (genpd->prepared_count > 0) {
1542 ret = -EAGAIN;
1543 goto out;
1544 }
1545
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001546 genpd->device_count--;
1547 genpd->max_off_time_changed = true;
1548
1549 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001550
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001551 dev->pm_domain = NULL;
1552 pdd = dev->power.subsys_data->domain_data;
1553 list_del_init(&pdd->list_node);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001554 gpd_data = to_gpd_data(pdd);
1555 if (--gpd_data->refcount == 0) {
1556 dev->power.subsys_data->domain_data = NULL;
1557 remove = true;
1558 }
1559
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001560 spin_unlock_irq(&dev->power.lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001561
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001562 mutex_lock(&gpd_data->lock);
1563 pdd->dev = NULL;
1564 mutex_unlock(&gpd_data->lock);
1565
1566 genpd_release_lock(genpd);
1567
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001568 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001569 if (remove)
1570 __pm_genpd_free_dev_data(dev, gpd_data);
1571
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001572 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001573
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001574 out:
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001575 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001576
1577 return ret;
1578}
1579
1580/**
Rafael J. Wysockica1d72f2012-05-14 21:45:52 +02001581 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1582 * @dev: Device to set/unset the flag for.
1583 * @val: The new value of the device's "need restore" flag.
1584 */
1585void pm_genpd_dev_need_restore(struct device *dev, bool val)
1586{
1587 struct pm_subsys_data *psd;
1588 unsigned long flags;
1589
1590 spin_lock_irqsave(&dev->power.lock, flags);
1591
1592 psd = dev_to_psd(dev);
1593 if (psd && psd->domain_data)
1594 to_gpd_data(psd->domain_data)->need_restore = val;
1595
1596 spin_unlock_irqrestore(&dev->power.lock, flags);
1597}
1598EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1599
1600/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001601 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1602 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001603 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001604 */
1605int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001606 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001607{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001608 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001609 int ret = 0;
1610
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001611 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1612 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001613 return -EINVAL;
1614
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001615 start:
1616 genpd_acquire_lock(genpd);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001617 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001618
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001619 if (subdomain->status != GPD_STATE_POWER_OFF
1620 && subdomain->status != GPD_STATE_ACTIVE) {
1621 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001622 genpd_release_lock(genpd);
1623 goto start;
1624 }
1625
1626 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001627 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001628 ret = -EINVAL;
1629 goto out;
1630 }
1631
Huang Ying4fcac102012-05-07 21:35:45 +02001632 list_for_each_entry(link, &genpd->master_links, master_node) {
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001633 if (link->slave == subdomain && link->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001634 ret = -EINVAL;
1635 goto out;
1636 }
1637 }
1638
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001639 link = kzalloc(sizeof(*link), GFP_KERNEL);
1640 if (!link) {
1641 ret = -ENOMEM;
1642 goto out;
1643 }
1644 link->master = genpd;
1645 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001646 link->slave = subdomain;
1647 list_add_tail(&link->slave_node, &subdomain->slave_links);
1648 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001649 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001650
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001651 out:
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001652 mutex_unlock(&subdomain->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001653 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001654
1655 return ret;
1656}
1657
1658/**
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001659 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1660 * @master_name: Name of the master PM domain to add the subdomain to.
1661 * @subdomain_name: Name of the subdomain to be added.
1662 */
1663int pm_genpd_add_subdomain_names(const char *master_name,
1664 const char *subdomain_name)
1665{
1666 struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1667
1668 if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1669 return -EINVAL;
1670
1671 mutex_lock(&gpd_list_lock);
1672 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1673 if (!master && !strcmp(gpd->name, master_name))
1674 master = gpd;
1675
1676 if (!subdomain && !strcmp(gpd->name, subdomain_name))
1677 subdomain = gpd;
1678
1679 if (master && subdomain)
1680 break;
1681 }
1682 mutex_unlock(&gpd_list_lock);
1683
1684 return pm_genpd_add_subdomain(master, subdomain);
1685}
1686
1687/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001688 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1689 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001690 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001691 */
1692int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001693 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001694{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001695 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001696 int ret = -EINVAL;
1697
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001698 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001699 return -EINVAL;
1700
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001701 start:
1702 genpd_acquire_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001703
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001704 list_for_each_entry(link, &genpd->master_links, master_node) {
1705 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001706 continue;
1707
1708 mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1709
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001710 if (subdomain->status != GPD_STATE_POWER_OFF
1711 && subdomain->status != GPD_STATE_ACTIVE) {
1712 mutex_unlock(&subdomain->lock);
1713 genpd_release_lock(genpd);
1714 goto start;
1715 }
1716
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001717 list_del(&link->master_node);
1718 list_del(&link->slave_node);
1719 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001720 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001721 genpd_sd_counter_dec(genpd);
1722
1723 mutex_unlock(&subdomain->lock);
1724
1725 ret = 0;
1726 break;
1727 }
1728
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001729 genpd_release_lock(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001730
1731 return ret;
1732}
1733
1734/**
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001735 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1736 * @dev: Device to add the callbacks to.
1737 * @ops: Set of callbacks to add.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001738 * @td: Timing data to add to the device along with the callbacks (optional).
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001739 *
1740 * Every call to this routine should be balanced with a call to
1741 * __pm_genpd_remove_callbacks() and they must not be nested.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001742 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001743int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1744 struct gpd_timing_data *td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001745{
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001746 struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001747 int ret = 0;
1748
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001749 if (!(dev && ops))
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001750 return -EINVAL;
1751
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001752 gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1753 if (!gpd_data_new)
1754 return -ENOMEM;
1755
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001756 pm_runtime_disable(dev);
1757 device_pm_lock();
1758
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001759 ret = dev_pm_get_subsys_data(dev);
1760 if (ret)
1761 goto out;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001762
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001763 spin_lock_irq(&dev->power.lock);
1764
1765 if (dev->power.subsys_data->domain_data) {
1766 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001767 } else {
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001768 gpd_data = gpd_data_new;
1769 dev->power.subsys_data->domain_data = &gpd_data->base;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001770 }
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001771 gpd_data->refcount++;
1772 gpd_data->ops = *ops;
1773 if (td)
1774 gpd_data->td = *td;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001775
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001776 spin_unlock_irq(&dev->power.lock);
1777
1778 out:
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001779 device_pm_unlock();
1780 pm_runtime_enable(dev);
1781
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001782 if (gpd_data != gpd_data_new)
1783 __pm_genpd_free_dev_data(dev, gpd_data_new);
1784
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001785 return ret;
1786}
1787EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1788
1789/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001790 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001791 * @dev: Device to remove the callbacks from.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001792 * @clear_td: If set, clear the device's timing data too.
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001793 *
1794 * This routine can only be called after pm_genpd_add_callbacks().
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001795 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001796int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001797{
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001798 struct generic_pm_domain_data *gpd_data = NULL;
1799 bool remove = false;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001800 int ret = 0;
1801
1802 if (!(dev && dev->power.subsys_data))
1803 return -EINVAL;
1804
1805 pm_runtime_disable(dev);
1806 device_pm_lock();
1807
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001808 spin_lock_irq(&dev->power.lock);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001809
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001810 if (dev->power.subsys_data->domain_data) {
1811 gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
Sachin Kamatdb79e532012-07-11 12:25:49 +02001812 gpd_data->ops = (struct gpd_dev_ops){ NULL };
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001813 if (clear_td)
1814 gpd_data->td = (struct gpd_timing_data){ 0 };
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001815
1816 if (--gpd_data->refcount == 0) {
1817 dev->power.subsys_data->domain_data = NULL;
1818 remove = true;
1819 }
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001820 } else {
1821 ret = -EINVAL;
1822 }
1823
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001824 spin_unlock_irq(&dev->power.lock);
1825
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001826 device_pm_unlock();
1827 pm_runtime_enable(dev);
1828
Rafael J. Wysocki62d44902012-07-05 22:12:54 +02001829 if (ret)
1830 return ret;
1831
1832 dev_pm_put_subsys_data(dev);
1833 if (remove)
1834 __pm_genpd_free_dev_data(dev, gpd_data);
1835
1836 return 0;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001837}
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001838EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001839
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001840/**
1841 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1842 * @genpd: PM domain to be connected with cpuidle.
1843 * @state: cpuidle state this domain can disable/enable.
1844 *
1845 * Make a PM domain behave as though it contained a CPU core, that is, instead
1846 * of calling its power down routine it will enable the given cpuidle state so
1847 * that the cpuidle subsystem can power it down (if possible and desirable).
1848 */
1849int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001850{
1851 struct cpuidle_driver *cpuidle_drv;
1852 struct gpd_cpu_data *cpu_data;
1853 struct cpuidle_state *idle_state;
1854 int ret = 0;
1855
1856 if (IS_ERR_OR_NULL(genpd) || state < 0)
1857 return -EINVAL;
1858
1859 genpd_acquire_lock(genpd);
1860
1861 if (genpd->cpu_data) {
1862 ret = -EEXIST;
1863 goto out;
1864 }
1865 cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1866 if (!cpu_data) {
1867 ret = -ENOMEM;
1868 goto out;
1869 }
1870 cpuidle_drv = cpuidle_driver_ref();
1871 if (!cpuidle_drv) {
1872 ret = -ENODEV;
jhbird.choi@samsung.comdebe0812012-10-23 00:54:38 +02001873 goto err_drv;
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001874 }
1875 if (cpuidle_drv->state_count <= state) {
1876 ret = -EINVAL;
1877 goto err;
1878 }
1879 idle_state = &cpuidle_drv->states[state];
1880 if (!idle_state->disabled) {
1881 ret = -EAGAIN;
1882 goto err;
1883 }
1884 cpu_data->idle_state = idle_state;
1885 cpu_data->saved_exit_latency = idle_state->exit_latency;
1886 genpd->cpu_data = cpu_data;
1887 genpd_recalc_cpu_exit_latency(genpd);
1888
1889 out:
1890 genpd_release_lock(genpd);
1891 return ret;
1892
1893 err:
1894 cpuidle_driver_unref();
jhbird.choi@samsung.comdebe0812012-10-23 00:54:38 +02001895
1896 err_drv:
1897 kfree(cpu_data);
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001898 goto out;
1899}
1900
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001901/**
Rafael J. Wysocki74a2799a2012-08-15 20:32:59 +02001902 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1903 * @name: Name of the domain to connect to cpuidle.
1904 * @state: cpuidle state this domain can manipulate.
1905 */
1906int pm_genpd_name_attach_cpuidle(const char *name, int state)
1907{
1908 return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
1909}
1910
1911/**
Rafael J. Wysocki40114442012-08-15 20:32:43 +02001912 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1913 * @genpd: PM domain to remove the cpuidle connection from.
1914 *
1915 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1916 * given PM domain.
1917 */
1918int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +02001919{
1920 struct gpd_cpu_data *cpu_data;
1921 struct cpuidle_state *idle_state;
1922 int ret = 0;
1923
1924 if (IS_ERR_OR_NULL(genpd))
1925 return -EINVAL;
1926
1927 genpd_acquire_lock(genpd);
1928
1929 cpu_data = genpd->cpu_data;
1930 if (!cpu_data) {
1931 ret = -ENODEV;
1932 goto out;
1933 }
1934 idle_state = cpu_data->idle_state;
1935 if (!idle_state->disabled) {
1936 ret = -EAGAIN;
1937 goto out;
1938 }
1939 idle_state->exit_latency = cpu_data->saved_exit_latency;
1940 cpuidle_driver_unref();
1941 genpd->cpu_data = NULL;
1942 kfree(cpu_data);
1943
1944 out:
1945 genpd_release_lock(genpd);
1946 return ret;
1947}
1948
Rafael J. Wysocki74a2799a2012-08-15 20:32:59 +02001949/**
1950 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1951 * @name: Name of the domain to disconnect cpuidle from.
1952 */
1953int pm_genpd_name_detach_cpuidle(const char *name)
1954{
1955 return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
1956}
1957
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001958/* Default device callbacks for generic PM domains. */
1959
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +01001960/**
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001961 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1962 * @dev: Device to handle.
1963 */
1964static int pm_genpd_default_save_state(struct device *dev)
1965{
1966 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001967
1968 cb = dev_gpd_data(dev)->ops.save_state;
1969 if (cb)
1970 return cb(dev);
1971
Rafael J. Wysocki0b589742012-06-16 00:02:22 +02001972 if (dev->type && dev->type->pm)
1973 cb = dev->type->pm->runtime_suspend;
1974 else if (dev->class && dev->class->pm)
1975 cb = dev->class->pm->runtime_suspend;
1976 else if (dev->bus && dev->bus->pm)
1977 cb = dev->bus->pm->runtime_suspend;
1978 else
1979 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001980
Rafael J. Wysocki0b589742012-06-16 00:02:22 +02001981 if (!cb && dev->driver && dev->driver->pm)
1982 cb = dev->driver->pm->runtime_suspend;
1983
1984 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001985}
1986
1987/**
1988 * pm_genpd_default_restore_state - Default PM domians "restore device state".
1989 * @dev: Device to handle.
1990 */
1991static int pm_genpd_default_restore_state(struct device *dev)
1992{
1993 int (*cb)(struct device *__dev);
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01001994
1995 cb = dev_gpd_data(dev)->ops.restore_state;
1996 if (cb)
1997 return cb(dev);
1998
Rafael J. Wysocki0b589742012-06-16 00:02:22 +02001999 if (dev->type && dev->type->pm)
2000 cb = dev->type->pm->runtime_resume;
2001 else if (dev->class && dev->class->pm)
2002 cb = dev->class->pm->runtime_resume;
2003 else if (dev->bus && dev->bus->pm)
2004 cb = dev->bus->pm->runtime_resume;
2005 else
2006 cb = NULL;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01002007
Rafael J. Wysocki0b589742012-06-16 00:02:22 +02002008 if (!cb && dev->driver && dev->driver->pm)
2009 cb = dev->driver->pm->runtime_resume;
2010
2011 return cb ? cb(dev) : 0;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01002012}
2013
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01002014#ifdef CONFIG_PM_SLEEP
2015
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01002016/**
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002017 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2018 * @dev: Device to handle.
2019 */
2020static int pm_genpd_default_suspend(struct device *dev)
2021{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01002022 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002023
2024 return cb ? cb(dev) : pm_generic_suspend(dev);
2025}
2026
2027/**
2028 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2029 * @dev: Device to handle.
2030 */
2031static int pm_genpd_default_suspend_late(struct device *dev)
2032{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01002033 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002034
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002035 return cb ? cb(dev) : pm_generic_suspend_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002036}
2037
2038/**
2039 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2040 * @dev: Device to handle.
2041 */
2042static int pm_genpd_default_resume_early(struct device *dev)
2043{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01002044 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002045
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002046 return cb ? cb(dev) : pm_generic_resume_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002047}
2048
2049/**
2050 * pm_genpd_default_resume - Default "device resume" for PM domians.
2051 * @dev: Device to handle.
2052 */
2053static int pm_genpd_default_resume(struct device *dev)
2054{
Rafael J. Wysockic9914852011-12-06 23:16:47 +01002055 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002056
2057 return cb ? cb(dev) : pm_generic_resume(dev);
2058}
2059
2060/**
2061 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2062 * @dev: Device to handle.
2063 */
2064static int pm_genpd_default_freeze(struct device *dev)
2065{
2066 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
2067
2068 return cb ? cb(dev) : pm_generic_freeze(dev);
2069}
2070
2071/**
2072 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2073 * @dev: Device to handle.
2074 */
2075static int pm_genpd_default_freeze_late(struct device *dev)
2076{
2077 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
2078
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002079 return cb ? cb(dev) : pm_generic_freeze_late(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002080}
2081
2082/**
2083 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2084 * @dev: Device to handle.
2085 */
2086static int pm_genpd_default_thaw_early(struct device *dev)
2087{
2088 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
2089
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002090 return cb ? cb(dev) : pm_generic_thaw_early(dev);
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002091}
2092
2093/**
2094 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2095 * @dev: Device to handle.
2096 */
2097static int pm_genpd_default_thaw(struct device *dev)
2098{
2099 int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
2100
2101 return cb ? cb(dev) : pm_generic_thaw(dev);
2102}
2103
Rafael J. Wysocki0f1d6982012-01-14 00:39:25 +01002104#else /* !CONFIG_PM_SLEEP */
2105
2106#define pm_genpd_default_suspend NULL
2107#define pm_genpd_default_suspend_late NULL
2108#define pm_genpd_default_resume_early NULL
2109#define pm_genpd_default_resume NULL
2110#define pm_genpd_default_freeze NULL
2111#define pm_genpd_default_freeze_late NULL
2112#define pm_genpd_default_thaw_early NULL
2113#define pm_genpd_default_thaw NULL
2114
2115#endif /* !CONFIG_PM_SLEEP */
2116
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002117/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002118 * pm_genpd_init - Initialize a generic I/O PM domain object.
2119 * @genpd: PM domain object to initialize.
2120 * @gov: PM domain governor to associate with the domain (may be NULL).
2121 * @is_off: Initial value of the domain's power_is_off field.
2122 */
2123void pm_genpd_init(struct generic_pm_domain *genpd,
2124 struct dev_power_governor *gov, bool is_off)
2125{
2126 if (IS_ERR_OR_NULL(genpd))
2127 return;
2128
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02002129 INIT_LIST_HEAD(&genpd->master_links);
2130 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002131 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002132 mutex_init(&genpd->lock);
2133 genpd->gov = gov;
2134 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2135 genpd->in_progress = 0;
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02002136 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02002137 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2138 init_waitqueue_head(&genpd->status_wait_queue);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +02002139 genpd->poweroff_task = NULL;
2140 genpd->resume_count = 0;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002141 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01002142 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02002143 genpd->max_off_time_changed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02002144 genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2145 genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2146 genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002147 genpd->domain.ops.prepare = pm_genpd_prepare;
2148 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002149 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002150 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2151 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002152 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002153 genpd->domain.ops.resume = pm_genpd_resume;
2154 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002155 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002156 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2157 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002158 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002159 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002160 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002161 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002162 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002163 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01002164 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002165 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02002166 genpd->domain.ops.complete = pm_genpd_complete;
Rafael J. Wysockiecf00472011-11-27 13:11:44 +01002167 genpd->dev_ops.save_state = pm_genpd_default_save_state;
2168 genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
Rafael J. Wysockic9914852011-12-06 23:16:47 +01002169 genpd->dev_ops.suspend = pm_genpd_default_suspend;
2170 genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2171 genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2172 genpd->dev_ops.resume = pm_genpd_default_resume;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01002173 genpd->dev_ops.freeze = pm_genpd_default_freeze;
2174 genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2175 genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2176 genpd->dev_ops.thaw = pm_genpd_default_thaw;
Rafael J. Wysocki5125bbf2011-07-13 12:31:52 +02002177 mutex_lock(&gpd_list_lock);
2178 list_add(&genpd->gpd_list_node, &gpd_list);
2179 mutex_unlock(&gpd_list_lock);
2180}