Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * drivers/base/power/domain.c - Common code related to device power domains.
   4 *
   5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
 
 
   6 */
   7#define pr_fmt(fmt) "PM: " fmt
   8
   9#include <linux/delay.h>
  10#include <linux/kernel.h>
  11#include <linux/io.h>
  12#include <linux/platform_device.h>
  13#include <linux/pm_opp.h>
  14#include <linux/pm_runtime.h>
  15#include <linux/pm_domain.h>
  16#include <linux/pm_qos.h>
  17#include <linux/pm_clock.h>
  18#include <linux/slab.h>
  19#include <linux/err.h>
  20#include <linux/sched.h>
  21#include <linux/suspend.h>
  22#include <linux/export.h>
  23#include <linux/cpu.h>
  24
  25#include "power.h"
  26
  27#define GENPD_RETRY_MAX_MS	250		/* Approximate */
  28
  29#define GENPD_DEV_CALLBACK(genpd, type, callback, dev)		\
  30({								\
  31	type (*__routine)(struct device *__d); 			\
  32	type __ret = (type)0;					\
  33								\
  34	__routine = genpd->dev_ops.callback; 			\
  35	if (__routine) {					\
  36		__ret = __routine(dev); 			\
 
 
 
 
  37	}							\
  38	__ret;							\
  39})
  40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  41static LIST_HEAD(gpd_list);
  42static DEFINE_MUTEX(gpd_list_lock);
  43
  44struct genpd_lock_ops {
  45	void (*lock)(struct generic_pm_domain *genpd);
  46	void (*lock_nested)(struct generic_pm_domain *genpd, int depth);
  47	int (*lock_interruptible)(struct generic_pm_domain *genpd);
  48	void (*unlock)(struct generic_pm_domain *genpd);
  49};
  50
  51static void genpd_lock_mtx(struct generic_pm_domain *genpd)
  52{
  53	mutex_lock(&genpd->mlock);
  54}
  55
  56static void genpd_lock_nested_mtx(struct generic_pm_domain *genpd,
  57					int depth)
  58{
  59	mutex_lock_nested(&genpd->mlock, depth);
  60}
  61
  62static int genpd_lock_interruptible_mtx(struct generic_pm_domain *genpd)
  63{
  64	return mutex_lock_interruptible(&genpd->mlock);
  65}
  66
  67static void genpd_unlock_mtx(struct generic_pm_domain *genpd)
  68{
  69	return mutex_unlock(&genpd->mlock);
  70}
  71
  72static const struct genpd_lock_ops genpd_mtx_ops = {
  73	.lock = genpd_lock_mtx,
  74	.lock_nested = genpd_lock_nested_mtx,
  75	.lock_interruptible = genpd_lock_interruptible_mtx,
  76	.unlock = genpd_unlock_mtx,
  77};
  78
  79static void genpd_lock_spin(struct generic_pm_domain *genpd)
  80	__acquires(&genpd->slock)
  81{
  82	unsigned long flags;
  83
  84	spin_lock_irqsave(&genpd->slock, flags);
  85	genpd->lock_flags = flags;
  86}
  87
  88static void genpd_lock_nested_spin(struct generic_pm_domain *genpd,
  89					int depth)
  90	__acquires(&genpd->slock)
  91{
  92	unsigned long flags;
  93
  94	spin_lock_irqsave_nested(&genpd->slock, flags, depth);
  95	genpd->lock_flags = flags;
  96}
  97
  98static int genpd_lock_interruptible_spin(struct generic_pm_domain *genpd)
  99	__acquires(&genpd->slock)
 100{
 101	unsigned long flags;
 102
 103	spin_lock_irqsave(&genpd->slock, flags);
 104	genpd->lock_flags = flags;
 105	return 0;
 106}
 107
 108static void genpd_unlock_spin(struct generic_pm_domain *genpd)
 109	__releases(&genpd->slock)
 110{
 111	spin_unlock_irqrestore(&genpd->slock, genpd->lock_flags);
 112}
 113
 114static const struct genpd_lock_ops genpd_spin_ops = {
 115	.lock = genpd_lock_spin,
 116	.lock_nested = genpd_lock_nested_spin,
 117	.lock_interruptible = genpd_lock_interruptible_spin,
 118	.unlock = genpd_unlock_spin,
 119};
 120
 121#define genpd_lock(p)			p->lock_ops->lock(p)
 122#define genpd_lock_nested(p, d)		p->lock_ops->lock_nested(p, d)
 123#define genpd_lock_interruptible(p)	p->lock_ops->lock_interruptible(p)
 124#define genpd_unlock(p)			p->lock_ops->unlock(p)
 125
 126#define genpd_status_on(genpd)		(genpd->status == GPD_STATE_ACTIVE)
 127#define genpd_is_irq_safe(genpd)	(genpd->flags & GENPD_FLAG_IRQ_SAFE)
 128#define genpd_is_always_on(genpd)	(genpd->flags & GENPD_FLAG_ALWAYS_ON)
 129#define genpd_is_active_wakeup(genpd)	(genpd->flags & GENPD_FLAG_ACTIVE_WAKEUP)
 130#define genpd_is_cpu_domain(genpd)	(genpd->flags & GENPD_FLAG_CPU_DOMAIN)
 131#define genpd_is_rpm_always_on(genpd)	(genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
 132
 133static inline bool irq_safe_dev_in_no_sleep_domain(struct device *dev,
 134		const struct generic_pm_domain *genpd)
 135{
 136	bool ret;
 137
 138	ret = pm_runtime_is_irq_safe(dev) && !genpd_is_irq_safe(genpd);
 139
 140	/*
 141	 * Warn once if an IRQ safe device is attached to a no sleep domain, as
 142	 * to indicate a suboptimal configuration for PM. For an always on
 143	 * domain this isn't case, thus don't warn.
 144	 */
 145	if (ret && !genpd_is_always_on(genpd))
 146		dev_warn_once(dev, "PM domain %s will not be powered off\n",
 147				genpd->name);
 148
 149	return ret;
 150}
 151
 152static int genpd_runtime_suspend(struct device *dev);
 153
 154/*
 155 * Get the generic PM domain for a particular struct device.
 156 * This validates the struct device pointer, the PM domain pointer,
 157 * and checks that the PM domain pointer is a real generic PM domain.
 158 * Any failure results in NULL being returned.
 159 */
 160static struct generic_pm_domain *dev_to_genpd_safe(struct device *dev)
 161{
 162	if (IS_ERR_OR_NULL(dev) || IS_ERR_OR_NULL(dev->pm_domain))
 163		return NULL;
 164
 165	/* A genpd's always have its ->runtime_suspend() callback assigned. */
 166	if (dev->pm_domain->ops.runtime_suspend == genpd_runtime_suspend)
 167		return pd_to_genpd(dev->pm_domain);
 168
 169	return NULL;
 
 
 
 
 170}
 171
 172/*
 173 * This should only be used where we are certain that the pm_domain
 174 * attached to the device is a genpd domain.
 175 */
 176static struct generic_pm_domain *dev_to_genpd(struct device *dev)
 177{
 178	if (IS_ERR_OR_NULL(dev->pm_domain))
 179		return ERR_PTR(-EINVAL);
 180
 181	return pd_to_genpd(dev->pm_domain);
 182}
 183
 184static int genpd_stop_dev(const struct generic_pm_domain *genpd,
 185			  struct device *dev)
 186{
 187	return GENPD_DEV_CALLBACK(genpd, int, stop, dev);
 
 188}
 189
 190static int genpd_start_dev(const struct generic_pm_domain *genpd,
 191			   struct device *dev)
 192{
 193	return GENPD_DEV_CALLBACK(genpd, int, start, dev);
 
 194}
 195
 196static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
 197{
 198	bool ret = false;
 199
 200	if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
 201		ret = !!atomic_dec_and_test(&genpd->sd_count);
 202
 203	return ret;
 204}
 205
 206static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
 207{
 208	atomic_inc(&genpd->sd_count);
 209	smp_mb__after_atomic();
 210}
 211
 212#ifdef CONFIG_DEBUG_FS
 213static void genpd_update_accounting(struct generic_pm_domain *genpd)
 214{
 215	ktime_t delta, now;
 216
 217	now = ktime_get();
 218	delta = ktime_sub(now, genpd->accounting_time);
 219
 
 220	/*
 221	 * If genpd->status is active, it means we are just
 222	 * out of off and so update the idle time and vice
 223	 * versa.
 224	 */
 225	if (genpd->status == GPD_STATE_ACTIVE) {
 226		int state_idx = genpd->state_idx;
 
 
 
 
 
 227
 228		genpd->states[state_idx].idle_time =
 229			ktime_add(genpd->states[state_idx].idle_time, delta);
 230	} else {
 231		genpd->on_time = ktime_add(genpd->on_time, delta);
 232	}
 233
 234	genpd->accounting_time = now;
 
 
 235}
 236#else
 237static inline void genpd_update_accounting(struct generic_pm_domain *genpd) {}
 238#endif
 239
 240static int _genpd_reeval_performance_state(struct generic_pm_domain *genpd,
 241					   unsigned int state)
 242{
 243	struct generic_pm_domain_data *pd_data;
 244	struct pm_domain_data *pdd;
 245	struct gpd_link *link;
 246
 247	/* New requested state is same as Max requested state */
 248	if (state == genpd->performance_state)
 249		return state;
 250
 251	/* New requested state is higher than Max requested state */
 252	if (state > genpd->performance_state)
 253		return state;
 254
 255	/* Traverse all devices within the domain */
 256	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
 257		pd_data = to_gpd_data(pdd);
 
 
 258
 259		if (pd_data->performance_state > state)
 260			state = pd_data->performance_state;
 261	}
 262
 263	/*
 264	 * Traverse all sub-domains within the domain. This can be
 265	 * done without any additional locking as the link->performance_state
 266	 * field is protected by the parent genpd->lock, which is already taken.
 267	 *
 268	 * Also note that link->performance_state (subdomain's performance state
 269	 * requirement to parent domain) is different from
 270	 * link->child->performance_state (current performance state requirement
 271	 * of the devices/sub-domains of the subdomain) and so can have a
 272	 * different value.
 273	 *
 274	 * Note that we also take vote from powered-off sub-domains into account
 275	 * as the same is done for devices right now.
 276	 */
 277	list_for_each_entry(link, &genpd->parent_links, parent_node) {
 278		if (link->performance_state > state)
 279			state = link->performance_state;
 280	}
 281
 282	return state;
 
 
 
 283}
 284
 285static int _genpd_set_performance_state(struct generic_pm_domain *genpd,
 286					unsigned int state, int depth)
 
 
 
 
 
 
 
 287{
 288	struct generic_pm_domain *parent;
 289	struct gpd_link *link;
 290	int parent_state, ret;
 
 291
 292	if (state == genpd->performance_state)
 293		return 0;
 
 
 
 
 
 294
 295	/* Propagate to parents of genpd */
 296	list_for_each_entry(link, &genpd->child_links, child_node) {
 297		parent = link->parent;
 298
 299		if (!parent->set_performance_state)
 300			continue;
 
 301
 302		/* Find parent's performance state */
 303		ret = dev_pm_opp_xlate_performance_state(genpd->opp_table,
 304							 parent->opp_table,
 305							 state);
 306		if (unlikely(ret < 0))
 307			goto err;
 308
 309		parent_state = ret;
 
 
 
 310
 311		genpd_lock_nested(parent, depth + 1);
 
 
 
 
 
 312
 313		link->prev_performance_state = link->performance_state;
 314		link->performance_state = parent_state;
 315		parent_state = _genpd_reeval_performance_state(parent,
 316						parent_state);
 317		ret = _genpd_set_performance_state(parent, parent_state, depth + 1);
 318		if (ret)
 319			link->performance_state = link->prev_performance_state;
 
 320
 321		genpd_unlock(parent);
 322
 323		if (ret)
 
 
 
 
 
 
 
 
 
 
 
 324			goto err;
 
 325	}
 326
 327	ret = genpd->set_performance_state(genpd, state);
 328	if (ret)
 329		goto err;
 330
 331	genpd->performance_state = state;
 332	return 0;
 
 333
 334err:
 335	/* Encountered an error, lets rollback */
 336	list_for_each_entry_continue_reverse(link, &genpd->child_links,
 337					     child_node) {
 338		parent = link->parent;
 
 
 
 
 
 
 339
 340		if (!parent->set_performance_state)
 341			continue;
 342
 343		genpd_lock_nested(parent, depth + 1);
 344
 345		parent_state = link->prev_performance_state;
 346		link->performance_state = parent_state;
 
 347
 348		parent_state = _genpd_reeval_performance_state(parent,
 349						parent_state);
 350		if (_genpd_set_performance_state(parent, parent_state, depth + 1)) {
 351			pr_err("%s: Failed to roll back to %d performance state\n",
 352			       parent->name, parent_state);
 353		}
 354
 355		genpd_unlock(parent);
 356	}
 
 
 
 
 
 357
 
 
 
 358	return ret;
 359}
 360
 361/**
 362 * dev_pm_genpd_set_performance_state- Set performance state of device's power
 363 * domain.
 364 *
 365 * @dev: Device for which the performance-state needs to be set.
 366 * @state: Target performance state of the device. This can be set as 0 when the
 367 *	   device doesn't have any performance state constraints left (And so
 368 *	   the device wouldn't participate anymore to find the target
 369 *	   performance state of the genpd).
 370 *
 371 * It is assumed that the users guarantee that the genpd wouldn't be detached
 372 * while this routine is getting called.
 373 *
 374 * Returns 0 on success and negative error values on failures.
 375 */
 376int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state)
 377{
 378	struct generic_pm_domain *genpd;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 379	struct generic_pm_domain_data *gpd_data;
 380	unsigned int prev;
 381	int ret;
 382
 383	genpd = dev_to_genpd_safe(dev);
 384	if (!genpd)
 385		return -ENODEV;
 386
 387	if (unlikely(!genpd->set_performance_state))
 388		return -EINVAL;
 
 
 
 
 
 389
 390	if (WARN_ON(!dev->power.subsys_data ||
 391		     !dev->power.subsys_data->domain_data))
 392		return -EINVAL;
 393
 394	genpd_lock(genpd);
 395
 396	gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
 397	prev = gpd_data->performance_state;
 398	gpd_data->performance_state = state;
 
 
 
 
 
 399
 400	state = _genpd_reeval_performance_state(genpd, state);
 401	ret = _genpd_set_performance_state(genpd, state, 0);
 402	if (ret)
 403		gpd_data->performance_state = prev;
 
 
 
 404
 405	genpd_unlock(genpd);
 
 
 
 406
 407	return ret;
 408}
 409EXPORT_SYMBOL_GPL(dev_pm_genpd_set_performance_state);
 410
 411static int _genpd_power_on(struct generic_pm_domain *genpd, bool timed)
 
 
 
 
 
 
 
 412{
 413	unsigned int state_idx = genpd->state_idx;
 414	ktime_t time_start;
 415	s64 elapsed_ns;
 416	int ret;
 417
 418	if (!genpd->power_on)
 419		return 0;
 420
 421	if (!timed)
 422		return genpd->power_on(genpd);
 423
 424	time_start = ktime_get();
 425	ret = genpd->power_on(genpd);
 426	if (ret)
 427		return ret;
 428
 429	elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 430	if (elapsed_ns <= genpd->states[state_idx].power_on_latency_ns)
 431		return ret;
 432
 433	genpd->states[state_idx].power_on_latency_ns = elapsed_ns;
 434	genpd->max_off_time_changed = true;
 435	pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
 436		 genpd->name, "on", elapsed_ns);
 437
 438	return ret;
 439}
 440
 441static int _genpd_power_off(struct generic_pm_domain *genpd, bool timed)
 
 
 
 
 
 
 
 442{
 443	unsigned int state_idx = genpd->state_idx;
 444	ktime_t time_start;
 445	s64 elapsed_ns;
 446	int ret;
 447
 448	if (!genpd->power_off)
 449		return 0;
 450
 451	if (!timed)
 452		return genpd->power_off(genpd);
 
 453
 454	time_start = ktime_get();
 455	ret = genpd->power_off(genpd);
 456	if (ret)
 457		return ret;
 458
 459	elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 460	if (elapsed_ns <= genpd->states[state_idx].power_off_latency_ns)
 461		return 0;
 462
 463	genpd->states[state_idx].power_off_latency_ns = elapsed_ns;
 464	genpd->max_off_time_changed = true;
 465	pr_debug("%s: Power-%s latency exceeded, new value %lld ns\n",
 466		 genpd->name, "off", elapsed_ns);
 467
 468	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 469}
 470
 471/**
 472 * genpd_queue_power_off_work - Queue up the execution of genpd_power_off().
 473 * @genpd: PM domain to power off.
 474 *
 475 * Queue up the execution of genpd_power_off() unless it's already been done
 476 * before.
 477 */
 478static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
 479{
 480	queue_work(pm_wq, &genpd->power_off_work);
 481}
 482
 483/**
 484 * genpd_power_off - Remove power from a given PM domain.
 485 * @genpd: PM domain to power down.
 486 * @one_dev_on: If invoked from genpd's ->runtime_suspend|resume() callback, the
 487 * RPM status of the releated device is in an intermediate state, not yet turned
 488 * into RPM_SUSPENDED. This means genpd_power_off() must allow one device to not
 489 * be RPM_SUSPENDED, while it tries to power off the PM domain.
 490 *
 491 * If all of the @genpd's devices have been suspended and all of its subdomains
 492 * have been powered down, remove power from @genpd.
 
 493 */
 494static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
 495			   unsigned int depth)
 496{
 497	struct pm_domain_data *pdd;
 498	struct gpd_link *link;
 499	unsigned int not_suspended = 0;
 
 500
 
 501	/*
 502	 * Do not try to power off the domain in the following situations:
 503	 * (1) The domain is already in the "power off" state.
 504	 * (2) System suspend is in progress.
 
 
 505	 */
 506	if (!genpd_status_on(genpd) || genpd->prepared_count > 0)
 
 
 507		return 0;
 508
 509	/*
 510	 * Abort power off for the PM domain in the following situations:
 511	 * (1) The domain is configured as always on.
 512	 * (2) When the domain has a subdomain being powered on.
 513	 */
 514	if (genpd_is_always_on(genpd) ||
 515			genpd_is_rpm_always_on(genpd) ||
 516			atomic_read(&genpd->sd_count) > 0)
 517		return -EBUSY;
 518
 
 519	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
 520		enum pm_qos_flags_status stat;
 521
 522		stat = dev_pm_qos_flags(pdd->dev, PM_QOS_FLAG_NO_POWER_OFF);
 
 
 523		if (stat > PM_QOS_FLAGS_NONE)
 524			return -EBUSY;
 525
 526		/*
 527		 * Do not allow PM domain to be powered off, when an IRQ safe
 528		 * device is part of a non-IRQ safe domain.
 529		 */
 530		if (!pm_runtime_suspended(pdd->dev) ||
 531			irq_safe_dev_in_no_sleep_domain(pdd->dev, genpd))
 532			not_suspended++;
 533	}
 534
 535	if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on))
 536		return -EBUSY;
 537
 538	if (genpd->gov && genpd->gov->power_down_ok) {
 539		if (!genpd->gov->power_down_ok(&genpd->domain))
 540			return -EAGAIN;
 541	}
 542
 543	/* Default to shallowest state. */
 544	if (!genpd->gov)
 545		genpd->state_idx = 0;
 546
 547	if (genpd->power_off) {
 548		int ret;
 549
 550		if (atomic_read(&genpd->sd_count) > 0)
 551			return -EBUSY;
 552
 553		/*
 554		 * If sd_count > 0 at this point, one of the subdomains hasn't
 555		 * managed to call genpd_power_on() for the parent yet after
 556		 * incrementing it.  In that case genpd_power_on() will wait
 557		 * for us to drop the lock, so we can call .power_off() and let
 558		 * the genpd_power_on() restore power for us (this shouldn't
 559		 * happen very often).
 560		 */
 561		ret = _genpd_power_off(genpd, true);
 562		if (ret)
 563			return ret;
 564	}
 565
 566	genpd->status = GPD_STATE_POWER_OFF;
 567	genpd_update_accounting(genpd);
 568
 569	list_for_each_entry(link, &genpd->child_links, child_node) {
 570		genpd_sd_counter_dec(link->parent);
 571		genpd_lock_nested(link->parent, depth + 1);
 572		genpd_power_off(link->parent, false, depth + 1);
 573		genpd_unlock(link->parent);
 574	}
 575
 576	return 0;
 577}
 578
 579/**
 580 * genpd_power_on - Restore power to a given PM domain and its parents.
 581 * @genpd: PM domain to power up.
 582 * @depth: nesting count for lockdep.
 583 *
 584 * Restore power to @genpd and all of its parents so that it is possible to
 585 * resume a device belonging to it.
 586 */
 587static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
 588{
 589	struct gpd_link *link;
 590	int ret = 0;
 591
 592	if (genpd_status_on(genpd))
 593		return 0;
 594
 595	/*
 596	 * The list is guaranteed not to change while the loop below is being
 597	 * executed, unless one of the parents' .power_on() callbacks fiddles
 598	 * with it.
 599	 */
 600	list_for_each_entry(link, &genpd->child_links, child_node) {
 601		struct generic_pm_domain *parent = link->parent;
 602
 603		genpd_sd_counter_inc(parent);
 604
 605		genpd_lock_nested(parent, depth + 1);
 606		ret = genpd_power_on(parent, depth + 1);
 607		genpd_unlock(parent);
 608
 609		if (ret) {
 610			genpd_sd_counter_dec(parent);
 611			goto err;
 612		}
 613	}
 614
 615	ret = _genpd_power_on(genpd, true);
 616	if (ret)
 617		goto err;
 618
 619	genpd->status = GPD_STATE_ACTIVE;
 620	genpd_update_accounting(genpd);
 621
 622	return 0;
 623
 624 err:
 625	list_for_each_entry_continue_reverse(link,
 626					&genpd->child_links,
 627					child_node) {
 628		genpd_sd_counter_dec(link->parent);
 629		genpd_lock_nested(link->parent, depth + 1);
 630		genpd_power_off(link->parent, false, depth + 1);
 631		genpd_unlock(link->parent);
 632	}
 633
 634	return ret;
 635}
 636
 637static int genpd_dev_pm_start(struct device *dev)
 638{
 639	struct generic_pm_domain *genpd = dev_to_genpd(dev);
 640
 641	return genpd_start_dev(genpd, dev);
 642}
 643
 644static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
 645				     unsigned long val, void *ptr)
 646{
 647	struct generic_pm_domain_data *gpd_data;
 648	struct device *dev;
 649
 650	gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
 651	dev = gpd_data->base.dev;
 
 652
 653	for (;;) {
 654		struct generic_pm_domain *genpd;
 655		struct pm_domain_data *pdd;
 
 656
 657		spin_lock_irq(&dev->power.lock);
 658
 659		pdd = dev->power.subsys_data ?
 660				dev->power.subsys_data->domain_data : NULL;
 661		if (pdd) {
 662			to_gpd_data(pdd)->td.constraint_changed = true;
 663			genpd = dev_to_genpd(dev);
 664		} else {
 665			genpd = ERR_PTR(-ENODATA);
 
 
 
 
 
 666		}
 667
 668		spin_unlock_irq(&dev->power.lock);
 669
 670		if (!IS_ERR(genpd)) {
 671			genpd_lock(genpd);
 672			genpd->max_off_time_changed = true;
 673			genpd_unlock(genpd);
 
 
 
 674		}
 
 675
 676		dev = dev->parent;
 677		if (!dev || dev->power.ignore_children)
 678			break;
 
 
 679	}
 680
 681	return NOTIFY_DONE;
 
 
 
 682}
 683
 684/**
 685 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
 686 * @work: Work structure used for scheduling the execution of this function.
 687 */
 688static void genpd_power_off_work_fn(struct work_struct *work)
 689{
 690	struct generic_pm_domain *genpd;
 691
 692	genpd = container_of(work, struct generic_pm_domain, power_off_work);
 693
 694	genpd_lock(genpd);
 695	genpd_power_off(genpd, false, 0);
 696	genpd_unlock(genpd);
 697}
 698
 699/**
 700 * __genpd_runtime_suspend - walk the hierarchy of ->runtime_suspend() callbacks
 701 * @dev: Device to handle.
 702 */
 703static int __genpd_runtime_suspend(struct device *dev)
 704{
 705	int (*cb)(struct device *__dev);
 706
 707	if (dev->type && dev->type->pm)
 708		cb = dev->type->pm->runtime_suspend;
 709	else if (dev->class && dev->class->pm)
 710		cb = dev->class->pm->runtime_suspend;
 711	else if (dev->bus && dev->bus->pm)
 712		cb = dev->bus->pm->runtime_suspend;
 713	else
 714		cb = NULL;
 715
 716	if (!cb && dev->driver && dev->driver->pm)
 717		cb = dev->driver->pm->runtime_suspend;
 718
 719	return cb ? cb(dev) : 0;
 720}
 721
 722/**
 723 * __genpd_runtime_resume - walk the hierarchy of ->runtime_resume() callbacks
 724 * @dev: Device to handle.
 725 */
 726static int __genpd_runtime_resume(struct device *dev)
 727{
 728	int (*cb)(struct device *__dev);
 729
 730	if (dev->type && dev->type->pm)
 731		cb = dev->type->pm->runtime_resume;
 732	else if (dev->class && dev->class->pm)
 733		cb = dev->class->pm->runtime_resume;
 734	else if (dev->bus && dev->bus->pm)
 735		cb = dev->bus->pm->runtime_resume;
 736	else
 737		cb = NULL;
 738
 739	if (!cb && dev->driver && dev->driver->pm)
 740		cb = dev->driver->pm->runtime_resume;
 741
 742	return cb ? cb(dev) : 0;
 743}
 744
 745/**
 746 * genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
 747 * @dev: Device to suspend.
 748 *
 749 * Carry out a runtime suspend of a device under the assumption that its
 750 * pm_domain field points to the domain member of an object of type
 751 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 752 */
 753static int genpd_runtime_suspend(struct device *dev)
 754{
 755	struct generic_pm_domain *genpd;
 756	bool (*suspend_ok)(struct device *__dev);
 757	struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
 758	bool runtime_pm = pm_runtime_enabled(dev);
 759	ktime_t time_start;
 760	s64 elapsed_ns;
 761	int ret;
 762
 763	dev_dbg(dev, "%s()\n", __func__);
 764
 765	genpd = dev_to_genpd(dev);
 766	if (IS_ERR(genpd))
 767		return -EINVAL;
 768
 769	/*
 770	 * A runtime PM centric subsystem/driver may re-use the runtime PM
 771	 * callbacks for other purposes than runtime PM. In those scenarios
 772	 * runtime PM is disabled. Under these circumstances, we shall skip
 773	 * validating/measuring the PM QoS latency.
 774	 */
 775	suspend_ok = genpd->gov ? genpd->gov->suspend_ok : NULL;
 776	if (runtime_pm && suspend_ok && !suspend_ok(dev))
 777		return -EBUSY;
 778
 779	/* Measure suspend latency. */
 780	time_start = 0;
 781	if (runtime_pm)
 782		time_start = ktime_get();
 783
 784	ret = __genpd_runtime_suspend(dev);
 785	if (ret)
 786		return ret;
 787
 788	ret = genpd_stop_dev(genpd, dev);
 789	if (ret) {
 790		__genpd_runtime_resume(dev);
 791		return ret;
 792	}
 793
 794	/* Update suspend latency value if the measured time exceeds it. */
 795	if (runtime_pm) {
 796		elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 797		if (elapsed_ns > td->suspend_latency_ns) {
 798			td->suspend_latency_ns = elapsed_ns;
 799			dev_dbg(dev, "suspend latency exceeded, %lld ns\n",
 800				elapsed_ns);
 801			genpd->max_off_time_changed = true;
 802			td->constraint_changed = true;
 803		}
 804	}
 805
 806	/*
 807	 * If power.irq_safe is set, this routine may be run with
 808	 * IRQs disabled, so suspend only if the PM domain also is irq_safe.
 809	 */
 810	if (irq_safe_dev_in_no_sleep_domain(dev, genpd))
 811		return 0;
 812
 813	genpd_lock(genpd);
 814	genpd_power_off(genpd, true, 0);
 815	genpd_unlock(genpd);
 
 
 816
 817	return 0;
 818}
 819
 820/**
 821 * genpd_runtime_resume - Resume a device belonging to I/O PM domain.
 822 * @dev: Device to resume.
 823 *
 824 * Carry out a runtime resume of a device under the assumption that its
 825 * pm_domain field points to the domain member of an object of type
 826 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 827 */
 828static int genpd_runtime_resume(struct device *dev)
 829{
 830	struct generic_pm_domain *genpd;
 831	struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
 832	bool runtime_pm = pm_runtime_enabled(dev);
 833	ktime_t time_start;
 834	s64 elapsed_ns;
 835	int ret;
 836	bool timed = true;
 837
 838	dev_dbg(dev, "%s()\n", __func__);
 839
 840	genpd = dev_to_genpd(dev);
 841	if (IS_ERR(genpd))
 842		return -EINVAL;
 843
 844	/*
 845	 * As we don't power off a non IRQ safe domain, which holds
 846	 * an IRQ safe device, we don't need to restore power to it.
 847	 */
 848	if (irq_safe_dev_in_no_sleep_domain(dev, genpd)) {
 849		timed = false;
 850		goto out;
 851	}
 852
 853	genpd_lock(genpd);
 854	ret = genpd_power_on(genpd, 0);
 855	genpd_unlock(genpd);
 856
 857	if (ret)
 
 
 
 858		return ret;
 859
 860 out:
 861	/* Measure resume latency. */
 862	time_start = 0;
 863	if (timed && runtime_pm)
 864		time_start = ktime_get();
 865
 866	ret = genpd_start_dev(genpd, dev);
 867	if (ret)
 868		goto err_poweroff;
 869
 870	ret = __genpd_runtime_resume(dev);
 871	if (ret)
 872		goto err_stop;
 873
 874	/* Update resume latency value if the measured time exceeds it. */
 875	if (timed && runtime_pm) {
 876		elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 877		if (elapsed_ns > td->resume_latency_ns) {
 878			td->resume_latency_ns = elapsed_ns;
 879			dev_dbg(dev, "resume latency exceeded, %lld ns\n",
 880				elapsed_ns);
 881			genpd->max_off_time_changed = true;
 882			td->constraint_changed = true;
 883		}
 884	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 885
 886	return 0;
 887
 888err_stop:
 889	genpd_stop_dev(genpd, dev);
 890err_poweroff:
 891	if (!pm_runtime_is_irq_safe(dev) ||
 892		(pm_runtime_is_irq_safe(dev) && genpd_is_irq_safe(genpd))) {
 893		genpd_lock(genpd);
 894		genpd_power_off(genpd, true, 0);
 895		genpd_unlock(genpd);
 896	}
 
 
 
 
 
 
 897
 898	return ret;
 899}
 900
 901static bool pd_ignore_unused;
 902static int __init pd_ignore_unused_setup(char *__unused)
 903{
 904	pd_ignore_unused = true;
 905	return 1;
 906}
 907__setup("pd_ignore_unused", pd_ignore_unused_setup);
 908
 909/**
 910 * genpd_power_off_unused - Power off all PM domains with no devices in use.
 911 */
 912static int __init genpd_power_off_unused(void)
 913{
 914	struct generic_pm_domain *genpd;
 915
 916	if (pd_ignore_unused) {
 917		pr_warn("genpd: Not disabling unused power domains\n");
 918		return 0;
 919	}
 920
 921	mutex_lock(&gpd_list_lock);
 922
 923	list_for_each_entry(genpd, &gpd_list, gpd_list_node)
 924		genpd_queue_power_off_work(genpd);
 925
 926	mutex_unlock(&gpd_list_lock);
 
 927
 928	return 0;
 
 
 
 
 
 929}
 930late_initcall(genpd_power_off_unused);
 
 
 
 
 
 
 931
 932#ifdef CONFIG_PM_SLEEP
 933
 934/**
 935 * genpd_sync_power_off - Synchronously power off a PM domain and its parents.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 936 * @genpd: PM domain to power off, if possible.
 937 * @use_lock: use the lock.
 938 * @depth: nesting count for lockdep.
 939 *
 940 * Check if the given PM domain can be powered off (during system suspend or
 941 * hibernation) and do that if so.  Also, in that case propagate to its parents.
 942 *
 943 * This function is only called in "noirq" and "syscore" stages of system power
 944 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
 945 * these cases the lock must be held.
 
 946 */
 947static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
 948				 unsigned int depth)
 949{
 950	struct gpd_link *link;
 951
 952	if (!genpd_status_on(genpd) || genpd_is_always_on(genpd))
 953		return;
 954
 955	if (genpd->suspended_count != genpd->device_count
 956	    || atomic_read(&genpd->sd_count) > 0)
 957		return;
 958
 959	/* Choose the deepest state when suspending */
 960	genpd->state_idx = genpd->state_count - 1;
 961	if (_genpd_power_off(genpd, false))
 962		return;
 963
 964	genpd->status = GPD_STATE_POWER_OFF;
 965
 966	list_for_each_entry(link, &genpd->child_links, child_node) {
 967		genpd_sd_counter_dec(link->parent);
 968
 969		if (use_lock)
 970			genpd_lock_nested(link->parent, depth + 1);
 971
 972		genpd_sync_power_off(link->parent, use_lock, depth + 1);
 973
 974		if (use_lock)
 975			genpd_unlock(link->parent);
 976	}
 977}
 978
 979/**
 980 * genpd_sync_power_on - Synchronously power on a PM domain and its parents.
 981 * @genpd: PM domain to power on.
 982 * @use_lock: use the lock.
 983 * @depth: nesting count for lockdep.
 984 *
 985 * This function is only called in "noirq" and "syscore" stages of system power
 986 * transitions. The "noirq" callbacks may be executed asynchronously, thus in
 987 * these cases the lock must be held.
 
 988 */
 989static void genpd_sync_power_on(struct generic_pm_domain *genpd, bool use_lock,
 990				unsigned int depth)
 991{
 992	struct gpd_link *link;
 993
 994	if (genpd_status_on(genpd))
 995		return;
 996
 997	list_for_each_entry(link, &genpd->child_links, child_node) {
 998		genpd_sd_counter_inc(link->parent);
 999
1000		if (use_lock)
1001			genpd_lock_nested(link->parent, depth + 1);
1002
1003		genpd_sync_power_on(link->parent, use_lock, depth + 1);
1004
1005		if (use_lock)
1006			genpd_unlock(link->parent);
1007	}
1008
1009	_genpd_power_on(genpd, false);
 
1010
1011	genpd->status = GPD_STATE_ACTIVE;
1012}
1013
1014/**
1015 * resume_needed - Check whether to resume a device before system suspend.
1016 * @dev: Device to check.
1017 * @genpd: PM domain the device belongs to.
1018 *
1019 * There are two cases in which a device that can wake up the system from sleep
1020 * states should be resumed by genpd_prepare(): (1) if the device is enabled
1021 * to wake up the system and it has to remain active for this purpose while the
1022 * system is in the sleep state and (2) if the device is not enabled to wake up
1023 * the system from sleep states and it generally doesn't generate wakeup signals
1024 * by itself (those signals are generated on its behalf by other parts of the
1025 * system).  In the latter case it may be necessary to reconfigure the device's
1026 * wakeup settings during system suspend, because it may have been set up to
1027 * signal remote wakeup from the system's working state as needed by runtime PM.
1028 * Return 'true' in either of the above cases.
1029 */
1030static bool resume_needed(struct device *dev,
1031			  const struct generic_pm_domain *genpd)
1032{
1033	bool active_wakeup;
1034
1035	if (!device_can_wakeup(dev))
1036		return false;
1037
1038	active_wakeup = genpd_is_active_wakeup(genpd);
1039	return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
1040}
1041
1042/**
1043 * genpd_prepare - Start power transition of a device in a PM domain.
1044 * @dev: Device to start the transition of.
1045 *
1046 * Start a power transition of a device (during a system-wide power transition)
1047 * under the assumption that its pm_domain field points to the domain member of
1048 * an object of type struct generic_pm_domain representing a PM domain
1049 * consisting of I/O devices.
1050 */
1051static int genpd_prepare(struct device *dev)
1052{
1053	struct generic_pm_domain *genpd;
1054	int ret;
1055
1056	dev_dbg(dev, "%s()\n", __func__);
1057
1058	genpd = dev_to_genpd(dev);
1059	if (IS_ERR(genpd))
1060		return -EINVAL;
1061
1062	/*
1063	 * If a wakeup request is pending for the device, it should be woken up
1064	 * at this point and a system wakeup event should be reported if it's
1065	 * set up to wake up the system from sleep states.
1066	 */
 
 
 
 
 
 
 
 
 
1067	if (resume_needed(dev, genpd))
1068		pm_runtime_resume(dev);
1069
1070	genpd_lock(genpd);
1071
1072	if (genpd->prepared_count++ == 0)
1073		genpd->suspended_count = 0;
 
 
1074
1075	genpd_unlock(genpd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1076
1077	ret = pm_generic_prepare(dev);
1078	if (ret < 0) {
1079		genpd_lock(genpd);
1080
1081		genpd->prepared_count--;
 
1082
1083		genpd_unlock(genpd);
 
1084	}
1085
1086	/* Never return 1, as genpd don't cope with the direct_complete path. */
1087	return ret >= 0 ? 0 : ret;
1088}
1089
1090/**
1091 * genpd_finish_suspend - Completion of suspend or hibernation of device in an
1092 *   I/O pm domain.
1093 * @dev: Device to suspend.
1094 * @poweroff: Specifies if this is a poweroff_noirq or suspend_noirq callback.
1095 *
1096 * Stop the device and remove power from the domain if all devices in it have
1097 * been stopped.
 
1098 */
1099static int genpd_finish_suspend(struct device *dev, bool poweroff)
1100{
1101	struct generic_pm_domain *genpd;
1102	int ret = 0;
 
1103
1104	genpd = dev_to_genpd(dev);
1105	if (IS_ERR(genpd))
1106		return -EINVAL;
1107
1108	if (poweroff)
1109		ret = pm_generic_poweroff_noirq(dev);
1110	else
1111		ret = pm_generic_suspend_noirq(dev);
1112	if (ret)
1113		return ret;
1114
1115	if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1116		return 0;
 
 
 
 
 
 
 
 
 
1117
1118	if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1119	    !pm_runtime_status_suspended(dev)) {
1120		ret = genpd_stop_dev(genpd, dev);
1121		if (ret) {
1122			if (poweroff)
1123				pm_generic_restore_noirq(dev);
1124			else
1125				pm_generic_resume_noirq(dev);
1126			return ret;
1127		}
1128	}
1129
1130	genpd_lock(genpd);
1131	genpd->suspended_count++;
1132	genpd_sync_power_off(genpd, true, 0);
1133	genpd_unlock(genpd);
1134
1135	return 0;
1136}
1137
1138/**
1139 * genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1140 * @dev: Device to suspend.
1141 *
1142 * Stop the device and remove power from the domain if all devices in it have
1143 * been stopped.
1144 */
1145static int genpd_suspend_noirq(struct device *dev)
1146{
 
 
1147	dev_dbg(dev, "%s()\n", __func__);
1148
1149	return genpd_finish_suspend(dev, false);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1150}
1151
1152/**
1153 * genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1154 * @dev: Device to resume.
1155 *
1156 * Restore power to the device's PM domain, if necessary, and start the device.
1157 */
1158static int genpd_resume_noirq(struct device *dev)
1159{
1160	struct generic_pm_domain *genpd;
1161	int ret;
1162
1163	dev_dbg(dev, "%s()\n", __func__);
1164
1165	genpd = dev_to_genpd(dev);
1166	if (IS_ERR(genpd))
1167		return -EINVAL;
1168
1169	if (dev->power.wakeup_path && genpd_is_active_wakeup(genpd))
1170		return pm_generic_resume_noirq(dev);
 
1171
1172	genpd_lock(genpd);
1173	genpd_sync_power_on(genpd, true, 0);
 
 
 
 
1174	genpd->suspended_count--;
1175	genpd_unlock(genpd);
1176
1177	if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1178	    !pm_runtime_status_suspended(dev)) {
1179		ret = genpd_start_dev(genpd, dev);
1180		if (ret)
1181			return ret;
1182	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1183
1184	return pm_generic_resume_noirq(dev);
 
 
 
 
1185}
1186
1187/**
1188 * genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1189 * @dev: Device to freeze.
1190 *
1191 * Carry out a late freeze of a device under the assumption that its
1192 * pm_domain field points to the domain member of an object of type
1193 * struct generic_pm_domain representing a power domain consisting of I/O
1194 * devices.
1195 */
1196static int genpd_freeze_noirq(struct device *dev)
1197{
1198	const struct generic_pm_domain *genpd;
1199	int ret = 0;
1200
1201	dev_dbg(dev, "%s()\n", __func__);
1202
1203	genpd = dev_to_genpd(dev);
1204	if (IS_ERR(genpd))
1205		return -EINVAL;
1206
1207	ret = pm_generic_freeze_noirq(dev);
1208	if (ret)
1209		return ret;
1210
1211	if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1212	    !pm_runtime_status_suspended(dev))
1213		ret = genpd_stop_dev(genpd, dev);
 
 
 
 
 
 
 
 
 
