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