Home | History | Annotate | Line # | Download | only in kern
kern_pmf.c revision 1.12.6.1
      1  1.12.6.1       mjf /* $NetBSD: kern_pmf.c,v 1.12.6.1 2008/04/03 12:43:02 mjf 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  * 3. All advertising materials mentioning features or use of this software
     16       1.2  jmcneill  *    must display the following acknowledgement:
     17       1.2  jmcneill  *        This product includes software developed by Jared D. McNeill.
     18       1.2  jmcneill  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19       1.2  jmcneill  *    contributors may be used to endorse or promote products derived
     20       1.2  jmcneill  *    from this software without specific prior written permission.
     21       1.2  jmcneill  *
     22       1.2  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23       1.2  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24       1.2  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25       1.2  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26       1.2  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27       1.2  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28       1.2  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29       1.2  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30       1.2  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31       1.2  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32       1.2  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     33       1.2  jmcneill  */
     34       1.2  jmcneill 
     35       1.2  jmcneill #include <sys/cdefs.h>
     36  1.12.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.12.6.1 2008/04/03 12:43:02 mjf Exp $");
     37       1.2  jmcneill 
     38       1.2  jmcneill #include <sys/types.h>
     39       1.2  jmcneill #include <sys/param.h>
     40       1.2  jmcneill #include <sys/malloc.h>
     41       1.2  jmcneill #include <sys/buf.h>
     42       1.2  jmcneill #include <sys/callout.h>
     43       1.2  jmcneill #include <sys/kernel.h>
     44       1.2  jmcneill #include <sys/device.h>
     45       1.2  jmcneill #include <sys/pmf.h>
     46       1.2  jmcneill #include <sys/queue.h>
     47       1.2  jmcneill #include <sys/syscallargs.h> /* for sys_sync */
     48       1.2  jmcneill #include <sys/workqueue.h>
     49       1.2  jmcneill #include <prop/proplib.h>
     50  1.12.6.1       mjf #include <sys/condvar.h>
     51  1.12.6.1       mjf #include <sys/mutex.h>
     52  1.12.6.1       mjf #include <sys/proc.h>
     53  1.12.6.1       mjf #include <sys/reboot.h>	/* for RB_NOSYNC */
     54  1.12.6.1       mjf #include <sys/sched.h>
     55       1.2  jmcneill 
     56      1.12  drochner /* XXX ugly special case, but for now the only client */
     57      1.12  drochner #include "wsdisplay.h"
     58      1.12  drochner #if NWSDISPLAY > 0
     59      1.12  drochner #include <dev/wscons/wsdisplayvar.h>
     60      1.12  drochner #endif
     61      1.12  drochner 
     62       1.2  jmcneill #ifdef PMF_DEBUG
     63       1.2  jmcneill int pmf_debug_event;
     64       1.2  jmcneill int pmf_debug_idle;
     65       1.2  jmcneill int pmf_debug_transition;
     66       1.2  jmcneill 
     67       1.2  jmcneill #define	PMF_EVENT_PRINTF(x)		if (pmf_debug_event) printf x
     68       1.2  jmcneill #define	PMF_IDLE_PRINTF(x)		if (pmf_debug_idle) printf x
     69       1.2  jmcneill #define	PMF_TRANSITION_PRINTF(x)	if (pmf_debug_transition) printf x
     70       1.2  jmcneill #define	PMF_TRANSITION_PRINTF2(y,x)	if (pmf_debug_transition>y) printf x
     71       1.2  jmcneill #else
     72       1.2  jmcneill #define	PMF_EVENT_PRINTF(x)		do { } while (0)
     73       1.2  jmcneill #define	PMF_IDLE_PRINTF(x)		do { } while (0)
     74       1.2  jmcneill #define	PMF_TRANSITION_PRINTF(x)	do { } while (0)
     75       1.2  jmcneill #define	PMF_TRANSITION_PRINTF2(y,x)	do { } while (0)
     76       1.2  jmcneill #endif
     77       1.2  jmcneill 
     78       1.2  jmcneill /* #define PMF_DEBUG */
     79       1.2  jmcneill 
     80       1.2  jmcneill MALLOC_DEFINE(M_PMF, "pmf", "device pmf messaging memory");
     81       1.2  jmcneill 
     82       1.2  jmcneill static prop_dictionary_t pmf_platform = NULL;
     83       1.2  jmcneill static struct workqueue *pmf_event_workqueue;
     84       1.2  jmcneill 
     85       1.2  jmcneill typedef struct pmf_event_handler {
     86       1.2  jmcneill 	TAILQ_ENTRY(pmf_event_handler) pmf_link;
     87       1.2  jmcneill 	pmf_generic_event_t pmf_event;
     88       1.2  jmcneill 	void (*pmf_handler)(device_t);
     89       1.2  jmcneill 	device_t pmf_device;
     90       1.2  jmcneill 	bool pmf_global;
     91       1.2  jmcneill } pmf_event_handler_t;
     92       1.2  jmcneill 
     93       1.2  jmcneill static TAILQ_HEAD(, pmf_event_handler) pmf_all_events =
     94       1.2  jmcneill     TAILQ_HEAD_INITIALIZER(pmf_all_events);
     95       1.2  jmcneill 
     96       1.2  jmcneill typedef struct pmf_event_workitem {
     97       1.2  jmcneill 	struct work		pew_work;
     98       1.2  jmcneill 	pmf_generic_event_t	pew_event;
     99       1.2  jmcneill 	device_t		pew_device;
    100       1.2  jmcneill } pmf_event_workitem_t;
    101       1.2  jmcneill 
    102  1.12.6.1       mjf struct shutdown_state {
    103  1.12.6.1       mjf 	bool initialized;
    104  1.12.6.1       mjf 	deviter_t di;
    105  1.12.6.1       mjf };
    106  1.12.6.1       mjf 
    107  1.12.6.1       mjf static device_t shutdown_first(struct shutdown_state *);
    108  1.12.6.1       mjf static device_t shutdown_next(struct shutdown_state *);
    109  1.12.6.1       mjf 
    110  1.12.6.1       mjf static bool pmf_device_resume_locked(device_t PMF_FN_PROTO);
    111  1.12.6.1       mjf static bool pmf_device_suspend_locked(device_t PMF_FN_PROTO);
    112  1.12.6.1       mjf 
    113       1.2  jmcneill static void
    114       1.2  jmcneill pmf_event_worker(struct work *wk, void *dummy)
    115       1.2  jmcneill {
    116       1.2  jmcneill 	pmf_event_workitem_t *pew;
    117       1.2  jmcneill 	pmf_event_handler_t *event;
    118       1.2  jmcneill 
    119       1.2  jmcneill 	pew = (void *)wk;
    120       1.2  jmcneill 	KASSERT(wk == &pew->pew_work);
    121       1.2  jmcneill 	KASSERT(pew != NULL);
    122       1.2  jmcneill 
    123       1.2  jmcneill 	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
    124       1.2  jmcneill 		if (event->pmf_event != pew->pew_event)
    125       1.2  jmcneill 			continue;
    126       1.7  jmcneill 		if (event->pmf_device == pew->pew_device || event->pmf_global)
    127       1.2  jmcneill 			(*event->pmf_handler)(event->pmf_device);
    128       1.2  jmcneill 	}
    129       1.2  jmcneill 
    130       1.4  jmcneill 	free(pew, M_TEMP);
    131       1.2  jmcneill }
    132       1.2  jmcneill 
    133       1.2  jmcneill static bool
    134       1.2  jmcneill pmf_check_system_drivers(void)
    135       1.2  jmcneill {
    136       1.2  jmcneill 	device_t curdev;
    137       1.2  jmcneill 	bool unsupported_devs;
    138  1.12.6.1       mjf 	deviter_t di;
    139       1.2  jmcneill 
    140       1.2  jmcneill 	unsupported_devs = false;
    141  1.12.6.1       mjf 	for (curdev = deviter_first(&di, 0); curdev != NULL;
    142  1.12.6.1       mjf 	     curdev = deviter_next(&di)) {
    143       1.2  jmcneill 		if (device_pmf_is_registered(curdev))
    144       1.2  jmcneill 			continue;
    145       1.2  jmcneill 		if (!unsupported_devs)
    146       1.2  jmcneill 			printf("Devices without power management support:");
    147       1.2  jmcneill 		printf(" %s", device_xname(curdev));
    148       1.2  jmcneill 		unsupported_devs = true;
    149       1.2  jmcneill 	}
    150  1.12.6.1       mjf 	deviter_release(&di);
    151       1.2  jmcneill 	if (unsupported_devs) {
    152       1.2  jmcneill 		printf("\n");
    153       1.2  jmcneill 		return false;
    154       1.2  jmcneill 	}
    155       1.2  jmcneill 	return true;
    156       1.2  jmcneill }
    157       1.2  jmcneill 
    158       1.2  jmcneill bool
    159  1.12.6.1       mjf pmf_system_bus_resume(PMF_FN_ARGS1)
    160       1.6  jmcneill {
    161       1.6  jmcneill 	bool rv;
    162       1.6  jmcneill 	device_t curdev;
    163  1.12.6.1       mjf 	deviter_t di;
    164       1.6  jmcneill 
    165       1.6  jmcneill 	aprint_debug("Powering devices:");
    166       1.6  jmcneill 	/* D0 handlers are run in order */
    167       1.6  jmcneill 	rv = true;
    168  1.12.6.1       mjf 	for (curdev = deviter_first(&di, DEVITER_F_ROOT_FIRST); curdev != NULL;
    169  1.12.6.1       mjf 	     curdev = deviter_next(&di)) {
    170  1.12.6.1       mjf 		if (!device_pmf_is_registered(curdev))
    171  1.12.6.1       mjf 			continue;
    172  1.12.6.1       mjf 		if (device_is_active(curdev) ||
    173  1.12.6.1       mjf 		    !device_is_enabled(curdev))
    174  1.12.6.1       mjf 			continue;
    175       1.6  jmcneill 
    176  1.12.6.1       mjf 		aprint_debug(" %s", device_xname(curdev));
    177       1.6  jmcneill 
    178  1.12.6.1       mjf 		if (!device_pmf_bus_resume(curdev PMF_FN_CALL)) {
    179  1.12.6.1       mjf 			rv = false;
    180  1.12.6.1       mjf 			aprint_debug("(failed)");
    181       1.6  jmcneill 		}
    182       1.6  jmcneill 	}
    183  1.12.6.1       mjf 	deviter_release(&di);
    184       1.6  jmcneill 	aprint_debug("\n");
    185       1.6  jmcneill 
    186       1.6  jmcneill 	return rv;
    187       1.6  jmcneill }
    188       1.6  jmcneill 
    189       1.6  jmcneill bool
    190  1.12.6.1       mjf pmf_system_resume(PMF_FN_ARGS1)
    191       1.2  jmcneill {
    192       1.2  jmcneill 	bool rv;
    193       1.2  jmcneill 	device_t curdev, parent;
    194  1.12.6.1       mjf 	deviter_t di;
    195       1.2  jmcneill 
    196       1.2  jmcneill 	if (!pmf_check_system_drivers())
    197       1.2  jmcneill 		return false;
    198       1.2  jmcneill 
    199       1.2  jmcneill 	aprint_debug("Resuming devices:");
    200       1.2  jmcneill 	/* D0 handlers are run in order */
    201       1.2  jmcneill 	rv = true;
    202  1.12.6.1       mjf 	for (curdev = deviter_first(&di, DEVITER_F_ROOT_FIRST); curdev != NULL;
    203  1.12.6.1       mjf 	     curdev = deviter_next(&di)) {
    204  1.12.6.1       mjf 		if (device_is_active(curdev) ||
    205  1.12.6.1       mjf 		    !device_is_enabled(curdev))
    206  1.12.6.1       mjf 			continue;
    207  1.12.6.1       mjf 		parent = device_parent(curdev);
    208  1.12.6.1       mjf 		if (parent != NULL &&
    209  1.12.6.1       mjf 		    !device_is_active(parent))
    210  1.12.6.1       mjf 			continue;
    211  1.12.6.1       mjf 
    212  1.12.6.1       mjf 		aprint_debug(" %s", device_xname(curdev));
    213  1.12.6.1       mjf 
    214  1.12.6.1       mjf 		if (!pmf_device_resume(curdev PMF_FN_CALL)) {
    215  1.12.6.1       mjf 			rv = false;
    216  1.12.6.1       mjf 			aprint_debug("(failed)");
    217       1.2  jmcneill 		}
    218       1.2  jmcneill 	}
    219  1.12.6.1       mjf 	deviter_release(&di);
    220       1.2  jmcneill 	aprint_debug(".\n");
    221       1.2  jmcneill 
    222      1.12  drochner 	KERNEL_UNLOCK_ONE(0);
    223      1.12  drochner #if NWSDISPLAY > 0
    224      1.12  drochner 	if (rv)
    225      1.12  drochner 		wsdisplay_handlex(1);
    226      1.12  drochner #endif
    227       1.2  jmcneill 	return rv;
    228       1.2  jmcneill }
    229       1.2  jmcneill 
    230       1.2  jmcneill bool
    231  1.12.6.1       mjf pmf_system_suspend(PMF_FN_ARGS1)
    232       1.2  jmcneill {
    233       1.2  jmcneill 	device_t curdev;
    234  1.12.6.1       mjf 	deviter_t di;
    235       1.2  jmcneill 
    236       1.2  jmcneill 	if (!pmf_check_system_drivers())
    237       1.2  jmcneill 		return false;
    238      1.12  drochner #if NWSDISPLAY > 0
    239      1.12  drochner 	if (wsdisplay_handlex(0))
    240      1.12  drochner 		return false;
    241      1.12  drochner #endif
    242      1.12  drochner 	KERNEL_LOCK(1, 0);
    243       1.2  jmcneill 
    244       1.2  jmcneill 	/*
    245       1.2  jmcneill 	 * Flush buffers only if the shutdown didn't do so
    246       1.2  jmcneill 	 * already and if there was no panic.
    247       1.2  jmcneill 	 */
    248       1.2  jmcneill 	if (doing_shutdown == 0 && panicstr == NULL) {
    249       1.2  jmcneill 		printf("Flushing disk caches: ");
    250       1.2  jmcneill 		sys_sync(NULL, NULL, NULL);
    251       1.2  jmcneill 		if (buf_syncwait() != 0)
    252       1.2  jmcneill 			printf("giving up\n");
    253       1.2  jmcneill 		else
    254       1.2  jmcneill 			printf("done\n");
    255       1.2  jmcneill 	}
    256       1.2  jmcneill 
    257       1.2  jmcneill 	aprint_debug("Suspending devices:");
    258       1.2  jmcneill 
    259  1.12.6.1       mjf 	for (curdev = deviter_first(&di, DEVITER_F_LEAVES_FIRST);
    260  1.12.6.1       mjf 	     curdev != NULL;
    261  1.12.6.1       mjf 	     curdev = deviter_next(&di)) {
    262  1.12.6.1       mjf 		if (!device_is_active(curdev))
    263  1.12.6.1       mjf 			continue;
    264  1.12.6.1       mjf 
    265  1.12.6.1       mjf 		aprint_debug(" %s", device_xname(curdev));
    266  1.12.6.1       mjf 
    267  1.12.6.1       mjf 		/* XXX joerg check return value and abort suspend */
    268  1.12.6.1       mjf 		if (!pmf_device_suspend(curdev PMF_FN_CALL))
    269  1.12.6.1       mjf 			aprint_debug("(failed)");
    270       1.2  jmcneill 	}
    271  1.12.6.1       mjf 	deviter_release(&di);
    272       1.2  jmcneill 
    273       1.2  jmcneill 	aprint_debug(".\n");
    274       1.2  jmcneill 
    275       1.2  jmcneill 	return true;
    276       1.2  jmcneill }
    277       1.2  jmcneill 
    278  1.12.6.1       mjf static device_t
    279  1.12.6.1       mjf shutdown_first(struct shutdown_state *s)
    280  1.12.6.1       mjf {
    281  1.12.6.1       mjf 	if (!s->initialized) {
    282  1.12.6.1       mjf 		deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST);
    283  1.12.6.1       mjf 		s->initialized = true;
    284  1.12.6.1       mjf 	}
    285  1.12.6.1       mjf 	return shutdown_next(s);
    286  1.12.6.1       mjf }
    287  1.12.6.1       mjf 
    288  1.12.6.1       mjf static device_t
    289  1.12.6.1       mjf shutdown_next(struct shutdown_state *s)
    290  1.12.6.1       mjf {
    291  1.12.6.1       mjf 	device_t dv;
    292  1.12.6.1       mjf 
    293  1.12.6.1       mjf 	while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv))
    294  1.12.6.1       mjf 		;
    295  1.12.6.1       mjf 
    296  1.12.6.1       mjf 	return dv;
    297  1.12.6.1       mjf }
    298  1.12.6.1       mjf 
    299       1.2  jmcneill void
    300  1.12.6.1       mjf pmf_system_shutdown(int how)
    301       1.2  jmcneill {
    302  1.12.6.1       mjf 	static struct shutdown_state s;
    303       1.2  jmcneill 	device_t curdev;
    304       1.2  jmcneill 
    305       1.2  jmcneill 	aprint_debug("Shutting down devices:");
    306       1.2  jmcneill 
    307  1.12.6.1       mjf 	for (curdev = shutdown_first(&s); curdev != NULL;
    308  1.12.6.1       mjf 	     curdev = shutdown_next(&s)) {
    309  1.12.6.1       mjf 		aprint_debug(" attempting %s shutdown",
    310  1.12.6.1       mjf 		    device_xname(curdev));
    311  1.12.6.1       mjf 		if (!device_pmf_is_registered(curdev))
    312  1.12.6.1       mjf 			aprint_debug("(skipped)");
    313  1.12.6.1       mjf #if 0 /* needed? */
    314  1.12.6.1       mjf 		else if (!device_pmf_class_shutdown(curdev, how))
    315  1.12.6.1       mjf 			aprint_debug("(failed)");
    316  1.12.6.1       mjf #endif
    317  1.12.6.1       mjf 		else if (!device_pmf_driver_shutdown(curdev, how))
    318  1.12.6.1       mjf 			aprint_debug("(failed)");
    319  1.12.6.1       mjf 		else if (!device_pmf_bus_shutdown(curdev, how))
    320  1.12.6.1       mjf 			aprint_debug("(failed)");
    321       1.2  jmcneill 	}
    322       1.2  jmcneill 
    323       1.2  jmcneill 	aprint_debug(".\n");
    324       1.2  jmcneill }
    325       1.2  jmcneill 
    326       1.2  jmcneill bool
    327       1.2  jmcneill pmf_set_platform(const char *key, const char *value)
    328       1.2  jmcneill {
    329       1.2  jmcneill 	if (pmf_platform == NULL)
    330       1.2  jmcneill 		pmf_platform = prop_dictionary_create();
    331       1.2  jmcneill 	if (pmf_platform == NULL)
    332       1.2  jmcneill 		return false;
    333       1.2  jmcneill 
    334       1.2  jmcneill 	return prop_dictionary_set_cstring(pmf_platform, key, value);
    335       1.2  jmcneill }
    336       1.2  jmcneill 
    337       1.2  jmcneill const char *
    338       1.2  jmcneill pmf_get_platform(const char *key)
    339       1.2  jmcneill {
    340       1.2  jmcneill 	const char *value;
    341       1.2  jmcneill 
    342       1.2  jmcneill 	if (pmf_platform == NULL)
    343       1.2  jmcneill 		return NULL;
    344       1.2  jmcneill 
    345       1.2  jmcneill 	if (!prop_dictionary_get_cstring_nocopy(pmf_platform, key, &value))
    346       1.2  jmcneill 		return NULL;
    347       1.2  jmcneill 
    348       1.2  jmcneill 	return value;
    349       1.2  jmcneill }
    350       1.2  jmcneill 
    351       1.2  jmcneill bool
    352  1.12.6.1       mjf pmf_device_register1(device_t dev,
    353  1.12.6.1       mjf     bool (*suspend)(device_t PMF_FN_PROTO),
    354  1.12.6.1       mjf     bool (*resume)(device_t PMF_FN_PROTO),
    355  1.12.6.1       mjf     bool (*shutdown)(device_t, int))
    356       1.2  jmcneill {
    357  1.12.6.1       mjf 	if (!device_pmf_driver_register(dev, suspend, resume, shutdown))
    358  1.12.6.1       mjf 		return false;
    359       1.2  jmcneill 
    360       1.2  jmcneill 	if (!device_pmf_driver_child_register(dev)) {
    361       1.2  jmcneill 		device_pmf_driver_deregister(dev);
    362       1.2  jmcneill 		return false;
    363       1.2  jmcneill 	}
    364       1.2  jmcneill 
    365       1.2  jmcneill 	return true;
    366       1.2  jmcneill }
    367       1.2  jmcneill 
    368       1.2  jmcneill void
    369       1.2  jmcneill pmf_device_deregister(device_t dev)
    370       1.2  jmcneill {
    371       1.2  jmcneill 	device_pmf_class_deregister(dev);
    372       1.2  jmcneill 	device_pmf_bus_deregister(dev);
    373       1.2  jmcneill 	device_pmf_driver_deregister(dev);
    374       1.2  jmcneill }
    375       1.2  jmcneill 
    376       1.2  jmcneill bool
    377  1.12.6.1       mjf pmf_device_suspend_self(device_t dev)
    378       1.2  jmcneill {
    379  1.12.6.1       mjf 	return pmf_device_suspend(dev, PMF_F_SELF);
    380  1.12.6.1       mjf }
    381  1.12.6.1       mjf 
    382  1.12.6.1       mjf bool
    383  1.12.6.1       mjf pmf_device_suspend(device_t dev PMF_FN_ARGS)
    384  1.12.6.1       mjf {
    385  1.12.6.1       mjf 	bool rc;
    386  1.12.6.1       mjf 
    387       1.2  jmcneill 	PMF_TRANSITION_PRINTF(("%s: suspend enter\n", device_xname(dev)));
    388       1.2  jmcneill 	if (!device_pmf_is_registered(dev))
    389       1.2  jmcneill 		return false;
    390  1.12.6.1       mjf 
    391  1.12.6.1       mjf 	if (!device_pmf_lock(dev PMF_FN_CALL))
    392  1.12.6.1       mjf 		return false;
    393  1.12.6.1       mjf 
    394  1.12.6.1       mjf 	rc = pmf_device_suspend_locked(dev PMF_FN_CALL);
    395  1.12.6.1       mjf 
    396  1.12.6.1       mjf 	device_pmf_unlock(dev PMF_FN_CALL);
    397  1.12.6.1       mjf 
    398  1.12.6.1       mjf 	PMF_TRANSITION_PRINTF(("%s: suspend exit\n", device_xname(dev)));
    399  1.12.6.1       mjf 	return rc;
    400  1.12.6.1       mjf }
    401  1.12.6.1       mjf 
    402  1.12.6.1       mjf static bool
    403  1.12.6.1       mjf pmf_device_suspend_locked(device_t dev PMF_FN_ARGS)
    404  1.12.6.1       mjf {
    405  1.12.6.1       mjf 	PMF_TRANSITION_PRINTF2(1, ("%s: self suspend\n", device_xname(dev)));
    406  1.12.6.1       mjf 	device_pmf_self_suspend(dev, flags);
    407       1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: class suspend\n", device_xname(dev)));
    408  1.12.6.1       mjf 	if (!device_pmf_class_suspend(dev PMF_FN_CALL))
    409       1.2  jmcneill 		return false;
    410       1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: driver suspend\n", device_xname(dev)));
    411  1.12.6.1       mjf 	if (!device_pmf_driver_suspend(dev PMF_FN_CALL))
    412       1.2  jmcneill 		return false;
    413       1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: bus suspend\n", device_xname(dev)));
    414  1.12.6.1       mjf 	if (!device_pmf_bus_suspend(dev PMF_FN_CALL))
    415       1.2  jmcneill 		return false;
    416  1.12.6.1       mjf 
    417       1.2  jmcneill 	return true;
    418       1.2  jmcneill }
    419       1.2  jmcneill 
    420       1.2  jmcneill bool
    421  1.12.6.1       mjf pmf_device_resume_self(device_t dev)
    422       1.2  jmcneill {
    423  1.12.6.1       mjf 	return pmf_device_resume(dev, PMF_F_SELF);
    424  1.12.6.1       mjf }
    425  1.12.6.1       mjf 
    426  1.12.6.1       mjf bool
    427  1.12.6.1       mjf pmf_device_resume(device_t dev PMF_FN_ARGS)
    428  1.12.6.1       mjf {
    429  1.12.6.1       mjf 	bool rc;
    430  1.12.6.1       mjf 
    431       1.2  jmcneill 	PMF_TRANSITION_PRINTF(("%s: resume enter\n", device_xname(dev)));
    432       1.2  jmcneill 	if (!device_pmf_is_registered(dev))
    433       1.2  jmcneill 		return false;
    434  1.12.6.1       mjf 
    435  1.12.6.1       mjf 	if (!device_pmf_lock(dev PMF_FN_CALL))
    436  1.12.6.1       mjf 		return false;
    437  1.12.6.1       mjf 
    438  1.12.6.1       mjf 	rc = pmf_device_resume_locked(dev PMF_FN_CALL);
    439  1.12.6.1       mjf 
    440  1.12.6.1       mjf 	device_pmf_unlock(dev PMF_FN_CALL);
    441  1.12.6.1       mjf 
    442  1.12.6.1       mjf 	PMF_TRANSITION_PRINTF(("%s: resume exit\n", device_xname(dev)));
    443  1.12.6.1       mjf 	return rc;
    444  1.12.6.1       mjf }
    445  1.12.6.1       mjf 
    446  1.12.6.1       mjf static bool
    447  1.12.6.1       mjf pmf_device_resume_locked(device_t dev PMF_FN_ARGS)
    448  1.12.6.1       mjf {
    449       1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: bus resume\n", device_xname(dev)));
    450  1.12.6.1       mjf 	if (!device_pmf_bus_resume(dev PMF_FN_CALL))
    451       1.2  jmcneill 		return false;
    452       1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: driver resume\n", device_xname(dev)));
    453  1.12.6.1       mjf 	if (!device_pmf_driver_resume(dev PMF_FN_CALL))
    454       1.2  jmcneill 		return false;
    455       1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: class resume\n", device_xname(dev)));
    456  1.12.6.1       mjf 	if (!device_pmf_class_resume(dev PMF_FN_CALL))
    457       1.2  jmcneill 		return false;
    458  1.12.6.1       mjf 	PMF_TRANSITION_PRINTF2(1, ("%s: self resume\n", device_xname(dev)));
    459  1.12.6.1       mjf 	device_pmf_self_resume(dev, flags);
    460  1.12.6.1       mjf 
    461       1.2  jmcneill 	return true;
    462       1.2  jmcneill }
    463       1.2  jmcneill 
    464       1.2  jmcneill bool
    465  1.12.6.1       mjf pmf_device_recursive_suspend(device_t dv PMF_FN_ARGS)
    466       1.2  jmcneill {
    467  1.12.6.1       mjf 	bool rv = true;
    468       1.2  jmcneill 	device_t curdev;
    469  1.12.6.1       mjf 	deviter_t di;
    470       1.2  jmcneill 
    471       1.2  jmcneill 	if (!device_is_active(dv))
    472       1.2  jmcneill 		return true;
    473       1.2  jmcneill 
    474  1.12.6.1       mjf 	for (curdev = deviter_first(&di, 0); curdev != NULL;
    475  1.12.6.1       mjf 	     curdev = deviter_next(&di)) {
    476       1.2  jmcneill 		if (device_parent(curdev) != dv)
    477       1.2  jmcneill 			continue;
    478  1.12.6.1       mjf 		if (!pmf_device_recursive_suspend(curdev PMF_FN_CALL)) {
    479  1.12.6.1       mjf 			rv = false;
    480  1.12.6.1       mjf 			break;
    481  1.12.6.1       mjf 		}
    482       1.2  jmcneill 	}
    483  1.12.6.1       mjf 	deviter_release(&di);
    484       1.2  jmcneill 
    485  1.12.6.1       mjf 	return rv && pmf_device_suspend(dv PMF_FN_CALL);
    486       1.2  jmcneill }
    487       1.2  jmcneill 
    488       1.2  jmcneill bool
    489  1.12.6.1       mjf pmf_device_recursive_resume(device_t dv PMF_FN_ARGS)
    490       1.2  jmcneill {
    491       1.2  jmcneill 	device_t parent;
    492       1.2  jmcneill 
    493       1.2  jmcneill 	if (device_is_active(dv))
    494       1.2  jmcneill 		return true;
    495       1.2  jmcneill 
    496       1.2  jmcneill 	parent = device_parent(dv);
    497       1.2  jmcneill 	if (parent != NULL) {
    498  1.12.6.1       mjf 		if (!pmf_device_recursive_resume(parent PMF_FN_CALL))
    499       1.2  jmcneill 			return false;
    500       1.2  jmcneill 	}
    501       1.2  jmcneill 
    502  1.12.6.1       mjf 	return pmf_device_resume(dv PMF_FN_CALL);
    503       1.2  jmcneill }
    504       1.2  jmcneill 
    505       1.2  jmcneill bool
    506  1.12.6.1       mjf pmf_device_resume_subtree(device_t dv PMF_FN_ARGS)
    507       1.2  jmcneill {
    508  1.12.6.1       mjf 	bool rv = true;
    509       1.2  jmcneill 	device_t curdev;
    510  1.12.6.1       mjf 	deviter_t di;
    511       1.2  jmcneill 
    512  1.12.6.1       mjf 	if (!pmf_device_recursive_resume(dv PMF_FN_CALL))
    513       1.2  jmcneill 		return false;
    514       1.2  jmcneill 
    515  1.12.6.1       mjf 	for (curdev = deviter_first(&di, 0); curdev != NULL;
    516  1.12.6.1       mjf 	     curdev = deviter_next(&di)) {
    517       1.2  jmcneill 		if (device_parent(curdev) != dv)
    518       1.2  jmcneill 			continue;
    519  1.12.6.1       mjf 		if (!pmf_device_resume_subtree(curdev PMF_FN_CALL)) {
    520  1.12.6.1       mjf 			rv = false;
    521  1.12.6.1       mjf 			break;
    522  1.12.6.1       mjf 		}
    523       1.2  jmcneill 	}
    524  1.12.6.1       mjf 	deviter_release(&di);
    525  1.12.6.1       mjf 	return rv;
    526       1.2  jmcneill }
    527       1.2  jmcneill 
    528       1.2  jmcneill #include <net/if.h>
    529       1.2  jmcneill 
    530       1.2  jmcneill static bool
    531  1.12.6.1       mjf pmf_class_network_suspend(device_t dev PMF_FN_ARGS)
    532       1.2  jmcneill {
    533       1.2  jmcneill 	struct ifnet *ifp = device_pmf_class_private(dev);
    534       1.2  jmcneill 	int s;
    535       1.2  jmcneill 
    536       1.2  jmcneill 	s = splnet();
    537  1.12.6.1       mjf 	(*ifp->if_stop)(ifp, 0);
    538       1.2  jmcneill 	splx(s);
    539       1.2  jmcneill 
    540       1.2  jmcneill 	return true;
    541       1.2  jmcneill }
    542       1.2  jmcneill 
    543       1.2  jmcneill static bool
    544  1.12.6.1       mjf pmf_class_network_resume(device_t dev PMF_FN_ARGS)
    545       1.2  jmcneill {
    546       1.2  jmcneill 	struct ifnet *ifp = device_pmf_class_private(dev);
    547       1.2  jmcneill 	int s;
    548       1.2  jmcneill 
    549  1.12.6.1       mjf 	if ((flags & PMF_F_SELF) != 0)
    550  1.12.6.1       mjf 		return true;
    551  1.12.6.1       mjf 
    552       1.2  jmcneill 	s = splnet();
    553       1.2  jmcneill 	if (ifp->if_flags & IFF_UP) {
    554       1.2  jmcneill 		ifp->if_flags &= ~IFF_RUNNING;
    555       1.2  jmcneill 		(*ifp->if_init)(ifp);
    556       1.2  jmcneill 		(*ifp->if_start)(ifp);
    557       1.2  jmcneill 	}
    558       1.2  jmcneill 	splx(s);
    559       1.2  jmcneill 
    560       1.2  jmcneill 	return true;
    561       1.2  jmcneill }
    562       1.2  jmcneill 
    563       1.2  jmcneill void
    564       1.2  jmcneill pmf_class_network_register(device_t dev, struct ifnet *ifp)
    565       1.2  jmcneill {
    566       1.2  jmcneill 	device_pmf_class_register(dev, ifp, pmf_class_network_suspend,
    567       1.2  jmcneill 	    pmf_class_network_resume, NULL);
    568       1.2  jmcneill }
    569       1.2  jmcneill 
    570       1.2  jmcneill bool
    571       1.2  jmcneill pmf_event_inject(device_t dv, pmf_generic_event_t ev)
    572       1.2  jmcneill {
    573       1.2  jmcneill 	pmf_event_workitem_t *pew;
    574       1.2  jmcneill 
    575       1.4  jmcneill 	pew = malloc(sizeof(pmf_event_workitem_t), M_TEMP, M_NOWAIT);
    576       1.2  jmcneill 	if (pew == NULL) {
    577       1.2  jmcneill 		PMF_EVENT_PRINTF(("%s: PMF event %d dropped (no memory)\n",
    578       1.2  jmcneill 		    dv ? device_xname(dv) : "<anonymous>", ev));
    579       1.2  jmcneill 		return false;
    580       1.2  jmcneill 	}
    581       1.2  jmcneill 
    582       1.2  jmcneill 	pew->pew_event = ev;
    583       1.2  jmcneill 	pew->pew_device = dv;
    584       1.2  jmcneill 
    585       1.2  jmcneill 	workqueue_enqueue(pmf_event_workqueue, (void *)pew, NULL);
    586       1.2  jmcneill 	PMF_EVENT_PRINTF(("%s: PMF event %d injected\n",
    587       1.2  jmcneill 	    dv ? device_xname(dv) : "<anonymous>", ev));
    588       1.2  jmcneill 
    589       1.2  jmcneill 	return true;
    590       1.2  jmcneill }
    591       1.2  jmcneill 
    592       1.2  jmcneill bool
    593       1.2  jmcneill pmf_event_register(device_t dv, pmf_generic_event_t ev,
    594       1.2  jmcneill     void (*handler)(device_t), bool global)
    595       1.2  jmcneill {
    596       1.2  jmcneill 	pmf_event_handler_t *event;
    597       1.2  jmcneill 
    598       1.2  jmcneill 	event = malloc(sizeof(*event), M_DEVBUF, M_WAITOK);
    599       1.2  jmcneill 	event->pmf_event = ev;
    600       1.2  jmcneill 	event->pmf_handler = handler;
    601       1.2  jmcneill 	event->pmf_device = dv;
    602       1.2  jmcneill 	event->pmf_global = global;
    603       1.2  jmcneill 	TAILQ_INSERT_TAIL(&pmf_all_events, event, pmf_link);
    604       1.2  jmcneill 
    605       1.2  jmcneill 	return true;
    606       1.2  jmcneill }
    607       1.2  jmcneill 
    608       1.2  jmcneill void
    609       1.2  jmcneill pmf_event_deregister(device_t dv, pmf_generic_event_t ev,
    610       1.2  jmcneill     void (*handler)(device_t), bool global)
    611       1.2  jmcneill {
    612       1.2  jmcneill 	pmf_event_handler_t *event;
    613       1.2  jmcneill 
    614       1.9     rmind 	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
    615       1.2  jmcneill 		if (event->pmf_event != ev)
    616       1.2  jmcneill 			continue;
    617       1.2  jmcneill 		if (event->pmf_device != dv)
    618       1.2  jmcneill 			continue;
    619       1.2  jmcneill 		if (event->pmf_global != global)
    620       1.2  jmcneill 			continue;
    621       1.2  jmcneill 		if (event->pmf_handler != handler)
    622       1.2  jmcneill 			continue;
    623       1.2  jmcneill 		TAILQ_REMOVE(&pmf_all_events, event, pmf_link);
    624      1.11  jmcneill 		free(event, M_DEVBUF);
    625       1.9     rmind 		return;
    626       1.2  jmcneill 	}
    627       1.2  jmcneill }
    628       1.2  jmcneill 
    629       1.2  jmcneill struct display_class_softc {
    630       1.2  jmcneill 	TAILQ_ENTRY(display_class_softc) dc_link;
    631       1.2  jmcneill 	device_t dc_dev;
    632       1.2  jmcneill };
    633       1.2  jmcneill 
    634       1.2  jmcneill static TAILQ_HEAD(, display_class_softc) all_displays;
    635       1.2  jmcneill static callout_t global_idle_counter;
    636       1.2  jmcneill static int idle_timeout = 30;
    637       1.2  jmcneill 
    638       1.2  jmcneill static void
    639       1.2  jmcneill input_idle(void *dummy)
    640       1.2  jmcneill {
    641       1.2  jmcneill 	PMF_IDLE_PRINTF(("Input idle handler called\n"));
    642       1.2  jmcneill 	pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
    643       1.2  jmcneill }
    644       1.2  jmcneill 
    645       1.2  jmcneill static void
    646       1.2  jmcneill input_activity_handler(device_t dv, devactive_t type)
    647       1.2  jmcneill {
    648       1.2  jmcneill 	if (!TAILQ_EMPTY(&all_displays))
    649       1.2  jmcneill 		callout_schedule(&global_idle_counter, idle_timeout * hz);
    650       1.2  jmcneill }
    651       1.2  jmcneill 
    652       1.2  jmcneill static void
    653       1.2  jmcneill pmf_class_input_deregister(device_t dv)
    654       1.2  jmcneill {
    655       1.2  jmcneill 	device_active_deregister(dv, input_activity_handler);
    656       1.2  jmcneill }
    657       1.2  jmcneill 
    658       1.2  jmcneill bool
    659       1.2  jmcneill pmf_class_input_register(device_t dv)
    660       1.2  jmcneill {
    661       1.2  jmcneill 	if (!device_active_register(dv, input_activity_handler))
    662       1.2  jmcneill 		return false;
    663       1.2  jmcneill 
    664       1.2  jmcneill 	device_pmf_class_register(dv, NULL, NULL, NULL,
    665       1.2  jmcneill 	    pmf_class_input_deregister);
    666       1.2  jmcneill 
    667       1.2  jmcneill 	return true;
    668       1.2  jmcneill }
    669       1.2  jmcneill 
    670       1.2  jmcneill static void
    671       1.2  jmcneill pmf_class_display_deregister(device_t dv)
    672       1.2  jmcneill {
    673       1.2  jmcneill 	struct display_class_softc *sc = device_pmf_class_private(dv);
    674       1.2  jmcneill 	int s;
    675       1.2  jmcneill 
    676       1.2  jmcneill 	s = splsoftclock();
    677       1.2  jmcneill 	TAILQ_REMOVE(&all_displays, sc, dc_link);
    678       1.2  jmcneill 	if (TAILQ_EMPTY(&all_displays))
    679       1.2  jmcneill 		callout_stop(&global_idle_counter);
    680       1.2  jmcneill 	splx(s);
    681       1.2  jmcneill 
    682       1.2  jmcneill 	free(sc, M_DEVBUF);
    683       1.2  jmcneill }
    684       1.2  jmcneill 
    685       1.2  jmcneill bool
    686       1.2  jmcneill pmf_class_display_register(device_t dv)
    687       1.2  jmcneill {
    688       1.2  jmcneill 	struct display_class_softc *sc;
    689       1.2  jmcneill 	int s;
    690       1.2  jmcneill 
    691       1.2  jmcneill 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
    692       1.2  jmcneill 
    693       1.2  jmcneill 	s = splsoftclock();
    694       1.2  jmcneill 	if (TAILQ_EMPTY(&all_displays))
    695       1.2  jmcneill 		callout_schedule(&global_idle_counter, idle_timeout * hz);
    696       1.2  jmcneill 
    697       1.2  jmcneill 	TAILQ_INSERT_HEAD(&all_displays, sc, dc_link);
    698       1.2  jmcneill 	splx(s);
    699       1.2  jmcneill 
    700       1.2  jmcneill 	device_pmf_class_register(dv, sc, NULL, NULL,
    701       1.2  jmcneill 	    pmf_class_display_deregister);
    702       1.2  jmcneill 
    703       1.2  jmcneill 	return true;
    704       1.2  jmcneill }
    705       1.2  jmcneill 
    706       1.2  jmcneill void
    707       1.2  jmcneill pmf_init(void)
    708       1.2  jmcneill {
    709       1.2  jmcneill 	int err;
    710       1.2  jmcneill 
    711       1.2  jmcneill 	KASSERT(pmf_event_workqueue == NULL);
    712       1.2  jmcneill 	err = workqueue_create(&pmf_event_workqueue, "pmfevent",
    713       1.5  jmcneill 	    pmf_event_worker, NULL, PRI_NONE, IPL_VM, 0);
    714       1.2  jmcneill 	if (err)
    715       1.2  jmcneill 		panic("couldn't create pmfevent workqueue");
    716       1.2  jmcneill 
    717       1.2  jmcneill 	callout_init(&global_idle_counter, 0);
    718       1.2  jmcneill 	callout_setfunc(&global_idle_counter, input_idle, NULL);
    719       1.2  jmcneill }
    720