1 /*
2  * drivers/base/power/domain.c - Common code related to device power domains.
3  *
4  * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
5  *
6  * This file is released under the GPLv2.
7  */
8 
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_domain.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
18 #include <linux/export.h>
19 
20 #define GENPD_DEV_CALLBACK(genpd, type, callback, dev)		\
21 ({								\
22 	type (*__routine)(struct device *__d); 			\
23 	type __ret = (type)0;					\
24 								\
25 	__routine = genpd->dev_ops.callback; 			\
26 	if (__routine) {					\
27 		__ret = __routine(dev); 			\
28 	} else {						\
29 		__routine = dev_gpd_data(dev)->ops.callback;	\
30 		if (__routine) 					\
31 			__ret = __routine(dev);			\
32 	}							\
33 	__ret;							\
34 })
35 
36 #define GENPD_DEV_TIMED_CALLBACK(genpd, type, callback, dev, field, name)	\
37 ({										\
38 	ktime_t __start = ktime_get();						\
39 	type __retval = GENPD_DEV_CALLBACK(genpd, type, callback, dev);		\
40 	s64 __elapsed = ktime_to_ns(ktime_sub(ktime_get(), __start));		\
41 	struct generic_pm_domain_data *__gpd_data = dev_gpd_data(dev);		\
42 	if (__elapsed > __gpd_data->td.field) {					\
43 		__gpd_data->td.field = __elapsed;				\
44 		dev_warn(dev, name " latency exceeded, new value %lld ns\n",	\
45 			__elapsed);						\
46 	}									\
47 	__retval;								\
48 })
49 
50 static LIST_HEAD(gpd_list);
51 static DEFINE_MUTEX(gpd_list_lock);
52 
53 #ifdef CONFIG_PM
54 
dev_to_genpd(struct device * dev)55 struct generic_pm_domain *dev_to_genpd(struct device *dev)
56 {
57 	if (IS_ERR_OR_NULL(dev->pm_domain))
58 		return ERR_PTR(-EINVAL);
59 
60 	return pd_to_genpd(dev->pm_domain);
61 }
62 
genpd_stop_dev(struct generic_pm_domain * genpd,struct device * dev)63 static int genpd_stop_dev(struct generic_pm_domain *genpd, struct device *dev)
64 {
65 	return GENPD_DEV_TIMED_CALLBACK(genpd, int, stop, dev,
66 					stop_latency_ns, "stop");
67 }
68 
genpd_start_dev(struct generic_pm_domain * genpd,struct device * dev)69 static int genpd_start_dev(struct generic_pm_domain *genpd, struct device *dev)
70 {
71 	return GENPD_DEV_TIMED_CALLBACK(genpd, int, start, dev,
72 					start_latency_ns, "start");
73 }
74 
genpd_save_dev(struct generic_pm_domain * genpd,struct device * dev)75 static int genpd_save_dev(struct generic_pm_domain *genpd, struct device *dev)
76 {
77 	return GENPD_DEV_TIMED_CALLBACK(genpd, int, save_state, dev,
78 					save_state_latency_ns, "state save");
79 }
80 
genpd_restore_dev(struct generic_pm_domain * genpd,struct device * dev)81 static int genpd_restore_dev(struct generic_pm_domain *genpd, struct device *dev)
82 {
83 	return GENPD_DEV_TIMED_CALLBACK(genpd, int, restore_state, dev,
84 					restore_state_latency_ns,
85 					"state restore");
86 }
87 
genpd_sd_counter_dec(struct generic_pm_domain * genpd)88 static bool genpd_sd_counter_dec(struct generic_pm_domain *genpd)
89 {
90 	bool ret = false;
91 
92 	if (!WARN_ON(atomic_read(&genpd->sd_count) == 0))
93 		ret = !!atomic_dec_and_test(&genpd->sd_count);
94 
95 	return ret;
96 }
97 
genpd_sd_counter_inc(struct generic_pm_domain * genpd)98 static void genpd_sd_counter_inc(struct generic_pm_domain *genpd)
99 {
100 	atomic_inc(&genpd->sd_count);
101 	smp_mb__after_atomic_inc();
102 }
103 
genpd_acquire_lock(struct generic_pm_domain * genpd)104 static void genpd_acquire_lock(struct generic_pm_domain *genpd)
105 {
106 	DEFINE_WAIT(wait);
107 
108 	mutex_lock(&genpd->lock);
109 	/*
110 	 * Wait for the domain to transition into either the active,
111 	 * or the power off state.
112 	 */
113 	for (;;) {
114 		prepare_to_wait(&genpd->status_wait_queue, &wait,
115 				TASK_UNINTERRUPTIBLE);
116 		if (genpd->status == GPD_STATE_ACTIVE
117 		    || genpd->status == GPD_STATE_POWER_OFF)
118 			break;
119 		mutex_unlock(&genpd->lock);
120 
121 		schedule();
122 
123 		mutex_lock(&genpd->lock);
124 	}
125 	finish_wait(&genpd->status_wait_queue, &wait);
126 }
127 
genpd_release_lock(struct generic_pm_domain * genpd)128 static void genpd_release_lock(struct generic_pm_domain *genpd)
129 {
130 	mutex_unlock(&genpd->lock);
131 }
132 
genpd_set_active(struct generic_pm_domain * genpd)133 static void genpd_set_active(struct generic_pm_domain *genpd)
134 {
135 	if (genpd->resume_count == 0)
136 		genpd->status = GPD_STATE_ACTIVE;
137 }
138 
139 /**
140  * __pm_genpd_poweron - Restore power to a given PM domain and its masters.
141  * @genpd: PM domain to power up.
142  *
143  * Restore power to @genpd and all of its masters so that it is possible to
144  * resume a device belonging to it.
145  */
__pm_genpd_poweron(struct generic_pm_domain * genpd)146 int __pm_genpd_poweron(struct generic_pm_domain *genpd)
147 	__releases(&genpd->lock) __acquires(&genpd->lock)
148 {
149 	struct gpd_link *link;
150 	DEFINE_WAIT(wait);
151 	int ret = 0;
152 
153 	/* If the domain's master is being waited for, we have to wait too. */
154 	for (;;) {
155 		prepare_to_wait(&genpd->status_wait_queue, &wait,
156 				TASK_UNINTERRUPTIBLE);
157 		if (genpd->status != GPD_STATE_WAIT_MASTER)
158 			break;
159 		mutex_unlock(&genpd->lock);
160 
161 		schedule();
162 
163 		mutex_lock(&genpd->lock);
164 	}
165 	finish_wait(&genpd->status_wait_queue, &wait);
166 
167 	if (genpd->status == GPD_STATE_ACTIVE
168 	    || (genpd->prepared_count > 0 && genpd->suspend_power_off))
169 		return 0;
170 
171 	if (genpd->status != GPD_STATE_POWER_OFF) {
172 		genpd_set_active(genpd);
173 		return 0;
174 	}
175 
176 	/*
177 	 * The list is guaranteed not to change while the loop below is being
178 	 * executed, unless one of the masters' .power_on() callbacks fiddles
179 	 * with it.
180 	 */
181 	list_for_each_entry(link, &genpd->slave_links, slave_node) {
182 		genpd_sd_counter_inc(link->master);
183 		genpd->status = GPD_STATE_WAIT_MASTER;
184 
185 		mutex_unlock(&genpd->lock);
186 
187 		ret = pm_genpd_poweron(link->master);
188 
189 		mutex_lock(&genpd->lock);
190 
191 		/*
192 		 * The "wait for parent" status is guaranteed not to change
193 		 * while the master is powering on.
194 		 */
195 		genpd->status = GPD_STATE_POWER_OFF;
196 		wake_up_all(&genpd->status_wait_queue);
197 		if (ret) {
198 			genpd_sd_counter_dec(link->master);
199 			goto err;
200 		}
201 	}
202 
203 	if (genpd->power_on) {
204 		ktime_t time_start = ktime_get();
205 		s64 elapsed_ns;
206 
207 		ret = genpd->power_on(genpd);
208 		if (ret)
209 			goto err;
210 
211 		elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
212 		if (elapsed_ns > genpd->power_on_latency_ns) {
213 			genpd->power_on_latency_ns = elapsed_ns;
214 			if (genpd->name)
215 				pr_warning("%s: Power-on latency exceeded, "
216 					"new value %lld ns\n", genpd->name,
217 					elapsed_ns);
218 		}
219 	}
220 
221 	genpd_set_active(genpd);
222 
223 	return 0;
224 
225  err:
226 	list_for_each_entry_continue_reverse(link, &genpd->slave_links, slave_node)
227 		genpd_sd_counter_dec(link->master);
228 
229 	return ret;
230 }
231 
232 /**
233  * pm_genpd_poweron - Restore power to a given PM domain and its masters.
234  * @genpd: PM domain to power up.
235  */
pm_genpd_poweron(struct generic_pm_domain * genpd)236 int pm_genpd_poweron(struct generic_pm_domain *genpd)
237 {
238 	int ret;
239 
240 	mutex_lock(&genpd->lock);
241 	ret = __pm_genpd_poweron(genpd);
242 	mutex_unlock(&genpd->lock);
243 	return ret;
244 }
245 
246 #endif /* CONFIG_PM */
247 
248 #ifdef CONFIG_PM_RUNTIME
249 
250 /**
251  * __pm_genpd_save_device - Save the pre-suspend state of a device.
252  * @pdd: Domain data of the device to save the state of.
253  * @genpd: PM domain the device belongs to.
254  */
__pm_genpd_save_device(struct pm_domain_data * pdd,struct generic_pm_domain * genpd)255 static int __pm_genpd_save_device(struct pm_domain_data *pdd,
256 				  struct generic_pm_domain *genpd)
257 	__releases(&genpd->lock) __acquires(&genpd->lock)
258 {
259 	struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
260 	struct device *dev = pdd->dev;
261 	int ret = 0;
262 
263 	if (gpd_data->need_restore)
264 		return 0;
265 
266 	mutex_unlock(&genpd->lock);
267 
268 	genpd_start_dev(genpd, dev);
269 	ret = genpd_save_dev(genpd, dev);
270 	genpd_stop_dev(genpd, dev);
271 
272 	mutex_lock(&genpd->lock);
273 
274 	if (!ret)
275 		gpd_data->need_restore = true;
276 
277 	return ret;
278 }
279 
280 /**
281  * __pm_genpd_restore_device - Restore the pre-suspend state of a device.
282  * @pdd: Domain data of the device to restore the state of.
283  * @genpd: PM domain the device belongs to.
284  */
__pm_genpd_restore_device(struct pm_domain_data * pdd,struct generic_pm_domain * genpd)285 static void __pm_genpd_restore_device(struct pm_domain_data *pdd,
286 				      struct generic_pm_domain *genpd)
287 	__releases(&genpd->lock) __acquires(&genpd->lock)
288 {
289 	struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
290 	struct device *dev = pdd->dev;
291 
292 	if (!gpd_data->need_restore)
293 		return;
294 
295 	mutex_unlock(&genpd->lock);
296 
297 	genpd_start_dev(genpd, dev);
298 	genpd_restore_dev(genpd, dev);
299 	genpd_stop_dev(genpd, dev);
300 
301 	mutex_lock(&genpd->lock);
302 
303 	gpd_data->need_restore = false;
304 }
305 
306 /**
307  * genpd_abort_poweroff - Check if a PM domain power off should be aborted.
308  * @genpd: PM domain to check.
309  *
310  * Return true if a PM domain's status changed to GPD_STATE_ACTIVE during
311  * a "power off" operation, which means that a "power on" has occured in the
312  * meantime, or if its resume_count field is different from zero, which means
313  * that one of its devices has been resumed in the meantime.
314  */
genpd_abort_poweroff(struct generic_pm_domain * genpd)315 static bool genpd_abort_poweroff(struct generic_pm_domain *genpd)
316 {
317 	return genpd->status == GPD_STATE_WAIT_MASTER
318 		|| genpd->status == GPD_STATE_ACTIVE || genpd->resume_count > 0;
319 }
320 
321 /**
322  * genpd_queue_power_off_work - Queue up the execution of pm_genpd_poweroff().
323  * @genpd: PM domait to power off.
324  *
325  * Queue up the execution of pm_genpd_poweroff() unless it's already been done
326  * before.
327  */
genpd_queue_power_off_work(struct generic_pm_domain * genpd)328 void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
329 {
330 	if (!work_pending(&genpd->power_off_work))
331 		queue_work(pm_wq, &genpd->power_off_work);
332 }
333 
334 /**
335  * pm_genpd_poweroff - Remove power from a given PM domain.
336  * @genpd: PM domain to power down.
337  *
338  * If all of the @genpd's devices have been suspended and all of its subdomains
339  * have been powered down, run the runtime suspend callbacks provided by all of
340  * the @genpd's devices' drivers and remove power from @genpd.
341  */
pm_genpd_poweroff(struct generic_pm_domain * genpd)342 static int pm_genpd_poweroff(struct generic_pm_domain *genpd)
343 	__releases(&genpd->lock) __acquires(&genpd->lock)
344 {
345 	struct pm_domain_data *pdd;
346 	struct gpd_link *link;
347 	unsigned int not_suspended;
348 	int ret = 0;
349 
350  start:
351 	/*
352 	 * Do not try to power off the domain in the following situations:
353 	 * (1) The domain is already in the "power off" state.
354 	 * (2) The domain is waiting for its master to power up.
355 	 * (3) One of the domain's devices is being resumed right now.
356 	 * (4) System suspend is in progress.
357 	 */
358 	if (genpd->status == GPD_STATE_POWER_OFF
359 	    || genpd->status == GPD_STATE_WAIT_MASTER
360 	    || genpd->resume_count > 0 || genpd->prepared_count > 0)
361 		return 0;
362 
363 	if (atomic_read(&genpd->sd_count) > 0)
364 		return -EBUSY;
365 
366 	not_suspended = 0;
367 	list_for_each_entry(pdd, &genpd->dev_list, list_node)
368 		if (pdd->dev->driver && (!pm_runtime_suspended(pdd->dev)
369 		    || pdd->dev->power.irq_safe || to_gpd_data(pdd)->always_on))
370 			not_suspended++;
371 
372 	if (not_suspended > genpd->in_progress)
373 		return -EBUSY;
374 
375 	if (genpd->poweroff_task) {
376 		/*
377 		 * Another instance of pm_genpd_poweroff() is executing
378 		 * callbacks, so tell it to start over and return.
379 		 */
380 		genpd->status = GPD_STATE_REPEAT;
381 		return 0;
382 	}
383 
384 	if (genpd->gov && genpd->gov->power_down_ok) {
385 		if (!genpd->gov->power_down_ok(&genpd->domain))
386 			return -EAGAIN;
387 	}
388 
389 	genpd->status = GPD_STATE_BUSY;
390 	genpd->poweroff_task = current;
391 
392 	list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
393 		ret = atomic_read(&genpd->sd_count) == 0 ?
394 			__pm_genpd_save_device(pdd, genpd) : -EBUSY;
395 
396 		if (genpd_abort_poweroff(genpd))
397 			goto out;
398 
399 		if (ret) {
400 			genpd_set_active(genpd);
401 			goto out;
402 		}
403 
404 		if (genpd->status == GPD_STATE_REPEAT) {
405 			genpd->poweroff_task = NULL;
406 			goto start;
407 		}
408 	}
409 
410 	if (genpd->power_off) {
411 		ktime_t time_start;
412 		s64 elapsed_ns;
413 
414 		if (atomic_read(&genpd->sd_count) > 0) {
415 			ret = -EBUSY;
416 			goto out;
417 		}
418 
419 		time_start = ktime_get();
420 
421 		/*
422 		 * If sd_count > 0 at this point, one of the subdomains hasn't
423 		 * managed to call pm_genpd_poweron() for the master yet after
424 		 * incrementing it.  In that case pm_genpd_poweron() will wait
425 		 * for us to drop the lock, so we can call .power_off() and let
426 		 * the pm_genpd_poweron() restore power for us (this shouldn't
427 		 * happen very often).
428 		 */
429 		ret = genpd->power_off(genpd);
430 		if (ret == -EBUSY) {
431 			genpd_set_active(genpd);
432 			goto out;
433 		}
434 
435 		elapsed_ns = ktime_to_ns(ktime_sub(ktime_get(), time_start));
436 		if (elapsed_ns > genpd->power_off_latency_ns) {
437 			genpd->power_off_latency_ns = elapsed_ns;
438 			if (genpd->name)
439 				pr_warning("%s: Power-off latency exceeded, "
440 					"new value %lld ns\n", genpd->name,
441 					elapsed_ns);
442 		}
443 	}
444 
445 	genpd->status = GPD_STATE_POWER_OFF;
446 	genpd->power_off_time = ktime_get();
447 
448 	/* Update PM QoS information for devices in the domain. */
449 	list_for_each_entry_reverse(pdd, &genpd->dev_list, list_node) {
450 		struct gpd_timing_data *td = &to_gpd_data(pdd)->td;
451 
452 		pm_runtime_update_max_time_suspended(pdd->dev,
453 					td->start_latency_ns +
454 					td->restore_state_latency_ns +
455 					genpd->power_on_latency_ns);
456 	}
457 
458 	list_for_each_entry(link, &genpd->slave_links, slave_node) {
459 		genpd_sd_counter_dec(link->master);
460 		genpd_queue_power_off_work(link->master);
461 	}
462 
463  out:
464 	genpd->poweroff_task = NULL;
465 	wake_up_all(&genpd->status_wait_queue);
466 	return ret;
467 }
468 
469 /**
470  * genpd_power_off_work_fn - Power off PM domain whose subdomain count is 0.
471  * @work: Work structure used for scheduling the execution of this function.
472  */
genpd_power_off_work_fn(struct work_struct * work)473 static void genpd_power_off_work_fn(struct work_struct *work)
474 {
475 	struct generic_pm_domain *genpd;
476 
477 	genpd = container_of(work, struct generic_pm_domain, power_off_work);
478 
479 	genpd_acquire_lock(genpd);
480 	pm_genpd_poweroff(genpd);
481 	genpd_release_lock(genpd);
482 }
483 
484 /**
485  * pm_genpd_runtime_suspend - Suspend a device belonging to I/O PM domain.
486  * @dev: Device to suspend.
487  *
488  * Carry out a runtime suspend of a device under the assumption that its
489  * pm_domain field points to the domain member of an object of type
490  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
491  */
pm_genpd_runtime_suspend(struct device * dev)492 static int pm_genpd_runtime_suspend(struct device *dev)
493 {
494 	struct generic_pm_domain *genpd;
495 	bool (*stop_ok)(struct device *__dev);
496 	int ret;
497 
498 	dev_dbg(dev, "%s()\n", __func__);
499 
500 	genpd = dev_to_genpd(dev);
501 	if (IS_ERR(genpd))
502 		return -EINVAL;
503 
504 	might_sleep_if(!genpd->dev_irq_safe);
505 
506 	if (dev_gpd_data(dev)->always_on)
507 		return -EBUSY;
508 
509 	stop_ok = genpd->gov ? genpd->gov->stop_ok : NULL;
510 	if (stop_ok && !stop_ok(dev))
511 		return -EBUSY;
512 
513 	ret = genpd_stop_dev(genpd, dev);
514 	if (ret)
515 		return ret;
516 
517 	pm_runtime_update_max_time_suspended(dev,
518 				dev_gpd_data(dev)->td.start_latency_ns);
519 
520 	/*
521 	 * If power.irq_safe is set, this routine will be run with interrupts
522 	 * off, so it can't use mutexes.
523 	 */
524 	if (dev->power.irq_safe)
525 		return 0;
526 
527 	mutex_lock(&genpd->lock);
528 	genpd->in_progress++;
529 	pm_genpd_poweroff(genpd);
530 	genpd->in_progress--;
531 	mutex_unlock(&genpd->lock);
532 
533 	return 0;
534 }
535 
536 /**
537  * pm_genpd_runtime_resume - Resume a device belonging to I/O PM domain.
538  * @dev: Device to resume.
539  *
540  * Carry out a runtime resume of a device under the assumption that its
541  * pm_domain field points to the domain member of an object of type
542  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
543  */
pm_genpd_runtime_resume(struct device * dev)544 static int pm_genpd_runtime_resume(struct device *dev)
545 {
546 	struct generic_pm_domain *genpd;
547 	DEFINE_WAIT(wait);
548 	int ret;
549 
550 	dev_dbg(dev, "%s()\n", __func__);
551 
552 	genpd = dev_to_genpd(dev);
553 	if (IS_ERR(genpd))
554 		return -EINVAL;
555 
556 	might_sleep_if(!genpd->dev_irq_safe);
557 
558 	/* If power.irq_safe, the PM domain is never powered off. */
559 	if (dev->power.irq_safe)
560 		goto out;
561 
562 	mutex_lock(&genpd->lock);
563 	ret = __pm_genpd_poweron(genpd);
564 	if (ret) {
565 		mutex_unlock(&genpd->lock);
566 		return ret;
567 	}
568 	genpd->status = GPD_STATE_BUSY;
569 	genpd->resume_count++;
570 	for (;;) {
571 		prepare_to_wait(&genpd->status_wait_queue, &wait,
572 				TASK_UNINTERRUPTIBLE);
573 		/*
574 		 * If current is the powering off task, we have been called
575 		 * reentrantly from one of the device callbacks, so we should
576 		 * not wait.
577 		 */
578 		if (!genpd->poweroff_task || genpd->poweroff_task == current)
579 			break;
580 		mutex_unlock(&genpd->lock);
581 
582 		schedule();
583 
584 		mutex_lock(&genpd->lock);
585 	}
586 	finish_wait(&genpd->status_wait_queue, &wait);
587 	__pm_genpd_restore_device(dev->power.subsys_data->domain_data, genpd);
588 	genpd->resume_count--;
589 	genpd_set_active(genpd);
590 	wake_up_all(&genpd->status_wait_queue);
591 	mutex_unlock(&genpd->lock);
592 
593  out:
594 	genpd_start_dev(genpd, dev);
595 
596 	return 0;
597 }
598 
599 /**
600  * pm_genpd_poweroff_unused - Power off all PM domains with no devices in use.
601  */
pm_genpd_poweroff_unused(void)602 void pm_genpd_poweroff_unused(void)
603 {
604 	struct generic_pm_domain *genpd;
605 
606 	mutex_lock(&gpd_list_lock);
607 
608 	list_for_each_entry(genpd, &gpd_list, gpd_list_node)
609 		genpd_queue_power_off_work(genpd);
610 
611 	mutex_unlock(&gpd_list_lock);
612 }
613 
614 #else
615 
genpd_power_off_work_fn(struct work_struct * work)616 static inline void genpd_power_off_work_fn(struct work_struct *work) {}
617 
618 #define pm_genpd_runtime_suspend	NULL
619 #define pm_genpd_runtime_resume		NULL
620 
621 #endif /* CONFIG_PM_RUNTIME */
622 
623 #ifdef CONFIG_PM_SLEEP
624 
genpd_dev_active_wakeup(struct generic_pm_domain * genpd,struct device * dev)625 static bool genpd_dev_active_wakeup(struct generic_pm_domain *genpd,
626 				    struct device *dev)
627 {
628 	return GENPD_DEV_CALLBACK(genpd, bool, active_wakeup, dev);
629 }
630 
genpd_suspend_dev(struct generic_pm_domain * genpd,struct device * dev)631 static int genpd_suspend_dev(struct generic_pm_domain *genpd, struct device *dev)
632 {
633 	return GENPD_DEV_CALLBACK(genpd, int, suspend, dev);
634 }
635 
genpd_suspend_late(struct generic_pm_domain * genpd,struct device * dev)636 static int genpd_suspend_late(struct generic_pm_domain *genpd, struct device *dev)
637 {
638 	return GENPD_DEV_CALLBACK(genpd, int, suspend_late, dev);
639 }
640 
genpd_resume_early(struct generic_pm_domain * genpd,struct device * dev)641 static int genpd_resume_early(struct generic_pm_domain *genpd, struct device *dev)
642 {
643 	return GENPD_DEV_CALLBACK(genpd, int, resume_early, dev);
644 }
645 
genpd_resume_dev(struct generic_pm_domain * genpd,struct device * dev)646 static int genpd_resume_dev(struct generic_pm_domain *genpd, struct device *dev)
647 {
648 	return GENPD_DEV_CALLBACK(genpd, int, resume, dev);
649 }
650 
genpd_freeze_dev(struct generic_pm_domain * genpd,struct device * dev)651 static int genpd_freeze_dev(struct generic_pm_domain *genpd, struct device *dev)
652 {
653 	return GENPD_DEV_CALLBACK(genpd, int, freeze, dev);
654 }
655 
genpd_freeze_late(struct generic_pm_domain * genpd,struct device * dev)656 static int genpd_freeze_late(struct generic_pm_domain *genpd, struct device *dev)
657 {
658 	return GENPD_DEV_CALLBACK(genpd, int, freeze_late, dev);
659 }
660 
genpd_thaw_early(struct generic_pm_domain * genpd,struct device * dev)661 static int genpd_thaw_early(struct generic_pm_domain *genpd, struct device *dev)
662 {
663 	return GENPD_DEV_CALLBACK(genpd, int, thaw_early, dev);
664 }
665 
genpd_thaw_dev(struct generic_pm_domain * genpd,struct device * dev)666 static int genpd_thaw_dev(struct generic_pm_domain *genpd, struct device *dev)
667 {
668 	return GENPD_DEV_CALLBACK(genpd, int, thaw, dev);
669 }
670 
671 /**
672  * pm_genpd_sync_poweroff - Synchronously power off a PM domain and its masters.
673  * @genpd: PM domain to power off, if possible.
674  *
675  * Check if the given PM domain can be powered off (during system suspend or
676  * hibernation) and do that if so.  Also, in that case propagate to its masters.
677  *
678  * This function is only called in "noirq" stages of system power transitions,
679  * so it need not acquire locks (all of the "noirq" callbacks are executed
680  * sequentially, so it is guaranteed that it will never run twice in parallel).
681  */
pm_genpd_sync_poweroff(struct generic_pm_domain * genpd)682 static void pm_genpd_sync_poweroff(struct generic_pm_domain *genpd)
683 {
684 	struct gpd_link *link;
685 
686 	if (genpd->status == GPD_STATE_POWER_OFF)
687 		return;
688 
689 	if (genpd->suspended_count != genpd->device_count
690 	    || atomic_read(&genpd->sd_count) > 0)
691 		return;
692 
693 	if (genpd->power_off)
694 		genpd->power_off(genpd);
695 
696 	genpd->status = GPD_STATE_POWER_OFF;
697 
698 	list_for_each_entry(link, &genpd->slave_links, slave_node) {
699 		genpd_sd_counter_dec(link->master);
700 		pm_genpd_sync_poweroff(link->master);
701 	}
702 }
703 
704 /**
705  * resume_needed - Check whether to resume a device before system suspend.
706  * @dev: Device to check.
707  * @genpd: PM domain the device belongs to.
708  *
709  * There are two cases in which a device that can wake up the system from sleep
710  * states should be resumed by pm_genpd_prepare(): (1) if the device is enabled
711  * to wake up the system and it has to remain active for this purpose while the
712  * system is in the sleep state and (2) if the device is not enabled to wake up
713  * the system from sleep states and it generally doesn't generate wakeup signals
714  * by itself (those signals are generated on its behalf by other parts of the
715  * system).  In the latter case it may be necessary to reconfigure the device's
716  * wakeup settings during system suspend, because it may have been set up to
717  * signal remote wakeup from the system's working state as needed by runtime PM.
718  * Return 'true' in either of the above cases.
719  */
resume_needed(struct device * dev,struct generic_pm_domain * genpd)720 static bool resume_needed(struct device *dev, struct generic_pm_domain *genpd)
721 {
722 	bool active_wakeup;
723 
724 	if (!device_can_wakeup(dev))
725 		return false;
726 
727 	active_wakeup = genpd_dev_active_wakeup(genpd, dev);
728 	return device_may_wakeup(dev) ? active_wakeup : !active_wakeup;
729 }
730 
731 /**
732  * pm_genpd_prepare - Start power transition of a device in a PM domain.
733  * @dev: Device to start the transition of.
734  *
735  * Start a power transition of a device (during a system-wide power transition)
736  * under the assumption that its pm_domain field points to the domain member of
737  * an object of type struct generic_pm_domain representing a PM domain
738  * consisting of I/O devices.
739  */
pm_genpd_prepare(struct device * dev)740 static int pm_genpd_prepare(struct device *dev)
741 {
742 	struct generic_pm_domain *genpd;
743 	int ret;
744 
745 	dev_dbg(dev, "%s()\n", __func__);
746 
747 	genpd = dev_to_genpd(dev);
748 	if (IS_ERR(genpd))
749 		return -EINVAL;
750 
751 	/*
752 	 * If a wakeup request is pending for the device, it should be woken up
753 	 * at this point and a system wakeup event should be reported if it's
754 	 * set up to wake up the system from sleep states.
755 	 */
756 	pm_runtime_get_noresume(dev);
757 	if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
758 		pm_wakeup_event(dev, 0);
759 
760 	if (pm_wakeup_pending()) {
761 		pm_runtime_put_sync(dev);
762 		return -EBUSY;
763 	}
764 
765 	if (resume_needed(dev, genpd))
766 		pm_runtime_resume(dev);
767 
768 	genpd_acquire_lock(genpd);
769 
770 	if (genpd->prepared_count++ == 0) {
771 		genpd->suspended_count = 0;
772 		genpd->suspend_power_off = genpd->status == GPD_STATE_POWER_OFF;
773 	}
774 
775 	genpd_release_lock(genpd);
776 
777 	if (genpd->suspend_power_off) {
778 		pm_runtime_put_noidle(dev);
779 		return 0;
780 	}
781 
782 	/*
783 	 * The PM domain must be in the GPD_STATE_ACTIVE state at this point,
784 	 * so pm_genpd_poweron() will return immediately, but if the device
785 	 * is suspended (e.g. it's been stopped by genpd_stop_dev()), we need
786 	 * to make it operational.
787 	 */
788 	pm_runtime_resume(dev);
789 	__pm_runtime_disable(dev, false);
790 
791 	ret = pm_generic_prepare(dev);
792 	if (ret) {
793 		mutex_lock(&genpd->lock);
794 
795 		if (--genpd->prepared_count == 0)
796 			genpd->suspend_power_off = false;
797 
798 		mutex_unlock(&genpd->lock);
799 		pm_runtime_enable(dev);
800 	}
801 
802 	pm_runtime_put_sync(dev);
803 	return ret;
804 }
805 
806 /**
807  * pm_genpd_suspend - Suspend a device belonging to an I/O PM domain.
808  * @dev: Device to suspend.
809  *
810  * Suspend a device under the assumption that its pm_domain field points to the
811  * domain member of an object of type struct generic_pm_domain representing
812  * a PM domain consisting of I/O devices.
813  */
pm_genpd_suspend(struct device * dev)814 static int pm_genpd_suspend(struct device *dev)
815 {
816 	struct generic_pm_domain *genpd;
817 
818 	dev_dbg(dev, "%s()\n", __func__);
819 
820 	genpd = dev_to_genpd(dev);
821 	if (IS_ERR(genpd))
822 		return -EINVAL;
823 
824 	return genpd->suspend_power_off ? 0 : genpd_suspend_dev(genpd, dev);
825 }
826 
827 /**
828  * pm_genpd_suspend_late - Late suspend of a device from an I/O PM domain.
829  * @dev: Device to suspend.
830  *
831  * Carry out a late suspend of a device under the assumption that its
832  * pm_domain field points to the domain member of an object of type
833  * struct generic_pm_domain representing a PM domain consisting of I/O devices.
834  */
pm_genpd_suspend_late(struct device * dev)835 static int pm_genpd_suspend_late(struct device *dev)
836 {
837 	struct generic_pm_domain *genpd;
838 
839 	dev_dbg(dev, "%s()\n", __func__);
840 
841 	genpd = dev_to_genpd(dev);
842 	if (IS_ERR(genpd))
843 		return -EINVAL;
844 
845 	return genpd->suspend_power_off ? 0 : genpd_suspend_late(genpd, dev);
846 }
847 
848 /**
849  * pm_genpd_suspend_noirq - Completion of suspend of device in an I/O PM domain.
850  * @dev: Device to suspend.
851  *
852  * Stop the device and remove power from the domain if all devices in it have
853  * been stopped.
854  */
pm_genpd_suspend_noirq(struct device * dev)855 static int pm_genpd_suspend_noirq(struct device *dev)
856 {
857 	struct generic_pm_domain *genpd;
858 
859 	dev_dbg(dev, "%s()\n", __func__);
860 
861 	genpd = dev_to_genpd(dev);
862 	if (IS_ERR(genpd))
863 		return -EINVAL;
864 
865 	if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
866 	    || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
867 		return 0;
868 
869 	genpd_stop_dev(genpd, dev);
870 
871 	/*
872 	 * Since all of the "noirq" callbacks are executed sequentially, it is
873 	 * guaranteed that this function will never run twice in parallel for
874 	 * the same PM domain, so it is not necessary to use locking here.
875 	 */
876 	genpd->suspended_count++;
877 	pm_genpd_sync_poweroff(genpd);
878 
879 	return 0;
880 }
881 
882 /**
883  * pm_genpd_resume_noirq - Start of resume of device in an I/O PM domain.
884  * @dev: Device to resume.
885  *
886  * Restore power to the device's PM domain, if necessary, and start the device.
887  */
pm_genpd_resume_noirq(struct device * dev)888 static int pm_genpd_resume_noirq(struct device *dev)
889 {
890 	struct generic_pm_domain *genpd;
891 
892 	dev_dbg(dev, "%s()\n", __func__);
893 
894 	genpd = dev_to_genpd(dev);
895 	if (IS_ERR(genpd))
896 		return -EINVAL;
897 
898 	if (genpd->suspend_power_off || dev_gpd_data(dev)->always_on
899 	    || (dev->power.wakeup_path && genpd_dev_active_wakeup(genpd, dev)))
900 		return 0;
901 
902 	/*
903 	 * Since all of the "noirq" callbacks are executed sequentially, it is
904 	 * guaranteed that this function will never run twice in parallel for
905 	 * the same PM domain, so it is not necessary to use locking here.
906 	 */
907 	pm_genpd_poweron(genpd);
908 	genpd->suspended_count--;
909 
910 	return genpd_start_dev(genpd, dev);
911 }
912 
913 /**
914  * pm_genpd_resume_early - Early resume of a device in an I/O PM domain.
915  * @dev: Device to resume.
916  *
917  * Carry out an early resume of a device under the assumption that its
918  * pm_domain field points to the domain member of an object of type
919  * struct generic_pm_domain representing a power domain consisting of I/O
920  * devices.
921  */
pm_genpd_resume_early(struct device * dev)922 static int pm_genpd_resume_early(struct device *dev)
923 {
924 	struct generic_pm_domain *genpd;
925 
926 	dev_dbg(dev, "%s()\n", __func__);
927 
928 	genpd = dev_to_genpd(dev);
929 	if (IS_ERR(genpd))
930 		return -EINVAL;
931 
932 	return genpd->suspend_power_off ? 0 : genpd_resume_early(genpd, dev);
933 }
934 
935 /**
936  * pm_genpd_resume - Resume of device in an I/O PM domain.
937  * @dev: Device to resume.
938  *
939  * Resume a device under the assumption that its pm_domain field points to the
940  * domain member of an object of type struct generic_pm_domain representing
941  * a power domain consisting of I/O devices.
942  */
pm_genpd_resume(struct device * dev)943 static int pm_genpd_resume(struct device *dev)
944 {
945 	struct generic_pm_domain *genpd;
946 
947 	dev_dbg(dev, "%s()\n", __func__);
948 
949 	genpd = dev_to_genpd(dev);
950 	if (IS_ERR(genpd))
951 		return -EINVAL;
952 
953 	return genpd->suspend_power_off ? 0 : genpd_resume_dev(genpd, dev);
954 }
955 
956 /**
957  * pm_genpd_freeze - Freezing a device in an I/O PM domain.
958  * @dev: Device to freeze.
959  *
960  * Freeze a device under the assumption that its pm_domain field points to the
961  * domain member of an object of type struct generic_pm_domain representing
962  * a power domain consisting of I/O devices.
963  */
pm_genpd_freeze(struct device * dev)964 static int pm_genpd_freeze(struct device *dev)
965 {
966 	struct generic_pm_domain *genpd;
967 
968 	dev_dbg(dev, "%s()\n", __func__);
969 
970 	genpd = dev_to_genpd(dev);
971 	if (IS_ERR(genpd))
972 		return -EINVAL;
973 
974 	return genpd->suspend_power_off ? 0 : genpd_freeze_dev(genpd, dev);
975 }
976 
977 /**
978  * pm_genpd_freeze_late - Late freeze of a device in an I/O PM domain.
979  * @dev: Device to freeze.
980  *
981  * Carry out a late freeze of a device under the assumption that its
982  * pm_domain field points to the domain member of an object of type
983  * struct generic_pm_domain representing a power domain consisting of I/O
984  * devices.
985  */
pm_genpd_freeze_late(struct device * dev)986 static int pm_genpd_freeze_late(struct device *dev)
987 {
988 	struct generic_pm_domain *genpd;
989 
990 	dev_dbg(dev, "%s()\n", __func__);
991 
992 	genpd = dev_to_genpd(dev);
993 	if (IS_ERR(genpd))
994 		return -EINVAL;
995 
996 	return genpd->suspend_power_off ? 0 : genpd_freeze_late(genpd, dev);
997 }
998 
999 /**
1000  * pm_genpd_freeze_noirq - Completion of freezing a device in an I/O PM domain.
1001  * @dev: Device to freeze.
1002  *
1003  * Carry out a late freeze of a device under the assumption that its
1004  * pm_domain field points to the domain member of an object of type
1005  * struct generic_pm_domain representing a power domain consisting of I/O
1006  * devices.
1007  */
pm_genpd_freeze_noirq(struct device * dev)1008 static int pm_genpd_freeze_noirq(struct device *dev)
1009 {
1010 	struct generic_pm_domain *genpd;
1011 
1012 	dev_dbg(dev, "%s()\n", __func__);
1013 
1014 	genpd = dev_to_genpd(dev);
1015 	if (IS_ERR(genpd))
1016 		return -EINVAL;
1017 
1018 	return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1019 		0 : genpd_stop_dev(genpd, dev);
1020 }
1021 
1022 /**
1023  * pm_genpd_thaw_noirq - Early thaw of device in an I/O PM domain.
1024  * @dev: Device to thaw.
1025  *
1026  * Start the device, unless power has been removed from the domain already
1027  * before the system transition.
1028  */
pm_genpd_thaw_noirq(struct device * dev)1029 static int pm_genpd_thaw_noirq(struct device *dev)
1030 {
1031 	struct generic_pm_domain *genpd;
1032 
1033 	dev_dbg(dev, "%s()\n", __func__);
1034 
1035 	genpd = dev_to_genpd(dev);
1036 	if (IS_ERR(genpd))
1037 		return -EINVAL;
1038 
1039 	return genpd->suspend_power_off || dev_gpd_data(dev)->always_on ?
1040 		0 : genpd_start_dev(genpd, dev);
1041 }
1042 
1043 /**
1044  * pm_genpd_thaw_early - Early thaw of device in an I/O PM domain.
1045  * @dev: Device to thaw.
1046  *
1047  * Carry out an early thaw of a device under the assumption that its
1048  * pm_domain field points to the domain member of an object of type
1049  * struct generic_pm_domain representing a power domain consisting of I/O
1050  * devices.
1051  */
pm_genpd_thaw_early(struct device * dev)1052 static int pm_genpd_thaw_early(struct device *dev)
1053 {
1054 	struct generic_pm_domain *genpd;
1055 
1056 	dev_dbg(dev, "%s()\n", __func__);
1057 
1058 	genpd = dev_to_genpd(dev);
1059 	if (IS_ERR(genpd))
1060 		return -EINVAL;
1061 
1062 	return genpd->suspend_power_off ? 0 : genpd_thaw_early(genpd, dev);
1063 }
1064 
1065 /**
1066  * pm_genpd_thaw - Thaw a device belonging to an I/O power domain.
1067  * @dev: Device to thaw.
1068  *
1069  * Thaw a device under the assumption that its pm_domain field points to the
1070  * domain member of an object of type struct generic_pm_domain representing
1071  * a power domain consisting of I/O devices.
1072  */
pm_genpd_thaw(struct device * dev)1073 static int pm_genpd_thaw(struct device *dev)
1074 {
1075 	struct generic_pm_domain *genpd;
1076 
1077 	dev_dbg(dev, "%s()\n", __func__);
1078 
1079 	genpd = dev_to_genpd(dev);
1080 	if (IS_ERR(genpd))
1081 		return -EINVAL;
1082 
1083 	return genpd->suspend_power_off ? 0 : genpd_thaw_dev(genpd, dev);
1084 }
1085 
1086 /**
1087  * pm_genpd_restore_noirq - Start of restore of device in an I/O PM domain.
1088  * @dev: Device to resume.
1089  *
1090  * Make sure the domain will be in the same power state as before the
1091  * hibernation the system is resuming from and start the device if necessary.
1092  */
pm_genpd_restore_noirq(struct device * dev)1093 static int pm_genpd_restore_noirq(struct device *dev)
1094 {
1095 	struct generic_pm_domain *genpd;
1096 
1097 	dev_dbg(dev, "%s()\n", __func__);
1098 
1099 	genpd = dev_to_genpd(dev);
1100 	if (IS_ERR(genpd))
1101 		return -EINVAL;
1102 
1103 	/*
1104 	 * Since all of the "noirq" callbacks are executed sequentially, it is
1105 	 * guaranteed that this function will never run twice in parallel for
1106 	 * the same PM domain, so it is not necessary to use locking here.
1107 	 *
1108 	 * At this point suspended_count == 0 means we are being run for the
1109 	 * first time for the given domain in the present cycle.
1110 	 */
1111 	if (genpd->suspended_count++ == 0) {
1112 		/*
1113 		 * The boot kernel might put the domain into arbitrary state,
1114 		 * so make it appear as powered off to pm_genpd_poweron(), so
1115 		 * that it tries to power it on in case it was really off.
1116 		 */
1117 		genpd->status = GPD_STATE_POWER_OFF;
1118 		if (genpd->suspend_power_off) {
1119 			/*
1120 			 * If the domain was off before the hibernation, make
1121 			 * sure it will be off going forward.
1122 			 */
1123 			if (genpd->power_off)
1124 				genpd->power_off(genpd);
1125 
1126 			return 0;
1127 		}
1128 	}
1129 
1130 	if (genpd->suspend_power_off)
1131 		return 0;
1132 
1133 	pm_genpd_poweron(genpd);
1134 
1135 	return dev_gpd_data(dev)->always_on ? 0 : genpd_start_dev(genpd, dev);
1136 }
1137 
1138 /**
1139  * pm_genpd_complete - Complete power transition of a device in a power domain.
1140  * @dev: Device to complete the transition of.
1141  *
1142  * Complete a power transition of a device (during a system-wide power
1143  * transition) under the assumption that its pm_domain field points to the
1144  * domain member of an object of type struct generic_pm_domain representing
1145  * a power domain consisting of I/O devices.
1146  */
pm_genpd_complete(struct device * dev)1147 static void pm_genpd_complete(struct device *dev)
1148 {
1149 	struct generic_pm_domain *genpd;
1150 	bool run_complete;
1151 
1152 	dev_dbg(dev, "%s()\n", __func__);
1153 
1154 	genpd = dev_to_genpd(dev);
1155 	if (IS_ERR(genpd))
1156 		return;
1157 
1158 	mutex_lock(&genpd->lock);
1159 
1160 	run_complete = !genpd->suspend_power_off;
1161 	if (--genpd->prepared_count == 0)
1162 		genpd->suspend_power_off = false;
1163 
1164 	mutex_unlock(&genpd->lock);
1165 
1166 	if (run_complete) {
1167 		pm_generic_complete(dev);
1168 		pm_runtime_set_active(dev);
1169 		pm_runtime_enable(dev);
1170 		pm_runtime_idle(dev);
1171 	}
1172 }
1173 
1174 #else
1175 
1176 #define pm_genpd_prepare		NULL
1177 #define pm_genpd_suspend		NULL
1178 #define pm_genpd_suspend_late		NULL
1179 #define pm_genpd_suspend_noirq		NULL
1180 #define pm_genpd_resume_early		NULL
1181 #define pm_genpd_resume_noirq		NULL
1182 #define pm_genpd_resume			NULL
1183 #define pm_genpd_freeze			NULL
1184 #define pm_genpd_freeze_late		NULL
1185 #define pm_genpd_freeze_noirq		NULL
1186 #define pm_genpd_thaw_early		NULL
1187 #define pm_genpd_thaw_noirq		NULL
1188 #define pm_genpd_thaw			NULL
1189 #define pm_genpd_restore_noirq		NULL
1190 #define pm_genpd_complete		NULL
1191 
1192 #endif /* CONFIG_PM_SLEEP */
1193 
1194 /**
1195  * __pm_genpd_add_device - Add a device to an I/O PM domain.
1196  * @genpd: PM domain to add the device to.
1197  * @dev: Device to be added.
1198  * @td: Set of PM QoS timing parameters to attach to the device.
1199  */
__pm_genpd_add_device(struct generic_pm_domain * genpd,struct device * dev,struct gpd_timing_data * td)1200 int __pm_genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
1201 			  struct gpd_timing_data *td)
1202 {
1203 	struct generic_pm_domain_data *gpd_data;
1204 	struct pm_domain_data *pdd;
1205 	int ret = 0;
1206 
1207 	dev_dbg(dev, "%s()\n", __func__);
1208 
1209 	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1210 		return -EINVAL;
1211 
1212 	genpd_acquire_lock(genpd);
1213 
1214 	if (genpd->status == GPD_STATE_POWER_OFF) {
1215 		ret = -EINVAL;
1216 		goto out;
1217 	}
1218 
1219 	if (genpd->prepared_count > 0) {
1220 		ret = -EAGAIN;
1221 		goto out;
1222 	}
1223 
1224 	list_for_each_entry(pdd, &genpd->dev_list, list_node)
1225 		if (pdd->dev == dev) {
1226 			ret = -EINVAL;
1227 			goto out;
1228 		}
1229 
1230 	gpd_data = kzalloc(sizeof(*gpd_data), GFP_KERNEL);
1231 	if (!gpd_data) {
1232 		ret = -ENOMEM;
1233 		goto out;
1234 	}
1235 
1236 	genpd->device_count++;
1237 
1238 	dev->pm_domain = &genpd->domain;
1239 	dev_pm_get_subsys_data(dev);
1240 	dev->power.subsys_data->domain_data = &gpd_data->base;
1241 	gpd_data->base.dev = dev;
1242 	gpd_data->need_restore = false;
1243 	list_add_tail(&gpd_data->base.list_node, &genpd->dev_list);
1244 	if (td)
1245 		gpd_data->td = *td;
1246 
1247  out:
1248 	genpd_release_lock(genpd);
1249 
1250 	return ret;
1251 }
1252 
1253 /**
1254  * __pm_genpd_of_add_device - Add a device to an I/O PM domain.
1255  * @genpd_node: Device tree node pointer representing a PM domain to which the
1256  *   the device is added to.
1257  * @dev: Device to be added.
1258  * @td: Set of PM QoS timing parameters to attach to the device.
1259  */
__pm_genpd_of_add_device(struct device_node * genpd_node,struct device * dev,struct gpd_timing_data * td)1260 int __pm_genpd_of_add_device(struct device_node *genpd_node, struct device *dev,
1261 			     struct gpd_timing_data *td)
1262 {
1263 	struct generic_pm_domain *genpd = NULL, *gpd;
1264 
1265 	dev_dbg(dev, "%s()\n", __func__);
1266 
1267 	if (IS_ERR_OR_NULL(genpd_node) || IS_ERR_OR_NULL(dev))
1268 		return -EINVAL;
1269 
1270 	mutex_lock(&gpd_list_lock);
1271 	list_for_each_entry(gpd, &gpd_list, gpd_list_node) {
1272 		if (gpd->of_node == genpd_node) {
1273 			genpd = gpd;
1274 			break;
1275 		}
1276 	}
1277 	mutex_unlock(&gpd_list_lock);
1278 
1279 	if (!genpd)
1280 		return -EINVAL;
1281 
1282 	return __pm_genpd_add_device(genpd, dev, td);
1283 }
1284 
1285 /**
1286  * pm_genpd_remove_device - Remove a device from an I/O PM domain.
1287  * @genpd: PM domain to remove the device from.
1288  * @dev: Device to be removed.
1289  */
pm_genpd_remove_device(struct generic_pm_domain * genpd,struct device * dev)1290 int pm_genpd_remove_device(struct generic_pm_domain *genpd,
1291 			   struct device *dev)
1292 {
1293 	struct pm_domain_data *pdd;
1294 	int ret = -EINVAL;
1295 
1296 	dev_dbg(dev, "%s()\n", __func__);
1297 
1298 	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(dev))
1299 		return -EINVAL;
1300 
1301 	genpd_acquire_lock(genpd);
1302 
1303 	if (genpd->prepared_count > 0) {
1304 		ret = -EAGAIN;
1305 		goto out;
1306 	}
1307 
1308 	list_for_each_entry(pdd, &genpd->dev_list, list_node) {
1309 		if (pdd->dev != dev)
1310 			continue;
1311 
1312 		list_del_init(&pdd->list_node);
1313 		pdd->dev = NULL;
1314 		dev_pm_put_subsys_data(dev);
1315 		dev->pm_domain = NULL;
1316 		kfree(to_gpd_data(pdd));
1317 
1318 		genpd->device_count--;
1319 
1320 		ret = 0;
1321 		break;
1322 	}
1323 
1324  out:
1325 	genpd_release_lock(genpd);
1326 
1327 	return ret;
1328 }
1329 
1330 /**
1331  * pm_genpd_dev_always_on - Set/unset the "always on" flag for a given device.
1332  * @dev: Device to set/unset the flag for.
1333  * @val: The new value of the device's "always on" flag.
1334  */
pm_genpd_dev_always_on(struct device * dev,bool val)1335 void pm_genpd_dev_always_on(struct device *dev, bool val)
1336 {
1337 	struct pm_subsys_data *psd;
1338 	unsigned long flags;
1339 
1340 	spin_lock_irqsave(&dev->power.lock, flags);
1341 
1342 	psd = dev_to_psd(dev);
1343 	if (psd && psd->domain_data)
1344 		to_gpd_data(psd->domain_data)->always_on = val;
1345 
1346 	spin_unlock_irqrestore(&dev->power.lock, flags);
1347 }
1348 EXPORT_SYMBOL_GPL(pm_genpd_dev_always_on);
1349 
1350 /**
1351  * pm_genpd_add_subdomain - Add a subdomain to an I/O PM domain.
1352  * @genpd: Master PM domain to add the subdomain to.
1353  * @subdomain: Subdomain to be added.
1354  */
pm_genpd_add_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * subdomain)1355 int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
1356 			   struct generic_pm_domain *subdomain)
1357 {
1358 	struct gpd_link *link;
1359 	int ret = 0;
1360 
1361 	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1362 		return -EINVAL;
1363 
1364  start:
1365 	genpd_acquire_lock(genpd);
1366 	mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1367 
1368 	if (subdomain->status != GPD_STATE_POWER_OFF
1369 	    && subdomain->status != GPD_STATE_ACTIVE) {
1370 		mutex_unlock(&subdomain->lock);
1371 		genpd_release_lock(genpd);
1372 		goto start;
1373 	}
1374 
1375 	if (genpd->status == GPD_STATE_POWER_OFF
1376 	    &&  subdomain->status != GPD_STATE_POWER_OFF) {
1377 		ret = -EINVAL;
1378 		goto out;
1379 	}
1380 
1381 	list_for_each_entry(link, &genpd->slave_links, slave_node) {
1382 		if (link->slave == subdomain && link->master == genpd) {
1383 			ret = -EINVAL;
1384 			goto out;
1385 		}
1386 	}
1387 
1388 	link = kzalloc(sizeof(*link), GFP_KERNEL);
1389 	if (!link) {
1390 		ret = -ENOMEM;
1391 		goto out;
1392 	}
1393 	link->master = genpd;
1394 	list_add_tail(&link->master_node, &genpd->master_links);
1395 	link->slave = subdomain;
1396 	list_add_tail(&link->slave_node, &subdomain->slave_links);
1397 	if (subdomain->status != GPD_STATE_POWER_OFF)
1398 		genpd_sd_counter_inc(genpd);
1399 
1400  out:
1401 	mutex_unlock(&subdomain->lock);
1402 	genpd_release_lock(genpd);
1403 
1404 	return ret;
1405 }
1406 
1407 /**
1408  * pm_genpd_remove_subdomain - Remove a subdomain from an I/O PM domain.
1409  * @genpd: Master PM domain to remove the subdomain from.
1410  * @subdomain: Subdomain to be removed.
1411  */
pm_genpd_remove_subdomain(struct generic_pm_domain * genpd,struct generic_pm_domain * subdomain)1412 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
1413 			      struct generic_pm_domain *subdomain)
1414 {
1415 	struct gpd_link *link;
1416 	int ret = -EINVAL;
1417 
1418 	if (IS_ERR_OR_NULL(genpd) || IS_ERR_OR_NULL(subdomain))
1419 		return -EINVAL;
1420 
1421  start:
1422 	genpd_acquire_lock(genpd);
1423 
1424 	list_for_each_entry(link, &genpd->master_links, master_node) {
1425 		if (link->slave != subdomain)
1426 			continue;
1427 
1428 		mutex_lock_nested(&subdomain->lock, SINGLE_DEPTH_NESTING);
1429 
1430 		if (subdomain->status != GPD_STATE_POWER_OFF
1431 		    && subdomain->status != GPD_STATE_ACTIVE) {
1432 			mutex_unlock(&subdomain->lock);
1433 			genpd_release_lock(genpd);
1434 			goto start;
1435 		}
1436 
1437 		list_del(&link->master_node);
1438 		list_del(&link->slave_node);
1439 		kfree(link);
1440 		if (subdomain->status != GPD_STATE_POWER_OFF)
1441 			genpd_sd_counter_dec(genpd);
1442 
1443 		mutex_unlock(&subdomain->lock);
1444 
1445 		ret = 0;
1446 		break;
1447 	}
1448 
1449 	genpd_release_lock(genpd);
1450 
1451 	return ret;
1452 }
1453 
1454 /**
1455  * pm_genpd_add_callbacks - Add PM domain callbacks to a given device.
1456  * @dev: Device to add the callbacks to.
1457  * @ops: Set of callbacks to add.
1458  * @td: Timing data to add to the device along with the callbacks (optional).
1459  */
pm_genpd_add_callbacks(struct device * dev,struct gpd_dev_ops * ops,struct gpd_timing_data * td)1460 int pm_genpd_add_callbacks(struct device *dev, struct gpd_dev_ops *ops,
1461 			   struct gpd_timing_data *td)
1462 {
1463 	struct pm_domain_data *pdd;
1464 	int ret = 0;
1465 
1466 	if (!(dev && dev->power.subsys_data && ops))
1467 		return -EINVAL;
1468 
1469 	pm_runtime_disable(dev);
1470 	device_pm_lock();
1471 
1472 	pdd = dev->power.subsys_data->domain_data;
1473 	if (pdd) {
1474 		struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1475 
1476 		gpd_data->ops = *ops;
1477 		if (td)
1478 			gpd_data->td = *td;
1479 	} else {
1480 		ret = -EINVAL;
1481 	}
1482 
1483 	device_pm_unlock();
1484 	pm_runtime_enable(dev);
1485 
1486 	return ret;
1487 }
1488 EXPORT_SYMBOL_GPL(pm_genpd_add_callbacks);
1489 
1490 /**
1491  * __pm_genpd_remove_callbacks - Remove PM domain callbacks from a given device.
1492  * @dev: Device to remove the callbacks from.
1493  * @clear_td: If set, clear the device's timing data too.
1494  */
__pm_genpd_remove_callbacks(struct device * dev,bool clear_td)1495 int __pm_genpd_remove_callbacks(struct device *dev, bool clear_td)
1496 {
1497 	struct pm_domain_data *pdd;
1498 	int ret = 0;
1499 
1500 	if (!(dev && dev->power.subsys_data))
1501 		return -EINVAL;
1502 
1503 	pm_runtime_disable(dev);
1504 	device_pm_lock();
1505 
1506 	pdd = dev->power.subsys_data->domain_data;
1507 	if (pdd) {
1508 		struct generic_pm_domain_data *gpd_data = to_gpd_data(pdd);
1509 
1510 		gpd_data->ops = (struct gpd_dev_ops){ 0 };
1511 		if (clear_td)
1512 			gpd_data->td = (struct gpd_timing_data){ 0 };
1513 	} else {
1514 		ret = -EINVAL;
1515 	}
1516 
1517 	device_pm_unlock();
1518 	pm_runtime_enable(dev);
1519 
1520 	return ret;
1521 }
1522 EXPORT_SYMBOL_GPL(__pm_genpd_remove_callbacks);
1523 
1524 /* Default device callbacks for generic PM domains. */
1525 
1526 /**
1527  * pm_genpd_default_save_state - Default "save device state" for PM domians.
1528  * @dev: Device to handle.
1529  */
pm_genpd_default_save_state(struct device * dev)1530 static int pm_genpd_default_save_state(struct device *dev)
1531 {
1532 	int (*cb)(struct device *__dev);
1533 	struct device_driver *drv = dev->driver;
1534 
1535 	cb = dev_gpd_data(dev)->ops.save_state;
1536 	if (cb)
1537 		return cb(dev);
1538 
1539 	if (drv && drv->pm && drv->pm->runtime_suspend)
1540 		return drv->pm->runtime_suspend(dev);
1541 
1542 	return 0;
1543 }
1544 
1545 /**
1546  * pm_genpd_default_restore_state - Default PM domians "restore device state".
1547  * @dev: Device to handle.
1548  */
pm_genpd_default_restore_state(struct device * dev)1549 static int pm_genpd_default_restore_state(struct device *dev)
1550 {
1551 	int (*cb)(struct device *__dev);
1552 	struct device_driver *drv = dev->driver;
1553 
1554 	cb = dev_gpd_data(dev)->ops.restore_state;
1555 	if (cb)
1556 		return cb(dev);
1557 
1558 	if (drv && drv->pm && drv->pm->runtime_resume)
1559 		return drv->pm->runtime_resume(dev);
1560 
1561 	return 0;
1562 }
1563 
1564 #ifdef CONFIG_PM_SLEEP
1565 
1566 /**
1567  * pm_genpd_default_suspend - Default "device suspend" for PM domians.
1568  * @dev: Device to handle.
1569  */
pm_genpd_default_suspend(struct device * dev)1570 static int pm_genpd_default_suspend(struct device *dev)
1571 {
1572 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend;
1573 
1574 	return cb ? cb(dev) : pm_generic_suspend(dev);
1575 }
1576 
1577 /**
1578  * pm_genpd_default_suspend_late - Default "late device suspend" for PM domians.
1579  * @dev: Device to handle.
1580  */
pm_genpd_default_suspend_late(struct device * dev)1581 static int pm_genpd_default_suspend_late(struct device *dev)
1582 {
1583 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.suspend_late;
1584 
1585 	return cb ? cb(dev) : pm_generic_suspend_late(dev);
1586 }
1587 
1588 /**
1589  * pm_genpd_default_resume_early - Default "early device resume" for PM domians.
1590  * @dev: Device to handle.
1591  */
pm_genpd_default_resume_early(struct device * dev)1592 static int pm_genpd_default_resume_early(struct device *dev)
1593 {
1594 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume_early;
1595 
1596 	return cb ? cb(dev) : pm_generic_resume_early(dev);
1597 }
1598 
1599 /**
1600  * pm_genpd_default_resume - Default "device resume" for PM domians.
1601  * @dev: Device to handle.
1602  */
pm_genpd_default_resume(struct device * dev)1603 static int pm_genpd_default_resume(struct device *dev)
1604 {
1605 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.resume;
1606 
1607 	return cb ? cb(dev) : pm_generic_resume(dev);
1608 }
1609 
1610 /**
1611  * pm_genpd_default_freeze - Default "device freeze" for PM domians.
1612  * @dev: Device to handle.
1613  */
pm_genpd_default_freeze(struct device * dev)1614 static int pm_genpd_default_freeze(struct device *dev)
1615 {
1616 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze;
1617 
1618 	return cb ? cb(dev) : pm_generic_freeze(dev);
1619 }
1620 
1621 /**
1622  * pm_genpd_default_freeze_late - Default "late device freeze" for PM domians.
1623  * @dev: Device to handle.
1624  */
pm_genpd_default_freeze_late(struct device * dev)1625 static int pm_genpd_default_freeze_late(struct device *dev)
1626 {
1627 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.freeze_late;
1628 
1629 	return cb ? cb(dev) : pm_generic_freeze_late(dev);
1630 }
1631 
1632 /**
1633  * pm_genpd_default_thaw_early - Default "early device thaw" for PM domians.
1634  * @dev: Device to handle.
1635  */
pm_genpd_default_thaw_early(struct device * dev)1636 static int pm_genpd_default_thaw_early(struct device *dev)
1637 {
1638 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw_early;
1639 
1640 	return cb ? cb(dev) : pm_generic_thaw_early(dev);
1641 }
1642 
1643 /**
1644  * pm_genpd_default_thaw - Default "device thaw" for PM domians.
1645  * @dev: Device to handle.
1646  */
pm_genpd_default_thaw(struct device * dev)1647 static int pm_genpd_default_thaw(struct device *dev)
1648 {
1649 	int (*cb)(struct device *__dev) = dev_gpd_data(dev)->ops.thaw;
1650 
1651 	return cb ? cb(dev) : pm_generic_thaw(dev);
1652 }
1653 
1654 #else /* !CONFIG_PM_SLEEP */
1655 
1656 #define pm_genpd_default_suspend	NULL
1657 #define pm_genpd_default_suspend_late	NULL
1658 #define pm_genpd_default_resume_early	NULL
1659 #define pm_genpd_default_resume		NULL
1660 #define pm_genpd_default_freeze		NULL
1661 #define pm_genpd_default_freeze_late	NULL
1662 #define pm_genpd_default_thaw_early	NULL
1663 #define pm_genpd_default_thaw		NULL
1664 
1665 #endif /* !CONFIG_PM_SLEEP */
1666 
1667 /**
1668  * pm_genpd_init - Initialize a generic I/O PM domain object.
1669  * @genpd: PM domain object to initialize.
1670  * @gov: PM domain governor to associate with the domain (may be NULL).
1671  * @is_off: Initial value of the domain's power_is_off field.
1672  */
pm_genpd_init(struct generic_pm_domain * genpd,struct dev_power_governor * gov,bool is_off)1673 void pm_genpd_init(struct generic_pm_domain *genpd,
1674 		   struct dev_power_governor *gov, bool is_off)
1675 {
1676 	if (IS_ERR_OR_NULL(genpd))
1677 		return;
1678 
1679 	INIT_LIST_HEAD(&genpd->master_links);
1680 	INIT_LIST_HEAD(&genpd->slave_links);
1681 	INIT_LIST_HEAD(&genpd->dev_list);
1682 	mutex_init(&genpd->lock);
1683 	genpd->gov = gov;
1684 	INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
1685 	genpd->in_progress = 0;
1686 	atomic_set(&genpd->sd_count, 0);
1687 	genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
1688 	init_waitqueue_head(&genpd->status_wait_queue);
1689 	genpd->poweroff_task = NULL;
1690 	genpd->resume_count = 0;
1691 	genpd->device_count = 0;
1692 	genpd->max_off_time_ns = -1;
1693 	genpd->domain.ops.runtime_suspend = pm_genpd_runtime_suspend;
1694 	genpd->domain.ops.runtime_resume = pm_genpd_runtime_resume;
1695 	genpd->domain.ops.runtime_idle = pm_generic_runtime_idle;
1696 	genpd->domain.ops.prepare = pm_genpd_prepare;
1697 	genpd->domain.ops.suspend = pm_genpd_suspend;
1698 	genpd->domain.ops.suspend_late = pm_genpd_suspend_late;
1699 	genpd->domain.ops.suspend_noirq = pm_genpd_suspend_noirq;
1700 	genpd->domain.ops.resume_noirq = pm_genpd_resume_noirq;
1701 	genpd->domain.ops.resume_early = pm_genpd_resume_early;
1702 	genpd->domain.ops.resume = pm_genpd_resume;
1703 	genpd->domain.ops.freeze = pm_genpd_freeze;
1704 	genpd->domain.ops.freeze_late = pm_genpd_freeze_late;
1705 	genpd->domain.ops.freeze_noirq = pm_genpd_freeze_noirq;
1706 	genpd->domain.ops.thaw_noirq = pm_genpd_thaw_noirq;
1707 	genpd->domain.ops.thaw_early = pm_genpd_thaw_early;
1708 	genpd->domain.ops.thaw = pm_genpd_thaw;
1709 	genpd->domain.ops.poweroff = pm_genpd_suspend;
1710 	genpd->domain.ops.poweroff_late = pm_genpd_suspend_late;
1711 	genpd->domain.ops.poweroff_noirq = pm_genpd_suspend_noirq;
1712 	genpd->domain.ops.restore_noirq = pm_genpd_restore_noirq;
1713 	genpd->domain.ops.restore_early = pm_genpd_resume_early;
1714 	genpd->domain.ops.restore = pm_genpd_resume;
1715 	genpd->domain.ops.complete = pm_genpd_complete;
1716 	genpd->dev_ops.save_state = pm_genpd_default_save_state;
1717 	genpd->dev_ops.restore_state = pm_genpd_default_restore_state;
1718 	genpd->dev_ops.suspend = pm_genpd_default_suspend;
1719 	genpd->dev_ops.suspend_late = pm_genpd_default_suspend_late;
1720 	genpd->dev_ops.resume_early = pm_genpd_default_resume_early;
1721 	genpd->dev_ops.resume = pm_genpd_default_resume;
1722 	genpd->dev_ops.freeze = pm_genpd_default_freeze;
1723 	genpd->dev_ops.freeze_late = pm_genpd_default_freeze_late;
1724 	genpd->dev_ops.thaw_early = pm_genpd_default_thaw_early;
1725 	genpd->dev_ops.thaw = pm_genpd_default_thaw;
1726 	mutex_lock(&gpd_list_lock);
1727 	list_add(&genpd->gpd_list_node, &gpd_list);
1728 	mutex_unlock(&gpd_list_lock);
1729 }
1730