blob: 4ce4ce0a273003b273b052d8d3ebe6cc94133160 [file] [log] [blame]
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001/*
2 * drivers/base/power/domain.c - Common code related to device power domains.
3 *
4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5 *
6 * This file is released under the GPLv2.
7 */
8
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02009#include <linux/delay.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020010#include <linux/kernel.h>
11#include <linux/io.h>
Tomasz Figaaa422402014-09-19 20:27:36 +020012#include <linux/platform_device.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020013#include <linux/pm_runtime.h>
14#include <linux/pm_domain.h>
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +020015#include <linux/pm_qos.h>
Ulf Hanssonc11f6f52014-12-01 12:50:21 +010016#include <linux/pm_clock.h>
Rafael J. Wysockif7218892011-07-01 22:12:45 +020017#include <linux/slab.h>
18#include <linux/err.h>
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +020019#include <linux/sched.h>
20#include <linux/suspend.h>
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010021#include <linux/export.h>
22
Tomeu Vizosoaa8e54b52016-01-07 16:46:14 +010023#include "power.h"
24
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +020025#define GENPD_RETRY_MAX_MS 250 /* Approximate */
26
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010027#define GENPD_DEV_CALLBACK(genpd, type, callback, dev) \
28({ \
29 type (*__routine)(struct device *__d); \
30 type __ret = (type)0; \
31 \
32 __routine = genpd->dev_ops.callback; \
33 if (__routine) { \
34 __ret = __routine(dev); \
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010035 } \
36 __ret; \
37})
Rafael J. Wysockif7218892011-07-01 22:12:45 +020038
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +020039static LIST_HEAD(gpd_list);
40static DEFINE_MUTEX(gpd_list_lock);
41
Russell King446d999c2015-03-20 17:20:33 +000042/*
43 * Get the generic PM domain for a particular struct device.
44 * This validates the struct device pointer, the PM domain pointer,
45 * and checks that the PM domain pointer is a real generic PM domain.
46 * Any failure results in NULL being returned.
47 */
48struct generic_pm_domain *pm_genpd_lookup_dev(struct device *dev)
49{
50 struct generic_pm_domain *genpd = NULL, *gpd;
51
52 if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
53 return NULL;
54
55 mutex_lock(&gpd_list_lock);
56 list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
57 if (&gpd->domain == dev->pm_domain) {
58 genpd = gpd;
59 break;
60 }
61 }
62 mutex_unlock(&gpd_list_lock);
63
64 return genpd;
65}
66
67/*
68 * This should only be used where we are certain that the pm_domain
69 * attached to the device is a genpd domain.
70 */
71static struct generic_pm_domain *dev_to_genpd(struct device *dev)
Rafael J. Wysocki52480512011-07-01 22:13:10 +020072{
73 if (IS_ERR_OR_NULL(dev->pm_domain))
74 return ERR_PTR(-EINVAL);
75
Rafael J. Wysocki596ba342011-07-01 22:13:19 +020076 return pd_to_genpd(dev->pm_domain);
Rafael J. Wysocki52480512011-07-01 22:13:10 +020077}
Rafael J. Wysockif7218892011-07-01 22:12:45 +020078
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020079static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010080{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020081 return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010082}
83
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020084static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010085{
Ulf Hansson2b1d88c2015-10-15 17:02:19 +020086 return GENPD_DEV_CALLBACK(genpd, int, start, dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +010087}
88
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020089static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
Rafael J. Wysockif7218892011-07-01 22:12:45 +020090{
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +020091 bool ret = false;
92
93 if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
94 ret = !!atomic_dec_and_test(&genpd->sd_count);
95
96 return ret;
97}
98
99static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
100{
101 atomic_inc(&genpd->sd_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100102 smp_mb__after_atomic();
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200103}
104
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200105static int genpd_power_on(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100106{
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100107 unsigned int state_idx = genpd->state_idx;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100108 ktime_t time_start;
109 s64 elapsed_ns;
110 int ret;
111
112 if (!genpd->power_on)
113 return 0;
114
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200115 if (!timed)
116 return genpd->power_on(genpd);
117
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100118 time_start = ktime_get();
119 ret = genpd->power_on(genpd);
120 if (ret)
121 return ret;
122
123 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100124 if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100125 return ret;
126
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100127 genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100128 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000129 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
130 genpd->name, "on", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100131
132 return ret;
133}
134
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200135static int genpd_power_off(struct generic_pm_domain *genpd, bool timed)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100136{
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100137 unsigned int state_idx = genpd->state_idx;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100138 ktime_t time_start;
139 s64 elapsed_ns;
140 int ret;
141
142 if (!genpd->power_off)
143 return 0;
144
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200145 if (!timed)
146 return genpd->power_off(genpd);
147
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100148 time_start = ktime_get();
149 ret = genpd->power_off(genpd);
150 if (ret == -EBUSY)
151 return ret;
152
153 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100154 if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100155 return ret;
156
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100157 genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100158 genpd->max_off_time_changed = true;
Russell King6d7d5c32015-03-20 17:20:28 +0000159 pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
160 genpd->name, "off", elapsed_ns);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100161
162 return ret;
163}
164
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200165/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200166 * genpd_queue_power_off_work - Queue up the execution of genpd_poweroff().
Moritz Fischera3d09c72016-01-27 08:29:27 +0100167 * @genpd: PM domain to power off.
Ulf Hansson29e47e22015-09-02 10:16:13 +0200168 *
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200169 * Queue up the execution of genpd_poweroff() unless it's already been done
Ulf Hansson29e47e22015-09-02 10:16:13 +0200170 * before.
171 */
172static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
173{
174 queue_work(pm_wq, &genpd->power_off_work);
175}
176
177/**
Ulf Hansson53af16f2016-02-01 14:52:41 +0100178 * genpd_poweron - Restore power to a given PM domain and its masters.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200179 * @genpd: PM domain to power up.
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100180 * @depth: nesting count for lockdep.
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200181 *
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200182 * Restore power to @genpd and all of its masters so that it is possible to
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200183 * resume a device belonging to it.
184 */
Ulf Hansson53af16f2016-02-01 14:52:41 +0100185static int genpd_poweron(struct generic_pm_domain *genpd, unsigned int depth)
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200186{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200187 struct gpd_link *link;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200188 int ret = 0;
189
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200190 if (genpd->status == GPD_STATE_ACTIVE
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200191 || (genpd->prepared_count > 0 && genpd->suspend_power_off))
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200192 return 0;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200193
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200194 /*
195 * The list is guaranteed not to change while the loop below is being
196 * executed, unless one of the masters' .power_on() callbacks fiddles
197 * with it.
198 */
199 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100200 struct generic_pm_domain *master = link->master;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200201
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100202 genpd_sd_counter_inc(master);
203
204 mutex_lock_nested(&master->lock, depth + 1);
Ulf Hansson53af16f2016-02-01 14:52:41 +0100205 ret = genpd_poweron(master, depth + 1);
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100206 mutex_unlock(&master->lock);
207
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200208 if (ret) {
Marek Szyprowski0106ef52016-01-20 10:13:42 +0100209 genpd_sd_counter_dec(master);
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200210 goto err;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200211 }
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200212 }
213
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200214 ret = genpd_power_on(genpd, true);
Geert Uytterhoevenc8f0ea42014-11-10 19:39:19 +0100215 if (ret)
216 goto err;
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200217
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200218 genpd->status = GPD_STATE_ACTIVE;
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200219 return 0;
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200220
221 err:
Ulf Hansson29e47e22015-09-02 10:16:13 +0200222 list_for_each_entry_continue_reverse(link,
223 &genpd->slave_links,
224 slave_node) {
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200225 genpd_sd_counter_dec(link->master);
Ulf Hansson29e47e22015-09-02 10:16:13 +0200226 genpd_queue_power_off_work(link->master);
227 }
Rafael J. Wysocki9e08cf42011-08-08 23:43:22 +0200228
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200229 return ret;
230}
231
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200232static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
233 unsigned long val, void *ptr)
234{
235 struct generic_pm_domain_data *gpd_data;
236 struct device *dev;
237
238 gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200239 dev = gpd_data->base.dev;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200240
241 for (;;) {
242 struct generic_pm_domain *genpd;
243 struct pm_domain_data *pdd;
244
245 spin_lock_irq(&dev->power.lock);
246
247 pdd = dev->power.subsys_data ?
248 dev->power.subsys_data->domain_data : NULL;
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +0200249 if (pdd && pdd->dev) {
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +0200250 to_gpd_data(pdd)->td.constraint_changed = true;
251 genpd = dev_to_genpd(dev);
252 } else {
253 genpd = ERR_PTR(-ENODATA);
254 }
255
256 spin_unlock_irq(&dev->power.lock);
257
258 if (!IS_ERR(genpd)) {
259 mutex_lock(&genpd->lock);
260 genpd->max_off_time_changed = true;
261 mutex_unlock(&genpd->lock);
262 }
263
264 dev = dev->parent;
265 if (!dev || dev->power.ignore_children)
266 break;
267 }
268
269 return NOTIFY_DONE;
270}
271
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200272/**
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200273 * genpd_poweroff - Remove power from a given PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200274 * @genpd: PM domain to power down.
Ulf Hanssonf96b3c42015-09-29 15:12:37 +0200275 * @is_async: PM domain is powered down from a scheduled work
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200276 *
277 * If all of the @genpd's devices have been suspended and all of its subdomains
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200278 * have been powered down, remove power from @genpd.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200279 */
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200280static int genpd_poweroff(struct generic_pm_domain *genpd, bool is_async)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200281{
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +0200282 struct pm_domain_data *pdd;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200283 struct gpd_link *link;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200284 unsigned int not_suspended = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200285
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200286 /*
287 * Do not try to power off the domain in the following situations:
288 * (1) The domain is already in the "power off" state.
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200289 * (2) System suspend is in progress.
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200290 */
Rafael J. Wysocki3f241772011-08-08 23:43:29 +0200291 if (genpd->status == GPD_STATE_POWER_OFF
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200292 || genpd->prepared_count > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200293 return 0;
294
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200295 if (atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200296 return -EBUSY;
297
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200298 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
299 enum pm_qos_flags_status stat;
300
301 stat = dev_pm_qos_flags(pdd->dev,
302 PM_QOS_FLAG_NO_POWER_OFF
303 | PM_QOS_FLAG_REMOTE_WAKEUP);
304 if (stat > PM_QOS_FLAGS_NONE)
305 return -EBUSY;
306
Lina Iyer298cd0f2015-10-28 15:18:59 -0600307 if (!pm_runtime_suspended(pdd->dev) || pdd->dev->power.irq_safe)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200308 not_suspended++;
Rafael J. Wysocki34b1f762012-10-24 02:08:30 +0200309 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200310
Ulf Hanssonf96b3c42015-09-29 15:12:37 +0200311 if (not_suspended > 1 || (not_suspended == 1 && is_async))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200312 return -EBUSY;
313
314 if (genpd->gov && genpd->gov->power_down_ok) {
315 if (!genpd->gov->power_down_ok(&genpd->domain))
316 return -EAGAIN;
317 }
318
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200319 if (genpd->power_off) {
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200320 int ret;
321
322 if (atomic_read(&genpd->sd_count) > 0)
323 return -EBUSY;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200324
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200325 /*
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200326 * If sd_count > 0 at this point, one of the subdomains hasn't
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200327 * managed to call genpd_poweron() for the master yet after
328 * incrementing it. In that case genpd_poweron() will wait
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200329 * for us to drop the lock, so we can call .power_off() and let
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200330 * the genpd_poweron() restore power for us (this shouldn't
Rafael J. Wysocki3c07cbc2011-08-08 23:43:14 +0200331 * happen very often).
332 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200333 ret = genpd_power_off(genpd, true);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200334 if (ret)
335 return ret;
Rafael J. Wysockid2805402011-07-14 20:59:20 +0200336 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200337
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200338 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +0100339
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200340 list_for_each_entry(link, &genpd->slave_links, slave_node) {
341 genpd_sd_counter_dec(link->master);
342 genpd_queue_power_off_work(link->master);
343 }
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200344
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200345 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200346}
347
348/**
349 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
350 * @work: Work structure used for scheduling the execution of this function.
351 */
352static void genpd_power_off_work_fn(struct work_struct *work)
353{
354 struct generic_pm_domain *genpd;
355
356 genpd = container_of(work, struct generic_pm_domain, power_off_work);
357
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200358 mutex_lock(&genpd->lock);
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200359 genpd_poweroff(genpd, true);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200360 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200361}
362
363/**
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200364 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
365 * @dev: Device to handle.
366 */
367static int __genpd_runtime_suspend(struct device *dev)
368{
369 int (*cb)(struct device *__dev);
370
371 if (dev->type && dev->type->pm)
372 cb = dev->type->pm->runtime_suspend;
373 else if (dev->class && dev->class->pm)
374 cb = dev->class->pm->runtime_suspend;
375 else if (dev->bus && dev->bus->pm)
376 cb = dev->bus->pm->runtime_suspend;
377 else
378 cb = NULL;
379
380 if (!cb && dev->driver && dev->driver->pm)
381 cb = dev->driver->pm->runtime_suspend;
382
383 return cb ? cb(dev) : 0;
384}
385
386/**
387 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
388 * @dev: Device to handle.
389 */
390static int __genpd_runtime_resume(struct device *dev)
391{
392 int (*cb)(struct device *__dev);
393
394 if (dev->type && dev->type->pm)
395 cb = dev->type->pm->runtime_resume;
396 else if (dev->class && dev->class->pm)
397 cb = dev->class->pm->runtime_resume;
398 else if (dev->bus && dev->bus->pm)
399 cb = dev->bus->pm->runtime_resume;
400 else
401 cb = NULL;
402
403 if (!cb && dev->driver && dev->driver->pm)
404 cb = dev->driver->pm->runtime_resume;
405
406 return cb ? cb(dev) : 0;
407}
408
409/**
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200410 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200411 * @dev: Device to suspend.
412 *
413 * Carry out a runtime suspend of a device under the assumption that its
414 * pm_domain field points to the domain member of an object of type
415 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
416 */
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200417static int genpd_runtime_suspend(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200418{
419 struct generic_pm_domain *genpd;
Ulf Hansson9df39212016-03-31 11:21:25 +0200420 bool (*suspend_ok)(struct device *__dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200421 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100422 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200423 ktime_t time_start;
424 s64 elapsed_ns;
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100425 int ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200426
427 dev_dbg(dev, "%s()\n", __func__);
428
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200429 genpd = dev_to_genpd(dev);
430 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200431 return -EINVAL;
432
Ulf Hanssonffe12852015-11-30 16:21:38 +0100433 /*
434 * A runtime PM centric subsystem/driver may re-use the runtime PM
435 * callbacks for other purposes than runtime PM. In those scenarios
436 * runtime PM is disabled. Under these circumstances, we shall skip
437 * validating/measuring the PM QoS latency.
438 */
Ulf Hansson9df39212016-03-31 11:21:25 +0200439 suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
440 if (runtime_pm && suspend_ok && !suspend_ok(dev))
Rafael J. Wysockib02c9992011-12-01 00:02:05 +0100441 return -EBUSY;
442
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200443 /* Measure suspend latency. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100444 if (runtime_pm)
445 time_start = ktime_get();
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200446
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200447 ret = __genpd_runtime_suspend(dev);
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100448 if (ret)
449 return ret;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200450
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200451 ret = genpd_stop_dev(genpd, dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200452 if (ret) {
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200453 __genpd_runtime_resume(dev);
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200454 return ret;
455 }
456
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200457 /* Update suspend latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100458 if (runtime_pm) {
459 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
460 if (elapsed_ns > td->suspend_latency_ns) {
461 td->suspend_latency_ns = elapsed_ns;
462 dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
463 elapsed_ns);
464 genpd->max_off_time_changed = true;
465 td->constraint_changed = true;
466 }
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200467 }
468
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200469 /*
470 * If power.irq_safe is set, this routine will be run with interrupts
471 * off, so it can't use mutexes.
472 */
473 if (dev->power.irq_safe)
474 return 0;
475
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200476 mutex_lock(&genpd->lock);
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200477 genpd_poweroff(genpd, false);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200478 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200479
480 return 0;
481}
482
483/**
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200484 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200485 * @dev: Device to resume.
486 *
487 * Carry out a runtime resume of a device under the assumption that its
488 * pm_domain field points to the domain member of an object of type
489 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
490 */
Ulf Hansson795bd2e2016-03-31 11:21:26 +0200491static int genpd_runtime_resume(struct device *dev)
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200492{
493 struct generic_pm_domain *genpd;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200494 struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
Ulf Hanssonffe12852015-11-30 16:21:38 +0100495 bool runtime_pm = pm_runtime_enabled(dev);
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200496 ktime_t time_start;
497 s64 elapsed_ns;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200498 int ret;
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200499 bool timed = true;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200500
501 dev_dbg(dev, "%s()\n", __func__);
502
Rafael J. Wysocki52480512011-07-01 22:13:10 +0200503 genpd = dev_to_genpd(dev);
504 if (IS_ERR(genpd))
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200505 return -EINVAL;
506
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200507 /* If power.irq_safe, the PM domain is never powered off. */
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200508 if (dev->power.irq_safe) {
509 timed = false;
510 goto out;
511 }
Rafael J. Wysocki0aa2a222011-08-25 15:37:04 +0200512
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200513 mutex_lock(&genpd->lock);
Ulf Hansson53af16f2016-02-01 14:52:41 +0100514 ret = genpd_poweron(genpd, 0);
Rafael J. Wysockic6d22b32011-07-12 00:39:36 +0200515 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200516
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200517 if (ret)
518 return ret;
519
520 out:
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200521 /* Measure resume latency. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100522 if (timed && runtime_pm)
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200523 time_start = ktime_get();
524
Laurent Pinchart076395c2016-03-02 01:20:38 +0200525 ret = genpd_start_dev(genpd, dev);
526 if (ret)
527 goto err_poweroff;
528
Ulf Hansson54eeddb2016-03-31 11:21:27 +0200529 ret = __genpd_runtime_resume(dev);
Laurent Pinchart076395c2016-03-02 01:20:38 +0200530 if (ret)
531 goto err_stop;
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200532
533 /* Update resume latency value if the measured time exceeds it. */
Ulf Hanssonffe12852015-11-30 16:21:38 +0100534 if (timed && runtime_pm) {
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200535 elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
536 if (elapsed_ns > td->resume_latency_ns) {
537 td->resume_latency_ns = elapsed_ns;
538 dev_dbg(dev, "resume latency exceeded, %lld ns\n",
539 elapsed_ns);
540 genpd->max_off_time_changed = true;
541 td->constraint_changed = true;
542 }
543 }
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200544
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200545 return 0;
Laurent Pinchart076395c2016-03-02 01:20:38 +0200546
547err_stop:
548 genpd_stop_dev(genpd, dev);
549err_poweroff:
550 if (!dev->power.irq_safe) {
551 mutex_lock(&genpd->lock);
552 genpd_poweroff(genpd, 0);
553 mutex_unlock(&genpd->lock);
554 }
555
556 return ret;
Rafael J. Wysockif7218892011-07-01 22:12:45 +0200557}
558
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530559static bool pd_ignore_unused;
560static int __init pd_ignore_unused_setup(char *__unused)
561{
562 pd_ignore_unused = true;
563 return 1;
564}
565__setup("pd_ignore_unused", pd_ignore_unused_setup);
566
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200567/**
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200568 * genpd_poweroff_unused - Power off all PM domains with no devices in use.
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200569 */
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200570static int __init genpd_poweroff_unused(void)
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200571{
572 struct generic_pm_domain *genpd;
573
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530574 if (pd_ignore_unused) {
575 pr_warn("genpd: Not disabling unused power domains\n");
Ulf Hanssonbb4b72f2015-10-06 14:27:42 +0200576 return 0;
Tushar Behera39ac5ba2014-03-28 10:50:21 +0530577 }
578
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200579 mutex_lock(&gpd_list_lock);
580
581 list_for_each_entry(genpd, &gpd_list, gpd_list_node)
582 genpd_queue_power_off_work(genpd);
583
584 mutex_unlock(&gpd_list_lock);
Rafael J. Wysocki17f2ae72011-08-14 13:34:31 +0200585
Ulf Hansson2fe71dc2014-09-03 12:52:26 +0200586 return 0;
587}
588late_initcall(genpd_poweroff_unused);
589
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200590#ifdef CONFIG_PM_SLEEP
591
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200592/**
593 * pm_genpd_present - Check if the given PM domain has been initialized.
594 * @genpd: PM domain to check.
595 */
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100596static bool pm_genpd_present(const struct generic_pm_domain *genpd)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200597{
Geert Uytterhoeven895b31f2014-11-10 19:37:39 +0100598 const struct generic_pm_domain *gpd;
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200599
600 if (IS_ERR_OR_NULL(genpd))
601 return false;
602
603 list_for_each_entry(gpd, &gpd_list, gpd_list_node)
604 if (gpd == genpd)
605 return true;
606
607 return false;
608}
609
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100610static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
611 struct device *dev)
612{
613 return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
614}
615
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200616/**
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200617 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200618 * @genpd: PM domain to power off, if possible.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200619 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200620 *
621 * Check if the given PM domain can be powered off (during system suspend or
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200622 * hibernation) and do that if so. Also, in that case propagate to its masters.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200623 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200624 * This function is only called in "noirq" and "syscore" stages of system power
625 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
626 * executed sequentially, so it is guaranteed that it will never run twice in
627 * parallel).
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200628 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200629static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd,
630 bool timed)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200631{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200632 struct gpd_link *link;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200633
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200634 if (genpd->status == GPD_STATE_POWER_OFF)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200635 return;
636
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +0200637 if (genpd->suspended_count != genpd->device_count
638 || atomic_read(&genpd->sd_count) > 0)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200639 return;
640
Axel Haslamfc5cbf02016-02-15 11:10:51 +0100641 /* Choose the deepest state when suspending */
642 genpd->state_idx = genpd->state_count - 1;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200643 genpd_power_off(genpd, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200644
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200645 genpd->status = GPD_STATE_POWER_OFF;
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +0200646
647 list_for_each_entry(link, &genpd->slave_links, slave_node) {
648 genpd_sd_counter_dec(link->master);
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200649 pm_genpd_sync_poweroff(link->master, timed);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200650 }
651}
652
653/**
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200654 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
655 * @genpd: PM domain to power on.
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200656 * @timed: True if latency measurements are allowed.
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200657 *
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +0200658 * This function is only called in "noirq" and "syscore" stages of system power
659 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
660 * executed sequentially, so it is guaranteed that it will never run twice in
661 * parallel).
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200662 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200663static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd,
664 bool timed)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200665{
666 struct gpd_link *link;
667
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200668 if (genpd->status == GPD_STATE_ACTIVE)
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200669 return;
670
671 list_for_each_entry(link, &genpd->slave_links, slave_node) {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200672 pm_genpd_sync_poweron(link->master, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200673 genpd_sd_counter_inc(link->master);
674 }
675
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200676 genpd_power_on(genpd, timed);
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +0200677
678 genpd->status = GPD_STATE_ACTIVE;
679}
680
681/**
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200682 * resume_needed - Check whether to resume a device before system suspend.
683 * @dev: Device to check.
684 * @genpd: PM domain the device belongs to.
685 *
686 * There are two cases in which a device that can wake up the system from sleep
687 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
688 * to wake up the system and it has to remain active for this purpose while the
689 * system is in the sleep state and (2) if the device is not enabled to wake up
690 * the system from sleep states and it generally doesn't generate wakeup signals
691 * by itself (those signals are generated on its behalf by other parts of the
692 * system). In the latter case it may be necessary to reconfigure the device's
693 * wakeup settings during system suspend, because it may have been set up to
694 * signal remote wakeup from the system's working state as needed by runtime PM.
695 * Return 'true' in either of the above cases.
696 */
697static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
698{
699 bool active_wakeup;
700
701 if (!device_can_wakeup(dev))
702 return false;
703
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100704 active_wakeup = genpd_dev_active_wakeup(genpd, dev);
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200705 return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
706}
707
708/**
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200709 * pm_genpd_prepare - Start power transition of a device in a PM domain.
710 * @dev: Device to start the transition of.
711 *
712 * Start a power transition of a device (during a system-wide power transition)
713 * under the assumption that its pm_domain field points to the domain member of
714 * an object of type struct generic_pm_domain representing a PM domain
715 * consisting of I/O devices.
716 */
717static int pm_genpd_prepare(struct device *dev)
718{
719 struct generic_pm_domain *genpd;
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200720 int ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200721
722 dev_dbg(dev, "%s()\n", __func__);
723
724 genpd = dev_to_genpd(dev);
725 if (IS_ERR(genpd))
726 return -EINVAL;
727
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200728 /*
729 * If a wakeup request is pending for the device, it should be woken up
730 * at this point and a system wakeup event should be reported if it's
731 * set up to wake up the system from sleep states.
732 */
733 pm_runtime_get_noresume(dev);
734 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
735 pm_wakeup_event(dev, 0);
736
737 if (pm_wakeup_pending()) {
Ulf Hansson84167032013-04-12 09:41:44 +0000738 pm_runtime_put(dev);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200739 return -EBUSY;
740 }
741
Rafael J. Wysocki4ecd6e62011-07-12 00:39:57 +0200742 if (resume_needed(dev, genpd))
743 pm_runtime_resume(dev);
744
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200745 mutex_lock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200746
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100747 if (genpd->prepared_count++ == 0) {
748 genpd->suspended_count = 0;
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200749 genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +0100750 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200751
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +0200752 mutex_unlock(&genpd->lock);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200753
754 if (genpd->suspend_power_off) {
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200755 pm_runtime_put_noidle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200756 return 0;
757 }
758
759 /*
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200760 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
Ulf Hansson7420aa4f2015-10-13 09:58:02 +0200761 * so genpd_poweron() will return immediately, but if the device
Rafael J. Wysockid5e4cbf2011-11-27 13:11:36 +0100762 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200763 * to make it operational.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200764 */
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200765 pm_runtime_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200766 __pm_runtime_disable(dev, false);
767
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200768 ret = pm_generic_prepare(dev);
769 if (ret) {
770 mutex_lock(&genpd->lock);
771
772 if (--genpd->prepared_count == 0)
773 genpd->suspend_power_off = false;
774
775 mutex_unlock(&genpd->lock);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200776 pm_runtime_enable(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200777 }
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +0200778
Ulf Hansson84167032013-04-12 09:41:44 +0000779 pm_runtime_put(dev);
Rafael J. Wysockib6c10c82011-07-12 00:39:21 +0200780 return ret;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200781}
782
783/**
784 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
785 * @dev: Device to suspend.
786 *
787 * Suspend a device under the assumption that its pm_domain field points to the
788 * domain member of an object of type struct generic_pm_domain representing
789 * a PM domain consisting of I/O devices.
790 */
791static int pm_genpd_suspend(struct device *dev)
792{
793 struct generic_pm_domain *genpd;
794
795 dev_dbg(dev, "%s()\n", __func__);
796
797 genpd = dev_to_genpd(dev);
798 if (IS_ERR(genpd))
799 return -EINVAL;
800
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200801 return genpd->suspend_power_off ? 0 : pm_generic_suspend(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200802}
803
804/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100805 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200806 * @dev: Device to suspend.
807 *
808 * Carry out a late suspend of a device under the assumption that its
809 * pm_domain field points to the domain member of an object of type
810 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
811 */
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100812static int pm_genpd_suspend_late(struct device *dev)
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200813{
814 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200815
816 dev_dbg(dev, "%s()\n", __func__);
817
818 genpd = dev_to_genpd(dev);
819 if (IS_ERR(genpd))
820 return -EINVAL;
821
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200822 return genpd->suspend_power_off ? 0 : pm_generic_suspend_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100823}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200824
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100825/**
826 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
827 * @dev: Device to suspend.
828 *
829 * Stop the device and remove power from the domain if all devices in it have
830 * been stopped.
831 */
832static int pm_genpd_suspend_noirq(struct device *dev)
833{
834 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200835
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100836 dev_dbg(dev, "%s()\n", __func__);
837
838 genpd = dev_to_genpd(dev);
839 if (IS_ERR(genpd))
840 return -EINVAL;
841
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200842 if (genpd->suspend_power_off
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100843 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysockid4f2d872011-07-01 22:13:29 +0200844 return 0;
845
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200846 genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200847
848 /*
849 * Since all of the "noirq" callbacks are executed sequentially, it is
850 * guaranteed that this function will never run twice in parallel for
851 * the same PM domain, so it is not necessary to use locking here.
852 */
853 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200854 pm_genpd_sync_poweroff(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200855
856 return 0;
857}
858
859/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100860 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200861 * @dev: Device to resume.
862 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100863 * Restore power to the device's PM domain, if necessary, and start the device.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200864 */
865static int pm_genpd_resume_noirq(struct device *dev)
866{
867 struct generic_pm_domain *genpd;
868
869 dev_dbg(dev, "%s()\n", __func__);
870
871 genpd = dev_to_genpd(dev);
872 if (IS_ERR(genpd))
873 return -EINVAL;
874
Rafael J. Wysockidbf37412012-08-06 01:46:39 +0200875 if (genpd->suspend_power_off
Rafael J. Wysockicc85b202012-03-13 22:39:31 +0100876 || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200877 return 0;
878
879 /*
880 * Since all of the "noirq" callbacks are executed sequentially, it is
881 * guaranteed that this function will never run twice in parallel for
882 * the same PM domain, so it is not necessary to use locking here.
883 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +0200884 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200885 genpd->suspended_count--;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200886
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200887 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200888}
889
890/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100891 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
892 * @dev: Device to resume.
893 *
894 * Carry out an early resume of a device under the assumption that its
895 * pm_domain field points to the domain member of an object of type
896 * struct generic_pm_domain representing a power domain consisting of I/O
897 * devices.
898 */
899static int pm_genpd_resume_early(struct device *dev)
900{
901 struct generic_pm_domain *genpd;
902
903 dev_dbg(dev, "%s()\n", __func__);
904
905 genpd = dev_to_genpd(dev);
906 if (IS_ERR(genpd))
907 return -EINVAL;
908
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200909 return genpd->suspend_power_off ? 0 : pm_generic_resume_early(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100910}
911
912/**
913 * pm_genpd_resume - Resume of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200914 * @dev: Device to resume.
915 *
916 * Resume a device under the assumption that its pm_domain field points to the
917 * domain member of an object of type struct generic_pm_domain representing
918 * a power domain consisting of I/O devices.
919 */
920static int pm_genpd_resume(struct device *dev)
921{
922 struct generic_pm_domain *genpd;
923
924 dev_dbg(dev, "%s()\n", __func__);
925
926 genpd = dev_to_genpd(dev);
927 if (IS_ERR(genpd))
928 return -EINVAL;
929
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200930 return genpd->suspend_power_off ? 0 : pm_generic_resume(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200931}
932
933/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100934 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200935 * @dev: Device to freeze.
936 *
937 * Freeze a device under the assumption that its pm_domain field points to the
938 * domain member of an object of type struct generic_pm_domain representing
939 * a power domain consisting of I/O devices.
940 */
941static int pm_genpd_freeze(struct device *dev)
942{
943 struct generic_pm_domain *genpd;
944
945 dev_dbg(dev, "%s()\n", __func__);
946
947 genpd = dev_to_genpd(dev);
948 if (IS_ERR(genpd))
949 return -EINVAL;
950
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200951 return genpd->suspend_power_off ? 0 : pm_generic_freeze(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200952}
953
954/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100955 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
956 * @dev: Device to freeze.
957 *
958 * Carry out a late freeze of a device under the assumption that its
959 * pm_domain field points to the domain member of an object of type
960 * struct generic_pm_domain representing a power domain consisting of I/O
961 * devices.
962 */
963static int pm_genpd_freeze_late(struct device *dev)
964{
965 struct generic_pm_domain *genpd;
966
967 dev_dbg(dev, "%s()\n", __func__);
968
969 genpd = dev_to_genpd(dev);
970 if (IS_ERR(genpd))
971 return -EINVAL;
972
Ulf Hansson1e0407c2014-09-03 12:52:19 +0200973 return genpd->suspend_power_off ? 0 : pm_generic_freeze_late(dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100974}
975
976/**
977 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200978 * @dev: Device to freeze.
979 *
980 * Carry out a late freeze of a device under the assumption that its
981 * pm_domain field points to the domain member of an object of type
982 * struct generic_pm_domain representing a power domain consisting of I/O
983 * devices.
984 */
985static int pm_genpd_freeze_noirq(struct device *dev)
986{
987 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200988
989 dev_dbg(dev, "%s()\n", __func__);
990
991 genpd = dev_to_genpd(dev);
992 if (IS_ERR(genpd))
993 return -EINVAL;
994
Ulf Hansson2b1d88c2015-10-15 17:02:19 +0200995 return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +0200996}
997
998/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +0100999 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001000 * @dev: Device to thaw.
1001 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001002 * Start the device, unless power has been removed from the domain already
1003 * before the system transition.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001004 */
1005static int pm_genpd_thaw_noirq(struct device *dev)
1006{
1007 struct generic_pm_domain *genpd;
1008
1009 dev_dbg(dev, "%s()\n", __func__);
1010
1011 genpd = dev_to_genpd(dev);
1012 if (IS_ERR(genpd))
1013 return -EINVAL;
1014
Ulf Hansson51cda842015-10-15 17:02:06 +02001015 return genpd->suspend_power_off ?
Ulf Hansson2b1d88c2015-10-15 17:02:19 +02001016 0 : genpd_start_dev(genpd, dev);
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001017}
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001018
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001019/**
1020 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1021 * @dev: Device to thaw.
1022 *
1023 * Carry out an early thaw of a device under the assumption that its
1024 * pm_domain field points to the domain member of an object of type
1025 * struct generic_pm_domain representing a power domain consisting of I/O
1026 * devices.
1027 */
1028static int pm_genpd_thaw_early(struct device *dev)
1029{
1030 struct generic_pm_domain *genpd;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001031
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001032 dev_dbg(dev, "%s()\n", __func__);
1033
1034 genpd = dev_to_genpd(dev);
1035 if (IS_ERR(genpd))
1036 return -EINVAL;
1037
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001038 return genpd->suspend_power_off ? 0 : pm_generic_thaw_early(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001039}
1040
1041/**
1042 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1043 * @dev: Device to thaw.
1044 *
1045 * Thaw a device under the assumption that its pm_domain field points to the
1046 * domain member of an object of type struct generic_pm_domain representing
1047 * a power domain consisting of I/O devices.
1048 */
1049static int pm_genpd_thaw(struct device *dev)
1050{
1051 struct generic_pm_domain *genpd;
1052
1053 dev_dbg(dev, "%s()\n", __func__);
1054
1055 genpd = dev_to_genpd(dev);
1056 if (IS_ERR(genpd))
1057 return -EINVAL;
1058
Ulf Hansson1e0407c2014-09-03 12:52:19 +02001059 return genpd->suspend_power_off ? 0 : pm_generic_thaw(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001060}
1061
1062/**
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001063 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001064 * @dev: Device to resume.
1065 *
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001066 * Make sure the domain will be in the same power state as before the
1067 * hibernation the system is resuming from and start the device if necessary.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001068 */
1069static int pm_genpd_restore_noirq(struct device *dev)
1070{
1071 struct generic_pm_domain *genpd;
1072
1073 dev_dbg(dev, "%s()\n", __func__);
1074
1075 genpd = dev_to_genpd(dev);
1076 if (IS_ERR(genpd))
1077 return -EINVAL;
1078
1079 /*
1080 * Since all of the "noirq" callbacks are executed sequentially, it is
1081 * guaranteed that this function will never run twice in parallel for
1082 * the same PM domain, so it is not necessary to use locking here.
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001083 *
1084 * At this point suspended_count == 0 means we are being run for the
1085 * first time for the given domain in the present cycle.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001086 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001087 if (genpd->suspended_count++ == 0) {
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001088 /*
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001089 * The boot kernel might put the domain into arbitrary state,
Rafael J. Wysocki802d8b42012-08-06 01:39:16 +02001090 * so make it appear as powered off to pm_genpd_sync_poweron(),
1091 * so that it tries to power it on in case it was really off.
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001092 */
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001093 genpd->status = GPD_STATE_POWER_OFF;
1094 if (genpd->suspend_power_off) {
1095 /*
1096 * If the domain was off before the hibernation, make
1097 * sure it will be off going forward.
1098 */
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001099 genpd_power_off(genpd, true);
Rafael J. Wysocki65533bb2012-03-13 22:39:37 +01001100
1101 return 0;
1102 }
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001103 }
1104
Rafael J. Wysocki18dd2ec2012-03-19 10:38:14 +01001105 if (genpd->suspend_power_off)
1106 return 0;
1107
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001108 pm_genpd_sync_poweron(genpd, true);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001109
Ulf Hansson2b1d88c2015-10-15 17:02:19 +02001110 return genpd_start_dev(genpd, dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001111}
1112
1113/**
1114 * pm_genpd_complete - Complete power transition of a device in a power domain.
1115 * @dev: Device to complete the transition of.
1116 *
1117 * Complete a power transition of a device (during a system-wide power
1118 * transition) under the assumption that its pm_domain field points to the
1119 * domain member of an object of type struct generic_pm_domain representing
1120 * a power domain consisting of I/O devices.
1121 */
1122static void pm_genpd_complete(struct device *dev)
1123{
1124 struct generic_pm_domain *genpd;
1125 bool run_complete;
1126
1127 dev_dbg(dev, "%s()\n", __func__);
1128
1129 genpd = dev_to_genpd(dev);
1130 if (IS_ERR(genpd))
1131 return;
1132
1133 mutex_lock(&genpd->lock);
1134
1135 run_complete = !genpd->suspend_power_off;
1136 if (--genpd->prepared_count == 0)
1137 genpd->suspend_power_off = false;
1138
1139 mutex_unlock(&genpd->lock);
1140
1141 if (run_complete) {
1142 pm_generic_complete(dev);
Rafael J. Wysocki6f00ff72011-07-12 00:39:10 +02001143 pm_runtime_set_active(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001144 pm_runtime_enable(dev);
Ulf Hanssonaf939332013-04-12 09:41:06 +00001145 pm_request_idle(dev);
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001146 }
1147}
1148
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001149/**
Ulf Hanssond47e6462014-09-03 12:52:24 +02001150 * genpd_syscore_switch - Switch power during system core suspend or resume.
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001151 * @dev: Device that normally is marked as "always on" to switch power for.
1152 *
1153 * This routine may only be called during the system core (syscore) suspend or
1154 * resume phase for devices whose "always on" flags are set.
1155 */
Ulf Hanssond47e6462014-09-03 12:52:24 +02001156static void genpd_syscore_switch(struct device *dev, bool suspend)
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001157{
1158 struct generic_pm_domain *genpd;
1159
1160 genpd = dev_to_genpd(dev);
1161 if (!pm_genpd_present(genpd))
1162 return;
1163
1164 if (suspend) {
1165 genpd->suspended_count++;
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001166 pm_genpd_sync_poweroff(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001167 } else {
Geert Uytterhoevena4630c62015-05-29 17:24:23 +02001168 pm_genpd_sync_poweron(genpd, false);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001169 genpd->suspended_count--;
1170 }
1171}
Ulf Hanssond47e6462014-09-03 12:52:24 +02001172
1173void pm_genpd_syscore_poweroff(struct device *dev)
1174{
1175 genpd_syscore_switch(dev, true);
1176}
1177EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1178
1179void pm_genpd_syscore_poweron(struct device *dev)
1180{
1181 genpd_syscore_switch(dev, false);
1182}
1183EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
Rafael J. Wysocki77f827d2012-08-06 01:39:57 +02001184
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001185#else /* !CONFIG_PM_SLEEP */
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001186
1187#define pm_genpd_prepare NULL
1188#define pm_genpd_suspend NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001189#define pm_genpd_suspend_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001190#define pm_genpd_suspend_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001191#define pm_genpd_resume_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001192#define pm_genpd_resume_noirq NULL
1193#define pm_genpd_resume NULL
1194#define pm_genpd_freeze NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001195#define pm_genpd_freeze_late NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001196#define pm_genpd_freeze_noirq NULL
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001197#define pm_genpd_thaw_early NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001198#define pm_genpd_thaw_noirq NULL
1199#define pm_genpd_thaw NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001200#define pm_genpd_restore_noirq NULL
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001201#define pm_genpd_complete NULL
1202
1203#endif /* CONFIG_PM_SLEEP */
1204
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001205static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
1206 struct generic_pm_domain *genpd,
1207 struct gpd_timing_data *td)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001208{
1209 struct generic_pm_domain_data *gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001210 int ret;
1211
1212 ret = dev_pm_get_subsys_data(dev);
1213 if (ret)
1214 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001215
1216 gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
Ulf Hansson3e235682015-01-27 21:13:43 +01001217 if (!gpd_data) {
1218 ret = -ENOMEM;
1219 goto err_put;
1220 }
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001221
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001222 if (td)
1223 gpd_data->td = *td;
1224
1225 gpd_data->base.dev = dev;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001226 gpd_data->td.constraint_changed = true;
1227 gpd_data->td.effective_constraint_ns = -1;
1228 gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1229
1230 spin_lock_irq(&dev->power.lock);
1231
1232 if (dev->power.subsys_data->domain_data) {
1233 ret = -EINVAL;
1234 goto err_free;
1235 }
1236
1237 dev->power.subsys_data->domain_data = &gpd_data->base;
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001238
1239 spin_unlock_irq(&dev->power.lock);
1240
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001241 dev_pm_domain_set(dev, &genpd->domain);
1242
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001243 return gpd_data;
Ulf Hansson3e235682015-01-27 21:13:43 +01001244
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001245 err_free:
1246 spin_unlock_irq(&dev->power.lock);
1247 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001248 err_put:
1249 dev_pm_put_subsys_data(dev);
1250 return ERR_PTR(ret);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001251}
1252
Ulf Hansson49d400c2015-01-27 21:13:38 +01001253static void genpd_free_dev_data(struct device *dev,
1254 struct generic_pm_domain_data *gpd_data)
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001255{
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001256 dev_pm_domain_set(dev, NULL);
1257
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001258 spin_lock_irq(&dev->power.lock);
1259
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001260 dev->power.subsys_data->domain_data = NULL;
1261
1262 spin_unlock_irq(&dev->power.lock);
1263
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001264 kfree(gpd_data);
Ulf Hansson3e235682015-01-27 21:13:43 +01001265 dev_pm_put_subsys_data(dev);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001266}
1267
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001268/**
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001269 * __pm_genpd_add_device - Add a device to an I/O PM domain.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001270 * @genpd: PM domain to add the device to.
1271 * @dev: Device to be added.
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001272 * @td: Set of PM QoS timing parameters to attach to the device.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001273 */
Rafael J. Wysockib02c9992011-12-01 00:02:05 +01001274int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1275 struct gpd_timing_data *td)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001276{
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001277 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001278 int ret = 0;
1279
1280 dev_dbg(dev, "%s()\n", __func__);
1281
1282 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1283 return -EINVAL;
1284
Ulf Hanssonf104e1e2015-01-27 21:13:44 +01001285 gpd_data = genpd_alloc_dev_data(dev, genpd, td);
Ulf Hansson3e235682015-01-27 21:13:43 +01001286 if (IS_ERR(gpd_data))
1287 return PTR_ERR(gpd_data);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001288
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001289 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001290
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001291 if (genpd->prepared_count > 0) {
1292 ret = -EAGAIN;
1293 goto out;
1294 }
1295
Ulf Hanssonb472c2f2015-01-27 21:13:45 +01001296 ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1297 if (ret)
1298 goto out;
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001299
Ulf Hansson14b53062015-01-27 21:13:40 +01001300 genpd->device_count++;
1301 genpd->max_off_time_changed = true;
1302
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001303 list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001304
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001305 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001306 mutex_unlock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001307
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001308 if (ret)
1309 genpd_free_dev_data(dev, gpd_data);
1310 else
1311 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001312
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001313 return ret;
1314}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301315EXPORT_SYMBOL_GPL(__pm_genpd_add_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001316
1317/**
1318 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1319 * @genpd: PM domain to remove the device from.
1320 * @dev: Device to be removed.
1321 */
1322int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1323 struct device *dev)
1324{
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001325 struct generic_pm_domain_data *gpd_data;
Rafael J. Wysocki4605ab62011-08-25 15:34:12 +02001326 struct pm_domain_data *pdd;
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001327 int ret = 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001328
1329 dev_dbg(dev, "%s()\n", __func__);
1330
Russell Kingdf6a0d62015-03-20 17:20:38 +00001331 if (!genpd || genpd != pm_genpd_lookup_dev(dev))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001332 return -EINVAL;
1333
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001334 /* The above validation also means we have existing domain_data. */
1335 pdd = dev->power.subsys_data->domain_data;
1336 gpd_data = to_gpd_data(pdd);
1337 dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
1338
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001339 mutex_lock(&genpd->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001340
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001341 if (genpd->prepared_count > 0) {
1342 ret = -EAGAIN;
1343 goto out;
1344 }
1345
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001346 genpd->device_count--;
1347 genpd->max_off_time_changed = true;
1348
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001349 if (genpd->detach_dev)
Ulf Hanssonc16561e2014-11-06 00:37:08 +01001350 genpd->detach_dev(genpd, dev);
Geert Uytterhoevend79b6fe2014-09-25 18:28:28 +02001351
Rafael J. Wysockiefa69022012-05-01 21:33:53 +02001352 list_del_init(&pdd->list_node);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001353
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001354 mutex_unlock(&genpd->lock);
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001355
Ulf Hanssonc1dbe2f2015-01-27 21:13:39 +01001356 genpd_free_dev_data(dev, gpd_data);
Rafael J. Wysocki1d5fcfe2012-07-05 22:12:32 +02001357
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001358 return 0;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001359
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001360 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001361 mutex_unlock(&genpd->lock);
Ulf Hanssonc0356db2015-01-27 21:13:42 +01001362 dev_pm_qos_add_notifier(dev, &gpd_data->nb);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001363
1364 return ret;
1365}
Maruthi Bayyavarapu24c96dc2015-11-18 01:12:00 +05301366EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001367
1368/**
1369 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1370 * @genpd: Master PM domain to add the subdomain to.
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001371 * @subdomain: Subdomain to be added.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001372 */
1373int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001374 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001375{
Lina Iyer25479232015-10-28 15:19:50 -06001376 struct gpd_link *link, *itr;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001377 int ret = 0;
1378
Rafael J. Wysockifb7268b2012-08-07 01:08:37 +02001379 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1380 || genpd == subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001381 return -EINVAL;
1382
Lina Iyer25479232015-10-28 15:19:50 -06001383 link = kzalloc(sizeof(*link), GFP_KERNEL);
1384 if (!link)
1385 return -ENOMEM;
1386
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001387 mutex_lock(&subdomain->lock);
1388 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001389
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001390 if (genpd->status == GPD_STATE_POWER_OFF
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001391 && subdomain->status != GPD_STATE_POWER_OFF) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001392 ret = -EINVAL;
1393 goto out;
1394 }
1395
Lina Iyer25479232015-10-28 15:19:50 -06001396 list_for_each_entry(itr, &genpd->master_links, master_node) {
1397 if (itr->slave == subdomain && itr->master == genpd) {
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001398 ret = -EINVAL;
1399 goto out;
1400 }
1401 }
1402
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001403 link->master = genpd;
1404 list_add_tail(&link->master_node, &genpd->master_links);
Rafael J. Wysockibc0403f2011-08-08 23:43:59 +02001405 link->slave = subdomain;
1406 list_add_tail(&link->slave_node, &subdomain->slave_links);
1407 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001408 genpd_sd_counter_inc(genpd);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001409
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001410 out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001411 mutex_unlock(&genpd->lock);
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001412 mutex_unlock(&subdomain->lock);
Lina Iyer25479232015-10-28 15:19:50 -06001413 if (ret)
1414 kfree(link);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001415 return ret;
1416}
Stephen Boydd60ee962015-10-01 12:22:53 -07001417EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001418
1419/**
1420 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1421 * @genpd: Master PM domain to remove the subdomain from.
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001422 * @subdomain: Subdomain to be removed.
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001423 */
1424int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001425 struct generic_pm_domain *subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001426{
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001427 struct gpd_link *link;
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001428 int ret = -EINVAL;
1429
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001430 if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001431 return -EINVAL;
1432
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001433 mutex_lock(&subdomain->lock);
1434 mutex_lock_nested(&genpd->lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001435
Jon Hunterbeda5fc2016-03-04 10:55:14 +00001436 if (!list_empty(&subdomain->master_links) || subdomain->device_count) {
Jon Hunter30e7a652015-09-03 09:10:37 +01001437 pr_warn("%s: unable to remove subdomain %s\n", genpd->name,
1438 subdomain->name);
1439 ret = -EBUSY;
1440 goto out;
1441 }
1442
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001443 list_for_each_entry(link, &genpd->master_links, master_node) {
1444 if (link->slave != subdomain)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001445 continue;
1446
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001447 list_del(&link->master_node);
1448 list_del(&link->slave_node);
1449 kfree(link);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001450 if (subdomain->status != GPD_STATE_POWER_OFF)
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001451 genpd_sd_counter_dec(genpd);
1452
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001453 ret = 0;
1454 break;
1455 }
1456
Jon Hunter30e7a652015-09-03 09:10:37 +01001457out:
Ulf Hanssonba2bbfb2015-06-18 15:17:53 +02001458 mutex_unlock(&genpd->lock);
Ulf Hanssoncdb300a2016-01-27 09:10:37 +01001459 mutex_unlock(&subdomain->lock);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001460
1461 return ret;
1462}
Stephen Boydd60ee962015-10-01 12:22:53 -07001463EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001464
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001465/**
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001466 * pm_genpd_init - Initialize a generic I/O PM domain object.
1467 * @genpd: PM domain object to initialize.
1468 * @gov: PM domain governor to associate with the domain (may be NULL).
1469 * @is_off: Initial value of the domain's power_is_off field.
1470 */
1471void pm_genpd_init(struct generic_pm_domain *genpd,
1472 struct dev_power_governor *gov, bool is_off)
1473{
1474 if (IS_ERR_OR_NULL(genpd))
1475 return;
1476
Rafael J. Wysocki5063ce12011-08-08 23:43:40 +02001477 INIT_LIST_HEAD(&genpd->master_links);
1478 INIT_LIST_HEAD(&genpd->slave_links);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001479 INIT_LIST_HEAD(&genpd->dev_list);
Rafael J. Wysockif7218892011-07-01 22:12:45 +02001480 mutex_init(&genpd->lock);
1481 genpd->gov = gov;
1482 INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
Rafael J. Wysockic4bb3162011-08-08 23:43:04 +02001483 atomic_set(&genpd->sd_count, 0);
Rafael J. Wysocki17b75ec2011-07-12 00:39:29 +02001484 genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001485 genpd->device_count = 0;
Rafael J. Wysocki221e9b52011-12-01 00:02:10 +01001486 genpd->max_off_time_ns = -1;
Rafael J. Wysocki6ff7bb0d02012-05-01 21:34:07 +02001487 genpd->max_off_time_changed = true;
Ulf Hansson795bd2e2016-03-31 11:21:26 +02001488 genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1489 genpd->domain.ops.runtime_resume = genpd_runtime_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001490 genpd->domain.ops.prepare = pm_genpd_prepare;
1491 genpd->domain.ops.suspend = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001492 genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001493 genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1494 genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001495 genpd->domain.ops.resume_early = pm_genpd_resume_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001496 genpd->domain.ops.resume = pm_genpd_resume;
1497 genpd->domain.ops.freeze = pm_genpd_freeze;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001498 genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001499 genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1500 genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001501 genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001502 genpd->domain.ops.thaw = pm_genpd_thaw;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001503 genpd->domain.ops.poweroff = pm_genpd_suspend;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001504 genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001505 genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001506 genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
Rafael J. Wysocki0496c8a2012-01-29 20:39:02 +01001507 genpd->domain.ops.restore_early = pm_genpd_resume_early;
Rafael J. Wysockid23b9b02011-11-27 13:11:51 +01001508 genpd->domain.ops.restore = pm_genpd_resume;
Rafael J. Wysocki596ba342011-07-01 22:13:19 +02001509 genpd->domain.ops.complete = pm_genpd_complete;
Ulf Hanssonc11f6f52014-12-01 12:50:21 +01001510
1511 if (genpd->flags & GENPD_FLAG_PM_CLK) {
1512 genpd->dev_ops.stop = pm_clk_suspend;
1513 genpd->dev_ops.start = pm_clk_resume;
1514 }
1515
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001516 if (genpd->state_idx >= GENPD_MAX_NUM_STATES) {
1517 pr_warn("Initial state index out of bounds.\n");
1518 genpd->state_idx = GENPD_MAX_NUM_STATES - 1;
1519 }
1520
1521 if (genpd->state_count > GENPD_MAX_NUM_STATES) {
1522 pr_warn("Limiting states to %d\n", GENPD_MAX_NUM_STATES);
1523 genpd->state_count = GENPD_MAX_NUM_STATES;
1524 }
1525
1526 /* Use only one "off" state if there were no states declared */
Axel Haslam90e63452016-02-15 11:10:53 +01001527 if (genpd->state_count == 0)
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001528 genpd->state_count = 1;
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001529
Rafael J. Wysocki5125bbf382011-07-13 12:31:52 +02001530 mutex_lock(&gpd_list_lock);
1531 list_add(&genpd->gpd_list_node, &gpd_list);
1532 mutex_unlock(&gpd_list_lock);
1533}
Rajendra Nayakbe5ed552015-08-13 11:51:57 +05301534EXPORT_SYMBOL_GPL(pm_genpd_init);
Tomasz Figaaa422402014-09-19 20:27:36 +02001535
1536#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1537/*
1538 * Device Tree based PM domain providers.
1539 *
1540 * The code below implements generic device tree based PM domain providers that
1541 * bind device tree nodes with generic PM domains registered in the system.
1542 *
1543 * Any driver that registers generic PM domains and needs to support binding of
1544 * devices to these domains is supposed to register a PM domain provider, which
1545 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1546 *
1547 * Two simple mapping functions have been provided for convenience:
1548 * - __of_genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1549 * - __of_genpd_xlate_onecell() for mapping of multiple PM domains per node by
1550 * index.
1551 */
1552
1553/**
1554 * struct of_genpd_provider - PM domain provider registration structure
1555 * @link: Entry in global list of PM domain providers
1556 * @node: Pointer to device tree node of PM domain provider
1557 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1558 * into a PM domain.
1559 * @data: context pointer to be passed into @xlate callback
1560 */
1561struct of_genpd_provider {
1562 struct list_head link;
1563 struct device_node *node;
1564 genpd_xlate_t xlate;
1565 void *data;
1566};
1567
1568/* List of registered PM domain providers. */
1569static LIST_HEAD(of_genpd_providers);
1570/* Mutex to protect the list above. */
1571static DEFINE_MUTEX(of_genpd_mutex);
1572
1573/**
1574 * __of_genpd_xlate_simple() - Xlate function for direct node-domain mapping
1575 * @genpdspec: OF phandle args to map into a PM domain
1576 * @data: xlate function private data - pointer to struct generic_pm_domain
1577 *
1578 * This is a generic xlate function that can be used to model PM domains that
1579 * have their own device tree nodes. The private data of xlate function needs
1580 * to be a valid pointer to struct generic_pm_domain.
1581 */
1582struct generic_pm_domain *__of_genpd_xlate_simple(
1583 struct of_phandle_args *genpdspec,
1584 void *data)
1585{
1586 if (genpdspec->args_count != 0)
1587 return ERR_PTR(-EINVAL);
1588 return data;
1589}
1590EXPORT_SYMBOL_GPL(__of_genpd_xlate_simple);
1591
1592/**
1593 * __of_genpd_xlate_onecell() - Xlate function using a single index.
1594 * @genpdspec: OF phandle args to map into a PM domain
1595 * @data: xlate function private data - pointer to struct genpd_onecell_data
1596 *
1597 * This is a generic xlate function that can be used to model simple PM domain
1598 * controllers that have one device tree node and provide multiple PM domains.
1599 * A single cell is used as an index into an array of PM domains specified in
1600 * the genpd_onecell_data struct when registering the provider.
1601 */
1602struct generic_pm_domain *__of_genpd_xlate_onecell(
1603 struct of_phandle_args *genpdspec,
1604 void *data)
1605{
1606 struct genpd_onecell_data *genpd_data = data;
1607 unsigned int idx = genpdspec->args[0];
1608
1609 if (genpdspec->args_count != 1)
1610 return ERR_PTR(-EINVAL);
1611
1612 if (idx >= genpd_data->num_domains) {
1613 pr_err("%s: invalid domain index %u\n", __func__, idx);
1614 return ERR_PTR(-EINVAL);
1615 }
1616
1617 if (!genpd_data->domains[idx])
1618 return ERR_PTR(-ENOENT);
1619
1620 return genpd_data->domains[idx];
1621}
1622EXPORT_SYMBOL_GPL(__of_genpd_xlate_onecell);
1623
1624/**
1625 * __of_genpd_add_provider() - Register a PM domain provider for a node
1626 * @np: Device node pointer associated with the PM domain provider.
1627 * @xlate: Callback for decoding PM domain from phandle arguments.
1628 * @data: Context pointer for @xlate callback.
1629 */
1630int __of_genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1631 void *data)
1632{
1633 struct of_genpd_provider *cp;
1634
1635 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1636 if (!cp)
1637 return -ENOMEM;
1638
1639 cp->node = of_node_get(np);
1640 cp->data = data;
1641 cp->xlate = xlate;
1642
1643 mutex_lock(&of_genpd_mutex);
1644 list_add(&cp->link, &of_genpd_providers);
1645 mutex_unlock(&of_genpd_mutex);
1646 pr_debug("Added domain provider from %s\n", np->full_name);
1647
1648 return 0;
1649}
1650EXPORT_SYMBOL_GPL(__of_genpd_add_provider);
1651
1652/**
1653 * of_genpd_del_provider() - Remove a previously registered PM domain provider
1654 * @np: Device node pointer associated with the PM domain provider
1655 */
1656void of_genpd_del_provider(struct device_node *np)
1657{
1658 struct of_genpd_provider *cp;
1659
1660 mutex_lock(&of_genpd_mutex);
1661 list_for_each_entry(cp, &of_genpd_providers, link) {
1662 if (cp->node == np) {
1663 list_del(&cp->link);
1664 of_node_put(cp->node);
1665 kfree(cp);
1666 break;
1667 }
1668 }
1669 mutex_unlock(&of_genpd_mutex);
1670}
1671EXPORT_SYMBOL_GPL(of_genpd_del_provider);
1672
1673/**
1674 * of_genpd_get_from_provider() - Look-up PM domain
1675 * @genpdspec: OF phandle args to use for look-up
1676 *
1677 * Looks for a PM domain provider under the node specified by @genpdspec and if
1678 * found, uses xlate function of the provider to map phandle args to a PM
1679 * domain.
1680 *
1681 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
1682 * on failure.
1683 */
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05301684struct generic_pm_domain *of_genpd_get_from_provider(
Tomasz Figaaa422402014-09-19 20:27:36 +02001685 struct of_phandle_args *genpdspec)
1686{
1687 struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
1688 struct of_genpd_provider *provider;
1689
Jon Hunter41795a82016-03-04 10:55:15 +00001690 if (!genpdspec)
1691 return ERR_PTR(-EINVAL);
1692
Tomasz Figaaa422402014-09-19 20:27:36 +02001693 mutex_lock(&of_genpd_mutex);
1694
1695 /* Check if we have such a provider in our array */
1696 list_for_each_entry(provider, &of_genpd_providers, link) {
1697 if (provider->node == genpdspec->np)
1698 genpd = provider->xlate(genpdspec, provider->data);
1699 if (!IS_ERR(genpd))
1700 break;
1701 }
1702
1703 mutex_unlock(&of_genpd_mutex);
1704
1705 return genpd;
1706}
Amit Daniel Kachhap7496fcb2014-12-15 09:08:59 +05301707EXPORT_SYMBOL_GPL(of_genpd_get_from_provider);
Tomasz Figaaa422402014-09-19 20:27:36 +02001708
1709/**
1710 * genpd_dev_pm_detach - Detach a device from its PM domain.
Jon Hunter8bb69442015-08-27 10:17:00 +01001711 * @dev: Device to detach.
Tomasz Figaaa422402014-09-19 20:27:36 +02001712 * @power_off: Currently not used
1713 *
1714 * Try to locate a corresponding generic PM domain, which the device was
1715 * attached to previously. If such is found, the device is detached from it.
1716 */
1717static void genpd_dev_pm_detach(struct device *dev, bool power_off)
1718{
Russell King446d999c2015-03-20 17:20:33 +00001719 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001720 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02001721 int ret = 0;
1722
Russell King446d999c2015-03-20 17:20:33 +00001723 pd = pm_genpd_lookup_dev(dev);
Tomasz Figaaa422402014-09-19 20:27:36 +02001724 if (!pd)
1725 return;
1726
1727 dev_dbg(dev, "removing from PM domain %s\n", pd->name);
1728
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001729 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02001730 ret = pm_genpd_remove_device(pd, dev);
1731 if (ret != -EAGAIN)
1732 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001733
1734 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02001735 cond_resched();
1736 }
1737
1738 if (ret < 0) {
1739 dev_err(dev, "failed to remove from PM domain %s: %d",
1740 pd->name, ret);
1741 return;
1742 }
1743
1744 /* Check if PM domain can be powered off after removing this device. */
1745 genpd_queue_power_off_work(pd);
1746}
1747
Russell King632f7ce2015-03-20 15:55:12 +01001748static void genpd_dev_pm_sync(struct device *dev)
1749{
1750 struct generic_pm_domain *pd;
1751
1752 pd = dev_to_genpd(dev);
1753 if (IS_ERR(pd))
1754 return;
1755
1756 genpd_queue_power_off_work(pd);
1757}
1758
Tomasz Figaaa422402014-09-19 20:27:36 +02001759/**
1760 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
1761 * @dev: Device to attach.
1762 *
1763 * Parse device's OF node to find a PM domain specifier. If such is found,
1764 * attaches the device to retrieved pm_domain ops.
1765 *
1766 * Both generic and legacy Samsung-specific DT bindings are supported to keep
1767 * backwards compatibility with existing DTBs.
1768 *
Jon Hunter311fa6a2015-07-31 10:20:00 +01001769 * Returns 0 on successfully attached PM domain or negative error code. Note
1770 * that if a power-domain exists for the device, but it cannot be found or
1771 * turned on, then return -EPROBE_DEFER to ensure that the device is not
1772 * probed and to re-try again later.
Tomasz Figaaa422402014-09-19 20:27:36 +02001773 */
1774int genpd_dev_pm_attach(struct device *dev)
1775{
1776 struct of_phandle_args pd_args;
1777 struct generic_pm_domain *pd;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001778 unsigned int i;
Tomasz Figaaa422402014-09-19 20:27:36 +02001779 int ret;
1780
1781 if (!dev->of_node)
1782 return -ENODEV;
1783
1784 if (dev->pm_domain)
1785 return -EEXIST;
1786
1787 ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
1788 "#power-domain-cells", 0, &pd_args);
1789 if (ret < 0) {
1790 if (ret != -ENOENT)
1791 return ret;
1792
1793 /*
1794 * Try legacy Samsung-specific bindings
1795 * (for backwards compatibility of DT ABI)
1796 */
1797 pd_args.args_count = 0;
1798 pd_args.np = of_parse_phandle(dev->of_node,
1799 "samsung,power-domain", 0);
1800 if (!pd_args.np)
1801 return -ENOENT;
1802 }
1803
1804 pd = of_genpd_get_from_provider(&pd_args);
Eric Anholt265e2cf2015-12-01 09:39:31 -08001805 of_node_put(pd_args.np);
Tomasz Figaaa422402014-09-19 20:27:36 +02001806 if (IS_ERR(pd)) {
1807 dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
1808 __func__, PTR_ERR(pd));
Jon Hunter311fa6a2015-07-31 10:20:00 +01001809 return -EPROBE_DEFER;
Tomasz Figaaa422402014-09-19 20:27:36 +02001810 }
1811
1812 dev_dbg(dev, "adding to PM domain %s\n", pd->name);
1813
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001814 for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
Tomasz Figaaa422402014-09-19 20:27:36 +02001815 ret = pm_genpd_add_device(pd, dev);
1816 if (ret != -EAGAIN)
1817 break;
Geert Uytterhoeven93af5e92015-06-26 11:14:14 +02001818
1819 mdelay(i);
Tomasz Figaaa422402014-09-19 20:27:36 +02001820 cond_resched();
1821 }
1822
1823 if (ret < 0) {
1824 dev_err(dev, "failed to add to PM domain %s: %d",
1825 pd->name, ret);
Jon Hunter311fa6a2015-07-31 10:20:00 +01001826 goto out;
Tomasz Figaaa422402014-09-19 20:27:36 +02001827 }
1828
1829 dev->pm_domain->detach = genpd_dev_pm_detach;
Russell King632f7ce2015-03-20 15:55:12 +01001830 dev->pm_domain->sync = genpd_dev_pm_sync;
Tomasz Figaaa422402014-09-19 20:27:36 +02001831
Ulf Hansson53af16f2016-02-01 14:52:41 +01001832 mutex_lock(&pd->lock);
1833 ret = genpd_poweron(pd, 0);
1834 mutex_unlock(&pd->lock);
Jon Hunter311fa6a2015-07-31 10:20:00 +01001835out:
1836 return ret ? -EPROBE_DEFER : 0;
Tomasz Figaaa422402014-09-19 20:27:36 +02001837}
1838EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001839#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001840
1841
1842/*** debugfs support ***/
1843
1844#ifdef CONFIG_PM_ADVANCED_DEBUG
1845#include <linux/pm.h>
1846#include <linux/device.h>
1847#include <linux/debugfs.h>
1848#include <linux/seq_file.h>
1849#include <linux/init.h>
1850#include <linux/kobject.h>
1851static struct dentry *pm_genpd_debugfs_dir;
1852
1853/*
1854 * TODO: This function is a slightly modified version of rtpm_status_show
Rafael J. Wysockid30d8192014-11-27 22:38:05 +01001855 * from sysfs.c, so generalize it.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001856 */
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001857static void rtpm_status_str(struct seq_file *s, struct device *dev)
1858{
1859 static const char * const status_lookup[] = {
1860 [RPM_ACTIVE] = "active",
1861 [RPM_RESUMING] = "resuming",
1862 [RPM_SUSPENDED] = "suspended",
1863 [RPM_SUSPENDING] = "suspending"
1864 };
1865 const char *p = "";
1866
1867 if (dev->power.runtime_error)
1868 p = "error";
1869 else if (dev->power.disable_depth)
1870 p = "unsupported";
1871 else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
1872 p = status_lookup[dev->power.runtime_status];
1873 else
1874 WARN_ON(1);
1875
1876 seq_puts(s, p);
1877}
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001878
1879static int pm_genpd_summary_one(struct seq_file *s,
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001880 struct generic_pm_domain *genpd)
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001881{
1882 static const char * const status_lookup[] = {
1883 [GPD_STATE_ACTIVE] = "on",
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001884 [GPD_STATE_POWER_OFF] = "off"
1885 };
1886 struct pm_domain_data *pm_data;
1887 const char *kobj_path;
1888 struct gpd_link *link;
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01001889 char state[16];
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001890 int ret;
1891
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001892 ret = mutex_lock_interruptible(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001893 if (ret)
1894 return -ERESTARTSYS;
1895
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001896 if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001897 goto exit;
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001898 if (genpd->status == GPD_STATE_POWER_OFF)
Geert Uytterhoeven0ba554e2016-02-23 17:49:18 +01001899 snprintf(state, sizeof(state), "%s-%u",
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01001900 status_lookup[genpd->status], genpd->state_idx);
Axel Haslamfc5cbf02016-02-15 11:10:51 +01001901 else
Geert Uytterhoeven6954d432016-02-23 17:49:17 +01001902 snprintf(state, sizeof(state), "%s",
1903 status_lookup[genpd->status]);
1904 seq_printf(s, "%-30s %-15s ", genpd->name, state);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001905
1906 /*
1907 * Modifications on the list require holding locks on both
1908 * master and slave, so we are safe.
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001909 * Also genpd->name is immutable.
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001910 */
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001911 list_for_each_entry(link, &genpd->master_links, master_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001912 seq_printf(s, "%s", link->slave->name);
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001913 if (!list_is_last(&link->master_node, &genpd->master_links))
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001914 seq_puts(s, ", ");
1915 }
1916
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001917 list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001918 kobj_path = kobject_get_path(&pm_data->dev->kobj, GFP_KERNEL);
1919 if (kobj_path == NULL)
1920 continue;
1921
1922 seq_printf(s, "\n %-50s ", kobj_path);
1923 rtpm_status_str(s, pm_data->dev);
1924 kfree(kobj_path);
1925 }
1926
1927 seq_puts(s, "\n");
1928exit:
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001929 mutex_unlock(&genpd->lock);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001930
1931 return 0;
1932}
1933
1934static int pm_genpd_summary_show(struct seq_file *s, void *data)
1935{
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001936 struct generic_pm_domain *genpd;
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001937 int ret = 0;
1938
Geert Uytterhoeven15dec672015-08-11 14:50:49 +02001939 seq_puts(s, "domain status slaves\n");
1940 seq_puts(s, " /device runtime status\n");
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001941 seq_puts(s, "----------------------------------------------------------------------\n");
1942
1943 ret = mutex_lock_interruptible(&gpd_list_lock);
1944 if (ret)
1945 return -ERESTARTSYS;
1946
Kevin Hilman66a5ca42015-03-02 11:24:28 -08001947 list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
1948 ret = pm_genpd_summary_one(s, genpd);
Maciej Matraszek2bd53062014-09-15 13:09:10 +02001949 if (ret)
1950 break;
1951 }
1952 mutex_unlock(&gpd_list_lock);
1953
1954 return ret;
1955}
1956
1957static int pm_genpd_summary_open(struct inode *inode, struct file *file)
1958{
1959 return single_open(file, pm_genpd_summary_show, NULL);
1960}
1961
1962static const struct file_operations pm_genpd_summary_fops = {
1963 .open = pm_genpd_summary_open,
1964 .read = seq_read,
1965 .llseek = seq_lseek,
1966 .release = single_release,
1967};
1968
1969static int __init pm_genpd_debug_init(void)
1970{
1971 struct dentry *d;
1972
1973 pm_genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
1974
1975 if (!pm_genpd_debugfs_dir)
1976 return -ENOMEM;
1977
1978 d = debugfs_create_file("pm_genpd_summary", S_IRUGO,
1979 pm_genpd_debugfs_dir, NULL, &pm_genpd_summary_fops);
1980 if (!d)
1981 return -ENOMEM;
1982
1983 return 0;
1984}
1985late_initcall(pm_genpd_debug_init);
1986
1987static void __exit pm_genpd_debug_exit(void)
1988{
1989 debugfs_remove_recursive(pm_genpd_debugfs_dir);
1990}
1991__exitcall(pm_genpd_debug_exit);
1992#endif /* CONFIG_PM_ADVANCED_DEBUG */