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