brintos

brintos / linux-shallow public Read only

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