xref: /netbsd-src/sys/kern/kern_pmf.c (revision dd3ee07da436799d8de85f3055253118b76bf345)
1 /* $NetBSD: kern_pmf.c,v 1.48 2022/03/28 12:38:59 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.48 2022/03/28 12:38:59 riastradh Exp $");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/kmem.h>
35 #include <sys/buf.h>
36 #include <sys/callout.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/device_impl.h>
40 #include <sys/pmf.h>
41 #include <sys/queue.h>
42 #include <sys/sched.h>
43 #include <sys/workqueue.h>
44 #include <prop/proplib.h>
45 #include <sys/condvar.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/reboot.h>	/* for RB_NOSYNC */
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/vfs_syscalls.h>
52 
53 /* XXX ugly special case, but for now the only client */
54 #include "wsdisplay.h"
55 #if NWSDISPLAY > 0
56 #include <dev/wscons/wsdisplayvar.h>
57 #endif
58 
59 #define PMF_DEBUG
60 
61 #ifdef PMF_DEBUG
62 int  pmf_debug_event;
63 int  pmf_debug_suspend;
64 int  pmf_debug_suspensor;
65 int  pmf_debug_idle;
66 int  pmf_debug_transition;
67 
68 #define	PMF_SUSPENSOR_PRINTF(x)		if (pmf_debug_suspensor) printf x
69 #define	PMF_SUSPEND_PRINTF(x)		if (pmf_debug_suspend) printf x
70 #define	PMF_EVENT_PRINTF(x)		if (pmf_debug_event) printf x
71 #define	PMF_IDLE_PRINTF(x)		if (pmf_debug_idle) printf x
72 #define	PMF_TRANSITION_PRINTF(x)	if (pmf_debug_transition) printf x
73 #define	PMF_TRANSITION_PRINTF2(y,x)	if (pmf_debug_transition>y) printf x
74 #else
75 #define	PMF_SUSPENSOR_PRINTF(x)		do { } while (0)
76 #define	PMF_SUSPEND_PRINTF(x)		do { } while (0)
77 #define	PMF_EVENT_PRINTF(x)		do { } while (0)
78 #define	PMF_IDLE_PRINTF(x)		do { } while (0)
79 #define	PMF_TRANSITION_PRINTF(x)	do { } while (0)
80 #define	PMF_TRANSITION_PRINTF2(y,x)	do { } while (0)
81 #endif
82 
83 static prop_dictionary_t pmf_platform = NULL;
84 static struct workqueue *pmf_event_workqueue;
85 static struct workqueue *pmf_suspend_workqueue;
86 
87 typedef struct pmf_event_handler {
88 	TAILQ_ENTRY(pmf_event_handler) pmf_link;
89 	pmf_generic_event_t pmf_event;
90 	void (*pmf_handler)(device_t);
91 	device_t pmf_device;
92 	bool pmf_global;
93 } pmf_event_handler_t;
94 
95 static TAILQ_HEAD(, pmf_event_handler) pmf_all_events =
96     TAILQ_HEAD_INITIALIZER(pmf_all_events);
97 
98 typedef struct pmf_event_workitem {
99 	struct work				pew_work;
100 	pmf_generic_event_t			pew_event;
101 	device_t				pew_device;
102 } pmf_event_workitem_t;
103 
104 typedef struct pmf_suspend_workitem {
105 	struct work	psw_work;
106 	device_t	psw_dev;
107 	pmf_qual_t	psw_qual;
108 } pmf_suspend_workitem_t;
109 
110 static struct pool pew_pl;
111 
112 static pmf_event_workitem_t *pmf_event_workitem_get(void);
113 static void pmf_event_workitem_put(pmf_event_workitem_t *);
114 
115 bool pmf_device_resume_locked(device_t, const pmf_qual_t *);
116 bool pmf_device_suspend_locked(device_t, const pmf_qual_t *);
117 static bool device_pmf_any_suspensor(device_t, devact_level_t);
118 
119 static bool
120 complete_suspension(device_t dev, const device_suspensor_t **susp,
121     const pmf_qual_t *pqp)
122 {
123 	int i;
124 	pmf_qual_t pq;
125 	const device_suspensor_t *ds;
126 
127 	ds = pmf_qual_suspension(pqp);
128 	KASSERT(ds->ds_delegator != NULL);
129 
130 	pq = *pqp;
131 	pq.pq_suspensor = ds->ds_delegator;
132 
133 	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
134 		if (susp[i] != ds)
135 			continue;
136 		if (!pmf_device_suspend(dev, &pq))
137 			return false;
138 	}
139 	return true;
140 }
141 
142 static void
143 pmf_suspend_worker(struct work *wk, void *dummy)
144 {
145 	pmf_suspend_workitem_t *psw;
146 	deviter_t di;
147 	device_t dev;
148 
149 	psw = (void *)wk;
150 	KASSERT(wk == &psw->psw_work);
151 	KASSERT(psw != NULL);
152 
153 	for (dev = deviter_first(&di, 0); dev != NULL;
154 	     dev = deviter_next(&di)) {
155 		if (dev == psw->psw_dev && device_pmf_lock(dev))
156 			break;
157 	}
158 	deviter_release(&di);
159 
160 	if (dev == NULL)
161 		return;
162 
163 	switch (pmf_qual_depth(&psw->psw_qual)) {
164 	case DEVACT_LEVEL_FULL:
165 		if (!complete_suspension(dev, dev->dv_class_suspensors,
166 		    &psw->psw_qual))
167 			break;
168 		/*FALLTHROUGH*/
169 	case DEVACT_LEVEL_DRIVER:
170 		if (!complete_suspension(dev, dev->dv_driver_suspensors,
171 		    &psw->psw_qual))
172 			break;
173 		/*FALLTHROUGH*/
174 	case DEVACT_LEVEL_BUS:
175 		if (!complete_suspension(dev, dev->dv_bus_suspensors,
176 		    &psw->psw_qual))
177 			break;
178 	}
179 	device_pmf_unlock(dev);
180 	kmem_free(psw, sizeof(*psw));
181 }
182 
183 static void
184 pmf_event_worker(struct work *wk, void *dummy)
185 {
186 	pmf_event_workitem_t *pew;
187 	pmf_event_handler_t *event;
188 
189 	pew = (void *)wk;
190 	KASSERT(wk == &pew->pew_work);
191 	KASSERT(pew != NULL);
192 
193 	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
194 		if (event->pmf_event != pew->pew_event)
195 			continue;
196 		if (event->pmf_device == pew->pew_device || event->pmf_global)
197 			(*event->pmf_handler)(event->pmf_device);
198 	}
199 
200 	pmf_event_workitem_put(pew);
201 }
202 
203 static bool
204 pmf_check_system_drivers(void)
205 {
206 	device_t curdev;
207 	bool unsupported_devs;
208 	deviter_t di;
209 
210 	unsupported_devs = false;
211 	for (curdev = deviter_first(&di, 0); curdev != NULL;
212 	     curdev = deviter_next(&di)) {
213 		if (device_pmf_is_registered(curdev))
214 			continue;
215 		if (!unsupported_devs)
216 			printf("Devices without power management support:");
217 		printf(" %s", device_xname(curdev));
218 		unsupported_devs = true;
219 	}
220 	deviter_release(&di);
221 	if (unsupported_devs) {
222 		printf("\n");
223 		return false;
224 	}
225 	return true;
226 }
227 
228 bool
229 pmf_system_bus_resume(const pmf_qual_t *qual)
230 {
231 	bool rv;
232 	device_t curdev;
233 	deviter_t di;
234 
235 	aprint_debug("Powering devices:");
236 	/* D0 handlers are run in order */
237 	rv = true;
238 	for (curdev = deviter_first(&di, DEVITER_F_ROOT_FIRST); curdev != NULL;
239 	     curdev = deviter_next(&di)) {
240 		if (!device_pmf_is_registered(curdev))
241 			continue;
242 		if (device_is_active(curdev) ||
243 		    !device_is_enabled(curdev))
244 			continue;
245 
246 		aprint_debug(" %s", device_xname(curdev));
247 
248 		if (!device_pmf_bus_resume(curdev, qual)) {
249 			rv = false;
250 			aprint_debug("(failed)");
251 		}
252 	}
253 	deviter_release(&di);
254 	aprint_debug("\n");
255 
256 	return rv;
257 }
258 
259 bool
260 pmf_system_resume(const pmf_qual_t *qual)
261 {
262 	bool rv;
263 	device_t curdev, parent;
264 	deviter_t di;
265 
266 	if (!pmf_check_system_drivers())
267 		return false;
268 
269 	aprint_debug("Resuming devices:");
270 	/* D0 handlers are run in order */
271 	rv = true;
272 	for (curdev = deviter_first(&di, DEVITER_F_ROOT_FIRST); curdev != NULL;
273 	     curdev = deviter_next(&di)) {
274 		if (device_is_active(curdev) ||
275 		    !device_is_enabled(curdev))
276 			continue;
277 		parent = device_parent(curdev);
278 		if (parent != NULL &&
279 		    !device_is_active(parent))
280 			continue;
281 
282 		aprint_debug(" %s", device_xname(curdev));
283 
284 		if (!pmf_device_resume(curdev, qual)) {
285 			rv = false;
286 			aprint_debug("(failed)");
287 		}
288 	}
289 	deviter_release(&di);
290 	aprint_debug(".\n");
291 
292 	KERNEL_UNLOCK_ONE(0);
293 #if NWSDISPLAY > 0
294 	if (rv)
295 		wsdisplay_handlex(1);
296 #endif
297 	return rv;
298 }
299 
300 bool
301 pmf_system_suspend(const pmf_qual_t *qual)
302 {
303 	device_t curdev;
304 	deviter_t di;
305 
306 	if (!pmf_check_system_drivers())
307 		return false;
308 #if NWSDISPLAY > 0
309 	if (wsdisplay_handlex(0))
310 		return false;
311 #endif
312 	KERNEL_LOCK(1, NULL);
313 
314 	/*
315 	 * Flush buffers only if the shutdown didn't do so
316 	 * already and if there was no panic.
317 	 */
318 	if (doing_shutdown == 0 && panicstr == NULL) {
319 		printf("Flushing disk caches: ");
320 		do_sys_sync(&lwp0);
321 		if (vfs_syncwait() != 0)
322 			printf("giving up\n");
323 		else
324 			printf("done\n");
325 	}
326 
327 	aprint_debug("Suspending devices:");
328 
329 	for (curdev = deviter_first(&di, DEVITER_F_LEAVES_FIRST);
330 	     curdev != NULL;
331 	     curdev = deviter_next(&di)) {
332 		if (!device_is_active(curdev))
333 			continue;
334 
335 		aprint_debug(" %s", device_xname(curdev));
336 
337 		/* XXX joerg check return value and abort suspend */
338 		if (!pmf_device_suspend(curdev, qual))
339 			aprint_debug("(failed)");
340 	}
341 	deviter_release(&di);
342 
343 	aprint_debug(".\n");
344 
345 	return true;
346 }
347 
348 static bool
349 shutdown_all(int how)
350 {
351 	static struct shutdown_state s;
352 	device_t curdev;
353 	bool progress = false;
354 
355 	KERNEL_LOCK(1, NULL);
356 	for (curdev = shutdown_first(&s); curdev != NULL;
357 	     curdev = shutdown_next(&s)) {
358 		aprint_debug(" shutting down %s, ", device_xname(curdev));
359 		if (!device_pmf_is_registered(curdev))
360 			aprint_debug("skipped.");
361 #if 0 /* needed? */
362 		else if (!device_pmf_class_shutdown(curdev, how))
363 			aprint_debug("failed.");
364 #endif
365 		else if (!device_pmf_driver_shutdown(curdev, how))
366 			aprint_debug("failed.");
367 		else if (!device_pmf_bus_shutdown(curdev, how))
368 			aprint_debug("failed.");
369 		else {
370 			progress = true;
371 			aprint_debug("success.");
372 		}
373 	}
374 	KERNEL_UNLOCK_ONE(NULL);
375 	return progress;
376 }
377 
378 void
379 pmf_system_shutdown(int how)
380 {
381 
382 	if (panicstr != NULL)
383 		return;
384 
385 	aprint_debug("Shutting down devices:");
386 	shutdown_all(how);
387 }
388 
389 bool
390 pmf_set_platform(const char *key, const char *value)
391 {
392 	if (pmf_platform == NULL)
393 		pmf_platform = prop_dictionary_create();
394 	if (pmf_platform == NULL)
395 		return false;
396 
397 	return prop_dictionary_set_string(pmf_platform, key, value);
398 }
399 
400 const char *
401 pmf_get_platform(const char *key)
402 {
403 	const char *value;
404 
405 	if (pmf_platform == NULL)
406 		return NULL;
407 
408 	if (!prop_dictionary_get_string(pmf_platform, key, &value))
409 		return NULL;
410 
411 	return value;
412 }
413 
414 bool
415 pmf_device_register1(device_t dev,
416     bool (*suspend)(device_t, const pmf_qual_t *),
417     bool (*resume)(device_t, const pmf_qual_t *),
418     bool (*shutdown)(device_t, int))
419 {
420 	if (!device_pmf_driver_register(dev, suspend, resume, shutdown))
421 		return false;
422 
423 	if (!device_pmf_driver_child_register(dev)) {
424 		device_pmf_driver_deregister(dev);
425 		return false;
426 	}
427 
428 	return true;
429 }
430 
431 void
432 pmf_device_deregister(device_t dev)
433 {
434 	device_pmf_class_deregister(dev);
435 	device_pmf_bus_deregister(dev);
436 	device_pmf_driver_deregister(dev);
437 }
438 
439 static const device_suspensor_t _device_suspensor_drvctl = {
440 	  .ds_delegator = NULL
441 	, .ds_name = "drvctl"
442 };
443 
444 static const device_suspensor_t _device_suspensor_self = {
445 	  .ds_delegator = NULL
446 	, .ds_name = "self"
447 };
448 
449 #if 0
450 static const device_suspensor_t _device_suspensor_self_delegate = {
451 	  .ds_delegator = &_device_suspensor_self
452 	, .ds_name = "self delegate"
453 };
454 #endif
455 
456 static const device_suspensor_t _device_suspensor_system = {
457 	  .ds_delegator = NULL
458 	, .ds_name = "system"
459 };
460 
461 const device_suspensor_t
462     * const device_suspensor_self = &_device_suspensor_self,
463 #if 0
464     * const device_suspensor_self_delegate = &_device_suspensor_self_delegate,
465 #endif
466     * const device_suspensor_system = &_device_suspensor_system,
467     * const device_suspensor_drvctl = &_device_suspensor_drvctl;
468 
469 static const pmf_qual_t _pmf_qual_system = {
470 	  .pq_actlvl = DEVACT_LEVEL_FULL
471 	, .pq_suspensor = &_device_suspensor_system
472 };
473 
474 static const pmf_qual_t _pmf_qual_drvctl = {
475 	  .pq_actlvl = DEVACT_LEVEL_FULL
476 	, .pq_suspensor = &_device_suspensor_drvctl
477 };
478 
479 static const pmf_qual_t _pmf_qual_self = {
480 	  .pq_actlvl = DEVACT_LEVEL_DRIVER
481 	, .pq_suspensor = &_device_suspensor_self
482 };
483 
484 const pmf_qual_t
485     * const PMF_Q_DRVCTL = &_pmf_qual_drvctl,
486     * const PMF_Q_NONE = &_pmf_qual_system,
487     * const PMF_Q_SELF = &_pmf_qual_self;
488 
489 static bool
490 device_suspensor_delegates_to(const device_suspensor_t *ds,
491     const device_suspensor_t *delegate)
492 {
493 	const device_suspensor_t *iter;
494 
495 	for (iter = delegate->ds_delegator; iter != NULL;
496 	     iter = iter->ds_delegator) {
497 		if (ds == iter)
498 			return true;
499 	}
500 	return false;
501 }
502 
503 static bool
504 add_suspensor(device_t dev, const char *kind, const device_suspensor_t **susp,
505     const device_suspensor_t *ds)
506 {
507 	int i;
508 
509 	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
510 		if (susp[i] == NULL)
511 			continue;
512 		if (ds == susp[i]) {
513 			PMF_SUSPENSOR_PRINTF((
514 			    "%s: %s-suspended by %s (delegator %s) already\n",
515 			    device_xname(dev), kind,
516 			    susp[i]->ds_name,
517 			    (susp[i]->ds_delegator != NULL) ?
518 			    susp[i]->ds_delegator->ds_name : "<none>"));
519 			return true;
520 		}
521 		if (device_suspensor_delegates_to(ds, susp[i])) {
522 			PMF_SUSPENSOR_PRINTF((
523 			    "%s: %s assumes %s-suspension by %s "
524 			    "(delegator %s)\n",
525 			    device_xname(dev), ds->ds_name, kind,
526 			    susp[i]->ds_name,
527 			    (susp[i]->ds_delegator != NULL) ?
528 			    susp[i]->ds_delegator->ds_name : "<none>"));
529 			susp[i] = ds;
530 			return true;
531 		}
532 	}
533 	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
534 		if (susp[i] == NULL) {
535 			susp[i] = ds;
536 			PMF_SUSPENSOR_PRINTF((
537 			    "%s: newly %s-suspended by %s (delegator %s)\n",
538 			    device_xname(dev), kind,
539 			    susp[i]->ds_name,
540 			    (susp[i]->ds_delegator != NULL) ?
541 			    susp[i]->ds_delegator->ds_name : "<none>"));
542 			return true;
543 		}
544 	}
545 	return false;
546 }
547 
548 static bool
549 device_pmf_add_suspensor(device_t dev, const pmf_qual_t *pq)
550 {
551 	const device_suspensor_t *ds;
552 
553 	KASSERT(pq != NULL);
554 
555 	ds = pmf_qual_suspension(pq);
556 
557 	KASSERT(ds != NULL);
558 
559 	if (!add_suspensor(dev, "class", dev->dv_class_suspensors, ds))
560 		return false;
561 	if (!add_suspensor(dev, "driver", dev->dv_driver_suspensors, ds))
562 		return false;
563 	if (!add_suspensor(dev, "bus", dev->dv_bus_suspensors, ds))
564 		return false;
565 	return true;
566 }
567 
568 #if 0
569 static bool
570 device_pmf_has_suspension(device_t dev, const device_suspensor_t *ds)
571 {
572 	int i;
573 
574 	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
575 		if (dev->dv_suspensions[i] == ds)
576 			return true;
577 		if (device_suspensor_delegates_to(dev->dv_suspensions[i], ds))
578 			return true;
579 	}
580 	return false;
581 }
582 #endif
583 
584 static bool
585 any_suspensor(device_t dev, const char *kind, const device_suspensor_t **susp)
586 {
587 	int i;
588 	bool suspended = false;
589 
590 	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
591 		if (susp[i] != NULL) {
592 			PMF_SUSPENSOR_PRINTF(("%s: %s is suspended by %s "
593 			    "(delegator %s)\n",
594 			    device_xname(dev), kind,
595 			    susp[i]->ds_name,
596 			    (susp[i]->ds_delegator != NULL) ?
597 			    susp[i]->ds_delegator->ds_name : "<none>"));
598 			suspended = true;
599 		}
600 	}
601 	return suspended;
602 }
603 
604 static bool
605 device_pmf_any_suspensor(device_t dev, devact_level_t depth)
606 {
607 	switch (depth) {
608 	case DEVACT_LEVEL_FULL:
609 		if (any_suspensor(dev, "class", dev->dv_class_suspensors))
610 			return true;
611 		/*FALLTHROUGH*/
612 	case DEVACT_LEVEL_DRIVER:
613 		if (any_suspensor(dev, "driver", dev->dv_driver_suspensors))
614 			return true;
615 		/*FALLTHROUGH*/
616 	case DEVACT_LEVEL_BUS:
617 		if (any_suspensor(dev, "bus", dev->dv_bus_suspensors))
618 			return true;
619 	}
620 	return false;
621 }
622 
623 static bool
624 remove_suspensor(device_t dev, const char *kind,
625     const device_suspensor_t **susp, const device_suspensor_t *ds)
626 {
627 	int i;
628 
629 	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
630 		if (susp[i] == NULL)
631 			continue;
632 		if (ds == susp[i] ||
633 		    device_suspensor_delegates_to(ds, susp[i])) {
634 			PMF_SUSPENSOR_PRINTF(("%s: %s suspension %s "
635 			    "(delegator %s) removed by %s\n",
636 			    device_xname(dev), kind,
637 			    susp[i]->ds_name,
638 			    (susp[i]->ds_delegator != NULL)
639 			        ?  susp[i]->ds_delegator->ds_name
640 			        : "<none>",
641 			    ds->ds_name));
642 			susp[i] = NULL;
643 			return true;
644 		}
645 	}
646 	return false;
647 }
648 
649 static bool
650 device_pmf_remove_suspensor(device_t dev, const pmf_qual_t *pq)
651 {
652 	const device_suspensor_t *ds;
653 
654 	KASSERT(pq != NULL);
655 
656 	ds = pmf_qual_suspension(pq);
657 
658 	KASSERT(ds != NULL);
659 
660 	if (!remove_suspensor(dev, "class", dev->dv_class_suspensors, ds))
661 		return false;
662 	if (!remove_suspensor(dev, "driver", dev->dv_driver_suspensors, ds))
663 		return false;
664 	if (!remove_suspensor(dev, "bus", dev->dv_bus_suspensors, ds))
665 		return false;
666 
667 	return true;
668 }
669 
670 void
671 pmf_self_suspensor_init(device_t dev, device_suspensor_t *ds,
672     pmf_qual_t *pq)
673 {
674 	ds->ds_delegator = device_suspensor_self;
675 	snprintf(ds->ds_name, sizeof(ds->ds_name), "%s-self",
676 	    device_xname(dev));
677 	pq->pq_actlvl = DEVACT_LEVEL_DRIVER;
678 	pq->pq_suspensor = ds;
679 }
680 
681 bool
682 pmf_device_suspend(device_t dev, const pmf_qual_t *qual)
683 {
684 	bool rc;
685 
686 	PMF_TRANSITION_PRINTF(("%s: suspend enter\n", device_xname(dev)));
687 	if (!device_pmf_is_registered(dev))
688 		return false;
689 
690 	if (!device_pmf_lock(dev))
691 		return false;
692 
693 	rc = pmf_device_suspend_locked(dev, qual);
694 
695 	device_pmf_unlock(dev);
696 
697 	PMF_TRANSITION_PRINTF(("%s: suspend exit\n", device_xname(dev)));
698 	return rc;
699 }
700 
701 bool
702 pmf_device_suspend_locked(device_t dev, const pmf_qual_t *qual)
703 {
704 	if (!device_pmf_add_suspensor(dev, qual))
705 		return false;
706 
707 	PMF_TRANSITION_PRINTF2(1, ("%s: class suspend\n", device_xname(dev)));
708 	if (!device_pmf_class_suspend(dev, qual))
709 		return false;
710 
711 	PMF_TRANSITION_PRINTF2(1, ("%s: driver suspend\n", device_xname(dev)));
712 	if (!device_pmf_driver_suspend(dev, qual))
713 		return false;
714 
715 	PMF_TRANSITION_PRINTF2(1, ("%s: bus suspend\n", device_xname(dev)));
716 	if (!device_pmf_bus_suspend(dev, qual))
717 		return false;
718 
719 	return true;
720 }
721 
722 bool
723 pmf_device_resume(device_t dev, const pmf_qual_t *qual)
724 {
725 	bool rc;
726 
727 	PMF_TRANSITION_PRINTF(("%s: resume enter\n", device_xname(dev)));
728 	if (!device_pmf_is_registered(dev))
729 		return false;
730 
731 	if (!device_pmf_lock(dev))
732 		return false;
733 
734 	rc = pmf_device_resume_locked(dev, qual);
735 
736 	device_pmf_unlock(dev);
737 
738 	PMF_TRANSITION_PRINTF(("%s: resume exit\n", device_xname(dev)));
739 	return rc;
740 }
741 
742 bool
743 pmf_device_resume_locked(device_t dev, const pmf_qual_t *qual)
744 {
745 	device_pmf_remove_suspensor(dev, qual);
746 
747 	if (device_pmf_any_suspensor(dev, DEVACT_LEVEL_FULL))
748 		return true;
749 
750 	PMF_TRANSITION_PRINTF2(1, ("%s: bus resume\n", device_xname(dev)));
751 	if (!device_pmf_bus_resume(dev, qual))
752 		return false;
753 
754 	PMF_TRANSITION_PRINTF2(1, ("%s: driver resume\n", device_xname(dev)));
755 	if (!device_pmf_driver_resume(dev, qual))
756 		return false;
757 
758 	PMF_TRANSITION_PRINTF2(1, ("%s: class resume\n", device_xname(dev)));
759 	if (!device_pmf_class_resume(dev, qual))
760 		return false;
761 
762 	return true;
763 }
764 
765 bool
766 pmf_device_recursive_suspend(device_t dv, const pmf_qual_t *qual)
767 {
768 	bool rv = true;
769 	device_t curdev;
770 	deviter_t di;
771 	pmf_qual_t pq;
772 
773 	pmf_qual_recursive_copy(&pq, qual);
774 
775 	for (curdev = deviter_first(&di, 0); curdev != NULL;
776 	     curdev = deviter_next(&di)) {
777 		if (device_parent(curdev) != dv)
778 			continue;
779 		if (!pmf_device_recursive_suspend(curdev, &pq)) {
780 			rv = false;
781 			break;
782 		}
783 	}
784 	deviter_release(&di);
785 
786 	return rv && pmf_device_suspend(dv, qual);
787 }
788 
789 void
790 pmf_qual_recursive_copy(pmf_qual_t *dst, const pmf_qual_t *src)
791 {
792 	*dst = *src;
793 	dst->pq_actlvl = DEVACT_LEVEL_FULL;
794 }
795 
796 bool
797 pmf_device_recursive_resume(device_t dv, const pmf_qual_t *qual)
798 {
799 	device_t parent;
800 	pmf_qual_t pq;
801 
802 	if (device_is_active(dv))
803 		return true;
804 
805 	pmf_qual_recursive_copy(&pq, qual);
806 
807 	parent = device_parent(dv);
808 	if (parent != NULL) {
809 		if (!pmf_device_recursive_resume(parent, &pq))
810 			return false;
811 	}
812 
813 	return pmf_device_resume(dv, qual);
814 }
815 
816 bool
817 pmf_device_descendants_release(device_t dv, const pmf_qual_t *qual)
818 {
819 	bool rv = true;
820 	device_t curdev;
821 	deviter_t di;
822 
823 	for (curdev = deviter_first(&di, 0); curdev != NULL;
824 	     curdev = deviter_next(&di)) {
825 		if (device_parent(curdev) != dv)
826 			continue;
827 		device_pmf_remove_suspensor(curdev, qual);
828 		if (!pmf_device_descendants_release(curdev, qual)) {
829 			rv = false;
830 			break;
831 		}
832 	}
833 	deviter_release(&di);
834 	return rv;
835 }
836 
837 bool
838 pmf_device_descendants_resume(device_t dv, const pmf_qual_t *qual)
839 {
840 	bool rv = true;
841 	device_t curdev;
842 	deviter_t di;
843 
844 	KASSERT(pmf_qual_descend_ok(qual));
845 
846 	for (curdev = deviter_first(&di, 0); curdev != NULL;
847 	     curdev = deviter_next(&di)) {
848 		if (device_parent(curdev) != dv)
849 			continue;
850 		if (!pmf_device_resume(curdev, qual) ||
851 		    !pmf_device_descendants_resume(curdev, qual)) {
852 			rv = false;
853 			break;
854 		}
855 	}
856 	deviter_release(&di);
857 	return rv;
858 }
859 
860 bool
861 pmf_device_subtree_release(device_t dv, const pmf_qual_t *qual)
862 {
863 	pmf_qual_t pq;
864 
865 	device_pmf_remove_suspensor(dv, qual);
866 
867 	pmf_qual_recursive_copy(&pq, qual);
868 
869 	return pmf_device_descendants_release(dv, &pq);
870 }
871 
872 bool
873 pmf_device_subtree_resume(device_t dv, const pmf_qual_t *qual)
874 {
875 	pmf_qual_t pq;
876 
877 	if (!pmf_device_subtree_release(dv, qual))
878 		return false;
879 
880 	if (!pmf_device_recursive_resume(dv, qual))
881 		return false;
882 
883 	pmf_qual_recursive_copy(&pq, qual);
884 
885 	return pmf_device_descendants_resume(dv, &pq);
886 }
887 
888 #include <net/if.h>
889 
890 static bool
891 pmf_class_network_suspend(device_t dev, const pmf_qual_t *qual)
892 {
893 	struct ifnet *ifp = device_pmf_class_private(dev);
894 	int s;
895 
896 	s = splnet();
897 	IFNET_LOCK(ifp);
898 	(*ifp->if_stop)(ifp, 0);
899 	IFNET_UNLOCK(ifp);
900 	splx(s);
901 
902 	return true;
903 }
904 
905 static bool
906 pmf_class_network_resume(device_t dev, const pmf_qual_t *qual)
907 {
908 	struct ifnet *ifp = device_pmf_class_private(dev);
909 	int s;
910 	bool restart = false;
911 
912 	s = splnet();
913 	IFNET_LOCK(ifp);
914 	if (ifp->if_flags & IFF_UP) {
915 		ifp->if_flags &= ~IFF_RUNNING;
916 		if ((*ifp->if_init)(ifp) != 0)
917 			aprint_normal_ifnet(ifp, "resume failed\n");
918 		restart = true;
919 	}
920 	IFNET_UNLOCK(ifp);
921 
922 	if (restart)
923 		if_start_lock(ifp);
924 
925 	splx(s);
926 
927 	return true;
928 }
929 
930 void
931 pmf_class_network_register(device_t dev, struct ifnet *ifp)
932 {
933 	device_pmf_class_register(dev, ifp, pmf_class_network_suspend,
934 	    pmf_class_network_resume, NULL);
935 }
936 
937 bool
938 pmf_event_inject(device_t dv, pmf_generic_event_t ev)
939 {
940 	pmf_event_workitem_t *pew;
941 
942 	pew = pmf_event_workitem_get();
943 	if (pew == NULL) {
944 		PMF_EVENT_PRINTF(("%s: PMF event %d dropped (no memory)\n",
945 		    dv ? device_xname(dv) : "<anonymous>", ev));
946 		return false;
947 	}
948 
949 	pew->pew_event = ev;
950 	pew->pew_device = dv;
951 
952 	workqueue_enqueue(pmf_event_workqueue, &pew->pew_work, NULL);
953 	PMF_EVENT_PRINTF(("%s: PMF event %d injected\n",
954 	    dv ? device_xname(dv) : "<anonymous>", ev));
955 
956 	return true;
957 }
958 
959 bool
960 pmf_event_register(device_t dv, pmf_generic_event_t ev,
961     void (*handler)(device_t), bool global)
962 {
963 	pmf_event_handler_t *event;
964 
965 	event = kmem_alloc(sizeof(*event), KM_SLEEP);
966 	event->pmf_event = ev;
967 	event->pmf_handler = handler;
968 	event->pmf_device = dv;
969 	event->pmf_global = global;
970 	TAILQ_INSERT_TAIL(&pmf_all_events, event, pmf_link);
971 
972 	return true;
973 }
974 
975 void
976 pmf_event_deregister(device_t dv, pmf_generic_event_t ev,
977     void (*handler)(device_t), bool global)
978 {
979 	pmf_event_handler_t *event;
980 
981 	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
982 		if (event->pmf_event != ev)
983 			continue;
984 		if (event->pmf_device != dv)
985 			continue;
986 		if (event->pmf_global != global)
987 			continue;
988 		if (event->pmf_handler != handler)
989 			continue;
990 		TAILQ_REMOVE(&pmf_all_events, event, pmf_link);
991 		kmem_free(event, sizeof(*event));
992 		return;
993 	}
994 }
995 
996 struct display_class_softc {
997 	TAILQ_ENTRY(display_class_softc) dc_link;
998 	device_t dc_dev;
999 };
1000 
1001 static TAILQ_HEAD(, display_class_softc) all_displays;
1002 static callout_t global_idle_counter;
1003 static int idle_timeout = 30;
1004 
1005 static void
1006 input_idle(void *dummy)
1007 {
1008 	PMF_IDLE_PRINTF(("Input idle handler called\n"));
1009 	pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
1010 }
1011 
1012 static void
1013 input_activity_handler(device_t dv, devactive_t type)
1014 {
1015 	if (!TAILQ_EMPTY(&all_displays))
1016 		callout_schedule(&global_idle_counter, idle_timeout * hz);
1017 }
1018 
1019 static void
1020 pmf_class_input_deregister(device_t dv)
1021 {
1022 	device_active_deregister(dv, input_activity_handler);
1023 }
1024 
1025 bool
1026 pmf_class_input_register(device_t dv)
1027 {
1028 	if (!device_active_register(dv, input_activity_handler))
1029 		return false;
1030 
1031 	device_pmf_class_register(dv, NULL, NULL, NULL,
1032 	    pmf_class_input_deregister);
1033 
1034 	return true;
1035 }
1036 
1037 static void
1038 pmf_class_display_deregister(device_t dv)
1039 {
1040 	struct display_class_softc *sc = device_pmf_class_private(dv);
1041 	int s;
1042 
1043 	s = splsoftclock();
1044 	TAILQ_REMOVE(&all_displays, sc, dc_link);
1045 	if (TAILQ_EMPTY(&all_displays))
1046 		callout_stop(&global_idle_counter);
1047 	splx(s);
1048 
1049 	kmem_free(sc, sizeof(*sc));
1050 }
1051 
1052 bool
1053 pmf_class_display_register(device_t dv)
1054 {
1055 	struct display_class_softc *sc;
1056 	int s;
1057 
1058 	sc = kmem_alloc(sizeof(*sc), KM_SLEEP);
1059 
1060 	s = splsoftclock();
1061 	if (TAILQ_EMPTY(&all_displays))
1062 		callout_schedule(&global_idle_counter, idle_timeout * hz);
1063 
1064 	TAILQ_INSERT_HEAD(&all_displays, sc, dc_link);
1065 	splx(s);
1066 
1067 	device_pmf_class_register(dv, sc, NULL, NULL,
1068 	    pmf_class_display_deregister);
1069 
1070 	return true;
1071 }
1072 
1073 static void
1074 pmf_event_workitem_put(pmf_event_workitem_t *pew)
1075 {
1076 
1077 	KASSERT(pew != NULL);
1078 	pool_put(&pew_pl, pew);
1079 }
1080 
1081 static pmf_event_workitem_t *
1082 pmf_event_workitem_get(void)
1083 {
1084 
1085 	return pool_get(&pew_pl, PR_NOWAIT);
1086 }
1087 
1088 SYSCTL_SETUP(sysctl_pmf_setup, "PMF subtree setup")
1089 {
1090 	const struct sysctlnode *node = NULL;
1091 
1092 	sysctl_createv(clog, 0, NULL, &node,
1093 		CTLFLAG_PERMANENT,
1094 		CTLTYPE_NODE, "pmf",
1095 		SYSCTL_DESCR("pmf controls"),
1096 		NULL, 0, NULL, 0,
1097 		CTL_KERN, CTL_CREATE, CTL_EOL);
1098 
1099 #ifdef PMF_DEBUG
1100 	sysctl_createv(clog, 0, &node, &node,
1101 		CTLFLAG_PERMANENT,
1102 		CTLTYPE_NODE, "debug",
1103 		SYSCTL_DESCR("debug levels"),
1104 		NULL, 0, NULL, 0,
1105 		CTL_CREATE, CTL_EOL);
1106 
1107 	sysctl_createv(clog, 0, &node, NULL,
1108 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1109 		CTLTYPE_INT, "event",
1110 		SYSCTL_DESCR("event"),
1111 		NULL, 0,  &pmf_debug_event, sizeof(pmf_debug_event),
1112 		CTL_CREATE, CTL_EOL);
1113 	sysctl_createv(clog, 0, &node, NULL,
1114 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1115 		CTLTYPE_INT, "suspend",
1116 		SYSCTL_DESCR("suspend"),
1117 		NULL, 0,  &pmf_debug_suspend, sizeof(pmf_debug_suspend),
1118 		CTL_CREATE, CTL_EOL);
1119 	sysctl_createv(clog, 0, &node, NULL,
1120 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1121 		CTLTYPE_INT, "suspensor",
1122 		SYSCTL_DESCR("suspensor"),
1123 		NULL, 0,  &pmf_debug_suspensor, sizeof(pmf_debug_suspensor),
1124 		CTL_CREATE, CTL_EOL);
1125 	sysctl_createv(clog, 0, &node, NULL,
1126 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1127 		CTLTYPE_INT, "idle",
1128 		SYSCTL_DESCR("idle"),
1129 		NULL, 0,  &pmf_debug_idle, sizeof(pmf_debug_idle),
1130 		CTL_CREATE, CTL_EOL);
1131 	sysctl_createv(clog, 0, &node, NULL,
1132 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1133 		CTLTYPE_INT, "transition",
1134 		SYSCTL_DESCR("event"),
1135 		NULL, 0,  &pmf_debug_transition, sizeof(pmf_debug_transition),
1136 		CTL_CREATE, CTL_EOL);
1137 #endif
1138 }
1139 
1140 
1141 void
1142 pmf_init(void)
1143 {
1144 	int err;
1145 
1146 	pool_init(&pew_pl, sizeof(pmf_event_workitem_t), 0, 0, 0,
1147 	    "pewpl", NULL, IPL_HIGH);
1148 	pool_setlowat(&pew_pl, 1);
1149 	pool_sethiwat(&pew_pl, 8);
1150 
1151 	KASSERT(pmf_event_workqueue == NULL);
1152 	err = workqueue_create(&pmf_event_workqueue, "pmfevent",
1153 	    pmf_event_worker, NULL, PRI_NONE, IPL_VM, 0);
1154 	if (err)
1155 		panic("couldn't create pmfevent workqueue");
1156 
1157 	KASSERT(pmf_suspend_workqueue == NULL);
1158 	err = workqueue_create(&pmf_suspend_workqueue, "pmfsuspend",
1159 	    pmf_suspend_worker, NULL, PRI_NONE, IPL_VM, 0);
1160 	if (err)
1161 		panic("couldn't create pmfsuspend workqueue");
1162 
1163 
1164 	callout_init(&global_idle_counter, 0);
1165 	callout_setfunc(&global_idle_counter, input_idle, NULL);
1166 }
1167