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