1214
1215	return ret;
 
 
 
 
 
 
1216}
1217
1218/**
1219 * genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1220 * @dev: Device to thaw.
1221 *
1222 * Start the device, unless power has been removed from the domain already
1223 * before the system transition.
1224 */
1225static int genpd_thaw_noirq(struct device *dev)
1226{
1227	const struct generic_pm_domain *genpd;
1228	int ret = 0;
1229
1230	dev_dbg(dev, "%s()\n", __func__);
1231
1232	genpd = dev_to_genpd(dev);
1233	if (IS_ERR(genpd))
1234		return -EINVAL;
1235
1236	if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1237	    !pm_runtime_status_suspended(dev)) {
1238		ret = genpd_start_dev(genpd, dev);
1239		if (ret)
1240			return ret;
1241	}
1242
1243	return pm_generic_thaw_noirq(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1244}
1245
1246/**
1247 * genpd_poweroff_noirq - Completion of hibernation of device in an
1248 *   I/O PM domain.
1249 * @dev: Device to poweroff.
1250 *
1251 * Stop the device and remove power from the domain if all devices in it have
1252 * been stopped.
 
1253 */
1254static int genpd_poweroff_noirq(struct device *dev)
1255{
 
 
1256	dev_dbg(dev, "%s()\n", __func__);
1257
1258	return genpd_finish_suspend(dev, true);
 
 
 
 
1259}
1260
1261/**
1262 * genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1263 * @dev: Device to resume.
1264 *
1265 * Make sure the domain will be in the same power state as before the
1266 * hibernation the system is resuming from and start the device if necessary.
1267 */
1268static int genpd_restore_noirq(struct device *dev)
1269{
1270	struct generic_pm_domain *genpd;
1271	int ret = 0;
1272
1273	dev_dbg(dev, "%s()\n", __func__);
1274
1275	genpd = dev_to_genpd(dev);
1276	if (IS_ERR(genpd))
1277		return -EINVAL;
1278
1279	/*
 
 
 
 
1280	 * At this point suspended_count == 0 means we are being run for the
1281	 * first time for the given domain in the present cycle.
1282	 */
1283	genpd_lock(genpd);
1284	if (genpd->suspended_count++ == 0)
1285		/*
1286		 * The boot kernel might put the domain into arbitrary state,
1287		 * so make it appear as powered off to genpd_sync_power_on(),
1288		 * so that it tries to power it on in case it was really off.
1289		 */
1290		genpd->status = GPD_STATE_POWER_OFF;
 
 
 
 
 
 
 
1291
1292	genpd_sync_power_on(genpd, true, 0);
1293	genpd_unlock(genpd);
1294
1295	if (genpd->dev_ops.stop && genpd->dev_ops.start &&
1296	    !pm_runtime_status_suspended(dev)) {
1297		ret = genpd_start_dev(genpd, dev);
1298		if (ret)
1299			return ret;
1300	}
1301
1302	return pm_generic_restore_noirq(dev);
 
 
 
 
 
1303}
1304
1305/**
1306 * genpd_complete - Complete power transition of a device in a power domain.
1307 * @dev: Device to complete the transition of.
1308 *
1309 * Complete a power transition of a device (during a system-wide power
1310 * transition) under the assumption that its pm_domain field points to the
1311 * domain member of an object of type struct generic_pm_domain representing
1312 * a power domain consisting of I/O devices.
1313 */
1314static void genpd_complete(struct device *dev)
1315{
1316	struct generic_pm_domain *genpd;
 
1317
1318	dev_dbg(dev, "%s()\n", __func__);
1319
1320	genpd = dev_to_genpd(dev);
1321	if (IS_ERR(genpd))
1322		return;
1323
1324	pm_generic_complete(dev);
1325
1326	genpd_lock(genpd);
 
 
1327
1328	genpd->prepared_count--;
1329	if (!genpd->prepared_count)
1330		genpd_queue_power_off_work(genpd);
1331
1332	genpd_unlock(genpd);
 
 
 
 
 
1333}
1334
1335/**
1336 * genpd_syscore_switch - Switch power during system core suspend or resume.
1337 * @dev: Device that normally is marked as "always on" to switch power for.
1338 *
1339 * This routine may only be called during the system core (syscore) suspend or
1340 * resume phase for devices whose "always on" flags are set.
1341 */
1342static void genpd_syscore_switch(struct device *dev, bool suspend)
1343{
1344	struct generic_pm_domain *genpd;
1345
1346	genpd = dev_to_genpd_safe(dev);
1347	if (!genpd)
1348		return;
1349
1350	if (suspend) {
1351		genpd->suspended_count++;
1352		genpd_sync_power_off(genpd, false, 0);
1353	} else {
1354		genpd_sync_power_on(genpd, false, 0);
1355		genpd->suspended_count--;
1356	}
1357}
 
