kern_pmf.c revision 1.16 1 /* $NetBSD: kern_pmf.c,v 1.16 2008/03/07 07:03:06 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.16 2008/03/07 07:03:06 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 #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(device_t dev PMF_FN_ARGS)
380 {
381 bool rc;
382
383 PMF_TRANSITION_PRINTF(("%s: suspend enter\n", device_xname(dev)));
384 if (!device_pmf_is_registered(dev))
385 return false;
386
387 if (!device_pmf_lock(dev PMF_FN_CALL))
388 return false;
389
390 rc = pmf_device_suspend_locked(dev PMF_FN_CALL);
391
392 device_pmf_unlock(dev PMF_FN_CALL);
393
394 PMF_TRANSITION_PRINTF(("%s: suspend exit\n", device_xname(dev)));
395 return rc;
396 }
397
398 static bool
399 pmf_device_suspend_locked(device_t dev PMF_FN_ARGS)
400 {
401 PMF_TRANSITION_PRINTF2(1, ("%s: class suspend\n", device_xname(dev)));
402 if (!device_pmf_class_suspend(dev PMF_FN_CALL))
403 return false;
404 PMF_TRANSITION_PRINTF2(1, ("%s: driver suspend\n", device_xname(dev)));
405 if (!device_pmf_driver_suspend(dev PMF_FN_CALL))
406 return false;
407 PMF_TRANSITION_PRINTF2(1, ("%s: bus suspend\n", device_xname(dev)));
408 if (!device_pmf_bus_suspend(dev PMF_FN_CALL))
409 return false;
410
411 return true;
412 }
413
414 bool
415 pmf_device_resume(device_t dev PMF_FN_ARGS)
416 {
417 bool rc;
418
419 PMF_TRANSITION_PRINTF(("%s: resume enter\n", device_xname(dev)));
420 if (!device_pmf_is_registered(dev))
421 return false;
422
423 if (!device_pmf_lock(dev PMF_FN_CALL))
424 return false;
425
426 rc = pmf_device_resume_locked(dev PMF_FN_CALL);
427
428 device_pmf_unlock(dev PMF_FN_CALL);
429
430 PMF_TRANSITION_PRINTF(("%s: resume exit\n", device_xname(dev)));
431 return rc;
432 }
433
434 static bool
435 pmf_device_resume_locked(device_t dev PMF_FN_ARGS)
436 {
437 PMF_TRANSITION_PRINTF2(1, ("%s: bus resume\n", device_xname(dev)));
438 if (!device_pmf_bus_resume(dev PMF_FN_CALL))
439 return false;
440 PMF_TRANSITION_PRINTF2(1, ("%s: driver resume\n", device_xname(dev)));
441 if (!device_pmf_driver_resume(dev PMF_FN_CALL))
442 return false;
443 PMF_TRANSITION_PRINTF2(1, ("%s: class resume\n", device_xname(dev)));
444 if (!device_pmf_class_resume(dev PMF_FN_CALL))
445 return false;
446 return true;
447 }
448
449 bool
450 pmf_device_recursive_suspend(device_t dv PMF_FN_ARGS)
451 {
452 bool rv = true;
453 device_t curdev;
454 deviter_t di;
455
456 if (!device_is_active(dv))
457 return true;
458
459 for (curdev = deviter_first(&di, 0); curdev != NULL;
460 curdev = deviter_next(&di)) {
461 if (device_parent(curdev) != dv)
462 continue;
463 if (!pmf_device_recursive_suspend(curdev PMF_FN_CALL)) {
464 rv = false;
465 break;
466 }
467 }
468 deviter_release(&di);
469
470 return rv && pmf_device_suspend(dv PMF_FN_CALL);
471 }
472
473 bool
474 pmf_device_recursive_resume(device_t dv PMF_FN_ARGS)
475 {
476 device_t parent;
477
478 if (device_is_active(dv))
479 return true;
480
481 parent = device_parent(dv);
482 if (parent != NULL) {
483 if (!pmf_device_recursive_resume(parent PMF_FN_CALL))
484 return false;
485 }
486
487 return pmf_device_resume(dv PMF_FN_CALL);
488 }
489
490 bool
491 pmf_device_resume_subtree(device_t dv PMF_FN_ARGS)
492 {
493 bool rv = true;
494 device_t curdev;
495 deviter_t di;
496
497 if (!pmf_device_recursive_resume(dv PMF_FN_CALL))
498 return false;
499
500 for (curdev = deviter_first(&di, 0); curdev != NULL;
501 curdev = deviter_next(&di)) {
502 if (device_parent(curdev) != dv)
503 continue;
504 if (!pmf_device_resume_subtree(curdev PMF_FN_CALL)) {
505 rv = false;
506 break;
507 }
508 }
509 deviter_release(&di);
510 return rv;
511 }
512
513 #include <net/if.h>
514
515 static bool
516 pmf_class_network_suspend(device_t dev PMF_FN_ARGS)
517 {
518 struct ifnet *ifp = device_pmf_class_private(dev);
519 int s;
520
521 s = splnet();
522 (*ifp->if_stop)(ifp, 1);
523 splx(s);
524
525 return true;
526 }
527
528 static bool
529 pmf_class_network_resume(device_t dev PMF_FN_ARGS)
530 {
531 struct ifnet *ifp = device_pmf_class_private(dev);
532 int s;
533
534 s = splnet();
535 if (ifp->if_flags & IFF_UP) {
536 ifp->if_flags &= ~IFF_RUNNING;
537 (*ifp->if_init)(ifp);
538 (*ifp->if_start)(ifp);
539 }
540 splx(s);
541
542 return true;
543 }
544
545 void
546 pmf_class_network_register(device_t dev, struct ifnet *ifp)
547 {
548 device_pmf_class_register(dev, ifp, pmf_class_network_suspend,
549 pmf_class_network_resume, NULL);
550 }
551
552 bool
553 pmf_event_inject(device_t dv, pmf_generic_event_t ev)
554 {
555 pmf_event_workitem_t *pew;
556
557 pew = malloc(sizeof(pmf_event_workitem_t), M_TEMP, M_NOWAIT);
558 if (pew == NULL) {
559 PMF_EVENT_PRINTF(("%s: PMF event %d dropped (no memory)\n",
560 dv ? device_xname(dv) : "<anonymous>", ev));
561 return false;
562 }
563
564 pew->pew_event = ev;
565 pew->pew_device = dv;
566
567 workqueue_enqueue(pmf_event_workqueue, (void *)pew, NULL);
568 PMF_EVENT_PRINTF(("%s: PMF event %d injected\n",
569 dv ? device_xname(dv) : "<anonymous>", ev));
570
571 return true;
572 }
573
574 bool
575 pmf_event_register(device_t dv, pmf_generic_event_t ev,
576 void (*handler)(device_t), bool global)
577 {
578 pmf_event_handler_t *event;
579
580 event = malloc(sizeof(*event), M_DEVBUF, M_WAITOK);
581 event->pmf_event = ev;
582 event->pmf_handler = handler;
583 event->pmf_device = dv;
584 event->pmf_global = global;
585 TAILQ_INSERT_TAIL(&pmf_all_events, event, pmf_link);
586
587 return true;
588 }
589
590 void
591 pmf_event_deregister(device_t dv, pmf_generic_event_t ev,
592 void (*handler)(device_t), bool global)
593 {
594 pmf_event_handler_t *event;
595
596 TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
597 if (event->pmf_event != ev)
598 continue;
599 if (event->pmf_device != dv)
600 continue;
601 if (event->pmf_global != global)
602 continue;
603 if (event->pmf_handler != handler)
604 continue;
605 TAILQ_REMOVE(&pmf_all_events, event, pmf_link);
606 free(event, M_DEVBUF);
607 return;
608 }
609 }
610
611 struct display_class_softc {
612 TAILQ_ENTRY(display_class_softc) dc_link;
613 device_t dc_dev;
614 };
615
616 static TAILQ_HEAD(, display_class_softc) all_displays;
617 static callout_t global_idle_counter;
618 static int idle_timeout = 30;
619
620 static void
621 input_idle(void *dummy)
622 {
623 PMF_IDLE_PRINTF(("Input idle handler called\n"));
624 pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
625 }
626
627 static void
628 input_activity_handler(device_t dv, devactive_t type)
629 {
630 if (!TAILQ_EMPTY(&all_displays))
631 callout_schedule(&global_idle_counter, idle_timeout * hz);
632 }
633
634 static void
635 pmf_class_input_deregister(device_t dv)
636 {
637 device_active_deregister(dv, input_activity_handler);
638 }
639
640 bool
641 pmf_class_input_register(device_t dv)
642 {
643 if (!device_active_register(dv, input_activity_handler))
644 return false;
645
646 device_pmf_class_register(dv, NULL, NULL, NULL,
647 pmf_class_input_deregister);
648
649 return true;
650 }
651
652 static void
653 pmf_class_display_deregister(device_t dv)
654 {
655 struct display_class_softc *sc = device_pmf_class_private(dv);
656 int s;
657
658 s = splsoftclock();
659 TAILQ_REMOVE(&all_displays, sc, dc_link);
660 if (TAILQ_EMPTY(&all_displays))
661 callout_stop(&global_idle_counter);
662 splx(s);
663
664 free(sc, M_DEVBUF);
665 }
666
667 bool
668 pmf_class_display_register(device_t dv)
669 {
670 struct display_class_softc *sc;
671 int s;
672
673 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
674
675 s = splsoftclock();
676 if (TAILQ_EMPTY(&all_displays))
677 callout_schedule(&global_idle_counter, idle_timeout * hz);
678
679 TAILQ_INSERT_HEAD(&all_displays, sc, dc_link);
680 splx(s);
681
682 device_pmf_class_register(dv, sc, NULL, NULL,
683 pmf_class_display_deregister);
684
685 return true;
686 }
687
688 void
689 pmf_init(void)
690 {
691 int err;
692
693 KASSERT(pmf_event_workqueue == NULL);
694 err = workqueue_create(&pmf_event_workqueue, "pmfevent",
695 pmf_event_worker, NULL, PRI_NONE, IPL_VM, 0);
696 if (err)
697 panic("couldn't create pmfevent workqueue");
698
699 callout_init(&global_idle_counter, 0);
700 callout_setfunc(&global_idle_counter, input_idle, NULL);
701 }
702