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