1358
1359void pm_genpd_syscore_poweroff(struct device *dev)
1360{
1361	genpd_syscore_switch(dev, true);
1362}
1363EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweroff);
1364
1365void pm_genpd_syscore_poweron(struct device *dev)
1366{
1367	genpd_syscore_switch(dev, false);
1368}
1369EXPORT_SYMBOL_GPL(pm_genpd_syscore_poweron);
1370
1371#else /* !CONFIG_PM_SLEEP */
1372
1373#define genpd_prepare		NULL
1374#define genpd_suspend_noirq	NULL
1375#define genpd_resume_noirq	NULL
1376#define genpd_freeze_noirq	NULL
1377#define genpd_thaw_noirq	NULL
1378#define genpd_poweroff_noirq	NULL
1379#define genpd_restore_noirq	NULL
1380#define genpd_complete		NULL
 
 
 
 
 
 
 
1381
1382#endif /* CONFIG_PM_SLEEP */
1383
1384static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev)
1385{
1386	struct generic_pm_domain_data *gpd_data;
1387	int ret;
1388
1389	ret = dev_pm_get_subsys_data(dev);
1390	if (ret)
1391		return ERR_PTR(ret);
1392
1393	gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1394	if (!gpd_data) {
1395		ret = -ENOMEM;
1396		goto err_put;
1397	}
1398
1399	gpd_data->base.dev = dev;
1400	gpd_data->td.constraint_changed = true;
1401	gpd_data->td.effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
1402	gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1403
1404	spin_lock_irq(&dev->power.lock);
1405
1406	if (dev->power.subsys_data->domain_data) {
1407		ret = -EINVAL;
1408		goto err_free;
1409	}
1410
1411	dev->power.subsys_data->domain_data = &gpd_data->base;
1412
1413	spin_unlock_irq(&dev->power.lock);
1414
1415	return gpd_data;
1416
1417 err_free:
1418	spin_unlock_irq(&dev->power.lock);
1419	kfree(gpd_data);
1420 err_put:
1421	dev_pm_put_subsys_data(dev);
1422	return ERR_PTR(ret);
1423}
1424
1425static void genpd_free_dev_data(struct device *dev,
1426				struct generic_pm_domain_data *gpd_data)
1427{
1428	spin_lock_irq(&dev->power.lock);
1429
1430	dev->power.subsys_data->domain_data = NULL;
1431
1432	spin_unlock_irq(&dev->power.lock);
1433
1434	kfree(gpd_data);
1435	dev_pm_put_subsys_data(dev);
1436}
1437
1438static void genpd_update_cpumask(struct generic_pm_domain *genpd,
1439				 int cpu, bool set, unsigned int depth)
1440{
1441	struct gpd_link *link;
1442
1443	if (!genpd_is_cpu_domain(genpd))
1444		return;
1445
1446	list_for_each_entry(link, &genpd->child_links, child_node) {
1447		struct generic_pm_domain *parent = link->parent;
1448
1449		genpd_lock_nested(parent, depth + 1);
1450		genpd_update_cpumask(parent, cpu, set, depth + 1);
1451		genpd_unlock(parent);
1452	}
1453
1454	if (set)
1455		cpumask_set_cpu(cpu, genpd->cpus);
1456	else
1457		cpumask_clear_cpu(cpu, genpd->cpus);
1458}
1459
1460static void genpd_set_cpumask(struct generic_pm_domain *genpd, int cpu)
1461{
1462	if (cpu >= 0)
1463		genpd_update_cpumask(genpd, cpu, true, 0);
1464}
1465
1466static void genpd_clear_cpumask(struct generic_pm_domain *genpd, int cpu)
1467{
1468	if (cpu >= 0)
1469		genpd_update_cpumask(genpd, cpu, false, 0);
1470}
1471
1472static int genpd_get_cpu(struct generic_pm_domain *genpd, struct device *dev)
1473{
1474	int cpu;
1475
1476	if (!genpd_is_cpu_domain(genpd))
1477		return -1;
1478
1479	for_each_possible_cpu(cpu) {
1480		if (get_cpu_device(cpu) == dev)
1481			return cpu;
1482	}
1483
1484	return -1;
1485}
1486
1487static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1488			    struct device *base_dev)
1489{
1490	struct generic_pm_domain_data *gpd_data;
1491	int ret;
 
1492
1493	dev_dbg(dev, "%s()\n", __func__);
1494
1495	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1496		return -EINVAL;
1497
1498	gpd_data = genpd_alloc_dev_data(dev);
1499	if (IS_ERR(gpd_data))
1500		return PTR_ERR(gpd_data);
1501
1502	gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
1503
1504	ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
1505	if (ret)
1506		goto out;
 
1507
1508	genpd_lock(genpd);
 
 
 
 
1509
1510	genpd_set_cpumask(genpd, gpd_data->cpu);
1511	dev_pm_domain_set(dev, &genpd->domain);
 
1512
1513	genpd->device_count++;
1514	genpd->max_off_time_changed = true;
1515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1516	list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
 
 
 
 
1517
1518	genpd_unlock(genpd);
1519 out:
1520	if (ret)
1521		genpd_free_dev_data(dev, gpd_data);
1522	else
1523		dev_pm_qos_add_notifier(dev, &gpd_data->nb,
1524					DEV_PM_QOS_RESUME_LATENCY);
1525
1526	return ret;
1527}
1528
1529/**
1530 * pm_genpd_add_device - Add a device to an I/O PM domain.
1531 * @genpd: PM domain to add the device to.
 
1532 * @dev: Device to be added.
 
1533 */
1534int pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev)
 
1535{
1536	int ret;
 
 
 
 
 
1537
1538	mutex_lock(&gpd_list_lock);
1539	ret = genpd_add_device(genpd, dev, dev);
 
 
 
 
 
1540	mutex_unlock(&gpd_list_lock);
1541
1542	return ret;
 
 
 
1543}
1544EXPORT_SYMBOL_GPL(pm_genpd_add_device);
1545
1546static int genpd_remove_device(struct generic_pm_domain *genpd,
1547			       struct device *dev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1548{
1549	struct generic_pm_domain_data *gpd_data;
1550	struct pm_domain_data *pdd;
 
1551	int ret = 0;
1552
1553	dev_dbg(dev, "%s()\n", __func__);
1554
1555	pdd = dev->power.subsys_data->domain_data;
1556	gpd_data = to_gpd_data(pdd);
1557	dev_pm_qos_remove_notifier(dev, &gpd_data->nb,
1558				   DEV_PM_QOS_RESUME_LATENCY);
1559
1560	genpd_lock(genpd);
1561
1562	if (genpd->prepared_count > 0) {
1563		ret = -EAGAIN;
1564		goto out;
1565	}
1566
1567	genpd->device_count--;
1568	genpd->max_off_time_changed = true;
1569
1570	genpd_clear_cpumask(genpd, gpd_data->cpu);
1571	dev_pm_domain_set(dev, NULL);
1572
 
 
1573	list_del_init(&pdd->list_node);
 
 
 
 
 
1574
1575	genpd_unlock(genpd);
1576
1577	if (genpd->detach_dev)
1578		genpd->detach_dev(genpd, dev);
 
1579
1580	genpd_free_dev_data(dev, gpd_data);
 
 
 
 
1581
1582	return 0;
1583
1584 out:
1585	genpd_unlock(genpd);
1586	dev_pm_qos_add_notifier(dev, &gpd_data->nb, DEV_PM_QOS_RESUME_LATENCY);
1587
1588	return ret;
1589}
1590
1591/**
1592 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1593 * @dev: Device to be removed.
 
1594 */
1595int pm_genpd_remove_device(struct device *dev)
1596{
1597	struct generic_pm_domain *genpd = dev_to_genpd_safe(dev);
 
1598
1599	if (!genpd)
1600		return -EINVAL;
 
 
 
1601
1602	return genpd_remove_device(genpd, dev);
1603}
1604EXPORT_SYMBOL_GPL(pm_genpd_remove_device);
1605
1606static int genpd_add_subdomain(struct generic_pm_domain *genpd,
1607			       struct generic_pm_domain *subdomain)
 
 
 
 
 
1608{
1609	struct gpd_link *link, *itr;
1610	int ret = 0;
1611
1612	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1613	    || genpd == subdomain)
1614		return -EINVAL;
1615
1616	/*
1617	 * If the domain can be powered on/off in an IRQ safe
1618	 * context, ensure that the subdomain can also be
1619	 * powered on/off in that context.
1620	 */
1621	if (!genpd_is_irq_safe(genpd) && genpd_is_irq_safe(subdomain)) {
1622		WARN(1, "Parent %s of subdomain %s must be IRQ safe\n",
1623				genpd->name, subdomain->name);
1624		return -EINVAL;
1625	}
1626
1627	link = kzalloc(sizeof(*link), GFP_KERNEL);
1628	if (!link)
1629		return -ENOMEM;
1630
1631	genpd_lock(subdomain);
1632	genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1633
1634	if (!genpd_status_on(genpd) && genpd_status_on(subdomain)) {
1635		ret = -EINVAL;
1636		goto out;
1637	}
1638
1639	list_for_each_entry(itr, &genpd->parent_links, parent_node) {
1640		if (itr->child == subdomain && itr->parent == genpd) {
1641			ret = -EINVAL;
1642			goto out;
1643		}
1644	}
1645
1646	link->parent = genpd;
1647	list_add_tail(&link->parent_node, &genpd->parent_links);
1648	link->child = subdomain;
1649	list_add_tail(&link->child_node, &subdomain->child_links);
1650	if (genpd_status_on(subdomain))
 
 
 
 
 
1651		genpd_sd_counter_inc(genpd);
1652
1653 out:
1654	genpd_unlock(genpd);
1655	genpd_unlock(subdomain);
1656	if (ret)
1657		kfree(link);
1658	return ret;
1659}
1660
1661/**
1662 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1663 * @genpd: Leader PM domain to add the subdomain to.
1664 * @subdomain: Subdomain to be added.
1665 */
1666int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1667			   struct generic_pm_domain *subdomain)
1668{
1669	int ret;
 
 
 
1670
1671	mutex_lock(&gpd_list_lock);
1672	ret = genpd_add_subdomain(genpd, subdomain);
 
 
 
 
 
 
 
 
 
1673	mutex_unlock(&gpd_list_lock);
1674
1675	return ret;
1676}
1677EXPORT_SYMBOL_GPL(pm_genpd_add_subdomain);
1678
1679/**
1680 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1681 * @genpd: Leader PM domain to remove the subdomain from.
1682 * @subdomain: Subdomain to be removed.
1683 */
1684int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1685			      struct generic_pm_domain *subdomain)
1686{
1687	struct gpd_link *l, *link;
1688	int ret = -EINVAL;
1689
1690	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1691		return -EINVAL;
1692
1693	genpd_lock(subdomain);
1694	genpd_lock_nested(genpd, SINGLE_DEPTH_NESTING);
1695
1696	if (!list_empty(&subdomain->parent_links) || subdomain->device_count) {
1697		pr_warn("%s: unable to remove subdomain %s\n",
1698			genpd->name, subdomain->name);
1699		ret = -EBUSY;
1700		goto out;
1701	}
1702
1703	list_for_each_entry_safe(link, l, &genpd->parent_links, parent_node) {
1704		if (link->child != subdomain)
1705			continue;
1706
1707		list_del(&link->parent_node);
1708		list_del(&link->child_node);
 
 
 
 
 
 
 
 
 
1709		kfree(link);
1710		if (genpd_status_on(subdomain))
1711			genpd_sd_counter_dec(genpd);
1712
 
 
1713		ret = 0;
1714		break;
1715	}
1716
1717out:
1718	genpd_unlock(genpd);
1719	genpd_unlock(subdomain);
1720
1721	return ret;
1722}
1723EXPORT_SYMBOL_GPL(pm_genpd_remove_subdomain);
1724
1725static void genpd_free_default_power_state(struct genpd_power_state *states,
1726					   unsigned int state_count)
1727{
1728	kfree(states);
1729}
1730
1731static int genpd_set_default_power_state(struct generic_pm_domain *genpd)
1732{
1733	struct genpd_power_state *state;
1734
1735	state = kzalloc(sizeof(*state), GFP_KERNEL);
1736	if (!state)
1737		return -ENOMEM;
1738
1739	genpd->states = state;
1740	genpd->state_count = 1;
1741	genpd->free_states = genpd_free_default_power_state;
1742
1743	return 0;
1744}
1745
1746static void genpd_lock_init(struct generic_pm_domain *genpd)
1747{
1748	if (genpd->flags & GENPD_FLAG_IRQ_SAFE) {
1749		spin_lock_init(&genpd->slock);
1750		genpd->lock_ops = &genpd_spin_ops;
1751	} else {
1752		mutex_init(&genpd->mlock);
1753		genpd->lock_ops = &genpd_mtx_ops;
1754	}
1755}
1756
1757/**
1758 * pm_genpd_init - Initialize a generic I/O PM domain object.
1759 * @genpd: PM domain object to initialize.
1760 * @gov: PM domain governor to associate with the domain (may be NULL).
1761 * @is_off: Initial value of the domain's power_is_off field.
1762 *
1763 * Returns 0 on successful initialization, else a negative error code.
 
1764 */
1765int pm_genpd_init(struct generic_pm_domain *genpd,
1766		  struct dev_power_governor *gov, bool is_off)
1767{
1768	int ret;
 
1769
1770	if (IS_ERR_OR_NULL(genpd))
1771		return -EINVAL;
1772
1773	INIT_LIST_HEAD(&genpd->parent_links);
1774	INIT_LIST_HEAD(&genpd->child_links);
1775	INIT_LIST_HEAD(&genpd->dev_list);
1776	genpd_lock_init(genpd);
1777	genpd->gov = gov;
1778	INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1779	atomic_set(&genpd->sd_count, 0);
1780	genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1781	genpd->device_count = 0;
1782	genpd->max_off_time_ns = -1;
1783	genpd->max_off_time_changed = true;
1784	genpd->provider = NULL;
1785	genpd->has_provider = false;
1786	genpd->accounting_time = ktime_get();
1787	genpd->domain.ops.runtime_suspend = genpd_runtime_suspend;
1788	genpd->domain.ops.runtime_resume = genpd_runtime_resume;
1789	genpd->domain.ops.prepare = genpd_prepare;
1790	genpd->domain.ops.suspend_noirq = genpd_suspend_noirq;
1791	genpd->domain.ops.resume_noirq = genpd_resume_noirq;
1792	genpd->domain.ops.freeze_noirq = genpd_freeze_noirq;
1793	genpd->domain.ops.thaw_noirq = genpd_thaw_noirq;
1794	genpd->domain.ops.poweroff_noirq = genpd_poweroff_noirq;
1795	genpd->domain.ops.restore_noirq = genpd_restore_noirq;
1796	genpd->domain.ops.complete = genpd_complete;
1797	genpd->domain.start = genpd_dev_pm_start;
1798
1799	if (genpd->flags & GENPD_FLAG_PM_CLK) {
1800		genpd->dev_ops.stop = pm_clk_suspend;
1801		genpd->dev_ops.start = pm_clk_resume;
1802	}
1803
1804	/* Always-on domains must be powered on at initialization. */
1805	if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
1806			!genpd_status_on(genpd))
1807		return -EINVAL;
1808
1809	if (genpd_is_cpu_domain(genpd) &&
1810	    !zalloc_cpumask_var(&genpd->cpus, GFP_KERNEL))
1811		return -ENOMEM;
1812
1813	/* Use only one "off" state if there were no states declared */
1814	if (genpd->state_count == 0) {
1815		ret = genpd_set_default_power_state(genpd);
1816		if (ret) {
1817			if (genpd_is_cpu_domain(genpd))
1818				free_cpumask_var(genpd->cpus);
1819			return ret;
1820		}
1821	} else if (!gov && genpd->state_count > 1) {
1822		pr_warn("%s: no governor for states\n", genpd->name);
1823	}
1824
1825	device_initialize(&genpd->dev);
1826	dev_set_name(&genpd->dev, "%s", genpd->name);
1827
1828	mutex_lock(&gpd_list_lock);
1829	list_add(&genpd->gpd_list_node, &gpd_list);
1830	mutex_unlock(&gpd_list_lock);
1831
1832	return 0;
1833}
1834EXPORT_SYMBOL_GPL(pm_genpd_init);
1835
1836static int genpd_remove(struct generic_pm_domain *genpd)
1837{
1838	struct gpd_link *l, *link;
1839
1840	if (IS_ERR_OR_NULL(genpd))
1841		return -EINVAL;
1842
1843	genpd_lock(genpd);
1844
1845	if (genpd->has_provider) {
1846		genpd_unlock(genpd);
1847		pr_err("Provider present, unable to remove %s\n", genpd->name);
1848		return -EBUSY;
1849	}
1850
1851	if (!list_empty(&genpd->parent_links) || genpd->device_count) {
1852		genpd_unlock(genpd);
1853		pr_err("%s: unable to remove %s\n", __func__, genpd->name);
1854		return -EBUSY;
1855	}
1856
1857	list_for_each_entry_safe(link, l, &genpd->child_links, child_node) {
1858		list_del(&link->parent_node);
1859		list_del(&link->child_node);
1860		kfree(link);
 
1861	}
 
 
 
 
1862
1863	list_del(&genpd->gpd_list_node);
1864	genpd_unlock(genpd);
1865	cancel_work_sync(&genpd->power_off_work);
1866	if (genpd_is_cpu_domain(genpd))
1867		free_cpumask_var(genpd->cpus);
1868	if (genpd->free_states)
1869		genpd->free_states(genpd->states, genpd->state_count);
1870
1871	pr_debug("%s: removed %s\n", __func__, genpd->name);
1872
1873	return 0;
1874}
1875
1876/**
1877 * pm_genpd_remove - Remove a generic I/O PM domain
1878 * @genpd: Pointer to PM domain that is to be removed.
1879 *
1880 * To remove the PM domain, this function:
1881 *  - Removes the PM domain as a subdomain to any parent domains,
1882 *    if it was added.
1883 *  - Removes the PM domain from the list of registered PM domains.
1884 *
1885 * The PM domain will only be removed, if the associated provider has
1886 * been removed, it is not a parent to any other PM domain and has no
1887 * devices associated with it.
1888 */
1889int pm_genpd_remove(struct generic_pm_domain *genpd)
1890{
1891	int ret;
1892
1893	mutex_lock(&gpd_list_lock);
1894	ret = genpd_remove(genpd);
1895	mutex_unlock(&gpd_list_lock);
1896
1897	return ret;
1898}
1899EXPORT_SYMBOL_GPL(pm_genpd_remove);
1900
1901#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
1902
1903/*
1904 * Device Tree based PM domain providers.
1905 *
1906 * The code below implements generic device tree based PM domain providers that
1907 * bind device tree nodes with generic PM domains registered in the system.
1908 *
1909 * Any driver that registers generic PM domains and needs to support binding of
1910 * devices to these domains is supposed to register a PM domain provider, which
1911 * maps a PM domain specifier retrieved from the device tree to a PM domain.
1912 *
1913 * Two simple mapping functions have been provided for convenience:
1914 *  - genpd_xlate_simple() for 1:1 device tree node to PM domain mapping.
1915 *  - genpd_xlate_onecell() for mapping of multiple PM domains per node by
1916 *    index.
1917 */
1918
1919/**
1920 * struct of_genpd_provider - PM domain provider registration structure
1921 * @link: Entry in global list of PM domain providers
1922 * @node: Pointer to device tree node of PM domain provider
1923 * @xlate: Provider-specific xlate callback mapping a set of specifier cells
1924 *         into a PM domain.
1925 * @data: context pointer to be passed into @xlate callback
1926 */
1927struct of_genpd_provider {
1928	struct list_head link;
1929	struct device_node *node;
1930	genpd_xlate_t xlate;
1931	void *data;
1932};
1933
1934/* List of registered PM domain providers. */
1935static LIST_HEAD(of_genpd_providers);
1936/* Mutex to protect the list above. */
1937static DEFINE_MUTEX(of_genpd_mutex);
1938
1939/**
1940 * genpd_xlate_simple() - Xlate function for direct node-domain mapping
1941 * @genpdspec: OF phandle args to map into a PM domain
1942 * @data: xlate function private data - pointer to struct generic_pm_domain
1943 *
1944 * This is a generic xlate function that can be used to model PM domains that
1945 * have their own device tree nodes. The private data of xlate function needs
1946 * to be a valid pointer to struct generic_pm_domain.
1947 */
1948static struct generic_pm_domain *genpd_xlate_simple(
1949					struct of_phandle_args *genpdspec,
1950					void *data)
1951{
1952	return data;
1953}
1954
1955/**
1956 * genpd_xlate_onecell() - Xlate function using a single index.
1957 * @genpdspec: OF phandle args to map into a PM domain
1958 * @data: xlate function private data - pointer to struct genpd_onecell_data
1959 *
1960 * This is a generic xlate function that can be used to model simple PM domain
1961 * controllers that have one device tree node and provide multiple PM domains.
1962 * A single cell is used as an index into an array of PM domains specified in
1963 * the genpd_onecell_data struct when registering the provider.
1964 */
1965static struct generic_pm_domain *genpd_xlate_onecell(
1966					struct of_phandle_args *genpdspec,
1967					void *data)
1968{
1969	struct genpd_onecell_data *genpd_data = data;
1970	unsigned int idx = genpdspec->args[0];
1971
1972	if (genpdspec->args_count != 1)
1973		return ERR_PTR(-EINVAL);
1974
1975	if (idx >= genpd_data->num_domains) {
1976		pr_err("%s: invalid domain index %u\n", __func__, idx);
1977		return ERR_PTR(-EINVAL);
1978	}
1979
1980	if (!genpd_data->domains[idx])
1981		return ERR_PTR(-ENOENT);
1982
1983	return genpd_data->domains[idx];
1984}
1985
1986/**
1987 * genpd_add_provider() - Register a PM domain provider for a node
1988 * @np: Device node pointer associated with the PM domain provider.
1989 * @xlate: Callback for decoding PM domain from phandle arguments.
1990 * @data: Context pointer for @xlate callback.
1991 */
1992static int genpd_add_provider(struct device_node *np, genpd_xlate_t xlate,
1993			      void *data)
1994{
1995	struct of_genpd_provider *cp;
1996
1997	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1998	if (!cp)
1999		return -ENOMEM;
2000
2001	cp->node = of_node_get(np);
2002	cp->data = data;
2003	cp->xlate = xlate;
2004
2005	mutex_lock(&of_genpd_mutex);
2006	list_add(&cp->link, &of_genpd_providers);
2007	mutex_unlock(&of_genpd_mutex);
2008	pr_debug("Added domain provider from %pOF\n", np);
2009
2010	return 0;
2011}
2012
2013static bool genpd_present(const struct generic_pm_domain *genpd)
2014{
2015	const struct generic_pm_domain *gpd;
2016
2017	list_for_each_entry(gpd, &gpd_list, gpd_list_node)
2018		if (gpd == genpd)
2019			return true;
2020	return false;
2021}
2022
2023/**
2024 * of_genpd_add_provider_simple() - Register a simple PM domain provider
2025 * @np: Device node pointer associated with the PM domain provider.
2026 * @genpd: Pointer to PM domain associated with the PM domain provider.
2027 */
2028int of_genpd_add_provider_simple(struct device_node *np,
2029				 struct generic_pm_domain *genpd)
2030{
2031	int ret = -EINVAL;
2032
2033	if (!np || !genpd)
2034		return -EINVAL;
2035
2036	mutex_lock(&gpd_list_lock);
 
2037
2038	if (!genpd_present(genpd))
2039		goto unlock;
2040
2041	genpd->dev.of_node = np;
2042
2043	/* Parse genpd OPP table */
2044	if (genpd->set_performance_state) {
2045		ret = dev_pm_opp_of_add_table(&genpd->dev);
2046		if (ret) {
2047			dev_err(&genpd->dev, "Failed to add OPP table: %d\n",
2048				ret);
2049			goto unlock;
 
 
2050		}
2051
2052		/*
2053		 * Save table for faster processing while setting performance
2054		 * state.
2055		 */
2056		genpd->opp_table = dev_pm_opp_get_opp_table(&genpd->dev);
2057		WARN_ON(!genpd->opp_table);
2058	}
2059
2060	ret = genpd_add_provider(np, genpd_xlate_simple, genpd);
2061	if (ret) {
2062		if (genpd->set_performance_state) {
2063			dev_pm_opp_put_opp_table(genpd->opp_table);
2064			dev_pm_opp_of_remove_table(&genpd->dev);
2065		}
2066
2067		goto unlock;
2068	}
2069
2070	genpd->provider = &np->fwnode;
2071	genpd->has_provider = true;
2072
2073unlock:
2074	mutex_unlock(&gpd_list_lock);
 
2075
2076	return ret;
2077}
2078EXPORT_SYMBOL_GPL(of_genpd_add_provider_simple);
2079
2080/**
2081 * of_genpd_add_provider_onecell() - Register a onecell PM domain provider
2082 * @np: Device node pointer associated with the PM domain provider.
2083 * @data: Pointer to the data associated with the PM domain provider.
 
 
 
 
2084 */
2085int of_genpd_add_provider_onecell(struct device_node *np,
2086				  struct genpd_onecell_data *data)
2087{
2088	struct generic_pm_domain *genpd;
2089	unsigned int i;
2090	int ret = -EINVAL;
 
2091
2092	if (!np || !data)
2093		return -EINVAL;
2094
2095	mutex_lock(&gpd_list_lock);
2096
2097	if (!data->xlate)
2098		data->xlate = genpd_xlate_onecell;
2099
2100	for (i = 0; i < data->num_domains; i++) {
2101		genpd = data->domains[i];
2102
2103		if (!genpd)
2104			continue;
2105		if (!genpd_present(genpd))
2106			goto error;
2107
2108		genpd->dev.of_node = np;
2109
2110		/* Parse genpd OPP table */
2111		if (genpd->set_performance_state) {
2112			ret = dev_pm_opp_of_add_table_indexed(&genpd->dev, i);
2113			if (ret) {
2114				dev_err(&genpd->dev, "Failed to add OPP table for index %d: %d\n",
2115					i, ret);
2116				goto error;
2117			}
2118
2119			/*
2120			 * Save table for faster processing while setting
2121			 * performance state.
2122			 */
2123			genpd->opp_table = dev_pm_opp_get_opp_table_indexed(&genpd->dev, i);
2124			WARN_ON(!genpd->opp_table);
2125		}
2126
2127		genpd->provider = &np->fwnode;
2128		genpd->has_provider = true;
 
2129	}
2130
2131	ret = genpd_add_provider(np, data->xlate, data);
2132	if (ret < 0)
2133		goto error;
2134
2135	mutex_unlock(&gpd_list_lock);
2136
2137	return 0;
2138
2139error:
2140	while (i--) {
2141		genpd = data->domains[i];
2142
2143		if (!genpd)
2144			continue;
2145
2146		genpd->provider = NULL;
2147		genpd->has_provider = false;
2148
2149		if (genpd->set_performance_state) {
2150			dev_pm_opp_put_opp_table(genpd->opp_table);
2151			dev_pm_opp_of_remove_table(&genpd->dev);
2152		}
2153	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2154
2155	mutex_unlock(&gpd_list_lock);
2156
2157	return ret;
2158}
2159EXPORT_SYMBOL_GPL(of_genpd_add_provider_onecell);
2160
2161/**
2162 * of_genpd_del_provider() - Remove a previously registered PM domain provider
2163 * @np: Device node pointer associated with the PM domain provider
2164 */
2165void of_genpd_del_provider(struct device_node *np)
2166{
2167	struct of_genpd_provider *cp, *tmp;
2168	struct generic_pm_domain *gpd;
2169
2170	mutex_lock(&gpd_list_lock);
2171	mutex_lock(&of_genpd_mutex);
2172	list_for_each_entry_safe(cp, tmp, &of_genpd_providers, link) {
2173		if (cp->node == np) {
2174			/*
2175			 * For each PM domain associated with the
2176			 * provider, set the 'has_provider' to false
2177			 * so that the PM domain can be safely removed.
2178			 */
2179			list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
2180				if (gpd->provider == &np->fwnode) {
2181					gpd->has_provider = false;
2182
2183					if (!gpd->set_performance_state)
2184						continue;
2185
2186					dev_pm_opp_put_opp_table(gpd->opp_table);
2187					dev_pm_opp_of_remove_table(&gpd->dev);
2188				}
2189			}
2190
2191			list_del(&cp->link);
2192			of_node_put(cp->node);
2193			kfree(cp);
2194			break;
2195		}
2196	}
2197	mutex_unlock(&of_genpd_mutex);
2198	mutex_unlock(&gpd_list_lock);
2199}
2200EXPORT_SYMBOL_GPL(of_genpd_del_provider);
2201
2202/**
2203 * genpd_get_from_provider() - Look-up PM domain
2204 * @genpdspec: OF phandle args to use for look-up
2205 *
2206 * Looks for a PM domain provider under the node specified by @genpdspec and if
2207 * found, uses xlate function of the provider to map phandle args to a PM
2208 * domain.
2209 *
2210 * Returns a valid pointer to struct generic_pm_domain on success or ERR_PTR()
2211 * on failure.
2212 */
2213static struct generic_pm_domain *genpd_get_from_provider(
2214					struct of_phandle_args *genpdspec)
2215{
2216	struct generic_pm_domain *genpd = ERR_PTR(-ENOENT);
2217	struct of_genpd_provider *provider;
2218
2219	if (!genpdspec)
2220		return ERR_PTR(-EINVAL);
2221
2222	mutex_lock(&of_genpd_mutex);
2223
2224	/* Check if we have such a provider in our array */
2225	list_for_each_entry(provider, &of_genpd_providers, link) {
2226		if (provider->node == genpdspec->np)
2227			genpd = provider->xlate(genpdspec, provider->data);
2228		if (!IS_ERR(genpd))
2229			break;
2230	}
2231
2232	mutex_unlock(&of_genpd_mutex);
2233
2234	return genpd;
2235}
2236
2237/**
2238 * of_genpd_add_device() - Add a device to an I/O PM domain
2239 * @genpdspec: OF phandle args to use for look-up PM domain
2240 * @dev: Device to be added.
2241 *
2242 * Looks-up an I/O PM domain based upon phandle args provided and adds
2243 * the device to the PM domain. Returns a negative error code on failure.
2244 */
2245int of_genpd_add_device(struct of_phandle_args *genpdspec, struct device *dev)
2246{
2247	struct generic_pm_domain *genpd;
2248	int ret;
2249
2250	mutex_lock(&gpd_list_lock);
2251
2252	genpd = genpd_get_from_provider(genpdspec);
2253	if (IS_ERR(genpd)) {
2254		ret = PTR_ERR(genpd);
2255		goto out;
2256	}
2257
2258	ret = genpd_add_device(genpd, dev, dev);
2259
2260out:
2261	mutex_unlock(&gpd_list_lock);
2262
2263	return ret;
2264}
2265EXPORT_SYMBOL_GPL(of_genpd_add_device);
2266
2267/**
2268 * of_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
2269 * @parent_spec: OF phandle args to use for parent PM domain look-up
2270 * @subdomain_spec: OF phandle args to use for subdomain look-up
2271 *
2272 * Looks-up a parent PM domain and subdomain based upon phandle args
2273 * provided and adds the subdomain to the parent PM domain. Returns a
2274 * negative error code on failure.
2275 */
2276int of_genpd_add_subdomain(struct of_phandle_args *parent_spec,
2277			   struct of_phandle_args *subdomain_spec)
2278{
2279	struct generic_pm_domain *parent, *subdomain;
2280	int ret;
2281
2282	mutex_lock(&gpd_list_lock);
2283
2284	parent = genpd_get_from_provider(parent_spec);
2285	if (IS_ERR(parent)) {
2286		ret = PTR_ERR(parent);
2287		goto out;
2288	}
2289
2290	subdomain = genpd_get_from_provider(subdomain_spec);
2291	if (IS_ERR(subdomain)) {
2292		ret = PTR_ERR(subdomain);
2293		goto out;
2294	}
 
 
 
 
2295
2296	ret = genpd_add_subdomain(parent, subdomain);
2297
2298out:
2299	mutex_unlock(&gpd_list_lock);
2300
2301	return ret;
2302}
2303EXPORT_SYMBOL_GPL(of_genpd_add_subdomain);
2304
2305/**
2306 * of_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
2307 * @parent_spec: OF phandle args to use for parent PM domain look-up
2308 * @subdomain_spec: OF phandle args to use for subdomain look-up
2309 *
2310 * Looks-up a parent PM domain and subdomain based upon phandle args
2311 * provided and removes the subdomain from the parent PM domain. Returns a
2312 * negative error code on failure.
2313 */
2314int of_genpd_remove_subdomain(struct of_phandle_args *parent_spec,
2315			      struct of_phandle_args *subdomain_spec)
2316{
2317	struct generic_pm_domain *parent, *subdomain;
2318	int ret;
2319
2320	mutex_lock(&gpd_list_lock);
2321
2322	parent = genpd_get_from_provider(parent_spec);
2323	if (IS_ERR(parent)) {
2324		ret = PTR_ERR(parent);
2325		goto out;
2326	}
2327
2328	subdomain = genpd_get_from_provider(subdomain_spec);
2329	if (IS_ERR(subdomain)) {
2330		ret = PTR_ERR(subdomain);
2331		goto out;
2332	}
2333
2334	ret = pm_genpd_remove_subdomain(parent, subdomain);
2335
2336out:
2337	mutex_unlock(&gpd_list_lock);
2338
2339	return ret;
2340}
2341EXPORT_SYMBOL_GPL(of_genpd_remove_subdomain);
 
