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