Linux Audio

Check our new training course

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