2342
2343/**
2344 * of_genpd_remove_last - Remove the last PM domain registered for a provider
2345 * @provider: Pointer to device structure associated with provider
2346 *
2347 * Find the last PM domain that was added by a particular provider and
2348 * remove this PM domain from the list of PM domains. The provider is
2349 * identified by the 'provider' device structure that is passed. The PM
2350 * domain will only be removed, if the provider associated with domain
2351 * has been removed.
2352 *
2353 * Returns a valid pointer to struct generic_pm_domain on success or
2354 * ERR_PTR() on failure.
2355 */
2356struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
2357{
2358	struct generic_pm_domain *gpd, *tmp, *genpd = ERR_PTR(-ENOENT);
2359	int ret;
2360
2361	if (IS_ERR_OR_NULL(np))
2362		return ERR_PTR(-EINVAL);
 
2363
2364	mutex_lock(&gpd_list_lock);
2365	list_for_each_entry_safe(gpd, tmp, &gpd_list, gpd_list_node) {
2366		if (gpd->provider == &np->fwnode) {
2367			ret = genpd_remove(gpd);
2368			genpd = ret ? ERR_PTR(ret) : gpd;
2369			break;
2370		}
2371	}
2372	mutex_unlock(&gpd_list_lock);
2373
2374	return genpd;
2375}
2376EXPORT_SYMBOL_GPL(of_genpd_remove_last);
2377
2378static void genpd_release_dev(struct device *dev)
2379{
2380	of_node_put(dev->of_node);
2381	kfree(dev);
2382}
2383
2384static struct bus_type genpd_bus_type = {
2385	.name		= "genpd",
2386};
2387
2388/**
2389 * genpd_dev_pm_detach - Detach a device from its PM domain.
2390 * @dev: Device to detach.
2391 * @power_off: Currently not used
2392 *
2393 * Try to locate a corresponding generic PM domain, which the device was
2394 * attached to previously. If such is found, the device is detached from it.
2395 */
2396static void genpd_dev_pm_detach(struct device *dev, bool power_off)
2397{
2398	struct generic_pm_domain *pd;
2399	unsigned int i;
2400	int ret = 0;
2401
2402	pd = dev_to_genpd(dev);
2403	if (IS_ERR(pd))
2404		return;
2405
2406	dev_dbg(dev, "removing from PM domain %s\n", pd->name);
2407
2408	for (i = 1; i < GENPD_RETRY_MAX_MS; i <<= 1) {
2409		ret = genpd_remove_device(pd, dev);
2410		if (ret != -EAGAIN)
2411			break;
2412
2413		mdelay(i);
2414		cond_resched();
2415	}
2416
2417	if (ret < 0) {
2418		dev_err(dev, "failed to remove from PM domain %s: %d",
2419			pd->name, ret);
2420		return;
2421	}
2422
2423	/* Check if PM domain can be powered off after removing this device. */
2424	genpd_queue_power_off_work(pd);
 
2425
2426	/* Unregister the device if it was created by genpd. */
2427	if (dev->bus == &genpd_bus_type)
2428		device_unregister(dev);
2429}
2430
2431static void genpd_dev_pm_sync(struct device *dev)
2432{
2433	struct generic_pm_domain *pd;
2434
2435	pd = dev_to_genpd(dev);
2436	if (IS_ERR(pd))
2437		return;
2438
2439	genpd_queue_power_off_work(pd);
2440}
2441
2442static int __genpd_dev_pm_attach(struct device *dev, struct device *base_dev,
2443				 unsigned int index, bool power_on)
2444{
2445	struct of_phandle_args pd_args;
2446	struct generic_pm_domain *pd;
2447	int ret;
2448
2449	ret = of_parse_phandle_with_args(dev->of_node, "power-domains",
2450				"#power-domain-cells", index, &pd_args);
2451	if (ret < 0)
2452		return ret;
2453
2454	mutex_lock(&gpd_list_lock);
2455	pd = genpd_get_from_provider(&pd_args);
2456	of_node_put(pd_args.np);
2457	if (IS_ERR(pd)) {
2458		mutex_unlock(&gpd_list_lock);
2459		dev_dbg(dev, "%s() failed to find PM domain: %ld\n",
2460			__func__, PTR_ERR(pd));
2461		return driver_deferred_probe_check_state(base_dev);
2462	}
2463
2464	dev_dbg(dev, "adding to PM domain %s\n", pd->name);
2465
2466	ret = genpd_add_device(pd, dev, base_dev);
2467	mutex_unlock(&gpd_list_lock);
2468
2469	if (ret < 0) {
2470		if (ret != -EPROBE_DEFER)
2471			dev_err(dev, "failed to add to PM domain %s: %d",
2472				pd->name, ret);
2473		return ret;
2474	}
2475
2476	dev->pm_domain->detach = genpd_dev_pm_detach;
2477	dev->pm_domain->sync = genpd_dev_pm_sync;
2478
2479	if (power_on) {
2480		genpd_lock(pd);
2481		ret = genpd_power_on(pd, 0);
2482		genpd_unlock(pd);
2483	}
2484
2485	if (ret)
2486		genpd_remove_device(pd, dev);
2487
2488	return ret ? -EPROBE_DEFER : 1;
2489}
2490
2491/**
2492 * genpd_dev_pm_attach - Attach a device to its PM domain using DT.
2493 * @dev: Device to attach.
2494 *
2495 * Parse device's OF node to find a PM domain specifier. If such is found,
2496 * attaches the device to retrieved pm_domain ops.
2497 *
2498 * Returns 1 on successfully attached PM domain, 0 when the device don't need a
2499 * PM domain or when multiple power-domains exists for it, else a negative error
2500 * code. Note that if a power-domain exists for the device, but it cannot be
2501 * found or turned on, then return -EPROBE_DEFER to ensure that the device is
2502 * not probed and to re-try again later.
2503 */
2504int genpd_dev_pm_attach(struct device *dev)
2505{
2506	if (!dev->of_node)
2507		return 0;
2508
2509	/*
2510	 * Devices with multiple PM domains must be attached separately, as we
2511	 * can only attach one PM domain per device.
2512	 */
2513	if (of_count_phandle_with_args(dev->of_node, "power-domains",
2514				       "#power-domain-cells") != 1)
2515		return 0;
2516
2517	return __genpd_dev_pm_attach(dev, dev, 0, true);
2518}
2519EXPORT_SYMBOL_GPL(genpd_dev_pm_attach);
2520
2521/**
2522 * genpd_dev_pm_attach_by_id - Associate a device with one of its PM domains.
2523 * @dev: The device used to lookup the PM domain.
2524 * @index: The index of the PM domain.
2525 *
2526 * Parse device's OF node to find a PM domain specifier at the provided @index.
2527 * If such is found, creates a virtual device and attaches it to the retrieved
2528 * pm_domain ops. To deal with detaching of the virtual device, the ->detach()
2529 * callback in the struct dev_pm_domain are assigned to genpd_dev_pm_detach().
2530 *
2531 * Returns the created virtual device if successfully attached PM domain, NULL
2532 * when the device don't need a PM domain, else an ERR_PTR() in case of
2533 * failures. If a power-domain exists for the device, but cannot be found or
2534 * turned on, then ERR_PTR(-EPROBE_DEFER) is returned to ensure that the device
2535 * is not probed and to re-try again later.
2536 */
2537struct device *genpd_dev_pm_attach_by_id(struct device *dev,
2538					 unsigned int index)
2539{
2540	struct device *virt_dev;
2541	int num_domains;
2542	int ret;
2543
2544	if (!dev->of_node)
2545		return NULL;
2546
2547	/* Verify that the index is within a valid range. */
2548	num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
2549						 "#power-domain-cells");
2550	if (index >= num_domains)
2551		return NULL;
2552
2553	/* Allocate and register device on the genpd bus. */
2554	virt_dev = kzalloc(sizeof(*virt_dev), GFP_KERNEL);
2555	if (!virt_dev)
2556		return ERR_PTR(-ENOMEM);
2557
2558	dev_set_name(virt_dev, "genpd:%u:%s", index, dev_name(dev));
2559	virt_dev->bus = &genpd_bus_type;
2560	virt_dev->release = genpd_release_dev;
2561	virt_dev->of_node = of_node_get(dev->of_node);
2562
2563	ret = device_register(virt_dev);
2564	if (ret) {
2565		put_device(virt_dev);
2566		return ERR_PTR(ret);
2567	}
2568
2569	/* Try to attach the device to the PM domain at the specified index. */
2570	ret = __genpd_dev_pm_attach(virt_dev, dev, index, false);
2571	if (ret < 1) {
2572		device_unregister(virt_dev);
2573		return ret ? ERR_PTR(ret) : NULL;
2574	}
2575
2576	pm_runtime_enable(virt_dev);
2577	genpd_queue_power_off_work(dev_to_genpd(virt_dev));
2578
2579	return virt_dev;
2580}
2581EXPORT_SYMBOL_GPL(genpd_dev_pm_attach_by_id);
2582
2583/**
2584 * genpd_dev_pm_attach_by_name - Associate a device with one of its PM domains.
2585 * @dev: The device used to lookup the PM domain.
2586 * @name: The name of the PM domain.
2587 *
2588 * Parse device's OF node to find a PM domain specifier using the
2589 * power-domain-names DT property. For further description see
2590 * genpd_dev_pm_attach_by_id().
2591 */
2592struct device *genpd_dev_pm_attach_by_name(struct device *dev, const char *name)
2593{
2594	int index;
2595
2596	if (!dev->of_node)
2597		return NULL;
2598
2599	index = of_property_match_string(dev->of_node, "power-domain-names",
2600					 name);
2601	if (index < 0)
2602		return NULL;
2603
2604	return genpd_dev_pm_attach_by_id(dev, index);
2605}
2606
2607static const struct of_device_id idle_state_match[] = {
2608	{ .compatible = "domain-idle-state", },
2609	{ }
2610};
2611
2612static int genpd_parse_state(struct genpd_power_state *genpd_state,
2613				    struct device_node *state_node)
2614{
2615	int err;
2616	u32 residency;
2617	u32 entry_latency, exit_latency;
2618
2619	err = of_property_read_u32(state_node, "entry-latency-us",
2620						&entry_latency);
2621	if (err) {
2622		pr_debug(" * %pOF missing entry-latency-us property\n",
2623			 state_node);
2624		return -EINVAL;
2625	}
2626
2627	err = of_property_read_u32(state_node, "exit-latency-us",
2628						&exit_latency);
2629	if (err) {
2630		pr_debug(" * %pOF missing exit-latency-us property\n",
2631			 state_node);
2632		return -EINVAL;
2633	}
2634
2635	err = of_property_read_u32(state_node, "min-residency-us", &residency);
2636	if (!err)
2637		genpd_state->residency_ns = 1000 * residency;
2638
2639	genpd_state->power_on_latency_ns = 1000 * exit_latency;
2640	genpd_state->power_off_latency_ns = 1000 * entry_latency;
2641	genpd_state->fwnode = &state_node->fwnode;
2642
2643	return 0;
2644}
2645
2646static int genpd_iterate_idle_states(struct device_node *dn,
2647				     struct genpd_power_state *states)
 
 
 
