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