kern_pmf.c revision 1.49
1/* $NetBSD: kern_pmf.c,v 1.49 2022/08/24 11:18:56 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.49 2022/08/24 11:18:56 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
62int  pmf_debug_event;
63int  pmf_debug_suspend;
64int  pmf_debug_suspensor;
65int  pmf_debug_idle;
66int  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
83static prop_dictionary_t pmf_platform = NULL;
84static struct workqueue *pmf_event_workqueue;
85static struct workqueue *pmf_suspend_workqueue;
86
87typedef 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
95static TAILQ_HEAD(, pmf_event_handler) pmf_all_events =
96    TAILQ_HEAD_INITIALIZER(pmf_all_events);
97
98typedef 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
104typedef 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
110static struct pool pew_pl;
111
112static pmf_event_workitem_t *pmf_event_workitem_get(void);
113static void pmf_event_workitem_put(pmf_event_workitem_t *);
114
115bool pmf_device_resume_locked(device_t, const pmf_qual_t *);
116bool pmf_device_suspend_locked(device_t, const pmf_qual_t *);
117static bool device_pmf_any_suspensor(device_t, devact_level_t);
118
119static bool
120complete_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
142static void
143pmf_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
183static void
184pmf_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
203static bool
204pmf_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
228bool
229pmf_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
259bool
260pmf_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
300bool
301pmf_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
348static bool
349shutdown_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
378void
379pmf_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
389bool
390pmf_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
400const char *
401pmf_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
414bool
415pmf_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
421	device_pmf_driver_register(dev, suspend, resume, shutdown);
422	if (!device_pmf_driver_child_register(dev)) {
423		device_pmf_driver_deregister(dev);
424		return false;
425	}
426
427	return true;
428}
429
430void
431pmf_device_deregister(device_t dev)
432{
433	device_pmf_class_deregister(dev);
434	device_pmf_bus_deregister(dev);
435	device_pmf_driver_deregister(dev);
436}
437
438static const device_suspensor_t _device_suspensor_drvctl = {
439	  .ds_delegator = NULL
440	, .ds_name = "drvctl"
441};
442
443static const device_suspensor_t _device_suspensor_self = {
444	  .ds_delegator = NULL
445	, .ds_name = "self"
446};
447
448#if 0
449static const device_suspensor_t _device_suspensor_self_delegate = {
450	  .ds_delegator = &_device_suspensor_self
451	, .ds_name = "self delegate"
452};
453#endif
454
455static const device_suspensor_t _device_suspensor_system = {
456	  .ds_delegator = NULL
457	, .ds_name = "system"
458};
459
460const device_suspensor_t
461    * const device_suspensor_self = &_device_suspensor_self,
462#if 0
463    * const device_suspensor_self_delegate = &_device_suspensor_self_delegate,
464#endif
465    * const device_suspensor_system = &_device_suspensor_system,
466    * const device_suspensor_drvctl = &_device_suspensor_drvctl;
467
468static const pmf_qual_t _pmf_qual_system = {
469	  .pq_actlvl = DEVACT_LEVEL_FULL
470	, .pq_suspensor = &_device_suspensor_system
471};
472
473static const pmf_qual_t _pmf_qual_drvctl = {
474	  .pq_actlvl = DEVACT_LEVEL_FULL
475	, .pq_suspensor = &_device_suspensor_drvctl
476};
477
478static const pmf_qual_t _pmf_qual_self = {
479	  .pq_actlvl = DEVACT_LEVEL_DRIVER
480	, .pq_suspensor = &_device_suspensor_self
481};
482
483const pmf_qual_t
484    * const PMF_Q_DRVCTL = &_pmf_qual_drvctl,
485    * const PMF_Q_NONE = &_pmf_qual_system,
486    * const PMF_Q_SELF = &_pmf_qual_self;
487
488static bool
489device_suspensor_delegates_to(const device_suspensor_t *ds,
490    const device_suspensor_t *delegate)
491{
492	const device_suspensor_t *iter;
493
494	for (iter = delegate->ds_delegator; iter != NULL;
495	     iter = iter->ds_delegator) {
496		if (ds == iter)
497			return true;
498	}
499	return false;
500}
501
502static bool
503add_suspensor(device_t dev, const char *kind, const device_suspensor_t **susp,
504    const device_suspensor_t *ds)
505{
506	int i;
507
508	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
509		if (susp[i] == NULL)
510			continue;
511		if (ds == susp[i]) {
512			PMF_SUSPENSOR_PRINTF((
513			    "%s: %s-suspended by %s (delegator %s) already\n",
514			    device_xname(dev), kind,
515			    susp[i]->ds_name,
516			    (susp[i]->ds_delegator != NULL) ?
517			    susp[i]->ds_delegator->ds_name : "<none>"));
518			return true;
519		}
520		if (device_suspensor_delegates_to(ds, susp[i])) {
521			PMF_SUSPENSOR_PRINTF((
522			    "%s: %s assumes %s-suspension by %s "
523			    "(delegator %s)\n",
524			    device_xname(dev), ds->ds_name, kind,
525			    susp[i]->ds_name,
526			    (susp[i]->ds_delegator != NULL) ?
527			    susp[i]->ds_delegator->ds_name : "<none>"));
528			susp[i] = ds;
529			return true;
530		}
531	}
532	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
533		if (susp[i] == NULL) {
534			susp[i] = ds;
535			PMF_SUSPENSOR_PRINTF((
536			    "%s: newly %s-suspended by %s (delegator %s)\n",
537			    device_xname(dev), kind,
538			    susp[i]->ds_name,
539			    (susp[i]->ds_delegator != NULL) ?
540			    susp[i]->ds_delegator->ds_name : "<none>"));
541			return true;
542		}
543	}
544	return false;
545}
546
547static bool
548device_pmf_add_suspensor(device_t dev, const pmf_qual_t *pq)
549{
550	const device_suspensor_t *ds;
551
552	KASSERT(pq != NULL);
553
554	ds = pmf_qual_suspension(pq);
555
556	KASSERT(ds != NULL);
557
558	if (!add_suspensor(dev, "class", dev->dv_class_suspensors, ds))
559		return false;
560	if (!add_suspensor(dev, "driver", dev->dv_driver_suspensors, ds))
561		return false;
562	if (!add_suspensor(dev, "bus", dev->dv_bus_suspensors, ds))
563		return false;
564	return true;
565}
566
567#if 0
568static bool
569device_pmf_has_suspension(device_t dev, const device_suspensor_t *ds)
570{
571	int i;
572
573	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
574		if (dev->dv_suspensions[i] == ds)
575			return true;
576		if (device_suspensor_delegates_to(dev->dv_suspensions[i], ds))
577			return true;
578	}
579	return false;
580}
581#endif
582
583static bool
584any_suspensor(device_t dev, const char *kind, const device_suspensor_t **susp)
585{
586	int i;
587	bool suspended = false;
588
589	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
590		if (susp[i] != NULL) {
591			PMF_SUSPENSOR_PRINTF(("%s: %s is suspended by %s "
592			    "(delegator %s)\n",
593			    device_xname(dev), kind,
594			    susp[i]->ds_name,
595			    (susp[i]->ds_delegator != NULL) ?
596			    susp[i]->ds_delegator->ds_name : "<none>"));
597			suspended = true;
598		}
599	}
600	return suspended;
601}
602
603static bool
604device_pmf_any_suspensor(device_t dev, devact_level_t depth)
605{
606	switch (depth) {
607	case DEVACT_LEVEL_FULL:
608		if (any_suspensor(dev, "class", dev->dv_class_suspensors))
609			return true;
610		/*FALLTHROUGH*/
611	case DEVACT_LEVEL_DRIVER:
612		if (any_suspensor(dev, "driver", dev->dv_driver_suspensors))
613			return true;
614		/*FALLTHROUGH*/
615	case DEVACT_LEVEL_BUS:
616		if (any_suspensor(dev, "bus", dev->dv_bus_suspensors))
617			return true;
618	}
619	return false;
620}
621
622static bool
623remove_suspensor(device_t dev, const char *kind,
624    const device_suspensor_t **susp, const device_suspensor_t *ds)
625{
626	int i;
627
628	for (i = 0; i < DEVICE_SUSPENSORS_MAX; i++) {
629		if (susp[i] == NULL)
630			continue;
631		if (ds == susp[i] ||
632		    device_suspensor_delegates_to(ds, susp[i])) {
633			PMF_SUSPENSOR_PRINTF(("%s: %s suspension %s "
634			    "(delegator %s) removed by %s\n",
635			    device_xname(dev), kind,
636			    susp[i]->ds_name,
637			    (susp[i]->ds_delegator != NULL)
638			        ?  susp[i]->ds_delegator->ds_name
639			        : "<none>",
640			    ds->ds_name));
641			susp[i] = NULL;
642			return true;
643		}
644	}
645	return false;
646}
647
648static bool
649device_pmf_remove_suspensor(device_t dev, const pmf_qual_t *pq)
650{
651	const device_suspensor_t *ds;
652
653	KASSERT(pq != NULL);
654
655	ds = pmf_qual_suspension(pq);
656
657	KASSERT(ds != NULL);
658
659	if (!remove_suspensor(dev, "class", dev->dv_class_suspensors, ds))
660		return false;
661	if (!remove_suspensor(dev, "driver", dev->dv_driver_suspensors, ds))
662		return false;
663	if (!remove_suspensor(dev, "bus", dev->dv_bus_suspensors, ds))
664		return false;
665
666	return true;
667}
668
669void
670pmf_self_suspensor_init(device_t dev, device_suspensor_t *ds,
671    pmf_qual_t *pq)
672{
673	ds->ds_delegator = device_suspensor_self;
674	snprintf(ds->ds_name, sizeof(ds->ds_name), "%s-self",
675	    device_xname(dev));
676	pq->pq_actlvl = DEVACT_LEVEL_DRIVER;
677	pq->pq_suspensor = ds;
678}
679
680bool
681pmf_device_suspend(device_t dev, const pmf_qual_t *qual)
682{
683	bool rc;
684
685	PMF_TRANSITION_PRINTF(("%s: suspend enter\n", device_xname(dev)));
686	if (!device_pmf_is_registered(dev))
687		return false;
688
689	if (!device_pmf_lock(dev))
690		return false;
691
692	rc = pmf_device_suspend_locked(dev, qual);
693
694	device_pmf_unlock(dev);
695
696	PMF_TRANSITION_PRINTF(("%s: suspend exit\n", device_xname(dev)));
697	return rc;
698}
699
700bool
701pmf_device_suspend_locked(device_t dev, const pmf_qual_t *qual)
702{
703	if (!device_pmf_add_suspensor(dev, qual))
704		return false;
705
706	PMF_TRANSITION_PRINTF2(1, ("%s: class suspend\n", device_xname(dev)));
707	if (!device_pmf_class_suspend(dev, qual))
708		return false;
709
710	PMF_TRANSITION_PRINTF2(1, ("%s: driver suspend\n", device_xname(dev)));
711	if (!device_pmf_driver_suspend(dev, qual))
712		return false;
713
714	PMF_TRANSITION_PRINTF2(1, ("%s: bus suspend\n", device_xname(dev)));
715	if (!device_pmf_bus_suspend(dev, qual))
716		return false;
717
718	return true;
719}
720
721bool
722pmf_device_resume(device_t dev, const pmf_qual_t *qual)
723{
724	bool rc;
725
726	PMF_TRANSITION_PRINTF(("%s: resume enter\n", device_xname(dev)));
727	if (!device_pmf_is_registered(dev))
728		return false;
729
730	if (!device_pmf_lock(dev))
731		return false;
732
733	rc = pmf_device_resume_locked(dev, qual);
734
735	device_pmf_unlock(dev);
736
737	PMF_TRANSITION_PRINTF(("%s: resume exit\n", device_xname(dev)));
738	return rc;
739}
740
741bool
742pmf_device_resume_locked(device_t dev, const pmf_qual_t *qual)
743{
744	device_pmf_remove_suspensor(dev, qual);
745
746	if (device_pmf_any_suspensor(dev, DEVACT_LEVEL_FULL))
747		return true;
748
749	PMF_TRANSITION_PRINTF2(1, ("%s: bus resume\n", device_xname(dev)));
750	if (!device_pmf_bus_resume(dev, qual))
751		return false;
752
753	PMF_TRANSITION_PRINTF2(1, ("%s: driver resume\n", device_xname(dev)));
754	if (!device_pmf_driver_resume(dev, qual))
755		return false;
756
757	PMF_TRANSITION_PRINTF2(1, ("%s: class resume\n", device_xname(dev)));
758	if (!device_pmf_class_resume(dev, qual))
759		return false;
760
761	return true;
762}
763
764bool
765pmf_device_recursive_suspend(device_t dv, const pmf_qual_t *qual)
766{
767	bool rv = true;
768	device_t curdev;
769	deviter_t di;
770	pmf_qual_t pq;
771
772	pmf_qual_recursive_copy(&pq, qual);
773
774	for (curdev = deviter_first(&di, 0); curdev != NULL;
775	     curdev = deviter_next(&di)) {
776		if (device_parent(curdev) != dv)
777			continue;
778		if (!pmf_device_recursive_suspend(curdev, &pq)) {
779			rv = false;
780			break;
781		}
782	}
783	deviter_release(&di);
784
785	return rv && pmf_device_suspend(dv, qual);
786}
787
788void
789pmf_qual_recursive_copy(pmf_qual_t *dst, const pmf_qual_t *src)
790{
791	*dst = *src;
792	dst->pq_actlvl = DEVACT_LEVEL_FULL;
793}
794
795bool
796pmf_device_recursive_resume(device_t dv, const pmf_qual_t *qual)
797{
798	device_t parent;
799	pmf_qual_t pq;
800
801	if (device_is_active(dv))
802		return true;
803
804	pmf_qual_recursive_copy(&pq, qual);
805
806	parent = device_parent(dv);
807	if (parent != NULL) {
808		if (!pmf_device_recursive_resume(parent, &pq))
809			return false;
810	}
811
812	return pmf_device_resume(dv, qual);
813}
814
815bool
816pmf_device_descendants_release(device_t dv, const pmf_qual_t *qual)
817{
818	bool rv = true;
819	device_t curdev;
820	deviter_t di;
821
822	for (curdev = deviter_first(&di, 0); curdev != NULL;
823	     curdev = deviter_next(&di)) {
824		if (device_parent(curdev) != dv)
825			continue;
826		device_pmf_remove_suspensor(curdev, qual);
827		if (!pmf_device_descendants_release(curdev, qual)) {
828			rv = false;
829			break;
830		}
831	}
832	deviter_release(&di);
833	return rv;
834}
835
836bool
837pmf_device_descendants_resume(device_t dv, const pmf_qual_t *qual)
838{
839	bool rv = true;
840	device_t curdev;
841	deviter_t di;
842
843	KASSERT(pmf_qual_descend_ok(qual));
844
845	for (curdev = deviter_first(&di, 0); curdev != NULL;
846	     curdev = deviter_next(&di)) {
847		if (device_parent(curdev) != dv)
848			continue;
849		if (!pmf_device_resume(curdev, qual) ||
850		    !pmf_device_descendants_resume(curdev, qual)) {
851			rv = false;
852			break;
853		}
854	}
855	deviter_release(&di);
856	return rv;
857}
858
859bool
860pmf_device_subtree_release(device_t dv, const pmf_qual_t *qual)
861{
862	pmf_qual_t pq;
863
864	device_pmf_remove_suspensor(dv, qual);
865
866	pmf_qual_recursive_copy(&pq, qual);
867
868	return pmf_device_descendants_release(dv, &pq);
869}
870
871bool
872pmf_device_subtree_resume(device_t dv, const pmf_qual_t *qual)
873{
874	pmf_qual_t pq;
875
876	if (!pmf_device_subtree_release(dv, qual))
877		return false;
878
879	if (!pmf_device_recursive_resume(dv, qual))
880		return false;
881
882	pmf_qual_recursive_copy(&pq, qual);
883
884	return pmf_device_descendants_resume(dv, &pq);
885}
886
887#include <net/if.h>
888
889static bool
890pmf_class_network_suspend(device_t dev, const pmf_qual_t *qual)
891{
892	struct ifnet *ifp = device_pmf_class_private(dev);
893	int s;
894
895	s = splnet();
896	IFNET_LOCK(ifp);
897	(*ifp->if_stop)(ifp, 0);
898	IFNET_UNLOCK(ifp);
899	splx(s);
900
901	return true;
902}
903
904static bool
905pmf_class_network_resume(device_t dev, const pmf_qual_t *qual)
906{
907	struct ifnet *ifp = device_pmf_class_private(dev);
908	int s;
909	bool restart = false;
910
911	s = splnet();
912	IFNET_LOCK(ifp);
913	if (ifp->if_flags & IFF_UP) {
914		ifp->if_flags &= ~IFF_RUNNING;
915		if ((*ifp->if_init)(ifp) != 0)
916			aprint_normal_ifnet(ifp, "resume failed\n");
917		restart = true;
918	}
919	IFNET_UNLOCK(ifp);
920
921	if (restart)
922		if_start_lock(ifp);
923
924	splx(s);
925
926	return true;
927}
928
929void
930pmf_class_network_register(device_t dev, struct ifnet *ifp)
931{
932	device_pmf_class_register(dev, ifp, pmf_class_network_suspend,
933	    pmf_class_network_resume, NULL);
934}
935
936bool
937pmf_event_inject(device_t dv, pmf_generic_event_t ev)
938{
939	pmf_event_workitem_t *pew;
940
941	pew = pmf_event_workitem_get();
942	if (pew == NULL) {
943		PMF_EVENT_PRINTF(("%s: PMF event %d dropped (no memory)\n",
944		    dv ? device_xname(dv) : "<anonymous>", ev));
945		return false;
946	}
947
948	pew->pew_event = ev;
949	pew->pew_device = dv;
950
951	workqueue_enqueue(pmf_event_workqueue, &pew->pew_work, NULL);
952	PMF_EVENT_PRINTF(("%s: PMF event %d injected\n",
953	    dv ? device_xname(dv) : "<anonymous>", ev));
954
955	return true;
956}
957
958bool
959pmf_event_register(device_t dv, pmf_generic_event_t ev,
960    void (*handler)(device_t), bool global)
961{
962	pmf_event_handler_t *event;
963
964	event = kmem_alloc(sizeof(*event), KM_SLEEP);
965	event->pmf_event = ev;
966	event->pmf_handler = handler;
967	event->pmf_device = dv;
968	event->pmf_global = global;
969	TAILQ_INSERT_TAIL(&pmf_all_events, event, pmf_link);
970
971	return true;
972}
973
974void
975pmf_event_deregister(device_t dv, pmf_generic_event_t ev,
976    void (*handler)(device_t), bool global)
977{
978	pmf_event_handler_t *event;
979
980	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
981		if (event->pmf_event != ev)
982			continue;
983		if (event->pmf_device != dv)
984			continue;
985		if (event->pmf_global != global)
986			continue;
987		if (event->pmf_handler != handler)
988			continue;
989		TAILQ_REMOVE(&pmf_all_events, event, pmf_link);
990		kmem_free(event, sizeof(*event));
991		return;
992	}
993}
994
995struct display_class_softc {
996	TAILQ_ENTRY(display_class_softc) dc_link;
997	device_t dc_dev;
998};
999
1000static TAILQ_HEAD(, display_class_softc) all_displays;
1001static callout_t global_idle_counter;
1002static int idle_timeout = 30;
1003
1004static void
1005input_idle(void *dummy)
1006{
1007	PMF_IDLE_PRINTF(("Input idle handler called\n"));
1008	pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
1009}
1010
1011static void
1012input_activity_handler(device_t dv, devactive_t type)
1013{
1014	if (!TAILQ_EMPTY(&all_displays))
1015		callout_schedule(&global_idle_counter, idle_timeout * hz);
1016}
1017
1018static void
1019pmf_class_input_deregister(device_t dv)
1020{
1021	device_active_deregister(dv, input_activity_handler);
1022}
1023
1024bool
1025pmf_class_input_register(device_t dv)
1026{
1027	if (!device_active_register(dv, input_activity_handler))
1028		return false;
1029
1030	device_pmf_class_register(dv, NULL, NULL, NULL,
1031	    pmf_class_input_deregister);
1032
1033	return true;
1034}
1035
1036static void
1037pmf_class_display_deregister(device_t dv)
1038{
1039	struct display_class_softc *sc = device_pmf_class_private(dv);
1040	int s;
1041
1042	s = splsoftclock();
1043	TAILQ_REMOVE(&all_displays, sc, dc_link);
1044	if (TAILQ_EMPTY(&all_displays))
1045		callout_stop(&global_idle_counter);
1046	splx(s);
1047
1048	kmem_free(sc, sizeof(*sc));
1049}
1050
1051bool
1052pmf_class_display_register(device_t dv)
1053{
1054	struct display_class_softc *sc;
1055	int s;
1056
1057	sc = kmem_alloc(sizeof(*sc), KM_SLEEP);
1058
1059	s = splsoftclock();
1060	if (TAILQ_EMPTY(&all_displays))
1061		callout_schedule(&global_idle_counter, idle_timeout * hz);
1062
1063	TAILQ_INSERT_HEAD(&all_displays, sc, dc_link);
1064	splx(s);
1065
1066	device_pmf_class_register(dv, sc, NULL, NULL,
1067	    pmf_class_display_deregister);
1068
1069	return true;
1070}
1071
1072static void
1073pmf_event_workitem_put(pmf_event_workitem_t *pew)
1074{
1075
1076	KASSERT(pew != NULL);
1077	pool_put(&pew_pl, pew);
1078}
1079
1080static pmf_event_workitem_t *
1081pmf_event_workitem_get(void)
1082{
1083
1084	return pool_get(&pew_pl, PR_NOWAIT);
1085}
1086
1087SYSCTL_SETUP(sysctl_pmf_setup, "PMF subtree setup")
1088{
1089	const struct sysctlnode *node = NULL;
1090
1091	sysctl_createv(clog, 0, NULL, &node,
1092		CTLFLAG_PERMANENT,
1093		CTLTYPE_NODE, "pmf",
1094		SYSCTL_DESCR("pmf controls"),
1095		NULL, 0, NULL, 0,
1096		CTL_KERN, CTL_CREATE, CTL_EOL);
1097
1098#ifdef PMF_DEBUG
1099	sysctl_createv(clog, 0, &node, &node,
1100		CTLFLAG_PERMANENT,
1101		CTLTYPE_NODE, "debug",
1102		SYSCTL_DESCR("debug levels"),
1103		NULL, 0, NULL, 0,
1104		CTL_CREATE, CTL_EOL);
1105
1106	sysctl_createv(clog, 0, &node, NULL,
1107		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1108		CTLTYPE_INT, "event",
1109		SYSCTL_DESCR("event"),
1110		NULL, 0,  &pmf_debug_event, sizeof(pmf_debug_event),
1111		CTL_CREATE, CTL_EOL);
1112	sysctl_createv(clog, 0, &node, NULL,
1113		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1114		CTLTYPE_INT, "suspend",
1115		SYSCTL_DESCR("suspend"),
1116		NULL, 0,  &pmf_debug_suspend, sizeof(pmf_debug_suspend),
1117		CTL_CREATE, CTL_EOL);
1118	sysctl_createv(clog, 0, &node, NULL,
1119		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1120		CTLTYPE_INT, "suspensor",
1121		SYSCTL_DESCR("suspensor"),
1122		NULL, 0,  &pmf_debug_suspensor, sizeof(pmf_debug_suspensor),
1123		CTL_CREATE, CTL_EOL);
1124	sysctl_createv(clog, 0, &node, NULL,
1125		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1126		CTLTYPE_INT, "idle",
1127		SYSCTL_DESCR("idle"),
1128		NULL, 0,  &pmf_debug_idle, sizeof(pmf_debug_idle),
1129		CTL_CREATE, CTL_EOL);
1130	sysctl_createv(clog, 0, &node, NULL,
1131		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1132		CTLTYPE_INT, "transition",
1133		SYSCTL_DESCR("event"),
1134		NULL, 0,  &pmf_debug_transition, sizeof(pmf_debug_transition),
1135		CTL_CREATE, CTL_EOL);
1136#endif
1137}
1138
1139
1140void
1141pmf_init(void)
1142{
1143	int err;
1144
1145	pool_init(&pew_pl, sizeof(pmf_event_workitem_t), 0, 0, 0,
1146	    "pewpl", NULL, IPL_HIGH);
1147	pool_setlowat(&pew_pl, 1);
1148	pool_sethiwat(&pew_pl, 8);
1149
1150	KASSERT(pmf_event_workqueue == NULL);
1151	err = workqueue_create(&pmf_event_workqueue, "pmfevent",
1152	    pmf_event_worker, NULL, PRI_NONE, IPL_VM, 0);
1153	if (err)
1154		panic("couldn't create pmfevent workqueue");
1155
1156	KASSERT(pmf_suspend_workqueue == NULL);
1157	err = workqueue_create(&pmf_suspend_workqueue, "pmfsuspend",
1158	    pmf_suspend_worker, NULL, PRI_NONE, IPL_VM, 0);
1159	if (err)
1160		panic("couldn't create pmfsuspend workqueue");
1161
1162
1163	callout_init(&global_idle_counter, 0);
1164	callout_setfunc(&global_idle_counter, input_idle, NULL);
1165}
1166