2648{
2649	int ret;
2650	struct of_phandle_iterator it;
2651	struct device_node *np;
2652	int i = 0;
2653
2654	ret = of_count_phandle_with_args(dn, "domain-idle-states", NULL);
2655	if (ret <= 0)
2656		return ret == -ENOENT ? 0 : ret;
2657
2658	/* Loop over the phandles until all the requested entry is found */
2659	of_for_each_phandle(&it, ret, dn, "domain-idle-states", NULL, 0) {
2660		np = it.node;
2661		if (!of_match_node(idle_state_match, np))
2662			continue;
2663		if (states) {
2664			ret = genpd_parse_state(&states[i], np);
2665			if (ret) {
2666				pr_err("Parsing idle state node %pOF failed with err %d\n",
2667				       np, ret);
2668				of_node_put(np);
2669				return ret;
2670			}
2671		}
2672		i++;
2673	}
2674
2675	return i;
2676}
2677
2678/**
2679 * of_genpd_parse_idle_states: Return array of idle states for the genpd.
2680 *
2681 * @dn: The genpd device node
2682 * @states: The pointer to which the state array will be saved.
2683 * @n: The count of elements in the array returned from this function.
2684 *
2685 * Returns the device states parsed from the OF node. The memory for the states
2686 * is allocated by this function and is the responsibility of the caller to
2687 * free the memory after use. If any or zero compatible domain idle states is
2688 * found it returns 0 and in case of errors, a negative error code is returned.
2689 */
2690int of_genpd_parse_idle_states(struct device_node *dn,
2691			struct genpd_power_state **states, int *n)
2692{
2693	struct genpd_power_state *st;
2694	int ret;
2695
2696	ret = genpd_iterate_idle_states(dn, NULL);
2697	if (ret < 0)
2698		return ret;
2699
2700	if (!ret) {
2701		*states = NULL;
2702		*n = 0;
2703		return 0;
2704	}
2705
2706	st = kcalloc(ret, sizeof(*st), GFP_KERNEL);
2707	if (!st)
2708		return -ENOMEM;
2709
2710	ret = genpd_iterate_idle_states(dn, st);
2711	if (ret <= 0) {
2712		kfree(st);
2713		return ret < 0 ? ret : -EINVAL;
2714	}
2715
2716	*states = st;
2717	*n = ret;
2718
2719	return 0;
2720}
2721EXPORT_SYMBOL_GPL(of_genpd_parse_idle_states);
2722
2723/**
2724 * pm_genpd_opp_to_performance_state - Gets performance state of the genpd from its OPP node.
2725 *
2726 * @genpd_dev: Genpd's device for which the performance-state needs to be found.
2727 * @opp: struct dev_pm_opp of the OPP for which we need to find performance
2728 *	state.
2729 *
2730 * Returns performance state encoded in the OPP of the genpd. This calls
2731 * platform specific genpd->opp_to_performance_state() callback to translate
2732 * power domain OPP to performance state.
2733 *
2734 * Returns performance state on success and 0 on failure.
2735 */
2736unsigned int pm_genpd_opp_to_performance_state(struct device *genpd_dev,
2737					       struct dev_pm_opp *opp)
2738{
2739	struct generic_pm_domain *genpd = NULL;
2740	int state;
2741
2742	genpd = container_of(genpd_dev, struct generic_pm_domain, dev);
2743
2744	if (unlikely(!genpd->opp_to_performance_state))
2745		return 0;
2746
2747	genpd_lock(genpd);
2748	state = genpd->opp_to_performance_state(genpd, opp);
2749	genpd_unlock(genpd);
2750
2751	return state;
2752}
2753EXPORT_SYMBOL_GPL(pm_genpd_opp_to_performance_state);
2754
2755static int __init genpd_bus_init(void)
2756{
2757	return bus_register(&genpd_bus_type);
2758}
2759core_initcall(genpd_bus_init);
2760
2761#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */
2762
2763
2764/***        debugfs support        ***/
2765
2766#ifdef CONFIG_DEBUG_FS
2767#include <linux/pm.h>
2768#include <linux/device.h>
2769#include <linux/debugfs.h>
2770#include <linux/seq_file.h>
2771#include <linux/init.h>
2772#include <linux/kobject.h>
2773static struct dentry *genpd_debugfs_dir;
2774
2775/*
2776 * TODO: This function is a slightly modified version of rtpm_status_show
2777 * from sysfs.c, so generalize it.
2778 */
2779static void rtpm_status_str(struct seq_file *s, struct device *dev)
2780{
2781	static const char * const status_lookup[] = {
2782		[RPM_ACTIVE] = "active",
2783		[RPM_RESUMING] = "resuming",
2784		[RPM_SUSPENDED] = "suspended",
2785		[RPM_SUSPENDING] = "suspending"
2786	};
2787	const char *p = "";
2788
2789	if (dev->power.runtime_error)
2790		p = "error";
2791	else if (dev->power.disable_depth)
2792		p = "unsupported";
2793	else if (dev->power.runtime_status < ARRAY_SIZE(status_lookup))
2794		p = status_lookup[dev->power.runtime_status];
2795	else
2796		WARN_ON(1);
2797
2798	seq_puts(s, p);
2799}
2800
2801static int genpd_summary_one(struct seq_file *s,
2802			struct generic_pm_domain *genpd)
2803{
2804	static const char * const status_lookup[] = {
2805		[GPD_STATE_ACTIVE] = "on",
2806		[GPD_STATE_POWER_OFF] = "off"
2807	};
2808	struct pm_domain_data *pm_data;
2809	const char *kobj_path;
2810	struct gpd_link *link;
2811	char state[16];
2812	int ret;
2813
2814	ret = genpd_lock_interruptible(genpd);
2815	if (ret)
2816		return -ERESTARTSYS;
2817
2818	if (WARN_ON(genpd->status >= ARRAY_SIZE(status_lookup)))
2819		goto exit;
2820	if (!genpd_status_on(genpd))
2821		snprintf(state, sizeof(state), "%s-%u",
2822			 status_lookup[genpd->status], genpd->state_idx);
2823	else
2824		snprintf(state, sizeof(state), "%s",
2825			 status_lookup[genpd->status]);
2826	seq_printf(s, "%-30s  %-15s ", genpd->name, state);
2827
2828	/*
2829	 * Modifications on the list require holding locks on both
2830	 * parent and child, so we are safe.
2831	 * Also genpd->name is immutable.
2832	 */
2833	list_for_each_entry(link, &genpd->parent_links, parent_node) {
2834		seq_printf(s, "%s", link->child->name);
2835		if (!list_is_last(&link->parent_node, &genpd->parent_links))
2836			seq_puts(s, ", ");
2837	}
2838
2839	list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
2840		kobj_path = kobject_get_path(&pm_data->dev->kobj,
2841				genpd_is_irq_safe(genpd) ?
2842				GFP_ATOMIC : GFP_KERNEL);
2843		if (kobj_path == NULL)
2844			continue;
2845
2846		seq_printf(s, "\n    %-50s  ", kobj_path);
2847		rtpm_status_str(s, pm_data->dev);
2848		kfree(kobj_path);
2849	}
2850
2851	seq_puts(s, "\n");
2852exit:
2853	genpd_unlock(genpd);
 
 
 
 
 
2854
2855	return 0;
2856}
2857
2858static int summary_show(struct seq_file *s, void *data)
 
 
 
 
 
 
 
