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