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