2859{
2860	struct generic_pm_domain *genpd;
2861	int ret = 0;
2862
2863	seq_puts(s, "domain                          status          children\n");
2864	seq_puts(s, "    /device                                             runtime status\n");
2865	seq_puts(s, "----------------------------------------------------------------------\n");
2866
2867	ret = mutex_lock_interruptible(&gpd_list_lock);
2868	if (ret)
2869		return -ERESTARTSYS;
2870
2871	list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
2872		ret = genpd_summary_one(s, genpd);
2873		if (ret)
2874			break;
2875	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2876	mutex_unlock(&gpd_list_lock);
2877
2878	return ret;
2879}
2880
2881static int status_show(struct seq_file *s, void *data)
2882{
2883	static const char * const status_lookup[] = {
2884		[GPD_STATE_ACTIVE] = "on",
2885		[GPD_STATE_POWER_OFF] = "off"
2886	};
2887
2888	struct generic_pm_domain *genpd = s->private;
2889	int ret = 0;
2890
2891	ret = genpd_lock_interruptible(genpd);
2892	if (ret)
2893		return -ERESTARTSYS;
2894
2895	if (WARN_ON_ONCE(genpd->status >= ARRAY_SIZE(status_lookup)))
2896		goto exit;
2897
2898	if (genpd->status == GPD_STATE_POWER_OFF)
2899		seq_printf(s, "%s-%u\n", status_lookup[genpd->status],
2900			genpd->state_idx);
2901	else
2902		seq_printf(s, "%s\n", status_lookup[genpd->status]);
2903exit:
2904	genpd_unlock(genpd);
2905	return ret;
2906}
2907
2908static int sub_domains_show(struct seq_file *s, void *data)
2909{
2910	struct generic_pm_domain *genpd = s->private;
2911	struct gpd_link *link;
2912	int ret = 0;
2913
2914	ret = genpd_lock_interruptible(genpd);
2915	if (ret)
2916		return -ERESTARTSYS;
2917
2918	list_for_each_entry(link, &genpd->parent_links, parent_node)
2919		seq_printf(s, "%s\n", link->child->name);
2920
2921	genpd_unlock(genpd);
2922	return ret;
2923}
2924
2925static int idle_states_show(struct seq_file *s, void *data)
2926{
2927	struct generic_pm_domain *genpd = s->private;
2928	unsigned int i;
2929	int ret = 0;
2930
2931	ret = genpd_lock_interruptible(genpd);
2932	if (ret)
2933		return -ERESTARTSYS;
2934
2935	seq_puts(s, "State          Time Spent(ms)\n");
2936
2937	for (i = 0; i < genpd->state_count; i++) {
2938		ktime_t delta = 0;
2939		s64 msecs;
2940
2941		if ((genpd->status == GPD_STATE_POWER_OFF) &&
2942				(genpd->state_idx == i))
2943			delta = ktime_sub(ktime_get(), genpd->accounting_time);
2944
2945		msecs = ktime_to_ms(
2946			ktime_add(genpd->states[i].idle_time, delta));
2947		seq_printf(s, "S%-13i %lld\n", i, msecs);
2948	}
2949
2950	genpd_unlock(genpd);
2951	return ret;
2952}
2953
2954static int active_time_show(struct seq_file *s, void *data)
2955{
2956	struct generic_pm_domain *genpd = s->private;
2957	ktime_t delta = 0;
2958	int ret = 0;
2959
2960	ret = genpd_lock_interruptible(genpd);
2961	if (ret)
2962		return -ERESTARTSYS;
2963
2964	if (genpd->status == GPD_STATE_ACTIVE)
2965		delta = ktime_sub(ktime_get(), genpd->accounting_time);
2966
2967	seq_printf(s, "%lld ms\n", ktime_to_ms(
2968				ktime_add(genpd->on_time, delta)));
2969
2970	genpd_unlock(genpd);
2971	return ret;
2972}
2973
2974static int total_idle_time_show(struct seq_file *s, void *data)
2975{
2976	struct generic_pm_domain *genpd = s->private;
2977	ktime_t delta = 0, total = 0;
2978	unsigned int i;
2979	int ret = 0;
2980
2981	ret = genpd_lock_interruptible(genpd);
2982	if (ret)
2983		return -ERESTARTSYS;
2984
2985	for (i = 0; i < genpd->state_count; i++) {
2986
2987		if ((genpd->status == GPD_STATE_POWER_OFF) &&
2988				(genpd->state_idx == i))
2989			delta = ktime_sub(ktime_get(), genpd->accounting_time);
2990
2991		total = ktime_add(total, genpd->states[i].idle_time);
2992	}
2993	total = ktime_add(total, delta);
2994
2995	seq_printf(s, "%lld ms\n", ktime_to_ms(total));
2996
2997	genpd_unlock(genpd);
2998	return ret;
2999}
3000
3001
3002static int devices_show(struct seq_file *s, void *data)
3003{
3004	struct generic_pm_domain *genpd = s->private;
3005	struct pm_domain_data *pm_data;
3006	const char *kobj_path;
3007	int ret = 0;
3008
3009	ret = genpd_lock_interruptible(genpd);
3010	if (ret)
3011		return -ERESTARTSYS;
3012
3013	list_for_each_entry(pm_data, &genpd->dev_list, list_node) {
3014		kobj_path = kobject_get_path(&pm_data->dev->kobj,
3015				genpd_is_irq_safe(genpd) ?
3016				GFP_ATOMIC : GFP_KERNEL);
3017		if (kobj_path == NULL)
3018			continue;
3019
3020		seq_printf(s, "%s\n", kobj_path);
3021		kfree(kobj_path);
3022	}
3023
3024	genpd_unlock(genpd);
3025	return ret;
3026}
3027
3028static int perf_state_show(struct seq_file *s, void *data)
3029{
3030	struct generic_pm_domain *genpd = s->private;
3031
3032	if (genpd_lock_interruptible(genpd))
3033		return -ERESTARTSYS;
3034
3035	seq_printf(s, "%u\n", genpd->performance_state);
3036
3037	genpd_unlock(genpd);
3038	return 0;
3039}
3040
3041DEFINE_SHOW_ATTRIBUTE(summary);
3042DEFINE_SHOW_ATTRIBUTE(status);
3043DEFINE_SHOW_ATTRIBUTE(sub_domains);
3044DEFINE_SHOW_ATTRIBUTE(idle_states);
3045DEFINE_SHOW_ATTRIBUTE(active_time);
3046DEFINE_SHOW_ATTRIBUTE(total_idle_time);
3047DEFINE_SHOW_ATTRIBUTE(devices);
3048DEFINE_SHOW_ATTRIBUTE(perf_state);
3049
3050static int __init genpd_debug_init(void)
3051{
3052	struct dentry *d;
3053	struct generic_pm_domain *genpd;
3054
3055	genpd_debugfs_dir = debugfs_create_dir("pm_genpd", NULL);
3056
3057	debugfs_create_file("pm_genpd_summary", S_IRUGO, genpd_debugfs_dir,
3058			    NULL, &summary_fops);
3059
3060	list_for_each_entry(genpd, &gpd_list, gpd_list_node) {
3061		d = debugfs_create_dir(genpd->name, genpd_debugfs_dir);
3062
3063		debugfs_create_file("current_state", 0444,
3064				d, genpd, &status_fops);
3065		debugfs_create_file("sub_domains", 0444,
3066				d, genpd, &sub_domains_fops);
3067		debugfs_create_file("idle_states", 0444,
3068				d, genpd, &idle_states_fops);
3069		debugfs_create_file("active_time", 0444,
3070				d, genpd, &active_time_fops);
3071		debugfs_create_file("total_idle_time", 0444,
3072				d, genpd, &total_idle_time_fops);
3073		debugfs_create_file("devices", 0444,
3074				d, genpd, &devices_fops);
3075		if (genpd->set_performance_state)
3076			debugfs_create_file("perf_state", 0444,
3077					    d, genpd, &perf_state_fops);
3078	}
3079
3080	return 0;
3081}
3082late_initcall(genpd_debug_init);
3083
3084static void __exit genpd_debug_exit(void)
3085{
3086	debugfs_remove_recursive(genpd_debugfs_dir);
3087}
3088__exitcall(genpd_debug_exit);
3089#endif /* CONFIG_DEBUG_FS */
v3.15
 
   1/*
   2 * drivers/base/power/domain.c - Common code related to device power domains.
   3 *
   4 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
   5 *
   6 * This file is released under the GPLv2.
   7 */
 
   8
 
   9#include <linux/kernel.h>
  10#include <linux/io.h>
 
 
  11#include <linux/pm_runtime.h>
  12#include <linux/pm_domain.h>
  13#include <linux/pm_qos.h>
 
  14#include <linux/slab.h>
  15#include <linux/err.h>
  16#include <linux/sched.h>
  17#include <linux/suspend.h>
  18#include <linux/export.h>
 
 
 
 
 
  19
  20#define GENPD_DEV_CALLBACK(genpd, type, callback, dev)		\
  21({								\
  22	type (*__routine)(struct device *__d); 			\
  23	type __ret = (type)0;					\
  24								\
  25	__routine = genpd->dev_ops.callback; 			\
  26	if (__routine) {					\
  27		__ret = __routine(dev); 			\
  28	} else {						\
  29		__routine = dev_gpd_data(dev)->ops.callback;	\
  30		if (__routine) 					\
  31			__ret = __routine(dev);			\
  32	}							\
  33	__ret;							\
  34})
  35
  36#define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name)	\
  37({										\
  38	ktime_t __start = ktime_get();						\
  39	type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev);		\
  40	s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start));		\
  41	struct gpd_timing_data *__td = &dev_gpd_data(dev)->td;			\
  42	if (!__retval && __elapsed > __td->field) {				\
  43		__td->field = __elapsed;					\
  44		dev_dbg(dev, name " latency exceeded, new value %lld ns\n",	\
  45			__elapsed);						\
  46		genpd->max_off_time_changed = true;				\
  47		__td->constraint_changed = true;				\
  48	}									\
  49	__retval;								\
  50})
  51
  52static LIST_HEAD(gpd_list);
  53static DEFINE_MUTEX(gpd_list_lock);
  54
  55static struct generic_pm_domain *pm_genpd_lookup_name(const char *domain_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  56{
  57	struct generic_pm_domain *genpd = NULL, *gpd;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  58
  59	if (IS_ERR_OR_NULL(domain_name))
 
 
 
 
 
 
 
 
  60		return NULL;
  61
  62	mutex_lock(&gpd_list_lock);
  63	list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
  64		if (!strcmp(gpd->name, domain_name)) {
  65			genpd = gpd;
  66			break;
  67		}
  68	}
  69	mutex_unlock(&gpd_list_lock);
  70	return genpd;
  71}
  72
  73#ifdef CONFIG_PM
  74
  75struct generic_pm_domain *dev_to_genpd(struct device *dev)
 
 
  76{
  77	if (IS_ERR_OR_NULL(dev->pm_domain))
  78		return ERR_PTR(-EINVAL);
  79
  80	return pd_to_genpd(dev->pm_domain);
  81}
  82
  83static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
 
  84{
  85	return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
  86					stop_latency_ns, "stop");
  87}
  88
  89static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
 
  90{
  91	return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
  92					start_latency_ns, "start");
  93}
  94
  95static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
  96{
  97	bool ret = false;
  98
  99	if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
 100		ret = !!atomic_dec_and_test(&genpd->sd_count);
 101
 102	return ret;
 103}
 104
 105static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
 106{
 107	atomic_inc(&genpd->sd_count);
 108	smp_mb__after_atomic_inc();
 109}
 110
 111static void genpd_acquire_lock(struct generic_pm_domain *genpd)
 
 112{
 113	DEFINE_WAIT(wait);
 
 
 
 114
 115	mutex_lock(&genpd->lock);
 116	/*
 117	 * Wait for the domain to transition into either the active,
 118	 * or the power off state.
 
 119	 */
 120	for (;;) {
 121		prepare_to_wait(&genpd->status_wait_queue, &wait,
 122				TASK_UNINTERRUPTIBLE);
 123		if (genpd->status == GPD_STATE_ACTIVE
 124		    || genpd->status == GPD_STATE_POWER_OFF)
 125			break;
 126		mutex_unlock(&genpd->lock);
 127
 128		schedule();
 
 
 
 
 129
 130		mutex_lock(&genpd->lock);
 131	}
 132	finish_wait(&genpd->status_wait_queue, &wait);
 133}
 
 
 
 134
 135static void genpd_release_lock(struct generic_pm_domain *genpd)
 
 136{
 137	mutex_unlock(&genpd->lock);
 138}
 
 
 
 
 
 
 
 
 
 139
 140static void genpd_set_active(struct generic_pm_domain *genpd)
 141{
 142	if (genpd->resume_count == 0)
 143		genpd->status = GPD_STATE_ACTIVE;
 144}
 145
 146static void genpd_recalc_cpu_exit_latency(struct generic_pm_domain *genpd)
 147{
 148	s64 usecs64;
 149
 150	if (!genpd->cpu_data)
 151		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 152
 153	usecs64 = genpd->power_on_latency_ns;
 154	do_div(usecs64, NSEC_PER_USEC);
 155	usecs64 += genpd->cpu_data->saved_exit_latency;
 156	genpd->cpu_data->idle_state->exit_latency = usecs64;
 157}
 158
 159/**
 160 * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
 161 * @genpd: PM domain to power up.
 162 *
 163 * Restore power to @genpd and all of its masters so that it is possible to
 164 * resume a device belonging to it.
 165 */
 166static int __pm_genpd_poweron(struct generic_pm_domain *genpd)
 167	__releases(&genpd->lock) __acquires(&genpd->lock)
 168{
 
 169	struct gpd_link *link;
 170	DEFINE_WAIT(wait);
 171	int ret = 0;
 172
 173	/* If the domain's master is being waited for, we have to wait too. */
 174	for (;;) {
 175		prepare_to_wait(&genpd->status_wait_queue, &wait,
 176				TASK_UNINTERRUPTIBLE);
 177		if (genpd->status != GPD_STATE_WAIT_MASTER)
 178			break;
 179		mutex_unlock(&genpd->lock);
 180
 181		schedule();
 
 
 182
 183		mutex_lock(&genpd->lock);
 184	}
 185	finish_wait(&genpd->status_wait_queue, &wait);
 186
 187	if (genpd->status == GPD_STATE_ACTIVE
 188	    || (genpd->prepared_count > 0 && genpd->suspend_power_off))
 189		return 0;
 
 
 
 190
 191	if (genpd->status != GPD_STATE_POWER_OFF) {
 192		genpd_set_active(genpd);
 193		return 0;
 194	}
 195
 196	if (genpd->cpu_data) {
 197		cpuidle_pause_and_lock();
 198		genpd->cpu_data->idle_state->disabled = true;
 199		cpuidle_resume_and_unlock();
 200		goto out;
 201	}
 202
 203	/*
 204	 * The list is guaranteed not to change while the loop below is being
 205	 * executed, unless one of the masters' .power_on() callbacks fiddles
 206	 * with it.
 207	 */
 208	list_for_each_entry(link, &genpd->slave_links, slave_node) {
 209		genpd_sd_counter_inc(link->master);
 210		genpd->status = GPD_STATE_WAIT_MASTER;
 211
 212		mutex_unlock(&genpd->lock);
 213
 214		ret = pm_genpd_poweron(link->master);
 215
 216		mutex_lock(&genpd->lock);
 217
 218		/*
 219		 * The "wait for parent" status is guaranteed not to change
 220		 * while the master is powering on.
 221		 */
 222		genpd->status = GPD_STATE_POWER_OFF;
 223		wake_up_all(&genpd->status_wait_queue);
 224		if (ret) {
 225			genpd_sd_counter_dec(link->master);
 226			goto err;
 227		}
 228	}
 229
 230	if (genpd->power_on) {
 231		ktime_t time_start = ktime_get();
 232		s64 elapsed_ns;
 233
 234		ret = genpd->power_on(genpd);
 235		if (ret)
 236			goto err;
 237
 238		elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 239		if (elapsed_ns > genpd->power_on_latency_ns) {
 240			genpd->power_on_latency_ns = elapsed_ns;
 241			genpd->max_off_time_changed = true;
 242			genpd_recalc_cpu_exit_latency(genpd);
 243			if (genpd->name)
 244				pr_warning("%s: Power-on latency exceeded, "
 245					"new value %lld ns\n", genpd->name,
 246					elapsed_ns);
 247		}
 248	}
 249
 250 out:
 251	genpd_set_active(genpd);
 252
 253	return 0;
 254
 255 err:
 256	list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
 257		genpd_sd_counter_dec(link->master);
 258
 259	return ret;
 260}
 
 
 
 
 261
 262/**
 263 * pm_genpd_poweron - Restore power to a given PM domain and its masters.
 264 * @genpd: PM domain to power up.
 265 */
 266int pm_genpd_poweron(struct generic_pm_domain *genpd)
 267{
 268	int ret;
 269
 270	mutex_lock(&genpd->lock);
 271	ret = __pm_genpd_poweron(genpd);
 272	mutex_unlock(&genpd->lock);
 273	return ret;
 274}
 275
 276/**
 277 * pm_genpd_name_poweron - Restore power to a given PM domain and its masters.
 278 * @domain_name: Name of the PM domain to power up.
 
 
 
 
 
 
 
 
 
 
 
 279 */
 280int pm_genpd_name_poweron(const char *domain_name)
 281{
 282	struct generic_pm_domain *genpd;
 283
 284	genpd = pm_genpd_lookup_name(domain_name);
 285	return genpd ? pm_genpd_poweron(genpd) : -EINVAL;
 286}
 287
 288#endif /* CONFIG_PM */
 289
 290#ifdef CONFIG_PM_RUNTIME
 291
 292static int genpd_start_dev_no_timing(struct generic_pm_domain *genpd,
 293				     struct device *dev)
 294{
 295	return GENPD_DEV_CALLBACK(genpd, int, start, dev);
 296}
 297
 298static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
 299{
 300	return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
 301					save_state_latency_ns, "state save");
 302}
 303
 304static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
 305{
 306	return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
 307					restore_state_latency_ns,
 308					"state restore");
 309}
 310
 311static int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
 312				     unsigned long val, void *ptr)
 313{
 314	struct generic_pm_domain_data *gpd_data;
 315	struct device *dev;
 
 316
 317	gpd_data = container_of(nb, struct generic_pm_domain_data, nb);
 
 
 318
 319	mutex_lock(&gpd_data->lock);
 320	dev = gpd_data->base.dev;
 321	if (!dev) {
 322		mutex_unlock(&gpd_data->lock);
 323		return NOTIFY_DONE;
 324	}
 325	mutex_unlock(&gpd_data->lock);
 326
 327	for (;;) {
 328		struct generic_pm_domain *genpd;
 329		struct pm_domain_data *pdd;
 330
 331		spin_lock_irq(&dev->power.lock);
 332
 333		pdd = dev->power.subsys_data ?
 334				dev->power.subsys_data->domain_data : NULL;
 335		if (pdd && pdd->dev) {
 336			to_gpd_data(pdd)->td.constraint_changed = true;
 337			genpd = dev_to_genpd(dev);
 338		} else {
 339			genpd = ERR_PTR(-ENODATA);
 340		}
 341
 342		spin_unlock_irq(&dev->power.lock);
 343
 344		if (!IS_ERR(genpd)) {
 345			mutex_lock(&genpd->lock);
 346			genpd->max_off_time_changed = true;
 347			mutex_unlock(&genpd->lock);
 348		}
 349
 350		dev = dev->parent;
 351		if (!dev || dev->power.ignore_children)
 352			break;
 353	}
 354
 355	return NOTIFY_DONE;
 356}
 
 357
 358/**
 359 * __pm_genpd_save_device - Save the pre-suspend state of a device.
 360 * @pdd: Domain data of the device to save the state of.
 361 * @genpd: PM domain the device belongs to.
 362 */
 363static int __pm_genpd_save_device(struct pm_domain_data *pdd,
 364				  struct generic_pm_domain *genpd)
 365	__releases(&genpd->lock) __acquires(&genpd->lock)
 366{
 367	struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
 368	struct device *dev = pdd->dev;
 369	int ret = 0;
 
 370
 371	if (gpd_data->need_restore)
 372		return 0;
 373
 374	mutex_unlock(&genpd->lock);
 
 375
 376	genpd_start_dev(genpd, dev);
 377	ret = genpd_save_dev(genpd, dev);
 378	genpd_stop_dev(genpd, dev);
 
 379
 380	mutex_lock(&genpd->lock);
 
 
 381
 382	if (!ret)
 383		gpd_data->need_restore = true;
 
 
 384
 385	return ret;
 386}
 387
 388/**
 389 * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
 390 * @pdd: Domain data of the device to restore the state of.
 391 * @genpd: PM domain the device belongs to.
 392 */
 393static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
 394				      struct generic_pm_domain *genpd)
 395	__releases(&genpd->lock) __acquires(&genpd->lock)
 396{
 397	struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
 398	struct device *dev = pdd->dev;
 399	bool need_restore = gpd_data->need_restore;
 
 400
 401	gpd_data->need_restore = false;
 402	mutex_unlock(&genpd->lock);
 403
 404	genpd_start_dev(genpd, dev);
 405	if (need_restore)
 406		genpd_restore_dev(genpd, dev);
 407
 408	mutex_lock(&genpd->lock);
 409}
 
 
 
 
 
 
 
 
 
 
 
 410
 411/**
 412 * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
 413 * @genpd: PM domain to check.
 414 *
 415 * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
 416 * a "power off" operation, which means that a "power on" has occured in the
 417 * meantime, or if its resume_count field is different from zero, which means
 418 * that one of its devices has been resumed in the meantime.
 419 */
 420static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
 421{
 422	return genpd->status == GPD_STATE_WAIT_MASTER
 423		|| genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
 424}
 425
 426/**
 427 * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
 428 * @genpd: PM domait to power off.
 429 *
 430 * Queue up the execution of pm_genpd_poweroff() unless it's already been done
 431 * before.
 432 */
 433void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
 434{
 435	queue_work(pm_wq, &genpd->power_off_work);
 436}
 437
 438/**
 439 * pm_genpd_poweroff - Remove power from a given PM domain.
 440 * @genpd: PM domain to power down.
 
 
 
 
 441 *
 442 * If all of the @genpd's devices have been suspended and all of its subdomains
 443 * have been powered down, run the runtime suspend callbacks provided by all of
 444 * the @genpd's devices' drivers and remove power from @genpd.
 445 */
 446static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
 447	__releases(&genpd->lock) __acquires(&genpd->lock)
 448{
 449	struct pm_domain_data *pdd;
 450	struct gpd_link *link;
 451	unsigned int not_suspended;
 452	int ret = 0;
 453
 454 start:
 455	/*
 456	 * Do not try to power off the domain in the following situations:
 457	 * (1) The domain is already in the "power off" state.
 458	 * (2) The domain is waiting for its master to power up.
 459	 * (3) One of the domain's devices is being resumed right now.
 460	 * (4) System suspend is in progress.
 461	 */
 462	if (genpd->status == GPD_STATE_POWER_OFF
 463	    || genpd->status == GPD_STATE_WAIT_MASTER
 464	    || genpd->resume_count > 0 || genpd->prepared_count > 0)
 465		return 0;
 466
 467	if (atomic_read(&genpd->sd_count) > 0)
 
 
 
 
 
 
 
 468		return -EBUSY;
 469
 470	not_suspended = 0;
 471	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
 472		enum pm_qos_flags_status stat;
 473
 474		stat = dev_pm_qos_flags(pdd->dev,
 475					PM_QOS_FLAG_NO_POWER_OFF
 476						| PM_QOS_FLAG_REMOTE_WAKEUP);
 477		if (stat > PM_QOS_FLAGS_NONE)
 478			return -EBUSY;
 479
 480		if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
 481		    || pdd->dev->power.irq_safe))
 
 
 
 
 482			not_suspended++;
 483	}
 484
 485	if (not_suspended > genpd->in_progress)
 486		return -EBUSY;
 487
 488	if (genpd->poweroff_task) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 489		/*
 490		 * Another instance of pm_genpd_poweroff() is executing
 491		 * callbacks, so tell it to start over and return.
 
 
 
 
 492		 */
 493		genpd->status = GPD_STATE_REPEAT;
 494		return 0;
 
 495	}
 496
 497	if (genpd->gov && genpd->gov->power_down_ok) {
 498		if (!genpd->gov->power_down_ok(&genpd->domain))
 499			return -EAGAIN;
 
 
 
 
 
 500	}
 501
 502	genpd->status = GPD_STATE_BUSY;
 503	genpd->poweroff_task = current;
 504
 505	list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
 506		ret = atomic_read(&genpd->sd_count) == 0 ?
 507			__pm_genpd_save_device(pdd, genpd) : -EBUSY;
 
 
 
 
 
 
 
 
 
 
 
 
 508
 509		if (genpd_abort_poweroff(genpd))
 510			goto out;
 
 
 
 
 
 
 
 
 
 
 
 511
 512		if (ret) {
 513			genpd_set_active(genpd);
 514			goto out;
 515		}
 
 
 
 
 
 
 
 
 516
 517		if (genpd->status == GPD_STATE_REPEAT) {
 518			genpd->poweroff_task = NULL;
 519			goto start;
 520		}
 
 
 
 
 
 
 521	}
 522
 523	if (genpd->cpu_data) {
 524		/*
 525		 * If cpu_data is set, cpuidle should turn the domain off when
 526		 * the CPU in it is idle.  In that case we don't decrement the
 527		 * subdomain counts of the master domains, so that power is not
 528		 * removed from the current domain prematurely as a result of
 529		 * cutting off the masters' power.
 530		 */
 531		genpd->status = GPD_STATE_POWER_OFF;
 532		cpuidle_pause_and_lock();
 533		genpd->cpu_data->idle_state->disabled = false;
 534		cpuidle_resume_and_unlock();
 535		goto out;
 536	}
 
 537
 538	if (genpd->power_off) {
 539		ktime_t time_start;
 540		s64 elapsed_ns;
 541
 542		if (atomic_read(&genpd->sd_count) > 0) {
 543			ret = -EBUSY;
 544			goto out;
 545		}
 546
 547		time_start = ktime_get();
 548
 549		/*
 550		 * If sd_count > 0 at this point, one of the subdomains hasn't
 551		 * managed to call pm_genpd_poweron() for the master yet after
 552		 * incrementing it.  In that case pm_genpd_poweron() will wait
 553		 * for us to drop the lock, so we can call .power_off() and let
 554		 * the pm_genpd_poweron() restore power for us (this shouldn't
 555		 * happen very often).
 556		 */
 557		ret = genpd->power_off(genpd);
 558		if (ret == -EBUSY) {
 559			genpd_set_active(genpd);
 560			goto out;
 561		}
 562
 563		elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
 564		if (elapsed_ns > genpd->power_off_latency_ns) {
 565			genpd->power_off_latency_ns = elapsed_ns;
 
 566			genpd->max_off_time_changed = true;
 567			if (genpd->name)
 568				pr_warning("%s: Power-off latency exceeded, "
 569					"new value %lld ns\n", genpd->name,
 570					elapsed_ns);
 571		}
 572	}
 573
 574	genpd->status = GPD_STATE_POWER_OFF;
 575
 576	list_for_each_entry(link, &genpd->slave_links, slave_node) {
 577		genpd_sd_counter_dec(link->master);
 578		genpd_queue_power_off_work(link->master);
 579	}
 580
 581 out:
 582	genpd->poweroff_task = NULL;
 583	wake_up_all(&genpd->status_wait_queue);
 584	return ret;
 585}
 586
 587/**
 588 * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
 589 * @work: Work structure used for scheduling the execution of this function.
 590 */
 591static void genpd_power_off_work_fn(struct work_struct *work)
 592{
 593	struct generic_pm_domain *genpd;
 594
 595	genpd = container_of(work, struct generic_pm_domain, power_off_work);
 596
 597	genpd_acquire_lock(genpd);
 598	pm_genpd_poweroff(genpd);
 599	genpd_release_lock(genpd);
 600}
 601
 602/**
 603 * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 604 * @dev: Device to suspend.
 605 *
 606 * Carry out a runtime suspend of a device under the assumption that its
 607 * pm_domain field points to the domain member of an object of type
 608 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 609 */
 610static int pm_genpd_runtime_suspend(struct device *dev)
 611{
 612	struct generic_pm_domain *genpd;
 613	bool (*stop_ok)(struct device *__dev);
 
 
 
 
 614	int ret;
 615
 616	dev_dbg(dev, "%s()\n", __func__);
 617
 618	genpd = dev_to_genpd(dev);
 619	if (IS_ERR(genpd))
 620		return -EINVAL;
 621
 622	might_sleep_if(!genpd->dev_irq_safe);
 
 
 
 
 
 
 
 
 623
 624	stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
 625	if (stop_ok && !stop_ok(dev))
 626		return -EBUSY;
 
 
 
 
 
 627
 628	ret = genpd_stop_dev(genpd, dev);
 629	if (ret)
 
 630		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 631
 632	/*
 633	 * If power.irq_safe is set, this routine will be run with interrupts
 634	 * off, so it can't use mutexes.
 635	 */
 636	if (dev->power.irq_safe)
 637		return 0;
 638
 639	mutex_lock(&genpd->lock);
 640	genpd->in_progress++;
 641	pm_genpd_poweroff(genpd);
 642	genpd->in_progress--;
 643	mutex_unlock(&genpd->lock);
 644
 645	return 0;
 646}
 647
 648/**
 649 * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
 650 * @dev: Device to resume.
 651 *
 652 * Carry out a runtime resume of a device under the assumption that its
 653 * pm_domain field points to the domain member of an object of type
 654 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
 655 */
 656static int pm_genpd_runtime_resume(struct device *dev)
 657{
 658	struct generic_pm_domain *genpd;
 659	DEFINE_WAIT(wait);
 
 
 
 660	int ret;
 
 661
 662	dev_dbg(dev, "%s()\n", __func__);
 663
 664	genpd = dev_to_genpd(dev);
 665	if (IS_ERR(genpd))
 666		return -EINVAL;
 667
 668	might_sleep_if(!genpd->dev_irq_safe);
 
 
 
 
 
 
 
 669
 670	/* If power.irq_safe, the PM domain is never powered off. */
 671	if (dev->power.irq_safe)
 672		return genpd_start_dev_no_timing(genpd, dev);
 673
 674	mutex_lock(&genpd->lock);
 675	ret = __pm_genpd_poweron(genpd);
 676	if (ret) {
 677		mutex_unlock(&genpd->lock);
 678		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 679	}
 680	genpd->status = GPD_STATE_BUSY;
 681	genpd->resume_count++;
 682	for (;;) {
 683		prepare_to_wait(&genpd->status_wait_queue, &wait,
 684				TASK_UNINTERRUPTIBLE);
 685		/*
 686		 * If current is the powering off task, we have been called
 687		 * reentrantly from one of the device callbacks, so we should
 688		 * not wait.
 689		 */
 690		if (!genpd->poweroff_task || genpd->poweroff_task == current)
 691			break;
 692		mutex_unlock(&genpd->lock);
 693
 694		schedule();
 695
 696		mutex_lock(&genpd->lock);
 
 
 
 
 
 
 
 697	}
 698	finish_wait(&genpd->status_wait_queue, &wait);
 699	__pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
 700	genpd->resume_count--;
 701	genpd_set_active(genpd);
 702	wake_up_all(&genpd->status_wait_queue);
 703	mutex_unlock(&genpd->lock);
 704
 705	return 0;
 706}
 707
 708static bool pd_ignore_unused;
 709static int __init pd_ignore_unused_setup(char *__unused)
 710{
 711	pd_ignore_unused = true;
 712	return 1;
 713}
 714__setup("pd_ignore_unused", pd_ignore_unused_setup);
 715
 716/**
 717 * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
 718 */
 719void pm_genpd_poweroff_unused(void)
 720{
 721	struct generic_pm_domain *genpd;
 722
 723	if (pd_ignore_unused) {
 724		pr_warn("genpd: Not disabling unused power domains\n");
 725		return;
 726	}
 727
 728	mutex_lock(&gpd_list_lock);
 729
 730	list_for_each_entry(genpd, &gpd_list, gpd_list_node)
 731		genpd_queue_power_off_work(genpd);
 732
 733	mutex_unlock(&gpd_list_lock);
 734}
 735
 736#else
 737
 738static inline int genpd_dev_pm_qos_notifier(struct notifier_block *nb,
 739					    unsigned long val, void *ptr)
 740{
 741	return NOTIFY_DONE;
 742}
 743
 744static inline void genpd_power_off_work_fn(struct work_struct *work) {}
 745
 746#define pm_genpd_runtime_suspend	NULL
 747#define pm_genpd_runtime_resume		NULL
 748
 749#endif /* CONFIG_PM_RUNTIME */
 750
 751#ifdef CONFIG_PM_SLEEP
 752
 753/**
 754 * pm_genpd_present - Check if the given PM domain has been initialized.
 755 * @genpd: PM domain to check.
 756 */
 757static bool pm_genpd_present(struct generic_pm_domain *genpd)
 758{
 759	struct generic_pm_domain *gpd;
 760
 761	if (IS_ERR_OR_NULL(genpd))
 762		return false;
 763
 764	list_for_each_entry(gpd, &gpd_list, gpd_list_node)
 765		if (gpd == genpd)
 766			return true;
 767
 768	return false;
 769}
 770
 771static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
 772				    struct device *dev)
 773{
 774	return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
 775}
 776
 777static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
 778{
 779	return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
 780}
 781
 782static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
 783{
 784	return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
 785}
 786
 787static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
 788{
 789	return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
 790}
 791
 792static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
 793{
 794	return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
 795}
 796
 797static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
 798{
 799	return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
 800}
 801
 802static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
 803{
 804	return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
 805}
 806
 807static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
 808{
 809	return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
 810}
 811
 812static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
 813{
 814	return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
 815}
 816
 817/**
 818 * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
 819 * @genpd: PM domain to power off, if possible.
 
 
 820 *
 821 * Check if the given PM domain can be powered off (during system suspend or
 822 * hibernation) and do that if so.  Also, in that case propagate to its masters.
 823 *
 824 * This function is only called in "noirq" and "syscore" stages of system power
 825 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
 826 * executed sequentially, so it is guaranteed that it will never run twice in
 827 * parallel).
 828 */
 829static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
 
 830{
 831	struct gpd_link *link;
 832
 833	if (genpd->status == GPD_STATE_POWER_OFF)
 834		return;
 835
 836	if (genpd->suspended_count != genpd->device_count
 837	    || atomic_read(&genpd->sd_count) > 0)
 838		return;
 839
 840	if (genpd->power_off)
 841		genpd->power_off(genpd);
 
 
 842
 843	genpd->status = GPD_STATE_POWER_OFF;
 844
 845	list_for_each_entry(link, &genpd->slave_links, slave_node) {
 846		genpd_sd_counter_dec(link->master);
 847		pm_genpd_sync_poweroff(link->master);
 
 
 
 
 
 
 
 848	}
 849}
 850
 851/**
 852 * pm_genpd_sync_poweron - Synchronously power on a PM domain and its masters.
 853 * @genpd: PM domain to power on.
 
 
 854 *
 855 * This function is only called in "noirq" and "syscore" stages of system power
 856 * transitions, so it need not acquire locks (all of the "noirq" callbacks are
 857 * executed sequentially, so it is guaranteed that it will never run twice in
 858 * parallel).
 859 */
 860static void pm_genpd_sync_poweron(struct generic_pm_domain *genpd)
 
 861{
 862	struct gpd_link *link;
 863
 864	if (genpd->status != GPD_STATE_POWER_OFF)
 865		return;
 866
 867	list_for_each_entry(link, &genpd->slave_links, slave_node) {
 868		pm_genpd_sync_poweron(link->master);
 869		genpd_sd_counter_inc(link->master);
 
 
 
 
 
 
 
 870	}
 871
 872	if (genpd->power_on)
 873		genpd->power_on(genpd);
 874
 875	genpd->status = GPD_STATE_ACTIVE;
 876}
 877
 878/**
 879 * resume_needed - Check whether to resume a device before system suspend.
 880 * @dev: Device to check.
 881 * @genpd: PM domain the device belongs to.
 882 *
 883 * There are two cases in which a device that can wake up the system from sleep
 884 * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
 885 * to wake up the system and it has to remain active for this purpose while the
 886 * system is in the sleep state and (2) if the device is not enabled to wake up
 887 * the system from sleep states and it generally doesn't generate wakeup signals
 888 * by itself (those signals are generated on its behalf by other parts of the
 889 * system).  In the latter case it may be necessary to reconfigure the device's
 890 * wakeup settings during system suspend, because it may have been set up to
 891 * signal remote wakeup from the system's working state as needed by runtime PM.
 892 * Return 'true' in either of the above cases.
 893 */
 894static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
 
 895{
 896	bool active_wakeup;
 897
 898	if (!device_can_wakeup(dev))
 899		return false;
 900
 901	active_wakeup = genpd_dev_active_wakeup(genpd, dev);
 902	return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
 903}
 904
 905/**
 906 * pm_genpd_prepare - Start power transition of a device in a PM domain.
 907 * @dev: Device to start the transition of.
 908 *
 909 * Start a power transition of a device (during a system-wide power transition)
 910 * under the assumption that its pm_domain field points to the domain member of
 911 * an object of type struct generic_pm_domain representing a PM domain
 912 * consisting of I/O devices.
 913 */
 914static int pm_genpd_prepare(struct device *dev)
 915{
 916	struct generic_pm_domain *genpd;
 917	int ret;
 918
 919	dev_dbg(dev, "%s()\n", __func__);
 920
 921	genpd = dev_to_genpd(dev);
 922	if (IS_ERR(genpd))
 923		return -EINVAL;
 924
 925	/*
 926	 * If a wakeup request is pending for the device, it should be woken up
 927	 * at this point and a system wakeup event should be reported if it's
 928	 * set up to wake up the system from sleep states.
 929	 */
 930	pm_runtime_get_noresume(dev);
 931	if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
 932		pm_wakeup_event(dev, 0);
 933
 934	if (pm_wakeup_pending()) {
 935		pm_runtime_put(dev);
 936		return -EBUSY;
 937	}
 938
 939	if (resume_needed(dev, genpd))
 940		pm_runtime_resume(dev);
 941
 942	genpd_acquire_lock(genpd);
 943
 944	if (genpd->prepared_count++ == 0) {
 945		genpd->suspended_count = 0;
 946		genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
 947	}
 948
 949	genpd_release_lock(genpd);
 950
 951	if (genpd->suspend_power_off) {
 952		pm_runtime_put_noidle(dev);
 953		return 0;
 954	}
 955
 956	/*
 957	 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
 958	 * so pm_genpd_poweron() will return immediately, but if the device
 959	 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
 960	 * to make it operational.
 961	 */
 962	pm_runtime_resume(dev);
 963	__pm_runtime_disable(dev, false);
 964
 965	ret = pm_generic_prepare(dev);
 966	if (ret) {
 967		mutex_lock(&genpd->lock);
 968
 969		if (--genpd->prepared_count == 0)
 970			genpd->suspend_power_off = false;
 971
 972		mutex_unlock(&genpd->lock);
 973		pm_runtime_enable(dev);
 974	}
 975
 976	pm_runtime_put(dev);
 977	return ret;
 978}
 979
 980/**
 981 * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
 
 982 * @dev: Device to suspend.
 
 983 *
 984 * Suspend a device under the assumption that its pm_domain field points to the
 985 * domain member of an object of type struct generic_pm_domain representing
 986 * a PM domain consisting of I/O devices.
 987 */
 988static int pm_genpd_suspend(struct device *dev)
 989{
 990	struct generic_pm_domain *genpd;
 991
 992	dev_dbg(dev, "%s()\n", __func__);
 993
 994	genpd = dev_to_genpd(dev);
 995	if (IS_ERR(genpd))
 996		return -EINVAL;
 997
 998	return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
 999}
 
 
 
 
