]> git.saurik.com Git - apple/xnu.git/blob - iokit/Drivers/platform/drvAppleRootDomain/RootDomain.cpp
ebc0a8b23f0484cbfab1af002ec9711da383de45
[apple/xnu.git] / iokit / Drivers / platform / drvAppleRootDomain / RootDomain.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 #include <IOKit/IOWorkLoop.h>
23 #include <IOKit/IOCommandQueue.h>
24 #include <IOKit/IOTimerEventSource.h>
25 #include <IOKit/IOPlatformExpert.h>
26 #include <IOKit/pwr_mgt/RootDomain.h>
27 #include <IOKit/pwr_mgt/IOPM.h>
28 #include <IOKit/IOMessage.h>
29 #include "RootDomainUserClient.h"
30 #include "IOKit/pwr_mgt/IOPowerConnection.h"
31
32 extern "C" {
33 extern void kprintf(const char *, ...);
34 }
35
36 extern const IORegistryPlane * gIOPowerPlane;
37
38 void PMreceiveCmd ( OSObject *, void *, void *, void *, void * );
39 static void sleepTimerExpired(thread_call_param_t);
40
41
42 #define number_of_power_states 5
43 #define OFF_STATE 0
44 #define RESTART_STATE 1
45 #define SLEEP_STATE 2
46 #define DOZE_STATE 3
47 #define ON_STATE 4
48
49 #define ON_POWER kIOPMPowerOn
50 #define RESTART_POWER kIOPMRestart
51 #define SLEEP_POWER kIOPMAuxPowerOn
52 #define DOZE_POWER kIOPMDoze
53
54 static IOPMPowerState ourPowerStates[number_of_power_states] = {
55 {1,0, 0, 0,0,0,0,0,0,0,0,0}, // state 0, off
56 {1,kIOPMRestartCapability, kIOPMRestart, RESTART_POWER,0,0,0,0,0,0,0,0}, // state 1, restart
57 {1,kIOPMSleepCapability, kIOPMSleep, SLEEP_POWER,0,0,0,0,0,0,0,0}, // state 2, sleep
58 {1,kIOPMDoze, kIOPMDoze, DOZE_POWER,0,0,0,0,0,0,0,0}, // state 3, doze
59 {1,kIOPMPowerOn, kIOPMPowerOn, ON_POWER,0,0,0,0,0,0,0,0}, // state 4, on
60 };
61
62 static IOPMrootDomain * gRootDomain;
63 static UInt32 gSleepOrShutdownPending = 0;
64
65
66 #define super IOService
67 OSDefineMetaClassAndStructors(IOPMrootDomain,IOService)
68
69 extern "C"
70 {
71 IONotifier * registerSleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref = 0)
72 {
73 return gRootDomain->registerInterest( gIOGeneralInterest, handler, self, ref );
74 }
75
76 IONotifier * registerPrioritySleepWakeInterest(IOServiceInterestHandler handler, void * self, void * ref = 0)
77 {
78 return gRootDomain->registerInterest( gIOPriorityPowerStateInterest, handler, self, ref );
79 }
80
81 IOReturn acknowledgeSleepWakeNotification(void * PMrefcon)
82 {
83 return gRootDomain->allowPowerChange ( (unsigned long)PMrefcon );
84 }
85
86 IOReturn vetoSleepWakeNotification(void * PMrefcon)
87 {
88 return gRootDomain->cancelPowerChange ( (unsigned long)PMrefcon );
89 }
90
91 IOReturn rootDomainRestart ( void )
92 {
93 return gRootDomain->restartSystem();
94 }
95
96 IOReturn rootDomainShutdown ( void )
97 {
98 return gRootDomain->shutdownSystem();
99 }
100
101 void IOSystemShutdownNotification ( void )
102 {
103 for ( int i = 0; i < 100; i++ )
104 {
105 if ( OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending ) ) break;
106 IOSleep( 100 );
107 }
108 }
109
110 int sync_internal(void);
111 }
112
113 /*
114 A device is always in the highest power state which satisfies its driver, its policy-maker, and any power domain
115 children it has, but within the constraint of the power state provided by its parent. The driver expresses its desire by
116 calling changePowerStateTo(), the policy-maker expresses its desire by calling changePowerStateToPriv(), and the children
117 express their desires by calling requestPowerDomainState().
118
119 The Root Power Domain owns the policy for idle and demand sleep and doze for the system. It is a power-managed IOService just
120 like the others in the system. It implements several power states which correspond to what we see as Sleep, Doze, etc.
121
122 The sleep/doze policy is as follows:
123 Sleep and Doze are prevented if the case is open so that nobody will think the machine is off and plug/unplug cards.
124 Sleep and Doze are prevented if the sleep timeout slider in the preferences panel is at zero.
125 The system cannot Sleep, but can Doze if some object in the tree is in a power state marked kIOPMPreventSystemSleep.
126
127 These three conditions are enforced using the "driver clamp" by calling changePowerStateTo(). For example, if the case is
128 opened, changePowerStateTo(ON_STATE) is called to hold the system on regardless of the desires of the children of the root or
129 the state of the other clamp.
130
131 Demand Sleep/Doze is initiated by pressing the front panel power button, closing the clamshell, or selecting the menu item.
132 In this case the root's parent actually initiates the power state change so that the root has no choice and does not give
133 applications the opportunity to veto the change.
134
135 Idle Sleep/Doze occurs if no objects in the tree are in a state marked kIOPMPreventIdleSleep. When this is true, the root's
136 children are not holding the root on, so it sets the "policy-maker clamp" by calling changePowerStateToPriv(ON_STATE)
137 to hold itself on until the sleep timer expires. This timer is set for the difference between the sleep timeout slider and
138 the larger of the display dim timeout slider and the disk spindown timeout slider in the Preferences panel. For example, if
139 the system is set to sleep after thirty idle minutes, and the display and disk are set to sleep after five idle minutes,
140 when there is no longer an object in the tree holding the system out of Idle Sleep (via kIOPMPreventIdleSleep), the root
141 sets its timer for 25 minutes (30 - 5). When the timer expires, it releases its clamp and now nothing is holding it awake,
142 so it falls asleep.
143
144 Demand sleep is prevented when the system is booting. When preferences are transmitted by the loginwindow at the end of
145 boot, a flag is cleared, and this allows subsequent Demand Sleep.
146
147 The system will not Sleep, but will Doze if some object calls setSleepSupported(kPCICantSleep) during a power change to the sleep state (this can be done by the PCI Aux Power Supply drivers, Slots99, MacRISC299, etc.). This is not enforced with
148 a clamp, but sets a flag which is noticed before actually sleeping the kernel. If the flag is set, the root steps up
149 one power state from Sleep to Doze, and any objects in the tree for which this is relevent will act appropriately (USB and
150 ADB will turn on again so that they can wake the system out of Doze (keyboard/mouse activity will cause the Display Wrangler
151 to be tickled)).
152 */
153
154
155 // **********************************************************************************
156
157 IOPMrootDomain * IOPMrootDomain::construct( void )
158 {
159 IOPMrootDomain * root;
160
161 root = new IOPMrootDomain;
162 if( root)
163 root->init();
164
165 return( root );
166 }
167
168 // **********************************************************************************
169
170 static void disk_sync_callout(thread_call_param_t p0, thread_call_param_t p1)
171 {
172 IOService * rootDomain = (IOService *) p0;
173 unsigned long pmRef = (unsigned long) p1;
174
175 sync_internal();
176 rootDomain->allowPowerChange(pmRef);
177 }
178
179 // **********************************************************************************
180 // start
181 //
182 // We don't do much here. The real initialization occurs when the platform
183 // expert informs us we are the root.
184 // **********************************************************************************
185
186
187 bool IOPMrootDomain::start ( IOService * nub )
188 {
189 super::start(nub);
190
191 gRootDomain = this;
192
193 PMinit();
194 setProperty("IOSleepSupported","");
195 allowSleep = true;
196 sleepIsSupported = true;
197 systemBooting = true;
198 ignoringClamshell = true;
199 sleepSlider = 0;
200 idleSleepPending = false;
201 canSleep = true;
202 wrangler = NULL;
203 sleepASAP = false;
204
205 pm_vars->PMworkloop = IOWorkLoop::workLoop(); // make the workloop
206 pm_vars->commandQueue = IOCommandQueue::commandQueue(this, PMreceiveCmd); // make a command queue
207 if (! pm_vars->commandQueue ||
208 ( pm_vars->PMworkloop->addEventSource( pm_vars->commandQueue) != kIOReturnSuccess) ) {
209 return IOPMNoErr;
210 }
211 extraSleepTimer = thread_call_allocate((thread_call_func_t)sleepTimerExpired, (thread_call_param_t) this);
212
213 diskSyncCalloutEntry = thread_call_allocate(&disk_sync_callout, (thread_call_param_t) this);
214
215 patriarch = new IORootParent; // create our parent
216 patriarch->init();
217 patriarch->attach(this);
218 patriarch->start(this);
219 patriarch->youAreRoot();
220 patriarch->wakeSystem();
221 patriarch->addPowerChild(this);
222
223 registerPowerDriver(this,ourPowerStates,number_of_power_states);
224
225 setPMRootDomain(this);
226 changePowerStateToPriv(ON_STATE); // set a clamp until we sleep
227
228 registerPrioritySleepWakeInterest( &sysPowerDownHandler, this, 0); // install power change handler
229
230 // Register for a notification when IODisplayWrangler is published
231 addNotification( gIOPublishNotification, serviceMatching("IODisplayWrangler"), &displayWranglerPublished, this, 0);
232
233 registerService(); // let clients find us
234
235 return true;
236 }
237
238
239 //*********************************************************************************
240 // youAreRoot
241 //
242 // Power Managment is informing us that we are the root power domain.
243 // We know we are not the root however, since we have just instantiated a parent
244 // for ourselves and made it the root. We override this method so it will have
245 // no effect
246 //*********************************************************************************
247 IOReturn IOPMrootDomain::youAreRoot ( void )
248 {
249 return IOPMNoErr;
250 }
251
252 // **********************************************************************************
253 // command_received
254 //
255 // We have received a command from ourselves on the command queue.
256 // If it is to send a recently-received aggressiveness factor, do so.
257 // Otherwise, it's something the superclass enqueued.
258 // **********************************************************************************
259 void IOPMrootDomain::command_received ( void * command, void * x, void * y, void * z )
260 {
261 switch ( (int)command ) {
262 case kIOPMBroadcastAggressiveness:
263
264 super::setAggressiveness((unsigned long)x,(unsigned long)y);
265
266 // Save user's spin down timer to restore after we replace it for idle sleep
267 if( (int)x == kPMMinutesToSpinDown ) user_spindown = (unsigned int) y;
268
269 // Use longestNonSleepSlider to calculate dimming adjust idle sleep timer
270 longestNonSleepSlider = pm_vars->current_aggressiveness_values[kPMMinutesToDim];
271
272
273 if ( (int)x == kPMMinutesToSleep ) {
274 if ( (sleepSlider == 0) && ((int)y != 0) ) {
275 sleepSlider = (int)y;
276 adjustPowerState(); // idle sleep is now enabled, maybe sleep now
277 }
278 sleepSlider = (int)y;
279 if ( sleepSlider == 0 ) {
280 adjustPowerState(); // idle sleep is now disabled
281 patriarch->wakeSystem(); // make sure we're powered
282 }
283 }
284 if ( sleepSlider > longestNonSleepSlider ) {
285 extraSleepDelay = sleepSlider - longestNonSleepSlider ;
286 }
287 else {
288 extraSleepDelay = 0;
289 }
290 break;
291
292 default:
293 super::command_received(command,x,y,z);
294 break;
295 }
296 }
297
298
299 // **********************************************************************************
300 // sleepTimerExpired
301 //
302 // **********************************************************************************
303 static void sleepTimerExpired ( thread_call_param_t us)
304 {
305 ((IOPMrootDomain *)us)->handleSleepTimerExpiration();
306 }
307
308
309 // **********************************************************************************
310 // handleSleepTimerExpiration
311 //
312 // The time between the sleep idle timeout and the next longest one has elapsed.
313 // It's time to sleep. Start that by removing the clamp that's holding us awake.
314 // **********************************************************************************
315 void IOPMrootDomain::handleSleepTimerExpiration ( void )
316 {
317 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
318 if(0 != user_spindown)
319 setQuickSpinDownTimeout();
320
321 sleepASAP = true;
322 adjustPowerState();
323 }
324
325
326 //*********************************************************************************
327 // setAggressiveness
328 //
329 // Some aggressiveness factor has changed. We put this change on our
330 // command queue so that we can broadcast it to the hierarchy while on
331 // the Power Mangement workloop thread. This enables objects in the
332 // hierarchy to successfully alter their idle timers, which are all on the
333 // same thread.
334 //*********************************************************************************
335
336 IOReturn IOPMrootDomain::setAggressiveness ( unsigned long type, unsigned long newLevel )
337 {
338 if ( systemBooting && (type == kPMMinutesToDim) ) {
339 systemBooting = false; // when the login window launches, this method gets called -- system booting is done.
340 IOLog("Root power domain receiving initial preferences\n");
341 adjustPowerState();
342 }
343
344 pm_vars->commandQueue->enqueueCommand(true, (void *)kIOPMBroadcastAggressiveness, (void *) type, (void *) newLevel );
345
346 return kIOReturnSuccess;
347 }
348
349
350 // **********************************************************************************
351 // sleepSystem
352 //
353 // **********************************************************************************
354 IOReturn IOPMrootDomain::sleepSystem ( void )
355 {
356 kprintf("sleep demand received\n");
357 if ( !systemBooting && allowSleep && sleepIsSupported ) {
358 patriarch->sleepSystem();
359 return kIOReturnSuccess;
360 }
361 if ( !systemBooting && allowSleep && !sleepIsSupported ) {
362 patriarch->dozeSystem();
363 return kIOReturnSuccess;
364 }
365 return kIOReturnSuccess;
366 }
367
368
369 // **********************************************************************************
370 // shutdownSystem
371 //
372 // **********************************************************************************
373 IOReturn IOPMrootDomain::shutdownSystem ( void )
374 {
375 patriarch->shutDownSystem();
376 return kIOReturnSuccess;
377 }
378
379
380 // **********************************************************************************
381 // restartSystem
382 //
383 // **********************************************************************************
384 IOReturn IOPMrootDomain::restartSystem ( void )
385 {
386 patriarch->restartSystem();
387 return kIOReturnSuccess;
388 }
389
390
391 // **********************************************************************************
392 // powerChangeDone
393 //
394 // This overrides powerChangeDone in IOService.
395 //
396 // Finder sleep and idle sleep move us from the ON state to the SLEEP_STATE.
397 // In this case:
398 // If we just finished going to the SLEEP_STATE, and the platform is capable of true sleep,
399 // sleep the kernel. Otherwise switch up to the DOZE_STATE which will keep almost
400 // everything as off as it can get.
401 //
402 // **********************************************************************************
403 void IOPMrootDomain::powerChangeDone ( unsigned long previousState )
404 {
405 OSNumber * propertyPtr;
406 unsigned short theProperty;
407 AbsoluteTime deadline;
408
409 switch ( pm_vars->myCurrentState ) {
410 case SLEEP_STATE:
411 if ( canSleep && sleepIsSupported ) {
412 idleSleepPending = false; // re-enable this timer for next sleep
413 IOLog("System Sleep\n");
414 pm_vars->thePlatform->sleepKernel(); // sleep now
415
416 // now we're waking
417 clock_interval_to_deadline(30, kSecondScale, &deadline); // stay awake for at least 30 seconds
418 thread_call_enter_delayed(extraSleepTimer, deadline);
419 idleSleepPending = true; // this gets turned off when we sleep again
420 gSleepOrShutdownPending = 0; // sleep transition complete
421 patriarch->wakeSystem(); // get us some power
422
423 IOLog("System Wake\n");
424 systemWake(); // tell the tree we're waking
425
426 propertyPtr = OSDynamicCast(OSNumber,getProperty("WakeEvent"));
427 if ( propertyPtr ) { // find out what woke us
428 theProperty = propertyPtr->unsigned16BitValue();
429 IOLog("Wake event %04x\n",theProperty);
430 if ( (theProperty == 0x0008) || //lid
431 (theProperty == 0x0800) || // front panel button
432 (theProperty == 0x0020) || // external keyboard
433 (theProperty == 0x0001) ) { // internal keyboard
434 reportUserInput();
435 }
436 }
437 else {
438 IOLog("Unknown wake event\n");
439 reportUserInput(); // don't know, call it user input then
440 }
441
442 changePowerStateToPriv(ON_STATE); // wake for thirty seconds
443 powerOverrideOffPriv();
444 }
445 else {
446 patriarch->sleepToDoze(); // allow us to step up a power state
447 changePowerStateToPriv(DOZE_STATE); // and do it
448 }
449 break;
450
451 case DOZE_STATE:
452 if ( previousState != DOZE_STATE ) {
453 IOLog("System Doze\n");
454 }
455 idleSleepPending = false; // re-enable this timer for next sleep
456 gSleepOrShutdownPending = 0;
457 break;
458
459 case RESTART_STATE:
460 IOLog("System Restart\n");
461 PEHaltRestart(kPERestartCPU);
462 break;
463
464 case OFF_STATE:
465 IOLog("System Halt\n");
466 PEHaltRestart(kPEHaltCPU);
467 break;
468 }
469 }
470
471
472 // **********************************************************************************
473 // wakeFromDoze
474 //
475 // The Display Wrangler calls here when it switches to its highest state. If the
476 // system is currently dozing, allow it to wake by making sure the parent is
477 // providing power.
478 // **********************************************************************************
479 void IOPMrootDomain::wakeFromDoze( void )
480 {
481 if ( pm_vars->myCurrentState == DOZE_STATE ) {
482 canSleep = true; // reset this till next attempt
483 powerOverrideOffPriv();
484 patriarch->wakeSystem(); // allow us to wake if children so desire
485 }
486 }
487
488
489 // **********************************************************************************
490 // newUserClient
491 //
492 // **********************************************************************************
493 IOReturn IOPMrootDomain::newUserClient( task_t owningTask, void * /* security_id */, UInt32 type, IOUserClient ** handler )
494 {
495 IOReturn err = kIOReturnSuccess;
496 RootDomainUserClient * client;
497
498 client = RootDomainUserClient::withTask(owningTask);
499
500 if( !client || (false == client->attach( this )) ||
501 (false == client->start( this )) ) {
502 if(client) {
503 client->detach( this );
504 client->release();
505 client = NULL;
506 }
507 err = kIOReturnNoMemory;
508 }
509 *handler = client;
510 return err;
511 }
512
513 //*********************************************************************************
514 // receivePowerNotification
515 //
516 // The power controller is notifying us of a hardware-related power management
517 // event that we must handle. This is a result of an 'environment' interrupt from
518 // the power mgt micro.
519 //*********************************************************************************
520
521 IOReturn IOPMrootDomain::receivePowerNotification (UInt32 msg)
522 {
523 if (msg & kIOPMSleepNow) {
524 (void) sleepSystem ();
525 }
526
527 if (msg & kIOPMPowerEmergency) {
528 (void) sleepSystem ();
529 }
530
531 if (msg & kIOPMClamshellClosed) {
532 if ( ! ignoringClamshell ) {
533 (void) sleepSystem ();
534 }
535 }
536
537 if (msg & kIOPMEnableClamshell) {
538 ignoringClamshell = false;
539 }
540 if (msg & kIOPMDisableClamshell) {
541 ignoringClamshell = true;
542 }
543
544 if (msg & kIOPMPowerButton) { // toggle state of sleep/wake
545 if ( pm_vars->myCurrentState == DOZE_STATE ) { // are we dozing?
546 systemWake(); // yes, tell the tree we're waking
547 reportUserInput(); // wake the Display Wrangler
548 }
549 else {
550 (void) sleepSystem ();
551 }
552 }
553
554 // if the case has been closed, we allow
555 // the machine to be put to sleep or to idle sleep
556
557 if ( (msg & kIOPMAllowSleep) && !allowSleep ) {
558 allowSleep = true;
559 adjustPowerState();
560 }
561
562 // if the case has been opened, we disallow sleep/doze
563
564 if (msg & kIOPMPreventSleep) {
565 allowSleep = false;
566 if ( pm_vars->myCurrentState == DOZE_STATE ) { // are we dozing?
567 systemWake(); // yes, tell the tree we're waking
568 adjustPowerState();
569 reportUserInput(); // wake the Display Wrangler
570 }
571 else {
572 adjustPowerState();
573 patriarch->wakeSystem(); // make sure we have power to clamp
574 }
575 }
576
577 return 0;
578 }
579
580
581 //*********************************************************************************
582 // sleepSupported
583 //
584 //*********************************************************************************
585
586 void IOPMrootDomain::setSleepSupported( IOOptionBits flags )
587 {
588 if ( flags & kPCICantSleep ) {
589 canSleep = false;
590 }
591 else {
592 platformSleepSupport = flags;
593 }
594
595 }
596
597 //*********************************************************************************
598 // requestPowerDomainState
599 //
600 // The root domain intercepts this call to the superclass.
601 //
602 // If the clamp bit is not set in the desire, then the child doesn't need the power
603 // state it's requesting; it just wants it. The root ignores desires but not needs.
604 // If the clamp bit is not set, the root takes it that the child can tolerate no
605 // power and interprets the request accordingly. If all children can thus tolerate
606 // no power, we are on our way to idle sleep.
607 //*********************************************************************************
608
609 IOReturn IOPMrootDomain::requestPowerDomainState ( IOPMPowerFlags desiredState, IOPowerConnection * whichChild, unsigned long specification )
610 {
611 OSIterator * iter;
612 OSObject * next;
613 IOPowerConnection * connection;
614 unsigned long powerRequestFlag = 0;
615 IOPMPowerFlags editedDesire = desiredState;
616
617 if ( !(desiredState & kIOPMPreventIdleSleep) ) { // if they don't really need it, they don't get it
618 editedDesire = 0;
619 }
620
621
622 IOLockLock(pm_vars->childLock); // recompute sleepIsSupported
623 // and see if all children are asleep
624 iter = getChildIterator(gIOPowerPlane);
625 sleepIsSupported = true;
626
627 if ( iter ) {
628 while ( (next = iter->getNextObject()) ) {
629 if ( (connection = OSDynamicCast(IOPowerConnection,next)) ) {
630 if ( connection == whichChild ) {
631 powerRequestFlag += editedDesire;
632 if ( desiredState & kIOPMPreventSystemSleep ) {
633 sleepIsSupported = false;
634 }
635 }
636 else {
637 powerRequestFlag += connection->getDesiredDomainState();
638 if ( connection->getPreventSystemSleepFlag() ) {
639 sleepIsSupported = false;
640 }
641 }
642 }
643 }
644 iter->release();
645 }
646
647 if ( (extraSleepDelay == 0) && (powerRequestFlag == 0) ) {
648 sleepASAP = true;
649 }
650
651 adjustPowerState(); // this may put the system to sleep
652
653 IOLockUnlock(pm_vars->childLock);
654
655 editedDesire |= desiredState & kIOPMPreventSystemSleep;
656
657 return super::requestPowerDomainState(editedDesire,whichChild,specification);
658 }
659
660
661 //*********************************************************************************
662 // getSleepSupported
663 //
664 //*********************************************************************************
665
666 IOOptionBits IOPMrootDomain::getSleepSupported( void )
667 {
668 return( platformSleepSupport );
669 }
670
671
672 //*********************************************************************************
673 // tellChangeDown
674 //
675 // We override the superclass implementation so we can send a different message
676 // type to the client or application being notified.
677 //*********************************************************************************
678
679 bool IOPMrootDomain::tellChangeDown ( unsigned long stateNum )
680 {
681 switch ( stateNum ) {
682 case DOZE_STATE:
683 case SLEEP_STATE:
684 return super::tellClientsWithResponse(kIOMessageSystemWillSleep);
685 case RESTART_STATE:
686 return super::tellClientsWithResponse(kIOMessageSystemWillRestart);
687 case OFF_STATE:
688 return super::tellClientsWithResponse(kIOMessageSystemWillPowerOff);
689 }
690 return super::tellChangeDown(stateNum); // this shouldn't execute
691 }
692
693
694 //*********************************************************************************
695 // askChangeDown
696 //
697 // We override the superclass implementation so we can send a different message
698 // type to the client or application being notified.
699 //
700 // This must be idle sleep since we don't ask apps during any other power change.
701 //*********************************************************************************
702
703 bool IOPMrootDomain::askChangeDown ( unsigned long )
704 {
705 return super::tellClientsWithResponse(kIOMessageCanSystemSleep);
706 }
707
708
709 //*********************************************************************************
710 // tellNoChangeDown
711 //
712 // Notify registered applications and kernel clients that we are not
713 // dropping power.
714 //
715 // We override the superclass implementation so we can send a different message
716 // type to the client or application being notified.
717 //
718 // This must be a vetoed idle sleep, since no other power change can be vetoed.
719 //*********************************************************************************
720
721 void IOPMrootDomain::tellNoChangeDown ( unsigned long )
722 {
723 return tellClients(kIOMessageSystemWillNotSleep);
724 }
725
726
727 //*********************************************************************************
728 // tellChangeUp
729 //
730 // Notify registered applications and kernel clients that we are raising power.
731 //
732 // We override the superclass implementation so we can send a different message
733 // type to the client or application being notified.
734 //*********************************************************************************
735
736 void IOPMrootDomain::tellChangeUp ( unsigned long stateNum)
737 {
738 if ( stateNum == ON_STATE ) {
739 return tellClients(kIOMessageSystemHasPoweredOn);
740 }
741 }
742
743 //*********************************************************************************
744 // reportUserInput
745 //
746 //*********************************************************************************
747
748 void IOPMrootDomain::reportUserInput ( void )
749 {
750 OSIterator * iter;
751
752 if(!wrangler) {
753 iter = getMatchingServices(serviceMatching("IODisplayWrangler"));
754 if(iter) {
755 wrangler = (IOService *) iter->getNextObject();
756 iter->release();
757 }
758 }
759
760 if(wrangler)
761 wrangler->activityTickle(0,0);
762 }
763
764 //*********************************************************************************
765 // setQuickSpinDownTimeout
766 //
767 //*********************************************************************************
768
769 void IOPMrootDomain::setQuickSpinDownTimeout ( void )
770 {
771 //IOLog("setQuickSpinDownTimeout\n");
772 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown,(unsigned long)1);
773 }
774
775 //*********************************************************************************
776 // restoreUserSpinDownTimeout
777 //
778 //*********************************************************************************
779
780 void IOPMrootDomain::restoreUserSpinDownTimeout ( void )
781 {
782 if(systemBooting) {
783 IOLog("!!!!! WARNING !!!!! restoreUserSpinDownTimeout called too early\n");
784 }
785 //IOLog("restoreUserSpinDownTimeout, user_spindown = %u\n", user_spindown);
786
787 super::setAggressiveness((unsigned long)kPMMinutesToSpinDown,(unsigned long)user_spindown);
788 }
789
790
791 //*********************************************************************************
792 // sysPowerDownHandler
793 //
794 // Receives a notification when the RootDomain changes state.
795 //
796 // Allows us to take action on system sleep, power down, and restart after
797 // applications have received their power change notifications and replied,
798 // but before drivers have powered down. We perform a vfs sync on power down.
799 //*********************************************************************************
800
801 IOReturn IOPMrootDomain::sysPowerDownHandler( void * target, void * refCon,
802 UInt32 messageType, IOService * service,
803 void * messageArgument, vm_size_t argSize )
804 {
805 IOReturn ret;
806 IOPowerStateChangeNotification * params = (IOPowerStateChangeNotification *) messageArgument;
807 IOPMrootDomain * rootDomain = OSDynamicCast(IOPMrootDomain, service);
808
809 if(!rootDomain)
810 return kIOReturnUnsupported;
811
812 switch (messageType) {
813 case kIOMessageSystemWillSleep:
814 rootDomain->powerOverrideOnPriv(); // start ignoring children's requests
815 // (fall through to other cases)
816
817 // Interested applications have been notified of an impending power
818 // change and have acked (when applicable).
819 // This is our chance to save whatever state we can before powering
820 // down.
821 // We call sync_internal defined in xnu/bsd/vfs/vfs_syscalls.c,
822 // via callout
823
824 // We will ack within 20 seconds
825 params->returnValue = 20 * 1000 * 1000;
826
827 if ( ! OSCompareAndSwap( 0, 1, &gSleepOrShutdownPending ) )
828 {
829 // Purposely delay the ack and hope that shutdown occurs quickly.
830 // Another option is not to schedule the thread and wait for
831 // ack timeout...
832 AbsoluteTime deadline;
833 clock_interval_to_deadline( 15, kSecondScale, &deadline );
834 thread_call_enter1_delayed( rootDomain->diskSyncCalloutEntry,
835 (thread_call_param_t)params->powerRef,
836 deadline );
837 }
838 else
839 thread_call_enter1(rootDomain->diskSyncCalloutEntry, (thread_call_param_t)params->powerRef);
840 ret = kIOReturnSuccess;
841 break;
842
843 case kIOMessageSystemWillPowerOff:
844 case kIOMessageSystemWillRestart:
845 break;
846
847 default:
848 ret = kIOReturnUnsupported;
849 break;
850 }
851 return ret;
852 }
853
854 //*********************************************************************************
855 // displayWranglerNotification
856 //
857 // Receives a notification when the IODisplayWrangler changes state.
858 //
859 // Allows us to take action on display dim/undim.
860 //
861 // When the display goes dim we:
862 // - Start the idle sleep timer
863 // - set the quick spin down timeout
864 //
865 // On wake from display dim:
866 // - Cancel the idle sleep timer
867 // - restore the user's chosen spindown timer from the "quick" spin down value
868 //*********************************************************************************
869
870 IOReturn IOPMrootDomain::displayWranglerNotification( void * target, void * refCon,
871 UInt32 messageType, IOService * service,
872 void * messageArgument, vm_size_t argSize )
873 {
874 IOPMrootDomain * rootDomain = OSDynamicCast(IOPMrootDomain, (IOService *)target);
875 AbsoluteTime deadline;
876 static bool deviceAlreadyPoweredOff = false;
877
878 if(!rootDomain)
879 return kIOReturnUnsupported;
880
881 switch (messageType) {
882 case kIOMessageDeviceWillPowerOff:
883 // The IODisplayWrangler has powered off either because of idle display sleep
884 // or force system sleep.
885
886 // The display wrangler will send the DeviceWillPowerOff message 4 times until
887 // it gets into its lowest state. We only want to act on the first of those 4.
888 if( deviceAlreadyPoweredOff ) return kIOReturnUnsupported;
889
890 deviceAlreadyPoweredOff = true;
891
892 if( rootDomain->extraSleepDelay ) {
893
894 // start the extra sleep timer
895 clock_interval_to_deadline(rootDomain->extraSleepDelay*60, kSecondScale, &deadline );
896 thread_call_enter_delayed(rootDomain->extraSleepTimer, deadline);
897 rootDomain->idleSleepPending = true;
898
899 } else {
900
901 // accelerate disk spin down if spin down timer is non-zero (zero = never spin down)
902 if(0 != rootDomain->user_spindown)
903 rootDomain->setQuickSpinDownTimeout();
904 }
905
906 break;
907
908 case kIOMessageDeviceHasPoweredOn:
909
910 // The display has powered on either because of UI activity or wake from sleep/doze
911 deviceAlreadyPoweredOff = false;
912 rootDomain->adjustPowerState();
913
914
915 // cancel any pending idle sleep
916 if(rootDomain->idleSleepPending) {
917 thread_call_cancel(rootDomain->extraSleepTimer);
918 rootDomain->idleSleepPending = false;
919 }
920
921 // Change the spindown value back to the user's selection from our accelerated setting
922 if(0 != rootDomain->user_spindown)
923 rootDomain->restoreUserSpinDownTimeout();
924
925 // Put on the policy maker's on clamp.
926
927 break;
928
929 default:
930 break;
931 }
932 return kIOReturnUnsupported;
933 }
934
935 //*********************************************************************************
936 // displayWranglerPublished
937 //
938 // Receives a notification when the IODisplayWrangler is published.
939 // When it's published we install a power state change handler.
940 //
941 //*********************************************************************************
942
943 bool IOPMrootDomain::displayWranglerPublished( void * target, void * refCon,
944 IOService * newService)
945 {
946 IOPMrootDomain * rootDomain = OSDynamicCast(IOPMrootDomain, (IOService *)target);
947
948 if(!rootDomain)
949 return false;
950
951 rootDomain->wrangler = newService;
952
953 // we found the display wrangler, now install a handler
954 if( !rootDomain->wrangler->registerInterest( gIOGeneralInterest, &displayWranglerNotification, target, 0) ) {
955 IOLog("IOPMrootDomain::displayWranglerPublished registerInterest failed\n");
956 return false;
957 }
958
959 return true;
960 }
961
962
963 //*********************************************************************************
964 // adjustPowerState
965 //
966 // Some condition that affects our wake/sleep/doze decision has changed.
967 //
968 // If the sleep slider is in the off position, we cannot sleep or doze.
969 // If the enclosure is open, we cannot sleep or doze.
970 // If the system is still booting, we cannot sleep or doze.
971 //
972 // In those circumstances, we prevent sleep and doze by holding power on with
973 // changePowerStateToPriv(ON).
974 //
975 // If the above conditions do not exist, and also the sleep timer has expired, we
976 // allow sleep or doze to occur with either changePowerStateToPriv(SLEEP) or
977 // changePowerStateToPriv(DOZE) depending on whether or not we already know the
978 // platform cannot sleep.
979 //
980 // In this case, sleep or doze will either occur immediately or at the next time
981 // that no children are holding the system out of idle sleep via the
982 // kIOPMPreventIdleSleep flag in their power state arrays.
983 //*********************************************************************************
984
985 void IOPMrootDomain::adjustPowerState( void )
986 {
987 if ( (sleepSlider == 0) ||
988 ! allowSleep ||
989 systemBooting ) {
990 changePowerStateToPriv(ON_STATE);
991 }
992 else {
993 if ( sleepASAP ) {
994 sleepASAP = false;
995 if ( sleepIsSupported ) {
996 changePowerStateToPriv(SLEEP_STATE);
997 }
998 else {
999 changePowerStateToPriv(DOZE_STATE);
1000 }
1001 }
1002 }
1003 }
1004
1005
1006 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1007
1008 #undef super
1009 #define super IOService
1010
1011 OSDefineMetaClassAndStructors(IORootParent, IOService)
1012
1013 // This array exactly parallels the state array for the root domain.
1014 // Power state changes initiated by a device can be vetoed by a client of the device, and
1015 // power state changes initiated by the parent of a device cannot be vetoed by a client of the device,
1016 // so when the root domain wants a power state change that cannot be vetoed (e.g. demand sleep), it asks
1017 // its parent to make the change. That is the reason for this complexity.
1018
1019 static IOPMPowerState patriarchPowerStates[number_of_power_states] = {
1020 {1,0,0,0,0,0,0,0,0,0,0,0}, // off
1021 {1,0,RESTART_POWER,0,0,0,0,0,0,0,0,0}, // reset
1022 {1,0,SLEEP_POWER,0,0,0,0,0,0,0,0,0}, // sleep
1023 {1,0,DOZE_POWER,0,0,0,0,0,0,0,0,0}, // doze
1024 {1,0,ON_POWER,0,0,0,0,0,0,0,0,0} // running
1025 };
1026
1027 bool IORootParent::start ( IOService * nub )
1028 {
1029 mostRecentChange = ON_STATE;
1030 super::start(nub);
1031 PMinit();
1032 registerPowerDriver(this,patriarchPowerStates,number_of_power_states);
1033 powerOverrideOnPriv();
1034 return true;
1035 }
1036
1037
1038 void IORootParent::shutDownSystem ( void )
1039 {
1040 mostRecentChange = OFF_STATE;
1041 changePowerStateToPriv(OFF_STATE);
1042 }
1043
1044
1045 void IORootParent::restartSystem ( void )
1046 {
1047 mostRecentChange = RESTART_STATE;
1048 changePowerStateToPriv(RESTART_STATE);
1049 }
1050
1051
1052 void IORootParent::sleepSystem ( void )
1053 {
1054 mostRecentChange = SLEEP_STATE;
1055 changePowerStateToPriv(SLEEP_STATE);
1056 }
1057
1058
1059 void IORootParent::dozeSystem ( void )
1060 {
1061 mostRecentChange = DOZE_STATE;
1062 changePowerStateToPriv(DOZE_STATE);
1063 }
1064
1065 // Called in demand sleep when sleep discovered to be impossible after actually attaining that state.
1066 // This brings the parent to doze, which allows the root to step up from sleep to doze.
1067
1068 // In idle sleep, do nothing because the parent is still on and the root can freely change state.
1069
1070 void IORootParent::sleepToDoze ( void )
1071 {
1072 if ( mostRecentChange == SLEEP_STATE ) {
1073 changePowerStateToPriv(DOZE_STATE);
1074 }
1075 }
1076
1077
1078 void IORootParent::wakeSystem ( void )
1079 {
1080 mostRecentChange = ON_STATE;
1081 changePowerStateToPriv(ON_STATE);
1082 }
1083