1000
1001/**
1002 * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
1003 * @dev: Device to suspend.
1004 *
1005 * Carry out a late suspend of a device under the assumption that its
1006 * pm_domain field points to the domain member of an object of type
1007 * struct generic_pm_domain representing a PM domain consisting of I/O devices.
1008 */
1009static int pm_genpd_suspend_late(struct device *dev)
1010{
1011	struct generic_pm_domain *genpd;
1012
1013	dev_dbg(dev, "%s()\n", __func__);
 
 
 
 
 
 
 
 
 
 
1014
1015	genpd = dev_to_genpd(dev);
1016	if (IS_ERR(genpd))
1017		return -EINVAL;
 
1018
1019	return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
1020}
1021
1022/**
1023 * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
1024 * @dev: Device to suspend.
1025 *
1026 * Stop the device and remove power from the domain if all devices in it have
1027 * been stopped.
1028 */
1029static int pm_genpd_suspend_noirq(struct device *dev)
1030{
1031	struct generic_pm_domain *genpd;
1032
1033	dev_dbg(dev, "%s()\n", __func__);
1034
1035	genpd = dev_to_genpd(dev);
1036	if (IS_ERR(genpd))
1037		return -EINVAL;
1038
1039	if (genpd->suspend_power_off
1040	    || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1041		return 0;
1042
1043	genpd_stop_dev(genpd, dev);
1044
1045	/*
1046	 * Since all of the "noirq" callbacks are executed sequentially, it is
1047	 * guaranteed that this function will never run twice in parallel for
1048	 * the same PM domain, so it is not necessary to use locking here.
1049	 */
1050	genpd->suspended_count++;
1051	pm_genpd_sync_poweroff(genpd);
1052
1053	return 0;
1054}
1055
1056/**
1057 * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
1058 * @dev: Device to resume.
1059 *
1060 * Restore power to the device's PM domain, if necessary, and start the device.
1061 */
1062static int pm_genpd_resume_noirq(struct device *dev)
1063{
1064	struct generic_pm_domain *genpd;
 
1065
1066	dev_dbg(dev, "%s()\n", __func__);
1067
1068	genpd = dev_to_genpd(dev);
1069	if (IS_ERR(genpd))
1070		return -EINVAL;
1071
1072	if (genpd->suspend_power_off
1073	    || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
1074		return 0;
1075
1076	/*
1077	 * Since all of the "noirq" callbacks are executed sequentially, it is
1078	 * guaranteed that this function will never run twice in parallel for
1079	 * the same PM domain, so it is not necessary to use locking here.
1080	 */
1081	pm_genpd_sync_poweron(genpd);
1082	genpd->suspended_count--;
 
1083
1084	return genpd_start_dev(genpd, dev);
1085}
1086
1087/**
1088 * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
1089 * @dev: Device to resume.
1090 *
1091 * Carry out an early resume of a device under the assumption that its
1092 * pm_domain field points to the domain member of an object of type
1093 * struct generic_pm_domain representing a power domain consisting of I/O
1094 * devices.
1095 */
1096static int pm_genpd_resume_early(struct device *dev)
1097{
1098	struct generic_pm_domain *genpd;
1099
1100	dev_dbg(dev, "%s()\n", __func__);
1101
1102	genpd = dev_to_genpd(dev);
1103	if (IS_ERR(genpd))
1104		return -EINVAL;
1105
1106	return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
1107}
1108
1109/**
1110 * pm_genpd_resume - Resume of device in an I/O PM domain.
1111 * @dev: Device to resume.
1112 *
1113 * Resume a device under the assumption that its pm_domain field points to the
1114 * domain member of an object of type struct generic_pm_domain representing
1115 * a power domain consisting of I/O devices.
1116 */
1117static int pm_genpd_resume(struct device *dev)
1118{
1119	struct generic_pm_domain *genpd;
1120
1121	dev_dbg(dev, "%s()\n", __func__);
1122
1123	genpd = dev_to_genpd(dev);
1124	if (IS_ERR(genpd))
1125		return -EINVAL;
1126
1127	return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
1128}
1129
1130/**
1131 * pm_genpd_freeze - Freezing a device in an I/O PM domain.
1132 * @dev: Device to freeze.
1133 *
1134 * Freeze a device under the assumption that its pm_domain field points to the
1135 * domain member of an object of type struct generic_pm_domain representing
1136 * a power domain consisting of I/O devices.
1137 */
1138static int pm_genpd_freeze(struct device *dev)
1139{
1140	struct generic_pm_domain *genpd;
1141
1142	dev_dbg(dev, "%s()\n", __func__);
1143
1144	genpd = dev_to_genpd(dev);
1145	if (IS_ERR(genpd))
1146		return -EINVAL;
1147
1148	return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
1149}
1150
1151/**
1152 * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
1153 * @dev: Device to freeze.
1154 *
1155 * Carry out a late freeze of a device under the assumption that its
1156 * pm_domain field points to the domain member of an object of type
1157 * struct generic_pm_domain representing a power domain consisting of I/O
1158 * devices.
1159 */
1160static int pm_genpd_freeze_late(struct device *dev)
1161{
1162	struct generic_pm_domain *genpd;
 
1163
1164	dev_dbg(dev, "%s()\n", __func__);
1165
1166	genpd = dev_to_genpd(dev);
1167	if (IS_ERR(genpd))
1168		return -EINVAL;
1169
1170	return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
1171}
 
1172
1173/**
1174 * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1175 * @dev: Device to freeze.
1176 *
1177 * Carry out a late freeze of a device under the assumption that its
1178 * pm_domain field points to the domain member of an object of type
1179 * struct generic_pm_domain representing a power domain consisting of I/O
1180 * devices.
1181 */
1182static int pm_genpd_freeze_noirq(struct device *dev)
1183{
1184	struct generic_pm_domain *genpd;
1185
1186	dev_dbg(dev, "%s()\n", __func__);
1187
1188	genpd = dev_to_genpd(dev);
1189	if (IS_ERR(genpd))
1190		return -EINVAL;
1191
1192	return genpd->suspend_power_off ? 0 : genpd_stop_dev(genpd, dev);
1193}
1194
1195/**
1196 * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1197 * @dev: Device to thaw.
1198 *
1199 * Start the device, unless power has been removed from the domain already
1200 * before the system transition.
1201 */
1202static int pm_genpd_thaw_noirq(struct device *dev)
1203{
1204	struct generic_pm_domain *genpd;
 
1205
1206	dev_dbg(dev, "%s()\n", __func__);
1207
1208	genpd = dev_to_genpd(dev);
1209	if (IS_ERR(genpd))
1210		return -EINVAL;
1211
1212	return genpd->suspend_power_off ? 0 : genpd_start_dev(genpd, dev);
1213}
 
 
 
 
1214
1215/**
1216 * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1217 * @dev: Device to thaw.
1218 *
1219 * Carry out an early thaw of a device under the assumption that its
1220 * pm_domain field points to the domain member of an object of type
1221 * struct generic_pm_domain representing a power domain consisting of I/O
1222 * devices.
1223 */
1224static int pm_genpd_thaw_early(struct device *dev)
1225{
1226	struct generic_pm_domain *genpd;
1227
1228	dev_dbg(dev, "%s()\n", __func__);
1229
1230	genpd = dev_to_genpd(dev);
1231	if (IS_ERR(genpd))
1232		return -EINVAL;
1233
1234	return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1235}
1236
1237/**
1238 * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1239 * @dev: Device to thaw.
 
1240 *
1241 * Thaw a device under the assumption that its pm_domain field points to the
1242 * domain member of an object of type struct generic_pm_domain representing
1243 * a power domain consisting of I/O devices.
1244 */
1245static int pm_genpd_thaw(struct device *dev)
1246{
1247	struct generic_pm_domain *genpd;
1248
1249	dev_dbg(dev, "%s()\n", __func__);
1250
1251	genpd = dev_to_genpd(dev);
1252	if (IS_ERR(genpd))
1253		return -EINVAL;
1254
1255	return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1256}
1257
1258/**
1259 * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1260 * @dev: Device to resume.
1261 *
1262 * Make sure the domain will be in the same power state as before the
1263 * hibernation the system is resuming from and start the device if necessary.
1264 */
1265static int pm_genpd_restore_noirq(struct device *dev)
1266{
1267	struct generic_pm_domain *genpd;
 
1268
1269	dev_dbg(dev, "%s()\n", __func__);
1270
1271	genpd = dev_to_genpd(dev);
1272	if (IS_ERR(genpd))
1273		return -EINVAL;
1274
1275	/*
1276	 * Since all of the "noirq" callbacks are executed sequentially, it is
1277	 * guaranteed that this function will never run twice in parallel for
1278	 * the same PM domain, so it is not necessary to use locking here.
1279	 *
1280	 * At this point suspended_count == 0 means we are being run for the
1281	 * first time for the given domain in the present cycle.
1282	 */
1283	if (genpd->suspended_count++ == 0) {
 
1284		/*
1285		 * The boot kernel might put the domain into arbitrary state,
1286		 * so make it appear as powered off to pm_genpd_sync_poweron(),
1287		 * so that it tries to power it on in case it was really off.
1288		 */
1289		genpd->status = GPD_STATE_POWER_OFF;
1290		if (genpd->suspend_power_off) {
1291			/*
1292			 * If the domain was off before the hibernation, make
1293			 * sure it will be off going forward.
1294			 */
1295			if (genpd->power_off)
1296				genpd->power_off(genpd);
1297
1298			return 0;
1299		}
 
 
 
 
 
 
1300	}
1301
1302	if (genpd->suspend_power_off)
1303		return 0;
1304
1305	pm_genpd_sync_poweron(genpd);
1306
1307	return genpd_start_dev(genpd, dev);
1308}
1309
1310/**
1311 * pm_genpd_complete - Complete power transition of a device in a power domain.
1312 * @dev: Device to complete the transition of.
1313 *
1314 * Complete a power transition of a device (during a system-wide power
1315 * transition) under the assumption that its pm_domain field points to the
1316 * domain member of an object of type struct generic_pm_domain representing
1317 * a power domain consisting of I/O devices.
1318 */
1319static void pm_genpd_complete(struct device *dev)
1320{
1321	struct generic_pm_domain *genpd;
1322	bool run_complete;
1323
1324	dev_dbg(dev, "%s()\n", __func__);
1325
1326	genpd = dev_to_genpd(dev);
1327	if (IS_ERR(genpd))
1328		return;
1329
1330	mutex_lock(&genpd->lock);
1331
1332	run_complete = !genpd->suspend_power_off;
1333	if (--genpd->prepared_count == 0)
1334		genpd->suspend_power_off = false;
1335
1336	mutex_unlock(&genpd->lock);
 
 
1337
1338	if (run_complete) {
1339		pm_generic_complete(dev);
1340		pm_runtime_set_active(dev);
1341		pm_runtime_enable(dev);
1342		pm_request_idle(dev);
1343	}
1344}
1345
1346/**
1347 * pm_genpd_syscore_switch - Switch power during system core suspend or resume.
1348 * @dev: Device that normally is marked as "always on" to switch power for.
1349 *
1350 * This routine may only be called during the system core (syscore) suspend or
1351 * resume phase for devices whose "always on" flags are set.
1352 */
1353void pm_genpd_syscore_switch(struct device *dev, bool suspend)
1354{
1355	struct generic_pm_domain *genpd;
1356
1357	genpd = dev_to_genpd(dev);
1358	if (!pm_genpd_present(genpd))
1359		return;
1360
1361	if (suspend) {
1362		genpd->suspended_count++;
1363		pm_genpd_sync_poweroff(genpd);
1364	} else {
1365		pm_genpd_sync_poweron(genpd);
1366		genpd->suspended_count--;
1367	}
1368}
1369EXPORT_SYMBOL_GPL(pm_genpd_syscore_switch);
1370
1371#else
 
 
 
 
 
 
 
 
 
 
 
 
1372
1373#define pm_genpd_prepare		NULL
1374#define pm_genpd_suspend		NULL
1375#define pm_genpd_suspend_late		NULL
1376#define pm_genpd_suspend_noirq		NULL
1377#define pm_genpd_resume_early		NULL
1378#define pm_genpd_resume_noirq		NULL
1379#define pm_genpd_resume			NULL
1380#define pm_genpd_freeze			NULL
1381#define pm_genpd_freeze_late		NULL
1382#define pm_genpd_freeze_noirq		NULL
1383#define pm_genpd_thaw_early		NULL
1384#define pm_genpd_thaw_noirq		NULL
1385#define pm_genpd_thaw			NULL
1386#define pm_genpd_restore_noirq		NULL
1387#define pm_genpd_complete		NULL
1388
1389#endif /* CONFIG_PM_SLEEP */
1390
1391static struct generic_pm_domain_data *__pm_genpd_alloc_dev_data(struct device *dev)
1392{
1393	struct generic_pm_domain_data *gpd_data;
 
 
 
 
 
1394
1395	gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1396	if (!gpd_data)
1397		return NULL;
 
 
1398
1399	mutex_init(&gpd_data->lock);
 
 
1400	gpd_data->nb.notifier_call = genpd_dev_pm_qos_notifier;
1401	dev_pm_qos_add_notifier(dev, &gpd_data->nb);
 
 
 
 
 
 
 
 
 
 
 
1402	return gpd_data;
 
 
 
 
 
 
 
1403}
1404
1405static void __pm_genpd_free_dev_data(struct device *dev,
1406				     struct generic_pm_domain_data *gpd_data)
1407{
1408	dev_pm_qos_remove_notifier(dev, &gpd_data->nb);
 
 
 
 
 
1409	kfree(gpd_data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1410}
1411
1412/**
1413 * __pm_genpd_add_device - Add a device to an I/O PM domain.
1414 * @genpd: PM domain to add the device to.
1415 * @dev: Device to be added.
1416 * @td: Set of PM QoS timing parameters to attach to the device.
1417 */
1418int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1419			  struct gpd_timing_data *td)
 
 
 
 
 
 
 
 
 
1420{
1421	struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1422	struct pm_domain_data *pdd;
1423	int ret = 0;
1424
1425	dev_dbg(dev, "%s()\n", __func__);
1426
1427	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1428		return -EINVAL;
1429
1430	gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1431	if (!gpd_data_new)
1432		return -ENOMEM;
1433
1434	genpd_acquire_lock(genpd);
1435
1436	if (genpd->prepared_count > 0) {
1437		ret = -EAGAIN;
1438		goto out;
1439	}
1440
1441	list_for_each_entry(pdd, &genpd->dev_list, list_node)
1442		if (pdd->dev == dev) {
1443			ret = -EINVAL;
1444			goto out;
1445		}
1446
1447	ret = dev_pm_get_subsys_data(dev);
1448	if (ret)
1449		goto out;
1450
1451	genpd->device_count++;
1452	genpd->max_off_time_changed = true;
1453
1454	spin_lock_irq(&dev->power.lock);
1455
1456	dev->pm_domain = &genpd->domain;
1457	if (dev->power.subsys_data->domain_data) {
1458		gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1459	} else {
1460		gpd_data = gpd_data_new;
1461		dev->power.subsys_data->domain_data = &gpd_data->base;
1462	}
1463	gpd_data->refcount++;
1464	if (td)
1465		gpd_data->td = *td;
1466
1467	spin_unlock_irq(&dev->power.lock);
1468
1469	mutex_lock(&gpd_data->lock);
1470	gpd_data->base.dev = dev;
1471	list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1472	gpd_data->need_restore = genpd->status == GPD_STATE_POWER_OFF;
1473	gpd_data->td.constraint_changed = true;
1474	gpd_data->td.effective_constraint_ns = -1;
1475	mutex_unlock(&gpd_data->lock);
1476
 
1477 out:
1478	genpd_release_lock(genpd);
1479
1480	if (gpd_data != gpd_data_new)
1481		__pm_genpd_free_dev_data(dev, gpd_data_new);
 
1482
1483	return ret;
1484}
1485
1486/**
1487 * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1488 * @genpd_node: Device tree node pointer representing a PM domain to which the
1489 *   the device is added to.
1490 * @dev: Device to be added.
1491 * @td: Set of PM QoS timing parameters to attach to the device.
1492 */
1493int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1494			     struct gpd_timing_data *td)
1495{
1496	struct generic_pm_domain *genpd = NULL, *gpd;
1497
1498	dev_dbg(dev, "%s()\n", __func__);
1499
1500	if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1501		return -EINVAL;
1502
1503	mutex_lock(&gpd_list_lock);
1504	list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1505		if (gpd->of_node == genpd_node) {
1506			genpd = gpd;
1507			break;
1508		}
1509	}
1510	mutex_unlock(&gpd_list_lock);
1511
1512	if (!genpd)
1513		return -EINVAL;
1514
1515	return __pm_genpd_add_device(genpd, dev, td);
1516}
 
1517
1518
1519/**
1520 * __pm_genpd_name_add_device - Find I/O PM domain and add a device to it.
1521 * @domain_name: Name of the PM domain to add the device to.
1522 * @dev: Device to be added.
1523 * @td: Set of PM QoS timing parameters to attach to the device.
1524 */
1525int __pm_genpd_name_add_device(const char *domain_name, struct device *dev,
1526			       struct gpd_timing_data *td)
1527{
1528	return __pm_genpd_add_device(pm_genpd_lookup_name(domain_name), dev, td);
1529}
1530
1531/**
1532 * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1533 * @genpd: PM domain to remove the device from.
1534 * @dev: Device to be removed.
1535 */
1536int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1537			   struct device *dev)
1538{
1539	struct generic_pm_domain_data *gpd_data;
1540	struct pm_domain_data *pdd;
1541	bool remove = false;
1542	int ret = 0;
1543
1544	dev_dbg(dev, "%s()\n", __func__);
1545
1546	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev)
1547	    ||  IS_ERR_OR_NULL(dev->pm_domain)
1548	    ||  pd_to_genpd(dev->pm_domain) != genpd)
1549		return -EINVAL;
1550
1551	genpd_acquire_lock(genpd);
1552
1553	if (genpd->prepared_count > 0) {
1554		ret = -EAGAIN;
1555		goto out;
1556	}
1557
1558	genpd->device_count--;
1559	genpd->max_off_time_changed = true;
1560
1561	spin_lock_irq(&dev->power.lock);
 
1562
1563	dev->pm_domain = NULL;
1564	pdd = dev->power.subsys_data->domain_data;
1565	list_del_init(&pdd->list_node);
1566	gpd_data = to_gpd_data(pdd);
1567	if (--gpd_data->refcount == 0) {
1568		dev->power.subsys_data->domain_data = NULL;
1569		remove = true;
1570	}
1571
1572	spin_unlock_irq(&dev->power.lock);
1573
1574	mutex_lock(&gpd_data->lock);
1575	pdd->dev = NULL;
1576	mutex_unlock(&gpd_data->lock);
1577
1578	genpd_release_lock(genpd);
1579
1580	dev_pm_put_subsys_data(dev);
1581	if (remove)
1582		__pm_genpd_free_dev_data(dev, gpd_data);
1583
1584	return 0;
1585
1586 out:
1587	genpd_release_lock(genpd);
 
1588
1589	return ret;
1590}
1591
1592/**
1593 * pm_genpd_dev_need_restore - Set/unset the device's "need restore" flag.
1594 * @dev: Device to set/unset the flag for.
1595 * @val: The new value of the device's "need restore" flag.
1596 */
1597void pm_genpd_dev_need_restore(struct device *dev, bool val)
1598{
1599	struct pm_subsys_data *psd;
1600	unsigned long flags;
1601
1602	spin_lock_irqsave(&dev->power.lock, flags);
1603
1604	psd = dev_to_psd(dev);
1605	if (psd && psd->domain_data)
1606		to_gpd_data(psd->domain_data)->need_restore = val;
1607
1608	spin_unlock_irqrestore(&dev->power.lock, flags);
1609}
1610EXPORT_SYMBOL_GPL(pm_genpd_dev_need_restore);
1611
1612/**
1613 * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1614 * @genpd: Master PM domain to add the subdomain to.
1615 * @subdomain: Subdomain to be added.
1616 */
1617int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1618			   struct generic_pm_domain *subdomain)
1619{
1620	struct gpd_link *link;
1621	int ret = 0;
1622
1623	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain)
1624	    || genpd == subdomain)
1625		return -EINVAL;
1626
1627 start:
1628	genpd_acquire_lock(genpd);
1629	mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1630
1631	if (subdomain->status != GPD_STATE_POWER_OFF
1632	    && subdomain->status != GPD_STATE_ACTIVE) {
1633		mutex_unlock(&subdomain->lock);
1634		genpd_release_lock(genpd);
1635		goto start;
1636	}
1637
1638	if (genpd->status == GPD_STATE_POWER_OFF
1639	    &&  subdomain->status != GPD_STATE_POWER_OFF) {
 
 
 
 
 
 
1640		ret = -EINVAL;
1641		goto out;
1642	}
1643
1644	list_for_each_entry(link, &genpd->master_links, master_node) {
1645		if (link->slave == subdomain && link->master == genpd) {
1646			ret = -EINVAL;
1647			goto out;
1648		}
1649	}
1650
1651	link = kzalloc(sizeof(*link), GFP_KERNEL);
1652	if (!link) {
1653		ret = -ENOMEM;
1654		goto out;
1655	}
1656	link->master = genpd;
1657	list_add_tail(&link->master_node, &genpd->master_links);
1658	link->slave = subdomain;
1659	list_add_tail(&link->slave_node, &subdomain->slave_links);
1660	if (subdomain->status != GPD_STATE_POWER_OFF)
1661		genpd_sd_counter_inc(genpd);
1662
1663 out:
1664	mutex_unlock(&subdomain->lock);
1665	genpd_release_lock(genpd);
1666
 
1667	return ret;
1668}
1669
1670/**
1671 * pm_genpd_add_subdomain_names - Add a subdomain to an I/O PM domain.
1672 * @master_name: Name of the master PM domain to add the subdomain to.
1673 * @subdomain_name: Name of the subdomain to be added.
1674 */
1675int pm_genpd_add_subdomain_names(const char *master_name,
1676				 const char *subdomain_name)
1677{
1678	struct generic_pm_domain *master = NULL, *subdomain = NULL, *gpd;
1679
1680	if (IS_ERR_OR_NULL(master_name) || IS_ERR_OR_NULL(subdomain_name))
1681		return -EINVAL;
1682
1683	mutex_lock(&gpd_list_lock);
1684	list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1685		if (!master && !strcmp(gpd->name, master_name))
1686			master = gpd;
1687
1688		if (!subdomain && !strcmp(gpd->name, subdomain_name))
1689			subdomain = gpd;
1690
1691		if (master && subdomain)
1692			break;
1693	}
1694	mutex_unlock(&gpd_list_lock);
1695
1696	return pm_genpd_add_subdomain(master, subdomain);
1697}
 
1698
1699/**
1700 * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1701 * @genpd: Master PM domain to remove the subdomain from.
1702 * @subdomain: Subdomain to be removed.
1703 */
1704int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1705			      struct generic_pm_domain *subdomain)
1706{
1707	struct gpd_link *link;
1708	int ret = -EINVAL;
1709
1710	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1711		return -EINVAL;
1712
1713 start:
1714	genpd_acquire_lock(genpd);
1715
1716	list_for_each_entry(link, &genpd->master_links, master_node) {
1717		if (link->slave != subdomain)
 
 
 
 
 
 
 
1718			continue;
1719
1720		mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1721
1722		if (subdomain->status != GPD_STATE_POWER_OFF
1723		    && subdomain->status != GPD_STATE_ACTIVE) {
1724			mutex_unlock(&subdomain->lock);
1725			genpd_release_lock(genpd);
1726			goto start;
1727		}
1728
1729		list_del(&link->master_node);
1730		list_del(&link->slave_node);
1731		kfree(link);
1732		if (subdomain->status != GPD_STATE_POWER_OFF)
1733			genpd_sd_counter_dec(genpd);
1734
1735		mutex_unlock(&subdomain->lock);
1736
1737		ret = 0;
1738		break;
1739	}
1740
1741	genpd_release_lock(genpd);
 
 
1742
1743	return ret;
1744}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1745
1746/**
1747 * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1748 * @dev: Device to add the callbacks to.
1749 * @ops: Set of callbacks to add.
1750 * @td: Timing data to add to the device along with the callbacks (optional).
1751 *
1752 * Every call to this routine should be balanced with a call to
1753 * __pm_genpd_remove_callbacks() and they must not be nested.
1754 */
1755int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1756			   struct gpd_timing_data *td)
1757{
1758	struct generic_pm_domain_data *gpd_data_new, *gpd_data = NULL;
1759	int ret = 0;
1760
1761	if (!(dev && ops))
1762		return -EINVAL;
1763
1764	gpd_data_new = __pm_genpd_alloc_dev_data(dev);
1765	if (!gpd_data_new)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1766		return -ENOMEM;
1767
1768	pm_runtime_disable(dev);
1769	device_pm_lock();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1770
1771	ret = dev_pm_get_subsys_data(dev);
1772	if (ret)
1773		goto out;
 
 
 
 
 
 
 
1774
1775	spin_lock_irq(&dev->power.lock);
 
 
 
 
1776
1777	if (dev->power.subsys_data->domain_data) {
1778		gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1779	} else {
1780		gpd_data = gpd_data_new;
1781		dev->power.subsys_data->domain_data = &gpd_data->base;
1782	}
1783	gpd_data->refcount++;
1784	gpd_data->ops = *ops;
1785	if (td)
1786		gpd_data->td = *td;
1787
1788	spin_unlock_irq(&dev->power.lock);
 
 
 
 
 
 
 
 
 
 
 
1789
1790 out:
1791	device_pm_unlock();
1792	pm_runtime_enable(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
1793
1794	if (gpd_data != gpd_data_new)
1795		__pm_genpd_free_dev_data(dev, gpd_data_new);
 
1796
1797	return ret;
1798}
1799EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1800
1801/**
1802 * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1803 * @dev: Device to remove the callbacks from.
1804 * @clear_td: If set, clear the device's timing data too.
1805 *
1806 * This routine can only be called after pm_genpd_add_callbacks().
 
 
 
1807 */
1808int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
 
 
1809{
1810	struct generic_pm_domain_data *gpd_data = NULL;
1811	bool remove = false;
1812	int ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1813
1814	if (!(dev && dev->power.subsys_data))
1815		return -EINVAL;
1816
1817	pm_runtime_disable(dev);
1818	device_pm_lock();
1819
1820	spin_lock_irq(&dev->power.lock);
 
 
 
1821
1822	if (dev->power.subsys_data->domain_data) {
1823		gpd_data = to_gpd_data(dev->power.subsys_data->domain_data);
1824		gpd_data->ops = (struct gpd_dev_ops){ NULL };
1825		if (clear_td)
1826			gpd_data->td = (struct gpd_timing_data){ 0 };
1827
1828		if (--gpd_data->refcount == 0) {
1829			dev->power.subsys_data->domain_data = NULL;
1830			remove = true;
1831		}
1832	} else {
1833		ret = -EINVAL;
 
 
 
 
 
1834	}
1835
1836	spin_unlock_irq(&dev->power.lock);
 
 
 
 
 
1837
1838	device_pm_unlock();
1839	pm_runtime_enable(dev);
1840
1841	if (ret)
1842		return ret;
1843
1844	dev_pm_put_subsys_data(dev);
1845	if (remove)
1846		__pm_genpd_free_dev_data(dev, gpd_data);
1847
1848	return 0;
1849}
1850EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1851
1852/**
1853 * pm_genpd_attach_cpuidle - Connect the given PM domain with cpuidle.
1854 * @genpd: PM domain to be connected with cpuidle.
1855 * @state: cpuidle state this domain can disable/enable.
1856 *
1857 * Make a PM domain behave as though it contained a CPU core, that is, instead
1858 * of calling its power down routine it will enable the given cpuidle state so
1859 * that the cpuidle subsystem can power it down (if possible and desirable).
1860 */
1861int pm_genpd_attach_cpuidle(struct generic_pm_domain *genpd, int state)
 
1862{
1863	struct cpuidle_driver *cpuidle_drv;
1864	struct gpd_cpu_data *cpu_data;
1865	struct cpuidle_state *idle_state;
1866	int ret = 0;
1867
1868	if (IS_ERR_OR_NULL(genpd) || state < 0)
1869		return -EINVAL;
1870
1871	genpd_acquire_lock(genpd);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1872
1873	if (genpd->cpu_data) {
1874		ret = -EEXIST;
1875		goto out;
1876	}
1877	cpu_data = kzalloc(sizeof(*cpu_data), GFP_KERNEL);
1878	if (!cpu_data) {
1879		ret = -ENOMEM;
1880		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1881	}
1882	cpuidle_drv = cpuidle_driver_ref();
1883	if (!cpuidle_drv) {
1884		ret = -ENODEV;
1885		goto err_drv;
1886	}
1887	if (cpuidle_drv->state_count <= state) {
1888		ret = -EINVAL;
1889		goto err;
1890	}
1891	idle_state = &cpuidle_drv->states[state];
1892	if (!idle_state->disabled) {
1893		ret = -EAGAIN;
1894		goto err;
1895	}
1896	cpu_data->idle_state = idle_state;
1897	cpu_data->saved_exit_latency = idle_state->exit_latency;
1898	genpd->cpu_data = cpu_data;
1899	genpd_recalc_cpu_exit_latency(genpd);
1900
1901 out:
1902	genpd_release_lock(genpd);
1903	return ret;
 
 
1904
1905 err:
1906	cpuidle_driver_unref();
 
 
 
 
 
 
1907
1908 err_drv:
1909	kfree(cpu_data);
1910	goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1911}
 
1912
1913/**
1914 * pm_genpd_name_attach_cpuidle - Find PM domain and connect cpuidle to it.
1915 * @name: Name of the domain to connect to cpuidle.
1916 * @state: cpuidle state this domain can manipulate.
 
 
 
 
 
 
1917 */
1918int pm_genpd_name_attach_cpuidle(const char *name, int state)
 
1919{
1920	return pm_genpd_attach_cpuidle(pm_genpd_lookup_name(name), state);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1921}
1922
1923/**
1924 * pm_genpd_detach_cpuidle - Remove the cpuidle connection from a PM domain.
1925 * @genpd: PM domain to remove the cpuidle connection from.
 
1926 *
1927 * Remove the cpuidle connection set up by pm_genpd_attach_cpuidle() from the
1928 * given PM domain.
1929 */
1930int pm_genpd_detach_cpuidle(struct generic_pm_domain *genpd)
1931{
1932	struct gpd_cpu_data *cpu_data;
1933	struct cpuidle_state *idle_state;
1934	int ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1935
1936	if (IS_ERR_OR_NULL(genpd))
1937		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
1938
1939	genpd_acquire_lock(genpd);
1940
1941	cpu_data = genpd->cpu_data;
1942	if (!cpu_data) {
1943		ret = -ENODEV;
1944		goto out;
1945	}
1946	idle_state = cpu_data->idle_state;
1947	if (!idle_state->disabled) {
1948		ret = -EAGAIN;
 
1949		goto out;
1950	}
1951	idle_state->exit_latency = cpu_data->saved_exit_latency;
1952	cpuidle_driver_unref();
1953	genpd->cpu_data = NULL;
1954	kfree(cpu_data);
1955
1956 out:
1957	genpd_release_lock(genpd);
 
 
 
1958	return ret;
1959}
 
1960
1961/**
1962 * pm_genpd_name_detach_cpuidle - Find PM domain and disconnect cpuidle from it.
1963 * @name: Name of the domain to disconnect cpuidle from.
 
 
 
 
 
1964 */
1965int pm_genpd_name_detach_cpuidle(const char *name)
 
1966{
1967	return pm_genpd_detach_cpuidle(pm_genpd_lookup_name(name));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1968}
1969
1970/* Default device callbacks for generic PM domains. */
1971
1972/**
1973 * pm_genpd_default_save_state - Default "save device state" for PM domians.
1974 * @dev: Device to handle.
 
 
 
 
 
 
 
 
 
1975 */
1976static int pm_genpd_default_save_state(struct device *dev)
1977{
1978	int (*cb)(struct device *__dev);
 
1979
1980	cb = dev_gpd_data(dev)->ops.save_state;
1981	if (cb)
1982		return cb(dev);
1983
1984	if (dev->type && dev->type->pm)
1985		cb = dev->type->pm->runtime_suspend;
1986	else if (dev->class && dev->class->pm)
1987		cb = dev->class->pm->runtime_suspend;
1988	else if (dev->bus && dev->bus->pm)
1989		cb = dev->bus->pm->runtime_suspend;
1990	else
1991		cb = NULL;
 
1992
1993	if (!cb && dev->driver && dev->driver->pm)
1994		cb = dev->driver->pm->runtime_suspend;
 
1995
1996	return cb ? cb(dev) : 0;
 
 
 
1997}
1998
 
 
 
 
1999/**
2000 * pm_genpd_default_restore_state - Default PM domians "restore device state".
2001 * @dev: Device to handle.
 
 
 
 
2002 */
2003static int pm_genpd_default_restore_state(struct device *dev)
2004{
2005	int (*cb)(struct device *__dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2006
2007	cb = dev_gpd_data(dev)->ops.restore_state;
2008	if (cb)
2009		return cb(dev);
2010
2011	if (dev->type && dev->type->pm)
2012		cb = dev->type->pm->runtime_resume;
2013	else if (dev->class && dev->class->pm)
2014		cb = dev->class->pm->runtime_resume;
2015	else if (dev->bus && dev->bus->pm)
2016		cb = dev->bus->pm->runtime_resume;
2017	else
2018		cb = NULL;
2019
2020	if (!cb && dev->driver && dev->driver->pm)
2021		cb = dev->driver->pm->runtime_resume;
 
2022
2023	return cb ? cb(dev) : 0;
2024}
2025
2026#ifdef CONFIG_PM_SLEEP
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2027
2028/**
2029 * pm_genpd_default_suspend - Default "device suspend" for PM domians.
2030 * @dev: Device to handle.
 
 
 
 
 
 
 
 
 
2031 */
2032static int pm_genpd_default_suspend(struct device *dev)
2033{
2034	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
 
 
 
 
 
 
 
 
 
2035
2036	return cb ? cb(dev) : pm_generic_suspend(dev);
2037}
 
2038
2039/**
2040 * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
2041 * @dev: Device to handle.
 
 
 
 
 
 
 
 
 
 
 
 
2042 */
2043static int pm_genpd_default_suspend_late(struct device *dev)
 
2044{
2045	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2046
2047	return cb ? cb(dev) : pm_generic_suspend_late(dev);
2048}
 
2049
2050/**
2051 * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
2052 * @dev: Device to handle.
 
 
 
 
 
2053 */
2054static int pm_genpd_default_resume_early(struct device *dev)
2055{
2056	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
 
 
 
 
 
 
 
 
2057
2058	return cb ? cb(dev) : pm_generic_resume_early(dev);
2059}
2060
2061/**
2062 * pm_genpd_default_resume - Default "device resume" for PM domians.
2063 * @dev: Device to handle.
2064 */
2065static int pm_genpd_default_resume(struct device *dev)
 
 
2066{
2067	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
 
 
2068
2069	return cb ? cb(dev) : pm_generic_resume(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2070}
2071
2072/**
2073 * pm_genpd_default_freeze - Default "device freeze" for PM domians.
2074 * @dev: Device to handle.
2075 */
2076static int pm_genpd_default_freeze(struct device *dev)
2077{
2078	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2079
2080	return cb ? cb(dev) : pm_generic_freeze(dev);
2081}
2082
2083/**
2084 * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
2085 * @dev: Device to handle.
 
 
 
 
 
 
 
 
2086 */
2087static int pm_genpd_default_freeze_late(struct device *dev)
 
2088{
2089	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
 
 
 
 
 
2090
2091	return cb ? cb(dev) : pm_generic_freeze_late(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2092}
 
2093
2094/**
2095 * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
2096 * @dev: Device to handle.
 
 
 
 
 
 
 
 
 
2097 */
2098static int pm_genpd_default_thaw_early(struct device *dev)
 
2099{
2100	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
 
 
 
 
 
 
 
 
 
 
2101
2102	return cb ? cb(dev) : pm_generic_thaw_early(dev);
2103}
 
 
 
 
 
 
 
 
 
 
2104
2105/**
2106 * pm_genpd_default_thaw - Default "device thaw" for PM domians.
2107 * @dev: Device to handle.
 
 
 
 
 
 
 
 
 
 
 
2108 */
2109static int pm_genpd_default_thaw(struct device *dev)
2110{
2111	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2112
2113	return cb ? cb(dev) : pm_generic_thaw(dev);
2114}
2115
2116#else /* !CONFIG_PM_SLEEP */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2117
2118#define pm_genpd_default_suspend	NULL
2119#define pm_genpd_default_suspend_late	NULL
2120#define pm_genpd_default_resume_early	NULL
2121#define pm_genpd_default_resume		NULL
2122#define pm_genpd_default_freeze		NULL
2123#define pm_genpd_default_freeze_late	NULL
2124#define pm_genpd_default_thaw_early	NULL
2125#define pm_genpd_default_thaw		NULL
2126
2127#endif /* !CONFIG_PM_SLEEP */
 
2128
2129/**
2130 * pm_genpd_init - Initialize a generic I/O PM domain object.
2131 * @genpd: PM domain object to initialize.
2132 * @gov: PM domain governor to associate with the domain (may be NULL).
2133 * @is_off: Initial value of the domain's power_is_off field.
2134 */
2135void pm_genpd_init(struct generic_pm_domain *genpd,
2136		   struct dev_power_governor *gov, bool is_off)
2137{
2138	if (IS_ERR_OR_NULL(genpd))
2139		return;
 
 
 
 
 
 
 
 
2140
2141	INIT_LIST_HEAD(&genpd->master_links);
2142	INIT_LIST_HEAD(&genpd->slave_links);
2143	INIT_LIST_HEAD(&genpd->dev_list);
2144	mutex_init(&genpd->lock);
2145	genpd->gov = gov;
2146	INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
2147	genpd->in_progress = 0;
2148	atomic_set(&genpd->sd_count, 0);
2149	genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
2150	init_waitqueue_head(&genpd->status_wait_queue);
2151	genpd->poweroff_task = NULL;
2152	genpd->resume_count = 0;
2153	genpd->device_count = 0;
2154	genpd->max_off_time_ns = -1;
2155	genpd->max_off_time_changed = true;
2156	genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
2157	genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
2158	genpd->domain.ops.prepare = pm_genpd_prepare;
2159	genpd->domain.ops.suspend = pm_genpd_suspend;
2160	genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
2161	genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
2162	genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
2163	genpd->domain.ops.resume_early = pm_genpd_resume_early;
2164	genpd->domain.ops.resume = pm_genpd_resume;
2165	genpd->domain.ops.freeze = pm_genpd_freeze;
2166	genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
2167	genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
2168	genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
2169	genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
2170	genpd->domain.ops.thaw = pm_genpd_thaw;
2171	genpd->domain.ops.poweroff = pm_genpd_suspend;
2172	genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
2173	genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
2174	genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
2175	genpd->domain.ops.restore_early = pm_genpd_resume_early;
2176	genpd->domain.ops.restore = pm_genpd_resume;
2177	genpd->domain.ops.complete = pm_genpd_complete;
2178	genpd->dev_ops.save_state = pm_genpd_default_save_state;
2179	genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
2180	genpd->dev_ops.suspend = pm_genpd_default_suspend;
2181	genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
2182	genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
2183	genpd->dev_ops.resume = pm_genpd_default_resume;
2184	genpd->dev_ops.freeze = pm_genpd_default_freeze;
2185	genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
2186	genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
2187	genpd->dev_ops.thaw = pm_genpd_default_thaw;
2188	mutex_lock(&gpd_list_lock);
2189	list_add(&genpd->gpd_list_node, &gpd_list);
2190	mutex_unlock(&gpd_list_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2191}